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