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 591333365 : static hashval_t hash (const value_type decl)
106 : {
107 591333365 : return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
108 : }
109 571630087 : static bool equal (const value_type existing, compare_type candidate)
110 : {
111 571630087 : tree name = DECL_ASSEMBLER_NAME_RAW (existing);
112 571630087 : 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 752492844 : static bool is_deleted (value_type e)
120 : {
121 752492844 : 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 514397 : template <typename T> static bool is_empty (const T &entry)
153 : {
154 514397 : 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 deleteable
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 101236002 : build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
197 : cp_ref_qualifier rqual)
198 : {
199 101236002 : if (fntype == error_mark_node || ctype == error_mark_node)
200 : return error_mark_node;
201 :
202 101235999 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
203 :
204 101235999 : cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
205 101235999 : ctype = cp_build_qualified_type (ctype, type_quals);
206 :
207 101235999 : tree newtype
208 101235999 : = build_method_type_directly (ctype, TREE_TYPE (fntype),
209 101235999 : (TREE_CODE (fntype) == METHOD_TYPE
210 58356 : ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
211 101177643 : : TYPE_ARG_TYPES (fntype)));
212 101235999 : if (tree attrs = TYPE_ATTRIBUTES (fntype))
213 71 : newtype = cp_build_type_attribute_variant (newtype, attrs);
214 303707997 : newtype = build_cp_fntype_variant (newtype, rqual,
215 101235999 : TYPE_RAISES_EXCEPTIONS (fntype),
216 101235999 : TYPE_HAS_LATE_RETURN_TYPE (fntype));
217 :
218 101235999 : 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 897963 : change_return_type (tree new_ret, tree fntype)
226 : {
227 897963 : if (new_ret == error_mark_node)
228 : return fntype;
229 :
230 897963 : if (same_type_p (new_ret, TREE_TYPE (fntype)))
231 : return fntype;
232 :
233 897960 : tree newtype;
234 897960 : tree args = TYPE_ARG_TYPES (fntype);
235 :
236 897960 : if (TREE_CODE (fntype) == FUNCTION_TYPE)
237 : {
238 826431 : newtype = build_function_type (new_ret, args,
239 275477 : TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
240 275477 : newtype = apply_memfn_quals (newtype,
241 : type_memfn_quals (fntype));
242 : }
243 : else
244 622483 : newtype = build_method_type_directly
245 622483 : (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
246 :
247 897960 : if (tree attrs = TYPE_ATTRIBUTES (fntype))
248 92 : newtype = cp_build_type_attribute_variant (newtype, attrs);
249 897960 : newtype = cxx_copy_lang_qualifiers (newtype, fntype);
250 :
251 897960 : 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 540706099 : cp_build_parm_decl (tree fn, tree name, tree type)
259 : {
260 540706099 : tree parm = build_decl (input_location,
261 : PARM_DECL, name, type);
262 540706099 : 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 540706099 : if (!processing_template_decl)
267 187374065 : DECL_ARG_TYPE (parm) = type_passed_as (type);
268 :
269 540706099 : 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 234353488 : build_artificial_parm (tree fn, tree name, tree type)
277 : {
278 234353488 : tree parm = cp_build_parm_decl (fn, name, type);
279 234353488 : DECL_ARTIFICIAL (parm) = 1;
280 : /* All our artificial parms are implicitly `const'; they cannot be
281 : assigned to. */
282 234353488 : TREE_READONLY (parm) = 1;
283 234353488 : 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 79271310 : maybe_retrofit_in_chrg (tree fn)
298 : {
299 79271310 : tree basetype, arg_types, parms, parm, fntype;
300 :
301 : /* If we've already add the in-charge parameter don't do it again. */
302 79271310 : 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 79271310 : 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 50487256 : if (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
313 : return;
314 :
315 1127094 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
316 1127094 : basetype = TREE_TYPE (TREE_VALUE (arg_types));
317 1127094 : arg_types = TREE_CHAIN (arg_types);
318 :
319 1127094 : 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 1127094 : if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
324 : {
325 1127094 : 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 1127094 : DECL_CHAIN (parm) = parms;
329 1127094 : parms = parm;
330 :
331 : /* ...and then to TYPE_ARG_TYPES. */
332 1127094 : arg_types = hash_tree_chain (vtt_parm_type, arg_types);
333 :
334 1127094 : DECL_HAS_VTT_PARM_P (fn) = 1;
335 : }
336 :
337 : /* Then add the in-charge parm (before the VTT parm). */
338 1127094 : parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
339 1127094 : DECL_CHAIN (parm) = parms;
340 1127094 : parms = parm;
341 1127094 : arg_types = hash_tree_chain (integer_type_node, arg_types);
342 :
343 : /* Insert our new parameter(s) into the list. */
344 1127094 : DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
345 :
346 : /* And rebuild the function type. */
347 1127094 : fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
348 : arg_types);
349 1127094 : if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
350 3 : fntype = (cp_build_type_attribute_variant
351 3 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
352 1127094 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
353 1127094 : TREE_TYPE (fn) = fntype;
354 :
355 : /* Now we've got the in-charge parameter. */
356 1127094 : 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 140579267 : grokclassfn (tree ctype, tree function, enum overload_flags flags)
381 : {
382 140579267 : 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 140579267 : SET_DECL_LANGUAGE (function, lang_cplusplus);
387 :
388 140579267 : 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 140579267 : DECL_CONTEXT (function) = ctype;
396 :
397 140579267 : if (flags == DTOR_FLAG)
398 10968844 : DECL_CXX_DESTRUCTOR_P (function) = 1;
399 :
400 270189690 : if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
401 53029979 : maybe_retrofit_in_chrg (function);
402 140579267 : }
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 11395221 : 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 11395221 : tree type;
414 11395221 : tree expr;
415 11395221 : tree orig_array_expr = array_expr;
416 11395221 : tree orig_index_exp = index_exp;
417 11385970 : vec<tree, va_gc> *orig_index_exp_list
418 11395221 : = index_exp_list ? *index_exp_list : NULL;
419 11395221 : tree overload = NULL_TREE;
420 :
421 11395221 : if (error_operand_p (array_expr) || error_operand_p (index_exp))
422 92 : return error_mark_node;
423 :
424 11395129 : if (processing_template_decl)
425 : {
426 9749914 : if (type_dependent_expression_p (array_expr)
427 9749940 : || (index_exp ? type_dependent_expression_p (index_exp)
428 26 : : any_type_dependent_arguments_p (*index_exp_list)))
429 : {
430 7933981 : if (index_exp == NULL)
431 6917 : index_exp = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
432 : *index_exp_list);
433 7933981 : return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
434 7933981 : NULL_TREE, NULL_TREE);
435 : }
436 1815933 : if (!index_exp)
437 25 : orig_index_exp_list = make_tree_vector_copy (*index_exp_list);
438 : }
439 :
440 3461148 : type = TREE_TYPE (array_expr);
441 3461148 : gcc_assert (type);
442 3461148 : type = non_reference (type);
443 :
444 : /* If they have an `operator[]', use that. */
445 3461148 : if (MAYBE_CLASS_TYPE_P (type)
446 3135084 : || (index_exp && MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
447 : || (index_exp == NULL_TREE
448 56 : && !(*index_exp_list)->is_empty ()
449 51 : && MAYBE_CLASS_TYPE_P (TREE_TYPE ((*index_exp_list)->last ()))))
450 : {
451 326107 : if (index_exp)
452 325669 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
453 : index_exp, NULL_TREE, NULL_TREE,
454 : &overload, complain);
455 438 : else if ((*index_exp_list)->is_empty ())
456 99 : expr = build_op_subscript (loc, array_expr, index_exp_list, &overload,
457 : complain);
458 : else
459 : {
460 339 : expr = build_op_subscript (loc, array_expr, index_exp_list,
461 : &overload, complain & tf_decltype);
462 339 : if (expr == error_mark_node
463 : /* Don't do the backward compatibility fallback in a SFINAE
464 : context. */
465 16 : && (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 3135041 : tree p1, p2, i1, i2;
503 3135041 : 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 3135041 : if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
510 : p1 = array_expr;
511 : else
512 2021903 : p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
513 :
514 3135041 : if (index_exp == NULL_TREE)
515 : {
516 56 : if (!(complain & tf_error))
517 : /* Don't do the backward compatibility fallback in a SFINAE
518 : context. */
519 8 : 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 3135032 : if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
554 : p2 = index_exp;
555 : else
556 3134996 : p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
557 :
558 3135032 : i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
559 : false);
560 3135032 : i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
561 : false);
562 :
563 3135032 : if ((p1 && i2) && (i1 && p2))
564 0 : error ("ambiguous conversion for array subscript");
565 :
566 3135032 : if (p1 && i2)
567 : array_expr = p1, index_exp = i2;
568 80 : else if (i1 && p2)
569 : swapped = true, array_expr = p2, index_exp = i1;
570 : else
571 : {
572 29 : if (complain & tf_error)
573 25 : error_at (loc, "invalid types %<%T[%T]%> for array subscript",
574 25 : type, TREE_TYPE (index_exp));
575 29 : return error_mark_node;
576 : }
577 :
578 3135003 : if (array_expr == error_mark_node || index_exp == error_mark_node)
579 0 : error ("ambiguous conversion for array subscript");
580 :
581 3135003 : if (TYPE_PTR_P (TREE_TYPE (array_expr)))
582 2021855 : array_expr = mark_rvalue_use (array_expr);
583 : else
584 1113148 : array_expr = mark_lvalue_use_nonread (array_expr);
585 3135003 : index_exp = mark_rvalue_use (index_exp);
586 3135003 : if (swapped
587 51 : && flag_strong_eval_order == 2
588 3135047 : && (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 3134985 : expr = build_array_ref (input_location, array_expr, index_exp);
592 : }
593 3461110 : if (processing_template_decl && expr != error_mark_node)
594 : {
595 1815933 : if (overload != NULL_TREE)
596 : {
597 264522 : if (orig_index_exp == NULL_TREE)
598 : {
599 22 : expr = build_min_non_dep_op_overload (expr, overload,
600 : orig_array_expr,
601 : orig_index_exp_list);
602 22 : release_tree_vector (orig_index_exp_list);
603 22 : return expr;
604 : }
605 264500 : return build_min_non_dep_op_overload (ARRAY_REF, expr, overload,
606 : orig_array_expr,
607 264500 : orig_index_exp);
608 : }
609 :
610 1551411 : 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 1551411 : return build_min_non_dep (ARRAY_REF, expr, orig_array_expr,
619 1551411 : 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 4803 : grok_omp_array_section (location_t loc, tree array_expr, tree index,
629 : tree length)
630 : {
631 4803 : tree orig_array_expr = array_expr;
632 4803 : tree orig_index = index;
633 4803 : tree orig_length = length;
634 :
635 4803 : if (error_operand_p (array_expr)
636 4794 : || error_operand_p (index)
637 9597 : || error_operand_p (length))
638 9 : return error_mark_node;
639 :
640 4794 : if (processing_template_decl
641 4794 : && (type_dependent_expression_p (array_expr)
642 757 : || type_dependent_expression_p (index)
643 741 : || type_dependent_expression_p (length)))
644 930 : return build_min_nt_loc (loc, OMP_ARRAY_SECTION, array_expr, index, length);
645 :
646 3864 : index = fold_non_dependent_expr (index);
647 3864 : 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 3864 : tree expr = build_omp_array_section (loc, array_expr, index, length);
654 :
655 3864 : 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 562044 : delete_sanity (location_t loc, tree exp, tree size, bool doing_vec,
670 : int use_global_delete, tsubst_flags_t complain)
671 : {
672 562044 : tree t, type;
673 :
674 562044 : if (exp == error_mark_node)
675 : return exp;
676 :
677 561927 : if (processing_template_decl)
678 : {
679 437364 : t = build_min (DELETE_EXPR, void_type_node, exp, size);
680 437364 : DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
681 437364 : DELETE_EXPR_USE_VEC (t) = doing_vec;
682 437364 : TREE_SIDE_EFFECTS (t) = 1;
683 437364 : SET_EXPR_LOCATION (t, loc);
684 437364 : return t;
685 : }
686 :
687 124563 : 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 124563 : if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
691 124563 : && (complain & tf_warning))
692 21 : warning_at (exp_loc, 0, "deleting array %q#E", exp);
693 :
694 124563 : t = build_expr_type_conversion (WANT_POINTER, exp, true);
695 :
696 124563 : 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 124542 : 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 124542 : 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 124536 : 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 124536 : if (integer_zerop (t))
730 3 : return build1_loc (loc, NOP_EXPR, void_type_node, t);
731 :
732 124533 : if (doing_vec)
733 21058 : return build_vec_delete (loc, t, /*maxindex=*/NULL_TREE,
734 : sfk_deleting_destructor,
735 21058 : use_global_delete, complain);
736 : else
737 103475 : return build_delete (loc, type, t, sfk_deleting_destructor,
738 : LOOKUP_NORMAL, use_global_delete,
739 103475 : 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 21863283 : check_member_template (tree tmpl)
747 : {
748 21863283 : tree decl;
749 :
750 21863283 : gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
751 21863283 : decl = DECL_TEMPLATE_RESULT (tmpl);
752 :
753 21863283 : if (TREE_CODE (decl) == FUNCTION_DECL
754 2310282 : || DECL_ALIAS_TEMPLATE_P (tmpl)
755 22951803 : || (TREE_CODE (decl) == TYPE_DECL
756 761951 : && 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 21963376 : gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
761 : /* The parser rejects any use of virtual in a function template. */
762 21536714 : 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 21536714 : DECL_IGNORED_P (tmpl) = 1;
768 : }
769 326569 : else if (variable_template_p (tmpl))
770 : /* OK */;
771 : else
772 0 : error ("template declaration of %q#D", decl);
773 21863283 : }
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 88 : : 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 74 : 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 9160810 : check_classfn (tree ctype, tree function, tree template_parms)
988 : {
989 9160810 : if (DECL_USE_TEMPLATE (function)
990 1169969 : && !(TREE_CODE (function) == TEMPLATE_DECL
991 1100 : && DECL_TEMPLATE_SPECIALIZATION (function))
992 10329679 : && 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 8771742 : if (TREE_CODE (function) == TEMPLATE_DECL)
1008 : {
1009 1118 : if (template_parms
1010 2218 : && !comp_template_parms (template_parms,
1011 1100 : 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 1118 : template_parms = DECL_TEMPLATE_PARMS (function);
1018 : }
1019 :
1020 : /* OK, is this a definition of a member template? */
1021 8771742 : bool is_template = (template_parms != NULL_TREE);
1022 :
1023 : /* [temp.mem]
1024 :
1025 : A destructor shall not be a member template. */
1026 17543484 : 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 8771736 : tree pushed_scope = push_scope (ctype);
1036 8771736 : tree matched = NULL_TREE;
1037 8771736 : tree fns = get_class_binding (ctype, DECL_NAME (function));
1038 8771736 : bool saw_template = false;
1039 :
1040 35381408 : for (ovl_iterator iter (fns); !matched && iter; ++iter)
1041 : {
1042 15771122 : tree fndecl = *iter;
1043 :
1044 15771122 : if (TREE_CODE (fndecl) == TEMPLATE_DECL)
1045 3904717 : saw_template = true;
1046 :
1047 : /* A member template definition only matches a member template
1048 : declaration. */
1049 15771122 : if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
1050 1713566 : continue;
1051 :
1052 14057556 : if (!DECL_DECLARES_FUNCTION_P (fndecl))
1053 294518 : continue;
1054 :
1055 13763038 : tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1056 13763038 : 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 13763038 : if (DECL_STATIC_FUNCTION_P (fndecl)
1066 13763038 : && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1067 170503 : p1 = TREE_CHAIN (p1);
1068 :
1069 : /* ref-qualifier or absence of same must match. */
1070 27526076 : if (type_memfn_rqual (TREE_TYPE (function))
1071 13763038 : != type_memfn_rqual (TREE_TYPE (fndecl)))
1072 86 : continue;
1073 :
1074 : // Include constraints in the match.
1075 13762952 : tree c1 = get_constraints (function);
1076 13762952 : 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 13762952 : if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1082 : TREE_TYPE (TREE_TYPE (fndecl)))
1083 13023891 : && compparms (p1, p2)
1084 8771664 : && !disjoint_version_decls (function, fndecl)
1085 8771664 : && (!is_template
1086 2139002 : || comp_template_parms (template_parms,
1087 2139002 : DECL_TEMPLATE_PARMS (fndecl)))
1088 8771658 : && equivalent_constraints (c1, c2)
1089 8771607 : && (DECL_TEMPLATE_SPECIALIZATION (function)
1090 8771607 : == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1091 22534559 : && (!DECL_TEMPLATE_SPECIALIZATION (function)
1092 778729 : || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
1093 : matched = fndecl;
1094 : }
1095 :
1096 114 : if (!matched && !is_template && saw_template
1097 8771751 : && !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 8771736 : 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 8771736 : if (pushed_scope)
1140 8420277 : 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 48252302 : note_vague_linkage_fn (tree decl)
1151 : {
1152 48252302 : if (processing_template_decl)
1153 : return;
1154 :
1155 48252293 : DECL_DEFER_OUTPUT (decl) = 1;
1156 48252293 : vec_safe_push (deferred_fns, decl);
1157 : }
1158 :
1159 : /* As above, but for variables. */
1160 :
1161 : void
1162 12479183 : note_vague_linkage_variable (tree decl)
1163 : {
1164 12479183 : vec_safe_push (pending_statics, decl);
1165 12479183 : }
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 16448971 : finish_static_data_member_decl (tree decl,
1172 : tree init, bool init_const_expr_p,
1173 : tree asmspec_tree,
1174 : int flags)
1175 : {
1176 16448971 : 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 16448968 : 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 16448968 : if (! processing_template_decl)
1188 13599747 : vec_safe_push (pending_statics, decl);
1189 :
1190 16448968 : if (LOCAL_CLASS_P (current_class_type)
1191 : /* We already complained about the template definition. */
1192 16448968 : && !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 33272672 : for (tree t = current_class_type; TYPE_P (t);
1198 16797231 : t = CP_TYPE_CONTEXT (t))
1199 33594534 : 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 16448968 : 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 5518611 : DECL_IN_AGGR_P (decl) = 1;
1215 :
1216 16448968 : if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
1217 16448968 : && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
1218 107461 : SET_VAR_HAD_UNKNOWN_BOUND (decl);
1219 :
1220 16448968 : 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 12047370 : tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
1226 12047370 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
1227 : }
1228 :
1229 16448968 : cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
1230 16448968 : 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 70748585 : 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 70748585 : tree value;
1245 70748585 : const char *asmspec = 0;
1246 70748585 : int flags;
1247 :
1248 70748585 : if (init
1249 12771439 : && TREE_CODE (init) == TREE_LIST
1250 0 : && TREE_VALUE (init) == error_mark_node
1251 70748585 : && TREE_CHAIN (init) == NULL_TREE)
1252 : init = NULL_TREE;
1253 :
1254 70748585 : int initialized;
1255 70748585 : if (init == ridpointers[(int)RID_DELETE]
1256 70748585 : || (init
1257 9202015 : && TREE_CODE (init) == STRING_CST
1258 178 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
1259 : initialized = SD_DELETED;
1260 67178983 : else if (init == ridpointers[(int)RID_DEFAULT])
1261 : initialized = SD_DEFAULTED;
1262 60544446 : else if (init)
1263 : initialized = SD_INITIALIZED;
1264 : else
1265 57977146 : initialized = SD_UNINITIALIZED;
1266 :
1267 70748585 : value = grokdeclarator (declarator, declspecs, FIELD, initialized, &attrlist);
1268 70748579 : if (! value || value == error_mark_node)
1269 : /* friend or constructor went bad. */
1270 340 : return error_mark_node;
1271 70748239 : if (TREE_TYPE (value) == error_mark_node)
1272 : return value;
1273 :
1274 70748080 : 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 70748080 : if (value == void_type_node)
1284 : return value;
1285 :
1286 70748080 : if (DECL_NAME (value)
1287 70748080 : && 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 70748077 : if (TREE_CODE (value) == TYPE_DECL)
1296 : {
1297 24824974 : DECL_NONLOCAL (value) = 1;
1298 24824974 : DECL_CONTEXT (value) = current_class_type;
1299 :
1300 24824974 : if (attrlist)
1301 : {
1302 23457 : 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 23457 : if (TYPE_DECL_FOR_LINKAGE_PURPOSES_P (value))
1307 12 : attrflags = ATTR_FLAG_TYPE_IN_PLACE;
1308 :
1309 23457 : cplus_decl_attributes (&value, attrlist, attrflags);
1310 : }
1311 :
1312 24824974 : if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
1313 24824974 : && TREE_TYPE (value) != error_mark_node
1314 49649948 : && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
1315 24824337 : 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 24824974 : if (processing_template_decl)
1321 20519714 : value = push_template_decl (value);
1322 :
1323 24824974 : record_locally_defined_typedef (value);
1324 24824974 : return value;
1325 : }
1326 :
1327 45923103 : int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
1328 :
1329 45923103 : 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 45923103 : if (asmspec_tree && asmspec_tree != error_mark_node)
1336 194 : asmspec = TREE_STRING_POINTER (asmspec_tree);
1337 :
1338 45923103 : if (init)
1339 : {
1340 12771376 : if (TREE_CODE (value) == FUNCTION_DECL)
1341 : {
1342 10569180 : if (init == ridpointers[(int)RID_DELETE]
1343 10569180 : || (TREE_CODE (init) == STRING_CST
1344 178 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
1345 : {
1346 3569596 : DECL_DELETED_FN (value) = 1;
1347 3569596 : DECL_DECLARED_INLINE_P (value) = 1;
1348 3569596 : if (TREE_CODE (init) == STRING_CST)
1349 178 : DECL_INITIAL (value) = init;
1350 : }
1351 6999584 : else if (init == ridpointers[(int)RID_DEFAULT])
1352 : {
1353 6634534 : if (defaultable_fn_check (value))
1354 : {
1355 6634455 : DECL_DEFAULTED_FN (value) = 1;
1356 6634455 : DECL_INITIALIZED_IN_CLASS_P (value) = 1;
1357 6634455 : 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 6634455 : DECL_INITIAL (value) = NULL_TREE;
1361 : }
1362 : }
1363 365050 : else if (TREE_CODE (init) == DEFERRED_PARSE)
1364 0 : error ("invalid initializer for member function %qD", value);
1365 365050 : else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
1366 : {
1367 365041 : if (integer_zerop (init))
1368 365002 : 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 2202196 : 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 45923103 : if ((TREE_CODE (value) == FUNCTION_DECL
1396 19644360 : || TREE_CODE (value) == TEMPLATE_DECL)
1397 45923160 : && DECL_CONTEXT (value) != current_class_type)
1398 : {
1399 1996641 : if (attrlist)
1400 0 : cplus_decl_attributes (&value, attrlist, 0);
1401 1996641 : return value;
1402 : }
1403 :
1404 : /* Need to set this before push_template_decl. */
1405 43926462 : if (VAR_P (value))
1406 685474 : DECL_CONTEXT (value) = current_class_type;
1407 :
1408 43926462 : if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1409 : {
1410 15254774 : value = push_template_decl (value);
1411 15254774 : if (error_operand_p (value))
1412 7 : return error_mark_node;
1413 : }
1414 :
1415 43926455 : if (attrlist)
1416 856385 : cplus_decl_attributes (&value, attrlist, 0);
1417 :
1418 43926455 : if (init && DIRECT_LIST_INIT_P (init))
1419 : flags = LOOKUP_NORMAL;
1420 : else
1421 : flags = LOOKUP_IMPLICIT;
1422 :
1423 43926455 : switch (TREE_CODE (value))
1424 : {
1425 685470 : case VAR_DECL:
1426 685470 : finish_static_data_member_decl (value, init, init_const_expr_p,
1427 : asmspec_tree, flags);
1428 685470 : return value;
1429 :
1430 18958829 : case FIELD_DECL:
1431 18958829 : if (asmspec)
1432 6 : error ("%<asm%> specifiers are not permitted on non-static data members");
1433 18958829 : if (DECL_INITIAL (value) == error_mark_node)
1434 0 : init = error_mark_node;
1435 18958829 : cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1436 : NULL_TREE, flags);
1437 18958829 : DECL_IN_AGGR_P (value) = 1;
1438 18958829 : return value;
1439 :
1440 24282156 : case FUNCTION_DECL:
1441 24282156 : if (asmspec)
1442 92 : set_user_assembler_name (value, asmspec);
1443 :
1444 24282156 : 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 24282156 : if (DECL_UNIQUE_FRIEND_P (value))
1451 0 : return void_type_node;
1452 :
1453 24282156 : DECL_IN_AGGR_P (value) = 1;
1454 24282156 : 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 12047377 : start_initialized_static_member (const cp_declarator *declarator,
1468 : cp_decl_specifier_seq *declspecs,
1469 : tree attrlist)
1470 : {
1471 12047377 : tree value = grokdeclarator (declarator, declspecs, FIELD, SD_INITIALIZED,
1472 12047377 : &attrlist);
1473 12047377 : if (!value || error_operand_p (value))
1474 3 : return error_mark_node;
1475 12047374 : 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 12047374 : 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 12047374 : 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 12047371 : gcc_checking_assert (VAR_P (value));
1503 :
1504 12047371 : DECL_CONTEXT (value) = current_class_type;
1505 12047371 : DECL_INITIALIZED_IN_CLASS_P (value) = true;
1506 :
1507 12047371 : if (processing_template_decl)
1508 : {
1509 2691059 : value = push_template_decl (value);
1510 2691059 : if (error_operand_p (value))
1511 0 : return error_mark_node;
1512 : }
1513 :
1514 12047371 : if (attrlist)
1515 7 : cplus_decl_attributes (&value, attrlist, 0);
1516 :
1517 : /* When defining a template we need to register the TEMPLATE_DECL. */
1518 12047371 : tree maybe_template = value;
1519 12047371 : if (template_parm_scope_p ())
1520 : {
1521 367961 : if (!DECL_TEMPLATE_SPECIALIZATION (value))
1522 326466 : maybe_template = DECL_TI_TEMPLATE (value);
1523 : else
1524 : maybe_template = NULL_TREE;
1525 : }
1526 12005876 : if (maybe_template)
1527 12005876 : finish_member_declaration (maybe_template);
1528 :
1529 12047371 : 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 34134591 : is_static_data_member_initialized_in_class (tree decl)
1537 : {
1538 34134591 : if (!decl || decl == error_mark_node)
1539 : return false;
1540 :
1541 34133788 : tree inner = STRIP_TEMPLATE (decl);
1542 20638858 : return (inner
1543 34133788 : && VAR_P (inner)
1544 12527823 : && DECL_CLASS_SCOPE_P (inner)
1545 33162484 : && DECL_INITIALIZED_IN_CLASS_P (inner));
1546 : }
1547 :
1548 : /* Finish a declaration prepared with start_initialized_static_member. */
1549 :
1550 : void
1551 12047374 : finish_initialized_static_member (tree decl, tree init, tree asmspec)
1552 : {
1553 12047374 : if (decl == error_mark_node)
1554 : return;
1555 12047368 : gcc_checking_assert (is_static_data_member_initialized_in_class (decl));
1556 :
1557 12047368 : int flags;
1558 12047368 : if (init && DIRECT_LIST_INIT_P (init))
1559 : flags = LOOKUP_NORMAL;
1560 : else
1561 : flags = LOOKUP_IMPLICIT;
1562 12047368 : 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 596961 : grokbitfield (const cp_declarator *declarator,
1572 : cp_decl_specifier_seq *declspecs, tree width, tree init,
1573 : tree attrlist)
1574 : {
1575 596961 : tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1576 596961 : init != NULL_TREE, &attrlist);
1577 :
1578 596961 : if (value == error_mark_node)
1579 : return NULL_TREE; /* friends went bad. */
1580 :
1581 596955 : tree type = TREE_TYPE (value);
1582 596955 : if (type == error_mark_node)
1583 : return value;
1584 :
1585 : /* Pass friendly classes back. */
1586 596908 : if (VOID_TYPE_P (value))
1587 0 : return void_type_node;
1588 :
1589 596908 : if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)
1590 596908 : && (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 596875 : 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 596869 : 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 596866 : 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 596857 : 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 596857 : 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 596851 : 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 596851 : int flags = LOOKUP_IMPLICIT;
1644 596851 : if (init && DIRECT_LIST_INIT_P (init))
1645 : flags = LOOKUP_NORMAL;
1646 596851 : cp_finish_decl (value, init, false, NULL_TREE, flags);
1647 :
1648 596851 : if (width != error_mark_node)
1649 : {
1650 : /* The width must be an integer type. */
1651 596834 : if (!type_dependent_expression_p (width)
1652 596834 : && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1653 12 : error ("width of bit-field %qD has non-integral type %qT", value,
1654 12 : TREE_TYPE (width));
1655 596822 : 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 596816 : DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1661 596816 : SET_DECL_C_BIT_FIELD (value);
1662 : }
1663 : }
1664 :
1665 596851 : DECL_IN_AGGR_P (value) = 1;
1666 :
1667 596851 : if (attrlist)
1668 1203 : cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1669 :
1670 596851 : 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 16533481 : is_late_template_attribute (tree attr, tree decl)
1679 : {
1680 16533481 : tree name = get_attribute_name (attr);
1681 16533481 : tree args = TREE_VALUE (attr);
1682 16533481 : const struct attribute_spec *spec
1683 16533481 : = lookup_attribute_spec (TREE_PURPOSE (attr));
1684 16533481 : 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 16533481 : if (annotation_p (attr))
1689 : return true;
1690 :
1691 16533441 : if (!spec)
1692 : /* Unknown attribute. */
1693 : return false;
1694 :
1695 : /* Attribute weak handling wants to write out assembly right away. */
1696 16533407 : 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 16533404 : if (TREE_CODE (decl) == TYPE_DECL
1703 16533404 : && (is_attribute_p ("unused", name)
1704 16371 : || is_attribute_p ("used", name)
1705 16365 : || (is_attribute_p ("maybe_unused", name)
1706 10 : && get_attribute_namespace (attr) == NULL_TREE)))
1707 244 : return false;
1708 :
1709 : /* Attribute tls_model wants to modify the symtab. */
1710 16533160 : if (is_attribute_p ("tls_model", name))
1711 : return true;
1712 :
1713 : /* #pragma omp declare simd attribute needs to be always deferred. */
1714 16533157 : if (flag_openmp
1715 16533157 : && is_attribute_p ("omp declare simd", name))
1716 : return true;
1717 :
1718 16532265 : if (args == error_mark_node)
1719 : return false;
1720 :
1721 : /* An attribute pack is clearly dependent. */
1722 16532265 : 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 17416593 : for (arg = args; arg; arg = TREE_CHAIN (arg))
1728 : {
1729 1012414 : 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 1049474 : if (arg == args
1736 972412 : && get_attribute_namespace (attr) == gnu_identifier
1737 815937 : && attribute_takes_identifier_p (name)
1738 1012414 : && identifier_p (t))
1739 37060 : continue;
1740 :
1741 975354 : if (value_dependent_expression_p (t))
1742 : return true;
1743 : }
1744 :
1745 16404179 : if (TREE_CODE (decl) == TYPE_DECL
1746 16388834 : || TYPE_P (decl)
1747 16028569 : || spec->type_required)
1748 : {
1749 765552 : tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1750 :
1751 405287 : if (!type)
1752 : return true;
1753 :
1754 : /* We can't apply any attributes to a completely unknown type until
1755 : instantiation time. */
1756 765549 : enum tree_code code = TREE_CODE (type);
1757 765549 : if (code == TEMPLATE_TYPE_PARM
1758 765549 : || code == BOUND_TEMPLATE_TEMPLATE_PARM
1759 765141 : || code == TYPENAME_TYPE)
1760 : return true;
1761 : /* Also defer most attributes on dependent types. This is not
1762 : necessary in all cases, but is the better default. */
1763 760863 : else if (dependent_type_p (type)
1764 : /* But some attributes specifically apply to templates. */
1765 688458 : && !is_attribute_p ("abi_tag", name)
1766 688434 : && !is_attribute_p ("deprecated", name)
1767 352719 : && !is_attribute_p ("unavailable", name)
1768 1113576 : && !is_attribute_p ("visibility", name))
1769 : return true;
1770 : else
1771 : return false;
1772 : }
1773 : else
1774 : return false;
1775 : }
1776 :
1777 : /* ATTR_P is a list of attributes. Remove any attributes which need to be
1778 : applied at instantiation time and return them. If IS_DEPENDENT is true,
1779 : the declaration itself is dependent, so all attributes should be applied
1780 : at instantiation time. */
1781 :
1782 : tree
1783 233158530 : splice_template_attributes (tree *attr_p, tree decl)
1784 : {
1785 233158530 : tree *p = attr_p;
1786 233158530 : tree late_attrs = NULL_TREE;
1787 233158530 : tree *q = &late_attrs;
1788 :
1789 233158530 : if (!p || *p == error_mark_node)
1790 : return NULL_TREE;
1791 :
1792 249692011 : for (; *p; )
1793 : {
1794 16533481 : if (is_late_template_attribute (*p, decl))
1795 : {
1796 486414 : ATTR_IS_DEPENDENT (*p) = 1;
1797 486414 : *q = *p;
1798 486414 : *p = TREE_CHAIN (*p);
1799 486414 : q = &TREE_CHAIN (*q);
1800 486414 : *q = NULL_TREE;
1801 : }
1802 : else
1803 16047067 : p = &TREE_CHAIN (*p);
1804 : }
1805 :
1806 233158530 : return late_attrs;
1807 : }
1808 :
1809 : /* Attach any LATE_ATTRS to DECL_P, after the non-dependent attributes have
1810 : been applied by a previous call to decl_attributes. */
1811 :
1812 : static void
1813 484424 : save_template_attributes (tree late_attrs, tree *decl_p, int flags)
1814 : {
1815 484424 : tree *q;
1816 :
1817 484424 : if (!late_attrs)
1818 : return;
1819 :
1820 484424 : if (DECL_P (*decl_p))
1821 472638 : q = &DECL_ATTRIBUTES (*decl_p);
1822 : else
1823 11786 : q = &TYPE_ATTRIBUTES (*decl_p);
1824 :
1825 484424 : tree old_attrs = *q;
1826 :
1827 : /* Place the late attributes at the beginning of the attribute
1828 : list. */
1829 484424 : late_attrs = chainon (late_attrs, *q);
1830 484424 : if (*q != late_attrs
1831 484424 : && !DECL_P (*decl_p)
1832 11786 : && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1833 : {
1834 52 : if (!dependent_type_p (*decl_p))
1835 12 : *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1836 : else
1837 : {
1838 40 : *decl_p = build_variant_type_copy (*decl_p);
1839 40 : TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1840 : }
1841 : }
1842 : else
1843 484372 : *q = late_attrs;
1844 :
1845 484424 : if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1846 : {
1847 : /* We've added new attributes directly to the main variant, so
1848 : now we need to update all of the other variants to include
1849 : these new attributes. */
1850 11746 : tree variant;
1851 11886 : for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1852 140 : variant = TYPE_NEXT_VARIANT (variant))
1853 : {
1854 140 : gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1855 140 : TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1856 : }
1857 : }
1858 : }
1859 :
1860 : /* True if ATTRS contains any dependent attributes that affect type
1861 : identity. */
1862 :
1863 : bool
1864 1873127228 : any_dependent_type_attributes_p (tree attrs)
1865 : {
1866 1904916498 : for (tree a = attrs; a; a = TREE_CHAIN (a))
1867 31790024 : if (ATTR_IS_DEPENDENT (a))
1868 : {
1869 1966941 : const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1870 1966941 : if (as && as->affects_type_identity)
1871 : return true;
1872 : }
1873 : return false;
1874 : }
1875 :
1876 : /* Return true iff ATTRS are acceptable attributes to be applied in-place
1877 : to a typedef which gives a previously unnamed class or enum a name for
1878 : linkage purposes. */
1879 :
1880 : bool
1881 386771 : attributes_naming_typedef_ok (tree attrs)
1882 : {
1883 398281 : for (; attrs; attrs = TREE_CHAIN (attrs))
1884 : {
1885 11513 : tree name = get_attribute_name (attrs);
1886 11513 : if (is_attribute_p ("vector_size", name))
1887 : return false;
1888 : }
1889 : return true;
1890 : }
1891 :
1892 : /* Like reconstruct_complex_type, but handle also template trees. */
1893 :
1894 : tree
1895 30714 : cp_reconstruct_complex_type (tree type, tree bottom)
1896 : {
1897 30714 : tree inner, outer;
1898 :
1899 30714 : if (TYPE_PTR_P (type))
1900 : {
1901 39 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1902 39 : outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1903 39 : TYPE_REF_CAN_ALIAS_ALL (type));
1904 : }
1905 : else if (TYPE_REF_P (type))
1906 : {
1907 12 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1908 12 : outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1909 12 : TYPE_REF_CAN_ALIAS_ALL (type));
1910 : }
1911 : else if (TREE_CODE (type) == ARRAY_TYPE)
1912 : {
1913 30 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1914 30 : outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1915 : /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1916 : element type qualification will be handled by the recursive
1917 : cp_reconstruct_complex_type call and cp_build_qualified_type
1918 : for ARRAY_TYPEs changes the element type. */
1919 30 : return outer;
1920 : }
1921 : else if (TREE_CODE (type) == FUNCTION_TYPE)
1922 : {
1923 131 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1924 131 : outer = build_function_type (inner, TYPE_ARG_TYPES (type),
1925 131 : TYPE_NO_NAMED_ARGS_STDARG_P (type));
1926 131 : outer = apply_memfn_quals (outer, type_memfn_quals (type));
1927 : }
1928 : else if (TREE_CODE (type) == METHOD_TYPE)
1929 : {
1930 12 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1931 : /* The build_method_type_directly() routine prepends 'this' to argument list,
1932 : so we must compensate by getting rid of it. */
1933 12 : outer
1934 : = build_method_type_directly
1935 12 : (class_of_this_parm (type), inner,
1936 12 : TREE_CHAIN (TYPE_ARG_TYPES (type)));
1937 : }
1938 : else if (TREE_CODE (type) == OFFSET_TYPE)
1939 : {
1940 3 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1941 3 : outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1942 : }
1943 : else
1944 : return bottom;
1945 :
1946 197 : if (TYPE_ATTRIBUTES (type))
1947 0 : outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1948 197 : outer = cp_build_qualified_type (outer, cp_type_quals (type));
1949 197 : outer = cxx_copy_lang_qualifiers (outer, type);
1950 :
1951 197 : return outer;
1952 : }
1953 :
1954 : /* Replaces any constexpr expression that may be into the attributes
1955 : arguments with their reduced value. */
1956 :
1957 : void
1958 374330977 : cp_check_const_attributes (tree attributes)
1959 : {
1960 374330977 : if (attributes == error_mark_node)
1961 : return;
1962 :
1963 : tree attr;
1964 412707914 : for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1965 : {
1966 : /* Annotation arguments are handled in handle_annotation_attribute. */
1967 38376937 : if (annotation_p (attr))
1968 379 : continue;
1969 :
1970 38376558 : tree arg;
1971 : /* As we implement alignas using gnu::aligned attribute and
1972 : alignas argument is a constant expression, force manifestly
1973 : constant evaluation of aligned attribute argument. */
1974 38376558 : bool manifestly_const_eval
1975 38376558 : = is_attribute_p ("aligned", get_attribute_name (attr));
1976 50000301 : for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1977 11623743 : arg = TREE_CHAIN (arg))
1978 : {
1979 11623743 : tree expr = TREE_VALUE (arg);
1980 11623743 : if (EXPR_P (expr))
1981 7190 : TREE_VALUE (arg)
1982 3595 : = fold_non_dependent_expr (expr, tf_warning_or_error,
1983 : manifestly_const_eval);
1984 : }
1985 : }
1986 : }
1987 :
1988 : /* Copies hot or cold attributes to a function FN if present on the
1989 : encapsulating class, struct, or union TYPE. */
1990 :
1991 : void
1992 23726397 : maybe_propagate_warmth_attributes (tree fn, tree type)
1993 : {
1994 23726397 : if (fn == NULL_TREE || type == NULL_TREE
1995 23726397 : || !(TREE_CODE (type) == RECORD_TYPE
1996 : || TREE_CODE (type) == UNION_TYPE))
1997 : return;
1998 :
1999 23726397 : tree has_cold_attr = lookup_attribute ("cold", TYPE_ATTRIBUTES (type));
2000 23726397 : tree has_hot_attr = lookup_attribute ("hot", TYPE_ATTRIBUTES (type));
2001 :
2002 23726397 : if (has_cold_attr || has_hot_attr)
2003 : {
2004 : /* Transparently ignore the new warmth attribute if it
2005 : conflicts with a present function attribute. Otherwise
2006 : decl_attribute would still honour the present attribute,
2007 : but producing an undesired warning in the process. */
2008 :
2009 18 : if (has_cold_attr)
2010 : {
2011 9 : if (lookup_attribute ("hot", DECL_ATTRIBUTES (fn)) == NULL)
2012 : {
2013 9 : tree cold_cons
2014 9 : = tree_cons (get_identifier ("cold"), NULL, NULL);
2015 :
2016 9 : decl_attributes (&fn, cold_cons, 0);
2017 : }
2018 : }
2019 9 : else if (has_hot_attr)
2020 : {
2021 9 : if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)) == NULL)
2022 : {
2023 9 : tree hot_cons
2024 9 : = tree_cons (get_identifier ("hot"), NULL, NULL);
2025 :
2026 9 : decl_attributes (&fn, hot_cons, 0);
2027 : }
2028 : }
2029 : }
2030 : }
2031 :
2032 : /* Return the last pushed declaration for the symbol DECL or NULL
2033 : when no such declaration exists. */
2034 :
2035 : static tree
2036 374265266 : find_last_decl (tree decl)
2037 : {
2038 374265266 : tree last_decl = NULL_TREE;
2039 :
2040 374265266 : if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
2041 : {
2042 : /* Template specializations are matched elsewhere. */
2043 312301555 : if (DECL_LANG_SPECIFIC (decl)
2044 312301555 : && DECL_USE_TEMPLATE (decl))
2045 : return NULL_TREE;
2046 :
2047 : /* Look up the declaration in its scope. */
2048 307559470 : tree pushed_scope = NULL_TREE;
2049 307559470 : if (tree ctype = DECL_CONTEXT (decl))
2050 235902785 : pushed_scope = push_scope (ctype);
2051 :
2052 307559470 : last_decl = lookup_name (name);
2053 :
2054 307559470 : if (pushed_scope)
2055 133333133 : pop_scope (pushed_scope);
2056 :
2057 : /* The declaration may be a member conversion operator
2058 : or a bunch of overfloads (handle the latter below). */
2059 307559470 : if (last_decl && BASELINK_P (last_decl))
2060 174611 : last_decl = BASELINK_FUNCTIONS (last_decl);
2061 : }
2062 :
2063 61917917 : if (!last_decl)
2064 274328497 : return NULL_TREE;
2065 :
2066 95194684 : if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
2067 : {
2068 : /* A set of overloads of the same function. */
2069 541298512 : for (lkp_iterator iter (last_decl); iter; ++iter)
2070 : {
2071 455912444 : if (TREE_CODE (*iter) == OVERLOAD)
2072 0 : continue;
2073 :
2074 455912444 : tree d = *iter;
2075 :
2076 : /* We can't compare versions in the middle of processing the
2077 : attribute that has the version. */
2078 455912444 : if (TREE_CODE (d) == FUNCTION_DECL
2079 455912444 : && DECL_FUNCTION_VERSIONED (d))
2080 9790111 : return NULL_TREE;
2081 :
2082 455911310 : if (decls_match (decl, d, /*record_decls=*/false))
2083 : return d;
2084 : }
2085 85386068 : return NULL_TREE;
2086 : }
2087 :
2088 : return NULL_TREE;
2089 : }
2090 :
2091 : /* Like decl_attributes, but handle C++ complexity. */
2092 :
2093 : void
2094 374328992 : cplus_decl_attributes (tree *decl, tree attributes, int flags)
2095 : {
2096 374328992 : if (*decl == NULL_TREE || *decl == void_type_node
2097 374328992 : || *decl == error_mark_node || attributes == error_mark_node)
2098 : return;
2099 :
2100 : /* Add implicit "omp declare target" attribute if requested. */
2101 374328909 : if (vec_safe_length (scope_chain->omp_declare_target_attribute)
2102 10874 : && ((VAR_P (*decl)
2103 2156 : && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
2104 3376 : || TREE_CODE (*decl) == FUNCTION_DECL))
2105 : {
2106 1697 : if (VAR_P (*decl)
2107 1697 : && DECL_CLASS_SCOPE_P (*decl))
2108 0 : error ("%q+D static data member inside of declare target directive",
2109 : *decl);
2110 : else
2111 : {
2112 1697 : if (VAR_P (*decl)
2113 1697 : && (processing_template_decl
2114 282 : || !omp_mappable_type (TREE_TYPE (*decl))))
2115 52 : attributes
2116 52 : = tree_cons (get_identifier ("omp declare target implicit"),
2117 : NULL_TREE, attributes);
2118 : else
2119 1645 : attributes = tree_cons (get_identifier ("omp declare target"),
2120 : NULL_TREE, attributes);
2121 1697 : if (TREE_CODE (*decl) == FUNCTION_DECL)
2122 : {
2123 1415 : cp_omp_declare_target_attr &last
2124 1415 : = scope_chain->omp_declare_target_attribute->last ();
2125 1415 : int device_type = MAX (last.device_type, 0);
2126 1415 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
2127 1415 : && !lookup_attribute ("omp declare target host",
2128 : attributes))
2129 6 : attributes
2130 6 : = tree_cons (get_identifier ("omp declare target host"),
2131 : NULL_TREE, attributes);
2132 1415 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
2133 1415 : && !lookup_attribute ("omp declare target nohost",
2134 : attributes))
2135 6 : attributes
2136 6 : = tree_cons (get_identifier ("omp declare target nohost"),
2137 : NULL_TREE, attributes);
2138 1415 : if (last.indirect
2139 1415 : && !lookup_attribute ("omp declare target indirect",
2140 : attributes))
2141 21 : attributes
2142 21 : = tree_cons (get_identifier ("omp declare target indirect"),
2143 : NULL_TREE, attributes);
2144 : }
2145 : }
2146 : }
2147 :
2148 374328909 : tree late_attrs = NULL_TREE;
2149 374328909 : if (processing_template_decl)
2150 : {
2151 233145082 : if (check_for_bare_parameter_packs (attributes))
2152 : return;
2153 233145073 : late_attrs = splice_template_attributes (&attributes, *decl);
2154 : }
2155 :
2156 374328900 : cp_check_const_attributes (attributes);
2157 :
2158 374328900 : if (flag_openmp || flag_openmp_simd)
2159 : {
2160 : bool diagnosed = false;
2161 1348062 : for (tree *pa = &attributes; *pa; )
2162 : {
2163 186002 : if (get_attribute_namespace (*pa) == omp_identifier)
2164 : {
2165 209 : tree name = get_attribute_name (*pa);
2166 209 : if (is_attribute_p ("directive", name)
2167 0 : || is_attribute_p ("sequence", name)
2168 209 : || is_attribute_p ("decl", name))
2169 : {
2170 209 : const char *p = NULL;
2171 209 : if (TREE_VALUE (*pa) == NULL_TREE)
2172 9 : p = IDENTIFIER_POINTER (name);
2173 409 : for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
2174 : {
2175 200 : tree d = TREE_VALUE (a);
2176 200 : gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
2177 340 : if (TREE_PUBLIC (d)
2178 143 : && (VAR_P (*decl)
2179 10 : || TREE_CODE (*decl) == FUNCTION_DECL)
2180 343 : && cp_maybe_parse_omp_decl (*decl, d))
2181 140 : continue;
2182 60 : p = TREE_PUBLIC (d) ? "decl" : "directive";
2183 : }
2184 209 : if (p && !diagnosed)
2185 : {
2186 69 : error ("%<omp::%s%> not allowed to be specified in "
2187 : "this context", p);
2188 69 : diagnosed = true;
2189 : }
2190 209 : if (p)
2191 : {
2192 69 : *pa = TREE_CHAIN (*pa);
2193 69 : continue;
2194 : }
2195 : }
2196 : }
2197 185933 : pa = &TREE_CHAIN (*pa);
2198 : }
2199 : }
2200 :
2201 374328900 : if (TREE_CODE (*decl) == TEMPLATE_DECL)
2202 1100 : decl = &DECL_TEMPLATE_RESULT (*decl);
2203 :
2204 374328900 : if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
2205 : {
2206 63634 : attributes
2207 63634 : = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
2208 63634 : decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
2209 : attributes, flags);
2210 : }
2211 : else
2212 : {
2213 374265266 : tree last_decl = find_last_decl (*decl);
2214 374265266 : decl_attributes (decl, attributes, flags, last_decl);
2215 : }
2216 :
2217 374328900 : if (late_attrs)
2218 484424 : save_template_attributes (late_attrs, decl, flags);
2219 :
2220 : /* Propagate deprecation out to the template. */
2221 374328900 : if (TREE_DEPRECATED (*decl))
2222 1485426 : if (tree ti = get_template_info (*decl))
2223 : {
2224 470686 : tree tmpl = TI_TEMPLATE (ti);
2225 470686 : tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
2226 470686 : : DECL_TEMPLATE_RESULT (tmpl));
2227 470686 : if (*decl == pattern)
2228 422266 : TREE_DEPRECATED (tmpl) = true;
2229 : }
2230 :
2231 : /* Likewise, propagate unavailability out to the template. */
2232 374328900 : if (TREE_UNAVAILABLE (*decl))
2233 252 : if (tree ti = get_template_info (*decl))
2234 : {
2235 9 : tree tmpl = TI_TEMPLATE (ti);
2236 9 : tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
2237 9 : : DECL_TEMPLATE_RESULT (tmpl));
2238 9 : if (*decl == pattern)
2239 9 : TREE_UNAVAILABLE (tmpl) = true;
2240 : }
2241 :
2242 374328900 : if (VAR_P (*decl) && CP_DECL_THREAD_LOCAL_P (*decl))
2243 : {
2244 : // tls_model attribute can set a stronger TLS access model.
2245 19474 : tls_model model = DECL_TLS_MODEL (*decl);
2246 : // Don't upgrade TLS model if TLS model isn't set yet.
2247 19474 : if (model != TLS_MODEL_NONE)
2248 : {
2249 19350 : tls_model default_model = decl_default_tls_model (*decl);
2250 19350 : if (default_model > model)
2251 19128 : set_decl_tls_model (*decl, default_model);
2252 : }
2253 : }
2254 :
2255 : /* For target_version semantics, mark any annotated function as versioned
2256 : so that it gets mangled even when on its own in a TU. */
2257 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2258 : && TREE_CODE (*decl) == FUNCTION_DECL
2259 : && get_target_version (*decl).is_valid ())
2260 : maybe_mark_function_versioned (*decl);
2261 : }
2262 :
2263 : /* Walks through the namespace- or function-scope anonymous union
2264 : OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
2265 : Returns one of the fields for use in the mangled name. */
2266 :
2267 : static tree
2268 377 : build_anon_union_vars (tree type, tree object)
2269 : {
2270 377 : tree main_decl = NULL_TREE;
2271 377 : tree field;
2272 :
2273 : /* Rather than write the code to handle the non-union case,
2274 : just give an error. */
2275 377 : if (TREE_CODE (type) != UNION_TYPE)
2276 : {
2277 6 : error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
2278 : "anonymous struct not inside named type");
2279 6 : return error_mark_node;
2280 : }
2281 :
2282 371 : for (field = TYPE_FIELDS (type);
2283 1322 : field != NULL_TREE;
2284 951 : field = DECL_CHAIN (field))
2285 : {
2286 951 : tree decl;
2287 951 : tree ref;
2288 :
2289 951 : if (DECL_ARTIFICIAL (field))
2290 468 : continue;
2291 483 : if (TREE_CODE (field) != FIELD_DECL)
2292 : {
2293 0 : permerror (DECL_SOURCE_LOCATION (field),
2294 : "%q#D invalid; an anonymous union can only "
2295 : "have non-static data members", field);
2296 0 : continue;
2297 : }
2298 :
2299 483 : if (TREE_PRIVATE (field))
2300 6 : permerror (DECL_SOURCE_LOCATION (field),
2301 : "private member %q#D in anonymous union", field);
2302 477 : else if (TREE_PROTECTED (field))
2303 0 : permerror (DECL_SOURCE_LOCATION (field),
2304 : "protected member %q#D in anonymous union", field);
2305 :
2306 483 : if (processing_template_decl)
2307 66 : ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
2308 : field, NULL_TREE);
2309 : else
2310 417 : ref = build_class_member_access_expr (object, field, NULL_TREE,
2311 : false, tf_warning_or_error);
2312 :
2313 483 : if (DECL_NAME (field))
2314 : {
2315 446 : tree base;
2316 :
2317 446 : decl = build_decl (input_location,
2318 446 : VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2319 446 : DECL_ANON_UNION_VAR_P (decl) = 1;
2320 446 : DECL_ARTIFICIAL (decl) = 1;
2321 :
2322 446 : base = get_base_address (object);
2323 446 : TREE_PUBLIC (decl) = TREE_PUBLIC (base);
2324 446 : TREE_STATIC (decl) = TREE_STATIC (base);
2325 446 : DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
2326 :
2327 446 : SET_DECL_VALUE_EXPR (decl, ref);
2328 446 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
2329 :
2330 446 : decl = pushdecl (decl);
2331 : }
2332 37 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2333 37 : decl = build_anon_union_vars (TREE_TYPE (field), ref);
2334 : else
2335 : decl = 0;
2336 :
2337 483 : if (main_decl == NULL_TREE)
2338 951 : main_decl = decl;
2339 : }
2340 :
2341 : return main_decl;
2342 : }
2343 :
2344 : /* Finish off the processing of a UNION_TYPE structure. If the union is an
2345 : anonymous union, then all members must be laid out together. PUBLIC_P
2346 : is nonzero if this union is not declared static. */
2347 :
2348 : void
2349 349 : finish_anon_union (tree anon_union_decl)
2350 : {
2351 349 : tree type;
2352 349 : tree main_decl;
2353 349 : bool public_p;
2354 :
2355 349 : if (anon_union_decl == error_mark_node)
2356 : return;
2357 :
2358 340 : type = TREE_TYPE (anon_union_decl);
2359 340 : public_p = TREE_PUBLIC (anon_union_decl);
2360 :
2361 : /* The VAR_DECL's context is the same as the TYPE's context. */
2362 340 : DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
2363 :
2364 340 : if (TYPE_FIELDS (type) == NULL_TREE)
2365 : return;
2366 :
2367 340 : if (public_p)
2368 : {
2369 0 : error ("namespace-scope anonymous aggregates must be static");
2370 0 : return;
2371 : }
2372 :
2373 340 : main_decl = build_anon_union_vars (type, anon_union_decl);
2374 340 : if (main_decl == error_mark_node)
2375 : return;
2376 316 : if (main_decl == NULL_TREE)
2377 : {
2378 12 : pedwarn (input_location, 0, "anonymous union with no members");
2379 12 : return;
2380 : }
2381 :
2382 304 : if (!processing_template_decl)
2383 : {
2384 : /* Use main_decl to set the mangled name. */
2385 265 : DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
2386 265 : maybe_commonize_var (anon_union_decl);
2387 265 : if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
2388 : {
2389 152 : if (DECL_DISCRIMINATOR_P (anon_union_decl))
2390 45 : determine_local_discriminator (anon_union_decl);
2391 152 : mangle_decl (anon_union_decl);
2392 : }
2393 265 : DECL_NAME (anon_union_decl) = NULL_TREE;
2394 : }
2395 :
2396 304 : pushdecl (anon_union_decl);
2397 304 : cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
2398 : }
2399 :
2400 : /* Auxiliary functions to make type signatures for
2401 : `operator new' and `operator delete' correspond to
2402 : what compiler will be expecting. */
2403 :
2404 : tree
2405 140840 : coerce_new_type (tree type, location_t loc)
2406 : {
2407 140840 : int e = 0;
2408 140840 : tree args = TYPE_ARG_TYPES (type);
2409 :
2410 140840 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2411 :
2412 140840 : if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2413 : {
2414 6 : e = 1;
2415 6 : error_at (loc, "%<operator new%> must return type %qT",
2416 : ptr_type_node);
2417 : }
2418 :
2419 140840 : if (args && args != void_list_node)
2420 : {
2421 140825 : if (TREE_PURPOSE (args))
2422 : {
2423 : /* [basic.stc.dynamic.allocation]
2424 :
2425 : The first parameter shall not have an associated default
2426 : argument. */
2427 18 : error_at (loc, "the first parameter of %<operator new%> cannot "
2428 : "have a default argument");
2429 : /* Throw away the default argument. */
2430 18 : TREE_PURPOSE (args) = NULL_TREE;
2431 : }
2432 :
2433 140825 : if (!same_type_p (TREE_VALUE (args), size_type_node))
2434 : {
2435 21 : e = 2;
2436 21 : args = TREE_CHAIN (args);
2437 : }
2438 : }
2439 : else
2440 : e = 2;
2441 :
2442 21 : if (e == 2)
2443 36 : permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
2444 : "as first parameter", size_type_node);
2445 :
2446 140840 : switch (e)
2447 : {
2448 36 : case 2:
2449 36 : args = tree_cons (NULL_TREE, size_type_node, args);
2450 : /* Fall through. */
2451 42 : case 1:
2452 42 : type = (cxx_copy_lang_qualifiers
2453 42 : (build_function_type (ptr_type_node, args),
2454 : type));
2455 : /* Fall through. */
2456 140840 : default:;
2457 : }
2458 140840 : return type;
2459 : }
2460 :
2461 : void
2462 188624 : coerce_delete_type (tree decl, location_t loc)
2463 : {
2464 188624 : int e = 0;
2465 188624 : tree type = TREE_TYPE (decl);
2466 188624 : tree args = TYPE_ARG_TYPES (type);
2467 :
2468 188624 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2469 :
2470 188624 : if (!same_type_p (TREE_TYPE (type), void_type_node))
2471 : {
2472 15 : e = 1;
2473 15 : error_at (loc, "%<operator delete%> must return type %qT",
2474 : void_type_node);
2475 : }
2476 :
2477 188624 : tree ptrtype = ptr_type_node;
2478 188624 : if (destroying_delete_p (decl))
2479 : {
2480 37 : if (DECL_CLASS_SCOPE_P (decl))
2481 : /* If the function is a destroying operator delete declared in class
2482 : type C, the type of its first parameter shall be C*. */
2483 37 : ptrtype = build_pointer_type (DECL_CONTEXT (decl));
2484 : else
2485 : /* A destroying operator delete shall be a class member function named
2486 : operator delete. */
2487 0 : error_at (loc,
2488 : "destroying %<operator delete%> must be a member function");
2489 37 : const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl));
2490 37 : if (op->flags & OVL_OP_FLAG_VEC)
2491 0 : error_at (loc, "%<operator delete[]%> cannot be a destroying delete");
2492 37 : if (!usual_deallocation_fn_p (decl))
2493 0 : error_at (loc, "destroying %<operator delete%> must be a usual "
2494 : "deallocation function");
2495 : }
2496 :
2497 188621 : if (!args || args == void_list_node
2498 377242 : || !same_type_p (TREE_VALUE (args), ptrtype))
2499 : {
2500 15 : e = 2;
2501 15 : if (args && args != void_list_node)
2502 9 : args = TREE_CHAIN (args);
2503 15 : error_at (loc, "%<operator delete%> takes type %qT as first parameter",
2504 : ptrtype);
2505 : }
2506 188609 : switch (e)
2507 : {
2508 15 : case 2:
2509 15 : args = tree_cons (NULL_TREE, ptrtype, args);
2510 : /* Fall through. */
2511 30 : case 1:
2512 30 : type = (cxx_copy_lang_qualifiers
2513 30 : (build_function_type (void_type_node, args),
2514 : type));
2515 : /* Fall through. */
2516 188624 : default:;
2517 : }
2518 :
2519 188624 : TREE_TYPE (decl) = type;
2520 188624 : }
2521 :
2522 : /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
2523 : and mark them as needed. */
2524 :
2525 : static void
2526 359715 : mark_vtable_entries (tree decl, vec<tree> &consteval_vtables)
2527 : {
2528 : /* It's OK for the vtable to refer to deprecated virtual functions. */
2529 359715 : auto du = make_temp_override (deprecated_state,
2530 359715 : UNAVAILABLE_DEPRECATED_SUPPRESS);
2531 :
2532 359715 : bool consteval_seen = false;
2533 :
2534 5725173 : for (auto &e: CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
2535 : {
2536 4646028 : tree fnaddr = e.value;
2537 :
2538 4646028 : STRIP_NOPS (fnaddr);
2539 :
2540 7093739 : if (TREE_CODE (fnaddr) != ADDR_EXPR
2541 4646028 : && TREE_CODE (fnaddr) != FDESC_EXPR)
2542 : /* This entry is an offset: a virtual base class offset, a
2543 : virtual call offset, an RTTI offset, etc. */
2544 2447711 : continue;
2545 :
2546 2198317 : tree fn = TREE_OPERAND (fnaddr, 0);
2547 3959034 : if (TREE_CODE (fn) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (fn))
2548 : {
2549 1265 : if (!consteval_seen)
2550 : {
2551 438 : consteval_seen = true;
2552 438 : consteval_vtables.safe_push (decl);
2553 : }
2554 1265 : continue;
2555 : }
2556 2197052 : TREE_ADDRESSABLE (fn) = 1;
2557 : /* When we don't have vcall offsets, we output thunks whenever
2558 : we output the vtables that contain them. With vcall offsets,
2559 : we know all the thunks we'll need when we emit a virtual
2560 : function, so we emit the thunks there instead. */
2561 2197052 : if (DECL_THUNK_P (fn))
2562 3891 : use_thunk (fn, /*emit_p=*/0);
2563 : /* Set the location, as marking the function could cause
2564 : instantiation. We do not need to preserve the incoming
2565 : location, as we're called from c_parse_final_cleanups, which
2566 : takes care of that. */
2567 2197052 : input_location = DECL_SOURCE_LOCATION (fn);
2568 2197052 : mark_used (fn);
2569 : }
2570 359715 : }
2571 :
2572 : /* Replace any consteval functions in vtables with null pointers. */
2573 :
2574 : static void
2575 96776 : clear_consteval_vfns (vec<tree> &consteval_vtables)
2576 : {
2577 98056 : for (tree vtable : consteval_vtables)
2578 3478 : for (constructor_elt &elt : CONSTRUCTOR_ELTS (DECL_INITIAL (vtable)))
2579 : {
2580 2164 : tree fn = cp_get_fndecl_from_callee (elt.value, /*fold*/false);
2581 3446 : if (fn && DECL_IMMEDIATE_FUNCTION_P (fn))
2582 1267 : elt.value = build_zero_cst (vtable_entry_type);
2583 : }
2584 96776 : }
2585 :
2586 : /* Adjust the TLS model on variable DECL if need be, typically after
2587 : the linkage of DECL has been modified. */
2588 :
2589 : static void
2590 56933211 : adjust_var_decl_tls_model (tree decl)
2591 : {
2592 56933211 : if (CP_DECL_THREAD_LOCAL_P (decl)
2593 56933211 : && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
2594 296 : set_decl_tls_model (decl, decl_default_tls_model (decl));
2595 56933211 : }
2596 :
2597 : /* Set DECL up to have the closest approximation of "initialized common"
2598 : linkage available. */
2599 :
2600 : void
2601 105402507 : comdat_linkage (tree decl)
2602 : {
2603 105402507 : if (flag_weak)
2604 105402483 : make_decl_one_only (decl, cxx_comdat_group (decl));
2605 24 : else if (TREE_CODE (decl) == FUNCTION_DECL
2606 24 : || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
2607 : /* We can just emit function and compiler-generated variables
2608 : statically; having multiple copies is (for the most part) only
2609 : a waste of space.
2610 :
2611 : There are two correctness issues, however: the address of a
2612 : template instantiation with external linkage should be the
2613 : same, independent of what translation unit asks for the
2614 : address, and this will not hold when we emit multiple copies of
2615 : the function. However, there's little else we can do.
2616 :
2617 : Also, by default, the typeinfo implementation assumes that
2618 : there will be only one copy of the string used as the name for
2619 : each type. Therefore, if weak symbols are unavailable, the
2620 : run-time library should perform a more conservative check; it
2621 : should perform a string comparison, rather than an address
2622 : comparison. */
2623 24 : TREE_PUBLIC (decl) = 0;
2624 : else
2625 : {
2626 : /* Static data member template instantiations, however, cannot
2627 : have multiple copies. */
2628 0 : if (DECL_INITIAL (decl) == 0
2629 0 : || DECL_INITIAL (decl) == error_mark_node)
2630 0 : DECL_COMMON (decl) = 1;
2631 0 : else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2632 : {
2633 0 : DECL_COMMON (decl) = 1;
2634 0 : DECL_INITIAL (decl) = error_mark_node;
2635 : }
2636 0 : else if (!DECL_EXPLICIT_INSTANTIATION (decl))
2637 : {
2638 : /* We can't do anything useful; leave vars for explicit
2639 : instantiation. */
2640 0 : DECL_EXTERNAL (decl) = 1;
2641 0 : DECL_NOT_REALLY_EXTERN (decl) = 0;
2642 : }
2643 : }
2644 :
2645 105402507 : if (TREE_PUBLIC (decl))
2646 105402483 : DECL_COMDAT (decl) = 1;
2647 :
2648 105402507 : if (VAR_P (decl))
2649 56932559 : adjust_var_decl_tls_model (decl);
2650 105402507 : }
2651 :
2652 : /* For win32 we also want to put explicit instantiations in
2653 : linkonce sections, so that they will be merged with implicit
2654 : instantiations; otherwise we get duplicate symbol errors.
2655 : For Darwin we do not want explicit instantiations to be
2656 : linkonce. */
2657 :
2658 : void
2659 680968 : maybe_make_one_only (tree decl)
2660 : {
2661 : /* We used to say that this was not necessary on targets that support weak
2662 : symbols, because the implicit instantiations will defer to the explicit
2663 : one. However, that's not actually the case in SVR4; a strong definition
2664 : after a weak one is an error. Also, not making explicit
2665 : instantiations one_only means that we can end up with two copies of
2666 : some template instantiations. */
2667 680968 : if (! flag_weak)
2668 : return;
2669 :
2670 : /* These are not to be output. */
2671 680965 : if (consteval_only_p (decl))
2672 : return;
2673 :
2674 : /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
2675 : we can get away with not emitting them if they aren't used. We need
2676 : to for variables so that cp_finish_decl will update their linkage,
2677 : because their DECL_INITIAL may not have been set properly yet. */
2678 :
2679 677246 : if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
2680 : || (! DECL_EXPLICIT_INSTANTIATION (decl)
2681 : && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
2682 : {
2683 677246 : make_decl_one_only (decl, cxx_comdat_group (decl));
2684 :
2685 677246 : if (VAR_P (decl))
2686 : {
2687 652 : varpool_node *node = varpool_node::get_create (decl);
2688 652 : DECL_COMDAT (decl) = 1;
2689 : /* Mark it needed so we don't forget to emit it. */
2690 652 : node->forced_by_abi = true;
2691 652 : TREE_USED (decl) = 1;
2692 :
2693 652 : adjust_var_decl_tls_model (decl);
2694 : }
2695 : }
2696 : }
2697 :
2698 : /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
2699 : This predicate will give the right answer during parsing of the
2700 : function, which other tests may not. */
2701 :
2702 : bool
2703 35544269 : vague_linkage_p (tree decl)
2704 : {
2705 35544272 : if (!TREE_PUBLIC (decl))
2706 : {
2707 : /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
2708 : maybe-in-charge 'tor variants; in that case we need to check one of
2709 : the "clones" for the real linkage. But only in that case; before
2710 : maybe_clone_body we haven't yet copied the linkage to the clones. */
2711 8154 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
2712 183 : && !DECL_ABSTRACT_P (decl)
2713 3 : && DECL_CHAIN (decl)
2714 4162 : && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
2715 3 : return vague_linkage_p (DECL_CHAIN (decl));
2716 :
2717 4156 : gcc_checking_assert (!DECL_COMDAT (decl));
2718 : return false;
2719 : }
2720 : /* Unfortunately, import_export_decl has not always been called
2721 : before the function is processed, so we cannot simply check
2722 : DECL_COMDAT. */
2723 35540113 : if (DECL_COMDAT (decl)
2724 927693 : || (TREE_CODE (decl) == FUNCTION_DECL
2725 915891 : && DECL_NONGNU_INLINE_P (decl))
2726 65213 : || (DECL_LANG_SPECIFIC (decl)
2727 64675 : && DECL_TEMPLOID_INSTANTIATION (decl))
2728 35576406 : || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
2729 35509275 : return true;
2730 30838 : else if (DECL_FUNCTION_SCOPE_P (decl))
2731 : /* A local static in an inline effectively has vague linkage. */
2732 0 : return (TREE_STATIC (decl)
2733 0 : && vague_linkage_p (DECL_CONTEXT (decl)));
2734 : else
2735 : return false;
2736 : }
2737 :
2738 : /* Determine whether or not we want to specifically import or export CTYPE,
2739 : using various heuristics. */
2740 :
2741 : static void
2742 3825148 : import_export_class (tree ctype)
2743 : {
2744 : /* -1 for imported, 1 for exported. */
2745 3825148 : int import_export = 0;
2746 :
2747 : /* It only makes sense to call this function at EOF. The reason is
2748 : that this function looks at whether or not the first non-inline
2749 : non-abstract virtual member function has been defined in this
2750 : translation unit. But, we can't possibly know that until we've
2751 : seen the entire translation unit. */
2752 3825148 : gcc_assert (at_eof);
2753 :
2754 3825148 : if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2755 : return;
2756 :
2757 : /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2758 : we will have CLASSTYPE_INTERFACE_ONLY set but not
2759 : CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2760 : heuristic because someone will supply a #pragma implementation
2761 : elsewhere, and deducing it here would produce a conflict. */
2762 2070824 : if (CLASSTYPE_INTERFACE_ONLY (ctype))
2763 : return;
2764 :
2765 2070824 : if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2766 : import_export = -1;
2767 2070824 : else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2768 : import_export = 1;
2769 2070824 : else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2770 2070824 : && !flag_implicit_templates)
2771 : /* For a template class, without -fimplicit-templates, check the
2772 : repository. If the virtual table is assigned to this
2773 : translation unit, then export the class; otherwise, import
2774 : it. */
2775 : import_export = -1;
2776 2070179 : else if (TYPE_CONTAINS_VPTR_P (ctype))
2777 : {
2778 1902731 : tree cdecl = TYPE_NAME (ctype);
2779 1906476 : if (DECL_LANG_SPECIFIC (cdecl) && DECL_MODULE_ATTACH_P (cdecl))
2780 : /* For class types attached to a named module, the ABI specifies
2781 : that the tables are uniquely emitted in the object for the
2782 : module unit in which it is defined. */
2783 1740 : import_export = (DECL_MODULE_IMPORT_P (cdecl) ? -1 : 1);
2784 : else
2785 : {
2786 : /* The ABI specifies that the virtual table and associated
2787 : information are emitted with the key method, if any. */
2788 1902536 : tree method = CLASSTYPE_KEY_METHOD (ctype);
2789 : /* If weak symbol support is not available, then we must be
2790 : careful not to emit the vtable when the key function is
2791 : inline. An inline function can be defined in multiple
2792 : translation units. If we were to emit the vtable in each
2793 : translation unit containing a definition, we would get
2794 : multiple definition errors at link-time. */
2795 1878919 : if (method && (flag_weak || ! DECL_NONGNU_INLINE_P (method)))
2796 568235 : import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2797 : }
2798 : }
2799 :
2800 : /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2801 : a definition anywhere else. */
2802 2070824 : if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2803 : import_export = 0;
2804 :
2805 : /* Allow back ends the chance to overrule the decision. */
2806 2070824 : if (targetm.cxx.import_export_class)
2807 0 : import_export = targetm.cxx.import_export_class (ctype, import_export);
2808 :
2809 2070824 : if (import_export)
2810 : {
2811 569075 : SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2812 569075 : CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2813 : }
2814 : }
2815 :
2816 : /* Return true if VAR has already been provided to the back end; in that
2817 : case VAR should not be modified further by the front end. */
2818 : static bool
2819 100782684 : var_finalized_p (tree var)
2820 : {
2821 0 : return varpool_node::get_create (var)->definition;
2822 : }
2823 :
2824 : /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2825 : must be emitted in this translation unit. Mark it as such. */
2826 :
2827 : void
2828 49714 : mark_needed (tree decl)
2829 : {
2830 : /* These are not to be output. */
2831 49714 : if (consteval_only_p (decl))
2832 : return;
2833 :
2834 49709 : TREE_USED (decl) = 1;
2835 49709 : if (TREE_CODE (decl) == FUNCTION_DECL)
2836 : {
2837 : /* Extern inline functions don't become needed when referenced.
2838 : If we know a method will be emitted in other TU and no new
2839 : functions can be marked reachable, just use the external
2840 : definition. */
2841 41643 : struct cgraph_node *node = cgraph_node::get_create (decl);
2842 41643 : node->forced_by_abi = true;
2843 :
2844 : /* #pragma interface can call mark_needed for
2845 : maybe-in-charge 'tors; mark the clones as well. */
2846 41643 : tree clone;
2847 50031 : FOR_EACH_CLONE (clone, decl)
2848 8388 : mark_needed (clone);
2849 : }
2850 8066 : else if (VAR_P (decl))
2851 : {
2852 8066 : varpool_node *node = varpool_node::get_create (decl);
2853 : /* C++ frontend use mark_decl_references to force COMDAT variables
2854 : to be output that might appear dead otherwise. */
2855 8066 : node->forced_by_abi = true;
2856 : }
2857 : }
2858 :
2859 : /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2860 : returns true if a definition of this entity should be provided in
2861 : this object file. Callers use this function to determine whether
2862 : or not to let the back end know that a definition of DECL is
2863 : available in this translation unit. */
2864 :
2865 : bool
2866 314421338 : decl_needed_p (tree decl)
2867 : {
2868 314421338 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2869 : /* This function should only be called at the end of the translation
2870 : unit. We cannot be sure of whether or not something will be
2871 : COMDAT until that point. */
2872 314421338 : gcc_assert (at_eof);
2873 :
2874 : /* All entities with external linkage that are not COMDAT/EXTERN should be
2875 : emitted; they may be referred to from other object files. */
2876 314421338 : if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2877 : return true;
2878 :
2879 : /* Functions marked "dllexport" must be emitted so that they are
2880 : visible to other DLLs. */
2881 314421058 : if (flag_keep_inline_dllexport
2882 314421058 : && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2883 : return true;
2884 :
2885 : /* When not optimizing, do not bother to produce definitions for extern
2886 : symbols. */
2887 388640027 : if (DECL_REALLY_EXTERN (decl)
2888 0 : && ((TREE_CODE (decl) != FUNCTION_DECL
2889 0 : && !optimize)
2890 0 : || (TREE_CODE (decl) == FUNCTION_DECL
2891 0 : && !opt_for_fn (decl, optimize)))
2892 314421058 : && !lookup_attribute ("always_inline", decl))
2893 : return false;
2894 :
2895 : /* If this entity was used, let the back end see it; it will decide
2896 : whether or not to emit it into the object file. */
2897 314421058 : if (TREE_USED (decl))
2898 : return true;
2899 :
2900 : /* Virtual functions might be needed for devirtualization. */
2901 65105396 : if (flag_devirtualize
2902 63153707 : && TREE_CODE (decl) == FUNCTION_DECL
2903 127091116 : && DECL_VIRTUAL_P (decl))
2904 : return true;
2905 :
2906 : /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2907 : reference to DECL might cause it to be emitted later. */
2908 : return false;
2909 : }
2910 :
2911 : /* If necessary, write out the vtables for the dynamic class CTYPE.
2912 : Returns true if any vtables were emitted. */
2913 :
2914 : static bool
2915 3574397 : maybe_emit_vtables (tree ctype, vec<tree> &consteval_vtables)
2916 : {
2917 3574397 : tree vtbl;
2918 3574397 : tree primary_vtbl;
2919 3574397 : int needed = 0;
2920 3574397 : varpool_node *current = NULL, *last = NULL;
2921 :
2922 : /* If the vtables for this class have already been emitted there is
2923 : nothing more to do. */
2924 3574397 : primary_vtbl = CLASSTYPE_VTABLES (ctype);
2925 3574397 : if (var_finalized_p (primary_vtbl))
2926 : return false;
2927 : /* Ignore dummy vtables made by get_vtable_decl. */
2928 3574397 : if (TREE_TYPE (primary_vtbl) == void_type_node)
2929 : return false;
2930 :
2931 : /* On some targets, we cannot determine the key method until the end
2932 : of the translation unit -- which is when this function is
2933 : called. */
2934 3574397 : if (!targetm.cxx.key_method_may_be_inline ())
2935 0 : determine_key_method (ctype);
2936 :
2937 : /* See if any of the vtables are needed. */
2938 8760895 : for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2939 : {
2940 5186498 : import_export_decl (vtbl);
2941 5186498 : if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2942 : needed = 1;
2943 : }
2944 3574397 : if (!needed)
2945 : {
2946 : /* If the references to this class' vtables are optimized away,
2947 : still emit the appropriate debugging information. See
2948 : dfs_debug_mark. */
2949 3234569 : if (DECL_COMDAT (primary_vtbl)
2950 3234569 : && CLASSTYPE_DEBUG_REQUESTED (ctype))
2951 37586 : note_debug_info_needed (ctype);
2952 3234569 : return false;
2953 : }
2954 :
2955 : /* The ABI requires that we emit all of the vtables if we emit any
2956 : of them. */
2957 699543 : for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2958 : {
2959 : /* Mark entities references from the virtual table as used. */
2960 359715 : mark_vtable_entries (vtbl, consteval_vtables);
2961 :
2962 359715 : if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2963 : {
2964 0 : vec<tree, va_gc> *cleanups = NULL;
2965 0 : tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2966 : LOOKUP_NORMAL);
2967 :
2968 : /* It had better be all done at compile-time. */
2969 0 : gcc_assert (!expr && !cleanups);
2970 : }
2971 :
2972 : /* Write it out. */
2973 359715 : DECL_EXTERNAL (vtbl) = 0;
2974 359715 : rest_of_decl_compilation (vtbl, 1, 1);
2975 :
2976 : /* Because we're only doing syntax-checking, we'll never end up
2977 : actually marking the variable as written. */
2978 359715 : if (flag_syntax_only)
2979 33 : TREE_ASM_WRITTEN (vtbl) = 1;
2980 359682 : else if (DECL_ONE_ONLY (vtbl))
2981 : {
2982 358960 : current = varpool_node::get_create (vtbl);
2983 358960 : if (last)
2984 19793 : current->add_to_same_comdat_group (last);
2985 : last = current;
2986 : }
2987 : }
2988 :
2989 : /* For abstract classes, the destructor has been removed from the
2990 : vtable (in class.cc's build_vtbl_initializer). For a compiler-
2991 : generated destructor, it hence might not have been generated in
2992 : this translation unit - and with '#pragma interface' it might
2993 : never get generated. */
2994 339828 : if (CLASSTYPE_PURE_VIRTUALS (ctype)
2995 108029 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype)
2996 68628 : && !CLASSTYPE_LAZY_DESTRUCTOR (ctype)
2997 408453 : && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype)))
2998 65 : note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype));
2999 :
3000 : /* Since we're writing out the vtable here, also write the debug
3001 : info. */
3002 339828 : note_debug_info_needed (ctype);
3003 :
3004 339828 : return true;
3005 : }
3006 :
3007 : /* A special return value from type_visibility meaning internal
3008 : linkage. */
3009 :
3010 : enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
3011 :
3012 : static int expr_visibility (tree);
3013 : static int type_visibility (tree);
3014 :
3015 : /* walk_tree helper function for type_visibility. */
3016 :
3017 : static tree
3018 3884993698 : min_vis_r (tree *tp, int *walk_subtrees, void *data)
3019 : {
3020 3884993698 : int *vis_p = (int *)data;
3021 3884993698 : int this_vis = VISIBILITY_DEFAULT;
3022 3884993698 : if (! TYPE_P (*tp))
3023 768498996 : *walk_subtrees = 0;
3024 3116494702 : else if (OVERLOAD_TYPE_P (*tp)
3025 4081391330 : && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
3026 : {
3027 926774 : this_vis = VISIBILITY_ANON;
3028 926774 : *walk_subtrees = 0;
3029 : }
3030 3115567928 : else if (CLASS_TYPE_P (*tp))
3031 : {
3032 937772873 : this_vis = CLASSTYPE_VISIBILITY (*tp);
3033 937772873 : *walk_subtrees = 0;
3034 : }
3035 2177795055 : else if (TREE_CODE (*tp) == ARRAY_TYPE
3036 2177795055 : && uses_template_parms (TYPE_DOMAIN (*tp)))
3037 400829 : this_vis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp)));
3038 :
3039 3884993698 : if (this_vis > *vis_p)
3040 1619968 : *vis_p = this_vis;
3041 :
3042 : /* Tell cp_walk_subtrees to look through typedefs. */
3043 3884993698 : if (*walk_subtrees == 1)
3044 2177795055 : *walk_subtrees = 2;
3045 :
3046 3884993698 : return NULL;
3047 : }
3048 :
3049 : /* walk_tree helper function for expr_visibility. */
3050 :
3051 : static tree
3052 251808454 : min_vis_expr_r (tree *tp, int *walk_subtrees, void *data)
3053 : {
3054 251808454 : int *vis_p = (int *)data;
3055 251808454 : int tpvis = VISIBILITY_DEFAULT;
3056 :
3057 251808454 : tree t = *tp;
3058 251808454 : if (TREE_CODE (t) == PTRMEM_CST)
3059 657 : t = PTRMEM_CST_MEMBER (t);
3060 :
3061 251808454 : if (TREE_CODE (t) == TEMPLATE_DECL)
3062 : {
3063 6721838 : if (DECL_ALIAS_TEMPLATE_P (t) || concept_definition_p (t))
3064 : /* FIXME: We don't maintain TREE_PUBLIC / DECL_VISIBILITY for
3065 : alias templates so we can't trust it here (PR107906). Ditto
3066 : for concepts. */
3067 : return NULL_TREE;
3068 : t = DECL_TEMPLATE_RESULT (t);
3069 : if (!t)
3070 : return NULL_TREE;
3071 : }
3072 :
3073 251138263 : switch (TREE_CODE (t))
3074 : {
3075 8312693 : case CAST_EXPR:
3076 8312693 : case IMPLICIT_CONV_EXPR:
3077 8312693 : case STATIC_CAST_EXPR:
3078 8312693 : case REINTERPRET_CAST_EXPR:
3079 8312693 : case CONST_CAST_EXPR:
3080 8312693 : case DYNAMIC_CAST_EXPR:
3081 8312693 : case NEW_EXPR:
3082 8312693 : case CONSTRUCTOR:
3083 8312693 : case LAMBDA_EXPR:
3084 8312693 : case TYPE_DECL:
3085 8312693 : tpvis = type_visibility (TREE_TYPE (t));
3086 8312693 : break;
3087 :
3088 4124 : case ADDR_EXPR:
3089 4124 : t = TREE_OPERAND (t, 0);
3090 4124 : if (VAR_P (t))
3091 : /* If a variable has its address taken, the lvalue-rvalue conversion is
3092 : not applied, so skip that case. */
3093 1945 : goto addressable;
3094 : break;
3095 :
3096 8430636 : case VAR_DECL:
3097 8430636 : case FUNCTION_DECL:
3098 8430636 : if (decl_constant_var_p (t))
3099 : /* The ODR allows definitions in different TUs to refer to distinct
3100 : constant variables with internal or no linkage, so such a reference
3101 : shouldn't affect visibility if the lvalue-rvalue conversion is
3102 : applied (PR110323). We still want to restrict visibility according
3103 : to the type of the declaration however. */
3104 : {
3105 4913599 : tpvis = type_visibility (TREE_TYPE (t));
3106 4913599 : break;
3107 : }
3108 3517037 : addressable:
3109 : /* For _DECLs with no linkage refer to the linkage of the containing
3110 : entity that does have a name with linkage. */
3111 3519189 : if (decl_linkage (t) == lk_none)
3112 21229 : tpvis = expr_visibility (DECL_CONTEXT (t));
3113 : /* Decls that have had their visibility constrained will report
3114 : as external linkage, but we still want to transitively constrain
3115 : if we refer to them, so just check TREE_PUBLIC instead. */
3116 3497960 : else if (!TREE_PUBLIC (t))
3117 : tpvis = VISIBILITY_ANON;
3118 : else
3119 3496494 : tpvis = DECL_VISIBILITY (t);
3120 : break;
3121 :
3122 957 : case FIELD_DECL:
3123 957 : tpvis = type_visibility (DECL_CONTEXT (t));
3124 957 : break;
3125 :
3126 877 : case REFLECT_EXPR:
3127 877 : tree r;
3128 877 : r = REFLECT_EXPR_HANDLE (t);
3129 877 : switch (REFLECT_EXPR_KIND (t))
3130 : {
3131 21 : case REFLECT_BASE:
3132 : /* For direct base class relationship, determine visibility
3133 : from both D and B types. */
3134 21 : tpvis = type_visibility (BINFO_TYPE (r));
3135 21 : if (tpvis > *vis_p)
3136 1 : *vis_p = tpvis;
3137 21 : tpvis = type_visibility (direct_base_derived (r));
3138 21 : *walk_subtrees = 0;
3139 21 : break;
3140 19 : case REFLECT_DATA_MEMBER_SPEC:
3141 : /* For data member description determine visibility
3142 : from the type. */
3143 19 : tpvis = type_visibility (TREE_VEC_ELT (r, 0));
3144 19 : *walk_subtrees = 0;
3145 19 : break;
3146 25 : case REFLECT_PARM:
3147 : /* For function parameter reflection determine visibility
3148 : based on parent_of. */
3149 25 : tpvis = expr_visibility (DECL_CONTEXT (r));
3150 25 : *walk_subtrees = 0;
3151 25 : break;
3152 3 : case REFLECT_ANNOTATION:
3153 : /* Annotations are always local to the TU. */
3154 3 : tpvis = VISIBILITY_ANON;
3155 3 : *walk_subtrees = 0;
3156 3 : break;
3157 10 : case REFLECT_OBJECT:
3158 10 : r = get_base_address (r);
3159 809 : gcc_fallthrough ();
3160 809 : default:
3161 809 : if (TYPE_P (r))
3162 : {
3163 168 : tpvis = type_visibility (r);
3164 168 : *walk_subtrees = 0;
3165 168 : break;
3166 : }
3167 641 : if (VAR_P (r) || TREE_CODE (r) == PARM_DECL)
3168 : {
3169 207 : t = r;
3170 207 : goto addressable;
3171 : }
3172 : break;
3173 : }
3174 : break;
3175 :
3176 : default:
3177 : break;
3178 : }
3179 :
3180 251138263 : if (tpvis > *vis_p)
3181 3305 : *vis_p = tpvis;
3182 :
3183 : return NULL_TREE;
3184 : }
3185 :
3186 : /* Returns the visibility of TYPE, which is the minimum visibility of its
3187 : component types. */
3188 :
3189 : static int
3190 1121996648 : type_visibility (tree type)
3191 : {
3192 1121996648 : int vis = VISIBILITY_DEFAULT;
3193 1121996648 : cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
3194 1121996648 : return vis;
3195 : }
3196 :
3197 : /* Returns the visibility of an expression EXPR that appears in the signature
3198 : of a function template, which is the minimum visibility of names that appear
3199 : in its mangling. */
3200 :
3201 : static int
3202 85191629 : expr_visibility (tree expr)
3203 : {
3204 85191629 : int vis = VISIBILITY_DEFAULT;
3205 85191629 : cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis);
3206 85191629 : return vis;
3207 : }
3208 :
3209 : /* Limit the visibility of DECL to VISIBILITY, if not explicitly
3210 : specified (or if VISIBILITY is static). If TMPL is true, this
3211 : constraint is for a template argument, and takes precedence
3212 : over explicitly-specified visibility on the template. */
3213 :
3214 : static void
3215 75563139 : constrain_visibility (tree decl, int visibility, bool tmpl)
3216 : {
3217 75563139 : if (visibility == VISIBILITY_ANON)
3218 : {
3219 : /* extern "C" declarations aren't affected by the anonymous
3220 : namespace. */
3221 1343730 : if (!DECL_EXTERN_C_P (decl))
3222 : {
3223 1343554 : TREE_PUBLIC (decl) = 0;
3224 1343554 : DECL_WEAK (decl) = 0;
3225 1343554 : DECL_COMMON (decl) = 0;
3226 1343554 : DECL_COMDAT (decl) = false;
3227 1343554 : if (VAR_OR_FUNCTION_DECL_P (decl))
3228 : {
3229 395505 : struct symtab_node *snode = symtab_node::get (decl);
3230 :
3231 395505 : if (snode)
3232 33148 : snode->set_comdat_group (NULL);
3233 : }
3234 1343554 : DECL_INTERFACE_KNOWN (decl) = 1;
3235 1343554 : if (DECL_LANG_SPECIFIC (decl))
3236 399144 : DECL_NOT_REALLY_EXTERN (decl) = 1;
3237 : }
3238 : }
3239 74219409 : else if (visibility > DECL_VISIBILITY (decl)
3240 74219409 : && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
3241 : {
3242 309 : DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
3243 : /* This visibility was not specified. */
3244 309 : DECL_VISIBILITY_SPECIFIED (decl) = false;
3245 : }
3246 75563139 : }
3247 :
3248 : /* Constrain the visibility of DECL based on the visibility of its template
3249 : arguments. */
3250 :
3251 : static void
3252 339161493 : constrain_visibility_for_template (tree decl, tree targs)
3253 : {
3254 : /* If this is a template instantiation, check the innermost
3255 : template args for visibility constraints. The outer template
3256 : args are covered by the class check. */
3257 339161493 : tree args = INNERMOST_TEMPLATE_ARGS (targs);
3258 339161493 : int i;
3259 990037730 : for (i = TREE_VEC_LENGTH (args); i > 0; --i)
3260 : {
3261 650876237 : int vis = 0;
3262 :
3263 650876237 : tree arg = TREE_VEC_ELT (args, i-1);
3264 650876237 : if (TYPE_P (arg))
3265 567549099 : vis = type_visibility (arg);
3266 : else
3267 83327138 : vis = expr_visibility (arg);
3268 650876237 : if (vis)
3269 342992 : constrain_visibility (decl, vis, true);
3270 : }
3271 339161493 : }
3272 :
3273 : /* Like c_determine_visibility, but with additional C++-specific
3274 : behavior.
3275 :
3276 : Function-scope entities can rely on the function's visibility because
3277 : it is set in start_preparsed_function.
3278 :
3279 : Class-scope entities cannot rely on the class's visibility until the end
3280 : of the enclosing class definition.
3281 :
3282 : Note that because namespaces have multiple independent definitions,
3283 : namespace visibility is handled elsewhere using the #pragma visibility
3284 : machinery rather than by decorating the namespace declaration.
3285 :
3286 : The goal is for constraints from the type to give a diagnostic, and
3287 : other constraints to be applied silently. */
3288 :
3289 : void
3290 671009693 : determine_visibility (tree decl)
3291 : {
3292 : /* Remember that all decls get VISIBILITY_DEFAULT when built. */
3293 :
3294 : /* Only relevant for names with external linkage. */
3295 671009693 : if (!TREE_PUBLIC (decl))
3296 : return;
3297 :
3298 : /* Cloned constructors and destructors get the same visibility as
3299 : the underlying function. That should be set up in
3300 : maybe_clone_body. */
3301 624126968 : gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
3302 :
3303 624126968 : bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
3304 624126968 : enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
3305 :
3306 : /* The decl may be a template instantiation, which could influence
3307 : visibilty. */
3308 624126968 : tree template_decl = NULL_TREE;
3309 624126968 : if (TREE_CODE (decl) == TYPE_DECL)
3310 : {
3311 173566757 : if (CLASS_TYPE_P (TREE_TYPE (decl)))
3312 : {
3313 170624485 : if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
3314 346646931 : template_decl = decl;
3315 : }
3316 2942272 : else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3317 346646931 : template_decl = decl;
3318 : }
3319 450560211 : else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3320 : template_decl = decl;
3321 :
3322 624126968 : if (TREE_CODE (decl) == TYPE_DECL
3323 343158597 : && LAMBDA_TYPE_P (TREE_TYPE (decl))
3324 627021169 : && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl)) != error_mark_node)
3325 2889004 : if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
3326 : {
3327 : /* The lambda's visibility is limited by that of its extra
3328 : scope. */
3329 1442408 : int vis = 0;
3330 1442408 : if (TYPE_P (extra))
3331 0 : vis = type_visibility (extra);
3332 : else
3333 1442408 : vis = expr_visibility (extra);
3334 1442408 : constrain_visibility (decl, vis, false);
3335 : }
3336 :
3337 : /* If DECL is a member of a class, visibility specifiers on the
3338 : class can influence the visibility of the DECL. */
3339 624126968 : tree class_type = NULL_TREE;
3340 624126968 : if (DECL_CLASS_SCOPE_P (decl))
3341 315675070 : class_type = DECL_CONTEXT (decl);
3342 : else
3343 : {
3344 : /* Not a class member. */
3345 :
3346 : /* Virtual tables have DECL_CONTEXT set to their associated class,
3347 : so they are automatically handled above. */
3348 308451898 : gcc_assert (!VAR_P (decl)
3349 : || !DECL_VTABLE_OR_VTT_P (decl));
3350 :
3351 308451898 : if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
3352 : {
3353 : /* Local statics and classes get the visibility of their
3354 : containing function by default, except that
3355 : -fvisibility-inlines-hidden doesn't affect them. */
3356 2254926 : tree fn = DECL_CONTEXT (decl);
3357 2254926 : if (DECL_VISIBILITY_SPECIFIED (fn))
3358 : {
3359 2184487 : DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
3360 2184487 : DECL_VISIBILITY_SPECIFIED (decl) =
3361 2184487 : DECL_VISIBILITY_SPECIFIED (fn);
3362 : }
3363 : else
3364 : {
3365 70439 : if (DECL_CLASS_SCOPE_P (fn))
3366 37324 : determine_visibility_from_class (decl, DECL_CONTEXT (fn));
3367 33115 : else if (determine_hidden_inline (fn))
3368 : {
3369 6 : DECL_VISIBILITY (decl) = default_visibility;
3370 6 : DECL_VISIBILITY_SPECIFIED (decl) =
3371 6 : visibility_options.inpragma;
3372 : }
3373 : else
3374 : {
3375 33109 : DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
3376 33109 : DECL_VISIBILITY_SPECIFIED (decl) =
3377 33109 : DECL_VISIBILITY_SPECIFIED (fn);
3378 : }
3379 : }
3380 :
3381 : /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
3382 : but have no TEMPLATE_INFO, so don't try to check it. */
3383 : template_decl = NULL_TREE;
3384 : }
3385 46319955 : else if (VAR_P (decl) && DECL_TINFO_P (decl)
3386 311111697 : && flag_visibility_ms_compat)
3387 : {
3388 : /* Under -fvisibility-ms-compat, types are visible by default,
3389 : even though their contents aren't. */
3390 84 : tree underlying_type = TREE_TYPE (DECL_NAME (decl));
3391 84 : int underlying_vis = type_visibility (underlying_type);
3392 84 : if (underlying_vis == VISIBILITY_ANON
3393 84 : || (CLASS_TYPE_P (underlying_type)
3394 69 : && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
3395 54 : constrain_visibility (decl, underlying_vis, false);
3396 : else
3397 30 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3398 : }
3399 306196888 : else if (VAR_P (decl) && DECL_TINFO_P (decl))
3400 : {
3401 : /* tinfo visibility is based on the type it's for. */
3402 4914641 : constrain_visibility
3403 4914641 : (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
3404 :
3405 : /* Give the target a chance to override the visibility associated
3406 : with DECL. */
3407 4914641 : if (TREE_PUBLIC (decl)
3408 4912709 : && !DECL_REALLY_EXTERN (decl)
3409 4912709 : && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
3410 9797355 : && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
3411 107522 : targetm.cxx.determine_class_data_visibility (decl);
3412 : }
3413 301282247 : else if (template_decl)
3414 : /* Template instantiations and specializations get visibility based
3415 : on their template unless they override it with an attribute. */;
3416 101050068 : else if (! DECL_VISIBILITY_SPECIFIED (decl))
3417 : {
3418 93183482 : if (determine_hidden_inline (decl))
3419 12 : DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3420 : else
3421 : {
3422 : /* Set default visibility to whatever the user supplied with
3423 : #pragma GCC visibility or a namespace visibility attribute. */
3424 93183470 : DECL_VISIBILITY (decl) = default_visibility;
3425 93183470 : DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
3426 : }
3427 : }
3428 : }
3429 :
3430 421639863 : if (template_decl)
3431 : {
3432 : /* If the specialization doesn't specify visibility, use the
3433 : visibility from the template. */
3434 346068226 : tree tinfo = get_template_info (template_decl);
3435 346068226 : tree args = TI_ARGS (tinfo);
3436 346068226 : tree attribs = (TREE_CODE (decl) == TYPE_DECL
3437 346068226 : ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
3438 346068226 : : DECL_ATTRIBUTES (decl));
3439 346068226 : tree attr = lookup_attribute ("visibility", attribs);
3440 :
3441 346068226 : if (args != error_mark_node)
3442 : {
3443 346068226 : tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
3444 :
3445 346068226 : if (!DECL_VISIBILITY_SPECIFIED (decl))
3446 : {
3447 155833973 : if (!attr
3448 155833973 : && determine_hidden_inline (decl))
3449 33 : DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3450 : else
3451 : {
3452 155833940 : DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
3453 311667880 : DECL_VISIBILITY_SPECIFIED (decl)
3454 155833940 : = DECL_VISIBILITY_SPECIFIED (pattern);
3455 : }
3456 : }
3457 :
3458 346068226 : if (args
3459 : /* Template argument visibility outweighs #pragma or namespace
3460 : visibility, but not an explicit attribute. */
3461 346068226 : && !attr)
3462 : {
3463 346068205 : int depth = TMPL_ARGS_DEPTH (args);
3464 346068205 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (TI_TEMPLATE (tinfo)))
3465 : /* Class template args don't affect template friends. */;
3466 343437929 : else if (DECL_VISIBILITY_SPECIFIED (decl))
3467 : {
3468 : /* A class template member with explicit visibility
3469 : overrides the class visibility, so we need to apply
3470 : all the levels of template args directly. */
3471 : int i;
3472 610247798 : for (i = 1; i <= depth; ++i)
3473 : {
3474 309711567 : tree lev = TMPL_ARGS_LEVEL (args, i);
3475 309711567 : constrain_visibility_for_template (decl, lev);
3476 : }
3477 : }
3478 42901698 : else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
3479 : /* Limit visibility based on its template arguments. */
3480 29449926 : constrain_visibility_for_template (decl, args);
3481 : }
3482 : }
3483 : }
3484 :
3485 624126968 : if (class_type)
3486 315675070 : determine_visibility_from_class (decl, class_type);
3487 :
3488 624126968 : if (decl_internal_context_p (decl))
3489 : /* Names in an anonymous namespace get internal linkage. */
3490 281162 : constrain_visibility (decl, VISIBILITY_ANON, false);
3491 623845806 : else if (TREE_CODE (decl) != TYPE_DECL)
3492 : {
3493 : /* Propagate anonymity from type to decl. */
3494 450410560 : int tvis = type_visibility (TREE_TYPE (decl));
3495 450410560 : if (tvis == VISIBILITY_ANON
3496 450410560 : || ! DECL_VISIBILITY_SPECIFIED (decl))
3497 67848232 : constrain_visibility (decl, tvis, false);
3498 : }
3499 173435246 : else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
3500 : /* DR 757: A type without linkage shall not be used as the type of a
3501 : variable or function with linkage, unless
3502 : o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
3503 : o the variable or function is not used (3.2 [basic.def.odr]) or is
3504 : defined in the same translation unit.
3505 :
3506 : Since non-extern "C" decls need to be defined in the same
3507 : translation unit, we can make the type internal. */
3508 733650 : constrain_visibility (decl, VISIBILITY_ANON, false);
3509 :
3510 : /* If visibility changed and DECL already has DECL_RTL, ensure
3511 : symbol flags are updated. */
3512 624126968 : if ((DECL_VISIBILITY (decl) != orig_visibility
3513 623138969 : || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
3514 328018193 : && ((VAR_P (decl) && TREE_STATIC (decl))
3515 304008575 : || TREE_CODE (decl) == FUNCTION_DECL)
3516 930668816 : && DECL_RTL_SET_P (decl))
3517 0 : make_decl_rtl (decl);
3518 : }
3519 :
3520 : /* By default, static data members and function members receive
3521 : the visibility of their containing class. */
3522 :
3523 : static void
3524 315712394 : determine_visibility_from_class (tree decl, tree class_type)
3525 : {
3526 315712394 : if (DECL_VISIBILITY_SPECIFIED (decl))
3527 : return;
3528 :
3529 158516012 : if (determine_hidden_inline (decl))
3530 145 : DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3531 : else
3532 : {
3533 : /* Default to the class visibility. */
3534 158515867 : DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
3535 317031734 : DECL_VISIBILITY_SPECIFIED (decl)
3536 158515867 : = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
3537 : }
3538 :
3539 : /* Give the target a chance to override the visibility associated
3540 : with DECL. */
3541 158516012 : if (VAR_P (decl)
3542 16189724 : && TREE_PUBLIC (decl)
3543 16189712 : && (DECL_TINFO_P (decl) || DECL_VTABLE_OR_VTT_P (decl))
3544 2219788 : && !DECL_REALLY_EXTERN (decl)
3545 160735800 : && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
3546 75213 : targetm.cxx.determine_class_data_visibility (decl);
3547 : }
3548 :
3549 : /* Returns true iff DECL is an inline that should get hidden visibility
3550 : because of -fvisibility-inlines-hidden. */
3551 :
3552 : static bool
3553 407566582 : determine_hidden_inline (tree decl)
3554 : {
3555 407566582 : return (visibility_options.inlines_hidden
3556 : /* Don't do this for inline templates; specializations might not be
3557 : inline, and we don't want them to inherit the hidden
3558 : visibility. We'll set it here for all inline instantiations. */
3559 410 : && !processing_template_decl
3560 386 : && TREE_CODE (decl) == FUNCTION_DECL
3561 297 : && DECL_DECLARED_INLINE_P (decl)
3562 407566793 : && (! DECL_LANG_SPECIFIC (decl)
3563 211 : || ! DECL_EXPLICIT_INSTANTIATION (decl)));
3564 : }
3565 :
3566 : /* Constrain the visibility of a class TYPE based on the visibility of its
3567 : field types. Warn if any fields require lesser visibility. */
3568 :
3569 : void
3570 50309958 : constrain_class_visibility (tree type)
3571 : {
3572 50309958 : tree binfo;
3573 50309958 : tree t;
3574 50309958 : int i;
3575 :
3576 50309958 : int vis = type_visibility (type);
3577 :
3578 50309958 : if (vis == VISIBILITY_ANON
3579 50309958 : || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
3580 1289372 : return;
3581 :
3582 : /* Don't warn about visibility if the class has explicit visibility. */
3583 49020586 : if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
3584 48276650 : vis = VISIBILITY_INTERNAL;
3585 :
3586 403070991 : for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
3587 35894665 : if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
3588 389944912 : && !DECL_ARTIFICIAL (t))
3589 : {
3590 11759181 : tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
3591 11759181 : int subvis = type_visibility (ftype);
3592 :
3593 11759181 : if (subvis == VISIBILITY_ANON)
3594 : {
3595 110 : if (!in_main_input_context())
3596 : {
3597 27 : tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
3598 27 : if (nlt)
3599 : {
3600 6 : if (same_type_p (TREE_TYPE (t), nlt))
3601 6 : warning (OPT_Wsubobject_linkage, "\
3602 : %qT has a field %q#D whose type has no linkage",
3603 : type, t);
3604 : else
3605 0 : warning (OPT_Wsubobject_linkage, "\
3606 : %qT has a field %qD whose type depends on the type %qT which has no linkage",
3607 : type, t, nlt);
3608 : }
3609 21 : else if (cxx_dialect > cxx98
3610 21 : && !decl_anon_ns_mem_p (ftype))
3611 2 : warning (OPT_Wsubobject_linkage, "\
3612 : %qT has a field %q#D whose type has internal linkage",
3613 : type, t);
3614 : else // In C++98 this can only happen with unnamed namespaces.
3615 19 : warning (OPT_Wsubobject_linkage, "\
3616 : %qT has a field %q#D whose type uses the anonymous namespace",
3617 : type, t);
3618 : }
3619 : }
3620 11759071 : else if (MAYBE_CLASS_TYPE_P (ftype)
3621 4615816 : && vis < VISIBILITY_HIDDEN
3622 4615816 : && subvis >= VISIBILITY_HIDDEN)
3623 6 : warning (OPT_Wattributes, "\
3624 : %qT declared with greater visibility than the type of its field %qD",
3625 : type, t);
3626 : }
3627 :
3628 49020586 : binfo = TYPE_BINFO (type);
3629 72846233 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
3630 : {
3631 23825647 : tree btype = BINFO_TYPE (t);
3632 23825647 : int subvis = type_visibility (btype);
3633 :
3634 23825647 : if (subvis == VISIBILITY_ANON)
3635 : {
3636 32 : if (!in_main_input_context())
3637 : {
3638 18 : tree nlt = no_linkage_check (btype, /*relaxed_p=*/false);
3639 18 : if (nlt)
3640 : {
3641 6 : if (same_type_p (btype, nlt))
3642 6 : warning (OPT_Wsubobject_linkage, "\
3643 : %qT has a base %qT which has no linkage",
3644 : type, btype);
3645 : else
3646 0 : warning (OPT_Wsubobject_linkage, "\
3647 : %qT has a base %qT which depends on the type %qT which has no linkage",
3648 : type, btype, nlt);
3649 : }
3650 12 : else if (cxx_dialect > cxx98
3651 12 : && !decl_anon_ns_mem_p (btype))
3652 3 : warning (OPT_Wsubobject_linkage, "\
3653 : %qT has a base %qT which has internal linkage",
3654 : type, btype);
3655 : else // In C++98 this can only happen with unnamed namespaces.
3656 9 : warning (OPT_Wsubobject_linkage, "\
3657 : %qT has a base %qT which uses the anonymous namespace",
3658 : type, btype);
3659 : }
3660 : }
3661 23825615 : else if (vis < VISIBILITY_HIDDEN
3662 23825615 : && subvis >= VISIBILITY_HIDDEN)
3663 6 : warning (OPT_Wattributes, "\
3664 : %qT declared with greater visibility than its base %qT",
3665 6 : type, TREE_TYPE (t));
3666 : }
3667 : }
3668 :
3669 : /* Functions for adjusting the visibility of a tagged type and its nested
3670 : types and declarations when it gets a name for linkage purposes from a
3671 : typedef. */
3672 : // FIXME: It is now a DR for such a class type to contain anything
3673 : // other than C. So at minium most of this can probably be deleted.
3674 :
3675 : /* First reset the visibility of all the types. */
3676 :
3677 : static void
3678 1068254 : reset_type_linkage_1 (tree type)
3679 : {
3680 1068254 : set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
3681 1068254 : if (CLASS_TYPE_P (type))
3682 3599267 : for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
3683 2861736 : if (DECL_IMPLICIT_TYPEDEF_P (member))
3684 231119 : reset_type_linkage_1 (TREE_TYPE (member));
3685 1068254 : }
3686 :
3687 : /* Then reset the visibility of any static data members or member
3688 : functions that use those types. */
3689 :
3690 : static void
3691 103 : reset_decl_linkage (tree decl)
3692 : {
3693 103 : if (TREE_PUBLIC (decl))
3694 : return;
3695 103 : if (DECL_CLONED_FUNCTION_P (decl))
3696 : return;
3697 99 : TREE_PUBLIC (decl) = true;
3698 99 : DECL_INTERFACE_KNOWN (decl) = false;
3699 99 : determine_visibility (decl);
3700 99 : tentative_decl_linkage (decl);
3701 : }
3702 :
3703 : void
3704 837135 : reset_type_linkage (tree type)
3705 : {
3706 837135 : reset_type_linkage_1 (type);
3707 837135 : if (CLASS_TYPE_P (type))
3708 : {
3709 506424 : if (tree vt = CLASSTYPE_VTABLES (type))
3710 : {
3711 15 : tree name = mangle_vtbl_for_type (type);
3712 15 : DECL_NAME (vt) = name;
3713 15 : SET_DECL_ASSEMBLER_NAME (vt, name);
3714 15 : reset_decl_linkage (vt);
3715 : }
3716 506424 : if (!ANON_AGGR_TYPE_P (type))
3717 506287 : if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
3718 : {
3719 12 : tree name = mangle_typeinfo_for_type (type);
3720 12 : DECL_NAME (ti) = name;
3721 12 : SET_DECL_ASSEMBLER_NAME (ti, name);
3722 12 : TREE_TYPE (name) = type;
3723 12 : reset_decl_linkage (ti);
3724 : }
3725 2382484 : for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
3726 : {
3727 1876060 : tree mem = STRIP_TEMPLATE (m);
3728 1876060 : if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
3729 76 : reset_decl_linkage (mem);
3730 1875984 : else if (DECL_IMPLICIT_TYPEDEF_P (mem))
3731 121423 : reset_type_linkage (TREE_TYPE (mem));
3732 : }
3733 : }
3734 837135 : }
3735 :
3736 : /* Set up our initial idea of what the linkage of DECL should be. */
3737 :
3738 : void
3739 29756422 : tentative_decl_linkage (tree decl)
3740 : {
3741 29756422 : if (DECL_INTERFACE_KNOWN (decl))
3742 : /* We've already made a decision as to how this function will
3743 : be handled. */;
3744 29755286 : else if (vague_linkage_p (decl))
3745 : {
3746 29754698 : if (TREE_CODE (decl) == FUNCTION_DECL
3747 29754698 : && decl_defined_p (decl))
3748 : {
3749 29732108 : DECL_EXTERNAL (decl) = 1;
3750 29732108 : DECL_NOT_REALLY_EXTERN (decl) = 1;
3751 29732108 : note_vague_linkage_fn (decl);
3752 : /* A non-template inline function with external linkage will
3753 : always be COMDAT. As we must eventually determine the
3754 : linkage of all functions, and as that causes writes to
3755 : the data mapped in from the PCH file, it's advantageous
3756 : to mark the functions at this point. */
3757 29732108 : if (DECL_DECLARED_INLINE_P (decl))
3758 : {
3759 29729563 : if (!DECL_IMPLICIT_INSTANTIATION (decl)
3760 29729563 : || DECL_DEFAULTED_FN (decl))
3761 : {
3762 : /* This function must have external linkage, as
3763 : otherwise DECL_INTERFACE_KNOWN would have been
3764 : set. */
3765 24649472 : gcc_assert (TREE_PUBLIC (decl));
3766 24649472 : comdat_linkage (decl);
3767 24649472 : DECL_INTERFACE_KNOWN (decl) = 1;
3768 : }
3769 5080091 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
3770 : /* For implicit instantiations of cdtors try to make
3771 : it comdat, so that maybe_clone_body can use aliases.
3772 : See PR113208. */
3773 649832 : maybe_make_one_only (decl);
3774 : }
3775 : }
3776 22590 : else if (VAR_P (decl))
3777 22588 : maybe_commonize_var (decl);
3778 : }
3779 29756422 : }
3780 :
3781 : /* For a polymorphic class type CTYPE, whether its vtables are emitted in a
3782 : unique object as per the ABI. */
3783 :
3784 : static bool
3785 2845 : vtables_uniquely_emitted (tree ctype)
3786 : {
3787 : /* If the class is templated, the tables are emitted in every object that
3788 : references any of them. */
3789 2845 : if (CLASSTYPE_USE_TEMPLATE (ctype))
3790 : return false;
3791 :
3792 : /* Otherwise, if the class is attached to a module, the tables are uniquely
3793 : emitted in the object for the module unit in which it is defined. */
3794 2219 : tree cdecl = TYPE_NAME (ctype);
3795 2354 : if (DECL_LANG_SPECIFIC (cdecl) && DECL_MODULE_ATTACH_P (cdecl))
3796 : return true;
3797 :
3798 : /* Otherwise, if the class has a key function, the tables are emitted in the
3799 : object for the TU containing the definition of the key function. This is
3800 : unique if the key function is not inline. */
3801 2087 : tree key_method = CLASSTYPE_KEY_METHOD (ctype);
3802 4154 : if (key_method && !DECL_NONGNU_INLINE_P (key_method))
3803 2047 : return true;
3804 :
3805 : /* Otherwise, the tables are emitted in every object that references
3806 : any of them. */
3807 : return false;
3808 : }
3809 :
3810 : /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
3811 : for DECL has not already been determined, do so now by setting
3812 : DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
3813 : function is called entities with vague linkage whose definitions
3814 : are available must have TREE_PUBLIC set.
3815 :
3816 : If this function decides to place DECL in COMDAT, it will set
3817 : appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
3818 : the caller to decide whether or not to clear DECL_EXTERNAL. Some
3819 : callers defer that decision until it is clear that DECL is actually
3820 : required. */
3821 :
3822 : void
3823 226837903 : import_export_decl (tree decl)
3824 : {
3825 226837903 : bool comdat_p;
3826 226837903 : bool import_p;
3827 226837903 : tree class_type = NULL_TREE;
3828 :
3829 226837903 : if (DECL_INTERFACE_KNOWN (decl))
3830 : return;
3831 :
3832 : /* We cannot determine what linkage to give to an entity with vague
3833 : linkage until the end of the file. For example, a virtual table
3834 : for a class will be defined if and only if the key method is
3835 : defined in this translation unit. */
3836 39117839 : gcc_assert (at_eof);
3837 : /* Object file linkage for explicit instantiations is handled in
3838 : mark_decl_instantiated. For static variables in functions with
3839 : vague linkage, maybe_commonize_var is used.
3840 :
3841 : Therefore, the only declarations that should be provided to this
3842 : function are those with external linkage that are:
3843 :
3844 : * implicit instantiations of function templates
3845 :
3846 : * inline functions
3847 :
3848 : * inline variables
3849 :
3850 : * implicit instantiations of static data members of class
3851 : templates
3852 :
3853 : * virtual tables
3854 :
3855 : * typeinfo objects
3856 :
3857 : Furthermore, all entities that reach this point must have a
3858 : definition available in this translation unit.
3859 :
3860 : The following assertions check these conditions. */
3861 39117839 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3862 : /* Any code that creates entities with TREE_PUBLIC cleared should
3863 : also set DECL_INTERFACE_KNOWN. */
3864 39117839 : gcc_assert (TREE_PUBLIC (decl));
3865 39117839 : if (TREE_CODE (decl) == FUNCTION_DECL)
3866 20952060 : gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3867 : || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
3868 : || DECL_DECLARED_INLINE_P (decl));
3869 : else
3870 18165779 : gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3871 : || DECL_INLINE_VAR_P (decl)
3872 : || DECL_VTABLE_OR_VTT_P (decl)
3873 : || DECL_TINFO_P (decl));
3874 : /* Check that a definition of DECL is available in this translation
3875 : unit. */
3876 39117839 : gcc_assert (!DECL_REALLY_EXTERN (decl));
3877 :
3878 : /* Assume that DECL will not have COMDAT linkage. */
3879 39117839 : comdat_p = false;
3880 : /* Assume that DECL will not be imported into this translation
3881 : unit. */
3882 39117839 : import_p = false;
3883 :
3884 39117839 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
3885 : {
3886 1598703 : class_type = DECL_CONTEXT (decl);
3887 1598703 : import_export_class (class_type);
3888 1598703 : if (CLASSTYPE_INTERFACE_KNOWN (class_type)
3889 1598703 : && CLASSTYPE_INTERFACE_ONLY (class_type))
3890 : import_p = true;
3891 513507 : else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
3892 513507 : && !vtables_uniquely_emitted (class_type))
3893 : /* The ABI historically required that all virtual tables be
3894 : emitted with COMDAT linkage. However, on systems where
3895 : COMDAT symbols don't show up in the table of contents for
3896 : a static archive, or on systems without weak symbols (where
3897 : we approximate COMDAT linkage by using internal linkage),
3898 : the linker will report errors about undefined symbols because
3899 : it will not see the virtual table definition. Therefore,
3900 : in the case that we know that the virtual table will be
3901 : emitted in only one translation unit, we make the virtual
3902 : table an ordinary definition with external linkage. */
3903 3 : DECL_EXTERNAL (decl) = 0;
3904 513504 : else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
3905 : {
3906 : /* CLASS_TYPE is being exported from this translation unit,
3907 : so DECL should be defined here. */
3908 2842 : if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
3909 : /* If a class is declared in a header with the "extern
3910 : template" extension, then it will not be instantiated,
3911 : even in translation units that would normally require
3912 : it. Often such classes are explicitly instantiated in
3913 : one translation unit. Therefore, the explicit
3914 : instantiation must be made visible to other translation
3915 : units. */
3916 0 : DECL_EXTERNAL (decl) = 0;
3917 : else
3918 : {
3919 : /* The generic C++ ABI used to say that class data is always
3920 : COMDAT, even if emitted in a unique object. This is no
3921 : longer true, but for now we continue to do so for
3922 : compatibility with programs that are not strictly valid.
3923 : However, some variants (e.g., the ARM EABI) explicitly say
3924 : that class data only has COMDAT linkage if the class data
3925 : might be emitted in more than one translation unit.
3926 : When the key method can be inline and is inline, we still
3927 : have to arrange for comdat even though
3928 : class_data_always_comdat is false. */
3929 2842 : if (!vtables_uniquely_emitted (class_type)
3930 2842 : || targetm.cxx.class_data_always_comdat ())
3931 : {
3932 : /* The ABI requires COMDAT linkage. Normally, we
3933 : only emit COMDAT things when they are needed;
3934 : make sure that we realize that this entity is
3935 : indeed needed. */
3936 2842 : comdat_p = true;
3937 2842 : mark_needed (decl);
3938 : }
3939 : }
3940 : }
3941 510662 : else if (!flag_implicit_templates
3942 510662 : && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
3943 : import_p = true;
3944 : else
3945 : comdat_p = true;
3946 : }
3947 37519136 : else if (VAR_P (decl) && DECL_TINFO_P (decl))
3948 : {
3949 2229114 : tree type = TREE_TYPE (DECL_NAME (decl));
3950 2229114 : if (CLASS_TYPE_P (type))
3951 : {
3952 2226445 : class_type = type;
3953 2226445 : import_export_class (type);
3954 2226445 : if (CLASSTYPE_INTERFACE_KNOWN (type)
3955 1235361 : && TYPE_CONTAINS_VPTR_P (type)
3956 1235271 : && CLASSTYPE_INTERFACE_ONLY (type)
3957 : /* If -fno-rtti was specified, then we cannot be sure
3958 : that RTTI information will be emitted with the
3959 : virtual table of the class, so we must emit it
3960 : wherever it is used. */
3961 3457814 : && flag_rtti)
3962 : import_p = true;
3963 : else
3964 : {
3965 995122 : if (CLASSTYPE_INTERFACE_KNOWN (type)
3966 995122 : && !CLASSTYPE_INTERFACE_ONLY (type))
3967 : {
3968 3932 : comdat_p = (targetm.cxx.class_data_always_comdat ()
3969 3932 : || !vtables_uniquely_emitted (class_type));
3970 3932 : mark_needed (decl);
3971 3932 : if (!flag_weak)
3972 : {
3973 0 : comdat_p = false;
3974 0 : DECL_EXTERNAL (decl) = 0;
3975 : }
3976 : }
3977 : else
3978 : comdat_p = true;
3979 : }
3980 : }
3981 : else
3982 : comdat_p = true;
3983 : }
3984 35290022 : else if (DECL_TEMPLOID_INSTANTIATION (decl))
3985 : {
3986 : /* DECL is an implicit instantiation of a function or static
3987 : data member. */
3988 35049349 : if (flag_implicit_templates
3989 35049349 : || (flag_implicit_inline_templates
3990 21052 : && TREE_CODE (decl) == FUNCTION_DECL
3991 16825 : && DECL_DECLARED_INLINE_P (decl)))
3992 : comdat_p = true;
3993 : else
3994 : /* If we are not implicitly generating templates, then mark
3995 : this entity as undefined in this translation unit. */
3996 : import_p = true;
3997 : }
3998 240673 : else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
3999 : {
4000 231929 : if (!DECL_NONGNU_INLINE_P (decl))
4001 : {
4002 0 : tree ctype = DECL_CONTEXT (decl);
4003 0 : import_export_class (ctype);
4004 0 : if (CLASSTYPE_INTERFACE_KNOWN (ctype))
4005 : {
4006 0 : DECL_NOT_REALLY_EXTERN (decl)
4007 0 : = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
4008 0 : || (DECL_DECLARED_INLINE_P (decl)
4009 0 : && ! flag_implement_inlines
4010 0 : && !DECL_VINDEX (decl)));
4011 :
4012 0 : if (!DECL_NOT_REALLY_EXTERN (decl))
4013 0 : DECL_EXTERNAL (decl) = 1;
4014 :
4015 : /* Always make artificials weak. */
4016 0 : if (DECL_ARTIFICIAL (decl) && flag_weak)
4017 : comdat_p = true;
4018 : else
4019 0 : maybe_make_one_only (decl);
4020 : }
4021 : }
4022 : else
4023 : comdat_p = true;
4024 : }
4025 : else
4026 : comdat_p = true;
4027 :
4028 2845 : if (import_p)
4029 : {
4030 : /* If we are importing DECL into this translation unit, mark is
4031 : an undefined here. */
4032 2324125 : DECL_EXTERNAL (decl) = 1;
4033 2324125 : DECL_NOT_REALLY_EXTERN (decl) = 0;
4034 : }
4035 36793714 : else if (comdat_p)
4036 : {
4037 : /* If we decided to put DECL in COMDAT, mark it accordingly at
4038 : this point. */
4039 36793711 : comdat_linkage (decl);
4040 : }
4041 :
4042 39117839 : DECL_INTERFACE_KNOWN (decl) = 1;
4043 : }
4044 :
4045 : /* Return an expression that performs the destruction of DECL, which
4046 : must be a VAR_DECL whose type has a non-trivial destructor, or is
4047 : an array whose (innermost) elements have a non-trivial destructor. */
4048 :
4049 : tree
4050 1306 : build_cleanup (tree decl)
4051 : {
4052 1306 : tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
4053 1306 : gcc_assert (clean != NULL_TREE);
4054 1306 : return clean;
4055 : }
4056 :
4057 : /* GUARD is a helper variable for DECL; make them have the same linkage and
4058 : visibility. */
4059 :
4060 : void
4061 71694 : copy_linkage (tree guard, tree decl)
4062 : {
4063 71694 : TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
4064 71694 : TREE_STATIC (guard) = TREE_STATIC (decl);
4065 71694 : DECL_COMMON (guard) = DECL_COMMON (decl);
4066 71694 : DECL_COMDAT (guard) = DECL_COMDAT (decl);
4067 71694 : if (TREE_STATIC (guard))
4068 : {
4069 6239 : CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
4070 6239 : set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
4071 6239 : if (DECL_ONE_ONLY (decl))
4072 4347 : make_decl_one_only (guard, cxx_comdat_group (guard));
4073 6239 : if (TREE_PUBLIC (decl))
4074 5059 : DECL_WEAK (guard) = DECL_WEAK (decl);
4075 : /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not
4076 : be set until import_export_decl at EOF. */
4077 6239 : if (vague_linkage_p (decl))
4078 4365 : comdat_linkage (guard);
4079 6239 : DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
4080 6239 : DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
4081 6239 : if (!TREE_PUBLIC (decl))
4082 : {
4083 1180 : gcc_checking_assert (DECL_INTERFACE_KNOWN (decl));
4084 1180 : DECL_INTERFACE_KNOWN (guard) = 1;
4085 1180 : if (DECL_LANG_SPECIFIC (decl) && DECL_LANG_SPECIFIC (guard))
4086 244 : DECL_NOT_REALLY_EXTERN (guard) = DECL_NOT_REALLY_EXTERN (decl);
4087 : }
4088 : }
4089 71694 : }
4090 :
4091 : /* Returns the initialization guard variable for the variable DECL,
4092 : which has static storage duration. */
4093 :
4094 : tree
4095 4726 : get_guard (tree decl)
4096 : {
4097 4726 : tree sname = mangle_guard_variable (decl);
4098 4726 : tree guard = get_global_binding (sname);
4099 4726 : if (! guard)
4100 : {
4101 4726 : tree guard_type;
4102 :
4103 : /* We use a type that is big enough to contain a mutex as well
4104 : as an integer counter. */
4105 4726 : guard_type = targetm.cxx.guard_type ();
4106 4726 : guard = build_decl (DECL_SOURCE_LOCATION (decl),
4107 : VAR_DECL, sname, guard_type);
4108 :
4109 : /* The guard should have the same linkage as what it guards. */
4110 4726 : copy_linkage (guard, decl);
4111 :
4112 4726 : DECL_ARTIFICIAL (guard) = 1;
4113 4726 : DECL_IGNORED_P (guard) = 1;
4114 4726 : TREE_USED (guard) = 1;
4115 4726 : pushdecl_top_level_and_finish (guard, NULL_TREE);
4116 : }
4117 4726 : return guard;
4118 : }
4119 :
4120 : /* Returns true if accessing the GUARD atomic is expensive,
4121 : i.e. involves a call to __sync_synchronize or similar.
4122 : In this case let __cxa_guard_acquire handle the atomics. */
4123 :
4124 : static bool
4125 3705 : is_atomic_expensive_p (machine_mode mode)
4126 : {
4127 3705 : if (!flag_inline_atomics)
4128 : return true;
4129 :
4130 3705 : if (!can_compare_and_swap_p (mode, false) || !can_atomic_load_p (mode))
4131 0 : return true;
4132 :
4133 : return false;
4134 : }
4135 :
4136 : /* Return an atomic load of src with the appropriate memory model. */
4137 :
4138 : static tree
4139 3705 : build_atomic_load_type (tree src, HOST_WIDE_INT model, tree type)
4140 : {
4141 3705 : tree ptr_type = build_pointer_type (type);
4142 3705 : tree mem_model = build_int_cst (integer_type_node, model);
4143 3705 : tree t, addr, val;
4144 3705 : unsigned int size;
4145 3705 : int fncode;
4146 :
4147 3705 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
4148 :
4149 3705 : fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
4150 3705 : t = builtin_decl_implicit ((enum built_in_function) fncode);
4151 :
4152 3705 : addr = build1 (ADDR_EXPR, ptr_type, src);
4153 3705 : val = build_call_expr (t, 2, addr, mem_model);
4154 3705 : return val;
4155 : }
4156 :
4157 : /* Return those bits of the GUARD variable that should be set when the
4158 : guarded entity is actually initialized. */
4159 :
4160 : static tree
4161 2042 : get_guard_bits (tree guard)
4162 : {
4163 2042 : if (!targetm.cxx.guard_mask_bit ())
4164 : {
4165 : /* We only set the first byte of the guard, in order to leave room
4166 : for a mutex in the high-order bits. */
4167 2042 : guard = build1 (ADDR_EXPR,
4168 2042 : build_pointer_type (TREE_TYPE (guard)),
4169 : guard);
4170 2042 : guard = build1 (NOP_EXPR,
4171 : build_pointer_type (char_type_node),
4172 : guard);
4173 2042 : guard = build1 (INDIRECT_REF, char_type_node, guard);
4174 : }
4175 :
4176 2042 : return guard;
4177 : }
4178 :
4179 : /* Return an expression which determines whether or not the GUARD
4180 : variable has already been initialized. */
4181 :
4182 : tree
4183 4726 : get_guard_cond (tree guard, bool thread_safe)
4184 : {
4185 4726 : tree guard_value;
4186 :
4187 4726 : if (!thread_safe)
4188 1021 : guard = get_guard_bits (guard);
4189 : else
4190 : {
4191 3705 : tree type = targetm.cxx.guard_mask_bit ()
4192 3705 : ? TREE_TYPE (guard) : char_type_node;
4193 :
4194 3705 : if (is_atomic_expensive_p (TYPE_MODE (type)))
4195 0 : guard = integer_zero_node;
4196 : else
4197 3705 : guard = build_atomic_load_type (guard, MEMMODEL_ACQUIRE, type);
4198 : }
4199 :
4200 : /* Mask off all but the low bit. */
4201 4726 : if (targetm.cxx.guard_mask_bit ())
4202 : {
4203 0 : guard_value = integer_one_node;
4204 0 : if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
4205 0 : guard_value = fold_convert (TREE_TYPE (guard), guard_value);
4206 0 : guard = cp_build_binary_op (input_location,
4207 : BIT_AND_EXPR, guard, guard_value,
4208 : tf_warning_or_error);
4209 : }
4210 :
4211 4726 : guard_value = integer_zero_node;
4212 4726 : if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
4213 4726 : guard_value = fold_convert (TREE_TYPE (guard), guard_value);
4214 4726 : return cp_build_binary_op (input_location,
4215 : EQ_EXPR, guard, guard_value,
4216 4726 : tf_warning_or_error);
4217 : }
4218 :
4219 : /* Return an expression which sets the GUARD variable, indicating that
4220 : the variable being guarded has been initialized. */
4221 :
4222 : tree
4223 1021 : set_guard (tree guard)
4224 : {
4225 1021 : tree guard_init;
4226 :
4227 : /* Set the GUARD to one. */
4228 1021 : guard = get_guard_bits (guard);
4229 1021 : guard_init = integer_one_node;
4230 1021 : if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
4231 1021 : guard_init = fold_convert (TREE_TYPE (guard), guard_init);
4232 1021 : return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
4233 1021 : tf_warning_or_error);
4234 : }
4235 :
4236 : /* Returns true iff we can tell that VAR does not have a dynamic
4237 : initializer. */
4238 :
4239 : static bool
4240 2590 : var_defined_without_dynamic_init (tree var)
4241 : {
4242 : /* constinit vars are guaranteed to not have dynamic initializer,
4243 : but still registering the destructor counts as dynamic initialization. */
4244 2590 : if (DECL_DECLARED_CONSTINIT_P (var)
4245 24 : && COMPLETE_TYPE_P (TREE_TYPE (var))
4246 2608 : && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
4247 : return true;
4248 : /* If it's defined in another TU, we can't tell. */
4249 2578 : if (DECL_EXTERNAL (var))
4250 : return false;
4251 : /* If it has a non-trivial destructor, registering the destructor
4252 : counts as dynamic initialization. */
4253 2331 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
4254 : return false;
4255 : /* If it's in this TU, its initializer has been processed, unless
4256 : it's a case of self-initialization, then DECL_INITIALIZED_P is
4257 : false while the initializer is handled by finish_id_expression. */
4258 2150 : if (!DECL_INITIALIZED_P (var))
4259 : return false;
4260 : /* If it has no initializer or a constant one, it's not dynamic. */
4261 2144 : return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
4262 2144 : || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
4263 : }
4264 :
4265 : /* Returns true iff VAR is a variable that needs uses to be
4266 : wrapped for possible dynamic initialization. */
4267 :
4268 : bool
4269 75319 : var_needs_tls_wrapper (tree var)
4270 : {
4271 75319 : return (!error_operand_p (var)
4272 75316 : && CP_DECL_THREAD_LOCAL_P (var)
4273 75316 : && !DECL_GNU_TLS_P (var)
4274 2728 : && !DECL_FUNCTION_SCOPE_P (var)
4275 77909 : && !var_defined_without_dynamic_init (var));
4276 : }
4277 :
4278 : /* Get the FUNCTION_DECL for the shared TLS init function for this
4279 : translation unit. */
4280 :
4281 : static tree
4282 195 : get_local_tls_init_fn (location_t loc)
4283 : {
4284 195 : tree sname = get_identifier ("__tls_init");
4285 195 : tree fn = get_global_binding (sname);
4286 195 : if (!fn)
4287 : {
4288 123 : fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname,
4289 : build_function_type (void_type_node,
4290 : void_list_node));
4291 123 : SET_DECL_LANGUAGE (fn, lang_c);
4292 123 : TREE_PUBLIC (fn) = false;
4293 123 : DECL_ARTIFICIAL (fn) = true;
4294 123 : mark_used (fn);
4295 123 : set_global_binding (fn);
4296 : }
4297 195 : return fn;
4298 : }
4299 :
4300 : /* Get a FUNCTION_DECL for the init function for the thread_local
4301 : variable VAR. The init function will be an alias to the function
4302 : that initializes all the non-local TLS variables in the translation
4303 : unit. The init function is only used by the wrapper function. */
4304 :
4305 : static tree
4306 1282 : get_tls_init_fn (tree var)
4307 : {
4308 : /* Only C++11 TLS vars need this init fn. */
4309 1282 : if (!var_needs_tls_wrapper (var))
4310 : return NULL_TREE;
4311 :
4312 : /* If -fno-extern-tls-init, assume that we don't need to call
4313 : a tls init function for a variable defined in another TU. */
4314 1263 : if (!flag_extern_tls_init && DECL_EXTERNAL (var))
4315 : return NULL_TREE;
4316 :
4317 : /* If the variable is internal, or if we can't generate aliases,
4318 : call the local init function directly. */
4319 1262 : if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
4320 72 : return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var));
4321 :
4322 1190 : tree sname = mangle_tls_init_fn (var);
4323 1190 : tree fn = get_global_binding (sname);
4324 1190 : if (!fn)
4325 : {
4326 765 : fn = build_lang_decl (FUNCTION_DECL, sname,
4327 : build_function_type (void_type_node,
4328 : void_list_node));
4329 765 : SET_DECL_LANGUAGE (fn, lang_c);
4330 765 : TREE_PUBLIC (fn) = TREE_PUBLIC (var);
4331 765 : DECL_ARTIFICIAL (fn) = true;
4332 765 : DECL_CONTEXT (fn) = FROB_CONTEXT (global_namespace);
4333 765 : DECL_COMDAT (fn) = DECL_COMDAT (var);
4334 765 : DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
4335 765 : if (DECL_ONE_ONLY (var))
4336 3 : make_decl_one_only (fn, cxx_comdat_group (fn));
4337 765 : if (TREE_PUBLIC (var))
4338 : {
4339 765 : tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
4340 : /* If the variable is defined somewhere else and might have static
4341 : initialization, make the init function a weak reference. */
4342 765 : if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
4343 603 : || TYPE_HAS_CONSTEXPR_CTOR (obtype)
4344 585 : || TYPE_HAS_TRIVIAL_DFLT (obtype))
4345 180 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
4346 927 : && DECL_EXTERNAL (var))
4347 51 : declare_weak (fn);
4348 : else
4349 714 : DECL_WEAK (fn) = DECL_WEAK (var);
4350 : }
4351 765 : DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
4352 765 : DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
4353 765 : DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
4354 765 : DECL_IGNORED_P (fn) = 1;
4355 765 : mark_used (fn);
4356 :
4357 765 : DECL_BEFRIENDING_CLASSES (fn) = var;
4358 :
4359 765 : set_global_binding (fn);
4360 : }
4361 : return fn;
4362 : }
4363 :
4364 : /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
4365 : variable VAR. The wrapper function calls the init function (if any) for
4366 : VAR and then returns a reference to VAR. The wrapper function is used
4367 : in place of VAR everywhere VAR is mentioned. */
4368 :
4369 : static tree
4370 74022 : get_tls_wrapper_fn (tree var)
4371 : {
4372 : /* Only C++11 TLS vars need this wrapper fn. */
4373 74022 : if (!var_needs_tls_wrapper (var))
4374 : return NULL_TREE;
4375 :
4376 749 : tree sname = mangle_tls_wrapper_fn (var);
4377 749 : tree fn = get_global_binding (sname);
4378 749 : if (!fn)
4379 : {
4380 : /* A named rvalue reference is an lvalue, so the wrapper should
4381 : always return an lvalue reference. */
4382 586 : tree type = non_reference (TREE_TYPE (var));
4383 586 : type = build_reference_type (type);
4384 586 : tree fntype = build_function_type (type, void_list_node);
4385 :
4386 586 : fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var),
4387 : FUNCTION_DECL, sname, fntype);
4388 586 : SET_DECL_LANGUAGE (fn, lang_c);
4389 586 : TREE_PUBLIC (fn) = TREE_PUBLIC (var);
4390 586 : DECL_ARTIFICIAL (fn) = true;
4391 586 : DECL_IGNORED_P (fn) = 1;
4392 586 : DECL_CONTEXT (fn) = FROB_CONTEXT (global_namespace);
4393 : /* The wrapper is inline and emitted everywhere var is used. */
4394 586 : DECL_DECLARED_INLINE_P (fn) = true;
4395 586 : if (TREE_PUBLIC (var))
4396 : {
4397 514 : comdat_linkage (fn);
4398 : #ifdef HAVE_GAS_HIDDEN
4399 : /* Make the wrapper bind locally; there's no reason to share
4400 : the wrapper between multiple shared objects. */
4401 514 : DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
4402 514 : DECL_VISIBILITY_SPECIFIED (fn) = true;
4403 : #endif
4404 : }
4405 586 : if (!TREE_PUBLIC (fn))
4406 72 : DECL_INTERFACE_KNOWN (fn) = true;
4407 586 : mark_used (fn);
4408 586 : note_vague_linkage_fn (fn);
4409 :
4410 : #if 0
4411 : /* We want CSE to commonize calls to the wrapper, but marking it as
4412 : pure is unsafe since it has side-effects. I guess we need a new
4413 : ECF flag even weaker than ECF_PURE. FIXME! */
4414 : DECL_PURE_P (fn) = true;
4415 : #endif
4416 :
4417 586 : DECL_BEFRIENDING_CLASSES (fn) = var;
4418 :
4419 586 : set_global_binding (fn);
4420 : }
4421 : return fn;
4422 : }
4423 :
4424 : /* If EXPR is a thread_local variable that should be wrapped by init
4425 : wrapper function, return a call to that function, otherwise return
4426 : NULL. */
4427 :
4428 : tree
4429 950508062 : maybe_get_tls_wrapper_call (tree expr)
4430 : {
4431 950508062 : if (VAR_P (expr)
4432 327536070 : && !processing_template_decl
4433 137674238 : && !cp_unevaluated_operand
4434 1086563456 : && CP_DECL_THREAD_LOCAL_P (expr))
4435 74022 : if (tree wrap = get_tls_wrapper_fn (expr))
4436 749 : return build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
4437 : return NULL;
4438 : }
4439 :
4440 : /* At EOF, generate the definition for the TLS wrapper function FN:
4441 :
4442 : T& var_wrapper() {
4443 : if (init_fn) init_fn();
4444 : return var;
4445 : } */
4446 :
4447 : static void
4448 586 : generate_tls_wrapper (tree fn)
4449 : {
4450 586 : tree var = DECL_BEFRIENDING_CLASSES (fn);
4451 :
4452 586 : start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
4453 586 : tree body = begin_function_body ();
4454 : /* Only call the init fn if there might be one. */
4455 586 : if (tree init_fn = get_tls_init_fn (var))
4456 : {
4457 566 : tree if_stmt = NULL_TREE;
4458 : /* If init_fn is a weakref, make sure it exists before calling. */
4459 566 : if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
4460 : {
4461 51 : if_stmt = begin_if_stmt ();
4462 51 : tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
4463 51 : tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
4464 : NE_EXPR, addr, nullptr_node,
4465 : tf_warning_or_error);
4466 51 : finish_if_stmt_cond (cond, if_stmt);
4467 : }
4468 566 : finish_expr_stmt (build_cxx_call
4469 : (init_fn, 0, NULL, tf_warning_or_error));
4470 566 : if (if_stmt)
4471 : {
4472 51 : finish_then_clause (if_stmt);
4473 51 : finish_if_stmt (if_stmt);
4474 : }
4475 : }
4476 : else
4477 : /* If there's no initialization, the wrapper is a constant function. */
4478 20 : TREE_READONLY (fn) = true;
4479 586 : finish_return_stmt (convert_from_reference (var));
4480 586 : finish_function_body (body);
4481 586 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
4482 586 : }
4483 :
4484 : /* Start a global constructor or destructor function. */
4485 :
4486 : static tree
4487 7418 : start_objects (bool initp, unsigned priority, bool has_body,
4488 : bool omp_target = false)
4489 : {
4490 7418 : bool default_init = initp && priority == DEFAULT_INIT_PRIORITY;
4491 : /* FIXME: We may eventually want to treat OpenMP offload initializers
4492 : in modules specially as well. */
4493 7418 : bool is_module_init = (default_init
4494 7418 : && !omp_target
4495 7418 : && module_global_init_needed ());
4496 7418 : tree name = NULL_TREE;
4497 :
4498 7418 : if (is_module_init)
4499 1980 : name = mangle_module_global_init (0);
4500 : else
4501 : {
4502 5438 : char type[14];
4503 :
4504 : /* We use `I' to indicate initialization and `D' to indicate
4505 : destruction. */
4506 5438 : unsigned len;
4507 5438 : if (omp_target)
4508 : /* Use "off_" signifying "offload" here. The name must be distinct
4509 : from the non-offload case. The format of the name is scanned in
4510 : tree.cc/get_file_function_name, so stick to the same length for
4511 : both name variants. */
4512 15 : len = sprintf (type, "off_%c", initp ? 'I' : 'D');
4513 : else
4514 5432 : len = sprintf (type, "sub_%c", initp ? 'I' : 'D');
4515 5438 : if (priority != DEFAULT_INIT_PRIORITY)
4516 : {
4517 40 : char joiner = '_';
4518 : #ifdef JOINER
4519 40 : joiner = JOINER;
4520 : #endif
4521 40 : type[len++] = joiner;
4522 40 : sprintf (type + len, "%.5u", priority);
4523 : }
4524 5438 : name = get_file_function_name (type);
4525 : }
4526 :
4527 7418 : tree fntype = build_function_type (void_type_node, void_list_node);
4528 7418 : tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
4529 :
4530 7418 : if (omp_target)
4531 : {
4532 15 : DECL_ATTRIBUTES (fndecl)
4533 15 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
4534 15 : DECL_ATTRIBUTES (fndecl));
4535 15 : DECL_ATTRIBUTES (fndecl)
4536 30 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
4537 15 : DECL_ATTRIBUTES (fndecl));
4538 : }
4539 :
4540 7418 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
4541 7418 : if (is_module_init)
4542 : {
4543 1980 : SET_DECL_ASSEMBLER_NAME (fndecl, name);
4544 1980 : TREE_PUBLIC (fndecl) = true;
4545 1980 : determine_visibility (fndecl);
4546 : }
4547 : else
4548 5438 : TREE_PUBLIC (fndecl) = 0;
4549 7418 : start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4550 :
4551 : /* Mark as artificial because it's not explicitly in the user's
4552 : source code. */
4553 7418 : DECL_ARTIFICIAL (current_function_decl) = 1;
4554 :
4555 : /* Mark this declaration as used to avoid spurious warnings. */
4556 7418 : TREE_USED (current_function_decl) = 1;
4557 :
4558 : /* Mark this function as a global constructor or destructor. */
4559 7418 : if (initp)
4560 7409 : DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
4561 : else
4562 9 : DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
4563 :
4564 7418 : tree body = begin_compound_stmt (BCS_FN_BODY);
4565 :
4566 7418 : if (is_module_init && has_body)
4567 : {
4568 : // If the function is going to be empty, don't emit idempotency.
4569 : // 'static bool __in_chrg = false;
4570 : // if (__inchrg) return;
4571 : // __inchrg = true
4572 64 : tree var = build_lang_decl (VAR_DECL, in_charge_identifier,
4573 : boolean_type_node);
4574 64 : DECL_CONTEXT (var) = fndecl;
4575 64 : DECL_ARTIFICIAL (var) = true;
4576 64 : TREE_STATIC (var) = true;
4577 64 : pushdecl (var);
4578 64 : cp_finish_decl (var, NULL_TREE, false, NULL_TREE, 0);
4579 :
4580 64 : tree if_stmt = begin_if_stmt ();
4581 64 : finish_if_stmt_cond (var, if_stmt);
4582 64 : finish_return_stmt (NULL_TREE);
4583 64 : finish_then_clause (if_stmt);
4584 64 : finish_if_stmt (if_stmt);
4585 :
4586 64 : tree assign = build2 (MODIFY_EXPR, boolean_type_node,
4587 : var, boolean_true_node);
4588 64 : TREE_SIDE_EFFECTS (assign) = true;
4589 64 : finish_expr_stmt (assign);
4590 : }
4591 :
4592 7418 : return body;
4593 : }
4594 :
4595 : /* Finish a global constructor or destructor. Add it to the global
4596 : ctors or dtors, if STARTP is true. */
4597 :
4598 : static tree
4599 7415 : finish_objects (bool initp, unsigned priority, tree body, bool startp)
4600 : {
4601 : /* Finish up. */
4602 7415 : finish_compound_stmt (body);
4603 7415 : tree fn = finish_function (/*inline_p=*/false);
4604 :
4605 7415 : if (!startp)
4606 : ; // Neither ctor nor dtor I be.
4607 5499 : else if (initp)
4608 : {
4609 5490 : DECL_STATIC_CONSTRUCTOR (fn) = 1;
4610 5490 : decl_init_priority_insert (fn, priority);
4611 : }
4612 : else
4613 : {
4614 9 : DECL_STATIC_DESTRUCTOR (fn) = 1;
4615 9 : decl_fini_priority_insert (fn, priority);
4616 : }
4617 :
4618 7415 : return fn;
4619 : }
4620 :
4621 : /* The name of the function we create to handle initializations and
4622 : destructions for objects with static storage duration. */
4623 : #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
4624 : #define OMP_SSDF_IDENTIFIER "__omp_target_static_init_and_destruction"
4625 :
4626 : /* Begins the generation of the function that will handle all
4627 : initialization or destruction of objects with static storage
4628 : duration at PRIORITY.
4629 :
4630 : It is assumed that this function will be called once for the host, and once
4631 : for an OpenMP offload target. */
4632 :
4633 : static tree
4634 5508 : start_partial_init_fini_fn (bool initp, unsigned priority, unsigned count,
4635 : bool omp_target)
4636 : {
4637 5508 : char id[MAX (sizeof (SSDF_IDENTIFIER), sizeof (OMP_SSDF_IDENTIFIER))
4638 : + 1 /* \0 */ + 32];
4639 5508 : tree name;
4640 :
4641 : /* Create the identifier for this function. It will be of the form
4642 : SSDF_IDENTIFIER_<number> if not omp_target and otherwise
4643 : OMP_SSDF_IDENTIFIER_<number>. */
4644 5508 : sprintf (id, "%s_%u", omp_target ? OMP_SSDF_IDENTIFIER : SSDF_IDENTIFIER,
4645 : count);
4646 5508 : name = get_identifier (id);
4647 5508 : tree type = build_function_type (void_type_node, void_list_node);
4648 :
4649 : /* Create the FUNCTION_DECL itself. */
4650 5508 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4651 5508 : TREE_PUBLIC (fn) = 0;
4652 5508 : DECL_ARTIFICIAL (fn) = 1;
4653 :
4654 5508 : if (omp_target)
4655 : {
4656 15 : DECL_ATTRIBUTES (fn)
4657 15 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
4658 15 : DECL_ATTRIBUTES (fn));
4659 15 : DECL_ATTRIBUTES (fn)
4660 30 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
4661 15 : DECL_ATTRIBUTES (fn));
4662 : }
4663 :
4664 5508 : int idx = initp + 2 * omp_target;
4665 :
4666 : /* Put this function in the list of functions to be called from the
4667 : static constructors and destructors. */
4668 5508 : if (!static_init_fini_fns[idx])
4669 5478 : static_init_fini_fns[idx] = priority_map_t::create_ggc ();
4670 5508 : auto &slot = static_init_fini_fns[idx]->get_or_insert (priority);
4671 5508 : slot = tree_cons (fn, NULL_TREE, slot);
4672 :
4673 : /* Put the function in the global scope. */
4674 5508 : pushdecl (fn);
4675 :
4676 : /* Start the function itself. This is equivalent to declaring the
4677 : function as:
4678 :
4679 : static void __ssdf (int __initialize_p, init __priority_p);
4680 :
4681 : It is static because we only need to call this function from the
4682 : various constructor and destructor functions for this module. */
4683 5508 : start_preparsed_function (fn, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4684 :
4685 : /* Set up the scope of the outermost block in the function. */
4686 5508 : return begin_compound_stmt (BCS_FN_BODY);
4687 : }
4688 :
4689 : /* Finish the generation of the function which performs initialization
4690 : or destruction of objects with static storage duration. */
4691 :
4692 : static void
4693 5508 : finish_partial_init_fini_fn (tree body)
4694 : {
4695 : /* Close out the function. */
4696 5508 : finish_compound_stmt (body);
4697 5508 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
4698 5508 : }
4699 :
4700 : /* The effective initialization priority of a DECL. */
4701 :
4702 : #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
4703 : ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
4704 : ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
4705 :
4706 : /* Whether a DECL needs a guard to protect it against multiple
4707 : initialization. */
4708 :
4709 : #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
4710 : || DECL_ONE_ONLY (decl) \
4711 : || DECL_WEAK (decl)))
4712 :
4713 : /* Walks the initializer list of a global variable and looks for
4714 : temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
4715 : and that have their DECL_CONTEXT() == NULL. For each such
4716 : temporary variable, set their DECL_CONTEXT() to CTX -- the
4717 : initializing function. This is necessary because otherwise some
4718 : optimizers (enabled by -O2 -fprofile-arcs) might crash when trying
4719 : to refer to a temporary variable that does not have its
4720 : DECL_CONTEXT() properly set. */
4721 :
4722 : static tree
4723 267579 : fix_temporary_vars_context_r (tree *node,
4724 : int * /*unused*/,
4725 : void *ctx)
4726 : {
4727 267579 : if (TREE_CODE (*node) == BIND_EXPR)
4728 800 : for (tree var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
4729 400 : if (VAR_P (var) && !DECL_NAME (var)
4730 800 : && DECL_ARTIFICIAL (var) && !DECL_CONTEXT (var))
4731 400 : DECL_CONTEXT (var) = tree (ctx);
4732 :
4733 267579 : return NULL_TREE;
4734 : }
4735 :
4736 : /* Set up to handle the initialization or destruction of DECL. If
4737 : INITP is nonzero, we are initializing the variable. Otherwise, we
4738 : are destroying it. */
4739 :
4740 : static void
4741 11067 : one_static_initialization_or_destruction (bool initp, tree decl, tree init,
4742 : bool omp_target)
4743 : {
4744 : /* If we are supposed to destruct and there's a trivial destructor,
4745 : nothing has to be done. */
4746 11067 : gcc_checking_assert (init || !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)));
4747 :
4748 : /* Trick the compiler into thinking we are at the file and line
4749 : where DECL was declared so that error-messages make sense, and so
4750 : that the debugger will show somewhat sensible file and line
4751 : information. */
4752 11067 : input_location = DECL_SOURCE_LOCATION (decl);
4753 :
4754 : /* Make sure temporary variables in the initialiser all have
4755 : their DECL_CONTEXT() set to a value different from NULL_TREE.
4756 : This can happen when global variables initializers are built.
4757 : In that case, the DECL_CONTEXT() of the global variables _AND_ of all
4758 : the temporary variables that might have been generated in the
4759 : accompanying initializers is NULL_TREE, meaning the variables have been
4760 : declared in the global namespace.
4761 : What we want to do here is to fix that and make sure the DECL_CONTEXT()
4762 : of the temporaries are set to the current function decl. */
4763 11067 : cp_walk_tree_without_duplicates (&init,
4764 : fix_temporary_vars_context_r,
4765 : current_function_decl);
4766 :
4767 : /* Because of:
4768 :
4769 : [class.access.spec]
4770 :
4771 : Access control for implicit calls to the constructors,
4772 : the conversion functions, or the destructor called to
4773 : create and destroy a static data member is performed as
4774 : if these calls appeared in the scope of the member's
4775 : class.
4776 :
4777 : we pretend we are in a static member function of the class of
4778 : which the DECL is a member. */
4779 11067 : if (member_p (decl))
4780 : {
4781 1604 : DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
4782 1604 : DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
4783 : }
4784 :
4785 : /* Assume we don't need a guard. */
4786 11067 : tree guard_if_stmt = NULL_TREE;
4787 :
4788 : /* We need a guard if this is an object with external linkage that
4789 : might be initialized in more than one place. (For example, a
4790 : static data member of a template, when the data member requires
4791 : construction.) */
4792 11067 : if (NEEDS_GUARD_P (decl))
4793 : {
4794 876 : tree guard = get_guard (decl);
4795 876 : tree guard_cond;
4796 :
4797 876 : if (flag_use_cxa_atexit)
4798 : {
4799 : /* When using __cxa_atexit, we just check the GUARD as we
4800 : would for a local static. We never try to destroy
4801 : anything from a static destructor. */
4802 876 : gcc_assert (initp);
4803 876 : guard_cond = get_guard_cond (guard, false);
4804 : }
4805 : else
4806 : {
4807 : /* If we don't have __cxa_atexit, then we will be running
4808 : destructors from .fini sections, or their equivalents.
4809 : So, we need to know how many times we've tried to
4810 : initialize this object. We do initializations only if
4811 : the GUARD was or becomes zero (initp vs !initp
4812 : respectively). */
4813 0 : guard_cond = cp_build_unary_op (initp ? POSTINCREMENT_EXPR
4814 : : PREDECREMENT_EXPR,
4815 : guard,
4816 : /*noconvert=*/true,
4817 : tf_warning_or_error);
4818 0 : guard_cond = cp_build_binary_op (input_location, EQ_EXPR, guard_cond,
4819 : integer_zero_node,
4820 : tf_warning_or_error);
4821 : }
4822 :
4823 876 : guard_if_stmt = begin_if_stmt ();
4824 876 : finish_if_stmt_cond (guard_cond, guard_if_stmt);
4825 :
4826 876 : if (flag_use_cxa_atexit)
4827 : /* Set the GUARD now. */
4828 876 : finish_expr_stmt (set_guard (guard));
4829 : }
4830 :
4831 : /* Perform the initialization or destruction. */
4832 11067 : if (initp)
4833 : {
4834 11052 : if (init)
4835 : {
4836 10527 : finish_expr_stmt (init);
4837 10527 : if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
4838 28 : if (varpool_node *vnode = varpool_node::get (decl))
4839 28 : vnode->dynamically_initialized = 1;
4840 : }
4841 :
4842 : /* If we're using __cxa_atexit, register a function that calls the
4843 : destructor for the object. */
4844 11052 : if (flag_use_cxa_atexit)
4845 11043 : finish_expr_stmt (register_dtor_fn (decl, omp_target));
4846 : }
4847 : else
4848 15 : finish_expr_stmt (build_cleanup (decl));
4849 :
4850 : /* Finish the guard if-stmt, if necessary. */
4851 11067 : if (guard_if_stmt)
4852 : {
4853 876 : finish_then_clause (guard_if_stmt);
4854 876 : finish_if_stmt (guard_if_stmt);
4855 : }
4856 :
4857 : /* Now that we're done with DECL we don't need to pretend to be a
4858 : member of its class any longer. */
4859 11067 : DECL_CONTEXT (current_function_decl) = NULL_TREE;
4860 11067 : DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
4861 11067 : }
4862 :
4863 : /* Helper function for emit_partial_init_fini_fn and handle_tls_init.
4864 : For structured bindings, disable stmts_are_full_exprs_p ()
4865 : on STATIC_INIT_DECOMP_BASE_P nodes, reenable it on the
4866 : first STATIC_INIT_DECOMP_NONBASE_P node and emit all the
4867 : STATIC_INIT_DECOMP_BASE_P and STATIC_INIT_DECOMP_NONBASE_P
4868 : consecutive nodes in a single STATEMENT_LIST wrapped with
4869 : CLEANUP_POINT_EXPR. */
4870 :
4871 : static inline tree
4872 11067 : decomp_handle_one_var (tree node, tree sl, bool *saw_nonbase,
4873 : int save_stmts_are_full_exprs_p)
4874 : {
4875 11136 : if (sl && !*saw_nonbase && STATIC_INIT_DECOMP_NONBASE_P (node))
4876 : {
4877 63 : *saw_nonbase = true;
4878 63 : current_stmt_tree ()->stmts_are_full_exprs_p
4879 63 : = save_stmts_are_full_exprs_p;
4880 : }
4881 11190 : else if (sl && *saw_nonbase && !STATIC_INIT_DECOMP_NONBASE_P (node))
4882 : {
4883 45 : sl = pop_stmt_list (sl);
4884 45 : sl = maybe_cleanup_point_expr_void (sl);
4885 45 : add_stmt (sl);
4886 45 : sl = NULL_TREE;
4887 : }
4888 21924 : if (sl == NULL_TREE && STATIC_INIT_DECOMP_BASE_P (node))
4889 : {
4890 63 : sl = push_stmt_list ();
4891 63 : *saw_nonbase = false;
4892 63 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
4893 : }
4894 11067 : return sl;
4895 : }
4896 :
4897 : /* Similarly helper called when the whole var list is processed. */
4898 :
4899 : static inline void
4900 5585 : decomp_finalize_var_list (tree sl, int save_stmts_are_full_exprs_p)
4901 : {
4902 5585 : if (sl)
4903 : {
4904 18 : current_stmt_tree ()->stmts_are_full_exprs_p
4905 18 : = save_stmts_are_full_exprs_p;
4906 18 : sl = pop_stmt_list (sl);
4907 18 : sl = maybe_cleanup_point_expr_void (sl);
4908 18 : add_stmt (sl);
4909 : }
4910 5585 : }
4911 :
4912 : /* Helper for emit_partial_init_fini_fn OpenMP target handling, called via
4913 : walk_tree. Set DECL_CONTEXT on any automatic temporaries which still
4914 : have it NULL to id->src_fn, so that later copy_tree_body_r can remap those.
4915 : Otherwise DECL_CONTEXT would be set only during gimplification of the host
4916 : fn and when copy_tree_body_r doesn't remap those, we'd ICE during the
4917 : target fn gimplification because the same automatic VAR_DECL can't be
4918 : used in multiple functions (with the exception of nested functions). */
4919 :
4920 : static tree
4921 303 : set_context_for_auto_vars_r (tree *tp, int *, void *data)
4922 : {
4923 303 : copy_body_data *id = (copy_body_data *) data;
4924 303 : if (auto_var_in_fn_p (*tp, NULL_TREE) && DECL_ARTIFICIAL (*tp))
4925 11 : DECL_CONTEXT (*tp) = id->src_fn;
4926 303 : return NULL_TREE;
4927 : }
4928 :
4929 : /* Generate code to do the initialization or destruction of the decls in VARS,
4930 : a TREE_LIST of VAR_DECL with static storage duration.
4931 : Whether initialization or destruction is performed is specified by INITP. */
4932 :
4933 : static tree
4934 5462 : emit_partial_init_fini_fn (bool initp, unsigned priority, tree vars,
4935 : unsigned counter, location_t locus, tree host_fn)
4936 : {
4937 5462 : input_location = locus;
4938 5462 : bool omp_target = (host_fn != NULL_TREE);
4939 5462 : tree body = start_partial_init_fini_fn (initp, priority, counter, omp_target);
4940 5462 : tree fndecl = current_function_decl;
4941 :
4942 5462 : tree nonhost_if_stmt = NULL_TREE;
4943 5462 : if (omp_target)
4944 : {
4945 15 : nonhost_if_stmt = begin_if_stmt ();
4946 : /* We add an "omp declare target nohost" attribute, but (for
4947 : now) we still get a copy of the constructor/destructor on
4948 : the host. Make sure it does nothing unless we're on the
4949 : target device. */
4950 15 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
4951 15 : tree initial_dev = build_call_expr (fn, 0);
4952 15 : tree target_dev_p
4953 15 : = cp_build_binary_op (input_location, NE_EXPR, initial_dev,
4954 : integer_one_node, tf_warning_or_error);
4955 15 : finish_if_stmt_cond (target_dev_p, nonhost_if_stmt);
4956 : }
4957 :
4958 5462 : tree sl = NULL_TREE;
4959 5462 : int save_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
4960 5462 : bool saw_nonbase = false;
4961 15734 : for (tree node = vars; node; node = TREE_CHAIN (node))
4962 : {
4963 10272 : tree decl = TREE_VALUE (node);
4964 10272 : tree init = TREE_PURPOSE (node);
4965 10272 : sl = decomp_handle_one_var (node, sl, &saw_nonbase,
4966 : save_stmts_are_full_exprs_p);
4967 : /* We will emit 'init' twice, and it is modified in-place during
4968 : gimplification. Make a copy here. */
4969 10272 : if (omp_target)
4970 : {
4971 : /* We've already emitted INIT in the host version of the ctor/dtor
4972 : function. We need to deep-copy it (including new versions of
4973 : local variables introduced, etc.) for use in the target
4974 : ctor/dtor function. */
4975 19 : copy_body_data id;
4976 19 : hash_map<tree, tree> decl_map;
4977 19 : memset (&id, 0, sizeof (id));
4978 19 : id.src_fn = host_fn;
4979 19 : id.dst_fn = current_function_decl;
4980 19 : id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
4981 19 : id.decl_map = &decl_map;
4982 19 : id.copy_decl = copy_decl_no_change;
4983 19 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4984 19 : id.transform_new_cfg = true;
4985 19 : id.transform_return_to_modify = false;
4986 19 : id.eh_lp_nr = 0;
4987 19 : walk_tree (&init, set_context_for_auto_vars_r, &id, NULL);
4988 19 : walk_tree (&init, copy_tree_body_r, &id, NULL);
4989 19 : }
4990 : /* Do one initialization or destruction. */
4991 10272 : one_static_initialization_or_destruction (initp, decl, init, omp_target);
4992 : }
4993 5462 : decomp_finalize_var_list (sl, save_stmts_are_full_exprs_p);
4994 :
4995 5462 : if (omp_target)
4996 : {
4997 : /* Finish up nonhost if-stmt body. */
4998 15 : finish_then_clause (nonhost_if_stmt);
4999 15 : finish_if_stmt (nonhost_if_stmt);
5000 : }
5001 :
5002 : /* Finish up the static storage duration function for this
5003 : round. */
5004 5462 : input_location = locus;
5005 5462 : finish_partial_init_fini_fn (body);
5006 :
5007 5462 : return fndecl;
5008 : }
5009 :
5010 : /* VARS is a list of variables with static storage duration which may
5011 : need initialization and/or finalization. Remove those variables
5012 : that don't really need to be initialized or finalized, and return
5013 : the resulting list. The order in which the variables appear in
5014 : VARS is in reverse order of the order in which they should actually
5015 : be initialized. That order is preserved. */
5016 :
5017 : static tree
5018 283946 : prune_vars_needing_no_initialization (tree *vars)
5019 : {
5020 283946 : tree *var = vars;
5021 283946 : tree result = NULL_TREE;
5022 :
5023 294985 : while (*var)
5024 : {
5025 11039 : tree t = *var;
5026 11039 : tree decl = TREE_VALUE (t);
5027 11039 : tree init = TREE_PURPOSE (t);
5028 :
5029 : /* Deal gracefully with error. */
5030 11039 : if (error_operand_p (decl))
5031 : {
5032 0 : var = &TREE_CHAIN (t);
5033 0 : continue;
5034 : }
5035 :
5036 : /* The only things that can be initialized are variables. */
5037 11039 : gcc_assert (VAR_P (decl));
5038 :
5039 : /* If this object is not defined, we don't need to do anything
5040 : here. */
5041 11039 : if (DECL_EXTERNAL (decl))
5042 : {
5043 0 : gcc_checking_assert (!STATIC_INIT_DECOMP_BASE_P (t)
5044 : && !STATIC_INIT_DECOMP_NONBASE_P (t));
5045 0 : var = &TREE_CHAIN (t);
5046 0 : continue;
5047 : }
5048 :
5049 : /* Also, if the initializer already contains errors, we can bail
5050 : out now. */
5051 10511 : if (init && TREE_CODE (init) == TREE_LIST
5052 11039 : && value_member (error_mark_node, init))
5053 : {
5054 0 : var = &TREE_CHAIN (t);
5055 0 : continue;
5056 : }
5057 :
5058 : /* Reflections are consteval-only types and we don't want them
5059 : to survive until gimplification. */
5060 11039 : if (consteval_only_p (decl))
5061 : {
5062 0 : var = &TREE_CHAIN (t);
5063 0 : continue;
5064 : }
5065 :
5066 : /* This variable is going to need initialization and/or
5067 : finalization, so we add it to the list. */
5068 11039 : *var = TREE_CHAIN (t);
5069 11039 : TREE_CHAIN (t) = result;
5070 11039 : result = t;
5071 : }
5072 :
5073 283946 : return result;
5074 : }
5075 :
5076 : /* Split VAR_LIST by init priority and add into PARTS hash table.
5077 : This reverses the variable ordering. */
5078 :
5079 : void
5080 5430 : partition_vars_for_init_fini (tree var_list, priority_map_t *(&parts)[4])
5081 : {
5082 5430 : unsigned priority = 0;
5083 5430 : enum { none, base, nonbase } decomp_state = none;
5084 15674 : for (auto node = var_list; node; node = TREE_CHAIN (node))
5085 : {
5086 10244 : tree decl = TREE_VALUE (node);
5087 10244 : tree init = TREE_PURPOSE (node);
5088 10244 : bool has_cleanup = !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl));
5089 10244 : if (decomp_state == base && STATIC_INIT_DECOMP_NONBASE_P (node))
5090 : decomp_state = nonbase;
5091 10208 : else if (decomp_state == nonbase && !STATIC_INIT_DECOMP_NONBASE_P (node))
5092 : decomp_state = none;
5093 10223 : if (decomp_state == none)
5094 20241 : priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
5095 :
5096 10244 : if (init || (flag_use_cxa_atexit && has_cleanup))
5097 : {
5098 : // Add to initialization list.
5099 10238 : if (!parts[true])
5100 5424 : parts[true] = priority_map_t::create_ggc ();
5101 10238 : auto &slot = parts[true]->get_or_insert (priority);
5102 10238 : slot = tree_cons (init, decl, slot);
5103 10238 : if (init
5104 9731 : && STATIC_INIT_DECOMP_BASE_P (node)
5105 10274 : && decomp_state == none)
5106 : {
5107 : /* If one or more STATIC_INIT_DECOMP_BASE_P with at least
5108 : one init is followed by at least one
5109 : STATIC_INIT_DECOMP_NONBASE_P with init, mark it in the
5110 : resulting chain as well. */
5111 36 : for (tree n = TREE_CHAIN (node); n; n = TREE_CHAIN (n))
5112 36 : if (STATIC_INIT_DECOMP_BASE_P (n))
5113 0 : continue;
5114 36 : else if (STATIC_INIT_DECOMP_NONBASE_P (n))
5115 : {
5116 36 : if (TREE_PURPOSE (n))
5117 : {
5118 : decomp_state = base;
5119 : break;
5120 : }
5121 : else
5122 0 : continue;
5123 : }
5124 : else
5125 : break;
5126 : }
5127 10238 : if (init && decomp_state == base)
5128 36 : STATIC_INIT_DECOMP_BASE_P (slot) = 1;
5129 10202 : else if (decomp_state == nonbase)
5130 108 : STATIC_INIT_DECOMP_NONBASE_P (slot) = 1;
5131 : }
5132 :
5133 10244 : if (!flag_use_cxa_atexit && has_cleanup)
5134 : {
5135 : // Add to finalization list.
5136 15 : if (!parts[false])
5137 9 : parts[false] = priority_map_t::create_ggc ();
5138 15 : auto &slot = parts[false]->get_or_insert (priority);
5139 15 : slot = tree_cons (NULL_TREE, decl, slot);
5140 : }
5141 :
5142 10244 : if (flag_openmp
5143 10244 : && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
5144 : {
5145 19 : priority_map_t **omp_parts = parts + 2;
5146 :
5147 19 : if (init || (flag_use_cxa_atexit && has_cleanup))
5148 : {
5149 : // Add to initialization list.
5150 19 : if (!omp_parts[true])
5151 14 : omp_parts[true] = priority_map_t::create_ggc ();
5152 19 : auto &slot = omp_parts[true]->get_or_insert (priority);
5153 19 : slot = tree_cons (init, decl, slot);
5154 19 : if (init && decomp_state == base)
5155 0 : STATIC_INIT_DECOMP_BASE_P (slot) = 1;
5156 19 : else if (decomp_state == nonbase)
5157 0 : STATIC_INIT_DECOMP_NONBASE_P (slot) = 1;
5158 : }
5159 :
5160 19 : if (!flag_use_cxa_atexit && has_cleanup)
5161 : {
5162 : // Add to finalization list.
5163 0 : if (!omp_parts[false])
5164 0 : omp_parts[false] = priority_map_t::create_ggc ();
5165 0 : auto &slot = omp_parts[false]->get_or_insert (priority);
5166 0 : slot = tree_cons (NULL_TREE, decl, slot);
5167 : }
5168 : }
5169 : }
5170 5430 : }
5171 :
5172 : /* Make sure we have told the back end about all the variables in
5173 : VARS. */
5174 :
5175 : static void
5176 5553 : write_out_vars (tree vars)
5177 : {
5178 5553 : tree v;
5179 :
5180 16592 : for (v = vars; v; v = TREE_CHAIN (v))
5181 : {
5182 11039 : tree var = TREE_VALUE (v);
5183 11039 : if (!var_finalized_p (var))
5184 : {
5185 807 : import_export_decl (var);
5186 807 : rest_of_decl_compilation (var, 1, 1);
5187 : }
5188 : }
5189 5553 : }
5190 :
5191 : /* Generate a static constructor or destructor that calls the given
5192 : init/fini fns at the indicated priority. */
5193 :
5194 : static void
5195 7409 : generate_ctor_or_dtor_function (bool initp, unsigned priority,
5196 : tree fns, location_t locus, bool omp_target)
5197 : {
5198 7409 : input_location = locus;
5199 7409 : tree body = start_objects (initp, priority, bool (fns), omp_target);
5200 :
5201 7409 : if (fns)
5202 : {
5203 : /* To make sure dynamic construction doesn't access globals from
5204 : other compilation units where they might not be yet
5205 : constructed, for -fsanitize=address insert
5206 : __asan_before_dynamic_init call that prevents access to
5207 : either all global variables that need construction in other
5208 : compilation units, or at least those that haven't been
5209 : initialized yet. Variables that need dynamic construction in
5210 : the current compilation unit are kept accessible. */
5211 5493 : if (initp && (flag_sanitize & SANITIZE_ADDRESS))
5212 21 : finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
5213 :
5214 : /* Call the static init/fini functions. */
5215 11001 : for (tree node = fns; node; node = TREE_CHAIN (node))
5216 : {
5217 5508 : tree fn = TREE_PURPOSE (node);
5218 :
5219 : // We should never find a pure or constant cdtor.
5220 5508 : gcc_checking_assert (!(flags_from_decl_or_type (fn)
5221 : & (ECF_CONST | ECF_PURE)));
5222 :
5223 5508 : tree call = cp_build_function_call_nary (fn, tf_warning_or_error,
5224 : NULL_TREE);
5225 5508 : finish_expr_stmt (call);
5226 : }
5227 :
5228 : /* Revert what __asan_before_dynamic_init did by calling
5229 : __asan_after_dynamic_init. */
5230 5493 : if (initp && (flag_sanitize & SANITIZE_ADDRESS))
5231 21 : finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
5232 : }
5233 :
5234 : /* Close out the function, and arrange for it to be called at init
5235 : or fini time, if non-empty. (Even non-nop module initializer
5236 : functions need this, as we cannot guarantee the module is
5237 : imported somewhere in the program.) */
5238 7409 : expand_or_defer_fn (finish_objects (initp, priority, body, fns != NULL_TREE));
5239 7409 : }
5240 :
5241 : /* Return C++ property of T, based on given operation OP. */
5242 :
5243 : static int
5244 1941 : cpp_check (tree t, cpp_operation op)
5245 : {
5246 1941 : switch (op)
5247 : {
5248 9 : case HAS_DEPENDENT_TEMPLATE_ARGS:
5249 9 : {
5250 9 : tree ti = CLASSTYPE_TEMPLATE_INFO (t);
5251 9 : if (!ti)
5252 : return 0;
5253 9 : ++processing_template_decl;
5254 9 : const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
5255 9 : --processing_template_decl;
5256 9 : return dep;
5257 : }
5258 294 : case IS_ABSTRACT:
5259 294 : return DECL_PURE_VIRTUAL_P (t);
5260 165 : case IS_ASSIGNMENT_OPERATOR:
5261 165 : return DECL_ASSIGNMENT_OPERATOR_P (t);
5262 165 : case IS_CONSTRUCTOR:
5263 330 : return DECL_CONSTRUCTOR_P (t);
5264 165 : case IS_DESTRUCTOR:
5265 330 : return DECL_DESTRUCTOR_P (t);
5266 165 : case IS_COPY_CONSTRUCTOR:
5267 477 : return DECL_COPY_CONSTRUCTOR_P (t);
5268 165 : case IS_MOVE_CONSTRUCTOR:
5269 483 : return DECL_MOVE_CONSTRUCTOR_P (t);
5270 447 : case IS_TEMPLATE:
5271 447 : return TREE_CODE (t) == TEMPLATE_DECL;
5272 366 : case IS_TRIVIAL:
5273 366 : return trivial_type_p (t);
5274 : default:
5275 : return 0;
5276 : }
5277 : }
5278 :
5279 : /* Collect source file references recursively, starting from NAMESPC. */
5280 :
5281 : static void
5282 78 : collect_source_refs (tree namespc)
5283 : {
5284 : /* Iterate over names in this name space. */
5285 183193 : for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
5286 183115 : if (DECL_IS_UNDECLARED_BUILTIN (t))
5287 : ;
5288 240 : else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
5289 3 : collect_source_refs (t);
5290 : else
5291 237 : collect_source_ref (DECL_SOURCE_FILE (t));
5292 78 : }
5293 :
5294 : /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
5295 : starting from NAMESPC. */
5296 :
5297 : static void
5298 228 : collect_ada_namespace (tree namespc, const char *source_file)
5299 : {
5300 228 : tree decl = NAMESPACE_LEVEL (namespc)->names;
5301 :
5302 : /* Collect decls from this namespace. This will skip
5303 : NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
5304 228 : collect_ada_nodes (decl, source_file);
5305 :
5306 : /* Now scan for namespace children, and dump them. */
5307 219371 : for (; decl; decl = TREE_CHAIN (decl))
5308 218915 : if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
5309 153 : collect_ada_namespace (decl, source_file);
5310 228 : }
5311 :
5312 : /* Returns true iff there is a definition available for variable or
5313 : function DECL. */
5314 :
5315 : bool
5316 79535174 : decl_defined_p (tree decl)
5317 : {
5318 79535174 : if (TREE_CODE (decl) == FUNCTION_DECL)
5319 71606881 : return (DECL_INITIAL (decl) != NULL_TREE
5320 : /* A pending instantiation of a friend temploid is defined. */
5321 71606881 : || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
5322 7363503 : && DECL_INITIAL (DECL_TEMPLATE_RESULT
5323 : (DECL_TI_TEMPLATE (decl)))));
5324 : else
5325 : {
5326 7928293 : gcc_assert (VAR_P (decl));
5327 7928293 : return !DECL_EXTERNAL (decl);
5328 : }
5329 : }
5330 :
5331 : /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
5332 :
5333 : [expr.const]
5334 :
5335 : An integral constant-expression can only involve ... const
5336 : variables of integral or enumeration types initialized with
5337 : constant expressions ...
5338 :
5339 : C++0x also allows constexpr variables and temporaries initialized
5340 : with constant expressions. We handle the former here, but the latter
5341 : are just folded away in cxx_eval_constant_expression.
5342 :
5343 : The standard does not require that the expression be non-volatile.
5344 : G++ implements the proposed correction in DR 457. */
5345 :
5346 : bool
5347 1103130275 : decl_constant_var_p (tree decl)
5348 : {
5349 1103130275 : if (!decl_maybe_constant_var_p (decl))
5350 : return false;
5351 :
5352 : /* We don't know if a template static data member is initialized with
5353 : a constant expression until we instantiate its initializer. Even
5354 : in the case of a constexpr variable, we can't treat it as a
5355 : constant until its initializer is complete in case it's used in
5356 : its own initializer. */
5357 140450572 : maybe_instantiate_decl (decl);
5358 140447875 : return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
5359 : }
5360 :
5361 : /* Returns true if DECL could be a symbolic constant variable, depending on
5362 : its initializer. */
5363 :
5364 : bool
5365 1457043018 : decl_maybe_constant_var_p (tree decl)
5366 : {
5367 1457043018 : tree type = TREE_TYPE (decl);
5368 1457043018 : if (!VAR_P (decl))
5369 : return false;
5370 693821265 : if (DECL_DECLARED_CONSTEXPR_P (decl)
5371 693821265 : && (!TREE_THIS_VOLATILE (decl) || NULLPTR_TYPE_P (type)))
5372 : return true;
5373 391515067 : if (DECL_HAS_VALUE_EXPR_P (decl))
5374 : /* A proxy isn't constant. */
5375 : return false;
5376 389378515 : if (TYPE_REF_P (type))
5377 : /* References can be constant. */;
5378 383371456 : else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
5379 383371456 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5380 : /* And const integers. */;
5381 : else
5382 : return false;
5383 :
5384 81344827 : if (DECL_INITIAL (decl)
5385 126130142 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
5386 : /* We know the initializer, and it isn't constant. */
5387 : return false;
5388 : else
5389 : return true;
5390 : }
5391 :
5392 : /* Complain that DECL uses a type with no linkage. In C++98 mode this is
5393 : called from grokfndecl and grokvardecl; in all modes it is called from
5394 : cp_write_global_declarations. */
5395 :
5396 : void
5397 1595895 : no_linkage_error (tree decl)
5398 : {
5399 1595895 : if (cxx_dialect >= cxx11
5400 1595895 : && (decl_defined_p (decl)
5401 : /* Treat templates which limit_bad_template_recursion decided
5402 : not to instantiate as if they were defined. */
5403 128 : || (errorcount + sorrycount > 0
5404 53 : && DECL_LANG_SPECIFIC (decl)
5405 53 : && DECL_TEMPLATE_INFO (decl)
5406 33 : && warning_suppressed_p (decl /* What warning? */))))
5407 : /* In C++11 it's ok if the decl is defined. */
5408 474955 : return;
5409 :
5410 1120940 : if (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl))
5411 : /* An imported decl is ok. */
5412 : return;
5413 :
5414 : /* Metafunctions are magic and should be considered defined even though
5415 : they have no bodies. ??? This can't be checked in decl_defined_p;
5416 : we'd get redefinition errors for some of our metafunctions. */
5417 1120895 : if (TREE_CODE (decl) == FUNCTION_DECL && metafunction_p (decl))
5418 : return;
5419 :
5420 1120892 : tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
5421 1120892 : if (t == NULL_TREE)
5422 : /* The type that got us on no_linkage_decls must have gotten a name for
5423 : linkage purposes. */;
5424 74 : else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
5425 : // FIXME: This is now invalid, as a DR to c++98
5426 : /* The type might end up having a typedef name for linkage purposes. */
5427 0 : vec_safe_push (no_linkage_decls, decl);
5428 201 : else if (TYPE_UNNAMED_P (t))
5429 : {
5430 26 : bool d = false;
5431 26 : auto_diagnostic_group grp;
5432 26 : if (cxx_dialect >= cxx11)
5433 : {
5434 : /* If t is declared in a module CMI, then decl could actually
5435 : be defined in a different TU, so don't warn since C++20. */
5436 9 : tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
5437 9 : if (relaxed != NULL_TREE)
5438 9 : d = permerror (DECL_SOURCE_LOCATION (decl),
5439 : "%q#D, declared using an unnamed type, "
5440 : "is used but never defined", decl);
5441 0 : else if (cxx_dialect < cxx20)
5442 0 : d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
5443 : "%q#D, declared using an unnamed type, "
5444 : "is used but not defined", decl);
5445 : }
5446 17 : else if (DECL_EXTERN_C_P (decl))
5447 : /* Allow this; it's pretty common in C. */;
5448 17 : else if (VAR_P (decl))
5449 : /* DRs 132, 319 and 389 seem to indicate types with
5450 : no linkage can only be used to declare extern "C"
5451 : entities. Since it's not always an error in the
5452 : ISO C++ 90 Standard, we only issue a warning. */
5453 16 : d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
5454 : "with no linkage used to declare variable %q#D with "
5455 : "linkage", decl);
5456 : else
5457 1 : d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
5458 : "linkage used to declare function %q#D with linkage",
5459 : decl);
5460 26 : if (d && is_typedef_decl (TYPE_NAME (t)))
5461 0 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
5462 : "to the unqualified type, so it is not used for linkage",
5463 0 : TYPE_NAME (t));
5464 : /* Suppress warning from check_global_declaration if needed. */
5465 26 : if (d)
5466 23 : suppress_warning (decl, OPT_Wunused);
5467 26 : }
5468 48 : else if (cxx_dialect >= cxx11)
5469 : {
5470 48 : if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
5471 : {
5472 : /* Similarly for local types in a function with vague linkage or
5473 : defined in a module CMI, then decl could actually be defined
5474 : in a different TU, so don't warn since C++20. */
5475 45 : bool d = false;
5476 45 : tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
5477 45 : if (relaxed != NULL_TREE)
5478 30 : d = permerror (DECL_SOURCE_LOCATION (decl),
5479 : "%q#D, declared using local type "
5480 : "%qT, is used but never defined", decl, t);
5481 15 : else if (cxx_dialect < cxx20)
5482 3 : d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
5483 : "%q#D, declared using local type "
5484 : "%qT, is used but not defined here", decl, t);
5485 : /* Suppress warning from check_global_declaration if needed. */
5486 33 : if (d)
5487 33 : suppress_warning (decl, OPT_Wunused);
5488 : }
5489 : }
5490 0 : else if (VAR_P (decl))
5491 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
5492 : "used to declare variable %q#D with linkage", t, decl);
5493 : else
5494 0 : permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
5495 : "to declare function %q#D with linkage", t, decl);
5496 : }
5497 :
5498 : /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
5499 :
5500 : static void
5501 75 : collect_all_refs (const char *source_file)
5502 : {
5503 75 : collect_ada_namespace (global_namespace, source_file);
5504 75 : }
5505 :
5506 : /* Clear DECL_EXTERNAL for NODE. */
5507 :
5508 : static bool
5509 147693008 : clear_decl_external (struct cgraph_node *node, void * /*data*/)
5510 : {
5511 147693008 : DECL_EXTERNAL (node->decl) = 0;
5512 147693008 : return false;
5513 : }
5514 :
5515 : /* Build up the function to run dynamic initializers for thread_local
5516 : variables in this translation unit and alias the init functions for the
5517 : individual variables to it. */
5518 :
5519 : static void
5520 141973 : handle_tls_init (void)
5521 : {
5522 141973 : tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
5523 141973 : if (vars == NULL_TREE)
5524 141850 : return;
5525 :
5526 123 : location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
5527 :
5528 123 : write_out_vars (vars);
5529 :
5530 123 : tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
5531 : boolean_type_node);
5532 123 : TREE_PUBLIC (guard) = false;
5533 123 : TREE_STATIC (guard) = true;
5534 123 : DECL_ARTIFICIAL (guard) = true;
5535 123 : DECL_IGNORED_P (guard) = true;
5536 123 : TREE_USED (guard) = true;
5537 123 : CP_DECL_THREAD_LOCAL_P (guard) = true;
5538 123 : set_decl_tls_model (guard, decl_default_tls_model (guard));
5539 123 : pushdecl_top_level_and_finish (guard, NULL_TREE);
5540 :
5541 123 : tree fn = get_local_tls_init_fn (loc);
5542 123 : start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
5543 123 : tree body = begin_function_body ();
5544 123 : tree if_stmt = begin_if_stmt ();
5545 123 : tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
5546 : tf_warning_or_error);
5547 123 : finish_if_stmt_cond (cond, if_stmt);
5548 123 : finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
5549 : boolean_true_node,
5550 : tf_warning_or_error));
5551 123 : tree sl = NULL_TREE;
5552 123 : int save_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5553 123 : bool saw_nonbase = false;
5554 918 : for (; vars; vars = TREE_CHAIN (vars))
5555 : {
5556 795 : tree var = TREE_VALUE (vars);
5557 795 : tree init = TREE_PURPOSE (vars);
5558 795 : sl = decomp_handle_one_var (vars, sl, &saw_nonbase,
5559 : save_stmts_are_full_exprs_p);
5560 795 : one_static_initialization_or_destruction (/*initp=*/true, var, init,
5561 : false);
5562 :
5563 : /* Output init aliases even with -fno-extern-tls-init. */
5564 795 : if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
5565 : {
5566 696 : tree single_init_fn = get_tls_init_fn (var);
5567 696 : if (single_init_fn == NULL_TREE)
5568 0 : continue;
5569 696 : cgraph_node *alias
5570 696 : = cgraph_node::get_create (fn)->create_same_body_alias
5571 696 : (single_init_fn, fn);
5572 696 : gcc_assert (alias != NULL);
5573 : }
5574 : }
5575 123 : decomp_finalize_var_list (sl, save_stmts_are_full_exprs_p);
5576 :
5577 123 : finish_then_clause (if_stmt);
5578 123 : finish_if_stmt (if_stmt);
5579 123 : finish_function_body (body);
5580 123 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
5581 : }
5582 :
5583 : /* We're at the end of compilation, so generate any mangling aliases that
5584 : we've been saving up, if DECL is going to be output and ID2 isn't
5585 : already taken by another declaration. */
5586 :
5587 : static void
5588 27760 : generate_mangling_alias (tree decl, tree id2)
5589 : {
5590 27760 : struct cgraph_node *n = NULL;
5591 :
5592 27760 : if (TREE_CODE (decl) == FUNCTION_DECL)
5593 : {
5594 27701 : n = cgraph_node::get (decl);
5595 27701 : if (!n)
5596 : /* Don't create an alias to an unreferenced function. */
5597 : return;
5598 : }
5599 :
5600 27760 : tree *slot
5601 27760 : = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
5602 : INSERT);
5603 :
5604 : /* If there's a declaration already using this mangled name,
5605 : don't create a compatibility alias that conflicts. */
5606 27760 : if (*slot)
5607 : return;
5608 :
5609 27730 : tree alias = make_alias_for (decl, id2);
5610 27730 : *slot = alias;
5611 :
5612 27730 : DECL_IGNORED_P (alias) = 1;
5613 27730 : TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
5614 27730 : DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
5615 27730 : if (vague_linkage_p (decl))
5616 27577 : DECL_WEAK (alias) = 1;
5617 :
5618 27730 : if (n)
5619 27671 : n->create_same_body_alias (alias, decl);
5620 : else
5621 59 : varpool_node::create_extra_name_alias (alias, decl);
5622 : }
5623 :
5624 : /* Note that we might want to emit an alias with the symbol ID2 for DECL at
5625 : the end of translation, for compatibility across bugs in the mangling
5626 : implementation. */
5627 :
5628 : void
5629 27790 : note_mangling_alias (tree decl, tree id2)
5630 : {
5631 27790 : if (TARGET_SUPPORTS_ALIASES)
5632 : {
5633 27790 : if (!defer_mangling_aliases)
5634 18889 : generate_mangling_alias (decl, id2);
5635 : else
5636 : {
5637 8901 : vec_safe_push (mangling_aliases, decl);
5638 8901 : vec_safe_push (mangling_aliases, id2);
5639 : }
5640 : }
5641 27790 : }
5642 :
5643 : /* Emit all mangling aliases that were deferred up to this point. */
5644 :
5645 : void
5646 96776 : generate_mangling_aliases ()
5647 : {
5648 105647 : while (!vec_safe_is_empty (mangling_aliases))
5649 : {
5650 8871 : tree id2 = mangling_aliases->pop();
5651 8871 : tree decl = mangling_aliases->pop();
5652 8871 : generate_mangling_alias (decl, id2);
5653 : }
5654 96776 : defer_mangling_aliases = false;
5655 96776 : }
5656 :
5657 : /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
5658 : set. NEED_WARNING is true if we must warn about collisions. We do
5659 : this to spot changes in mangling that may require compatibility
5660 : aliases. */
5661 :
5662 : void
5663 80789280 : record_mangling (tree decl, bool need_warning)
5664 : {
5665 80789280 : if (!mangled_decls)
5666 74813 : mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
5667 :
5668 80789280 : gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
5669 80789280 : tree id = DECL_ASSEMBLER_NAME_RAW (decl);
5670 80789280 : tree *slot
5671 80789280 : = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
5672 : INSERT);
5673 :
5674 : /* If this is already an alias, cancel the alias, because the real
5675 : decl takes precedence. */
5676 80789280 : if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
5677 : {
5678 27 : if (symtab_node *n = symtab_node::get (*slot))
5679 : {
5680 27 : if (n->cpp_implicit_alias)
5681 : /* Actually removing the node isn't safe if other code is already
5682 : holding a pointer to it, so just neutralize it. */
5683 27 : n->reset ();
5684 : }
5685 : else
5686 : /* analyze_functions might have already removed the alias from the
5687 : symbol table if it's internal. */
5688 0 : gcc_checking_assert (!TREE_PUBLIC (*slot));
5689 :
5690 27 : *slot = NULL_TREE;
5691 : }
5692 :
5693 80789280 : if (!*slot)
5694 80789058 : *slot = decl;
5695 222 : else if (need_warning)
5696 : {
5697 3 : auto_diagnostic_group d;
5698 3 : error_at (DECL_SOURCE_LOCATION (decl),
5699 : "mangling of %q#D as %qE conflicts with a previous mangle",
5700 : decl, id);
5701 3 : inform (DECL_SOURCE_LOCATION (*slot),
5702 : "previous mangling %q#D", *slot);
5703 3 : inform (DECL_SOURCE_LOCATION (decl),
5704 : "a later %<-fabi-version=%> (or =0)"
5705 : " avoids this error with a change in mangling");
5706 3 : *slot = decl;
5707 3 : }
5708 80789280 : }
5709 :
5710 : /* The mangled name of DECL is being forcibly changed to NAME. Remove
5711 : any existing knowledge of DECL's mangled name meaning DECL. */
5712 :
5713 : void
5714 266894906 : overwrite_mangling (tree decl, tree name)
5715 : {
5716 266894906 : if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
5717 229545 : if ((TREE_CODE (decl) == VAR_DECL
5718 229455 : || TREE_CODE (decl) == FUNCTION_DECL)
5719 229545 : && mangled_decls)
5720 445890 : if (tree *slot
5721 222945 : = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
5722 : NO_INSERT))
5723 118834 : if (*slot == decl)
5724 : {
5725 75 : mangled_decls->clear_slot (slot);
5726 :
5727 : /* If this is an alias, remove it from the symbol table. */
5728 75 : if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
5729 0 : if (symtab_node *n = symtab_node::get (decl))
5730 0 : if (n->cpp_implicit_alias)
5731 0 : n->remove ();
5732 : }
5733 :
5734 266894906 : DECL_ASSEMBLER_NAME_RAW (decl) = name;
5735 266894906 : }
5736 :
5737 : /* The entire file is now complete. If requested, dump everything
5738 : to a file. */
5739 :
5740 : static void
5741 96873 : dump_tu (void)
5742 : {
5743 96873 : dump_flags_t flags;
5744 96873 : if (FILE *stream = dump_begin (raw_dump_id, &flags))
5745 : {
5746 6 : dump_node (global_namespace, flags & ~TDF_SLIM, stream);
5747 6 : dump_end (raw_dump_id, stream);
5748 : }
5749 96873 : }
5750 :
5751 : static location_t locus_at_end_of_parsing;
5752 :
5753 : /* Check the deallocation functions for CODE to see if we want to warn that
5754 : only one was defined. */
5755 :
5756 : static void
5757 1946 : maybe_warn_sized_delete (enum tree_code code)
5758 : {
5759 1946 : tree sized = NULL_TREE;
5760 1946 : tree unsized = NULL_TREE;
5761 :
5762 10728 : for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
5763 10728 : iter; ++iter)
5764 : {
5765 8782 : tree fn = *iter;
5766 : /* We're only interested in usual deallocation functions. */
5767 8782 : if (!usual_deallocation_fn_p (fn))
5768 998 : continue;
5769 7784 : if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5770 : unsized = fn;
5771 : else
5772 5834 : sized = fn;
5773 : }
5774 1946 : if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
5775 6 : warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
5776 : "the program should also define %qD", sized);
5777 1940 : else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
5778 4 : warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
5779 : "the program should also define %qD", unsized);
5780 1946 : }
5781 :
5782 : /* Check the global deallocation functions to see if we want to warn about
5783 : defining unsized without sized (or vice versa). */
5784 :
5785 : static void
5786 96776 : maybe_warn_sized_delete ()
5787 : {
5788 96776 : if (!flag_sized_deallocation || !warn_sized_deallocation)
5789 : return;
5790 973 : maybe_warn_sized_delete (DELETE_EXPR);
5791 973 : maybe_warn_sized_delete (VEC_DELETE_EXPR);
5792 : }
5793 :
5794 : /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
5795 : look them up when evaluating non-type template parameters. Now we need to
5796 : lower them to something the back end can understand. */
5797 :
5798 : static void
5799 96776 : lower_var_init ()
5800 : {
5801 96776 : varpool_node *node;
5802 32303238 : FOR_EACH_VARIABLE (node)
5803 : {
5804 32206462 : tree d = node->decl;
5805 32206462 : if (tree init = DECL_INITIAL (d))
5806 29841691 : DECL_INITIAL (d) = cplus_expand_constant (init);
5807 : }
5808 96776 : }
5809 :
5810 : /* This routine is called at the end of compilation.
5811 : Its job is to create all the code needed to initialize and
5812 : destroy the global aggregates. We do the destruction
5813 : first, since that way we only need to reverse the decls once. */
5814 :
5815 : void
5816 96888 : c_parse_final_cleanups (void)
5817 : {
5818 96888 : size_t i;
5819 96888 : tree decl;
5820 :
5821 96888 : locus_at_end_of_parsing = input_location;
5822 : /* We're done parsing. */
5823 96888 : at_eof = 1;
5824 :
5825 : /* Bad parse errors. Just forget about it. */
5826 193776 : if (! global_bindings_p () || current_class_type
5827 193776 : || !vec_safe_is_empty (decl_namespace_list))
5828 100 : return;
5829 :
5830 : /* This is the point to write out a PCH if we're doing that.
5831 : In that case we do not want to do anything else. */
5832 96885 : if (pch_file)
5833 : {
5834 : /* Mangle all symbols at PCH creation time. */
5835 97 : symtab_node *node;
5836 49522 : FOR_EACH_SYMBOL (node)
5837 49425 : if (! is_a <varpool_node *> (node)
5838 16312 : || ! DECL_HARD_REGISTER (node->decl))
5839 49425 : DECL_ASSEMBLER_NAME (node->decl);
5840 97 : c_common_write_pch ();
5841 97 : dump_tu ();
5842 : /* Ensure even the callers don't try to finalize the CU. */
5843 97 : flag_syntax_only = 1;
5844 97 : return;
5845 : }
5846 :
5847 96788 : timevar_stop (TV_PHASE_PARSING);
5848 96788 : timevar_start (TV_PHASE_DEFERRED);
5849 :
5850 96788 : symtab->process_same_body_aliases ();
5851 :
5852 : /* Handle -fdump-ada-spec[-slim] */
5853 96788 : if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
5854 : {
5855 75 : collect_source_ref (main_input_filename);
5856 75 : if (!flag_dump_ada_spec_slim)
5857 75 : collect_source_refs (global_namespace);
5858 :
5859 75 : dump_ada_specs (collect_all_refs, cpp_check);
5860 : }
5861 :
5862 : /* FIXME - huh? was input_line -= 1;*/
5863 :
5864 : /* We now have to write out all the stuff we put off writing out.
5865 : These include:
5866 :
5867 : o Template specializations that we have not yet instantiated,
5868 : but which are needed.
5869 : o Initialization and destruction for non-local objects with
5870 : static storage duration. (Local objects with static storage
5871 : duration are initialized when their scope is first entered,
5872 : and are cleaned up via atexit.)
5873 : o Virtual function tables.
5874 :
5875 : All of these may cause others to be needed. For example,
5876 : instantiating one function may cause another to be needed, and
5877 : generating the initializer for an object may cause templates to be
5878 : instantiated, etc., etc. */
5879 :
5880 96788 : emit_support_tinfos ();
5881 :
5882 : /* Track vtables we want to emit that refer to consteval functions. */
5883 96788 : auto_vec<tree> consteval_vtables;
5884 :
5885 96788 : int retries = 0;
5886 96788 : unsigned ssdf_count = 0, omp_ssdf_count = 0;
5887 239672 : for (bool reconsider = true; reconsider; retries++)
5888 : {
5889 142896 : reconsider = false;
5890 :
5891 : /* If there are templates that we've put off instantiating, do
5892 : them now. */
5893 142896 : instantiate_pending_templates (retries);
5894 142884 : ggc_collect ();
5895 :
5896 142884 : if (header_module_p ())
5897 : /* A header modules initializations are handled in its
5898 : importer. */
5899 911 : continue;
5900 :
5901 : /* Emit wrappers where needed, and if that causes more to be added then
5902 : make sure we account for possible additional instantiations. */
5903 141973 : if (flag_contracts)
5904 29005 : if (emit_contract_wrapper_func (/*done*/false))
5905 141973 : reconsider = true;
5906 :
5907 : /* Write out virtual tables as required. Writing out the
5908 : virtual table for a template class may cause the
5909 : instantiation of members of that class. If we write out
5910 : vtables then we remove the class from our list so we don't
5911 : have to look at it again. */
5912 141973 : tree t;
5913 141973 : for (i = keyed_classes->length ();
5914 3716370 : keyed_classes->iterate (--i, &t);)
5915 3574397 : if (maybe_emit_vtables (t, consteval_vtables))
5916 : {
5917 339828 : reconsider = true;
5918 339828 : keyed_classes->unordered_remove (i);
5919 : }
5920 : /* The input_location may have been changed during marking of
5921 : vtable entries. */
5922 141973 : input_location = locus_at_end_of_parsing;
5923 :
5924 : /* Write out needed type info variables. We have to be careful
5925 : looping through unemitted decls, because emit_tinfo_decl may
5926 : cause other variables to be needed. New elements will be
5927 : appended, and we remove from the vector those that actually
5928 : get emitted. */
5929 141973 : for (i = unemitted_tinfo_decls->length ();
5930 5930396 : unemitted_tinfo_decls->iterate (--i, &t);)
5931 5788423 : if (DECL_INITIAL (t) || emit_tinfo_decl (t))
5932 : {
5933 424464 : reconsider = true;
5934 424464 : unemitted_tinfo_decls->unordered_remove (i);
5935 : }
5936 :
5937 : /* The list of objects with static storage duration is built up
5938 : in reverse order. We clear STATIC_AGGREGATES so that any new
5939 : aggregates added during the initialization of these will be
5940 : initialized in the correct order when we next come around the
5941 : loop. */
5942 141973 : if (tree vars = prune_vars_needing_no_initialization (&static_aggregates))
5943 : {
5944 5430 : if (flag_openmp)
5945 : /* Add initializer information from VARS into
5946 : DYNAMIC_INITIALIZERS. */
5947 394 : for (t = vars; t; t = TREE_CHAIN (t))
5948 566 : hash_map_safe_put<hm_ggc> (dynamic_initializers,
5949 283 : TREE_VALUE (t), TREE_PURPOSE (t));
5950 :
5951 : /* Make sure the back end knows about all the variables. */
5952 5430 : write_out_vars (vars);
5953 :
5954 5430 : function_depth++; // Disable GC
5955 5430 : priority_map_t *parts[4] = {nullptr, nullptr, nullptr, nullptr};
5956 5430 : partition_vars_for_init_fini (vars, parts);
5957 5430 : tree host_init_fini[2] = { NULL_TREE, NULL_TREE };
5958 :
5959 16290 : for (unsigned initp = 2; initp--;)
5960 10860 : if (parts[initp])
5961 16313 : for (auto iter : *parts[initp])
5962 : {
5963 5447 : auto list = iter.second;
5964 5447 : if (initp)
5965 : // Partitioning kept the vars in reverse order.
5966 : // We only want that for dtors.
5967 5438 : list = nreverse (list);
5968 5447 : host_init_fini[initp]
5969 5447 : = emit_partial_init_fini_fn (initp, iter.first, list,
5970 : ssdf_count++,
5971 : locus_at_end_of_parsing,
5972 : NULL_TREE);
5973 : }
5974 :
5975 5430 : if (flag_openmp)
5976 : {
5977 : priority_map_t **omp_parts = parts + 2;
5978 333 : for (unsigned initp = 2; initp--;)
5979 222 : if (omp_parts[initp])
5980 58 : for (auto iter : *omp_parts[initp])
5981 : {
5982 15 : auto list = iter.second;
5983 15 : if (initp)
5984 : // Partitioning kept the vars in reverse order.
5985 : // We only want that for dtors.
5986 15 : list = nreverse (list);
5987 15 : emit_partial_init_fini_fn (initp, iter.first, list,
5988 : omp_ssdf_count++,
5989 : locus_at_end_of_parsing,
5990 : host_init_fini[initp]);
5991 : }
5992 : }
5993 :
5994 5430 : function_depth--; // Re-enable GC
5995 :
5996 : /* All those initializations and finalizations might cause
5997 : us to need more inline functions, more template
5998 : instantiations, etc. */
5999 5430 : reconsider = true;
6000 : }
6001 :
6002 : /* Now do the same for thread_local variables. */
6003 141973 : handle_tls_init ();
6004 :
6005 : /* Go through the set of inline functions whose bodies have not
6006 : been emitted yet. If out-of-line copies of these functions
6007 : are required, emit them. */
6008 185295934 : FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
6009 : {
6010 : /* Does it need synthesizing? */
6011 190761526 : if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
6012 185153967 : && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
6013 : {
6014 : /* Even though we're already at the top-level, we push
6015 : there again. That way, when we pop back a few lines
6016 : hence, all of our state is restored. Otherwise,
6017 : finish_function doesn't clean things up, and we end
6018 : up with CURRENT_FUNCTION_DECL set. */
6019 6 : push_to_top_level ();
6020 : /* The decl's location will mark where it was first
6021 : needed. Save that so synthesize method can indicate
6022 : where it was needed from, in case of error */
6023 6 : input_location = DECL_SOURCE_LOCATION (decl);
6024 6 : synthesize_method (decl);
6025 6 : pop_from_top_level ();
6026 6 : reconsider = true;
6027 : }
6028 :
6029 185153961 : if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
6030 586 : generate_tls_wrapper (decl);
6031 :
6032 185153961 : if (!DECL_SAVED_TREE (decl))
6033 210717 : continue;
6034 :
6035 184943244 : cgraph_node *node = cgraph_node::get_create (decl);
6036 :
6037 : /* We lie to the back end, pretending that some functions
6038 : are not defined when they really are. This keeps these
6039 : functions from being put out unnecessarily. But, we must
6040 : stop lying when the functions are referenced, or if they
6041 : are not comdat since they need to be put out now. If
6042 : DECL_INTERFACE_KNOWN, then we have already set
6043 : DECL_EXTERNAL appropriately, so there's no need to check
6044 : again, and we do not want to clear DECL_EXTERNAL if a
6045 : previous call to import_export_decl set it.
6046 :
6047 : This is done in a separate for cycle, because if some
6048 : deferred function is contained in another deferred
6049 : function later in deferred_fns varray,
6050 : rest_of_compilation would skip this function and we
6051 : really cannot expand the same function twice. */
6052 184943244 : import_export_decl (decl);
6053 184943244 : if (DECL_NOT_REALLY_EXTERN (decl)
6054 178151588 : && DECL_INITIAL (decl)
6055 363094832 : && decl_needed_p (decl))
6056 : {
6057 117101848 : if (node->cpp_implicit_alias)
6058 10334833 : node = node->get_alias_target ();
6059 :
6060 117101848 : node->call_for_symbol_thunks_and_aliases (clear_decl_external,
6061 : NULL, true);
6062 : /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
6063 : group, we need to mark all symbols in the same comdat group
6064 : that way. */
6065 117101848 : if (node->same_comdat_group)
6066 30569092 : for (cgraph_node *next
6067 15070432 : = dyn_cast<cgraph_node *> (node->same_comdat_group);
6068 30569092 : next != node;
6069 46067752 : next = dyn_cast<cgraph_node *> (next->same_comdat_group))
6070 15498660 : next->call_for_symbol_thunks_and_aliases (clear_decl_external,
6071 : NULL, true);
6072 : }
6073 :
6074 : /* If we're going to need to write this function out, and
6075 : there's already a body for it, create RTL for it now.
6076 : (There might be no body if this is a method we haven't
6077 : gotten around to synthesizing yet.) */
6078 184943244 : if (!DECL_EXTERNAL (decl)
6079 119949801 : && decl_needed_p (decl)
6080 117768287 : && !TREE_ASM_WRITTEN (decl)
6081 215738106 : && !DECL_IMMEDIATE_FUNCTION_P (decl)
6082 287542550 : && !node->definition)
6083 : {
6084 : /* We will output the function; no longer consider it in this
6085 : loop. */
6086 0 : DECL_DEFER_OUTPUT (decl) = 0;
6087 : /* Generate RTL for this function now that we know we
6088 : need it. */
6089 0 : expand_or_defer_fn (decl);
6090 0 : reconsider = true;
6091 : }
6092 : }
6093 :
6094 141973 : if (wrapup_namespace_globals ())
6095 555 : reconsider = true;
6096 :
6097 : /* Static data members are just like namespace-scope globals. */
6098 97339363 : FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
6099 : {
6100 180013665 : if (consteval_only_p (decl)
6101 97197248 : || var_finalized_p (decl)
6102 25162384 : || DECL_REALLY_EXTERN (decl)
6103 : /* Don't write it out if we haven't seen a definition. */
6104 111585153 : || DECL_IN_AGGR_P (decl))
6105 82816275 : continue;
6106 14381115 : import_export_decl (decl);
6107 : /* If this static data member is needed, provide it to the
6108 : back end. */
6109 14381115 : if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
6110 14351068 : DECL_EXTERNAL (decl) = 0;
6111 : }
6112 :
6113 284857 : if (vec_safe_length (pending_statics) != 0
6114 114134 : && wrapup_global_declarations (pending_statics->address (),
6115 57067 : pending_statics->length ()))
6116 : reconsider = true;
6117 : }
6118 :
6119 96776 : if (flag_contracts)
6120 : {
6121 23500 : emit_contract_wrapper_func (/*done*/true);
6122 23500 : maybe_emit_violation_handler_wrappers ();
6123 : }
6124 :
6125 : /* All templates have been instantiated. */
6126 96776 : at_eof = 2;
6127 :
6128 96776 : void *module_cookie = finish_module_processing (parse_in);
6129 :
6130 96776 : lower_var_init ();
6131 :
6132 96776 : generate_mangling_aliases ();
6133 :
6134 : /* All used inline functions must have a definition at this point. */
6135 48316619 : FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
6136 : {
6137 48219843 : if (/* Check online inline functions that were actually used. */
6138 78643115 : DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
6139 : /* If the definition actually was available here, then the
6140 : fact that the function was not defined merely represents
6141 : that for some reason (use of a template repository,
6142 : #pragma interface, etc.) we decided not to emit the
6143 : definition here. */
6144 30421868 : && !DECL_INITIAL (decl)
6145 : /* A defaulted fn or TLS wrapper in a header module can be
6146 : synthesized on demand later. (In non-header modules we
6147 : should have synthesized it above.) */
6148 4740 : && !(header_module_p ()
6149 11 : && (DECL_DEFAULTED_FN (decl) || decl_tls_wrapper_p (decl)))
6150 : /* Metafunctions are never defined. */
6151 4734 : && !metafunction_p (decl)
6152 : /* Don't complain if the template was defined. */
6153 715 : && !(DECL_TEMPLOID_INSTANTIATION (decl)
6154 639 : && DECL_INITIAL (DECL_TEMPLATE_RESULT
6155 : (template_for_substitution (decl))))
6156 78643115 : && warning_at (DECL_SOURCE_LOCATION (decl), 0,
6157 : "inline function %qD used but never defined", decl))
6158 : /* Avoid a duplicate warning from check_global_declaration. */
6159 73 : suppress_warning (decl, OPT_Wunused);
6160 : }
6161 :
6162 : /* So must decls that use a type with no linkage. */
6163 571836 : FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
6164 475060 : no_linkage_error (decl);
6165 :
6166 96776 : maybe_warn_sized_delete ();
6167 :
6168 : // Place the init fns in the right order. We need to do this now,
6169 : // so that any module init will go at the start.
6170 96776 : if (static_init_fini_fns[true])
6171 16277 : for (auto iter : *static_init_fini_fns[true])
6172 5435 : iter.second = nreverse (iter.second);
6173 :
6174 96776 : if (flag_openmp && static_init_fini_fns[2 + true])
6175 43 : for (auto iter : *static_init_fini_fns[2 + true])
6176 15 : iter.second = nreverse (iter.second);
6177 :
6178 : /* Now we've instantiated all templates. Now we can escalate the functions
6179 : we squirreled away earlier. */
6180 96776 : process_and_check_pending_immediate_escalating_fns ();
6181 :
6182 : /* Then, do the Objective-C stuff. This is where all the
6183 : Objective-C module stuff gets generated (symtab,
6184 : class/protocol/selector lists etc). This must be done after C++
6185 : templates, destructors etc. so that selectors used in C++
6186 : templates are properly allocated. */
6187 96776 : if (c_dialect_objc ())
6188 0 : objc_write_global_declarations ();
6189 :
6190 96776 : bool has_module_inits = module_determine_import_inits ();
6191 96776 : bool has_objc_init = c_dialect_objc () && objc_static_init_needed_p ();
6192 96776 : if (has_module_inits || has_objc_init)
6193 : {
6194 46 : input_location = locus_at_end_of_parsing;
6195 46 : tree body = start_partial_init_fini_fn (true, DEFAULT_INIT_PRIORITY,
6196 : ssdf_count++, false);
6197 : /* For Objective-C++, we may need to initialize metadata found
6198 : in this module. This must be done _before_ any other static
6199 : initializations. */
6200 46 : if (has_objc_init)
6201 0 : objc_generate_static_init_call (NULL_TREE);
6202 46 : if (has_module_inits)
6203 46 : module_add_import_initializers ();
6204 46 : input_location = locus_at_end_of_parsing;
6205 46 : finish_partial_init_fini_fn (body);
6206 : }
6207 :
6208 96776 : if (module_global_init_needed ())
6209 : {
6210 : // Make sure there's a default priority entry.
6211 1980 : if (!static_init_fini_fns[true])
6212 1916 : static_init_fini_fns[true] = priority_map_t::create_ggc ();
6213 1980 : if (static_init_fini_fns[true]->get_or_insert (DEFAULT_INIT_PRIORITY))
6214 96776 : has_module_inits = true;
6215 :
6216 : /* FIXME: We need to work out what static constructors on OpenMP offload
6217 : target in modules will look like. */
6218 : }
6219 :
6220 : /* Generate initialization and destruction functions for all
6221 : priorities for which they are required. They have C-language
6222 : linkage. */
6223 96776 : push_lang_context (lang_name_c);
6224 483880 : for (unsigned initp = 4; initp--;)
6225 387104 : if (static_init_fini_fns[initp])
6226 : {
6227 14803 : for (auto iter : *static_init_fini_fns[initp])
6228 7409 : generate_ctor_or_dtor_function (initp & 1, iter.first, iter.second,
6229 : locus_at_end_of_parsing,
6230 7409 : (initp & 2) != 0);
6231 7394 : static_init_fini_fns[initp] = nullptr;
6232 : }
6233 96776 : pop_lang_context ();
6234 :
6235 96776 : fini_modules (parse_in, module_cookie, has_module_inits);
6236 :
6237 : /* Generate any missing aliases. */
6238 96776 : maybe_apply_pending_pragma_weaks ();
6239 :
6240 96776 : if (flag_vtable_verify)
6241 : {
6242 9 : vtv_recover_class_info ();
6243 9 : vtv_compute_class_hierarchy_transitive_closure ();
6244 9 : vtv_build_vtable_verify_fndecl ();
6245 : }
6246 :
6247 96776 : perform_deferred_noexcept_checks ();
6248 :
6249 96776 : fini_constexpr ();
6250 96776 : cp_tree_c_finish_parsing ();
6251 96776 : clear_consteval_vfns (consteval_vtables);
6252 :
6253 : /* The entire file is now complete. If requested, dump everything
6254 : to a file. */
6255 96776 : dump_tu ();
6256 :
6257 96776 : if (flag_detailed_statistics)
6258 : {
6259 0 : dump_tree_statistics ();
6260 0 : dump_time_statistics ();
6261 : }
6262 :
6263 96776 : timevar_stop (TV_PHASE_DEFERRED);
6264 96776 : timevar_start (TV_PHASE_PARSING);
6265 :
6266 : /* Indicate that we're done with front end processing. */
6267 96776 : at_eof = 3;
6268 96776 : }
6269 :
6270 : /* Perform any post compilation-proper cleanups for the C++ front-end.
6271 : This should really go away. No front-end should need to do
6272 : anything past the compilation process. */
6273 :
6274 : void
6275 96533 : cxx_post_compilation_parsing_cleanups (void)
6276 : {
6277 96533 : timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
6278 :
6279 96533 : if (flag_vtable_verify)
6280 : {
6281 : /* Generate the special constructor initialization function that
6282 : calls __VLTRegisterPairs, and give it a very high
6283 : initialization priority. This must be done after
6284 : finalize_compilation_unit so that we have accurate
6285 : information about which vtable will actually be emitted. */
6286 9 : vtv_generate_init_routine ();
6287 : }
6288 :
6289 96533 : input_location = locus_at_end_of_parsing;
6290 :
6291 96533 : if (flag_checking)
6292 96524 : validate_conversion_obstack ();
6293 :
6294 96533 : timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
6295 96533 : }
6296 :
6297 : /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
6298 : function to call in parse-tree form; it has not yet been
6299 : semantically analyzed. ARGS are the arguments to the function.
6300 : They have already been semantically analyzed. This may change
6301 : ARGS. */
6302 :
6303 : tree
6304 354902 : build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
6305 : tsubst_flags_t complain)
6306 : {
6307 354902 : tree orig_fn;
6308 354902 : vec<tree, va_gc> *orig_args = NULL;
6309 354902 : tree expr;
6310 354902 : tree object;
6311 :
6312 354902 : orig_fn = fn;
6313 354902 : object = TREE_OPERAND (fn, 0);
6314 :
6315 354902 : if (processing_template_decl)
6316 : {
6317 271311 : gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
6318 : || TREE_CODE (fn) == MEMBER_REF);
6319 271311 : if (type_dependent_expression_p (fn)
6320 271311 : || any_type_dependent_arguments_p (*args))
6321 271272 : return build_min_nt_call_vec (fn, *args);
6322 :
6323 39 : orig_args = make_tree_vector_copy (*args);
6324 :
6325 : /* Transform the arguments and add the implicit "this"
6326 : parameter. */
6327 39 : if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6328 : {
6329 33 : if (TREE_CODE (fn) == DOTSTAR_EXPR)
6330 27 : object = cp_build_addr_expr (object, complain);
6331 33 : vec_safe_insert (*args, 0, object);
6332 : }
6333 : }
6334 :
6335 : /* A qualified name corresponding to a bound pointer-to-member is
6336 : represented as an OFFSET_REF:
6337 :
6338 : struct B { void g(); };
6339 : void (B::*p)();
6340 : void B::g() { (this->*p)(); } */
6341 83630 : if (TREE_CODE (fn) == OFFSET_REF)
6342 : {
6343 83591 : tree object_addr = cp_build_addr_expr (object, complain);
6344 83591 : fn = TREE_OPERAND (fn, 1);
6345 83591 : fn = get_member_function_from_ptrfunc (&object_addr, fn,
6346 : complain);
6347 83591 : vec_safe_insert (*args, 0, object_addr);
6348 : }
6349 :
6350 83630 : if (CLASS_TYPE_P (TREE_TYPE (fn)))
6351 3 : expr = build_op_call (fn, args, complain);
6352 : else
6353 83627 : expr = cp_build_function_call_vec (fn, args, complain);
6354 83630 : if (processing_template_decl && expr != error_mark_node)
6355 39 : expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
6356 :
6357 83630 : if (orig_args != NULL)
6358 39 : release_tree_vector (orig_args);
6359 :
6360 : return expr;
6361 : }
6362 :
6363 :
6364 : void
6365 359694339 : check_default_args (tree x)
6366 : {
6367 359694339 : tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
6368 359694339 : bool saw_def = false;
6369 359694339 : bool noted_first_def = false;
6370 359694339 : int idx_of_first_default_arg = 0;
6371 359694339 : location_t loc_of_first_default_arg = UNKNOWN_LOCATION;
6372 359694339 : int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
6373 359694339 : tree fndecl = STRIP_TEMPLATE (x);
6374 359694339 : auto_diagnostic_group d;
6375 981462178 : for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
6376 : {
6377 621767839 : if (TREE_PURPOSE (arg))
6378 : {
6379 11463354 : if (!saw_def)
6380 : {
6381 8590978 : saw_def = true;
6382 8590978 : idx_of_first_default_arg = i;
6383 8590978 : location_t loc = get_fndecl_argument_location (fndecl, i);
6384 8590978 : if (loc != DECL_SOURCE_LOCATION (x))
6385 8590940 : loc_of_first_default_arg = loc;
6386 : }
6387 : }
6388 610304485 : else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
6389 : {
6390 147 : error_at (get_fndecl_argument_location (fndecl, i),
6391 : "default argument missing for parameter %P of %q#D", i, x);
6392 147 : if (loc_of_first_default_arg != UNKNOWN_LOCATION
6393 147 : && !noted_first_def)
6394 : {
6395 144 : inform (loc_of_first_default_arg,
6396 : "...following parameter %P which has a default argument",
6397 : idx_of_first_default_arg);
6398 144 : noted_first_def = true;
6399 : }
6400 147 : TREE_PURPOSE (arg) = error_mark_node;
6401 : }
6402 : }
6403 359694339 : }
6404 :
6405 : /* Return true if function DECL can be inlined. This is used to force
6406 : instantiation of methods that might be interesting for inlining. */
6407 : bool
6408 3 : possibly_inlined_p (tree decl)
6409 : {
6410 3 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6411 3 : if (DECL_UNINLINABLE (decl))
6412 : return false;
6413 3 : if (!optimize)
6414 3 : return DECL_DECLARED_INLINE_P (decl);
6415 : /* When optimizing, we might inline everything when flatten
6416 : attribute or heuristics inlining for size or autoinlining
6417 : is used. */
6418 : return true;
6419 : }
6420 :
6421 : /* If DECL is a function or variable template specialization, instantiate
6422 : its definition now. */
6423 :
6424 : void
6425 167498496 : maybe_instantiate_decl (tree decl)
6426 : {
6427 602841 : if (VAR_OR_FUNCTION_DECL_P (decl)
6428 167498496 : && DECL_LANG_SPECIFIC (decl)
6429 127444775 : && DECL_TEMPLATE_INFO (decl)
6430 269194109 : && !uses_template_parms (DECL_TI_ARGS (decl)))
6431 : {
6432 : /* Instantiating a function will result in garbage collection. We
6433 : must treat this situation as if we were within the body of a
6434 : function so as to avoid collecting live data only referenced from
6435 : the stack (such as overload resolution candidates). */
6436 97296603 : ++function_depth;
6437 97296603 : instantiate_decl (decl, /*defer_ok=*/false,
6438 : /*expl_inst_class_mem_p=*/false);
6439 97293906 : --function_depth;
6440 : }
6441 167495799 : }
6442 :
6443 : /* Error if the DECL is unavailable (unless this is currently suppressed).
6444 : Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns true if
6445 : an error or warning was emitted. */
6446 :
6447 : bool
6448 4005594931 : cp_handle_deprecated_or_unavailable (tree decl, tsubst_flags_t complain)
6449 : {
6450 4005594931 : if (!decl)
6451 : return false;
6452 :
6453 3967529645 : if ((complain & tf_error)
6454 3626516105 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6455 : {
6456 2920764241 : if (TREE_UNAVAILABLE (decl))
6457 : {
6458 252 : error_unavailable_use (decl, NULL_TREE);
6459 252 : return true;
6460 : }
6461 : else
6462 : {
6463 : /* Perhaps this is an unavailable typedef. */
6464 2920763989 : if (TYPE_P (decl)
6465 775988324 : && TYPE_NAME (decl)
6466 3690637812 : && TREE_UNAVAILABLE (TYPE_NAME (decl)))
6467 : {
6468 6 : decl = TYPE_NAME (decl);
6469 : /* Don't error within members of a unavailable type. */
6470 6 : if (TYPE_P (decl)
6471 6 : && currently_open_class (decl))
6472 : return false;
6473 :
6474 6 : error_unavailable_use (decl, NULL_TREE);
6475 6 : return true;
6476 : }
6477 : }
6478 : /* Carry on to consider deprecatedness. */
6479 : }
6480 :
6481 3967529387 : if (!(complain & tf_warning)
6482 3582964484 : || deprecated_state == DEPRECATED_SUPPRESS
6483 3580878427 : || deprecated_state == UNAVAILABLE_DEPRECATED_SUPPRESS)
6484 : return false;
6485 :
6486 2880552514 : if (!TREE_DEPRECATED (decl))
6487 : {
6488 : /* Perhaps this is a deprecated typedef. */
6489 2880034551 : if (TYPE_P (decl) && TYPE_NAME (decl))
6490 768635505 : decl = TYPE_NAME (decl);
6491 :
6492 2880034551 : if (!TREE_DEPRECATED (decl))
6493 : return false;
6494 : }
6495 :
6496 : /* Don't warn within members of a deprecated type. */
6497 517971 : if (TYPE_P (decl)
6498 517971 : && currently_open_class (decl))
6499 : return false;
6500 :
6501 42691 : bool warned = false;
6502 42691 : if (cxx_dialect >= cxx11
6503 42600 : && DECL_P (decl)
6504 42479 : && DECL_ARTIFICIAL (decl)
6505 19665 : && DECL_IOBJ_MEMBER_FUNCTION_P (decl)
6506 62356 : && copy_fn_p (decl))
6507 : {
6508 : /* Don't warn if the flag was disabled around the class definition
6509 : (c++/94492). */
6510 19653 : if (warning_enabled_at (DECL_SOURCE_LOCATION (decl),
6511 19653 : OPT_Wdeprecated_copy))
6512 : {
6513 20 : auto_diagnostic_group d;
6514 20 : tree ctx = DECL_CONTEXT (decl);
6515 20 : tree other = classtype_has_depr_implicit_copy (ctx);
6516 20 : int opt = (DECL_DESTRUCTOR_P (other)
6517 20 : ? OPT_Wdeprecated_copy_dtor
6518 0 : : OPT_Wdeprecated_copy);
6519 20 : warned = warning (opt, "implicitly-declared %qD is deprecated",
6520 : decl);
6521 20 : if (warned)
6522 4 : inform (DECL_SOURCE_LOCATION (other),
6523 : "because %qT has user-provided %qD",
6524 : ctx, other);
6525 20 : }
6526 : }
6527 : else
6528 : {
6529 23038 : if (!warning_suppressed_at (input_location,
6530 : OPT_Wdeprecated_declarations))
6531 20181 : warned = warn_deprecated_use (decl, NULL_TREE);
6532 23038 : suppress_warning_at (input_location, OPT_Wdeprecated_declarations);
6533 : }
6534 :
6535 : return warned;
6536 : }
6537 :
6538 : /* Like above, but takes into account outer scopes. */
6539 :
6540 : void
6541 1490734909 : cp_warn_deprecated_use_scopes (tree scope)
6542 : {
6543 1490734909 : while (scope
6544 2614561678 : && scope != error_mark_node
6545 5229123321 : && scope != global_namespace)
6546 : {
6547 249696621 : if ((TREE_CODE (scope) == NAMESPACE_DECL || OVERLOAD_TYPE_P (scope))
6548 1357121201 : && cp_handle_deprecated_or_unavailable (scope))
6549 : return;
6550 1123826769 : if (TYPE_P (scope))
6551 235007869 : scope = CP_TYPE_CONTEXT (scope);
6552 : else
6553 888818900 : scope = CP_DECL_CONTEXT (scope);
6554 : }
6555 : }
6556 :
6557 : /* True if DECL or its enclosing scope have unbound template parameters. */
6558 :
6559 : bool
6560 473089957 : decl_dependent_p (tree decl)
6561 : {
6562 473089957 : tree orig_decl = decl;
6563 945576746 : if (DECL_FUNCTION_SCOPE_P (decl)
6564 206372361 : || TREE_CODE (decl) == CONST_DECL
6565 206372361 : || TREE_CODE (decl) == USING_DECL
6566 679462318 : || TREE_CODE (decl) == FIELD_DECL)
6567 266717596 : decl = CP_DECL_CONTEXT (decl);
6568 473089957 : if (tree tinfo = get_template_info (decl))
6569 406122448 : if (any_dependent_template_arguments_p (TI_ARGS (tinfo)))
6570 : return true;
6571 304638146 : if (LAMBDA_FUNCTION_P (decl)
6572 303180900 : && dependent_type_p (DECL_CONTEXT (decl)))
6573 : return true;
6574 : /* for-range-declaration of expansion statement as well as variable
6575 : declarations in the expansion statement body when the expansion statement
6576 : is not inside a template still need to be treated as dependent during
6577 : parsing. When the body is instantiated, in_expansion_stmt will be already
6578 : false. */
6579 301930911 : if (VAR_P (orig_decl) && in_expansion_stmt && decl == current_function_decl)
6580 : return true;
6581 : return false;
6582 : }
6583 :
6584 : /* [basic.def.odr] A function is named [and therefore odr-used] by an
6585 : expression or conversion if it is the selected member of an overload set in
6586 : an overload resolution performed as part of forming that expression or
6587 : conversion, unless it is a pure virtual function and either the expression
6588 : is not an id-expression naming the function with an explicitly qualified
6589 : name or the expression forms a pointer to member.
6590 :
6591 : Mostly, we call mark_used in places that actually do something with a
6592 : function, like build_over_call. But in a few places we end up with a
6593 : non-overloaded FUNCTION_DECL that we aren't going to do any more with, like
6594 : convert_to_void. resolve_nondeduced_context is called in those places,
6595 : but it's also called in too many other places. */
6596 :
6597 : bool
6598 439629297 : mark_single_function (tree expr, tsubst_flags_t complain)
6599 : {
6600 439629297 : expr = maybe_undo_parenthesized_ref (expr);
6601 439629297 : expr = tree_strip_any_location_wrapper (expr);
6602 :
6603 439629297 : if (expr == error_mark_node)
6604 : return false;
6605 :
6606 439628608 : if (is_overloaded_fn (expr) == 1
6607 115560042 : && !mark_used (expr, complain)
6608 439628644 : && !(complain & tf_error))
6609 : return false;
6610 : return true;
6611 : }
6612 :
6613 : /* True iff we have started, but not finished, defining FUNCTION_DECL DECL. */
6614 :
6615 : bool
6616 87737889 : fn_being_defined (tree decl)
6617 : {
6618 : /* DECL_INITIAL is set to error_mark_node in grokfndecl for a definition, and
6619 : changed to BLOCK by poplevel at the end of the function. */
6620 87737889 : return (TREE_CODE (decl) == FUNCTION_DECL
6621 87737889 : && DECL_INITIAL (decl) == error_mark_node);
6622 : }
6623 :
6624 : /* True if DECL is an instantiation of a function template currently being
6625 : defined. */
6626 :
6627 : bool
6628 301930455 : fn_template_being_defined (tree decl)
6629 : {
6630 301930455 : if (TREE_CODE (decl) != FUNCTION_DECL
6631 140530787 : || !DECL_LANG_SPECIFIC (decl)
6632 140530787 : || !DECL_TEMPLOID_INSTANTIATION (decl)
6633 416893671 : || DECL_TEMPLATE_INSTANTIATED (decl))
6634 : return false;
6635 87737889 : tree tinfo = DECL_TEMPLATE_INFO (decl);
6636 87737889 : tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
6637 87737889 : return fn_being_defined (pattern);
6638 : }
6639 :
6640 : /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
6641 : If DECL is a specialization or implicitly declared class member,
6642 : generate the actual definition. Return false if something goes
6643 : wrong, true otherwise. */
6644 :
6645 : bool
6646 1326992892 : mark_used (tree decl, tsubst_flags_t complain /* = tf_warning_or_error */)
6647 : {
6648 1326992892 : if (decl == error_mark_node)
6649 : return false;
6650 :
6651 : /* If we're just testing conversions or resolving overloads, we
6652 : don't want any permanent effects like forcing functions to be
6653 : output or instantiating templates. */
6654 1326992802 : if ((complain & tf_conv))
6655 : return true;
6656 :
6657 : /* If DECL is a BASELINK for a single function, then treat it just
6658 : like the DECL for the function. Otherwise, if the BASELINK is
6659 : for an overloaded function, we don't know which function was
6660 : actually used until after overload resolution. */
6661 1326972050 : if (BASELINK_P (decl))
6662 : {
6663 11066 : tree fns = BASELINK_FUNCTIONS (decl);
6664 11066 : if (really_overloaded_fn (fns))
6665 : return true;
6666 11023 : fns = OVL_FIRST (fns);
6667 11023 : if (!mark_used (fns, complain))
6668 : return false;
6669 : /* We might have deduced its return type. */
6670 11011 : TREE_TYPE (decl) = TREE_TYPE (fns);
6671 11011 : return true;
6672 : }
6673 :
6674 1326960984 : if (!DECL_P (decl))
6675 : return true;
6676 :
6677 : /* Set TREE_USED for the benefit of -Wunused. */
6678 1326054126 : TREE_USED (decl) = true;
6679 :
6680 : /* And for structured bindings also the underlying decl. */
6681 1326054126 : if (DECL_DECOMPOSITION_P (decl) && !DECL_DECOMP_IS_BASE (decl))
6682 857505 : TREE_USED (DECL_DECOMP_BASE (decl)) = true;
6683 :
6684 1326054126 : if (TREE_CODE (decl) == TEMPLATE_DECL)
6685 : return true;
6686 :
6687 1326042754 : if (DECL_CLONED_FUNCTION_P (decl))
6688 20423025 : TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
6689 :
6690 : /* Mark enumeration types as used. */
6691 1326042754 : if (TREE_CODE (decl) == CONST_DECL)
6692 32078808 : used_types_insert (DECL_CONTEXT (decl));
6693 :
6694 1326042754 : if (TREE_CODE (decl) == FUNCTION_DECL)
6695 : {
6696 293832907 : if (DECL_MAYBE_DELETED (decl))
6697 : {
6698 33245 : ++function_depth;
6699 33245 : maybe_synthesize_method (decl);
6700 33245 : --function_depth;
6701 : }
6702 :
6703 293832907 : if (DECL_DELETED_FN (decl))
6704 : {
6705 2403 : if (DECL_ARTIFICIAL (decl)
6706 1631 : && DECL_CONV_FN_P (decl)
6707 2415 : && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
6708 : /* We mark a lambda conversion op as deleted if we can't
6709 : generate it properly; see maybe_add_lambda_conv_op. */
6710 6 : sorry ("converting lambda that uses %<...%> to function pointer");
6711 2397 : else if (complain & tf_error)
6712 : {
6713 2379 : auto_diagnostic_group d;
6714 2379 : if (DECL_INITIAL (decl)
6715 2379 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6716 : {
6717 23 : escaped_string msg;
6718 23 : msg.escape (TREE_STRING_POINTER (DECL_INITIAL (decl)));
6719 23 : error ("use of deleted function %qD: %s",
6720 : decl, (const char *) msg);
6721 23 : }
6722 : else
6723 2356 : error ("use of deleted function %qD", decl);
6724 2379 : if (!maybe_explain_implicit_delete (decl))
6725 661 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
6726 2379 : }
6727 2403 : return false;
6728 : }
6729 :
6730 293830504 : if (!maybe_instantiate_noexcept (decl, complain))
6731 : return false;
6732 : }
6733 :
6734 1326040255 : if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl))
6735 : {
6736 70223 : if (!DECL_LANG_SPECIFIC (decl))
6737 : /* An unresolved dependent local extern. */
6738 : return true;
6739 :
6740 70223 : DECL_ODR_USED (decl) = 1;
6741 70223 : auto alias = DECL_LOCAL_DECL_ALIAS (decl);
6742 70223 : if (!alias || alias == error_mark_node)
6743 : return true;
6744 :
6745 : /* Process the underlying decl. */
6746 69931 : decl = alias;
6747 69931 : TREE_USED (decl) = true;
6748 : }
6749 :
6750 1326039963 : cp_handle_deprecated_or_unavailable (decl, complain);
6751 :
6752 : /* We can only check DECL_ODR_USED on variables or functions with
6753 : DECL_LANG_SPECIFIC set, and these are also the only decls that we
6754 : might need special handling for. */
6755 760316759 : if (!VAR_OR_FUNCTION_DECL_P (decl)
6756 1619870349 : || DECL_THUNK_P (decl))
6757 : return true;
6758 :
6759 : /* We only want to do this processing once. We don't need to keep trying
6760 : to instantiate inline templates, because unit-at-a-time will make sure
6761 : we get them compiled before functions that want to inline them. */
6762 859545198 : if (DECL_LANG_SPECIFIC (decl) && DECL_ODR_USED (decl))
6763 : return true;
6764 :
6765 467232366 : if (flag_concepts && TREE_CODE (decl) == FUNCTION_DECL
6766 613052974 : && !constraints_satisfied_p (decl))
6767 : {
6768 15 : if (complain & tf_error)
6769 : {
6770 6 : auto_diagnostic_group d;
6771 6 : error ("use of function %qD with unsatisfied constraints",
6772 : decl);
6773 6 : location_t loc = DECL_SOURCE_LOCATION (decl);
6774 6 : inform (loc, "declared here");
6775 6 : diagnose_constraints (loc, decl, NULL_TREE);
6776 6 : }
6777 15 : return false;
6778 : }
6779 :
6780 : /* If DECL has a deduced return type, we need to instantiate it now to
6781 : find out its type. For OpenMP user defined reductions, we need them
6782 : instantiated for reduction clauses which inline them by hand directly.
6783 : OpenMP declared mappers are used implicitly so must be instantiated
6784 : before they can be detected. */
6785 473089957 : if (undeduced_auto_decl (decl)
6786 446061709 : || (VAR_P (decl)
6787 305344253 : && VAR_HAD_UNKNOWN_BOUND (decl))
6788 446042394 : || (TREE_CODE (decl) == FUNCTION_DECL
6789 140717456 : && DECL_OMP_DECLARE_REDUCTION_P (decl))
6790 919132089 : || (TREE_CODE (decl) == VAR_DECL
6791 305324938 : && DECL_LANG_SPECIFIC (decl)
6792 72711464 : && DECL_OMP_DECLARE_MAPPER_P (decl)))
6793 27047906 : maybe_instantiate_decl (decl);
6794 :
6795 473089957 : if (!decl_dependent_p (decl)
6796 : /* Don't require this yet for an instantiation of a function template
6797 : we're currently defining (c++/120555). */
6798 301930455 : && !fn_template_being_defined (decl)
6799 774900092 : && !require_deduced_type (decl, complain))
6800 : return false;
6801 :
6802 473089302 : if (DECL_LANG_SPECIFIC (decl) == NULL)
6803 : return true;
6804 :
6805 214740095 : if (processing_template_decl || in_template_context)
6806 : return true;
6807 :
6808 : /* Check this too in case we're within instantiate_non_dependent_expr. */
6809 160841049 : if (DECL_TEMPLATE_INFO (decl)
6810 160841049 : && uses_template_parms (DECL_TI_ARGS (decl)))
6811 : return true;
6812 :
6813 160841049 : if (builtin_pack_fn_p (decl))
6814 : {
6815 12 : error ("use of built-in parameter pack %qD outside of a template",
6816 6 : DECL_NAME (decl));
6817 6 : return false;
6818 : }
6819 :
6820 : /* If we don't need a value, then we don't need to synthesize DECL. */
6821 160841043 : if (cp_unevaluated_operand || in_discarded_stmt)
6822 : return true;
6823 :
6824 47299581 : DECL_ODR_USED (decl) = 1;
6825 47299581 : if (DECL_CLONED_FUNCTION_P (decl))
6826 7136215 : DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
6827 :
6828 : /* DR 757: A type without linkage shall not be used as the type of a
6829 : variable or function with linkage, unless
6830 : o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
6831 : o the variable or function is not used (3.2 [basic.def.odr]) or is
6832 : defined in the same translation unit. */
6833 47299581 : if (cxx_dialect > cxx98
6834 47085065 : && decl_linkage (decl) != lk_none
6835 44490050 : && !DECL_EXTERN_C_P (decl)
6836 40003776 : && !DECL_ARTIFICIAL (decl)
6837 38274667 : && !decl_defined_p (decl)
6838 75891977 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
6839 475145 : vec_safe_push (no_linkage_decls, decl);
6840 :
6841 47299581 : if (TREE_CODE (decl) == FUNCTION_DECL
6842 37028496 : && DECL_DECLARED_INLINE_P (decl)
6843 28440400 : && !DECL_INITIAL (decl)
6844 19089480 : && !DECL_ARTIFICIAL (decl)
6845 65820971 : && !DECL_PURE_VIRTUAL_P (decl))
6846 : /* Remember it, so we can check it was defined. */
6847 18519537 : note_vague_linkage_fn (decl);
6848 :
6849 : /* Is it a synthesized method that needs to be synthesized? */
6850 47299581 : if (TREE_CODE (decl) == FUNCTION_DECL
6851 37028496 : && DECL_DEFAULTED_FN (decl)
6852 : /* A function defaulted outside the class is synthesized either by
6853 : cp_finish_decl or instantiate_decl. */
6854 1349195 : && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
6855 48648543 : && ! DECL_INITIAL (decl))
6856 : {
6857 : /* Remember the current location for a function we will end up
6858 : synthesizing. Then we can inform the user where it was
6859 : required in the case of error. */
6860 1153640 : if (decl_remember_implicit_trigger_p (decl))
6861 515164 : DECL_SOURCE_LOCATION (decl) = input_location;
6862 :
6863 : /* Synthesizing an implicitly defined member function will result in
6864 : garbage collection. We must treat this situation as if we were
6865 : within the body of a function so as to avoid collecting live data
6866 : on the stack (such as overload resolution candidates).
6867 :
6868 : We could just let c_parse_final_cleanups handle synthesizing
6869 : this function by adding it to deferred_fns, but doing
6870 : it at the use site produces better error messages. */
6871 1153640 : ++function_depth;
6872 1153640 : synthesize_method (decl);
6873 1153640 : --function_depth;
6874 : /* If this is a synthesized method we don't need to
6875 : do the instantiation test below. */
6876 : }
6877 35874856 : else if (VAR_OR_FUNCTION_DECL_P (decl)
6878 46145941 : && DECL_TEMPLATE_INFO (decl)
6879 74089372 : && (!DECL_EXPLICIT_INSTANTIATION (decl)
6880 1133770 : || always_instantiate_p (decl)))
6881 : /* If this is a function or variable that is an instance of some
6882 : template, we now know that we will need to actually do the
6883 : instantiation. We check that DECL is not an explicit
6884 : instantiation because that is not checked in instantiate_decl.
6885 :
6886 : We put off instantiating functions in order to improve compile
6887 : times. Maintaining a stack of active functions is expensive,
6888 : and the inliner knows to instantiate any functions it might
6889 : need. Therefore, we always try to defer instantiation. */
6890 : {
6891 27890434 : ++function_depth;
6892 27890434 : instantiate_decl (decl, /*defer_ok=*/true,
6893 : /*expl_inst_class_mem_p=*/false);
6894 27882334 : --function_depth;
6895 : }
6896 :
6897 : return true;
6898 : }
6899 :
6900 : tree
6901 9 : vtv_start_verification_constructor_init_function (void)
6902 : {
6903 9 : return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, true);
6904 : }
6905 :
6906 : tree
6907 6 : vtv_finish_verification_constructor_init_function (tree body)
6908 : {
6909 6 : return finish_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, body);
6910 : }
6911 :
6912 : #include "gt-cp-decl2.h"
|