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

source code of gcc/cp/decl2.cc