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 : :
54 : : /* Id for dumping the raw trees. */
55 : : int raw_dump_id;
56 : :
57 : : extern cpp_reader *parse_in;
58 : :
59 : : static tree start_objects (bool, unsigned, bool);
60 : : static tree finish_objects (bool, unsigned, tree, bool = true);
61 : : static tree start_partial_init_fini_fn (bool, unsigned, unsigned);
62 : : static void finish_partial_init_fini_fn (tree);
63 : : static void emit_partial_init_fini_fn (bool, unsigned, tree,
64 : : unsigned, location_t);
65 : : static void one_static_initialization_or_destruction (bool, tree, tree);
66 : : static void generate_ctor_or_dtor_function (bool, unsigned, tree, location_t);
67 : : static tree prune_vars_needing_no_initialization (tree *);
68 : : static void write_out_vars (tree);
69 : : static void import_export_class (tree);
70 : : static tree get_guard_bits (tree);
71 : : static void determine_visibility_from_class (tree, tree);
72 : : static bool determine_hidden_inline (tree);
73 : :
74 : : /* A list of static class variables. This is needed, because a
75 : : static class variable can be declared inside the class without
76 : : an initializer, and then initialized, statically, outside the class. */
77 : : static GTY(()) vec<tree, va_gc> *pending_statics;
78 : :
79 : : /* A list of functions which were declared inline, but which we
80 : : may need to emit outline anyway. */
81 : : static GTY(()) vec<tree, va_gc> *deferred_fns;
82 : :
83 : : /* A list of decls that use types with no linkage, which we need to make
84 : : sure are defined. */
85 : : static GTY(()) vec<tree, va_gc> *no_linkage_decls;
86 : :
87 : : /* A vector of alternating decls and identifiers, where the latter
88 : : is to be an alias for the former if the former is defined. */
89 : : static GTY(()) vec<tree, va_gc> *mangling_aliases;
90 : :
91 : : /* hash traits for declarations. Hashes single decls via
92 : : DECL_ASSEMBLER_NAME_RAW. */
93 : :
94 : : struct mangled_decl_hash : ggc_remove <tree>
95 : : {
96 : : typedef tree value_type; /* A DECL. */
97 : : typedef tree compare_type; /* An identifier. */
98 : :
99 : 370269322 : static hashval_t hash (const value_type decl)
100 : : {
101 : 370269322 : return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
102 : : }
103 : 366381926 : static bool equal (const value_type existing, compare_type candidate)
104 : : {
105 : 366381926 : tree name = DECL_ASSEMBLER_NAME_RAW (existing);
106 : 366381926 : return candidate == name;
107 : : }
108 : :
109 : : static const bool empty_zero_p = true;
110 : 16 : static inline void mark_empty (value_type &p) {p = NULL_TREE;}
111 : : static inline bool is_empty (value_type p) {return !p;}
112 : :
113 : 477152148 : static bool is_deleted (value_type e)
114 : : {
115 : 477152148 : return e == reinterpret_cast <value_type> (1);
116 : : }
117 : 46 : static void mark_deleted (value_type &e)
118 : : {
119 : 46 : e = reinterpret_cast <value_type> (1);
120 : : }
121 : : };
122 : :
123 : : /* A hash table of decls keyed by mangled name. Used to figure out if
124 : : we need compatibility aliases. */
125 : : static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
126 : :
127 : : // Hash table mapping priority to lists of variables or functions.
128 : : struct priority_map_traits
129 : : {
130 : : typedef unsigned key_type;
131 : : typedef tree value_type;
132 : : static const bool maybe_mx = true;
133 : : static hashval_t hash (key_type v)
134 : : {
135 : : return hashval_t (v);
136 : : }
137 : : static bool equal_keys (key_type k1, key_type k2)
138 : : {
139 : : return k1 == k2;
140 : : }
141 : : template <typename T> static void remove (T &)
142 : : {
143 : : }
144 : : // Zero is not a priority
145 : : static const bool empty_zero_p = true;
146 : 472489 : template <typename T> static bool is_empty (const T &entry)
147 : : {
148 : 472489 : return entry.m_key == 0;
149 : : }
150 : 0 : template <typename T> static void mark_empty (T &entry)
151 : : {
152 : 0 : entry.m_key = 0;
153 : : }
154 : : // Entries are not deleteable
155 : : template <typename T> static bool is_deleted (const T &)
156 : : {
157 : : return false;
158 : : }
159 : : template <typename T> static void mark_deleted (T &)
160 : : {
161 : : gcc_unreachable ();
162 : : }
163 : : };
164 : :
165 : : typedef hash_map<unsigned/*Priority*/, tree/*List*/,
166 : : priority_map_traits> priority_map_t;
167 : :
168 : : /* A pair of such hash tables, indexed by initp -- one for fini and
169 : : one for init. The fini table is only ever used when !cxa_atexit. */
170 : : static GTY(()) priority_map_t *static_init_fini_fns[2];
171 : :
172 : : /* Nonzero if we're done parsing and into end-of-file activities.
173 : : 2 if all templates have been instantiated.
174 : : 3 if we're done with front-end processing. */
175 : :
176 : : int at_eof;
177 : :
178 : : /* True if note_mangling_alias should enqueue mangling aliases for
179 : : later generation, rather than emitting them right away. */
180 : :
181 : : bool defer_mangling_aliases = true;
182 : :
183 : :
184 : : /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
185 : : FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
186 : : that apply to the function). */
187 : :
188 : : tree
189 : 85738263 : build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
190 : : cp_ref_qualifier rqual)
191 : : {
192 : 85738263 : if (fntype == error_mark_node || ctype == error_mark_node)
193 : : return error_mark_node;
194 : :
195 : 85738259 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
196 : :
197 : 85738259 : cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
198 : 85738259 : ctype = cp_build_qualified_type (ctype, type_quals);
199 : :
200 : 85738259 : tree newtype
201 : 85738259 : = build_method_type_directly (ctype, TREE_TYPE (fntype),
202 : 85738259 : (TREE_CODE (fntype) == METHOD_TYPE
203 : 60170 : ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
204 : 85678089 : : TYPE_ARG_TYPES (fntype)));
205 : 85738259 : if (tree attrs = TYPE_ATTRIBUTES (fntype))
206 : 80 : newtype = cp_build_type_attribute_variant (newtype, attrs);
207 : 257214777 : newtype = build_cp_fntype_variant (newtype, rqual,
208 : 85738259 : TYPE_RAISES_EXCEPTIONS (fntype),
209 : 85738259 : TYPE_HAS_LATE_RETURN_TYPE (fntype));
210 : :
211 : 85738259 : return newtype;
212 : : }
213 : :
214 : : /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
215 : : return type changed to NEW_RET. */
216 : :
217 : : tree
218 : 551632 : change_return_type (tree new_ret, tree fntype)
219 : : {
220 : 551632 : if (new_ret == error_mark_node)
221 : : return fntype;
222 : :
223 : 551632 : if (same_type_p (new_ret, TREE_TYPE (fntype)))
224 : : return fntype;
225 : :
226 : 551631 : tree newtype;
227 : 551631 : tree args = TYPE_ARG_TYPES (fntype);
228 : :
229 : 551631 : if (TREE_CODE (fntype) == FUNCTION_TYPE)
230 : : {
231 : 99973 : newtype = build_function_type (new_ret, args);
232 : 99973 : newtype = apply_memfn_quals (newtype,
233 : : type_memfn_quals (fntype));
234 : : }
235 : : else
236 : 451658 : newtype = build_method_type_directly
237 : 451658 : (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
238 : :
239 : 551631 : if (tree attrs = TYPE_ATTRIBUTES (fntype))
240 : 116 : newtype = cp_build_type_attribute_variant (newtype, attrs);
241 : 551631 : newtype = cxx_copy_lang_qualifiers (newtype, fntype);
242 : :
243 : 551631 : return newtype;
244 : : }
245 : :
246 : : /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
247 : : appropriately. */
248 : :
249 : : tree
250 : 477985522 : cp_build_parm_decl (tree fn, tree name, tree type)
251 : : {
252 : 477985522 : tree parm = build_decl (input_location,
253 : : PARM_DECL, name, type);
254 : 477985522 : DECL_CONTEXT (parm) = fn;
255 : :
256 : : /* DECL_ARG_TYPE is only used by the back end and the back end never
257 : : sees templates. */
258 : 477985522 : if (!processing_template_decl)
259 : 168551016 : DECL_ARG_TYPE (parm) = type_passed_as (type);
260 : :
261 : 477985522 : return parm;
262 : : }
263 : :
264 : : /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
265 : : indicated NAME. */
266 : :
267 : : tree
268 : 202752561 : build_artificial_parm (tree fn, tree name, tree type)
269 : : {
270 : 202752561 : tree parm = cp_build_parm_decl (fn, name, type);
271 : 202752561 : DECL_ARTIFICIAL (parm) = 1;
272 : : /* All our artificial parms are implicitly `const'; they cannot be
273 : : assigned to. */
274 : 202752561 : TREE_READONLY (parm) = 1;
275 : 202752561 : return parm;
276 : : }
277 : :
278 : : /* Constructors for types with virtual baseclasses need an "in-charge" flag
279 : : saying whether this constructor is responsible for initialization of
280 : : virtual baseclasses or not. All destructors also need this "in-charge"
281 : : flag, which additionally determines whether or not the destructor should
282 : : free the memory for the object.
283 : :
284 : : This function adds the "in-charge" flag to member function FN if
285 : : appropriate. It is called from grokclassfn and tsubst.
286 : : FN must be either a constructor or destructor.
287 : :
288 : : The in-charge flag follows the 'this' parameter, and is followed by the
289 : : VTT parm (if any), then the user-written parms. */
290 : :
291 : : void
292 : 63249241 : maybe_retrofit_in_chrg (tree fn)
293 : : {
294 : 63249241 : tree basetype, arg_types, parms, parm, fntype;
295 : :
296 : : /* If we've already add the in-charge parameter don't do it again. */
297 : 63249241 : if (DECL_HAS_IN_CHARGE_PARM_P (fn))
298 : : return;
299 : :
300 : : /* When processing templates we can't know, in general, whether or
301 : : not we're going to have virtual baseclasses. */
302 : 63249241 : if (processing_template_decl)
303 : : return;
304 : :
305 : : /* We don't need an in-charge parameter for constructors that don't
306 : : have virtual bases. */
307 : 38655215 : if (DECL_CONSTRUCTOR_P (fn)
308 : 38655215 : && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
309 : : return;
310 : :
311 : 9879466 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
312 : 9879466 : basetype = TREE_TYPE (TREE_VALUE (arg_types));
313 : 9879466 : arg_types = TREE_CHAIN (arg_types);
314 : :
315 : 9879466 : 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 : 9879466 : if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
320 : : {
321 : 1074676 : 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 : 1074676 : DECL_CHAIN (parm) = parms;
325 : 1074676 : parms = parm;
326 : :
327 : : /* ...and then to TYPE_ARG_TYPES. */
328 : 1074676 : arg_types = hash_tree_chain (vtt_parm_type, arg_types);
329 : :
330 : 1074676 : DECL_HAS_VTT_PARM_P (fn) = 1;
331 : : }
332 : :
333 : : /* Then add the in-charge parm (before the VTT parm). */
334 : 9879466 : parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
335 : 9879466 : DECL_CHAIN (parm) = parms;
336 : 9879466 : parms = parm;
337 : 9879466 : arg_types = hash_tree_chain (integer_type_node, arg_types);
338 : :
339 : : /* Insert our new parameter(s) into the list. */
340 : 9879466 : DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
341 : :
342 : : /* And rebuild the function type. */
343 : 9879466 : fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
344 : : arg_types);
345 : 9879466 : if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
346 : 8 : fntype = (cp_build_type_attribute_variant
347 : 8 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
348 : 9879466 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
349 : 9879466 : TREE_TYPE (fn) = fntype;
350 : :
351 : : /* Now we've got the in-charge parameter. */
352 : 9879466 : 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 : 114189027 : grokclassfn (tree ctype, tree function, enum overload_flags flags)
377 : : {
378 : 114189027 : 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 : 114189027 : SET_DECL_LANGUAGE (function, lang_cplusplus);
383 : :
384 : 114189027 : 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 : 114189027 : DECL_CONTEXT (function) = ctype;
392 : :
393 : 114189027 : if (flags == DTOR_FLAG)
394 : 8310256 : DECL_CXX_DESTRUCTOR_P (function) = 1;
395 : :
396 : 220067798 : if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
397 : 41380088 : maybe_retrofit_in_chrg (function);
398 : 114189027 : }
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 : 9942028 : 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 : 9942028 : tree type;
410 : 9942028 : tree expr;
411 : 9942028 : tree orig_array_expr = array_expr;
412 : 9942028 : tree orig_index_exp = index_exp;
413 : 19884056 : vec<tree, va_gc> *orig_index_exp_list
414 : 9942028 : = index_exp_list ? *index_exp_list : NULL;
415 : 9942028 : tree overload = NULL_TREE;
416 : :
417 : 9942028 : if (error_operand_p (array_expr) || error_operand_p (index_exp))
418 : 98 : return error_mark_node;
419 : :
420 : 9941930 : if (processing_template_decl)
421 : : {
422 : 8465624 : if (type_dependent_expression_p (array_expr)
423 : 8465647 : || (index_exp ? type_dependent_expression_p (index_exp)
424 : 23 : : any_type_dependent_arguments_p (*index_exp_list)))
425 : : {
426 : 6739276 : if (index_exp == NULL)
427 : 76 : index_exp = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
428 : : *index_exp_list);
429 : 6739276 : return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
430 : 6739276 : NULL_TREE, NULL_TREE);
431 : : }
432 : 1726348 : if (!index_exp)
433 : 22 : orig_index_exp_list = make_tree_vector_copy (*index_exp_list);
434 : : }
435 : :
436 : 3202654 : type = TREE_TYPE (array_expr);
437 : 3202654 : gcc_assert (type);
438 : 3202654 : type = non_reference (type);
439 : :
440 : : /* If they have an `operator[]', use that. */
441 : 3202654 : if (MAYBE_CLASS_TYPE_P (type)
442 : 2964323 : || (index_exp && MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
443 : : || (index_exp == NULL_TREE
444 : 11 : && !(*index_exp_list)->is_empty ()
445 : 6 : && MAYBE_CLASS_TYPE_P (TREE_TYPE ((*index_exp_list)->last ()))))
446 : : {
447 : 238387 : if (index_exp)
448 : 238247 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
449 : : index_exp, NULL_TREE, NULL_TREE,
450 : : &overload, complain);
451 : 140 : 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 : 51 : expr = build_op_subscript (loc, array_expr, index_exp_list,
457 : : &overload, complain & tf_decltype);
458 : 51 : if (expr == error_mark_node
459 : : /* Don't do the backward compatibility fallback in a SFINAE
460 : : context. */
461 : 6 : && (complain & tf_error))
462 : : {
463 : 4 : tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL,
464 : : tf_none);
465 : 4 : if (idx != error_mark_node)
466 : 4 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
467 : : idx, NULL_TREE, NULL_TREE, &overload,
468 : : complain & tf_decltype);
469 : 4 : 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 : 3 : pedwarn (loc, OPT_Wcomma_subscript,
481 : : "top-level comma expression in array subscript "
482 : : "changed meaning in C++23");
483 : 3 : if (processing_template_decl)
484 : : {
485 : 0 : orig_index_exp
486 : 0 : = build_x_compound_expr_from_vec (orig_index_exp_list,
487 : : NULL, complain);
488 : 0 : if (orig_index_exp == error_mark_node)
489 : 0 : expr = error_mark_node;
490 : 0 : release_tree_vector (orig_index_exp_list);
491 : : }
492 : : }
493 : : }
494 : : }
495 : : }
496 : : else
497 : : {
498 : 2964267 : tree p1, p2, i1, i2;
499 : 2964267 : 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 : 2964267 : if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
506 : : p1 = array_expr;
507 : : else
508 : 1873903 : p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
509 : :
510 : 2964267 : if (index_exp == NULL_TREE)
511 : : {
512 : 11 : 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 : 3 : 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 : 2 : tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL,
524 : : tf_none);
525 : 2 : 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 : 2 : 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 : 2 : index_exp = idx;
538 : 2 : if (processing_template_decl)
539 : : {
540 : 0 : orig_index_exp
541 : 0 : = build_x_compound_expr_from_vec (orig_index_exp_list,
542 : : NULL, complain);
543 : 0 : release_tree_vector (orig_index_exp_list);
544 : 0 : if (orig_index_exp == error_mark_node)
545 : : return error_mark_node;
546 : : }
547 : : }
548 : :
549 : 2964258 : if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
550 : : p2 = index_exp;
551 : : else
552 : 2964228 : p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
553 : :
554 : 2964258 : i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
555 : : false);
556 : 2964258 : i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
557 : : false);
558 : :
559 : 2964258 : if ((p1 && i2) && (i1 && p2))
560 : 0 : error ("ambiguous conversion for array subscript");
561 : :
562 : 2964258 : if (p1 && i2)
563 : : array_expr = p1, index_exp = i2;
564 : 76 : else if (i1 && p2)
565 : : swapped = true, array_expr = p2, index_exp = i1;
566 : : else
567 : : {
568 : 36 : if (complain & tf_error)
569 : 32 : error_at (loc, "invalid types %<%T[%T]%> for array subscript",
570 : 32 : type, TREE_TYPE (index_exp));
571 : 36 : return error_mark_node;
572 : : }
573 : :
574 : 2964222 : if (array_expr == error_mark_node || index_exp == error_mark_node)
575 : 0 : error ("ambiguous conversion for array subscript");
576 : :
577 : 2964222 : if (TYPE_PTR_P (TREE_TYPE (array_expr)))
578 : 1873862 : array_expr = mark_rvalue_use (array_expr);
579 : : else
580 : 1090360 : array_expr = mark_lvalue_use_nonread (array_expr);
581 : 2964222 : index_exp = mark_rvalue_use (index_exp);
582 : 2964222 : if (swapped
583 : 40 : && flag_strong_eval_order == 2
584 : 2964252 : && (TREE_SIDE_EFFECTS (array_expr) || TREE_SIDE_EFFECTS (index_exp)))
585 : 14 : expr = build_array_ref (input_location, index_exp, array_expr);
586 : : else
587 : 2964208 : expr = build_array_ref (input_location, array_expr, index_exp);
588 : : }
589 : 3202609 : if (processing_template_decl && expr != error_mark_node)
590 : : {
591 : 1726348 : if (overload != NULL_TREE)
592 : : {
593 : 219439 : 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 : 219417 : return build_min_non_dep_op_overload (ARRAY_REF, expr, overload,
602 : : orig_array_expr,
603 : 219417 : orig_index_exp);
604 : : }
605 : :
606 : 1506909 : 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 : 1506909 : return build_min_non_dep (ARRAY_REF, expr, orig_array_expr,
615 : 1506909 : 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 : 4938 : grok_omp_array_section (location_t loc, tree array_expr, tree index,
625 : : tree length)
626 : : {
627 : 4938 : tree orig_array_expr = array_expr;
628 : 4938 : tree orig_index = index;
629 : 4938 : tree orig_length = length;
630 : :
631 : 4938 : if (error_operand_p (array_expr)
632 : 4930 : || error_operand_p (index)
633 : 9868 : || error_operand_p (length))
634 : 8 : return error_mark_node;
635 : :
636 : 4930 : if (processing_template_decl
637 : 4930 : && (type_dependent_expression_p (array_expr)
638 : 1012 : || type_dependent_expression_p (index)
639 : 992 : || type_dependent_expression_p (length)))
640 : 861 : return build_min_nt_loc (loc, OMP_ARRAY_SECTION, array_expr, index, length);
641 : :
642 : 4069 : index = fold_non_dependent_expr (index);
643 : 4069 : 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 : 4069 : tree expr = build_omp_array_section (loc, array_expr, index, length);
650 : :
651 : 4069 : if (processing_template_decl)
652 : 992 : 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 : 546942 : delete_sanity (location_t loc, tree exp, tree size, bool doing_vec,
666 : : int use_global_delete, tsubst_flags_t complain)
667 : : {
668 : 546942 : tree t, type;
669 : :
670 : 546942 : if (exp == error_mark_node)
671 : : return exp;
672 : :
673 : 546825 : if (processing_template_decl)
674 : : {
675 : 448730 : t = build_min (DELETE_EXPR, void_type_node, exp, size);
676 : 448730 : DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
677 : 448730 : DELETE_EXPR_USE_VEC (t) = doing_vec;
678 : 448730 : TREE_SIDE_EFFECTS (t) = 1;
679 : 448730 : SET_EXPR_LOCATION (t, loc);
680 : 448730 : return t;
681 : : }
682 : :
683 : 98095 : 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 : 98095 : if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
687 : 98095 : && (complain & tf_warning))
688 : 24 : warning_at (exp_loc, 0, "deleting array %q#E", exp);
689 : :
690 : 98095 : t = build_expr_type_conversion (WANT_POINTER, exp, true);
691 : :
692 : 98095 : if (t == NULL_TREE || t == error_mark_node)
693 : : {
694 : 28 : if (complain & tf_error)
695 : 28 : error_at (exp_loc,
696 : : "type %q#T argument given to %<delete%>, expected pointer",
697 : 28 : TREE_TYPE (exp));
698 : 28 : return error_mark_node;
699 : : }
700 : :
701 : 98067 : 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 : 98067 : if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
707 : : {
708 : 8 : if (complain & tf_error)
709 : 8 : error_at (exp_loc,
710 : : "cannot delete a function. Only pointer-to-objects are "
711 : : "valid arguments to %<delete%>");
712 : 8 : return error_mark_node;
713 : : }
714 : :
715 : : /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
716 : 98059 : if (VOID_TYPE_P (TREE_TYPE (type)))
717 : : {
718 : 27 : if (complain & tf_warning)
719 : 27 : 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 : 98059 : if (integer_zerop (t))
726 : 4 : return build1_loc (loc, NOP_EXPR, void_type_node, t);
727 : :
728 : 98055 : if (doing_vec)
729 : 3203 : return build_vec_delete (loc, t, /*maxindex=*/NULL_TREE,
730 : : sfk_deleting_destructor,
731 : 3203 : use_global_delete, complain);
732 : : else
733 : 94852 : return build_delete (loc, type, t, sfk_deleting_destructor,
734 : : LOOKUP_NORMAL, use_global_delete,
735 : 94852 : 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 : 17970415 : check_member_template (tree tmpl)
743 : : {
744 : 17970415 : tree decl;
745 : :
746 : 17970415 : gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
747 : 17970415 : decl = DECL_TEMPLATE_RESULT (tmpl);
748 : :
749 : 17970415 : if (TREE_CODE (decl) == FUNCTION_DECL
750 : 2053987 : || DECL_ALIAS_TEMPLATE_P (tmpl)
751 : 18724425 : || (TREE_CODE (decl) == TYPE_DECL
752 : 661833 : && 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 : 18104010 : gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
757 : : /* The parser rejects any use of virtual in a function template. */
758 : 17878238 : 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 : 17878238 : DECL_IGNORED_P (tmpl) = 1;
764 : : }
765 : 92177 : else if (variable_template_p (tmpl))
766 : : /* OK */;
767 : : else
768 : 0 : error ("template declaration of %q#D", decl);
769 : 17970415 : }
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 : 9222788 : check_classfn (tree ctype, tree function, tree template_parms)
783 : : {
784 : 9222788 : if (DECL_USE_TEMPLATE (function)
785 : 1217795 : && !(TREE_CODE (function) == TEMPLATE_DECL
786 : 1209 : && DECL_TEMPLATE_SPECIALIZATION (function))
787 : 10439374 : && 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 : 8818171 : if (TREE_CODE (function) == TEMPLATE_DECL)
803 : : {
804 : 1229 : if (template_parms
805 : 2438 : && !comp_template_parms (template_parms,
806 : 1209 : 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 : 1229 : template_parms = DECL_TEMPLATE_PARMS (function);
813 : : }
814 : :
815 : : /* OK, is this a definition of a member template? */
816 : 8818171 : bool is_template = (template_parms != NULL_TREE);
817 : :
818 : : /* [temp.mem]
819 : :
820 : : A destructor shall not be a member template. */
821 : 17636342 : if (DECL_DESTRUCTOR_P (function) && is_template)
822 : : {
823 : 8 : error ("destructor %qD declared as member template", function);
824 : 8 : 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 : 8818163 : tree pushed_scope = push_scope (ctype);
831 : 8818163 : tree matched = NULL_TREE;
832 : 8818163 : tree fns = get_class_binding (ctype, DECL_NAME (function));
833 : 8818163 : bool saw_template = false;
834 : :
835 : 35901419 : for (ovl_iterator iter (fns); !matched && iter; ++iter)
836 : : {
837 : 16040397 : tree fndecl = *iter;
838 : :
839 : 16040397 : if (TREE_CODE (fndecl) == TEMPLATE_DECL)
840 : 3960393 : saw_template = true;
841 : :
842 : : /* A member template definition only matches a member template
843 : : declaration. */
844 : 16040397 : if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
845 : 1708170 : continue;
846 : :
847 : 14332227 : if (!DECL_DECLARES_FUNCTION_P (fndecl))
848 : 302719 : continue;
849 : :
850 : 14029508 : tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
851 : 14029508 : 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 : 14029508 : if (DECL_STATIC_FUNCTION_P (fndecl)
861 : 14029508 : && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
862 : 149184 : p1 = TREE_CHAIN (p1);
863 : :
864 : : /* ref-qualifier or absence of same must match. */
865 : 28059016 : if (type_memfn_rqual (TREE_TYPE (function))
866 : 14029508 : != type_memfn_rqual (TREE_TYPE (fndecl)))
867 : 86 : continue;
868 : :
869 : : // Include constraints in the match.
870 : 14029422 : tree c1 = get_constraints (function);
871 : 14029422 : 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 : 14029422 : if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
877 : : TREE_TYPE (TREE_TYPE (fndecl)))
878 : 13227679 : && compparms (p1, p2)
879 : 8818063 : && !targetm.target_option.function_versions (function, fndecl)
880 : 8818063 : && (!is_template
881 : 2057088 : || comp_template_parms (template_parms,
882 : 2057088 : DECL_TEMPLATE_PARMS (fndecl)))
883 : 8818055 : && equivalent_constraints (c1, c2)
884 : 8818038 : && (DECL_TEMPLATE_SPECIALIZATION (function)
885 : 8818038 : == DECL_TEMPLATE_SPECIALIZATION (fndecl))
886 : 22847460 : && (!DECL_TEMPLATE_SPECIALIZATION (function)
887 : 808192 : || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
888 : : matched = fndecl;
889 : : }
890 : :
891 : 105 : if (!matched && !is_template && saw_template
892 : 8818183 : && !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 : 4 : SET_DECL_IMPLICIT_INSTANTIATION (function);
900 : 4 : DECL_TEMPLATE_INFO (function) = build_template_info (fns, NULL_TREE);
901 : 4 : matched = function;
902 : : }
903 : :
904 : 8818163 : if (!matched)
905 : : {
906 : 121 : if (!COMPLETE_TYPE_P (ctype))
907 : 8 : cxx_incomplete_type_error (DECL_SOURCE_LOCATION (function),
908 : : function, ctype);
909 : : else
910 : : {
911 : 218 : if (DECL_CONV_FN_P (function))
912 : 8 : fns = get_class_binding (ctype, conv_op_identifier);
913 : :
914 : 113 : error_at (DECL_SOURCE_LOCATION (function),
915 : : "no declaration matches %q#D", function);
916 : 113 : if (fns)
917 : 61 : print_candidates (fns);
918 : 100 : else if (DECL_CONV_FN_P (function))
919 : 4 : inform (DECL_SOURCE_LOCATION (function),
920 : : "no conversion operators declared");
921 : : else
922 : 48 : inform (DECL_SOURCE_LOCATION (function),
923 : : "no functions named %qD", function);
924 : 113 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
925 : : "%#qT defined here", ctype);
926 : : }
927 : 121 : matched = error_mark_node;
928 : : }
929 : :
930 : 8818163 : if (pushed_scope)
931 : 8458285 : pop_scope (pushed_scope);
932 : :
933 : : return matched;
934 : : }
935 : :
936 : : /* DECL is a function with vague linkage. Remember it so that at the
937 : : end of the translation unit we can decide whether or not to emit
938 : : it. */
939 : :
940 : : void
941 : 35468271 : note_vague_linkage_fn (tree decl)
942 : : {
943 : 35468271 : if (processing_template_decl)
944 : : return;
945 : :
946 : 35468259 : DECL_DEFER_OUTPUT (decl) = 1;
947 : 35468259 : vec_safe_push (deferred_fns, decl);
948 : : }
949 : :
950 : : /* As above, but for variables. */
951 : :
952 : : void
953 : 2982337 : note_vague_linkage_variable (tree decl)
954 : : {
955 : 2982337 : vec_safe_push (pending_statics, decl);
956 : 2982337 : }
957 : :
958 : : /* We have just processed the DECL, which is a static data member.
959 : : The other parameters are as for cp_finish_decl. */
960 : :
961 : : void
962 : 11760840 : finish_static_data_member_decl (tree decl,
963 : : tree init, bool init_const_expr_p,
964 : : tree asmspec_tree,
965 : : int flags)
966 : : {
967 : 11760840 : if (DECL_TEMPLATE_INSTANTIATED (decl))
968 : : /* We already needed to instantiate this, so the processing in this
969 : : function is unnecessary/wrong. */
970 : : return;
971 : :
972 : 11760836 : DECL_CONTEXT (decl) = current_class_type;
973 : :
974 : : /* We cannot call pushdecl here, because that would fill in the
975 : : TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
976 : : the right thing, namely, to put this decl out straight away. */
977 : :
978 : 11760836 : if (! processing_template_decl)
979 : 9666111 : vec_safe_push (pending_statics, decl);
980 : :
981 : 11760836 : if (LOCAL_CLASS_P (current_class_type)
982 : : /* We already complained about the template definition. */
983 : 11760836 : && !DECL_TEMPLATE_INSTANTIATION (decl))
984 : 20 : permerror (DECL_SOURCE_LOCATION (decl),
985 : : "local class %q#T shall not have static data member %q#D",
986 : : current_class_type, decl);
987 : : else
988 : 23853740 : for (tree t = current_class_type; TYPE_P (t);
989 : 12092924 : t = CP_TYPE_CONTEXT (t))
990 : 24185920 : if (TYPE_UNNAMED_P (t))
991 : : {
992 : 24 : auto_diagnostic_group d;
993 : 24 : if (permerror (DECL_SOURCE_LOCATION (decl),
994 : : "static data member %qD in unnamed class", decl))
995 : 24 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
996 : : "unnamed class defined here");
997 : 24 : break;
998 : 24 : }
999 : :
1000 : 11760836 : if (DECL_INLINE_VAR_P (decl) && !DECL_TEMPLATE_INSTANTIATION (decl))
1001 : : /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P.
1002 : : Except that if decl is a template instantiation, it isn't defined until
1003 : : instantiate_decl. */;
1004 : : else
1005 : 4091609 : DECL_IN_AGGR_P (decl) = 1;
1006 : :
1007 : 11760836 : if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
1008 : 11760836 : && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
1009 : 91651 : SET_VAR_HAD_UNKNOWN_BOUND (decl);
1010 : :
1011 : 11760836 : if (init)
1012 : : {
1013 : : /* Similarly to start_decl_1, we want to complete the type in order
1014 : : to do the right thing in cp_apply_type_quals_to_decl, possibly
1015 : : clear TYPE_QUAL_CONST (c++/65579). */
1016 : 8803894 : tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
1017 : 8803894 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
1018 : : }
1019 : :
1020 : 11760836 : cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
1021 : : }
1022 : :
1023 : : /* DECLARATOR and DECLSPECS correspond to a class member. The other
1024 : : parameters are as for cp_finish_decl. Return the DECL for the
1025 : : class member declared. */
1026 : :
1027 : : tree
1028 : 70855889 : grokfield (const cp_declarator *declarator,
1029 : : cp_decl_specifier_seq *declspecs,
1030 : : tree init, bool init_const_expr_p,
1031 : : tree asmspec_tree,
1032 : : tree attrlist)
1033 : : {
1034 : 70855889 : tree value;
1035 : 70855889 : const char *asmspec = 0;
1036 : 70855889 : int flags;
1037 : :
1038 : 70855889 : if (init
1039 : 18219462 : && TREE_CODE (init) == TREE_LIST
1040 : 0 : && TREE_VALUE (init) == error_mark_node
1041 : 70855889 : && TREE_CHAIN (init) == NULL_TREE)
1042 : : init = NULL_TREE;
1043 : :
1044 : 70855889 : int initialized;
1045 : 70855889 : if (init == ridpointers[(int)RID_DELETE])
1046 : : initialized = SD_DELETED;
1047 : 67942216 : else if (init == ridpointers[(int)RID_DEFAULT])
1048 : : initialized = SD_DEFAULTED;
1049 : 62661153 : else if (init)
1050 : : initialized = SD_INITIALIZED;
1051 : : else
1052 : 52636427 : initialized = SD_UNINITIALIZED;
1053 : :
1054 : 70855889 : value = grokdeclarator (declarator, declspecs, FIELD, initialized, &attrlist);
1055 : 70855881 : if (! value || value == error_mark_node)
1056 : : /* friend or constructor went bad. */
1057 : 414 : return error_mark_node;
1058 : 70855467 : if (TREE_TYPE (value) == error_mark_node)
1059 : : return value;
1060 : :
1061 : 70855280 : if (TREE_CODE (value) == TYPE_DECL && init)
1062 : : {
1063 : 8 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)),
1064 : : "typedef %qD is initialized (use %qs instead)",
1065 : : value, "decltype");
1066 : 8 : init = NULL_TREE;
1067 : : }
1068 : :
1069 : : /* Pass friendly classes back. */
1070 : 70855280 : if (value == void_type_node)
1071 : : return value;
1072 : :
1073 : 70855280 : if (DECL_NAME (value)
1074 : 70855280 : && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR)
1075 : : {
1076 : 4 : error_at (declarator->id_loc,
1077 : : "explicit template argument list not allowed");
1078 : 4 : return error_mark_node;
1079 : : }
1080 : :
1081 : : /* Stash away type declarations. */
1082 : 70855276 : if (TREE_CODE (value) == TYPE_DECL)
1083 : : {
1084 : 22389964 : DECL_NONLOCAL (value) = 1;
1085 : 22389964 : DECL_CONTEXT (value) = current_class_type;
1086 : :
1087 : 22389964 : if (attrlist)
1088 : : {
1089 : 87062 : int attrflags = 0;
1090 : :
1091 : : /* If this is a typedef that names the class for linkage purposes
1092 : : (7.1.3p8), apply any attributes directly to the type. */
1093 : 174124 : if (OVERLOAD_TYPE_P (TREE_TYPE (value))
1094 : 120487 : && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
1095 : : attrflags = ATTR_FLAG_TYPE_IN_PLACE;
1096 : :
1097 : 87062 : cplus_decl_attributes (&value, attrlist, attrflags);
1098 : : }
1099 : :
1100 : 22389964 : if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
1101 : 22389964 : && TREE_TYPE (value) != error_mark_node
1102 : 44779928 : && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
1103 : 22389271 : set_underlying_type (value);
1104 : :
1105 : : /* It's important that push_template_decl below follows
1106 : : set_underlying_type above so that the created template
1107 : : carries the properly set type of VALUE. */
1108 : 22389964 : if (processing_template_decl)
1109 : 18606166 : value = push_template_decl (value);
1110 : :
1111 : 22389964 : record_locally_defined_typedef (value);
1112 : 22389964 : return value;
1113 : : }
1114 : :
1115 : 48465312 : int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
1116 : :
1117 : 48465312 : if (!friendp && DECL_IN_AGGR_P (value))
1118 : : {
1119 : 0 : error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
1120 : 0 : return void_type_node;
1121 : : }
1122 : :
1123 : 48465312 : if (asmspec_tree && asmspec_tree != error_mark_node)
1124 : 202 : asmspec = TREE_STRING_POINTER (asmspec_tree);
1125 : :
1126 : 48465312 : if (init)
1127 : : {
1128 : 18219396 : if (TREE_CODE (value) == FUNCTION_DECL)
1129 : : {
1130 : 8552280 : if (init == ridpointers[(int)RID_DELETE])
1131 : : {
1132 : 2913667 : DECL_DELETED_FN (value) = 1;
1133 : 2913667 : DECL_DECLARED_INLINE_P (value) = 1;
1134 : : }
1135 : 5638613 : else if (init == ridpointers[(int)RID_DEFAULT])
1136 : : {
1137 : 5281062 : if (defaultable_fn_check (value))
1138 : : {
1139 : 5281023 : DECL_DEFAULTED_FN (value) = 1;
1140 : 5281023 : DECL_INITIALIZED_IN_CLASS_P (value) = 1;
1141 : 5281023 : DECL_DECLARED_INLINE_P (value) = 1;
1142 : : /* grokfndecl set this to error_mark_node, but we want to
1143 : : leave it unset until synthesize_method. */
1144 : 5281023 : DECL_INITIAL (value) = NULL_TREE;
1145 : : }
1146 : : }
1147 : 357551 : else if (TREE_CODE (init) == DEFERRED_PARSE)
1148 : 0 : error ("invalid initializer for member function %qD", value);
1149 : 357551 : else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
1150 : : {
1151 : 357540 : if (integer_zerop (init))
1152 : 357492 : DECL_PURE_VIRTUAL_P (value) = 1;
1153 : 48 : else if (error_operand_p (init))
1154 : : ; /* An error has already been reported. */
1155 : : else
1156 : 0 : error ("invalid initializer for member function %qD",
1157 : : value);
1158 : : }
1159 : : else
1160 : : {
1161 : 11 : gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
1162 : 11 : location_t iloc
1163 : 11 : = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value));
1164 : 11 : if (friendp)
1165 : 3 : error_at (iloc, "initializer specified for friend "
1166 : : "function %qD", value);
1167 : : else
1168 : 8 : error_at (iloc, "initializer specified for static "
1169 : : "member function %qD", value);
1170 : : }
1171 : : }
1172 : 9667116 : else if (TREE_CODE (value) == FIELD_DECL)
1173 : : /* C++11 NSDMI, keep going. */;
1174 : 8803894 : else if (!VAR_P (value))
1175 : 0 : gcc_unreachable ();
1176 : : }
1177 : :
1178 : : /* Pass friend decls back. */
1179 : 48465312 : if ((TREE_CODE (value) == FUNCTION_DECL
1180 : 24782587 : || TREE_CODE (value) == TEMPLATE_DECL)
1181 : 48465375 : && DECL_CONTEXT (value) != current_class_type)
1182 : : {
1183 : 1230439 : if (attrlist)
1184 : 0 : cplus_decl_attributes (&value, attrlist, 0);
1185 : 1230439 : return value;
1186 : : }
1187 : :
1188 : : /* Need to set this before push_template_decl. */
1189 : 47234873 : if (VAR_P (value))
1190 : 9366495 : DECL_CONTEXT (value) = current_class_type;
1191 : :
1192 : 47234873 : if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1193 : : {
1194 : 16015017 : value = push_template_decl (value);
1195 : 16015017 : if (error_operand_p (value))
1196 : 8 : return error_mark_node;
1197 : : }
1198 : :
1199 : 47234865 : if (attrlist)
1200 : 219251 : cplus_decl_attributes (&value, attrlist, 0);
1201 : :
1202 : 47234865 : if (init && DIRECT_LIST_INIT_P (init))
1203 : : flags = LOOKUP_NORMAL;
1204 : : else
1205 : : flags = LOOKUP_IMPLICIT;
1206 : :
1207 : 47234865 : switch (TREE_CODE (value))
1208 : : {
1209 : 9366491 : case VAR_DECL:
1210 : 9366491 : finish_static_data_member_decl (value, init, init_const_expr_p,
1211 : : asmspec_tree, flags);
1212 : 9366491 : return value;
1213 : :
1214 : 15416029 : case FIELD_DECL:
1215 : 15416029 : if (asmspec)
1216 : 8 : error ("%<asm%> specifiers are not permitted on non-static data members");
1217 : 15416029 : if (DECL_INITIAL (value) == error_mark_node)
1218 : 3 : init = error_mark_node;
1219 : 15416029 : cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1220 : : NULL_TREE, flags);
1221 : 15416029 : DECL_IN_AGGR_P (value) = 1;
1222 : 15416029 : return value;
1223 : :
1224 : 22452345 : case FUNCTION_DECL:
1225 : 22452345 : if (asmspec)
1226 : 94 : set_user_assembler_name (value, asmspec);
1227 : :
1228 : 22452345 : cp_finish_decl (value,
1229 : : /*init=*/NULL_TREE,
1230 : : /*init_const_expr_p=*/false,
1231 : : asmspec_tree, flags);
1232 : :
1233 : : /* Pass friends back this way. */
1234 : 22452345 : if (DECL_UNIQUE_FRIEND_P (value))
1235 : 0 : return void_type_node;
1236 : :
1237 : 22452345 : DECL_IN_AGGR_P (value) = 1;
1238 : 22452345 : return value;
1239 : :
1240 : 0 : default:
1241 : 0 : gcc_unreachable ();
1242 : : }
1243 : : return NULL_TREE;
1244 : : }
1245 : :
1246 : : /* Like `grokfield', but for bitfields.
1247 : : WIDTH is the width of the bitfield, a constant expression.
1248 : : The other parameters are as for grokfield. */
1249 : :
1250 : : tree
1251 : 352890 : grokbitfield (const cp_declarator *declarator,
1252 : : cp_decl_specifier_seq *declspecs, tree width, tree init,
1253 : : tree attrlist)
1254 : : {
1255 : 352890 : tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1256 : 352890 : init != NULL_TREE, &attrlist);
1257 : :
1258 : 352890 : if (value == error_mark_node)
1259 : : return NULL_TREE; /* friends went bad. */
1260 : :
1261 : 352882 : tree type = TREE_TYPE (value);
1262 : 352882 : if (type == error_mark_node)
1263 : : return value;
1264 : :
1265 : : /* Pass friendly classes back. */
1266 : 352826 : if (VOID_TYPE_P (value))
1267 : 0 : return void_type_node;
1268 : :
1269 : 352826 : if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)
1270 : 352826 : && (INDIRECT_TYPE_P (type) || !dependent_type_p (type)))
1271 : : {
1272 : 44 : error_at (DECL_SOURCE_LOCATION (value),
1273 : : "bit-field %qD with non-integral type %qT",
1274 : : value, type);
1275 : 44 : return error_mark_node;
1276 : : }
1277 : :
1278 : 352782 : if (TREE_CODE (value) == TYPE_DECL)
1279 : : {
1280 : 8 : error_at (DECL_SOURCE_LOCATION (value),
1281 : : "cannot declare %qD to be a bit-field type", value);
1282 : 8 : return NULL_TREE;
1283 : : }
1284 : :
1285 : : /* Usually, finish_struct_1 catches bitfields with invalid types.
1286 : : But, in the case of bitfields with function type, we confuse
1287 : : ourselves into thinking they are member functions, so we must
1288 : : check here. */
1289 : 352774 : if (TREE_CODE (value) == FUNCTION_DECL)
1290 : : {
1291 : 4 : error_at (DECL_SOURCE_LOCATION (value),
1292 : : "cannot declare bit-field %qD with function type", value);
1293 : 4 : return NULL_TREE;
1294 : : }
1295 : :
1296 : 352770 : if (TYPE_WARN_IF_NOT_ALIGN (type))
1297 : : {
1298 : 12 : error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field "
1299 : : "%qD with %<warn_if_not_aligned%> type", value);
1300 : 12 : return NULL_TREE;
1301 : : }
1302 : :
1303 : 352758 : if (DECL_IN_AGGR_P (value))
1304 : : {
1305 : 0 : error ("%qD is already defined in the class %qT", value,
1306 : 0 : DECL_CONTEXT (value));
1307 : 0 : return void_type_node;
1308 : : }
1309 : :
1310 : 352758 : if (TREE_STATIC (value))
1311 : : {
1312 : 8 : error_at (DECL_SOURCE_LOCATION (value),
1313 : : "static member %qD cannot be a bit-field", value);
1314 : 8 : return NULL_TREE;
1315 : : }
1316 : :
1317 : 352750 : int flags = LOOKUP_IMPLICIT;
1318 : 352750 : if (init && DIRECT_LIST_INIT_P (init))
1319 : : flags = LOOKUP_NORMAL;
1320 : 352750 : cp_finish_decl (value, init, false, NULL_TREE, flags);
1321 : :
1322 : 352750 : if (width != error_mark_node)
1323 : : {
1324 : : /* The width must be an integer type. */
1325 : 352731 : if (!type_dependent_expression_p (width)
1326 : 352731 : && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1327 : 12 : error ("width of bit-field %qD has non-integral type %qT", value,
1328 : 12 : TREE_TYPE (width));
1329 : 352719 : else if (!check_for_bare_parameter_packs (width))
1330 : : {
1331 : : /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1332 : : check_bitfield_decl picks it from there later and sets DECL_SIZE
1333 : : accordingly. */
1334 : 352713 : DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1335 : 352713 : SET_DECL_C_BIT_FIELD (value);
1336 : : }
1337 : : }
1338 : :
1339 : 352750 : DECL_IN_AGGR_P (value) = 1;
1340 : :
1341 : 352750 : if (attrlist)
1342 : 1141 : cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1343 : :
1344 : 352750 : return value;
1345 : : }
1346 : :
1347 : :
1348 : : /* Returns true iff ATTR is an attribute which needs to be applied at
1349 : : instantiation time rather than template definition time. */
1350 : :
1351 : : static bool
1352 : 11224776 : is_late_template_attribute (tree attr, tree decl)
1353 : : {
1354 : 11224776 : tree name = get_attribute_name (attr);
1355 : 11224776 : tree args = TREE_VALUE (attr);
1356 : 11224776 : const struct attribute_spec *spec = lookup_attribute_spec (name);
1357 : 11224776 : tree arg;
1358 : :
1359 : 11224776 : if (!spec)
1360 : : /* Unknown attribute. */
1361 : : return false;
1362 : :
1363 : : /* Attribute weak handling wants to write out assembly right away. */
1364 : 3620891 : if (is_attribute_p ("weak", name))
1365 : : return true;
1366 : :
1367 : : /* Attributes used and unused are applied directly to typedefs for the
1368 : : benefit of maybe_warn_unused_local_typedefs. */
1369 : 3620887 : if (TREE_CODE (decl) == TYPE_DECL
1370 : 3620887 : && (is_attribute_p ("unused", name)
1371 : 65998 : || is_attribute_p ("used", name)))
1372 : : return false;
1373 : :
1374 : : /* Attribute tls_model wants to modify the symtab. */
1375 : 3620833 : if (is_attribute_p ("tls_model", name))
1376 : : return true;
1377 : :
1378 : : /* #pragma omp declare simd attribute needs to be always deferred. */
1379 : 3620829 : if (flag_openmp
1380 : 3620829 : && is_attribute_p ("omp declare simd", name))
1381 : : return true;
1382 : :
1383 : 3619766 : if (args == error_mark_node)
1384 : : return false;
1385 : :
1386 : : /* An attribute pack is clearly dependent. */
1387 : 3619765 : if (args && PACK_EXPANSION_P (args))
1388 : : return true;
1389 : :
1390 : : /* If any of the arguments are dependent expressions, we can't evaluate
1391 : : the attribute until instantiation time. */
1392 : 4319766 : for (arg = args; arg; arg = TREE_CHAIN (arg))
1393 : : {
1394 : 787684 : tree t = TREE_VALUE (arg);
1395 : :
1396 : : /* If the first attribute argument is an identifier, only consider
1397 : : second and following arguments. Attributes like mode, format,
1398 : : cleanup and several target specific attributes aren't late
1399 : : just because they have an IDENTIFIER_NODE as first argument. */
1400 : 752125 : if (arg == args && attribute_takes_identifier_p (name)
1401 : 787684 : && identifier_p (t))
1402 : 33228 : continue;
1403 : :
1404 : 754456 : if (value_dependent_expression_p (t))
1405 : : return true;
1406 : : }
1407 : :
1408 : 3532082 : if (TREE_CODE (decl) == TYPE_DECL
1409 : 3466164 : || TYPE_P (decl)
1410 : 3146453 : || spec->type_required)
1411 : : {
1412 : 783855 : tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1413 : :
1414 : 464144 : if (!type)
1415 : : return true;
1416 : :
1417 : : /* We can't apply any attributes to a completely unknown type until
1418 : : instantiation time. */
1419 : 783851 : enum tree_code code = TREE_CODE (type);
1420 : 783851 : if (code == TEMPLATE_TYPE_PARM
1421 : 783851 : || code == BOUND_TEMPLATE_TEMPLATE_PARM
1422 : 760486 : || code == TYPENAME_TYPE)
1423 : : return true;
1424 : : /* Also defer most attributes on dependent types. This is not
1425 : : necessary in all cases, but is the better default. */
1426 : 757868 : else if (dependent_type_p (type)
1427 : : /* But some attributes specifically apply to templates. */
1428 : 709847 : && !is_attribute_p ("abi_tag", name)
1429 : 709815 : && !is_attribute_p ("deprecated", name)
1430 : 401482 : && !is_attribute_p ("unavailable", name)
1431 : 1159343 : && !is_attribute_p ("visibility", name))
1432 : : return true;
1433 : : else
1434 : : return false;
1435 : : }
1436 : : else
1437 : : return false;
1438 : : }
1439 : :
1440 : : /* ATTR_P is a list of attributes. Remove any attributes which need to be
1441 : : applied at instantiation time and return them. If IS_DEPENDENT is true,
1442 : : the declaration itself is dependent, so all attributes should be applied
1443 : : at instantiation time. */
1444 : :
1445 : : tree
1446 : 198459624 : splice_template_attributes (tree *attr_p, tree decl)
1447 : : {
1448 : 198459624 : tree *p = attr_p;
1449 : 198459624 : tree late_attrs = NULL_TREE;
1450 : 198459624 : tree *q = &late_attrs;
1451 : :
1452 : 198459624 : if (!p || *p == error_mark_node)
1453 : : return NULL_TREE;
1454 : :
1455 : 209684400 : for (; *p; )
1456 : : {
1457 : 11224776 : if (is_late_template_attribute (*p, decl))
1458 : : {
1459 : 516200 : ATTR_IS_DEPENDENT (*p) = 1;
1460 : 516200 : *q = *p;
1461 : 516200 : *p = TREE_CHAIN (*p);
1462 : 516200 : q = &TREE_CHAIN (*q);
1463 : 516200 : *q = NULL_TREE;
1464 : : }
1465 : : else
1466 : 10708576 : p = &TREE_CHAIN (*p);
1467 : : }
1468 : :
1469 : 198459624 : return late_attrs;
1470 : : }
1471 : :
1472 : : /* Attach any LATE_ATTRS to DECL_P, after the non-dependent attributes have
1473 : : been applied by a previous call to decl_attributes. */
1474 : :
1475 : : static void
1476 : 489583 : save_template_attributes (tree late_attrs, tree *decl_p, int flags)
1477 : : {
1478 : 489583 : tree *q;
1479 : :
1480 : 489583 : if (!late_attrs)
1481 : : return;
1482 : :
1483 : 489583 : if (DECL_P (*decl_p))
1484 : 458301 : q = &DECL_ATTRIBUTES (*decl_p);
1485 : : else
1486 : 31282 : q = &TYPE_ATTRIBUTES (*decl_p);
1487 : :
1488 : 489583 : tree old_attrs = *q;
1489 : :
1490 : : /* Place the late attributes at the beginning of the attribute
1491 : : list. */
1492 : 489583 : late_attrs = chainon (late_attrs, *q);
1493 : 489583 : if (*q != late_attrs
1494 : 489583 : && !DECL_P (*decl_p)
1495 : 31282 : && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1496 : : {
1497 : 19 : if (!dependent_type_p (*decl_p))
1498 : 6 : *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1499 : : else
1500 : : {
1501 : 13 : *decl_p = build_variant_type_copy (*decl_p);
1502 : 13 : TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1503 : : }
1504 : : }
1505 : : else
1506 : 489564 : *q = late_attrs;
1507 : :
1508 : 489583 : if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1509 : : {
1510 : : /* We've added new attributes directly to the main variant, so
1511 : : now we need to update all of the other variants to include
1512 : : these new attributes. */
1513 : 31269 : tree variant;
1514 : 31440 : for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1515 : 171 : variant = TYPE_NEXT_VARIANT (variant))
1516 : : {
1517 : 171 : gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1518 : 171 : TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1519 : : }
1520 : : }
1521 : : }
1522 : :
1523 : : /* True if ATTRS contains any dependent attributes that affect type
1524 : : identity. */
1525 : :
1526 : : bool
1527 : 1097453753 : any_dependent_type_attributes_p (tree attrs)
1528 : : {
1529 : 1118751995 : for (tree a = attrs; a; a = TREE_CHAIN (a))
1530 : 21298362 : if (ATTR_IS_DEPENDENT (a))
1531 : : {
1532 : 1455885 : const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1533 : 1455885 : if (as && as->affects_type_identity)
1534 : : return true;
1535 : : }
1536 : : return false;
1537 : : }
1538 : :
1539 : : /* Return true iff ATTRS are acceptable attributes to be applied in-place
1540 : : to a typedef which gives a previously unnamed class or enum a name for
1541 : : linkage purposes. */
1542 : :
1543 : : bool
1544 : 380755 : attributes_naming_typedef_ok (tree attrs)
1545 : : {
1546 : 392565 : for (; attrs; attrs = TREE_CHAIN (attrs))
1547 : : {
1548 : 11814 : tree name = get_attribute_name (attrs);
1549 : 11814 : if (is_attribute_p ("vector_size", name))
1550 : : return false;
1551 : : }
1552 : : return true;
1553 : : }
1554 : :
1555 : : /* Like reconstruct_complex_type, but handle also template trees. */
1556 : :
1557 : : tree
1558 : 29435 : cp_reconstruct_complex_type (tree type, tree bottom)
1559 : : {
1560 : 29435 : tree inner, outer;
1561 : :
1562 : 29435 : if (TYPE_PTR_P (type))
1563 : : {
1564 : 28 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1565 : 28 : outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1566 : 28 : TYPE_REF_CAN_ALIAS_ALL (type));
1567 : : }
1568 : : else if (TYPE_REF_P (type))
1569 : : {
1570 : 4 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1571 : 4 : outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1572 : 4 : TYPE_REF_CAN_ALIAS_ALL (type));
1573 : : }
1574 : : else if (TREE_CODE (type) == ARRAY_TYPE)
1575 : : {
1576 : 36 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1577 : 36 : outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1578 : : /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1579 : : element type qualification will be handled by the recursive
1580 : : cp_reconstruct_complex_type call and cp_build_qualified_type
1581 : : for ARRAY_TYPEs changes the element type. */
1582 : 36 : return outer;
1583 : : }
1584 : : else if (TREE_CODE (type) == FUNCTION_TYPE)
1585 : : {
1586 : 127 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1587 : 127 : outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1588 : 127 : outer = apply_memfn_quals (outer, type_memfn_quals (type));
1589 : : }
1590 : : else if (TREE_CODE (type) == METHOD_TYPE)
1591 : : {
1592 : 16 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1593 : : /* The build_method_type_directly() routine prepends 'this' to argument list,
1594 : : so we must compensate by getting rid of it. */
1595 : 16 : outer
1596 : : = build_method_type_directly
1597 : 16 : (class_of_this_parm (type), inner,
1598 : 16 : TREE_CHAIN (TYPE_ARG_TYPES (type)));
1599 : : }
1600 : : else if (TREE_CODE (type) == OFFSET_TYPE)
1601 : : {
1602 : 4 : inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1603 : 4 : outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1604 : : }
1605 : : else
1606 : : return bottom;
1607 : :
1608 : 179 : if (TYPE_ATTRIBUTES (type))
1609 : 0 : outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1610 : 179 : outer = cp_build_qualified_type (outer, cp_type_quals (type));
1611 : 179 : outer = cxx_copy_lang_qualifiers (outer, type);
1612 : :
1613 : 179 : return outer;
1614 : : }
1615 : :
1616 : : /* Replaces any constexpr expression that may be into the attributes
1617 : : arguments with their reduced value. */
1618 : :
1619 : : void
1620 : 321887682 : cp_check_const_attributes (tree attributes)
1621 : : {
1622 : 321887682 : if (attributes == error_mark_node)
1623 : : return;
1624 : :
1625 : : tree attr;
1626 : 354349982 : for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1627 : : {
1628 : 32462300 : if (cxx_contract_attribute_p (attr))
1629 : 660 : continue;
1630 : :
1631 : 32461640 : tree arg;
1632 : : /* As we implement alignas using gnu::aligned attribute and
1633 : : alignas argument is a constant expression, force manifestly
1634 : : constant evaluation of aligned attribute argument. */
1635 : 32461640 : bool manifestly_const_eval
1636 : 32461640 : = is_attribute_p ("aligned", get_attribute_name (attr));
1637 : 43872730 : for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1638 : 11411090 : arg = TREE_CHAIN (arg))
1639 : : {
1640 : 11411090 : tree expr = TREE_VALUE (arg);
1641 : 11411090 : if (EXPR_P (expr))
1642 : 5712 : TREE_VALUE (arg)
1643 : 2856 : = fold_non_dependent_expr (expr, tf_warning_or_error,
1644 : : manifestly_const_eval);
1645 : : }
1646 : : }
1647 : : }
1648 : :
1649 : : /* Copies hot or cold attributes to a function FN if present on the
1650 : : encapsulating class, struct, or union TYPE. */
1651 : :
1652 : : void
1653 : 15968469 : maybe_propagate_warmth_attributes (tree fn, tree type)
1654 : : {
1655 : 15968469 : if (fn == NULL_TREE || type == NULL_TREE
1656 : 15968469 : || !(TREE_CODE (type) == RECORD_TYPE
1657 : : || TREE_CODE (type) == UNION_TYPE))
1658 : : return;
1659 : :
1660 : 15968469 : tree has_cold_attr = lookup_attribute ("cold", TYPE_ATTRIBUTES (type));
1661 : 15968469 : tree has_hot_attr = lookup_attribute ("hot", TYPE_ATTRIBUTES (type));
1662 : :
1663 : 15968469 : if (has_cold_attr || has_hot_attr)
1664 : : {
1665 : : /* Transparently ignore the new warmth attribute if it
1666 : : conflicts with a present function attribute. Otherwise
1667 : : decl_attribute would still honour the present attribute,
1668 : : but producing an undesired warning in the process. */
1669 : :
1670 : 24 : if (has_cold_attr)
1671 : : {
1672 : 12 : if (lookup_attribute ("hot", DECL_ATTRIBUTES (fn)) == NULL)
1673 : : {
1674 : 12 : tree cold_cons
1675 : 12 : = tree_cons (get_identifier ("cold"), NULL, NULL);
1676 : :
1677 : 12 : decl_attributes (&fn, cold_cons, 0);
1678 : : }
1679 : : }
1680 : 12 : else if (has_hot_attr)
1681 : : {
1682 : 12 : if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)) == NULL)
1683 : : {
1684 : 12 : tree hot_cons
1685 : 12 : = tree_cons (get_identifier ("hot"), NULL, NULL);
1686 : :
1687 : 12 : decl_attributes (&fn, hot_cons, 0);
1688 : : }
1689 : : }
1690 : : }
1691 : : }
1692 : :
1693 : : /* Return the last pushed declaration for the symbol DECL or NULL
1694 : : when no such declaration exists. */
1695 : :
1696 : : static tree
1697 : 321835966 : find_last_decl (tree decl)
1698 : : {
1699 : 321835966 : tree last_decl = NULL_TREE;
1700 : :
1701 : 321835966 : if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1702 : : {
1703 : : /* Template specializations are matched elsewhere. */
1704 : 269096867 : if (DECL_LANG_SPECIFIC (decl)
1705 : 269096867 : && DECL_USE_TEMPLATE (decl))
1706 : : return NULL_TREE;
1707 : :
1708 : : /* Look up the declaration in its scope. */
1709 : 264616836 : tree pushed_scope = NULL_TREE;
1710 : 264616836 : if (tree ctype = DECL_CONTEXT (decl))
1711 : 206495930 : pushed_scope = push_scope (ctype);
1712 : :
1713 : 264616836 : last_decl = lookup_name (name);
1714 : :
1715 : 264616836 : if (pushed_scope)
1716 : 121477778 : pop_scope (pushed_scope);
1717 : :
1718 : : /* The declaration may be a member conversion operator
1719 : : or a bunch of overfloads (handle the latter below). */
1720 : 264616836 : if (last_decl && BASELINK_P (last_decl))
1721 : 84882 : last_decl = BASELINK_FUNCTIONS (last_decl);
1722 : : }
1723 : :
1724 : 52767730 : if (!last_decl)
1725 : 232807953 : return NULL_TREE;
1726 : :
1727 : 84547982 : if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1728 : : {
1729 : : /* A set of overloads of the same function. */
1730 : 569167010 : for (lkp_iterator iter (last_decl); iter; ++iter)
1731 : : {
1732 : 493215181 : if (TREE_CODE (*iter) == OVERLOAD)
1733 : 0 : continue;
1734 : :
1735 : 493215181 : tree d = *iter;
1736 : :
1737 : : /* We can't compare versions in the middle of processing the
1738 : : attribute that has the version. */
1739 : 493215181 : if (TREE_CODE (d) == FUNCTION_DECL
1740 : 493215181 : && DECL_FUNCTION_VERSIONED (d))
1741 : 8577101 : return NULL_TREE;
1742 : :
1743 : 493213925 : if (decls_match (decl, d, /*record_decls=*/false))
1744 : 8575845 : return d;
1745 : : }
1746 : 75951829 : return NULL_TREE;
1747 : : }
1748 : :
1749 : : return NULL_TREE;
1750 : : }
1751 : :
1752 : : /* Like decl_attributes, but handle C++ complexity. */
1753 : :
1754 : : void
1755 : 321885006 : cplus_decl_attributes (tree *decl, tree attributes, int flags)
1756 : : {
1757 : 321885006 : if (*decl == NULL_TREE || *decl == void_type_node
1758 : 321885006 : || *decl == error_mark_node || attributes == error_mark_node)
1759 : : return;
1760 : :
1761 : : /* Add implicit "omp declare target" attribute if requested. */
1762 : 321884921 : if (vec_safe_length (scope_chain->omp_declare_target_attribute)
1763 : 10760 : && ((VAR_P (*decl)
1764 : 2104 : && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1765 : 3253 : || TREE_CODE (*decl) == FUNCTION_DECL))
1766 : : {
1767 : 1691 : if (VAR_P (*decl)
1768 : 1691 : && DECL_CLASS_SCOPE_P (*decl))
1769 : 0 : error ("%q+D static data member inside of declare target directive",
1770 : : *decl);
1771 : : else
1772 : : {
1773 : 1691 : if (VAR_P (*decl)
1774 : 1691 : && (processing_template_decl
1775 : 302 : || !omp_mappable_type (TREE_TYPE (*decl))))
1776 : 65 : attributes
1777 : 65 : = tree_cons (get_identifier ("omp declare target implicit"),
1778 : : NULL_TREE, attributes);
1779 : : else
1780 : : {
1781 : 1626 : attributes = tree_cons (get_identifier ("omp declare target"),
1782 : : NULL_TREE, attributes);
1783 : 1626 : attributes
1784 : 1626 : = tree_cons (get_identifier ("omp declare target block"),
1785 : : NULL_TREE, attributes);
1786 : : }
1787 : 1691 : if (TREE_CODE (*decl) == FUNCTION_DECL)
1788 : : {
1789 : 1389 : cp_omp_declare_target_attr &last
1790 : 1389 : = scope_chain->omp_declare_target_attribute->last ();
1791 : 1389 : int device_type = MAX (last.device_type, 0);
1792 : 1389 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
1793 : 1389 : && !lookup_attribute ("omp declare target host",
1794 : : attributes))
1795 : 8 : attributes
1796 : 8 : = tree_cons (get_identifier ("omp declare target host"),
1797 : : NULL_TREE, attributes);
1798 : 1389 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
1799 : 1389 : && !lookup_attribute ("omp declare target nohost",
1800 : : attributes))
1801 : 8 : attributes
1802 : 8 : = tree_cons (get_identifier ("omp declare target nohost"),
1803 : : NULL_TREE, attributes);
1804 : 1389 : if (last.indirect
1805 : 1389 : && !lookup_attribute ("omp declare target indirect",
1806 : : attributes))
1807 : 26 : attributes
1808 : 26 : = tree_cons (get_identifier ("omp declare target indirect"),
1809 : : NULL_TREE, attributes);
1810 : : }
1811 : : }
1812 : : }
1813 : :
1814 : 321884921 : tree late_attrs = NULL_TREE;
1815 : 321884921 : if (processing_template_decl)
1816 : : {
1817 : 198446009 : if (check_for_bare_parameter_packs (attributes))
1818 : : return;
1819 : 198446002 : late_attrs = splice_template_attributes (&attributes, *decl);
1820 : : }
1821 : :
1822 : 321884914 : cp_check_const_attributes (attributes);
1823 : :
1824 : 321884914 : if (flag_openmp || flag_openmp_simd)
1825 : : {
1826 : : bool diagnosed = false;
1827 : 487319 : for (tree *pa = &attributes; *pa; )
1828 : : {
1829 : 83770 : if (get_attribute_namespace (*pa) == omp_identifier)
1830 : : {
1831 : 174 : tree name = get_attribute_name (*pa);
1832 : 174 : if (is_attribute_p ("directive", name)
1833 : 0 : || is_attribute_p ("sequence", name)
1834 : 174 : || is_attribute_p ("decl", name))
1835 : : {
1836 : 174 : const char *p = NULL;
1837 : 174 : if (TREE_VALUE (*pa) == NULL_TREE)
1838 : 9 : p = IDENTIFIER_POINTER (name);
1839 : 339 : for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
1840 : : {
1841 : 165 : tree d = TREE_VALUE (a);
1842 : 165 : gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
1843 : 279 : if (TREE_PUBLIC (d)
1844 : 117 : && (VAR_P (*decl)
1845 : 12 : || TREE_CODE (*decl) == FUNCTION_DECL)
1846 : 282 : && cp_maybe_parse_omp_decl (*decl, d))
1847 : 114 : continue;
1848 : 51 : p = TREE_PUBLIC (d) ? "decl" : "directive";
1849 : : }
1850 : 174 : if (p && !diagnosed)
1851 : : {
1852 : 60 : error ("%<omp::%s%> not allowed to be specified in "
1853 : : "this context", p);
1854 : 60 : diagnosed = true;
1855 : : }
1856 : 174 : if (p)
1857 : : {
1858 : 60 : *pa = TREE_CHAIN (*pa);
1859 : 60 : continue;
1860 : : }
1861 : : }
1862 : : }
1863 : 83710 : pa = &TREE_CHAIN (*pa);
1864 : : }
1865 : : }
1866 : :
1867 : 321884914 : if (TREE_CODE (*decl) == TEMPLATE_DECL)
1868 : 1209 : decl = &DECL_TEMPLATE_RESULT (*decl);
1869 : :
1870 : 321884914 : if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1871 : : {
1872 : 48948 : attributes
1873 : 48948 : = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1874 : 48948 : decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1875 : : attributes, flags);
1876 : : }
1877 : : else
1878 : : {
1879 : 321835966 : tree last_decl = find_last_decl (*decl);
1880 : 321835966 : decl_attributes (decl, attributes, flags, last_decl);
1881 : : }
1882 : :
1883 : 321884914 : if (late_attrs)
1884 : 489583 : save_template_attributes (late_attrs, decl, flags);
1885 : :
1886 : : /* Propagate deprecation out to the template. */
1887 : 321884914 : if (TREE_DEPRECATED (*decl))
1888 : 1966754 : if (tree ti = get_template_info (*decl))
1889 : : {
1890 : 1056961 : tree tmpl = TI_TEMPLATE (ti);
1891 : 1056961 : tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1892 : 692955 : : DECL_TEMPLATE_RESULT (tmpl));
1893 : 1056961 : if (*decl == pattern)
1894 : 341229 : TREE_DEPRECATED (tmpl) = true;
1895 : : }
1896 : :
1897 : : /* Likewise, propagate unavailability out to the template. */
1898 : 321884914 : if (TREE_UNAVAILABLE (*decl))
1899 : 300 : if (tree ti = get_template_info (*decl))
1900 : : {
1901 : 11 : tree tmpl = TI_TEMPLATE (ti);
1902 : 11 : tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1903 : 0 : : DECL_TEMPLATE_RESULT (tmpl));
1904 : 11 : if (*decl == pattern)
1905 : 11 : TREE_UNAVAILABLE (tmpl) = true;
1906 : : }
1907 : : }
1908 : :
1909 : : /* Walks through the namespace- or function-scope anonymous union
1910 : : OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1911 : : Returns one of the fields for use in the mangled name. */
1912 : :
1913 : : static tree
1914 : 343 : build_anon_union_vars (tree type, tree object)
1915 : : {
1916 : 343 : tree main_decl = NULL_TREE;
1917 : 343 : tree field;
1918 : :
1919 : : /* Rather than write the code to handle the non-union case,
1920 : : just give an error. */
1921 : 343 : if (TREE_CODE (type) != UNION_TYPE)
1922 : : {
1923 : 8 : error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
1924 : : "anonymous struct not inside named type");
1925 : 8 : return error_mark_node;
1926 : : }
1927 : :
1928 : 335 : for (field = TYPE_FIELDS (type);
1929 : 1223 : field != NULL_TREE;
1930 : 888 : field = DECL_CHAIN (field))
1931 : : {
1932 : 888 : tree decl;
1933 : 888 : tree ref;
1934 : :
1935 : 888 : if (DECL_ARTIFICIAL (field))
1936 : 433 : continue;
1937 : 455 : if (TREE_CODE (field) != FIELD_DECL)
1938 : : {
1939 : 0 : permerror (DECL_SOURCE_LOCATION (field),
1940 : : "%q#D invalid; an anonymous union can only "
1941 : : "have non-static data members", field);
1942 : 0 : continue;
1943 : : }
1944 : :
1945 : 455 : if (TREE_PRIVATE (field))
1946 : 8 : permerror (DECL_SOURCE_LOCATION (field),
1947 : : "private member %q#D in anonymous union", field);
1948 : 447 : else if (TREE_PROTECTED (field))
1949 : 0 : permerror (DECL_SOURCE_LOCATION (field),
1950 : : "protected member %q#D in anonymous union", field);
1951 : :
1952 : 455 : if (processing_template_decl)
1953 : 73 : ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1954 : 73 : DECL_NAME (field), NULL_TREE);
1955 : : else
1956 : 382 : ref = build_class_member_access_expr (object, field, NULL_TREE,
1957 : : false, tf_warning_or_error);
1958 : :
1959 : 455 : if (DECL_NAME (field))
1960 : : {
1961 : 424 : tree base;
1962 : :
1963 : 424 : decl = build_decl (input_location,
1964 : 424 : VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1965 : 424 : DECL_ANON_UNION_VAR_P (decl) = 1;
1966 : 424 : DECL_ARTIFICIAL (decl) = 1;
1967 : :
1968 : 424 : base = get_base_address (object);
1969 : 424 : TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1970 : 424 : TREE_STATIC (decl) = TREE_STATIC (base);
1971 : 424 : DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1972 : :
1973 : 424 : SET_DECL_VALUE_EXPR (decl, ref);
1974 : 424 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
1975 : :
1976 : 424 : decl = pushdecl (decl);
1977 : : }
1978 : 31 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1979 : 31 : decl = build_anon_union_vars (TREE_TYPE (field), ref);
1980 : : else
1981 : : decl = 0;
1982 : :
1983 : 455 : if (main_decl == NULL_TREE)
1984 : 888 : main_decl = decl;
1985 : : }
1986 : :
1987 : : return main_decl;
1988 : : }
1989 : :
1990 : : /* Finish off the processing of a UNION_TYPE structure. If the union is an
1991 : : anonymous union, then all members must be laid out together. PUBLIC_P
1992 : : is nonzero if this union is not declared static. */
1993 : :
1994 : : void
1995 : 322 : finish_anon_union (tree anon_union_decl)
1996 : : {
1997 : 322 : tree type;
1998 : 322 : tree main_decl;
1999 : 322 : bool public_p;
2000 : :
2001 : 322 : if (anon_union_decl == error_mark_node)
2002 : : return;
2003 : :
2004 : 312 : type = TREE_TYPE (anon_union_decl);
2005 : 312 : public_p = TREE_PUBLIC (anon_union_decl);
2006 : :
2007 : : /* The VAR_DECL's context is the same as the TYPE's context. */
2008 : 312 : DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
2009 : :
2010 : 312 : if (TYPE_FIELDS (type) == NULL_TREE)
2011 : : return;
2012 : :
2013 : 312 : if (public_p)
2014 : : {
2015 : 0 : error ("namespace-scope anonymous aggregates must be static");
2016 : 0 : return;
2017 : : }
2018 : :
2019 : 312 : main_decl = build_anon_union_vars (type, anon_union_decl);
2020 : 312 : if (main_decl == error_mark_node)
2021 : : return;
2022 : 285 : if (main_decl == NULL_TREE)
2023 : : {
2024 : 15 : pedwarn (input_location, 0, "anonymous union with no members");
2025 : 15 : return;
2026 : : }
2027 : :
2028 : 270 : if (!processing_template_decl)
2029 : : {
2030 : : /* Use main_decl to set the mangled name. */
2031 : 226 : DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
2032 : 226 : maybe_commonize_var (anon_union_decl);
2033 : 226 : if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
2034 : : {
2035 : 114 : if (DECL_DISCRIMINATOR_P (anon_union_decl))
2036 : 55 : determine_local_discriminator (anon_union_decl);
2037 : 114 : mangle_decl (anon_union_decl);
2038 : : }
2039 : 226 : DECL_NAME (anon_union_decl) = NULL_TREE;
2040 : : }
2041 : :
2042 : 270 : pushdecl (anon_union_decl);
2043 : 270 : cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
2044 : : }
2045 : :
2046 : : /* Auxiliary functions to make type signatures for
2047 : : `operator new' and `operator delete' correspond to
2048 : : what compiler will be expecting. */
2049 : :
2050 : : tree
2051 : 137773 : coerce_new_type (tree type, location_t loc)
2052 : : {
2053 : 137773 : int e = 0;
2054 : 137773 : tree args = TYPE_ARG_TYPES (type);
2055 : :
2056 : 137773 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2057 : :
2058 : 137773 : if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2059 : : {
2060 : 8 : e = 1;
2061 : 8 : error_at (loc, "%<operator new%> must return type %qT",
2062 : : ptr_type_node);
2063 : : }
2064 : :
2065 : 137773 : if (args && args != void_list_node)
2066 : : {
2067 : 137769 : if (TREE_PURPOSE (args))
2068 : : {
2069 : : /* [basic.stc.dynamic.allocation]
2070 : :
2071 : : The first parameter shall not have an associated default
2072 : : argument. */
2073 : 24 : error_at (loc, "the first parameter of %<operator new%> cannot "
2074 : : "have a default argument");
2075 : : /* Throw away the default argument. */
2076 : 24 : TREE_PURPOSE (args) = NULL_TREE;
2077 : : }
2078 : :
2079 : 137769 : if (!same_type_p (TREE_VALUE (args), size_type_node))
2080 : : {
2081 : 16 : e = 2;
2082 : 16 : args = TREE_CHAIN (args);
2083 : : }
2084 : : }
2085 : : else
2086 : : e = 2;
2087 : :
2088 : 16 : if (e == 2)
2089 : 20 : permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
2090 : : "as first parameter", size_type_node);
2091 : :
2092 : 137773 : switch (e)
2093 : : {
2094 : 20 : case 2:
2095 : 20 : args = tree_cons (NULL_TREE, size_type_node, args);
2096 : : /* Fall through. */
2097 : 28 : case 1:
2098 : 28 : type = (cxx_copy_lang_qualifiers
2099 : 28 : (build_function_type (ptr_type_node, args),
2100 : : type));
2101 : : /* Fall through. */
2102 : 137773 : default:;
2103 : : }
2104 : 137773 : return type;
2105 : : }
2106 : :
2107 : : void
2108 : 181884 : coerce_delete_type (tree decl, location_t loc)
2109 : : {
2110 : 181884 : int e = 0;
2111 : 181884 : tree type = TREE_TYPE (decl);
2112 : 181884 : tree args = TYPE_ARG_TYPES (type);
2113 : :
2114 : 181884 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2115 : :
2116 : 181884 : if (!same_type_p (TREE_TYPE (type), void_type_node))
2117 : : {
2118 : 14 : e = 1;
2119 : 14 : error_at (loc, "%<operator delete%> must return type %qT",
2120 : : void_type_node);
2121 : : }
2122 : :
2123 : 181884 : tree ptrtype = ptr_type_node;
2124 : 181884 : if (destroying_delete_p (decl))
2125 : : {
2126 : 14 : if (DECL_CLASS_SCOPE_P (decl))
2127 : : /* If the function is a destroying operator delete declared in class
2128 : : type C, the type of its first parameter shall be C*. */
2129 : 14 : ptrtype = build_pointer_type (DECL_CONTEXT (decl));
2130 : : else
2131 : : /* A destroying operator delete shall be a class member function named
2132 : : operator delete. */
2133 : 0 : error_at (loc,
2134 : : "destroying %<operator delete%> must be a member function");
2135 : 14 : const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl));
2136 : 14 : if (op->flags & OVL_OP_FLAG_VEC)
2137 : 0 : error_at (loc, "%<operator delete[]%> cannot be a destroying delete");
2138 : 14 : if (!usual_deallocation_fn_p (decl))
2139 : 0 : error_at (loc, "destroying %<operator delete%> must be a usual "
2140 : : "deallocation function");
2141 : : }
2142 : :
2143 : 181880 : if (!args || args == void_list_node
2144 : 363760 : || !same_type_p (TREE_VALUE (args), ptrtype))
2145 : : {
2146 : 17 : e = 2;
2147 : 17 : if (args && args != void_list_node)
2148 : 9 : args = TREE_CHAIN (args);
2149 : 17 : error_at (loc, "%<operator delete%> takes type %qT as first parameter",
2150 : : ptrtype);
2151 : : }
2152 : 181867 : switch (e)
2153 : : {
2154 : 17 : case 2:
2155 : 17 : args = tree_cons (NULL_TREE, ptrtype, args);
2156 : : /* Fall through. */
2157 : 31 : case 1:
2158 : 31 : type = (cxx_copy_lang_qualifiers
2159 : 31 : (build_function_type (void_type_node, args),
2160 : : type));
2161 : : /* Fall through. */
2162 : 181884 : default:;
2163 : : }
2164 : :
2165 : 181884 : TREE_TYPE (decl) = type;
2166 : 181884 : }
2167 : :
2168 : : /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
2169 : : and mark them as needed. */
2170 : :
2171 : : static void
2172 : 288330 : mark_vtable_entries (tree decl, vec<tree> &consteval_vtables)
2173 : : {
2174 : : /* It's OK for the vtable to refer to deprecated virtual functions. */
2175 : 288330 : warning_sentinel w(warn_deprecated_decl);
2176 : :
2177 : 288330 : bool consteval_seen = false;
2178 : :
2179 : 4926607 : for (auto &e: CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
2180 : : {
2181 : 4061617 : tree fnaddr = e.value;
2182 : :
2183 : 4061617 : STRIP_NOPS (fnaddr);
2184 : :
2185 : 6420590 : if (TREE_CODE (fnaddr) != ADDR_EXPR
2186 : 4061617 : && TREE_CODE (fnaddr) != FDESC_EXPR)
2187 : : /* This entry is an offset: a virtual base class offset, a
2188 : : virtual call offset, an RTTI offset, etc. */
2189 : 2358973 : continue;
2190 : :
2191 : 1702644 : tree fn = TREE_OPERAND (fnaddr, 0);
2192 : 3038530 : if (TREE_CODE (fn) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (fn))
2193 : : {
2194 : 29 : if (!consteval_seen)
2195 : : {
2196 : 14 : consteval_seen = true;
2197 : 14 : consteval_vtables.safe_push (decl);
2198 : : }
2199 : 29 : continue;
2200 : : }
2201 : 1702615 : TREE_ADDRESSABLE (fn) = 1;
2202 : : /* When we don't have vcall offsets, we output thunks whenever
2203 : : we output the vtables that contain them. With vcall offsets,
2204 : : we know all the thunks we'll need when we emit a virtual
2205 : : function, so we emit the thunks there instead. */
2206 : 1702615 : if (DECL_THUNK_P (fn))
2207 : 4815 : use_thunk (fn, /*emit_p=*/0);
2208 : : /* Set the location, as marking the function could cause
2209 : : instantiation. We do not need to preserve the incoming
2210 : : location, as we're called from c_parse_final_cleanups, which
2211 : : takes care of that. */
2212 : 1702615 : input_location = DECL_SOURCE_LOCATION (fn);
2213 : 1702615 : mark_used (fn);
2214 : : }
2215 : 288330 : }
2216 : :
2217 : : /* Replace any consteval functions in vtables with null pointers. */
2218 : :
2219 : : static void
2220 : 99146 : clear_consteval_vfns (vec<tree> &consteval_vtables)
2221 : : {
2222 : 99178 : for (tree vtable : consteval_vtables)
2223 : 106 : for (constructor_elt &elt : CONSTRUCTOR_ELTS (DECL_INITIAL (vtable)))
2224 : : {
2225 : 64 : tree fn = cp_get_fndecl_from_callee (elt.value, /*fold*/false);
2226 : 98 : if (fn && DECL_IMMEDIATE_FUNCTION_P (fn))
2227 : 29 : elt.value = build_zero_cst (vtable_entry_type);
2228 : : }
2229 : 99146 : }
2230 : :
2231 : : /* Adjust the TLS model on variable DECL if need be, typically after
2232 : : the linkage of DECL has been modified. */
2233 : :
2234 : : static void
2235 : 20623279 : adjust_var_decl_tls_model (tree decl)
2236 : : {
2237 : 20623279 : if (CP_DECL_THREAD_LOCAL_P (decl)
2238 : 20623279 : && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
2239 : 78 : set_decl_tls_model (decl, decl_default_tls_model (decl));
2240 : 20623279 : }
2241 : :
2242 : : /* Set DECL up to have the closest approximation of "initialized common"
2243 : : linkage available. */
2244 : :
2245 : : void
2246 : 57771398 : comdat_linkage (tree decl)
2247 : : {
2248 : 57771398 : if (flag_weak)
2249 : : {
2250 : 57771367 : make_decl_one_only (decl, cxx_comdat_group (decl));
2251 : 57771367 : if (HAVE_COMDAT_GROUP && flag_contracts && DECL_CONTRACTS (decl))
2252 : : {
2253 : 244 : symtab_node *n = symtab_node::get (decl);
2254 : 244 : if (tree pre = DECL_PRE_FN (decl))
2255 : 191 : cgraph_node::get_create (pre)->add_to_same_comdat_group (n);
2256 : 244 : if (tree post = DECL_POST_FN (decl))
2257 : 34 : cgraph_node::get_create (post)->add_to_same_comdat_group (n);
2258 : : }
2259 : : }
2260 : 31 : else if (TREE_CODE (decl) == FUNCTION_DECL
2261 : 31 : || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
2262 : : /* We can just emit function and compiler-generated variables
2263 : : statically; having multiple copies is (for the most part) only
2264 : : a waste of space.
2265 : :
2266 : : There are two correctness issues, however: the address of a
2267 : : template instantiation with external linkage should be the
2268 : : same, independent of what translation unit asks for the
2269 : : address, and this will not hold when we emit multiple copies of
2270 : : the function. However, there's little else we can do.
2271 : :
2272 : : Also, by default, the typeinfo implementation assumes that
2273 : : there will be only one copy of the string used as the name for
2274 : : each type. Therefore, if weak symbols are unavailable, the
2275 : : run-time library should perform a more conservative check; it
2276 : : should perform a string comparison, rather than an address
2277 : : comparison. */
2278 : 31 : TREE_PUBLIC (decl) = 0;
2279 : : else
2280 : : {
2281 : : /* Static data member template instantiations, however, cannot
2282 : : have multiple copies. */
2283 : 0 : if (DECL_INITIAL (decl) == 0
2284 : 0 : || DECL_INITIAL (decl) == error_mark_node)
2285 : 0 : DECL_COMMON (decl) = 1;
2286 : 0 : else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2287 : : {
2288 : 0 : DECL_COMMON (decl) = 1;
2289 : 0 : DECL_INITIAL (decl) = error_mark_node;
2290 : : }
2291 : 0 : else if (!DECL_EXPLICIT_INSTANTIATION (decl))
2292 : : {
2293 : : /* We can't do anything useful; leave vars for explicit
2294 : : instantiation. */
2295 : 0 : DECL_EXTERNAL (decl) = 1;
2296 : 0 : DECL_NOT_REALLY_EXTERN (decl) = 0;
2297 : : }
2298 : : }
2299 : :
2300 : 57771398 : if (TREE_PUBLIC (decl))
2301 : 57771367 : DECL_COMDAT (decl) = 1;
2302 : :
2303 : 57771398 : if (VAR_P (decl))
2304 : 20622738 : adjust_var_decl_tls_model (decl);
2305 : 57771398 : }
2306 : :
2307 : : /* For win32 we also want to put explicit instantiations in
2308 : : linkonce sections, so that they will be merged with implicit
2309 : : instantiations; otherwise we get duplicate symbol errors.
2310 : : For Darwin we do not want explicit instantiations to be
2311 : : linkonce. */
2312 : :
2313 : : void
2314 : 31032 : maybe_make_one_only (tree decl)
2315 : : {
2316 : : /* We used to say that this was not necessary on targets that support weak
2317 : : symbols, because the implicit instantiations will defer to the explicit
2318 : : one. However, that's not actually the case in SVR4; a strong definition
2319 : : after a weak one is an error. Also, not making explicit
2320 : : instantiations one_only means that we can end up with two copies of
2321 : : some template instantiations. */
2322 : 31032 : if (! flag_weak)
2323 : : return;
2324 : :
2325 : : /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
2326 : : we can get away with not emitting them if they aren't used. We need
2327 : : to for variables so that cp_finish_decl will update their linkage,
2328 : : because their DECL_INITIAL may not have been set properly yet. */
2329 : :
2330 : 31028 : if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
2331 : : || (! DECL_EXPLICIT_INSTANTIATION (decl)
2332 : : && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
2333 : : {
2334 : 31028 : make_decl_one_only (decl, cxx_comdat_group (decl));
2335 : :
2336 : 31028 : if (VAR_P (decl))
2337 : : {
2338 : 541 : varpool_node *node = varpool_node::get_create (decl);
2339 : 541 : DECL_COMDAT (decl) = 1;
2340 : : /* Mark it needed so we don't forget to emit it. */
2341 : 541 : node->forced_by_abi = true;
2342 : 541 : TREE_USED (decl) = 1;
2343 : :
2344 : 541 : adjust_var_decl_tls_model (decl);
2345 : : }
2346 : : }
2347 : : }
2348 : :
2349 : : /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
2350 : : This predicate will give the right answer during parsing of the
2351 : : function, which other tests may not. */
2352 : :
2353 : : bool
2354 : 26856019 : vague_linkage_p (tree decl)
2355 : : {
2356 : 26856022 : if (!TREE_PUBLIC (decl))
2357 : : {
2358 : : /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
2359 : : maybe-in-charge 'tor variants; in that case we need to check one of
2360 : : the "clones" for the real linkage. But only in that case; before
2361 : : maybe_clone_body we haven't yet copied the linkage to the clones. */
2362 : 11269 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
2363 : 95 : && !DECL_ABSTRACT_P (decl)
2364 : 3 : && DECL_CHAIN (decl)
2365 : 5675 : && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
2366 : 3 : return vague_linkage_p (DECL_CHAIN (decl));
2367 : :
2368 : 5669 : gcc_checking_assert (!DECL_COMDAT (decl));
2369 : : return false;
2370 : : }
2371 : : /* Unfortunately, import_export_decl has not always been called
2372 : : before the function is processed, so we cannot simply check
2373 : : DECL_COMDAT. */
2374 : 26850350 : if (DECL_COMDAT (decl)
2375 : 866298 : || (TREE_CODE (decl) == FUNCTION_DECL
2376 : 865891 : && DECL_DECLARED_INLINE_P (decl))
2377 : 52311 : || (DECL_LANG_SPECIFIC (decl)
2378 : 52010 : && DECL_TEMPLATE_INSTANTIATION (decl))
2379 : 26872321 : || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
2380 : : return true;
2381 : 21961 : else if (DECL_FUNCTION_SCOPE_P (decl))
2382 : : /* A local static in an inline effectively has vague linkage. */
2383 : 0 : return (TREE_STATIC (decl)
2384 : 0 : && vague_linkage_p (DECL_CONTEXT (decl)));
2385 : : else
2386 : : return false;
2387 : : }
2388 : :
2389 : : /* Determine whether or not we want to specifically import or export CTYPE,
2390 : : using various heuristics. */
2391 : :
2392 : : static void
2393 : 3551626 : import_export_class (tree ctype)
2394 : : {
2395 : : /* -1 for imported, 1 for exported. */
2396 : 3551626 : int import_export = 0;
2397 : :
2398 : : /* It only makes sense to call this function at EOF. The reason is
2399 : : that this function looks at whether or not the first non-inline
2400 : : non-abstract virtual member function has been defined in this
2401 : : translation unit. But, we can't possibly know that until we've
2402 : : seen the entire translation unit. */
2403 : 3551626 : gcc_assert (at_eof);
2404 : :
2405 : 3551626 : if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2406 : : return;
2407 : :
2408 : : /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2409 : : we will have CLASSTYPE_INTERFACE_ONLY set but not
2410 : : CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2411 : : heuristic because someone will supply a #pragma implementation
2412 : : elsewhere, and deducing it here would produce a conflict. */
2413 : 1768079 : if (CLASSTYPE_INTERFACE_ONLY (ctype))
2414 : : return;
2415 : :
2416 : 1768079 : if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2417 : : import_export = -1;
2418 : 1768079 : else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2419 : : import_export = 1;
2420 : 1768079 : else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2421 : 1768079 : && !flag_implicit_templates)
2422 : : /* For a template class, without -fimplicit-templates, check the
2423 : : repository. If the virtual table is assigned to this
2424 : : translation unit, then export the class; otherwise, import
2425 : : it. */
2426 : : import_export = -1;
2427 : 1767435 : else if (TYPE_POLYMORPHIC_P (ctype))
2428 : : {
2429 : : /* The ABI specifies that the virtual table and associated
2430 : : information are emitted with the key method, if any. */
2431 : 1571541 : tree method = CLASSTYPE_KEY_METHOD (ctype);
2432 : : /* If weak symbol support is not available, then we must be
2433 : : careful not to emit the vtable when the key function is
2434 : : inline. An inline function can be defined in multiple
2435 : : translation units. If we were to emit the vtable in each
2436 : : translation unit containing a definition, we would get
2437 : : multiple definition errors at link-time. */
2438 : 1571541 : if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2439 : 557530 : import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2440 : : }
2441 : :
2442 : : /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2443 : : a definition anywhere else. */
2444 : 1768079 : if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2445 : : import_export = 0;
2446 : :
2447 : : /* Allow back ends the chance to overrule the decision. */
2448 : 1768079 : if (targetm.cxx.import_export_class)
2449 : 0 : import_export = targetm.cxx.import_export_class (ctype, import_export);
2450 : :
2451 : 1768079 : if (import_export)
2452 : : {
2453 : 558174 : SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2454 : 558174 : CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2455 : : }
2456 : : }
2457 : :
2458 : : /* Return true if VAR has already been provided to the back end; in that
2459 : : case VAR should not be modified further by the front end. */
2460 : : static bool
2461 : 51550871 : var_finalized_p (tree var)
2462 : : {
2463 : 0 : return varpool_node::get_create (var)->definition;
2464 : : }
2465 : :
2466 : : /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2467 : : must be emitted in this translation unit. Mark it as such. */
2468 : :
2469 : : void
2470 : 49931 : mark_needed (tree decl)
2471 : : {
2472 : 49931 : TREE_USED (decl) = 1;
2473 : 49931 : if (TREE_CODE (decl) == FUNCTION_DECL)
2474 : : {
2475 : : /* Extern inline functions don't become needed when referenced.
2476 : : If we know a method will be emitted in other TU and no new
2477 : : functions can be marked reachable, just use the external
2478 : : definition. */
2479 : 40968 : struct cgraph_node *node = cgraph_node::get_create (decl);
2480 : 40968 : node->forced_by_abi = true;
2481 : :
2482 : : /* #pragma interface can call mark_needed for
2483 : : maybe-in-charge 'tors; mark the clones as well. */
2484 : 40968 : tree clone;
2485 : 49150 : FOR_EACH_CLONE (clone, decl)
2486 : 8182 : mark_needed (clone);
2487 : : }
2488 : 8963 : else if (VAR_P (decl))
2489 : : {
2490 : 8963 : varpool_node *node = varpool_node::get_create (decl);
2491 : : /* C++ frontend use mark_decl_references to force COMDAT variables
2492 : : to be output that might appear dead otherwise. */
2493 : 8963 : node->forced_by_abi = true;
2494 : : }
2495 : 49931 : }
2496 : :
2497 : : /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2498 : : returns true if a definition of this entity should be provided in
2499 : : this object file. Callers use this function to determine whether
2500 : : or not to let the back end know that a definition of DECL is
2501 : : available in this translation unit. */
2502 : :
2503 : : bool
2504 : 194763897 : decl_needed_p (tree decl)
2505 : : {
2506 : 194763897 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2507 : : /* This function should only be called at the end of the translation
2508 : : unit. We cannot be sure of whether or not something will be
2509 : : COMDAT until that point. */
2510 : 194763897 : gcc_assert (at_eof);
2511 : :
2512 : : /* All entities with external linkage that are not COMDAT/EXTERN should be
2513 : : emitted; they may be referred to from other object files. */
2514 : 194763897 : if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2515 : : return true;
2516 : :
2517 : : /* Functions marked "dllexport" must be emitted so that they are
2518 : : visible to other DLLs. */
2519 : 194763589 : if (flag_keep_inline_dllexport
2520 : 194763589 : && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2521 : : return true;
2522 : :
2523 : : /* When not optimizing, do not bother to produce definitions for extern
2524 : : symbols. */
2525 : 250930123 : if (DECL_REALLY_EXTERN (decl)
2526 : 0 : && ((TREE_CODE (decl) != FUNCTION_DECL
2527 : 0 : && !optimize)
2528 : 0 : || (TREE_CODE (decl) == FUNCTION_DECL
2529 : 0 : && !opt_for_fn (decl, optimize)))
2530 : 194763589 : && !lookup_attribute ("always_inline", decl))
2531 : : return false;
2532 : :
2533 : : /* If this entity was used, let the back end see it; it will decide
2534 : : whether or not to emit it into the object file. */
2535 : 194763589 : if (TREE_USED (decl))
2536 : : return true;
2537 : :
2538 : : /* Virtual functions might be needed for devirtualization. */
2539 : 48898232 : if (flag_devirtualize
2540 : 48105577 : && TREE_CODE (decl) == FUNCTION_DECL
2541 : 96143814 : && DECL_VIRTUAL_P (decl))
2542 : : return true;
2543 : :
2544 : : /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2545 : : reference to DECL might cause it to be emitted later. */
2546 : : return false;
2547 : : }
2548 : :
2549 : : /* If necessary, write out the vtables for the dynamic class CTYPE.
2550 : : Returns true if any vtables were emitted. */
2551 : :
2552 : : static bool
2553 : 3353819 : maybe_emit_vtables (tree ctype, vec<tree> &consteval_vtables)
2554 : : {
2555 : 3353819 : tree vtbl;
2556 : 3353819 : tree primary_vtbl;
2557 : 3353819 : int needed = 0;
2558 : 3353819 : varpool_node *current = NULL, *last = NULL;
2559 : :
2560 : : /* If the vtables for this class have already been emitted there is
2561 : : nothing more to do. */
2562 : 3353819 : primary_vtbl = CLASSTYPE_VTABLES (ctype);
2563 : 3353819 : if (var_finalized_p (primary_vtbl))
2564 : : return false;
2565 : : /* Ignore dummy vtables made by get_vtable_decl. */
2566 : 3353819 : if (TREE_TYPE (primary_vtbl) == void_type_node)
2567 : : return false;
2568 : :
2569 : : /* On some targets, we cannot determine the key method until the end
2570 : : of the translation unit -- which is when this function is
2571 : : called. */
2572 : 3353819 : if (!targetm.cxx.key_method_may_be_inline ())
2573 : 0 : determine_key_method (ctype);
2574 : :
2575 : : /* See if any of the vtables are needed. */
2576 : 8370844 : for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2577 : : {
2578 : 5017025 : import_export_decl (vtbl);
2579 : 5017025 : if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2580 : : needed = 1;
2581 : : }
2582 : 3353819 : if (!needed)
2583 : : {
2584 : : /* If the references to this class' vtables are optimized away,
2585 : : still emit the appropriate debugging information. See
2586 : : dfs_debug_mark. */
2587 : 3086783 : if (DECL_COMDAT (primary_vtbl)
2588 : 3086783 : && CLASSTYPE_DEBUG_REQUESTED (ctype))
2589 : 38690 : note_debug_info_needed (ctype);
2590 : 3086783 : return false;
2591 : : }
2592 : :
2593 : : /* The ABI requires that we emit all of the vtables if we emit any
2594 : : of them. */
2595 : 555366 : for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2596 : : {
2597 : : /* Mark entities references from the virtual table as used. */
2598 : 288330 : mark_vtable_entries (vtbl, consteval_vtables);
2599 : :
2600 : 288330 : if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2601 : : {
2602 : 0 : vec<tree, va_gc> *cleanups = NULL;
2603 : 0 : tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2604 : : LOOKUP_NORMAL);
2605 : :
2606 : : /* It had better be all done at compile-time. */
2607 : 0 : gcc_assert (!expr && !cleanups);
2608 : : }
2609 : :
2610 : : /* Write it out. */
2611 : 288330 : DECL_EXTERNAL (vtbl) = 0;
2612 : 288330 : rest_of_decl_compilation (vtbl, 1, 1);
2613 : :
2614 : : /* Because we're only doing syntax-checking, we'll never end up
2615 : : actually marking the variable as written. */
2616 : 288330 : if (flag_syntax_only)
2617 : 41 : TREE_ASM_WRITTEN (vtbl) = 1;
2618 : 288289 : else if (DECL_ONE_ONLY (vtbl))
2619 : : {
2620 : 287503 : current = varpool_node::get_create (vtbl);
2621 : 287503 : if (last)
2622 : 21198 : current->add_to_same_comdat_group (last);
2623 : : last = current;
2624 : : }
2625 : : }
2626 : :
2627 : : /* For abstract classes, the destructor has been removed from the
2628 : : vtable (in class.cc's build_vtbl_initializer). For a compiler-
2629 : : generated destructor, it hence might not have been generated in
2630 : : this translation unit - and with '#pragma interface' it might
2631 : : never get generated. */
2632 : 267036 : if (CLASSTYPE_PURE_VIRTUALS (ctype)
2633 : 66863 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype)
2634 : 54088 : && !CLASSTYPE_LAZY_DESTRUCTOR (ctype)
2635 : 321120 : && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype)))
2636 : 58 : note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype));
2637 : :
2638 : : /* Since we're writing out the vtable here, also write the debug
2639 : : info. */
2640 : 267036 : note_debug_info_needed (ctype);
2641 : :
2642 : 267036 : return true;
2643 : : }
2644 : :
2645 : : /* A special return value from type_visibility meaning internal
2646 : : linkage. */
2647 : :
2648 : : enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2649 : :
2650 : : static int expr_visibility (tree);
2651 : : static int type_visibility (tree);
2652 : :
2653 : : /* walk_tree helper function for type_visibility. */
2654 : :
2655 : : static tree
2656 : 2486794698 : min_vis_r (tree *tp, int *walk_subtrees, void *data)
2657 : : {
2658 : 2486794698 : int *vis_p = (int *)data;
2659 : 2486794698 : int this_vis = VISIBILITY_DEFAULT;
2660 : 2486794698 : if (! TYPE_P (*tp))
2661 : 36408608 : *walk_subtrees = 0;
2662 : 2450386090 : else if (OVERLOAD_TYPE_P (*tp)
2663 : 3216986649 : && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2664 : : {
2665 : 1251992 : this_vis = VISIBILITY_ANON;
2666 : 1251992 : *walk_subtrees = 0;
2667 : : }
2668 : 2449134098 : else if (CLASS_TYPE_P (*tp))
2669 : : {
2670 : 745714797 : this_vis = CLASSTYPE_VISIBILITY (*tp);
2671 : 745714797 : *walk_subtrees = 0;
2672 : : }
2673 : 1703419301 : else if (TREE_CODE (*tp) == ARRAY_TYPE
2674 : 1703419301 : && uses_template_parms (TYPE_DOMAIN (*tp)))
2675 : 334145 : this_vis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp)));
2676 : :
2677 : 2486794698 : if (this_vis > *vis_p)
2678 : 1897397 : *vis_p = this_vis;
2679 : :
2680 : : /* Tell cp_walk_subtrees to look through typedefs. */
2681 : 2486794698 : if (*walk_subtrees == 1)
2682 : 1703419301 : *walk_subtrees = 2;
2683 : :
2684 : 2486794698 : return NULL;
2685 : : }
2686 : :
2687 : : /* walk_tree helper function for expr_visibility. */
2688 : :
2689 : : static tree
2690 : 247235108 : min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
2691 : : {
2692 : 247235108 : int *vis_p = (int *)data;
2693 : 247235108 : int tpvis = VISIBILITY_DEFAULT;
2694 : :
2695 : 247235108 : tree t = *tp;
2696 : 247235108 : if (TREE_CODE (t) == PTRMEM_CST)
2697 : 673 : t = PTRMEM_CST_MEMBER (t);
2698 : 247235108 : switch (TREE_CODE (t))
2699 : : {
2700 : 3257532 : case CAST_EXPR:
2701 : 3257532 : case IMPLICIT_CONV_EXPR:
2702 : 3257532 : case STATIC_CAST_EXPR:
2703 : 3257532 : case REINTERPRET_CAST_EXPR:
2704 : 3257532 : case CONST_CAST_EXPR:
2705 : 3257532 : case DYNAMIC_CAST_EXPR:
2706 : 3257532 : case NEW_EXPR:
2707 : 3257532 : case CONSTRUCTOR:
2708 : 3257532 : case LAMBDA_EXPR:
2709 : 3257532 : tpvis = type_visibility (TREE_TYPE (t));
2710 : 3257532 : break;
2711 : :
2712 : 6632002 : case TEMPLATE_DECL:
2713 : 6632002 : if (DECL_ALIAS_TEMPLATE_P (t))
2714 : : /* FIXME: We don't maintain TREE_PUBLIC / DECL_VISIBILITY for
2715 : : alias templates so we can't trust it here (PR107906). */
2716 : : break;
2717 : 6393793 : t = DECL_TEMPLATE_RESULT (t);
2718 : : /* Fall through. */
2719 : 10900142 : case VAR_DECL:
2720 : 10900142 : case FUNCTION_DECL:
2721 : 10900142 : if (decl_constant_var_p (t))
2722 : : /* The ODR allows definitions in different TUs to refer to distinct
2723 : : constant variables with internal or no linkage, so such a reference
2724 : : shouldn't affect visibility (PR110323). FIXME but only if the
2725 : : lvalue-rvalue conversion is applied. */;
2726 : 7062809 : else if (! TREE_PUBLIC (t))
2727 : : tpvis = VISIBILITY_ANON;
2728 : : else
2729 : 6986460 : tpvis = DECL_VISIBILITY (t);
2730 : : break;
2731 : :
2732 : 517 : case FIELD_DECL:
2733 : 517 : tpvis = type_visibility (DECL_CONTEXT (t));
2734 : 517 : break;
2735 : :
2736 : : default:
2737 : : break;
2738 : : }
2739 : :
2740 : 247235108 : if (tpvis > *vis_p)
2741 : 73334 : *vis_p = tpvis;
2742 : :
2743 : 247235108 : return NULL_TREE;
2744 : : }
2745 : :
2746 : : /* Returns the visibility of TYPE, which is the minimum visibility of its
2747 : : component types. */
2748 : :
2749 : : static int
2750 : 836116940 : type_visibility (tree type)
2751 : : {
2752 : 836116940 : int vis = VISIBILITY_DEFAULT;
2753 : 836116940 : cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2754 : 836116940 : return vis;
2755 : : }
2756 : :
2757 : : /* Returns the visibility of an expression EXPR that appears in the signature
2758 : : of a function template, which is the minimum visibility of names that appear
2759 : : in its mangling. */
2760 : :
2761 : : static int
2762 : 64162052 : expr_visibility (tree expr)
2763 : : {
2764 : 64162052 : int vis = VISIBILITY_DEFAULT;
2765 : 64162052 : cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis);
2766 : 64162052 : return vis;
2767 : : }
2768 : :
2769 : : /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2770 : : specified (or if VISIBILITY is static). If TMPL is true, this
2771 : : constraint is for a template argument, and takes precedence
2772 : : over explicitly-specified visibility on the template. */
2773 : :
2774 : : static void
2775 : 52184778 : constrain_visibility (tree decl, int visibility, bool tmpl)
2776 : : {
2777 : 52184778 : if (visibility == VISIBILITY_ANON)
2778 : : {
2779 : : /* extern "C" declarations aren't affected by the anonymous
2780 : : namespace. */
2781 : 1725944 : if (!DECL_EXTERN_C_P (decl))
2782 : : {
2783 : 1725768 : TREE_PUBLIC (decl) = 0;
2784 : 1725768 : DECL_WEAK (decl) = 0;
2785 : 1725768 : DECL_COMMON (decl) = 0;
2786 : 1725768 : DECL_COMDAT (decl) = false;
2787 : 1725768 : if (VAR_OR_FUNCTION_DECL_P (decl))
2788 : : {
2789 : 646677 : struct symtab_node *snode = symtab_node::get (decl);
2790 : :
2791 : 646677 : if (snode)
2792 : 31964 : snode->set_comdat_group (NULL);
2793 : : }
2794 : 1725768 : DECL_INTERFACE_KNOWN (decl) = 1;
2795 : 1725768 : if (DECL_LANG_SPECIFIC (decl))
2796 : 649887 : DECL_NOT_REALLY_EXTERN (decl) = 1;
2797 : : }
2798 : : }
2799 : 50458834 : else if (visibility > DECL_VISIBILITY (decl)
2800 : 50458834 : && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2801 : : {
2802 : 346 : DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2803 : : /* This visibility was not specified. */
2804 : 346 : DECL_VISIBILITY_SPECIFIED (decl) = false;
2805 : : }
2806 : 52184778 : }
2807 : :
2808 : : /* Constrain the visibility of DECL based on the visibility of its template
2809 : : arguments. */
2810 : :
2811 : : static void
2812 : 252913163 : constrain_visibility_for_template (tree decl, tree targs)
2813 : : {
2814 : : /* If this is a template instantiation, check the innermost
2815 : : template args for visibility constraints. The outer template
2816 : : args are covered by the class check. */
2817 : 252913163 : tree args = INNERMOST_TEMPLATE_ARGS (targs);
2818 : 252913163 : int i;
2819 : 766891409 : for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2820 : : {
2821 : 513978246 : int vis = 0;
2822 : :
2823 : 513978246 : tree arg = TREE_VEC_ELT (args, i-1);
2824 : 513978246 : if (TYPE_P (arg))
2825 : 450822376 : vis = type_visibility (arg);
2826 : : else
2827 : 63155870 : vis = expr_visibility (arg);
2828 : 513978246 : if (vis)
2829 : 497398 : constrain_visibility (decl, vis, true);
2830 : : }
2831 : 252913163 : }
2832 : :
2833 : : /* Like c_determine_visibility, but with additional C++-specific
2834 : : behavior.
2835 : :
2836 : : Function-scope entities can rely on the function's visibility because
2837 : : it is set in start_preparsed_function.
2838 : :
2839 : : Class-scope entities cannot rely on the class's visibility until the end
2840 : : of the enclosing class definition.
2841 : :
2842 : : Note that because namespaces have multiple independent definitions,
2843 : : namespace visibility is handled elsewhere using the #pragma visibility
2844 : : machinery rather than by decorating the namespace declaration.
2845 : :
2846 : : The goal is for constraints from the type to give a diagnostic, and
2847 : : other constraints to be applied silently. */
2848 : :
2849 : : void
2850 : 522056779 : determine_visibility (tree decl)
2851 : : {
2852 : : /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2853 : :
2854 : : /* Only relevant for names with external linkage. */
2855 : 522056779 : if (!TREE_PUBLIC (decl))
2856 : : return;
2857 : :
2858 : : /* Cloned constructors and destructors get the same visibility as
2859 : : the underlying function. That should be set up in
2860 : : maybe_clone_body. */
2861 : 478625477 : gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2862 : :
2863 : 478625477 : bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2864 : 478625477 : enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2865 : :
2866 : : /* The decl may be a template instantiation, which could influence
2867 : : visibilty. */
2868 : 478625477 : tree template_decl = NULL_TREE;
2869 : 478625477 : if (TREE_CODE (decl) == TYPE_DECL)
2870 : : {
2871 : 138312036 : if (CLASS_TYPE_P (TREE_TYPE (decl)))
2872 : : {
2873 : 136031242 : if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2874 : 258149981 : template_decl = decl;
2875 : : }
2876 : 2280794 : else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2877 : 258149981 : template_decl = decl;
2878 : : }
2879 : 340313441 : else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2880 : : template_decl = decl;
2881 : :
2882 : 478625477 : if (TREE_CODE (decl) == TYPE_DECL
2883 : 273370576 : && LAMBDA_TYPE_P (TREE_TYPE (decl))
2884 : 479974678 : && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl)) != error_mark_node)
2885 : 672475 : if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
2886 : : {
2887 : : /* The lambda's visibility is limited by that of its extra
2888 : : scope. */
2889 : 672037 : int vis = 0;
2890 : 672037 : if (TYPE_P (extra))
2891 : 0 : vis = type_visibility (extra);
2892 : : else
2893 : 672037 : vis = expr_visibility (extra);
2894 : 672037 : constrain_visibility (decl, vis, false);
2895 : : }
2896 : :
2897 : : /* If DECL is a member of a class, visibility specifiers on the
2898 : : class can influence the visibility of the DECL. */
2899 : 478625477 : tree class_type = NULL_TREE;
2900 : 478625477 : if (DECL_CLASS_SCOPE_P (decl))
2901 : 251378129 : class_type = DECL_CONTEXT (decl);
2902 : : else
2903 : : {
2904 : : /* Not a class member. */
2905 : :
2906 : : /* Virtual tables have DECL_CONTEXT set to their associated class,
2907 : : so they are automatically handled above. */
2908 : 227247348 : gcc_assert (!VAR_P (decl)
2909 : : || !DECL_VTABLE_OR_VTT_P (decl));
2910 : :
2911 : 227247348 : if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2912 : : {
2913 : : /* Local statics and classes get the visibility of their
2914 : : containing function by default, except that
2915 : : -fvisibility-inlines-hidden doesn't affect them. */
2916 : 1533176 : tree fn = DECL_CONTEXT (decl);
2917 : 1533176 : if (DECL_VISIBILITY_SPECIFIED (fn))
2918 : : {
2919 : 1485991 : DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2920 : 1485991 : DECL_VISIBILITY_SPECIFIED (decl) =
2921 : 1485991 : DECL_VISIBILITY_SPECIFIED (fn);
2922 : : }
2923 : : else
2924 : : {
2925 : 47185 : if (DECL_CLASS_SCOPE_P (fn))
2926 : 26456 : determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2927 : 20729 : else if (determine_hidden_inline (fn))
2928 : : {
2929 : 8 : DECL_VISIBILITY (decl) = default_visibility;
2930 : 8 : DECL_VISIBILITY_SPECIFIED (decl) =
2931 : 8 : visibility_options.inpragma;
2932 : : }
2933 : : else
2934 : : {
2935 : 20721 : DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2936 : 20721 : DECL_VISIBILITY_SPECIFIED (decl) =
2937 : 20721 : DECL_VISIBILITY_SPECIFIED (fn);
2938 : : }
2939 : : }
2940 : :
2941 : : /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2942 : : but have no TEMPLATE_INFO, so don't try to check it. */
2943 : : template_decl = NULL_TREE;
2944 : : }
2945 : 14801941 : else if (VAR_P (decl) && DECL_TINFO_P (decl)
2946 : 230210694 : && flag_visibility_ms_compat)
2947 : : {
2948 : : /* Under -fvisibility-ms-compat, types are visible by default,
2949 : : even though their contents aren't. */
2950 : 112 : tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2951 : 112 : int underlying_vis = type_visibility (underlying_type);
2952 : 112 : if (underlying_vis == VISIBILITY_ANON
2953 : 112 : || (CLASS_TYPE_P (underlying_type)
2954 : 92 : && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2955 : 72 : constrain_visibility (decl, underlying_vis, false);
2956 : : else
2957 : 40 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2958 : : }
2959 : 225714060 : else if (VAR_P (decl) && DECL_TINFO_P (decl))
2960 : : {
2961 : : /* tinfo visibility is based on the type it's for. */
2962 : 4496410 : constrain_visibility
2963 : 4496410 : (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2964 : :
2965 : : /* Give the target a chance to override the visibility associated
2966 : : with DECL. */
2967 : 4496410 : if (TREE_PUBLIC (decl)
2968 : 4494273 : && !DECL_REALLY_EXTERN (decl)
2969 : 4494273 : && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2970 : 8958474 : && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2971 : 113157 : targetm.cxx.determine_class_data_visibility (decl);
2972 : : }
2973 : 221217650 : else if (template_decl)
2974 : : /* Template instantiations and specializations get visibility based
2975 : : on their template unless they override it with an attribute. */;
2976 : 80485269 : else if (! DECL_VISIBILITY_SPECIFIED (decl))
2977 : : {
2978 : 74881849 : if (determine_hidden_inline (decl))
2979 : 16 : DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2980 : : else
2981 : : {
2982 : : /* Set default visibility to whatever the user supplied with
2983 : : #pragma GCC visibility or a namespace visibility attribute. */
2984 : 74881833 : DECL_VISIBILITY (decl) = default_visibility;
2985 : 74881833 : DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2986 : : }
2987 : : }
2988 : : }
2989 : :
2990 : 336359920 : if (template_decl)
2991 : : {
2992 : : /* If the specialization doesn't specify visibility, use the
2993 : : visibility from the template. */
2994 : 257604507 : tree tinfo = get_template_info (template_decl);
2995 : 257604507 : tree args = TI_ARGS (tinfo);
2996 : 257604507 : tree attribs = (TREE_CODE (decl) == TYPE_DECL
2997 : 396567050 : ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2998 : 138962543 : : DECL_ATTRIBUTES (decl));
2999 : 257604507 : tree attr = lookup_attribute ("visibility", attribs);
3000 : :
3001 : 257604507 : if (args != error_mark_node)
3002 : : {
3003 : 257604507 : tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
3004 : :
3005 : 257604507 : if (!DECL_VISIBILITY_SPECIFIED (decl))
3006 : : {
3007 : 115626843 : if (!attr
3008 : 115626843 : && determine_hidden_inline (decl))
3009 : 44 : DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3010 : : else
3011 : : {
3012 : 115626799 : DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
3013 : 231253598 : DECL_VISIBILITY_SPECIFIED (decl)
3014 : 115626799 : = DECL_VISIBILITY_SPECIFIED (pattern);
3015 : : }
3016 : : }
3017 : :
3018 : 257604507 : if (args
3019 : : /* Template argument visibility outweighs #pragma or namespace
3020 : : visibility, but not an explicit attribute. */
3021 : 257604507 : && !attr)
3022 : : {
3023 : 257604479 : int depth = TMPL_ARGS_DEPTH (args);
3024 : 257604479 : if (DECL_VISIBILITY_SPECIFIED (decl))
3025 : : {
3026 : : /* A class template member with explicit visibility
3027 : : overrides the class visibility, so we need to apply
3028 : : all the levels of template args directly. */
3029 : : int i;
3030 : 471974842 : for (i = 1; i <= depth; ++i)
3031 : : {
3032 : 239539692 : tree lev = TMPL_ARGS_LEVEL (args, i);
3033 : 239539692 : constrain_visibility_for_template (decl, lev);
3034 : : }
3035 : : }
3036 : 25169329 : else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
3037 : : /* Limit visibility based on its template arguments. */
3038 : 13373471 : constrain_visibility_for_template (decl, args);
3039 : : }
3040 : : }
3041 : : }
3042 : :
3043 : 478625477 : if (class_type)
3044 : 251378129 : determine_visibility_from_class (decl, class_type);
3045 : :
3046 : 478625477 : if (decl_internal_context_p (decl))
3047 : : /* Names in an anonymous namespace get internal linkage. */
3048 : 299141 : constrain_visibility (decl, VISIBILITY_ANON, false);
3049 : 478326336 : else if (TREE_CODE (decl) != TYPE_DECL)
3050 : : {
3051 : : /* Propagate anonymity from type to decl. */
3052 : 340149641 : int tvis = type_visibility (TREE_TYPE (decl));
3053 : 340149641 : if (tvis == VISIBILITY_ANON
3054 : 340149641 : || ! DECL_VISIBILITY_SPECIFIED (decl))
3055 : 45487838 : constrain_visibility (decl, tvis, false);
3056 : : }
3057 : 138176695 : else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
3058 : : /* DR 757: A type without linkage shall not be used as the type of a
3059 : : variable or function with linkage, unless
3060 : : o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
3061 : : o the variable or function is not used (3.2 [basic.def.odr]) or is
3062 : : defined in the same translation unit.
3063 : :
3064 : : Since non-extern "C" decls need to be defined in the same
3065 : : translation unit, we can make the type internal. */
3066 : 731882 : constrain_visibility (decl, VISIBILITY_ANON, false);
3067 : :
3068 : : /* If visibility changed and DECL already has DECL_RTL, ensure
3069 : : symbol flags are updated. */
3070 : 478625477 : if ((DECL_VISIBILITY (decl) != orig_visibility
3071 : 477665766 : || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
3072 : 262110289 : && ((VAR_P (decl) && TREE_STATIC (decl))
3073 : 247951158 : || TREE_CODE (decl) == FUNCTION_DECL)
3074 : 723690862 : && DECL_RTL_SET_P (decl))
3075 : 0 : make_decl_rtl (decl);
3076 : : }
3077 : :
3078 : : /* By default, static data members and function members receive
3079 : : the visibility of their containing class. */
3080 : :
3081 : : static void
3082 : 251404585 : determine_visibility_from_class (tree decl, tree class_type)
3083 : : {
3084 : 251404585 : if (DECL_VISIBILITY_SPECIFIED (decl))
3085 : : return;
3086 : :
3087 : 127258051 : if (determine_hidden_inline (decl))
3088 : 187 : DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3089 : : else
3090 : : {
3091 : : /* Default to the class visibility. */
3092 : 127257864 : DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
3093 : 254515728 : DECL_VISIBILITY_SPECIFIED (decl)
3094 : 127257864 : = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
3095 : : }
3096 : :
3097 : : /* Give the target a chance to override the visibility associated
3098 : : with DECL. */
3099 : 127258051 : if (VAR_P (decl)
3100 : 127258051 : && TREE_PUBLIC (decl)
3101 : 24000804 : && (DECL_TINFO_P (decl) || DECL_VTABLE_OR_VTT_P (decl))
3102 : 2134054 : && !DECL_REALLY_EXTERN (decl)
3103 : 129392105 : && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
3104 : 80781 : targetm.cxx.determine_class_data_visibility (decl);
3105 : : }
3106 : :
3107 : : /* Returns true iff DECL is an inline that should get hidden visibility
3108 : : because of -fvisibility-inlines-hidden. */
3109 : :
3110 : : static bool
3111 : 317787472 : determine_hidden_inline (tree decl)
3112 : : {
3113 : 317787472 : return (visibility_options.inlines_hidden
3114 : : /* Don't do this for inline templates; specializations might not be
3115 : : inline, and we don't want them to inherit the hidden
3116 : : visibility. We'll set it here for all inline instantiations. */
3117 : 545 : && !processing_template_decl
3118 : 513 : && TREE_CODE (decl) == FUNCTION_DECL
3119 : 399 : && DECL_DECLARED_INLINE_P (decl)
3120 : 317787747 : && (! DECL_LANG_SPECIFIC (decl)
3121 : 275 : || ! DECL_EXPLICIT_INSTANTIATION (decl)));
3122 : : }
3123 : :
3124 : : /* Constrain the visibility of a class TYPE based on the visibility of its
3125 : : field types. Warn if any fields require lesser visibility. */
3126 : :
3127 : : void
3128 : 36807142 : constrain_class_visibility (tree type)
3129 : : {
3130 : 36807142 : tree binfo;
3131 : 36807142 : tree t;
3132 : 36807142 : int i;
3133 : :
3134 : 36807142 : int vis = type_visibility (type);
3135 : :
3136 : 36807142 : if (vis == VISIBILITY_ANON
3137 : 36807142 : || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
3138 : 36395279 : return;
3139 : :
3140 : : /* Don't warn about visibility if the class has explicit visibility. */
3141 : 411863 : if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
3142 : 46646 : vis = VISIBILITY_INTERNAL;
3143 : :
3144 : 3339461 : for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
3145 : 593210 : if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
3146 : 3520596 : && !DECL_ARTIFICIAL (t))
3147 : : {
3148 : 511944 : tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
3149 : 511944 : int subvis = type_visibility (ftype);
3150 : :
3151 : 511944 : if (subvis == VISIBILITY_ANON)
3152 : : {
3153 : 123 : if (!in_main_input_context())
3154 : : {
3155 : 44 : tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
3156 : 44 : if (nlt)
3157 : : {
3158 : 16 : if (same_type_p (TREE_TYPE (t), nlt))
3159 : 16 : warning (OPT_Wsubobject_linkage, "\
3160 : : %qT has a field %q#D whose type has no linkage",
3161 : : type, t);
3162 : : else
3163 : 0 : warning (OPT_Wsubobject_linkage, "\
3164 : : %qT has a field %qD whose type depends on the type %qT which has no linkage",
3165 : : type, t, nlt);
3166 : : }
3167 : 28 : else if (cxx_dialect > cxx98
3168 : 28 : && !decl_anon_ns_mem_p (ftype))
3169 : 3 : warning (OPT_Wsubobject_linkage, "\
3170 : : %qT has a field %q#D whose type has internal linkage",
3171 : : type, t);
3172 : : else // In C++98 this can only happen with unnamed namespaces.
3173 : 25 : warning (OPT_Wsubobject_linkage, "\
3174 : : %qT has a field %q#D whose type uses the anonymous namespace",
3175 : : type, t);
3176 : : }
3177 : : }
3178 : 511821 : else if (MAYBE_CLASS_TYPE_P (ftype)
3179 : 114725 : && vis < VISIBILITY_HIDDEN
3180 : 114725 : && subvis >= VISIBILITY_HIDDEN)
3181 : 8 : warning (OPT_Wattributes, "\
3182 : : %qT declared with greater visibility than the type of its field %qD",
3183 : : type, t);
3184 : : }
3185 : :
3186 : 411863 : binfo = TYPE_BINFO (type);
3187 : 483129 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
3188 : : {
3189 : 71266 : tree btype = BINFO_TYPE (t);
3190 : 71266 : int subvis = type_visibility (btype);
3191 : :
3192 : 71266 : if (subvis == VISIBILITY_ANON)
3193 : : {
3194 : 41 : if (!in_main_input_context())
3195 : : {
3196 : 23 : tree nlt = no_linkage_check (btype, /*relaxed_p=*/false);
3197 : 23 : if (nlt)
3198 : : {
3199 : 8 : if (same_type_p (btype, nlt))
3200 : 8 : warning (OPT_Wsubobject_linkage, "\
3201 : : %qT has a base %qT which has no linkage",
3202 : : type, btype);
3203 : : else
3204 : 0 : warning (OPT_Wsubobject_linkage, "\
3205 : : %qT has a base %qT which depends on the type %qT which has no linkage",
3206 : : type, btype, nlt);
3207 : : }
3208 : 15 : else if (cxx_dialect > cxx98
3209 : 15 : && !decl_anon_ns_mem_p (btype))
3210 : 3 : warning (OPT_Wsubobject_linkage, "\
3211 : : %qT has a base %qT which has internal linkage",
3212 : : type, btype);
3213 : : else // In C++98 this can only happen with unnamed namespaces.
3214 : 12 : warning (OPT_Wsubobject_linkage, "\
3215 : : %qT has a base %qT which uses the anonymous namespace",
3216 : : type, btype);
3217 : : }
3218 : : }
3219 : 71225 : else if (vis < VISIBILITY_HIDDEN
3220 : 71225 : && subvis >= VISIBILITY_HIDDEN)
3221 : 8 : warning (OPT_Wattributes, "\
3222 : : %qT declared with greater visibility than its base %qT",
3223 : 8 : type, TREE_TYPE (t));
3224 : : }
3225 : : }
3226 : :
3227 : : /* Functions for adjusting the visibility of a tagged type and its nested
3228 : : types and declarations when it gets a name for linkage purposes from a
3229 : : typedef. */
3230 : : // FIXME: It is now a DR for such a class type to contain anything
3231 : : // other than C. So at minium most of this can probably be deleted.
3232 : :
3233 : : /* First reset the visibility of all the types. */
3234 : :
3235 : : static void
3236 : 742819 : reset_type_linkage_1 (tree type)
3237 : : {
3238 : 742819 : set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
3239 : 742819 : if (CLASS_TYPE_P (type))
3240 : 3645154 : for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
3241 : 2903999 : if (DECL_IMPLICIT_TYPEDEF_P (member))
3242 : 237655 : reset_type_linkage_1 (TREE_TYPE (member));
3243 : 742819 : }
3244 : :
3245 : : /* Then reset the visibility of any static data members or member
3246 : : functions that use those types. */
3247 : :
3248 : : static void
3249 : 100 : reset_decl_linkage (tree decl)
3250 : : {
3251 : 100 : if (TREE_PUBLIC (decl))
3252 : : return;
3253 : 100 : if (DECL_CLONED_FUNCTION_P (decl))
3254 : : return;
3255 : 100 : TREE_PUBLIC (decl) = true;
3256 : 100 : DECL_INTERFACE_KNOWN (decl) = false;
3257 : 100 : determine_visibility (decl);
3258 : 100 : tentative_decl_linkage (decl);
3259 : : }
3260 : :
3261 : : void
3262 : 505164 : reset_type_linkage (tree type)
3263 : : {
3264 : 505164 : reset_type_linkage_1 (type);
3265 : 505164 : if (CLASS_TYPE_P (type))
3266 : : {
3267 : 503512 : if (tree vt = CLASSTYPE_VTABLES (type))
3268 : : {
3269 : 20 : tree name = mangle_vtbl_for_type (type);
3270 : 20 : DECL_NAME (vt) = name;
3271 : 20 : SET_DECL_ASSEMBLER_NAME (vt, name);
3272 : 20 : reset_decl_linkage (vt);
3273 : : }
3274 : 503512 : if (!ANON_AGGR_TYPE_P (type))
3275 : 503371 : if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
3276 : : {
3277 : 16 : tree name = mangle_typeinfo_for_type (type);
3278 : 16 : DECL_NAME (ti) = name;
3279 : 16 : SET_DECL_ASSEMBLER_NAME (ti, name);
3280 : 16 : TREE_TYPE (name) = type;
3281 : 16 : reset_decl_linkage (ti);
3282 : : }
3283 : 2392848 : for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
3284 : : {
3285 : 1889336 : tree mem = STRIP_TEMPLATE (m);
3286 : 1889336 : if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
3287 : 64 : reset_decl_linkage (mem);
3288 : 1889272 : else if (DECL_IMPLICIT_TYPEDEF_P (mem))
3289 : 124438 : reset_type_linkage (TREE_TYPE (mem));
3290 : : }
3291 : : }
3292 : 505164 : }
3293 : :
3294 : : /* Set up our initial idea of what the linkage of DECL should be. */
3295 : :
3296 : : void
3297 : 21779719 : tentative_decl_linkage (tree decl)
3298 : : {
3299 : 21779719 : if (DECL_INTERFACE_KNOWN (decl))
3300 : : /* We've already made a decision as to how this function will
3301 : : be handled. */;
3302 : 21779719 : else if (vague_linkage_p (decl))
3303 : : {
3304 : 21779677 : if (TREE_CODE (decl) == FUNCTION_DECL
3305 : 21779677 : && decl_defined_p (decl))
3306 : : {
3307 : 21779667 : DECL_EXTERNAL (decl) = 1;
3308 : 21779667 : DECL_NOT_REALLY_EXTERN (decl) = 1;
3309 : 21779667 : note_vague_linkage_fn (decl);
3310 : : /* A non-template inline function with external linkage will
3311 : : always be COMDAT. As we must eventually determine the
3312 : : linkage of all functions, and as that causes writes to
3313 : : the data mapped in from the PCH file, it's advantageous
3314 : : to mark the functions at this point. */
3315 : 21779667 : if (DECL_DECLARED_INLINE_P (decl)
3316 : 21779667 : && (!DECL_IMPLICIT_INSTANTIATION (decl)
3317 : 2414970 : || DECL_DEFAULTED_FN (decl)))
3318 : : {
3319 : : /* This function must have external linkage, as
3320 : : otherwise DECL_INTERFACE_KNOWN would have been
3321 : : set. */
3322 : 19542338 : gcc_assert (TREE_PUBLIC (decl));
3323 : 19542338 : comdat_linkage (decl);
3324 : 19542338 : DECL_INTERFACE_KNOWN (decl) = 1;
3325 : : }
3326 : : }
3327 : 10 : else if (VAR_P (decl))
3328 : 10 : maybe_commonize_var (decl);
3329 : : }
3330 : 21779719 : }
3331 : :
3332 : : /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
3333 : : for DECL has not already been determined, do so now by setting
3334 : : DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
3335 : : function is called entities with vague linkage whose definitions
3336 : : are available must have TREE_PUBLIC set.
3337 : :
3338 : : If this function decides to place DECL in COMDAT, it will set
3339 : : appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
3340 : : the caller to decide whether or not to clear DECL_EXTERNAL. Some
3341 : : callers defer that decision until it is clear that DECL is actually
3342 : : required. */
3343 : :
3344 : : void
3345 : 149082244 : import_export_decl (tree decl)
3346 : : {
3347 : 149082244 : bool comdat_p;
3348 : 149082244 : bool import_p;
3349 : 149082244 : tree class_type = NULL_TREE;
3350 : :
3351 : 149082244 : if (DECL_INTERFACE_KNOWN (decl))
3352 : : return;
3353 : :
3354 : : /* We cannot determine what linkage to give to an entity with vague
3355 : : linkage until the end of the file. For example, a virtual table
3356 : : for a class will be defined if and only if the key method is
3357 : : defined in this translation unit. */
3358 : 22188229 : gcc_assert (at_eof);
3359 : : /* Object file linkage for explicit instantiations is handled in
3360 : : mark_decl_instantiated. For static variables in functions with
3361 : : vague linkage, maybe_commonize_var is used.
3362 : :
3363 : : Therefore, the only declarations that should be provided to this
3364 : : function are those with external linkage that are:
3365 : :
3366 : : * implicit instantiations of function templates
3367 : :
3368 : : * inline functions
3369 : :
3370 : : * inline variables
3371 : :
3372 : : * implicit instantiations of static data members of class
3373 : : templates
3374 : :
3375 : : * virtual tables
3376 : :
3377 : : * typeinfo objects
3378 : :
3379 : : Furthermore, all entities that reach this point must have a
3380 : : definition available in this translation unit.
3381 : :
3382 : : The following assertions check these conditions. */
3383 : 22188229 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3384 : : /* Any code that creates entities with TREE_PUBLIC cleared should
3385 : : also set DECL_INTERFACE_KNOWN. */
3386 : 22188229 : gcc_assert (TREE_PUBLIC (decl));
3387 : 22188229 : if (TREE_CODE (decl) == FUNCTION_DECL)
3388 : 14944657 : gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3389 : : || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
3390 : : || DECL_DECLARED_INLINE_P (decl));
3391 : : else
3392 : 7243572 : gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
3393 : : || DECL_INLINE_VAR_P (decl)
3394 : : || DECL_VTABLE_OR_VTT_P (decl)
3395 : : || DECL_TINFO_P (decl));
3396 : : /* Check that a definition of DECL is available in this translation
3397 : : unit. */
3398 : 22188229 : gcc_assert (!DECL_REALLY_EXTERN (decl));
3399 : :
3400 : : /* Assume that DECL will not have COMDAT linkage. */
3401 : 22188229 : comdat_p = false;
3402 : : /* Assume that DECL will not be imported into this translation
3403 : : unit. */
3404 : 22188229 : import_p = false;
3405 : :
3406 : : /* FIXME: Since https://github.com/itanium-cxx-abi/cxx-abi/pull/171,
3407 : : the ABI specifies that classes attached to named modules should
3408 : : have their vtables uniquely emitted in the object for the module
3409 : : unit in which it is defined. And similarly for RTTI structures. */
3410 : 22188229 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
3411 : : {
3412 : 1501253 : class_type = DECL_CONTEXT (decl);
3413 : 1501253 : import_export_class (class_type);
3414 : 1501253 : if (CLASSTYPE_INTERFACE_KNOWN (class_type)
3415 : 1501253 : && CLASSTYPE_INTERFACE_ONLY (class_type))
3416 : : import_p = true;
3417 : 403923 : else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
3418 : 4 : && !CLASSTYPE_USE_TEMPLATE (class_type)
3419 : 4 : && CLASSTYPE_KEY_METHOD (class_type)
3420 : 403923 : && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
3421 : : /* The ABI requires that all virtual tables be emitted with
3422 : : COMDAT linkage. However, on systems where COMDAT symbols
3423 : : don't show up in the table of contents for a static
3424 : : archive, or on systems without weak symbols (where we
3425 : : approximate COMDAT linkage by using internal linkage), the
3426 : : linker will report errors about undefined symbols because
3427 : : it will not see the virtual table definition. Therefore,
3428 : : in the case that we know that the virtual table will be
3429 : : emitted in only one translation unit, we make the virtual
3430 : : table an ordinary definition with external linkage. */
3431 : 0 : DECL_EXTERNAL (decl) = 0;
3432 : 403923 : else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
3433 : : {
3434 : : /* CLASS_TYPE is being exported from this translation unit,
3435 : : so DECL should be defined here. */
3436 : 3236 : if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
3437 : : /* If a class is declared in a header with the "extern
3438 : : template" extension, then it will not be instantiated,
3439 : : even in translation units that would normally require
3440 : : it. Often such classes are explicitly instantiated in
3441 : : one translation unit. Therefore, the explicit
3442 : : instantiation must be made visible to other translation
3443 : : units. */
3444 : 0 : DECL_EXTERNAL (decl) = 0;
3445 : : else
3446 : : {
3447 : : /* The generic C++ ABI says that class data is always
3448 : : COMDAT, even if there is a key function. Some
3449 : : variants (e.g., the ARM EABI) says that class data
3450 : : only has COMDAT linkage if the class data might be
3451 : : emitted in more than one translation unit. When the
3452 : : key method can be inline and is inline, we still have
3453 : : to arrange for comdat even though
3454 : : class_data_always_comdat is false. */
3455 : 3236 : if (!CLASSTYPE_KEY_METHOD (class_type)
3456 : 2596 : || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
3457 : 5798 : || targetm.cxx.class_data_always_comdat ())
3458 : : {
3459 : : /* The ABI requires COMDAT linkage. Normally, we
3460 : : only emit COMDAT things when they are needed;
3461 : : make sure that we realize that this entity is
3462 : : indeed needed. */
3463 : 3236 : comdat_p = true;
3464 : 3236 : mark_needed (decl);
3465 : : }
3466 : : }
3467 : : }
3468 : 400687 : else if (!flag_implicit_templates
3469 : 400687 : && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
3470 : : import_p = true;
3471 : : else
3472 : : comdat_p = true;
3473 : : }
3474 : 20686976 : else if (VAR_P (decl) && DECL_TINFO_P (decl))
3475 : : {
3476 : 2053554 : tree type = TREE_TYPE (DECL_NAME (decl));
3477 : 2053554 : if (CLASS_TYPE_P (type))
3478 : : {
3479 : 2050373 : class_type = type;
3480 : 2050373 : import_export_class (type);
3481 : 2050373 : if (CLASSTYPE_INTERFACE_KNOWN (type)
3482 : 1241155 : && TYPE_POLYMORPHIC_P (type)
3483 : 1241025 : && CLASSTYPE_INTERFACE_ONLY (type)
3484 : : /* If -fno-rtti was specified, then we cannot be sure
3485 : : that RTTI information will be emitted with the
3486 : : virtual table of the class, so we must emit it
3487 : : wherever it is used. */
3488 : 3286968 : && flag_rtti)
3489 : : import_p = true;
3490 : : else
3491 : : {
3492 : 813884 : if (CLASSTYPE_INTERFACE_KNOWN (type)
3493 : 813884 : && !CLASSTYPE_INTERFACE_ONLY (type))
3494 : : {
3495 : 4486 : comdat_p = (targetm.cxx.class_data_always_comdat ()
3496 : 4486 : || (CLASSTYPE_KEY_METHOD (type)
3497 : 0 : && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
3498 : 4486 : mark_needed (decl);
3499 : 4486 : if (!flag_weak)
3500 : : {
3501 : 0 : comdat_p = false;
3502 : 0 : DECL_EXTERNAL (decl) = 0;
3503 : : }
3504 : : }
3505 : : else
3506 : : comdat_p = true;
3507 : : }
3508 : : }
3509 : : else
3510 : : comdat_p = true;
3511 : : }
3512 : 18633422 : else if (DECL_TEMPLOID_INSTANTIATION (decl))
3513 : : {
3514 : : /* DECL is an implicit instantiation of a function or static
3515 : : data member. */
3516 : 18403645 : if (flag_implicit_templates
3517 : 18403645 : || (flag_implicit_inline_templates
3518 : 18269 : && TREE_CODE (decl) == FUNCTION_DECL
3519 : 15455 : && DECL_DECLARED_INLINE_P (decl)))
3520 : : comdat_p = true;
3521 : : else
3522 : : /* If we are not implicitly generating templates, then mark
3523 : : this entity as undefined in this translation unit. */
3524 : : import_p = true;
3525 : : }
3526 : 229777 : else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
3527 : : {
3528 : 229343 : if (!DECL_DECLARED_INLINE_P (decl))
3529 : : {
3530 : 0 : tree ctype = DECL_CONTEXT (decl);
3531 : 0 : import_export_class (ctype);
3532 : 0 : if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3533 : : {
3534 : 0 : DECL_NOT_REALLY_EXTERN (decl)
3535 : 0 : = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3536 : 0 : || (DECL_DECLARED_INLINE_P (decl)
3537 : 0 : && ! flag_implement_inlines
3538 : 0 : && !DECL_VINDEX (decl)));
3539 : :
3540 : 0 : if (!DECL_NOT_REALLY_EXTERN (decl))
3541 : 0 : DECL_EXTERNAL (decl) = 1;
3542 : :
3543 : : /* Always make artificials weak. */
3544 : 0 : if (DECL_ARTIFICIAL (decl) && flag_weak)
3545 : : comdat_p = true;
3546 : : else
3547 : 0 : maybe_make_one_only (decl);
3548 : : }
3549 : : }
3550 : : else
3551 : : comdat_p = true;
3552 : : }
3553 : : else
3554 : : comdat_p = true;
3555 : :
3556 : 3236 : if (import_p)
3557 : : {
3558 : : /* If we are importing DECL into this translation unit, mark is
3559 : : an undefined here. */
3560 : 2340017 : DECL_EXTERNAL (decl) = 1;
3561 : 2340017 : DECL_NOT_REALLY_EXTERN (decl) = 0;
3562 : : }
3563 : 19848212 : else if (comdat_p)
3564 : : {
3565 : : /* If we decided to put DECL in COMDAT, mark it accordingly at
3566 : : this point. */
3567 : 19848212 : comdat_linkage (decl);
3568 : : }
3569 : :
3570 : 22188229 : DECL_INTERFACE_KNOWN (decl) = 1;
3571 : : }
3572 : :
3573 : : /* Return an expression that performs the destruction of DECL, which
3574 : : must be a VAR_DECL whose type has a non-trivial destructor, or is
3575 : : an array whose (innermost) elements have a non-trivial destructor. */
3576 : :
3577 : : tree
3578 : 1373 : build_cleanup (tree decl)
3579 : : {
3580 : 1373 : tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3581 : 1373 : gcc_assert (clean != NULL_TREE);
3582 : 1373 : return clean;
3583 : : }
3584 : :
3585 : : /* GUARD is a helper variable for DECL; make them have the same linkage and
3586 : : visibility. */
3587 : :
3588 : : void
3589 : 33363 : copy_linkage (tree guard, tree decl)
3590 : : {
3591 : 33363 : TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3592 : 33363 : TREE_STATIC (guard) = TREE_STATIC (decl);
3593 : 33363 : DECL_COMMON (guard) = DECL_COMMON (decl);
3594 : 33363 : DECL_COMDAT (guard) = DECL_COMDAT (decl);
3595 : 33363 : if (TREE_STATIC (guard))
3596 : : {
3597 : 4902 : CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3598 : 4902 : set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3599 : 4902 : if (DECL_ONE_ONLY (decl))
3600 : 3621 : make_decl_one_only (guard, cxx_comdat_group (guard));
3601 : 4902 : if (TREE_PUBLIC (decl))
3602 : 4005 : DECL_WEAK (guard) = DECL_WEAK (decl);
3603 : : /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not
3604 : : be set until import_export_decl at EOF. */
3605 : 4902 : if (vague_linkage_p (decl))
3606 : 3639 : comdat_linkage (guard);
3607 : 4902 : DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3608 : 4902 : DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3609 : : }
3610 : 33363 : }
3611 : :
3612 : : /* Returns the initialization guard variable for the variable DECL,
3613 : : which has static storage duration. */
3614 : :
3615 : : tree
3616 : 4307 : get_guard (tree decl)
3617 : : {
3618 : 4307 : tree sname = mangle_guard_variable (decl);
3619 : 4307 : tree guard = get_global_binding (sname);
3620 : 4307 : if (! guard)
3621 : : {
3622 : 4307 : tree guard_type;
3623 : :
3624 : : /* We use a type that is big enough to contain a mutex as well
3625 : : as an integer counter. */
3626 : 4307 : guard_type = targetm.cxx.guard_type ();
3627 : 4307 : guard = build_decl (DECL_SOURCE_LOCATION (decl),
3628 : : VAR_DECL, sname, guard_type);
3629 : :
3630 : : /* The guard should have the same linkage as what it guards. */
3631 : 4307 : copy_linkage (guard, decl);
3632 : :
3633 : 4307 : DECL_ARTIFICIAL (guard) = 1;
3634 : 4307 : DECL_IGNORED_P (guard) = 1;
3635 : 4307 : TREE_USED (guard) = 1;
3636 : 4307 : pushdecl_top_level_and_finish (guard, NULL_TREE);
3637 : : }
3638 : 4307 : return guard;
3639 : : }
3640 : :
3641 : : /* Returns true if accessing the GUARD atomic is expensive,
3642 : : i.e. involves a call to __sync_synchronize or similar.
3643 : : In this case let __cxa_guard_acquire handle the atomics. */
3644 : :
3645 : : static bool
3646 : 3520 : is_atomic_expensive_p (machine_mode mode)
3647 : : {
3648 : 3520 : if (!flag_inline_atomics)
3649 : : return true;
3650 : :
3651 : 3520 : if (!can_compare_and_swap_p (mode, false) || !can_atomic_load_p (mode))
3652 : 0 : return true;
3653 : :
3654 : : return false;
3655 : : }
3656 : :
3657 : : /* Return an atomic load of src with the appropriate memory model. */
3658 : :
3659 : : static tree
3660 : 3520 : build_atomic_load_type (tree src, HOST_WIDE_INT model, tree type)
3661 : : {
3662 : 3520 : tree ptr_type = build_pointer_type (type);
3663 : 3520 : tree mem_model = build_int_cst (integer_type_node, model);
3664 : 3520 : tree t, addr, val;
3665 : 3520 : unsigned int size;
3666 : 3520 : int fncode;
3667 : :
3668 : 3520 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
3669 : :
3670 : 3520 : fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3671 : 3520 : t = builtin_decl_implicit ((enum built_in_function) fncode);
3672 : :
3673 : 3520 : addr = build1 (ADDR_EXPR, ptr_type, src);
3674 : 3520 : val = build_call_expr (t, 2, addr, mem_model);
3675 : 3520 : return val;
3676 : : }
3677 : :
3678 : : /* Return those bits of the GUARD variable that should be set when the
3679 : : guarded entity is actually initialized. */
3680 : :
3681 : : static tree
3682 : 1574 : get_guard_bits (tree guard)
3683 : : {
3684 : 1574 : if (!targetm.cxx.guard_mask_bit ())
3685 : : {
3686 : : /* We only set the first byte of the guard, in order to leave room
3687 : : for a mutex in the high-order bits. */
3688 : 1574 : guard = build1 (ADDR_EXPR,
3689 : 1574 : build_pointer_type (TREE_TYPE (guard)),
3690 : : guard);
3691 : 1574 : guard = build1 (NOP_EXPR,
3692 : : build_pointer_type (char_type_node),
3693 : : guard);
3694 : 1574 : guard = build1 (INDIRECT_REF, char_type_node, guard);
3695 : : }
3696 : :
3697 : 1574 : return guard;
3698 : : }
3699 : :
3700 : : /* Return an expression which determines whether or not the GUARD
3701 : : variable has already been initialized. */
3702 : :
3703 : : tree
3704 : 4307 : get_guard_cond (tree guard, bool thread_safe)
3705 : : {
3706 : 4307 : tree guard_value;
3707 : :
3708 : 4307 : if (!thread_safe)
3709 : 787 : guard = get_guard_bits (guard);
3710 : : else
3711 : : {
3712 : 3520 : tree type = targetm.cxx.guard_mask_bit ()
3713 : 3520 : ? TREE_TYPE (guard) : char_type_node;
3714 : :
3715 : 3520 : if (is_atomic_expensive_p (TYPE_MODE (type)))
3716 : 0 : guard = integer_zero_node;
3717 : : else
3718 : 3520 : guard = build_atomic_load_type (guard, MEMMODEL_ACQUIRE, type);
3719 : : }
3720 : :
3721 : : /* Mask off all but the low bit. */
3722 : 4307 : if (targetm.cxx.guard_mask_bit ())
3723 : : {
3724 : 0 : guard_value = integer_one_node;
3725 : 0 : if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3726 : 0 : guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3727 : 0 : guard = cp_build_binary_op (input_location,
3728 : : BIT_AND_EXPR, guard, guard_value,
3729 : : tf_warning_or_error);
3730 : : }
3731 : :
3732 : 4307 : guard_value = integer_zero_node;
3733 : 4307 : if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3734 : 4307 : guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3735 : 4307 : return cp_build_binary_op (input_location,
3736 : : EQ_EXPR, guard, guard_value,
3737 : 4307 : tf_warning_or_error);
3738 : : }
3739 : :
3740 : : /* Return an expression which sets the GUARD variable, indicating that
3741 : : the variable being guarded has been initialized. */
3742 : :
3743 : : tree
3744 : 787 : set_guard (tree guard)
3745 : : {
3746 : 787 : tree guard_init;
3747 : :
3748 : : /* Set the GUARD to one. */
3749 : 787 : guard = get_guard_bits (guard);
3750 : 787 : guard_init = integer_one_node;
3751 : 787 : if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3752 : 787 : guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3753 : 787 : return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3754 : 787 : tf_warning_or_error);
3755 : : }
3756 : :
3757 : : /* Returns true iff we can tell that VAR does not have a dynamic
3758 : : initializer. */
3759 : :
3760 : : static bool
3761 : 1899 : var_defined_without_dynamic_init (tree var)
3762 : : {
3763 : : /* constinit vars are guaranteed to not have dynamic initializer,
3764 : : but still registering the destructor counts as dynamic initialization. */
3765 : 1899 : if (DECL_DECLARED_CONSTINIT_P (var)
3766 : 8 : && COMPLETE_TYPE_P (TREE_TYPE (var))
3767 : 1905 : && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3768 : : return true;
3769 : : /* If it's defined in another TU, we can't tell. */
3770 : 1895 : if (DECL_EXTERNAL (var))
3771 : : return false;
3772 : : /* If it has a non-trivial destructor, registering the destructor
3773 : : counts as dynamic initialization. */
3774 : 1678 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3775 : : return false;
3776 : : /* If it's in this TU, its initializer has been processed, unless
3777 : : it's a case of self-initialization, then DECL_INITIALIZED_P is
3778 : : false while the initializer is handled by finish_id_expression. */
3779 : 1496 : if (!DECL_INITIALIZED_P (var))
3780 : : return false;
3781 : : /* If it has no initializer or a constant one, it's not dynamic. */
3782 : 1490 : return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3783 : 1490 : || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3784 : : }
3785 : :
3786 : : /* Returns true iff VAR is a variable that needs uses to be
3787 : : wrapped for possible dynamic initialization. */
3788 : :
3789 : : bool
3790 : 76637 : var_needs_tls_wrapper (tree var)
3791 : : {
3792 : 76637 : return (!error_operand_p (var)
3793 : 76634 : && CP_DECL_THREAD_LOCAL_P (var)
3794 : 76634 : && !DECL_GNU_TLS_P (var)
3795 : 1956 : && !DECL_FUNCTION_SCOPE_P (var)
3796 : 78536 : && !var_defined_without_dynamic_init (var));
3797 : : }
3798 : :
3799 : : /* Get the FUNCTION_DECL for the shared TLS init function for this
3800 : : translation unit. */
3801 : :
3802 : : static tree
3803 : 115 : get_local_tls_init_fn (location_t loc)
3804 : : {
3805 : 115 : tree sname = get_identifier ("__tls_init");
3806 : 115 : tree fn = get_global_binding (sname);
3807 : 115 : if (!fn)
3808 : : {
3809 : 112 : fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname,
3810 : : build_function_type (void_type_node,
3811 : : void_list_node));
3812 : 112 : SET_DECL_LANGUAGE (fn, lang_c);
3813 : 112 : TREE_PUBLIC (fn) = false;
3814 : 112 : DECL_ARTIFICIAL (fn) = true;
3815 : 112 : mark_used (fn);
3816 : 112 : set_global_binding (fn);
3817 : : }
3818 : 115 : return fn;
3819 : : }
3820 : :
3821 : : /* Get a FUNCTION_DECL for the init function for the thread_local
3822 : : variable VAR. The init function will be an alias to the function
3823 : : that initializes all the non-local TLS variables in the translation
3824 : : unit. The init function is only used by the wrapper function. */
3825 : :
3826 : : static tree
3827 : 1022 : get_tls_init_fn (tree var)
3828 : : {
3829 : : /* Only C++11 TLS vars need this init fn. */
3830 : 1022 : if (!var_needs_tls_wrapper (var))
3831 : : return NULL_TREE;
3832 : :
3833 : : /* If -fno-extern-tls-init, assume that we don't need to call
3834 : : a tls init function for a variable defined in another TU. */
3835 : 1012 : if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3836 : : return NULL_TREE;
3837 : :
3838 : : /* If the variable is internal, or if we can't generate aliases,
3839 : : call the local init function directly. */
3840 : 1011 : if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3841 : 3 : return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var));
3842 : :
3843 : 1008 : tree sname = mangle_tls_init_fn (var);
3844 : 1008 : tree fn = get_global_binding (sname);
3845 : 1008 : if (!fn)
3846 : : {
3847 : 650 : fn = build_lang_decl (FUNCTION_DECL, sname,
3848 : : build_function_type (void_type_node,
3849 : : void_list_node));
3850 : 650 : SET_DECL_LANGUAGE (fn, lang_c);
3851 : 650 : TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3852 : 650 : DECL_ARTIFICIAL (fn) = true;
3853 : 650 : DECL_COMDAT (fn) = DECL_COMDAT (var);
3854 : 650 : DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3855 : 650 : if (DECL_ONE_ONLY (var))
3856 : 3 : make_decl_one_only (fn, cxx_comdat_group (fn));
3857 : 650 : if (TREE_PUBLIC (var))
3858 : : {
3859 : 650 : tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3860 : : /* If the variable is defined somewhere else and might have static
3861 : : initialization, make the init function a weak reference. */
3862 : 650 : if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3863 : 577 : || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3864 : 562 : || TYPE_HAS_TRIVIAL_DFLT (obtype))
3865 : 88 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3866 : 728 : && DECL_EXTERNAL (var))
3867 : 41 : declare_weak (fn);
3868 : : else
3869 : 609 : DECL_WEAK (fn) = DECL_WEAK (var);
3870 : : }
3871 : 650 : DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3872 : 650 : DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3873 : 650 : DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3874 : 650 : DECL_IGNORED_P (fn) = 1;
3875 : 650 : mark_used (fn);
3876 : :
3877 : 650 : DECL_BEFRIENDING_CLASSES (fn) = var;
3878 : :
3879 : 650 : set_global_binding (fn);
3880 : : }
3881 : : return fn;
3882 : : }
3883 : :
3884 : : /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3885 : : variable VAR. The wrapper function calls the init function (if any) for
3886 : : VAR and then returns a reference to VAR. The wrapper function is used
3887 : : in place of VAR everywhere VAR is mentioned. */
3888 : :
3889 : : static tree
3890 : 75603 : get_tls_wrapper_fn (tree var)
3891 : : {
3892 : : /* Only C++11 TLS vars need this wrapper fn. */
3893 : 75603 : if (!var_needs_tls_wrapper (var))
3894 : : return NULL_TREE;
3895 : :
3896 : 617 : tree sname = mangle_tls_wrapper_fn (var);
3897 : 617 : tree fn = get_global_binding (sname);
3898 : 617 : if (!fn)
3899 : : {
3900 : : /* A named rvalue reference is an lvalue, so the wrapper should
3901 : : always return an lvalue reference. */
3902 : 429 : tree type = non_reference (TREE_TYPE (var));
3903 : 429 : type = build_reference_type (type);
3904 : 429 : tree fntype = build_function_type (type, void_list_node);
3905 : :
3906 : 429 : fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var),
3907 : : FUNCTION_DECL, sname, fntype);
3908 : 429 : SET_DECL_LANGUAGE (fn, lang_c);
3909 : 429 : TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3910 : 429 : DECL_ARTIFICIAL (fn) = true;
3911 : 429 : DECL_IGNORED_P (fn) = 1;
3912 : 429 : DECL_CONTEXT (fn) = DECL_CONTEXT (var);
3913 : : /* The wrapper is inline and emitted everywhere var is used. */
3914 : 429 : DECL_DECLARED_INLINE_P (fn) = true;
3915 : 429 : if (TREE_PUBLIC (var))
3916 : : {
3917 : 426 : comdat_linkage (fn);
3918 : : #ifdef HAVE_GAS_HIDDEN
3919 : : /* Make the wrapper bind locally; there's no reason to share
3920 : : the wrapper between multiple shared objects. */
3921 : 426 : DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3922 : 426 : DECL_VISIBILITY_SPECIFIED (fn) = true;
3923 : : #endif
3924 : : }
3925 : 429 : if (!TREE_PUBLIC (fn))
3926 : 3 : DECL_INTERFACE_KNOWN (fn) = true;
3927 : 429 : mark_used (fn);
3928 : 429 : note_vague_linkage_fn (fn);
3929 : :
3930 : : #if 0
3931 : : /* We want CSE to commonize calls to the wrapper, but marking it as
3932 : : pure is unsafe since it has side-effects. I guess we need a new
3933 : : ECF flag even weaker than ECF_PURE. FIXME! */
3934 : : DECL_PURE_P (fn) = true;
3935 : : #endif
3936 : :
3937 : 429 : DECL_BEFRIENDING_CLASSES (fn) = var;
3938 : :
3939 : 429 : set_global_binding (fn);
3940 : : }
3941 : : return fn;
3942 : : }
3943 : :
3944 : : /* If EXPR is a thread_local variable that should be wrapped by init
3945 : : wrapper function, return a call to that function, otherwise return
3946 : : NULL. */
3947 : :
3948 : : tree
3949 : 723145513 : maybe_get_tls_wrapper_call (tree expr)
3950 : : {
3951 : 723145513 : if (VAR_P (expr)
3952 : 253092813 : && !processing_template_decl
3953 : 108939294 : && !cp_unevaluated_operand
3954 : 831232988 : && CP_DECL_THREAD_LOCAL_P (expr))
3955 : 75603 : if (tree wrap = get_tls_wrapper_fn (expr))
3956 : 617 : return build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3957 : : return NULL;
3958 : : }
3959 : :
3960 : : /* At EOF, generate the definition for the TLS wrapper function FN:
3961 : :
3962 : : T& var_wrapper() {
3963 : : if (init_fn) init_fn();
3964 : : return var;
3965 : : } */
3966 : :
3967 : : static void
3968 : 432 : generate_tls_wrapper (tree fn)
3969 : : {
3970 : 432 : tree var = DECL_BEFRIENDING_CLASSES (fn);
3971 : :
3972 : 432 : start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3973 : 432 : tree body = begin_function_body ();
3974 : : /* Only call the init fn if there might be one. */
3975 : 432 : if (tree init_fn = get_tls_init_fn (var))
3976 : : {
3977 : 421 : tree if_stmt = NULL_TREE;
3978 : : /* If init_fn is a weakref, make sure it exists before calling. */
3979 : 421 : if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3980 : : {
3981 : 41 : if_stmt = begin_if_stmt ();
3982 : 41 : tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3983 : 41 : tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3984 : : NE_EXPR, addr, nullptr_node,
3985 : : tf_warning_or_error);
3986 : 41 : finish_if_stmt_cond (cond, if_stmt);
3987 : : }
3988 : 421 : finish_expr_stmt (build_cxx_call
3989 : : (init_fn, 0, NULL, tf_warning_or_error));
3990 : 421 : if (if_stmt)
3991 : : {
3992 : 41 : finish_then_clause (if_stmt);
3993 : 41 : finish_if_stmt (if_stmt);
3994 : : }
3995 : : }
3996 : : else
3997 : : /* If there's no initialization, the wrapper is a constant function. */
3998 : 11 : TREE_READONLY (fn) = true;
3999 : 432 : finish_return_stmt (convert_from_reference (var));
4000 : 432 : finish_function_body (body);
4001 : 432 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
4002 : 432 : }
4003 : :
4004 : : /* Start a global constructor or destructor function. */
4005 : :
4006 : : static tree
4007 : 6471 : start_objects (bool initp, unsigned priority, bool has_body)
4008 : : {
4009 : 6471 : bool default_init = initp && priority == DEFAULT_INIT_PRIORITY;
4010 : 6471 : bool is_module_init = default_init && module_global_init_needed ();
4011 : 6471 : tree name = NULL_TREE;
4012 : :
4013 : 6471 : if (is_module_init)
4014 : 1199 : name = mangle_module_global_init (0);
4015 : : else
4016 : : {
4017 : 5272 : char type[14];
4018 : :
4019 : : /* We use `I' to indicate initialization and `D' to indicate
4020 : : destruction. */
4021 : 5272 : unsigned len = sprintf (type, "sub_%c", initp ? 'I' : 'D');
4022 : 5272 : if (priority != DEFAULT_INIT_PRIORITY)
4023 : : {
4024 : 35 : char joiner = '_';
4025 : : #ifdef JOINER
4026 : 35 : joiner = JOINER;
4027 : : #endif
4028 : 35 : type[len++] = joiner;
4029 : 35 : sprintf (type + len, "%.5u", priority);
4030 : : }
4031 : 5272 : name = get_file_function_name (type);
4032 : : }
4033 : :
4034 : 6471 : tree fntype = build_function_type (void_type_node, void_list_node);
4035 : 6471 : tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
4036 : 6471 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
4037 : 6471 : if (is_module_init)
4038 : : {
4039 : 1199 : SET_DECL_ASSEMBLER_NAME (fndecl, name);
4040 : 1199 : TREE_PUBLIC (fndecl) = true;
4041 : 1199 : determine_visibility (fndecl);
4042 : : }
4043 : : else
4044 : 5272 : TREE_PUBLIC (fndecl) = 0;
4045 : 6471 : start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4046 : :
4047 : : /* Mark as artificial because it's not explicitly in the user's
4048 : : source code. */
4049 : 6471 : DECL_ARTIFICIAL (current_function_decl) = 1;
4050 : :
4051 : : /* Mark this declaration as used to avoid spurious warnings. */
4052 : 6471 : TREE_USED (current_function_decl) = 1;
4053 : :
4054 : : /* Mark this function as a global constructor or destructor. */
4055 : 6471 : if (initp)
4056 : 6459 : DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
4057 : : else
4058 : 12 : DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
4059 : :
4060 : 6471 : tree body = begin_compound_stmt (BCS_FN_BODY);
4061 : :
4062 : 6471 : if (is_module_init && has_body)
4063 : : {
4064 : : // If the function is going to be empty, don't emit idempotency.
4065 : : // 'static bool __in_chrg = false;
4066 : : // if (__inchrg) return;
4067 : : // __inchrg = true
4068 : 30 : tree var = build_lang_decl (VAR_DECL, in_charge_identifier,
4069 : : boolean_type_node);
4070 : 30 : DECL_CONTEXT (var) = fndecl;
4071 : 30 : DECL_ARTIFICIAL (var) = true;
4072 : 30 : TREE_STATIC (var) = true;
4073 : 30 : pushdecl (var);
4074 : 30 : cp_finish_decl (var, NULL_TREE, false, NULL_TREE, 0);
4075 : :
4076 : 30 : tree if_stmt = begin_if_stmt ();
4077 : 30 : finish_if_stmt_cond (var, if_stmt);
4078 : 30 : finish_return_stmt (NULL_TREE);
4079 : 30 : finish_then_clause (if_stmt);
4080 : 30 : finish_if_stmt (if_stmt);
4081 : :
4082 : 30 : tree assign = build2 (MODIFY_EXPR, boolean_type_node,
4083 : : var, boolean_true_node);
4084 : 30 : TREE_SIDE_EFFECTS (assign) = true;
4085 : 30 : finish_expr_stmt (assign);
4086 : : }
4087 : :
4088 : 6471 : return body;
4089 : : }
4090 : :
4091 : : /* Finish a global constructor or destructor. Add it to the global
4092 : : ctors or dtors, if STARTP is true. */
4093 : :
4094 : : static tree
4095 : 6467 : finish_objects (bool initp, unsigned priority, tree body, bool startp)
4096 : : {
4097 : : /* Finish up. */
4098 : 6467 : finish_compound_stmt (body);
4099 : 6467 : tree fn = finish_function (/*inline_p=*/false);
4100 : :
4101 : 6467 : if (!startp)
4102 : : ; // Neither ctor nor dtor I be.
4103 : 5298 : else if (initp)
4104 : : {
4105 : 5286 : DECL_STATIC_CONSTRUCTOR (fn) = 1;
4106 : 5286 : decl_init_priority_insert (fn, priority);
4107 : : }
4108 : : else
4109 : : {
4110 : 12 : DECL_STATIC_DESTRUCTOR (fn) = 1;
4111 : 12 : decl_fini_priority_insert (fn, priority);
4112 : : }
4113 : :
4114 : 6467 : return fn;
4115 : : }
4116 : :
4117 : : /* The name of the function we create to handle initializations and
4118 : : destructions for objects with static storage duration. */
4119 : : #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
4120 : :
4121 : : /* Begins the generation of the function that will handle all
4122 : : initialization or destruction of objects with static storage
4123 : : duration at PRIORITY.
4124 : :
4125 : : It is assumed that this function will only be called once. */
4126 : :
4127 : : static tree
4128 : 5303 : start_partial_init_fini_fn (bool initp, unsigned priority, unsigned count)
4129 : : {
4130 : 5303 : char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
4131 : :
4132 : : /* Create the identifier for this function. It will be of the form
4133 : : SSDF_IDENTIFIER_<number>. */
4134 : 5303 : sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
4135 : :
4136 : 5303 : tree type = build_function_type (void_type_node, void_list_node);
4137 : :
4138 : : /* Create the FUNCTION_DECL itself. */
4139 : 5303 : tree fn = build_lang_decl (FUNCTION_DECL, get_identifier (id), type);
4140 : 5303 : TREE_PUBLIC (fn) = 0;
4141 : 5303 : DECL_ARTIFICIAL (fn) = 1;
4142 : :
4143 : : /* Put this function in the list of functions to be called from the
4144 : : static constructors and destructors. */
4145 : 5303 : if (!static_init_fini_fns[initp])
4146 : 5279 : static_init_fini_fns[initp] = priority_map_t::create_ggc ();
4147 : 5303 : auto &slot = static_init_fini_fns[initp]->get_or_insert (priority);
4148 : 5303 : slot = tree_cons (fn, NULL_TREE, slot);
4149 : :
4150 : : /* Put the function in the global scope. */
4151 : 5303 : pushdecl (fn);
4152 : :
4153 : : /* Start the function itself. This is equivalent to declaring the
4154 : : function as:
4155 : :
4156 : : static void __ssdf (int __initialize_p, init __priority_p);
4157 : :
4158 : : It is static because we only need to call this function from the
4159 : : various constructor and destructor functions for this module. */
4160 : 5303 : start_preparsed_function (fn, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
4161 : :
4162 : : /* Set up the scope of the outermost block in the function. */
4163 : 5303 : return begin_compound_stmt (BCS_FN_BODY);
4164 : : }
4165 : :
4166 : : /* Finish the generation of the function which performs initialization
4167 : : or destruction of objects with static storage duration. */
4168 : :
4169 : : static void
4170 : 5303 : finish_partial_init_fini_fn (tree body)
4171 : : {
4172 : : /* Close out the function. */
4173 : 5303 : finish_compound_stmt (body);
4174 : 5303 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
4175 : 5303 : }
4176 : :
4177 : : /* The effective initialization priority of a DECL. */
4178 : :
4179 : : #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
4180 : : ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
4181 : : ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
4182 : :
4183 : : /* Whether a DECL needs a guard to protect it against multiple
4184 : : initialization. */
4185 : :
4186 : : #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
4187 : : || DECL_ONE_ONLY (decl) \
4188 : : || DECL_WEAK (decl)))
4189 : :
4190 : : /* Walks the initializer list of a global variable and looks for
4191 : : temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
4192 : : and that have their DECL_CONTEXT() == NULL. For each such
4193 : : temporary variable, set their DECL_CONTEXT() to CTX -- the
4194 : : initializing function. This is necessary because otherwise some
4195 : : optimizers (enabled by -O2 -fprofile-arcs) might crash when trying
4196 : : to refer to a temporary variable that does not have its
4197 : : DECL_CONTEXT() properly set. */
4198 : :
4199 : : static tree
4200 : 239121 : fix_temporary_vars_context_r (tree *node,
4201 : : int * /*unused*/,
4202 : : void *ctx)
4203 : : {
4204 : 239121 : if (TREE_CODE (*node) == BIND_EXPR)
4205 : 838 : for (tree var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
4206 : 419 : if (VAR_P (var) && !DECL_NAME (var)
4207 : 838 : && DECL_ARTIFICIAL (var) && !DECL_CONTEXT (var))
4208 : 0 : DECL_CONTEXT (var) = tree (ctx);
4209 : :
4210 : 239121 : return NULL_TREE;
4211 : : }
4212 : :
4213 : : /* Set up to handle the initialization or destruction of DECL. If
4214 : : INITP is nonzero, we are initializing the variable. Otherwise, we
4215 : : are destroying it. */
4216 : :
4217 : : static void
4218 : 9992 : one_static_initialization_or_destruction (bool initp, tree decl, tree init)
4219 : : {
4220 : : /* If we are supposed to destruct and there's a trivial destructor,
4221 : : nothing has to be done. */
4222 : 9992 : gcc_checking_assert (init || !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)));
4223 : :
4224 : : /* Trick the compiler into thinking we are at the file and line
4225 : : where DECL was declared so that error-messages make sense, and so
4226 : : that the debugger will show somewhat sensible file and line
4227 : : information. */
4228 : 9992 : input_location = DECL_SOURCE_LOCATION (decl);
4229 : :
4230 : : /* Make sure temporary variables in the initialiser all have
4231 : : their DECL_CONTEXT() set to a value different from NULL_TREE.
4232 : : This can happen when global variables initializers are built.
4233 : : In that case, the DECL_CONTEXT() of the global variables _AND_ of all
4234 : : the temporary variables that might have been generated in the
4235 : : accompanying initializers is NULL_TREE, meaning the variables have been
4236 : : declared in the global namespace.
4237 : : What we want to do here is to fix that and make sure the DECL_CONTEXT()
4238 : : of the temporaries are set to the current function decl. */
4239 : 9992 : cp_walk_tree_without_duplicates (&init,
4240 : : fix_temporary_vars_context_r,
4241 : : current_function_decl);
4242 : :
4243 : : /* Because of:
4244 : :
4245 : : [class.access.spec]
4246 : :
4247 : : Access control for implicit calls to the constructors,
4248 : : the conversion functions, or the destructor called to
4249 : : create and destroy a static data member is performed as
4250 : : if these calls appeared in the scope of the member's
4251 : : class.
4252 : :
4253 : : we pretend we are in a static member function of the class of
4254 : : which the DECL is a member. */
4255 : 9992 : if (member_p (decl))
4256 : : {
4257 : 1642 : DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
4258 : 1642 : DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
4259 : : }
4260 : :
4261 : : /* Assume we don't need a guard. */
4262 : 9992 : tree guard_if_stmt = NULL_TREE;
4263 : :
4264 : : /* We need a guard if this is an object with external linkage that
4265 : : might be initialized in more than one place. (For example, a
4266 : : static data member of a template, when the data member requires
4267 : : construction.) */
4268 : 9992 : if (NEEDS_GUARD_P (decl))
4269 : : {
4270 : 733 : tree guard = get_guard (decl);
4271 : 733 : tree guard_cond;
4272 : :
4273 : 733 : if (flag_use_cxa_atexit)
4274 : : {
4275 : : /* When using __cxa_atexit, we just check the GUARD as we
4276 : : would for a local static. We never try to destroy
4277 : : anything from a static destructor. */
4278 : 733 : gcc_assert (initp);
4279 : 733 : guard_cond = get_guard_cond (guard, false);
4280 : : }
4281 : : else
4282 : : {
4283 : : /* If we don't have __cxa_atexit, then we will be running
4284 : : destructors from .fini sections, or their equivalents.
4285 : : So, we need to know how many times we've tried to
4286 : : initialize this object. We do initializations only if
4287 : : the GUARD was or becomes zero (initp vs !initp
4288 : : respectively). */
4289 : 0 : guard_cond = cp_build_unary_op (initp ? POSTINCREMENT_EXPR
4290 : : : PREDECREMENT_EXPR,
4291 : : guard,
4292 : : /*noconvert=*/true,
4293 : : tf_warning_or_error);
4294 : 0 : guard_cond = cp_build_binary_op (input_location, EQ_EXPR, guard_cond,
4295 : : integer_zero_node,
4296 : : tf_warning_or_error);
4297 : : }
4298 : :
4299 : 733 : guard_if_stmt = begin_if_stmt ();
4300 : 733 : finish_if_stmt_cond (guard_cond, guard_if_stmt);
4301 : :
4302 : 733 : if (flag_use_cxa_atexit)
4303 : : /* Set the GUARD now. */
4304 : 733 : finish_expr_stmt (set_guard (guard));
4305 : : }
4306 : :
4307 : : /* Perform the initialization or destruction. */
4308 : 9992 : if (initp)
4309 : : {
4310 : 9972 : if (init)
4311 : : {
4312 : 9662 : finish_expr_stmt (init);
4313 : 9662 : if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
4314 : 28 : if (varpool_node *vnode = varpool_node::get (decl))
4315 : 28 : vnode->dynamically_initialized = 1;
4316 : : }
4317 : :
4318 : : /* If we're using __cxa_atexit, register a function that calls the
4319 : : destructor for the object. */
4320 : 9972 : if (flag_use_cxa_atexit)
4321 : 9960 : finish_expr_stmt (register_dtor_fn (decl));
4322 : : }
4323 : : else
4324 : 20 : finish_expr_stmt (build_cleanup (decl));
4325 : :
4326 : : /* Finish the guard if-stmt, if necessary. */
4327 : 9992 : if (guard_if_stmt)
4328 : : {
4329 : 733 : finish_then_clause (guard_if_stmt);
4330 : 733 : finish_if_stmt (guard_if_stmt);
4331 : : }
4332 : :
4333 : : /* Now that we're done with DECL we don't need to pretend to be a
4334 : : member of its class any longer. */
4335 : 9992 : DECL_CONTEXT (current_function_decl) = NULL_TREE;
4336 : 9992 : DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
4337 : 9992 : }
4338 : :
4339 : : /* Generate code to do the initialization or destruction of the decls in VARS,
4340 : : a TREE_LIST of VAR_DECL with static storage duration.
4341 : : Whether initialization or destruction is performed is specified by INITP. */
4342 : :
4343 : : static void
4344 : 5275 : emit_partial_init_fini_fn (bool initp, unsigned priority, tree vars,
4345 : : unsigned counter, location_t locus)
4346 : : {
4347 : 5275 : input_location = locus;
4348 : 5275 : tree body = start_partial_init_fini_fn (initp, priority, counter);
4349 : :
4350 : 14674 : for (tree node = vars; node; node = TREE_CHAIN (node))
4351 : : /* Do one initialization or destruction. */
4352 : 9399 : one_static_initialization_or_destruction (initp, TREE_VALUE (node),
4353 : 9399 : TREE_PURPOSE (node));
4354 : :
4355 : : /* Finish up the static storage duration function for this
4356 : : round. */
4357 : 5275 : input_location = locus;
4358 : 5275 : finish_partial_init_fini_fn (body);
4359 : 5275 : }
4360 : :
4361 : : /* VARS is a list of variables with static storage duration which may
4362 : : need initialization and/or finalization. Remove those variables
4363 : : that don't really need to be initialized or finalized, and return
4364 : : the resulting list. The order in which the variables appear in
4365 : : VARS is in reverse order of the order in which they should actually
4366 : : be initialized. That order is preserved. */
4367 : :
4368 : : static tree
4369 : 288608 : prune_vars_needing_no_initialization (tree *vars)
4370 : : {
4371 : 288608 : tree *var = vars;
4372 : 288608 : tree result = NULL_TREE;
4373 : :
4374 : 298588 : while (*var)
4375 : : {
4376 : 9980 : tree t = *var;
4377 : 9980 : tree decl = TREE_VALUE (t);
4378 : 9980 : tree init = TREE_PURPOSE (t);
4379 : :
4380 : : /* Deal gracefully with error. */
4381 : 9980 : if (error_operand_p (decl))
4382 : : {
4383 : 0 : var = &TREE_CHAIN (t);
4384 : 0 : continue;
4385 : : }
4386 : :
4387 : : /* The only things that can be initialized are variables. */
4388 : 9980 : gcc_assert (VAR_P (decl));
4389 : :
4390 : : /* If this object is not defined, we don't need to do anything
4391 : : here. */
4392 : 9980 : if (DECL_EXTERNAL (decl))
4393 : : {
4394 : 0 : var = &TREE_CHAIN (t);
4395 : 0 : continue;
4396 : : }
4397 : :
4398 : : /* Also, if the initializer already contains errors, we can bail
4399 : : out now. */
4400 : 9662 : if (init && TREE_CODE (init) == TREE_LIST
4401 : 9980 : && value_member (error_mark_node, init))
4402 : : {
4403 : 0 : var = &TREE_CHAIN (t);
4404 : 0 : continue;
4405 : : }
4406 : :
4407 : : /* This variable is going to need initialization and/or
4408 : : finalization, so we add it to the list. */
4409 : 9980 : *var = TREE_CHAIN (t);
4410 : 9980 : TREE_CHAIN (t) = result;
4411 : 9980 : result = t;
4412 : : }
4413 : :
4414 : 288608 : return result;
4415 : : }
4416 : :
4417 : : /* Split VAR_LIST by init priority and add into PARTS hash table.
4418 : : This reverses the variable ordering. */
4419 : :
4420 : : void
4421 : 5260 : partition_vars_for_init_fini (tree var_list, priority_map_t *(&parts)[2])
4422 : : {
4423 : 14647 : for (auto node = var_list; node; node = TREE_CHAIN (node))
4424 : : {
4425 : 9387 : tree decl = TREE_VALUE (node);
4426 : 9387 : tree init = TREE_PURPOSE (node);
4427 : 9387 : bool has_cleanup = !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl));
4428 : 9387 : unsigned priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
4429 : :
4430 : 9387 : if (init || (flag_use_cxa_atexit && has_cleanup))
4431 : : {
4432 : : // Add to initialization list.
4433 : 9379 : if (!parts[true])
4434 : 5252 : parts[true] = priority_map_t::create_ggc ();
4435 : 9379 : auto &slot = parts[true]->get_or_insert (priority);
4436 : 9379 : slot = tree_cons (init, decl, slot);
4437 : : }
4438 : :
4439 : 9387 : if (!flag_use_cxa_atexit && has_cleanup)
4440 : : {
4441 : : // Add to finalization list.
4442 : 20 : if (!parts[false])
4443 : 12 : parts[false] = priority_map_t::create_ggc ();
4444 : 20 : auto &slot = parts[false]->get_or_insert (priority);
4445 : 20 : slot = tree_cons (NULL_TREE, decl, slot);
4446 : : }
4447 : : }
4448 : 5260 : }
4449 : :
4450 : : /* Make sure we have told the back end about all the variables in
4451 : : VARS. */
4452 : :
4453 : : static void
4454 : 5372 : write_out_vars (tree vars)
4455 : : {
4456 : 5372 : tree v;
4457 : :
4458 : 15352 : for (v = vars; v; v = TREE_CHAIN (v))
4459 : : {
4460 : 9980 : tree var = TREE_VALUE (v);
4461 : 9980 : if (!var_finalized_p (var))
4462 : : {
4463 : 537 : import_export_decl (var);
4464 : 537 : rest_of_decl_compilation (var, 1, 1);
4465 : : }
4466 : : }
4467 : 5372 : }
4468 : :
4469 : : /* Generate a static constructor or destructor that calls the given
4470 : : init/fini fns at the indicated priority. */
4471 : :
4472 : : static void
4473 : 6459 : generate_ctor_or_dtor_function (bool initp, unsigned priority,
4474 : : tree fns, location_t locus)
4475 : : {
4476 : 6459 : input_location = locus;
4477 : 6459 : tree body = start_objects (initp, priority, bool (fns));
4478 : :
4479 : 6459 : if (fns)
4480 : : {
4481 : : /* To make sure dynamic construction doesn't access globals from
4482 : : other compilation units where they might not be yet
4483 : : constructed, for -fsanitize=address insert
4484 : : __asan_before_dynamic_init call that prevents access to
4485 : : either all global variables that need construction in other
4486 : : compilation units, or at least those that haven't been
4487 : : initialized yet. Variables that need dynamic construction in
4488 : : the current compilation unit are kept accessible. */
4489 : 5290 : if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4490 : 21 : finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
4491 : :
4492 : : /* Call the static init/fini functions. */
4493 : 10593 : for (tree node = fns; node; node = TREE_CHAIN (node))
4494 : : {
4495 : 5303 : tree fn = TREE_PURPOSE (node);
4496 : :
4497 : : // We should never find a pure or constant cdtor.
4498 : 5303 : gcc_checking_assert (!(flags_from_decl_or_type (fn)
4499 : : & (ECF_CONST | ECF_PURE)));
4500 : :
4501 : 5303 : tree call = cp_build_function_call_nary (fn, tf_warning_or_error,
4502 : : NULL_TREE);
4503 : 5303 : finish_expr_stmt (call);
4504 : : }
4505 : :
4506 : : /* Revert what __asan_before_dynamic_init did by calling
4507 : : __asan_after_dynamic_init. */
4508 : 5290 : if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4509 : 21 : finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4510 : : }
4511 : :
4512 : : /* Close out the function, and arrange for it to be called at init
4513 : : or fini time, if non-empty. (Even non-nop module initializer
4514 : : functions need this, as we cannot guarantee the module is
4515 : : imported somewhere in the program.) */
4516 : 6459 : expand_or_defer_fn (finish_objects (initp, priority, body, fns != NULL_TREE));
4517 : 6459 : }
4518 : :
4519 : : /* Return C++ property of T, based on given operation OP. */
4520 : :
4521 : : static int
4522 : 2568 : cpp_check (tree t, cpp_operation op)
4523 : : {
4524 : 2568 : switch (op)
4525 : : {
4526 : 12 : case HAS_DEPENDENT_TEMPLATE_ARGS:
4527 : 12 : {
4528 : 12 : tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4529 : 12 : if (!ti)
4530 : : return 0;
4531 : 12 : ++processing_template_decl;
4532 : 12 : const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4533 : 12 : --processing_template_decl;
4534 : 12 : return dep;
4535 : : }
4536 : 390 : case IS_ABSTRACT:
4537 : 390 : return DECL_PURE_VIRTUAL_P (t);
4538 : 217 : case IS_ASSIGNMENT_OPERATOR:
4539 : 434 : return DECL_ASSIGNMENT_OPERATOR_P (t);
4540 : 217 : case IS_CONSTRUCTOR:
4541 : 434 : return DECL_CONSTRUCTOR_P (t);
4542 : 217 : case IS_DESTRUCTOR:
4543 : 434 : return DECL_DESTRUCTOR_P (t);
4544 : 217 : case IS_COPY_CONSTRUCTOR:
4545 : 630 : return DECL_COPY_CONSTRUCTOR_P (t);
4546 : 217 : case IS_MOVE_CONSTRUCTOR:
4547 : 633 : return DECL_MOVE_CONSTRUCTOR_P (t);
4548 : 593 : case IS_TEMPLATE:
4549 : 593 : return TREE_CODE (t) == TEMPLATE_DECL;
4550 : 488 : case IS_TRIVIAL:
4551 : 488 : return trivial_type_p (t);
4552 : : default:
4553 : : return 0;
4554 : : }
4555 : : }
4556 : :
4557 : : /* Collect source file references recursively, starting from NAMESPC. */
4558 : :
4559 : : static void
4560 : 104 : collect_source_refs (tree namespc)
4561 : : {
4562 : : /* Iterate over names in this name space. */
4563 : 228849 : for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4564 : 228745 : if (DECL_IS_UNDECLARED_BUILTIN (t))
4565 : : ;
4566 : 320 : else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4567 : 4 : collect_source_refs (t);
4568 : : else
4569 : 316 : collect_source_ref (DECL_SOURCE_FILE (t));
4570 : 104 : }
4571 : :
4572 : : /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4573 : : starting from NAMESPC. */
4574 : :
4575 : : static void
4576 : 304 : collect_ada_namespace (tree namespc, const char *source_file)
4577 : : {
4578 : 304 : tree decl = NAMESPACE_LEVEL (namespc)->names;
4579 : :
4580 : : /* Collect decls from this namespace. This will skip
4581 : : NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4582 : 304 : collect_ada_nodes (decl, source_file);
4583 : :
4584 : : /* Now scan for namespace children, and dump them. */
4585 : 274203 : for (; decl; decl = TREE_CHAIN (decl))
4586 : 273595 : if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4587 : 204 : collect_ada_namespace (decl, source_file);
4588 : 304 : }
4589 : :
4590 : : /* Returns true iff there is a definition available for variable or
4591 : : function DECL. */
4592 : :
4593 : : bool
4594 : 59040867 : decl_defined_p (tree decl)
4595 : : {
4596 : 59040867 : if (TREE_CODE (decl) == FUNCTION_DECL)
4597 : 55262670 : return (DECL_INITIAL (decl) != NULL_TREE
4598 : : /* A pending instantiation of a friend temploid is defined. */
4599 : 55262670 : || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4600 : 7400051 : && DECL_INITIAL (DECL_TEMPLATE_RESULT
4601 : : (DECL_TI_TEMPLATE (decl)))));
4602 : : else
4603 : : {
4604 : 3778197 : gcc_assert (VAR_P (decl));
4605 : 3778197 : return !DECL_EXTERNAL (decl);
4606 : : }
4607 : : }
4608 : :
4609 : : /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4610 : :
4611 : : [expr.const]
4612 : :
4613 : : An integral constant-expression can only involve ... const
4614 : : variables of integral or enumeration types initialized with
4615 : : constant expressions ...
4616 : :
4617 : : C++0x also allows constexpr variables and temporaries initialized
4618 : : with constant expressions. We handle the former here, but the latter
4619 : : are just folded away in cxx_eval_constant_expression.
4620 : :
4621 : : The standard does not require that the expression be non-volatile.
4622 : : G++ implements the proposed correction in DR 457. */
4623 : :
4624 : : bool
4625 : 768646952 : decl_constant_var_p (tree decl)
4626 : : {
4627 : 768646952 : if (!decl_maybe_constant_var_p (decl))
4628 : : return false;
4629 : :
4630 : : /* We don't know if a template static data member is initialized with
4631 : : a constant expression until we instantiate its initializer. Even
4632 : : in the case of a constexpr variable, we can't treat it as a
4633 : : constant until its initializer is complete in case it's used in
4634 : : its own initializer. */
4635 : 83152955 : maybe_instantiate_decl (decl);
4636 : 83142171 : return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4637 : : }
4638 : :
4639 : : /* Returns true if DECL could be a symbolic constant variable, depending on
4640 : : its initializer. */
4641 : :
4642 : : bool
4643 : 1004737142 : decl_maybe_constant_var_p (tree decl)
4644 : : {
4645 : 1004737142 : tree type = TREE_TYPE (decl);
4646 : 1004737142 : if (!VAR_P (decl))
4647 : : return false;
4648 : 446628238 : if (DECL_DECLARED_CONSTEXPR_P (decl) && !TREE_THIS_VOLATILE (decl))
4649 : : return true;
4650 : 281467807 : if (DECL_HAS_VALUE_EXPR_P (decl))
4651 : : /* A proxy isn't constant. */
4652 : : return false;
4653 : 280884412 : if (TYPE_REF_P (type))
4654 : : /* References can be constant. */;
4655 : 278023715 : else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4656 : 278023715 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4657 : : /* And const integers. */;
4658 : : else
4659 : : return false;
4660 : :
4661 : 54067249 : if (DECL_INITIAL (decl)
4662 : 84820536 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4663 : : /* We know the initializer, and it isn't constant. */
4664 : : return false;
4665 : : else
4666 : : return true;
4667 : : }
4668 : :
4669 : : /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4670 : : called from grokfndecl and grokvardecl; in all modes it is called from
4671 : : cp_write_global_declarations. */
4672 : :
4673 : : void
4674 : 1425346 : no_linkage_error (tree decl)
4675 : : {
4676 : 1425346 : if (cxx_dialect >= cxx11
4677 : 1425346 : && (decl_defined_p (decl)
4678 : : /* Treat templates which limit_bad_template_recursion decided
4679 : : not to instantiate as if they were defined. */
4680 : 101 : || (errorcount + sorrycount > 0
4681 : 31 : && DECL_LANG_SPECIFIC (decl)
4682 : 31 : && DECL_TEMPLATE_INFO (decl)
4683 : 23 : && warning_suppressed_p (decl /* What warning? */))))
4684 : : /* In C++11 it's ok if the decl is defined. */
4685 : 319777 : return;
4686 : :
4687 : 1105569 : if (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl))
4688 : : /* An imported decl is ok. */
4689 : : return;
4690 : :
4691 : 1105521 : tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4692 : 1105521 : if (t == NULL_TREE)
4693 : : /* The type that got us on no_linkage_decls must have gotten a name for
4694 : : linkage purposes. */;
4695 : 61 : else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4696 : : // FIXME: This is now invalid, as a DR to c++98
4697 : : /* The type might end up having a typedef name for linkage purposes. */
4698 : 0 : vec_safe_push (no_linkage_decls, decl);
4699 : 160 : else if (TYPE_UNNAMED_P (t))
4700 : : {
4701 : 35 : bool d = false;
4702 : 35 : auto_diagnostic_group grp;
4703 : 35 : if (cxx_dialect >= cxx11)
4704 : : {
4705 : : /* If t is declared in a module CMI, then decl could actually
4706 : : be defined in a different TU, so don't warn since C++20. */
4707 : 12 : tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
4708 : 12 : if (relaxed != NULL_TREE)
4709 : 9 : d = permerror (DECL_SOURCE_LOCATION (decl),
4710 : : "%q#D, declared using an unnamed type, "
4711 : : "is used but never defined", decl);
4712 : 3 : else if (cxx_dialect < cxx20)
4713 : 1 : d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
4714 : : "%q#D, declared using an unnamed type, "
4715 : : "is used but not defined", decl);
4716 : : }
4717 : 23 : else if (DECL_EXTERN_C_P (decl))
4718 : : /* Allow this; it's pretty common in C. */;
4719 : 22 : else if (VAR_P (decl))
4720 : : /* DRs 132, 319 and 389 seem to indicate types with
4721 : : no linkage can only be used to declare extern "C"
4722 : : entities. Since it's not always an error in the
4723 : : ISO C++ 90 Standard, we only issue a warning. */
4724 : 18 : d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4725 : : "with no linkage used to declare variable %q#D with "
4726 : : "linkage", decl);
4727 : : else
4728 : 4 : d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4729 : : "linkage used to declare function %q#D with linkage",
4730 : : decl);
4731 : 32 : if (d && is_typedef_decl (TYPE_NAME (t)))
4732 : 1 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4733 : : "to the unqualified type, so it is not used for linkage",
4734 : 1 : TYPE_NAME (t));
4735 : : /* Suppress warning from check_global_declaration if needed. */
4736 : 35 : if (d)
4737 : 29 : suppress_warning (decl, OPT_Wunused);
4738 : 35 : }
4739 : 26 : else if (cxx_dialect >= cxx11)
4740 : : {
4741 : 24 : if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4742 : : {
4743 : : /* Similarly for local types in a function with vague linkage or
4744 : : defined in a module CMI, then decl could actually be defined
4745 : : in a different TU, so don't warn since C++20. */
4746 : 21 : bool d = false;
4747 : 21 : tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
4748 : 21 : if (relaxed != NULL_TREE)
4749 : 15 : d = permerror (DECL_SOURCE_LOCATION (decl),
4750 : : "%q#D, declared using local type "
4751 : : "%qT, is used but never defined", decl, t);
4752 : 6 : else if (cxx_dialect < cxx20)
4753 : 3 : d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
4754 : : "%q#D, declared using local type "
4755 : : "%qT, is used but not defined here", decl, t);
4756 : : /* Suppress warning from check_global_declaration if needed. */
4757 : 18 : if (d)
4758 : 18 : suppress_warning (decl, OPT_Wunused);
4759 : : }
4760 : : }
4761 : 2 : else if (VAR_P (decl))
4762 : 1 : warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4763 : : "used to declare variable %q#D with linkage", t, decl);
4764 : : else
4765 : 1 : permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4766 : : "to declare function %q#D with linkage", t, decl);
4767 : : }
4768 : :
4769 : : /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4770 : :
4771 : : static void
4772 : 100 : collect_all_refs (const char *source_file)
4773 : : {
4774 : 100 : collect_ada_namespace (global_namespace, source_file);
4775 : 100 : }
4776 : :
4777 : : /* Clear DECL_EXTERNAL for NODE. */
4778 : :
4779 : : static bool
4780 : 78514270 : clear_decl_external (struct cgraph_node *node, void * /*data*/)
4781 : : {
4782 : 78514270 : DECL_EXTERNAL (node->decl) = 0;
4783 : 78514270 : return false;
4784 : : }
4785 : :
4786 : : /* Build up the function to run dynamic initializers for thread_local
4787 : : variables in this translation unit and alias the init functions for the
4788 : : individual variables to it. */
4789 : :
4790 : : static void
4791 : 144304 : handle_tls_init (void)
4792 : : {
4793 : 144304 : tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4794 : 144304 : if (vars == NULL_TREE)
4795 : : return;
4796 : :
4797 : 112 : location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4798 : :
4799 : 112 : write_out_vars (vars);
4800 : :
4801 : 112 : tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4802 : : boolean_type_node);
4803 : 112 : TREE_PUBLIC (guard) = false;
4804 : 112 : TREE_STATIC (guard) = true;
4805 : 112 : DECL_ARTIFICIAL (guard) = true;
4806 : 112 : DECL_IGNORED_P (guard) = true;
4807 : 112 : TREE_USED (guard) = true;
4808 : 112 : CP_DECL_THREAD_LOCAL_P (guard) = true;
4809 : 112 : set_decl_tls_model (guard, decl_default_tls_model (guard));
4810 : 112 : pushdecl_top_level_and_finish (guard, NULL_TREE);
4811 : :
4812 : 112 : tree fn = get_local_tls_init_fn (loc);
4813 : 112 : start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4814 : 112 : tree body = begin_function_body ();
4815 : 112 : tree if_stmt = begin_if_stmt ();
4816 : 112 : tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4817 : : tf_warning_or_error);
4818 : 112 : finish_if_stmt_cond (cond, if_stmt);
4819 : 112 : finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4820 : : boolean_true_node,
4821 : : tf_warning_or_error));
4822 : 705 : for (; vars; vars = TREE_CHAIN (vars))
4823 : : {
4824 : 593 : tree var = TREE_VALUE (vars);
4825 : 593 : tree init = TREE_PURPOSE (vars);
4826 : 593 : one_static_initialization_or_destruction (/*initp=*/true, var, init);
4827 : :
4828 : : /* Output init aliases even with -fno-extern-tls-init. */
4829 : 593 : if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4830 : : {
4831 : 590 : tree single_init_fn = get_tls_init_fn (var);
4832 : 590 : if (single_init_fn == NULL_TREE)
4833 : 0 : continue;
4834 : 590 : cgraph_node *alias
4835 : 590 : = cgraph_node::get_create (fn)->create_same_body_alias
4836 : 590 : (single_init_fn, fn);
4837 : 590 : gcc_assert (alias != NULL);
4838 : : }
4839 : : }
4840 : :
4841 : 112 : finish_then_clause (if_stmt);
4842 : 112 : finish_if_stmt (if_stmt);
4843 : 112 : finish_function_body (body);
4844 : 112 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
4845 : : }
4846 : :
4847 : : /* We're at the end of compilation, so generate any mangling aliases that
4848 : : we've been saving up, if DECL is going to be output and ID2 isn't
4849 : : already taken by another declaration. */
4850 : :
4851 : : static void
4852 : 207563 : generate_mangling_alias (tree decl, tree id2)
4853 : : {
4854 : 207563 : struct cgraph_node *n = NULL;
4855 : :
4856 : 207563 : if (TREE_CODE (decl) == FUNCTION_DECL)
4857 : : {
4858 : 202925 : n = cgraph_node::get (decl);
4859 : 202925 : if (!n)
4860 : : /* Don't create an alias to an unreferenced function. */
4861 : : return;
4862 : : }
4863 : :
4864 : 207557 : tree *slot
4865 : 207557 : = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4866 : : INSERT);
4867 : :
4868 : : /* If there's a declaration already using this mangled name,
4869 : : don't create a compatibility alias that conflicts. */
4870 : 207557 : if (*slot)
4871 : : return;
4872 : :
4873 : 207479 : tree alias = make_alias_for (decl, id2);
4874 : 207479 : *slot = alias;
4875 : :
4876 : 207479 : DECL_IGNORED_P (alias) = 1;
4877 : 207479 : TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4878 : 207479 : DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4879 : 207479 : if (vague_linkage_p (decl))
4880 : 204823 : DECL_WEAK (alias) = 1;
4881 : :
4882 : 207479 : if (n)
4883 : 202843 : n->create_same_body_alias (alias, decl);
4884 : : else
4885 : 4636 : varpool_node::create_extra_name_alias (alias, decl);
4886 : : }
4887 : :
4888 : : /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4889 : : the end of translation, for compatibility across bugs in the mangling
4890 : : implementation. */
4891 : :
4892 : : void
4893 : 207563 : note_mangling_alias (tree decl, tree id2)
4894 : : {
4895 : 207563 : if (TARGET_SUPPORTS_ALIASES)
4896 : : {
4897 : 207563 : if (!defer_mangling_aliases)
4898 : 105966 : generate_mangling_alias (decl, id2);
4899 : : else
4900 : : {
4901 : 101597 : vec_safe_push (mangling_aliases, decl);
4902 : 101597 : vec_safe_push (mangling_aliases, id2);
4903 : : }
4904 : : }
4905 : 207563 : }
4906 : :
4907 : : /* Emit all mangling aliases that were deferred up to this point. */
4908 : :
4909 : : void
4910 : 99146 : generate_mangling_aliases ()
4911 : : {
4912 : 200743 : while (!vec_safe_is_empty (mangling_aliases))
4913 : : {
4914 : 101597 : tree id2 = mangling_aliases->pop();
4915 : 101597 : tree decl = mangling_aliases->pop();
4916 : 101597 : generate_mangling_alias (decl, id2);
4917 : : }
4918 : 99146 : defer_mangling_aliases = false;
4919 : 99146 : }
4920 : :
4921 : : /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4922 : : set. NEED_WARNING is true if we must warn about collisions. We do
4923 : : this to spot changes in mangling that may require compatibility
4924 : : aliases. */
4925 : :
4926 : : void
4927 : 53540932 : record_mangling (tree decl, bool need_warning)
4928 : : {
4929 : 53540932 : if (!mangled_decls)
4930 : 76178 : mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4931 : :
4932 : 53540932 : gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4933 : 53540932 : tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4934 : 53540932 : tree *slot
4935 : 53540932 : = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4936 : : INSERT);
4937 : :
4938 : : /* If this is already an alias, cancel the alias, because the real
4939 : : decl takes precedence. */
4940 : 53540932 : if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4941 : : {
4942 : 44 : if (symtab_node *n = symtab_node::get (*slot))
4943 : : {
4944 : 43 : if (n->cpp_implicit_alias)
4945 : : /* Actually removing the node isn't safe if other code is already
4946 : : holding a pointer to it, so just neutralize it. */
4947 : 43 : n->reset ();
4948 : : }
4949 : : else
4950 : : /* analyze_functions might have already removed the alias from the
4951 : : symbol table if it's internal. */
4952 : 1 : gcc_checking_assert (!TREE_PUBLIC (*slot));
4953 : :
4954 : 44 : *slot = NULL_TREE;
4955 : : }
4956 : :
4957 : 53540932 : if (!*slot)
4958 : 53540689 : *slot = decl;
4959 : 243 : else if (need_warning)
4960 : : {
4961 : 4 : error_at (DECL_SOURCE_LOCATION (decl),
4962 : : "mangling of %q#D as %qE conflicts with a previous mangle",
4963 : : decl, id);
4964 : 4 : inform (DECL_SOURCE_LOCATION (*slot),
4965 : : "previous mangling %q#D", *slot);
4966 : 4 : inform (DECL_SOURCE_LOCATION (decl),
4967 : : "a later %<-fabi-version=%> (or =0)"
4968 : : " avoids this error with a change in mangling");
4969 : 4 : *slot = decl;
4970 : : }
4971 : 53540932 : }
4972 : :
4973 : : /* The mangled name of DECL is being forcibly changed to NAME. Remove
4974 : : any existing knowledge of DECL's mangled name meaning DECL. */
4975 : :
4976 : : void
4977 : 222151411 : overwrite_mangling (tree decl, tree name)
4978 : : {
4979 : 222151411 : if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4980 : 209810 : if ((TREE_CODE (decl) == VAR_DECL
4981 : 209715 : || TREE_CODE (decl) == FUNCTION_DECL)
4982 : 209810 : && mangled_decls)
4983 : 405006 : if (tree *slot
4984 : 202503 : = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4985 : : NO_INSERT))
4986 : 105955 : if (*slot == decl)
4987 : : {
4988 : 46 : mangled_decls->clear_slot (slot);
4989 : :
4990 : : /* If this is an alias, remove it from the symbol table. */
4991 : 46 : if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4992 : 0 : if (symtab_node *n = symtab_node::get (decl))
4993 : 0 : if (n->cpp_implicit_alias)
4994 : 0 : n->remove ();
4995 : : }
4996 : :
4997 : 222151411 : DECL_ASSEMBLER_NAME_RAW (decl) = name;
4998 : 222151411 : }
4999 : :
5000 : : /* The entire file is now complete. If requested, dump everything
5001 : : to a file. */
5002 : :
5003 : : static void
5004 : 99232 : dump_tu (void)
5005 : : {
5006 : 99232 : dump_flags_t flags;
5007 : 99232 : if (FILE *stream = dump_begin (raw_dump_id, &flags))
5008 : : {
5009 : 4 : dump_node (global_namespace, flags & ~TDF_SLIM, stream);
5010 : 4 : dump_end (raw_dump_id, stream);
5011 : : }
5012 : 99232 : }
5013 : :
5014 : : static location_t locus_at_end_of_parsing;
5015 : :
5016 : : /* Check the deallocation functions for CODE to see if we want to warn that
5017 : : only one was defined. */
5018 : :
5019 : : static void
5020 : 2008 : maybe_warn_sized_delete (enum tree_code code)
5021 : : {
5022 : 2008 : tree sized = NULL_TREE;
5023 : 2008 : tree unsized = NULL_TREE;
5024 : :
5025 : 8406 : for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
5026 : 8406 : iter; ++iter)
5027 : : {
5028 : 6398 : tree fn = *iter;
5029 : : /* We're only interested in usual deallocation functions. */
5030 : 6398 : if (!usual_deallocation_fn_p (fn))
5031 : 918 : continue;
5032 : 5480 : if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
5033 : : unsized = fn;
5034 : : else
5035 : 3472 : sized = fn;
5036 : : }
5037 : 2008 : if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
5038 : 7 : warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
5039 : : "the program should also define %qD", sized);
5040 : 2001 : else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
5041 : 4 : warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
5042 : : "the program should also define %qD", unsized);
5043 : 2008 : }
5044 : :
5045 : : /* Check the global deallocation functions to see if we want to warn about
5046 : : defining unsized without sized (or vice versa). */
5047 : :
5048 : : static void
5049 : 99146 : maybe_warn_sized_delete ()
5050 : : {
5051 : 99146 : if (!flag_sized_deallocation || !warn_sized_deallocation)
5052 : : return;
5053 : 1004 : maybe_warn_sized_delete (DELETE_EXPR);
5054 : 1004 : maybe_warn_sized_delete (VEC_DELETE_EXPR);
5055 : : }
5056 : :
5057 : : /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
5058 : : look them up when evaluating non-type template parameters. Now we need to
5059 : : lower them to something the back end can understand. */
5060 : :
5061 : : static void
5062 : 99146 : lower_var_init ()
5063 : : {
5064 : 99146 : varpool_node *node;
5065 : 32930624 : FOR_EACH_VARIABLE (node)
5066 : : {
5067 : 16366166 : tree d = node->decl;
5068 : 16366166 : if (tree init = DECL_INITIAL (d))
5069 : 14421623 : DECL_INITIAL (d) = cplus_expand_constant (init);
5070 : : }
5071 : 99146 : }
5072 : :
5073 : : /* This routine is called at the end of compilation.
5074 : : Its job is to create all the code needed to initialize and
5075 : : destroy the global aggregates. We do the destruction
5076 : : first, since that way we only need to reverse the decls once. */
5077 : :
5078 : : void
5079 : 99250 : c_parse_final_cleanups (void)
5080 : : {
5081 : 99250 : size_t i;
5082 : 99250 : tree decl;
5083 : :
5084 : 99250 : locus_at_end_of_parsing = input_location;
5085 : : /* We're done parsing. */
5086 : 99250 : at_eof = 1;
5087 : :
5088 : : /* Bad parse errors. Just forget about it. */
5089 : 198500 : if (! global_bindings_p () || current_class_type
5090 : 198500 : || !vec_safe_is_empty (decl_namespace_list))
5091 : 89 : return;
5092 : :
5093 : : /* This is the point to write out a PCH if we're doing that.
5094 : : In that case we do not want to do anything else. */
5095 : 99247 : if (pch_file)
5096 : : {
5097 : : /* Mangle all symbols at PCH creation time. */
5098 : 86 : symtab_node *node;
5099 : 28523 : FOR_EACH_SYMBOL (node)
5100 : 28437 : if (! is_a <varpool_node *> (node)
5101 : 5043 : || ! DECL_HARD_REGISTER (node->decl))
5102 : 28437 : DECL_ASSEMBLER_NAME (node->decl);
5103 : 86 : c_common_write_pch ();
5104 : 86 : dump_tu ();
5105 : : /* Ensure even the callers don't try to finalize the CU. */
5106 : 86 : flag_syntax_only = 1;
5107 : 86 : return;
5108 : : }
5109 : :
5110 : 99161 : timevar_stop (TV_PHASE_PARSING);
5111 : 99161 : timevar_start (TV_PHASE_DEFERRED);
5112 : :
5113 : 99161 : symtab->process_same_body_aliases ();
5114 : :
5115 : : /* Handle -fdump-ada-spec[-slim] */
5116 : 99161 : if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
5117 : : {
5118 : 100 : collect_source_ref (main_input_filename);
5119 : 100 : if (!flag_dump_ada_spec_slim)
5120 : 100 : collect_source_refs (global_namespace);
5121 : :
5122 : 100 : dump_ada_specs (collect_all_refs, cpp_check);
5123 : : }
5124 : :
5125 : : /* FIXME - huh? was input_line -= 1;*/
5126 : :
5127 : : /* We now have to write out all the stuff we put off writing out.
5128 : : These include:
5129 : :
5130 : : o Template specializations that we have not yet instantiated,
5131 : : but which are needed.
5132 : : o Initialization and destruction for non-local objects with
5133 : : static storage duration. (Local objects with static storage
5134 : : duration are initialized when their scope is first entered,
5135 : : and are cleaned up via atexit.)
5136 : : o Virtual function tables.
5137 : :
5138 : : All of these may cause others to be needed. For example,
5139 : : instantiating one function may cause another to be needed, and
5140 : : generating the initializer for an object may cause templates to be
5141 : : instantiated, etc., etc. */
5142 : :
5143 : 99161 : emit_support_tinfos ();
5144 : :
5145 : : /* Track vtables we want to emit that refer to consteval functions. */
5146 : 99161 : auto_vec<tree> consteval_vtables;
5147 : :
5148 : 99161 : int retries = 0;
5149 : 99161 : unsigned ssdf_count = 0;
5150 : 244163 : for (bool reconsider = true; reconsider; retries++)
5151 : : {
5152 : 145017 : reconsider = false;
5153 : :
5154 : : /* If there are templates that we've put off instantiating, do
5155 : : them now. */
5156 : 145017 : instantiate_pending_templates (retries);
5157 : 145002 : ggc_collect ();
5158 : :
5159 : 145002 : if (header_module_p ())
5160 : : /* A header modules initializations are handled in its
5161 : : importer. */
5162 : 698 : continue;
5163 : :
5164 : : /* Write out virtual tables as required. Writing out the
5165 : : virtual table for a template class may cause the
5166 : : instantiation of members of that class. If we write out
5167 : : vtables then we remove the class from our list so we don't
5168 : : have to look at it again. */
5169 : 144304 : tree t;
5170 : 144304 : for (i = keyed_classes->length ();
5171 : 3498123 : keyed_classes->iterate (--i, &t);)
5172 : 3353819 : if (maybe_emit_vtables (t, consteval_vtables))
5173 : : {
5174 : 267036 : reconsider = true;
5175 : 267036 : keyed_classes->unordered_remove (i);
5176 : : }
5177 : : /* The input_location may have been changed during marking of
5178 : : vtable entries. */
5179 : 144304 : input_location = locus_at_end_of_parsing;
5180 : :
5181 : : /* Write out needed type info variables. We have to be careful
5182 : : looping through unemitted decls, because emit_tinfo_decl may
5183 : : cause other variables to be needed. New elements will be
5184 : : appended, and we remove from the vector those that actually
5185 : : get emitted. */
5186 : 144304 : for (i = unemitted_tinfo_decls->length ();
5187 : 5735064 : unemitted_tinfo_decls->iterate (--i, &t);)
5188 : 5590760 : if (DECL_INITIAL (t) || emit_tinfo_decl (t))
5189 : : {
5190 : 353395 : reconsider = true;
5191 : 353395 : unemitted_tinfo_decls->unordered_remove (i);
5192 : : }
5193 : :
5194 : : /* The list of objects with static storage duration is built up
5195 : : in reverse order. We clear STATIC_AGGREGATES so that any new
5196 : : aggregates added during the initialization of these will be
5197 : : initialized in the correct order when we next come around the
5198 : : loop. */
5199 : 144304 : if (tree vars = prune_vars_needing_no_initialization (&static_aggregates))
5200 : : {
5201 : 5260 : if (flag_openmp)
5202 : : /* Add initializer information from VARS into
5203 : : DYNAMIC_INITIALIZERS. */
5204 : 387 : for (t = vars; t; t = TREE_CHAIN (t))
5205 : 564 : hash_map_safe_put<hm_ggc> (dynamic_initializers,
5206 : 282 : TREE_VALUE (t), TREE_PURPOSE (t));
5207 : :
5208 : : /* Make sure the back end knows about all the variables. */
5209 : 5260 : write_out_vars (vars);
5210 : :
5211 : 5260 : function_depth++; // Disable GC
5212 : 5260 : priority_map_t *parts[2] = {nullptr, nullptr};
5213 : 5260 : partition_vars_for_init_fini (vars, parts);
5214 : :
5215 : 15780 : for (unsigned initp = 2; initp--;)
5216 : 10520 : if (parts[initp])
5217 : 21078 : for (auto iter : *parts[initp])
5218 : : {
5219 : 5275 : auto list = iter.second;
5220 : 5275 : if (initp)
5221 : : // Partitioning kept the vars in reverse order.
5222 : : // We only want that for dtors.
5223 : 5263 : list = nreverse (list);
5224 : 5275 : emit_partial_init_fini_fn (initp, iter.first, list,
5225 : : ssdf_count++,
5226 : : locus_at_end_of_parsing);
5227 : : }
5228 : 5260 : function_depth--; // Re-enable GC
5229 : :
5230 : : /* All those initializations and finalizations might cause
5231 : : us to need more inline functions, more template
5232 : : instantiations, etc. */
5233 : 5260 : reconsider = true;
5234 : : }
5235 : :
5236 : : /* Now do the same for thread_local variables. */
5237 : 144304 : handle_tls_init ();
5238 : :
5239 : : /* Go through the set of inline functions whose bodies have not
5240 : : been emitted yet. If out-of-line copies of these functions
5241 : : are required, emit them. */
5242 : 137184418 : FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5243 : : {
5244 : : /* Does it need synthesizing? */
5245 : 141778648 : if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
5246 : 137040120 : && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
5247 : : {
5248 : : /* Even though we're already at the top-level, we push
5249 : : there again. That way, when we pop back a few lines
5250 : : hence, all of our state is restored. Otherwise,
5251 : : finish_function doesn't clean things up, and we end
5252 : : up with CURRENT_FUNCTION_DECL set. */
5253 : 6 : push_to_top_level ();
5254 : : /* The decl's location will mark where it was first
5255 : : needed. Save that so synthesize method can indicate
5256 : : where it was needed from, in case of error */
5257 : 6 : input_location = DECL_SOURCE_LOCATION (decl);
5258 : 6 : synthesize_method (decl);
5259 : 6 : pop_from_top_level ();
5260 : 6 : reconsider = true;
5261 : : }
5262 : :
5263 : 137040114 : if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
5264 : 432 : generate_tls_wrapper (decl);
5265 : :
5266 : 137040114 : if (!DECL_SAVED_TREE (decl))
5267 : 15603801 : continue;
5268 : :
5269 : 121436313 : cgraph_node *node = cgraph_node::get_create (decl);
5270 : :
5271 : : /* We lie to the back end, pretending that some functions
5272 : : are not defined when they really are. This keeps these
5273 : : functions from being put out unnecessarily. But, we must
5274 : : stop lying when the functions are referenced, or if they
5275 : : are not comdat since they need to be put out now. If
5276 : : DECL_INTERFACE_KNOWN, then we have already set
5277 : : DECL_EXTERNAL appropriately, so there's no need to check
5278 : : again, and we do not want to clear DECL_EXTERNAL if a
5279 : : previous call to import_export_decl set it.
5280 : :
5281 : : This is done in a separate for cycle, because if some
5282 : : deferred function is contained in another deferred
5283 : : function later in deferred_fns varray,
5284 : : rest_of_compilation would skip this function and we
5285 : : really cannot expand the same function twice. */
5286 : 121436313 : import_export_decl (decl);
5287 : 121436313 : if (DECL_NOT_REALLY_EXTERN (decl)
5288 : 118152654 : && DECL_INITIAL (decl)
5289 : 239588967 : && decl_needed_p (decl))
5290 : : {
5291 : 70871730 : if (node->cpp_implicit_alias)
5292 : 0 : node = node->get_alias_target ();
5293 : :
5294 : 70871730 : node->call_for_symbol_thunks_and_aliases (clear_decl_external,
5295 : : NULL, true);
5296 : : /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
5297 : : group, we need to mark all symbols in the same comdat group
5298 : : that way. */
5299 : 70871730 : if (node->same_comdat_group)
5300 : 7635851 : for (cgraph_node *next
5301 : 3679639 : = dyn_cast<cgraph_node *> (node->same_comdat_group);
5302 : 7635851 : next != node;
5303 : 11592063 : next = dyn_cast<cgraph_node *> (next->same_comdat_group))
5304 : 3956212 : next->call_for_symbol_thunks_and_aliases (clear_decl_external,
5305 : : NULL, true);
5306 : : }
5307 : :
5308 : : /* If we're going to need to write this function out, and
5309 : : there's already a body for it, create RTL for it now.
5310 : : (There might be no body if this is a method we haven't
5311 : : gotten around to synthesizing yet.) */
5312 : 121436313 : if (!DECL_EXTERNAL (decl)
5313 : 71117098 : && decl_needed_p (decl)
5314 : 71113163 : && !TREE_ASM_WRITTEN (decl)
5315 : 138571070 : && !DECL_IMMEDIATE_FUNCTION_P (decl)
5316 : 190302618 : && !node->definition)
5317 : : {
5318 : : /* We will output the function; no longer consider it in this
5319 : : loop. */
5320 : 0 : DECL_DEFER_OUTPUT (decl) = 0;
5321 : : /* Generate RTL for this function now that we know we
5322 : : need it. */
5323 : 0 : expand_or_defer_fn (decl);
5324 : 0 : reconsider = true;
5325 : : }
5326 : : }
5327 : :
5328 : 144304 : if (wrapup_namespace_globals ())
5329 : 321 : reconsider = true;
5330 : :
5331 : : /* Static data members are just like namespace-scope globals. */
5332 : 48331376 : FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
5333 : : {
5334 : 61420611 : if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
5335 : : /* Don't write it out if we haven't seen a definition. */
5336 : 52180676 : || DECL_IN_AGGR_P (decl))
5337 : 44201672 : continue;
5338 : 3985400 : import_export_decl (decl);
5339 : : /* If this static data member is needed, provide it to the
5340 : : back end. */
5341 : 3985400 : if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
5342 : 3949821 : DECL_EXTERNAL (decl) = 0;
5343 : : }
5344 : :
5345 : 289306 : if (vec_safe_length (pending_statics) != 0
5346 : 110166 : && wrapup_global_declarations (pending_statics->address (),
5347 : 55083 : pending_statics->length ()))
5348 : : reconsider = true;
5349 : : }
5350 : :
5351 : : /* All templates have been instantiated. */
5352 : 99146 : at_eof = 2;
5353 : :
5354 : 99146 : void *module_cookie = finish_module_processing (parse_in);
5355 : :
5356 : 99146 : lower_var_init ();
5357 : :
5358 : 99146 : generate_mangling_aliases ();
5359 : :
5360 : : /* All used inline functions must have a definition at this point. */
5361 : 35546065 : FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5362 : : {
5363 : 35446919 : if (/* Check online inline functions that were actually used. */
5364 : 56661699 : DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
5365 : : /* If the definition actually was available here, then the
5366 : : fact that the function was not defined merely represents
5367 : : that for some reason (use of a template repository,
5368 : : #pragma interface, etc.) we decided not to emit the
5369 : : definition here. */
5370 : 21212860 : && !DECL_INITIAL (decl)
5371 : : /* A defaulted fn or TLS wrapper in a header module can be
5372 : : synthesized on demand later. (In non-header modules we
5373 : : should have synthesized it above.) */
5374 : 507 : && !(header_module_p ()
5375 : 3 : && (DECL_DEFAULTED_FN (decl) || decl_tls_wrapper_p (decl)))
5376 : : /* Don't complain if the template was defined. */
5377 : 504 : && !(DECL_TEMPLATE_INSTANTIATION (decl)
5378 : 446 : && DECL_INITIAL (DECL_TEMPLATE_RESULT
5379 : : (template_for_substitution (decl))))
5380 : 35446997 : && warning_at (DECL_SOURCE_LOCATION (decl), 0,
5381 : : "inline function %qD used but never defined", decl))
5382 : : /* Avoid a duplicate warning from check_global_declaration. */
5383 : 59 : suppress_warning (decl, OPT_Wunused);
5384 : : }
5385 : :
5386 : : /* So must decls that use a type with no linkage. */
5387 : 419007 : FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
5388 : 319861 : no_linkage_error (decl);
5389 : :
5390 : 99146 : maybe_warn_sized_delete ();
5391 : :
5392 : : // Place the init fns in the right order. We need to do this now,
5393 : : // so that any module init will go at the start.
5394 : 99146 : if (static_init_fini_fns[true])
5395 : 15755 : for (auto iter : *static_init_fini_fns[true])
5396 : 5259 : iter.second = nreverse (iter.second);
5397 : :
5398 : : /* Now we've instantiated all templates. Now we can escalate the functions
5399 : : we squirreled away earlier. */
5400 : 99146 : process_and_check_pending_immediate_escalating_fns ();
5401 : :
5402 : : /* Then, do the Objective-C stuff. This is where all the
5403 : : Objective-C module stuff gets generated (symtab,
5404 : : class/protocol/selector lists etc). This must be done after C++
5405 : : templates, destructors etc. so that selectors used in C++
5406 : : templates are properly allocated. */
5407 : 99146 : if (c_dialect_objc ())
5408 : 0 : objc_write_global_declarations ();
5409 : :
5410 : 99146 : bool has_module_inits = module_determine_import_inits ();
5411 : 99146 : bool has_objc_init = c_dialect_objc () && objc_static_init_needed_p ();
5412 : 99146 : if (has_module_inits || has_objc_init)
5413 : : {
5414 : 28 : input_location = locus_at_end_of_parsing;
5415 : 28 : tree body = start_partial_init_fini_fn (true, DEFAULT_INIT_PRIORITY,
5416 : : ssdf_count++);
5417 : : /* For Objective-C++, we may need to initialize metadata found
5418 : : in this module. This must be done _before_ any other static
5419 : : initializations. */
5420 : 28 : if (has_objc_init)
5421 : 0 : objc_generate_static_init_call (NULL_TREE);
5422 : 28 : if (has_module_inits)
5423 : 28 : module_add_import_initializers ();
5424 : 28 : input_location = locus_at_end_of_parsing;
5425 : 28 : finish_partial_init_fini_fn (body);
5426 : : }
5427 : :
5428 : 99146 : if (module_global_init_needed ())
5429 : : {
5430 : : // Make sure there's a default priority entry.
5431 : 1199 : if (!static_init_fini_fns[true])
5432 : 1169 : static_init_fini_fns[true] = priority_map_t::create_ggc ();
5433 : 1199 : if (static_init_fini_fns[true]->get_or_insert (DEFAULT_INIT_PRIORITY))
5434 : 99146 : has_module_inits = true;
5435 : : }
5436 : :
5437 : : /* Generate initialization and destruction functions for all
5438 : : priorities for which they are required. They have C-language
5439 : : linkage. */
5440 : 99146 : push_lang_context (lang_name_c);
5441 : 297438 : for (unsigned initp = 2; initp--;)
5442 : 198292 : if (static_init_fini_fns[initp])
5443 : : {
5444 : 12907 : for (auto iter : *static_init_fini_fns[initp])
5445 : 6459 : generate_ctor_or_dtor_function (initp, iter.first, iter.second,
5446 : : locus_at_end_of_parsing);
5447 : 6448 : static_init_fini_fns[initp] = nullptr;
5448 : : }
5449 : 99146 : pop_lang_context ();
5450 : :
5451 : 99146 : fini_modules (parse_in, module_cookie, has_module_inits);
5452 : :
5453 : : /* Generate any missing aliases. */
5454 : 99146 : maybe_apply_pending_pragma_weaks ();
5455 : :
5456 : 99146 : if (flag_vtable_verify)
5457 : : {
5458 : 12 : vtv_recover_class_info ();
5459 : 12 : vtv_compute_class_hierarchy_transitive_closure ();
5460 : 12 : vtv_build_vtable_verify_fndecl ();
5461 : : }
5462 : :
5463 : 99146 : perform_deferred_noexcept_checks ();
5464 : :
5465 : 99146 : fini_constexpr ();
5466 : 99146 : cp_tree_c_finish_parsing ();
5467 : 99146 : clear_consteval_vfns (consteval_vtables);
5468 : :
5469 : : /* The entire file is now complete. If requested, dump everything
5470 : : to a file. */
5471 : 99146 : dump_tu ();
5472 : :
5473 : 99146 : if (flag_detailed_statistics)
5474 : : {
5475 : 0 : dump_tree_statistics ();
5476 : 0 : dump_time_statistics ();
5477 : : }
5478 : :
5479 : 99146 : timevar_stop (TV_PHASE_DEFERRED);
5480 : 99146 : timevar_start (TV_PHASE_PARSING);
5481 : :
5482 : : /* Indicate that we're done with front end processing. */
5483 : 99146 : at_eof = 3;
5484 : 99146 : }
5485 : :
5486 : : /* Perform any post compilation-proper cleanups for the C++ front-end.
5487 : : This should really go away. No front-end should need to do
5488 : : anything past the compilation process. */
5489 : :
5490 : : void
5491 : 99040 : cxx_post_compilation_parsing_cleanups (void)
5492 : : {
5493 : 99040 : timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5494 : :
5495 : 99040 : if (flag_vtable_verify)
5496 : : {
5497 : : /* Generate the special constructor initialization function that
5498 : : calls __VLTRegisterPairs, and give it a very high
5499 : : initialization priority. This must be done after
5500 : : finalize_compilation_unit so that we have accurate
5501 : : information about which vtable will actually be emitted. */
5502 : 12 : vtv_generate_init_routine ();
5503 : : }
5504 : :
5505 : 99040 : input_location = locus_at_end_of_parsing;
5506 : :
5507 : 99040 : if (flag_checking)
5508 : 99028 : validate_conversion_obstack ();
5509 : :
5510 : 99040 : timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5511 : 99040 : }
5512 : :
5513 : : /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5514 : : function to call in parse-tree form; it has not yet been
5515 : : semantically analyzed. ARGS are the arguments to the function.
5516 : : They have already been semantically analyzed. This may change
5517 : : ARGS. */
5518 : :
5519 : : tree
5520 : 348632 : build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5521 : : tsubst_flags_t complain)
5522 : : {
5523 : 348632 : tree orig_fn;
5524 : 348632 : vec<tree, va_gc> *orig_args = NULL;
5525 : 348632 : tree expr;
5526 : 348632 : tree object;
5527 : :
5528 : 348632 : orig_fn = fn;
5529 : 348632 : object = TREE_OPERAND (fn, 0);
5530 : :
5531 : 348632 : if (processing_template_decl)
5532 : : {
5533 : 263118 : gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5534 : : || TREE_CODE (fn) == MEMBER_REF);
5535 : 263118 : if (type_dependent_expression_p (fn)
5536 : 263118 : || any_type_dependent_arguments_p (*args))
5537 : 263071 : return build_min_nt_call_vec (fn, *args);
5538 : :
5539 : 47 : orig_args = make_tree_vector_copy (*args);
5540 : :
5541 : : /* Transform the arguments and add the implicit "this"
5542 : : parameter. */
5543 : 47 : if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5544 : : {
5545 : 39 : if (TREE_CODE (fn) == DOTSTAR_EXPR)
5546 : 33 : object = cp_build_addr_expr (object, complain);
5547 : 39 : vec_safe_insert (*args, 0, object);
5548 : : }
5549 : : }
5550 : :
5551 : : /* A qualified name corresponding to a bound pointer-to-member is
5552 : : represented as an OFFSET_REF:
5553 : :
5554 : : struct B { void g(); };
5555 : : void (B::*p)();
5556 : : void B::g() { (this->*p)(); } */
5557 : 85561 : if (TREE_CODE (fn) == OFFSET_REF)
5558 : : {
5559 : 85514 : tree object_addr = cp_build_addr_expr (object, complain);
5560 : 85514 : fn = TREE_OPERAND (fn, 1);
5561 : 85514 : fn = get_member_function_from_ptrfunc (&object_addr, fn,
5562 : : complain);
5563 : 85514 : vec_safe_insert (*args, 0, object_addr);
5564 : : }
5565 : :
5566 : 85561 : if (CLASS_TYPE_P (TREE_TYPE (fn)))
5567 : 4 : expr = build_op_call (fn, args, complain);
5568 : : else
5569 : 85557 : expr = cp_build_function_call_vec (fn, args, complain);
5570 : 85561 : if (processing_template_decl && expr != error_mark_node)
5571 : 47 : expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5572 : :
5573 : 85561 : if (orig_args != NULL)
5574 : 47 : release_tree_vector (orig_args);
5575 : :
5576 : : return expr;
5577 : : }
5578 : :
5579 : :
5580 : : void
5581 : 342331155 : check_default_args (tree x)
5582 : : {
5583 : 342331155 : tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5584 : 342331155 : bool saw_def = false;
5585 : 342331155 : bool noted_first_def = false;
5586 : 342331155 : int idx_of_first_default_arg = 0;
5587 : 342331155 : location_t loc_of_first_default_arg = UNKNOWN_LOCATION;
5588 : 342331155 : int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5589 : 342331155 : tree fndecl = STRIP_TEMPLATE (x);
5590 : 342331155 : auto_diagnostic_group d;
5591 : 927625003 : for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5592 : : {
5593 : 585293848 : if (TREE_PURPOSE (arg))
5594 : : {
5595 : 8288849 : if (!saw_def)
5596 : : {
5597 : 6516735 : saw_def = true;
5598 : 6516735 : idx_of_first_default_arg = i;
5599 : 6516735 : location_t loc = get_fndecl_argument_location (fndecl, i);
5600 : 6516735 : if (loc != DECL_SOURCE_LOCATION (x))
5601 : 6516705 : loc_of_first_default_arg = loc;
5602 : : }
5603 : : }
5604 : 577004999 : else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5605 : : {
5606 : 196 : error_at (get_fndecl_argument_location (fndecl, i),
5607 : : "default argument missing for parameter %P of %q#D", i, x);
5608 : 196 : if (loc_of_first_default_arg != UNKNOWN_LOCATION
5609 : 196 : && !noted_first_def)
5610 : : {
5611 : 192 : inform (loc_of_first_default_arg,
5612 : : "...following parameter %P which has a default argument",
5613 : : idx_of_first_default_arg);
5614 : 192 : noted_first_def = true;
5615 : : }
5616 : 196 : TREE_PURPOSE (arg) = error_mark_node;
5617 : : }
5618 : : }
5619 : 342331155 : }
5620 : :
5621 : : /* Return true if function DECL can be inlined. This is used to force
5622 : : instantiation of methods that might be interesting for inlining. */
5623 : : bool
5624 : 3 : possibly_inlined_p (tree decl)
5625 : : {
5626 : 3 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5627 : 3 : if (DECL_UNINLINABLE (decl))
5628 : : return false;
5629 : 3 : if (!optimize)
5630 : 3 : return DECL_DECLARED_INLINE_P (decl);
5631 : : /* When optimizing, we might inline everything when flatten
5632 : : attribute or heuristics inlining for size or autoinlining
5633 : : is used. */
5634 : : return true;
5635 : : }
5636 : :
5637 : : /* If DECL is a function or variable template specialization, instantiate
5638 : : its definition now. */
5639 : :
5640 : : void
5641 : 83653408 : maybe_instantiate_decl (tree decl)
5642 : : {
5643 : 324606 : if (VAR_OR_FUNCTION_DECL_P (decl)
5644 : 83653408 : && DECL_LANG_SPECIFIC (decl)
5645 : 77856115 : && DECL_TEMPLATE_INFO (decl)
5646 : 62709856 : && !DECL_DECLARED_CONCEPT_P (decl)
5647 : 146363264 : && !uses_template_parms (DECL_TI_ARGS (decl)))
5648 : : {
5649 : : /* Instantiating a function will result in garbage collection. We
5650 : : must treat this situation as if we were within the body of a
5651 : : function so as to avoid collecting live data only referenced from
5652 : : the stack (such as overload resolution candidates). */
5653 : 58911422 : ++function_depth;
5654 : 58911422 : instantiate_decl (decl, /*defer_ok=*/false,
5655 : : /*expl_inst_class_mem_p=*/false);
5656 : 58900638 : --function_depth;
5657 : : }
5658 : 83642624 : }
5659 : :
5660 : : /* Error if the DECL is unavailable (unless this is currently suppressed).
5661 : : Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns true if
5662 : : an error or warning was emitted. */
5663 : :
5664 : : bool
5665 : 3190321331 : cp_handle_deprecated_or_unavailable (tree decl, tsubst_flags_t complain)
5666 : : {
5667 : 3190321331 : if (!decl)
5668 : : return false;
5669 : :
5670 : 3157607841 : if ((complain & tf_error)
5671 : 2944200471 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
5672 : : {
5673 : 2328968406 : if (TREE_UNAVAILABLE (decl))
5674 : : {
5675 : 308 : error_unavailable_use (decl, NULL_TREE);
5676 : 308 : return true;
5677 : : }
5678 : : else
5679 : : {
5680 : : /* Perhaps this is an unavailable typedef. */
5681 : 2328968098 : if (TYPE_P (decl)
5682 : 645850205 : && TYPE_NAME (decl)
5683 : 2971057253 : && TREE_UNAVAILABLE (TYPE_NAME (decl)))
5684 : : {
5685 : 8 : decl = TYPE_NAME (decl);
5686 : : /* Don't error within members of a unavailable type. */
5687 : 8 : if (TYPE_P (decl)
5688 : 8 : && currently_open_class (decl))
5689 : : return false;
5690 : :
5691 : 8 : error_unavailable_use (decl, NULL_TREE);
5692 : 8 : return true;
5693 : : }
5694 : : }
5695 : : /* Carry on to consider deprecatedness. */
5696 : : }
5697 : :
5698 : 3157607525 : if (!(complain & tf_warning)
5699 : 2922306285 : || deprecated_state == DEPRECATED_SUPPRESS
5700 : 2920883945 : || deprecated_state == UNAVAILABLE_DEPRECATED_SUPPRESS)
5701 : : return false;
5702 : :
5703 : 2309018800 : if (!TREE_DEPRECATED (decl))
5704 : : {
5705 : : /* Perhaps this is a deprecated typedef. */
5706 : 2308862461 : if (TYPE_P (decl) && TYPE_NAME (decl))
5707 : 641314150 : decl = TYPE_NAME (decl);
5708 : :
5709 : 2308862461 : if (!TREE_DEPRECATED (decl))
5710 : : return false;
5711 : : }
5712 : :
5713 : : /* Don't warn within members of a deprecated type. */
5714 : 156347 : if (TYPE_P (decl)
5715 : 156347 : && currently_open_class (decl))
5716 : : return false;
5717 : :
5718 : 34733 : bool warned = false;
5719 : 34733 : if (cxx_dialect >= cxx11
5720 : 34642 : && DECL_P (decl)
5721 : 34539 : && DECL_ARTIFICIAL (decl)
5722 : 10865 : && DECL_IOBJ_MEMBER_FUNCTION_P (decl)
5723 : 45598 : && copy_fn_p (decl))
5724 : : {
5725 : : /* Don't warn if the flag was disabled around the class definition
5726 : : (c++/94492). */
5727 : 10854 : if (warning_enabled_at (DECL_SOURCE_LOCATION (decl),
5728 : : OPT_Wdeprecated_copy))
5729 : : {
5730 : 6 : auto_diagnostic_group d;
5731 : 6 : tree ctx = DECL_CONTEXT (decl);
5732 : 6 : tree other = classtype_has_depr_implicit_copy (ctx);
5733 : 6 : int opt = (DECL_DESTRUCTOR_P (other)
5734 : 6 : ? OPT_Wdeprecated_copy_dtor
5735 : 6 : : OPT_Wdeprecated_copy);
5736 : 6 : warned = warning (opt, "implicitly-declared %qD is deprecated",
5737 : : decl);
5738 : 6 : if (warned)
5739 : 6 : inform (DECL_SOURCE_LOCATION (other),
5740 : : "because %qT has user-provided %qD",
5741 : : ctx, other);
5742 : 6 : }
5743 : : }
5744 : : else
5745 : 23879 : warned = warn_deprecated_use (decl, NULL_TREE);
5746 : :
5747 : : return warned;
5748 : : }
5749 : :
5750 : : /* Like above, but takes into account outer scopes. */
5751 : :
5752 : : void
5753 : 1265498489 : cp_warn_deprecated_use_scopes (tree scope)
5754 : : {
5755 : 1265498489 : while (scope
5756 : 2180628795 : && scope != error_mark_node
5757 : 4361257549 : && scope != global_namespace)
5758 : : {
5759 : 217822614 : if ((TREE_CODE (scope) == NAMESPACE_DECL || OVERLOAD_TYPE_P (scope))
5760 : 1119131631 : && cp_handle_deprecated_or_unavailable (scope))
5761 : : return;
5762 : 915130306 : if (TYPE_P (scope))
5763 : 205691280 : scope = CP_TYPE_CONTEXT (scope);
5764 : : else
5765 : 709439026 : scope = CP_DECL_CONTEXT (scope);
5766 : : }
5767 : : }
5768 : :
5769 : : /* True if DECL or its enclosing scope have unbound template parameters. */
5770 : :
5771 : : bool
5772 : 580874334 : decl_dependent_p (tree decl)
5773 : : {
5774 : 1153362834 : if (DECL_FUNCTION_SCOPE_P (decl)
5775 : 75928359 : || TREE_CODE (decl) == CONST_DECL
5776 : 49891086 : || TREE_CODE (decl) == USING_DECL
5777 : 630765412 : || TREE_CODE (decl) == FIELD_DECL)
5778 : 570657890 : decl = CP_DECL_CONTEXT (decl);
5779 : 580874334 : if (tree tinfo = get_template_info (decl))
5780 : 490927044 : if (any_dependent_template_arguments_p (TI_ARGS (tinfo)))
5781 : : return true;
5782 : 249287888 : if (LAMBDA_FUNCTION_P (decl)
5783 : 248138249 : && dependent_type_p (DECL_CONTEXT (decl)))
5784 : : return true;
5785 : : return false;
5786 : : }
5787 : :
5788 : : /* [basic.def.odr] A function is named [and therefore odr-used] by an
5789 : : expression or conversion if it is the selected member of an overload set in
5790 : : an overload resolution performed as part of forming that expression or
5791 : : conversion, unless it is a pure virtual function and either the expression
5792 : : is not an id-expression naming the function with an explicitly qualified
5793 : : name or the expression forms a pointer to member.
5794 : :
5795 : : Mostly, we call mark_used in places that actually do something with a
5796 : : function, like build_over_call. But in a few places we end up with a
5797 : : non-overloaded FUNCTION_DECL that we aren't going to do any more with, like
5798 : : convert_to_void. resolve_nondeduced_context is called in those places,
5799 : : but it's also called in too many other places. */
5800 : :
5801 : : bool
5802 : 294854043 : mark_single_function (tree expr, tsubst_flags_t complain)
5803 : : {
5804 : 294854043 : expr = maybe_undo_parenthesized_ref (expr);
5805 : 294854043 : expr = tree_strip_any_location_wrapper (expr);
5806 : :
5807 : 294854043 : if (is_overloaded_fn (expr) == 1
5808 : 83874019 : && !mark_used (expr, complain)
5809 : 294854061 : && !(complain & tf_error))
5810 : : return false;
5811 : : return true;
5812 : : }
5813 : :
5814 : : /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5815 : : If DECL is a specialization or implicitly declared class member,
5816 : : generate the actual definition. Return false if something goes
5817 : : wrong, true otherwise. */
5818 : :
5819 : : bool
5820 : 978806306 : mark_used (tree decl, tsubst_flags_t complain /* = tf_warning_or_error */)
5821 : : {
5822 : : /* If we're just testing conversions or resolving overloads, we
5823 : : don't want any permanent effects like forcing functions to be
5824 : : output or instantiating templates. */
5825 : 978806306 : if ((complain & tf_conv))
5826 : : return true;
5827 : :
5828 : : /* If DECL is a BASELINK for a single function, then treat it just
5829 : : like the DECL for the function. Otherwise, if the BASELINK is
5830 : : for an overloaded function, we don't know which function was
5831 : : actually used until after overload resolution. */
5832 : 977072260 : if (BASELINK_P (decl))
5833 : : {
5834 : 13225 : tree fns = BASELINK_FUNCTIONS (decl);
5835 : 13225 : if (really_overloaded_fn (fns))
5836 : : return true;
5837 : 13206 : fns = OVL_FIRST (fns);
5838 : 13206 : if (!mark_used (fns, complain))
5839 : : return false;
5840 : : /* We might have deduced its return type. */
5841 : 13204 : TREE_TYPE (decl) = TREE_TYPE (fns);
5842 : 13204 : return true;
5843 : : }
5844 : :
5845 : 977059035 : if (!DECL_P (decl))
5846 : : return true;
5847 : :
5848 : : /* Set TREE_USED for the benefit of -Wunused. */
5849 : 976217151 : TREE_USED (decl) = true;
5850 : :
5851 : : /* And for structured bindings also the underlying decl. */
5852 : 976217151 : if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5853 : 252581 : TREE_USED (DECL_DECOMP_BASE (decl)) = true;
5854 : :
5855 : 976217151 : if (TREE_CODE (decl) == TEMPLATE_DECL)
5856 : : return true;
5857 : :
5858 : 976207219 : if (DECL_CLONED_FUNCTION_P (decl))
5859 : 15270323 : TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5860 : :
5861 : : /* Mark enumeration types as used. */
5862 : 976207219 : if (TREE_CODE (decl) == CONST_DECL)
5863 : 26037273 : used_types_insert (DECL_CONTEXT (decl));
5864 : :
5865 : 976207219 : if (TREE_CODE (decl) == FUNCTION_DECL)
5866 : : {
5867 : 205974618 : if (DECL_MAYBE_DELETED (decl))
5868 : : {
5869 : 6865 : ++function_depth;
5870 : 6865 : maybe_synthesize_method (decl);
5871 : 6865 : --function_depth;
5872 : : }
5873 : :
5874 : 205974618 : if (DECL_DELETED_FN (decl))
5875 : : {
5876 : 35702 : if (DECL_ARTIFICIAL (decl)
5877 : 3294 : && DECL_CONV_FN_P (decl)
5878 : 35714 : && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5879 : : /* We mark a lambda conversion op as deleted if we can't
5880 : : generate it properly; see maybe_add_lambda_conv_op. */
5881 : 6 : sorry ("converting lambda that uses %<...%> to function pointer");
5882 : 35696 : else if (complain & tf_error)
5883 : : {
5884 : 2195 : error ("use of deleted function %qD", decl);
5885 : 2195 : if (!maybe_explain_implicit_delete (decl))
5886 : 493 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
5887 : : }
5888 : 35702 : return false;
5889 : : }
5890 : :
5891 : 205938916 : if (!maybe_instantiate_noexcept (decl, complain))
5892 : : return false;
5893 : : }
5894 : :
5895 : 976171421 : if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl))
5896 : : {
5897 : 50754 : if (!DECL_LANG_SPECIFIC (decl))
5898 : : /* An unresolved dependent local extern. */
5899 : : return true;
5900 : :
5901 : 50754 : DECL_ODR_USED (decl) = 1;
5902 : 50754 : auto alias = DECL_LOCAL_DECL_ALIAS (decl);
5903 : 50754 : if (!alias || alias == error_mark_node)
5904 : : return true;
5905 : :
5906 : : /* Process the underlying decl. */
5907 : 50456 : decl = alias;
5908 : 50456 : TREE_USED (decl) = true;
5909 : : }
5910 : :
5911 : 976171123 : cp_handle_deprecated_or_unavailable (decl, complain);
5912 : :
5913 : : /* We can only check DECL_ODR_USED on variables or functions with
5914 : : DECL_LANG_SPECIFIC set, and these are also the only decls that we
5915 : : might need special handling for. */
5916 : 566870804 : if (!VAR_OR_FUNCTION_DECL_P (decl)
5917 : 615239116 : || DECL_LANG_SPECIFIC (decl) == NULL
5918 : 1371478216 : || DECL_THUNK_P (decl))
5919 : : {
5920 : 580874334 : if (!decl_dependent_p (decl)
5921 : 580874334 : && !require_deduced_type (decl, complain))
5922 : : return false;
5923 : 580874311 : return true;
5924 : : }
5925 : :
5926 : : /* We only want to do this processing once. We don't need to keep trying
5927 : : to instantiate inline templates, because unit-at-a-time will make sure
5928 : : we get them compiled before functions that want to inline them. */
5929 : 395296789 : if (DECL_ODR_USED (decl))
5930 : : return true;
5931 : :
5932 : 60500264 : if (flag_concepts && TREE_CODE (decl) == FUNCTION_DECL
5933 : 161351398 : && !constraints_satisfied_p (decl))
5934 : : {
5935 : 10 : if (complain & tf_error)
5936 : : {
5937 : 3 : auto_diagnostic_group d;
5938 : 3 : error ("use of function %qD with unsatisfied constraints",
5939 : : decl);
5940 : 3 : location_t loc = DECL_SOURCE_LOCATION (decl);
5941 : 3 : inform (loc, "declared here");
5942 : 3 : diagnose_constraints (loc, decl, NULL_TREE);
5943 : 3 : }
5944 : 10 : return false;
5945 : : }
5946 : :
5947 : : /* If DECL has a deduced return type, we need to instantiate it now to
5948 : : find out its type. For OpenMP user defined reductions, we need them
5949 : : instantiated for reduction clauses which inline them by hand directly. */
5950 : 116266697 : if (undeduced_auto_decl (decl)
5951 : 116266697 : || (TREE_CODE (decl) == FUNCTION_DECL
5952 : 80505134 : && DECL_OMP_DECLARE_REDUCTION_P (decl)))
5953 : 500437 : maybe_instantiate_decl (decl);
5954 : :
5955 : 116266697 : if (processing_template_decl || in_template_context)
5956 : : return true;
5957 : :
5958 : : /* Check this too in case we're within instantiate_non_dependent_expr. */
5959 : 80061077 : if (DECL_TEMPLATE_INFO (decl)
5960 : 80061077 : && uses_template_parms (DECL_TI_ARGS (decl)))
5961 : : return true;
5962 : :
5963 : 80061071 : if (!require_deduced_type (decl, complain))
5964 : : return false;
5965 : :
5966 : 80060930 : if (builtin_pack_fn_p (decl))
5967 : : {
5968 : 12 : error ("use of built-in parameter pack %qD outside of a template",
5969 : 6 : DECL_NAME (decl));
5970 : 6 : return false;
5971 : : }
5972 : :
5973 : : /* If we don't need a value, then we don't need to synthesize DECL. */
5974 : 80060924 : if (cp_unevaluated_operand || in_discarded_stmt)
5975 : : return true;
5976 : :
5977 : 35216152 : DECL_ODR_USED (decl) = 1;
5978 : 35216152 : if (DECL_CLONED_FUNCTION_P (decl))
5979 : 5834959 : DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5980 : :
5981 : : /* DR 757: A type without linkage shall not be used as the type of a
5982 : : variable or function with linkage, unless
5983 : : o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5984 : : o the variable or function is not used (3.2 [basic.def.odr]) or is
5985 : : defined in the same translation unit. */
5986 : 35216152 : if (cxx_dialect > cxx98
5987 : 35028951 : && decl_linkage (decl) != lk_none
5988 : 32715684 : && !DECL_EXTERN_C_P (decl)
5989 : 28409522 : && !DECL_ARTIFICIAL (decl)
5990 : 26933052 : && !decl_defined_p (decl)
5991 : 55867728 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5992 : 319909 : vec_safe_push (no_linkage_decls, decl);
5993 : :
5994 : 35216152 : if (TREE_CODE (decl) == FUNCTION_DECL
5995 : 29404311 : && DECL_DECLARED_INLINE_P (decl)
5996 : 20997377 : && !DECL_INITIAL (decl)
5997 : 14150161 : && !DECL_ARTIFICIAL (decl)
5998 : 48858737 : && !DECL_PURE_VIRTUAL_P (decl))
5999 : : /* Remember it, so we can check it was defined. */
6000 : 13638589 : note_vague_linkage_fn (decl);
6001 : :
6002 : : /* Is it a synthesized method that needs to be synthesized? */
6003 : 35216152 : if (TREE_CODE (decl) == FUNCTION_DECL
6004 : 29404311 : && DECL_DEFAULTED_FN (decl)
6005 : : /* A function defaulted outside the class is synthesized either by
6006 : : cp_finish_decl or instantiate_decl. */
6007 : 1217511 : && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
6008 : 36433439 : && ! DECL_INITIAL (decl))
6009 : : {
6010 : : /* Remember the current location for a function we will end up
6011 : : synthesizing. Then we can inform the user where it was
6012 : : required in the case of error. */
6013 : 1040649 : if (decl_remember_implicit_trigger_p (decl))
6014 : 476197 : DECL_SOURCE_LOCATION (decl) = input_location;
6015 : :
6016 : : /* Synthesizing an implicitly defined member function will result in
6017 : : garbage collection. We must treat this situation as if we were
6018 : : within the body of a function so as to avoid collecting live data
6019 : : on the stack (such as overload resolution candidates).
6020 : :
6021 : : We could just let c_parse_final_cleanups handle synthesizing
6022 : : this function by adding it to deferred_fns, but doing
6023 : : it at the use site produces better error messages. */
6024 : 1040649 : ++function_depth;
6025 : 1040649 : synthesize_method (decl);
6026 : 1040649 : --function_depth;
6027 : : /* If this is a synthesized method we don't need to
6028 : : do the instantiation test below. */
6029 : : }
6030 : 28363662 : else if (VAR_OR_FUNCTION_DECL_P (decl)
6031 : 34175503 : && DECL_TEMPLATE_INFO (decl)
6032 : 19254937 : && !DECL_DECLARED_CONCEPT_P (decl)
6033 : 53430440 : && (!DECL_EXPLICIT_INSTANTIATION (decl)
6034 : 1009247 : || always_instantiate_p (decl)))
6035 : : /* If this is a function or variable that is an instance of some
6036 : : template, we now know that we will need to actually do the
6037 : : instantiation. We check that DECL is not an explicit
6038 : : instantiation because that is not checked in instantiate_decl.
6039 : :
6040 : : We put off instantiating functions in order to improve compile
6041 : : times. Maintaining a stack of active functions is expensive,
6042 : : and the inliner knows to instantiate any functions it might
6043 : : need. Therefore, we always try to defer instantiation. */
6044 : : {
6045 : 19137366 : ++function_depth;
6046 : 19137366 : instantiate_decl (decl, /*defer_ok=*/true,
6047 : : /*expl_inst_class_mem_p=*/false);
6048 : 19133746 : --function_depth;
6049 : : }
6050 : :
6051 : : return true;
6052 : : }
6053 : :
6054 : : tree
6055 : 12 : vtv_start_verification_constructor_init_function (void)
6056 : : {
6057 : 12 : return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, true);
6058 : : }
6059 : :
6060 : : tree
6061 : 8 : vtv_finish_verification_constructor_init_function (tree body)
6062 : : {
6063 : 8 : return finish_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, body);
6064 : : }
6065 : :
6066 : : #include "gt-cp-decl2.h"
|