1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2016 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 "c-family/c-common.h"
36 #include "stringpool.h"
40 #include "stor-layout.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-pragma.h"
48 #include "c-family/c-ada-spec.h"
51 extern cpp_reader
*parse_in
;
53 /* This structure contains information about the initializations
54 and/or destructions required for a particular priority level. */
55 typedef struct priority_info_s
{
56 /* Nonzero if there have been any initializations at this priority
57 throughout the translation unit. */
58 int initializations_p
;
59 /* Nonzero if there have been any destructions at this priority
60 throughout the translation unit. */
64 static void mark_vtable_entries (tree
);
65 static bool maybe_emit_vtables (tree
);
66 static bool acceptable_java_type (tree
);
67 static tree
start_objects (int, int);
68 static void finish_objects (int, int, tree
);
69 static tree
start_static_storage_duration_function (unsigned);
70 static void finish_static_storage_duration_function (tree
);
71 static priority_info
get_priority_info (int);
72 static void do_static_initialization_or_destruction (tree
, bool);
73 static void one_static_initialization_or_destruction (tree
, tree
, bool);
74 static void generate_ctor_or_dtor_function (bool, int, location_t
*);
75 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node
,
77 static tree
prune_vars_needing_no_initialization (tree
*);
78 static void write_out_vars (tree
);
79 static void import_export_class (tree
);
80 static tree
get_guard_bits (tree
);
81 static void determine_visibility_from_class (tree
, tree
);
82 static bool determine_hidden_inline (tree
);
83 static bool decl_defined_p (tree
);
84 static void maybe_instantiate_decl (tree
);
86 /* A list of static class variables. This is needed, because a
87 static class variable can be declared inside the class without
88 an initializer, and then initialized, statically, outside the class. */
89 static GTY(()) vec
<tree
, va_gc
> *pending_statics
;
91 /* A list of functions which were declared inline, but which we
92 may need to emit outline anyway. */
93 static GTY(()) vec
<tree
, va_gc
> *deferred_fns
;
95 /* A list of decls that use types with no linkage, which we need to make
97 static GTY(()) vec
<tree
, va_gc
> *no_linkage_decls
;
99 /* A vector of alternating decls and identifiers, where the latter
100 is to be an alias for the former if the former is defined. */
101 static GTY(()) vec
<tree
, va_gc
> *mangling_aliases
;
103 /* Nonzero if we're done parsing and into end-of-file activities. */
107 /* True if note_mangling_alias should enqueue mangling aliases for
108 later generation, rather than emitting them right away. */
110 bool defer_mangling_aliases
= true;
113 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
114 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
115 that apply to the function). */
118 build_memfn_type (tree fntype
, tree ctype
, cp_cv_quals quals
,
119 cp_ref_qualifier rqual
)
124 bool late_return_type_p
;
126 if (fntype
== error_mark_node
|| ctype
== error_mark_node
)
127 return error_mark_node
;
129 gcc_assert (TREE_CODE (fntype
) == FUNCTION_TYPE
130 || TREE_CODE (fntype
) == METHOD_TYPE
);
132 type_quals
= quals
& ~TYPE_QUAL_RESTRICT
;
133 ctype
= cp_build_qualified_type (ctype
, type_quals
);
134 raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
135 attrs
= TYPE_ATTRIBUTES (fntype
);
136 late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (fntype
);
137 fntype
= build_method_type_directly (ctype
, TREE_TYPE (fntype
),
138 (TREE_CODE (fntype
) == METHOD_TYPE
139 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype
))
140 : TYPE_ARG_TYPES (fntype
)));
142 fntype
= cp_build_type_attribute_variant (fntype
, attrs
);
144 fntype
= build_ref_qualified_type (fntype
, rqual
);
146 fntype
= build_exception_variant (fntype
, raises
);
147 if (late_return_type_p
)
148 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
153 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
154 return type changed to NEW_RET. */
157 change_return_type (tree new_ret
, tree fntype
)
160 tree args
= TYPE_ARG_TYPES (fntype
);
161 tree raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
162 tree attrs
= TYPE_ATTRIBUTES (fntype
);
163 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (fntype
);
165 if (new_ret
== error_mark_node
)
168 if (same_type_p (new_ret
, TREE_TYPE (fntype
)))
171 if (TREE_CODE (fntype
) == FUNCTION_TYPE
)
173 newtype
= build_function_type (new_ret
, args
);
174 newtype
= apply_memfn_quals (newtype
,
175 type_memfn_quals (fntype
),
176 type_memfn_rqual (fntype
));
179 newtype
= build_method_type_directly
180 (class_of_this_parm (fntype
), new_ret
, TREE_CHAIN (args
));
181 if (FUNCTION_REF_QUALIFIED (fntype
))
182 newtype
= build_ref_qualified_type (newtype
, type_memfn_rqual (fntype
));
184 newtype
= build_exception_variant (newtype
, raises
);
186 newtype
= cp_build_type_attribute_variant (newtype
, attrs
);
187 if (late_return_type_p
)
188 TYPE_HAS_LATE_RETURN_TYPE (newtype
) = 1;
193 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
197 cp_build_parm_decl (tree name
, tree type
)
199 tree parm
= build_decl (input_location
,
200 PARM_DECL
, name
, type
);
201 /* DECL_ARG_TYPE is only used by the back end and the back end never
203 if (!processing_template_decl
)
204 DECL_ARG_TYPE (parm
) = type_passed_as (type
);
209 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
213 build_artificial_parm (tree name
, tree type
)
215 tree parm
= cp_build_parm_decl (name
, type
);
216 DECL_ARTIFICIAL (parm
) = 1;
217 /* All our artificial parms are implicitly `const'; they cannot be
219 TREE_READONLY (parm
) = 1;
223 /* Constructors for types with virtual baseclasses need an "in-charge" flag
224 saying whether this constructor is responsible for initialization of
225 virtual baseclasses or not. All destructors also need this "in-charge"
226 flag, which additionally determines whether or not the destructor should
227 free the memory for the object.
229 This function adds the "in-charge" flag to member function FN if
230 appropriate. It is called from grokclassfn and tsubst.
231 FN must be either a constructor or destructor.
233 The in-charge flag follows the 'this' parameter, and is followed by the
234 VTT parm (if any), then the user-written parms. */
237 maybe_retrofit_in_chrg (tree fn
)
239 tree basetype
, arg_types
, parms
, parm
, fntype
;
241 /* If we've already add the in-charge parameter don't do it again. */
242 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
245 /* When processing templates we can't know, in general, whether or
246 not we're going to have virtual baseclasses. */
247 if (processing_template_decl
)
250 /* We don't need an in-charge parameter for constructors that don't
251 have virtual bases. */
252 if (DECL_CONSTRUCTOR_P (fn
)
253 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
256 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
257 basetype
= TREE_TYPE (TREE_VALUE (arg_types
));
258 arg_types
= TREE_CHAIN (arg_types
);
260 parms
= DECL_CHAIN (DECL_ARGUMENTS (fn
));
262 /* If this is a subobject constructor or destructor, our caller will
263 pass us a pointer to our VTT. */
264 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
266 parm
= build_artificial_parm (vtt_parm_identifier
, vtt_parm_type
);
268 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
269 DECL_CHAIN (parm
) = parms
;
272 /* ...and then to TYPE_ARG_TYPES. */
273 arg_types
= hash_tree_chain (vtt_parm_type
, arg_types
);
275 DECL_HAS_VTT_PARM_P (fn
) = 1;
278 /* Then add the in-charge parm (before the VTT parm). */
279 parm
= build_artificial_parm (in_charge_identifier
, integer_type_node
);
280 DECL_CHAIN (parm
) = parms
;
282 arg_types
= hash_tree_chain (integer_type_node
, arg_types
);
284 /* Insert our new parameter(s) into the list. */
285 DECL_CHAIN (DECL_ARGUMENTS (fn
)) = parms
;
287 /* And rebuild the function type. */
288 fntype
= build_method_type_directly (basetype
, TREE_TYPE (TREE_TYPE (fn
)),
290 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)))
291 fntype
= build_exception_variant (fntype
,
292 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)));
293 if (TYPE_ATTRIBUTES (TREE_TYPE (fn
)))
294 fntype
= (cp_build_type_attribute_variant
295 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (fn
))));
296 TREE_TYPE (fn
) = fntype
;
298 /* Now we've got the in-charge parameter. */
299 DECL_HAS_IN_CHARGE_PARM_P (fn
) = 1;
302 /* Classes overload their constituent function names automatically.
303 When a function name is declared in a record structure,
304 its name is changed to it overloaded name. Since names for
305 constructors and destructors can conflict, we place a leading
308 CNAME is the name of the class we are grokking for.
310 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
312 FLAGS contains bits saying what's special about today's
313 arguments. DTOR_FLAG == DESTRUCTOR.
315 If FUNCTION is a destructor, then we must add the `auto-delete' field
316 as a second parameter. There is some hair associated with the fact
317 that we must "declare" this variable in the manner consistent with the
318 way the rest of the arguments were declared.
320 QUALS are the qualifiers for the this pointer. */
323 grokclassfn (tree ctype
, tree function
, enum overload_flags flags
)
325 tree fn_name
= DECL_NAME (function
);
327 /* Even within an `extern "C"' block, members get C++ linkage. See
328 [dcl.link] for details. */
329 SET_DECL_LANGUAGE (function
, lang_cplusplus
);
331 if (fn_name
== NULL_TREE
)
333 error ("name missing for member function");
334 fn_name
= get_identifier ("<anonymous>");
335 DECL_NAME (function
) = fn_name
;
338 DECL_CONTEXT (function
) = ctype
;
340 if (flags
== DTOR_FLAG
)
341 DECL_DESTRUCTOR_P (function
) = 1;
343 if (flags
== DTOR_FLAG
|| DECL_CONSTRUCTOR_P (function
))
344 maybe_retrofit_in_chrg (function
);
347 /* Create an ARRAY_REF, checking for the user doing things backwards
348 along the way. DECLTYPE_P is for N3276, as in the parser. */
351 grok_array_decl (location_t loc
, tree array_expr
, tree index_exp
,
356 tree orig_array_expr
= array_expr
;
357 tree orig_index_exp
= index_exp
;
358 tree overload
= NULL_TREE
;
360 if (error_operand_p (array_expr
) || error_operand_p (index_exp
))
361 return error_mark_node
;
363 if (processing_template_decl
)
365 if (type_dependent_expression_p (array_expr
)
366 || type_dependent_expression_p (index_exp
))
367 return build_min_nt_loc (loc
, ARRAY_REF
, array_expr
, index_exp
,
368 NULL_TREE
, NULL_TREE
);
369 array_expr
= build_non_dependent_expr (array_expr
);
370 index_exp
= build_non_dependent_expr (index_exp
);
373 type
= TREE_TYPE (array_expr
);
375 type
= non_reference (type
);
377 /* If they have an `operator[]', use that. */
378 if (MAYBE_CLASS_TYPE_P (type
) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp
)))
380 tsubst_flags_t complain
= tf_warning_or_error
;
382 complain
|= tf_decltype
;
383 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, array_expr
,
384 index_exp
, NULL_TREE
, &overload
, complain
);
390 /* Otherwise, create an ARRAY_REF for a pointer or array type.
391 It is a little-known fact that, if `a' is an array and `i' is
392 an int, you can write `i[a]', which means the same thing as
394 if (TREE_CODE (type
) == ARRAY_TYPE
|| VECTOR_TYPE_P (type
))
397 p1
= build_expr_type_conversion (WANT_POINTER
, array_expr
, false);
399 if (TREE_CODE (TREE_TYPE (index_exp
)) == ARRAY_TYPE
)
402 p2
= build_expr_type_conversion (WANT_POINTER
, index_exp
, false);
404 i1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, array_expr
,
406 i2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, index_exp
,
409 if ((p1
&& i2
) && (i1
&& p2
))
410 error ("ambiguous conversion for array subscript");
413 array_expr
= p1
, index_exp
= i2
;
415 array_expr
= p2
, index_exp
= i1
;
418 error ("invalid types %<%T[%T]%> for array subscript",
419 type
, TREE_TYPE (index_exp
));
420 return error_mark_node
;
423 if (array_expr
== error_mark_node
|| index_exp
== error_mark_node
)
424 error ("ambiguous conversion for array subscript");
426 expr
= build_array_ref (input_location
, array_expr
, index_exp
);
428 if (processing_template_decl
&& expr
!= error_mark_node
)
430 if (overload
!= NULL_TREE
)
431 return (build_min_non_dep_op_overload
432 (ARRAY_REF
, expr
, overload
, orig_array_expr
, orig_index_exp
));
434 return build_min_non_dep (ARRAY_REF
, expr
, orig_array_expr
, orig_index_exp
,
435 NULL_TREE
, NULL_TREE
);
440 /* Given the cast expression EXP, checking out its validity. Either return
441 an error_mark_node if there was an unavoidable error, return a cast to
442 void for trying to delete a pointer w/ the value 0, or return the
443 call to delete. If DOING_VEC is true, we handle things differently
444 for doing an array delete.
445 Implements ARM $5.3.4. This is called from the parser. */
448 delete_sanity (tree exp
, tree size
, bool doing_vec
, int use_global_delete
,
449 tsubst_flags_t complain
)
453 if (exp
== error_mark_node
)
456 if (processing_template_decl
)
458 t
= build_min (DELETE_EXPR
, void_type_node
, exp
, size
);
459 DELETE_EXPR_USE_GLOBAL (t
) = use_global_delete
;
460 DELETE_EXPR_USE_VEC (t
) = doing_vec
;
461 TREE_SIDE_EFFECTS (t
) = 1;
465 /* An array can't have been allocated by new, so complain. */
466 if (TREE_CODE (TREE_TYPE (exp
)) == ARRAY_TYPE
)
467 warning (0, "deleting array %q#E", exp
);
469 t
= build_expr_type_conversion (WANT_POINTER
, exp
, true);
471 if (t
== NULL_TREE
|| t
== error_mark_node
)
473 error ("type %q#T argument given to %<delete%>, expected pointer",
475 return error_mark_node
;
478 type
= TREE_TYPE (t
);
480 /* As of Valley Forge, you can delete a pointer to const. */
482 /* You can't delete functions. */
483 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
485 error ("cannot delete a function. Only pointer-to-objects are "
486 "valid arguments to %<delete%>");
487 return error_mark_node
;
490 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
491 if (VOID_TYPE_P (TREE_TYPE (type
)))
493 warning (OPT_Wdelete_incomplete
, "deleting %qT is undefined", type
);
497 /* Deleting a pointer with the value zero is valid and has no effect. */
498 if (integer_zerop (t
))
499 return build1 (NOP_EXPR
, void_type_node
, t
);
502 return build_vec_delete (t
, /*maxindex=*/NULL_TREE
,
503 sfk_deleting_destructor
,
504 use_global_delete
, complain
);
506 return build_delete (type
, t
, sfk_deleting_destructor
,
507 LOOKUP_NORMAL
, use_global_delete
,
511 /* Report an error if the indicated template declaration is not the
512 sort of thing that should be a member template. */
515 check_member_template (tree tmpl
)
519 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
520 decl
= DECL_TEMPLATE_RESULT (tmpl
);
522 if (TREE_CODE (decl
) == FUNCTION_DECL
523 || DECL_ALIAS_TEMPLATE_P (tmpl
)
524 || (TREE_CODE (decl
) == TYPE_DECL
525 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))))
527 /* The parser rejects template declarations in local classes
528 (with the exception of generic lambdas). */
529 gcc_assert (!current_function_decl
|| LAMBDA_FUNCTION_P (decl
));
530 /* The parser rejects any use of virtual in a function template. */
531 gcc_assert (!(TREE_CODE (decl
) == FUNCTION_DECL
532 && DECL_VIRTUAL_P (decl
)));
534 /* The debug-information generating code doesn't know what to do
535 with member templates. */
536 DECL_IGNORED_P (tmpl
) = 1;
538 else if (variable_template_p (tmpl
))
541 error ("template declaration of %q#D", decl
);
544 /* Return true iff TYPE is a valid Java parameter or return type. */
547 acceptable_java_type (tree type
)
549 if (type
== error_mark_node
)
552 if (VOID_TYPE_P (type
) || TYPE_FOR_JAVA (type
))
554 if (TYPE_PTR_P (type
) || TREE_CODE (type
) == REFERENCE_TYPE
)
556 type
= TREE_TYPE (type
);
557 if (TREE_CODE (type
) == RECORD_TYPE
)
560 if (! TYPE_FOR_JAVA (type
))
562 if (! CLASSTYPE_TEMPLATE_INFO (type
))
564 args
= CLASSTYPE_TI_ARGS (type
);
565 i
= TREE_VEC_LENGTH (args
);
568 type
= TREE_VEC_ELT (args
, i
);
569 if (TYPE_PTR_P (type
))
570 type
= TREE_TYPE (type
);
571 if (! TYPE_FOR_JAVA (type
))
580 /* For a METHOD in a Java class CTYPE, return true if
581 the parameter and return types are valid Java types.
582 Otherwise, print appropriate error messages, and return false. */
585 check_java_method (tree method
)
588 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (method
));
589 tree ret_type
= TREE_TYPE (TREE_TYPE (method
));
591 if (!acceptable_java_type (ret_type
))
593 error ("Java method %qD has non-Java return type %qT",
598 arg_types
= TREE_CHAIN (arg_types
);
599 if (DECL_HAS_IN_CHARGE_PARM_P (method
))
600 arg_types
= TREE_CHAIN (arg_types
);
601 if (DECL_HAS_VTT_PARM_P (method
))
602 arg_types
= TREE_CHAIN (arg_types
);
604 for (; arg_types
!= NULL_TREE
; arg_types
= TREE_CHAIN (arg_types
))
606 tree type
= TREE_VALUE (arg_types
);
607 if (!acceptable_java_type (type
))
609 if (type
!= error_mark_node
)
610 error ("Java method %qD has non-Java parameter type %qT",
618 /* Sanity check: report error if this function FUNCTION is not
619 really a member of the class (CTYPE) it is supposed to belong to.
620 TEMPLATE_PARMS is used to specify the template parameters of a member
621 template passed as FUNCTION_DECL. If the member template is passed as a
622 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
623 from the declaration. If the function is not a function template, it
625 It returns the original declaration for the function, NULL_TREE if
626 no declaration was found, error_mark_node if an error was emitted. */
629 check_classfn (tree ctype
, tree function
, tree template_parms
)
635 if (DECL_USE_TEMPLATE (function
)
636 && !(TREE_CODE (function
) == TEMPLATE_DECL
637 && DECL_TEMPLATE_SPECIALIZATION (function
))
638 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function
)))
639 /* Since this is a specialization of a member template,
640 we're not going to find the declaration in the class.
643 struct S { template <typename T> void f(T); };
644 template <> void S::f(int);
646 we're not going to find `S::f(int)', but there's no
647 reason we should, either. We let our callers know we didn't
648 find the method, but we don't complain. */
651 /* Basic sanity check: for a template function, the template parameters
652 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
653 if (TREE_CODE (function
) == TEMPLATE_DECL
)
656 && !comp_template_parms (template_parms
,
657 DECL_TEMPLATE_PARMS (function
)))
659 error ("template parameter lists provided don%'t match the "
660 "template parameters of %qD", function
);
661 return error_mark_node
;
663 template_parms
= DECL_TEMPLATE_PARMS (function
);
666 /* OK, is this a definition of a member template? */
667 is_template
= (template_parms
!= NULL_TREE
);
671 A destructor shall not be a member template. */
672 if (DECL_DESTRUCTOR_P (function
) && is_template
)
674 error ("destructor %qD declared as member template", function
);
675 return error_mark_node
;
678 /* We must enter the scope here, because conversion operators are
679 named by target type, and type equivalence relies on typenames
680 resolving within the scope of CTYPE. */
681 pushed_scope
= push_scope (ctype
);
682 ix
= class_method_index_for_fn (complete_type (ctype
), function
);
685 vec
<tree
, va_gc
> *methods
= CLASSTYPE_METHOD_VEC (ctype
);
686 tree fndecls
, fndecl
= 0;
688 const char *format
= NULL
;
690 for (fndecls
= (*methods
)[ix
];
691 fndecls
; fndecls
= OVL_NEXT (fndecls
))
695 fndecl
= OVL_CURRENT (fndecls
);
696 p1
= TYPE_ARG_TYPES (TREE_TYPE (function
));
697 p2
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
699 /* We cannot simply call decls_match because this doesn't
700 work for static member functions that are pretending to
701 be methods, and because the name may have been changed by
704 /* Get rid of the this parameter on functions that become
706 if (DECL_STATIC_FUNCTION_P (fndecl
)
707 && TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
708 p1
= TREE_CHAIN (p1
);
710 /* A member template definition only matches a member template
712 if (is_template
!= (TREE_CODE (fndecl
) == TEMPLATE_DECL
))
715 /* ref-qualifier or absence of same must match. */
716 if (type_memfn_rqual (TREE_TYPE (function
))
717 != type_memfn_rqual (TREE_TYPE (fndecl
)))
720 // Include constraints in the match.
721 tree c1
= get_constraints (function
);
722 tree c2
= get_constraints (fndecl
);
724 /* While finding a match, same types and params are not enough
725 if the function is versioned. Also check version ("target")
727 if (same_type_p (TREE_TYPE (TREE_TYPE (function
)),
728 TREE_TYPE (TREE_TYPE (fndecl
)))
729 && compparms (p1
, p2
)
730 && !targetm
.target_option
.function_versions (function
, fndecl
)
732 || comp_template_parms (template_parms
,
733 DECL_TEMPLATE_PARMS (fndecl
)))
734 && equivalent_constraints (c1
, c2
)
735 && (DECL_TEMPLATE_SPECIALIZATION (function
)
736 == DECL_TEMPLATE_SPECIALIZATION (fndecl
))
737 && (!DECL_TEMPLATE_SPECIALIZATION (function
)
738 || (DECL_TI_TEMPLATE (function
)
739 == DECL_TI_TEMPLATE (fndecl
))))
745 pop_scope (pushed_scope
);
746 return OVL_CURRENT (fndecls
);
749 error_at (DECL_SOURCE_LOCATION (function
),
750 "prototype for %q#D does not match any in class %qT",
752 is_conv_op
= DECL_CONV_FN_P (fndecl
);
755 ix
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
756 fndecls
= (*methods
)[ix
];
759 fndecl
= OVL_CURRENT (fndecls
);
760 fndecls
= OVL_NEXT (fndecls
);
762 if (!fndecls
&& is_conv_op
)
764 if (methods
->length () > (size_t) ++ix
)
766 fndecls
= (*methods
)[ix
];
767 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls
)))
779 format
= N_("candidates are: %+#D");
781 format
= N_("candidate is: %+#D");
782 error (format
, fndecl
);
785 else if (!COMPLETE_TYPE_P (ctype
))
786 cxx_incomplete_type_error (function
, ctype
);
788 error ("no %q#D member function declared in class %qT",
792 pop_scope (pushed_scope
);
793 return error_mark_node
;
796 /* DECL is a function with vague linkage. Remember it so that at the
797 end of the translation unit we can decide whether or not to emit
801 note_vague_linkage_fn (tree decl
)
803 DECL_DEFER_OUTPUT (decl
) = 1;
804 vec_safe_push (deferred_fns
, decl
);
807 /* As above, but for variable template instantiations. */
810 note_variable_template_instantiation (tree decl
)
812 vec_safe_push (pending_statics
, decl
);
815 /* We have just processed the DECL, which is a static data member.
816 The other parameters are as for cp_finish_decl. */
819 finish_static_data_member_decl (tree decl
,
820 tree init
, bool init_const_expr_p
,
824 DECL_CONTEXT (decl
) = current_class_type
;
826 /* We cannot call pushdecl here, because that would fill in the
827 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
828 the right thing, namely, to put this decl out straight away. */
830 if (! processing_template_decl
)
831 vec_safe_push (pending_statics
, decl
);
833 if (LOCAL_CLASS_P (current_class_type
)
834 /* We already complained about the template definition. */
835 && !DECL_TEMPLATE_INSTANTIATION (decl
))
836 permerror (input_location
, "local class %q#T shall not have static data member %q#D",
837 current_class_type
, decl
);
839 for (tree t
= current_class_type
; TYPE_P (t
);
840 t
= CP_TYPE_CONTEXT (t
))
841 if (TYPE_UNNAMED_P (t
))
843 if (permerror (DECL_SOURCE_LOCATION (decl
),
844 "static data member %qD in unnamed class", decl
))
845 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)),
846 "unnamed class defined here");
850 DECL_IN_AGGR_P (decl
) = 1;
852 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
853 && TYPE_DOMAIN (TREE_TYPE (decl
)) == NULL_TREE
)
854 SET_VAR_HAD_UNKNOWN_BOUND (decl
);
856 cp_finish_decl (decl
, init
, init_const_expr_p
, asmspec_tree
, flags
);
859 /* DECLARATOR and DECLSPECS correspond to a class member. The other
860 parameters are as for cp_finish_decl. Return the DECL for the
861 class member declared. */
864 grokfield (const cp_declarator
*declarator
,
865 cp_decl_specifier_seq
*declspecs
,
866 tree init
, bool init_const_expr_p
,
871 const char *asmspec
= 0;
876 && TREE_CODE (init
) == TREE_LIST
877 && TREE_VALUE (init
) == error_mark_node
878 && TREE_CHAIN (init
) == NULL_TREE
)
881 value
= grokdeclarator (declarator
, declspecs
, FIELD
, init
!= 0, &attrlist
);
882 if (! value
|| value
== error_mark_node
)
883 /* friend or constructor went bad. */
884 return error_mark_node
;
885 if (TREE_TYPE (value
) == error_mark_node
)
888 if (TREE_CODE (value
) == TYPE_DECL
&& init
)
890 error ("typedef %qD is initialized (use decltype instead)", value
);
894 /* Pass friendly classes back. */
895 if (value
== void_type_node
)
899 name
= DECL_NAME (value
);
901 if (name
!= NULL_TREE
)
903 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
905 error ("explicit template argument list not allowed");
906 return error_mark_node
;
909 if (IDENTIFIER_POINTER (name
)[0] == '_'
910 && ! strcmp (IDENTIFIER_POINTER (name
), "_vptr"))
911 error ("member %qD conflicts with virtual function table field name",
915 /* Stash away type declarations. */
916 if (TREE_CODE (value
) == TYPE_DECL
)
918 DECL_NONLOCAL (value
) = 1;
919 DECL_CONTEXT (value
) = current_class_type
;
925 /* If this is a typedef that names the class for linkage purposes
926 (7.1.3p8), apply any attributes directly to the type. */
927 if (OVERLOAD_TYPE_P (TREE_TYPE (value
))
928 && value
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
929 attrflags
= ATTR_FLAG_TYPE_IN_PLACE
;
931 cplus_decl_attributes (&value
, attrlist
, attrflags
);
934 if (decl_spec_seq_has_spec_p (declspecs
, ds_typedef
)
935 && TREE_TYPE (value
) != error_mark_node
936 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))) != value
)
937 set_underlying_type (value
);
939 /* It's important that push_template_decl below follows
940 set_underlying_type above so that the created template
941 carries the properly set type of VALUE. */
942 if (processing_template_decl
)
943 value
= push_template_decl (value
);
945 record_locally_defined_typedef (value
);
949 int friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
951 if (!friendp
&& DECL_IN_AGGR_P (value
))
953 error ("%qD is already defined in %qT", value
, DECL_CONTEXT (value
));
954 return void_type_node
;
957 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
958 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
962 if (TREE_CODE (value
) == FUNCTION_DECL
)
964 if (init
== ridpointers
[(int)RID_DELETE
])
966 DECL_DELETED_FN (value
) = 1;
967 DECL_DECLARED_INLINE_P (value
) = 1;
968 DECL_INITIAL (value
) = error_mark_node
;
970 else if (init
== ridpointers
[(int)RID_DEFAULT
])
972 if (defaultable_fn_check (value
))
974 DECL_DEFAULTED_FN (value
) = 1;
975 DECL_INITIALIZED_IN_CLASS_P (value
) = 1;
976 DECL_DECLARED_INLINE_P (value
) = 1;
979 else if (TREE_CODE (init
) == DEFAULT_ARG
)
980 error ("invalid initializer for member function %qD", value
);
981 else if (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
)
983 if (integer_zerop (init
))
984 DECL_PURE_VIRTUAL_P (value
) = 1;
985 else if (error_operand_p (init
))
986 ; /* An error has already been reported. */
988 error ("invalid initializer for member function %qD",
993 gcc_assert (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
);
995 error ("initializer specified for friend function %qD",
998 error ("initializer specified for static member function %qD",
1002 else if (TREE_CODE (value
) == FIELD_DECL
)
1003 /* C++11 NSDMI, keep going. */;
1004 else if (!VAR_P (value
))
1008 /* Pass friend decls back. */
1009 if ((TREE_CODE (value
) == FUNCTION_DECL
1010 || TREE_CODE (value
) == TEMPLATE_DECL
)
1011 && DECL_CONTEXT (value
) != current_class_type
)
1014 /* Need to set this before push_template_decl. */
1016 DECL_CONTEXT (value
) = current_class_type
;
1018 if (processing_template_decl
&& VAR_OR_FUNCTION_DECL_P (value
))
1020 value
= push_template_decl (value
);
1021 if (error_operand_p (value
))
1022 return error_mark_node
;
1026 cplus_decl_attributes (&value
, attrlist
, 0);
1028 if (init
&& DIRECT_LIST_INIT_P (init
))
1029 flags
= LOOKUP_NORMAL
;
1031 flags
= LOOKUP_IMPLICIT
;
1033 switch (TREE_CODE (value
))
1036 finish_static_data_member_decl (value
, init
, init_const_expr_p
,
1037 asmspec_tree
, flags
);
1042 error ("%<asm%> specifiers are not permitted on non-static data members");
1043 if (DECL_INITIAL (value
) == error_mark_node
)
1044 init
= error_mark_node
;
1045 cp_finish_decl (value
, init
, /*init_const_expr_p=*/false,
1047 DECL_IN_AGGR_P (value
) = 1;
1052 set_user_assembler_name (value
, asmspec
);
1054 cp_finish_decl (value
,
1056 /*init_const_expr_p=*/false,
1057 asmspec_tree
, flags
);
1059 /* Pass friends back this way. */
1060 if (DECL_FRIEND_P (value
))
1061 return void_type_node
;
1063 DECL_IN_AGGR_P (value
) = 1;
1072 /* Like `grokfield', but for bitfields.
1073 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1076 grokbitfield (const cp_declarator
*declarator
,
1077 cp_decl_specifier_seq
*declspecs
, tree width
,
1080 tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
, 0, &attrlist
);
1082 if (value
== error_mark_node
)
1083 return NULL_TREE
; /* friends went bad. */
1084 if (TREE_TYPE (value
) == error_mark_node
)
1087 /* Pass friendly classes back. */
1088 if (VOID_TYPE_P (value
))
1089 return void_type_node
;
1091 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value
))
1092 && (POINTER_TYPE_P (value
)
1093 || !dependent_type_p (TREE_TYPE (value
))))
1095 error ("bit-field %qD with non-integral type", value
);
1096 return error_mark_node
;
1099 if (TREE_CODE (value
) == TYPE_DECL
)
1101 error ("cannot declare %qD to be a bit-field type", value
);
1105 /* Usually, finish_struct_1 catches bitfields with invalid types.
1106 But, in the case of bitfields with function type, we confuse
1107 ourselves into thinking they are member functions, so we must
1109 if (TREE_CODE (value
) == FUNCTION_DECL
)
1111 error ("cannot declare bit-field %qD with function type",
1116 if (DECL_IN_AGGR_P (value
))
1118 error ("%qD is already defined in the class %qT", value
,
1119 DECL_CONTEXT (value
));
1120 return void_type_node
;
1123 if (TREE_STATIC (value
))
1125 error ("static member %qD cannot be a bit-field", value
);
1128 cp_finish_decl (value
, NULL_TREE
, false, NULL_TREE
, 0);
1130 if (width
!= error_mark_node
)
1132 /* The width must be an integer type. */
1133 if (!type_dependent_expression_p (width
)
1134 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width
)))
1135 error ("width of bit-field %qD has non-integral type %qT", value
,
1137 DECL_INITIAL (value
) = width
;
1138 SET_DECL_C_BIT_FIELD (value
);
1141 DECL_IN_AGGR_P (value
) = 1;
1144 cplus_decl_attributes (&value
, attrlist
, /*flags=*/0);
1150 /* Returns true iff ATTR is an attribute which needs to be applied at
1151 instantiation time rather than template definition time. */
1154 is_late_template_attribute (tree attr
, tree decl
)
1156 tree name
= get_attribute_name (attr
);
1157 tree args
= TREE_VALUE (attr
);
1158 const struct attribute_spec
*spec
= lookup_attribute_spec (name
);
1162 /* Unknown attribute. */
1165 /* Attribute weak handling wants to write out assembly right away. */
1166 if (is_attribute_p ("weak", name
))
1169 /* Attribute unused is applied directly, as it appertains to
1171 if (is_attribute_p ("unused", name
))
1174 /* Attribute tls_model wants to modify the symtab. */
1175 if (is_attribute_p ("tls_model", name
))
1178 /* #pragma omp declare simd attribute needs to be always deferred. */
1180 && is_attribute_p ("omp declare simd", name
))
1183 /* An attribute pack is clearly dependent. */
1184 if (args
&& PACK_EXPANSION_P (args
))
1187 /* If any of the arguments are dependent expressions, we can't evaluate
1188 the attribute until instantiation time. */
1189 for (arg
= args
; arg
; arg
= TREE_CHAIN (arg
))
1191 tree t
= TREE_VALUE (arg
);
1193 /* If the first attribute argument is an identifier, only consider
1194 second and following arguments. Attributes like mode, format,
1195 cleanup and several target specific attributes aren't late
1196 just because they have an IDENTIFIER_NODE as first argument. */
1197 if (arg
== args
&& attribute_takes_identifier_p (name
)
1198 && identifier_p (t
))
1201 if (value_dependent_expression_p (t
)
1202 || type_dependent_expression_p (t
))
1206 if (TREE_CODE (decl
) == TYPE_DECL
1208 || spec
->type_required
)
1210 tree type
= TYPE_P (decl
) ? decl
: TREE_TYPE (decl
);
1212 /* We can't apply any attributes to a completely unknown type until
1213 instantiation time. */
1214 enum tree_code code
= TREE_CODE (type
);
1215 if (code
== TEMPLATE_TYPE_PARM
1216 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
1217 || code
== TYPENAME_TYPE
)
1219 /* Also defer most attributes on dependent types. This is not
1220 necessary in all cases, but is the better default. */
1221 else if (dependent_type_p (type
)
1222 /* But some attributes specifically apply to templates. */
1223 && !is_attribute_p ("abi_tag", name
)
1224 && !is_attribute_p ("deprecated", name
)
1225 && !is_attribute_p ("visibility", name
))
1234 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1235 applied at instantiation time and return them. If IS_DEPENDENT is true,
1236 the declaration itself is dependent, so all attributes should be applied
1237 at instantiation time. */
1240 splice_template_attributes (tree
*attr_p
, tree decl
)
1243 tree late_attrs
= NULL_TREE
;
1244 tree
*q
= &late_attrs
;
1251 if (is_late_template_attribute (*p
, decl
))
1253 ATTR_IS_DEPENDENT (*p
) = 1;
1255 *p
= TREE_CHAIN (*p
);
1256 q
= &TREE_CHAIN (*q
);
1260 p
= &TREE_CHAIN (*p
);
1266 /* Remove any late attributes from the list in ATTR_P and attach them to
1270 save_template_attributes (tree
*attr_p
, tree
*decl_p
)
1274 if (attr_p
&& *attr_p
== error_mark_node
)
1277 tree late_attrs
= splice_template_attributes (attr_p
, *decl_p
);
1281 if (DECL_P (*decl_p
))
1282 q
= &DECL_ATTRIBUTES (*decl_p
);
1284 q
= &TYPE_ATTRIBUTES (*decl_p
);
1286 tree old_attrs
= *q
;
1288 /* Merge the late attributes at the beginning with the attribute
1290 late_attrs
= merge_attributes (late_attrs
, *q
);
1293 if (!DECL_P (*decl_p
) && *decl_p
== TYPE_MAIN_VARIANT (*decl_p
))
1295 /* We've added new attributes directly to the main variant, so
1296 now we need to update all of the other variants to include
1297 these new attributes. */
1299 for (variant
= TYPE_NEXT_VARIANT (*decl_p
); variant
;
1300 variant
= TYPE_NEXT_VARIANT (variant
))
1302 gcc_assert (TYPE_ATTRIBUTES (variant
) == old_attrs
);
1303 TYPE_ATTRIBUTES (variant
) = TYPE_ATTRIBUTES (*decl_p
);
1308 /* True if ATTRS contains any dependent attributes that affect type
1312 any_dependent_type_attributes_p (tree attrs
)
1314 for (tree a
= attrs
; a
; a
= TREE_CHAIN (a
))
1315 if (ATTR_IS_DEPENDENT (a
))
1317 const attribute_spec
*as
= lookup_attribute_spec (TREE_PURPOSE (a
));
1318 if (as
&& as
->affects_type_identity
)
1324 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1325 to a typedef which gives a previously unnamed class or enum a name for
1326 linkage purposes. */
1329 attributes_naming_typedef_ok (tree attrs
)
1331 for (; attrs
; attrs
= TREE_CHAIN (attrs
))
1333 tree name
= get_attribute_name (attrs
);
1334 if (is_attribute_p ("vector_size", name
))
1340 /* Like reconstruct_complex_type, but handle also template trees. */
1343 cp_reconstruct_complex_type (tree type
, tree bottom
)
1346 bool late_return_type_p
= false;
1348 if (TYPE_PTR_P (type
))
1350 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1351 outer
= build_pointer_type_for_mode (inner
, TYPE_MODE (type
),
1352 TYPE_REF_CAN_ALIAS_ALL (type
));
1354 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
1356 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1357 outer
= build_reference_type_for_mode (inner
, TYPE_MODE (type
),
1358 TYPE_REF_CAN_ALIAS_ALL (type
));
1360 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1362 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1363 outer
= build_cplus_array_type (inner
, TYPE_DOMAIN (type
));
1364 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1365 element type qualification will be handled by the recursive
1366 cp_reconstruct_complex_type call and cp_build_qualified_type
1367 for ARRAY_TYPEs changes the element type. */
1370 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
1372 late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (type
);
1373 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1374 outer
= build_function_type (inner
, TYPE_ARG_TYPES (type
));
1375 outer
= apply_memfn_quals (outer
,
1376 type_memfn_quals (type
),
1377 type_memfn_rqual (type
));
1379 else if (TREE_CODE (type
) == METHOD_TYPE
)
1381 late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (type
);
1382 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1383 /* The build_method_type_directly() routine prepends 'this' to argument list,
1384 so we must compensate by getting rid of it. */
1386 = build_method_type_directly
1387 (class_of_this_parm (type
), inner
,
1388 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
1390 else if (TREE_CODE (type
) == OFFSET_TYPE
)
1392 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1393 outer
= build_offset_type (TYPE_OFFSET_BASETYPE (type
), inner
);
1398 if (TYPE_ATTRIBUTES (type
))
1399 outer
= cp_build_type_attribute_variant (outer
, TYPE_ATTRIBUTES (type
));
1400 outer
= cp_build_qualified_type (outer
, cp_type_quals (type
));
1402 if (late_return_type_p
)
1403 TYPE_HAS_LATE_RETURN_TYPE (outer
) = 1;
1408 /* Replaces any constexpr expression that may be into the attributes
1409 arguments with their reduced value. */
1412 cp_check_const_attributes (tree attributes
)
1414 if (attributes
== error_mark_node
)
1418 for (attr
= attributes
; attr
; attr
= TREE_CHAIN (attr
))
1421 for (arg
= TREE_VALUE (attr
); arg
; arg
= TREE_CHAIN (arg
))
1423 tree expr
= TREE_VALUE (arg
);
1425 TREE_VALUE (arg
) = maybe_constant_value (expr
);
1430 /* Return true if TYPE is an OpenMP mappable type. */
1432 cp_omp_mappable_type (tree type
)
1434 /* Mappable type has to be complete. */
1435 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
1437 /* Arrays have mappable type if the elements have mappable type. */
1438 while (TREE_CODE (type
) == ARRAY_TYPE
)
1439 type
= TREE_TYPE (type
);
1440 /* A mappable type cannot contain virtual members. */
1441 if (CLASS_TYPE_P (type
) && CLASSTYPE_VTABLES (type
))
1443 /* All data members must be non-static. */
1444 if (CLASS_TYPE_P (type
))
1447 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1450 /* All fields must have mappable types. */
1451 else if (TREE_CODE (field
) == FIELD_DECL
1452 && !cp_omp_mappable_type (TREE_TYPE (field
)))
1458 /* Like decl_attributes, but handle C++ complexity. */
1461 cplus_decl_attributes (tree
*decl
, tree attributes
, int flags
)
1463 if (*decl
== NULL_TREE
|| *decl
== void_type_node
1464 || *decl
== error_mark_node
)
1467 /* Add implicit "omp declare target" attribute if requested. */
1468 if (scope_chain
->omp_declare_target_attribute
1470 && (TREE_STATIC (*decl
) || DECL_EXTERNAL (*decl
)))
1471 || TREE_CODE (*decl
) == FUNCTION_DECL
))
1474 && DECL_CLASS_SCOPE_P (*decl
))
1475 error ("%q+D static data member inside of declare target directive",
1477 else if (!processing_template_decl
1479 && !cp_omp_mappable_type (TREE_TYPE (*decl
)))
1480 error ("%q+D in declare target directive does not have mappable type",
1483 attributes
= tree_cons (get_identifier ("omp declare target"),
1484 NULL_TREE
, attributes
);
1487 if (processing_template_decl
)
1489 if (check_for_bare_parameter_packs (attributes
))
1492 save_template_attributes (&attributes
, decl
);
1495 cp_check_const_attributes (attributes
);
1497 if (TREE_CODE (*decl
) == TEMPLATE_DECL
)
1498 decl
= &DECL_TEMPLATE_RESULT (*decl
);
1500 if (TREE_TYPE (*decl
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl
)))
1503 = decl_attributes (decl
, attributes
, flags
| ATTR_FLAG_FUNCTION_NEXT
);
1504 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl
)),
1508 decl_attributes (decl
, attributes
, flags
);
1510 if (TREE_CODE (*decl
) == TYPE_DECL
)
1511 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl
), TREE_TYPE (*decl
));
1513 /* Propagate deprecation out to the template. */
1514 if (TREE_DEPRECATED (*decl
))
1515 if (tree ti
= get_template_info (*decl
))
1517 tree tmpl
= TI_TEMPLATE (ti
);
1518 tree pattern
= (TYPE_P (*decl
) ? TREE_TYPE (tmpl
)
1519 : DECL_TEMPLATE_RESULT (tmpl
));
1520 if (*decl
== pattern
)
1521 TREE_DEPRECATED (tmpl
) = true;
1525 /* Walks through the namespace- or function-scope anonymous union
1526 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1527 Returns one of the fields for use in the mangled name. */
1530 build_anon_union_vars (tree type
, tree object
)
1532 tree main_decl
= NULL_TREE
;
1535 /* Rather than write the code to handle the non-union case,
1536 just give an error. */
1537 if (TREE_CODE (type
) != UNION_TYPE
)
1539 error ("anonymous struct not inside named type");
1540 return error_mark_node
;
1543 for (field
= TYPE_FIELDS (type
);
1545 field
= DECL_CHAIN (field
))
1550 if (DECL_ARTIFICIAL (field
))
1552 if (TREE_CODE (field
) != FIELD_DECL
)
1554 permerror (DECL_SOURCE_LOCATION (field
),
1555 "%q#D invalid; an anonymous union can only "
1556 "have non-static data members", field
);
1560 if (TREE_PRIVATE (field
))
1561 permerror (DECL_SOURCE_LOCATION (field
),
1562 "private member %q#D in anonymous union", field
);
1563 else if (TREE_PROTECTED (field
))
1564 permerror (DECL_SOURCE_LOCATION (field
),
1565 "protected member %q#D in anonymous union", field
);
1567 if (processing_template_decl
)
1568 ref
= build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
, object
,
1569 DECL_NAME (field
), NULL_TREE
);
1571 ref
= build_class_member_access_expr (object
, field
, NULL_TREE
,
1572 false, tf_warning_or_error
);
1574 if (DECL_NAME (field
))
1578 decl
= build_decl (input_location
,
1579 VAR_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
1580 DECL_ANON_UNION_VAR_P (decl
) = 1;
1581 DECL_ARTIFICIAL (decl
) = 1;
1583 base
= get_base_address (object
);
1584 TREE_PUBLIC (decl
) = TREE_PUBLIC (base
);
1585 TREE_STATIC (decl
) = TREE_STATIC (base
);
1586 DECL_EXTERNAL (decl
) = DECL_EXTERNAL (base
);
1588 SET_DECL_VALUE_EXPR (decl
, ref
);
1589 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1591 decl
= pushdecl (decl
);
1593 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1594 decl
= build_anon_union_vars (TREE_TYPE (field
), ref
);
1598 if (main_decl
== NULL_TREE
)
1605 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1606 anonymous union, then all members must be laid out together. PUBLIC_P
1607 is nonzero if this union is not declared static. */
1610 finish_anon_union (tree anon_union_decl
)
1616 if (anon_union_decl
== error_mark_node
)
1619 type
= TREE_TYPE (anon_union_decl
);
1620 public_p
= TREE_PUBLIC (anon_union_decl
);
1622 /* The VAR_DECL's context is the same as the TYPE's context. */
1623 DECL_CONTEXT (anon_union_decl
) = DECL_CONTEXT (TYPE_NAME (type
));
1625 if (TYPE_FIELDS (type
) == NULL_TREE
)
1630 error ("namespace-scope anonymous aggregates must be static");
1634 main_decl
= build_anon_union_vars (type
, anon_union_decl
);
1635 if (main_decl
== error_mark_node
)
1637 if (main_decl
== NULL_TREE
)
1639 warning (0, "anonymous union with no members");
1643 if (!processing_template_decl
)
1645 /* Use main_decl to set the mangled name. */
1646 DECL_NAME (anon_union_decl
) = DECL_NAME (main_decl
);
1647 maybe_commonize_var (anon_union_decl
);
1648 if (TREE_STATIC (anon_union_decl
) || DECL_EXTERNAL (anon_union_decl
))
1649 mangle_decl (anon_union_decl
);
1650 DECL_NAME (anon_union_decl
) = NULL_TREE
;
1653 pushdecl (anon_union_decl
);
1654 cp_finish_decl (anon_union_decl
, NULL_TREE
, false, NULL_TREE
, 0);
1657 /* Auxiliary functions to make type signatures for
1658 `operator new' and `operator delete' correspond to
1659 what compiler will be expecting. */
1662 coerce_new_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
), ptr_type_node
))
1672 error ("%<operator new%> must return type %qT", ptr_type_node
);
1675 if (args
&& args
!= void_list_node
)
1677 if (TREE_PURPOSE (args
))
1679 /* [basic.stc.dynamic.allocation]
1681 The first parameter shall not have an associated default
1683 error ("the first parameter of %<operator new%> cannot "
1684 "have a default argument");
1685 /* Throw away the default argument. */
1686 TREE_PURPOSE (args
) = NULL_TREE
;
1689 if (!same_type_p (TREE_VALUE (args
), size_type_node
))
1692 args
= TREE_CHAIN (args
);
1699 permerror (input_location
, "%<operator new%> takes type %<size_t%> (%qT) "
1700 "as first parameter", size_type_node
);
1705 args
= tree_cons (NULL_TREE
, size_type_node
, args
);
1708 type
= build_exception_variant
1709 (build_function_type (ptr_type_node
, args
),
1710 TYPE_RAISES_EXCEPTIONS (type
));
1718 coerce_delete_type (tree type
)
1721 tree args
= TYPE_ARG_TYPES (type
);
1723 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1725 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
1728 error ("%<operator delete%> must return type %qT", void_type_node
);
1731 if (!args
|| args
== void_list_node
1732 || !same_type_p (TREE_VALUE (args
), ptr_type_node
))
1735 if (args
&& args
!= void_list_node
)
1736 args
= TREE_CHAIN (args
);
1737 error ("%<operator delete%> takes type %qT as first parameter",
1743 args
= tree_cons (NULL_TREE
, ptr_type_node
, args
);
1746 type
= build_exception_variant
1747 (build_function_type (void_type_node
, args
),
1748 TYPE_RAISES_EXCEPTIONS (type
));
1756 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1757 and mark them as needed. */
1760 mark_vtable_entries (tree decl
)
1763 unsigned HOST_WIDE_INT idx
;
1765 /* It's OK for the vtable to refer to deprecated virtual functions. */
1766 warning_sentinel
w(warn_deprecated_decl
);
1768 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
1773 STRIP_NOPS (fnaddr
);
1775 if (TREE_CODE (fnaddr
) != ADDR_EXPR
1776 && TREE_CODE (fnaddr
) != FDESC_EXPR
)
1777 /* This entry is an offset: a virtual base class offset, a
1778 virtual call offset, an RTTI offset, etc. */
1781 fn
= TREE_OPERAND (fnaddr
, 0);
1782 TREE_ADDRESSABLE (fn
) = 1;
1783 /* When we don't have vcall offsets, we output thunks whenever
1784 we output the vtables that contain them. With vcall offsets,
1785 we know all the thunks we'll need when we emit a virtual
1786 function, so we emit the thunks there instead. */
1787 if (DECL_THUNK_P (fn
))
1788 use_thunk (fn
, /*emit_p=*/0);
1793 /* Set DECL up to have the closest approximation of "initialized common"
1794 linkage available. */
1797 comdat_linkage (tree decl
)
1800 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1801 else if (TREE_CODE (decl
) == FUNCTION_DECL
1802 || (VAR_P (decl
) && DECL_ARTIFICIAL (decl
)))
1803 /* We can just emit function and compiler-generated variables
1804 statically; having multiple copies is (for the most part) only
1807 There are two correctness issues, however: the address of a
1808 template instantiation with external linkage should be the
1809 same, independent of what translation unit asks for the
1810 address, and this will not hold when we emit multiple copies of
1811 the function. However, there's little else we can do.
1813 Also, by default, the typeinfo implementation assumes that
1814 there will be only one copy of the string used as the name for
1815 each type. Therefore, if weak symbols are unavailable, the
1816 run-time library should perform a more conservative check; it
1817 should perform a string comparison, rather than an address
1819 TREE_PUBLIC (decl
) = 0;
1822 /* Static data member template instantiations, however, cannot
1823 have multiple copies. */
1824 if (DECL_INITIAL (decl
) == 0
1825 || DECL_INITIAL (decl
) == error_mark_node
)
1826 DECL_COMMON (decl
) = 1;
1827 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
1829 DECL_COMMON (decl
) = 1;
1830 DECL_INITIAL (decl
) = error_mark_node
;
1832 else if (!DECL_EXPLICIT_INSTANTIATION (decl
))
1834 /* We can't do anything useful; leave vars for explicit
1836 DECL_EXTERNAL (decl
) = 1;
1837 DECL_NOT_REALLY_EXTERN (decl
) = 0;
1841 if (TREE_PUBLIC (decl
))
1842 DECL_COMDAT (decl
) = 1;
1845 /* For win32 we also want to put explicit instantiations in
1846 linkonce sections, so that they will be merged with implicit
1847 instantiations; otherwise we get duplicate symbol errors.
1848 For Darwin we do not want explicit instantiations to be
1852 maybe_make_one_only (tree decl
)
1854 /* We used to say that this was not necessary on targets that support weak
1855 symbols, because the implicit instantiations will defer to the explicit
1856 one. However, that's not actually the case in SVR4; a strong definition
1857 after a weak one is an error. Also, not making explicit
1858 instantiations one_only means that we can end up with two copies of
1859 some template instantiations. */
1863 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1864 we can get away with not emitting them if they aren't used. We need
1865 to for variables so that cp_finish_decl will update their linkage,
1866 because their DECL_INITIAL may not have been set properly yet. */
1868 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1869 || (! DECL_EXPLICIT_INSTANTIATION (decl
)
1870 && ! DECL_TEMPLATE_SPECIALIZATION (decl
)))
1872 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1876 varpool_node
*node
= varpool_node::get_create (decl
);
1877 DECL_COMDAT (decl
) = 1;
1878 /* Mark it needed so we don't forget to emit it. */
1879 node
->forced_by_abi
= true;
1880 TREE_USED (decl
) = 1;
1885 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1886 This predicate will give the right answer during parsing of the
1887 function, which other tests may not. */
1890 vague_linkage_p (tree decl
)
1892 if (!TREE_PUBLIC (decl
))
1894 gcc_checking_assert (!DECL_COMDAT (decl
));
1897 /* Unfortunately, import_export_decl has not always been called
1898 before the function is processed, so we cannot simply check
1900 if (DECL_COMDAT (decl
)
1901 || (TREE_CODE (decl
) == FUNCTION_DECL
1902 && DECL_DECLARED_INLINE_P (decl
))
1903 || (DECL_LANG_SPECIFIC (decl
)
1904 && DECL_TEMPLATE_INSTANTIATION (decl
)))
1906 else if (DECL_FUNCTION_SCOPE_P (decl
))
1907 /* A local static in an inline effectively has vague linkage. */
1908 return (TREE_STATIC (decl
)
1909 && vague_linkage_p (DECL_CONTEXT (decl
)));
1914 /* Determine whether or not we want to specifically import or export CTYPE,
1915 using various heuristics. */
1918 import_export_class (tree ctype
)
1920 /* -1 for imported, 1 for exported. */
1921 int import_export
= 0;
1923 /* It only makes sense to call this function at EOF. The reason is
1924 that this function looks at whether or not the first non-inline
1925 non-abstract virtual member function has been defined in this
1926 translation unit. But, we can't possibly know that until we've
1927 seen the entire translation unit. */
1928 gcc_assert (at_eof
);
1930 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
1933 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1934 we will have CLASSTYPE_INTERFACE_ONLY set but not
1935 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1936 heuristic because someone will supply a #pragma implementation
1937 elsewhere, and deducing it here would produce a conflict. */
1938 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
1941 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
1943 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
1945 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
1946 && !flag_implicit_templates
)
1947 /* For a template class, without -fimplicit-templates, check the
1948 repository. If the virtual table is assigned to this
1949 translation unit, then export the class; otherwise, import
1951 import_export
= repo_export_class_p (ctype
) ? 1 : -1;
1952 else if (TYPE_POLYMORPHIC_P (ctype
))
1954 /* The ABI specifies that the virtual table and associated
1955 information are emitted with the key method, if any. */
1956 tree method
= CLASSTYPE_KEY_METHOD (ctype
);
1957 /* If weak symbol support is not available, then we must be
1958 careful not to emit the vtable when the key function is
1959 inline. An inline function can be defined in multiple
1960 translation units. If we were to emit the vtable in each
1961 translation unit containing a definition, we would get
1962 multiple definition errors at link-time. */
1963 if (method
&& (flag_weak
|| ! DECL_DECLARED_INLINE_P (method
)))
1964 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
1967 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1968 a definition anywhere else. */
1969 if (MULTIPLE_SYMBOL_SPACES
&& import_export
== -1)
1972 /* Allow back ends the chance to overrule the decision. */
1973 if (targetm
.cxx
.import_export_class
)
1974 import_export
= targetm
.cxx
.import_export_class (ctype
, import_export
);
1978 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
1979 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
1983 /* Return true if VAR has already been provided to the back end; in that
1984 case VAR should not be modified further by the front end. */
1986 var_finalized_p (tree var
)
1988 return varpool_node::get_create (var
)->definition
;
1991 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1992 must be emitted in this translation unit. Mark it as such. */
1995 mark_needed (tree decl
)
1997 TREE_USED (decl
) = 1;
1998 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2000 /* Extern inline functions don't become needed when referenced.
2001 If we know a method will be emitted in other TU and no new
2002 functions can be marked reachable, just use the external
2004 struct cgraph_node
*node
= cgraph_node::get_create (decl
);
2005 node
->forced_by_abi
= true;
2007 /* #pragma interface and -frepo code can call mark_needed for
2008 maybe-in-charge 'tors; mark the clones as well. */
2010 FOR_EACH_CLONE (clone
, decl
)
2011 mark_needed (clone
);
2013 else if (VAR_P (decl
))
2015 varpool_node
*node
= varpool_node::get_create (decl
);
2016 /* C++ frontend use mark_decl_references to force COMDAT variables
2017 to be output that might appear dead otherwise. */
2018 node
->forced_by_abi
= true;
2022 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2023 returns true if a definition of this entity should be provided in
2024 this object file. Callers use this function to determine whether
2025 or not to let the back end know that a definition of DECL is
2026 available in this translation unit. */
2029 decl_needed_p (tree decl
)
2031 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2032 /* This function should only be called at the end of the translation
2033 unit. We cannot be sure of whether or not something will be
2034 COMDAT until that point. */
2035 gcc_assert (at_eof
);
2037 /* All entities with external linkage that are not COMDAT/EXTERN should be
2038 emitted; they may be referred to from other object files. */
2039 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_REALLY_EXTERN (decl
))
2041 /* Functions marked "dllexport" must be emitted so that they are
2042 visible to other DLLs. */
2043 if (flag_keep_inline_dllexport
2044 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))
2047 /* When not optimizing, do not bother to produce definitions for extern
2049 if (DECL_REALLY_EXTERN (decl
)
2050 && ((TREE_CODE (decl
) != FUNCTION_DECL
2052 || (TREE_CODE (decl
) == FUNCTION_DECL
2053 && !opt_for_fn (decl
, optimize
)))
2054 && !lookup_attribute ("always_inline", decl
))
2057 /* If this entity was used, let the back end see it; it will decide
2058 whether or not to emit it into the object file. */
2059 if (TREE_USED (decl
))
2061 /* Virtual functions might be needed for devirtualization. */
2062 if (flag_devirtualize
2063 && TREE_CODE (decl
) == FUNCTION_DECL
2064 && DECL_VIRTUAL_P (decl
))
2066 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2067 reference to DECL might cause it to be emitted later. */
2071 /* If necessary, write out the vtables for the dynamic class CTYPE.
2072 Returns true if any vtables were emitted. */
2075 maybe_emit_vtables (tree ctype
)
2080 varpool_node
*current
= NULL
, *last
= NULL
;
2082 /* If the vtables for this class have already been emitted there is
2083 nothing more to do. */
2084 primary_vtbl
= CLASSTYPE_VTABLES (ctype
);
2085 if (var_finalized_p (primary_vtbl
))
2087 /* Ignore dummy vtables made by get_vtable_decl. */
2088 if (TREE_TYPE (primary_vtbl
) == void_type_node
)
2091 /* On some targets, we cannot determine the key method until the end
2092 of the translation unit -- which is when this function is
2094 if (!targetm
.cxx
.key_method_may_be_inline ())
2095 determine_key_method (ctype
);
2097 /* See if any of the vtables are needed. */
2098 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2100 import_export_decl (vtbl
);
2101 if (DECL_NOT_REALLY_EXTERN (vtbl
) && decl_needed_p (vtbl
))
2106 /* If the references to this class' vtables are optimized away,
2107 still emit the appropriate debugging information. See
2109 if (DECL_COMDAT (primary_vtbl
)
2110 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
2111 note_debug_info_needed (ctype
);
2115 /* The ABI requires that we emit all of the vtables if we emit any
2117 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2119 /* Mark entities references from the virtual table as used. */
2120 mark_vtable_entries (vtbl
);
2122 if (TREE_TYPE (DECL_INITIAL (vtbl
)) == 0)
2124 vec
<tree
, va_gc
> *cleanups
= NULL
;
2125 tree expr
= store_init_value (vtbl
, DECL_INITIAL (vtbl
), &cleanups
,
2128 /* It had better be all done at compile-time. */
2129 gcc_assert (!expr
&& !cleanups
);
2133 DECL_EXTERNAL (vtbl
) = 0;
2134 rest_of_decl_compilation (vtbl
, 1, 1);
2136 /* Because we're only doing syntax-checking, we'll never end up
2137 actually marking the variable as written. */
2138 if (flag_syntax_only
)
2139 TREE_ASM_WRITTEN (vtbl
) = 1;
2140 else if (DECL_ONE_ONLY (vtbl
))
2142 current
= varpool_node::get_create (vtbl
);
2144 current
->add_to_same_comdat_group (last
);
2149 /* Since we're writing out the vtable here, also write the debug
2151 note_debug_info_needed (ctype
);
2156 /* A special return value from type_visibility meaning internal
2159 enum { VISIBILITY_ANON
= VISIBILITY_INTERNAL
+1 };
2161 /* walk_tree helper function for type_visibility. */
2164 min_vis_r (tree
*tp
, int *walk_subtrees
, void *data
)
2166 int *vis_p
= (int *)data
;
2171 else if (OVERLOAD_TYPE_P (*tp
)
2172 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp
)))
2174 *vis_p
= VISIBILITY_ANON
;
2177 else if (CLASS_TYPE_P (*tp
)
2178 && CLASSTYPE_VISIBILITY (*tp
) > *vis_p
)
2179 *vis_p
= CLASSTYPE_VISIBILITY (*tp
);
2183 /* Returns the visibility of TYPE, which is the minimum visibility of its
2187 type_visibility (tree type
)
2189 int vis
= VISIBILITY_DEFAULT
;
2190 cp_walk_tree_without_duplicates (&type
, min_vis_r
, &vis
);
2194 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2195 specified (or if VISIBILITY is static). If TMPL is true, this
2196 constraint is for a template argument, and takes precedence
2197 over explicitly-specified visibility on the template. */
2200 constrain_visibility (tree decl
, int visibility
, bool tmpl
)
2202 if (visibility
== VISIBILITY_ANON
)
2204 /* extern "C" declarations aren't affected by the anonymous
2206 if (!DECL_EXTERN_C_P (decl
))
2208 TREE_PUBLIC (decl
) = 0;
2209 DECL_WEAK (decl
) = 0;
2210 DECL_COMMON (decl
) = 0;
2211 DECL_COMDAT (decl
) = false;
2212 if (VAR_OR_FUNCTION_DECL_P (decl
))
2214 struct symtab_node
*snode
= symtab_node::get (decl
);
2217 snode
->set_comdat_group (NULL
);
2219 DECL_INTERFACE_KNOWN (decl
) = 1;
2220 if (DECL_LANG_SPECIFIC (decl
))
2221 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2224 else if (visibility
> DECL_VISIBILITY (decl
)
2225 && (tmpl
|| !DECL_VISIBILITY_SPECIFIED (decl
)))
2227 DECL_VISIBILITY (decl
) = (enum symbol_visibility
) visibility
;
2228 /* This visibility was not specified. */
2229 DECL_VISIBILITY_SPECIFIED (decl
) = false;
2233 /* Constrain the visibility of DECL based on the visibility of its template
2237 constrain_visibility_for_template (tree decl
, tree targs
)
2239 /* If this is a template instantiation, check the innermost
2240 template args for visibility constraints. The outer template
2241 args are covered by the class check. */
2242 tree args
= INNERMOST_TEMPLATE_ARGS (targs
);
2244 for (i
= TREE_VEC_LENGTH (args
); i
> 0; --i
)
2248 tree arg
= TREE_VEC_ELT (args
, i
-1);
2250 vis
= type_visibility (arg
);
2253 if (REFERENCE_REF_P (arg
))
2254 arg
= TREE_OPERAND (arg
, 0);
2255 if (TREE_TYPE (arg
))
2257 if (TREE_CODE (arg
) == ADDR_EXPR
)
2258 arg
= TREE_OPERAND (arg
, 0);
2259 if (VAR_OR_FUNCTION_DECL_P (arg
))
2261 if (! TREE_PUBLIC (arg
))
2262 vis
= VISIBILITY_ANON
;
2264 vis
= DECL_VISIBILITY (arg
);
2268 constrain_visibility (decl
, vis
, true);
2272 /* Like c_determine_visibility, but with additional C++-specific
2275 Function-scope entities can rely on the function's visibility because
2276 it is set in start_preparsed_function.
2278 Class-scope entities cannot rely on the class's visibility until the end
2279 of the enclosing class definition.
2281 Note that because namespaces have multiple independent definitions,
2282 namespace visibility is handled elsewhere using the #pragma visibility
2283 machinery rather than by decorating the namespace declaration.
2285 The goal is for constraints from the type to give a diagnostic, and
2286 other constraints to be applied silently. */
2289 determine_visibility (tree decl
)
2291 tree class_type
= NULL_TREE
;
2293 bool orig_visibility_specified
;
2294 enum symbol_visibility orig_visibility
;
2296 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2298 /* Only relevant for names with external linkage. */
2299 if (!TREE_PUBLIC (decl
))
2302 /* Cloned constructors and destructors get the same visibility as
2303 the underlying function. That should be set up in
2304 maybe_clone_body. */
2305 gcc_assert (!DECL_CLONED_FUNCTION_P (decl
));
2307 orig_visibility_specified
= DECL_VISIBILITY_SPECIFIED (decl
);
2308 orig_visibility
= DECL_VISIBILITY (decl
);
2310 if (TREE_CODE (decl
) == TYPE_DECL
)
2312 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
2313 use_template
= CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
));
2314 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
2319 else if (DECL_LANG_SPECIFIC (decl
))
2320 use_template
= DECL_USE_TEMPLATE (decl
);
2324 /* If DECL is a member of a class, visibility specifiers on the
2325 class can influence the visibility of the DECL. */
2326 if (DECL_CLASS_SCOPE_P (decl
))
2327 class_type
= DECL_CONTEXT (decl
);
2330 /* Not a class member. */
2332 /* Virtual tables have DECL_CONTEXT set to their associated class,
2333 so they are automatically handled above. */
2334 gcc_assert (!VAR_P (decl
)
2335 || !DECL_VTABLE_OR_VTT_P (decl
));
2337 if (DECL_FUNCTION_SCOPE_P (decl
) && ! DECL_VISIBILITY_SPECIFIED (decl
))
2339 /* Local statics and classes get the visibility of their
2340 containing function by default, except that
2341 -fvisibility-inlines-hidden doesn't affect them. */
2342 tree fn
= DECL_CONTEXT (decl
);
2343 if (DECL_VISIBILITY_SPECIFIED (fn
))
2345 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2346 DECL_VISIBILITY_SPECIFIED (decl
) =
2347 DECL_VISIBILITY_SPECIFIED (fn
);
2351 if (DECL_CLASS_SCOPE_P (fn
))
2352 determine_visibility_from_class (decl
, DECL_CONTEXT (fn
));
2353 else if (determine_hidden_inline (fn
))
2355 DECL_VISIBILITY (decl
) = default_visibility
;
2356 DECL_VISIBILITY_SPECIFIED (decl
) =
2357 visibility_options
.inpragma
;
2361 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2362 DECL_VISIBILITY_SPECIFIED (decl
) =
2363 DECL_VISIBILITY_SPECIFIED (fn
);
2367 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2368 but have no TEMPLATE_INFO, so don't try to check it. */
2371 else if (VAR_P (decl
) && DECL_TINFO_P (decl
)
2372 && flag_visibility_ms_compat
)
2374 /* Under -fvisibility-ms-compat, types are visible by default,
2375 even though their contents aren't. */
2376 tree underlying_type
= TREE_TYPE (DECL_NAME (decl
));
2377 int underlying_vis
= type_visibility (underlying_type
);
2378 if (underlying_vis
== VISIBILITY_ANON
2379 || (CLASS_TYPE_P (underlying_type
)
2380 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type
)))
2381 constrain_visibility (decl
, underlying_vis
, false);
2383 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
2385 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2387 /* tinfo visibility is based on the type it's for. */
2388 constrain_visibility
2389 (decl
, type_visibility (TREE_TYPE (DECL_NAME (decl
))), false);
2391 /* Give the target a chance to override the visibility associated
2393 if (TREE_PUBLIC (decl
)
2394 && !DECL_REALLY_EXTERN (decl
)
2395 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl
)))
2396 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl
))))
2397 targetm
.cxx
.determine_class_data_visibility (decl
);
2399 else if (use_template
)
2400 /* Template instantiations and specializations get visibility based
2401 on their template unless they override it with an attribute. */;
2402 else if (! DECL_VISIBILITY_SPECIFIED (decl
))
2404 if (determine_hidden_inline (decl
))
2405 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2408 /* Set default visibility to whatever the user supplied with
2409 #pragma GCC visibility or a namespace visibility attribute. */
2410 DECL_VISIBILITY (decl
) = default_visibility
;
2411 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
2418 /* If the specialization doesn't specify visibility, use the
2419 visibility from the template. */
2420 tree tinfo
= get_template_info (decl
);
2421 tree args
= TI_ARGS (tinfo
);
2422 tree attribs
= (TREE_CODE (decl
) == TYPE_DECL
2423 ? TYPE_ATTRIBUTES (TREE_TYPE (decl
))
2424 : DECL_ATTRIBUTES (decl
));
2426 if (args
!= error_mark_node
)
2428 tree pattern
= DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo
));
2430 if (!DECL_VISIBILITY_SPECIFIED (decl
))
2432 if (!DECL_VISIBILITY_SPECIFIED (pattern
)
2433 && determine_hidden_inline (decl
))
2434 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2437 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (pattern
);
2438 DECL_VISIBILITY_SPECIFIED (decl
)
2439 = DECL_VISIBILITY_SPECIFIED (pattern
);
2444 /* Template argument visibility outweighs #pragma or namespace
2445 visibility, but not an explicit attribute. */
2446 && !lookup_attribute ("visibility", attribs
))
2448 int depth
= TMPL_ARGS_DEPTH (args
);
2449 if (DECL_VISIBILITY_SPECIFIED (decl
))
2451 /* A class template member with explicit visibility
2452 overrides the class visibility, so we need to apply
2453 all the levels of template args directly. */
2455 for (i
= 1; i
<= depth
; ++i
)
2457 tree lev
= TMPL_ARGS_LEVEL (args
, i
);
2458 constrain_visibility_for_template (decl
, lev
);
2461 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
2462 /* Limit visibility based on its template arguments. */
2463 constrain_visibility_for_template (decl
, args
);
2469 determine_visibility_from_class (decl
, class_type
);
2471 if (decl_anon_ns_mem_p (decl
))
2472 /* Names in an anonymous namespace get internal linkage.
2473 This might change once we implement export. */
2474 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2475 else if (TREE_CODE (decl
) != TYPE_DECL
)
2477 /* Propagate anonymity from type to decl. */
2478 int tvis
= type_visibility (TREE_TYPE (decl
));
2479 if (tvis
== VISIBILITY_ANON
2480 || ! DECL_VISIBILITY_SPECIFIED (decl
))
2481 constrain_visibility (decl
, tvis
, false);
2483 else if (no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/true))
2484 /* DR 757: A type without linkage shall not be used as the type of a
2485 variable or function with linkage, unless
2486 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2487 o the variable or function is not used (3.2 [basic.def.odr]) or is
2488 defined in the same translation unit.
2490 Since non-extern "C" decls need to be defined in the same
2491 translation unit, we can make the type internal. */
2492 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2494 /* If visibility changed and DECL already has DECL_RTL, ensure
2495 symbol flags are updated. */
2496 if ((DECL_VISIBILITY (decl
) != orig_visibility
2497 || DECL_VISIBILITY_SPECIFIED (decl
) != orig_visibility_specified
)
2498 && ((VAR_P (decl
) && TREE_STATIC (decl
))
2499 || TREE_CODE (decl
) == FUNCTION_DECL
)
2500 && DECL_RTL_SET_P (decl
))
2501 make_decl_rtl (decl
);
2504 /* By default, static data members and function members receive
2505 the visibility of their containing class. */
2508 determine_visibility_from_class (tree decl
, tree class_type
)
2510 if (DECL_VISIBILITY_SPECIFIED (decl
))
2513 if (determine_hidden_inline (decl
))
2514 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2517 /* Default to the class visibility. */
2518 DECL_VISIBILITY (decl
) = CLASSTYPE_VISIBILITY (class_type
);
2519 DECL_VISIBILITY_SPECIFIED (decl
)
2520 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type
);
2523 /* Give the target a chance to override the visibility associated
2526 && (DECL_TINFO_P (decl
)
2527 || (DECL_VTABLE_OR_VTT_P (decl
)
2528 /* Construction virtual tables are not exported because
2529 they cannot be referred to from other object files;
2530 their name is not standardized by the ABI. */
2531 && !DECL_CONSTRUCTION_VTABLE_P (decl
)))
2532 && TREE_PUBLIC (decl
)
2533 && !DECL_REALLY_EXTERN (decl
)
2534 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type
))
2535 targetm
.cxx
.determine_class_data_visibility (decl
);
2538 /* Returns true iff DECL is an inline that should get hidden visibility
2539 because of -fvisibility-inlines-hidden. */
2542 determine_hidden_inline (tree decl
)
2544 return (visibility_options
.inlines_hidden
2545 /* Don't do this for inline templates; specializations might not be
2546 inline, and we don't want them to inherit the hidden
2547 visibility. We'll set it here for all inline instantiations. */
2548 && !processing_template_decl
2549 && TREE_CODE (decl
) == FUNCTION_DECL
2550 && DECL_DECLARED_INLINE_P (decl
)
2551 && (! DECL_LANG_SPECIFIC (decl
)
2552 || ! DECL_EXPLICIT_INSTANTIATION (decl
)));
2555 /* Constrain the visibility of a class TYPE based on the visibility of its
2556 field types. Warn if any fields require lesser visibility. */
2559 constrain_class_visibility (tree type
)
2565 int vis
= type_visibility (type
);
2567 if (vis
== VISIBILITY_ANON
2568 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type
)))
2571 /* Don't warn about visibility if the class has explicit visibility. */
2572 if (CLASSTYPE_VISIBILITY_SPECIFIED (type
))
2573 vis
= VISIBILITY_INTERNAL
;
2575 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
2576 if (TREE_CODE (t
) == FIELD_DECL
&& TREE_TYPE (t
) != error_mark_node
)
2578 tree ftype
= strip_pointer_or_array_types (TREE_TYPE (t
));
2579 int subvis
= type_visibility (ftype
);
2581 if (subvis
== VISIBILITY_ANON
)
2583 if (!in_main_input_context())
2585 tree nlt
= no_linkage_check (ftype
, /*relaxed_p=*/false);
2588 if (same_type_p (TREE_TYPE (t
), nlt
))
2589 warning (OPT_Wsubobject_linkage
, "\
2590 %qT has a field %qD whose type has no linkage",
2593 warning (OPT_Wsubobject_linkage
, "\
2594 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2598 warning (OPT_Wsubobject_linkage
, "\
2599 %qT has a field %qD whose type uses the anonymous namespace",
2603 else if (MAYBE_CLASS_TYPE_P (ftype
)
2604 && vis
< VISIBILITY_HIDDEN
2605 && subvis
>= VISIBILITY_HIDDEN
)
2606 warning (OPT_Wattributes
, "\
2607 %qT declared with greater visibility than the type of its field %qD",
2611 binfo
= TYPE_BINFO (type
);
2612 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
2614 int subvis
= type_visibility (TREE_TYPE (t
));
2616 if (subvis
== VISIBILITY_ANON
)
2618 if (!in_main_input_context())
2620 tree nlt
= no_linkage_check (TREE_TYPE (t
), /*relaxed_p=*/false);
2623 if (same_type_p (TREE_TYPE (t
), nlt
))
2624 warning (OPT_Wsubobject_linkage
, "\
2625 %qT has a base %qT whose type has no linkage",
2626 type
, TREE_TYPE (t
));
2628 warning (OPT_Wsubobject_linkage
, "\
2629 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2630 type
, TREE_TYPE (t
), nlt
);
2633 warning (OPT_Wsubobject_linkage
, "\
2634 %qT has a base %qT whose type uses the anonymous namespace",
2635 type
, TREE_TYPE (t
));
2638 else if (vis
< VISIBILITY_HIDDEN
2639 && subvis
>= VISIBILITY_HIDDEN
)
2640 warning (OPT_Wattributes
, "\
2641 %qT declared with greater visibility than its base %qT",
2642 type
, TREE_TYPE (t
));
2646 /* Functions for adjusting the visibility of a tagged type and its nested
2647 types and declarations when it gets a name for linkage purposes from a
2650 static void bt_reset_linkage_1 (binding_entry
, void *);
2651 static void bt_reset_linkage_2 (binding_entry
, void *);
2653 /* First reset the visibility of all the types. */
2656 reset_type_linkage_1 (tree type
)
2658 set_linkage_according_to_type (type
, TYPE_MAIN_DECL (type
));
2659 if (CLASS_TYPE_P (type
))
2660 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2661 bt_reset_linkage_1
, NULL
);
2664 bt_reset_linkage_1 (binding_entry b
, void */
*data*/
)
2666 reset_type_linkage_1 (b
->type
);
2669 /* Then reset the visibility of any static data members or member
2670 functions that use those types. */
2673 reset_decl_linkage (tree decl
)
2675 if (TREE_PUBLIC (decl
))
2677 if (DECL_CLONED_FUNCTION_P (decl
))
2679 TREE_PUBLIC (decl
) = true;
2680 DECL_INTERFACE_KNOWN (decl
) = false;
2681 determine_visibility (decl
);
2682 tentative_decl_linkage (decl
);
2685 reset_type_linkage_2 (tree type
)
2687 if (CLASS_TYPE_P (type
))
2689 if (tree vt
= CLASSTYPE_VTABLES (type
))
2691 tree name
= mangle_vtbl_for_type (type
);
2692 DECL_NAME (vt
) = name
;
2693 SET_DECL_ASSEMBLER_NAME (vt
, name
);
2694 reset_decl_linkage (vt
);
2696 if (tree ti
= CLASSTYPE_TYPEINFO_VAR (type
))
2698 tree name
= mangle_typeinfo_for_type (type
);
2699 DECL_NAME (ti
) = name
;
2700 SET_DECL_ASSEMBLER_NAME (ti
, name
);
2701 TREE_TYPE (name
) = type
;
2702 reset_decl_linkage (ti
);
2704 for (tree m
= TYPE_FIELDS (type
); m
; m
= DECL_CHAIN (m
))
2706 tree mem
= STRIP_TEMPLATE (m
);
2708 reset_decl_linkage (mem
);
2710 for (tree m
= TYPE_METHODS (type
); m
; m
= DECL_CHAIN (m
))
2712 tree mem
= STRIP_TEMPLATE (m
);
2713 reset_decl_linkage (mem
);
2714 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (mem
))
2716 /* Also update its name, for cxx_dwarf_name. */
2717 DECL_NAME (mem
) = TYPE_IDENTIFIER (type
);
2719 DECL_NAME (m
) = TYPE_IDENTIFIER (type
);
2722 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2723 bt_reset_linkage_2
, NULL
);
2727 bt_reset_linkage_2 (binding_entry b
, void */
*data*/
)
2729 reset_type_linkage_2 (b
->type
);
2732 reset_type_linkage (tree type
)
2734 reset_type_linkage_1 (type
);
2735 reset_type_linkage_2 (type
);
2738 /* Set up our initial idea of what the linkage of DECL should be. */
2741 tentative_decl_linkage (tree decl
)
2743 if (DECL_INTERFACE_KNOWN (decl
))
2744 /* We've already made a decision as to how this function will
2746 else if (vague_linkage_p (decl
))
2748 if (TREE_CODE (decl
) == FUNCTION_DECL
2749 && decl_defined_p (decl
))
2751 DECL_EXTERNAL (decl
) = 1;
2752 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2753 note_vague_linkage_fn (decl
);
2754 /* A non-template inline function with external linkage will
2755 always be COMDAT. As we must eventually determine the
2756 linkage of all functions, and as that causes writes to
2757 the data mapped in from the PCH file, it's advantageous
2758 to mark the functions at this point. */
2759 if (DECL_DECLARED_INLINE_P (decl
)
2760 && (!DECL_IMPLICIT_INSTANTIATION (decl
)
2761 || DECL_DEFAULTED_FN (decl
)))
2763 /* This function must have external linkage, as
2764 otherwise DECL_INTERFACE_KNOWN would have been
2766 gcc_assert (TREE_PUBLIC (decl
));
2767 comdat_linkage (decl
);
2768 DECL_INTERFACE_KNOWN (decl
) = 1;
2771 else if (VAR_P (decl
))
2772 maybe_commonize_var (decl
);
2776 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2777 for DECL has not already been determined, do so now by setting
2778 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2779 function is called entities with vague linkage whose definitions
2780 are available must have TREE_PUBLIC set.
2782 If this function decides to place DECL in COMDAT, it will set
2783 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2784 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2785 callers defer that decision until it is clear that DECL is actually
2789 import_export_decl (tree decl
)
2794 tree class_type
= NULL_TREE
;
2796 if (DECL_INTERFACE_KNOWN (decl
))
2799 /* We cannot determine what linkage to give to an entity with vague
2800 linkage until the end of the file. For example, a virtual table
2801 for a class will be defined if and only if the key method is
2802 defined in this translation unit. As a further example, consider
2803 that when compiling a translation unit that uses PCH file with
2804 "-frepo" it would be incorrect to make decisions about what
2805 entities to emit when building the PCH; those decisions must be
2806 delayed until the repository information has been processed. */
2807 gcc_assert (at_eof
);
2808 /* Object file linkage for explicit instantiations is handled in
2809 mark_decl_instantiated. For static variables in functions with
2810 vague linkage, maybe_commonize_var is used.
2812 Therefore, the only declarations that should be provided to this
2813 function are those with external linkage that are:
2815 * implicit instantiations of function templates
2819 * implicit instantiations of static data members of class
2826 Furthermore, all entities that reach this point must have a
2827 definition available in this translation unit.
2829 The following assertions check these conditions. */
2830 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2831 /* Any code that creates entities with TREE_PUBLIC cleared should
2832 also set DECL_INTERFACE_KNOWN. */
2833 gcc_assert (TREE_PUBLIC (decl
));
2834 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2835 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2836 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
2837 || DECL_DECLARED_INLINE_P (decl
));
2839 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2840 || DECL_VTABLE_OR_VTT_P (decl
)
2841 || DECL_TINFO_P (decl
));
2842 /* Check that a definition of DECL is available in this translation
2844 gcc_assert (!DECL_REALLY_EXTERN (decl
));
2846 /* Assume that DECL will not have COMDAT linkage. */
2848 /* Assume that DECL will not be imported into this translation
2852 /* See if the repository tells us whether or not to emit DECL in
2853 this translation unit. */
2854 emit_p
= repo_emit_p (decl
);
2857 else if (emit_p
== 1)
2859 /* The repository indicates that this entity should be defined
2860 here. Make sure the back end honors that request. */
2862 /* Output the definition as an ordinary strong definition. */
2863 DECL_EXTERNAL (decl
) = 0;
2864 DECL_INTERFACE_KNOWN (decl
) = 1;
2869 /* We have already decided what to do with this DECL; there is no
2870 need to check anything further. */
2872 else if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
2874 class_type
= DECL_CONTEXT (decl
);
2875 import_export_class (class_type
);
2876 if (TYPE_FOR_JAVA (class_type
))
2878 else if (CLASSTYPE_INTERFACE_KNOWN (class_type
)
2879 && CLASSTYPE_INTERFACE_ONLY (class_type
))
2881 else if ((!flag_weak
|| TARGET_WEAK_NOT_IN_ARCHIVE_TOC
)
2882 && !CLASSTYPE_USE_TEMPLATE (class_type
)
2883 && CLASSTYPE_KEY_METHOD (class_type
)
2884 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
)))
2885 /* The ABI requires that all virtual tables be emitted with
2886 COMDAT linkage. However, on systems where COMDAT symbols
2887 don't show up in the table of contents for a static
2888 archive, or on systems without weak symbols (where we
2889 approximate COMDAT linkage by using internal linkage), the
2890 linker will report errors about undefined symbols because
2891 it will not see the virtual table definition. Therefore,
2892 in the case that we know that the virtual table will be
2893 emitted in only one translation unit, we make the virtual
2894 table an ordinary definition with external linkage. */
2895 DECL_EXTERNAL (decl
) = 0;
2896 else if (CLASSTYPE_INTERFACE_KNOWN (class_type
))
2898 /* CLASS_TYPE is being exported from this translation unit,
2899 so DECL should be defined here. */
2900 if (!flag_weak
&& CLASSTYPE_EXPLICIT_INSTANTIATION (class_type
))
2901 /* If a class is declared in a header with the "extern
2902 template" extension, then it will not be instantiated,
2903 even in translation units that would normally require
2904 it. Often such classes are explicitly instantiated in
2905 one translation unit. Therefore, the explicit
2906 instantiation must be made visible to other translation
2908 DECL_EXTERNAL (decl
) = 0;
2911 /* The generic C++ ABI says that class data is always
2912 COMDAT, even if there is a key function. Some
2913 variants (e.g., the ARM EABI) says that class data
2914 only has COMDAT linkage if the class data might be
2915 emitted in more than one translation unit. When the
2916 key method can be inline and is inline, we still have
2917 to arrange for comdat even though
2918 class_data_always_comdat is false. */
2919 if (!CLASSTYPE_KEY_METHOD (class_type
)
2920 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
))
2921 || targetm
.cxx
.class_data_always_comdat ())
2923 /* The ABI requires COMDAT linkage. Normally, we
2924 only emit COMDAT things when they are needed;
2925 make sure that we realize that this entity is
2932 else if (!flag_implicit_templates
2933 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type
))
2938 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2940 tree type
= TREE_TYPE (DECL_NAME (decl
));
2941 if (CLASS_TYPE_P (type
))
2944 import_export_class (type
);
2945 if (CLASSTYPE_INTERFACE_KNOWN (type
)
2946 && TYPE_POLYMORPHIC_P (type
)
2947 && CLASSTYPE_INTERFACE_ONLY (type
)
2948 /* If -fno-rtti was specified, then we cannot be sure
2949 that RTTI information will be emitted with the
2950 virtual table of the class, so we must emit it
2951 wherever it is used. */
2956 if (CLASSTYPE_INTERFACE_KNOWN (type
)
2957 && !CLASSTYPE_INTERFACE_ONLY (type
))
2959 comdat_p
= (targetm
.cxx
.class_data_always_comdat ()
2960 || (CLASSTYPE_KEY_METHOD (type
)
2961 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type
))));
2966 DECL_EXTERNAL (decl
) = 0;
2976 else if (DECL_TEMPLOID_INSTANTIATION (decl
))
2978 /* DECL is an implicit instantiation of a function or static
2980 if ((flag_implicit_templates
2981 && !flag_use_repository
)
2982 || (flag_implicit_inline_templates
2983 && TREE_CODE (decl
) == FUNCTION_DECL
2984 && DECL_DECLARED_INLINE_P (decl
)))
2987 /* If we are not implicitly generating templates, then mark
2988 this entity as undefined in this translation unit. */
2991 else if (DECL_FUNCTION_MEMBER_P (decl
))
2993 if (!DECL_DECLARED_INLINE_P (decl
))
2995 tree ctype
= DECL_CONTEXT (decl
);
2996 import_export_class (ctype
);
2997 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2999 DECL_NOT_REALLY_EXTERN (decl
)
3000 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
3001 || (DECL_DECLARED_INLINE_P (decl
)
3002 && ! flag_implement_inlines
3003 && !DECL_VINDEX (decl
)));
3005 if (!DECL_NOT_REALLY_EXTERN (decl
))
3006 DECL_EXTERNAL (decl
) = 1;
3008 /* Always make artificials weak. */
3009 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
3012 maybe_make_one_only (decl
);
3023 /* If we are importing DECL into this translation unit, mark is
3024 an undefined here. */
3025 DECL_EXTERNAL (decl
) = 1;
3026 DECL_NOT_REALLY_EXTERN (decl
) = 0;
3030 /* If we decided to put DECL in COMDAT, mark it accordingly at
3032 comdat_linkage (decl
);
3035 DECL_INTERFACE_KNOWN (decl
) = 1;
3038 /* Return an expression that performs the destruction of DECL, which
3039 must be a VAR_DECL whose type has a non-trivial destructor, or is
3040 an array whose (innermost) elements have a non-trivial destructor. */
3043 build_cleanup (tree decl
)
3045 tree clean
= cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
3046 gcc_assert (clean
!= NULL_TREE
);
3050 /* Returns the initialization guard variable for the variable DECL,
3051 which has static storage duration. */
3054 get_guard (tree decl
)
3059 sname
= mangle_guard_variable (decl
);
3060 guard
= IDENTIFIER_GLOBAL_VALUE (sname
);
3065 /* We use a type that is big enough to contain a mutex as well
3066 as an integer counter. */
3067 guard_type
= targetm
.cxx
.guard_type ();
3068 guard
= build_decl (DECL_SOURCE_LOCATION (decl
),
3069 VAR_DECL
, sname
, guard_type
);
3071 /* The guard should have the same linkage as what it guards. */
3072 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
3073 TREE_STATIC (guard
) = TREE_STATIC (decl
);
3074 DECL_COMMON (guard
) = DECL_COMMON (decl
);
3075 DECL_COMDAT (guard
) = DECL_COMDAT (decl
);
3076 CP_DECL_THREAD_LOCAL_P (guard
) = CP_DECL_THREAD_LOCAL_P (decl
);
3077 set_decl_tls_model (guard
, DECL_TLS_MODEL (decl
));
3078 if (DECL_ONE_ONLY (decl
))
3079 make_decl_one_only (guard
, cxx_comdat_group (guard
));
3080 if (TREE_PUBLIC (decl
))
3081 DECL_WEAK (guard
) = DECL_WEAK (decl
);
3082 DECL_VISIBILITY (guard
) = DECL_VISIBILITY (decl
);
3083 DECL_VISIBILITY_SPECIFIED (guard
) = DECL_VISIBILITY_SPECIFIED (decl
);
3085 DECL_ARTIFICIAL (guard
) = 1;
3086 DECL_IGNORED_P (guard
) = 1;
3087 TREE_USED (guard
) = 1;
3088 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
3093 /* Return an atomic load of src with the appropriate memory model. */
3096 build_atomic_load_byte (tree src
, HOST_WIDE_INT model
)
3098 tree ptr_type
= build_pointer_type (char_type_node
);
3099 tree mem_model
= build_int_cst (integer_type_node
, model
);
3104 size
= tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node
));
3106 fncode
= BUILT_IN_ATOMIC_LOAD_N
+ exact_log2 (size
) + 1;
3107 t
= builtin_decl_implicit ((enum built_in_function
) fncode
);
3109 addr
= build1 (ADDR_EXPR
, ptr_type
, src
);
3110 val
= build_call_expr (t
, 2, addr
, mem_model
);
3114 /* Return those bits of the GUARD variable that should be set when the
3115 guarded entity is actually initialized. */
3118 get_guard_bits (tree guard
)
3120 if (!targetm
.cxx
.guard_mask_bit ())
3122 /* We only set the first byte of the guard, in order to leave room
3123 for a mutex in the high-order bits. */
3124 guard
= build1 (ADDR_EXPR
,
3125 build_pointer_type (TREE_TYPE (guard
)),
3127 guard
= build1 (NOP_EXPR
,
3128 build_pointer_type (char_type_node
),
3130 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
3136 /* Return an expression which determines whether or not the GUARD
3137 variable has already been initialized. */
3140 get_guard_cond (tree guard
, bool thread_safe
)
3145 guard
= get_guard_bits (guard
);
3147 guard
= build_atomic_load_byte (guard
, MEMMODEL_ACQUIRE
);
3149 /* Mask off all but the low bit. */
3150 if (targetm
.cxx
.guard_mask_bit ())
3152 guard_value
= integer_one_node
;
3153 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3154 guard_value
= fold_convert (TREE_TYPE (guard
), guard_value
);
3155 guard
= cp_build_binary_op (input_location
,
3156 BIT_AND_EXPR
, guard
, guard_value
,
3157 tf_warning_or_error
);
3160 guard_value
= integer_zero_node
;
3161 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3162 guard_value
= fold_convert (TREE_TYPE (guard
), guard_value
);
3163 return cp_build_binary_op (input_location
,
3164 EQ_EXPR
, guard
, guard_value
,
3165 tf_warning_or_error
);
3168 /* Return an expression which sets the GUARD variable, indicating that
3169 the variable being guarded has been initialized. */
3172 set_guard (tree guard
)
3176 /* Set the GUARD to one. */
3177 guard
= get_guard_bits (guard
);
3178 guard_init
= integer_one_node
;
3179 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
3180 guard_init
= fold_convert (TREE_TYPE (guard
), guard_init
);
3181 return cp_build_modify_expr (input_location
, guard
, NOP_EXPR
, guard_init
,
3182 tf_warning_or_error
);
3185 /* Returns true iff we can tell that VAR does not have a dynamic
3189 var_defined_without_dynamic_init (tree var
)
3191 /* If it's defined in another TU, we can't tell. */
3192 if (DECL_EXTERNAL (var
))
3194 /* If it has a non-trivial destructor, registering the destructor
3195 counts as dynamic initialization. */
3196 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var
)))
3198 /* If it's in this TU, its initializer has been processed, unless
3199 it's a case of self-initialization, then DECL_INITIALIZED_P is
3200 false while the initializer is handled by finish_id_expression. */
3201 if (!DECL_INITIALIZED_P (var
))
3203 /* If it has no initializer or a constant one, it's not dynamic. */
3204 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var
)
3205 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var
));
3208 /* Returns true iff VAR is a variable that needs uses to be
3209 wrapped for possible dynamic initialization. */
3212 var_needs_tls_wrapper (tree var
)
3214 return (!error_operand_p (var
)
3215 && CP_DECL_THREAD_LOCAL_P (var
)
3216 && !DECL_GNU_TLS_P (var
)
3217 && !DECL_FUNCTION_SCOPE_P (var
)
3218 && !var_defined_without_dynamic_init (var
));
3221 /* Get the FUNCTION_DECL for the shared TLS init function for this
3222 translation unit. */
3225 get_local_tls_init_fn (void)
3227 tree sname
= get_identifier ("__tls_init");
3228 tree fn
= IDENTIFIER_GLOBAL_VALUE (sname
);
3231 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
3232 build_function_type (void_type_node
,
3234 SET_DECL_LANGUAGE (fn
, lang_c
);
3235 TREE_PUBLIC (fn
) = false;
3236 DECL_ARTIFICIAL (fn
) = true;
3238 SET_IDENTIFIER_GLOBAL_VALUE (sname
, fn
);
3243 /* Get a FUNCTION_DECL for the init function for the thread_local
3244 variable VAR. The init function will be an alias to the function
3245 that initializes all the non-local TLS variables in the translation
3246 unit. The init function is only used by the wrapper function. */
3249 get_tls_init_fn (tree var
)
3251 /* Only C++11 TLS vars need this init fn. */
3252 if (!var_needs_tls_wrapper (var
))
3255 /* If -fno-extern-tls-init, assume that we don't need to call
3256 a tls init function for a variable defined in another TU. */
3257 if (!flag_extern_tls_init
&& DECL_EXTERNAL (var
))
3260 #ifdef ASM_OUTPUT_DEF
3261 /* If the variable is internal, or if we can't generate aliases,
3262 call the local init function directly. */
3263 if (!TREE_PUBLIC (var
))
3265 return get_local_tls_init_fn ();
3267 tree sname
= mangle_tls_init_fn (var
);
3268 tree fn
= IDENTIFIER_GLOBAL_VALUE (sname
);
3271 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
3272 build_function_type (void_type_node
,
3274 SET_DECL_LANGUAGE (fn
, lang_c
);
3275 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3276 DECL_ARTIFICIAL (fn
) = true;
3277 DECL_COMDAT (fn
) = DECL_COMDAT (var
);
3278 DECL_EXTERNAL (fn
) = DECL_EXTERNAL (var
);
3279 if (DECL_ONE_ONLY (var
))
3280 make_decl_one_only (fn
, cxx_comdat_group (fn
));
3281 if (TREE_PUBLIC (var
))
3283 tree obtype
= strip_array_types (non_reference (TREE_TYPE (var
)));
3284 /* If the variable is defined somewhere else and might have static
3285 initialization, make the init function a weak reference. */
3286 if ((!TYPE_NEEDS_CONSTRUCTING (obtype
)
3287 || TYPE_HAS_CONSTEXPR_CTOR (obtype
))
3288 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype
)
3289 && DECL_EXTERNAL (var
))
3292 DECL_WEAK (fn
) = DECL_WEAK (var
);
3294 DECL_VISIBILITY (fn
) = DECL_VISIBILITY (var
);
3295 DECL_VISIBILITY_SPECIFIED (fn
) = DECL_VISIBILITY_SPECIFIED (var
);
3296 DECL_DLLIMPORT_P (fn
) = DECL_DLLIMPORT_P (var
);
3297 DECL_IGNORED_P (fn
) = 1;
3300 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3302 SET_IDENTIFIER_GLOBAL_VALUE (sname
, fn
);
3307 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3308 variable VAR. The wrapper function calls the init function (if any) for
3309 VAR and then returns a reference to VAR. The wrapper function is used
3310 in place of VAR everywhere VAR is mentioned. */
3313 get_tls_wrapper_fn (tree var
)
3315 /* Only C++11 TLS vars need this wrapper fn. */
3316 if (!var_needs_tls_wrapper (var
))
3319 tree sname
= mangle_tls_wrapper_fn (var
);
3320 tree fn
= IDENTIFIER_GLOBAL_VALUE (sname
);
3323 /* A named rvalue reference is an lvalue, so the wrapper should
3324 always return an lvalue reference. */
3325 tree type
= non_reference (TREE_TYPE (var
));
3326 type
= build_reference_type (type
);
3327 tree fntype
= build_function_type (type
, void_list_node
);
3328 fn
= build_lang_decl (FUNCTION_DECL
, sname
, fntype
);
3329 SET_DECL_LANGUAGE (fn
, lang_c
);
3330 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3331 DECL_ARTIFICIAL (fn
) = true;
3332 DECL_IGNORED_P (fn
) = 1;
3333 /* The wrapper is inline and emitted everywhere var is used. */
3334 DECL_DECLARED_INLINE_P (fn
) = true;
3335 if (TREE_PUBLIC (var
))
3337 comdat_linkage (fn
);
3338 #ifdef HAVE_GAS_HIDDEN
3339 /* Make the wrapper bind locally; there's no reason to share
3340 the wrapper between multiple shared objects. */
3341 DECL_VISIBILITY (fn
) = VISIBILITY_INTERNAL
;
3342 DECL_VISIBILITY_SPECIFIED (fn
) = true;
3345 if (!TREE_PUBLIC (fn
))
3346 DECL_INTERFACE_KNOWN (fn
) = true;
3348 note_vague_linkage_fn (fn
);
3351 /* We want CSE to commonize calls to the wrapper, but marking it as
3352 pure is unsafe since it has side-effects. I guess we need a new
3353 ECF flag even weaker than ECF_PURE. FIXME! */
3354 DECL_PURE_P (fn
) = true;
3357 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3359 SET_IDENTIFIER_GLOBAL_VALUE (sname
, fn
);
3364 /* At EOF, generate the definition for the TLS wrapper function FN:
3367 if (init_fn) init_fn();
3372 generate_tls_wrapper (tree fn
)
3374 tree var
= DECL_BEFRIENDING_CLASSES (fn
);
3376 start_preparsed_function (fn
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
3377 tree body
= begin_function_body ();
3378 /* Only call the init fn if there might be one. */
3379 if (tree init_fn
= get_tls_init_fn (var
))
3381 tree if_stmt
= NULL_TREE
;
3382 /* If init_fn is a weakref, make sure it exists before calling. */
3383 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn
)))
3385 if_stmt
= begin_if_stmt ();
3386 tree addr
= cp_build_addr_expr (init_fn
, tf_warning_or_error
);
3387 tree cond
= cp_build_binary_op (DECL_SOURCE_LOCATION (var
),
3388 NE_EXPR
, addr
, nullptr_node
,
3389 tf_warning_or_error
);
3390 finish_if_stmt_cond (cond
, if_stmt
);
3392 finish_expr_stmt (build_cxx_call
3393 (init_fn
, 0, NULL
, tf_warning_or_error
));
3396 finish_then_clause (if_stmt
);
3397 finish_if_stmt (if_stmt
);
3401 /* If there's no initialization, the wrapper is a constant function. */
3402 TREE_READONLY (fn
) = true;
3403 finish_return_stmt (convert_from_reference (var
));
3404 finish_function_body (body
);
3405 expand_or_defer_fn (finish_function (0));
3408 /* Start the process of running a particular set of global constructors
3409 or destructors. Subroutine of do_[cd]tors. Also called from
3410 vtv_start_verification_constructor_init_function. */
3413 start_objects (int method_type
, int initp
)
3419 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3421 if (initp
!= DEFAULT_INIT_PRIORITY
)
3431 sprintf (type
, "sub_%c%c%.5u", method_type
, joiner
, initp
);
3434 sprintf (type
, "sub_%c", method_type
);
3436 fndecl
= build_lang_decl (FUNCTION_DECL
,
3437 get_file_function_name (type
),
3438 build_function_type_list (void_type_node
,
3440 start_preparsed_function (fndecl
, /*attrs=*/NULL_TREE
, SF_PRE_PARSED
);
3442 TREE_PUBLIC (current_function_decl
) = 0;
3444 /* Mark as artificial because it's not explicitly in the user's
3446 DECL_ARTIFICIAL (current_function_decl
) = 1;
3448 /* Mark this declaration as used to avoid spurious warnings. */
3449 TREE_USED (current_function_decl
) = 1;
3451 /* Mark this function as a global constructor or destructor. */
3452 if (method_type
== 'I')
3453 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
3455 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
3457 body
= begin_compound_stmt (BCS_FN_BODY
);
3462 /* Finish the process of running a particular set of global constructors
3463 or destructors. Subroutine of do_[cd]tors. */
3466 finish_objects (int method_type
, int initp
, tree body
)
3471 finish_compound_stmt (body
);
3472 fn
= finish_function (0);
3474 if (method_type
== 'I')
3476 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
3477 decl_init_priority_insert (fn
, initp
);
3481 DECL_STATIC_DESTRUCTOR (fn
) = 1;
3482 decl_fini_priority_insert (fn
, initp
);
3485 expand_or_defer_fn (fn
);
3488 /* The names of the parameters to the function created to handle
3489 initializations and destructions for objects with static storage
3491 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3492 #define PRIORITY_IDENTIFIER "__priority"
3494 /* The name of the function we create to handle initializations and
3495 destructions for objects with static storage duration. */
3496 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3498 /* The declaration for the __INITIALIZE_P argument. */
3499 static GTY(()) tree initialize_p_decl
;
3501 /* The declaration for the __PRIORITY argument. */
3502 static GTY(()) tree priority_decl
;
3504 /* The declaration for the static storage duration function. */
3505 static GTY(()) tree ssdf_decl
;
3507 /* All the static storage duration functions created in this
3508 translation unit. */
3509 static GTY(()) vec
<tree
, va_gc
> *ssdf_decls
;
3511 /* A map from priority levels to information about that priority
3512 level. There may be many such levels, so efficient lookup is
3514 static splay_tree priority_info_map
;
3516 /* Begins the generation of the function that will handle all
3517 initialization and destruction of objects with static storage
3518 duration. The function generated takes two parameters of type
3519 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3520 nonzero, it performs initializations. Otherwise, it performs
3521 destructions. It only performs those initializations or
3522 destructions with the indicated __PRIORITY. The generated function
3525 It is assumed that this function will only be called once per
3526 translation unit. */
3529 start_static_storage_duration_function (unsigned count
)
3533 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
3535 /* Create the identifier for this function. It will be of the form
3536 SSDF_IDENTIFIER_<number>. */
3537 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, count
);
3539 type
= build_function_type_list (void_type_node
,
3540 integer_type_node
, integer_type_node
,
3543 /* Create the FUNCTION_DECL itself. */
3544 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
3545 get_identifier (id
),
3547 TREE_PUBLIC (ssdf_decl
) = 0;
3548 DECL_ARTIFICIAL (ssdf_decl
) = 1;
3550 /* Put this function in the list of functions to be called from the
3551 static constructors and destructors. */
3554 vec_alloc (ssdf_decls
, 32);
3556 /* Take this opportunity to initialize the map from priority
3557 numbers to information about that priority level. */
3558 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
3559 /*delete_key_fn=*/0,
3560 /*delete_value_fn=*/
3561 (splay_tree_delete_value_fn
) &free
);
3563 /* We always need to generate functions for the
3564 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3565 priorities later, we'll be sure to find the
3566 DEFAULT_INIT_PRIORITY. */
3567 get_priority_info (DEFAULT_INIT_PRIORITY
);
3570 vec_safe_push (ssdf_decls
, ssdf_decl
);
3572 /* Create the argument list. */
3573 initialize_p_decl
= cp_build_parm_decl
3574 (get_identifier (INITIALIZE_P_IDENTIFIER
), integer_type_node
);
3575 DECL_CONTEXT (initialize_p_decl
) = ssdf_decl
;
3576 TREE_USED (initialize_p_decl
) = 1;
3577 priority_decl
= cp_build_parm_decl
3578 (get_identifier (PRIORITY_IDENTIFIER
), integer_type_node
);
3579 DECL_CONTEXT (priority_decl
) = ssdf_decl
;
3580 TREE_USED (priority_decl
) = 1;
3582 DECL_CHAIN (initialize_p_decl
) = priority_decl
;
3583 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
3585 /* Put the function in the global scope. */
3586 pushdecl (ssdf_decl
);
3588 /* Start the function itself. This is equivalent to declaring the
3591 static void __ssdf (int __initialize_p, init __priority_p);
3593 It is static because we only need to call this function from the
3594 various constructor and destructor functions for this module. */
3595 start_preparsed_function (ssdf_decl
,
3596 /*attrs=*/NULL_TREE
,
3599 /* Set up the scope of the outermost block in the function. */
3600 body
= begin_compound_stmt (BCS_FN_BODY
);
3605 /* Finish the generation of the function which performs initialization
3606 and destruction of objects with static storage duration. After
3607 this point, no more such objects can be created. */
3610 finish_static_storage_duration_function (tree body
)
3612 /* Close out the function. */
3613 finish_compound_stmt (body
);
3614 expand_or_defer_fn (finish_function (0));
3617 /* Return the information about the indicated PRIORITY level. If no
3618 code to handle this level has yet been generated, generate the
3619 appropriate prologue. */
3621 static priority_info
3622 get_priority_info (int priority
)
3627 n
= splay_tree_lookup (priority_info_map
,
3628 (splay_tree_key
) priority
);
3631 /* Create a new priority information structure, and insert it
3633 pi
= XNEW (struct priority_info_s
);
3634 pi
->initializations_p
= 0;
3635 pi
->destructions_p
= 0;
3636 splay_tree_insert (priority_info_map
,
3637 (splay_tree_key
) priority
,
3638 (splay_tree_value
) pi
);
3641 pi
= (priority_info
) n
->value
;
3646 /* The effective initialization priority of a DECL. */
3648 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3649 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3650 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3652 /* Whether a DECL needs a guard to protect it against multiple
3655 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3656 || DECL_ONE_ONLY (decl) \
3657 || DECL_WEAK (decl)))
3659 /* Called from one_static_initialization_or_destruction(),
3661 Walks the initializer list of a global variable and looks for
3662 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3663 and that have their DECL_CONTEXT() == NULL.
3664 For each such temporary variable, set their DECL_CONTEXT() to
3665 the current function. This is necessary because otherwise
3666 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3667 when trying to refer to a temporary variable that does not have
3668 it's DECL_CONTECT() properly set. */
3670 fix_temporary_vars_context_r (tree
*node
,
3674 gcc_assert (current_function_decl
);
3676 if (TREE_CODE (*node
) == BIND_EXPR
)
3680 for (var
= BIND_EXPR_VARS (*node
); var
; var
= DECL_CHAIN (var
))
3683 && DECL_ARTIFICIAL (var
)
3684 && !DECL_CONTEXT (var
))
3685 DECL_CONTEXT (var
) = current_function_decl
;
3691 /* Set up to handle the initialization or destruction of DECL. If
3692 INITP is nonzero, we are initializing the variable. Otherwise, we
3693 are destroying it. */
3696 one_static_initialization_or_destruction (tree decl
, tree init
, bool initp
)
3698 tree guard_if_stmt
= NULL_TREE
;
3701 /* If we are supposed to destruct and there's a trivial destructor,
3702 nothing has to be done. */
3704 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3707 /* Trick the compiler into thinking we are at the file and line
3708 where DECL was declared so that error-messages make sense, and so
3709 that the debugger will show somewhat sensible file and line
3711 input_location
= DECL_SOURCE_LOCATION (decl
);
3713 /* Make sure temporary variables in the initialiser all have
3714 their DECL_CONTEXT() set to a value different from NULL_TREE.
3715 This can happen when global variables initialisers are built.
3716 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3717 the temporary variables that might have been generated in the
3718 accompagning initialisers is NULL_TREE, meaning the variables have been
3719 declared in the global namespace.
3720 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3721 of the temporaries are set to the current function decl. */
3722 cp_walk_tree_without_duplicates (&init
,
3723 fix_temporary_vars_context_r
,
3730 Access control for implicit calls to the constructors,
3731 the conversion functions, or the destructor called to
3732 create and destroy a static data member is performed as
3733 if these calls appeared in the scope of the member's
3736 we pretend we are in a static member function of the class of
3737 which the DECL is a member. */
3738 if (member_p (decl
))
3740 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
3741 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
3744 /* Assume we don't need a guard. */
3746 /* We need a guard if this is an object with external linkage that
3747 might be initialized in more than one place. (For example, a
3748 static data member of a template, when the data member requires
3750 if (NEEDS_GUARD_P (decl
))
3754 guard
= get_guard (decl
);
3756 /* When using __cxa_atexit, we just check the GUARD as we would
3757 for a local static. */
3758 if (flag_use_cxa_atexit
)
3760 /* When using __cxa_atexit, we never try to destroy
3761 anything from a static destructor. */
3763 guard_cond
= get_guard_cond (guard
, false);
3765 /* If we don't have __cxa_atexit, then we will be running
3766 destructors from .fini sections, or their equivalents. So,
3767 we need to know how many times we've tried to initialize this
3768 object. We do initializations only if the GUARD is zero,
3769 i.e., if we are the first to initialize the variable. We do
3770 destructions only if the GUARD is one, i.e., if we are the
3771 last to destroy the variable. */
3774 = cp_build_binary_op (input_location
,
3776 cp_build_unary_op (PREINCREMENT_EXPR
,
3779 tf_warning_or_error
),
3781 tf_warning_or_error
);
3784 = cp_build_binary_op (input_location
,
3786 cp_build_unary_op (PREDECREMENT_EXPR
,
3789 tf_warning_or_error
),
3791 tf_warning_or_error
);
3793 guard_if_stmt
= begin_if_stmt ();
3794 finish_if_stmt_cond (guard_cond
, guard_if_stmt
);
3798 /* If we're using __cxa_atexit, we have not already set the GUARD,
3799 so we must do so now. */
3800 if (guard
&& initp
&& flag_use_cxa_atexit
)
3801 finish_expr_stmt (set_guard (guard
));
3803 /* Perform the initialization or destruction. */
3808 finish_expr_stmt (init
);
3809 if (flag_sanitize
& SANITIZE_ADDRESS
)
3811 varpool_node
*vnode
= varpool_node::get (decl
);
3813 vnode
->dynamically_initialized
= 1;
3817 /* If we're using __cxa_atexit, register a function that calls the
3818 destructor for the object. */
3819 if (flag_use_cxa_atexit
)
3820 finish_expr_stmt (register_dtor_fn (decl
));
3823 finish_expr_stmt (build_cleanup (decl
));
3825 /* Finish the guard if-stmt, if necessary. */
3828 finish_then_clause (guard_if_stmt
);
3829 finish_if_stmt (guard_if_stmt
);
3832 /* Now that we're done with DECL we don't need to pretend to be a
3833 member of its class any longer. */
3834 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
3835 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
3838 /* Generate code to do the initialization or destruction of the decls in VARS,
3839 a TREE_LIST of VAR_DECL with static storage duration.
3840 Whether initialization or destruction is performed is specified by INITP. */
3843 do_static_initialization_or_destruction (tree vars
, bool initp
)
3845 tree node
, init_if_stmt
, cond
;
3847 /* Build the outer if-stmt to check for initialization or destruction. */
3848 init_if_stmt
= begin_if_stmt ();
3849 cond
= initp
? integer_one_node
: integer_zero_node
;
3850 cond
= cp_build_binary_op (input_location
,
3854 tf_warning_or_error
);
3855 finish_if_stmt_cond (cond
, init_if_stmt
);
3857 /* To make sure dynamic construction doesn't access globals from other
3858 compilation units where they might not be yet constructed, for
3859 -fsanitize=address insert __asan_before_dynamic_init call that
3860 prevents access to either all global variables that need construction
3861 in other compilation units, or at least those that haven't been
3862 initialized yet. Variables that need dynamic construction in
3863 the current compilation unit are kept accessible. */
3864 if (initp
&& (flag_sanitize
& SANITIZE_ADDRESS
))
3865 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3869 tree decl
= TREE_VALUE (node
);
3870 tree priority_if_stmt
;
3874 /* If we don't need a destructor, there's nothing to do. Avoid
3875 creating a possibly empty if-stmt. */
3876 if (!initp
&& TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3878 node
= TREE_CHAIN (node
);
3882 /* Remember that we had an initialization or finalization at this
3884 priority
= DECL_EFFECTIVE_INIT_PRIORITY (decl
);
3885 pi
= get_priority_info (priority
);
3887 pi
->initializations_p
= 1;
3889 pi
->destructions_p
= 1;
3891 /* Conditionalize this initialization on being in the right priority
3892 and being initializing/finalizing appropriately. */
3893 priority_if_stmt
= begin_if_stmt ();
3894 cond
= cp_build_binary_op (input_location
,
3897 build_int_cst (NULL_TREE
, priority
),
3898 tf_warning_or_error
);
3899 finish_if_stmt_cond (cond
, priority_if_stmt
);
3901 /* Process initializers with same priority. */
3903 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node
)) == priority
;
3904 node
= TREE_CHAIN (node
))
3905 /* Do one initialization or destruction. */
3906 one_static_initialization_or_destruction (TREE_VALUE (node
),
3907 TREE_PURPOSE (node
), initp
);
3909 /* Finish up the priority if-stmt body. */
3910 finish_then_clause (priority_if_stmt
);
3911 finish_if_stmt (priority_if_stmt
);
3915 /* Revert what __asan_before_dynamic_init did by calling
3916 __asan_after_dynamic_init. */
3917 if (initp
&& (flag_sanitize
& SANITIZE_ADDRESS
))
3918 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3920 /* Finish up the init/destruct if-stmt body. */
3921 finish_then_clause (init_if_stmt
);
3922 finish_if_stmt (init_if_stmt
);
3925 /* VARS is a list of variables with static storage duration which may
3926 need initialization and/or finalization. Remove those variables
3927 that don't really need to be initialized or finalized, and return
3928 the resulting list. The order in which the variables appear in
3929 VARS is in reverse order of the order in which they should actually
3930 be initialized. The list we return is in the unreversed order;
3931 i.e., the first variable should be initialized first. */
3934 prune_vars_needing_no_initialization (tree
*vars
)
3937 tree result
= NULL_TREE
;
3942 tree decl
= TREE_VALUE (t
);
3943 tree init
= TREE_PURPOSE (t
);
3945 /* Deal gracefully with error. */
3946 if (decl
== error_mark_node
)
3948 var
= &TREE_CHAIN (t
);
3952 /* The only things that can be initialized are variables. */
3953 gcc_assert (VAR_P (decl
));
3955 /* If this object is not defined, we don't need to do anything
3957 if (DECL_EXTERNAL (decl
))
3959 var
= &TREE_CHAIN (t
);
3963 /* Also, if the initializer already contains errors, we can bail
3965 if (init
&& TREE_CODE (init
) == TREE_LIST
3966 && value_member (error_mark_node
, init
))
3968 var
= &TREE_CHAIN (t
);
3972 /* This variable is going to need initialization and/or
3973 finalization, so we add it to the list. */
3974 *var
= TREE_CHAIN (t
);
3975 TREE_CHAIN (t
) = result
;
3982 /* Make sure we have told the back end about all the variables in
3986 write_out_vars (tree vars
)
3990 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3992 tree var
= TREE_VALUE (v
);
3993 if (!var_finalized_p (var
))
3995 import_export_decl (var
);
3996 rest_of_decl_compilation (var
, 1, 1);
4001 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4002 (otherwise) that will initialize all global objects with static
4003 storage duration having the indicated PRIORITY. */
4006 generate_ctor_or_dtor_function (bool constructor_p
, int priority
,
4014 input_location
= *locus
;
4016 /* Was: locus->line++; */
4018 /* We use `I' to indicate initialization and `D' to indicate
4020 function_key
= constructor_p
? 'I' : 'D';
4022 /* We emit the function lazily, to avoid generating empty
4023 global constructors and destructors. */
4026 /* For Objective-C++, we may need to initialize metadata found in this module.
4027 This must be done _before_ any other static initializations. */
4028 if (c_dialect_objc () && (priority
== DEFAULT_INIT_PRIORITY
)
4029 && constructor_p
&& objc_static_init_needed_p ())
4031 body
= start_objects (function_key
, priority
);
4032 objc_generate_static_init_call (NULL_TREE
);
4035 /* Call the static storage duration function with appropriate
4037 FOR_EACH_VEC_SAFE_ELT (ssdf_decls
, i
, fndecl
)
4039 /* Calls to pure or const functions will expand to nothing. */
4040 if (! (flags_from_decl_or_type (fndecl
) & (ECF_CONST
| ECF_PURE
)))
4045 body
= start_objects (function_key
, priority
);
4047 call
= cp_build_function_call_nary (fndecl
, tf_warning_or_error
,
4048 build_int_cst (NULL_TREE
,
4050 build_int_cst (NULL_TREE
,
4053 finish_expr_stmt (call
);
4057 /* Close out the function. */
4059 finish_objects (function_key
, priority
, body
);
4062 /* Generate constructor and destructor functions for the priority
4066 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n
, void * data
)
4068 location_t
*locus
= (location_t
*) data
;
4069 int priority
= (int) n
->key
;
4070 priority_info pi
= (priority_info
) n
->value
;
4072 /* Generate the functions themselves, but only if they are really
4074 if (pi
->initializations_p
)
4075 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority
, locus
);
4076 if (pi
->destructions_p
)
4077 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority
, locus
);
4079 /* Keep iterating. */
4083 /* Java requires that we be able to reference a local address for a
4084 method, and not be confused by PLT entries. If supported, create a
4085 hidden alias for all such methods. */
4088 build_java_method_aliases (void)
4090 #ifndef HAVE_GAS_HIDDEN
4094 struct cgraph_node
*node
;
4095 FOR_EACH_FUNCTION (node
)
4097 tree fndecl
= node
->decl
;
4099 if (DECL_CLASS_SCOPE_P (fndecl
)
4100 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl
))
4101 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl
))
4103 /* Mangle the name in a predictable way; we need to reference
4104 this from a java compiled object file. */
4105 tree oid
= DECL_ASSEMBLER_NAME (fndecl
);
4106 const char *oname
= IDENTIFIER_POINTER (oid
);
4107 gcc_assert (oname
[0] == '_' && oname
[1] == 'Z');
4108 char *nname
= ACONCAT (("_ZGA", oname
+ 2, NULL
));
4110 tree alias
= make_alias_for (fndecl
, get_identifier (nname
));
4111 TREE_PUBLIC (alias
) = 1;
4112 DECL_VISIBILITY (alias
) = VISIBILITY_HIDDEN
;
4114 cgraph_node::create_same_body_alias (alias
, fndecl
);
4119 /* Return C++ property of T, based on given operation OP. */
4122 cpp_check (tree t
, cpp_operation op
)
4126 case HAS_DEPENDENT_TEMPLATE_ARGS
:
4128 tree ti
= CLASSTYPE_TEMPLATE_INFO (t
);
4131 ++processing_template_decl
;
4132 const bool dep
= any_dependent_template_arguments_p (TI_ARGS (ti
));
4133 --processing_template_decl
;
4137 return DECL_PURE_VIRTUAL_P (t
);
4138 case IS_CONSTRUCTOR
:
4139 return DECL_CONSTRUCTOR_P (t
);
4141 return DECL_DESTRUCTOR_P (t
);
4142 case IS_COPY_CONSTRUCTOR
:
4143 return DECL_COPY_CONSTRUCTOR_P (t
);
4144 case IS_MOVE_CONSTRUCTOR
:
4145 return DECL_MOVE_CONSTRUCTOR_P (t
);
4147 return TREE_CODE (t
) == TEMPLATE_DECL
;
4149 return trivial_type_p (t
);
4155 /* Collect source file references recursively, starting from NAMESPC. */
4158 collect_source_refs (tree namespc
)
4165 /* Iterate over names in this name space. */
4166 for (t
= NAMESPACE_LEVEL (namespc
)->names
; t
; t
= TREE_CHAIN (t
))
4167 if (!DECL_IS_BUILTIN (t
) )
4168 collect_source_ref (DECL_SOURCE_FILE (t
));
4170 /* Dump siblings, if any */
4171 collect_source_refs (TREE_CHAIN (namespc
));
4173 /* Dump children, if any */
4174 collect_source_refs (NAMESPACE_LEVEL (namespc
)->namespaces
);
4177 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4178 starting from NAMESPC. */
4181 collect_ada_namespace (tree namespc
, const char *source_file
)
4186 /* Collect decls from this namespace */
4187 collect_ada_nodes (NAMESPACE_LEVEL (namespc
)->names
, source_file
);
4189 /* Collect siblings, if any */
4190 collect_ada_namespace (TREE_CHAIN (namespc
), source_file
);
4192 /* Collect children, if any */
4193 collect_ada_namespace (NAMESPACE_LEVEL (namespc
)->namespaces
, source_file
);
4196 /* Returns true iff there is a definition available for variable or
4200 decl_defined_p (tree decl
)
4202 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4203 return (DECL_INITIAL (decl
) != NULL_TREE
);
4206 gcc_assert (VAR_P (decl
));
4207 return !DECL_EXTERNAL (decl
);
4211 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4215 An integral constant-expression can only involve ... const
4216 variables of integral or enumeration types initialized with
4217 constant expressions ...
4219 C++0x also allows constexpr variables and temporaries initialized
4220 with constant expressions. We handle the former here, but the latter
4221 are just folded away in cxx_eval_constant_expression.
4223 The standard does not require that the expression be non-volatile.
4224 G++ implements the proposed correction in DR 457. */
4227 decl_constant_var_p (tree decl
)
4229 if (!decl_maybe_constant_var_p (decl
))
4232 /* We don't know if a template static data member is initialized with
4233 a constant expression until we instantiate its initializer. Even
4234 in the case of a constexpr variable, we can't treat it as a
4235 constant until its initializer is complete in case it's used in
4236 its own initializer. */
4237 maybe_instantiate_decl (decl
);
4238 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
);
4241 /* Returns true if DECL could be a symbolic constant variable, depending on
4245 decl_maybe_constant_var_p (tree decl
)
4247 tree type
= TREE_TYPE (decl
);
4250 if (DECL_DECLARED_CONSTEXPR_P (decl
))
4252 if (DECL_HAS_VALUE_EXPR_P (decl
))
4253 /* A proxy isn't constant. */
4255 return (CP_TYPE_CONST_NON_VOLATILE_P (type
)
4256 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
4259 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4260 called from grokfndecl and grokvardecl; in all modes it is called from
4261 cp_write_global_declarations. */
4264 no_linkage_error (tree decl
)
4266 if (cxx_dialect
>= cxx11
&& decl_defined_p (decl
))
4267 /* In C++11 it's ok if the decl is defined. */
4269 tree t
= no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false);
4271 /* The type that got us on no_linkage_decls must have gotten a name for
4272 linkage purposes. */;
4273 else if (CLASS_TYPE_P (t
) && TYPE_BEING_DEFINED (t
))
4274 /* The type might end up having a typedef name for linkage purposes. */
4275 vec_safe_push (no_linkage_decls
, decl
);
4276 else if (TYPE_UNNAMED_P (t
))
4279 if (cxx_dialect
>= cxx11
)
4280 d
= permerror (DECL_SOURCE_LOCATION (decl
), "%q#D, declared using "
4281 "unnamed type, is used but never defined", decl
);
4282 else if (DECL_EXTERN_C_P (decl
))
4283 /* Allow this; it's pretty common in C. */;
4284 else if (VAR_P (decl
))
4285 /* DRs 132, 319 and 389 seem to indicate types with
4286 no linkage can only be used to declare extern "C"
4287 entities. Since it's not always an error in the
4288 ISO C++ 90 Standard, we only issue a warning. */
4289 d
= warning_at (DECL_SOURCE_LOCATION (decl
), 0, "unnamed type "
4290 "with no linkage used to declare variable %q#D with "
4293 d
= permerror (DECL_SOURCE_LOCATION (decl
), "unnamed type with no "
4294 "linkage used to declare function %q#D with linkage",
4296 if (d
&& is_typedef_decl (TYPE_NAME (t
)))
4297 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)), "%q#D does not refer "
4298 "to the unqualified type, so it is not used for linkage",
4301 else if (cxx_dialect
>= cxx11
)
4303 if (VAR_P (decl
) || !DECL_PURE_VIRTUAL_P (decl
))
4304 permerror (DECL_SOURCE_LOCATION (decl
),
4305 "%q#D, declared using local type "
4306 "%qT, is used but never defined", decl
, t
);
4308 else if (VAR_P (decl
))
4309 warning_at (DECL_SOURCE_LOCATION (decl
), 0, "type %qT with no linkage "
4310 "used to declare variable %q#D with linkage", t
, decl
);
4312 permerror (DECL_SOURCE_LOCATION (decl
), "type %qT with no linkage used "
4313 "to declare function %q#D with linkage", t
, decl
);
4316 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4319 collect_all_refs (const char *source_file
)
4321 collect_ada_namespace (global_namespace
, source_file
);
4324 /* Clear DECL_EXTERNAL for NODE. */
4327 clear_decl_external (struct cgraph_node
*node
, void * /*data*/)
4329 DECL_EXTERNAL (node
->decl
) = 0;
4333 /* Build up the function to run dynamic initializers for thread_local
4334 variables in this translation unit and alias the init functions for the
4335 individual variables to it. */
4338 handle_tls_init (void)
4340 tree vars
= prune_vars_needing_no_initialization (&tls_aggregates
);
4341 if (vars
== NULL_TREE
)
4344 location_t loc
= DECL_SOURCE_LOCATION (TREE_VALUE (vars
));
4346 write_out_vars (vars
);
4348 tree guard
= build_decl (loc
, VAR_DECL
, get_identifier ("__tls_guard"),
4350 TREE_PUBLIC (guard
) = false;
4351 TREE_STATIC (guard
) = true;
4352 DECL_ARTIFICIAL (guard
) = true;
4353 DECL_IGNORED_P (guard
) = true;
4354 TREE_USED (guard
) = true;
4355 CP_DECL_THREAD_LOCAL_P (guard
) = true;
4356 set_decl_tls_model (guard
, decl_default_tls_model (guard
));
4357 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
4359 tree fn
= get_local_tls_init_fn ();
4360 start_preparsed_function (fn
, NULL_TREE
, SF_PRE_PARSED
);
4361 tree body
= begin_function_body ();
4362 tree if_stmt
= begin_if_stmt ();
4363 tree cond
= cp_build_unary_op (TRUTH_NOT_EXPR
, guard
, false,
4364 tf_warning_or_error
);
4365 finish_if_stmt_cond (cond
, if_stmt
);
4366 finish_expr_stmt (cp_build_modify_expr (loc
, guard
, NOP_EXPR
,
4368 tf_warning_or_error
));
4369 for (; vars
; vars
= TREE_CHAIN (vars
))
4371 tree var
= TREE_VALUE (vars
);
4372 tree init
= TREE_PURPOSE (vars
);
4373 one_static_initialization_or_destruction (var
, init
, true);
4375 #ifdef ASM_OUTPUT_DEF
4376 /* Output init aliases even with -fno-extern-tls-init. */
4377 if (TREE_PUBLIC (var
))
4379 tree single_init_fn
= get_tls_init_fn (var
);
4380 if (single_init_fn
== NULL_TREE
)
4383 = cgraph_node::get_create (fn
)->create_same_body_alias
4384 (single_init_fn
, fn
);
4385 gcc_assert (alias
!= NULL
);
4390 finish_then_clause (if_stmt
);
4391 finish_if_stmt (if_stmt
);
4392 finish_function_body (body
);
4393 expand_or_defer_fn (finish_function (0));
4396 /* We're at the end of compilation, so generate any mangling aliases that
4397 we've been saving up, if DECL is going to be output and ID2 isn't
4398 already taken by another declaration. */
4401 generate_mangling_alias (tree decl
, tree id2
)
4403 /* If there's a declaration already using this mangled name,
4404 don't create a compatibility alias that conflicts. */
4405 if (IDENTIFIER_GLOBAL_VALUE (id2
))
4408 struct cgraph_node
*n
= NULL
;
4409 if (TREE_CODE (decl
) == FUNCTION_DECL
4410 && !(n
= cgraph_node::get (decl
)))
4411 /* Don't create an alias to an unreferenced function. */
4414 tree alias
= make_alias_for (decl
, id2
);
4415 SET_IDENTIFIER_GLOBAL_VALUE (id2
, alias
);
4416 DECL_IGNORED_P (alias
) = 1;
4417 TREE_PUBLIC (alias
) = TREE_PUBLIC (decl
);
4418 DECL_VISIBILITY (alias
) = DECL_VISIBILITY (decl
);
4419 if (vague_linkage_p (decl
))
4420 DECL_WEAK (alias
) = 1;
4421 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4422 n
->create_same_body_alias (alias
, decl
);
4424 varpool_node::create_extra_name_alias (alias
, decl
);
4427 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4428 the end of translation, for compatibility across bugs in the mangling
4432 note_mangling_alias (tree decl ATTRIBUTE_UNUSED
, tree id2 ATTRIBUTE_UNUSED
)
4434 #ifdef ASM_OUTPUT_DEF
4435 if (!defer_mangling_aliases
)
4436 generate_mangling_alias (decl
, id2
);
4439 vec_safe_push (mangling_aliases
, decl
);
4440 vec_safe_push (mangling_aliases
, id2
);
4445 /* Emit all mangling aliases that were deferred up to this point. */
4448 generate_mangling_aliases ()
4450 while (!vec_safe_is_empty (mangling_aliases
))
4452 tree id2
= mangling_aliases
->pop();
4453 tree decl
= mangling_aliases
->pop();
4454 generate_mangling_alias (decl
, id2
);
4456 defer_mangling_aliases
= false;
4459 /* The entire file is now complete. If requested, dump everything
4466 FILE *stream
= dump_begin (TDI_tu
, &flags
);
4470 dump_node (global_namespace
, flags
& ~TDF_SLIM
, stream
);
4471 dump_end (TDI_tu
, stream
);
4475 static location_t locus_at_end_of_parsing
;
4477 /* Check the deallocation functions for CODE to see if we want to warn that
4478 only one was defined. */
4481 maybe_warn_sized_delete (enum tree_code code
)
4483 tree sized
= NULL_TREE
;
4484 tree unsized
= NULL_TREE
;
4486 for (tree ovl
= IDENTIFIER_GLOBAL_VALUE (ansi_opname (code
));
4487 ovl
; ovl
= OVL_NEXT (ovl
))
4489 tree fn
= OVL_CURRENT (ovl
);
4490 /* We're only interested in usual deallocation functions. */
4491 if (!usual_deallocation_fn_p (fn
))
4493 if (FUNCTION_ARG_CHAIN (fn
) == void_list_node
)
4498 if (DECL_INITIAL (unsized
) && !DECL_INITIAL (sized
))
4499 warning_at (DECL_SOURCE_LOCATION (unsized
), OPT_Wsized_deallocation
,
4500 "the program should also define %qD", sized
);
4501 else if (!DECL_INITIAL (unsized
) && DECL_INITIAL (sized
))
4502 warning_at (DECL_SOURCE_LOCATION (sized
), OPT_Wsized_deallocation
,
4503 "the program should also define %qD", unsized
);
4506 /* Check the global deallocation functions to see if we want to warn about
4507 defining unsized without sized (or vice versa). */
4510 maybe_warn_sized_delete ()
4512 if (!flag_sized_deallocation
|| !warn_sized_deallocation
)
4514 maybe_warn_sized_delete (DELETE_EXPR
);
4515 maybe_warn_sized_delete (VEC_DELETE_EXPR
);
4518 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4519 look them up when evaluating non-type template parameters. Now we need to
4520 lower them to something the back end can understand. */
4526 FOR_EACH_VARIABLE (node
)
4528 tree d
= node
->decl
;
4529 if (tree init
= DECL_INITIAL (d
))
4530 DECL_INITIAL (d
) = cplus_expand_constant (init
);
4534 /* This routine is called at the end of compilation.
4535 Its job is to create all the code needed to initialize and
4536 destroy the global aggregates. We do the destruction
4537 first, since that way we only need to reverse the decls once. */
4540 c_parse_final_cleanups (void)
4545 unsigned ssdf_count
= 0;
4549 locus_at_end_of_parsing
= input_location
;
4552 /* Bad parse errors. Just forget about it. */
4553 if (! global_bindings_p () || current_class_type
4554 || !vec_safe_is_empty (decl_namespace_list
))
4557 /* This is the point to write out a PCH if we're doing that.
4558 In that case we do not want to do anything else. */
4561 /* Mangle all symbols at PCH creation time. */
4563 FOR_EACH_SYMBOL (node
)
4564 if (! is_a
<varpool_node
*> (node
)
4565 || ! DECL_HARD_REGISTER (node
->decl
))
4566 DECL_ASSEMBLER_NAME (node
->decl
);
4567 c_common_write_pch ();
4572 timevar_stop (TV_PHASE_PARSING
);
4573 timevar_start (TV_PHASE_DEFERRED
);
4575 symtab
->process_same_body_aliases ();
4577 /* Handle -fdump-ada-spec[-slim] */
4578 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
4580 if (flag_dump_ada_spec_slim
)
4581 collect_source_ref (main_input_filename
);
4583 collect_source_refs (global_namespace
);
4585 dump_ada_specs (collect_all_refs
, cpp_check
);
4588 /* FIXME - huh? was input_line -= 1;*/
4590 /* We now have to write out all the stuff we put off writing out.
4593 o Template specializations that we have not yet instantiated,
4594 but which are needed.
4595 o Initialization and destruction for non-local objects with
4596 static storage duration. (Local objects with static storage
4597 duration are initialized when their scope is first entered,
4598 and are cleaned up via atexit.)
4599 o Virtual function tables.
4601 All of these may cause others to be needed. For example,
4602 instantiating one function may cause another to be needed, and
4603 generating the initializer for an object may cause templates to be
4604 instantiated, etc., etc. */
4606 emit_support_tinfos ();
4615 /* If there are templates that we've put off instantiating, do
4617 instantiate_pending_templates (retries
);
4620 /* Write out virtual tables as required. Note that writing out
4621 the virtual table for a template class may cause the
4622 instantiation of members of that class. If we write out
4623 vtables then we remove the class from our list so we don't
4624 have to look at it again. */
4626 while (keyed_classes
!= NULL_TREE
4627 && maybe_emit_vtables (TREE_VALUE (keyed_classes
)))
4630 keyed_classes
= TREE_CHAIN (keyed_classes
);
4636 tree next
= TREE_CHAIN (t
);
4640 if (maybe_emit_vtables (TREE_VALUE (next
)))
4643 TREE_CHAIN (t
) = TREE_CHAIN (next
);
4648 next
= TREE_CHAIN (t
);
4652 /* Write out needed type info variables. We have to be careful
4653 looping through unemitted decls, because emit_tinfo_decl may
4654 cause other variables to be needed. New elements will be
4655 appended, and we remove from the vector those that actually
4657 for (i
= unemitted_tinfo_decls
->length ();
4658 unemitted_tinfo_decls
->iterate (--i
, &t
);)
4659 if (emit_tinfo_decl (t
))
4662 unemitted_tinfo_decls
->unordered_remove (i
);
4665 /* The list of objects with static storage duration is built up
4666 in reverse order. We clear STATIC_AGGREGATES so that any new
4667 aggregates added during the initialization of these will be
4668 initialized in the correct order when we next come around the
4670 vars
= prune_vars_needing_no_initialization (&static_aggregates
);
4674 /* We need to start a new initialization function each time
4675 through the loop. That's because we need to know which
4676 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4677 isn't computed until a function is finished, and written
4678 out. That's a deficiency in the back end. When this is
4679 fixed, these initialization functions could all become
4680 inline, with resulting performance improvements. */
4683 /* Set the line and file, so that it is obviously not from
4685 input_location
= locus_at_end_of_parsing
;
4686 ssdf_body
= start_static_storage_duration_function (ssdf_count
);
4688 /* Make sure the back end knows about all the variables. */
4689 write_out_vars (vars
);
4691 /* First generate code to do all the initializations. */
4693 do_static_initialization_or_destruction (vars
, /*initp=*/true);
4695 /* Then, generate code to do all the destructions. Do these
4696 in reverse order so that the most recently constructed
4697 variable is the first destroyed. If we're using
4698 __cxa_atexit, then we don't need to do this; functions
4699 were registered at initialization time to destroy the
4701 if (!flag_use_cxa_atexit
&& vars
)
4703 vars
= nreverse (vars
);
4704 do_static_initialization_or_destruction (vars
, /*initp=*/false);
4709 /* Finish up the static storage duration function for this
4711 input_location
= locus_at_end_of_parsing
;
4712 finish_static_storage_duration_function (ssdf_body
);
4714 /* All those initializations and finalizations might cause
4715 us to need more inline functions, more template
4716 instantiations, etc. */
4719 /* ??? was: locus_at_end_of_parsing.line++; */
4722 /* Now do the same for thread_local variables. */
4725 /* Go through the set of inline functions whose bodies have not
4726 been emitted yet. If out-of-line copies of these functions
4727 are required, emit them. */
4728 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4730 /* Does it need synthesizing? */
4731 if (DECL_DEFAULTED_FN (decl
) && ! DECL_INITIAL (decl
)
4732 && (! DECL_REALLY_EXTERN (decl
) || possibly_inlined_p (decl
)))
4734 /* Even though we're already at the top-level, we push
4735 there again. That way, when we pop back a few lines
4736 hence, all of our state is restored. Otherwise,
4737 finish_function doesn't clean things up, and we end
4738 up with CURRENT_FUNCTION_DECL set. */
4739 push_to_top_level ();
4740 /* The decl's location will mark where it was first
4741 needed. Save that so synthesize method can indicate
4742 where it was needed from, in case of error */
4743 input_location
= DECL_SOURCE_LOCATION (decl
);
4744 synthesize_method (decl
);
4745 pop_from_top_level ();
4749 if (!DECL_INITIAL (decl
) && decl_tls_wrapper_p (decl
))
4750 generate_tls_wrapper (decl
);
4752 if (!DECL_SAVED_TREE (decl
))
4755 /* We lie to the back end, pretending that some functions
4756 are not defined when they really are. This keeps these
4757 functions from being put out unnecessarily. But, we must
4758 stop lying when the functions are referenced, or if they
4759 are not comdat since they need to be put out now. If
4760 DECL_INTERFACE_KNOWN, then we have already set
4761 DECL_EXTERNAL appropriately, so there's no need to check
4762 again, and we do not want to clear DECL_EXTERNAL if a
4763 previous call to import_export_decl set it.
4765 This is done in a separate for cycle, because if some
4766 deferred function is contained in another deferred
4767 function later in deferred_fns varray,
4768 rest_of_compilation would skip this function and we
4769 really cannot expand the same function twice. */
4770 import_export_decl (decl
);
4771 if (DECL_NOT_REALLY_EXTERN (decl
)
4772 && DECL_INITIAL (decl
)
4773 && decl_needed_p (decl
))
4775 struct cgraph_node
*node
, *next
;
4777 node
= cgraph_node::get (decl
);
4778 if (node
->cpp_implicit_alias
)
4779 node
= node
->get_alias_target ();
4781 node
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
4783 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4784 group, we need to mark all symbols in the same comdat group
4786 if (node
->same_comdat_group
)
4787 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
4789 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
4790 next
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
4794 /* If we're going to need to write this function out, and
4795 there's already a body for it, create RTL for it now.
4796 (There might be no body if this is a method we haven't
4797 gotten around to synthesizing yet.) */
4798 if (!DECL_EXTERNAL (decl
)
4799 && decl_needed_p (decl
)
4800 && !TREE_ASM_WRITTEN (decl
)
4801 && !cgraph_node::get (decl
)->definition
)
4803 /* We will output the function; no longer consider it in this
4805 DECL_DEFER_OUTPUT (decl
) = 0;
4806 /* Generate RTL for this function now that we know we
4808 expand_or_defer_fn (decl
);
4809 /* If we're compiling -fsyntax-only pretend that this
4810 function has been written out so that we don't try to
4812 if (flag_syntax_only
)
4813 TREE_ASM_WRITTEN (decl
) = 1;
4818 if (walk_namespaces (wrapup_globals_for_namespace
, /*data=*/0))
4821 /* Static data members are just like namespace-scope globals. */
4822 FOR_EACH_VEC_SAFE_ELT (pending_statics
, i
, decl
)
4824 if (var_finalized_p (decl
) || DECL_REALLY_EXTERN (decl
)
4825 /* Don't write it out if we haven't seen a definition. */
4826 || DECL_IN_AGGR_P (decl
))
4828 import_export_decl (decl
);
4829 /* If this static data member is needed, provide it to the
4831 if (DECL_NOT_REALLY_EXTERN (decl
) && decl_needed_p (decl
))
4832 DECL_EXTERNAL (decl
) = 0;
4834 if (vec_safe_length (pending_statics
) != 0
4835 && wrapup_global_declarations (pending_statics
->address (),
4836 pending_statics
->length ()))
4845 generate_mangling_aliases ();
4847 /* All used inline functions must have a definition at this point. */
4848 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4850 if (/* Check online inline functions that were actually used. */
4851 DECL_ODR_USED (decl
) && DECL_DECLARED_INLINE_P (decl
)
4852 /* If the definition actually was available here, then the
4853 fact that the function was not defined merely represents
4854 that for some reason (use of a template repository,
4855 #pragma interface, etc.) we decided not to emit the
4857 && !DECL_INITIAL (decl
)
4858 /* Don't complain if the template was defined. */
4859 && !(DECL_TEMPLATE_INSTANTIATION (decl
)
4860 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4861 (template_for_substitution (decl
)))))
4863 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
4864 "inline function %qD used but never defined", decl
);
4865 /* Avoid a duplicate warning from check_global_declaration. */
4866 TREE_NO_WARNING (decl
) = 1;
4870 /* So must decls that use a type with no linkage. */
4871 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls
, i
, decl
)
4872 no_linkage_error (decl
);
4874 maybe_warn_sized_delete ();
4876 /* Then, do the Objective-C stuff. This is where all the
4877 Objective-C module stuff gets generated (symtab,
4878 class/protocol/selector lists etc). This must be done after C++
4879 templates, destructors etc. so that selectors used in C++
4880 templates are properly allocated. */
4881 if (c_dialect_objc ())
4882 objc_write_global_declarations ();
4884 /* We give C linkage to static constructors and destructors. */
4885 push_lang_context (lang_name_c
);
4887 /* Generate initialization and destruction functions for all
4888 priorities for which they are required. */
4889 if (priority_info_map
)
4890 splay_tree_foreach (priority_info_map
,
4891 generate_ctor_and_dtor_functions_for_priority
,
4892 /*data=*/&locus_at_end_of_parsing
);
4893 else if (c_dialect_objc () && objc_static_init_needed_p ())
4894 /* If this is obj-c++ and we need a static init, call
4895 generate_ctor_or_dtor_function. */
4896 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4897 DEFAULT_INIT_PRIORITY
,
4898 &locus_at_end_of_parsing
);
4900 /* We're done with the splay-tree now. */
4901 if (priority_info_map
)
4902 splay_tree_delete (priority_info_map
);
4904 /* Generate any missing aliases. */
4905 maybe_apply_pending_pragma_weaks ();
4907 /* We're done with static constructors, so we can go back to "C++"
4909 pop_lang_context ();
4911 /* Generate Java hidden aliases. */
4912 build_java_method_aliases ();
4914 if (flag_vtable_verify
)
4916 vtv_recover_class_info ();
4917 vtv_compute_class_hierarchy_transitive_closure ();
4918 vtv_build_vtable_verify_fndecl ();
4921 perform_deferred_noexcept_checks ();
4927 /* The entire file is now complete. If requested, dump everything
4931 if (flag_detailed_statistics
)
4933 dump_tree_statistics ();
4934 dump_time_statistics ();
4937 timevar_stop (TV_PHASE_DEFERRED
);
4938 timevar_start (TV_PHASE_PARSING
);
4940 /* Indicate that we're done with front end processing. */
4944 /* Perform any post compilation-proper cleanups for the C++ front-end.
4945 This should really go away. No front-end should need to do
4946 anything past the compilation process. */
4949 cxx_post_compilation_parsing_cleanups (void)
4951 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS
);
4953 if (flag_vtable_verify
)
4955 /* Generate the special constructor initialization function that
4956 calls __VLTRegisterPairs, and give it a very high
4957 initialization priority. This must be done after
4958 finalize_compilation_unit so that we have accurate
4959 information about which vtable will actually be emitted. */
4960 vtv_generate_init_routine ();
4963 input_location
= locus_at_end_of_parsing
;
4966 validate_conversion_obstack ();
4968 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS
);
4971 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4972 function to call in parse-tree form; it has not yet been
4973 semantically analyzed. ARGS are the arguments to the function.
4974 They have already been semantically analyzed. This may change
4978 build_offset_ref_call_from_tree (tree fn
, vec
<tree
, va_gc
> **args
,
4979 tsubst_flags_t complain
)
4982 vec
<tree
, va_gc
> *orig_args
= NULL
;
4987 object
= TREE_OPERAND (fn
, 0);
4989 if (processing_template_decl
)
4991 gcc_assert (TREE_CODE (fn
) == DOTSTAR_EXPR
4992 || TREE_CODE (fn
) == MEMBER_REF
);
4993 if (type_dependent_expression_p (fn
)
4994 || any_type_dependent_arguments_p (*args
))
4995 return build_nt_call_vec (fn
, *args
);
4997 orig_args
= make_tree_vector_copy (*args
);
4999 /* Transform the arguments and add the implicit "this"
5000 parameter. That must be done before the FN is transformed
5001 because we depend on the form of FN. */
5002 make_args_non_dependent (*args
);
5003 object
= build_non_dependent_expr (object
);
5004 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
5006 if (TREE_CODE (fn
) == DOTSTAR_EXPR
)
5007 object
= cp_build_addr_expr (object
, complain
);
5008 vec_safe_insert (*args
, 0, object
);
5010 /* Now that the arguments are done, transform FN. */
5011 fn
= build_non_dependent_expr (fn
);
5014 /* A qualified name corresponding to a bound pointer-to-member is
5015 represented as an OFFSET_REF:
5017 struct B { void g(); };
5019 void B::g() { (this->*p)(); } */
5020 if (TREE_CODE (fn
) == OFFSET_REF
)
5022 tree object_addr
= cp_build_addr_expr (object
, complain
);
5023 fn
= TREE_OPERAND (fn
, 1);
5024 fn
= get_member_function_from_ptrfunc (&object_addr
, fn
,
5026 vec_safe_insert (*args
, 0, object_addr
);
5029 if (CLASS_TYPE_P (TREE_TYPE (fn
)))
5030 expr
= build_op_call (fn
, args
, complain
);
5032 expr
= cp_build_function_call_vec (fn
, args
, complain
);
5033 if (processing_template_decl
&& expr
!= error_mark_node
)
5034 expr
= build_min_non_dep_call_vec (expr
, orig_fn
, orig_args
);
5036 if (orig_args
!= NULL
)
5037 release_tree_vector (orig_args
);
5044 check_default_args (tree x
)
5046 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
5047 bool saw_def
= false;
5048 int i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
5049 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
5051 if (TREE_PURPOSE (arg
))
5053 else if (saw_def
&& !PACK_EXPANSION_P (TREE_VALUE (arg
)))
5055 error ("default argument missing for parameter %P of %q+#D", i
, x
);
5056 TREE_PURPOSE (arg
) = error_mark_node
;
5061 /* Return true if function DECL can be inlined. This is used to force
5062 instantiation of methods that might be interesting for inlining. */
5064 possibly_inlined_p (tree decl
)
5066 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
5067 if (DECL_UNINLINABLE (decl
))
5069 if (!optimize
|| pragma_java_exceptions
)
5070 return DECL_DECLARED_INLINE_P (decl
);
5071 /* When optimizing, we might inline everything when flatten
5072 attribute or heuristics inlining for size or autoinlining
5077 /* Normally, we can wait until instantiation-time to synthesize DECL.
5078 However, if DECL is a static data member initialized with a constant
5079 or a constexpr function, we need it right now because a reference to
5080 such a data member or a call to such function is not value-dependent.
5081 For a function that uses auto in the return type, we need to instantiate
5082 it to find out its type. For OpenMP user defined reductions, we need
5083 them instantiated for reduction clauses which inline them by hand
5087 maybe_instantiate_decl (tree decl
)
5089 if (DECL_LANG_SPECIFIC (decl
)
5090 && DECL_TEMPLATE_INFO (decl
)
5091 && (decl_maybe_constant_var_p (decl
)
5092 || (TREE_CODE (decl
) == FUNCTION_DECL
5093 && DECL_OMP_DECLARE_REDUCTION_P (decl
))
5094 || undeduced_auto_decl (decl
))
5095 && !DECL_DECLARED_CONCEPT_P (decl
)
5096 && !uses_template_parms (DECL_TI_ARGS (decl
)))
5098 /* Instantiating a function will result in garbage collection. We
5099 must treat this situation as if we were within the body of a
5100 function so as to avoid collecting live data only referenced from
5101 the stack (such as overload resolution candidates). */
5103 instantiate_decl (decl
, /*defer_ok=*/false,
5104 /*expl_inst_class_mem_p=*/false);
5109 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5110 If DECL is a specialization or implicitly declared class member,
5111 generate the actual definition. Return false if something goes
5112 wrong, true otherwise. */
5115 mark_used (tree decl
, tsubst_flags_t complain
)
5117 /* If DECL is a BASELINK for a single function, then treat it just
5118 like the DECL for the function. Otherwise, if the BASELINK is
5119 for an overloaded function, we don't know which function was
5120 actually used until after overload resolution. */
5121 if (BASELINK_P (decl
))
5123 decl
= BASELINK_FUNCTIONS (decl
);
5124 if (really_overloaded_fn (decl
))
5126 decl
= OVL_CURRENT (decl
);
5129 /* Set TREE_USED for the benefit of -Wunused. */
5130 TREE_USED (decl
) = 1;
5132 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5135 if (DECL_CLONED_FUNCTION_P (decl
))
5136 TREE_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
5138 /* Mark enumeration types as used. */
5139 if (TREE_CODE (decl
) == CONST_DECL
)
5140 used_types_insert (DECL_CONTEXT (decl
));
5142 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5143 maybe_instantiate_noexcept (decl
);
5145 if (TREE_CODE (decl
) == FUNCTION_DECL
5146 && DECL_DELETED_FN (decl
))
5148 if (DECL_ARTIFICIAL (decl
))
5150 if (DECL_OVERLOADED_OPERATOR_P (decl
) == TYPE_EXPR
5151 && LAMBDA_TYPE_P (DECL_CONTEXT (decl
)))
5153 /* We mark a lambda conversion op as deleted if we can't
5154 generate it properly; see maybe_add_lambda_conv_op. */
5155 sorry ("converting lambda which uses %<...%> to "
5156 "function pointer");
5160 if (complain
& tf_error
)
5162 error ("use of deleted function %qD", decl
);
5163 if (!maybe_explain_implicit_delete (decl
))
5164 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
5169 if (TREE_DEPRECATED (decl
) && (complain
& tf_warning
)
5170 && deprecated_state
!= DEPRECATED_SUPPRESS
)
5171 warn_deprecated_use (decl
, NULL_TREE
);
5173 /* We can only check DECL_ODR_USED on variables or functions with
5174 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5175 might need special handling for. */
5176 if (!VAR_OR_FUNCTION_DECL_P (decl
)
5177 || DECL_LANG_SPECIFIC (decl
) == NULL
5178 || DECL_THUNK_P (decl
))
5180 if (!processing_template_decl
&& type_uses_auto (TREE_TYPE (decl
)))
5182 if (complain
& tf_error
)
5183 error ("use of %qD before deduction of %<auto%>", decl
);
5189 /* We only want to do this processing once. We don't need to keep trying
5190 to instantiate inline templates, because unit-at-a-time will make sure
5191 we get them compiled before functions that want to inline them. */
5192 if (DECL_ODR_USED (decl
))
5195 /* Normally, we can wait until instantiation-time to synthesize DECL.
5196 However, if DECL is a static data member initialized with a constant
5197 or a constexpr function, we need it right now because a reference to
5198 such a data member or a call to such function is not value-dependent.
5199 For a function that uses auto in the return type, we need to instantiate
5200 it to find out its type. For OpenMP user defined reductions, we need
5201 them instantiated for reduction clauses which inline them by hand
5203 maybe_instantiate_decl (decl
);
5205 if (processing_template_decl
|| in_template_function ())
5208 /* Check this too in case we're within instantiate_non_dependent_expr. */
5209 if (DECL_TEMPLATE_INFO (decl
)
5210 && uses_template_parms (DECL_TI_ARGS (decl
)))
5213 if (undeduced_auto_decl (decl
))
5215 if (complain
& tf_error
)
5216 error ("use of %qD before deduction of %<auto%>", decl
);
5220 /* If we don't need a value, then we don't need to synthesize DECL. */
5221 if (cp_unevaluated_operand
!= 0)
5224 DECL_ODR_USED (decl
) = 1;
5225 if (DECL_CLONED_FUNCTION_P (decl
))
5226 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
5228 /* DR 757: A type without linkage shall not be used as the type of a
5229 variable or function with linkage, unless
5230 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5231 o the variable or function is not used (3.2 [basic.def.odr]) or is
5232 defined in the same translation unit. */
5233 if (cxx_dialect
> cxx98
5234 && decl_linkage (decl
) != lk_none
5235 && !DECL_EXTERN_C_P (decl
)
5236 && !DECL_ARTIFICIAL (decl
)
5237 && !decl_defined_p (decl
)
5238 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
5240 if (is_local_extern (decl
))
5241 /* There's no way to define a local extern, and adding it to
5242 the vector interferes with GC, so give an error now. */
5243 no_linkage_error (decl
);
5245 vec_safe_push (no_linkage_decls
, decl
);
5248 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (decl
)
5249 && !DECL_INITIAL (decl
) && !DECL_ARTIFICIAL (decl
))
5250 /* Remember it, so we can check it was defined. */
5251 note_vague_linkage_fn (decl
);
5253 /* Is it a synthesized method that needs to be synthesized? */
5254 if (TREE_CODE (decl
) == FUNCTION_DECL
5255 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
5256 && DECL_DEFAULTED_FN (decl
)
5257 /* A function defaulted outside the class is synthesized either by
5258 cp_finish_decl or instantiate_decl. */
5259 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl
)
5260 && ! DECL_INITIAL (decl
))
5262 /* Defer virtual destructors so that thunks get the right
5264 if (DECL_VIRTUAL_P (decl
) && !at_eof
)
5266 note_vague_linkage_fn (decl
);
5270 /* Remember the current location for a function we will end up
5271 synthesizing. Then we can inform the user where it was
5272 required in the case of error. */
5273 DECL_SOURCE_LOCATION (decl
) = input_location
;
5275 /* Synthesizing an implicitly defined member function will result in
5276 garbage collection. We must treat this situation as if we were
5277 within the body of a function so as to avoid collecting live data
5278 on the stack (such as overload resolution candidates).
5280 We could just let cp_write_global_declarations handle synthesizing
5281 this function by adding it to deferred_fns, but doing
5282 it at the use site produces better error messages. */
5284 synthesize_method (decl
);
5286 /* If this is a synthesized method we don't need to
5287 do the instantiation test below. */
5289 else if (VAR_OR_FUNCTION_DECL_P (decl
)
5290 && DECL_TEMPLATE_INFO (decl
)
5291 && !DECL_DECLARED_CONCEPT_P (decl
)
5292 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
5293 || always_instantiate_p (decl
)))
5294 /* If this is a function or variable that is an instance of some
5295 template, we now know that we will need to actually do the
5296 instantiation. We check that DECL is not an explicit
5297 instantiation because that is not checked in instantiate_decl.
5299 We put off instantiating functions in order to improve compile
5300 times. Maintaining a stack of active functions is expensive,
5301 and the inliner knows to instantiate any functions it might
5302 need. Therefore, we always try to defer instantiation. */
5305 instantiate_decl (decl
, /*defer_ok=*/true,
5306 /*expl_inst_class_mem_p=*/false);
5314 mark_used (tree decl
)
5316 return mark_used (decl
, tf_warning_or_error
);
5320 vtv_start_verification_constructor_init_function (void)
5322 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY
- 1);
5326 vtv_finish_verification_constructor_init_function (tree function_body
)
5330 finish_compound_stmt (function_body
);
5331 fn
= finish_function (0);
5332 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
5333 decl_init_priority_insert (fn
, MAX_RESERVED_INIT_PRIORITY
- 1);
5338 #include "gt-cp-decl2.h"