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