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