Branch data Line data Source code
1 : : /* Process declarations and variables for C++ compiler.
2 : : Copyright (C) 1988-2024 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);
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 : 374475320 : static hashval_t hash (const value_type decl)
103 : : {
104 : 374475320 : return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
105 : : }
106 : 370367156 : static bool equal (const value_type existing, compare_type candidate)
107 : : {
108 : 370367156 : tree name = DECL_ASSEMBLER_NAME_RAW (existing);
109 : 370367156 : 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 : 486973523 : static bool is_deleted (value_type e)
117 : : {
118 : 486973523 : 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 : 462717 : template <typename T> static bool is_empty (const T &entry)
150 : : {
151 : 462717 : 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 : 86311659 : build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
194 : : cp_ref_qualifier rqual)
195 : : {
196 : 86311659 : if (fntype == error_mark_node || ctype == error_mark_node)
197 : : return error_mark_node;
198 : :
199 : 86311656 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
200 : :
201 : 86311656 : cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
202 : 86311656 : ctype = cp_build_qualified_type (ctype, type_quals);
203 : :
204 : 86311656 : tree newtype
205 : 86311656 : = build_method_type_directly (ctype, TREE_TYPE (fntype),
206 : 86311656 : (TREE_CODE (fntype) == METHOD_TYPE
207 : 60193 : ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
208 : 86251463 : : TYPE_ARG_TYPES (fntype)));
209 : 86311656 : if (tree attrs = TYPE_ATTRIBUTES (fntype))
210 : 74 : newtype = cp_build_type_attribute_variant (newtype, attrs);
211 : 258934968 : newtype = build_cp_fntype_variant (newtype, rqual,
212 : 86311656 : TYPE_RAISES_EXCEPTIONS (fntype),
213 : 86311656 : TYPE_HAS_LATE_RETURN_TYPE (fntype));
214 : :
215 : 86311656 : 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 : 587264 : change_return_type (tree new_ret, tree fntype)
223 : : {
224 : 587264 : if (new_ret == error_mark_node)
225 : : return fntype;
226 : :
227 : 587264 : if (same_type_p (new_ret, TREE_TYPE (fntype)))
228 : : return fntype;
229 : :
230 : 587261 : tree newtype;
231 : 587261 : tree args = TYPE_ARG_TYPES (fntype);
232 : :
233 : 587261 : if (TREE_CODE (fntype) == FUNCTION_TYPE)
234 : : {
235 : 109159 : newtype = build_function_type (new_ret, args);
236 : 109159 : newtype = apply_memfn_quals (newtype,
237 : : type_memfn_quals (fntype));
238 : : }
239 : : else
240 : 478102 : newtype = build_method_type_directly
241 : 478102 : (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
242 : :
243 : 587261 : if (tree attrs = TYPE_ATTRIBUTES (fntype))
244 : 124 : newtype = cp_build_type_attribute_variant (newtype, attrs);
245 : 587261 : newtype = cxx_copy_lang_qualifiers (newtype, fntype);
246 : :
247 : 587261 : 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 : 472104183 : cp_build_parm_decl (tree fn, tree name, tree type)
255 : : {
256 : 472104183 : tree parm = build_decl (input_location,
257 : : PARM_DECL, name, type);
258 : 472104183 : 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 : 472104183 : if (!processing_template_decl)
263 : 161248745 : DECL_ARG_TYPE (parm) = type_passed_as (type);
264 : :
265 : 472104183 : 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 : 195101924 : build_artificial_parm (tree fn, tree name, tree type)
273 : : {
274 : 195101924 : tree parm = cp_build_parm_decl (fn, name, type);
275 : 195101924 : DECL_ARTIFICIAL (parm) = 1;
276 : : /* All our artificial parms are implicitly `const'; they cannot be
277 : : assigned to. */
278 : 195101924 : TREE_READONLY (parm) = 1;
279 : 195101924 : 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 : 63542028 : maybe_retrofit_in_chrg (tree fn)
294 : : {
295 : 63542028 : tree basetype, arg_types, parms, parm, fntype;
296 : :
297 : : /* If we've already add the in-charge parameter don't do it again. */
298 : 63542028 : 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 : 63542028 : 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 : 38784765 : if (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
309 : : return;
310 : :
311 : 1074114 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
312 : 1074114 : basetype = TREE_TYPE (TREE_VALUE (arg_types));
313 : 1074114 : arg_types = TREE_CHAIN (arg_types);
314 : :
315 : 1074114 : 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 : 1074114 : if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
320 : : {
321 : 1074114 : 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 : 1074114 : DECL_CHAIN (parm) = parms;
325 : 1074114 : parms = parm;
326 : :
327 : : /* ...and then to TYPE_ARG_TYPES. */
328 : 1074114 : arg_types = hash_tree_chain (vtt_parm_type, arg_types);
329 : :
330 : 1074114 : DECL_HAS_VTT_PARM_P (fn) = 1;
331 : : }
332 : :
333 : : /* Then add the in-charge parm (before the VTT parm). */
334 : 1074114 : parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
335 : 1074114 : DECL_CHAIN (parm) = parms;
336 : 1074114 : parms = parm;
337 : 1074114 : arg_types = hash_tree_chain (integer_type_node, arg_types);
338 : :
339 : : /* Insert our new parameter(s) into the list. */
340 : 1074114 : DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
341 : :
342 : : /* And rebuild the function type. */
343 : 1074114 : fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
344 : : arg_types);
345 : 1074114 : if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
346 : 3 : fntype = (cp_build_type_attribute_variant
347 : 3 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
348 : 1074114 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
349 : 1074114 : TREE_TYPE (fn) = fntype;
350 : :
351 : : /* Now we've got the in-charge parameter. */
352 : 1074114 : 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 : 114681519 : grokclassfn (tree ctype, tree function, enum overload_flags flags)
377 : : {
378 : 114681519 : 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 : 114681519 : SET_DECL_LANGUAGE (function, lang_cplusplus);
383 : :
384 : 114681519 : 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 : 114681519 : DECL_CONTEXT (function) = ctype;
392 : :
393 : 114681519 : if (flags == DTOR_FLAG)
394 : 8331617 : DECL_CXX_DESTRUCTOR_P (function) = 1;
395 : :
396 : 221031421 : if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
397 : 41514355 : maybe_retrofit_in_chrg (function);
398 : 114681519 : }
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 : 10012158 : 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 : 10012158 : tree type;
410 : 10012158 : tree expr;
411 : 10012158 : tree orig_array_expr = array_expr;
412 : 10012158 : tree orig_index_exp = index_exp;
413 : 20024316 : vec<tree, va_gc> *orig_index_exp_list
414 : 10012158 : = index_exp_list ? *index_exp_list : NULL;
415 : 10012158 : tree overload = NULL_TREE;
416 : :
417 : 10012158 : if (error_operand_p (array_expr) || error_operand_p (index_exp))
418 : 79 : return error_mark_node;
419 : :
420 : 10012079 : if (processing_template_decl)
421 : : {
422 : 8551913 : if (type_dependent_expression_p (array_expr)
423 : 8551939 : || (index_exp ? type_dependent_expression_p (index_exp)
424 : 26 : : any_type_dependent_arguments_p (*index_exp_list)))
425 : : {
426 : 6787741 : if (index_exp == NULL)
427 : 79 : index_exp = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
428 : : *index_exp_list);
429 : 6787741 : return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
430 : 6787741 : NULL_TREE, NULL_TREE);
431 : : }
432 : 1764172 : if (!index_exp)
433 : 25 : orig_index_exp_list = make_tree_vector_copy (*index_exp_list);
434 : : }
435 : :
436 : 3224338 : type = TREE_TYPE (array_expr);
437 : 3224338 : gcc_assert (type);
438 : 3224338 : type = non_reference (type);
439 : :
440 : : /* If they have an `operator[]', use that. */
441 : 3224338 : if (MAYBE_CLASS_TYPE_P (type)
442 : 2984241 : || (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 : 240139 : if (index_exp)
448 : 239993 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
449 : : index_exp, NULL_TREE, NULL_TREE,
450 : : &overload, complain);
451 : 146 : 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 : 57 : expr = build_op_subscript (loc, array_expr, index_exp_list,
457 : : &overload, complain & tf_decltype);
458 : 57 : 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 : 2984199 : tree p1, p2, i1, i2;
499 : 2984199 : 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 : 2984199 : if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
506 : : p1 = array_expr;
507 : : else
508 : 1903352 : p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
509 : :
510 : 2984199 : 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 : 2984190 : if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
550 : : p2 = index_exp;
551 : : else
552 : 2984157 : p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
553 : :
554 : 2984190 : i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
555 : : false);
556 : 2984190 : i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
557 : : false);
558 : :
559 : 2984190 : if ((p1 && i2) && (i1 && p2))
560 : 0 : error ("ambiguous conversion for array subscript");
561 : :
562 : 2984190 : 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 : 2984161 : if (array_expr == error_mark_node || index_exp == error_mark_node)
575 : 0 : error ("ambiguous conversion for array subscript");
576 : :
577 : 2984161 : if (TYPE_PTR_P (TREE_TYPE (array_expr)))
578 : 1903307 : array_expr = mark_rvalue_use (array_expr);
579 : : else
580 : 1080854 : array_expr = mark_lvalue_use_nonread (array_expr);
581 : 2984161 : index_exp = mark_rvalue_use (index_exp);
582 : 2984161 : if (swapped
583 : 48 : && flag_strong_eval_order == 2
584 : 2984202 : && (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 : 2984146 : expr = build_array_ref (input_location, array_expr, index_exp);
588 : : }
589 : 3224300 : if (processing_template_decl && expr != error_mark_node)
590 : : {
591 : 1764172 : if (overload != NULL_TREE)
592 : : {
593 : 221183 : 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 : 221161 : return build_min_non_dep_op_overload (ARRAY_REF, expr, overload,
602 : : orig_array_expr,
603 : 221161 : orig_index_exp);
604 : : }
605 : :
606 : 1542989 : 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 : 1542989 : return build_min_non_dep (ARRAY_REF, expr, orig_array_expr,
615 : 1542989 : 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 : 4160 : grok_omp_array_section (location_t loc, tree array_expr, tree index,
625 : : tree length)
626 : : {
627 : 4160 : tree orig_array_expr = array_expr;
628 : 4160 : tree orig_index = index;
629 : 4160 : tree orig_length = length;
630 : :
631 : 4160 : if (error_operand_p (array_expr)
632 : 4154 : || error_operand_p (index)
633 : 8314 : || error_operand_p (length))
634 : 6 : return error_mark_node;
635 : :
636 : 4154 : if (processing_template_decl
637 : 4154 : && (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 : 3336 : index = fold_non_dependent_expr (index);
643 : 3336 : 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 : 3336 : tree expr = build_omp_array_section (loc, array_expr, index, length);
650 : :
651 : 3336 : 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 : 560485 : delete_sanity (location_t loc, tree exp, tree size, bool doing_vec,
666 : : int use_global_delete, tsubst_flags_t complain)
667 : : {
668 : 560485 : tree t, type;
669 : :
670 : 560485 : if (exp == error_mark_node)
671 : : return exp;
672 : :
673 : 560368 : if (processing_template_decl)
674 : : {
675 : 462212 : t = build_min (DELETE_EXPR, void_type_node, exp, size);
676 : 462212 : DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
677 : 462212 : DELETE_EXPR_USE_VEC (t) = doing_vec;
678 : 462212 : TREE_SIDE_EFFECTS (t) = 1;
679 : 462212 : SET_EXPR_LOCATION (t, loc);
680 : 462212 : return t;
681 : : }
682 : :
683 : 98156 : 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 : 98156 : if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
687 : 98156 : && (complain & tf_warning))
688 : 21 : warning_at (exp_loc, 0, "deleting array %q#E", exp);
689 : :
690 : 98156 : t = build_expr_type_conversion (WANT_POINTER, exp, true);
691 : :
692 : 98156 : 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 : 98135 : 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 : 98135 : 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 : 98129 : 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 : 98129 : if (integer_zerop (t))
726 : 3 : return build1_loc (loc, NOP_EXPR, void_type_node, t);
727 : :
728 : 98126 : if (doing_vec)
729 : 3150 : return build_vec_delete (loc, t, /*maxindex=*/NULL_TREE,
730 : : sfk_deleting_destructor,
731 : 3150 : use_global_delete, complain);
732 : : else
733 : 94976 : return build_delete (loc, type, t, sfk_deleting_destructor,
734 : : LOOKUP_NORMAL, use_global_delete,
735 : 94976 : 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 : 18173379 : check_member_template (tree tmpl)
743 : : {
744 : 18173379 : tree decl;
745 : :
746 : 18173379 : gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
747 : 18173379 : decl = DECL_TEMPLATE_RESULT (tmpl);
748 : :
749 : 18173379 : if (TREE_CODE (decl) == FUNCTION_DECL
750 : 2203380 : || DECL_ALIAS_TEMPLATE_P (tmpl)
751 : 19024401 : || (TREE_CODE (decl) == TYPE_DECL
752 : 659227 : && 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 : 18248840 : gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
757 : : /* The parser rejects any use of virtual in a function template. */
758 : 17981584 : 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 : 17981584 : DECL_IGNORED_P (tmpl) = 1;
764 : : }
765 : 191795 : else if (variable_template_p (tmpl))
766 : : /* OK */;
767 : : else
768 : 0 : error ("template declaration of %q#D", decl);
769 : 18173379 : }
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 : 9227760 : check_classfn (tree ctype, tree function, tree template_parms)
783 : : {
784 : 9227760 : if (DECL_USE_TEMPLATE (function)
785 : 1220871 : && !(TREE_CODE (function) == TEMPLATE_DECL
786 : 1197 : && DECL_TEMPLATE_SPECIALIZATION (function))
787 : 10447434 : && 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 : 8824403 : if (TREE_CODE (function) == TEMPLATE_DECL)
803 : : {
804 : 1212 : if (template_parms
805 : 2409 : && !comp_template_parms (template_parms,
806 : 1197 : 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 : 1212 : template_parms = DECL_TEMPLATE_PARMS (function);
813 : : }
814 : :
815 : : /* OK, is this a definition of a member template? */
816 : 8824403 : bool is_template = (template_parms != NULL_TREE);
817 : :
818 : : /* [temp.mem]
819 : :
820 : : A destructor shall not be a member template. */
821 : 17648806 : 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 : 8824397 : tree pushed_scope = push_scope (ctype);
831 : 8824397 : tree matched = NULL_TREE;
832 : 8824397 : tree fns = get_class_binding (ctype, DECL_NAME (function));
833 : 8824397 : bool saw_template = false;
834 : :
835 : 35956393 : for (ovl_iterator iter (fns); !matched && iter; ++iter)
836 : : {
837 : 16064653 : tree fndecl = *iter;
838 : :
839 : 16064653 : if (TREE_CODE (fndecl) == TEMPLATE_DECL)
840 : 3980203 : saw_template = true;
841 : :
842 : : /* A member template definition only matches a member template
843 : : declaration. */
844 : 16064653 : if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
845 : 1715747 : continue;
846 : :
847 : 14348906 : if (!DECL_DECLARES_FUNCTION_P (fndecl))
848 : 303884 : continue;
849 : :
850 : 14045022 : tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
851 : 14045022 : 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 : 14045022 : if (DECL_STATIC_FUNCTION_P (fndecl)
861 : 14045022 : && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
862 : 147288 : p1 = TREE_CHAIN (p1);
863 : :
864 : : /* ref-qualifier or absence of same must match. */
865 : 28090044 : if (type_memfn_rqual (TREE_TYPE (function))
866 : 14045022 : != type_memfn_rqual (TREE_TYPE (fndecl)))
867 : 86 : continue;
868 : :
869 : : // Include constraints in the match.
870 : 14044936 : tree c1 = get_constraints (function);
871 : 14044936 : 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 : 14044936 : if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
877 : : TREE_TYPE (TREE_TYPE (fndecl)))
878 : 13243169 : && compparms (p1, p2)
879 : 8824355 : && !targetm.target_option.function_versions (function, fndecl)
880 : 8824355 : && (!is_template
881 : 2065412 : || comp_template_parms (template_parms,
882 : 2065412 : DECL_TEMPLATE_PARMS (fndecl)))
883 : 8824349 : && equivalent_constraints (c1, c2)
884 : 8824298 : && (DECL_TEMPLATE_SPECIALIZATION (function)
885 : 8824298 : == DECL_TEMPLATE_SPECIALIZATION (fndecl))
886 : 22869234 : && (!DECL_TEMPLATE_SPECIALIZATION (function)
887 : 812409 : || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
888 : : matched = fndecl;
889 : : }
890 : :
891 : 84 : if (!matched && !is_template && saw_template
892 : 8824412 : && !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 : 8824397 : if (!matched)
905 : : {
906 : 96 : if (!COMPLETE_TYPE_P (ctype))
907 : 6 : cxx_incomplete_type_error (DECL_SOURCE_LOCATION (function),
908 : : function, ctype);
909 : : else
910 : : {
911 : 90 : if (DECL_CONV_FN_P (function))
912 : 6 : fns = get_class_binding (ctype, conv_op_identifier);
913 : :
914 : 90 : auto_diagnostic_group d;
915 : 90 : error_at (DECL_SOURCE_LOCATION (function),
916 : : "no declaration matches %q#D", function);
917 : 90 : if (fns)
918 : 49 : print_candidates (fns);
919 : 41 : else if (DECL_CONV_FN_P (function))
920 : 3 : inform (DECL_SOURCE_LOCATION (function),
921 : : "no conversion operators declared");
922 : : else
923 : 38 : inform (DECL_SOURCE_LOCATION (function),
924 : : "no functions named %qD", function);
925 : 90 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
926 : : "%#qT defined here", ctype);
927 : 90 : }
928 : 96 : matched = error_mark_node;
929 : : }
930 : :
931 : 8824397 : if (pushed_scope)
932 : 8462447 : 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 : 35916794 : note_vague_linkage_fn (tree decl)
943 : : {
944 : 35916794 : if (processing_template_decl)
945 : : return;
946 : :
947 : 35916785 : DECL_DEFER_OUTPUT (decl) = 1;
948 : 35916785 : vec_safe_push (deferred_fns, decl);
949 : : }
950 : :
951 : : /* As above, but for variables. */
952 : :
953 : : void
954 : 3268220 : note_vague_linkage_variable (tree decl)
955 : : {
956 : 3268220 : vec_safe_push (pending_statics, decl);
957 : 3268220 : }
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 : 12076561 : finish_static_data_member_decl (tree decl,
964 : : tree init, bool init_const_expr_p,
965 : : tree asmspec_tree,
966 : : int flags)
967 : : {
968 : 12076561 : 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 : 12076558 : 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 : 12076558 : if (! processing_template_decl)
980 : 9803728 : vec_safe_push (pending_statics, decl);
981 : :
982 : 12076558 : if (LOCAL_CLASS_P (current_class_type)
983 : : /* We already complained about the template definition. */
984 : 12076558 : && !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 : 24486882 : for (tree t = current_class_type; TYPE_P (t);
990 : 12410339 : t = CP_TYPE_CONTEXT (t))
991 : 24820732 : 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 : 12076558 : 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 : 4110154 : DECL_IN_AGGR_P (decl) = 1;
1007 : :
1008 : 12076558 : if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
1009 : 12076558 : && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
1010 : 91498 : SET_VAR_HAD_UNKNOWN_BOUND (decl);
1011 : :
1012 : 12076558 : 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 : 9090807 : tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
1018 : 9090807 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
1019 : : }
1020 : :
1021 : 12076558 : cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
1022 : : }
1023 : :
1024 : : /* DECLARATOR and DECLSPECS correspond to a class member. The other
1025 : : parameters are as for cp_finish_decl. Return the DECL for the
1026 : : class member declared. */
1027 : :
1028 : : tree
1029 : 71095923 : grokfield (const cp_declarator *declarator,
1030 : : cp_decl_specifier_seq *declspecs,
1031 : : tree init, bool init_const_expr_p,
1032 : : tree asmspec_tree,
1033 : : tree attrlist)
1034 : : {
1035 : 71095923 : tree value;
1036 : 71095923 : const char *asmspec = 0;
1037 : 71095923 : int flags;
1038 : :
1039 : 71095923 : if (init
1040 : 18624030 : && TREE_CODE (init) == TREE_LIST
1041 : 0 : && TREE_VALUE (init) == error_mark_node
1042 : 71095923 : && TREE_CHAIN (init) == NULL_TREE)
1043 : : init = NULL_TREE;
1044 : :
1045 : 71095923 : int initialized;
1046 : 71095923 : if (init == ridpointers[(int)RID_DELETE]
1047 : 71095923 : || (init
1048 : 15677560 : && TREE_CODE (init) == STRING_CST
1049 : 9 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
1050 : : initialized = SD_DELETED;
1051 : 68149444 : else if (init == ridpointers[(int)RID_DEFAULT])
1052 : : initialized = SD_DEFAULTED;
1053 : 62805121 : else if (init)
1054 : : initialized = SD_INITIALIZED;
1055 : : else
1056 : 52471893 : initialized = SD_UNINITIALIZED;
1057 : :
1058 : 71095923 : value = grokdeclarator (declarator, declspecs, FIELD, initialized, &attrlist);
1059 : 71095917 : if (! value || value == error_mark_node)
1060 : : /* friend or constructor went bad. */
1061 : 347 : return error_mark_node;
1062 : 71095570 : if (TREE_TYPE (value) == error_mark_node)
1063 : : return value;
1064 : :
1065 : 71095431 : if (TREE_CODE (value) == TYPE_DECL && init)
1066 : : {
1067 : 6 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)),
1068 : : "typedef %qD is initialized (use %qs instead)",
1069 : : value, "decltype");
1070 : 6 : init = NULL_TREE;
1071 : : }
1072 : :
1073 : : /* Pass friendly classes back. */
1074 : 71095431 : if (value == void_type_node)
1075 : : return value;
1076 : :
1077 : 71095431 : if (DECL_NAME (value)
1078 : 71095431 : && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR)
1079 : : {
1080 : 3 : error_at (declarator->id_loc,
1081 : : "explicit template argument list not allowed");
1082 : 3 : return error_mark_node;
1083 : : }
1084 : :
1085 : : /* Stash away type declarations. */
1086 : 71095428 : if (TREE_CODE (value) == TYPE_DECL)
1087 : : {
1088 : 22290280 : DECL_NONLOCAL (value) = 1;
1089 : 22290280 : DECL_CONTEXT (value) = current_class_type;
1090 : :
1091 : 22290280 : if (attrlist)
1092 : : {
1093 : 88030 : int attrflags = 0;
1094 : :
1095 : : /* If this is a typedef that names the class for linkage purposes
1096 : : (7.1.3p8), apply any attributes directly to the type. */
1097 : 176060 : if (OVERLOAD_TYPE_P (TREE_TYPE (value))
1098 : 122056 : && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
1099 : : attrflags = ATTR_FLAG_TYPE_IN_PLACE;
1100 : :
1101 : 88030 : cplus_decl_attributes (&value, attrlist, attrflags);
1102 : : }
1103 : :
1104 : 22290280 : if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
1105 : 22290280 : && TREE_TYPE (value) != error_mark_node
1106 : 44580560 : && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
1107 : 22289648 : set_underlying_type (value);
1108 : :
1109 : : /* It's important that push_template_decl below follows
1110 : : set_underlying_type above so that the created template
1111 : : carries the properly set type of VALUE. */
1112 : 22290280 : if (processing_template_decl)
1113 : 18501670 : value = push_template_decl (value);
1114 : :
1115 : 22290280 : record_locally_defined_typedef (value);
1116 : 22290280 : return value;
1117 : : }
1118 : :
1119 : 48805148 : int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
1120 : :
1121 : 48805148 : if (!friendp && DECL_IN_AGGR_P (value))
1122 : : {
1123 : 0 : error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
1124 : 0 : return void_type_node;
1125 : : }
1126 : :
1127 : 48805148 : if (asmspec_tree && asmspec_tree != error_mark_node)
1128 : 194 : asmspec = TREE_STRING_POINTER (asmspec_tree);
1129 : :
1130 : 48805148 : if (init)
1131 : : {
1132 : 18623967 : if (TREE_CODE (value) == FUNCTION_DECL)
1133 : : {
1134 : 8648681 : if (init == ridpointers[(int)RID_DELETE]
1135 : 8648681 : || (TREE_CODE (init) == STRING_CST
1136 : 9 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
1137 : : {
1138 : 2946473 : DECL_DELETED_FN (value) = 1;
1139 : 2946473 : DECL_DECLARED_INLINE_P (value) = 1;
1140 : 2946473 : if (TREE_CODE (init) == STRING_CST)
1141 : 9 : DECL_INITIAL (value) = init;
1142 : : }
1143 : 5702208 : else if (init == ridpointers[(int)RID_DEFAULT])
1144 : : {
1145 : 5344320 : if (defaultable_fn_check (value))
1146 : : {
1147 : 5344241 : DECL_DEFAULTED_FN (value) = 1;
1148 : 5344241 : DECL_INITIALIZED_IN_CLASS_P (value) = 1;
1149 : 5344241 : DECL_DECLARED_INLINE_P (value) = 1;
1150 : : /* grokfndecl set this to error_mark_node, but we want to
1151 : : leave it unset until synthesize_method. */
1152 : 5344241 : DECL_INITIAL (value) = NULL_TREE;
1153 : : }
1154 : : }
1155 : 357888 : else if (TREE_CODE (init) == DEFERRED_PARSE)
1156 : 0 : error ("invalid initializer for member function %qD", value);
1157 : 357888 : else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
1158 : : {
1159 : 357879 : if (integer_zerop (init))
1160 : 357840 : DECL_PURE_VIRTUAL_P (value) = 1;
1161 : 39 : else if (error_operand_p (init))
1162 : : ; /* An error has already been reported. */
1163 : : else
1164 : 0 : error ("invalid initializer for member function %qD",
1165 : : value);
1166 : : }
1167 : : else
1168 : : {
1169 : 9 : gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
1170 : 9 : location_t iloc
1171 : 9 : = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value));
1172 : 9 : if (friendp)
1173 : 3 : error_at (iloc, "initializer specified for friend "
1174 : : "function %qD", value);
1175 : : else
1176 : 6 : error_at (iloc, "initializer specified for static "
1177 : : "member function %qD", value);
1178 : : }
1179 : : }
1180 : 9975286 : else if (TREE_CODE (value) == FIELD_DECL)
1181 : : /* C++11 NSDMI, keep going. */;
1182 : 9090807 : else if (!VAR_P (value))
1183 : 0 : gcc_unreachable ();
1184 : : }
1185 : :
1186 : : /* Pass friend decls back. */
1187 : 48805148 : if ((TREE_CODE (value) == FUNCTION_DECL
1188 : 25088157 : || TREE_CODE (value) == TEMPLATE_DECL)
1189 : 48805196 : && DECL_CONTEXT (value) != current_class_type)
1190 : : {
1191 : 1187445 : if (attrlist)
1192 : 0 : cplus_decl_attributes (&value, attrlist, 0);
1193 : 1187445 : return value;
1194 : : }
1195 : :
1196 : : /* Need to set this before push_template_decl. */
1197 : 47617703 : if (VAR_P (value))
1198 : 9655759 : DECL_CONTEXT (value) = current_class_type;
1199 : :
1200 : 47617703 : if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1201 : : {
1202 : 16244456 : value = push_template_decl (value);
1203 : 16244456 : if (error_operand_p (value))
1204 : 7 : return error_mark_node;
1205 : : }
1206 : :
1207 : 47617696 : if (attrlist)
1208 : 233645 : cplus_decl_attributes (&value, attrlist, 0);
1209 : :
1210 : 47617696 : if (init && DIRECT_LIST_INIT_P (init))
1211 : : flags = LOOKUP_NORMAL;
1212 : : else
1213 : : flags = LOOKUP_IMPLICIT;
1214 : :
1215 : 47617696 : switch (TREE_CODE (value))
1216 : : {
1217 : 9655755 : case VAR_DECL:
1218 : 9655755 : finish_static_data_member_decl (value, init, init_const_expr_p,
1219 : : asmspec_tree, flags);
1220 : 9655755 : return value;
1221 : :
1222 : 15432350 : case FIELD_DECL:
1223 : 15432350 : if (asmspec)
1224 : 6 : error ("%<asm%> specifiers are not permitted on non-static data members");
1225 : 15432350 : if (DECL_INITIAL (value) == error_mark_node)
1226 : 3 : init = error_mark_node;
1227 : 15432350 : cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1228 : : NULL_TREE, flags);
1229 : 15432350 : DECL_IN_AGGR_P (value) = 1;
1230 : 15432350 : return value;
1231 : :
1232 : 22529591 : case FUNCTION_DECL:
1233 : 22529591 : if (asmspec)
1234 : 92 : set_user_assembler_name (value, asmspec);
1235 : :
1236 : 22529591 : cp_finish_decl (value,
1237 : : /*init=*/NULL_TREE,
1238 : : /*init_const_expr_p=*/false,
1239 : : asmspec_tree, flags);
1240 : :
1241 : : /* Pass friends back this way. */
1242 : 22529591 : if (DECL_UNIQUE_FRIEND_P (value))
1243 : 0 : return void_type_node;
1244 : :
1245 : 22529591 : DECL_IN_AGGR_P (value) = 1;
1246 : 22529591 : return value;
1247 : :
1248 : 0 : default:
1249 : 0 : gcc_unreachable ();
1250 : : }
1251 : : return NULL_TREE;
1252 : : }
1253 : :
1254 : : /* Like `grokfield', but for bitfields.
1255 : : WIDTH is the width of the bitfield, a constant expression.
1256 : : The other parameters are as for grokfield. */
1257 : :
1258 : : tree
1259 : 356490 : grokbitfield (const cp_declarator *declarator,
1260 : : cp_decl_specifier_seq *declspecs, tree width, tree init,
1261 : : tree attrlist)
1262 : : {
1263 : 356490 : tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1264 : 356490 : init != NULL_TREE, &attrlist);
1265 : :
1266 : 356490 : if (value == error_mark_node)
1267 : : return NULL_TREE; /* friends went bad. */
1268 : :
1269 : 356484 : tree type = TREE_TYPE (value);
1270 : 356484 : if (type == error_mark_node)
1271 : : return value;
1272 : :
1273 : : /* Pass friendly classes back. */
1274 : 356442 : if (VOID_TYPE_P (value))
1275 : 0 : return void_type_node;
1276 : :
1277 : 356442 : if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)
1278 : 356442 : && (INDIRECT_TYPE_P (type) || !dependent_type_p (type)))
1279 : : {
1280 : 33 : error_at (DECL_SOURCE_LOCATION (value),
1281 : : "bit-field %qD with non-integral type %qT",
1282 : : value, type);
1283 : 33 : return error_mark_node;
1284 : : }
1285 : :
1286 : 356409 : if (TREE_CODE (value) == TYPE_DECL)
1287 : : {
1288 : 6 : error_at (DECL_SOURCE_LOCATION (value),
1289 : : "cannot declare %qD to be a bit-field type", value);
1290 : 6 : return NULL_TREE;
1291 : : }
1292 : :
1293 : : /* Usually, finish_struct_1 catches bitfields with invalid types.
1294 : : But, in the case of bitfields with function type, we confuse
1295 : : ourselves into thinking they are member functions, so we must
1296 : : check here. */
1297 : 356403 : if (TREE_CODE (value) == FUNCTION_DECL)
1298 : : {
1299 : 3 : error_at (DECL_SOURCE_LOCATION (value),
1300 : : "cannot declare bit-field %qD with function type", value);
1301 : 3 : return NULL_TREE;
1302 : : }
1303 : :
1304 : 356400 : if (TYPE_WARN_IF_NOT_ALIGN (type))
1305 : : {
1306 : 9 : error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field "
1307 : : "%qD with %<warn_if_not_aligned%> type", value);
1308 : 9 : return NULL_TREE;
1309 : : }
1310 : :
1311 : 356391 : if (DECL_IN_AGGR_P (value))
1312 : : {
1313 : 0 : error ("%qD is already defined in the class %qT", value,
1314 : 0 : DECL_CONTEXT (value));
1315 : 0 : return void_type_node;
1316 : : }
1317 : :
1318 : 356391 : if (TREE_STATIC (value))
1319 : : {
1320 : 6 : error_at (DECL_SOURCE_LOCATION (value),
1321 : : "static member %qD cannot be a bit-field", value);
1322 : 6 : return NULL_TREE;
1323 : : }
1324 : :
1325 : 356385 : int flags = LOOKUP_IMPLICIT;
1326 : 356385 : if (init && DIRECT_LIST_INIT_P (init))
1327 : : flags = LOOKUP_NORMAL;
1328 : 356385 : cp_finish_decl (value, init, false, NULL_TREE, flags);
1329 : :
1330 : 356385 : if (width != error_mark_node)
1331 : : {
1332 : : /* The width must be an integer type. */
1333 : 356368 : if (!type_dependent_expression_p (width)
1334 : 356368 : && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1335 : 12 : error ("width of bit-field %qD has non-integral type %qT", value,
1336 : 12 : TREE_TYPE (width));
1337 : 356356 : else if (!check_for_bare_parameter_packs (width))
1338 : : {
1339 : : /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1340 : : check_bitfield_decl picks it from there later and sets DECL_SIZE
1341 : : accordingly. */
1342 : 356350 : DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1343 : 356350 : SET_DECL_C_BIT_FIELD (value);
1344 : : }
1345 : : }
1346 : :
1347 : 356385 : DECL_IN_AGGR_P (value) = 1;
1348 : :
1349 : 356385 : if (attrlist)
1350 : 1126 : cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1351 : :
1352 : 356385 : return value;
1353 : : }
1354 : :
1355 : :
1356 : : /* Returns true iff ATTR is an attribute which needs to be applied at
1357 : : instantiation time rather than template definition time. */
1358 : :
1359 : : static bool
1360 : 11752274 : is_late_template_attribute (tree attr, tree decl)
1361 : : {
1362 : 11752274 : tree name = get_attribute_name (attr);
1363 : 11752274 : tree args = TREE_VALUE (attr);
1364 : 11752274 : const struct attribute_spec *spec = lookup_attribute_spec (name);
1365 : 11752274 : tree arg;
1366 : :
1367 : 11752274 : if (!spec)
1368 : : /* Unknown attribute. */
1369 : : return false;
1370 : :
1371 : : /* Attribute weak handling wants to write out assembly right away. */
1372 : 3705998 : if (is_attribute_p ("weak", name))
1373 : : return true;
1374 : :
1375 : : /* Attributes used and unused are applied directly to typedefs for the
1376 : : benefit of maybe_warn_unused_local_typedefs. */
1377 : 3705995 : if (TREE_CODE (decl) == TYPE_DECL
1378 : 3705995 : && (is_attribute_p ("unused", name)
1379 : 68092 : || is_attribute_p ("used", name)))
1380 : : return false;
1381 : :
1382 : : /* Attribute tls_model wants to modify the symtab. */
1383 : 3705961 : if (is_attribute_p ("tls_model", name))
1384 : : return true;
1385 : :
1386 : : /* #pragma omp declare simd attribute needs to be always deferred. */
1387 : 3705958 : if (flag_openmp
1388 : 3705958 : && is_attribute_p ("omp declare simd", name))
1389 : : return true;
1390 : :
1391 : 3705086 : if (args == error_mark_node)
1392 : : return false;
1393 : :
1394 : : /* An attribute pack is clearly dependent. */
1395 : 3705083 : if (args && PACK_EXPANSION_P (args))
1396 : : return true;
1397 : :
1398 : : /* If any of the arguments are dependent expressions, we can't evaluate
1399 : : the attribute until instantiation time. */
1400 : 4405730 : for (arg = args; arg; arg = TREE_CHAIN (arg))
1401 : : {
1402 : 799095 : tree t = TREE_VALUE (arg);
1403 : :
1404 : : /* If the first attribute argument is an identifier, only consider
1405 : : second and following arguments. Attributes like mode, format,
1406 : : cleanup and several target specific attributes aren't late
1407 : : just because they have an IDENTIFIER_NODE as first argument. */
1408 : 764175 : if (arg == args && attribute_takes_identifier_p (name)
1409 : 799095 : && identifier_p (t))
1410 : 33149 : continue;
1411 : :
1412 : 765946 : if (value_dependent_expression_p (t))
1413 : : return true;
1414 : : }
1415 : :
1416 : 3606635 : if (TREE_CODE (decl) == TYPE_DECL
1417 : 3538636 : || TYPE_P (decl)
1418 : 3196770 : || spec->type_required)
1419 : : {
1420 : 811257 : tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1421 : :
1422 : 469391 : if (!type)
1423 : : return true;
1424 : :
1425 : : /* We can't apply any attributes to a completely unknown type until
1426 : : instantiation time. */
1427 : 811254 : enum tree_code code = TREE_CODE (type);
1428 : 811254 : if (code == TEMPLATE_TYPE_PARM
1429 : 811254 : || code == BOUND_TEMPLATE_TEMPLATE_PARM
1430 : 787618 : || code == TYPENAME_TYPE)
1431 : : return true;
1432 : : /* Also defer most attributes on dependent types. This is not
1433 : : necessary in all cases, but is the better default. */
1434 : 784039 : else if (dependent_type_p (type)
1435 : : /* But some attributes specifically apply to templates. */
1436 : 736181 : && !is_attribute_p ("abi_tag", name)
1437 : 736157 : && !is_attribute_p ("deprecated", name)
1438 : 404615 : && !is_attribute_p ("unavailable", name)
1439 : 1188648 : && !is_attribute_p ("visibility", name))
1440 : : return true;
1441 : : else
1442 : : return false;
1443 : : }
1444 : : else
1445 : : return false;
1446 : : }
1447 : :
1448 : : /* ATTR_P is a list of attributes. Remove any attributes which need to be
1449 : : applied at instantiation time and return them. If IS_DEPENDENT is true,
1450 : : the declaration itself is dependent, so all attributes should be applied
1451 : : at instantiation time. */
1452 : :
1453 : : tree
1454 : 198997128 : splice_template_attributes (tree *attr_p, tree decl)
1455 : : {
1456 : 198997128 : tree *p = attr_p;
1457 : 198997128 : tree late_attrs = NULL_TREE;
1458 : 198997128 : tree *q = &late_attrs;
1459 : :
1460 : 198997128 : if (!p || *p == error_mark_node)
1461 : : return NULL_TREE;
1462 : :
1463 : 210749402 : for (; *p; )
1464 : : {
1465 : 11752274 : if (is_late_template_attribute (*p, decl))
1466 : : {
1467 : 531141 : ATTR_IS_DEPENDENT (*p) = 1;
1468 : 531141 : *q = *p;
1469 : 531141 : *p = TREE_CHAIN (*p);
1470 : 531141 : q = &TREE_CHAIN (*q);
1471 : 531141 : *q = NULL_TREE;
1472 : : }
1473 : : else
1474 : 11221133 : p = &TREE_CHAIN (*p);
1475 : : }
1476 : :
1477 : 198997128 : return late_attrs;
1478 : : }
1479 : :
1480 : : /* Attach any LATE_ATTRS to DECL_P, after the non-dependent attributes have
1481 : : been applied by a previous call to decl_attributes. */
1482 : :
1483 : : static void
1484 : 504982 : save_template_attributes (tree late_attrs, tree *decl_p, int flags)
1485 : : {
1486 : 504982 : tree *q;
1487 : :
1488 : 504982 : if (!late_attrs)
1489 : : return;
1490 : :
1491 : 504982 : if (DECL_P (*decl_p))
1492 : 473485 : q = &DECL_ATTRIBUTES (*decl_p);
1493 : : else
1494 : 31497 : q = &TYPE_ATTRIBUTES (*decl_p);
1495 : :
1496 : 504982 : tree old_attrs = *q;
1497 : :
1498 : : /* Place the late attributes at the beginning of the attribute
1499 : : list. */
1500 : 504982 : late_attrs = chainon (late_attrs, *q);
1501 : 504982 : if (*q != late_attrs
1502 : 504982 : && !DECL_P (*decl_p)
1503 : 31497 : && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1504 : : {
1505 : 31 : if (!dependent_type_p (*decl_p))
1506 : 3 : *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1507 : : else
1508 : : {
1509 : 28 : *decl_p = build_variant_type_copy (*decl_p);
1510 : 28 : TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1511 : : }
1512 : : }
1513 : : else
1514 : 504951 : *q = late_attrs;
1515 : :
1516 : 504982 : if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1517 : : {
1518 : : /* We've added new attributes directly to the main variant, so
1519 : : now we need to update all of the other variants to include
1520 : : these new attributes. */
1521 : 31469 : tree variant;
1522 : 31609 : for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1523 : 140 : variant = TYPE_NEXT_VARIANT (variant))
1524 : : {
1525 : 140 : gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1526 : 140 : TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1527 : : }
1528 : : }
1529 : : }
1530 : :
1531 : : /* True if ATTRS contains any dependent attributes that affect type
1532 : : identity. */
1533 : :
1534 : : bool
1535 : 1382220208 : any_dependent_type_attributes_p (tree attrs)
1536 : : {
1537 : 1404165848 : for (tree a = attrs; a; a = TREE_CHAIN (a))
1538 : 21946191 : if (ATTR_IS_DEPENDENT (a))
1539 : : {
1540 : 1539173 : const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1541 : 1539173 : if (as && as->affects_type_identity)
1542 : : return true;
1543 : : }
1544 : : return false;
1545 : : }
1546 : :
1547 : : /* Return true iff ATTRS are acceptable attributes to be applied in-place
1548 : : to a typedef which gives a previously unnamed class or enum a name for
1549 : : linkage purposes. */
1550 : :
1551 : : bool
1552 : 374905 : attributes_naming_typedef_ok (tree attrs)
1553 : : {
1554 : 386670 : for (; attrs; attrs = TREE_CHAIN (attrs))
1555 : : {
1556 : 11768 : tree name = get_attribute_name (attrs);
1557 : 11768 : if (is_attribute_p ("vector_size", name))
1558 : : return false;
1559 : : }
1560 : : return true;
1561 : : }
1562 : :
1563 : : /* Like reconstruct_complex_type, but handle also template trees. */
1564 : :
1565 : : tree
1566 : 26516 : cp_reconstruct_complex_type (tree type, tree bottom)
1567 : : {
1568 : 26516 : tree inner, outer;
1569 : :
1570 : 26516 : if (TYPE_PTR_P (type))
1571 : : {
1572 : 32 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1573 : 32 : outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1574 : 32 : TYPE_REF_CAN_ALIAS_ALL (type));
1575 : : }
1576 : : else if (TYPE_REF_P (type))
1577 : : {
1578 : 3 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1579 : 3 : outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1580 : 3 : TYPE_REF_CAN_ALIAS_ALL (type));
1581 : : }
1582 : : else if (TREE_CODE (type) == ARRAY_TYPE)
1583 : : {
1584 : 27 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1585 : 27 : outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1586 : : /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1587 : : element type qualification will be handled by the recursive
1588 : : cp_reconstruct_complex_type call and cp_build_qualified_type
1589 : : for ARRAY_TYPEs changes the element type. */
1590 : 27 : return outer;
1591 : : }
1592 : : else if (TREE_CODE (type) == FUNCTION_TYPE)
1593 : : {
1594 : 99 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1595 : 99 : outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1596 : 99 : outer = apply_memfn_quals (outer, type_memfn_quals (type));
1597 : : }
1598 : : else if (TREE_CODE (type) == METHOD_TYPE)
1599 : : {
1600 : 12 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1601 : : /* The build_method_type_directly() routine prepends 'this' to argument list,
1602 : : so we must compensate by getting rid of it. */
1603 : 12 : outer
1604 : : = build_method_type_directly
1605 : 12 : (class_of_this_parm (type), inner,
1606 : 12 : TREE_CHAIN (TYPE_ARG_TYPES (type)));
1607 : : }
1608 : : else if (TREE_CODE (type) == OFFSET_TYPE)
1609 : : {
1610 : 3 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1611 : 3 : outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1612 : : }
1613 : : else
1614 : : return bottom;
1615 : :
1616 : 149 : if (TYPE_ATTRIBUTES (type))
1617 : 0 : outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1618 : 149 : outer = cp_build_qualified_type (outer, cp_type_quals (type));
1619 : 149 : outer = cxx_copy_lang_qualifiers (outer, type);
1620 : :
1621 : 149 : return outer;
1622 : : }
1623 : :
1624 : : /* Replaces any constexpr expression that may be into the attributes
1625 : : arguments with their reduced value. */
1626 : :
1627 : : void
1628 : 324302661 : cp_check_const_attributes (tree attributes)
1629 : : {
1630 : 324302661 : if (attributes == error_mark_node)
1631 : : return;
1632 : :
1633 : : tree attr;
1634 : 357600306 : for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1635 : : {
1636 : 33297645 : if (cxx_contract_attribute_p (attr))
1637 : 665 : continue;
1638 : :
1639 : 33296980 : tree arg;
1640 : : /* As we implement alignas using gnu::aligned attribute and
1641 : : alignas argument is a constant expression, force manifestly
1642 : : constant evaluation of aligned attribute argument. */
1643 : 33296980 : bool manifestly_const_eval
1644 : 33296980 : = is_attribute_p ("aligned", get_attribute_name (attr));
1645 : 44609740 : for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1646 : 11312760 : arg = TREE_CHAIN (arg))
1647 : : {
1648 : 11312760 : tree expr = TREE_VALUE (arg);
1649 : 11312760 : if (EXPR_P (expr))
1650 : 5522 : TREE_VALUE (arg)
1651 : 2761 : = fold_non_dependent_expr (expr, tf_warning_or_error,
1652 : : manifestly_const_eval);
1653 : : }
1654 : : }
1655 : : }
1656 : :
1657 : : /* Copies hot or cold attributes to a function FN if present on the
1658 : : encapsulating class, struct, or union TYPE. */
1659 : :
1660 : : void
1661 : 15954334 : maybe_propagate_warmth_attributes (tree fn, tree type)
1662 : : {
1663 : 15954334 : if (fn == NULL_TREE || type == NULL_TREE
1664 : 15954334 : || !(TREE_CODE (type) == RECORD_TYPE
1665 : : || TREE_CODE (type) == UNION_TYPE))
1666 : : return;
1667 : :
1668 : 15954334 : tree has_cold_attr = lookup_attribute ("cold", TYPE_ATTRIBUTES (type));
1669 : 15954334 : tree has_hot_attr = lookup_attribute ("hot", TYPE_ATTRIBUTES (type));
1670 : :
1671 : 15954334 : if (has_cold_attr || has_hot_attr)
1672 : : {
1673 : : /* Transparently ignore the new warmth attribute if it
1674 : : conflicts with a present function attribute. Otherwise
1675 : : decl_attribute would still honour the present attribute,
1676 : : but producing an undesired warning in the process. */
1677 : :
1678 : 18 : if (has_cold_attr)
1679 : : {
1680 : 9 : if (lookup_attribute ("hot", DECL_ATTRIBUTES (fn)) == NULL)
1681 : : {
1682 : 9 : tree cold_cons
1683 : 9 : = tree_cons (get_identifier ("cold"), NULL, NULL);
1684 : :
1685 : 9 : decl_attributes (&fn, cold_cons, 0);
1686 : : }
1687 : : }
1688 : 9 : else if (has_hot_attr)
1689 : : {
1690 : 9 : if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)) == NULL)
1691 : : {
1692 : 9 : tree hot_cons
1693 : 9 : = tree_cons (get_identifier ("hot"), NULL, NULL);
1694 : :
1695 : 9 : decl_attributes (&fn, hot_cons, 0);
1696 : : }
1697 : : }
1698 : : }
1699 : : }
1700 : :
1701 : : /* Return the last pushed declaration for the symbol DECL or NULL
1702 : : when no such declaration exists. */
1703 : :
1704 : : static tree
1705 : 324250971 : find_last_decl (tree decl)
1706 : : {
1707 : 324250971 : tree last_decl = NULL_TREE;
1708 : :
1709 : 324250971 : if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1710 : : {
1711 : : /* Template specializations are matched elsewhere. */
1712 : 272163233 : if (DECL_LANG_SPECIFIC (decl)
1713 : 272163233 : && DECL_USE_TEMPLATE (decl))
1714 : : return NULL_TREE;
1715 : :
1716 : : /* Look up the declaration in its scope. */
1717 : 267691020 : tree pushed_scope = NULL_TREE;
1718 : 267691020 : if (tree ctype = DECL_CONTEXT (decl))
1719 : 208995015 : pushed_scope = push_scope (ctype);
1720 : :
1721 : 267691020 : last_decl = lookup_name (name);
1722 : :
1723 : 267691020 : if (pushed_scope)
1724 : 123515365 : pop_scope (pushed_scope);
1725 : :
1726 : : /* The declaration may be a member conversion operator
1727 : : or a bunch of overfloads (handle the latter below). */
1728 : 267691020 : if (last_decl && BASELINK_P (last_decl))
1729 : 86406 : last_decl = BASELINK_FUNCTIONS (last_decl);
1730 : : }
1731 : :
1732 : 52114513 : if (!last_decl)
1733 : 234880786 : return NULL_TREE;
1734 : :
1735 : 84897972 : if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1736 : : {
1737 : : /* A set of overloads of the same function. */
1738 : 571712562 : for (lkp_iterator iter (last_decl); iter; ++iter)
1739 : : {
1740 : 495388285 : if (TREE_CODE (*iter) == OVERLOAD)
1741 : 0 : continue;
1742 : :
1743 : 495388285 : tree d = *iter;
1744 : :
1745 : : /* We can't compare versions in the middle of processing the
1746 : : attribute that has the version. */
1747 : 495388285 : if (TREE_CODE (d) == FUNCTION_DECL
1748 : 495388285 : && DECL_FUNCTION_VERSIONED (d))
1749 : 8554623 : return NULL_TREE;
1750 : :
1751 : 495387253 : if (decls_match (decl, d, /*record_decls=*/false))
1752 : 8553591 : return d;
1753 : : }
1754 : 76324277 : return NULL_TREE;
1755 : : }
1756 : :
1757 : : return NULL_TREE;
1758 : : }
1759 : :
1760 : : /* Like decl_attributes, but handle C++ complexity. */
1761 : :
1762 : : void
1763 : 324300675 : cplus_decl_attributes (tree *decl, tree attributes, int flags)
1764 : : {
1765 : 324300675 : if (*decl == NULL_TREE || *decl == void_type_node
1766 : 324300675 : || *decl == error_mark_node || attributes == error_mark_node)
1767 : : return;
1768 : :
1769 : : /* Add implicit "omp declare target" attribute if requested. */
1770 : 324300593 : if (vec_safe_length (scope_chain->omp_declare_target_attribute)
1771 : 10421 : && ((VAR_P (*decl)
1772 : 2056 : && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1773 : 3152 : || TREE_CODE (*decl) == FUNCTION_DECL))
1774 : : {
1775 : 1577 : if (VAR_P (*decl)
1776 : 1577 : && DECL_CLASS_SCOPE_P (*decl))
1777 : 0 : error ("%q+D static data member inside of declare target directive",
1778 : : *decl);
1779 : : else
1780 : : {
1781 : 1577 : if (VAR_P (*decl)
1782 : 1577 : && (processing_template_decl
1783 : 269 : || !omp_mappable_type (TREE_TYPE (*decl))))
1784 : 50 : attributes
1785 : 50 : = tree_cons (get_identifier ("omp declare target implicit"),
1786 : : NULL_TREE, attributes);
1787 : : else
1788 : : {
1789 : 1527 : attributes = tree_cons (get_identifier ("omp declare target"),
1790 : : NULL_TREE, attributes);
1791 : 1527 : attributes
1792 : 1527 : = tree_cons (get_identifier ("omp declare target block"),
1793 : : NULL_TREE, attributes);
1794 : : }
1795 : 1577 : if (TREE_CODE (*decl) == FUNCTION_DECL)
1796 : : {
1797 : 1308 : cp_omp_declare_target_attr &last
1798 : 1308 : = scope_chain->omp_declare_target_attribute->last ();
1799 : 1308 : int device_type = MAX (last.device_type, 0);
1800 : 1308 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
1801 : 1308 : && !lookup_attribute ("omp declare target host",
1802 : : attributes))
1803 : 6 : attributes
1804 : 6 : = tree_cons (get_identifier ("omp declare target host"),
1805 : : NULL_TREE, attributes);
1806 : 1308 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
1807 : 1308 : && !lookup_attribute ("omp declare target nohost",
1808 : : attributes))
1809 : 6 : attributes
1810 : 6 : = tree_cons (get_identifier ("omp declare target nohost"),
1811 : : NULL_TREE, attributes);
1812 : 1308 : if (last.indirect
1813 : 1308 : && !lookup_attribute ("omp declare target indirect",
1814 : : attributes))
1815 : 21 : attributes
1816 : 21 : = tree_cons (get_identifier ("omp declare target indirect"),
1817 : : NULL_TREE, attributes);
1818 : : }
1819 : : }
1820 : : }
1821 : :
1822 : 324300593 : tree late_attrs = NULL_TREE;
1823 : 324300593 : if (processing_template_decl)
1824 : : {
1825 : 198984365 : if (check_for_bare_parameter_packs (attributes))
1826 : : return;
1827 : 198984356 : late_attrs = splice_template_attributes (&attributes, *decl);
1828 : : }
1829 : :
1830 : 324300584 : cp_check_const_attributes (attributes);
1831 : :
1832 : 324300584 : if (flag_openmp || flag_openmp_simd)
1833 : : {
1834 : : bool diagnosed = false;
1835 : 646326 : for (tree *pa = &attributes; *pa; )
1836 : : {
1837 : 97708 : if (get_attribute_namespace (*pa) == omp_identifier)
1838 : : {
1839 : 172 : tree name = get_attribute_name (*pa);
1840 : 172 : if (is_attribute_p ("directive", name)
1841 : 0 : || is_attribute_p ("sequence", name)
1842 : 172 : || is_attribute_p ("decl", name))
1843 : : {
1844 : 172 : const char *p = NULL;
1845 : 172 : if (TREE_VALUE (*pa) == NULL_TREE)
1846 : 9 : p = IDENTIFIER_POINTER (name);
1847 : 335 : for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
1848 : : {
1849 : 163 : tree d = TREE_VALUE (a);
1850 : 163 : gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
1851 : 275 : if (TREE_PUBLIC (d)
1852 : 115 : && (VAR_P (*decl)
1853 : 10 : || TREE_CODE (*decl) == FUNCTION_DECL)
1854 : 278 : && cp_maybe_parse_omp_decl (*decl, d))
1855 : 112 : continue;
1856 : 51 : p = TREE_PUBLIC (d) ? "decl" : "directive";
1857 : : }
1858 : 172 : if (p && !diagnosed)
1859 : : {
1860 : 60 : error ("%<omp::%s%> not allowed to be specified in "
1861 : : "this context", p);
1862 : 60 : diagnosed = true;
1863 : : }
1864 : 172 : if (p)
1865 : : {
1866 : 60 : *pa = TREE_CHAIN (*pa);
1867 : 60 : continue;
1868 : : }
1869 : : }
1870 : : }
1871 : 97648 : pa = &TREE_CHAIN (*pa);
1872 : : }
1873 : : }
1874 : :
1875 : 324300584 : if (TREE_CODE (*decl) == TEMPLATE_DECL)
1876 : 1197 : decl = &DECL_TEMPLATE_RESULT (*decl);
1877 : :
1878 : 324300584 : if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1879 : : {
1880 : 49613 : attributes
1881 : 49613 : = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1882 : 49613 : decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1883 : : attributes, flags);
1884 : : }
1885 : : else
1886 : : {
1887 : 324250971 : tree last_decl = find_last_decl (*decl);
1888 : 324250971 : decl_attributes (decl, attributes, flags, last_decl);
1889 : : }
1890 : :
1891 : 324300584 : if (late_attrs)
1892 : 504982 : save_template_attributes (late_attrs, decl, flags);
1893 : :
1894 : : /* Propagate deprecation out to the template. */
1895 : 324300584 : if (TREE_DEPRECATED (*decl))
1896 : 2027528 : if (tree ti = get_template_info (*decl))
1897 : : {
1898 : 1107136 : tree tmpl = TI_TEMPLATE (ti);
1899 : 1107136 : tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1900 : 697818 : : DECL_TEMPLATE_RESULT (tmpl));
1901 : 1107136 : if (*decl == pattern)
1902 : 386181 : TREE_DEPRECATED (tmpl) = true;
1903 : : }
1904 : :
1905 : : /* Likewise, propagate unavailability out to the template. */
1906 : 324300584 : if (TREE_UNAVAILABLE (*decl))
1907 : 237 : if (tree ti = get_template_info (*decl))
1908 : : {
1909 : 9 : tree tmpl = TI_TEMPLATE (ti);
1910 : 9 : tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1911 : 0 : : DECL_TEMPLATE_RESULT (tmpl));
1912 : 9 : if (*decl == pattern)
1913 : 9 : TREE_UNAVAILABLE (tmpl) = true;
1914 : : }
1915 : : }
1916 : :
1917 : : /* Walks through the namespace- or function-scope anonymous union
1918 : : OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1919 : : Returns one of the fields for use in the mangled name. */
1920 : :
1921 : : static tree
1922 : 281 : build_anon_union_vars (tree type, tree object)
1923 : : {
1924 : 281 : tree main_decl = NULL_TREE;
1925 : 281 : tree field;
1926 : :
1927 : : /* Rather than write the code to handle the non-union case,
1928 : : just give an error. */
1929 : 281 : if (TREE_CODE (type) != UNION_TYPE)
1930 : : {
1931 : 6 : error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
1932 : : "anonymous struct not inside named type");
1933 : 6 : return error_mark_node;
1934 : : }
1935 : :
1936 : 275 : for (field = TYPE_FIELDS (type);
1937 : 1005 : field != NULL_TREE;
1938 : 730 : field = DECL_CHAIN (field))
1939 : : {
1940 : 730 : tree decl;
1941 : 730 : tree ref;
1942 : :
1943 : 730 : if (DECL_ARTIFICIAL (field))
1944 : 353 : continue;
1945 : 377 : if (TREE_CODE (field) != FIELD_DECL)
1946 : : {
1947 : 0 : permerror (DECL_SOURCE_LOCATION (field),
1948 : : "%q#D invalid; an anonymous union can only "
1949 : : "have non-static data members", field);
1950 : 0 : continue;
1951 : : }
1952 : :
1953 : 377 : if (TREE_PRIVATE (field))
1954 : 6 : permerror (DECL_SOURCE_LOCATION (field),
1955 : : "private member %q#D in anonymous union", field);
1956 : 371 : else if (TREE_PROTECTED (field))
1957 : 0 : permerror (DECL_SOURCE_LOCATION (field),
1958 : : "protected member %q#D in anonymous union", field);
1959 : :
1960 : 377 : if (processing_template_decl)
1961 : 60 : ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1962 : 60 : DECL_NAME (field), NULL_TREE);
1963 : : else
1964 : 317 : ref = build_class_member_access_expr (object, field, NULL_TREE,
1965 : : false, tf_warning_or_error);
1966 : :
1967 : 377 : if (DECL_NAME (field))
1968 : : {
1969 : 353 : tree base;
1970 : :
1971 : 353 : decl = build_decl (input_location,
1972 : 353 : VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1973 : 353 : DECL_ANON_UNION_VAR_P (decl) = 1;
1974 : 353 : DECL_ARTIFICIAL (decl) = 1;
1975 : :
1976 : 353 : base = get_base_address (object);
1977 : 353 : TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1978 : 353 : TREE_STATIC (decl) = TREE_STATIC (base);
1979 : 353 : DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1980 : :
1981 : 353 : SET_DECL_VALUE_EXPR (decl, ref);
1982 : 353 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
1983 : :
1984 : 353 : decl = pushdecl (decl);
1985 : : }
1986 : 24 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1987 : 24 : decl = build_anon_union_vars (TREE_TYPE (field), ref);
1988 : : else
1989 : : decl = 0;
1990 : :
1991 : 377 : if (main_decl == NULL_TREE)
1992 : 730 : main_decl = decl;
1993 : : }
1994 : :
1995 : : return main_decl;
1996 : : }
1997 : :
1998 : : /* Finish off the processing of a UNION_TYPE structure. If the union is an
1999 : : anonymous union, then all members must be laid out together. PUBLIC_P
2000 : : is nonzero if this union is not declared static. */
2001 : :
2002 : : void
2003 : 266 : finish_anon_union (tree anon_union_decl)
2004 : : {
2005 : 266 : tree type;
2006 : 266 : tree main_decl;
2007 : 266 : bool public_p;
2008 : :
2009 : 266 : if (anon_union_decl == error_mark_node)
2010 : : return;
2011 : :
2012 : 257 : type = TREE_TYPE (anon_union_decl);
2013 : 257 : public_p = TREE_PUBLIC (anon_union_decl);
2014 : :
2015 : : /* The VAR_DECL's context is the same as the TYPE's context. */
2016 : 257 : DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
2017 : :
2018 : 257 : if (TYPE_FIELDS (type) == NULL_TREE)
2019 : : return;
2020 : :
2021 : 257 : if (public_p)
2022 : : {
2023 : 0 : error ("namespace-scope anonymous aggregates must be static");
2024 : 0 : return;
2025 : : }
2026 : :
2027 : 257 : main_decl = build_anon_union_vars (type, anon_union_decl);
2028 : 257 : if (main_decl == error_mark_node)
2029 : : return;
2030 : 233 : if (main_decl == NULL_TREE)
2031 : : {
2032 : 12 : pedwarn (input_location, 0, "anonymous union with no members");
2033 : 12 : return;
2034 : : }
2035 : :
2036 : 221 : if (!processing_template_decl)
2037 : : {
2038 : : /* Use main_decl to set the mangled name. */
2039 : 185 : DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
2040 : 185 : maybe_commonize_var (anon_union_decl);
2041 : 185 : if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
2042 : : {
2043 : 88 : if (DECL_DISCRIMINATOR_P (anon_union_decl))
2044 : 43 : determine_local_discriminator (anon_union_decl);
2045 : 88 : mangle_decl (anon_union_decl);
2046 : : }
2047 : 185 : DECL_NAME (anon_union_decl) = NULL_TREE;
2048 : : }
2049 : :
2050 : 221 : pushdecl (anon_union_decl);
2051 : 221 : cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
2052 : : }
2053 : :
2054 : : /* Auxiliary functions to make type signatures for
2055 : : `operator new' and `operator delete' correspond to
2056 : : what compiler will be expecting. */
2057 : :
2058 : : tree
2059 : 137808 : coerce_new_type (tree type, location_t loc)
2060 : : {
2061 : 137808 : int e = 0;
2062 : 137808 : tree args = TYPE_ARG_TYPES (type);
2063 : :
2064 : 137808 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2065 : :
2066 : 137808 : if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2067 : : {
2068 : 6 : e = 1;
2069 : 6 : error_at (loc, "%<operator new%> must return type %qT",
2070 : : ptr_type_node);
2071 : : }
2072 : :
2073 : 137808 : if (args && args != void_list_node)
2074 : : {
2075 : 137805 : if (TREE_PURPOSE (args))
2076 : : {
2077 : : /* [basic.stc.dynamic.allocation]
2078 : :
2079 : : The first parameter shall not have an associated default
2080 : : argument. */
2081 : 18 : error_at (loc, "the first parameter of %<operator new%> cannot "
2082 : : "have a default argument");
2083 : : /* Throw away the default argument. */
2084 : 18 : TREE_PURPOSE (args) = NULL_TREE;
2085 : : }
2086 : :
2087 : 137805 : if (!same_type_p (TREE_VALUE (args), size_type_node))
2088 : : {
2089 : 12 : e = 2;
2090 : 12 : args = TREE_CHAIN (args);
2091 : : }
2092 : : }
2093 : : else
2094 : : e = 2;
2095 : :
2096 : 12 : if (e == 2)
2097 : 15 : permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
2098 : : "as first parameter", size_type_node);
2099 : :
2100 : 137808 : switch (e)
2101 : : {
2102 : 15 : case 2:
2103 : 15 : args = tree_cons (NULL_TREE, size_type_node, args);
2104 : : /* Fall through. */
2105 : 21 : case 1:
2106 : 21 : type = (cxx_copy_lang_qualifiers
2107 : 21 : (build_function_type (ptr_type_node, args),
2108 : : type));
2109 : : /* Fall through. */
2110 : 137808 : default:;
2111 : : }
2112 : 137808 : return type;
2113 : : }
2114 : :
2115 : : void
2116 : 181869 : coerce_delete_type (tree decl, location_t loc)
2117 : : {
2118 : 181869 : int e = 0;
2119 : 181869 : tree type = TREE_TYPE (decl);
2120 : 181869 : tree args = TYPE_ARG_TYPES (type);
2121 : :
2122 : 181869 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2123 : :
2124 : 181869 : if (!same_type_p (TREE_TYPE (type), void_type_node))
2125 : : {
2126 : 12 : e = 1;
2127 : 12 : error_at (loc, "%<operator delete%> must return type %qT",
2128 : : void_type_node);
2129 : : }
2130 : :
2131 : 181869 : tree ptrtype = ptr_type_node;
2132 : 181869 : if (destroying_delete_p (decl))
2133 : : {
2134 : 38 : if (DECL_CLASS_SCOPE_P (decl))
2135 : : /* If the function is a destroying operator delete declared in class
2136 : : type C, the type of its first parameter shall be C*. */
2137 : 38 : ptrtype = build_pointer_type (DECL_CONTEXT (decl));
2138 : : else
2139 : : /* A destroying operator delete shall be a class member function named
2140 : : operator delete. */
2141 : 0 : error_at (loc,
2142 : : "destroying %<operator delete%> must be a member function");
2143 : 38 : const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl));
2144 : 38 : if (op->flags & OVL_OP_FLAG_VEC)
2145 : 0 : error_at (loc, "%<operator delete[]%> cannot be a destroying delete");
2146 : 38 : if (!usual_deallocation_fn_p (decl))
2147 : 0 : error_at (loc, "destroying %<operator delete%> must be a usual "
2148 : : "deallocation function");
2149 : : }
2150 : :
2151 : 181866 : if (!args || args == void_list_node
2152 : 363732 : || !same_type_p (TREE_VALUE (args), ptrtype))
2153 : : {
2154 : 15 : e = 2;
2155 : 15 : if (args && args != void_list_node)
2156 : 9 : args = TREE_CHAIN (args);
2157 : 15 : error_at (loc, "%<operator delete%> takes type %qT as first parameter",
2158 : : ptrtype);
2159 : : }
2160 : 181854 : switch (e)
2161 : : {
2162 : 15 : case 2:
2163 : 15 : args = tree_cons (NULL_TREE, ptrtype, args);
2164 : : /* Fall through. */
2165 : 27 : case 1:
2166 : 27 : type = (cxx_copy_lang_qualifiers
2167 : 27 : (build_function_type (void_type_node, args),
2168 : : type));
2169 : : /* Fall through. */
2170 : 181869 : default:;
2171 : : }
2172 : :
2173 : 181869 : TREE_TYPE (decl) = type;
2174 : 181869 : }
2175 : :
2176 : : /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
2177 : : and mark them as needed. */
2178 : :
2179 : : static void
2180 : 288713 : mark_vtable_entries (tree decl, vec<tree> &consteval_vtables)
2181 : : {
2182 : : /* It's OK for the vtable to refer to deprecated virtual functions. */
2183 : 288713 : auto du = make_temp_override (deprecated_state,
2184 : 288713 : UNAVAILABLE_DEPRECATED_SUPPRESS);
2185 : :
2186 : 288713 : bool consteval_seen = false;
2187 : :
2188 : 4927205 : for (auto &e: CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
2189 : : {
2190 : 4061066 : tree fnaddr = e.value;
2191 : :
2192 : 4061066 : STRIP_NOPS (fnaddr);
2193 : :
2194 : 6407026 : if (TREE_CODE (fnaddr) != ADDR_EXPR
2195 : 4061066 : && TREE_CODE (fnaddr) != FDESC_EXPR)
2196 : : /* This entry is an offset: a virtual base class offset, a
2197 : : virtual call offset, an RTTI offset, etc. */
2198 : 2345960 : continue;
2199 : :
2200 : 1715106 : tree fn = TREE_OPERAND (fnaddr, 0);
2201 : 3063957 : if (TREE_CODE (fn) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (fn))
2202 : : {
2203 : 87 : if (!consteval_seen)
2204 : : {
2205 : 42 : consteval_seen = true;
2206 : 42 : consteval_vtables.safe_push (decl);
2207 : : }
2208 : 87 : continue;
2209 : : }
2210 : 1715019 : TREE_ADDRESSABLE (fn) = 1;
2211 : : /* When we don't have vcall offsets, we output thunks whenever
2212 : : we output the vtables that contain them. With vcall offsets,
2213 : : we know all the thunks we'll need when we emit a virtual
2214 : : function, so we emit the thunks there instead. */
2215 : 1715019 : if (DECL_THUNK_P (fn))
2216 : 3890 : use_thunk (fn, /*emit_p=*/0);
2217 : : /* Set the location, as marking the function could cause
2218 : : instantiation. We do not need to preserve the incoming
2219 : : location, as we're called from c_parse_final_cleanups, which
2220 : : takes care of that. */
2221 : 1715019 : input_location = DECL_SOURCE_LOCATION (fn);
2222 : 1715019 : mark_used (fn);
2223 : : }
2224 : 288713 : }
2225 : :
2226 : : /* Replace any consteval functions in vtables with null pointers. */
2227 : :
2228 : : static void
2229 : 90704 : clear_consteval_vfns (vec<tree> &consteval_vtables)
2230 : : {
2231 : 90800 : for (tree vtable : consteval_vtables)
2232 : 318 : for (constructor_elt &elt : CONSTRUCTOR_ELTS (DECL_INITIAL (vtable)))
2233 : : {
2234 : 192 : tree fn = cp_get_fndecl_from_callee (elt.value, /*fold*/false);
2235 : 294 : if (fn && DECL_IMMEDIATE_FUNCTION_P (fn))
2236 : 87 : elt.value = build_zero_cst (vtable_entry_type);
2237 : : }
2238 : 90704 : }
2239 : :
2240 : : /* Adjust the TLS model on variable DECL if need be, typically after
2241 : : the linkage of DECL has been modified. */
2242 : :
2243 : : static void
2244 : 21918302 : adjust_var_decl_tls_model (tree decl)
2245 : : {
2246 : 21918302 : if (CP_DECL_THREAD_LOCAL_P (decl)
2247 : 21918302 : && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
2248 : 77 : set_decl_tls_model (decl, decl_default_tls_model (decl));
2249 : 21918302 : }
2250 : :
2251 : : /* Set DECL up to have the closest approximation of "initialized common"
2252 : : linkage available. */
2253 : :
2254 : : void
2255 : 59496582 : comdat_linkage (tree decl)
2256 : : {
2257 : 59496582 : if (flag_weak)
2258 : : {
2259 : 59496558 : make_decl_one_only (decl, cxx_comdat_group (decl));
2260 : 59496558 : if (HAVE_COMDAT_GROUP && flag_contracts && DECL_CONTRACTS (decl))
2261 : : {
2262 : 249 : symtab_node *n = symtab_node::get (decl);
2263 : 249 : if (tree pre = DECL_PRE_FN (decl))
2264 : 196 : cgraph_node::get_create (pre)->add_to_same_comdat_group (n);
2265 : 249 : if (tree post = DECL_POST_FN (decl))
2266 : 34 : cgraph_node::get_create (post)->add_to_same_comdat_group (n);
2267 : : }
2268 : : }
2269 : 24 : else if (TREE_CODE (decl) == FUNCTION_DECL
2270 : 24 : || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
2271 : : /* We can just emit function and compiler-generated variables
2272 : : statically; having multiple copies is (for the most part) only
2273 : : a waste of space.
2274 : :
2275 : : There are two correctness issues, however: the address of a
2276 : : template instantiation with external linkage should be the
2277 : : same, independent of what translation unit asks for the
2278 : : address, and this will not hold when we emit multiple copies of
2279 : : the function. However, there's little else we can do.
2280 : :
2281 : : Also, by default, the typeinfo implementation assumes that
2282 : : there will be only one copy of the string used as the name for
2283 : : each type. Therefore, if weak symbols are unavailable, the
2284 : : run-time library should perform a more conservative check; it
2285 : : should perform a string comparison, rather than an address
2286 : : comparison. */
2287 : 24 : TREE_PUBLIC (decl) = 0;
2288 : : else
2289 : : {
2290 : : /* Static data member template instantiations, however, cannot
2291 : : have multiple copies. */
2292 : 0 : if (DECL_INITIAL (decl) == 0
2293 : 0 : || DECL_INITIAL (decl) == error_mark_node)
2294 : 0 : DECL_COMMON (decl) = 1;
2295 : 0 : else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2296 : : {
2297 : 0 : DECL_COMMON (decl) = 1;
2298 : 0 : DECL_INITIAL (decl) = error_mark_node;
2299 : : }
2300 : 0 : else if (!DECL_EXPLICIT_INSTANTIATION (decl))
2301 : : {
2302 : : /* We can't do anything useful; leave vars for explicit
2303 : : instantiation. */
2304 : 0 : DECL_EXTERNAL (decl) = 1;
2305 : 0 : DECL_NOT_REALLY_EXTERN (decl) = 0;
2306 : : }
2307 : : }
2308 : :
2309 : 59496582 : if (TREE_PUBLIC (decl))
2310 : 59496558 : DECL_COMDAT (decl) = 1;
2311 : :
2312 : 59496582 : if (VAR_P (decl))
2313 : 21917758 : adjust_var_decl_tls_model (decl);
2314 : 59496582 : }
2315 : :
2316 : : /* For win32 we also want to put explicit instantiations in
2317 : : linkonce sections, so that they will be merged with implicit
2318 : : instantiations; otherwise we get duplicate symbol errors.
2319 : : For Darwin we do not want explicit instantiations to be
2320 : : linkonce. */
2321 : :
2322 : : void
2323 : 211054 : maybe_make_one_only (tree decl)
2324 : : {
2325 : : /* We used to say that this was not necessary on targets that support weak
2326 : : symbols, because the implicit instantiations will defer to the explicit
2327 : : one. However, that's not actually the case in SVR4; a strong definition
2328 : : after a weak one is an error. Also, not making explicit
2329 : : instantiations one_only means that we can end up with two copies of
2330 : : some template instantiations. */
2331 : 211054 : if (! flag_weak)
2332 : : return;
2333 : :
2334 : : /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
2335 : : we can get away with not emitting them if they aren't used. We need
2336 : : to for variables so that cp_finish_decl will update their linkage,
2337 : : because their DECL_INITIAL may not have been set properly yet. */
2338 : :
2339 : 211051 : if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
2340 : : || (! DECL_EXPLICIT_INSTANTIATION (decl)
2341 : : && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
2342 : : {
2343 : 211051 : make_decl_one_only (decl, cxx_comdat_group (decl));
2344 : :
2345 : 211051 : if (VAR_P (decl))
2346 : : {
2347 : 544 : varpool_node *node = varpool_node::get_create (decl);
2348 : 544 : DECL_COMDAT (decl) = 1;
2349 : : /* Mark it needed so we don't forget to emit it. */
2350 : 544 : node->forced_by_abi = true;
2351 : 544 : TREE_USED (decl) = 1;
2352 : :
2353 : 544 : adjust_var_decl_tls_model (decl);
2354 : : }
2355 : : }
2356 : : }
2357 : :
2358 : : /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
2359 : : This predicate will give the right answer during parsing of the
2360 : : function, which other tests may not. */
2361 : :
2362 : : bool
2363 : 27143568 : vague_linkage_p (tree decl)
2364 : : {
2365 : 27143571 : if (!TREE_PUBLIC (decl))
2366 : : {
2367 : : /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
2368 : : maybe-in-charge 'tor variants; in that case we need to check one of
2369 : : the "clones" for the real linkage. But only in that case; before
2370 : : maybe_clone_body we haven't yet copied the linkage to the clones. */
2371 : 11361 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
2372 : 95 : && !DECL_ABSTRACT_P (decl)
2373 : 3 : && DECL_CHAIN (decl)
2374 : 5721 : && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
2375 : 3 : return vague_linkage_p (DECL_CHAIN (decl));
2376 : :
2377 : 5715 : gcc_checking_assert (!DECL_COMDAT (decl));
2378 : : return false;
2379 : : }
2380 : : /* Unfortunately, import_export_decl has not always been called
2381 : : before the function is processed, so we cannot simply check
2382 : : DECL_COMDAT. */
2383 : 27137853 : if (DECL_COMDAT (decl)
2384 : 869224 : || (TREE_CODE (decl) == FUNCTION_DECL
2385 : 868794 : && DECL_DECLARED_INLINE_P (decl))
2386 : 51323 : || (DECL_LANG_SPECIFIC (decl)
2387 : 51007 : && DECL_TEMPLATE_INSTANTIATION (decl))
2388 : 27158836 : || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
2389 : : return true;
2390 : 20973 : else if (DECL_FUNCTION_SCOPE_P (decl))
2391 : : /* A local static in an inline effectively has vague linkage. */
2392 : 0 : return (TREE_STATIC (decl)
2393 : 0 : && vague_linkage_p (DECL_CONTEXT (decl)));
2394 : : else
2395 : : return false;
2396 : : }
2397 : :
2398 : : /* Determine whether or not we want to specifically import or export CTYPE,
2399 : : using various heuristics. */
2400 : :
2401 : : static void
2402 : 3568573 : import_export_class (tree ctype)
2403 : : {
2404 : : /* -1 for imported, 1 for exported. */
2405 : 3568573 : int import_export = 0;
2406 : :
2407 : : /* It only makes sense to call this function at EOF. The reason is
2408 : : that this function looks at whether or not the first non-inline
2409 : : non-abstract virtual member function has been defined in this
2410 : : translation unit. But, we can't possibly know that until we've
2411 : : seen the entire translation unit. */
2412 : 3568573 : gcc_assert (at_eof);
2413 : :
2414 : 3568573 : if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2415 : : return;
2416 : :
2417 : : /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2418 : : we will have CLASSTYPE_INTERFACE_ONLY set but not
2419 : : CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2420 : : heuristic because someone will supply a #pragma implementation
2421 : : elsewhere, and deducing it here would produce a conflict. */
2422 : 1776546 : if (CLASSTYPE_INTERFACE_ONLY (ctype))
2423 : : return;
2424 : :
2425 : 1776546 : if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2426 : : import_export = -1;
2427 : 1776546 : else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2428 : : import_export = 1;
2429 : 1776546 : else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2430 : 1776546 : && !flag_implicit_templates)
2431 : : /* For a template class, without -fimplicit-templates, check the
2432 : : repository. If the virtual table is assigned to this
2433 : : translation unit, then export the class; otherwise, import
2434 : : it. */
2435 : : import_export = -1;
2436 : 1775905 : else if (TYPE_POLYMORPHIC_P (ctype))
2437 : : {
2438 : 1581953 : tree cdecl = TYPE_NAME (ctype);
2439 : 1586828 : if (DECL_LANG_SPECIFIC (cdecl) && DECL_MODULE_ATTACH_P (cdecl))
2440 : : /* For class types attached to a named module, the ABI specifies
2441 : : that the tables are uniquely emitted in the object for the
2442 : : module unit in which it is defined. */
2443 : 153 : import_export = (DECL_MODULE_IMPORT_P (cdecl) ? -1 : 1);
2444 : : else
2445 : : {
2446 : : /* The ABI specifies that the virtual table and associated
2447 : : information are emitted with the key method, if any. */
2448 : 1581800 : tree method = CLASSTYPE_KEY_METHOD (ctype);
2449 : : /* If weak symbol support is not available, then we must be
2450 : : careful not to emit the vtable when the key function is
2451 : : inline. An inline function can be defined in multiple
2452 : : translation units. If we were to emit the vtable in each
2453 : : translation unit containing a definition, we would get
2454 : : multiple definition errors at link-time. */
2455 : 1581800 : if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2456 : 557400 : import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2457 : : }
2458 : : }
2459 : :
2460 : : /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2461 : : a definition anywhere else. */
2462 : 1776546 : if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2463 : : import_export = 0;
2464 : :
2465 : : /* Allow back ends the chance to overrule the decision. */
2466 : 1776546 : if (targetm.cxx.import_export_class)
2467 : 0 : import_export = targetm.cxx.import_export_class (ctype, import_export);
2468 : :
2469 : 1776546 : if (import_export)
2470 : : {
2471 : 558194 : SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2472 : 558194 : CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2473 : : }
2474 : : }
2475 : :
2476 : : /* Return true if VAR has already been provided to the back end; in that
2477 : : case VAR should not be modified further by the front end. */
2478 : : static bool
2479 : 53062034 : var_finalized_p (tree var)
2480 : : {
2481 : 0 : return varpool_node::get_create (var)->definition;
2482 : : }
2483 : :
2484 : : /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2485 : : must be emitted in this translation unit. Mark it as such. */
2486 : :
2487 : : void
2488 : 48506 : mark_needed (tree decl)
2489 : : {
2490 : 48506 : TREE_USED (decl) = 1;
2491 : 48506 : if (TREE_CODE (decl) == FUNCTION_DECL)
2492 : : {
2493 : : /* Extern inline functions don't become needed when referenced.
2494 : : If we know a method will be emitted in other TU and no new
2495 : : functions can be marked reachable, just use the external
2496 : : definition. */
2497 : 40656 : struct cgraph_node *node = cgraph_node::get_create (decl);
2498 : 40656 : node->forced_by_abi = true;
2499 : :
2500 : : /* #pragma interface can call mark_needed for
2501 : : maybe-in-charge 'tors; mark the clones as well. */
2502 : 40656 : tree clone;
2503 : 48802 : FOR_EACH_CLONE (clone, decl)
2504 : 8146 : mark_needed (clone);
2505 : : }
2506 : 7850 : else if (VAR_P (decl))
2507 : : {
2508 : 7850 : varpool_node *node = varpool_node::get_create (decl);
2509 : : /* C++ frontend use mark_decl_references to force COMDAT variables
2510 : : to be output that might appear dead otherwise. */
2511 : 7850 : node->forced_by_abi = true;
2512 : : }
2513 : 48506 : }
2514 : :
2515 : : /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2516 : : returns true if a definition of this entity should be provided in
2517 : : this object file. Callers use this function to determine whether
2518 : : or not to let the back end know that a definition of DECL is
2519 : : available in this translation unit. */
2520 : :
2521 : : bool
2522 : 210431874 : decl_needed_p (tree decl)
2523 : : {
2524 : 210431874 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2525 : : /* This function should only be called at the end of the translation
2526 : : unit. We cannot be sure of whether or not something will be
2527 : : COMDAT until that point. */
2528 : 210431874 : gcc_assert (at_eof);
2529 : :
2530 : : /* All entities with external linkage that are not COMDAT/EXTERN should be
2531 : : emitted; they may be referred to from other object files. */
2532 : 210431874 : if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2533 : : return true;
2534 : :
2535 : : /* Functions marked "dllexport" must be emitted so that they are
2536 : : visible to other DLLs. */
2537 : 210431592 : if (flag_keep_inline_dllexport
2538 : 210431592 : && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2539 : : return true;
2540 : :
2541 : : /* When not optimizing, do not bother to produce definitions for extern
2542 : : symbols. */
2543 : 266568125 : if (DECL_REALLY_EXTERN (decl)
2544 : 0 : && ((TREE_CODE (decl) != FUNCTION_DECL
2545 : 0 : && !optimize)
2546 : 0 : || (TREE_CODE (decl) == FUNCTION_DECL
2547 : 0 : && !opt_for_fn (decl, optimize)))
2548 : 210431592 : && !lookup_attribute ("always_inline", decl))
2549 : : return false;
2550 : :
2551 : : /* If this entity was used, let the back end see it; it will decide
2552 : : whether or not to emit it into the object file. */
2553 : 210431592 : if (TREE_USED (decl))
2554 : : return true;
2555 : :
2556 : : /* Virtual functions might be needed for devirtualization. */
2557 : 49963000 : if (flag_devirtualize
2558 : 49030137 : && TREE_CODE (decl) == FUNCTION_DECL
2559 : 98127202 : && DECL_VIRTUAL_P (decl))
2560 : : return true;
2561 : :
2562 : : /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2563 : : reference to DECL might cause it to be emitted later. */
2564 : : return false;
2565 : : }
2566 : :
2567 : : /* If necessary, write out the vtables for the dynamic class CTYPE.
2568 : : Returns true if any vtables were emitted. */
2569 : :
2570 : : static bool
2571 : 3380902 : maybe_emit_vtables (tree ctype, vec<tree> &consteval_vtables)
2572 : : {
2573 : 3380902 : tree vtbl;
2574 : 3380902 : tree primary_vtbl;
2575 : 3380902 : int needed = 0;
2576 : 3380902 : varpool_node *current = NULL, *last = NULL;
2577 : :
2578 : : /* If the vtables for this class have already been emitted there is
2579 : : nothing more to do. */
2580 : 3380902 : primary_vtbl = CLASSTYPE_VTABLES (ctype);
2581 : 3380902 : if (var_finalized_p (primary_vtbl))
2582 : : return false;
2583 : : /* Ignore dummy vtables made by get_vtable_decl. */
2584 : 3380902 : if (TREE_TYPE (primary_vtbl) == void_type_node)
2585 : : return false;
2586 : :
2587 : : /* On some targets, we cannot determine the key method until the end
2588 : : of the translation unit -- which is when this function is
2589 : : called. */
2590 : 3380902 : if (!targetm.cxx.key_method_may_be_inline ())
2591 : 0 : determine_key_method (ctype);
2592 : :
2593 : : /* See if any of the vtables are needed. */
2594 : 8429177 : for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2595 : : {
2596 : 5048275 : import_export_decl (vtbl);
2597 : 5048275 : if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2598 : : needed = 1;
2599 : : }
2600 : 3380902 : if (!needed)
2601 : : {
2602 : : /* If the references to this class' vtables are optimized away,
2603 : : still emit the appropriate debugging information. See
2604 : : dfs_debug_mark. */
2605 : 3111618 : if (DECL_COMDAT (primary_vtbl)
2606 : 3111618 : && CLASSTYPE_DEBUG_REQUESTED (ctype))
2607 : 38828 : note_debug_info_needed (ctype);
2608 : 3111618 : return false;
2609 : : }
2610 : :
2611 : : /* The ABI requires that we emit all of the vtables if we emit any
2612 : : of them. */
2613 : 557997 : for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2614 : : {
2615 : : /* Mark entities references from the virtual table as used. */
2616 : 288713 : mark_vtable_entries (vtbl, consteval_vtables);
2617 : :
2618 : 288713 : if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2619 : : {
2620 : 0 : vec<tree, va_gc> *cleanups = NULL;
2621 : 0 : tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2622 : : LOOKUP_NORMAL);
2623 : :
2624 : : /* It had better be all done at compile-time. */
2625 : 0 : gcc_assert (!expr && !cleanups);
2626 : : }
2627 : :
2628 : : /* Write it out. */
2629 : 288713 : DECL_EXTERNAL (vtbl) = 0;
2630 : 288713 : rest_of_decl_compilation (vtbl, 1, 1);
2631 : :
2632 : : /* Because we're only doing syntax-checking, we'll never end up
2633 : : actually marking the variable as written. */
2634 : 288713 : if (flag_syntax_only)
2635 : 33 : TREE_ASM_WRITTEN (vtbl) = 1;
2636 : 288680 : else if (DECL_ONE_ONLY (vtbl))
2637 : : {
2638 : 287950 : current = varpool_node::get_create (vtbl);
2639 : 287950 : if (last)
2640 : 19353 : current->add_to_same_comdat_group (last);
2641 : : last = current;
2642 : : }
2643 : : }
2644 : :
2645 : : /* For abstract classes, the destructor has been removed from the
2646 : : vtable (in class.cc's build_vtbl_initializer). For a compiler-
2647 : : generated destructor, it hence might not have been generated in
2648 : : this translation unit - and with '#pragma interface' it might
2649 : : never get generated. */
2650 : 269284 : if (CLASSTYPE_PURE_VIRTUALS (ctype)
2651 : 68379 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype)
2652 : 54760 : && !CLASSTYPE_LAZY_DESTRUCTOR (ctype)
2653 : 324041 : && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype)))
2654 : 58 : note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype));
2655 : :
2656 : : /* Since we're writing out the vtable here, also write the debug
2657 : : info. */
2658 : 269284 : note_debug_info_needed (ctype);
2659 : :
2660 : 269284 : return true;
2661 : : }
2662 : :
2663 : : /* A special return value from type_visibility meaning internal
2664 : : linkage. */
2665 : :
2666 : : enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2667 : :
2668 : : static int expr_visibility (tree);
2669 : : static int type_visibility (tree);
2670 : :
2671 : : /* walk_tree helper function for type_visibility. */
2672 : :
2673 : : static tree
2674 : 2498407137 : min_vis_r (tree *tp, int *walk_subtrees, void *data)
2675 : : {
2676 : 2498407137 : int *vis_p = (int *)data;
2677 : 2498407137 : int this_vis = VISIBILITY_DEFAULT;
2678 : 2498407137 : if (! TYPE_P (*tp))
2679 : 36394001 : *walk_subtrees = 0;
2680 : 2462013136 : else if (OVERLOAD_TYPE_P (*tp)
2681 : 3232433034 : && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2682 : : {
2683 : 1012086 : this_vis = VISIBILITY_ANON;
2684 : 1012086 : *walk_subtrees = 0;
2685 : : }
2686 : 2461001050 : else if (CLASS_TYPE_P (*tp))
2687 : : {
2688 : 749801301 : this_vis = CLASSTYPE_VISIBILITY (*tp);
2689 : 749801301 : *walk_subtrees = 0;
2690 : : }
2691 : 1711199749 : else if (TREE_CODE (*tp) == ARRAY_TYPE
2692 : 1711199749 : && uses_template_parms (TYPE_DOMAIN (*tp)))
2693 : 281211 : this_vis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp)));
2694 : :
2695 : 2498407137 : if (this_vis > *vis_p)
2696 : 1657232 : *vis_p = this_vis;
2697 : :
2698 : : /* Tell cp_walk_subtrees to look through typedefs. */
2699 : 2498407137 : if (*walk_subtrees == 1)
2700 : 1711199749 : *walk_subtrees = 2;
2701 : :
2702 : 2498407137 : return NULL;
2703 : : }
2704 : :
2705 : : /* walk_tree helper function for expr_visibility. */
2706 : :
2707 : : static tree
2708 : 244241531 : min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
2709 : : {
2710 : 244241531 : int *vis_p = (int *)data;
2711 : 244241531 : int tpvis = VISIBILITY_DEFAULT;
2712 : :
2713 : 244241531 : tree t = *tp;
2714 : 244241531 : if (TREE_CODE (t) == PTRMEM_CST)
2715 : 577 : t = PTRMEM_CST_MEMBER (t);
2716 : 244241531 : switch (TREE_CODE (t))
2717 : : {
2718 : 3296495 : case CAST_EXPR:
2719 : 3296495 : case IMPLICIT_CONV_EXPR:
2720 : 3296495 : case STATIC_CAST_EXPR:
2721 : 3296495 : case REINTERPRET_CAST_EXPR:
2722 : 3296495 : case CONST_CAST_EXPR:
2723 : 3296495 : case DYNAMIC_CAST_EXPR:
2724 : 3296495 : case NEW_EXPR:
2725 : 3296495 : case CONSTRUCTOR:
2726 : 3296495 : case LAMBDA_EXPR:
2727 : 3296495 : tpvis = type_visibility (TREE_TYPE (t));
2728 : 3296495 : break;
2729 : :
2730 : 6675970 : case TEMPLATE_DECL:
2731 : 6675970 : if (DECL_ALIAS_TEMPLATE_P (t) || concept_definition_p (t))
2732 : : /* FIXME: We don't maintain TREE_PUBLIC / DECL_VISIBILITY for
2733 : : alias templates so we can't trust it here (PR107906). Ditto
2734 : : for concepts. */
2735 : : break;
2736 : 6374688 : t = DECL_TEMPLATE_RESULT (t);
2737 : : /* Fall through. */
2738 : 11008185 : case VAR_DECL:
2739 : 11008185 : case FUNCTION_DECL:
2740 : 11008185 : if (decl_constant_var_p (t))
2741 : : /* The ODR allows definitions in different TUs to refer to distinct
2742 : : constant variables with internal or no linkage, so such a reference
2743 : : shouldn't affect visibility (PR110323). FIXME but only if the
2744 : : lvalue-rvalue conversion is applied. */;
2745 : 7057068 : else if (! TREE_PUBLIC (t))
2746 : : tpvis = VISIBILITY_ANON;
2747 : : else
2748 : 7040065 : tpvis = DECL_VISIBILITY (t);
2749 : : break;
2750 : :
2751 : 512 : case FIELD_DECL:
2752 : 512 : tpvis = type_visibility (DECL_CONTEXT (t));
2753 : 512 : break;
2754 : :
2755 : : default:
2756 : : break;
2757 : : }
2758 : :
2759 : 244241531 : if (tpvis > *vis_p)
2760 : 17506 : *vis_p = tpvis;
2761 : :
2762 : 244241531 : return NULL_TREE;
2763 : : }
2764 : :
2765 : : /* Returns the visibility of TYPE, which is the minimum visibility of its
2766 : : component types. */
2767 : :
2768 : : static int
2769 : 842985880 : type_visibility (tree type)
2770 : : {
2771 : 842985880 : int vis = VISIBILITY_DEFAULT;
2772 : 842985880 : cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2773 : 842985880 : return vis;
2774 : : }
2775 : :
2776 : : /* Returns the visibility of an expression EXPR that appears in the signature
2777 : : of a function template, which is the minimum visibility of names that appear
2778 : : in its mangling. */
2779 : :
2780 : : static int
2781 : 64321535 : expr_visibility (tree expr)
2782 : : {
2783 : 64321535 : int vis = VISIBILITY_DEFAULT;
2784 : 64321535 : cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis);
2785 : 64321535 : return vis;
2786 : : }
2787 : :
2788 : : /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2789 : : specified (or if VISIBILITY is static). If TMPL is true, this
2790 : : constraint is for a template argument, and takes precedence
2791 : : over explicitly-specified visibility on the template. */
2792 : :
2793 : : static void
2794 : 54099190 : constrain_visibility (tree decl, int visibility, bool tmpl)
2795 : : {
2796 : 54099190 : if (visibility == VISIBILITY_ANON)
2797 : : {
2798 : : /* extern "C" declarations aren't affected by the anonymous
2799 : : namespace. */
2800 : 1443514 : if (!DECL_EXTERN_C_P (decl))
2801 : : {
2802 : 1442640 : TREE_PUBLIC (decl) = 0;
2803 : 1442640 : DECL_WEAK (decl) = 0;
2804 : 1442640 : DECL_COMMON (decl) = 0;
2805 : 1442640 : DECL_COMDAT (decl) = false;
2806 : 1442640 : if (VAR_OR_FUNCTION_DECL_P (decl))
2807 : : {
2808 : 459729 : struct symtab_node *snode = symtab_node::get (decl);
2809 : :
2810 : 459729 : if (snode)
2811 : 31535 : snode->set_comdat_group (NULL);
2812 : : }
2813 : 1442640 : DECL_INTERFACE_KNOWN (decl) = 1;
2814 : 1442640 : if (DECL_LANG_SPECIFIC (decl))
2815 : 462945 : DECL_NOT_REALLY_EXTERN (decl) = 1;
2816 : : }
2817 : : }
2818 : 52655676 : else if (visibility > DECL_VISIBILITY (decl)
2819 : 52655676 : && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2820 : : {
2821 : 303 : DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2822 : : /* This visibility was not specified. */
2823 : 303 : DECL_VISIBILITY_SPECIFIED (decl) = false;
2824 : : }
2825 : 54099190 : }
2826 : :
2827 : : /* Constrain the visibility of DECL based on the visibility of its template
2828 : : arguments. */
2829 : :
2830 : : static void
2831 : 254616498 : constrain_visibility_for_template (tree decl, tree targs)
2832 : : {
2833 : : /* If this is a template instantiation, check the innermost
2834 : : template args for visibility constraints. The outer template
2835 : : args are covered by the class check. */
2836 : 254616498 : tree args = INNERMOST_TEMPLATE_ARGS (targs);
2837 : 254616498 : int i;
2838 : 771511016 : for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2839 : : {
2840 : 516894518 : int vis = 0;
2841 : :
2842 : 516894518 : tree arg = TREE_VEC_ELT (args, i-1);
2843 : 516894518 : if (TYPE_P (arg))
2844 : 453584755 : vis = type_visibility (arg);
2845 : : else
2846 : 63309763 : vis = expr_visibility (arg);
2847 : 516894518 : if (vis)
2848 : 404561 : constrain_visibility (decl, vis, true);
2849 : : }
2850 : 254616498 : }
2851 : :
2852 : : /* Like c_determine_visibility, but with additional C++-specific
2853 : : behavior.
2854 : :
2855 : : Function-scope entities can rely on the function's visibility because
2856 : : it is set in start_preparsed_function.
2857 : :
2858 : : Class-scope entities cannot rely on the class's visibility until the end
2859 : : of the enclosing class definition.
2860 : :
2861 : : Note that because namespaces have multiple independent definitions,
2862 : : namespace visibility is handled elsewhere using the #pragma visibility
2863 : : machinery rather than by decorating the namespace declaration.
2864 : :
2865 : : The goal is for constraints from the type to give a diagnostic, and
2866 : : other constraints to be applied silently. */
2867 : :
2868 : : void
2869 : 525896986 : determine_visibility (tree decl)
2870 : : {
2871 : : /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2872 : :
2873 : : /* Only relevant for names with external linkage. */
2874 : 525896986 : if (!TREE_PUBLIC (decl))
2875 : : return;
2876 : :
2877 : : /* Cloned constructors and destructors get the same visibility as
2878 : : the underlying function. That should be set up in
2879 : : maybe_clone_body. */
2880 : 482586523 : gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2881 : :
2882 : 482586523 : bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2883 : 482586523 : enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2884 : :
2885 : : /* The decl may be a template instantiation, which could influence
2886 : : visibilty. */
2887 : 482586523 : tree template_decl = NULL_TREE;
2888 : 482586523 : if (TREE_CODE (decl) == TYPE_DECL)
2889 : : {
2890 : 137432305 : if (CLASS_TYPE_P (TREE_TYPE (decl)))
2891 : : {
2892 : 135201580 : if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2893 : 259682040 : template_decl = decl;
2894 : : }
2895 : 2230725 : else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2896 : 259682040 : template_decl = decl;
2897 : : }
2898 : 345154218 : else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2899 : : template_decl = decl;
2900 : :
2901 : 482586523 : if (TREE_CODE (decl) == TYPE_DECL
2902 : 271756491 : && LAMBDA_TYPE_P (TREE_TYPE (decl))
2903 : 484059167 : && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl)) != error_mark_node)
2904 : 733587 : if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
2905 : : {
2906 : : /* The lambda's visibility is limited by that of its extra
2907 : : scope. */
2908 : 730561 : int vis = 0;
2909 : 730561 : if (TYPE_P (extra))
2910 : 0 : vis = type_visibility (extra);
2911 : : else
2912 : 730561 : vis = expr_visibility (extra);
2913 : 730561 : constrain_visibility (decl, vis, false);
2914 : : }
2915 : :
2916 : : /* If DECL is a member of a class, visibility specifiers on the
2917 : : class can influence the visibility of the DECL. */
2918 : 482586523 : tree class_type = NULL_TREE;
2919 : 482586523 : if (DECL_CLASS_SCOPE_P (decl))
2920 : 253070767 : class_type = DECL_CONTEXT (decl);
2921 : : else
2922 : : {
2923 : : /* Not a class member. */
2924 : :
2925 : : /* Virtual tables have DECL_CONTEXT set to their associated class,
2926 : : so they are automatically handled above. */
2927 : 229515756 : gcc_assert (!VAR_P (decl)
2928 : : || !DECL_VTABLE_OR_VTT_P (decl));
2929 : :
2930 : 229515756 : if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2931 : : {
2932 : : /* Local statics and classes get the visibility of their
2933 : : containing function by default, except that
2934 : : -fvisibility-inlines-hidden doesn't affect them. */
2935 : 1561837 : tree fn = DECL_CONTEXT (decl);
2936 : 1561837 : if (DECL_VISIBILITY_SPECIFIED (fn))
2937 : : {
2938 : 1510556 : DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2939 : 1510556 : DECL_VISIBILITY_SPECIFIED (decl) =
2940 : 1510556 : DECL_VISIBILITY_SPECIFIED (fn);
2941 : : }
2942 : : else
2943 : : {
2944 : 51281 : if (DECL_CLASS_SCOPE_P (fn))
2945 : 30324 : determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2946 : 20957 : else if (determine_hidden_inline (fn))
2947 : : {
2948 : 6 : DECL_VISIBILITY (decl) = default_visibility;
2949 : 6 : DECL_VISIBILITY_SPECIFIED (decl) =
2950 : 6 : visibility_options.inpragma;
2951 : : }
2952 : : else
2953 : : {
2954 : 20951 : DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2955 : 20951 : DECL_VISIBILITY_SPECIFIED (decl) =
2956 : 20951 : DECL_VISIBILITY_SPECIFIED (fn);
2957 : : }
2958 : : }
2959 : :
2960 : : /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2961 : : but have no TEMPLATE_INFO, so don't try to check it. */
2962 : : template_decl = NULL_TREE;
2963 : : }
2964 : 16396997 : else if (VAR_P (decl) && DECL_TINFO_P (decl)
2965 : 232472414 : && flag_visibility_ms_compat)
2966 : : {
2967 : : /* Under -fvisibility-ms-compat, types are visible by default,
2968 : : even though their contents aren't. */
2969 : 84 : tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2970 : 84 : int underlying_vis = type_visibility (underlying_type);
2971 : 84 : if (underlying_vis == VISIBILITY_ANON
2972 : 84 : || (CLASS_TYPE_P (underlying_type)
2973 : 69 : && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2974 : 54 : constrain_visibility (decl, underlying_vis, false);
2975 : : else
2976 : 30 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2977 : : }
2978 : 227953835 : else if (VAR_P (decl) && DECL_TINFO_P (decl))
2979 : : {
2980 : : /* tinfo visibility is based on the type it's for. */
2981 : 4518411 : constrain_visibility
2982 : 4518411 : (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2983 : :
2984 : : /* Give the target a chance to override the visibility associated
2985 : : with DECL. */
2986 : 4518411 : if (TREE_PUBLIC (decl)
2987 : 4516395 : && !DECL_REALLY_EXTERN (decl)
2988 : 4516395 : && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2989 : 9004532 : && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2990 : 100627 : targetm.cxx.determine_class_data_visibility (decl);
2991 : : }
2992 : 223435424 : else if (template_decl)
2993 : : /* Template instantiations and specializations get visibility based
2994 : : on their template unless they override it with an attribute. */;
2995 : 82045451 : else if (! DECL_VISIBILITY_SPECIFIED (decl))
2996 : : {
2997 : 76362548 : if (determine_hidden_inline (decl))
2998 : 12 : DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2999 : : else
3000 : : {
3001 : : /* Set default visibility to whatever the user supplied with
3002 : : #pragma GCC visibility or a namespace visibility attribute. */
3003 : 76362536 : DECL_VISIBILITY (decl) = default_visibility;
3004 : 76362536 : DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
3005 : : }
3006 : : }
3007 : : }
3008 : :
3009 : 339634713 : if (template_decl)
3010 : : {
3011 : : /* If the specialization doesn't specify visibility, use the
3012 : : visibility from the template. */
3013 : 259138256 : tree tinfo = get_template_info (template_decl);
3014 : 259138256 : tree args = TI_ARGS (tinfo);
3015 : 259138256 : tree attribs = (TREE_CODE (decl) == TYPE_DECL
3016 : 400559568 : ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
3017 : 141421312 : : DECL_ATTRIBUTES (decl));
3018 : 259138256 : tree attr = lookup_attribute ("visibility", attribs);
3019 : :
3020 : 259138256 : if (args != error_mark_node)
3021 : : {
3022 : 259138256 : tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
3023 : :
3024 : 259138256 : if (!DECL_VISIBILITY_SPECIFIED (decl))
3025 : : {
3026 : 117414096 : if (!attr
3027 : 117414096 : && determine_hidden_inline (decl))
3028 : 33 : DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3029 : : else
3030 : : {
3031 : 117414063 : DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
3032 : 234828126 : DECL_VISIBILITY_SPECIFIED (decl)
3033 : 117414063 : = DECL_VISIBILITY_SPECIFIED (pattern);
3034 : : }
3035 : : }
3036 : :
3037 : 259138256 : if (args
3038 : : /* Template argument visibility outweighs #pragma or namespace
3039 : : visibility, but not an explicit attribute. */
3040 : 259138256 : && !attr)
3041 : : {
3042 : 259138235 : int depth = TMPL_ARGS_DEPTH (args);
3043 : 259138235 : if (DECL_VISIBILITY_SPECIFIED (decl))
3044 : : {
3045 : : /* A class template member with explicit visibility
3046 : : overrides the class visibility, so we need to apply
3047 : : all the levels of template args directly. */
3048 : : int i;
3049 : 473005669 : for (i = 1; i <= depth; ++i)
3050 : : {
3051 : 240140453 : tree lev = TMPL_ARGS_LEVEL (args, i);
3052 : 240140453 : constrain_visibility_for_template (decl, lev);
3053 : : }
3054 : : }
3055 : 26273019 : else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
3056 : : /* Limit visibility based on its template arguments. */
3057 : 14476045 : constrain_visibility_for_template (decl, args);
3058 : : }
3059 : : }
3060 : : }
3061 : :
3062 : 482586523 : if (class_type)
3063 : 253070767 : determine_visibility_from_class (decl, class_type);
3064 : :
3065 : 482586523 : if (decl_internal_context_p (decl))
3066 : : /* Names in an anonymous namespace get internal linkage. */
3067 : 302075 : constrain_visibility (decl, VISIBILITY_ANON, false);
3068 : 482284448 : else if (TREE_CODE (decl) != TYPE_DECL)
3069 : : {
3070 : : /* Propagate anonymity from type to decl. */
3071 : 344987846 : int tvis = type_visibility (TREE_TYPE (decl));
3072 : 344987846 : if (tvis == VISIBILITY_ANON
3073 : 344987846 : || ! DECL_VISIBILITY_SPECIFIED (decl))
3074 : 47413004 : constrain_visibility (decl, tvis, false);
3075 : : }
3076 : 137296602 : else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
3077 : : /* DR 757: A type without linkage shall not be used as the type of a
3078 : : variable or function with linkage, unless
3079 : : o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
3080 : : o the variable or function is not used (3.2 [basic.def.odr]) or is
3081 : : defined in the same translation unit.
3082 : :
3083 : : Since non-extern "C" decls need to be defined in the same
3084 : : translation unit, we can make the type internal. */
3085 : 730524 : constrain_visibility (decl, VISIBILITY_ANON, false);
3086 : :
3087 : : /* If visibility changed and DECL already has DECL_RTL, ensure
3088 : : symbol flags are updated. */
3089 : 482586523 : if ((DECL_VISIBILITY (decl) != orig_visibility
3090 : 481627112 : || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
3091 : 263950954 : && ((VAR_P (decl) && TREE_STATIC (decl))
3092 : 249466699 : || TREE_CODE (decl) == FUNCTION_DECL)
3093 : 729509647 : && DECL_RTL_SET_P (decl))
3094 : 0 : make_decl_rtl (decl);
3095 : : }
3096 : :
3097 : : /* By default, static data members and function members receive
3098 : : the visibility of their containing class. */
3099 : :
3100 : : static void
3101 : 253101091 : determine_visibility_from_class (tree decl, tree class_type)
3102 : : {
3103 : 253101091 : if (DECL_VISIBILITY_SPECIFIED (decl))
3104 : : return;
3105 : :
3106 : 127884428 : if (determine_hidden_inline (decl))
3107 : 135 : DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3108 : : else
3109 : : {
3110 : : /* Default to the class visibility. */
3111 : 127884293 : DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
3112 : 255768586 : DECL_VISIBILITY_SPECIFIED (decl)
3113 : 127884293 : = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
3114 : : }
3115 : :
3116 : : /* Give the target a chance to override the visibility associated
3117 : : with DECL. */
3118 : 127884428 : if (VAR_P (decl)
3119 : 127884428 : && TREE_PUBLIC (decl)
3120 : 24296078 : && (DECL_TINFO_P (decl) || DECL_VTABLE_OR_VTT_P (decl))
3121 : 2135179 : && !DECL_REALLY_EXTERN (decl)
3122 : 130019607 : && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
3123 : 72084 : targetm.cxx.determine_class_data_visibility (decl);
3124 : : }
3125 : :
3126 : : /* Returns true iff DECL is an inline that should get hidden visibility
3127 : : because of -fvisibility-inlines-hidden. */
3128 : :
3129 : : static bool
3130 : 321682029 : determine_hidden_inline (tree decl)
3131 : : {
3132 : 321682029 : return (visibility_options.inlines_hidden
3133 : : /* Don't do this for inline templates; specializations might not be
3134 : : inline, and we don't want them to inherit the hidden
3135 : : visibility. We'll set it here for all inline instantiations. */
3136 : 406 : && !processing_template_decl
3137 : 382 : && TREE_CODE (decl) == FUNCTION_DECL
3138 : 293 : && DECL_DECLARED_INLINE_P (decl)
3139 : 321682230 : && (! DECL_LANG_SPECIFIC (decl)
3140 : 201 : || ! DECL_EXPLICIT_INSTANTIATION (decl)));
3141 : : }
3142 : :
3143 : : /* Constrain the visibility of a class TYPE based on the visibility of its
3144 : : field types. Warn if any fields require lesser visibility. */
3145 : :
3146 : : void
3147 : 36027736 : constrain_class_visibility (tree type)
3148 : : {
3149 : 36027736 : tree binfo;
3150 : 36027736 : tree t;
3151 : 36027736 : int i;
3152 : :
3153 : 36027736 : int vis = type_visibility (type);
3154 : :
3155 : 36027736 : if (vis == VISIBILITY_ANON
3156 : 36027736 : || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
3157 : 35622399 : return;
3158 : :
3159 : : /* Don't warn about visibility if the class has explicit visibility. */
3160 : 405337 : if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
3161 : 47013 : vis = VISIBILITY_INTERNAL;
3162 : :
3163 : 3294669 : for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
3164 : 580047 : if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
3165 : 3469225 : && !DECL_ARTIFICIAL (t))
3166 : : {
3167 : 502712 : tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
3168 : 502712 : int subvis = type_visibility (ftype);
3169 : :
3170 : 502712 : if (subvis == VISIBILITY_ANON)
3171 : : {
3172 : 111 : if (!in_main_input_context())
3173 : : {
3174 : 35 : tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
3175 : 35 : if (nlt)
3176 : : {
3177 : 14 : if (same_type_p (TREE_TYPE (t), nlt))
3178 : 14 : warning (OPT_Wsubobject_linkage, "\
3179 : : %qT has a field %q#D whose type has no linkage",
3180 : : type, t);
3181 : : else
3182 : 0 : warning (OPT_Wsubobject_linkage, "\
3183 : : %qT has a field %qD whose type depends on the type %qT which has no linkage",
3184 : : type, t, nlt);
3185 : : }
3186 : 21 : else if (cxx_dialect > cxx98
3187 : 21 : && !decl_anon_ns_mem_p (ftype))
3188 : 2 : warning (OPT_Wsubobject_linkage, "\
3189 : : %qT has a field %q#D whose type has internal linkage",
3190 : : type, t);
3191 : : else // In C++98 this can only happen with unnamed namespaces.
3192 : 19 : warning (OPT_Wsubobject_linkage, "\
3193 : : %qT has a field %q#D whose type uses the anonymous namespace",
3194 : : type, t);
3195 : : }
3196 : : }
3197 : 502601 : else if (MAYBE_CLASS_TYPE_P (ftype)
3198 : 109316 : && vis < VISIBILITY_HIDDEN
3199 : 109316 : && subvis >= VISIBILITY_HIDDEN)
3200 : 6 : warning (OPT_Wattributes, "\
3201 : : %qT declared with greater visibility than the type of its field %qD",
3202 : : type, t);
3203 : : }
3204 : :
3205 : 405337 : binfo = TYPE_BINFO (type);
3206 : 472666 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
3207 : : {
3208 : 67329 : tree btype = BINFO_TYPE (t);
3209 : 67329 : int subvis = type_visibility (btype);
3210 : :
3211 : 67329 : if (subvis == VISIBILITY_ANON)
3212 : : {
3213 : 33 : if (!in_main_input_context())
3214 : : {
3215 : 18 : tree nlt = no_linkage_check (btype, /*relaxed_p=*/false);
3216 : 18 : if (nlt)
3217 : : {
3218 : 6 : if (same_type_p (btype, nlt))
3219 : 6 : warning (OPT_Wsubobject_linkage, "\
3220 : : %qT has a base %qT which has no linkage",
3221 : : type, btype);
3222 : : else
3223 : 0 : warning (OPT_Wsubobject_linkage, "\
3224 : : %qT has a base %qT which depends on the type %qT which has no linkage",
3225 : : type, btype, nlt);
3226 : : }
3227 : 12 : else if (cxx_dialect > cxx98
3228 : 12 : && !decl_anon_ns_mem_p (btype))
3229 : 3 : warning (OPT_Wsubobject_linkage, "\
3230 : : %qT has a base %qT which has internal linkage",
3231 : : type, btype);
3232 : : else // In C++98 this can only happen with unnamed namespaces.
3233 : 9 : warning (OPT_Wsubobject_linkage, "\
3234 : : %qT has a base %qT which uses the anonymous namespace",
3235 : : type, btype);
3236 : : }
3237 : : }
3238 : 67296 : else if (vis < VISIBILITY_HIDDEN
3239 : 67296 : && subvis >= VISIBILITY_HIDDEN)
3240 : 6 : warning (OPT_Wattributes, "\
3241 : : %qT declared with greater visibility than its base %qT",
3242 : 6 : type, TREE_TYPE (t));
3243 : : }
3244 : : }
3245 : :
3246 : : /* Functions for adjusting the visibility of a tagged type and its nested
3247 : : types and declarations when it gets a name for linkage purposes from a
3248 : : typedef. */
3249 : : // FIXME: It is now a DR for such a class type to contain anything
3250 : : // other than C. So at minium most of this can probably be deleted.
3251 : :
3252 : : /* First reset the visibility of all the types. */
3253 : :
3254 : : static void
3255 : 736064 : reset_type_linkage_1 (tree type)
3256 : : {
3257 : 736064 : set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
3258 : 736064 : if (CLASS_TYPE_P (type))
3259 : 3619338 : for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
3260 : 2884850 : if (DECL_IMPLICIT_TYPEDEF_P (member))
3261 : 237236 : reset_type_linkage_1 (TREE_TYPE (member));
3262 : 736064 : }
3263 : :
3264 : : /* Then reset the visibility of any static data members or member
3265 : : functions that use those types. */
3266 : :
3267 : : static void
3268 : 75 : reset_decl_linkage (tree decl)
3269 : : {
3270 : 75 : if (TREE_PUBLIC (decl))
3271 : : return;
3272 : 75 : if (DECL_CLONED_FUNCTION_P (decl))
3273 : : return;
3274 : 75 : TREE_PUBLIC (decl) = true;
3275 : 75 : DECL_INTERFACE_KNOWN (decl) = false;
3276 : 75 : determine_visibility (decl);
3277 : 75 : tentative_decl_linkage (decl);
3278 : : }
3279 : :
3280 : : void
3281 : 498828 : reset_type_linkage (tree type)
3282 : : {
3283 : 498828 : reset_type_linkage_1 (type);
3284 : 498828 : if (CLASS_TYPE_P (type))
3285 : : {
3286 : 497261 : if (tree vt = CLASSTYPE_VTABLES (type))
3287 : : {
3288 : 15 : tree name = mangle_vtbl_for_type (type);
3289 : 15 : DECL_NAME (vt) = name;
3290 : 15 : SET_DECL_ASSEMBLER_NAME (vt, name);
3291 : 15 : reset_decl_linkage (vt);
3292 : : }
3293 : 497261 : if (!ANON_AGGR_TYPE_P (type))
3294 : 497123 : if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
3295 : : {
3296 : 12 : tree name = mangle_typeinfo_for_type (type);
3297 : 12 : DECL_NAME (ti) = name;
3298 : 12 : SET_DECL_ASSEMBLER_NAME (ti, name);
3299 : 12 : TREE_TYPE (name) = type;
3300 : 12 : reset_decl_linkage (ti);
3301 : : }
3302 : 2368494 : for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
3303 : : {
3304 : 1871233 : tree mem = STRIP_TEMPLATE (m);
3305 : 1871233 : if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
3306 : 48 : reset_decl_linkage (mem);
3307 : 1871185 : else if (DECL_IMPLICIT_TYPEDEF_P (mem))
3308 : 123944 : reset_type_linkage (TREE_TYPE (mem));
3309 : : }
3310 : : }
3311 : 498828 : }
3312 : :
3313 : : /* Set up our initial idea of what the linkage of DECL should be. */
3314 : :
3315 : : void
3316 : 22018491 : tentative_decl_linkage (tree decl)
3317 : : {
3318 : 22018491 : if (DECL_INTERFACE_KNOWN (decl))
3319 : : /* We've already made a decision as to how this function will
3320 : : be handled. */;
3321 : 22018491 : else if (vague_linkage_p (decl))
3322 : : {
3323 : 22018462 : if (TREE_CODE (decl) == FUNCTION_DECL
3324 : 22018462 : && decl_defined_p (decl))
3325 : : {
3326 : 22018452 : DECL_EXTERNAL (decl) = 1;
3327 : 22018452 : DECL_NOT_REALLY_EXTERN (decl) = 1;
3328 : 22018452 : note_vague_linkage_fn (decl);
3329 : : /* A non-template inline function with external linkage will
3330 : : always be COMDAT. As we must eventually determine the
3331 : : linkage of all functions, and as that causes writes to
3332 : : the data mapped in from the PCH file, it's advantageous
3333 : : to mark the functions at this point. */
3334 : 22018452 : if (DECL_DECLARED_INLINE_P (decl))
3335 : : {
3336 : 22012989 : if (!DECL_IMPLICIT_INSTANTIATION (decl)
3337 : 22012989 : || DECL_DEFAULTED_FN (decl))
3338 : : {
3339 : : /* This function must have external linkage, as
3340 : : otherwise DECL_INTERFACE_KNOWN would have been
3341 : : set. */
3342 : 19704635 : gcc_assert (TREE_PUBLIC (decl));
3343 : 19704635 : comdat_linkage (decl);
3344 : 19704635 : DECL_INTERFACE_KNOWN (decl) = 1;
3345 : : }
3346 : 2308354 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
3347 : : /* For implicit instantiations of cdtors try to make
3348 : : it comdat, so that maybe_clone_body can use aliases.
3349 : : See PR113208. */
3350 : 180285 : maybe_make_one_only (decl);
3351 : : }
3352 : : }
3353 : 10 : else if (VAR_P (decl))
3354 : 10 : maybe_commonize_var (decl);
3355 : : }
3356 : 22018491 : }
3357 : :
3358 : : /* For a polymorphic class type CTYPE, whether its vtables are emitted in a
3359 : : unique object as per the ABI. */
3360 : :
3361 : : static bool
3362 : 2794 : vtables_uniquely_emitted (tree ctype)
3363 : : {
3364 : : /* If the class is templated, the tables are emitted in every object that
3365 : : references any of them. */
3366 : 2794 : if (CLASSTYPE_USE_TEMPLATE (ctype))
3367 : : return false;
3368 : :
3369 : : /* Otherwise, if the class is attached to a module, the tables are uniquely
3370 : : emitted in the object for the module unit in which it is defined. */
3371 : 2168 : tree cdecl = TYPE_NAME (ctype);
3372 : 2267 : if (DECL_LANG_SPECIFIC (cdecl) && DECL_MODULE_ATTACH_P (cdecl))
3373 : : return true;
3374 : :
3375 : : /* Otherwise, if the class has a key function, the tables are emitted in the
3376 : : object for the TU containing the definition of the key function. This is
3377 : : unique if the key function is not inline. */
3378 : 2072 : tree key_method = CLASSTYPE_KEY_METHOD (ctype);
3379 : 4124 : if (key_method && !DECL_DECLARED_INLINE_P (key_method))
3380 : : return true;
3381 : :
3382 : : /* Otherwise, the tables are emitted in every object that references
3383 : : any of them. */
3384 : : return false;
3385 : : }
3386 : :
3387 : : /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
3388 : : for DECL has not already been determined, do so now by setting
3389 : : DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
3390 : : function is called entities with vague linkage whose definitions
3391 : : are available must have TREE_PUBLIC set.
3392 : :
3393 : : If this function decides to place DECL in COMDAT, it will set
3394 : : appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
3395 : : the caller to decide whether or not to clear DECL_EXTERNAL. Some
3396 : : callers defer that decision until it is clear that DECL is actually
3397 : : required. */
3398 : :
3399 : : void
3400 : 158248060 : import_export_decl (tree decl)
3401 : : {
3402 : 158248060 : bool comdat_p;
3403 : 158248060 : bool import_p;
3404 : 158248060 : tree class_type = NULL_TREE;
3405 : :
3406 : 158248060 : if (DECL_INTERFACE_KNOWN (decl))
3407 : : return;
3408 : :
3409 : : /* We cannot determine what linkage to give to an entity with vague
3410 : : linkage until the end of the file. For example, a virtual table
3411 : : for a class will be defined if and only if the key method is
3412 : : defined in this translation unit. */
3413 : 22993617 : gcc_assert (at_eof);
3414 : : /* Object file linkage for explicit instantiations is handled in
3415 : : mark_decl_instantiated. For static variables in functions with
3416 : : vague linkage, maybe_commonize_var is used.
3417 : :
3418 : : Therefore, the only declarations that should be provided to this
3419 : : function are those with external linkage that are:
3420 : :
3421 : : * implicit instantiations of function templates
3422 : :
3423 : : * inline functions
3424 : :
3425 : : * inline variables
3426 : :
3427 : : * implicit instantiations of static data members of class
3428 : : templates
3429 : :
3430 : : * virtual tables
3431 : :
3432 : : * typeinfo objects
3433 : :
3434 : : Furthermore, all entities that reach this point must have a
3435 : : definition available in this translation unit.
3436 : :
3437 : : The following assertions check these conditions. */
3438 : 22993617 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3439 : : /* Any code that creates entities with TREE_PUBLIC cleared should
3440 : : also set DECL_INTERFACE_KNOWN. */
3441 : 22993617 : gcc_assert (TREE_PUBLIC (decl));
3442 : 22993617 : if (TREE_CODE (decl) == FUNCTION_DECL)
3443 : 15205206 : gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3444 : : || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
3445 : : || DECL_DECLARED_INLINE_P (decl));
3446 : : else
3447 : 7788411 : gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3448 : : || DECL_INLINE_VAR_P (decl)
3449 : : || DECL_VTABLE_OR_VTT_P (decl)
3450 : : || DECL_TINFO_P (decl));
3451 : : /* Check that a definition of DECL is available in this translation
3452 : : unit. */
3453 : 22993617 : gcc_assert (!DECL_REALLY_EXTERN (decl));
3454 : :
3455 : : /* Assume that DECL will not have COMDAT linkage. */
3456 : 22993617 : comdat_p = false;
3457 : : /* Assume that DECL will not be imported into this translation
3458 : : unit. */
3459 : 22993617 : import_p = false;
3460 : :
3461 : 22993617 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
3462 : : {
3463 : 1507252 : class_type = DECL_CONTEXT (decl);
3464 : 1507252 : import_export_class (class_type);
3465 : 1507252 : if (CLASSTYPE_INTERFACE_KNOWN (class_type)
3466 : 1507252 : && CLASSTYPE_INTERFACE_ONLY (class_type))
3467 : : import_p = true;
3468 : 405025 : else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
3469 : 405025 : && !vtables_uniquely_emitted (class_type))
3470 : : /* The ABI historically required that all virtual tables be
3471 : : emitted with COMDAT linkage. However, on systems where
3472 : : COMDAT symbols don't show up in the table of contents for
3473 : : a static archive, or on systems without weak symbols (where
3474 : : we approximate COMDAT linkage by using internal linkage),
3475 : : the linker will report errors about undefined symbols because
3476 : : it will not see the virtual table definition. Therefore,
3477 : : in the case that we know that the virtual table will be
3478 : : emitted in only one translation unit, we make the virtual
3479 : : table an ordinary definition with external linkage. */
3480 : 3 : DECL_EXTERNAL (decl) = 0;
3481 : 405022 : else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
3482 : : {
3483 : : /* CLASS_TYPE is being exported from this translation unit,
3484 : : so DECL should be defined here. */
3485 : 2791 : if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
3486 : : /* If a class is declared in a header with the "extern
3487 : : template" extension, then it will not be instantiated,
3488 : : even in translation units that would normally require
3489 : : it. Often such classes are explicitly instantiated in
3490 : : one translation unit. Therefore, the explicit
3491 : : instantiation must be made visible to other translation
3492 : : units. */
3493 : 0 : DECL_EXTERNAL (decl) = 0;
3494 : : else
3495 : : {
3496 : : /* The generic C++ ABI used to say that class data is always
3497 : : COMDAT, even if emitted in a unique object. This is no
3498 : : longer true, but for now we continue to do so for
3499 : : compatibility with programs that are not strictly valid.
3500 : : However, some variants (e.g., the ARM EABI) explicitly say
3501 : : that class data only has COMDAT linkage if the class data
3502 : : might be emitted in more than one translation unit.
3503 : : When the key method can be inline and is inline, we still
3504 : : have to arrange for comdat even though
3505 : : class_data_always_comdat is false. */
3506 : 2791 : if (!vtables_uniquely_emitted (class_type)
3507 : 2791 : || targetm.cxx.class_data_always_comdat ())
3508 : : {
3509 : : /* The ABI requires COMDAT linkage. Normally, we
3510 : : only emit COMDAT things when they are needed;
3511 : : make sure that we realize that this entity is
3512 : : indeed needed. */
3513 : 2791 : comdat_p = true;
3514 : 2791 : mark_needed (decl);
3515 : : }
3516 : : }
3517 : : }
3518 : 402231 : else if (!flag_implicit_templates
3519 : 402231 : && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
3520 : : import_p = true;
3521 : : else
3522 : : comdat_p = true;
3523 : : }
3524 : 21486365 : else if (VAR_P (decl) && DECL_TINFO_P (decl))
3525 : : {
3526 : 2063936 : tree type = TREE_TYPE (DECL_NAME (decl));
3527 : 2063936 : if (CLASS_TYPE_P (type))
3528 : : {
3529 : 2061321 : class_type = type;
3530 : 2061321 : import_export_class (type);
3531 : 2061321 : if (CLASSTYPE_INTERFACE_KNOWN (type)
3532 : 1245203 : && TYPE_POLYMORPHIC_P (type)
3533 : 1245079 : && CLASSTYPE_INTERFACE_ONLY (type)
3534 : : /* If -fno-rtti was specified, then we cannot be sure
3535 : : that RTTI information will be emitted with the
3536 : : virtual table of the class, so we must emit it
3537 : : wherever it is used. */
3538 : 3302572 : && flag_rtti)
3539 : : import_p = true;
3540 : : else
3541 : : {
3542 : 820116 : if (CLASSTYPE_INTERFACE_KNOWN (type)
3543 : 820116 : && !CLASSTYPE_INTERFACE_ONLY (type))
3544 : : {
3545 : 3880 : comdat_p = (targetm.cxx.class_data_always_comdat ()
3546 : 3880 : || !vtables_uniquely_emitted (class_type));
3547 : 3880 : mark_needed (decl);
3548 : 3880 : if (!flag_weak)
3549 : : {
3550 : 0 : comdat_p = false;
3551 : 0 : DECL_EXTERNAL (decl) = 0;
3552 : : }
3553 : : }
3554 : : else
3555 : : comdat_p = true;
3556 : : }
3557 : : }
3558 : : else
3559 : : comdat_p = true;
3560 : : }
3561 : 19422429 : else if (DECL_TEMPLOID_INSTANTIATION (decl))
3562 : : {
3563 : : /* DECL is an implicit instantiation of a function or static
3564 : : data member. */
3565 : 19186956 : if (flag_implicit_templates
3566 : 19186956 : || (flag_implicit_inline_templates
3567 : 18277 : && TREE_CODE (decl) == FUNCTION_DECL
3568 : 15437 : && DECL_DECLARED_INLINE_P (decl)))
3569 : : comdat_p = true;
3570 : : else
3571 : : /* If we are not implicitly generating templates, then mark
3572 : : this entity as undefined in this translation unit. */
3573 : : import_p = true;
3574 : : }
3575 : 235473 : else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
3576 : : {
3577 : 235042 : if (!DECL_DECLARED_INLINE_P (decl))
3578 : : {
3579 : 0 : tree ctype = DECL_CONTEXT (decl);
3580 : 0 : import_export_class (ctype);
3581 : 0 : if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3582 : : {
3583 : 0 : DECL_NOT_REALLY_EXTERN (decl)
3584 : 0 : = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3585 : 0 : || (DECL_DECLARED_INLINE_P (decl)
3586 : 0 : && ! flag_implement_inlines
3587 : 0 : && !DECL_VINDEX (decl)));
3588 : :
3589 : 0 : if (!DECL_NOT_REALLY_EXTERN (decl))
3590 : 0 : DECL_EXTERNAL (decl) = 1;
3591 : :
3592 : : /* Always make artificials weak. */
3593 : 0 : if (DECL_ARTIFICIAL (decl) && flag_weak)
3594 : : comdat_p = true;
3595 : : else
3596 : 0 : maybe_make_one_only (decl);
3597 : : }
3598 : : }
3599 : : else
3600 : : comdat_p = true;
3601 : : }
3602 : : else
3603 : : comdat_p = true;
3604 : :
3605 : 2794 : if (import_p)
3606 : : {
3607 : : /* If we are importing DECL into this translation unit, mark is
3608 : : an undefined here. */
3609 : 2349655 : DECL_EXTERNAL (decl) = 1;
3610 : 2349655 : DECL_NOT_REALLY_EXTERN (decl) = 0;
3611 : : }
3612 : 20643962 : else if (comdat_p)
3613 : : {
3614 : : /* If we decided to put DECL in COMDAT, mark it accordingly at
3615 : : this point. */
3616 : 20643959 : comdat_linkage (decl);
3617 : : }
3618 : :
3619 : 22993617 : DECL_INTERFACE_KNOWN (decl) = 1;
3620 : : }
3621 : :
3622 : : /* Return an expression that performs the destruction of DECL, which
3623 : : must be a VAR_DECL whose type has a non-trivial destructor, or is
3624 : : an array whose (innermost) elements have a non-trivial destructor. */
3625 : :
3626 : : tree
3627 : 1296 : build_cleanup (tree decl)
3628 : : {
3629 : 1296 : tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3630 : 1296 : gcc_assert (clean != NULL_TREE);
3631 : 1296 : return clean;
3632 : : }
3633 : :
3634 : : /* GUARD is a helper variable for DECL; make them have the same linkage and
3635 : : visibility. */
3636 : :
3637 : : void
3638 : 35868 : copy_linkage (tree guard, tree decl)
3639 : : {
3640 : 35868 : TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3641 : 35868 : TREE_STATIC (guard) = TREE_STATIC (decl);
3642 : 35868 : DECL_COMMON (guard) = DECL_COMMON (decl);
3643 : 35868 : DECL_COMDAT (guard) = DECL_COMDAT (decl);
3644 : 35868 : if (TREE_STATIC (guard))
3645 : : {
3646 : 4796 : CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3647 : 4796 : set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3648 : 4796 : if (DECL_ONE_ONLY (decl))
3649 : 3496 : make_decl_one_only (guard, cxx_comdat_group (guard));
3650 : 4796 : if (TREE_PUBLIC (decl))
3651 : 3913 : DECL_WEAK (guard) = DECL_WEAK (decl);
3652 : : /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not
3653 : : be set until import_export_decl at EOF. */
3654 : 4796 : if (vague_linkage_p (decl))
3655 : 3514 : comdat_linkage (guard);
3656 : 4796 : DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3657 : 4796 : DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3658 : : }
3659 : 35868 : }
3660 : :
3661 : : /* Returns the initialization guard variable for the variable DECL,
3662 : : which has static storage duration. */
3663 : :
3664 : : tree
3665 : 4148 : get_guard (tree decl)
3666 : : {
3667 : 4148 : tree sname = mangle_guard_variable (decl);
3668 : 4148 : tree guard = get_global_binding (sname);
3669 : 4148 : if (! guard)
3670 : : {
3671 : 4148 : tree guard_type;
3672 : :
3673 : : /* We use a type that is big enough to contain a mutex as well
3674 : : as an integer counter. */
3675 : 4148 : guard_type = targetm.cxx.guard_type ();
3676 : 4148 : guard = build_decl (DECL_SOURCE_LOCATION (decl),
3677 : : VAR_DECL, sname, guard_type);
3678 : :
3679 : : /* The guard should have the same linkage as what it guards. */
3680 : 4148 : copy_linkage (guard, decl);
3681 : :
3682 : 4148 : DECL_ARTIFICIAL (guard) = 1;
3683 : 4148 : DECL_IGNORED_P (guard) = 1;
3684 : 4148 : TREE_USED (guard) = 1;
3685 : 4148 : pushdecl_top_level_and_finish (guard, NULL_TREE);
3686 : : }
3687 : 4148 : return guard;
3688 : : }
3689 : :
3690 : : /* Returns true if accessing the GUARD atomic is expensive,
3691 : : i.e. involves a call to __sync_synchronize or similar.
3692 : : In this case let __cxa_guard_acquire handle the atomics. */
3693 : :
3694 : : static bool
3695 : 3326 : is_atomic_expensive_p (machine_mode mode)
3696 : : {
3697 : 3326 : if (!flag_inline_atomics)
3698 : : return true;
3699 : :
3700 : 3326 : if (!can_compare_and_swap_p (mode, false) || !can_atomic_load_p (mode))
3701 : 0 : return true;
3702 : :
3703 : : return false;
3704 : : }
3705 : :
3706 : : /* Return an atomic load of src with the appropriate memory model. */
3707 : :
3708 : : static tree
3709 : 3326 : build_atomic_load_type (tree src, HOST_WIDE_INT model, tree type)
3710 : : {
3711 : 3326 : tree ptr_type = build_pointer_type (type);
3712 : 3326 : tree mem_model = build_int_cst (integer_type_node, model);
3713 : 3326 : tree t, addr, val;
3714 : 3326 : unsigned int size;
3715 : 3326 : int fncode;
3716 : :
3717 : 3326 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
3718 : :
3719 : 3326 : fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3720 : 3326 : t = builtin_decl_implicit ((enum built_in_function) fncode);
3721 : :
3722 : 3326 : addr = build1 (ADDR_EXPR, ptr_type, src);
3723 : 3326 : val = build_call_expr (t, 2, addr, mem_model);
3724 : 3326 : return val;
3725 : : }
3726 : :
3727 : : /* Return those bits of the GUARD variable that should be set when the
3728 : : guarded entity is actually initialized. */
3729 : :
3730 : : static tree
3731 : 1644 : get_guard_bits (tree guard)
3732 : : {
3733 : 1644 : if (!targetm.cxx.guard_mask_bit ())
3734 : : {
3735 : : /* We only set the first byte of the guard, in order to leave room
3736 : : for a mutex in the high-order bits. */
3737 : 1644 : guard = build1 (ADDR_EXPR,
3738 : 1644 : build_pointer_type (TREE_TYPE (guard)),
3739 : : guard);
3740 : 1644 : guard = build1 (NOP_EXPR,
3741 : : build_pointer_type (char_type_node),
3742 : : guard);
3743 : 1644 : guard = build1 (INDIRECT_REF, char_type_node, guard);
3744 : : }
3745 : :
3746 : 1644 : return guard;
3747 : : }
3748 : :
3749 : : /* Return an expression which determines whether or not the GUARD
3750 : : variable has already been initialized. */
3751 : :
3752 : : tree
3753 : 4148 : get_guard_cond (tree guard, bool thread_safe)
3754 : : {
3755 : 4148 : tree guard_value;
3756 : :
3757 : 4148 : if (!thread_safe)
3758 : 822 : guard = get_guard_bits (guard);
3759 : : else
3760 : : {
3761 : 3326 : tree type = targetm.cxx.guard_mask_bit ()
3762 : 3326 : ? TREE_TYPE (guard) : char_type_node;
3763 : :
3764 : 3326 : if (is_atomic_expensive_p (TYPE_MODE (type)))
3765 : 0 : guard = integer_zero_node;
3766 : : else
3767 : 3326 : guard = build_atomic_load_type (guard, MEMMODEL_ACQUIRE, type);
3768 : : }
3769 : :
3770 : : /* Mask off all but the low bit. */
3771 : 4148 : if (targetm.cxx.guard_mask_bit ())
3772 : : {
3773 : 0 : guard_value = integer_one_node;
3774 : 0 : if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3775 : 0 : guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3776 : 0 : guard = cp_build_binary_op (input_location,
3777 : : BIT_AND_EXPR, guard, guard_value,
3778 : : tf_warning_or_error);
3779 : : }
3780 : :
3781 : 4148 : guard_value = integer_zero_node;
3782 : 4148 : if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3783 : 4148 : guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3784 : 4148 : return cp_build_binary_op (input_location,
3785 : : EQ_EXPR, guard, guard_value,
3786 : 4148 : tf_warning_or_error);
3787 : : }
3788 : :
3789 : : /* Return an expression which sets the GUARD variable, indicating that
3790 : : the variable being guarded has been initialized. */
3791 : :
3792 : : tree
3793 : 822 : set_guard (tree guard)
3794 : : {
3795 : 822 : tree guard_init;
3796 : :
3797 : : /* Set the GUARD to one. */
3798 : 822 : guard = get_guard_bits (guard);
3799 : 822 : guard_init = integer_one_node;
3800 : 822 : if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3801 : 822 : guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3802 : 822 : return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3803 : 822 : tf_warning_or_error);
3804 : : }
3805 : :
3806 : : /* Returns true iff we can tell that VAR does not have a dynamic
3807 : : initializer. */
3808 : :
3809 : : static bool
3810 : 1806 : var_defined_without_dynamic_init (tree var)
3811 : : {
3812 : : /* constinit vars are guaranteed to not have dynamic initializer,
3813 : : but still registering the destructor counts as dynamic initialization. */
3814 : 1806 : if (DECL_DECLARED_CONSTINIT_P (var)
3815 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (var))
3816 : 1824 : && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3817 : : return true;
3818 : : /* If it's defined in another TU, we can't tell. */
3819 : 1794 : if (DECL_EXTERNAL (var))
3820 : : return false;
3821 : : /* If it has a non-trivial destructor, registering the destructor
3822 : : counts as dynamic initialization. */
3823 : 1598 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3824 : : return false;
3825 : : /* If it's in this TU, its initializer has been processed, unless
3826 : : it's a case of self-initialization, then DECL_INITIALIZED_P is
3827 : : false while the initializer is handled by finish_id_expression. */
3828 : 1444 : if (!DECL_INITIALIZED_P (var))
3829 : : return false;
3830 : : /* If it has no initializer or a constant one, it's not dynamic. */
3831 : 1438 : return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3832 : 1438 : || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3833 : : }
3834 : :
3835 : : /* Returns true iff VAR is a variable that needs uses to be
3836 : : wrapped for possible dynamic initialization. */
3837 : :
3838 : : bool
3839 : 76898 : var_needs_tls_wrapper (tree var)
3840 : : {
3841 : 76898 : return (!error_operand_p (var)
3842 : 76895 : && CP_DECL_THREAD_LOCAL_P (var)
3843 : 76895 : && !DECL_GNU_TLS_P (var)
3844 : 1861 : && !DECL_FUNCTION_SCOPE_P (var)
3845 : 78704 : && !var_defined_without_dynamic_init (var));
3846 : : }
3847 : :
3848 : : /* Get the FUNCTION_DECL for the shared TLS init function for this
3849 : : translation unit. */
3850 : :
3851 : : static tree
3852 : 114 : get_local_tls_init_fn (location_t loc)
3853 : : {
3854 : 114 : tree sname = get_identifier ("__tls_init");
3855 : 114 : tree fn = get_global_binding (sname);
3856 : 114 : if (!fn)
3857 : : {
3858 : 111 : fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname,
3859 : : build_function_type (void_type_node,
3860 : : void_list_node));
3861 : 111 : SET_DECL_LANGUAGE (fn, lang_c);
3862 : 111 : TREE_PUBLIC (fn) = false;
3863 : 111 : DECL_ARTIFICIAL (fn) = true;
3864 : 111 : mark_used (fn);
3865 : 111 : set_global_binding (fn);
3866 : : }
3867 : 114 : return fn;
3868 : : }
3869 : :
3870 : : /* Get a FUNCTION_DECL for the init function for the thread_local
3871 : : variable VAR. The init function will be an alias to the function
3872 : : that initializes all the non-local TLS variables in the translation
3873 : : unit. The init function is only used by the wrapper function. */
3874 : :
3875 : : static tree
3876 : 1017 : get_tls_init_fn (tree var)
3877 : : {
3878 : : /* Only C++11 TLS vars need this init fn. */
3879 : 1017 : if (!var_needs_tls_wrapper (var))
3880 : : return NULL_TREE;
3881 : :
3882 : : /* If -fno-extern-tls-init, assume that we don't need to call
3883 : : a tls init function for a variable defined in another TU. */
3884 : 1008 : if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3885 : : return NULL_TREE;
3886 : :
3887 : : /* If the variable is internal, or if we can't generate aliases,
3888 : : call the local init function directly. */
3889 : 1007 : if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3890 : 3 : return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var));
3891 : :
3892 : 1004 : tree sname = mangle_tls_init_fn (var);
3893 : 1004 : tree fn = get_global_binding (sname);
3894 : 1004 : if (!fn)
3895 : : {
3896 : 648 : fn = build_lang_decl (FUNCTION_DECL, sname,
3897 : : build_function_type (void_type_node,
3898 : : void_list_node));
3899 : 648 : SET_DECL_LANGUAGE (fn, lang_c);
3900 : 648 : TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3901 : 648 : DECL_ARTIFICIAL (fn) = true;
3902 : 648 : DECL_COMDAT (fn) = DECL_COMDAT (var);
3903 : 648 : DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3904 : 648 : if (DECL_ONE_ONLY (var))
3905 : 3 : make_decl_one_only (fn, cxx_comdat_group (fn));
3906 : 648 : if (TREE_PUBLIC (var))
3907 : : {
3908 : 648 : tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3909 : : /* If the variable is defined somewhere else and might have static
3910 : : initialization, make the init function a weak reference. */
3911 : 648 : if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3912 : 573 : || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3913 : 558 : || TYPE_HAS_TRIVIAL_DFLT (obtype))
3914 : 90 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3915 : 726 : && DECL_EXTERNAL (var))
3916 : 42 : declare_weak (fn);
3917 : : else
3918 : 606 : DECL_WEAK (fn) = DECL_WEAK (var);
3919 : : }
3920 : 648 : DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3921 : 648 : DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3922 : 648 : DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3923 : 648 : DECL_IGNORED_P (fn) = 1;
3924 : 648 : mark_used (fn);
3925 : :
3926 : 648 : DECL_BEFRIENDING_CLASSES (fn) = var;
3927 : :
3928 : 648 : set_global_binding (fn);
3929 : : }
3930 : : return fn;
3931 : : }
3932 : :
3933 : : /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3934 : : variable VAR. The wrapper function calls the init function (if any) for
3935 : : VAR and then returns a reference to VAR. The wrapper function is used
3936 : : in place of VAR everywhere VAR is mentioned. */
3937 : :
3938 : : static tree
3939 : 75866 : get_tls_wrapper_fn (tree var)
3940 : : {
3941 : : /* Only C++11 TLS vars need this wrapper fn. */
3942 : 75866 : if (!var_needs_tls_wrapper (var))
3943 : : return NULL_TREE;
3944 : :
3945 : 569 : tree sname = mangle_tls_wrapper_fn (var);
3946 : 569 : tree fn = get_global_binding (sname);
3947 : 569 : if (!fn)
3948 : : {
3949 : : /* A named rvalue reference is an lvalue, so the wrapper should
3950 : : always return an lvalue reference. */
3951 : 426 : tree type = non_reference (TREE_TYPE (var));
3952 : 426 : type = build_reference_type (type);
3953 : 426 : tree fntype = build_function_type (type, void_list_node);
3954 : :
3955 : 426 : fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var),
3956 : : FUNCTION_DECL, sname, fntype);
3957 : 426 : SET_DECL_LANGUAGE (fn, lang_c);
3958 : 426 : TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3959 : 426 : DECL_ARTIFICIAL (fn) = true;
3960 : 426 : DECL_IGNORED_P (fn) = 1;
3961 : 426 : DECL_CONTEXT (fn) = DECL_CONTEXT (var);
3962 : : /* The wrapper is inline and emitted everywhere var is used. */
3963 : 426 : DECL_DECLARED_INLINE_P (fn) = true;
3964 : 426 : if (TREE_PUBLIC (var))
3965 : : {
3966 : 423 : comdat_linkage (fn);
3967 : : #ifdef HAVE_GAS_HIDDEN
3968 : : /* Make the wrapper bind locally; there's no reason to share
3969 : : the wrapper between multiple shared objects. */
3970 : 423 : DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3971 : 423 : DECL_VISIBILITY_SPECIFIED (fn) = true;
3972 : : #endif
3973 : : }
3974 : 426 : if (!TREE_PUBLIC (fn))
3975 : 3 : DECL_INTERFACE_KNOWN (fn) = true;
3976 : 426 : mark_used (fn);
3977 : 426 : note_vague_linkage_fn (fn);
3978 : :
3979 : : #if 0
3980 : : /* We want CSE to commonize calls to the wrapper, but marking it as
3981 : : pure is unsafe since it has side-effects. I guess we need a new
3982 : : ECF flag even weaker than ECF_PURE. FIXME! */
3983 : : DECL_PURE_P (fn) = true;
3984 : : #endif
3985 : :
3986 : 426 : DECL_BEFRIENDING_CLASSES (fn) = var;
3987 : :
3988 : 426 : set_global_binding (fn);
3989 : : }
3990 : : return fn;
3991 : : }
3992 : :
3993 : : /* If EXPR is a thread_local variable that should be wrapped by init
3994 : : wrapper function, return a call to that function, otherwise return
3995 : : NULL. */
3996 : :
3997 : : tree
3998 : 731344386 : maybe_get_tls_wrapper_call (tree expr)
3999 : : {
4000 : 731344386 : if (VAR_P (expr)
4001 : 255368664 : && !processing_template_decl
4002 : 110739339 : && !cp_unevaluated_operand
4003 : 841200312 : && CP_DECL_THREAD_LOCAL_P (expr))
4004 : 75866 : if (tree wrap = get_tls_wrapper_fn (expr))
4005 : 569 : return build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
4006 : : return NULL;
4007 : : }
4008 : :
4009 : : /* At EOF, generate the definition for the TLS wrapper function FN:
4010 : :
4011 : : T& var_wrapper() {
4012 : : if (init_fn) init_fn();
4013 : : return var;
4014 : : } */
4015 : :
4016 : : static void
4017 : 429 : generate_tls_wrapper (tree fn)
4018 : : {
4019 : 429 : tree var = DECL_BEFRIENDING_CLASSES (fn);
4020 : :
4021 : 429 : start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
4022 : 429 : tree body = begin_function_body ();
4023 : : /* Only call the init fn if there might be one. */
4024 : 429 : if (tree init_fn = get_tls_init_fn (var))
4025 : : {
4026 : 419 : tree if_stmt = NULL_TREE;
4027 : : /* If init_fn is a weakref, make sure it exists before calling. */
4028 : 419 : if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
4029 : : {
4030 : 42 : if_stmt = begin_if_stmt ();
4031 : 42 : tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
4032 : 42 : tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
4033 : : NE_EXPR, addr, nullptr_node,
4034 : : tf_warning_or_error);
4035 : 42 : finish_if_stmt_cond (cond, if_stmt);
4036 : : }
4037 : 419 : finish_expr_stmt (build_cxx_call
4038 : : (init_fn, 0, NULL, tf_warning_or_error));
4039 : 419 : if (if_stmt)
4040 : : {
4041 : 42 : finish_then_clause (if_stmt);
4042 : 42 : finish_if_stmt (if_stmt);
4043 : : }
4044 : : }
4045 : : else
4046 : : /* If there's no initialization, the wrapper is a constant function. */
4047 : 10 : TREE_READONLY (fn) = true;
4048 : 429 : finish_return_stmt (convert_from_reference (var));
4049 : 429 : finish_function_body (body);
4050 : 429 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
4051 : 429 : }
4052 : :
4053 : : /* Start a global constructor or destructor function. */
4054 : :
4055 : : static tree
4056 : 6381 : start_objects (bool initp, unsigned priority, bool has_body,
4057 : : bool omp_target = false)
4058 : : {
4059 : 6381 : bool default_init = initp && priority == DEFAULT_INIT_PRIORITY;
4060 : 6381 : bool is_module_init = default_init && module_global_init_needed ();
4061 : 6381 : tree name = NULL_TREE;
4062 : :
4063 : 6381 : if (is_module_init)
4064 : 1384 : name = mangle_module_global_init (0);
4065 : : else
4066 : : {
4067 : 4997 : char type[14];
4068 : :
4069 : : /* We use `I' to indicate initialization and `D' to indicate
4070 : : destruction. */
4071 : 4997 : unsigned len;
4072 : 4997 : if (omp_target)
4073 : : /* Use "off_" signifying "offload" here. The name must be distinct
4074 : : from the non-offload case. The format of the name is scanned in
4075 : : tree.cc/get_file_function_name, so stick to the same length for
4076 : : both name variants. */
4077 : 6 : len = sprintf (type, "off_%c", initp ? 'I' : 'D');
4078 : : else
4079 : 5000 : len = sprintf (type, "sub_%c", initp ? 'I' : 'D');
4080 : 4997 : if (priority != DEFAULT_INIT_PRIORITY)
4081 : : {
4082 : 32 : char joiner = '_';
4083 : : #ifdef JOINER
4084 : 32 : joiner = JOINER;
4085 : : #endif
4086 : 32 : type[len++] = joiner;
4087 : 32 : sprintf (type + len, "%.5u", priority);
4088 : : }
4089 : 4997 : name = get_file_function_name (type);
4090 : : }
4091 : :
4092 : 6381 : tree fntype = build_function_type (void_type_node, void_list_node);
4093 : 6381 : tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
4094 : :
4095 : 6381 : if (omp_target)
4096 : : {
4097 : 12 : DECL_ATTRIBUTES (fndecl)
4098 : 12 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
4099 : 12 : DECL_ATTRIBUTES (fndecl));
4100 : 12 : DECL_ATTRIBUTES (fndecl)
4101 : 24 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
4102 : 12 : DECL_ATTRIBUTES (fndecl));
4103 : : }
4104 : :
4105 : 6381 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
4106 : 6381 : if (is_module_init)
4107 : : {
4108 : 1384 : SET_DECL_ASSEMBLER_NAME (fndecl, name);
4109 : 1384 : TREE_PUBLIC (fndecl) = true;
4110 : 1384 : determine_visibility (fndecl);
4111 : : }
4112 : : else
4113 : 4997 : TREE_PUBLIC (fndecl) = 0;
4114 : 6381 : start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4115 : :
4116 : : /* Mark as artificial because it's not explicitly in the user's
4117 : : source code. */
4118 : 6381 : DECL_ARTIFICIAL (current_function_decl) = 1;
4119 : :
4120 : : /* Mark this declaration as used to avoid spurious warnings. */
4121 : 6381 : TREE_USED (current_function_decl) = 1;
4122 : :
4123 : : /* Mark this function as a global constructor or destructor. */
4124 : 6381 : if (initp)
4125 : 6372 : DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
4126 : : else
4127 : 9 : DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
4128 : :
4129 : 6381 : tree body = begin_compound_stmt (BCS_FN_BODY);
4130 : :
4131 : 6381 : if (is_module_init && has_body)
4132 : : {
4133 : : // If the function is going to be empty, don't emit idempotency.
4134 : : // 'static bool __in_chrg = false;
4135 : : // if (__inchrg) return;
4136 : : // __inchrg = true
4137 : 30 : tree var = build_lang_decl (VAR_DECL, in_charge_identifier,
4138 : : boolean_type_node);
4139 : 30 : DECL_CONTEXT (var) = fndecl;
4140 : 30 : DECL_ARTIFICIAL (var) = true;
4141 : 30 : TREE_STATIC (var) = true;
4142 : 30 : pushdecl (var);
4143 : 30 : cp_finish_decl (var, NULL_TREE, false, NULL_TREE, 0);
4144 : :
4145 : 30 : tree if_stmt = begin_if_stmt ();
4146 : 30 : finish_if_stmt_cond (var, if_stmt);
4147 : 30 : finish_return_stmt (NULL_TREE);
4148 : 30 : finish_then_clause (if_stmt);
4149 : 30 : finish_if_stmt (if_stmt);
4150 : :
4151 : 30 : tree assign = build2 (MODIFY_EXPR, boolean_type_node,
4152 : : var, boolean_true_node);
4153 : 30 : TREE_SIDE_EFFECTS (assign) = true;
4154 : 30 : finish_expr_stmt (assign);
4155 : : }
4156 : :
4157 : 6381 : return body;
4158 : : }
4159 : :
4160 : : /* Finish a global constructor or destructor. Add it to the global
4161 : : ctors or dtors, if STARTP is true. */
4162 : :
4163 : : static tree
4164 : 6378 : finish_objects (bool initp, unsigned priority, tree body, bool startp)
4165 : : {
4166 : : /* Finish up. */
4167 : 6378 : finish_compound_stmt (body);
4168 : 6378 : tree fn = finish_function (/*inline_p=*/false);
4169 : :
4170 : 6378 : if (!startp)
4171 : : ; // Neither ctor nor dtor I be.
4172 : 5024 : else if (initp)
4173 : : {
4174 : 5015 : DECL_STATIC_CONSTRUCTOR (fn) = 1;
4175 : 5015 : decl_init_priority_insert (fn, priority);
4176 : : }
4177 : : else
4178 : : {
4179 : 9 : DECL_STATIC_DESTRUCTOR (fn) = 1;
4180 : 9 : decl_fini_priority_insert (fn, priority);
4181 : : }
4182 : :
4183 : 6378 : return fn;
4184 : : }
4185 : :
4186 : : /* The name of the function we create to handle initializations and
4187 : : destructions for objects with static storage duration. */
4188 : : #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
4189 : : #define OMP_SSDF_IDENTIFIER "__omp_target_static_init_and_destruction"
4190 : :
4191 : : /* Begins the generation of the function that will handle all
4192 : : initialization or destruction of objects with static storage
4193 : : duration at PRIORITY.
4194 : :
4195 : : It is assumed that this function will be called once for the host, and once
4196 : : for an OpenMP offload target. */
4197 : :
4198 : : static tree
4199 : 5030 : start_partial_init_fini_fn (bool initp, unsigned priority, unsigned count,
4200 : : bool omp_target)
4201 : : {
4202 : 5030 : char id[MAX (sizeof (SSDF_IDENTIFIER), sizeof (OMP_SSDF_IDENTIFIER))
4203 : : + 1 /* \0 */ + 32];
4204 : 5030 : tree name;
4205 : :
4206 : : /* Create the identifier for this function. It will be of the form
4207 : : SSDF_IDENTIFIER_<number> if not omp_target and otherwise
4208 : : OMP_SSDF_IDENTIFIER_<number>. */
4209 : 5030 : sprintf (id, "%s_%u", omp_target ? OMP_SSDF_IDENTIFIER : SSDF_IDENTIFIER,
4210 : : count);
4211 : 5030 : name = get_identifier (id);
4212 : 5030 : tree type = build_function_type (void_type_node, void_list_node);
4213 : :
4214 : : /* Create the FUNCTION_DECL itself. */
4215 : 5030 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4216 : 5030 : TREE_PUBLIC (fn) = 0;
4217 : 5030 : DECL_ARTIFICIAL (fn) = 1;
4218 : :
4219 : 5030 : if (omp_target)
4220 : : {
4221 : 6 : DECL_ATTRIBUTES (fn)
4222 : 6 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
4223 : 6 : DECL_ATTRIBUTES (fn));
4224 : 6 : DECL_ATTRIBUTES (fn)
4225 : 12 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
4226 : 6 : DECL_ATTRIBUTES (fn));
4227 : : }
4228 : :
4229 : 5030 : int idx = initp + 2 * omp_target;
4230 : :
4231 : : /* Put this function in the list of functions to be called from the
4232 : : static constructors and destructors. */
4233 : 5030 : if (!static_init_fini_fns[idx])
4234 : 5007 : static_init_fini_fns[idx] = priority_map_t::create_ggc ();
4235 : 5030 : auto &slot = static_init_fini_fns[idx]->get_or_insert (priority);
4236 : 5030 : slot = tree_cons (fn, NULL_TREE, slot);
4237 : :
4238 : : /* Put the function in the global scope. */
4239 : 5030 : pushdecl (fn);
4240 : :
4241 : : /* Start the function itself. This is equivalent to declaring the
4242 : : function as:
4243 : :
4244 : : static void __ssdf (int __initialize_p, init __priority_p);
4245 : :
4246 : : It is static because we only need to call this function from the
4247 : : various constructor and destructor functions for this module. */
4248 : 5030 : start_preparsed_function (fn, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4249 : :
4250 : : /* Set up the scope of the outermost block in the function. */
4251 : 5030 : return begin_compound_stmt (BCS_FN_BODY);
4252 : : }
4253 : :
4254 : : /* Finish the generation of the function which performs initialization
4255 : : or destruction of objects with static storage duration. */
4256 : :
4257 : : static void
4258 : 5030 : finish_partial_init_fini_fn (tree body)
4259 : : {
4260 : : /* Close out the function. */
4261 : 5030 : finish_compound_stmt (body);
4262 : 5030 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
4263 : 5030 : }
4264 : :
4265 : : /* The effective initialization priority of a DECL. */
4266 : :
4267 : : #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
4268 : : ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
4269 : : ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
4270 : :
4271 : : /* Whether a DECL needs a guard to protect it against multiple
4272 : : initialization. */
4273 : :
4274 : : #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
4275 : : || DECL_ONE_ONLY (decl) \
4276 : : || DECL_WEAK (decl)))
4277 : :
4278 : : /* Walks the initializer list of a global variable and looks for
4279 : : temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
4280 : : and that have their DECL_CONTEXT() == NULL. For each such
4281 : : temporary variable, set their DECL_CONTEXT() to CTX -- the
4282 : : initializing function. This is necessary because otherwise some
4283 : : optimizers (enabled by -O2 -fprofile-arcs) might crash when trying
4284 : : to refer to a temporary variable that does not have its
4285 : : DECL_CONTEXT() properly set. */
4286 : :
4287 : : static tree
4288 : 228881 : fix_temporary_vars_context_r (tree *node,
4289 : : int * /*unused*/,
4290 : : void *ctx)
4291 : : {
4292 : 228881 : if (TREE_CODE (*node) == BIND_EXPR)
4293 : 792 : for (tree var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
4294 : 396 : if (VAR_P (var) && !DECL_NAME (var)
4295 : 792 : && DECL_ARTIFICIAL (var) && !DECL_CONTEXT (var))
4296 : 396 : DECL_CONTEXT (var) = tree (ctx);
4297 : :
4298 : 228881 : return NULL_TREE;
4299 : : }
4300 : :
4301 : : /* Set up to handle the initialization or destruction of DECL. If
4302 : : INITP is nonzero, we are initializing the variable. Otherwise, we
4303 : : are destroying it. */
4304 : :
4305 : : static void
4306 : 9894 : one_static_initialization_or_destruction (bool initp, tree decl, tree init)
4307 : : {
4308 : : /* If we are supposed to destruct and there's a trivial destructor,
4309 : : nothing has to be done. */
4310 : 9894 : gcc_checking_assert (init || !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)));
4311 : :
4312 : : /* Trick the compiler into thinking we are at the file and line
4313 : : where DECL was declared so that error-messages make sense, and so
4314 : : that the debugger will show somewhat sensible file and line
4315 : : information. */
4316 : 9894 : input_location = DECL_SOURCE_LOCATION (decl);
4317 : :
4318 : : /* Make sure temporary variables in the initialiser all have
4319 : : their DECL_CONTEXT() set to a value different from NULL_TREE.
4320 : : This can happen when global variables initializers are built.
4321 : : In that case, the DECL_CONTEXT() of the global variables _AND_ of all
4322 : : the temporary variables that might have been generated in the
4323 : : accompanying initializers is NULL_TREE, meaning the variables have been
4324 : : declared in the global namespace.
4325 : : What we want to do here is to fix that and make sure the DECL_CONTEXT()
4326 : : of the temporaries are set to the current function decl. */
4327 : 9894 : cp_walk_tree_without_duplicates (&init,
4328 : : fix_temporary_vars_context_r,
4329 : : current_function_decl);
4330 : :
4331 : : /* Because of:
4332 : :
4333 : : [class.access.spec]
4334 : :
4335 : : Access control for implicit calls to the constructors,
4336 : : the conversion functions, or the destructor called to
4337 : : create and destroy a static data member is performed as
4338 : : if these calls appeared in the scope of the member's
4339 : : class.
4340 : :
4341 : : we pretend we are in a static member function of the class of
4342 : : which the DECL is a member. */
4343 : 9894 : if (member_p (decl))
4344 : : {
4345 : 1630 : DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
4346 : 1630 : DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
4347 : : }
4348 : :
4349 : : /* Assume we don't need a guard. */
4350 : 9894 : tree guard_if_stmt = NULL_TREE;
4351 : :
4352 : : /* We need a guard if this is an object with external linkage that
4353 : : might be initialized in more than one place. (For example, a
4354 : : static data member of a template, when the data member requires
4355 : : construction.) */
4356 : 9894 : if (NEEDS_GUARD_P (decl))
4357 : : {
4358 : 769 : tree guard = get_guard (decl);
4359 : 769 : tree guard_cond;
4360 : :
4361 : 769 : if (flag_use_cxa_atexit)
4362 : : {
4363 : : /* When using __cxa_atexit, we just check the GUARD as we
4364 : : would for a local static. We never try to destroy
4365 : : anything from a static destructor. */
4366 : 769 : gcc_assert (initp);
4367 : 769 : guard_cond = get_guard_cond (guard, false);
4368 : : }
4369 : : else
4370 : : {
4371 : : /* If we don't have __cxa_atexit, then we will be running
4372 : : destructors from .fini sections, or their equivalents.
4373 : : So, we need to know how many times we've tried to
4374 : : initialize this object. We do initializations only if
4375 : : the GUARD was or becomes zero (initp vs !initp
4376 : : respectively). */
4377 : 0 : guard_cond = cp_build_unary_op (initp ? POSTINCREMENT_EXPR
4378 : : : PREDECREMENT_EXPR,
4379 : : guard,
4380 : : /*noconvert=*/true,
4381 : : tf_warning_or_error);
4382 : 0 : guard_cond = cp_build_binary_op (input_location, EQ_EXPR, guard_cond,
4383 : : integer_zero_node,
4384 : : tf_warning_or_error);
4385 : : }
4386 : :
4387 : 769 : guard_if_stmt = begin_if_stmt ();
4388 : 769 : finish_if_stmt_cond (guard_cond, guard_if_stmt);
4389 : :
4390 : 769 : if (flag_use_cxa_atexit)
4391 : : /* Set the GUARD now. */
4392 : 769 : finish_expr_stmt (set_guard (guard));
4393 : : }
4394 : :
4395 : : /* Perform the initialization or destruction. */
4396 : 9894 : if (initp)
4397 : : {
4398 : 9879 : if (init)
4399 : : {
4400 : 9588 : finish_expr_stmt (init);
4401 : 9588 : if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
4402 : 28 : if (varpool_node *vnode = varpool_node::get (decl))
4403 : 28 : vnode->dynamically_initialized = 1;
4404 : : }
4405 : :
4406 : : /* If we're using __cxa_atexit, register a function that calls the
4407 : : destructor for the object. */
4408 : 9879 : if (flag_use_cxa_atexit)
4409 : 9870 : finish_expr_stmt (register_dtor_fn (decl));
4410 : : }
4411 : : else
4412 : 15 : finish_expr_stmt (build_cleanup (decl));
4413 : :
4414 : : /* Finish the guard if-stmt, if necessary. */
4415 : 9894 : if (guard_if_stmt)
4416 : : {
4417 : 769 : finish_then_clause (guard_if_stmt);
4418 : 769 : finish_if_stmt (guard_if_stmt);
4419 : : }
4420 : :
4421 : : /* Now that we're done with DECL we don't need to pretend to be a
4422 : : member of its class any longer. */
4423 : 9894 : DECL_CONTEXT (current_function_decl) = NULL_TREE;
4424 : 9894 : DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
4425 : 9894 : }
4426 : :
4427 : : /* Generate code to do the initialization or destruction of the decls in VARS,
4428 : : a TREE_LIST of VAR_DECL with static storage duration.
4429 : : Whether initialization or destruction is performed is specified by INITP. */
4430 : :
4431 : : static tree
4432 : 5002 : emit_partial_init_fini_fn (bool initp, unsigned priority, tree vars,
4433 : : unsigned counter, location_t locus, tree host_fn)
4434 : : {
4435 : 5002 : input_location = locus;
4436 : 5002 : bool omp_target = (host_fn != NULL_TREE);
4437 : 5002 : tree body = start_partial_init_fini_fn (initp, priority, counter, omp_target);
4438 : 5002 : tree fndecl = current_function_decl;
4439 : :
4440 : 5002 : tree nonhost_if_stmt = NULL_TREE;
4441 : 5002 : if (omp_target)
4442 : : {
4443 : 6 : nonhost_if_stmt = begin_if_stmt ();
4444 : : /* We add an "omp declare target nohost" attribute, but (for
4445 : : now) we still get a copy of the constructor/destructor on
4446 : : the host. Make sure it does nothing unless we're on the
4447 : : target device. */
4448 : 6 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
4449 : 6 : tree initial_dev = build_call_expr (fn, 0);
4450 : 6 : tree target_dev_p
4451 : 6 : = cp_build_binary_op (input_location, NE_EXPR, initial_dev,
4452 : : integer_one_node, tf_warning_or_error);
4453 : 6 : finish_if_stmt_cond (target_dev_p, nonhost_if_stmt);
4454 : : }
4455 : :
4456 : 14305 : for (tree node = vars; node; node = TREE_CHAIN (node))
4457 : : {
4458 : 9303 : tree decl = TREE_VALUE (node);
4459 : 9303 : tree init = TREE_PURPOSE (node);
4460 : : /* We will emit 'init' twice, and it is modified in-place during
4461 : : gimplification. Make a copy here. */
4462 : 9303 : if (omp_target)
4463 : : {
4464 : : /* We've already emitted INIT in the host version of the ctor/dtor
4465 : : function. We need to deep-copy it (including new versions of
4466 : : local variables introduced, etc.) for use in the target
4467 : : ctor/dtor function. */
4468 : 9 : copy_body_data id;
4469 : 9 : hash_map<tree, tree> decl_map;
4470 : 9 : memset (&id, 0, sizeof (id));
4471 : 9 : id.src_fn = host_fn;
4472 : 9 : id.dst_fn = current_function_decl;
4473 : 9 : id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
4474 : 9 : id.decl_map = &decl_map;
4475 : 9 : id.copy_decl = copy_decl_no_change;
4476 : 9 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4477 : 9 : id.transform_new_cfg = true;
4478 : 9 : id.transform_return_to_modify = false;
4479 : 9 : id.eh_lp_nr = 0;
4480 : 9 : walk_tree (&init, copy_tree_body_r, &id, NULL);
4481 : 9 : }
4482 : : /* Do one initialization or destruction. */
4483 : 9303 : one_static_initialization_or_destruction (initp, decl, init);
4484 : : }
4485 : :
4486 : 5002 : if (omp_target)
4487 : : {
4488 : : /* Finish up nonhost if-stmt body. */
4489 : 6 : finish_then_clause (nonhost_if_stmt);
4490 : 6 : finish_if_stmt (nonhost_if_stmt);
4491 : : }
4492 : :
4493 : : /* Finish up the static storage duration function for this
4494 : : round. */
4495 : 5002 : input_location = locus;
4496 : 5002 : finish_partial_init_fini_fn (body);
4497 : :
4498 : 5002 : return fndecl;
4499 : : }
4500 : :
4501 : : /* VARS is a list of variables with static storage duration which may
4502 : : need initialization and/or finalization. Remove those variables
4503 : : that don't really need to be initialized or finalized, and return
4504 : : the resulting list. The order in which the variables appear in
4505 : : VARS is in reverse order of the order in which they should actually
4506 : : be initialized. That order is preserved. */
4507 : :
4508 : : static tree
4509 : 270224 : prune_vars_needing_no_initialization (tree *vars)
4510 : : {
4511 : 270224 : tree *var = vars;
4512 : 270224 : tree result = NULL_TREE;
4513 : :
4514 : 280100 : while (*var)
4515 : : {
4516 : 9876 : tree t = *var;
4517 : 9876 : tree decl = TREE_VALUE (t);
4518 : 9876 : tree init = TREE_PURPOSE (t);
4519 : :
4520 : : /* Deal gracefully with error. */
4521 : 9876 : if (error_operand_p (decl))
4522 : : {
4523 : 0 : var = &TREE_CHAIN (t);
4524 : 0 : continue;
4525 : : }
4526 : :
4527 : : /* The only things that can be initialized are variables. */
4528 : 9876 : gcc_assert (VAR_P (decl));
4529 : :
4530 : : /* If this object is not defined, we don't need to do anything
4531 : : here. */
4532 : 9876 : if (DECL_EXTERNAL (decl))
4533 : : {
4534 : 0 : var = &TREE_CHAIN (t);
4535 : 0 : continue;
4536 : : }
4537 : :
4538 : : /* Also, if the initializer already contains errors, we can bail
4539 : : out now. */
4540 : 9579 : if (init && TREE_CODE (init) == TREE_LIST
4541 : 9876 : && value_member (error_mark_node, init))
4542 : : {
4543 : 0 : var = &TREE_CHAIN (t);
4544 : 0 : continue;
4545 : : }
4546 : :
4547 : : /* This variable is going to need initialization and/or
4548 : : finalization, so we add it to the list. */
4549 : 9876 : *var = TREE_CHAIN (t);
4550 : 9876 : TREE_CHAIN (t) = result;
4551 : 9876 : result = t;
4552 : : }
4553 : :
4554 : 270224 : return result;
4555 : : }
4556 : :
4557 : : /* Split VAR_LIST by init priority and add into PARTS hash table.
4558 : : This reverses the variable ordering. */
4559 : :
4560 : : void
4561 : 4982 : partition_vars_for_init_fini (tree var_list, priority_map_t *(&parts)[4])
4562 : : {
4563 : 14267 : for (auto node = var_list; node; node = TREE_CHAIN (node))
4564 : : {
4565 : 9285 : tree decl = TREE_VALUE (node);
4566 : 9285 : tree init = TREE_PURPOSE (node);
4567 : 9285 : bool has_cleanup = !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl));
4568 : 9285 : unsigned priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
4569 : :
4570 : 9285 : if (init || (flag_use_cxa_atexit && has_cleanup))
4571 : : {
4572 : : // Add to initialization list.
4573 : 9279 : if (!parts[true])
4574 : 4976 : parts[true] = priority_map_t::create_ggc ();
4575 : 9279 : auto &slot = parts[true]->get_or_insert (priority);
4576 : 9279 : slot = tree_cons (init, decl, slot);
4577 : : }
4578 : :
4579 : 9285 : if (!flag_use_cxa_atexit && has_cleanup)
4580 : : {
4581 : : // Add to finalization list.
4582 : 15 : if (!parts[false])
4583 : 9 : parts[false] = priority_map_t::create_ggc ();
4584 : 15 : auto &slot = parts[false]->get_or_insert (priority);
4585 : 15 : slot = tree_cons (NULL_TREE, decl, slot);
4586 : : }
4587 : :
4588 : 9285 : if (flag_openmp
4589 : 9285 : && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
4590 : : {
4591 : 9 : priority_map_t **omp_parts = parts + 2;
4592 : :
4593 : 9 : if (init || (flag_use_cxa_atexit && has_cleanup))
4594 : : {
4595 : : // Add to initialization list.
4596 : 9 : if (!omp_parts[true])
4597 : 6 : omp_parts[true] = priority_map_t::create_ggc ();
4598 : 9 : auto &slot = omp_parts[true]->get_or_insert (priority);
4599 : 9 : slot = tree_cons (init, decl, slot);
4600 : : }
4601 : :
4602 : 9 : if (!flag_use_cxa_atexit && has_cleanup)
4603 : : {
4604 : : // Add to finalization list.
4605 : 0 : if (!omp_parts[false])
4606 : 0 : omp_parts[false] = priority_map_t::create_ggc ();
4607 : 0 : auto &slot = omp_parts[false]->get_or_insert (priority);
4608 : 0 : slot = tree_cons (NULL_TREE, decl, slot);
4609 : : }
4610 : : }
4611 : : }
4612 : 4982 : }
4613 : :
4614 : : /* Make sure we have told the back end about all the variables in
4615 : : VARS. */
4616 : :
4617 : : static void
4618 : 5093 : write_out_vars (tree vars)
4619 : : {
4620 : 5093 : tree v;
4621 : :
4622 : 14969 : for (v = vars; v; v = TREE_CHAIN (v))
4623 : : {
4624 : 9876 : tree var = TREE_VALUE (v);
4625 : 9876 : if (!var_finalized_p (var))
4626 : : {
4627 : 535 : import_export_decl (var);
4628 : 535 : rest_of_decl_compilation (var, 1, 1);
4629 : : }
4630 : : }
4631 : 5093 : }
4632 : :
4633 : : /* Generate a static constructor or destructor that calls the given
4634 : : init/fini fns at the indicated priority. */
4635 : :
4636 : : static void
4637 : 6372 : generate_ctor_or_dtor_function (bool initp, unsigned priority,
4638 : : tree fns, location_t locus, bool omp_target)
4639 : : {
4640 : 6372 : input_location = locus;
4641 : 6372 : tree body = start_objects (initp, priority, bool (fns), omp_target);
4642 : :
4643 : 6372 : if (fns)
4644 : : {
4645 : : /* To make sure dynamic construction doesn't access globals from
4646 : : other compilation units where they might not be yet
4647 : : constructed, for -fsanitize=address insert
4648 : : __asan_before_dynamic_init call that prevents access to
4649 : : either all global variables that need construction in other
4650 : : compilation units, or at least those that haven't been
4651 : : initialized yet. Variables that need dynamic construction in
4652 : : the current compilation unit are kept accessible. */
4653 : 5018 : if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4654 : 21 : finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
4655 : :
4656 : : /* Call the static init/fini functions. */
4657 : 10048 : for (tree node = fns; node; node = TREE_CHAIN (node))
4658 : : {
4659 : 5030 : tree fn = TREE_PURPOSE (node);
4660 : :
4661 : : // We should never find a pure or constant cdtor.
4662 : 5030 : gcc_checking_assert (!(flags_from_decl_or_type (fn)
4663 : : & (ECF_CONST | ECF_PURE)));
4664 : :
4665 : 5030 : tree call = cp_build_function_call_nary (fn, tf_warning_or_error,
4666 : : NULL_TREE);
4667 : 5030 : finish_expr_stmt (call);
4668 : : }
4669 : :
4670 : : /* Revert what __asan_before_dynamic_init did by calling
4671 : : __asan_after_dynamic_init. */
4672 : 5018 : if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4673 : 21 : finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4674 : : }
4675 : :
4676 : : /* Close out the function, and arrange for it to be called at init
4677 : : or fini time, if non-empty. (Even non-nop module initializer
4678 : : functions need this, as we cannot guarantee the module is
4679 : : imported somewhere in the program.) */
4680 : 6372 : expand_or_defer_fn (finish_objects (initp, priority, body, fns != NULL_TREE));
4681 : 6372 : }
4682 : :
4683 : : /* Return C++ property of T, based on given operation OP. */
4684 : :
4685 : : static int
4686 : 1941 : cpp_check (tree t, cpp_operation op)
4687 : : {
4688 : 1941 : switch (op)
4689 : : {
4690 : 9 : case HAS_DEPENDENT_TEMPLATE_ARGS:
4691 : 9 : {
4692 : 9 : tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4693 : 9 : if (!ti)
4694 : : return 0;
4695 : 9 : ++processing_template_decl;
4696 : 9 : const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4697 : 9 : --processing_template_decl;
4698 : 9 : return dep;
4699 : : }
4700 : 294 : case IS_ABSTRACT:
4701 : 294 : return DECL_PURE_VIRTUAL_P (t);
4702 : 165 : case IS_ASSIGNMENT_OPERATOR:
4703 : 165 : return DECL_ASSIGNMENT_OPERATOR_P (t);
4704 : 165 : case IS_CONSTRUCTOR:
4705 : 330 : return DECL_CONSTRUCTOR_P (t);
4706 : 165 : case IS_DESTRUCTOR:
4707 : 330 : return DECL_DESTRUCTOR_P (t);
4708 : 165 : case IS_COPY_CONSTRUCTOR:
4709 : 477 : return DECL_COPY_CONSTRUCTOR_P (t);
4710 : 165 : case IS_MOVE_CONSTRUCTOR:
4711 : 483 : return DECL_MOVE_CONSTRUCTOR_P (t);
4712 : 447 : case IS_TEMPLATE:
4713 : 447 : return TREE_CODE (t) == TEMPLATE_DECL;
4714 : 366 : case IS_TRIVIAL:
4715 : 366 : return trivial_type_p (t);
4716 : : default:
4717 : : return 0;
4718 : : }
4719 : : }
4720 : :
4721 : : /* Collect source file references recursively, starting from NAMESPC. */
4722 : :
4723 : : static void
4724 : 78 : collect_source_refs (tree namespc)
4725 : : {
4726 : : /* Iterate over names in this name space. */
4727 : 173843 : for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4728 : 173765 : if (DECL_IS_UNDECLARED_BUILTIN (t))
4729 : : ;
4730 : 240 : else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4731 : 3 : collect_source_refs (t);
4732 : : else
4733 : 237 : collect_source_ref (DECL_SOURCE_FILE (t));
4734 : 78 : }
4735 : :
4736 : : /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4737 : : starting from NAMESPC. */
4738 : :
4739 : : static void
4740 : 228 : collect_ada_namespace (tree namespc, const char *source_file)
4741 : : {
4742 : 228 : tree decl = NAMESPACE_LEVEL (namespc)->names;
4743 : :
4744 : : /* Collect decls from this namespace. This will skip
4745 : : NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4746 : 228 : collect_ada_nodes (decl, source_file);
4747 : :
4748 : : /* Now scan for namespace children, and dump them. */
4749 : 207896 : for (; decl; decl = TREE_CHAIN (decl))
4750 : 207440 : if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4751 : 153 : collect_ada_namespace (decl, source_file);
4752 : 228 : }
4753 : :
4754 : : /* Returns true iff there is a definition available for variable or
4755 : : function DECL. */
4756 : :
4757 : : bool
4758 : 59785310 : decl_defined_p (tree decl)
4759 : : {
4760 : 59785310 : if (TREE_CODE (decl) == FUNCTION_DECL)
4761 : 55778376 : return (DECL_INITIAL (decl) != NULL_TREE
4762 : : /* A pending instantiation of a friend temploid is defined. */
4763 : 55778376 : || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4764 : 7403406 : && DECL_INITIAL (DECL_TEMPLATE_RESULT
4765 : : (DECL_TI_TEMPLATE (decl)))));
4766 : : else
4767 : : {
4768 : 4006934 : gcc_assert (VAR_P (decl));
4769 : 4006934 : return !DECL_EXTERNAL (decl);
4770 : : }
4771 : : }
4772 : :
4773 : : /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4774 : :
4775 : : [expr.const]
4776 : :
4777 : : An integral constant-expression can only involve ... const
4778 : : variables of integral or enumeration types initialized with
4779 : : constant expressions ...
4780 : :
4781 : : C++0x also allows constexpr variables and temporaries initialized
4782 : : with constant expressions. We handle the former here, but the latter
4783 : : are just folded away in cxx_eval_constant_expression.
4784 : :
4785 : : The standard does not require that the expression be non-volatile.
4786 : : G++ implements the proposed correction in DR 457. */
4787 : :
4788 : : bool
4789 : 784260802 : decl_constant_var_p (tree decl)
4790 : : {
4791 : 784260802 : if (!decl_maybe_constant_var_p (decl))
4792 : : return false;
4793 : :
4794 : : /* We don't know if a template static data member is initialized with
4795 : : a constant expression until we instantiate its initializer. Even
4796 : : in the case of a constexpr variable, we can't treat it as a
4797 : : constant until its initializer is complete in case it's used in
4798 : : its own initializer. */
4799 : 86265003 : maybe_instantiate_decl (decl);
4800 : 86262306 : return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4801 : : }
4802 : :
4803 : : /* Returns true if DECL could be a symbolic constant variable, depending on
4804 : : its initializer. */
4805 : :
4806 : : bool
4807 : 1026803360 : decl_maybe_constant_var_p (tree decl)
4808 : : {
4809 : 1026803360 : tree type = TREE_TYPE (decl);
4810 : 1026803360 : if (!VAR_P (decl))
4811 : : return false;
4812 : 458455989 : if (DECL_DECLARED_CONSTEXPR_P (decl) && !TREE_THIS_VOLATILE (decl))
4813 : : return true;
4814 : 285584736 : if (DECL_HAS_VALUE_EXPR_P (decl))
4815 : : /* A proxy isn't constant. */
4816 : : return false;
4817 : 284964266 : if (TYPE_REF_P (type))
4818 : : /* References can be constant. */;
4819 : 282066989 : else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4820 : 282066989 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4821 : : /* And const integers. */;
4822 : : else
4823 : : return false;
4824 : :
4825 : 54669965 : if (DECL_INITIAL (decl)
4826 : 85685460 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4827 : : /* We know the initializer, and it isn't constant. */
4828 : : return false;
4829 : : else
4830 : : return true;
4831 : : }
4832 : :
4833 : : /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4834 : : called from grokfndecl and grokvardecl; in all modes it is called from
4835 : : cp_write_global_declarations. */
4836 : :
4837 : : void
4838 : 1432070 : no_linkage_error (tree decl)
4839 : : {
4840 : 1432070 : if (cxx_dialect >= cxx11
4841 : 1432070 : && (decl_defined_p (decl)
4842 : : /* Treat templates which limit_bad_template_recursion decided
4843 : : not to instantiate as if they were defined. */
4844 : 95 : || (errorcount + sorrycount > 0
4845 : 32 : && DECL_LANG_SPECIFIC (decl)
4846 : 32 : && DECL_TEMPLATE_INFO (decl)
4847 : 24 : && warning_suppressed_p (decl /* What warning? */))))
4848 : : /* In C++11 it's ok if the decl is defined. */
4849 : 329324 : return;
4850 : :
4851 : 1102746 : if (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl))
4852 : : /* An imported decl is ok. */
4853 : : return;
4854 : :
4855 : 1102701 : tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4856 : 1102701 : if (t == NULL_TREE)
4857 : : /* The type that got us on no_linkage_decls must have gotten a name for
4858 : : linkage purposes. */;
4859 : 50 : else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4860 : : // FIXME: This is now invalid, as a DR to c++98
4861 : : /* The type might end up having a typedef name for linkage purposes. */
4862 : 0 : vec_safe_push (no_linkage_decls, decl);
4863 : 129 : else if (TYPE_UNNAMED_P (t))
4864 : : {
4865 : 26 : bool d = false;
4866 : 26 : auto_diagnostic_group grp;
4867 : 26 : if (cxx_dialect >= cxx11)
4868 : : {
4869 : : /* If t is declared in a module CMI, then decl could actually
4870 : : be defined in a different TU, so don't warn since C++20. */
4871 : 9 : tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
4872 : 9 : if (relaxed != NULL_TREE)
4873 : 9 : d = permerror (DECL_SOURCE_LOCATION (decl),
4874 : : "%q#D, declared using an unnamed type, "
4875 : : "is used but never defined", decl);
4876 : 0 : else if (cxx_dialect < cxx20)
4877 : 0 : d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
4878 : : "%q#D, declared using an unnamed type, "
4879 : : "is used but not defined", decl);
4880 : : }
4881 : 17 : else if (DECL_EXTERN_C_P (decl))
4882 : : /* Allow this; it's pretty common in C. */;
4883 : 17 : else if (VAR_P (decl))
4884 : : /* DRs 132, 319 and 389 seem to indicate types with
4885 : : no linkage can only be used to declare extern "C"
4886 : : entities. Since it's not always an error in the
4887 : : ISO C++ 90 Standard, we only issue a warning. */
4888 : 16 : d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4889 : : "with no linkage used to declare variable %q#D with "
4890 : : "linkage", decl);
4891 : : else
4892 : 1 : d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4893 : : "linkage used to declare function %q#D with linkage",
4894 : : decl);
4895 : 26 : if (d && is_typedef_decl (TYPE_NAME (t)))
4896 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4897 : : "to the unqualified type, so it is not used for linkage",
4898 : 0 : TYPE_NAME (t));
4899 : : /* Suppress warning from check_global_declaration if needed. */
4900 : 26 : if (d)
4901 : 23 : suppress_warning (decl, OPT_Wunused);
4902 : 26 : }
4903 : 24 : else if (cxx_dialect >= cxx11)
4904 : : {
4905 : 24 : if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4906 : : {
4907 : : /* Similarly for local types in a function with vague linkage or
4908 : : defined in a module CMI, then decl could actually be defined
4909 : : in a different TU, so don't warn since C++20. */
4910 : 21 : bool d = false;
4911 : 21 : tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
4912 : 21 : if (relaxed != NULL_TREE)
4913 : 15 : d = permerror (DECL_SOURCE_LOCATION (decl),
4914 : : "%q#D, declared using local type "
4915 : : "%qT, is used but never defined", decl, t);
4916 : 6 : else if (cxx_dialect < cxx20)
4917 : 3 : d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
4918 : : "%q#D, declared using local type "
4919 : : "%qT, is used but not defined here", decl, t);
4920 : : /* Suppress warning from check_global_declaration if needed. */
4921 : 18 : if (d)
4922 : 18 : suppress_warning (decl, OPT_Wunused);
4923 : : }
4924 : : }
4925 : 0 : else if (VAR_P (decl))
4926 : 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4927 : : "used to declare variable %q#D with linkage", t, decl);
4928 : : else
4929 : 0 : permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4930 : : "to declare function %q#D with linkage", t, decl);
4931 : : }
4932 : :
4933 : : /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4934 : :
4935 : : static void
4936 : 75 : collect_all_refs (const char *source_file)
4937 : : {
4938 : 75 : collect_ada_namespace (global_namespace, source_file);
4939 : 75 : }
4940 : :
4941 : : /* Clear DECL_EXTERNAL for NODE. */
4942 : :
4943 : : static bool
4944 : 99707768 : clear_decl_external (struct cgraph_node *node, void * /*data*/)
4945 : : {
4946 : 99707768 : DECL_EXTERNAL (node->decl) = 0;
4947 : 99707768 : return false;
4948 : : }
4949 : :
4950 : : /* Build up the function to run dynamic initializers for thread_local
4951 : : variables in this translation unit and alias the init functions for the
4952 : : individual variables to it. */
4953 : :
4954 : : static void
4955 : 135112 : handle_tls_init (void)
4956 : : {
4957 : 135112 : tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4958 : 135112 : if (vars == NULL_TREE)
4959 : : return;
4960 : :
4961 : 111 : location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4962 : :
4963 : 111 : write_out_vars (vars);
4964 : :
4965 : 111 : tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4966 : : boolean_type_node);
4967 : 111 : TREE_PUBLIC (guard) = false;
4968 : 111 : TREE_STATIC (guard) = true;
4969 : 111 : DECL_ARTIFICIAL (guard) = true;
4970 : 111 : DECL_IGNORED_P (guard) = true;
4971 : 111 : TREE_USED (guard) = true;
4972 : 111 : CP_DECL_THREAD_LOCAL_P (guard) = true;
4973 : 111 : set_decl_tls_model (guard, decl_default_tls_model (guard));
4974 : 111 : pushdecl_top_level_and_finish (guard, NULL_TREE);
4975 : :
4976 : 111 : tree fn = get_local_tls_init_fn (loc);
4977 : 111 : start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4978 : 111 : tree body = begin_function_body ();
4979 : 111 : tree if_stmt = begin_if_stmt ();
4980 : 111 : tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4981 : : tf_warning_or_error);
4982 : 111 : finish_if_stmt_cond (cond, if_stmt);
4983 : 111 : finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4984 : : boolean_true_node,
4985 : : tf_warning_or_error));
4986 : 702 : for (; vars; vars = TREE_CHAIN (vars))
4987 : : {
4988 : 591 : tree var = TREE_VALUE (vars);
4989 : 591 : tree init = TREE_PURPOSE (vars);
4990 : 591 : one_static_initialization_or_destruction (/*initp=*/true, var, init);
4991 : :
4992 : : /* Output init aliases even with -fno-extern-tls-init. */
4993 : 591 : if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4994 : : {
4995 : 588 : tree single_init_fn = get_tls_init_fn (var);
4996 : 588 : if (single_init_fn == NULL_TREE)
4997 : 0 : continue;
4998 : 588 : cgraph_node *alias
4999 : 588 : = cgraph_node::get_create (fn)->create_same_body_alias
5000 : 588 : (single_init_fn, fn);
5001 : 588 : gcc_assert (alias != NULL);
5002 : : }
5003 : : }
5004 : :
5005 : 111 : finish_then_clause (if_stmt);
5006 : 111 : finish_if_stmt (if_stmt);
5007 : 111 : finish_function_body (body);
5008 : 111 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
5009 : : }
5010 : :
5011 : : /* We're at the end of compilation, so generate any mangling aliases that
5012 : : we've been saving up, if DECL is going to be output and ID2 isn't
5013 : : already taken by another declaration. */
5014 : :
5015 : : static void
5016 : 232814 : generate_mangling_alias (tree decl, tree id2)
5017 : : {
5018 : 232814 : struct cgraph_node *n = NULL;
5019 : :
5020 : 232814 : if (TREE_CODE (decl) == FUNCTION_DECL)
5021 : : {
5022 : 227313 : n = cgraph_node::get (decl);
5023 : 227313 : if (!n)
5024 : : /* Don't create an alias to an unreferenced function. */
5025 : : return;
5026 : : }
5027 : :
5028 : 232806 : tree *slot
5029 : 232806 : = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
5030 : : INSERT);
5031 : :
5032 : : /* If there's a declaration already using this mangled name,
5033 : : don't create a compatibility alias that conflicts. */
5034 : 232806 : if (*slot)
5035 : : return;
5036 : :
5037 : 232706 : tree alias = make_alias_for (decl, id2);
5038 : 232706 : *slot = alias;
5039 : :
5040 : 232706 : DECL_IGNORED_P (alias) = 1;
5041 : 232706 : TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
5042 : 232706 : DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
5043 : 232706 : if (vague_linkage_p (decl))
5044 : 229956 : DECL_WEAK (alias) = 1;
5045 : :
5046 : 232706 : if (n)
5047 : 227207 : n->create_same_body_alias (alias, decl);
5048 : : else
5049 : 5499 : varpool_node::create_extra_name_alias (alias, decl);
5050 : : }
5051 : :
5052 : : /* Note that we might want to emit an alias with the symbol ID2 for DECL at
5053 : : the end of translation, for compatibility across bugs in the mangling
5054 : : implementation. */
5055 : :
5056 : : void
5057 : 232814 : note_mangling_alias (tree decl, tree id2)
5058 : : {
5059 : 232814 : if (TARGET_SUPPORTS_ALIASES)
5060 : : {
5061 : 232814 : if (!defer_mangling_aliases)
5062 : 105775 : generate_mangling_alias (decl, id2);
5063 : : else
5064 : : {
5065 : 127039 : vec_safe_push (mangling_aliases, decl);
5066 : 127039 : vec_safe_push (mangling_aliases, id2);
5067 : : }
5068 : : }
5069 : 232814 : }
5070 : :
5071 : : /* Emit all mangling aliases that were deferred up to this point. */
5072 : :
5073 : : void
5074 : 90704 : generate_mangling_aliases ()
5075 : : {
5076 : 217743 : while (!vec_safe_is_empty (mangling_aliases))
5077 : : {
5078 : 127039 : tree id2 = mangling_aliases->pop();
5079 : 127039 : tree decl = mangling_aliases->pop();
5080 : 127039 : generate_mangling_alias (decl, id2);
5081 : : }
5082 : 90704 : defer_mangling_aliases = false;
5083 : 90704 : }
5084 : :
5085 : : /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
5086 : : set. NEED_WARNING is true if we must warn about collisions. We do
5087 : : this to spot changes in mangling that may require compatibility
5088 : : aliases. */
5089 : :
5090 : : void
5091 : 54714767 : record_mangling (tree decl, bool need_warning)
5092 : : {
5093 : 54714767 : if (!mangled_decls)
5094 : 69673 : mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
5095 : :
5096 : 54714767 : gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
5097 : 54714767 : tree id = DECL_ASSEMBLER_NAME_RAW (decl);
5098 : 54714767 : tree *slot
5099 : 54714767 : = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
5100 : : INSERT);
5101 : :
5102 : : /* If this is already an alias, cancel the alias, because the real
5103 : : decl takes precedence. */
5104 : 54714767 : if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
5105 : : {
5106 : 63 : if (symtab_node *n = symtab_node::get (*slot))
5107 : : {
5108 : 61 : if (n->cpp_implicit_alias)
5109 : : /* Actually removing the node isn't safe if other code is already
5110 : : holding a pointer to it, so just neutralize it. */
5111 : 61 : n->reset ();
5112 : : }
5113 : : else
5114 : : /* analyze_functions might have already removed the alias from the
5115 : : symbol table if it's internal. */
5116 : 2 : gcc_checking_assert (!TREE_PUBLIC (*slot));
5117 : :
5118 : 63 : *slot = NULL_TREE;
5119 : : }
5120 : :
5121 : 54714767 : if (!*slot)
5122 : 54714580 : *slot = decl;
5123 : 187 : else if (need_warning)
5124 : : {
5125 : 3 : auto_diagnostic_group d;
5126 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
5127 : : "mangling of %q#D as %qE conflicts with a previous mangle",
5128 : : decl, id);
5129 : 3 : inform (DECL_SOURCE_LOCATION (*slot),
5130 : : "previous mangling %q#D", *slot);
5131 : 3 : inform (DECL_SOURCE_LOCATION (decl),
5132 : : "a later %<-fabi-version=%> (or =0)"
5133 : : " avoids this error with a change in mangling");
5134 : 3 : *slot = decl;
5135 : 3 : }
5136 : 54714767 : }
5137 : :
5138 : : /* The mangled name of DECL is being forcibly changed to NAME. Remove
5139 : : any existing knowledge of DECL's mangled name meaning DECL. */
5140 : :
5141 : : void
5142 : 215292856 : overwrite_mangling (tree decl, tree name)
5143 : : {
5144 : 215292856 : if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
5145 : 208626 : if ((TREE_CODE (decl) == VAR_DECL
5146 : 208536 : || TREE_CODE (decl) == FUNCTION_DECL)
5147 : 208626 : && mangled_decls)
5148 : 404306 : if (tree *slot
5149 : 202153 : = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
5150 : : NO_INSERT))
5151 : 106016 : if (*slot == decl)
5152 : : {
5153 : 45 : mangled_decls->clear_slot (slot);
5154 : :
5155 : : /* If this is an alias, remove it from the symbol table. */
5156 : 45 : if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
5157 : 0 : if (symtab_node *n = symtab_node::get (decl))
5158 : 0 : if (n->cpp_implicit_alias)
5159 : 0 : n->remove ();
5160 : : }
5161 : :
5162 : 215292856 : DECL_ASSEMBLER_NAME_RAW (decl) = name;
5163 : 215292856 : }
5164 : :
5165 : : /* The entire file is now complete. If requested, dump everything
5166 : : to a file. */
5167 : :
5168 : : static void
5169 : 90792 : dump_tu (void)
5170 : : {
5171 : 90792 : dump_flags_t flags;
5172 : 90792 : if (FILE *stream = dump_begin (raw_dump_id, &flags))
5173 : : {
5174 : 3 : dump_node (global_namespace, flags & ~TDF_SLIM, stream);
5175 : 3 : dump_end (raw_dump_id, stream);
5176 : : }
5177 : 90792 : }
5178 : :
5179 : : static location_t locus_at_end_of_parsing;
5180 : :
5181 : : /* Check the deallocation functions for CODE to see if we want to warn that
5182 : : only one was defined. */
5183 : :
5184 : : static void
5185 : 1862 : maybe_warn_sized_delete (enum tree_code code)
5186 : : {
5187 : 1862 : tree sized = NULL_TREE;
5188 : 1862 : tree unsized = NULL_TREE;
5189 : :
5190 : 8004 : for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
5191 : 8004 : iter; ++iter)
5192 : : {
5193 : 6142 : tree fn = *iter;
5194 : : /* We're only interested in usual deallocation functions. */
5195 : 6142 : if (!usual_deallocation_fn_p (fn))
5196 : 930 : continue;
5197 : 5212 : if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5198 : : unsized = fn;
5199 : : else
5200 : 3350 : sized = fn;
5201 : : }
5202 : 1862 : if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
5203 : 6 : warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
5204 : : "the program should also define %qD", sized);
5205 : 1856 : else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
5206 : 4 : warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
5207 : : "the program should also define %qD", unsized);
5208 : 1862 : }
5209 : :
5210 : : /* Check the global deallocation functions to see if we want to warn about
5211 : : defining unsized without sized (or vice versa). */
5212 : :
5213 : : static void
5214 : 90704 : maybe_warn_sized_delete ()
5215 : : {
5216 : 90704 : if (!flag_sized_deallocation || !warn_sized_deallocation)
5217 : : return;
5218 : 931 : maybe_warn_sized_delete (DELETE_EXPR);
5219 : 931 : maybe_warn_sized_delete (VEC_DELETE_EXPR);
5220 : : }
5221 : :
5222 : : /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
5223 : : look them up when evaluating non-type template parameters. Now we need to
5224 : : lower them to something the back end can understand. */
5225 : :
5226 : : static void
5227 : 90704 : lower_var_init ()
5228 : : {
5229 : 90704 : varpool_node *node;
5230 : 33823502 : FOR_EACH_VARIABLE (node)
5231 : : {
5232 : 16821047 : tree d = node->decl;
5233 : 16821047 : if (tree init = DECL_INITIAL (d))
5234 : 14866948 : DECL_INITIAL (d) = cplus_expand_constant (init);
5235 : : }
5236 : 90704 : }
5237 : :
5238 : : /* This routine is called at the end of compilation.
5239 : : Its job is to create all the code needed to initialize and
5240 : : destroy the global aggregates. We do the destruction
5241 : : first, since that way we only need to reverse the decls once. */
5242 : :
5243 : : void
5244 : 90807 : c_parse_final_cleanups (void)
5245 : : {
5246 : 90807 : size_t i;
5247 : 90807 : tree decl;
5248 : :
5249 : 90807 : locus_at_end_of_parsing = input_location;
5250 : : /* We're done parsing. */
5251 : 90807 : at_eof = 1;
5252 : :
5253 : : /* Bad parse errors. Just forget about it. */
5254 : 181614 : if (! global_bindings_p () || current_class_type
5255 : 181614 : || !vec_safe_is_empty (decl_namespace_list))
5256 : 91 : return;
5257 : :
5258 : : /* This is the point to write out a PCH if we're doing that.
5259 : : In that case we do not want to do anything else. */
5260 : 90804 : if (pch_file)
5261 : : {
5262 : : /* Mangle all symbols at PCH creation time. */
5263 : 88 : symtab_node *node;
5264 : 28560 : FOR_EACH_SYMBOL (node)
5265 : 28472 : if (! is_a <varpool_node *> (node)
5266 : 5046 : || ! DECL_HARD_REGISTER (node->decl))
5267 : 28472 : DECL_ASSEMBLER_NAME (node->decl);
5268 : 88 : c_common_write_pch ();
5269 : 88 : dump_tu ();
5270 : : /* Ensure even the callers don't try to finalize the CU. */
5271 : 88 : flag_syntax_only = 1;
5272 : 88 : return;
5273 : : }
5274 : :
5275 : 90716 : timevar_stop (TV_PHASE_PARSING);
5276 : 90716 : timevar_start (TV_PHASE_DEFERRED);
5277 : :
5278 : 90716 : symtab->process_same_body_aliases ();
5279 : :
5280 : : /* Handle -fdump-ada-spec[-slim] */
5281 : 90716 : if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
5282 : : {
5283 : 75 : collect_source_ref (main_input_filename);
5284 : 75 : if (!flag_dump_ada_spec_slim)
5285 : 75 : collect_source_refs (global_namespace);
5286 : :
5287 : 75 : dump_ada_specs (collect_all_refs, cpp_check);
5288 : : }
5289 : :
5290 : : /* FIXME - huh? was input_line -= 1;*/
5291 : :
5292 : : /* We now have to write out all the stuff we put off writing out.
5293 : : These include:
5294 : :
5295 : : o Template specializations that we have not yet instantiated,
5296 : : but which are needed.
5297 : : o Initialization and destruction for non-local objects with
5298 : : static storage duration. (Local objects with static storage
5299 : : duration are initialized when their scope is first entered,
5300 : : and are cleaned up via atexit.)
5301 : : o Virtual function tables.
5302 : :
5303 : : All of these may cause others to be needed. For example,
5304 : : instantiating one function may cause another to be needed, and
5305 : : generating the initializer for an object may cause templates to be
5306 : : instantiated, etc., etc. */
5307 : :
5308 : 90716 : emit_support_tinfos ();
5309 : :
5310 : : /* Track vtables we want to emit that refer to consteval functions. */
5311 : 90716 : auto_vec<tree> consteval_vtables;
5312 : :
5313 : 90716 : int retries = 0;
5314 : 90716 : unsigned ssdf_count = 0, omp_ssdf_count = 0;
5315 : 226579 : for (bool reconsider = true; reconsider; retries++)
5316 : : {
5317 : 135875 : reconsider = false;
5318 : :
5319 : : /* If there are templates that we've put off instantiating, do
5320 : : them now. */
5321 : 135875 : instantiate_pending_templates (retries);
5322 : 135863 : ggc_collect ();
5323 : :
5324 : 135863 : if (header_module_p ())
5325 : : /* A header modules initializations are handled in its
5326 : : importer. */
5327 : 751 : continue;
5328 : :
5329 : : /* Write out virtual tables as required. Writing out the
5330 : : virtual table for a template class may cause the
5331 : : instantiation of members of that class. If we write out
5332 : : vtables then we remove the class from our list so we don't
5333 : : have to look at it again. */
5334 : 135112 : tree t;
5335 : 135112 : for (i = keyed_classes->length ();
5336 : 3516014 : keyed_classes->iterate (--i, &t);)
5337 : 3380902 : if (maybe_emit_vtables (t, consteval_vtables))
5338 : : {
5339 : 269284 : reconsider = true;
5340 : 269284 : keyed_classes->unordered_remove (i);
5341 : : }
5342 : : /* The input_location may have been changed during marking of
5343 : : vtable entries. */
5344 : 135112 : input_location = locus_at_end_of_parsing;
5345 : :
5346 : : /* Write out needed type info variables. We have to be careful
5347 : : looping through unemitted decls, because emit_tinfo_decl may
5348 : : cause other variables to be needed. New elements will be
5349 : : appended, and we remove from the vector those that actually
5350 : : get emitted. */
5351 : 135112 : for (i = unemitted_tinfo_decls->length ();
5352 : 5763341 : unemitted_tinfo_decls->iterate (--i, &t);)
5353 : 5628229 : if (DECL_INITIAL (t) || emit_tinfo_decl (t))
5354 : : {
5355 : 355612 : reconsider = true;
5356 : 355612 : unemitted_tinfo_decls->unordered_remove (i);
5357 : : }
5358 : :
5359 : : /* The list of objects with static storage duration is built up
5360 : : in reverse order. We clear STATIC_AGGREGATES so that any new
5361 : : aggregates added during the initialization of these will be
5362 : : initialized in the correct order when we next come around the
5363 : : loop. */
5364 : 135112 : if (tree vars = prune_vars_needing_no_initialization (&static_aggregates))
5365 : : {
5366 : 4982 : if (flag_openmp)
5367 : : /* Add initializer information from VARS into
5368 : : DYNAMIC_INITIALIZERS. */
5369 : 349 : for (t = vars; t; t = TREE_CHAIN (t))
5370 : 510 : hash_map_safe_put<hm_ggc> (dynamic_initializers,
5371 : 255 : TREE_VALUE (t), TREE_PURPOSE (t));
5372 : :
5373 : : /* Make sure the back end knows about all the variables. */
5374 : 4982 : write_out_vars (vars);
5375 : :
5376 : 4982 : function_depth++; // Disable GC
5377 : 4982 : priority_map_t *parts[4] = {nullptr, nullptr, nullptr, nullptr};
5378 : 4982 : partition_vars_for_init_fini (vars, parts);
5379 : 4982 : tree host_init_fini[2] = { NULL_TREE, NULL_TREE };
5380 : :
5381 : 14946 : for (unsigned initp = 2; initp--;)
5382 : 9964 : if (parts[initp])
5383 : 14966 : for (auto iter : *parts[initp])
5384 : : {
5385 : 4996 : auto list = iter.second;
5386 : 4996 : if (initp)
5387 : : // Partitioning kept the vars in reverse order.
5388 : : // We only want that for dtors.
5389 : 4987 : list = nreverse (list);
5390 : 4996 : host_init_fini[initp]
5391 : 4996 : = emit_partial_init_fini_fn (initp, iter.first, list,
5392 : : ssdf_count++,
5393 : : locus_at_end_of_parsing,
5394 : : NULL_TREE);
5395 : : }
5396 : :
5397 : 4982 : if (flag_openmp)
5398 : : {
5399 : : priority_map_t **omp_parts = parts + 2;
5400 : 282 : for (unsigned initp = 2; initp--;)
5401 : 188 : if (omp_parts[initp])
5402 : 24 : for (auto iter : *omp_parts[initp])
5403 : : {
5404 : 6 : auto list = iter.second;
5405 : 6 : if (initp)
5406 : : // Partitioning kept the vars in reverse order.
5407 : : // We only want that for dtors.
5408 : 6 : list = nreverse (list);
5409 : 6 : emit_partial_init_fini_fn (initp, iter.first, list,
5410 : : omp_ssdf_count++,
5411 : : locus_at_end_of_parsing,
5412 : : host_init_fini[initp]);
5413 : : }
5414 : : }
5415 : :
5416 : 4982 : function_depth--; // Re-enable GC
5417 : :
5418 : : /* All those initializations and finalizations might cause
5419 : : us to need more inline functions, more template
5420 : : instantiations, etc. */
5421 : 4982 : reconsider = true;
5422 : : }
5423 : :
5424 : : /* Now do the same for thread_local variables. */
5425 : 135112 : handle_tls_init ();
5426 : :
5427 : : /* Go through the set of inline functions whose bodies have not
5428 : : been emitted yet. If out-of-line copies of these functions
5429 : : are required, emit them. */
5430 : 138892480 : FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5431 : : {
5432 : : /* Does it need synthesizing? */
5433 : 143540862 : if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
5434 : 138757374 : && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
5435 : : {
5436 : : /* Even though we're already at the top-level, we push
5437 : : there again. That way, when we pop back a few lines
5438 : : hence, all of our state is restored. Otherwise,
5439 : : finish_function doesn't clean things up, and we end
5440 : : up with CURRENT_FUNCTION_DECL set. */
5441 : 6 : push_to_top_level ();
5442 : : /* The decl's location will mark where it was first
5443 : : needed. Save that so synthesize method can indicate
5444 : : where it was needed from, in case of error */
5445 : 6 : input_location = DECL_SOURCE_LOCATION (decl);
5446 : 6 : synthesize_method (decl);
5447 : 6 : pop_from_top_level ();
5448 : 6 : reconsider = true;
5449 : : }
5450 : :
5451 : 138757368 : if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
5452 : 429 : generate_tls_wrapper (decl);
5453 : :
5454 : 138757368 : if (!DECL_SAVED_TREE (decl))
5455 : 8709178 : continue;
5456 : :
5457 : 130048190 : cgraph_node *node = cgraph_node::get_create (decl);
5458 : :
5459 : : /* We lie to the back end, pretending that some functions
5460 : : are not defined when they really are. This keeps these
5461 : : functions from being put out unnecessarily. But, we must
5462 : : stop lying when the functions are referenced, or if they
5463 : : are not comdat since they need to be put out now. If
5464 : : DECL_INTERFACE_KNOWN, then we have already set
5465 : : DECL_EXTERNAL appropriately, so there's no need to check
5466 : : again, and we do not want to clear DECL_EXTERNAL if a
5467 : : previous call to import_export_decl set it.
5468 : :
5469 : : This is done in a separate for cycle, because if some
5470 : : deferred function is contained in another deferred
5471 : : function later in deferred_fns varray,
5472 : : rest_of_compilation would skip this function and we
5473 : : really cannot expand the same function twice. */
5474 : 130048190 : import_export_decl (decl);
5475 : 130048190 : if (DECL_NOT_REALLY_EXTERN (decl)
5476 : 125534669 : && DECL_INITIAL (decl)
5477 : 255582859 : && decl_needed_p (decl))
5478 : : {
5479 : 77808350 : if (node->cpp_implicit_alias)
5480 : 6628596 : node = node->get_alias_target ();
5481 : :
5482 : 77808350 : node->call_for_symbol_thunks_and_aliases (clear_decl_external,
5483 : : NULL, true);
5484 : : /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
5485 : : group, we need to mark all symbols in the same comdat group
5486 : : that way. */
5487 : 77808350 : if (node->same_comdat_group)
5488 : 21874630 : for (cgraph_node *next
5489 : 10749214 : = dyn_cast<cgraph_node *> (node->same_comdat_group);
5490 : 21874630 : next != node;
5491 : 33000046 : next = dyn_cast<cgraph_node *> (next->same_comdat_group))
5492 : 11125416 : next->call_for_symbol_thunks_and_aliases (clear_decl_external,
5493 : : NULL, true);
5494 : : }
5495 : :
5496 : : /* If we're going to need to write this function out, and
5497 : : there's already a body for it, create RTL for it now.
5498 : : (There might be no body if this is a method we haven't
5499 : : gotten around to synthesizing yet.) */
5500 : 130048190 : if (!DECL_EXTERNAL (decl)
5501 : 79092185 : && decl_needed_p (decl)
5502 : 78479922 : && !TREE_ASM_WRITTEN (decl)
5503 : 153201494 : && !DECL_IMMEDIATE_FUNCTION_P (decl)
5504 : 206017602 : && !node->definition)
5505 : : {
5506 : : /* We will output the function; no longer consider it in this
5507 : : loop. */
5508 : 0 : DECL_DEFER_OUTPUT (decl) = 0;
5509 : : /* Generate RTL for this function now that we know we
5510 : : need it. */
5511 : 0 : expand_or_defer_fn (decl);
5512 : 0 : reconsider = true;
5513 : : }
5514 : : }
5515 : :
5516 : 135112 : if (wrapup_namespace_globals ())
5517 : 398 : reconsider = true;
5518 : :
5519 : : /* Static data members are just like namespace-scope globals. */
5520 : 49806368 : FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
5521 : : {
5522 : 63230874 : if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
5523 : : /* Don't write it out if we haven't seen a definition. */
5524 : 53963638 : || DECL_IN_AGGR_P (decl))
5525 : 45387046 : continue;
5526 : 4284210 : import_export_decl (decl);
5527 : : /* If this static data member is needed, provide it to the
5528 : : back end. */
5529 : 4284210 : if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
5530 : 4248598 : DECL_EXTERNAL (decl) = 0;
5531 : : }
5532 : :
5533 : 270975 : if (vec_safe_length (pending_statics) != 0
5534 : 111076 : && wrapup_global_declarations (pending_statics->address (),
5535 : 55538 : pending_statics->length ()))
5536 : : reconsider = true;
5537 : : }
5538 : :
5539 : : /* All templates have been instantiated. */
5540 : 90704 : at_eof = 2;
5541 : :
5542 : 90704 : void *module_cookie = finish_module_processing (parse_in);
5543 : :
5544 : 90704 : lower_var_init ();
5545 : :
5546 : 90704 : generate_mangling_aliases ();
5547 : :
5548 : : /* All used inline functions must have a definition at this point. */
5549 : 35987088 : FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5550 : : {
5551 : 35896384 : if (/* Check online inline functions that were actually used. */
5552 : 57422046 : DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
5553 : : /* If the definition actually was available here, then the
5554 : : fact that the function was not defined merely represents
5555 : : that for some reason (use of a template repository,
5556 : : #pragma interface, etc.) we decided not to emit the
5557 : : definition here. */
5558 : 21523468 : && !DECL_INITIAL (decl)
5559 : : /* A defaulted fn or TLS wrapper in a header module can be
5560 : : synthesized on demand later. (In non-header modules we
5561 : : should have synthesized it above.) */
5562 : 511 : && !(header_module_p ()
5563 : 3 : && (DECL_DEFAULTED_FN (decl) || decl_tls_wrapper_p (decl)))
5564 : : /* Don't complain if the template was defined. */
5565 : 508 : && !((DECL_TEMPLATE_INSTANTIATION (decl)
5566 : 64 : || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
5567 : 450 : && DECL_INITIAL (DECL_TEMPLATE_RESULT
5568 : : (template_for_substitution (decl))))
5569 : 35896461 : && warning_at (DECL_SOURCE_LOCATION (decl), 0,
5570 : : "inline function %qD used but never defined", decl))
5571 : : /* Avoid a duplicate warning from check_global_declaration. */
5572 : 62 : suppress_warning (decl, OPT_Wunused);
5573 : : }
5574 : :
5575 : : /* So must decls that use a type with no linkage. */
5576 : 420106 : FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
5577 : 329402 : no_linkage_error (decl);
5578 : :
5579 : 90704 : maybe_warn_sized_delete ();
5580 : :
5581 : : // Place the init fns in the right order. We need to do this now,
5582 : : // so that any module init will go at the start.
5583 : 90704 : if (static_init_fini_fns[true])
5584 : 14930 : for (auto iter : *static_init_fini_fns[true])
5585 : 4984 : iter.second = nreverse (iter.second);
5586 : :
5587 : 90704 : if (flag_openmp && static_init_fini_fns[2 + true])
5588 : 18 : for (auto iter : *static_init_fini_fns[2 + true])
5589 : 6 : iter.second = nreverse (iter.second);
5590 : :
5591 : : /* Now we've instantiated all templates. Now we can escalate the functions
5592 : : we squirreled away earlier. */
5593 : 90704 : process_and_check_pending_immediate_escalating_fns ();
5594 : :
5595 : : /* Then, do the Objective-C stuff. This is where all the
5596 : : Objective-C module stuff gets generated (symtab,
5597 : : class/protocol/selector lists etc). This must be done after C++
5598 : : templates, destructors etc. so that selectors used in C++
5599 : : templates are properly allocated. */
5600 : 90704 : if (c_dialect_objc ())
5601 : 0 : objc_write_global_declarations ();
5602 : :
5603 : 90704 : bool has_module_inits = module_determine_import_inits ();
5604 : 90704 : bool has_objc_init = c_dialect_objc () && objc_static_init_needed_p ();
5605 : 90704 : if (has_module_inits || has_objc_init)
5606 : : {
5607 : 28 : input_location = locus_at_end_of_parsing;
5608 : 28 : tree body = start_partial_init_fini_fn (true, DEFAULT_INIT_PRIORITY,
5609 : : ssdf_count++, false);
5610 : : /* For Objective-C++, we may need to initialize metadata found
5611 : : in this module. This must be done _before_ any other static
5612 : : initializations. */
5613 : 28 : if (has_objc_init)
5614 : 0 : objc_generate_static_init_call (NULL_TREE);
5615 : 28 : if (has_module_inits)
5616 : 28 : module_add_import_initializers ();
5617 : 28 : input_location = locus_at_end_of_parsing;
5618 : 28 : finish_partial_init_fini_fn (body);
5619 : : }
5620 : :
5621 : 90704 : if (module_global_init_needed ())
5622 : : {
5623 : : // Make sure there's a default priority entry.
5624 : 1378 : if (!static_init_fini_fns[true])
5625 : 1348 : static_init_fini_fns[true] = priority_map_t::create_ggc ();
5626 : 1378 : if (static_init_fini_fns[true]->get_or_insert (DEFAULT_INIT_PRIORITY))
5627 : 30 : has_module_inits = true;
5628 : :
5629 : 1378 : if (flag_openmp)
5630 : : {
5631 : 6 : if (!static_init_fini_fns[2 + true])
5632 : 6 : static_init_fini_fns[2 + true] = priority_map_t::create_ggc ();
5633 : 6 : static_init_fini_fns[2 + true]->get_or_insert (DEFAULT_INIT_PRIORITY);
5634 : : }
5635 : : }
5636 : :
5637 : : /* Generate initialization and destruction functions for all
5638 : : priorities for which they are required. They have C-language
5639 : : linkage. */
5640 : 90704 : push_lang_context (lang_name_c);
5641 : 453520 : for (unsigned initp = 4; initp--;)
5642 : 362816 : if (static_init_fini_fns[initp])
5643 : : {
5644 : 12733 : for (auto iter : *static_init_fini_fns[initp])
5645 : 6372 : generate_ctor_or_dtor_function (initp & 1, iter.first, iter.second,
5646 : : locus_at_end_of_parsing,
5647 : 6372 : (initp & 2) != 0);
5648 : 6361 : static_init_fini_fns[initp] = nullptr;
5649 : : }
5650 : 90704 : pop_lang_context ();
5651 : :
5652 : 90704 : fini_modules (parse_in, module_cookie, has_module_inits);
5653 : :
5654 : : /* Generate any missing aliases. */
5655 : 90704 : maybe_apply_pending_pragma_weaks ();
5656 : :
5657 : 90704 : if (flag_vtable_verify)
5658 : : {
5659 : 9 : vtv_recover_class_info ();
5660 : 9 : vtv_compute_class_hierarchy_transitive_closure ();
5661 : 9 : vtv_build_vtable_verify_fndecl ();
5662 : : }
5663 : :
5664 : 90704 : perform_deferred_noexcept_checks ();
5665 : :
5666 : 90704 : fini_constexpr ();
5667 : 90704 : cp_tree_c_finish_parsing ();
5668 : 90704 : clear_consteval_vfns (consteval_vtables);
5669 : :
5670 : : /* The entire file is now complete. If requested, dump everything
5671 : : to a file. */
5672 : 90704 : dump_tu ();
5673 : :
5674 : 90704 : if (flag_detailed_statistics)
5675 : : {
5676 : 0 : dump_tree_statistics ();
5677 : 0 : dump_time_statistics ();
5678 : : }
5679 : :
5680 : 90704 : timevar_stop (TV_PHASE_DEFERRED);
5681 : 90704 : timevar_start (TV_PHASE_PARSING);
5682 : :
5683 : : /* Indicate that we're done with front end processing. */
5684 : 90704 : at_eof = 3;
5685 : 90704 : }
5686 : :
5687 : : /* Perform any post compilation-proper cleanups for the C++ front-end.
5688 : : This should really go away. No front-end should need to do
5689 : : anything past the compilation process. */
5690 : :
5691 : : void
5692 : 90606 : cxx_post_compilation_parsing_cleanups (void)
5693 : : {
5694 : 90606 : timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5695 : :
5696 : 90606 : if (flag_vtable_verify)
5697 : : {
5698 : : /* Generate the special constructor initialization function that
5699 : : calls __VLTRegisterPairs, and give it a very high
5700 : : initialization priority. This must be done after
5701 : : finalize_compilation_unit so that we have accurate
5702 : : information about which vtable will actually be emitted. */
5703 : 9 : vtv_generate_init_routine ();
5704 : : }
5705 : :
5706 : 90606 : input_location = locus_at_end_of_parsing;
5707 : :
5708 : 90606 : if (flag_checking)
5709 : 90597 : validate_conversion_obstack ();
5710 : :
5711 : 90606 : timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5712 : 90606 : }
5713 : :
5714 : : /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5715 : : function to call in parse-tree form; it has not yet been
5716 : : semantically analyzed. ARGS are the arguments to the function.
5717 : : They have already been semantically analyzed. This may change
5718 : : ARGS. */
5719 : :
5720 : : tree
5721 : 349841 : build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5722 : : tsubst_flags_t complain)
5723 : : {
5724 : 349841 : tree orig_fn;
5725 : 349841 : vec<tree, va_gc> *orig_args = NULL;
5726 : 349841 : tree expr;
5727 : 349841 : tree object;
5728 : :
5729 : 349841 : orig_fn = fn;
5730 : 349841 : object = TREE_OPERAND (fn, 0);
5731 : :
5732 : 349841 : if (processing_template_decl)
5733 : : {
5734 : 263676 : gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5735 : : || TREE_CODE (fn) == MEMBER_REF);
5736 : 263676 : if (type_dependent_expression_p (fn)
5737 : 263676 : || any_type_dependent_arguments_p (*args))
5738 : 263637 : return build_min_nt_call_vec (fn, *args);
5739 : :
5740 : 39 : orig_args = make_tree_vector_copy (*args);
5741 : :
5742 : : /* Transform the arguments and add the implicit "this"
5743 : : parameter. */
5744 : 39 : if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5745 : : {
5746 : 33 : if (TREE_CODE (fn) == DOTSTAR_EXPR)
5747 : 27 : object = cp_build_addr_expr (object, complain);
5748 : 33 : vec_safe_insert (*args, 0, object);
5749 : : }
5750 : : }
5751 : :
5752 : : /* A qualified name corresponding to a bound pointer-to-member is
5753 : : represented as an OFFSET_REF:
5754 : :
5755 : : struct B { void g(); };
5756 : : void (B::*p)();
5757 : : void B::g() { (this->*p)(); } */
5758 : 86204 : if (TREE_CODE (fn) == OFFSET_REF)
5759 : : {
5760 : 86165 : tree object_addr = cp_build_addr_expr (object, complain);
5761 : 86165 : fn = TREE_OPERAND (fn, 1);
5762 : 86165 : fn = get_member_function_from_ptrfunc (&object_addr, fn,
5763 : : complain);
5764 : 86165 : vec_safe_insert (*args, 0, object_addr);
5765 : : }
5766 : :
5767 : 86204 : if (CLASS_TYPE_P (TREE_TYPE (fn)))
5768 : 3 : expr = build_op_call (fn, args, complain);
5769 : : else
5770 : 86201 : expr = cp_build_function_call_vec (fn, args, complain);
5771 : 86204 : if (processing_template_decl && expr != error_mark_node)
5772 : 39 : expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5773 : :
5774 : 86204 : if (orig_args != NULL)
5775 : 39 : release_tree_vector (orig_args);
5776 : :
5777 : : return expr;
5778 : : }
5779 : :
5780 : :
5781 : : void
5782 : 324081241 : check_default_args (tree x)
5783 : : {
5784 : 324081241 : tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5785 : 324081241 : bool saw_def = false;
5786 : 324081241 : bool noted_first_def = false;
5787 : 324081241 : int idx_of_first_default_arg = 0;
5788 : 324081241 : location_t loc_of_first_default_arg = UNKNOWN_LOCATION;
5789 : 324081241 : int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5790 : 324081241 : tree fndecl = STRIP_TEMPLATE (x);
5791 : 324081241 : auto_diagnostic_group d;
5792 : 885167955 : for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5793 : : {
5794 : 561086714 : if (TREE_PURPOSE (arg))
5795 : : {
5796 : 8388621 : if (!saw_def)
5797 : : {
5798 : 6578871 : saw_def = true;
5799 : 6578871 : idx_of_first_default_arg = i;
5800 : 6578871 : location_t loc = get_fndecl_argument_location (fndecl, i);
5801 : 6578871 : if (loc != DECL_SOURCE_LOCATION (x))
5802 : 6578845 : loc_of_first_default_arg = loc;
5803 : : }
5804 : : }
5805 : 552698093 : else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5806 : : {
5807 : 147 : error_at (get_fndecl_argument_location (fndecl, i),
5808 : : "default argument missing for parameter %P of %q#D", i, x);
5809 : 147 : if (loc_of_first_default_arg != UNKNOWN_LOCATION
5810 : 147 : && !noted_first_def)
5811 : : {
5812 : 144 : inform (loc_of_first_default_arg,
5813 : : "...following parameter %P which has a default argument",
5814 : : idx_of_first_default_arg);
5815 : 144 : noted_first_def = true;
5816 : : }
5817 : 147 : TREE_PURPOSE (arg) = error_mark_node;
5818 : : }
5819 : : }
5820 : 324081241 : }
5821 : :
5822 : : /* Return true if function DECL can be inlined. This is used to force
5823 : : instantiation of methods that might be interesting for inlining. */
5824 : : bool
5825 : 3 : possibly_inlined_p (tree decl)
5826 : : {
5827 : 3 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5828 : 3 : if (DECL_UNINLINABLE (decl))
5829 : : return false;
5830 : 3 : if (!optimize)
5831 : 3 : return DECL_DECLARED_INLINE_P (decl);
5832 : : /* When optimizing, we might inline everything when flatten
5833 : : attribute or heuristics inlining for size or autoinlining
5834 : : is used. */
5835 : : return true;
5836 : : }
5837 : :
5838 : : /* If DECL is a function or variable template specialization, instantiate
5839 : : its definition now. */
5840 : :
5841 : : void
5842 : 86798672 : maybe_instantiate_decl (tree decl)
5843 : : {
5844 : 350162 : if (VAR_OR_FUNCTION_DECL_P (decl)
5845 : 86798672 : && DECL_LANG_SPECIFIC (decl)
5846 : 80887892 : && DECL_TEMPLATE_INFO (decl)
5847 : 152289102 : && !uses_template_parms (DECL_TI_ARGS (decl)))
5848 : : {
5849 : : /* Instantiating a function will result in garbage collection. We
5850 : : must treat this situation as if we were within the body of a
5851 : : function so as to avoid collecting live data only referenced from
5852 : : the stack (such as overload resolution candidates). */
5853 : 61603392 : ++function_depth;
5854 : 61603392 : instantiate_decl (decl, /*defer_ok=*/false,
5855 : : /*expl_inst_class_mem_p=*/false);
5856 : 61600695 : --function_depth;
5857 : : }
5858 : 86795975 : }
5859 : :
5860 : : /* Error if the DECL is unavailable (unless this is currently suppressed).
5861 : : Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns true if
5862 : : an error or warning was emitted. */
5863 : :
5864 : : bool
5865 : 3224999633 : cp_handle_deprecated_or_unavailable (tree decl, tsubst_flags_t complain)
5866 : : {
5867 : 3224999633 : if (!decl)
5868 : : return false;
5869 : :
5870 : 3192071008 : if ((complain & tf_error)
5871 : 2972719946 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
5872 : : {
5873 : 2350214739 : if (TREE_UNAVAILABLE (decl))
5874 : : {
5875 : 237 : error_unavailable_use (decl, NULL_TREE);
5876 : 237 : return true;
5877 : : }
5878 : : else
5879 : : {
5880 : : /* Perhaps this is an unavailable typedef. */
5881 : 2350214502 : if (TYPE_P (decl)
5882 : 651820708 : && TYPE_NAME (decl)
5883 : 2998102574 : && TREE_UNAVAILABLE (TYPE_NAME (decl)))
5884 : : {
5885 : 6 : decl = TYPE_NAME (decl);
5886 : : /* Don't error within members of a unavailable type. */
5887 : 6 : if (TYPE_P (decl)
5888 : 6 : && currently_open_class (decl))
5889 : : return false;
5890 : :
5891 : 6 : error_unavailable_use (decl, NULL_TREE);
5892 : 6 : return true;
5893 : : }
5894 : : }
5895 : : /* Carry on to consider deprecatedness. */
5896 : : }
5897 : :
5898 : 3192070765 : if (!(complain & tf_warning)
5899 : 2950463846 : || deprecated_state == DEPRECATED_SUPPRESS
5900 : 2949024632 : || deprecated_state == UNAVAILABLE_DEPRECATED_SUPPRESS)
5901 : : return false;
5902 : :
5903 : 2330024247 : if (!TREE_DEPRECATED (decl))
5904 : : {
5905 : : /* Perhaps this is a deprecated typedef. */
5906 : 2329492560 : if (TYPE_P (decl) && TYPE_NAME (decl))
5907 : 646731120 : decl = TYPE_NAME (decl);
5908 : :
5909 : 2329492560 : if (!TREE_DEPRECATED (decl))
5910 : : return false;
5911 : : }
5912 : :
5913 : : /* Don't warn within members of a deprecated type. */
5914 : 531693 : if (TYPE_P (decl)
5915 : 531693 : && currently_open_class (decl))
5916 : : return false;
5917 : :
5918 : 40838 : bool warned = false;
5919 : 40838 : if (cxx_dialect >= cxx11
5920 : 40750 : && DECL_P (decl)
5921 : 40663 : && DECL_ARTIFICIAL (decl)
5922 : 10728 : && DECL_IOBJ_MEMBER_FUNCTION_P (decl)
5923 : 51566 : && copy_fn_p (decl))
5924 : : {
5925 : : /* Don't warn if the flag was disabled around the class definition
5926 : : (c++/94492). */
5927 : 10716 : if (warning_enabled_at (DECL_SOURCE_LOCATION (decl),
5928 : : OPT_Wdeprecated_copy))
5929 : : {
5930 : 4 : auto_diagnostic_group d;
5931 : 4 : tree ctx = DECL_CONTEXT (decl);
5932 : 4 : tree other = classtype_has_depr_implicit_copy (ctx);
5933 : 4 : int opt = (DECL_DESTRUCTOR_P (other)
5934 : 4 : ? OPT_Wdeprecated_copy_dtor
5935 : 4 : : OPT_Wdeprecated_copy);
5936 : 4 : warned = warning (opt, "implicitly-declared %qD is deprecated",
5937 : : decl);
5938 : 4 : if (warned)
5939 : 4 : inform (DECL_SOURCE_LOCATION (other),
5940 : : "because %qT has user-provided %qD",
5941 : : ctx, other);
5942 : 4 : }
5943 : : }
5944 : : else
5945 : 30122 : warned = warn_deprecated_use (decl, NULL_TREE);
5946 : :
5947 : : return warned;
5948 : : }
5949 : :
5950 : : /* Like above, but takes into account outer scopes. */
5951 : :
5952 : : void
5953 : 1275396035 : cp_warn_deprecated_use_scopes (tree scope)
5954 : : {
5955 : 1275396035 : while (scope
5956 : 2197972919 : && scope != error_mark_node
5957 : 4395945806 : && scope != global_namespace)
5958 : : {
5959 : 219172965 : if ((TREE_CODE (scope) == NAMESPACE_DECL || OVERLOAD_TYPE_P (scope))
5960 : 1127806824 : && cp_handle_deprecated_or_unavailable (scope))
5961 : : return;
5962 : 922576884 : if (TYPE_P (scope))
5963 : 206924559 : scope = CP_TYPE_CONTEXT (scope);
5964 : : else
5965 : 715652325 : scope = CP_DECL_CONTEXT (scope);
5966 : : }
5967 : : }
5968 : :
5969 : : /* True if DECL or its enclosing scope have unbound template parameters. */
5970 : :
5971 : : bool
5972 : 585803456 : decl_dependent_p (tree decl)
5973 : : {
5974 : 1163151823 : if (DECL_FUNCTION_SCOPE_P (decl)
5975 : 76932516 : || TREE_CODE (decl) == CONST_DECL
5976 : 50556221 : || TREE_CODE (decl) == USING_DECL
5977 : 636359671 : || TREE_CODE (decl) == FIELD_DECL)
5978 : 575555500 : decl = CP_DECL_CONTEXT (decl);
5979 : 585803456 : if (tree tinfo = get_template_info (decl))
5980 : 495058720 : if (any_dependent_template_arguments_p (TI_ARGS (tinfo)))
5981 : : return true;
5982 : 252759552 : if (LAMBDA_FUNCTION_P (decl)
5983 : 251597691 : && dependent_type_p (DECL_CONTEXT (decl)))
5984 : : return true;
5985 : : return false;
5986 : : }
5987 : :
5988 : : /* [basic.def.odr] A function is named [and therefore odr-used] by an
5989 : : expression or conversion if it is the selected member of an overload set in
5990 : : an overload resolution performed as part of forming that expression or
5991 : : conversion, unless it is a pure virtual function and either the expression
5992 : : is not an id-expression naming the function with an explicitly qualified
5993 : : name or the expression forms a pointer to member.
5994 : :
5995 : : Mostly, we call mark_used in places that actually do something with a
5996 : : function, like build_over_call. But in a few places we end up with a
5997 : : non-overloaded FUNCTION_DECL that we aren't going to do any more with, like
5998 : : convert_to_void. resolve_nondeduced_context is called in those places,
5999 : : but it's also called in too many other places. */
6000 : :
6001 : : bool
6002 : 302791613 : mark_single_function (tree expr, tsubst_flags_t complain)
6003 : : {
6004 : 302791613 : expr = maybe_undo_parenthesized_ref (expr);
6005 : 302791613 : expr = tree_strip_any_location_wrapper (expr);
6006 : :
6007 : 302791613 : if (is_overloaded_fn (expr) == 1
6008 : 86655055 : && !mark_used (expr, complain)
6009 : 302791637 : && !(complain & tf_error))
6010 : : return false;
6011 : : return true;
6012 : : }
6013 : :
6014 : : /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
6015 : : If DECL is a specialization or implicitly declared class member,
6016 : : generate the actual definition. Return false if something goes
6017 : : wrong, true otherwise. */
6018 : :
6019 : : bool
6020 : 993853529 : mark_used (tree decl, tsubst_flags_t complain /* = tf_warning_or_error */)
6021 : : {
6022 : : /* If we're just testing conversions or resolving overloads, we
6023 : : don't want any permanent effects like forcing functions to be
6024 : : output or instantiating templates. */
6025 : 993853529 : if ((complain & tf_conv))
6026 : : return true;
6027 : :
6028 : : /* If DECL is a BASELINK for a single function, then treat it just
6029 : : like the DECL for the function. Otherwise, if the BASELINK is
6030 : : for an overloaded function, we don't know which function was
6031 : : actually used until after overload resolution. */
6032 : 992045329 : if (BASELINK_P (decl))
6033 : : {
6034 : 13358 : tree fns = BASELINK_FUNCTIONS (decl);
6035 : 13358 : if (really_overloaded_fn (fns))
6036 : : return true;
6037 : 13343 : fns = OVL_FIRST (fns);
6038 : 13343 : if (!mark_used (fns, complain))
6039 : : return false;
6040 : : /* We might have deduced its return type. */
6041 : 13337 : TREE_TYPE (decl) = TREE_TYPE (fns);
6042 : 13337 : return true;
6043 : : }
6044 : :
6045 : 992031971 : if (!DECL_P (decl))
6046 : : return true;
6047 : :
6048 : : /* Set TREE_USED for the benefit of -Wunused. */
6049 : 991193092 : TREE_USED (decl) = true;
6050 : :
6051 : : /* And for structured bindings also the underlying decl. */
6052 : 991193092 : if (DECL_DECOMPOSITION_P (decl) && !DECL_DECOMP_IS_BASE (decl))
6053 : 268627 : TREE_USED (DECL_DECOMP_BASE (decl)) = true;
6054 : :
6055 : 991193092 : if (TREE_CODE (decl) == TEMPLATE_DECL)
6056 : : return true;
6057 : :
6058 : 991182958 : if (DECL_CLONED_FUNCTION_P (decl))
6059 : 15471301 : TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
6060 : :
6061 : : /* Mark enumeration types as used. */
6062 : 991182958 : if (TREE_CODE (decl) == CONST_DECL)
6063 : 26376295 : used_types_insert (DECL_CONTEXT (decl));
6064 : :
6065 : 991182958 : if (TREE_CODE (decl) == FUNCTION_DECL)
6066 : : {
6067 : 211223478 : if (DECL_MAYBE_DELETED (decl))
6068 : : {
6069 : 7473 : ++function_depth;
6070 : 7473 : maybe_synthesize_method (decl);
6071 : 7473 : --function_depth;
6072 : : }
6073 : :
6074 : 211223478 : if (DECL_DELETED_FN (decl))
6075 : : {
6076 : 34805 : if (DECL_ARTIFICIAL (decl)
6077 : 1622 : && DECL_CONV_FN_P (decl)
6078 : 34817 : && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
6079 : : /* We mark a lambda conversion op as deleted if we can't
6080 : : generate it properly; see maybe_add_lambda_conv_op. */
6081 : 6 : sorry ("converting lambda that uses %<...%> to function pointer");
6082 : 34799 : else if (complain & tf_error)
6083 : : {
6084 : 2257 : auto_diagnostic_group d;
6085 : 2257 : if (DECL_INITIAL (decl)
6086 : 2257 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6087 : : {
6088 : 21 : escaped_string msg;
6089 : 21 : msg.escape (TREE_STRING_POINTER (DECL_INITIAL (decl)));
6090 : 21 : error ("use of deleted function %qD: %s",
6091 : : decl, (const char *) msg);
6092 : 21 : }
6093 : : else
6094 : 2236 : error ("use of deleted function %qD", decl);
6095 : 2257 : if (!maybe_explain_implicit_delete (decl))
6096 : 564 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
6097 : 2257 : }
6098 : 34805 : return false;
6099 : : }
6100 : :
6101 : 211188673 : if (!maybe_instantiate_noexcept (decl, complain))
6102 : : return false;
6103 : : }
6104 : :
6105 : 991148056 : if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl))
6106 : : {
6107 : 52491 : if (!DECL_LANG_SPECIFIC (decl))
6108 : : /* An unresolved dependent local extern. */
6109 : : return true;
6110 : :
6111 : 52491 : DECL_ODR_USED (decl) = 1;
6112 : 52491 : auto alias = DECL_LOCAL_DECL_ALIAS (decl);
6113 : 52491 : if (!alias || alias == error_mark_node)
6114 : : return true;
6115 : :
6116 : : /* Process the underlying decl. */
6117 : 52202 : decl = alias;
6118 : 52202 : TREE_USED (decl) = true;
6119 : : }
6120 : :
6121 : 991147767 : cp_handle_deprecated_or_unavailable (decl, complain);
6122 : :
6123 : : /* We can only check DECL_ODR_USED on variables or functions with
6124 : : DECL_LANG_SPECIFIC set, and these are also the only decls that we
6125 : : might need special handling for. */
6126 : 575337151 : if (!VAR_OR_FUNCTION_DECL_P (decl)
6127 : 626999173 : || DECL_LANG_SPECIFIC (decl) == NULL
6128 : 1396500461 : || DECL_THUNK_P (decl))
6129 : : {
6130 : 585803456 : if (!decl_dependent_p (decl)
6131 : 585803456 : && !require_deduced_type (decl, complain))
6132 : : return false;
6133 : 585803432 : return true;
6134 : : }
6135 : :
6136 : : /* We only want to do this processing once. We don't need to keep trying
6137 : : to instantiate inline templates, because unit-at-a-time will make sure
6138 : : we get them compiled before functions that want to inline them. */
6139 : 405344311 : if (DECL_ODR_USED (decl))
6140 : : return true;
6141 : :
6142 : 62920717 : if (flag_concepts && TREE_CODE (decl) == FUNCTION_DECL
6143 : 165502787 : && !constraints_satisfied_p (decl))
6144 : : {
6145 : 15 : if (complain & tf_error)
6146 : : {
6147 : 6 : auto_diagnostic_group d;
6148 : 6 : error ("use of function %qD with unsatisfied constraints",
6149 : : decl);
6150 : 6 : location_t loc = DECL_SOURCE_LOCATION (decl);
6151 : 6 : inform (loc, "declared here");
6152 : 6 : diagnose_constraints (loc, decl, NULL_TREE);
6153 : 6 : }
6154 : 15 : return false;
6155 : : }
6156 : :
6157 : : /* If DECL has a deduced return type, we need to instantiate it now to
6158 : : find out its type. For OpenMP user defined reductions, we need them
6159 : : instantiated for reduction clauses which inline them by hand directly. */
6160 : 118728043 : if (undeduced_auto_decl (decl)
6161 : 118196242 : || (VAR_P (decl)
6162 : 35687245 : && VAR_HAD_UNKNOWN_BOUND (decl))
6163 : 236922696 : || (TREE_CODE (decl) == FUNCTION_DECL
6164 : 82508997 : && DECL_OMP_DECLARE_REDUCTION_P (decl)))
6165 : 533651 : maybe_instantiate_decl (decl);
6166 : :
6167 : 118728043 : if (processing_template_decl || in_template_context)
6168 : : return true;
6169 : :
6170 : : /* Check this too in case we're within instantiate_non_dependent_expr. */
6171 : 82192470 : if (DECL_TEMPLATE_INFO (decl)
6172 : 82192470 : && uses_template_parms (DECL_TI_ARGS (decl)))
6173 : : return true;
6174 : :
6175 : 82192466 : if (!require_deduced_type (decl, complain))
6176 : : return false;
6177 : :
6178 : 82192259 : if (builtin_pack_fn_p (decl))
6179 : : {
6180 : 12 : error ("use of built-in parameter pack %qD outside of a template",
6181 : 6 : DECL_NAME (decl));
6182 : 6 : return false;
6183 : : }
6184 : :
6185 : : /* If we don't need a value, then we don't need to synthesize DECL. */
6186 : 82192253 : if (cp_unevaluated_operand || in_discarded_stmt)
6187 : : return true;
6188 : :
6189 : 35833989 : DECL_ODR_USED (decl) = 1;
6190 : 35833989 : if (DECL_CLONED_FUNCTION_P (decl))
6191 : 5882960 : DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
6192 : :
6193 : : /* DR 757: A type without linkage shall not be used as the type of a
6194 : : variable or function with linkage, unless
6195 : : o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
6196 : : o the variable or function is not used (3.2 [basic.def.odr]) or is
6197 : : defined in the same translation unit. */
6198 : 35833989 : if (cxx_dialect > cxx98
6199 : 35642592 : && decl_linkage (decl) != lk_none
6200 : 33270638 : && !DECL_EXTERN_C_P (decl)
6201 : 28916840 : && !DECL_ARTIFICIAL (decl)
6202 : 27428743 : && !decl_defined_p (decl)
6203 : 56924350 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
6204 : 329450 : vec_safe_push (no_linkage_decls, decl);
6205 : :
6206 : 35833989 : if (TREE_CODE (decl) == FUNCTION_DECL
6207 : 29742608 : && DECL_DECLARED_INLINE_P (decl)
6208 : 21271532 : && !DECL_INITIAL (decl)
6209 : 14366650 : && !DECL_ARTIFICIAL (decl)
6210 : 49686725 : && !DECL_PURE_VIRTUAL_P (decl))
6211 : : /* Remember it, so we can check it was defined. */
6212 : 13848424 : note_vague_linkage_fn (decl);
6213 : :
6214 : : /* Is it a synthesized method that needs to be synthesized? */
6215 : 35833989 : if (TREE_CODE (decl) == FUNCTION_DECL
6216 : 29742608 : && DECL_DEFAULTED_FN (decl)
6217 : : /* A function defaulted outside the class is synthesized either by
6218 : : cp_finish_decl or instantiate_decl. */
6219 : 1233235 : && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
6220 : 37066998 : && ! DECL_INITIAL (decl))
6221 : : {
6222 : : /* Remember the current location for a function we will end up
6223 : : synthesizing. Then we can inform the user where it was
6224 : : required in the case of error. */
6225 : 1055515 : if (decl_remember_implicit_trigger_p (decl))
6226 : 477665 : DECL_SOURCE_LOCATION (decl) = input_location;
6227 : :
6228 : : /* Synthesizing an implicitly defined member function will result in
6229 : : garbage collection. We must treat this situation as if we were
6230 : : within the body of a function so as to avoid collecting live data
6231 : : on the stack (such as overload resolution candidates).
6232 : :
6233 : : We could just let c_parse_final_cleanups handle synthesizing
6234 : : this function by adding it to deferred_fns, but doing
6235 : : it at the use site produces better error messages. */
6236 : 1055515 : ++function_depth;
6237 : 1055515 : synthesize_method (decl);
6238 : 1055515 : --function_depth;
6239 : : /* If this is a synthesized method we don't need to
6240 : : do the instantiation test below. */
6241 : : }
6242 : 28687093 : else if (VAR_OR_FUNCTION_DECL_P (decl)
6243 : 34778474 : && DECL_TEMPLATE_INFO (decl)
6244 : 54472829 : && (!DECL_EXPLICIT_INSTANTIATION (decl)
6245 : 1010946 : || always_instantiate_p (decl)))
6246 : : /* If this is a function or variable that is an instance of some
6247 : : template, we now know that we will need to actually do the
6248 : : instantiation. We check that DECL is not an explicit
6249 : : instantiation because that is not checked in instantiate_decl.
6250 : :
6251 : : We put off instantiating functions in order to improve compile
6252 : : times. Maintaining a stack of active functions is expensive,
6253 : : and the inliner knows to instantiate any functions it might
6254 : : need. Therefore, we always try to defer instantiation. */
6255 : : {
6256 : 19576474 : ++function_depth;
6257 : 19576474 : instantiate_decl (decl, /*defer_ok=*/true,
6258 : : /*expl_inst_class_mem_p=*/false);
6259 : 19568374 : --function_depth;
6260 : : }
6261 : :
6262 : : return true;
6263 : : }
6264 : :
6265 : : tree
6266 : 9 : vtv_start_verification_constructor_init_function (void)
6267 : : {
6268 : 9 : return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, true);
6269 : : }
6270 : :
6271 : : tree
6272 : 6 : vtv_finish_verification_constructor_init_function (tree body)
6273 : : {
6274 : 6 : return finish_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, body);
6275 : : }
6276 : :
6277 : : #include "gt-cp-decl2.h"
|