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