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