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