1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 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
)
161 bool late_return_type_p
;
163 if (fntype
== error_mark_node
|| ctype
== error_mark_node
)
164 return error_mark_node
;
166 gcc_assert (TREE_CODE (fntype
) == FUNCTION_TYPE
167 || TREE_CODE (fntype
) == METHOD_TYPE
);
169 type_quals
= quals
& ~TYPE_QUAL_RESTRICT
;
170 ctype
= cp_build_qualified_type (ctype
, type_quals
);
171 raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
172 attrs
= TYPE_ATTRIBUTES (fntype
);
173 late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (fntype
);
174 fntype
= build_method_type_directly (ctype
, TREE_TYPE (fntype
),
175 (TREE_CODE (fntype
) == METHOD_TYPE
176 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype
))
177 : TYPE_ARG_TYPES (fntype
)));
179 fntype
= cp_build_type_attribute_variant (fntype
, attrs
);
181 fntype
= build_ref_qualified_type (fntype
, rqual
);
183 fntype
= build_exception_variant (fntype
, raises
);
184 if (late_return_type_p
)
185 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
190 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
191 return type changed to NEW_RET. */
194 change_return_type (tree new_ret
, tree fntype
)
197 tree args
= TYPE_ARG_TYPES (fntype
);
198 tree raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
199 tree attrs
= TYPE_ATTRIBUTES (fntype
);
200 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (fntype
);
202 if (new_ret
== error_mark_node
)
205 if (same_type_p (new_ret
, TREE_TYPE (fntype
)))
208 if (TREE_CODE (fntype
) == FUNCTION_TYPE
)
210 newtype
= build_function_type (new_ret
, args
);
211 newtype
= apply_memfn_quals (newtype
,
212 type_memfn_quals (fntype
),
213 type_memfn_rqual (fntype
));
216 newtype
= build_method_type_directly
217 (class_of_this_parm (fntype
), new_ret
, TREE_CHAIN (args
));
218 if (FUNCTION_REF_QUALIFIED (fntype
))
219 newtype
= build_ref_qualified_type (newtype
, type_memfn_rqual (fntype
));
221 newtype
= build_exception_variant (newtype
, raises
);
223 newtype
= cp_build_type_attribute_variant (newtype
, attrs
);
224 if (late_return_type_p
)
225 TYPE_HAS_LATE_RETURN_TYPE (newtype
) = 1;
230 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
234 cp_build_parm_decl (tree fn
, tree name
, tree type
)
236 tree parm
= build_decl (input_location
,
237 PARM_DECL
, name
, type
);
238 DECL_CONTEXT (parm
) = fn
;
240 /* DECL_ARG_TYPE is only used by the back end and the back end never
242 if (!processing_template_decl
)
243 DECL_ARG_TYPE (parm
) = type_passed_as (type
);
248 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
252 build_artificial_parm (tree fn
, tree name
, tree type
)
254 tree parm
= cp_build_parm_decl (fn
, name
, type
);
255 DECL_ARTIFICIAL (parm
) = 1;
256 /* All our artificial parms are implicitly `const'; they cannot be
258 TREE_READONLY (parm
) = 1;
262 /* Constructors for types with virtual baseclasses need an "in-charge" flag
263 saying whether this constructor is responsible for initialization of
264 virtual baseclasses or not. All destructors also need this "in-charge"
265 flag, which additionally determines whether or not the destructor should
266 free the memory for the object.
268 This function adds the "in-charge" flag to member function FN if
269 appropriate. It is called from grokclassfn and tsubst.
270 FN must be either a constructor or destructor.
272 The in-charge flag follows the 'this' parameter, and is followed by the
273 VTT parm (if any), then the user-written parms. */
276 maybe_retrofit_in_chrg (tree fn
)
278 tree basetype
, arg_types
, parms
, parm
, fntype
;
280 /* If we've already add the in-charge parameter don't do it again. */
281 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
284 /* When processing templates we can't know, in general, whether or
285 not we're going to have virtual baseclasses. */
286 if (processing_template_decl
)
289 /* We don't need an in-charge parameter for constructors that don't
290 have virtual bases. */
291 if (DECL_CONSTRUCTOR_P (fn
)
292 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
295 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
296 basetype
= TREE_TYPE (TREE_VALUE (arg_types
));
297 arg_types
= TREE_CHAIN (arg_types
);
299 parms
= DECL_CHAIN (DECL_ARGUMENTS (fn
));
301 /* If this is a subobject constructor or destructor, our caller will
302 pass us a pointer to our VTT. */
303 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
305 parm
= build_artificial_parm (fn
, vtt_parm_identifier
, vtt_parm_type
);
307 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
308 DECL_CHAIN (parm
) = parms
;
311 /* ...and then to TYPE_ARG_TYPES. */
312 arg_types
= hash_tree_chain (vtt_parm_type
, arg_types
);
314 DECL_HAS_VTT_PARM_P (fn
) = 1;
317 /* Then add the in-charge parm (before the VTT parm). */
318 parm
= build_artificial_parm (fn
, in_charge_identifier
, integer_type_node
);
319 DECL_CHAIN (parm
) = parms
;
321 arg_types
= hash_tree_chain (integer_type_node
, arg_types
);
323 /* Insert our new parameter(s) into the list. */
324 DECL_CHAIN (DECL_ARGUMENTS (fn
)) = parms
;
326 /* And rebuild the function type. */
327 fntype
= build_method_type_directly (basetype
, TREE_TYPE (TREE_TYPE (fn
)),
329 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)))
330 fntype
= build_exception_variant (fntype
,
331 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)));
332 if (TYPE_ATTRIBUTES (TREE_TYPE (fn
)))
333 fntype
= (cp_build_type_attribute_variant
334 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (fn
))));
335 TREE_TYPE (fn
) = fntype
;
337 /* Now we've got the in-charge parameter. */
338 DECL_HAS_IN_CHARGE_PARM_P (fn
) = 1;
341 /* Classes overload their constituent function names automatically.
342 When a function name is declared in a record structure,
343 its name is changed to it overloaded name. Since names for
344 constructors and destructors can conflict, we place a leading
347 CNAME is the name of the class we are grokking for.
349 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
351 FLAGS contains bits saying what's special about today's
352 arguments. DTOR_FLAG == DESTRUCTOR.
354 If FUNCTION is a destructor, then we must add the `auto-delete' field
355 as a second parameter. There is some hair associated with the fact
356 that we must "declare" this variable in the manner consistent with the
357 way the rest of the arguments were declared.
359 QUALS are the qualifiers for the this pointer. */
362 grokclassfn (tree ctype
, tree function
, enum overload_flags flags
)
364 tree fn_name
= DECL_NAME (function
);
366 /* Even within an `extern "C"' block, members get C++ linkage. See
367 [dcl.link] for details. */
368 SET_DECL_LANGUAGE (function
, lang_cplusplus
);
370 if (fn_name
== NULL_TREE
)
372 error ("name missing for member function");
373 fn_name
= get_identifier ("<anonymous>");
374 DECL_NAME (function
) = fn_name
;
377 DECL_CONTEXT (function
) = ctype
;
379 if (flags
== DTOR_FLAG
)
380 DECL_CXX_DESTRUCTOR_P (function
) = 1;
382 if (flags
== DTOR_FLAG
|| DECL_CONSTRUCTOR_P (function
))
383 maybe_retrofit_in_chrg (function
);
386 /* Create an ARRAY_REF, checking for the user doing things backwards
387 along the way. DECLTYPE_P is for N3276, as in the parser. */
390 grok_array_decl (location_t loc
, tree array_expr
, tree index_exp
,
395 tree orig_array_expr
= array_expr
;
396 tree orig_index_exp
= index_exp
;
397 tree overload
= NULL_TREE
;
399 if (error_operand_p (array_expr
) || error_operand_p (index_exp
))
400 return error_mark_node
;
402 if (processing_template_decl
)
404 if (type_dependent_expression_p (array_expr
)
405 || type_dependent_expression_p (index_exp
))
406 return build_min_nt_loc (loc
, ARRAY_REF
, array_expr
, index_exp
,
407 NULL_TREE
, NULL_TREE
);
408 array_expr
= build_non_dependent_expr (array_expr
);
409 index_exp
= build_non_dependent_expr (index_exp
);
412 type
= TREE_TYPE (array_expr
);
414 type
= non_reference (type
);
416 /* If they have an `operator[]', use that. */
417 if (MAYBE_CLASS_TYPE_P (type
) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp
)))
419 tsubst_flags_t complain
= tf_warning_or_error
;
421 complain
|= tf_decltype
;
422 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, array_expr
,
423 index_exp
, NULL_TREE
, &overload
, complain
);
429 /* Otherwise, create an ARRAY_REF for a pointer or array type.
430 It is a little-known fact that, if `a' is an array and `i' is
431 an int, you can write `i[a]', which means the same thing as
433 if (TREE_CODE (type
) == ARRAY_TYPE
|| VECTOR_TYPE_P (type
))
436 p1
= build_expr_type_conversion (WANT_POINTER
, array_expr
, false);
438 if (TREE_CODE (TREE_TYPE (index_exp
)) == ARRAY_TYPE
)
441 p2
= build_expr_type_conversion (WANT_POINTER
, index_exp
, false);
443 i1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, array_expr
,
445 i2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, index_exp
,
448 if ((p1
&& i2
) && (i1
&& p2
))
449 error ("ambiguous conversion for array subscript");
452 array_expr
= p1
, index_exp
= i2
;
454 array_expr
= p2
, index_exp
= i1
;
457 error ("invalid types %<%T[%T]%> for array subscript",
458 type
, TREE_TYPE (index_exp
));
459 return error_mark_node
;
462 if (array_expr
== error_mark_node
|| index_exp
== error_mark_node
)
463 error ("ambiguous conversion for array subscript");
465 if (TREE_CODE (TREE_TYPE (array_expr
)) == POINTER_TYPE
)
466 array_expr
= mark_rvalue_use (array_expr
);
468 array_expr
= mark_lvalue_use_nonread (array_expr
);
469 index_exp
= mark_rvalue_use (index_exp
);
470 expr
= build_array_ref (input_location
, array_expr
, index_exp
);
472 if (processing_template_decl
&& expr
!= error_mark_node
)
474 if (overload
!= NULL_TREE
)
475 return (build_min_non_dep_op_overload
476 (ARRAY_REF
, expr
, overload
, orig_array_expr
, orig_index_exp
));
478 return build_min_non_dep (ARRAY_REF
, expr
, orig_array_expr
, orig_index_exp
,
479 NULL_TREE
, NULL_TREE
);
484 /* Given the cast expression EXP, checking out its validity. Either return
485 an error_mark_node if there was an unavoidable error, return a cast to
486 void for trying to delete a pointer w/ the value 0, or return the
487 call to delete. If DOING_VEC is true, we handle things differently
488 for doing an array delete.
489 Implements ARM $5.3.4. This is called from the parser. */
492 delete_sanity (tree exp
, tree size
, bool doing_vec
, int use_global_delete
,
493 tsubst_flags_t complain
)
497 if (exp
== error_mark_node
)
500 if (processing_template_decl
)
502 t
= build_min (DELETE_EXPR
, void_type_node
, exp
, size
);
503 DELETE_EXPR_USE_GLOBAL (t
) = use_global_delete
;
504 DELETE_EXPR_USE_VEC (t
) = doing_vec
;
505 TREE_SIDE_EFFECTS (t
) = 1;
509 /* An array can't have been allocated by new, so complain. */
510 if (TREE_CODE (TREE_TYPE (exp
)) == ARRAY_TYPE
)
511 warning (0, "deleting array %q#E", exp
);
513 t
= build_expr_type_conversion (WANT_POINTER
, exp
, true);
515 if (t
== NULL_TREE
|| t
== error_mark_node
)
517 error ("type %q#T argument given to %<delete%>, expected pointer",
519 return error_mark_node
;
522 type
= TREE_TYPE (t
);
524 /* As of Valley Forge, you can delete a pointer to const. */
526 /* You can't delete functions. */
527 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
529 error ("cannot delete a function. Only pointer-to-objects are "
530 "valid arguments to %<delete%>");
531 return error_mark_node
;
534 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
535 if (VOID_TYPE_P (TREE_TYPE (type
)))
537 warning (OPT_Wdelete_incomplete
, "deleting %qT is undefined", type
);
541 /* Deleting a pointer with the value zero is valid and has no effect. */
542 if (integer_zerop (t
))
543 return build1 (NOP_EXPR
, void_type_node
, t
);
546 return build_vec_delete (t
, /*maxindex=*/NULL_TREE
,
547 sfk_deleting_destructor
,
548 use_global_delete
, complain
);
550 return build_delete (type
, t
, sfk_deleting_destructor
,
551 LOOKUP_NORMAL
, use_global_delete
,
555 /* Report an error if the indicated template declaration is not the
556 sort of thing that should be a member template. */
559 check_member_template (tree tmpl
)
563 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
564 decl
= DECL_TEMPLATE_RESULT (tmpl
);
566 if (TREE_CODE (decl
) == FUNCTION_DECL
567 || DECL_ALIAS_TEMPLATE_P (tmpl
)
568 || (TREE_CODE (decl
) == TYPE_DECL
569 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))))
571 /* The parser rejects template declarations in local classes
572 (with the exception of generic lambdas). */
573 gcc_assert (!current_function_decl
|| LAMBDA_FUNCTION_P (decl
));
574 /* The parser rejects any use of virtual in a function template. */
575 gcc_assert (!(TREE_CODE (decl
) == FUNCTION_DECL
576 && DECL_VIRTUAL_P (decl
)));
578 /* The debug-information generating code doesn't know what to do
579 with member templates. */
580 DECL_IGNORED_P (tmpl
) = 1;
582 else if (variable_template_p (tmpl
))
585 error ("template declaration of %q#D", decl
);
588 /* Sanity check: report error if this function FUNCTION is not
589 really a member of the class (CTYPE) it is supposed to belong to.
590 TEMPLATE_PARMS is used to specify the template parameters of a member
591 template passed as FUNCTION_DECL. If the member template is passed as a
592 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
593 from the declaration. If the function is not a function template, it
595 It returns the original declaration for the function, NULL_TREE if
596 no declaration was found, error_mark_node if an error was emitted. */
599 check_classfn (tree ctype
, tree function
, tree template_parms
)
601 if (DECL_USE_TEMPLATE (function
)
602 && !(TREE_CODE (function
) == TEMPLATE_DECL
603 && DECL_TEMPLATE_SPECIALIZATION (function
))
604 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function
)))
605 /* Since this is a specialization of a member template,
606 we're not going to find the declaration in the class.
609 struct S { template <typename T> void f(T); };
610 template <> void S::f(int);
612 we're not going to find `S::f(int)', but there's no
613 reason we should, either. We let our callers know we didn't
614 find the method, but we don't complain. */
617 /* Basic sanity check: for a template function, the template parameters
618 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
619 if (TREE_CODE (function
) == TEMPLATE_DECL
)
622 && !comp_template_parms (template_parms
,
623 DECL_TEMPLATE_PARMS (function
)))
625 error ("template parameter lists provided don%'t match the "
626 "template parameters of %qD", function
);
627 return error_mark_node
;
629 template_parms
= DECL_TEMPLATE_PARMS (function
);
632 /* OK, is this a definition of a member template? */
633 bool is_template
= (template_parms
!= NULL_TREE
);
637 A destructor shall not be a member template. */
638 if (DECL_DESTRUCTOR_P (function
) && is_template
)
640 error ("destructor %qD declared as member template", function
);
641 return error_mark_node
;
644 /* We must enter the scope here, because conversion operators are
645 named by target type, and type equivalence relies on typenames
646 resolving within the scope of CTYPE. */
647 tree pushed_scope
= push_scope (ctype
);
648 tree matched
= NULL_TREE
;
649 tree fns
= get_class_binding (ctype
, DECL_NAME (function
));
651 for (ovl_iterator
iter (fns
); !matched
&& iter
; ++iter
)
655 /* A member template definition only matches a member template
657 if (is_template
!= (TREE_CODE (fndecl
) == TEMPLATE_DECL
))
660 if (!DECL_DECLARES_FUNCTION_P (fndecl
))
663 tree p1
= TYPE_ARG_TYPES (TREE_TYPE (function
));
664 tree p2
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
666 /* We cannot simply call decls_match because this doesn't work
667 for static member functions that are pretending to be
668 methods, and because the name may have been changed by
671 /* Get rid of the this parameter on functions that become
673 if (DECL_STATIC_FUNCTION_P (fndecl
)
674 && TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
675 p1
= TREE_CHAIN (p1
);
677 /* ref-qualifier or absence of same must match. */
678 if (type_memfn_rqual (TREE_TYPE (function
))
679 != type_memfn_rqual (TREE_TYPE (fndecl
)))
682 // Include constraints in the match.
683 tree c1
= get_constraints (function
);
684 tree c2
= get_constraints (fndecl
);
686 /* While finding a match, same types and params are not enough
687 if the function is versioned. Also check version ("target")
689 if (same_type_p (TREE_TYPE (TREE_TYPE (function
)),
690 TREE_TYPE (TREE_TYPE (fndecl
)))
691 && compparms (p1
, p2
)
692 && !targetm
.target_option
.function_versions (function
, fndecl
)
694 || comp_template_parms (template_parms
,
695 DECL_TEMPLATE_PARMS (fndecl
)))
696 && equivalent_constraints (c1
, c2
)
697 && (DECL_TEMPLATE_SPECIALIZATION (function
)
698 == DECL_TEMPLATE_SPECIALIZATION (fndecl
))
699 && (!DECL_TEMPLATE_SPECIALIZATION (function
)
700 || (DECL_TI_TEMPLATE (function
) == DECL_TI_TEMPLATE (fndecl
))))
706 if (!COMPLETE_TYPE_P (ctype
))
707 cxx_incomplete_type_error (function
, ctype
);
710 if (DECL_CONV_FN_P (function
))
711 fns
= get_class_binding (ctype
, conv_op_identifier
);
713 error_at (DECL_SOURCE_LOCATION (function
),
714 "no declaration matches %q#D", function
);
716 print_candidates (fns
);
717 else if (DECL_CONV_FN_P (function
))
718 inform (DECL_SOURCE_LOCATION (function
),
719 "no conversion operators declared");
721 inform (DECL_SOURCE_LOCATION (function
),
722 "no functions named %qD", function
);
723 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype
)),
724 "%#qT defined here", ctype
);
726 matched
= error_mark_node
;
730 pop_scope (pushed_scope
);
735 /* DECL is a function with vague linkage. Remember it so that at the
736 end of the translation unit we can decide whether or not to emit
740 note_vague_linkage_fn (tree decl
)
742 DECL_DEFER_OUTPUT (decl
) = 1;
743 vec_safe_push (deferred_fns
, decl
);
746 /* As above, but for variable template instantiations. */
749 note_variable_template_instantiation (tree decl
)
751 vec_safe_push (pending_statics
, decl
);
754 /* We have just processed the DECL, which is a static data member.
755 The other parameters are as for cp_finish_decl. */
758 finish_static_data_member_decl (tree decl
,
759 tree init
, bool init_const_expr_p
,
763 DECL_CONTEXT (decl
) = current_class_type
;
765 /* We cannot call pushdecl here, because that would fill in the
766 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
767 the right thing, namely, to put this decl out straight away. */
769 if (! processing_template_decl
)
770 vec_safe_push (pending_statics
, decl
);
772 if (LOCAL_CLASS_P (current_class_type
)
773 /* We already complained about the template definition. */
774 && !DECL_TEMPLATE_INSTANTIATION (decl
))
775 permerror (input_location
, "local class %q#T shall not have static data member %q#D",
776 current_class_type
, decl
);
778 for (tree t
= current_class_type
; TYPE_P (t
);
779 t
= CP_TYPE_CONTEXT (t
))
780 if (TYPE_UNNAMED_P (t
))
782 if (permerror (DECL_SOURCE_LOCATION (decl
),
783 "static data member %qD in unnamed class", decl
))
784 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)),
785 "unnamed class defined here");
789 DECL_IN_AGGR_P (decl
) = 1;
791 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
792 && TYPE_DOMAIN (TREE_TYPE (decl
)) == NULL_TREE
)
793 SET_VAR_HAD_UNKNOWN_BOUND (decl
);
797 /* Similarly to start_decl_1, we want to complete the type in order
798 to do the right thing in cp_apply_type_quals_to_decl, possibly
799 clear TYPE_QUAL_CONST (c++/65579). */
800 tree type
= TREE_TYPE (decl
) = complete_type (TREE_TYPE (decl
));
801 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
804 cp_finish_decl (decl
, init
, init_const_expr_p
, asmspec_tree
, flags
);
807 /* DECLARATOR and DECLSPECS correspond to a class member. The other
808 parameters are as for cp_finish_decl. Return the DECL for the
809 class member declared. */
812 grokfield (const cp_declarator
*declarator
,
813 cp_decl_specifier_seq
*declspecs
,
814 tree init
, bool init_const_expr_p
,
819 const char *asmspec
= 0;
824 && TREE_CODE (init
) == TREE_LIST
825 && TREE_VALUE (init
) == error_mark_node
826 && TREE_CHAIN (init
) == NULL_TREE
)
829 value
= grokdeclarator (declarator
, declspecs
, FIELD
, init
!= 0, &attrlist
);
830 if (! value
|| value
== error_mark_node
)
831 /* friend or constructor went bad. */
832 return error_mark_node
;
833 if (TREE_TYPE (value
) == error_mark_node
)
836 if (TREE_CODE (value
) == TYPE_DECL
&& init
)
838 error ("typedef %qD is initialized (use decltype instead)", value
);
842 /* Pass friendly classes back. */
843 if (value
== void_type_node
)
847 name
= DECL_NAME (value
);
849 if (name
!= NULL_TREE
)
851 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
853 error ("explicit template argument list not allowed");
854 return error_mark_node
;
857 if (IDENTIFIER_POINTER (name
)[0] == '_'
858 && id_equal (name
, "_vptr"))
859 error ("member %qD conflicts with virtual function table field name",
863 /* Stash away type declarations. */
864 if (TREE_CODE (value
) == TYPE_DECL
)
866 DECL_NONLOCAL (value
) = 1;
867 DECL_CONTEXT (value
) = current_class_type
;
873 /* If this is a typedef that names the class for linkage purposes
874 (7.1.3p8), apply any attributes directly to the type. */
875 if (OVERLOAD_TYPE_P (TREE_TYPE (value
))
876 && value
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
877 attrflags
= ATTR_FLAG_TYPE_IN_PLACE
;
879 cplus_decl_attributes (&value
, attrlist
, attrflags
);
882 if (decl_spec_seq_has_spec_p (declspecs
, ds_typedef
)
883 && TREE_TYPE (value
) != error_mark_node
884 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))) != value
)
885 set_underlying_type (value
);
887 /* It's important that push_template_decl below follows
888 set_underlying_type above so that the created template
889 carries the properly set type of VALUE. */
890 if (processing_template_decl
)
891 value
= push_template_decl (value
);
893 record_locally_defined_typedef (value
);
897 int friendp
= decl_spec_seq_has_spec_p (declspecs
, ds_friend
);
899 if (!friendp
&& DECL_IN_AGGR_P (value
))
901 error ("%qD is already defined in %qT", value
, DECL_CONTEXT (value
));
902 return void_type_node
;
905 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
906 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
910 if (TREE_CODE (value
) == FUNCTION_DECL
)
912 if (init
== ridpointers
[(int)RID_DELETE
])
914 DECL_DELETED_FN (value
) = 1;
915 DECL_DECLARED_INLINE_P (value
) = 1;
916 DECL_INITIAL (value
) = error_mark_node
;
918 else if (init
== ridpointers
[(int)RID_DEFAULT
])
920 if (defaultable_fn_check (value
))
922 DECL_DEFAULTED_FN (value
) = 1;
923 DECL_INITIALIZED_IN_CLASS_P (value
) = 1;
924 DECL_DECLARED_INLINE_P (value
) = 1;
927 else if (TREE_CODE (init
) == DEFAULT_ARG
)
928 error ("invalid initializer for member function %qD", value
);
929 else if (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
)
931 if (integer_zerop (init
))
932 DECL_PURE_VIRTUAL_P (value
) = 1;
933 else if (error_operand_p (init
))
934 ; /* An error has already been reported. */
936 error ("invalid initializer for member function %qD",
941 gcc_assert (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
);
943 error ("initializer specified for friend function %qD",
946 error ("initializer specified for static member function %qD",
950 else if (TREE_CODE (value
) == FIELD_DECL
)
951 /* C++11 NSDMI, keep going. */;
952 else if (!VAR_P (value
))
956 /* Pass friend decls back. */
957 if ((TREE_CODE (value
) == FUNCTION_DECL
958 || TREE_CODE (value
) == TEMPLATE_DECL
)
959 && DECL_CONTEXT (value
) != current_class_type
)
962 /* Need to set this before push_template_decl. */
964 DECL_CONTEXT (value
) = current_class_type
;
966 if (processing_template_decl
&& VAR_OR_FUNCTION_DECL_P (value
))
968 value
= push_template_decl (value
);
969 if (error_operand_p (value
))
970 return error_mark_node
;
974 cplus_decl_attributes (&value
, attrlist
, 0);
976 if (init
&& DIRECT_LIST_INIT_P (init
))
977 flags
= LOOKUP_NORMAL
;
979 flags
= LOOKUP_IMPLICIT
;
981 switch (TREE_CODE (value
))
984 finish_static_data_member_decl (value
, init
, init_const_expr_p
,
985 asmspec_tree
, flags
);
990 error ("%<asm%> specifiers are not permitted on non-static data members");
991 if (DECL_INITIAL (value
) == error_mark_node
)
992 init
= error_mark_node
;
993 cp_finish_decl (value
, init
, /*init_const_expr_p=*/false,
995 DECL_IN_AGGR_P (value
) = 1;
1000 set_user_assembler_name (value
, asmspec
);
1002 cp_finish_decl (value
,
1004 /*init_const_expr_p=*/false,
1005 asmspec_tree
, flags
);
1007 /* Pass friends back this way. */
1008 if (DECL_FRIEND_P (value
))
1009 return void_type_node
;
1011 DECL_IN_AGGR_P (value
) = 1;
1020 /* Like `grokfield', but for bitfields.
1021 WIDTH is the width of the bitfield, a constant expression.
1022 The other parameters are as for grokfield. */
1025 grokbitfield (const cp_declarator
*declarator
,
1026 cp_decl_specifier_seq
*declspecs
, tree width
, tree init
,
1029 tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
,
1030 init
!= NULL_TREE
, &attrlist
);
1032 if (value
== error_mark_node
)
1033 return NULL_TREE
; /* friends went bad. */
1034 if (TREE_TYPE (value
) == 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 (TREE_TYPE (value
))
1042 && (POINTER_TYPE_P (value
)
1043 || !dependent_type_p (TREE_TYPE (value
))))
1045 error ("bit-field %qD with non-integral type", value
);
1046 return error_mark_node
;
1049 if (TREE_CODE (value
) == TYPE_DECL
)
1051 error ("cannot declare %qD to be a bit-field type", value
);
1055 /* Usually, finish_struct_1 catches bitfields with invalid types.
1056 But, in the case of bitfields with function type, we confuse
1057 ourselves into thinking they are member functions, so we must
1059 if (TREE_CODE (value
) == FUNCTION_DECL
)
1061 error ("cannot declare bit-field %qD with function type",
1066 if (width
&& TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value
)))
1068 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1073 if (DECL_IN_AGGR_P (value
))
1075 error ("%qD is already defined in the class %qT", value
,
1076 DECL_CONTEXT (value
));
1077 return void_type_node
;
1080 if (TREE_STATIC (value
))
1082 error ("static member %qD cannot be a bit-field", value
);
1086 int flags
= LOOKUP_IMPLICIT
;
1087 if (init
&& DIRECT_LIST_INIT_P (init
))
1088 flags
= LOOKUP_NORMAL
;
1089 cp_finish_decl (value
, init
, false, NULL_TREE
, flags
);
1091 if (width
!= error_mark_node
)
1093 /* The width must be an integer type. */
1094 if (!type_dependent_expression_p (width
)
1095 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width
)))
1096 error ("width of bit-field %qD has non-integral type %qT", value
,
1100 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1101 check_bitfield_decl picks it from there later and sets DECL_SIZE
1103 DECL_BIT_FIELD_REPRESENTATIVE (value
) = width
;
1104 SET_DECL_C_BIT_FIELD (value
);
1108 DECL_IN_AGGR_P (value
) = 1;
1111 cplus_decl_attributes (&value
, attrlist
, /*flags=*/0);
1117 /* Returns true iff ATTR is an attribute which needs to be applied at
1118 instantiation time rather than template definition time. */
1121 is_late_template_attribute (tree attr
, tree decl
)
1123 tree name
= get_attribute_name (attr
);
1124 tree args
= TREE_VALUE (attr
);
1125 const struct attribute_spec
*spec
= lookup_attribute_spec (name
);
1129 /* Unknown attribute. */
1132 /* Attribute weak handling wants to write out assembly right away. */
1133 if (is_attribute_p ("weak", name
))
1136 /* Attributes used and unused are applied directly, as they appertain to
1138 if (is_attribute_p ("unused", name
)
1139 || is_attribute_p ("used", name
))
1142 /* Attribute tls_model wants to modify the symtab. */
1143 if (is_attribute_p ("tls_model", name
))
1146 /* #pragma omp declare simd attribute needs to be always deferred. */
1148 && is_attribute_p ("omp declare simd", name
))
1151 /* An attribute pack is clearly dependent. */
1152 if (args
&& PACK_EXPANSION_P (args
))
1155 /* If any of the arguments are dependent expressions, we can't evaluate
1156 the attribute until instantiation time. */
1157 for (arg
= args
; arg
; arg
= TREE_CHAIN (arg
))
1159 tree t
= TREE_VALUE (arg
);
1161 /* If the first attribute argument is an identifier, only consider
1162 second and following arguments. Attributes like mode, format,
1163 cleanup and several target specific attributes aren't late
1164 just because they have an IDENTIFIER_NODE as first argument. */
1165 if (arg
== args
&& attribute_takes_identifier_p (name
)
1166 && identifier_p (t
))
1169 if (value_dependent_expression_p (t
)
1170 || type_dependent_expression_p (t
))
1174 if (TREE_CODE (decl
) == TYPE_DECL
1176 || spec
->type_required
)
1178 tree type
= TYPE_P (decl
) ? decl
: TREE_TYPE (decl
);
1180 /* We can't apply any attributes to a completely unknown type until
1181 instantiation time. */
1182 enum tree_code code
= TREE_CODE (type
);
1183 if (code
== TEMPLATE_TYPE_PARM
1184 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
1185 || code
== TYPENAME_TYPE
)
1187 /* Also defer most attributes on dependent types. This is not
1188 necessary in all cases, but is the better default. */
1189 else if (dependent_type_p (type
)
1190 /* But some attributes specifically apply to templates. */
1191 && !is_attribute_p ("abi_tag", name
)
1192 && !is_attribute_p ("deprecated", name
)
1193 && !is_attribute_p ("visibility", name
))
1202 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1203 applied at instantiation time and return them. If IS_DEPENDENT is true,
1204 the declaration itself is dependent, so all attributes should be applied
1205 at instantiation time. */
1208 splice_template_attributes (tree
*attr_p
, tree decl
)
1211 tree late_attrs
= NULL_TREE
;
1212 tree
*q
= &late_attrs
;
1219 if (is_late_template_attribute (*p
, decl
))
1221 ATTR_IS_DEPENDENT (*p
) = 1;
1223 *p
= TREE_CHAIN (*p
);
1224 q
= &TREE_CHAIN (*q
);
1228 p
= &TREE_CHAIN (*p
);
1234 /* Remove any late attributes from the list in ATTR_P and attach them to
1238 save_template_attributes (tree
*attr_p
, tree
*decl_p
)
1242 if (attr_p
&& *attr_p
== error_mark_node
)
1245 tree late_attrs
= splice_template_attributes (attr_p
, *decl_p
);
1249 if (DECL_P (*decl_p
))
1250 q
= &DECL_ATTRIBUTES (*decl_p
);
1252 q
= &TYPE_ATTRIBUTES (*decl_p
);
1254 tree old_attrs
= *q
;
1256 /* Merge the late attributes at the beginning with the attribute
1258 late_attrs
= merge_attributes (late_attrs
, *q
);
1261 if (!DECL_P (*decl_p
) && *decl_p
== TYPE_MAIN_VARIANT (*decl_p
))
1263 /* We've added new attributes directly to the main variant, so
1264 now we need to update all of the other variants to include
1265 these new attributes. */
1267 for (variant
= TYPE_NEXT_VARIANT (*decl_p
); variant
;
1268 variant
= TYPE_NEXT_VARIANT (variant
))
1270 gcc_assert (TYPE_ATTRIBUTES (variant
) == old_attrs
);
1271 TYPE_ATTRIBUTES (variant
) = TYPE_ATTRIBUTES (*decl_p
);
1276 /* True if ATTRS contains any dependent attributes that affect type
1280 any_dependent_type_attributes_p (tree attrs
)
1282 for (tree a
= attrs
; a
; a
= TREE_CHAIN (a
))
1283 if (ATTR_IS_DEPENDENT (a
))
1285 const attribute_spec
*as
= lookup_attribute_spec (TREE_PURPOSE (a
));
1286 if (as
&& as
->affects_type_identity
)
1292 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1293 to a typedef which gives a previously unnamed class or enum a name for
1294 linkage purposes. */
1297 attributes_naming_typedef_ok (tree attrs
)
1299 for (; attrs
; attrs
= TREE_CHAIN (attrs
))
1301 tree name
= get_attribute_name (attrs
);
1302 if (is_attribute_p ("vector_size", name
))
1308 /* Like reconstruct_complex_type, but handle also template trees. */
1311 cp_reconstruct_complex_type (tree type
, tree bottom
)
1314 bool late_return_type_p
= false;
1316 if (TYPE_PTR_P (type
))
1318 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1319 outer
= build_pointer_type_for_mode (inner
, TYPE_MODE (type
),
1320 TYPE_REF_CAN_ALIAS_ALL (type
));
1322 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
1324 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1325 outer
= build_reference_type_for_mode (inner
, TYPE_MODE (type
),
1326 TYPE_REF_CAN_ALIAS_ALL (type
));
1328 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1330 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1331 outer
= build_cplus_array_type (inner
, TYPE_DOMAIN (type
));
1332 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1333 element type qualification will be handled by the recursive
1334 cp_reconstruct_complex_type call and cp_build_qualified_type
1335 for ARRAY_TYPEs changes the element type. */
1338 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
1340 late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (type
);
1341 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1342 outer
= build_function_type (inner
, TYPE_ARG_TYPES (type
));
1343 outer
= apply_memfn_quals (outer
,
1344 type_memfn_quals (type
),
1345 type_memfn_rqual (type
));
1347 else if (TREE_CODE (type
) == METHOD_TYPE
)
1349 late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (type
);
1350 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1351 /* The build_method_type_directly() routine prepends 'this' to argument list,
1352 so we must compensate by getting rid of it. */
1354 = build_method_type_directly
1355 (class_of_this_parm (type
), inner
,
1356 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
1358 else if (TREE_CODE (type
) == OFFSET_TYPE
)
1360 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1361 outer
= build_offset_type (TYPE_OFFSET_BASETYPE (type
), inner
);
1366 if (TYPE_ATTRIBUTES (type
))
1367 outer
= cp_build_type_attribute_variant (outer
, TYPE_ATTRIBUTES (type
));
1368 outer
= cp_build_qualified_type (outer
, cp_type_quals (type
));
1370 if (late_return_type_p
)
1371 TYPE_HAS_LATE_RETURN_TYPE (outer
) = 1;
1376 /* Replaces any constexpr expression that may be into the attributes
1377 arguments with their reduced value. */
1380 cp_check_const_attributes (tree attributes
)
1382 if (attributes
== error_mark_node
)
1386 for (attr
= attributes
; attr
; attr
= TREE_CHAIN (attr
))
1389 for (arg
= TREE_VALUE (attr
); arg
; arg
= TREE_CHAIN (arg
))
1391 tree expr
= TREE_VALUE (arg
);
1393 TREE_VALUE (arg
) = maybe_constant_value (expr
);
1398 /* Return true if TYPE is an OpenMP mappable type. */
1400 cp_omp_mappable_type (tree type
)
1402 /* Mappable type has to be complete. */
1403 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
1405 /* Arrays have mappable type if the elements have mappable type. */
1406 while (TREE_CODE (type
) == ARRAY_TYPE
)
1407 type
= TREE_TYPE (type
);
1408 /* A mappable type cannot contain virtual members. */
1409 if (CLASS_TYPE_P (type
) && CLASSTYPE_VTABLES (type
))
1411 /* All data members must be non-static. */
1412 if (CLASS_TYPE_P (type
))
1415 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1418 /* All fields must have mappable types. */
1419 else if (TREE_CODE (field
) == FIELD_DECL
1420 && !cp_omp_mappable_type (TREE_TYPE (field
)))
1426 /* Like decl_attributes, but handle C++ complexity. */
1429 cplus_decl_attributes (tree
*decl
, tree attributes
, int flags
)
1431 if (*decl
== NULL_TREE
|| *decl
== void_type_node
1432 || *decl
== error_mark_node
)
1435 /* Add implicit "omp declare target" attribute if requested. */
1436 if (scope_chain
->omp_declare_target_attribute
1438 && (TREE_STATIC (*decl
) || DECL_EXTERNAL (*decl
)))
1439 || TREE_CODE (*decl
) == FUNCTION_DECL
))
1442 && DECL_CLASS_SCOPE_P (*decl
))
1443 error ("%q+D static data member inside of declare target directive",
1445 else if (!processing_template_decl
1447 && !cp_omp_mappable_type (TREE_TYPE (*decl
)))
1448 error ("%q+D in declare target directive does not have mappable type",
1451 attributes
= tree_cons (get_identifier ("omp declare target"),
1452 NULL_TREE
, attributes
);
1455 if (processing_template_decl
)
1457 if (check_for_bare_parameter_packs (attributes
))
1460 save_template_attributes (&attributes
, decl
);
1463 cp_check_const_attributes (attributes
);
1465 if (TREE_CODE (*decl
) == TEMPLATE_DECL
)
1466 decl
= &DECL_TEMPLATE_RESULT (*decl
);
1468 if (TREE_TYPE (*decl
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl
)))
1471 = decl_attributes (decl
, attributes
, flags
| ATTR_FLAG_FUNCTION_NEXT
);
1472 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl
)),
1476 decl_attributes (decl
, attributes
, flags
);
1478 if (TREE_CODE (*decl
) == TYPE_DECL
)
1479 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl
), TREE_TYPE (*decl
));
1481 /* Propagate deprecation out to the template. */
1482 if (TREE_DEPRECATED (*decl
))
1483 if (tree ti
= get_template_info (*decl
))
1485 tree tmpl
= TI_TEMPLATE (ti
);
1486 tree pattern
= (TYPE_P (*decl
) ? TREE_TYPE (tmpl
)
1487 : DECL_TEMPLATE_RESULT (tmpl
));
1488 if (*decl
== pattern
)
1489 TREE_DEPRECATED (tmpl
) = true;
1493 /* Walks through the namespace- or function-scope anonymous union
1494 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1495 Returns one of the fields for use in the mangled name. */
1498 build_anon_union_vars (tree type
, tree object
)
1500 tree main_decl
= NULL_TREE
;
1503 /* Rather than write the code to handle the non-union case,
1504 just give an error. */
1505 if (TREE_CODE (type
) != UNION_TYPE
)
1507 error ("anonymous struct not inside named type");
1508 return error_mark_node
;
1511 for (field
= TYPE_FIELDS (type
);
1513 field
= DECL_CHAIN (field
))
1518 if (DECL_ARTIFICIAL (field
))
1520 if (TREE_CODE (field
) != FIELD_DECL
)
1522 permerror (DECL_SOURCE_LOCATION (field
),
1523 "%q#D invalid; an anonymous union can only "
1524 "have non-static data members", field
);
1528 if (TREE_PRIVATE (field
))
1529 permerror (DECL_SOURCE_LOCATION (field
),
1530 "private member %q#D in anonymous union", field
);
1531 else if (TREE_PROTECTED (field
))
1532 permerror (DECL_SOURCE_LOCATION (field
),
1533 "protected member %q#D in anonymous union", field
);
1535 if (processing_template_decl
)
1536 ref
= build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
, object
,
1537 DECL_NAME (field
), NULL_TREE
);
1539 ref
= build_class_member_access_expr (object
, field
, NULL_TREE
,
1540 false, tf_warning_or_error
);
1542 if (DECL_NAME (field
))
1546 decl
= build_decl (input_location
,
1547 VAR_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
1548 DECL_ANON_UNION_VAR_P (decl
) = 1;
1549 DECL_ARTIFICIAL (decl
) = 1;
1551 base
= get_base_address (object
);
1552 TREE_PUBLIC (decl
) = TREE_PUBLIC (base
);
1553 TREE_STATIC (decl
) = TREE_STATIC (base
);
1554 DECL_EXTERNAL (decl
) = DECL_EXTERNAL (base
);
1556 SET_DECL_VALUE_EXPR (decl
, ref
);
1557 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1559 decl
= pushdecl (decl
);
1561 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1562 decl
= build_anon_union_vars (TREE_TYPE (field
), ref
);
1566 if (main_decl
== NULL_TREE
)
1573 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1574 anonymous union, then all members must be laid out together. PUBLIC_P
1575 is nonzero if this union is not declared static. */
1578 finish_anon_union (tree anon_union_decl
)
1584 if (anon_union_decl
== error_mark_node
)
1587 type
= TREE_TYPE (anon_union_decl
);
1588 public_p
= TREE_PUBLIC (anon_union_decl
);
1590 /* The VAR_DECL's context is the same as the TYPE's context. */
1591 DECL_CONTEXT (anon_union_decl
) = DECL_CONTEXT (TYPE_NAME (type
));
1593 if (TYPE_FIELDS (type
) == NULL_TREE
)
1598 error ("namespace-scope anonymous aggregates must be static");
1602 main_decl
= build_anon_union_vars (type
, anon_union_decl
);
1603 if (main_decl
== error_mark_node
)
1605 if (main_decl
== NULL_TREE
)
1607 pedwarn (input_location
, 0, "anonymous union with no members");
1611 if (!processing_template_decl
)
1613 /* Use main_decl to set the mangled name. */
1614 DECL_NAME (anon_union_decl
) = DECL_NAME (main_decl
);
1615 maybe_commonize_var (anon_union_decl
);
1616 if (TREE_STATIC (anon_union_decl
) || DECL_EXTERNAL (anon_union_decl
))
1617 mangle_decl (anon_union_decl
);
1618 DECL_NAME (anon_union_decl
) = NULL_TREE
;
1621 pushdecl (anon_union_decl
);
1622 cp_finish_decl (anon_union_decl
, NULL_TREE
, false, NULL_TREE
, 0);
1625 /* Auxiliary functions to make type signatures for
1626 `operator new' and `operator delete' correspond to
1627 what compiler will be expecting. */
1630 coerce_new_type (tree type
)
1633 tree args
= TYPE_ARG_TYPES (type
);
1635 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1637 if (!same_type_p (TREE_TYPE (type
), ptr_type_node
))
1640 error ("%<operator new%> must return type %qT", ptr_type_node
);
1643 if (args
&& args
!= void_list_node
)
1645 if (TREE_PURPOSE (args
))
1647 /* [basic.stc.dynamic.allocation]
1649 The first parameter shall not have an associated default
1651 error ("the first parameter of %<operator new%> cannot "
1652 "have a default argument");
1653 /* Throw away the default argument. */
1654 TREE_PURPOSE (args
) = NULL_TREE
;
1657 if (!same_type_p (TREE_VALUE (args
), size_type_node
))
1660 args
= TREE_CHAIN (args
);
1667 permerror (input_location
, "%<operator new%> takes type %<size_t%> (%qT) "
1668 "as first parameter", size_type_node
);
1673 args
= tree_cons (NULL_TREE
, size_type_node
, args
);
1676 type
= build_exception_variant
1677 (build_function_type (ptr_type_node
, args
),
1678 TYPE_RAISES_EXCEPTIONS (type
));
1686 coerce_delete_type (tree type
)
1689 tree args
= TYPE_ARG_TYPES (type
);
1691 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1693 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
1696 error ("%<operator delete%> must return type %qT", void_type_node
);
1699 if (!args
|| args
== void_list_node
1700 || !same_type_p (TREE_VALUE (args
), ptr_type_node
))
1703 if (args
&& args
!= void_list_node
)
1704 args
= TREE_CHAIN (args
);
1705 error ("%<operator delete%> takes type %qT as first parameter",
1711 args
= tree_cons (NULL_TREE
, ptr_type_node
, args
);
1714 type
= build_exception_variant
1715 (build_function_type (void_type_node
, args
),
1716 TYPE_RAISES_EXCEPTIONS (type
));
1724 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1725 and mark them as needed. */
1728 mark_vtable_entries (tree decl
)
1731 unsigned HOST_WIDE_INT idx
;
1733 /* It's OK for the vtable to refer to deprecated virtual functions. */
1734 warning_sentinel
w(warn_deprecated_decl
);
1736 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
1741 STRIP_NOPS (fnaddr
);
1743 if (TREE_CODE (fnaddr
) != ADDR_EXPR
1744 && TREE_CODE (fnaddr
) != FDESC_EXPR
)
1745 /* This entry is an offset: a virtual base class offset, a
1746 virtual call offset, an RTTI offset, etc. */
1749 fn
= TREE_OPERAND (fnaddr
, 0);
1750 TREE_ADDRESSABLE (fn
) = 1;
1751 /* When we don't have vcall offsets, we output thunks whenever
1752 we output the vtables that contain them. With vcall offsets,
1753 we know all the thunks we'll need when we emit a virtual
1754 function, so we emit the thunks there instead. */
1755 if (DECL_THUNK_P (fn
))
1756 use_thunk (fn
, /*emit_p=*/0);
1761 /* Set DECL up to have the closest approximation of "initialized common"
1762 linkage available. */
1765 comdat_linkage (tree decl
)
1768 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1769 else if (TREE_CODE (decl
) == FUNCTION_DECL
1770 || (VAR_P (decl
) && DECL_ARTIFICIAL (decl
)))
1771 /* We can just emit function and compiler-generated variables
1772 statically; having multiple copies is (for the most part) only
1775 There are two correctness issues, however: the address of a
1776 template instantiation with external linkage should be the
1777 same, independent of what translation unit asks for the
1778 address, and this will not hold when we emit multiple copies of
1779 the function. However, there's little else we can do.
1781 Also, by default, the typeinfo implementation assumes that
1782 there will be only one copy of the string used as the name for
1783 each type. Therefore, if weak symbols are unavailable, the
1784 run-time library should perform a more conservative check; it
1785 should perform a string comparison, rather than an address
1787 TREE_PUBLIC (decl
) = 0;
1790 /* Static data member template instantiations, however, cannot
1791 have multiple copies. */
1792 if (DECL_INITIAL (decl
) == 0
1793 || DECL_INITIAL (decl
) == error_mark_node
)
1794 DECL_COMMON (decl
) = 1;
1795 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
1797 DECL_COMMON (decl
) = 1;
1798 DECL_INITIAL (decl
) = error_mark_node
;
1800 else if (!DECL_EXPLICIT_INSTANTIATION (decl
))
1802 /* We can't do anything useful; leave vars for explicit
1804 DECL_EXTERNAL (decl
) = 1;
1805 DECL_NOT_REALLY_EXTERN (decl
) = 0;
1809 if (TREE_PUBLIC (decl
))
1810 DECL_COMDAT (decl
) = 1;
1813 /* For win32 we also want to put explicit instantiations in
1814 linkonce sections, so that they will be merged with implicit
1815 instantiations; otherwise we get duplicate symbol errors.
1816 For Darwin we do not want explicit instantiations to be
1820 maybe_make_one_only (tree decl
)
1822 /* We used to say that this was not necessary on targets that support weak
1823 symbols, because the implicit instantiations will defer to the explicit
1824 one. However, that's not actually the case in SVR4; a strong definition
1825 after a weak one is an error. Also, not making explicit
1826 instantiations one_only means that we can end up with two copies of
1827 some template instantiations. */
1831 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1832 we can get away with not emitting them if they aren't used. We need
1833 to for variables so that cp_finish_decl will update their linkage,
1834 because their DECL_INITIAL may not have been set properly yet. */
1836 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1837 || (! DECL_EXPLICIT_INSTANTIATION (decl
)
1838 && ! DECL_TEMPLATE_SPECIALIZATION (decl
)))
1840 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1844 varpool_node
*node
= varpool_node::get_create (decl
);
1845 DECL_COMDAT (decl
) = 1;
1846 /* Mark it needed so we don't forget to emit it. */
1847 node
->forced_by_abi
= true;
1848 TREE_USED (decl
) = 1;
1853 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1854 This predicate will give the right answer during parsing of the
1855 function, which other tests may not. */
1858 vague_linkage_p (tree decl
)
1860 if (!TREE_PUBLIC (decl
))
1862 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1863 variants, check one of the "clones" for the real linkage. */
1864 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl
)
1865 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl
))
1866 && DECL_CHAIN (decl
)
1867 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl
)))
1868 return vague_linkage_p (DECL_CHAIN (decl
));
1870 gcc_checking_assert (!DECL_COMDAT (decl
));
1873 /* Unfortunately, import_export_decl has not always been called
1874 before the function is processed, so we cannot simply check
1876 if (DECL_COMDAT (decl
)
1877 || (TREE_CODE (decl
) == FUNCTION_DECL
1878 && DECL_DECLARED_INLINE_P (decl
))
1879 || (DECL_LANG_SPECIFIC (decl
)
1880 && DECL_TEMPLATE_INSTANTIATION (decl
))
1881 || (VAR_P (decl
) && DECL_INLINE_VAR_P (decl
)))
1883 else if (DECL_FUNCTION_SCOPE_P (decl
))
1884 /* A local static in an inline effectively has vague linkage. */
1885 return (TREE_STATIC (decl
)
1886 && vague_linkage_p (DECL_CONTEXT (decl
)));
1891 /* Determine whether or not we want to specifically import or export CTYPE,
1892 using various heuristics. */
1895 import_export_class (tree ctype
)
1897 /* -1 for imported, 1 for exported. */
1898 int import_export
= 0;
1900 /* It only makes sense to call this function at EOF. The reason is
1901 that this function looks at whether or not the first non-inline
1902 non-abstract virtual member function has been defined in this
1903 translation unit. But, we can't possibly know that until we've
1904 seen the entire translation unit. */
1905 gcc_assert (at_eof
);
1907 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
1910 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1911 we will have CLASSTYPE_INTERFACE_ONLY set but not
1912 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1913 heuristic because someone will supply a #pragma implementation
1914 elsewhere, and deducing it here would produce a conflict. */
1915 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
1918 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
1920 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
1922 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
1923 && !flag_implicit_templates
)
1924 /* For a template class, without -fimplicit-templates, check the
1925 repository. If the virtual table is assigned to this
1926 translation unit, then export the class; otherwise, import
1928 import_export
= repo_export_class_p (ctype
) ? 1 : -1;
1929 else if (TYPE_POLYMORPHIC_P (ctype
))
1931 /* The ABI specifies that the virtual table and associated
1932 information are emitted with the key method, if any. */
1933 tree method
= CLASSTYPE_KEY_METHOD (ctype
);
1934 /* If weak symbol support is not available, then we must be
1935 careful not to emit the vtable when the key function is
1936 inline. An inline function can be defined in multiple
1937 translation units. If we were to emit the vtable in each
1938 translation unit containing a definition, we would get
1939 multiple definition errors at link-time. */
1940 if (method
&& (flag_weak
|| ! DECL_DECLARED_INLINE_P (method
)))
1941 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
1944 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1945 a definition anywhere else. */
1946 if (MULTIPLE_SYMBOL_SPACES
&& import_export
== -1)
1949 /* Allow back ends the chance to overrule the decision. */
1950 if (targetm
.cxx
.import_export_class
)
1951 import_export
= targetm
.cxx
.import_export_class (ctype
, import_export
);
1955 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
1956 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
1960 /* Return true if VAR has already been provided to the back end; in that
1961 case VAR should not be modified further by the front end. */
1963 var_finalized_p (tree var
)
1965 return varpool_node::get_create (var
)->definition
;
1968 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1969 must be emitted in this translation unit. Mark it as such. */
1972 mark_needed (tree decl
)
1974 TREE_USED (decl
) = 1;
1975 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1977 /* Extern inline functions don't become needed when referenced.
1978 If we know a method will be emitted in other TU and no new
1979 functions can be marked reachable, just use the external
1981 struct cgraph_node
*node
= cgraph_node::get_create (decl
);
1982 node
->forced_by_abi
= true;
1984 /* #pragma interface and -frepo code can call mark_needed for
1985 maybe-in-charge 'tors; mark the clones as well. */
1987 FOR_EACH_CLONE (clone
, decl
)
1988 mark_needed (clone
);
1990 else if (VAR_P (decl
))
1992 varpool_node
*node
= varpool_node::get_create (decl
);
1993 /* C++ frontend use mark_decl_references to force COMDAT variables
1994 to be output that might appear dead otherwise. */
1995 node
->forced_by_abi
= true;
1999 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2000 returns true if a definition of this entity should be provided in
2001 this object file. Callers use this function to determine whether
2002 or not to let the back end know that a definition of DECL is
2003 available in this translation unit. */
2006 decl_needed_p (tree decl
)
2008 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2009 /* This function should only be called at the end of the translation
2010 unit. We cannot be sure of whether or not something will be
2011 COMDAT until that point. */
2012 gcc_assert (at_eof
);
2014 /* All entities with external linkage that are not COMDAT/EXTERN should be
2015 emitted; they may be referred to from other object files. */
2016 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_REALLY_EXTERN (decl
))
2018 /* Functions marked "dllexport" must be emitted so that they are
2019 visible to other DLLs. */
2020 if (flag_keep_inline_dllexport
2021 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))
2024 /* When not optimizing, do not bother to produce definitions for extern
2026 if (DECL_REALLY_EXTERN (decl
)
2027 && ((TREE_CODE (decl
) != FUNCTION_DECL
2029 || (TREE_CODE (decl
) == FUNCTION_DECL
2030 && !opt_for_fn (decl
, optimize
)))
2031 && !lookup_attribute ("always_inline", decl
))
2034 /* If this entity was used, let the back end see it; it will decide
2035 whether or not to emit it into the object file. */
2036 if (TREE_USED (decl
))
2039 /* Virtual functions might be needed for devirtualization. */
2040 if (flag_devirtualize
2041 && TREE_CODE (decl
) == FUNCTION_DECL
2042 && DECL_VIRTUAL_P (decl
))
2045 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2046 reference to DECL might cause it to be emitted later. */
2050 /* If necessary, write out the vtables for the dynamic class CTYPE.
2051 Returns true if any vtables were emitted. */
2054 maybe_emit_vtables (tree ctype
)
2059 varpool_node
*current
= NULL
, *last
= NULL
;
2061 /* If the vtables for this class have already been emitted there is
2062 nothing more to do. */
2063 primary_vtbl
= CLASSTYPE_VTABLES (ctype
);
2064 if (var_finalized_p (primary_vtbl
))
2066 /* Ignore dummy vtables made by get_vtable_decl. */
2067 if (TREE_TYPE (primary_vtbl
) == void_type_node
)
2070 /* On some targets, we cannot determine the key method until the end
2071 of the translation unit -- which is when this function is
2073 if (!targetm
.cxx
.key_method_may_be_inline ())
2074 determine_key_method (ctype
);
2076 /* See if any of the vtables are needed. */
2077 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2079 import_export_decl (vtbl
);
2080 if (DECL_NOT_REALLY_EXTERN (vtbl
) && decl_needed_p (vtbl
))
2085 /* If the references to this class' vtables are optimized away,
2086 still emit the appropriate debugging information. See
2088 if (DECL_COMDAT (primary_vtbl
)
2089 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
2090 note_debug_info_needed (ctype
);
2094 /* The ABI requires that we emit all of the vtables if we emit any
2096 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
2098 /* Mark entities references from the virtual table as used. */
2099 mark_vtable_entries (vtbl
);
2101 if (TREE_TYPE (DECL_INITIAL (vtbl
)) == 0)
2103 vec
<tree
, va_gc
> *cleanups
= NULL
;
2104 tree expr
= store_init_value (vtbl
, DECL_INITIAL (vtbl
), &cleanups
,
2107 /* It had better be all done at compile-time. */
2108 gcc_assert (!expr
&& !cleanups
);
2112 DECL_EXTERNAL (vtbl
) = 0;
2113 rest_of_decl_compilation (vtbl
, 1, 1);
2115 /* Because we're only doing syntax-checking, we'll never end up
2116 actually marking the variable as written. */
2117 if (flag_syntax_only
)
2118 TREE_ASM_WRITTEN (vtbl
) = 1;
2119 else if (DECL_ONE_ONLY (vtbl
))
2121 current
= varpool_node::get_create (vtbl
);
2123 current
->add_to_same_comdat_group (last
);
2128 /* Since we're writing out the vtable here, also write the debug
2130 note_debug_info_needed (ctype
);
2135 /* A special return value from type_visibility meaning internal
2138 enum { VISIBILITY_ANON
= VISIBILITY_INTERNAL
+1 };
2140 /* walk_tree helper function for type_visibility. */
2143 min_vis_r (tree
*tp
, int *walk_subtrees
, void *data
)
2145 int *vis_p
= (int *)data
;
2150 else if (OVERLOAD_TYPE_P (*tp
)
2151 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp
)))
2153 *vis_p
= VISIBILITY_ANON
;
2156 else if (CLASS_TYPE_P (*tp
)
2157 && CLASSTYPE_VISIBILITY (*tp
) > *vis_p
)
2158 *vis_p
= CLASSTYPE_VISIBILITY (*tp
);
2162 /* Returns the visibility of TYPE, which is the minimum visibility of its
2166 type_visibility (tree type
)
2168 int vis
= VISIBILITY_DEFAULT
;
2169 cp_walk_tree_without_duplicates (&type
, min_vis_r
, &vis
);
2173 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2174 specified (or if VISIBILITY is static). If TMPL is true, this
2175 constraint is for a template argument, and takes precedence
2176 over explicitly-specified visibility on the template. */
2179 constrain_visibility (tree decl
, int visibility
, bool tmpl
)
2181 if (visibility
== VISIBILITY_ANON
)
2183 /* extern "C" declarations aren't affected by the anonymous
2185 if (!DECL_EXTERN_C_P (decl
))
2187 TREE_PUBLIC (decl
) = 0;
2188 DECL_WEAK (decl
) = 0;
2189 DECL_COMMON (decl
) = 0;
2190 DECL_COMDAT (decl
) = false;
2191 if (VAR_OR_FUNCTION_DECL_P (decl
))
2193 struct symtab_node
*snode
= symtab_node::get (decl
);
2196 snode
->set_comdat_group (NULL
);
2198 DECL_INTERFACE_KNOWN (decl
) = 1;
2199 if (DECL_LANG_SPECIFIC (decl
))
2200 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2203 else if (visibility
> DECL_VISIBILITY (decl
)
2204 && (tmpl
|| !DECL_VISIBILITY_SPECIFIED (decl
)))
2206 DECL_VISIBILITY (decl
) = (enum symbol_visibility
) visibility
;
2207 /* This visibility was not specified. */
2208 DECL_VISIBILITY_SPECIFIED (decl
) = false;
2212 /* Constrain the visibility of DECL based on the visibility of its template
2216 constrain_visibility_for_template (tree decl
, tree targs
)
2218 /* If this is a template instantiation, check the innermost
2219 template args for visibility constraints. The outer template
2220 args are covered by the class check. */
2221 tree args
= INNERMOST_TEMPLATE_ARGS (targs
);
2223 for (i
= TREE_VEC_LENGTH (args
); i
> 0; --i
)
2227 tree arg
= TREE_VEC_ELT (args
, i
-1);
2229 vis
= type_visibility (arg
);
2232 if (REFERENCE_REF_P (arg
))
2233 arg
= TREE_OPERAND (arg
, 0);
2234 if (TREE_TYPE (arg
))
2236 if (TREE_CODE (arg
) == ADDR_EXPR
)
2237 arg
= TREE_OPERAND (arg
, 0);
2238 if (VAR_OR_FUNCTION_DECL_P (arg
))
2240 if (! TREE_PUBLIC (arg
))
2241 vis
= VISIBILITY_ANON
;
2243 vis
= DECL_VISIBILITY (arg
);
2247 constrain_visibility (decl
, vis
, true);
2251 /* Like c_determine_visibility, but with additional C++-specific
2254 Function-scope entities can rely on the function's visibility because
2255 it is set in start_preparsed_function.
2257 Class-scope entities cannot rely on the class's visibility until the end
2258 of the enclosing class definition.
2260 Note that because namespaces have multiple independent definitions,
2261 namespace visibility is handled elsewhere using the #pragma visibility
2262 machinery rather than by decorating the namespace declaration.
2264 The goal is for constraints from the type to give a diagnostic, and
2265 other constraints to be applied silently. */
2268 determine_visibility (tree decl
)
2270 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2272 /* Only relevant for names with external linkage. */
2273 if (!TREE_PUBLIC (decl
))
2276 /* Cloned constructors and destructors get the same visibility as
2277 the underlying function. That should be set up in
2278 maybe_clone_body. */
2279 gcc_assert (!DECL_CLONED_FUNCTION_P (decl
));
2281 bool orig_visibility_specified
= DECL_VISIBILITY_SPECIFIED (decl
);
2282 enum symbol_visibility orig_visibility
= DECL_VISIBILITY (decl
);
2284 /* The decl may be a template instantiation, which could influence
2286 tree template_decl
= NULL_TREE
;
2287 if (TREE_CODE (decl
) == TYPE_DECL
)
2289 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
2291 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)))
2292 template_decl
= decl
;
2294 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
2295 template_decl
= decl
;
2297 else if (DECL_LANG_SPECIFIC (decl
) && DECL_USE_TEMPLATE (decl
))
2298 template_decl
= decl
;
2300 /* If DECL is a member of a class, visibility specifiers on the
2301 class can influence the visibility of the DECL. */
2302 tree class_type
= NULL_TREE
;
2303 if (DECL_CLASS_SCOPE_P (decl
))
2304 class_type
= DECL_CONTEXT (decl
);
2307 /* Not a class member. */
2309 /* Virtual tables have DECL_CONTEXT set to their associated class,
2310 so they are automatically handled above. */
2311 gcc_assert (!VAR_P (decl
)
2312 || !DECL_VTABLE_OR_VTT_P (decl
));
2314 if (DECL_FUNCTION_SCOPE_P (decl
) && ! DECL_VISIBILITY_SPECIFIED (decl
))
2316 /* Local statics and classes get the visibility of their
2317 containing function by default, except that
2318 -fvisibility-inlines-hidden doesn't affect them. */
2319 tree fn
= DECL_CONTEXT (decl
);
2320 if (DECL_VISIBILITY_SPECIFIED (fn
))
2322 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2323 DECL_VISIBILITY_SPECIFIED (decl
) =
2324 DECL_VISIBILITY_SPECIFIED (fn
);
2328 if (DECL_CLASS_SCOPE_P (fn
))
2329 determine_visibility_from_class (decl
, DECL_CONTEXT (fn
));
2330 else if (determine_hidden_inline (fn
))
2332 DECL_VISIBILITY (decl
) = default_visibility
;
2333 DECL_VISIBILITY_SPECIFIED (decl
) =
2334 visibility_options
.inpragma
;
2338 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2339 DECL_VISIBILITY_SPECIFIED (decl
) =
2340 DECL_VISIBILITY_SPECIFIED (fn
);
2344 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2345 but have no TEMPLATE_INFO. Their containing template
2346 function does, and the local class could be constrained
2351 else if (VAR_P (decl
) && DECL_TINFO_P (decl
)
2352 && flag_visibility_ms_compat
)
2354 /* Under -fvisibility-ms-compat, types are visible by default,
2355 even though their contents aren't. */
2356 tree underlying_type
= TREE_TYPE (DECL_NAME (decl
));
2357 int underlying_vis
= type_visibility (underlying_type
);
2358 if (underlying_vis
== VISIBILITY_ANON
2359 || (CLASS_TYPE_P (underlying_type
)
2360 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type
)))
2361 constrain_visibility (decl
, underlying_vis
, false);
2363 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
2365 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2367 /* tinfo visibility is based on the type it's for. */
2368 constrain_visibility
2369 (decl
, type_visibility (TREE_TYPE (DECL_NAME (decl
))), false);
2371 /* Give the target a chance to override the visibility associated
2373 if (TREE_PUBLIC (decl
)
2374 && !DECL_REALLY_EXTERN (decl
)
2375 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl
)))
2376 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl
))))
2377 targetm
.cxx
.determine_class_data_visibility (decl
);
2379 else if (template_decl
)
2380 /* Template instantiations and specializations get visibility based
2381 on their template unless they override it with an attribute. */;
2382 else if (! DECL_VISIBILITY_SPECIFIED (decl
))
2384 if (determine_hidden_inline (decl
))
2385 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2388 /* Set default visibility to whatever the user supplied with
2389 #pragma GCC visibility or a namespace visibility attribute. */
2390 DECL_VISIBILITY (decl
) = default_visibility
;
2391 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
2398 /* If the specialization doesn't specify visibility, use the
2399 visibility from the template. */
2400 tree tinfo
= get_template_info (template_decl
);
2401 tree args
= TI_ARGS (tinfo
);
2402 tree attribs
= (TREE_CODE (decl
) == TYPE_DECL
2403 ? TYPE_ATTRIBUTES (TREE_TYPE (decl
))
2404 : DECL_ATTRIBUTES (decl
));
2406 if (args
!= error_mark_node
)
2408 tree pattern
= DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo
));
2410 if (!DECL_VISIBILITY_SPECIFIED (decl
))
2412 if (!DECL_VISIBILITY_SPECIFIED (pattern
)
2413 && determine_hidden_inline (decl
))
2414 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2417 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (pattern
);
2418 DECL_VISIBILITY_SPECIFIED (decl
)
2419 = DECL_VISIBILITY_SPECIFIED (pattern
);
2424 /* Template argument visibility outweighs #pragma or namespace
2425 visibility, but not an explicit attribute. */
2426 && !lookup_attribute ("visibility", attribs
))
2428 int depth
= TMPL_ARGS_DEPTH (args
);
2429 if (DECL_VISIBILITY_SPECIFIED (decl
))
2431 /* A class template member with explicit visibility
2432 overrides the class visibility, so we need to apply
2433 all the levels of template args directly. */
2435 for (i
= 1; i
<= depth
; ++i
)
2437 tree lev
= TMPL_ARGS_LEVEL (args
, i
);
2438 constrain_visibility_for_template (decl
, lev
);
2441 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
2442 /* Limit visibility based on its template arguments. */
2443 constrain_visibility_for_template (decl
, args
);
2449 determine_visibility_from_class (decl
, class_type
);
2451 if (decl_anon_ns_mem_p (decl
))
2452 /* Names in an anonymous namespace get internal linkage.
2453 This might change once we implement export. */
2454 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2455 else if (TREE_CODE (decl
) != TYPE_DECL
)
2457 /* Propagate anonymity from type to decl. */
2458 int tvis
= type_visibility (TREE_TYPE (decl
));
2459 if (tvis
== VISIBILITY_ANON
2460 || ! DECL_VISIBILITY_SPECIFIED (decl
))
2461 constrain_visibility (decl
, tvis
, false);
2463 else if (no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/true))
2464 /* DR 757: A type without linkage shall not be used as the type of a
2465 variable or function with linkage, unless
2466 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2467 o the variable or function is not used (3.2 [basic.def.odr]) or is
2468 defined in the same translation unit.
2470 Since non-extern "C" decls need to be defined in the same
2471 translation unit, we can make the type internal. */
2472 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2474 /* If visibility changed and DECL already has DECL_RTL, ensure
2475 symbol flags are updated. */
2476 if ((DECL_VISIBILITY (decl
) != orig_visibility
2477 || DECL_VISIBILITY_SPECIFIED (decl
) != orig_visibility_specified
)
2478 && ((VAR_P (decl
) && TREE_STATIC (decl
))
2479 || TREE_CODE (decl
) == FUNCTION_DECL
)
2480 && DECL_RTL_SET_P (decl
))
2481 make_decl_rtl (decl
);
2484 /* By default, static data members and function members receive
2485 the visibility of their containing class. */
2488 determine_visibility_from_class (tree decl
, tree class_type
)
2490 if (DECL_VISIBILITY_SPECIFIED (decl
))
2493 if (determine_hidden_inline (decl
))
2494 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2497 /* Default to the class visibility. */
2498 DECL_VISIBILITY (decl
) = CLASSTYPE_VISIBILITY (class_type
);
2499 DECL_VISIBILITY_SPECIFIED (decl
)
2500 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type
);
2503 /* Give the target a chance to override the visibility associated
2506 && (DECL_TINFO_P (decl
)
2507 || (DECL_VTABLE_OR_VTT_P (decl
)
2508 /* Construction virtual tables are not exported because
2509 they cannot be referred to from other object files;
2510 their name is not standardized by the ABI. */
2511 && !DECL_CONSTRUCTION_VTABLE_P (decl
)))
2512 && TREE_PUBLIC (decl
)
2513 && !DECL_REALLY_EXTERN (decl
)
2514 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type
))
2515 targetm
.cxx
.determine_class_data_visibility (decl
);
2518 /* Returns true iff DECL is an inline that should get hidden visibility
2519 because of -fvisibility-inlines-hidden. */
2522 determine_hidden_inline (tree decl
)
2524 return (visibility_options
.inlines_hidden
2525 /* Don't do this for inline templates; specializations might not be
2526 inline, and we don't want them to inherit the hidden
2527 visibility. We'll set it here for all inline instantiations. */
2528 && !processing_template_decl
2529 && TREE_CODE (decl
) == FUNCTION_DECL
2530 && DECL_DECLARED_INLINE_P (decl
)
2531 && (! DECL_LANG_SPECIFIC (decl
)
2532 || ! DECL_EXPLICIT_INSTANTIATION (decl
)));
2535 /* Constrain the visibility of a class TYPE based on the visibility of its
2536 field types. Warn if any fields require lesser visibility. */
2539 constrain_class_visibility (tree type
)
2545 int vis
= type_visibility (type
);
2547 if (vis
== VISIBILITY_ANON
2548 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type
)))
2551 /* Don't warn about visibility if the class has explicit visibility. */
2552 if (CLASSTYPE_VISIBILITY_SPECIFIED (type
))
2553 vis
= VISIBILITY_INTERNAL
;
2555 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
2556 if (TREE_CODE (t
) == FIELD_DECL
&& TREE_TYPE (t
) != error_mark_node
2557 && !DECL_ARTIFICIAL (t
))
2559 tree ftype
= strip_pointer_or_array_types (TREE_TYPE (t
));
2560 int subvis
= type_visibility (ftype
);
2562 if (subvis
== VISIBILITY_ANON
)
2564 if (!in_main_input_context())
2566 tree nlt
= no_linkage_check (ftype
, /*relaxed_p=*/false);
2569 if (same_type_p (TREE_TYPE (t
), nlt
))
2570 warning (OPT_Wsubobject_linkage
, "\
2571 %qT has a field %qD whose type has no linkage",
2574 warning (OPT_Wsubobject_linkage
, "\
2575 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2579 warning (OPT_Wsubobject_linkage
, "\
2580 %qT has a field %qD whose type uses the anonymous namespace",
2584 else if (MAYBE_CLASS_TYPE_P (ftype
)
2585 && vis
< VISIBILITY_HIDDEN
2586 && subvis
>= VISIBILITY_HIDDEN
)
2587 warning (OPT_Wattributes
, "\
2588 %qT declared with greater visibility than the type of its field %qD",
2592 binfo
= TYPE_BINFO (type
);
2593 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
2595 int subvis
= type_visibility (TREE_TYPE (t
));
2597 if (subvis
== VISIBILITY_ANON
)
2599 if (!in_main_input_context())
2601 tree nlt
= no_linkage_check (TREE_TYPE (t
), /*relaxed_p=*/false);
2604 if (same_type_p (TREE_TYPE (t
), nlt
))
2605 warning (OPT_Wsubobject_linkage
, "\
2606 %qT has a base %qT whose type has no linkage",
2607 type
, TREE_TYPE (t
));
2609 warning (OPT_Wsubobject_linkage
, "\
2610 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2611 type
, TREE_TYPE (t
), nlt
);
2614 warning (OPT_Wsubobject_linkage
, "\
2615 %qT has a base %qT whose type uses the anonymous namespace",
2616 type
, TREE_TYPE (t
));
2619 else if (vis
< VISIBILITY_HIDDEN
2620 && subvis
>= VISIBILITY_HIDDEN
)
2621 warning (OPT_Wattributes
, "\
2622 %qT declared with greater visibility than its base %qT",
2623 type
, TREE_TYPE (t
));
2627 /* Functions for adjusting the visibility of a tagged type and its nested
2628 types and declarations when it gets a name for linkage purposes from a
2631 static void bt_reset_linkage_1 (binding_entry
, void *);
2632 static void bt_reset_linkage_2 (binding_entry
, void *);
2634 /* First reset the visibility of all the types. */
2637 reset_type_linkage_1 (tree type
)
2639 set_linkage_according_to_type (type
, TYPE_MAIN_DECL (type
));
2640 if (CLASS_TYPE_P (type
))
2641 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2642 bt_reset_linkage_1
, NULL
);
2645 bt_reset_linkage_1 (binding_entry b
, void */
*data*/
)
2647 reset_type_linkage_1 (b
->type
);
2650 /* Then reset the visibility of any static data members or member
2651 functions that use those types. */
2654 reset_decl_linkage (tree decl
)
2656 if (TREE_PUBLIC (decl
))
2658 if (DECL_CLONED_FUNCTION_P (decl
))
2660 TREE_PUBLIC (decl
) = true;
2661 DECL_INTERFACE_KNOWN (decl
) = false;
2662 determine_visibility (decl
);
2663 tentative_decl_linkage (decl
);
2667 reset_type_linkage_2 (tree type
)
2669 if (CLASS_TYPE_P (type
))
2671 if (tree vt
= CLASSTYPE_VTABLES (type
))
2673 tree name
= mangle_vtbl_for_type (type
);
2674 DECL_NAME (vt
) = name
;
2675 SET_DECL_ASSEMBLER_NAME (vt
, name
);
2676 reset_decl_linkage (vt
);
2678 if (tree ti
= CLASSTYPE_TYPEINFO_VAR (type
))
2680 tree name
= mangle_typeinfo_for_type (type
);
2681 DECL_NAME (ti
) = name
;
2682 SET_DECL_ASSEMBLER_NAME (ti
, name
);
2683 TREE_TYPE (name
) = type
;
2684 reset_decl_linkage (ti
);
2686 for (tree m
= TYPE_FIELDS (type
); m
; m
= DECL_CHAIN (m
))
2688 tree mem
= STRIP_TEMPLATE (m
);
2689 if (TREE_CODE (mem
) == VAR_DECL
|| TREE_CODE (mem
) == FUNCTION_DECL
)
2690 reset_decl_linkage (mem
);
2692 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type
),
2693 bt_reset_linkage_2
, NULL
);
2698 bt_reset_linkage_2 (binding_entry b
, void */
*data*/
)
2700 reset_type_linkage_2 (b
->type
);
2703 reset_type_linkage (tree type
)
2705 reset_type_linkage_1 (type
);
2706 reset_type_linkage_2 (type
);
2709 /* Set up our initial idea of what the linkage of DECL should be. */
2712 tentative_decl_linkage (tree decl
)
2714 if (DECL_INTERFACE_KNOWN (decl
))
2715 /* We've already made a decision as to how this function will
2717 else if (vague_linkage_p (decl
))
2719 if (TREE_CODE (decl
) == FUNCTION_DECL
2720 && decl_defined_p (decl
))
2722 DECL_EXTERNAL (decl
) = 1;
2723 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2724 note_vague_linkage_fn (decl
);
2725 /* A non-template inline function with external linkage will
2726 always be COMDAT. As we must eventually determine the
2727 linkage of all functions, and as that causes writes to
2728 the data mapped in from the PCH file, it's advantageous
2729 to mark the functions at this point. */
2730 if (DECL_DECLARED_INLINE_P (decl
)
2731 && (!DECL_IMPLICIT_INSTANTIATION (decl
)
2732 || DECL_DEFAULTED_FN (decl
)))
2734 /* This function must have external linkage, as
2735 otherwise DECL_INTERFACE_KNOWN would have been
2737 gcc_assert (TREE_PUBLIC (decl
));
2738 comdat_linkage (decl
);
2739 DECL_INTERFACE_KNOWN (decl
) = 1;
2742 else if (VAR_P (decl
))
2743 maybe_commonize_var (decl
);
2747 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2748 for DECL has not already been determined, do so now by setting
2749 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2750 function is called entities with vague linkage whose definitions
2751 are available must have TREE_PUBLIC set.
2753 If this function decides to place DECL in COMDAT, it will set
2754 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2755 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2756 callers defer that decision until it is clear that DECL is actually
2760 import_export_decl (tree decl
)
2765 tree class_type
= NULL_TREE
;
2767 if (DECL_INTERFACE_KNOWN (decl
))
2770 /* We cannot determine what linkage to give to an entity with vague
2771 linkage until the end of the file. For example, a virtual table
2772 for a class will be defined if and only if the key method is
2773 defined in this translation unit. As a further example, consider
2774 that when compiling a translation unit that uses PCH file with
2775 "-frepo" it would be incorrect to make decisions about what
2776 entities to emit when building the PCH; those decisions must be
2777 delayed until the repository information has been processed. */
2778 gcc_assert (at_eof
);
2779 /* Object file linkage for explicit instantiations is handled in
2780 mark_decl_instantiated. For static variables in functions with
2781 vague linkage, maybe_commonize_var is used.
2783 Therefore, the only declarations that should be provided to this
2784 function are those with external linkage that are:
2786 * implicit instantiations of function templates
2790 * implicit instantiations of static data members of class
2797 Furthermore, all entities that reach this point must have a
2798 definition available in this translation unit.
2800 The following assertions check these conditions. */
2801 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2802 /* Any code that creates entities with TREE_PUBLIC cleared should
2803 also set DECL_INTERFACE_KNOWN. */
2804 gcc_assert (TREE_PUBLIC (decl
));
2805 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2806 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2807 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
2808 || DECL_DECLARED_INLINE_P (decl
));
2810 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2811 || DECL_VTABLE_OR_VTT_P (decl
)
2812 || DECL_TINFO_P (decl
));
2813 /* Check that a definition of DECL is available in this translation
2815 gcc_assert (!DECL_REALLY_EXTERN (decl
));
2817 /* Assume that DECL will not have COMDAT linkage. */
2819 /* Assume that DECL will not be imported into this translation
2823 /* See if the repository tells us whether or not to emit DECL in
2824 this translation unit. */
2825 emit_p
= repo_emit_p (decl
);
2828 else if (emit_p
== 1)
2830 /* The repository indicates that this entity should be defined
2831 here. Make sure the back end honors that request. */
2833 /* Output the definition as an ordinary strong definition. */
2834 DECL_EXTERNAL (decl
) = 0;
2835 DECL_INTERFACE_KNOWN (decl
) = 1;
2840 /* We have already decided what to do with this DECL; there is no
2841 need to check anything further. */
2843 else if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
2845 class_type
= DECL_CONTEXT (decl
);
2846 import_export_class (class_type
);
2847 if (CLASSTYPE_INTERFACE_KNOWN (class_type
)
2848 && CLASSTYPE_INTERFACE_ONLY (class_type
))
2850 else if ((!flag_weak
|| TARGET_WEAK_NOT_IN_ARCHIVE_TOC
)
2851 && !CLASSTYPE_USE_TEMPLATE (class_type
)
2852 && CLASSTYPE_KEY_METHOD (class_type
)
2853 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
)))
2854 /* The ABI requires that all virtual tables be emitted with
2855 COMDAT linkage. However, on systems where COMDAT symbols
2856 don't show up in the table of contents for a static
2857 archive, or on systems without weak symbols (where we
2858 approximate COMDAT linkage by using internal linkage), the
2859 linker will report errors about undefined symbols because
2860 it will not see the virtual table definition. Therefore,
2861 in the case that we know that the virtual table will be
2862 emitted in only one translation unit, we make the virtual
2863 table an ordinary definition with external linkage. */
2864 DECL_EXTERNAL (decl
) = 0;
2865 else if (CLASSTYPE_INTERFACE_KNOWN (class_type
))
2867 /* CLASS_TYPE is being exported from this translation unit,
2868 so DECL should be defined here. */
2869 if (!flag_weak
&& CLASSTYPE_EXPLICIT_INSTANTIATION (class_type
))
2870 /* If a class is declared in a header with the "extern
2871 template" extension, then it will not be instantiated,
2872 even in translation units that would normally require
2873 it. Often such classes are explicitly instantiated in
2874 one translation unit. Therefore, the explicit
2875 instantiation must be made visible to other translation
2877 DECL_EXTERNAL (decl
) = 0;
2880 /* The generic C++ ABI says that class data is always
2881 COMDAT, even if there is a key function. Some
2882 variants (e.g., the ARM EABI) says that class data
2883 only has COMDAT linkage if the class data might be
2884 emitted in more than one translation unit. When the
2885 key method can be inline and is inline, we still have
2886 to arrange for comdat even though
2887 class_data_always_comdat is false. */
2888 if (!CLASSTYPE_KEY_METHOD (class_type
)
2889 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
))
2890 || targetm
.cxx
.class_data_always_comdat ())
2892 /* The ABI requires COMDAT linkage. Normally, we
2893 only emit COMDAT things when they are needed;
2894 make sure that we realize that this entity is
2901 else if (!flag_implicit_templates
2902 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type
))
2907 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2909 tree type
= TREE_TYPE (DECL_NAME (decl
));
2910 if (CLASS_TYPE_P (type
))
2913 import_export_class (type
);
2914 if (CLASSTYPE_INTERFACE_KNOWN (type
)
2915 && TYPE_POLYMORPHIC_P (type
)
2916 && CLASSTYPE_INTERFACE_ONLY (type
)
2917 /* If -fno-rtti was specified, then we cannot be sure
2918 that RTTI information will be emitted with the
2919 virtual table of the class, so we must emit it
2920 wherever it is used. */
2925 if (CLASSTYPE_INTERFACE_KNOWN (type
)
2926 && !CLASSTYPE_INTERFACE_ONLY (type
))
2928 comdat_p
= (targetm
.cxx
.class_data_always_comdat ()
2929 || (CLASSTYPE_KEY_METHOD (type
)
2930 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type
))));
2935 DECL_EXTERNAL (decl
) = 0;
2945 else if (DECL_TEMPLOID_INSTANTIATION (decl
))
2947 /* DECL is an implicit instantiation of a function or static
2949 if ((flag_implicit_templates
2950 && !flag_use_repository
)
2951 || (flag_implicit_inline_templates
2952 && TREE_CODE (decl
) == FUNCTION_DECL
2953 && DECL_DECLARED_INLINE_P (decl
)))
2956 /* If we are not implicitly generating templates, then mark
2957 this entity as undefined in this translation unit. */
2960 else if (DECL_FUNCTION_MEMBER_P (decl
))
2962 if (!DECL_DECLARED_INLINE_P (decl
))
2964 tree ctype
= DECL_CONTEXT (decl
);
2965 import_export_class (ctype
);
2966 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2968 DECL_NOT_REALLY_EXTERN (decl
)
2969 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
2970 || (DECL_DECLARED_INLINE_P (decl
)
2971 && ! flag_implement_inlines
2972 && !DECL_VINDEX (decl
)));
2974 if (!DECL_NOT_REALLY_EXTERN (decl
))
2975 DECL_EXTERNAL (decl
) = 1;
2977 /* Always make artificials weak. */
2978 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
2981 maybe_make_one_only (decl
);
2992 /* If we are importing DECL into this translation unit, mark is
2993 an undefined here. */
2994 DECL_EXTERNAL (decl
) = 1;
2995 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2999 /* If we decided to put DECL in COMDAT, mark it accordingly at
3001 comdat_linkage (decl
);
3004 DECL_INTERFACE_KNOWN (decl
) = 1;
3007 /* Return an expression that performs the destruction of DECL, which
3008 must be a VAR_DECL whose type has a non-trivial destructor, or is
3009 an array whose (innermost) elements have a non-trivial destructor. */
3012 build_cleanup (tree decl
)
3014 tree clean
= cxx_maybe_build_cleanup (decl
, tf_warning_or_error
);
3015 gcc_assert (clean
!= NULL_TREE
);
3019 /* Returns the initialization guard variable for the variable DECL,
3020 which has static storage duration. */
3023 get_guard (tree decl
)
3028 sname
= mangle_guard_variable (decl
);
3029 guard
= get_global_binding (sname
);
3034 /* We use a type that is big enough to contain a mutex as well
3035 as an integer counter. */
3036 guard_type
= targetm
.cxx
.guard_type ();
3037 guard
= build_decl (DECL_SOURCE_LOCATION (decl
),
3038 VAR_DECL
, sname
, guard_type
);
3040 /* The guard should have the same linkage as what it guards. */
3041 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
3042 TREE_STATIC (guard
) = TREE_STATIC (decl
);
3043 DECL_COMMON (guard
) = DECL_COMMON (decl
);
3044 DECL_COMDAT (guard
) = DECL_COMDAT (decl
);
3045 CP_DECL_THREAD_LOCAL_P (guard
) = CP_DECL_THREAD_LOCAL_P (decl
);
3046 set_decl_tls_model (guard
, DECL_TLS_MODEL (decl
));
3047 if (DECL_ONE_ONLY (decl
))
3048 make_decl_one_only (guard
, cxx_comdat_group (guard
));
3049 if (TREE_PUBLIC (decl
))
3050 DECL_WEAK (guard
) = DECL_WEAK (decl
);
3051 DECL_VISIBILITY (guard
) = DECL_VISIBILITY (decl
);
3052 DECL_VISIBILITY_SPECIFIED (guard
) = DECL_VISIBILITY_SPECIFIED (decl
);
3054 DECL_ARTIFICIAL (guard
) = 1;
3055 DECL_IGNORED_P (guard
) = 1;
3056 TREE_USED (guard
) = 1;
3057 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
3062 /* Return an atomic load of src with the appropriate memory model. */
3065 build_atomic_load_byte (tree src
, HOST_WIDE_INT model
)
3067 tree ptr_type
= build_pointer_type (char_type_node
);
3068 tree mem_model
= build_int_cst (integer_type_node
, model
);
3073 size
= tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node
));
3075 fncode
= BUILT_IN_ATOMIC_LOAD_N
+ exact_log2 (size
) + 1;
3076 t
= builtin_decl_implicit ((enum built_in_function
) fncode
);
3078 addr
= build1 (ADDR_EXPR
, ptr_type
, src
);
3079 val
= build_call_expr (t
, 2, addr
, mem_model
);
3083 /* Return those bits of the GUARD variable that should be set when the
3084 guarded entity is actually initialized. */
3087 get_guard_bits (tree guard
)
3089 if (!targetm
.cxx
.guard_mask_bit ())
3091 /* We only set the first byte of the guard, in order to leave room
3092 for a mutex in the high-order bits. */
3093 guard
= build1 (ADDR_EXPR
,
3094 build_pointer_type (TREE_TYPE (guard
)),
3096 guard
= build1 (NOP_EXPR
,
3097 build_pointer_type (char_type_node
),
3099 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
3105 /* Return an expression which determines whether or not the GUARD
3106 variable has already been initialized. */
3109 get_guard_cond (tree guard
, bool thread_safe
)
3114 guard
= get_guard_bits (guard
);
3116 guard
= build_atomic_load_byte (guard
, MEMMODEL_ACQUIRE
);
3118 /* Mask off all but the low bit. */
3119 if (targetm
.cxx
.guard_mask_bit ())
3121 guard_value
= integer_one_node
;
3122 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3123 guard_value
= fold_convert (TREE_TYPE (guard
), guard_value
);
3124 guard
= cp_build_binary_op (input_location
,
3125 BIT_AND_EXPR
, guard
, guard_value
,
3126 tf_warning_or_error
);
3129 guard_value
= integer_zero_node
;
3130 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
3131 guard_value
= fold_convert (TREE_TYPE (guard
), guard_value
);
3132 return cp_build_binary_op (input_location
,
3133 EQ_EXPR
, guard
, guard_value
,
3134 tf_warning_or_error
);
3137 /* Return an expression which sets the GUARD variable, indicating that
3138 the variable being guarded has been initialized. */
3141 set_guard (tree guard
)
3145 /* Set the GUARD to one. */
3146 guard
= get_guard_bits (guard
);
3147 guard_init
= integer_one_node
;
3148 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
3149 guard_init
= fold_convert (TREE_TYPE (guard
), guard_init
);
3150 return cp_build_modify_expr (input_location
, guard
, NOP_EXPR
, guard_init
,
3151 tf_warning_or_error
);
3154 /* Returns true iff we can tell that VAR does not have a dynamic
3158 var_defined_without_dynamic_init (tree var
)
3160 /* If it's defined in another TU, we can't tell. */
3161 if (DECL_EXTERNAL (var
))
3163 /* If it has a non-trivial destructor, registering the destructor
3164 counts as dynamic initialization. */
3165 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var
)))
3167 /* If it's in this TU, its initializer has been processed, unless
3168 it's a case of self-initialization, then DECL_INITIALIZED_P is
3169 false while the initializer is handled by finish_id_expression. */
3170 if (!DECL_INITIALIZED_P (var
))
3172 /* If it has no initializer or a constant one, it's not dynamic. */
3173 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var
)
3174 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var
));
3177 /* Returns true iff VAR is a variable that needs uses to be
3178 wrapped for possible dynamic initialization. */
3181 var_needs_tls_wrapper (tree var
)
3183 return (!error_operand_p (var
)
3184 && CP_DECL_THREAD_LOCAL_P (var
)
3185 && !DECL_GNU_TLS_P (var
)
3186 && !DECL_FUNCTION_SCOPE_P (var
)
3187 && !var_defined_without_dynamic_init (var
));
3190 /* Get the FUNCTION_DECL for the shared TLS init function for this
3191 translation unit. */
3194 get_local_tls_init_fn (void)
3196 tree sname
= get_identifier ("__tls_init");
3197 tree fn
= get_global_binding (sname
);
3200 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
3201 build_function_type (void_type_node
,
3203 SET_DECL_LANGUAGE (fn
, lang_c
);
3204 TREE_PUBLIC (fn
) = false;
3205 DECL_ARTIFICIAL (fn
) = true;
3207 set_global_binding (fn
);
3212 /* Get a FUNCTION_DECL for the init function for the thread_local
3213 variable VAR. The init function will be an alias to the function
3214 that initializes all the non-local TLS variables in the translation
3215 unit. The init function is only used by the wrapper function. */
3218 get_tls_init_fn (tree var
)
3220 /* Only C++11 TLS vars need this init fn. */
3221 if (!var_needs_tls_wrapper (var
))
3224 /* If -fno-extern-tls-init, assume that we don't need to call
3225 a tls init function for a variable defined in another TU. */
3226 if (!flag_extern_tls_init
&& DECL_EXTERNAL (var
))
3229 /* If the variable is internal, or if we can't generate aliases,
3230 call the local init function directly. */
3231 if (!TREE_PUBLIC (var
) || !TARGET_SUPPORTS_ALIASES
)
3232 return get_local_tls_init_fn ();
3234 tree sname
= mangle_tls_init_fn (var
);
3235 tree fn
= get_global_binding (sname
);
3238 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
3239 build_function_type (void_type_node
,
3241 SET_DECL_LANGUAGE (fn
, lang_c
);
3242 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3243 DECL_ARTIFICIAL (fn
) = true;
3244 DECL_COMDAT (fn
) = DECL_COMDAT (var
);
3245 DECL_EXTERNAL (fn
) = DECL_EXTERNAL (var
);
3246 if (DECL_ONE_ONLY (var
))
3247 make_decl_one_only (fn
, cxx_comdat_group (fn
));
3248 if (TREE_PUBLIC (var
))
3250 tree obtype
= strip_array_types (non_reference (TREE_TYPE (var
)));
3251 /* If the variable is defined somewhere else and might have static
3252 initialization, make the init function a weak reference. */
3253 if ((!TYPE_NEEDS_CONSTRUCTING (obtype
)
3254 || TYPE_HAS_CONSTEXPR_CTOR (obtype
))
3255 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype
)
3256 && DECL_EXTERNAL (var
))
3259 DECL_WEAK (fn
) = DECL_WEAK (var
);
3261 DECL_VISIBILITY (fn
) = DECL_VISIBILITY (var
);
3262 DECL_VISIBILITY_SPECIFIED (fn
) = DECL_VISIBILITY_SPECIFIED (var
);
3263 DECL_DLLIMPORT_P (fn
) = DECL_DLLIMPORT_P (var
);
3264 DECL_IGNORED_P (fn
) = 1;
3267 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3269 set_global_binding (fn
);
3274 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3275 variable VAR. The wrapper function calls the init function (if any) for
3276 VAR and then returns a reference to VAR. The wrapper function is used
3277 in place of VAR everywhere VAR is mentioned. */
3280 get_tls_wrapper_fn (tree var
)
3282 /* Only C++11 TLS vars need this wrapper fn. */
3283 if (!var_needs_tls_wrapper (var
))
3286 tree sname
= mangle_tls_wrapper_fn (var
);
3287 tree fn
= get_global_binding (sname
);
3290 /* A named rvalue reference is an lvalue, so the wrapper should
3291 always return an lvalue reference. */
3292 tree type
= non_reference (TREE_TYPE (var
));
3293 type
= build_reference_type (type
);
3294 tree fntype
= build_function_type (type
, void_list_node
);
3295 fn
= build_lang_decl (FUNCTION_DECL
, sname
, fntype
);
3296 SET_DECL_LANGUAGE (fn
, lang_c
);
3297 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
3298 DECL_ARTIFICIAL (fn
) = true;
3299 DECL_IGNORED_P (fn
) = 1;
3300 /* The wrapper is inline and emitted everywhere var is used. */
3301 DECL_DECLARED_INLINE_P (fn
) = true;
3302 if (TREE_PUBLIC (var
))
3304 comdat_linkage (fn
);
3305 #ifdef HAVE_GAS_HIDDEN
3306 /* Make the wrapper bind locally; there's no reason to share
3307 the wrapper between multiple shared objects. */
3308 DECL_VISIBILITY (fn
) = VISIBILITY_INTERNAL
;
3309 DECL_VISIBILITY_SPECIFIED (fn
) = true;
3312 if (!TREE_PUBLIC (fn
))
3313 DECL_INTERFACE_KNOWN (fn
) = true;
3315 note_vague_linkage_fn (fn
);
3318 /* We want CSE to commonize calls to the wrapper, but marking it as
3319 pure is unsafe since it has side-effects. I guess we need a new
3320 ECF flag even weaker than ECF_PURE. FIXME! */
3321 DECL_PURE_P (fn
) = true;
3324 DECL_BEFRIENDING_CLASSES (fn
) = var
;
3326 set_global_binding (fn
);
3331 /* At EOF, generate the definition for the TLS wrapper function FN:
3334 if (init_fn) init_fn();
3339 generate_tls_wrapper (tree fn
)
3341 tree var
= DECL_BEFRIENDING_CLASSES (fn
);
3343 start_preparsed_function (fn
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
3344 tree body
= begin_function_body ();
3345 /* Only call the init fn if there might be one. */
3346 if (tree init_fn
= get_tls_init_fn (var
))
3348 tree if_stmt
= NULL_TREE
;
3349 /* If init_fn is a weakref, make sure it exists before calling. */
3350 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn
)))
3352 if_stmt
= begin_if_stmt ();
3353 tree addr
= cp_build_addr_expr (init_fn
, tf_warning_or_error
);
3354 tree cond
= cp_build_binary_op (DECL_SOURCE_LOCATION (var
),
3355 NE_EXPR
, addr
, nullptr_node
,
3356 tf_warning_or_error
);
3357 finish_if_stmt_cond (cond
, if_stmt
);
3359 finish_expr_stmt (build_cxx_call
3360 (init_fn
, 0, NULL
, tf_warning_or_error
));
3363 finish_then_clause (if_stmt
);
3364 finish_if_stmt (if_stmt
);
3368 /* If there's no initialization, the wrapper is a constant function. */
3369 TREE_READONLY (fn
) = true;
3370 finish_return_stmt (convert_from_reference (var
));
3371 finish_function_body (body
);
3372 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3375 /* Start the process of running a particular set of global constructors
3376 or destructors. Subroutine of do_[cd]tors. Also called from
3377 vtv_start_verification_constructor_init_function. */
3380 start_objects (int method_type
, int initp
)
3386 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3388 if (initp
!= DEFAULT_INIT_PRIORITY
)
3398 sprintf (type
, "sub_%c%c%.5u", method_type
, joiner
, initp
);
3401 sprintf (type
, "sub_%c", method_type
);
3403 fndecl
= build_lang_decl (FUNCTION_DECL
,
3404 get_file_function_name (type
),
3405 build_function_type_list (void_type_node
,
3407 start_preparsed_function (fndecl
, /*attrs=*/NULL_TREE
, SF_PRE_PARSED
);
3409 TREE_PUBLIC (current_function_decl
) = 0;
3411 /* Mark as artificial because it's not explicitly in the user's
3413 DECL_ARTIFICIAL (current_function_decl
) = 1;
3415 /* Mark this declaration as used to avoid spurious warnings. */
3416 TREE_USED (current_function_decl
) = 1;
3418 /* Mark this function as a global constructor or destructor. */
3419 if (method_type
== 'I')
3420 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
3422 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
3424 body
= begin_compound_stmt (BCS_FN_BODY
);
3429 /* Finish the process of running a particular set of global constructors
3430 or destructors. Subroutine of do_[cd]tors. */
3433 finish_objects (int method_type
, int initp
, tree body
)
3438 finish_compound_stmt (body
);
3439 fn
= finish_function (/*inline_p=*/false);
3441 if (method_type
== 'I')
3443 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
3444 decl_init_priority_insert (fn
, initp
);
3448 DECL_STATIC_DESTRUCTOR (fn
) = 1;
3449 decl_fini_priority_insert (fn
, initp
);
3452 expand_or_defer_fn (fn
);
3455 /* The names of the parameters to the function created to handle
3456 initializations and destructions for objects with static storage
3458 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3459 #define PRIORITY_IDENTIFIER "__priority"
3461 /* The name of the function we create to handle initializations and
3462 destructions for objects with static storage duration. */
3463 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3465 /* The declaration for the __INITIALIZE_P argument. */
3466 static GTY(()) tree initialize_p_decl
;
3468 /* The declaration for the __PRIORITY argument. */
3469 static GTY(()) tree priority_decl
;
3471 /* The declaration for the static storage duration function. */
3472 static GTY(()) tree ssdf_decl
;
3474 /* All the static storage duration functions created in this
3475 translation unit. */
3476 static GTY(()) vec
<tree
, va_gc
> *ssdf_decls
;
3478 /* A map from priority levels to information about that priority
3479 level. There may be many such levels, so efficient lookup is
3481 static splay_tree priority_info_map
;
3483 /* Begins the generation of the function that will handle all
3484 initialization and destruction of objects with static storage
3485 duration. The function generated takes two parameters of type
3486 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3487 nonzero, it performs initializations. Otherwise, it performs
3488 destructions. It only performs those initializations or
3489 destructions with the indicated __PRIORITY. The generated function
3492 It is assumed that this function will only be called once per
3493 translation unit. */
3496 start_static_storage_duration_function (unsigned count
)
3500 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
3502 /* Create the identifier for this function. It will be of the form
3503 SSDF_IDENTIFIER_<number>. */
3504 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, count
);
3506 type
= build_function_type_list (void_type_node
,
3507 integer_type_node
, integer_type_node
,
3510 /* Create the FUNCTION_DECL itself. */
3511 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
3512 get_identifier (id
),
3514 TREE_PUBLIC (ssdf_decl
) = 0;
3515 DECL_ARTIFICIAL (ssdf_decl
) = 1;
3517 /* Put this function in the list of functions to be called from the
3518 static constructors and destructors. */
3521 vec_alloc (ssdf_decls
, 32);
3523 /* Take this opportunity to initialize the map from priority
3524 numbers to information about that priority level. */
3525 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
3526 /*delete_key_fn=*/0,
3527 /*delete_value_fn=*/
3528 (splay_tree_delete_value_fn
) &free
);
3530 /* We always need to generate functions for the
3531 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3532 priorities later, we'll be sure to find the
3533 DEFAULT_INIT_PRIORITY. */
3534 get_priority_info (DEFAULT_INIT_PRIORITY
);
3537 vec_safe_push (ssdf_decls
, ssdf_decl
);
3539 /* Create the argument list. */
3540 initialize_p_decl
= cp_build_parm_decl
3541 (ssdf_decl
, get_identifier (INITIALIZE_P_IDENTIFIER
), integer_type_node
);
3542 TREE_USED (initialize_p_decl
) = 1;
3543 priority_decl
= cp_build_parm_decl
3544 (ssdf_decl
, get_identifier (PRIORITY_IDENTIFIER
), integer_type_node
);
3545 TREE_USED (priority_decl
) = 1;
3547 DECL_CHAIN (initialize_p_decl
) = priority_decl
;
3548 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
3550 /* Put the function in the global scope. */
3551 pushdecl (ssdf_decl
);
3553 /* Start the function itself. This is equivalent to declaring the
3556 static void __ssdf (int __initialize_p, init __priority_p);
3558 It is static because we only need to call this function from the
3559 various constructor and destructor functions for this module. */
3560 start_preparsed_function (ssdf_decl
,
3561 /*attrs=*/NULL_TREE
,
3564 /* Set up the scope of the outermost block in the function. */
3565 body
= begin_compound_stmt (BCS_FN_BODY
);
3570 /* Finish the generation of the function which performs initialization
3571 and destruction of objects with static storage duration. After
3572 this point, no more such objects can be created. */
3575 finish_static_storage_duration_function (tree body
)
3577 /* Close out the function. */
3578 finish_compound_stmt (body
);
3579 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3582 /* Return the information about the indicated PRIORITY level. If no
3583 code to handle this level has yet been generated, generate the
3584 appropriate prologue. */
3586 static priority_info
3587 get_priority_info (int priority
)
3592 n
= splay_tree_lookup (priority_info_map
,
3593 (splay_tree_key
) priority
);
3596 /* Create a new priority information structure, and insert it
3598 pi
= XNEW (struct priority_info_s
);
3599 pi
->initializations_p
= 0;
3600 pi
->destructions_p
= 0;
3601 splay_tree_insert (priority_info_map
,
3602 (splay_tree_key
) priority
,
3603 (splay_tree_value
) pi
);
3606 pi
= (priority_info
) n
->value
;
3611 /* The effective initialization priority of a DECL. */
3613 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3614 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3615 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3617 /* Whether a DECL needs a guard to protect it against multiple
3620 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3621 || DECL_ONE_ONLY (decl) \
3622 || DECL_WEAK (decl)))
3624 /* Called from one_static_initialization_or_destruction(),
3626 Walks the initializer list of a global variable and looks for
3627 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3628 and that have their DECL_CONTEXT() == NULL.
3629 For each such temporary variable, set their DECL_CONTEXT() to
3630 the current function. This is necessary because otherwise
3631 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3632 when trying to refer to a temporary variable that does not have
3633 it's DECL_CONTECT() properly set. */
3635 fix_temporary_vars_context_r (tree
*node
,
3639 gcc_assert (current_function_decl
);
3641 if (TREE_CODE (*node
) == BIND_EXPR
)
3645 for (var
= BIND_EXPR_VARS (*node
); var
; var
= DECL_CHAIN (var
))
3648 && DECL_ARTIFICIAL (var
)
3649 && !DECL_CONTEXT (var
))
3650 DECL_CONTEXT (var
) = current_function_decl
;
3656 /* Set up to handle the initialization or destruction of DECL. If
3657 INITP is nonzero, we are initializing the variable. Otherwise, we
3658 are destroying it. */
3661 one_static_initialization_or_destruction (tree decl
, tree init
, bool initp
)
3663 tree guard_if_stmt
= NULL_TREE
;
3666 /* If we are supposed to destruct and there's a trivial destructor,
3667 nothing has to be done. */
3669 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3672 /* Trick the compiler into thinking we are at the file and line
3673 where DECL was declared so that error-messages make sense, and so
3674 that the debugger will show somewhat sensible file and line
3676 input_location
= DECL_SOURCE_LOCATION (decl
);
3678 /* Make sure temporary variables in the initialiser all have
3679 their DECL_CONTEXT() set to a value different from NULL_TREE.
3680 This can happen when global variables initializers are built.
3681 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3682 the temporary variables that might have been generated in the
3683 accompanying initializers is NULL_TREE, meaning the variables have been
3684 declared in the global namespace.
3685 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3686 of the temporaries are set to the current function decl. */
3687 cp_walk_tree_without_duplicates (&init
,
3688 fix_temporary_vars_context_r
,
3695 Access control for implicit calls to the constructors,
3696 the conversion functions, or the destructor called to
3697 create and destroy a static data member is performed as
3698 if these calls appeared in the scope of the member's
3701 we pretend we are in a static member function of the class of
3702 which the DECL is a member. */
3703 if (member_p (decl
))
3705 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
3706 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
3709 /* Assume we don't need a guard. */
3711 /* We need a guard if this is an object with external linkage that
3712 might be initialized in more than one place. (For example, a
3713 static data member of a template, when the data member requires
3715 if (NEEDS_GUARD_P (decl
))
3719 guard
= get_guard (decl
);
3721 /* When using __cxa_atexit, we just check the GUARD as we would
3722 for a local static. */
3723 if (flag_use_cxa_atexit
)
3725 /* When using __cxa_atexit, we never try to destroy
3726 anything from a static destructor. */
3728 guard_cond
= get_guard_cond (guard
, false);
3730 /* If we don't have __cxa_atexit, then we will be running
3731 destructors from .fini sections, or their equivalents. So,
3732 we need to know how many times we've tried to initialize this
3733 object. We do initializations only if the GUARD is zero,
3734 i.e., if we are the first to initialize the variable. We do
3735 destructions only if the GUARD is one, i.e., if we are the
3736 last to destroy the variable. */
3739 = cp_build_binary_op (input_location
,
3741 cp_build_unary_op (PREINCREMENT_EXPR
,
3744 tf_warning_or_error
),
3746 tf_warning_or_error
);
3749 = cp_build_binary_op (input_location
,
3751 cp_build_unary_op (PREDECREMENT_EXPR
,
3754 tf_warning_or_error
),
3756 tf_warning_or_error
);
3758 guard_if_stmt
= begin_if_stmt ();
3759 finish_if_stmt_cond (guard_cond
, guard_if_stmt
);
3763 /* If we're using __cxa_atexit, we have not already set the GUARD,
3764 so we must do so now. */
3765 if (guard
&& initp
&& flag_use_cxa_atexit
)
3766 finish_expr_stmt (set_guard (guard
));
3768 /* Perform the initialization or destruction. */
3773 finish_expr_stmt (init
);
3774 if (sanitize_flags_p (SANITIZE_ADDRESS
, decl
))
3776 varpool_node
*vnode
= varpool_node::get (decl
);
3778 vnode
->dynamically_initialized
= 1;
3782 /* If we're using __cxa_atexit, register a function that calls the
3783 destructor for the object. */
3784 if (flag_use_cxa_atexit
)
3785 finish_expr_stmt (register_dtor_fn (decl
));
3788 finish_expr_stmt (build_cleanup (decl
));
3790 /* Finish the guard if-stmt, if necessary. */
3793 finish_then_clause (guard_if_stmt
);
3794 finish_if_stmt (guard_if_stmt
);
3797 /* Now that we're done with DECL we don't need to pretend to be a
3798 member of its class any longer. */
3799 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
3800 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
3803 /* Generate code to do the initialization or destruction of the decls in VARS,
3804 a TREE_LIST of VAR_DECL with static storage duration.
3805 Whether initialization or destruction is performed is specified by INITP. */
3808 do_static_initialization_or_destruction (tree vars
, bool initp
)
3810 tree node
, init_if_stmt
, cond
;
3812 /* Build the outer if-stmt to check for initialization or destruction. */
3813 init_if_stmt
= begin_if_stmt ();
3814 cond
= initp
? integer_one_node
: integer_zero_node
;
3815 cond
= cp_build_binary_op (input_location
,
3819 tf_warning_or_error
);
3820 finish_if_stmt_cond (cond
, init_if_stmt
);
3822 /* To make sure dynamic construction doesn't access globals from other
3823 compilation units where they might not be yet constructed, for
3824 -fsanitize=address insert __asan_before_dynamic_init call that
3825 prevents access to either all global variables that need construction
3826 in other compilation units, or at least those that haven't been
3827 initialized yet. Variables that need dynamic construction in
3828 the current compilation unit are kept accessible. */
3829 if (initp
&& (flag_sanitize
& SANITIZE_ADDRESS
))
3830 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3834 tree decl
= TREE_VALUE (node
);
3835 tree priority_if_stmt
;
3839 /* If we don't need a destructor, there's nothing to do. Avoid
3840 creating a possibly empty if-stmt. */
3841 if (!initp
&& TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3843 node
= TREE_CHAIN (node
);
3847 /* Remember that we had an initialization or finalization at this
3849 priority
= DECL_EFFECTIVE_INIT_PRIORITY (decl
);
3850 pi
= get_priority_info (priority
);
3852 pi
->initializations_p
= 1;
3854 pi
->destructions_p
= 1;
3856 /* Conditionalize this initialization on being in the right priority
3857 and being initializing/finalizing appropriately. */
3858 priority_if_stmt
= begin_if_stmt ();
3859 cond
= cp_build_binary_op (input_location
,
3862 build_int_cst (NULL_TREE
, priority
),
3863 tf_warning_or_error
);
3864 finish_if_stmt_cond (cond
, priority_if_stmt
);
3866 /* Process initializers with same priority. */
3868 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node
)) == priority
;
3869 node
= TREE_CHAIN (node
))
3870 /* Do one initialization or destruction. */
3871 one_static_initialization_or_destruction (TREE_VALUE (node
),
3872 TREE_PURPOSE (node
), initp
);
3874 /* Finish up the priority if-stmt body. */
3875 finish_then_clause (priority_if_stmt
);
3876 finish_if_stmt (priority_if_stmt
);
3880 /* Revert what __asan_before_dynamic_init did by calling
3881 __asan_after_dynamic_init. */
3882 if (initp
&& (flag_sanitize
& SANITIZE_ADDRESS
))
3883 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3885 /* Finish up the init/destruct if-stmt body. */
3886 finish_then_clause (init_if_stmt
);
3887 finish_if_stmt (init_if_stmt
);
3890 /* VARS is a list of variables with static storage duration which may
3891 need initialization and/or finalization. Remove those variables
3892 that don't really need to be initialized or finalized, and return
3893 the resulting list. The order in which the variables appear in
3894 VARS is in reverse order of the order in which they should actually
3895 be initialized. The list we return is in the unreversed order;
3896 i.e., the first variable should be initialized first. */
3899 prune_vars_needing_no_initialization (tree
*vars
)
3902 tree result
= NULL_TREE
;
3907 tree decl
= TREE_VALUE (t
);
3908 tree init
= TREE_PURPOSE (t
);
3910 /* Deal gracefully with error. */
3911 if (error_operand_p (decl
))
3913 var
= &TREE_CHAIN (t
);
3917 /* The only things that can be initialized are variables. */
3918 gcc_assert (VAR_P (decl
));
3920 /* If this object is not defined, we don't need to do anything
3922 if (DECL_EXTERNAL (decl
))
3924 var
= &TREE_CHAIN (t
);
3928 /* Also, if the initializer already contains errors, we can bail
3930 if (init
&& TREE_CODE (init
) == TREE_LIST
3931 && value_member (error_mark_node
, init
))
3933 var
= &TREE_CHAIN (t
);
3937 /* This variable is going to need initialization and/or
3938 finalization, so we add it to the list. */
3939 *var
= TREE_CHAIN (t
);
3940 TREE_CHAIN (t
) = result
;
3947 /* Make sure we have told the back end about all the variables in
3951 write_out_vars (tree vars
)
3955 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3957 tree var
= TREE_VALUE (v
);
3958 if (!var_finalized_p (var
))
3960 import_export_decl (var
);
3961 rest_of_decl_compilation (var
, 1, 1);
3966 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3967 (otherwise) that will initialize all global objects with static
3968 storage duration having the indicated PRIORITY. */
3971 generate_ctor_or_dtor_function (bool constructor_p
, int priority
,
3979 input_location
= *locus
;
3981 /* Was: locus->line++; */
3983 /* We use `I' to indicate initialization and `D' to indicate
3985 function_key
= constructor_p
? 'I' : 'D';
3987 /* We emit the function lazily, to avoid generating empty
3988 global constructors and destructors. */
3991 /* For Objective-C++, we may need to initialize metadata found in this module.
3992 This must be done _before_ any other static initializations. */
3993 if (c_dialect_objc () && (priority
== DEFAULT_INIT_PRIORITY
)
3994 && constructor_p
&& objc_static_init_needed_p ())
3996 body
= start_objects (function_key
, priority
);
3997 objc_generate_static_init_call (NULL_TREE
);
4000 /* Call the static storage duration function with appropriate
4002 FOR_EACH_VEC_SAFE_ELT (ssdf_decls
, i
, fndecl
)
4004 /* Calls to pure or const functions will expand to nothing. */
4005 if (! (flags_from_decl_or_type (fndecl
) & (ECF_CONST
| ECF_PURE
)))
4010 body
= start_objects (function_key
, priority
);
4012 call
= cp_build_function_call_nary (fndecl
, tf_warning_or_error
,
4013 build_int_cst (NULL_TREE
,
4015 build_int_cst (NULL_TREE
,
4018 finish_expr_stmt (call
);
4022 /* Close out the function. */
4024 finish_objects (function_key
, priority
, body
);
4027 /* Generate constructor and destructor functions for the priority
4031 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n
, void * data
)
4033 location_t
*locus
= (location_t
*) data
;
4034 int priority
= (int) n
->key
;
4035 priority_info pi
= (priority_info
) n
->value
;
4037 /* Generate the functions themselves, but only if they are really
4039 if (pi
->initializations_p
)
4040 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority
, locus
);
4041 if (pi
->destructions_p
)
4042 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority
, locus
);
4044 /* Keep iterating. */
4048 /* Return C++ property of T, based on given operation OP. */
4051 cpp_check (tree t
, cpp_operation op
)
4055 case HAS_DEPENDENT_TEMPLATE_ARGS
:
4057 tree ti
= CLASSTYPE_TEMPLATE_INFO (t
);
4060 ++processing_template_decl
;
4061 const bool dep
= any_dependent_template_arguments_p (TI_ARGS (ti
));
4062 --processing_template_decl
;
4066 return DECL_PURE_VIRTUAL_P (t
);
4067 case IS_CONSTRUCTOR
:
4068 return DECL_CONSTRUCTOR_P (t
);
4070 return DECL_DESTRUCTOR_P (t
);
4071 case IS_COPY_CONSTRUCTOR
:
4072 return DECL_COPY_CONSTRUCTOR_P (t
);
4073 case IS_MOVE_CONSTRUCTOR
:
4074 return DECL_MOVE_CONSTRUCTOR_P (t
);
4076 return TREE_CODE (t
) == TEMPLATE_DECL
;
4078 return trivial_type_p (t
);
4084 /* Collect source file references recursively, starting from NAMESPC. */
4087 collect_source_refs (tree namespc
)
4089 /* Iterate over names in this name space. */
4090 for (tree t
= NAMESPACE_LEVEL (namespc
)->names
; t
; t
= TREE_CHAIN (t
))
4091 if (DECL_IS_BUILTIN (t
))
4093 else if (TREE_CODE (t
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (t
))
4094 collect_source_refs (t
);
4096 collect_source_ref (DECL_SOURCE_FILE (t
));
4099 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4100 starting from NAMESPC. */
4103 collect_ada_namespace (tree namespc
, const char *source_file
)
4105 tree decl
= NAMESPACE_LEVEL (namespc
)->names
;
4107 /* Collect decls from this namespace. This will skip
4108 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4109 collect_ada_nodes (decl
, source_file
);
4111 /* Now scan for namespace children, and dump them. */
4112 for (; decl
; decl
= TREE_CHAIN (decl
))
4113 if (TREE_CODE (decl
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (decl
))
4114 collect_ada_namespace (decl
, source_file
);
4117 /* Returns true iff there is a definition available for variable or
4121 decl_defined_p (tree decl
)
4123 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4124 return (DECL_INITIAL (decl
) != NULL_TREE
4125 /* A pending instantiation of a friend temploid is defined. */
4126 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
4127 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4128 (DECL_TI_TEMPLATE (decl
)))));
4131 gcc_assert (VAR_P (decl
));
4132 return !DECL_EXTERNAL (decl
);
4136 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4140 An integral constant-expression can only involve ... const
4141 variables of integral or enumeration types initialized with
4142 constant expressions ...
4144 C++0x also allows constexpr variables and temporaries initialized
4145 with constant expressions. We handle the former here, but the latter
4146 are just folded away in cxx_eval_constant_expression.
4148 The standard does not require that the expression be non-volatile.
4149 G++ implements the proposed correction in DR 457. */
4152 decl_constant_var_p (tree decl
)
4154 if (!decl_maybe_constant_var_p (decl
))
4157 /* We don't know if a template static data member is initialized with
4158 a constant expression until we instantiate its initializer. Even
4159 in the case of a constexpr variable, we can't treat it as a
4160 constant until its initializer is complete in case it's used in
4161 its own initializer. */
4162 maybe_instantiate_decl (decl
);
4163 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
);
4166 /* Returns true if DECL could be a symbolic constant variable, depending on
4170 decl_maybe_constant_var_p (tree decl
)
4172 tree type
= TREE_TYPE (decl
);
4175 if (DECL_DECLARED_CONSTEXPR_P (decl
))
4177 if (DECL_HAS_VALUE_EXPR_P (decl
))
4178 /* A proxy isn't constant. */
4180 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4181 /* References can be constant. */;
4182 else if (CP_TYPE_CONST_NON_VOLATILE_P (type
)
4183 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4184 /* And const integers. */;
4188 if (DECL_INITIAL (decl
)
4189 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
4190 /* We know the initializer, and it isn't constant. */
4196 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4197 called from grokfndecl and grokvardecl; in all modes it is called from
4198 cp_write_global_declarations. */
4201 no_linkage_error (tree decl
)
4203 if (cxx_dialect
>= cxx11
&& decl_defined_p (decl
))
4204 /* In C++11 it's ok if the decl is defined. */
4206 tree t
= no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false);
4208 /* The type that got us on no_linkage_decls must have gotten a name for
4209 linkage purposes. */;
4210 else if (CLASS_TYPE_P (t
) && TYPE_BEING_DEFINED (t
))
4211 /* The type might end up having a typedef name for linkage purposes. */
4212 vec_safe_push (no_linkage_decls
, decl
);
4213 else if (TYPE_UNNAMED_P (t
))
4216 if (cxx_dialect
>= cxx11
)
4217 d
= permerror (DECL_SOURCE_LOCATION (decl
), "%q#D, declared using "
4218 "unnamed type, is used but never defined", decl
);
4219 else if (DECL_EXTERN_C_P (decl
))
4220 /* Allow this; it's pretty common in C. */;
4221 else if (VAR_P (decl
))
4222 /* DRs 132, 319 and 389 seem to indicate types with
4223 no linkage can only be used to declare extern "C"
4224 entities. Since it's not always an error in the
4225 ISO C++ 90 Standard, we only issue a warning. */
4226 d
= warning_at (DECL_SOURCE_LOCATION (decl
), 0, "unnamed type "
4227 "with no linkage used to declare variable %q#D with "
4230 d
= permerror (DECL_SOURCE_LOCATION (decl
), "unnamed type with no "
4231 "linkage used to declare function %q#D with linkage",
4233 if (d
&& is_typedef_decl (TYPE_NAME (t
)))
4234 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t
)), "%q#D does not refer "
4235 "to the unqualified type, so it is not used for linkage",
4238 else if (cxx_dialect
>= cxx11
)
4240 if (VAR_P (decl
) || !DECL_PURE_VIRTUAL_P (decl
))
4241 permerror (DECL_SOURCE_LOCATION (decl
),
4242 "%q#D, declared using local type "
4243 "%qT, is used but never defined", decl
, t
);
4245 else if (VAR_P (decl
))
4246 warning_at (DECL_SOURCE_LOCATION (decl
), 0, "type %qT with no linkage "
4247 "used to declare variable %q#D with linkage", t
, decl
);
4249 permerror (DECL_SOURCE_LOCATION (decl
), "type %qT with no linkage used "
4250 "to declare function %q#D with linkage", t
, decl
);
4253 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4256 collect_all_refs (const char *source_file
)
4258 collect_ada_namespace (global_namespace
, source_file
);
4261 /* Clear DECL_EXTERNAL for NODE. */
4264 clear_decl_external (struct cgraph_node
*node
, void * /*data*/)
4266 DECL_EXTERNAL (node
->decl
) = 0;
4270 /* Build up the function to run dynamic initializers for thread_local
4271 variables in this translation unit and alias the init functions for the
4272 individual variables to it. */
4275 handle_tls_init (void)
4277 tree vars
= prune_vars_needing_no_initialization (&tls_aggregates
);
4278 if (vars
== NULL_TREE
)
4281 location_t loc
= DECL_SOURCE_LOCATION (TREE_VALUE (vars
));
4283 write_out_vars (vars
);
4285 tree guard
= build_decl (loc
, VAR_DECL
, get_identifier ("__tls_guard"),
4287 TREE_PUBLIC (guard
) = false;
4288 TREE_STATIC (guard
) = true;
4289 DECL_ARTIFICIAL (guard
) = true;
4290 DECL_IGNORED_P (guard
) = true;
4291 TREE_USED (guard
) = true;
4292 CP_DECL_THREAD_LOCAL_P (guard
) = true;
4293 set_decl_tls_model (guard
, decl_default_tls_model (guard
));
4294 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
4296 tree fn
= get_local_tls_init_fn ();
4297 start_preparsed_function (fn
, NULL_TREE
, SF_PRE_PARSED
);
4298 tree body
= begin_function_body ();
4299 tree if_stmt
= begin_if_stmt ();
4300 tree cond
= cp_build_unary_op (TRUTH_NOT_EXPR
, guard
, false,
4301 tf_warning_or_error
);
4302 finish_if_stmt_cond (cond
, if_stmt
);
4303 finish_expr_stmt (cp_build_modify_expr (loc
, guard
, NOP_EXPR
,
4305 tf_warning_or_error
));
4306 for (; vars
; vars
= TREE_CHAIN (vars
))
4308 tree var
= TREE_VALUE (vars
);
4309 tree init
= TREE_PURPOSE (vars
);
4310 one_static_initialization_or_destruction (var
, init
, true);
4312 /* Output init aliases even with -fno-extern-tls-init. */
4313 if (TARGET_SUPPORTS_ALIASES
&& TREE_PUBLIC (var
))
4315 tree single_init_fn
= get_tls_init_fn (var
);
4316 if (single_init_fn
== NULL_TREE
)
4319 = cgraph_node::get_create (fn
)->create_same_body_alias
4320 (single_init_fn
, fn
);
4321 gcc_assert (alias
!= NULL
);
4325 finish_then_clause (if_stmt
);
4326 finish_if_stmt (if_stmt
);
4327 finish_function_body (body
);
4328 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4331 /* We're at the end of compilation, so generate any mangling aliases that
4332 we've been saving up, if DECL is going to be output and ID2 isn't
4333 already taken by another declaration. */
4336 generate_mangling_alias (tree decl
, tree id2
)
4338 struct cgraph_node
*n
= NULL
;
4340 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4342 n
= cgraph_node::get (decl
);
4344 /* Don't create an alias to an unreferenced function. */
4349 = mangled_decls
->find_slot_with_hash (id2
, IDENTIFIER_HASH_VALUE (id2
),
4352 /* If there's a declaration already using this mangled name,
4353 don't create a compatibility alias that conflicts. */
4357 tree alias
= make_alias_for (decl
, id2
);
4360 DECL_IGNORED_P (alias
) = 1;
4361 TREE_PUBLIC (alias
) = TREE_PUBLIC (decl
);
4362 DECL_VISIBILITY (alias
) = DECL_VISIBILITY (decl
);
4363 if (vague_linkage_p (decl
))
4364 DECL_WEAK (alias
) = 1;
4367 n
->create_same_body_alias (alias
, decl
);
4369 varpool_node::create_extra_name_alias (alias
, decl
);
4372 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4373 the end of translation, for compatibility across bugs in the mangling
4377 note_mangling_alias (tree decl
, tree id2
)
4379 if (TARGET_SUPPORTS_ALIASES
)
4381 if (!defer_mangling_aliases
)
4382 generate_mangling_alias (decl
, id2
);
4385 vec_safe_push (mangling_aliases
, decl
);
4386 vec_safe_push (mangling_aliases
, id2
);
4391 /* Emit all mangling aliases that were deferred up to this point. */
4394 generate_mangling_aliases ()
4396 while (!vec_safe_is_empty (mangling_aliases
))
4398 tree id2
= mangling_aliases
->pop();
4399 tree decl
= mangling_aliases
->pop();
4400 generate_mangling_alias (decl
, id2
);
4402 defer_mangling_aliases
= false;
4405 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4406 set. NEED_WARNING is true if we must warn about collisions. We do
4407 this to spot changes in mangling that may require compatibility
4411 record_mangling (tree decl
, bool need_warning
)
4414 mangled_decls
= hash_table
<mangled_decl_hash
>::create_ggc (499);
4416 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
4417 tree id
= DECL_ASSEMBLER_NAME_RAW (decl
);
4419 = mangled_decls
->find_slot_with_hash (id
, IDENTIFIER_HASH_VALUE (id
),
4422 /* If this is already an alias, remove the alias, because the real
4423 decl takes precedence. */
4424 if (*slot
&& DECL_ARTIFICIAL (*slot
) && DECL_IGNORED_P (*slot
))
4425 if (symtab_node
*n
= symtab_node::get (*slot
))
4426 if (n
->cpp_implicit_alias
)
4434 else if (need_warning
)
4436 error_at (DECL_SOURCE_LOCATION (decl
),
4437 "mangling of %q#D as %qE conflicts with a previous mangle",
4439 inform (DECL_SOURCE_LOCATION (*slot
),
4440 "previous mangling %q#D", *slot
);
4441 inform (DECL_SOURCE_LOCATION (decl
),
4442 "a later -fabi-version= (or =0)"
4443 " avoids this error with a change in mangling");
4448 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4449 any existing knowledge of DECL's mangled name meaning DECL. */
4452 overwrite_mangling (tree decl
, tree name
)
4454 if (tree id
= DECL_ASSEMBLER_NAME_RAW (decl
))
4455 if ((TREE_CODE (decl
) == VAR_DECL
4456 || TREE_CODE (decl
) == FUNCTION_DECL
)
4459 = mangled_decls
->find_slot_with_hash (id
, IDENTIFIER_HASH_VALUE (id
),
4463 mangled_decls
->clear_slot (slot
);
4465 /* If this is an alias, remove it from the symbol table. */
4466 if (DECL_ARTIFICIAL (decl
) && DECL_IGNORED_P (decl
))
4467 if (symtab_node
*n
= symtab_node::get (decl
))
4468 if (n
->cpp_implicit_alias
)
4472 DECL_ASSEMBLER_NAME_RAW (decl
) = name
;
4475 /* The entire file is now complete. If requested, dump everything
4482 if (FILE *stream
= dump_begin (raw_dump_id
, &flags
))
4484 dump_node (global_namespace
, flags
& ~TDF_SLIM
, stream
);
4485 dump_end (raw_dump_id
, stream
);
4489 static location_t locus_at_end_of_parsing
;
4491 /* Check the deallocation functions for CODE to see if we want to warn that
4492 only one was defined. */
4495 maybe_warn_sized_delete (enum tree_code code
)
4497 tree sized
= NULL_TREE
;
4498 tree unsized
= NULL_TREE
;
4500 for (ovl_iterator
iter (get_global_binding (ovl_op_identifier (false, code
)));
4504 /* We're only interested in usual deallocation functions. */
4505 if (!usual_deallocation_fn_p (fn
))
4507 if (FUNCTION_ARG_CHAIN (fn
) == void_list_node
)
4512 if (DECL_INITIAL (unsized
) && !DECL_INITIAL (sized
))
4513 warning_at (DECL_SOURCE_LOCATION (unsized
), OPT_Wsized_deallocation
,
4514 "the program should also define %qD", sized
);
4515 else if (!DECL_INITIAL (unsized
) && DECL_INITIAL (sized
))
4516 warning_at (DECL_SOURCE_LOCATION (sized
), OPT_Wsized_deallocation
,
4517 "the program should also define %qD", unsized
);
4520 /* Check the global deallocation functions to see if we want to warn about
4521 defining unsized without sized (or vice versa). */
4524 maybe_warn_sized_delete ()
4526 if (!flag_sized_deallocation
|| !warn_sized_deallocation
)
4528 maybe_warn_sized_delete (DELETE_EXPR
);
4529 maybe_warn_sized_delete (VEC_DELETE_EXPR
);
4532 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4533 look them up when evaluating non-type template parameters. Now we need to
4534 lower them to something the back end can understand. */
4540 FOR_EACH_VARIABLE (node
)
4542 tree d
= node
->decl
;
4543 if (tree init
= DECL_INITIAL (d
))
4544 DECL_INITIAL (d
) = cplus_expand_constant (init
);
4548 /* This routine is called at the end of compilation.
4549 Its job is to create all the code needed to initialize and
4550 destroy the global aggregates. We do the destruction
4551 first, since that way we only need to reverse the decls once. */
4554 c_parse_final_cleanups (void)
4559 unsigned ssdf_count
= 0;
4563 locus_at_end_of_parsing
= input_location
;
4566 /* Bad parse errors. Just forget about it. */
4567 if (! global_bindings_p () || current_class_type
4568 || !vec_safe_is_empty (decl_namespace_list
))
4571 /* This is the point to write out a PCH if we're doing that.
4572 In that case we do not want to do anything else. */
4575 /* Mangle all symbols at PCH creation time. */
4577 FOR_EACH_SYMBOL (node
)
4578 if (! is_a
<varpool_node
*> (node
)
4579 || ! DECL_HARD_REGISTER (node
->decl
))
4580 DECL_ASSEMBLER_NAME (node
->decl
);
4581 c_common_write_pch ();
4583 /* Ensure even the callers don't try to finalize the CU. */
4584 flag_syntax_only
= 1;
4588 timevar_stop (TV_PHASE_PARSING
);
4589 timevar_start (TV_PHASE_DEFERRED
);
4591 symtab
->process_same_body_aliases ();
4593 /* Handle -fdump-ada-spec[-slim] */
4594 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
4596 if (flag_dump_ada_spec_slim
)
4597 collect_source_ref (main_input_filename
);
4599 collect_source_refs (global_namespace
);
4601 dump_ada_specs (collect_all_refs
, cpp_check
);
4604 /* FIXME - huh? was input_line -= 1;*/
4606 /* We now have to write out all the stuff we put off writing out.
4609 o Template specializations that we have not yet instantiated,
4610 but which are needed.
4611 o Initialization and destruction for non-local objects with
4612 static storage duration. (Local objects with static storage
4613 duration are initialized when their scope is first entered,
4614 and are cleaned up via atexit.)
4615 o Virtual function tables.
4617 All of these may cause others to be needed. For example,
4618 instantiating one function may cause another to be needed, and
4619 generating the initializer for an object may cause templates to be
4620 instantiated, etc., etc. */
4622 emit_support_tinfos ();
4631 /* If there are templates that we've put off instantiating, do
4633 instantiate_pending_templates (retries
);
4636 /* Write out virtual tables as required. Writing out the
4637 virtual table for a template class may cause the
4638 instantiation of members of that class. If we write out
4639 vtables then we remove the class from our list so we don't
4640 have to look at it again. */
4641 for (i
= keyed_classes
->length ();
4642 keyed_classes
->iterate (--i
, &t
);)
4643 if (maybe_emit_vtables (t
))
4646 keyed_classes
->unordered_remove (i
);
4649 /* Write out needed type info variables. We have to be careful
4650 looping through unemitted decls, because emit_tinfo_decl may
4651 cause other variables to be needed. New elements will be
4652 appended, and we remove from the vector those that actually
4654 for (i
= unemitted_tinfo_decls
->length ();
4655 unemitted_tinfo_decls
->iterate (--i
, &t
);)
4656 if (emit_tinfo_decl (t
))
4659 unemitted_tinfo_decls
->unordered_remove (i
);
4662 /* The list of objects with static storage duration is built up
4663 in reverse order. We clear STATIC_AGGREGATES so that any new
4664 aggregates added during the initialization of these will be
4665 initialized in the correct order when we next come around the
4667 vars
= prune_vars_needing_no_initialization (&static_aggregates
);
4671 /* We need to start a new initialization function each time
4672 through the loop. That's because we need to know which
4673 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4674 isn't computed until a function is finished, and written
4675 out. That's a deficiency in the back end. When this is
4676 fixed, these initialization functions could all become
4677 inline, with resulting performance improvements. */
4680 /* Set the line and file, so that it is obviously not from
4682 input_location
= locus_at_end_of_parsing
;
4683 ssdf_body
= start_static_storage_duration_function (ssdf_count
);
4685 /* Make sure the back end knows about all the variables. */
4686 write_out_vars (vars
);
4688 /* First generate code to do all the initializations. */
4690 do_static_initialization_or_destruction (vars
, /*initp=*/true);
4692 /* Then, generate code to do all the destructions. Do these
4693 in reverse order so that the most recently constructed
4694 variable is the first destroyed. If we're using
4695 __cxa_atexit, then we don't need to do this; functions
4696 were registered at initialization time to destroy the
4698 if (!flag_use_cxa_atexit
&& vars
)
4700 vars
= nreverse (vars
);
4701 do_static_initialization_or_destruction (vars
, /*initp=*/false);
4706 /* Finish up the static storage duration function for this
4708 input_location
= locus_at_end_of_parsing
;
4709 finish_static_storage_duration_function (ssdf_body
);
4711 /* All those initializations and finalizations might cause
4712 us to need more inline functions, more template
4713 instantiations, etc. */
4716 /* ??? was: locus_at_end_of_parsing.line++; */
4719 /* Now do the same for thread_local variables. */
4722 /* Go through the set of inline functions whose bodies have not
4723 been emitted yet. If out-of-line copies of these functions
4724 are required, emit them. */
4725 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4727 /* Does it need synthesizing? */
4728 if (DECL_DEFAULTED_FN (decl
) && ! DECL_INITIAL (decl
)
4729 && (! DECL_REALLY_EXTERN (decl
) || possibly_inlined_p (decl
)))
4731 /* Even though we're already at the top-level, we push
4732 there again. That way, when we pop back a few lines
4733 hence, all of our state is restored. Otherwise,
4734 finish_function doesn't clean things up, and we end
4735 up with CURRENT_FUNCTION_DECL set. */
4736 push_to_top_level ();
4737 /* The decl's location will mark where it was first
4738 needed. Save that so synthesize method can indicate
4739 where it was needed from, in case of error */
4740 input_location
= DECL_SOURCE_LOCATION (decl
);
4741 synthesize_method (decl
);
4742 pop_from_top_level ();
4746 if (!DECL_INITIAL (decl
) && decl_tls_wrapper_p (decl
))
4747 generate_tls_wrapper (decl
);
4749 if (!DECL_SAVED_TREE (decl
))
4752 cgraph_node
*node
= cgraph_node::get_create (decl
);
4754 /* We lie to the back end, pretending that some functions
4755 are not defined when they really are. This keeps these
4756 functions from being put out unnecessarily. But, we must
4757 stop lying when the functions are referenced, or if they
4758 are not comdat since they need to be put out now. If
4759 DECL_INTERFACE_KNOWN, then we have already set
4760 DECL_EXTERNAL appropriately, so there's no need to check
4761 again, and we do not want to clear DECL_EXTERNAL if a
4762 previous call to import_export_decl set it.
4764 This is done in a separate for cycle, because if some
4765 deferred function is contained in another deferred
4766 function later in deferred_fns varray,
4767 rest_of_compilation would skip this function and we
4768 really cannot expand the same function twice. */
4769 import_export_decl (decl
);
4770 if (DECL_NOT_REALLY_EXTERN (decl
)
4771 && DECL_INITIAL (decl
)
4772 && decl_needed_p (decl
))
4774 if (node
->cpp_implicit_alias
)
4775 node
= node
->get_alias_target ();
4777 node
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
4779 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4780 group, we need to mark all symbols in the same comdat group
4782 if (node
->same_comdat_group
)
4783 for (cgraph_node
*next
4784 = dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
4786 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
4787 next
->call_for_symbol_thunks_and_aliases (clear_decl_external
,
4791 /* If we're going to need to write this function out, and
4792 there's already a body for it, create RTL for it now.
4793 (There might be no body if this is a method we haven't
4794 gotten around to synthesizing yet.) */
4795 if (!DECL_EXTERNAL (decl
)
4796 && decl_needed_p (decl
)
4797 && !TREE_ASM_WRITTEN (decl
)
4798 && !node
->definition
)
4800 /* We will output the function; no longer consider it in this
4802 DECL_DEFER_OUTPUT (decl
) = 0;
4803 /* Generate RTL for this function now that we know we
4805 expand_or_defer_fn (decl
);
4806 /* If we're compiling -fsyntax-only pretend that this
4807 function has been written out so that we don't try to
4809 if (flag_syntax_only
)
4810 TREE_ASM_WRITTEN (decl
) = 1;
4815 if (wrapup_namespace_globals ())
4818 /* Static data members are just like namespace-scope globals. */
4819 FOR_EACH_VEC_SAFE_ELT (pending_statics
, i
, decl
)
4821 if (var_finalized_p (decl
) || DECL_REALLY_EXTERN (decl
)
4822 /* Don't write it out if we haven't seen a definition. */
4823 || (DECL_IN_AGGR_P (decl
) && !DECL_INLINE_VAR_P (decl
)))
4825 import_export_decl (decl
);
4826 /* If this static data member is needed, provide it to the
4828 if (DECL_NOT_REALLY_EXTERN (decl
) && decl_needed_p (decl
))
4829 DECL_EXTERNAL (decl
) = 0;
4831 if (vec_safe_length (pending_statics
) != 0
4832 && wrapup_global_declarations (pending_statics
->address (),
4833 pending_statics
->length ()))
4842 generate_mangling_aliases ();
4844 /* All used inline functions must have a definition at this point. */
4845 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4847 if (/* Check online inline functions that were actually used. */
4848 DECL_ODR_USED (decl
) && DECL_DECLARED_INLINE_P (decl
)
4849 /* If the definition actually was available here, then the
4850 fact that the function was not defined merely represents
4851 that for some reason (use of a template repository,
4852 #pragma interface, etc.) we decided not to emit the
4854 && !DECL_INITIAL (decl
)
4855 /* Don't complain if the template was defined. */
4856 && !(DECL_TEMPLATE_INSTANTIATION (decl
)
4857 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4858 (template_for_substitution (decl
)))))
4860 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
4861 "inline function %qD used but never defined", decl
);
4862 /* Avoid a duplicate warning from check_global_declaration. */
4863 TREE_NO_WARNING (decl
) = 1;
4867 /* So must decls that use a type with no linkage. */
4868 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls
, i
, decl
)
4869 no_linkage_error (decl
);
4871 maybe_warn_sized_delete ();
4873 /* Then, do the Objective-C stuff. This is where all the
4874 Objective-C module stuff gets generated (symtab,
4875 class/protocol/selector lists etc). This must be done after C++
4876 templates, destructors etc. so that selectors used in C++
4877 templates are properly allocated. */
4878 if (c_dialect_objc ())
4879 objc_write_global_declarations ();
4881 /* We give C linkage to static constructors and destructors. */
4882 push_lang_context (lang_name_c
);
4884 /* Generate initialization and destruction functions for all
4885 priorities for which they are required. */
4886 if (priority_info_map
)
4887 splay_tree_foreach (priority_info_map
,
4888 generate_ctor_and_dtor_functions_for_priority
,
4889 /*data=*/&locus_at_end_of_parsing
);
4890 else if (c_dialect_objc () && objc_static_init_needed_p ())
4891 /* If this is obj-c++ and we need a static init, call
4892 generate_ctor_or_dtor_function. */
4893 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4894 DEFAULT_INIT_PRIORITY
,
4895 &locus_at_end_of_parsing
);
4897 /* We're done with the splay-tree now. */
4898 if (priority_info_map
)
4899 splay_tree_delete (priority_info_map
);
4901 /* Generate any missing aliases. */
4902 maybe_apply_pending_pragma_weaks ();
4904 /* We're done with static constructors, so we can go back to "C++"
4906 pop_lang_context ();
4908 if (flag_vtable_verify
)
4910 vtv_recover_class_info ();
4911 vtv_compute_class_hierarchy_transitive_closure ();
4912 vtv_build_vtable_verify_fndecl ();
4915 perform_deferred_noexcept_checks ();
4920 /* The entire file is now complete. If requested, dump everything
4924 if (flag_detailed_statistics
)
4926 dump_tree_statistics ();
4927 dump_time_statistics ();
4930 timevar_stop (TV_PHASE_DEFERRED
);
4931 timevar_start (TV_PHASE_PARSING
);
4933 /* Indicate that we're done with front end processing. */
4937 /* Perform any post compilation-proper cleanups for the C++ front-end.
4938 This should really go away. No front-end should need to do
4939 anything past the compilation process. */
4942 cxx_post_compilation_parsing_cleanups (void)
4944 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS
);
4946 if (flag_vtable_verify
)
4948 /* Generate the special constructor initialization function that
4949 calls __VLTRegisterPairs, and give it a very high
4950 initialization priority. This must be done after
4951 finalize_compilation_unit so that we have accurate
4952 information about which vtable will actually be emitted. */
4953 vtv_generate_init_routine ();
4956 input_location
= locus_at_end_of_parsing
;
4959 validate_conversion_obstack ();
4961 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS
);
4964 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4965 function to call in parse-tree form; it has not yet been
4966 semantically analyzed. ARGS are the arguments to the function.
4967 They have already been semantically analyzed. This may change
4971 build_offset_ref_call_from_tree (tree fn
, vec
<tree
, va_gc
> **args
,
4972 tsubst_flags_t complain
)
4975 vec
<tree
, va_gc
> *orig_args
= NULL
;
4980 object
= TREE_OPERAND (fn
, 0);
4982 if (processing_template_decl
)
4984 gcc_assert (TREE_CODE (fn
) == DOTSTAR_EXPR
4985 || TREE_CODE (fn
) == MEMBER_REF
);
4986 if (type_dependent_expression_p (fn
)
4987 || any_type_dependent_arguments_p (*args
))
4988 return build_min_nt_call_vec (fn
, *args
);
4990 orig_args
= make_tree_vector_copy (*args
);
4992 /* Transform the arguments and add the implicit "this"
4993 parameter. That must be done before the FN is transformed
4994 because we depend on the form of FN. */
4995 make_args_non_dependent (*args
);
4996 object
= build_non_dependent_expr (object
);
4997 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4999 if (TREE_CODE (fn
) == DOTSTAR_EXPR
)
5000 object
= cp_build_addr_expr (object
, complain
);
5001 vec_safe_insert (*args
, 0, object
);
5003 /* Now that the arguments are done, transform FN. */
5004 fn
= build_non_dependent_expr (fn
);
5007 /* A qualified name corresponding to a bound pointer-to-member is
5008 represented as an OFFSET_REF:
5010 struct B { void g(); };
5012 void B::g() { (this->*p)(); } */
5013 if (TREE_CODE (fn
) == OFFSET_REF
)
5015 tree object_addr
= cp_build_addr_expr (object
, complain
);
5016 fn
= TREE_OPERAND (fn
, 1);
5017 fn
= get_member_function_from_ptrfunc (&object_addr
, fn
,
5019 vec_safe_insert (*args
, 0, object_addr
);
5022 if (CLASS_TYPE_P (TREE_TYPE (fn
)))
5023 expr
= build_op_call (fn
, args
, complain
);
5025 expr
= cp_build_function_call_vec (fn
, args
, complain
);
5026 if (processing_template_decl
&& expr
!= error_mark_node
)
5027 expr
= build_min_non_dep_call_vec (expr
, orig_fn
, orig_args
);
5029 if (orig_args
!= NULL
)
5030 release_tree_vector (orig_args
);
5037 check_default_args (tree x
)
5039 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
5040 bool saw_def
= false;
5041 int i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
5042 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
5044 if (TREE_PURPOSE (arg
))
5046 else if (saw_def
&& !PACK_EXPANSION_P (TREE_VALUE (arg
)))
5048 error ("default argument missing for parameter %P of %q+#D", i
, x
);
5049 TREE_PURPOSE (arg
) = error_mark_node
;
5054 /* Return true if function DECL can be inlined. This is used to force
5055 instantiation of methods that might be interesting for inlining. */
5057 possibly_inlined_p (tree decl
)
5059 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
5060 if (DECL_UNINLINABLE (decl
))
5063 return DECL_DECLARED_INLINE_P (decl
);
5064 /* When optimizing, we might inline everything when flatten
5065 attribute or heuristics inlining for size or autoinlining
5070 /* Normally, we can wait until instantiation-time to synthesize DECL.
5071 However, if DECL is a static data member initialized with a constant
5072 or a constexpr function, we need it right now because a reference to
5073 such a data member or a call to such function is not value-dependent.
5074 For a function that uses auto in the return type, we need to instantiate
5075 it to find out its type. For OpenMP user defined reductions, we need
5076 them instantiated for reduction clauses which inline them by hand
5080 maybe_instantiate_decl (tree decl
)
5082 if (DECL_LANG_SPECIFIC (decl
)
5083 && DECL_TEMPLATE_INFO (decl
)
5084 && (decl_maybe_constant_var_p (decl
)
5085 || (TREE_CODE (decl
) == FUNCTION_DECL
5086 && DECL_OMP_DECLARE_REDUCTION_P (decl
))
5087 || undeduced_auto_decl (decl
))
5088 && !DECL_DECLARED_CONCEPT_P (decl
)
5089 && !uses_template_parms (DECL_TI_ARGS (decl
)))
5091 /* Instantiating a function will result in garbage collection. We
5092 must treat this situation as if we were within the body of a
5093 function so as to avoid collecting live data only referenced from
5094 the stack (such as overload resolution candidates). */
5096 instantiate_decl (decl
, /*defer_ok=*/false,
5097 /*expl_inst_class_mem_p=*/false);
5102 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5103 If DECL is a specialization or implicitly declared class member,
5104 generate the actual definition. Return false if something goes
5105 wrong, true otherwise. */
5108 mark_used (tree decl
, tsubst_flags_t complain
)
5110 /* If DECL is a BASELINK for a single function, then treat it just
5111 like the DECL for the function. Otherwise, if the BASELINK is
5112 for an overloaded function, we don't know which function was
5113 actually used until after overload resolution. */
5114 if (BASELINK_P (decl
))
5116 decl
= BASELINK_FUNCTIONS (decl
);
5117 if (really_overloaded_fn (decl
))
5119 decl
= OVL_FIRST (decl
);
5122 /* Set TREE_USED for the benefit of -Wunused. */
5123 TREE_USED (decl
) = 1;
5124 /* And for structured bindings also the underlying decl. */
5125 if (DECL_DECOMPOSITION_P (decl
) && DECL_DECOMP_BASE (decl
))
5126 TREE_USED (DECL_DECOMP_BASE (decl
)) = 1;
5128 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5131 if (DECL_CLONED_FUNCTION_P (decl
))
5132 TREE_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
5134 /* Mark enumeration types as used. */
5135 if (TREE_CODE (decl
) == CONST_DECL
)
5136 used_types_insert (DECL_CONTEXT (decl
));
5138 if (TREE_CODE (decl
) == FUNCTION_DECL
5139 && !maybe_instantiate_noexcept (decl
, complain
))
5142 if (TREE_CODE (decl
) == FUNCTION_DECL
5143 && DECL_DELETED_FN (decl
))
5145 if (DECL_ARTIFICIAL (decl
)
5146 && DECL_CONV_FN_P (decl
)
5147 && LAMBDA_TYPE_P (DECL_CONTEXT (decl
)))
5148 /* We mark a lambda conversion op as deleted if we can't
5149 generate it properly; see maybe_add_lambda_conv_op. */
5150 sorry ("converting lambda that uses %<...%> to function pointer");
5151 else if (complain
& tf_error
)
5153 error ("use of deleted function %qD", decl
);
5154 if (!maybe_explain_implicit_delete (decl
))
5155 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
5160 if (TREE_DEPRECATED (decl
) && (complain
& tf_warning
)
5161 && deprecated_state
!= DEPRECATED_SUPPRESS
)
5162 warn_deprecated_use (decl
, NULL_TREE
);
5164 /* We can only check DECL_ODR_USED on variables or functions with
5165 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5166 might need special handling for. */
5167 if (!VAR_OR_FUNCTION_DECL_P (decl
)
5168 || DECL_LANG_SPECIFIC (decl
) == NULL
5169 || DECL_THUNK_P (decl
))
5171 if (!processing_template_decl
5172 && !require_deduced_type (decl
, complain
))
5177 /* We only want to do this processing once. We don't need to keep trying
5178 to instantiate inline templates, because unit-at-a-time will make sure
5179 we get them compiled before functions that want to inline them. */
5180 if (DECL_ODR_USED (decl
))
5183 /* Normally, we can wait until instantiation-time to synthesize DECL.
5184 However, if DECL is a static data member initialized with a constant
5185 or a constexpr function, we need it right now because a reference to
5186 such a data member or a call to such function is not value-dependent.
5187 For a function that uses auto in the return type, we need to instantiate
5188 it to find out its type. For OpenMP user defined reductions, we need
5189 them instantiated for reduction clauses which inline them by hand
5191 maybe_instantiate_decl (decl
);
5193 if (processing_template_decl
|| in_template_function ())
5196 /* Check this too in case we're within instantiate_non_dependent_expr. */
5197 if (DECL_TEMPLATE_INFO (decl
)
5198 && uses_template_parms (DECL_TI_ARGS (decl
)))
5201 if (!require_deduced_type (decl
, complain
))
5204 if (builtin_pack_fn_p (decl
))
5206 error ("use of built-in parameter pack %qD outside of a template",
5211 /* If we don't need a value, then we don't need to synthesize DECL. */
5212 if (cp_unevaluated_operand
|| in_discarded_stmt
)
5215 DECL_ODR_USED (decl
) = 1;
5216 if (DECL_CLONED_FUNCTION_P (decl
))
5217 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
5219 /* DR 757: A type without linkage shall not be used as the type of a
5220 variable or function with linkage, unless
5221 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5222 o the variable or function is not used (3.2 [basic.def.odr]) or is
5223 defined in the same translation unit. */
5224 if (cxx_dialect
> cxx98
5225 && decl_linkage (decl
) != lk_none
5226 && !DECL_EXTERN_C_P (decl
)
5227 && !DECL_ARTIFICIAL (decl
)
5228 && !decl_defined_p (decl
)
5229 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
5231 if (is_local_extern (decl
))
5232 /* There's no way to define a local extern, and adding it to
5233 the vector interferes with GC, so give an error now. */
5234 no_linkage_error (decl
);
5236 vec_safe_push (no_linkage_decls
, decl
);
5239 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (decl
)
5240 && !DECL_INITIAL (decl
) && !DECL_ARTIFICIAL (decl
))
5241 /* Remember it, so we can check it was defined. */
5242 note_vague_linkage_fn (decl
);
5244 /* Is it a synthesized method that needs to be synthesized? */
5245 if (TREE_CODE (decl
) == FUNCTION_DECL
5246 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
5247 && DECL_DEFAULTED_FN (decl
)
5248 /* A function defaulted outside the class is synthesized either by
5249 cp_finish_decl or instantiate_decl. */
5250 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl
)
5251 && ! DECL_INITIAL (decl
))
5253 /* Defer virtual destructors so that thunks get the right
5255 if (DECL_VIRTUAL_P (decl
) && !at_eof
)
5257 note_vague_linkage_fn (decl
);
5261 /* Remember the current location for a function we will end up
5262 synthesizing. Then we can inform the user where it was
5263 required in the case of error. */
5264 DECL_SOURCE_LOCATION (decl
) = input_location
;
5266 /* Synthesizing an implicitly defined member function will result in
5267 garbage collection. We must treat this situation as if we were
5268 within the body of a function so as to avoid collecting live data
5269 on the stack (such as overload resolution candidates).
5271 We could just let cp_write_global_declarations handle synthesizing
5272 this function by adding it to deferred_fns, but doing
5273 it at the use site produces better error messages. */
5275 synthesize_method (decl
);
5277 /* If this is a synthesized method we don't need to
5278 do the instantiation test below. */
5280 else if (VAR_OR_FUNCTION_DECL_P (decl
)
5281 && DECL_TEMPLATE_INFO (decl
)
5282 && !DECL_DECLARED_CONCEPT_P (decl
)
5283 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
5284 || always_instantiate_p (decl
)))
5285 /* If this is a function or variable that is an instance of some
5286 template, we now know that we will need to actually do the
5287 instantiation. We check that DECL is not an explicit
5288 instantiation because that is not checked in instantiate_decl.
5290 We put off instantiating functions in order to improve compile
5291 times. Maintaining a stack of active functions is expensive,
5292 and the inliner knows to instantiate any functions it might
5293 need. Therefore, we always try to defer instantiation. */
5296 instantiate_decl (decl
, /*defer_ok=*/true,
5297 /*expl_inst_class_mem_p=*/false);
5305 mark_used (tree decl
)
5307 return mark_used (decl
, tf_warning_or_error
);
5311 vtv_start_verification_constructor_init_function (void)
5313 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY
- 1);
5317 vtv_finish_verification_constructor_init_function (tree function_body
)
5321 finish_compound_stmt (function_body
);
5322 fn
= finish_function (/*inline_p=*/false);
5323 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
5324 decl_init_priority_insert (fn
, MAX_RESERVED_INIT_PRIORITY
- 1);
5329 #include "gt-cp-decl2.h"