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