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