comparison gcc/cp/decl2.c @ 111:04ced10e8804

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