Update gcc-50 to SVN version 221572
[dragonfly.git] / contrib / gcc-5.0 / gcc / cp / decl2.c
CommitLineData
dda118e3
JM
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
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21
22/* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26/* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29#include "config.h"
30#include "system.h"
31#include "coretypes.h"
32#include "tm.h"
33#include "hash-set.h"
34#include "machmode.h"
35#include "vec.h"
36#include "double-int.h"
37#include "input.h"
38#include "alias.h"
39#include "symtab.h"
40#include "wide-int.h"
41#include "inchash.h"
42#include "tree.h"
43#include "stringpool.h"
44#include "varasm.h"
45#include "attribs.h"
46#include "stor-layout.h"
47#include "calls.h"
48#include "flags.h"
49#include "cp-tree.h"
50#include "decl.h"
51#include "toplev.h"
52#include "timevar.h"
53#include "cpplib.h"
54#include "target.h"
55#include "c-family/c-common.h"
56#include "c-family/c-objc.h"
57#include "hash-map.h"
58#include "is-a.h"
59#include "plugin-api.h"
60#include "hard-reg-set.h"
61#include "input.h"
62#include "function.h"
63#include "ipa-ref.h"
64#include "cgraph.h"
65#include "tree-inline.h"
66#include "c-family/c-pragma.h"
67#include "dumpfile.h"
68#include "intl.h"
69#include "splay-tree.h"
70#include "langhooks.h"
71#include "c-family/c-ada-spec.h"
72#include "asan.h"
73
74extern cpp_reader *parse_in;
75
76/* This structure contains information about the initializations
77 and/or destructions required for a particular priority level. */
78typedef struct priority_info_s {
79 /* Nonzero if there have been any initializations at this priority
80 throughout the translation unit. */
81 int initializations_p;
82 /* Nonzero if there have been any destructions at this priority
83 throughout the translation unit. */
84 int destructions_p;
85} *priority_info;
86
87static void mark_vtable_entries (tree);
88static bool maybe_emit_vtables (tree);
89static bool acceptable_java_type (tree);
90static tree start_objects (int, int);
91static void finish_objects (int, int, tree);
92static tree start_static_storage_duration_function (unsigned);
93static void finish_static_storage_duration_function (tree);
94static priority_info get_priority_info (int);
95static void do_static_initialization_or_destruction (tree, bool);
96static void one_static_initialization_or_destruction (tree, tree, bool);
97static void generate_ctor_or_dtor_function (bool, int, location_t *);
98static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
99 void *);
100static tree prune_vars_needing_no_initialization (tree *);
101static void write_out_vars (tree);
102static void import_export_class (tree);
103static tree get_guard_bits (tree);
104static void determine_visibility_from_class (tree, tree);
105static bool determine_hidden_inline (tree);
106static bool decl_defined_p (tree);
107
108/* A list of static class variables. This is needed, because a
109 static class variable can be declared inside the class without
110 an initializer, and then initialized, statically, outside the class. */
111static GTY(()) vec<tree, va_gc> *pending_statics;
112
113/* A list of functions which were declared inline, but which we
114 may need to emit outline anyway. */
115static GTY(()) vec<tree, va_gc> *deferred_fns;
116
117/* A list of decls that use types with no linkage, which we need to make
118 sure are defined. */
119static GTY(()) vec<tree, va_gc> *no_linkage_decls;
120
121/* Nonzero if we're done parsing and into end-of-file activities. */
122
123int at_eof;
124\f
125
126/* Return a member function type (a METHOD_TYPE), given FNTYPE (a
127 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
128 that apply to the function). */
129
130tree
131build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
132 cp_ref_qualifier rqual)
133{
134 tree raises;
135 tree attrs;
136 int type_quals;
137 bool late_return_type_p;
138
139 if (fntype == error_mark_node || ctype == error_mark_node)
140 return error_mark_node;
141
142 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
143 || TREE_CODE (fntype) == METHOD_TYPE);
144
145 type_quals = quals & ~TYPE_QUAL_RESTRICT;
146 ctype = cp_build_qualified_type (ctype, type_quals);
147 raises = TYPE_RAISES_EXCEPTIONS (fntype);
148 attrs = TYPE_ATTRIBUTES (fntype);
149 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
150 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
151 (TREE_CODE (fntype) == METHOD_TYPE
152 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
153 : TYPE_ARG_TYPES (fntype)));
154 if (attrs)
155 fntype = cp_build_type_attribute_variant (fntype, attrs);
156 if (rqual)
157 fntype = build_ref_qualified_type (fntype, rqual);
158 if (raises)
159 fntype = build_exception_variant (fntype, raises);
160 if (late_return_type_p)
161 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
162
163 return fntype;
164}
165
166/* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
167 return type changed to NEW_RET. */
168
169tree
170change_return_type (tree new_ret, tree fntype)
171{
172 tree newtype;
173 tree args = TYPE_ARG_TYPES (fntype);
174 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
175 tree attrs = TYPE_ATTRIBUTES (fntype);
176 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
177
178 if (new_ret == error_mark_node)
179 return fntype;
180
181 if (same_type_p (new_ret, TREE_TYPE (fntype)))
182 return fntype;
183
184 if (TREE_CODE (fntype) == FUNCTION_TYPE)
185 {
186 newtype = build_function_type (new_ret, args);
187 newtype = apply_memfn_quals (newtype,
188 type_memfn_quals (fntype),
189 type_memfn_rqual (fntype));
190 }
191 else
192 newtype = build_method_type_directly
193 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
194 if (raises)
195 newtype = build_exception_variant (newtype, raises);
196 if (attrs)
197 newtype = cp_build_type_attribute_variant (newtype, attrs);
198 if (late_return_type_p)
199 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
200
201 return newtype;
202}
203
204/* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
205 appropriately. */
206
207tree
208cp_build_parm_decl (tree name, tree type)
209{
210 tree parm = build_decl (input_location,
211 PARM_DECL, name, type);
212 /* DECL_ARG_TYPE is only used by the back end and the back end never
213 sees templates. */
214 if (!processing_template_decl)
215 DECL_ARG_TYPE (parm) = type_passed_as (type);
216
217 return parm;
218}
219
220/* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
221 indicated NAME. */
222
223tree
224build_artificial_parm (tree name, tree type)
225{
226 tree parm = cp_build_parm_decl (name, type);
227 DECL_ARTIFICIAL (parm) = 1;
228 /* All our artificial parms are implicitly `const'; they cannot be
229 assigned to. */
230 TREE_READONLY (parm) = 1;
231 return parm;
232}
233
234/* Constructors for types with virtual baseclasses need an "in-charge" flag
235 saying whether this constructor is responsible for initialization of
236 virtual baseclasses or not. All destructors also need this "in-charge"
237 flag, which additionally determines whether or not the destructor should
238 free the memory for the object.
239
240 This function adds the "in-charge" flag to member function FN if
241 appropriate. It is called from grokclassfn and tsubst.
242 FN must be either a constructor or destructor.
243
244 The in-charge flag follows the 'this' parameter, and is followed by the
245 VTT parm (if any), then the user-written parms. */
246
247void
248maybe_retrofit_in_chrg (tree fn)
249{
250 tree basetype, arg_types, parms, parm, fntype;
251
252 /* If we've already add the in-charge parameter don't do it again. */
253 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
254 return;
255
256 /* When processing templates we can't know, in general, whether or
257 not we're going to have virtual baseclasses. */
258 if (processing_template_decl)
259 return;
260
261 /* We don't need an in-charge parameter for constructors that don't
262 have virtual bases. */
263 if (DECL_CONSTRUCTOR_P (fn)
264 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
265 return;
266
267 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
268 basetype = TREE_TYPE (TREE_VALUE (arg_types));
269 arg_types = TREE_CHAIN (arg_types);
270
271 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
272
273 /* If this is a subobject constructor or destructor, our caller will
274 pass us a pointer to our VTT. */
275 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
276 {
277 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
278
279 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
280 DECL_CHAIN (parm) = parms;
281 parms = parm;
282
283 /* ...and then to TYPE_ARG_TYPES. */
284 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
285
286 DECL_HAS_VTT_PARM_P (fn) = 1;
287 }
288
289 /* Then add the in-charge parm (before the VTT parm). */
290 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
291 DECL_CHAIN (parm) = parms;
292 parms = parm;
293 arg_types = hash_tree_chain (integer_type_node, arg_types);
294
295 /* Insert our new parameter(s) into the list. */
296 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
297
298 /* And rebuild the function type. */
299 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
300 arg_types);
301 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
302 fntype = build_exception_variant (fntype,
303 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
304 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
305 fntype = (cp_build_type_attribute_variant
306 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
307 TREE_TYPE (fn) = fntype;
308
309 /* Now we've got the in-charge parameter. */
310 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
311}
312
313/* Classes overload their constituent function names automatically.
314 When a function name is declared in a record structure,
315 its name is changed to it overloaded name. Since names for
316 constructors and destructors can conflict, we place a leading
317 '$' for destructors.
318
319 CNAME is the name of the class we are grokking for.
320
321 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
322
323 FLAGS contains bits saying what's special about today's
324 arguments. DTOR_FLAG == DESTRUCTOR.
325
326 If FUNCTION is a destructor, then we must add the `auto-delete' field
327 as a second parameter. There is some hair associated with the fact
328 that we must "declare" this variable in the manner consistent with the
329 way the rest of the arguments were declared.
330
331 QUALS are the qualifiers for the this pointer. */
332
333void
334grokclassfn (tree ctype, tree function, enum overload_flags flags)
335{
336 tree fn_name = DECL_NAME (function);
337
338 /* Even within an `extern "C"' block, members get C++ linkage. See
339 [dcl.link] for details. */
340 SET_DECL_LANGUAGE (function, lang_cplusplus);
341
342 if (fn_name == NULL_TREE)
343 {
344 error ("name missing for member function");
345 fn_name = get_identifier ("<anonymous>");
346 DECL_NAME (function) = fn_name;
347 }
348
349 DECL_CONTEXT (function) = ctype;
350
351 if (flags == DTOR_FLAG)
352 DECL_DESTRUCTOR_P (function) = 1;
353
354 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
355 maybe_retrofit_in_chrg (function);
356}
357
358/* Create an ARRAY_REF, checking for the user doing things backwards
359 along the way. DECLTYPE_P is for N3276, as in the parser. */
360
361tree
362grok_array_decl (location_t loc, tree array_expr, tree index_exp,
363 bool decltype_p)
364{
365 tree type;
366 tree expr;
367 tree orig_array_expr = array_expr;
368 tree orig_index_exp = index_exp;
369
370 if (error_operand_p (array_expr) || error_operand_p (index_exp))
371 return error_mark_node;
372
373 if (processing_template_decl)
374 {
375 if (type_dependent_expression_p (array_expr)
376 || type_dependent_expression_p (index_exp))
377 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
378 NULL_TREE, NULL_TREE);
379 array_expr = build_non_dependent_expr (array_expr);
380 index_exp = build_non_dependent_expr (index_exp);
381 }
382
383 type = TREE_TYPE (array_expr);
384 gcc_assert (type);
385 type = non_reference (type);
386
387 /* If they have an `operator[]', use that. */
388 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
389 {
390 tsubst_flags_t complain = tf_warning_or_error;
391 if (decltype_p)
392 complain |= tf_decltype;
393 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
394 index_exp, NULL_TREE, /*overload=*/NULL, complain);
395 }
396 else
397 {
398 tree p1, p2, i1, i2;
399
400 /* Otherwise, create an ARRAY_REF for a pointer or array type.
401 It is a little-known fact that, if `a' is an array and `i' is
402 an int, you can write `i[a]', which means the same thing as
403 `a[i]'. */
404 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
405 p1 = array_expr;
406 else
407 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
408
409 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
410 p2 = index_exp;
411 else
412 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
413
414 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
415 false);
416 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
417 false);
418
419 if ((p1 && i2) && (i1 && p2))
420 error ("ambiguous conversion for array subscript");
421
422 if (p1 && i2)
423 array_expr = p1, index_exp = i2;
424 else if (i1 && p2)
425 array_expr = p2, index_exp = i1;
426 else
427 {
428 error ("invalid types %<%T[%T]%> for array subscript",
429 type, TREE_TYPE (index_exp));
430 return error_mark_node;
431 }
432
433 if (array_expr == error_mark_node || index_exp == error_mark_node)
434 error ("ambiguous conversion for array subscript");
435
436 expr = build_array_ref (input_location, array_expr, index_exp);
437 }
438 if (processing_template_decl && expr != error_mark_node)
439 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
440 NULL_TREE, NULL_TREE);
441 return expr;
442}
443
444/* Given the cast expression EXP, checking out its validity. Either return
445 an error_mark_node if there was an unavoidable error, return a cast to
446 void for trying to delete a pointer w/ the value 0, or return the
447 call to delete. If DOING_VEC is true, we handle things differently
448 for doing an array delete.
449 Implements ARM $5.3.4. This is called from the parser. */
450
451tree
452delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
453 tsubst_flags_t complain)
454{
455 tree t, type;
456
457 if (exp == error_mark_node)
458 return exp;
459
460 if (processing_template_decl)
461 {
462 t = build_min (DELETE_EXPR, void_type_node, exp, size);
463 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
464 DELETE_EXPR_USE_VEC (t) = doing_vec;
465 TREE_SIDE_EFFECTS (t) = 1;
466 return t;
467 }
468
469 /* An array can't have been allocated by new, so complain. */
470 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
471 warning (0, "deleting array %q#E", exp);
472
473 t = build_expr_type_conversion (WANT_POINTER, exp, true);
474
475 if (t == NULL_TREE || t == error_mark_node)
476 {
477 error ("type %q#T argument given to %<delete%>, expected pointer",
478 TREE_TYPE (exp));
479 return error_mark_node;
480 }
481
482 type = TREE_TYPE (t);
483
484 /* As of Valley Forge, you can delete a pointer to const. */
485
486 /* You can't delete functions. */
487 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
488 {
489 error ("cannot delete a function. Only pointer-to-objects are "
490 "valid arguments to %<delete%>");
491 return error_mark_node;
492 }
493
494 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
495 if (VOID_TYPE_P (TREE_TYPE (type)))
496 {
497 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
498 doing_vec = 0;
499 }
500
501 /* Deleting a pointer with the value zero is valid and has no effect. */
502 if (integer_zerop (t))
503 return build1 (NOP_EXPR, void_type_node, t);
504
505 if (doing_vec)
506 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
507 sfk_deleting_destructor,
508 use_global_delete, complain);
509 else
510 return build_delete (type, t, sfk_deleting_destructor,
511 LOOKUP_NORMAL, use_global_delete,
512 complain);
513}
514
515/* Report an error if the indicated template declaration is not the
516 sort of thing that should be a member template. */
517
518void
519check_member_template (tree tmpl)
520{
521 tree decl;
522
523 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
524 decl = DECL_TEMPLATE_RESULT (tmpl);
525
526 if (TREE_CODE (decl) == FUNCTION_DECL
527 || DECL_ALIAS_TEMPLATE_P (tmpl)
528 || (TREE_CODE (decl) == TYPE_DECL
529 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
530 {
531 /* The parser rejects template declarations in local classes
532 (with the exception of generic lambdas). */
533 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
534 /* The parser rejects any use of virtual in a function template. */
535 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
536 && DECL_VIRTUAL_P (decl)));
537
538 /* The debug-information generating code doesn't know what to do
539 with member templates. */
540 DECL_IGNORED_P (tmpl) = 1;
541 }
542 else if (variable_template_p (tmpl))
543 /* OK */;
544 else
545 error ("template declaration of %q#D", decl);
546}
547
548/* Return true iff TYPE is a valid Java parameter or return type. */
549
550static bool
551acceptable_java_type (tree type)
552{
553 if (type == error_mark_node)
554 return false;
555
556 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
557 return true;
558 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
559 {
560 type = TREE_TYPE (type);
561 if (TREE_CODE (type) == RECORD_TYPE)
562 {
563 tree args; int i;
564 if (! TYPE_FOR_JAVA (type))
565 return false;
566 if (! CLASSTYPE_TEMPLATE_INFO (type))
567 return true;
568 args = CLASSTYPE_TI_ARGS (type);
569 i = TREE_VEC_LENGTH (args);
570 while (--i >= 0)
571 {
572 type = TREE_VEC_ELT (args, i);
573 if (TYPE_PTR_P (type))
574 type = TREE_TYPE (type);
575 if (! TYPE_FOR_JAVA (type))
576 return false;
577 }
578 return true;
579 }
580 }
581 return false;
582}
583
584/* For a METHOD in a Java class CTYPE, return true if
585 the parameter and return types are valid Java types.
586 Otherwise, print appropriate error messages, and return false. */
587
588bool
589check_java_method (tree method)
590{
591 bool jerr = false;
592 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
593 tree ret_type = TREE_TYPE (TREE_TYPE (method));
594
595 if (!acceptable_java_type (ret_type))
596 {
597 error ("Java method %qD has non-Java return type %qT",
598 method, ret_type);
599 jerr = true;
600 }
601
602 arg_types = TREE_CHAIN (arg_types);
603 if (DECL_HAS_IN_CHARGE_PARM_P (method))
604 arg_types = TREE_CHAIN (arg_types);
605 if (DECL_HAS_VTT_PARM_P (method))
606 arg_types = TREE_CHAIN (arg_types);
607
608 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
609 {
610 tree type = TREE_VALUE (arg_types);
611 if (!acceptable_java_type (type))
612 {
613 if (type != error_mark_node)
614 error ("Java method %qD has non-Java parameter type %qT",
615 method, type);
616 jerr = true;
617 }
618 }
619 return !jerr;
620}
621
622/* Sanity check: report error if this function FUNCTION is not
623 really a member of the class (CTYPE) it is supposed to belong to.
624 TEMPLATE_PARMS is used to specify the template parameters of a member
625 template passed as FUNCTION_DECL. If the member template is passed as a
626 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
627 from the declaration. If the function is not a function template, it
628 must be NULL.
629 It returns the original declaration for the function, NULL_TREE if
630 no declaration was found, error_mark_node if an error was emitted. */
631
632tree
633check_classfn (tree ctype, tree function, tree template_parms)
634{
635 int ix;
636 bool is_template;
637 tree pushed_scope;
638
639 if (DECL_USE_TEMPLATE (function)
640 && !(TREE_CODE (function) == TEMPLATE_DECL
641 && DECL_TEMPLATE_SPECIALIZATION (function))
642 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
643 /* Since this is a specialization of a member template,
644 we're not going to find the declaration in the class.
645 For example, in:
646
647 struct S { template <typename T> void f(T); };
648 template <> void S::f(int);
649
650 we're not going to find `S::f(int)', but there's no
651 reason we should, either. We let our callers know we didn't
652 find the method, but we don't complain. */
653 return NULL_TREE;
654
655 /* Basic sanity check: for a template function, the template parameters
656 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
657 if (TREE_CODE (function) == TEMPLATE_DECL)
658 {
659 if (template_parms
660 && !comp_template_parms (template_parms,
661 DECL_TEMPLATE_PARMS (function)))
662 {
663 error ("template parameter lists provided don%'t match the "
664 "template parameters of %qD", function);
665 return error_mark_node;
666 }
667 template_parms = DECL_TEMPLATE_PARMS (function);
668 }
669
670 /* OK, is this a definition of a member template? */
671 is_template = (template_parms != NULL_TREE);
672
673 /* [temp.mem]
674
675 A destructor shall not be a member template. */
676 if (DECL_DESTRUCTOR_P (function) && is_template)
677 {
678 error ("destructor %qD declared as member template", function);
679 return error_mark_node;
680 }
681
682 /* We must enter the scope here, because conversion operators are
683 named by target type, and type equivalence relies on typenames
684 resolving within the scope of CTYPE. */
685 pushed_scope = push_scope (ctype);
686 ix = class_method_index_for_fn (complete_type (ctype), function);
687 if (ix >= 0)
688 {
689 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
690 tree fndecls, fndecl = 0;
691 bool is_conv_op;
692 const char *format = NULL;
693
694 for (fndecls = (*methods)[ix];
695 fndecls; fndecls = OVL_NEXT (fndecls))
696 {
697 tree p1, p2;
698
699 fndecl = OVL_CURRENT (fndecls);
700 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
701 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
702
703 /* We cannot simply call decls_match because this doesn't
704 work for static member functions that are pretending to
705 be methods, and because the name may have been changed by
706 asm("new_name"). */
707
708 /* Get rid of the this parameter on functions that become
709 static. */
710 if (DECL_STATIC_FUNCTION_P (fndecl)
711 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
712 p1 = TREE_CHAIN (p1);
713
714 /* A member template definition only matches a member template
715 declaration. */
716 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
717 continue;
718
719 /* ref-qualifier or absence of same must match. */
720 if (type_memfn_rqual (TREE_TYPE (function))
721 != type_memfn_rqual (TREE_TYPE (fndecl)))
722 continue;
723
724 /* While finding a match, same types and params are not enough
725 if the function is versioned. Also check version ("target")
726 attributes. */
727 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
728 TREE_TYPE (TREE_TYPE (fndecl)))
729 && compparms (p1, p2)
730 && !targetm.target_option.function_versions (function, fndecl)
731 && (!is_template
732 || comp_template_parms (template_parms,
733 DECL_TEMPLATE_PARMS (fndecl)))
734 && (DECL_TEMPLATE_SPECIALIZATION (function)
735 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
736 && (!DECL_TEMPLATE_SPECIALIZATION (function)
737 || (DECL_TI_TEMPLATE (function)
738 == DECL_TI_TEMPLATE (fndecl))))
739 break;
740 }
741 if (fndecls)
742 {
743 if (pushed_scope)
744 pop_scope (pushed_scope);
745 return OVL_CURRENT (fndecls);
746 }
747
748 error_at (DECL_SOURCE_LOCATION (function),
749 "prototype for %q#D does not match any in class %qT",
750 function, ctype);
751 is_conv_op = DECL_CONV_FN_P (fndecl);
752
753 if (is_conv_op)
754 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
755 fndecls = (*methods)[ix];
756 while (fndecls)
757 {
758 fndecl = OVL_CURRENT (fndecls);
759 fndecls = OVL_NEXT (fndecls);
760
761 if (!fndecls && is_conv_op)
762 {
763 if (methods->length () > (size_t) ++ix)
764 {
765 fndecls = (*methods)[ix];
766 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
767 {
768 fndecls = NULL_TREE;
769 is_conv_op = false;
770 }
771 }
772 else
773 is_conv_op = false;
774 }
775 if (format)
776 format = " %+#D";
777 else if (fndecls)
778 format = N_("candidates are: %+#D");
779 else
780 format = N_("candidate is: %+#D");
781 error (format, fndecl);
782 }
783 }
784 else if (!COMPLETE_TYPE_P (ctype))
785 cxx_incomplete_type_error (function, ctype);
786 else
787 error ("no %q#D member function declared in class %qT",
788 function, ctype);
789
790 if (pushed_scope)
791 pop_scope (pushed_scope);
792 return error_mark_node;
793}
794
795/* DECL is a function with vague linkage. Remember it so that at the
796 end of the translation unit we can decide whether or not to emit
797 it. */
798
799void
800note_vague_linkage_fn (tree decl)
801{
802 DECL_DEFER_OUTPUT (decl) = 1;
803 vec_safe_push (deferred_fns, decl);
804}
805
806/* As above, but for variable template instantiations. */
807
808void
809note_variable_template_instantiation (tree decl)
810{
811 vec_safe_push (pending_statics, decl);
812}
813
814/* We have just processed the DECL, which is a static data member.
815 The other parameters are as for cp_finish_decl. */
816
817void
818finish_static_data_member_decl (tree decl,
819 tree init, bool init_const_expr_p,
820 tree asmspec_tree,
821 int flags)
822{
823 DECL_CONTEXT (decl) = current_class_type;
824
825 /* We cannot call pushdecl here, because that would fill in the
826 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
827 the right thing, namely, to put this decl out straight away. */
828
829 if (! processing_template_decl)
830 vec_safe_push (pending_statics, decl);
831
832 if (LOCAL_CLASS_P (current_class_type)
833 /* We already complained about the template definition. */
834 && !DECL_TEMPLATE_INSTANTIATION (decl))
835 permerror (input_location, "local class %q#T shall not have static data member %q#D",
836 current_class_type, decl);
837 else
838 for (tree t = current_class_type; TYPE_P (t);
839 t = CP_TYPE_CONTEXT (t))
840 if (TYPE_ANONYMOUS_P (t))
841 {
842 if (permerror (DECL_SOURCE_LOCATION (decl),
843 "static data member %qD in unnamed class", decl))
844 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
845 "unnamed class defined here");
846 break;
847 }
848
849 DECL_IN_AGGR_P (decl) = 1;
850
851 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
852 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
853 SET_VAR_HAD_UNKNOWN_BOUND (decl);
854
855 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
856}
857
858/* DECLARATOR and DECLSPECS correspond to a class member. The other
859 parameters are as for cp_finish_decl. Return the DECL for the
860 class member declared. */
861
862tree
863grokfield (const cp_declarator *declarator,
864 cp_decl_specifier_seq *declspecs,
865 tree init, bool init_const_expr_p,
866 tree asmspec_tree,
867 tree attrlist)
868{
869 tree value;
870 const char *asmspec = 0;
871 int flags;
872 tree name;
873
874 if (init
875 && TREE_CODE (init) == TREE_LIST
876 && TREE_VALUE (init) == error_mark_node
877 && TREE_CHAIN (init) == NULL_TREE)
878 init = NULL_TREE;
879
880 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
881 if (! value || value == error_mark_node)
882 /* friend or constructor went bad. */
883 return error_mark_node;
884 if (TREE_TYPE (value) == error_mark_node)
885 return value;
886
887 if (TREE_CODE (value) == TYPE_DECL && init)
888 {
889 error ("typedef %qD is initialized (use decltype instead)", value);
890 init = NULL_TREE;
891 }
892
893 /* Pass friendly classes back. */
894 if (value == void_type_node)
895 return value;
896
897
898 name = DECL_NAME (value);
899
900 if (name != NULL_TREE)
901 {
902 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
903 {
904 error ("explicit template argument list not allowed");
905 return error_mark_node;
906 }
907
908 if (IDENTIFIER_POINTER (name)[0] == '_'
909 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
910 error ("member %qD conflicts with virtual function table field name",
911 value);
912 }
913
914 /* Stash away type declarations. */
915 if (TREE_CODE (value) == TYPE_DECL)
916 {
917 DECL_NONLOCAL (value) = 1;
918 DECL_CONTEXT (value) = current_class_type;
919
920 if (attrlist)
921 {
922 int attrflags = 0;
923
924 /* If this is a typedef that names the class for linkage purposes
925 (7.1.3p8), apply any attributes directly to the type. */
926 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
927 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
928 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
929
930 cplus_decl_attributes (&value, attrlist, attrflags);
931 }
932
933 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
934 && TREE_TYPE (value) != error_mark_node
935 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
936 set_underlying_type (value);
937
938 /* It's important that push_template_decl below follows
939 set_underlying_type above so that the created template
940 carries the properly set type of VALUE. */
941 if (processing_template_decl)
942 value = push_template_decl (value);
943
944 record_locally_defined_typedef (value);
945 return value;
946 }
947
948 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
949
950 if (!friendp && DECL_IN_AGGR_P (value))
951 {
952 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
953 return void_type_node;
954 }
955
956 if (asmspec_tree && asmspec_tree != error_mark_node)
957 asmspec = TREE_STRING_POINTER (asmspec_tree);
958
959 if (init)
960 {
961 if (TREE_CODE (value) == FUNCTION_DECL)
962 {
963 if (init == ridpointers[(int)RID_DELETE])
964 {
965 DECL_DELETED_FN (value) = 1;
966 DECL_DECLARED_INLINE_P (value) = 1;
967 DECL_INITIAL (value) = error_mark_node;
968 }
969 else if (init == ridpointers[(int)RID_DEFAULT])
970 {
971 if (defaultable_fn_check (value))
972 {
973 DECL_DEFAULTED_FN (value) = 1;
974 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
975 DECL_DECLARED_INLINE_P (value) = 1;
976 }
977 }
978 else if (TREE_CODE (init) == DEFAULT_ARG)
979 error ("invalid initializer for member function %qD", value);
980 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
981 {
982 if (integer_zerop (init))
983 DECL_PURE_VIRTUAL_P (value) = 1;
984 else if (error_operand_p (init))
985 ; /* An error has already been reported. */
986 else
987 error ("invalid initializer for member function %qD",
988 value);
989 }
990 else
991 {
992 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
993 if (friendp)
994 error ("initializer specified for friend function %qD",
995 value);
996 else
997 error ("initializer specified for static member function %qD",
998 value);
999 }
1000 }
1001 else if (TREE_CODE (value) == FIELD_DECL)
1002 /* C++11 NSDMI, keep going. */;
1003 else if (!VAR_P (value))
1004 gcc_unreachable ();
1005 }
1006
1007 /* Pass friend decls back. */
1008 if ((TREE_CODE (value) == FUNCTION_DECL
1009 || TREE_CODE (value) == TEMPLATE_DECL)
1010 && DECL_CONTEXT (value) != current_class_type)
1011 return value;
1012
1013 /* Need to set this before push_template_decl. */
1014 if (TREE_CODE (value) == VAR_DECL)
1015 DECL_CONTEXT (value) = current_class_type;
1016
1017 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1018 {
1019 value = push_template_decl (value);
1020 if (error_operand_p (value))
1021 return error_mark_node;
1022 }
1023
1024 if (attrlist)
1025 cplus_decl_attributes (&value, attrlist, 0);
1026
1027 if (init && DIRECT_LIST_INIT_P (init))
1028 flags = LOOKUP_NORMAL;
1029 else
1030 flags = LOOKUP_IMPLICIT;
1031
1032 switch (TREE_CODE (value))
1033 {
1034 case VAR_DECL:
1035 finish_static_data_member_decl (value, init, init_const_expr_p,
1036 asmspec_tree, flags);
1037 return value;
1038
1039 case FIELD_DECL:
1040 if (asmspec)
1041 error ("%<asm%> specifiers are not permitted on non-static data members");
1042 if (DECL_INITIAL (value) == error_mark_node)
1043 init = error_mark_node;
1044 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1045 NULL_TREE, flags);
1046 DECL_IN_AGGR_P (value) = 1;
1047 return value;
1048
1049 case FUNCTION_DECL:
1050 if (asmspec)
1051 set_user_assembler_name (value, asmspec);
1052
1053 cp_finish_decl (value,
1054 /*init=*/NULL_TREE,
1055 /*init_const_expr_p=*/false,
1056 asmspec_tree, flags);
1057
1058 /* Pass friends back this way. */
1059 if (DECL_FRIEND_P (value))
1060 return void_type_node;
1061
1062 DECL_IN_AGGR_P (value) = 1;
1063 return value;
1064
1065 default:
1066 gcc_unreachable ();
1067 }
1068 return NULL_TREE;
1069}
1070
1071/* Like `grokfield', but for bitfields.
1072 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1073
1074tree
1075grokbitfield (const cp_declarator *declarator,
1076 cp_decl_specifier_seq *declspecs, tree width,
1077 tree attrlist)
1078{
1079 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1080
1081 if (value == error_mark_node)
1082 return NULL_TREE; /* friends went bad. */
1083 if (TREE_TYPE (value) == error_mark_node)
1084 return value;
1085
1086 /* Pass friendly classes back. */
1087 if (VOID_TYPE_P (value))
1088 return void_type_node;
1089
1090 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1091 && (POINTER_TYPE_P (value)
1092 || !dependent_type_p (TREE_TYPE (value))))
1093 {
1094 error ("bit-field %qD with non-integral type", value);
1095 return error_mark_node;
1096 }
1097
1098 if (TREE_CODE (value) == TYPE_DECL)
1099 {
1100 error ("cannot declare %qD to be a bit-field type", value);
1101 return NULL_TREE;
1102 }
1103
1104 /* Usually, finish_struct_1 catches bitfields with invalid types.
1105 But, in the case of bitfields with function type, we confuse
1106 ourselves into thinking they are member functions, so we must
1107 check here. */
1108 if (TREE_CODE (value) == FUNCTION_DECL)
1109 {
1110 error ("cannot declare bit-field %qD with function type",
1111 DECL_NAME (value));
1112 return NULL_TREE;
1113 }
1114
1115 if (DECL_IN_AGGR_P (value))
1116 {
1117 error ("%qD is already defined in the class %qT", value,
1118 DECL_CONTEXT (value));
1119 return void_type_node;
1120 }
1121
1122 if (TREE_STATIC (value))
1123 {
1124 error ("static member %qD cannot be a bit-field", value);
1125 return NULL_TREE;
1126 }
1127 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1128
1129 if (width != error_mark_node)
1130 {
1131 /* The width must be an integer type. */
1132 if (!type_dependent_expression_p (width)
1133 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1134 error ("width of bit-field %qD has non-integral type %qT", value,
1135 TREE_TYPE (width));
1136 DECL_INITIAL (value) = width;
1137 SET_DECL_C_BIT_FIELD (value);
1138 }
1139
1140 DECL_IN_AGGR_P (value) = 1;
1141
1142 if (attrlist)
1143 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1144
1145 return value;
1146}
1147
1148\f
1149/* Returns true iff ATTR is an attribute which needs to be applied at
1150 instantiation time rather than template definition time. */
1151
1152static bool
1153is_late_template_attribute (tree attr, tree decl)
1154{
1155 tree name = get_attribute_name (attr);
1156 tree args = TREE_VALUE (attr);
1157 const struct attribute_spec *spec = lookup_attribute_spec (name);
1158 tree arg;
1159
1160 if (!spec)
1161 /* Unknown attribute. */
1162 return false;
1163
1164 /* Attribute weak handling wants to write out assembly right away. */
1165 if (is_attribute_p ("weak", name))
1166 return true;
1167
1168 /* Attribute unused is applied directly, as it appertains to
1169 decls. */
1170 if (is_attribute_p ("unused", name))
1171 return false;
1172
1173 /* #pragma omp declare simd attribute needs to be always deferred. */
1174 if (flag_openmp
1175 && is_attribute_p ("omp declare simd", name))
1176 return true;
1177
1178 /* If any of the arguments are dependent expressions, we can't evaluate
1179 the attribute until instantiation time. */
1180 for (arg = args; arg; arg = TREE_CHAIN (arg))
1181 {
1182 tree t = TREE_VALUE (arg);
1183
1184 /* If the first attribute argument is an identifier, only consider
1185 second and following arguments. Attributes like mode, format,
1186 cleanup and several target specific attributes aren't late
1187 just because they have an IDENTIFIER_NODE as first argument. */
1188 if (arg == args && identifier_p (t))
1189 continue;
1190
1191 if (value_dependent_expression_p (t)
1192 || type_dependent_expression_p (t))
1193 return true;
1194 }
1195
1196 if (TREE_CODE (decl) == TYPE_DECL
1197 || TYPE_P (decl)
1198 || spec->type_required)
1199 {
1200 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1201
1202 /* We can't apply any attributes to a completely unknown type until
1203 instantiation time. */
1204 enum tree_code code = TREE_CODE (type);
1205 if (code == TEMPLATE_TYPE_PARM
1206 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1207 || code == TYPENAME_TYPE)
1208 return true;
1209 /* Also defer most attributes on dependent types. This is not
1210 necessary in all cases, but is the better default. */
1211 else if (dependent_type_p (type)
1212 /* But some attributes specifically apply to templates. */
1213 && !is_attribute_p ("abi_tag", name)
1214 && !is_attribute_p ("deprecated", name)
1215 && !is_attribute_p ("visibility", name))
1216 return true;
1217 else
1218 return false;
1219 }
1220 else
1221 return false;
1222}
1223
1224/* ATTR_P is a list of attributes. Remove any attributes which need to be
1225 applied at instantiation time and return them. If IS_DEPENDENT is true,
1226 the declaration itself is dependent, so all attributes should be applied
1227 at instantiation time. */
1228
1229static tree
1230splice_template_attributes (tree *attr_p, tree decl)
1231{
1232 tree *p = attr_p;
1233 tree late_attrs = NULL_TREE;
1234 tree *q = &late_attrs;
1235
1236 if (!p)
1237 return NULL_TREE;
1238
1239 for (; *p; )
1240 {
1241 if (is_late_template_attribute (*p, decl))
1242 {
1243 ATTR_IS_DEPENDENT (*p) = 1;
1244 *q = *p;
1245 *p = TREE_CHAIN (*p);
1246 q = &TREE_CHAIN (*q);
1247 *q = NULL_TREE;
1248 }
1249 else
1250 p = &TREE_CHAIN (*p);
1251 }
1252
1253 return late_attrs;
1254}
1255
1256/* Remove any late attributes from the list in ATTR_P and attach them to
1257 DECL_P. */
1258
1259static void
1260save_template_attributes (tree *attr_p, tree *decl_p)
1261{
1262 tree *q;
1263
1264 if (attr_p && *attr_p == error_mark_node)
1265 return;
1266
1267 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1268 if (!late_attrs)
1269 return;
1270
1271 if (DECL_P (*decl_p))
1272 q = &DECL_ATTRIBUTES (*decl_p);
1273 else
1274 q = &TYPE_ATTRIBUTES (*decl_p);
1275
1276 tree old_attrs = *q;
1277
1278 /* Merge the late attributes at the beginning with the attribute
1279 list. */
1280 late_attrs = merge_attributes (late_attrs, *q);
1281 *q = late_attrs;
1282
1283 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1284 {
1285 /* We've added new attributes directly to the main variant, so
1286 now we need to update all of the other variants to include
1287 these new attributes. */
1288 tree variant;
1289 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1290 variant = TYPE_NEXT_VARIANT (variant))
1291 {
1292 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1293 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1294 }
1295 }
1296}
1297
1298/* Return true iff ATTRS are acceptable attributes to be applied in-place
1299 to a typedef which gives a previously anonymous class or enum a name for
1300 linkage purposes. */
1301
1302bool
1303attributes_naming_typedef_ok (tree attrs)
1304{
1305 for (; attrs; attrs = TREE_CHAIN (attrs))
1306 {
1307 tree name = get_attribute_name (attrs);
1308 if (is_attribute_p ("vector_size", name))
1309 return false;
1310 }
1311 return true;
1312}
1313
1314/* Like reconstruct_complex_type, but handle also template trees. */
1315
1316tree
1317cp_reconstruct_complex_type (tree type, tree bottom)
1318{
1319 tree inner, outer;
1320 bool late_return_type_p = false;
1321
1322 if (TYPE_PTR_P (type))
1323 {
1324 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1325 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1326 TYPE_REF_CAN_ALIAS_ALL (type));
1327 }
1328 else if (TREE_CODE (type) == REFERENCE_TYPE)
1329 {
1330 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1331 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1332 TYPE_REF_CAN_ALIAS_ALL (type));
1333 }
1334 else if (TREE_CODE (type) == ARRAY_TYPE)
1335 {
1336 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1337 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1338 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1339 element type qualification will be handled by the recursive
1340 cp_reconstruct_complex_type call and cp_build_qualified_type
1341 for ARRAY_TYPEs changes the element type. */
1342 return outer;
1343 }
1344 else if (TREE_CODE (type) == FUNCTION_TYPE)
1345 {
1346 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1347 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1348 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1349 outer = apply_memfn_quals (outer,
1350 type_memfn_quals (type),
1351 type_memfn_rqual (type));
1352 }
1353 else if (TREE_CODE (type) == METHOD_TYPE)
1354 {
1355 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1356 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1357 /* The build_method_type_directly() routine prepends 'this' to argument list,
1358 so we must compensate by getting rid of it. */
1359 outer
1360 = build_method_type_directly
1361 (class_of_this_parm (type), inner,
1362 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1363 }
1364 else if (TREE_CODE (type) == OFFSET_TYPE)
1365 {
1366 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1367 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1368 }
1369 else
1370 return bottom;
1371
1372 if (TYPE_ATTRIBUTES (type))
1373 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1374 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1375
1376 if (late_return_type_p)
1377 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1378
1379 return outer;
1380}
1381
1382/* Replaces any constexpr expression that may be into the attributes
1383 arguments with their reduced value. */
1384
1385static void
1386cp_check_const_attributes (tree attributes)
1387{
1388 if (attributes == error_mark_node)
1389 return;
1390
1391 tree attr;
1392 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1393 {
1394 tree arg;
1395 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1396 {
1397 tree expr = TREE_VALUE (arg);
1398 if (EXPR_P (expr))
1399 TREE_VALUE (arg) = maybe_constant_value (expr);
1400 }
1401 }
1402}
1403
1404/* Return true if TYPE is an OpenMP mappable type. */
1405bool
1406cp_omp_mappable_type (tree type)
1407{
1408 /* Mappable type has to be complete. */
1409 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1410 return false;
1411 /* Arrays have mappable type if the elements have mappable type. */
1412 while (TREE_CODE (type) == ARRAY_TYPE)
1413 type = TREE_TYPE (type);
1414 /* A mappable type cannot contain virtual members. */
1415 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1416 return false;
1417 /* All data members must be non-static. */
1418 if (CLASS_TYPE_P (type))
1419 {
1420 tree field;
1421 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1422 if (TREE_CODE (field) == VAR_DECL)
1423 return false;
1424 /* All fields must have mappable types. */
1425 else if (TREE_CODE (field) == FIELD_DECL
1426 && !cp_omp_mappable_type (TREE_TYPE (field)))
1427 return false;
1428 }
1429 return true;
1430}
1431
1432/* Like decl_attributes, but handle C++ complexity. */
1433
1434void
1435cplus_decl_attributes (tree *decl, tree attributes, int flags)
1436{
1437 if (*decl == NULL_TREE || *decl == void_type_node
1438 || *decl == error_mark_node)
1439 return;
1440
1441 /* Add implicit "omp declare target" attribute if requested. */
1442 if (scope_chain->omp_declare_target_attribute
38c0c85b
JM
1443 && ((TREE_CODE (*decl) == VAR_DECL
1444 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
dda118e3
JM
1445 || TREE_CODE (*decl) == FUNCTION_DECL))
1446 {
1447 if (TREE_CODE (*decl) == VAR_DECL
1448 && DECL_CLASS_SCOPE_P (*decl))
1449 error ("%q+D static data member inside of declare target directive",
1450 *decl);
1451 else if (TREE_CODE (*decl) == VAR_DECL
1452 && (DECL_FUNCTION_SCOPE_P (*decl)
1453 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1454 error ("%q+D in block scope inside of declare target directive",
1455 *decl);
1456 else if (!processing_template_decl
1457 && TREE_CODE (*decl) == VAR_DECL
1458 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1459 error ("%q+D in declare target directive does not have mappable type",
1460 *decl);
1461 else
1462 attributes = tree_cons (get_identifier ("omp declare target"),
1463 NULL_TREE, attributes);
1464 }
1465
1466 if (processing_template_decl)
1467 {
1468 if (check_for_bare_parameter_packs (attributes))
1469 return;
1470
1471 save_template_attributes (&attributes, decl);
1472 }
1473
1474 cp_check_const_attributes (attributes);
1475
1476 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1477 decl = &DECL_TEMPLATE_RESULT (*decl);
1478
1479 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1480 {
1481 attributes
1482 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1483 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1484 attributes, flags);
1485 }
1486 else
1487 decl_attributes (decl, attributes, flags);
1488
1489 if (TREE_CODE (*decl) == TYPE_DECL)
1490 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1491
1492 /* Propagate deprecation out to the template. */
1493 if (TREE_DEPRECATED (*decl))
1494 if (tree ti = get_template_info (*decl))
1495 {
1496 tree tmpl = TI_TEMPLATE (ti);
1497 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1498 : DECL_TEMPLATE_RESULT (tmpl));
1499 if (*decl == pattern)
1500 TREE_DEPRECATED (tmpl) = true;
1501 }
1502}
1503\f
1504/* Walks through the namespace- or function-scope anonymous union
1505 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1506 Returns one of the fields for use in the mangled name. */
1507
1508static tree
1509build_anon_union_vars (tree type, tree object)
1510{
1511 tree main_decl = NULL_TREE;
1512 tree field;
1513
1514 /* Rather than write the code to handle the non-union case,
1515 just give an error. */
1516 if (TREE_CODE (type) != UNION_TYPE)
1517 {
1518 error ("anonymous struct not inside named type");
1519 return error_mark_node;
1520 }
1521
1522 for (field = TYPE_FIELDS (type);
1523 field != NULL_TREE;
1524 field = DECL_CHAIN (field))
1525 {
1526 tree decl;
1527 tree ref;
1528
1529 if (DECL_ARTIFICIAL (field))
1530 continue;
1531 if (TREE_CODE (field) != FIELD_DECL)
1532 {
1533 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1534 "have non-static data members", field);
1535 continue;
1536 }
1537
1538 if (TREE_PRIVATE (field))
1539 permerror (input_location, "private member %q+#D in anonymous union", field);
1540 else if (TREE_PROTECTED (field))
1541 permerror (input_location, "protected member %q+#D in anonymous union", field);
1542
1543 if (processing_template_decl)
1544 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1545 DECL_NAME (field), NULL_TREE);
1546 else
1547 ref = build_class_member_access_expr (object, field, NULL_TREE,
1548 false, tf_warning_or_error);
1549
1550 if (DECL_NAME (field))
1551 {
1552 tree base;
1553
1554 decl = build_decl (input_location,
1555 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1556 DECL_ANON_UNION_VAR_P (decl) = 1;
1557 DECL_ARTIFICIAL (decl) = 1;
1558
1559 base = get_base_address (object);
1560 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1561 TREE_STATIC (decl) = TREE_STATIC (base);
1562 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1563
1564 SET_DECL_VALUE_EXPR (decl, ref);
1565 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1566
1567 decl = pushdecl (decl);
1568 }
1569 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1570 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1571 else
1572 decl = 0;
1573
1574 if (main_decl == NULL_TREE)
1575 main_decl = decl;
1576 }
1577
1578 return main_decl;
1579}
1580
1581/* Finish off the processing of a UNION_TYPE structure. If the union is an
1582 anonymous union, then all members must be laid out together. PUBLIC_P
1583 is nonzero if this union is not declared static. */
1584
1585void
1586finish_anon_union (tree anon_union_decl)
1587{
1588 tree type;
1589 tree main_decl;
1590 bool public_p;
1591
1592 if (anon_union_decl == error_mark_node)
1593 return;
1594
1595 type = TREE_TYPE (anon_union_decl);
1596 public_p = TREE_PUBLIC (anon_union_decl);
1597
1598 /* The VAR_DECL's context is the same as the TYPE's context. */
1599 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1600
1601 if (TYPE_FIELDS (type) == NULL_TREE)
1602 return;
1603
1604 if (public_p)
1605 {
1606 error ("namespace-scope anonymous aggregates must be static");
1607 return;
1608 }
1609
1610 main_decl = build_anon_union_vars (type, anon_union_decl);
1611 if (main_decl == error_mark_node)
1612 return;
1613 if (main_decl == NULL_TREE)
1614 {
1615 warning (0, "anonymous union with no members");
1616 return;
1617 }
1618
1619 if (!processing_template_decl)
1620 {
1621 /* Use main_decl to set the mangled name. */
1622 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1623 maybe_commonize_var (anon_union_decl);
1624 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1625 mangle_decl (anon_union_decl);
1626 DECL_NAME (anon_union_decl) = NULL_TREE;
1627 }
1628
1629 pushdecl (anon_union_decl);
1630 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1631}
1632\f
1633/* Auxiliary functions to make type signatures for
1634 `operator new' and `operator delete' correspond to
1635 what compiler will be expecting. */
1636
1637tree
1638coerce_new_type (tree type)
1639{
1640 int e = 0;
1641 tree args = TYPE_ARG_TYPES (type);
1642
1643 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1644
1645 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1646 {
1647 e = 1;
1648 error ("%<operator new%> must return type %qT", ptr_type_node);
1649 }
1650
1651 if (args && args != void_list_node)
1652 {
1653 if (TREE_PURPOSE (args))
1654 {
1655 /* [basic.stc.dynamic.allocation]
1656
1657 The first parameter shall not have an associated default
1658 argument. */
1659 error ("the first parameter of %<operator new%> cannot "
1660 "have a default argument");
1661 /* Throw away the default argument. */
1662 TREE_PURPOSE (args) = NULL_TREE;
1663 }
1664
1665 if (!same_type_p (TREE_VALUE (args), size_type_node))
1666 {
1667 e = 2;
1668 args = TREE_CHAIN (args);
1669 }
1670 }
1671 else
1672 e = 2;
1673
1674 if (e == 2)
1675 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1676 "as first parameter", size_type_node);
1677
1678 switch (e)
1679 {
1680 case 2:
1681 args = tree_cons (NULL_TREE, size_type_node, args);
1682 /* Fall through. */
1683 case 1:
1684 type = build_exception_variant
1685 (build_function_type (ptr_type_node, args),
1686 TYPE_RAISES_EXCEPTIONS (type));
1687 /* Fall through. */
1688 default:;
1689 }
1690 return type;
1691}
1692
1693tree
1694coerce_delete_type (tree type)
1695{
1696 int e = 0;
1697 tree args = TYPE_ARG_TYPES (type);
1698
1699 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1700
1701 if (!same_type_p (TREE_TYPE (type), void_type_node))
1702 {
1703 e = 1;
1704 error ("%<operator delete%> must return type %qT", void_type_node);
1705 }
1706
1707 if (!args || args == void_list_node
1708 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1709 {
1710 e = 2;
1711 if (args && args != void_list_node)
1712 args = TREE_CHAIN (args);
1713 error ("%<operator delete%> takes type %qT as first parameter",
1714 ptr_type_node);
1715 }
1716 switch (e)
1717 {
1718 case 2:
1719 args = tree_cons (NULL_TREE, ptr_type_node, args);
1720 /* Fall through. */
1721 case 1:
1722 type = build_exception_variant
1723 (build_function_type (void_type_node, args),
1724 TYPE_RAISES_EXCEPTIONS (type));
1725 /* Fall through. */
1726 default:;
1727 }
1728
1729 return type;
1730}
1731\f
1732/* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1733 and mark them as needed. */
1734
1735static void
1736mark_vtable_entries (tree decl)
1737{
1738 tree fnaddr;
1739 unsigned HOST_WIDE_INT idx;
1740
1741 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1742 idx, fnaddr)
1743 {
1744 tree fn;
1745
1746 STRIP_NOPS (fnaddr);
1747
1748 if (TREE_CODE (fnaddr) != ADDR_EXPR
1749 && TREE_CODE (fnaddr) != FDESC_EXPR)
1750 /* This entry is an offset: a virtual base class offset, a
1751 virtual call offset, an RTTI offset, etc. */
1752 continue;
1753
1754 fn = TREE_OPERAND (fnaddr, 0);
1755 TREE_ADDRESSABLE (fn) = 1;
1756 /* When we don't have vcall offsets, we output thunks whenever
1757 we output the vtables that contain them. With vcall offsets,
1758 we know all the thunks we'll need when we emit a virtual
1759 function, so we emit the thunks there instead. */
1760 if (DECL_THUNK_P (fn))
1761 use_thunk (fn, /*emit_p=*/0);
1762 mark_used (fn);
1763 }
1764}
1765
1766/* Set DECL up to have the closest approximation of "initialized common"
1767 linkage available. */
1768
1769void
1770comdat_linkage (tree decl)
1771{
1772 if (flag_weak)
1773 make_decl_one_only (decl, cxx_comdat_group (decl));
1774 else if (TREE_CODE (decl) == FUNCTION_DECL
1775 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1776 /* We can just emit function and compiler-generated variables
1777 statically; having multiple copies is (for the most part) only
1778 a waste of space.
1779
1780 There are two correctness issues, however: the address of a
1781 template instantiation with external linkage should be the
1782 same, independent of what translation unit asks for the
1783 address, and this will not hold when we emit multiple copies of
1784 the function. However, there's little else we can do.
1785
1786 Also, by default, the typeinfo implementation assumes that
1787 there will be only one copy of the string used as the name for
1788 each type. Therefore, if weak symbols are unavailable, the
1789 run-time library should perform a more conservative check; it
1790 should perform a string comparison, rather than an address
1791 comparison. */
1792 TREE_PUBLIC (decl) = 0;
1793 else
1794 {
1795 /* Static data member template instantiations, however, cannot
1796 have multiple copies. */
1797 if (DECL_INITIAL (decl) == 0
1798 || DECL_INITIAL (decl) == error_mark_node)
1799 DECL_COMMON (decl) = 1;
1800 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1801 {
1802 DECL_COMMON (decl) = 1;
1803 DECL_INITIAL (decl) = error_mark_node;
1804 }
1805 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1806 {
1807 /* We can't do anything useful; leave vars for explicit
1808 instantiation. */
1809 DECL_EXTERNAL (decl) = 1;
1810 DECL_NOT_REALLY_EXTERN (decl) = 0;
1811 }
1812 }
1813
1814 DECL_COMDAT (decl) = 1;
1815}
1816
1817/* For win32 we also want to put explicit instantiations in
1818 linkonce sections, so that they will be merged with implicit
1819 instantiations; otherwise we get duplicate symbol errors.
1820 For Darwin we do not want explicit instantiations to be
1821 linkonce. */
1822
1823void
1824maybe_make_one_only (tree decl)
1825{
1826 /* We used to say that this was not necessary on targets that support weak
1827 symbols, because the implicit instantiations will defer to the explicit
1828 one. However, that's not actually the case in SVR4; a strong definition
1829 after a weak one is an error. Also, not making explicit
1830 instantiations one_only means that we can end up with two copies of
1831 some template instantiations. */
1832 if (! flag_weak)
1833 return;
1834
1835 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1836 we can get away with not emitting them if they aren't used. We need
1837 to for variables so that cp_finish_decl will update their linkage,
1838 because their DECL_INITIAL may not have been set properly yet. */
1839
1840 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1841 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1842 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1843 {
1844 make_decl_one_only (decl, cxx_comdat_group (decl));
1845
1846 if (VAR_P (decl))
1847 {
1848 varpool_node *node = varpool_node::get_create (decl);
1849 DECL_COMDAT (decl) = 1;
1850 /* Mark it needed so we don't forget to emit it. */
1851 node->forced_by_abi = true;
1852 TREE_USED (decl) = 1;
1853 }
1854 }
1855}
1856
1857/* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1858 This predicate will give the right answer during parsing of the
1859 function, which other tests may not. */
1860
1861bool
1862vague_linkage_p (tree decl)
1863{
1864 /* Unfortunately, import_export_decl has not always been called
1865 before the function is processed, so we cannot simply check
1866 DECL_COMDAT. */
1867 if (DECL_COMDAT (decl)
1868 || (((TREE_CODE (decl) == FUNCTION_DECL
1869 && DECL_DECLARED_INLINE_P (decl))
1870 || (DECL_LANG_SPECIFIC (decl)
1871 && DECL_TEMPLATE_INSTANTIATION (decl)))
1872 && TREE_PUBLIC (decl)))
1873 return true;
1874 else if (DECL_FUNCTION_SCOPE_P (decl))
1875 /* A local static in an inline effectively has vague linkage. */
1876 return (TREE_STATIC (decl)
1877 && vague_linkage_p (DECL_CONTEXT (decl)));
1878 else
1879 return false;
1880}
1881
1882/* Determine whether or not we want to specifically import or export CTYPE,
1883 using various heuristics. */
1884
1885static void
1886import_export_class (tree ctype)
1887{
1888 /* -1 for imported, 1 for exported. */
1889 int import_export = 0;
1890
1891 /* It only makes sense to call this function at EOF. The reason is
1892 that this function looks at whether or not the first non-inline
1893 non-abstract virtual member function has been defined in this
1894 translation unit. But, we can't possibly know that until we've
1895 seen the entire translation unit. */
1896 gcc_assert (at_eof);
1897
1898 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1899 return;
1900
1901 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1902 we will have CLASSTYPE_INTERFACE_ONLY set but not
1903 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1904 heuristic because someone will supply a #pragma implementation
1905 elsewhere, and deducing it here would produce a conflict. */
1906 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1907 return;
1908
1909 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1910 import_export = -1;
1911 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1912 import_export = 1;
1913 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1914 && !flag_implicit_templates)
1915 /* For a template class, without -fimplicit-templates, check the
1916 repository. If the virtual table is assigned to this
1917 translation unit, then export the class; otherwise, import
1918 it. */
1919 import_export = repo_export_class_p (ctype) ? 1 : -1;
1920 else if (TYPE_POLYMORPHIC_P (ctype))
1921 {
1922 /* The ABI specifies that the virtual table and associated
1923 information are emitted with the key method, if any. */
1924 tree method = CLASSTYPE_KEY_METHOD (ctype);
1925 /* If weak symbol support is not available, then we must be
1926 careful not to emit the vtable when the key function is
1927 inline. An inline function can be defined in multiple
1928 translation units. If we were to emit the vtable in each
1929 translation unit containing a definition, we would get
1930 multiple definition errors at link-time. */
1931 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1932 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1933 }
1934
1935 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1936 a definition anywhere else. */
1937 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1938 import_export = 0;
1939
1940 /* Allow back ends the chance to overrule the decision. */
1941 if (targetm.cxx.import_export_class)
1942 import_export = targetm.cxx.import_export_class (ctype, import_export);
1943
1944 if (import_export)
1945 {
1946 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1947 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1948 }
1949}
1950
1951/* Return true if VAR has already been provided to the back end; in that
1952 case VAR should not be modified further by the front end. */
1953static bool
1954var_finalized_p (tree var)
1955{
1956 return varpool_node::get_create (var)->definition;
1957}
1958
1959/* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1960 must be emitted in this translation unit. Mark it as such. */
1961
1962void
1963mark_needed (tree decl)
1964{
1965 TREE_USED (decl) = 1;
1966 if (TREE_CODE (decl) == FUNCTION_DECL)
1967 {
1968 /* Extern inline functions don't become needed when referenced.
1969 If we know a method will be emitted in other TU and no new
1970 functions can be marked reachable, just use the external
1971 definition. */
1972 struct cgraph_node *node = cgraph_node::get_create (decl);
1973 node->forced_by_abi = true;
1974
1975 /* #pragma interface and -frepo code can call mark_needed for
1976 maybe-in-charge 'tors; mark the clones as well. */
1977 tree clone;
1978 FOR_EACH_CLONE (clone, decl)
1979 mark_needed (clone);
1980 }
1981 else if (TREE_CODE (decl) == VAR_DECL)
1982 {
1983 varpool_node *node = varpool_node::get_create (decl);
1984 /* C++ frontend use mark_decl_references to force COMDAT variables
1985 to be output that might appear dead otherwise. */
1986 node->forced_by_abi = true;
1987 }
1988}
1989
1990/* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1991 returns true if a definition of this entity should be provided in
1992 this object file. Callers use this function to determine whether
1993 or not to let the back end know that a definition of DECL is
1994 available in this translation unit. */
1995
1996bool
1997decl_needed_p (tree decl)
1998{
1999 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2000 /* This function should only be called at the end of the translation
2001 unit. We cannot be sure of whether or not something will be
2002 COMDAT until that point. */
2003 gcc_assert (at_eof);
2004
2005 /* All entities with external linkage that are not COMDAT/EXTERN should be
2006 emitted; they may be referred to from other object files. */
2007 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2008 return true;
2009 /* Functions marked "dllexport" must be emitted so that they are
2010 visible to other DLLs. */
2011 if (flag_keep_inline_dllexport
2012 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2013 return true;
2014
2015 /* When not optimizing, do not bother to produce definitions for extern
2016 symbols. */
2017 if (DECL_REALLY_EXTERN (decl)
2018 && ((TREE_CODE (decl) != FUNCTION_DECL
2019 && !optimize)
2020 || (TREE_CODE (decl) == FUNCTION_DECL
2021 && !opt_for_fn (decl, optimize)))
2022 && !lookup_attribute ("always_inline", decl))
2023 return false;
2024
2025 /* If this entity was used, let the back end see it; it will decide
2026 whether or not to emit it into the object file. */
2027 if (TREE_USED (decl))
2028 return true;
2029 /* Virtual functions might be needed for devirtualization. */
2030 if (flag_devirtualize
2031 && TREE_CODE (decl) == FUNCTION_DECL
2032 && DECL_VIRTUAL_P (decl))
2033 return true;
2034 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2035 reference to DECL might cause it to be emitted later. */
2036 return false;
2037}
2038
2039/* If necessary, write out the vtables for the dynamic class CTYPE.
2040 Returns true if any vtables were emitted. */
2041
2042static bool
2043maybe_emit_vtables (tree ctype)
2044{
2045 tree vtbl;
2046 tree primary_vtbl;
2047 int needed = 0;
2048 varpool_node *current = NULL, *last = NULL;
2049
2050 /* If the vtables for this class have already been emitted there is
2051 nothing more to do. */
2052 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2053 if (var_finalized_p (primary_vtbl))
2054 return false;
2055 /* Ignore dummy vtables made by get_vtable_decl. */
2056 if (TREE_TYPE (primary_vtbl) == void_type_node)
2057 return false;
2058
2059 /* On some targets, we cannot determine the key method until the end
2060 of the translation unit -- which is when this function is
2061 called. */
2062 if (!targetm.cxx.key_method_may_be_inline ())
2063 determine_key_method (ctype);
2064
2065 /* See if any of the vtables are needed. */
2066 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2067 {
2068 import_export_decl (vtbl);
2069 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2070 needed = 1;
2071 }
2072 if (!needed)
2073 {
2074 /* If the references to this class' vtables are optimized away,
2075 still emit the appropriate debugging information. See
2076 dfs_debug_mark. */
2077 if (DECL_COMDAT (primary_vtbl)
2078 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2079 note_debug_info_needed (ctype);
2080 return false;
2081 }
2082
2083 /* The ABI requires that we emit all of the vtables if we emit any
2084 of them. */
2085 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2086 {
2087 /* Mark entities references from the virtual table as used. */
2088 mark_vtable_entries (vtbl);
2089
2090 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2091 {
2092 vec<tree, va_gc> *cleanups = NULL;
2093 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2094 LOOKUP_NORMAL);
2095
2096 /* It had better be all done at compile-time. */
2097 gcc_assert (!expr && !cleanups);
2098 }
2099
2100 /* Write it out. */
2101 DECL_EXTERNAL (vtbl) = 0;
2102 rest_of_decl_compilation (vtbl, 1, 1);
2103
2104 /* Because we're only doing syntax-checking, we'll never end up
2105 actually marking the variable as written. */
2106 if (flag_syntax_only)
2107 TREE_ASM_WRITTEN (vtbl) = 1;
2108 else if (DECL_ONE_ONLY (vtbl))
2109 {
2110 current = varpool_node::get_create (vtbl);
2111 if (last)
2112 current->add_to_same_comdat_group (last);
2113 last = current;
2114 }
2115 }
2116
2117 /* Since we're writing out the vtable here, also write the debug
2118 info. */
2119 note_debug_info_needed (ctype);
2120
2121 return true;
2122}
2123
2124/* A special return value from type_visibility meaning internal
2125 linkage. */
2126
2127enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2128
2129/* walk_tree helper function for type_visibility. */
2130
2131static tree
2132min_vis_r (tree *tp, int *walk_subtrees, void *data)
2133{
2134 int *vis_p = (int *)data;
2135 if (! TYPE_P (*tp))
2136 {
2137 *walk_subtrees = 0;
2138 }
2139 else if (OVERLOAD_TYPE_P (*tp)
2140 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2141 {
2142 *vis_p = VISIBILITY_ANON;
2143 return *tp;
2144 }
2145 else if (CLASS_TYPE_P (*tp)
2146 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2147 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2148 return NULL;
2149}
2150
2151/* Returns the visibility of TYPE, which is the minimum visibility of its
2152 component types. */
2153
2154static int
2155type_visibility (tree type)
2156{
2157 int vis = VISIBILITY_DEFAULT;
2158 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2159 return vis;
2160}
2161
2162/* Limit the visibility of DECL to VISIBILITY, if not explicitly
2163 specified (or if VISIBILITY is static). If TMPL is true, this
2164 constraint is for a template argument, and takes precedence
2165 over explicitly-specified visibility on the template. */
2166
2167static void
2168constrain_visibility (tree decl, int visibility, bool tmpl)
2169{
2170 if (visibility == VISIBILITY_ANON)
2171 {
2172 /* extern "C" declarations aren't affected by the anonymous
2173 namespace. */
2174 if (!DECL_EXTERN_C_P (decl))
2175 {
2176 TREE_PUBLIC (decl) = 0;
2177 DECL_WEAK (decl) = 0;
2178 DECL_COMMON (decl) = 0;
224ceb26 2179 DECL_COMDAT (decl) = false;
dda118e3
JM
2180 if (TREE_CODE (decl) == FUNCTION_DECL
2181 || TREE_CODE (decl) == VAR_DECL)
2182 {
2183 struct symtab_node *snode = symtab_node::get (decl);
2184
2185 if (snode)
2186 snode->set_comdat_group (NULL);
2187 }
2188 DECL_INTERFACE_KNOWN (decl) = 1;
2189 if (DECL_LANG_SPECIFIC (decl))
2190 DECL_NOT_REALLY_EXTERN (decl) = 1;
2191 }
2192 }
2193 else if (visibility > DECL_VISIBILITY (decl)
2194 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2195 {
2196 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2197 /* This visibility was not specified. */
2198 DECL_VISIBILITY_SPECIFIED (decl) = false;
2199 }
2200}
2201
2202/* Constrain the visibility of DECL based on the visibility of its template
2203 arguments. */
2204
2205static void
2206constrain_visibility_for_template (tree decl, tree targs)
2207{
2208 /* If this is a template instantiation, check the innermost
2209 template args for visibility constraints. The outer template
2210 args are covered by the class check. */
2211 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2212 int i;
2213 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2214 {
2215 int vis = 0;
2216
2217 tree arg = TREE_VEC_ELT (args, i-1);
2218 if (TYPE_P (arg))
2219 vis = type_visibility (arg);
224ceb26 2220 else
dda118e3 2221 {
224ceb26
JM
2222 if (REFERENCE_REF_P (arg))
2223 arg = TREE_OPERAND (arg, 0);
2224 if (TREE_TYPE (arg))
2225 STRIP_NOPS (arg);
dda118e3
JM
2226 if (TREE_CODE (arg) == ADDR_EXPR)
2227 arg = TREE_OPERAND (arg, 0);
2228 if (VAR_OR_FUNCTION_DECL_P (arg))
2229 {
2230 if (! TREE_PUBLIC (arg))
2231 vis = VISIBILITY_ANON;
2232 else
2233 vis = DECL_VISIBILITY (arg);
2234 }
2235 }
2236 if (vis)
2237 constrain_visibility (decl, vis, true);
2238 }
2239}
2240
2241/* Like c_determine_visibility, but with additional C++-specific
2242 behavior.
2243
2244 Function-scope entities can rely on the function's visibility because
2245 it is set in start_preparsed_function.
2246
2247 Class-scope entities cannot rely on the class's visibility until the end
2248 of the enclosing class definition.
2249
2250 Note that because namespaces have multiple independent definitions,
2251 namespace visibility is handled elsewhere using the #pragma visibility
2252 machinery rather than by decorating the namespace declaration.
2253
2254 The goal is for constraints from the type to give a diagnostic, and
2255 other constraints to be applied silently. */
2256
2257void
2258determine_visibility (tree decl)
2259{
2260 tree class_type = NULL_TREE;
2261 bool use_template;
2262 bool orig_visibility_specified;
2263 enum symbol_visibility orig_visibility;
2264
2265 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2266
2267 /* Only relevant for names with external linkage. */
2268 if (!TREE_PUBLIC (decl))
2269 return;
2270
2271 /* Cloned constructors and destructors get the same visibility as
2272 the underlying function. That should be set up in
2273 maybe_clone_body. */
2274 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2275
2276 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2277 orig_visibility = DECL_VISIBILITY (decl);
2278
2279 if (TREE_CODE (decl) == TYPE_DECL)
2280 {
2281 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2282 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2283 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2284 use_template = 1;
2285 else
2286 use_template = 0;
2287 }
2288 else if (DECL_LANG_SPECIFIC (decl))
2289 use_template = DECL_USE_TEMPLATE (decl);
2290 else
2291 use_template = 0;
2292
2293 /* If DECL is a member of a class, visibility specifiers on the
2294 class can influence the visibility of the DECL. */
2295 if (DECL_CLASS_SCOPE_P (decl))
2296 class_type = DECL_CONTEXT (decl);
2297 else
2298 {
2299 /* Not a class member. */
2300
2301 /* Virtual tables have DECL_CONTEXT set to their associated class,
2302 so they are automatically handled above. */
2303 gcc_assert (!VAR_P (decl)
2304 || !DECL_VTABLE_OR_VTT_P (decl));
2305
2306 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2307 {
2308 /* Local statics and classes get the visibility of their
2309 containing function by default, except that
2310 -fvisibility-inlines-hidden doesn't affect them. */
2311 tree fn = DECL_CONTEXT (decl);
2312 if (DECL_VISIBILITY_SPECIFIED (fn))
2313 {
2314 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2315 DECL_VISIBILITY_SPECIFIED (decl) =
2316 DECL_VISIBILITY_SPECIFIED (fn);
2317 }
2318 else
2319 {
2320 if (DECL_CLASS_SCOPE_P (fn))
2321 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2322 else if (determine_hidden_inline (fn))
2323 {
2324 DECL_VISIBILITY (decl) = default_visibility;
2325 DECL_VISIBILITY_SPECIFIED (decl) =
2326 visibility_options.inpragma;
2327 }
2328 else
2329 {
2330 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2331 DECL_VISIBILITY_SPECIFIED (decl) =
2332 DECL_VISIBILITY_SPECIFIED (fn);
2333 }
2334 }
2335
2336 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2337 but have no TEMPLATE_INFO, so don't try to check it. */
2338 use_template = 0;
2339 }
2340 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2341 && flag_visibility_ms_compat)
2342 {
2343 /* Under -fvisibility-ms-compat, types are visible by default,
2344 even though their contents aren't. */
2345 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2346 int underlying_vis = type_visibility (underlying_type);
2347 if (underlying_vis == VISIBILITY_ANON
2348 || (CLASS_TYPE_P (underlying_type)
2349 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2350 constrain_visibility (decl, underlying_vis, false);
2351 else
2352 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2353 }
2354 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2355 {
2356 /* tinfo visibility is based on the type it's for. */
2357 constrain_visibility
2358 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2359
2360 /* Give the target a chance to override the visibility associated
2361 with DECL. */
2362 if (TREE_PUBLIC (decl)
2363 && !DECL_REALLY_EXTERN (decl)
2364 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2365 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2366 targetm.cxx.determine_class_data_visibility (decl);
2367 }
2368 else if (use_template)
2369 /* Template instantiations and specializations get visibility based
2370 on their template unless they override it with an attribute. */;
2371 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2372 {
2373 if (determine_hidden_inline (decl))
2374 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2375 else
2376 {
2377 /* Set default visibility to whatever the user supplied with
2378 #pragma GCC visibility or a namespace visibility attribute. */
2379 DECL_VISIBILITY (decl) = default_visibility;
2380 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2381 }
2382 }
2383 }
2384
2385 if (use_template)
2386 {
2387 /* If the specialization doesn't specify visibility, use the
2388 visibility from the template. */
2389 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2390 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2391 : DECL_TEMPLATE_INFO (decl));
2392 tree args = TI_ARGS (tinfo);
2393 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2394 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2395 : DECL_ATTRIBUTES (decl));
2396
2397 if (args != error_mark_node)
2398 {
2399 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2400
2401 if (!DECL_VISIBILITY_SPECIFIED (decl))
2402 {
2403 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2404 && determine_hidden_inline (decl))
2405 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2406 else
2407 {
2408 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2409 DECL_VISIBILITY_SPECIFIED (decl)
2410 = DECL_VISIBILITY_SPECIFIED (pattern);
2411 }
2412 }
2413
2414 if (args
2415 /* Template argument visibility outweighs #pragma or namespace
2416 visibility, but not an explicit attribute. */
2417 && !lookup_attribute ("visibility", attribs))
2418 {
2419 int depth = TMPL_ARGS_DEPTH (args);
2420 if (DECL_VISIBILITY_SPECIFIED (decl))
2421 {
2422 /* A class template member with explicit visibility
2423 overrides the class visibility, so we need to apply
2424 all the levels of template args directly. */
2425 int i;
2426 for (i = 1; i <= depth; ++i)
2427 {
2428 tree lev = TMPL_ARGS_LEVEL (args, i);
2429 constrain_visibility_for_template (decl, lev);
2430 }
2431 }
2432 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2433 /* Limit visibility based on its template arguments. */
2434 constrain_visibility_for_template (decl, args);
2435 }
2436 }
2437 }
2438
2439 if (class_type)
2440 determine_visibility_from_class (decl, class_type);
2441
2442 if (decl_anon_ns_mem_p (decl))
2443 /* Names in an anonymous namespace get internal linkage.
2444 This might change once we implement export. */
2445 constrain_visibility (decl, VISIBILITY_ANON, false);
2446 else if (TREE_CODE (decl) != TYPE_DECL)
2447 {
2448 /* Propagate anonymity from type to decl. */
2449 int tvis = type_visibility (TREE_TYPE (decl));
2450 if (tvis == VISIBILITY_ANON
2451 || ! DECL_VISIBILITY_SPECIFIED (decl))
2452 constrain_visibility (decl, tvis, false);
2453 }
2454 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2455 /* DR 757: A type without linkage shall not be used as the type of a
2456 variable or function with linkage, unless
2457 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2458 o the variable or function is not used (3.2 [basic.def.odr]) or is
2459 defined in the same translation unit.
2460
2461 Since non-extern "C" decls need to be defined in the same
2462 translation unit, we can make the type internal. */
2463 constrain_visibility (decl, VISIBILITY_ANON, false);
2464
2465 /* If visibility changed and DECL already has DECL_RTL, ensure
2466 symbol flags are updated. */
2467 if ((DECL_VISIBILITY (decl) != orig_visibility
2468 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2469 && ((VAR_P (decl) && TREE_STATIC (decl))
2470 || TREE_CODE (decl) == FUNCTION_DECL)
2471 && DECL_RTL_SET_P (decl))
2472 make_decl_rtl (decl);
2473}
2474
2475/* By default, static data members and function members receive
2476 the visibility of their containing class. */
2477
2478static void
2479determine_visibility_from_class (tree decl, tree class_type)
2480{
2481 if (DECL_VISIBILITY_SPECIFIED (decl))
2482 return;
2483
2484 if (determine_hidden_inline (decl))
2485 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2486 else
2487 {
2488 /* Default to the class visibility. */
2489 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2490 DECL_VISIBILITY_SPECIFIED (decl)
2491 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2492 }
2493
2494 /* Give the target a chance to override the visibility associated
2495 with DECL. */
2496 if (VAR_P (decl)
2497 && (DECL_TINFO_P (decl)
2498 || (DECL_VTABLE_OR_VTT_P (decl)
2499 /* Construction virtual tables are not exported because
2500 they cannot be referred to from other object files;
2501 their name is not standardized by the ABI. */
2502 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2503 && TREE_PUBLIC (decl)
2504 && !DECL_REALLY_EXTERN (decl)
2505 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2506 targetm.cxx.determine_class_data_visibility (decl);
2507}
2508
2509/* Returns true iff DECL is an inline that should get hidden visibility
2510 because of -fvisibility-inlines-hidden. */
2511
2512static bool
2513determine_hidden_inline (tree decl)
2514{
2515 return (visibility_options.inlines_hidden
2516 /* Don't do this for inline templates; specializations might not be
2517 inline, and we don't want them to inherit the hidden
2518 visibility. We'll set it here for all inline instantiations. */
2519 && !processing_template_decl
2520 && TREE_CODE (decl) == FUNCTION_DECL
2521 && DECL_DECLARED_INLINE_P (decl)
2522 && (! DECL_LANG_SPECIFIC (decl)
2523 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2524}
2525
2526/* Constrain the visibility of a class TYPE based on the visibility of its
2527 field types. Warn if any fields require lesser visibility. */
2528
2529void
2530constrain_class_visibility (tree type)
2531{
2532 tree binfo;
2533 tree t;
2534 int i;
2535
2536 int vis = type_visibility (type);
2537
2538 if (vis == VISIBILITY_ANON
2539 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2540 return;
2541
2542 /* Don't warn about visibility if the class has explicit visibility. */
2543 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2544 vis = VISIBILITY_INTERNAL;
2545
2546 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2547 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2548 {
2549 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2550 int subvis = type_visibility (ftype);
2551
2552 if (subvis == VISIBILITY_ANON)
2553 {
2554 if (!in_main_input_context ())
2555 warning (0, "\
2556%qT has a field %qD whose type uses the anonymous namespace",
2557 type, t);
2558 }
2559 else if (MAYBE_CLASS_TYPE_P (ftype)
2560 && vis < VISIBILITY_HIDDEN
2561 && subvis >= VISIBILITY_HIDDEN)
2562 warning (OPT_Wattributes, "\
2563%qT declared with greater visibility than the type of its field %qD",
2564 type, t);
2565 }
2566
2567 binfo = TYPE_BINFO (type);
2568 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2569 {
2570 int subvis = type_visibility (TREE_TYPE (t));
2571
2572 if (subvis == VISIBILITY_ANON)
2573 {
2574 if (!in_main_input_context())
2575 warning (0, "\
2576%qT has a base %qT whose type uses the anonymous namespace",
2577 type, TREE_TYPE (t));
2578 }
2579 else if (vis < VISIBILITY_HIDDEN
2580 && subvis >= VISIBILITY_HIDDEN)
2581 warning (OPT_Wattributes, "\
2582%qT declared with greater visibility than its base %qT",
2583 type, TREE_TYPE (t));
2584 }
2585}
2586
2587/* Functions for adjusting the visibility of a tagged type and its nested
2588 types and declarations when it gets a name for linkage purposes from a
2589 typedef. */
2590
2591static void bt_reset_linkage_1 (binding_entry, void *);
2592static void bt_reset_linkage_2 (binding_entry, void *);
2593
2594/* First reset the visibility of all the types. */
2595
2596static void
2597reset_type_linkage_1 (tree type)
2598{
2599 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2600 if (CLASS_TYPE_P (type))
2601 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2602 bt_reset_linkage_1, NULL);
2603}
2604static void
2605bt_reset_linkage_1 (binding_entry b, void */*data*/)
2606{
2607 reset_type_linkage_1 (b->type);
2608}
2609
2610/* Then reset the visibility of any static data members or member
2611 functions that use those types. */
2612
2613static void
2614reset_decl_linkage (tree decl)
2615{
2616 if (TREE_PUBLIC (decl))
2617 return;
2618 if (DECL_CLONED_FUNCTION_P (decl))
2619 return;
2620 TREE_PUBLIC (decl) = true;
2621 DECL_INTERFACE_KNOWN (decl) = false;
2622 determine_visibility (decl);
2623 tentative_decl_linkage (decl);
2624}
2625static void
2626reset_type_linkage_2 (tree type)
2627{
2628 if (CLASS_TYPE_P (type))
2629 {
2630 if (tree vt = CLASSTYPE_VTABLES (type))
2631 {
2632 tree name = mangle_vtbl_for_type (type);
2633 DECL_NAME (vt) = name;
2634 SET_DECL_ASSEMBLER_NAME (vt, name);
2635 reset_decl_linkage (vt);
2636 }
2637 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2638 {
2639 tree name = mangle_typeinfo_for_type (type);
2640 DECL_NAME (ti) = name;
2641 SET_DECL_ASSEMBLER_NAME (ti, name);
2642 TREE_TYPE (name) = type;
2643 reset_decl_linkage (ti);
2644 }
2645 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2646 if (TREE_CODE (m) == VAR_DECL)
2647 reset_decl_linkage (m);
2648 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2649 reset_decl_linkage (m);
2650 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2651 bt_reset_linkage_2, NULL);
2652 }
2653}
2654static void
2655bt_reset_linkage_2 (binding_entry b, void */*data*/)
2656{
2657 reset_type_linkage_2 (b->type);
2658}
2659void
2660reset_type_linkage (tree type)
2661{
2662 reset_type_linkage_1 (type);
2663 reset_type_linkage_2 (type);
2664}
2665
2666/* Set up our initial idea of what the linkage of DECL should be. */
2667
2668void
2669tentative_decl_linkage (tree decl)
2670{
2671 if (DECL_INTERFACE_KNOWN (decl))
2672 /* We've already made a decision as to how this function will
2673 be handled. */;
2674 else if (vague_linkage_p (decl))
2675 {
2676 if (TREE_CODE (decl) == FUNCTION_DECL
2677 && decl_defined_p (decl))
2678 {
2679 DECL_EXTERNAL (decl) = 1;
2680 DECL_NOT_REALLY_EXTERN (decl) = 1;
2681 note_vague_linkage_fn (decl);
2682 /* A non-template inline function with external linkage will
2683 always be COMDAT. As we must eventually determine the
2684 linkage of all functions, and as that causes writes to
2685 the data mapped in from the PCH file, it's advantageous
2686 to mark the functions at this point. */
2687 if (DECL_DECLARED_INLINE_P (decl)
2688 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2689 || DECL_DEFAULTED_FN (decl)))
2690 {
2691 /* This function must have external linkage, as
2692 otherwise DECL_INTERFACE_KNOWN would have been
2693 set. */
2694 gcc_assert (TREE_PUBLIC (decl));
2695 comdat_linkage (decl);
2696 DECL_INTERFACE_KNOWN (decl) = 1;
2697 }
2698 }
2699 else if (TREE_CODE (decl) == VAR_DECL)
2700 maybe_commonize_var (decl);
2701 }
2702}
2703
2704/* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2705 for DECL has not already been determined, do so now by setting
2706 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2707 function is called entities with vague linkage whose definitions
2708 are available must have TREE_PUBLIC set.
2709
2710 If this function decides to place DECL in COMDAT, it will set
2711 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2712 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2713 callers defer that decision until it is clear that DECL is actually
2714 required. */
2715
2716void
2717import_export_decl (tree decl)
2718{
2719 int emit_p;
2720 bool comdat_p;
2721 bool import_p;
2722 tree class_type = NULL_TREE;
2723
2724 if (DECL_INTERFACE_KNOWN (decl))
2725 return;
2726
2727 /* We cannot determine what linkage to give to an entity with vague
2728 linkage until the end of the file. For example, a virtual table
2729 for a class will be defined if and only if the key method is
2730 defined in this translation unit. As a further example, consider
2731 that when compiling a translation unit that uses PCH file with
2732 "-frepo" it would be incorrect to make decisions about what
2733 entities to emit when building the PCH; those decisions must be
2734 delayed until the repository information has been processed. */
2735 gcc_assert (at_eof);
2736 /* Object file linkage for explicit instantiations is handled in
2737 mark_decl_instantiated. For static variables in functions with
2738 vague linkage, maybe_commonize_var is used.
2739
2740 Therefore, the only declarations that should be provided to this
2741 function are those with external linkage that are:
2742
2743 * implicit instantiations of function templates
2744
2745 * inline function
2746
2747 * implicit instantiations of static data members of class
2748 templates
2749
2750 * virtual tables
2751
2752 * typeinfo objects
2753
2754 Furthermore, all entities that reach this point must have a
2755 definition available in this translation unit.
2756
2757 The following assertions check these conditions. */
2758 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2759 /* Any code that creates entities with TREE_PUBLIC cleared should
2760 also set DECL_INTERFACE_KNOWN. */
2761 gcc_assert (TREE_PUBLIC (decl));
2762 if (TREE_CODE (decl) == FUNCTION_DECL)
2763 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2764 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2765 || DECL_DECLARED_INLINE_P (decl));
2766 else
2767 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2768 || DECL_VTABLE_OR_VTT_P (decl)
2769 || DECL_TINFO_P (decl));
2770 /* Check that a definition of DECL is available in this translation
2771 unit. */
2772 gcc_assert (!DECL_REALLY_EXTERN (decl));
2773
2774 /* Assume that DECL will not have COMDAT linkage. */
2775 comdat_p = false;
2776 /* Assume that DECL will not be imported into this translation
2777 unit. */
2778 import_p = false;
2779
2780 /* See if the repository tells us whether or not to emit DECL in
2781 this translation unit. */
2782 emit_p = repo_emit_p (decl);
2783 if (emit_p == 0)
2784 import_p = true;
2785 else if (emit_p == 1)
2786 {
2787 /* The repository indicates that this entity should be defined
2788 here. Make sure the back end honors that request. */
2789 mark_needed (decl);
2790 /* Output the definition as an ordinary strong definition. */
2791 DECL_EXTERNAL (decl) = 0;
2792 DECL_INTERFACE_KNOWN (decl) = 1;
2793 return;
2794 }
2795
2796 if (import_p)
2797 /* We have already decided what to do with this DECL; there is no
2798 need to check anything further. */
2799 ;
2800 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2801 {
2802 class_type = DECL_CONTEXT (decl);
2803 import_export_class (class_type);
2804 if (TYPE_FOR_JAVA (class_type))
2805 import_p = true;
2806 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2807 && CLASSTYPE_INTERFACE_ONLY (class_type))
2808 import_p = true;
2809 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2810 && !CLASSTYPE_USE_TEMPLATE (class_type)
2811 && CLASSTYPE_KEY_METHOD (class_type)
2812 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2813 /* The ABI requires that all virtual tables be emitted with
2814 COMDAT linkage. However, on systems where COMDAT symbols
2815 don't show up in the table of contents for a static
2816 archive, or on systems without weak symbols (where we
2817 approximate COMDAT linkage by using internal linkage), the
2818 linker will report errors about undefined symbols because
2819 it will not see the virtual table definition. Therefore,
2820 in the case that we know that the virtual table will be
2821 emitted in only one translation unit, we make the virtual
2822 table an ordinary definition with external linkage. */
2823 DECL_EXTERNAL (decl) = 0;
2824 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2825 {
2826 /* CLASS_TYPE is being exported from this translation unit,
2827 so DECL should be defined here. */
2828 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2829 /* If a class is declared in a header with the "extern
2830 template" extension, then it will not be instantiated,
2831 even in translation units that would normally require
2832 it. Often such classes are explicitly instantiated in
2833 one translation unit. Therefore, the explicit
2834 instantiation must be made visible to other translation
2835 units. */
2836 DECL_EXTERNAL (decl) = 0;
2837 else
2838 {
2839 /* The generic C++ ABI says that class data is always
2840 COMDAT, even if there is a key function. Some
2841 variants (e.g., the ARM EABI) says that class data
2842 only has COMDAT linkage if the class data might be
2843 emitted in more than one translation unit. When the
2844 key method can be inline and is inline, we still have
2845 to arrange for comdat even though
2846 class_data_always_comdat is false. */
2847 if (!CLASSTYPE_KEY_METHOD (class_type)
2848 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2849 || targetm.cxx.class_data_always_comdat ())
2850 {
2851 /* The ABI requires COMDAT linkage. Normally, we
2852 only emit COMDAT things when they are needed;
2853 make sure that we realize that this entity is
2854 indeed needed. */
2855 comdat_p = true;
2856 mark_needed (decl);
2857 }
2858 }
2859 }
2860 else if (!flag_implicit_templates
2861 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2862 import_p = true;
2863 else
2864 comdat_p = true;
2865 }
2866 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2867 {
2868 tree type = TREE_TYPE (DECL_NAME (decl));
2869 if (CLASS_TYPE_P (type))
2870 {
2871 class_type = type;
2872 import_export_class (type);
2873 if (CLASSTYPE_INTERFACE_KNOWN (type)
2874 && TYPE_POLYMORPHIC_P (type)
2875 && CLASSTYPE_INTERFACE_ONLY (type)
2876 /* If -fno-rtti was specified, then we cannot be sure
2877 that RTTI information will be emitted with the
2878 virtual table of the class, so we must emit it
2879 wherever it is used. */
2880 && flag_rtti)
2881 import_p = true;
2882 else
2883 {
2884 if (CLASSTYPE_INTERFACE_KNOWN (type)
2885 && !CLASSTYPE_INTERFACE_ONLY (type))
2886 {
2887 comdat_p = (targetm.cxx.class_data_always_comdat ()
2888 || (CLASSTYPE_KEY_METHOD (type)
2889 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2890 mark_needed (decl);
2891 if (!flag_weak)
2892 {
2893 comdat_p = false;
2894 DECL_EXTERNAL (decl) = 0;
2895 }
2896 }
2897 else
2898 comdat_p = true;
2899 }
2900 }
2901 else
2902 comdat_p = true;
2903 }
2904 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2905 {
2906 /* DECL is an implicit instantiation of a function or static
2907 data member. */
2908 if ((flag_implicit_templates
2909 && !flag_use_repository)
2910 || (flag_implicit_inline_templates
2911 && TREE_CODE (decl) == FUNCTION_DECL
2912 && DECL_DECLARED_INLINE_P (decl)))
2913 comdat_p = true;
2914 else
2915 /* If we are not implicitly generating templates, then mark
2916 this entity as undefined in this translation unit. */
2917 import_p = true;
2918 }
2919 else if (DECL_FUNCTION_MEMBER_P (decl))
2920 {
2921 if (!DECL_DECLARED_INLINE_P (decl))
2922 {
2923 tree ctype = DECL_CONTEXT (decl);
2924 import_export_class (ctype);
2925 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2926 {
2927 DECL_NOT_REALLY_EXTERN (decl)
2928 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2929 || (DECL_DECLARED_INLINE_P (decl)
2930 && ! flag_implement_inlines
2931 && !DECL_VINDEX (decl)));
2932
2933 if (!DECL_NOT_REALLY_EXTERN (decl))
2934 DECL_EXTERNAL (decl) = 1;
2935
2936 /* Always make artificials weak. */
2937 if (DECL_ARTIFICIAL (decl) && flag_weak)
2938 comdat_p = true;
2939 else
2940 maybe_make_one_only (decl);
2941 }
2942 }
2943 else
2944 comdat_p = true;
2945 }
2946 else
2947 comdat_p = true;
2948
2949 if (import_p)
2950 {
2951 /* If we are importing DECL into this translation unit, mark is
2952 an undefined here. */
2953 DECL_EXTERNAL (decl) = 1;
2954 DECL_NOT_REALLY_EXTERN (decl) = 0;
2955 }
2956 else if (comdat_p)
2957 {
2958 /* If we decided to put DECL in COMDAT, mark it accordingly at
2959 this point. */
2960 comdat_linkage (decl);
2961 }
2962
2963 DECL_INTERFACE_KNOWN (decl) = 1;
2964}
2965
2966/* Return an expression that performs the destruction of DECL, which
2967 must be a VAR_DECL whose type has a non-trivial destructor, or is
2968 an array whose (innermost) elements have a non-trivial destructor. */
2969
2970tree
2971build_cleanup (tree decl)
2972{
2973 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2974 gcc_assert (clean != NULL_TREE);
2975 return clean;
2976}
2977
2978/* Returns the initialization guard variable for the variable DECL,
2979 which has static storage duration. */
2980
2981tree
2982get_guard (tree decl)
2983{
2984 tree sname;
2985 tree guard;
2986
2987 sname = mangle_guard_variable (decl);
2988 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2989 if (! guard)
2990 {
2991 tree guard_type;
2992
2993 /* We use a type that is big enough to contain a mutex as well
2994 as an integer counter. */
2995 guard_type = targetm.cxx.guard_type ();
2996 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2997 VAR_DECL, sname, guard_type);
2998
2999 /* The guard should have the same linkage as what it guards. */
3000 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3001 TREE_STATIC (guard) = TREE_STATIC (decl);
3002 DECL_COMMON (guard) = DECL_COMMON (decl);
3003 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3004 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3005 if (DECL_ONE_ONLY (decl))
3006 make_decl_one_only (guard, cxx_comdat_group (guard));
3007 if (TREE_PUBLIC (decl))
3008 DECL_WEAK (guard) = DECL_WEAK (decl);
3009 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3010 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3011
3012 DECL_ARTIFICIAL (guard) = 1;
3013 DECL_IGNORED_P (guard) = 1;
3014 TREE_USED (guard) = 1;
3015 pushdecl_top_level_and_finish (guard, NULL_TREE);
3016 }
3017 return guard;
3018}
3019
3020/* Return those bits of the GUARD variable that should be set when the
3021 guarded entity is actually initialized. */
3022
3023static tree
3024get_guard_bits (tree guard)
3025{
3026 if (!targetm.cxx.guard_mask_bit ())
3027 {
3028 /* We only set the first byte of the guard, in order to leave room
3029 for a mutex in the high-order bits. */
3030 guard = build1 (ADDR_EXPR,
3031 build_pointer_type (TREE_TYPE (guard)),
3032 guard);
3033 guard = build1 (NOP_EXPR,
3034 build_pointer_type (char_type_node),
3035 guard);
3036 guard = build1 (INDIRECT_REF, char_type_node, guard);
3037 }
3038
3039 return guard;
3040}
3041
3042/* Return an expression which determines whether or not the GUARD
3043 variable has already been initialized. */
3044
3045tree
3046get_guard_cond (tree guard)
3047{
3048 tree guard_value;
3049
3050 /* Check to see if the GUARD is zero. */
3051 guard = get_guard_bits (guard);
3052
3053 /* Mask off all but the low bit. */
3054 if (targetm.cxx.guard_mask_bit ())
3055 {
3056 guard_value = integer_one_node;
3057 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3058 guard_value = convert (TREE_TYPE (guard), guard_value);
3059 guard = cp_build_binary_op (input_location,
3060 BIT_AND_EXPR, guard, guard_value,
3061 tf_warning_or_error);
3062 }
3063
3064 guard_value = integer_zero_node;
3065 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3066 guard_value = convert (TREE_TYPE (guard), guard_value);
3067 return cp_build_binary_op (input_location,
3068 EQ_EXPR, guard, guard_value,
3069 tf_warning_or_error);
3070}
3071
3072/* Return an expression which sets the GUARD variable, indicating that
3073 the variable being guarded has been initialized. */
3074
3075tree
3076set_guard (tree guard)
3077{
3078 tree guard_init;
3079
3080 /* Set the GUARD to one. */
3081 guard = get_guard_bits (guard);
3082 guard_init = integer_one_node;
3083 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3084 guard_init = convert (TREE_TYPE (guard), guard_init);
3085 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3086 tf_warning_or_error);
3087}
3088
3089/* Returns true iff we can tell that VAR does not have a dynamic
3090 initializer. */
3091
3092static bool
3093var_defined_without_dynamic_init (tree var)
3094{
3095 /* If it's defined in another TU, we can't tell. */
3096 if (DECL_EXTERNAL (var))
3097 return false;
3098 /* If it has a non-trivial destructor, registering the destructor
3099 counts as dynamic initialization. */
3100 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3101 return false;
3102 /* If it's in this TU, its initializer has been processed, unless
3103 it's a case of self-initialization, then DECL_INITIALIZED_P is
3104 false while the initializer is handled by finish_id_expression. */
3105 if (!DECL_INITIALIZED_P (var))
3106 return false;
3107 /* If it has no initializer or a constant one, it's not dynamic. */
3108 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3109 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3110}
3111
3112/* Returns true iff VAR is a variable that needs uses to be
3113 wrapped for possible dynamic initialization. */
3114
3115static bool
3116var_needs_tls_wrapper (tree var)
3117{
3118 return (!error_operand_p (var)
3119 && DECL_THREAD_LOCAL_P (var)
3120 && !DECL_GNU_TLS_P (var)
3121 && !DECL_FUNCTION_SCOPE_P (var)
3122 && !var_defined_without_dynamic_init (var));
3123}
3124
3125/* Get the FUNCTION_DECL for the shared TLS init function for this
3126 translation unit. */
3127
3128static tree
3129get_local_tls_init_fn (void)
3130{
3131 tree sname = get_identifier ("__tls_init");
3132 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3133 if (!fn)
3134 {
3135 fn = build_lang_decl (FUNCTION_DECL, sname,
3136 build_function_type (void_type_node,
3137 void_list_node));
3138 SET_DECL_LANGUAGE (fn, lang_c);
3139 TREE_PUBLIC (fn) = false;
3140 DECL_ARTIFICIAL (fn) = true;
3141 mark_used (fn);
3142 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3143 }
3144 return fn;
3145}
3146
3147/* Get a FUNCTION_DECL for the init function for the thread_local
3148 variable VAR. The init function will be an alias to the function
3149 that initializes all the non-local TLS variables in the translation
3150 unit. The init function is only used by the wrapper function. */
3151
3152static tree
3153get_tls_init_fn (tree var)
3154{
3155 /* Only C++11 TLS vars need this init fn. */
3156 if (!var_needs_tls_wrapper (var))
3157 return NULL_TREE;
3158
3159 /* If -fno-extern-tls-init, assume that we don't need to call
3160 a tls init function for a variable defined in another TU. */
3161 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3162 return NULL_TREE;
3163
3164#ifdef ASM_OUTPUT_DEF
3165 /* If the variable is internal, or if we can't generate aliases,
3166 call the local init function directly. */
3167 if (!TREE_PUBLIC (var))
3168#endif
3169 return get_local_tls_init_fn ();
3170
3171 tree sname = mangle_tls_init_fn (var);
3172 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3173 if (!fn)
3174 {
3175 fn = build_lang_decl (FUNCTION_DECL, sname,
3176 build_function_type (void_type_node,
3177 void_list_node));
3178 SET_DECL_LANGUAGE (fn, lang_c);
3179 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3180 DECL_ARTIFICIAL (fn) = true;
3181 DECL_COMDAT (fn) = DECL_COMDAT (var);
3182 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3183 if (DECL_ONE_ONLY (var))
3184 make_decl_one_only (fn, cxx_comdat_group (fn));
3185 if (TREE_PUBLIC (var))
3186 {
3187 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3188 /* If the variable is defined somewhere else and might have static
3189 initialization, make the init function a weak reference. */
3190 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3191 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3192 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3193 && DECL_EXTERNAL (var))
3194 declare_weak (fn);
3195 else
3196 DECL_WEAK (fn) = DECL_WEAK (var);
3197 }
3198 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3199 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3200 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3201 DECL_IGNORED_P (fn) = 1;
3202 mark_used (fn);
3203
3204 DECL_BEFRIENDING_CLASSES (fn) = var;
3205
3206 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3207 }
3208 return fn;
3209}
3210
3211/* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3212 variable VAR. The wrapper function calls the init function (if any) for
3213 VAR and then returns a reference to VAR. The wrapper function is used
3214 in place of VAR everywhere VAR is mentioned. */
3215
3216tree
3217get_tls_wrapper_fn (tree var)
3218{
3219 /* Only C++11 TLS vars need this wrapper fn. */
3220 if (!var_needs_tls_wrapper (var))
3221 return NULL_TREE;
3222
3223 tree sname = mangle_tls_wrapper_fn (var);
3224 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3225 if (!fn)
3226 {
3227 /* A named rvalue reference is an lvalue, so the wrapper should
3228 always return an lvalue reference. */
3229 tree type = non_reference (TREE_TYPE (var));
3230 type = build_reference_type (type);
3231 tree fntype = build_function_type (type, void_list_node);
3232 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3233 SET_DECL_LANGUAGE (fn, lang_c);
3234 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3235 DECL_ARTIFICIAL (fn) = true;
3236 DECL_IGNORED_P (fn) = 1;
3237 /* The wrapper is inline and emitted everywhere var is used. */
3238 DECL_DECLARED_INLINE_P (fn) = true;
3239 if (TREE_PUBLIC (var))
3240 {
3241 comdat_linkage (fn);
3242#ifdef HAVE_GAS_HIDDEN
3243 /* Make the wrapper bind locally; there's no reason to share
3244 the wrapper between multiple shared objects. */
3245 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3246 DECL_VISIBILITY_SPECIFIED (fn) = true;
3247#endif
3248 }
3249 if (!TREE_PUBLIC (fn))
3250 DECL_INTERFACE_KNOWN (fn) = true;
3251 mark_used (fn);
3252 note_vague_linkage_fn (fn);
3253
3254#if 0
3255 /* We want CSE to commonize calls to the wrapper, but marking it as
3256 pure is unsafe since it has side-effects. I guess we need a new
3257 ECF flag even weaker than ECF_PURE. FIXME! */
3258 DECL_PURE_P (fn) = true;
3259#endif
3260
3261 DECL_BEFRIENDING_CLASSES (fn) = var;
3262
3263 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3264 }
3265 return fn;
3266}
3267
3268/* At EOF, generate the definition for the TLS wrapper function FN:
3269
3270 T& var_wrapper() {
3271 if (init_fn) init_fn();
3272 return var;
3273 } */
3274
3275static void
3276generate_tls_wrapper (tree fn)
3277{
3278 tree var = DECL_BEFRIENDING_CLASSES (fn);
3279
3280 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3281 tree body = begin_function_body ();
3282 /* Only call the init fn if there might be one. */
3283 if (tree init_fn = get_tls_init_fn (var))
3284 {
3285 tree if_stmt = NULL_TREE;
3286 /* If init_fn is a weakref, make sure it exists before calling. */
3287 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3288 {
3289 if_stmt = begin_if_stmt ();
3290 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3291 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3292 NE_EXPR, addr, nullptr_node,
3293 tf_warning_or_error);
3294 finish_if_stmt_cond (cond, if_stmt);
3295 }
3296 finish_expr_stmt (build_cxx_call
3297 (init_fn, 0, NULL, tf_warning_or_error));
3298 if (if_stmt)
3299 {
3300 finish_then_clause (if_stmt);
3301 finish_if_stmt (if_stmt);
3302 }
3303 }
3304 else
3305 /* If there's no initialization, the wrapper is a constant function. */
3306 TREE_READONLY (fn) = true;
3307 finish_return_stmt (convert_from_reference (var));
3308 finish_function_body (body);
3309 expand_or_defer_fn (finish_function (0));
3310}
3311
3312/* Start the process of running a particular set of global constructors
3313 or destructors. Subroutine of do_[cd]tors. Also called from
3314 vtv_start_verification_constructor_init_function. */
3315
3316static tree
3317start_objects (int method_type, int initp)
3318{
3319 tree body;
3320 tree fndecl;
3321 char type[14];
3322
3323 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3324
3325 if (initp != DEFAULT_INIT_PRIORITY)
3326 {
3327 char joiner;
3328
3329#ifdef JOINER
3330 joiner = JOINER;
3331#else
3332 joiner = '_';
3333#endif
3334
3335 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3336 }
3337 else
3338 sprintf (type, "sub_%c", method_type);
3339
3340 fndecl = build_lang_decl (FUNCTION_DECL,
3341 get_file_function_name (type),
3342 build_function_type_list (void_type_node,
3343 NULL_TREE));
3344 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3345
3346 TREE_PUBLIC (current_function_decl) = 0;
3347
3348 /* Mark as artificial because it's not explicitly in the user's
3349 source code. */
3350 DECL_ARTIFICIAL (current_function_decl) = 1;
3351
3352 /* Mark this declaration as used to avoid spurious warnings. */
3353 TREE_USED (current_function_decl) = 1;
3354
3355 /* Mark this function as a global constructor or destructor. */
3356 if (method_type == 'I')
3357 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3358 else
3359 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3360
3361 body = begin_compound_stmt (BCS_FN_BODY);
3362
3363 return body;
3364}
3365
3366/* Finish the process of running a particular set of global constructors
3367 or destructors. Subroutine of do_[cd]tors. */
3368
3369static void
3370finish_objects (int method_type, int initp, tree body)
3371{
3372 tree fn;
3373
3374 /* Finish up. */
3375 finish_compound_stmt (body);
3376 fn = finish_function (0);
3377
3378 if (method_type == 'I')
3379 {
3380 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3381 decl_init_priority_insert (fn, initp);
3382 }
3383 else
3384 {
3385 DECL_STATIC_DESTRUCTOR (fn) = 1;
3386 decl_fini_priority_insert (fn, initp);
3387 }
3388
3389 expand_or_defer_fn (fn);
3390}
3391
3392/* The names of the parameters to the function created to handle
3393 initializations and destructions for objects with static storage
3394 duration. */
3395#define INITIALIZE_P_IDENTIFIER "__initialize_p"
3396#define PRIORITY_IDENTIFIER "__priority"
3397
3398/* The name of the function we create to handle initializations and
3399 destructions for objects with static storage duration. */
3400#define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3401
3402/* The declaration for the __INITIALIZE_P argument. */
3403static GTY(()) tree initialize_p_decl;
3404
3405/* The declaration for the __PRIORITY argument. */
3406static GTY(()) tree priority_decl;
3407
3408/* The declaration for the static storage duration function. */
3409static GTY(()) tree ssdf_decl;
3410
3411/* All the static storage duration functions created in this
3412 translation unit. */
3413static GTY(()) vec<tree, va_gc> *ssdf_decls;
3414
3415/* A map from priority levels to information about that priority
3416 level. There may be many such levels, so efficient lookup is
3417 important. */
3418static splay_tree priority_info_map;
3419
3420/* Begins the generation of the function that will handle all
3421 initialization and destruction of objects with static storage
3422 duration. The function generated takes two parameters of type
3423 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3424 nonzero, it performs initializations. Otherwise, it performs
3425 destructions. It only performs those initializations or
3426 destructions with the indicated __PRIORITY. The generated function
3427 returns no value.
3428
3429 It is assumed that this function will only be called once per
3430 translation unit. */
3431
3432static tree
3433start_static_storage_duration_function (unsigned count)
3434{
3435 tree type;
3436 tree body;
3437 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3438
3439 /* Create the identifier for this function. It will be of the form
3440 SSDF_IDENTIFIER_<number>. */
3441 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3442
3443 type = build_function_type_list (void_type_node,
3444 integer_type_node, integer_type_node,
3445 NULL_TREE);
3446
3447 /* Create the FUNCTION_DECL itself. */
3448 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3449 get_identifier (id),
3450 type);
3451 TREE_PUBLIC (ssdf_decl) = 0;
3452 DECL_ARTIFICIAL (ssdf_decl) = 1;
3453
3454 /* Put this function in the list of functions to be called from the
3455 static constructors and destructors. */
3456 if (!ssdf_decls)
3457 {
3458 vec_alloc (ssdf_decls, 32);
3459
3460 /* Take this opportunity to initialize the map from priority
3461 numbers to information about that priority level. */
3462 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3463 /*delete_key_fn=*/0,
3464 /*delete_value_fn=*/
3465 (splay_tree_delete_value_fn) &free);
3466
3467 /* We always need to generate functions for the
3468 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3469 priorities later, we'll be sure to find the
3470 DEFAULT_INIT_PRIORITY. */
3471 get_priority_info (DEFAULT_INIT_PRIORITY);
3472 }
3473
3474 vec_safe_push (ssdf_decls, ssdf_decl);
3475
3476 /* Create the argument list. */
3477 initialize_p_decl = cp_build_parm_decl
3478 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3479 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3480 TREE_USED (initialize_p_decl) = 1;
3481 priority_decl = cp_build_parm_decl
3482 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3483 DECL_CONTEXT (priority_decl) = ssdf_decl;
3484 TREE_USED (priority_decl) = 1;
3485
3486 DECL_CHAIN (initialize_p_decl) = priority_decl;
3487 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3488
3489 /* Put the function in the global scope. */
3490 pushdecl (ssdf_decl);
3491
3492 /* Start the function itself. This is equivalent to declaring the
3493 function as:
3494
3495 static void __ssdf (int __initialize_p, init __priority_p);
3496
3497 It is static because we only need to call this function from the
3498 various constructor and destructor functions for this module. */
3499 start_preparsed_function (ssdf_decl,
3500 /*attrs=*/NULL_TREE,
3501 SF_PRE_PARSED);
3502
3503 /* Set up the scope of the outermost block in the function. */
3504 body = begin_compound_stmt (BCS_FN_BODY);
3505
3506 return body;
3507}
3508
3509/* Finish the generation of the function which performs initialization
3510 and destruction of objects with static storage duration. After
3511 this point, no more such objects can be created. */
3512
3513static void
3514finish_static_storage_duration_function (tree body)
3515{
3516 /* Close out the function. */
3517 finish_compound_stmt (body);
3518 expand_or_defer_fn (finish_function (0));
3519}
3520
3521/* Return the information about the indicated PRIORITY level. If no
3522 code to handle this level has yet been generated, generate the
3523 appropriate prologue. */
3524
3525static priority_info
3526get_priority_info (int priority)
3527{
3528 priority_info pi;
3529 splay_tree_node n;
3530
3531 n = splay_tree_lookup (priority_info_map,
3532 (splay_tree_key) priority);
3533 if (!n)
3534 {
3535 /* Create a new priority information structure, and insert it
3536 into the map. */
3537 pi = XNEW (struct priority_info_s);
3538 pi->initializations_p = 0;
3539 pi->destructions_p = 0;
3540 splay_tree_insert (priority_info_map,
3541 (splay_tree_key) priority,
3542 (splay_tree_value) pi);
3543 }
3544 else
3545 pi = (priority_info) n->value;
3546
3547 return pi;
3548}
3549
3550/* The effective initialization priority of a DECL. */
3551
3552#define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3553 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3554 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3555
3556/* Whether a DECL needs a guard to protect it against multiple
3557 initialization. */
3558
3559#define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3560 || DECL_ONE_ONLY (decl) \
3561 || DECL_WEAK (decl)))
3562
3563/* Called from one_static_initialization_or_destruction(),
3564 via walk_tree.
3565 Walks the initializer list of a global variable and looks for
3566 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3567 and that have their DECL_CONTEXT() == NULL.
3568 For each such temporary variable, set their DECL_CONTEXT() to
3569 the current function. This is necessary because otherwise
3570 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3571 when trying to refer to a temporary variable that does not have
3572 it's DECL_CONTECT() properly set. */
3573static tree
3574fix_temporary_vars_context_r (tree *node,
3575 int * /*unused*/,
3576 void * /*unused1*/)
3577{
3578 gcc_assert (current_function_decl);
3579
3580 if (TREE_CODE (*node) == BIND_EXPR)
3581 {
3582 tree var;
3583
3584 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3585 if (VAR_P (var)
3586 && !DECL_NAME (var)
3587 && DECL_ARTIFICIAL (var)
3588 && !DECL_CONTEXT (var))
3589 DECL_CONTEXT (var) = current_function_decl;
3590 }
3591
3592 return NULL_TREE;
3593}
3594
3595/* Set up to handle the initialization or destruction of DECL. If
3596 INITP is nonzero, we are initializing the variable. Otherwise, we
3597 are destroying it. */
3598
3599static void
3600one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3601{
3602 tree guard_if_stmt = NULL_TREE;
3603 tree guard;
3604
3605 /* If we are supposed to destruct and there's a trivial destructor,
3606 nothing has to be done. */
3607 if (!initp
3608 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3609 return;
3610
3611 /* Trick the compiler into thinking we are at the file and line
3612 where DECL was declared so that error-messages make sense, and so
3613 that the debugger will show somewhat sensible file and line
3614 information. */
3615 input_location = DECL_SOURCE_LOCATION (decl);
3616
3617 /* Make sure temporary variables in the initialiser all have
3618 their DECL_CONTEXT() set to a value different from NULL_TREE.
3619 This can happen when global variables initialisers are built.
3620 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3621 the temporary variables that might have been generated in the
3622 accompagning initialisers is NULL_TREE, meaning the variables have been
3623 declared in the global namespace.
3624 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3625 of the temporaries are set to the current function decl. */
3626 cp_walk_tree_without_duplicates (&init,
3627 fix_temporary_vars_context_r,
3628 NULL);
3629
3630 /* Because of:
3631
3632 [class.access.spec]
3633
3634 Access control for implicit calls to the constructors,
3635 the conversion functions, or the destructor called to
3636 create and destroy a static data member is performed as
3637 if these calls appeared in the scope of the member's
3638 class.
3639
3640 we pretend we are in a static member function of the class of
3641 which the DECL is a member. */
3642 if (member_p (decl))
3643 {
3644 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3645 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3646 }
3647
3648 /* Assume we don't need a guard. */
3649 guard = NULL_TREE;
3650 /* We need a guard if this is an object with external linkage that
3651 might be initialized in more than one place. (For example, a
3652 static data member of a template, when the data member requires
3653 construction.) */
3654 if (NEEDS_GUARD_P (decl))
3655 {
3656 tree guard_cond;
3657
3658 guard = get_guard (decl);
3659
3660 /* When using __cxa_atexit, we just check the GUARD as we would
3661 for a local static. */
3662 if (flag_use_cxa_atexit)
3663 {
3664 /* When using __cxa_atexit, we never try to destroy
3665 anything from a static destructor. */
3666 gcc_assert (initp);
3667 guard_cond = get_guard_cond (guard);
3668 }
3669 /* If we don't have __cxa_atexit, then we will be running
3670 destructors from .fini sections, or their equivalents. So,
3671 we need to know how many times we've tried to initialize this
3672 object. We do initializations only if the GUARD is zero,
3673 i.e., if we are the first to initialize the variable. We do
3674 destructions only if the GUARD is one, i.e., if we are the
3675 last to destroy the variable. */
3676 else if (initp)
3677 guard_cond
3678 = cp_build_binary_op (input_location,
3679 EQ_EXPR,
3680 cp_build_unary_op (PREINCREMENT_EXPR,
3681 guard,
3682 /*noconvert=*/1,
3683 tf_warning_or_error),
3684 integer_one_node,
3685 tf_warning_or_error);
3686 else
3687 guard_cond
3688 = cp_build_binary_op (input_location,
3689 EQ_EXPR,
3690 cp_build_unary_op (PREDECREMENT_EXPR,
3691 guard,
3692 /*noconvert=*/1,
3693 tf_warning_or_error),
3694 integer_zero_node,
3695 tf_warning_or_error);
3696
3697 guard_if_stmt = begin_if_stmt ();
3698 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3699 }
3700
3701
3702 /* If we're using __cxa_atexit, we have not already set the GUARD,
3703 so we must do so now. */
3704 if (guard && initp && flag_use_cxa_atexit)
3705 finish_expr_stmt (set_guard (guard));
3706
3707 /* Perform the initialization or destruction. */
3708 if (initp)
3709 {
3710 if (init)
3711 {
3712 finish_expr_stmt (init);
3713 if (flag_sanitize & SANITIZE_ADDRESS)
3714 {
3715 varpool_node *vnode = varpool_node::get (decl);
3716 if (vnode)
3717 vnode->dynamically_initialized = 1;
3718 }
3719 }
3720
3721 /* If we're using __cxa_atexit, register a function that calls the
3722 destructor for the object. */
3723 if (flag_use_cxa_atexit)
3724 finish_expr_stmt (register_dtor_fn (decl));
3725 }
3726 else
3727 finish_expr_stmt (build_cleanup (decl));
3728
3729 /* Finish the guard if-stmt, if necessary. */
3730 if (guard)
3731 {
3732 finish_then_clause (guard_if_stmt);
3733 finish_if_stmt (guard_if_stmt);
3734 }
3735
3736 /* Now that we're done with DECL we don't need to pretend to be a
3737 member of its class any longer. */
3738 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3739 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3740}
3741
3742/* Generate code to do the initialization or destruction of the decls in VARS,
3743 a TREE_LIST of VAR_DECL with static storage duration.
3744 Whether initialization or destruction is performed is specified by INITP. */
3745
3746static void
3747do_static_initialization_or_destruction (tree vars, bool initp)
3748{
3749 tree node, init_if_stmt, cond;
3750
3751 /* Build the outer if-stmt to check for initialization or destruction. */
3752 init_if_stmt = begin_if_stmt ();
3753 cond = initp ? integer_one_node : integer_zero_node;
3754 cond = cp_build_binary_op (input_location,
3755 EQ_EXPR,
3756 initialize_p_decl,
3757 cond,
3758 tf_warning_or_error);
3759 finish_if_stmt_cond (cond, init_if_stmt);
3760
3761 /* To make sure dynamic construction doesn't access globals from other
3762 compilation units where they might not be yet constructed, for
3763 -fsanitize=address insert __asan_before_dynamic_init call that
3764 prevents access to either all global variables that need construction
3765 in other compilation units, or at least those that haven't been
3766 initialized yet. Variables that need dynamic construction in
3767 the current compilation unit are kept accessible. */
3768 if (flag_sanitize & SANITIZE_ADDRESS)
3769 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3770
3771 node = vars;
3772 do {
3773 tree decl = TREE_VALUE (node);
3774 tree priority_if_stmt;
3775 int priority;
3776 priority_info pi;
3777
3778 /* If we don't need a destructor, there's nothing to do. Avoid
3779 creating a possibly empty if-stmt. */
3780 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3781 {
3782 node = TREE_CHAIN (node);
3783 continue;
3784 }
3785
3786 /* Remember that we had an initialization or finalization at this
3787 priority. */
3788 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3789 pi = get_priority_info (priority);
3790 if (initp)
3791 pi->initializations_p = 1;
3792 else
3793 pi->destructions_p = 1;
3794
3795 /* Conditionalize this initialization on being in the right priority
3796 and being initializing/finalizing appropriately. */
3797 priority_if_stmt = begin_if_stmt ();
3798 cond = cp_build_binary_op (input_location,
3799 EQ_EXPR,
3800 priority_decl,
3801 build_int_cst (NULL_TREE, priority),
3802 tf_warning_or_error);