Fix PR 93568 (thinko)
[official-gcc.git] / gcc / cp / decl2.c
blob98d8e6a6b53f998a66e6352d58d5529cfc98b28e
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2020 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"
52 /* Id for dumping the raw trees. */
53 int raw_dump_id;
55 extern cpp_reader *parse_in;
57 /* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59 typedef struct priority_info_s {
60 /* Nonzero if there have been any initializations at this priority
61 throughout the translation unit. */
62 int initializations_p;
63 /* Nonzero if there have been any destructions at this priority
64 throughout the translation unit. */
65 int destructions_p;
66 } *priority_info;
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static void maybe_instantiate_decl (tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec<tree, va_gc> *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec<tree, va_gc> *deferred_fns;
97 /* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
101 /* A vector of alternating decls and identifiers, where the latter
102 is to be an alias for the former if the former is defined. */
103 static GTY(()) vec<tree, va_gc> *mangling_aliases;
105 /* hash traits for declarations. Hashes single decls via
106 DECL_ASSEMBLER_NAME_RAW. */
108 struct mangled_decl_hash : ggc_remove <tree>
110 typedef tree value_type; /* A DECL. */
111 typedef tree compare_type; /* An identifier. */
113 static hashval_t hash (const value_type decl)
115 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
117 static bool equal (const value_type existing, compare_type candidate)
119 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
120 return candidate == name;
123 static const bool empty_zero_p = true;
124 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
125 static inline bool is_empty (value_type p) {return !p;}
127 static bool is_deleted (value_type e)
129 return e == reinterpret_cast <value_type> (1);
131 static void mark_deleted (value_type &e)
133 e = reinterpret_cast <value_type> (1);
137 /* A hash table of decls keyed by mangled name. Used to figure out if
138 we need compatibility aliases. */
139 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
141 /* Nonzero if we're done parsing and into end-of-file activities. */
143 int at_eof;
145 /* True if note_mangling_alias should enqueue mangling aliases for
146 later generation, rather than emitting them right away. */
148 bool defer_mangling_aliases = true;
151 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
152 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
153 that apply to the function). */
155 tree
156 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
157 cp_ref_qualifier rqual)
159 if (fntype == error_mark_node || ctype == error_mark_node)
160 return error_mark_node;
162 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
164 cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
165 ctype = cp_build_qualified_type (ctype, type_quals);
167 tree newtype
168 = build_method_type_directly (ctype, TREE_TYPE (fntype),
169 (TREE_CODE (fntype) == METHOD_TYPE
170 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
171 : TYPE_ARG_TYPES (fntype)));
172 if (tree attrs = TYPE_ATTRIBUTES (fntype))
173 newtype = cp_build_type_attribute_variant (newtype, attrs);
174 newtype = build_cp_fntype_variant (newtype, rqual,
175 TYPE_RAISES_EXCEPTIONS (fntype),
176 TYPE_HAS_LATE_RETURN_TYPE (fntype));
178 return newtype;
181 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
182 return type changed to NEW_RET. */
184 tree
185 change_return_type (tree new_ret, tree fntype)
187 if (new_ret == error_mark_node)
188 return fntype;
190 if (same_type_p (new_ret, TREE_TYPE (fntype)))
191 return fntype;
193 tree newtype;
194 tree args = TYPE_ARG_TYPES (fntype);
196 if (TREE_CODE (fntype) == FUNCTION_TYPE)
198 newtype = build_function_type (new_ret, args);
199 newtype = apply_memfn_quals (newtype,
200 type_memfn_quals (fntype));
202 else
203 newtype = build_method_type_directly
204 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
206 if (tree attrs = TYPE_ATTRIBUTES (fntype))
207 newtype = cp_build_type_attribute_variant (newtype, attrs);
208 newtype = cxx_copy_lang_qualifiers (newtype, fntype);
210 return newtype;
213 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
214 appropriately. */
216 tree
217 cp_build_parm_decl (tree fn, tree name, tree type)
219 tree parm = build_decl (input_location,
220 PARM_DECL, name, type);
221 DECL_CONTEXT (parm) = fn;
223 /* DECL_ARG_TYPE is only used by the back end and the back end never
224 sees templates. */
225 if (!processing_template_decl)
226 DECL_ARG_TYPE (parm) = type_passed_as (type);
228 return parm;
231 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
232 indicated NAME. */
234 tree
235 build_artificial_parm (tree fn, tree name, tree type)
237 tree parm = cp_build_parm_decl (fn, name, type);
238 DECL_ARTIFICIAL (parm) = 1;
239 /* All our artificial parms are implicitly `const'; they cannot be
240 assigned to. */
241 TREE_READONLY (parm) = 1;
242 return parm;
245 /* Constructors for types with virtual baseclasses need an "in-charge" flag
246 saying whether this constructor is responsible for initialization of
247 virtual baseclasses or not. All destructors also need this "in-charge"
248 flag, which additionally determines whether or not the destructor should
249 free the memory for the object.
251 This function adds the "in-charge" flag to member function FN if
252 appropriate. It is called from grokclassfn and tsubst.
253 FN must be either a constructor or destructor.
255 The in-charge flag follows the 'this' parameter, and is followed by the
256 VTT parm (if any), then the user-written parms. */
258 void
259 maybe_retrofit_in_chrg (tree fn)
261 tree basetype, arg_types, parms, parm, fntype;
263 /* If we've already add the in-charge parameter don't do it again. */
264 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
265 return;
267 /* When processing templates we can't know, in general, whether or
268 not we're going to have virtual baseclasses. */
269 if (processing_template_decl)
270 return;
272 /* We don't need an in-charge parameter for constructors that don't
273 have virtual bases. */
274 if (DECL_CONSTRUCTOR_P (fn)
275 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
276 return;
278 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
279 basetype = TREE_TYPE (TREE_VALUE (arg_types));
280 arg_types = TREE_CHAIN (arg_types);
282 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
284 /* If this is a subobject constructor or destructor, our caller will
285 pass us a pointer to our VTT. */
286 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
288 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
290 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
291 DECL_CHAIN (parm) = parms;
292 parms = parm;
294 /* ...and then to TYPE_ARG_TYPES. */
295 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
297 DECL_HAS_VTT_PARM_P (fn) = 1;
300 /* Then add the in-charge parm (before the VTT parm). */
301 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
302 DECL_CHAIN (parm) = parms;
303 parms = parm;
304 arg_types = hash_tree_chain (integer_type_node, arg_types);
306 /* Insert our new parameter(s) into the list. */
307 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
309 /* And rebuild the function type. */
310 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
311 arg_types);
312 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
313 fntype = (cp_build_type_attribute_variant
314 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
315 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
316 TREE_TYPE (fn) = fntype;
318 /* Now we've got the in-charge parameter. */
319 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
322 /* Classes overload their constituent function names automatically.
323 When a function name is declared in a record structure,
324 its name is changed to it overloaded name. Since names for
325 constructors and destructors can conflict, we place a leading
326 '$' for destructors.
328 CNAME is the name of the class we are grokking for.
330 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
332 FLAGS contains bits saying what's special about today's
333 arguments. DTOR_FLAG == DESTRUCTOR.
335 If FUNCTION is a destructor, then we must add the `auto-delete' field
336 as a second parameter. There is some hair associated with the fact
337 that we must "declare" this variable in the manner consistent with the
338 way the rest of the arguments were declared.
340 QUALS are the qualifiers for the this pointer. */
342 void
343 grokclassfn (tree ctype, tree function, enum overload_flags flags)
345 tree fn_name = DECL_NAME (function);
347 /* Even within an `extern "C"' block, members get C++ linkage. See
348 [dcl.link] for details. */
349 SET_DECL_LANGUAGE (function, lang_cplusplus);
351 if (fn_name == NULL_TREE)
353 error ("name missing for member function");
354 fn_name = get_identifier ("<anonymous>");
355 DECL_NAME (function) = fn_name;
358 DECL_CONTEXT (function) = ctype;
360 if (flags == DTOR_FLAG)
361 DECL_CXX_DESTRUCTOR_P (function) = 1;
363 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
364 maybe_retrofit_in_chrg (function);
367 /* Create an ARRAY_REF, checking for the user doing things backwards
368 along the way. DECLTYPE_P is for N3276, as in the parser. */
370 tree
371 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
372 bool decltype_p)
374 tree type;
375 tree expr;
376 tree orig_array_expr = array_expr;
377 tree orig_index_exp = index_exp;
378 tree overload = NULL_TREE;
380 if (error_operand_p (array_expr) || error_operand_p (index_exp))
381 return error_mark_node;
383 if (processing_template_decl)
385 if (type_dependent_expression_p (array_expr)
386 || type_dependent_expression_p (index_exp))
387 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
388 NULL_TREE, NULL_TREE);
389 array_expr = build_non_dependent_expr (array_expr);
390 index_exp = build_non_dependent_expr (index_exp);
393 type = TREE_TYPE (array_expr);
394 gcc_assert (type);
395 type = non_reference (type);
397 /* If they have an `operator[]', use that. */
398 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
400 tsubst_flags_t complain = tf_warning_or_error;
401 if (decltype_p)
402 complain |= tf_decltype;
403 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
404 index_exp, NULL_TREE, &overload, complain);
406 else
408 tree p1, p2, i1, i2;
409 bool swapped = false;
411 /* Otherwise, create an ARRAY_REF for a pointer or array type.
412 It is a little-known fact that, if `a' is an array and `i' is
413 an int, you can write `i[a]', which means the same thing as
414 `a[i]'. */
415 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
416 p1 = array_expr;
417 else
418 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
420 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
421 p2 = index_exp;
422 else
423 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
425 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
426 false);
427 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
428 false);
430 if ((p1 && i2) && (i1 && p2))
431 error ("ambiguous conversion for array subscript");
433 if (p1 && i2)
434 array_expr = p1, index_exp = i2;
435 else if (i1 && p2)
436 swapped = true, array_expr = p2, index_exp = i1;
437 else
439 error_at (loc, "invalid types %<%T[%T]%> for array subscript",
440 type, TREE_TYPE (index_exp));
441 return error_mark_node;
444 if (array_expr == error_mark_node || index_exp == error_mark_node)
445 error ("ambiguous conversion for array subscript");
447 if (TYPE_PTR_P (TREE_TYPE (array_expr)))
448 array_expr = mark_rvalue_use (array_expr);
449 else
450 array_expr = mark_lvalue_use_nonread (array_expr);
451 index_exp = mark_rvalue_use (index_exp);
452 if (swapped
453 && flag_strong_eval_order == 2
454 && (TREE_SIDE_EFFECTS (array_expr) || TREE_SIDE_EFFECTS (index_exp)))
455 expr = build_array_ref (input_location, index_exp, array_expr);
456 else
457 expr = build_array_ref (input_location, array_expr, index_exp);
459 if (processing_template_decl && expr != error_mark_node)
461 if (overload != NULL_TREE)
462 return (build_min_non_dep_op_overload
463 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
465 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
466 NULL_TREE, NULL_TREE);
468 return expr;
471 /* Given the cast expression EXP, checking out its validity. Either return
472 an error_mark_node if there was an unavoidable error, return a cast to
473 void for trying to delete a pointer w/ the value 0, or return the
474 call to delete. If DOING_VEC is true, we handle things differently
475 for doing an array delete.
476 Implements ARM $5.3.4. This is called from the parser. */
478 tree
479 delete_sanity (location_t loc, tree exp, tree size, bool doing_vec,
480 int use_global_delete, tsubst_flags_t complain)
482 tree t, type;
484 if (exp == error_mark_node)
485 return exp;
487 if (processing_template_decl)
489 t = build_min (DELETE_EXPR, void_type_node, exp, size);
490 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
491 DELETE_EXPR_USE_VEC (t) = doing_vec;
492 TREE_SIDE_EFFECTS (t) = 1;
493 SET_EXPR_LOCATION (t, loc);
494 return t;
497 location_t exp_loc = cp_expr_loc_or_loc (exp, loc);
499 /* An array can't have been allocated by new, so complain. */
500 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
501 && (complain & tf_warning))
502 warning_at (exp_loc, 0, "deleting array %q#E", exp);
504 t = build_expr_type_conversion (WANT_POINTER, exp, true);
506 if (t == NULL_TREE || t == error_mark_node)
508 if (complain & tf_error)
509 error_at (exp_loc,
510 "type %q#T argument given to %<delete%>, expected pointer",
511 TREE_TYPE (exp));
512 return error_mark_node;
515 type = TREE_TYPE (t);
517 /* As of Valley Forge, you can delete a pointer to const. */
519 /* You can't delete functions. */
520 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
522 if (complain & tf_error)
523 error_at (exp_loc,
524 "cannot delete a function. Only pointer-to-objects are "
525 "valid arguments to %<delete%>");
526 return error_mark_node;
529 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
530 if (VOID_TYPE_P (TREE_TYPE (type)))
532 if (complain & tf_warning)
533 warning_at (exp_loc, OPT_Wdelete_incomplete,
534 "deleting %qT is undefined", type);
535 doing_vec = 0;
538 /* Deleting a pointer with the value zero is valid and has no effect. */
539 if (integer_zerop (t))
540 return build1_loc (loc, NOP_EXPR, void_type_node, t);
542 if (doing_vec)
543 return build_vec_delete (loc, t, /*maxindex=*/NULL_TREE,
544 sfk_deleting_destructor,
545 use_global_delete, complain);
546 else
547 return build_delete (loc, type, t, sfk_deleting_destructor,
548 LOOKUP_NORMAL, use_global_delete,
549 complain);
552 /* Report an error if the indicated template declaration is not the
553 sort of thing that should be a member template. */
555 void
556 check_member_template (tree tmpl)
558 tree decl;
560 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
561 decl = DECL_TEMPLATE_RESULT (tmpl);
563 if (TREE_CODE (decl) == FUNCTION_DECL
564 || DECL_ALIAS_TEMPLATE_P (tmpl)
565 || (TREE_CODE (decl) == TYPE_DECL
566 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
568 /* The parser rejects template declarations in local classes
569 (with the exception of generic lambdas). */
570 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
571 /* The parser rejects any use of virtual in a function template. */
572 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
573 && DECL_VIRTUAL_P (decl)));
575 /* The debug-information generating code doesn't know what to do
576 with member templates. */
577 DECL_IGNORED_P (tmpl) = 1;
579 else if (variable_template_p (tmpl))
580 /* OK */;
581 else
582 error ("template declaration of %q#D", decl);
585 /* Sanity check: report error if this function FUNCTION is not
586 really a member of the class (CTYPE) it is supposed to belong to.
587 TEMPLATE_PARMS is used to specify the template parameters of a member
588 template passed as FUNCTION_DECL. If the member template is passed as a
589 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
590 from the declaration. If the function is not a function template, it
591 must be NULL.
592 It returns the original declaration for the function, NULL_TREE if
593 no declaration was found, error_mark_node if an error was emitted. */
595 tree
596 check_classfn (tree ctype, tree function, tree template_parms)
598 if (DECL_USE_TEMPLATE (function)
599 && !(TREE_CODE (function) == TEMPLATE_DECL
600 && DECL_TEMPLATE_SPECIALIZATION (function))
601 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
602 /* Since this is a specialization of a member template,
603 we're not going to find the declaration in the class.
604 For example, in:
606 struct S { template <typename T> void f(T); };
607 template <> void S::f(int);
609 we're not going to find `S::f(int)', but there's no
610 reason we should, either. We let our callers know we didn't
611 find the method, but we don't complain. */
612 return NULL_TREE;
614 /* Basic sanity check: for a template function, the template parameters
615 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
616 if (TREE_CODE (function) == TEMPLATE_DECL)
618 if (template_parms
619 && !comp_template_parms (template_parms,
620 DECL_TEMPLATE_PARMS (function)))
622 error ("template parameter lists provided don%'t match the "
623 "template parameters of %qD", function);
624 return error_mark_node;
626 template_parms = DECL_TEMPLATE_PARMS (function);
629 /* OK, is this a definition of a member template? */
630 bool is_template = (template_parms != NULL_TREE);
632 /* [temp.mem]
634 A destructor shall not be a member template. */
635 if (DECL_DESTRUCTOR_P (function) && is_template)
637 error ("destructor %qD declared as member template", function);
638 return error_mark_node;
641 /* We must enter the scope here, because conversion operators are
642 named by target type, and type equivalence relies on typenames
643 resolving within the scope of CTYPE. */
644 tree pushed_scope = push_scope (ctype);
645 tree matched = NULL_TREE;
646 tree fns = get_class_binding (ctype, DECL_NAME (function));
648 for (ovl_iterator iter (fns); !matched && iter; ++iter)
650 tree fndecl = *iter;
652 /* A member template definition only matches a member template
653 declaration. */
654 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
655 continue;
657 if (!DECL_DECLARES_FUNCTION_P (fndecl))
658 continue;
660 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
661 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
663 /* We cannot simply call decls_match because this doesn't work
664 for static member functions that are pretending to be
665 methods, and because the name may have been changed by
666 asm("new_name"). */
668 /* Get rid of the this parameter on functions that become
669 static. */
670 if (DECL_STATIC_FUNCTION_P (fndecl)
671 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
672 p1 = TREE_CHAIN (p1);
674 /* ref-qualifier or absence of same must match. */
675 if (type_memfn_rqual (TREE_TYPE (function))
676 != type_memfn_rqual (TREE_TYPE (fndecl)))
677 continue;
679 // Include constraints in the match.
680 tree c1 = get_constraints (function);
681 tree c2 = get_constraints (fndecl);
683 /* While finding a match, same types and params are not enough
684 if the function is versioned. Also check version ("target")
685 attributes. */
686 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
687 TREE_TYPE (TREE_TYPE (fndecl)))
688 && compparms (p1, p2)
689 && !targetm.target_option.function_versions (function, fndecl)
690 && (!is_template
691 || comp_template_parms (template_parms,
692 DECL_TEMPLATE_PARMS (fndecl)))
693 && equivalent_constraints (c1, c2)
694 && (DECL_TEMPLATE_SPECIALIZATION (function)
695 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
696 && (!DECL_TEMPLATE_SPECIALIZATION (function)
697 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
698 matched = fndecl;
701 if (!matched)
703 if (!COMPLETE_TYPE_P (ctype))
704 cxx_incomplete_type_error (DECL_SOURCE_LOCATION (function),
705 function, ctype);
706 else
708 if (DECL_CONV_FN_P (function))
709 fns = get_class_binding (ctype, conv_op_identifier);
711 error_at (DECL_SOURCE_LOCATION (function),
712 "no declaration matches %q#D", function);
713 if (fns)
714 print_candidates (fns);
715 else if (DECL_CONV_FN_P (function))
716 inform (DECL_SOURCE_LOCATION (function),
717 "no conversion operators declared");
718 else
719 inform (DECL_SOURCE_LOCATION (function),
720 "no functions named %qD", function);
721 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
722 "%#qT defined here", ctype);
724 matched = error_mark_node;
727 if (pushed_scope)
728 pop_scope (pushed_scope);
730 return matched;
733 /* DECL is a function with vague linkage. Remember it so that at the
734 end of the translation unit we can decide whether or not to emit
735 it. */
737 void
738 note_vague_linkage_fn (tree decl)
740 if (processing_template_decl)
741 return;
743 DECL_DEFER_OUTPUT (decl) = 1;
744 vec_safe_push (deferred_fns, decl);
747 /* As above, but for variable template instantiations. */
749 void
750 note_variable_template_instantiation (tree decl)
752 vec_safe_push (pending_statics, decl);
755 /* We have just processed the DECL, which is a static data member.
756 The other parameters are as for cp_finish_decl. */
758 void
759 finish_static_data_member_decl (tree decl,
760 tree init, bool init_const_expr_p,
761 tree asmspec_tree,
762 int flags)
764 if (DECL_TEMPLATE_INSTANTIATED (decl))
765 /* We already needed to instantiate this, so the processing in this
766 function is unnecessary/wrong. */
767 return;
769 DECL_CONTEXT (decl) = current_class_type;
771 /* We cannot call pushdecl here, because that would fill in the
772 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
773 the right thing, namely, to put this decl out straight away. */
775 if (! processing_template_decl)
776 vec_safe_push (pending_statics, decl);
778 if (LOCAL_CLASS_P (current_class_type)
779 /* We already complained about the template definition. */
780 && !DECL_TEMPLATE_INSTANTIATION (decl))
781 permerror (DECL_SOURCE_LOCATION (decl),
782 "local class %q#T shall not have static data member %q#D",
783 current_class_type, decl);
784 else
785 for (tree t = current_class_type; TYPE_P (t);
786 t = CP_TYPE_CONTEXT (t))
787 if (TYPE_UNNAMED_P (t))
789 auto_diagnostic_group d;
790 if (permerror (DECL_SOURCE_LOCATION (decl),
791 "static data member %qD in unnamed class", decl))
792 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
793 "unnamed class defined here");
794 break;
797 if (DECL_INLINE_VAR_P (decl) && !DECL_TEMPLATE_INSTANTIATION (decl))
798 /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P.
799 Except that if decl is a template instantiation, it isn't defined until
800 instantiate_decl. */;
801 else
802 DECL_IN_AGGR_P (decl) = 1;
804 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
805 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
806 SET_VAR_HAD_UNKNOWN_BOUND (decl);
808 if (init)
810 /* Similarly to start_decl_1, we want to complete the type in order
811 to do the right thing in cp_apply_type_quals_to_decl, possibly
812 clear TYPE_QUAL_CONST (c++/65579). */
813 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
814 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
817 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
820 /* DECLARATOR and DECLSPECS correspond to a class member. The other
821 parameters are as for cp_finish_decl. Return the DECL for the
822 class member declared. */
824 tree
825 grokfield (const cp_declarator *declarator,
826 cp_decl_specifier_seq *declspecs,
827 tree init, bool init_const_expr_p,
828 tree asmspec_tree,
829 tree attrlist)
831 tree value;
832 const char *asmspec = 0;
833 int flags;
835 if (init
836 && TREE_CODE (init) == TREE_LIST
837 && TREE_VALUE (init) == error_mark_node
838 && TREE_CHAIN (init) == NULL_TREE)
839 init = NULL_TREE;
841 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
842 if (! value || value == error_mark_node)
843 /* friend or constructor went bad. */
844 return error_mark_node;
845 if (TREE_TYPE (value) == error_mark_node)
846 return value;
848 if (TREE_CODE (value) == TYPE_DECL && init)
850 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)),
851 "typedef %qD is initialized (use %qs instead)",
852 value, "decltype");
853 init = NULL_TREE;
856 /* Pass friendly classes back. */
857 if (value == void_type_node)
858 return value;
860 if (DECL_NAME (value)
861 && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR)
863 error_at (declarator->id_loc,
864 "explicit template argument list not allowed");
865 return error_mark_node;
868 /* Stash away type declarations. */
869 if (TREE_CODE (value) == TYPE_DECL)
871 DECL_NONLOCAL (value) = 1;
872 DECL_CONTEXT (value) = current_class_type;
874 if (attrlist)
876 int attrflags = 0;
878 /* If this is a typedef that names the class for linkage purposes
879 (7.1.3p8), apply any attributes directly to the type. */
880 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
881 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
882 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
884 cplus_decl_attributes (&value, attrlist, attrflags);
887 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
888 && TREE_TYPE (value) != error_mark_node
889 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
890 set_underlying_type (value);
892 /* It's important that push_template_decl below follows
893 set_underlying_type above so that the created template
894 carries the properly set type of VALUE. */
895 if (processing_template_decl)
896 value = push_template_decl (value);
898 record_locally_defined_typedef (value);
899 return value;
902 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
904 if (!friendp && DECL_IN_AGGR_P (value))
906 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
907 return void_type_node;
910 if (asmspec_tree && asmspec_tree != error_mark_node)
911 asmspec = TREE_STRING_POINTER (asmspec_tree);
913 if (init)
915 if (TREE_CODE (value) == FUNCTION_DECL)
917 if (init == ridpointers[(int)RID_DELETE])
919 if (friendp && decl_defined_p (value))
921 error ("redefinition of %q#D", value);
922 inform (DECL_SOURCE_LOCATION (value),
923 "%q#D previously defined here", value);
925 else
927 DECL_DELETED_FN (value) = 1;
928 DECL_DECLARED_INLINE_P (value) = 1;
929 DECL_INITIAL (value) = error_mark_node;
932 else if (init == ridpointers[(int)RID_DEFAULT])
934 if (defaultable_fn_check (value))
936 DECL_DEFAULTED_FN (value) = 1;
937 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
938 DECL_DECLARED_INLINE_P (value) = 1;
941 else if (TREE_CODE (init) == DEFERRED_PARSE)
942 error ("invalid initializer for member function %qD", value);
943 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
945 if (integer_zerop (init))
946 DECL_PURE_VIRTUAL_P (value) = 1;
947 else if (error_operand_p (init))
948 ; /* An error has already been reported. */
949 else
950 error ("invalid initializer for member function %qD",
951 value);
953 else
955 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
956 location_t iloc
957 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value));
958 if (friendp)
959 error_at (iloc, "initializer specified for friend "
960 "function %qD", value);
961 else
962 error_at (iloc, "initializer specified for static "
963 "member function %qD", value);
966 else if (TREE_CODE (value) == FIELD_DECL)
967 /* C++11 NSDMI, keep going. */;
968 else if (!VAR_P (value))
969 gcc_unreachable ();
972 /* Pass friend decls back. */
973 if ((TREE_CODE (value) == FUNCTION_DECL
974 || TREE_CODE (value) == TEMPLATE_DECL)
975 && DECL_CONTEXT (value) != current_class_type)
976 return value;
978 /* Need to set this before push_template_decl. */
979 if (VAR_P (value))
980 DECL_CONTEXT (value) = current_class_type;
982 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
984 value = push_template_decl (value);
985 if (error_operand_p (value))
986 return error_mark_node;
989 if (attrlist)
990 cplus_decl_attributes (&value, attrlist, 0);
992 if (init && DIRECT_LIST_INIT_P (init))
993 flags = LOOKUP_NORMAL;
994 else
995 flags = LOOKUP_IMPLICIT;
997 if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
998 flags |= LOOKUP_CONSTINIT;
1000 switch (TREE_CODE (value))
1002 case VAR_DECL:
1003 finish_static_data_member_decl (value, init, init_const_expr_p,
1004 asmspec_tree, flags);
1005 return value;
1007 case FIELD_DECL:
1008 if (asmspec)
1009 error ("%<asm%> specifiers are not permitted on non-static data members");
1010 if (DECL_INITIAL (value) == error_mark_node)
1011 init = error_mark_node;
1012 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1013 NULL_TREE, flags);
1014 DECL_IN_AGGR_P (value) = 1;
1015 return value;
1017 case FUNCTION_DECL:
1018 if (asmspec)
1019 set_user_assembler_name (value, asmspec);
1021 cp_finish_decl (value,
1022 /*init=*/NULL_TREE,
1023 /*init_const_expr_p=*/false,
1024 asmspec_tree, flags);
1026 /* Pass friends back this way. */
1027 if (DECL_FRIEND_P (value))
1028 return void_type_node;
1030 DECL_IN_AGGR_P (value) = 1;
1031 return value;
1033 default:
1034 gcc_unreachable ();
1036 return NULL_TREE;
1039 /* Like `grokfield', but for bitfields.
1040 WIDTH is the width of the bitfield, a constant expression.
1041 The other parameters are as for grokfield. */
1043 tree
1044 grokbitfield (const cp_declarator *declarator,
1045 cp_decl_specifier_seq *declspecs, tree width, tree init,
1046 tree attrlist)
1048 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1049 init != NULL_TREE, &attrlist);
1051 if (value == error_mark_node)
1052 return NULL_TREE; /* friends went bad. */
1054 tree type = TREE_TYPE (value);
1055 if (type == error_mark_node)
1056 return value;
1058 /* Pass friendly classes back. */
1059 if (VOID_TYPE_P (value))
1060 return void_type_node;
1062 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)
1063 && (INDIRECT_TYPE_P (type) || !dependent_type_p (type)))
1065 error_at (DECL_SOURCE_LOCATION (value),
1066 "bit-field %qD with non-integral type %qT",
1067 value, type);
1068 return error_mark_node;
1071 if (TREE_CODE (value) == TYPE_DECL)
1073 error_at (DECL_SOURCE_LOCATION (value),
1074 "cannot declare %qD to be a bit-field type", value);
1075 return NULL_TREE;
1078 /* Usually, finish_struct_1 catches bitfields with invalid types.
1079 But, in the case of bitfields with function type, we confuse
1080 ourselves into thinking they are member functions, so we must
1081 check here. */
1082 if (TREE_CODE (value) == FUNCTION_DECL)
1084 error_at (DECL_SOURCE_LOCATION (value),
1085 "cannot declare bit-field %qD with function type", value);
1086 return NULL_TREE;
1089 if (TYPE_WARN_IF_NOT_ALIGN (type))
1091 error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field "
1092 "%qD with %<warn_if_not_aligned%> type", value);
1093 return NULL_TREE;
1096 if (DECL_IN_AGGR_P (value))
1098 error ("%qD is already defined in the class %qT", value,
1099 DECL_CONTEXT (value));
1100 return void_type_node;
1103 if (TREE_STATIC (value))
1105 error_at (DECL_SOURCE_LOCATION (value),
1106 "static member %qD cannot be a bit-field", value);
1107 return NULL_TREE;
1110 int flags = LOOKUP_IMPLICIT;
1111 if (init && DIRECT_LIST_INIT_P (init))
1112 flags = LOOKUP_NORMAL;
1113 cp_finish_decl (value, init, false, NULL_TREE, flags);
1115 if (width != error_mark_node)
1117 /* The width must be an integer type. */
1118 if (!type_dependent_expression_p (width)
1119 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1120 error ("width of bit-field %qD has non-integral type %qT", value,
1121 TREE_TYPE (width));
1122 else
1124 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1125 check_bitfield_decl picks it from there later and sets DECL_SIZE
1126 accordingly. */
1127 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1128 SET_DECL_C_BIT_FIELD (value);
1132 DECL_IN_AGGR_P (value) = 1;
1134 if (attrlist)
1135 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1137 return value;
1141 /* Returns true iff ATTR is an attribute which needs to be applied at
1142 instantiation time rather than template definition time. */
1144 static bool
1145 is_late_template_attribute (tree attr, tree decl)
1147 tree name = get_attribute_name (attr);
1148 tree args = TREE_VALUE (attr);
1149 const struct attribute_spec *spec = lookup_attribute_spec (name);
1150 tree arg;
1152 if (!spec)
1153 /* Unknown attribute. */
1154 return false;
1156 /* Attribute weak handling wants to write out assembly right away. */
1157 if (is_attribute_p ("weak", name))
1158 return true;
1160 /* Attributes used and unused are applied directly to typedefs for the
1161 benefit of maybe_warn_unused_local_typedefs. */
1162 if (TREE_CODE (decl) == TYPE_DECL
1163 && (is_attribute_p ("unused", name)
1164 || is_attribute_p ("used", name)))
1165 return false;
1167 /* Attribute tls_model wants to modify the symtab. */
1168 if (is_attribute_p ("tls_model", name))
1169 return true;
1171 /* #pragma omp declare simd attribute needs to be always deferred. */
1172 if (flag_openmp
1173 && is_attribute_p ("omp declare simd", name))
1174 return true;
1176 /* An attribute pack is clearly dependent. */
1177 if (args && PACK_EXPANSION_P (args))
1178 return true;
1180 /* If any of the arguments are dependent expressions, we can't evaluate
1181 the attribute until instantiation time. */
1182 for (arg = args; arg; arg = TREE_CHAIN (arg))
1184 tree t = TREE_VALUE (arg);
1186 /* If the first attribute argument is an identifier, only consider
1187 second and following arguments. Attributes like mode, format,
1188 cleanup and several target specific attributes aren't late
1189 just because they have an IDENTIFIER_NODE as first argument. */
1190 if (arg == args && attribute_takes_identifier_p (name)
1191 && identifier_p (t))
1192 continue;
1194 if (value_dependent_expression_p (t)
1195 || type_dependent_expression_p (t))
1196 return true;
1199 if (TREE_CODE (decl) == TYPE_DECL
1200 || TYPE_P (decl)
1201 || spec->type_required)
1203 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1205 /* We can't apply any attributes to a completely unknown type until
1206 instantiation time. */
1207 enum tree_code code = TREE_CODE (type);
1208 if (code == TEMPLATE_TYPE_PARM
1209 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1210 || code == TYPENAME_TYPE)
1211 return true;
1212 /* Also defer most attributes on dependent types. This is not
1213 necessary in all cases, but is the better default. */
1214 else if (dependent_type_p (type)
1215 /* But some attributes specifically apply to templates. */
1216 && !is_attribute_p ("abi_tag", name)
1217 && !is_attribute_p ("deprecated", name)
1218 && !is_attribute_p ("visibility", name))
1219 return true;
1220 else
1221 return false;
1223 else
1224 return false;
1227 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1228 applied at instantiation time and return them. If IS_DEPENDENT is true,
1229 the declaration itself is dependent, so all attributes should be applied
1230 at instantiation time. */
1232 static tree
1233 splice_template_attributes (tree *attr_p, tree decl)
1235 tree *p = attr_p;
1236 tree late_attrs = NULL_TREE;
1237 tree *q = &late_attrs;
1239 if (!p)
1240 return NULL_TREE;
1242 for (; *p; )
1244 if (is_late_template_attribute (*p, decl))
1246 ATTR_IS_DEPENDENT (*p) = 1;
1247 *q = *p;
1248 *p = TREE_CHAIN (*p);
1249 q = &TREE_CHAIN (*q);
1250 *q = NULL_TREE;
1252 else
1253 p = &TREE_CHAIN (*p);
1256 return late_attrs;
1259 /* Remove any late attributes from the list in ATTR_P and attach them to
1260 DECL_P. */
1262 static void
1263 save_template_attributes (tree *attr_p, tree *decl_p, int flags)
1265 tree *q;
1267 if (attr_p && *attr_p == error_mark_node)
1268 return;
1270 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1271 if (!late_attrs)
1272 return;
1274 if (DECL_P (*decl_p))
1275 q = &DECL_ATTRIBUTES (*decl_p);
1276 else
1277 q = &TYPE_ATTRIBUTES (*decl_p);
1279 tree old_attrs = *q;
1281 /* Merge the late attributes at the beginning with the attribute
1282 list. */
1283 late_attrs = merge_attributes (late_attrs, *q);
1284 if (*q != late_attrs
1285 && !DECL_P (*decl_p)
1286 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1288 if (!dependent_type_p (*decl_p))
1289 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1290 else
1292 *decl_p = build_variant_type_copy (*decl_p);
1293 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1296 else
1297 *q = late_attrs;
1299 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1301 /* We've added new attributes directly to the main variant, so
1302 now we need to update all of the other variants to include
1303 these new attributes. */
1304 tree variant;
1305 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1306 variant = TYPE_NEXT_VARIANT (variant))
1308 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1309 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1314 /* True if ATTRS contains any dependent attributes that affect type
1315 identity. */
1317 bool
1318 any_dependent_type_attributes_p (tree attrs)
1320 for (tree a = attrs; a; a = TREE_CHAIN (a))
1321 if (ATTR_IS_DEPENDENT (a))
1323 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1324 if (as && as->affects_type_identity)
1325 return true;
1327 return false;
1330 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1331 to a typedef which gives a previously unnamed class or enum a name for
1332 linkage purposes. */
1334 bool
1335 attributes_naming_typedef_ok (tree attrs)
1337 for (; attrs; attrs = TREE_CHAIN (attrs))
1339 tree name = get_attribute_name (attrs);
1340 if (is_attribute_p ("vector_size", name))
1341 return false;
1343 return true;
1346 /* Like reconstruct_complex_type, but handle also template trees. */
1348 tree
1349 cp_reconstruct_complex_type (tree type, tree bottom)
1351 tree inner, outer;
1353 if (TYPE_PTR_P (type))
1355 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1356 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1357 TYPE_REF_CAN_ALIAS_ALL (type));
1359 else if (TYPE_REF_P (type))
1361 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1362 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1363 TYPE_REF_CAN_ALIAS_ALL (type));
1365 else if (TREE_CODE (type) == ARRAY_TYPE)
1367 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1368 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1369 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1370 element type qualification will be handled by the recursive
1371 cp_reconstruct_complex_type call and cp_build_qualified_type
1372 for ARRAY_TYPEs changes the element type. */
1373 return outer;
1375 else if (TREE_CODE (type) == FUNCTION_TYPE)
1377 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1378 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1379 outer = apply_memfn_quals (outer, type_memfn_quals (type));
1381 else if (TREE_CODE (type) == METHOD_TYPE)
1383 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1384 /* The build_method_type_directly() routine prepends 'this' to argument list,
1385 so we must compensate by getting rid of it. */
1386 outer
1387 = build_method_type_directly
1388 (class_of_this_parm (type), inner,
1389 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1391 else if (TREE_CODE (type) == OFFSET_TYPE)
1393 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1394 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1396 else
1397 return bottom;
1399 if (TYPE_ATTRIBUTES (type))
1400 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1401 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1402 outer = cxx_copy_lang_qualifiers (outer, type);
1404 return outer;
1407 /* Replaces any constexpr expression that may be into the attributes
1408 arguments with their reduced value. */
1410 void
1411 cp_check_const_attributes (tree attributes)
1413 if (attributes == error_mark_node)
1414 return;
1416 tree attr;
1417 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1419 tree arg;
1420 for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1421 arg = TREE_CHAIN (arg))
1423 tree expr = TREE_VALUE (arg);
1424 if (EXPR_P (expr))
1425 TREE_VALUE (arg) = fold_non_dependent_expr (expr);
1430 /* Return true if TYPE is an OpenMP mappable type.
1431 If NOTES is non-zero, emit a note message for each problem. */
1432 static bool
1433 cp_omp_mappable_type_1 (tree type, bool notes)
1435 bool result = true;
1437 /* Mappable type has to be complete. */
1438 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1440 if (notes && type != error_mark_node)
1442 tree decl = TYPE_MAIN_DECL (type);
1443 inform ((decl ? DECL_SOURCE_LOCATION (decl) : input_location),
1444 "incomplete type %qT is not mappable", type);
1446 result = false;
1448 /* Arrays have mappable type if the elements have mappable type. */
1449 while (TREE_CODE (type) == ARRAY_TYPE)
1450 type = TREE_TYPE (type);
1451 /* A mappable type cannot contain virtual members. */
1452 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1454 if (notes)
1455 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
1456 "type %qT with virtual members is not mappable", type);
1457 result = false;
1459 /* All data members must be non-static. */
1460 if (CLASS_TYPE_P (type))
1462 tree field;
1463 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1464 if (VAR_P (field))
1466 if (notes)
1467 inform (DECL_SOURCE_LOCATION (field),
1468 "static field %qD is not mappable", field);
1469 result = false;
1471 /* All fields must have mappable types. */
1472 else if (TREE_CODE (field) == FIELD_DECL
1473 && !cp_omp_mappable_type_1 (TREE_TYPE (field), notes))
1474 result = false;
1476 return result;
1479 /* Return true if TYPE is an OpenMP mappable type. */
1480 bool
1481 cp_omp_mappable_type (tree type)
1483 return cp_omp_mappable_type_1 (type, false);
1486 /* Return true if TYPE is an OpenMP mappable type.
1487 Emit an error messages if not. */
1488 bool
1489 cp_omp_emit_unmappable_type_notes (tree type)
1491 return cp_omp_mappable_type_1 (type, true);
1494 /* Return the last pushed declaration for the symbol DECL or NULL
1495 when no such declaration exists. */
1497 static tree
1498 find_last_decl (tree decl)
1500 tree last_decl = NULL_TREE;
1502 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1504 /* Look up the declaration in its scope. */
1505 tree pushed_scope = NULL_TREE;
1506 if (tree ctype = DECL_CONTEXT (decl))
1507 pushed_scope = push_scope (ctype);
1509 last_decl = lookup_name (name);
1511 if (pushed_scope)
1512 pop_scope (pushed_scope);
1514 /* The declaration may be a member conversion operator
1515 or a bunch of overfloads (handle the latter below). */
1516 if (last_decl && BASELINK_P (last_decl))
1517 last_decl = BASELINK_FUNCTIONS (last_decl);
1520 if (!last_decl)
1521 return NULL_TREE;
1523 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1525 /* A set of overloads of the same function. */
1526 for (lkp_iterator iter (last_decl); iter; ++iter)
1528 if (TREE_CODE (*iter) == OVERLOAD)
1529 continue;
1531 if (decls_match (decl, *iter, /*record_decls=*/false))
1532 return *iter;
1534 return NULL_TREE;
1537 return NULL_TREE;
1540 /* Like decl_attributes, but handle C++ complexity. */
1542 void
1543 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1545 if (*decl == NULL_TREE || *decl == void_type_node
1546 || *decl == error_mark_node)
1547 return;
1549 /* Add implicit "omp declare target" attribute if requested. */
1550 if (scope_chain->omp_declare_target_attribute
1551 && ((VAR_P (*decl)
1552 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1553 || TREE_CODE (*decl) == FUNCTION_DECL))
1555 if (VAR_P (*decl)
1556 && DECL_CLASS_SCOPE_P (*decl))
1557 error ("%q+D static data member inside of declare target directive",
1558 *decl);
1559 else if (VAR_P (*decl)
1560 && (processing_template_decl
1561 || !cp_omp_mappable_type (TREE_TYPE (*decl))))
1562 attributes = tree_cons (get_identifier ("omp declare target implicit"),
1563 NULL_TREE, attributes);
1564 else
1566 attributes = tree_cons (get_identifier ("omp declare target"),
1567 NULL_TREE, attributes);
1568 attributes = tree_cons (get_identifier ("omp declare target block"),
1569 NULL_TREE, attributes);
1573 if (processing_template_decl)
1575 if (check_for_bare_parameter_packs (attributes))
1576 return;
1578 save_template_attributes (&attributes, decl, flags);
1581 cp_check_const_attributes (attributes);
1583 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1584 decl = &DECL_TEMPLATE_RESULT (*decl);
1586 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1588 attributes
1589 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1590 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1591 attributes, flags);
1593 else
1595 tree last_decl = find_last_decl (*decl);
1596 decl_attributes (decl, attributes, flags, last_decl);
1599 if (TREE_CODE (*decl) == TYPE_DECL)
1600 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1602 /* Propagate deprecation out to the template. */
1603 if (TREE_DEPRECATED (*decl))
1604 if (tree ti = get_template_info (*decl))
1606 tree tmpl = TI_TEMPLATE (ti);
1607 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1608 : DECL_TEMPLATE_RESULT (tmpl));
1609 if (*decl == pattern)
1610 TREE_DEPRECATED (tmpl) = true;
1614 /* Walks through the namespace- or function-scope anonymous union
1615 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1616 Returns one of the fields for use in the mangled name. */
1618 static tree
1619 build_anon_union_vars (tree type, tree object)
1621 tree main_decl = NULL_TREE;
1622 tree field;
1624 /* Rather than write the code to handle the non-union case,
1625 just give an error. */
1626 if (TREE_CODE (type) != UNION_TYPE)
1628 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
1629 "anonymous struct not inside named type");
1630 return error_mark_node;
1633 for (field = TYPE_FIELDS (type);
1634 field != NULL_TREE;
1635 field = DECL_CHAIN (field))
1637 tree decl;
1638 tree ref;
1640 if (DECL_ARTIFICIAL (field))
1641 continue;
1642 if (TREE_CODE (field) != FIELD_DECL)
1644 permerror (DECL_SOURCE_LOCATION (field),
1645 "%q#D invalid; an anonymous union can only "
1646 "have non-static data members", field);
1647 continue;
1650 if (TREE_PRIVATE (field))
1651 permerror (DECL_SOURCE_LOCATION (field),
1652 "private member %q#D in anonymous union", field);
1653 else if (TREE_PROTECTED (field))
1654 permerror (DECL_SOURCE_LOCATION (field),
1655 "protected member %q#D in anonymous union", field);
1657 if (processing_template_decl)
1658 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1659 DECL_NAME (field), NULL_TREE);
1660 else
1661 ref = build_class_member_access_expr (object, field, NULL_TREE,
1662 false, tf_warning_or_error);
1664 if (DECL_NAME (field))
1666 tree base;
1668 decl = build_decl (input_location,
1669 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1670 DECL_ANON_UNION_VAR_P (decl) = 1;
1671 DECL_ARTIFICIAL (decl) = 1;
1673 base = get_base_address (object);
1674 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1675 TREE_STATIC (decl) = TREE_STATIC (base);
1676 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1678 SET_DECL_VALUE_EXPR (decl, ref);
1679 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1681 decl = pushdecl (decl);
1683 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1684 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1685 else
1686 decl = 0;
1688 if (main_decl == NULL_TREE)
1689 main_decl = decl;
1692 return main_decl;
1695 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1696 anonymous union, then all members must be laid out together. PUBLIC_P
1697 is nonzero if this union is not declared static. */
1699 void
1700 finish_anon_union (tree anon_union_decl)
1702 tree type;
1703 tree main_decl;
1704 bool public_p;
1706 if (anon_union_decl == error_mark_node)
1707 return;
1709 type = TREE_TYPE (anon_union_decl);
1710 public_p = TREE_PUBLIC (anon_union_decl);
1712 /* The VAR_DECL's context is the same as the TYPE's context. */
1713 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1715 if (TYPE_FIELDS (type) == NULL_TREE)
1716 return;
1718 if (public_p)
1720 error ("namespace-scope anonymous aggregates must be static");
1721 return;
1724 main_decl = build_anon_union_vars (type, anon_union_decl);
1725 if (main_decl == error_mark_node)
1726 return;
1727 if (main_decl == NULL_TREE)
1729 pedwarn (input_location, 0, "anonymous union with no members");
1730 return;
1733 if (!processing_template_decl)
1735 /* Use main_decl to set the mangled name. */
1736 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1737 maybe_commonize_var (anon_union_decl);
1738 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1740 if (DECL_DISCRIMINATOR_P (anon_union_decl))
1741 determine_local_discriminator (anon_union_decl);
1742 mangle_decl (anon_union_decl);
1744 DECL_NAME (anon_union_decl) = NULL_TREE;
1747 pushdecl (anon_union_decl);
1748 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1751 /* Auxiliary functions to make type signatures for
1752 `operator new' and `operator delete' correspond to
1753 what compiler will be expecting. */
1755 tree
1756 coerce_new_type (tree type, location_t loc)
1758 int e = 0;
1759 tree args = TYPE_ARG_TYPES (type);
1761 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1763 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1765 e = 1;
1766 error_at (loc, "%<operator new%> must return type %qT",
1767 ptr_type_node);
1770 if (args && args != void_list_node)
1772 if (TREE_PURPOSE (args))
1774 /* [basic.stc.dynamic.allocation]
1776 The first parameter shall not have an associated default
1777 argument. */
1778 error_at (loc, "the first parameter of %<operator new%> cannot "
1779 "have a default argument");
1780 /* Throw away the default argument. */
1781 TREE_PURPOSE (args) = NULL_TREE;
1784 if (!same_type_p (TREE_VALUE (args), size_type_node))
1786 e = 2;
1787 args = TREE_CHAIN (args);
1790 else
1791 e = 2;
1793 if (e == 2)
1794 permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
1795 "as first parameter", size_type_node);
1797 switch (e)
1799 case 2:
1800 args = tree_cons (NULL_TREE, size_type_node, args);
1801 /* Fall through. */
1802 case 1:
1803 type = (cxx_copy_lang_qualifiers
1804 (build_function_type (ptr_type_node, args),
1805 type));
1806 /* Fall through. */
1807 default:;
1809 return type;
1812 void
1813 coerce_delete_type (tree decl, location_t loc)
1815 int e = 0;
1816 tree type = TREE_TYPE (decl);
1817 tree args = TYPE_ARG_TYPES (type);
1819 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1821 if (!same_type_p (TREE_TYPE (type), void_type_node))
1823 e = 1;
1824 error_at (loc, "%<operator delete%> must return type %qT",
1825 void_type_node);
1828 tree ptrtype = ptr_type_node;
1829 if (destroying_delete_p (decl))
1831 if (DECL_CLASS_SCOPE_P (decl))
1832 /* If the function is a destroying operator delete declared in class
1833 type C, the type of its first parameter shall be C*. */
1834 ptrtype = build_pointer_type (DECL_CONTEXT (decl));
1835 else
1836 /* A destroying operator delete shall be a class member function named
1837 operator delete. */
1838 error_at (loc,
1839 "destroying %<operator delete%> must be a member function");
1840 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl));
1841 if (op->flags & OVL_OP_FLAG_VEC)
1842 error_at (loc, "%<operator delete[]%> cannot be a destroying delete");
1843 if (!usual_deallocation_fn_p (decl))
1844 error_at (loc, "destroying %<operator delete%> must be a usual "
1845 "deallocation function");
1848 if (!args || args == void_list_node
1849 || !same_type_p (TREE_VALUE (args), ptrtype))
1851 e = 2;
1852 if (args && args != void_list_node)
1853 args = TREE_CHAIN (args);
1854 error_at (loc, "%<operator delete%> takes type %qT as first parameter",
1855 ptrtype);
1857 switch (e)
1859 case 2:
1860 args = tree_cons (NULL_TREE, ptrtype, args);
1861 /* Fall through. */
1862 case 1:
1863 type = (cxx_copy_lang_qualifiers
1864 (build_function_type (void_type_node, args),
1865 type));
1866 /* Fall through. */
1867 default:;
1870 TREE_TYPE (decl) = type;
1873 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1874 and mark them as needed. */
1876 static void
1877 mark_vtable_entries (tree decl)
1879 tree fnaddr;
1880 unsigned HOST_WIDE_INT idx;
1882 /* It's OK for the vtable to refer to deprecated virtual functions. */
1883 warning_sentinel w(warn_deprecated_decl);
1885 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1886 idx, fnaddr)
1888 tree fn;
1890 STRIP_NOPS (fnaddr);
1892 if (TREE_CODE (fnaddr) != ADDR_EXPR
1893 && TREE_CODE (fnaddr) != FDESC_EXPR)
1894 /* This entry is an offset: a virtual base class offset, a
1895 virtual call offset, an RTTI offset, etc. */
1896 continue;
1898 fn = TREE_OPERAND (fnaddr, 0);
1899 TREE_ADDRESSABLE (fn) = 1;
1900 /* When we don't have vcall offsets, we output thunks whenever
1901 we output the vtables that contain them. With vcall offsets,
1902 we know all the thunks we'll need when we emit a virtual
1903 function, so we emit the thunks there instead. */
1904 if (DECL_THUNK_P (fn))
1905 use_thunk (fn, /*emit_p=*/0);
1906 /* Set the location, as marking the function could cause
1907 instantiation. We do not need to preserve the incoming
1908 location, as we're called from c_parse_final_cleanups, which
1909 takes care of that. */
1910 input_location = DECL_SOURCE_LOCATION (fn);
1911 mark_used (fn);
1915 /* Adjust the TLS model on variable DECL if need be, typically after
1916 the linkage of DECL has been modified. */
1918 static void
1919 adjust_var_decl_tls_model (tree decl)
1921 if (CP_DECL_THREAD_LOCAL_P (decl)
1922 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
1923 set_decl_tls_model (decl, decl_default_tls_model (decl));
1926 /* Set DECL up to have the closest approximation of "initialized common"
1927 linkage available. */
1929 void
1930 comdat_linkage (tree decl)
1932 if (flag_weak)
1933 make_decl_one_only (decl, cxx_comdat_group (decl));
1934 else if (TREE_CODE (decl) == FUNCTION_DECL
1935 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1936 /* We can just emit function and compiler-generated variables
1937 statically; having multiple copies is (for the most part) only
1938 a waste of space.
1940 There are two correctness issues, however: the address of a
1941 template instantiation with external linkage should be the
1942 same, independent of what translation unit asks for the
1943 address, and this will not hold when we emit multiple copies of
1944 the function. However, there's little else we can do.
1946 Also, by default, the typeinfo implementation assumes that
1947 there will be only one copy of the string used as the name for
1948 each type. Therefore, if weak symbols are unavailable, the
1949 run-time library should perform a more conservative check; it
1950 should perform a string comparison, rather than an address
1951 comparison. */
1952 TREE_PUBLIC (decl) = 0;
1953 else
1955 /* Static data member template instantiations, however, cannot
1956 have multiple copies. */
1957 if (DECL_INITIAL (decl) == 0
1958 || DECL_INITIAL (decl) == error_mark_node)
1959 DECL_COMMON (decl) = 1;
1960 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1962 DECL_COMMON (decl) = 1;
1963 DECL_INITIAL (decl) = error_mark_node;
1965 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1967 /* We can't do anything useful; leave vars for explicit
1968 instantiation. */
1969 DECL_EXTERNAL (decl) = 1;
1970 DECL_NOT_REALLY_EXTERN (decl) = 0;
1974 if (TREE_PUBLIC (decl))
1975 DECL_COMDAT (decl) = 1;
1977 if (VAR_P (decl))
1978 adjust_var_decl_tls_model (decl);
1981 /* For win32 we also want to put explicit instantiations in
1982 linkonce sections, so that they will be merged with implicit
1983 instantiations; otherwise we get duplicate symbol errors.
1984 For Darwin we do not want explicit instantiations to be
1985 linkonce. */
1987 void
1988 maybe_make_one_only (tree decl)
1990 /* We used to say that this was not necessary on targets that support weak
1991 symbols, because the implicit instantiations will defer to the explicit
1992 one. However, that's not actually the case in SVR4; a strong definition
1993 after a weak one is an error. Also, not making explicit
1994 instantiations one_only means that we can end up with two copies of
1995 some template instantiations. */
1996 if (! flag_weak)
1997 return;
1999 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
2000 we can get away with not emitting them if they aren't used. We need
2001 to for variables so that cp_finish_decl will update their linkage,
2002 because their DECL_INITIAL may not have been set properly yet. */
2004 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
2005 || (! DECL_EXPLICIT_INSTANTIATION (decl)
2006 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
2008 make_decl_one_only (decl, cxx_comdat_group (decl));
2010 if (VAR_P (decl))
2012 varpool_node *node = varpool_node::get_create (decl);
2013 DECL_COMDAT (decl) = 1;
2014 /* Mark it needed so we don't forget to emit it. */
2015 node->forced_by_abi = true;
2016 TREE_USED (decl) = 1;
2018 adjust_var_decl_tls_model (decl);
2023 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
2024 This predicate will give the right answer during parsing of the
2025 function, which other tests may not. */
2027 bool
2028 vague_linkage_p (tree decl)
2030 if (!TREE_PUBLIC (decl))
2032 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
2033 maybe-in-charge 'tor variants; in that case we need to check one of
2034 the "clones" for the real linkage. But only in that case; before
2035 maybe_clone_body we haven't yet copied the linkage to the clones. */
2036 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
2037 && !DECL_ABSTRACT_P (decl)
2038 && DECL_CHAIN (decl)
2039 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
2040 return vague_linkage_p (DECL_CHAIN (decl));
2042 gcc_checking_assert (!DECL_COMDAT (decl));
2043 return false;
2045 /* Unfortunately, import_export_decl has not always been called
2046 before the function is processed, so we cannot simply check
2047 DECL_COMDAT. */
2048 if (DECL_COMDAT (decl)
2049 || (TREE_CODE (decl) == FUNCTION_DECL
2050 && DECL_DECLARED_INLINE_P (decl))
2051 || (DECL_LANG_SPECIFIC (decl)
2052 && DECL_TEMPLATE_INSTANTIATION (decl))
2053 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
2054 return true;
2055 else if (DECL_FUNCTION_SCOPE_P (decl))
2056 /* A local static in an inline effectively has vague linkage. */
2057 return (TREE_STATIC (decl)
2058 && vague_linkage_p (DECL_CONTEXT (decl)));
2059 else
2060 return false;
2063 /* Determine whether or not we want to specifically import or export CTYPE,
2064 using various heuristics. */
2066 static void
2067 import_export_class (tree ctype)
2069 /* -1 for imported, 1 for exported. */
2070 int import_export = 0;
2072 /* It only makes sense to call this function at EOF. The reason is
2073 that this function looks at whether or not the first non-inline
2074 non-abstract virtual member function has been defined in this
2075 translation unit. But, we can't possibly know that until we've
2076 seen the entire translation unit. */
2077 gcc_assert (at_eof);
2079 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2080 return;
2082 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2083 we will have CLASSTYPE_INTERFACE_ONLY set but not
2084 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2085 heuristic because someone will supply a #pragma implementation
2086 elsewhere, and deducing it here would produce a conflict. */
2087 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2088 return;
2090 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2091 import_export = -1;
2092 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2093 import_export = 1;
2094 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2095 && !flag_implicit_templates)
2096 /* For a template class, without -fimplicit-templates, check the
2097 repository. If the virtual table is assigned to this
2098 translation unit, then export the class; otherwise, import
2099 it. */
2100 import_export = -1;
2101 else if (TYPE_POLYMORPHIC_P (ctype))
2103 /* The ABI specifies that the virtual table and associated
2104 information are emitted with the key method, if any. */
2105 tree method = CLASSTYPE_KEY_METHOD (ctype);
2106 /* If weak symbol support is not available, then we must be
2107 careful not to emit the vtable when the key function is
2108 inline. An inline function can be defined in multiple
2109 translation units. If we were to emit the vtable in each
2110 translation unit containing a definition, we would get
2111 multiple definition errors at link-time. */
2112 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2113 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2116 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2117 a definition anywhere else. */
2118 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2119 import_export = 0;
2121 /* Allow back ends the chance to overrule the decision. */
2122 if (targetm.cxx.import_export_class)
2123 import_export = targetm.cxx.import_export_class (ctype, import_export);
2125 if (import_export)
2127 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2128 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2132 /* Return true if VAR has already been provided to the back end; in that
2133 case VAR should not be modified further by the front end. */
2134 static bool
2135 var_finalized_p (tree var)
2137 return varpool_node::get_create (var)->definition;
2140 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2141 must be emitted in this translation unit. Mark it as such. */
2143 void
2144 mark_needed (tree decl)
2146 TREE_USED (decl) = 1;
2147 if (TREE_CODE (decl) == FUNCTION_DECL)
2149 /* Extern inline functions don't become needed when referenced.
2150 If we know a method will be emitted in other TU and no new
2151 functions can be marked reachable, just use the external
2152 definition. */
2153 struct cgraph_node *node = cgraph_node::get_create (decl);
2154 node->forced_by_abi = true;
2156 /* #pragma interface can call mark_needed for
2157 maybe-in-charge 'tors; mark the clones as well. */
2158 tree clone;
2159 FOR_EACH_CLONE (clone, decl)
2160 mark_needed (clone);
2162 else if (VAR_P (decl))
2164 varpool_node *node = varpool_node::get_create (decl);
2165 /* C++ frontend use mark_decl_references to force COMDAT variables
2166 to be output that might appear dead otherwise. */
2167 node->forced_by_abi = true;
2171 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2172 returns true if a definition of this entity should be provided in
2173 this object file. Callers use this function to determine whether
2174 or not to let the back end know that a definition of DECL is
2175 available in this translation unit. */
2177 bool
2178 decl_needed_p (tree decl)
2180 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2181 /* This function should only be called at the end of the translation
2182 unit. We cannot be sure of whether or not something will be
2183 COMDAT until that point. */
2184 gcc_assert (at_eof);
2186 /* All entities with external linkage that are not COMDAT/EXTERN should be
2187 emitted; they may be referred to from other object files. */
2188 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2189 return true;
2190 /* Functions marked "dllexport" must be emitted so that they are
2191 visible to other DLLs. */
2192 if (flag_keep_inline_dllexport
2193 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2194 return true;
2196 /* When not optimizing, do not bother to produce definitions for extern
2197 symbols. */
2198 if (DECL_REALLY_EXTERN (decl)
2199 && ((TREE_CODE (decl) != FUNCTION_DECL
2200 && !optimize)
2201 || (TREE_CODE (decl) == FUNCTION_DECL
2202 && !opt_for_fn (decl, optimize)))
2203 && !lookup_attribute ("always_inline", decl))
2204 return false;
2206 /* If this entity was used, let the back end see it; it will decide
2207 whether or not to emit it into the object file. */
2208 if (TREE_USED (decl))
2209 return true;
2211 /* Virtual functions might be needed for devirtualization. */
2212 if (flag_devirtualize
2213 && TREE_CODE (decl) == FUNCTION_DECL
2214 && DECL_VIRTUAL_P (decl))
2215 return true;
2217 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2218 reference to DECL might cause it to be emitted later. */
2219 return false;
2222 /* If necessary, write out the vtables for the dynamic class CTYPE.
2223 Returns true if any vtables were emitted. */
2225 static bool
2226 maybe_emit_vtables (tree ctype)
2228 tree vtbl;
2229 tree primary_vtbl;
2230 int needed = 0;
2231 varpool_node *current = NULL, *last = NULL;
2233 /* If the vtables for this class have already been emitted there is
2234 nothing more to do. */
2235 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2236 if (var_finalized_p (primary_vtbl))
2237 return false;
2238 /* Ignore dummy vtables made by get_vtable_decl. */
2239 if (TREE_TYPE (primary_vtbl) == void_type_node)
2240 return false;
2242 /* On some targets, we cannot determine the key method until the end
2243 of the translation unit -- which is when this function is
2244 called. */
2245 if (!targetm.cxx.key_method_may_be_inline ())
2246 determine_key_method (ctype);
2248 /* See if any of the vtables are needed. */
2249 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2251 import_export_decl (vtbl);
2252 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2253 needed = 1;
2255 if (!needed)
2257 /* If the references to this class' vtables are optimized away,
2258 still emit the appropriate debugging information. See
2259 dfs_debug_mark. */
2260 if (DECL_COMDAT (primary_vtbl)
2261 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2262 note_debug_info_needed (ctype);
2263 return false;
2266 /* The ABI requires that we emit all of the vtables if we emit any
2267 of them. */
2268 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2270 /* Mark entities references from the virtual table as used. */
2271 mark_vtable_entries (vtbl);
2273 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2275 vec<tree, va_gc> *cleanups = NULL;
2276 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2277 LOOKUP_NORMAL);
2279 /* It had better be all done at compile-time. */
2280 gcc_assert (!expr && !cleanups);
2283 /* Write it out. */
2284 DECL_EXTERNAL (vtbl) = 0;
2285 rest_of_decl_compilation (vtbl, 1, 1);
2287 /* Because we're only doing syntax-checking, we'll never end up
2288 actually marking the variable as written. */
2289 if (flag_syntax_only)
2290 TREE_ASM_WRITTEN (vtbl) = 1;
2291 else if (DECL_ONE_ONLY (vtbl))
2293 current = varpool_node::get_create (vtbl);
2294 if (last)
2295 current->add_to_same_comdat_group (last);
2296 last = current;
2300 /* For abstract classes, the destructor has been removed from the
2301 vtable (in class.c's build_vtbl_initializer). For a compiler-
2302 generated destructor, it hence might not have been generated in
2303 this translation unit - and with '#pragma interface' it might
2304 never get generated. */
2305 if (CLASSTYPE_PURE_VIRTUALS (ctype)
2306 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype)
2307 && !CLASSTYPE_LAZY_DESTRUCTOR (ctype)
2308 && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype)))
2309 note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype));
2311 /* Since we're writing out the vtable here, also write the debug
2312 info. */
2313 note_debug_info_needed (ctype);
2315 return true;
2318 /* A special return value from type_visibility meaning internal
2319 linkage. */
2321 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2323 static int expr_visibility (tree);
2324 static int type_visibility (tree);
2326 /* walk_tree helper function for type_visibility. */
2328 static tree
2329 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2331 int *vis_p = (int *)data;
2332 if (! TYPE_P (*tp))
2334 *walk_subtrees = 0;
2336 else if (OVERLOAD_TYPE_P (*tp)
2337 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2339 *vis_p = VISIBILITY_ANON;
2340 return *tp;
2342 else if (CLASS_TYPE_P (*tp)
2343 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2344 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2345 else if (TREE_CODE (*tp) == ARRAY_TYPE
2346 && uses_template_parms (TYPE_DOMAIN (*tp)))
2348 int evis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp)));
2349 if (evis > *vis_p)
2350 *vis_p = evis;
2352 return NULL;
2355 /* walk_tree helper function for expr_visibility. */
2357 static tree
2358 min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
2360 int *vis_p = (int *)data;
2361 int tpvis = VISIBILITY_DEFAULT;
2363 switch (TREE_CODE (*tp))
2365 case CAST_EXPR:
2366 case IMPLICIT_CONV_EXPR:
2367 case STATIC_CAST_EXPR:
2368 case REINTERPRET_CAST_EXPR:
2369 case CONST_CAST_EXPR:
2370 case DYNAMIC_CAST_EXPR:
2371 case NEW_EXPR:
2372 case CONSTRUCTOR:
2373 case LAMBDA_EXPR:
2374 tpvis = type_visibility (TREE_TYPE (*tp));
2375 break;
2377 case VAR_DECL:
2378 case FUNCTION_DECL:
2379 if (! TREE_PUBLIC (*tp))
2380 tpvis = VISIBILITY_ANON;
2381 else
2382 tpvis = DECL_VISIBILITY (*tp);
2383 break;
2385 default:
2386 break;
2389 if (tpvis > *vis_p)
2390 *vis_p = tpvis;
2392 return NULL_TREE;
2395 /* Returns the visibility of TYPE, which is the minimum visibility of its
2396 component types. */
2398 static int
2399 type_visibility (tree type)
2401 int vis = VISIBILITY_DEFAULT;
2402 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2403 return vis;
2406 /* Returns the visibility of an expression EXPR that appears in the signature
2407 of a function template, which is the minimum visibility of names that appear
2408 in its mangling. */
2410 static int
2411 expr_visibility (tree expr)
2413 int vis = VISIBILITY_DEFAULT;
2414 cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis);
2415 return vis;
2418 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2419 specified (or if VISIBILITY is static). If TMPL is true, this
2420 constraint is for a template argument, and takes precedence
2421 over explicitly-specified visibility on the template. */
2423 static void
2424 constrain_visibility (tree decl, int visibility, bool tmpl)
2426 if (visibility == VISIBILITY_ANON)
2428 /* extern "C" declarations aren't affected by the anonymous
2429 namespace. */
2430 if (!DECL_EXTERN_C_P (decl))
2432 TREE_PUBLIC (decl) = 0;
2433 DECL_WEAK (decl) = 0;
2434 DECL_COMMON (decl) = 0;
2435 DECL_COMDAT (decl) = false;
2436 if (VAR_OR_FUNCTION_DECL_P (decl))
2438 struct symtab_node *snode = symtab_node::get (decl);
2440 if (snode)
2441 snode->set_comdat_group (NULL);
2443 DECL_INTERFACE_KNOWN (decl) = 1;
2444 if (DECL_LANG_SPECIFIC (decl))
2445 DECL_NOT_REALLY_EXTERN (decl) = 1;
2448 else if (visibility > DECL_VISIBILITY (decl)
2449 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2451 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2452 /* This visibility was not specified. */
2453 DECL_VISIBILITY_SPECIFIED (decl) = false;
2457 /* Constrain the visibility of DECL based on the visibility of its template
2458 arguments. */
2460 static void
2461 constrain_visibility_for_template (tree decl, tree targs)
2463 /* If this is a template instantiation, check the innermost
2464 template args for visibility constraints. The outer template
2465 args are covered by the class check. */
2466 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2467 int i;
2468 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2470 int vis = 0;
2472 tree arg = TREE_VEC_ELT (args, i-1);
2473 if (TYPE_P (arg))
2474 vis = type_visibility (arg);
2475 else
2476 vis = expr_visibility (arg);
2477 if (vis)
2478 constrain_visibility (decl, vis, true);
2482 /* Like c_determine_visibility, but with additional C++-specific
2483 behavior.
2485 Function-scope entities can rely on the function's visibility because
2486 it is set in start_preparsed_function.
2488 Class-scope entities cannot rely on the class's visibility until the end
2489 of the enclosing class definition.
2491 Note that because namespaces have multiple independent definitions,
2492 namespace visibility is handled elsewhere using the #pragma visibility
2493 machinery rather than by decorating the namespace declaration.
2495 The goal is for constraints from the type to give a diagnostic, and
2496 other constraints to be applied silently. */
2498 void
2499 determine_visibility (tree decl)
2501 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2503 /* Only relevant for names with external linkage. */
2504 if (!TREE_PUBLIC (decl))
2505 return;
2507 /* Cloned constructors and destructors get the same visibility as
2508 the underlying function. That should be set up in
2509 maybe_clone_body. */
2510 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2512 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2513 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2515 /* The decl may be a template instantiation, which could influence
2516 visibilty. */
2517 tree template_decl = NULL_TREE;
2518 if (TREE_CODE (decl) == TYPE_DECL)
2520 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2522 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2523 template_decl = decl;
2525 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2526 template_decl = decl;
2528 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2529 template_decl = decl;
2531 /* If DECL is a member of a class, visibility specifiers on the
2532 class can influence the visibility of the DECL. */
2533 tree class_type = NULL_TREE;
2534 if (DECL_CLASS_SCOPE_P (decl))
2535 class_type = DECL_CONTEXT (decl);
2536 else
2538 /* Not a class member. */
2540 /* Virtual tables have DECL_CONTEXT set to their associated class,
2541 so they are automatically handled above. */
2542 gcc_assert (!VAR_P (decl)
2543 || !DECL_VTABLE_OR_VTT_P (decl));
2545 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2547 /* Local statics and classes get the visibility of their
2548 containing function by default, except that
2549 -fvisibility-inlines-hidden doesn't affect them. */
2550 tree fn = DECL_CONTEXT (decl);
2551 if (DECL_VISIBILITY_SPECIFIED (fn))
2553 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2554 DECL_VISIBILITY_SPECIFIED (decl) =
2555 DECL_VISIBILITY_SPECIFIED (fn);
2557 else
2559 if (DECL_CLASS_SCOPE_P (fn))
2560 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2561 else if (determine_hidden_inline (fn))
2563 DECL_VISIBILITY (decl) = default_visibility;
2564 DECL_VISIBILITY_SPECIFIED (decl) =
2565 visibility_options.inpragma;
2567 else
2569 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2570 DECL_VISIBILITY_SPECIFIED (decl) =
2571 DECL_VISIBILITY_SPECIFIED (fn);
2575 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2576 but have no TEMPLATE_INFO, so don't try to check it. */
2577 template_decl = NULL_TREE;
2579 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2580 && flag_visibility_ms_compat)
2582 /* Under -fvisibility-ms-compat, types are visible by default,
2583 even though their contents aren't. */
2584 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2585 int underlying_vis = type_visibility (underlying_type);
2586 if (underlying_vis == VISIBILITY_ANON
2587 || (CLASS_TYPE_P (underlying_type)
2588 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2589 constrain_visibility (decl, underlying_vis, false);
2590 else
2591 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2593 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2595 /* tinfo visibility is based on the type it's for. */
2596 constrain_visibility
2597 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2599 /* Give the target a chance to override the visibility associated
2600 with DECL. */
2601 if (TREE_PUBLIC (decl)
2602 && !DECL_REALLY_EXTERN (decl)
2603 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2604 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2605 targetm.cxx.determine_class_data_visibility (decl);
2607 else if (template_decl)
2608 /* Template instantiations and specializations get visibility based
2609 on their template unless they override it with an attribute. */;
2610 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2612 if (determine_hidden_inline (decl))
2613 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2614 else
2616 /* Set default visibility to whatever the user supplied with
2617 #pragma GCC visibility or a namespace visibility attribute. */
2618 DECL_VISIBILITY (decl) = default_visibility;
2619 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2624 if (template_decl)
2626 /* If the specialization doesn't specify visibility, use the
2627 visibility from the template. */
2628 tree tinfo = get_template_info (template_decl);
2629 tree args = TI_ARGS (tinfo);
2630 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2631 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2632 : DECL_ATTRIBUTES (decl));
2633 tree attr = lookup_attribute ("visibility", attribs);
2635 if (args != error_mark_node)
2637 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2639 if (!DECL_VISIBILITY_SPECIFIED (decl))
2641 if (!attr
2642 && determine_hidden_inline (decl))
2643 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2644 else
2646 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2647 DECL_VISIBILITY_SPECIFIED (decl)
2648 = DECL_VISIBILITY_SPECIFIED (pattern);
2652 if (args
2653 /* Template argument visibility outweighs #pragma or namespace
2654 visibility, but not an explicit attribute. */
2655 && !attr)
2657 int depth = TMPL_ARGS_DEPTH (args);
2658 if (DECL_VISIBILITY_SPECIFIED (decl))
2660 /* A class template member with explicit visibility
2661 overrides the class visibility, so we need to apply
2662 all the levels of template args directly. */
2663 int i;
2664 for (i = 1; i <= depth; ++i)
2666 tree lev = TMPL_ARGS_LEVEL (args, i);
2667 constrain_visibility_for_template (decl, lev);
2670 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2671 /* Limit visibility based on its template arguments. */
2672 constrain_visibility_for_template (decl, args);
2677 if (class_type)
2678 determine_visibility_from_class (decl, class_type);
2680 if (decl_anon_ns_mem_p (decl))
2681 /* Names in an anonymous namespace get internal linkage.
2682 This might change once we implement export. */
2683 constrain_visibility (decl, VISIBILITY_ANON, false);
2684 else if (TREE_CODE (decl) != TYPE_DECL)
2686 /* Propagate anonymity from type to decl. */
2687 int tvis = type_visibility (TREE_TYPE (decl));
2688 if (tvis == VISIBILITY_ANON
2689 || ! DECL_VISIBILITY_SPECIFIED (decl))
2690 constrain_visibility (decl, tvis, false);
2692 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2693 /* DR 757: A type without linkage shall not be used as the type of a
2694 variable or function with linkage, unless
2695 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2696 o the variable or function is not used (3.2 [basic.def.odr]) or is
2697 defined in the same translation unit.
2699 Since non-extern "C" decls need to be defined in the same
2700 translation unit, we can make the type internal. */
2701 constrain_visibility (decl, VISIBILITY_ANON, false);
2703 /* If visibility changed and DECL already has DECL_RTL, ensure
2704 symbol flags are updated. */
2705 if ((DECL_VISIBILITY (decl) != orig_visibility
2706 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2707 && ((VAR_P (decl) && TREE_STATIC (decl))
2708 || TREE_CODE (decl) == FUNCTION_DECL)
2709 && DECL_RTL_SET_P (decl))
2710 make_decl_rtl (decl);
2713 /* By default, static data members and function members receive
2714 the visibility of their containing class. */
2716 static void
2717 determine_visibility_from_class (tree decl, tree class_type)
2719 if (DECL_VISIBILITY_SPECIFIED (decl))
2720 return;
2722 if (determine_hidden_inline (decl))
2723 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2724 else
2726 /* Default to the class visibility. */
2727 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2728 DECL_VISIBILITY_SPECIFIED (decl)
2729 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2732 /* Give the target a chance to override the visibility associated
2733 with DECL. */
2734 if (VAR_P (decl)
2735 && TREE_PUBLIC (decl)
2736 && (DECL_TINFO_P (decl) || DECL_VTABLE_OR_VTT_P (decl))
2737 && !DECL_REALLY_EXTERN (decl)
2738 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2739 targetm.cxx.determine_class_data_visibility (decl);
2742 /* Returns true iff DECL is an inline that should get hidden visibility
2743 because of -fvisibility-inlines-hidden. */
2745 static bool
2746 determine_hidden_inline (tree decl)
2748 return (visibility_options.inlines_hidden
2749 /* Don't do this for inline templates; specializations might not be
2750 inline, and we don't want them to inherit the hidden
2751 visibility. We'll set it here for all inline instantiations. */
2752 && !processing_template_decl
2753 && TREE_CODE (decl) == FUNCTION_DECL
2754 && DECL_DECLARED_INLINE_P (decl)
2755 && (! DECL_LANG_SPECIFIC (decl)
2756 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2759 /* Constrain the visibility of a class TYPE based on the visibility of its
2760 field types. Warn if any fields require lesser visibility. */
2762 void
2763 constrain_class_visibility (tree type)
2765 tree binfo;
2766 tree t;
2767 int i;
2769 int vis = type_visibility (type);
2771 if (vis == VISIBILITY_ANON
2772 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2773 return;
2775 /* Don't warn about visibility if the class has explicit visibility. */
2776 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2777 vis = VISIBILITY_INTERNAL;
2779 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2780 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
2781 && !DECL_ARTIFICIAL (t))
2783 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2784 int subvis = type_visibility (ftype);
2786 if (subvis == VISIBILITY_ANON)
2788 if (!in_main_input_context())
2790 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2791 if (nlt)
2793 if (same_type_p (TREE_TYPE (t), nlt))
2794 warning (OPT_Wsubobject_linkage, "\
2795 %qT has a field %qD whose type has no linkage",
2796 type, t);
2797 else
2798 warning (OPT_Wsubobject_linkage, "\
2799 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2800 type, t, nlt);
2802 else
2803 warning (OPT_Wsubobject_linkage, "\
2804 %qT has a field %qD whose type uses the anonymous namespace",
2805 type, t);
2808 else if (MAYBE_CLASS_TYPE_P (ftype)
2809 && vis < VISIBILITY_HIDDEN
2810 && subvis >= VISIBILITY_HIDDEN)
2811 warning (OPT_Wattributes, "\
2812 %qT declared with greater visibility than the type of its field %qD",
2813 type, t);
2816 binfo = TYPE_BINFO (type);
2817 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2819 int subvis = type_visibility (TREE_TYPE (t));
2821 if (subvis == VISIBILITY_ANON)
2823 if (!in_main_input_context())
2825 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2826 if (nlt)
2828 if (same_type_p (TREE_TYPE (t), nlt))
2829 warning (OPT_Wsubobject_linkage, "\
2830 %qT has a base %qT whose type has no linkage",
2831 type, TREE_TYPE (t));
2832 else
2833 warning (OPT_Wsubobject_linkage, "\
2834 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2835 type, TREE_TYPE (t), nlt);
2837 else
2838 warning (OPT_Wsubobject_linkage, "\
2839 %qT has a base %qT whose type uses the anonymous namespace",
2840 type, TREE_TYPE (t));
2843 else if (vis < VISIBILITY_HIDDEN
2844 && subvis >= VISIBILITY_HIDDEN)
2845 warning (OPT_Wattributes, "\
2846 %qT declared with greater visibility than its base %qT",
2847 type, TREE_TYPE (t));
2851 /* Functions for adjusting the visibility of a tagged type and its nested
2852 types and declarations when it gets a name for linkage purposes from a
2853 typedef. */
2855 static void bt_reset_linkage_1 (binding_entry, void *);
2856 static void bt_reset_linkage_2 (binding_entry, void *);
2858 /* First reset the visibility of all the types. */
2860 static void
2861 reset_type_linkage_1 (tree type)
2863 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2864 if (CLASS_TYPE_P (type))
2865 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2866 bt_reset_linkage_1, NULL);
2868 static void
2869 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2871 reset_type_linkage_1 (b->type);
2874 /* Then reset the visibility of any static data members or member
2875 functions that use those types. */
2877 static void
2878 reset_decl_linkage (tree decl)
2880 if (TREE_PUBLIC (decl))
2881 return;
2882 if (DECL_CLONED_FUNCTION_P (decl))
2883 return;
2884 TREE_PUBLIC (decl) = true;
2885 DECL_INTERFACE_KNOWN (decl) = false;
2886 determine_visibility (decl);
2887 tentative_decl_linkage (decl);
2890 static void
2891 reset_type_linkage_2 (tree type)
2893 if (CLASS_TYPE_P (type))
2895 if (tree vt = CLASSTYPE_VTABLES (type))
2897 tree name = mangle_vtbl_for_type (type);
2898 DECL_NAME (vt) = name;
2899 SET_DECL_ASSEMBLER_NAME (vt, name);
2900 reset_decl_linkage (vt);
2902 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2904 tree name = mangle_typeinfo_for_type (type);
2905 DECL_NAME (ti) = name;
2906 SET_DECL_ASSEMBLER_NAME (ti, name);
2907 TREE_TYPE (name) = type;
2908 reset_decl_linkage (ti);
2910 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2912 tree mem = STRIP_TEMPLATE (m);
2913 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2914 reset_decl_linkage (mem);
2916 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2917 bt_reset_linkage_2, NULL);
2921 static void
2922 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2924 reset_type_linkage_2 (b->type);
2926 void
2927 reset_type_linkage (tree type)
2929 reset_type_linkage_1 (type);
2930 reset_type_linkage_2 (type);
2933 /* Set up our initial idea of what the linkage of DECL should be. */
2935 void
2936 tentative_decl_linkage (tree decl)
2938 if (DECL_INTERFACE_KNOWN (decl))
2939 /* We've already made a decision as to how this function will
2940 be handled. */;
2941 else if (vague_linkage_p (decl))
2943 if (TREE_CODE (decl) == FUNCTION_DECL
2944 && decl_defined_p (decl))
2946 DECL_EXTERNAL (decl) = 1;
2947 DECL_NOT_REALLY_EXTERN (decl) = 1;
2948 note_vague_linkage_fn (decl);
2949 /* A non-template inline function with external linkage will
2950 always be COMDAT. As we must eventually determine the
2951 linkage of all functions, and as that causes writes to
2952 the data mapped in from the PCH file, it's advantageous
2953 to mark the functions at this point. */
2954 if (DECL_DECLARED_INLINE_P (decl)
2955 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2956 || DECL_DEFAULTED_FN (decl)))
2958 /* This function must have external linkage, as
2959 otherwise DECL_INTERFACE_KNOWN would have been
2960 set. */
2961 gcc_assert (TREE_PUBLIC (decl));
2962 comdat_linkage (decl);
2963 DECL_INTERFACE_KNOWN (decl) = 1;
2966 else if (VAR_P (decl))
2967 maybe_commonize_var (decl);
2971 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2972 for DECL has not already been determined, do so now by setting
2973 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2974 function is called entities with vague linkage whose definitions
2975 are available must have TREE_PUBLIC set.
2977 If this function decides to place DECL in COMDAT, it will set
2978 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2979 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2980 callers defer that decision until it is clear that DECL is actually
2981 required. */
2983 void
2984 import_export_decl (tree decl)
2986 bool comdat_p;
2987 bool import_p;
2988 tree class_type = NULL_TREE;
2990 if (DECL_INTERFACE_KNOWN (decl))
2991 return;
2993 /* We cannot determine what linkage to give to an entity with vague
2994 linkage until the end of the file. For example, a virtual table
2995 for a class will be defined if and only if the key method is
2996 defined in this translation unit. */
2997 gcc_assert (at_eof);
2998 /* Object file linkage for explicit instantiations is handled in
2999 mark_decl_instantiated. For static variables in functions with
3000 vague linkage, maybe_commonize_var is used.
3002 Therefore, the only declarations that should be provided to this
3003 function are those with external linkage that are:
3005 * implicit instantiations of function templates
3007 * inline function
3009 * implicit instantiations of static data members of class
3010 templates
3012 * virtual tables
3014 * typeinfo objects
3016 Furthermore, all entities that reach this point must have a
3017 definition available in this translation unit.
3019 The following assertions check these conditions. */
3020 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3021 /* Any code that creates entities with TREE_PUBLIC cleared should
3022 also set DECL_INTERFACE_KNOWN. */
3023 gcc_assert (TREE_PUBLIC (decl));
3024 if (TREE_CODE (decl) == FUNCTION_DECL)
3025 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3026 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
3027 || DECL_DECLARED_INLINE_P (decl));
3028 else
3029 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3030 || DECL_VTABLE_OR_VTT_P (decl)
3031 || DECL_TINFO_P (decl));
3032 /* Check that a definition of DECL is available in this translation
3033 unit. */
3034 gcc_assert (!DECL_REALLY_EXTERN (decl));
3036 /* Assume that DECL will not have COMDAT linkage. */
3037 comdat_p = false;
3038 /* Assume that DECL will not be imported into this translation
3039 unit. */
3040 import_p = false;
3042 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
3044 class_type = DECL_CONTEXT (decl);
3045 import_export_class (class_type);
3046 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
3047 && CLASSTYPE_INTERFACE_ONLY (class_type))
3048 import_p = true;
3049 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
3050 && !CLASSTYPE_USE_TEMPLATE (class_type)
3051 && CLASSTYPE_KEY_METHOD (class_type)
3052 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
3053 /* The ABI requires that all virtual tables be emitted with
3054 COMDAT linkage. However, on systems where COMDAT symbols
3055 don't show up in the table of contents for a static
3056 archive, or on systems without weak symbols (where we
3057 approximate COMDAT linkage by using internal linkage), the
3058 linker will report errors about undefined symbols because
3059 it will not see the virtual table definition. Therefore,
3060 in the case that we know that the virtual table will be
3061 emitted in only one translation unit, we make the virtual
3062 table an ordinary definition with external linkage. */
3063 DECL_EXTERNAL (decl) = 0;
3064 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
3066 /* CLASS_TYPE is being exported from this translation unit,
3067 so DECL should be defined here. */
3068 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
3069 /* If a class is declared in a header with the "extern
3070 template" extension, then it will not be instantiated,
3071 even in translation units that would normally require
3072 it. Often such classes are explicitly instantiated in
3073 one translation unit. Therefore, the explicit
3074 instantiation must be made visible to other translation
3075 units. */
3076 DECL_EXTERNAL (decl) = 0;
3077 else
3079 /* The generic C++ ABI says that class data is always
3080 COMDAT, even if there is a key function. Some
3081 variants (e.g., the ARM EABI) says that class data
3082 only has COMDAT linkage if the class data might be
3083 emitted in more than one translation unit. When the
3084 key method can be inline and is inline, we still have
3085 to arrange for comdat even though
3086 class_data_always_comdat is false. */
3087 if (!CLASSTYPE_KEY_METHOD (class_type)
3088 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
3089 || targetm.cxx.class_data_always_comdat ())
3091 /* The ABI requires COMDAT linkage. Normally, we
3092 only emit COMDAT things when they are needed;
3093 make sure that we realize that this entity is
3094 indeed needed. */
3095 comdat_p = true;
3096 mark_needed (decl);
3100 else if (!flag_implicit_templates
3101 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
3102 import_p = true;
3103 else
3104 comdat_p = true;
3106 else if (VAR_P (decl) && DECL_TINFO_P (decl))
3108 tree type = TREE_TYPE (DECL_NAME (decl));
3109 if (CLASS_TYPE_P (type))
3111 class_type = type;
3112 import_export_class (type);
3113 if (CLASSTYPE_INTERFACE_KNOWN (type)
3114 && TYPE_POLYMORPHIC_P (type)
3115 && CLASSTYPE_INTERFACE_ONLY (type)
3116 /* If -fno-rtti was specified, then we cannot be sure
3117 that RTTI information will be emitted with the
3118 virtual table of the class, so we must emit it
3119 wherever it is used. */
3120 && flag_rtti)
3121 import_p = true;
3122 else
3124 if (CLASSTYPE_INTERFACE_KNOWN (type)
3125 && !CLASSTYPE_INTERFACE_ONLY (type))
3127 comdat_p = (targetm.cxx.class_data_always_comdat ()
3128 || (CLASSTYPE_KEY_METHOD (type)
3129 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
3130 mark_needed (decl);
3131 if (!flag_weak)
3133 comdat_p = false;
3134 DECL_EXTERNAL (decl) = 0;
3137 else
3138 comdat_p = true;
3141 else
3142 comdat_p = true;
3144 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3146 /* DECL is an implicit instantiation of a function or static
3147 data member. */
3148 if (flag_implicit_templates
3149 || (flag_implicit_inline_templates
3150 && TREE_CODE (decl) == FUNCTION_DECL
3151 && DECL_DECLARED_INLINE_P (decl)))
3152 comdat_p = true;
3153 else
3154 /* If we are not implicitly generating templates, then mark
3155 this entity as undefined in this translation unit. */
3156 import_p = true;
3158 else if (DECL_FUNCTION_MEMBER_P (decl))
3160 if (!DECL_DECLARED_INLINE_P (decl))
3162 tree ctype = DECL_CONTEXT (decl);
3163 import_export_class (ctype);
3164 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3166 DECL_NOT_REALLY_EXTERN (decl)
3167 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3168 || (DECL_DECLARED_INLINE_P (decl)
3169 && ! flag_implement_inlines
3170 && !DECL_VINDEX (decl)));
3172 if (!DECL_NOT_REALLY_EXTERN (decl))
3173 DECL_EXTERNAL (decl) = 1;
3175 /* Always make artificials weak. */
3176 if (DECL_ARTIFICIAL (decl) && flag_weak)
3177 comdat_p = true;
3178 else
3179 maybe_make_one_only (decl);
3182 else
3183 comdat_p = true;
3185 else
3186 comdat_p = true;
3188 if (import_p)
3190 /* If we are importing DECL into this translation unit, mark is
3191 an undefined here. */
3192 DECL_EXTERNAL (decl) = 1;
3193 DECL_NOT_REALLY_EXTERN (decl) = 0;
3195 else if (comdat_p)
3197 /* If we decided to put DECL in COMDAT, mark it accordingly at
3198 this point. */
3199 comdat_linkage (decl);
3202 DECL_INTERFACE_KNOWN (decl) = 1;
3205 /* Return an expression that performs the destruction of DECL, which
3206 must be a VAR_DECL whose type has a non-trivial destructor, or is
3207 an array whose (innermost) elements have a non-trivial destructor. */
3209 tree
3210 build_cleanup (tree decl)
3212 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3213 gcc_assert (clean != NULL_TREE);
3214 return clean;
3217 /* GUARD is a helper variable for DECL; make them have the same linkage and
3218 visibility. */
3220 void
3221 copy_linkage (tree guard, tree decl)
3223 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3224 TREE_STATIC (guard) = TREE_STATIC (decl);
3225 DECL_COMMON (guard) = DECL_COMMON (decl);
3226 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3227 if (TREE_STATIC (guard))
3229 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3230 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3231 if (DECL_ONE_ONLY (decl))
3232 make_decl_one_only (guard, cxx_comdat_group (guard));
3233 if (TREE_PUBLIC (decl))
3234 DECL_WEAK (guard) = DECL_WEAK (decl);
3235 /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not
3236 be set until import_export_decl at EOF. */
3237 if (vague_linkage_p (decl))
3238 comdat_linkage (guard);
3239 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3240 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3244 /* Returns the initialization guard variable for the variable DECL,
3245 which has static storage duration. */
3247 tree
3248 get_guard (tree decl)
3250 tree sname;
3251 tree guard;
3253 sname = mangle_guard_variable (decl);
3254 guard = get_global_binding (sname);
3255 if (! guard)
3257 tree guard_type;
3259 /* We use a type that is big enough to contain a mutex as well
3260 as an integer counter. */
3261 guard_type = targetm.cxx.guard_type ();
3262 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3263 VAR_DECL, sname, guard_type);
3265 /* The guard should have the same linkage as what it guards. */
3266 copy_linkage (guard, decl);
3268 DECL_ARTIFICIAL (guard) = 1;
3269 DECL_IGNORED_P (guard) = 1;
3270 TREE_USED (guard) = 1;
3271 pushdecl_top_level_and_finish (guard, NULL_TREE);
3273 return guard;
3276 /* Return an atomic load of src with the appropriate memory model. */
3278 static tree
3279 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3281 tree ptr_type = build_pointer_type (char_type_node);
3282 tree mem_model = build_int_cst (integer_type_node, model);
3283 tree t, addr, val;
3284 unsigned int size;
3285 int fncode;
3287 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3289 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3290 t = builtin_decl_implicit ((enum built_in_function) fncode);
3292 addr = build1 (ADDR_EXPR, ptr_type, src);
3293 val = build_call_expr (t, 2, addr, mem_model);
3294 return val;
3297 /* Return those bits of the GUARD variable that should be set when the
3298 guarded entity is actually initialized. */
3300 static tree
3301 get_guard_bits (tree guard)
3303 if (!targetm.cxx.guard_mask_bit ())
3305 /* We only set the first byte of the guard, in order to leave room
3306 for a mutex in the high-order bits. */
3307 guard = build1 (ADDR_EXPR,
3308 build_pointer_type (TREE_TYPE (guard)),
3309 guard);
3310 guard = build1 (NOP_EXPR,
3311 build_pointer_type (char_type_node),
3312 guard);
3313 guard = build1 (INDIRECT_REF, char_type_node, guard);
3316 return guard;
3319 /* Return an expression which determines whether or not the GUARD
3320 variable has already been initialized. */
3322 tree
3323 get_guard_cond (tree guard, bool thread_safe)
3325 tree guard_value;
3327 if (!thread_safe)
3328 guard = get_guard_bits (guard);
3329 else
3330 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3332 /* Mask off all but the low bit. */
3333 if (targetm.cxx.guard_mask_bit ())
3335 guard_value = integer_one_node;
3336 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3337 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3338 guard = cp_build_binary_op (input_location,
3339 BIT_AND_EXPR, guard, guard_value,
3340 tf_warning_or_error);
3343 guard_value = integer_zero_node;
3344 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3345 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3346 return cp_build_binary_op (input_location,
3347 EQ_EXPR, guard, guard_value,
3348 tf_warning_or_error);
3351 /* Return an expression which sets the GUARD variable, indicating that
3352 the variable being guarded has been initialized. */
3354 tree
3355 set_guard (tree guard)
3357 tree guard_init;
3359 /* Set the GUARD to one. */
3360 guard = get_guard_bits (guard);
3361 guard_init = integer_one_node;
3362 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3363 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3364 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3365 tf_warning_or_error);
3368 /* Returns true iff we can tell that VAR does not have a dynamic
3369 initializer. */
3371 static bool
3372 var_defined_without_dynamic_init (tree var)
3374 /* If it's defined in another TU, we can't tell. */
3375 if (DECL_EXTERNAL (var))
3376 return false;
3377 /* If it has a non-trivial destructor, registering the destructor
3378 counts as dynamic initialization. */
3379 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3380 return false;
3381 /* If it's in this TU, its initializer has been processed, unless
3382 it's a case of self-initialization, then DECL_INITIALIZED_P is
3383 false while the initializer is handled by finish_id_expression. */
3384 if (!DECL_INITIALIZED_P (var))
3385 return false;
3386 /* If it has no initializer or a constant one, it's not dynamic. */
3387 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3388 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3391 /* Returns true iff VAR is a variable that needs uses to be
3392 wrapped for possible dynamic initialization. */
3394 static bool
3395 var_needs_tls_wrapper (tree var)
3397 return (!error_operand_p (var)
3398 && CP_DECL_THREAD_LOCAL_P (var)
3399 && !DECL_GNU_TLS_P (var)
3400 && !DECL_FUNCTION_SCOPE_P (var)
3401 && !var_defined_without_dynamic_init (var));
3404 /* Get the FUNCTION_DECL for the shared TLS init function for this
3405 translation unit. */
3407 static tree
3408 get_local_tls_init_fn (location_t loc)
3410 tree sname = get_identifier ("__tls_init");
3411 tree fn = get_global_binding (sname);
3412 if (!fn)
3414 fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname,
3415 build_function_type (void_type_node,
3416 void_list_node));
3417 SET_DECL_LANGUAGE (fn, lang_c);
3418 TREE_PUBLIC (fn) = false;
3419 DECL_ARTIFICIAL (fn) = true;
3420 mark_used (fn);
3421 set_global_binding (fn);
3423 return fn;
3426 /* Get a FUNCTION_DECL for the init function for the thread_local
3427 variable VAR. The init function will be an alias to the function
3428 that initializes all the non-local TLS variables in the translation
3429 unit. The init function is only used by the wrapper function. */
3431 static tree
3432 get_tls_init_fn (tree var)
3434 /* Only C++11 TLS vars need this init fn. */
3435 if (!var_needs_tls_wrapper (var))
3436 return NULL_TREE;
3438 /* If -fno-extern-tls-init, assume that we don't need to call
3439 a tls init function for a variable defined in another TU. */
3440 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3441 return NULL_TREE;
3443 /* If the variable is internal, or if we can't generate aliases,
3444 call the local init function directly. */
3445 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3446 return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var));
3448 tree sname = mangle_tls_init_fn (var);
3449 tree fn = get_global_binding (sname);
3450 if (!fn)
3452 fn = build_lang_decl (FUNCTION_DECL, sname,
3453 build_function_type (void_type_node,
3454 void_list_node));
3455 SET_DECL_LANGUAGE (fn, lang_c);
3456 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3457 DECL_ARTIFICIAL (fn) = true;
3458 DECL_COMDAT (fn) = DECL_COMDAT (var);
3459 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3460 if (DECL_ONE_ONLY (var))
3461 make_decl_one_only (fn, cxx_comdat_group (fn));
3462 if (TREE_PUBLIC (var))
3464 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3465 /* If the variable is defined somewhere else and might have static
3466 initialization, make the init function a weak reference. */
3467 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3468 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3469 || TYPE_HAS_TRIVIAL_DFLT (obtype))
3470 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3471 && DECL_EXTERNAL (var))
3472 declare_weak (fn);
3473 else
3474 DECL_WEAK (fn) = DECL_WEAK (var);
3476 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3477 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3478 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3479 DECL_IGNORED_P (fn) = 1;
3480 mark_used (fn);
3482 DECL_BEFRIENDING_CLASSES (fn) = var;
3484 set_global_binding (fn);
3486 return fn;
3489 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3490 variable VAR. The wrapper function calls the init function (if any) for
3491 VAR and then returns a reference to VAR. The wrapper function is used
3492 in place of VAR everywhere VAR is mentioned. */
3494 static tree
3495 get_tls_wrapper_fn (tree var)
3497 /* Only C++11 TLS vars need this wrapper fn. */
3498 if (!var_needs_tls_wrapper (var))
3499 return NULL_TREE;
3501 tree sname = mangle_tls_wrapper_fn (var);
3502 tree fn = get_global_binding (sname);
3503 if (!fn)
3505 /* A named rvalue reference is an lvalue, so the wrapper should
3506 always return an lvalue reference. */
3507 tree type = non_reference (TREE_TYPE (var));
3508 type = build_reference_type (type);
3509 tree fntype = build_function_type (type, void_list_node);
3511 fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var),
3512 FUNCTION_DECL, sname, fntype);
3513 SET_DECL_LANGUAGE (fn, lang_c);
3514 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3515 DECL_ARTIFICIAL (fn) = true;
3516 DECL_IGNORED_P (fn) = 1;
3517 /* The wrapper is inline and emitted everywhere var is used. */
3518 DECL_DECLARED_INLINE_P (fn) = true;
3519 if (TREE_PUBLIC (var))
3521 comdat_linkage (fn);
3522 #ifdef HAVE_GAS_HIDDEN
3523 /* Make the wrapper bind locally; there's no reason to share
3524 the wrapper between multiple shared objects. */
3525 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3526 DECL_VISIBILITY_SPECIFIED (fn) = true;
3527 #endif
3529 if (!TREE_PUBLIC (fn))
3530 DECL_INTERFACE_KNOWN (fn) = true;
3531 mark_used (fn);
3532 note_vague_linkage_fn (fn);
3534 #if 0
3535 /* We want CSE to commonize calls to the wrapper, but marking it as
3536 pure is unsafe since it has side-effects. I guess we need a new
3537 ECF flag even weaker than ECF_PURE. FIXME! */
3538 DECL_PURE_P (fn) = true;
3539 #endif
3541 DECL_BEFRIENDING_CLASSES (fn) = var;
3543 set_global_binding (fn);
3545 return fn;
3548 /* If EXPR is a thread_local variable that should be wrapped by init
3549 wrapper function, return a call to that function, otherwise return
3550 NULL. */
3552 tree
3553 maybe_get_tls_wrapper_call (tree expr)
3555 if (VAR_P (expr)
3556 && !processing_template_decl
3557 && !cp_unevaluated_operand
3558 && CP_DECL_THREAD_LOCAL_P (expr))
3559 if (tree wrap = get_tls_wrapper_fn (expr))
3560 return build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3561 return NULL;
3564 /* At EOF, generate the definition for the TLS wrapper function FN:
3566 T& var_wrapper() {
3567 if (init_fn) init_fn();
3568 return var;
3569 } */
3571 static void
3572 generate_tls_wrapper (tree fn)
3574 tree var = DECL_BEFRIENDING_CLASSES (fn);
3576 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3577 tree body = begin_function_body ();
3578 /* Only call the init fn if there might be one. */
3579 if (tree init_fn = get_tls_init_fn (var))
3581 tree if_stmt = NULL_TREE;
3582 /* If init_fn is a weakref, make sure it exists before calling. */
3583 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3585 if_stmt = begin_if_stmt ();
3586 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3587 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3588 NE_EXPR, addr, nullptr_node,
3589 tf_warning_or_error);
3590 finish_if_stmt_cond (cond, if_stmt);
3592 finish_expr_stmt (build_cxx_call
3593 (init_fn, 0, NULL, tf_warning_or_error));
3594 if (if_stmt)
3596 finish_then_clause (if_stmt);
3597 finish_if_stmt (if_stmt);
3600 else
3601 /* If there's no initialization, the wrapper is a constant function. */
3602 TREE_READONLY (fn) = true;
3603 finish_return_stmt (convert_from_reference (var));
3604 finish_function_body (body);
3605 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3608 /* Start the process of running a particular set of global constructors
3609 or destructors. Subroutine of do_[cd]tors. Also called from
3610 vtv_start_verification_constructor_init_function. */
3612 static tree
3613 start_objects (int method_type, int initp)
3615 tree body;
3616 tree fndecl;
3617 char type[14];
3619 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3621 if (initp != DEFAULT_INIT_PRIORITY)
3623 char joiner;
3625 #ifdef JOINER
3626 joiner = JOINER;
3627 #else
3628 joiner = '_';
3629 #endif
3631 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3633 else
3634 sprintf (type, "sub_%c", method_type);
3636 fndecl = build_lang_decl (FUNCTION_DECL,
3637 get_file_function_name (type),
3638 build_function_type_list (void_type_node,
3639 NULL_TREE));
3640 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3642 TREE_PUBLIC (current_function_decl) = 0;
3644 /* Mark as artificial because it's not explicitly in the user's
3645 source code. */
3646 DECL_ARTIFICIAL (current_function_decl) = 1;
3648 /* Mark this declaration as used to avoid spurious warnings. */
3649 TREE_USED (current_function_decl) = 1;
3651 /* Mark this function as a global constructor or destructor. */
3652 if (method_type == 'I')
3653 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3654 else
3655 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3657 body = begin_compound_stmt (BCS_FN_BODY);
3659 return body;
3662 /* Finish the process of running a particular set of global constructors
3663 or destructors. Subroutine of do_[cd]tors. */
3665 static void
3666 finish_objects (int method_type, int initp, tree body)
3668 tree fn;
3670 /* Finish up. */
3671 finish_compound_stmt (body);
3672 fn = finish_function (/*inline_p=*/false);
3674 if (method_type == 'I')
3676 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3677 decl_init_priority_insert (fn, initp);
3679 else
3681 DECL_STATIC_DESTRUCTOR (fn) = 1;
3682 decl_fini_priority_insert (fn, initp);
3685 expand_or_defer_fn (fn);
3688 /* The names of the parameters to the function created to handle
3689 initializations and destructions for objects with static storage
3690 duration. */
3691 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3692 #define PRIORITY_IDENTIFIER "__priority"
3694 /* The name of the function we create to handle initializations and
3695 destructions for objects with static storage duration. */
3696 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3698 /* The declaration for the __INITIALIZE_P argument. */
3699 static GTY(()) tree initialize_p_decl;
3701 /* The declaration for the __PRIORITY argument. */
3702 static GTY(()) tree priority_decl;
3704 /* The declaration for the static storage duration function. */
3705 static GTY(()) tree ssdf_decl;
3707 /* All the static storage duration functions created in this
3708 translation unit. */
3709 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3711 /* A map from priority levels to information about that priority
3712 level. There may be many such levels, so efficient lookup is
3713 important. */
3714 static splay_tree priority_info_map;
3716 /* Begins the generation of the function that will handle all
3717 initialization and destruction of objects with static storage
3718 duration. The function generated takes two parameters of type
3719 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3720 nonzero, it performs initializations. Otherwise, it performs
3721 destructions. It only performs those initializations or
3722 destructions with the indicated __PRIORITY. The generated function
3723 returns no value.
3725 It is assumed that this function will only be called once per
3726 translation unit. */
3728 static tree
3729 start_static_storage_duration_function (unsigned count)
3731 tree type;
3732 tree body;
3733 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3735 /* Create the identifier for this function. It will be of the form
3736 SSDF_IDENTIFIER_<number>. */
3737 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3739 type = build_function_type_list (void_type_node,
3740 integer_type_node, integer_type_node,
3741 NULL_TREE);
3743 /* Create the FUNCTION_DECL itself. */
3744 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3745 get_identifier (id),
3746 type);
3747 TREE_PUBLIC (ssdf_decl) = 0;
3748 DECL_ARTIFICIAL (ssdf_decl) = 1;
3750 /* Put this function in the list of functions to be called from the
3751 static constructors and destructors. */
3752 if (!ssdf_decls)
3754 vec_alloc (ssdf_decls, 32);
3756 /* Take this opportunity to initialize the map from priority
3757 numbers to information about that priority level. */
3758 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3759 /*delete_key_fn=*/0,
3760 /*delete_value_fn=*/
3761 splay_tree_delete_pointers);
3763 /* We always need to generate functions for the
3764 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3765 priorities later, we'll be sure to find the
3766 DEFAULT_INIT_PRIORITY. */
3767 get_priority_info (DEFAULT_INIT_PRIORITY);
3770 vec_safe_push (ssdf_decls, ssdf_decl);
3772 /* Create the argument list. */
3773 initialize_p_decl = cp_build_parm_decl
3774 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3775 TREE_USED (initialize_p_decl) = 1;
3776 priority_decl = cp_build_parm_decl
3777 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3778 TREE_USED (priority_decl) = 1;
3780 DECL_CHAIN (initialize_p_decl) = priority_decl;
3781 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3783 /* Put the function in the global scope. */
3784 pushdecl (ssdf_decl);
3786 /* Start the function itself. This is equivalent to declaring the
3787 function as:
3789 static void __ssdf (int __initialize_p, init __priority_p);
3791 It is static because we only need to call this function from the
3792 various constructor and destructor functions for this module. */
3793 start_preparsed_function (ssdf_decl,
3794 /*attrs=*/NULL_TREE,
3795 SF_PRE_PARSED);
3797 /* Set up the scope of the outermost block in the function. */
3798 body = begin_compound_stmt (BCS_FN_BODY);
3800 return body;
3803 /* Finish the generation of the function which performs initialization
3804 and destruction of objects with static storage duration. After
3805 this point, no more such objects can be created. */
3807 static void
3808 finish_static_storage_duration_function (tree body)
3810 /* Close out the function. */
3811 finish_compound_stmt (body);
3812 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3815 /* Return the information about the indicated PRIORITY level. If no
3816 code to handle this level has yet been generated, generate the
3817 appropriate prologue. */
3819 static priority_info
3820 get_priority_info (int priority)
3822 priority_info pi;
3823 splay_tree_node n;
3825 n = splay_tree_lookup (priority_info_map,
3826 (splay_tree_key) priority);
3827 if (!n)
3829 /* Create a new priority information structure, and insert it
3830 into the map. */
3831 pi = XNEW (struct priority_info_s);
3832 pi->initializations_p = 0;
3833 pi->destructions_p = 0;
3834 splay_tree_insert (priority_info_map,
3835 (splay_tree_key) priority,
3836 (splay_tree_value) pi);
3838 else
3839 pi = (priority_info) n->value;
3841 return pi;
3844 /* The effective initialization priority of a DECL. */
3846 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3847 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3848 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3850 /* Whether a DECL needs a guard to protect it against multiple
3851 initialization. */
3853 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3854 || DECL_ONE_ONLY (decl) \
3855 || DECL_WEAK (decl)))
3857 /* Called from one_static_initialization_or_destruction(),
3858 via walk_tree.
3859 Walks the initializer list of a global variable and looks for
3860 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3861 and that have their DECL_CONTEXT() == NULL.
3862 For each such temporary variable, set their DECL_CONTEXT() to
3863 the current function. This is necessary because otherwise
3864 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3865 when trying to refer to a temporary variable that does not have
3866 it's DECL_CONTECT() properly set. */
3867 static tree
3868 fix_temporary_vars_context_r (tree *node,
3869 int * /*unused*/,
3870 void * /*unused1*/)
3872 gcc_assert (current_function_decl);
3874 if (TREE_CODE (*node) == BIND_EXPR)
3876 tree var;
3878 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3879 if (VAR_P (var)
3880 && !DECL_NAME (var)
3881 && DECL_ARTIFICIAL (var)
3882 && !DECL_CONTEXT (var))
3883 DECL_CONTEXT (var) = current_function_decl;
3886 return NULL_TREE;
3889 /* Set up to handle the initialization or destruction of DECL. If
3890 INITP is nonzero, we are initializing the variable. Otherwise, we
3891 are destroying it. */
3893 static void
3894 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3896 tree guard_if_stmt = NULL_TREE;
3897 tree guard;
3899 /* If we are supposed to destruct and there's a trivial destructor,
3900 nothing has to be done. */
3901 if (!initp
3902 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3903 return;
3905 /* Trick the compiler into thinking we are at the file and line
3906 where DECL was declared so that error-messages make sense, and so
3907 that the debugger will show somewhat sensible file and line
3908 information. */
3909 input_location = DECL_SOURCE_LOCATION (decl);
3911 /* Make sure temporary variables in the initialiser all have
3912 their DECL_CONTEXT() set to a value different from NULL_TREE.
3913 This can happen when global variables initializers are built.
3914 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3915 the temporary variables that might have been generated in the
3916 accompanying initializers is NULL_TREE, meaning the variables have been
3917 declared in the global namespace.
3918 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3919 of the temporaries are set to the current function decl. */
3920 cp_walk_tree_without_duplicates (&init,
3921 fix_temporary_vars_context_r,
3922 NULL);
3924 /* Because of:
3926 [class.access.spec]
3928 Access control for implicit calls to the constructors,
3929 the conversion functions, or the destructor called to
3930 create and destroy a static data member is performed as
3931 if these calls appeared in the scope of the member's
3932 class.
3934 we pretend we are in a static member function of the class of
3935 which the DECL is a member. */
3936 if (member_p (decl))
3938 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3939 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3942 /* Assume we don't need a guard. */
3943 guard = NULL_TREE;
3944 /* We need a guard if this is an object with external linkage that
3945 might be initialized in more than one place. (For example, a
3946 static data member of a template, when the data member requires
3947 construction.) */
3948 if (NEEDS_GUARD_P (decl))
3950 tree guard_cond;
3952 guard = get_guard (decl);
3954 /* When using __cxa_atexit, we just check the GUARD as we would
3955 for a local static. */
3956 if (flag_use_cxa_atexit)
3958 /* When using __cxa_atexit, we never try to destroy
3959 anything from a static destructor. */
3960 gcc_assert (initp);
3961 guard_cond = get_guard_cond (guard, false);
3963 /* If we don't have __cxa_atexit, then we will be running
3964 destructors from .fini sections, or their equivalents. So,
3965 we need to know how many times we've tried to initialize this
3966 object. We do initializations only if the GUARD is zero,
3967 i.e., if we are the first to initialize the variable. We do
3968 destructions only if the GUARD is one, i.e., if we are the
3969 last to destroy the variable. */
3970 else if (initp)
3971 guard_cond
3972 = cp_build_binary_op (input_location,
3973 EQ_EXPR,
3974 cp_build_unary_op (PREINCREMENT_EXPR,
3975 guard,
3976 /*noconvert=*/true,
3977 tf_warning_or_error),
3978 integer_one_node,
3979 tf_warning_or_error);
3980 else
3981 guard_cond
3982 = cp_build_binary_op (input_location,
3983 EQ_EXPR,
3984 cp_build_unary_op (PREDECREMENT_EXPR,
3985 guard,
3986 /*noconvert=*/true,
3987 tf_warning_or_error),
3988 integer_zero_node,
3989 tf_warning_or_error);
3991 guard_if_stmt = begin_if_stmt ();
3992 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3996 /* If we're using __cxa_atexit, we have not already set the GUARD,
3997 so we must do so now. */
3998 if (guard && initp && flag_use_cxa_atexit)
3999 finish_expr_stmt (set_guard (guard));
4001 /* Perform the initialization or destruction. */
4002 if (initp)
4004 if (init)
4006 finish_expr_stmt (init);
4007 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
4009 varpool_node *vnode = varpool_node::get (decl);
4010 if (vnode)
4011 vnode->dynamically_initialized = 1;
4015 /* If we're using __cxa_atexit, register a function that calls the
4016 destructor for the object. */
4017 if (flag_use_cxa_atexit)
4018 finish_expr_stmt (register_dtor_fn (decl));
4020 else
4021 finish_expr_stmt (build_cleanup (decl));
4023 /* Finish the guard if-stmt, if necessary. */
4024 if (guard)
4026 finish_then_clause (guard_if_stmt);
4027 finish_if_stmt (guard_if_stmt);
4030 /* Now that we're done with DECL we don't need to pretend to be a
4031 member of its class any longer. */
4032 DECL_CONTEXT (current_function_decl) = NULL_TREE;
4033 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
4036 /* Generate code to do the initialization or destruction of the decls in VARS,
4037 a TREE_LIST of VAR_DECL with static storage duration.
4038 Whether initialization or destruction is performed is specified by INITP. */
4040 static void
4041 do_static_initialization_or_destruction (tree vars, bool initp)
4043 tree node, init_if_stmt, cond;
4045 /* Build the outer if-stmt to check for initialization or destruction. */
4046 init_if_stmt = begin_if_stmt ();
4047 cond = initp ? integer_one_node : integer_zero_node;
4048 cond = cp_build_binary_op (input_location,
4049 EQ_EXPR,
4050 initialize_p_decl,
4051 cond,
4052 tf_warning_or_error);
4053 finish_if_stmt_cond (cond, init_if_stmt);
4055 /* To make sure dynamic construction doesn't access globals from other
4056 compilation units where they might not be yet constructed, for
4057 -fsanitize=address insert __asan_before_dynamic_init call that
4058 prevents access to either all global variables that need construction
4059 in other compilation units, or at least those that haven't been
4060 initialized yet. Variables that need dynamic construction in
4061 the current compilation unit are kept accessible. */
4062 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4063 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
4065 node = vars;
4066 do {
4067 tree decl = TREE_VALUE (node);
4068 tree priority_if_stmt;
4069 int priority;
4070 priority_info pi;
4072 /* If we don't need a destructor, there's nothing to do. Avoid
4073 creating a possibly empty if-stmt. */
4074 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
4076 node = TREE_CHAIN (node);
4077 continue;
4080 /* Remember that we had an initialization or finalization at this
4081 priority. */
4082 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
4083 pi = get_priority_info (priority);
4084 if (initp)
4085 pi->initializations_p = 1;
4086 else
4087 pi->destructions_p = 1;
4089 /* Conditionalize this initialization on being in the right priority
4090 and being initializing/finalizing appropriately. */
4091 priority_if_stmt = begin_if_stmt ();
4092 cond = cp_build_binary_op (input_location,
4093 EQ_EXPR,
4094 priority_decl,
4095 build_int_cst (NULL_TREE, priority),
4096 tf_warning_or_error);
4097 finish_if_stmt_cond (cond, priority_if_stmt);
4099 /* Process initializers with same priority. */
4100 for (; node
4101 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
4102 node = TREE_CHAIN (node))
4103 /* Do one initialization or destruction. */
4104 one_static_initialization_or_destruction (TREE_VALUE (node),
4105 TREE_PURPOSE (node), initp);
4107 /* Finish up the priority if-stmt body. */
4108 finish_then_clause (priority_if_stmt);
4109 finish_if_stmt (priority_if_stmt);
4111 } while (node);
4113 /* Revert what __asan_before_dynamic_init did by calling
4114 __asan_after_dynamic_init. */
4115 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4116 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4118 /* Finish up the init/destruct if-stmt body. */
4119 finish_then_clause (init_if_stmt);
4120 finish_if_stmt (init_if_stmt);
4123 /* VARS is a list of variables with static storage duration which may
4124 need initialization and/or finalization. Remove those variables
4125 that don't really need to be initialized or finalized, and return
4126 the resulting list. The order in which the variables appear in
4127 VARS is in reverse order of the order in which they should actually
4128 be initialized. The list we return is in the unreversed order;
4129 i.e., the first variable should be initialized first. */
4131 static tree
4132 prune_vars_needing_no_initialization (tree *vars)
4134 tree *var = vars;
4135 tree result = NULL_TREE;
4137 while (*var)
4139 tree t = *var;
4140 tree decl = TREE_VALUE (t);
4141 tree init = TREE_PURPOSE (t);
4143 /* Deal gracefully with error. */
4144 if (error_operand_p (decl))
4146 var = &TREE_CHAIN (t);
4147 continue;
4150 /* The only things that can be initialized are variables. */
4151 gcc_assert (VAR_P (decl));
4153 /* If this object is not defined, we don't need to do anything
4154 here. */
4155 if (DECL_EXTERNAL (decl))
4157 var = &TREE_CHAIN (t);
4158 continue;
4161 /* Also, if the initializer already contains errors, we can bail
4162 out now. */
4163 if (init && TREE_CODE (init) == TREE_LIST
4164 && value_member (error_mark_node, init))
4166 var = &TREE_CHAIN (t);
4167 continue;
4170 /* This variable is going to need initialization and/or
4171 finalization, so we add it to the list. */
4172 *var = TREE_CHAIN (t);
4173 TREE_CHAIN (t) = result;
4174 result = t;
4177 return result;
4180 /* Make sure we have told the back end about all the variables in
4181 VARS. */
4183 static void
4184 write_out_vars (tree vars)
4186 tree v;
4188 for (v = vars; v; v = TREE_CHAIN (v))
4190 tree var = TREE_VALUE (v);
4191 if (!var_finalized_p (var))
4193 import_export_decl (var);
4194 rest_of_decl_compilation (var, 1, 1);
4199 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4200 (otherwise) that will initialize all global objects with static
4201 storage duration having the indicated PRIORITY. */
4203 static void
4204 generate_ctor_or_dtor_function (bool constructor_p, int priority,
4205 location_t *locus)
4207 char function_key;
4208 tree fndecl;
4209 tree body;
4210 size_t i;
4212 input_location = *locus;
4213 /* ??? */
4214 /* Was: locus->line++; */
4216 /* We use `I' to indicate initialization and `D' to indicate
4217 destruction. */
4218 function_key = constructor_p ? 'I' : 'D';
4220 /* We emit the function lazily, to avoid generating empty
4221 global constructors and destructors. */
4222 body = NULL_TREE;
4224 /* For Objective-C++, we may need to initialize metadata found in this module.
4225 This must be done _before_ any other static initializations. */
4226 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4227 && constructor_p && objc_static_init_needed_p ())
4229 body = start_objects (function_key, priority);
4230 objc_generate_static_init_call (NULL_TREE);
4233 /* Call the static storage duration function with appropriate
4234 arguments. */
4235 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4237 /* Calls to pure or const functions will expand to nothing. */
4238 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4240 tree call;
4242 if (! body)
4243 body = start_objects (function_key, priority);
4245 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4246 build_int_cst (NULL_TREE,
4247 constructor_p),
4248 build_int_cst (NULL_TREE,
4249 priority),
4250 NULL_TREE);
4251 finish_expr_stmt (call);
4255 /* Close out the function. */
4256 if (body)
4257 finish_objects (function_key, priority, body);
4260 /* Generate constructor and destructor functions for the priority
4261 indicated by N. */
4263 static int
4264 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4266 location_t *locus = (location_t *) data;
4267 int priority = (int) n->key;
4268 priority_info pi = (priority_info) n->value;
4270 /* Generate the functions themselves, but only if they are really
4271 needed. */
4272 if (pi->initializations_p)
4273 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4274 if (pi->destructions_p)
4275 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4277 /* Keep iterating. */
4278 return 0;
4281 /* Return C++ property of T, based on given operation OP. */
4283 static int
4284 cpp_check (tree t, cpp_operation op)
4286 switch (op)
4288 case HAS_DEPENDENT_TEMPLATE_ARGS:
4290 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4291 if (!ti)
4292 return 0;
4293 ++processing_template_decl;
4294 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4295 --processing_template_decl;
4296 return dep;
4298 case IS_ABSTRACT:
4299 return DECL_PURE_VIRTUAL_P (t);
4300 case IS_ASSIGNMENT_OPERATOR:
4301 return DECL_ASSIGNMENT_OPERATOR_P (t);
4302 case IS_CONSTRUCTOR:
4303 return DECL_CONSTRUCTOR_P (t);
4304 case IS_DESTRUCTOR:
4305 return DECL_DESTRUCTOR_P (t);
4306 case IS_COPY_CONSTRUCTOR:
4307 return DECL_COPY_CONSTRUCTOR_P (t);
4308 case IS_MOVE_CONSTRUCTOR:
4309 return DECL_MOVE_CONSTRUCTOR_P (t);
4310 case IS_TEMPLATE:
4311 return TREE_CODE (t) == TEMPLATE_DECL;
4312 case IS_TRIVIAL:
4313 return trivial_type_p (t);
4314 default:
4315 return 0;
4319 /* Collect source file references recursively, starting from NAMESPC. */
4321 static void
4322 collect_source_refs (tree namespc)
4324 /* Iterate over names in this name space. */
4325 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4326 if (DECL_IS_BUILTIN (t))
4328 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4329 collect_source_refs (t);
4330 else
4331 collect_source_ref (DECL_SOURCE_FILE (t));
4334 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4335 starting from NAMESPC. */
4337 static void
4338 collect_ada_namespace (tree namespc, const char *source_file)
4340 tree decl = NAMESPACE_LEVEL (namespc)->names;
4342 /* Collect decls from this namespace. This will skip
4343 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4344 collect_ada_nodes (decl, source_file);
4346 /* Now scan for namespace children, and dump them. */
4347 for (; decl; decl = TREE_CHAIN (decl))
4348 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4349 collect_ada_namespace (decl, source_file);
4352 /* Returns true iff there is a definition available for variable or
4353 function DECL. */
4355 bool
4356 decl_defined_p (tree decl)
4358 if (TREE_CODE (decl) == FUNCTION_DECL)
4359 return (DECL_INITIAL (decl) != NULL_TREE
4360 /* A pending instantiation of a friend temploid is defined. */
4361 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4362 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4363 (DECL_TI_TEMPLATE (decl)))));
4364 else
4366 gcc_assert (VAR_P (decl));
4367 return !DECL_EXTERNAL (decl);
4371 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4373 [expr.const]
4375 An integral constant-expression can only involve ... const
4376 variables of integral or enumeration types initialized with
4377 constant expressions ...
4379 C++0x also allows constexpr variables and temporaries initialized
4380 with constant expressions. We handle the former here, but the latter
4381 are just folded away in cxx_eval_constant_expression.
4383 The standard does not require that the expression be non-volatile.
4384 G++ implements the proposed correction in DR 457. */
4386 bool
4387 decl_constant_var_p (tree decl)
4389 if (!decl_maybe_constant_var_p (decl))
4390 return false;
4392 /* We don't know if a template static data member is initialized with
4393 a constant expression until we instantiate its initializer. Even
4394 in the case of a constexpr variable, we can't treat it as a
4395 constant until its initializer is complete in case it's used in
4396 its own initializer. */
4397 maybe_instantiate_decl (decl);
4398 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4401 /* Returns true if DECL could be a symbolic constant variable, depending on
4402 its initializer. */
4404 bool
4405 decl_maybe_constant_var_p (tree decl)
4407 tree type = TREE_TYPE (decl);
4408 if (!VAR_P (decl))
4409 return false;
4410 if (DECL_DECLARED_CONSTEXPR_P (decl) && !TREE_THIS_VOLATILE (decl))
4411 return true;
4412 if (DECL_HAS_VALUE_EXPR_P (decl))
4413 /* A proxy isn't constant. */
4414 return false;
4415 if (TYPE_REF_P (type))
4416 /* References can be constant. */;
4417 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4418 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4419 /* And const integers. */;
4420 else
4421 return false;
4423 if (DECL_INITIAL (decl)
4424 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4425 /* We know the initializer, and it isn't constant. */
4426 return false;
4427 else
4428 return true;
4431 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4432 called from grokfndecl and grokvardecl; in all modes it is called from
4433 cp_write_global_declarations. */
4435 void
4436 no_linkage_error (tree decl)
4438 if (cxx_dialect >= cxx11
4439 && (decl_defined_p (decl)
4440 /* Treat templates which limit_bad_template_recursion decided
4441 not to instantiate as if they were defined. */
4442 || (errorcount + sorrycount > 0
4443 && DECL_LANG_SPECIFIC (decl)
4444 && DECL_TEMPLATE_INFO (decl)
4445 && TREE_NO_WARNING (decl))))
4446 /* In C++11 it's ok if the decl is defined. */
4447 return;
4448 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4449 if (t == NULL_TREE)
4450 /* The type that got us on no_linkage_decls must have gotten a name for
4451 linkage purposes. */;
4452 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4453 /* The type might end up having a typedef name for linkage purposes. */
4454 vec_safe_push (no_linkage_decls, decl);
4455 else if (TYPE_UNNAMED_P (t))
4457 bool d = false;
4458 auto_diagnostic_group grp;
4459 if (cxx_dialect >= cxx11)
4460 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4461 "unnamed type, is used but never defined", decl);
4462 else if (DECL_EXTERN_C_P (decl))
4463 /* Allow this; it's pretty common in C. */;
4464 else if (VAR_P (decl))
4465 /* DRs 132, 319 and 389 seem to indicate types with
4466 no linkage can only be used to declare extern "C"
4467 entities. Since it's not always an error in the
4468 ISO C++ 90 Standard, we only issue a warning. */
4469 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4470 "with no linkage used to declare variable %q#D with "
4471 "linkage", decl);
4472 else
4473 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4474 "linkage used to declare function %q#D with linkage",
4475 decl);
4476 if (d && is_typedef_decl (TYPE_NAME (t)))
4477 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4478 "to the unqualified type, so it is not used for linkage",
4479 TYPE_NAME (t));
4481 else if (cxx_dialect >= cxx11)
4483 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4484 permerror (DECL_SOURCE_LOCATION (decl),
4485 "%q#D, declared using local type "
4486 "%qT, is used but never defined", decl, t);
4488 else if (VAR_P (decl))
4489 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4490 "used to declare variable %q#D with linkage", t, decl);
4491 else
4492 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4493 "to declare function %q#D with linkage", t, decl);
4496 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4498 static void
4499 collect_all_refs (const char *source_file)
4501 collect_ada_namespace (global_namespace, source_file);
4504 /* Clear DECL_EXTERNAL for NODE. */
4506 static bool
4507 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4509 DECL_EXTERNAL (node->decl) = 0;
4510 return false;
4513 /* Build up the function to run dynamic initializers for thread_local
4514 variables in this translation unit and alias the init functions for the
4515 individual variables to it. */
4517 static void
4518 handle_tls_init (void)
4520 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4521 if (vars == NULL_TREE)
4522 return;
4524 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4526 write_out_vars (vars);
4528 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4529 boolean_type_node);
4530 TREE_PUBLIC (guard) = false;
4531 TREE_STATIC (guard) = true;
4532 DECL_ARTIFICIAL (guard) = true;
4533 DECL_IGNORED_P (guard) = true;
4534 TREE_USED (guard) = true;
4535 CP_DECL_THREAD_LOCAL_P (guard) = true;
4536 set_decl_tls_model (guard, decl_default_tls_model (guard));
4537 pushdecl_top_level_and_finish (guard, NULL_TREE);
4539 tree fn = get_local_tls_init_fn (loc);
4540 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4541 tree body = begin_function_body ();
4542 tree if_stmt = begin_if_stmt ();
4543 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4544 tf_warning_or_error);
4545 finish_if_stmt_cond (cond, if_stmt);
4546 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4547 boolean_true_node,
4548 tf_warning_or_error));
4549 for (; vars; vars = TREE_CHAIN (vars))
4551 tree var = TREE_VALUE (vars);
4552 tree init = TREE_PURPOSE (vars);
4553 one_static_initialization_or_destruction (var, init, true);
4555 /* Output init aliases even with -fno-extern-tls-init. */
4556 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4558 tree single_init_fn = get_tls_init_fn (var);
4559 if (single_init_fn == NULL_TREE)
4560 continue;
4561 cgraph_node *alias
4562 = cgraph_node::get_create (fn)->create_same_body_alias
4563 (single_init_fn, fn);
4564 gcc_assert (alias != NULL);
4568 finish_then_clause (if_stmt);
4569 finish_if_stmt (if_stmt);
4570 finish_function_body (body);
4571 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4574 /* We're at the end of compilation, so generate any mangling aliases that
4575 we've been saving up, if DECL is going to be output and ID2 isn't
4576 already taken by another declaration. */
4578 static void
4579 generate_mangling_alias (tree decl, tree id2)
4581 struct cgraph_node *n = NULL;
4583 if (TREE_CODE (decl) == FUNCTION_DECL)
4585 n = cgraph_node::get (decl);
4586 if (!n)
4587 /* Don't create an alias to an unreferenced function. */
4588 return;
4591 tree *slot
4592 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4593 INSERT);
4595 /* If there's a declaration already using this mangled name,
4596 don't create a compatibility alias that conflicts. */
4597 if (*slot)
4598 return;
4600 tree alias = make_alias_for (decl, id2);
4601 *slot = alias;
4603 DECL_IGNORED_P (alias) = 1;
4604 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4605 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4606 if (vague_linkage_p (decl))
4607 DECL_WEAK (alias) = 1;
4609 if (n)
4610 n->create_same_body_alias (alias, decl);
4611 else
4612 varpool_node::create_extra_name_alias (alias, decl);
4615 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4616 the end of translation, for compatibility across bugs in the mangling
4617 implementation. */
4619 void
4620 note_mangling_alias (tree decl, tree id2)
4622 if (TARGET_SUPPORTS_ALIASES)
4624 if (!defer_mangling_aliases)
4625 generate_mangling_alias (decl, id2);
4626 else
4628 vec_safe_push (mangling_aliases, decl);
4629 vec_safe_push (mangling_aliases, id2);
4634 /* Emit all mangling aliases that were deferred up to this point. */
4636 void
4637 generate_mangling_aliases ()
4639 while (!vec_safe_is_empty (mangling_aliases))
4641 tree id2 = mangling_aliases->pop();
4642 tree decl = mangling_aliases->pop();
4643 generate_mangling_alias (decl, id2);
4645 defer_mangling_aliases = false;
4648 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4649 set. NEED_WARNING is true if we must warn about collisions. We do
4650 this to spot changes in mangling that may require compatibility
4651 aliases. */
4653 void
4654 record_mangling (tree decl, bool need_warning)
4656 if (!mangled_decls)
4657 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4659 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4660 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4661 tree *slot
4662 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4663 INSERT);
4665 /* If this is already an alias, remove the alias, because the real
4666 decl takes precedence. */
4667 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4668 if (symtab_node *n = symtab_node::get (*slot))
4669 if (n->cpp_implicit_alias)
4671 n->remove ();
4672 *slot = NULL_TREE;
4675 if (!*slot)
4676 *slot = decl;
4677 else if (need_warning)
4679 error_at (DECL_SOURCE_LOCATION (decl),
4680 "mangling of %q#D as %qE conflicts with a previous mangle",
4681 decl, id);
4682 inform (DECL_SOURCE_LOCATION (*slot),
4683 "previous mangling %q#D", *slot);
4684 inform (DECL_SOURCE_LOCATION (decl),
4685 "a later %<-fabi-version=%> (or =0)"
4686 " avoids this error with a change in mangling");
4687 *slot = decl;
4691 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4692 any existing knowledge of DECL's mangled name meaning DECL. */
4694 void
4695 overwrite_mangling (tree decl, tree name)
4697 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4698 if ((TREE_CODE (decl) == VAR_DECL
4699 || TREE_CODE (decl) == FUNCTION_DECL)
4700 && mangled_decls)
4701 if (tree *slot
4702 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4703 NO_INSERT))
4704 if (*slot == decl)
4706 mangled_decls->clear_slot (slot);
4708 /* If this is an alias, remove it from the symbol table. */
4709 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4710 if (symtab_node *n = symtab_node::get (decl))
4711 if (n->cpp_implicit_alias)
4712 n->remove ();
4715 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4718 /* The entire file is now complete. If requested, dump everything
4719 to a file. */
4721 static void
4722 dump_tu (void)
4724 dump_flags_t flags;
4725 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4727 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4728 dump_end (raw_dump_id, stream);
4732 static location_t locus_at_end_of_parsing;
4734 /* Check the deallocation functions for CODE to see if we want to warn that
4735 only one was defined. */
4737 static void
4738 maybe_warn_sized_delete (enum tree_code code)
4740 tree sized = NULL_TREE;
4741 tree unsized = NULL_TREE;
4743 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4744 iter; ++iter)
4746 tree fn = *iter;
4747 /* We're only interested in usual deallocation functions. */
4748 if (!usual_deallocation_fn_p (fn))
4749 continue;
4750 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4751 unsized = fn;
4752 else
4753 sized = fn;
4755 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4756 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4757 "the program should also define %qD", sized);
4758 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4759 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4760 "the program should also define %qD", unsized);
4763 /* Check the global deallocation functions to see if we want to warn about
4764 defining unsized without sized (or vice versa). */
4766 static void
4767 maybe_warn_sized_delete ()
4769 if (!flag_sized_deallocation || !warn_sized_deallocation)
4770 return;
4771 maybe_warn_sized_delete (DELETE_EXPR);
4772 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4775 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4776 look them up when evaluating non-type template parameters. Now we need to
4777 lower them to something the back end can understand. */
4779 static void
4780 lower_var_init ()
4782 varpool_node *node;
4783 FOR_EACH_VARIABLE (node)
4785 tree d = node->decl;
4786 if (tree init = DECL_INITIAL (d))
4787 DECL_INITIAL (d) = cplus_expand_constant (init);
4791 /* This routine is called at the end of compilation.
4792 Its job is to create all the code needed to initialize and
4793 destroy the global aggregates. We do the destruction
4794 first, since that way we only need to reverse the decls once. */
4796 void
4797 c_parse_final_cleanups (void)
4799 tree vars;
4800 bool reconsider;
4801 size_t i;
4802 unsigned ssdf_count = 0;
4803 int retries = 0;
4804 tree decl;
4806 locus_at_end_of_parsing = input_location;
4807 at_eof = 1;
4809 /* Bad parse errors. Just forget about it. */
4810 if (! global_bindings_p () || current_class_type
4811 || !vec_safe_is_empty (decl_namespace_list))
4812 return;
4814 /* This is the point to write out a PCH if we're doing that.
4815 In that case we do not want to do anything else. */
4816 if (pch_file)
4818 /* Mangle all symbols at PCH creation time. */
4819 symtab_node *node;
4820 FOR_EACH_SYMBOL (node)
4821 if (! is_a <varpool_node *> (node)
4822 || ! DECL_HARD_REGISTER (node->decl))
4823 DECL_ASSEMBLER_NAME (node->decl);
4824 c_common_write_pch ();
4825 dump_tu ();
4826 /* Ensure even the callers don't try to finalize the CU. */
4827 flag_syntax_only = 1;
4828 return;
4831 timevar_stop (TV_PHASE_PARSING);
4832 timevar_start (TV_PHASE_DEFERRED);
4834 symtab->process_same_body_aliases ();
4836 /* Handle -fdump-ada-spec[-slim] */
4837 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4839 collect_source_ref (main_input_filename);
4840 if (!flag_dump_ada_spec_slim)
4841 collect_source_refs (global_namespace);
4843 dump_ada_specs (collect_all_refs, cpp_check);
4846 /* FIXME - huh? was input_line -= 1;*/
4848 /* We now have to write out all the stuff we put off writing out.
4849 These include:
4851 o Template specializations that we have not yet instantiated,
4852 but which are needed.
4853 o Initialization and destruction for non-local objects with
4854 static storage duration. (Local objects with static storage
4855 duration are initialized when their scope is first entered,
4856 and are cleaned up via atexit.)
4857 o Virtual function tables.
4859 All of these may cause others to be needed. For example,
4860 instantiating one function may cause another to be needed, and
4861 generating the initializer for an object may cause templates to be
4862 instantiated, etc., etc. */
4864 emit_support_tinfos ();
4868 tree t;
4869 tree decl;
4871 reconsider = false;
4873 /* If there are templates that we've put off instantiating, do
4874 them now. */
4875 instantiate_pending_templates (retries);
4876 ggc_collect ();
4878 /* Write out virtual tables as required. Writing out the
4879 virtual table for a template class may cause the
4880 instantiation of members of that class. If we write out
4881 vtables then we remove the class from our list so we don't
4882 have to look at it again. */
4883 for (i = keyed_classes->length ();
4884 keyed_classes->iterate (--i, &t);)
4885 if (maybe_emit_vtables (t))
4887 reconsider = true;
4888 keyed_classes->unordered_remove (i);
4890 /* The input_location may have been changed during marking of
4891 vtable entries. */
4892 input_location = locus_at_end_of_parsing;
4894 /* Write out needed type info variables. We have to be careful
4895 looping through unemitted decls, because emit_tinfo_decl may
4896 cause other variables to be needed. New elements will be
4897 appended, and we remove from the vector those that actually
4898 get emitted. */
4899 for (i = unemitted_tinfo_decls->length ();
4900 unemitted_tinfo_decls->iterate (--i, &t);)
4901 if (emit_tinfo_decl (t))
4903 reconsider = true;
4904 unemitted_tinfo_decls->unordered_remove (i);
4907 /* The list of objects with static storage duration is built up
4908 in reverse order. We clear STATIC_AGGREGATES so that any new
4909 aggregates added during the initialization of these will be
4910 initialized in the correct order when we next come around the
4911 loop. */
4912 vars = prune_vars_needing_no_initialization (&static_aggregates);
4914 if (vars)
4916 /* We need to start a new initialization function each time
4917 through the loop. That's because we need to know which
4918 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4919 isn't computed until a function is finished, and written
4920 out. That's a deficiency in the back end. When this is
4921 fixed, these initialization functions could all become
4922 inline, with resulting performance improvements. */
4923 tree ssdf_body;
4925 /* Make sure the back end knows about all the variables. */
4926 write_out_vars (vars);
4928 /* Set the line and file, so that it is obviously not from
4929 the source file. */
4930 input_location = locus_at_end_of_parsing;
4931 ssdf_body = start_static_storage_duration_function (ssdf_count);
4933 /* First generate code to do all the initializations. */
4934 if (vars)
4935 do_static_initialization_or_destruction (vars, /*initp=*/true);
4937 /* Then, generate code to do all the destructions. Do these
4938 in reverse order so that the most recently constructed
4939 variable is the first destroyed. If we're using
4940 __cxa_atexit, then we don't need to do this; functions
4941 were registered at initialization time to destroy the
4942 local statics. */
4943 if (!flag_use_cxa_atexit && vars)
4945 vars = nreverse (vars);
4946 do_static_initialization_or_destruction (vars, /*initp=*/false);
4948 else
4949 vars = NULL_TREE;
4951 /* Finish up the static storage duration function for this
4952 round. */
4953 input_location = locus_at_end_of_parsing;
4954 finish_static_storage_duration_function (ssdf_body);
4956 /* All those initializations and finalizations might cause
4957 us to need more inline functions, more template
4958 instantiations, etc. */
4959 reconsider = true;
4960 ssdf_count++;
4961 /* ??? was: locus_at_end_of_parsing.line++; */
4964 /* Now do the same for thread_local variables. */
4965 handle_tls_init ();
4967 /* Go through the set of inline functions whose bodies have not
4968 been emitted yet. If out-of-line copies of these functions
4969 are required, emit them. */
4970 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4972 /* Does it need synthesizing? */
4973 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4974 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4976 /* Even though we're already at the top-level, we push
4977 there again. That way, when we pop back a few lines
4978 hence, all of our state is restored. Otherwise,
4979 finish_function doesn't clean things up, and we end
4980 up with CURRENT_FUNCTION_DECL set. */
4981 push_to_top_level ();
4982 /* The decl's location will mark where it was first
4983 needed. Save that so synthesize method can indicate
4984 where it was needed from, in case of error */
4985 input_location = DECL_SOURCE_LOCATION (decl);
4986 synthesize_method (decl);
4987 pop_from_top_level ();
4988 reconsider = true;
4991 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4992 generate_tls_wrapper (decl);
4994 if (!DECL_SAVED_TREE (decl))
4995 continue;
4997 cgraph_node *node = cgraph_node::get_create (decl);
4999 /* We lie to the back end, pretending that some functions
5000 are not defined when they really are. This keeps these
5001 functions from being put out unnecessarily. But, we must
5002 stop lying when the functions are referenced, or if they
5003 are not comdat since they need to be put out now. If
5004 DECL_INTERFACE_KNOWN, then we have already set
5005 DECL_EXTERNAL appropriately, so there's no need to check
5006 again, and we do not want to clear DECL_EXTERNAL if a
5007 previous call to import_export_decl set it.
5009 This is done in a separate for cycle, because if some
5010 deferred function is contained in another deferred
5011 function later in deferred_fns varray,
5012 rest_of_compilation would skip this function and we
5013 really cannot expand the same function twice. */
5014 import_export_decl (decl);
5015 if (DECL_NOT_REALLY_EXTERN (decl)
5016 && DECL_INITIAL (decl)
5017 && decl_needed_p (decl))
5019 if (node->cpp_implicit_alias)
5020 node = node->get_alias_target ();
5022 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
5023 NULL, true);
5024 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
5025 group, we need to mark all symbols in the same comdat group
5026 that way. */
5027 if (node->same_comdat_group)
5028 for (cgraph_node *next
5029 = dyn_cast<cgraph_node *> (node->same_comdat_group);
5030 next != node;
5031 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
5032 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
5033 NULL, true);
5036 /* If we're going to need to write this function out, and
5037 there's already a body for it, create RTL for it now.
5038 (There might be no body if this is a method we haven't
5039 gotten around to synthesizing yet.) */
5040 if (!DECL_EXTERNAL (decl)
5041 && decl_needed_p (decl)
5042 && !TREE_ASM_WRITTEN (decl)
5043 && !node->definition)
5045 /* We will output the function; no longer consider it in this
5046 loop. */
5047 DECL_DEFER_OUTPUT (decl) = 0;
5048 /* Generate RTL for this function now that we know we
5049 need it. */
5050 expand_or_defer_fn (decl);
5051 reconsider = true;
5055 if (wrapup_namespace_globals ())
5056 reconsider = true;
5058 /* Static data members are just like namespace-scope globals. */
5059 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
5061 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
5062 /* Don't write it out if we haven't seen a definition. */
5063 || DECL_IN_AGGR_P (decl))
5064 continue;
5065 import_export_decl (decl);
5066 /* If this static data member is needed, provide it to the
5067 back end. */
5068 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
5069 DECL_EXTERNAL (decl) = 0;
5071 if (vec_safe_length (pending_statics) != 0
5072 && wrapup_global_declarations (pending_statics->address (),
5073 pending_statics->length ()))
5074 reconsider = true;
5076 retries++;
5078 while (reconsider);
5080 lower_var_init ();
5082 generate_mangling_aliases ();
5084 /* All used inline functions must have a definition at this point. */
5085 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5087 if (/* Check online inline functions that were actually used. */
5088 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
5089 /* If the definition actually was available here, then the
5090 fact that the function was not defined merely represents
5091 that for some reason (use of a template repository,
5092 #pragma interface, etc.) we decided not to emit the
5093 definition here. */
5094 && !DECL_INITIAL (decl)
5095 /* Don't complain if the template was defined. */
5096 && !(DECL_TEMPLATE_INSTANTIATION (decl)
5097 && DECL_INITIAL (DECL_TEMPLATE_RESULT
5098 (template_for_substitution (decl))))
5099 && warning_at (DECL_SOURCE_LOCATION (decl), 0,
5100 "inline function %qD used but never defined", decl))
5101 /* Avoid a duplicate warning from check_global_declaration. */
5102 TREE_NO_WARNING (decl) = 1;
5105 /* So must decls that use a type with no linkage. */
5106 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
5107 no_linkage_error (decl);
5109 maybe_warn_sized_delete ();
5111 /* Then, do the Objective-C stuff. This is where all the
5112 Objective-C module stuff gets generated (symtab,
5113 class/protocol/selector lists etc). This must be done after C++
5114 templates, destructors etc. so that selectors used in C++
5115 templates are properly allocated. */
5116 if (c_dialect_objc ())
5117 objc_write_global_declarations ();
5119 /* We give C linkage to static constructors and destructors. */
5120 push_lang_context (lang_name_c);
5122 /* Generate initialization and destruction functions for all
5123 priorities for which they are required. */
5124 if (priority_info_map)
5125 splay_tree_foreach (priority_info_map,
5126 generate_ctor_and_dtor_functions_for_priority,
5127 /*data=*/&locus_at_end_of_parsing);
5128 else if (c_dialect_objc () && objc_static_init_needed_p ())
5129 /* If this is obj-c++ and we need a static init, call
5130 generate_ctor_or_dtor_function. */
5131 generate_ctor_or_dtor_function (/*constructor_p=*/true,
5132 DEFAULT_INIT_PRIORITY,
5133 &locus_at_end_of_parsing);
5135 /* We're done with the splay-tree now. */
5136 if (priority_info_map)
5137 splay_tree_delete (priority_info_map);
5139 /* Generate any missing aliases. */
5140 maybe_apply_pending_pragma_weaks ();
5142 /* We're done with static constructors, so we can go back to "C++"
5143 linkage now. */
5144 pop_lang_context ();
5146 if (flag_vtable_verify)
5148 vtv_recover_class_info ();
5149 vtv_compute_class_hierarchy_transitive_closure ();
5150 vtv_build_vtable_verify_fndecl ();
5153 perform_deferred_noexcept_checks ();
5155 fini_constexpr ();
5157 /* The entire file is now complete. If requested, dump everything
5158 to a file. */
5159 dump_tu ();
5161 if (flag_detailed_statistics)
5163 dump_tree_statistics ();
5164 dump_time_statistics ();
5167 timevar_stop (TV_PHASE_DEFERRED);
5168 timevar_start (TV_PHASE_PARSING);
5170 /* Indicate that we're done with front end processing. */
5171 at_eof = 2;
5174 /* Perform any post compilation-proper cleanups for the C++ front-end.
5175 This should really go away. No front-end should need to do
5176 anything past the compilation process. */
5178 void
5179 cxx_post_compilation_parsing_cleanups (void)
5181 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5183 if (flag_vtable_verify)
5185 /* Generate the special constructor initialization function that
5186 calls __VLTRegisterPairs, and give it a very high
5187 initialization priority. This must be done after
5188 finalize_compilation_unit so that we have accurate
5189 information about which vtable will actually be emitted. */
5190 vtv_generate_init_routine ();
5193 input_location = locus_at_end_of_parsing;
5195 if (flag_checking)
5196 validate_conversion_obstack ();
5198 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5201 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5202 function to call in parse-tree form; it has not yet been
5203 semantically analyzed. ARGS are the arguments to the function.
5204 They have already been semantically analyzed. This may change
5205 ARGS. */
5207 tree
5208 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5209 tsubst_flags_t complain)
5211 tree orig_fn;
5212 vec<tree, va_gc> *orig_args = NULL;
5213 tree expr;
5214 tree object;
5216 orig_fn = fn;
5217 object = TREE_OPERAND (fn, 0);
5219 if (processing_template_decl)
5221 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5222 || TREE_CODE (fn) == MEMBER_REF);
5223 if (type_dependent_expression_p (fn)
5224 || any_type_dependent_arguments_p (*args))
5225 return build_min_nt_call_vec (fn, *args);
5227 orig_args = make_tree_vector_copy (*args);
5229 /* Transform the arguments and add the implicit "this"
5230 parameter. That must be done before the FN is transformed
5231 because we depend on the form of FN. */
5232 make_args_non_dependent (*args);
5233 object = build_non_dependent_expr (object);
5234 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5236 if (TREE_CODE (fn) == DOTSTAR_EXPR)
5237 object = cp_build_addr_expr (object, complain);
5238 vec_safe_insert (*args, 0, object);
5240 /* Now that the arguments are done, transform FN. */
5241 fn = build_non_dependent_expr (fn);
5244 /* A qualified name corresponding to a bound pointer-to-member is
5245 represented as an OFFSET_REF:
5247 struct B { void g(); };
5248 void (B::*p)();
5249 void B::g() { (this->*p)(); } */
5250 if (TREE_CODE (fn) == OFFSET_REF)
5252 tree object_addr = cp_build_addr_expr (object, complain);
5253 fn = TREE_OPERAND (fn, 1);
5254 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5255 complain);
5256 vec_safe_insert (*args, 0, object_addr);
5259 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5260 expr = build_op_call (fn, args, complain);
5261 else
5262 expr = cp_build_function_call_vec (fn, args, complain);
5263 if (processing_template_decl && expr != error_mark_node)
5264 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5266 if (orig_args != NULL)
5267 release_tree_vector (orig_args);
5269 return expr;
5273 void
5274 check_default_args (tree x)
5276 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5277 bool saw_def = false;
5278 bool noted_first_def = false;
5279 int idx_of_first_default_arg = 0;
5280 location_t loc_of_first_default_arg = UNKNOWN_LOCATION;
5281 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5282 tree fndecl = STRIP_TEMPLATE (x);
5283 auto_diagnostic_group d;
5284 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5286 if (TREE_PURPOSE (arg))
5288 if (!saw_def)
5290 saw_def = true;
5291 idx_of_first_default_arg = i;
5292 location_t loc = get_fndecl_argument_location (fndecl, i);
5293 if (loc != DECL_SOURCE_LOCATION (x))
5294 loc_of_first_default_arg = loc;
5297 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5299 error_at (get_fndecl_argument_location (fndecl, i),
5300 "default argument missing for parameter %P of %q#D", i, x);
5301 if (loc_of_first_default_arg != UNKNOWN_LOCATION
5302 && !noted_first_def)
5304 inform (loc_of_first_default_arg,
5305 "...following parameter %P which has a default argument",
5306 idx_of_first_default_arg);
5307 noted_first_def = true;
5309 TREE_PURPOSE (arg) = error_mark_node;
5314 /* Return true if function DECL can be inlined. This is used to force
5315 instantiation of methods that might be interesting for inlining. */
5316 bool
5317 possibly_inlined_p (tree decl)
5319 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5320 if (DECL_UNINLINABLE (decl))
5321 return false;
5322 if (!optimize)
5323 return DECL_DECLARED_INLINE_P (decl);
5324 /* When optimizing, we might inline everything when flatten
5325 attribute or heuristics inlining for size or autoinlining
5326 is used. */
5327 return true;
5330 /* Normally, we can wait until instantiation-time to synthesize DECL.
5331 However, if DECL is a static data member initialized with a constant
5332 or a constexpr function, we need it right now because a reference to
5333 such a data member or a call to such function is not value-dependent.
5334 For a function that uses auto in the return type, we need to instantiate
5335 it to find out its type. For OpenMP user defined reductions, we need
5336 them instantiated for reduction clauses which inline them by hand
5337 directly. */
5339 static void
5340 maybe_instantiate_decl (tree decl)
5342 if (DECL_LANG_SPECIFIC (decl)
5343 && DECL_TEMPLATE_INFO (decl)
5344 && (decl_maybe_constant_var_p (decl)
5345 || (TREE_CODE (decl) == FUNCTION_DECL
5346 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5347 || undeduced_auto_decl (decl))
5348 && !DECL_DECLARED_CONCEPT_P (decl)
5349 && !uses_template_parms (DECL_TI_ARGS (decl)))
5351 /* Instantiating a function will result in garbage collection. We
5352 must treat this situation as if we were within the body of a
5353 function so as to avoid collecting live data only referenced from
5354 the stack (such as overload resolution candidates). */
5355 ++function_depth;
5356 instantiate_decl (decl, /*defer_ok=*/false,
5357 /*expl_inst_class_mem_p=*/false);
5358 --function_depth;
5362 /* Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns whether or
5363 not a warning was emitted. */
5365 bool
5366 cp_warn_deprecated_use (tree decl, tsubst_flags_t complain)
5368 if (!(complain & tf_warning) || !decl
5369 || deprecated_state == DEPRECATED_SUPPRESS)
5370 return false;
5372 if (!TREE_DEPRECATED (decl))
5374 /* Perhaps this is a deprecated typedef. */
5375 if (TYPE_P (decl) && TYPE_NAME (decl))
5376 decl = TYPE_NAME (decl);
5378 if (!TREE_DEPRECATED (decl))
5379 return false;
5382 /* Don't warn within members of a deprecated type. */
5383 if (TYPE_P (decl)
5384 && currently_open_class (decl))
5385 return false;
5387 bool warned = false;
5388 if (cxx_dialect >= cxx11
5389 && DECL_P (decl)
5390 && DECL_ARTIFICIAL (decl)
5391 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5392 && copy_fn_p (decl))
5394 if (warn_deprecated_copy
5395 /* Don't warn about system library classes (c++/86342). */
5396 && (!DECL_IN_SYSTEM_HEADER (decl)
5397 || global_dc->dc_warn_system_headers))
5399 auto_diagnostic_group d;
5400 tree ctx = DECL_CONTEXT (decl);
5401 tree other = classtype_has_depr_implicit_copy (ctx);
5402 int opt = (DECL_DESTRUCTOR_P (other)
5403 ? OPT_Wdeprecated_copy_dtor
5404 : OPT_Wdeprecated_copy);
5405 warned = warning (opt, "implicitly-declared %qD is deprecated",
5406 decl);
5407 if (warned)
5408 inform (DECL_SOURCE_LOCATION (other),
5409 "because %qT has user-provided %qD",
5410 ctx, other);
5413 else
5414 warned = warn_deprecated_use (decl, NULL_TREE);
5416 return warned;
5419 /* Like above, but takes into account outer scopes. */
5421 void
5422 cp_warn_deprecated_use_scopes (tree scope)
5424 while (scope
5425 && scope != error_mark_node
5426 && scope != global_namespace)
5428 if (cp_warn_deprecated_use (scope))
5429 return;
5430 if (TYPE_P (scope))
5431 scope = CP_TYPE_CONTEXT (scope);
5432 else
5433 scope = CP_DECL_CONTEXT (scope);
5437 /* True if DECL or its enclosing scope have unbound template parameters. */
5439 bool
5440 decl_dependent_p (tree decl)
5442 if (DECL_FUNCTION_SCOPE_P (decl)
5443 || TREE_CODE (decl) == CONST_DECL
5444 || TREE_CODE (decl) == USING_DECL
5445 || TREE_CODE (decl) == FIELD_DECL)
5446 decl = CP_DECL_CONTEXT (decl);
5447 if (tree tinfo = get_template_info (decl))
5448 if (any_dependent_template_arguments_p (TI_ARGS (tinfo)))
5449 return true;
5450 if (LAMBDA_FUNCTION_P (decl)
5451 && dependent_type_p (DECL_CONTEXT (decl)))
5452 return true;
5453 return false;
5456 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5457 If DECL is a specialization or implicitly declared class member,
5458 generate the actual definition. Return false if something goes
5459 wrong, true otherwise. */
5461 bool
5462 mark_used (tree decl, tsubst_flags_t complain)
5464 /* If we're just testing conversions or resolving overloads, we
5465 don't want any permanent effects like forcing functions to be
5466 output or instantiating templates. */
5467 if ((complain & tf_conv))
5468 return true;
5470 /* If DECL is a BASELINK for a single function, then treat it just
5471 like the DECL for the function. Otherwise, if the BASELINK is
5472 for an overloaded function, we don't know which function was
5473 actually used until after overload resolution. */
5474 if (BASELINK_P (decl))
5476 decl = BASELINK_FUNCTIONS (decl);
5477 if (really_overloaded_fn (decl))
5478 return true;
5479 decl = OVL_FIRST (decl);
5482 if (!DECL_P (decl))
5483 return true;
5485 /* Set TREE_USED for the benefit of -Wunused. */
5486 TREE_USED (decl) = 1;
5487 /* And for structured bindings also the underlying decl. */
5488 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5489 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5491 if (TREE_CODE (decl) == TEMPLATE_DECL)
5492 return true;
5494 if (DECL_CLONED_FUNCTION_P (decl))
5495 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5497 /* Mark enumeration types as used. */
5498 if (TREE_CODE (decl) == CONST_DECL)
5499 used_types_insert (DECL_CONTEXT (decl));
5501 if (TREE_CODE (decl) == FUNCTION_DECL
5502 && DECL_MAYBE_DELETED (decl))
5504 /* ??? Switch other defaulted functions to use DECL_MAYBE_DELETED? */
5505 gcc_assert (special_function_p (decl) == sfk_comparison);
5507 ++function_depth;
5508 synthesize_method (decl);
5509 --function_depth;
5512 if (TREE_CODE (decl) == FUNCTION_DECL
5513 && !maybe_instantiate_noexcept (decl, complain))
5514 return false;
5516 if (TREE_CODE (decl) == FUNCTION_DECL
5517 && DECL_DELETED_FN (decl))
5519 if (DECL_ARTIFICIAL (decl)
5520 && DECL_CONV_FN_P (decl)
5521 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5522 /* We mark a lambda conversion op as deleted if we can't
5523 generate it properly; see maybe_add_lambda_conv_op. */
5524 sorry ("converting lambda that uses %<...%> to function pointer");
5525 else if (complain & tf_error)
5527 error ("use of deleted function %qD", decl);
5528 if (!maybe_explain_implicit_delete (decl))
5529 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5531 return false;
5534 cp_warn_deprecated_use (decl, complain);
5536 /* We can only check DECL_ODR_USED on variables or functions with
5537 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5538 might need special handling for. */
5539 if (!VAR_OR_FUNCTION_DECL_P (decl)
5540 || DECL_LANG_SPECIFIC (decl) == NULL
5541 || DECL_THUNK_P (decl))
5543 if (!decl_dependent_p (decl)
5544 && !require_deduced_type (decl, complain))
5545 return false;
5546 return true;
5549 /* We only want to do this processing once. We don't need to keep trying
5550 to instantiate inline templates, because unit-at-a-time will make sure
5551 we get them compiled before functions that want to inline them. */
5552 if (DECL_ODR_USED (decl))
5553 return true;
5555 /* Normally, we can wait until instantiation-time to synthesize DECL.
5556 However, if DECL is a static data member initialized with a constant
5557 or a constexpr function, we need it right now because a reference to
5558 such a data member or a call to such function is not value-dependent.
5559 For a function that uses auto in the return type, we need to instantiate
5560 it to find out its type. For OpenMP user defined reductions, we need
5561 them instantiated for reduction clauses which inline them by hand
5562 directly. */
5563 maybe_instantiate_decl (decl);
5565 if (flag_concepts && TREE_CODE (decl) == FUNCTION_DECL
5566 && !constraints_satisfied_p (decl))
5568 if (complain & tf_error)
5570 auto_diagnostic_group d;
5571 error ("use of function %qD with unsatisfied constraints",
5572 decl);
5573 location_t loc = DECL_SOURCE_LOCATION (decl);
5574 inform (loc, "declared here");
5575 diagnose_constraints (loc, decl, NULL_TREE);
5577 return false;
5580 if (processing_template_decl || in_template_function ())
5581 return true;
5583 /* Check this too in case we're within instantiate_non_dependent_expr. */
5584 if (DECL_TEMPLATE_INFO (decl)
5585 && uses_template_parms (DECL_TI_ARGS (decl)))
5586 return true;
5588 if (!require_deduced_type (decl, complain))
5589 return false;
5591 if (builtin_pack_fn_p (decl))
5593 error ("use of built-in parameter pack %qD outside of a template",
5594 DECL_NAME (decl));
5595 return false;
5598 /* If we don't need a value, then we don't need to synthesize DECL. */
5599 if (cp_unevaluated_operand || in_discarded_stmt)
5600 return true;
5602 DECL_ODR_USED (decl) = 1;
5603 if (DECL_CLONED_FUNCTION_P (decl))
5604 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5606 /* DR 757: A type without linkage shall not be used as the type of a
5607 variable or function with linkage, unless
5608 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5609 o the variable or function is not used (3.2 [basic.def.odr]) or is
5610 defined in the same translation unit. */
5611 if (cxx_dialect > cxx98
5612 && decl_linkage (decl) != lk_none
5613 && !DECL_EXTERN_C_P (decl)
5614 && !DECL_ARTIFICIAL (decl)
5615 && !decl_defined_p (decl)
5616 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5618 if (is_local_extern (decl))
5619 /* There's no way to define a local extern, and adding it to
5620 the vector interferes with GC, so give an error now. */
5621 no_linkage_error (decl);
5622 else
5623 vec_safe_push (no_linkage_decls, decl);
5626 if (TREE_CODE (decl) == FUNCTION_DECL
5627 && DECL_DECLARED_INLINE_P (decl)
5628 && !DECL_INITIAL (decl)
5629 && !DECL_ARTIFICIAL (decl)
5630 && !DECL_PURE_VIRTUAL_P (decl))
5631 /* Remember it, so we can check it was defined. */
5632 note_vague_linkage_fn (decl);
5634 /* Is it a synthesized method that needs to be synthesized? */
5635 if (TREE_CODE (decl) == FUNCTION_DECL
5636 && DECL_DEFAULTED_FN (decl)
5637 /* A function defaulted outside the class is synthesized either by
5638 cp_finish_decl or instantiate_decl. */
5639 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5640 && ! DECL_INITIAL (decl))
5642 /* Defer virtual destructors so that thunks get the right
5643 linkage. */
5644 if (DECL_VIRTUAL_P (decl) && !at_eof)
5646 note_vague_linkage_fn (decl);
5647 return true;
5650 /* Remember the current location for a function we will end up
5651 synthesizing. Then we can inform the user where it was
5652 required in the case of error. */
5653 if (DECL_ARTIFICIAL (decl))
5654 DECL_SOURCE_LOCATION (decl) = input_location;
5656 /* Synthesizing an implicitly defined member function will result in
5657 garbage collection. We must treat this situation as if we were
5658 within the body of a function so as to avoid collecting live data
5659 on the stack (such as overload resolution candidates).
5661 We could just let c_parse_final_cleanups handle synthesizing
5662 this function by adding it to deferred_fns, but doing
5663 it at the use site produces better error messages. */
5664 ++function_depth;
5665 synthesize_method (decl);
5666 --function_depth;
5667 /* If this is a synthesized method we don't need to
5668 do the instantiation test below. */
5670 else if (VAR_OR_FUNCTION_DECL_P (decl)
5671 && DECL_TEMPLATE_INFO (decl)
5672 && !DECL_DECLARED_CONCEPT_P (decl)
5673 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5674 || always_instantiate_p (decl)))
5675 /* If this is a function or variable that is an instance of some
5676 template, we now know that we will need to actually do the
5677 instantiation. We check that DECL is not an explicit
5678 instantiation because that is not checked in instantiate_decl.
5680 We put off instantiating functions in order to improve compile
5681 times. Maintaining a stack of active functions is expensive,
5682 and the inliner knows to instantiate any functions it might
5683 need. Therefore, we always try to defer instantiation. */
5685 ++function_depth;
5686 instantiate_decl (decl, /*defer_ok=*/true,
5687 /*expl_inst_class_mem_p=*/false);
5688 --function_depth;
5691 return true;
5694 bool
5695 mark_used (tree decl)
5697 return mark_used (decl, tf_warning_or_error);
5700 tree
5701 vtv_start_verification_constructor_init_function (void)
5703 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5706 tree
5707 vtv_finish_verification_constructor_init_function (tree function_body)
5709 tree fn;
5711 finish_compound_stmt (function_body);
5712 fn = finish_function (/*inline_p=*/false);
5713 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5714 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5716 return fn;
5719 #include "gt-cp-decl2.h"