1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2019 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 if (DECL_TEMPLATE_INSTANTIATED (decl
))
748 /* We already needed to instantiate this, so the processing in this
749 function is unnecessary/wrong. */
752 DECL_CONTEXT (decl
) = current_class_type
;
754 /* We cannot call pushdecl here, because that would fill in the
755 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
756 the right thing, namely, to put this decl out straight away. */
758 if (! processing_template_decl
)
759 vec_safe_push (pending_statics
, decl
);
761 if (LOCAL_CLASS_P (current_class_type
)
762 /* We already complained about the template definition. */
763 && !DECL_TEMPLATE_INSTANTIATION (decl
))
764 permerror (DECL_SOURCE_LOCATION (decl
),
765 "local class %q#T shall not have static data member %q#D",
766 current_class_type
, decl
);
768 for (tree t
= current_class_type
; TYPE_P (t
);
769 t
= CP_TYPE_CONTEXT (t
))
770 if (TYPE_UNNAMED_P (t
))
772 auto_diagnostic_group d
;
773 if (permerror (DECL_SOURCE_LOCATION (decl
),
774 "static data member %qD in unnamed class", decl
))
775 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)),
776 "unnamed class defined here");
780 if (DECL_INLINE_VAR_P (decl
) && !DECL_TEMPLATE_INSTANTIATION (decl
))
781 /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P.
782 Except that if decl is a template instantiation, it isn't defined until
783 instantiate_decl. */;
785 DECL_IN_AGGR_P (decl
) = 1;
787 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
788 && TYPE_DOMAIN (TREE_TYPE (decl
)) == NULL_TREE
)
789 SET_VAR_HAD_UNKNOWN_BOUND (decl
);
793 /* Similarly to start_decl_1, we want to complete the type in order
794 to do the right thing in cp_apply_type_quals_to_decl, possibly
795 clear TYPE_QUAL_CONST (c++/65579). */
796 tree type
= TREE_TYPE (decl
) = complete_type (TREE_TYPE (decl
));
797 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
800 cp_finish_decl (decl
, init
, init_const_expr_p
, asmspec_tree
, flags
);
803 /* DECLARATOR and DECLSPECS correspond to a class member. The other
804 parameters are as for cp_finish_decl. Return the DECL for the
805 class member declared. */
808 grokfield (const cp_declarator
*declarator
,
809 cp_decl_specifier_seq
*declspecs
,
810 tree init
, bool init_const_expr_p
,
815 const char *asmspec
= 0;
819 && TREE_CODE (init
) == TREE_LIST
820 && TREE_VALUE (init
) == error_mark_node
821 && TREE_CHAIN (init
) == NULL_TREE
)
824 value
= grokdeclarator (declarator
, declspecs
, FIELD
, init
!= 0, &attrlist
);
825 if (! value
|| value
== error_mark_node
)
826 /* friend or constructor went bad. */
827 return error_mark_node
;
828 if (TREE_TYPE (value
) == error_mark_node
)
831 if (TREE_CODE (value
) == TYPE_DECL
&& init
)
833 error_at (cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (value
)),
834 "typedef %qD is initialized (use decltype instead)", value
);
838 /* Pass friendly classes back. */
839 if (value
== void_type_node
)
842 if (DECL_NAME (value
)
843 && TREE_CODE (DECL_NAME (value
)) == TEMPLATE_ID_EXPR
)
845 error_at (declarator
->id_loc
,
846 "explicit template argument list not allowed");
847 return error_mark_node
;
850 /* Stash away type declarations. */
851 if (TREE_CODE (value
) == TYPE_DECL
)
853 DECL_NONLOCAL (value
) = 1;
854 DECL_CONTEXT (value
) = current_class_type
;
860 /* If this is a typedef that names the class for linkage purposes
861 (7.1.3p8), apply any attributes directly to the type. */
862 if (OVERLOAD_TYPE_P (TREE_TYPE (value
))
863 && value
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
864 attrflags
= ATTR_FLAG_TYPE_IN_PLACE
;
866 cplus_decl_attributes (&value
, attrlist
, attrflags
);
869 if (decl_spec_seq_has_spec_p (declspecs
, ds_typedef
)
870 && TREE_TYPE (value
) != error_mark_node
871 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))) != value
)
872 set_underlying_type (value
);
874 /* It's important that push_template_decl below follows
875 set_underlying_type above so that the created template
876 carries the properly set type of VALUE. */
877 if (processing_template_decl
)
878 value
= push_template_decl (value
);
880 record_locally_defined_typedef (value
);
884 int friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
886 if (!friendp
&& DECL_IN_AGGR_P (value
))
888 error ("%qD is already defined in %qT", value
, DECL_CONTEXT (value
));
889 return void_type_node
;
892 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
893 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
897 if (TREE_CODE (value
) == FUNCTION_DECL
)
899 if (init
== ridpointers
[(int)RID_DELETE
])
901 if (friendp
&& decl_defined_p (value
))
903 error ("redefinition of %q#D", value
);
904 inform (DECL_SOURCE_LOCATION (value
),
905 "%q#D previously defined here", value
);
909 DECL_DELETED_FN (value
) = 1;
910 DECL_DECLARED_INLINE_P (value
) = 1;
911 DECL_INITIAL (value
) = error_mark_node
;
914 else if (init
== ridpointers
[(int)RID_DEFAULT
])
916 if (defaultable_fn_check (value
))
918 DECL_DEFAULTED_FN (value
) = 1;
919 DECL_INITIALIZED_IN_CLASS_P (value
) = 1;
920 DECL_DECLARED_INLINE_P (value
) = 1;
923 else if (TREE_CODE (init
) == DEFAULT_ARG
)
924 error ("invalid initializer for member function %qD", value
);
925 else if (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
)
927 if (integer_zerop (init
))
928 DECL_PURE_VIRTUAL_P (value
) = 1;
929 else if (error_operand_p (init
))
930 ; /* An error has already been reported. */
932 error ("invalid initializer for member function %qD",
937 gcc_assert (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
);
939 = cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (value
));
941 error_at (iloc
, "initializer specified for friend "
942 "function %qD", value
);
944 error_at (iloc
, "initializer specified for static "
945 "member function %qD", value
);
948 else if (TREE_CODE (value
) == FIELD_DECL
)
949 /* C++11 NSDMI, keep going. */;
950 else if (!VAR_P (value
))
954 /* Pass friend decls back. */
955 if ((TREE_CODE (value
) == FUNCTION_DECL
956 || TREE_CODE (value
) == TEMPLATE_DECL
)
957 && DECL_CONTEXT (value
) != current_class_type
)
960 /* Need to set this before push_template_decl. */
962 DECL_CONTEXT (value
) = current_class_type
;
964 if (processing_template_decl
&& VAR_OR_FUNCTION_DECL_P (value
))
966 value
= push_template_decl (value
);
967 if (error_operand_p (value
))
968 return error_mark_node
;
972 cplus_decl_attributes (&value
, attrlist
, 0);
974 if (init
&& DIRECT_LIST_INIT_P (init
))
975 flags
= LOOKUP_NORMAL
;
977 flags
= LOOKUP_IMPLICIT
;
979 switch (TREE_CODE (value
))
982 finish_static_data_member_decl (value
, init
, init_const_expr_p
,
983 asmspec_tree
, flags
);
988 error ("%<asm%> specifiers are not permitted on non-static data members");
989 if (DECL_INITIAL (value
) == error_mark_node
)
990 init
= error_mark_node
;
991 cp_finish_decl (value
, init
, /*init_const_expr_p=*/false,
993 DECL_IN_AGGR_P (value
) = 1;
998 set_user_assembler_name (value
, asmspec
);
1000 cp_finish_decl (value
,
1002 /*init_const_expr_p=*/false,
1003 asmspec_tree
, flags
);
1005 /* Pass friends back this way. */
1006 if (DECL_FRIEND_P (value
))
1007 return void_type_node
;
1009 DECL_IN_AGGR_P (value
) = 1;
1018 /* Like `grokfield', but for bitfields.
1019 WIDTH is the width of the bitfield, a constant expression.
1020 The other parameters are as for grokfield. */
1023 grokbitfield (const cp_declarator
*declarator
,
1024 cp_decl_specifier_seq
*declspecs
, tree width
, tree init
,
1027 tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
,
1028 init
!= NULL_TREE
, &attrlist
);
1030 if (value
== error_mark_node
)
1031 return NULL_TREE
; /* friends went bad. */
1033 tree type
= TREE_TYPE (value
);
1034 if (type
== error_mark_node
)
1037 /* Pass friendly classes back. */
1038 if (VOID_TYPE_P (value
))
1039 return void_type_node
;
1041 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
1042 && (INDIRECT_TYPE_P (type
) || !dependent_type_p (type
)))
1044 error_at (DECL_SOURCE_LOCATION (value
),
1045 "bit-field %qD with non-integral type %qT",
1047 return error_mark_node
;
1050 if (TREE_CODE (value
) == TYPE_DECL
)
1052 error_at (DECL_SOURCE_LOCATION (value
),
1053 "cannot declare %qD to be a bit-field type", value
);
1057 /* Usually, finish_struct_1 catches bitfields with invalid types.
1058 But, in the case of bitfields with function type, we confuse
1059 ourselves into thinking they are member functions, so we must
1061 if (TREE_CODE (value
) == FUNCTION_DECL
)
1063 error_at (DECL_SOURCE_LOCATION (value
),
1064 "cannot declare bit-field %qD with function type", value
);
1068 if (TYPE_WARN_IF_NOT_ALIGN (type
))
1070 error_at (DECL_SOURCE_LOCATION (value
), "cannot declare bit-field "
1071 "%qD with %<warn_if_not_aligned%> type", value
);
1075 if (DECL_IN_AGGR_P (value
))
1077 error ("%qD is already defined in the class %qT", value
,
1078 DECL_CONTEXT (value
));
1079 return void_type_node
;
1082 if (TREE_STATIC (value
))
1084 error_at (DECL_SOURCE_LOCATION (value
),
1085 "static member %qD cannot be a bit-field", value
);
1089 int flags
= LOOKUP_IMPLICIT
;
1090 if (init
&& DIRECT_LIST_INIT_P (init
))
1091 flags
= LOOKUP_NORMAL
;
1092 cp_finish_decl (value
, init
, false, NULL_TREE
, flags
);
1094 if (width
!= error_mark_node
)
1096 /* The width must be an integer type. */
1097 if (!type_dependent_expression_p (width
)
1098 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width
)))
1099 error ("width of bit-field %qD has non-integral type %qT", value
,
1103 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1104 check_bitfield_decl picks it from there later and sets DECL_SIZE
1106 DECL_BIT_FIELD_REPRESENTATIVE (value
) = width
;
1107 SET_DECL_C_BIT_FIELD (value
);
1111 DECL_IN_AGGR_P (value
) = 1;
1114 cplus_decl_attributes (&value
, attrlist
, /*flags=*/0);
1120 /* Returns true iff ATTR is an attribute which needs to be applied at
1121 instantiation time rather than template definition time. */
1124 is_late_template_attribute (tree attr
, tree decl
)
1126 tree name
= get_attribute_name (attr
);
1127 tree args
= TREE_VALUE (attr
);
1128 const struct attribute_spec
*spec
= lookup_attribute_spec (name
);
1132 /* Unknown attribute. */
1135 /* Attribute weak handling wants to write out assembly right away. */
1136 if (is_attribute_p ("weak", name
))
1139 /* Attributes used and unused are applied directly to typedefs for the
1140 benefit of maybe_warn_unused_local_typedefs. */
1141 if (TREE_CODE (decl
) == TYPE_DECL
1142 && (is_attribute_p ("unused", name
)
1143 || is_attribute_p ("used", name
)))
1146 /* Attribute tls_model wants to modify the symtab. */
1147 if (is_attribute_p ("tls_model", name
))
1150 /* #pragma omp declare simd attribute needs to be always deferred. */
1152 && is_attribute_p ("omp declare simd", name
))
1155 /* An attribute pack is clearly dependent. */
1156 if (args
&& PACK_EXPANSION_P (args
))
1159 /* If any of the arguments are dependent expressions, we can't evaluate
1160 the attribute until instantiation time. */
1161 for (arg
= args
; arg
; arg
= TREE_CHAIN (arg
))
1163 tree t
= TREE_VALUE (arg
);
1165 /* If the first attribute argument is an identifier, only consider
1166 second and following arguments. Attributes like mode, format,
1167 cleanup and several target specific attributes aren't late
1168 just because they have an IDENTIFIER_NODE as first argument. */
1169 if (arg
== args
&& attribute_takes_identifier_p (name
)
1170 && identifier_p (t
))
1173 if (value_dependent_expression_p (t
)
1174 || type_dependent_expression_p (t
))
1178 if (TREE_CODE (decl
) == TYPE_DECL
1180 || spec
->type_required
)
1182 tree type
= TYPE_P (decl
) ? decl
: TREE_TYPE (decl
);
1184 /* We can't apply any attributes to a completely unknown type until
1185 instantiation time. */
1186 enum tree_code code
= TREE_CODE (type
);
1187 if (code
== TEMPLATE_TYPE_PARM
1188 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
1189 || code
== TYPENAME_TYPE
)
1191 /* Also defer most attributes on dependent types. This is not
1192 necessary in all cases, but is the better default. */
1193 else if (dependent_type_p (type
)
1194 /* But some attributes specifically apply to templates. */
1195 && !is_attribute_p ("abi_tag", name
)
1196 && !is_attribute_p ("deprecated", name
)
1197 && !is_attribute_p ("visibility", name
))
1206 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1207 applied at instantiation time and return them. If IS_DEPENDENT is true,
1208 the declaration itself is dependent, so all attributes should be applied
1209 at instantiation time. */
1212 splice_template_attributes (tree
*attr_p
, tree decl
)
1215 tree late_attrs
= NULL_TREE
;
1216 tree
*q
= &late_attrs
;
1223 if (is_late_template_attribute (*p
, decl
))
1225 ATTR_IS_DEPENDENT (*p
) = 1;
1227 *p
= TREE_CHAIN (*p
);
1228 q
= &TREE_CHAIN (*q
);
1232 p
= &TREE_CHAIN (*p
);
1238 /* Remove any late attributes from the list in ATTR_P and attach them to
1242 save_template_attributes (tree
*attr_p
, tree
*decl_p
, int flags
)
1246 if (attr_p
&& *attr_p
== error_mark_node
)
1249 tree late_attrs
= splice_template_attributes (attr_p
, *decl_p
);
1253 if (DECL_P (*decl_p
))
1254 q
= &DECL_ATTRIBUTES (*decl_p
);
1256 q
= &TYPE_ATTRIBUTES (*decl_p
);
1258 tree old_attrs
= *q
;
1260 /* Merge the late attributes at the beginning with the attribute
1262 late_attrs
= merge_attributes (late_attrs
, *q
);
1263 if (*q
!= late_attrs
1264 && !DECL_P (*decl_p
)
1265 && !(flags
& ATTR_FLAG_TYPE_IN_PLACE
))
1267 if (!dependent_type_p (*decl_p
))
1268 *decl_p
= cp_build_type_attribute_variant (*decl_p
, late_attrs
);
1271 *decl_p
= build_variant_type_copy (*decl_p
);
1272 TYPE_ATTRIBUTES (*decl_p
) = late_attrs
;
1278 if (!DECL_P (*decl_p
) && *decl_p
== TYPE_MAIN_VARIANT (*decl_p
))
1280 /* We've added new attributes directly to the main variant, so
1281 now we need to update all of the other variants to include
1282 these new attributes. */
1284 for (variant
= TYPE_NEXT_VARIANT (*decl_p
); variant
;
1285 variant
= TYPE_NEXT_VARIANT (variant
))
1287 gcc_assert (TYPE_ATTRIBUTES (variant
) == old_attrs
);
1288 TYPE_ATTRIBUTES (variant
) = TYPE_ATTRIBUTES (*decl_p
);
1293 /* True if ATTRS contains any dependent attributes that affect type
1297 any_dependent_type_attributes_p (tree attrs
)
1299 for (tree a
= attrs
; a
; a
= TREE_CHAIN (a
))
1300 if (ATTR_IS_DEPENDENT (a
))
1302 const attribute_spec
*as
= lookup_attribute_spec (TREE_PURPOSE (a
));
1303 if (as
&& as
->affects_type_identity
)
1309 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1310 to a typedef which gives a previously unnamed class or enum a name for
1311 linkage purposes. */
1314 attributes_naming_typedef_ok (tree attrs
)
1316 for (; attrs
; attrs
= TREE_CHAIN (attrs
))
1318 tree name
= get_attribute_name (attrs
);
1319 if (is_attribute_p ("vector_size", name
))
1325 /* Like reconstruct_complex_type, but handle also template trees. */
1328 cp_reconstruct_complex_type (tree type
, tree bottom
)
1332 if (TYPE_PTR_P (type
))
1334 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1335 outer
= build_pointer_type_for_mode (inner
, TYPE_MODE (type
),
1336 TYPE_REF_CAN_ALIAS_ALL (type
));
1338 else if (TYPE_REF_P (type
))
1340 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1341 outer
= build_reference_type_for_mode (inner
, TYPE_MODE (type
),
1342 TYPE_REF_CAN_ALIAS_ALL (type
));
1344 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1346 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1347 outer
= build_cplus_array_type (inner
, TYPE_DOMAIN (type
));
1348 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1349 element type qualification will be handled by the recursive
1350 cp_reconstruct_complex_type call and cp_build_qualified_type
1351 for ARRAY_TYPEs changes the element type. */
1354 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
1356 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1357 outer
= build_function_type (inner
, TYPE_ARG_TYPES (type
));
1358 outer
= apply_memfn_quals (outer
, type_memfn_quals (type
));
1360 else if (TREE_CODE (type
) == METHOD_TYPE
)
1362 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1363 /* The build_method_type_directly() routine prepends 'this' to argument list,
1364 so we must compensate by getting rid of it. */
1366 = build_method_type_directly
1367 (class_of_this_parm (type
), inner
,
1368 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
1370 else if (TREE_CODE (type
) == OFFSET_TYPE
)
1372 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1373 outer
= build_offset_type (TYPE_OFFSET_BASETYPE (type
), inner
);
1378 if (TYPE_ATTRIBUTES (type
))
1379 outer
= cp_build_type_attribute_variant (outer
, TYPE_ATTRIBUTES (type
));
1380 outer
= cp_build_qualified_type (outer
, cp_type_quals (type
));
1381 outer
= cxx_copy_lang_qualifiers (outer
, type
);
1386 /* Replaces any constexpr expression that may be into the attributes
1387 arguments with their reduced value. */
1390 cp_check_const_attributes (tree attributes
)
1392 if (attributes
== error_mark_node
)
1396 for (attr
= attributes
; attr
; attr
= TREE_CHAIN (attr
))
1399 for (arg
= TREE_VALUE (attr
); arg
&& TREE_CODE (arg
) == TREE_LIST
;
1400 arg
= TREE_CHAIN (arg
))
1402 tree expr
= TREE_VALUE (arg
);
1404 TREE_VALUE (arg
) = fold_non_dependent_expr (expr
);
1409 /* Return true if TYPE is an OpenMP mappable type. */
1411 cp_omp_mappable_type (tree type
)
1413 /* Mappable type has to be complete. */
1414 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
1416 /* Arrays have mappable type if the elements have mappable type. */
1417 while (TREE_CODE (type
) == ARRAY_TYPE
)
1418 type
= TREE_TYPE (type
);
1419 /* A mappable type cannot contain virtual members. */
1420 if (CLASS_TYPE_P (type
) && CLASSTYPE_VTABLES (type
))
1422 /* All data members must be non-static. */
1423 if (CLASS_TYPE_P (type
))
1426 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1429 /* All fields must have mappable types. */
1430 else if (TREE_CODE (field
) == FIELD_DECL
1431 && !cp_omp_mappable_type (TREE_TYPE (field
)))
1437 /* Return the last pushed declaration for the symbol DECL or NULL
1438 when no such declaration exists. */
1441 find_last_decl (tree decl
)
1443 tree last_decl
= NULL_TREE
;
1445 if (tree name
= DECL_P (decl
) ? DECL_NAME (decl
) : NULL_TREE
)
1447 /* Look up the declaration in its scope. */
1448 tree pushed_scope
= NULL_TREE
;
1449 if (tree ctype
= DECL_CONTEXT (decl
))
1450 pushed_scope
= push_scope (ctype
);
1452 last_decl
= lookup_name (name
);
1455 pop_scope (pushed_scope
);
1457 /* The declaration may be a member conversion operator
1458 or a bunch of overfloads (handle the latter below). */
1459 if (last_decl
&& BASELINK_P (last_decl
))
1460 last_decl
= BASELINK_FUNCTIONS (last_decl
);
1466 if (DECL_P (last_decl
) || TREE_CODE (last_decl
) == OVERLOAD
)
1468 /* A set of overloads of the same function. */
1469 for (lkp_iterator
iter (last_decl
); iter
; ++iter
)
1471 if (TREE_CODE (*iter
) == OVERLOAD
)
1474 if (decls_match (decl
, *iter
, /*record_decls=*/false))
1483 /* Like decl_attributes, but handle C++ complexity. */
1486 cplus_decl_attributes (tree
*decl
, tree attributes
, int flags
)
1488 if (*decl
== NULL_TREE
|| *decl
== void_type_node
1489 || *decl
== error_mark_node
)
1492 /* Add implicit "omp declare target" attribute if requested. */
1493 if (scope_chain
->omp_declare_target_attribute
1495 && (TREE_STATIC (*decl
) || DECL_EXTERNAL (*decl
)))
1496 || TREE_CODE (*decl
) == FUNCTION_DECL
))
1499 && DECL_CLASS_SCOPE_P (*decl
))
1500 error ("%q+D static data member inside of declare target directive",
1502 else if (VAR_P (*decl
)
1503 && (processing_template_decl
1504 || !cp_omp_mappable_type (TREE_TYPE (*decl
))))
1505 attributes
= tree_cons (get_identifier ("omp declare target implicit"),
1506 NULL_TREE
, attributes
);
1508 attributes
= tree_cons (get_identifier ("omp declare target"),
1509 NULL_TREE
, attributes
);
1512 if (processing_template_decl
)
1514 if (check_for_bare_parameter_packs (attributes
))
1517 save_template_attributes (&attributes
, decl
, flags
);
1520 cp_check_const_attributes (attributes
);
1522 if (TREE_CODE (*decl
) == TEMPLATE_DECL
)
1523 decl
= &DECL_TEMPLATE_RESULT (*decl
);
1525 if (TREE_TYPE (*decl
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl
)))
1528 = decl_attributes (decl
, attributes
, flags
| ATTR_FLAG_FUNCTION_NEXT
);
1529 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl
)),
1534 tree last_decl
= find_last_decl (*decl
);
1535 decl_attributes (decl
, attributes
, flags
, last_decl
);
1538 if (TREE_CODE (*decl
) == TYPE_DECL
)
1539 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl
), TREE_TYPE (*decl
));
1541 /* Propagate deprecation out to the template. */
1542 if (TREE_DEPRECATED (*decl
))
1543 if (tree ti
= get_template_info (*decl
))
1545 tree tmpl
= TI_TEMPLATE (ti
);
1546 tree pattern
= (TYPE_P (*decl
) ? TREE_TYPE (tmpl
)
1547 : DECL_TEMPLATE_RESULT (tmpl
));
1548 if (*decl
== pattern
)
1549 TREE_DEPRECATED (tmpl
) = true;
1553 /* Walks through the namespace- or function-scope anonymous union
1554 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1555 Returns one of the fields for use in the mangled name. */
1558 build_anon_union_vars (tree type
, tree object
)
1560 tree main_decl
= NULL_TREE
;
1563 /* Rather than write the code to handle the non-union case,
1564 just give an error. */
1565 if (TREE_CODE (type
) != UNION_TYPE
)
1567 error ("anonymous struct not inside named type");
1568 return error_mark_node
;
1571 for (field
= TYPE_FIELDS (type
);
1573 field
= DECL_CHAIN (field
))
1578 if (DECL_ARTIFICIAL (field
))
1580 if (TREE_CODE (field
) != FIELD_DECL
)
1582 permerror (DECL_SOURCE_LOCATION (field
),
1583 "%q#D invalid; an anonymous union can only "
1584 "have non-static data members", field
);
1588 if (TREE_PRIVATE (field
))
1589 permerror (DECL_SOURCE_LOCATION (field
),
1590 "private member %q#D in anonymous union", field
);
1591 else if (TREE_PROTECTED (field
))
1592 permerror (DECL_SOURCE_LOCATION (field
),
1593 "protected member %q#D in anonymous union", field
);
1595 if (processing_template_decl
)
1596 ref
= build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
, object
,
1597 DECL_NAME (field
), NULL_TREE
);
1599 ref
= build_class_member_access_expr (object
, field
, NULL_TREE
,
1600 false, tf_warning_or_error
);
1602 if (DECL_NAME (field
))
1606 decl
= build_decl (input_location
,
1607 VAR_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
1608 DECL_ANON_UNION_VAR_P (decl
) = 1;
1609 DECL_ARTIFICIAL (decl
) = 1;
1611 base
= get_base_address (object
);
1612 TREE_PUBLIC (decl
) = TREE_PUBLIC (base
);
1613 TREE_STATIC (decl
) = TREE_STATIC (base
);
1614 DECL_EXTERNAL (decl
) = DECL_EXTERNAL (base
);
1616 SET_DECL_VALUE_EXPR (decl
, ref
);
1617 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1619 decl
= pushdecl (decl
);
1621 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1622 decl
= build_anon_union_vars (TREE_TYPE (field
), ref
);
1626 if (main_decl
== NULL_TREE
)
1633 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1634 anonymous union, then all members must be laid out together. PUBLIC_P
1635 is nonzero if this union is not declared static. */
1638 finish_anon_union (tree anon_union_decl
)
1644 if (anon_union_decl
== error_mark_node
)
1647 type
= TREE_TYPE (anon_union_decl
);
1648 public_p
= TREE_PUBLIC (anon_union_decl
);
1650 /* The VAR_DECL's context is the same as the TYPE's context. */
1651 DECL_CONTEXT (anon_union_decl
) = DECL_CONTEXT (TYPE_NAME (type
));
1653 if (TYPE_FIELDS (type
) == NULL_TREE
)
1658 error ("namespace-scope anonymous aggregates must be static");
1662 main_decl
= build_anon_union_vars (type
, anon_union_decl
);
1663 if (main_decl
== error_mark_node
)
1665 if (main_decl
== NULL_TREE
)
1667 pedwarn (input_location
, 0, "anonymous union with no members");
1671 if (!processing_template_decl
)
1673 /* Use main_decl to set the mangled name. */
1674 DECL_NAME (anon_union_decl
) = DECL_NAME (main_decl
);
1675 maybe_commonize_var (anon_union_decl
);
1676 if (TREE_STATIC (anon_union_decl
) || DECL_EXTERNAL (anon_union_decl
))
1678 if (DECL_DISCRIMINATOR_P (anon_union_decl
))
1679 determine_local_discriminator (anon_union_decl
);
1680 mangle_decl (anon_union_decl
);
1682 DECL_NAME (anon_union_decl
) = NULL_TREE
;
1685 pushdecl (anon_union_decl
);
1686 cp_finish_decl (anon_union_decl
, NULL_TREE
, false, NULL_TREE
, 0);
1689 /* Auxiliary functions to make type signatures for
1690 `operator new' and `operator delete' correspond to
1691 what compiler will be expecting. */
1694 coerce_new_type (tree type
, location_t loc
)
1697 tree args
= TYPE_ARG_TYPES (type
);
1699 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1701 if (!same_type_p (TREE_TYPE (type
), ptr_type_node
))
1704 error_at (loc
, "%<operator new%> must return type %qT",
1708 if (args
&& args
!= void_list_node
)
1710 if (TREE_PURPOSE (args
))
1712 /* [basic.stc.dynamic.allocation]
1714 The first parameter shall not have an associated default
1716 error_at (loc
, "the first parameter of %<operator new%> cannot "
1717 "have a default argument");
1718 /* Throw away the default argument. */
1719 TREE_PURPOSE (args
) = NULL_TREE
;
1722 if (!same_type_p (TREE_VALUE (args
), size_type_node
))
1725 args
= TREE_CHAIN (args
);
1732 permerror (loc
, "%<operator new%> takes type %<size_t%> (%qT) "
1733 "as first parameter", size_type_node
);
1738 args
= tree_cons (NULL_TREE
, size_type_node
, args
);
1741 type
= (cxx_copy_lang_qualifiers
1742 (build_function_type (ptr_type_node
, args
),
1751 coerce_delete_type (tree decl
, location_t loc
)
1754 tree type
= TREE_TYPE (decl
);
1755 tree args
= TYPE_ARG_TYPES (type
);
1757 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1759 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
1762 error_at (loc
, "%<operator delete%> must return type %qT",
1766 tree ptrtype
= ptr_type_node
;
1767 if (destroying_delete_p (decl
))
1769 if (DECL_CLASS_SCOPE_P (decl
))
1770 /* If the function is a destroying operator delete declared in class
1771 type C, the type of its first parameter shall be C*. */
1772 ptrtype
= build_pointer_type (DECL_CONTEXT (decl
));
1774 /* A destroying operator delete shall be a class member function named
1776 error_at (loc
, "destroying operator delete must be a member function");
1777 const ovl_op_info_t
*op
= IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl
));
1778 if (op
->flags
& OVL_OP_FLAG_VEC
)
1779 error_at (loc
, "operator delete[] cannot be a destroying delete");
1780 if (!usual_deallocation_fn_p (decl
))
1781 error_at (loc
, "destroying operator delete must be a usual "
1782 "deallocation function");
1785 if (!args
|| args
== void_list_node
1786 || !same_type_p (TREE_VALUE (args
), ptrtype
))
1789 if (args
&& args
!= void_list_node
)
1790 args
= TREE_CHAIN (args
);
1791 error_at (loc
, "%<operator delete%> takes type %qT as first parameter",
1797 args
= tree_cons (NULL_TREE
, ptrtype
, args
);
1800 type
= (cxx_copy_lang_qualifiers
1801 (build_function_type (void_type_node
, args
),
1807 TREE_TYPE (decl
) = type
;
1810 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1811 and mark them as needed. */
1814 mark_vtable_entries (tree decl
)
1817 unsigned HOST_WIDE_INT idx
;
1819 /* It's OK for the vtable to refer to deprecated virtual functions. */
1820 warning_sentinel
w(warn_deprecated_decl
);
1822 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
1827 STRIP_NOPS (fnaddr
);
1829 if (TREE_CODE (fnaddr
) != ADDR_EXPR
1830 && TREE_CODE (fnaddr
) != FDESC_EXPR
)
1831 /* This entry is an offset: a virtual base class offset, a
1832 virtual call offset, an RTTI offset, etc. */
1835 fn
= TREE_OPERAND (fnaddr
, 0);
1836 TREE_ADDRESSABLE (fn
) = 1;
1837 /* When we don't have vcall offsets, we output thunks whenever
1838 we output the vtables that contain them. With vcall offsets,
1839 we know all the thunks we'll need when we emit a virtual
1840 function, so we emit the thunks there instead. */
1841 if (DECL_THUNK_P (fn
))
1842 use_thunk (fn
, /*emit_p=*/0);
1843 /* Set the location, as marking the function could cause
1844 instantiation. We do not need to preserve the incoming
1845 location, as we're called from c_parse_final_cleanups, which
1846 takes care of that. */
1847 input_location
= DECL_SOURCE_LOCATION (fn
);
1852 /* Adjust the TLS model on variable DECL if need be, typically after
1853 the linkage of DECL has been modified. */
1856 adjust_var_decl_tls_model (tree decl
)
1858 if (CP_DECL_THREAD_LOCAL_P (decl
)
1859 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl
)))
1860 set_decl_tls_model (decl
, decl_default_tls_model (decl
));
1863 /* Set DECL up to have the closest approximation of "initialized common"
1864 linkage available. */
1867 comdat_linkage (tree decl
)
1870 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1871 else if (TREE_CODE (decl
) == FUNCTION_DECL
1872 || (VAR_P (decl
) && DECL_ARTIFICIAL (decl
)))
1873 /* We can just emit function and compiler-generated variables
1874 statically; having multiple copies is (for the most part) only
1877 There are two correctness issues, however: the address of a
1878 template instantiation with external linkage should be the
1879 same, independent of what translation unit asks for the
1880 address, and this will not hold when we emit multiple copies of
1881 the function. However, there's little else we can do.
1883 Also, by default, the typeinfo implementation assumes that
1884 there will be only one copy of the string used as the name for
1885 each type. Therefore, if weak symbols are unavailable, the
1886 run-time library should perform a more conservative check; it
1887 should perform a string comparison, rather than an address
1889 TREE_PUBLIC (decl
) = 0;
1892 /* Static data member template instantiations, however, cannot
1893 have multiple copies. */
1894 if (DECL_INITIAL (decl
) == 0
1895 || DECL_INITIAL (decl
) == error_mark_node
)
1896 DECL_COMMON (decl
) = 1;
1897 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
1899 DECL_COMMON (decl
) = 1;
1900 DECL_INITIAL (decl
) = error_mark_node
;
1902 else if (!DECL_EXPLICIT_INSTANTIATION (decl
))
1904 /* We can't do anything useful; leave vars for explicit
1906 DECL_EXTERNAL (decl
) = 1;
1907 DECL_NOT_REALLY_EXTERN (decl
) = 0;
1911 if (TREE_PUBLIC (decl
))
1912 DECL_COMDAT (decl
) = 1;
1915 adjust_var_decl_tls_model (decl
);
1918 /* For win32 we also want to put explicit instantiations in
1919 linkonce sections, so that they will be merged with implicit
1920 instantiations; otherwise we get duplicate symbol errors.
1921 For Darwin we do not want explicit instantiations to be
1925 maybe_make_one_only (tree decl
)
1927 /* We used to say that this was not necessary on targets that support weak
1928 symbols, because the implicit instantiations will defer to the explicit
1929 one. However, that's not actually the case in SVR4; a strong definition
1930 after a weak one is an error. Also, not making explicit
1931 instantiations one_only means that we can end up with two copies of
1932 some template instantiations. */
1936 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1937 we can get away with not emitting them if they aren't used. We need
1938 to for variables so that cp_finish_decl will update their linkage,
1939 because their DECL_INITIAL may not have been set properly yet. */
1941 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1942 || (! DECL_EXPLICIT_INSTANTIATION (decl
)
1943 && ! DECL_TEMPLATE_SPECIALIZATION (decl
)))
1945 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1949 varpool_node
*node
= varpool_node::get_create (decl
);
1950 DECL_COMDAT (decl
) = 1;
1951 /* Mark it needed so we don't forget to emit it. */
1952 node
->forced_by_abi
= true;
1953 TREE_USED (decl
) = 1;
1955 adjust_var_decl_tls_model (decl
);
1960 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1961 This predicate will give the right answer during parsing of the
1962 function, which other tests may not. */
1965 vague_linkage_p (tree decl
)
1967 if (!TREE_PUBLIC (decl
))
1969 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
1970 maybe-in-charge 'tor variants; in that case we need to check one of
1971 the "clones" for the real linkage. But only in that case; before
1972 maybe_clone_body we haven't yet copied the linkage to the clones. */
1973 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl
)
1974 && !DECL_ABSTRACT_P (decl
)
1975 && DECL_CHAIN (decl
)
1976 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)))
1977 return vague_linkage_p (DECL_CHAIN (decl
));
1979 gcc_checking_assert (!DECL_COMDAT (decl
));
1982 /* Unfortunately, import_export_decl has not always been called
1983 before the function is processed, so we cannot simply check
1985 if (DECL_COMDAT (decl
)
1986 || (TREE_CODE (decl
) == FUNCTION_DECL
1987 && DECL_DECLARED_INLINE_P (decl
))
1988 || (DECL_LANG_SPECIFIC (decl
)
1989 && DECL_TEMPLATE_INSTANTIATION (decl
))
1990 || (VAR_P (decl
) && DECL_INLINE_VAR_P (decl
)))
1992 else if (DECL_FUNCTION_SCOPE_P (decl
))
1993 /* A local static in an inline effectively has vague linkage. */
1994 return (TREE_STATIC (decl
)
1995 && vague_linkage_p (DECL_CONTEXT (decl
)));
2000 /* Determine whether or not we want to specifically import or export CTYPE,
2001 using various heuristics. */
2004 import_export_class (tree ctype
)
2006 /* -1 for imported, 1 for exported. */
2007 int import_export
= 0;
2009 /* It only makes sense to call this function at EOF. The reason is
2010 that this function looks at whether or not the first non-inline
2011 non-abstract virtual member function has been defined in this
2012 translation unit. But, we can't possibly know that until we've
2013 seen the entire translation unit. */
2014 gcc_assert (at_eof
);
2016 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2019 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2020 we will have CLASSTYPE_INTERFACE_ONLY set but not
2021 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2022 heuristic because someone will supply a #pragma implementation
2023 elsewhere, and deducing it here would produce a conflict. */
2024 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
2027 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
2029 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
2031 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
2032 && !flag_implicit_templates
)
2033 /* For a template class, without -fimplicit-templates, check the
2034 repository. If the virtual table is assigned to this
2035 translation unit, then export the class; otherwise, import
2037 import_export
= repo_export_class_p (ctype
) ? 1 : -1;
2038 else if (TYPE_POLYMORPHIC_P (ctype
))
2040 /* The ABI specifies that the virtual table and associated
2041 information are emitted with the key method, if any. */
2042 tree method
= CLASSTYPE_KEY_METHOD (ctype
);
2043 /* If weak symbol support is not available, then we must be
2044 careful not to emit the vtable when the key function is
2045 inline. An inline function can be defined in multiple
2046 translation units. If we were to emit the vtable in each
2047 translation unit containing a definition, we would get
2048 multiple definition errors at link-time. */
2049 if (method
&& (flag_weak
|| ! DECL_DECLARED_INLINE_P (method
)))
2050 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
2053 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2054 a definition anywhere else. */
2055 if (MULTIPLE_SYMBOL_SPACES
&& import_export
== -1)
2058 /* Allow back ends the chance to overrule the decision. */
2059 if (targetm
.cxx
.import_export_class
)
2060 import_export
= targetm
.cxx
.import_export_class (ctype
, import_export
);
2064 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
2065 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
2069 /* Return true if VAR has already been provided to the back end; in that
2070 case VAR should not be modified further by the front end. */
2072 var_finalized_p (tree var
)
2074 return varpool_node::get_create (var
)->definition
;
2077 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2078 must be emitted in this translation unit. Mark it as such. */
2081 mark_needed (tree decl
)
2083 TREE_USED (decl
) = 1;
2084 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2086 /* Extern inline functions don't become needed when referenced.
2087 If we know a method will be emitted in other TU and no new
2088 functions can be marked reachable, just use the external
2090 struct cgraph_node
*node
= cgraph_node::get_create (decl
);
2091 node
->forced_by_abi
= true;
2093 /* #pragma interface and -frepo code can call mark_needed for
2094 maybe-in-charge 'tors; mark the clones as well. */
2096 FOR_EACH_CLONE (clone
, decl
)
2097 mark_needed (clone
);
2099 else if (VAR_P (decl
))
2101 varpool_node
*node
= varpool_node::get_create (decl
);
2102 /* C++ frontend use mark_decl_references to force COMDAT variables
2103 to be output that might appear dead otherwise. */
2104 node
->forced_by_abi
= true;
2108 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2109 returns true if a definition of this entity should be provided in
2110 this object file. Callers use this function to determine whether
2111 or not to let the back end know that a definition of DECL is
2112 available in this translation unit. */
2115 decl_needed_p (tree decl
)
2117 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2118 /* This function should only be called at the end of the translation
2119 unit. We cannot be sure of whether or not something will be
2120 COMDAT until that point. */
2121 gcc_assert (at_eof
);
2123 /* All entities with external linkage that are not COMDAT/EXTERN should be
2124 emitted; they may be referred to from other object files. */
2125 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_REALLY_EXTERN (decl
))
2127 /* Functions marked "dllexport" must be emitted so that they are
2128 visible to other DLLs. */
2129 if (flag_keep_inline_dllexport
2130 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))
2133 /* When not optimizing, do not bother to produce definitions for extern
2135 if (DECL_REALLY_EXTERN (decl
)
2136 && ((TREE_CODE (decl
) != FUNCTION_DECL
2138 || (TREE_CODE (decl
) == FUNCTION_DECL
2139 && !opt_for_fn (decl
, optimize
)))
2140 && !lookup_attribute ("always_inline", decl
))
2143 /* If this entity was used, let the back end see it; it will decide
2144 whether or not to emit it into the object file. */
2145 if (TREE_USED (decl
))
2148 /* Virtual functions might be needed for devirtualization. */
2149 if (flag_devirtualize
2150 && TREE_CODE (decl
) == FUNCTION_DECL
2151 && DECL_VIRTUAL_P (decl
))
2154 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2155 reference to DECL might cause it to be emitted later. */
2159 /* If necessary, write out the vtables for the dynamic class CTYPE.
2160 Returns true if any vtables were emitted. */
2163 maybe_emit_vtables (tree ctype
)
2168 varpool_node
*current
= NULL
, *last
= NULL
;
2170 /* If the vtables for this class have already been emitted there is
2171 nothing more to do. */
2172 primary_vtbl
= CLASSTYPE_VTABLES (ctype
);
2173 if (var_finalized_p (primary_vtbl
))
2175 /* Ignore dummy vtables made by get_vtable_decl. */
2176 if (TREE_TYPE (primary_vtbl
) == void_type_node
)
2179 /* On some targets, we cannot determine the key method until the end
2180 of the translation unit -- which is when this function is
2182 if (!targetm
.cxx
.key_method_may_be_inline ())
2183 determine_key_method (ctype
);
2185 /* See if any of the vtables are needed. */
2186 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2188 import_export_decl (vtbl
);
2189 if (DECL_NOT_REALLY_EXTERN (vtbl
) && decl_needed_p (vtbl
))
2194 /* If the references to this class' vtables are optimized away,
2195 still emit the appropriate debugging information. See
2197 if (DECL_COMDAT (primary_vtbl
)
2198 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
2199 note_debug_info_needed (ctype
);
2203 /* The ABI requires that we emit all of the vtables if we emit any
2205 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2207 /* Mark entities references from the virtual table as used. */
2208 mark_vtable_entries (vtbl
);
2210 if (TREE_TYPE (DECL_INITIAL (vtbl
)) == 0)
2212 vec
<tree
, va_gc
> *cleanups
= NULL
;
2213 tree expr
= store_init_value (vtbl
, DECL_INITIAL (vtbl
), &cleanups
,
2216 /* It had better be all done at compile-time. */
2217 gcc_assert (!expr
&& !cleanups
);
2221 DECL_EXTERNAL (vtbl
) = 0;
2222 rest_of_decl_compilation (vtbl
, 1, 1);
2224 /* Because we're only doing syntax-checking, we'll never end up
2225 actually marking the variable as written. */
2226 if (flag_syntax_only
)
2227 TREE_ASM_WRITTEN (vtbl
) = 1;
2228 else if (DECL_ONE_ONLY (vtbl
))
2230 current
= varpool_node::get_create (vtbl
);
2232 current
->add_to_same_comdat_group (last
);
2237 /* For abstract classes, the destructor has been removed from the
2238 vtable (in class.c's build_vtbl_initializer). For a compiler-
2239 generated destructor, it hence might not have been generated in
2240 this translation unit - and with '#pragma interface' it might
2241 never get generated. */
2242 if (CLASSTYPE_PURE_VIRTUALS (ctype
)
2243 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
)
2244 && !CLASSTYPE_LAZY_DESTRUCTOR (ctype
)
2245 && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype
)))
2246 note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype
));
2248 /* Since we're writing out the vtable here, also write the debug
2250 note_debug_info_needed (ctype
);
2255 /* A special return value from type_visibility meaning internal
2258 enum { VISIBILITY_ANON
= VISIBILITY_INTERNAL
+1 };
2260 static int expr_visibility (tree
);
2261 static int type_visibility (tree
);
2263 /* walk_tree helper function for type_visibility. */
2266 min_vis_r (tree
*tp
, int *walk_subtrees
, void *data
)
2268 int *vis_p
= (int *)data
;
2273 else if (OVERLOAD_TYPE_P (*tp
)
2274 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp
)))
2276 *vis_p
= VISIBILITY_ANON
;
2279 else if (CLASS_TYPE_P (*tp
)
2280 && CLASSTYPE_VISIBILITY (*tp
) > *vis_p
)
2281 *vis_p
= CLASSTYPE_VISIBILITY (*tp
);
2282 else if (TREE_CODE (*tp
) == ARRAY_TYPE
2283 && uses_template_parms (TYPE_DOMAIN (*tp
)))
2285 int evis
= expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp
)));
2292 /* walk_tree helper function for expr_visibility. */
2295 min_vis_expr_r (tree
*tp
, int */
*walk_subtrees*/
, void *data
)
2297 int *vis_p
= (int *)data
;
2298 int tpvis
= VISIBILITY_DEFAULT
;
2300 switch (TREE_CODE (*tp
))
2303 case IMPLICIT_CONV_EXPR
:
2304 case STATIC_CAST_EXPR
:
2305 case REINTERPRET_CAST_EXPR
:
2306 case CONST_CAST_EXPR
:
2307 case DYNAMIC_CAST_EXPR
:
2311 tpvis
= type_visibility (TREE_TYPE (*tp
));
2316 if (! TREE_PUBLIC (*tp
))
2317 tpvis
= VISIBILITY_ANON
;
2319 tpvis
= DECL_VISIBILITY (*tp
);
2332 /* Returns the visibility of TYPE, which is the minimum visibility of its
2336 type_visibility (tree type
)
2338 int vis
= VISIBILITY_DEFAULT
;
2339 cp_walk_tree_without_duplicates (&type
, min_vis_r
, &vis
);
2343 /* Returns the visibility of an expression EXPR that appears in the signature
2344 of a function template, which is the minimum visibility of names that appear
2348 expr_visibility (tree expr
)
2350 int vis
= VISIBILITY_DEFAULT
;
2351 cp_walk_tree_without_duplicates (&expr
, min_vis_expr_r
, &vis
);
2355 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2356 specified (or if VISIBILITY is static). If TMPL is true, this
2357 constraint is for a template argument, and takes precedence
2358 over explicitly-specified visibility on the template. */
2361 constrain_visibility (tree decl
, int visibility
, bool tmpl
)
2363 if (visibility
== VISIBILITY_ANON
)
2365 /* extern "C" declarations aren't affected by the anonymous
2367 if (!DECL_EXTERN_C_P (decl
))
2369 TREE_PUBLIC (decl
) = 0;
2370 DECL_WEAK (decl
) = 0;
2371 DECL_COMMON (decl
) = 0;
2372 DECL_COMDAT (decl
) = false;
2373 if (VAR_OR_FUNCTION_DECL_P (decl
))
2375 struct symtab_node
*snode
= symtab_node::get (decl
);
2378 snode
->set_comdat_group (NULL
);
2380 DECL_INTERFACE_KNOWN (decl
) = 1;
2381 if (DECL_LANG_SPECIFIC (decl
))
2382 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2385 else if (visibility
> DECL_VISIBILITY (decl
)
2386 && (tmpl
|| !DECL_VISIBILITY_SPECIFIED (decl
)))
2388 DECL_VISIBILITY (decl
) = (enum symbol_visibility
) visibility
;
2389 /* This visibility was not specified. */
2390 DECL_VISIBILITY_SPECIFIED (decl
) = false;
2394 /* Constrain the visibility of DECL based on the visibility of its template
2398 constrain_visibility_for_template (tree decl
, tree targs
)
2400 /* If this is a template instantiation, check the innermost
2401 template args for visibility constraints. The outer template
2402 args are covered by the class check. */
2403 tree args
= INNERMOST_TEMPLATE_ARGS (targs
);
2405 for (i
= TREE_VEC_LENGTH (args
); i
> 0; --i
)
2409 tree arg
= TREE_VEC_ELT (args
, i
-1);
2411 vis
= type_visibility (arg
);
2413 vis
= expr_visibility (arg
);
2415 constrain_visibility (decl
, vis
, true);
2419 /* Like c_determine_visibility, but with additional C++-specific
2422 Function-scope entities can rely on the function's visibility because
2423 it is set in start_preparsed_function.
2425 Class-scope entities cannot rely on the class's visibility until the end
2426 of the enclosing class definition.
2428 Note that because namespaces have multiple independent definitions,
2429 namespace visibility is handled elsewhere using the #pragma visibility
2430 machinery rather than by decorating the namespace declaration.
2432 The goal is for constraints from the type to give a diagnostic, and
2433 other constraints to be applied silently. */
2436 determine_visibility (tree decl
)
2438 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2440 /* Only relevant for names with external linkage. */
2441 if (!TREE_PUBLIC (decl
))
2444 /* Cloned constructors and destructors get the same visibility as
2445 the underlying function. That should be set up in
2446 maybe_clone_body. */
2447 gcc_assert (!DECL_CLONED_FUNCTION_P (decl
));
2449 bool orig_visibility_specified
= DECL_VISIBILITY_SPECIFIED (decl
);
2450 enum symbol_visibility orig_visibility
= DECL_VISIBILITY (decl
);
2452 /* The decl may be a template instantiation, which could influence
2454 tree template_decl
= NULL_TREE
;
2455 if (TREE_CODE (decl
) == TYPE_DECL
)
2457 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
2459 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)))
2460 template_decl
= decl
;
2462 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
2463 template_decl
= decl
;
2465 else if (DECL_LANG_SPECIFIC (decl
) && DECL_USE_TEMPLATE (decl
))
2466 template_decl
= decl
;
2468 /* If DECL is a member of a class, visibility specifiers on the
2469 class can influence the visibility of the DECL. */
2470 tree class_type
= NULL_TREE
;
2471 if (DECL_CLASS_SCOPE_P (decl
))
2472 class_type
= DECL_CONTEXT (decl
);
2475 /* Not a class member. */
2477 /* Virtual tables have DECL_CONTEXT set to their associated class,
2478 so they are automatically handled above. */
2479 gcc_assert (!VAR_P (decl
)
2480 || !DECL_VTABLE_OR_VTT_P (decl
));
2482 if (DECL_FUNCTION_SCOPE_P (decl
) && ! DECL_VISIBILITY_SPECIFIED (decl
))
2484 /* Local statics and classes get the visibility of their
2485 containing function by default, except that
2486 -fvisibility-inlines-hidden doesn't affect them. */
2487 tree fn
= DECL_CONTEXT (decl
);
2488 if (DECL_VISIBILITY_SPECIFIED (fn
))
2490 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2491 DECL_VISIBILITY_SPECIFIED (decl
) =
2492 DECL_VISIBILITY_SPECIFIED (fn
);
2496 if (DECL_CLASS_SCOPE_P (fn
))
2497 determine_visibility_from_class (decl
, DECL_CONTEXT (fn
));
2498 else if (determine_hidden_inline (fn
))
2500 DECL_VISIBILITY (decl
) = default_visibility
;
2501 DECL_VISIBILITY_SPECIFIED (decl
) =
2502 visibility_options
.inpragma
;
2506 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2507 DECL_VISIBILITY_SPECIFIED (decl
) =
2508 DECL_VISIBILITY_SPECIFIED (fn
);
2512 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2513 but have no TEMPLATE_INFO, so don't try to check it. */
2514 template_decl
= NULL_TREE
;
2516 else if (VAR_P (decl
) && DECL_TINFO_P (decl
)
2517 && flag_visibility_ms_compat
)
2519 /* Under -fvisibility-ms-compat, types are visible by default,
2520 even though their contents aren't. */
2521 tree underlying_type
= TREE_TYPE (DECL_NAME (decl
));
2522 int underlying_vis
= type_visibility (underlying_type
);
2523 if (underlying_vis
== VISIBILITY_ANON
2524 || (CLASS_TYPE_P (underlying_type
)
2525 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type
)))
2526 constrain_visibility (decl
, underlying_vis
, false);
2528 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
2530 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2532 /* tinfo visibility is based on the type it's for. */
2533 constrain_visibility
2534 (decl
, type_visibility (TREE_TYPE (DECL_NAME (decl
))), false);
2536 /* Give the target a chance to override the visibility associated
2538 if (TREE_PUBLIC (decl
)
2539 && !DECL_REALLY_EXTERN (decl
)
2540 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl
)))
2541 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl
))))
2542 targetm
.cxx
.determine_class_data_visibility (decl
);
2544 else if (template_decl
)
2545 /* Template instantiations and specializations get visibility based
2546 on their template unless they override it with an attribute. */;
2547 else if (! DECL_VISIBILITY_SPECIFIED (decl
))
2549 if (determine_hidden_inline (decl
))
2550 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2553 /* Set default visibility to whatever the user supplied with
2554 #pragma GCC visibility or a namespace visibility attribute. */
2555 DECL_VISIBILITY (decl
) = default_visibility
;
2556 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
2563 /* If the specialization doesn't specify visibility, use the
2564 visibility from the template. */
2565 tree tinfo
= get_template_info (template_decl
);
2566 tree args
= TI_ARGS (tinfo
);
2567 tree attribs
= (TREE_CODE (decl
) == TYPE_DECL
2568 ? TYPE_ATTRIBUTES (TREE_TYPE (decl
))
2569 : DECL_ATTRIBUTES (decl
));
2571 if (args
!= error_mark_node
)
2573 tree pattern
= DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo
));
2575 if (!DECL_VISIBILITY_SPECIFIED (decl
))
2577 if (!DECL_VISIBILITY_SPECIFIED (pattern
)
2578 && determine_hidden_inline (decl
))
2579 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2582 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (pattern
);
2583 DECL_VISIBILITY_SPECIFIED (decl
)
2584 = DECL_VISIBILITY_SPECIFIED (pattern
);
2589 /* Template argument visibility outweighs #pragma or namespace
2590 visibility, but not an explicit attribute. */
2591 && !lookup_attribute ("visibility", attribs
))
2593 int depth
= TMPL_ARGS_DEPTH (args
);
2594 if (DECL_VISIBILITY_SPECIFIED (decl
))
2596 /* A class template member with explicit visibility
2597 overrides the class visibility, so we need to apply
2598 all the levels of template args directly. */
2600 for (i
= 1; i
<= depth
; ++i
)
2602 tree lev
= TMPL_ARGS_LEVEL (args
, i
);
2603 constrain_visibility_for_template (decl
, lev
);
2606 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
2607 /* Limit visibility based on its template arguments. */
2608 constrain_visibility_for_template (decl
, args
);
2614 determine_visibility_from_class (decl
, class_type
);
2616 if (decl_anon_ns_mem_p (decl
))
2617 /* Names in an anonymous namespace get internal linkage.
2618 This might change once we implement export. */
2619 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2620 else if (TREE_CODE (decl
) != TYPE_DECL
)
2622 /* Propagate anonymity from type to decl. */
2623 int tvis
= type_visibility (TREE_TYPE (decl
));
2624 if (tvis
== VISIBILITY_ANON
2625 || ! DECL_VISIBILITY_SPECIFIED (decl
))
2626 constrain_visibility (decl
, tvis
, false);
2628 else if (no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/true))
2629 /* DR 757: A type without linkage shall not be used as the type of a
2630 variable or function with linkage, unless
2631 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2632 o the variable or function is not used (3.2 [basic.def.odr]) or is
2633 defined in the same translation unit.
2635 Since non-extern "C" decls need to be defined in the same
2636 translation unit, we can make the type internal. */
2637 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2639 /* If visibility changed and DECL already has DECL_RTL, ensure
2640 symbol flags are updated. */
2641 if ((DECL_VISIBILITY (decl
) != orig_visibility
2642 || DECL_VISIBILITY_SPECIFIED (decl
) != orig_visibility_specified
)
2643 && ((VAR_P (decl
) && TREE_STATIC (decl
))
2644 || TREE_CODE (decl
) == FUNCTION_DECL
)
2645 && DECL_RTL_SET_P (decl
))
2646 make_decl_rtl (decl
);
2649 /* By default, static data members and function members receive
2650 the visibility of their containing class. */
2653 determine_visibility_from_class (tree decl
, tree class_type
)
2655 if (DECL_VISIBILITY_SPECIFIED (decl
))
2658 if (determine_hidden_inline (decl
))
2659 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2662 /* Default to the class visibility. */
2663 DECL_VISIBILITY (decl
) = CLASSTYPE_VISIBILITY (class_type
);
2664 DECL_VISIBILITY_SPECIFIED (decl
)
2665 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type
);
2668 /* Give the target a chance to override the visibility associated
2671 && (DECL_TINFO_P (decl
)
2672 || (DECL_VTABLE_OR_VTT_P (decl
)
2673 /* Construction virtual tables are not exported because
2674 they cannot be referred to from other object files;
2675 their name is not standardized by the ABI. */
2676 && !DECL_CONSTRUCTION_VTABLE_P (decl
)))
2677 && TREE_PUBLIC (decl
)
2678 && !DECL_REALLY_EXTERN (decl
)
2679 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type
))
2680 targetm
.cxx
.determine_class_data_visibility (decl
);
2683 /* Returns true iff DECL is an inline that should get hidden visibility
2684 because of -fvisibility-inlines-hidden. */
2687 determine_hidden_inline (tree decl
)
2689 return (visibility_options
.inlines_hidden
2690 /* Don't do this for inline templates; specializations might not be
2691 inline, and we don't want them to inherit the hidden
2692 visibility. We'll set it here for all inline instantiations. */
2693 && !processing_template_decl
2694 && TREE_CODE (decl
) == FUNCTION_DECL
2695 && DECL_DECLARED_INLINE_P (decl
)
2696 && (! DECL_LANG_SPECIFIC (decl
)
2697 || ! DECL_EXPLICIT_INSTANTIATION (decl
)));
2700 /* Constrain the visibility of a class TYPE based on the visibility of its
2701 field types. Warn if any fields require lesser visibility. */
2704 constrain_class_visibility (tree type
)
2710 int vis
= type_visibility (type
);
2712 if (vis
== VISIBILITY_ANON
2713 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type
)))
2716 /* Don't warn about visibility if the class has explicit visibility. */
2717 if (CLASSTYPE_VISIBILITY_SPECIFIED (type
))
2718 vis
= VISIBILITY_INTERNAL
;
2720 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
2721 if (TREE_CODE (t
) == FIELD_DECL
&& TREE_TYPE (t
) != error_mark_node
2722 && !DECL_ARTIFICIAL (t
))
2724 tree ftype
= strip_pointer_or_array_types (TREE_TYPE (t
));
2725 int subvis
= type_visibility (ftype
);
2727 if (subvis
== VISIBILITY_ANON
)
2729 if (!in_main_input_context())
2731 tree nlt
= no_linkage_check (ftype
, /*relaxed_p=*/false);
2734 if (same_type_p (TREE_TYPE (t
), nlt
))
2735 warning (OPT_Wsubobject_linkage
, "\
2736 %qT has a field %qD whose type has no linkage",
2739 warning (OPT_Wsubobject_linkage
, "\
2740 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2744 warning (OPT_Wsubobject_linkage
, "\
2745 %qT has a field %qD whose type uses the anonymous namespace",
2749 else if (MAYBE_CLASS_TYPE_P (ftype
)
2750 && vis
< VISIBILITY_HIDDEN
2751 && subvis
>= VISIBILITY_HIDDEN
)
2752 warning (OPT_Wattributes
, "\
2753 %qT declared with greater visibility than the type of its field %qD",
2757 binfo
= TYPE_BINFO (type
);
2758 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
2760 int subvis
= type_visibility (TREE_TYPE (t
));
2762 if (subvis
== VISIBILITY_ANON
)
2764 if (!in_main_input_context())
2766 tree nlt
= no_linkage_check (TREE_TYPE (t
), /*relaxed_p=*/false);
2769 if (same_type_p (TREE_TYPE (t
), nlt
))
2770 warning (OPT_Wsubobject_linkage
, "\
2771 %qT has a base %qT whose type has no linkage",
2772 type
, TREE_TYPE (t
));
2774 warning (OPT_Wsubobject_linkage
, "\
2775 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2776 type
, TREE_TYPE (t
), nlt
);
2779 warning (OPT_Wsubobject_linkage
, "\
2780 %qT has a base %qT whose type uses the anonymous namespace",
2781 type
, TREE_TYPE (t
));
2784 else if (vis
< VISIBILITY_HIDDEN
2785 && subvis
>= VISIBILITY_HIDDEN
)
2786 warning (OPT_Wattributes
, "\
2787 %qT declared with greater visibility than its base %qT",
2788 type
, TREE_TYPE (t
));
2792 /* Functions for adjusting the visibility of a tagged type and its nested
2793 types and declarations when it gets a name for linkage purposes from a
2796 static void bt_reset_linkage_1 (binding_entry
, void *);
2797 static void bt_reset_linkage_2 (binding_entry
, void *);
2799 /* First reset the visibility of all the types. */
2802 reset_type_linkage_1 (tree type
)
2804 set_linkage_according_to_type (type
, TYPE_MAIN_DECL (type
));
2805 if (CLASS_TYPE_P (type
))
2806 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2807 bt_reset_linkage_1
, NULL
);
2810 bt_reset_linkage_1 (binding_entry b
, void */
*data*/
)
2812 reset_type_linkage_1 (b
->type
);
2815 /* Then reset the visibility of any static data members or member
2816 functions that use those types. */
2819 reset_decl_linkage (tree decl
)
2821 if (TREE_PUBLIC (decl
))
2823 if (DECL_CLONED_FUNCTION_P (decl
))
2825 TREE_PUBLIC (decl
) = true;
2826 DECL_INTERFACE_KNOWN (decl
) = false;
2827 determine_visibility (decl
);
2828 tentative_decl_linkage (decl
);
2832 reset_type_linkage_2 (tree type
)
2834 if (CLASS_TYPE_P (type
))
2836 if (tree vt
= CLASSTYPE_VTABLES (type
))
2838 tree name
= mangle_vtbl_for_type (type
);
2839 DECL_NAME (vt
) = name
;
2840 SET_DECL_ASSEMBLER_NAME (vt
, name
);
2841 reset_decl_linkage (vt
);
2843 if (tree ti
= CLASSTYPE_TYPEINFO_VAR (type
))
2845 tree name
= mangle_typeinfo_for_type (type
);
2846 DECL_NAME (ti
) = name
;
2847 SET_DECL_ASSEMBLER_NAME (ti
, name
);
2848 TREE_TYPE (name
) = type
;
2849 reset_decl_linkage (ti
);
2851 for (tree m
= TYPE_FIELDS (type
); m
; m
= DECL_CHAIN (m
))
2853 tree mem
= STRIP_TEMPLATE (m
);
2854 if (TREE_CODE (mem
) == VAR_DECL
|| TREE_CODE (mem
) == FUNCTION_DECL
)
2855 reset_decl_linkage (mem
);
2857 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2858 bt_reset_linkage_2
, NULL
);
2863 bt_reset_linkage_2 (binding_entry b
, void */
*data*/
)
2865 reset_type_linkage_2 (b
->type
);
2868 reset_type_linkage (tree type
)
2870 reset_type_linkage_1 (type
);
2871 reset_type_linkage_2 (type
);
2874 /* Set up our initial idea of what the linkage of DECL should be. */
2877 tentative_decl_linkage (tree decl
)
2879 if (DECL_INTERFACE_KNOWN (decl
))
2880 /* We've already made a decision as to how this function will
2882 else if (vague_linkage_p (decl
))
2884 if (TREE_CODE (decl
) == FUNCTION_DECL
2885 && decl_defined_p (decl
))
2887 DECL_EXTERNAL (decl
) = 1;
2888 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2889 note_vague_linkage_fn (decl
);
2890 /* A non-template inline function with external linkage will
2891 always be COMDAT. As we must eventually determine the
2892 linkage of all functions, and as that causes writes to
2893 the data mapped in from the PCH file, it's advantageous
2894 to mark the functions at this point. */
2895 if (DECL_DECLARED_INLINE_P (decl
)
2896 && (!DECL_IMPLICIT_INSTANTIATION (decl
)
2897 || DECL_DEFAULTED_FN (decl
)))
2899 /* This function must have external linkage, as
2900 otherwise DECL_INTERFACE_KNOWN would have been
2902 gcc_assert (TREE_PUBLIC (decl
));
2903 comdat_linkage (decl
);
2904 DECL_INTERFACE_KNOWN (decl
) = 1;
2907 else if (VAR_P (decl
))
2908 maybe_commonize_var (decl
);
2912 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2913 for DECL has not already been determined, do so now by setting
2914 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2915 function is called entities with vague linkage whose definitions
2916 are available must have TREE_PUBLIC set.
2918 If this function decides to place DECL in COMDAT, it will set
2919 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2920 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2921 callers defer that decision until it is clear that DECL is actually
2925 import_export_decl (tree decl
)
2930 tree class_type
= NULL_TREE
;
2932 if (DECL_INTERFACE_KNOWN (decl
))
2935 /* We cannot determine what linkage to give to an entity with vague
2936 linkage until the end of the file. For example, a virtual table
2937 for a class will be defined if and only if the key method is
2938 defined in this translation unit. As a further example, consider
2939 that when compiling a translation unit that uses PCH file with
2940 "-frepo" it would be incorrect to make decisions about what
2941 entities to emit when building the PCH; those decisions must be
2942 delayed until the repository information has been processed. */
2943 gcc_assert (at_eof
);
2944 /* Object file linkage for explicit instantiations is handled in
2945 mark_decl_instantiated. For static variables in functions with
2946 vague linkage, maybe_commonize_var is used.
2948 Therefore, the only declarations that should be provided to this
2949 function are those with external linkage that are:
2951 * implicit instantiations of function templates
2955 * implicit instantiations of static data members of class
2962 Furthermore, all entities that reach this point must have a
2963 definition available in this translation unit.
2965 The following assertions check these conditions. */
2966 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2967 /* Any code that creates entities with TREE_PUBLIC cleared should
2968 also set DECL_INTERFACE_KNOWN. */
2969 gcc_assert (TREE_PUBLIC (decl
));
2970 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2971 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2972 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
2973 || DECL_DECLARED_INLINE_P (decl
));
2975 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2976 || DECL_VTABLE_OR_VTT_P (decl
)
2977 || DECL_TINFO_P (decl
));
2978 /* Check that a definition of DECL is available in this translation
2980 gcc_assert (!DECL_REALLY_EXTERN (decl
));
2982 /* Assume that DECL will not have COMDAT linkage. */
2984 /* Assume that DECL will not be imported into this translation
2988 /* See if the repository tells us whether or not to emit DECL in
2989 this translation unit. */
2990 emit_p
= repo_emit_p (decl
);
2993 else if (emit_p
== 1)
2995 /* The repository indicates that this entity should be defined
2996 here. Make sure the back end honors that request. */
2998 /* Output the definition as an ordinary strong definition. */
2999 DECL_EXTERNAL (decl
) = 0;
3000 DECL_INTERFACE_KNOWN (decl
) = 1;
3005 /* We have already decided what to do with this DECL; there is no
3006 need to check anything further. */
3008 else if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
3010 class_type
= DECL_CONTEXT (decl
);
3011 import_export_class (class_type
);
3012 if (CLASSTYPE_INTERFACE_KNOWN (class_type
)
3013 && CLASSTYPE_INTERFACE_ONLY (class_type
))
3015 else if ((!flag_weak
|| TARGET_WEAK_NOT_IN_ARCHIVE_TOC
)
3016 && !CLASSTYPE_USE_TEMPLATE (class_type
)
3017 && CLASSTYPE_KEY_METHOD (class_type
)
3018 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
)))
3019 /* The ABI requires that all virtual tables be emitted with
3020 COMDAT linkage. However, on systems where COMDAT symbols
3021 don't show up in the table of contents for a static
3022 archive, or on systems without weak symbols (where we
3023 approximate COMDAT linkage by using internal linkage), the
3024 linker will report errors about undefined symbols because
3025 it will not see the virtual table definition. Therefore,
3026 in the case that we know that the virtual table will be
3027 emitted in only one translation unit, we make the virtual
3028 table an ordinary definition with external linkage. */
3029 DECL_EXTERNAL (decl
) = 0;
3030 else if (CLASSTYPE_INTERFACE_KNOWN (class_type
))
3032 /* CLASS_TYPE is being exported from this translation unit,
3033 so DECL should be defined here. */
3034 if (!flag_weak
&& CLASSTYPE_EXPLICIT_INSTANTIATION (class_type
))
3035 /* If a class is declared in a header with the "extern
3036 template" extension, then it will not be instantiated,
3037 even in translation units that would normally require
3038 it. Often such classes are explicitly instantiated in
3039 one translation unit. Therefore, the explicit
3040 instantiation must be made visible to other translation
3042 DECL_EXTERNAL (decl
) = 0;
3045 /* The generic C++ ABI says that class data is always
3046 COMDAT, even if there is a key function. Some
3047 variants (e.g., the ARM EABI) says that class data
3048 only has COMDAT linkage if the class data might be
3049 emitted in more than one translation unit. When the
3050 key method can be inline and is inline, we still have
3051 to arrange for comdat even though
3052 class_data_always_comdat is false. */
3053 if (!CLASSTYPE_KEY_METHOD (class_type
)
3054 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
))
3055 || targetm
.cxx
.class_data_always_comdat ())
3057 /* The ABI requires COMDAT linkage. Normally, we
3058 only emit COMDAT things when they are needed;
3059 make sure that we realize that this entity is
3066 else if (!flag_implicit_templates
3067 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type
))
3072 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
3074 tree type
= TREE_TYPE (DECL_NAME (decl
));
3075 if (CLASS_TYPE_P (type
))
3078 import_export_class (type
);
3079 if (CLASSTYPE_INTERFACE_KNOWN (type
)
3080 && TYPE_POLYMORPHIC_P (type
)
3081 && CLASSTYPE_INTERFACE_ONLY (type
)
3082 /* If -fno-rtti was specified, then we cannot be sure
3083 that RTTI information will be emitted with the
3084 virtual table of the class, so we must emit it
3085 wherever it is used. */
3090 if (CLASSTYPE_INTERFACE_KNOWN (type
)
3091 && !CLASSTYPE_INTERFACE_ONLY (type
))
3093 comdat_p
= (targetm
.cxx
.class_data_always_comdat ()
3094 || (CLASSTYPE_KEY_METHOD (type
)
3095 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type
))));
3100 DECL_EXTERNAL (decl
) = 0;
3110 else if (DECL_TEMPLOID_INSTANTIATION (decl
))
3112 /* DECL is an implicit instantiation of a function or static
3114 if ((flag_implicit_templates
3115 && !flag_use_repository
)
3116 || (flag_implicit_inline_templates
3117 && TREE_CODE (decl
) == FUNCTION_DECL
3118 && DECL_DECLARED_INLINE_P (decl
)))
3121 /* If we are not implicitly generating templates, then mark
3122 this entity as undefined in this translation unit. */
3125 else if (DECL_FUNCTION_MEMBER_P (decl
))
3127 if (!DECL_DECLARED_INLINE_P (decl
))
3129 tree ctype
= DECL_CONTEXT (decl
);
3130 import_export_class (ctype
);
3131 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
3133 DECL_NOT_REALLY_EXTERN (decl
)
3134 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
3135 || (DECL_DECLARED_INLINE_P (decl
)
3136 && ! flag_implement_inlines
3137 && !DECL_VINDEX (decl
)));
3139 if (!DECL_NOT_REALLY_EXTERN (decl
))
3140 DECL_EXTERNAL (decl
) = 1;
3142 /* Always make artificials weak. */
3143 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
3146 maybe_make_one_only (decl
);
3157 /* If we are importing DECL into this translation unit, mark is
3158 an undefined here. */
3159 DECL_EXTERNAL (decl
) = 1;
3160 DECL_NOT_REALLY_EXTERN (decl
) = 0;
3164 /* If we decided to put DECL in COMDAT, mark it accordingly at
3166 comdat_linkage (decl
);
3169 DECL_INTERFACE_KNOWN (decl
) = 1;
3172 /* Return an expression that performs the destruction of DECL, which
3173 must be a VAR_DECL whose type has a non-trivial destructor, or is
3174 an array whose (innermost) elements have a non-trivial destructor. */
3177 build_cleanup (tree decl
)
3179 tree clean
= cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
3180 gcc_assert (clean
!= NULL_TREE
);
3184 /* Returns the initialization guard variable for the variable DECL,
3185 which has static storage duration. */
3188 get_guard (tree decl
)
3193 sname
= mangle_guard_variable (decl
);
3194 guard
= get_global_binding (sname
);
3199 /* We use a type that is big enough to contain a mutex as well
3200 as an integer counter. */
3201 guard_type
= targetm
.cxx
.guard_type ();
3202 guard
= build_decl (DECL_SOURCE_LOCATION (decl
),
3203 VAR_DECL
, sname
, guard_type
);
3205 /* The guard should have the same linkage as what it guards. */
3206 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
3207 TREE_STATIC (guard
) = TREE_STATIC (decl
);
3208 DECL_COMMON (guard
) = DECL_COMMON (decl
);
3209 DECL_COMDAT (guard
) = DECL_COMDAT (decl
);
3210 CP_DECL_THREAD_LOCAL_P (guard
) = CP_DECL_THREAD_LOCAL_P (decl
);
3211 set_decl_tls_model (guard
, DECL_TLS_MODEL (decl
));
3212 if (DECL_ONE_ONLY (decl
))
3213 make_decl_one_only (guard
, cxx_comdat_group (guard
));
3214 if (TREE_PUBLIC (decl
))
3215 DECL_WEAK (guard
) = DECL_WEAK (decl
);
3216 DECL_VISIBILITY (guard
) = DECL_VISIBILITY (decl
);
3217 DECL_VISIBILITY_SPECIFIED (guard
) = DECL_VISIBILITY_SPECIFIED (decl
);
3219 DECL_ARTIFICIAL (guard
) = 1;
3220 DECL_IGNORED_P (guard
) = 1;
3221 TREE_USED (guard
) = 1;
3222 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
3227 /* Return an atomic load of src with the appropriate memory model. */
3230 build_atomic_load_byte (tree src
, HOST_WIDE_INT model
)
3232 tree ptr_type
= build_pointer_type (char_type_node
);
3233 tree mem_model
= build_int_cst (integer_type_node
, model
);
3238 size
= tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node
));
3240 fncode
= BUILT_IN_ATOMIC_LOAD_N
+ exact_log2 (size
) + 1;
3241 t
= builtin_decl_implicit ((enum built_in_function
) fncode
);
3243 addr
= build1 (ADDR_EXPR
, ptr_type
, src
);
3244 val
= build_call_expr (t
, 2, addr
, mem_model
);
3248 /* Return those bits of the GUARD variable that should be set when the
3249 guarded entity is actually initialized. */
3252 get_guard_bits (tree guard
)
3254 if (!targetm
.cxx
.guard_mask_bit ())
3256 /* We only set the first byte of the guard, in order to leave room
3257 for a mutex in the high-order bits. */
3258 guard
= build1 (ADDR_EXPR
,
3259 build_pointer_type (TREE_TYPE (guard
)),
3261 guard
= build1 (NOP_EXPR
,
3262 build_pointer_type (char_type_node
),
3264 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
3270 /* Return an expression which determines whether or not the GUARD
3271 variable has already been initialized. */
3274 get_guard_cond (tree guard
, bool thread_safe
)
3279 guard
= get_guard_bits (guard
);
3281 guard
= build_atomic_load_byte (guard
, MEMMODEL_ACQUIRE
);
3283 /* Mask off all but the low bit. */
3284 if (targetm
.cxx
.guard_mask_bit ())
3286 guard_value
= integer_one_node
;
3287 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3288 guard_value
= fold_convert (TREE_TYPE (guard
), guard_value
);
3289 guard
= cp_build_binary_op (input_location
,
3290 BIT_AND_EXPR
, guard
, guard_value
,
3291 tf_warning_or_error
);
3294 guard_value
= integer_zero_node
;
3295 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3296 guard_value
= fold_convert (TREE_TYPE (guard
), guard_value
);
3297 return cp_build_binary_op (input_location
,
3298 EQ_EXPR
, guard
, guard_value
,
3299 tf_warning_or_error
);
3302 /* Return an expression which sets the GUARD variable, indicating that
3303 the variable being guarded has been initialized. */
3306 set_guard (tree guard
)
3310 /* Set the GUARD to one. */
3311 guard
= get_guard_bits (guard
);
3312 guard_init
= integer_one_node
;
3313 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
3314 guard_init
= fold_convert (TREE_TYPE (guard
), guard_init
);
3315 return cp_build_modify_expr (input_location
, guard
, NOP_EXPR
, guard_init
,
3316 tf_warning_or_error
);
3319 /* Returns true iff we can tell that VAR does not have a dynamic
3323 var_defined_without_dynamic_init (tree var
)
3325 /* If it's defined in another TU, we can't tell. */
3326 if (DECL_EXTERNAL (var
))
3328 /* If it has a non-trivial destructor, registering the destructor
3329 counts as dynamic initialization. */
3330 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var
)))
3332 /* If it's in this TU, its initializer has been processed, unless
3333 it's a case of self-initialization, then DECL_INITIALIZED_P is
3334 false while the initializer is handled by finish_id_expression. */
3335 if (!DECL_INITIALIZED_P (var
))
3337 /* If it has no initializer or a constant one, it's not dynamic. */
3338 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var
)
3339 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var
));
3342 /* Returns true iff VAR is a variable that needs uses to be
3343 wrapped for possible dynamic initialization. */
3346 var_needs_tls_wrapper (tree var
)
3348 return (!error_operand_p (var
)
3349 && CP_DECL_THREAD_LOCAL_P (var
)
3350 && !DECL_GNU_TLS_P (var
)
3351 && !DECL_FUNCTION_SCOPE_P (var
)
3352 && !var_defined_without_dynamic_init (var
));
3355 /* Get the FUNCTION_DECL for the shared TLS init function for this
3356 translation unit. */
3359 get_local_tls_init_fn (location_t loc
)
3361 tree sname
= get_identifier ("__tls_init");
3362 tree fn
= get_global_binding (sname
);
3365 fn
= build_lang_decl_loc (loc
, FUNCTION_DECL
, sname
,
3366 build_function_type (void_type_node
,
3368 SET_DECL_LANGUAGE (fn
, lang_c
);
3369 TREE_PUBLIC (fn
) = false;
3370 DECL_ARTIFICIAL (fn
) = true;
3372 set_global_binding (fn
);
3377 /* Get a FUNCTION_DECL for the init function for the thread_local
3378 variable VAR. The init function will be an alias to the function
3379 that initializes all the non-local TLS variables in the translation
3380 unit. The init function is only used by the wrapper function. */
3383 get_tls_init_fn (tree var
)
3385 /* Only C++11 TLS vars need this init fn. */
3386 if (!var_needs_tls_wrapper (var
))
3389 /* If -fno-extern-tls-init, assume that we don't need to call
3390 a tls init function for a variable defined in another TU. */
3391 if (!flag_extern_tls_init
&& DECL_EXTERNAL (var
))
3394 /* If the variable is internal, or if we can't generate aliases,
3395 call the local init function directly. */
3396 if (!TREE_PUBLIC (var
) || !TARGET_SUPPORTS_ALIASES
)
3397 return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var
));
3399 tree sname
= mangle_tls_init_fn (var
);
3400 tree fn
= get_global_binding (sname
);
3403 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
3404 build_function_type (void_type_node
,
3406 SET_DECL_LANGUAGE (fn
, lang_c
);
3407 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3408 DECL_ARTIFICIAL (fn
) = true;
3409 DECL_COMDAT (fn
) = DECL_COMDAT (var
);
3410 DECL_EXTERNAL (fn
) = DECL_EXTERNAL (var
);
3411 if (DECL_ONE_ONLY (var
))
3412 make_decl_one_only (fn
, cxx_comdat_group (fn
));
3413 if (TREE_PUBLIC (var
))
3415 tree obtype
= strip_array_types (non_reference (TREE_TYPE (var
)));
3416 /* If the variable is defined somewhere else and might have static
3417 initialization, make the init function a weak reference. */
3418 if ((!TYPE_NEEDS_CONSTRUCTING (obtype
)
3419 || TYPE_HAS_CONSTEXPR_CTOR (obtype
)
3420 || TYPE_HAS_TRIVIAL_DFLT (obtype
))
3421 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype
)
3422 && DECL_EXTERNAL (var
))
3425 DECL_WEAK (fn
) = DECL_WEAK (var
);
3427 DECL_VISIBILITY (fn
) = DECL_VISIBILITY (var
);
3428 DECL_VISIBILITY_SPECIFIED (fn
) = DECL_VISIBILITY_SPECIFIED (var
);
3429 DECL_DLLIMPORT_P (fn
) = DECL_DLLIMPORT_P (var
);
3430 DECL_IGNORED_P (fn
) = 1;
3433 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3435 set_global_binding (fn
);
3440 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3441 variable VAR. The wrapper function calls the init function (if any) for
3442 VAR and then returns a reference to VAR. The wrapper function is used
3443 in place of VAR everywhere VAR is mentioned. */
3446 get_tls_wrapper_fn (tree var
)
3448 /* Only C++11 TLS vars need this wrapper fn. */
3449 if (!var_needs_tls_wrapper (var
))
3452 tree sname
= mangle_tls_wrapper_fn (var
);
3453 tree fn
= get_global_binding (sname
);
3456 /* A named rvalue reference is an lvalue, so the wrapper should
3457 always return an lvalue reference. */
3458 tree type
= non_reference (TREE_TYPE (var
));
3459 type
= build_reference_type (type
);
3460 tree fntype
= build_function_type (type
, void_list_node
);
3462 fn
= build_lang_decl_loc (DECL_SOURCE_LOCATION (var
),
3463 FUNCTION_DECL
, sname
, fntype
);
3464 SET_DECL_LANGUAGE (fn
, lang_c
);
3465 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3466 DECL_ARTIFICIAL (fn
) = true;
3467 DECL_IGNORED_P (fn
) = 1;
3468 /* The wrapper is inline and emitted everywhere var is used. */
3469 DECL_DECLARED_INLINE_P (fn
) = true;
3470 if (TREE_PUBLIC (var
))
3472 comdat_linkage (fn
);
3473 #ifdef HAVE_GAS_HIDDEN
3474 /* Make the wrapper bind locally; there's no reason to share
3475 the wrapper between multiple shared objects. */
3476 DECL_VISIBILITY (fn
) = VISIBILITY_INTERNAL
;
3477 DECL_VISIBILITY_SPECIFIED (fn
) = true;
3480 if (!TREE_PUBLIC (fn
))
3481 DECL_INTERFACE_KNOWN (fn
) = true;
3483 note_vague_linkage_fn (fn
);
3486 /* We want CSE to commonize calls to the wrapper, but marking it as
3487 pure is unsafe since it has side-effects. I guess we need a new
3488 ECF flag even weaker than ECF_PURE. FIXME! */
3489 DECL_PURE_P (fn
) = true;
3492 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3494 set_global_binding (fn
);
3499 /* At EOF, generate the definition for the TLS wrapper function FN:
3502 if (init_fn) init_fn();
3507 generate_tls_wrapper (tree fn
)
3509 tree var
= DECL_BEFRIENDING_CLASSES (fn
);
3511 start_preparsed_function (fn
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
3512 tree body
= begin_function_body ();
3513 /* Only call the init fn if there might be one. */
3514 if (tree init_fn
= get_tls_init_fn (var
))
3516 tree if_stmt
= NULL_TREE
;
3517 /* If init_fn is a weakref, make sure it exists before calling. */
3518 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn
)))
3520 if_stmt
= begin_if_stmt ();
3521 tree addr
= cp_build_addr_expr (init_fn
, tf_warning_or_error
);
3522 tree cond
= cp_build_binary_op (DECL_SOURCE_LOCATION (var
),
3523 NE_EXPR
, addr
, nullptr_node
,
3524 tf_warning_or_error
);
3525 finish_if_stmt_cond (cond
, if_stmt
);
3527 finish_expr_stmt (build_cxx_call
3528 (init_fn
, 0, NULL
, tf_warning_or_error
));
3531 finish_then_clause (if_stmt
);
3532 finish_if_stmt (if_stmt
);
3536 /* If there's no initialization, the wrapper is a constant function. */
3537 TREE_READONLY (fn
) = true;
3538 finish_return_stmt (convert_from_reference (var
));
3539 finish_function_body (body
);
3540 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3543 /* Start the process of running a particular set of global constructors
3544 or destructors. Subroutine of do_[cd]tors. Also called from
3545 vtv_start_verification_constructor_init_function. */
3548 start_objects (int method_type
, int initp
)
3554 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3556 if (initp
!= DEFAULT_INIT_PRIORITY
)
3566 sprintf (type
, "sub_%c%c%.5u", method_type
, joiner
, initp
);
3569 sprintf (type
, "sub_%c", method_type
);
3571 fndecl
= build_lang_decl (FUNCTION_DECL
,
3572 get_file_function_name (type
),
3573 build_function_type_list (void_type_node
,
3575 start_preparsed_function (fndecl
, /*attrs=*/NULL_TREE
, SF_PRE_PARSED
);
3577 TREE_PUBLIC (current_function_decl
) = 0;
3579 /* Mark as artificial because it's not explicitly in the user's
3581 DECL_ARTIFICIAL (current_function_decl
) = 1;
3583 /* Mark this declaration as used to avoid spurious warnings. */
3584 TREE_USED (current_function_decl
) = 1;
3586 /* Mark this function as a global constructor or destructor. */
3587 if (method_type
== 'I')
3588 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
3590 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
3592 body
= begin_compound_stmt (BCS_FN_BODY
);
3597 /* Finish the process of running a particular set of global constructors
3598 or destructors. Subroutine of do_[cd]tors. */
3601 finish_objects (int method_type
, int initp
, tree body
)
3606 finish_compound_stmt (body
);
3607 fn
= finish_function (/*inline_p=*/false);
3609 if (method_type
== 'I')
3611 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
3612 decl_init_priority_insert (fn
, initp
);
3616 DECL_STATIC_DESTRUCTOR (fn
) = 1;
3617 decl_fini_priority_insert (fn
, initp
);
3620 expand_or_defer_fn (fn
);
3623 /* The names of the parameters to the function created to handle
3624 initializations and destructions for objects with static storage
3626 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3627 #define PRIORITY_IDENTIFIER "__priority"
3629 /* The name of the function we create to handle initializations and
3630 destructions for objects with static storage duration. */
3631 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3633 /* The declaration for the __INITIALIZE_P argument. */
3634 static GTY(()) tree initialize_p_decl
;
3636 /* The declaration for the __PRIORITY argument. */
3637 static GTY(()) tree priority_decl
;
3639 /* The declaration for the static storage duration function. */
3640 static GTY(()) tree ssdf_decl
;
3642 /* All the static storage duration functions created in this
3643 translation unit. */
3644 static GTY(()) vec
<tree
, va_gc
> *ssdf_decls
;
3646 /* A map from priority levels to information about that priority
3647 level. There may be many such levels, so efficient lookup is
3649 static splay_tree priority_info_map
;
3651 /* Begins the generation of the function that will handle all
3652 initialization and destruction of objects with static storage
3653 duration. The function generated takes two parameters of type
3654 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3655 nonzero, it performs initializations. Otherwise, it performs
3656 destructions. It only performs those initializations or
3657 destructions with the indicated __PRIORITY. The generated function
3660 It is assumed that this function will only be called once per
3661 translation unit. */
3664 start_static_storage_duration_function (unsigned count
)
3668 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
3670 /* Create the identifier for this function. It will be of the form
3671 SSDF_IDENTIFIER_<number>. */
3672 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, count
);
3674 type
= build_function_type_list (void_type_node
,
3675 integer_type_node
, integer_type_node
,
3678 /* Create the FUNCTION_DECL itself. */
3679 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
3680 get_identifier (id
),
3682 TREE_PUBLIC (ssdf_decl
) = 0;
3683 DECL_ARTIFICIAL (ssdf_decl
) = 1;
3685 /* Put this function in the list of functions to be called from the
3686 static constructors and destructors. */
3689 vec_alloc (ssdf_decls
, 32);
3691 /* Take this opportunity to initialize the map from priority
3692 numbers to information about that priority level. */
3693 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
3694 /*delete_key_fn=*/0,
3695 /*delete_value_fn=*/
3696 splay_tree_delete_pointers
);
3698 /* We always need to generate functions for the
3699 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3700 priorities later, we'll be sure to find the
3701 DEFAULT_INIT_PRIORITY. */
3702 get_priority_info (DEFAULT_INIT_PRIORITY
);
3705 vec_safe_push (ssdf_decls
, ssdf_decl
);
3707 /* Create the argument list. */
3708 initialize_p_decl
= cp_build_parm_decl
3709 (ssdf_decl
, get_identifier (INITIALIZE_P_IDENTIFIER
), integer_type_node
);
3710 TREE_USED (initialize_p_decl
) = 1;
3711 priority_decl
= cp_build_parm_decl
3712 (ssdf_decl
, get_identifier (PRIORITY_IDENTIFIER
), integer_type_node
);
3713 TREE_USED (priority_decl
) = 1;
3715 DECL_CHAIN (initialize_p_decl
) = priority_decl
;
3716 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
3718 /* Put the function in the global scope. */
3719 pushdecl (ssdf_decl
);
3721 /* Start the function itself. This is equivalent to declaring the
3724 static void __ssdf (int __initialize_p, init __priority_p);
3726 It is static because we only need to call this function from the
3727 various constructor and destructor functions for this module. */
3728 start_preparsed_function (ssdf_decl
,
3729 /*attrs=*/NULL_TREE
,
3732 /* Set up the scope of the outermost block in the function. */
3733 body
= begin_compound_stmt (BCS_FN_BODY
);
3738 /* Finish the generation of the function which performs initialization
3739 and destruction of objects with static storage duration. After
3740 this point, no more such objects can be created. */
3743 finish_static_storage_duration_function (tree body
)
3745 /* Close out the function. */
3746 finish_compound_stmt (body
);
3747 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3750 /* Return the information about the indicated PRIORITY level. If no
3751 code to handle this level has yet been generated, generate the
3752 appropriate prologue. */
3754 static priority_info
3755 get_priority_info (int priority
)
3760 n
= splay_tree_lookup (priority_info_map
,
3761 (splay_tree_key
) priority
);
3764 /* Create a new priority information structure, and insert it
3766 pi
= XNEW (struct priority_info_s
);
3767 pi
->initializations_p
= 0;
3768 pi
->destructions_p
= 0;
3769 splay_tree_insert (priority_info_map
,
3770 (splay_tree_key
) priority
,
3771 (splay_tree_value
) pi
);
3774 pi
= (priority_info
) n
->value
;
3779 /* The effective initialization priority of a DECL. */
3781 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3782 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3783 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3785 /* Whether a DECL needs a guard to protect it against multiple
3788 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3789 || DECL_ONE_ONLY (decl) \
3790 || DECL_WEAK (decl)))
3792 /* Called from one_static_initialization_or_destruction(),
3794 Walks the initializer list of a global variable and looks for
3795 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3796 and that have their DECL_CONTEXT() == NULL.
3797 For each such temporary variable, set their DECL_CONTEXT() to
3798 the current function. This is necessary because otherwise
3799 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3800 when trying to refer to a temporary variable that does not have
3801 it's DECL_CONTECT() properly set. */
3803 fix_temporary_vars_context_r (tree
*node
,
3807 gcc_assert (current_function_decl
);
3809 if (TREE_CODE (*node
) == BIND_EXPR
)
3813 for (var
= BIND_EXPR_VARS (*node
); var
; var
= DECL_CHAIN (var
))
3816 && DECL_ARTIFICIAL (var
)
3817 && !DECL_CONTEXT (var
))
3818 DECL_CONTEXT (var
) = current_function_decl
;
3824 /* Set up to handle the initialization or destruction of DECL. If
3825 INITP is nonzero, we are initializing the variable. Otherwise, we
3826 are destroying it. */
3829 one_static_initialization_or_destruction (tree decl
, tree init
, bool initp
)
3831 tree guard_if_stmt
= NULL_TREE
;
3834 /* If we are supposed to destruct and there's a trivial destructor,
3835 nothing has to be done. */
3837 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3840 /* Trick the compiler into thinking we are at the file and line
3841 where DECL was declared so that error-messages make sense, and so
3842 that the debugger will show somewhat sensible file and line
3844 input_location
= DECL_SOURCE_LOCATION (decl
);
3846 /* Make sure temporary variables in the initialiser all have
3847 their DECL_CONTEXT() set to a value different from NULL_TREE.
3848 This can happen when global variables initializers are built.
3849 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3850 the temporary variables that might have been generated in the
3851 accompanying initializers is NULL_TREE, meaning the variables have been
3852 declared in the global namespace.
3853 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3854 of the temporaries are set to the current function decl. */
3855 cp_walk_tree_without_duplicates (&init
,
3856 fix_temporary_vars_context_r
,
3863 Access control for implicit calls to the constructors,
3864 the conversion functions, or the destructor called to
3865 create and destroy a static data member is performed as
3866 if these calls appeared in the scope of the member's
3869 we pretend we are in a static member function of the class of
3870 which the DECL is a member. */
3871 if (member_p (decl
))
3873 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
3874 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
3877 /* Assume we don't need a guard. */
3879 /* We need a guard if this is an object with external linkage that
3880 might be initialized in more than one place. (For example, a
3881 static data member of a template, when the data member requires
3883 if (NEEDS_GUARD_P (decl
))
3887 guard
= get_guard (decl
);
3889 /* When using __cxa_atexit, we just check the GUARD as we would
3890 for a local static. */
3891 if (flag_use_cxa_atexit
)
3893 /* When using __cxa_atexit, we never try to destroy
3894 anything from a static destructor. */
3896 guard_cond
= get_guard_cond (guard
, false);
3898 /* If we don't have __cxa_atexit, then we will be running
3899 destructors from .fini sections, or their equivalents. So,
3900 we need to know how many times we've tried to initialize this
3901 object. We do initializations only if the GUARD is zero,
3902 i.e., if we are the first to initialize the variable. We do
3903 destructions only if the GUARD is one, i.e., if we are the
3904 last to destroy the variable. */
3907 = cp_build_binary_op (input_location
,
3909 cp_build_unary_op (PREINCREMENT_EXPR
,
3912 tf_warning_or_error
),
3914 tf_warning_or_error
);
3917 = cp_build_binary_op (input_location
,
3919 cp_build_unary_op (PREDECREMENT_EXPR
,
3922 tf_warning_or_error
),
3924 tf_warning_or_error
);
3926 guard_if_stmt
= begin_if_stmt ();
3927 finish_if_stmt_cond (guard_cond
, guard_if_stmt
);
3931 /* If we're using __cxa_atexit, we have not already set the GUARD,
3932 so we must do so now. */
3933 if (guard
&& initp
&& flag_use_cxa_atexit
)
3934 finish_expr_stmt (set_guard (guard
));
3936 /* Perform the initialization or destruction. */
3941 finish_expr_stmt (init
);
3942 if (sanitize_flags_p (SANITIZE_ADDRESS
, decl
))
3944 varpool_node
*vnode
= varpool_node::get (decl
);
3946 vnode
->dynamically_initialized
= 1;
3950 /* If we're using __cxa_atexit, register a function that calls the
3951 destructor for the object. */
3952 if (flag_use_cxa_atexit
)
3953 finish_expr_stmt (register_dtor_fn (decl
));
3956 finish_expr_stmt (build_cleanup (decl
));
3958 /* Finish the guard if-stmt, if necessary. */
3961 finish_then_clause (guard_if_stmt
);
3962 finish_if_stmt (guard_if_stmt
);
3965 /* Now that we're done with DECL we don't need to pretend to be a
3966 member of its class any longer. */
3967 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
3968 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
3971 /* Generate code to do the initialization or destruction of the decls in VARS,
3972 a TREE_LIST of VAR_DECL with static storage duration.
3973 Whether initialization or destruction is performed is specified by INITP. */
3976 do_static_initialization_or_destruction (tree vars
, bool initp
)
3978 tree node
, init_if_stmt
, cond
;
3980 /* Build the outer if-stmt to check for initialization or destruction. */
3981 init_if_stmt
= begin_if_stmt ();
3982 cond
= initp
? integer_one_node
: integer_zero_node
;
3983 cond
= cp_build_binary_op (input_location
,
3987 tf_warning_or_error
);
3988 finish_if_stmt_cond (cond
, init_if_stmt
);
3990 /* To make sure dynamic construction doesn't access globals from other
3991 compilation units where they might not be yet constructed, for
3992 -fsanitize=address insert __asan_before_dynamic_init call that
3993 prevents access to either all global variables that need construction
3994 in other compilation units, or at least those that haven't been
3995 initialized yet. Variables that need dynamic construction in
3996 the current compilation unit are kept accessible. */
3997 if (initp
&& (flag_sanitize
& SANITIZE_ADDRESS
))
3998 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
4002 tree decl
= TREE_VALUE (node
);
4003 tree priority_if_stmt
;
4007 /* If we don't need a destructor, there's nothing to do. Avoid
4008 creating a possibly empty if-stmt. */
4009 if (!initp
&& TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
4011 node
= TREE_CHAIN (node
);
4015 /* Remember that we had an initialization or finalization at this
4017 priority
= DECL_EFFECTIVE_INIT_PRIORITY (decl
);
4018 pi
= get_priority_info (priority
);
4020 pi
->initializations_p
= 1;
4022 pi
->destructions_p
= 1;
4024 /* Conditionalize this initialization on being in the right priority
4025 and being initializing/finalizing appropriately. */
4026 priority_if_stmt
= begin_if_stmt ();
4027 cond
= cp_build_binary_op (input_location
,
4030 build_int_cst (NULL_TREE
, priority
),
4031 tf_warning_or_error
);
4032 finish_if_stmt_cond (cond
, priority_if_stmt
);
4034 /* Process initializers with same priority. */
4036 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node
)) == priority
;
4037 node
= TREE_CHAIN (node
))
4038 /* Do one initialization or destruction. */
4039 one_static_initialization_or_destruction (TREE_VALUE (node
),
4040 TREE_PURPOSE (node
), initp
);
4042 /* Finish up the priority if-stmt body. */
4043 finish_then_clause (priority_if_stmt
);
4044 finish_if_stmt (priority_if_stmt
);
4048 /* Revert what __asan_before_dynamic_init did by calling
4049 __asan_after_dynamic_init. */
4050 if (initp
&& (flag_sanitize
& SANITIZE_ADDRESS
))
4051 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4053 /* Finish up the init/destruct if-stmt body. */
4054 finish_then_clause (init_if_stmt
);
4055 finish_if_stmt (init_if_stmt
);
4058 /* VARS is a list of variables with static storage duration which may
4059 need initialization and/or finalization. Remove those variables
4060 that don't really need to be initialized or finalized, and return
4061 the resulting list. The order in which the variables appear in
4062 VARS is in reverse order of the order in which they should actually
4063 be initialized. The list we return is in the unreversed order;
4064 i.e., the first variable should be initialized first. */
4067 prune_vars_needing_no_initialization (tree
*vars
)
4070 tree result
= NULL_TREE
;
4075 tree decl
= TREE_VALUE (t
);
4076 tree init
= TREE_PURPOSE (t
);
4078 /* Deal gracefully with error. */
4079 if (error_operand_p (decl
))
4081 var
= &TREE_CHAIN (t
);
4085 /* The only things that can be initialized are variables. */
4086 gcc_assert (VAR_P (decl
));
4088 /* If this object is not defined, we don't need to do anything
4090 if (DECL_EXTERNAL (decl
))
4092 var
= &TREE_CHAIN (t
);
4096 /* Also, if the initializer already contains errors, we can bail
4098 if (init
&& TREE_CODE (init
) == TREE_LIST
4099 && value_member (error_mark_node
, init
))
4101 var
= &TREE_CHAIN (t
);
4105 /* This variable is going to need initialization and/or
4106 finalization, so we add it to the list. */
4107 *var
= TREE_CHAIN (t
);
4108 TREE_CHAIN (t
) = result
;
4115 /* Make sure we have told the back end about all the variables in
4119 write_out_vars (tree vars
)
4123 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
4125 tree var
= TREE_VALUE (v
);
4126 if (!var_finalized_p (var
))
4128 import_export_decl (var
);
4129 rest_of_decl_compilation (var
, 1, 1);
4134 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4135 (otherwise) that will initialize all global objects with static
4136 storage duration having the indicated PRIORITY. */
4139 generate_ctor_or_dtor_function (bool constructor_p
, int priority
,
4147 input_location
= *locus
;
4149 /* Was: locus->line++; */
4151 /* We use `I' to indicate initialization and `D' to indicate
4153 function_key
= constructor_p
? 'I' : 'D';
4155 /* We emit the function lazily, to avoid generating empty
4156 global constructors and destructors. */
4159 /* For Objective-C++, we may need to initialize metadata found in this module.
4160 This must be done _before_ any other static initializations. */
4161 if (c_dialect_objc () && (priority
== DEFAULT_INIT_PRIORITY
)
4162 && constructor_p
&& objc_static_init_needed_p ())
4164 body
= start_objects (function_key
, priority
);
4165 objc_generate_static_init_call (NULL_TREE
);
4168 /* Call the static storage duration function with appropriate
4170 FOR_EACH_VEC_SAFE_ELT (ssdf_decls
, i
, fndecl
)
4172 /* Calls to pure or const functions will expand to nothing. */
4173 if (! (flags_from_decl_or_type (fndecl
) & (ECF_CONST
| ECF_PURE
)))
4178 body
= start_objects (function_key
, priority
);
4180 call
= cp_build_function_call_nary (fndecl
, tf_warning_or_error
,
4181 build_int_cst (NULL_TREE
,
4183 build_int_cst (NULL_TREE
,
4186 finish_expr_stmt (call
);
4190 /* Close out the function. */
4192 finish_objects (function_key
, priority
, body
);
4195 /* Generate constructor and destructor functions for the priority
4199 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n
, void * data
)
4201 location_t
*locus
= (location_t
*) data
;
4202 int priority
= (int) n
->key
;
4203 priority_info pi
= (priority_info
) n
->value
;
4205 /* Generate the functions themselves, but only if they are really
4207 if (pi
->initializations_p
)
4208 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority
, locus
);
4209 if (pi
->destructions_p
)
4210 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority
, locus
);
4212 /* Keep iterating. */
4216 /* Return C++ property of T, based on given operation OP. */
4219 cpp_check (tree t
, cpp_operation op
)
4223 case HAS_DEPENDENT_TEMPLATE_ARGS
:
4225 tree ti
= CLASSTYPE_TEMPLATE_INFO (t
);
4228 ++processing_template_decl
;
4229 const bool dep
= any_dependent_template_arguments_p (TI_ARGS (ti
));
4230 --processing_template_decl
;
4234 return DECL_PURE_VIRTUAL_P (t
);
4235 case IS_CONSTRUCTOR
:
4236 return DECL_CONSTRUCTOR_P (t
);
4238 return DECL_DESTRUCTOR_P (t
);
4239 case IS_COPY_CONSTRUCTOR
:
4240 return DECL_COPY_CONSTRUCTOR_P (t
);
4241 case IS_MOVE_CONSTRUCTOR
:
4242 return DECL_MOVE_CONSTRUCTOR_P (t
);
4244 return TREE_CODE (t
) == TEMPLATE_DECL
;
4246 return trivial_type_p (t
);
4252 /* Collect source file references recursively, starting from NAMESPC. */
4255 collect_source_refs (tree namespc
)
4257 /* Iterate over names in this name space. */
4258 for (tree t
= NAMESPACE_LEVEL (namespc
)->names
; t
; t
= TREE_CHAIN (t
))
4259 if (DECL_IS_BUILTIN (t
))
4261 else if (TREE_CODE (t
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (t
))
4262 collect_source_refs (t
);
4264 collect_source_ref (DECL_SOURCE_FILE (t
));
4267 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4268 starting from NAMESPC. */
4271 collect_ada_namespace (tree namespc
, const char *source_file
)
4273 tree decl
= NAMESPACE_LEVEL (namespc
)->names
;
4275 /* Collect decls from this namespace. This will skip
4276 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4277 collect_ada_nodes (decl
, source_file
);
4279 /* Now scan for namespace children, and dump them. */
4280 for (; decl
; decl
= TREE_CHAIN (decl
))
4281 if (TREE_CODE (decl
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (decl
))
4282 collect_ada_namespace (decl
, source_file
);
4285 /* Returns true iff there is a definition available for variable or
4289 decl_defined_p (tree decl
)
4291 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4292 return (DECL_INITIAL (decl
) != NULL_TREE
4293 /* A pending instantiation of a friend temploid is defined. */
4294 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
4295 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4296 (DECL_TI_TEMPLATE (decl
)))));
4299 gcc_assert (VAR_P (decl
));
4300 return !DECL_EXTERNAL (decl
);
4304 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4308 An integral constant-expression can only involve ... const
4309 variables of integral or enumeration types initialized with
4310 constant expressions ...
4312 C++0x also allows constexpr variables and temporaries initialized
4313 with constant expressions. We handle the former here, but the latter
4314 are just folded away in cxx_eval_constant_expression.
4316 The standard does not require that the expression be non-volatile.
4317 G++ implements the proposed correction in DR 457. */
4320 decl_constant_var_p (tree decl
)
4322 if (!decl_maybe_constant_var_p (decl
))
4325 /* We don't know if a template static data member is initialized with
4326 a constant expression until we instantiate its initializer. Even
4327 in the case of a constexpr variable, we can't treat it as a
4328 constant until its initializer is complete in case it's used in
4329 its own initializer. */
4330 maybe_instantiate_decl (decl
);
4331 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
);
4334 /* Returns true if DECL could be a symbolic constant variable, depending on
4338 decl_maybe_constant_var_p (tree decl
)
4340 tree type
= TREE_TYPE (decl
);
4343 if (DECL_DECLARED_CONSTEXPR_P (decl
) && !TREE_THIS_VOLATILE (decl
))
4345 if (DECL_HAS_VALUE_EXPR_P (decl
))
4346 /* A proxy isn't constant. */
4348 if (TYPE_REF_P (type
))
4349 /* References can be constant. */;
4350 else if (CP_TYPE_CONST_NON_VOLATILE_P (type
)
4351 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4352 /* And const integers. */;
4356 if (DECL_INITIAL (decl
)
4357 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
4358 /* We know the initializer, and it isn't constant. */
4364 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4365 called from grokfndecl and grokvardecl; in all modes it is called from
4366 cp_write_global_declarations. */
4369 no_linkage_error (tree decl
)
4371 if (cxx_dialect
>= cxx11
&& decl_defined_p (decl
))
4372 /* In C++11 it's ok if the decl is defined. */
4374 tree t
= no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false);
4376 /* The type that got us on no_linkage_decls must have gotten a name for
4377 linkage purposes. */;
4378 else if (CLASS_TYPE_P (t
) && TYPE_BEING_DEFINED (t
))
4379 /* The type might end up having a typedef name for linkage purposes. */
4380 vec_safe_push (no_linkage_decls
, decl
);
4381 else if (TYPE_UNNAMED_P (t
))
4384 auto_diagnostic_group grp
;
4385 if (cxx_dialect
>= cxx11
)
4386 d
= permerror (DECL_SOURCE_LOCATION (decl
), "%q#D, declared using "
4387 "unnamed type, is used but never defined", decl
);
4388 else if (DECL_EXTERN_C_P (decl
))
4389 /* Allow this; it's pretty common in C. */;
4390 else if (VAR_P (decl
))
4391 /* DRs 132, 319 and 389 seem to indicate types with
4392 no linkage can only be used to declare extern "C"
4393 entities. Since it's not always an error in the
4394 ISO C++ 90 Standard, we only issue a warning. */
4395 d
= warning_at (DECL_SOURCE_LOCATION (decl
), 0, "unnamed type "
4396 "with no linkage used to declare variable %q#D with "
4399 d
= permerror (DECL_SOURCE_LOCATION (decl
), "unnamed type with no "
4400 "linkage used to declare function %q#D with linkage",
4402 if (d
&& is_typedef_decl (TYPE_NAME (t
)))
4403 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)), "%q#D does not refer "
4404 "to the unqualified type, so it is not used for linkage",
4407 else if (cxx_dialect
>= cxx11
)
4409 if (VAR_P (decl
) || !DECL_PURE_VIRTUAL_P (decl
))
4410 permerror (DECL_SOURCE_LOCATION (decl
),
4411 "%q#D, declared using local type "
4412 "%qT, is used but never defined", decl
, t
);
4414 else if (VAR_P (decl
))
4415 warning_at (DECL_SOURCE_LOCATION (decl
), 0, "type %qT with no linkage "
4416 "used to declare variable %q#D with linkage", t
, decl
);
4418 permerror (DECL_SOURCE_LOCATION (decl
), "type %qT with no linkage used "
4419 "to declare function %q#D with linkage", t
, decl
);
4422 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4425 collect_all_refs (const char *source_file
)
4427 collect_ada_namespace (global_namespace
, source_file
);
4430 /* Clear DECL_EXTERNAL for NODE. */
4433 clear_decl_external (struct cgraph_node
*node
, void * /*data*/)
4435 DECL_EXTERNAL (node
->decl
) = 0;
4439 /* Build up the function to run dynamic initializers for thread_local
4440 variables in this translation unit and alias the init functions for the
4441 individual variables to it. */
4444 handle_tls_init (void)
4446 tree vars
= prune_vars_needing_no_initialization (&tls_aggregates
);
4447 if (vars
== NULL_TREE
)
4450 location_t loc
= DECL_SOURCE_LOCATION (TREE_VALUE (vars
));
4452 write_out_vars (vars
);
4454 tree guard
= build_decl (loc
, VAR_DECL
, get_identifier ("__tls_guard"),
4456 TREE_PUBLIC (guard
) = false;
4457 TREE_STATIC (guard
) = true;
4458 DECL_ARTIFICIAL (guard
) = true;
4459 DECL_IGNORED_P (guard
) = true;
4460 TREE_USED (guard
) = true;
4461 CP_DECL_THREAD_LOCAL_P (guard
) = true;
4462 set_decl_tls_model (guard
, decl_default_tls_model (guard
));
4463 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
4465 tree fn
= get_local_tls_init_fn (loc
);
4466 start_preparsed_function (fn
, NULL_TREE
, SF_PRE_PARSED
);
4467 tree body
= begin_function_body ();
4468 tree if_stmt
= begin_if_stmt ();
4469 tree cond
= cp_build_unary_op (TRUTH_NOT_EXPR
, guard
, false,
4470 tf_warning_or_error
);
4471 finish_if_stmt_cond (cond
, if_stmt
);
4472 finish_expr_stmt (cp_build_modify_expr (loc
, guard
, NOP_EXPR
,
4474 tf_warning_or_error
));
4475 for (; vars
; vars
= TREE_CHAIN (vars
))
4477 tree var
= TREE_VALUE (vars
);
4478 tree init
= TREE_PURPOSE (vars
);
4479 one_static_initialization_or_destruction (var
, init
, true);
4481 /* Output init aliases even with -fno-extern-tls-init. */
4482 if (TARGET_SUPPORTS_ALIASES
&& TREE_PUBLIC (var
))
4484 tree single_init_fn
= get_tls_init_fn (var
);
4485 if (single_init_fn
== NULL_TREE
)
4488 = cgraph_node::get_create (fn
)->create_same_body_alias
4489 (single_init_fn
, fn
);
4490 gcc_assert (alias
!= NULL
);
4494 finish_then_clause (if_stmt
);
4495 finish_if_stmt (if_stmt
);
4496 finish_function_body (body
);
4497 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4500 /* We're at the end of compilation, so generate any mangling aliases that
4501 we've been saving up, if DECL is going to be output and ID2 isn't
4502 already taken by another declaration. */
4505 generate_mangling_alias (tree decl
, tree id2
)
4507 struct cgraph_node
*n
= NULL
;
4509 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4511 n
= cgraph_node::get (decl
);
4513 /* Don't create an alias to an unreferenced function. */
4518 = mangled_decls
->find_slot_with_hash (id2
, IDENTIFIER_HASH_VALUE (id2
),
4521 /* If there's a declaration already using this mangled name,
4522 don't create a compatibility alias that conflicts. */
4526 tree alias
= make_alias_for (decl
, id2
);
4529 DECL_IGNORED_P (alias
) = 1;
4530 TREE_PUBLIC (alias
) = TREE_PUBLIC (decl
);
4531 DECL_VISIBILITY (alias
) = DECL_VISIBILITY (decl
);
4532 if (vague_linkage_p (decl
))
4533 DECL_WEAK (alias
) = 1;
4536 n
->create_same_body_alias (alias
, decl
);
4538 varpool_node::create_extra_name_alias (alias
, decl
);
4541 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4542 the end of translation, for compatibility across bugs in the mangling
4546 note_mangling_alias (tree decl
, tree id2
)
4548 if (TARGET_SUPPORTS_ALIASES
)
4550 if (!defer_mangling_aliases
)
4551 generate_mangling_alias (decl
, id2
);
4554 vec_safe_push (mangling_aliases
, decl
);
4555 vec_safe_push (mangling_aliases
, id2
);
4560 /* Emit all mangling aliases that were deferred up to this point. */
4563 generate_mangling_aliases ()
4565 while (!vec_safe_is_empty (mangling_aliases
))
4567 tree id2
= mangling_aliases
->pop();
4568 tree decl
= mangling_aliases
->pop();
4569 generate_mangling_alias (decl
, id2
);
4571 defer_mangling_aliases
= false;
4574 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4575 set. NEED_WARNING is true if we must warn about collisions. We do
4576 this to spot changes in mangling that may require compatibility
4580 record_mangling (tree decl
, bool need_warning
)
4583 mangled_decls
= hash_table
<mangled_decl_hash
>::create_ggc (499);
4585 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
4586 tree id
= DECL_ASSEMBLER_NAME_RAW (decl
);
4588 = mangled_decls
->find_slot_with_hash (id
, IDENTIFIER_HASH_VALUE (id
),
4591 /* If this is already an alias, remove the alias, because the real
4592 decl takes precedence. */
4593 if (*slot
&& DECL_ARTIFICIAL (*slot
) && DECL_IGNORED_P (*slot
))
4594 if (symtab_node
*n
= symtab_node::get (*slot
))
4595 if (n
->cpp_implicit_alias
)
4603 else if (need_warning
)
4605 error_at (DECL_SOURCE_LOCATION (decl
),
4606 "mangling of %q#D as %qE conflicts with a previous mangle",
4608 inform (DECL_SOURCE_LOCATION (*slot
),
4609 "previous mangling %q#D", *slot
);
4610 inform (DECL_SOURCE_LOCATION (decl
),
4611 "a later %<-fabi-version=%> (or =0)"
4612 " avoids this error with a change in mangling");
4617 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4618 any existing knowledge of DECL's mangled name meaning DECL. */
4621 overwrite_mangling (tree decl
, tree name
)
4623 if (tree id
= DECL_ASSEMBLER_NAME_RAW (decl
))
4624 if ((TREE_CODE (decl
) == VAR_DECL
4625 || TREE_CODE (decl
) == FUNCTION_DECL
)
4628 = mangled_decls
->find_slot_with_hash (id
, IDENTIFIER_HASH_VALUE (id
),
4632 mangled_decls
->clear_slot (slot
);
4634 /* If this is an alias, remove it from the symbol table. */
4635 if (DECL_ARTIFICIAL (decl
) && DECL_IGNORED_P (decl
))
4636 if (symtab_node
*n
= symtab_node::get (decl
))
4637 if (n
->cpp_implicit_alias
)
4641 DECL_ASSEMBLER_NAME_RAW (decl
) = name
;
4644 /* The entire file is now complete. If requested, dump everything
4651 if (FILE *stream
= dump_begin (raw_dump_id
, &flags
))
4653 dump_node (global_namespace
, flags
& ~TDF_SLIM
, stream
);
4654 dump_end (raw_dump_id
, stream
);
4658 static location_t locus_at_end_of_parsing
;
4660 /* Check the deallocation functions for CODE to see if we want to warn that
4661 only one was defined. */
4664 maybe_warn_sized_delete (enum tree_code code
)
4666 tree sized
= NULL_TREE
;
4667 tree unsized
= NULL_TREE
;
4669 for (ovl_iterator
iter (get_global_binding (ovl_op_identifier (false, code
)));
4673 /* We're only interested in usual deallocation functions. */
4674 if (!usual_deallocation_fn_p (fn
))
4676 if (FUNCTION_ARG_CHAIN (fn
) == void_list_node
)
4681 if (DECL_INITIAL (unsized
) && !DECL_INITIAL (sized
))
4682 warning_at (DECL_SOURCE_LOCATION (unsized
), OPT_Wsized_deallocation
,
4683 "the program should also define %qD", sized
);
4684 else if (!DECL_INITIAL (unsized
) && DECL_INITIAL (sized
))
4685 warning_at (DECL_SOURCE_LOCATION (sized
), OPT_Wsized_deallocation
,
4686 "the program should also define %qD", unsized
);
4689 /* Check the global deallocation functions to see if we want to warn about
4690 defining unsized without sized (or vice versa). */
4693 maybe_warn_sized_delete ()
4695 if (!flag_sized_deallocation
|| !warn_sized_deallocation
)
4697 maybe_warn_sized_delete (DELETE_EXPR
);
4698 maybe_warn_sized_delete (VEC_DELETE_EXPR
);
4701 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4702 look them up when evaluating non-type template parameters. Now we need to
4703 lower them to something the back end can understand. */
4709 FOR_EACH_VARIABLE (node
)
4711 tree d
= node
->decl
;
4712 if (tree init
= DECL_INITIAL (d
))
4713 DECL_INITIAL (d
) = cplus_expand_constant (init
);
4717 /* This routine is called at the end of compilation.
4718 Its job is to create all the code needed to initialize and
4719 destroy the global aggregates. We do the destruction
4720 first, since that way we only need to reverse the decls once. */
4723 c_parse_final_cleanups (void)
4728 unsigned ssdf_count
= 0;
4732 locus_at_end_of_parsing
= input_location
;
4735 /* Bad parse errors. Just forget about it. */
4736 if (! global_bindings_p () || current_class_type
4737 || !vec_safe_is_empty (decl_namespace_list
))
4740 /* This is the point to write out a PCH if we're doing that.
4741 In that case we do not want to do anything else. */
4744 /* Mangle all symbols at PCH creation time. */
4746 FOR_EACH_SYMBOL (node
)
4747 if (! is_a
<varpool_node
*> (node
)
4748 || ! DECL_HARD_REGISTER (node
->decl
))
4749 DECL_ASSEMBLER_NAME (node
->decl
);
4750 c_common_write_pch ();
4752 /* Ensure even the callers don't try to finalize the CU. */
4753 flag_syntax_only
= 1;
4757 timevar_stop (TV_PHASE_PARSING
);
4758 timevar_start (TV_PHASE_DEFERRED
);
4760 symtab
->process_same_body_aliases ();
4762 /* Handle -fdump-ada-spec[-slim] */
4763 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
4765 if (flag_dump_ada_spec_slim
)
4766 collect_source_ref (main_input_filename
);
4768 collect_source_refs (global_namespace
);
4770 dump_ada_specs (collect_all_refs
, cpp_check
);
4773 /* FIXME - huh? was input_line -= 1;*/
4775 /* We now have to write out all the stuff we put off writing out.
4778 o Template specializations that we have not yet instantiated,
4779 but which are needed.
4780 o Initialization and destruction for non-local objects with
4781 static storage duration. (Local objects with static storage
4782 duration are initialized when their scope is first entered,
4783 and are cleaned up via atexit.)
4784 o Virtual function tables.
4786 All of these may cause others to be needed. For example,
4787 instantiating one function may cause another to be needed, and
4788 generating the initializer for an object may cause templates to be
4789 instantiated, etc., etc. */
4791 emit_support_tinfos ();
4800 /* If there are templates that we've put off instantiating, do
4802 instantiate_pending_templates (retries
);
4805 /* Write out virtual tables as required. Writing out the
4806 virtual table for a template class may cause the
4807 instantiation of members of that class. If we write out
4808 vtables then we remove the class from our list so we don't
4809 have to look at it again. */
4810 for (i
= keyed_classes
->length ();
4811 keyed_classes
->iterate (--i
, &t
);)
4812 if (maybe_emit_vtables (t
))
4815 keyed_classes
->unordered_remove (i
);
4817 /* The input_location may have been changed during marking of
4819 input_location
= locus_at_end_of_parsing
;
4821 /* Write out needed type info variables. We have to be careful
4822 looping through unemitted decls, because emit_tinfo_decl may
4823 cause other variables to be needed. New elements will be
4824 appended, and we remove from the vector those that actually
4826 for (i
= unemitted_tinfo_decls
->length ();
4827 unemitted_tinfo_decls
->iterate (--i
, &t
);)
4828 if (emit_tinfo_decl (t
))
4831 unemitted_tinfo_decls
->unordered_remove (i
);
4834 /* The list of objects with static storage duration is built up
4835 in reverse order. We clear STATIC_AGGREGATES so that any new
4836 aggregates added during the initialization of these will be
4837 initialized in the correct order when we next come around the
4839 vars
= prune_vars_needing_no_initialization (&static_aggregates
);
4843 /* We need to start a new initialization function each time
4844 through the loop. That's because we need to know which
4845 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4846 isn't computed until a function is finished, and written
4847 out. That's a deficiency in the back end. When this is
4848 fixed, these initialization functions could all become
4849 inline, with resulting performance improvements. */
4852 /* Make sure the back end knows about all the variables. */
4853 write_out_vars (vars
);
4855 /* Set the line and file, so that it is obviously not from
4857 input_location
= locus_at_end_of_parsing
;
4858 ssdf_body
= start_static_storage_duration_function (ssdf_count
);
4860 /* First generate code to do all the initializations. */
4862 do_static_initialization_or_destruction (vars
, /*initp=*/true);
4864 /* Then, generate code to do all the destructions. Do these
4865 in reverse order so that the most recently constructed
4866 variable is the first destroyed. If we're using
4867 __cxa_atexit, then we don't need to do this; functions
4868 were registered at initialization time to destroy the
4870 if (!flag_use_cxa_atexit
&& vars
)
4872 vars
= nreverse (vars
);
4873 do_static_initialization_or_destruction (vars
, /*initp=*/false);
4878 /* Finish up the static storage duration function for this
4880 input_location
= locus_at_end_of_parsing
;
4881 finish_static_storage_duration_function (ssdf_body
);
4883 /* All those initializations and finalizations might cause
4884 us to need more inline functions, more template
4885 instantiations, etc. */
4888 /* ??? was: locus_at_end_of_parsing.line++; */
4891 /* Now do the same for thread_local variables. */
4894 /* Go through the set of inline functions whose bodies have not
4895 been emitted yet. If out-of-line copies of these functions
4896 are required, emit them. */
4897 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4899 /* Does it need synthesizing? */
4900 if (DECL_DEFAULTED_FN (decl
) && ! DECL_INITIAL (decl
)
4901 && (! DECL_REALLY_EXTERN (decl
) || possibly_inlined_p (decl
)))
4903 /* Even though we're already at the top-level, we push
4904 there again. That way, when we pop back a few lines
4905 hence, all of our state is restored. Otherwise,
4906 finish_function doesn't clean things up, and we end
4907 up with CURRENT_FUNCTION_DECL set. */
4908 push_to_top_level ();
4909 /* The decl's location will mark where it was first
4910 needed. Save that so synthesize method can indicate
4911 where it was needed from, in case of error */
4912 input_location
= DECL_SOURCE_LOCATION (decl
);
4913 synthesize_method (decl
);
4914 pop_from_top_level ();
4918 if (!DECL_INITIAL (decl
) && decl_tls_wrapper_p (decl
))
4919 generate_tls_wrapper (decl
);
4921 if (!DECL_SAVED_TREE (decl
))
4924 cgraph_node
*node
= cgraph_node::get_create (decl
);
4926 /* We lie to the back end, pretending that some functions
4927 are not defined when they really are. This keeps these
4928 functions from being put out unnecessarily. But, we must
4929 stop lying when the functions are referenced, or if they
4930 are not comdat since they need to be put out now. If
4931 DECL_INTERFACE_KNOWN, then we have already set
4932 DECL_EXTERNAL appropriately, so there's no need to check
4933 again, and we do not want to clear DECL_EXTERNAL if a
4934 previous call to import_export_decl set it.
4936 This is done in a separate for cycle, because if some
4937 deferred function is contained in another deferred
4938 function later in deferred_fns varray,
4939 rest_of_compilation would skip this function and we
4940 really cannot expand the same function twice. */
4941 import_export_decl (decl
);
4942 if (DECL_NOT_REALLY_EXTERN (decl
)
4943 && DECL_INITIAL (decl
)
4944 && decl_needed_p (decl
))
4946 if (node
->cpp_implicit_alias
)
4947 node
= node
->get_alias_target ();
4949 node
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
4951 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4952 group, we need to mark all symbols in the same comdat group
4954 if (node
->same_comdat_group
)
4955 for (cgraph_node
*next
4956 = dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
4958 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
4959 next
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
4963 /* If we're going to need to write this function out, and
4964 there's already a body for it, create RTL for it now.
4965 (There might be no body if this is a method we haven't
4966 gotten around to synthesizing yet.) */
4967 if (!DECL_EXTERNAL (decl
)
4968 && decl_needed_p (decl
)
4969 && !TREE_ASM_WRITTEN (decl
)
4970 && !node
->definition
)
4972 /* We will output the function; no longer consider it in this
4974 DECL_DEFER_OUTPUT (decl
) = 0;
4975 /* Generate RTL for this function now that we know we
4977 expand_or_defer_fn (decl
);
4982 if (wrapup_namespace_globals ())
4985 /* Static data members are just like namespace-scope globals. */
4986 FOR_EACH_VEC_SAFE_ELT (pending_statics
, i
, decl
)
4988 if (var_finalized_p (decl
) || DECL_REALLY_EXTERN (decl
)
4989 /* Don't write it out if we haven't seen a definition. */
4990 || DECL_IN_AGGR_P (decl
))
4992 import_export_decl (decl
);
4993 /* If this static data member is needed, provide it to the
4995 if (DECL_NOT_REALLY_EXTERN (decl
) && decl_needed_p (decl
))
4996 DECL_EXTERNAL (decl
) = 0;
4998 if (vec_safe_length (pending_statics
) != 0
4999 && wrapup_global_declarations (pending_statics
->address (),
5000 pending_statics
->length ()))
5009 generate_mangling_aliases ();
5011 /* All used inline functions must have a definition at this point. */
5012 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
5014 if (/* Check online inline functions that were actually used. */
5015 DECL_ODR_USED (decl
) && DECL_DECLARED_INLINE_P (decl
)
5016 /* If the definition actually was available here, then the
5017 fact that the function was not defined merely represents
5018 that for some reason (use of a template repository,
5019 #pragma interface, etc.) we decided not to emit the
5021 && !DECL_INITIAL (decl
)
5022 /* Don't complain if the template was defined. */
5023 && !(DECL_TEMPLATE_INSTANTIATION (decl
)
5024 && DECL_INITIAL (DECL_TEMPLATE_RESULT
5025 (template_for_substitution (decl
))))
5026 && warning_at (DECL_SOURCE_LOCATION (decl
), 0,
5027 "inline function %qD used but never defined", decl
))
5028 /* Avoid a duplicate warning from check_global_declaration. */
5029 TREE_NO_WARNING (decl
) = 1;
5032 /* So must decls that use a type with no linkage. */
5033 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls
, i
, decl
)
5034 no_linkage_error (decl
);
5036 maybe_warn_sized_delete ();
5038 /* Then, do the Objective-C stuff. This is where all the
5039 Objective-C module stuff gets generated (symtab,
5040 class/protocol/selector lists etc). This must be done after C++
5041 templates, destructors etc. so that selectors used in C++
5042 templates are properly allocated. */
5043 if (c_dialect_objc ())
5044 objc_write_global_declarations ();
5046 /* We give C linkage to static constructors and destructors. */
5047 push_lang_context (lang_name_c
);
5049 /* Generate initialization and destruction functions for all
5050 priorities for which they are required. */
5051 if (priority_info_map
)
5052 splay_tree_foreach (priority_info_map
,
5053 generate_ctor_and_dtor_functions_for_priority
,
5054 /*data=*/&locus_at_end_of_parsing
);
5055 else if (c_dialect_objc () && objc_static_init_needed_p ())
5056 /* If this is obj-c++ and we need a static init, call
5057 generate_ctor_or_dtor_function. */
5058 generate_ctor_or_dtor_function (/*constructor_p=*/true,
5059 DEFAULT_INIT_PRIORITY
,
5060 &locus_at_end_of_parsing
);
5062 /* We're done with the splay-tree now. */
5063 if (priority_info_map
)
5064 splay_tree_delete (priority_info_map
);
5066 /* Generate any missing aliases. */
5067 maybe_apply_pending_pragma_weaks ();
5069 /* We're done with static constructors, so we can go back to "C++"
5071 pop_lang_context ();
5073 if (flag_vtable_verify
)
5075 vtv_recover_class_info ();
5076 vtv_compute_class_hierarchy_transitive_closure ();
5077 vtv_build_vtable_verify_fndecl ();
5080 perform_deferred_noexcept_checks ();
5085 /* The entire file is now complete. If requested, dump everything
5089 if (flag_detailed_statistics
)
5091 dump_tree_statistics ();
5092 dump_time_statistics ();
5095 timevar_stop (TV_PHASE_DEFERRED
);
5096 timevar_start (TV_PHASE_PARSING
);
5098 /* Indicate that we're done with front end processing. */
5102 /* Perform any post compilation-proper cleanups for the C++ front-end.
5103 This should really go away. No front-end should need to do
5104 anything past the compilation process. */
5107 cxx_post_compilation_parsing_cleanups (void)
5109 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS
);
5111 if (flag_vtable_verify
)
5113 /* Generate the special constructor initialization function that
5114 calls __VLTRegisterPairs, and give it a very high
5115 initialization priority. This must be done after
5116 finalize_compilation_unit so that we have accurate
5117 information about which vtable will actually be emitted. */
5118 vtv_generate_init_routine ();
5121 input_location
= locus_at_end_of_parsing
;
5124 validate_conversion_obstack ();
5126 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS
);
5129 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5130 function to call in parse-tree form; it has not yet been
5131 semantically analyzed. ARGS are the arguments to the function.
5132 They have already been semantically analyzed. This may change
5136 build_offset_ref_call_from_tree (tree fn
, vec
<tree
, va_gc
> **args
,
5137 tsubst_flags_t complain
)
5140 vec
<tree
, va_gc
> *orig_args
= NULL
;
5145 object
= TREE_OPERAND (fn
, 0);
5147 if (processing_template_decl
)
5149 gcc_assert (TREE_CODE (fn
) == DOTSTAR_EXPR
5150 || TREE_CODE (fn
) == MEMBER_REF
);
5151 if (type_dependent_expression_p (fn
)
5152 || any_type_dependent_arguments_p (*args
))
5153 return build_min_nt_call_vec (fn
, *args
);
5155 orig_args
= make_tree_vector_copy (*args
);
5157 /* Transform the arguments and add the implicit "this"
5158 parameter. That must be done before the FN is transformed
5159 because we depend on the form of FN. */
5160 make_args_non_dependent (*args
);
5161 object
= build_non_dependent_expr (object
);
5162 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
5164 if (TREE_CODE (fn
) == DOTSTAR_EXPR
)
5165 object
= cp_build_addr_expr (object
, complain
);
5166 vec_safe_insert (*args
, 0, object
);
5168 /* Now that the arguments are done, transform FN. */
5169 fn
= build_non_dependent_expr (fn
);
5172 /* A qualified name corresponding to a bound pointer-to-member is
5173 represented as an OFFSET_REF:
5175 struct B { void g(); };
5177 void B::g() { (this->*p)(); } */
5178 if (TREE_CODE (fn
) == OFFSET_REF
)
5180 tree object_addr
= cp_build_addr_expr (object
, complain
);
5181 fn
= TREE_OPERAND (fn
, 1);
5182 fn
= get_member_function_from_ptrfunc (&object_addr
, fn
,
5184 vec_safe_insert (*args
, 0, object_addr
);
5187 if (CLASS_TYPE_P (TREE_TYPE (fn
)))
5188 expr
= build_op_call (fn
, args
, complain
);
5190 expr
= cp_build_function_call_vec (fn
, args
, complain
);
5191 if (processing_template_decl
&& expr
!= error_mark_node
)
5192 expr
= build_min_non_dep_call_vec (expr
, orig_fn
, orig_args
);
5194 if (orig_args
!= NULL
)
5195 release_tree_vector (orig_args
);
5202 check_default_args (tree x
)
5204 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
5205 bool saw_def
= false;
5206 bool noted_first_def
= false;
5207 int idx_of_first_default_arg
= 0;
5208 location_t loc_of_first_default_arg
= UNKNOWN_LOCATION
;
5209 int i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
5210 tree fndecl
= STRIP_TEMPLATE (x
);
5211 auto_diagnostic_group d
;
5212 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
5214 if (TREE_PURPOSE (arg
))
5219 idx_of_first_default_arg
= i
;
5220 location_t loc
= get_fndecl_argument_location (fndecl
, i
);
5221 if (loc
!= DECL_SOURCE_LOCATION (x
))
5222 loc_of_first_default_arg
= loc
;
5225 else if (saw_def
&& !PACK_EXPANSION_P (TREE_VALUE (arg
)))
5227 error_at (get_fndecl_argument_location (fndecl
, i
),
5228 "default argument missing for parameter %P of %q#D", i
, x
);
5229 if (loc_of_first_default_arg
!= UNKNOWN_LOCATION
5230 && !noted_first_def
)
5232 inform (loc_of_first_default_arg
,
5233 "...following parameter %P which has a default argument",
5234 idx_of_first_default_arg
);
5235 noted_first_def
= true;
5237 TREE_PURPOSE (arg
) = error_mark_node
;
5242 /* Return true if function DECL can be inlined. This is used to force
5243 instantiation of methods that might be interesting for inlining. */
5245 possibly_inlined_p (tree decl
)
5247 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
5248 if (DECL_UNINLINABLE (decl
))
5251 return DECL_DECLARED_INLINE_P (decl
);
5252 /* When optimizing, we might inline everything when flatten
5253 attribute or heuristics inlining for size or autoinlining
5258 /* Normally, we can wait until instantiation-time to synthesize DECL.
5259 However, if DECL is a static data member initialized with a constant
5260 or a constexpr function, we need it right now because a reference to
5261 such a data member or a call to such function is not value-dependent.
5262 For a function that uses auto in the return type, we need to instantiate
5263 it to find out its type. For OpenMP user defined reductions, we need
5264 them instantiated for reduction clauses which inline them by hand
5268 maybe_instantiate_decl (tree decl
)
5270 if (DECL_LANG_SPECIFIC (decl
)
5271 && DECL_TEMPLATE_INFO (decl
)
5272 && (decl_maybe_constant_var_p (decl
)
5273 || (TREE_CODE (decl
) == FUNCTION_DECL
5274 && DECL_OMP_DECLARE_REDUCTION_P (decl
))
5275 || undeduced_auto_decl (decl
))
5276 && !DECL_DECLARED_CONCEPT_P (decl
)
5277 && !uses_template_parms (DECL_TI_ARGS (decl
)))
5279 /* Instantiating a function will result in garbage collection. We
5280 must treat this situation as if we were within the body of a
5281 function so as to avoid collecting live data only referenced from
5282 the stack (such as overload resolution candidates). */
5284 instantiate_decl (decl
, /*defer_ok=*/false,
5285 /*expl_inst_class_mem_p=*/false);
5290 /* Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns whether or
5291 not a warning was emitted. */
5294 cp_warn_deprecated_use (tree decl
, tsubst_flags_t complain
)
5296 if (!(complain
& tf_warning
) || !decl
5297 || deprecated_state
== DEPRECATED_SUPPRESS
)
5300 if (!TREE_DEPRECATED (decl
))
5302 /* Perhaps this is a deprecated typedef. */
5303 if (TYPE_P (decl
) && TYPE_NAME (decl
))
5304 decl
= TYPE_NAME (decl
);
5306 if (!TREE_DEPRECATED (decl
))
5310 /* Don't warn within members of a deprecated type. */
5312 && currently_open_class (decl
))
5315 bool warned
= false;
5316 if (cxx_dialect
>= cxx11
5318 && DECL_ARTIFICIAL (decl
)
5319 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
5320 && copy_fn_p (decl
))
5322 if (warn_deprecated_copy
5323 /* Don't warn about system library classes (c++/86342). */
5324 && (!DECL_IN_SYSTEM_HEADER (decl
)
5325 || global_dc
->dc_warn_system_headers
))
5327 auto_diagnostic_group d
;
5328 tree ctx
= DECL_CONTEXT (decl
);
5329 tree other
= classtype_has_depr_implicit_copy (ctx
);
5330 int opt
= (DECL_DESTRUCTOR_P (other
)
5331 ? OPT_Wdeprecated_copy_dtor
5332 : OPT_Wdeprecated_copy
);
5333 warned
= warning (opt
, "implicitly-declared %qD is deprecated",
5336 inform (DECL_SOURCE_LOCATION (other
),
5337 "because %qT has user-provided %qD",
5342 warned
= warn_deprecated_use (decl
, NULL_TREE
);
5347 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5348 If DECL is a specialization or implicitly declared class member,
5349 generate the actual definition. Return false if something goes
5350 wrong, true otherwise. */
5353 mark_used (tree decl
, tsubst_flags_t complain
)
5355 /* If we're just testing conversions or resolving overloads, we
5356 don't want any permanent effects like forcing functions to be
5357 output or instantiating templates. */
5358 if ((complain
& tf_conv
))
5361 /* If DECL is a BASELINK for a single function, then treat it just
5362 like the DECL for the function. Otherwise, if the BASELINK is
5363 for an overloaded function, we don't know which function was
5364 actually used until after overload resolution. */
5365 if (BASELINK_P (decl
))
5367 decl
= BASELINK_FUNCTIONS (decl
);
5368 if (really_overloaded_fn (decl
))
5370 decl
= OVL_FIRST (decl
);
5373 /* Set TREE_USED for the benefit of -Wunused. */
5374 TREE_USED (decl
) = 1;
5375 /* And for structured bindings also the underlying decl. */
5376 if (DECL_DECOMPOSITION_P (decl
) && DECL_DECOMP_BASE (decl
))
5377 TREE_USED (DECL_DECOMP_BASE (decl
)) = 1;
5379 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5382 if (DECL_CLONED_FUNCTION_P (decl
))
5383 TREE_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
5385 /* Mark enumeration types as used. */
5386 if (TREE_CODE (decl
) == CONST_DECL
)
5387 used_types_insert (DECL_CONTEXT (decl
));
5389 if (TREE_CODE (decl
) == FUNCTION_DECL
5390 && !maybe_instantiate_noexcept (decl
, complain
))
5393 if (TREE_CODE (decl
) == FUNCTION_DECL
5394 && DECL_DELETED_FN (decl
))
5396 if (DECL_ARTIFICIAL (decl
)
5397 && DECL_CONV_FN_P (decl
)
5398 && LAMBDA_TYPE_P (DECL_CONTEXT (decl
)))
5399 /* We mark a lambda conversion op as deleted if we can't
5400 generate it properly; see maybe_add_lambda_conv_op. */
5401 sorry ("converting lambda that uses %<...%> to function pointer");
5402 else if (complain
& tf_error
)
5404 error ("use of deleted function %qD", decl
);
5405 if (!maybe_explain_implicit_delete (decl
))
5406 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
5411 cp_warn_deprecated_use (decl
, complain
);
5413 /* We can only check DECL_ODR_USED on variables or functions with
5414 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5415 might need special handling for. */
5416 if (!VAR_OR_FUNCTION_DECL_P (decl
)
5417 || DECL_LANG_SPECIFIC (decl
) == NULL
5418 || DECL_THUNK_P (decl
))
5420 if (!processing_template_decl
5421 && !require_deduced_type (decl
, complain
))
5426 /* We only want to do this processing once. We don't need to keep trying
5427 to instantiate inline templates, because unit-at-a-time will make sure
5428 we get them compiled before functions that want to inline them. */
5429 if (DECL_ODR_USED (decl
))
5432 /* Normally, we can wait until instantiation-time to synthesize DECL.
5433 However, if DECL is a static data member initialized with a constant
5434 or a constexpr function, we need it right now because a reference to
5435 such a data member or a call to such function is not value-dependent.
5436 For a function that uses auto in the return type, we need to instantiate
5437 it to find out its type. For OpenMP user defined reductions, we need
5438 them instantiated for reduction clauses which inline them by hand
5440 maybe_instantiate_decl (decl
);
5442 if (processing_template_decl
|| in_template_function ())
5445 /* Check this too in case we're within instantiate_non_dependent_expr. */
5446 if (DECL_TEMPLATE_INFO (decl
)
5447 && uses_template_parms (DECL_TI_ARGS (decl
)))
5450 if (!require_deduced_type (decl
, complain
))
5453 if (builtin_pack_fn_p (decl
))
5455 error ("use of built-in parameter pack %qD outside of a template",
5460 /* If we don't need a value, then we don't need to synthesize DECL. */
5461 if (cp_unevaluated_operand
|| in_discarded_stmt
)
5464 DECL_ODR_USED (decl
) = 1;
5465 if (DECL_CLONED_FUNCTION_P (decl
))
5466 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
5468 /* DR 757: A type without linkage shall not be used as the type of a
5469 variable or function with linkage, unless
5470 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5471 o the variable or function is not used (3.2 [basic.def.odr]) or is
5472 defined in the same translation unit. */
5473 if (cxx_dialect
> cxx98
5474 && decl_linkage (decl
) != lk_none
5475 && !DECL_EXTERN_C_P (decl
)
5476 && !DECL_ARTIFICIAL (decl
)
5477 && !decl_defined_p (decl
)
5478 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
5480 if (is_local_extern (decl
))
5481 /* There's no way to define a local extern, and adding it to
5482 the vector interferes with GC, so give an error now. */
5483 no_linkage_error (decl
);
5485 vec_safe_push (no_linkage_decls
, decl
);
5488 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (decl
)
5489 && !DECL_INITIAL (decl
) && !DECL_ARTIFICIAL (decl
))
5490 /* Remember it, so we can check it was defined. */
5491 note_vague_linkage_fn (decl
);
5493 /* Is it a synthesized method that needs to be synthesized? */
5494 if (TREE_CODE (decl
) == FUNCTION_DECL
5495 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
5496 && DECL_DEFAULTED_FN (decl
)
5497 /* A function defaulted outside the class is synthesized either by
5498 cp_finish_decl or instantiate_decl. */
5499 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl
)
5500 && ! DECL_INITIAL (decl
))
5502 /* Defer virtual destructors so that thunks get the right
5504 if (DECL_VIRTUAL_P (decl
) && !at_eof
)
5506 note_vague_linkage_fn (decl
);
5510 /* Remember the current location for a function we will end up
5511 synthesizing. Then we can inform the user where it was
5512 required in the case of error. */
5513 DECL_SOURCE_LOCATION (decl
) = input_location
;
5515 /* Synthesizing an implicitly defined member function will result in
5516 garbage collection. We must treat this situation as if we were
5517 within the body of a function so as to avoid collecting live data
5518 on the stack (such as overload resolution candidates).
5520 We could just let c_parse_final_cleanups handle synthesizing
5521 this function by adding it to deferred_fns, but doing
5522 it at the use site produces better error messages. */
5524 synthesize_method (decl
);
5526 /* If this is a synthesized method we don't need to
5527 do the instantiation test below. */
5529 else if (VAR_OR_FUNCTION_DECL_P (decl
)
5530 && DECL_TEMPLATE_INFO (decl
)
5531 && !DECL_DECLARED_CONCEPT_P (decl
)
5532 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
5533 || always_instantiate_p (decl
)))
5534 /* If this is a function or variable that is an instance of some
5535 template, we now know that we will need to actually do the
5536 instantiation. We check that DECL is not an explicit
5537 instantiation because that is not checked in instantiate_decl.
5539 We put off instantiating functions in order to improve compile
5540 times. Maintaining a stack of active functions is expensive,
5541 and the inliner knows to instantiate any functions it might
5542 need. Therefore, we always try to defer instantiation. */
5545 instantiate_decl (decl
, /*defer_ok=*/true,
5546 /*expl_inst_class_mem_p=*/false);
5554 mark_used (tree decl
)
5556 return mark_used (decl
, tf_warning_or_error
);
5560 vtv_start_verification_constructor_init_function (void)
5562 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY
- 1);
5566 vtv_finish_verification_constructor_init_function (tree function_body
)
5570 finish_compound_stmt (function_body
);
5571 fn
= finish_function (/*inline_p=*/false);
5572 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
5573 decl_init_priority_insert (fn
, MAX_RESERVED_INIT_PRIORITY
- 1);
5578 #include "gt-cp-decl2.h"