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