1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2018 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 void mark_vtable_entries (tree
);
69 static bool maybe_emit_vtables (tree
);
70 static tree
start_objects (int, int);
71 static void finish_objects (int, int, tree
);
72 static tree
start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree
);
74 static priority_info
get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree
, bool);
76 static void one_static_initialization_or_destruction (tree
, tree
, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t
*);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node
,
80 static tree
prune_vars_needing_no_initialization (tree
*);
81 static void write_out_vars (tree
);
82 static void import_export_class (tree
);
83 static tree
get_guard_bits (tree
);
84 static void determine_visibility_from_class (tree
, tree
);
85 static bool determine_hidden_inline (tree
);
86 static void maybe_instantiate_decl (tree
);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec
<tree
, va_gc
> *pending_statics
;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec
<tree
, va_gc
> *deferred_fns
;
97 /* A list of decls that use types with no linkage, which we need to make
99 static GTY(()) vec
<tree
, va_gc
> *no_linkage_decls
;
101 /* A vector of alternating decls and identifiers, where the latter
102 is to be an alias for the former if the former is defined. */
103 static GTY(()) vec
<tree
, va_gc
> *mangling_aliases
;
105 /* hash traits for declarations. Hashes single decls via
106 DECL_ASSEMBLER_NAME_RAW. */
108 struct mangled_decl_hash
: ggc_remove
<tree
>
110 typedef tree value_type
; /* A DECL. */
111 typedef tree compare_type
; /* An identifier. */
113 static hashval_t
hash (const value_type decl
)
115 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl
));
117 static bool equal (const value_type existing
, compare_type candidate
)
119 tree name
= DECL_ASSEMBLER_NAME_RAW (existing
);
120 return candidate
== name
;
123 static inline void mark_empty (value_type
&p
) {p
= NULL_TREE
;}
124 static inline bool is_empty (value_type p
) {return !p
;}
126 static bool is_deleted (value_type e
)
128 return e
== reinterpret_cast <value_type
> (1);
130 static void mark_deleted (value_type
&e
)
132 e
= reinterpret_cast <value_type
> (1);
136 /* A hash table of decls keyed by mangled name. Used to figure out if
137 we need compatibility aliases. */
138 static GTY(()) hash_table
<mangled_decl_hash
> *mangled_decls
;
140 /* Nonzero if we're done parsing and into end-of-file activities. */
144 /* True if note_mangling_alias should enqueue mangling aliases for
145 later generation, rather than emitting them right away. */
147 bool defer_mangling_aliases
= true;
150 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
151 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
152 that apply to the function). */
155 build_memfn_type (tree fntype
, tree ctype
, cp_cv_quals quals
,
156 cp_ref_qualifier rqual
)
158 if (fntype
== error_mark_node
|| ctype
== error_mark_node
)
159 return error_mark_node
;
161 gcc_assert (TREE_CODE (fntype
) == FUNCTION_TYPE
162 || TREE_CODE (fntype
) == METHOD_TYPE
);
164 cp_cv_quals type_quals
= quals
& ~TYPE_QUAL_RESTRICT
;
165 ctype
= cp_build_qualified_type (ctype
, type_quals
);
168 = build_method_type_directly (ctype
, TREE_TYPE (fntype
),
169 (TREE_CODE (fntype
) == METHOD_TYPE
170 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype
))
171 : TYPE_ARG_TYPES (fntype
)));
172 if (tree attrs
= TYPE_ATTRIBUTES (fntype
))
173 newtype
= cp_build_type_attribute_variant (newtype
, attrs
);
174 newtype
= build_cp_fntype_variant (newtype
, rqual
,
175 TYPE_RAISES_EXCEPTIONS (fntype
),
176 TYPE_HAS_LATE_RETURN_TYPE (fntype
));
181 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
182 return type changed to NEW_RET. */
185 change_return_type (tree new_ret
, tree fntype
)
187 if (new_ret
== error_mark_node
)
190 if (same_type_p (new_ret
, TREE_TYPE (fntype
)))
194 tree args
= TYPE_ARG_TYPES (fntype
);
196 if (TREE_CODE (fntype
) == FUNCTION_TYPE
)
198 newtype
= build_function_type (new_ret
, args
);
199 newtype
= apply_memfn_quals (newtype
,
200 type_memfn_quals (fntype
));
203 newtype
= build_method_type_directly
204 (class_of_this_parm (fntype
), new_ret
, TREE_CHAIN (args
));
206 if (tree attrs
= TYPE_ATTRIBUTES (fntype
))
207 newtype
= cp_build_type_attribute_variant (newtype
, attrs
);
208 newtype
= cxx_copy_lang_qualifiers (newtype
, fntype
);
213 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
217 cp_build_parm_decl (tree fn
, tree name
, tree type
)
219 tree parm
= build_decl (input_location
,
220 PARM_DECL
, name
, type
);
221 DECL_CONTEXT (parm
) = fn
;
223 /* DECL_ARG_TYPE is only used by the back end and the back end never
225 if (!processing_template_decl
)
226 DECL_ARG_TYPE (parm
) = type_passed_as (type
);
231 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
235 build_artificial_parm (tree fn
, tree name
, tree type
)
237 tree parm
= cp_build_parm_decl (fn
, name
, type
);
238 DECL_ARTIFICIAL (parm
) = 1;
239 /* All our artificial parms are implicitly `const'; they cannot be
241 TREE_READONLY (parm
) = 1;
245 /* Constructors for types with virtual baseclasses need an "in-charge" flag
246 saying whether this constructor is responsible for initialization of
247 virtual baseclasses or not. All destructors also need this "in-charge"
248 flag, which additionally determines whether or not the destructor should
249 free the memory for the object.
251 This function adds the "in-charge" flag to member function FN if
252 appropriate. It is called from grokclassfn and tsubst.
253 FN must be either a constructor or destructor.
255 The in-charge flag follows the 'this' parameter, and is followed by the
256 VTT parm (if any), then the user-written parms. */
259 maybe_retrofit_in_chrg (tree fn
)
261 tree basetype
, arg_types
, parms
, parm
, fntype
;
263 /* If we've already add the in-charge parameter don't do it again. */
264 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
267 /* When processing templates we can't know, in general, whether or
268 not we're going to have virtual baseclasses. */
269 if (processing_template_decl
)
272 /* We don't need an in-charge parameter for constructors that don't
273 have virtual bases. */
274 if (DECL_CONSTRUCTOR_P (fn
)
275 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
278 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
279 basetype
= TREE_TYPE (TREE_VALUE (arg_types
));
280 arg_types
= TREE_CHAIN (arg_types
);
282 parms
= DECL_CHAIN (DECL_ARGUMENTS (fn
));
284 /* If this is a subobject constructor or destructor, our caller will
285 pass us a pointer to our VTT. */
286 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
288 parm
= build_artificial_parm (fn
, vtt_parm_identifier
, vtt_parm_type
);
290 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
291 DECL_CHAIN (parm
) = parms
;
294 /* ...and then to TYPE_ARG_TYPES. */
295 arg_types
= hash_tree_chain (vtt_parm_type
, arg_types
);
297 DECL_HAS_VTT_PARM_P (fn
) = 1;
300 /* Then add the in-charge parm (before the VTT parm). */
301 parm
= build_artificial_parm (fn
, in_charge_identifier
, integer_type_node
);
302 DECL_CHAIN (parm
) = parms
;
304 arg_types
= hash_tree_chain (integer_type_node
, arg_types
);
306 /* Insert our new parameter(s) into the list. */
307 DECL_CHAIN (DECL_ARGUMENTS (fn
)) = parms
;
309 /* And rebuild the function type. */
310 fntype
= build_method_type_directly (basetype
, TREE_TYPE (TREE_TYPE (fn
)),
312 if (TYPE_ATTRIBUTES (TREE_TYPE (fn
)))
313 fntype
= (cp_build_type_attribute_variant
314 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (fn
))));
315 fntype
= cxx_copy_lang_qualifiers (fntype
, TREE_TYPE (fn
));
316 TREE_TYPE (fn
) = fntype
;
318 /* Now we've got the in-charge parameter. */
319 DECL_HAS_IN_CHARGE_PARM_P (fn
) = 1;
322 /* Classes overload their constituent function names automatically.
323 When a function name is declared in a record structure,
324 its name is changed to it overloaded name. Since names for
325 constructors and destructors can conflict, we place a leading
328 CNAME is the name of the class we are grokking for.
330 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
332 FLAGS contains bits saying what's special about today's
333 arguments. DTOR_FLAG == DESTRUCTOR.
335 If FUNCTION is a destructor, then we must add the `auto-delete' field
336 as a second parameter. There is some hair associated with the fact
337 that we must "declare" this variable in the manner consistent with the
338 way the rest of the arguments were declared.
340 QUALS are the qualifiers for the this pointer. */
343 grokclassfn (tree ctype
, tree function
, enum overload_flags flags
)
345 tree fn_name
= DECL_NAME (function
);
347 /* Even within an `extern "C"' block, members get C++ linkage. See
348 [dcl.link] for details. */
349 SET_DECL_LANGUAGE (function
, lang_cplusplus
);
351 if (fn_name
== NULL_TREE
)
353 error ("name missing for member function");
354 fn_name
= get_identifier ("<anonymous>");
355 DECL_NAME (function
) = fn_name
;
358 DECL_CONTEXT (function
) = ctype
;
360 if (flags
== DTOR_FLAG
)
361 DECL_CXX_DESTRUCTOR_P (function
) = 1;
363 if (flags
== DTOR_FLAG
|| DECL_CONSTRUCTOR_P (function
))
364 maybe_retrofit_in_chrg (function
);
367 /* Create an ARRAY_REF, checking for the user doing things backwards
368 along the way. DECLTYPE_P is for N3276, as in the parser. */
371 grok_array_decl (location_t loc
, tree array_expr
, tree index_exp
,
376 tree orig_array_expr
= array_expr
;
377 tree orig_index_exp
= index_exp
;
378 tree overload
= NULL_TREE
;
380 if (error_operand_p (array_expr
) || error_operand_p (index_exp
))
381 return error_mark_node
;
383 if (processing_template_decl
)
385 if (type_dependent_expression_p (array_expr
)
386 || type_dependent_expression_p (index_exp
))
387 return build_min_nt_loc (loc
, ARRAY_REF
, array_expr
, index_exp
,
388 NULL_TREE
, NULL_TREE
);
389 array_expr
= build_non_dependent_expr (array_expr
);
390 index_exp
= build_non_dependent_expr (index_exp
);
393 type
= TREE_TYPE (array_expr
);
395 type
= non_reference (type
);
397 /* If they have an `operator[]', use that. */
398 if (MAYBE_CLASS_TYPE_P (type
) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp
)))
400 tsubst_flags_t complain
= tf_warning_or_error
;
402 complain
|= tf_decltype
;
403 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, array_expr
,
404 index_exp
, NULL_TREE
, &overload
, complain
);
410 /* Otherwise, create an ARRAY_REF for a pointer or array type.
411 It is a little-known fact that, if `a' is an array and `i' is
412 an int, you can write `i[a]', which means the same thing as
414 if (TREE_CODE (type
) == ARRAY_TYPE
|| VECTOR_TYPE_P (type
))
417 p1
= build_expr_type_conversion (WANT_POINTER
, array_expr
, false);
419 if (TREE_CODE (TREE_TYPE (index_exp
)) == ARRAY_TYPE
)
422 p2
= build_expr_type_conversion (WANT_POINTER
, index_exp
, false);
424 i1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, array_expr
,
426 i2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, index_exp
,
429 if ((p1
&& i2
) && (i1
&& p2
))
430 error ("ambiguous conversion for array subscript");
433 array_expr
= p1
, index_exp
= i2
;
435 array_expr
= p2
, index_exp
= i1
;
438 error ("invalid types %<%T[%T]%> for array subscript",
439 type
, TREE_TYPE (index_exp
));
440 return error_mark_node
;
443 if (array_expr
== error_mark_node
|| index_exp
== error_mark_node
)
444 error ("ambiguous conversion for array subscript");
446 if (TYPE_PTR_P (TREE_TYPE (array_expr
)))
447 array_expr
= mark_rvalue_use (array_expr
);
449 array_expr
= mark_lvalue_use_nonread (array_expr
);
450 index_exp
= mark_rvalue_use (index_exp
);
451 expr
= build_array_ref (input_location
, array_expr
, index_exp
);
453 if (processing_template_decl
&& expr
!= error_mark_node
)
455 if (overload
!= NULL_TREE
)
456 return (build_min_non_dep_op_overload
457 (ARRAY_REF
, expr
, overload
, orig_array_expr
, orig_index_exp
));
459 return build_min_non_dep (ARRAY_REF
, expr
, orig_array_expr
, orig_index_exp
,
460 NULL_TREE
, NULL_TREE
);
465 /* Given the cast expression EXP, checking out its validity. Either return
466 an error_mark_node if there was an unavoidable error, return a cast to
467 void for trying to delete a pointer w/ the value 0, or return the
468 call to delete. If DOING_VEC is true, we handle things differently
469 for doing an array delete.
470 Implements ARM $5.3.4. This is called from the parser. */
473 delete_sanity (tree exp
, tree size
, bool doing_vec
, int use_global_delete
,
474 tsubst_flags_t complain
)
478 if (exp
== error_mark_node
)
481 if (processing_template_decl
)
483 t
= build_min (DELETE_EXPR
, void_type_node
, exp
, size
);
484 DELETE_EXPR_USE_GLOBAL (t
) = use_global_delete
;
485 DELETE_EXPR_USE_VEC (t
) = doing_vec
;
486 TREE_SIDE_EFFECTS (t
) = 1;
490 /* An array can't have been allocated by new, so complain. */
491 if (TREE_CODE (TREE_TYPE (exp
)) == ARRAY_TYPE
)
492 warning (0, "deleting array %q#E", exp
);
494 t
= build_expr_type_conversion (WANT_POINTER
, exp
, true);
496 if (t
== NULL_TREE
|| t
== error_mark_node
)
498 error ("type %q#T argument given to %<delete%>, expected pointer",
500 return error_mark_node
;
503 type
= TREE_TYPE (t
);
505 /* As of Valley Forge, you can delete a pointer to const. */
507 /* You can't delete functions. */
508 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
510 error ("cannot delete a function. Only pointer-to-objects are "
511 "valid arguments to %<delete%>");
512 return error_mark_node
;
515 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
516 if (VOID_TYPE_P (TREE_TYPE (type
)))
518 warning (OPT_Wdelete_incomplete
, "deleting %qT is undefined", type
);
522 /* Deleting a pointer with the value zero is valid and has no effect. */
523 if (integer_zerop (t
))
524 return build1 (NOP_EXPR
, void_type_node
, t
);
527 return build_vec_delete (t
, /*maxindex=*/NULL_TREE
,
528 sfk_deleting_destructor
,
529 use_global_delete
, complain
);
531 return build_delete (type
, t
, sfk_deleting_destructor
,
532 LOOKUP_NORMAL
, use_global_delete
,
536 /* Report an error if the indicated template declaration is not the
537 sort of thing that should be a member template. */
540 check_member_template (tree tmpl
)
544 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
545 decl
= DECL_TEMPLATE_RESULT (tmpl
);
547 if (TREE_CODE (decl
) == FUNCTION_DECL
548 || DECL_ALIAS_TEMPLATE_P (tmpl
)
549 || (TREE_CODE (decl
) == TYPE_DECL
550 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))))
552 /* The parser rejects template declarations in local classes
553 (with the exception of generic lambdas). */
554 gcc_assert (!current_function_decl
|| LAMBDA_FUNCTION_P (decl
));
555 /* The parser rejects any use of virtual in a function template. */
556 gcc_assert (!(TREE_CODE (decl
) == FUNCTION_DECL
557 && DECL_VIRTUAL_P (decl
)));
559 /* The debug-information generating code doesn't know what to do
560 with member templates. */
561 DECL_IGNORED_P (tmpl
) = 1;
563 else if (variable_template_p (tmpl
))
566 error ("template declaration of %q#D", decl
);
569 /* Sanity check: report error if this function FUNCTION is not
570 really a member of the class (CTYPE) it is supposed to belong to.
571 TEMPLATE_PARMS is used to specify the template parameters of a member
572 template passed as FUNCTION_DECL. If the member template is passed as a
573 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
574 from the declaration. If the function is not a function template, it
576 It returns the original declaration for the function, NULL_TREE if
577 no declaration was found, error_mark_node if an error was emitted. */
580 check_classfn (tree ctype
, tree function
, tree template_parms
)
582 if (DECL_USE_TEMPLATE (function
)
583 && !(TREE_CODE (function
) == TEMPLATE_DECL
584 && DECL_TEMPLATE_SPECIALIZATION (function
))
585 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function
)))
586 /* Since this is a specialization of a member template,
587 we're not going to find the declaration in the class.
590 struct S { template <typename T> void f(T); };
591 template <> void S::f(int);
593 we're not going to find `S::f(int)', but there's no
594 reason we should, either. We let our callers know we didn't
595 find the method, but we don't complain. */
598 /* Basic sanity check: for a template function, the template parameters
599 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
600 if (TREE_CODE (function
) == TEMPLATE_DECL
)
603 && !comp_template_parms (template_parms
,
604 DECL_TEMPLATE_PARMS (function
)))
606 error ("template parameter lists provided don%'t match the "
607 "template parameters of %qD", function
);
608 return error_mark_node
;
610 template_parms
= DECL_TEMPLATE_PARMS (function
);
613 /* OK, is this a definition of a member template? */
614 bool is_template
= (template_parms
!= NULL_TREE
);
618 A destructor shall not be a member template. */
619 if (DECL_DESTRUCTOR_P (function
) && is_template
)
621 error ("destructor %qD declared as member template", function
);
622 return error_mark_node
;
625 /* We must enter the scope here, because conversion operators are
626 named by target type, and type equivalence relies on typenames
627 resolving within the scope of CTYPE. */
628 tree pushed_scope
= push_scope (ctype
);
629 tree matched
= NULL_TREE
;
630 tree fns
= get_class_binding (ctype
, DECL_NAME (function
));
632 for (ovl_iterator
iter (fns
); !matched
&& iter
; ++iter
)
636 /* A member template definition only matches a member template
638 if (is_template
!= (TREE_CODE (fndecl
) == TEMPLATE_DECL
))
641 if (!DECL_DECLARES_FUNCTION_P (fndecl
))
644 tree p1
= TYPE_ARG_TYPES (TREE_TYPE (function
));
645 tree p2
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
647 /* We cannot simply call decls_match because this doesn't work
648 for static member functions that are pretending to be
649 methods, and because the name may have been changed by
652 /* Get rid of the this parameter on functions that become
654 if (DECL_STATIC_FUNCTION_P (fndecl
)
655 && TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
656 p1
= TREE_CHAIN (p1
);
658 /* ref-qualifier or absence of same must match. */
659 if (type_memfn_rqual (TREE_TYPE (function
))
660 != type_memfn_rqual (TREE_TYPE (fndecl
)))
663 // Include constraints in the match.
664 tree c1
= get_constraints (function
);
665 tree c2
= get_constraints (fndecl
);
667 /* While finding a match, same types and params are not enough
668 if the function is versioned. Also check version ("target")
670 if (same_type_p (TREE_TYPE (TREE_TYPE (function
)),
671 TREE_TYPE (TREE_TYPE (fndecl
)))
672 && compparms (p1
, p2
)
673 && !targetm
.target_option
.function_versions (function
, fndecl
)
675 || comp_template_parms (template_parms
,
676 DECL_TEMPLATE_PARMS (fndecl
)))
677 && equivalent_constraints (c1
, c2
)
678 && (DECL_TEMPLATE_SPECIALIZATION (function
)
679 == DECL_TEMPLATE_SPECIALIZATION (fndecl
))
680 && (!DECL_TEMPLATE_SPECIALIZATION (function
)
681 || (DECL_TI_TEMPLATE (function
) == DECL_TI_TEMPLATE (fndecl
))))
687 if (!COMPLETE_TYPE_P (ctype
))
688 cxx_incomplete_type_error (function
, ctype
);
691 if (DECL_CONV_FN_P (function
))
692 fns
= get_class_binding (ctype
, conv_op_identifier
);
694 error_at (DECL_SOURCE_LOCATION (function
),
695 "no declaration matches %q#D", function
);
697 print_candidates (fns
);
698 else if (DECL_CONV_FN_P (function
))
699 inform (DECL_SOURCE_LOCATION (function
),
700 "no conversion operators declared");
702 inform (DECL_SOURCE_LOCATION (function
),
703 "no functions named %qD", function
);
704 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype
)),
705 "%#qT defined here", ctype
);
707 matched
= error_mark_node
;
711 pop_scope (pushed_scope
);
716 /* DECL is a function with vague linkage. Remember it so that at the
717 end of the translation unit we can decide whether or not to emit
721 note_vague_linkage_fn (tree decl
)
723 if (processing_template_decl
)
726 DECL_DEFER_OUTPUT (decl
) = 1;
727 vec_safe_push (deferred_fns
, decl
);
730 /* As above, but for variable template instantiations. */
733 note_variable_template_instantiation (tree decl
)
735 vec_safe_push (pending_statics
, decl
);
738 /* We have just processed the DECL, which is a static data member.
739 The other parameters are as for cp_finish_decl. */
742 finish_static_data_member_decl (tree decl
,
743 tree init
, bool init_const_expr_p
,
747 DECL_CONTEXT (decl
) = current_class_type
;
749 /* We cannot call pushdecl here, because that would fill in the
750 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
751 the right thing, namely, to put this decl out straight away. */
753 if (! processing_template_decl
)
754 vec_safe_push (pending_statics
, decl
);
756 if (LOCAL_CLASS_P (current_class_type
)
757 /* We already complained about the template definition. */
758 && !DECL_TEMPLATE_INSTANTIATION (decl
))
759 permerror (input_location
, "local class %q#T shall not have static data member %q#D",
760 current_class_type
, decl
);
762 for (tree t
= current_class_type
; TYPE_P (t
);
763 t
= CP_TYPE_CONTEXT (t
))
764 if (TYPE_UNNAMED_P (t
))
766 if (permerror (DECL_SOURCE_LOCATION (decl
),
767 "static data member %qD in unnamed class", decl
))
768 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)),
769 "unnamed class defined here");
773 DECL_IN_AGGR_P (decl
) = 1;
775 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
776 && TYPE_DOMAIN (TREE_TYPE (decl
)) == NULL_TREE
)
777 SET_VAR_HAD_UNKNOWN_BOUND (decl
);
781 /* Similarly to start_decl_1, we want to complete the type in order
782 to do the right thing in cp_apply_type_quals_to_decl, possibly
783 clear TYPE_QUAL_CONST (c++/65579). */
784 tree type
= TREE_TYPE (decl
) = complete_type (TREE_TYPE (decl
));
785 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
788 cp_finish_decl (decl
, init
, init_const_expr_p
, asmspec_tree
, flags
);
791 /* DECLARATOR and DECLSPECS correspond to a class member. The other
792 parameters are as for cp_finish_decl. Return the DECL for the
793 class member declared. */
796 grokfield (const cp_declarator
*declarator
,
797 cp_decl_specifier_seq
*declspecs
,
798 tree init
, bool init_const_expr_p
,
803 const char *asmspec
= 0;
808 && TREE_CODE (init
) == TREE_LIST
809 && TREE_VALUE (init
) == error_mark_node
810 && TREE_CHAIN (init
) == NULL_TREE
)
813 value
= grokdeclarator (declarator
, declspecs
, FIELD
, init
!= 0, &attrlist
);
814 if (! value
|| value
== error_mark_node
)
815 /* friend or constructor went bad. */
816 return error_mark_node
;
817 if (TREE_TYPE (value
) == error_mark_node
)
820 if (TREE_CODE (value
) == TYPE_DECL
&& init
)
822 error ("typedef %qD is initialized (use decltype instead)", value
);
826 /* Pass friendly classes back. */
827 if (value
== void_type_node
)
831 name
= DECL_NAME (value
);
833 if (name
!= NULL_TREE
)
835 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
837 error ("explicit template argument list not allowed");
838 return error_mark_node
;
841 if (IDENTIFIER_POINTER (name
)[0] == '_'
842 && id_equal (name
, "_vptr"))
843 error ("member %qD conflicts with virtual function table field name",
847 /* Stash away type declarations. */
848 if (TREE_CODE (value
) == TYPE_DECL
)
850 DECL_NONLOCAL (value
) = 1;
851 DECL_CONTEXT (value
) = current_class_type
;
857 /* If this is a typedef that names the class for linkage purposes
858 (7.1.3p8), apply any attributes directly to the type. */
859 if (OVERLOAD_TYPE_P (TREE_TYPE (value
))
860 && value
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
861 attrflags
= ATTR_FLAG_TYPE_IN_PLACE
;
863 cplus_decl_attributes (&value
, attrlist
, attrflags
);
866 if (decl_spec_seq_has_spec_p (declspecs
, ds_typedef
)
867 && TREE_TYPE (value
) != error_mark_node
868 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))) != value
)
869 set_underlying_type (value
);
871 /* It's important that push_template_decl below follows
872 set_underlying_type above so that the created template
873 carries the properly set type of VALUE. */
874 if (processing_template_decl
)
875 value
= push_template_decl (value
);
877 record_locally_defined_typedef (value
);
881 int friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
883 if (!friendp
&& DECL_IN_AGGR_P (value
))
885 error ("%qD is already defined in %qT", value
, DECL_CONTEXT (value
));
886 return void_type_node
;
889 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
890 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
894 if (TREE_CODE (value
) == FUNCTION_DECL
)
896 if (init
== ridpointers
[(int)RID_DELETE
])
898 if (friendp
&& decl_defined_p (value
))
900 error ("redefinition of %q#D", value
);
901 inform (DECL_SOURCE_LOCATION (value
),
902 "%q#D previously defined here", value
);
906 DECL_DELETED_FN (value
) = 1;
907 DECL_DECLARED_INLINE_P (value
) = 1;
908 DECL_INITIAL (value
) = error_mark_node
;
911 else if (init
== ridpointers
[(int)RID_DEFAULT
])
913 if (defaultable_fn_check (value
))
915 DECL_DEFAULTED_FN (value
) = 1;
916 DECL_INITIALIZED_IN_CLASS_P (value
) = 1;
917 DECL_DECLARED_INLINE_P (value
) = 1;
920 else if (TREE_CODE (init
) == DEFAULT_ARG
)
921 error ("invalid initializer for member function %qD", value
);
922 else if (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
)
924 if (integer_zerop (init
))
925 DECL_PURE_VIRTUAL_P (value
) = 1;
926 else if (error_operand_p (init
))
927 ; /* An error has already been reported. */
929 error ("invalid initializer for member function %qD",
934 gcc_assert (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
);
936 error ("initializer specified for friend function %qD",
939 error ("initializer specified for static member function %qD",
943 else if (TREE_CODE (value
) == FIELD_DECL
)
944 /* C++11 NSDMI, keep going. */;
945 else if (!VAR_P (value
))
949 /* Pass friend decls back. */
950 if ((TREE_CODE (value
) == FUNCTION_DECL
951 || TREE_CODE (value
) == TEMPLATE_DECL
)
952 && DECL_CONTEXT (value
) != current_class_type
)
955 /* Need to set this before push_template_decl. */
957 DECL_CONTEXT (value
) = current_class_type
;
959 if (processing_template_decl
&& VAR_OR_FUNCTION_DECL_P (value
))
961 value
= push_template_decl (value
);
962 if (error_operand_p (value
))
963 return error_mark_node
;
967 cplus_decl_attributes (&value
, attrlist
, 0);
969 if (init
&& DIRECT_LIST_INIT_P (init
))
970 flags
= LOOKUP_NORMAL
;
972 flags
= LOOKUP_IMPLICIT
;
974 switch (TREE_CODE (value
))
977 finish_static_data_member_decl (value
, init
, init_const_expr_p
,
978 asmspec_tree
, flags
);
983 error ("%<asm%> specifiers are not permitted on non-static data members");
984 if (DECL_INITIAL (value
) == error_mark_node
)
985 init
= error_mark_node
;
986 cp_finish_decl (value
, init
, /*init_const_expr_p=*/false,
988 DECL_IN_AGGR_P (value
) = 1;
993 set_user_assembler_name (value
, asmspec
);
995 cp_finish_decl (value
,
997 /*init_const_expr_p=*/false,
998 asmspec_tree
, flags
);
1000 /* Pass friends back this way. */
1001 if (DECL_FRIEND_P (value
))
1002 return void_type_node
;
1004 DECL_IN_AGGR_P (value
) = 1;
1013 /* Like `grokfield', but for bitfields.
1014 WIDTH is the width of the bitfield, a constant expression.
1015 The other parameters are as for grokfield. */
1018 grokbitfield (const cp_declarator
*declarator
,
1019 cp_decl_specifier_seq
*declspecs
, tree width
, tree init
,
1022 tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
,
1023 init
!= NULL_TREE
, &attrlist
);
1025 if (value
== error_mark_node
)
1026 return NULL_TREE
; /* friends went bad. */
1027 if (TREE_TYPE (value
) == error_mark_node
)
1030 /* Pass friendly classes back. */
1031 if (VOID_TYPE_P (value
))
1032 return void_type_node
;
1034 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value
))
1035 && (POINTER_TYPE_P (value
)
1036 || !dependent_type_p (TREE_TYPE (value
))))
1038 error ("bit-field %qD with non-integral type", value
);
1039 return error_mark_node
;
1042 if (TREE_CODE (value
) == TYPE_DECL
)
1044 error ("cannot declare %qD to be a bit-field type", value
);
1048 /* Usually, finish_struct_1 catches bitfields with invalid types.
1049 But, in the case of bitfields with function type, we confuse
1050 ourselves into thinking they are member functions, so we must
1052 if (TREE_CODE (value
) == FUNCTION_DECL
)
1054 error ("cannot declare bit-field %qD with function type",
1059 if (width
&& TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value
)))
1061 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1066 if (DECL_IN_AGGR_P (value
))
1068 error ("%qD is already defined in the class %qT", value
,
1069 DECL_CONTEXT (value
));
1070 return void_type_node
;
1073 if (TREE_STATIC (value
))
1075 error ("static member %qD cannot be a bit-field", value
);
1079 int flags
= LOOKUP_IMPLICIT
;
1080 if (init
&& DIRECT_LIST_INIT_P (init
))
1081 flags
= LOOKUP_NORMAL
;
1082 cp_finish_decl (value
, init
, false, NULL_TREE
, flags
);
1084 if (width
!= error_mark_node
)
1086 /* The width must be an integer type. */
1087 if (!type_dependent_expression_p (width
)
1088 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width
)))
1089 error ("width of bit-field %qD has non-integral type %qT", value
,
1093 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1094 check_bitfield_decl picks it from there later and sets DECL_SIZE
1096 DECL_BIT_FIELD_REPRESENTATIVE (value
) = width
;
1097 SET_DECL_C_BIT_FIELD (value
);
1101 DECL_IN_AGGR_P (value
) = 1;
1104 cplus_decl_attributes (&value
, attrlist
, /*flags=*/0);
1110 /* Returns true iff ATTR is an attribute which needs to be applied at
1111 instantiation time rather than template definition time. */
1114 is_late_template_attribute (tree attr
, tree decl
)
1116 tree name
= get_attribute_name (attr
);
1117 tree args
= TREE_VALUE (attr
);
1118 const struct attribute_spec
*spec
= lookup_attribute_spec (name
);
1122 /* Unknown attribute. */
1125 /* Attribute weak handling wants to write out assembly right away. */
1126 if (is_attribute_p ("weak", name
))
1129 /* Attributes used and unused are applied directly to typedefs for the
1130 benefit of maybe_warn_unused_local_typedefs. */
1131 if (TREE_CODE (decl
) == TYPE_DECL
1132 && (is_attribute_p ("unused", name
)
1133 || is_attribute_p ("used", name
)))
1136 /* Attribute tls_model wants to modify the symtab. */
1137 if (is_attribute_p ("tls_model", name
))
1140 /* #pragma omp declare simd attribute needs to be always deferred. */
1142 && is_attribute_p ("omp declare simd", name
))
1145 /* An attribute pack is clearly dependent. */
1146 if (args
&& PACK_EXPANSION_P (args
))
1149 /* If any of the arguments are dependent expressions, we can't evaluate
1150 the attribute until instantiation time. */
1151 for (arg
= args
; arg
; arg
= TREE_CHAIN (arg
))
1153 tree t
= TREE_VALUE (arg
);
1155 /* If the first attribute argument is an identifier, only consider
1156 second and following arguments. Attributes like mode, format,
1157 cleanup and several target specific attributes aren't late
1158 just because they have an IDENTIFIER_NODE as first argument. */
1159 if (arg
== args
&& attribute_takes_identifier_p (name
)
1160 && identifier_p (t
))
1163 if (value_dependent_expression_p (t
)
1164 || type_dependent_expression_p (t
))
1168 if (TREE_CODE (decl
) == TYPE_DECL
1170 || spec
->type_required
)
1172 tree type
= TYPE_P (decl
) ? decl
: TREE_TYPE (decl
);
1174 /* We can't apply any attributes to a completely unknown type until
1175 instantiation time. */
1176 enum tree_code code
= TREE_CODE (type
);
1177 if (code
== TEMPLATE_TYPE_PARM
1178 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
1179 || code
== TYPENAME_TYPE
)
1181 /* Also defer most attributes on dependent types. This is not
1182 necessary in all cases, but is the better default. */
1183 else if (dependent_type_p (type
)
1184 /* But some attributes specifically apply to templates. */
1185 && !is_attribute_p ("abi_tag", name
)
1186 && !is_attribute_p ("deprecated", name
)
1187 && !is_attribute_p ("visibility", name
))
1196 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1197 applied at instantiation time and return them. If IS_DEPENDENT is true,
1198 the declaration itself is dependent, so all attributes should be applied
1199 at instantiation time. */
1202 splice_template_attributes (tree
*attr_p
, tree decl
)
1205 tree late_attrs
= NULL_TREE
;
1206 tree
*q
= &late_attrs
;
1213 if (is_late_template_attribute (*p
, decl
))
1215 ATTR_IS_DEPENDENT (*p
) = 1;
1217 *p
= TREE_CHAIN (*p
);
1218 q
= &TREE_CHAIN (*q
);
1222 p
= &TREE_CHAIN (*p
);
1228 /* Remove any late attributes from the list in ATTR_P and attach them to
1232 save_template_attributes (tree
*attr_p
, tree
*decl_p
, int flags
)
1236 if (attr_p
&& *attr_p
== error_mark_node
)
1239 tree late_attrs
= splice_template_attributes (attr_p
, *decl_p
);
1243 if (DECL_P (*decl_p
))
1244 q
= &DECL_ATTRIBUTES (*decl_p
);
1246 q
= &TYPE_ATTRIBUTES (*decl_p
);
1248 tree old_attrs
= *q
;
1250 /* Merge the late attributes at the beginning with the attribute
1252 late_attrs
= merge_attributes (late_attrs
, *q
);
1253 if (*q
!= late_attrs
1254 && !DECL_P (*decl_p
)
1255 && !(flags
& ATTR_FLAG_TYPE_IN_PLACE
))
1257 if (!dependent_type_p (*decl_p
))
1258 *decl_p
= cp_build_type_attribute_variant (*decl_p
, late_attrs
);
1261 *decl_p
= build_variant_type_copy (*decl_p
);
1262 TYPE_ATTRIBUTES (*decl_p
) = late_attrs
;
1268 if (!DECL_P (*decl_p
) && *decl_p
== TYPE_MAIN_VARIANT (*decl_p
))
1270 /* We've added new attributes directly to the main variant, so
1271 now we need to update all of the other variants to include
1272 these new attributes. */
1274 for (variant
= TYPE_NEXT_VARIANT (*decl_p
); variant
;
1275 variant
= TYPE_NEXT_VARIANT (variant
))
1277 gcc_assert (TYPE_ATTRIBUTES (variant
) == old_attrs
);
1278 TYPE_ATTRIBUTES (variant
) = TYPE_ATTRIBUTES (*decl_p
);
1283 /* True if ATTRS contains any dependent attributes that affect type
1287 any_dependent_type_attributes_p (tree attrs
)
1289 for (tree a
= attrs
; a
; a
= TREE_CHAIN (a
))
1290 if (ATTR_IS_DEPENDENT (a
))
1292 const attribute_spec
*as
= lookup_attribute_spec (TREE_PURPOSE (a
));
1293 if (as
&& as
->affects_type_identity
)
1299 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1300 to a typedef which gives a previously unnamed class or enum a name for
1301 linkage purposes. */
1304 attributes_naming_typedef_ok (tree attrs
)
1306 for (; attrs
; attrs
= TREE_CHAIN (attrs
))
1308 tree name
= get_attribute_name (attrs
);
1309 if (is_attribute_p ("vector_size", name
))
1315 /* Like reconstruct_complex_type, but handle also template trees. */
1318 cp_reconstruct_complex_type (tree type
, tree bottom
)
1322 if (TYPE_PTR_P (type
))
1324 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1325 outer
= build_pointer_type_for_mode (inner
, TYPE_MODE (type
),
1326 TYPE_REF_CAN_ALIAS_ALL (type
));
1328 else if (TYPE_REF_P (type
))
1330 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1331 outer
= build_reference_type_for_mode (inner
, TYPE_MODE (type
),
1332 TYPE_REF_CAN_ALIAS_ALL (type
));
1334 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1336 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1337 outer
= build_cplus_array_type (inner
, TYPE_DOMAIN (type
));
1338 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1339 element type qualification will be handled by the recursive
1340 cp_reconstruct_complex_type call and cp_build_qualified_type
1341 for ARRAY_TYPEs changes the element type. */
1344 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
1346 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1347 outer
= build_function_type (inner
, TYPE_ARG_TYPES (type
));
1348 outer
= apply_memfn_quals (outer
, type_memfn_quals (type
));
1350 else if (TREE_CODE (type
) == METHOD_TYPE
)
1352 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1353 /* The build_method_type_directly() routine prepends 'this' to argument list,
1354 so we must compensate by getting rid of it. */
1356 = build_method_type_directly
1357 (class_of_this_parm (type
), inner
,
1358 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
1360 else if (TREE_CODE (type
) == OFFSET_TYPE
)
1362 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1363 outer
= build_offset_type (TYPE_OFFSET_BASETYPE (type
), inner
);
1368 if (TYPE_ATTRIBUTES (type
))
1369 outer
= cp_build_type_attribute_variant (outer
, TYPE_ATTRIBUTES (type
));
1370 outer
= cp_build_qualified_type (outer
, cp_type_quals (type
));
1371 outer
= cxx_copy_lang_qualifiers (outer
, type
);
1376 /* Replaces any constexpr expression that may be into the attributes
1377 arguments with their reduced value. */
1380 cp_check_const_attributes (tree attributes
)
1382 if (attributes
== error_mark_node
)
1386 for (attr
= attributes
; attr
; attr
= TREE_CHAIN (attr
))
1389 for (arg
= TREE_VALUE (attr
); arg
; arg
= TREE_CHAIN (arg
))
1391 tree expr
= TREE_VALUE (arg
);
1393 TREE_VALUE (arg
) = fold_non_dependent_expr (expr
);
1398 /* Return true if TYPE is an OpenMP mappable type. */
1400 cp_omp_mappable_type (tree type
)
1402 /* Mappable type has to be complete. */
1403 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
1405 /* Arrays have mappable type if the elements have mappable type. */
1406 while (TREE_CODE (type
) == ARRAY_TYPE
)
1407 type
= TREE_TYPE (type
);
1408 /* A mappable type cannot contain virtual members. */
1409 if (CLASS_TYPE_P (type
) && CLASSTYPE_VTABLES (type
))
1411 /* All data members must be non-static. */
1412 if (CLASS_TYPE_P (type
))
1415 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1418 /* All fields must have mappable types. */
1419 else if (TREE_CODE (field
) == FIELD_DECL
1420 && !cp_omp_mappable_type (TREE_TYPE (field
)))
1426 /* Return the last pushed declaration for the symbol DECL or NULL
1427 when no such declaration exists. */
1430 find_last_decl (tree decl
)
1432 tree last_decl
= NULL_TREE
;
1434 if (tree name
= DECL_P (decl
) ? DECL_NAME (decl
) : NULL_TREE
)
1436 /* Look up the declaration in its scope. */
1437 tree pushed_scope
= NULL_TREE
;
1438 if (tree ctype
= DECL_CONTEXT (decl
))
1439 pushed_scope
= push_scope (ctype
);
1441 last_decl
= lookup_name (name
);
1444 pop_scope (pushed_scope
);
1446 /* The declaration may be a member conversion operator
1447 or a bunch of overfloads (handle the latter below). */
1448 if (last_decl
&& BASELINK_P (last_decl
))
1449 last_decl
= BASELINK_FUNCTIONS (last_decl
);
1455 if (DECL_P (last_decl
) || TREE_CODE (last_decl
) == OVERLOAD
)
1457 /* A set of overloads of the same function. */
1458 for (lkp_iterator
iter (last_decl
); iter
; ++iter
)
1460 if (TREE_CODE (*iter
) == OVERLOAD
)
1463 if (decls_match (decl
, *iter
, /*record_decls=*/false))
1472 /* Like decl_attributes, but handle C++ complexity. */
1475 cplus_decl_attributes (tree
*decl
, tree attributes
, int flags
)
1477 if (*decl
== NULL_TREE
|| *decl
== void_type_node
1478 || *decl
== error_mark_node
)
1481 /* Add implicit "omp declare target" attribute if requested. */
1482 if (scope_chain
->omp_declare_target_attribute
1484 && (TREE_STATIC (*decl
) || DECL_EXTERNAL (*decl
)))
1485 || TREE_CODE (*decl
) == FUNCTION_DECL
))
1488 && DECL_CLASS_SCOPE_P (*decl
))
1489 error ("%q+D static data member inside of declare target directive",
1491 else if (!processing_template_decl
1493 && !cp_omp_mappable_type (TREE_TYPE (*decl
)))
1494 error ("%q+D in declare target directive does not have mappable type",
1497 attributes
= tree_cons (get_identifier ("omp declare target"),
1498 NULL_TREE
, attributes
);
1501 if (processing_template_decl
)
1503 if (check_for_bare_parameter_packs (attributes
))
1506 save_template_attributes (&attributes
, decl
, flags
);
1509 cp_check_const_attributes (attributes
);
1511 if (TREE_CODE (*decl
) == TEMPLATE_DECL
)
1512 decl
= &DECL_TEMPLATE_RESULT (*decl
);
1514 if (TREE_TYPE (*decl
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl
)))
1517 = decl_attributes (decl
, attributes
, flags
| ATTR_FLAG_FUNCTION_NEXT
);
1518 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl
)),
1523 tree last_decl
= find_last_decl (*decl
);
1524 decl_attributes (decl
, attributes
, flags
, last_decl
);
1527 if (TREE_CODE (*decl
) == TYPE_DECL
)
1528 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl
), TREE_TYPE (*decl
));
1530 /* Propagate deprecation out to the template. */
1531 if (TREE_DEPRECATED (*decl
))
1532 if (tree ti
= get_template_info (*decl
))
1534 tree tmpl
= TI_TEMPLATE (ti
);
1535 tree pattern
= (TYPE_P (*decl
) ? TREE_TYPE (tmpl
)
1536 : DECL_TEMPLATE_RESULT (tmpl
));
1537 if (*decl
== pattern
)
1538 TREE_DEPRECATED (tmpl
) = true;
1542 /* Walks through the namespace- or function-scope anonymous union
1543 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1544 Returns one of the fields for use in the mangled name. */
1547 build_anon_union_vars (tree type
, tree object
)
1549 tree main_decl
= NULL_TREE
;
1552 /* Rather than write the code to handle the non-union case,
1553 just give an error. */
1554 if (TREE_CODE (type
) != UNION_TYPE
)
1556 error ("anonymous struct not inside named type");
1557 return error_mark_node
;
1560 for (field
= TYPE_FIELDS (type
);
1562 field
= DECL_CHAIN (field
))
1567 if (DECL_ARTIFICIAL (field
))
1569 if (TREE_CODE (field
) != FIELD_DECL
)
1571 permerror (DECL_SOURCE_LOCATION (field
),
1572 "%q#D invalid; an anonymous union can only "
1573 "have non-static data members", field
);
1577 if (TREE_PRIVATE (field
))
1578 permerror (DECL_SOURCE_LOCATION (field
),
1579 "private member %q#D in anonymous union", field
);
1580 else if (TREE_PROTECTED (field
))
1581 permerror (DECL_SOURCE_LOCATION (field
),
1582 "protected member %q#D in anonymous union", field
);
1584 if (processing_template_decl
)
1585 ref
= build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
, object
,
1586 DECL_NAME (field
), NULL_TREE
);
1588 ref
= build_class_member_access_expr (object
, field
, NULL_TREE
,
1589 false, tf_warning_or_error
);
1591 if (DECL_NAME (field
))
1595 decl
= build_decl (input_location
,
1596 VAR_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
1597 DECL_ANON_UNION_VAR_P (decl
) = 1;
1598 DECL_ARTIFICIAL (decl
) = 1;
1600 base
= get_base_address (object
);
1601 TREE_PUBLIC (decl
) = TREE_PUBLIC (base
);
1602 TREE_STATIC (decl
) = TREE_STATIC (base
);
1603 DECL_EXTERNAL (decl
) = DECL_EXTERNAL (base
);
1605 SET_DECL_VALUE_EXPR (decl
, ref
);
1606 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1608 decl
= pushdecl (decl
);
1610 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1611 decl
= build_anon_union_vars (TREE_TYPE (field
), ref
);
1615 if (main_decl
== NULL_TREE
)
1622 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1623 anonymous union, then all members must be laid out together. PUBLIC_P
1624 is nonzero if this union is not declared static. */
1627 finish_anon_union (tree anon_union_decl
)
1633 if (anon_union_decl
== error_mark_node
)
1636 type
= TREE_TYPE (anon_union_decl
);
1637 public_p
= TREE_PUBLIC (anon_union_decl
);
1639 /* The VAR_DECL's context is the same as the TYPE's context. */
1640 DECL_CONTEXT (anon_union_decl
) = DECL_CONTEXT (TYPE_NAME (type
));
1642 if (TYPE_FIELDS (type
) == NULL_TREE
)
1647 error ("namespace-scope anonymous aggregates must be static");
1651 main_decl
= build_anon_union_vars (type
, anon_union_decl
);
1652 if (main_decl
== error_mark_node
)
1654 if (main_decl
== NULL_TREE
)
1656 pedwarn (input_location
, 0, "anonymous union with no members");
1660 if (!processing_template_decl
)
1662 /* Use main_decl to set the mangled name. */
1663 DECL_NAME (anon_union_decl
) = DECL_NAME (main_decl
);
1664 maybe_commonize_var (anon_union_decl
);
1665 if (TREE_STATIC (anon_union_decl
) || DECL_EXTERNAL (anon_union_decl
))
1666 mangle_decl (anon_union_decl
);
1667 DECL_NAME (anon_union_decl
) = NULL_TREE
;
1670 pushdecl (anon_union_decl
);
1671 cp_finish_decl (anon_union_decl
, NULL_TREE
, false, NULL_TREE
, 0);
1674 /* Auxiliary functions to make type signatures for
1675 `operator new' and `operator delete' correspond to
1676 what compiler will be expecting. */
1679 coerce_new_type (tree type
)
1682 tree args
= TYPE_ARG_TYPES (type
);
1684 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1686 if (!same_type_p (TREE_TYPE (type
), ptr_type_node
))
1689 error ("%<operator new%> must return type %qT", ptr_type_node
);
1692 if (args
&& args
!= void_list_node
)
1694 if (TREE_PURPOSE (args
))
1696 /* [basic.stc.dynamic.allocation]
1698 The first parameter shall not have an associated default
1700 error ("the first parameter of %<operator new%> cannot "
1701 "have a default argument");
1702 /* Throw away the default argument. */
1703 TREE_PURPOSE (args
) = NULL_TREE
;
1706 if (!same_type_p (TREE_VALUE (args
), size_type_node
))
1709 args
= TREE_CHAIN (args
);
1716 permerror (input_location
, "%<operator new%> takes type %<size_t%> (%qT) "
1717 "as first parameter", size_type_node
);
1722 args
= tree_cons (NULL_TREE
, size_type_node
, args
);
1725 type
= (cxx_copy_lang_qualifiers
1726 (build_function_type (ptr_type_node
, args
),
1735 coerce_delete_type (tree type
)
1738 tree args
= TYPE_ARG_TYPES (type
);
1740 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1742 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
1745 error ("%<operator delete%> must return type %qT", void_type_node
);
1748 if (!args
|| args
== void_list_node
1749 || !same_type_p (TREE_VALUE (args
), ptr_type_node
))
1752 if (args
&& args
!= void_list_node
)
1753 args
= TREE_CHAIN (args
);
1754 error ("%<operator delete%> takes type %qT as first parameter",
1760 args
= tree_cons (NULL_TREE
, ptr_type_node
, args
);
1763 type
= (cxx_copy_lang_qualifiers
1764 (build_function_type (void_type_node
, args
),
1773 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1774 and mark them as needed. */
1777 mark_vtable_entries (tree decl
)
1780 unsigned HOST_WIDE_INT idx
;
1782 /* It's OK for the vtable to refer to deprecated virtual functions. */
1783 warning_sentinel
w(warn_deprecated_decl
);
1785 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
1790 STRIP_NOPS (fnaddr
);
1792 if (TREE_CODE (fnaddr
) != ADDR_EXPR
1793 && TREE_CODE (fnaddr
) != FDESC_EXPR
)
1794 /* This entry is an offset: a virtual base class offset, a
1795 virtual call offset, an RTTI offset, etc. */
1798 fn
= TREE_OPERAND (fnaddr
, 0);
1799 TREE_ADDRESSABLE (fn
) = 1;
1800 /* When we don't have vcall offsets, we output thunks whenever
1801 we output the vtables that contain them. With vcall offsets,
1802 we know all the thunks we'll need when we emit a virtual
1803 function, so we emit the thunks there instead. */
1804 if (DECL_THUNK_P (fn
))
1805 use_thunk (fn
, /*emit_p=*/0);
1806 /* Set the location, as marking the function could cause
1807 instantiation. We do not need to preserve the incoming
1808 location, as we're called from c_parse_final_cleanups, which
1809 takes care of that. */
1810 input_location
= DECL_SOURCE_LOCATION (fn
);
1815 /* Adjust the TLS model on variable DECL if need be, typically after
1816 the linkage of DECL has been modified. */
1819 adjust_var_decl_tls_model (tree decl
)
1821 if (CP_DECL_THREAD_LOCAL_P (decl
)
1822 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl
)))
1823 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
1826 /* Set DECL up to have the closest approximation of "initialized common"
1827 linkage available. */
1830 comdat_linkage (tree decl
)
1833 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1834 else if (TREE_CODE (decl
) == FUNCTION_DECL
1835 || (VAR_P (decl
) && DECL_ARTIFICIAL (decl
)))
1836 /* We can just emit function and compiler-generated variables
1837 statically; having multiple copies is (for the most part) only
1840 There are two correctness issues, however: the address of a
1841 template instantiation with external linkage should be the
1842 same, independent of what translation unit asks for the
1843 address, and this will not hold when we emit multiple copies of
1844 the function. However, there's little else we can do.
1846 Also, by default, the typeinfo implementation assumes that
1847 there will be only one copy of the string used as the name for
1848 each type. Therefore, if weak symbols are unavailable, the
1849 run-time library should perform a more conservative check; it
1850 should perform a string comparison, rather than an address
1852 TREE_PUBLIC (decl
) = 0;
1855 /* Static data member template instantiations, however, cannot
1856 have multiple copies. */
1857 if (DECL_INITIAL (decl
) == 0
1858 || DECL_INITIAL (decl
) == error_mark_node
)
1859 DECL_COMMON (decl
) = 1;
1860 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
1862 DECL_COMMON (decl
) = 1;
1863 DECL_INITIAL (decl
) = error_mark_node
;
1865 else if (!DECL_EXPLICIT_INSTANTIATION (decl
))
1867 /* We can't do anything useful; leave vars for explicit
1869 DECL_EXTERNAL (decl
) = 1;
1870 DECL_NOT_REALLY_EXTERN (decl
) = 0;
1874 if (TREE_PUBLIC (decl
))
1875 DECL_COMDAT (decl
) = 1;
1878 adjust_var_decl_tls_model (decl
);
1881 /* For win32 we also want to put explicit instantiations in
1882 linkonce sections, so that they will be merged with implicit
1883 instantiations; otherwise we get duplicate symbol errors.
1884 For Darwin we do not want explicit instantiations to be
1888 maybe_make_one_only (tree decl
)
1890 /* We used to say that this was not necessary on targets that support weak
1891 symbols, because the implicit instantiations will defer to the explicit
1892 one. However, that's not actually the case in SVR4; a strong definition
1893 after a weak one is an error. Also, not making explicit
1894 instantiations one_only means that we can end up with two copies of
1895 some template instantiations. */
1899 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1900 we can get away with not emitting them if they aren't used. We need
1901 to for variables so that cp_finish_decl will update their linkage,
1902 because their DECL_INITIAL may not have been set properly yet. */
1904 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1905 || (! DECL_EXPLICIT_INSTANTIATION (decl
)
1906 && ! DECL_TEMPLATE_SPECIALIZATION (decl
)))
1908 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1912 varpool_node
*node
= varpool_node::get_create (decl
);
1913 DECL_COMDAT (decl
) = 1;
1914 /* Mark it needed so we don't forget to emit it. */
1915 node
->forced_by_abi
= true;
1916 TREE_USED (decl
) = 1;
1918 adjust_var_decl_tls_model (decl
);
1923 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1924 This predicate will give the right answer during parsing of the
1925 function, which other tests may not. */
1928 vague_linkage_p (tree decl
)
1930 if (!TREE_PUBLIC (decl
))
1932 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
1933 maybe-in-charge 'tor variants; in that case we need to check one of
1934 the "clones" for the real linkage. But only in that case; before
1935 maybe_clone_body we haven't yet copied the linkage to the clones. */
1936 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl
)
1937 && !DECL_ABSTRACT_P (decl
)
1938 && DECL_CHAIN (decl
)
1939 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)))
1940 return vague_linkage_p (DECL_CHAIN (decl
));
1942 gcc_checking_assert (!DECL_COMDAT (decl
));
1945 /* Unfortunately, import_export_decl has not always been called
1946 before the function is processed, so we cannot simply check
1948 if (DECL_COMDAT (decl
)
1949 || (TREE_CODE (decl
) == FUNCTION_DECL
1950 && DECL_DECLARED_INLINE_P (decl
))
1951 || (DECL_LANG_SPECIFIC (decl
)
1952 && DECL_TEMPLATE_INSTANTIATION (decl
))
1953 || (VAR_P (decl
) && DECL_INLINE_VAR_P (decl
)))
1955 else if (DECL_FUNCTION_SCOPE_P (decl
))
1956 /* A local static in an inline effectively has vague linkage. */
1957 return (TREE_STATIC (decl
)
1958 && vague_linkage_p (DECL_CONTEXT (decl
)));
1963 /* Determine whether or not we want to specifically import or export CTYPE,
1964 using various heuristics. */
1967 import_export_class (tree ctype
)
1969 /* -1 for imported, 1 for exported. */
1970 int import_export
= 0;
1972 /* It only makes sense to call this function at EOF. The reason is
1973 that this function looks at whether or not the first non-inline
1974 non-abstract virtual member function has been defined in this
1975 translation unit. But, we can't possibly know that until we've
1976 seen the entire translation unit. */
1977 gcc_assert (at_eof
);
1979 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
1982 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1983 we will have CLASSTYPE_INTERFACE_ONLY set but not
1984 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1985 heuristic because someone will supply a #pragma implementation
1986 elsewhere, and deducing it here would produce a conflict. */
1987 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
1990 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
1992 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
1994 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
1995 && !flag_implicit_templates
)
1996 /* For a template class, without -fimplicit-templates, check the
1997 repository. If the virtual table is assigned to this
1998 translation unit, then export the class; otherwise, import
2000 import_export
= repo_export_class_p (ctype
) ? 1 : -1;
2001 else if (TYPE_POLYMORPHIC_P (ctype
))
2003 /* The ABI specifies that the virtual table and associated
2004 information are emitted with the key method, if any. */
2005 tree method
= CLASSTYPE_KEY_METHOD (ctype
);
2006 /* If weak symbol support is not available, then we must be
2007 careful not to emit the vtable when the key function is
2008 inline. An inline function can be defined in multiple
2009 translation units. If we were to emit the vtable in each
2010 translation unit containing a definition, we would get
2011 multiple definition errors at link-time. */
2012 if (method
&& (flag_weak
|| ! DECL_DECLARED_INLINE_P (method
)))
2013 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
2016 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2017 a definition anywhere else. */
2018 if (MULTIPLE_SYMBOL_SPACES
&& import_export
== -1)
2021 /* Allow back ends the chance to overrule the decision. */
2022 if (targetm
.cxx
.import_export_class
)
2023 import_export
= targetm
.cxx
.import_export_class (ctype
, import_export
);
2027 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
2028 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
2032 /* Return true if VAR has already been provided to the back end; in that
2033 case VAR should not be modified further by the front end. */
2035 var_finalized_p (tree var
)
2037 return varpool_node::get_create (var
)->definition
;
2040 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2041 must be emitted in this translation unit. Mark it as such. */
2044 mark_needed (tree decl
)
2046 TREE_USED (decl
) = 1;
2047 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2049 /* Extern inline functions don't become needed when referenced.
2050 If we know a method will be emitted in other TU and no new
2051 functions can be marked reachable, just use the external
2053 struct cgraph_node
*node
= cgraph_node::get_create (decl
);
2054 node
->forced_by_abi
= true;
2056 /* #pragma interface and -frepo code can call mark_needed for
2057 maybe-in-charge 'tors; mark the clones as well. */
2059 FOR_EACH_CLONE (clone
, decl
)
2060 mark_needed (clone
);
2062 else if (VAR_P (decl
))
2064 varpool_node
*node
= varpool_node::get_create (decl
);
2065 /* C++ frontend use mark_decl_references to force COMDAT variables
2066 to be output that might appear dead otherwise. */
2067 node
->forced_by_abi
= true;
2071 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2072 returns true if a definition of this entity should be provided in
2073 this object file. Callers use this function to determine whether
2074 or not to let the back end know that a definition of DECL is
2075 available in this translation unit. */
2078 decl_needed_p (tree decl
)
2080 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2081 /* This function should only be called at the end of the translation
2082 unit. We cannot be sure of whether or not something will be
2083 COMDAT until that point. */
2084 gcc_assert (at_eof
);
2086 /* All entities with external linkage that are not COMDAT/EXTERN should be
2087 emitted; they may be referred to from other object files. */
2088 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_REALLY_EXTERN (decl
))
2090 /* Functions marked "dllexport" must be emitted so that they are
2091 visible to other DLLs. */
2092 if (flag_keep_inline_dllexport
2093 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))
2096 /* When not optimizing, do not bother to produce definitions for extern
2098 if (DECL_REALLY_EXTERN (decl
)
2099 && ((TREE_CODE (decl
) != FUNCTION_DECL
2101 || (TREE_CODE (decl
) == FUNCTION_DECL
2102 && !opt_for_fn (decl
, optimize
)))
2103 && !lookup_attribute ("always_inline", decl
))
2106 /* If this entity was used, let the back end see it; it will decide
2107 whether or not to emit it into the object file. */
2108 if (TREE_USED (decl
))
2111 /* Virtual functions might be needed for devirtualization. */
2112 if (flag_devirtualize
2113 && TREE_CODE (decl
) == FUNCTION_DECL
2114 && DECL_VIRTUAL_P (decl
))
2117 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2118 reference to DECL might cause it to be emitted later. */
2122 /* If necessary, write out the vtables for the dynamic class CTYPE.
2123 Returns true if any vtables were emitted. */
2126 maybe_emit_vtables (tree ctype
)
2131 varpool_node
*current
= NULL
, *last
= NULL
;
2133 /* If the vtables for this class have already been emitted there is
2134 nothing more to do. */
2135 primary_vtbl
= CLASSTYPE_VTABLES (ctype
);
2136 if (var_finalized_p (primary_vtbl
))
2138 /* Ignore dummy vtables made by get_vtable_decl. */
2139 if (TREE_TYPE (primary_vtbl
) == void_type_node
)
2142 /* On some targets, we cannot determine the key method until the end
2143 of the translation unit -- which is when this function is
2145 if (!targetm
.cxx
.key_method_may_be_inline ())
2146 determine_key_method (ctype
);
2148 /* See if any of the vtables are needed. */
2149 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2151 import_export_decl (vtbl
);
2152 if (DECL_NOT_REALLY_EXTERN (vtbl
) && decl_needed_p (vtbl
))
2157 /* If the references to this class' vtables are optimized away,
2158 still emit the appropriate debugging information. See
2160 if (DECL_COMDAT (primary_vtbl
)
2161 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
2162 note_debug_info_needed (ctype
);
2166 /* The ABI requires that we emit all of the vtables if we emit any
2168 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2170 /* Mark entities references from the virtual table as used. */
2171 mark_vtable_entries (vtbl
);
2173 if (TREE_TYPE (DECL_INITIAL (vtbl
)) == 0)
2175 vec
<tree
, va_gc
> *cleanups
= NULL
;
2176 tree expr
= store_init_value (vtbl
, DECL_INITIAL (vtbl
), &cleanups
,
2179 /* It had better be all done at compile-time. */
2180 gcc_assert (!expr
&& !cleanups
);
2184 DECL_EXTERNAL (vtbl
) = 0;
2185 rest_of_decl_compilation (vtbl
, 1, 1);
2187 /* Because we're only doing syntax-checking, we'll never end up
2188 actually marking the variable as written. */
2189 if (flag_syntax_only
)
2190 TREE_ASM_WRITTEN (vtbl
) = 1;
2191 else if (DECL_ONE_ONLY (vtbl
))
2193 current
= varpool_node::get_create (vtbl
);
2195 current
->add_to_same_comdat_group (last
);
2200 /* Since we're writing out the vtable here, also write the debug
2202 note_debug_info_needed (ctype
);
2207 /* A special return value from type_visibility meaning internal
2210 enum { VISIBILITY_ANON
= VISIBILITY_INTERNAL
+1 };
2212 /* walk_tree helper function for type_visibility. */
2215 min_vis_r (tree
*tp
, int *walk_subtrees
, void *data
)
2217 int *vis_p
= (int *)data
;
2222 else if (OVERLOAD_TYPE_P (*tp
)
2223 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp
)))
2225 *vis_p
= VISIBILITY_ANON
;
2228 else if (CLASS_TYPE_P (*tp
)
2229 && CLASSTYPE_VISIBILITY (*tp
) > *vis_p
)
2230 *vis_p
= CLASSTYPE_VISIBILITY (*tp
);
2234 /* Returns the visibility of TYPE, which is the minimum visibility of its
2238 type_visibility (tree type
)
2240 int vis
= VISIBILITY_DEFAULT
;
2241 cp_walk_tree_without_duplicates (&type
, min_vis_r
, &vis
);
2245 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2246 specified (or if VISIBILITY is static). If TMPL is true, this
2247 constraint is for a template argument, and takes precedence
2248 over explicitly-specified visibility on the template. */
2251 constrain_visibility (tree decl
, int visibility
, bool tmpl
)
2253 if (visibility
== VISIBILITY_ANON
)
2255 /* extern "C" declarations aren't affected by the anonymous
2257 if (!DECL_EXTERN_C_P (decl
))
2259 TREE_PUBLIC (decl
) = 0;
2260 DECL_WEAK (decl
) = 0;
2261 DECL_COMMON (decl
) = 0;
2262 DECL_COMDAT (decl
) = false;
2263 if (VAR_OR_FUNCTION_DECL_P (decl
))
2265 struct symtab_node
*snode
= symtab_node::get (decl
);
2268 snode
->set_comdat_group (NULL
);
2270 DECL_INTERFACE_KNOWN (decl
) = 1;
2271 if (DECL_LANG_SPECIFIC (decl
))
2272 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2275 else if (visibility
> DECL_VISIBILITY (decl
)
2276 && (tmpl
|| !DECL_VISIBILITY_SPECIFIED (decl
)))
2278 DECL_VISIBILITY (decl
) = (enum symbol_visibility
) visibility
;
2279 /* This visibility was not specified. */
2280 DECL_VISIBILITY_SPECIFIED (decl
) = false;
2284 /* Constrain the visibility of DECL based on the visibility of its template
2288 constrain_visibility_for_template (tree decl
, tree targs
)
2290 /* If this is a template instantiation, check the innermost
2291 template args for visibility constraints. The outer template
2292 args are covered by the class check. */
2293 tree args
= INNERMOST_TEMPLATE_ARGS (targs
);
2295 for (i
= TREE_VEC_LENGTH (args
); i
> 0; --i
)
2299 tree arg
= TREE_VEC_ELT (args
, i
-1);
2301 vis
= type_visibility (arg
);
2304 if (REFERENCE_REF_P (arg
))
2305 arg
= TREE_OPERAND (arg
, 0);
2306 if (TREE_TYPE (arg
))
2308 if (TREE_CODE (arg
) == ADDR_EXPR
)
2309 arg
= TREE_OPERAND (arg
, 0);
2310 if (VAR_OR_FUNCTION_DECL_P (arg
))
2312 if (! TREE_PUBLIC (arg
))
2313 vis
= VISIBILITY_ANON
;
2315 vis
= DECL_VISIBILITY (arg
);
2319 constrain_visibility (decl
, vis
, true);
2323 /* Like c_determine_visibility, but with additional C++-specific
2326 Function-scope entities can rely on the function's visibility because
2327 it is set in start_preparsed_function.
2329 Class-scope entities cannot rely on the class's visibility until the end
2330 of the enclosing class definition.
2332 Note that because namespaces have multiple independent definitions,
2333 namespace visibility is handled elsewhere using the #pragma visibility
2334 machinery rather than by decorating the namespace declaration.
2336 The goal is for constraints from the type to give a diagnostic, and
2337 other constraints to be applied silently. */
2340 determine_visibility (tree decl
)
2342 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2344 /* Only relevant for names with external linkage. */
2345 if (!TREE_PUBLIC (decl
))
2348 /* Cloned constructors and destructors get the same visibility as
2349 the underlying function. That should be set up in
2350 maybe_clone_body. */
2351 gcc_assert (!DECL_CLONED_FUNCTION_P (decl
));
2353 bool orig_visibility_specified
= DECL_VISIBILITY_SPECIFIED (decl
);
2354 enum symbol_visibility orig_visibility
= DECL_VISIBILITY (decl
);
2356 /* The decl may be a template instantiation, which could influence
2358 tree template_decl
= NULL_TREE
;
2359 if (TREE_CODE (decl
) == TYPE_DECL
)
2361 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
2363 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)))
2364 template_decl
= decl
;
2366 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
2367 template_decl
= decl
;
2369 else if (DECL_LANG_SPECIFIC (decl
) && DECL_USE_TEMPLATE (decl
))
2370 template_decl
= decl
;
2372 /* If DECL is a member of a class, visibility specifiers on the
2373 class can influence the visibility of the DECL. */
2374 tree class_type
= NULL_TREE
;
2375 if (DECL_CLASS_SCOPE_P (decl
))
2376 class_type
= DECL_CONTEXT (decl
);
2379 /* Not a class member. */
2381 /* Virtual tables have DECL_CONTEXT set to their associated class,
2382 so they are automatically handled above. */
2383 gcc_assert (!VAR_P (decl
)
2384 || !DECL_VTABLE_OR_VTT_P (decl
));
2386 if (DECL_FUNCTION_SCOPE_P (decl
) && ! DECL_VISIBILITY_SPECIFIED (decl
))
2388 /* Local statics and classes get the visibility of their
2389 containing function by default, except that
2390 -fvisibility-inlines-hidden doesn't affect them. */
2391 tree fn
= DECL_CONTEXT (decl
);
2392 if (DECL_VISIBILITY_SPECIFIED (fn
))
2394 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2395 DECL_VISIBILITY_SPECIFIED (decl
) =
2396 DECL_VISIBILITY_SPECIFIED (fn
);
2400 if (DECL_CLASS_SCOPE_P (fn
))
2401 determine_visibility_from_class (decl
, DECL_CONTEXT (fn
));
2402 else if (determine_hidden_inline (fn
))
2404 DECL_VISIBILITY (decl
) = default_visibility
;
2405 DECL_VISIBILITY_SPECIFIED (decl
) =
2406 visibility_options
.inpragma
;
2410 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2411 DECL_VISIBILITY_SPECIFIED (decl
) =
2412 DECL_VISIBILITY_SPECIFIED (fn
);
2416 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2417 but have no TEMPLATE_INFO, so don't try to check it. */
2418 template_decl
= NULL_TREE
;
2420 else if (VAR_P (decl
) && DECL_TINFO_P (decl
)
2421 && flag_visibility_ms_compat
)
2423 /* Under -fvisibility-ms-compat, types are visible by default,
2424 even though their contents aren't. */
2425 tree underlying_type
= TREE_TYPE (DECL_NAME (decl
));
2426 int underlying_vis
= type_visibility (underlying_type
);
2427 if (underlying_vis
== VISIBILITY_ANON
2428 || (CLASS_TYPE_P (underlying_type
)
2429 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type
)))
2430 constrain_visibility (decl
, underlying_vis
, false);
2432 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
2434 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2436 /* tinfo visibility is based on the type it's for. */
2437 constrain_visibility
2438 (decl
, type_visibility (TREE_TYPE (DECL_NAME (decl
))), false);
2440 /* Give the target a chance to override the visibility associated
2442 if (TREE_PUBLIC (decl
)
2443 && !DECL_REALLY_EXTERN (decl
)
2444 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl
)))
2445 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl
))))
2446 targetm
.cxx
.determine_class_data_visibility (decl
);
2448 else if (template_decl
)
2449 /* Template instantiations and specializations get visibility based
2450 on their template unless they override it with an attribute. */;
2451 else if (! DECL_VISIBILITY_SPECIFIED (decl
))
2453 if (determine_hidden_inline (decl
))
2454 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2457 /* Set default visibility to whatever the user supplied with
2458 #pragma GCC visibility or a namespace visibility attribute. */
2459 DECL_VISIBILITY (decl
) = default_visibility
;
2460 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
2467 /* If the specialization doesn't specify visibility, use the
2468 visibility from the template. */
2469 tree tinfo
= get_template_info (template_decl
);
2470 tree args
= TI_ARGS (tinfo
);
2471 tree attribs
= (TREE_CODE (decl
) == TYPE_DECL
2472 ? TYPE_ATTRIBUTES (TREE_TYPE (decl
))
2473 : DECL_ATTRIBUTES (decl
));
2475 if (args
!= error_mark_node
)
2477 tree pattern
= DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo
));
2479 if (!DECL_VISIBILITY_SPECIFIED (decl
))
2481 if (!DECL_VISIBILITY_SPECIFIED (pattern
)
2482 && determine_hidden_inline (decl
))
2483 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2486 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (pattern
);
2487 DECL_VISIBILITY_SPECIFIED (decl
)
2488 = DECL_VISIBILITY_SPECIFIED (pattern
);
2493 /* Template argument visibility outweighs #pragma or namespace
2494 visibility, but not an explicit attribute. */
2495 && !lookup_attribute ("visibility", attribs
))
2497 int depth
= TMPL_ARGS_DEPTH (args
);
2498 if (DECL_VISIBILITY_SPECIFIED (decl
))
2500 /* A class template member with explicit visibility
2501 overrides the class visibility, so we need to apply
2502 all the levels of template args directly. */
2504 for (i
= 1; i
<= depth
; ++i
)
2506 tree lev
= TMPL_ARGS_LEVEL (args
, i
);
2507 constrain_visibility_for_template (decl
, lev
);
2510 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
2511 /* Limit visibility based on its template arguments. */
2512 constrain_visibility_for_template (decl
, args
);
2518 determine_visibility_from_class (decl
, class_type
);
2520 if (decl_anon_ns_mem_p (decl
))
2521 /* Names in an anonymous namespace get internal linkage.
2522 This might change once we implement export. */
2523 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2524 else if (TREE_CODE (decl
) != TYPE_DECL
)
2526 /* Propagate anonymity from type to decl. */
2527 int tvis
= type_visibility (TREE_TYPE (decl
));
2528 if (tvis
== VISIBILITY_ANON
2529 || ! DECL_VISIBILITY_SPECIFIED (decl
))
2530 constrain_visibility (decl
, tvis
, false);
2532 else if (no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/true))
2533 /* DR 757: A type without linkage shall not be used as the type of a
2534 variable or function with linkage, unless
2535 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2536 o the variable or function is not used (3.2 [basic.def.odr]) or is
2537 defined in the same translation unit.
2539 Since non-extern "C" decls need to be defined in the same
2540 translation unit, we can make the type internal. */
2541 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2543 /* If visibility changed and DECL already has DECL_RTL, ensure
2544 symbol flags are updated. */
2545 if ((DECL_VISIBILITY (decl
) != orig_visibility
2546 || DECL_VISIBILITY_SPECIFIED (decl
) != orig_visibility_specified
)
2547 && ((VAR_P (decl
) && TREE_STATIC (decl
))
2548 || TREE_CODE (decl
) == FUNCTION_DECL
)
2549 && DECL_RTL_SET_P (decl
))
2550 make_decl_rtl (decl
);
2553 /* By default, static data members and function members receive
2554 the visibility of their containing class. */
2557 determine_visibility_from_class (tree decl
, tree class_type
)
2559 if (DECL_VISIBILITY_SPECIFIED (decl
))
2562 if (determine_hidden_inline (decl
))
2563 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2566 /* Default to the class visibility. */
2567 DECL_VISIBILITY (decl
) = CLASSTYPE_VISIBILITY (class_type
);
2568 DECL_VISIBILITY_SPECIFIED (decl
)
2569 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type
);
2572 /* Give the target a chance to override the visibility associated
2575 && (DECL_TINFO_P (decl
)
2576 || (DECL_VTABLE_OR_VTT_P (decl
)
2577 /* Construction virtual tables are not exported because
2578 they cannot be referred to from other object files;
2579 their name is not standardized by the ABI. */
2580 && !DECL_CONSTRUCTION_VTABLE_P (decl
)))
2581 && TREE_PUBLIC (decl
)
2582 && !DECL_REALLY_EXTERN (decl
)
2583 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type
))
2584 targetm
.cxx
.determine_class_data_visibility (decl
);
2587 /* Returns true iff DECL is an inline that should get hidden visibility
2588 because of -fvisibility-inlines-hidden. */
2591 determine_hidden_inline (tree decl
)
2593 return (visibility_options
.inlines_hidden
2594 /* Don't do this for inline templates; specializations might not be
2595 inline, and we don't want them to inherit the hidden
2596 visibility. We'll set it here for all inline instantiations. */
2597 && !processing_template_decl
2598 && TREE_CODE (decl
) == FUNCTION_DECL
2599 && DECL_DECLARED_INLINE_P (decl
)
2600 && (! DECL_LANG_SPECIFIC (decl
)
2601 || ! DECL_EXPLICIT_INSTANTIATION (decl
)));
2604 /* Constrain the visibility of a class TYPE based on the visibility of its
2605 field types. Warn if any fields require lesser visibility. */
2608 constrain_class_visibility (tree type
)
2614 int vis
= type_visibility (type
);
2616 if (vis
== VISIBILITY_ANON
2617 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type
)))
2620 /* Don't warn about visibility if the class has explicit visibility. */
2621 if (CLASSTYPE_VISIBILITY_SPECIFIED (type
))
2622 vis
= VISIBILITY_INTERNAL
;
2624 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
2625 if (TREE_CODE (t
) == FIELD_DECL
&& TREE_TYPE (t
) != error_mark_node
2626 && !DECL_ARTIFICIAL (t
))
2628 tree ftype
= strip_pointer_or_array_types (TREE_TYPE (t
));
2629 int subvis
= type_visibility (ftype
);
2631 if (subvis
== VISIBILITY_ANON
)
2633 if (!in_main_input_context())
2635 tree nlt
= no_linkage_check (ftype
, /*relaxed_p=*/false);
2638 if (same_type_p (TREE_TYPE (t
), nlt
))
2639 warning (OPT_Wsubobject_linkage
, "\
2640 %qT has a field %qD whose type has no linkage",
2643 warning (OPT_Wsubobject_linkage
, "\
2644 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2648 warning (OPT_Wsubobject_linkage
, "\
2649 %qT has a field %qD whose type uses the anonymous namespace",
2653 else if (MAYBE_CLASS_TYPE_P (ftype
)
2654 && vis
< VISIBILITY_HIDDEN
2655 && subvis
>= VISIBILITY_HIDDEN
)
2656 warning (OPT_Wattributes
, "\
2657 %qT declared with greater visibility than the type of its field %qD",
2661 binfo
= TYPE_BINFO (type
);
2662 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
2664 int subvis
= type_visibility (TREE_TYPE (t
));
2666 if (subvis
== VISIBILITY_ANON
)
2668 if (!in_main_input_context())
2670 tree nlt
= no_linkage_check (TREE_TYPE (t
), /*relaxed_p=*/false);
2673 if (same_type_p (TREE_TYPE (t
), nlt
))
2674 warning (OPT_Wsubobject_linkage
, "\
2675 %qT has a base %qT whose type has no linkage",
2676 type
, TREE_TYPE (t
));
2678 warning (OPT_Wsubobject_linkage
, "\
2679 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2680 type
, TREE_TYPE (t
), nlt
);
2683 warning (OPT_Wsubobject_linkage
, "\
2684 %qT has a base %qT whose type uses the anonymous namespace",
2685 type
, TREE_TYPE (t
));
2688 else if (vis
< VISIBILITY_HIDDEN
2689 && subvis
>= VISIBILITY_HIDDEN
)
2690 warning (OPT_Wattributes
, "\
2691 %qT declared with greater visibility than its base %qT",
2692 type
, TREE_TYPE (t
));
2696 /* Functions for adjusting the visibility of a tagged type and its nested
2697 types and declarations when it gets a name for linkage purposes from a
2700 static void bt_reset_linkage_1 (binding_entry
, void *);
2701 static void bt_reset_linkage_2 (binding_entry
, void *);
2703 /* First reset the visibility of all the types. */
2706 reset_type_linkage_1 (tree type
)
2708 set_linkage_according_to_type (type
, TYPE_MAIN_DECL (type
));
2709 if (CLASS_TYPE_P (type
))
2710 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2711 bt_reset_linkage_1
, NULL
);
2714 bt_reset_linkage_1 (binding_entry b
, void */
*data*/
)
2716 reset_type_linkage_1 (b
->type
);
2719 /* Then reset the visibility of any static data members or member
2720 functions that use those types. */
2723 reset_decl_linkage (tree decl
)
2725 if (TREE_PUBLIC (decl
))
2727 if (DECL_CLONED_FUNCTION_P (decl
))
2729 TREE_PUBLIC (decl
) = true;
2730 DECL_INTERFACE_KNOWN (decl
) = false;
2731 determine_visibility (decl
);
2732 tentative_decl_linkage (decl
);
2736 reset_type_linkage_2 (tree type
)
2738 if (CLASS_TYPE_P (type
))
2740 if (tree vt
= CLASSTYPE_VTABLES (type
))
2742 tree name
= mangle_vtbl_for_type (type
);
2743 DECL_NAME (vt
) = name
;
2744 SET_DECL_ASSEMBLER_NAME (vt
, name
);
2745 reset_decl_linkage (vt
);
2747 if (tree ti
= CLASSTYPE_TYPEINFO_VAR (type
))
2749 tree name
= mangle_typeinfo_for_type (type
);
2750 DECL_NAME (ti
) = name
;
2751 SET_DECL_ASSEMBLER_NAME (ti
, name
);
2752 TREE_TYPE (name
) = type
;
2753 reset_decl_linkage (ti
);
2755 for (tree m
= TYPE_FIELDS (type
); m
; m
= DECL_CHAIN (m
))
2757 tree mem
= STRIP_TEMPLATE (m
);
2758 if (TREE_CODE (mem
) == VAR_DECL
|| TREE_CODE (mem
) == FUNCTION_DECL
)
2759 reset_decl_linkage (mem
);
2761 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2762 bt_reset_linkage_2
, NULL
);
2767 bt_reset_linkage_2 (binding_entry b
, void */
*data*/
)
2769 reset_type_linkage_2 (b
->type
);
2772 reset_type_linkage (tree type
)
2774 reset_type_linkage_1 (type
);
2775 reset_type_linkage_2 (type
);
2778 /* Set up our initial idea of what the linkage of DECL should be. */
2781 tentative_decl_linkage (tree decl
)
2783 if (DECL_INTERFACE_KNOWN (decl
))
2784 /* We've already made a decision as to how this function will
2786 else if (vague_linkage_p (decl
))
2788 if (TREE_CODE (decl
) == FUNCTION_DECL
2789 && decl_defined_p (decl
))
2791 DECL_EXTERNAL (decl
) = 1;
2792 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2793 note_vague_linkage_fn (decl
);
2794 /* A non-template inline function with external linkage will
2795 always be COMDAT. As we must eventually determine the
2796 linkage of all functions, and as that causes writes to
2797 the data mapped in from the PCH file, it's advantageous
2798 to mark the functions at this point. */
2799 if (DECL_DECLARED_INLINE_P (decl
)
2800 && (!DECL_IMPLICIT_INSTANTIATION (decl
)
2801 || DECL_DEFAULTED_FN (decl
)))
2803 /* This function must have external linkage, as
2804 otherwise DECL_INTERFACE_KNOWN would have been
2806 gcc_assert (TREE_PUBLIC (decl
));
2807 comdat_linkage (decl
);
2808 DECL_INTERFACE_KNOWN (decl
) = 1;
2811 else if (VAR_P (decl
))
2812 maybe_commonize_var (decl
);
2816 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2817 for DECL has not already been determined, do so now by setting
2818 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2819 function is called entities with vague linkage whose definitions
2820 are available must have TREE_PUBLIC set.
2822 If this function decides to place DECL in COMDAT, it will set
2823 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2824 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2825 callers defer that decision until it is clear that DECL is actually
2829 import_export_decl (tree decl
)
2834 tree class_type
= NULL_TREE
;
2836 if (DECL_INTERFACE_KNOWN (decl
))
2839 /* We cannot determine what linkage to give to an entity with vague
2840 linkage until the end of the file. For example, a virtual table
2841 for a class will be defined if and only if the key method is
2842 defined in this translation unit. As a further example, consider
2843 that when compiling a translation unit that uses PCH file with
2844 "-frepo" it would be incorrect to make decisions about what
2845 entities to emit when building the PCH; those decisions must be
2846 delayed until the repository information has been processed. */
2847 gcc_assert (at_eof
);
2848 /* Object file linkage for explicit instantiations is handled in
2849 mark_decl_instantiated. For static variables in functions with
2850 vague linkage, maybe_commonize_var is used.
2852 Therefore, the only declarations that should be provided to this
2853 function are those with external linkage that are:
2855 * implicit instantiations of function templates
2859 * implicit instantiations of static data members of class
2866 Furthermore, all entities that reach this point must have a
2867 definition available in this translation unit.
2869 The following assertions check these conditions. */
2870 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2871 /* Any code that creates entities with TREE_PUBLIC cleared should
2872 also set DECL_INTERFACE_KNOWN. */
2873 gcc_assert (TREE_PUBLIC (decl
));
2874 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2875 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2876 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
2877 || DECL_DECLARED_INLINE_P (decl
));
2879 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2880 || DECL_VTABLE_OR_VTT_P (decl
)
2881 || DECL_TINFO_P (decl
));
2882 /* Check that a definition of DECL is available in this translation
2884 gcc_assert (!DECL_REALLY_EXTERN (decl
));
2886 /* Assume that DECL will not have COMDAT linkage. */
2888 /* Assume that DECL will not be imported into this translation
2892 /* See if the repository tells us whether or not to emit DECL in
2893 this translation unit. */
2894 emit_p
= repo_emit_p (decl
);
2897 else if (emit_p
== 1)
2899 /* The repository indicates that this entity should be defined
2900 here. Make sure the back end honors that request. */
2902 /* Output the definition as an ordinary strong definition. */
2903 DECL_EXTERNAL (decl
) = 0;
2904 DECL_INTERFACE_KNOWN (decl
) = 1;
2909 /* We have already decided what to do with this DECL; there is no
2910 need to check anything further. */
2912 else if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
2914 class_type
= DECL_CONTEXT (decl
);
2915 import_export_class (class_type
);
2916 if (CLASSTYPE_INTERFACE_KNOWN (class_type
)
2917 && CLASSTYPE_INTERFACE_ONLY (class_type
))
2919 else if ((!flag_weak
|| TARGET_WEAK_NOT_IN_ARCHIVE_TOC
)
2920 && !CLASSTYPE_USE_TEMPLATE (class_type
)
2921 && CLASSTYPE_KEY_METHOD (class_type
)
2922 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
)))
2923 /* The ABI requires that all virtual tables be emitted with
2924 COMDAT linkage. However, on systems where COMDAT symbols
2925 don't show up in the table of contents for a static
2926 archive, or on systems without weak symbols (where we
2927 approximate COMDAT linkage by using internal linkage), the
2928 linker will report errors about undefined symbols because
2929 it will not see the virtual table definition. Therefore,
2930 in the case that we know that the virtual table will be
2931 emitted in only one translation unit, we make the virtual
2932 table an ordinary definition with external linkage. */
2933 DECL_EXTERNAL (decl
) = 0;
2934 else if (CLASSTYPE_INTERFACE_KNOWN (class_type
))
2936 /* CLASS_TYPE is being exported from this translation unit,
2937 so DECL should be defined here. */
2938 if (!flag_weak
&& CLASSTYPE_EXPLICIT_INSTANTIATION (class_type
))
2939 /* If a class is declared in a header with the "extern
2940 template" extension, then it will not be instantiated,
2941 even in translation units that would normally require
2942 it. Often such classes are explicitly instantiated in
2943 one translation unit. Therefore, the explicit
2944 instantiation must be made visible to other translation
2946 DECL_EXTERNAL (decl
) = 0;
2949 /* The generic C++ ABI says that class data is always
2950 COMDAT, even if there is a key function. Some
2951 variants (e.g., the ARM EABI) says that class data
2952 only has COMDAT linkage if the class data might be
2953 emitted in more than one translation unit. When the
2954 key method can be inline and is inline, we still have
2955 to arrange for comdat even though
2956 class_data_always_comdat is false. */
2957 if (!CLASSTYPE_KEY_METHOD (class_type
)
2958 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
))
2959 || targetm
.cxx
.class_data_always_comdat ())
2961 /* The ABI requires COMDAT linkage. Normally, we
2962 only emit COMDAT things when they are needed;
2963 make sure that we realize that this entity is
2970 else if (!flag_implicit_templates
2971 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type
))
2976 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2978 tree type
= TREE_TYPE (DECL_NAME (decl
));
2979 if (CLASS_TYPE_P (type
))
2982 import_export_class (type
);
2983 if (CLASSTYPE_INTERFACE_KNOWN (type
)
2984 && TYPE_POLYMORPHIC_P (type
)
2985 && CLASSTYPE_INTERFACE_ONLY (type
)
2986 /* If -fno-rtti was specified, then we cannot be sure
2987 that RTTI information will be emitted with the
2988 virtual table of the class, so we must emit it
2989 wherever it is used. */
2994 if (CLASSTYPE_INTERFACE_KNOWN (type
)
2995 && !CLASSTYPE_INTERFACE_ONLY (type
))
2997 comdat_p
= (targetm
.cxx
.class_data_always_comdat ()
2998 || (CLASSTYPE_KEY_METHOD (type
)
2999 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type
))));
3004 DECL_EXTERNAL (decl
) = 0;
3014 else if (DECL_TEMPLOID_INSTANTIATION (decl
))
3016 /* DECL is an implicit instantiation of a function or static
3018 if ((flag_implicit_templates
3019 && !flag_use_repository
)
3020 || (flag_implicit_inline_templates
3021 && TREE_CODE (decl
) == FUNCTION_DECL
3022 && DECL_DECLARED_INLINE_P (decl
)))
3025 /* If we are not implicitly generating templates, then mark
3026 this entity as undefined in this translation unit. */
3029 else if (DECL_FUNCTION_MEMBER_P (decl
))
3031 if (!DECL_DECLARED_INLINE_P (decl
))
3033 tree ctype
= DECL_CONTEXT (decl
);
3034 import_export_class (ctype
);
3035 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
3037 DECL_NOT_REALLY_EXTERN (decl
)
3038 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
3039 || (DECL_DECLARED_INLINE_P (decl
)
3040 && ! flag_implement_inlines
3041 && !DECL_VINDEX (decl
)));
3043 if (!DECL_NOT_REALLY_EXTERN (decl
))
3044 DECL_EXTERNAL (decl
) = 1;
3046 /* Always make artificials weak. */
3047 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
3050 maybe_make_one_only (decl
);
3061 /* If we are importing DECL into this translation unit, mark is
3062 an undefined here. */
3063 DECL_EXTERNAL (decl
) = 1;
3064 DECL_NOT_REALLY_EXTERN (decl
) = 0;
3068 /* If we decided to put DECL in COMDAT, mark it accordingly at
3070 comdat_linkage (decl
);
3073 DECL_INTERFACE_KNOWN (decl
) = 1;
3076 /* Return an expression that performs the destruction of DECL, which
3077 must be a VAR_DECL whose type has a non-trivial destructor, or is
3078 an array whose (innermost) elements have a non-trivial destructor. */
3081 build_cleanup (tree decl
)
3083 tree clean
= cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
3084 gcc_assert (clean
!= NULL_TREE
);
3088 /* Returns the initialization guard variable for the variable DECL,
3089 which has static storage duration. */
3092 get_guard (tree decl
)
3097 sname
= mangle_guard_variable (decl
);
3098 guard
= get_global_binding (sname
);
3103 /* We use a type that is big enough to contain a mutex as well
3104 as an integer counter. */
3105 guard_type
= targetm
.cxx
.guard_type ();
3106 guard
= build_decl (DECL_SOURCE_LOCATION (decl
),
3107 VAR_DECL
, sname
, guard_type
);
3109 /* The guard should have the same linkage as what it guards. */
3110 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
3111 TREE_STATIC (guard
) = TREE_STATIC (decl
);
3112 DECL_COMMON (guard
) = DECL_COMMON (decl
);
3113 DECL_COMDAT (guard
) = DECL_COMDAT (decl
);
3114 CP_DECL_THREAD_LOCAL_P (guard
) = CP_DECL_THREAD_LOCAL_P (decl
);
3115 set_decl_tls_model (guard
, DECL_TLS_MODEL (decl
));
3116 if (DECL_ONE_ONLY (decl
))
3117 make_decl_one_only (guard
, cxx_comdat_group (guard
));
3118 if (TREE_PUBLIC (decl
))
3119 DECL_WEAK (guard
) = DECL_WEAK (decl
);
3120 DECL_VISIBILITY (guard
) = DECL_VISIBILITY (decl
);
3121 DECL_VISIBILITY_SPECIFIED (guard
) = DECL_VISIBILITY_SPECIFIED (decl
);
3123 DECL_ARTIFICIAL (guard
) = 1;
3124 DECL_IGNORED_P (guard
) = 1;
3125 TREE_USED (guard
) = 1;
3126 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
3131 /* Return an atomic load of src with the appropriate memory model. */
3134 build_atomic_load_byte (tree src
, HOST_WIDE_INT model
)
3136 tree ptr_type
= build_pointer_type (char_type_node
);
3137 tree mem_model
= build_int_cst (integer_type_node
, model
);
3142 size
= tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node
));
3144 fncode
= BUILT_IN_ATOMIC_LOAD_N
+ exact_log2 (size
) + 1;
3145 t
= builtin_decl_implicit ((enum built_in_function
) fncode
);
3147 addr
= build1 (ADDR_EXPR
, ptr_type
, src
);
3148 val
= build_call_expr (t
, 2, addr
, mem_model
);
3152 /* Return those bits of the GUARD variable that should be set when the
3153 guarded entity is actually initialized. */
3156 get_guard_bits (tree guard
)
3158 if (!targetm
.cxx
.guard_mask_bit ())
3160 /* We only set the first byte of the guard, in order to leave room
3161 for a mutex in the high-order bits. */
3162 guard
= build1 (ADDR_EXPR
,
3163 build_pointer_type (TREE_TYPE (guard
)),
3165 guard
= build1 (NOP_EXPR
,
3166 build_pointer_type (char_type_node
),
3168 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
3174 /* Return an expression which determines whether or not the GUARD
3175 variable has already been initialized. */
3178 get_guard_cond (tree guard
, bool thread_safe
)
3183 guard
= get_guard_bits (guard
);
3185 guard
= build_atomic_load_byte (guard
, MEMMODEL_ACQUIRE
);
3187 /* Mask off all but the low bit. */
3188 if (targetm
.cxx
.guard_mask_bit ())
3190 guard_value
= integer_one_node
;
3191 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3192 guard_value
= fold_convert (TREE_TYPE (guard
), guard_value
);
3193 guard
= cp_build_binary_op (input_location
,
3194 BIT_AND_EXPR
, guard
, guard_value
,
3195 tf_warning_or_error
);
3198 guard_value
= integer_zero_node
;
3199 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3200 guard_value
= fold_convert (TREE_TYPE (guard
), guard_value
);
3201 return cp_build_binary_op (input_location
,
3202 EQ_EXPR
, guard
, guard_value
,
3203 tf_warning_or_error
);
3206 /* Return an expression which sets the GUARD variable, indicating that
3207 the variable being guarded has been initialized. */
3210 set_guard (tree guard
)
3214 /* Set the GUARD to one. */
3215 guard
= get_guard_bits (guard
);
3216 guard_init
= integer_one_node
;
3217 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
3218 guard_init
= fold_convert (TREE_TYPE (guard
), guard_init
);
3219 return cp_build_modify_expr (input_location
, guard
, NOP_EXPR
, guard_init
,
3220 tf_warning_or_error
);
3223 /* Returns true iff we can tell that VAR does not have a dynamic
3227 var_defined_without_dynamic_init (tree var
)
3229 /* If it's defined in another TU, we can't tell. */
3230 if (DECL_EXTERNAL (var
))
3232 /* If it has a non-trivial destructor, registering the destructor
3233 counts as dynamic initialization. */
3234 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var
)))
3236 /* If it's in this TU, its initializer has been processed, unless
3237 it's a case of self-initialization, then DECL_INITIALIZED_P is
3238 false while the initializer is handled by finish_id_expression. */
3239 if (!DECL_INITIALIZED_P (var
))
3241 /* If it has no initializer or a constant one, it's not dynamic. */
3242 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var
)
3243 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var
));
3246 /* Returns true iff VAR is a variable that needs uses to be
3247 wrapped for possible dynamic initialization. */
3250 var_needs_tls_wrapper (tree var
)
3252 return (!error_operand_p (var
)
3253 && CP_DECL_THREAD_LOCAL_P (var
)
3254 && !DECL_GNU_TLS_P (var
)
3255 && !DECL_FUNCTION_SCOPE_P (var
)
3256 && !var_defined_without_dynamic_init (var
));
3259 /* Get the FUNCTION_DECL for the shared TLS init function for this
3260 translation unit. */
3263 get_local_tls_init_fn (void)
3265 tree sname
= get_identifier ("__tls_init");
3266 tree fn
= get_global_binding (sname
);
3269 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
3270 build_function_type (void_type_node
,
3272 SET_DECL_LANGUAGE (fn
, lang_c
);
3273 TREE_PUBLIC (fn
) = false;
3274 DECL_ARTIFICIAL (fn
) = true;
3276 set_global_binding (fn
);
3281 /* Get a FUNCTION_DECL for the init function for the thread_local
3282 variable VAR. The init function will be an alias to the function
3283 that initializes all the non-local TLS variables in the translation
3284 unit. The init function is only used by the wrapper function. */
3287 get_tls_init_fn (tree var
)
3289 /* Only C++11 TLS vars need this init fn. */
3290 if (!var_needs_tls_wrapper (var
))
3293 /* If -fno-extern-tls-init, assume that we don't need to call
3294 a tls init function for a variable defined in another TU. */
3295 if (!flag_extern_tls_init
&& DECL_EXTERNAL (var
))
3298 /* If the variable is internal, or if we can't generate aliases,
3299 call the local init function directly. */
3300 if (!TREE_PUBLIC (var
) || !TARGET_SUPPORTS_ALIASES
)
3301 return get_local_tls_init_fn ();
3303 tree sname
= mangle_tls_init_fn (var
);
3304 tree fn
= get_global_binding (sname
);
3307 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
3308 build_function_type (void_type_node
,
3310 SET_DECL_LANGUAGE (fn
, lang_c
);
3311 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3312 DECL_ARTIFICIAL (fn
) = true;
3313 DECL_COMDAT (fn
) = DECL_COMDAT (var
);
3314 DECL_EXTERNAL (fn
) = DECL_EXTERNAL (var
);
3315 if (DECL_ONE_ONLY (var
))
3316 make_decl_one_only (fn
, cxx_comdat_group (fn
));
3317 if (TREE_PUBLIC (var
))
3319 tree obtype
= strip_array_types (non_reference (TREE_TYPE (var
)));
3320 /* If the variable is defined somewhere else and might have static
3321 initialization, make the init function a weak reference. */
3322 if ((!TYPE_NEEDS_CONSTRUCTING (obtype
)
3323 || TYPE_HAS_CONSTEXPR_CTOR (obtype
)
3324 || TYPE_HAS_TRIVIAL_DFLT (obtype
))
3325 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype
)
3326 && DECL_EXTERNAL (var
))
3329 DECL_WEAK (fn
) = DECL_WEAK (var
);
3331 DECL_VISIBILITY (fn
) = DECL_VISIBILITY (var
);
3332 DECL_VISIBILITY_SPECIFIED (fn
) = DECL_VISIBILITY_SPECIFIED (var
);
3333 DECL_DLLIMPORT_P (fn
) = DECL_DLLIMPORT_P (var
);
3334 DECL_IGNORED_P (fn
) = 1;
3337 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3339 set_global_binding (fn
);
3344 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3345 variable VAR. The wrapper function calls the init function (if any) for
3346 VAR and then returns a reference to VAR. The wrapper function is used
3347 in place of VAR everywhere VAR is mentioned. */
3350 get_tls_wrapper_fn (tree var
)
3352 /* Only C++11 TLS vars need this wrapper fn. */
3353 if (!var_needs_tls_wrapper (var
))
3356 tree sname
= mangle_tls_wrapper_fn (var
);
3357 tree fn
= get_global_binding (sname
);
3360 /* A named rvalue reference is an lvalue, so the wrapper should
3361 always return an lvalue reference. */
3362 tree type
= non_reference (TREE_TYPE (var
));
3363 type
= build_reference_type (type
);
3364 tree fntype
= build_function_type (type
, void_list_node
);
3365 fn
= build_lang_decl (FUNCTION_DECL
, sname
, fntype
);
3366 SET_DECL_LANGUAGE (fn
, lang_c
);
3367 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3368 DECL_ARTIFICIAL (fn
) = true;
3369 DECL_IGNORED_P (fn
) = 1;
3370 /* The wrapper is inline and emitted everywhere var is used. */
3371 DECL_DECLARED_INLINE_P (fn
) = true;
3372 if (TREE_PUBLIC (var
))
3374 comdat_linkage (fn
);
3375 #ifdef HAVE_GAS_HIDDEN
3376 /* Make the wrapper bind locally; there's no reason to share
3377 the wrapper between multiple shared objects. */
3378 DECL_VISIBILITY (fn
) = VISIBILITY_INTERNAL
;
3379 DECL_VISIBILITY_SPECIFIED (fn
) = true;
3382 if (!TREE_PUBLIC (fn
))
3383 DECL_INTERFACE_KNOWN (fn
) = true;
3385 note_vague_linkage_fn (fn
);
3388 /* We want CSE to commonize calls to the wrapper, but marking it as
3389 pure is unsafe since it has side-effects. I guess we need a new
3390 ECF flag even weaker than ECF_PURE. FIXME! */
3391 DECL_PURE_P (fn
) = true;
3394 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3396 set_global_binding (fn
);
3401 /* At EOF, generate the definition for the TLS wrapper function FN:
3404 if (init_fn) init_fn();
3409 generate_tls_wrapper (tree fn
)
3411 tree var
= DECL_BEFRIENDING_CLASSES (fn
);
3413 start_preparsed_function (fn
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
3414 tree body
= begin_function_body ();
3415 /* Only call the init fn if there might be one. */
3416 if (tree init_fn
= get_tls_init_fn (var
))
3418 tree if_stmt
= NULL_TREE
;
3419 /* If init_fn is a weakref, make sure it exists before calling. */
3420 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn
)))
3422 if_stmt
= begin_if_stmt ();
3423 tree addr
= cp_build_addr_expr (init_fn
, tf_warning_or_error
);
3424 tree cond
= cp_build_binary_op (DECL_SOURCE_LOCATION (var
),
3425 NE_EXPR
, addr
, nullptr_node
,
3426 tf_warning_or_error
);
3427 finish_if_stmt_cond (cond
, if_stmt
);
3429 finish_expr_stmt (build_cxx_call
3430 (init_fn
, 0, NULL
, tf_warning_or_error
));
3433 finish_then_clause (if_stmt
);
3434 finish_if_stmt (if_stmt
);
3438 /* If there's no initialization, the wrapper is a constant function. */
3439 TREE_READONLY (fn
) = true;
3440 finish_return_stmt (convert_from_reference (var
));
3441 finish_function_body (body
);
3442 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3445 /* Start the process of running a particular set of global constructors
3446 or destructors. Subroutine of do_[cd]tors. Also called from
3447 vtv_start_verification_constructor_init_function. */
3450 start_objects (int method_type
, int initp
)
3456 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3458 if (initp
!= DEFAULT_INIT_PRIORITY
)
3468 sprintf (type
, "sub_%c%c%.5u", method_type
, joiner
, initp
);
3471 sprintf (type
, "sub_%c", method_type
);
3473 fndecl
= build_lang_decl (FUNCTION_DECL
,
3474 get_file_function_name (type
),
3475 build_function_type_list (void_type_node
,
3477 start_preparsed_function (fndecl
, /*attrs=*/NULL_TREE
, SF_PRE_PARSED
);
3479 TREE_PUBLIC (current_function_decl
) = 0;
3481 /* Mark as artificial because it's not explicitly in the user's
3483 DECL_ARTIFICIAL (current_function_decl
) = 1;
3485 /* Mark this declaration as used to avoid spurious warnings. */
3486 TREE_USED (current_function_decl
) = 1;
3488 /* Mark this function as a global constructor or destructor. */
3489 if (method_type
== 'I')
3490 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
3492 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
3494 body
= begin_compound_stmt (BCS_FN_BODY
);
3499 /* Finish the process of running a particular set of global constructors
3500 or destructors. Subroutine of do_[cd]tors. */
3503 finish_objects (int method_type
, int initp
, tree body
)
3508 finish_compound_stmt (body
);
3509 fn
= finish_function (/*inline_p=*/false);
3511 if (method_type
== 'I')
3513 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
3514 decl_init_priority_insert (fn
, initp
);
3518 DECL_STATIC_DESTRUCTOR (fn
) = 1;
3519 decl_fini_priority_insert (fn
, initp
);
3522 expand_or_defer_fn (fn
);
3525 /* The names of the parameters to the function created to handle
3526 initializations and destructions for objects with static storage
3528 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3529 #define PRIORITY_IDENTIFIER "__priority"
3531 /* The name of the function we create to handle initializations and
3532 destructions for objects with static storage duration. */
3533 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3535 /* The declaration for the __INITIALIZE_P argument. */
3536 static GTY(()) tree initialize_p_decl
;
3538 /* The declaration for the __PRIORITY argument. */
3539 static GTY(()) tree priority_decl
;
3541 /* The declaration for the static storage duration function. */
3542 static GTY(()) tree ssdf_decl
;
3544 /* All the static storage duration functions created in this
3545 translation unit. */
3546 static GTY(()) vec
<tree
, va_gc
> *ssdf_decls
;
3548 /* A map from priority levels to information about that priority
3549 level. There may be many such levels, so efficient lookup is
3551 static splay_tree priority_info_map
;
3553 /* Begins the generation of the function that will handle all
3554 initialization and destruction of objects with static storage
3555 duration. The function generated takes two parameters of type
3556 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3557 nonzero, it performs initializations. Otherwise, it performs
3558 destructions. It only performs those initializations or
3559 destructions with the indicated __PRIORITY. The generated function
3562 It is assumed that this function will only be called once per
3563 translation unit. */
3566 start_static_storage_duration_function (unsigned count
)
3570 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
3572 /* Create the identifier for this function. It will be of the form
3573 SSDF_IDENTIFIER_<number>. */
3574 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, count
);
3576 type
= build_function_type_list (void_type_node
,
3577 integer_type_node
, integer_type_node
,
3580 /* Create the FUNCTION_DECL itself. */
3581 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
3582 get_identifier (id
),
3584 TREE_PUBLIC (ssdf_decl
) = 0;
3585 DECL_ARTIFICIAL (ssdf_decl
) = 1;
3587 /* Put this function in the list of functions to be called from the
3588 static constructors and destructors. */
3591 vec_alloc (ssdf_decls
, 32);
3593 /* Take this opportunity to initialize the map from priority
3594 numbers to information about that priority level. */
3595 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
3596 /*delete_key_fn=*/0,
3597 /*delete_value_fn=*/
3598 (splay_tree_delete_value_fn
)
3599 (void (*) (void)) free
);
3601 /* We always need to generate functions for the
3602 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3603 priorities later, we'll be sure to find the
3604 DEFAULT_INIT_PRIORITY. */
3605 get_priority_info (DEFAULT_INIT_PRIORITY
);
3608 vec_safe_push (ssdf_decls
, ssdf_decl
);
3610 /* Create the argument list. */
3611 initialize_p_decl
= cp_build_parm_decl
3612 (ssdf_decl
, get_identifier (INITIALIZE_P_IDENTIFIER
), integer_type_node
);
3613 TREE_USED (initialize_p_decl
) = 1;
3614 priority_decl
= cp_build_parm_decl
3615 (ssdf_decl
, get_identifier (PRIORITY_IDENTIFIER
), integer_type_node
);
3616 TREE_USED (priority_decl
) = 1;
3618 DECL_CHAIN (initialize_p_decl
) = priority_decl
;
3619 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
3621 /* Put the function in the global scope. */
3622 pushdecl (ssdf_decl
);
3624 /* Start the function itself. This is equivalent to declaring the
3627 static void __ssdf (int __initialize_p, init __priority_p);
3629 It is static because we only need to call this function from the
3630 various constructor and destructor functions for this module. */
3631 start_preparsed_function (ssdf_decl
,
3632 /*attrs=*/NULL_TREE
,
3635 /* Set up the scope of the outermost block in the function. */
3636 body
= begin_compound_stmt (BCS_FN_BODY
);
3641 /* Finish the generation of the function which performs initialization
3642 and destruction of objects with static storage duration. After
3643 this point, no more such objects can be created. */
3646 finish_static_storage_duration_function (tree body
)
3648 /* Close out the function. */
3649 finish_compound_stmt (body
);
3650 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3653 /* Return the information about the indicated PRIORITY level. If no
3654 code to handle this level has yet been generated, generate the
3655 appropriate prologue. */
3657 static priority_info
3658 get_priority_info (int priority
)
3663 n
= splay_tree_lookup (priority_info_map
,
3664 (splay_tree_key
) priority
);
3667 /* Create a new priority information structure, and insert it
3669 pi
= XNEW (struct priority_info_s
);
3670 pi
->initializations_p
= 0;
3671 pi
->destructions_p
= 0;
3672 splay_tree_insert (priority_info_map
,
3673 (splay_tree_key
) priority
,
3674 (splay_tree_value
) pi
);
3677 pi
= (priority_info
) n
->value
;
3682 /* The effective initialization priority of a DECL. */
3684 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3685 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3686 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3688 /* Whether a DECL needs a guard to protect it against multiple
3691 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3692 || DECL_ONE_ONLY (decl) \
3693 || DECL_WEAK (decl)))
3695 /* Called from one_static_initialization_or_destruction(),
3697 Walks the initializer list of a global variable and looks for
3698 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3699 and that have their DECL_CONTEXT() == NULL.
3700 For each such temporary variable, set their DECL_CONTEXT() to
3701 the current function. This is necessary because otherwise
3702 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3703 when trying to refer to a temporary variable that does not have
3704 it's DECL_CONTECT() properly set. */
3706 fix_temporary_vars_context_r (tree
*node
,
3710 gcc_assert (current_function_decl
);
3712 if (TREE_CODE (*node
) == BIND_EXPR
)
3716 for (var
= BIND_EXPR_VARS (*node
); var
; var
= DECL_CHAIN (var
))
3719 && DECL_ARTIFICIAL (var
)
3720 && !DECL_CONTEXT (var
))
3721 DECL_CONTEXT (var
) = current_function_decl
;
3727 /* Set up to handle the initialization or destruction of DECL. If
3728 INITP is nonzero, we are initializing the variable. Otherwise, we
3729 are destroying it. */
3732 one_static_initialization_or_destruction (tree decl
, tree init
, bool initp
)
3734 tree guard_if_stmt
= NULL_TREE
;
3737 /* If we are supposed to destruct and there's a trivial destructor,
3738 nothing has to be done. */
3740 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3743 /* Trick the compiler into thinking we are at the file and line
3744 where DECL was declared so that error-messages make sense, and so
3745 that the debugger will show somewhat sensible file and line
3747 input_location
= DECL_SOURCE_LOCATION (decl
);
3749 /* Make sure temporary variables in the initialiser all have
3750 their DECL_CONTEXT() set to a value different from NULL_TREE.
3751 This can happen when global variables initializers are built.
3752 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3753 the temporary variables that might have been generated in the
3754 accompanying initializers is NULL_TREE, meaning the variables have been
3755 declared in the global namespace.
3756 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3757 of the temporaries are set to the current function decl. */
3758 cp_walk_tree_without_duplicates (&init
,
3759 fix_temporary_vars_context_r
,
3766 Access control for implicit calls to the constructors,
3767 the conversion functions, or the destructor called to
3768 create and destroy a static data member is performed as
3769 if these calls appeared in the scope of the member's
3772 we pretend we are in a static member function of the class of
3773 which the DECL is a member. */
3774 if (member_p (decl
))
3776 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
3777 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
3780 /* Assume we don't need a guard. */
3782 /* We need a guard if this is an object with external linkage that
3783 might be initialized in more than one place. (For example, a
3784 static data member of a template, when the data member requires
3786 if (NEEDS_GUARD_P (decl
))
3790 guard
= get_guard (decl
);
3792 /* When using __cxa_atexit, we just check the GUARD as we would
3793 for a local static. */
3794 if (flag_use_cxa_atexit
)
3796 /* When using __cxa_atexit, we never try to destroy
3797 anything from a static destructor. */
3799 guard_cond
= get_guard_cond (guard
, false);
3801 /* If we don't have __cxa_atexit, then we will be running
3802 destructors from .fini sections, or their equivalents. So,
3803 we need to know how many times we've tried to initialize this
3804 object. We do initializations only if the GUARD is zero,
3805 i.e., if we are the first to initialize the variable. We do
3806 destructions only if the GUARD is one, i.e., if we are the
3807 last to destroy the variable. */
3810 = cp_build_binary_op (input_location
,
3812 cp_build_unary_op (PREINCREMENT_EXPR
,
3815 tf_warning_or_error
),
3817 tf_warning_or_error
);
3820 = cp_build_binary_op (input_location
,
3822 cp_build_unary_op (PREDECREMENT_EXPR
,
3825 tf_warning_or_error
),
3827 tf_warning_or_error
);
3829 guard_if_stmt
= begin_if_stmt ();
3830 finish_if_stmt_cond (guard_cond
, guard_if_stmt
);
3834 /* If we're using __cxa_atexit, we have not already set the GUARD,
3835 so we must do so now. */
3836 if (guard
&& initp
&& flag_use_cxa_atexit
)
3837 finish_expr_stmt (set_guard (guard
));
3839 /* Perform the initialization or destruction. */
3844 finish_expr_stmt (init
);
3845 if (sanitize_flags_p (SANITIZE_ADDRESS
, decl
))
3847 varpool_node
*vnode
= varpool_node::get (decl
);
3849 vnode
->dynamically_initialized
= 1;
3853 /* If we're using __cxa_atexit, register a function that calls the
3854 destructor for the object. */
3855 if (flag_use_cxa_atexit
)
3856 finish_expr_stmt (register_dtor_fn (decl
));
3859 finish_expr_stmt (build_cleanup (decl
));
3861 /* Finish the guard if-stmt, if necessary. */
3864 finish_then_clause (guard_if_stmt
);
3865 finish_if_stmt (guard_if_stmt
);
3868 /* Now that we're done with DECL we don't need to pretend to be a
3869 member of its class any longer. */
3870 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
3871 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
3874 /* Generate code to do the initialization or destruction of the decls in VARS,
3875 a TREE_LIST of VAR_DECL with static storage duration.
3876 Whether initialization or destruction is performed is specified by INITP. */
3879 do_static_initialization_or_destruction (tree vars
, bool initp
)
3881 tree node
, init_if_stmt
, cond
;
3883 /* Build the outer if-stmt to check for initialization or destruction. */
3884 init_if_stmt
= begin_if_stmt ();
3885 cond
= initp
? integer_one_node
: integer_zero_node
;
3886 cond
= cp_build_binary_op (input_location
,
3890 tf_warning_or_error
);
3891 finish_if_stmt_cond (cond
, init_if_stmt
);
3893 /* To make sure dynamic construction doesn't access globals from other
3894 compilation units where they might not be yet constructed, for
3895 -fsanitize=address insert __asan_before_dynamic_init call that
3896 prevents access to either all global variables that need construction
3897 in other compilation units, or at least those that haven't been
3898 initialized yet. Variables that need dynamic construction in
3899 the current compilation unit are kept accessible. */
3900 if (initp
&& (flag_sanitize
& SANITIZE_ADDRESS
))
3901 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3905 tree decl
= TREE_VALUE (node
);
3906 tree priority_if_stmt
;
3910 /* If we don't need a destructor, there's nothing to do. Avoid
3911 creating a possibly empty if-stmt. */
3912 if (!initp
&& TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3914 node
= TREE_CHAIN (node
);
3918 /* Remember that we had an initialization or finalization at this
3920 priority
= DECL_EFFECTIVE_INIT_PRIORITY (decl
);
3921 pi
= get_priority_info (priority
);
3923 pi
->initializations_p
= 1;
3925 pi
->destructions_p
= 1;
3927 /* Conditionalize this initialization on being in the right priority
3928 and being initializing/finalizing appropriately. */
3929 priority_if_stmt
= begin_if_stmt ();
3930 cond
= cp_build_binary_op (input_location
,
3933 build_int_cst (NULL_TREE
, priority
),
3934 tf_warning_or_error
);
3935 finish_if_stmt_cond (cond
, priority_if_stmt
);
3937 /* Process initializers with same priority. */
3939 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node
)) == priority
;
3940 node
= TREE_CHAIN (node
))
3941 /* Do one initialization or destruction. */
3942 one_static_initialization_or_destruction (TREE_VALUE (node
),
3943 TREE_PURPOSE (node
), initp
);
3945 /* Finish up the priority if-stmt body. */
3946 finish_then_clause (priority_if_stmt
);
3947 finish_if_stmt (priority_if_stmt
);
3951 /* Revert what __asan_before_dynamic_init did by calling
3952 __asan_after_dynamic_init. */
3953 if (initp
&& (flag_sanitize
& SANITIZE_ADDRESS
))
3954 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3956 /* Finish up the init/destruct if-stmt body. */
3957 finish_then_clause (init_if_stmt
);
3958 finish_if_stmt (init_if_stmt
);
3961 /* VARS is a list of variables with static storage duration which may
3962 need initialization and/or finalization. Remove those variables
3963 that don't really need to be initialized or finalized, and return
3964 the resulting list. The order in which the variables appear in
3965 VARS is in reverse order of the order in which they should actually
3966 be initialized. The list we return is in the unreversed order;
3967 i.e., the first variable should be initialized first. */
3970 prune_vars_needing_no_initialization (tree
*vars
)
3973 tree result
= NULL_TREE
;
3978 tree decl
= TREE_VALUE (t
);
3979 tree init
= TREE_PURPOSE (t
);
3981 /* Deal gracefully with error. */
3982 if (error_operand_p (decl
))
3984 var
= &TREE_CHAIN (t
);
3988 /* The only things that can be initialized are variables. */
3989 gcc_assert (VAR_P (decl
));
3991 /* If this object is not defined, we don't need to do anything
3993 if (DECL_EXTERNAL (decl
))
3995 var
= &TREE_CHAIN (t
);
3999 /* Also, if the initializer already contains errors, we can bail
4001 if (init
&& TREE_CODE (init
) == TREE_LIST
4002 && value_member (error_mark_node
, init
))
4004 var
= &TREE_CHAIN (t
);
4008 /* This variable is going to need initialization and/or
4009 finalization, so we add it to the list. */
4010 *var
= TREE_CHAIN (t
);
4011 TREE_CHAIN (t
) = result
;
4018 /* Make sure we have told the back end about all the variables in
4022 write_out_vars (tree vars
)
4026 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
4028 tree var
= TREE_VALUE (v
);
4029 if (!var_finalized_p (var
))
4031 import_export_decl (var
);
4032 rest_of_decl_compilation (var
, 1, 1);
4037 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4038 (otherwise) that will initialize all global objects with static
4039 storage duration having the indicated PRIORITY. */
4042 generate_ctor_or_dtor_function (bool constructor_p
, int priority
,
4050 input_location
= *locus
;
4052 /* Was: locus->line++; */
4054 /* We use `I' to indicate initialization and `D' to indicate
4056 function_key
= constructor_p
? 'I' : 'D';
4058 /* We emit the function lazily, to avoid generating empty
4059 global constructors and destructors. */
4062 /* For Objective-C++, we may need to initialize metadata found in this module.
4063 This must be done _before_ any other static initializations. */
4064 if (c_dialect_objc () && (priority
== DEFAULT_INIT_PRIORITY
)
4065 && constructor_p
&& objc_static_init_needed_p ())
4067 body
= start_objects (function_key
, priority
);
4068 objc_generate_static_init_call (NULL_TREE
);
4071 /* Call the static storage duration function with appropriate
4073 FOR_EACH_VEC_SAFE_ELT (ssdf_decls
, i
, fndecl
)
4075 /* Calls to pure or const functions will expand to nothing. */
4076 if (! (flags_from_decl_or_type (fndecl
) & (ECF_CONST
| ECF_PURE
)))
4081 body
= start_objects (function_key
, priority
);
4083 call
= cp_build_function_call_nary (fndecl
, tf_warning_or_error
,
4084 build_int_cst (NULL_TREE
,
4086 build_int_cst (NULL_TREE
,
4089 finish_expr_stmt (call
);
4093 /* Close out the function. */
4095 finish_objects (function_key
, priority
, body
);
4098 /* Generate constructor and destructor functions for the priority
4102 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n
, void * data
)
4104 location_t
*locus
= (location_t
*) data
;
4105 int priority
= (int) n
->key
;
4106 priority_info pi
= (priority_info
) n
->value
;
4108 /* Generate the functions themselves, but only if they are really
4110 if (pi
->initializations_p
)
4111 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority
, locus
);
4112 if (pi
->destructions_p
)
4113 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority
, locus
);
4115 /* Keep iterating. */
4119 /* Return C++ property of T, based on given operation OP. */
4122 cpp_check (tree t
, cpp_operation op
)
4126 case HAS_DEPENDENT_TEMPLATE_ARGS
:
4128 tree ti
= CLASSTYPE_TEMPLATE_INFO (t
);
4131 ++processing_template_decl
;
4132 const bool dep
= any_dependent_template_arguments_p (TI_ARGS (ti
));
4133 --processing_template_decl
;
4137 return DECL_PURE_VIRTUAL_P (t
);
4138 case IS_CONSTRUCTOR
:
4139 return DECL_CONSTRUCTOR_P (t
);
4141 return DECL_DESTRUCTOR_P (t
);
4142 case IS_COPY_CONSTRUCTOR
:
4143 return DECL_COPY_CONSTRUCTOR_P (t
);
4144 case IS_MOVE_CONSTRUCTOR
:
4145 return DECL_MOVE_CONSTRUCTOR_P (t
);
4147 return TREE_CODE (t
) == TEMPLATE_DECL
;
4149 return trivial_type_p (t
);
4155 /* Collect source file references recursively, starting from NAMESPC. */
4158 collect_source_refs (tree namespc
)
4160 /* Iterate over names in this name space. */
4161 for (tree t
= NAMESPACE_LEVEL (namespc
)->names
; t
; t
= TREE_CHAIN (t
))
4162 if (DECL_IS_BUILTIN (t
))
4164 else if (TREE_CODE (t
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (t
))
4165 collect_source_refs (t
);
4167 collect_source_ref (DECL_SOURCE_FILE (t
));
4170 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4171 starting from NAMESPC. */
4174 collect_ada_namespace (tree namespc
, const char *source_file
)
4176 tree decl
= NAMESPACE_LEVEL (namespc
)->names
;
4178 /* Collect decls from this namespace. This will skip
4179 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4180 collect_ada_nodes (decl
, source_file
);
4182 /* Now scan for namespace children, and dump them. */
4183 for (; decl
; decl
= TREE_CHAIN (decl
))
4184 if (TREE_CODE (decl
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (decl
))
4185 collect_ada_namespace (decl
, source_file
);
4188 /* Returns true iff there is a definition available for variable or
4192 decl_defined_p (tree decl
)
4194 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4195 return (DECL_INITIAL (decl
) != NULL_TREE
4196 /* A pending instantiation of a friend temploid is defined. */
4197 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
4198 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4199 (DECL_TI_TEMPLATE (decl
)))));
4202 gcc_assert (VAR_P (decl
));
4203 return !DECL_EXTERNAL (decl
);
4207 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4211 An integral constant-expression can only involve ... const
4212 variables of integral or enumeration types initialized with
4213 constant expressions ...
4215 C++0x also allows constexpr variables and temporaries initialized
4216 with constant expressions. We handle the former here, but the latter
4217 are just folded away in cxx_eval_constant_expression.
4219 The standard does not require that the expression be non-volatile.
4220 G++ implements the proposed correction in DR 457. */
4223 decl_constant_var_p (tree decl
)
4225 if (!decl_maybe_constant_var_p (decl
))
4228 /* We don't know if a template static data member is initialized with
4229 a constant expression until we instantiate its initializer. Even
4230 in the case of a constexpr variable, we can't treat it as a
4231 constant until its initializer is complete in case it's used in
4232 its own initializer. */
4233 maybe_instantiate_decl (decl
);
4234 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
);
4237 /* Returns true if DECL could be a symbolic constant variable, depending on
4241 decl_maybe_constant_var_p (tree decl
)
4243 tree type
= TREE_TYPE (decl
);
4246 if (DECL_DECLARED_CONSTEXPR_P (decl
))
4248 if (DECL_HAS_VALUE_EXPR_P (decl
))
4249 /* A proxy isn't constant. */
4251 if (TYPE_REF_P (type
))
4252 /* References can be constant. */;
4253 else if (CP_TYPE_CONST_NON_VOLATILE_P (type
)
4254 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4255 /* And const integers. */;
4259 if (DECL_INITIAL (decl
)
4260 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
4261 /* We know the initializer, and it isn't constant. */
4267 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4268 called from grokfndecl and grokvardecl; in all modes it is called from
4269 cp_write_global_declarations. */
4272 no_linkage_error (tree decl
)
4274 if (cxx_dialect
>= cxx11
&& decl_defined_p (decl
))
4275 /* In C++11 it's ok if the decl is defined. */
4277 tree t
= no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false);
4279 /* The type that got us on no_linkage_decls must have gotten a name for
4280 linkage purposes. */;
4281 else if (CLASS_TYPE_P (t
) && TYPE_BEING_DEFINED (t
))
4282 /* The type might end up having a typedef name for linkage purposes. */
4283 vec_safe_push (no_linkage_decls
, decl
);
4284 else if (TYPE_UNNAMED_P (t
))
4287 if (cxx_dialect
>= cxx11
)
4288 d
= permerror (DECL_SOURCE_LOCATION (decl
), "%q#D, declared using "
4289 "unnamed type, is used but never defined", decl
);
4290 else if (DECL_EXTERN_C_P (decl
))
4291 /* Allow this; it's pretty common in C. */;
4292 else if (VAR_P (decl
))
4293 /* DRs 132, 319 and 389 seem to indicate types with
4294 no linkage can only be used to declare extern "C"
4295 entities. Since it's not always an error in the
4296 ISO C++ 90 Standard, we only issue a warning. */
4297 d
= warning_at (DECL_SOURCE_LOCATION (decl
), 0, "unnamed type "
4298 "with no linkage used to declare variable %q#D with "
4301 d
= permerror (DECL_SOURCE_LOCATION (decl
), "unnamed type with no "
4302 "linkage used to declare function %q#D with linkage",
4304 if (d
&& is_typedef_decl (TYPE_NAME (t
)))
4305 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)), "%q#D does not refer "
4306 "to the unqualified type, so it is not used for linkage",
4309 else if (cxx_dialect
>= cxx11
)
4311 if (VAR_P (decl
) || !DECL_PURE_VIRTUAL_P (decl
))
4312 permerror (DECL_SOURCE_LOCATION (decl
),
4313 "%q#D, declared using local type "
4314 "%qT, is used but never defined", decl
, t
);
4316 else if (VAR_P (decl
))
4317 warning_at (DECL_SOURCE_LOCATION (decl
), 0, "type %qT with no linkage "
4318 "used to declare variable %q#D with linkage", t
, decl
);
4320 permerror (DECL_SOURCE_LOCATION (decl
), "type %qT with no linkage used "
4321 "to declare function %q#D with linkage", t
, decl
);
4324 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4327 collect_all_refs (const char *source_file
)
4329 collect_ada_namespace (global_namespace
, source_file
);
4332 /* Clear DECL_EXTERNAL for NODE. */
4335 clear_decl_external (struct cgraph_node
*node
, void * /*data*/)
4337 DECL_EXTERNAL (node
->decl
) = 0;
4341 /* Build up the function to run dynamic initializers for thread_local
4342 variables in this translation unit and alias the init functions for the
4343 individual variables to it. */
4346 handle_tls_init (void)
4348 tree vars
= prune_vars_needing_no_initialization (&tls_aggregates
);
4349 if (vars
== NULL_TREE
)
4352 location_t loc
= DECL_SOURCE_LOCATION (TREE_VALUE (vars
));
4354 write_out_vars (vars
);
4356 tree guard
= build_decl (loc
, VAR_DECL
, get_identifier ("__tls_guard"),
4358 TREE_PUBLIC (guard
) = false;
4359 TREE_STATIC (guard
) = true;
4360 DECL_ARTIFICIAL (guard
) = true;
4361 DECL_IGNORED_P (guard
) = true;
4362 TREE_USED (guard
) = true;
4363 CP_DECL_THREAD_LOCAL_P (guard
) = true;
4364 set_decl_tls_model (guard
, decl_default_tls_model (guard
));
4365 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
4367 tree fn
= get_local_tls_init_fn ();
4368 start_preparsed_function (fn
, NULL_TREE
, SF_PRE_PARSED
);
4369 tree body
= begin_function_body ();
4370 tree if_stmt
= begin_if_stmt ();
4371 tree cond
= cp_build_unary_op (TRUTH_NOT_EXPR
, guard
, false,
4372 tf_warning_or_error
);
4373 finish_if_stmt_cond (cond
, if_stmt
);
4374 finish_expr_stmt (cp_build_modify_expr (loc
, guard
, NOP_EXPR
,
4376 tf_warning_or_error
));
4377 for (; vars
; vars
= TREE_CHAIN (vars
))
4379 tree var
= TREE_VALUE (vars
);
4380 tree init
= TREE_PURPOSE (vars
);
4381 one_static_initialization_or_destruction (var
, init
, true);
4383 /* Output init aliases even with -fno-extern-tls-init. */
4384 if (TARGET_SUPPORTS_ALIASES
&& TREE_PUBLIC (var
))
4386 tree single_init_fn
= get_tls_init_fn (var
);
4387 if (single_init_fn
== NULL_TREE
)
4390 = cgraph_node::get_create (fn
)->create_same_body_alias
4391 (single_init_fn
, fn
);
4392 gcc_assert (alias
!= NULL
);
4396 finish_then_clause (if_stmt
);
4397 finish_if_stmt (if_stmt
);
4398 finish_function_body (body
);
4399 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4402 /* We're at the end of compilation, so generate any mangling aliases that
4403 we've been saving up, if DECL is going to be output and ID2 isn't
4404 already taken by another declaration. */
4407 generate_mangling_alias (tree decl
, tree id2
)
4409 struct cgraph_node
*n
= NULL
;
4411 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4413 n
= cgraph_node::get (decl
);
4415 /* Don't create an alias to an unreferenced function. */
4420 = mangled_decls
->find_slot_with_hash (id2
, IDENTIFIER_HASH_VALUE (id2
),
4423 /* If there's a declaration already using this mangled name,
4424 don't create a compatibility alias that conflicts. */
4428 tree alias
= make_alias_for (decl
, id2
);
4431 DECL_IGNORED_P (alias
) = 1;
4432 TREE_PUBLIC (alias
) = TREE_PUBLIC (decl
);
4433 DECL_VISIBILITY (alias
) = DECL_VISIBILITY (decl
);
4434 if (vague_linkage_p (decl
))
4435 DECL_WEAK (alias
) = 1;
4438 n
->create_same_body_alias (alias
, decl
);
4440 varpool_node::create_extra_name_alias (alias
, decl
);
4443 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4444 the end of translation, for compatibility across bugs in the mangling
4448 note_mangling_alias (tree decl
, tree id2
)
4450 if (TARGET_SUPPORTS_ALIASES
)
4452 if (!defer_mangling_aliases
)
4453 generate_mangling_alias (decl
, id2
);
4456 vec_safe_push (mangling_aliases
, decl
);
4457 vec_safe_push (mangling_aliases
, id2
);
4462 /* Emit all mangling aliases that were deferred up to this point. */
4465 generate_mangling_aliases ()
4467 while (!vec_safe_is_empty (mangling_aliases
))
4469 tree id2
= mangling_aliases
->pop();
4470 tree decl
= mangling_aliases
->pop();
4471 generate_mangling_alias (decl
, id2
);
4473 defer_mangling_aliases
= false;
4476 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4477 set. NEED_WARNING is true if we must warn about collisions. We do
4478 this to spot changes in mangling that may require compatibility
4482 record_mangling (tree decl
, bool need_warning
)
4485 mangled_decls
= hash_table
<mangled_decl_hash
>::create_ggc (499);
4487 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
4488 tree id
= DECL_ASSEMBLER_NAME_RAW (decl
);
4490 = mangled_decls
->find_slot_with_hash (id
, IDENTIFIER_HASH_VALUE (id
),
4493 /* If this is already an alias, remove the alias, because the real
4494 decl takes precedence. */
4495 if (*slot
&& DECL_ARTIFICIAL (*slot
) && DECL_IGNORED_P (*slot
))
4496 if (symtab_node
*n
= symtab_node::get (*slot
))
4497 if (n
->cpp_implicit_alias
)
4505 else if (need_warning
)
4507 error_at (DECL_SOURCE_LOCATION (decl
),
4508 "mangling of %q#D as %qE conflicts with a previous mangle",
4510 inform (DECL_SOURCE_LOCATION (*slot
),
4511 "previous mangling %q#D", *slot
);
4512 inform (DECL_SOURCE_LOCATION (decl
),
4513 "a later -fabi-version= (or =0)"
4514 " avoids this error with a change in mangling");
4519 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4520 any existing knowledge of DECL's mangled name meaning DECL. */
4523 overwrite_mangling (tree decl
, tree name
)
4525 if (tree id
= DECL_ASSEMBLER_NAME_RAW (decl
))
4526 if ((TREE_CODE (decl
) == VAR_DECL
4527 || TREE_CODE (decl
) == FUNCTION_DECL
)
4530 = mangled_decls
->find_slot_with_hash (id
, IDENTIFIER_HASH_VALUE (id
),
4534 mangled_decls
->clear_slot (slot
);
4536 /* If this is an alias, remove it from the symbol table. */
4537 if (DECL_ARTIFICIAL (decl
) && DECL_IGNORED_P (decl
))
4538 if (symtab_node
*n
= symtab_node::get (decl
))
4539 if (n
->cpp_implicit_alias
)
4543 DECL_ASSEMBLER_NAME_RAW (decl
) = name
;
4546 /* The entire file is now complete. If requested, dump everything
4553 if (FILE *stream
= dump_begin (raw_dump_id
, &flags
))
4555 dump_node (global_namespace
, flags
& ~TDF_SLIM
, stream
);
4556 dump_end (raw_dump_id
, stream
);
4560 static location_t locus_at_end_of_parsing
;
4562 /* Check the deallocation functions for CODE to see if we want to warn that
4563 only one was defined. */
4566 maybe_warn_sized_delete (enum tree_code code
)
4568 tree sized
= NULL_TREE
;
4569 tree unsized
= NULL_TREE
;
4571 for (ovl_iterator
iter (get_global_binding (ovl_op_identifier (false, code
)));
4575 /* We're only interested in usual deallocation functions. */
4576 if (!usual_deallocation_fn_p (fn
))
4578 if (FUNCTION_ARG_CHAIN (fn
) == void_list_node
)
4583 if (DECL_INITIAL (unsized
) && !DECL_INITIAL (sized
))
4584 warning_at (DECL_SOURCE_LOCATION (unsized
), OPT_Wsized_deallocation
,
4585 "the program should also define %qD", sized
);
4586 else if (!DECL_INITIAL (unsized
) && DECL_INITIAL (sized
))
4587 warning_at (DECL_SOURCE_LOCATION (sized
), OPT_Wsized_deallocation
,
4588 "the program should also define %qD", unsized
);
4591 /* Check the global deallocation functions to see if we want to warn about
4592 defining unsized without sized (or vice versa). */
4595 maybe_warn_sized_delete ()
4597 if (!flag_sized_deallocation
|| !warn_sized_deallocation
)
4599 maybe_warn_sized_delete (DELETE_EXPR
);
4600 maybe_warn_sized_delete (VEC_DELETE_EXPR
);
4603 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4604 look them up when evaluating non-type template parameters. Now we need to
4605 lower them to something the back end can understand. */
4611 FOR_EACH_VARIABLE (node
)
4613 tree d
= node
->decl
;
4614 if (tree init
= DECL_INITIAL (d
))
4615 DECL_INITIAL (d
) = cplus_expand_constant (init
);
4619 /* This routine is called at the end of compilation.
4620 Its job is to create all the code needed to initialize and
4621 destroy the global aggregates. We do the destruction
4622 first, since that way we only need to reverse the decls once. */
4625 c_parse_final_cleanups (void)
4630 unsigned ssdf_count
= 0;
4634 locus_at_end_of_parsing
= input_location
;
4637 /* Bad parse errors. Just forget about it. */
4638 if (! global_bindings_p () || current_class_type
4639 || !vec_safe_is_empty (decl_namespace_list
))
4642 /* This is the point to write out a PCH if we're doing that.
4643 In that case we do not want to do anything else. */
4646 /* Mangle all symbols at PCH creation time. */
4648 FOR_EACH_SYMBOL (node
)
4649 if (! is_a
<varpool_node
*> (node
)
4650 || ! DECL_HARD_REGISTER (node
->decl
))
4651 DECL_ASSEMBLER_NAME (node
->decl
);
4652 c_common_write_pch ();
4654 /* Ensure even the callers don't try to finalize the CU. */
4655 flag_syntax_only
= 1;
4659 timevar_stop (TV_PHASE_PARSING
);
4660 timevar_start (TV_PHASE_DEFERRED
);
4662 symtab
->process_same_body_aliases ();
4664 /* Handle -fdump-ada-spec[-slim] */
4665 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
4667 if (flag_dump_ada_spec_slim
)
4668 collect_source_ref (main_input_filename
);
4670 collect_source_refs (global_namespace
);
4672 dump_ada_specs (collect_all_refs
, cpp_check
);
4675 /* FIXME - huh? was input_line -= 1;*/
4677 /* We now have to write out all the stuff we put off writing out.
4680 o Template specializations that we have not yet instantiated,
4681 but which are needed.
4682 o Initialization and destruction for non-local objects with
4683 static storage duration. (Local objects with static storage
4684 duration are initialized when their scope is first entered,
4685 and are cleaned up via atexit.)
4686 o Virtual function tables.
4688 All of these may cause others to be needed. For example,
4689 instantiating one function may cause another to be needed, and
4690 generating the initializer for an object may cause templates to be
4691 instantiated, etc., etc. */
4693 emit_support_tinfos ();
4702 /* If there are templates that we've put off instantiating, do
4704 instantiate_pending_templates (retries
);
4707 /* Write out virtual tables as required. Writing out the
4708 virtual table for a template class may cause the
4709 instantiation of members of that class. If we write out
4710 vtables then we remove the class from our list so we don't
4711 have to look at it again. */
4712 for (i
= keyed_classes
->length ();
4713 keyed_classes
->iterate (--i
, &t
);)
4714 if (maybe_emit_vtables (t
))
4717 keyed_classes
->unordered_remove (i
);
4719 /* The input_location may have been changed during marking of
4721 input_location
= locus_at_end_of_parsing
;
4723 /* Write out needed type info variables. We have to be careful
4724 looping through unemitted decls, because emit_tinfo_decl may
4725 cause other variables to be needed. New elements will be
4726 appended, and we remove from the vector those that actually
4728 for (i
= unemitted_tinfo_decls
->length ();
4729 unemitted_tinfo_decls
->iterate (--i
, &t
);)
4730 if (emit_tinfo_decl (t
))
4733 unemitted_tinfo_decls
->unordered_remove (i
);
4736 /* The list of objects with static storage duration is built up
4737 in reverse order. We clear STATIC_AGGREGATES so that any new
4738 aggregates added during the initialization of these will be
4739 initialized in the correct order when we next come around the
4741 vars
= prune_vars_needing_no_initialization (&static_aggregates
);
4745 /* We need to start a new initialization function each time
4746 through the loop. That's because we need to know which
4747 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4748 isn't computed until a function is finished, and written
4749 out. That's a deficiency in the back end. When this is
4750 fixed, these initialization functions could all become
4751 inline, with resulting performance improvements. */
4754 /* Set the line and file, so that it is obviously not from
4756 input_location
= locus_at_end_of_parsing
;
4757 ssdf_body
= start_static_storage_duration_function (ssdf_count
);
4759 /* Make sure the back end knows about all the variables. */
4760 write_out_vars (vars
);
4762 /* First generate code to do all the initializations. */
4764 do_static_initialization_or_destruction (vars
, /*initp=*/true);
4766 /* Then, generate code to do all the destructions. Do these
4767 in reverse order so that the most recently constructed
4768 variable is the first destroyed. If we're using
4769 __cxa_atexit, then we don't need to do this; functions
4770 were registered at initialization time to destroy the
4772 if (!flag_use_cxa_atexit
&& vars
)
4774 vars
= nreverse (vars
);
4775 do_static_initialization_or_destruction (vars
, /*initp=*/false);
4780 /* Finish up the static storage duration function for this
4782 input_location
= locus_at_end_of_parsing
;
4783 finish_static_storage_duration_function (ssdf_body
);
4785 /* All those initializations and finalizations might cause
4786 us to need more inline functions, more template
4787 instantiations, etc. */
4790 /* ??? was: locus_at_end_of_parsing.line++; */
4793 /* Now do the same for thread_local variables. */
4796 /* Go through the set of inline functions whose bodies have not
4797 been emitted yet. If out-of-line copies of these functions
4798 are required, emit them. */
4799 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4801 /* Does it need synthesizing? */
4802 if (DECL_DEFAULTED_FN (decl
) && ! DECL_INITIAL (decl
)
4803 && (! DECL_REALLY_EXTERN (decl
) || possibly_inlined_p (decl
)))
4805 /* Even though we're already at the top-level, we push
4806 there again. That way, when we pop back a few lines
4807 hence, all of our state is restored. Otherwise,
4808 finish_function doesn't clean things up, and we end
4809 up with CURRENT_FUNCTION_DECL set. */
4810 push_to_top_level ();
4811 /* The decl's location will mark where it was first
4812 needed. Save that so synthesize method can indicate
4813 where it was needed from, in case of error */
4814 input_location
= DECL_SOURCE_LOCATION (decl
);
4815 synthesize_method (decl
);
4816 pop_from_top_level ();
4820 if (!DECL_INITIAL (decl
) && decl_tls_wrapper_p (decl
))
4821 generate_tls_wrapper (decl
);
4823 if (!DECL_SAVED_TREE (decl
))
4826 cgraph_node
*node
= cgraph_node::get_create (decl
);
4828 /* We lie to the back end, pretending that some functions
4829 are not defined when they really are. This keeps these
4830 functions from being put out unnecessarily. But, we must
4831 stop lying when the functions are referenced, or if they
4832 are not comdat since they need to be put out now. If
4833 DECL_INTERFACE_KNOWN, then we have already set
4834 DECL_EXTERNAL appropriately, so there's no need to check
4835 again, and we do not want to clear DECL_EXTERNAL if a
4836 previous call to import_export_decl set it.
4838 This is done in a separate for cycle, because if some
4839 deferred function is contained in another deferred
4840 function later in deferred_fns varray,
4841 rest_of_compilation would skip this function and we
4842 really cannot expand the same function twice. */
4843 import_export_decl (decl
);
4844 if (DECL_NOT_REALLY_EXTERN (decl
)
4845 && DECL_INITIAL (decl
)
4846 && decl_needed_p (decl
))
4848 if (node
->cpp_implicit_alias
)
4849 node
= node
->get_alias_target ();
4851 node
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
4853 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4854 group, we need to mark all symbols in the same comdat group
4856 if (node
->same_comdat_group
)
4857 for (cgraph_node
*next
4858 = dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
4860 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
4861 next
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
4865 /* If we're going to need to write this function out, and
4866 there's already a body for it, create RTL for it now.
4867 (There might be no body if this is a method we haven't
4868 gotten around to synthesizing yet.) */
4869 if (!DECL_EXTERNAL (decl
)
4870 && decl_needed_p (decl
)
4871 && !TREE_ASM_WRITTEN (decl
)
4872 && !node
->definition
)
4874 /* We will output the function; no longer consider it in this
4876 DECL_DEFER_OUTPUT (decl
) = 0;
4877 /* Generate RTL for this function now that we know we
4879 expand_or_defer_fn (decl
);
4880 /* If we're compiling -fsyntax-only pretend that this
4881 function has been written out so that we don't try to
4883 if (flag_syntax_only
)
4884 TREE_ASM_WRITTEN (decl
) = 1;
4889 if (wrapup_namespace_globals ())
4892 /* Static data members are just like namespace-scope globals. */
4893 FOR_EACH_VEC_SAFE_ELT (pending_statics
, i
, decl
)
4895 if (var_finalized_p (decl
) || DECL_REALLY_EXTERN (decl
)
4896 /* Don't write it out if we haven't seen a definition. */
4897 || (DECL_IN_AGGR_P (decl
) && !DECL_INLINE_VAR_P (decl
)))
4899 import_export_decl (decl
);
4900 /* If this static data member is needed, provide it to the
4902 if (DECL_NOT_REALLY_EXTERN (decl
) && decl_needed_p (decl
))
4903 DECL_EXTERNAL (decl
) = 0;
4905 if (vec_safe_length (pending_statics
) != 0
4906 && wrapup_global_declarations (pending_statics
->address (),
4907 pending_statics
->length ()))
4916 generate_mangling_aliases ();
4918 /* All used inline functions must have a definition at this point. */
4919 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4921 if (/* Check online inline functions that were actually used. */
4922 DECL_ODR_USED (decl
) && DECL_DECLARED_INLINE_P (decl
)
4923 /* If the definition actually was available here, then the
4924 fact that the function was not defined merely represents
4925 that for some reason (use of a template repository,
4926 #pragma interface, etc.) we decided not to emit the
4928 && !DECL_INITIAL (decl
)
4929 /* Don't complain if the template was defined. */
4930 && !(DECL_TEMPLATE_INSTANTIATION (decl
)
4931 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4932 (template_for_substitution (decl
)))))
4934 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
4935 "inline function %qD used but never defined", decl
);
4936 /* Avoid a duplicate warning from check_global_declaration. */
4937 TREE_NO_WARNING (decl
) = 1;
4941 /* So must decls that use a type with no linkage. */
4942 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls
, i
, decl
)
4943 no_linkage_error (decl
);
4945 maybe_warn_sized_delete ();
4947 /* Then, do the Objective-C stuff. This is where all the
4948 Objective-C module stuff gets generated (symtab,
4949 class/protocol/selector lists etc). This must be done after C++
4950 templates, destructors etc. so that selectors used in C++
4951 templates are properly allocated. */
4952 if (c_dialect_objc ())
4953 objc_write_global_declarations ();
4955 /* We give C linkage to static constructors and destructors. */
4956 push_lang_context (lang_name_c
);
4958 /* Generate initialization and destruction functions for all
4959 priorities for which they are required. */
4960 if (priority_info_map
)
4961 splay_tree_foreach (priority_info_map
,
4962 generate_ctor_and_dtor_functions_for_priority
,
4963 /*data=*/&locus_at_end_of_parsing
);
4964 else if (c_dialect_objc () && objc_static_init_needed_p ())
4965 /* If this is obj-c++ and we need a static init, call
4966 generate_ctor_or_dtor_function. */
4967 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4968 DEFAULT_INIT_PRIORITY
,
4969 &locus_at_end_of_parsing
);
4971 /* We're done with the splay-tree now. */
4972 if (priority_info_map
)
4973 splay_tree_delete (priority_info_map
);
4975 /* Generate any missing aliases. */
4976 maybe_apply_pending_pragma_weaks ();
4978 /* We're done with static constructors, so we can go back to "C++"
4980 pop_lang_context ();
4982 if (flag_vtable_verify
)
4984 vtv_recover_class_info ();
4985 vtv_compute_class_hierarchy_transitive_closure ();
4986 vtv_build_vtable_verify_fndecl ();
4989 perform_deferred_noexcept_checks ();
4994 /* The entire file is now complete. If requested, dump everything
4998 if (flag_detailed_statistics
)
5000 dump_tree_statistics ();
5001 dump_time_statistics ();
5004 timevar_stop (TV_PHASE_DEFERRED
);
5005 timevar_start (TV_PHASE_PARSING
);
5007 /* Indicate that we're done with front end processing. */
5011 /* Perform any post compilation-proper cleanups for the C++ front-end.
5012 This should really go away. No front-end should need to do
5013 anything past the compilation process. */
5016 cxx_post_compilation_parsing_cleanups (void)
5018 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS
);
5020 if (flag_vtable_verify
)
5022 /* Generate the special constructor initialization function that
5023 calls __VLTRegisterPairs, and give it a very high
5024 initialization priority. This must be done after
5025 finalize_compilation_unit so that we have accurate
5026 information about which vtable will actually be emitted. */
5027 vtv_generate_init_routine ();
5030 input_location
= locus_at_end_of_parsing
;
5033 validate_conversion_obstack ();
5035 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS
);
5038 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5039 function to call in parse-tree form; it has not yet been
5040 semantically analyzed. ARGS are the arguments to the function.
5041 They have already been semantically analyzed. This may change
5045 build_offset_ref_call_from_tree (tree fn
, vec
<tree
, va_gc
> **args
,
5046 tsubst_flags_t complain
)
5049 vec
<tree
, va_gc
> *orig_args
= NULL
;
5054 object
= TREE_OPERAND (fn
, 0);
5056 if (processing_template_decl
)
5058 gcc_assert (TREE_CODE (fn
) == DOTSTAR_EXPR
5059 || TREE_CODE (fn
) == MEMBER_REF
);
5060 if (type_dependent_expression_p (fn
)
5061 || any_type_dependent_arguments_p (*args
))
5062 return build_min_nt_call_vec (fn
, *args
);
5064 orig_args
= make_tree_vector_copy (*args
);
5066 /* Transform the arguments and add the implicit "this"
5067 parameter. That must be done before the FN is transformed
5068 because we depend on the form of FN. */
5069 make_args_non_dependent (*args
);
5070 object
= build_non_dependent_expr (object
);
5071 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
5073 if (TREE_CODE (fn
) == DOTSTAR_EXPR
)
5074 object
= cp_build_addr_expr (object
, complain
);
5075 vec_safe_insert (*args
, 0, object
);
5077 /* Now that the arguments are done, transform FN. */
5078 fn
= build_non_dependent_expr (fn
);
5081 /* A qualified name corresponding to a bound pointer-to-member is
5082 represented as an OFFSET_REF:
5084 struct B { void g(); };
5086 void B::g() { (this->*p)(); } */
5087 if (TREE_CODE (fn
) == OFFSET_REF
)
5089 tree object_addr
= cp_build_addr_expr (object
, complain
);
5090 fn
= TREE_OPERAND (fn
, 1);
5091 fn
= get_member_function_from_ptrfunc (&object_addr
, fn
,
5093 vec_safe_insert (*args
, 0, object_addr
);
5096 if (CLASS_TYPE_P (TREE_TYPE (fn
)))
5097 expr
= build_op_call (fn
, args
, complain
);
5099 expr
= cp_build_function_call_vec (fn
, args
, complain
);
5100 if (processing_template_decl
&& expr
!= error_mark_node
)
5101 expr
= build_min_non_dep_call_vec (expr
, orig_fn
, orig_args
);
5103 if (orig_args
!= NULL
)
5104 release_tree_vector (orig_args
);
5111 check_default_args (tree x
)
5113 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
5114 bool saw_def
= false;
5115 int i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
5116 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
5118 if (TREE_PURPOSE (arg
))
5120 else if (saw_def
&& !PACK_EXPANSION_P (TREE_VALUE (arg
)))
5122 error ("default argument missing for parameter %P of %q+#D", i
, x
);
5123 TREE_PURPOSE (arg
) = error_mark_node
;
5128 /* Return true if function DECL can be inlined. This is used to force
5129 instantiation of methods that might be interesting for inlining. */
5131 possibly_inlined_p (tree decl
)
5133 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
5134 if (DECL_UNINLINABLE (decl
))
5137 return DECL_DECLARED_INLINE_P (decl
);
5138 /* When optimizing, we might inline everything when flatten
5139 attribute or heuristics inlining for size or autoinlining
5144 /* Normally, we can wait until instantiation-time to synthesize DECL.
5145 However, if DECL is a static data member initialized with a constant
5146 or a constexpr function, we need it right now because a reference to
5147 such a data member or a call to such function is not value-dependent.
5148 For a function that uses auto in the return type, we need to instantiate
5149 it to find out its type. For OpenMP user defined reductions, we need
5150 them instantiated for reduction clauses which inline them by hand
5154 maybe_instantiate_decl (tree decl
)
5156 if (DECL_LANG_SPECIFIC (decl
)
5157 && DECL_TEMPLATE_INFO (decl
)
5158 && (decl_maybe_constant_var_p (decl
)
5159 || (TREE_CODE (decl
) == FUNCTION_DECL
5160 && DECL_OMP_DECLARE_REDUCTION_P (decl
))
5161 || undeduced_auto_decl (decl
))
5162 && !DECL_DECLARED_CONCEPT_P (decl
)
5163 && !uses_template_parms (DECL_TI_ARGS (decl
)))
5165 /* Instantiating a function will result in garbage collection. We
5166 must treat this situation as if we were within the body of a
5167 function so as to avoid collecting live data only referenced from
5168 the stack (such as overload resolution candidates). */
5170 instantiate_decl (decl
, /*defer_ok=*/false,
5171 /*expl_inst_class_mem_p=*/false);
5176 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5177 If DECL is a specialization or implicitly declared class member,
5178 generate the actual definition. Return false if something goes
5179 wrong, true otherwise. */
5182 mark_used (tree decl
, tsubst_flags_t complain
)
5184 /* If we're just testing conversions or resolving overloads, we
5185 don't want any permanent effects like forcing functions to be
5186 output or instantiating templates. */
5187 if ((complain
& tf_conv
))
5190 /* If DECL is a BASELINK for a single function, then treat it just
5191 like the DECL for the function. Otherwise, if the BASELINK is
5192 for an overloaded function, we don't know which function was
5193 actually used until after overload resolution. */
5194 if (BASELINK_P (decl
))
5196 decl
= BASELINK_FUNCTIONS (decl
);
5197 if (really_overloaded_fn (decl
))
5199 decl
= OVL_FIRST (decl
);
5202 /* Set TREE_USED for the benefit of -Wunused. */
5203 TREE_USED (decl
) = 1;
5204 /* And for structured bindings also the underlying decl. */
5205 if (DECL_DECOMPOSITION_P (decl
) && DECL_DECOMP_BASE (decl
))
5206 TREE_USED (DECL_DECOMP_BASE (decl
)) = 1;
5208 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5211 if (DECL_CLONED_FUNCTION_P (decl
))
5212 TREE_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
5214 /* Mark enumeration types as used. */
5215 if (TREE_CODE (decl
) == CONST_DECL
)
5216 used_types_insert (DECL_CONTEXT (decl
));
5218 if (TREE_CODE (decl
) == FUNCTION_DECL
5219 && !maybe_instantiate_noexcept (decl
, complain
))
5222 if (TREE_CODE (decl
) == FUNCTION_DECL
5223 && DECL_DELETED_FN (decl
))
5225 if (DECL_ARTIFICIAL (decl
)
5226 && DECL_CONV_FN_P (decl
)
5227 && LAMBDA_TYPE_P (DECL_CONTEXT (decl
)))
5228 /* We mark a lambda conversion op as deleted if we can't
5229 generate it properly; see maybe_add_lambda_conv_op. */
5230 sorry ("converting lambda that uses %<...%> to function pointer");
5231 else if (complain
& tf_error
)
5233 error ("use of deleted function %qD", decl
);
5234 if (!maybe_explain_implicit_delete (decl
))
5235 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
5240 if (TREE_DEPRECATED (decl
) && (complain
& tf_warning
)
5241 && deprecated_state
!= DEPRECATED_SUPPRESS
)
5242 warn_deprecated_use (decl
, NULL_TREE
);
5244 /* We can only check DECL_ODR_USED on variables or functions with
5245 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5246 might need special handling for. */
5247 if (!VAR_OR_FUNCTION_DECL_P (decl
)
5248 || DECL_LANG_SPECIFIC (decl
) == NULL
5249 || DECL_THUNK_P (decl
))
5251 if (!processing_template_decl
5252 && !require_deduced_type (decl
, complain
))
5257 /* We only want to do this processing once. We don't need to keep trying
5258 to instantiate inline templates, because unit-at-a-time will make sure
5259 we get them compiled before functions that want to inline them. */
5260 if (DECL_ODR_USED (decl
))
5263 /* Normally, we can wait until instantiation-time to synthesize DECL.
5264 However, if DECL is a static data member initialized with a constant
5265 or a constexpr function, we need it right now because a reference to
5266 such a data member or a call to such function is not value-dependent.
5267 For a function that uses auto in the return type, we need to instantiate
5268 it to find out its type. For OpenMP user defined reductions, we need
5269 them instantiated for reduction clauses which inline them by hand
5271 maybe_instantiate_decl (decl
);
5273 if (processing_template_decl
|| in_template_function ())
5276 /* Check this too in case we're within instantiate_non_dependent_expr. */
5277 if (DECL_TEMPLATE_INFO (decl
)
5278 && uses_template_parms (DECL_TI_ARGS (decl
)))
5281 if (!require_deduced_type (decl
, complain
))
5284 if (builtin_pack_fn_p (decl
))
5286 error ("use of built-in parameter pack %qD outside of a template",
5291 /* If we don't need a value, then we don't need to synthesize DECL. */
5292 if (cp_unevaluated_operand
|| in_discarded_stmt
)
5295 DECL_ODR_USED (decl
) = 1;
5296 if (DECL_CLONED_FUNCTION_P (decl
))
5297 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
5299 /* DR 757: A type without linkage shall not be used as the type of a
5300 variable or function with linkage, unless
5301 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5302 o the variable or function is not used (3.2 [basic.def.odr]) or is
5303 defined in the same translation unit. */
5304 if (cxx_dialect
> cxx98
5305 && decl_linkage (decl
) != lk_none
5306 && !DECL_EXTERN_C_P (decl
)
5307 && !DECL_ARTIFICIAL (decl
)
5308 && !decl_defined_p (decl
)
5309 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
5311 if (is_local_extern (decl
))
5312 /* There's no way to define a local extern, and adding it to
5313 the vector interferes with GC, so give an error now. */
5314 no_linkage_error (decl
);
5316 vec_safe_push (no_linkage_decls
, decl
);
5319 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (decl
)
5320 && !DECL_INITIAL (decl
) && !DECL_ARTIFICIAL (decl
))
5321 /* Remember it, so we can check it was defined. */
5322 note_vague_linkage_fn (decl
);
5324 /* Is it a synthesized method that needs to be synthesized? */
5325 if (TREE_CODE (decl
) == FUNCTION_DECL
5326 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
5327 && DECL_DEFAULTED_FN (decl
)
5328 /* A function defaulted outside the class is synthesized either by
5329 cp_finish_decl or instantiate_decl. */
5330 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl
)
5331 && ! DECL_INITIAL (decl
))
5333 /* Defer virtual destructors so that thunks get the right
5335 if (DECL_VIRTUAL_P (decl
) && !at_eof
)
5337 note_vague_linkage_fn (decl
);
5341 /* Remember the current location for a function we will end up
5342 synthesizing. Then we can inform the user where it was
5343 required in the case of error. */
5344 DECL_SOURCE_LOCATION (decl
) = input_location
;
5346 /* Synthesizing an implicitly defined member function will result in
5347 garbage collection. We must treat this situation as if we were
5348 within the body of a function so as to avoid collecting live data
5349 on the stack (such as overload resolution candidates).
5351 We could just let cp_write_global_declarations handle synthesizing
5352 this function by adding it to deferred_fns, but doing
5353 it at the use site produces better error messages. */
5355 synthesize_method (decl
);
5357 /* If this is a synthesized method we don't need to
5358 do the instantiation test below. */
5360 else if (VAR_OR_FUNCTION_DECL_P (decl
)
5361 && DECL_TEMPLATE_INFO (decl
)
5362 && !DECL_DECLARED_CONCEPT_P (decl
)
5363 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
5364 || always_instantiate_p (decl
)))
5365 /* If this is a function or variable that is an instance of some
5366 template, we now know that we will need to actually do the
5367 instantiation. We check that DECL is not an explicit
5368 instantiation because that is not checked in instantiate_decl.
5370 We put off instantiating functions in order to improve compile
5371 times. Maintaining a stack of active functions is expensive,
5372 and the inliner knows to instantiate any functions it might
5373 need. Therefore, we always try to defer instantiation. */
5376 instantiate_decl (decl
, /*defer_ok=*/true,
5377 /*expl_inst_class_mem_p=*/false);
5385 mark_used (tree decl
)
5387 return mark_used (decl
, tf_warning_or_error
);
5391 vtv_start_verification_constructor_init_function (void)
5393 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY
- 1);
5397 vtv_finish_verification_constructor_init_function (tree function_body
)
5401 finish_compound_stmt (function_body
);
5402 fn
= finish_function (/*inline_p=*/false);
5403 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
5404 decl_init_priority_insert (fn
, MAX_RESERVED_INIT_PRIORITY
- 1);
5409 #include "gt-cp-decl2.h"