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