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