1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2020 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"
35 #include "c-family/c-common.h"
37 #include "stringpool.h"
41 #include "stor-layout.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
49 #include "c-family/c-ada-spec.h"
52 /* Id for dumping the raw trees. */
55 extern cpp_reader
*parse_in
;
57 /* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59 typedef struct priority_info_s
{
60 /* Nonzero if there have been any initializations at this priority
61 throughout the translation unit. */
62 int initializations_p
;
63 /* Nonzero if there have been any destructions at this priority
64 throughout the translation unit. */
68 static tree
start_objects (int, int);
69 static void finish_objects (int, int, tree
);
70 static tree
start_static_storage_duration_function (unsigned);
71 static void finish_static_storage_duration_function (tree
);
72 static priority_info
get_priority_info (int);
73 static void do_static_initialization_or_destruction (tree
, bool);
74 static void one_static_initialization_or_destruction (tree
, tree
, bool);
75 static void generate_ctor_or_dtor_function (bool, int, location_t
*);
76 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node
,
78 static tree
prune_vars_needing_no_initialization (tree
*);
79 static void write_out_vars (tree
);
80 static void import_export_class (tree
);
81 static tree
get_guard_bits (tree
);
82 static void determine_visibility_from_class (tree
, tree
);
83 static bool determine_hidden_inline (tree
);
85 /* A list of static class variables. This is needed, because a
86 static class variable can be declared inside the class without
87 an initializer, and then initialized, statically, outside the class. */
88 static GTY(()) vec
<tree
, va_gc
> *pending_statics
;
90 /* A list of functions which were declared inline, but which we
91 may need to emit outline anyway. */
92 static GTY(()) vec
<tree
, va_gc
> *deferred_fns
;
94 /* A list of decls that use types with no linkage, which we need to make
96 static GTY(()) vec
<tree
, va_gc
> *no_linkage_decls
;
98 /* A vector of alternating decls and identifiers, where the latter
99 is to be an alias for the former if the former is defined. */
100 static GTY(()) vec
<tree
, va_gc
> *mangling_aliases
;
102 /* hash traits for declarations. Hashes single decls via
103 DECL_ASSEMBLER_NAME_RAW. */
105 struct mangled_decl_hash
: ggc_remove
<tree
>
107 typedef tree value_type
; /* A DECL. */
108 typedef tree compare_type
; /* An identifier. */
110 static hashval_t
hash (const value_type decl
)
112 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl
));
114 static bool equal (const value_type existing
, compare_type candidate
)
116 tree name
= DECL_ASSEMBLER_NAME_RAW (existing
);
117 return candidate
== name
;
120 static const bool empty_zero_p
= true;
121 static inline void mark_empty (value_type
&p
) {p
= NULL_TREE
;}
122 static inline bool is_empty (value_type p
) {return !p
;}
124 static bool is_deleted (value_type e
)
126 return e
== reinterpret_cast <value_type
> (1);
128 static void mark_deleted (value_type
&e
)
130 e
= reinterpret_cast <value_type
> (1);
134 /* A hash table of decls keyed by mangled name. Used to figure out if
135 we need compatibility aliases. */
136 static GTY(()) hash_table
<mangled_decl_hash
> *mangled_decls
;
138 /* Nonzero if we're done parsing and into end-of-file activities. */
142 /* True if note_mangling_alias should enqueue mangling aliases for
143 later generation, rather than emitting them right away. */
145 bool defer_mangling_aliases
= true;
148 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
149 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
150 that apply to the function). */
153 build_memfn_type (tree fntype
, tree ctype
, cp_cv_quals quals
,
154 cp_ref_qualifier rqual
)
156 if (fntype
== error_mark_node
|| ctype
== error_mark_node
)
157 return error_mark_node
;
159 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype
));
161 cp_cv_quals type_quals
= quals
& ~TYPE_QUAL_RESTRICT
;
162 ctype
= cp_build_qualified_type (ctype
, type_quals
);
165 = build_method_type_directly (ctype
, TREE_TYPE (fntype
),
166 (TREE_CODE (fntype
) == METHOD_TYPE
167 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype
))
168 : TYPE_ARG_TYPES (fntype
)));
169 if (tree attrs
= TYPE_ATTRIBUTES (fntype
))
170 newtype
= cp_build_type_attribute_variant (newtype
, attrs
);
171 newtype
= build_cp_fntype_variant (newtype
, rqual
,
172 TYPE_RAISES_EXCEPTIONS (fntype
),
173 TYPE_HAS_LATE_RETURN_TYPE (fntype
));
178 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
179 return type changed to NEW_RET. */
182 change_return_type (tree new_ret
, tree fntype
)
184 if (new_ret
== error_mark_node
)
187 if (same_type_p (new_ret
, TREE_TYPE (fntype
)))
191 tree args
= TYPE_ARG_TYPES (fntype
);
193 if (TREE_CODE (fntype
) == FUNCTION_TYPE
)
195 newtype
= build_function_type (new_ret
, args
);
196 newtype
= apply_memfn_quals (newtype
,
197 type_memfn_quals (fntype
));
200 newtype
= build_method_type_directly
201 (class_of_this_parm (fntype
), new_ret
, TREE_CHAIN (args
));
203 if (tree attrs
= TYPE_ATTRIBUTES (fntype
))
204 newtype
= cp_build_type_attribute_variant (newtype
, attrs
);
205 newtype
= cxx_copy_lang_qualifiers (newtype
, fntype
);
210 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
214 cp_build_parm_decl (tree fn
, tree name
, tree type
)
216 tree parm
= build_decl (input_location
,
217 PARM_DECL
, name
, type
);
218 DECL_CONTEXT (parm
) = fn
;
220 /* DECL_ARG_TYPE is only used by the back end and the back end never
222 if (!processing_template_decl
)
223 DECL_ARG_TYPE (parm
) = type_passed_as (type
);
228 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
232 build_artificial_parm (tree fn
, tree name
, tree type
)
234 tree parm
= cp_build_parm_decl (fn
, name
, type
);
235 DECL_ARTIFICIAL (parm
) = 1;
236 /* All our artificial parms are implicitly `const'; they cannot be
238 TREE_READONLY (parm
) = 1;
242 /* Constructors for types with virtual baseclasses need an "in-charge" flag
243 saying whether this constructor is responsible for initialization of
244 virtual baseclasses or not. All destructors also need this "in-charge"
245 flag, which additionally determines whether or not the destructor should
246 free the memory for the object.
248 This function adds the "in-charge" flag to member function FN if
249 appropriate. It is called from grokclassfn and tsubst.
250 FN must be either a constructor or destructor.
252 The in-charge flag follows the 'this' parameter, and is followed by the
253 VTT parm (if any), then the user-written parms. */
256 maybe_retrofit_in_chrg (tree fn
)
258 tree basetype
, arg_types
, parms
, parm
, fntype
;
260 /* If we've already add the in-charge parameter don't do it again. */
261 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
264 /* When processing templates we can't know, in general, whether or
265 not we're going to have virtual baseclasses. */
266 if (processing_template_decl
)
269 /* We don't need an in-charge parameter for constructors that don't
270 have virtual bases. */
271 if (DECL_CONSTRUCTOR_P (fn
)
272 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
275 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
276 basetype
= TREE_TYPE (TREE_VALUE (arg_types
));
277 arg_types
= TREE_CHAIN (arg_types
);
279 parms
= DECL_CHAIN (DECL_ARGUMENTS (fn
));
281 /* If this is a subobject constructor or destructor, our caller will
282 pass us a pointer to our VTT. */
283 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
285 parm
= build_artificial_parm (fn
, vtt_parm_identifier
, vtt_parm_type
);
287 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
288 DECL_CHAIN (parm
) = parms
;
291 /* ...and then to TYPE_ARG_TYPES. */
292 arg_types
= hash_tree_chain (vtt_parm_type
, arg_types
);
294 DECL_HAS_VTT_PARM_P (fn
) = 1;
297 /* Then add the in-charge parm (before the VTT parm). */
298 parm
= build_artificial_parm (fn
, in_charge_identifier
, integer_type_node
);
299 DECL_CHAIN (parm
) = parms
;
301 arg_types
= hash_tree_chain (integer_type_node
, arg_types
);
303 /* Insert our new parameter(s) into the list. */
304 DECL_CHAIN (DECL_ARGUMENTS (fn
)) = parms
;
306 /* And rebuild the function type. */
307 fntype
= build_method_type_directly (basetype
, TREE_TYPE (TREE_TYPE (fn
)),
309 if (TYPE_ATTRIBUTES (TREE_TYPE (fn
)))
310 fntype
= (cp_build_type_attribute_variant
311 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (fn
))));
312 fntype
= cxx_copy_lang_qualifiers (fntype
, TREE_TYPE (fn
));
313 TREE_TYPE (fn
) = fntype
;
315 /* Now we've got the in-charge parameter. */
316 DECL_HAS_IN_CHARGE_PARM_P (fn
) = 1;
319 /* Classes overload their constituent function names automatically.
320 When a function name is declared in a record structure,
321 its name is changed to it overloaded name. Since names for
322 constructors and destructors can conflict, we place a leading
325 CNAME is the name of the class we are grokking for.
327 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
329 FLAGS contains bits saying what's special about today's
330 arguments. DTOR_FLAG == DESTRUCTOR.
332 If FUNCTION is a destructor, then we must add the `auto-delete' field
333 as a second parameter. There is some hair associated with the fact
334 that we must "declare" this variable in the manner consistent with the
335 way the rest of the arguments were declared.
337 QUALS are the qualifiers for the this pointer. */
340 grokclassfn (tree ctype
, tree function
, enum overload_flags flags
)
342 tree fn_name
= DECL_NAME (function
);
344 /* Even within an `extern "C"' block, members get C++ linkage. See
345 [dcl.link] for details. */
346 SET_DECL_LANGUAGE (function
, lang_cplusplus
);
348 if (fn_name
== NULL_TREE
)
350 error ("name missing for member function");
351 fn_name
= get_identifier ("<anonymous>");
352 DECL_NAME (function
) = fn_name
;
355 DECL_CONTEXT (function
) = ctype
;
357 if (flags
== DTOR_FLAG
)
358 DECL_CXX_DESTRUCTOR_P (function
) = 1;
360 if (flags
== DTOR_FLAG
|| DECL_CONSTRUCTOR_P (function
))
361 maybe_retrofit_in_chrg (function
);
364 /* Create an ARRAY_REF, checking for the user doing things backwards
365 along the way. DECLTYPE_P is for N3276, as in the parser. */
368 grok_array_decl (location_t loc
, tree array_expr
, tree index_exp
,
373 tree orig_array_expr
= array_expr
;
374 tree orig_index_exp
= index_exp
;
375 tree overload
= NULL_TREE
;
377 if (error_operand_p (array_expr
) || error_operand_p (index_exp
))
378 return error_mark_node
;
380 if (processing_template_decl
)
382 if (type_dependent_expression_p (array_expr
)
383 || type_dependent_expression_p (index_exp
))
384 return build_min_nt_loc (loc
, ARRAY_REF
, array_expr
, index_exp
,
385 NULL_TREE
, NULL_TREE
);
386 array_expr
= build_non_dependent_expr (array_expr
);
387 index_exp
= build_non_dependent_expr (index_exp
);
390 type
= TREE_TYPE (array_expr
);
392 type
= non_reference (type
);
394 /* If they have an `operator[]', use that. */
395 if (MAYBE_CLASS_TYPE_P (type
) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp
)))
397 tsubst_flags_t complain
= tf_warning_or_error
;
399 complain
|= tf_decltype
;
400 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, array_expr
,
401 index_exp
, NULL_TREE
, &overload
, complain
);
406 bool swapped
= false;
408 /* Otherwise, create an ARRAY_REF for a pointer or array type.
409 It is a little-known fact that, if `a' is an array and `i' is
410 an int, you can write `i[a]', which means the same thing as
412 if (TREE_CODE (type
) == ARRAY_TYPE
|| VECTOR_TYPE_P (type
))
415 p1
= build_expr_type_conversion (WANT_POINTER
, array_expr
, false);
417 if (TREE_CODE (TREE_TYPE (index_exp
)) == ARRAY_TYPE
)
420 p2
= build_expr_type_conversion (WANT_POINTER
, index_exp
, false);
422 i1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, array_expr
,
424 i2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, index_exp
,
427 if ((p1
&& i2
) && (i1
&& p2
))
428 error ("ambiguous conversion for array subscript");
431 array_expr
= p1
, index_exp
= i2
;
433 swapped
= true, array_expr
= p2
, index_exp
= i1
;
436 error_at (loc
, "invalid types %<%T[%T]%> for array subscript",
437 type
, TREE_TYPE (index_exp
));
438 return error_mark_node
;
441 if (array_expr
== error_mark_node
|| index_exp
== error_mark_node
)
442 error ("ambiguous conversion for array subscript");
444 if (TYPE_PTR_P (TREE_TYPE (array_expr
)))
445 array_expr
= mark_rvalue_use (array_expr
);
447 array_expr
= mark_lvalue_use_nonread (array_expr
);
448 index_exp
= mark_rvalue_use (index_exp
);
450 && flag_strong_eval_order
== 2
451 && (TREE_SIDE_EFFECTS (array_expr
) || TREE_SIDE_EFFECTS (index_exp
)))
452 expr
= build_array_ref (input_location
, index_exp
, array_expr
);
454 expr
= build_array_ref (input_location
, array_expr
, index_exp
);
456 if (processing_template_decl
&& expr
!= error_mark_node
)
458 if (overload
!= NULL_TREE
)
459 return (build_min_non_dep_op_overload
460 (ARRAY_REF
, expr
, overload
, orig_array_expr
, orig_index_exp
));
462 return build_min_non_dep (ARRAY_REF
, expr
, orig_array_expr
, orig_index_exp
,
463 NULL_TREE
, NULL_TREE
);
468 /* Given the cast expression EXP, checking out its validity. Either return
469 an error_mark_node if there was an unavoidable error, return a cast to
470 void for trying to delete a pointer w/ the value 0, or return the
471 call to delete. If DOING_VEC is true, we handle things differently
472 for doing an array delete.
473 Implements ARM $5.3.4. This is called from the parser. */
476 delete_sanity (location_t loc
, tree exp
, tree size
, bool doing_vec
,
477 int use_global_delete
, tsubst_flags_t complain
)
481 if (exp
== error_mark_node
)
484 if (processing_template_decl
)
486 t
= build_min (DELETE_EXPR
, void_type_node
, exp
, size
);
487 DELETE_EXPR_USE_GLOBAL (t
) = use_global_delete
;
488 DELETE_EXPR_USE_VEC (t
) = doing_vec
;
489 TREE_SIDE_EFFECTS (t
) = 1;
490 SET_EXPR_LOCATION (t
, loc
);
494 location_t exp_loc
= cp_expr_loc_or_loc (exp
, loc
);
496 /* An array can't have been allocated by new, so complain. */
497 if (TREE_CODE (TREE_TYPE (exp
)) == ARRAY_TYPE
498 && (complain
& tf_warning
))
499 warning_at (exp_loc
, 0, "deleting array %q#E", exp
);
501 t
= build_expr_type_conversion (WANT_POINTER
, exp
, true);
503 if (t
== NULL_TREE
|| t
== error_mark_node
)
505 if (complain
& tf_error
)
507 "type %q#T argument given to %<delete%>, expected pointer",
509 return error_mark_node
;
512 type
= TREE_TYPE (t
);
514 /* As of Valley Forge, you can delete a pointer to const. */
516 /* You can't delete functions. */
517 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
519 if (complain
& tf_error
)
521 "cannot delete a function. Only pointer-to-objects are "
522 "valid arguments to %<delete%>");
523 return error_mark_node
;
526 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
527 if (VOID_TYPE_P (TREE_TYPE (type
)))
529 if (complain
& tf_warning
)
530 warning_at (exp_loc
, OPT_Wdelete_incomplete
,
531 "deleting %qT is undefined", type
);
535 /* Deleting a pointer with the value zero is valid and has no effect. */
536 if (integer_zerop (t
))
537 return build1_loc (loc
, NOP_EXPR
, void_type_node
, t
);
540 return build_vec_delete (loc
, t
, /*maxindex=*/NULL_TREE
,
541 sfk_deleting_destructor
,
542 use_global_delete
, complain
);
544 return build_delete (loc
, type
, t
, sfk_deleting_destructor
,
545 LOOKUP_NORMAL
, use_global_delete
,
549 /* Report an error if the indicated template declaration is not the
550 sort of thing that should be a member template. */
553 check_member_template (tree tmpl
)
557 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
558 decl
= DECL_TEMPLATE_RESULT (tmpl
);
560 if (TREE_CODE (decl
) == FUNCTION_DECL
561 || DECL_ALIAS_TEMPLATE_P (tmpl
)
562 || (TREE_CODE (decl
) == TYPE_DECL
563 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))))
565 /* The parser rejects template declarations in local classes
566 (with the exception of generic lambdas). */
567 gcc_assert (!current_function_decl
|| LAMBDA_FUNCTION_P (decl
));
568 /* The parser rejects any use of virtual in a function template. */
569 gcc_assert (!(TREE_CODE (decl
) == FUNCTION_DECL
570 && DECL_VIRTUAL_P (decl
)));
572 /* The debug-information generating code doesn't know what to do
573 with member templates. */
574 DECL_IGNORED_P (tmpl
) = 1;
576 else if (variable_template_p (tmpl
))
579 error ("template declaration of %q#D", decl
);
582 /* Sanity check: report error if this function FUNCTION is not
583 really a member of the class (CTYPE) it is supposed to belong to.
584 TEMPLATE_PARMS is used to specify the template parameters of a member
585 template passed as FUNCTION_DECL. If the member template is passed as a
586 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
587 from the declaration. If the function is not a function template, it
589 It returns the original declaration for the function, NULL_TREE if
590 no declaration was found, error_mark_node if an error was emitted. */
593 check_classfn (tree ctype
, tree function
, tree template_parms
)
595 if (DECL_USE_TEMPLATE (function
)
596 && !(TREE_CODE (function
) == TEMPLATE_DECL
597 && DECL_TEMPLATE_SPECIALIZATION (function
))
598 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function
)))
599 /* Since this is a specialization of a member template,
600 we're not going to find the declaration in the class.
603 struct S { template <typename T> void f(T); };
604 template <> void S::f(int);
606 we're not going to find `S::f(int)', but there's no
607 reason we should, either. We let our callers know we didn't
608 find the method, but we don't complain. */
611 /* Basic sanity check: for a template function, the template parameters
612 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
613 if (TREE_CODE (function
) == TEMPLATE_DECL
)
616 && !comp_template_parms (template_parms
,
617 DECL_TEMPLATE_PARMS (function
)))
619 error ("template parameter lists provided don%'t match the "
620 "template parameters of %qD", function
);
621 return error_mark_node
;
623 template_parms
= DECL_TEMPLATE_PARMS (function
);
626 /* OK, is this a definition of a member template? */
627 bool is_template
= (template_parms
!= NULL_TREE
);
631 A destructor shall not be a member template. */
632 if (DECL_DESTRUCTOR_P (function
) && is_template
)
634 error ("destructor %qD declared as member template", function
);
635 return error_mark_node
;
638 /* We must enter the scope here, because conversion operators are
639 named by target type, and type equivalence relies on typenames
640 resolving within the scope of CTYPE. */
641 tree pushed_scope
= push_scope (ctype
);
642 tree matched
= NULL_TREE
;
643 tree fns
= get_class_binding (ctype
, DECL_NAME (function
));
645 for (ovl_iterator
iter (fns
); !matched
&& iter
; ++iter
)
649 /* A member template definition only matches a member template
651 if (is_template
!= (TREE_CODE (fndecl
) == TEMPLATE_DECL
))
654 if (!DECL_DECLARES_FUNCTION_P (fndecl
))
657 tree p1
= TYPE_ARG_TYPES (TREE_TYPE (function
));
658 tree p2
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
660 /* We cannot simply call decls_match because this doesn't work
661 for static member functions that are pretending to be
662 methods, and because the name may have been changed by
665 /* Get rid of the this parameter on functions that become
667 if (DECL_STATIC_FUNCTION_P (fndecl
)
668 && TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
669 p1
= TREE_CHAIN (p1
);
671 /* ref-qualifier or absence of same must match. */
672 if (type_memfn_rqual (TREE_TYPE (function
))
673 != type_memfn_rqual (TREE_TYPE (fndecl
)))
676 // Include constraints in the match.
677 tree c1
= get_constraints (function
);
678 tree c2
= get_constraints (fndecl
);
680 /* While finding a match, same types and params are not enough
681 if the function is versioned. Also check version ("target")
683 if (same_type_p (TREE_TYPE (TREE_TYPE (function
)),
684 TREE_TYPE (TREE_TYPE (fndecl
)))
685 && compparms (p1
, p2
)
686 && !targetm
.target_option
.function_versions (function
, fndecl
)
688 || comp_template_parms (template_parms
,
689 DECL_TEMPLATE_PARMS (fndecl
)))
690 && equivalent_constraints (c1
, c2
)
691 && (DECL_TEMPLATE_SPECIALIZATION (function
)
692 == DECL_TEMPLATE_SPECIALIZATION (fndecl
))
693 && (!DECL_TEMPLATE_SPECIALIZATION (function
)
694 || (DECL_TI_TEMPLATE (function
) == DECL_TI_TEMPLATE (fndecl
))))
700 if (!COMPLETE_TYPE_P (ctype
))
701 cxx_incomplete_type_error (DECL_SOURCE_LOCATION (function
),
705 if (DECL_CONV_FN_P (function
))
706 fns
= get_class_binding (ctype
, conv_op_identifier
);
708 error_at (DECL_SOURCE_LOCATION (function
),
709 "no declaration matches %q#D", function
);
711 print_candidates (fns
);
712 else if (DECL_CONV_FN_P (function
))
713 inform (DECL_SOURCE_LOCATION (function
),
714 "no conversion operators declared");
716 inform (DECL_SOURCE_LOCATION (function
),
717 "no functions named %qD", function
);
718 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype
)),
719 "%#qT defined here", ctype
);
721 matched
= error_mark_node
;
725 pop_scope (pushed_scope
);
730 /* DECL is a function with vague linkage. Remember it so that at the
731 end of the translation unit we can decide whether or not to emit
735 note_vague_linkage_fn (tree decl
)
737 if (processing_template_decl
)
740 DECL_DEFER_OUTPUT (decl
) = 1;
741 vec_safe_push (deferred_fns
, decl
);
744 /* As above, but for variable template instantiations. */
747 note_variable_template_instantiation (tree decl
)
749 vec_safe_push (pending_statics
, decl
);
752 /* We have just processed the DECL, which is a static data member.
753 The other parameters are as for cp_finish_decl. */
756 finish_static_data_member_decl (tree decl
,
757 tree init
, bool init_const_expr_p
,
761 if (DECL_TEMPLATE_INSTANTIATED (decl
))
762 /* We already needed to instantiate this, so the processing in this
763 function is unnecessary/wrong. */
766 DECL_CONTEXT (decl
) = current_class_type
;
768 /* We cannot call pushdecl here, because that would fill in the
769 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
770 the right thing, namely, to put this decl out straight away. */
772 if (! processing_template_decl
)
773 vec_safe_push (pending_statics
, decl
);
775 if (LOCAL_CLASS_P (current_class_type
)
776 /* We already complained about the template definition. */
777 && !DECL_TEMPLATE_INSTANTIATION (decl
))
778 permerror (DECL_SOURCE_LOCATION (decl
),
779 "local class %q#T shall not have static data member %q#D",
780 current_class_type
, decl
);
782 for (tree t
= current_class_type
; TYPE_P (t
);
783 t
= CP_TYPE_CONTEXT (t
))
784 if (TYPE_UNNAMED_P (t
))
786 auto_diagnostic_group d
;
787 if (permerror (DECL_SOURCE_LOCATION (decl
),
788 "static data member %qD in unnamed class", decl
))
789 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)),
790 "unnamed class defined here");
794 if (DECL_INLINE_VAR_P (decl
) && !DECL_TEMPLATE_INSTANTIATION (decl
))
795 /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P.
796 Except that if decl is a template instantiation, it isn't defined until
797 instantiate_decl. */;
799 DECL_IN_AGGR_P (decl
) = 1;
801 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
802 && TYPE_DOMAIN (TREE_TYPE (decl
)) == NULL_TREE
)
803 SET_VAR_HAD_UNKNOWN_BOUND (decl
);
807 /* Similarly to start_decl_1, we want to complete the type in order
808 to do the right thing in cp_apply_type_quals_to_decl, possibly
809 clear TYPE_QUAL_CONST (c++/65579). */
810 tree type
= TREE_TYPE (decl
) = complete_type (TREE_TYPE (decl
));
811 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
814 cp_finish_decl (decl
, init
, init_const_expr_p
, asmspec_tree
, flags
);
817 /* DECLARATOR and DECLSPECS correspond to a class member. The other
818 parameters are as for cp_finish_decl. Return the DECL for the
819 class member declared. */
822 grokfield (const cp_declarator
*declarator
,
823 cp_decl_specifier_seq
*declspecs
,
824 tree init
, bool init_const_expr_p
,
829 const char *asmspec
= 0;
833 && TREE_CODE (init
) == TREE_LIST
834 && TREE_VALUE (init
) == error_mark_node
835 && TREE_CHAIN (init
) == NULL_TREE
)
839 if (init
== ridpointers
[(int)RID_DELETE
])
840 initialized
= SD_DELETED
;
841 else if (init
== ridpointers
[(int)RID_DEFAULT
])
842 initialized
= SD_DEFAULTED
;
844 initialized
= SD_INITIALIZED
;
846 initialized
= SD_UNINITIALIZED
;
848 value
= grokdeclarator (declarator
, declspecs
, FIELD
, initialized
, &attrlist
);
849 if (! value
|| value
== error_mark_node
)
850 /* friend or constructor went bad. */
851 return error_mark_node
;
852 if (TREE_TYPE (value
) == error_mark_node
)
855 if (TREE_CODE (value
) == TYPE_DECL
&& init
)
857 error_at (cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (value
)),
858 "typedef %qD is initialized (use %qs instead)",
863 /* Pass friendly classes back. */
864 if (value
== void_type_node
)
867 if (DECL_NAME (value
)
868 && TREE_CODE (DECL_NAME (value
)) == TEMPLATE_ID_EXPR
)
870 error_at (declarator
->id_loc
,
871 "explicit template argument list not allowed");
872 return error_mark_node
;
875 /* Stash away type declarations. */
876 if (TREE_CODE (value
) == TYPE_DECL
)
878 DECL_NONLOCAL (value
) = 1;
879 DECL_CONTEXT (value
) = current_class_type
;
885 /* If this is a typedef that names the class for linkage purposes
886 (7.1.3p8), apply any attributes directly to the type. */
887 if (OVERLOAD_TYPE_P (TREE_TYPE (value
))
888 && value
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
889 attrflags
= ATTR_FLAG_TYPE_IN_PLACE
;
891 cplus_decl_attributes (&value
, attrlist
, attrflags
);
894 if (decl_spec_seq_has_spec_p (declspecs
, ds_typedef
)
895 && TREE_TYPE (value
) != error_mark_node
896 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))) != value
)
897 set_underlying_type (value
);
899 /* It's important that push_template_decl below follows
900 set_underlying_type above so that the created template
901 carries the properly set type of VALUE. */
902 if (processing_template_decl
)
903 value
= push_template_decl (value
);
905 record_locally_defined_typedef (value
);
909 int friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
911 if (!friendp
&& DECL_IN_AGGR_P (value
))
913 error ("%qD is already defined in %qT", value
, DECL_CONTEXT (value
));
914 return void_type_node
;
917 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
918 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
922 if (TREE_CODE (value
) == FUNCTION_DECL
)
924 if (init
== ridpointers
[(int)RID_DELETE
])
926 DECL_DELETED_FN (value
) = 1;
927 DECL_DECLARED_INLINE_P (value
) = 1;
929 else if (init
== ridpointers
[(int)RID_DEFAULT
])
931 if (defaultable_fn_check (value
))
933 DECL_DEFAULTED_FN (value
) = 1;
934 DECL_INITIALIZED_IN_CLASS_P (value
) = 1;
935 DECL_DECLARED_INLINE_P (value
) = 1;
936 /* grokfndecl set this to error_mark_node, but we want to
937 leave it unset until synthesize_method. */
938 DECL_INITIAL (value
) = NULL_TREE
;
941 else if (TREE_CODE (init
) == DEFERRED_PARSE
)
942 error ("invalid initializer for member function %qD", value
);
943 else if (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
)
945 if (integer_zerop (init
))
946 DECL_PURE_VIRTUAL_P (value
) = 1;
947 else if (error_operand_p (init
))
948 ; /* An error has already been reported. */
950 error ("invalid initializer for member function %qD",
955 gcc_assert (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
);
957 = cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (value
));
959 error_at (iloc
, "initializer specified for friend "
960 "function %qD", value
);
962 error_at (iloc
, "initializer specified for static "
963 "member function %qD", value
);
966 else if (TREE_CODE (value
) == FIELD_DECL
)
967 /* C++11 NSDMI, keep going. */;
968 else if (!VAR_P (value
))
972 /* Pass friend decls back. */
973 if ((TREE_CODE (value
) == FUNCTION_DECL
974 || TREE_CODE (value
) == TEMPLATE_DECL
)
975 && DECL_CONTEXT (value
) != current_class_type
)
978 /* Need to set this before push_template_decl. */
980 DECL_CONTEXT (value
) = current_class_type
;
982 if (processing_template_decl
&& VAR_OR_FUNCTION_DECL_P (value
))
984 value
= push_template_decl (value
);
985 if (error_operand_p (value
))
986 return error_mark_node
;
990 cplus_decl_attributes (&value
, attrlist
, 0);
992 if (init
&& DIRECT_LIST_INIT_P (init
))
993 flags
= LOOKUP_NORMAL
;
995 flags
= LOOKUP_IMPLICIT
;
997 switch (TREE_CODE (value
))
1000 finish_static_data_member_decl (value
, init
, init_const_expr_p
,
1001 asmspec_tree
, flags
);
1006 error ("%<asm%> specifiers are not permitted on non-static data members");
1007 if (DECL_INITIAL (value
) == error_mark_node
)
1008 init
= error_mark_node
;
1009 cp_finish_decl (value
, init
, /*init_const_expr_p=*/false,
1011 DECL_IN_AGGR_P (value
) = 1;
1016 set_user_assembler_name (value
, asmspec
);
1018 cp_finish_decl (value
,
1020 /*init_const_expr_p=*/false,
1021 asmspec_tree
, flags
);
1023 /* Pass friends back this way. */
1024 if (DECL_FRIEND_P (value
))
1025 return void_type_node
;
1027 DECL_IN_AGGR_P (value
) = 1;
1036 /* Like `grokfield', but for bitfields.
1037 WIDTH is the width of the bitfield, a constant expression.
1038 The other parameters are as for grokfield. */
1041 grokbitfield (const cp_declarator
*declarator
,
1042 cp_decl_specifier_seq
*declspecs
, tree width
, tree init
,
1045 tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
,
1046 init
!= NULL_TREE
, &attrlist
);
1048 if (value
== error_mark_node
)
1049 return NULL_TREE
; /* friends went bad. */
1051 tree type
= TREE_TYPE (value
);
1052 if (type
== error_mark_node
)
1055 /* Pass friendly classes back. */
1056 if (VOID_TYPE_P (value
))
1057 return void_type_node
;
1059 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
1060 && (INDIRECT_TYPE_P (type
) || !dependent_type_p (type
)))
1062 error_at (DECL_SOURCE_LOCATION (value
),
1063 "bit-field %qD with non-integral type %qT",
1065 return error_mark_node
;
1068 if (TREE_CODE (value
) == TYPE_DECL
)
1070 error_at (DECL_SOURCE_LOCATION (value
),
1071 "cannot declare %qD to be a bit-field type", value
);
1075 /* Usually, finish_struct_1 catches bitfields with invalid types.
1076 But, in the case of bitfields with function type, we confuse
1077 ourselves into thinking they are member functions, so we must
1079 if (TREE_CODE (value
) == FUNCTION_DECL
)
1081 error_at (DECL_SOURCE_LOCATION (value
),
1082 "cannot declare bit-field %qD with function type", value
);
1086 if (TYPE_WARN_IF_NOT_ALIGN (type
))
1088 error_at (DECL_SOURCE_LOCATION (value
), "cannot declare bit-field "
1089 "%qD with %<warn_if_not_aligned%> type", value
);
1093 if (DECL_IN_AGGR_P (value
))
1095 error ("%qD is already defined in the class %qT", value
,
1096 DECL_CONTEXT (value
));
1097 return void_type_node
;
1100 if (TREE_STATIC (value
))
1102 error_at (DECL_SOURCE_LOCATION (value
),
1103 "static member %qD cannot be a bit-field", value
);
1107 int flags
= LOOKUP_IMPLICIT
;
1108 if (init
&& DIRECT_LIST_INIT_P (init
))
1109 flags
= LOOKUP_NORMAL
;
1110 cp_finish_decl (value
, init
, false, NULL_TREE
, flags
);
1112 if (width
!= error_mark_node
)
1114 /* The width must be an integer type. */
1115 if (!type_dependent_expression_p (width
)
1116 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width
)))
1117 error ("width of bit-field %qD has non-integral type %qT", value
,
1121 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1122 check_bitfield_decl picks it from there later and sets DECL_SIZE
1124 DECL_BIT_FIELD_REPRESENTATIVE (value
) = width
;
1125 SET_DECL_C_BIT_FIELD (value
);
1129 DECL_IN_AGGR_P (value
) = 1;
1132 cplus_decl_attributes (&value
, attrlist
, /*flags=*/0);
1138 /* Returns true iff ATTR is an attribute which needs to be applied at
1139 instantiation time rather than template definition time. */
1142 is_late_template_attribute (tree attr
, tree decl
)
1144 tree name
= get_attribute_name (attr
);
1145 tree args
= TREE_VALUE (attr
);
1146 const struct attribute_spec
*spec
= lookup_attribute_spec (name
);
1150 /* Unknown attribute. */
1153 /* Attribute weak handling wants to write out assembly right away. */
1154 if (is_attribute_p ("weak", name
))
1157 /* Attributes used and unused are applied directly to typedefs for the
1158 benefit of maybe_warn_unused_local_typedefs. */
1159 if (TREE_CODE (decl
) == TYPE_DECL
1160 && (is_attribute_p ("unused", name
)
1161 || is_attribute_p ("used", name
)))
1164 /* Attribute tls_model wants to modify the symtab. */
1165 if (is_attribute_p ("tls_model", name
))
1168 /* #pragma omp declare simd attribute needs to be always deferred. */
1170 && is_attribute_p ("omp declare simd", name
))
1173 if (args
== error_mark_node
)
1176 /* An attribute pack is clearly dependent. */
1177 if (args
&& PACK_EXPANSION_P (args
))
1180 /* If any of the arguments are dependent expressions, we can't evaluate
1181 the attribute until instantiation time. */
1182 for (arg
= args
; arg
; arg
= TREE_CHAIN (arg
))
1184 tree t
= TREE_VALUE (arg
);
1186 /* If the first attribute argument is an identifier, only consider
1187 second and following arguments. Attributes like mode, format,
1188 cleanup and several target specific attributes aren't late
1189 just because they have an IDENTIFIER_NODE as first argument. */
1190 if (arg
== args
&& attribute_takes_identifier_p (name
)
1191 && identifier_p (t
))
1194 if (value_dependent_expression_p (t
))
1198 if (TREE_CODE (decl
) == TYPE_DECL
1200 || spec
->type_required
)
1202 tree type
= TYPE_P (decl
) ? decl
: TREE_TYPE (decl
);
1204 /* We can't apply any attributes to a completely unknown type until
1205 instantiation time. */
1206 enum tree_code code
= TREE_CODE (type
);
1207 if (code
== TEMPLATE_TYPE_PARM
1208 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
1209 || code
== TYPENAME_TYPE
)
1211 /* Also defer most attributes on dependent types. This is not
1212 necessary in all cases, but is the better default. */
1213 else if (dependent_type_p (type
)
1214 /* But some attributes specifically apply to templates. */
1215 && !is_attribute_p ("abi_tag", name
)
1216 && !is_attribute_p ("deprecated", name
)
1217 && !is_attribute_p ("visibility", name
))
1226 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1227 applied at instantiation time and return them. If IS_DEPENDENT is true,
1228 the declaration itself is dependent, so all attributes should be applied
1229 at instantiation time. */
1232 splice_template_attributes (tree
*attr_p
, tree decl
)
1235 tree late_attrs
= NULL_TREE
;
1236 tree
*q
= &late_attrs
;
1243 if (is_late_template_attribute (*p
, decl
))
1245 ATTR_IS_DEPENDENT (*p
) = 1;
1247 *p
= TREE_CHAIN (*p
);
1248 q
= &TREE_CHAIN (*q
);
1252 p
= &TREE_CHAIN (*p
);
1258 /* Remove any late attributes from the list in ATTR_P and attach them to
1262 save_template_attributes (tree
*attr_p
, tree
*decl_p
, int flags
)
1266 if (attr_p
&& *attr_p
== error_mark_node
)
1269 tree late_attrs
= splice_template_attributes (attr_p
, *decl_p
);
1273 if (DECL_P (*decl_p
))
1274 q
= &DECL_ATTRIBUTES (*decl_p
);
1276 q
= &TYPE_ATTRIBUTES (*decl_p
);
1278 tree old_attrs
= *q
;
1280 /* Merge the late attributes at the beginning with the attribute
1282 late_attrs
= merge_attributes (late_attrs
, *q
);
1283 if (*q
!= late_attrs
1284 && !DECL_P (*decl_p
)
1285 && !(flags
& ATTR_FLAG_TYPE_IN_PLACE
))
1287 if (!dependent_type_p (*decl_p
))
1288 *decl_p
= cp_build_type_attribute_variant (*decl_p
, late_attrs
);
1291 *decl_p
= build_variant_type_copy (*decl_p
);
1292 TYPE_ATTRIBUTES (*decl_p
) = late_attrs
;
1298 if (!DECL_P (*decl_p
) && *decl_p
== TYPE_MAIN_VARIANT (*decl_p
))
1300 /* We've added new attributes directly to the main variant, so
1301 now we need to update all of the other variants to include
1302 these new attributes. */
1304 for (variant
= TYPE_NEXT_VARIANT (*decl_p
); variant
;
1305 variant
= TYPE_NEXT_VARIANT (variant
))
1307 gcc_assert (TYPE_ATTRIBUTES (variant
) == old_attrs
);
1308 TYPE_ATTRIBUTES (variant
) = TYPE_ATTRIBUTES (*decl_p
);
1313 /* True if ATTRS contains any dependent attributes that affect type
1317 any_dependent_type_attributes_p (tree attrs
)
1319 for (tree a
= attrs
; a
; a
= TREE_CHAIN (a
))
1320 if (ATTR_IS_DEPENDENT (a
))
1322 const attribute_spec
*as
= lookup_attribute_spec (TREE_PURPOSE (a
));
1323 if (as
&& as
->affects_type_identity
)
1329 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1330 to a typedef which gives a previously unnamed class or enum a name for
1331 linkage purposes. */
1334 attributes_naming_typedef_ok (tree attrs
)
1336 for (; attrs
; attrs
= TREE_CHAIN (attrs
))
1338 tree name
= get_attribute_name (attrs
);
1339 if (is_attribute_p ("vector_size", name
))
1345 /* Like reconstruct_complex_type, but handle also template trees. */
1348 cp_reconstruct_complex_type (tree type
, tree bottom
)
1352 if (TYPE_PTR_P (type
))
1354 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1355 outer
= build_pointer_type_for_mode (inner
, TYPE_MODE (type
),
1356 TYPE_REF_CAN_ALIAS_ALL (type
));
1358 else if (TYPE_REF_P (type
))
1360 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1361 outer
= build_reference_type_for_mode (inner
, TYPE_MODE (type
),
1362 TYPE_REF_CAN_ALIAS_ALL (type
));
1364 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1366 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1367 outer
= build_cplus_array_type (inner
, TYPE_DOMAIN (type
));
1368 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1369 element type qualification will be handled by the recursive
1370 cp_reconstruct_complex_type call and cp_build_qualified_type
1371 for ARRAY_TYPEs changes the element type. */
1374 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
1376 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1377 outer
= build_function_type (inner
, TYPE_ARG_TYPES (type
));
1378 outer
= apply_memfn_quals (outer
, type_memfn_quals (type
));
1380 else if (TREE_CODE (type
) == METHOD_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
));
1401 outer
= cxx_copy_lang_qualifiers (outer
, type
);
1406 /* Replaces any constexpr expression that may be into the attributes
1407 arguments with their reduced value. */
1410 cp_check_const_attributes (tree attributes
)
1412 if (attributes
== error_mark_node
)
1416 for (attr
= attributes
; attr
; attr
= TREE_CHAIN (attr
))
1419 for (arg
= TREE_VALUE (attr
); arg
&& TREE_CODE (arg
) == TREE_LIST
;
1420 arg
= TREE_CHAIN (arg
))
1422 tree expr
= TREE_VALUE (arg
);
1424 TREE_VALUE (arg
) = fold_non_dependent_expr (expr
);
1429 /* Return true if TYPE is an OpenMP mappable type.
1430 If NOTES is non-zero, emit a note message for each problem. */
1432 cp_omp_mappable_type_1 (tree type
, bool notes
)
1436 /* Mappable type has to be complete. */
1437 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
1439 if (notes
&& type
!= error_mark_node
)
1441 tree decl
= TYPE_MAIN_DECL (type
);
1442 inform ((decl
? DECL_SOURCE_LOCATION (decl
) : input_location
),
1443 "incomplete type %qT is not mappable", type
);
1447 /* Arrays have mappable type if the elements have mappable type. */
1448 while (TREE_CODE (type
) == ARRAY_TYPE
)
1449 type
= TREE_TYPE (type
);
1450 /* A mappable type cannot contain virtual members. */
1451 if (CLASS_TYPE_P (type
) && CLASSTYPE_VTABLES (type
))
1454 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
1455 "type %qT with virtual members is not mappable", type
);
1458 /* All data members must be non-static. */
1459 if (CLASS_TYPE_P (type
))
1462 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1466 inform (DECL_SOURCE_LOCATION (field
),
1467 "static field %qD is not mappable", field
);
1470 /* All fields must have mappable types. */
1471 else if (TREE_CODE (field
) == FIELD_DECL
1472 && !cp_omp_mappable_type_1 (TREE_TYPE (field
), notes
))
1478 /* Return true if TYPE is an OpenMP mappable type. */
1480 cp_omp_mappable_type (tree type
)
1482 return cp_omp_mappable_type_1 (type
, false);
1485 /* Return true if TYPE is an OpenMP mappable type.
1486 Emit an error messages if not. */
1488 cp_omp_emit_unmappable_type_notes (tree type
)
1490 return cp_omp_mappable_type_1 (type
, true);
1493 /* Return the last pushed declaration for the symbol DECL or NULL
1494 when no such declaration exists. */
1497 find_last_decl (tree decl
)
1499 tree last_decl
= NULL_TREE
;
1501 if (tree name
= DECL_P (decl
) ? DECL_NAME (decl
) : NULL_TREE
)
1503 /* Look up the declaration in its scope. */
1504 tree pushed_scope
= NULL_TREE
;
1505 if (tree ctype
= DECL_CONTEXT (decl
))
1506 pushed_scope
= push_scope (ctype
);
1508 last_decl
= lookup_name (name
);
1511 pop_scope (pushed_scope
);
1513 /* The declaration may be a member conversion operator
1514 or a bunch of overfloads (handle the latter below). */
1515 if (last_decl
&& BASELINK_P (last_decl
))
1516 last_decl
= BASELINK_FUNCTIONS (last_decl
);
1522 if (DECL_P (last_decl
) || TREE_CODE (last_decl
) == OVERLOAD
)
1524 /* A set of overloads of the same function. */
1525 for (lkp_iterator
iter (last_decl
); iter
; ++iter
)
1527 if (TREE_CODE (*iter
) == OVERLOAD
)
1530 if (decls_match (decl
, *iter
, /*record_decls=*/false))
1539 /* Like decl_attributes, but handle C++ complexity. */
1542 cplus_decl_attributes (tree
*decl
, tree attributes
, int flags
)
1544 if (*decl
== NULL_TREE
|| *decl
== void_type_node
1545 || *decl
== error_mark_node
)
1548 /* Add implicit "omp declare target" attribute if requested. */
1549 if (scope_chain
->omp_declare_target_attribute
1551 && (TREE_STATIC (*decl
) || DECL_EXTERNAL (*decl
)))
1552 || TREE_CODE (*decl
) == FUNCTION_DECL
))
1555 && DECL_CLASS_SCOPE_P (*decl
))
1556 error ("%q+D static data member inside of declare target directive",
1558 else if (VAR_P (*decl
)
1559 && (processing_template_decl
1560 || !cp_omp_mappable_type (TREE_TYPE (*decl
))))
1561 attributes
= tree_cons (get_identifier ("omp declare target implicit"),
1562 NULL_TREE
, attributes
);
1565 attributes
= tree_cons (get_identifier ("omp declare target"),
1566 NULL_TREE
, attributes
);
1567 attributes
= tree_cons (get_identifier ("omp declare target block"),
1568 NULL_TREE
, attributes
);
1572 if (processing_template_decl
)
1574 if (check_for_bare_parameter_packs (attributes
))
1577 save_template_attributes (&attributes
, decl
, flags
);
1580 cp_check_const_attributes (attributes
);
1582 if (TREE_CODE (*decl
) == TEMPLATE_DECL
)
1583 decl
= &DECL_TEMPLATE_RESULT (*decl
);
1585 if (TREE_TYPE (*decl
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl
)))
1588 = decl_attributes (decl
, attributes
, flags
| ATTR_FLAG_FUNCTION_NEXT
);
1589 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl
)),
1594 tree last_decl
= find_last_decl (*decl
);
1595 decl_attributes (decl
, attributes
, flags
, last_decl
);
1598 if (TREE_CODE (*decl
) == TYPE_DECL
)
1599 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl
), TREE_TYPE (*decl
));
1601 /* Propagate deprecation out to the template. */
1602 if (TREE_DEPRECATED (*decl
))
1603 if (tree ti
= get_template_info (*decl
))
1605 tree tmpl
= TI_TEMPLATE (ti
);
1606 tree pattern
= (TYPE_P (*decl
) ? TREE_TYPE (tmpl
)
1607 : DECL_TEMPLATE_RESULT (tmpl
));
1608 if (*decl
== pattern
)
1609 TREE_DEPRECATED (tmpl
) = true;
1613 /* Walks through the namespace- or function-scope anonymous union
1614 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1615 Returns one of the fields for use in the mangled name. */
1618 build_anon_union_vars (tree type
, tree object
)
1620 tree main_decl
= NULL_TREE
;
1623 /* Rather than write the code to handle the non-union case,
1624 just give an error. */
1625 if (TREE_CODE (type
) != UNION_TYPE
)
1627 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
1628 "anonymous struct not inside named type");
1629 return error_mark_node
;
1632 for (field
= TYPE_FIELDS (type
);
1634 field
= DECL_CHAIN (field
))
1639 if (DECL_ARTIFICIAL (field
))
1641 if (TREE_CODE (field
) != FIELD_DECL
)
1643 permerror (DECL_SOURCE_LOCATION (field
),
1644 "%q#D invalid; an anonymous union can only "
1645 "have non-static data members", field
);
1649 if (TREE_PRIVATE (field
))
1650 permerror (DECL_SOURCE_LOCATION (field
),
1651 "private member %q#D in anonymous union", field
);
1652 else if (TREE_PROTECTED (field
))
1653 permerror (DECL_SOURCE_LOCATION (field
),
1654 "protected member %q#D in anonymous union", field
);
1656 if (processing_template_decl
)
1657 ref
= build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
, object
,
1658 DECL_NAME (field
), NULL_TREE
);
1660 ref
= build_class_member_access_expr (object
, field
, NULL_TREE
,
1661 false, tf_warning_or_error
);
1663 if (DECL_NAME (field
))
1667 decl
= build_decl (input_location
,
1668 VAR_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
1669 DECL_ANON_UNION_VAR_P (decl
) = 1;
1670 DECL_ARTIFICIAL (decl
) = 1;
1672 base
= get_base_address (object
);
1673 TREE_PUBLIC (decl
) = TREE_PUBLIC (base
);
1674 TREE_STATIC (decl
) = TREE_STATIC (base
);
1675 DECL_EXTERNAL (decl
) = DECL_EXTERNAL (base
);
1677 SET_DECL_VALUE_EXPR (decl
, ref
);
1678 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1680 decl
= pushdecl (decl
);
1682 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1683 decl
= build_anon_union_vars (TREE_TYPE (field
), ref
);
1687 if (main_decl
== NULL_TREE
)
1694 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1695 anonymous union, then all members must be laid out together. PUBLIC_P
1696 is nonzero if this union is not declared static. */
1699 finish_anon_union (tree anon_union_decl
)
1705 if (anon_union_decl
== error_mark_node
)
1708 type
= TREE_TYPE (anon_union_decl
);
1709 public_p
= TREE_PUBLIC (anon_union_decl
);
1711 /* The VAR_DECL's context is the same as the TYPE's context. */
1712 DECL_CONTEXT (anon_union_decl
) = DECL_CONTEXT (TYPE_NAME (type
));
1714 if (TYPE_FIELDS (type
) == NULL_TREE
)
1719 error ("namespace-scope anonymous aggregates must be static");
1723 main_decl
= build_anon_union_vars (type
, anon_union_decl
);
1724 if (main_decl
== error_mark_node
)
1726 if (main_decl
== NULL_TREE
)
1728 pedwarn (input_location
, 0, "anonymous union with no members");
1732 if (!processing_template_decl
)
1734 /* Use main_decl to set the mangled name. */
1735 DECL_NAME (anon_union_decl
) = DECL_NAME (main_decl
);
1736 maybe_commonize_var (anon_union_decl
);
1737 if (TREE_STATIC (anon_union_decl
) || DECL_EXTERNAL (anon_union_decl
))
1739 if (DECL_DISCRIMINATOR_P (anon_union_decl
))
1740 determine_local_discriminator (anon_union_decl
);
1741 mangle_decl (anon_union_decl
);
1743 DECL_NAME (anon_union_decl
) = NULL_TREE
;
1746 pushdecl (anon_union_decl
);
1747 cp_finish_decl (anon_union_decl
, NULL_TREE
, false, NULL_TREE
, 0);
1750 /* Auxiliary functions to make type signatures for
1751 `operator new' and `operator delete' correspond to
1752 what compiler will be expecting. */
1755 coerce_new_type (tree type
, location_t loc
)
1758 tree args
= TYPE_ARG_TYPES (type
);
1760 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1762 if (!same_type_p (TREE_TYPE (type
), ptr_type_node
))
1765 error_at (loc
, "%<operator new%> must return type %qT",
1769 if (args
&& args
!= void_list_node
)
1771 if (TREE_PURPOSE (args
))
1773 /* [basic.stc.dynamic.allocation]
1775 The first parameter shall not have an associated default
1777 error_at (loc
, "the first parameter of %<operator new%> cannot "
1778 "have a default argument");
1779 /* Throw away the default argument. */
1780 TREE_PURPOSE (args
) = NULL_TREE
;
1783 if (!same_type_p (TREE_VALUE (args
), size_type_node
))
1786 args
= TREE_CHAIN (args
);
1793 permerror (loc
, "%<operator new%> takes type %<size_t%> (%qT) "
1794 "as first parameter", size_type_node
);
1799 args
= tree_cons (NULL_TREE
, size_type_node
, args
);
1802 type
= (cxx_copy_lang_qualifiers
1803 (build_function_type (ptr_type_node
, args
),
1812 coerce_delete_type (tree decl
, location_t loc
)
1815 tree type
= TREE_TYPE (decl
);
1816 tree args
= TYPE_ARG_TYPES (type
);
1818 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1820 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
1823 error_at (loc
, "%<operator delete%> must return type %qT",
1827 tree ptrtype
= ptr_type_node
;
1828 if (destroying_delete_p (decl
))
1830 if (DECL_CLASS_SCOPE_P (decl
))
1831 /* If the function is a destroying operator delete declared in class
1832 type C, the type of its first parameter shall be C*. */
1833 ptrtype
= build_pointer_type (DECL_CONTEXT (decl
));
1835 /* A destroying operator delete shall be a class member function named
1838 "destroying %<operator delete%> must be a member function");
1839 const ovl_op_info_t
*op
= IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl
));
1840 if (op
->flags
& OVL_OP_FLAG_VEC
)
1841 error_at (loc
, "%<operator delete[]%> cannot be a destroying delete");
1842 if (!usual_deallocation_fn_p (decl
))
1843 error_at (loc
, "destroying %<operator delete%> must be a usual "
1844 "deallocation function");
1847 if (!args
|| args
== void_list_node
1848 || !same_type_p (TREE_VALUE (args
), ptrtype
))
1851 if (args
&& args
!= void_list_node
)
1852 args
= TREE_CHAIN (args
);
1853 error_at (loc
, "%<operator delete%> takes type %qT as first parameter",
1859 args
= tree_cons (NULL_TREE
, ptrtype
, args
);
1862 type
= (cxx_copy_lang_qualifiers
1863 (build_function_type (void_type_node
, args
),
1869 TREE_TYPE (decl
) = type
;
1872 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1873 and mark them as needed. */
1876 mark_vtable_entries (tree decl
, vec
<tree
> &consteval_vtables
)
1879 unsigned HOST_WIDE_INT idx
;
1881 /* It's OK for the vtable to refer to deprecated virtual functions. */
1882 warning_sentinel
w(warn_deprecated_decl
);
1884 bool consteval_seen
= false;
1886 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
1891 STRIP_NOPS (fnaddr
);
1893 if (TREE_CODE (fnaddr
) != ADDR_EXPR
1894 && TREE_CODE (fnaddr
) != FDESC_EXPR
)
1895 /* This entry is an offset: a virtual base class offset, a
1896 virtual call offset, an RTTI offset, etc. */
1899 fn
= TREE_OPERAND (fnaddr
, 0);
1900 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (fn
))
1902 if (!consteval_seen
)
1904 consteval_seen
= true;
1905 consteval_vtables
.safe_push (decl
);
1909 TREE_ADDRESSABLE (fn
) = 1;
1910 /* When we don't have vcall offsets, we output thunks whenever
1911 we output the vtables that contain them. With vcall offsets,
1912 we know all the thunks we'll need when we emit a virtual
1913 function, so we emit the thunks there instead. */
1914 if (DECL_THUNK_P (fn
))
1915 use_thunk (fn
, /*emit_p=*/0);
1916 /* Set the location, as marking the function could cause
1917 instantiation. We do not need to preserve the incoming
1918 location, as we're called from c_parse_final_cleanups, which
1919 takes care of that. */
1920 input_location
= DECL_SOURCE_LOCATION (fn
);
1925 /* Replace any consteval functions in vtables with null pointers. */
1928 clear_consteval_vfns (vec
<tree
> &consteval_vtables
)
1930 for (tree vtable
: consteval_vtables
)
1931 for (constructor_elt
&elt
: *CONSTRUCTOR_ELTS (DECL_INITIAL (vtable
)))
1933 tree fn
= cp_get_fndecl_from_callee (elt
.value
, /*fold*/false);
1934 if (fn
&& DECL_IMMEDIATE_FUNCTION_P (fn
))
1935 elt
.value
= build_zero_cst (vtable_entry_type
);
1939 /* Adjust the TLS model on variable DECL if need be, typically after
1940 the linkage of DECL has been modified. */
1943 adjust_var_decl_tls_model (tree decl
)
1945 if (CP_DECL_THREAD_LOCAL_P (decl
)
1946 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl
)))
1947 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
1950 /* Set DECL up to have the closest approximation of "initialized common"
1951 linkage available. */
1954 comdat_linkage (tree decl
)
1957 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1958 else if (TREE_CODE (decl
) == FUNCTION_DECL
1959 || (VAR_P (decl
) && DECL_ARTIFICIAL (decl
)))
1960 /* We can just emit function and compiler-generated variables
1961 statically; having multiple copies is (for the most part) only
1964 There are two correctness issues, however: the address of a
1965 template instantiation with external linkage should be the
1966 same, independent of what translation unit asks for the
1967 address, and this will not hold when we emit multiple copies of
1968 the function. However, there's little else we can do.
1970 Also, by default, the typeinfo implementation assumes that
1971 there will be only one copy of the string used as the name for
1972 each type. Therefore, if weak symbols are unavailable, the
1973 run-time library should perform a more conservative check; it
1974 should perform a string comparison, rather than an address
1976 TREE_PUBLIC (decl
) = 0;
1979 /* Static data member template instantiations, however, cannot
1980 have multiple copies. */
1981 if (DECL_INITIAL (decl
) == 0
1982 || DECL_INITIAL (decl
) == error_mark_node
)
1983 DECL_COMMON (decl
) = 1;
1984 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
1986 DECL_COMMON (decl
) = 1;
1987 DECL_INITIAL (decl
) = error_mark_node
;
1989 else if (!DECL_EXPLICIT_INSTANTIATION (decl
))
1991 /* We can't do anything useful; leave vars for explicit
1993 DECL_EXTERNAL (decl
) = 1;
1994 DECL_NOT_REALLY_EXTERN (decl
) = 0;
1998 if (TREE_PUBLIC (decl
))
1999 DECL_COMDAT (decl
) = 1;
2002 adjust_var_decl_tls_model (decl
);
2005 /* For win32 we also want to put explicit instantiations in
2006 linkonce sections, so that they will be merged with implicit
2007 instantiations; otherwise we get duplicate symbol errors.
2008 For Darwin we do not want explicit instantiations to be
2012 maybe_make_one_only (tree decl
)
2014 /* We used to say that this was not necessary on targets that support weak
2015 symbols, because the implicit instantiations will defer to the explicit
2016 one. However, that's not actually the case in SVR4; a strong definition
2017 after a weak one is an error. Also, not making explicit
2018 instantiations one_only means that we can end up with two copies of
2019 some template instantiations. */
2023 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
2024 we can get away with not emitting them if they aren't used. We need
2025 to for variables so that cp_finish_decl will update their linkage,
2026 because their DECL_INITIAL may not have been set properly yet. */
2028 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
2029 || (! DECL_EXPLICIT_INSTANTIATION (decl
)
2030 && ! DECL_TEMPLATE_SPECIALIZATION (decl
)))
2032 make_decl_one_only (decl
, cxx_comdat_group (decl
));
2036 varpool_node
*node
= varpool_node::get_create (decl
);
2037 DECL_COMDAT (decl
) = 1;
2038 /* Mark it needed so we don't forget to emit it. */
2039 node
->forced_by_abi
= true;
2040 TREE_USED (decl
) = 1;
2042 adjust_var_decl_tls_model (decl
);
2047 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
2048 This predicate will give the right answer during parsing of the
2049 function, which other tests may not. */
2052 vague_linkage_p (tree decl
)
2054 if (!TREE_PUBLIC (decl
))
2056 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
2057 maybe-in-charge 'tor variants; in that case we need to check one of
2058 the "clones" for the real linkage. But only in that case; before
2059 maybe_clone_body we haven't yet copied the linkage to the clones. */
2060 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl
)
2061 && !DECL_ABSTRACT_P (decl
)
2062 && DECL_CHAIN (decl
)
2063 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)))
2064 return vague_linkage_p (DECL_CHAIN (decl
));
2066 gcc_checking_assert (!DECL_COMDAT (decl
));
2069 /* Unfortunately, import_export_decl has not always been called
2070 before the function is processed, so we cannot simply check
2072 if (DECL_COMDAT (decl
)
2073 || (TREE_CODE (decl
) == FUNCTION_DECL
2074 && DECL_DECLARED_INLINE_P (decl
))
2075 || (DECL_LANG_SPECIFIC (decl
)
2076 && DECL_TEMPLATE_INSTANTIATION (decl
))
2077 || (VAR_P (decl
) && DECL_INLINE_VAR_P (decl
)))
2079 else if (DECL_FUNCTION_SCOPE_P (decl
))
2080 /* A local static in an inline effectively has vague linkage. */
2081 return (TREE_STATIC (decl
)
2082 && vague_linkage_p (DECL_CONTEXT (decl
)));
2087 /* Determine whether or not we want to specifically import or export CTYPE,
2088 using various heuristics. */
2091 import_export_class (tree ctype
)
2093 /* -1 for imported, 1 for exported. */
2094 int import_export
= 0;
2096 /* It only makes sense to call this function at EOF. The reason is
2097 that this function looks at whether or not the first non-inline
2098 non-abstract virtual member function has been defined in this
2099 translation unit. But, we can't possibly know that until we've
2100 seen the entire translation unit. */
2101 gcc_assert (at_eof
);
2103 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2106 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2107 we will have CLASSTYPE_INTERFACE_ONLY set but not
2108 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2109 heuristic because someone will supply a #pragma implementation
2110 elsewhere, and deducing it here would produce a conflict. */
2111 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
2114 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
2116 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
2118 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
2119 && !flag_implicit_templates
)
2120 /* For a template class, without -fimplicit-templates, check the
2121 repository. If the virtual table is assigned to this
2122 translation unit, then export the class; otherwise, import
2125 else if (TYPE_POLYMORPHIC_P (ctype
))
2127 /* The ABI specifies that the virtual table and associated
2128 information are emitted with the key method, if any. */
2129 tree method
= CLASSTYPE_KEY_METHOD (ctype
);
2130 /* If weak symbol support is not available, then we must be
2131 careful not to emit the vtable when the key function is
2132 inline. An inline function can be defined in multiple
2133 translation units. If we were to emit the vtable in each
2134 translation unit containing a definition, we would get
2135 multiple definition errors at link-time. */
2136 if (method
&& (flag_weak
|| ! DECL_DECLARED_INLINE_P (method
)))
2137 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
2140 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2141 a definition anywhere else. */
2142 if (MULTIPLE_SYMBOL_SPACES
&& import_export
== -1)
2145 /* Allow back ends the chance to overrule the decision. */
2146 if (targetm
.cxx
.import_export_class
)
2147 import_export
= targetm
.cxx
.import_export_class (ctype
, import_export
);
2151 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
2152 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
2156 /* Return true if VAR has already been provided to the back end; in that
2157 case VAR should not be modified further by the front end. */
2159 var_finalized_p (tree var
)
2161 return varpool_node::get_create (var
)->definition
;
2164 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2165 must be emitted in this translation unit. Mark it as such. */
2168 mark_needed (tree decl
)
2170 TREE_USED (decl
) = 1;
2171 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2173 /* Extern inline functions don't become needed when referenced.
2174 If we know a method will be emitted in other TU and no new
2175 functions can be marked reachable, just use the external
2177 struct cgraph_node
*node
= cgraph_node::get_create (decl
);
2178 node
->forced_by_abi
= true;
2180 /* #pragma interface can call mark_needed for
2181 maybe-in-charge 'tors; mark the clones as well. */
2183 FOR_EACH_CLONE (clone
, decl
)
2184 mark_needed (clone
);
2186 else if (VAR_P (decl
))
2188 varpool_node
*node
= varpool_node::get_create (decl
);
2189 /* C++ frontend use mark_decl_references to force COMDAT variables
2190 to be output that might appear dead otherwise. */
2191 node
->forced_by_abi
= true;
2195 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2196 returns true if a definition of this entity should be provided in
2197 this object file. Callers use this function to determine whether
2198 or not to let the back end know that a definition of DECL is
2199 available in this translation unit. */
2202 decl_needed_p (tree decl
)
2204 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2205 /* This function should only be called at the end of the translation
2206 unit. We cannot be sure of whether or not something will be
2207 COMDAT until that point. */
2208 gcc_assert (at_eof
);
2210 /* All entities with external linkage that are not COMDAT/EXTERN should be
2211 emitted; they may be referred to from other object files. */
2212 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_REALLY_EXTERN (decl
))
2215 /* Functions marked "dllexport" must be emitted so that they are
2216 visible to other DLLs. */
2217 if (flag_keep_inline_dllexport
2218 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))
2221 /* When not optimizing, do not bother to produce definitions for extern
2223 if (DECL_REALLY_EXTERN (decl
)
2224 && ((TREE_CODE (decl
) != FUNCTION_DECL
2226 || (TREE_CODE (decl
) == FUNCTION_DECL
2227 && !opt_for_fn (decl
, optimize
)))
2228 && !lookup_attribute ("always_inline", decl
))
2231 /* If this entity was used, let the back end see it; it will decide
2232 whether or not to emit it into the object file. */
2233 if (TREE_USED (decl
))
2236 /* Virtual functions might be needed for devirtualization. */
2237 if (flag_devirtualize
2238 && TREE_CODE (decl
) == FUNCTION_DECL
2239 && DECL_VIRTUAL_P (decl
))
2242 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2243 reference to DECL might cause it to be emitted later. */
2247 /* If necessary, write out the vtables for the dynamic class CTYPE.
2248 Returns true if any vtables were emitted. */
2251 maybe_emit_vtables (tree ctype
, vec
<tree
> &consteval_vtables
)
2256 varpool_node
*current
= NULL
, *last
= NULL
;
2258 /* If the vtables for this class have already been emitted there is
2259 nothing more to do. */
2260 primary_vtbl
= CLASSTYPE_VTABLES (ctype
);
2261 if (var_finalized_p (primary_vtbl
))
2263 /* Ignore dummy vtables made by get_vtable_decl. */
2264 if (TREE_TYPE (primary_vtbl
) == void_type_node
)
2267 /* On some targets, we cannot determine the key method until the end
2268 of the translation unit -- which is when this function is
2270 if (!targetm
.cxx
.key_method_may_be_inline ())
2271 determine_key_method (ctype
);
2273 /* See if any of the vtables are needed. */
2274 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2276 import_export_decl (vtbl
);
2277 if (DECL_NOT_REALLY_EXTERN (vtbl
) && decl_needed_p (vtbl
))
2282 /* If the references to this class' vtables are optimized away,
2283 still emit the appropriate debugging information. See
2285 if (DECL_COMDAT (primary_vtbl
)
2286 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
2287 note_debug_info_needed (ctype
);
2291 /* The ABI requires that we emit all of the vtables if we emit any
2293 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2295 /* Mark entities references from the virtual table as used. */
2296 mark_vtable_entries (vtbl
, consteval_vtables
);
2298 if (TREE_TYPE (DECL_INITIAL (vtbl
)) == 0)
2300 vec
<tree
, va_gc
> *cleanups
= NULL
;
2301 tree expr
= store_init_value (vtbl
, DECL_INITIAL (vtbl
), &cleanups
,
2304 /* It had better be all done at compile-time. */
2305 gcc_assert (!expr
&& !cleanups
);
2309 DECL_EXTERNAL (vtbl
) = 0;
2310 rest_of_decl_compilation (vtbl
, 1, 1);
2312 /* Because we're only doing syntax-checking, we'll never end up
2313 actually marking the variable as written. */
2314 if (flag_syntax_only
)
2315 TREE_ASM_WRITTEN (vtbl
) = 1;
2316 else if (DECL_ONE_ONLY (vtbl
))
2318 current
= varpool_node::get_create (vtbl
);
2320 current
->add_to_same_comdat_group (last
);
2325 /* For abstract classes, the destructor has been removed from the
2326 vtable (in class.c's build_vtbl_initializer). For a compiler-
2327 generated destructor, it hence might not have been generated in
2328 this translation unit - and with '#pragma interface' it might
2329 never get generated. */
2330 if (CLASSTYPE_PURE_VIRTUALS (ctype
)
2331 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
)
2332 && !CLASSTYPE_LAZY_DESTRUCTOR (ctype
)
2333 && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype
)))
2334 note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype
));
2336 /* Since we're writing out the vtable here, also write the debug
2338 note_debug_info_needed (ctype
);
2343 /* A special return value from type_visibility meaning internal
2346 enum { VISIBILITY_ANON
= VISIBILITY_INTERNAL
+1 };
2348 static int expr_visibility (tree
);
2349 static int type_visibility (tree
);
2351 /* walk_tree helper function for type_visibility. */
2354 min_vis_r (tree
*tp
, int *walk_subtrees
, void *data
)
2356 int *vis_p
= (int *)data
;
2357 int this_vis
= VISIBILITY_DEFAULT
;
2360 else if (typedef_variant_p (*tp
))
2361 /* Look through typedefs despite cp_walk_subtrees. */
2362 this_vis
= type_visibility (DECL_ORIGINAL_TYPE (TYPE_NAME (*tp
)));
2363 else if (OVERLOAD_TYPE_P (*tp
)
2364 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp
)))
2366 this_vis
= VISIBILITY_ANON
;
2369 else if (CLASS_TYPE_P (*tp
))
2371 this_vis
= CLASSTYPE_VISIBILITY (*tp
);
2374 else if (TREE_CODE (*tp
) == ARRAY_TYPE
2375 && uses_template_parms (TYPE_DOMAIN (*tp
)))
2376 this_vis
= expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp
)));
2378 if (this_vis
> *vis_p
)
2384 /* walk_tree helper function for expr_visibility. */
2387 min_vis_expr_r (tree
*tp
, int */
*walk_subtrees*/
, void *data
)
2389 int *vis_p
= (int *)data
;
2390 int tpvis
= VISIBILITY_DEFAULT
;
2392 switch (TREE_CODE (*tp
))
2395 case IMPLICIT_CONV_EXPR
:
2396 case STATIC_CAST_EXPR
:
2397 case REINTERPRET_CAST_EXPR
:
2398 case CONST_CAST_EXPR
:
2399 case DYNAMIC_CAST_EXPR
:
2403 tpvis
= type_visibility (TREE_TYPE (*tp
));
2408 if (! TREE_PUBLIC (*tp
))
2409 tpvis
= VISIBILITY_ANON
;
2411 tpvis
= DECL_VISIBILITY (*tp
);
2424 /* Returns the visibility of TYPE, which is the minimum visibility of its
2428 type_visibility (tree type
)
2430 int vis
= VISIBILITY_DEFAULT
;
2431 cp_walk_tree_without_duplicates (&type
, min_vis_r
, &vis
);
2435 /* Returns the visibility of an expression EXPR that appears in the signature
2436 of a function template, which is the minimum visibility of names that appear
2440 expr_visibility (tree expr
)
2442 int vis
= VISIBILITY_DEFAULT
;
2443 cp_walk_tree_without_duplicates (&expr
, min_vis_expr_r
, &vis
);
2447 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2448 specified (or if VISIBILITY is static). If TMPL is true, this
2449 constraint is for a template argument, and takes precedence
2450 over explicitly-specified visibility on the template. */
2453 constrain_visibility (tree decl
, int visibility
, bool tmpl
)
2455 if (visibility
== VISIBILITY_ANON
)
2457 /* extern "C" declarations aren't affected by the anonymous
2459 if (!DECL_EXTERN_C_P (decl
))
2461 TREE_PUBLIC (decl
) = 0;
2462 DECL_WEAK (decl
) = 0;
2463 DECL_COMMON (decl
) = 0;
2464 DECL_COMDAT (decl
) = false;
2465 if (VAR_OR_FUNCTION_DECL_P (decl
))
2467 struct symtab_node
*snode
= symtab_node::get (decl
);
2470 snode
->set_comdat_group (NULL
);
2472 DECL_INTERFACE_KNOWN (decl
) = 1;
2473 if (DECL_LANG_SPECIFIC (decl
))
2474 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2477 else if (visibility
> DECL_VISIBILITY (decl
)
2478 && (tmpl
|| !DECL_VISIBILITY_SPECIFIED (decl
)))
2480 DECL_VISIBILITY (decl
) = (enum symbol_visibility
) visibility
;
2481 /* This visibility was not specified. */
2482 DECL_VISIBILITY_SPECIFIED (decl
) = false;
2486 /* Constrain the visibility of DECL based on the visibility of its template
2490 constrain_visibility_for_template (tree decl
, tree targs
)
2492 /* If this is a template instantiation, check the innermost
2493 template args for visibility constraints. The outer template
2494 args are covered by the class check. */
2495 tree args
= INNERMOST_TEMPLATE_ARGS (targs
);
2497 for (i
= TREE_VEC_LENGTH (args
); i
> 0; --i
)
2501 tree arg
= TREE_VEC_ELT (args
, i
-1);
2503 vis
= type_visibility (arg
);
2505 vis
= expr_visibility (arg
);
2507 constrain_visibility (decl
, vis
, true);
2511 /* Like c_determine_visibility, but with additional C++-specific
2514 Function-scope entities can rely on the function's visibility because
2515 it is set in start_preparsed_function.
2517 Class-scope entities cannot rely on the class's visibility until the end
2518 of the enclosing class definition.
2520 Note that because namespaces have multiple independent definitions,
2521 namespace visibility is handled elsewhere using the #pragma visibility
2522 machinery rather than by decorating the namespace declaration.
2524 The goal is for constraints from the type to give a diagnostic, and
2525 other constraints to be applied silently. */
2528 determine_visibility (tree decl
)
2530 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2532 /* Only relevant for names with external linkage. */
2533 if (!TREE_PUBLIC (decl
))
2536 /* Cloned constructors and destructors get the same visibility as
2537 the underlying function. That should be set up in
2538 maybe_clone_body. */
2539 gcc_assert (!DECL_CLONED_FUNCTION_P (decl
));
2541 bool orig_visibility_specified
= DECL_VISIBILITY_SPECIFIED (decl
);
2542 enum symbol_visibility orig_visibility
= DECL_VISIBILITY (decl
);
2544 /* The decl may be a template instantiation, which could influence
2546 tree template_decl
= NULL_TREE
;
2547 if (TREE_CODE (decl
) == TYPE_DECL
)
2549 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
2551 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)))
2552 template_decl
= decl
;
2554 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
2555 template_decl
= decl
;
2557 else if (DECL_LANG_SPECIFIC (decl
) && DECL_USE_TEMPLATE (decl
))
2558 template_decl
= decl
;
2560 if (TREE_CODE (decl
) == TYPE_DECL
2561 && LAMBDA_TYPE_P (TREE_TYPE (decl
))
2562 && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl
)) != error_mark_node
)
2563 if (tree extra
= LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl
)))
2565 /* The lambda's visibility is limited by that of its extra
2569 vis
= type_visibility (extra
);
2571 vis
= expr_visibility (extra
);
2572 constrain_visibility (decl
, vis
, false);
2575 /* If DECL is a member of a class, visibility specifiers on the
2576 class can influence the visibility of the DECL. */
2577 tree class_type
= NULL_TREE
;
2578 if (DECL_CLASS_SCOPE_P (decl
))
2579 class_type
= DECL_CONTEXT (decl
);
2582 /* Not a class member. */
2584 /* Virtual tables have DECL_CONTEXT set to their associated class,
2585 so they are automatically handled above. */
2586 gcc_assert (!VAR_P (decl
)
2587 || !DECL_VTABLE_OR_VTT_P (decl
));
2589 if (DECL_FUNCTION_SCOPE_P (decl
) && ! DECL_VISIBILITY_SPECIFIED (decl
))
2591 /* Local statics and classes get the visibility of their
2592 containing function by default, except that
2593 -fvisibility-inlines-hidden doesn't affect them. */
2594 tree fn
= DECL_CONTEXT (decl
);
2595 if (DECL_VISIBILITY_SPECIFIED (fn
))
2597 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2598 DECL_VISIBILITY_SPECIFIED (decl
) =
2599 DECL_VISIBILITY_SPECIFIED (fn
);
2603 if (DECL_CLASS_SCOPE_P (fn
))
2604 determine_visibility_from_class (decl
, DECL_CONTEXT (fn
));
2605 else if (determine_hidden_inline (fn
))
2607 DECL_VISIBILITY (decl
) = default_visibility
;
2608 DECL_VISIBILITY_SPECIFIED (decl
) =
2609 visibility_options
.inpragma
;
2613 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2614 DECL_VISIBILITY_SPECIFIED (decl
) =
2615 DECL_VISIBILITY_SPECIFIED (fn
);
2619 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2620 but have no TEMPLATE_INFO, so don't try to check it. */
2621 template_decl
= NULL_TREE
;
2623 else if (VAR_P (decl
) && DECL_TINFO_P (decl
)
2624 && flag_visibility_ms_compat
)
2626 /* Under -fvisibility-ms-compat, types are visible by default,
2627 even though their contents aren't. */
2628 tree underlying_type
= TREE_TYPE (DECL_NAME (decl
));
2629 int underlying_vis
= type_visibility (underlying_type
);
2630 if (underlying_vis
== VISIBILITY_ANON
2631 || (CLASS_TYPE_P (underlying_type
)
2632 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type
)))
2633 constrain_visibility (decl
, underlying_vis
, false);
2635 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
2637 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2639 /* tinfo visibility is based on the type it's for. */
2640 constrain_visibility
2641 (decl
, type_visibility (TREE_TYPE (DECL_NAME (decl
))), false);
2643 /* Give the target a chance to override the visibility associated
2645 if (TREE_PUBLIC (decl
)
2646 && !DECL_REALLY_EXTERN (decl
)
2647 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl
)))
2648 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl
))))
2649 targetm
.cxx
.determine_class_data_visibility (decl
);
2651 else if (template_decl
)
2652 /* Template instantiations and specializations get visibility based
2653 on their template unless they override it with an attribute. */;
2654 else if (! DECL_VISIBILITY_SPECIFIED (decl
))
2656 if (determine_hidden_inline (decl
))
2657 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2660 /* Set default visibility to whatever the user supplied with
2661 #pragma GCC visibility or a namespace visibility attribute. */
2662 DECL_VISIBILITY (decl
) = default_visibility
;
2663 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
2670 /* If the specialization doesn't specify visibility, use the
2671 visibility from the template. */
2672 tree tinfo
= get_template_info (template_decl
);
2673 tree args
= TI_ARGS (tinfo
);
2674 tree attribs
= (TREE_CODE (decl
) == TYPE_DECL
2675 ? TYPE_ATTRIBUTES (TREE_TYPE (decl
))
2676 : DECL_ATTRIBUTES (decl
));
2677 tree attr
= lookup_attribute ("visibility", attribs
);
2679 if (args
!= error_mark_node
)
2681 tree pattern
= DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo
));
2683 if (!DECL_VISIBILITY_SPECIFIED (decl
))
2686 && determine_hidden_inline (decl
))
2687 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2690 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (pattern
);
2691 DECL_VISIBILITY_SPECIFIED (decl
)
2692 = DECL_VISIBILITY_SPECIFIED (pattern
);
2697 /* Template argument visibility outweighs #pragma or namespace
2698 visibility, but not an explicit attribute. */
2701 int depth
= TMPL_ARGS_DEPTH (args
);
2702 if (DECL_VISIBILITY_SPECIFIED (decl
))
2704 /* A class template member with explicit visibility
2705 overrides the class visibility, so we need to apply
2706 all the levels of template args directly. */
2708 for (i
= 1; i
<= depth
; ++i
)
2710 tree lev
= TMPL_ARGS_LEVEL (args
, i
);
2711 constrain_visibility_for_template (decl
, lev
);
2714 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
2715 /* Limit visibility based on its template arguments. */
2716 constrain_visibility_for_template (decl
, args
);
2722 determine_visibility_from_class (decl
, class_type
);
2724 if (decl_anon_ns_mem_p (decl
))
2725 /* Names in an anonymous namespace get internal linkage. */
2726 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2727 else if (TREE_CODE (decl
) != TYPE_DECL
)
2729 /* Propagate anonymity from type to decl. */
2730 int tvis
= type_visibility (TREE_TYPE (decl
));
2731 if (tvis
== VISIBILITY_ANON
2732 || ! DECL_VISIBILITY_SPECIFIED (decl
))
2733 constrain_visibility (decl
, tvis
, false);
2735 else if (no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/true))
2736 /* DR 757: A type without linkage shall not be used as the type of a
2737 variable or function with linkage, unless
2738 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2739 o the variable or function is not used (3.2 [basic.def.odr]) or is
2740 defined in the same translation unit.
2742 Since non-extern "C" decls need to be defined in the same
2743 translation unit, we can make the type internal. */
2744 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2746 /* If visibility changed and DECL already has DECL_RTL, ensure
2747 symbol flags are updated. */
2748 if ((DECL_VISIBILITY (decl
) != orig_visibility
2749 || DECL_VISIBILITY_SPECIFIED (decl
) != orig_visibility_specified
)
2750 && ((VAR_P (decl
) && TREE_STATIC (decl
))
2751 || TREE_CODE (decl
) == FUNCTION_DECL
)
2752 && DECL_RTL_SET_P (decl
))
2753 make_decl_rtl (decl
);
2756 /* By default, static data members and function members receive
2757 the visibility of their containing class. */
2760 determine_visibility_from_class (tree decl
, tree class_type
)
2762 if (DECL_VISIBILITY_SPECIFIED (decl
))
2765 if (determine_hidden_inline (decl
))
2766 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2769 /* Default to the class visibility. */
2770 DECL_VISIBILITY (decl
) = CLASSTYPE_VISIBILITY (class_type
);
2771 DECL_VISIBILITY_SPECIFIED (decl
)
2772 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type
);
2775 /* Give the target a chance to override the visibility associated
2778 && TREE_PUBLIC (decl
)
2779 && (DECL_TINFO_P (decl
) || DECL_VTABLE_OR_VTT_P (decl
))
2780 && !DECL_REALLY_EXTERN (decl
)
2781 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type
))
2782 targetm
.cxx
.determine_class_data_visibility (decl
);
2785 /* Returns true iff DECL is an inline that should get hidden visibility
2786 because of -fvisibility-inlines-hidden. */
2789 determine_hidden_inline (tree decl
)
2791 return (visibility_options
.inlines_hidden
2792 /* Don't do this for inline templates; specializations might not be
2793 inline, and we don't want them to inherit the hidden
2794 visibility. We'll set it here for all inline instantiations. */
2795 && !processing_template_decl
2796 && TREE_CODE (decl
) == FUNCTION_DECL
2797 && DECL_DECLARED_INLINE_P (decl
)
2798 && (! DECL_LANG_SPECIFIC (decl
)
2799 || ! DECL_EXPLICIT_INSTANTIATION (decl
)));
2802 /* Constrain the visibility of a class TYPE based on the visibility of its
2803 field types. Warn if any fields require lesser visibility. */
2806 constrain_class_visibility (tree type
)
2812 int vis
= type_visibility (type
);
2814 if (vis
== VISIBILITY_ANON
2815 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type
)))
2818 /* Don't warn about visibility if the class has explicit visibility. */
2819 if (CLASSTYPE_VISIBILITY_SPECIFIED (type
))
2820 vis
= VISIBILITY_INTERNAL
;
2822 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
2823 if (TREE_CODE (t
) == FIELD_DECL
&& TREE_TYPE (t
) != error_mark_node
2824 && !DECL_ARTIFICIAL (t
))
2826 tree ftype
= strip_pointer_or_array_types (TREE_TYPE (t
));
2827 int subvis
= type_visibility (ftype
);
2829 if (subvis
== VISIBILITY_ANON
)
2831 if (!in_main_input_context())
2833 tree nlt
= no_linkage_check (ftype
, /*relaxed_p=*/false);
2836 if (same_type_p (TREE_TYPE (t
), nlt
))
2837 warning (OPT_Wsubobject_linkage
, "\
2838 %qT has a field %qD whose type has no linkage",
2841 warning (OPT_Wsubobject_linkage
, "\
2842 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2846 warning (OPT_Wsubobject_linkage
, "\
2847 %qT has a field %qD whose type uses the anonymous namespace",
2851 else if (MAYBE_CLASS_TYPE_P (ftype
)
2852 && vis
< VISIBILITY_HIDDEN
2853 && subvis
>= VISIBILITY_HIDDEN
)
2854 warning (OPT_Wattributes
, "\
2855 %qT declared with greater visibility than the type of its field %qD",
2859 binfo
= TYPE_BINFO (type
);
2860 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
2862 int subvis
= type_visibility (TREE_TYPE (t
));
2864 if (subvis
== VISIBILITY_ANON
)
2866 if (!in_main_input_context())
2868 tree nlt
= no_linkage_check (TREE_TYPE (t
), /*relaxed_p=*/false);
2871 if (same_type_p (TREE_TYPE (t
), nlt
))
2872 warning (OPT_Wsubobject_linkage
, "\
2873 %qT has a base %qT whose type has no linkage",
2874 type
, TREE_TYPE (t
));
2876 warning (OPT_Wsubobject_linkage
, "\
2877 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2878 type
, TREE_TYPE (t
), nlt
);
2881 warning (OPT_Wsubobject_linkage
, "\
2882 %qT has a base %qT whose type uses the anonymous namespace",
2883 type
, TREE_TYPE (t
));
2886 else if (vis
< VISIBILITY_HIDDEN
2887 && subvis
>= VISIBILITY_HIDDEN
)
2888 warning (OPT_Wattributes
, "\
2889 %qT declared with greater visibility than its base %qT",
2890 type
, TREE_TYPE (t
));
2894 /* Functions for adjusting the visibility of a tagged type and its nested
2895 types and declarations when it gets a name for linkage purposes from a
2898 static void bt_reset_linkage_1 (binding_entry
, void *);
2899 static void bt_reset_linkage_2 (binding_entry
, void *);
2901 /* First reset the visibility of all the types. */
2904 reset_type_linkage_1 (tree type
)
2906 set_linkage_according_to_type (type
, TYPE_MAIN_DECL (type
));
2907 if (CLASS_TYPE_P (type
))
2908 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2909 bt_reset_linkage_1
, NULL
);
2912 bt_reset_linkage_1 (binding_entry b
, void */
*data*/
)
2914 reset_type_linkage_1 (b
->type
);
2917 /* Then reset the visibility of any static data members or member
2918 functions that use those types. */
2921 reset_decl_linkage (tree decl
)
2923 if (TREE_PUBLIC (decl
))
2925 if (DECL_CLONED_FUNCTION_P (decl
))
2927 TREE_PUBLIC (decl
) = true;
2928 DECL_INTERFACE_KNOWN (decl
) = false;
2929 determine_visibility (decl
);
2930 tentative_decl_linkage (decl
);
2934 reset_type_linkage_2 (tree type
)
2936 if (CLASS_TYPE_P (type
))
2938 if (tree vt
= CLASSTYPE_VTABLES (type
))
2940 tree name
= mangle_vtbl_for_type (type
);
2941 DECL_NAME (vt
) = name
;
2942 SET_DECL_ASSEMBLER_NAME (vt
, name
);
2943 reset_decl_linkage (vt
);
2945 if (tree ti
= CLASSTYPE_TYPEINFO_VAR (type
))
2947 tree name
= mangle_typeinfo_for_type (type
);
2948 DECL_NAME (ti
) = name
;
2949 SET_DECL_ASSEMBLER_NAME (ti
, name
);
2950 TREE_TYPE (name
) = type
;
2951 reset_decl_linkage (ti
);
2953 for (tree m
= TYPE_FIELDS (type
); m
; m
= DECL_CHAIN (m
))
2955 tree mem
= STRIP_TEMPLATE (m
);
2956 if (TREE_CODE (mem
) == VAR_DECL
|| TREE_CODE (mem
) == FUNCTION_DECL
)
2957 reset_decl_linkage (mem
);
2959 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2960 bt_reset_linkage_2
, NULL
);
2965 bt_reset_linkage_2 (binding_entry b
, void */
*data*/
)
2967 reset_type_linkage_2 (b
->type
);
2970 reset_type_linkage (tree type
)
2972 reset_type_linkage_1 (type
);
2973 reset_type_linkage_2 (type
);
2976 /* Set up our initial idea of what the linkage of DECL should be. */
2979 tentative_decl_linkage (tree decl
)
2981 if (DECL_INTERFACE_KNOWN (decl
))
2982 /* We've already made a decision as to how this function will
2984 else if (vague_linkage_p (decl
))
2986 if (TREE_CODE (decl
) == FUNCTION_DECL
2987 && decl_defined_p (decl
))
2989 DECL_EXTERNAL (decl
) = 1;
2990 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2991 note_vague_linkage_fn (decl
);
2992 /* A non-template inline function with external linkage will
2993 always be COMDAT. As we must eventually determine the
2994 linkage of all functions, and as that causes writes to
2995 the data mapped in from the PCH file, it's advantageous
2996 to mark the functions at this point. */
2997 if (DECL_DECLARED_INLINE_P (decl
)
2998 && (!DECL_IMPLICIT_INSTANTIATION (decl
)
2999 || DECL_DEFAULTED_FN (decl
)))
3001 /* This function must have external linkage, as
3002 otherwise DECL_INTERFACE_KNOWN would have been
3004 gcc_assert (TREE_PUBLIC (decl
));
3005 comdat_linkage (decl
);
3006 DECL_INTERFACE_KNOWN (decl
) = 1;
3009 else if (VAR_P (decl
))
3010 maybe_commonize_var (decl
);
3014 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
3015 for DECL has not already been determined, do so now by setting
3016 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
3017 function is called entities with vague linkage whose definitions
3018 are available must have TREE_PUBLIC set.
3020 If this function decides to place DECL in COMDAT, it will set
3021 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
3022 the caller to decide whether or not to clear DECL_EXTERNAL. Some
3023 callers defer that decision until it is clear that DECL is actually
3027 import_export_decl (tree decl
)
3031 tree class_type
= NULL_TREE
;
3033 if (DECL_INTERFACE_KNOWN (decl
))
3036 /* We cannot determine what linkage to give to an entity with vague
3037 linkage until the end of the file. For example, a virtual table
3038 for a class will be defined if and only if the key method is
3039 defined in this translation unit. */
3040 gcc_assert (at_eof
);
3041 /* Object file linkage for explicit instantiations is handled in
3042 mark_decl_instantiated. For static variables in functions with
3043 vague linkage, maybe_commonize_var is used.
3045 Therefore, the only declarations that should be provided to this
3046 function are those with external linkage that are:
3048 * implicit instantiations of function templates
3052 * implicit instantiations of static data members of class
3059 Furthermore, all entities that reach this point must have a
3060 definition available in this translation unit.
3062 The following assertions check these conditions. */
3063 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
3064 /* Any code that creates entities with TREE_PUBLIC cleared should
3065 also set DECL_INTERFACE_KNOWN. */
3066 gcc_assert (TREE_PUBLIC (decl
));
3067 if (TREE_CODE (decl
) == FUNCTION_DECL
)
3068 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
3069 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
3070 || DECL_DECLARED_INLINE_P (decl
));
3072 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
3073 || DECL_VTABLE_OR_VTT_P (decl
)
3074 || DECL_TINFO_P (decl
));
3075 /* Check that a definition of DECL is available in this translation
3077 gcc_assert (!DECL_REALLY_EXTERN (decl
));
3079 /* Assume that DECL will not have COMDAT linkage. */
3081 /* Assume that DECL will not be imported into this translation
3085 if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
3087 class_type
= DECL_CONTEXT (decl
);
3088 import_export_class (class_type
);
3089 if (CLASSTYPE_INTERFACE_KNOWN (class_type
)
3090 && CLASSTYPE_INTERFACE_ONLY (class_type
))
3092 else if ((!flag_weak
|| TARGET_WEAK_NOT_IN_ARCHIVE_TOC
)
3093 && !CLASSTYPE_USE_TEMPLATE (class_type
)
3094 && CLASSTYPE_KEY_METHOD (class_type
)
3095 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
)))
3096 /* The ABI requires that all virtual tables be emitted with
3097 COMDAT linkage. However, on systems where COMDAT symbols
3098 don't show up in the table of contents for a static
3099 archive, or on systems without weak symbols (where we
3100 approximate COMDAT linkage by using internal linkage), the
3101 linker will report errors about undefined symbols because
3102 it will not see the virtual table definition. Therefore,
3103 in the case that we know that the virtual table will be
3104 emitted in only one translation unit, we make the virtual
3105 table an ordinary definition with external linkage. */
3106 DECL_EXTERNAL (decl
) = 0;
3107 else if (CLASSTYPE_INTERFACE_KNOWN (class_type
))
3109 /* CLASS_TYPE is being exported from this translation unit,
3110 so DECL should be defined here. */
3111 if (!flag_weak
&& CLASSTYPE_EXPLICIT_INSTANTIATION (class_type
))
3112 /* If a class is declared in a header with the "extern
3113 template" extension, then it will not be instantiated,
3114 even in translation units that would normally require
3115 it. Often such classes are explicitly instantiated in
3116 one translation unit. Therefore, the explicit
3117 instantiation must be made visible to other translation
3119 DECL_EXTERNAL (decl
) = 0;
3122 /* The generic C++ ABI says that class data is always
3123 COMDAT, even if there is a key function. Some
3124 variants (e.g., the ARM EABI) says that class data
3125 only has COMDAT linkage if the class data might be
3126 emitted in more than one translation unit. When the
3127 key method can be inline and is inline, we still have
3128 to arrange for comdat even though
3129 class_data_always_comdat is false. */
3130 if (!CLASSTYPE_KEY_METHOD (class_type
)
3131 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
))
3132 || targetm
.cxx
.class_data_always_comdat ())
3134 /* The ABI requires COMDAT linkage. Normally, we
3135 only emit COMDAT things when they are needed;
3136 make sure that we realize that this entity is
3143 else if (!flag_implicit_templates
3144 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type
))
3149 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
3151 tree type
= TREE_TYPE (DECL_NAME (decl
));
3152 if (CLASS_TYPE_P (type
))
3155 import_export_class (type
);
3156 if (CLASSTYPE_INTERFACE_KNOWN (type
)
3157 && TYPE_POLYMORPHIC_P (type
)
3158 && CLASSTYPE_INTERFACE_ONLY (type
)
3159 /* If -fno-rtti was specified, then we cannot be sure
3160 that RTTI information will be emitted with the
3161 virtual table of the class, so we must emit it
3162 wherever it is used. */
3167 if (CLASSTYPE_INTERFACE_KNOWN (type
)
3168 && !CLASSTYPE_INTERFACE_ONLY (type
))
3170 comdat_p
= (targetm
.cxx
.class_data_always_comdat ()
3171 || (CLASSTYPE_KEY_METHOD (type
)
3172 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type
))));
3177 DECL_EXTERNAL (decl
) = 0;
3187 else if (DECL_TEMPLOID_INSTANTIATION (decl
))
3189 /* DECL is an implicit instantiation of a function or static
3191 if (flag_implicit_templates
3192 || (flag_implicit_inline_templates
3193 && TREE_CODE (decl
) == FUNCTION_DECL
3194 && DECL_DECLARED_INLINE_P (decl
)))
3197 /* If we are not implicitly generating templates, then mark
3198 this entity as undefined in this translation unit. */
3201 else if (DECL_FUNCTION_MEMBER_P (decl
))
3203 if (!DECL_DECLARED_INLINE_P (decl
))
3205 tree ctype
= DECL_CONTEXT (decl
);
3206 import_export_class (ctype
);
3207 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
3209 DECL_NOT_REALLY_EXTERN (decl
)
3210 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
3211 || (DECL_DECLARED_INLINE_P (decl
)
3212 && ! flag_implement_inlines
3213 && !DECL_VINDEX (decl
)));
3215 if (!DECL_NOT_REALLY_EXTERN (decl
))
3216 DECL_EXTERNAL (decl
) = 1;
3218 /* Always make artificials weak. */
3219 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
3222 maybe_make_one_only (decl
);
3233 /* If we are importing DECL into this translation unit, mark is
3234 an undefined here. */
3235 DECL_EXTERNAL (decl
) = 1;
3236 DECL_NOT_REALLY_EXTERN (decl
) = 0;
3240 /* If we decided to put DECL in COMDAT, mark it accordingly at
3242 comdat_linkage (decl
);
3245 DECL_INTERFACE_KNOWN (decl
) = 1;
3248 /* Return an expression that performs the destruction of DECL, which
3249 must be a VAR_DECL whose type has a non-trivial destructor, or is
3250 an array whose (innermost) elements have a non-trivial destructor. */
3253 build_cleanup (tree decl
)
3255 tree clean
= cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
3256 gcc_assert (clean
!= NULL_TREE
);
3260 /* GUARD is a helper variable for DECL; make them have the same linkage and
3264 copy_linkage (tree guard
, tree decl
)
3266 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
3267 TREE_STATIC (guard
) = TREE_STATIC (decl
);
3268 DECL_COMMON (guard
) = DECL_COMMON (decl
);
3269 DECL_COMDAT (guard
) = DECL_COMDAT (decl
);
3270 if (TREE_STATIC (guard
))
3272 CP_DECL_THREAD_LOCAL_P (guard
) = CP_DECL_THREAD_LOCAL_P (decl
);
3273 set_decl_tls_model (guard
, DECL_TLS_MODEL (decl
));
3274 if (DECL_ONE_ONLY (decl
))
3275 make_decl_one_only (guard
, cxx_comdat_group (guard
));
3276 if (TREE_PUBLIC (decl
))
3277 DECL_WEAK (guard
) = DECL_WEAK (decl
);
3278 /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not
3279 be set until import_export_decl at EOF. */
3280 if (vague_linkage_p (decl
))
3281 comdat_linkage (guard
);
3282 DECL_VISIBILITY (guard
) = DECL_VISIBILITY (decl
);
3283 DECL_VISIBILITY_SPECIFIED (guard
) = DECL_VISIBILITY_SPECIFIED (decl
);
3287 /* Returns the initialization guard variable for the variable DECL,
3288 which has static storage duration. */
3291 get_guard (tree decl
)
3293 tree sname
= mangle_guard_variable (decl
);
3294 tree guard
= get_global_binding (sname
);
3299 /* We use a type that is big enough to contain a mutex as well
3300 as an integer counter. */
3301 guard_type
= targetm
.cxx
.guard_type ();
3302 guard
= build_decl (DECL_SOURCE_LOCATION (decl
),
3303 VAR_DECL
, sname
, guard_type
);
3305 /* The guard should have the same linkage as what it guards. */
3306 copy_linkage (guard
, decl
);
3308 DECL_ARTIFICIAL (guard
) = 1;
3309 DECL_IGNORED_P (guard
) = 1;
3310 TREE_USED (guard
) = 1;
3311 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
3316 /* Return an atomic load of src with the appropriate memory model. */
3319 build_atomic_load_byte (tree src
, HOST_WIDE_INT model
)
3321 tree ptr_type
= build_pointer_type (char_type_node
);
3322 tree mem_model
= build_int_cst (integer_type_node
, model
);
3327 size
= tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node
));
3329 fncode
= BUILT_IN_ATOMIC_LOAD_N
+ exact_log2 (size
) + 1;
3330 t
= builtin_decl_implicit ((enum built_in_function
) fncode
);
3332 addr
= build1 (ADDR_EXPR
, ptr_type
, src
);
3333 val
= build_call_expr (t
, 2, addr
, mem_model
);
3337 /* Return those bits of the GUARD variable that should be set when the
3338 guarded entity is actually initialized. */
3341 get_guard_bits (tree guard
)
3343 if (!targetm
.cxx
.guard_mask_bit ())
3345 /* We only set the first byte of the guard, in order to leave room
3346 for a mutex in the high-order bits. */
3347 guard
= build1 (ADDR_EXPR
,
3348 build_pointer_type (TREE_TYPE (guard
)),
3350 guard
= build1 (NOP_EXPR
,
3351 build_pointer_type (char_type_node
),
3353 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
3359 /* Return an expression which determines whether or not the GUARD
3360 variable has already been initialized. */
3363 get_guard_cond (tree guard
, bool thread_safe
)
3368 guard
= get_guard_bits (guard
);
3370 guard
= build_atomic_load_byte (guard
, MEMMODEL_ACQUIRE
);
3372 /* Mask off all but the low bit. */
3373 if (targetm
.cxx
.guard_mask_bit ())
3375 guard_value
= integer_one_node
;
3376 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3377 guard_value
= fold_convert (TREE_TYPE (guard
), guard_value
);
3378 guard
= cp_build_binary_op (input_location
,
3379 BIT_AND_EXPR
, guard
, guard_value
,
3380 tf_warning_or_error
);
3383 guard_value
= integer_zero_node
;
3384 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3385 guard_value
= fold_convert (TREE_TYPE (guard
), guard_value
);
3386 return cp_build_binary_op (input_location
,
3387 EQ_EXPR
, guard
, guard_value
,
3388 tf_warning_or_error
);
3391 /* Return an expression which sets the GUARD variable, indicating that
3392 the variable being guarded has been initialized. */
3395 set_guard (tree guard
)
3399 /* Set the GUARD to one. */
3400 guard
= get_guard_bits (guard
);
3401 guard_init
= integer_one_node
;
3402 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
3403 guard_init
= fold_convert (TREE_TYPE (guard
), guard_init
);
3404 return cp_build_modify_expr (input_location
, guard
, NOP_EXPR
, guard_init
,
3405 tf_warning_or_error
);
3408 /* Returns true iff we can tell that VAR does not have a dynamic
3412 var_defined_without_dynamic_init (tree var
)
3414 /* If it's defined in another TU, we can't tell. */
3415 if (DECL_EXTERNAL (var
))
3417 /* If it has a non-trivial destructor, registering the destructor
3418 counts as dynamic initialization. */
3419 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var
)))
3421 /* If it's in this TU, its initializer has been processed, unless
3422 it's a case of self-initialization, then DECL_INITIALIZED_P is
3423 false while the initializer is handled by finish_id_expression. */
3424 if (!DECL_INITIALIZED_P (var
))
3426 /* If it has no initializer or a constant one, it's not dynamic. */
3427 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var
)
3428 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var
));
3431 /* Returns true iff VAR is a variable that needs uses to be
3432 wrapped for possible dynamic initialization. */
3435 var_needs_tls_wrapper (tree var
)
3437 return (!error_operand_p (var
)
3438 && CP_DECL_THREAD_LOCAL_P (var
)
3439 && !DECL_GNU_TLS_P (var
)
3440 && !DECL_FUNCTION_SCOPE_P (var
)
3441 && !var_defined_without_dynamic_init (var
));
3444 /* Get the FUNCTION_DECL for the shared TLS init function for this
3445 translation unit. */
3448 get_local_tls_init_fn (location_t loc
)
3450 tree sname
= get_identifier ("__tls_init");
3451 tree fn
= get_global_binding (sname
);
3454 fn
= build_lang_decl_loc (loc
, FUNCTION_DECL
, sname
,
3455 build_function_type (void_type_node
,
3457 SET_DECL_LANGUAGE (fn
, lang_c
);
3458 TREE_PUBLIC (fn
) = false;
3459 DECL_ARTIFICIAL (fn
) = true;
3461 set_global_binding (fn
);
3466 /* Get a FUNCTION_DECL for the init function for the thread_local
3467 variable VAR. The init function will be an alias to the function
3468 that initializes all the non-local TLS variables in the translation
3469 unit. The init function is only used by the wrapper function. */
3472 get_tls_init_fn (tree var
)
3474 /* Only C++11 TLS vars need this init fn. */
3475 if (!var_needs_tls_wrapper (var
))
3478 /* If -fno-extern-tls-init, assume that we don't need to call
3479 a tls init function for a variable defined in another TU. */
3480 if (!flag_extern_tls_init
&& DECL_EXTERNAL (var
))
3483 /* If the variable is internal, or if we can't generate aliases,
3484 call the local init function directly. */
3485 if (!TREE_PUBLIC (var
) || !TARGET_SUPPORTS_ALIASES
)
3486 return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var
));
3488 tree sname
= mangle_tls_init_fn (var
);
3489 tree fn
= get_global_binding (sname
);
3492 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
3493 build_function_type (void_type_node
,
3495 SET_DECL_LANGUAGE (fn
, lang_c
);
3496 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3497 DECL_ARTIFICIAL (fn
) = true;
3498 DECL_COMDAT (fn
) = DECL_COMDAT (var
);
3499 DECL_EXTERNAL (fn
) = DECL_EXTERNAL (var
);
3500 if (DECL_ONE_ONLY (var
))
3501 make_decl_one_only (fn
, cxx_comdat_group (fn
));
3502 if (TREE_PUBLIC (var
))
3504 tree obtype
= strip_array_types (non_reference (TREE_TYPE (var
)));
3505 /* If the variable is defined somewhere else and might have static
3506 initialization, make the init function a weak reference. */
3507 if ((!TYPE_NEEDS_CONSTRUCTING (obtype
)
3508 || TYPE_HAS_CONSTEXPR_CTOR (obtype
)
3509 || TYPE_HAS_TRIVIAL_DFLT (obtype
))
3510 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype
)
3511 && DECL_EXTERNAL (var
))
3514 DECL_WEAK (fn
) = DECL_WEAK (var
);
3516 DECL_VISIBILITY (fn
) = DECL_VISIBILITY (var
);
3517 DECL_VISIBILITY_SPECIFIED (fn
) = DECL_VISIBILITY_SPECIFIED (var
);
3518 DECL_DLLIMPORT_P (fn
) = DECL_DLLIMPORT_P (var
);
3519 DECL_IGNORED_P (fn
) = 1;
3522 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3524 set_global_binding (fn
);
3529 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3530 variable VAR. The wrapper function calls the init function (if any) for
3531 VAR and then returns a reference to VAR. The wrapper function is used
3532 in place of VAR everywhere VAR is mentioned. */
3535 get_tls_wrapper_fn (tree var
)
3537 /* Only C++11 TLS vars need this wrapper fn. */
3538 if (!var_needs_tls_wrapper (var
))
3541 tree sname
= mangle_tls_wrapper_fn (var
);
3542 tree fn
= get_global_binding (sname
);
3545 /* A named rvalue reference is an lvalue, so the wrapper should
3546 always return an lvalue reference. */
3547 tree type
= non_reference (TREE_TYPE (var
));
3548 type
= build_reference_type (type
);
3549 tree fntype
= build_function_type (type
, void_list_node
);
3551 fn
= build_lang_decl_loc (DECL_SOURCE_LOCATION (var
),
3552 FUNCTION_DECL
, sname
, fntype
);
3553 SET_DECL_LANGUAGE (fn
, lang_c
);
3554 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3555 DECL_ARTIFICIAL (fn
) = true;
3556 DECL_IGNORED_P (fn
) = 1;
3557 /* The wrapper is inline and emitted everywhere var is used. */
3558 DECL_DECLARED_INLINE_P (fn
) = true;
3559 if (TREE_PUBLIC (var
))
3561 comdat_linkage (fn
);
3562 #ifdef HAVE_GAS_HIDDEN
3563 /* Make the wrapper bind locally; there's no reason to share
3564 the wrapper between multiple shared objects. */
3565 DECL_VISIBILITY (fn
) = VISIBILITY_INTERNAL
;
3566 DECL_VISIBILITY_SPECIFIED (fn
) = true;
3569 if (!TREE_PUBLIC (fn
))
3570 DECL_INTERFACE_KNOWN (fn
) = true;
3572 note_vague_linkage_fn (fn
);
3575 /* We want CSE to commonize calls to the wrapper, but marking it as
3576 pure is unsafe since it has side-effects. I guess we need a new
3577 ECF flag even weaker than ECF_PURE. FIXME! */
3578 DECL_PURE_P (fn
) = true;
3581 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3583 set_global_binding (fn
);
3588 /* If EXPR is a thread_local variable that should be wrapped by init
3589 wrapper function, return a call to that function, otherwise return
3593 maybe_get_tls_wrapper_call (tree expr
)
3596 && !processing_template_decl
3597 && !cp_unevaluated_operand
3598 && CP_DECL_THREAD_LOCAL_P (expr
))
3599 if (tree wrap
= get_tls_wrapper_fn (expr
))
3600 return build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
3604 /* At EOF, generate the definition for the TLS wrapper function FN:
3607 if (init_fn) init_fn();
3612 generate_tls_wrapper (tree fn
)
3614 tree var
= DECL_BEFRIENDING_CLASSES (fn
);
3616 start_preparsed_function (fn
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
3617 tree body
= begin_function_body ();
3618 /* Only call the init fn if there might be one. */
3619 if (tree init_fn
= get_tls_init_fn (var
))
3621 tree if_stmt
= NULL_TREE
;
3622 /* If init_fn is a weakref, make sure it exists before calling. */
3623 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn
)))
3625 if_stmt
= begin_if_stmt ();
3626 tree addr
= cp_build_addr_expr (init_fn
, tf_warning_or_error
);
3627 tree cond
= cp_build_binary_op (DECL_SOURCE_LOCATION (var
),
3628 NE_EXPR
, addr
, nullptr_node
,
3629 tf_warning_or_error
);
3630 finish_if_stmt_cond (cond
, if_stmt
);
3632 finish_expr_stmt (build_cxx_call
3633 (init_fn
, 0, NULL
, tf_warning_or_error
));
3636 finish_then_clause (if_stmt
);
3637 finish_if_stmt (if_stmt
);
3641 /* If there's no initialization, the wrapper is a constant function. */
3642 TREE_READONLY (fn
) = true;
3643 finish_return_stmt (convert_from_reference (var
));
3644 finish_function_body (body
);
3645 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3648 /* Start the process of running a particular set of global constructors
3649 or destructors. Subroutine of do_[cd]tors. Also called from
3650 vtv_start_verification_constructor_init_function. */
3653 start_objects (int method_type
, int initp
)
3659 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3661 if (initp
!= DEFAULT_INIT_PRIORITY
)
3671 sprintf (type
, "sub_%c%c%.5u", method_type
, joiner
, initp
);
3674 sprintf (type
, "sub_%c", method_type
);
3676 fndecl
= build_lang_decl (FUNCTION_DECL
,
3677 get_file_function_name (type
),
3678 build_function_type_list (void_type_node
,
3680 start_preparsed_function (fndecl
, /*attrs=*/NULL_TREE
, SF_PRE_PARSED
);
3682 TREE_PUBLIC (current_function_decl
) = 0;
3684 /* Mark as artificial because it's not explicitly in the user's
3686 DECL_ARTIFICIAL (current_function_decl
) = 1;
3688 /* Mark this declaration as used to avoid spurious warnings. */
3689 TREE_USED (current_function_decl
) = 1;
3691 /* Mark this function as a global constructor or destructor. */
3692 if (method_type
== 'I')
3693 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
3695 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
3697 body
= begin_compound_stmt (BCS_FN_BODY
);
3702 /* Finish the process of running a particular set of global constructors
3703 or destructors. Subroutine of do_[cd]tors. */
3706 finish_objects (int method_type
, int initp
, tree body
)
3711 finish_compound_stmt (body
);
3712 fn
= finish_function (/*inline_p=*/false);
3714 if (method_type
== 'I')
3716 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
3717 decl_init_priority_insert (fn
, initp
);
3721 DECL_STATIC_DESTRUCTOR (fn
) = 1;
3722 decl_fini_priority_insert (fn
, initp
);
3725 expand_or_defer_fn (fn
);
3728 /* The names of the parameters to the function created to handle
3729 initializations and destructions for objects with static storage
3731 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3732 #define PRIORITY_IDENTIFIER "__priority"
3734 /* The name of the function we create to handle initializations and
3735 destructions for objects with static storage duration. */
3736 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3738 /* The declaration for the __INITIALIZE_P argument. */
3739 static GTY(()) tree initialize_p_decl
;
3741 /* The declaration for the __PRIORITY argument. */
3742 static GTY(()) tree priority_decl
;
3744 /* The declaration for the static storage duration function. */
3745 static GTY(()) tree ssdf_decl
;
3747 /* All the static storage duration functions created in this
3748 translation unit. */
3749 static GTY(()) vec
<tree
, va_gc
> *ssdf_decls
;
3751 /* A map from priority levels to information about that priority
3752 level. There may be many such levels, so efficient lookup is
3754 static splay_tree priority_info_map
;
3756 /* Begins the generation of the function that will handle all
3757 initialization and destruction of objects with static storage
3758 duration. The function generated takes two parameters of type
3759 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3760 nonzero, it performs initializations. Otherwise, it performs
3761 destructions. It only performs those initializations or
3762 destructions with the indicated __PRIORITY. The generated function
3765 It is assumed that this function will only be called once per
3766 translation unit. */
3769 start_static_storage_duration_function (unsigned count
)
3773 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
3775 /* Create the identifier for this function. It will be of the form
3776 SSDF_IDENTIFIER_<number>. */
3777 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, count
);
3779 type
= build_function_type_list (void_type_node
,
3780 integer_type_node
, integer_type_node
,
3783 /* Create the FUNCTION_DECL itself. */
3784 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
3785 get_identifier (id
),
3787 TREE_PUBLIC (ssdf_decl
) = 0;
3788 DECL_ARTIFICIAL (ssdf_decl
) = 1;
3790 /* Put this function in the list of functions to be called from the
3791 static constructors and destructors. */
3794 vec_alloc (ssdf_decls
, 32);
3796 /* Take this opportunity to initialize the map from priority
3797 numbers to information about that priority level. */
3798 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
3799 /*delete_key_fn=*/0,
3800 /*delete_value_fn=*/
3801 splay_tree_delete_pointers
);
3803 /* We always need to generate functions for the
3804 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3805 priorities later, we'll be sure to find the
3806 DEFAULT_INIT_PRIORITY. */
3807 get_priority_info (DEFAULT_INIT_PRIORITY
);
3810 vec_safe_push (ssdf_decls
, ssdf_decl
);
3812 /* Create the argument list. */
3813 initialize_p_decl
= cp_build_parm_decl
3814 (ssdf_decl
, get_identifier (INITIALIZE_P_IDENTIFIER
), integer_type_node
);
3815 TREE_USED (initialize_p_decl
) = 1;
3816 priority_decl
= cp_build_parm_decl
3817 (ssdf_decl
, get_identifier (PRIORITY_IDENTIFIER
), integer_type_node
);
3818 TREE_USED (priority_decl
) = 1;
3820 DECL_CHAIN (initialize_p_decl
) = priority_decl
;
3821 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
3823 /* Put the function in the global scope. */
3824 pushdecl (ssdf_decl
);
3826 /* Start the function itself. This is equivalent to declaring the
3829 static void __ssdf (int __initialize_p, init __priority_p);
3831 It is static because we only need to call this function from the
3832 various constructor and destructor functions for this module. */
3833 start_preparsed_function (ssdf_decl
,
3834 /*attrs=*/NULL_TREE
,
3837 /* Set up the scope of the outermost block in the function. */
3838 body
= begin_compound_stmt (BCS_FN_BODY
);
3843 /* Finish the generation of the function which performs initialization
3844 and destruction of objects with static storage duration. After
3845 this point, no more such objects can be created. */
3848 finish_static_storage_duration_function (tree body
)
3850 /* Close out the function. */
3851 finish_compound_stmt (body
);
3852 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3855 /* Return the information about the indicated PRIORITY level. If no
3856 code to handle this level has yet been generated, generate the
3857 appropriate prologue. */
3859 static priority_info
3860 get_priority_info (int priority
)
3865 n
= splay_tree_lookup (priority_info_map
,
3866 (splay_tree_key
) priority
);
3869 /* Create a new priority information structure, and insert it
3871 pi
= XNEW (struct priority_info_s
);
3872 pi
->initializations_p
= 0;
3873 pi
->destructions_p
= 0;
3874 splay_tree_insert (priority_info_map
,
3875 (splay_tree_key
) priority
,
3876 (splay_tree_value
) pi
);
3879 pi
= (priority_info
) n
->value
;
3884 /* The effective initialization priority of a DECL. */
3886 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3887 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3888 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3890 /* Whether a DECL needs a guard to protect it against multiple
3893 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3894 || DECL_ONE_ONLY (decl) \
3895 || DECL_WEAK (decl)))
3897 /* Called from one_static_initialization_or_destruction(),
3899 Walks the initializer list of a global variable and looks for
3900 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3901 and that have their DECL_CONTEXT() == NULL.
3902 For each such temporary variable, set their DECL_CONTEXT() to
3903 the current function. This is necessary because otherwise
3904 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3905 when trying to refer to a temporary variable that does not have
3906 it's DECL_CONTECT() properly set. */
3908 fix_temporary_vars_context_r (tree
*node
,
3912 gcc_assert (current_function_decl
);
3914 if (TREE_CODE (*node
) == BIND_EXPR
)
3918 for (var
= BIND_EXPR_VARS (*node
); var
; var
= DECL_CHAIN (var
))
3921 && DECL_ARTIFICIAL (var
)
3922 && !DECL_CONTEXT (var
))
3923 DECL_CONTEXT (var
) = current_function_decl
;
3929 /* Set up to handle the initialization or destruction of DECL. If
3930 INITP is nonzero, we are initializing the variable. Otherwise, we
3931 are destroying it. */
3934 one_static_initialization_or_destruction (tree decl
, tree init
, bool initp
)
3936 tree guard_if_stmt
= NULL_TREE
;
3939 /* If we are supposed to destruct and there's a trivial destructor,
3940 nothing has to be done. */
3942 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3945 /* Trick the compiler into thinking we are at the file and line
3946 where DECL was declared so that error-messages make sense, and so
3947 that the debugger will show somewhat sensible file and line
3949 input_location
= DECL_SOURCE_LOCATION (decl
);
3951 /* Make sure temporary variables in the initialiser all have
3952 their DECL_CONTEXT() set to a value different from NULL_TREE.
3953 This can happen when global variables initializers are built.
3954 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3955 the temporary variables that might have been generated in the
3956 accompanying initializers is NULL_TREE, meaning the variables have been
3957 declared in the global namespace.
3958 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3959 of the temporaries are set to the current function decl. */
3960 cp_walk_tree_without_duplicates (&init
,
3961 fix_temporary_vars_context_r
,
3968 Access control for implicit calls to the constructors,
3969 the conversion functions, or the destructor called to
3970 create and destroy a static data member is performed as
3971 if these calls appeared in the scope of the member's
3974 we pretend we are in a static member function of the class of
3975 which the DECL is a member. */
3976 if (member_p (decl
))
3978 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
3979 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
3982 /* Assume we don't need a guard. */
3984 /* We need a guard if this is an object with external linkage that
3985 might be initialized in more than one place. (For example, a
3986 static data member of a template, when the data member requires
3988 if (NEEDS_GUARD_P (decl
))
3992 guard
= get_guard (decl
);
3994 /* When using __cxa_atexit, we just check the GUARD as we would
3995 for a local static. */
3996 if (flag_use_cxa_atexit
)
3998 /* When using __cxa_atexit, we never try to destroy
3999 anything from a static destructor. */
4001 guard_cond
= get_guard_cond (guard
, false);
4003 /* If we don't have __cxa_atexit, then we will be running
4004 destructors from .fini sections, or their equivalents. So,
4005 we need to know how many times we've tried to initialize this
4006 object. We do initializations only if the GUARD is zero,
4007 i.e., if we are the first to initialize the variable. We do
4008 destructions only if the GUARD is one, i.e., if we are the
4009 last to destroy the variable. */
4012 = cp_build_binary_op (input_location
,
4014 cp_build_unary_op (PREINCREMENT_EXPR
,
4017 tf_warning_or_error
),
4019 tf_warning_or_error
);
4022 = cp_build_binary_op (input_location
,
4024 cp_build_unary_op (PREDECREMENT_EXPR
,
4027 tf_warning_or_error
),
4029 tf_warning_or_error
);
4031 guard_if_stmt
= begin_if_stmt ();
4032 finish_if_stmt_cond (guard_cond
, guard_if_stmt
);
4036 /* If we're using __cxa_atexit, we have not already set the GUARD,
4037 so we must do so now. */
4038 if (guard
&& initp
&& flag_use_cxa_atexit
)
4039 finish_expr_stmt (set_guard (guard
));
4041 /* Perform the initialization or destruction. */
4046 finish_expr_stmt (init
);
4047 if (sanitize_flags_p (SANITIZE_ADDRESS
, decl
))
4049 varpool_node
*vnode
= varpool_node::get (decl
);
4051 vnode
->dynamically_initialized
= 1;
4055 /* If we're using __cxa_atexit, register a function that calls the
4056 destructor for the object. */
4057 if (flag_use_cxa_atexit
)
4058 finish_expr_stmt (register_dtor_fn (decl
));
4061 finish_expr_stmt (build_cleanup (decl
));
4063 /* Finish the guard if-stmt, if necessary. */
4066 finish_then_clause (guard_if_stmt
);
4067 finish_if_stmt (guard_if_stmt
);
4070 /* Now that we're done with DECL we don't need to pretend to be a
4071 member of its class any longer. */
4072 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
4073 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
4076 /* Generate code to do the initialization or destruction of the decls in VARS,
4077 a TREE_LIST of VAR_DECL with static storage duration.
4078 Whether initialization or destruction is performed is specified by INITP. */
4081 do_static_initialization_or_destruction (tree vars
, bool initp
)
4083 tree node
, init_if_stmt
, cond
;
4085 /* Build the outer if-stmt to check for initialization or destruction. */
4086 init_if_stmt
= begin_if_stmt ();
4087 cond
= initp
? integer_one_node
: integer_zero_node
;
4088 cond
= cp_build_binary_op (input_location
,
4092 tf_warning_or_error
);
4093 finish_if_stmt_cond (cond
, init_if_stmt
);
4095 /* To make sure dynamic construction doesn't access globals from other
4096 compilation units where they might not be yet constructed, for
4097 -fsanitize=address insert __asan_before_dynamic_init call that
4098 prevents access to either all global variables that need construction
4099 in other compilation units, or at least those that haven't been
4100 initialized yet. Variables that need dynamic construction in
4101 the current compilation unit are kept accessible. */
4102 if (initp
&& (flag_sanitize
& SANITIZE_ADDRESS
))
4103 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
4107 tree decl
= TREE_VALUE (node
);
4108 tree priority_if_stmt
;
4112 /* If we don't need a destructor, there's nothing to do. Avoid
4113 creating a possibly empty if-stmt. */
4114 if (!initp
&& TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
4116 node
= TREE_CHAIN (node
);
4120 /* Remember that we had an initialization or finalization at this
4122 priority
= DECL_EFFECTIVE_INIT_PRIORITY (decl
);
4123 pi
= get_priority_info (priority
);
4125 pi
->initializations_p
= 1;
4127 pi
->destructions_p
= 1;
4129 /* Conditionalize this initialization on being in the right priority
4130 and being initializing/finalizing appropriately. */
4131 priority_if_stmt
= begin_if_stmt ();
4132 cond
= cp_build_binary_op (input_location
,
4135 build_int_cst (NULL_TREE
, priority
),
4136 tf_warning_or_error
);
4137 finish_if_stmt_cond (cond
, priority_if_stmt
);
4139 /* Process initializers with same priority. */
4141 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node
)) == priority
;
4142 node
= TREE_CHAIN (node
))
4143 /* Do one initialization or destruction. */
4144 one_static_initialization_or_destruction (TREE_VALUE (node
),
4145 TREE_PURPOSE (node
), initp
);
4147 /* Finish up the priority if-stmt body. */
4148 finish_then_clause (priority_if_stmt
);
4149 finish_if_stmt (priority_if_stmt
);
4153 /* Revert what __asan_before_dynamic_init did by calling
4154 __asan_after_dynamic_init. */
4155 if (initp
&& (flag_sanitize
& SANITIZE_ADDRESS
))
4156 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4158 /* Finish up the init/destruct if-stmt body. */
4159 finish_then_clause (init_if_stmt
);
4160 finish_if_stmt (init_if_stmt
);
4163 /* VARS is a list of variables with static storage duration which may
4164 need initialization and/or finalization. Remove those variables
4165 that don't really need to be initialized or finalized, and return
4166 the resulting list. The order in which the variables appear in
4167 VARS is in reverse order of the order in which they should actually
4168 be initialized. The list we return is in the unreversed order;
4169 i.e., the first variable should be initialized first. */
4172 prune_vars_needing_no_initialization (tree
*vars
)
4175 tree result
= NULL_TREE
;
4180 tree decl
= TREE_VALUE (t
);
4181 tree init
= TREE_PURPOSE (t
);
4183 /* Deal gracefully with error. */
4184 if (error_operand_p (decl
))
4186 var
= &TREE_CHAIN (t
);
4190 /* The only things that can be initialized are variables. */
4191 gcc_assert (VAR_P (decl
));
4193 /* If this object is not defined, we don't need to do anything
4195 if (DECL_EXTERNAL (decl
))
4197 var
= &TREE_CHAIN (t
);
4201 /* Also, if the initializer already contains errors, we can bail
4203 if (init
&& TREE_CODE (init
) == TREE_LIST
4204 && value_member (error_mark_node
, init
))
4206 var
= &TREE_CHAIN (t
);
4210 /* This variable is going to need initialization and/or
4211 finalization, so we add it to the list. */
4212 *var
= TREE_CHAIN (t
);
4213 TREE_CHAIN (t
) = result
;
4220 /* Make sure we have told the back end about all the variables in
4224 write_out_vars (tree vars
)
4228 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
4230 tree var
= TREE_VALUE (v
);
4231 if (!var_finalized_p (var
))
4233 import_export_decl (var
);
4234 rest_of_decl_compilation (var
, 1, 1);
4239 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4240 (otherwise) that will initialize all global objects with static
4241 storage duration having the indicated PRIORITY. */
4244 generate_ctor_or_dtor_function (bool constructor_p
, int priority
,
4252 input_location
= *locus
;
4254 /* Was: locus->line++; */
4256 /* We use `I' to indicate initialization and `D' to indicate
4258 function_key
= constructor_p
? 'I' : 'D';
4260 /* We emit the function lazily, to avoid generating empty
4261 global constructors and destructors. */
4264 /* For Objective-C++, we may need to initialize metadata found in this module.
4265 This must be done _before_ any other static initializations. */
4266 if (c_dialect_objc () && (priority
== DEFAULT_INIT_PRIORITY
)
4267 && constructor_p
&& objc_static_init_needed_p ())
4269 body
= start_objects (function_key
, priority
);
4270 objc_generate_static_init_call (NULL_TREE
);
4273 /* Call the static storage duration function with appropriate
4275 FOR_EACH_VEC_SAFE_ELT (ssdf_decls
, i
, fndecl
)
4277 /* Calls to pure or const functions will expand to nothing. */
4278 if (! (flags_from_decl_or_type (fndecl
) & (ECF_CONST
| ECF_PURE
)))
4283 body
= start_objects (function_key
, priority
);
4285 call
= cp_build_function_call_nary (fndecl
, tf_warning_or_error
,
4286 build_int_cst (NULL_TREE
,
4288 build_int_cst (NULL_TREE
,
4291 finish_expr_stmt (call
);
4295 /* Close out the function. */
4297 finish_objects (function_key
, priority
, body
);
4300 /* Generate constructor and destructor functions for the priority
4304 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n
, void * data
)
4306 location_t
*locus
= (location_t
*) data
;
4307 int priority
= (int) n
->key
;
4308 priority_info pi
= (priority_info
) n
->value
;
4310 /* Generate the functions themselves, but only if they are really
4312 if (pi
->initializations_p
)
4313 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority
, locus
);
4314 if (pi
->destructions_p
)
4315 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority
, locus
);
4317 /* Keep iterating. */
4321 /* Return C++ property of T, based on given operation OP. */
4324 cpp_check (tree t
, cpp_operation op
)
4328 case HAS_DEPENDENT_TEMPLATE_ARGS
:
4330 tree ti
= CLASSTYPE_TEMPLATE_INFO (t
);
4333 ++processing_template_decl
;
4334 const bool dep
= any_dependent_template_arguments_p (TI_ARGS (ti
));
4335 --processing_template_decl
;
4339 return DECL_PURE_VIRTUAL_P (t
);
4340 case IS_ASSIGNMENT_OPERATOR
:
4341 return DECL_ASSIGNMENT_OPERATOR_P (t
);
4342 case IS_CONSTRUCTOR
:
4343 return DECL_CONSTRUCTOR_P (t
);
4345 return DECL_DESTRUCTOR_P (t
);
4346 case IS_COPY_CONSTRUCTOR
:
4347 return DECL_COPY_CONSTRUCTOR_P (t
);
4348 case IS_MOVE_CONSTRUCTOR
:
4349 return DECL_MOVE_CONSTRUCTOR_P (t
);
4351 return TREE_CODE (t
) == TEMPLATE_DECL
;
4353 return trivial_type_p (t
);
4359 /* Collect source file references recursively, starting from NAMESPC. */
4362 collect_source_refs (tree namespc
)
4364 /* Iterate over names in this name space. */
4365 for (tree t
= NAMESPACE_LEVEL (namespc
)->names
; t
; t
= TREE_CHAIN (t
))
4366 if (DECL_IS_BUILTIN (t
))
4368 else if (TREE_CODE (t
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (t
))
4369 collect_source_refs (t
);
4371 collect_source_ref (DECL_SOURCE_FILE (t
));
4374 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4375 starting from NAMESPC. */
4378 collect_ada_namespace (tree namespc
, const char *source_file
)
4380 tree decl
= NAMESPACE_LEVEL (namespc
)->names
;
4382 /* Collect decls from this namespace. This will skip
4383 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4384 collect_ada_nodes (decl
, source_file
);
4386 /* Now scan for namespace children, and dump them. */
4387 for (; decl
; decl
= TREE_CHAIN (decl
))
4388 if (TREE_CODE (decl
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (decl
))
4389 collect_ada_namespace (decl
, source_file
);
4392 /* Returns true iff there is a definition available for variable or
4396 decl_defined_p (tree decl
)
4398 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4399 return (DECL_INITIAL (decl
) != NULL_TREE
4400 /* A pending instantiation of a friend temploid is defined. */
4401 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
4402 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4403 (DECL_TI_TEMPLATE (decl
)))));
4406 gcc_assert (VAR_P (decl
));
4407 return !DECL_EXTERNAL (decl
);
4411 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4415 An integral constant-expression can only involve ... const
4416 variables of integral or enumeration types initialized with
4417 constant expressions ...
4419 C++0x also allows constexpr variables and temporaries initialized
4420 with constant expressions. We handle the former here, but the latter
4421 are just folded away in cxx_eval_constant_expression.
4423 The standard does not require that the expression be non-volatile.
4424 G++ implements the proposed correction in DR 457. */
4427 decl_constant_var_p (tree decl
)
4429 if (!decl_maybe_constant_var_p (decl
))
4432 /* We don't know if a template static data member is initialized with
4433 a constant expression until we instantiate its initializer. Even
4434 in the case of a constexpr variable, we can't treat it as a
4435 constant until its initializer is complete in case it's used in
4436 its own initializer. */
4437 maybe_instantiate_decl (decl
);
4438 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
);
4441 /* Returns true if DECL could be a symbolic constant variable, depending on
4445 decl_maybe_constant_var_p (tree decl
)
4447 tree type
= TREE_TYPE (decl
);
4450 if (DECL_DECLARED_CONSTEXPR_P (decl
) && !TREE_THIS_VOLATILE (decl
))
4452 if (DECL_HAS_VALUE_EXPR_P (decl
))
4453 /* A proxy isn't constant. */
4455 if (TYPE_REF_P (type
))
4456 /* References can be constant. */;
4457 else if (CP_TYPE_CONST_NON_VOLATILE_P (type
)
4458 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4459 /* And const integers. */;
4463 if (DECL_INITIAL (decl
)
4464 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
4465 /* We know the initializer, and it isn't constant. */
4471 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4472 called from grokfndecl and grokvardecl; in all modes it is called from
4473 cp_write_global_declarations. */
4476 no_linkage_error (tree decl
)
4478 if (cxx_dialect
>= cxx11
4479 && (decl_defined_p (decl
)
4480 /* Treat templates which limit_bad_template_recursion decided
4481 not to instantiate as if they were defined. */
4482 || (errorcount
+ sorrycount
> 0
4483 && DECL_LANG_SPECIFIC (decl
)
4484 && DECL_TEMPLATE_INFO (decl
)
4485 && TREE_NO_WARNING (decl
))))
4486 /* In C++11 it's ok if the decl is defined. */
4489 tree t
= no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false);
4491 /* The type that got us on no_linkage_decls must have gotten a name for
4492 linkage purposes. */;
4493 else if (CLASS_TYPE_P (t
) && TYPE_BEING_DEFINED (t
))
4494 // FIXME: This is now invalid, as a DR to c++98
4495 /* The type might end up having a typedef name for linkage purposes. */
4496 vec_safe_push (no_linkage_decls
, decl
);
4497 else if (TYPE_UNNAMED_P (t
))
4500 auto_diagnostic_group grp
;
4501 if (cxx_dialect
>= cxx11
)
4502 d
= permerror (DECL_SOURCE_LOCATION (decl
), "%q#D, declared using "
4503 "unnamed type, is used but never defined", decl
);
4504 else if (DECL_EXTERN_C_P (decl
))
4505 /* Allow this; it's pretty common in C. */;
4506 else if (VAR_P (decl
))
4507 /* DRs 132, 319 and 389 seem to indicate types with
4508 no linkage can only be used to declare extern "C"
4509 entities. Since it's not always an error in the
4510 ISO C++ 90 Standard, we only issue a warning. */
4511 d
= warning_at (DECL_SOURCE_LOCATION (decl
), 0, "unnamed type "
4512 "with no linkage used to declare variable %q#D with "
4515 d
= permerror (DECL_SOURCE_LOCATION (decl
), "unnamed type with no "
4516 "linkage used to declare function %q#D with linkage",
4518 if (d
&& is_typedef_decl (TYPE_NAME (t
)))
4519 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)), "%q#D does not refer "
4520 "to the unqualified type, so it is not used for linkage",
4523 else if (cxx_dialect
>= cxx11
)
4525 if (VAR_P (decl
) || !DECL_PURE_VIRTUAL_P (decl
))
4526 permerror (DECL_SOURCE_LOCATION (decl
),
4527 "%q#D, declared using local type "
4528 "%qT, is used but never defined", decl
, t
);
4530 else if (VAR_P (decl
))
4531 warning_at (DECL_SOURCE_LOCATION (decl
), 0, "type %qT with no linkage "
4532 "used to declare variable %q#D with linkage", t
, decl
);
4534 permerror (DECL_SOURCE_LOCATION (decl
), "type %qT with no linkage used "
4535 "to declare function %q#D with linkage", t
, decl
);
4538 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4541 collect_all_refs (const char *source_file
)
4543 collect_ada_namespace (global_namespace
, source_file
);
4546 /* Clear DECL_EXTERNAL for NODE. */
4549 clear_decl_external (struct cgraph_node
*node
, void * /*data*/)
4551 DECL_EXTERNAL (node
->decl
) = 0;
4555 /* Build up the function to run dynamic initializers for thread_local
4556 variables in this translation unit and alias the init functions for the
4557 individual variables to it. */
4560 handle_tls_init (void)
4562 tree vars
= prune_vars_needing_no_initialization (&tls_aggregates
);
4563 if (vars
== NULL_TREE
)
4566 location_t loc
= DECL_SOURCE_LOCATION (TREE_VALUE (vars
));
4568 write_out_vars (vars
);
4570 tree guard
= build_decl (loc
, VAR_DECL
, get_identifier ("__tls_guard"),
4572 TREE_PUBLIC (guard
) = false;
4573 TREE_STATIC (guard
) = true;
4574 DECL_ARTIFICIAL (guard
) = true;
4575 DECL_IGNORED_P (guard
) = true;
4576 TREE_USED (guard
) = true;
4577 CP_DECL_THREAD_LOCAL_P (guard
) = true;
4578 set_decl_tls_model (guard
, decl_default_tls_model (guard
));
4579 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
4581 tree fn
= get_local_tls_init_fn (loc
);
4582 start_preparsed_function (fn
, NULL_TREE
, SF_PRE_PARSED
);
4583 tree body
= begin_function_body ();
4584 tree if_stmt
= begin_if_stmt ();
4585 tree cond
= cp_build_unary_op (TRUTH_NOT_EXPR
, guard
, false,
4586 tf_warning_or_error
);
4587 finish_if_stmt_cond (cond
, if_stmt
);
4588 finish_expr_stmt (cp_build_modify_expr (loc
, guard
, NOP_EXPR
,
4590 tf_warning_or_error
));
4591 for (; vars
; vars
= TREE_CHAIN (vars
))
4593 tree var
= TREE_VALUE (vars
);
4594 tree init
= TREE_PURPOSE (vars
);
4595 one_static_initialization_or_destruction (var
, init
, true);
4597 /* Output init aliases even with -fno-extern-tls-init. */
4598 if (TARGET_SUPPORTS_ALIASES
&& TREE_PUBLIC (var
))
4600 tree single_init_fn
= get_tls_init_fn (var
);
4601 if (single_init_fn
== NULL_TREE
)
4604 = cgraph_node::get_create (fn
)->create_same_body_alias
4605 (single_init_fn
, fn
);
4606 gcc_assert (alias
!= NULL
);
4610 finish_then_clause (if_stmt
);
4611 finish_if_stmt (if_stmt
);
4612 finish_function_body (body
);
4613 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4616 /* We're at the end of compilation, so generate any mangling aliases that
4617 we've been saving up, if DECL is going to be output and ID2 isn't
4618 already taken by another declaration. */
4621 generate_mangling_alias (tree decl
, tree id2
)
4623 struct cgraph_node
*n
= NULL
;
4625 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4627 n
= cgraph_node::get (decl
);
4629 /* Don't create an alias to an unreferenced function. */
4634 = mangled_decls
->find_slot_with_hash (id2
, IDENTIFIER_HASH_VALUE (id2
),
4637 /* If there's a declaration already using this mangled name,
4638 don't create a compatibility alias that conflicts. */
4642 tree alias
= make_alias_for (decl
, id2
);
4645 DECL_IGNORED_P (alias
) = 1;
4646 TREE_PUBLIC (alias
) = TREE_PUBLIC (decl
);
4647 DECL_VISIBILITY (alias
) = DECL_VISIBILITY (decl
);
4648 if (vague_linkage_p (decl
))
4649 DECL_WEAK (alias
) = 1;
4652 n
->create_same_body_alias (alias
, decl
);
4654 varpool_node::create_extra_name_alias (alias
, decl
);
4657 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4658 the end of translation, for compatibility across bugs in the mangling
4662 note_mangling_alias (tree decl
, tree id2
)
4664 if (TARGET_SUPPORTS_ALIASES
)
4666 if (!defer_mangling_aliases
)
4667 generate_mangling_alias (decl
, id2
);
4670 vec_safe_push (mangling_aliases
, decl
);
4671 vec_safe_push (mangling_aliases
, id2
);
4676 /* Emit all mangling aliases that were deferred up to this point. */
4679 generate_mangling_aliases ()
4681 while (!vec_safe_is_empty (mangling_aliases
))
4683 tree id2
= mangling_aliases
->pop();
4684 tree decl
= mangling_aliases
->pop();
4685 generate_mangling_alias (decl
, id2
);
4687 defer_mangling_aliases
= false;
4690 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4691 set. NEED_WARNING is true if we must warn about collisions. We do
4692 this to spot changes in mangling that may require compatibility
4696 record_mangling (tree decl
, bool need_warning
)
4699 mangled_decls
= hash_table
<mangled_decl_hash
>::create_ggc (499);
4701 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
4702 tree id
= DECL_ASSEMBLER_NAME_RAW (decl
);
4704 = mangled_decls
->find_slot_with_hash (id
, IDENTIFIER_HASH_VALUE (id
),
4707 /* If this is already an alias, remove the alias, because the real
4708 decl takes precedence. */
4709 if (*slot
&& DECL_ARTIFICIAL (*slot
) && DECL_IGNORED_P (*slot
))
4710 if (symtab_node
*n
= symtab_node::get (*slot
))
4711 if (n
->cpp_implicit_alias
)
4719 else if (need_warning
)
4721 error_at (DECL_SOURCE_LOCATION (decl
),
4722 "mangling of %q#D as %qE conflicts with a previous mangle",
4724 inform (DECL_SOURCE_LOCATION (*slot
),
4725 "previous mangling %q#D", *slot
);
4726 inform (DECL_SOURCE_LOCATION (decl
),
4727 "a later %<-fabi-version=%> (or =0)"
4728 " avoids this error with a change in mangling");
4733 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4734 any existing knowledge of DECL's mangled name meaning DECL. */
4737 overwrite_mangling (tree decl
, tree name
)
4739 if (tree id
= DECL_ASSEMBLER_NAME_RAW (decl
))
4740 if ((TREE_CODE (decl
) == VAR_DECL
4741 || TREE_CODE (decl
) == FUNCTION_DECL
)
4744 = mangled_decls
->find_slot_with_hash (id
, IDENTIFIER_HASH_VALUE (id
),
4748 mangled_decls
->clear_slot (slot
);
4750 /* If this is an alias, remove it from the symbol table. */
4751 if (DECL_ARTIFICIAL (decl
) && DECL_IGNORED_P (decl
))
4752 if (symtab_node
*n
= symtab_node::get (decl
))
4753 if (n
->cpp_implicit_alias
)
4757 DECL_ASSEMBLER_NAME_RAW (decl
) = name
;
4760 /* The entire file is now complete. If requested, dump everything
4767 if (FILE *stream
= dump_begin (raw_dump_id
, &flags
))
4769 dump_node (global_namespace
, flags
& ~TDF_SLIM
, stream
);
4770 dump_end (raw_dump_id
, stream
);
4774 static location_t locus_at_end_of_parsing
;
4776 /* Check the deallocation functions for CODE to see if we want to warn that
4777 only one was defined. */
4780 maybe_warn_sized_delete (enum tree_code code
)
4782 tree sized
= NULL_TREE
;
4783 tree unsized
= NULL_TREE
;
4785 for (ovl_iterator
iter (get_global_binding (ovl_op_identifier (false, code
)));
4789 /* We're only interested in usual deallocation functions. */
4790 if (!usual_deallocation_fn_p (fn
))
4792 if (FUNCTION_ARG_CHAIN (fn
) == void_list_node
)
4797 if (DECL_INITIAL (unsized
) && !DECL_INITIAL (sized
))
4798 warning_at (DECL_SOURCE_LOCATION (unsized
), OPT_Wsized_deallocation
,
4799 "the program should also define %qD", sized
);
4800 else if (!DECL_INITIAL (unsized
) && DECL_INITIAL (sized
))
4801 warning_at (DECL_SOURCE_LOCATION (sized
), OPT_Wsized_deallocation
,
4802 "the program should also define %qD", unsized
);
4805 /* Check the global deallocation functions to see if we want to warn about
4806 defining unsized without sized (or vice versa). */
4809 maybe_warn_sized_delete ()
4811 if (!flag_sized_deallocation
|| !warn_sized_deallocation
)
4813 maybe_warn_sized_delete (DELETE_EXPR
);
4814 maybe_warn_sized_delete (VEC_DELETE_EXPR
);
4817 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4818 look them up when evaluating non-type template parameters. Now we need to
4819 lower them to something the back end can understand. */
4825 FOR_EACH_VARIABLE (node
)
4827 tree d
= node
->decl
;
4828 if (tree init
= DECL_INITIAL (d
))
4829 DECL_INITIAL (d
) = cplus_expand_constant (init
);
4833 /* This routine is called at the end of compilation.
4834 Its job is to create all the code needed to initialize and
4835 destroy the global aggregates. We do the destruction
4836 first, since that way we only need to reverse the decls once. */
4839 c_parse_final_cleanups (void)
4844 unsigned ssdf_count
= 0;
4848 locus_at_end_of_parsing
= input_location
;
4851 /* Bad parse errors. Just forget about it. */
4852 if (! global_bindings_p () || current_class_type
4853 || !vec_safe_is_empty (decl_namespace_list
))
4856 /* This is the point to write out a PCH if we're doing that.
4857 In that case we do not want to do anything else. */
4860 /* Mangle all symbols at PCH creation time. */
4862 FOR_EACH_SYMBOL (node
)
4863 if (! is_a
<varpool_node
*> (node
)
4864 || ! DECL_HARD_REGISTER (node
->decl
))
4865 DECL_ASSEMBLER_NAME (node
->decl
);
4866 c_common_write_pch ();
4868 /* Ensure even the callers don't try to finalize the CU. */
4869 flag_syntax_only
= 1;
4873 timevar_stop (TV_PHASE_PARSING
);
4874 timevar_start (TV_PHASE_DEFERRED
);
4876 symtab
->process_same_body_aliases ();
4878 /* Handle -fdump-ada-spec[-slim] */
4879 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
4881 collect_source_ref (main_input_filename
);
4882 if (!flag_dump_ada_spec_slim
)
4883 collect_source_refs (global_namespace
);
4885 dump_ada_specs (collect_all_refs
, cpp_check
);
4888 /* FIXME - huh? was input_line -= 1;*/
4890 /* We now have to write out all the stuff we put off writing out.
4893 o Template specializations that we have not yet instantiated,
4894 but which are needed.
4895 o Initialization and destruction for non-local objects with
4896 static storage duration. (Local objects with static storage
4897 duration are initialized when their scope is first entered,
4898 and are cleaned up via atexit.)
4899 o Virtual function tables.
4901 All of these may cause others to be needed. For example,
4902 instantiating one function may cause another to be needed, and
4903 generating the initializer for an object may cause templates to be
4904 instantiated, etc., etc. */
4906 emit_support_tinfos ();
4908 /* Track vtables we want to emit that refer to consteval functions. */
4909 auto_vec
<tree
> consteval_vtables
;
4918 /* If there are templates that we've put off instantiating, do
4920 instantiate_pending_templates (retries
);
4923 /* Write out virtual tables as required. Writing out the
4924 virtual table for a template class may cause the
4925 instantiation of members of that class. If we write out
4926 vtables then we remove the class from our list so we don't
4927 have to look at it again. */
4928 for (i
= keyed_classes
->length ();
4929 keyed_classes
->iterate (--i
, &t
);)
4930 if (maybe_emit_vtables (t
, consteval_vtables
))
4933 keyed_classes
->unordered_remove (i
);
4935 /* The input_location may have been changed during marking of
4937 input_location
= locus_at_end_of_parsing
;
4939 /* Write out needed type info variables. We have to be careful
4940 looping through unemitted decls, because emit_tinfo_decl may
4941 cause other variables to be needed. New elements will be
4942 appended, and we remove from the vector those that actually
4944 for (i
= unemitted_tinfo_decls
->length ();
4945 unemitted_tinfo_decls
->iterate (--i
, &t
);)
4946 if (emit_tinfo_decl (t
))
4949 unemitted_tinfo_decls
->unordered_remove (i
);
4952 /* The list of objects with static storage duration is built up
4953 in reverse order. We clear STATIC_AGGREGATES so that any new
4954 aggregates added during the initialization of these will be
4955 initialized in the correct order when we next come around the
4957 vars
= prune_vars_needing_no_initialization (&static_aggregates
);
4961 /* We need to start a new initialization function each time
4962 through the loop. That's because we need to know which
4963 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4964 isn't computed until a function is finished, and written
4965 out. That's a deficiency in the back end. When this is
4966 fixed, these initialization functions could all become
4967 inline, with resulting performance improvements. */
4970 /* Make sure the back end knows about all the variables. */
4971 write_out_vars (vars
);
4973 /* Set the line and file, so that it is obviously not from
4975 input_location
= locus_at_end_of_parsing
;
4976 ssdf_body
= start_static_storage_duration_function (ssdf_count
);
4978 /* First generate code to do all the initializations. */
4980 do_static_initialization_or_destruction (vars
, /*initp=*/true);
4982 /* Then, generate code to do all the destructions. Do these
4983 in reverse order so that the most recently constructed
4984 variable is the first destroyed. If we're using
4985 __cxa_atexit, then we don't need to do this; functions
4986 were registered at initialization time to destroy the
4988 if (!flag_use_cxa_atexit
&& vars
)
4990 vars
= nreverse (vars
);
4991 do_static_initialization_or_destruction (vars
, /*initp=*/false);
4996 /* Finish up the static storage duration function for this
4998 input_location
= locus_at_end_of_parsing
;
4999 finish_static_storage_duration_function (ssdf_body
);
5001 /* All those initializations and finalizations might cause
5002 us to need more inline functions, more template
5003 instantiations, etc. */
5006 /* ??? was: locus_at_end_of_parsing.line++; */
5009 /* Now do the same for thread_local variables. */
5012 /* Go through the set of inline functions whose bodies have not
5013 been emitted yet. If out-of-line copies of these functions
5014 are required, emit them. */
5015 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
5017 /* Does it need synthesizing? */
5018 if (DECL_DEFAULTED_FN (decl
) && ! DECL_INITIAL (decl
)
5019 && (! DECL_REALLY_EXTERN (decl
) || possibly_inlined_p (decl
)))
5021 /* Even though we're already at the top-level, we push
5022 there again. That way, when we pop back a few lines
5023 hence, all of our state is restored. Otherwise,
5024 finish_function doesn't clean things up, and we end
5025 up with CURRENT_FUNCTION_DECL set. */
5026 push_to_top_level ();
5027 /* The decl's location will mark where it was first
5028 needed. Save that so synthesize method can indicate
5029 where it was needed from, in case of error */
5030 input_location
= DECL_SOURCE_LOCATION (decl
);
5031 synthesize_method (decl
);
5032 pop_from_top_level ();
5036 if (!DECL_INITIAL (decl
) && decl_tls_wrapper_p (decl
))
5037 generate_tls_wrapper (decl
);
5039 if (!DECL_SAVED_TREE (decl
))
5042 cgraph_node
*node
= cgraph_node::get_create (decl
);
5044 /* We lie to the back end, pretending that some functions
5045 are not defined when they really are. This keeps these
5046 functions from being put out unnecessarily. But, we must
5047 stop lying when the functions are referenced, or if they
5048 are not comdat since they need to be put out now. If
5049 DECL_INTERFACE_KNOWN, then we have already set
5050 DECL_EXTERNAL appropriately, so there's no need to check
5051 again, and we do not want to clear DECL_EXTERNAL if a
5052 previous call to import_export_decl set it.
5054 This is done in a separate for cycle, because if some
5055 deferred function is contained in another deferred
5056 function later in deferred_fns varray,
5057 rest_of_compilation would skip this function and we
5058 really cannot expand the same function twice. */
5059 import_export_decl (decl
);
5060 if (DECL_NOT_REALLY_EXTERN (decl
)
5061 && DECL_INITIAL (decl
)
5062 && decl_needed_p (decl
))
5064 if (node
->cpp_implicit_alias
)
5065 node
= node
->get_alias_target ();
5067 node
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
5069 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
5070 group, we need to mark all symbols in the same comdat group
5072 if (node
->same_comdat_group
)
5073 for (cgraph_node
*next
5074 = dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
5076 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
5077 next
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
5081 /* If we're going to need to write this function out, and
5082 there's already a body for it, create RTL for it now.
5083 (There might be no body if this is a method we haven't
5084 gotten around to synthesizing yet.) */
5085 if (!DECL_EXTERNAL (decl
)
5086 && decl_needed_p (decl
)
5087 && !TREE_ASM_WRITTEN (decl
)
5088 && !node
->definition
)
5090 /* We will output the function; no longer consider it in this
5092 DECL_DEFER_OUTPUT (decl
) = 0;
5093 /* Generate RTL for this function now that we know we
5095 expand_or_defer_fn (decl
);
5100 if (wrapup_namespace_globals ())
5103 /* Static data members are just like namespace-scope globals. */
5104 FOR_EACH_VEC_SAFE_ELT (pending_statics
, i
, decl
)
5106 if (var_finalized_p (decl
) || DECL_REALLY_EXTERN (decl
)
5107 /* Don't write it out if we haven't seen a definition. */
5108 || DECL_IN_AGGR_P (decl
))
5110 import_export_decl (decl
);
5111 /* If this static data member is needed, provide it to the
5113 if (DECL_NOT_REALLY_EXTERN (decl
) && decl_needed_p (decl
))
5114 DECL_EXTERNAL (decl
) = 0;
5116 if (vec_safe_length (pending_statics
) != 0
5117 && wrapup_global_declarations (pending_statics
->address (),
5118 pending_statics
->length ()))
5127 generate_mangling_aliases ();
5129 /* All used inline functions must have a definition at this point. */
5130 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
5132 if (/* Check online inline functions that were actually used. */
5133 DECL_ODR_USED (decl
) && DECL_DECLARED_INLINE_P (decl
)
5134 /* If the definition actually was available here, then the
5135 fact that the function was not defined merely represents
5136 that for some reason (use of a template repository,
5137 #pragma interface, etc.) we decided not to emit the
5139 && !DECL_INITIAL (decl
)
5140 /* Don't complain if the template was defined. */
5141 && !(DECL_TEMPLATE_INSTANTIATION (decl
)
5142 && DECL_INITIAL (DECL_TEMPLATE_RESULT
5143 (template_for_substitution (decl
))))
5144 && warning_at (DECL_SOURCE_LOCATION (decl
), 0,
5145 "inline function %qD used but never defined", decl
))
5146 /* Avoid a duplicate warning from check_global_declaration. */
5147 TREE_NO_WARNING (decl
) = 1;
5150 /* So must decls that use a type with no linkage. */
5151 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls
, i
, decl
)
5152 no_linkage_error (decl
);
5154 maybe_warn_sized_delete ();
5156 /* Then, do the Objective-C stuff. This is where all the
5157 Objective-C module stuff gets generated (symtab,
5158 class/protocol/selector lists etc). This must be done after C++
5159 templates, destructors etc. so that selectors used in C++
5160 templates are properly allocated. */
5161 if (c_dialect_objc ())
5162 objc_write_global_declarations ();
5164 /* We give C linkage to static constructors and destructors. */
5165 push_lang_context (lang_name_c
);
5167 /* Generate initialization and destruction functions for all
5168 priorities for which they are required. */
5169 if (priority_info_map
)
5170 splay_tree_foreach (priority_info_map
,
5171 generate_ctor_and_dtor_functions_for_priority
,
5172 /*data=*/&locus_at_end_of_parsing
);
5173 else if (c_dialect_objc () && objc_static_init_needed_p ())
5174 /* If this is obj-c++ and we need a static init, call
5175 generate_ctor_or_dtor_function. */
5176 generate_ctor_or_dtor_function (/*constructor_p=*/true,
5177 DEFAULT_INIT_PRIORITY
,
5178 &locus_at_end_of_parsing
);
5180 /* We're done with the splay-tree now. */
5181 if (priority_info_map
)
5182 splay_tree_delete (priority_info_map
);
5184 /* Generate any missing aliases. */
5185 maybe_apply_pending_pragma_weaks ();
5187 /* We're done with static constructors, so we can go back to "C++"
5189 pop_lang_context ();
5191 if (flag_vtable_verify
)
5193 vtv_recover_class_info ();
5194 vtv_compute_class_hierarchy_transitive_closure ();
5195 vtv_build_vtable_verify_fndecl ();
5198 perform_deferred_noexcept_checks ();
5201 clear_consteval_vfns (consteval_vtables
);
5203 /* The entire file is now complete. If requested, dump everything
5207 if (flag_detailed_statistics
)
5209 dump_tree_statistics ();
5210 dump_time_statistics ();
5213 timevar_stop (TV_PHASE_DEFERRED
);
5214 timevar_start (TV_PHASE_PARSING
);
5216 /* Indicate that we're done with front end processing. */
5220 /* Perform any post compilation-proper cleanups for the C++ front-end.
5221 This should really go away. No front-end should need to do
5222 anything past the compilation process. */
5225 cxx_post_compilation_parsing_cleanups (void)
5227 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS
);
5229 if (flag_vtable_verify
)
5231 /* Generate the special constructor initialization function that
5232 calls __VLTRegisterPairs, and give it a very high
5233 initialization priority. This must be done after
5234 finalize_compilation_unit so that we have accurate
5235 information about which vtable will actually be emitted. */
5236 vtv_generate_init_routine ();
5239 input_location
= locus_at_end_of_parsing
;
5242 validate_conversion_obstack ();
5244 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS
);
5247 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5248 function to call in parse-tree form; it has not yet been
5249 semantically analyzed. ARGS are the arguments to the function.
5250 They have already been semantically analyzed. This may change
5254 build_offset_ref_call_from_tree (tree fn
, vec
<tree
, va_gc
> **args
,
5255 tsubst_flags_t complain
)
5258 vec
<tree
, va_gc
> *orig_args
= NULL
;
5263 object
= TREE_OPERAND (fn
, 0);
5265 if (processing_template_decl
)
5267 gcc_assert (TREE_CODE (fn
) == DOTSTAR_EXPR
5268 || TREE_CODE (fn
) == MEMBER_REF
);
5269 if (type_dependent_expression_p (fn
)
5270 || any_type_dependent_arguments_p (*args
))
5271 return build_min_nt_call_vec (fn
, *args
);
5273 orig_args
= make_tree_vector_copy (*args
);
5275 /* Transform the arguments and add the implicit "this"
5276 parameter. That must be done before the FN is transformed
5277 because we depend on the form of FN. */
5278 make_args_non_dependent (*args
);
5279 object
= build_non_dependent_expr (object
);
5280 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
5282 if (TREE_CODE (fn
) == DOTSTAR_EXPR
)
5283 object
= cp_build_addr_expr (object
, complain
);
5284 vec_safe_insert (*args
, 0, object
);
5286 /* Now that the arguments are done, transform FN. */
5287 fn
= build_non_dependent_expr (fn
);
5290 /* A qualified name corresponding to a bound pointer-to-member is
5291 represented as an OFFSET_REF:
5293 struct B { void g(); };
5295 void B::g() { (this->*p)(); } */
5296 if (TREE_CODE (fn
) == OFFSET_REF
)
5298 tree object_addr
= cp_build_addr_expr (object
, complain
);
5299 fn
= TREE_OPERAND (fn
, 1);
5300 fn
= get_member_function_from_ptrfunc (&object_addr
, fn
,
5302 vec_safe_insert (*args
, 0, object_addr
);
5305 if (CLASS_TYPE_P (TREE_TYPE (fn
)))
5306 expr
= build_op_call (fn
, args
, complain
);
5308 expr
= cp_build_function_call_vec (fn
, args
, complain
);
5309 if (processing_template_decl
&& expr
!= error_mark_node
)
5310 expr
= build_min_non_dep_call_vec (expr
, orig_fn
, orig_args
);
5312 if (orig_args
!= NULL
)
5313 release_tree_vector (orig_args
);
5320 check_default_args (tree x
)
5322 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
5323 bool saw_def
= false;
5324 bool noted_first_def
= false;
5325 int idx_of_first_default_arg
= 0;
5326 location_t loc_of_first_default_arg
= UNKNOWN_LOCATION
;
5327 int i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
5328 tree fndecl
= STRIP_TEMPLATE (x
);
5329 auto_diagnostic_group d
;
5330 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
5332 if (TREE_PURPOSE (arg
))
5337 idx_of_first_default_arg
= i
;
5338 location_t loc
= get_fndecl_argument_location (fndecl
, i
);
5339 if (loc
!= DECL_SOURCE_LOCATION (x
))
5340 loc_of_first_default_arg
= loc
;
5343 else if (saw_def
&& !PACK_EXPANSION_P (TREE_VALUE (arg
)))
5345 error_at (get_fndecl_argument_location (fndecl
, i
),
5346 "default argument missing for parameter %P of %q#D", i
, x
);
5347 if (loc_of_first_default_arg
!= UNKNOWN_LOCATION
5348 && !noted_first_def
)
5350 inform (loc_of_first_default_arg
,
5351 "...following parameter %P which has a default argument",
5352 idx_of_first_default_arg
);
5353 noted_first_def
= true;
5355 TREE_PURPOSE (arg
) = error_mark_node
;
5360 /* Return true if function DECL can be inlined. This is used to force
5361 instantiation of methods that might be interesting for inlining. */
5363 possibly_inlined_p (tree decl
)
5365 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
5366 if (DECL_UNINLINABLE (decl
))
5369 return DECL_DECLARED_INLINE_P (decl
);
5370 /* When optimizing, we might inline everything when flatten
5371 attribute or heuristics inlining for size or autoinlining
5376 /* Normally, we can wait until instantiation-time to synthesize DECL.
5377 However, if DECL is a static data member initialized with a constant
5378 or a constexpr function, we need it right now because a reference to
5379 such a data member or a call to such function is not value-dependent.
5380 For a function that uses auto in the return type, we need to instantiate
5381 it to find out its type. For OpenMP user defined reductions, we need
5382 them instantiated for reduction clauses which inline them by hand
5386 maybe_instantiate_decl (tree decl
)
5388 if (DECL_LANG_SPECIFIC (decl
)
5389 && DECL_TEMPLATE_INFO (decl
)
5390 && (decl_maybe_constant_var_p (decl
)
5391 || (TREE_CODE (decl
) == FUNCTION_DECL
5392 && DECL_OMP_DECLARE_REDUCTION_P (decl
))
5393 || undeduced_auto_decl (decl
))
5394 && !DECL_DECLARED_CONCEPT_P (decl
)
5395 && !uses_template_parms (DECL_TI_ARGS (decl
)))
5397 /* Instantiating a function will result in garbage collection. We
5398 must treat this situation as if we were within the body of a
5399 function so as to avoid collecting live data only referenced from
5400 the stack (such as overload resolution candidates). */
5402 instantiate_decl (decl
, /*defer_ok=*/false,
5403 /*expl_inst_class_mem_p=*/false);
5408 /* Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns whether or
5409 not a warning was emitted. */
5412 cp_warn_deprecated_use (tree decl
, tsubst_flags_t complain
)
5414 if (!(complain
& tf_warning
) || !decl
5415 || deprecated_state
== DEPRECATED_SUPPRESS
)
5418 if (!TREE_DEPRECATED (decl
))
5420 /* Perhaps this is a deprecated typedef. */
5421 if (TYPE_P (decl
) && TYPE_NAME (decl
))
5422 decl
= TYPE_NAME (decl
);
5424 if (!TREE_DEPRECATED (decl
))
5428 /* Don't warn within members of a deprecated type. */
5430 && currently_open_class (decl
))
5433 bool warned
= false;
5434 if (cxx_dialect
>= cxx11
5436 && DECL_ARTIFICIAL (decl
)
5437 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
5438 && copy_fn_p (decl
))
5440 if (warn_deprecated_copy
5441 /* Don't warn about system library classes (c++/86342). */
5442 && (!DECL_IN_SYSTEM_HEADER (decl
)
5443 || global_dc
->dc_warn_system_headers
))
5445 auto_diagnostic_group d
;
5446 tree ctx
= DECL_CONTEXT (decl
);
5447 tree other
= classtype_has_depr_implicit_copy (ctx
);
5448 int opt
= (DECL_DESTRUCTOR_P (other
)
5449 ? OPT_Wdeprecated_copy_dtor
5450 : OPT_Wdeprecated_copy
);
5451 warned
= warning (opt
, "implicitly-declared %qD is deprecated",
5454 inform (DECL_SOURCE_LOCATION (other
),
5455 "because %qT has user-provided %qD",
5460 warned
= warn_deprecated_use (decl
, NULL_TREE
);
5465 /* Like above, but takes into account outer scopes. */
5468 cp_warn_deprecated_use_scopes (tree scope
)
5471 && scope
!= error_mark_node
5472 && scope
!= global_namespace
)
5474 if (cp_warn_deprecated_use (scope
))
5477 scope
= CP_TYPE_CONTEXT (scope
);
5479 scope
= CP_DECL_CONTEXT (scope
);
5483 /* True if DECL or its enclosing scope have unbound template parameters. */
5486 decl_dependent_p (tree decl
)
5488 if (DECL_FUNCTION_SCOPE_P (decl
)
5489 || TREE_CODE (decl
) == CONST_DECL
5490 || TREE_CODE (decl
) == USING_DECL
5491 || TREE_CODE (decl
) == FIELD_DECL
)
5492 decl
= CP_DECL_CONTEXT (decl
);
5493 if (tree tinfo
= get_template_info (decl
))
5494 if (any_dependent_template_arguments_p (TI_ARGS (tinfo
)))
5496 if (LAMBDA_FUNCTION_P (decl
)
5497 && dependent_type_p (DECL_CONTEXT (decl
)))
5502 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5503 If DECL is a specialization or implicitly declared class member,
5504 generate the actual definition. Return false if something goes
5505 wrong, true otherwise. */
5508 mark_used (tree decl
, tsubst_flags_t complain
)
5510 /* If we're just testing conversions or resolving overloads, we
5511 don't want any permanent effects like forcing functions to be
5512 output or instantiating templates. */
5513 if ((complain
& tf_conv
))
5516 /* If DECL is a BASELINK for a single function, then treat it just
5517 like the DECL for the function. Otherwise, if the BASELINK is
5518 for an overloaded function, we don't know which function was
5519 actually used until after overload resolution. */
5520 if (BASELINK_P (decl
))
5522 decl
= BASELINK_FUNCTIONS (decl
);
5523 if (really_overloaded_fn (decl
))
5525 decl
= OVL_FIRST (decl
);
5531 /* Set TREE_USED for the benefit of -Wunused. */
5532 TREE_USED (decl
) = true;
5534 /* And for structured bindings also the underlying decl. */
5535 if (DECL_DECOMPOSITION_P (decl
) && DECL_DECOMP_BASE (decl
))
5536 TREE_USED (DECL_DECOMP_BASE (decl
)) = true;
5538 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5541 if (DECL_CLONED_FUNCTION_P (decl
))
5542 TREE_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
5544 /* Mark enumeration types as used. */
5545 if (TREE_CODE (decl
) == CONST_DECL
)
5546 used_types_insert (DECL_CONTEXT (decl
));
5548 if (TREE_CODE (decl
) == FUNCTION_DECL
5549 && !maybe_instantiate_noexcept (decl
, complain
))
5552 if (TREE_CODE (decl
) == FUNCTION_DECL
5553 && DECL_DELETED_FN (decl
))
5555 if (DECL_ARTIFICIAL (decl
)
5556 && DECL_CONV_FN_P (decl
)
5557 && LAMBDA_TYPE_P (DECL_CONTEXT (decl
)))
5558 /* We mark a lambda conversion op as deleted if we can't
5559 generate it properly; see maybe_add_lambda_conv_op. */
5560 sorry ("converting lambda that uses %<...%> to function pointer");
5561 else if (complain
& tf_error
)
5563 error ("use of deleted function %qD", decl
);
5564 if (!maybe_explain_implicit_delete (decl
))
5565 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
5570 cp_warn_deprecated_use (decl
, complain
);
5572 /* We can only check DECL_ODR_USED on variables or functions with
5573 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5574 might need special handling for. */
5575 if (!VAR_OR_FUNCTION_DECL_P (decl
)
5576 || DECL_LANG_SPECIFIC (decl
) == NULL
5577 || DECL_THUNK_P (decl
))
5579 if (!decl_dependent_p (decl
)
5580 && !require_deduced_type (decl
, complain
))
5585 /* We only want to do this processing once. We don't need to keep trying
5586 to instantiate inline templates, because unit-at-a-time will make sure
5587 we get them compiled before functions that want to inline them. */
5588 if (DECL_ODR_USED (decl
))
5591 /* Normally, we can wait until instantiation-time to synthesize DECL.
5592 However, if DECL is a static data member initialized with a constant
5593 or a constexpr function, we need it right now because a reference to
5594 such a data member or a call to such function is not value-dependent.
5595 For a function that uses auto in the return type, we need to instantiate
5596 it to find out its type. For OpenMP user defined reductions, we need
5597 them instantiated for reduction clauses which inline them by hand
5599 maybe_instantiate_decl (decl
);
5601 if (flag_concepts
&& TREE_CODE (decl
) == FUNCTION_DECL
5602 && !constraints_satisfied_p (decl
))
5604 if (complain
& tf_error
)
5606 auto_diagnostic_group d
;
5607 error ("use of function %qD with unsatisfied constraints",
5609 location_t loc
= DECL_SOURCE_LOCATION (decl
);
5610 inform (loc
, "declared here");
5611 diagnose_constraints (loc
, decl
, NULL_TREE
);
5616 if (processing_template_decl
|| in_template_function ())
5619 /* Check this too in case we're within instantiate_non_dependent_expr. */
5620 if (DECL_TEMPLATE_INFO (decl
)
5621 && uses_template_parms (DECL_TI_ARGS (decl
)))
5624 if (!require_deduced_type (decl
, complain
))
5627 if (builtin_pack_fn_p (decl
))
5629 error ("use of built-in parameter pack %qD outside of a template",
5634 /* If we don't need a value, then we don't need to synthesize DECL. */
5635 if (cp_unevaluated_operand
|| in_discarded_stmt
)
5638 DECL_ODR_USED (decl
) = 1;
5639 if (DECL_CLONED_FUNCTION_P (decl
))
5640 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
5642 /* DR 757: A type without linkage shall not be used as the type of a
5643 variable or function with linkage, unless
5644 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5645 o the variable or function is not used (3.2 [basic.def.odr]) or is
5646 defined in the same translation unit. */
5647 if (cxx_dialect
> cxx98
5648 && decl_linkage (decl
) != lk_none
5649 && !DECL_EXTERN_C_P (decl
)
5650 && !DECL_ARTIFICIAL (decl
)
5651 && !decl_defined_p (decl
)
5652 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
5654 if (is_local_extern (decl
))
5655 /* There's no way to define a local extern, and adding it to
5656 the vector interferes with GC, so give an error now. */
5657 no_linkage_error (decl
);
5659 vec_safe_push (no_linkage_decls
, decl
);
5662 if (TREE_CODE (decl
) == FUNCTION_DECL
5663 && DECL_DECLARED_INLINE_P (decl
)
5664 && !DECL_INITIAL (decl
)
5665 && !DECL_ARTIFICIAL (decl
)
5666 && !DECL_PURE_VIRTUAL_P (decl
))
5667 /* Remember it, so we can check it was defined. */
5668 note_vague_linkage_fn (decl
);
5670 /* Is it a synthesized method that needs to be synthesized? */
5671 if (TREE_CODE (decl
) == FUNCTION_DECL
5672 && DECL_DEFAULTED_FN (decl
)
5673 /* A function defaulted outside the class is synthesized either by
5674 cp_finish_decl or instantiate_decl. */
5675 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl
)
5676 && ! DECL_INITIAL (decl
))
5678 /* Defer virtual destructors so that thunks get the right
5680 if (DECL_VIRTUAL_P (decl
) && !at_eof
)
5682 note_vague_linkage_fn (decl
);
5686 /* Remember the current location for a function we will end up
5687 synthesizing. Then we can inform the user where it was
5688 required in the case of error. */
5689 if (decl_remember_implicit_trigger_p (decl
))
5690 DECL_SOURCE_LOCATION (decl
) = input_location
;
5692 /* Synthesizing an implicitly defined member function will result in
5693 garbage collection. We must treat this situation as if we were
5694 within the body of a function so as to avoid collecting live data
5695 on the stack (such as overload resolution candidates).
5697 We could just let c_parse_final_cleanups handle synthesizing
5698 this function by adding it to deferred_fns, but doing
5699 it at the use site produces better error messages. */
5701 synthesize_method (decl
);
5703 /* If this is a synthesized method we don't need to
5704 do the instantiation test below. */
5706 else if (VAR_OR_FUNCTION_DECL_P (decl
)
5707 && DECL_TEMPLATE_INFO (decl
)
5708 && !DECL_DECLARED_CONCEPT_P (decl
)
5709 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
5710 || always_instantiate_p (decl
)))
5711 /* If this is a function or variable that is an instance of some
5712 template, we now know that we will need to actually do the
5713 instantiation. We check that DECL is not an explicit
5714 instantiation because that is not checked in instantiate_decl.
5716 We put off instantiating functions in order to improve compile
5717 times. Maintaining a stack of active functions is expensive,
5718 and the inliner knows to instantiate any functions it might
5719 need. Therefore, we always try to defer instantiation. */
5722 instantiate_decl (decl
, /*defer_ok=*/true,
5723 /*expl_inst_class_mem_p=*/false);
5731 mark_used (tree decl
)
5733 return mark_used (decl
, tf_warning_or_error
);
5737 vtv_start_verification_constructor_init_function (void)
5739 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY
- 1);
5743 vtv_finish_verification_constructor_init_function (tree function_body
)
5747 finish_compound_stmt (function_body
);
5748 fn
= finish_function (/*inline_p=*/false);
5749 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
5750 decl_init_priority_insert (fn
, MAX_RESERVED_INIT_PRIORITY
- 1);
5755 #include "gt-cp-decl2.h"