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