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