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)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
31 #include "coretypes.h"
36 #include "double-int.h"
43 #include "stringpool.h"
46 #include "stor-layout.h"
55 #include "c-family/c-common.h"
56 #include "c-family/c-objc.h"
59 #include "plugin-api.h"
60 #include "hard-reg-set.h"
65 #include "tree-inline.h"
66 #include "c-family/c-pragma.h"
69 #include "splay-tree.h"
70 #include "langhooks.h"
71 #include "c-family/c-ada-spec.h"
74 extern cpp_reader *parse_in;
76 /* This structure contains information about the initializations
77 and/or destructions required for a particular priority level. */
78 typedef struct priority_info_s {
79 /* Nonzero if there have been any initializations at this priority
80 throughout the translation unit. */
81 int initializations_p;
82 /* Nonzero if there have been any destructions at this priority
83 throughout the translation unit. */
87 static void mark_vtable_entries (tree);
88 static bool maybe_emit_vtables (tree);
89 static bool acceptable_java_type (tree);
90 static tree start_objects (int, int);
91 static void finish_objects (int, int, tree);
92 static tree start_static_storage_duration_function (unsigned);
93 static void finish_static_storage_duration_function (tree);
94 static priority_info get_priority_info (int);
95 static void do_static_initialization_or_destruction (tree, bool);
96 static void one_static_initialization_or_destruction (tree, tree, bool);
97 static void generate_ctor_or_dtor_function (bool, int, location_t *);
98 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
100 static tree prune_vars_needing_no_initialization (tree *);
101 static void write_out_vars (tree);
102 static void import_export_class (tree);
103 static tree get_guard_bits (tree);
104 static void determine_visibility_from_class (tree, tree);
105 static bool determine_hidden_inline (tree);
106 static bool decl_defined_p (tree);
108 /* A list of static class variables. This is needed, because a
109 static class variable can be declared inside the class without
110 an initializer, and then initialized, statically, outside the class. */
111 static GTY(()) vec<tree, va_gc> *pending_statics;
113 /* A list of functions which were declared inline, but which we
114 may need to emit outline anyway. */
115 static GTY(()) vec<tree, va_gc> *deferred_fns;
117 /* A list of decls that use types with no linkage, which we need to make
119 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
121 /* Nonzero if we're done parsing and into end-of-file activities. */
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). */
131 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
132 cp_ref_qualifier rqual)
137 bool late_return_type_p;
139 if (fntype == error_mark_node || ctype == error_mark_node)
140 return error_mark_node;
142 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
143 || TREE_CODE (fntype) == METHOD_TYPE);
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)));
155 fntype = cp_build_type_attribute_variant (fntype, attrs);
157 fntype = build_ref_qualified_type (fntype, rqual);
159 fntype = build_exception_variant (fntype, raises);
160 if (late_return_type_p)
161 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
166 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
167 return type changed to NEW_RET. */
170 change_return_type (tree new_ret, tree fntype)
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);
178 if (new_ret == error_mark_node)
181 if (same_type_p (new_ret, TREE_TYPE (fntype)))
184 if (TREE_CODE (fntype) == FUNCTION_TYPE)
186 newtype = build_function_type (new_ret, args);
187 newtype = apply_memfn_quals (newtype,
188 type_memfn_quals (fntype),
189 type_memfn_rqual (fntype));
192 newtype = build_method_type_directly
193 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
195 newtype = build_exception_variant (newtype, raises);
197 newtype = cp_build_type_attribute_variant (newtype, attrs);
198 if (late_return_type_p)
199 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
204 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
208 cp_build_parm_decl (tree name, tree type)
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
214 if (!processing_template_decl)
215 DECL_ARG_TYPE (parm) = type_passed_as (type);
220 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
224 build_artificial_parm (tree name, tree type)
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
230 TREE_READONLY (parm) = 1;
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.
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.
244 The in-charge flag follows the 'this' parameter, and is followed by the
245 VTT parm (if any), then the user-written parms. */
248 maybe_retrofit_in_chrg (tree fn)
250 tree basetype, arg_types, parms, parm, fntype;
252 /* If we've already add the in-charge parameter don't do it again. */
253 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
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)
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)))
267 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
268 basetype = TREE_TYPE (TREE_VALUE (arg_types));
269 arg_types = TREE_CHAIN (arg_types);
271 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
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)))
277 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
279 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
280 DECL_CHAIN (parm) = parms;
283 /* ...and then to TYPE_ARG_TYPES. */
284 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
286 DECL_HAS_VTT_PARM_P (fn) = 1;
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;
293 arg_types = hash_tree_chain (integer_type_node, arg_types);
295 /* Insert our new parameter(s) into the list. */
296 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
298 /* And rebuild the function type. */
299 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
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;
309 /* Now we've got the in-charge parameter. */
310 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
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
319 CNAME is the name of the class we are grokking for.
321 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
323 FLAGS contains bits saying what's special about today's
324 arguments. DTOR_FLAG == DESTRUCTOR.
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.
331 QUALS are the qualifiers for the this pointer. */
334 grokclassfn (tree ctype, tree function, enum overload_flags flags)
336 tree fn_name = DECL_NAME (function);
338 /* Even within an `extern "C"' block, members get C++ linkage. See
339 [dcl.link] for details. */
340 SET_DECL_LANGUAGE (function, lang_cplusplus);
342 if (fn_name == NULL_TREE)
344 error ("name missing for member function");
345 fn_name = get_identifier ("<anonymous>");
346 DECL_NAME (function) = fn_name;
349 DECL_CONTEXT (function) = ctype;
351 if (flags == DTOR_FLAG)
352 DECL_DESTRUCTOR_P (function) = 1;
354 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
355 maybe_retrofit_in_chrg (function);
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. */
362 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
367 tree orig_array_expr = array_expr;
368 tree orig_index_exp = index_exp;
370 if (error_operand_p (array_expr) || error_operand_p (index_exp))
371 return error_mark_node;
373 if (processing_template_decl)
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);
383 type = TREE_TYPE (array_expr);
385 type = non_reference (type);
387 /* If they have an `operator[]', use that. */
388 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
390 tsubst_flags_t complain = tf_warning_or_error;
392 complain |= tf_decltype;
393 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
394 index_exp, NULL_TREE, /*overload=*/NULL, complain);
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
404 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
407 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
409 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
412 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
414 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
416 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
419 if ((p1 && i2) && (i1 && p2))
420 error ("ambiguous conversion for array subscript");
423 array_expr = p1, index_exp = i2;
425 array_expr = p2, index_exp = i1;
428 error ("invalid types %<%T[%T]%> for array subscript",
429 type, TREE_TYPE (index_exp));
430 return error_mark_node;
433 if (array_expr == error_mark_node || index_exp == error_mark_node)
434 error ("ambiguous conversion for array subscript");
436 expr = build_array_ref (input_location, array_expr, index_exp);
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);
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. */
452 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
453 tsubst_flags_t complain)
457 if (exp == error_mark_node)
460 if (processing_template_decl)
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;
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);
473 t = build_expr_type_conversion (WANT_POINTER, exp, true);
475 if (t == NULL_TREE || t == error_mark_node)
477 error ("type %q#T argument given to %<delete%>, expected pointer",
479 return error_mark_node;
482 type = TREE_TYPE (t);
484 /* As of Valley Forge, you can delete a pointer to const. */
486 /* You can't delete functions. */
487 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
489 error ("cannot delete a function. Only pointer-to-objects are "
490 "valid arguments to %<delete%>");
491 return error_mark_node;
494 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
495 if (VOID_TYPE_P (TREE_TYPE (type)))
497 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
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);
506 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
507 sfk_deleting_destructor,
508 use_global_delete, complain);
510 return build_delete (type, t, sfk_deleting_destructor,
511 LOOKUP_NORMAL, use_global_delete,
515 /* Report an error if the indicated template declaration is not the
516 sort of thing that should be a member template. */
519 check_member_template (tree tmpl)
523 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
524 decl = DECL_TEMPLATE_RESULT (tmpl);
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))))
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)));
538 /* The debug-information generating code doesn't know what to do
539 with member templates. */
540 DECL_IGNORED_P (tmpl) = 1;
542 else if (variable_template_p (tmpl))
545 error ("template declaration of %q#D", decl);
548 /* Return true iff TYPE is a valid Java parameter or return type. */
551 acceptable_java_type (tree type)
553 if (type == error_mark_node)
556 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
558 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
560 type = TREE_TYPE (type);
561 if (TREE_CODE (type) == RECORD_TYPE)
564 if (! TYPE_FOR_JAVA (type))
566 if (! CLASSTYPE_TEMPLATE_INFO (type))
568 args = CLASSTYPE_TI_ARGS (type);
569 i = TREE_VEC_LENGTH (args);
572 type = TREE_VEC_ELT (args, i);
573 if (TYPE_PTR_P (type))
574 type = TREE_TYPE (type);
575 if (! TYPE_FOR_JAVA (type))
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. */
589 check_java_method (tree method)
592 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
593 tree ret_type = TREE_TYPE (TREE_TYPE (method));
595 if (!acceptable_java_type (ret_type))
597 error ("Java method %qD has non-Java return type %qT",
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);
608 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
610 tree type = TREE_VALUE (arg_types);
611 if (!acceptable_java_type (type))
613 if (type != error_mark_node)
614 error ("Java method %qD has non-Java parameter type %qT",
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
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. */
633 check_classfn (tree ctype, tree function, tree template_parms)
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.
647 struct S { template <typename T> void f(T); };
648 template <> void S::f(int);
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. */
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)
660 && !comp_template_parms (template_parms,
661 DECL_TEMPLATE_PARMS (function)))
663 error ("template parameter lists provided don%'t match the "
664 "template parameters of %qD", function);
665 return error_mark_node;
667 template_parms = DECL_TEMPLATE_PARMS (function);
670 /* OK, is this a definition of a member template? */
671 is_template = (template_parms != NULL_TREE);
675 A destructor shall not be a member template. */
676 if (DECL_DESTRUCTOR_P (function) && is_template)
678 error ("destructor %qD declared as member template", function);
679 return error_mark_node;
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);
689 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
690 tree fndecls, fndecl = 0;
692 const char *format = NULL;
694 for (fndecls = (*methods)[ix];
695 fndecls; fndecls = OVL_NEXT (fndecls))
699 fndecl = OVL_CURRENT (fndecls);
700 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
701 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
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
708 /* Get rid of the this parameter on functions that become
710 if (DECL_STATIC_FUNCTION_P (fndecl)
711 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
712 p1 = TREE_CHAIN (p1);
714 /* A member template definition only matches a member template
716 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
719 /* ref-qualifier or absence of same must match. */
720 if (type_memfn_rqual (TREE_TYPE (function))
721 != type_memfn_rqual (TREE_TYPE (fndecl)))
724 /* While finding a match, same types and params are not enough
725 if the function is versioned. Also check version ("target")
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)
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))))
744 pop_scope (pushed_scope);
745 return OVL_CURRENT (fndecls);
748 error_at (DECL_SOURCE_LOCATION (function),
749 "prototype for %q#D does not match any in class %qT",
751 is_conv_op = DECL_CONV_FN_P (fndecl);
754 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
755 fndecls = (*methods)[ix];
758 fndecl = OVL_CURRENT (fndecls);
759 fndecls = OVL_NEXT (fndecls);
761 if (!fndecls && is_conv_op)
763 if (methods->length () > (size_t) ++ix)
765 fndecls = (*methods)[ix];
766 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
778 format = N_("candidates are: %+#D");
780 format = N_("candidate is: %+#D");
781 error (format, fndecl);
784 else if (!COMPLETE_TYPE_P (ctype))
785 cxx_incomplete_type_error (function, ctype);
787 error ("no %q#D member function declared in class %qT",
791 pop_scope (pushed_scope);
792 return error_mark_node;
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
800 note_vague_linkage_fn (tree decl)
802 DECL_DEFER_OUTPUT (decl) = 1;
803 vec_safe_push (deferred_fns, decl);
806 /* As above, but for variable template instantiations. */
809 note_variable_template_instantiation (tree decl)
811 vec_safe_push (pending_statics, decl);
814 /* We have just processed the DECL, which is a static data member.
815 The other parameters are as for cp_finish_decl. */
818 finish_static_data_member_decl (tree decl,
819 tree init, bool init_const_expr_p,
823 DECL_CONTEXT (decl) = current_class_type;
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. */
829 if (! processing_template_decl)
830 vec_safe_push (pending_statics, decl);
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);
838 for (tree t = current_class_type; TYPE_P (t);
839 t = CP_TYPE_CONTEXT (t))
840 if (TYPE_ANONYMOUS_P (t))
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");
849 DECL_IN_AGGR_P (decl) = 1;
851 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
852 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
853 SET_VAR_HAD_UNKNOWN_BOUND (decl);
855 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
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. */
863 grokfield (const cp_declarator *declarator,
864 cp_decl_specifier_seq *declspecs,
865 tree init, bool init_const_expr_p,
870 const char *asmspec = 0;
875 && TREE_CODE (init) == TREE_LIST
876 && TREE_VALUE (init) == error_mark_node
877 && TREE_CHAIN (init) == NULL_TREE)
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)
887 if (TREE_CODE (value) == TYPE_DECL && init)
889 error ("typedef %qD is initialized (use decltype instead)", value);
893 /* Pass friendly classes back. */
894 if (value == void_type_node)
898 name = DECL_NAME (value);
900 if (name != NULL_TREE)
902 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
904 error ("explicit template argument list not allowed");
905 return error_mark_node;
908 if (IDENTIFIER_POINTER (name)[0] == '_'
909 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
910 error ("member %qD conflicts with virtual function table field name",
914 /* Stash away type declarations. */
915 if (TREE_CODE (value) == TYPE_DECL)
917 DECL_NONLOCAL (value) = 1;
918 DECL_CONTEXT (value) = current_class_type;
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;
930 cplus_decl_attributes (&value, attrlist, attrflags);
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);
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);
944 record_locally_defined_typedef (value);
948 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
950 if (!friendp && DECL_IN_AGGR_P (value))
952 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
953 return void_type_node;
956 if (asmspec_tree && asmspec_tree != error_mark_node)
957 asmspec = TREE_STRING_POINTER (asmspec_tree);
961 if (TREE_CODE (value) == FUNCTION_DECL)
963 if (init == ridpointers[(int)RID_DELETE])
965 DECL_DELETED_FN (value) = 1;
966 DECL_DECLARED_INLINE_P (value) = 1;
967 DECL_INITIAL (value) = error_mark_node;
969 else if (init == ridpointers[(int)RID_DEFAULT])
971 if (defaultable_fn_check (value))
973 DECL_DEFAULTED_FN (value) = 1;
974 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
975 DECL_DECLARED_INLINE_P (value) = 1;
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)
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. */
987 error ("invalid initializer for member function %qD",
992 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
994 error ("initializer specified for friend function %qD",
997 error ("initializer specified for static member function %qD",
1001 else if (TREE_CODE (value) == FIELD_DECL)
1002 /* C++11 NSDMI, keep going. */;
1003 else if (!VAR_P (value))
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)
1013 /* Need to set this before push_template_decl. */
1014 if (TREE_CODE (value) == VAR_DECL)
1015 DECL_CONTEXT (value) = current_class_type;
1017 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1019 value = push_template_decl (value);
1020 if (error_operand_p (value))
1021 return error_mark_node;
1025 cplus_decl_attributes (&value, attrlist, 0);
1027 if (init && DIRECT_LIST_INIT_P (init))
1028 flags = LOOKUP_NORMAL;
1030 flags = LOOKUP_IMPLICIT;
1032 switch (TREE_CODE (value))
1035 finish_static_data_member_decl (value, init, init_const_expr_p,
1036 asmspec_tree, flags);
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,
1046 DECL_IN_AGGR_P (value) = 1;
1051 set_user_assembler_name (value, asmspec);
1053 cp_finish_decl (value,
1055 /*init_const_expr_p=*/false,
1056 asmspec_tree, flags);
1058 /* Pass friends back this way. */
1059 if (DECL_FRIEND_P (value))
1060 return void_type_node;
1062 DECL_IN_AGGR_P (value) = 1;
1071 /* Like `grokfield', but for bitfields.
1072 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1075 grokbitfield (const cp_declarator *declarator,
1076 cp_decl_specifier_seq *declspecs, tree width,
1079 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1081 if (value == error_mark_node)
1082 return NULL_TREE; /* friends went bad. */
1083 if (TREE_TYPE (value) == error_mark_node)
1086 /* Pass friendly classes back. */
1087 if (VOID_TYPE_P (value))
1088 return void_type_node;
1090 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1091 && (POINTER_TYPE_P (value)
1092 || !dependent_type_p (TREE_TYPE (value))))
1094 error ("bit-field %qD with non-integral type", value);
1095 return error_mark_node;
1098 if (TREE_CODE (value) == TYPE_DECL)
1100 error ("cannot declare %qD to be a bit-field type", value);
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
1108 if (TREE_CODE (value) == FUNCTION_DECL)
1110 error ("cannot declare bit-field %qD with function type",
1115 if (DECL_IN_AGGR_P (value))
1117 error ("%qD is already defined in the class %qT", value,
1118 DECL_CONTEXT (value));
1119 return void_type_node;
1122 if (TREE_STATIC (value))
1124 error ("static member %qD cannot be a bit-field", value);
1127 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1129 if (width != error_mark_node)
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,
1136 DECL_INITIAL (value) = width;
1137 SET_DECL_C_BIT_FIELD (value);
1140 DECL_IN_AGGR_P (value) = 1;
1143 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1149 /* Returns true iff ATTR is an attribute which needs to be applied at
1150 instantiation time rather than template definition time. */
1153 is_late_template_attribute (tree attr, tree decl)
1155 tree name = get_attribute_name (attr);
1156 tree args = TREE_VALUE (attr);
1157 const struct attribute_spec *spec = lookup_attribute_spec (name);
1161 /* Unknown attribute. */
1164 /* Attribute weak handling wants to write out assembly right away. */
1165 if (is_attribute_p ("weak", name))
1168 /* Attribute unused is applied directly, as it appertains to
1170 if (is_attribute_p ("unused", name))
1173 /* #pragma omp declare simd attribute needs to be always deferred. */
1175 && is_attribute_p ("omp declare simd", name))
1178 /* An attribute pack is clearly dependent. */
1179 if (args && PACK_EXPANSION_P (args))
1182 /* If any of the arguments are dependent expressions, we can't evaluate
1183 the attribute until instantiation time. */
1184 for (arg = args; arg; arg = TREE_CHAIN (arg))
1186 tree t = TREE_VALUE (arg);
1188 /* If the first attribute argument is an identifier, only consider
1189 second and following arguments. Attributes like mode, format,
1190 cleanup and several target specific attributes aren't late
1191 just because they have an IDENTIFIER_NODE as first argument. */
1192 if (arg == args && identifier_p (t))
1195 if (value_dependent_expression_p (t)
1196 || type_dependent_expression_p (t))
1200 if (TREE_CODE (decl) == TYPE_DECL
1202 || spec->type_required)
1204 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1206 /* We can't apply any attributes to a completely unknown type until
1207 instantiation time. */
1208 enum tree_code code = TREE_CODE (type);
1209 if (code == TEMPLATE_TYPE_PARM
1210 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1211 || code == TYPENAME_TYPE)
1213 /* Also defer most attributes on dependent types. This is not
1214 necessary in all cases, but is the better default. */
1215 else if (dependent_type_p (type)
1216 /* But some attributes specifically apply to templates. */
1217 && !is_attribute_p ("abi_tag", name)
1218 && !is_attribute_p ("deprecated", name)
1219 && !is_attribute_p ("visibility", name))
1228 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1229 applied at instantiation time and return them. If IS_DEPENDENT is true,
1230 the declaration itself is dependent, so all attributes should be applied
1231 at instantiation time. */
1234 splice_template_attributes (tree *attr_p, tree decl)
1237 tree late_attrs = NULL_TREE;
1238 tree *q = &late_attrs;
1245 if (is_late_template_attribute (*p, decl))
1247 ATTR_IS_DEPENDENT (*p) = 1;
1249 *p = TREE_CHAIN (*p);
1250 q = &TREE_CHAIN (*q);
1254 p = &TREE_CHAIN (*p);
1260 /* Remove any late attributes from the list in ATTR_P and attach them to
1264 save_template_attributes (tree *attr_p, tree *decl_p)
1268 if (attr_p && *attr_p == error_mark_node)
1271 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1275 if (DECL_P (*decl_p))
1276 q = &DECL_ATTRIBUTES (*decl_p);
1278 q = &TYPE_ATTRIBUTES (*decl_p);
1280 tree old_attrs = *q;
1282 /* Merge the late attributes at the beginning with the attribute
1284 late_attrs = merge_attributes (late_attrs, *q);
1287 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1289 /* We've added new attributes directly to the main variant, so
1290 now we need to update all of the other variants to include
1291 these new attributes. */
1293 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1294 variant = TYPE_NEXT_VARIANT (variant))
1296 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1297 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1302 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1303 to a typedef which gives a previously anonymous class or enum a name for
1304 linkage purposes. */
1307 attributes_naming_typedef_ok (tree attrs)
1309 for (; attrs; attrs = TREE_CHAIN (attrs))
1311 tree name = get_attribute_name (attrs);
1312 if (is_attribute_p ("vector_size", name))
1318 /* Like reconstruct_complex_type, but handle also template trees. */
1321 cp_reconstruct_complex_type (tree type, tree bottom)
1324 bool late_return_type_p = false;
1326 if (TYPE_PTR_P (type))
1328 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1329 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1330 TYPE_REF_CAN_ALIAS_ALL (type));
1332 else if (TREE_CODE (type) == REFERENCE_TYPE)
1334 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1335 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1336 TYPE_REF_CAN_ALIAS_ALL (type));
1338 else if (TREE_CODE (type) == ARRAY_TYPE)
1340 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1341 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1342 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1343 element type qualification will be handled by the recursive
1344 cp_reconstruct_complex_type call and cp_build_qualified_type
1345 for ARRAY_TYPEs changes the element type. */
1348 else if (TREE_CODE (type) == FUNCTION_TYPE)
1350 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1351 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1352 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1353 outer = apply_memfn_quals (outer,
1354 type_memfn_quals (type),
1355 type_memfn_rqual (type));
1357 else if (TREE_CODE (type) == METHOD_TYPE)
1359 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1360 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1361 /* The build_method_type_directly() routine prepends 'this' to argument list,
1362 so we must compensate by getting rid of it. */
1364 = build_method_type_directly
1365 (class_of_this_parm (type), inner,
1366 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1368 else if (TREE_CODE (type) == OFFSET_TYPE)
1370 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1371 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1376 if (TYPE_ATTRIBUTES (type))
1377 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1378 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1380 if (late_return_type_p)
1381 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1386 /* Replaces any constexpr expression that may be into the attributes
1387 arguments with their reduced value. */
1390 cp_check_const_attributes (tree attributes)
1392 if (attributes == error_mark_node)
1396 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1399 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1401 tree expr = TREE_VALUE (arg);
1403 TREE_VALUE (arg) = maybe_constant_value (expr);
1408 /* Return true if TYPE is an OpenMP mappable type. */
1410 cp_omp_mappable_type (tree type)
1412 /* Mappable type has to be complete. */
1413 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1415 /* Arrays have mappable type if the elements have mappable type. */
1416 while (TREE_CODE (type) == ARRAY_TYPE)
1417 type = TREE_TYPE (type);
1418 /* A mappable type cannot contain virtual members. */
1419 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1421 /* All data members must be non-static. */
1422 if (CLASS_TYPE_P (type))
1425 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1426 if (TREE_CODE (field) == VAR_DECL)
1428 /* All fields must have mappable types. */
1429 else if (TREE_CODE (field) == FIELD_DECL
1430 && !cp_omp_mappable_type (TREE_TYPE (field)))
1436 /* Like decl_attributes, but handle C++ complexity. */
1439 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1441 if (*decl == NULL_TREE || *decl == void_type_node
1442 || *decl == error_mark_node)
1445 /* Add implicit "omp declare target" attribute if requested. */
1446 if (scope_chain->omp_declare_target_attribute
1447 && ((TREE_CODE (*decl) == VAR_DECL
1448 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1449 || TREE_CODE (*decl) == FUNCTION_DECL))
1451 if (TREE_CODE (*decl) == VAR_DECL
1452 && DECL_CLASS_SCOPE_P (*decl))
1453 error ("%q+D static data member inside of declare target directive",
1455 else if (TREE_CODE (*decl) == VAR_DECL
1456 && (DECL_FUNCTION_SCOPE_P (*decl)
1457 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1458 error ("%q+D in block scope inside of declare target directive",
1460 else if (!processing_template_decl
1461 && TREE_CODE (*decl) == VAR_DECL
1462 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1463 error ("%q+D in declare target directive does not have mappable type",
1466 attributes = tree_cons (get_identifier ("omp declare target"),
1467 NULL_TREE, attributes);
1470 if (processing_template_decl)
1472 if (check_for_bare_parameter_packs (attributes))
1475 save_template_attributes (&attributes, decl);
1478 cp_check_const_attributes (attributes);
1480 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1481 decl = &DECL_TEMPLATE_RESULT (*decl);
1483 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1486 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1487 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1491 decl_attributes (decl, attributes, flags);
1493 if (TREE_CODE (*decl) == TYPE_DECL)
1494 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1496 /* Propagate deprecation out to the template. */
1497 if (TREE_DEPRECATED (*decl))
1498 if (tree ti = get_template_info (*decl))
1500 tree tmpl = TI_TEMPLATE (ti);
1501 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1502 : DECL_TEMPLATE_RESULT (tmpl));
1503 if (*decl == pattern)
1504 TREE_DEPRECATED (tmpl) = true;
1508 /* Walks through the namespace- or function-scope anonymous union
1509 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1510 Returns one of the fields for use in the mangled name. */
1513 build_anon_union_vars (tree type, tree object)
1515 tree main_decl = NULL_TREE;
1518 /* Rather than write the code to handle the non-union case,
1519 just give an error. */
1520 if (TREE_CODE (type) != UNION_TYPE)
1522 error ("anonymous struct not inside named type");
1523 return error_mark_node;
1526 for (field = TYPE_FIELDS (type);
1528 field = DECL_CHAIN (field))
1533 if (DECL_ARTIFICIAL (field))
1535 if (TREE_CODE (field) != FIELD_DECL)
1537 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1538 "have non-static data members", field);
1542 if (TREE_PRIVATE (field))
1543 permerror (input_location, "private member %q+#D in anonymous union", field);
1544 else if (TREE_PROTECTED (field))
1545 permerror (input_location, "protected member %q+#D in anonymous union", field);
1547 if (processing_template_decl)
1548 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1549 DECL_NAME (field), NULL_TREE);
1551 ref = build_class_member_access_expr (object, field, NULL_TREE,
1552 false, tf_warning_or_error);
1554 if (DECL_NAME (field))
1558 decl = build_decl (input_location,
1559 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1560 DECL_ANON_UNION_VAR_P (decl) = 1;
1561 DECL_ARTIFICIAL (decl) = 1;
1563 base = get_base_address (object);
1564 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1565 TREE_STATIC (decl) = TREE_STATIC (base);
1566 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1568 SET_DECL_VALUE_EXPR (decl, ref);
1569 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1571 decl = pushdecl (decl);
1573 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1574 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1578 if (main_decl == NULL_TREE)
1585 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1586 anonymous union, then all members must be laid out together. PUBLIC_P
1587 is nonzero if this union is not declared static. */
1590 finish_anon_union (tree anon_union_decl)
1596 if (anon_union_decl == error_mark_node)
1599 type = TREE_TYPE (anon_union_decl);
1600 public_p = TREE_PUBLIC (anon_union_decl);
1602 /* The VAR_DECL's context is the same as the TYPE's context. */
1603 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1605 if (TYPE_FIELDS (type) == NULL_TREE)
1610 error ("namespace-scope anonymous aggregates must be static");
1614 main_decl = build_anon_union_vars (type, anon_union_decl);
1615 if (main_decl == error_mark_node)
1617 if (main_decl == NULL_TREE)
1619 warning (0, "anonymous union with no members");
1623 if (!processing_template_decl)
1625 /* Use main_decl to set the mangled name. */
1626 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1627 maybe_commonize_var (anon_union_decl);
1628 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1629 mangle_decl (anon_union_decl);
1630 DECL_NAME (anon_union_decl) = NULL_TREE;
1633 pushdecl (anon_union_decl);
1634 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1637 /* Auxiliary functions to make type signatures for
1638 `operator new' and `operator delete' correspond to
1639 what compiler will be expecting. */
1642 coerce_new_type (tree type)
1645 tree args = TYPE_ARG_TYPES (type);
1647 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1649 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1652 error ("%<operator new%> must return type %qT", ptr_type_node);
1655 if (args && args != void_list_node)
1657 if (TREE_PURPOSE (args))
1659 /* [basic.stc.dynamic.allocation]
1661 The first parameter shall not have an associated default
1663 error ("the first parameter of %<operator new%> cannot "
1664 "have a default argument");
1665 /* Throw away the default argument. */
1666 TREE_PURPOSE (args) = NULL_TREE;
1669 if (!same_type_p (TREE_VALUE (args), size_type_node))
1672 args = TREE_CHAIN (args);
1679 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1680 "as first parameter", size_type_node);
1685 args = tree_cons (NULL_TREE, size_type_node, args);
1688 type = build_exception_variant
1689 (build_function_type (ptr_type_node, args),
1690 TYPE_RAISES_EXCEPTIONS (type));
1698 coerce_delete_type (tree type)
1701 tree args = TYPE_ARG_TYPES (type);
1703 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1705 if (!same_type_p (TREE_TYPE (type), void_type_node))
1708 error ("%<operator delete%> must return type %qT", void_type_node);
1711 if (!args || args == void_list_node
1712 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1715 if (args && args != void_list_node)
1716 args = TREE_CHAIN (args);
1717 error ("%<operator delete%> takes type %qT as first parameter",
1723 args = tree_cons (NULL_TREE, ptr_type_node, args);
1726 type = build_exception_variant
1727 (build_function_type (void_type_node, args),
1728 TYPE_RAISES_EXCEPTIONS (type));
1736 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1737 and mark them as needed. */
1740 mark_vtable_entries (tree decl)
1743 unsigned HOST_WIDE_INT idx;
1745 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1750 STRIP_NOPS (fnaddr);
1752 if (TREE_CODE (fnaddr) != ADDR_EXPR
1753 && TREE_CODE (fnaddr) != FDESC_EXPR)
1754 /* This entry is an offset: a virtual base class offset, a
1755 virtual call offset, an RTTI offset, etc. */
1758 fn = TREE_OPERAND (fnaddr, 0);
1759 TREE_ADDRESSABLE (fn) = 1;
1760 /* When we don't have vcall offsets, we output thunks whenever
1761 we output the vtables that contain them. With vcall offsets,
1762 we know all the thunks we'll need when we emit a virtual
1763 function, so we emit the thunks there instead. */
1764 if (DECL_THUNK_P (fn))
1765 use_thunk (fn, /*emit_p=*/0);
1770 /* Set DECL up to have the closest approximation of "initialized common"
1771 linkage available. */
1774 comdat_linkage (tree decl)
1777 make_decl_one_only (decl, cxx_comdat_group (decl));
1778 else if (TREE_CODE (decl) == FUNCTION_DECL
1779 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1780 /* We can just emit function and compiler-generated variables
1781 statically; having multiple copies is (for the most part) only
1784 There are two correctness issues, however: the address of a
1785 template instantiation with external linkage should be the
1786 same, independent of what translation unit asks for the
1787 address, and this will not hold when we emit multiple copies of
1788 the function. However, there's little else we can do.
1790 Also, by default, the typeinfo implementation assumes that
1791 there will be only one copy of the string used as the name for
1792 each type. Therefore, if weak symbols are unavailable, the
1793 run-time library should perform a more conservative check; it
1794 should perform a string comparison, rather than an address
1796 TREE_PUBLIC (decl) = 0;
1799 /* Static data member template instantiations, however, cannot
1800 have multiple copies. */
1801 if (DECL_INITIAL (decl) == 0
1802 || DECL_INITIAL (decl) == error_mark_node)
1803 DECL_COMMON (decl) = 1;
1804 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1806 DECL_COMMON (decl) = 1;
1807 DECL_INITIAL (decl) = error_mark_node;
1809 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1811 /* We can't do anything useful; leave vars for explicit
1813 DECL_EXTERNAL (decl) = 1;
1814 DECL_NOT_REALLY_EXTERN (decl) = 0;
1818 DECL_COMDAT (decl) = 1;
1821 /* For win32 we also want to put explicit instantiations in
1822 linkonce sections, so that they will be merged with implicit
1823 instantiations; otherwise we get duplicate symbol errors.
1824 For Darwin we do not want explicit instantiations to be
1828 maybe_make_one_only (tree decl)
1830 /* We used to say that this was not necessary on targets that support weak
1831 symbols, because the implicit instantiations will defer to the explicit
1832 one. However, that's not actually the case in SVR4; a strong definition
1833 after a weak one is an error. Also, not making explicit
1834 instantiations one_only means that we can end up with two copies of
1835 some template instantiations. */
1839 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1840 we can get away with not emitting them if they aren't used. We need
1841 to for variables so that cp_finish_decl will update their linkage,
1842 because their DECL_INITIAL may not have been set properly yet. */
1844 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1845 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1846 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1848 make_decl_one_only (decl, cxx_comdat_group (decl));
1852 varpool_node *node = varpool_node::get_create (decl);
1853 DECL_COMDAT (decl) = 1;
1854 /* Mark it needed so we don't forget to emit it. */
1855 node->forced_by_abi = true;
1856 TREE_USED (decl) = 1;
1861 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1862 This predicate will give the right answer during parsing of the
1863 function, which other tests may not. */
1866 vague_linkage_p (tree decl)
1868 /* Unfortunately, import_export_decl has not always been called
1869 before the function is processed, so we cannot simply check
1871 if (DECL_COMDAT (decl)
1872 || (((TREE_CODE (decl) == FUNCTION_DECL
1873 && DECL_DECLARED_INLINE_P (decl))
1874 || (DECL_LANG_SPECIFIC (decl)
1875 && DECL_TEMPLATE_INSTANTIATION (decl)))
1876 && TREE_PUBLIC (decl)))
1878 else if (DECL_FUNCTION_SCOPE_P (decl))
1879 /* A local static in an inline effectively has vague linkage. */
1880 return (TREE_STATIC (decl)
1881 && vague_linkage_p (DECL_CONTEXT (decl)));
1886 /* Determine whether or not we want to specifically import or export CTYPE,
1887 using various heuristics. */
1890 import_export_class (tree ctype)
1892 /* -1 for imported, 1 for exported. */
1893 int import_export = 0;
1895 /* It only makes sense to call this function at EOF. The reason is
1896 that this function looks at whether or not the first non-inline
1897 non-abstract virtual member function has been defined in this
1898 translation unit. But, we can't possibly know that until we've
1899 seen the entire translation unit. */
1900 gcc_assert (at_eof);
1902 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1905 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1906 we will have CLASSTYPE_INTERFACE_ONLY set but not
1907 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1908 heuristic because someone will supply a #pragma implementation
1909 elsewhere, and deducing it here would produce a conflict. */
1910 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1913 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1915 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1917 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1918 && !flag_implicit_templates)
1919 /* For a template class, without -fimplicit-templates, check the
1920 repository. If the virtual table is assigned to this
1921 translation unit, then export the class; otherwise, import
1923 import_export = repo_export_class_p (ctype) ? 1 : -1;
1924 else if (TYPE_POLYMORPHIC_P (ctype))
1926 /* The ABI specifies that the virtual table and associated
1927 information are emitted with the key method, if any. */
1928 tree method = CLASSTYPE_KEY_METHOD (ctype);
1929 /* If weak symbol support is not available, then we must be
1930 careful not to emit the vtable when the key function is
1931 inline. An inline function can be defined in multiple
1932 translation units. If we were to emit the vtable in each
1933 translation unit containing a definition, we would get
1934 multiple definition errors at link-time. */
1935 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1936 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1939 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1940 a definition anywhere else. */
1941 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1944 /* Allow back ends the chance to overrule the decision. */
1945 if (targetm.cxx.import_export_class)
1946 import_export = targetm.cxx.import_export_class (ctype, import_export);
1950 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1951 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1955 /* Return true if VAR has already been provided to the back end; in that
1956 case VAR should not be modified further by the front end. */
1958 var_finalized_p (tree var)
1960 return varpool_node::get_create (var)->definition;
1963 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1964 must be emitted in this translation unit. Mark it as such. */
1967 mark_needed (tree decl)
1969 TREE_USED (decl) = 1;
1970 if (TREE_CODE (decl) == FUNCTION_DECL)
1972 /* Extern inline functions don't become needed when referenced.
1973 If we know a method will be emitted in other TU and no new
1974 functions can be marked reachable, just use the external
1976 struct cgraph_node *node = cgraph_node::get_create (decl);
1977 node->forced_by_abi = true;
1979 /* #pragma interface and -frepo code can call mark_needed for
1980 maybe-in-charge 'tors; mark the clones as well. */
1982 FOR_EACH_CLONE (clone, decl)
1983 mark_needed (clone);
1985 else if (TREE_CODE (decl) == VAR_DECL)
1987 varpool_node *node = varpool_node::get_create (decl);
1988 /* C++ frontend use mark_decl_references to force COMDAT variables
1989 to be output that might appear dead otherwise. */
1990 node->forced_by_abi = true;
1994 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1995 returns true if a definition of this entity should be provided in
1996 this object file. Callers use this function to determine whether
1997 or not to let the back end know that a definition of DECL is
1998 available in this translation unit. */
2001 decl_needed_p (tree decl)
2003 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2004 /* This function should only be called at the end of the translation
2005 unit. We cannot be sure of whether or not something will be
2006 COMDAT until that point. */
2007 gcc_assert (at_eof);
2009 /* All entities with external linkage that are not COMDAT/EXTERN should be
2010 emitted; they may be referred to from other object files. */
2011 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2013 /* Functions marked "dllexport" must be emitted so that they are
2014 visible to other DLLs. */
2015 if (flag_keep_inline_dllexport
2016 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2019 /* When not optimizing, do not bother to produce definitions for extern
2021 if (DECL_REALLY_EXTERN (decl)
2022 && ((TREE_CODE (decl) != FUNCTION_DECL
2024 || (TREE_CODE (decl) == FUNCTION_DECL
2025 && !opt_for_fn (decl, optimize)))
2026 && !lookup_attribute ("always_inline", decl))
2029 /* If this entity was used, let the back end see it; it will decide
2030 whether or not to emit it into the object file. */
2031 if (TREE_USED (decl))
2033 /* Virtual functions might be needed for devirtualization. */
2034 if (flag_devirtualize
2035 && TREE_CODE (decl) == FUNCTION_DECL
2036 && DECL_VIRTUAL_P (decl))
2038 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2039 reference to DECL might cause it to be emitted later. */
2043 /* If necessary, write out the vtables for the dynamic class CTYPE.
2044 Returns true if any vtables were emitted. */
2047 maybe_emit_vtables (tree ctype)
2052 varpool_node *current = NULL, *last = NULL;
2054 /* If the vtables for this class have already been emitted there is
2055 nothing more to do. */
2056 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2057 if (var_finalized_p (primary_vtbl))
2059 /* Ignore dummy vtables made by get_vtable_decl. */
2060 if (TREE_TYPE (primary_vtbl) == void_type_node)
2063 /* On some targets, we cannot determine the key method until the end
2064 of the translation unit -- which is when this function is
2066 if (!targetm.cxx.key_method_may_be_inline ())
2067 determine_key_method (ctype);
2069 /* See if any of the vtables are needed. */
2070 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2072 import_export_decl (vtbl);
2073 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2078 /* If the references to this class' vtables are optimized away,
2079 still emit the appropriate debugging information. See
2081 if (DECL_COMDAT (primary_vtbl)
2082 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2083 note_debug_info_needed (ctype);
2087 /* The ABI requires that we emit all of the vtables if we emit any
2089 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2091 /* Mark entities references from the virtual table as used. */
2092 mark_vtable_entries (vtbl);
2094 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2096 vec<tree, va_gc> *cleanups = NULL;
2097 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2100 /* It had better be all done at compile-time. */
2101 gcc_assert (!expr && !cleanups);
2105 DECL_EXTERNAL (vtbl) = 0;
2106 rest_of_decl_compilation (vtbl, 1, 1);
2108 /* Because we're only doing syntax-checking, we'll never end up
2109 actually marking the variable as written. */
2110 if (flag_syntax_only)
2111 TREE_ASM_WRITTEN (vtbl) = 1;
2112 else if (DECL_ONE_ONLY (vtbl))
2114 current = varpool_node::get_create (vtbl);
2116 current->add_to_same_comdat_group (last);
2121 /* Since we're writing out the vtable here, also write the debug
2123 note_debug_info_needed (ctype);
2128 /* A special return value from type_visibility meaning internal
2131 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2133 /* walk_tree helper function for type_visibility. */
2136 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2138 int *vis_p = (int *)data;
2143 else if (OVERLOAD_TYPE_P (*tp)
2144 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2146 *vis_p = VISIBILITY_ANON;
2149 else if (CLASS_TYPE_P (*tp)
2150 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2151 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2155 /* Returns the visibility of TYPE, which is the minimum visibility of its
2159 type_visibility (tree type)
2161 int vis = VISIBILITY_DEFAULT;
2162 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2166 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2167 specified (or if VISIBILITY is static). If TMPL is true, this
2168 constraint is for a template argument, and takes precedence
2169 over explicitly-specified visibility on the template. */
2172 constrain_visibility (tree decl, int visibility, bool tmpl)
2174 if (visibility == VISIBILITY_ANON)
2176 /* extern "C" declarations aren't affected by the anonymous
2178 if (!DECL_EXTERN_C_P (decl))
2180 TREE_PUBLIC (decl) = 0;
2181 DECL_WEAK (decl) = 0;
2182 DECL_COMMON (decl) = 0;
2183 DECL_COMDAT (decl) = false;
2184 if (TREE_CODE (decl) == FUNCTION_DECL
2185 || TREE_CODE (decl) == VAR_DECL)
2187 struct symtab_node *snode = symtab_node::get (decl);
2190 snode->set_comdat_group (NULL);
2192 DECL_INTERFACE_KNOWN (decl) = 1;
2193 if (DECL_LANG_SPECIFIC (decl))
2194 DECL_NOT_REALLY_EXTERN (decl) = 1;
2197 else if (visibility > DECL_VISIBILITY (decl)
2198 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2200 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2201 /* This visibility was not specified. */
2202 DECL_VISIBILITY_SPECIFIED (decl) = false;
2206 /* Constrain the visibility of DECL based on the visibility of its template
2210 constrain_visibility_for_template (tree decl, tree targs)
2212 /* If this is a template instantiation, check the innermost
2213 template args for visibility constraints. The outer template
2214 args are covered by the class check. */
2215 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2217 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2221 tree arg = TREE_VEC_ELT (args, i-1);
2223 vis = type_visibility (arg);
2226 if (REFERENCE_REF_P (arg))
2227 arg = TREE_OPERAND (arg, 0);
2228 if (TREE_TYPE (arg))
2230 if (TREE_CODE (arg) == ADDR_EXPR)
2231 arg = TREE_OPERAND (arg, 0);
2232 if (VAR_OR_FUNCTION_DECL_P (arg))
2234 if (! TREE_PUBLIC (arg))
2235 vis = VISIBILITY_ANON;
2237 vis = DECL_VISIBILITY (arg);
2241 constrain_visibility (decl, vis, true);
2245 /* Like c_determine_visibility, but with additional C++-specific
2248 Function-scope entities can rely on the function's visibility because
2249 it is set in start_preparsed_function.
2251 Class-scope entities cannot rely on the class's visibility until the end
2252 of the enclosing class definition.
2254 Note that because namespaces have multiple independent definitions,
2255 namespace visibility is handled elsewhere using the #pragma visibility
2256 machinery rather than by decorating the namespace declaration.
2258 The goal is for constraints from the type to give a diagnostic, and
2259 other constraints to be applied silently. */
2262 determine_visibility (tree decl)
2264 tree class_type = NULL_TREE;
2266 bool orig_visibility_specified;
2267 enum symbol_visibility orig_visibility;
2269 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2271 /* Only relevant for names with external linkage. */
2272 if (!TREE_PUBLIC (decl))
2275 /* Cloned constructors and destructors get the same visibility as
2276 the underlying function. That should be set up in
2277 maybe_clone_body. */
2278 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2280 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2281 orig_visibility = DECL_VISIBILITY (decl);
2283 if (TREE_CODE (decl) == TYPE_DECL)
2285 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2286 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2287 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2292 else if (DECL_LANG_SPECIFIC (decl))
2293 use_template = DECL_USE_TEMPLATE (decl);
2297 /* If DECL is a member of a class, visibility specifiers on the
2298 class can influence the visibility of the DECL. */
2299 if (DECL_CLASS_SCOPE_P (decl))
2300 class_type = DECL_CONTEXT (decl);
2303 /* Not a class member. */
2305 /* Virtual tables have DECL_CONTEXT set to their associated class,
2306 so they are automatically handled above. */
2307 gcc_assert (!VAR_P (decl)
2308 || !DECL_VTABLE_OR_VTT_P (decl));
2310 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2312 /* Local statics and classes get the visibility of their
2313 containing function by default, except that
2314 -fvisibility-inlines-hidden doesn't affect them. */
2315 tree fn = DECL_CONTEXT (decl);
2316 if (DECL_VISIBILITY_SPECIFIED (fn))
2318 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2319 DECL_VISIBILITY_SPECIFIED (decl) =
2320 DECL_VISIBILITY_SPECIFIED (fn);
2324 if (DECL_CLASS_SCOPE_P (fn))
2325 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2326 else if (determine_hidden_inline (fn))
2328 DECL_VISIBILITY (decl) = default_visibility;
2329 DECL_VISIBILITY_SPECIFIED (decl) =
2330 visibility_options.inpragma;
2334 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2335 DECL_VISIBILITY_SPECIFIED (decl) =
2336 DECL_VISIBILITY_SPECIFIED (fn);
2340 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2341 but have no TEMPLATE_INFO, so don't try to check it. */
2344 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2345 && flag_visibility_ms_compat)
2347 /* Under -fvisibility-ms-compat, types are visible by default,
2348 even though their contents aren't. */
2349 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2350 int underlying_vis = type_visibility (underlying_type);
2351 if (underlying_vis == VISIBILITY_ANON
2352 || (CLASS_TYPE_P (underlying_type)
2353 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2354 constrain_visibility (decl, underlying_vis, false);
2356 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2358 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2360 /* tinfo visibility is based on the type it's for. */
2361 constrain_visibility
2362 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2364 /* Give the target a chance to override the visibility associated
2366 if (TREE_PUBLIC (decl)
2367 && !DECL_REALLY_EXTERN (decl)
2368 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2369 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2370 targetm.cxx.determine_class_data_visibility (decl);
2372 else if (use_template)
2373 /* Template instantiations and specializations get visibility based
2374 on their template unless they override it with an attribute. */;
2375 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2377 if (determine_hidden_inline (decl))
2378 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2381 /* Set default visibility to whatever the user supplied with
2382 #pragma GCC visibility or a namespace visibility attribute. */
2383 DECL_VISIBILITY (decl) = default_visibility;
2384 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2391 /* If the specialization doesn't specify visibility, use the
2392 visibility from the template. */
2393 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2394 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2395 : DECL_TEMPLATE_INFO (decl));
2396 tree args = TI_ARGS (tinfo);
2397 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2398 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2399 : DECL_ATTRIBUTES (decl));
2401 if (args != error_mark_node)
2403 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2405 if (!DECL_VISIBILITY_SPECIFIED (decl))
2407 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2408 && determine_hidden_inline (decl))
2409 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2412 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2413 DECL_VISIBILITY_SPECIFIED (decl)
2414 = DECL_VISIBILITY_SPECIFIED (pattern);
2419 /* Template argument visibility outweighs #pragma or namespace
2420 visibility, but not an explicit attribute. */
2421 && !lookup_attribute ("visibility", attribs))
2423 int depth = TMPL_ARGS_DEPTH (args);
2424 if (DECL_VISIBILITY_SPECIFIED (decl))
2426 /* A class template member with explicit visibility
2427 overrides the class visibility, so we need to apply
2428 all the levels of template args directly. */
2430 for (i = 1; i <= depth; ++i)
2432 tree lev = TMPL_ARGS_LEVEL (args, i);
2433 constrain_visibility_for_template (decl, lev);
2436 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2437 /* Limit visibility based on its template arguments. */
2438 constrain_visibility_for_template (decl, args);
2444 determine_visibility_from_class (decl, class_type);
2446 if (decl_anon_ns_mem_p (decl))
2447 /* Names in an anonymous namespace get internal linkage.
2448 This might change once we implement export. */
2449 constrain_visibility (decl, VISIBILITY_ANON, false);
2450 else if (TREE_CODE (decl) != TYPE_DECL)
2452 /* Propagate anonymity from type to decl. */
2453 int tvis = type_visibility (TREE_TYPE (decl));
2454 if (tvis == VISIBILITY_ANON
2455 || ! DECL_VISIBILITY_SPECIFIED (decl))
2456 constrain_visibility (decl, tvis, false);
2458 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2459 /* DR 757: A type without linkage shall not be used as the type of a
2460 variable or function with linkage, unless
2461 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2462 o the variable or function is not used (3.2 [basic.def.odr]) or is
2463 defined in the same translation unit.
2465 Since non-extern "C" decls need to be defined in the same
2466 translation unit, we can make the type internal. */
2467 constrain_visibility (decl, VISIBILITY_ANON, false);
2469 /* If visibility changed and DECL already has DECL_RTL, ensure
2470 symbol flags are updated. */
2471 if ((DECL_VISIBILITY (decl) != orig_visibility
2472 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2473 && ((VAR_P (decl) && TREE_STATIC (decl))
2474 || TREE_CODE (decl) == FUNCTION_DECL)
2475 && DECL_RTL_SET_P (decl))
2476 make_decl_rtl (decl);
2479 /* By default, static data members and function members receive
2480 the visibility of their containing class. */
2483 determine_visibility_from_class (tree decl, tree class_type)
2485 if (DECL_VISIBILITY_SPECIFIED (decl))
2488 if (determine_hidden_inline (decl))
2489 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2492 /* Default to the class visibility. */
2493 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2494 DECL_VISIBILITY_SPECIFIED (decl)
2495 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2498 /* Give the target a chance to override the visibility associated
2501 && (DECL_TINFO_P (decl)
2502 || (DECL_VTABLE_OR_VTT_P (decl)
2503 /* Construction virtual tables are not exported because
2504 they cannot be referred to from other object files;
2505 their name is not standardized by the ABI. */
2506 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2507 && TREE_PUBLIC (decl)
2508 && !DECL_REALLY_EXTERN (decl)
2509 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2510 targetm.cxx.determine_class_data_visibility (decl);
2513 /* Returns true iff DECL is an inline that should get hidden visibility
2514 because of -fvisibility-inlines-hidden. */
2517 determine_hidden_inline (tree decl)
2519 return (visibility_options.inlines_hidden
2520 /* Don't do this for inline templates; specializations might not be
2521 inline, and we don't want them to inherit the hidden
2522 visibility. We'll set it here for all inline instantiations. */
2523 && !processing_template_decl
2524 && TREE_CODE (decl) == FUNCTION_DECL
2525 && DECL_DECLARED_INLINE_P (decl)
2526 && (! DECL_LANG_SPECIFIC (decl)
2527 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2530 /* Constrain the visibility of a class TYPE based on the visibility of its
2531 field types. Warn if any fields require lesser visibility. */
2534 constrain_class_visibility (tree type)
2540 int vis = type_visibility (type);
2542 if (vis == VISIBILITY_ANON
2543 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2546 /* Don't warn about visibility if the class has explicit visibility. */
2547 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2548 vis = VISIBILITY_INTERNAL;
2550 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2551 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2553 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2554 int subvis = type_visibility (ftype);
2556 if (subvis == VISIBILITY_ANON)
2558 if (!in_main_input_context ())
2560 %qT has a field %qD whose type uses the anonymous namespace",
2563 else if (MAYBE_CLASS_TYPE_P (ftype)
2564 && vis < VISIBILITY_HIDDEN
2565 && subvis >= VISIBILITY_HIDDEN)
2566 warning (OPT_Wattributes, "\
2567 %qT declared with greater visibility than the type of its field %qD",
2571 binfo = TYPE_BINFO (type);
2572 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2574 int subvis = type_visibility (TREE_TYPE (t));
2576 if (subvis == VISIBILITY_ANON)
2578 if (!in_main_input_context())
2580 %qT has a base %qT whose type uses the anonymous namespace",
2581 type, TREE_TYPE (t));
2583 else if (vis < VISIBILITY_HIDDEN
2584 && subvis >= VISIBILITY_HIDDEN)
2585 warning (OPT_Wattributes, "\
2586 %qT declared with greater visibility than its base %qT",
2587 type, TREE_TYPE (t));
2591 /* Functions for adjusting the visibility of a tagged type and its nested
2592 types and declarations when it gets a name for linkage purposes from a
2595 static void bt_reset_linkage_1 (binding_entry, void *);
2596 static void bt_reset_linkage_2 (binding_entry, void *);
2598 /* First reset the visibility of all the types. */
2601 reset_type_linkage_1 (tree type)
2603 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2604 if (CLASS_TYPE_P (type))
2605 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2606 bt_reset_linkage_1, NULL);
2609 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2611 reset_type_linkage_1 (b->type);
2614 /* Then reset the visibility of any static data members or member
2615 functions that use those types. */
2618 reset_decl_linkage (tree decl)
2620 if (TREE_PUBLIC (decl))
2622 if (DECL_CLONED_FUNCTION_P (decl))
2624 TREE_PUBLIC (decl) = true;
2625 DECL_INTERFACE_KNOWN (decl) = false;
2626 determine_visibility (decl);
2627 tentative_decl_linkage (decl);
2630 reset_type_linkage_2 (tree type)
2632 if (CLASS_TYPE_P (type))
2634 if (tree vt = CLASSTYPE_VTABLES (type))
2636 tree name = mangle_vtbl_for_type (type);
2637 DECL_NAME (vt) = name;
2638 SET_DECL_ASSEMBLER_NAME (vt, name);
2639 reset_decl_linkage (vt);
2641 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2643 tree name = mangle_typeinfo_for_type (type);
2644 DECL_NAME (ti) = name;
2645 SET_DECL_ASSEMBLER_NAME (ti, name);
2646 TREE_TYPE (name) = type;
2647 reset_decl_linkage (ti);
2649 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2650 if (TREE_CODE (m) == VAR_DECL)
2651 reset_decl_linkage (m);
2652 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2653 reset_decl_linkage (m);
2654 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2655 bt_reset_linkage_2, NULL);
2659 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2661 reset_type_linkage_2 (b->type);
2664 reset_type_linkage (tree type)
2666 reset_type_linkage_1 (type);
2667 reset_type_linkage_2 (type);
2670 /* Set up our initial idea of what the linkage of DECL should be. */
2673 tentative_decl_linkage (tree decl)
2675 if (DECL_INTERFACE_KNOWN (decl))
2676 /* We've already made a decision as to how this function will
2678 else if (vague_linkage_p (decl))
2680 if (TREE_CODE (decl) == FUNCTION_DECL
2681 && decl_defined_p (decl))
2683 DECL_EXTERNAL (decl) = 1;
2684 DECL_NOT_REALLY_EXTERN (decl) = 1;
2685 note_vague_linkage_fn (decl);
2686 /* A non-template inline function with external linkage will
2687 always be COMDAT. As we must eventually determine the
2688 linkage of all functions, and as that causes writes to
2689 the data mapped in from the PCH file, it's advantageous
2690 to mark the functions at this point. */
2691 if (DECL_DECLARED_INLINE_P (decl)
2692 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2693 || DECL_DEFAULTED_FN (decl)))
2695 /* This function must have external linkage, as
2696 otherwise DECL_INTERFACE_KNOWN would have been
2698 gcc_assert (TREE_PUBLIC (decl));
2699 comdat_linkage (decl);
2700 DECL_INTERFACE_KNOWN (decl) = 1;
2703 else if (TREE_CODE (decl) == VAR_DECL)
2704 maybe_commonize_var (decl);
2708 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2709 for DECL has not already been determined, do so now by setting
2710 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2711 function is called entities with vague linkage whose definitions
2712 are available must have TREE_PUBLIC set.
2714 If this function decides to place DECL in COMDAT, it will set
2715 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2716 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2717 callers defer that decision until it is clear that DECL is actually
2721 import_export_decl (tree decl)
2726 tree class_type = NULL_TREE;
2728 if (DECL_INTERFACE_KNOWN (decl))
2731 /* We cannot determine what linkage to give to an entity with vague
2732 linkage until the end of the file. For example, a virtual table
2733 for a class will be defined if and only if the key method is
2734 defined in this translation unit. As a further example, consider
2735 that when compiling a translation unit that uses PCH file with
2736 "-frepo" it would be incorrect to make decisions about what
2737 entities to emit when building the PCH; those decisions must be
2738 delayed until the repository information has been processed. */
2739 gcc_assert (at_eof);
2740 /* Object file linkage for explicit instantiations is handled in
2741 mark_decl_instantiated. For static variables in functions with
2742 vague linkage, maybe_commonize_var is used.
2744 Therefore, the only declarations that should be provided to this
2745 function are those with external linkage that are:
2747 * implicit instantiations of function templates
2751 * implicit instantiations of static data members of class
2758 Furthermore, all entities that reach this point must have a
2759 definition available in this translation unit.
2761 The following assertions check these conditions. */
2762 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2763 /* Any code that creates entities with TREE_PUBLIC cleared should
2764 also set DECL_INTERFACE_KNOWN. */
2765 gcc_assert (TREE_PUBLIC (decl));
2766 if (TREE_CODE (decl) == FUNCTION_DECL)
2767 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2768 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2769 || DECL_DECLARED_INLINE_P (decl));
2771 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2772 || DECL_VTABLE_OR_VTT_P (decl)
2773 || DECL_TINFO_P (decl));
2774 /* Check that a definition of DECL is available in this translation
2776 gcc_assert (!DECL_REALLY_EXTERN (decl));
2778 /* Assume that DECL will not have COMDAT linkage. */
2780 /* Assume that DECL will not be imported into this translation
2784 /* See if the repository tells us whether or not to emit DECL in
2785 this translation unit. */
2786 emit_p = repo_emit_p (decl);
2789 else if (emit_p == 1)
2791 /* The repository indicates that this entity should be defined
2792 here. Make sure the back end honors that request. */
2794 /* Output the definition as an ordinary strong definition. */
2795 DECL_EXTERNAL (decl) = 0;
2796 DECL_INTERFACE_KNOWN (decl) = 1;
2801 /* We have already decided what to do with this DECL; there is no
2802 need to check anything further. */
2804 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2806 class_type = DECL_CONTEXT (decl);
2807 import_export_class (class_type);
2808 if (TYPE_FOR_JAVA (class_type))
2810 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2811 && CLASSTYPE_INTERFACE_ONLY (class_type))
2813 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2814 && !CLASSTYPE_USE_TEMPLATE (class_type)
2815 && CLASSTYPE_KEY_METHOD (class_type)
2816 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2817 /* The ABI requires that all virtual tables be emitted with
2818 COMDAT linkage. However, on systems where COMDAT symbols
2819 don't show up in the table of contents for a static
2820 archive, or on systems without weak symbols (where we
2821 approximate COMDAT linkage by using internal linkage), the
2822 linker will report errors about undefined symbols because
2823 it will not see the virtual table definition. Therefore,
2824 in the case that we know that the virtual table will be
2825 emitted in only one translation unit, we make the virtual
2826 table an ordinary definition with external linkage. */
2827 DECL_EXTERNAL (decl) = 0;
2828 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2830 /* CLASS_TYPE is being exported from this translation unit,
2831 so DECL should be defined here. */
2832 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2833 /* If a class is declared in a header with the "extern
2834 template" extension, then it will not be instantiated,
2835 even in translation units that would normally require
2836 it. Often such classes are explicitly instantiated in
2837 one translation unit. Therefore, the explicit
2838 instantiation must be made visible to other translation
2840 DECL_EXTERNAL (decl) = 0;
2843 /* The generic C++ ABI says that class data is always
2844 COMDAT, even if there is a key function. Some
2845 variants (e.g., the ARM EABI) says that class data
2846 only has COMDAT linkage if the class data might be
2847 emitted in more than one translation unit. When the
2848 key method can be inline and is inline, we still have
2849 to arrange for comdat even though
2850 class_data_always_comdat is false. */
2851 if (!CLASSTYPE_KEY_METHOD (class_type)
2852 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2853 || targetm.cxx.class_data_always_comdat ())
2855 /* The ABI requires COMDAT linkage. Normally, we
2856 only emit COMDAT things when they are needed;
2857 make sure that we realize that this entity is
2864 else if (!flag_implicit_templates
2865 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2870 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2872 tree type = TREE_TYPE (DECL_NAME (decl));
2873 if (CLASS_TYPE_P (type))
2876 import_export_class (type);
2877 if (CLASSTYPE_INTERFACE_KNOWN (type)
2878 && TYPE_POLYMORPHIC_P (type)
2879 && CLASSTYPE_INTERFACE_ONLY (type)
2880 /* If -fno-rtti was specified, then we cannot be sure
2881 that RTTI information will be emitted with the
2882 virtual table of the class, so we must emit it
2883 wherever it is used. */
2888 if (CLASSTYPE_INTERFACE_KNOWN (type)
2889 && !CLASSTYPE_INTERFACE_ONLY (type))
2891 comdat_p = (targetm.cxx.class_data_always_comdat ()
2892 || (CLASSTYPE_KEY_METHOD (type)
2893 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2898 DECL_EXTERNAL (decl) = 0;
2908 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2910 /* DECL is an implicit instantiation of a function or static
2912 if ((flag_implicit_templates
2913 && !flag_use_repository)
2914 || (flag_implicit_inline_templates
2915 && TREE_CODE (decl) == FUNCTION_DECL
2916 && DECL_DECLARED_INLINE_P (decl)))
2919 /* If we are not implicitly generating templates, then mark
2920 this entity as undefined in this translation unit. */
2923 else if (DECL_FUNCTION_MEMBER_P (decl))
2925 if (!DECL_DECLARED_INLINE_P (decl))
2927 tree ctype = DECL_CONTEXT (decl);
2928 import_export_class (ctype);
2929 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2931 DECL_NOT_REALLY_EXTERN (decl)
2932 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2933 || (DECL_DECLARED_INLINE_P (decl)
2934 && ! flag_implement_inlines
2935 && !DECL_VINDEX (decl)));
2937 if (!DECL_NOT_REALLY_EXTERN (decl))
2938 DECL_EXTERNAL (decl) = 1;
2940 /* Always make artificials weak. */
2941 if (DECL_ARTIFICIAL (decl) && flag_weak)
2944 maybe_make_one_only (decl);
2955 /* If we are importing DECL into this translation unit, mark is
2956 an undefined here. */
2957 DECL_EXTERNAL (decl) = 1;
2958 DECL_NOT_REALLY_EXTERN (decl) = 0;
2962 /* If we decided to put DECL in COMDAT, mark it accordingly at
2964 comdat_linkage (decl);
2967 DECL_INTERFACE_KNOWN (decl) = 1;
2970 /* Return an expression that performs the destruction of DECL, which
2971 must be a VAR_DECL whose type has a non-trivial destructor, or is
2972 an array whose (innermost) elements have a non-trivial destructor. */
2975 build_cleanup (tree decl)
2977 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2978 gcc_assert (clean != NULL_TREE);
2982 /* Returns the initialization guard variable for the variable DECL,
2983 which has static storage duration. */
2986 get_guard (tree decl)
2991 sname = mangle_guard_variable (decl);
2992 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2997 /* We use a type that is big enough to contain a mutex as well
2998 as an integer counter. */
2999 guard_type = targetm.cxx.guard_type ();
3000 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3001 VAR_DECL, sname, guard_type);
3003 /* The guard should have the same linkage as what it guards. */
3004 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3005 TREE_STATIC (guard) = TREE_STATIC (decl);
3006 DECL_COMMON (guard) = DECL_COMMON (decl);
3007 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3008 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3009 if (DECL_ONE_ONLY (decl))
3010 make_decl_one_only (guard, cxx_comdat_group (guard));
3011 if (TREE_PUBLIC (decl))
3012 DECL_WEAK (guard) = DECL_WEAK (decl);
3013 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3014 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3016 DECL_ARTIFICIAL (guard) = 1;
3017 DECL_IGNORED_P (guard) = 1;
3018 TREE_USED (guard) = 1;
3019 pushdecl_top_level_and_finish (guard, NULL_TREE);
3024 /* Return those bits of the GUARD variable that should be set when the
3025 guarded entity is actually initialized. */
3028 get_guard_bits (tree guard)
3030 if (!targetm.cxx.guard_mask_bit ())
3032 /* We only set the first byte of the guard, in order to leave room
3033 for a mutex in the high-order bits. */
3034 guard = build1 (ADDR_EXPR,
3035 build_pointer_type (TREE_TYPE (guard)),
3037 guard = build1 (NOP_EXPR,
3038 build_pointer_type (char_type_node),
3040 guard = build1 (INDIRECT_REF, char_type_node, guard);
3046 /* Return an expression which determines whether or not the GUARD
3047 variable has already been initialized. */
3050 get_guard_cond (tree guard)
3054 /* Check to see if the GUARD is zero. */
3055 guard = get_guard_bits (guard);
3057 /* Mask off all but the low bit. */
3058 if (targetm.cxx.guard_mask_bit ())
3060 guard_value = integer_one_node;
3061 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3062 guard_value = convert (TREE_TYPE (guard), guard_value);
3063 guard = cp_build_binary_op (input_location,
3064 BIT_AND_EXPR, guard, guard_value,
3065 tf_warning_or_error);
3068 guard_value = integer_zero_node;
3069 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3070 guard_value = convert (TREE_TYPE (guard), guard_value);
3071 return cp_build_binary_op (input_location,
3072 EQ_EXPR, guard, guard_value,
3073 tf_warning_or_error);
3076 /* Return an expression which sets the GUARD variable, indicating that
3077 the variable being guarded has been initialized. */
3080 set_guard (tree guard)
3084 /* Set the GUARD to one. */
3085 guard = get_guard_bits (guard);
3086 guard_init = integer_one_node;
3087 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3088 guard_init = convert (TREE_TYPE (guard), guard_init);
3089 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3090 tf_warning_or_error);
3093 /* Returns true iff we can tell that VAR does not have a dynamic
3097 var_defined_without_dynamic_init (tree var)
3099 /* If it's defined in another TU, we can't tell. */
3100 if (DECL_EXTERNAL (var))
3102 /* If it has a non-trivial destructor, registering the destructor
3103 counts as dynamic initialization. */
3104 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3106 /* If it's in this TU, its initializer has been processed, unless
3107 it's a case of self-initialization, then DECL_INITIALIZED_P is
3108 false while the initializer is handled by finish_id_expression. */
3109 if (!DECL_INITIALIZED_P (var))
3111 /* If it has no initializer or a constant one, it's not dynamic. */
3112 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3113 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3116 /* Returns true iff VAR is a variable that needs uses to be
3117 wrapped for possible dynamic initialization. */
3120 var_needs_tls_wrapper (tree var)
3122 return (!error_operand_p (var)
3123 && DECL_THREAD_LOCAL_P (var)
3124 && !DECL_GNU_TLS_P (var)
3125 && !DECL_FUNCTION_SCOPE_P (var)
3126 && !var_defined_without_dynamic_init (var));
3129 /* Get the FUNCTION_DECL for the shared TLS init function for this
3130 translation unit. */
3133 get_local_tls_init_fn (void)
3135 tree sname = get_identifier ("__tls_init");
3136 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3139 fn = build_lang_decl (FUNCTION_DECL, sname,
3140 build_function_type (void_type_node,
3142 SET_DECL_LANGUAGE (fn, lang_c);
3143 TREE_PUBLIC (fn) = false;
3144 DECL_ARTIFICIAL (fn) = true;
3146 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3151 /* Get a FUNCTION_DECL for the init function for the thread_local
3152 variable VAR. The init function will be an alias to the function
3153 that initializes all the non-local TLS variables in the translation
3154 unit. The init function is only used by the wrapper function. */
3157 get_tls_init_fn (tree var)
3159 /* Only C++11 TLS vars need this init fn. */
3160 if (!var_needs_tls_wrapper (var))
3163 /* If -fno-extern-tls-init, assume that we don't need to call
3164 a tls init function for a variable defined in another TU. */
3165 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3168 #ifdef ASM_OUTPUT_DEF
3169 /* If the variable is internal, or if we can't generate aliases,
3170 call the local init function directly. */
3171 if (!TREE_PUBLIC (var))
3173 return get_local_tls_init_fn ();
3175 tree sname = mangle_tls_init_fn (var);
3176 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3179 fn = build_lang_decl (FUNCTION_DECL, sname,
3180 build_function_type (void_type_node,
3182 SET_DECL_LANGUAGE (fn, lang_c);
3183 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3184 DECL_ARTIFICIAL (fn) = true;
3185 DECL_COMDAT (fn) = DECL_COMDAT (var);
3186 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3187 if (DECL_ONE_ONLY (var))
3188 make_decl_one_only (fn, cxx_comdat_group (fn));
3189 if (TREE_PUBLIC (var))
3191 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3192 /* If the variable is defined somewhere else and might have static
3193 initialization, make the init function a weak reference. */
3194 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3195 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3196 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3197 && DECL_EXTERNAL (var))
3200 DECL_WEAK (fn) = DECL_WEAK (var);
3202 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3203 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3204 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3205 DECL_IGNORED_P (fn) = 1;
3208 DECL_BEFRIENDING_CLASSES (fn) = var;
3210 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3215 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3216 variable VAR. The wrapper function calls the init function (if any) for
3217 VAR and then returns a reference to VAR. The wrapper function is used
3218 in place of VAR everywhere VAR is mentioned. */
3221 get_tls_wrapper_fn (tree var)
3223 /* Only C++11 TLS vars need this wrapper fn. */
3224 if (!var_needs_tls_wrapper (var))
3227 tree sname = mangle_tls_wrapper_fn (var);
3228 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3231 /* A named rvalue reference is an lvalue, so the wrapper should
3232 always return an lvalue reference. */
3233 tree type = non_reference (TREE_TYPE (var));
3234 type = build_reference_type (type);
3235 tree fntype = build_function_type (type, void_list_node);
3236 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3237 SET_DECL_LANGUAGE (fn, lang_c);
3238 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3239 DECL_ARTIFICIAL (fn) = true;
3240 DECL_IGNORED_P (fn) = 1;
3241 /* The wrapper is inline and emitted everywhere var is used. */
3242 DECL_DECLARED_INLINE_P (fn) = true;
3243 if (TREE_PUBLIC (var))
3245 comdat_linkage (fn);
3246 #ifdef HAVE_GAS_HIDDEN
3247 /* Make the wrapper bind locally; there's no reason to share
3248 the wrapper between multiple shared objects. */
3249 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3250 DECL_VISIBILITY_SPECIFIED (fn) = true;
3253 if (!TREE_PUBLIC (fn))
3254 DECL_INTERFACE_KNOWN (fn) = true;
3256 note_vague_linkage_fn (fn);
3259 /* We want CSE to commonize calls to the wrapper, but marking it as
3260 pure is unsafe since it has side-effects. I guess we need a new
3261 ECF flag even weaker than ECF_PURE. FIXME! */
3262 DECL_PURE_P (fn) = true;
3265 DECL_BEFRIENDING_CLASSES (fn) = var;
3267 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3272 /* At EOF, generate the definition for the TLS wrapper function FN:
3275 if (init_fn) init_fn();
3280 generate_tls_wrapper (tree fn)
3282 tree var = DECL_BEFRIENDING_CLASSES (fn);
3284 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3285 tree body = begin_function_body ();
3286 /* Only call the init fn if there might be one. */
3287 if (tree init_fn = get_tls_init_fn (var))
3289 tree if_stmt = NULL_TREE;
3290 /* If init_fn is a weakref, make sure it exists before calling. */
3291 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3293 if_stmt = begin_if_stmt ();
3294 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3295 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3296 NE_EXPR, addr, nullptr_node,
3297 tf_warning_or_error);
3298 finish_if_stmt_cond (cond, if_stmt);
3300 finish_expr_stmt (build_cxx_call
3301 (init_fn, 0, NULL, tf_warning_or_error));
3304 finish_then_clause (if_stmt);
3305 finish_if_stmt (if_stmt);
3309 /* If there's no initialization, the wrapper is a constant function. */
3310 TREE_READONLY (fn) = true;
3311 finish_return_stmt (convert_from_reference (var));
3312 finish_function_body (body);
3313 expand_or_defer_fn (finish_function (0));
3316 /* Start the process of running a particular set of global constructors
3317 or destructors. Subroutine of do_[cd]tors. Also called from
3318 vtv_start_verification_constructor_init_function. */
3321 start_objects (int method_type, int initp)
3327 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3329 if (initp != DEFAULT_INIT_PRIORITY)
3339 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3342 sprintf (type, "sub_%c", method_type);
3344 fndecl = build_lang_decl (FUNCTION_DECL,
3345 get_file_function_name (type),
3346 build_function_type_list (void_type_node,
3348 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3350 TREE_PUBLIC (current_function_decl) = 0;
3352 /* Mark as artificial because it's not explicitly in the user's
3354 DECL_ARTIFICIAL (current_function_decl) = 1;
3356 /* Mark this declaration as used to avoid spurious warnings. */
3357 TREE_USED (current_function_decl) = 1;
3359 /* Mark this function as a global constructor or destructor. */
3360 if (method_type == 'I')
3361 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3363 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3365 body = begin_compound_stmt (BCS_FN_BODY);
3370 /* Finish the process of running a particular set of global constructors
3371 or destructors. Subroutine of do_[cd]tors. */
3374 finish_objects (int method_type, int initp, tree body)
3379 finish_compound_stmt (body);
3380 fn = finish_function (0);
3382 if (method_type == 'I')
3384 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3385 decl_init_priority_insert (fn, initp);
3389 DECL_STATIC_DESTRUCTOR (fn) = 1;
3390 decl_fini_priority_insert (fn, initp);
3393 expand_or_defer_fn (fn);
3396 /* The names of the parameters to the function created to handle
3397 initializations and destructions for objects with static storage
3399 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3400 #define PRIORITY_IDENTIFIER "__priority"
3402 /* The name of the function we create to handle initializations and
3403 destructions for objects with static storage duration. */
3404 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3406 /* The declaration for the __INITIALIZE_P argument. */
3407 static GTY(()) tree initialize_p_decl;
3409 /* The declaration for the __PRIORITY argument. */
3410 static GTY(()) tree priority_decl;
3412 /* The declaration for the static storage duration function. */
3413 static GTY(()) tree ssdf_decl;
3415 /* All the static storage duration functions created in this
3416 translation unit. */
3417 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3419 /* A map from priority levels to information about that priority
3420 level. There may be many such levels, so efficient lookup is
3422 static splay_tree priority_info_map;
3424 /* Begins the generation of the function that will handle all
3425 initialization and destruction of objects with static storage
3426 duration. The function generated takes two parameters of type
3427 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3428 nonzero, it performs initializations. Otherwise, it performs
3429 destructions. It only performs those initializations or
3430 destructions with the indicated __PRIORITY. The generated function
3433 It is assumed that this function will only be called once per
3434 translation unit. */
3437 start_static_storage_duration_function (unsigned count)
3441 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3443 /* Create the identifier for this function. It will be of the form
3444 SSDF_IDENTIFIER_<number>. */
3445 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3447 type = build_function_type_list (void_type_node,
3448 integer_type_node, integer_type_node,
3451 /* Create the FUNCTION_DECL itself. */
3452 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3453 get_identifier (id),
3455 TREE_PUBLIC (ssdf_decl) = 0;
3456 DECL_ARTIFICIAL (ssdf_decl) = 1;
3458 /* Put this function in the list of functions to be called from the
3459 static constructors and destructors. */
3462 vec_alloc (ssdf_decls, 32);
3464 /* Take this opportunity to initialize the map from priority
3465 numbers to information about that priority level. */
3466 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3467 /*delete_key_fn=*/0,
3468 /*delete_value_fn=*/
3469 (splay_tree_delete_value_fn) &free);
3471 /* We always need to generate functions for the
3472 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3473 priorities later, we'll be sure to find the
3474 DEFAULT_INIT_PRIORITY. */
3475 get_priority_info (DEFAULT_INIT_PRIORITY);
3478 vec_safe_push (ssdf_decls, ssdf_decl);
3480 /* Create the argument list. */
3481 initialize_p_decl = cp_build_parm_decl
3482 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3483 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3484 TREE_USED (initialize_p_decl) = 1;
3485 priority_decl = cp_build_parm_decl
3486 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3487 DECL_CONTEXT (priority_decl) = ssdf_decl;
3488 TREE_USED (priority_decl) = 1;
3490 DECL_CHAIN (initialize_p_decl) = priority_decl;
3491 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3493 /* Put the function in the global scope. */
3494 pushdecl (ssdf_decl);
3496 /* Start the function itself. This is equivalent to declaring the
3499 static void __ssdf (int __initialize_p, init __priority_p);
3501 It is static because we only need to call this function from the
3502 various constructor and destructor functions for this module. */
3503 start_preparsed_function (ssdf_decl,
3504 /*attrs=*/NULL_TREE,
3507 /* Set up the scope of the outermost block in the function. */
3508 body = begin_compound_stmt (BCS_FN_BODY);
3513 /* Finish the generation of the function which performs initialization
3514 and destruction of objects with static storage duration. After
3515 this point, no more such objects can be created. */
3518 finish_static_storage_duration_function (tree body)
3520 /* Close out the function. */
3521 finish_compound_stmt (body);
3522 expand_or_defer_fn (finish_function (0));
3525 /* Return the information about the indicated PRIORITY level. If no
3526 code to handle this level has yet been generated, generate the
3527 appropriate prologue. */
3529 static priority_info
3530 get_priority_info (int priority)
3535 n = splay_tree_lookup (priority_info_map,
3536 (splay_tree_key) priority);
3539 /* Create a new priority information structure, and insert it
3541 pi = XNEW (struct priority_info_s);
3542 pi->initializations_p = 0;
3543 pi->destructions_p = 0;
3544 splay_tree_insert (priority_info_map,
3545 (splay_tree_key) priority,
3546 (splay_tree_value) pi);
3549 pi = (priority_info) n->value;
3554 /* The effective initialization priority of a DECL. */
3556 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3557 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3558 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3560 /* Whether a DECL needs a guard to protect it against multiple
3563 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3564 || DECL_ONE_ONLY (decl) \
3565 || DECL_WEAK (decl)))
3567 /* Called from one_static_initialization_or_destruction(),
3569 Walks the initializer list of a global variable and looks for
3570 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3571 and that have their DECL_CONTEXT() == NULL.
3572 For each such temporary variable, set their DECL_CONTEXT() to
3573 the current function. This is necessary because otherwise
3574 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3575 when trying to refer to a temporary variable that does not have
3576 it's DECL_CONTECT() properly set. */
3578 fix_temporary_vars_context_r (tree *node,
3582 gcc_assert (current_function_decl);
3584 if (TREE_CODE (*node) == BIND_EXPR)
3588 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3591 && DECL_ARTIFICIAL (var)
3592 && !DECL_CONTEXT (var))
3593 DECL_CONTEXT (var) = current_function_decl;
3599 /* Set up to handle the initialization or destruction of DECL. If
3600 INITP is nonzero, we are initializing the variable. Otherwise, we
3601 are destroying it. */
3604 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3606 tree guard_if_stmt = NULL_TREE;
3609 /* If we are supposed to destruct and there's a trivial destructor,
3610 nothing has to be done. */
3612 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3615 /* Trick the compiler into thinking we are at the file and line
3616 where DECL was declared so that error-messages make sense, and so
3617 that the debugger will show somewhat sensible file and line
3619 input_location = DECL_SOURCE_LOCATION (decl);
3621 /* Make sure temporary variables in the initialiser all have
3622 their DECL_CONTEXT() set to a value different from NULL_TREE.
3623 This can happen when global variables initialisers are built.
3624 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3625 the temporary variables that might have been generated in the
3626 accompagning initialisers is NULL_TREE, meaning the variables have been
3627 declared in the global namespace.
3628 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3629 of the temporaries are set to the current function decl. */
3630 cp_walk_tree_without_duplicates (&init,
3631 fix_temporary_vars_context_r,
3638 Access control for implicit calls to the constructors,
3639 the conversion functions, or the destructor called to
3640 create and destroy a static data member is performed as
3641 if these calls appeared in the scope of the member's
3644 we pretend we are in a static member function of the class of
3645 which the DECL is a member. */
3646 if (member_p (decl))
3648 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3649 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3652 /* Assume we don't need a guard. */
3654 /* We need a guard if this is an object with external linkage that
3655 might be initialized in more than one place. (For example, a
3656 static data member of a template, when the data member requires
3658 if (NEEDS_GUARD_P (decl))
3662 guard = get_guard (decl);
3664 /* When using __cxa_atexit, we just check the GUARD as we would
3665 for a local static. */
3666 if (flag_use_cxa_atexit)
3668 /* When using __cxa_atexit, we never try to destroy
3669 anything from a static destructor. */
3671 guard_cond = get_guard_cond (guard);
3673 /* If we don't have __cxa_atexit, then we will be running
3674 destructors from .fini sections, or their equivalents. So,
3675 we need to know how many times we've tried to initialize this
3676 object. We do initializations only if the GUARD is zero,
3677 i.e., if we are the first to initialize the variable. We do
3678 destructions only if the GUARD is one, i.e., if we are the
3679 last to destroy the variable. */
3682 = cp_build_binary_op (input_location,
3684 cp_build_unary_op (PREINCREMENT_EXPR,
3687 tf_warning_or_error),
3689 tf_warning_or_error);
3692 = cp_build_binary_op (input_location,
3694 cp_build_unary_op (PREDECREMENT_EXPR,
3697 tf_warning_or_error),
3699 tf_warning_or_error);
3701 guard_if_stmt = begin_if_stmt ();
3702 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3706 /* If we're using __cxa_atexit, we have not already set the GUARD,
3707 so we must do so now. */
3708 if (guard && initp && flag_use_cxa_atexit)
3709 finish_expr_stmt (set_guard (guard));
3711 /* Perform the initialization or destruction. */
3716 finish_expr_stmt (init);
3717 if (flag_sanitize & SANITIZE_ADDRESS)
3719 varpool_node *vnode = varpool_node::get (decl);
3721 vnode->dynamically_initialized = 1;
3725 /* If we're using __cxa_atexit, register a function that calls the
3726 destructor for the object. */
3727 if (flag_use_cxa_atexit)
3728 finish_expr_stmt (register_dtor_fn (decl));
3731 finish_expr_stmt (build_cleanup (decl));
3733 /* Finish the guard if-stmt, if necessary. */
3736 finish_then_clause (guard_if_stmt);
3737 finish_if_stmt (guard_if_stmt);
3740 /* Now that we're done with DECL we don't need to pretend to be a
3741 member of its class any longer. */
3742 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3743 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3746 /* Generate code to do the initialization or destruction of the decls in VARS,
3747 a TREE_LIST of VAR_DECL with static storage duration.
3748 Whether initialization or destruction is performed is specified by INITP. */
3751 do_static_initialization_or_destruction (tree vars, bool initp)
3753 tree node, init_if_stmt, cond;
3755 /* Build the outer if-stmt to check for initialization or destruction. */
3756 init_if_stmt = begin_if_stmt ();
3757 cond = initp ? integer_one_node : integer_zero_node;
3758 cond = cp_build_binary_op (input_location,
3762 tf_warning_or_error);
3763 finish_if_stmt_cond (cond, init_if_stmt);
3765 /* To make sure dynamic construction doesn't access globals from other
3766 compilation units where they might not be yet constructed, for
3767 -fsanitize=address insert __asan_before_dynamic_init call that
3768 prevents access to either all global variables that need construction
3769 in other compilation units, or at least those that haven't been
3770 initialized yet. Variables that need dynamic construction in
3771 the current compilation unit are kept accessible. */
3772 if (flag_sanitize & SANITIZE_ADDRESS)
3773 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3777 tree decl = TREE_VALUE (node);
3778 tree priority_if_stmt;
3782 /* If we don't need a destructor, there's nothing to do. Avoid
3783 creating a possibly empty if-stmt. */
3784 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3786 node = TREE_CHAIN (node);
3790 /* Remember that we had an initialization or finalization at this
3792 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3793 pi = get_priority_info (priority);
3795 pi->initializations_p = 1;
3797 pi->destructions_p = 1;
3799 /* Conditionalize this initialization on being in the right priority
3800 and being initializing/finalizing appropriately. */
3801 priority_if_stmt = begin_if_stmt ();
3802 cond = cp_build_binary_op (input_location,
3805 build_int_cst (NULL_TREE, priority),
3806 tf_warning_or_error);
3807 finish_if_stmt_cond (cond, priority_if_stmt);
3809 /* Process initializers with same priority. */
3811 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3812 node = TREE_CHAIN (node))
3813 /* Do one initialization or destruction. */
3814 one_static_initialization_or_destruction (TREE_VALUE (node),
3815 TREE_PURPOSE (node), initp);
3817 /* Finish up the priority if-stmt body. */
3818 finish_then_clause (priority_if_stmt);
3819 finish_if_stmt (priority_if_stmt);
3823 /* Revert what __asan_before_dynamic_init did by calling
3824 __asan_after_dynamic_init. */
3825 if (flag_sanitize & SANITIZE_ADDRESS)
3826 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3828 /* Finish up the init/destruct if-stmt body. */
3829 finish_then_clause (init_if_stmt);
3830 finish_if_stmt (init_if_stmt);
3833 /* VARS is a list of variables with static storage duration which may
3834 need initialization and/or finalization. Remove those variables
3835 that don't really need to be initialized or finalized, and return
3836 the resulting list. The order in which the variables appear in
3837 VARS is in reverse order of the order in which they should actually
3838 be initialized. The list we return is in the unreversed order;
3839 i.e., the first variable should be initialized first. */
3842 prune_vars_needing_no_initialization (tree *vars)
3845 tree result = NULL_TREE;
3850 tree decl = TREE_VALUE (t);
3851 tree init = TREE_PURPOSE (t);
3853 /* Deal gracefully with error. */
3854 if (decl == error_mark_node)
3856 var = &TREE_CHAIN (t);
3860 /* The only things that can be initialized are variables. */
3861 gcc_assert (VAR_P (decl));
3863 /* If this object is not defined, we don't need to do anything
3865 if (DECL_EXTERNAL (decl))
3867 var = &TREE_CHAIN (t);
3871 /* Also, if the initializer already contains errors, we can bail
3873 if (init && TREE_CODE (init) == TREE_LIST
3874 && value_member (error_mark_node, init))
3876 var = &TREE_CHAIN (t);
3880 /* This variable is going to need initialization and/or
3881 finalization, so we add it to the list. */
3882 *var = TREE_CHAIN (t);
3883 TREE_CHAIN (t) = result;
3890 /* Make sure we have told the back end about all the variables in
3894 write_out_vars (tree vars)
3898 for (v = vars; v; v = TREE_CHAIN (v))
3900 tree var = TREE_VALUE (v);
3901 if (!var_finalized_p (var))
3903 import_export_decl (var);
3904 rest_of_decl_compilation (var, 1, 1);
3909 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3910 (otherwise) that will initialize all global objects with static
3911 storage duration having the indicated PRIORITY. */
3914 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3922 input_location = *locus;
3924 /* Was: locus->line++; */
3926 /* We use `I' to indicate initialization and `D' to indicate
3928 function_key = constructor_p ? 'I' : 'D';
3930 /* We emit the function lazily, to avoid generating empty
3931 global constructors and destructors. */
3934 /* For Objective-C++, we may need to initialize metadata found in this module.
3935 This must be done _before_ any other static initializations. */
3936 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3937 && constructor_p && objc_static_init_needed_p ())
3939 body = start_objects (function_key, priority);
3940 objc_generate_static_init_call (NULL_TREE);
3943 /* Call the static storage duration function with appropriate
3945 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3947 /* Calls to pure or const functions will expand to nothing. */
3948 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3953 body = start_objects (function_key, priority);
3955 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3956 build_int_cst (NULL_TREE,
3958 build_int_cst (NULL_TREE,
3961 finish_expr_stmt (call);
3965 /* Close out the function. */
3967 finish_objects (function_key, priority, body);
3970 /* Generate constructor and destructor functions for the priority
3974 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3976 location_t *locus = (location_t *) data;
3977 int priority = (int) n->key;
3978 priority_info pi = (priority_info) n->value;
3980 /* Generate the functions themselves, but only if they are really
3982 if (pi->initializations_p)
3983 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3984 if (pi->destructions_p)
3985 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3987 /* Keep iterating. */
3991 /* Java requires that we be able to reference a local address for a
3992 method, and not be confused by PLT entries. If hidden aliases are
3993 supported, collect and return all the functions for which we should
3994 emit a hidden alias. */
3996 static hash_set<tree> *
3997 collect_candidates_for_java_method_aliases (void)
3999 struct cgraph_node *node;
4000 hash_set<tree> *candidates = NULL;
4002 #ifndef HAVE_GAS_HIDDEN
4006 FOR_EACH_FUNCTION (node)
4008 tree fndecl = node->decl;
4010 if (DECL_CLASS_SCOPE_P (fndecl)
4011 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4012 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4014 if (candidates == NULL)
4015 candidates = new hash_set<tree>;
4016 candidates->add (fndecl);
4024 /* Java requires that we be able to reference a local address for a
4025 method, and not be confused by PLT entries. If hidden aliases are
4026 supported, emit one for each java function that we've emitted.
4027 CANDIDATES is the set of FUNCTION_DECLs that were gathered
4028 by collect_candidates_for_java_method_aliases. */
4031 build_java_method_aliases (hash_set<tree> *candidates)
4033 struct cgraph_node *node;
4035 #ifndef HAVE_GAS_HIDDEN
4039 FOR_EACH_FUNCTION (node)
4041 tree fndecl = node->decl;
4043 if (TREE_ASM_WRITTEN (fndecl)
4044 && candidates->contains (fndecl))
4046 /* Mangle the name in a predictable way; we need to reference
4047 this from a java compiled object file. */
4048 tree oid, nid, alias;
4052 oid = DECL_ASSEMBLER_NAME (fndecl);
4053 oname = IDENTIFIER_POINTER (oid);
4054 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4055 nname = ACONCAT (("_ZGA", oname+2, NULL));
4056 nid = get_identifier (nname);
4058 alias = make_alias_for (fndecl, nid);
4059 TREE_PUBLIC (alias) = 1;
4060 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4062 assemble_alias (alias, oid);
4067 /* Return C++ property of T, based on given operation OP. */
4070 cpp_check (tree t, cpp_operation op)
4075 return DECL_PURE_VIRTUAL_P (t);
4076 case IS_CONSTRUCTOR:
4077 return DECL_CONSTRUCTOR_P (t);
4079 return DECL_DESTRUCTOR_P (t);
4080 case IS_COPY_CONSTRUCTOR:
4081 return DECL_COPY_CONSTRUCTOR_P (t);
4083 return TREE_CODE (t) == TEMPLATE_DECL;
4085 return trivial_type_p (t);
4091 /* Collect source file references recursively, starting from NAMESPC. */
4094 collect_source_refs (tree namespc)
4101 /* Iterate over names in this name space. */
4102 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4103 if (!DECL_IS_BUILTIN (t) )
4104 collect_source_ref (DECL_SOURCE_FILE (t));
4106 /* Dump siblings, if any */
4107 collect_source_refs (TREE_CHAIN (namespc));
4109 /* Dump children, if any */
4110 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4113 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4114 starting from NAMESPC. */
4117 collect_ada_namespace (tree namespc, const char *source_file)
4122 /* Collect decls from this namespace */
4123 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4125 /* Collect siblings, if any */
4126 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4128 /* Collect children, if any */
4129 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4132 /* Returns true iff there is a definition available for variable or
4136 decl_defined_p (tree decl)
4138 if (TREE_CODE (decl) == FUNCTION_DECL)
4139 return (DECL_INITIAL (decl) != NULL_TREE);
4142 gcc_assert (VAR_P (decl));
4143 return !DECL_EXTERNAL (decl);
4147 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4151 An integral constant-expression can only involve ... const
4152 variables of integral or enumeration types initialized with
4153 constant expressions ...
4155 C++0x also allows constexpr variables and temporaries initialized
4156 with constant expressions. We handle the former here, but the latter
4157 are just folded away in cxx_eval_constant_expression.
4159 The standard does not require that the expression be non-volatile.
4160 G++ implements the proposed correction in DR 457. */
4163 decl_constant_var_p (tree decl)
4165 if (!decl_maybe_constant_var_p (decl))
4168 /* We don't know if a template static data member is initialized with
4169 a constant expression until we instantiate its initializer. Even
4170 in the case of a constexpr variable, we can't treat it as a
4171 constant until its initializer is complete in case it's used in
4172 its own initializer. */
4174 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4177 /* Returns true if DECL could be a symbolic constant variable, depending on
4181 decl_maybe_constant_var_p (tree decl)
4183 tree type = TREE_TYPE (decl);
4186 if (DECL_DECLARED_CONSTEXPR_P (decl))
4188 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4189 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4192 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4193 called from grokfndecl and grokvardecl; in all modes it is called from
4194 cp_write_global_declarations. */
4197 no_linkage_error (tree decl)
4199 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4200 /* In C++11 it's ok if the decl is defined. */
4202 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4204 /* The type that got us on no_linkage_decls must have gotten a name for
4205 linkage purposes. */;
4206 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4207 /* The type might end up having a typedef name for linkage purposes. */
4208 vec_safe_push (no_linkage_decls, decl);
4209 else if (TYPE_ANONYMOUS_P (t))
4212 if (cxx_dialect >= cxx11)
4213 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4214 "anonymous type, is used but never defined", decl);
4215 else if (DECL_EXTERN_C_P (decl))
4216 /* Allow this; it's pretty common in C. */;
4217 else if (TREE_CODE (decl) == VAR_DECL)
4218 /* DRs 132, 319 and 389 seem to indicate types with
4219 no linkage can only be used to declare extern "C"
4220 entities. Since it's not always an error in the
4221 ISO C++ 90 Standard, we only issue a warning. */
4222 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4223 "with no linkage used to declare variable %q#D with "
4226 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4227 "linkage used to declare function %q#D with linkage",
4229 if (d && is_typedef_decl (TYPE_NAME (t)))
4230 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4231 "to the unqualified type, so it is not used for linkage",
4234 else if (cxx_dialect >= cxx11)
4235 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4236 "%qT, is used but never defined", decl, t);
4237 else if (TREE_CODE (decl) == VAR_DECL)
4238 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4239 "used to declare variable %q#D with linkage", t, decl);
4241 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4242 "to declare function %q#D with linkage", t, decl);
4245 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4248 collect_all_refs (const char *source_file)
4250 collect_ada_namespace (global_namespace, source_file);
4253 /* Clear DECL_EXTERNAL for NODE. */
4256 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4258 DECL_EXTERNAL (node->decl) = 0;
4262 /* Build up the function to run dynamic initializers for thread_local
4263 variables in this translation unit and alias the init functions for the
4264 individual variables to it. */
4267 handle_tls_init (void)
4269 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4270 if (vars == NULL_TREE)
4273 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4275 write_out_vars (vars);
4277 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4279 TREE_PUBLIC (guard) = false;
4280 TREE_STATIC (guard) = true;
4281 DECL_ARTIFICIAL (guard) = true;
4282 DECL_IGNORED_P (guard) = true;
4283 TREE_USED (guard) = true;
4284 set_decl_tls_model (guard, decl_default_tls_model (guard));
4285 pushdecl_top_level_and_finish (guard, NULL_TREE);
4287 tree fn = get_local_tls_init_fn ();
4288 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4289 tree body = begin_function_body ();
4290 tree if_stmt = begin_if_stmt ();
4291 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4292 tf_warning_or_error);
4293 finish_if_stmt_cond (cond, if_stmt);
4294 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4295 tf_warning_or_error));
4296 for (; vars; vars = TREE_CHAIN (vars))
4298 tree var = TREE_VALUE (vars);
4299 tree init = TREE_PURPOSE (vars);
4300 one_static_initialization_or_destruction (var, init, true);
4302 #ifdef ASM_OUTPUT_DEF
4303 /* Output init aliases even with -fno-extern-tls-init. */
4304 if (TREE_PUBLIC (var))
4306 tree single_init_fn = get_tls_init_fn (var);
4307 if (single_init_fn == NULL_TREE)
4310 = cgraph_node::get_create (fn)->create_same_body_alias
4311 (single_init_fn, fn);
4312 gcc_assert (alias != NULL);
4317 finish_then_clause (if_stmt);
4318 finish_if_stmt (if_stmt);
4319 finish_function_body (body);
4320 expand_or_defer_fn (finish_function (0));
4323 /* The entire file is now complete. If requested, dump everything
4330 FILE *stream = dump_begin (TDI_tu, &flags);
4334 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4335 dump_end (TDI_tu, stream);
4339 /* Check the deallocation functions for CODE to see if we want to warn that
4340 only one was defined. */
4343 maybe_warn_sized_delete (enum tree_code code)
4345 tree sized = NULL_TREE;
4346 tree unsized = NULL_TREE;
4348 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4349 ovl; ovl = OVL_NEXT (ovl))
4351 tree fn = OVL_CURRENT (ovl);
4352 /* We're only interested in usual deallocation functions. */
4353 if (!non_placement_deallocation_fn_p (fn))
4355 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4360 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4361 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4362 "the program should also define %qD", sized);
4363 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4364 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4365 "the program should also define %qD", unsized);
4368 /* Check the global deallocation functions to see if we want to warn about
4369 defining unsized without sized (or vice versa). */
4372 maybe_warn_sized_delete ()
4374 if (!flag_sized_deallocation || !warn_sized_deallocation)
4376 maybe_warn_sized_delete (DELETE_EXPR);
4377 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4380 /* This routine is called at the end of compilation.
4381 Its job is to create all the code needed to initialize and
4382 destroy the global aggregates. We do the destruction
4383 first, since that way we only need to reverse the decls once. */
4386 cp_write_global_declarations (void)
4392 unsigned ssdf_count = 0;
4395 hash_set<tree> *candidates;
4397 locus = input_location;
4400 /* Bad parse errors. Just forget about it. */
4401 if (! global_bindings_p () || current_class_type
4402 || !vec_safe_is_empty (decl_namespace_list))
4405 /* This is the point to write out a PCH if we're doing that.
4406 In that case we do not want to do anything else. */
4409 c_common_write_pch ();
4414 symtab->process_same_body_aliases ();
4416 /* Handle -fdump-ada-spec[-slim] */
4417 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4419 if (flag_dump_ada_spec_slim)
4420 collect_source_ref (main_input_filename);
4422 collect_source_refs (global_namespace);
4424 dump_ada_specs (collect_all_refs, cpp_check);
4427 /* FIXME - huh? was input_line -= 1;*/
4429 timevar_start (TV_PHASE_DEFERRED);
4431 /* We now have to write out all the stuff we put off writing out.
4434 o Template specializations that we have not yet instantiated,
4435 but which are needed.
4436 o Initialization and destruction for non-local objects with
4437 static storage duration. (Local objects with static storage
4438 duration are initialized when their scope is first entered,
4439 and are cleaned up via atexit.)
4440 o Virtual function tables.
4442 All of these may cause others to be needed. For example,
4443 instantiating one function may cause another to be needed, and
4444 generating the initializer for an object may cause templates to be
4445 instantiated, etc., etc. */
4447 emit_support_tinfos ();
4456 /* If there are templates that we've put off instantiating, do
4458 instantiate_pending_templates (retries);
4461 /* Write out virtual tables as required. Note that writing out
4462 the virtual table for a template class may cause the
4463 instantiation of members of that class. If we write out
4464 vtables then we remove the class from our list so we don't
4465 have to look at it again. */
4467 while (keyed_classes != NULL_TREE
4468 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4471 keyed_classes = TREE_CHAIN (keyed_classes);
4477 tree next = TREE_CHAIN (t);
4481 if (maybe_emit_vtables (TREE_VALUE (next)))
4484 TREE_CHAIN (t) = TREE_CHAIN (next);
4489 next = TREE_CHAIN (t);
4493 /* Write out needed type info variables. We have to be careful
4494 looping through unemitted decls, because emit_tinfo_decl may
4495 cause other variables to be needed. New elements will be
4496 appended, and we remove from the vector those that actually
4498 for (i = unemitted_tinfo_decls->length ();
4499 unemitted_tinfo_decls->iterate (--i, &t);)
4500 if (emit_tinfo_decl (t))
4503 unemitted_tinfo_decls->unordered_remove (i);
4506 /* The list of objects with static storage duration is built up
4507 in reverse order. We clear STATIC_AGGREGATES so that any new
4508 aggregates added during the initialization of these will be
4509 initialized in the correct order when we next come around the
4511 vars = prune_vars_needing_no_initialization (&static_aggregates);
4515 /* We need to start a new initialization function each time
4516 through the loop. That's because we need to know which
4517 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4518 isn't computed until a function is finished, and written
4519 out. That's a deficiency in the back end. When this is
4520 fixed, these initialization functions could all become
4521 inline, with resulting performance improvements. */
4524 /* Set the line and file, so that it is obviously not from
4526 input_location = locus;
4527 ssdf_body = start_static_storage_duration_function (ssdf_count);
4529 /* Make sure the back end knows about all the variables. */
4530 write_out_vars (vars);
4532 /* First generate code to do all the initializations. */
4534 do_static_initialization_or_destruction (vars, /*initp=*/true);
4536 /* Then, generate code to do all the destructions. Do these
4537 in reverse order so that the most recently constructed
4538 variable is the first destroyed. If we're using
4539 __cxa_atexit, then we don't need to do this; functions
4540 were registered at initialization time to destroy the
4542 if (!flag_use_cxa_atexit && vars)
4544 vars = nreverse (vars);
4545 do_static_initialization_or_destruction (vars, /*initp=*/false);
4550 /* Finish up the static storage duration function for this
4552 input_location = locus;
4553 finish_static_storage_duration_function (ssdf_body);
4555 /* All those initializations and finalizations might cause
4556 us to need more inline functions, more template
4557 instantiations, etc. */
4560 /* ??? was: locus.line++; */
4563 /* Now do the same for thread_local variables. */
4566 /* Go through the set of inline functions whose bodies have not
4567 been emitted yet. If out-of-line copies of these functions
4568 are required, emit them. */
4569 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4571 /* Does it need synthesizing? */
4572 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4573 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4575 /* Even though we're already at the top-level, we push
4576 there again. That way, when we pop back a few lines
4577 hence, all of our state is restored. Otherwise,
4578 finish_function doesn't clean things up, and we end
4579 up with CURRENT_FUNCTION_DECL set. */
4580 push_to_top_level ();
4581 /* The decl's location will mark where it was first
4582 needed. Save that so synthesize method can indicate
4583 where it was needed from, in case of error */
4584 input_location = DECL_SOURCE_LOCATION (decl);
4585 synthesize_method (decl);
4586 pop_from_top_level ();
4590 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4591 generate_tls_wrapper (decl);
4593 if (!DECL_SAVED_TREE (decl))
4596 /* We lie to the back end, pretending that some functions
4597 are not defined when they really are. This keeps these
4598 functions from being put out unnecessarily. But, we must
4599 stop lying when the functions are referenced, or if they
4600 are not comdat since they need to be put out now. If
4601 DECL_INTERFACE_KNOWN, then we have already set
4602 DECL_EXTERNAL appropriately, so there's no need to check
4603 again, and we do not want to clear DECL_EXTERNAL if a
4604 previous call to import_export_decl set it.
4606 This is done in a separate for cycle, because if some
4607 deferred function is contained in another deferred
4608 function later in deferred_fns varray,
4609 rest_of_compilation would skip this function and we
4610 really cannot expand the same function twice. */
4611 import_export_decl (decl);
4612 if (DECL_NOT_REALLY_EXTERN (decl)
4613 && DECL_INITIAL (decl)
4614 && decl_needed_p (decl))
4616 struct cgraph_node *node, *next;
4618 node = cgraph_node::get (decl);
4619 if (node->cpp_implicit_alias)
4620 node = node->get_alias_target ();
4622 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4624 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4625 group, we need to mark all symbols in the same comdat group
4627 if (node->same_comdat_group)
4628 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4630 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4631 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4635 /* If we're going to need to write this function out, and
4636 there's already a body for it, create RTL for it now.
4637 (There might be no body if this is a method we haven't
4638 gotten around to synthesizing yet.) */
4639 if (!DECL_EXTERNAL (decl)
4640 && decl_needed_p (decl)
4641 && !TREE_ASM_WRITTEN (decl)
4642 && !cgraph_node::get (decl)->definition)
4644 /* We will output the function; no longer consider it in this
4646 DECL_DEFER_OUTPUT (decl) = 0;
4647 /* Generate RTL for this function now that we know we
4649 expand_or_defer_fn (decl);
4650 /* If we're compiling -fsyntax-only pretend that this
4651 function has been written out so that we don't try to
4653 if (flag_syntax_only)
4654 TREE_ASM_WRITTEN (decl) = 1;
4659 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4662 /* Static data members are just like namespace-scope globals. */
4663 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4665 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4666 /* Don't write it out if we haven't seen a definition. */
4667 || DECL_IN_AGGR_P (decl))
4669 import_export_decl (decl);
4670 /* If this static data member is needed, provide it to the
4672 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4673 DECL_EXTERNAL (decl) = 0;
4675 if (vec_safe_length (pending_statics) != 0
4676 && wrapup_global_declarations (pending_statics->address (),
4677 pending_statics->length ()))
4684 /* All used inline functions must have a definition at this point. */
4685 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4687 if (/* Check online inline functions that were actually used. */
4688 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4689 /* If the definition actually was available here, then the
4690 fact that the function was not defined merely represents
4691 that for some reason (use of a template repository,
4692 #pragma interface, etc.) we decided not to emit the
4694 && !DECL_INITIAL (decl)
4695 /* Don't complain if the template was defined. */
4696 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4697 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4698 (template_for_substitution (decl)))))
4700 warning (0, "inline function %q+D used but never defined", decl);
4701 /* Avoid a duplicate warning from check_global_declaration_1. */
4702 TREE_NO_WARNING (decl) = 1;
4706 /* So must decls that use a type with no linkage. */
4707 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4708 no_linkage_error (decl);
4710 maybe_warn_sized_delete ();
4712 /* Then, do the Objective-C stuff. This is where all the
4713 Objective-C module stuff gets generated (symtab,
4714 class/protocol/selector lists etc). This must be done after C++
4715 templates, destructors etc. so that selectors used in C++
4716 templates are properly allocated. */
4717 if (c_dialect_objc ())
4718 objc_write_global_declarations ();
4720 /* We give C linkage to static constructors and destructors. */
4721 push_lang_context (lang_name_c);
4723 /* Generate initialization and destruction functions for all
4724 priorities for which they are required. */
4725 if (priority_info_map)
4726 splay_tree_foreach (priority_info_map,
4727 generate_ctor_and_dtor_functions_for_priority,
4729 else if (c_dialect_objc () && objc_static_init_needed_p ())
4730 /* If this is obj-c++ and we need a static init, call
4731 generate_ctor_or_dtor_function. */
4732 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4733 DEFAULT_INIT_PRIORITY, &locus);
4735 /* We're done with the splay-tree now. */
4736 if (priority_info_map)
4737 splay_tree_delete (priority_info_map);
4739 /* Generate any missing aliases. */
4740 maybe_apply_pending_pragma_weaks ();
4742 /* We're done with static constructors, so we can go back to "C++"
4744 pop_lang_context ();
4746 /* Collect candidates for Java hidden aliases. */
4747 candidates = collect_candidates_for_java_method_aliases ();
4749 timevar_stop (TV_PHASE_DEFERRED);
4750 timevar_start (TV_PHASE_OPT_GEN);
4752 if (flag_vtable_verify)
4754 vtv_recover_class_info ();
4755 vtv_compute_class_hierarchy_transitive_closure ();
4756 vtv_build_vtable_verify_fndecl ();
4759 symtab->finalize_compilation_unit ();
4761 if (flag_vtable_verify)
4763 /* Generate the special constructor initialization function that
4764 calls __VLTRegisterPairs, and give it a very high
4765 initialization priority. This must be done after
4766 finalize_compilation_unit so that we have accurate
4767 information about which vtable will actually be emitted. */
4768 vtv_generate_init_routine ();
4771 timevar_stop (TV_PHASE_OPT_GEN);
4772 timevar_start (TV_PHASE_CHECK_DBGINFO);
4774 /* Now, issue warnings about static, but not defined, functions,
4775 etc., and emit debugging information. */
4776 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4777 if (vec_safe_length (pending_statics) != 0)
4779 check_global_declarations (pending_statics->address (),
4780 pending_statics->length ());
4781 emit_debug_global_declarations (pending_statics->address (),
4782 pending_statics->length ());
4785 perform_deferred_noexcept_checks ();
4787 /* Generate hidden aliases for Java. */
4790 build_java_method_aliases (candidates);
4796 /* The entire file is now complete. If requested, dump everything
4800 if (flag_detailed_statistics)
4802 dump_tree_statistics ();
4803 dump_time_statistics ();
4805 input_location = locus;
4807 #ifdef ENABLE_CHECKING
4808 validate_conversion_obstack ();
4809 #endif /* ENABLE_CHECKING */
4811 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4814 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4815 function to call in parse-tree form; it has not yet been
4816 semantically analyzed. ARGS are the arguments to the function.
4817 They have already been semantically analyzed. This may change
4821 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4822 tsubst_flags_t complain)
4825 vec<tree, va_gc> *orig_args = NULL;
4830 object = TREE_OPERAND (fn, 0);
4832 if (processing_template_decl)
4834 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4835 || TREE_CODE (fn) == MEMBER_REF);
4836 if (type_dependent_expression_p (fn)
4837 || any_type_dependent_arguments_p (*args))
4838 return build_nt_call_vec (fn, *args);
4840 orig_args = make_tree_vector_copy (*args);
4842 /* Transform the arguments and add the implicit "this"
4843 parameter. That must be done before the FN is transformed
4844 because we depend on the form of FN. */
4845 make_args_non_dependent (*args);
4846 object = build_non_dependent_expr (object);
4847 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4849 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4850 object = cp_build_addr_expr (object, complain);
4851 vec_safe_insert (*args, 0, object);
4853 /* Now that the arguments are done, transform FN. */
4854 fn = build_non_dependent_expr (fn);
4857 /* A qualified name corresponding to a bound pointer-to-member is
4858 represented as an OFFSET_REF:
4860 struct B { void g(); };
4862 void B::g() { (this->*p)(); } */
4863 if (TREE_CODE (fn) == OFFSET_REF)
4865 tree object_addr = cp_build_addr_expr (object, complain);
4866 fn = TREE_OPERAND (fn, 1);
4867 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4869 vec_safe_insert (*args, 0, object_addr);
4872 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4873 expr = build_op_call (fn, args, complain);
4875 expr = cp_build_function_call_vec (fn, args, complain);
4876 if (processing_template_decl && expr != error_mark_node)
4877 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4879 if (orig_args != NULL)
4880 release_tree_vector (orig_args);
4887 check_default_args (tree x)
4889 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4890 bool saw_def = false;
4891 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4892 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4894 if (TREE_PURPOSE (arg))
4896 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4898 error ("default argument missing for parameter %P of %q+#D", i, x);
4899 TREE_PURPOSE (arg) = error_mark_node;
4904 /* Return true if function DECL can be inlined. This is used to force
4905 instantiation of methods that might be interesting for inlining. */
4907 possibly_inlined_p (tree decl)
4909 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4910 if (DECL_UNINLINABLE (decl))
4912 if (!optimize || pragma_java_exceptions)
4913 return DECL_DECLARED_INLINE_P (decl);
4914 /* When optimizing, we might inline everything when flatten
4915 attribute or heuristics inlining for size or autoinlining
4920 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4921 If DECL is a specialization or implicitly declared class member,
4922 generate the actual definition. Return false if something goes
4923 wrong, true otherwise. */
4926 mark_used (tree decl, tsubst_flags_t complain)
4928 /* If DECL is a BASELINK for a single function, then treat it just
4929 like the DECL for the function. Otherwise, if the BASELINK is
4930 for an overloaded function, we don't know which function was
4931 actually used until after overload resolution. */
4932 if (BASELINK_P (decl))
4934 decl = BASELINK_FUNCTIONS (decl);
4935 if (really_overloaded_fn (decl))
4937 decl = OVL_CURRENT (decl);
4940 /* Set TREE_USED for the benefit of -Wunused. */
4941 TREE_USED (decl) = 1;
4942 if (DECL_CLONED_FUNCTION_P (decl))
4943 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4945 /* Mark enumeration types as used. */
4946 if (TREE_CODE (decl) == CONST_DECL)
4947 used_types_insert (DECL_CONTEXT (decl));
4949 if (TREE_CODE (decl) == FUNCTION_DECL)
4950 maybe_instantiate_noexcept (decl);
4952 if (TREE_CODE (decl) == FUNCTION_DECL
4953 && DECL_DELETED_FN (decl))
4955 if (DECL_ARTIFICIAL (decl))
4957 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4958 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4960 /* We mark a lambda conversion op as deleted if we can't
4961 generate it properly; see maybe_add_lambda_conv_op. */
4962 sorry ("converting lambda which uses %<...%> to "
4963 "function pointer");
4967 if (complain & tf_error)
4969 error ("use of deleted function %qD", decl);
4970 if (!maybe_explain_implicit_delete (decl))
4971 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4976 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
4977 && deprecated_state != DEPRECATED_SUPPRESS)
4978 warn_deprecated_use (decl, NULL_TREE);
4980 /* We can only check DECL_ODR_USED on variables or functions with
4981 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4982 might need special handling for. */
4983 if (!VAR_OR_FUNCTION_DECL_P (decl)
4984 || DECL_LANG_SPECIFIC (decl) == NULL
4985 || DECL_THUNK_P (decl))
4987 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4989 if (complain & tf_error)
4990 error ("use of %qD before deduction of %<auto%>", decl);
4996 /* We only want to do this processing once. We don't need to keep trying
4997 to instantiate inline templates, because unit-at-a-time will make sure
4998 we get them compiled before functions that want to inline them. */
4999 if (DECL_ODR_USED (decl))
5002 /* If within finish_function, defer the rest until that function
5003 finishes, otherwise it might recurse. */
5004 if (defer_mark_used_calls)
5006 vec_safe_push (deferred_mark_used_calls, decl);
5010 /* Normally, we can wait until instantiation-time to synthesize DECL.
5011 However, if DECL is a static data member initialized with a constant
5012 or a constexpr function, we need it right now because a reference to
5013 such a data member or a call to such function is not value-dependent.
5014 For a function that uses auto in the return type, we need to instantiate
5015 it to find out its type. For OpenMP user defined reductions, we need
5016 them instantiated for reduction clauses which inline them by hand
5018 if (DECL_LANG_SPECIFIC (decl)
5019 && DECL_TEMPLATE_INFO (decl)
5020 && (decl_maybe_constant_var_p (decl)
5021 || (TREE_CODE (decl) == FUNCTION_DECL
5022 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5023 || undeduced_auto_decl (decl))
5024 && !uses_template_parms (DECL_TI_ARGS (decl)))
5026 /* Instantiating a function will result in garbage collection. We
5027 must treat this situation as if we were within the body of a
5028 function so as to avoid collecting live data only referenced from
5029 the stack (such as overload resolution candidates). */
5031 instantiate_decl (decl, /*defer_ok=*/false,
5032 /*expl_inst_class_mem_p=*/false);
5036 if (processing_template_decl || in_template_function ())
5039 /* Check this too in case we're within instantiate_non_dependent_expr. */
5040 if (DECL_TEMPLATE_INFO (decl)
5041 && uses_template_parms (DECL_TI_ARGS (decl)))
5044 if (undeduced_auto_decl (decl))
5046 if (complain & tf_error)
5047 error ("use of %qD before deduction of %<auto%>", decl);
5051 /* If we don't need a value, then we don't need to synthesize DECL. */
5052 if (cp_unevaluated_operand != 0)
5055 DECL_ODR_USED (decl) = 1;
5056 if (DECL_CLONED_FUNCTION_P (decl))
5057 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5059 /* DR 757: A type without linkage shall not be used as the type of a
5060 variable or function with linkage, unless
5061 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5062 o the variable or function is not used (3.2 [basic.def.odr]) or is
5063 defined in the same translation unit. */
5064 if (cxx_dialect > cxx98
5065 && decl_linkage (decl) != lk_none
5066 && !DECL_EXTERN_C_P (decl)
5067 && !DECL_ARTIFICIAL (decl)
5068 && !decl_defined_p (decl)
5069 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5071 if (is_local_extern (decl))
5072 /* There's no way to define a local extern, and adding it to
5073 the vector interferes with GC, so give an error now. */
5074 no_linkage_error (decl);
5076 vec_safe_push (no_linkage_decls, decl);
5079 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5080 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5081 /* Remember it, so we can check it was defined. */
5082 note_vague_linkage_fn (decl);
5084 /* Is it a synthesized method that needs to be synthesized? */
5085 if (TREE_CODE (decl) == FUNCTION_DECL
5086 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5087 && DECL_DEFAULTED_FN (decl)
5088 /* A function defaulted outside the class is synthesized either by
5089 cp_finish_decl or instantiate_decl. */
5090 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5091 && ! DECL_INITIAL (decl))
5093 /* Defer virtual destructors so that thunks get the right
5095 if (DECL_VIRTUAL_P (decl) && !at_eof)
5097 note_vague_linkage_fn (decl);
5101 /* Remember the current location for a function we will end up
5102 synthesizing. Then we can inform the user where it was
5103 required in the case of error. */
5104 DECL_SOURCE_LOCATION (decl) = input_location;
5106 /* Synthesizing an implicitly defined member function will result in
5107 garbage collection. We must treat this situation as if we were
5108 within the body of a function so as to avoid collecting live data
5109 on the stack (such as overload resolution candidates).
5111 We could just let cp_write_global_declarations handle synthesizing
5112 this function by adding it to deferred_fns, but doing
5113 it at the use site produces better error messages. */
5115 synthesize_method (decl);
5117 /* If this is a synthesized method we don't need to
5118 do the instantiation test below. */
5120 else if (VAR_OR_FUNCTION_DECL_P (decl)
5121 && DECL_TEMPLATE_INFO (decl)
5122 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5123 || always_instantiate_p (decl)))
5124 /* If this is a function or variable that is an instance of some
5125 template, we now know that we will need to actually do the
5126 instantiation. We check that DECL is not an explicit
5127 instantiation because that is not checked in instantiate_decl.
5129 We put off instantiating functions in order to improve compile
5130 times. Maintaining a stack of active functions is expensive,
5131 and the inliner knows to instantiate any functions it might
5132 need. Therefore, we always try to defer instantiation. */
5135 instantiate_decl (decl, /*defer_ok=*/true,
5136 /*expl_inst_class_mem_p=*/false);
5144 mark_used (tree decl)
5146 return mark_used (decl, tf_warning_or_error);
5150 vtv_start_verification_constructor_init_function (void)
5152 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5156 vtv_finish_verification_constructor_init_function (tree function_body)
5160 finish_compound_stmt (function_body);
5161 fn = finish_function (0);
5162 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5163 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5168 #include "gt-cp-decl2.h"