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