1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
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"
34 #include "stringpool.h"
37 #include "stor-layout.h"
46 #include "c-family/c-common.h"
47 #include "c-family/c-objc.h"
49 #include "tree-inline.h"
50 #include "c-family/c-pragma.h"
53 #include "splay-tree.h"
54 #include "langhooks.h"
55 #include "c-family/c-ada-spec.h"
58 extern cpp_reader
*parse_in
;
60 /* This structure contains information about the initializations
61 and/or destructions required for a particular priority level. */
62 typedef struct priority_info_s
{
63 /* Nonzero if there have been any initializations at this priority
64 throughout the translation unit. */
65 int initializations_p
;
66 /* Nonzero if there have been any destructions at this priority
67 throughout the translation unit. */
71 static void mark_vtable_entries (tree
);
72 static bool maybe_emit_vtables (tree
);
73 static bool acceptable_java_type (tree
);
74 static tree
start_objects (int, int);
75 static void finish_objects (int, int, tree
);
76 static tree
start_static_storage_duration_function (unsigned);
77 static void finish_static_storage_duration_function (tree
);
78 static priority_info
get_priority_info (int);
79 static void do_static_initialization_or_destruction (tree
, bool);
80 static void one_static_initialization_or_destruction (tree
, tree
, bool);
81 static void generate_ctor_or_dtor_function (bool, int, location_t
*);
82 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node
,
84 static tree
prune_vars_needing_no_initialization (tree
*);
85 static void write_out_vars (tree
);
86 static void import_export_class (tree
);
87 static tree
get_guard_bits (tree
);
88 static void determine_visibility_from_class (tree
, tree
);
89 static bool determine_hidden_inline (tree
);
90 static bool decl_defined_p (tree
);
92 /* A list of static class variables. This is needed, because a
93 static class variable can be declared inside the class without
94 an initializer, and then initialized, statically, outside the class. */
95 static GTY(()) vec
<tree
, va_gc
> *pending_statics
;
97 /* A list of functions which were declared inline, but which we
98 may need to emit outline anyway. */
99 static GTY(()) vec
<tree
, va_gc
> *deferred_fns
;
101 /* A list of decls that use types with no linkage, which we need to make
103 static GTY(()) vec
<tree
, va_gc
> *no_linkage_decls
;
105 /* Nonzero if we're done parsing and into end-of-file activities. */
110 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
111 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
112 that apply to the function). */
115 build_memfn_type (tree fntype
, tree ctype
, cp_cv_quals quals
,
116 cp_ref_qualifier rqual
)
121 bool late_return_type_p
;
123 if (fntype
== error_mark_node
|| ctype
== error_mark_node
)
124 return error_mark_node
;
126 gcc_assert (TREE_CODE (fntype
) == FUNCTION_TYPE
127 || TREE_CODE (fntype
) == METHOD_TYPE
);
129 type_quals
= quals
& ~TYPE_QUAL_RESTRICT
;
130 ctype
= cp_build_qualified_type (ctype
, type_quals
);
131 raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
132 attrs
= TYPE_ATTRIBUTES (fntype
);
133 late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (fntype
);
134 fntype
= build_method_type_directly (ctype
, TREE_TYPE (fntype
),
135 (TREE_CODE (fntype
) == METHOD_TYPE
136 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype
))
137 : TYPE_ARG_TYPES (fntype
)));
139 fntype
= cp_build_type_attribute_variant (fntype
, attrs
);
141 fntype
= build_ref_qualified_type (fntype
, rqual
);
143 fntype
= build_exception_variant (fntype
, raises
);
144 if (late_return_type_p
)
145 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
150 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
151 return type changed to NEW_RET. */
154 change_return_type (tree new_ret
, tree fntype
)
157 tree args
= TYPE_ARG_TYPES (fntype
);
158 tree raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
159 tree attrs
= TYPE_ATTRIBUTES (fntype
);
160 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (fntype
);
162 if (new_ret
== error_mark_node
)
165 if (same_type_p (new_ret
, TREE_TYPE (fntype
)))
168 if (TREE_CODE (fntype
) == FUNCTION_TYPE
)
170 newtype
= build_function_type (new_ret
, args
);
171 newtype
= apply_memfn_quals (newtype
,
172 type_memfn_quals (fntype
),
173 type_memfn_rqual (fntype
));
176 newtype
= build_method_type_directly
177 (class_of_this_parm (fntype
), new_ret
, TREE_CHAIN (args
));
179 newtype
= build_exception_variant (newtype
, raises
);
181 newtype
= cp_build_type_attribute_variant (newtype
, attrs
);
182 if (late_return_type_p
)
183 TYPE_HAS_LATE_RETURN_TYPE (newtype
) = 1;
188 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
192 cp_build_parm_decl (tree name
, tree type
)
194 tree parm
= build_decl (input_location
,
195 PARM_DECL
, name
, type
);
196 /* DECL_ARG_TYPE is only used by the back end and the back end never
198 if (!processing_template_decl
)
199 DECL_ARG_TYPE (parm
) = type_passed_as (type
);
204 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
208 build_artificial_parm (tree name
, tree type
)
210 tree parm
= cp_build_parm_decl (name
, type
);
211 DECL_ARTIFICIAL (parm
) = 1;
212 /* All our artificial parms are implicitly `const'; they cannot be
214 TREE_READONLY (parm
) = 1;
218 /* Constructors for types with virtual baseclasses need an "in-charge" flag
219 saying whether this constructor is responsible for initialization of
220 virtual baseclasses or not. All destructors also need this "in-charge"
221 flag, which additionally determines whether or not the destructor should
222 free the memory for the object.
224 This function adds the "in-charge" flag to member function FN if
225 appropriate. It is called from grokclassfn and tsubst.
226 FN must be either a constructor or destructor.
228 The in-charge flag follows the 'this' parameter, and is followed by the
229 VTT parm (if any), then the user-written parms. */
232 maybe_retrofit_in_chrg (tree fn
)
234 tree basetype
, arg_types
, parms
, parm
, fntype
;
236 /* If we've already add the in-charge parameter don't do it again. */
237 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
240 /* When processing templates we can't know, in general, whether or
241 not we're going to have virtual baseclasses. */
242 if (processing_template_decl
)
245 /* We don't need an in-charge parameter for constructors that don't
246 have virtual bases. */
247 if (DECL_CONSTRUCTOR_P (fn
)
248 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
251 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
252 basetype
= TREE_TYPE (TREE_VALUE (arg_types
));
253 arg_types
= TREE_CHAIN (arg_types
);
255 parms
= DECL_CHAIN (DECL_ARGUMENTS (fn
));
257 /* If this is a subobject constructor or destructor, our caller will
258 pass us a pointer to our VTT. */
259 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
261 parm
= build_artificial_parm (vtt_parm_identifier
, vtt_parm_type
);
263 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
264 DECL_CHAIN (parm
) = parms
;
267 /* ...and then to TYPE_ARG_TYPES. */
268 arg_types
= hash_tree_chain (vtt_parm_type
, arg_types
);
270 DECL_HAS_VTT_PARM_P (fn
) = 1;
273 /* Then add the in-charge parm (before the VTT parm). */
274 parm
= build_artificial_parm (in_charge_identifier
, integer_type_node
);
275 DECL_CHAIN (parm
) = parms
;
277 arg_types
= hash_tree_chain (integer_type_node
, arg_types
);
279 /* Insert our new parameter(s) into the list. */
280 DECL_CHAIN (DECL_ARGUMENTS (fn
)) = parms
;
282 /* And rebuild the function type. */
283 fntype
= build_method_type_directly (basetype
, TREE_TYPE (TREE_TYPE (fn
)),
285 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)))
286 fntype
= build_exception_variant (fntype
,
287 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)));
288 if (TYPE_ATTRIBUTES (TREE_TYPE (fn
)))
289 fntype
= (cp_build_type_attribute_variant
290 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (fn
))));
291 TREE_TYPE (fn
) = fntype
;
293 /* Now we've got the in-charge parameter. */
294 DECL_HAS_IN_CHARGE_PARM_P (fn
) = 1;
297 /* Classes overload their constituent function names automatically.
298 When a function name is declared in a record structure,
299 its name is changed to it overloaded name. Since names for
300 constructors and destructors can conflict, we place a leading
303 CNAME is the name of the class we are grokking for.
305 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
307 FLAGS contains bits saying what's special about today's
308 arguments. DTOR_FLAG == DESTRUCTOR.
310 If FUNCTION is a destructor, then we must add the `auto-delete' field
311 as a second parameter. There is some hair associated with the fact
312 that we must "declare" this variable in the manner consistent with the
313 way the rest of the arguments were declared.
315 QUALS are the qualifiers for the this pointer. */
318 grokclassfn (tree ctype
, tree function
, enum overload_flags flags
)
320 tree fn_name
= DECL_NAME (function
);
322 /* Even within an `extern "C"' block, members get C++ linkage. See
323 [dcl.link] for details. */
324 SET_DECL_LANGUAGE (function
, lang_cplusplus
);
326 if (fn_name
== NULL_TREE
)
328 error ("name missing for member function");
329 fn_name
= get_identifier ("<anonymous>");
330 DECL_NAME (function
) = fn_name
;
333 DECL_CONTEXT (function
) = ctype
;
335 if (flags
== DTOR_FLAG
)
336 DECL_DESTRUCTOR_P (function
) = 1;
338 if (flags
== DTOR_FLAG
|| DECL_CONSTRUCTOR_P (function
))
339 maybe_retrofit_in_chrg (function
);
342 /* Create an ARRAY_REF, checking for the user doing things backwards
343 along the way. DECLTYPE_P is for N3276, as in the parser. */
346 grok_array_decl (location_t loc
, tree array_expr
, tree index_exp
,
351 tree orig_array_expr
= array_expr
;
352 tree orig_index_exp
= index_exp
;
354 if (error_operand_p (array_expr
) || error_operand_p (index_exp
))
355 return error_mark_node
;
357 if (processing_template_decl
)
359 if (type_dependent_expression_p (array_expr
)
360 || type_dependent_expression_p (index_exp
))
361 return build_min_nt_loc (loc
, ARRAY_REF
, array_expr
, index_exp
,
362 NULL_TREE
, NULL_TREE
);
363 array_expr
= build_non_dependent_expr (array_expr
);
364 index_exp
= build_non_dependent_expr (index_exp
);
367 type
= TREE_TYPE (array_expr
);
369 type
= non_reference (type
);
371 /* If they have an `operator[]', use that. */
372 if (MAYBE_CLASS_TYPE_P (type
) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp
)))
374 tsubst_flags_t complain
= tf_warning_or_error
;
376 complain
|= tf_decltype
;
377 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, array_expr
,
378 index_exp
, NULL_TREE
, /*overload=*/NULL
, complain
);
384 /* Otherwise, create an ARRAY_REF for a pointer or array type.
385 It is a little-known fact that, if `a' is an array and `i' is
386 an int, you can write `i[a]', which means the same thing as
388 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
391 p1
= build_expr_type_conversion (WANT_POINTER
, array_expr
, false);
393 if (TREE_CODE (TREE_TYPE (index_exp
)) == ARRAY_TYPE
)
396 p2
= build_expr_type_conversion (WANT_POINTER
, index_exp
, false);
398 i1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, array_expr
,
400 i2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, index_exp
,
403 if ((p1
&& i2
) && (i1
&& p2
))
404 error ("ambiguous conversion for array subscript");
407 array_expr
= p1
, index_exp
= i2
;
409 array_expr
= p2
, index_exp
= i1
;
412 error ("invalid types %<%T[%T]%> for array subscript",
413 type
, TREE_TYPE (index_exp
));
414 return error_mark_node
;
417 if (array_expr
== error_mark_node
|| index_exp
== error_mark_node
)
418 error ("ambiguous conversion for array subscript");
420 expr
= build_array_ref (input_location
, array_expr
, index_exp
);
422 if (processing_template_decl
&& expr
!= error_mark_node
)
423 return build_min_non_dep (ARRAY_REF
, expr
, orig_array_expr
, orig_index_exp
,
424 NULL_TREE
, NULL_TREE
);
428 /* Given the cast expression EXP, checking out its validity. Either return
429 an error_mark_node if there was an unavoidable error, return a cast to
430 void for trying to delete a pointer w/ the value 0, or return the
431 call to delete. If DOING_VEC is true, we handle things differently
432 for doing an array delete.
433 Implements ARM $5.3.4. This is called from the parser. */
436 delete_sanity (tree exp
, tree size
, bool doing_vec
, int use_global_delete
,
437 tsubst_flags_t complain
)
441 if (exp
== error_mark_node
)
444 if (processing_template_decl
)
446 t
= build_min (DELETE_EXPR
, void_type_node
, exp
, size
);
447 DELETE_EXPR_USE_GLOBAL (t
) = use_global_delete
;
448 DELETE_EXPR_USE_VEC (t
) = doing_vec
;
449 TREE_SIDE_EFFECTS (t
) = 1;
453 /* An array can't have been allocated by new, so complain. */
454 if (TREE_CODE (TREE_TYPE (exp
)) == ARRAY_TYPE
)
455 warning (0, "deleting array %q#E", exp
);
457 t
= build_expr_type_conversion (WANT_POINTER
, exp
, true);
459 if (t
== NULL_TREE
|| t
== error_mark_node
)
461 error ("type %q#T argument given to %<delete%>, expected pointer",
463 return error_mark_node
;
466 type
= TREE_TYPE (t
);
468 /* As of Valley Forge, you can delete a pointer to const. */
470 /* You can't delete functions. */
471 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
473 error ("cannot delete a function. Only pointer-to-objects are "
474 "valid arguments to %<delete%>");
475 return error_mark_node
;
478 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
479 if (VOID_TYPE_P (TREE_TYPE (type
)))
481 warning (0, "deleting %qT is undefined", type
);
485 /* Deleting a pointer with the value zero is valid and has no effect. */
486 if (integer_zerop (t
))
487 return build1 (NOP_EXPR
, void_type_node
, t
);
490 return build_vec_delete (t
, /*maxindex=*/NULL_TREE
,
491 sfk_deleting_destructor
,
492 use_global_delete
, complain
);
494 return build_delete (type
, t
, sfk_deleting_destructor
,
495 LOOKUP_NORMAL
, use_global_delete
,
499 /* Report an error if the indicated template declaration is not the
500 sort of thing that should be a member template. */
503 check_member_template (tree tmpl
)
507 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
508 decl
= DECL_TEMPLATE_RESULT (tmpl
);
510 if (TREE_CODE (decl
) == FUNCTION_DECL
511 || DECL_ALIAS_TEMPLATE_P (tmpl
)
512 || (TREE_CODE (decl
) == TYPE_DECL
513 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))))
515 /* The parser rejects template declarations in local classes
516 (with the exception of generic lambdas). */
517 gcc_assert (!current_function_decl
|| LAMBDA_FUNCTION_P (decl
));
518 /* The parser rejects any use of virtual in a function template. */
519 gcc_assert (!(TREE_CODE (decl
) == FUNCTION_DECL
520 && DECL_VIRTUAL_P (decl
)));
522 /* The debug-information generating code doesn't know what to do
523 with member templates. */
524 DECL_IGNORED_P (tmpl
) = 1;
526 else if (variable_template_p (tmpl
))
529 error ("template declaration of %q#D", decl
);
532 /* Return true iff TYPE is a valid Java parameter or return type. */
535 acceptable_java_type (tree type
)
537 if (type
== error_mark_node
)
540 if (VOID_TYPE_P (type
) || TYPE_FOR_JAVA (type
))
542 if (TYPE_PTR_P (type
) || TREE_CODE (type
) == REFERENCE_TYPE
)
544 type
= TREE_TYPE (type
);
545 if (TREE_CODE (type
) == RECORD_TYPE
)
548 if (! TYPE_FOR_JAVA (type
))
550 if (! CLASSTYPE_TEMPLATE_INFO (type
))
552 args
= CLASSTYPE_TI_ARGS (type
);
553 i
= TREE_VEC_LENGTH (args
);
556 type
= TREE_VEC_ELT (args
, i
);
557 if (TYPE_PTR_P (type
))
558 type
= TREE_TYPE (type
);
559 if (! TYPE_FOR_JAVA (type
))
568 /* For a METHOD in a Java class CTYPE, return true if
569 the parameter and return types are valid Java types.
570 Otherwise, print appropriate error messages, and return false. */
573 check_java_method (tree method
)
576 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (method
));
577 tree ret_type
= TREE_TYPE (TREE_TYPE (method
));
579 if (!acceptable_java_type (ret_type
))
581 error ("Java method %qD has non-Java return type %qT",
586 arg_types
= TREE_CHAIN (arg_types
);
587 if (DECL_HAS_IN_CHARGE_PARM_P (method
))
588 arg_types
= TREE_CHAIN (arg_types
);
589 if (DECL_HAS_VTT_PARM_P (method
))
590 arg_types
= TREE_CHAIN (arg_types
);
592 for (; arg_types
!= NULL_TREE
; arg_types
= TREE_CHAIN (arg_types
))
594 tree type
= TREE_VALUE (arg_types
);
595 if (!acceptable_java_type (type
))
597 if (type
!= error_mark_node
)
598 error ("Java method %qD has non-Java parameter type %qT",
606 /* Sanity check: report error if this function FUNCTION is not
607 really a member of the class (CTYPE) it is supposed to belong to.
608 TEMPLATE_PARMS is used to specify the template parameters of a member
609 template passed as FUNCTION_DECL. If the member template is passed as a
610 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
611 from the declaration. If the function is not a function template, it
613 It returns the original declaration for the function, NULL_TREE if
614 no declaration was found, error_mark_node if an error was emitted. */
617 check_classfn (tree ctype
, tree function
, tree template_parms
)
623 if (DECL_USE_TEMPLATE (function
)
624 && !(TREE_CODE (function
) == TEMPLATE_DECL
625 && DECL_TEMPLATE_SPECIALIZATION (function
))
626 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function
)))
627 /* Since this is a specialization of a member template,
628 we're not going to find the declaration in the class.
631 struct S { template <typename T> void f(T); };
632 template <> void S::f(int);
634 we're not going to find `S::f(int)', but there's no
635 reason we should, either. We let our callers know we didn't
636 find the method, but we don't complain. */
639 /* Basic sanity check: for a template function, the template parameters
640 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
641 if (TREE_CODE (function
) == TEMPLATE_DECL
)
644 && !comp_template_parms (template_parms
,
645 DECL_TEMPLATE_PARMS (function
)))
647 error ("template parameter lists provided don%'t match the "
648 "template parameters of %qD", function
);
649 return error_mark_node
;
651 template_parms
= DECL_TEMPLATE_PARMS (function
);
654 /* OK, is this a definition of a member template? */
655 is_template
= (template_parms
!= NULL_TREE
);
659 A destructor shall not be a member template. */
660 if (DECL_DESTRUCTOR_P (function
) && is_template
)
662 error ("destructor %qD declared as member template", function
);
663 return error_mark_node
;
666 /* We must enter the scope here, because conversion operators are
667 named by target type, and type equivalence relies on typenames
668 resolving within the scope of CTYPE. */
669 pushed_scope
= push_scope (ctype
);
670 ix
= class_method_index_for_fn (complete_type (ctype
), function
);
673 vec
<tree
, va_gc
> *methods
= CLASSTYPE_METHOD_VEC (ctype
);
674 tree fndecls
, fndecl
= 0;
676 const char *format
= NULL
;
678 for (fndecls
= (*methods
)[ix
];
679 fndecls
; fndecls
= OVL_NEXT (fndecls
))
683 fndecl
= OVL_CURRENT (fndecls
);
684 p1
= TYPE_ARG_TYPES (TREE_TYPE (function
));
685 p2
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
687 /* We cannot simply call decls_match because this doesn't
688 work for static member functions that are pretending to
689 be methods, and because the name may have been changed by
692 /* Get rid of the this parameter on functions that become
694 if (DECL_STATIC_FUNCTION_P (fndecl
)
695 && TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
696 p1
= TREE_CHAIN (p1
);
698 /* A member template definition only matches a member template
700 if (is_template
!= (TREE_CODE (fndecl
) == TEMPLATE_DECL
))
703 /* ref-qualifier or absence of same must match. */
704 if (type_memfn_rqual (TREE_TYPE (function
))
705 != type_memfn_rqual (TREE_TYPE (fndecl
)))
708 /* While finding a match, same types and params are not enough
709 if the function is versioned. Also check version ("target")
711 if (same_type_p (TREE_TYPE (TREE_TYPE (function
)),
712 TREE_TYPE (TREE_TYPE (fndecl
)))
713 && compparms (p1
, p2
)
714 && !targetm
.target_option
.function_versions (function
, fndecl
)
716 || comp_template_parms (template_parms
,
717 DECL_TEMPLATE_PARMS (fndecl
)))
718 && (DECL_TEMPLATE_SPECIALIZATION (function
)
719 == DECL_TEMPLATE_SPECIALIZATION (fndecl
))
720 && (!DECL_TEMPLATE_SPECIALIZATION (function
)
721 || (DECL_TI_TEMPLATE (function
)
722 == DECL_TI_TEMPLATE (fndecl
))))
728 pop_scope (pushed_scope
);
729 return OVL_CURRENT (fndecls
);
732 error_at (DECL_SOURCE_LOCATION (function
),
733 "prototype for %q#D does not match any in class %qT",
735 is_conv_op
= DECL_CONV_FN_P (fndecl
);
738 ix
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
739 fndecls
= (*methods
)[ix
];
742 fndecl
= OVL_CURRENT (fndecls
);
743 fndecls
= OVL_NEXT (fndecls
);
745 if (!fndecls
&& is_conv_op
)
747 if (methods
->length () > (size_t) ++ix
)
749 fndecls
= (*methods
)[ix
];
750 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls
)))
762 format
= N_("candidates are: %+#D");
764 format
= N_("candidate is: %+#D");
765 error (format
, fndecl
);
768 else if (!COMPLETE_TYPE_P (ctype
))
769 cxx_incomplete_type_error (function
, ctype
);
771 error ("no %q#D member function declared in class %qT",
775 pop_scope (pushed_scope
);
776 return error_mark_node
;
779 /* DECL is a function with vague linkage. Remember it so that at the
780 end of the translation unit we can decide whether or not to emit
784 note_vague_linkage_fn (tree decl
)
786 DECL_DEFER_OUTPUT (decl
) = 1;
787 vec_safe_push (deferred_fns
, decl
);
790 /* As above, but for variable template instantiations. */
793 note_variable_template_instantiation (tree decl
)
795 vec_safe_push (pending_statics
, decl
);
798 /* We have just processed the DECL, which is a static data member.
799 The other parameters are as for cp_finish_decl. */
802 finish_static_data_member_decl (tree decl
,
803 tree init
, bool init_const_expr_p
,
807 DECL_CONTEXT (decl
) = current_class_type
;
809 /* We cannot call pushdecl here, because that would fill in the
810 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
811 the right thing, namely, to put this decl out straight away. */
813 if (! processing_template_decl
)
814 vec_safe_push (pending_statics
, decl
);
816 if (LOCAL_CLASS_P (current_class_type
)
817 /* We already complained about the template definition. */
818 && !DECL_TEMPLATE_INSTANTIATION (decl
))
819 permerror (input_location
, "local class %q#T shall not have static data member %q#D",
820 current_class_type
, decl
);
822 for (tree t
= current_class_type
; TYPE_P (t
);
823 t
= CP_TYPE_CONTEXT (t
))
824 if (TYPE_ANONYMOUS_P (t
))
826 if (permerror (DECL_SOURCE_LOCATION (decl
),
827 "static data member %qD in unnamed class", decl
))
828 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)),
829 "unnamed class defined here");
833 DECL_IN_AGGR_P (decl
) = 1;
835 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
836 && TYPE_DOMAIN (TREE_TYPE (decl
)) == NULL_TREE
)
837 SET_VAR_HAD_UNKNOWN_BOUND (decl
);
839 cp_finish_decl (decl
, init
, init_const_expr_p
, asmspec_tree
, flags
);
842 /* DECLARATOR and DECLSPECS correspond to a class member. The other
843 parameters are as for cp_finish_decl. Return the DECL for the
844 class member declared. */
847 grokfield (const cp_declarator
*declarator
,
848 cp_decl_specifier_seq
*declspecs
,
849 tree init
, bool init_const_expr_p
,
854 const char *asmspec
= 0;
859 && TREE_CODE (init
) == TREE_LIST
860 && TREE_VALUE (init
) == error_mark_node
861 && TREE_CHAIN (init
) == NULL_TREE
)
864 value
= grokdeclarator (declarator
, declspecs
, FIELD
, init
!= 0, &attrlist
);
865 if (! value
|| value
== error_mark_node
)
866 /* friend or constructor went bad. */
867 return error_mark_node
;
868 if (TREE_TYPE (value
) == error_mark_node
)
871 if (TREE_CODE (value
) == TYPE_DECL
&& init
)
873 error ("typedef %qD is initialized (use decltype instead)", value
);
877 /* Pass friendly classes back. */
878 if (value
== void_type_node
)
882 name
= DECL_NAME (value
);
884 if (name
!= NULL_TREE
)
886 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
888 error ("explicit template argument list not allowed");
889 return error_mark_node
;
892 if (IDENTIFIER_POINTER (name
)[0] == '_'
893 && ! strcmp (IDENTIFIER_POINTER (name
), "_vptr"))
894 error ("member %qD conflicts with virtual function table field name",
898 /* Stash away type declarations. */
899 if (TREE_CODE (value
) == TYPE_DECL
)
901 DECL_NONLOCAL (value
) = 1;
902 DECL_CONTEXT (value
) = current_class_type
;
908 /* If this is a typedef that names the class for linkage purposes
909 (7.1.3p8), apply any attributes directly to the type. */
910 if (OVERLOAD_TYPE_P (TREE_TYPE (value
))
911 && value
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
912 attrflags
= ATTR_FLAG_TYPE_IN_PLACE
;
914 cplus_decl_attributes (&value
, attrlist
, attrflags
);
917 if (decl_spec_seq_has_spec_p (declspecs
, ds_typedef
)
918 && TREE_TYPE (value
) != error_mark_node
919 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))) != value
)
920 set_underlying_type (value
);
922 /* It's important that push_template_decl below follows
923 set_underlying_type above so that the created template
924 carries the properly set type of VALUE. */
925 if (processing_template_decl
)
926 value
= push_template_decl (value
);
928 record_locally_defined_typedef (value
);
932 int friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
934 if (!friendp
&& DECL_IN_AGGR_P (value
))
936 error ("%qD is already defined in %qT", value
, DECL_CONTEXT (value
));
937 return void_type_node
;
940 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
941 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
945 if (TREE_CODE (value
) == FUNCTION_DECL
)
947 if (init
== ridpointers
[(int)RID_DELETE
])
949 DECL_DELETED_FN (value
) = 1;
950 DECL_DECLARED_INLINE_P (value
) = 1;
951 DECL_INITIAL (value
) = error_mark_node
;
953 else if (init
== ridpointers
[(int)RID_DEFAULT
])
955 if (defaultable_fn_check (value
))
957 DECL_DEFAULTED_FN (value
) = 1;
958 DECL_INITIALIZED_IN_CLASS_P (value
) = 1;
959 DECL_DECLARED_INLINE_P (value
) = 1;
962 else if (TREE_CODE (init
) == DEFAULT_ARG
)
963 error ("invalid initializer for member function %qD", value
);
964 else if (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
)
966 if (integer_zerop (init
))
967 DECL_PURE_VIRTUAL_P (value
) = 1;
968 else if (error_operand_p (init
))
969 ; /* An error has already been reported. */
971 error ("invalid initializer for member function %qD",
976 gcc_assert (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
);
978 error ("initializer specified for friend function %qD",
981 error ("initializer specified for static member function %qD",
985 else if (TREE_CODE (value
) == FIELD_DECL
)
986 /* C++11 NSDMI, keep going. */;
987 else if (!VAR_P (value
))
991 /* Pass friend decls back. */
992 if ((TREE_CODE (value
) == FUNCTION_DECL
993 || TREE_CODE (value
) == TEMPLATE_DECL
)
994 && DECL_CONTEXT (value
) != current_class_type
)
997 if (processing_template_decl
&& VAR_OR_FUNCTION_DECL_P (value
))
999 value
= push_template_decl (value
);
1000 if (error_operand_p (value
))
1001 return error_mark_node
;
1005 cplus_decl_attributes (&value
, attrlist
, 0);
1007 if (init
&& DIRECT_LIST_INIT_P (init
))
1008 flags
= LOOKUP_NORMAL
;
1010 flags
= LOOKUP_IMPLICIT
;
1012 switch (TREE_CODE (value
))
1015 finish_static_data_member_decl (value
, init
, init_const_expr_p
,
1016 asmspec_tree
, flags
);
1021 error ("%<asm%> specifiers are not permitted on non-static data members");
1022 if (DECL_INITIAL (value
) == error_mark_node
)
1023 init
= error_mark_node
;
1024 cp_finish_decl (value
, init
, /*init_const_expr_p=*/false,
1026 DECL_IN_AGGR_P (value
) = 1;
1031 set_user_assembler_name (value
, asmspec
);
1033 cp_finish_decl (value
,
1035 /*init_const_expr_p=*/false,
1036 asmspec_tree
, flags
);
1038 /* Pass friends back this way. */
1039 if (DECL_FRIEND_P (value
))
1040 return void_type_node
;
1042 DECL_IN_AGGR_P (value
) = 1;
1051 /* Like `grokfield', but for bitfields.
1052 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1055 grokbitfield (const cp_declarator
*declarator
,
1056 cp_decl_specifier_seq
*declspecs
, tree width
,
1059 tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
, 0, &attrlist
);
1061 if (value
== error_mark_node
)
1062 return NULL_TREE
; /* friends went bad. */
1063 if (TREE_TYPE (value
) == error_mark_node
)
1066 /* Pass friendly classes back. */
1067 if (VOID_TYPE_P (value
))
1068 return void_type_node
;
1070 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value
))
1071 && (POINTER_TYPE_P (value
)
1072 || !dependent_type_p (TREE_TYPE (value
))))
1074 error ("bit-field %qD with non-integral type", value
);
1075 return error_mark_node
;
1078 if (TREE_CODE (value
) == TYPE_DECL
)
1080 error ("cannot declare %qD to be a bit-field type", value
);
1084 /* Usually, finish_struct_1 catches bitfields with invalid types.
1085 But, in the case of bitfields with function type, we confuse
1086 ourselves into thinking they are member functions, so we must
1088 if (TREE_CODE (value
) == FUNCTION_DECL
)
1090 error ("cannot declare bit-field %qD with function type",
1095 if (DECL_IN_AGGR_P (value
))
1097 error ("%qD is already defined in the class %qT", value
,
1098 DECL_CONTEXT (value
));
1099 return void_type_node
;
1102 if (TREE_STATIC (value
))
1104 error ("static member %qD cannot be a bit-field", value
);
1107 cp_finish_decl (value
, NULL_TREE
, false, NULL_TREE
, 0);
1109 if (width
!= error_mark_node
)
1111 /* The width must be an integer type. */
1112 if (!type_dependent_expression_p (width
)
1113 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width
)))
1114 error ("width of bit-field %qD has non-integral type %qT", value
,
1116 DECL_INITIAL (value
) = width
;
1117 SET_DECL_C_BIT_FIELD (value
);
1120 DECL_IN_AGGR_P (value
) = 1;
1123 cplus_decl_attributes (&value
, attrlist
, /*flags=*/0);
1129 /* Returns true iff ATTR is an attribute which needs to be applied at
1130 instantiation time rather than template definition time. */
1133 is_late_template_attribute (tree attr
, tree decl
)
1135 tree name
= get_attribute_name (attr
);
1136 tree args
= TREE_VALUE (attr
);
1137 const struct attribute_spec
*spec
= lookup_attribute_spec (name
);
1141 /* Unknown attribute. */
1144 /* Attribute weak handling wants to write out assembly right away. */
1145 if (is_attribute_p ("weak", name
))
1148 /* Attribute unused is applied directly, as it appertains to
1150 if (is_attribute_p ("unused", name
))
1153 /* #pragma omp declare simd attribute needs to be always deferred. */
1155 && is_attribute_p ("omp declare simd", name
))
1158 /* If any of the arguments are dependent expressions, we can't evaluate
1159 the attribute until instantiation time. */
1160 for (arg
= args
; arg
; arg
= TREE_CHAIN (arg
))
1162 tree t
= TREE_VALUE (arg
);
1164 /* If the first attribute argument is an identifier, only consider
1165 second and following arguments. Attributes like mode, format,
1166 cleanup and several target specific attributes aren't late
1167 just because they have an IDENTIFIER_NODE as first argument. */
1168 if (arg
== args
&& identifier_p (t
))
1171 if (value_dependent_expression_p (t
)
1172 || type_dependent_expression_p (t
))
1176 if (TREE_CODE (decl
) == TYPE_DECL
1178 || spec
->type_required
)
1180 tree type
= TYPE_P (decl
) ? decl
: TREE_TYPE (decl
);
1182 /* We can't apply any attributes to a completely unknown type until
1183 instantiation time. */
1184 enum tree_code code
= TREE_CODE (type
);
1185 if (code
== TEMPLATE_TYPE_PARM
1186 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
1187 || code
== TYPENAME_TYPE
)
1189 /* Also defer most attributes on dependent types. This is not
1190 necessary in all cases, but is the better default. */
1191 else if (dependent_type_p (type
)
1192 /* But attributes abi_tag and visibility specifically apply
1194 && !is_attribute_p ("abi_tag", name
)
1195 && !is_attribute_p ("visibility", name
))
1204 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1205 applied at instantiation time and return them. If IS_DEPENDENT is true,
1206 the declaration itself is dependent, so all attributes should be applied
1207 at instantiation time. */
1210 splice_template_attributes (tree
*attr_p
, tree decl
)
1213 tree late_attrs
= NULL_TREE
;
1214 tree
*q
= &late_attrs
;
1221 if (is_late_template_attribute (*p
, decl
))
1223 ATTR_IS_DEPENDENT (*p
) = 1;
1225 *p
= TREE_CHAIN (*p
);
1226 q
= &TREE_CHAIN (*q
);
1230 p
= &TREE_CHAIN (*p
);
1236 /* Remove any late attributes from the list in ATTR_P and attach them to
1240 save_template_attributes (tree
*attr_p
, tree
*decl_p
)
1244 if (attr_p
&& *attr_p
== error_mark_node
)
1247 tree late_attrs
= splice_template_attributes (attr_p
, *decl_p
);
1251 if (DECL_P (*decl_p
))
1252 q
= &DECL_ATTRIBUTES (*decl_p
);
1254 q
= &TYPE_ATTRIBUTES (*decl_p
);
1256 tree old_attrs
= *q
;
1258 /* Merge the late attributes at the beginning with the attribute
1260 late_attrs
= merge_attributes (late_attrs
, *q
);
1263 if (!DECL_P (*decl_p
) && *decl_p
== TYPE_MAIN_VARIANT (*decl_p
))
1265 /* We've added new attributes directly to the main variant, so
1266 now we need to update all of the other variants to include
1267 these new attributes. */
1269 for (variant
= TYPE_NEXT_VARIANT (*decl_p
); variant
;
1270 variant
= TYPE_NEXT_VARIANT (variant
))
1272 gcc_assert (TYPE_ATTRIBUTES (variant
) == old_attrs
);
1273 TYPE_ATTRIBUTES (variant
) = TYPE_ATTRIBUTES (*decl_p
);
1278 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1279 to a typedef which gives a previously anonymous class or enum a name for
1280 linkage purposes. */
1283 attributes_naming_typedef_ok (tree attrs
)
1285 for (; attrs
; attrs
= TREE_CHAIN (attrs
))
1287 tree name
= get_attribute_name (attrs
);
1288 if (is_attribute_p ("vector_size", name
))
1294 /* Like reconstruct_complex_type, but handle also template trees. */
1297 cp_reconstruct_complex_type (tree type
, tree bottom
)
1300 bool late_return_type_p
= false;
1302 if (TYPE_PTR_P (type
))
1304 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1305 outer
= build_pointer_type_for_mode (inner
, TYPE_MODE (type
),
1306 TYPE_REF_CAN_ALIAS_ALL (type
));
1308 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
1310 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1311 outer
= build_reference_type_for_mode (inner
, TYPE_MODE (type
),
1312 TYPE_REF_CAN_ALIAS_ALL (type
));
1314 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1316 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1317 outer
= build_cplus_array_type (inner
, TYPE_DOMAIN (type
));
1318 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1319 element type qualification will be handled by the recursive
1320 cp_reconstruct_complex_type call and cp_build_qualified_type
1321 for ARRAY_TYPEs changes the element type. */
1324 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
1326 late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (type
);
1327 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1328 outer
= build_function_type (inner
, TYPE_ARG_TYPES (type
));
1329 outer
= apply_memfn_quals (outer
,
1330 type_memfn_quals (type
),
1331 type_memfn_rqual (type
));
1333 else if (TREE_CODE (type
) == METHOD_TYPE
)
1335 late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (type
);
1336 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1337 /* The build_method_type_directly() routine prepends 'this' to argument list,
1338 so we must compensate by getting rid of it. */
1340 = build_method_type_directly
1341 (class_of_this_parm (type
), inner
,
1342 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
1344 else if (TREE_CODE (type
) == OFFSET_TYPE
)
1346 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1347 outer
= build_offset_type (TYPE_OFFSET_BASETYPE (type
), inner
);
1352 if (TYPE_ATTRIBUTES (type
))
1353 outer
= cp_build_type_attribute_variant (outer
, TYPE_ATTRIBUTES (type
));
1354 outer
= cp_build_qualified_type (outer
, cp_type_quals (type
));
1356 if (late_return_type_p
)
1357 TYPE_HAS_LATE_RETURN_TYPE (outer
) = 1;
1362 /* Replaces any constexpr expression that may be into the attributes
1363 arguments with their reduced value. */
1366 cp_check_const_attributes (tree attributes
)
1368 if (attributes
== error_mark_node
)
1372 for (attr
= attributes
; attr
; attr
= TREE_CHAIN (attr
))
1375 for (arg
= TREE_VALUE (attr
); arg
; arg
= TREE_CHAIN (arg
))
1377 tree expr
= TREE_VALUE (arg
);
1379 TREE_VALUE (arg
) = maybe_constant_value (expr
);
1384 /* Return true if TYPE is an OpenMP mappable type. */
1386 cp_omp_mappable_type (tree type
)
1388 /* Mappable type has to be complete. */
1389 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
1391 /* Arrays have mappable type if the elements have mappable type. */
1392 while (TREE_CODE (type
) == ARRAY_TYPE
)
1393 type
= TREE_TYPE (type
);
1394 /* A mappable type cannot contain virtual members. */
1395 if (CLASS_TYPE_P (type
) && CLASSTYPE_VTABLES (type
))
1397 /* All data members must be non-static. */
1398 if (CLASS_TYPE_P (type
))
1401 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1402 if (TREE_CODE (field
) == VAR_DECL
)
1404 /* All fields must have mappable types. */
1405 else if (TREE_CODE (field
) == FIELD_DECL
1406 && !cp_omp_mappable_type (TREE_TYPE (field
)))
1412 /* Like decl_attributes, but handle C++ complexity. */
1415 cplus_decl_attributes (tree
*decl
, tree attributes
, int flags
)
1417 if (*decl
== NULL_TREE
|| *decl
== void_type_node
1418 || *decl
== error_mark_node
)
1421 /* Add implicit "omp declare target" attribute if requested. */
1422 if (scope_chain
->omp_declare_target_attribute
1423 && ((TREE_CODE (*decl
) == VAR_DECL
&& TREE_STATIC (*decl
))
1424 || TREE_CODE (*decl
) == FUNCTION_DECL
))
1426 if (TREE_CODE (*decl
) == VAR_DECL
1427 && DECL_CLASS_SCOPE_P (*decl
))
1428 error ("%q+D static data member inside of declare target directive",
1430 else if (TREE_CODE (*decl
) == VAR_DECL
1431 && (DECL_FUNCTION_SCOPE_P (*decl
)
1432 || (current_function_decl
&& !DECL_EXTERNAL (*decl
))))
1433 error ("%q+D in block scope inside of declare target directive",
1435 else if (!processing_template_decl
1436 && TREE_CODE (*decl
) == VAR_DECL
1437 && !cp_omp_mappable_type (TREE_TYPE (*decl
)))
1438 error ("%q+D in declare target directive does not have mappable type",
1441 attributes
= tree_cons (get_identifier ("omp declare target"),
1442 NULL_TREE
, attributes
);
1445 if (processing_template_decl
)
1447 if (check_for_bare_parameter_packs (attributes
))
1450 save_template_attributes (&attributes
, decl
);
1453 cp_check_const_attributes (attributes
);
1455 if (TREE_CODE (*decl
) == TEMPLATE_DECL
)
1456 decl
= &DECL_TEMPLATE_RESULT (*decl
);
1458 if (TREE_TYPE (*decl
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl
)))
1461 = decl_attributes (decl
, attributes
, flags
| ATTR_FLAG_FUNCTION_NEXT
);
1462 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (*decl
)),
1466 decl_attributes (decl
, attributes
, flags
);
1468 if (TREE_CODE (*decl
) == TYPE_DECL
)
1469 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl
), TREE_TYPE (*decl
));
1472 /* Walks through the namespace- or function-scope anonymous union
1473 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1474 Returns one of the fields for use in the mangled name. */
1477 build_anon_union_vars (tree type
, tree object
)
1479 tree main_decl
= NULL_TREE
;
1482 /* Rather than write the code to handle the non-union case,
1483 just give an error. */
1484 if (TREE_CODE (type
) != UNION_TYPE
)
1486 error ("anonymous struct not inside named type");
1487 return error_mark_node
;
1490 for (field
= TYPE_FIELDS (type
);
1492 field
= DECL_CHAIN (field
))
1497 if (DECL_ARTIFICIAL (field
))
1499 if (TREE_CODE (field
) != FIELD_DECL
)
1501 permerror (input_location
, "%q+#D invalid; an anonymous union can only "
1502 "have non-static data members", field
);
1506 if (TREE_PRIVATE (field
))
1507 permerror (input_location
, "private member %q+#D in anonymous union", field
);
1508 else if (TREE_PROTECTED (field
))
1509 permerror (input_location
, "protected member %q+#D in anonymous union", field
);
1511 if (processing_template_decl
)
1512 ref
= build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
, object
,
1513 DECL_NAME (field
), NULL_TREE
);
1515 ref
= build_class_member_access_expr (object
, field
, NULL_TREE
,
1516 false, tf_warning_or_error
);
1518 if (DECL_NAME (field
))
1522 decl
= build_decl (input_location
,
1523 VAR_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
1524 DECL_ANON_UNION_VAR_P (decl
) = 1;
1525 DECL_ARTIFICIAL (decl
) = 1;
1527 base
= get_base_address (object
);
1528 TREE_PUBLIC (decl
) = TREE_PUBLIC (base
);
1529 TREE_STATIC (decl
) = TREE_STATIC (base
);
1530 DECL_EXTERNAL (decl
) = DECL_EXTERNAL (base
);
1532 SET_DECL_VALUE_EXPR (decl
, ref
);
1533 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1535 decl
= pushdecl (decl
);
1537 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1538 decl
= build_anon_union_vars (TREE_TYPE (field
), ref
);
1542 if (main_decl
== NULL_TREE
)
1549 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1550 anonymous union, then all members must be laid out together. PUBLIC_P
1551 is nonzero if this union is not declared static. */
1554 finish_anon_union (tree anon_union_decl
)
1560 if (anon_union_decl
== error_mark_node
)
1563 type
= TREE_TYPE (anon_union_decl
);
1564 public_p
= TREE_PUBLIC (anon_union_decl
);
1566 /* The VAR_DECL's context is the same as the TYPE's context. */
1567 DECL_CONTEXT (anon_union_decl
) = DECL_CONTEXT (TYPE_NAME (type
));
1569 if (TYPE_FIELDS (type
) == NULL_TREE
)
1574 error ("namespace-scope anonymous aggregates must be static");
1578 main_decl
= build_anon_union_vars (type
, anon_union_decl
);
1579 if (main_decl
== error_mark_node
)
1581 if (main_decl
== NULL_TREE
)
1583 warning (0, "anonymous union with no members");
1587 if (!processing_template_decl
)
1589 /* Use main_decl to set the mangled name. */
1590 DECL_NAME (anon_union_decl
) = DECL_NAME (main_decl
);
1591 maybe_commonize_var (anon_union_decl
);
1592 if (TREE_STATIC (anon_union_decl
) || DECL_EXTERNAL (anon_union_decl
))
1593 mangle_decl (anon_union_decl
);
1594 DECL_NAME (anon_union_decl
) = NULL_TREE
;
1597 pushdecl (anon_union_decl
);
1598 cp_finish_decl (anon_union_decl
, NULL_TREE
, false, NULL_TREE
, 0);
1601 /* Auxiliary functions to make type signatures for
1602 `operator new' and `operator delete' correspond to
1603 what compiler will be expecting. */
1606 coerce_new_type (tree type
)
1609 tree args
= TYPE_ARG_TYPES (type
);
1611 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1613 if (!same_type_p (TREE_TYPE (type
), ptr_type_node
))
1616 error ("%<operator new%> must return type %qT", ptr_type_node
);
1619 if (args
&& args
!= void_list_node
)
1621 if (TREE_PURPOSE (args
))
1623 /* [basic.stc.dynamic.allocation]
1625 The first parameter shall not have an associated default
1627 error ("the first parameter of %<operator new%> cannot "
1628 "have a default argument");
1629 /* Throw away the default argument. */
1630 TREE_PURPOSE (args
) = NULL_TREE
;
1633 if (!same_type_p (TREE_VALUE (args
), size_type_node
))
1636 args
= TREE_CHAIN (args
);
1643 permerror (input_location
, "%<operator new%> takes type %<size_t%> (%qT) "
1644 "as first parameter", size_type_node
);
1649 args
= tree_cons (NULL_TREE
, size_type_node
, args
);
1652 type
= build_exception_variant
1653 (build_function_type (ptr_type_node
, args
),
1654 TYPE_RAISES_EXCEPTIONS (type
));
1662 coerce_delete_type (tree type
)
1665 tree args
= TYPE_ARG_TYPES (type
);
1667 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1669 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
1672 error ("%<operator delete%> must return type %qT", void_type_node
);
1675 if (!args
|| args
== void_list_node
1676 || !same_type_p (TREE_VALUE (args
), ptr_type_node
))
1679 if (args
&& args
!= void_list_node
)
1680 args
= TREE_CHAIN (args
);
1681 error ("%<operator delete%> takes type %qT as first parameter",
1687 args
= tree_cons (NULL_TREE
, ptr_type_node
, args
);
1690 type
= build_exception_variant
1691 (build_function_type (void_type_node
, args
),
1692 TYPE_RAISES_EXCEPTIONS (type
));
1700 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1701 and mark them as needed. */
1704 mark_vtable_entries (tree decl
)
1707 unsigned HOST_WIDE_INT idx
;
1709 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
1714 STRIP_NOPS (fnaddr
);
1716 if (TREE_CODE (fnaddr
) != ADDR_EXPR
1717 && TREE_CODE (fnaddr
) != FDESC_EXPR
)
1718 /* This entry is an offset: a virtual base class offset, a
1719 virtual call offset, an RTTI offset, etc. */
1722 fn
= TREE_OPERAND (fnaddr
, 0);
1723 TREE_ADDRESSABLE (fn
) = 1;
1724 /* When we don't have vcall offsets, we output thunks whenever
1725 we output the vtables that contain them. With vcall offsets,
1726 we know all the thunks we'll need when we emit a virtual
1727 function, so we emit the thunks there instead. */
1728 if (DECL_THUNK_P (fn
))
1729 use_thunk (fn
, /*emit_p=*/0);
1734 /* Set DECL up to have the closest approximation of "initialized common"
1735 linkage available. */
1738 comdat_linkage (tree decl
)
1741 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1742 else if (TREE_CODE (decl
) == FUNCTION_DECL
1743 || (VAR_P (decl
) && DECL_ARTIFICIAL (decl
)))
1744 /* We can just emit function and compiler-generated variables
1745 statically; having multiple copies is (for the most part) only
1748 There are two correctness issues, however: the address of a
1749 template instantiation with external linkage should be the
1750 same, independent of what translation unit asks for the
1751 address, and this will not hold when we emit multiple copies of
1752 the function. However, there's little else we can do.
1754 Also, by default, the typeinfo implementation assumes that
1755 there will be only one copy of the string used as the name for
1756 each type. Therefore, if weak symbols are unavailable, the
1757 run-time library should perform a more conservative check; it
1758 should perform a string comparison, rather than an address
1760 TREE_PUBLIC (decl
) = 0;
1763 /* Static data member template instantiations, however, cannot
1764 have multiple copies. */
1765 if (DECL_INITIAL (decl
) == 0
1766 || DECL_INITIAL (decl
) == error_mark_node
)
1767 DECL_COMMON (decl
) = 1;
1768 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
1770 DECL_COMMON (decl
) = 1;
1771 DECL_INITIAL (decl
) = error_mark_node
;
1773 else if (!DECL_EXPLICIT_INSTANTIATION (decl
))
1775 /* We can't do anything useful; leave vars for explicit
1777 DECL_EXTERNAL (decl
) = 1;
1778 DECL_NOT_REALLY_EXTERN (decl
) = 0;
1782 DECL_COMDAT (decl
) = 1;
1785 /* For win32 we also want to put explicit instantiations in
1786 linkonce sections, so that they will be merged with implicit
1787 instantiations; otherwise we get duplicate symbol errors.
1788 For Darwin we do not want explicit instantiations to be
1792 maybe_make_one_only (tree decl
)
1794 /* We used to say that this was not necessary on targets that support weak
1795 symbols, because the implicit instantiations will defer to the explicit
1796 one. However, that's not actually the case in SVR4; a strong definition
1797 after a weak one is an error. Also, not making explicit
1798 instantiations one_only means that we can end up with two copies of
1799 some template instantiations. */
1803 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1804 we can get away with not emitting them if they aren't used. We need
1805 to for variables so that cp_finish_decl will update their linkage,
1806 because their DECL_INITIAL may not have been set properly yet. */
1808 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1809 || (! DECL_EXPLICIT_INSTANTIATION (decl
)
1810 && ! DECL_TEMPLATE_SPECIALIZATION (decl
)))
1812 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1816 varpool_node
*node
= varpool_node::get_create (decl
);
1817 DECL_COMDAT (decl
) = 1;
1818 /* Mark it needed so we don't forget to emit it. */
1819 node
->forced_by_abi
= true;
1820 TREE_USED (decl
) = 1;
1825 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1826 This predicate will give the right answer during parsing of the
1827 function, which other tests may not. */
1830 vague_linkage_p (tree decl
)
1832 /* Unfortunately, import_export_decl has not always been called
1833 before the function is processed, so we cannot simply check
1835 if (DECL_COMDAT (decl
)
1836 || (((TREE_CODE (decl
) == FUNCTION_DECL
1837 && DECL_DECLARED_INLINE_P (decl
))
1838 || (DECL_LANG_SPECIFIC (decl
)
1839 && DECL_TEMPLATE_INSTANTIATION (decl
)))
1840 && TREE_PUBLIC (decl
)))
1842 else if (DECL_FUNCTION_SCOPE_P (decl
))
1843 /* A local static in an inline effectively has vague linkage. */
1844 return (TREE_STATIC (decl
)
1845 && vague_linkage_p (DECL_CONTEXT (decl
)));
1850 /* Determine whether or not we want to specifically import or export CTYPE,
1851 using various heuristics. */
1854 import_export_class (tree ctype
)
1856 /* -1 for imported, 1 for exported. */
1857 int import_export
= 0;
1859 /* It only makes sense to call this function at EOF. The reason is
1860 that this function looks at whether or not the first non-inline
1861 non-abstract virtual member function has been defined in this
1862 translation unit. But, we can't possibly know that until we've
1863 seen the entire translation unit. */
1864 gcc_assert (at_eof
);
1866 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
1869 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1870 we will have CLASSTYPE_INTERFACE_ONLY set but not
1871 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1872 heuristic because someone will supply a #pragma implementation
1873 elsewhere, and deducing it here would produce a conflict. */
1874 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
1877 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
1879 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
1881 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
1882 && !flag_implicit_templates
)
1883 /* For a template class, without -fimplicit-templates, check the
1884 repository. If the virtual table is assigned to this
1885 translation unit, then export the class; otherwise, import
1887 import_export
= repo_export_class_p (ctype
) ? 1 : -1;
1888 else if (TYPE_POLYMORPHIC_P (ctype
))
1890 /* The ABI specifies that the virtual table and associated
1891 information are emitted with the key method, if any. */
1892 tree method
= CLASSTYPE_KEY_METHOD (ctype
);
1893 /* If weak symbol support is not available, then we must be
1894 careful not to emit the vtable when the key function is
1895 inline. An inline function can be defined in multiple
1896 translation units. If we were to emit the vtable in each
1897 translation unit containing a definition, we would get
1898 multiple definition errors at link-time. */
1899 if (method
&& (flag_weak
|| ! DECL_DECLARED_INLINE_P (method
)))
1900 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
1903 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1904 a definition anywhere else. */
1905 if (MULTIPLE_SYMBOL_SPACES
&& import_export
== -1)
1908 /* Allow back ends the chance to overrule the decision. */
1909 if (targetm
.cxx
.import_export_class
)
1910 import_export
= targetm
.cxx
.import_export_class (ctype
, import_export
);
1914 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
1915 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
1919 /* Return true if VAR has already been provided to the back end; in that
1920 case VAR should not be modified further by the front end. */
1922 var_finalized_p (tree var
)
1924 return varpool_node::get_create (var
)->definition
;
1927 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1928 must be emitted in this translation unit. Mark it as such. */
1931 mark_needed (tree decl
)
1933 TREE_USED (decl
) = 1;
1934 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1936 /* Extern inline functions don't become needed when referenced.
1937 If we know a method will be emitted in other TU and no new
1938 functions can be marked reachable, just use the external
1940 struct cgraph_node
*node
= cgraph_node::get_create (decl
);
1941 node
->forced_by_abi
= true;
1943 /* #pragma interface and -frepo code can call mark_needed for
1944 maybe-in-charge 'tors; mark the clones as well. */
1946 FOR_EACH_CLONE (clone
, decl
)
1947 mark_needed (clone
);
1949 else if (TREE_CODE (decl
) == VAR_DECL
)
1951 varpool_node
*node
= varpool_node::get_create (decl
);
1952 /* C++ frontend use mark_decl_references to force COMDAT variables
1953 to be output that might appear dead otherwise. */
1954 node
->forced_by_abi
= true;
1958 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1959 returns true if a definition of this entity should be provided in
1960 this object file. Callers use this function to determine whether
1961 or not to let the back end know that a definition of DECL is
1962 available in this translation unit. */
1965 decl_needed_p (tree decl
)
1967 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
1968 /* This function should only be called at the end of the translation
1969 unit. We cannot be sure of whether or not something will be
1970 COMDAT until that point. */
1971 gcc_assert (at_eof
);
1973 /* All entities with external linkage that are not COMDAT should be
1974 emitted; they may be referred to from other object files. */
1975 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
1977 /* If this entity was used, let the back end see it; it will decide
1978 whether or not to emit it into the object file. */
1979 if (TREE_USED (decl
))
1981 /* Functions marked "dllexport" must be emitted so that they are
1982 visible to other DLLs. */
1983 if (flag_keep_inline_dllexport
1984 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))
1986 /* Virtual functions might be needed for devirtualization. */
1987 if (flag_devirtualize
1988 && TREE_CODE (decl
) == FUNCTION_DECL
1989 && DECL_VIRTUAL_P (decl
))
1991 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1992 reference to DECL might cause it to be emitted later. */
1996 /* If necessary, write out the vtables for the dynamic class CTYPE.
1997 Returns true if any vtables were emitted. */
2000 maybe_emit_vtables (tree ctype
)
2005 varpool_node
*current
= NULL
, *last
= NULL
;
2007 /* If the vtables for this class have already been emitted there is
2008 nothing more to do. */
2009 primary_vtbl
= CLASSTYPE_VTABLES (ctype
);
2010 if (var_finalized_p (primary_vtbl
))
2012 /* Ignore dummy vtables made by get_vtable_decl. */
2013 if (TREE_TYPE (primary_vtbl
) == void_type_node
)
2016 /* On some targets, we cannot determine the key method until the end
2017 of the translation unit -- which is when this function is
2019 if (!targetm
.cxx
.key_method_may_be_inline ())
2020 determine_key_method (ctype
);
2022 /* See if any of the vtables are needed. */
2023 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2025 import_export_decl (vtbl
);
2026 if (DECL_NOT_REALLY_EXTERN (vtbl
) && decl_needed_p (vtbl
))
2031 /* If the references to this class' vtables are optimized away,
2032 still emit the appropriate debugging information. See
2034 if (DECL_COMDAT (primary_vtbl
)
2035 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
2036 note_debug_info_needed (ctype
);
2040 /* The ABI requires that we emit all of the vtables if we emit any
2042 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2044 /* Mark entities references from the virtual table as used. */
2045 mark_vtable_entries (vtbl
);
2047 if (TREE_TYPE (DECL_INITIAL (vtbl
)) == 0)
2049 vec
<tree
, va_gc
> *cleanups
= NULL
;
2050 tree expr
= store_init_value (vtbl
, DECL_INITIAL (vtbl
), &cleanups
,
2053 /* It had better be all done at compile-time. */
2054 gcc_assert (!expr
&& !cleanups
);
2058 DECL_EXTERNAL (vtbl
) = 0;
2059 rest_of_decl_compilation (vtbl
, 1, 1);
2061 /* Because we're only doing syntax-checking, we'll never end up
2062 actually marking the variable as written. */
2063 if (flag_syntax_only
)
2064 TREE_ASM_WRITTEN (vtbl
) = 1;
2065 else if (DECL_ONE_ONLY (vtbl
))
2067 current
= varpool_node::get_create (vtbl
);
2069 current
->add_to_same_comdat_group (last
);
2074 /* Since we're writing out the vtable here, also write the debug
2076 note_debug_info_needed (ctype
);
2081 /* A special return value from type_visibility meaning internal
2084 enum { VISIBILITY_ANON
= VISIBILITY_INTERNAL
+1 };
2086 /* walk_tree helper function for type_visibility. */
2089 min_vis_r (tree
*tp
, int *walk_subtrees
, void *data
)
2091 int *vis_p
= (int *)data
;
2096 else if (OVERLOAD_TYPE_P (*tp
)
2097 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp
)))
2099 *vis_p
= VISIBILITY_ANON
;
2102 else if (CLASS_TYPE_P (*tp
)
2103 && CLASSTYPE_VISIBILITY (*tp
) > *vis_p
)
2104 *vis_p
= CLASSTYPE_VISIBILITY (*tp
);
2108 /* Returns the visibility of TYPE, which is the minimum visibility of its
2112 type_visibility (tree type
)
2114 int vis
= VISIBILITY_DEFAULT
;
2115 cp_walk_tree_without_duplicates (&type
, min_vis_r
, &vis
);
2119 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2120 specified (or if VISIBILITY is static). If TMPL is true, this
2121 constraint is for a template argument, and takes precedence
2122 over explicitly-specified visibility on the template. */
2125 constrain_visibility (tree decl
, int visibility
, bool tmpl
)
2127 if (visibility
== VISIBILITY_ANON
)
2129 /* extern "C" declarations aren't affected by the anonymous
2131 if (!DECL_EXTERN_C_P (decl
))
2133 TREE_PUBLIC (decl
) = 0;
2134 DECL_WEAK (decl
) = 0;
2135 DECL_COMMON (decl
) = 0;
2136 if (TREE_CODE (decl
) == FUNCTION_DECL
2137 || TREE_CODE (decl
) == VAR_DECL
)
2139 struct symtab_node
*snode
= symtab_node::get (decl
);
2142 snode
->set_comdat_group (NULL
);
2144 DECL_INTERFACE_KNOWN (decl
) = 1;
2145 if (DECL_LANG_SPECIFIC (decl
))
2146 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2149 else if (visibility
> DECL_VISIBILITY (decl
)
2150 && (tmpl
|| !DECL_VISIBILITY_SPECIFIED (decl
)))
2152 DECL_VISIBILITY (decl
) = (enum symbol_visibility
) visibility
;
2153 /* This visibility was not specified. */
2154 DECL_VISIBILITY_SPECIFIED (decl
) = false;
2158 /* Constrain the visibility of DECL based on the visibility of its template
2162 constrain_visibility_for_template (tree decl
, tree targs
)
2164 /* If this is a template instantiation, check the innermost
2165 template args for visibility constraints. The outer template
2166 args are covered by the class check. */
2167 tree args
= INNERMOST_TEMPLATE_ARGS (targs
);
2169 for (i
= TREE_VEC_LENGTH (args
); i
> 0; --i
)
2173 tree arg
= TREE_VEC_ELT (args
, i
-1);
2175 vis
= type_visibility (arg
);
2176 else if (TREE_TYPE (arg
) && POINTER_TYPE_P (TREE_TYPE (arg
)))
2179 if (TREE_CODE (arg
) == ADDR_EXPR
)
2180 arg
= TREE_OPERAND (arg
, 0);
2181 if (VAR_OR_FUNCTION_DECL_P (arg
))
2183 if (! TREE_PUBLIC (arg
))
2184 vis
= VISIBILITY_ANON
;
2186 vis
= DECL_VISIBILITY (arg
);
2190 constrain_visibility (decl
, vis
, true);
2194 /* Like c_determine_visibility, but with additional C++-specific
2197 Function-scope entities can rely on the function's visibility because
2198 it is set in start_preparsed_function.
2200 Class-scope entities cannot rely on the class's visibility until the end
2201 of the enclosing class definition.
2203 Note that because namespaces have multiple independent definitions,
2204 namespace visibility is handled elsewhere using the #pragma visibility
2205 machinery rather than by decorating the namespace declaration.
2207 The goal is for constraints from the type to give a diagnostic, and
2208 other constraints to be applied silently. */
2211 determine_visibility (tree decl
)
2213 tree class_type
= NULL_TREE
;
2215 bool orig_visibility_specified
;
2216 enum symbol_visibility orig_visibility
;
2218 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2220 /* Only relevant for names with external linkage. */
2221 if (!TREE_PUBLIC (decl
))
2224 /* Cloned constructors and destructors get the same visibility as
2225 the underlying function. That should be set up in
2226 maybe_clone_body. */
2227 gcc_assert (!DECL_CLONED_FUNCTION_P (decl
));
2229 orig_visibility_specified
= DECL_VISIBILITY_SPECIFIED (decl
);
2230 orig_visibility
= DECL_VISIBILITY (decl
);
2232 if (TREE_CODE (decl
) == TYPE_DECL
)
2234 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
2235 use_template
= CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
));
2236 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
2241 else if (DECL_LANG_SPECIFIC (decl
))
2242 use_template
= DECL_USE_TEMPLATE (decl
);
2246 /* If DECL is a member of a class, visibility specifiers on the
2247 class can influence the visibility of the DECL. */
2248 if (DECL_CLASS_SCOPE_P (decl
))
2249 class_type
= DECL_CONTEXT (decl
);
2252 /* Not a class member. */
2254 /* Virtual tables have DECL_CONTEXT set to their associated class,
2255 so they are automatically handled above. */
2256 gcc_assert (!VAR_P (decl
)
2257 || !DECL_VTABLE_OR_VTT_P (decl
));
2259 if (DECL_FUNCTION_SCOPE_P (decl
) && ! DECL_VISIBILITY_SPECIFIED (decl
))
2261 /* Local statics and classes get the visibility of their
2262 containing function by default, except that
2263 -fvisibility-inlines-hidden doesn't affect them. */
2264 tree fn
= DECL_CONTEXT (decl
);
2265 if (DECL_VISIBILITY_SPECIFIED (fn
))
2267 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2268 DECL_VISIBILITY_SPECIFIED (decl
) =
2269 DECL_VISIBILITY_SPECIFIED (fn
);
2273 if (DECL_CLASS_SCOPE_P (fn
))
2274 determine_visibility_from_class (decl
, DECL_CONTEXT (fn
));
2275 else if (determine_hidden_inline (fn
))
2277 DECL_VISIBILITY (decl
) = default_visibility
;
2278 DECL_VISIBILITY_SPECIFIED (decl
) =
2279 visibility_options
.inpragma
;
2283 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2284 DECL_VISIBILITY_SPECIFIED (decl
) =
2285 DECL_VISIBILITY_SPECIFIED (fn
);
2289 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2290 but have no TEMPLATE_INFO, so don't try to check it. */
2293 else if (VAR_P (decl
) && DECL_TINFO_P (decl
)
2294 && flag_visibility_ms_compat
)
2296 /* Under -fvisibility-ms-compat, types are visible by default,
2297 even though their contents aren't. */
2298 tree underlying_type
= TREE_TYPE (DECL_NAME (decl
));
2299 int underlying_vis
= type_visibility (underlying_type
);
2300 if (underlying_vis
== VISIBILITY_ANON
2301 || (CLASS_TYPE_P (underlying_type
)
2302 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type
)))
2303 constrain_visibility (decl
, underlying_vis
, false);
2305 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
2307 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2309 /* tinfo visibility is based on the type it's for. */
2310 constrain_visibility
2311 (decl
, type_visibility (TREE_TYPE (DECL_NAME (decl
))), false);
2313 /* Give the target a chance to override the visibility associated
2315 if (TREE_PUBLIC (decl
)
2316 && !DECL_REALLY_EXTERN (decl
)
2317 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl
)))
2318 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl
))))
2319 targetm
.cxx
.determine_class_data_visibility (decl
);
2321 else if (use_template
)
2322 /* Template instantiations and specializations get visibility based
2323 on their template unless they override it with an attribute. */;
2324 else if (! DECL_VISIBILITY_SPECIFIED (decl
))
2326 if (determine_hidden_inline (decl
))
2327 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2330 /* Set default visibility to whatever the user supplied with
2331 #pragma GCC visibility or a namespace visibility attribute. */
2332 DECL_VISIBILITY (decl
) = default_visibility
;
2333 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
2340 /* If the specialization doesn't specify visibility, use the
2341 visibility from the template. */
2342 tree tinfo
= (TREE_CODE (decl
) == TYPE_DECL
2343 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2344 : DECL_TEMPLATE_INFO (decl
));
2345 tree args
= TI_ARGS (tinfo
);
2346 tree attribs
= (TREE_CODE (decl
) == TYPE_DECL
2347 ? TYPE_ATTRIBUTES (TREE_TYPE (decl
))
2348 : DECL_ATTRIBUTES (decl
));
2350 if (args
!= error_mark_node
)
2352 tree pattern
= DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo
));
2354 if (!DECL_VISIBILITY_SPECIFIED (decl
))
2356 if (!DECL_VISIBILITY_SPECIFIED (pattern
)
2357 && determine_hidden_inline (decl
))
2358 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2361 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (pattern
);
2362 DECL_VISIBILITY_SPECIFIED (decl
)
2363 = DECL_VISIBILITY_SPECIFIED (pattern
);
2368 /* Template argument visibility outweighs #pragma or namespace
2369 visibility, but not an explicit attribute. */
2370 && !lookup_attribute ("visibility", attribs
))
2372 int depth
= TMPL_ARGS_DEPTH (args
);
2373 if (DECL_VISIBILITY_SPECIFIED (decl
))
2375 /* A class template member with explicit visibility
2376 overrides the class visibility, so we need to apply
2377 all the levels of template args directly. */
2379 for (i
= 1; i
<= depth
; ++i
)
2381 tree lev
= TMPL_ARGS_LEVEL (args
, i
);
2382 constrain_visibility_for_template (decl
, lev
);
2385 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
2386 /* Limit visibility based on its template arguments. */
2387 constrain_visibility_for_template (decl
, args
);
2393 determine_visibility_from_class (decl
, class_type
);
2395 if (decl_anon_ns_mem_p (decl
))
2396 /* Names in an anonymous namespace get internal linkage.
2397 This might change once we implement export. */
2398 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2399 else if (TREE_CODE (decl
) != TYPE_DECL
)
2401 /* Propagate anonymity from type to decl. */
2402 int tvis
= type_visibility (TREE_TYPE (decl
));
2403 if (tvis
== VISIBILITY_ANON
2404 || ! DECL_VISIBILITY_SPECIFIED (decl
))
2405 constrain_visibility (decl
, tvis
, false);
2407 else if (no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/true))
2408 /* DR 757: A type without linkage shall not be used as the type of a
2409 variable or function with linkage, unless
2410 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2411 o the variable or function is not used (3.2 [basic.def.odr]) or is
2412 defined in the same translation unit.
2414 Since non-extern "C" decls need to be defined in the same
2415 translation unit, we can make the type internal. */
2416 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2418 /* If visibility changed and DECL already has DECL_RTL, ensure
2419 symbol flags are updated. */
2420 if ((DECL_VISIBILITY (decl
) != orig_visibility
2421 || DECL_VISIBILITY_SPECIFIED (decl
) != orig_visibility_specified
)
2422 && ((VAR_P (decl
) && TREE_STATIC (decl
))
2423 || TREE_CODE (decl
) == FUNCTION_DECL
)
2424 && DECL_RTL_SET_P (decl
))
2425 make_decl_rtl (decl
);
2428 /* By default, static data members and function members receive
2429 the visibility of their containing class. */
2432 determine_visibility_from_class (tree decl
, tree class_type
)
2434 if (DECL_VISIBILITY_SPECIFIED (decl
))
2437 if (determine_hidden_inline (decl
))
2438 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2441 /* Default to the class visibility. */
2442 DECL_VISIBILITY (decl
) = CLASSTYPE_VISIBILITY (class_type
);
2443 DECL_VISIBILITY_SPECIFIED (decl
)
2444 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type
);
2447 /* Give the target a chance to override the visibility associated
2450 && (DECL_TINFO_P (decl
)
2451 || (DECL_VTABLE_OR_VTT_P (decl
)
2452 /* Construction virtual tables are not exported because
2453 they cannot be referred to from other object files;
2454 their name is not standardized by the ABI. */
2455 && !DECL_CONSTRUCTION_VTABLE_P (decl
)))
2456 && TREE_PUBLIC (decl
)
2457 && !DECL_REALLY_EXTERN (decl
)
2458 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type
))
2459 targetm
.cxx
.determine_class_data_visibility (decl
);
2462 /* Returns true iff DECL is an inline that should get hidden visibility
2463 because of -fvisibility-inlines-hidden. */
2466 determine_hidden_inline (tree decl
)
2468 return (visibility_options
.inlines_hidden
2469 /* Don't do this for inline templates; specializations might not be
2470 inline, and we don't want them to inherit the hidden
2471 visibility. We'll set it here for all inline instantiations. */
2472 && !processing_template_decl
2473 && TREE_CODE (decl
) == FUNCTION_DECL
2474 && DECL_DECLARED_INLINE_P (decl
)
2475 && (! DECL_LANG_SPECIFIC (decl
)
2476 || ! DECL_EXPLICIT_INSTANTIATION (decl
)));
2479 /* Constrain the visibility of a class TYPE based on the visibility of its
2480 field types. Warn if any fields require lesser visibility. */
2483 constrain_class_visibility (tree type
)
2489 int vis
= type_visibility (type
);
2491 if (vis
== VISIBILITY_ANON
2492 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type
)))
2495 /* Don't warn about visibility if the class has explicit visibility. */
2496 if (CLASSTYPE_VISIBILITY_SPECIFIED (type
))
2497 vis
= VISIBILITY_INTERNAL
;
2499 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
2500 if (TREE_CODE (t
) == FIELD_DECL
&& TREE_TYPE (t
) != error_mark_node
)
2502 tree ftype
= strip_pointer_or_array_types (TREE_TYPE (t
));
2503 int subvis
= type_visibility (ftype
);
2505 if (subvis
== VISIBILITY_ANON
)
2507 if (!in_main_input_context ())
2509 %qT has a field %qD whose type uses the anonymous namespace",
2512 else if (MAYBE_CLASS_TYPE_P (ftype
)
2513 && vis
< VISIBILITY_HIDDEN
2514 && subvis
>= VISIBILITY_HIDDEN
)
2515 warning (OPT_Wattributes
, "\
2516 %qT declared with greater visibility than the type of its field %qD",
2520 binfo
= TYPE_BINFO (type
);
2521 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
2523 int subvis
= type_visibility (TREE_TYPE (t
));
2525 if (subvis
== VISIBILITY_ANON
)
2527 if (!in_main_input_context())
2529 %qT has a base %qT whose type uses the anonymous namespace",
2530 type
, TREE_TYPE (t
));
2532 else if (vis
< VISIBILITY_HIDDEN
2533 && subvis
>= VISIBILITY_HIDDEN
)
2534 warning (OPT_Wattributes
, "\
2535 %qT declared with greater visibility than its base %qT",
2536 type
, TREE_TYPE (t
));
2540 /* Functions for adjusting the visibility of a tagged type and its nested
2541 types and declarations when it gets a name for linkage purposes from a
2544 static void bt_reset_linkage_1 (binding_entry
, void *);
2545 static void bt_reset_linkage_2 (binding_entry
, void *);
2547 /* First reset the visibility of all the types. */
2550 reset_type_linkage_1 (tree type
)
2552 set_linkage_according_to_type (type
, TYPE_MAIN_DECL (type
));
2553 if (CLASS_TYPE_P (type
))
2554 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2555 bt_reset_linkage_1
, NULL
);
2558 bt_reset_linkage_1 (binding_entry b
, void */
*data*/
)
2560 reset_type_linkage_1 (b
->type
);
2563 /* Then reset the visibility of any static data members or member
2564 functions that use those types. */
2567 reset_decl_linkage (tree decl
)
2569 if (TREE_PUBLIC (decl
))
2571 if (DECL_CLONED_FUNCTION_P (decl
))
2573 TREE_PUBLIC (decl
) = true;
2574 DECL_INTERFACE_KNOWN (decl
) = false;
2575 determine_visibility (decl
);
2576 tentative_decl_linkage (decl
);
2579 reset_type_linkage_2 (tree type
)
2581 if (CLASS_TYPE_P (type
))
2583 if (tree vt
= CLASSTYPE_VTABLES (type
))
2585 tree name
= mangle_vtbl_for_type (type
);
2586 DECL_NAME (vt
) = name
;
2587 SET_DECL_ASSEMBLER_NAME (vt
, name
);
2588 reset_decl_linkage (vt
);
2590 if (tree ti
= CLASSTYPE_TYPEINFO_VAR (type
))
2592 tree name
= mangle_typeinfo_for_type (type
);
2593 DECL_NAME (ti
) = name
;
2594 SET_DECL_ASSEMBLER_NAME (ti
, name
);
2595 TREE_TYPE (name
) = type
;
2596 reset_decl_linkage (ti
);
2598 for (tree m
= TYPE_FIELDS (type
); m
; m
= DECL_CHAIN (m
))
2599 if (TREE_CODE (m
) == VAR_DECL
)
2600 reset_decl_linkage (m
);
2601 for (tree m
= TYPE_METHODS (type
); m
; m
= DECL_CHAIN (m
))
2602 reset_decl_linkage (m
);
2603 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2604 bt_reset_linkage_2
, NULL
);
2608 bt_reset_linkage_2 (binding_entry b
, void */
*data*/
)
2610 reset_type_linkage_2 (b
->type
);
2613 reset_type_linkage (tree type
)
2615 reset_type_linkage_1 (type
);
2616 reset_type_linkage_2 (type
);
2619 /* Set up our initial idea of what the linkage of DECL should be. */
2622 tentative_decl_linkage (tree decl
)
2624 if (DECL_INTERFACE_KNOWN (decl
))
2625 /* We've already made a decision as to how this function will
2627 else if (vague_linkage_p (decl
))
2629 if (TREE_CODE (decl
) == FUNCTION_DECL
2630 && decl_defined_p (decl
))
2632 DECL_EXTERNAL (decl
) = 1;
2633 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2634 note_vague_linkage_fn (decl
);
2635 /* A non-template inline function with external linkage will
2636 always be COMDAT. As we must eventually determine the
2637 linkage of all functions, and as that causes writes to
2638 the data mapped in from the PCH file, it's advantageous
2639 to mark the functions at this point. */
2640 if (DECL_DECLARED_INLINE_P (decl
)
2641 && (!DECL_IMPLICIT_INSTANTIATION (decl
)
2642 || DECL_DEFAULTED_FN (decl
)))
2644 /* This function must have external linkage, as
2645 otherwise DECL_INTERFACE_KNOWN would have been
2647 gcc_assert (TREE_PUBLIC (decl
));
2648 comdat_linkage (decl
);
2649 DECL_INTERFACE_KNOWN (decl
) = 1;
2652 else if (TREE_CODE (decl
) == VAR_DECL
)
2653 maybe_commonize_var (decl
);
2657 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2658 for DECL has not already been determined, do so now by setting
2659 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2660 function is called entities with vague linkage whose definitions
2661 are available must have TREE_PUBLIC set.
2663 If this function decides to place DECL in COMDAT, it will set
2664 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2665 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2666 callers defer that decision until it is clear that DECL is actually
2670 import_export_decl (tree decl
)
2675 tree class_type
= NULL_TREE
;
2677 if (DECL_INTERFACE_KNOWN (decl
))
2680 /* We cannot determine what linkage to give to an entity with vague
2681 linkage until the end of the file. For example, a virtual table
2682 for a class will be defined if and only if the key method is
2683 defined in this translation unit. As a further example, consider
2684 that when compiling a translation unit that uses PCH file with
2685 "-frepo" it would be incorrect to make decisions about what
2686 entities to emit when building the PCH; those decisions must be
2687 delayed until the repository information has been processed. */
2688 gcc_assert (at_eof
);
2689 /* Object file linkage for explicit instantiations is handled in
2690 mark_decl_instantiated. For static variables in functions with
2691 vague linkage, maybe_commonize_var is used.
2693 Therefore, the only declarations that should be provided to this
2694 function are those with external linkage that are:
2696 * implicit instantiations of function templates
2700 * implicit instantiations of static data members of class
2707 Furthermore, all entities that reach this point must have a
2708 definition available in this translation unit.
2710 The following assertions check these conditions. */
2711 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2712 /* Any code that creates entities with TREE_PUBLIC cleared should
2713 also set DECL_INTERFACE_KNOWN. */
2714 gcc_assert (TREE_PUBLIC (decl
));
2715 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2716 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2717 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
2718 || DECL_DECLARED_INLINE_P (decl
));
2720 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2721 || DECL_VTABLE_OR_VTT_P (decl
)
2722 || DECL_TINFO_P (decl
));
2723 /* Check that a definition of DECL is available in this translation
2725 gcc_assert (!DECL_REALLY_EXTERN (decl
));
2727 /* Assume that DECL will not have COMDAT linkage. */
2729 /* Assume that DECL will not be imported into this translation
2733 /* See if the repository tells us whether or not to emit DECL in
2734 this translation unit. */
2735 emit_p
= repo_emit_p (decl
);
2738 else if (emit_p
== 1)
2740 /* The repository indicates that this entity should be defined
2741 here. Make sure the back end honors that request. */
2743 /* Output the definition as an ordinary strong definition. */
2744 DECL_EXTERNAL (decl
) = 0;
2745 DECL_INTERFACE_KNOWN (decl
) = 1;
2750 /* We have already decided what to do with this DECL; there is no
2751 need to check anything further. */
2753 else if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
2755 class_type
= DECL_CONTEXT (decl
);
2756 import_export_class (class_type
);
2757 if (TYPE_FOR_JAVA (class_type
))
2759 else if (CLASSTYPE_INTERFACE_KNOWN (class_type
)
2760 && CLASSTYPE_INTERFACE_ONLY (class_type
))
2762 else if ((!flag_weak
|| TARGET_WEAK_NOT_IN_ARCHIVE_TOC
)
2763 && !CLASSTYPE_USE_TEMPLATE (class_type
)
2764 && CLASSTYPE_KEY_METHOD (class_type
)
2765 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
)))
2766 /* The ABI requires that all virtual tables be emitted with
2767 COMDAT linkage. However, on systems where COMDAT symbols
2768 don't show up in the table of contents for a static
2769 archive, or on systems without weak symbols (where we
2770 approximate COMDAT linkage by using internal linkage), the
2771 linker will report errors about undefined symbols because
2772 it will not see the virtual table definition. Therefore,
2773 in the case that we know that the virtual table will be
2774 emitted in only one translation unit, we make the virtual
2775 table an ordinary definition with external linkage. */
2776 DECL_EXTERNAL (decl
) = 0;
2777 else if (CLASSTYPE_INTERFACE_KNOWN (class_type
))
2779 /* CLASS_TYPE is being exported from this translation unit,
2780 so DECL should be defined here. */
2781 if (!flag_weak
&& CLASSTYPE_EXPLICIT_INSTANTIATION (class_type
))
2782 /* If a class is declared in a header with the "extern
2783 template" extension, then it will not be instantiated,
2784 even in translation units that would normally require
2785 it. Often such classes are explicitly instantiated in
2786 one translation unit. Therefore, the explicit
2787 instantiation must be made visible to other translation
2789 DECL_EXTERNAL (decl
) = 0;
2792 /* The generic C++ ABI says that class data is always
2793 COMDAT, even if there is a key function. Some
2794 variants (e.g., the ARM EABI) says that class data
2795 only has COMDAT linkage if the class data might be
2796 emitted in more than one translation unit. When the
2797 key method can be inline and is inline, we still have
2798 to arrange for comdat even though
2799 class_data_always_comdat is false. */
2800 if (!CLASSTYPE_KEY_METHOD (class_type
)
2801 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
))
2802 || targetm
.cxx
.class_data_always_comdat ())
2804 /* The ABI requires COMDAT linkage. Normally, we
2805 only emit COMDAT things when they are needed;
2806 make sure that we realize that this entity is
2813 else if (!flag_implicit_templates
2814 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type
))
2819 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2821 tree type
= TREE_TYPE (DECL_NAME (decl
));
2822 if (CLASS_TYPE_P (type
))
2825 import_export_class (type
);
2826 if (CLASSTYPE_INTERFACE_KNOWN (type
)
2827 && TYPE_POLYMORPHIC_P (type
)
2828 && CLASSTYPE_INTERFACE_ONLY (type
)
2829 /* If -fno-rtti was specified, then we cannot be sure
2830 that RTTI information will be emitted with the
2831 virtual table of the class, so we must emit it
2832 wherever it is used. */
2837 if (CLASSTYPE_INTERFACE_KNOWN (type
)
2838 && !CLASSTYPE_INTERFACE_ONLY (type
))
2840 comdat_p
= (targetm
.cxx
.class_data_always_comdat ()
2841 || (CLASSTYPE_KEY_METHOD (type
)
2842 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type
))));
2847 DECL_EXTERNAL (decl
) = 0;
2857 else if (DECL_TEMPLOID_INSTANTIATION (decl
))
2859 /* DECL is an implicit instantiation of a function or static
2861 if ((flag_implicit_templates
2862 && !flag_use_repository
)
2863 || (flag_implicit_inline_templates
2864 && TREE_CODE (decl
) == FUNCTION_DECL
2865 && DECL_DECLARED_INLINE_P (decl
)))
2868 /* If we are not implicitly generating templates, then mark
2869 this entity as undefined in this translation unit. */
2872 else if (DECL_FUNCTION_MEMBER_P (decl
))
2874 if (!DECL_DECLARED_INLINE_P (decl
))
2876 tree ctype
= DECL_CONTEXT (decl
);
2877 import_export_class (ctype
);
2878 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2880 DECL_NOT_REALLY_EXTERN (decl
)
2881 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
2882 || (DECL_DECLARED_INLINE_P (decl
)
2883 && ! flag_implement_inlines
2884 && !DECL_VINDEX (decl
)));
2886 if (!DECL_NOT_REALLY_EXTERN (decl
))
2887 DECL_EXTERNAL (decl
) = 1;
2889 /* Always make artificials weak. */
2890 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
2893 maybe_make_one_only (decl
);
2904 /* If we are importing DECL into this translation unit, mark is
2905 an undefined here. */
2906 DECL_EXTERNAL (decl
) = 1;
2907 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2911 /* If we decided to put DECL in COMDAT, mark it accordingly at
2913 comdat_linkage (decl
);
2916 DECL_INTERFACE_KNOWN (decl
) = 1;
2919 /* Return an expression that performs the destruction of DECL, which
2920 must be a VAR_DECL whose type has a non-trivial destructor, or is
2921 an array whose (innermost) elements have a non-trivial destructor. */
2924 build_cleanup (tree decl
)
2926 tree clean
= cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
2927 gcc_assert (clean
!= NULL_TREE
);
2931 /* Returns the initialization guard variable for the variable DECL,
2932 which has static storage duration. */
2935 get_guard (tree decl
)
2940 sname
= mangle_guard_variable (decl
);
2941 guard
= IDENTIFIER_GLOBAL_VALUE (sname
);
2946 /* We use a type that is big enough to contain a mutex as well
2947 as an integer counter. */
2948 guard_type
= targetm
.cxx
.guard_type ();
2949 guard
= build_decl (DECL_SOURCE_LOCATION (decl
),
2950 VAR_DECL
, sname
, guard_type
);
2952 /* The guard should have the same linkage as what it guards. */
2953 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
2954 TREE_STATIC (guard
) = TREE_STATIC (decl
);
2955 DECL_COMMON (guard
) = DECL_COMMON (decl
);
2956 DECL_COMDAT (guard
) = DECL_COMDAT (decl
);
2957 set_decl_tls_model (guard
, DECL_TLS_MODEL (decl
));
2958 if (DECL_ONE_ONLY (decl
))
2959 make_decl_one_only (guard
, cxx_comdat_group (guard
));
2960 if (TREE_PUBLIC (decl
))
2961 DECL_WEAK (guard
) = DECL_WEAK (decl
);
2962 DECL_VISIBILITY (guard
) = DECL_VISIBILITY (decl
);
2963 DECL_VISIBILITY_SPECIFIED (guard
) = DECL_VISIBILITY_SPECIFIED (decl
);
2965 DECL_ARTIFICIAL (guard
) = 1;
2966 DECL_IGNORED_P (guard
) = 1;
2967 TREE_USED (guard
) = 1;
2968 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
2973 /* Return those bits of the GUARD variable that should be set when the
2974 guarded entity is actually initialized. */
2977 get_guard_bits (tree guard
)
2979 if (!targetm
.cxx
.guard_mask_bit ())
2981 /* We only set the first byte of the guard, in order to leave room
2982 for a mutex in the high-order bits. */
2983 guard
= build1 (ADDR_EXPR
,
2984 build_pointer_type (TREE_TYPE (guard
)),
2986 guard
= build1 (NOP_EXPR
,
2987 build_pointer_type (char_type_node
),
2989 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
2995 /* Return an expression which determines whether or not the GUARD
2996 variable has already been initialized. */
2999 get_guard_cond (tree guard
)
3003 /* Check to see if the GUARD is zero. */
3004 guard
= get_guard_bits (guard
);
3006 /* Mask off all but the low bit. */
3007 if (targetm
.cxx
.guard_mask_bit ())
3009 guard_value
= integer_one_node
;
3010 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3011 guard_value
= convert (TREE_TYPE (guard
), guard_value
);
3012 guard
= cp_build_binary_op (input_location
,
3013 BIT_AND_EXPR
, guard
, guard_value
,
3014 tf_warning_or_error
);
3017 guard_value
= integer_zero_node
;
3018 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3019 guard_value
= convert (TREE_TYPE (guard
), guard_value
);
3020 return cp_build_binary_op (input_location
,
3021 EQ_EXPR
, guard
, guard_value
,
3022 tf_warning_or_error
);
3025 /* Return an expression which sets the GUARD variable, indicating that
3026 the variable being guarded has been initialized. */
3029 set_guard (tree guard
)
3033 /* Set the GUARD to one. */
3034 guard
= get_guard_bits (guard
);
3035 guard_init
= integer_one_node
;
3036 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
3037 guard_init
= convert (TREE_TYPE (guard
), guard_init
);
3038 return cp_build_modify_expr (guard
, NOP_EXPR
, guard_init
,
3039 tf_warning_or_error
);
3042 /* Returns true iff we can tell that VAR does not have a dynamic
3046 var_defined_without_dynamic_init (tree var
)
3048 /* If it's defined in another TU, we can't tell. */
3049 if (DECL_EXTERNAL (var
))
3051 /* If it has a non-trivial destructor, registering the destructor
3052 counts as dynamic initialization. */
3053 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var
)))
3055 /* If it's in this TU, its initializer has been processed. */
3056 gcc_assert (DECL_INITIALIZED_P (var
));
3057 /* If it has no initializer or a constant one, it's not dynamic. */
3058 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var
)
3059 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var
));
3062 /* Returns true iff VAR is a variable that needs uses to be
3063 wrapped for possible dynamic initialization. */
3066 var_needs_tls_wrapper (tree var
)
3068 return (!error_operand_p (var
)
3069 && DECL_THREAD_LOCAL_P (var
)
3070 && !DECL_GNU_TLS_P (var
)
3071 && !DECL_FUNCTION_SCOPE_P (var
)
3072 && !var_defined_without_dynamic_init (var
));
3075 /* Get the FUNCTION_DECL for the shared TLS init function for this
3076 translation unit. */
3079 get_local_tls_init_fn (void)
3081 tree sname
= get_identifier ("__tls_init");
3082 tree fn
= IDENTIFIER_GLOBAL_VALUE (sname
);
3085 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
3086 build_function_type (void_type_node
,
3088 SET_DECL_LANGUAGE (fn
, lang_c
);
3089 TREE_PUBLIC (fn
) = false;
3090 DECL_ARTIFICIAL (fn
) = true;
3092 SET_IDENTIFIER_GLOBAL_VALUE (sname
, fn
);
3097 /* Get a FUNCTION_DECL for the init function for the thread_local
3098 variable VAR. The init function will be an alias to the function
3099 that initializes all the non-local TLS variables in the translation
3100 unit. The init function is only used by the wrapper function. */
3103 get_tls_init_fn (tree var
)
3105 /* Only C++11 TLS vars need this init fn. */
3106 if (!var_needs_tls_wrapper (var
))
3109 /* If -fno-extern-tls-init, assume that we don't need to call
3110 a tls init function for a variable defined in another TU. */
3111 if (!flag_extern_tls_init
&& DECL_EXTERNAL (var
))
3114 #ifdef ASM_OUTPUT_DEF
3115 /* If the variable is internal, or if we can't generate aliases,
3116 call the local init function directly. */
3117 if (!TREE_PUBLIC (var
))
3119 return get_local_tls_init_fn ();
3121 tree sname
= mangle_tls_init_fn (var
);
3122 tree fn
= IDENTIFIER_GLOBAL_VALUE (sname
);
3125 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
3126 build_function_type (void_type_node
,
3128 SET_DECL_LANGUAGE (fn
, lang_c
);
3129 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3130 DECL_ARTIFICIAL (fn
) = true;
3131 DECL_COMDAT (fn
) = DECL_COMDAT (var
);
3132 DECL_EXTERNAL (fn
) = DECL_EXTERNAL (var
);
3133 if (DECL_ONE_ONLY (var
))
3134 make_decl_one_only (fn
, cxx_comdat_group (fn
));
3135 if (TREE_PUBLIC (var
))
3137 tree obtype
= strip_array_types (non_reference (TREE_TYPE (var
)));
3138 /* If the variable is defined somewhere else and might have static
3139 initialization, make the init function a weak reference. */
3140 if ((!TYPE_NEEDS_CONSTRUCTING (obtype
)
3141 || TYPE_HAS_CONSTEXPR_CTOR (obtype
))
3142 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype
)
3143 && DECL_EXTERNAL (var
))
3146 DECL_WEAK (fn
) = DECL_WEAK (var
);
3148 DECL_VISIBILITY (fn
) = DECL_VISIBILITY (var
);
3149 DECL_VISIBILITY_SPECIFIED (fn
) = DECL_VISIBILITY_SPECIFIED (var
);
3150 DECL_DLLIMPORT_P (fn
) = DECL_DLLIMPORT_P (var
);
3151 DECL_IGNORED_P (fn
) = 1;
3154 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3156 SET_IDENTIFIER_GLOBAL_VALUE (sname
, fn
);
3161 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3162 variable VAR. The wrapper function calls the init function (if any) for
3163 VAR and then returns a reference to VAR. The wrapper function is used
3164 in place of VAR everywhere VAR is mentioned. */
3167 get_tls_wrapper_fn (tree var
)
3169 /* Only C++11 TLS vars need this wrapper fn. */
3170 if (!var_needs_tls_wrapper (var
))
3173 tree sname
= mangle_tls_wrapper_fn (var
);
3174 tree fn
= IDENTIFIER_GLOBAL_VALUE (sname
);
3177 /* A named rvalue reference is an lvalue, so the wrapper should
3178 always return an lvalue reference. */
3179 tree type
= non_reference (TREE_TYPE (var
));
3180 type
= build_reference_type (type
);
3181 tree fntype
= build_function_type (type
, void_list_node
);
3182 fn
= build_lang_decl (FUNCTION_DECL
, sname
, fntype
);
3183 SET_DECL_LANGUAGE (fn
, lang_c
);
3184 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3185 DECL_ARTIFICIAL (fn
) = true;
3186 DECL_IGNORED_P (fn
) = 1;
3187 /* The wrapper is inline and emitted everywhere var is used. */
3188 DECL_DECLARED_INLINE_P (fn
) = true;
3189 if (TREE_PUBLIC (var
))
3191 comdat_linkage (fn
);
3192 #ifdef HAVE_GAS_HIDDEN
3193 /* Make the wrapper bind locally; there's no reason to share
3194 the wrapper between multiple shared objects. */
3195 DECL_VISIBILITY (fn
) = VISIBILITY_INTERNAL
;
3196 DECL_VISIBILITY_SPECIFIED (fn
) = true;
3199 if (!TREE_PUBLIC (fn
))
3200 DECL_INTERFACE_KNOWN (fn
) = true;
3202 note_vague_linkage_fn (fn
);
3205 /* We want CSE to commonize calls to the wrapper, but marking it as
3206 pure is unsafe since it has side-effects. I guess we need a new
3207 ECF flag even weaker than ECF_PURE. FIXME! */
3208 DECL_PURE_P (fn
) = true;
3211 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3213 SET_IDENTIFIER_GLOBAL_VALUE (sname
, fn
);
3218 /* At EOF, generate the definition for the TLS wrapper function FN:
3221 if (init_fn) init_fn();
3226 generate_tls_wrapper (tree fn
)
3228 tree var
= DECL_BEFRIENDING_CLASSES (fn
);
3230 start_preparsed_function (fn
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
3231 tree body
= begin_function_body ();
3232 /* Only call the init fn if there might be one. */
3233 if (tree init_fn
= get_tls_init_fn (var
))
3235 tree if_stmt
= NULL_TREE
;
3236 /* If init_fn is a weakref, make sure it exists before calling. */
3237 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn
)))
3239 if_stmt
= begin_if_stmt ();
3240 tree addr
= cp_build_addr_expr (init_fn
, tf_warning_or_error
);
3241 tree cond
= cp_build_binary_op (DECL_SOURCE_LOCATION (var
),
3242 NE_EXPR
, addr
, nullptr_node
,
3243 tf_warning_or_error
);
3244 finish_if_stmt_cond (cond
, if_stmt
);
3246 finish_expr_stmt (build_cxx_call
3247 (init_fn
, 0, NULL
, tf_warning_or_error
));
3250 finish_then_clause (if_stmt
);
3251 finish_if_stmt (if_stmt
);
3255 /* If there's no initialization, the wrapper is a constant function. */
3256 TREE_READONLY (fn
) = true;
3257 finish_return_stmt (convert_from_reference (var
));
3258 finish_function_body (body
);
3259 expand_or_defer_fn (finish_function (0));
3262 /* Start the process of running a particular set of global constructors
3263 or destructors. Subroutine of do_[cd]tors. Also called from
3264 vtv_start_verification_constructor_init_function. */
3267 start_objects (int method_type
, int initp
)
3273 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3275 if (initp
!= DEFAULT_INIT_PRIORITY
)
3285 sprintf (type
, "sub_%c%c%.5u", method_type
, joiner
, initp
);
3288 sprintf (type
, "sub_%c", method_type
);
3290 fndecl
= build_lang_decl (FUNCTION_DECL
,
3291 get_file_function_name (type
),
3292 build_function_type_list (void_type_node
,
3294 start_preparsed_function (fndecl
, /*attrs=*/NULL_TREE
, SF_PRE_PARSED
);
3296 TREE_PUBLIC (current_function_decl
) = 0;
3298 /* Mark as artificial because it's not explicitly in the user's
3300 DECL_ARTIFICIAL (current_function_decl
) = 1;
3302 /* Mark this declaration as used to avoid spurious warnings. */
3303 TREE_USED (current_function_decl
) = 1;
3305 /* Mark this function as a global constructor or destructor. */
3306 if (method_type
== 'I')
3307 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
3309 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
3311 body
= begin_compound_stmt (BCS_FN_BODY
);
3316 /* Finish the process of running a particular set of global constructors
3317 or destructors. Subroutine of do_[cd]tors. */
3320 finish_objects (int method_type
, int initp
, tree body
)
3325 finish_compound_stmt (body
);
3326 fn
= finish_function (0);
3328 if (method_type
== 'I')
3330 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
3331 decl_init_priority_insert (fn
, initp
);
3335 DECL_STATIC_DESTRUCTOR (fn
) = 1;
3336 decl_fini_priority_insert (fn
, initp
);
3339 expand_or_defer_fn (fn
);
3342 /* The names of the parameters to the function created to handle
3343 initializations and destructions for objects with static storage
3345 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3346 #define PRIORITY_IDENTIFIER "__priority"
3348 /* The name of the function we create to handle initializations and
3349 destructions for objects with static storage duration. */
3350 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3352 /* The declaration for the __INITIALIZE_P argument. */
3353 static GTY(()) tree initialize_p_decl
;
3355 /* The declaration for the __PRIORITY argument. */
3356 static GTY(()) tree priority_decl
;
3358 /* The declaration for the static storage duration function. */
3359 static GTY(()) tree ssdf_decl
;
3361 /* All the static storage duration functions created in this
3362 translation unit. */
3363 static GTY(()) vec
<tree
, va_gc
> *ssdf_decls
;
3365 /* A map from priority levels to information about that priority
3366 level. There may be many such levels, so efficient lookup is
3368 static splay_tree priority_info_map
;
3370 /* Begins the generation of the function that will handle all
3371 initialization and destruction of objects with static storage
3372 duration. The function generated takes two parameters of type
3373 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3374 nonzero, it performs initializations. Otherwise, it performs
3375 destructions. It only performs those initializations or
3376 destructions with the indicated __PRIORITY. The generated function
3379 It is assumed that this function will only be called once per
3380 translation unit. */
3383 start_static_storage_duration_function (unsigned count
)
3387 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
3389 /* Create the identifier for this function. It will be of the form
3390 SSDF_IDENTIFIER_<number>. */
3391 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, count
);
3393 type
= build_function_type_list (void_type_node
,
3394 integer_type_node
, integer_type_node
,
3397 /* Create the FUNCTION_DECL itself. */
3398 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
3399 get_identifier (id
),
3401 TREE_PUBLIC (ssdf_decl
) = 0;
3402 DECL_ARTIFICIAL (ssdf_decl
) = 1;
3404 /* Put this function in the list of functions to be called from the
3405 static constructors and destructors. */
3408 vec_alloc (ssdf_decls
, 32);
3410 /* Take this opportunity to initialize the map from priority
3411 numbers to information about that priority level. */
3412 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
3413 /*delete_key_fn=*/0,
3414 /*delete_value_fn=*/
3415 (splay_tree_delete_value_fn
) &free
);
3417 /* We always need to generate functions for the
3418 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3419 priorities later, we'll be sure to find the
3420 DEFAULT_INIT_PRIORITY. */
3421 get_priority_info (DEFAULT_INIT_PRIORITY
);
3424 vec_safe_push (ssdf_decls
, ssdf_decl
);
3426 /* Create the argument list. */
3427 initialize_p_decl
= cp_build_parm_decl
3428 (get_identifier (INITIALIZE_P_IDENTIFIER
), integer_type_node
);
3429 DECL_CONTEXT (initialize_p_decl
) = ssdf_decl
;
3430 TREE_USED (initialize_p_decl
) = 1;
3431 priority_decl
= cp_build_parm_decl
3432 (get_identifier (PRIORITY_IDENTIFIER
), integer_type_node
);
3433 DECL_CONTEXT (priority_decl
) = ssdf_decl
;
3434 TREE_USED (priority_decl
) = 1;
3436 DECL_CHAIN (initialize_p_decl
) = priority_decl
;
3437 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
3439 /* Put the function in the global scope. */
3440 pushdecl (ssdf_decl
);
3442 /* Start the function itself. This is equivalent to declaring the
3445 static void __ssdf (int __initialize_p, init __priority_p);
3447 It is static because we only need to call this function from the
3448 various constructor and destructor functions for this module. */
3449 start_preparsed_function (ssdf_decl
,
3450 /*attrs=*/NULL_TREE
,
3453 /* Set up the scope of the outermost block in the function. */
3454 body
= begin_compound_stmt (BCS_FN_BODY
);
3459 /* Finish the generation of the function which performs initialization
3460 and destruction of objects with static storage duration. After
3461 this point, no more such objects can be created. */
3464 finish_static_storage_duration_function (tree body
)
3466 /* Close out the function. */
3467 finish_compound_stmt (body
);
3468 expand_or_defer_fn (finish_function (0));
3471 /* Return the information about the indicated PRIORITY level. If no
3472 code to handle this level has yet been generated, generate the
3473 appropriate prologue. */
3475 static priority_info
3476 get_priority_info (int priority
)
3481 n
= splay_tree_lookup (priority_info_map
,
3482 (splay_tree_key
) priority
);
3485 /* Create a new priority information structure, and insert it
3487 pi
= XNEW (struct priority_info_s
);
3488 pi
->initializations_p
= 0;
3489 pi
->destructions_p
= 0;
3490 splay_tree_insert (priority_info_map
,
3491 (splay_tree_key
) priority
,
3492 (splay_tree_value
) pi
);
3495 pi
= (priority_info
) n
->value
;
3500 /* The effective initialization priority of a DECL. */
3502 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3503 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3504 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3506 /* Whether a DECL needs a guard to protect it against multiple
3509 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3510 || DECL_ONE_ONLY (decl) \
3511 || DECL_WEAK (decl)))
3513 /* Called from one_static_initialization_or_destruction(),
3515 Walks the initializer list of a global variable and looks for
3516 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3517 and that have their DECL_CONTEXT() == NULL.
3518 For each such temporary variable, set their DECL_CONTEXT() to
3519 the current function. This is necessary because otherwise
3520 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3521 when trying to refer to a temporary variable that does not have
3522 it's DECL_CONTECT() properly set. */
3524 fix_temporary_vars_context_r (tree
*node
,
3528 gcc_assert (current_function_decl
);
3530 if (TREE_CODE (*node
) == BIND_EXPR
)
3534 for (var
= BIND_EXPR_VARS (*node
); var
; var
= DECL_CHAIN (var
))
3537 && DECL_ARTIFICIAL (var
)
3538 && !DECL_CONTEXT (var
))
3539 DECL_CONTEXT (var
) = current_function_decl
;
3545 /* Set up to handle the initialization or destruction of DECL. If
3546 INITP is nonzero, we are initializing the variable. Otherwise, we
3547 are destroying it. */
3550 one_static_initialization_or_destruction (tree decl
, tree init
, bool initp
)
3552 tree guard_if_stmt
= NULL_TREE
;
3555 /* If we are supposed to destruct and there's a trivial destructor,
3556 nothing has to be done. */
3558 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3561 /* Trick the compiler into thinking we are at the file and line
3562 where DECL was declared so that error-messages make sense, and so
3563 that the debugger will show somewhat sensible file and line
3565 input_location
= DECL_SOURCE_LOCATION (decl
);
3567 /* Make sure temporary variables in the initialiser all have
3568 their DECL_CONTEXT() set to a value different from NULL_TREE.
3569 This can happen when global variables initialisers are built.
3570 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3571 the temporary variables that might have been generated in the
3572 accompagning initialisers is NULL_TREE, meaning the variables have been
3573 declared in the global namespace.
3574 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3575 of the temporaries are set to the current function decl. */
3576 cp_walk_tree_without_duplicates (&init
,
3577 fix_temporary_vars_context_r
,
3584 Access control for implicit calls to the constructors,
3585 the conversion functions, or the destructor called to
3586 create and destroy a static data member is performed as
3587 if these calls appeared in the scope of the member's
3590 we pretend we are in a static member function of the class of
3591 which the DECL is a member. */
3592 if (member_p (decl
))
3594 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
3595 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
3598 /* Assume we don't need a guard. */
3600 /* We need a guard if this is an object with external linkage that
3601 might be initialized in more than one place. (For example, a
3602 static data member of a template, when the data member requires
3604 if (NEEDS_GUARD_P (decl
))
3608 guard
= get_guard (decl
);
3610 /* When using __cxa_atexit, we just check the GUARD as we would
3611 for a local static. */
3612 if (flag_use_cxa_atexit
)
3614 /* When using __cxa_atexit, we never try to destroy
3615 anything from a static destructor. */
3617 guard_cond
= get_guard_cond (guard
);
3619 /* If we don't have __cxa_atexit, then we will be running
3620 destructors from .fini sections, or their equivalents. So,
3621 we need to know how many times we've tried to initialize this
3622 object. We do initializations only if the GUARD is zero,
3623 i.e., if we are the first to initialize the variable. We do
3624 destructions only if the GUARD is one, i.e., if we are the
3625 last to destroy the variable. */
3628 = cp_build_binary_op (input_location
,
3630 cp_build_unary_op (PREINCREMENT_EXPR
,
3633 tf_warning_or_error
),
3635 tf_warning_or_error
);
3638 = cp_build_binary_op (input_location
,
3640 cp_build_unary_op (PREDECREMENT_EXPR
,
3643 tf_warning_or_error
),
3645 tf_warning_or_error
);
3647 guard_if_stmt
= begin_if_stmt ();
3648 finish_if_stmt_cond (guard_cond
, guard_if_stmt
);
3652 /* If we're using __cxa_atexit, we have not already set the GUARD,
3653 so we must do so now. */
3654 if (guard
&& initp
&& flag_use_cxa_atexit
)
3655 finish_expr_stmt (set_guard (guard
));
3657 /* Perform the initialization or destruction. */
3662 finish_expr_stmt (init
);
3663 if (flag_sanitize
& SANITIZE_ADDRESS
)
3665 varpool_node
*vnode
= varpool_node::get (decl
);
3667 vnode
->dynamically_initialized
= 1;
3671 /* If we're using __cxa_atexit, register a function that calls the
3672 destructor for the object. */
3673 if (flag_use_cxa_atexit
)
3674 finish_expr_stmt (register_dtor_fn (decl
));
3677 finish_expr_stmt (build_cleanup (decl
));
3679 /* Finish the guard if-stmt, if necessary. */
3682 finish_then_clause (guard_if_stmt
);
3683 finish_if_stmt (guard_if_stmt
);
3686 /* Now that we're done with DECL we don't need to pretend to be a
3687 member of its class any longer. */
3688 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
3689 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
3692 /* Generate code to do the initialization or destruction of the decls in VARS,
3693 a TREE_LIST of VAR_DECL with static storage duration.
3694 Whether initialization or destruction is performed is specified by INITP. */
3697 do_static_initialization_or_destruction (tree vars
, bool initp
)
3699 tree node
, init_if_stmt
, cond
;
3701 /* Build the outer if-stmt to check for initialization or destruction. */
3702 init_if_stmt
= begin_if_stmt ();
3703 cond
= initp
? integer_one_node
: integer_zero_node
;
3704 cond
= cp_build_binary_op (input_location
,
3708 tf_warning_or_error
);
3709 finish_if_stmt_cond (cond
, init_if_stmt
);
3711 /* To make sure dynamic construction doesn't access globals from other
3712 compilation units where they might not be yet constructed, for
3713 -fsanitize=address insert __asan_before_dynamic_init call that
3714 prevents access to either all global variables that need construction
3715 in other compilation units, or at least those that haven't been
3716 initialized yet. Variables that need dynamic construction in
3717 the current compilation unit are kept accessible. */
3718 if (flag_sanitize
& SANITIZE_ADDRESS
)
3719 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3723 tree decl
= TREE_VALUE (node
);
3724 tree priority_if_stmt
;
3728 /* If we don't need a destructor, there's nothing to do. Avoid
3729 creating a possibly empty if-stmt. */
3730 if (!initp
&& TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3732 node
= TREE_CHAIN (node
);
3736 /* Remember that we had an initialization or finalization at this
3738 priority
= DECL_EFFECTIVE_INIT_PRIORITY (decl
);
3739 pi
= get_priority_info (priority
);
3741 pi
->initializations_p
= 1;
3743 pi
->destructions_p
= 1;
3745 /* Conditionalize this initialization on being in the right priority
3746 and being initializing/finalizing appropriately. */
3747 priority_if_stmt
= begin_if_stmt ();
3748 cond
= cp_build_binary_op (input_location
,
3751 build_int_cst (NULL_TREE
, priority
),
3752 tf_warning_or_error
);
3753 finish_if_stmt_cond (cond
, priority_if_stmt
);
3755 /* Process initializers with same priority. */
3757 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node
)) == priority
;
3758 node
= TREE_CHAIN (node
))
3759 /* Do one initialization or destruction. */
3760 one_static_initialization_or_destruction (TREE_VALUE (node
),
3761 TREE_PURPOSE (node
), initp
);
3763 /* Finish up the priority if-stmt body. */
3764 finish_then_clause (priority_if_stmt
);
3765 finish_if_stmt (priority_if_stmt
);
3769 /* Revert what __asan_before_dynamic_init did by calling
3770 __asan_after_dynamic_init. */
3771 if (flag_sanitize
& SANITIZE_ADDRESS
)
3772 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3774 /* Finish up the init/destruct if-stmt body. */
3775 finish_then_clause (init_if_stmt
);
3776 finish_if_stmt (init_if_stmt
);
3779 /* VARS is a list of variables with static storage duration which may
3780 need initialization and/or finalization. Remove those variables
3781 that don't really need to be initialized or finalized, and return
3782 the resulting list. The order in which the variables appear in
3783 VARS is in reverse order of the order in which they should actually
3784 be initialized. The list we return is in the unreversed order;
3785 i.e., the first variable should be initialized first. */
3788 prune_vars_needing_no_initialization (tree
*vars
)
3791 tree result
= NULL_TREE
;
3796 tree decl
= TREE_VALUE (t
);
3797 tree init
= TREE_PURPOSE (t
);
3799 /* Deal gracefully with error. */
3800 if (decl
== error_mark_node
)
3802 var
= &TREE_CHAIN (t
);
3806 /* The only things that can be initialized are variables. */
3807 gcc_assert (VAR_P (decl
));
3809 /* If this object is not defined, we don't need to do anything
3811 if (DECL_EXTERNAL (decl
))
3813 var
= &TREE_CHAIN (t
);
3817 /* Also, if the initializer already contains errors, we can bail
3819 if (init
&& TREE_CODE (init
) == TREE_LIST
3820 && value_member (error_mark_node
, init
))
3822 var
= &TREE_CHAIN (t
);
3826 /* This variable is going to need initialization and/or
3827 finalization, so we add it to the list. */
3828 *var
= TREE_CHAIN (t
);
3829 TREE_CHAIN (t
) = result
;
3836 /* Make sure we have told the back end about all the variables in
3840 write_out_vars (tree vars
)
3844 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3846 tree var
= TREE_VALUE (v
);
3847 if (!var_finalized_p (var
))
3849 import_export_decl (var
);
3850 rest_of_decl_compilation (var
, 1, 1);
3855 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3856 (otherwise) that will initialize all global objects with static
3857 storage duration having the indicated PRIORITY. */
3860 generate_ctor_or_dtor_function (bool constructor_p
, int priority
,
3868 input_location
= *locus
;
3870 /* Was: locus->line++; */
3872 /* We use `I' to indicate initialization and `D' to indicate
3874 function_key
= constructor_p
? 'I' : 'D';
3876 /* We emit the function lazily, to avoid generating empty
3877 global constructors and destructors. */
3880 /* For Objective-C++, we may need to initialize metadata found in this module.
3881 This must be done _before_ any other static initializations. */
3882 if (c_dialect_objc () && (priority
== DEFAULT_INIT_PRIORITY
)
3883 && constructor_p
&& objc_static_init_needed_p ())
3885 body
= start_objects (function_key
, priority
);
3886 objc_generate_static_init_call (NULL_TREE
);
3889 /* Call the static storage duration function with appropriate
3891 FOR_EACH_VEC_SAFE_ELT (ssdf_decls
, i
, fndecl
)
3893 /* Calls to pure or const functions will expand to nothing. */
3894 if (! (flags_from_decl_or_type (fndecl
) & (ECF_CONST
| ECF_PURE
)))
3899 body
= start_objects (function_key
, priority
);
3901 call
= cp_build_function_call_nary (fndecl
, tf_warning_or_error
,
3902 build_int_cst (NULL_TREE
,
3904 build_int_cst (NULL_TREE
,
3907 finish_expr_stmt (call
);
3911 /* Close out the function. */
3913 finish_objects (function_key
, priority
, body
);
3916 /* Generate constructor and destructor functions for the priority
3920 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n
, void * data
)
3922 location_t
*locus
= (location_t
*) data
;
3923 int priority
= (int) n
->key
;
3924 priority_info pi
= (priority_info
) n
->value
;
3926 /* Generate the functions themselves, but only if they are really
3928 if (pi
->initializations_p
)
3929 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority
, locus
);
3930 if (pi
->destructions_p
)
3931 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority
, locus
);
3933 /* Keep iterating. */
3937 /* Java requires that we be able to reference a local address for a
3938 method, and not be confused by PLT entries. If hidden aliases are
3939 supported, collect and return all the functions for which we should
3940 emit a hidden alias. */
3942 static hash_set
<tree
> *
3943 collect_candidates_for_java_method_aliases (void)
3945 struct cgraph_node
*node
;
3946 hash_set
<tree
> *candidates
= NULL
;
3948 #ifndef HAVE_GAS_HIDDEN
3952 FOR_EACH_FUNCTION (node
)
3954 tree fndecl
= node
->decl
;
3956 if (DECL_CLASS_SCOPE_P (fndecl
)
3957 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl
))
3958 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl
))
3960 if (candidates
== NULL
)
3961 candidates
= new hash_set
<tree
>;
3962 candidates
->add (fndecl
);
3970 /* Java requires that we be able to reference a local address for a
3971 method, and not be confused by PLT entries. If hidden aliases are
3972 supported, emit one for each java function that we've emitted.
3973 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3974 by collect_candidates_for_java_method_aliases. */
3977 build_java_method_aliases (hash_set
<tree
> *candidates
)
3979 struct cgraph_node
*node
;
3981 #ifndef HAVE_GAS_HIDDEN
3985 FOR_EACH_FUNCTION (node
)
3987 tree fndecl
= node
->decl
;
3989 if (TREE_ASM_WRITTEN (fndecl
)
3990 && candidates
->contains (fndecl
))
3992 /* Mangle the name in a predictable way; we need to reference
3993 this from a java compiled object file. */
3994 tree oid
, nid
, alias
;
3998 oid
= DECL_ASSEMBLER_NAME (fndecl
);
3999 oname
= IDENTIFIER_POINTER (oid
);
4000 gcc_assert (oname
[0] == '_' && oname
[1] == 'Z');
4001 nname
= ACONCAT (("_ZGA", oname
+2, NULL
));
4002 nid
= get_identifier (nname
);
4004 alias
= make_alias_for (fndecl
, nid
);
4005 TREE_PUBLIC (alias
) = 1;
4006 DECL_VISIBILITY (alias
) = VISIBILITY_HIDDEN
;
4008 assemble_alias (alias
, oid
);
4013 /* Return C++ property of T, based on given operation OP. */
4016 cpp_check (tree t
, cpp_operation op
)
4021 return DECL_PURE_VIRTUAL_P (t
);
4022 case IS_CONSTRUCTOR
:
4023 return DECL_CONSTRUCTOR_P (t
);
4025 return DECL_DESTRUCTOR_P (t
);
4026 case IS_COPY_CONSTRUCTOR
:
4027 return DECL_COPY_CONSTRUCTOR_P (t
);
4029 return TREE_CODE (t
) == TEMPLATE_DECL
;
4031 return trivial_type_p (t
);
4037 /* Collect source file references recursively, starting from NAMESPC. */
4040 collect_source_refs (tree namespc
)
4047 /* Iterate over names in this name space. */
4048 for (t
= NAMESPACE_LEVEL (namespc
)->names
; t
; t
= TREE_CHAIN (t
))
4049 if (!DECL_IS_BUILTIN (t
) )
4050 collect_source_ref (DECL_SOURCE_FILE (t
));
4052 /* Dump siblings, if any */
4053 collect_source_refs (TREE_CHAIN (namespc
));
4055 /* Dump children, if any */
4056 collect_source_refs (NAMESPACE_LEVEL (namespc
)->namespaces
);
4059 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4060 starting from NAMESPC. */
4063 collect_ada_namespace (tree namespc
, const char *source_file
)
4068 /* Collect decls from this namespace */
4069 collect_ada_nodes (NAMESPACE_LEVEL (namespc
)->names
, source_file
);
4071 /* Collect siblings, if any */
4072 collect_ada_namespace (TREE_CHAIN (namespc
), source_file
);
4074 /* Collect children, if any */
4075 collect_ada_namespace (NAMESPACE_LEVEL (namespc
)->namespaces
, source_file
);
4078 /* Returns true iff there is a definition available for variable or
4082 decl_defined_p (tree decl
)
4084 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4085 return (DECL_INITIAL (decl
) != NULL_TREE
);
4088 gcc_assert (VAR_P (decl
));
4089 return !DECL_EXTERNAL (decl
);
4093 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4097 An integral constant-expression can only involve ... const
4098 variables of integral or enumeration types initialized with
4099 constant expressions ...
4101 C++0x also allows constexpr variables and temporaries initialized
4102 with constant expressions. We handle the former here, but the latter
4103 are just folded away in cxx_eval_constant_expression.
4105 The standard does not require that the expression be non-volatile.
4106 G++ implements the proposed correction in DR 457. */
4109 decl_constant_var_p (tree decl
)
4111 if (!decl_maybe_constant_var_p (decl
))
4114 /* We don't know if a template static data member is initialized with
4115 a constant expression until we instantiate its initializer. Even
4116 in the case of a constexpr variable, we can't treat it as a
4117 constant until its initializer is complete in case it's used in
4118 its own initializer. */
4120 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
);
4123 /* Returns true if DECL could be a symbolic constant variable, depending on
4127 decl_maybe_constant_var_p (tree decl
)
4129 tree type
= TREE_TYPE (decl
);
4132 if (DECL_DECLARED_CONSTEXPR_P (decl
))
4134 return (CP_TYPE_CONST_NON_VOLATILE_P (type
)
4135 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
4138 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4139 called from grokfndecl and grokvardecl; in all modes it is called from
4140 cp_write_global_declarations. */
4143 no_linkage_error (tree decl
)
4145 if (cxx_dialect
>= cxx11
&& decl_defined_p (decl
))
4146 /* In C++11 it's ok if the decl is defined. */
4148 tree t
= no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false);
4150 /* The type that got us on no_linkage_decls must have gotten a name for
4151 linkage purposes. */;
4152 else if (CLASS_TYPE_P (t
) && TYPE_BEING_DEFINED (t
))
4153 /* The type might end up having a typedef name for linkage purposes. */
4154 vec_safe_push (no_linkage_decls
, decl
);
4155 else if (TYPE_ANONYMOUS_P (t
))
4158 if (cxx_dialect
>= cxx11
)
4159 d
= permerror (DECL_SOURCE_LOCATION (decl
), "%q#D, declared using "
4160 "anonymous type, is used but never defined", decl
);
4161 else if (DECL_EXTERN_C_P (decl
))
4162 /* Allow this; it's pretty common in C. */;
4163 else if (TREE_CODE (decl
) == VAR_DECL
)
4164 /* DRs 132, 319 and 389 seem to indicate types with
4165 no linkage can only be used to declare extern "C"
4166 entities. Since it's not always an error in the
4167 ISO C++ 90 Standard, we only issue a warning. */
4168 d
= warning_at (DECL_SOURCE_LOCATION (decl
), 0, "anonymous type "
4169 "with no linkage used to declare variable %q#D with "
4172 d
= permerror (DECL_SOURCE_LOCATION (decl
), "anonymous type with no "
4173 "linkage used to declare function %q#D with linkage",
4175 if (d
&& is_typedef_decl (TYPE_NAME (t
)))
4176 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)), "%q#D does not refer "
4177 "to the unqualified type, so it is not used for linkage",
4180 else if (cxx_dialect
>= cxx11
)
4181 permerror (DECL_SOURCE_LOCATION (decl
), "%q#D, declared using local type "
4182 "%qT, is used but never defined", decl
, t
);
4183 else if (TREE_CODE (decl
) == VAR_DECL
)
4184 warning_at (DECL_SOURCE_LOCATION (decl
), 0, "type %qT with no linkage "
4185 "used to declare variable %q#D with linkage", t
, decl
);
4187 permerror (DECL_SOURCE_LOCATION (decl
), "type %qT with no linkage used "
4188 "to declare function %q#D with linkage", t
, decl
);
4191 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4194 collect_all_refs (const char *source_file
)
4196 collect_ada_namespace (global_namespace
, source_file
);
4199 /* Clear DECL_EXTERNAL for NODE. */
4202 clear_decl_external (struct cgraph_node
*node
, void * /*data*/)
4204 DECL_EXTERNAL (node
->decl
) = 0;
4208 /* Build up the function to run dynamic initializers for thread_local
4209 variables in this translation unit and alias the init functions for the
4210 individual variables to it. */
4213 handle_tls_init (void)
4215 tree vars
= prune_vars_needing_no_initialization (&tls_aggregates
);
4216 if (vars
== NULL_TREE
)
4219 location_t loc
= DECL_SOURCE_LOCATION (TREE_VALUE (vars
));
4221 write_out_vars (vars
);
4223 tree guard
= build_decl (loc
, VAR_DECL
, get_identifier ("__tls_guard"),
4225 TREE_PUBLIC (guard
) = false;
4226 TREE_STATIC (guard
) = true;
4227 DECL_ARTIFICIAL (guard
) = true;
4228 DECL_IGNORED_P (guard
) = true;
4229 TREE_USED (guard
) = true;
4230 set_decl_tls_model (guard
, decl_default_tls_model (guard
));
4231 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
4233 tree fn
= get_local_tls_init_fn ();
4234 start_preparsed_function (fn
, NULL_TREE
, SF_PRE_PARSED
);
4235 tree body
= begin_function_body ();
4236 tree if_stmt
= begin_if_stmt ();
4237 tree cond
= cp_build_unary_op (TRUTH_NOT_EXPR
, guard
, false,
4238 tf_warning_or_error
);
4239 finish_if_stmt_cond (cond
, if_stmt
);
4240 finish_expr_stmt (cp_build_modify_expr (guard
, NOP_EXPR
, boolean_true_node
,
4241 tf_warning_or_error
));
4242 for (; vars
; vars
= TREE_CHAIN (vars
))
4244 tree var
= TREE_VALUE (vars
);
4245 tree init
= TREE_PURPOSE (vars
);
4246 one_static_initialization_or_destruction (var
, init
, true);
4248 #ifdef ASM_OUTPUT_DEF
4249 /* Output init aliases even with -fno-extern-tls-init. */
4250 if (TREE_PUBLIC (var
))
4252 tree single_init_fn
= get_tls_init_fn (var
);
4253 if (single_init_fn
== NULL_TREE
)
4256 = cgraph_node::get_create (fn
)->create_same_body_alias
4257 (single_init_fn
, fn
);
4258 gcc_assert (alias
!= NULL
);
4263 finish_then_clause (if_stmt
);
4264 finish_if_stmt (if_stmt
);
4265 finish_function_body (body
);
4266 expand_or_defer_fn (finish_function (0));
4269 /* The entire file is now complete. If requested, dump everything
4276 FILE *stream
= dump_begin (TDI_tu
, &flags
);
4280 dump_node (global_namespace
, flags
& ~TDF_SLIM
, stream
);
4281 dump_end (TDI_tu
, stream
);
4285 /* This routine is called at the end of compilation.
4286 Its job is to create all the code needed to initialize and
4287 destroy the global aggregates. We do the destruction
4288 first, since that way we only need to reverse the decls once. */
4291 cp_write_global_declarations (void)
4297 unsigned ssdf_count
= 0;
4300 hash_set
<tree
> *candidates
;
4302 locus
= input_location
;
4305 /* Bad parse errors. Just forget about it. */
4306 if (! global_bindings_p () || current_class_type
4307 || !vec_safe_is_empty (decl_namespace_list
))
4310 /* This is the point to write out a PCH if we're doing that.
4311 In that case we do not want to do anything else. */
4314 c_common_write_pch ();
4319 cgraph_process_same_body_aliases ();
4321 /* Handle -fdump-ada-spec[-slim] */
4322 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
4324 if (flag_dump_ada_spec_slim
)
4325 collect_source_ref (main_input_filename
);
4327 collect_source_refs (global_namespace
);
4329 dump_ada_specs (collect_all_refs
, cpp_check
);
4332 /* FIXME - huh? was input_line -= 1;*/
4334 timevar_start (TV_PHASE_DEFERRED
);
4336 /* We now have to write out all the stuff we put off writing out.
4339 o Template specializations that we have not yet instantiated,
4340 but which are needed.
4341 o Initialization and destruction for non-local objects with
4342 static storage duration. (Local objects with static storage
4343 duration are initialized when their scope is first entered,
4344 and are cleaned up via atexit.)
4345 o Virtual function tables.
4347 All of these may cause others to be needed. For example,
4348 instantiating one function may cause another to be needed, and
4349 generating the initializer for an object may cause templates to be
4350 instantiated, etc., etc. */
4352 emit_support_tinfos ();
4361 /* If there are templates that we've put off instantiating, do
4363 instantiate_pending_templates (retries
);
4366 /* Write out virtual tables as required. Note that writing out
4367 the virtual table for a template class may cause the
4368 instantiation of members of that class. If we write out
4369 vtables then we remove the class from our list so we don't
4370 have to look at it again. */
4372 while (keyed_classes
!= NULL_TREE
4373 && maybe_emit_vtables (TREE_VALUE (keyed_classes
)))
4376 keyed_classes
= TREE_CHAIN (keyed_classes
);
4382 tree next
= TREE_CHAIN (t
);
4386 if (maybe_emit_vtables (TREE_VALUE (next
)))
4389 TREE_CHAIN (t
) = TREE_CHAIN (next
);
4394 next
= TREE_CHAIN (t
);
4398 /* Write out needed type info variables. We have to be careful
4399 looping through unemitted decls, because emit_tinfo_decl may
4400 cause other variables to be needed. New elements will be
4401 appended, and we remove from the vector those that actually
4403 for (i
= unemitted_tinfo_decls
->length ();
4404 unemitted_tinfo_decls
->iterate (--i
, &t
);)
4405 if (emit_tinfo_decl (t
))
4408 unemitted_tinfo_decls
->unordered_remove (i
);
4411 /* The list of objects with static storage duration is built up
4412 in reverse order. We clear STATIC_AGGREGATES so that any new
4413 aggregates added during the initialization of these will be
4414 initialized in the correct order when we next come around the
4416 vars
= prune_vars_needing_no_initialization (&static_aggregates
);
4420 /* We need to start a new initialization function each time
4421 through the loop. That's because we need to know which
4422 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4423 isn't computed until a function is finished, and written
4424 out. That's a deficiency in the back end. When this is
4425 fixed, these initialization functions could all become
4426 inline, with resulting performance improvements. */
4429 /* Set the line and file, so that it is obviously not from
4431 input_location
= locus
;
4432 ssdf_body
= start_static_storage_duration_function (ssdf_count
);
4434 /* Make sure the back end knows about all the variables. */
4435 write_out_vars (vars
);
4437 /* First generate code to do all the initializations. */
4439 do_static_initialization_or_destruction (vars
, /*initp=*/true);
4441 /* Then, generate code to do all the destructions. Do these
4442 in reverse order so that the most recently constructed
4443 variable is the first destroyed. If we're using
4444 __cxa_atexit, then we don't need to do this; functions
4445 were registered at initialization time to destroy the
4447 if (!flag_use_cxa_atexit
&& vars
)
4449 vars
= nreverse (vars
);
4450 do_static_initialization_or_destruction (vars
, /*initp=*/false);
4455 /* Finish up the static storage duration function for this
4457 input_location
= locus
;
4458 finish_static_storage_duration_function (ssdf_body
);
4460 /* All those initializations and finalizations might cause
4461 us to need more inline functions, more template
4462 instantiations, etc. */
4465 /* ??? was: locus.line++; */
4468 /* Now do the same for thread_local variables. */
4471 /* Go through the set of inline functions whose bodies have not
4472 been emitted yet. If out-of-line copies of these functions
4473 are required, emit them. */
4474 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4476 /* Does it need synthesizing? */
4477 if (DECL_DEFAULTED_FN (decl
) && ! DECL_INITIAL (decl
)
4478 && (! DECL_REALLY_EXTERN (decl
) || possibly_inlined_p (decl
)))
4480 /* Even though we're already at the top-level, we push
4481 there again. That way, when we pop back a few lines
4482 hence, all of our state is restored. Otherwise,
4483 finish_function doesn't clean things up, and we end
4484 up with CURRENT_FUNCTION_DECL set. */
4485 push_to_top_level ();
4486 /* The decl's location will mark where it was first
4487 needed. Save that so synthesize method can indicate
4488 where it was needed from, in case of error */
4489 input_location
= DECL_SOURCE_LOCATION (decl
);
4490 synthesize_method (decl
);
4491 pop_from_top_level ();
4495 if (!DECL_INITIAL (decl
) && decl_tls_wrapper_p (decl
))
4496 generate_tls_wrapper (decl
);
4498 if (!DECL_SAVED_TREE (decl
))
4501 /* We lie to the back end, pretending that some functions
4502 are not defined when they really are. This keeps these
4503 functions from being put out unnecessarily. But, we must
4504 stop lying when the functions are referenced, or if they
4505 are not comdat since they need to be put out now. If
4506 DECL_INTERFACE_KNOWN, then we have already set
4507 DECL_EXTERNAL appropriately, so there's no need to check
4508 again, and we do not want to clear DECL_EXTERNAL if a
4509 previous call to import_export_decl set it.
4511 This is done in a separate for cycle, because if some
4512 deferred function is contained in another deferred
4513 function later in deferred_fns varray,
4514 rest_of_compilation would skip this function and we
4515 really cannot expand the same function twice. */
4516 import_export_decl (decl
);
4517 if (DECL_NOT_REALLY_EXTERN (decl
)
4518 && DECL_INITIAL (decl
)
4519 && decl_needed_p (decl
))
4521 struct cgraph_node
*node
, *next
;
4523 node
= cgraph_node::get (decl
);
4524 if (node
->cpp_implicit_alias
)
4525 node
= node
->get_alias_target ();
4527 node
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
4529 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4530 group, we need to mark all symbols in the same comdat group
4532 if (node
->same_comdat_group
)
4533 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
4535 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
4536 next
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
4540 /* If we're going to need to write this function out, and
4541 there's already a body for it, create RTL for it now.
4542 (There might be no body if this is a method we haven't
4543 gotten around to synthesizing yet.) */
4544 if (!DECL_EXTERNAL (decl
)
4545 && decl_needed_p (decl
)
4546 && !TREE_ASM_WRITTEN (decl
)
4547 && !cgraph_node::get (decl
)->definition
)
4549 /* We will output the function; no longer consider it in this
4551 DECL_DEFER_OUTPUT (decl
) = 0;
4552 /* Generate RTL for this function now that we know we
4554 expand_or_defer_fn (decl
);
4555 /* If we're compiling -fsyntax-only pretend that this
4556 function has been written out so that we don't try to
4558 if (flag_syntax_only
)
4559 TREE_ASM_WRITTEN (decl
) = 1;
4564 if (walk_namespaces (wrapup_globals_for_namespace
, /*data=*/0))
4567 /* Static data members are just like namespace-scope globals. */
4568 FOR_EACH_VEC_SAFE_ELT (pending_statics
, i
, decl
)
4570 if (var_finalized_p (decl
) || DECL_REALLY_EXTERN (decl
)
4571 /* Don't write it out if we haven't seen a definition. */
4572 || DECL_IN_AGGR_P (decl
))
4574 import_export_decl (decl
);
4575 /* If this static data member is needed, provide it to the
4577 if (DECL_NOT_REALLY_EXTERN (decl
) && decl_needed_p (decl
))
4578 DECL_EXTERNAL (decl
) = 0;
4580 if (vec_safe_length (pending_statics
) != 0
4581 && wrapup_global_declarations (pending_statics
->address (),
4582 pending_statics
->length ()))
4589 /* All used inline functions must have a definition at this point. */
4590 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4592 if (/* Check online inline functions that were actually used. */
4593 DECL_ODR_USED (decl
) && DECL_DECLARED_INLINE_P (decl
)
4594 /* If the definition actually was available here, then the
4595 fact that the function was not defined merely represents
4596 that for some reason (use of a template repository,
4597 #pragma interface, etc.) we decided not to emit the
4599 && !DECL_INITIAL (decl
)
4600 /* Don't complain if the template was defined. */
4601 && !(DECL_TEMPLATE_INSTANTIATION (decl
)
4602 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4603 (template_for_substitution (decl
)))))
4605 warning (0, "inline function %q+D used but never defined", decl
);
4606 /* Avoid a duplicate warning from check_global_declaration_1. */
4607 TREE_NO_WARNING (decl
) = 1;
4611 /* So must decls that use a type with no linkage. */
4612 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls
, i
, decl
)
4613 no_linkage_error (decl
);
4615 /* Then, do the Objective-C stuff. This is where all the
4616 Objective-C module stuff gets generated (symtab,
4617 class/protocol/selector lists etc). This must be done after C++
4618 templates, destructors etc. so that selectors used in C++
4619 templates are properly allocated. */
4620 if (c_dialect_objc ())
4621 objc_write_global_declarations ();
4623 /* We give C linkage to static constructors and destructors. */
4624 push_lang_context (lang_name_c
);
4626 /* Generate initialization and destruction functions for all
4627 priorities for which they are required. */
4628 if (priority_info_map
)
4629 splay_tree_foreach (priority_info_map
,
4630 generate_ctor_and_dtor_functions_for_priority
,
4632 else if (c_dialect_objc () && objc_static_init_needed_p ())
4633 /* If this is obj-c++ and we need a static init, call
4634 generate_ctor_or_dtor_function. */
4635 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4636 DEFAULT_INIT_PRIORITY
, &locus
);
4638 /* We're done with the splay-tree now. */
4639 if (priority_info_map
)
4640 splay_tree_delete (priority_info_map
);
4642 /* Generate any missing aliases. */
4643 maybe_apply_pending_pragma_weaks ();
4645 /* We're done with static constructors, so we can go back to "C++"
4647 pop_lang_context ();
4649 /* Collect candidates for Java hidden aliases. */
4650 candidates
= collect_candidates_for_java_method_aliases ();
4652 timevar_stop (TV_PHASE_DEFERRED
);
4653 timevar_start (TV_PHASE_OPT_GEN
);
4655 if (flag_vtable_verify
)
4657 vtv_recover_class_info ();
4658 vtv_compute_class_hierarchy_transitive_closure ();
4659 vtv_build_vtable_verify_fndecl ();
4662 finalize_compilation_unit ();
4664 if (flag_vtable_verify
)
4666 /* Generate the special constructor initialization function that
4667 calls __VLTRegisterPairs, and give it a very high
4668 initialization priority. This must be done after
4669 finalize_compilation_unit so that we have accurate
4670 information about which vtable will actually be emitted. */
4671 vtv_generate_init_routine ();
4674 timevar_stop (TV_PHASE_OPT_GEN
);
4675 timevar_start (TV_PHASE_CHECK_DBGINFO
);
4677 /* Now, issue warnings about static, but not defined, functions,
4678 etc., and emit debugging information. */
4679 walk_namespaces (wrapup_globals_for_namespace
, /*data=*/&reconsider
);
4680 if (vec_safe_length (pending_statics
) != 0)
4682 check_global_declarations (pending_statics
->address (),
4683 pending_statics
->length ());
4684 emit_debug_global_declarations (pending_statics
->address (),
4685 pending_statics
->length ());
4688 perform_deferred_noexcept_checks ();
4690 /* Generate hidden aliases for Java. */
4693 build_java_method_aliases (candidates
);
4699 /* The entire file is now complete. If requested, dump everything
4703 if (flag_detailed_statistics
)
4705 dump_tree_statistics ();
4706 dump_time_statistics ();
4708 input_location
= locus
;
4710 #ifdef ENABLE_CHECKING
4711 validate_conversion_obstack ();
4712 #endif /* ENABLE_CHECKING */
4714 timevar_stop (TV_PHASE_CHECK_DBGINFO
);
4717 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4718 function to call in parse-tree form; it has not yet been
4719 semantically analyzed. ARGS are the arguments to the function.
4720 They have already been semantically analyzed. This may change
4724 build_offset_ref_call_from_tree (tree fn
, vec
<tree
, va_gc
> **args
,
4725 tsubst_flags_t complain
)
4728 vec
<tree
, va_gc
> *orig_args
= NULL
;
4733 object
= TREE_OPERAND (fn
, 0);
4735 if (processing_template_decl
)
4737 gcc_assert (TREE_CODE (fn
) == DOTSTAR_EXPR
4738 || TREE_CODE (fn
) == MEMBER_REF
);
4739 if (type_dependent_expression_p (fn
)
4740 || any_type_dependent_arguments_p (*args
))
4741 return build_nt_call_vec (fn
, *args
);
4743 orig_args
= make_tree_vector_copy (*args
);
4745 /* Transform the arguments and add the implicit "this"
4746 parameter. That must be done before the FN is transformed
4747 because we depend on the form of FN. */
4748 make_args_non_dependent (*args
);
4749 object
= build_non_dependent_expr (object
);
4750 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4752 if (TREE_CODE (fn
) == DOTSTAR_EXPR
)
4753 object
= cp_build_addr_expr (object
, complain
);
4754 vec_safe_insert (*args
, 0, object
);
4756 /* Now that the arguments are done, transform FN. */
4757 fn
= build_non_dependent_expr (fn
);
4760 /* A qualified name corresponding to a bound pointer-to-member is
4761 represented as an OFFSET_REF:
4763 struct B { void g(); };
4765 void B::g() { (this->*p)(); } */
4766 if (TREE_CODE (fn
) == OFFSET_REF
)
4768 tree object_addr
= cp_build_addr_expr (object
, complain
);
4769 fn
= TREE_OPERAND (fn
, 1);
4770 fn
= get_member_function_from_ptrfunc (&object_addr
, fn
,
4772 vec_safe_insert (*args
, 0, object_addr
);
4775 if (CLASS_TYPE_P (TREE_TYPE (fn
)))
4776 expr
= build_op_call (fn
, args
, complain
);
4778 expr
= cp_build_function_call_vec (fn
, args
, complain
);
4779 if (processing_template_decl
&& expr
!= error_mark_node
)
4780 expr
= build_min_non_dep_call_vec (expr
, orig_fn
, orig_args
);
4782 if (orig_args
!= NULL
)
4783 release_tree_vector (orig_args
);
4790 check_default_args (tree x
)
4792 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
4793 bool saw_def
= false;
4794 int i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
4795 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
4797 if (TREE_PURPOSE (arg
))
4799 else if (saw_def
&& !PACK_EXPANSION_P (TREE_VALUE (arg
)))
4801 error ("default argument missing for parameter %P of %q+#D", i
, x
);
4802 TREE_PURPOSE (arg
) = error_mark_node
;
4807 /* Return true if function DECL can be inlined. This is used to force
4808 instantiation of methods that might be interesting for inlining. */
4810 possibly_inlined_p (tree decl
)
4812 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
4813 if (DECL_UNINLINABLE (decl
))
4815 if (!optimize
|| pragma_java_exceptions
)
4816 return DECL_DECLARED_INLINE_P (decl
);
4817 /* When optimizing, we might inline everything when flatten
4818 attribute or heuristics inlining for size or autoinlining
4823 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4824 If DECL is a specialization or implicitly declared class member,
4825 generate the actual definition. Return false if something goes
4826 wrong, true otherwise. */
4829 mark_used (tree decl
, tsubst_flags_t complain
)
4831 /* If DECL is a BASELINK for a single function, then treat it just
4832 like the DECL for the function. Otherwise, if the BASELINK is
4833 for an overloaded function, we don't know which function was
4834 actually used until after overload resolution. */
4835 if (BASELINK_P (decl
))
4837 decl
= BASELINK_FUNCTIONS (decl
);
4838 if (really_overloaded_fn (decl
))
4840 decl
= OVL_CURRENT (decl
);
4843 /* Set TREE_USED for the benefit of -Wunused. */
4844 TREE_USED (decl
) = 1;
4845 if (DECL_CLONED_FUNCTION_P (decl
))
4846 TREE_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
4848 /* Mark enumeration types as used. */
4849 if (TREE_CODE (decl
) == CONST_DECL
)
4850 used_types_insert (DECL_CONTEXT (decl
));
4852 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4853 maybe_instantiate_noexcept (decl
);
4855 if (TREE_CODE (decl
) == FUNCTION_DECL
4856 && DECL_DELETED_FN (decl
))
4858 if (DECL_ARTIFICIAL (decl
))
4860 if (DECL_OVERLOADED_OPERATOR_P (decl
) == TYPE_EXPR
4861 && LAMBDA_TYPE_P (DECL_CONTEXT (decl
)))
4863 /* We mark a lambda conversion op as deleted if we can't
4864 generate it properly; see maybe_add_lambda_conv_op. */
4865 sorry ("converting lambda which uses %<...%> to "
4866 "function pointer");
4870 if (complain
& tf_error
)
4872 error ("use of deleted function %qD", decl
);
4873 if (!maybe_explain_implicit_delete (decl
))
4874 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
4879 /* We can only check DECL_ODR_USED on variables or functions with
4880 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4881 might need special handling for. */
4882 if (!VAR_OR_FUNCTION_DECL_P (decl
)
4883 || DECL_LANG_SPECIFIC (decl
) == NULL
4884 || DECL_THUNK_P (decl
))
4886 if (!processing_template_decl
&& type_uses_auto (TREE_TYPE (decl
)))
4888 if (complain
& tf_error
)
4889 error ("use of %qD before deduction of %<auto%>", decl
);
4895 /* We only want to do this processing once. We don't need to keep trying
4896 to instantiate inline templates, because unit-at-a-time will make sure
4897 we get them compiled before functions that want to inline them. */
4898 if (DECL_ODR_USED (decl
))
4901 /* If within finish_function, defer the rest until that function
4902 finishes, otherwise it might recurse. */
4903 if (defer_mark_used_calls
)
4905 vec_safe_push (deferred_mark_used_calls
, decl
);
4909 /* Normally, we can wait until instantiation-time to synthesize DECL.
4910 However, if DECL is a static data member initialized with a constant
4911 or a constexpr function, we need it right now because a reference to
4912 such a data member or a call to such function is not value-dependent.
4913 For a function that uses auto in the return type, we need to instantiate
4914 it to find out its type. For OpenMP user defined reductions, we need
4915 them instantiated for reduction clauses which inline them by hand
4917 if (DECL_LANG_SPECIFIC (decl
)
4918 && DECL_TEMPLATE_INFO (decl
)
4919 && (decl_maybe_constant_var_p (decl
)
4920 || (TREE_CODE (decl
) == FUNCTION_DECL
4921 && (DECL_DECLARED_CONSTEXPR_P (decl
)
4922 || DECL_OMP_DECLARE_REDUCTION_P (decl
)))
4923 || undeduced_auto_decl (decl
))
4924 && !uses_template_parms (DECL_TI_ARGS (decl
)))
4926 /* Instantiating a function will result in garbage collection. We
4927 must treat this situation as if we were within the body of a
4928 function so as to avoid collecting live data only referenced from
4929 the stack (such as overload resolution candidates). */
4931 instantiate_decl (decl
, /*defer_ok=*/false,
4932 /*expl_inst_class_mem_p=*/false);
4936 if (processing_template_decl
)
4939 /* Check this too in case we're within fold_non_dependent_expr. */
4940 if (DECL_TEMPLATE_INFO (decl
)
4941 && uses_template_parms (DECL_TI_ARGS (decl
)))
4944 require_deduced_type (decl
);
4946 /* If we don't need a value, then we don't need to synthesize DECL. */
4947 if (cp_unevaluated_operand
!= 0)
4950 DECL_ODR_USED (decl
) = 1;
4951 if (DECL_CLONED_FUNCTION_P (decl
))
4952 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
4954 /* DR 757: A type without linkage shall not be used as the type of a
4955 variable or function with linkage, unless
4956 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4957 o the variable or function is not used (3.2 [basic.def.odr]) or is
4958 defined in the same translation unit. */
4959 if (cxx_dialect
> cxx98
4960 && decl_linkage (decl
) != lk_none
4961 && !DECL_EXTERN_C_P (decl
)
4962 && !DECL_ARTIFICIAL (decl
)
4963 && !decl_defined_p (decl
)
4964 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
4966 if (is_local_extern (decl
))
4967 /* There's no way to define a local extern, and adding it to
4968 the vector interferes with GC, so give an error now. */
4969 no_linkage_error (decl
);
4971 vec_safe_push (no_linkage_decls
, decl
);
4974 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (decl
)
4975 && !DECL_INITIAL (decl
) && !DECL_ARTIFICIAL (decl
))
4976 /* Remember it, so we can check it was defined. */
4977 note_vague_linkage_fn (decl
);
4979 /* Is it a synthesized method that needs to be synthesized? */
4980 if (TREE_CODE (decl
) == FUNCTION_DECL
4981 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
4982 && DECL_DEFAULTED_FN (decl
)
4983 /* A function defaulted outside the class is synthesized either by
4984 cp_finish_decl or instantiate_decl. */
4985 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl
)
4986 && ! DECL_INITIAL (decl
))
4988 /* Defer virtual destructors so that thunks get the right
4990 if (DECL_VIRTUAL_P (decl
) && !at_eof
)
4992 note_vague_linkage_fn (decl
);
4996 /* Remember the current location for a function we will end up
4997 synthesizing. Then we can inform the user where it was
4998 required in the case of error. */
4999 DECL_SOURCE_LOCATION (decl
) = input_location
;
5001 /* Synthesizing an implicitly defined member function will result in
5002 garbage collection. We must treat this situation as if we were
5003 within the body of a function so as to avoid collecting live data
5004 on the stack (such as overload resolution candidates).
5006 We could just let cp_write_global_declarations handle synthesizing
5007 this function by adding it to deferred_fns, but doing
5008 it at the use site produces better error messages. */
5010 synthesize_method (decl
);
5012 /* If this is a synthesized method we don't need to
5013 do the instantiation test below. */
5015 else if (VAR_OR_FUNCTION_DECL_P (decl
)
5016 && DECL_TEMPLATE_INFO (decl
)
5017 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
5018 || always_instantiate_p (decl
)))
5019 /* If this is a function or variable that is an instance of some
5020 template, we now know that we will need to actually do the
5021 instantiation. We check that DECL is not an explicit
5022 instantiation because that is not checked in instantiate_decl.
5024 We put off instantiating functions in order to improve compile
5025 times. Maintaining a stack of active functions is expensive,
5026 and the inliner knows to instantiate any functions it might
5027 need. Therefore, we always try to defer instantiation. */
5030 instantiate_decl (decl
, /*defer_ok=*/true,
5031 /*expl_inst_class_mem_p=*/false);
5039 mark_used (tree decl
)
5041 return mark_used (decl
, tf_warning_or_error
);
5045 vtv_start_verification_constructor_init_function (void)
5047 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY
- 1);
5051 vtv_finish_verification_constructor_init_function (tree function_body
)
5055 finish_compound_stmt (function_body
);
5056 fn
= finish_function (0);
5057 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
5058 decl_init_priority_insert (fn
, MAX_RESERVED_INIT_PRIORITY
- 1);
5063 #include "gt-cp-decl2.h"