Debian Codegraph @

gcc/cp/decl2.cc

raw
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2025 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann ([email protected])
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21
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. */
25
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "memmodel.h"
33 #include "target.h"
34 #include "cp-tree.h"
35 #include "c-family/c-common.h"
36 #include "timevar.h"
37 #include "stringpool.h"
38 #include "cgraph.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "stor-layout.h"
42 #include "calls.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "dumpfile.h"
48 #include "intl.h"
49 #include "c-family/c-ada-spec.h"
50 #include "asan.h"
51 #include "optabs-query.h"
52 #include "omp-general.h"
53 #include "tree-inline.h"
54 #include "escaped_string.h"
55
56 /* Id for dumping the raw trees. */
57 int raw_dump_id;
58
59 extern cpp_reader *parse_in;
60
61 static tree start_objects (bool, unsigned, bool, bool);
62 static tree finish_objects (bool, unsigned, tree, bool = true);
63 static tree start_partial_init_fini_fn (bool, unsigned, unsigned, bool);
64 static void finish_partial_init_fini_fn (tree);
65 static tree emit_partial_init_fini_fn (bool, unsigned, tree,
66 unsigned, location_t, tree);
67 static void one_static_initialization_or_destruction (bool, tree, tree, bool);
68 static void generate_ctor_or_dtor_function (bool, unsigned, tree, location_t,
69 bool);
70 static tree prune_vars_needing_no_initialization (tree *);
71 static void write_out_vars (tree);
72 static void import_export_class (tree);
73 static tree get_guard_bits (tree);
74 static void determine_visibility_from_class (tree, tree);
75 static bool determine_hidden_inline (tree);
76
77 /* A list of static class variables. This is needed, because a
78 static class variable can be declared inside the class without
79 an initializer, and then initialized, statically, outside the class. */
80 static GTY(()) vec<tree, va_gc> *pending_statics;
81
82 /* A list of functions which were declared inline, but which we
83 may need to emit outline anyway. */
84 static GTY(()) vec<tree, va_gc> *deferred_fns;
85
86 /* A list of decls that use types with no linkage, which we need to make
87 sure are defined. */
88 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
89
90 /* A vector of alternating decls and identifiers, where the latter
91 is to be an alias for the former if the former is defined. */
92 static GTY(()) vec<tree, va_gc> *mangling_aliases;
93
94 /* hash traits for declarations. Hashes single decls via
95 DECL_ASSEMBLER_NAME_RAW. */
96
97 struct mangled_decl_hash : ggc_remove <tree>
98 {
99 typedef tree value_type; /* A DECL. */
100 typedef tree compare_type; /* An identifier. */
101
102 static hashval_t hash (const value_type decl)
103 {
104 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
105 }
106 static bool equal (const value_type existing, compare_type candidate)
107 {
108 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
109 return candidate == name;
110 }
111
112 static const bool empty_zero_p = true;
113 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
114 static inline bool is_empty (value_type p) {return !p;}
115
116 static bool is_deleted (value_type e)
117 {
118 return e == reinterpret_cast <value_type> (1);
119 }
120 static void mark_deleted (value_type &e)
121 {
122 e = reinterpret_cast <value_type> (1);
123 }
124 };
125
126 /* A hash table of decls keyed by mangled name. Used to figure out if
127 we need compatibility aliases. */
128 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
129
130 // Hash table mapping priority to lists of variables or functions.
131 struct priority_map_traits
132 {
133 typedef unsigned key_type;
134 typedef tree value_type;
135 static const bool maybe_mx = true;
136 static hashval_t hash (key_type v)
137 {
138 return hashval_t (v);
139 }
140 static bool equal_keys (key_type k1, key_type k2)
141 {
142 return k1 == k2;
143 }
144 template <typename T> static void remove (T &)
145 {
146 }
147 // Zero is not a priority
148 static const bool empty_zero_p = true;
149 template <typename T> static bool is_empty (const T &entry)
150 {
151 return entry.m_key == 0;
152 }
153 template <typename T> static void mark_empty (T &entry)
154 {
155 entry.m_key = 0;
156 }
157 // Entries are not deleteable
158 template <typename T> static bool is_deleted (const T &)
159 {
160 return false;
161 }
162 template <typename T> static void mark_deleted (T &)
163 {
164 gcc_unreachable ();
165 }
166 };
167
168 typedef hash_map<unsigned/*Priority*/, tree/*List*/,
169 priority_map_traits> priority_map_t;
170
171 /* Two pairs of such hash tables, for the host and an OpenMP offload device.
172 Each pair has one priority map for fini and one for init. The fini tables
173 are only ever used when !cxa_atexit. */
174 static GTY(()) priority_map_t *static_init_fini_fns[4];
175
176 /* Nonzero if we're done parsing and into end-of-file activities.
177 2 if all templates have been instantiated.
178 3 if we're done with front-end processing. */
179
180 int at_eof;
181
182 /* True if note_mangling_alias should enqueue mangling aliases for
183 later generation, rather than emitting them right away. */
184
185 bool defer_mangling_aliases = true;
186
187
188 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
189 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
190 that apply to the function). */
191
192 tree
193 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
194 cp_ref_qualifier rqual)
195 {
196 if (fntype == error_mark_node || ctype == error_mark_node)
197 return error_mark_node;
198
199 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
200
201 cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
202 ctype = cp_build_qualified_type (ctype, type_quals);
203
204 tree newtype
205 = build_method_type_directly (ctype, TREE_TYPE (fntype),
206 (TREE_CODE (fntype) == METHOD_TYPE
207 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
208 : TYPE_ARG_TYPES (fntype)));
209 if (tree attrs = TYPE_ATTRIBUTES (fntype))
210 newtype = cp_build_type_attribute_variant (newtype, attrs);
211 newtype = build_cp_fntype_variant (newtype, rqual,
212 TYPE_RAISES_EXCEPTIONS (fntype),
213 TYPE_HAS_LATE_RETURN_TYPE (fntype));
214
215 return newtype;
216 }
217
218 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
219 return type changed to NEW_RET. */
220
221 tree
222 change_return_type (tree new_ret, tree fntype)
223 {
224 if (new_ret == error_mark_node)
225 return fntype;
226
227 if (same_type_p (new_ret, TREE_TYPE (fntype)))
228 return fntype;
229
230 tree newtype;
231 tree args = TYPE_ARG_TYPES (fntype);
232
233 if (TREE_CODE (fntype) == FUNCTION_TYPE)
234 {
235 newtype = build_function_type (new_ret, args);
236 newtype = apply_memfn_quals (newtype,
237 type_memfn_quals (fntype));
238 }
239 else
240 newtype = build_method_type_directly
241 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
242
243 if (tree attrs = TYPE_ATTRIBUTES (fntype))
244 newtype = cp_build_type_attribute_variant (newtype, attrs);
245 newtype = cxx_copy_lang_qualifiers (newtype, fntype);
246
247 return newtype;
248 }
249
250 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
251 appropriately. */
252
253 tree
254 cp_build_parm_decl (tree fn, tree name, tree type)
255 {
256 tree parm = build_decl (input_location,
257 PARM_DECL, name, type);
258 DECL_CONTEXT (parm) = fn;
259
260 /* DECL_ARG_TYPE is only used by the back end and the back end never
261 sees templates. */
262 if (!processing_template_decl)
263 DECL_ARG_TYPE (parm) = type_passed_as (type);
264
265 return parm;
266 }
267
268 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
269 indicated NAME. */
270
271 tree
272 build_artificial_parm (tree fn, tree name, tree type)
273 {
274 tree parm = cp_build_parm_decl (fn, name, type);
275 DECL_ARTIFICIAL (parm) = 1;
276 /* All our artificial parms are implicitly `const'; they cannot be
277 assigned to. */
278 TREE_READONLY (parm) = 1;
279 return parm;
280 }
281
282 /* 'structors for types with virtual baseclasses need an "in-charge" flag
283 saying whether this function is responsible for virtual baseclasses or not.
284
285 This function adds the "in-charge" flag to member function FN if
286 appropriate. It is called from grokclassfn and tsubst.
287 FN must be either a constructor or destructor.
288
289 The in-charge flag follows the 'this' parameter, and is followed by the
290 VTT parm (if any), then the user-written parms. */
291
292 void
293 maybe_retrofit_in_chrg (tree fn)
294 {
295 tree basetype, arg_types, parms, parm, fntype;
296
297 /* If we've already add the in-charge parameter don't do it again. */
298 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
299 return;
300
301 /* When processing templates we can't know, in general, whether or
302 not we're going to have virtual baseclasses. */
303 if (processing_template_decl)
304 return;
305
306 /* We don't need an in-charge parameter for 'structors that don't
307 have virtual bases. */
308 if (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
309 return;
310
311 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
312 basetype = TREE_TYPE (TREE_VALUE (arg_types));
313 arg_types = TREE_CHAIN (arg_types);
314
315 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
316
317 /* If this is a subobject constructor or destructor, our caller will
318 pass us a pointer to our VTT. */
319 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
320 {
321 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
322
323 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
324 DECL_CHAIN (parm) = parms;
325 parms = parm;
326
327 /* ...and then to TYPE_ARG_TYPES. */
328 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
329
330 DECL_HAS_VTT_PARM_P (fn) = 1;
331 }
332
333 /* Then add the in-charge parm (before the VTT parm). */
334 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
335 DECL_CHAIN (parm) = parms;
336 parms = parm;
337 arg_types = hash_tree_chain (integer_type_node, arg_types);
338
339 /* Insert our new parameter(s) into the list. */
340 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
341
342 /* And rebuild the function type. */
343 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
344 arg_types);
345 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
346 fntype = (cp_build_type_attribute_variant
347 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
348 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
349 TREE_TYPE (fn) = fntype;
350
351 /* Now we've got the in-charge parameter. */
352 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
353 }
354
355 /* Classes overload their constituent function names automatically.
356 When a function name is declared in a record structure,
357 its name is changed to it overloaded name. Since names for
358 constructors and destructors can conflict, we place a leading
359 '$' for destructors.
360
361 CNAME is the name of the class we are grokking for.
362
363 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
364
365 FLAGS contains bits saying what's special about today's
366 arguments. DTOR_FLAG == DESTRUCTOR.
367
368 If FUNCTION is a destructor, then we must add the `auto-delete' field
369 as a second parameter. There is some hair associated with the fact
370 that we must "declare" this variable in the manner consistent with the
371 way the rest of the arguments were declared.
372
373 QUALS are the qualifiers for the this pointer. */
374
375 void
376 grokclassfn (tree ctype, tree function, enum overload_flags flags)
377 {
378 tree fn_name = DECL_NAME (function);
379
380 /* Even within an `extern "C"' block, members get C++ linkage. See
381 [dcl.link] for details. */
382 SET_DECL_LANGUAGE (function, lang_cplusplus);
383
384 if (fn_name == NULL_TREE)
385 {
386 error ("name missing for member function");
387 fn_name = get_identifier ("<anonymous>");
388 DECL_NAME (function) = fn_name;
389 }
390
391 DECL_CONTEXT (function) = ctype;
392
393 if (flags == DTOR_FLAG)
394 DECL_CXX_DESTRUCTOR_P (function) = 1;
395
396 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
397 maybe_retrofit_in_chrg (function);
398 }
399
400 /* Create an ARRAY_REF, checking for the user doing things backwards
401 along the way.
402 If INDEX_EXP is non-NULL, then that is the index expression,
403 otherwise INDEX_EXP_LIST is the list of index expressions. */
404
405 tree
406 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
407 vec<tree, va_gc> **index_exp_list, tsubst_flags_t complain)
408 {
409 tree type;
410 tree expr;
411 tree orig_array_expr = array_expr;
412 tree orig_index_exp = index_exp;
413 vec<tree, va_gc> *orig_index_exp_list
414 = index_exp_list ? *index_exp_list : NULL;
415 tree overload = NULL_TREE;
416
417 if (error_operand_p (array_expr) || error_operand_p (index_exp))
418 return error_mark_node;
419
420 if (processing_template_decl)
421 {
422 if (type_dependent_expression_p (array_expr)
423 || (index_exp ? type_dependent_expression_p (index_exp)
424 : any_type_dependent_arguments_p (*index_exp_list)))
425 {
426 if (index_exp == NULL)
427 index_exp = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
428 *index_exp_list);
429 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
430 NULL_TREE, NULL_TREE);
431 }
432 if (!index_exp)
433 orig_index_exp_list = make_tree_vector_copy (*index_exp_list);
434 }
435
436 type = TREE_TYPE (array_expr);
437 gcc_assert (type);
438 type = non_reference (type);
439
440 /* If they have an `operator[]', use that. */
441 if (MAYBE_CLASS_TYPE_P (type)
442 || (index_exp && MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
443 || (index_exp == NULL_TREE
444 && !(*index_exp_list)->is_empty ()
445 && MAYBE_CLASS_TYPE_P (TREE_TYPE ((*index_exp_list)->last ()))))
446 {
447 if (index_exp)
448 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
449 index_exp, NULL_TREE, NULL_TREE,
450 &overload, complain);
451 else if ((*index_exp_list)->is_empty ())
452 expr = build_op_subscript (loc, array_expr, index_exp_list, &overload,
453 complain);
454 else
455 {
456 expr = build_op_subscript (loc, array_expr, index_exp_list,
457 &overload, complain & tf_decltype);
458 if (expr == error_mark_node
459 /* Don't do the backward compatibility fallback in a SFINAE
460 context. */
461 && (complain & tf_error))
462 {
463 tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL,
464 tf_none);
465 if (idx != error_mark_node)
466 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
467 idx, NULL_TREE, NULL_TREE, &overload,
468 complain & tf_decltype);
469 if (expr == error_mark_node)
470 {
471 overload = NULL_TREE;
472 expr = build_op_subscript (loc, array_expr, index_exp_list,
473 &overload, complain);
474 }
475 else
476 {
477 /* If it would be valid albeit deprecated expression in
478 C++20, just pedwarn on it and treat it as if wrapped
479 in (). */
480 pedwarn (loc, OPT_Wcomma_subscript,
481 "top-level comma expression in array subscript "
482 "changed meaning in C++23");
483 if (processing_template_decl)
484 {
485 orig_index_exp
486 = build_x_compound_expr_from_vec (orig_index_exp_list,
487 NULL, complain);
488 if (orig_index_exp == error_mark_node)
489 expr = error_mark_node;
490 release_tree_vector (orig_index_exp_list);
491 }
492 }
493 }
494 }
495 }
496 else
497 {
498 tree p1, p2, i1, i2;
499 bool swapped = false;
500
501 /* Otherwise, create an ARRAY_REF for a pointer or array type.
502 It is a little-known fact that, if `a' is an array and `i' is
503 an int, you can write `i[a]', which means the same thing as
504 `a[i]'. */
505 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
506 p1 = array_expr;
507 else
508 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
509
510 if (index_exp == NULL_TREE)
511 {
512 if (!(complain & tf_error))
513 /* Don't do the backward compatibility fallback in a SFINAE
514 context. */
515 return error_mark_node;
516
517 if ((*index_exp_list)->is_empty ())
518 {
519 error_at (loc, "built-in subscript operator without expression "
520 "list");
521 return error_mark_node;
522 }
523 tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL,
524 tf_none);
525 if (idx != error_mark_node)
526 /* If it would be valid albeit deprecated expression in C++20,
527 just pedwarn on it and treat it as if wrapped in (). */
528 pedwarn (loc, OPT_Wcomma_subscript,
529 "top-level comma expression in array subscript "
530 "changed meaning in C++23");
531 else
532 {
533 error_at (loc, "built-in subscript operator with more than one "
534 "expression in expression list");
535 return error_mark_node;
536 }
537 index_exp = idx;
538 if (processing_template_decl)
539 {
540 orig_index_exp
541 = build_x_compound_expr_from_vec (orig_index_exp_list,
542 NULL, complain);
543 release_tree_vector (orig_index_exp_list);
544 if (orig_index_exp == error_mark_node)
545 return error_mark_node;
546 }
547 }
548
549 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
550 p2 = index_exp;
551 else
552 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
553
554 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
555 false);
556 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
557 false);
558
559 if ((p1 && i2) && (i1 && p2))
560 error ("ambiguous conversion for array subscript");
561
562 if (p1 && i2)
563 array_expr = p1, index_exp = i2;
564 else if (i1 && p2)
565 swapped = true, array_expr = p2, index_exp = i1;
566 else
567 {
568 if (complain & tf_error)
569 error_at (loc, "invalid types %<%T[%T]%> for array subscript",
570 type, TREE_TYPE (index_exp));
571 return error_mark_node;
572 }
573
574 if (array_expr == error_mark_node || index_exp == error_mark_node)
575 error ("ambiguous conversion for array subscript");
576
577 if (TYPE_PTR_P (TREE_TYPE (array_expr)))
578 array_expr = mark_rvalue_use (array_expr);
579 else
580 array_expr = mark_lvalue_use_nonread (array_expr);
581 index_exp = mark_rvalue_use (index_exp);
582 if (swapped
583 && flag_strong_eval_order == 2
584 && (TREE_SIDE_EFFECTS (array_expr) || TREE_SIDE_EFFECTS (index_exp)))
585 expr = build_array_ref (input_location, index_exp, array_expr);
586 else
587 expr = build_array_ref (input_location, array_expr, index_exp);
588 }
589 if (processing_template_decl && expr != error_mark_node)
590 {
591 if (overload != NULL_TREE)
592 {
593 if (orig_index_exp == NULL_TREE)
594 {
595 expr = build_min_non_dep_op_overload (expr, overload,
596 orig_array_expr,
597 orig_index_exp_list);
598 release_tree_vector (orig_index_exp_list);
599 return expr;
600 }
601 return build_min_non_dep_op_overload (ARRAY_REF, expr, overload,
602 orig_array_expr,
603 orig_index_exp);
604 }
605
606 if (orig_index_exp == NULL_TREE)
607 {
608 orig_index_exp
609 = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
610 orig_index_exp_list);
611 release_tree_vector (orig_index_exp_list);
612 }
613
614 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr,
615 orig_index_exp, NULL_TREE, NULL_TREE);
616 }
617 return expr;
618 }
619
620 /* Build an OMP_ARRAY_SECTION expression, handling usage in template
621 definitions, etc. */
622
623 tree
624 grok_omp_array_section (location_t loc, tree array_expr, tree index,
625 tree length)
626 {
627 tree orig_array_expr = array_expr;
628 tree orig_index = index;
629 tree orig_length = length;
630
631 if (error_operand_p (array_expr)
632 || error_operand_p (index)
633 || error_operand_p (length))
634 return error_mark_node;
635
636 if (processing_template_decl
637 && (type_dependent_expression_p (array_expr)
638 || type_dependent_expression_p (index)
639 || type_dependent_expression_p (length)))
640 return build_min_nt_loc (loc, OMP_ARRAY_SECTION, array_expr, index, length);
641
642 index = fold_non_dependent_expr (index);
643 length = fold_non_dependent_expr (length);
644
645 /* NOTE: We can pass through invalidly-typed index/length fields
646 here (e.g. if the user tries to use a floating-point index/length).
647 This is diagnosed later in semantics.cc:handle_omp_array_sections_1. */
648
649 tree expr = build_omp_array_section (loc, array_expr, index, length);
650
651 if (processing_template_decl)
652 expr = build_min_non_dep (OMP_ARRAY_SECTION, expr, orig_array_expr,
653 orig_index, orig_length);
654 return expr;
655 }
656
657 /* Given the cast expression EXP, checking out its validity. Either return
658 an error_mark_node if there was an unavoidable error, return a cast to
659 void for trying to delete a pointer w/ the value 0, or return the
660 call to delete. If DOING_VEC is true, we handle things differently
661 for doing an array delete.
662 Implements ARM $5.3.4. This is called from the parser. */
663
664 tree
665 delete_sanity (location_t loc, tree exp, tree size, bool doing_vec,
666 int use_global_delete, tsubst_flags_t complain)
667 {
668 tree t, type;
669
670 if (exp == error_mark_node)
671 return exp;
672
673 if (processing_template_decl)
674 {
675 t = build_min (DELETE_EXPR, void_type_node, exp, size);
676 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
677 DELETE_EXPR_USE_VEC (t) = doing_vec;
678 TREE_SIDE_EFFECTS (t) = 1;
679 SET_EXPR_LOCATION (t, loc);
680 return t;
681 }
682
683 location_t exp_loc = cp_expr_loc_or_loc (exp, loc);
684
685 /* An array can't have been allocated by new, so complain. */
686 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
687 && (complain & tf_warning))
688 warning_at (exp_loc, 0, "deleting array %q#E", exp);
689
690 t = build_expr_type_conversion (WANT_POINTER, exp, true);
691
692 if (t == NULL_TREE || t == error_mark_node)
693 {
694 if (complain & tf_error)
695 error_at (exp_loc,
696 "type %q#T argument given to %<delete%>, expected pointer",
697 TREE_TYPE (exp));
698 return error_mark_node;
699 }
700
701 type = TREE_TYPE (t);
702
703 /* As of Valley Forge, you can delete a pointer to const. */
704
705 /* You can't delete functions. */
706 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
707 {
708 if (complain & tf_error)
709 error_at (exp_loc,
710 "cannot delete a function. Only pointer-to-objects are "
711 "valid arguments to %<delete%>");
712 return error_mark_node;
713 }
714
715 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
716 if (VOID_TYPE_P (TREE_TYPE (type)))
717 {
718 if (complain & tf_warning)
719 warning_at (exp_loc, OPT_Wdelete_incomplete,
720 "deleting %qT is undefined", type);
721 doing_vec = 0;
722 }
723
724 /* Deleting a pointer with the value zero is valid and has no effect. */
725 if (integer_zerop (t))
726 return build1_loc (loc, NOP_EXPR, void_type_node, t);
727
728 if (doing_vec)
729 return build_vec_delete (loc, t, /*maxindex=*/NULL_TREE,
730 sfk_deleting_destructor,
731 use_global_delete, complain);
732 else
733 return build_delete (loc, type, t, sfk_deleting_destructor,
734 LOOKUP_NORMAL, use_global_delete,
735 complain);
736 }
737
738 /* Report an error if the indicated template declaration is not the
739 sort of thing that should be a member template. */
740
741 void
742 check_member_template (tree tmpl)
743 {
744 tree decl;
745
746 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
747 decl = DECL_TEMPLATE_RESULT (tmpl);
748
749 if (TREE_CODE (decl) == FUNCTION_DECL
750 || DECL_ALIAS_TEMPLATE_P (tmpl)
751 || (TREE_CODE (decl) == TYPE_DECL
752 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
753 {
754 /* The parser rejects template declarations in local classes
755 (with the exception of generic lambdas). */
756 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
757 /* The parser rejects any use of virtual in a function template. */
758 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
759 && DECL_VIRTUAL_P (decl)));
760
761 /* The debug-information generating code doesn't know what to do
762 with member templates. */
763 DECL_IGNORED_P (tmpl) = 1;
764 }
765 else if (variable_template_p (tmpl))
766 /* OK */;
767 else
768 error ("template declaration of %q#D", decl);
769 }
770
771 /* Sanity check: report error if this function FUNCTION is not
772 really a member of the class (CTYPE) it is supposed to belong to.
773 TEMPLATE_PARMS is used to specify the template parameters of a member
774 template passed as FUNCTION_DECL. If the member template is passed as a
775 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
776 from the declaration. If the function is not a function template, it
777 must be NULL.
778 It returns the original declaration for the function, NULL_TREE if
779 no declaration was found, error_mark_node if an error was emitted. */
780
781 tree
782 check_classfn (tree ctype, tree function, tree template_parms)
783 {
784 if (DECL_USE_TEMPLATE (function)
785 && !(TREE_CODE (function) == TEMPLATE_DECL
786 && DECL_TEMPLATE_SPECIALIZATION (function))
787 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
788 /* Since this is a specialization of a member template,
789 we're not going to find the declaration in the class.
790 For example, in:
791
792 struct S { template <typename T> void f(T); };
793 template <> void S::f(int);
794
795 we're not going to find `S::f(int)', but there's no
796 reason we should, either. We let our callers know we didn't
797 find the method, but we don't complain. */
798 return NULL_TREE;
799
800 /* Basic sanity check: for a template function, the template parameters
801 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
802 if (TREE_CODE (function) == TEMPLATE_DECL)
803 {
804 if (template_parms
805 && !comp_template_parms (template_parms,
806 DECL_TEMPLATE_PARMS (function)))
807 {
808 error ("template parameter lists provided don%'t match the "
809 "template parameters of %qD", function);
810 return error_mark_node;
811 }
812 template_parms = DECL_TEMPLATE_PARMS (function);
813 }
814
815 /* OK, is this a definition of a member template? */
816 bool is_template = (template_parms != NULL_TREE);
817
818 /* [temp.mem]
819
820 A destructor shall not be a member template. */
821 if (DECL_DESTRUCTOR_P (function) && is_template)
822 {
823 error ("destructor %qD declared as member template", function);
824 return error_mark_node;
825 }
826
827 /* We must enter the scope here, because conversion operators are
828 named by target type, and type equivalence relies on typenames
829 resolving within the scope of CTYPE. */
830 tree pushed_scope = push_scope (ctype);
831 tree matched = NULL_TREE;
832 tree fns = get_class_binding (ctype, DECL_NAME (function));
833 bool saw_template = false;
834
835 for (ovl_iterator iter (fns); !matched && iter; ++iter)
836 {
837 tree fndecl = *iter;
838
839 if (TREE_CODE (fndecl) == TEMPLATE_DECL)
840 saw_template = true;
841
842 /* A member template definition only matches a member template
843 declaration. */
844 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
845 continue;
846
847 if (!DECL_DECLARES_FUNCTION_P (fndecl))
848 continue;
849
850 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
851 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
852
853 /* We cannot simply call decls_match because this doesn't work
854 for static member functions that are pretending to be
855 methods, and because the name may have been changed by
856 asm("new_name"). */
857
858 /* Get rid of the this parameter on functions that become
859 static. */
860 if (DECL_STATIC_FUNCTION_P (fndecl)
861 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
862 p1 = TREE_CHAIN (p1);
863
864 /* ref-qualifier or absence of same must match. */
865 if (type_memfn_rqual (TREE_TYPE (function))
866 != type_memfn_rqual (TREE_TYPE (fndecl)))
867 continue;
868
869 // Include constraints in the match.
870 tree c1 = get_constraints (function);
871 tree c2 = get_constraints (fndecl);
872
873 /* While finding a match, same types and params are not enough
874 if the function is versioned. Also check for different target
875 specific attributes. */
876 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
877 TREE_TYPE (TREE_TYPE (fndecl)))
878 && compparms (p1, p2)
879 && !targetm.target_option.function_versions (function, fndecl)
880 && (!is_template
881 || comp_template_parms (template_parms,
882 DECL_TEMPLATE_PARMS (fndecl)))
883 && equivalent_constraints (c1, c2)
884 && (DECL_TEMPLATE_SPECIALIZATION (function)
885 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
886 && (!DECL_TEMPLATE_SPECIALIZATION (function)
887 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
888 matched = fndecl;
889 }
890
891 if (!matched && !is_template && saw_template
892 && !processing_template_decl && DECL_UNIQUE_FRIEND_P (function))
893 {
894 /* "[if no non-template match is found,] each remaining function template
895 is replaced with the specialization chosen by deduction from the
896 friend declaration or discarded if deduction fails."
897
898 So tell check_explicit_specialization to look for a match. */
899 SET_DECL_IMPLICIT_INSTANTIATION (function);
900 DECL_TEMPLATE_INFO (function) = build_template_info (fns, NULL_TREE);
901 matched = function;
902 }
903
904 if (!matched)
905 {
906 if (!COMPLETE_TYPE_P (ctype))
907 cxx_incomplete_type_error (DECL_SOURCE_LOCATION (function),
908 function, ctype);
909 else
910 {
911 if (DECL_CONV_FN_P (function))
912 fns = get_class_binding (ctype, conv_op_identifier);
913
914 auto_diagnostic_group d;
915 error_at (DECL_SOURCE_LOCATION (function),
916 "no declaration matches %q#D", function);
917 if (fns)
918 print_candidates (fns);
919 else if (DECL_CONV_FN_P (function))
920 inform (DECL_SOURCE_LOCATION (function),
921 "no conversion operators declared");
922 else
923 inform (DECL_SOURCE_LOCATION (function),
924 "no functions named %qD", function);
925 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
926 "%#qT defined here", ctype);
927 }
928 matched = error_mark_node;
929 }
930
931 if (pushed_scope)
932 pop_scope (pushed_scope);
933
934 return matched;
935 }
936
937 /* DECL is a function with vague linkage. Remember it so that at the
938 end of the translation unit we can decide whether or not to emit
939 it. */
940
941 void
942 note_vague_linkage_fn (tree decl)
943 {
944 if (processing_template_decl)
945 return;
946
947 DECL_DEFER_OUTPUT (decl) = 1;
948 vec_safe_push (deferred_fns, decl);
949 }
950
951 /* As above, but for variables. */
952
953 void
954 note_vague_linkage_variable (tree decl)
955 {
956 vec_safe_push (pending_statics, decl);
957 }
958
959 /* We have just processed the DECL, which is a static data member.
960 The other parameters are as for cp_finish_decl. */
961
962 void
963 finish_static_data_member_decl (tree decl,
964 tree init, bool init_const_expr_p,
965 tree asmspec_tree,
966 int flags)
967 {
968 if (DECL_TEMPLATE_INSTANTIATED (decl))
969 /* We already needed to instantiate this, so the processing in this
970 function is unnecessary/wrong. */
971 return;
972
973 DECL_CONTEXT (decl) = current_class_type;
974
975 /* We cannot call pushdecl here, because that would fill in the
976 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
977 the right thing, namely, to put this decl out straight away. */
978
979 if (! processing_template_decl)
980 vec_safe_push (pending_statics, decl);
981
982 if (LOCAL_CLASS_P (current_class_type)
983 /* We already complained about the template definition. */
984 && !DECL_TEMPLATE_INSTANTIATION (decl))
985 permerror (DECL_SOURCE_LOCATION (decl),
986 "local class %q#T shall not have static data member %q#D",
987 current_class_type, decl);
988 else
989 for (tree t = current_class_type; TYPE_P (t);
990 t = CP_TYPE_CONTEXT (t))
991 if (TYPE_UNNAMED_P (t))
992 {
993 auto_diagnostic_group d;
994 if (permerror (DECL_SOURCE_LOCATION (decl),
995 "static data member %qD in unnamed class", decl))
996 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
997 "unnamed class defined here");
998 break;
999 }
1000
1001 if (DECL_INLINE_VAR_P (decl) && !DECL_TEMPLATE_INSTANTIATION (decl))
1002 /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P.
1003 Except that if decl is a template instantiation, it isn't defined until
1004 instantiate_decl. */;
1005 else
1006 DECL_IN_AGGR_P (decl) = 1;
1007
1008 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
1009 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
1010 SET_VAR_HAD_UNKNOWN_BOUND (decl);
1011
1012 if (init)
1013 {
1014 /* Similarly to start_decl_1, we want to complete the type in order
1015 to do the right thing in cp_apply_type_quals_to_decl, possibly
1016 clear TYPE_QUAL_CONST (c++/65579). */
1017 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
1018 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
1019 }
1020
1021 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
1022 check_module_decl_linkage (decl);
1023 }
1024
1025 /* DECLARATOR and DECLSPECS correspond to a class member. The other
1026 parameters are as for cp_finish_decl. Return the DECL for the
1027 class member declared. */
1028
1029 tree
1030 grokfield (const cp_declarator *declarator,
1031 cp_decl_specifier_seq *declspecs,
1032 tree init, bool init_const_expr_p,
1033 tree asmspec_tree,
1034 tree attrlist)
1035 {
1036 tree value;
1037 const char *asmspec = 0;
1038 int flags;
1039
1040 if (init
1041 && TREE_CODE (init) == TREE_LIST
1042 && TREE_VALUE (init) == error_mark_node
1043 && TREE_CHAIN (init) == NULL_TREE)
1044 init = NULL_TREE;
1045
1046 int initialized;
1047 if (init == ridpointers[(int)RID_DELETE]
1048 || (init
1049 && TREE_CODE (init) == STRING_CST
1050 && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
1051 initialized = SD_DELETED;
1052 else if (init == ridpointers[(int)RID_DEFAULT])
1053 initialized = SD_DEFAULTED;
1054 else if (init)
1055 initialized = SD_INITIALIZED;
1056 else
1057 initialized = SD_UNINITIALIZED;
1058
1059 value = grokdeclarator (declarator, declspecs, FIELD, initialized, &attrlist);
1060 if (! value || value == error_mark_node)
1061 /* friend or constructor went bad. */
1062 return error_mark_node;
1063 if (TREE_TYPE (value) == error_mark_node)
1064 return value;
1065
1066 if (TREE_CODE (value) == TYPE_DECL && init)
1067 {
1068 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)),
1069 "typedef %qD is initialized (use %qs instead)",
1070 value, "decltype");
1071 init = NULL_TREE;
1072 }
1073
1074 /* Pass friendly classes back. */
1075 if (value == void_type_node)
1076 return value;
1077
1078 if (DECL_NAME (value)
1079 && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR)
1080 {
1081 error_at (declarator->id_loc,
1082 "explicit template argument list not allowed");
1083 return error_mark_node;
1084 }
1085
1086 /* Stash away type declarations. */
1087 if (TREE_CODE (value) == TYPE_DECL)
1088 {
1089 DECL_NONLOCAL (value) = 1;
1090 DECL_CONTEXT (value) = current_class_type;
1091
1092 if (attrlist)
1093 {
1094 int attrflags = 0;
1095
1096 /* If this is a typedef that names the class for linkage purposes
1097 (7.1.3p8), apply any attributes directly to the type. */
1098 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
1099 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
1100 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
1101
1102 cplus_decl_attributes (&value, attrlist, attrflags);
1103 }
1104
1105 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
1106 && TREE_TYPE (value) != error_mark_node
1107 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
1108 set_underlying_type (value);
1109
1110 /* It's important that push_template_decl below follows
1111 set_underlying_type above so that the created template
1112 carries the properly set type of VALUE. */
1113 if (processing_template_decl)
1114 value = push_template_decl (value);
1115
1116 record_locally_defined_typedef (value);
1117 return value;
1118 }
1119
1120 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
1121
1122 if (!friendp && DECL_IN_AGGR_P (value))
1123 {
1124 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
1125 return void_type_node;
1126 }
1127
1128 if (asmspec_tree && asmspec_tree != error_mark_node)
1129 asmspec = TREE_STRING_POINTER (asmspec_tree);
1130
1131 if (init)
1132 {
1133 if (TREE_CODE (value) == FUNCTION_DECL)
1134 {
1135 if (init == ridpointers[(int)RID_DELETE]
1136 || (TREE_CODE (init) == STRING_CST
1137 && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
1138 {
1139 DECL_DELETED_FN (value) = 1;
1140 DECL_DECLARED_INLINE_P (value) = 1;
1141 if (TREE_CODE (init) == STRING_CST)
1142 DECL_INITIAL (value) = init;
1143 }
1144 else if (init == ridpointers[(int)RID_DEFAULT])
1145 {
1146 if (defaultable_fn_check (value))
1147 {
1148 DECL_DEFAULTED_FN (value) = 1;
1149 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
1150 DECL_DECLARED_INLINE_P (value) = 1;
1151 /* grokfndecl set this to error_mark_node, but we want to
1152 leave it unset until synthesize_method. */
1153 DECL_INITIAL (value) = NULL_TREE;
1154 }
1155 }
1156 else if (TREE_CODE (init) == DEFERRED_PARSE)
1157 error ("invalid initializer for member function %qD", value);
1158 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
1159 {
1160 if (integer_zerop (init))
1161 DECL_PURE_VIRTUAL_P (value) = 1;
1162 else if (error_operand_p (init))
1163 ; /* An error has already been reported. */
1164 else
1165 error ("invalid initializer for member function %qD",
1166 value);
1167 }
1168 else
1169 {
1170 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
1171 location_t iloc
1172 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value));
1173 if (friendp)
1174 error_at (iloc, "initializer specified for friend "
1175 "function %qD", value);
1176 else
1177 error_at (iloc, "initializer specified for static "
1178 "member function %qD", value);
1179 }
1180 }
1181 else if (TREE_CODE (value) == FIELD_DECL)
1182 /* C++11 NSDMI, keep going. */;
1183 else if (!VAR_P (value))
1184 gcc_unreachable ();
1185 }
1186
1187 /* Pass friend decls back. */
1188 if ((TREE_CODE (value) == FUNCTION_DECL
1189 || TREE_CODE (value) == TEMPLATE_DECL)
1190 && DECL_CONTEXT (value) != current_class_type)
1191 {
1192 if (attrlist)
1193 cplus_decl_attributes (&value, attrlist, 0);
1194 return value;
1195 }
1196
1197 /* Need to set this before push_template_decl. */
1198 if (VAR_P (value))
1199 DECL_CONTEXT (value) = current_class_type;
1200
1201 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1202 {
1203 value = push_template_decl (value);
1204 if (error_operand_p (value))
1205 return error_mark_node;
1206 }
1207
1208 if (attrlist)
1209 cplus_decl_attributes (&value, attrlist, 0);
1210
1211 if (init && DIRECT_LIST_INIT_P (init))
1212 flags = LOOKUP_NORMAL;
1213 else
1214 flags = LOOKUP_IMPLICIT;
1215
1216 switch (TREE_CODE (value))
1217 {
1218 case VAR_DECL:
1219 finish_static_data_member_decl (value, init, init_const_expr_p,
1220 asmspec_tree, flags);
1221 return value;
1222
1223 case FIELD_DECL:
1224 if (asmspec)
1225 error ("%<asm%> specifiers are not permitted on non-static data members");
1226 if (DECL_INITIAL (value) == error_mark_node)
1227 init = error_mark_node;
1228 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1229 NULL_TREE, flags);
1230 DECL_IN_AGGR_P (value) = 1;
1231 return value;
1232
1233 case FUNCTION_DECL:
1234 if (asmspec)
1235 set_user_assembler_name (value, asmspec);
1236
1237 cp_finish_decl (value,
1238 /*init=*/NULL_TREE,
1239 /*init_const_expr_p=*/false,
1240 asmspec_tree, flags);
1241
1242 /* Pass friends back this way. */
1243 if (DECL_UNIQUE_FRIEND_P (value))
1244 return void_type_node;
1245
1246 DECL_IN_AGGR_P (value) = 1;
1247 return value;
1248
1249 default:
1250 gcc_unreachable ();
1251 }
1252 return NULL_TREE;
1253 }
1254
1255 /* Like grokfield, but just for the initial grok of an initialized static
1256 member. Used to be able to push the new decl before parsing the
1257 initialiser. */
1258
1259 tree
1260 start_initialized_static_member (const cp_declarator *declarator,
1261 cp_decl_specifier_seq *declspecs,
1262 tree attrlist)
1263 {
1264 tree value = grokdeclarator (declarator, declspecs, FIELD, SD_INITIALIZED,
1265 &attrlist);
1266 if (!value || error_operand_p (value))
1267 return error_mark_node;
1268 if (TREE_CODE (value) == TYPE_DECL)
1269 {
1270 error_at (declarator->init_loc,
1271 "typedef %qD is initialized (use %qs instead)",
1272 value, "decltype");
1273 return error_mark_node;
1274 }
1275 else if (TREE_CODE (value) == FUNCTION_DECL)
1276 {
1277 if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
1278 error_at (declarator->init_loc,
1279 "invalid initializer for member function %qD",
1280 value);
1281 else if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
1282 error_at (declarator->init_loc,
1283 "initializer specified for static member function %qD",
1284 value);
1285 else
1286 gcc_unreachable ();
1287 return error_mark_node;
1288 }
1289 else if (TREE_CODE (value) == FIELD_DECL)
1290 {
1291 /* NSDM marked 'static', grokdeclarator has already errored. */
1292 gcc_checking_assert (seen_error ());
1293 return error_mark_node;
1294 }
1295 gcc_checking_assert (VAR_P (value));
1296
1297 DECL_CONTEXT (value) = current_class_type;
1298 DECL_INITIALIZED_IN_CLASS_P (value) = true;
1299
1300 if (processing_template_decl)
1301 {
1302 value = push_template_decl (value);
1303 if (error_operand_p (value))
1304 return error_mark_node;
1305 }
1306
1307 if (attrlist)
1308 cplus_decl_attributes (&value, attrlist, 0);
1309
1310 /* When defining a template we need to register the TEMPLATE_DECL. */
1311 tree maybe_template = value;
1312 if (template_parm_scope_p ())
1313 {
1314 if (!DECL_TEMPLATE_SPECIALIZATION (value))
1315 maybe_template = DECL_TI_TEMPLATE (value);
1316 else
1317 maybe_template = NULL_TREE;
1318 }
1319 if (maybe_template)
1320 finish_member_declaration (maybe_template);
1321
1322 return value;
1323 }
1324
1325 /* Whether DECL is a static data member initialized at the point
1326 of declaration within its class. */
1327
1328 bool
1329 is_static_data_member_initialized_in_class (tree decl)
1330 {
1331 if (!decl || decl == error_mark_node)
1332 return false;
1333
1334 tree inner = STRIP_TEMPLATE (decl);
1335 return (inner
1336 && VAR_P (inner)
1337 && DECL_CLASS_SCOPE_P (inner)
1338 && DECL_INITIALIZED_IN_CLASS_P (inner));
1339 }
1340
1341 /* Finish a declaration prepared with start_initialized_static_member. */
1342
1343 void
1344 finish_initialized_static_member (tree decl, tree init, tree asmspec)
1345 {
1346 if (decl == error_mark_node)
1347 return;
1348 gcc_checking_assert (is_static_data_member_initialized_in_class (decl));
1349
1350 int flags;
1351 if (init && DIRECT_LIST_INIT_P (init))
1352 flags = LOOKUP_NORMAL;
1353 else
1354 flags = LOOKUP_IMPLICIT;
1355 finish_static_data_member_decl (decl, init, /*init_const_expr_p=*/true,
1356 asmspec, flags);
1357 }
1358
1359 /* Like `grokfield', but for bitfields.
1360 WIDTH is the width of the bitfield, a constant expression.
1361 The other parameters are as for grokfield. */
1362
1363 tree
1364 grokbitfield (const cp_declarator *declarator,
1365 cp_decl_specifier_seq *declspecs, tree width, tree init,
1366 tree attrlist)
1367 {
1368 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1369 init != NULL_TREE, &attrlist);
1370
1371 if (value == error_mark_node)
1372 return NULL_TREE; /* friends went bad. */
1373
1374 tree type = TREE_TYPE (value);
1375 if (type == error_mark_node)
1376 return value;
1377
1378 /* Pass friendly classes back. */
1379 if (VOID_TYPE_P (value))
1380 return void_type_node;
1381
1382 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)
1383 && (INDIRECT_TYPE_P (type) || !dependent_type_p (type)))
1384 {
1385 error_at (DECL_SOURCE_LOCATION (value),
1386 "bit-field %qD with non-integral type %qT",
1387 value, type);
1388 return error_mark_node;
1389 }
1390
1391 if (TREE_CODE (value) == TYPE_DECL)
1392 {
1393 error_at (DECL_SOURCE_LOCATION (value),
1394 "cannot declare %qD to be a bit-field type", value);
1395 return NULL_TREE;
1396 }
1397
1398 /* Usually, finish_struct_1 catches bitfields with invalid types.
1399 But, in the case of bitfields with function type, we confuse
1400 ourselves into thinking they are member functions, so we must
1401 check here. */
1402 if (TREE_CODE (value) == FUNCTION_DECL)
1403 {
1404 error_at (DECL_SOURCE_LOCATION (value),
1405 "cannot declare bit-field %qD with function type", value);
1406 return NULL_TREE;
1407 }
1408
1409 if (TYPE_WARN_IF_NOT_ALIGN (type))
1410 {
1411 error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field "
1412 "%qD with %<warn_if_not_aligned%> type", value);
1413 return NULL_TREE;
1414 }
1415
1416 if (DECL_IN_AGGR_P (value))
1417 {
1418 error ("%qD is already defined in the class %qT", value,
1419 DECL_CONTEXT (value));
1420 return void_type_node;
1421 }
1422
1423 if (TREE_STATIC (value))
1424 {
1425 error_at (DECL_SOURCE_LOCATION (value),
1426 "static member %qD cannot be a bit-field", value);
1427 return NULL_TREE;
1428 }
1429
1430 int flags = LOOKUP_IMPLICIT;
1431 if (init && DIRECT_LIST_INIT_P (init))
1432 flags = LOOKUP_NORMAL;
1433 cp_finish_decl (value, init, false, NULL_TREE, flags);
1434
1435 if (width != error_mark_node)
1436 {
1437 /* The width must be an integer type. */
1438 if (!type_dependent_expression_p (width)
1439 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1440 error ("width of bit-field %qD has non-integral type %qT", value,
1441 TREE_TYPE (width));
1442 else if (!check_for_bare_parameter_packs (width))
1443 {
1444 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1445 check_bitfield_decl picks it from there later and sets DECL_SIZE
1446 accordingly. */
1447 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1448 SET_DECL_C_BIT_FIELD (value);
1449 }
1450 }
1451
1452 DECL_IN_AGGR_P (value) = 1;
1453
1454 if (attrlist)
1455 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1456
1457 return value;
1458 }
1459
1460
1461 /* Returns true iff ATTR is an attribute which needs to be applied at
1462 instantiation time rather than template definition time. */
1463
1464 static bool
1465 is_late_template_attribute (tree attr, tree decl)
1466 {
1467 tree name = get_attribute_name (attr);
1468 tree args = TREE_VALUE (attr);
1469 const struct attribute_spec *spec = lookup_attribute_spec (name);
1470 tree arg;
1471
1472 if (!spec)
1473 /* Unknown attribute. */
1474 return false;
1475
1476 /* Attribute weak handling wants to write out assembly right away. */
1477 if (is_attribute_p ("weak", name))
1478 return true;
1479
1480 /* Attributes used and unused are applied directly to typedefs for the
1481 benefit of maybe_warn_unused_local_typedefs. */
1482 if (TREE_CODE (decl) == TYPE_DECL
1483 && (is_attribute_p ("unused", name)
1484 || is_attribute_p ("used", name)))
1485 return false;
1486
1487 /* Attribute tls_model wants to modify the symtab. */
1488 if (is_attribute_p ("tls_model", name))
1489 return true;
1490
1491 /* #pragma omp declare simd attribute needs to be always deferred. */
1492 if (flag_openmp
1493 && is_attribute_p ("omp declare simd", name))
1494 return true;
1495
1496 if (args == error_mark_node)
1497 return false;
1498
1499 /* An attribute pack is clearly dependent. */
1500 if (args && PACK_EXPANSION_P (args))
1501 return true;
1502
1503 /* If any of the arguments are dependent expressions, we can't evaluate
1504 the attribute until instantiation time. */
1505 for (arg = args; arg; arg = TREE_CHAIN (arg))
1506 {
1507 tree t = TREE_VALUE (arg);
1508
1509 /* If the first attribute argument is an identifier, only consider
1510 second and following arguments. Attributes like mode, format,
1511 cleanup and several target specific attributes aren't late
1512 just because they have an IDENTIFIER_NODE as first argument. */
1513 if (arg == args && attribute_takes_identifier_p (name)
1514 && identifier_p (t))
1515 continue;
1516
1517 if (value_dependent_expression_p (t))
1518 return true;
1519 }
1520
1521 if (TREE_CODE (decl) == TYPE_DECL
1522 || TYPE_P (decl)
1523 || spec->type_required)
1524 {
1525 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1526
1527 if (!type)
1528 return true;
1529
1530 /* We can't apply any attributes to a completely unknown type until
1531 instantiation time. */
1532 enum tree_code code = TREE_CODE (type);
1533 if (code == TEMPLATE_TYPE_PARM
1534 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1535 || code == TYPENAME_TYPE)
1536 return true;
1537 /* Also defer most attributes on dependent types. This is not
1538 necessary in all cases, but is the better default. */
1539 else if (dependent_type_p (type)
1540 /* But some attributes specifically apply to templates. */
1541 && !is_attribute_p ("abi_tag", name)
1542 && !is_attribute_p ("deprecated", name)
1543 && !is_attribute_p ("unavailable", name)
1544 && !is_attribute_p ("visibility", name))
1545 return true;
1546 else
1547 return false;
1548 }
1549 else
1550 return false;
1551 }
1552
1553 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1554 applied at instantiation time and return them. If IS_DEPENDENT is true,
1555 the declaration itself is dependent, so all attributes should be applied
1556 at instantiation time. */
1557
1558 tree
1559 splice_template_attributes (tree *attr_p, tree decl)
1560 {
1561 tree *p = attr_p;
1562 tree late_attrs = NULL_TREE;
1563 tree *q = &late_attrs;
1564
1565 if (!p || *p == error_mark_node)
1566 return NULL_TREE;
1567
1568 for (; *p; )
1569 {
1570 if (is_late_template_attribute (*p, decl))
1571 {
1572 ATTR_IS_DEPENDENT (*p) = 1;
1573 *q = *p;
1574 *p = TREE_CHAIN (*p);
1575 q = &TREE_CHAIN (*q);
1576 *q = NULL_TREE;
1577 }
1578 else
1579 p = &TREE_CHAIN (*p);
1580 }
1581
1582 return late_attrs;
1583 }
1584
1585 /* Attach any LATE_ATTRS to DECL_P, after the non-dependent attributes have
1586 been applied by a previous call to decl_attributes. */
1587
1588 static void
1589 save_template_attributes (tree late_attrs, tree *decl_p, int flags)
1590 {
1591 tree *q;
1592
1593 if (!late_attrs)
1594 return;
1595
1596 if (DECL_P (*decl_p))
1597 q = &DECL_ATTRIBUTES (*decl_p);
1598 else
1599 q = &TYPE_ATTRIBUTES (*decl_p);
1600
1601 tree old_attrs = *q;
1602
1603 /* Place the late attributes at the beginning of the attribute
1604 list. */
1605 late_attrs = chainon (late_attrs, *q);
1606 if (*q != late_attrs
1607 && !DECL_P (*decl_p)
1608 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1609 {
1610 if (!dependent_type_p (*decl_p))
1611 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1612 else
1613 {
1614 *decl_p = build_variant_type_copy (*decl_p);
1615 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1616 }
1617 }
1618 else
1619 *q = late_attrs;
1620
1621 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1622 {
1623 /* We've added new attributes directly to the main variant, so
1624 now we need to update all of the other variants to include
1625 these new attributes. */
1626 tree variant;
1627 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1628 variant = TYPE_NEXT_VARIANT (variant))
1629 {
1630 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1631 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1632 }
1633 }
1634 }
1635
1636 /* True if ATTRS contains any dependent attributes that affect type
1637 identity. */
1638
1639 bool
1640 any_dependent_type_attributes_p (tree attrs)
1641 {
1642 for (tree a = attrs; a; a = TREE_CHAIN (a))
1643 if (ATTR_IS_DEPENDENT (a))
1644 {
1645 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1646 if (as && as->affects_type_identity)
1647 return true;
1648 }
1649 return false;
1650 }
1651
1652 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1653 to a typedef which gives a previously unnamed class or enum a name for
1654 linkage purposes. */
1655
1656 bool
1657 attributes_naming_typedef_ok (tree attrs)
1658 {
1659 for (; attrs; attrs = TREE_CHAIN (attrs))
1660 {
1661 tree name = get_attribute_name (attrs);
1662 if (is_attribute_p ("vector_size", name))
1663 return false;
1664 }
1665 return true;
1666 }
1667
1668 /* Like reconstruct_complex_type, but handle also template trees. */
1669
1670 tree
1671 cp_reconstruct_complex_type (tree type, tree bottom)
1672 {
1673 tree inner, outer;
1674
1675 if (TYPE_PTR_P (type))
1676 {
1677 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1678 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1679 TYPE_REF_CAN_ALIAS_ALL (type));
1680 }
1681 else if (TYPE_REF_P (type))
1682 {
1683 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1684 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1685 TYPE_REF_CAN_ALIAS_ALL (type));
1686 }
1687 else if (TREE_CODE (type) == ARRAY_TYPE)
1688 {
1689 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1690 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1691 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1692 element type qualification will be handled by the recursive
1693 cp_reconstruct_complex_type call and cp_build_qualified_type
1694 for ARRAY_TYPEs changes the element type. */
1695 return outer;
1696 }
1697 else if (TREE_CODE (type) == FUNCTION_TYPE)
1698 {
1699 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1700 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1701 outer = apply_memfn_quals (outer, type_memfn_quals (type));
1702 }
1703 else if (TREE_CODE (type) == METHOD_TYPE)
1704 {
1705 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1706 /* The build_method_type_directly() routine prepends 'this' to argument list,
1707 so we must compensate by getting rid of it. */
1708 outer
1709 = build_method_type_directly
1710 (class_of_this_parm (type), inner,
1711 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1712 }
1713 else if (TREE_CODE (type) == OFFSET_TYPE)
1714 {
1715 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1716 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1717 }
1718 else
1719 return bottom;
1720
1721 if (TYPE_ATTRIBUTES (type))
1722 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1723 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1724 outer = cxx_copy_lang_qualifiers (outer, type);
1725
1726 return outer;
1727 }
1728
1729 /* Replaces any constexpr expression that may be into the attributes
1730 arguments with their reduced value. */
1731
1732 void
1733 cp_check_const_attributes (tree attributes)
1734 {
1735 if (attributes == error_mark_node)
1736 return;
1737
1738 tree attr;
1739 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1740 {
1741 if (cxx_contract_attribute_p (attr))
1742 continue;
1743
1744 tree arg;
1745 /* As we implement alignas using gnu::aligned attribute and
1746 alignas argument is a constant expression, force manifestly
1747 constant evaluation of aligned attribute argument. */
1748 bool manifestly_const_eval
1749 = is_attribute_p ("aligned", get_attribute_name (attr));
1750 for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1751 arg = TREE_CHAIN (arg))
1752 {
1753 tree expr = TREE_VALUE (arg);
1754 if (EXPR_P (expr))
1755 TREE_VALUE (arg)
1756 = fold_non_dependent_expr (expr, tf_warning_or_error,
1757 manifestly_const_eval);
1758 }
1759 }
1760 }
1761
1762 /* Copies hot or cold attributes to a function FN if present on the
1763 encapsulating class, struct, or union TYPE. */
1764
1765 void
1766 maybe_propagate_warmth_attributes (tree fn, tree type)
1767 {
1768 if (fn == NULL_TREE || type == NULL_TREE
1769 || !(TREE_CODE (type) == RECORD_TYPE
1770 || TREE_CODE (type) == UNION_TYPE))
1771 return;
1772
1773 tree has_cold_attr = lookup_attribute ("cold", TYPE_ATTRIBUTES (type));
1774 tree has_hot_attr = lookup_attribute ("hot", TYPE_ATTRIBUTES (type));
1775
1776 if (has_cold_attr || has_hot_attr)
1777 {
1778 /* Transparently ignore the new warmth attribute if it
1779 conflicts with a present function attribute. Otherwise
1780 decl_attribute would still honour the present attribute,
1781 but producing an undesired warning in the process. */
1782
1783 if (has_cold_attr)
1784 {
1785 if (lookup_attribute ("hot", DECL_ATTRIBUTES (fn)) == NULL)
1786 {
1787 tree cold_cons
1788 = tree_cons (get_identifier ("cold"), NULL, NULL);
1789
1790 decl_attributes (&fn, cold_cons, 0);
1791 }
1792 }
1793 else if (has_hot_attr)
1794 {
1795 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)) == NULL)
1796 {
1797 tree hot_cons
1798 = tree_cons (get_identifier ("hot"), NULL, NULL);
1799
1800 decl_attributes (&fn, hot_cons, 0);
1801 }
1802 }
1803 }
1804 }
1805
1806 /* Return the last pushed declaration for the symbol DECL or NULL
1807 when no such declaration exists. */
1808
1809 static tree
1810 find_last_decl (tree decl)
1811 {
1812 tree last_decl = NULL_TREE;
1813
1814 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1815 {
1816 /* Template specializations are matched elsewhere. */
1817 if (DECL_LANG_SPECIFIC (decl)
1818 && DECL_USE_TEMPLATE (decl))
1819 return NULL_TREE;
1820
1821 /* Look up the declaration in its scope. */
1822 tree pushed_scope = NULL_TREE;
1823 if (tree ctype = DECL_CONTEXT (decl))
1824 pushed_scope = push_scope (ctype);
1825
1826 last_decl = lookup_name (name);
1827
1828 if (pushed_scope)
1829 pop_scope (pushed_scope);
1830
1831 /* The declaration may be a member conversion operator
1832 or a bunch of overfloads (handle the latter below). */
1833 if (last_decl && BASELINK_P (last_decl))
1834 last_decl = BASELINK_FUNCTIONS (last_decl);
1835 }
1836
1837 if (!last_decl)
1838 return NULL_TREE;
1839
1840 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1841 {
1842 /* A set of overloads of the same function. */
1843 for (lkp_iterator iter (last_decl); iter; ++iter)
1844 {
1845 if (TREE_CODE (*iter) == OVERLOAD)
1846 continue;
1847
1848 tree d = *iter;
1849
1850 /* We can't compare versions in the middle of processing the
1851 attribute that has the version. */
1852 if (TREE_CODE (d) == FUNCTION_DECL
1853 && DECL_FUNCTION_VERSIONED (d))
1854 return NULL_TREE;
1855
1856 if (decls_match (decl, d, /*record_decls=*/false))
1857 return d;
1858 }
1859 return NULL_TREE;
1860 }
1861
1862 return NULL_TREE;
1863 }
1864
1865 /* Like decl_attributes, but handle C++ complexity. */
1866
1867 void
1868 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1869 {
1870 if (*decl == NULL_TREE || *decl == void_type_node
1871 || *decl == error_mark_node || attributes == error_mark_node)
1872 return;
1873
1874 /* Add implicit "omp declare target" attribute if requested. */
1875 if (vec_safe_length (scope_chain->omp_declare_target_attribute)
1876 && ((VAR_P (*decl)
1877 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1878 || TREE_CODE (*decl) == FUNCTION_DECL))
1879 {
1880 if (VAR_P (*decl)
1881 && DECL_CLASS_SCOPE_P (*decl))
1882 error ("%q+D static data member inside of declare target directive",
1883 *decl);
1884 else
1885 {
1886 if (VAR_P (*decl)
1887 && (processing_template_decl
1888 || !omp_mappable_type (TREE_TYPE (*decl))))
1889 attributes
1890 = tree_cons (get_identifier ("omp declare target implicit"),
1891 NULL_TREE, attributes);
1892 else
1893 attributes = tree_cons (get_identifier ("omp declare target"),
1894 NULL_TREE, attributes);
1895 if (TREE_CODE (*decl) == FUNCTION_DECL)
1896 {
1897 cp_omp_declare_target_attr &last
1898 = scope_chain->omp_declare_target_attribute->last ();
1899 int device_type = MAX (last.device_type, 0);
1900 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
1901 && !lookup_attribute ("omp declare target host",
1902 attributes))
1903 attributes
1904 = tree_cons (get_identifier ("omp declare target host"),
1905 NULL_TREE, attributes);
1906 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
1907 && !lookup_attribute ("omp declare target nohost",
1908 attributes))
1909 attributes
1910 = tree_cons (get_identifier ("omp declare target nohost"),
1911 NULL_TREE, attributes);
1912 if (last.indirect
1913 && !lookup_attribute ("omp declare target indirect",
1914 attributes))
1915 attributes
1916 = tree_cons (get_identifier ("omp declare target indirect"),
1917 NULL_TREE, attributes);
1918 }
1919 }
1920 }
1921
1922 tree late_attrs = NULL_TREE;
1923 if (processing_template_decl)
1924 {
1925 if (check_for_bare_parameter_packs (attributes))
1926 return;
1927 late_attrs = splice_template_attributes (&attributes, *decl);
1928 }
1929
1930 cp_check_const_attributes (attributes);
1931
1932 if (flag_openmp || flag_openmp_simd)
1933 {
1934 bool diagnosed = false;
1935 for (tree *pa = &attributes; *pa; )
1936 {
1937 if (get_attribute_namespace (*pa) == omp_identifier)
1938 {
1939 tree name = get_attribute_name (*pa);
1940 if (is_attribute_p ("directive", name)
1941 || is_attribute_p ("sequence", name)
1942 || is_attribute_p ("decl", name))
1943 {
1944 const char *p = NULL;
1945 if (TREE_VALUE (*pa) == NULL_TREE)
1946 p = IDENTIFIER_POINTER (name);
1947 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
1948 {
1949 tree d = TREE_VALUE (a);
1950 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
1951 if (TREE_PUBLIC (d)
1952 && (VAR_P (*decl)
1953 || TREE_CODE (*decl) == FUNCTION_DECL)
1954 && cp_maybe_parse_omp_decl (*decl, d))
1955 continue;
1956 p = TREE_PUBLIC (d) ? "decl" : "directive";
1957 }
1958 if (p && !diagnosed)
1959 {
1960 error ("%<omp::%s%> not allowed to be specified in "
1961 "this context", p);
1962 diagnosed = true;
1963 }
1964 if (p)
1965 {
1966 *pa = TREE_CHAIN (*pa);
1967 continue;
1968 }
1969 }
1970 }
1971 pa = &TREE_CHAIN (*pa);
1972 }
1973 }
1974
1975 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1976 decl = &DECL_TEMPLATE_RESULT (*decl);
1977
1978 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1979 {
1980 attributes
1981 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1982 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1983 attributes, flags);
1984 }
1985 else
1986 {
1987 tree last_decl = find_last_decl (*decl);
1988 decl_attributes (decl, attributes, flags, last_decl);
1989 }
1990
1991 if (late_attrs)
1992 save_template_attributes (late_attrs, decl, flags);
1993
1994 /* Propagate deprecation out to the template. */
1995 if (TREE_DEPRECATED (*decl))
1996 if (tree ti = get_template_info (*decl))
1997 {
1998 tree tmpl = TI_TEMPLATE (ti);
1999 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
2000 : DECL_TEMPLATE_RESULT (tmpl));
2001 if (*decl == pattern)
2002 TREE_DEPRECATED (tmpl) = true;
2003 }
2004
2005 /* Likewise, propagate unavailability out to the template. */
2006 if (TREE_UNAVAILABLE (*decl))
2007 if (tree ti = get_template_info (*decl))
2008 {
2009 tree tmpl = TI_TEMPLATE (ti);
2010 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
2011 : DECL_TEMPLATE_RESULT (tmpl));
2012 if (*decl == pattern)
2013 TREE_UNAVAILABLE (tmpl) = true;
2014 }
2015 }
2016
2017 /* Walks through the namespace- or function-scope anonymous union
2018 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
2019 Returns one of the fields for use in the mangled name. */
2020
2021 static tree
2022 build_anon_union_vars (tree type, tree object)
2023 {
2024 tree main_decl = NULL_TREE;
2025 tree field;
2026
2027 /* Rather than write the code to handle the non-union case,
2028 just give an error. */
2029 if (TREE_CODE (type) != UNION_TYPE)
2030 {
2031 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
2032 "anonymous struct not inside named type");
2033 return error_mark_node;
2034 }
2035
2036 for (field = TYPE_FIELDS (type);
2037 field != NULL_TREE;
2038 field = DECL_CHAIN (field))
2039 {
2040 tree decl;
2041 tree ref;
2042
2043 if (DECL_ARTIFICIAL (field))
2044 continue;
2045 if (TREE_CODE (field) != FIELD_DECL)
2046 {
2047 permerror (DECL_SOURCE_LOCATION (field),
2048 "%q#D invalid; an anonymous union can only "
2049 "have non-static data members", field);
2050 continue;
2051 }
2052
2053 if (TREE_PRIVATE (field))
2054 permerror (DECL_SOURCE_LOCATION (field),
2055 "private member %q#D in anonymous union", field);
2056 else if (TREE_PROTECTED (field))
2057 permerror (DECL_SOURCE_LOCATION (field),
2058 "protected member %q#D in anonymous union", field);
2059
2060 if (processing_template_decl)
2061 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
2062 field, NULL_TREE);
2063 else
2064 ref = build_class_member_access_expr (object, field, NULL_TREE,
2065 false, tf_warning_or_error);
2066
2067 if (DECL_NAME (field))
2068 {
2069 tree base;
2070
2071 decl = build_decl (input_location,
2072 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2073 DECL_ANON_UNION_VAR_P (decl) = 1;
2074 DECL_ARTIFICIAL (decl) = 1;
2075
2076 base = get_base_address (object);
2077 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
2078 TREE_STATIC (decl) = TREE_STATIC (base);
2079 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
2080
2081 SET_DECL_VALUE_EXPR (decl, ref);
2082 DECL_HAS_VALUE_EXPR_P (decl) = 1;
2083
2084 decl = pushdecl (decl);
2085 }
2086 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2087 decl = build_anon_union_vars (TREE_TYPE (field), ref);
2088 else
2089 decl = 0;
2090
2091 if (main_decl == NULL_TREE)
2092 main_decl = decl;
2093 }
2094
2095 return main_decl;
2096 }
2097
2098 /* Finish off the processing of a UNION_TYPE structure. If the union is an
2099 anonymous union, then all members must be laid out together. PUBLIC_P
2100 is nonzero if this union is not declared static. */
2101
2102 void
2103 finish_anon_union (tree anon_union_decl)
2104 {
2105 tree type;
2106 tree main_decl;
2107 bool public_p;
2108
2109 if (anon_union_decl == error_mark_node)
2110 return;
2111
2112 type = TREE_TYPE (anon_union_decl);
2113 public_p = TREE_PUBLIC (anon_union_decl);
2114
2115 /* The VAR_DECL's context is the same as the TYPE's context. */
2116 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
2117
2118 if (TYPE_FIELDS (type) == NULL_TREE)
2119 return;
2120
2121 if (public_p)
2122 {
2123 error ("namespace-scope anonymous aggregates must be static");
2124 return;
2125 }
2126
2127 main_decl = build_anon_union_vars (type, anon_union_decl);
2128 if (main_decl == error_mark_node)
2129 return;
2130 if (main_decl == NULL_TREE)
2131 {
2132 pedwarn (input_location, 0, "anonymous union with no members");
2133 return;
2134 }
2135
2136 if (!processing_template_decl)
2137 {
2138 /* Use main_decl to set the mangled name. */
2139 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
2140 maybe_commonize_var (anon_union_decl);
2141 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
2142 {
2143 if (DECL_DISCRIMINATOR_P (anon_union_decl))
2144 determine_local_discriminator (anon_union_decl);
2145 mangle_decl (anon_union_decl);
2146 }
2147 DECL_NAME (anon_union_decl) = NULL_TREE;
2148 }
2149
2150 pushdecl (anon_union_decl);
2151 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
2152 }
2153
2154 /* Auxiliary functions to make type signatures for
2155 `operator new' and `operator delete' correspond to
2156 what compiler will be expecting. */
2157
2158 tree
2159 coerce_new_type (tree type, location_t loc)
2160 {
2161 int e = 0;
2162 tree args = TYPE_ARG_TYPES (type);
2163
2164 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2165
2166 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2167 {
2168 e = 1;
2169 error_at (loc, "%<operator new%> must return type %qT",
2170 ptr_type_node);
2171 }
2172
2173 if (args && args != void_list_node)
2174 {
2175 if (TREE_PURPOSE (args))
2176 {
2177 /* [basic.stc.dynamic.allocation]
2178
2179 The first parameter shall not have an associated default
2180 argument. */
2181 error_at (loc, "the first parameter of %<operator new%> cannot "
2182 "have a default argument");
2183 /* Throw away the default argument. */
2184 TREE_PURPOSE (args) = NULL_TREE;
2185 }
2186
2187 if (!same_type_p (TREE_VALUE (args), size_type_node))
2188 {
2189 e = 2;
2190 args = TREE_CHAIN (args);
2191 }
2192 }
2193 else
2194 e = 2;
2195
2196 if (e == 2)
2197 permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
2198 "as first parameter", size_type_node);
2199
2200 switch (e)
2201 {
2202 case 2:
2203 args = tree_cons (NULL_TREE, size_type_node, args);
2204 /* Fall through. */
2205 case 1:
2206 type = (cxx_copy_lang_qualifiers
2207 (build_function_type (ptr_type_node, args),
2208 type));
2209 /* Fall through. */
2210 default:;
2211 }
2212 return type;
2213 }
2214
2215 void
2216 coerce_delete_type (tree decl, location_t loc)
2217 {
2218 int e = 0;
2219 tree type = TREE_TYPE (decl);
2220 tree args = TYPE_ARG_TYPES (type);
2221
2222 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2223
2224 if (!same_type_p (TREE_TYPE (type), void_type_node))
2225 {
2226 e = 1;
2227 error_at (loc, "%<operator delete%> must return type %qT",
2228 void_type_node);
2229 }
2230
2231 tree ptrtype = ptr_type_node;
2232 if (destroying_delete_p (decl))
2233 {
2234 if (DECL_CLASS_SCOPE_P (decl))
2235 /* If the function is a destroying operator delete declared in class
2236 type C, the type of its first parameter shall be C*. */
2237 ptrtype = build_pointer_type (DECL_CONTEXT (decl));
2238 else
2239 /* A destroying operator delete shall be a class member function named
2240 operator delete. */
2241 error_at (loc,
2242 "destroying %<operator delete%> must be a member function");
2243 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl));
2244 if (op->flags & OVL_OP_FLAG_VEC)
2245 error_at (loc, "%<operator delete[]%> cannot be a destroying delete");
2246 if (!usual_deallocation_fn_p (decl))
2247 error_at (loc, "destroying %<operator delete%> must be a usual "
2248 "deallocation function");
2249 }
2250
2251 if (!args || args == void_list_node
2252 || !same_type_p (TREE_VALUE (args), ptrtype))
2253 {
2254 e = 2;
2255 if (args && args != void_list_node)
2256 args = TREE_CHAIN (args);
2257 error_at (loc, "%<operator delete%> takes type %qT as first parameter",
2258 ptrtype);
2259 }
2260 switch (e)
2261 {
2262 case 2:
2263 args = tree_cons (NULL_TREE, ptrtype, args);
2264 /* Fall through. */
2265 case 1:
2266 type = (cxx_copy_lang_qualifiers
2267 (build_function_type (void_type_node, args),
2268 type));
2269 /* Fall through. */
2270 default:;
2271 }
2272
2273 TREE_TYPE (decl) = type;
2274 }
2275
2276 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
2277 and mark them as needed. */
2278
2279 static void
2280 mark_vtable_entries (tree decl, vec<tree> &consteval_vtables)
2281 {
2282 /* It's OK for the vtable to refer to deprecated virtual functions. */
2283 auto du = make_temp_override (deprecated_state,
2284 UNAVAILABLE_DEPRECATED_SUPPRESS);
2285
2286 bool consteval_seen = false;
2287
2288 for (auto &e: CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
2289 {
2290 tree fnaddr = e.value;
2291
2292 STRIP_NOPS (fnaddr);
2293
2294 if (TREE_CODE (fnaddr) != ADDR_EXPR
2295 && TREE_CODE (fnaddr) != FDESC_EXPR)
2296 /* This entry is an offset: a virtual base class offset, a
2297 virtual call offset, an RTTI offset, etc. */
2298 continue;
2299
2300 tree fn = TREE_OPERAND (fnaddr, 0);
2301 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (fn))
2302 {
2303 if (!consteval_seen)
2304 {
2305 consteval_seen = true;
2306 consteval_vtables.safe_push (decl);
2307 }
2308 continue;
2309 }
2310 TREE_ADDRESSABLE (fn) = 1;
2311 /* When we don't have vcall offsets, we output thunks whenever
2312 we output the vtables that contain them. With vcall offsets,
2313 we know all the thunks we'll need when we emit a virtual
2314 function, so we emit the thunks there instead. */
2315 if (DECL_THUNK_P (fn))
2316 use_thunk (fn, /*emit_p=*/0);
2317 /* Set the location, as marking the function could cause
2318 instantiation. We do not need to preserve the incoming
2319 location, as we're called from c_parse_final_cleanups, which
2320 takes care of that. */
2321 input_location = DECL_SOURCE_LOCATION (fn);
2322 mark_used (fn);
2323 }
2324 }
2325
2326 /* Replace any consteval functions in vtables with null pointers. */
2327
2328 static void
2329 clear_consteval_vfns (vec<tree> &consteval_vtables)
2330 {
2331 for (tree vtable : consteval_vtables)
2332 for (constructor_elt &elt : CONSTRUCTOR_ELTS (DECL_INITIAL (vtable)))
2333 {
2334 tree fn = cp_get_fndecl_from_callee (elt.value, /*fold*/false);
2335 if (fn && DECL_IMMEDIATE_FUNCTION_P (fn))
2336 elt.value = build_zero_cst (vtable_entry_type);
2337 }
2338 }
2339
2340 /* Adjust the TLS model on variable DECL if need be, typically after
2341 the linkage of DECL has been modified. */
2342
2343 static void
2344 adjust_var_decl_tls_model (tree decl)
2345 {
2346 if (CP_DECL_THREAD_LOCAL_P (decl)
2347 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
2348 set_decl_tls_model (decl, decl_default_tls_model (decl));
2349 }
2350
2351 /* Set DECL up to have the closest approximation of "initialized common"
2352 linkage available. */
2353
2354 void
2355 comdat_linkage (tree decl)
2356 {
2357 if (flag_weak)
2358 {
2359 make_decl_one_only (decl, cxx_comdat_group (decl));
2360 if (HAVE_COMDAT_GROUP && flag_contracts && DECL_CONTRACTS (decl))
2361 {
2362 symtab_node *n = symtab_node::get (decl);
2363 if (tree pre = DECL_PRE_FN (decl))
2364 cgraph_node::get_create (pre)->add_to_same_comdat_group (n);
2365 if (tree post = DECL_POST_FN (decl))
2366 cgraph_node::get_create (post)->add_to_same_comdat_group (n);
2367 }
2368 }
2369 else if (TREE_CODE (decl) == FUNCTION_DECL
2370 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
2371 /* We can just emit function and compiler-generated variables
2372 statically; having multiple copies is (for the most part) only
2373 a waste of space.
2374
2375 There are two correctness issues, however: the address of a
2376 template instantiation with external linkage should be the
2377 same, independent of what translation unit asks for the
2378 address, and this will not hold when we emit multiple copies of
2379 the function. However, there's little else we can do.
2380
2381 Also, by default, the typeinfo implementation assumes that
2382 there will be only one copy of the string used as the name for
2383 each type. Therefore, if weak symbols are unavailable, the
2384 run-time library should perform a more conservative check; it
2385 should perform a string comparison, rather than an address
2386 comparison. */
2387 TREE_PUBLIC (decl) = 0;
2388 else
2389 {
2390 /* Static data member template instantiations, however, cannot
2391 have multiple copies. */
2392 if (DECL_INITIAL (decl) == 0
2393 || DECL_INITIAL (decl) == error_mark_node)
2394 DECL_COMMON (decl) = 1;
2395 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2396 {
2397 DECL_COMMON (decl) = 1;
2398 DECL_INITIAL (decl) = error_mark_node;
2399 }
2400 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
2401 {
2402 /* We can't do anything useful; leave vars for explicit
2403 instantiation. */
2404 DECL_EXTERNAL (decl) = 1;
2405 DECL_NOT_REALLY_EXTERN (decl) = 0;
2406 }
2407 }
2408
2409 if (TREE_PUBLIC (decl))
2410 DECL_COMDAT (decl) = 1;
2411
2412 if (VAR_P (decl))
2413 adjust_var_decl_tls_model (decl);
2414 }
2415
2416 /* For win32 we also want to put explicit instantiations in
2417 linkonce sections, so that they will be merged with implicit
2418 instantiations; otherwise we get duplicate symbol errors.
2419 For Darwin we do not want explicit instantiations to be
2420 linkonce. */
2421
2422 void
2423 maybe_make_one_only (tree decl)
2424 {
2425 /* We used to say that this was not necessary on targets that support weak
2426 symbols, because the implicit instantiations will defer to the explicit
2427 one. However, that's not actually the case in SVR4; a strong definition
2428 after a weak one is an error. Also, not making explicit
2429 instantiations one_only means that we can end up with two copies of
2430 some template instantiations. */
2431 if (! flag_weak)
2432 return;
2433
2434 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
2435 we can get away with not emitting them if they aren't used. We need
2436 to for variables so that cp_finish_decl will update their linkage,
2437 because their DECL_INITIAL may not have been set properly yet. */
2438
2439 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
2440 || (! DECL_EXPLICIT_INSTANTIATION (decl)
2441 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
2442 {
2443 make_decl_one_only (decl, cxx_comdat_group (decl));
2444
2445 if (VAR_P (decl))
2446 {
2447 varpool_node *node = varpool_node::get_create (decl);
2448 DECL_COMDAT (decl) = 1;
2449 /* Mark it needed so we don't forget to emit it. */
2450 node->forced_by_abi = true;
2451 TREE_USED (decl) = 1;
2452
2453 adjust_var_decl_tls_model (decl);
2454 }
2455 }
2456 }
2457
2458 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
2459 This predicate will give the right answer during parsing of the
2460 function, which other tests may not. */
2461
2462 bool
2463 vague_linkage_p (tree decl)
2464 {
2465 if (!TREE_PUBLIC (decl))
2466 {
2467 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
2468 maybe-in-charge 'tor variants; in that case we need to check one of
2469 the "clones" for the real linkage. But only in that case; before
2470 maybe_clone_body we haven't yet copied the linkage to the clones. */
2471 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
2472 && !DECL_ABSTRACT_P (decl)
2473 && DECL_CHAIN (decl)
2474 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
2475 return vague_linkage_p (DECL_CHAIN (decl));
2476
2477 gcc_checking_assert (!DECL_COMDAT (decl));
2478 return false;
2479 }
2480 /* Unfortunately, import_export_decl has not always been called
2481 before the function is processed, so we cannot simply check
2482 DECL_COMDAT. */
2483 if (DECL_COMDAT (decl)
2484 || (TREE_CODE (decl) == FUNCTION_DECL
2485 && DECL_DECLARED_INLINE_P (decl))
2486 || (DECL_LANG_SPECIFIC (decl)
2487 && DECL_TEMPLATE_INSTANTIATION (decl))
2488 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
2489 return true;
2490 else if (DECL_FUNCTION_SCOPE_P (decl))
2491 /* A local static in an inline effectively has vague linkage. */
2492 return (TREE_STATIC (decl)
2493 && vague_linkage_p (DECL_CONTEXT (decl)));
2494 else
2495 return false;
2496 }
2497
2498 /* Determine whether or not we want to specifically import or export CTYPE,
2499 using various heuristics. */
2500
2501 static void
2502 import_export_class (tree ctype)
2503 {
2504 /* -1 for imported, 1 for exported. */
2505 int import_export = 0;
2506
2507 /* It only makes sense to call this function at EOF. The reason is
2508 that this function looks at whether or not the first non-inline
2509 non-abstract virtual member function has been defined in this
2510 translation unit. But, we can't possibly know that until we've
2511 seen the entire translation unit. */
2512 gcc_assert (at_eof);
2513
2514 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2515 return;
2516
2517 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2518 we will have CLASSTYPE_INTERFACE_ONLY set but not
2519 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2520 heuristic because someone will supply a #pragma implementation
2521 elsewhere, and deducing it here would produce a conflict. */
2522 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2523 return;
2524
2525 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2526 import_export = -1;
2527 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2528 import_export = 1;
2529 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2530 && !flag_implicit_templates)
2531 /* For a template class, without -fimplicit-templates, check the
2532 repository. If the virtual table is assigned to this
2533 translation unit, then export the class; otherwise, import
2534 it. */
2535 import_export = -1;
2536 else if (TYPE_CONTAINS_VPTR_P (ctype))
2537 {
2538 tree cdecl = TYPE_NAME (ctype);
2539 if (DECL_LANG_SPECIFIC (cdecl) && DECL_MODULE_ATTACH_P (cdecl))
2540 /* For class types attached to a named module, the ABI specifies
2541 that the tables are uniquely emitted in the object for the
2542 module unit in which it is defined. */
2543 import_export = (DECL_MODULE_IMPORT_P (cdecl) ? -1 : 1);
2544 else
2545 {
2546 /* The ABI specifies that the virtual table and associated
2547 information are emitted with the key method, if any. */
2548 tree method = CLASSTYPE_KEY_METHOD (ctype);
2549 /* If weak symbol support is not available, then we must be
2550 careful not to emit the vtable when the key function is
2551 inline. An inline function can be defined in multiple
2552 translation units. If we were to emit the vtable in each
2553 translation unit containing a definition, we would get
2554 multiple definition errors at link-time. */
2555 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2556 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2557 }
2558 }
2559
2560 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2561 a definition anywhere else. */
2562 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2563 import_export = 0;
2564
2565 /* Allow back ends the chance to overrule the decision. */
2566 if (targetm.cxx.import_export_class)
2567 import_export = targetm.cxx.import_export_class (ctype, import_export);
2568
2569 if (import_export)
2570 {
2571 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2572 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2573 }
2574 }
2575
2576 /* Return true if VAR has already been provided to the back end; in that
2577 case VAR should not be modified further by the front end. */
2578 static bool
2579 var_finalized_p (tree var)
2580 {
2581 return varpool_node::get_create (var)->definition;
2582 }
2583
2584 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2585 must be emitted in this translation unit. Mark it as such. */
2586
2587 void
2588 mark_needed (tree decl)
2589 {
2590 TREE_USED (decl) = 1;
2591 if (TREE_CODE (decl) == FUNCTION_DECL)
2592 {
2593 /* Extern inline functions don't become needed when referenced.
2594 If we know a method will be emitted in other TU and no new
2595 functions can be marked reachable, just use the external
2596 definition. */
2597 struct cgraph_node *node = cgraph_node::get_create (decl);
2598 node->forced_by_abi = true;
2599
2600 /* #pragma interface can call mark_needed for
2601 maybe-in-charge 'tors; mark the clones as well. */
2602 tree clone;
2603 FOR_EACH_CLONE (clone, decl)
2604 mark_needed (clone);
2605 }
2606 else if (VAR_P (decl))
2607 {
2608 varpool_node *node = varpool_node::get_create (decl);
2609 /* C++ frontend use mark_decl_references to force COMDAT variables
2610 to be output that might appear dead otherwise. */
2611 node->forced_by_abi = true;
2612 }
2613 }
2614
2615 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2616 returns true if a definition of this entity should be provided in
2617 this object file. Callers use this function to determine whether
2618 or not to let the back end know that a definition of DECL is
2619 available in this translation unit. */
2620
2621 bool
2622 decl_needed_p (tree decl)
2623 {
2624 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2625 /* This function should only be called at the end of the translation
2626 unit. We cannot be sure of whether or not something will be
2627 COMDAT until that point. */
2628 gcc_assert (at_eof);
2629
2630 /* All entities with external linkage that are not COMDAT/EXTERN should be
2631 emitted; they may be referred to from other object files. */
2632 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2633 return true;
2634
2635 /* Functions marked "dllexport" must be emitted so that they are
2636 visible to other DLLs. */
2637 if (flag_keep_inline_dllexport
2638 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2639 return true;
2640
2641 /* When not optimizing, do not bother to produce definitions for extern
2642 symbols. */
2643 if (DECL_REALLY_EXTERN (decl)
2644 && ((TREE_CODE (decl) != FUNCTION_DECL
2645 && !optimize)
2646 || (TREE_CODE (decl) == FUNCTION_DECL
2647 && !opt_for_fn (decl, optimize)))
2648 && !lookup_attribute ("always_inline", decl))
2649 return false;
2650
2651 /* If this entity was used, let the back end see it; it will decide
2652 whether or not to emit it into the object file. */
2653 if (TREE_USED (decl))
2654 return true;
2655
2656 /* Virtual functions might be needed for devirtualization. */
2657 if (flag_devirtualize
2658 && TREE_CODE (decl) == FUNCTION_DECL
2659 && DECL_VIRTUAL_P (decl))
2660 return true;
2661
2662 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2663 reference to DECL might cause it to be emitted later. */
2664 return false;
2665 }
2666
2667 /* If necessary, write out the vtables for the dynamic class CTYPE.
2668 Returns true if any vtables were emitted. */
2669
2670 static bool
2671 maybe_emit_vtables (tree ctype, vec<tree> &consteval_vtables)
2672 {
2673 tree vtbl;
2674 tree primary_vtbl;
2675 int needed = 0;
2676 varpool_node *current = NULL, *last = NULL;
2677
2678 /* If the vtables for this class have already been emitted there is
2679 nothing more to do. */
2680 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2681 if (var_finalized_p (primary_vtbl))
2682 return false;
2683 /* Ignore dummy vtables made by get_vtable_decl. */
2684 if (TREE_TYPE (primary_vtbl) == void_type_node)
2685 return false;
2686
2687 /* On some targets, we cannot determine the key method until the end
2688 of the translation unit -- which is when this function is
2689 called. */
2690 if (!targetm.cxx.key_method_may_be_inline ())
2691 determine_key_method (ctype);
2692
2693 /* See if any of the vtables are needed. */
2694 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2695 {
2696 import_export_decl (vtbl);
2697 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2698 needed = 1;
2699 }
2700 if (!needed)
2701 {
2702 /* If the references to this class' vtables are optimized away,
2703 still emit the appropriate debugging information. See
2704 dfs_debug_mark. */
2705 if (DECL_COMDAT (primary_vtbl)
2706 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2707 note_debug_info_needed (ctype);
2708 return false;
2709 }
2710
2711 /* The ABI requires that we emit all of the vtables if we emit any
2712 of them. */
2713 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2714 {
2715 /* Mark entities references from the virtual table as used. */
2716 mark_vtable_entries (vtbl, consteval_vtables);
2717
2718 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2719 {
2720 vec<tree, va_gc> *cleanups = NULL;
2721 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2722 LOOKUP_NORMAL);
2723
2724 /* It had better be all done at compile-time. */
2725 gcc_assert (!expr && !cleanups);
2726 }
2727
2728 /* Write it out. */
2729 DECL_EXTERNAL (vtbl) = 0;
2730 rest_of_decl_compilation (vtbl, 1, 1);
2731
2732 /* Because we're only doing syntax-checking, we'll never end up
2733 actually marking the variable as written. */
2734 if (flag_syntax_only)
2735 TREE_ASM_WRITTEN (vtbl) = 1;
2736 else if (DECL_ONE_ONLY (vtbl))
2737 {
2738 current = varpool_node::get_create (vtbl);
2739 if (last)
2740 current->add_to_same_comdat_group (last);
2741 last = current;
2742 }
2743 }
2744
2745 /* For abstract classes, the destructor has been removed from the
2746 vtable (in class.cc's build_vtbl_initializer). For a compiler-
2747 generated destructor, it hence might not have been generated in
2748 this translation unit - and with '#pragma interface' it might
2749 never get generated. */
2750 if (CLASSTYPE_PURE_VIRTUALS (ctype)
2751 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype)
2752 && !CLASSTYPE_LAZY_DESTRUCTOR (ctype)
2753 && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype)))
2754 note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype));
2755
2756 /* Since we're writing out the vtable here, also write the debug
2757 info. */
2758 note_debug_info_needed (ctype);
2759
2760 return true;
2761 }
2762
2763 /* A special return value from type_visibility meaning internal
2764 linkage. */
2765
2766 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2767
2768 static int expr_visibility (tree);
2769 static int type_visibility (tree);
2770
2771 /* walk_tree helper function for type_visibility. */
2772
2773 static tree
2774 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2775 {
2776 int *vis_p = (int *)data;
2777 int this_vis = VISIBILITY_DEFAULT;
2778 if (! TYPE_P (*tp))
2779 *walk_subtrees = 0;
2780 else if (OVERLOAD_TYPE_P (*tp)
2781 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2782 {
2783 this_vis = VISIBILITY_ANON;
2784 *walk_subtrees = 0;
2785 }
2786 else if (CLASS_TYPE_P (*tp))
2787 {
2788 this_vis = CLASSTYPE_VISIBILITY (*tp);
2789 *walk_subtrees = 0;
2790 }
2791 else if (TREE_CODE (*tp) == ARRAY_TYPE
2792 && uses_template_parms (TYPE_DOMAIN (*tp)))
2793 this_vis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp)));
2794
2795 if (this_vis > *vis_p)
2796 *vis_p = this_vis;
2797
2798 /* Tell cp_walk_subtrees to look through typedefs. */
2799 if (*walk_subtrees == 1)
2800 *walk_subtrees = 2;
2801
2802 return NULL;
2803 }
2804
2805 /* walk_tree helper function for expr_visibility. */
2806
2807 static tree
2808 min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
2809 {
2810 int *vis_p = (int *)data;
2811 int tpvis = VISIBILITY_DEFAULT;
2812
2813 tree t = *tp;
2814 if (TREE_CODE (t) == PTRMEM_CST)
2815 t = PTRMEM_CST_MEMBER (t);
2816
2817 if (TREE_CODE (t) == TEMPLATE_DECL)
2818 {
2819 if (DECL_ALIAS_TEMPLATE_P (t) || concept_definition_p (t))
2820 /* FIXME: We don't maintain TREE_PUBLIC / DECL_VISIBILITY for
2821 alias templates so we can't trust it here (PR107906). Ditto
2822 for concepts. */
2823 return NULL_TREE;
2824 t = DECL_TEMPLATE_RESULT (t);
2825 if (!t)
2826 return NULL_TREE;
2827 }
2828
2829 switch (TREE_CODE (t))
2830 {
2831 case CAST_EXPR:
2832 case IMPLICIT_CONV_EXPR:
2833 case STATIC_CAST_EXPR:
2834 case REINTERPRET_CAST_EXPR:
2835 case CONST_CAST_EXPR:
2836 case DYNAMIC_CAST_EXPR:
2837 case NEW_EXPR:
2838 case CONSTRUCTOR:
2839 case LAMBDA_EXPR:
2840 case TYPE_DECL:
2841 tpvis = type_visibility (TREE_TYPE (t));
2842 break;
2843
2844 case ADDR_EXPR:
2845 t = TREE_OPERAND (t, 0);
2846 if (VAR_P (t))
2847 /* If a variable has its address taken, the lvalue-rvalue conversion is
2848 not applied, so skip that case. */
2849 goto addressable;
2850 break;
2851
2852 case VAR_DECL:
2853 case FUNCTION_DECL:
2854 if (decl_constant_var_p (t))
2855 /* The ODR allows definitions in different TUs to refer to distinct
2856 constant variables with internal or no linkage, so such a reference
2857 shouldn't affect visibility if the lvalue-rvalue conversion is
2858 applied (PR110323). We still want to restrict visibility according
2859 to the type of the declaration however. */
2860 {
2861 tpvis = type_visibility (TREE_TYPE (t));
2862 break;
2863 }
2864 addressable:
2865 if (decl_linkage (t) == lk_none)
2866 tpvis = type_visibility (TREE_TYPE (t));
2867 /* Decls that have had their visibility constrained will report
2868 as external linkage, but we still want to transitively constrain
2869 if we refer to them, so just check TREE_PUBLIC instead. */
2870 else if (!TREE_PUBLIC (t))
2871 tpvis = VISIBILITY_ANON;
2872 else
2873 tpvis = DECL_VISIBILITY (t);
2874 break;
2875
2876 case FIELD_DECL:
2877 tpvis = type_visibility (DECL_CONTEXT (t));
2878 break;
2879
2880 default:
2881 break;
2882 }
2883
2884 if (tpvis > *vis_p)
2885 *vis_p = tpvis;
2886
2887 return NULL_TREE;
2888 }
2889
2890 /* Returns the visibility of TYPE, which is the minimum visibility of its
2891 component types. */
2892
2893 static int
2894 type_visibility (tree type)
2895 {
2896 int vis = VISIBILITY_DEFAULT;
2897 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2898 return vis;
2899 }
2900
2901 /* Returns the visibility of an expression EXPR that appears in the signature
2902 of a function template, which is the minimum visibility of names that appear
2903 in its mangling. */
2904
2905 static int
2906 expr_visibility (tree expr)
2907 {
2908 int vis = VISIBILITY_DEFAULT;
2909 cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis);
2910 return vis;
2911 }
2912
2913 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2914 specified (or if VISIBILITY is static). If TMPL is true, this
2915 constraint is for a template argument, and takes precedence
2916 over explicitly-specified visibility on the template. */
2917
2918 static void
2919 constrain_visibility (tree decl, int visibility, bool tmpl)
2920 {
2921 if (visibility == VISIBILITY_ANON)
2922 {
2923 /* extern "C" declarations aren't affected by the anonymous
2924 namespace. */
2925 if (!DECL_EXTERN_C_P (decl))
2926 {
2927 TREE_PUBLIC (decl) = 0;
2928 DECL_WEAK (decl) = 0;
2929 DECL_COMMON (decl) = 0;
2930 DECL_COMDAT (decl) = false;
2931 if (VAR_OR_FUNCTION_DECL_P (decl))
2932 {
2933 struct symtab_node *snode = symtab_node::get (decl);
2934
2935 if (snode)
2936 snode->set_comdat_group (NULL);
2937 }
2938 DECL_INTERFACE_KNOWN (decl) = 1;
2939 if (DECL_LANG_SPECIFIC (decl))
2940 DECL_NOT_REALLY_EXTERN (decl) = 1;
2941 }
2942 }
2943 else if (visibility > DECL_VISIBILITY (decl)
2944 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2945 {
2946 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2947 /* This visibility was not specified. */
2948 DECL_VISIBILITY_SPECIFIED (decl) = false;
2949 }
2950 }
2951
2952 /* Constrain the visibility of DECL based on the visibility of its template
2953 arguments. */
2954
2955 static void
2956 constrain_visibility_for_template (tree decl, tree targs)
2957 {
2958 /* If this is a template instantiation, check the innermost
2959 template args for visibility constraints. The outer template
2960 args are covered by the class check. */
2961 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2962 int i;
2963 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2964 {
2965 int vis = 0;
2966
2967 tree arg = TREE_VEC_ELT (args, i-1);
2968 if (TYPE_P (arg))
2969 vis = type_visibility (arg);
2970 else
2971 vis = expr_visibility (arg);
2972 if (vis)
2973 constrain_visibility (decl, vis, true);
2974 }
2975 }
2976
2977 /* Like c_determine_visibility, but with additional C++-specific
2978 behavior.
2979
2980 Function-scope entities can rely on the function's visibility because
2981 it is set in start_preparsed_function.
2982
2983 Class-scope entities cannot rely on the class's visibility until the end
2984 of the enclosing class definition.
2985
2986 Note that because namespaces have multiple independent definitions,
2987 namespace visibility is handled elsewhere using the #pragma visibility
2988 machinery rather than by decorating the namespace declaration.
2989
2990 The goal is for constraints from the type to give a diagnostic, and
2991 other constraints to be applied silently. */
2992
2993 void
2994 determine_visibility (tree decl)
2995 {
2996 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2997
2998 /* Only relevant for names with external linkage. */
2999 if (!TREE_PUBLIC (decl))
3000 return;
3001
3002 /* Cloned constructors and destructors get the same visibility as
3003 the underlying function. That should be set up in
3004 maybe_clone_body. */
3005 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
3006
3007 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
3008 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
3009
3010 /* The decl may be a template instantiation, which could influence
3011 visibilty. */
3012 tree template_decl = NULL_TREE;
3013 if (TREE_CODE (decl) == TYPE_DECL)
3014 {
3015 if (CLASS_TYPE_P (TREE_TYPE (decl)))
3016 {
3017 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
3018 template_decl = decl;
3019 }
3020 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3021 template_decl = decl;
3022 }
3023 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3024 template_decl = decl;
3025
3026 if (TREE_CODE (decl) == TYPE_DECL
3027 && LAMBDA_TYPE_P (TREE_TYPE (decl))
3028 && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl)) != error_mark_node)
3029 if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
3030 {
3031 /* The lambda's visibility is limited by that of its extra
3032 scope. */
3033 int vis = 0;
3034 if (TYPE_P (extra))
3035 vis = type_visibility (extra);
3036 else
3037 vis = expr_visibility (extra);
3038 constrain_visibility (decl, vis, false);
3039 }
3040
3041 /* If DECL is a member of a class, visibility specifiers on the
3042 class can influence the visibility of the DECL. */
3043 tree class_type = NULL_TREE;
3044 if (DECL_CLASS_SCOPE_P (decl))
3045 class_type = DECL_CONTEXT (decl);
3046 else
3047 {
3048 /* Not a class member. */
3049
3050 /* Virtual tables have DECL_CONTEXT set to their associated class,
3051 so they are automatically handled above. */
3052 gcc_assert (!VAR_P (decl)
3053 || !DECL_VTABLE_OR_VTT_P (decl));
3054
3055 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
3056 {
3057 /* Local statics and classes get the visibility of their
3058 containing function by default, except that
3059 -fvisibility-inlines-hidden doesn't affect them. */
3060 tree fn = DECL_CONTEXT (decl);
3061 if (DECL_VISIBILITY_SPECIFIED (fn))
3062 {
3063 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
3064 DECL_VISIBILITY_SPECIFIED (decl) =
3065 DECL_VISIBILITY_SPECIFIED (fn);
3066 }
3067 else
3068 {
3069 if (DECL_CLASS_SCOPE_P (fn))
3070 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
3071 else if (determine_hidden_inline (fn))
3072 {
3073 DECL_VISIBILITY (decl) = default_visibility;
3074 DECL_VISIBILITY_SPECIFIED (decl) =
3075 visibility_options.inpragma;
3076 }
3077 else
3078 {
3079 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
3080 DECL_VISIBILITY_SPECIFIED (decl) =
3081 DECL_VISIBILITY_SPECIFIED (fn);
3082 }
3083 }
3084
3085 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
3086 but have no TEMPLATE_INFO, so don't try to check it. */
3087 template_decl = NULL_TREE;
3088 }
3089 else if (VAR_P (decl) && DECL_TINFO_P (decl)
3090 && flag_visibility_ms_compat)
3091 {
3092 /* Under -fvisibility-ms-compat, types are visible by default,
3093 even though their contents aren't. */
3094 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
3095 int underlying_vis = type_visibility (underlying_type);
3096 if (underlying_vis == VISIBILITY_ANON
3097 || (CLASS_TYPE_P (underlying_type)
3098 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
3099 constrain_visibility (decl, underlying_vis, false);
3100 else
3101 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3102 }
3103 else if (VAR_P (decl) && DECL_TINFO_P (decl))
3104 {
3105 /* tinfo visibility is based on the type it's for. */
3106 constrain_visibility
3107 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
3108
3109 /* Give the target a chance to override the visibility associated
3110 with DECL. */
3111 if (TREE_PUBLIC (decl)
3112 && !DECL_REALLY_EXTERN (decl)
3113 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
3114 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
3115 targetm.cxx.determine_class_data_visibility (decl);
3116 }
3117 else if (template_decl)
3118 /* Template instantiations and specializations get visibility based
3119 on their template unless they override it with an attribute. */;
3120 else if (! DECL_VISIBILITY_SPECIFIED (decl))
3121 {
3122 if (determine_hidden_inline (decl))
3123 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3124 else
3125 {
3126 /* Set default visibility to whatever the user supplied with
3127 #pragma GCC visibility or a namespace visibility attribute. */
3128 DECL_VISIBILITY (decl) = default_visibility;
3129 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
3130 }
3131 }
3132 }
3133
3134 if (template_decl)
3135 {
3136 /* If the specialization doesn't specify visibility, use the
3137 visibility from the template. */
3138 tree tinfo = get_template_info (template_decl);
3139 tree args = TI_ARGS (tinfo);
3140 tree attribs = (TREE_CODE (decl) == TYPE_DECL
3141 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
3142 : DECL_ATTRIBUTES (decl));
3143 tree attr = lookup_attribute ("visibility", attribs);
3144
3145 if (args != error_mark_node)
3146 {
3147 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
3148
3149 if (!DECL_VISIBILITY_SPECIFIED (decl))
3150 {
3151 if (!attr
3152 && determine_hidden_inline (decl))
3153 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3154 else
3155 {
3156 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
3157 DECL_VISIBILITY_SPECIFIED (decl)
3158 = DECL_VISIBILITY_SPECIFIED (pattern);
3159 }
3160 }
3161
3162 if (args
3163 /* Template argument visibility outweighs #pragma or namespace
3164 visibility, but not an explicit attribute. */
3165 && !attr)
3166 {
3167 int depth = TMPL_ARGS_DEPTH (args);
3168 if (DECL_VISIBILITY_SPECIFIED (decl))
3169 {
3170 /* A class template member with explicit visibility
3171 overrides the class visibility, so we need to apply
3172 all the levels of template args directly. */
3173 int i;
3174 for (i = 1; i <= depth; ++i)
3175 {
3176 tree lev = TMPL_ARGS_LEVEL (args, i);
3177 constrain_visibility_for_template (decl, lev);
3178 }
3179 }
3180 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
3181 /* Limit visibility based on its template arguments. */
3182 constrain_visibility_for_template (decl, args);
3183 }
3184 }
3185 }
3186
3187 if (class_type)
3188 determine_visibility_from_class (decl, class_type);
3189
3190 if (decl_internal_context_p (decl))
3191 /* Names in an anonymous namespace get internal linkage. */
3192 constrain_visibility (decl, VISIBILITY_ANON, false);
3193 else if (TREE_CODE (decl) != TYPE_DECL)
3194 {
3195 /* Propagate anonymity from type to decl. */
3196 int tvis = type_visibility (TREE_TYPE (decl));
3197 if (tvis == VISIBILITY_ANON
3198 || ! DECL_VISIBILITY_SPECIFIED (decl))
3199 constrain_visibility (decl, tvis, false);
3200 }
3201 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
3202 /* DR 757: A type without linkage shall not be used as the type of a
3203 variable or function with linkage, unless
3204 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
3205 o the variable or function is not used (3.2 [basic.def.odr]) or is
3206 defined in the same translation unit.
3207
3208 Since non-extern "C" decls need to be defined in the same
3209 translation unit, we can make the type internal. */
3210 constrain_visibility (decl, VISIBILITY_ANON, false);
3211
3212 /* If visibility changed and DECL already has DECL_RTL, ensure
3213 symbol flags are updated. */
3214 if ((DECL_VISIBILITY (decl) != orig_visibility
3215 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
3216 && ((VAR_P (decl) && TREE_STATIC (decl))
3217 || TREE_CODE (decl) == FUNCTION_DECL)
3218 && DECL_RTL_SET_P (decl))
3219 make_decl_rtl (decl);
3220 }
3221
3222 /* By default, static data members and function members receive
3223 the visibility of their containing class. */
3224
3225 static void
3226 determine_visibility_from_class (tree decl, tree class_type)
3227 {
3228 if (DECL_VISIBILITY_SPECIFIED (decl))
3229 return;
3230
3231 if (determine_hidden_inline (decl))
3232 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3233 else
3234 {
3235 /* Default to the class visibility. */
3236 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
3237 DECL_VISIBILITY_SPECIFIED (decl)
3238 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
3239 }
3240
3241 /* Give the target a chance to override the visibility associated
3242 with DECL. */
3243 if (VAR_P (decl)
3244 && TREE_PUBLIC (decl)
3245 && (DECL_TINFO_P (decl) || DECL_VTABLE_OR_VTT_P (decl))
3246 && !DECL_REALLY_EXTERN (decl)
3247 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
3248 targetm.cxx.determine_class_data_visibility (decl);
3249 }
3250
3251 /* Returns true iff DECL is an inline that should get hidden visibility
3252 because of -fvisibility-inlines-hidden. */
3253
3254 static bool
3255 determine_hidden_inline (tree decl)
3256 {
3257 return (visibility_options.inlines_hidden
3258 /* Don't do this for inline templates; specializations might not be
3259 inline, and we don't want them to inherit the hidden
3260 visibility. We'll set it here for all inline instantiations. */
3261 && !processing_template_decl
3262 && TREE_CODE (decl) == FUNCTION_DECL
3263 && DECL_DECLARED_INLINE_P (decl)
3264 && (! DECL_LANG_SPECIFIC (decl)
3265 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
3266 }
3267
3268 /* Constrain the visibility of a class TYPE based on the visibility of its
3269 field types. Warn if any fields require lesser visibility. */
3270
3271 void
3272 constrain_class_visibility (tree type)
3273 {
3274 tree binfo;
3275 tree t;
3276 int i;
3277
3278 int vis = type_visibility (type);
3279
3280 if (vis == VISIBILITY_ANON
3281 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
3282 return;
3283
3284 /* Don't warn about visibility if the class has explicit visibility. */
3285 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
3286 vis = VISIBILITY_INTERNAL;
3287
3288 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
3289 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
3290 && !DECL_ARTIFICIAL (t))
3291 {
3292 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
3293 int subvis = type_visibility (ftype);
3294
3295 if (subvis == VISIBILITY_ANON)
3296 {
3297 if (!in_main_input_context())
3298 {
3299 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
3300 if (nlt)
3301 {
3302 if (same_type_p (TREE_TYPE (t), nlt))
3303 warning (OPT_Wsubobject_linkage, "\
3304 %qT has a field %q#D whose type has no linkage",
3305 type, t);
3306 else
3307 warning (OPT_Wsubobject_linkage, "\
3308 %qT has a field %qD whose type depends on the type %qT which has no linkage",
3309 type, t, nlt);
3310 }
3311 else if (cxx_dialect > cxx98
3312 && !decl_anon_ns_mem_p (ftype))
3313 warning (OPT_Wsubobject_linkage, "\
3314 %qT has a field %q#D whose type has internal linkage",
3315 type, t);
3316 else // In C++98 this can only happen with unnamed namespaces.
3317 warning (OPT_Wsubobject_linkage, "\
3318 %qT has a field %q#D whose type uses the anonymous namespace",
3319 type, t);
3320 }
3321 }
3322 else if (MAYBE_CLASS_TYPE_P (ftype)
3323 && vis < VISIBILITY_HIDDEN
3324 && subvis >= VISIBILITY_HIDDEN)
3325 warning (OPT_Wattributes, "\
3326 %qT declared with greater visibility than the type of its field %qD",
3327 type, t);
3328 }
3329
3330 binfo = TYPE_BINFO (type);
3331 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
3332 {
3333 tree btype = BINFO_TYPE (t);
3334 int subvis = type_visibility (btype);
3335
3336 if (subvis == VISIBILITY_ANON)
3337 {
3338 if (!in_main_input_context())
3339 {
3340 tree nlt = no_linkage_check (btype, /*relaxed_p=*/false);
3341 if (nlt)
3342 {
3343 if (same_type_p (btype, nlt))
3344 warning (OPT_Wsubobject_linkage, "\
3345 %qT has a base %qT which has no linkage",
3346 type, btype);
3347 else
3348 warning (OPT_Wsubobject_linkage, "\
3349 %qT has a base %qT which depends on the type %qT which has no linkage",
3350 type, btype, nlt);
3351 }
3352 else if (cxx_dialect > cxx98
3353 && !decl_anon_ns_mem_p (btype))
3354 warning (OPT_Wsubobject_linkage, "\
3355 %qT has a base %qT which has internal linkage",
3356 type, btype);
3357 else // In C++98 this can only happen with unnamed namespaces.
3358 warning (OPT_Wsubobject_linkage, "\
3359 %qT has a base %qT which uses the anonymous namespace",
3360 type, btype);
3361 }
3362 }
3363 else if (vis < VISIBILITY_HIDDEN
3364 && subvis >= VISIBILITY_HIDDEN)
3365 warning (OPT_Wattributes, "\
3366 %qT declared with greater visibility than its base %qT",
3367 type, TREE_TYPE (t));
3368 }
3369 }
3370
3371 /* Functions for adjusting the visibility of a tagged type and its nested
3372 types and declarations when it gets a name for linkage purposes from a
3373 typedef. */
3374 // FIXME: It is now a DR for such a class type to contain anything
3375 // other than C. So at minium most of this can probably be deleted.
3376
3377 /* First reset the visibility of all the types. */
3378
3379 static void
3380 reset_type_linkage_1 (tree type)
3381 {
3382 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
3383 if (CLASS_TYPE_P (type))
3384 for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
3385 if (DECL_IMPLICIT_TYPEDEF_P (member))
3386 reset_type_linkage_1 (TREE_TYPE (member));
3387 }
3388
3389 /* Then reset the visibility of any static data members or member
3390 functions that use those types. */
3391
3392 static void
3393 reset_decl_linkage (tree decl)
3394 {
3395 if (TREE_PUBLIC (decl))
3396 return;
3397 if (DECL_CLONED_FUNCTION_P (decl))
3398 return;
3399 TREE_PUBLIC (decl) = true;
3400 DECL_INTERFACE_KNOWN (decl) = false;
3401 determine_visibility (decl);
3402 tentative_decl_linkage (decl);
3403 }
3404
3405 void
3406 reset_type_linkage (tree type)
3407 {
3408 reset_type_linkage_1 (type);
3409 if (CLASS_TYPE_P (type))
3410 {
3411 if (tree vt = CLASSTYPE_VTABLES (type))
3412 {
3413 tree name = mangle_vtbl_for_type (type);
3414 DECL_NAME (vt) = name;
3415 SET_DECL_ASSEMBLER_NAME (vt, name);
3416 reset_decl_linkage (vt);
3417 }
3418 if (!ANON_AGGR_TYPE_P (type))
3419 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
3420 {
3421 tree name = mangle_typeinfo_for_type (type);
3422 DECL_NAME (ti) = name;
3423 SET_DECL_ASSEMBLER_NAME (ti, name);
3424 TREE_TYPE (name) = type;
3425 reset_decl_linkage (ti);
3426 }
3427 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
3428 {
3429 tree mem = STRIP_TEMPLATE (m);
3430 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
3431 reset_decl_linkage (mem);
3432 else if (DECL_IMPLICIT_TYPEDEF_P (mem))
3433 reset_type_linkage (TREE_TYPE (mem));
3434 }
3435 }
3436 }
3437
3438 /* Set up our initial idea of what the linkage of DECL should be. */
3439
3440 void
3441 tentative_decl_linkage (tree decl)
3442 {
3443 if (DECL_INTERFACE_KNOWN (decl))
3444 /* We've already made a decision as to how this function will
3445 be handled. */;
3446 else if (vague_linkage_p (decl))
3447 {
3448 if (TREE_CODE (decl) == FUNCTION_DECL
3449 && decl_defined_p (decl))
3450 {
3451 DECL_EXTERNAL (decl) = 1;
3452 DECL_NOT_REALLY_EXTERN (decl) = 1;
3453 note_vague_linkage_fn (decl);
3454 /* A non-template inline function with external linkage will
3455 always be COMDAT. As we must eventually determine the
3456 linkage of all functions, and as that causes writes to
3457 the data mapped in from the PCH file, it's advantageous
3458 to mark the functions at this point. */
3459 if (DECL_DECLARED_INLINE_P (decl))
3460 {
3461 if (!DECL_IMPLICIT_INSTANTIATION (decl)
3462 || DECL_DEFAULTED_FN (decl))
3463 {
3464 /* This function must have external linkage, as
3465 otherwise DECL_INTERFACE_KNOWN would have been
3466 set. */
3467 gcc_assert (TREE_PUBLIC (decl));
3468 comdat_linkage (decl);
3469 DECL_INTERFACE_KNOWN (decl) = 1;
3470 }
3471 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
3472 /* For implicit instantiations of cdtors try to make
3473 it comdat, so that maybe_clone_body can use aliases.
3474 See PR113208. */
3475 maybe_make_one_only (decl);
3476 }
3477 }
3478 else if (VAR_P (decl))
3479 maybe_commonize_var (decl);
3480 }
3481 }
3482
3483 /* For a polymorphic class type CTYPE, whether its vtables are emitted in a
3484 unique object as per the ABI. */
3485
3486 static bool
3487 vtables_uniquely_emitted (tree ctype)
3488 {
3489 /* If the class is templated, the tables are emitted in every object that
3490 references any of them. */
3491 if (CLASSTYPE_USE_TEMPLATE (ctype))
3492 return false;
3493
3494 /* Otherwise, if the class is attached to a module, the tables are uniquely
3495 emitted in the object for the module unit in which it is defined. */
3496 tree cdecl = TYPE_NAME (ctype);
3497 if (DECL_LANG_SPECIFIC (cdecl) && DECL_MODULE_ATTACH_P (cdecl))
3498 return true;
3499
3500 /* Otherwise, if the class has a key function, the tables are emitted in the
3501 object for the TU containing the definition of the key function. This is
3502 unique if the key function is not inline. */
3503 tree key_method = CLASSTYPE_KEY_METHOD (ctype);
3504 if (key_method && !DECL_DECLARED_INLINE_P (key_method))
3505 return true;
3506
3507 /* Otherwise, the tables are emitted in every object that references
3508 any of them. */
3509 return false;
3510 }
3511
3512 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
3513 for DECL has not already been determined, do so now by setting
3514 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
3515 function is called entities with vague linkage whose definitions
3516 are available must have TREE_PUBLIC set.
3517
3518 If this function decides to place DECL in COMDAT, it will set
3519 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
3520 the caller to decide whether or not to clear DECL_EXTERNAL. Some
3521 callers defer that decision until it is clear that DECL is actually
3522 required. */
3523
3524 void
3525 import_export_decl (tree decl)
3526 {
3527 bool comdat_p;
3528 bool import_p;
3529 tree class_type = NULL_TREE;
3530
3531 if (DECL_INTERFACE_KNOWN (decl))
3532 return;
3533
3534 /* We cannot determine what linkage to give to an entity with vague
3535 linkage until the end of the file. For example, a virtual table
3536 for a class will be defined if and only if the key method is
3537 defined in this translation unit. */
3538 gcc_assert (at_eof);
3539 /* Object file linkage for explicit instantiations is handled in
3540 mark_decl_instantiated. For static variables in functions with
3541 vague linkage, maybe_commonize_var is used.
3542
3543 Therefore, the only declarations that should be provided to this
3544 function are those with external linkage that are:
3545
3546 * implicit instantiations of function templates
3547
3548 * inline functions
3549
3550 * inline variables
3551
3552 * implicit instantiations of static data members of class
3553 templates
3554
3555 * virtual tables
3556
3557 * typeinfo objects
3558
3559 Furthermore, all entities that reach this point must have a
3560 definition available in this translation unit.
3561
3562 The following assertions check these conditions. */
3563 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3564 /* Any code that creates entities with TREE_PUBLIC cleared should
3565 also set DECL_INTERFACE_KNOWN. */
3566 gcc_assert (TREE_PUBLIC (decl));
3567 if (TREE_CODE (decl) == FUNCTION_DECL)
3568 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3569 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
3570 || DECL_DECLARED_INLINE_P (decl));
3571 else
3572 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3573 || DECL_INLINE_VAR_P (decl)
3574 || DECL_VTABLE_OR_VTT_P (decl)
3575 || DECL_TINFO_P (decl));
3576 /* Check that a definition of DECL is available in this translation
3577 unit. */
3578 gcc_assert (!DECL_REALLY_EXTERN (decl));
3579
3580 /* Assume that DECL will not have COMDAT linkage. */
3581 comdat_p = false;
3582 /* Assume that DECL will not be imported into this translation
3583 unit. */
3584 import_p = false;
3585
3586 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
3587 {
3588 class_type = DECL_CONTEXT (decl);
3589 import_export_class (class_type);
3590 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
3591 && CLASSTYPE_INTERFACE_ONLY (class_type))
3592 import_p = true;
3593 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
3594 && !vtables_uniquely_emitted (class_type))
3595 /* The ABI historically required that all virtual tables be
3596 emitted with COMDAT linkage. However, on systems where
3597 COMDAT symbols don't show up in the table of contents for
3598 a static archive, or on systems without weak symbols (where
3599 we approximate COMDAT linkage by using internal linkage),
3600 the linker will report errors about undefined symbols because
3601 it will not see the virtual table definition. Therefore,
3602 in the case that we know that the virtual table will be
3603 emitted in only one translation unit, we make the virtual
3604 table an ordinary definition with external linkage. */
3605 DECL_EXTERNAL (decl) = 0;
3606 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
3607 {
3608 /* CLASS_TYPE is being exported from this translation unit,
3609 so DECL should be defined here. */
3610 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
3611 /* If a class is declared in a header with the "extern
3612 template" extension, then it will not be instantiated,
3613 even in translation units that would normally require
3614 it. Often such classes are explicitly instantiated in
3615 one translation unit. Therefore, the explicit
3616 instantiation must be made visible to other translation
3617 units. */
3618 DECL_EXTERNAL (decl) = 0;
3619 else
3620 {
3621 /* The generic C++ ABI used to say that class data is always
3622 COMDAT, even if emitted in a unique object. This is no
3623 longer true, but for now we continue to do so for
3624 compatibility with programs that are not strictly valid.
3625 However, some variants (e.g., the ARM EABI) explicitly say
3626 that class data only has COMDAT linkage if the class data
3627 might be emitted in more than one translation unit.
3628 When the key method can be inline and is inline, we still
3629 have to arrange for comdat even though
3630 class_data_always_comdat is false. */
3631 if (!vtables_uniquely_emitted (class_type)
3632 || targetm.cxx.class_data_always_comdat ())
3633 {
3634 /* The ABI requires COMDAT linkage. Normally, we
3635 only emit COMDAT things when they are needed;
3636 make sure that we realize that this entity is
3637 indeed needed. */
3638 comdat_p = true;
3639 mark_needed (decl);
3640 }
3641 }
3642 }
3643 else if (!flag_implicit_templates
3644 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
3645 import_p = true;
3646 else
3647 comdat_p = true;
3648 }
3649 else if (VAR_P (decl) && DECL_TINFO_P (decl))
3650 {
3651 tree type = TREE_TYPE (DECL_NAME (decl));
3652 if (CLASS_TYPE_P (type))
3653 {
3654 class_type = type;
3655 import_export_class (type);
3656 if (CLASSTYPE_INTERFACE_KNOWN (type)
3657 && TYPE_CONTAINS_VPTR_P (type)
3658 && CLASSTYPE_INTERFACE_ONLY (type)
3659 /* If -fno-rtti was specified, then we cannot be sure
3660 that RTTI information will be emitted with the
3661 virtual table of the class, so we must emit it
3662 wherever it is used. */
3663 && flag_rtti)
3664 import_p = true;
3665 else
3666 {
3667 if (CLASSTYPE_INTERFACE_KNOWN (type)
3668 && !CLASSTYPE_INTERFACE_ONLY (type))
3669 {
3670 comdat_p = (targetm.cxx.class_data_always_comdat ()
3671 || !vtables_uniquely_emitted (class_type));
3672 mark_needed (decl);
3673 if (!flag_weak)
3674 {
3675 comdat_p = false;
3676 DECL_EXTERNAL (decl) = 0;
3677 }
3678 }
3679 else
3680 comdat_p = true;
3681 }
3682 }
3683 else
3684 comdat_p = true;
3685 }
3686 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3687 {
3688 /* DECL is an implicit instantiation of a function or static
3689 data member. */
3690 if (flag_implicit_templates
3691 || (flag_implicit_inline_templates
3692 && TREE_CODE (decl) == FUNCTION_DECL
3693 && DECL_DECLARED_INLINE_P (decl)))
3694 comdat_p = true;
3695 else
3696 /* If we are not implicitly generating templates, then mark
3697 this entity as undefined in this translation unit. */
3698 import_p = true;
3699 }
3700 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
3701 {
3702 if (!DECL_DECLARED_INLINE_P (decl))
3703 {
3704 tree ctype = DECL_CONTEXT (decl);
3705 import_export_class (ctype);
3706 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3707 {
3708 DECL_NOT_REALLY_EXTERN (decl)
3709 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3710 || (DECL_DECLARED_INLINE_P (decl)
3711 && ! flag_implement_inlines
3712 && !DECL_VINDEX (decl)));
3713
3714 if (!DECL_NOT_REALLY_EXTERN (decl))
3715 DECL_EXTERNAL (decl) = 1;
3716
3717 /* Always make artificials weak. */
3718 if (DECL_ARTIFICIAL (decl) && flag_weak)
3719 comdat_p = true;
3720 else
3721 maybe_make_one_only (decl);
3722 }
3723 }
3724 else
3725 comdat_p = true;
3726 }
3727 else
3728 comdat_p = true;
3729
3730 if (import_p)
3731 {
3732 /* If we are importing DECL into this translation unit, mark is
3733 an undefined here. */
3734 DECL_EXTERNAL (decl) = 1;
3735 DECL_NOT_REALLY_EXTERN (decl) = 0;
3736 }
3737 else if (comdat_p)
3738 {
3739 /* If we decided to put DECL in COMDAT, mark it accordingly at
3740 this point. */
3741 comdat_linkage (decl);
3742 }
3743
3744 DECL_INTERFACE_KNOWN (decl) = 1;
3745 }
3746
3747 /* Return an expression that performs the destruction of DECL, which
3748 must be a VAR_DECL whose type has a non-trivial destructor, or is
3749 an array whose (innermost) elements have a non-trivial destructor. */
3750
3751 tree
3752 build_cleanup (tree decl)
3753 {
3754 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3755 gcc_assert (clean != NULL_TREE);
3756 return clean;
3757 }
3758
3759 /* GUARD is a helper variable for DECL; make them have the same linkage and
3760 visibility. */
3761
3762 void
3763 copy_linkage (tree guard, tree decl)
3764 {
3765 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3766 TREE_STATIC (guard) = TREE_STATIC (decl);
3767 DECL_COMMON (guard) = DECL_COMMON (decl);
3768 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3769 if (TREE_STATIC (guard))
3770 {
3771 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3772 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3773 if (DECL_ONE_ONLY (decl))
3774 make_decl_one_only (guard, cxx_comdat_group (guard));
3775 if (TREE_PUBLIC (decl))
3776 DECL_WEAK (guard) = DECL_WEAK (decl);
3777 /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not
3778 be set until import_export_decl at EOF. */
3779 if (vague_linkage_p (decl))
3780 comdat_linkage (guard);
3781 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3782 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3783 if (!TREE_PUBLIC (decl))
3784 {
3785 gcc_checking_assert (DECL_INTERFACE_KNOWN (decl));
3786 DECL_INTERFACE_KNOWN (guard) = 1;
3787 if (DECL_LANG_SPECIFIC (decl) && DECL_LANG_SPECIFIC (guard))
3788 DECL_NOT_REALLY_EXTERN (guard) = DECL_NOT_REALLY_EXTERN (decl);
3789 }
3790 }
3791 }
3792
3793 /* Returns the initialization guard variable for the variable DECL,
3794 which has static storage duration. */
3795
3796 tree
3797 get_guard (tree decl)
3798 {
3799 tree sname = mangle_guard_variable (decl);
3800 tree guard = get_global_binding (sname);
3801 if (! guard)
3802 {
3803 tree guard_type;
3804
3805 /* We use a type that is big enough to contain a mutex as well
3806 as an integer counter. */
3807 guard_type = targetm.cxx.guard_type ();
3808 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3809 VAR_DECL, sname, guard_type);
3810
3811 /* The guard should have the same linkage as what it guards. */
3812 copy_linkage (guard, decl);
3813
3814 DECL_ARTIFICIAL (guard) = 1;
3815 DECL_IGNORED_P (guard) = 1;
3816 TREE_USED (guard) = 1;
3817 pushdecl_top_level_and_finish (guard, NULL_TREE);
3818 }
3819 return guard;
3820 }
3821
3822 /* Returns true if accessing the GUARD atomic is expensive,
3823 i.e. involves a call to __sync_synchronize or similar.
3824 In this case let __cxa_guard_acquire handle the atomics. */
3825
3826 static bool
3827 is_atomic_expensive_p (machine_mode mode)
3828 {
3829 if (!flag_inline_atomics)
3830 return true;
3831
3832 if (!can_compare_and_swap_p (mode, false) || !can_atomic_load_p (mode))
3833 return true;
3834
3835 return false;
3836 }
3837
3838 /* Return an atomic load of src with the appropriate memory model. */
3839
3840 static tree
3841 build_atomic_load_type (tree src, HOST_WIDE_INT model, tree type)
3842 {
3843 tree ptr_type = build_pointer_type (type);
3844 tree mem_model = build_int_cst (integer_type_node, model);
3845 tree t, addr, val;
3846 unsigned int size;
3847 int fncode;
3848
3849 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
3850
3851 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3852 t = builtin_decl_implicit ((enum built_in_function) fncode);
3853
3854 addr = build1 (ADDR_EXPR, ptr_type, src);
3855 val = build_call_expr (t, 2, addr, mem_model);
3856 return val;
3857 }
3858
3859 /* Return those bits of the GUARD variable that should be set when the
3860 guarded entity is actually initialized. */
3861
3862 static tree
3863 get_guard_bits (tree guard)
3864 {
3865 if (!targetm.cxx.guard_mask_bit ())
3866 {
3867 /* We only set the first byte of the guard, in order to leave room
3868 for a mutex in the high-order bits. */
3869 guard = build1 (ADDR_EXPR,
3870 build_pointer_type (TREE_TYPE (guard)),
3871 guard);
3872 guard = build1 (NOP_EXPR,
3873 build_pointer_type (char_type_node),
3874 guard);
3875 guard = build1 (INDIRECT_REF, char_type_node, guard);
3876 }
3877
3878 return guard;
3879 }
3880
3881 /* Return an expression which determines whether or not the GUARD
3882 variable has already been initialized. */
3883
3884 tree
3885 get_guard_cond (tree guard, bool thread_safe)
3886 {
3887 tree guard_value;
3888
3889 if (!thread_safe)
3890 guard = get_guard_bits (guard);
3891 else
3892 {
3893 tree type = targetm.cxx.guard_mask_bit ()
3894 ? TREE_TYPE (guard) : char_type_node;
3895
3896 if (is_atomic_expensive_p (TYPE_MODE (type)))
3897 guard = integer_zero_node;
3898 else
3899 guard = build_atomic_load_type (guard, MEMMODEL_ACQUIRE, type);
3900 }
3901
3902 /* Mask off all but the low bit. */
3903 if (targetm.cxx.guard_mask_bit ())
3904 {
3905 guard_value = integer_one_node;
3906 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3907 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3908 guard = cp_build_binary_op (input_location,
3909 BIT_AND_EXPR, guard, guard_value,
3910 tf_warning_or_error);
3911 }
3912
3913 guard_value = integer_zero_node;
3914 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3915 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3916 return cp_build_binary_op (input_location,
3917 EQ_EXPR, guard, guard_value,
3918 tf_warning_or_error);
3919 }
3920
3921 /* Return an expression which sets the GUARD variable, indicating that
3922 the variable being guarded has been initialized. */
3923
3924 tree
3925 set_guard (tree guard)
3926 {
3927 tree guard_init;
3928
3929 /* Set the GUARD to one. */
3930 guard = get_guard_bits (guard);
3931 guard_init = integer_one_node;
3932 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3933 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3934 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3935 tf_warning_or_error);
3936 }
3937
3938 /* Returns true iff we can tell that VAR does not have a dynamic
3939 initializer. */
3940
3941 static bool
3942 var_defined_without_dynamic_init (tree var)
3943 {
3944 /* constinit vars are guaranteed to not have dynamic initializer,
3945 but still registering the destructor counts as dynamic initialization. */
3946 if (DECL_DECLARED_CONSTINIT_P (var)
3947 && COMPLETE_TYPE_P (TREE_TYPE (var))
3948 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3949 return true;
3950 /* If it's defined in another TU, we can't tell. */
3951 if (DECL_EXTERNAL (var))
3952 return false;
3953 /* If it has a non-trivial destructor, registering the destructor
3954 counts as dynamic initialization. */
3955 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3956 return false;
3957 /* If it's in this TU, its initializer has been processed, unless
3958 it's a case of self-initialization, then DECL_INITIALIZED_P is
3959 false while the initializer is handled by finish_id_expression. */
3960 if (!DECL_INITIALIZED_P (var))
3961 return false;
3962 /* If it has no initializer or a constant one, it's not dynamic. */
3963 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3964 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3965 }
3966
3967 /* Returns true iff VAR is a variable that needs uses to be
3968 wrapped for possible dynamic initialization. */
3969
3970 bool
3971 var_needs_tls_wrapper (tree var)
3972 {
3973 return (!error_operand_p (var)
3974 && CP_DECL_THREAD_LOCAL_P (var)
3975 && !DECL_GNU_TLS_P (var)
3976 && !DECL_FUNCTION_SCOPE_P (var)
3977 && !var_defined_without_dynamic_init (var));
3978 }
3979
3980 /* Get the FUNCTION_DECL for the shared TLS init function for this
3981 translation unit. */
3982
3983 static tree
3984 get_local_tls_init_fn (location_t loc)
3985 {
3986 tree sname = get_identifier ("__tls_init");
3987 tree fn = get_global_binding (sname);
3988 if (!fn)
3989 {
3990 fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname,
3991 build_function_type (void_type_node,
3992 void_list_node));
3993 SET_DECL_LANGUAGE (fn, lang_c);
3994 TREE_PUBLIC (fn) = false;
3995 DECL_ARTIFICIAL (fn) = true;
3996 mark_used (fn);
3997 set_global_binding (fn);
3998 }
3999 return fn;
4000 }
4001
4002 /* Get a FUNCTION_DECL for the init function for the thread_local
4003 variable VAR. The init function will be an alias to the function
4004 that initializes all the non-local TLS variables in the translation
4005 unit. The init function is only used by the wrapper function. */
4006
4007 static tree
4008 get_tls_init_fn (tree var)
4009 {
4010 /* Only C++11 TLS vars need this init fn. */
4011 if (!var_needs_tls_wrapper (var))
4012 return NULL_TREE;
4013
4014 /* If -fno-extern-tls-init, assume that we don't need to call
4015 a tls init function for a variable defined in another TU. */
4016 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
4017 return NULL_TREE;
4018
4019 /* If the variable is internal, or if we can't generate aliases,
4020 call the local init function directly. */
4021 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
4022 return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var));
4023
4024 tree sname = mangle_tls_init_fn (var);
4025 tree fn = get_global_binding (sname);
4026 if (!fn)
4027 {
4028 fn = build_lang_decl (FUNCTION_DECL, sname,
4029 build_function_type (void_type_node,
4030 void_list_node));
4031 SET_DECL_LANGUAGE (fn, lang_c);
4032 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
4033 DECL_ARTIFICIAL (fn) = true;
4034 DECL_CONTEXT (fn) = FROB_CONTEXT (global_namespace);
4035 DECL_COMDAT (fn) = DECL_COMDAT (var);
4036 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
4037 if (DECL_ONE_ONLY (var))
4038 make_decl_one_only (fn, cxx_comdat_group (fn));
4039 if (TREE_PUBLIC (var))
4040 {
4041 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
4042 /* If the variable is defined somewhere else and might have static
4043 initialization, make the init function a weak reference. */
4044 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
4045 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
4046 || TYPE_HAS_TRIVIAL_DFLT (obtype))
4047 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
4048 && DECL_EXTERNAL (var))
4049 declare_weak (fn);
4050 else
4051 DECL_WEAK (fn) = DECL_WEAK (var);
4052 }
4053 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
4054 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
4055 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
4056 DECL_IGNORED_P (fn) = 1;
4057 mark_used (fn);
4058
4059 DECL_BEFRIENDING_CLASSES (fn) = var;
4060
4061 set_global_binding (fn);
4062 }
4063 return fn;
4064 }
4065
4066 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
4067 variable VAR. The wrapper function calls the init function (if any) for
4068 VAR and then returns a reference to VAR. The wrapper function is used
4069 in place of VAR everywhere VAR is mentioned. */
4070
4071 static tree
4072 get_tls_wrapper_fn (tree var)
4073 {
4074 /* Only C++11 TLS vars need this wrapper fn. */
4075 if (!var_needs_tls_wrapper (var))
4076 return NULL_TREE;
4077
4078 tree sname = mangle_tls_wrapper_fn (var);
4079 tree fn = get_global_binding (sname);
4080 if (!fn)
4081 {
4082 /* A named rvalue reference is an lvalue, so the wrapper should
4083 always return an lvalue reference. */
4084 tree type = non_reference (TREE_TYPE (var));
4085 type = build_reference_type (type);
4086 tree fntype = build_function_type (type, void_list_node);
4087
4088 fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var),
4089 FUNCTION_DECL, sname, fntype);
4090 SET_DECL_LANGUAGE (fn, lang_c);
4091 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
4092 DECL_ARTIFICIAL (fn) = true;
4093 DECL_IGNORED_P (fn) = 1;
4094 DECL_CONTEXT (fn) = FROB_CONTEXT (global_namespace);
4095 /* The wrapper is inline and emitted everywhere var is used. */
4096 DECL_DECLARED_INLINE_P (fn) = true;
4097 if (TREE_PUBLIC (var))
4098 {
4099 comdat_linkage (fn);
4100 #ifdef HAVE_GAS_HIDDEN
4101 /* Make the wrapper bind locally; there's no reason to share
4102 the wrapper between multiple shared objects. */
4103 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
4104 DECL_VISIBILITY_SPECIFIED (fn) = true;
4105 #endif
4106 }
4107 if (!TREE_PUBLIC (fn))
4108 DECL_INTERFACE_KNOWN (fn) = true;
4109 mark_used (fn);
4110 note_vague_linkage_fn (fn);
4111
4112 #if 0
4113 /* We want CSE to commonize calls to the wrapper, but marking it as
4114 pure is unsafe since it has side-effects. I guess we need a new
4115 ECF flag even weaker than ECF_PURE. FIXME! */
4116 DECL_PURE_P (fn) = true;
4117 #endif
4118
4119 DECL_BEFRIENDING_CLASSES (fn) = var;
4120
4121 set_global_binding (fn);
4122 }
4123 return fn;
4124 }
4125
4126 /* If EXPR is a thread_local variable that should be wrapped by init
4127 wrapper function, return a call to that function, otherwise return
4128 NULL. */
4129
4130 tree
4131 maybe_get_tls_wrapper_call (tree expr)
4132 {
4133 if (VAR_P (expr)
4134 && !processing_template_decl
4135 && !cp_unevaluated_operand
4136 && CP_DECL_THREAD_LOCAL_P (expr))
4137 if (tree wrap = get_tls_wrapper_fn (expr))
4138 return build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
4139 return NULL;
4140 }
4141
4142 /* At EOF, generate the definition for the TLS wrapper function FN:
4143
4144 T& var_wrapper() {
4145 if (init_fn) init_fn();
4146 return var;
4147 } */
4148
4149 static void
4150 generate_tls_wrapper (tree fn)
4151 {
4152 tree var = DECL_BEFRIENDING_CLASSES (fn);
4153
4154 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
4155 tree body = begin_function_body ();
4156 /* Only call the init fn if there might be one. */
4157 if (tree init_fn = get_tls_init_fn (var))
4158 {
4159 tree if_stmt = NULL_TREE;
4160 /* If init_fn is a weakref, make sure it exists before calling. */
4161 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
4162 {
4163 if_stmt = begin_if_stmt ();
4164 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
4165 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
4166 NE_EXPR, addr, nullptr_node,
4167 tf_warning_or_error);
4168 finish_if_stmt_cond (cond, if_stmt);
4169 }
4170 finish_expr_stmt (build_cxx_call
4171 (init_fn, 0, NULL, tf_warning_or_error));
4172 if (if_stmt)
4173 {
4174 finish_then_clause (if_stmt);
4175 finish_if_stmt (if_stmt);
4176 }
4177 }
4178 else
4179 /* If there's no initialization, the wrapper is a constant function. */
4180 TREE_READONLY (fn) = true;
4181 finish_return_stmt (convert_from_reference (var));
4182 finish_function_body (body);
4183 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4184 }
4185
4186 /* Start a global constructor or destructor function. */
4187
4188 static tree
4189 start_objects (bool initp, unsigned priority, bool has_body,
4190 bool omp_target = false)
4191 {
4192 bool default_init = initp && priority == DEFAULT_INIT_PRIORITY;
4193 /* FIXME: We may eventually want to treat OpenMP offload initializers
4194 in modules specially as well. */
4195 bool is_module_init = (default_init
4196 && !omp_target
4197 && module_global_init_needed ());
4198 tree name = NULL_TREE;
4199
4200 if (is_module_init)
4201 name = mangle_module_global_init (0);
4202 else
4203 {
4204 char type[14];
4205
4206 /* We use `I' to indicate initialization and `D' to indicate
4207 destruction. */
4208 unsigned len;
4209 if (omp_target)
4210 /* Use "off_" signifying "offload" here. The name must be distinct
4211 from the non-offload case. The format of the name is scanned in
4212 tree.cc/get_file_function_name, so stick to the same length for
4213 both name variants. */
4214 len = sprintf (type, "off_%c", initp ? 'I' : 'D');
4215 else
4216 len = sprintf (type, "sub_%c", initp ? 'I' : 'D');
4217 if (priority != DEFAULT_INIT_PRIORITY)
4218 {
4219 char joiner = '_';
4220 #ifdef JOINER
4221 joiner = JOINER;
4222 #endif
4223 type[len++] = joiner;
4224 sprintf (type + len, "%.5u", priority);
4225 }
4226 name = get_file_function_name (type);
4227 }
4228
4229 tree fntype = build_function_type (void_type_node, void_list_node);
4230 tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
4231
4232 if (omp_target)
4233 {
4234 DECL_ATTRIBUTES (fndecl)
4235 = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
4236 DECL_ATTRIBUTES (fndecl));
4237 DECL_ATTRIBUTES (fndecl)
4238 = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
4239 DECL_ATTRIBUTES (fndecl));
4240 }
4241
4242 DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
4243 if (is_module_init)
4244 {
4245 SET_DECL_ASSEMBLER_NAME (fndecl, name);
4246 TREE_PUBLIC (fndecl) = true;
4247 determine_visibility (fndecl);
4248 }
4249 else
4250 TREE_PUBLIC (fndecl) = 0;
4251 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4252
4253 /* Mark as artificial because it's not explicitly in the user's
4254 source code. */
4255 DECL_ARTIFICIAL (current_function_decl) = 1;
4256
4257 /* Mark this declaration as used to avoid spurious warnings. */
4258 TREE_USED (current_function_decl) = 1;
4259
4260 /* Mark this function as a global constructor or destructor. */
4261 if (initp)
4262 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
4263 else
4264 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
4265
4266 tree body = begin_compound_stmt (BCS_FN_BODY);
4267
4268 if (is_module_init && has_body)
4269 {
4270 // If the function is going to be empty, don't emit idempotency.
4271 // 'static bool __in_chrg = false;
4272 // if (__inchrg) return;
4273 // __inchrg = true
4274 tree var = build_lang_decl (VAR_DECL, in_charge_identifier,
4275 boolean_type_node);
4276 DECL_CONTEXT (var) = fndecl;
4277 DECL_ARTIFICIAL (var) = true;
4278 TREE_STATIC (var) = true;
4279 pushdecl (var);
4280 cp_finish_decl (var, NULL_TREE, false, NULL_TREE, 0);
4281
4282 tree if_stmt = begin_if_stmt ();
4283 finish_if_stmt_cond (var, if_stmt);
4284 finish_return_stmt (NULL_TREE);
4285 finish_then_clause (if_stmt);
4286 finish_if_stmt (if_stmt);
4287
4288 tree assign = build2 (MODIFY_EXPR, boolean_type_node,
4289 var, boolean_true_node);
4290 TREE_SIDE_EFFECTS (assign) = true;
4291 finish_expr_stmt (assign);
4292 }
4293
4294 return body;
4295 }
4296
4297 /* Finish a global constructor or destructor. Add it to the global
4298 ctors or dtors, if STARTP is true. */
4299
4300 static tree
4301 finish_objects (bool initp, unsigned priority, tree body, bool startp)
4302 {
4303 /* Finish up. */
4304 finish_compound_stmt (body);
4305 tree fn = finish_function (/*inline_p=*/false);
4306
4307 if (!startp)
4308 ; // Neither ctor nor dtor I be.
4309 else if (initp)
4310 {
4311 DECL_STATIC_CONSTRUCTOR (fn) = 1;
4312 decl_init_priority_insert (fn, priority);
4313 }
4314 else
4315 {
4316 DECL_STATIC_DESTRUCTOR (fn) = 1;
4317 decl_fini_priority_insert (fn, priority);
4318 }
4319
4320 return fn;
4321 }
4322
4323 /* The name of the function we create to handle initializations and
4324 destructions for objects with static storage duration. */
4325 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
4326 #define OMP_SSDF_IDENTIFIER "__omp_target_static_init_and_destruction"
4327
4328 /* Begins the generation of the function that will handle all
4329 initialization or destruction of objects with static storage
4330 duration at PRIORITY.
4331
4332 It is assumed that this function will be called once for the host, and once
4333 for an OpenMP offload target. */
4334
4335 static tree
4336 start_partial_init_fini_fn (bool initp, unsigned priority, unsigned count,
4337 bool omp_target)
4338 {
4339 char id[MAX (sizeof (SSDF_IDENTIFIER), sizeof (OMP_SSDF_IDENTIFIER))
4340 + 1 /* \0 */ + 32];
4341 tree name;
4342
4343 /* Create the identifier for this function. It will be of the form
4344 SSDF_IDENTIFIER_<number> if not omp_target and otherwise
4345 OMP_SSDF_IDENTIFIER_<number>. */
4346 sprintf (id, "%s_%u", omp_target ? OMP_SSDF_IDENTIFIER : SSDF_IDENTIFIER,
4347 count);
4348 name = get_identifier (id);
4349 tree type = build_function_type (void_type_node, void_list_node);
4350
4351 /* Create the FUNCTION_DECL itself. */
4352 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4353 TREE_PUBLIC (fn) = 0;
4354 DECL_ARTIFICIAL (fn) = 1;
4355
4356 if (omp_target)
4357 {
4358 DECL_ATTRIBUTES (fn)
4359 = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
4360 DECL_ATTRIBUTES (fn));
4361 DECL_ATTRIBUTES (fn)
4362 = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
4363 DECL_ATTRIBUTES (fn));
4364 }
4365
4366 int idx = initp + 2 * omp_target;
4367
4368 /* Put this function in the list of functions to be called from the
4369 static constructors and destructors. */
4370 if (!static_init_fini_fns[idx])
4371 static_init_fini_fns[idx] = priority_map_t::create_ggc ();
4372 auto &slot = static_init_fini_fns[idx]->get_or_insert (priority);
4373 slot = tree_cons (fn, NULL_TREE, slot);
4374
4375 /* Put the function in the global scope. */
4376 pushdecl (fn);
4377
4378 /* Start the function itself. This is equivalent to declaring the
4379 function as:
4380
4381 static void __ssdf (int __initialize_p, init __priority_p);
4382
4383 It is static because we only need to call this function from the
4384 various constructor and destructor functions for this module. */
4385 start_preparsed_function (fn, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4386
4387 /* Set up the scope of the outermost block in the function. */
4388 return begin_compound_stmt (BCS_FN_BODY);
4389 }
4390
4391 /* Finish the generation of the function which performs initialization
4392 or destruction of objects with static storage duration. */
4393
4394 static void
4395 finish_partial_init_fini_fn (tree body)
4396 {
4397 /* Close out the function. */
4398 finish_compound_stmt (body);
4399 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4400 }
4401
4402 /* The effective initialization priority of a DECL. */
4403
4404 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
4405 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
4406 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
4407
4408 /* Whether a DECL needs a guard to protect it against multiple
4409 initialization. */
4410
4411 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
4412 || DECL_ONE_ONLY (decl) \
4413 || DECL_WEAK (decl)))
4414
4415 /* Walks the initializer list of a global variable and looks for
4416 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
4417 and that have their DECL_CONTEXT() == NULL. For each such
4418 temporary variable, set their DECL_CONTEXT() to CTX -- the
4419 initializing function. This is necessary because otherwise some
4420 optimizers (enabled by -O2 -fprofile-arcs) might crash when trying
4421 to refer to a temporary variable that does not have its
4422 DECL_CONTEXT() properly set. */
4423
4424 static tree
4425 fix_temporary_vars_context_r (tree *node,
4426 int * /*unused*/,
4427 void *ctx)
4428 {
4429 if (TREE_CODE (*node) == BIND_EXPR)
4430 for (tree var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
4431 if (VAR_P (var) && !DECL_NAME (var)
4432 && DECL_ARTIFICIAL (var) && !DECL_CONTEXT (var))
4433 DECL_CONTEXT (var) = tree (ctx);
4434
4435 return NULL_TREE;
4436 }
4437
4438 /* Set up to handle the initialization or destruction of DECL. If
4439 INITP is nonzero, we are initializing the variable. Otherwise, we
4440 are destroying it. */
4441
4442 static void
4443 one_static_initialization_or_destruction (bool initp, tree decl, tree init,
4444 bool omp_target)
4445 {
4446 /* If we are supposed to destruct and there's a trivial destructor,
4447 nothing has to be done. */
4448 gcc_checking_assert (init || !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)));
4449
4450 /* Trick the compiler into thinking we are at the file and line
4451 where DECL was declared so that error-messages make sense, and so
4452 that the debugger will show somewhat sensible file and line
4453 information. */
4454 input_location = DECL_SOURCE_LOCATION (decl);
4455
4456 /* Make sure temporary variables in the initialiser all have
4457 their DECL_CONTEXT() set to a value different from NULL_TREE.
4458 This can happen when global variables initializers are built.
4459 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
4460 the temporary variables that might have been generated in the
4461 accompanying initializers is NULL_TREE, meaning the variables have been
4462 declared in the global namespace.
4463 What we want to do here is to fix that and make sure the DECL_CONTEXT()
4464 of the temporaries are set to the current function decl. */
4465 cp_walk_tree_without_duplicates (&init,
4466 fix_temporary_vars_context_r,
4467 current_function_decl);
4468
4469 /* Because of:
4470
4471 [class.access.spec]
4472
4473 Access control for implicit calls to the constructors,
4474 the conversion functions, or the destructor called to
4475 create and destroy a static data member is performed as
4476 if these calls appeared in the scope of the member's
4477 class.
4478
4479 we pretend we are in a static member function of the class of
4480 which the DECL is a member. */
4481 if (member_p (decl))
4482 {
4483 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
4484 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
4485 }
4486
4487 /* Assume we don't need a guard. */
4488 tree guard_if_stmt = NULL_TREE;
4489
4490 /* We need a guard if this is an object with external linkage that
4491 might be initialized in more than one place. (For example, a
4492 static data member of a template, when the data member requires
4493 construction.) */
4494 if (NEEDS_GUARD_P (decl))
4495 {
4496 tree guard = get_guard (decl);
4497 tree guard_cond;
4498
4499 if (flag_use_cxa_atexit)
4500 {
4501 /* When using __cxa_atexit, we just check the GUARD as we
4502 would for a local static. We never try to destroy
4503 anything from a static destructor. */
4504 gcc_assert (initp);
4505 guard_cond = get_guard_cond (guard, false);
4506 }
4507 else
4508 {
4509 /* If we don't have __cxa_atexit, then we will be running
4510 destructors from .fini sections, or their equivalents.
4511 So, we need to know how many times we've tried to
4512 initialize this object. We do initializations only if
4513 the GUARD was or becomes zero (initp vs !initp
4514 respectively). */
4515 guard_cond = cp_build_unary_op (initp ? POSTINCREMENT_EXPR
4516 : PREDECREMENT_EXPR,
4517 guard,
4518 /*noconvert=*/true,
4519 tf_warning_or_error);
4520 guard_cond = cp_build_binary_op (input_location, EQ_EXPR, guard_cond,
4521 integer_zero_node,
4522 tf_warning_or_error);
4523 }
4524
4525 guard_if_stmt = begin_if_stmt ();
4526 finish_if_stmt_cond (guard_cond, guard_if_stmt);
4527
4528 if (flag_use_cxa_atexit)
4529 /* Set the GUARD now. */
4530 finish_expr_stmt (set_guard (guard));
4531 }
4532
4533 /* Perform the initialization or destruction. */
4534 if (initp)
4535 {
4536 if (init)
4537 {
4538 finish_expr_stmt (init);
4539 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
4540 if (varpool_node *vnode = varpool_node::get (decl))
4541 vnode->dynamically_initialized = 1;
4542 }
4543
4544 /* If we're using __cxa_atexit, register a function that calls the
4545 destructor for the object. */
4546 if (flag_use_cxa_atexit)
4547 finish_expr_stmt (register_dtor_fn (decl, omp_target));
4548 }
4549 else
4550 finish_expr_stmt (build_cleanup (decl));
4551
4552 /* Finish the guard if-stmt, if necessary. */
4553 if (guard_if_stmt)
4554 {
4555 finish_then_clause (guard_if_stmt);
4556 finish_if_stmt (guard_if_stmt);
4557 }
4558
4559 /* Now that we're done with DECL we don't need to pretend to be a
4560 member of its class any longer. */
4561 DECL_CONTEXT (current_function_decl) = NULL_TREE;
4562 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
4563 }
4564
4565 /* Helper function for emit_partial_init_fini_fn and handle_tls_init.
4566 For structured bindings, disable stmts_are_full_exprs_p ()
4567 on STATIC_INIT_DECOMP_BASE_P nodes, reenable it on the
4568 first STATIC_INIT_DECOMP_NONBASE_P node and emit all the
4569 STATIC_INIT_DECOMP_BASE_P and STATIC_INIT_DECOMP_NONBASE_P
4570 consecutive nodes in a single STATEMENT_LIST wrapped with
4571 CLEANUP_POINT_EXPR. */
4572
4573 static inline tree
4574 decomp_handle_one_var (tree node, tree sl, bool *saw_nonbase,
4575 int save_stmts_are_full_exprs_p)
4576 {
4577 if (sl && !*saw_nonbase && STATIC_INIT_DECOMP_NONBASE_P (node))
4578 {
4579 *saw_nonbase = true;
4580 current_stmt_tree ()->stmts_are_full_exprs_p
4581 = save_stmts_are_full_exprs_p;
4582 }
4583 else if (sl && *saw_nonbase && !STATIC_INIT_DECOMP_NONBASE_P (node))
4584 {
4585 sl = pop_stmt_list (sl);
4586 sl = maybe_cleanup_point_expr_void (sl);
4587 add_stmt (sl);
4588 sl = NULL_TREE;
4589 }
4590 if (sl == NULL_TREE && STATIC_INIT_DECOMP_BASE_P (node))
4591 {
4592 sl = push_stmt_list ();
4593 *saw_nonbase = false;
4594 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
4595 }
4596 return sl;
4597 }
4598
4599 /* Similarly helper called when the whole var list is processed. */
4600
4601 static inline void
4602 decomp_finalize_var_list (tree sl, int save_stmts_are_full_exprs_p)
4603 {
4604 if (sl)
4605 {
4606 current_stmt_tree ()->stmts_are_full_exprs_p
4607 = save_stmts_are_full_exprs_p;
4608 sl = pop_stmt_list (sl);
4609 sl = maybe_cleanup_point_expr_void (sl);
4610 add_stmt (sl);
4611 }
4612 }
4613
4614 /* Helper for emit_partial_init_fini_fn OpenMP target handling, called via
4615 walk_tree. Set DECL_CONTEXT on any automatic temporaries which still
4616 have it NULL to id->src_fn, so that later copy_tree_body_r can remap those.
4617 Otherwise DECL_CONTEXT would be set only during gimplification of the host
4618 fn and when copy_tree_body_r doesn't remap those, we'd ICE during the
4619 target fn gimplification because the same automatic VAR_DECL can't be
4620 used in multiple functions (with the exception of nested functions). */
4621
4622 static tree
4623 set_context_for_auto_vars_r (tree *tp, int *, void *data)
4624 {
4625 copy_body_data *id = (copy_body_data *) data;
4626 if (auto_var_in_fn_p (*tp, NULL_TREE) && DECL_ARTIFICIAL (*tp))
4627 DECL_CONTEXT (*tp) = id->src_fn;
4628 return NULL_TREE;
4629 }
4630
4631 /* Generate code to do the initialization or destruction of the decls in VARS,
4632 a TREE_LIST of VAR_DECL with static storage duration.
4633 Whether initialization or destruction is performed is specified by INITP. */
4634
4635 static tree
4636 emit_partial_init_fini_fn (bool initp, unsigned priority, tree vars,
4637 unsigned counter, location_t locus, tree host_fn)
4638 {
4639 input_location = locus;
4640 bool omp_target = (host_fn != NULL_TREE);
4641 tree body = start_partial_init_fini_fn (initp, priority, counter, omp_target);
4642 tree fndecl = current_function_decl;
4643
4644 tree nonhost_if_stmt = NULL_TREE;
4645 if (omp_target)
4646 {
4647 nonhost_if_stmt = begin_if_stmt ();
4648 /* We add an "omp declare target nohost" attribute, but (for
4649 now) we still get a copy of the constructor/destructor on
4650 the host. Make sure it does nothing unless we're on the
4651 target device. */
4652 tree fn = builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
4653 tree initial_dev = build_call_expr (fn, 0);
4654 tree target_dev_p
4655 = cp_build_binary_op (input_location, NE_EXPR, initial_dev,
4656 integer_one_node, tf_warning_or_error);
4657 finish_if_stmt_cond (target_dev_p, nonhost_if_stmt);
4658 }
4659
4660 tree sl = NULL_TREE;
4661 int save_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
4662 bool saw_nonbase = false;
4663 for (tree node = vars; node; node = TREE_CHAIN (node))
4664 {
4665 tree decl = TREE_VALUE (node);
4666 tree init = TREE_PURPOSE (node);
4667 sl = decomp_handle_one_var (node, sl, &saw_nonbase,
4668 save_stmts_are_full_exprs_p);
4669 /* We will emit 'init' twice, and it is modified in-place during
4670 gimplification. Make a copy here. */
4671 if (omp_target)
4672 {
4673 /* We've already emitted INIT in the host version of the ctor/dtor
4674 function. We need to deep-copy it (including new versions of
4675 local variables introduced, etc.) for use in the target
4676 ctor/dtor function. */
4677 copy_body_data id;
4678 hash_map<tree, tree> decl_map;
4679 memset (&id, 0, sizeof (id));
4680 id.src_fn = host_fn;
4681 id.dst_fn = current_function_decl;
4682 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
4683 id.decl_map = &decl_map;
4684 id.copy_decl = copy_decl_no_change;
4685 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4686 id.transform_new_cfg = true;
4687 id.transform_return_to_modify = false;
4688 id.eh_lp_nr = 0;
4689 walk_tree (&init, set_context_for_auto_vars_r, &id, NULL);
4690 walk_tree (&init, copy_tree_body_r, &id, NULL);
4691 }
4692 /* Do one initialization or destruction. */
4693 one_static_initialization_or_destruction (initp, decl, init, omp_target);
4694 }
4695 decomp_finalize_var_list (sl, save_stmts_are_full_exprs_p);
4696
4697 if (omp_target)
4698 {
4699 /* Finish up nonhost if-stmt body. */
4700 finish_then_clause (nonhost_if_stmt);
4701 finish_if_stmt (nonhost_if_stmt);
4702 }
4703
4704 /* Finish up the static storage duration function for this
4705 round. */
4706 input_location = locus;
4707 finish_partial_init_fini_fn (body);
4708
4709 return fndecl;
4710 }
4711
4712 /* VARS is a list of variables with static storage duration which may
4713 need initialization and/or finalization. Remove those variables
4714 that don't really need to be initialized or finalized, and return
4715 the resulting list. The order in which the variables appear in
4716 VARS is in reverse order of the order in which they should actually
4717 be initialized. That order is preserved. */
4718
4719 static tree
4720 prune_vars_needing_no_initialization (tree *vars)
4721 {
4722 tree *var = vars;
4723 tree result = NULL_TREE;
4724
4725 while (*var)
4726 {
4727 tree t = *var;
4728 tree decl = TREE_VALUE (t);
4729 tree init = TREE_PURPOSE (t);
4730
4731 /* Deal gracefully with error. */
4732 if (error_operand_p (decl))
4733 {
4734 var = &TREE_CHAIN (t);
4735 continue;
4736 }
4737
4738 /* The only things that can be initialized are variables. */
4739 gcc_assert (VAR_P (decl));
4740
4741 /* If this object is not defined, we don't need to do anything
4742 here. */
4743 if (DECL_EXTERNAL (decl))
4744 {
4745 gcc_checking_assert (!STATIC_INIT_DECOMP_BASE_P (t)
4746 && !STATIC_INIT_DECOMP_NONBASE_P (t));
4747 var = &TREE_CHAIN (t);
4748 continue;
4749 }
4750
4751 /* Also, if the initializer already contains errors, we can bail
4752 out now. */
4753 if (init && TREE_CODE (init) == TREE_LIST
4754 && value_member (error_mark_node, init))
4755 {
4756 var = &TREE_CHAIN (t);
4757 continue;
4758 }
4759
4760 /* This variable is going to need initialization and/or
4761 finalization, so we add it to the list. */
4762 *var = TREE_CHAIN (t);
4763 TREE_CHAIN (t) = result;
4764 result = t;
4765 }
4766
4767 return result;
4768 }
4769
4770 /* Split VAR_LIST by init priority and add into PARTS hash table.
4771 This reverses the variable ordering. */
4772
4773 void
4774 partition_vars_for_init_fini (tree var_list, priority_map_t *(&parts)[4])
4775 {
4776 unsigned priority = 0;
4777 enum { none, base, nonbase } decomp_state = none;
4778 for (auto node = var_list; node; node = TREE_CHAIN (node))
4779 {
4780 tree decl = TREE_VALUE (node);
4781 tree init = TREE_PURPOSE (node);
4782 bool has_cleanup = !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl));
4783 if (decomp_state == base && STATIC_INIT_DECOMP_NONBASE_P (node))
4784 decomp_state = nonbase;
4785 else if (decomp_state == nonbase && !STATIC_INIT_DECOMP_NONBASE_P (node))
4786 decomp_state = none;
4787 if (decomp_state == none)
4788 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
4789
4790 if (init || (flag_use_cxa_atexit && has_cleanup))
4791 {
4792 // Add to initialization list.
4793 if (!parts[true])
4794 parts[true] = priority_map_t::create_ggc ();
4795 auto &slot = parts[true]->get_or_insert (priority);
4796 slot = tree_cons (init, decl, slot);
4797 if (init
4798 && STATIC_INIT_DECOMP_BASE_P (node)
4799 && decomp_state == none)
4800 {
4801 /* If one or more STATIC_INIT_DECOMP_BASE_P with at least
4802 one init is followed by at least one
4803 STATIC_INIT_DECOMP_NONBASE_P with init, mark it in the
4804 resulting chain as well. */
4805 for (tree n = TREE_CHAIN (node); n; n = TREE_CHAIN (n))
4806 if (STATIC_INIT_DECOMP_BASE_P (n))
4807 continue;
4808 else if (STATIC_INIT_DECOMP_NONBASE_P (n))
4809 {
4810 if (TREE_PURPOSE (n))
4811 {
4812 decomp_state = base;
4813 break;
4814 }
4815 else
4816 continue;
4817 }
4818 else
4819 break;
4820 }
4821 if (init && decomp_state == base)
4822 STATIC_INIT_DECOMP_BASE_P (slot) = 1;
4823 else if (decomp_state == nonbase)
4824 STATIC_INIT_DECOMP_NONBASE_P (slot) = 1;
4825 }
4826
4827 if (!flag_use_cxa_atexit && has_cleanup)
4828 {
4829 // Add to finalization list.
4830 if (!parts[false])
4831 parts[false] = priority_map_t::create_ggc ();
4832 auto &slot = parts[false]->get_or_insert (priority);
4833 slot = tree_cons (NULL_TREE, decl, slot);
4834 }
4835
4836 if (flag_openmp
4837 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
4838 {
4839 priority_map_t **omp_parts = parts + 2;
4840
4841 if (init || (flag_use_cxa_atexit && has_cleanup))
4842 {
4843 // Add to initialization list.
4844 if (!omp_parts[true])
4845 omp_parts[true] = priority_map_t::create_ggc ();
4846 auto &slot = omp_parts[true]->get_or_insert (priority);
4847 slot = tree_cons (init, decl, slot);
4848 if (init && decomp_state == base)
4849 STATIC_INIT_DECOMP_BASE_P (slot) = 1;
4850 else if (decomp_state == nonbase)
4851 STATIC_INIT_DECOMP_NONBASE_P (slot) = 1;
4852 }
4853
4854 if (!flag_use_cxa_atexit && has_cleanup)
4855 {
4856 // Add to finalization list.
4857 if (!omp_parts[false])
4858 omp_parts[false] = priority_map_t::create_ggc ();
4859 auto &slot = omp_parts[false]->get_or_insert (priority);
4860 slot = tree_cons (NULL_TREE, decl, slot);
4861 }
4862 }
4863 }
4864 }
4865
4866 /* Make sure we have told the back end about all the variables in
4867 VARS. */
4868
4869 static void
4870 write_out_vars (tree vars)
4871 {
4872 tree v;
4873
4874 for (v = vars; v; v = TREE_CHAIN (v))
4875 {
4876 tree var = TREE_VALUE (v);
4877 if (!var_finalized_p (var))
4878 {
4879 import_export_decl (var);
4880 rest_of_decl_compilation (var, 1, 1);
4881 }
4882 }
4883 }
4884
4885 /* Generate a static constructor or destructor that calls the given
4886 init/fini fns at the indicated priority. */
4887
4888 static void
4889 generate_ctor_or_dtor_function (bool initp, unsigned priority,
4890 tree fns, location_t locus, bool omp_target)
4891 {
4892 input_location = locus;
4893 tree body = start_objects (initp, priority, bool (fns), omp_target);
4894
4895 if (fns)
4896 {
4897 /* To make sure dynamic construction doesn't access globals from
4898 other compilation units where they might not be yet
4899 constructed, for -fsanitize=address insert
4900 __asan_before_dynamic_init call that prevents access to
4901 either all global variables that need construction in other
4902 compilation units, or at least those that haven't been
4903 initialized yet. Variables that need dynamic construction in
4904 the current compilation unit are kept accessible. */
4905 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4906 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
4907
4908 /* Call the static init/fini functions. */
4909 for (tree node = fns; node; node = TREE_CHAIN (node))
4910 {
4911 tree fn = TREE_PURPOSE (node);
4912
4913 // We should never find a pure or constant cdtor.
4914 gcc_checking_assert (!(flags_from_decl_or_type (fn)
4915 & (ECF_CONST | ECF_PURE)));
4916
4917 tree call = cp_build_function_call_nary (fn, tf_warning_or_error,
4918 NULL_TREE);
4919 finish_expr_stmt (call);
4920 }
4921
4922 /* Revert what __asan_before_dynamic_init did by calling
4923 __asan_after_dynamic_init. */
4924 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4925 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4926 }
4927
4928 /* Close out the function, and arrange for it to be called at init
4929 or fini time, if non-empty. (Even non-nop module initializer
4930 functions need this, as we cannot guarantee the module is
4931 imported somewhere in the program.) */
4932 expand_or_defer_fn (finish_objects (initp, priority, body, fns != NULL_TREE));
4933 }
4934
4935 /* Return C++ property of T, based on given operation OP. */
4936
4937 static int
4938 cpp_check (tree t, cpp_operation op)
4939 {
4940 switch (op)
4941 {
4942 case HAS_DEPENDENT_TEMPLATE_ARGS:
4943 {
4944 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4945 if (!ti)
4946 return 0;
4947 ++processing_template_decl;
4948 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4949 --processing_template_decl;
4950 return dep;
4951 }
4952 case IS_ABSTRACT:
4953 return DECL_PURE_VIRTUAL_P (t);
4954 case IS_ASSIGNMENT_OPERATOR:
4955 return DECL_ASSIGNMENT_OPERATOR_P (t);
4956 case IS_CONSTRUCTOR:
4957 return DECL_CONSTRUCTOR_P (t);
4958 case IS_DESTRUCTOR:
4959 return DECL_DESTRUCTOR_P (t);
4960 case IS_COPY_CONSTRUCTOR:
4961 return DECL_COPY_CONSTRUCTOR_P (t);
4962 case IS_MOVE_CONSTRUCTOR:
4963 return DECL_MOVE_CONSTRUCTOR_P (t);
4964 case IS_TEMPLATE:
4965 return TREE_CODE (t) == TEMPLATE_DECL;
4966 case IS_TRIVIAL:
4967 return trivial_type_p (t);
4968 default:
4969 return 0;
4970 }
4971 }
4972
4973 /* Collect source file references recursively, starting from NAMESPC. */
4974
4975 static void
4976 collect_source_refs (tree namespc)
4977 {
4978 /* Iterate over names in this name space. */
4979 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4980 if (DECL_IS_UNDECLARED_BUILTIN (t))
4981 ;
4982 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4983 collect_source_refs (t);
4984 else
4985 collect_source_ref (DECL_SOURCE_FILE (t));
4986 }
4987
4988 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4989 starting from NAMESPC. */
4990
4991 static void
4992 collect_ada_namespace (tree namespc, const char *source_file)
4993 {
4994 tree decl = NAMESPACE_LEVEL (namespc)->names;
4995
4996 /* Collect decls from this namespace. This will skip
4997 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4998 collect_ada_nodes (decl, source_file);
4999
5000 /* Now scan for namespace children, and dump them. */
5001 for (; decl; decl = TREE_CHAIN (decl))
5002 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
5003 collect_ada_namespace (decl, source_file);
5004 }
5005
5006 /* Returns true iff there is a definition available for variable or
5007 function DECL. */
5008
5009 bool
5010 decl_defined_p (tree decl)
5011 {
5012 if (TREE_CODE (decl) == FUNCTION_DECL)
5013 return (DECL_INITIAL (decl) != NULL_TREE
5014 /* A pending instantiation of a friend temploid is defined. */
5015 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
5016 && DECL_INITIAL (DECL_TEMPLATE_RESULT
5017 (DECL_TI_TEMPLATE (decl)))));
5018 else
5019 {
5020 gcc_assert (VAR_P (decl));
5021 return !DECL_EXTERNAL (decl);
5022 }
5023 }
5024
5025 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
5026
5027 [expr.const]
5028
5029 An integral constant-expression can only involve ... const
5030 variables of integral or enumeration types initialized with
5031 constant expressions ...
5032
5033 C++0x also allows constexpr variables and temporaries initialized
5034 with constant expressions. We handle the former here, but the latter
5035 are just folded away in cxx_eval_constant_expression.
5036
5037 The standard does not require that the expression be non-volatile.
5038 G++ implements the proposed correction in DR 457. */
5039
5040 bool
5041 decl_constant_var_p (tree decl)
5042 {
5043 if (!decl_maybe_constant_var_p (decl))
5044 return false;
5045
5046 /* We don't know if a template static data member is initialized with
5047 a constant expression until we instantiate its initializer. Even
5048 in the case of a constexpr variable, we can't treat it as a
5049 constant until its initializer is complete in case it's used in
5050 its own initializer. */
5051 maybe_instantiate_decl (decl);
5052 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
5053 }
5054
5055 /* Returns true if DECL could be a symbolic constant variable, depending on
5056 its initializer. */
5057
5058 bool
5059 decl_maybe_constant_var_p (tree decl)
5060 {
5061 tree type = TREE_TYPE (decl);
5062 if (!VAR_P (decl))
5063 return false;
5064 if (DECL_DECLARED_CONSTEXPR_P (decl)
5065 && (!TREE_THIS_VOLATILE (decl) || NULLPTR_TYPE_P (type)))
5066 return true;
5067 if (DECL_HAS_VALUE_EXPR_P (decl))
5068 /* A proxy isn't constant. */
5069 return false;
5070 if (TYPE_REF_P (type))
5071 /* References can be constant. */;
5072 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
5073 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5074 /* And const integers. */;
5075 else
5076 return false;
5077
5078 if (DECL_INITIAL (decl)
5079 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
5080 /* We know the initializer, and it isn't constant. */
5081 return false;
5082 else
5083 return true;
5084 }
5085
5086 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
5087 called from grokfndecl and grokvardecl; in all modes it is called from
5088 cp_write_global_declarations. */
5089
5090 void
5091 no_linkage_error (tree decl)
5092 {
5093 if (cxx_dialect >= cxx11
5094 && (decl_defined_p (decl)
5095 /* Treat templates which limit_bad_template_recursion decided
5096 not to instantiate as if they were defined. */
5097 || (errorcount + sorrycount > 0
5098 && DECL_LANG_SPECIFIC (decl)
5099 && DECL_TEMPLATE_INFO (decl)
5100 && warning_suppressed_p (decl /* What warning? */))))
5101 /* In C++11 it's ok if the decl is defined. */
5102 return;
5103
5104 if (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl))
5105 /* An imported decl is ok. */
5106 return;
5107
5108 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
5109 if (t == NULL_TREE)
5110 /* The type that got us on no_linkage_decls must have gotten a name for
5111 linkage purposes. */;
5112 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
5113 // FIXME: This is now invalid, as a DR to c++98
5114 /* The type might end up having a typedef name for linkage purposes. */
5115 vec_safe_push (no_linkage_decls, decl);
5116 else if (TYPE_UNNAMED_P (t))
5117 {
5118 bool d = false;
5119 auto_diagnostic_group grp;
5120 if (cxx_dialect >= cxx11)
5121 {
5122 /* If t is declared in a module CMI, then decl could actually
5123 be defined in a different TU, so don't warn since C++20. */
5124 tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
5125 if (relaxed != NULL_TREE)
5126 d = permerror (DECL_SOURCE_LOCATION (decl),
5127 "%q#D, declared using an unnamed type, "
5128 "is used but never defined", decl);
5129 else if (cxx_dialect < cxx20)
5130 d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
5131 "%q#D, declared using an unnamed type, "
5132 "is used but not defined", decl);
5133 }
5134 else if (DECL_EXTERN_C_P (decl))
5135 /* Allow this; it's pretty common in C. */;
5136 else if (VAR_P (decl))
5137 /* DRs 132, 319 and 389 seem to indicate types with
5138 no linkage can only be used to declare extern "C"
5139 entities. Since it's not always an error in the
5140 ISO C++ 90 Standard, we only issue a warning. */
5141 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
5142 "with no linkage used to declare variable %q#D with "
5143 "linkage", decl);
5144 else
5145 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
5146 "linkage used to declare function %q#D with linkage",
5147 decl);
5148 if (d && is_typedef_decl (TYPE_NAME (t)))
5149 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
5150 "to the unqualified type, so it is not used for linkage",
5151 TYPE_NAME (t));
5152 /* Suppress warning from check_global_declaration if needed. */
5153 if (d)
5154 suppress_warning (decl, OPT_Wunused);
5155 }
5156 else if (cxx_dialect >= cxx11)
5157 {
5158 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
5159 {
5160 /* Similarly for local types in a function with vague linkage or
5161 defined in a module CMI, then decl could actually be defined
5162 in a different TU, so don't warn since C++20. */
5163 bool d = false;
5164 tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
5165 if (relaxed != NULL_TREE)
5166 d = permerror (DECL_SOURCE_LOCATION (decl),
5167 "%q#D, declared using local type "
5168 "%qT, is used but never defined", decl, t);
5169 else if (cxx_dialect < cxx20)
5170 d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
5171 "%q#D, declared using local type "
5172 "%qT, is used but not defined here", decl, t);
5173 /* Suppress warning from check_global_declaration if needed. */
5174 if (d)
5175 suppress_warning (decl, OPT_Wunused);
5176 }
5177 }
5178 else if (VAR_P (decl))
5179 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
5180 "used to declare variable %q#D with linkage", t, decl);
5181 else
5182 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
5183 "to declare function %q#D with linkage", t, decl);
5184 }
5185
5186 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
5187
5188 static void
5189 collect_all_refs (const char *source_file)
5190 {
5191 collect_ada_namespace (global_namespace, source_file);
5192 }
5193
5194 /* Clear DECL_EXTERNAL for NODE. */
5195
5196 static bool
5197 clear_decl_external (struct cgraph_node *node, void * /*data*/)
5198 {
5199 DECL_EXTERNAL (node->decl) = 0;
5200 return false;
5201 }
5202
5203 /* Build up the function to run dynamic initializers for thread_local
5204 variables in this translation unit and alias the init functions for the
5205 individual variables to it. */
5206
5207 static void
5208 handle_tls_init (void)
5209 {
5210 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
5211 if (vars == NULL_TREE)
5212 return;
5213
5214 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
5215
5216 write_out_vars (vars);
5217
5218 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
5219 boolean_type_node);
5220 TREE_PUBLIC (guard) = false;
5221 TREE_STATIC (guard) = true;
5222 DECL_ARTIFICIAL (guard) = true;
5223 DECL_IGNORED_P (guard) = true;
5224 TREE_USED (guard) = true;
5225 CP_DECL_THREAD_LOCAL_P (guard) = true;
5226 set_decl_tls_model (guard, decl_default_tls_model (guard));
5227 pushdecl_top_level_and_finish (guard, NULL_TREE);
5228
5229 tree fn = get_local_tls_init_fn (loc);
5230 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
5231 tree body = begin_function_body ();
5232 tree if_stmt = begin_if_stmt ();
5233 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
5234 tf_warning_or_error);
5235 finish_if_stmt_cond (cond, if_stmt);
5236 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
5237 boolean_true_node,
5238 tf_warning_or_error));
5239 tree sl = NULL_TREE;
5240 int save_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5241 bool saw_nonbase = false;
5242 for (; vars; vars = TREE_CHAIN (vars))
5243 {
5244 tree var = TREE_VALUE (vars);
5245 tree init = TREE_PURPOSE (vars);
5246 sl = decomp_handle_one_var (vars, sl, &saw_nonbase,
5247 save_stmts_are_full_exprs_p);
5248 one_static_initialization_or_destruction (/*initp=*/true, var, init,
5249 false);
5250
5251 /* Output init aliases even with -fno-extern-tls-init. */
5252 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
5253 {
5254 tree single_init_fn = get_tls_init_fn (var);
5255 if (single_init_fn == NULL_TREE)
5256 continue;
5257 cgraph_node *alias
5258 = cgraph_node::get_create (fn)->create_same_body_alias
5259 (single_init_fn, fn);
5260 gcc_assert (alias != NULL);
5261 }
5262 }
5263 decomp_finalize_var_list (sl, save_stmts_are_full_exprs_p);
5264
5265 finish_then_clause (if_stmt);
5266 finish_if_stmt (if_stmt);
5267 finish_function_body (body);
5268 expand_or_defer_fn (finish_function (/*inline_p=*/false));
5269 }
5270
5271 /* We're at the end of compilation, so generate any mangling aliases that
5272 we've been saving up, if DECL is going to be output and ID2 isn't
5273 already taken by another declaration. */
5274
5275 static void
5276 generate_mangling_alias (tree decl, tree id2)
5277 {
5278 struct cgraph_node *n = NULL;
5279
5280 if (TREE_CODE (decl) == FUNCTION_DECL)
5281 {
5282 n = cgraph_node::get (decl);
5283 if (!n)
5284 /* Don't create an alias to an unreferenced function. */
5285 return;
5286 }
5287
5288 tree *slot
5289 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
5290 INSERT);
5291
5292 /* If there's a declaration already using this mangled name,
5293 don't create a compatibility alias that conflicts. */
5294 if (*slot)
5295 return;
5296
5297 tree alias = make_alias_for (decl, id2);
5298 *slot = alias;
5299
5300 DECL_IGNORED_P (alias) = 1;
5301 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
5302 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
5303 if (vague_linkage_p (decl))
5304 DECL_WEAK (alias) = 1;
5305
5306 if (n)
5307 n->create_same_body_alias (alias, decl);
5308 else
5309 varpool_node::create_extra_name_alias (alias, decl);
5310 }
5311
5312 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
5313 the end of translation, for compatibility across bugs in the mangling
5314 implementation. */
5315
5316 void
5317 note_mangling_alias (tree decl, tree id2)
5318 {
5319 if (TARGET_SUPPORTS_ALIASES)
5320 {
5321 if (!defer_mangling_aliases)
5322 generate_mangling_alias (decl, id2);
5323 else
5324 {
5325 vec_safe_push (mangling_aliases, decl);
5326 vec_safe_push (mangling_aliases, id2);
5327 }
5328 }
5329 }
5330
5331 /* Emit all mangling aliases that were deferred up to this point. */
5332
5333 void
5334 generate_mangling_aliases ()
5335 {
5336 while (!vec_safe_is_empty (mangling_aliases))
5337 {
5338 tree id2 = mangling_aliases->pop();
5339 tree decl = mangling_aliases->pop();
5340 generate_mangling_alias (decl, id2);
5341 }
5342 defer_mangling_aliases = false;
5343 }
5344
5345 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
5346 set. NEED_WARNING is true if we must warn about collisions. We do
5347 this to spot changes in mangling that may require compatibility
5348 aliases. */
5349
5350 void
5351 record_mangling (tree decl, bool need_warning)
5352 {
5353 if (!mangled_decls)
5354 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
5355
5356 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
5357 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
5358 tree *slot
5359 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
5360 INSERT);
5361
5362 /* If this is already an alias, cancel the alias, because the real
5363 decl takes precedence. */
5364 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
5365 {
5366 if (symtab_node *n = symtab_node::get (*slot))
5367 {
5368 if (n->cpp_implicit_alias)
5369 /* Actually removing the node isn't safe if other code is already
5370 holding a pointer to it, so just neutralize it. */
5371 n->reset ();
5372 }
5373 else
5374 /* analyze_functions might have already removed the alias from the
5375 symbol table if it's internal. */
5376 gcc_checking_assert (!TREE_PUBLIC (*slot));
5377
5378 *slot = NULL_TREE;
5379 }
5380
5381 if (!*slot)
5382 *slot = decl;
5383 else if (need_warning)
5384 {
5385 auto_diagnostic_group d;
5386 error_at (DECL_SOURCE_LOCATION (decl),
5387 "mangling of %q#D as %qE conflicts with a previous mangle",
5388 decl, id);
5389 inform (DECL_SOURCE_LOCATION (*slot),
5390 "previous mangling %q#D", *slot);
5391 inform (DECL_SOURCE_LOCATION (decl),
5392 "a later %<-fabi-version=%> (or =0)"
5393 " avoids this error with a change in mangling");
5394 *slot = decl;
5395 }
5396 }
5397
5398 /* The mangled name of DECL is being forcibly changed to NAME. Remove
5399 any existing knowledge of DECL's mangled name meaning DECL. */
5400
5401 void
5402 overwrite_mangling (tree decl, tree name)
5403 {
5404 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
5405 if ((TREE_CODE (decl) == VAR_DECL
5406 || TREE_CODE (decl) == FUNCTION_DECL)
5407 && mangled_decls)
5408 if (tree *slot
5409 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
5410 NO_INSERT))
5411 if (*slot == decl)
5412 {
5413 mangled_decls->clear_slot (slot);
5414
5415 /* If this is an alias, remove it from the symbol table. */
5416 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
5417 if (symtab_node *n = symtab_node::get (decl))
5418 if (n->cpp_implicit_alias)
5419 n->remove ();
5420 }
5421
5422 DECL_ASSEMBLER_NAME_RAW (decl) = name;
5423 }
5424
5425 /* The entire file is now complete. If requested, dump everything
5426 to a file. */
5427
5428 static void
5429 dump_tu (void)
5430 {
5431 dump_flags_t flags;
5432 if (FILE *stream = dump_begin (raw_dump_id, &flags))
5433 {
5434 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
5435 dump_end (raw_dump_id, stream);
5436 }
5437 }
5438
5439 static location_t locus_at_end_of_parsing;
5440
5441 /* Check the deallocation functions for CODE to see if we want to warn that
5442 only one was defined. */
5443
5444 static void
5445 maybe_warn_sized_delete (enum tree_code code)
5446 {
5447 tree sized = NULL_TREE;
5448 tree unsized = NULL_TREE;
5449
5450 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
5451 iter; ++iter)
5452 {
5453 tree fn = *iter;
5454 /* We're only interested in usual deallocation functions. */
5455 if (!usual_deallocation_fn_p (fn))
5456 continue;
5457 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5458 unsized = fn;
5459 else
5460 sized = fn;
5461 }
5462 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
5463 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
5464 "the program should also define %qD", sized);
5465 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
5466 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
5467 "the program should also define %qD", unsized);
5468 }
5469
5470 /* Check the global deallocation functions to see if we want to warn about
5471 defining unsized without sized (or vice versa). */
5472
5473 static void
5474 maybe_warn_sized_delete ()
5475 {
5476 if (!flag_sized_deallocation || !warn_sized_deallocation)
5477 return;
5478 maybe_warn_sized_delete (DELETE_EXPR);
5479 maybe_warn_sized_delete (VEC_DELETE_EXPR);
5480 }
5481
5482 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
5483 look them up when evaluating non-type template parameters. Now we need to
5484 lower them to something the back end can understand. */
5485
5486 static void
5487 lower_var_init ()
5488 {
5489 varpool_node *node;
5490 FOR_EACH_VARIABLE (node)
5491 {
5492 tree d = node->decl;
5493 if (tree init = DECL_INITIAL (d))
5494 DECL_INITIAL (d) = cplus_expand_constant (init);
5495 }
5496 }
5497
5498 /* This routine is called at the end of compilation.
5499 Its job is to create all the code needed to initialize and
5500 destroy the global aggregates. We do the destruction
5501 first, since that way we only need to reverse the decls once. */
5502
5503 void
5504 c_parse_final_cleanups (void)
5505 {
5506 size_t i;
5507 tree decl;
5508
5509 locus_at_end_of_parsing = input_location;
5510 /* We're done parsing. */
5511 at_eof = 1;
5512
5513 /* Bad parse errors. Just forget about it. */
5514 if (! global_bindings_p () || current_class_type
5515 || !vec_safe_is_empty (decl_namespace_list))
5516 return;
5517
5518 /* This is the point to write out a PCH if we're doing that.
5519 In that case we do not want to do anything else. */
5520 if (pch_file)
5521 {
5522 /* Mangle all symbols at PCH creation time. */
5523 symtab_node *node;
5524 FOR_EACH_SYMBOL (node)
5525 if (! is_a <varpool_node *> (node)
5526 || ! DECL_HARD_REGISTER (node->decl))
5527 DECL_ASSEMBLER_NAME (node->decl);
5528 c_common_write_pch ();
5529 dump_tu ();
5530 /* Ensure even the callers don't try to finalize the CU. */
5531 flag_syntax_only = 1;
5532 return;
5533 }
5534
5535 timevar_stop (TV_PHASE_PARSING);
5536 timevar_start (TV_PHASE_DEFERRED);
5537
5538 symtab->process_same_body_aliases ();
5539
5540 /* Handle -fdump-ada-spec[-slim] */
5541 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
5542 {
5543 collect_source_ref (main_input_filename);
5544 if (!flag_dump_ada_spec_slim)
5545 collect_source_refs (global_namespace);
5546
5547 dump_ada_specs (collect_all_refs, cpp_check);
5548 }
5549
5550 /* FIXME - huh? was input_line -= 1;*/
5551
5552 /* We now have to write out all the stuff we put off writing out.
5553 These include:
5554
5555 o Template specializations that we have not yet instantiated,
5556 but which are needed.
5557 o Initialization and destruction for non-local objects with
5558 static storage duration. (Local objects with static storage
5559 duration are initialized when their scope is first entered,
5560 and are cleaned up via atexit.)
5561 o Virtual function tables.
5562
5563 All of these may cause others to be needed. For example,
5564 instantiating one function may cause another to be needed, and
5565 generating the initializer for an object may cause templates to be
5566 instantiated, etc., etc. */
5567
5568 emit_support_tinfos ();
5569
5570 /* Track vtables we want to emit that refer to consteval functions. */
5571 auto_vec<tree> consteval_vtables;
5572
5573 int retries = 0;
5574 unsigned ssdf_count = 0, omp_ssdf_count = 0;
5575 for (bool reconsider = true; reconsider; retries++)
5576 {
5577 reconsider = false;
5578
5579 /* If there are templates that we've put off instantiating, do
5580 them now. */
5581 instantiate_pending_templates (retries);
5582 ggc_collect ();
5583
5584 if (header_module_p ())
5585 /* A header modules initializations are handled in its
5586 importer. */
5587 continue;
5588
5589 /* Write out virtual tables as required. Writing out the
5590 virtual table for a template class may cause the
5591 instantiation of members of that class. If we write out
5592 vtables then we remove the class from our list so we don't
5593 have to look at it again. */
5594 tree t;
5595 for (i = keyed_classes->length ();
5596 keyed_classes->iterate (--i, &t);)
5597 if (maybe_emit_vtables (t, consteval_vtables))
5598 {
5599 reconsider = true;
5600 keyed_classes->unordered_remove (i);
5601 }
5602 /* The input_location may have been changed during marking of
5603 vtable entries. */
5604 input_location = locus_at_end_of_parsing;
5605
5606 /* Write out needed type info variables. We have to be careful
5607 looping through unemitted decls, because emit_tinfo_decl may
5608 cause other variables to be needed. New elements will be
5609 appended, and we remove from the vector those that actually
5610 get emitted. */
5611 for (i = unemitted_tinfo_decls->length ();
5612 unemitted_tinfo_decls->iterate (--i, &t);)
5613 if (DECL_INITIAL (t) || emit_tinfo_decl (t))
5614 {
5615 reconsider = true;
5616 unemitted_tinfo_decls->unordered_remove (i);
5617 }
5618
5619 /* The list of objects with static storage duration is built up
5620 in reverse order. We clear STATIC_AGGREGATES so that any new
5621 aggregates added during the initialization of these will be
5622 initialized in the correct order when we next come around the
5623 loop. */
5624 if (tree vars = prune_vars_needing_no_initialization (&static_aggregates))
5625 {
5626 if (flag_openmp)
5627 /* Add initializer information from VARS into
5628 DYNAMIC_INITIALIZERS. */
5629 for (t = vars; t; t = TREE_CHAIN (t))
5630 hash_map_safe_put<hm_ggc> (dynamic_initializers,
5631 TREE_VALUE (t), TREE_PURPOSE (t));
5632
5633 /* Make sure the back end knows about all the variables. */
5634 write_out_vars (vars);
5635
5636 function_depth++; // Disable GC
5637 priority_map_t *parts[4] = {nullptr, nullptr, nullptr, nullptr};
5638 partition_vars_for_init_fini (vars, parts);
5639 tree host_init_fini[2] = { NULL_TREE, NULL_TREE };
5640
5641 for (unsigned initp = 2; initp--;)
5642 if (parts[initp])
5643 for (auto iter : *parts[initp])
5644 {
5645 auto list = iter.second;
5646 if (initp)
5647 // Partitioning kept the vars in reverse order.
5648 // We only want that for dtors.
5649 list = nreverse (list);
5650 host_init_fini[initp]
5651 = emit_partial_init_fini_fn (initp, iter.first, list,
5652 ssdf_count++,
5653 locus_at_end_of_parsing,
5654 NULL_TREE);
5655 }
5656
5657 if (flag_openmp)
5658 {
5659 priority_map_t **omp_parts = parts + 2;
5660 for (unsigned initp = 2; initp--;)
5661 if (omp_parts[initp])
5662 for (auto iter : *omp_parts[initp])
5663 {
5664 auto list = iter.second;
5665 if (initp)
5666 // Partitioning kept the vars in reverse order.
5667 // We only want that for dtors.
5668 list = nreverse (list);
5669 emit_partial_init_fini_fn (initp, iter.first, list,
5670 omp_ssdf_count++,
5671 locus_at_end_of_parsing,
5672 host_init_fini[initp]);
5673 }
5674 }
5675
5676 function_depth--; // Re-enable GC
5677
5678 /* All those initializations and finalizations might cause
5679 us to need more inline functions, more template
5680 instantiations, etc. */
5681 reconsider = true;
5682 }
5683
5684 /* Now do the same for thread_local variables. */
5685 handle_tls_init ();
5686
5687 /* Go through the set of inline functions whose bodies have not
5688 been emitted yet. If out-of-line copies of these functions
5689 are required, emit them. */
5690 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5691 {
5692 /* Does it need synthesizing? */
5693 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
5694 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
5695 {
5696 /* Even though we're already at the top-level, we push
5697 there again. That way, when we pop back a few lines
5698 hence, all of our state is restored. Otherwise,
5699 finish_function doesn't clean things up, and we end
5700 up with CURRENT_FUNCTION_DECL set. */
5701 push_to_top_level ();
5702 /* The decl's location will mark where it was first
5703 needed. Save that so synthesize method can indicate
5704 where it was needed from, in case of error */
5705 input_location = DECL_SOURCE_LOCATION (decl);
5706 synthesize_method (decl);
5707 pop_from_top_level ();
5708 reconsider = true;
5709 }
5710
5711 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
5712 generate_tls_wrapper (decl);
5713
5714 if (!DECL_SAVED_TREE (decl))
5715 continue;
5716
5717 cgraph_node *node = cgraph_node::get_create (decl);
5718
5719 /* We lie to the back end, pretending that some functions
5720 are not defined when they really are. This keeps these
5721 functions from being put out unnecessarily. But, we must
5722 stop lying when the functions are referenced, or if they
5723 are not comdat since they need to be put out now. If
5724 DECL_INTERFACE_KNOWN, then we have already set
5725 DECL_EXTERNAL appropriately, so there's no need to check
5726 again, and we do not want to clear DECL_EXTERNAL if a
5727 previous call to import_export_decl set it.
5728
5729 This is done in a separate for cycle, because if some
5730 deferred function is contained in another deferred
5731 function later in deferred_fns varray,
5732 rest_of_compilation would skip this function and we
5733 really cannot expand the same function twice. */
5734 import_export_decl (decl);
5735 if (DECL_NOT_REALLY_EXTERN (decl)
5736 && DECL_INITIAL (decl)
5737 && decl_needed_p (decl))
5738 {
5739 if (node->cpp_implicit_alias)
5740 node = node->get_alias_target ();
5741
5742 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
5743 NULL, true);
5744 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
5745 group, we need to mark all symbols in the same comdat group
5746 that way. */
5747 if (node->same_comdat_group)
5748 for (cgraph_node *next
5749 = dyn_cast<cgraph_node *> (node->same_comdat_group);
5750 next != node;
5751 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
5752 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
5753 NULL, true);
5754 }
5755
5756 /* If we're going to need to write this function out, and
5757 there's already a body for it, create RTL for it now.
5758 (There might be no body if this is a method we haven't
5759 gotten around to synthesizing yet.) */
5760 if (!DECL_EXTERNAL (decl)
5761 && decl_needed_p (decl)
5762 && !TREE_ASM_WRITTEN (decl)
5763 && !DECL_IMMEDIATE_FUNCTION_P (decl)
5764 && !node->definition)
5765 {
5766 /* We will output the function; no longer consider it in this
5767 loop. */
5768 DECL_DEFER_OUTPUT (decl) = 0;
5769 /* Generate RTL for this function now that we know we
5770 need it. */
5771 expand_or_defer_fn (decl);
5772 reconsider = true;
5773 }
5774 }
5775
5776 if (wrapup_namespace_globals ())
5777 reconsider = true;
5778
5779 /* Static data members are just like namespace-scope globals. */
5780 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
5781 {
5782 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
5783 /* Don't write it out if we haven't seen a definition. */
5784 || DECL_IN_AGGR_P (decl))
5785 continue;
5786 import_export_decl (decl);
5787 /* If this static data member is needed, provide it to the
5788 back end. */
5789 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
5790 DECL_EXTERNAL (decl) = 0;
5791 }
5792
5793 if (vec_safe_length (pending_statics) != 0
5794 && wrapup_global_declarations (pending_statics->address (),
5795 pending_statics->length ()))
5796 reconsider = true;
5797 }
5798
5799 /* All templates have been instantiated. */
5800 at_eof = 2;
5801
5802 void *module_cookie = finish_module_processing (parse_in);
5803
5804 lower_var_init ();
5805
5806 generate_mangling_aliases ();
5807
5808 /* All used inline functions must have a definition at this point. */
5809 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5810 {
5811 if (/* Check online inline functions that were actually used. */
5812 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
5813 /* If the definition actually was available here, then the
5814 fact that the function was not defined merely represents
5815 that for some reason (use of a template repository,
5816 #pragma interface, etc.) we decided not to emit the
5817 definition here. */
5818 && !DECL_INITIAL (decl)
5819 /* A defaulted fn or TLS wrapper in a header module can be
5820 synthesized on demand later. (In non-header modules we
5821 should have synthesized it above.) */
5822 && !(header_module_p ()
5823 && (DECL_DEFAULTED_FN (decl) || decl_tls_wrapper_p (decl)))
5824 /* Don't complain if the template was defined. */
5825 && !((DECL_TEMPLATE_INSTANTIATION (decl)
5826 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
5827 && DECL_INITIAL (DECL_TEMPLATE_RESULT
5828 (template_for_substitution (decl))))
5829 && warning_at (DECL_SOURCE_LOCATION (decl), 0,
5830 "inline function %qD used but never defined", decl))
5831 /* Avoid a duplicate warning from check_global_declaration. */
5832 suppress_warning (decl, OPT_Wunused);
5833 }
5834
5835 /* So must decls that use a type with no linkage. */
5836 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
5837 no_linkage_error (decl);
5838
5839 maybe_warn_sized_delete ();
5840
5841 // Place the init fns in the right order. We need to do this now,
5842 // so that any module init will go at the start.
5843 if (static_init_fini_fns[true])
5844 for (auto iter : *static_init_fini_fns[true])
5845 iter.second = nreverse (iter.second);
5846
5847 if (flag_openmp && static_init_fini_fns[2 + true])
5848 for (auto iter : *static_init_fini_fns[2 + true])
5849 iter.second = nreverse (iter.second);
5850
5851 /* Now we've instantiated all templates. Now we can escalate the functions
5852 we squirreled away earlier. */
5853 process_and_check_pending_immediate_escalating_fns ();
5854
5855 /* Then, do the Objective-C stuff. This is where all the
5856 Objective-C module stuff gets generated (symtab,
5857 class/protocol/selector lists etc). This must be done after C++
5858 templates, destructors etc. so that selectors used in C++
5859 templates are properly allocated. */
5860 if (c_dialect_objc ())
5861 objc_write_global_declarations ();
5862
5863 bool has_module_inits = module_determine_import_inits ();
5864 bool has_objc_init = c_dialect_objc () && objc_static_init_needed_p ();
5865 if (has_module_inits || has_objc_init)
5866 {
5867 input_location = locus_at_end_of_parsing;
5868 tree body = start_partial_init_fini_fn (true, DEFAULT_INIT_PRIORITY,
5869 ssdf_count++, false);
5870 /* For Objective-C++, we may need to initialize metadata found
5871 in this module. This must be done _before_ any other static
5872 initializations. */
5873 if (has_objc_init)
5874 objc_generate_static_init_call (NULL_TREE);
5875 if (has_module_inits)
5876 module_add_import_initializers ();
5877 input_location = locus_at_end_of_parsing;
5878 finish_partial_init_fini_fn (body);
5879 }
5880
5881 if (module_global_init_needed ())
5882 {
5883 // Make sure there's a default priority entry.
5884 if (!static_init_fini_fns[true])
5885 static_init_fini_fns[true] = priority_map_t::create_ggc ();
5886 if (static_init_fini_fns[true]->get_or_insert (DEFAULT_INIT_PRIORITY))
5887 has_module_inits = true;
5888
5889 /* FIXME: We need to work out what static constructors on OpenMP offload
5890 target in modules will look like. */
5891 }
5892
5893 /* Generate initialization and destruction functions for all
5894 priorities for which they are required. They have C-language
5895 linkage. */
5896 push_lang_context (lang_name_c);
5897 for (unsigned initp = 4; initp--;)
5898 if (static_init_fini_fns[initp])
5899 {
5900 for (auto iter : *static_init_fini_fns[initp])
5901 generate_ctor_or_dtor_function (initp & 1, iter.first, iter.second,
5902 locus_at_end_of_parsing,
5903 (initp & 2) != 0);
5904 static_init_fini_fns[initp] = nullptr;
5905 }
5906 pop_lang_context ();
5907
5908 fini_modules (parse_in, module_cookie, has_module_inits);
5909
5910 /* Generate any missing aliases. */
5911 maybe_apply_pending_pragma_weaks ();
5912
5913 if (flag_vtable_verify)
5914 {
5915 vtv_recover_class_info ();
5916 vtv_compute_class_hierarchy_transitive_closure ();
5917 vtv_build_vtable_verify_fndecl ();
5918 }
5919
5920 perform_deferred_noexcept_checks ();
5921
5922 fini_constexpr ();
5923 cp_tree_c_finish_parsing ();
5924 clear_consteval_vfns (consteval_vtables);
5925
5926 /* The entire file is now complete. If requested, dump everything
5927 to a file. */
5928 dump_tu ();
5929
5930 if (flag_detailed_statistics)
5931 {
5932 dump_tree_statistics ();
5933 dump_time_statistics ();
5934 }
5935
5936 timevar_stop (TV_PHASE_DEFERRED);
5937 timevar_start (TV_PHASE_PARSING);
5938
5939 /* Indicate that we're done with front end processing. */
5940 at_eof = 3;
5941 }
5942
5943 /* Perform any post compilation-proper cleanups for the C++ front-end.
5944 This should really go away. No front-end should need to do
5945 anything past the compilation process. */
5946
5947 void
5948 cxx_post_compilation_parsing_cleanups (void)
5949 {
5950 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5951
5952 if (flag_vtable_verify)
5953 {
5954 /* Generate the special constructor initialization function that
5955 calls __VLTRegisterPairs, and give it a very high
5956 initialization priority. This must be done after
5957 finalize_compilation_unit so that we have accurate
5958 information about which vtable will actually be emitted. */
5959 vtv_generate_init_routine ();
5960 }
5961
5962 input_location = locus_at_end_of_parsing;
5963
5964 if (flag_checking)
5965 validate_conversion_obstack ();
5966
5967 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5968 }
5969
5970 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5971 function to call in parse-tree form; it has not yet been
5972 semantically analyzed. ARGS are the arguments to the function.
5973 They have already been semantically analyzed. This may change
5974 ARGS. */
5975
5976 tree
5977 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5978 tsubst_flags_t complain)
5979 {
5980 tree orig_fn;
5981 vec<tree, va_gc> *orig_args = NULL;
5982 tree expr;
5983 tree object;
5984
5985 orig_fn = fn;
5986 object = TREE_OPERAND (fn, 0);
5987
5988 if (processing_template_decl)
5989 {
5990 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5991 || TREE_CODE (fn) == MEMBER_REF);
5992 if (type_dependent_expression_p (fn)
5993 || any_type_dependent_arguments_p (*args))
5994 return build_min_nt_call_vec (fn, *args);
5995
5996 orig_args = make_tree_vector_copy (*args);
5997
5998 /* Transform the arguments and add the implicit "this"
5999 parameter. */
6000 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6001 {
6002 if (TREE_CODE (fn) == DOTSTAR_EXPR)
6003 object = cp_build_addr_expr (object, complain);
6004 vec_safe_insert (*args, 0, object);
6005 }
6006 }
6007
6008 /* A qualified name corresponding to a bound pointer-to-member is
6009 represented as an OFFSET_REF:
6010
6011 struct B { void g(); };
6012 void (B::*p)();
6013 void B::g() { (this->*p)(); } */
6014 if (TREE_CODE (fn) == OFFSET_REF)
6015 {
6016 tree object_addr = cp_build_addr_expr (object, complain);
6017 fn = TREE_OPERAND (fn, 1);
6018 fn = get_member_function_from_ptrfunc (&object_addr, fn,
6019 complain);
6020 vec_safe_insert (*args, 0, object_addr);
6021 }
6022
6023 if (CLASS_TYPE_P (TREE_TYPE (fn)))
6024 expr = build_op_call (fn, args, complain);
6025 else
6026 expr = cp_build_function_call_vec (fn, args, complain);
6027 if (processing_template_decl && expr != error_mark_node)
6028 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
6029
6030 if (orig_args != NULL)
6031 release_tree_vector (orig_args);
6032
6033 return expr;
6034 }
6035
6036
6037 void
6038 check_default_args (tree x)
6039 {
6040 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
6041 bool saw_def = false;
6042 bool noted_first_def = false;
6043 int idx_of_first_default_arg = 0;
6044 location_t loc_of_first_default_arg = UNKNOWN_LOCATION;
6045 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
6046 tree fndecl = STRIP_TEMPLATE (x);
6047 auto_diagnostic_group d;
6048 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
6049 {
6050 if (TREE_PURPOSE (arg))
6051 {
6052 if (!saw_def)
6053 {
6054 saw_def = true;
6055 idx_of_first_default_arg = i;
6056 location_t loc = get_fndecl_argument_location (fndecl, i);
6057 if (loc != DECL_SOURCE_LOCATION (x))
6058 loc_of_first_default_arg = loc;
6059 }
6060 }
6061 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
6062 {
6063 error_at (get_fndecl_argument_location (fndecl, i),
6064 "default argument missing for parameter %P of %q#D", i, x);
6065 if (loc_of_first_default_arg != UNKNOWN_LOCATION
6066 && !noted_first_def)
6067 {
6068 inform (loc_of_first_default_arg,
6069 "...following parameter %P which has a default argument",
6070 idx_of_first_default_arg);
6071 noted_first_def = true;
6072 }
6073 TREE_PURPOSE (arg) = error_mark_node;
6074 }
6075 }
6076 }
6077
6078 /* Return true if function DECL can be inlined. This is used to force
6079 instantiation of methods that might be interesting for inlining. */
6080 bool
6081 possibly_inlined_p (tree decl)
6082 {
6083 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6084 if (DECL_UNINLINABLE (decl))
6085 return false;
6086 if (!optimize)
6087 return DECL_DECLARED_INLINE_P (decl);
6088 /* When optimizing, we might inline everything when flatten
6089 attribute or heuristics inlining for size or autoinlining
6090 is used. */
6091 return true;
6092 }
6093
6094 /* If DECL is a function or variable template specialization, instantiate
6095 its definition now. */
6096
6097 void
6098 maybe_instantiate_decl (tree decl)
6099 {
6100 if (VAR_OR_FUNCTION_DECL_P (decl)
6101 && DECL_LANG_SPECIFIC (decl)
6102 && DECL_TEMPLATE_INFO (decl)
6103 && !uses_template_parms (DECL_TI_ARGS (decl)))
6104 {
6105 /* Instantiating a function will result in garbage collection. We
6106 must treat this situation as if we were within the body of a
6107 function so as to avoid collecting live data only referenced from
6108 the stack (such as overload resolution candidates). */
6109 ++function_depth;
6110 instantiate_decl (decl, /*defer_ok=*/false,
6111 /*expl_inst_class_mem_p=*/false);
6112 --function_depth;
6113 }
6114 }
6115
6116 /* Error if the DECL is unavailable (unless this is currently suppressed).
6117 Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns true if
6118 an error or warning was emitted. */
6119
6120 bool
6121 cp_handle_deprecated_or_unavailable (tree decl, tsubst_flags_t complain)
6122 {
6123 if (!decl)
6124 return false;
6125
6126 if ((complain & tf_error)
6127 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6128 {
6129 if (TREE_UNAVAILABLE (decl))
6130 {
6131 error_unavailable_use (decl, NULL_TREE);
6132 return true;
6133 }
6134 else
6135 {
6136 /* Perhaps this is an unavailable typedef. */
6137 if (TYPE_P (decl)
6138 && TYPE_NAME (decl)
6139 && TREE_UNAVAILABLE (TYPE_NAME (decl)))
6140 {
6141 decl = TYPE_NAME (decl);
6142 /* Don't error within members of a unavailable type. */
6143 if (TYPE_P (decl)
6144 && currently_open_class (decl))
6145 return false;
6146
6147 error_unavailable_use (decl, NULL_TREE);
6148 return true;
6149 }
6150 }
6151 /* Carry on to consider deprecatedness. */
6152 }
6153
6154 if (!(complain & tf_warning)
6155 || deprecated_state == DEPRECATED_SUPPRESS
6156 || deprecated_state == UNAVAILABLE_DEPRECATED_SUPPRESS)
6157 return false;
6158
6159 if (!TREE_DEPRECATED (decl))
6160 {
6161 /* Perhaps this is a deprecated typedef. */
6162 if (TYPE_P (decl) && TYPE_NAME (decl))
6163 decl = TYPE_NAME (decl);
6164
6165 if (!TREE_DEPRECATED (decl))
6166 return false;
6167 }
6168
6169 /* Don't warn within members of a deprecated type. */
6170 if (TYPE_P (decl)
6171 && currently_open_class (decl))
6172 return false;
6173
6174 bool warned = false;
6175 if (cxx_dialect >= cxx11
6176 && DECL_P (decl)
6177 && DECL_ARTIFICIAL (decl)
6178 && DECL_IOBJ_MEMBER_FUNCTION_P (decl)
6179 && copy_fn_p (decl))
6180 {
6181 /* Don't warn if the flag was disabled around the class definition
6182 (c++/94492). */
6183 if (warning_enabled_at (DECL_SOURCE_LOCATION (decl),
6184 OPT_Wdeprecated_copy))
6185 {
6186 auto_diagnostic_group d;
6187 tree ctx = DECL_CONTEXT (decl);
6188 tree other = classtype_has_depr_implicit_copy (ctx);
6189 int opt = (DECL_DESTRUCTOR_P (other)
6190 ? OPT_Wdeprecated_copy_dtor
6191 : OPT_Wdeprecated_copy);
6192 warned = warning (opt, "implicitly-declared %qD is deprecated",
6193 decl);
6194 if (warned)
6195 inform (DECL_SOURCE_LOCATION (other),
6196 "because %qT has user-provided %qD",
6197 ctx, other);
6198 }
6199 }
6200 else
6201 {
6202 if (!warning_suppressed_at (input_location,
6203 OPT_Wdeprecated_declarations))
6204 warned = warn_deprecated_use (decl, NULL_TREE);
6205 suppress_warning_at (input_location, OPT_Wdeprecated_declarations);
6206 }
6207
6208 return warned;
6209 }
6210
6211 /* Like above, but takes into account outer scopes. */
6212
6213 void
6214 cp_warn_deprecated_use_scopes (tree scope)
6215 {
6216 while (scope
6217 && scope != error_mark_node
6218 && scope != global_namespace)
6219 {
6220 if ((TREE_CODE (scope) == NAMESPACE_DECL || OVERLOAD_TYPE_P (scope))
6221 && cp_handle_deprecated_or_unavailable (scope))
6222 return;
6223 if (TYPE_P (scope))
6224 scope = CP_TYPE_CONTEXT (scope);
6225 else
6226 scope = CP_DECL_CONTEXT (scope);
6227 }
6228 }
6229
6230 /* True if DECL or its enclosing scope have unbound template parameters. */
6231
6232 bool
6233 decl_dependent_p (tree decl)
6234 {
6235 if (DECL_FUNCTION_SCOPE_P (decl)
6236 || TREE_CODE (decl) == CONST_DECL
6237 || TREE_CODE (decl) == USING_DECL
6238 || TREE_CODE (decl) == FIELD_DECL)
6239 decl = CP_DECL_CONTEXT (decl);
6240 if (tree tinfo = get_template_info (decl))
6241 if (any_dependent_template_arguments_p (TI_ARGS (tinfo)))
6242 return true;
6243 if (LAMBDA_FUNCTION_P (decl)
6244 && dependent_type_p (DECL_CONTEXT (decl)))
6245 return true;
6246 return false;
6247 }
6248
6249 /* [basic.def.odr] A function is named [and therefore odr-used] by an
6250 expression or conversion if it is the selected member of an overload set in
6251 an overload resolution performed as part of forming that expression or
6252 conversion, unless it is a pure virtual function and either the expression
6253 is not an id-expression naming the function with an explicitly qualified
6254 name or the expression forms a pointer to member.
6255
6256 Mostly, we call mark_used in places that actually do something with a
6257 function, like build_over_call. But in a few places we end up with a
6258 non-overloaded FUNCTION_DECL that we aren't going to do any more with, like
6259 convert_to_void. resolve_nondeduced_context is called in those places,
6260 but it's also called in too many other places. */
6261
6262 bool
6263 mark_single_function (tree expr, tsubst_flags_t complain)
6264 {
6265 expr = maybe_undo_parenthesized_ref (expr);
6266 expr = tree_strip_any_location_wrapper (expr);
6267
6268 if (is_overloaded_fn (expr) == 1
6269 && !mark_used (expr, complain)
6270 && !(complain & tf_error))
6271 return false;
6272 return true;
6273 }
6274
6275 /* True iff we have started, but not finished, defining FUNCTION_DECL DECL. */
6276
6277 bool
6278 fn_being_defined (tree decl)
6279 {
6280 /* DECL_INITIAL is set to error_mark_node in grokfndecl for a definition, and
6281 changed to BLOCK by poplevel at the end of the function. */
6282 return (TREE_CODE (decl) == FUNCTION_DECL
6283 && DECL_INITIAL (decl) == error_mark_node);
6284 }
6285
6286 /* True if DECL is an instantiation of a function template currently being
6287 defined. */
6288
6289 bool
6290 fn_template_being_defined (tree decl)
6291 {
6292 if (TREE_CODE (decl) != FUNCTION_DECL
6293 || !DECL_LANG_SPECIFIC (decl)
6294 || !DECL_TEMPLOID_INSTANTIATION (decl)
6295 || DECL_TEMPLATE_INSTANTIATED (decl))
6296 return false;
6297 tree tinfo = DECL_TEMPLATE_INFO (decl);
6298 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
6299 return fn_being_defined (pattern);
6300 }
6301
6302 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
6303 If DECL is a specialization or implicitly declared class member,
6304 generate the actual definition. Return false if something goes
6305 wrong, true otherwise. */
6306
6307 bool
6308 mark_used (tree decl, tsubst_flags_t complain /* = tf_warning_or_error */)
6309 {
6310 /* If we're just testing conversions or resolving overloads, we
6311 don't want any permanent effects like forcing functions to be
6312 output or instantiating templates. */
6313 if ((complain & tf_conv))
6314 return true;
6315
6316 /* If DECL is a BASELINK for a single function, then treat it just
6317 like the DECL for the function. Otherwise, if the BASELINK is
6318 for an overloaded function, we don't know which function was
6319 actually used until after overload resolution. */
6320 if (BASELINK_P (decl))
6321 {
6322 tree fns = BASELINK_FUNCTIONS (decl);
6323 if (really_overloaded_fn (fns))
6324 return true;
6325 fns = OVL_FIRST (fns);
6326 if (!mark_used (fns, complain))
6327 return false;
6328 /* We might have deduced its return type. */
6329 TREE_TYPE (decl) = TREE_TYPE (fns);
6330 return true;
6331 }
6332
6333 if (!DECL_P (decl))
6334 return true;
6335
6336 /* Set TREE_USED for the benefit of -Wunused. */
6337 TREE_USED (decl) = true;
6338
6339 /* And for structured bindings also the underlying decl. */
6340 if (DECL_DECOMPOSITION_P (decl) && !DECL_DECOMP_IS_BASE (decl))
6341 TREE_USED (DECL_DECOMP_BASE (decl)) = true;
6342
6343 if (TREE_CODE (decl) == TEMPLATE_DECL)
6344 return true;
6345
6346 if (DECL_CLONED_FUNCTION_P (decl))
6347 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
6348
6349 /* Mark enumeration types as used. */
6350 if (TREE_CODE (decl) == CONST_DECL)
6351 used_types_insert (DECL_CONTEXT (decl));
6352
6353 if (TREE_CODE (decl) == FUNCTION_DECL)
6354 {
6355 if (DECL_MAYBE_DELETED (decl))
6356 {
6357 ++function_depth;
6358 maybe_synthesize_method (decl);
6359 --function_depth;
6360 }
6361
6362 if (DECL_DELETED_FN (decl))
6363 {
6364 if (DECL_ARTIFICIAL (decl)
6365 && DECL_CONV_FN_P (decl)
6366 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
6367 /* We mark a lambda conversion op as deleted if we can't
6368 generate it properly; see maybe_add_lambda_conv_op. */
6369 sorry ("converting lambda that uses %<...%> to function pointer");
6370 else if (complain & tf_error)
6371 {
6372 auto_diagnostic_group d;
6373 if (DECL_INITIAL (decl)
6374 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6375 {
6376 escaped_string msg;
6377 msg.escape (TREE_STRING_POINTER (DECL_INITIAL (decl)));
6378 error ("use of deleted function %qD: %s",
6379 decl, (const char *) msg);
6380 }
6381 else
6382 error ("use of deleted function %qD", decl);
6383 if (!maybe_explain_implicit_delete (decl))
6384 inform (DECL_SOURCE_LOCATION (decl), "declared here");
6385 }
6386 return false;
6387 }
6388
6389 if (!maybe_instantiate_noexcept (decl, complain))
6390 return false;
6391 }
6392
6393 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl))
6394 {
6395 if (!DECL_LANG_SPECIFIC (decl))
6396 /* An unresolved dependent local extern. */
6397 return true;
6398
6399 DECL_ODR_USED (decl) = 1;
6400 auto alias = DECL_LOCAL_DECL_ALIAS (decl);
6401 if (!alias || alias == error_mark_node)
6402 return true;
6403
6404 /* Process the underlying decl. */
6405 decl = alias;
6406 TREE_USED (decl) = true;
6407 }
6408
6409 cp_handle_deprecated_or_unavailable (decl, complain);
6410
6411 /* We can only check DECL_ODR_USED on variables or functions with
6412 DECL_LANG_SPECIFIC set, and these are also the only decls that we
6413 might need special handling for. */
6414 if (!VAR_OR_FUNCTION_DECL_P (decl)
6415 || DECL_THUNK_P (decl))
6416 return true;
6417
6418 /* We only want to do this processing once. We don't need to keep trying
6419 to instantiate inline templates, because unit-at-a-time will make sure
6420 we get them compiled before functions that want to inline them. */
6421 if (DECL_LANG_SPECIFIC (decl) && DECL_ODR_USED (decl))
6422 return true;
6423
6424 if (flag_concepts && TREE_CODE (decl) == FUNCTION_DECL
6425 && !constraints_satisfied_p (decl))
6426 {
6427 if (complain & tf_error)
6428 {
6429 auto_diagnostic_group d;
6430 error ("use of function %qD with unsatisfied constraints",
6431 decl);
6432 location_t loc = DECL_SOURCE_LOCATION (decl);
6433 inform (loc, "declared here");
6434 diagnose_constraints (loc, decl, NULL_TREE);
6435 }
6436 return false;
6437 }
6438
6439 /* If DECL has a deduced return type, we need to instantiate it now to
6440 find out its type. For OpenMP user defined reductions, we need them
6441 instantiated for reduction clauses which inline them by hand directly. */
6442 if (undeduced_auto_decl (decl)
6443 || (VAR_P (decl)
6444 && VAR_HAD_UNKNOWN_BOUND (decl))
6445 || (TREE_CODE (decl) == FUNCTION_DECL
6446 && DECL_OMP_DECLARE_REDUCTION_P (decl)))
6447 maybe_instantiate_decl (decl);
6448
6449 if (!decl_dependent_p (decl)
6450 /* Don't require this yet for an instantiation of a function template
6451 we're currently defining (c++/120555). */
6452 && !fn_template_being_defined (decl)
6453 && !require_deduced_type (decl, complain))
6454 return false;
6455
6456 if (DECL_LANG_SPECIFIC (decl) == NULL)
6457 return true;
6458
6459 if (processing_template_decl || in_template_context)
6460 return true;
6461
6462 /* Check this too in case we're within instantiate_non_dependent_expr. */
6463 if (DECL_TEMPLATE_INFO (decl)
6464 && uses_template_parms (DECL_TI_ARGS (decl)))
6465 return true;
6466
6467 if (builtin_pack_fn_p (decl))
6468 {
6469 error ("use of built-in parameter pack %qD outside of a template",
6470 DECL_NAME (decl));
6471 return false;
6472 }
6473
6474 /* If we don't need a value, then we don't need to synthesize DECL. */
6475 if (cp_unevaluated_operand || in_discarded_stmt)
6476 return true;
6477
6478 DECL_ODR_USED (decl) = 1;
6479 if (DECL_CLONED_FUNCTION_P (decl))
6480 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
6481
6482 /* DR 757: A type without linkage shall not be used as the type of a
6483 variable or function with linkage, unless
6484 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
6485 o the variable or function is not used (3.2 [basic.def.odr]) or is
6486 defined in the same translation unit. */
6487 if (cxx_dialect > cxx98
6488 && decl_linkage (decl) != lk_none
6489 && !DECL_EXTERN_C_P (decl)
6490 && !DECL_ARTIFICIAL (decl)
6491 && !decl_defined_p (decl)
6492 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
6493 vec_safe_push (no_linkage_decls, decl);
6494
6495 if (TREE_CODE (decl) == FUNCTION_DECL
6496 && DECL_DECLARED_INLINE_P (decl)
6497 && !DECL_INITIAL (decl)
6498 && !DECL_ARTIFICIAL (decl)
6499 && !DECL_PURE_VIRTUAL_P (decl))
6500 /* Remember it, so we can check it was defined. */
6501 note_vague_linkage_fn (decl);
6502
6503 /* Is it a synthesized method that needs to be synthesized? */
6504 if (TREE_CODE (decl) == FUNCTION_DECL
6505 && DECL_DEFAULTED_FN (decl)
6506 /* A function defaulted outside the class is synthesized either by
6507 cp_finish_decl or instantiate_decl. */
6508 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
6509 && ! DECL_INITIAL (decl))
6510 {
6511 /* Remember the current location for a function we will end up
6512 synthesizing. Then we can inform the user where it was
6513 required in the case of error. */
6514 if (decl_remember_implicit_trigger_p (decl))
6515 DECL_SOURCE_LOCATION (decl) = input_location;
6516
6517 /* Synthesizing an implicitly defined member function will result in
6518 garbage collection. We must treat this situation as if we were
6519 within the body of a function so as to avoid collecting live data
6520 on the stack (such as overload resolution candidates).
6521
6522 We could just let c_parse_final_cleanups handle synthesizing
6523 this function by adding it to deferred_fns, but doing
6524 it at the use site produces better error messages. */
6525 ++function_depth;
6526 synthesize_method (decl);
6527 --function_depth;
6528 /* If this is a synthesized method we don't need to
6529 do the instantiation test below. */
6530 }
6531 else if (VAR_OR_FUNCTION_DECL_P (decl)
6532 && DECL_TEMPLATE_INFO (decl)
6533 && (!DECL_EXPLICIT_INSTANTIATION (decl)
6534 || always_instantiate_p (decl)))
6535 /* If this is a function or variable that is an instance of some
6536 template, we now know that we will need to actually do the
6537 instantiation. We check that DECL is not an explicit
6538 instantiation because that is not checked in instantiate_decl.
6539
6540 We put off instantiating functions in order to improve compile
6541 times. Maintaining a stack of active functions is expensive,
6542 and the inliner knows to instantiate any functions it might
6543 need. Therefore, we always try to defer instantiation. */
6544 {
6545 ++function_depth;
6546 instantiate_decl (decl, /*defer_ok=*/true,
6547 /*expl_inst_class_mem_p=*/false);
6548 --function_depth;
6549 }
6550
6551 return true;
6552 }
6553
6554 tree
6555 vtv_start_verification_constructor_init_function (void)
6556 {
6557 return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, true);
6558 }
6559
6560 tree
6561 vtv_finish_verification_constructor_init_function (tree body)
6562 {
6563 return finish_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, body);
6564 }
6565
6566 #include "gt-cp-decl2.h"