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