1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2013 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"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
44 #include "tree-inline.h"
45 #include "c-family/c-pragma.h"
49 #include "pointer-set.h"
50 #include "splay-tree.h"
51 #include "langhooks.h"
52 #include "c-family/c-ada-spec.h"
54 extern cpp_reader
*parse_in
;
56 /* This structure contains information about the initializations
57 and/or destructions required for a particular priority level. */
58 typedef struct priority_info_s
{
59 /* Nonzero if there have been any initializations at this priority
60 throughout the translation unit. */
61 int initializations_p
;
62 /* Nonzero if there have been any destructions at this priority
63 throughout the translation unit. */
67 static void mark_vtable_entries (tree
);
68 static bool maybe_emit_vtables (tree
);
69 static bool acceptable_java_type (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 bool decl_defined_p (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 /* Nonzero if we're done parsing and into end-of-file activities. */
107 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
108 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
109 that apply to the function). */
112 build_memfn_type (tree fntype
, tree ctype
, cp_cv_quals quals
,
113 cp_ref_qualifier rqual
)
119 if (fntype
== error_mark_node
|| ctype
== error_mark_node
)
120 return error_mark_node
;
122 gcc_assert (TREE_CODE (fntype
) == FUNCTION_TYPE
123 || TREE_CODE (fntype
) == METHOD_TYPE
);
125 type_quals
= quals
& ~TYPE_QUAL_RESTRICT
;
126 ctype
= cp_build_qualified_type (ctype
, type_quals
);
127 raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
128 attrs
= TYPE_ATTRIBUTES (fntype
);
129 fntype
= build_method_type_directly (ctype
, TREE_TYPE (fntype
),
130 (TREE_CODE (fntype
) == METHOD_TYPE
131 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype
))
132 : TYPE_ARG_TYPES (fntype
)));
134 fntype
= cp_build_type_attribute_variant (fntype
, attrs
);
136 fntype
= build_ref_qualified_type (fntype
, rqual
);
138 fntype
= build_exception_variant (fntype
, raises
);
143 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
144 return type changed to NEW_RET. */
147 change_return_type (tree new_ret
, tree fntype
)
150 tree args
= TYPE_ARG_TYPES (fntype
);
151 tree raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
152 tree attrs
= TYPE_ATTRIBUTES (fntype
);
154 if (new_ret
== error_mark_node
)
157 if (same_type_p (new_ret
, TREE_TYPE (fntype
)))
160 if (TREE_CODE (fntype
) == FUNCTION_TYPE
)
162 newtype
= build_function_type (new_ret
, args
);
163 newtype
= apply_memfn_quals (newtype
,
164 type_memfn_quals (fntype
),
165 type_memfn_rqual (fntype
));
168 newtype
= build_method_type_directly
169 (class_of_this_parm (fntype
), new_ret
, TREE_CHAIN (args
));
171 newtype
= build_exception_variant (newtype
, raises
);
173 newtype
= cp_build_type_attribute_variant (newtype
, attrs
);
178 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
182 cp_build_parm_decl (tree name
, tree type
)
184 tree parm
= build_decl (input_location
,
185 PARM_DECL
, name
, type
);
186 /* DECL_ARG_TYPE is only used by the back end and the back end never
188 if (!processing_template_decl
)
189 DECL_ARG_TYPE (parm
) = type_passed_as (type
);
191 /* If the type is a pack expansion, then we have a function
193 if (type
&& TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
194 FUNCTION_PARAMETER_PACK_P (parm
) = 1;
199 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
203 build_artificial_parm (tree name
, tree type
)
205 tree parm
= cp_build_parm_decl (name
, type
);
206 DECL_ARTIFICIAL (parm
) = 1;
207 /* All our artificial parms are implicitly `const'; they cannot be
209 TREE_READONLY (parm
) = 1;
213 /* Constructors for types with virtual baseclasses need an "in-charge" flag
214 saying whether this constructor is responsible for initialization of
215 virtual baseclasses or not. All destructors also need this "in-charge"
216 flag, which additionally determines whether or not the destructor should
217 free the memory for the object.
219 This function adds the "in-charge" flag to member function FN if
220 appropriate. It is called from grokclassfn and tsubst.
221 FN must be either a constructor or destructor.
223 The in-charge flag follows the 'this' parameter, and is followed by the
224 VTT parm (if any), then the user-written parms. */
227 maybe_retrofit_in_chrg (tree fn
)
229 tree basetype
, arg_types
, parms
, parm
, fntype
;
231 /* If we've already add the in-charge parameter don't do it again. */
232 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
235 /* When processing templates we can't know, in general, whether or
236 not we're going to have virtual baseclasses. */
237 if (processing_template_decl
)
240 /* We don't need an in-charge parameter for constructors that don't
241 have virtual bases. */
242 if (DECL_CONSTRUCTOR_P (fn
)
243 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
246 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
247 basetype
= TREE_TYPE (TREE_VALUE (arg_types
));
248 arg_types
= TREE_CHAIN (arg_types
);
250 parms
= DECL_CHAIN (DECL_ARGUMENTS (fn
));
252 /* If this is a subobject constructor or destructor, our caller will
253 pass us a pointer to our VTT. */
254 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
256 parm
= build_artificial_parm (vtt_parm_identifier
, vtt_parm_type
);
258 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
259 DECL_CHAIN (parm
) = parms
;
262 /* ...and then to TYPE_ARG_TYPES. */
263 arg_types
= hash_tree_chain (vtt_parm_type
, arg_types
);
265 DECL_HAS_VTT_PARM_P (fn
) = 1;
268 /* Then add the in-charge parm (before the VTT parm). */
269 parm
= build_artificial_parm (in_charge_identifier
, integer_type_node
);
270 DECL_CHAIN (parm
) = parms
;
272 arg_types
= hash_tree_chain (integer_type_node
, arg_types
);
274 /* Insert our new parameter(s) into the list. */
275 DECL_CHAIN (DECL_ARGUMENTS (fn
)) = parms
;
277 /* And rebuild the function type. */
278 fntype
= build_method_type_directly (basetype
, TREE_TYPE (TREE_TYPE (fn
)),
280 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)))
281 fntype
= build_exception_variant (fntype
,
282 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)));
283 if (TYPE_ATTRIBUTES (TREE_TYPE (fn
)))
284 fntype
= (cp_build_type_attribute_variant
285 (fntype
, TYPE_ATTRIBUTES (TREE_TYPE (fn
))));
286 TREE_TYPE (fn
) = fntype
;
288 /* Now we've got the in-charge parameter. */
289 DECL_HAS_IN_CHARGE_PARM_P (fn
) = 1;
292 /* Classes overload their constituent function names automatically.
293 When a function name is declared in a record structure,
294 its name is changed to it overloaded name. Since names for
295 constructors and destructors can conflict, we place a leading
298 CNAME is the name of the class we are grokking for.
300 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
302 FLAGS contains bits saying what's special about today's
303 arguments. DTOR_FLAG == DESTRUCTOR.
305 If FUNCTION is a destructor, then we must add the `auto-delete' field
306 as a second parameter. There is some hair associated with the fact
307 that we must "declare" this variable in the manner consistent with the
308 way the rest of the arguments were declared.
310 QUALS are the qualifiers for the this pointer. */
313 grokclassfn (tree ctype
, tree function
, enum overload_flags flags
)
315 tree fn_name
= DECL_NAME (function
);
317 /* Even within an `extern "C"' block, members get C++ linkage. See
318 [dcl.link] for details. */
319 SET_DECL_LANGUAGE (function
, lang_cplusplus
);
321 if (fn_name
== NULL_TREE
)
323 error ("name missing for member function");
324 fn_name
= get_identifier ("<anonymous>");
325 DECL_NAME (function
) = fn_name
;
328 DECL_CONTEXT (function
) = ctype
;
330 if (flags
== DTOR_FLAG
)
331 DECL_DESTRUCTOR_P (function
) = 1;
333 if (flags
== DTOR_FLAG
|| DECL_CONSTRUCTOR_P (function
))
334 maybe_retrofit_in_chrg (function
);
337 /* Create an ARRAY_REF, checking for the user doing things backwards
341 grok_array_decl (location_t loc
, tree array_expr
, tree index_exp
)
345 tree orig_array_expr
= array_expr
;
346 tree orig_index_exp
= index_exp
;
348 if (error_operand_p (array_expr
) || error_operand_p (index_exp
))
349 return error_mark_node
;
351 if (processing_template_decl
)
353 if (type_dependent_expression_p (array_expr
)
354 || type_dependent_expression_p (index_exp
))
355 return build_min_nt_loc (loc
, ARRAY_REF
, array_expr
, index_exp
,
356 NULL_TREE
, NULL_TREE
);
357 array_expr
= build_non_dependent_expr (array_expr
);
358 index_exp
= build_non_dependent_expr (index_exp
);
361 type
= TREE_TYPE (array_expr
);
363 type
= non_reference (type
);
365 /* If they have an `operator[]', use that. */
366 if (MAYBE_CLASS_TYPE_P (type
) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp
)))
367 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, array_expr
, index_exp
,
368 NULL_TREE
, /*overload=*/NULL
, tf_warning_or_error
);
373 /* Otherwise, create an ARRAY_REF for a pointer or array type.
374 It is a little-known fact that, if `a' is an array and `i' is
375 an int, you can write `i[a]', which means the same thing as
377 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
380 p1
= build_expr_type_conversion (WANT_POINTER
, array_expr
, false);
382 if (TREE_CODE (TREE_TYPE (index_exp
)) == ARRAY_TYPE
)
385 p2
= build_expr_type_conversion (WANT_POINTER
, index_exp
, false);
387 i1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, array_expr
,
389 i2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, index_exp
,
392 if ((p1
&& i2
) && (i1
&& p2
))
393 error ("ambiguous conversion for array subscript");
396 array_expr
= p1
, index_exp
= i2
;
398 array_expr
= p2
, index_exp
= i1
;
401 error ("invalid types %<%T[%T]%> for array subscript",
402 type
, TREE_TYPE (index_exp
));
403 return error_mark_node
;
406 if (array_expr
== error_mark_node
|| index_exp
== error_mark_node
)
407 error ("ambiguous conversion for array subscript");
409 expr
= build_array_ref (input_location
, array_expr
, index_exp
);
411 if (processing_template_decl
&& expr
!= error_mark_node
)
412 return build_min_non_dep (ARRAY_REF
, expr
, orig_array_expr
, orig_index_exp
,
413 NULL_TREE
, NULL_TREE
);
417 /* Given the cast expression EXP, checking out its validity. Either return
418 an error_mark_node if there was an unavoidable error, return a cast to
419 void for trying to delete a pointer w/ the value 0, or return the
420 call to delete. If DOING_VEC is true, we handle things differently
421 for doing an array delete.
422 Implements ARM $5.3.4. This is called from the parser. */
425 delete_sanity (tree exp
, tree size
, bool doing_vec
, int use_global_delete
,
426 tsubst_flags_t complain
)
430 if (exp
== error_mark_node
)
433 if (processing_template_decl
)
435 t
= build_min (DELETE_EXPR
, void_type_node
, exp
, size
);
436 DELETE_EXPR_USE_GLOBAL (t
) = use_global_delete
;
437 DELETE_EXPR_USE_VEC (t
) = doing_vec
;
438 TREE_SIDE_EFFECTS (t
) = 1;
442 /* An array can't have been allocated by new, so complain. */
443 if (TREE_CODE (TREE_TYPE (exp
)) == ARRAY_TYPE
)
444 warning (0, "deleting array %q#E", exp
);
446 t
= build_expr_type_conversion (WANT_POINTER
, exp
, true);
448 if (t
== NULL_TREE
|| t
== error_mark_node
)
450 error ("type %q#T argument given to %<delete%>, expected pointer",
452 return error_mark_node
;
455 type
= TREE_TYPE (t
);
457 /* As of Valley Forge, you can delete a pointer to const. */
459 /* You can't delete functions. */
460 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
462 error ("cannot delete a function. Only pointer-to-objects are "
463 "valid arguments to %<delete%>");
464 return error_mark_node
;
467 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
468 if (VOID_TYPE_P (TREE_TYPE (type
)))
470 warning (0, "deleting %qT is undefined", type
);
474 /* Deleting a pointer with the value zero is valid and has no effect. */
475 if (integer_zerop (t
))
476 return build1 (NOP_EXPR
, void_type_node
, t
);
479 return build_vec_delete (t
, /*maxindex=*/NULL_TREE
,
480 sfk_deleting_destructor
,
481 use_global_delete
, complain
);
483 return build_delete (type
, t
, sfk_deleting_destructor
,
484 LOOKUP_NORMAL
, use_global_delete
,
488 /* Report an error if the indicated template declaration is not the
489 sort of thing that should be a member template. */
492 check_member_template (tree tmpl
)
496 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
497 decl
= DECL_TEMPLATE_RESULT (tmpl
);
499 if (TREE_CODE (decl
) == FUNCTION_DECL
500 || DECL_ALIAS_TEMPLATE_P (tmpl
)
501 || (TREE_CODE (decl
) == TYPE_DECL
502 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))))
504 /* The parser rejects template declarations in local classes. */
505 gcc_assert (!current_function_decl
);
506 /* The parser rejects any use of virtual in a function template. */
507 gcc_assert (!(TREE_CODE (decl
) == FUNCTION_DECL
508 && DECL_VIRTUAL_P (decl
)));
510 /* The debug-information generating code doesn't know what to do
511 with member templates. */
512 DECL_IGNORED_P (tmpl
) = 1;
515 error ("template declaration of %q#D", decl
);
518 /* Return true iff TYPE is a valid Java parameter or return type. */
521 acceptable_java_type (tree type
)
523 if (type
== error_mark_node
)
526 if (VOID_TYPE_P (type
) || TYPE_FOR_JAVA (type
))
528 if (TYPE_PTR_P (type
) || TREE_CODE (type
) == REFERENCE_TYPE
)
530 type
= TREE_TYPE (type
);
531 if (TREE_CODE (type
) == RECORD_TYPE
)
534 if (! TYPE_FOR_JAVA (type
))
536 if (! CLASSTYPE_TEMPLATE_INFO (type
))
538 args
= CLASSTYPE_TI_ARGS (type
);
539 i
= TREE_VEC_LENGTH (args
);
542 type
= TREE_VEC_ELT (args
, i
);
543 if (TYPE_PTR_P (type
))
544 type
= TREE_TYPE (type
);
545 if (! TYPE_FOR_JAVA (type
))
554 /* For a METHOD in a Java class CTYPE, return true if
555 the parameter and return types are valid Java types.
556 Otherwise, print appropriate error messages, and return false. */
559 check_java_method (tree method
)
562 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (method
));
563 tree ret_type
= TREE_TYPE (TREE_TYPE (method
));
565 if (!acceptable_java_type (ret_type
))
567 error ("Java method %qD has non-Java return type %qT",
572 arg_types
= TREE_CHAIN (arg_types
);
573 if (DECL_HAS_IN_CHARGE_PARM_P (method
))
574 arg_types
= TREE_CHAIN (arg_types
);
575 if (DECL_HAS_VTT_PARM_P (method
))
576 arg_types
= TREE_CHAIN (arg_types
);
578 for (; arg_types
!= NULL_TREE
; arg_types
= TREE_CHAIN (arg_types
))
580 tree type
= TREE_VALUE (arg_types
);
581 if (!acceptable_java_type (type
))
583 if (type
!= error_mark_node
)
584 error ("Java method %qD has non-Java parameter type %qT",
592 /* Sanity check: report error if this function FUNCTION is not
593 really a member of the class (CTYPE) it is supposed to belong to.
594 TEMPLATE_PARMS is used to specify the template parameters of a member
595 template passed as FUNCTION_DECL. If the member template is passed as a
596 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
597 from the declaration. If the function is not a function template, it
599 It returns the original declaration for the function, NULL_TREE if
600 no declaration was found, error_mark_node if an error was emitted. */
603 check_classfn (tree ctype
, tree function
, tree template_parms
)
609 if (DECL_USE_TEMPLATE (function
)
610 && !(TREE_CODE (function
) == TEMPLATE_DECL
611 && DECL_TEMPLATE_SPECIALIZATION (function
))
612 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function
)))
613 /* Since this is a specialization of a member template,
614 we're not going to find the declaration in the class.
617 struct S { template <typename T> void f(T); };
618 template <> void S::f(int);
620 we're not going to find `S::f(int)', but there's no
621 reason we should, either. We let our callers know we didn't
622 find the method, but we don't complain. */
625 /* Basic sanity check: for a template function, the template parameters
626 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
627 if (TREE_CODE (function
) == TEMPLATE_DECL
)
630 && !comp_template_parms (template_parms
,
631 DECL_TEMPLATE_PARMS (function
)))
633 error ("template parameter lists provided don%'t match the "
634 "template parameters of %qD", function
);
635 return error_mark_node
;
637 template_parms
= DECL_TEMPLATE_PARMS (function
);
640 /* OK, is this a definition of a member template? */
641 is_template
= (template_parms
!= NULL_TREE
);
643 /* We must enter the scope here, because conversion operators are
644 named by target type, and type equivalence relies on typenames
645 resolving within the scope of CTYPE. */
646 pushed_scope
= push_scope (ctype
);
647 ix
= class_method_index_for_fn (complete_type (ctype
), function
);
650 vec
<tree
, va_gc
> *methods
= CLASSTYPE_METHOD_VEC (ctype
);
651 tree fndecls
, fndecl
= 0;
653 const char *format
= NULL
;
655 for (fndecls
= (*methods
)[ix
];
656 fndecls
; fndecls
= OVL_NEXT (fndecls
))
660 fndecl
= OVL_CURRENT (fndecls
);
661 p1
= TYPE_ARG_TYPES (TREE_TYPE (function
));
662 p2
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
664 /* We cannot simply call decls_match because this doesn't
665 work for static member functions that are pretending to
666 be methods, and because the name may have been changed by
669 /* Get rid of the this parameter on functions that become
671 if (DECL_STATIC_FUNCTION_P (fndecl
)
672 && TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
673 p1
= TREE_CHAIN (p1
);
675 /* A member template definition only matches a member template
677 if (is_template
!= (TREE_CODE (fndecl
) == TEMPLATE_DECL
))
680 /* ref-qualifier or absence of same must match. */
681 if (type_memfn_rqual (TREE_TYPE (function
))
682 != type_memfn_rqual (TREE_TYPE (fndecl
)))
685 /* While finding a match, same types and params are not enough
686 if the function is versioned. Also check version ("target")
688 if (same_type_p (TREE_TYPE (TREE_TYPE (function
)),
689 TREE_TYPE (TREE_TYPE (fndecl
)))
690 && compparms (p1
, p2
)
691 && !targetm
.target_option
.function_versions (function
, fndecl
)
693 || comp_template_parms (template_parms
,
694 DECL_TEMPLATE_PARMS (fndecl
)))
695 && (DECL_TEMPLATE_SPECIALIZATION (function
)
696 == DECL_TEMPLATE_SPECIALIZATION (fndecl
))
697 && (!DECL_TEMPLATE_SPECIALIZATION (function
)
698 || (DECL_TI_TEMPLATE (function
)
699 == DECL_TI_TEMPLATE (fndecl
))))
705 pop_scope (pushed_scope
);
706 return OVL_CURRENT (fndecls
);
709 error_at (DECL_SOURCE_LOCATION (function
),
710 "prototype for %q#D does not match any in class %qT",
712 is_conv_op
= DECL_CONV_FN_P (fndecl
);
715 ix
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
716 fndecls
= (*methods
)[ix
];
719 fndecl
= OVL_CURRENT (fndecls
);
720 fndecls
= OVL_NEXT (fndecls
);
722 if (!fndecls
&& is_conv_op
)
724 if (methods
->length () > (size_t) ++ix
)
726 fndecls
= (*methods
)[ix
];
727 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls
)))
739 format
= N_("candidates are: %+#D");
741 format
= N_("candidate is: %+#D");
742 error (format
, fndecl
);
745 else if (!COMPLETE_TYPE_P (ctype
))
746 cxx_incomplete_type_error (function
, ctype
);
748 error ("no %q#D member function declared in class %qT",
752 pop_scope (pushed_scope
);
753 return error_mark_node
;
756 /* DECL is a function with vague linkage. Remember it so that at the
757 end of the translation unit we can decide whether or not to emit
761 note_vague_linkage_fn (tree decl
)
763 DECL_DEFER_OUTPUT (decl
) = 1;
764 vec_safe_push (deferred_fns
, decl
);
767 /* We have just processed the DECL, which is a static data member.
768 The other parameters are as for cp_finish_decl. */
771 finish_static_data_member_decl (tree decl
,
772 tree init
, bool init_const_expr_p
,
776 DECL_CONTEXT (decl
) = current_class_type
;
778 /* We cannot call pushdecl here, because that would fill in the
779 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
780 the right thing, namely, to put this decl out straight away. */
782 if (! processing_template_decl
)
783 vec_safe_push (pending_statics
, decl
);
785 if (LOCAL_CLASS_P (current_class_type
)
786 /* We already complained about the template definition. */
787 && !DECL_TEMPLATE_INSTANTIATION (decl
))
788 permerror (input_location
, "local class %q#T shall not have static data member %q#D",
789 current_class_type
, decl
);
791 DECL_IN_AGGR_P (decl
) = 1;
793 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
794 && TYPE_DOMAIN (TREE_TYPE (decl
)) == NULL_TREE
)
795 SET_VAR_HAD_UNKNOWN_BOUND (decl
);
797 cp_finish_decl (decl
, init
, init_const_expr_p
, asmspec_tree
, flags
);
800 /* DECLARATOR and DECLSPECS correspond to a class member. The other
801 parameters are as for cp_finish_decl. Return the DECL for the
802 class member declared. */
805 grokfield (const cp_declarator
*declarator
,
806 cp_decl_specifier_seq
*declspecs
,
807 tree init
, bool init_const_expr_p
,
812 const char *asmspec
= 0;
817 && TREE_CODE (init
) == TREE_LIST
818 && TREE_VALUE (init
) == error_mark_node
819 && TREE_CHAIN (init
) == NULL_TREE
)
822 value
= grokdeclarator (declarator
, declspecs
, FIELD
, init
!= 0, &attrlist
);
823 if (! value
|| error_operand_p (value
))
824 /* friend or constructor went bad. */
825 return error_mark_node
;
827 if (TREE_CODE (value
) == TYPE_DECL
&& init
)
829 error ("typedef %qD is initialized (use decltype instead)", value
);
833 /* Pass friendly classes back. */
834 if (value
== void_type_node
)
837 /* Pass friend decls back. */
838 if ((TREE_CODE (value
) == FUNCTION_DECL
839 || TREE_CODE (value
) == TEMPLATE_DECL
)
840 && DECL_CONTEXT (value
) != current_class_type
)
843 name
= DECL_NAME (value
);
845 if (name
!= NULL_TREE
)
847 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
849 error ("explicit template argument list not allowed");
850 return error_mark_node
;
853 if (IDENTIFIER_POINTER (name
)[0] == '_'
854 && ! strcmp (IDENTIFIER_POINTER (name
), "_vptr"))
855 error ("member %qD conflicts with virtual function table field name",
859 /* Stash away type declarations. */
860 if (TREE_CODE (value
) == TYPE_DECL
)
862 DECL_NONLOCAL (value
) = 1;
863 DECL_CONTEXT (value
) = current_class_type
;
869 /* If this is a typedef that names the class for linkage purposes
870 (7.1.3p8), apply any attributes directly to the type. */
871 if (OVERLOAD_TYPE_P (TREE_TYPE (value
))
872 && value
== TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
873 attrflags
= ATTR_FLAG_TYPE_IN_PLACE
;
875 cplus_decl_attributes (&value
, attrlist
, attrflags
);
878 if (decl_spec_seq_has_spec_p (declspecs
, ds_typedef
)
879 && TREE_TYPE (value
) != error_mark_node
880 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value
))) != value
)
881 set_underlying_type (value
);
883 /* It's important that push_template_decl below follows
884 set_underlying_type above so that the created template
885 carries the properly set type of VALUE. */
886 if (processing_template_decl
)
887 value
= push_template_decl (value
);
889 record_locally_defined_typedef (value
);
893 if (DECL_IN_AGGR_P (value
))
895 error ("%qD is already defined in %qT", value
, DECL_CONTEXT (value
));
896 return void_type_node
;
899 if (asmspec_tree
&& asmspec_tree
!= error_mark_node
)
900 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
904 if (TREE_CODE (value
) == FUNCTION_DECL
)
906 /* Initializers for functions are rejected early in the parser.
907 If we get here, it must be a pure specifier for a method. */
908 if (init
== ridpointers
[(int)RID_DELETE
])
910 DECL_DELETED_FN (value
) = 1;
911 DECL_DECLARED_INLINE_P (value
) = 1;
912 DECL_INITIAL (value
) = error_mark_node
;
914 else if (init
== ridpointers
[(int)RID_DEFAULT
])
916 if (defaultable_fn_check (value
))
918 DECL_DEFAULTED_FN (value
) = 1;
919 DECL_INITIALIZED_IN_CLASS_P (value
) = 1;
920 DECL_DECLARED_INLINE_P (value
) = 1;
923 else if (TREE_CODE (init
) == DEFAULT_ARG
)
924 error ("invalid initializer for member function %qD", value
);
925 else if (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
)
927 if (integer_zerop (init
))
928 DECL_PURE_VIRTUAL_P (value
) = 1;
929 else if (error_operand_p (init
))
930 ; /* An error has already been reported. */
932 error ("invalid initializer for member function %qD",
937 gcc_assert (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
);
938 error ("initializer specified for static member function %qD",
942 else if (TREE_CODE (value
) == FIELD_DECL
)
943 /* C++11 NSDMI, keep going. */;
944 else if (!VAR_P (value
))
946 else if (!processing_template_decl
)
948 if (TREE_CODE (init
) == CONSTRUCTOR
)
949 init
= digest_init (TREE_TYPE (value
), init
, tf_warning_or_error
);
950 init
= maybe_constant_init (init
);
952 if (init
!= error_mark_node
&& !TREE_CONSTANT (init
))
954 /* We can allow references to things that are effectively
955 static, since references are initialized with the
957 if (TREE_CODE (TREE_TYPE (value
)) != REFERENCE_TYPE
958 || (TREE_STATIC (init
) == 0
959 && (!DECL_P (init
) || DECL_EXTERNAL (init
) == 0)))
961 error ("field initializer is not constant");
962 init
= error_mark_node
;
968 if (processing_template_decl
&& VAR_OR_FUNCTION_DECL_P (value
))
970 value
= push_template_decl (value
);
971 if (error_operand_p (value
))
972 return error_mark_node
;
976 cplus_decl_attributes (&value
, attrlist
, 0);
978 if (init
&& BRACE_ENCLOSED_INITIALIZER_P (init
)
979 && CONSTRUCTOR_IS_DIRECT_INIT (init
))
980 flags
= LOOKUP_NORMAL
;
982 flags
= LOOKUP_IMPLICIT
;
984 switch (TREE_CODE (value
))
987 finish_static_data_member_decl (value
, init
, init_const_expr_p
,
988 asmspec_tree
, flags
);
993 error ("%<asm%> specifiers are not permitted on non-static data members");
994 if (DECL_INITIAL (value
) == error_mark_node
)
995 init
= error_mark_node
;
996 cp_finish_decl (value
, init
, /*init_const_expr_p=*/false,
998 DECL_IN_AGGR_P (value
) = 1;
1003 set_user_assembler_name (value
, asmspec
);
1005 cp_finish_decl (value
,
1007 /*init_const_expr_p=*/false,
1008 asmspec_tree
, flags
);
1010 /* Pass friends back this way. */
1011 if (DECL_FRIEND_P (value
))
1012 return void_type_node
;
1014 DECL_IN_AGGR_P (value
) = 1;
1023 /* Like `grokfield', but for bitfields.
1024 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1027 grokbitfield (const cp_declarator
*declarator
,
1028 cp_decl_specifier_seq
*declspecs
, tree width
,
1031 tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
, 0, &attrlist
);
1033 if (value
== error_mark_node
)
1034 return NULL_TREE
; /* friends went bad. */
1036 /* Pass friendly classes back. */
1037 if (VOID_TYPE_P (value
))
1038 return void_type_node
;
1040 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value
))
1041 && (POINTER_TYPE_P (value
)
1042 || !dependent_type_p (TREE_TYPE (value
))))
1044 error ("bit-field %qD with non-integral type", value
);
1045 return error_mark_node
;
1048 if (TREE_CODE (value
) == TYPE_DECL
)
1050 error ("cannot declare %qD to be a bit-field type", value
);
1054 /* Usually, finish_struct_1 catches bitfields with invalid types.
1055 But, in the case of bitfields with function type, we confuse
1056 ourselves into thinking they are member functions, so we must
1058 if (TREE_CODE (value
) == FUNCTION_DECL
)
1060 error ("cannot declare bit-field %qD with function type",
1065 if (DECL_IN_AGGR_P (value
))
1067 error ("%qD is already defined in the class %qT", value
,
1068 DECL_CONTEXT (value
));
1069 return void_type_node
;
1072 if (TREE_STATIC (value
))
1074 error ("static member %qD cannot be a bit-field", value
);
1077 cp_finish_decl (value
, NULL_TREE
, false, NULL_TREE
, 0);
1079 if (width
!= error_mark_node
)
1081 /* The width must be an integer type. */
1082 if (!type_dependent_expression_p (width
)
1083 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width
)))
1084 error ("width of bit-field %qD has non-integral type %qT", value
,
1086 DECL_INITIAL (value
) = width
;
1087 SET_DECL_C_BIT_FIELD (value
);
1090 DECL_IN_AGGR_P (value
) = 1;
1093 cplus_decl_attributes (&value
, attrlist
, /*flags=*/0);
1099 /* Returns true iff ATTR is an attribute which needs to be applied at
1100 instantiation time rather than template definition time. */
1103 is_late_template_attribute (tree attr
, tree decl
)
1105 tree name
= get_attribute_name (attr
);
1106 tree args
= TREE_VALUE (attr
);
1107 const struct attribute_spec
*spec
= lookup_attribute_spec (name
);
1111 /* Unknown attribute. */
1114 /* Attribute weak handling wants to write out assembly right away. */
1115 if (is_attribute_p ("weak", name
))
1118 /* Attribute unused is applied directly, as it appertains to
1120 if (is_attribute_p ("unused", name
))
1123 /* If any of the arguments are dependent expressions, we can't evaluate
1124 the attribute until instantiation time. */
1125 for (arg
= args
; arg
; arg
= TREE_CHAIN (arg
))
1127 tree t
= TREE_VALUE (arg
);
1129 /* If the first attribute argument is an identifier, only consider
1130 second and following arguments. Attributes like mode, format,
1131 cleanup and several target specific attributes aren't late
1132 just because they have an IDENTIFIER_NODE as first argument. */
1133 if (arg
== args
&& identifier_p (t
))
1136 if (value_dependent_expression_p (t
)
1137 || type_dependent_expression_p (t
))
1141 if (TREE_CODE (decl
) == TYPE_DECL
1143 || spec
->type_required
)
1145 tree type
= TYPE_P (decl
) ? decl
: TREE_TYPE (decl
);
1147 /* We can't apply any attributes to a completely unknown type until
1148 instantiation time. */
1149 enum tree_code code
= TREE_CODE (type
);
1150 if (code
== TEMPLATE_TYPE_PARM
1151 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
1152 || code
== TYPENAME_TYPE
)
1154 /* Also defer most attributes on dependent types. This is not
1155 necessary in all cases, but is the better default. */
1156 else if (dependent_type_p (type
)
1157 /* But attribute visibility specifically works on
1159 && !is_attribute_p ("visibility", name
))
1168 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1169 applied at instantiation time and return them. If IS_DEPENDENT is true,
1170 the declaration itself is dependent, so all attributes should be applied
1171 at instantiation time. */
1174 splice_template_attributes (tree
*attr_p
, tree decl
)
1177 tree late_attrs
= NULL_TREE
;
1178 tree
*q
= &late_attrs
;
1185 if (is_late_template_attribute (*p
, decl
))
1187 ATTR_IS_DEPENDENT (*p
) = 1;
1189 *p
= TREE_CHAIN (*p
);
1190 q
= &TREE_CHAIN (*q
);
1194 p
= &TREE_CHAIN (*p
);
1200 /* Remove any late attributes from the list in ATTR_P and attach them to
1204 save_template_attributes (tree
*attr_p
, tree
*decl_p
)
1206 tree late_attrs
= splice_template_attributes (attr_p
, *decl_p
);
1208 tree old_attrs
= NULL_TREE
;
1213 if (DECL_P (*decl_p
))
1214 q
= &DECL_ATTRIBUTES (*decl_p
);
1216 q
= &TYPE_ATTRIBUTES (*decl_p
);
1220 /* Merge the late attributes at the beginning with the attribute
1222 late_attrs
= merge_attributes (late_attrs
, *q
);
1225 if (!DECL_P (*decl_p
) && *decl_p
== TYPE_MAIN_VARIANT (*decl_p
))
1227 /* We've added new attributes directly to the main variant, so
1228 now we need to update all of the other variants to include
1229 these new attributes. */
1231 for (variant
= TYPE_NEXT_VARIANT (*decl_p
); variant
;
1232 variant
= TYPE_NEXT_VARIANT (variant
))
1234 gcc_assert (TYPE_ATTRIBUTES (variant
) == old_attrs
);
1235 TYPE_ATTRIBUTES (variant
) = TYPE_ATTRIBUTES (*decl_p
);
1240 /* Like reconstruct_complex_type, but handle also template trees. */
1243 cp_reconstruct_complex_type (tree type
, tree bottom
)
1247 if (TYPE_PTR_P (type
))
1249 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1250 outer
= build_pointer_type_for_mode (inner
, TYPE_MODE (type
),
1251 TYPE_REF_CAN_ALIAS_ALL (type
));
1253 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
1255 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1256 outer
= build_reference_type_for_mode (inner
, TYPE_MODE (type
),
1257 TYPE_REF_CAN_ALIAS_ALL (type
));
1259 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1261 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1262 outer
= build_cplus_array_type (inner
, TYPE_DOMAIN (type
));
1263 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1264 element type qualification will be handled by the recursive
1265 cp_reconstruct_complex_type call and cp_build_qualified_type
1266 for ARRAY_TYPEs changes the element type. */
1269 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
1271 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1272 outer
= build_function_type (inner
, TYPE_ARG_TYPES (type
));
1273 outer
= apply_memfn_quals (outer
,
1274 type_memfn_quals (type
),
1275 type_memfn_rqual (type
));
1277 else if (TREE_CODE (type
) == METHOD_TYPE
)
1279 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1280 /* The build_method_type_directly() routine prepends 'this' to argument list,
1281 so we must compensate by getting rid of it. */
1283 = build_method_type_directly
1284 (class_of_this_parm (type
), inner
,
1285 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
1287 else if (TREE_CODE (type
) == OFFSET_TYPE
)
1289 inner
= cp_reconstruct_complex_type (TREE_TYPE (type
), bottom
);
1290 outer
= build_offset_type (TYPE_OFFSET_BASETYPE (type
), inner
);
1295 if (TYPE_ATTRIBUTES (type
))
1296 outer
= cp_build_type_attribute_variant (outer
, TYPE_ATTRIBUTES (type
));
1297 return cp_build_qualified_type (outer
, cp_type_quals (type
));
1300 /* Replaces any constexpr expression that may be into the attributes
1301 arguments with their reduced value. */
1304 cp_check_const_attributes (tree attributes
)
1307 for (attr
= attributes
; attr
; attr
= TREE_CHAIN (attr
))
1310 for (arg
= TREE_VALUE (attr
); arg
; arg
= TREE_CHAIN (arg
))
1312 tree expr
= TREE_VALUE (arg
);
1314 TREE_VALUE (arg
) = maybe_constant_value (expr
);
1319 /* Like decl_attributes, but handle C++ complexity. */
1322 cplus_decl_attributes (tree
*decl
, tree attributes
, int flags
)
1324 if (*decl
== NULL_TREE
|| *decl
== void_type_node
1325 || *decl
== error_mark_node
)
1328 if (processing_template_decl
)
1330 if (check_for_bare_parameter_packs (attributes
))
1333 save_template_attributes (&attributes
, decl
);
1336 cp_check_const_attributes (attributes
);
1338 if (TREE_CODE (*decl
) == TEMPLATE_DECL
)
1339 decl
= &DECL_TEMPLATE_RESULT (*decl
);
1341 decl_attributes (decl
, attributes
, flags
);
1343 if (TREE_CODE (*decl
) == TYPE_DECL
)
1344 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl
), TREE_TYPE (*decl
));
1347 /* Walks through the namespace- or function-scope anonymous union
1348 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1349 Returns one of the fields for use in the mangled name. */
1352 build_anon_union_vars (tree type
, tree object
)
1354 tree main_decl
= NULL_TREE
;
1357 /* Rather than write the code to handle the non-union case,
1358 just give an error. */
1359 if (TREE_CODE (type
) != UNION_TYPE
)
1361 error ("anonymous struct not inside named type");
1362 return error_mark_node
;
1365 for (field
= TYPE_FIELDS (type
);
1367 field
= DECL_CHAIN (field
))
1372 if (DECL_ARTIFICIAL (field
))
1374 if (TREE_CODE (field
) != FIELD_DECL
)
1376 permerror (input_location
, "%q+#D invalid; an anonymous union can only "
1377 "have non-static data members", field
);
1381 if (TREE_PRIVATE (field
))
1382 permerror (input_location
, "private member %q+#D in anonymous union", field
);
1383 else if (TREE_PROTECTED (field
))
1384 permerror (input_location
, "protected member %q+#D in anonymous union", field
);
1386 if (processing_template_decl
)
1387 ref
= build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
, object
,
1388 DECL_NAME (field
), NULL_TREE
);
1390 ref
= build_class_member_access_expr (object
, field
, NULL_TREE
,
1391 false, tf_warning_or_error
);
1393 if (DECL_NAME (field
))
1397 decl
= build_decl (input_location
,
1398 VAR_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
1399 DECL_ANON_UNION_VAR_P (decl
) = 1;
1400 DECL_ARTIFICIAL (decl
) = 1;
1402 base
= get_base_address (object
);
1403 TREE_PUBLIC (decl
) = TREE_PUBLIC (base
);
1404 TREE_STATIC (decl
) = TREE_STATIC (base
);
1405 DECL_EXTERNAL (decl
) = DECL_EXTERNAL (base
);
1407 SET_DECL_VALUE_EXPR (decl
, ref
);
1408 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1410 decl
= pushdecl (decl
);
1412 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1413 decl
= build_anon_union_vars (TREE_TYPE (field
), ref
);
1417 if (main_decl
== NULL_TREE
)
1424 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1425 anonymous union, then all members must be laid out together. PUBLIC_P
1426 is nonzero if this union is not declared static. */
1429 finish_anon_union (tree anon_union_decl
)
1435 if (anon_union_decl
== error_mark_node
)
1438 type
= TREE_TYPE (anon_union_decl
);
1439 public_p
= TREE_PUBLIC (anon_union_decl
);
1441 /* The VAR_DECL's context is the same as the TYPE's context. */
1442 DECL_CONTEXT (anon_union_decl
) = DECL_CONTEXT (TYPE_NAME (type
));
1444 if (TYPE_FIELDS (type
) == NULL_TREE
)
1449 error ("namespace-scope anonymous aggregates must be static");
1453 main_decl
= build_anon_union_vars (type
, anon_union_decl
);
1454 if (main_decl
== error_mark_node
)
1456 if (main_decl
== NULL_TREE
)
1458 warning (0, "anonymous union with no members");
1462 if (!processing_template_decl
)
1464 /* Use main_decl to set the mangled name. */
1465 DECL_NAME (anon_union_decl
) = DECL_NAME (main_decl
);
1466 maybe_commonize_var (anon_union_decl
);
1467 if (TREE_STATIC (anon_union_decl
) || DECL_EXTERNAL (anon_union_decl
))
1468 mangle_decl (anon_union_decl
);
1469 DECL_NAME (anon_union_decl
) = NULL_TREE
;
1472 pushdecl (anon_union_decl
);
1473 cp_finish_decl (anon_union_decl
, NULL_TREE
, false, NULL_TREE
, 0);
1476 /* Auxiliary functions to make type signatures for
1477 `operator new' and `operator delete' correspond to
1478 what compiler will be expecting. */
1481 coerce_new_type (tree type
)
1484 tree args
= TYPE_ARG_TYPES (type
);
1486 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1488 if (!same_type_p (TREE_TYPE (type
), ptr_type_node
))
1491 error ("%<operator new%> must return type %qT", ptr_type_node
);
1494 if (args
&& args
!= void_list_node
)
1496 if (TREE_PURPOSE (args
))
1498 /* [basic.stc.dynamic.allocation]
1500 The first parameter shall not have an associated default
1502 error ("the first parameter of %<operator new%> cannot "
1503 "have a default argument");
1504 /* Throw away the default argument. */
1505 TREE_PURPOSE (args
) = NULL_TREE
;
1508 if (!same_type_p (TREE_VALUE (args
), size_type_node
))
1511 args
= TREE_CHAIN (args
);
1518 permerror (input_location
, "%<operator new%> takes type %<size_t%> (%qT) "
1519 "as first parameter", size_type_node
);
1524 args
= tree_cons (NULL_TREE
, size_type_node
, args
);
1527 type
= build_exception_variant
1528 (build_function_type (ptr_type_node
, args
),
1529 TYPE_RAISES_EXCEPTIONS (type
));
1537 coerce_delete_type (tree type
)
1540 tree args
= TYPE_ARG_TYPES (type
);
1542 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
1544 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
1547 error ("%<operator delete%> must return type %qT", void_type_node
);
1550 if (!args
|| args
== void_list_node
1551 || !same_type_p (TREE_VALUE (args
), ptr_type_node
))
1554 if (args
&& args
!= void_list_node
)
1555 args
= TREE_CHAIN (args
);
1556 error ("%<operator delete%> takes type %qT as first parameter",
1562 args
= tree_cons (NULL_TREE
, ptr_type_node
, args
);
1565 type
= build_exception_variant
1566 (build_function_type (void_type_node
, args
),
1567 TYPE_RAISES_EXCEPTIONS (type
));
1575 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1576 and mark them as needed. */
1579 mark_vtable_entries (tree decl
)
1582 unsigned HOST_WIDE_INT idx
;
1584 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl
)),
1589 STRIP_NOPS (fnaddr
);
1591 if (TREE_CODE (fnaddr
) != ADDR_EXPR
1592 && TREE_CODE (fnaddr
) != FDESC_EXPR
)
1593 /* This entry is an offset: a virtual base class offset, a
1594 virtual call offset, an RTTI offset, etc. */
1597 fn
= TREE_OPERAND (fnaddr
, 0);
1598 TREE_ADDRESSABLE (fn
) = 1;
1599 /* When we don't have vcall offsets, we output thunks whenever
1600 we output the vtables that contain them. With vcall offsets,
1601 we know all the thunks we'll need when we emit a virtual
1602 function, so we emit the thunks there instead. */
1603 if (DECL_THUNK_P (fn
))
1604 use_thunk (fn
, /*emit_p=*/0);
1609 /* Set DECL up to have the closest approximation of "initialized common"
1610 linkage available. */
1613 comdat_linkage (tree decl
)
1616 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1617 else if (TREE_CODE (decl
) == FUNCTION_DECL
1618 || (VAR_P (decl
) && DECL_ARTIFICIAL (decl
)))
1619 /* We can just emit function and compiler-generated variables
1620 statically; having multiple copies is (for the most part) only
1623 There are two correctness issues, however: the address of a
1624 template instantiation with external linkage should be the
1625 same, independent of what translation unit asks for the
1626 address, and this will not hold when we emit multiple copies of
1627 the function. However, there's little else we can do.
1629 Also, by default, the typeinfo implementation assumes that
1630 there will be only one copy of the string used as the name for
1631 each type. Therefore, if weak symbols are unavailable, the
1632 run-time library should perform a more conservative check; it
1633 should perform a string comparison, rather than an address
1635 TREE_PUBLIC (decl
) = 0;
1638 /* Static data member template instantiations, however, cannot
1639 have multiple copies. */
1640 if (DECL_INITIAL (decl
) == 0
1641 || DECL_INITIAL (decl
) == error_mark_node
)
1642 DECL_COMMON (decl
) = 1;
1643 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
1645 DECL_COMMON (decl
) = 1;
1646 DECL_INITIAL (decl
) = error_mark_node
;
1648 else if (!DECL_EXPLICIT_INSTANTIATION (decl
))
1650 /* We can't do anything useful; leave vars for explicit
1652 DECL_EXTERNAL (decl
) = 1;
1653 DECL_NOT_REALLY_EXTERN (decl
) = 0;
1657 DECL_COMDAT (decl
) = 1;
1660 /* For win32 we also want to put explicit instantiations in
1661 linkonce sections, so that they will be merged with implicit
1662 instantiations; otherwise we get duplicate symbol errors.
1663 For Darwin we do not want explicit instantiations to be
1667 maybe_make_one_only (tree decl
)
1669 /* We used to say that this was not necessary on targets that support weak
1670 symbols, because the implicit instantiations will defer to the explicit
1671 one. However, that's not actually the case in SVR4; a strong definition
1672 after a weak one is an error. Also, not making explicit
1673 instantiations one_only means that we can end up with two copies of
1674 some template instantiations. */
1678 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1679 we can get away with not emitting them if they aren't used. We need
1680 to for variables so that cp_finish_decl will update their linkage,
1681 because their DECL_INITIAL may not have been set properly yet. */
1683 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1684 || (! DECL_EXPLICIT_INSTANTIATION (decl
)
1685 && ! DECL_TEMPLATE_SPECIALIZATION (decl
)))
1687 make_decl_one_only (decl
, cxx_comdat_group (decl
));
1691 DECL_COMDAT (decl
) = 1;
1692 /* Mark it needed so we don't forget to emit it. */
1693 mark_decl_referenced (decl
);
1694 TREE_USED (decl
) = 1;
1699 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1700 This predicate will give the right answer during parsing of the
1701 function, which other tests may not. */
1704 vague_linkage_p (tree decl
)
1706 /* Unfortunately, import_export_decl has not always been called
1707 before the function is processed, so we cannot simply check
1709 return (DECL_COMDAT (decl
)
1710 || (((TREE_CODE (decl
) == FUNCTION_DECL
1711 && DECL_DECLARED_INLINE_P (decl
))
1712 || (DECL_LANG_SPECIFIC (decl
)
1713 && DECL_TEMPLATE_INSTANTIATION (decl
)))
1714 && TREE_PUBLIC (decl
)));
1717 /* Determine whether or not we want to specifically import or export CTYPE,
1718 using various heuristics. */
1721 import_export_class (tree ctype
)
1723 /* -1 for imported, 1 for exported. */
1724 int import_export
= 0;
1726 /* It only makes sense to call this function at EOF. The reason is
1727 that this function looks at whether or not the first non-inline
1728 non-abstract virtual member function has been defined in this
1729 translation unit. But, we can't possibly know that until we've
1730 seen the entire translation unit. */
1731 gcc_assert (at_eof
);
1733 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
1736 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1737 we will have CLASSTYPE_INTERFACE_ONLY set but not
1738 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1739 heuristic because someone will supply a #pragma implementation
1740 elsewhere, and deducing it here would produce a conflict. */
1741 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
1744 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
1746 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
1748 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
1749 && !flag_implicit_templates
)
1750 /* For a template class, without -fimplicit-templates, check the
1751 repository. If the virtual table is assigned to this
1752 translation unit, then export the class; otherwise, import
1754 import_export
= repo_export_class_p (ctype
) ? 1 : -1;
1755 else if (TYPE_POLYMORPHIC_P (ctype
))
1757 /* The ABI specifies that the virtual table and associated
1758 information are emitted with the key method, if any. */
1759 tree method
= CLASSTYPE_KEY_METHOD (ctype
);
1760 /* If weak symbol support is not available, then we must be
1761 careful not to emit the vtable when the key function is
1762 inline. An inline function can be defined in multiple
1763 translation units. If we were to emit the vtable in each
1764 translation unit containing a definition, we would get
1765 multiple definition errors at link-time. */
1766 if (method
&& (flag_weak
|| ! DECL_DECLARED_INLINE_P (method
)))
1767 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
1770 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1771 a definition anywhere else. */
1772 if (MULTIPLE_SYMBOL_SPACES
&& import_export
== -1)
1775 /* Allow back ends the chance to overrule the decision. */
1776 if (targetm
.cxx
.import_export_class
)
1777 import_export
= targetm
.cxx
.import_export_class (ctype
, import_export
);
1781 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
1782 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
1786 /* Return true if VAR has already been provided to the back end; in that
1787 case VAR should not be modified further by the front end. */
1789 var_finalized_p (tree var
)
1791 return varpool_node_for_decl (var
)->finalized
;
1794 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1795 must be emitted in this translation unit. Mark it as such. */
1798 mark_needed (tree decl
)
1800 TREE_USED (decl
) = 1;
1801 mark_decl_referenced (decl
);
1804 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1805 returns true if a definition of this entity should be provided in
1806 this object file. Callers use this function to determine whether
1807 or not to let the back end know that a definition of DECL is
1808 available in this translation unit. */
1811 decl_needed_p (tree decl
)
1813 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
1814 /* This function should only be called at the end of the translation
1815 unit. We cannot be sure of whether or not something will be
1816 COMDAT until that point. */
1817 gcc_assert (at_eof
);
1819 /* All entities with external linkage that are not COMDAT should be
1820 emitted; they may be referred to from other object files. */
1821 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
1823 /* If this entity was used, let the back end see it; it will decide
1824 whether or not to emit it into the object file. */
1825 if (TREE_USED (decl
))
1827 /* Functions marked "dllexport" must be emitted so that they are
1828 visible to other DLLs. */
1829 if (flag_keep_inline_dllexport
1830 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))
1832 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1833 reference to DECL might cause it to be emitted later. */
1837 /* If necessary, write out the vtables for the dynamic class CTYPE.
1838 Returns true if any vtables were emitted. */
1841 maybe_emit_vtables (tree ctype
)
1846 struct varpool_node
*current
= NULL
, *last
= NULL
;
1848 /* If the vtables for this class have already been emitted there is
1849 nothing more to do. */
1850 primary_vtbl
= CLASSTYPE_VTABLES (ctype
);
1851 if (var_finalized_p (primary_vtbl
))
1853 /* Ignore dummy vtables made by get_vtable_decl. */
1854 if (TREE_TYPE (primary_vtbl
) == void_type_node
)
1857 /* On some targets, we cannot determine the key method until the end
1858 of the translation unit -- which is when this function is
1860 if (!targetm
.cxx
.key_method_may_be_inline ())
1861 determine_key_method (ctype
);
1863 /* See if any of the vtables are needed. */
1864 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
1866 import_export_decl (vtbl
);
1867 if (DECL_NOT_REALLY_EXTERN (vtbl
) && decl_needed_p (vtbl
))
1872 /* If the references to this class' vtables are optimized away,
1873 still emit the appropriate debugging information. See
1875 if (DECL_COMDAT (primary_vtbl
)
1876 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
1877 note_debug_info_needed (ctype
);
1881 /* The ABI requires that we emit all of the vtables if we emit any
1883 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= DECL_CHAIN (vtbl
))
1885 /* Mark entities references from the virtual table as used. */
1886 mark_vtable_entries (vtbl
);
1888 if (TREE_TYPE (DECL_INITIAL (vtbl
)) == 0)
1890 vec
<tree
, va_gc
> *cleanups
= NULL
;
1891 tree expr
= store_init_value (vtbl
, DECL_INITIAL (vtbl
), &cleanups
,
1894 /* It had better be all done at compile-time. */
1895 gcc_assert (!expr
&& !cleanups
);
1899 DECL_EXTERNAL (vtbl
) = 0;
1900 rest_of_decl_compilation (vtbl
, 1, 1);
1902 /* Because we're only doing syntax-checking, we'll never end up
1903 actually marking the variable as written. */
1904 if (flag_syntax_only
)
1905 TREE_ASM_WRITTEN (vtbl
) = 1;
1906 else if (DECL_ONE_ONLY (vtbl
))
1908 current
= varpool_node_for_decl (vtbl
);
1910 symtab_add_to_same_comdat_group ((symtab_node
) current
, (symtab_node
) last
);
1915 /* Since we're writing out the vtable here, also write the debug
1917 note_debug_info_needed (ctype
);
1922 /* A special return value from type_visibility meaning internal
1925 enum { VISIBILITY_ANON
= VISIBILITY_INTERNAL
+1 };
1927 /* walk_tree helper function for type_visibility. */
1930 min_vis_r (tree
*tp
, int *walk_subtrees
, void *data
)
1932 int *vis_p
= (int *)data
;
1937 else if (OVERLOAD_TYPE_P (*tp
)
1938 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp
)))
1940 *vis_p
= VISIBILITY_ANON
;
1943 else if (CLASS_TYPE_P (*tp
)
1944 && CLASSTYPE_VISIBILITY (*tp
) > *vis_p
)
1945 *vis_p
= CLASSTYPE_VISIBILITY (*tp
);
1949 /* Returns the visibility of TYPE, which is the minimum visibility of its
1953 type_visibility (tree type
)
1955 int vis
= VISIBILITY_DEFAULT
;
1956 cp_walk_tree_without_duplicates (&type
, min_vis_r
, &vis
);
1960 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
1961 specified (or if VISIBILITY is static). If TMPL is true, this
1962 constraint is for a template argument, and takes precedence
1963 over explicitly-specified visibility on the template. */
1966 constrain_visibility (tree decl
, int visibility
, bool tmpl
)
1968 if (visibility
== VISIBILITY_ANON
)
1970 /* extern "C" declarations aren't affected by the anonymous
1972 if (!DECL_EXTERN_C_P (decl
))
1974 TREE_PUBLIC (decl
) = 0;
1975 DECL_WEAK (decl
) = 0;
1976 DECL_COMMON (decl
) = 0;
1977 DECL_COMDAT_GROUP (decl
) = NULL_TREE
;
1978 DECL_INTERFACE_KNOWN (decl
) = 1;
1979 if (DECL_LANG_SPECIFIC (decl
))
1980 DECL_NOT_REALLY_EXTERN (decl
) = 1;
1983 else if (visibility
> DECL_VISIBILITY (decl
)
1984 && (tmpl
|| !DECL_VISIBILITY_SPECIFIED (decl
)))
1986 DECL_VISIBILITY (decl
) = (enum symbol_visibility
) visibility
;
1987 /* This visibility was not specified. */
1988 DECL_VISIBILITY_SPECIFIED (decl
) = false;
1992 /* Constrain the visibility of DECL based on the visibility of its template
1996 constrain_visibility_for_template (tree decl
, tree targs
)
1998 /* If this is a template instantiation, check the innermost
1999 template args for visibility constraints. The outer template
2000 args are covered by the class check. */
2001 tree args
= INNERMOST_TEMPLATE_ARGS (targs
);
2003 for (i
= TREE_VEC_LENGTH (args
); i
> 0; --i
)
2007 tree arg
= TREE_VEC_ELT (args
, i
-1);
2009 vis
= type_visibility (arg
);
2010 else if (TREE_TYPE (arg
) && POINTER_TYPE_P (TREE_TYPE (arg
)))
2013 if (TREE_CODE (arg
) == ADDR_EXPR
)
2014 arg
= TREE_OPERAND (arg
, 0);
2015 if (VAR_OR_FUNCTION_DECL_P (arg
))
2017 if (! TREE_PUBLIC (arg
))
2018 vis
= VISIBILITY_ANON
;
2020 vis
= DECL_VISIBILITY (arg
);
2024 constrain_visibility (decl
, vis
, true);
2028 /* Like c_determine_visibility, but with additional C++-specific
2031 Function-scope entities can rely on the function's visibility because
2032 it is set in start_preparsed_function.
2034 Class-scope entities cannot rely on the class's visibility until the end
2035 of the enclosing class definition.
2037 Note that because namespaces have multiple independent definitions,
2038 namespace visibility is handled elsewhere using the #pragma visibility
2039 machinery rather than by decorating the namespace declaration.
2041 The goal is for constraints from the type to give a diagnostic, and
2042 other constraints to be applied silently. */
2045 determine_visibility (tree decl
)
2047 tree class_type
= NULL_TREE
;
2049 bool orig_visibility_specified
;
2050 enum symbol_visibility orig_visibility
;
2052 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2054 /* Only relevant for names with external linkage. */
2055 if (!TREE_PUBLIC (decl
))
2058 /* Cloned constructors and destructors get the same visibility as
2059 the underlying function. That should be set up in
2060 maybe_clone_body. */
2061 gcc_assert (!DECL_CLONED_FUNCTION_P (decl
));
2063 orig_visibility_specified
= DECL_VISIBILITY_SPECIFIED (decl
);
2064 orig_visibility
= DECL_VISIBILITY (decl
);
2066 if (TREE_CODE (decl
) == TYPE_DECL
)
2068 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
2069 use_template
= CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
));
2070 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
2075 else if (DECL_LANG_SPECIFIC (decl
))
2076 use_template
= DECL_USE_TEMPLATE (decl
);
2080 /* If DECL is a member of a class, visibility specifiers on the
2081 class can influence the visibility of the DECL. */
2082 if (DECL_CLASS_SCOPE_P (decl
))
2083 class_type
= DECL_CONTEXT (decl
);
2086 /* Not a class member. */
2088 /* Virtual tables have DECL_CONTEXT set to their associated class,
2089 so they are automatically handled above. */
2090 gcc_assert (!VAR_P (decl
)
2091 || !DECL_VTABLE_OR_VTT_P (decl
));
2093 if (DECL_FUNCTION_SCOPE_P (decl
) && ! DECL_VISIBILITY_SPECIFIED (decl
))
2095 /* Local statics and classes get the visibility of their
2096 containing function by default, except that
2097 -fvisibility-inlines-hidden doesn't affect them. */
2098 tree fn
= DECL_CONTEXT (decl
);
2099 if (DECL_VISIBILITY_SPECIFIED (fn
))
2101 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2102 DECL_VISIBILITY_SPECIFIED (decl
) =
2103 DECL_VISIBILITY_SPECIFIED (fn
);
2107 if (DECL_CLASS_SCOPE_P (fn
))
2108 determine_visibility_from_class (decl
, DECL_CONTEXT (fn
));
2109 else if (determine_hidden_inline (fn
))
2111 DECL_VISIBILITY (decl
) = default_visibility
;
2112 DECL_VISIBILITY_SPECIFIED (decl
) =
2113 visibility_options
.inpragma
;
2117 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (fn
);
2118 DECL_VISIBILITY_SPECIFIED (decl
) =
2119 DECL_VISIBILITY_SPECIFIED (fn
);
2123 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2124 but have no TEMPLATE_INFO, so don't try to check it. */
2127 else if (VAR_P (decl
) && DECL_TINFO_P (decl
)
2128 && flag_visibility_ms_compat
)
2130 /* Under -fvisibility-ms-compat, types are visible by default,
2131 even though their contents aren't. */
2132 tree underlying_type
= TREE_TYPE (DECL_NAME (decl
));
2133 int underlying_vis
= type_visibility (underlying_type
);
2134 if (underlying_vis
== VISIBILITY_ANON
2135 || (CLASS_TYPE_P (underlying_type
)
2136 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type
)))
2137 constrain_visibility (decl
, underlying_vis
, false);
2139 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
2141 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2143 /* tinfo visibility is based on the type it's for. */
2144 constrain_visibility
2145 (decl
, type_visibility (TREE_TYPE (DECL_NAME (decl
))), false);
2147 /* Give the target a chance to override the visibility associated
2149 if (TREE_PUBLIC (decl
)
2150 && !DECL_REALLY_EXTERN (decl
)
2151 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl
)))
2152 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl
))))
2153 targetm
.cxx
.determine_class_data_visibility (decl
);
2155 else if (use_template
)
2156 /* Template instantiations and specializations get visibility based
2157 on their template unless they override it with an attribute. */;
2158 else if (! DECL_VISIBILITY_SPECIFIED (decl
))
2160 if (determine_hidden_inline (decl
))
2161 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2164 /* Set default visibility to whatever the user supplied with
2165 #pragma GCC visibility or a namespace visibility attribute. */
2166 DECL_VISIBILITY (decl
) = default_visibility
;
2167 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
2174 /* If the specialization doesn't specify visibility, use the
2175 visibility from the template. */
2176 tree tinfo
= (TREE_CODE (decl
) == TYPE_DECL
2177 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2178 : DECL_TEMPLATE_INFO (decl
));
2179 tree args
= TI_ARGS (tinfo
);
2180 tree attribs
= (TREE_CODE (decl
) == TYPE_DECL
2181 ? TYPE_ATTRIBUTES (TREE_TYPE (decl
))
2182 : DECL_ATTRIBUTES (decl
));
2184 if (args
!= error_mark_node
)
2186 tree pattern
= DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo
));
2188 if (!DECL_VISIBILITY_SPECIFIED (decl
))
2190 if (!DECL_VISIBILITY_SPECIFIED (pattern
)
2191 && determine_hidden_inline (decl
))
2192 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2195 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (pattern
);
2196 DECL_VISIBILITY_SPECIFIED (decl
)
2197 = DECL_VISIBILITY_SPECIFIED (pattern
);
2202 /* Template argument visibility outweighs #pragma or namespace
2203 visibility, but not an explicit attribute. */
2204 && !lookup_attribute ("visibility", attribs
))
2206 int depth
= TMPL_ARGS_DEPTH (args
);
2207 int class_depth
= 0;
2208 if (class_type
&& CLASSTYPE_TEMPLATE_INFO (class_type
))
2209 class_depth
= TMPL_ARGS_DEPTH (CLASSTYPE_TI_ARGS (class_type
));
2210 if (DECL_VISIBILITY_SPECIFIED (decl
))
2212 /* A class template member with explicit visibility
2213 overrides the class visibility, so we need to apply
2214 all the levels of template args directly. */
2216 for (i
= 1; i
<= depth
; ++i
)
2218 tree lev
= TMPL_ARGS_LEVEL (args
, i
);
2219 constrain_visibility_for_template (decl
, lev
);
2222 else if (depth
> class_depth
)
2223 /* Limit visibility based on its template arguments. */
2224 constrain_visibility_for_template (decl
, args
);
2230 determine_visibility_from_class (decl
, class_type
);
2232 if (decl_anon_ns_mem_p (decl
))
2233 /* Names in an anonymous namespace get internal linkage.
2234 This might change once we implement export. */
2235 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2236 else if (TREE_CODE (decl
) != TYPE_DECL
)
2238 /* Propagate anonymity from type to decl. */
2239 int tvis
= type_visibility (TREE_TYPE (decl
));
2240 if (tvis
== VISIBILITY_ANON
2241 || ! DECL_VISIBILITY_SPECIFIED (decl
))
2242 constrain_visibility (decl
, tvis
, false);
2244 else if (no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/true))
2245 /* DR 757: A type without linkage shall not be used as the type of a
2246 variable or function with linkage, unless
2247 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2248 o the variable or function is not used (3.2 [basic.def.odr]) or is
2249 defined in the same translation unit.
2251 Since non-extern "C" decls need to be defined in the same
2252 translation unit, we can make the type internal. */
2253 constrain_visibility (decl
, VISIBILITY_ANON
, false);
2255 /* If visibility changed and DECL already has DECL_RTL, ensure
2256 symbol flags are updated. */
2257 if ((DECL_VISIBILITY (decl
) != orig_visibility
2258 || DECL_VISIBILITY_SPECIFIED (decl
) != orig_visibility_specified
)
2259 && ((VAR_P (decl
) && TREE_STATIC (decl
))
2260 || TREE_CODE (decl
) == FUNCTION_DECL
)
2261 && DECL_RTL_SET_P (decl
))
2262 make_decl_rtl (decl
);
2265 /* By default, static data members and function members receive
2266 the visibility of their containing class. */
2269 determine_visibility_from_class (tree decl
, tree class_type
)
2271 if (DECL_VISIBILITY_SPECIFIED (decl
))
2274 if (determine_hidden_inline (decl
))
2275 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
2278 /* Default to the class visibility. */
2279 DECL_VISIBILITY (decl
) = CLASSTYPE_VISIBILITY (class_type
);
2280 DECL_VISIBILITY_SPECIFIED (decl
)
2281 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type
);
2284 /* Give the target a chance to override the visibility associated
2287 && (DECL_TINFO_P (decl
)
2288 || (DECL_VTABLE_OR_VTT_P (decl
)
2289 /* Construction virtual tables are not exported because
2290 they cannot be referred to from other object files;
2291 their name is not standardized by the ABI. */
2292 && !DECL_CONSTRUCTION_VTABLE_P (decl
)))
2293 && TREE_PUBLIC (decl
)
2294 && !DECL_REALLY_EXTERN (decl
)
2295 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type
))
2296 targetm
.cxx
.determine_class_data_visibility (decl
);
2299 /* Returns true iff DECL is an inline that should get hidden visibility
2300 because of -fvisibility-inlines-hidden. */
2303 determine_hidden_inline (tree decl
)
2305 return (visibility_options
.inlines_hidden
2306 /* Don't do this for inline templates; specializations might not be
2307 inline, and we don't want them to inherit the hidden
2308 visibility. We'll set it here for all inline instantiations. */
2309 && !processing_template_decl
2310 && TREE_CODE (decl
) == FUNCTION_DECL
2311 && DECL_DECLARED_INLINE_P (decl
)
2312 && (! DECL_LANG_SPECIFIC (decl
)
2313 || ! DECL_EXPLICIT_INSTANTIATION (decl
)));
2316 /* Constrain the visibility of a class TYPE based on the visibility of its
2317 field types. Warn if any fields require lesser visibility. */
2320 constrain_class_visibility (tree type
)
2326 int vis
= type_visibility (type
);
2328 if (vis
== VISIBILITY_ANON
2329 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type
)))
2332 /* Don't warn about visibility if the class has explicit visibility. */
2333 if (CLASSTYPE_VISIBILITY_SPECIFIED (type
))
2334 vis
= VISIBILITY_INTERNAL
;
2336 for (t
= TYPE_FIELDS (type
); t
; t
= DECL_CHAIN (t
))
2337 if (TREE_CODE (t
) == FIELD_DECL
&& TREE_TYPE (t
) != error_mark_node
)
2339 tree ftype
= strip_pointer_or_array_types (TREE_TYPE (t
));
2340 int subvis
= type_visibility (ftype
);
2342 if (subvis
== VISIBILITY_ANON
)
2344 if (!in_main_input_context ())
2346 %qT has a field %qD whose type uses the anonymous namespace",
2349 else if (MAYBE_CLASS_TYPE_P (ftype
)
2350 && vis
< VISIBILITY_HIDDEN
2351 && subvis
>= VISIBILITY_HIDDEN
)
2352 warning (OPT_Wattributes
, "\
2353 %qT declared with greater visibility than the type of its field %qD",
2357 binfo
= TYPE_BINFO (type
);
2358 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, t
); ++i
)
2360 int subvis
= type_visibility (TREE_TYPE (t
));
2362 if (subvis
== VISIBILITY_ANON
)
2364 if (!in_main_input_context())
2366 %qT has a base %qT whose type uses the anonymous namespace",
2367 type
, TREE_TYPE (t
));
2369 else if (vis
< VISIBILITY_HIDDEN
2370 && subvis
>= VISIBILITY_HIDDEN
)
2371 warning (OPT_Wattributes
, "\
2372 %qT declared with greater visibility than its base %qT",
2373 type
, TREE_TYPE (t
));
2377 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2378 for DECL has not already been determined, do so now by setting
2379 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2380 function is called entities with vague linkage whose definitions
2381 are available must have TREE_PUBLIC set.
2383 If this function decides to place DECL in COMDAT, it will set
2384 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2385 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2386 callers defer that decision until it is clear that DECL is actually
2390 import_export_decl (tree decl
)
2395 tree class_type
= NULL_TREE
;
2397 if (DECL_INTERFACE_KNOWN (decl
))
2400 /* We cannot determine what linkage to give to an entity with vague
2401 linkage until the end of the file. For example, a virtual table
2402 for a class will be defined if and only if the key method is
2403 defined in this translation unit. As a further example, consider
2404 that when compiling a translation unit that uses PCH file with
2405 "-frepo" it would be incorrect to make decisions about what
2406 entities to emit when building the PCH; those decisions must be
2407 delayed until the repository information has been processed. */
2408 gcc_assert (at_eof
);
2409 /* Object file linkage for explicit instantiations is handled in
2410 mark_decl_instantiated. For static variables in functions with
2411 vague linkage, maybe_commonize_var is used.
2413 Therefore, the only declarations that should be provided to this
2414 function are those with external linkage that are:
2416 * implicit instantiations of function templates
2420 * implicit instantiations of static data members of class
2427 Furthermore, all entities that reach this point must have a
2428 definition available in this translation unit.
2430 The following assertions check these conditions. */
2431 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
2432 /* Any code that creates entities with TREE_PUBLIC cleared should
2433 also set DECL_INTERFACE_KNOWN. */
2434 gcc_assert (TREE_PUBLIC (decl
));
2435 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2436 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2437 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
)
2438 || DECL_DECLARED_INLINE_P (decl
));
2440 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl
)
2441 || DECL_VTABLE_OR_VTT_P (decl
)
2442 || DECL_TINFO_P (decl
));
2443 /* Check that a definition of DECL is available in this translation
2445 gcc_assert (!DECL_REALLY_EXTERN (decl
));
2447 /* Assume that DECL will not have COMDAT linkage. */
2449 /* Assume that DECL will not be imported into this translation
2453 /* See if the repository tells us whether or not to emit DECL in
2454 this translation unit. */
2455 emit_p
= repo_emit_p (decl
);
2458 else if (emit_p
== 1)
2460 /* The repository indicates that this entity should be defined
2461 here. Make sure the back end honors that request. */
2464 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl
)
2465 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl
))
2468 FOR_EACH_CLONE (clone
, decl
)
2469 mark_needed (clone
);
2473 /* Output the definition as an ordinary strong definition. */
2474 DECL_EXTERNAL (decl
) = 0;
2475 DECL_INTERFACE_KNOWN (decl
) = 1;
2480 /* We have already decided what to do with this DECL; there is no
2481 need to check anything further. */
2483 else if (VAR_P (decl
) && DECL_VTABLE_OR_VTT_P (decl
))
2485 class_type
= DECL_CONTEXT (decl
);
2486 import_export_class (class_type
);
2487 if (TYPE_FOR_JAVA (class_type
))
2489 else if (CLASSTYPE_INTERFACE_KNOWN (class_type
)
2490 && CLASSTYPE_INTERFACE_ONLY (class_type
))
2492 else if ((!flag_weak
|| TARGET_WEAK_NOT_IN_ARCHIVE_TOC
)
2493 && !CLASSTYPE_USE_TEMPLATE (class_type
)
2494 && CLASSTYPE_KEY_METHOD (class_type
)
2495 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
)))
2496 /* The ABI requires that all virtual tables be emitted with
2497 COMDAT linkage. However, on systems where COMDAT symbols
2498 don't show up in the table of contents for a static
2499 archive, or on systems without weak symbols (where we
2500 approximate COMDAT linkage by using internal linkage), the
2501 linker will report errors about undefined symbols because
2502 it will not see the virtual table definition. Therefore,
2503 in the case that we know that the virtual table will be
2504 emitted in only one translation unit, we make the virtual
2505 table an ordinary definition with external linkage. */
2506 DECL_EXTERNAL (decl
) = 0;
2507 else if (CLASSTYPE_INTERFACE_KNOWN (class_type
))
2509 /* CLASS_TYPE is being exported from this translation unit,
2510 so DECL should be defined here. */
2511 if (!flag_weak
&& CLASSTYPE_EXPLICIT_INSTANTIATION (class_type
))
2512 /* If a class is declared in a header with the "extern
2513 template" extension, then it will not be instantiated,
2514 even in translation units that would normally require
2515 it. Often such classes are explicitly instantiated in
2516 one translation unit. Therefore, the explicit
2517 instantiation must be made visible to other translation
2519 DECL_EXTERNAL (decl
) = 0;
2522 /* The generic C++ ABI says that class data is always
2523 COMDAT, even if there is a key function. Some
2524 variants (e.g., the ARM EABI) says that class data
2525 only has COMDAT linkage if the class data might be
2526 emitted in more than one translation unit. When the
2527 key method can be inline and is inline, we still have
2528 to arrange for comdat even though
2529 class_data_always_comdat is false. */
2530 if (!CLASSTYPE_KEY_METHOD (class_type
)
2531 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type
))
2532 || targetm
.cxx
.class_data_always_comdat ())
2534 /* The ABI requires COMDAT linkage. Normally, we
2535 only emit COMDAT things when they are needed;
2536 make sure that we realize that this entity is
2543 else if (!flag_implicit_templates
2544 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type
))
2549 else if (VAR_P (decl
) && DECL_TINFO_P (decl
))
2551 tree type
= TREE_TYPE (DECL_NAME (decl
));
2552 if (CLASS_TYPE_P (type
))
2555 import_export_class (type
);
2556 if (CLASSTYPE_INTERFACE_KNOWN (type
)
2557 && TYPE_POLYMORPHIC_P (type
)
2558 && CLASSTYPE_INTERFACE_ONLY (type
)
2559 /* If -fno-rtti was specified, then we cannot be sure
2560 that RTTI information will be emitted with the
2561 virtual table of the class, so we must emit it
2562 wherever it is used. */
2567 if (CLASSTYPE_INTERFACE_KNOWN (type
)
2568 && !CLASSTYPE_INTERFACE_ONLY (type
))
2570 comdat_p
= (targetm
.cxx
.class_data_always_comdat ()
2571 || (CLASSTYPE_KEY_METHOD (type
)
2572 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type
))));
2577 DECL_EXTERNAL (decl
) = 0;
2587 else if (DECL_TEMPLOID_INSTANTIATION (decl
))
2589 /* DECL is an implicit instantiation of a function or static
2591 if ((flag_implicit_templates
2592 && !flag_use_repository
)
2593 || (flag_implicit_inline_templates
2594 && TREE_CODE (decl
) == FUNCTION_DECL
2595 && DECL_DECLARED_INLINE_P (decl
)))
2598 /* If we are not implicitly generating templates, then mark
2599 this entity as undefined in this translation unit. */
2602 else if (DECL_FUNCTION_MEMBER_P (decl
))
2604 if (!DECL_DECLARED_INLINE_P (decl
))
2606 tree ctype
= DECL_CONTEXT (decl
);
2607 import_export_class (ctype
);
2608 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2610 DECL_NOT_REALLY_EXTERN (decl
)
2611 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
2612 || (DECL_DECLARED_INLINE_P (decl
)
2613 && ! flag_implement_inlines
2614 && !DECL_VINDEX (decl
)));
2616 if (!DECL_NOT_REALLY_EXTERN (decl
))
2617 DECL_EXTERNAL (decl
) = 1;
2619 /* Always make artificials weak. */
2620 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
2623 maybe_make_one_only (decl
);
2634 /* If we are importing DECL into this translation unit, mark is
2635 an undefined here. */
2636 DECL_EXTERNAL (decl
) = 1;
2637 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2641 /* If we decided to put DECL in COMDAT, mark it accordingly at
2643 comdat_linkage (decl
);
2646 DECL_INTERFACE_KNOWN (decl
) = 1;
2649 /* Return an expression that performs the destruction of DECL, which
2650 must be a VAR_DECL whose type has a non-trivial destructor, or is
2651 an array whose (innermost) elements have a non-trivial destructor. */
2654 build_cleanup (tree decl
)
2657 tree type
= TREE_TYPE (decl
);
2659 /* This function should only be called for declarations that really
2660 require cleanups. */
2661 gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type
));
2663 /* Treat all objects with destructors as used; the destructor may do
2664 something substantive. */
2667 if (TREE_CODE (type
) == ARRAY_TYPE
)
2670 temp
= build_address (decl
);
2671 temp
= build_delete (TREE_TYPE (temp
), temp
,
2672 sfk_complete_destructor
,
2673 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
, 0,
2674 tf_warning_or_error
);
2678 /* Returns the initialization guard variable for the variable DECL,
2679 which has static storage duration. */
2682 get_guard (tree decl
)
2687 sname
= mangle_guard_variable (decl
);
2688 guard
= IDENTIFIER_GLOBAL_VALUE (sname
);
2693 /* We use a type that is big enough to contain a mutex as well
2694 as an integer counter. */
2695 guard_type
= targetm
.cxx
.guard_type ();
2696 guard
= build_decl (DECL_SOURCE_LOCATION (decl
),
2697 VAR_DECL
, sname
, guard_type
);
2699 /* The guard should have the same linkage as what it guards. */
2700 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
2701 TREE_STATIC (guard
) = TREE_STATIC (decl
);
2702 DECL_COMMON (guard
) = DECL_COMMON (decl
);
2703 DECL_COMDAT (guard
) = DECL_COMDAT (decl
);
2704 DECL_TLS_MODEL (guard
) = DECL_TLS_MODEL (decl
);
2705 if (DECL_ONE_ONLY (decl
))
2706 make_decl_one_only (guard
, cxx_comdat_group (guard
));
2707 if (TREE_PUBLIC (decl
))
2708 DECL_WEAK (guard
) = DECL_WEAK (decl
);
2709 DECL_VISIBILITY (guard
) = DECL_VISIBILITY (decl
);
2710 DECL_VISIBILITY_SPECIFIED (guard
) = DECL_VISIBILITY_SPECIFIED (decl
);
2712 DECL_ARTIFICIAL (guard
) = 1;
2713 DECL_IGNORED_P (guard
) = 1;
2714 TREE_USED (guard
) = 1;
2715 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
2720 /* Return those bits of the GUARD variable that should be set when the
2721 guarded entity is actually initialized. */
2724 get_guard_bits (tree guard
)
2726 if (!targetm
.cxx
.guard_mask_bit ())
2728 /* We only set the first byte of the guard, in order to leave room
2729 for a mutex in the high-order bits. */
2730 guard
= build1 (ADDR_EXPR
,
2731 build_pointer_type (TREE_TYPE (guard
)),
2733 guard
= build1 (NOP_EXPR
,
2734 build_pointer_type (char_type_node
),
2736 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
2742 /* Return an expression which determines whether or not the GUARD
2743 variable has already been initialized. */
2746 get_guard_cond (tree guard
)
2750 /* Check to see if the GUARD is zero. */
2751 guard
= get_guard_bits (guard
);
2753 /* Mask off all but the low bit. */
2754 if (targetm
.cxx
.guard_mask_bit ())
2756 guard_value
= integer_one_node
;
2757 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
2758 guard_value
= convert (TREE_TYPE (guard
), guard_value
);
2759 guard
= cp_build_binary_op (input_location
,
2760 BIT_AND_EXPR
, guard
, guard_value
,
2761 tf_warning_or_error
);
2764 guard_value
= integer_zero_node
;
2765 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
2766 guard_value
= convert (TREE_TYPE (guard
), guard_value
);
2767 return cp_build_binary_op (input_location
,
2768 EQ_EXPR
, guard
, guard_value
,
2769 tf_warning_or_error
);
2772 /* Return an expression which sets the GUARD variable, indicating that
2773 the variable being guarded has been initialized. */
2776 set_guard (tree guard
)
2780 /* Set the GUARD to one. */
2781 guard
= get_guard_bits (guard
);
2782 guard_init
= integer_one_node
;
2783 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
2784 guard_init
= convert (TREE_TYPE (guard
), guard_init
);
2785 return cp_build_modify_expr (guard
, NOP_EXPR
, guard_init
,
2786 tf_warning_or_error
);
2789 /* Returns true iff we can tell that VAR does not have a dynamic
2793 var_defined_without_dynamic_init (tree var
)
2795 /* If it's defined in another TU, we can't tell. */
2796 if (DECL_EXTERNAL (var
))
2798 /* If it has a non-trivial destructor, registering the destructor
2799 counts as dynamic initialization. */
2800 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var
)))
2802 /* If it's in this TU, its initializer has been processed. */
2803 gcc_assert (DECL_INITIALIZED_P (var
));
2804 /* If it has no initializer or a constant one, it's not dynamic. */
2805 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var
)
2806 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var
));
2809 /* Returns true iff VAR is a variable that needs uses to be
2810 wrapped for possible dynamic initialization. */
2813 var_needs_tls_wrapper (tree var
)
2815 return (!error_operand_p (var
)
2816 && DECL_THREAD_LOCAL_P (var
)
2817 && !DECL_GNU_TLS_P (var
)
2818 && !DECL_FUNCTION_SCOPE_P (var
)
2819 && !var_defined_without_dynamic_init (var
));
2822 /* Get the FUNCTION_DECL for the shared TLS init function for this
2823 translation unit. */
2826 get_local_tls_init_fn (void)
2828 tree sname
= get_identifier ("__tls_init");
2829 tree fn
= IDENTIFIER_GLOBAL_VALUE (sname
);
2832 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
2833 build_function_type (void_type_node
,
2835 SET_DECL_LANGUAGE (fn
, lang_c
);
2836 TREE_PUBLIC (fn
) = false;
2837 DECL_ARTIFICIAL (fn
) = true;
2839 SET_IDENTIFIER_GLOBAL_VALUE (sname
, fn
);
2844 /* Get a FUNCTION_DECL for the init function for the thread_local
2845 variable VAR. The init function will be an alias to the function
2846 that initializes all the non-local TLS variables in the translation
2847 unit. The init function is only used by the wrapper function. */
2850 get_tls_init_fn (tree var
)
2852 /* Only C++11 TLS vars need this init fn. */
2853 if (!var_needs_tls_wrapper (var
))
2856 /* If -fno-extern-tls-init, assume that we don't need to call
2857 a tls init function for a variable defined in another TU. */
2858 if (!flag_extern_tls_init
&& DECL_EXTERNAL (var
))
2861 #ifdef ASM_OUTPUT_DEF
2862 /* If the variable is internal, or if we can't generate aliases,
2863 call the local init function directly. */
2864 if (!TREE_PUBLIC (var
))
2866 return get_local_tls_init_fn ();
2868 tree sname
= mangle_tls_init_fn (var
);
2869 tree fn
= IDENTIFIER_GLOBAL_VALUE (sname
);
2872 fn
= build_lang_decl (FUNCTION_DECL
, sname
,
2873 build_function_type (void_type_node
,
2875 SET_DECL_LANGUAGE (fn
, lang_c
);
2876 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
2877 DECL_ARTIFICIAL (fn
) = true;
2878 DECL_COMDAT (fn
) = DECL_COMDAT (var
);
2879 DECL_EXTERNAL (fn
) = true;
2880 if (DECL_ONE_ONLY (var
))
2881 make_decl_one_only (fn
, cxx_comdat_group (fn
));
2882 if (TREE_PUBLIC (var
))
2884 tree obtype
= strip_array_types (non_reference (TREE_TYPE (var
)));
2885 /* If the variable is defined somewhere else and might have static
2886 initialization, make the init function a weak reference. */
2887 if ((!TYPE_NEEDS_CONSTRUCTING (obtype
)
2888 || TYPE_HAS_CONSTEXPR_CTOR (obtype
))
2889 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype
)
2890 && DECL_EXTERNAL (var
))
2893 DECL_WEAK (fn
) = DECL_WEAK (var
);
2895 DECL_VISIBILITY (fn
) = DECL_VISIBILITY (var
);
2896 DECL_VISIBILITY_SPECIFIED (fn
) = DECL_VISIBILITY_SPECIFIED (var
);
2897 DECL_DLLIMPORT_P (fn
) = DECL_DLLIMPORT_P (var
);
2898 DECL_IGNORED_P (fn
) = 1;
2901 DECL_BEFRIENDING_CLASSES (fn
) = var
;
2903 SET_IDENTIFIER_GLOBAL_VALUE (sname
, fn
);
2908 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
2909 variable VAR. The wrapper function calls the init function (if any) for
2910 VAR and then returns a reference to VAR. The wrapper function is used
2911 in place of VAR everywhere VAR is mentioned. */
2914 get_tls_wrapper_fn (tree var
)
2916 /* Only C++11 TLS vars need this wrapper fn. */
2917 if (!var_needs_tls_wrapper (var
))
2920 tree sname
= mangle_tls_wrapper_fn (var
);
2921 tree fn
= IDENTIFIER_GLOBAL_VALUE (sname
);
2924 /* A named rvalue reference is an lvalue, so the wrapper should
2925 always return an lvalue reference. */
2926 tree type
= non_reference (TREE_TYPE (var
));
2927 type
= build_reference_type (type
);
2928 tree fntype
= build_function_type (type
, void_list_node
);
2929 fn
= build_lang_decl (FUNCTION_DECL
, sname
, fntype
);
2930 SET_DECL_LANGUAGE (fn
, lang_c
);
2931 TREE_PUBLIC (fn
) = TREE_PUBLIC (var
);
2932 DECL_ARTIFICIAL (fn
) = true;
2933 DECL_IGNORED_P (fn
) = 1;
2934 /* The wrapper is inline and emitted everywhere var is used. */
2935 DECL_DECLARED_INLINE_P (fn
) = true;
2936 if (TREE_PUBLIC (var
))
2938 comdat_linkage (fn
);
2939 #ifdef HAVE_GAS_HIDDEN
2940 /* Make the wrapper bind locally; there's no reason to share
2941 the wrapper between multiple shared objects. */
2942 DECL_VISIBILITY (fn
) = VISIBILITY_INTERNAL
;
2943 DECL_VISIBILITY_SPECIFIED (fn
) = true;
2946 if (!TREE_PUBLIC (fn
))
2947 DECL_INTERFACE_KNOWN (fn
) = true;
2949 note_vague_linkage_fn (fn
);
2952 /* We want CSE to commonize calls to the wrapper, but marking it as
2953 pure is unsafe since it has side-effects. I guess we need a new
2954 ECF flag even weaker than ECF_PURE. FIXME! */
2955 DECL_PURE_P (fn
) = true;
2958 DECL_BEFRIENDING_CLASSES (fn
) = var
;
2960 SET_IDENTIFIER_GLOBAL_VALUE (sname
, fn
);
2965 /* At EOF, generate the definition for the TLS wrapper function FN:
2968 if (init_fn) init_fn();
2973 generate_tls_wrapper (tree fn
)
2975 tree var
= DECL_BEFRIENDING_CLASSES (fn
);
2977 start_preparsed_function (fn
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
2978 tree body
= begin_function_body ();
2979 /* Only call the init fn if there might be one. */
2980 if (tree init_fn
= get_tls_init_fn (var
))
2982 tree if_stmt
= NULL_TREE
;
2983 /* If init_fn is a weakref, make sure it exists before calling. */
2984 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn
)))
2986 if_stmt
= begin_if_stmt ();
2987 tree addr
= cp_build_addr_expr (init_fn
, tf_warning_or_error
);
2988 tree cond
= cp_build_binary_op (DECL_SOURCE_LOCATION (var
),
2989 NE_EXPR
, addr
, nullptr_node
,
2990 tf_warning_or_error
);
2991 finish_if_stmt_cond (cond
, if_stmt
);
2993 finish_expr_stmt (build_cxx_call
2994 (init_fn
, 0, NULL
, tf_warning_or_error
));
2997 finish_then_clause (if_stmt
);
2998 finish_if_stmt (if_stmt
);
3002 /* If there's no initialization, the wrapper is a constant function. */
3003 TREE_READONLY (fn
) = true;
3004 finish_return_stmt (convert_from_reference (var
));
3005 finish_function_body (body
);
3006 expand_or_defer_fn (finish_function (0));
3009 /* Start the process of running a particular set of global constructors
3010 or destructors. Subroutine of do_[cd]tors. */
3013 start_objects (int method_type
, int initp
)
3019 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3021 if (initp
!= DEFAULT_INIT_PRIORITY
)
3031 sprintf (type
, "sub_%c%c%.5u", method_type
, joiner
, initp
);
3034 sprintf (type
, "sub_%c", method_type
);
3036 fndecl
= build_lang_decl (FUNCTION_DECL
,
3037 get_file_function_name (type
),
3038 build_function_type_list (void_type_node
,
3040 start_preparsed_function (fndecl
, /*attrs=*/NULL_TREE
, SF_PRE_PARSED
);
3042 TREE_PUBLIC (current_function_decl
) = 0;
3044 /* Mark as artificial because it's not explicitly in the user's
3046 DECL_ARTIFICIAL (current_function_decl
) = 1;
3048 /* Mark this declaration as used to avoid spurious warnings. */
3049 TREE_USED (current_function_decl
) = 1;
3051 /* Mark this function as a global constructor or destructor. */
3052 if (method_type
== 'I')
3053 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
3055 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
3057 body
= begin_compound_stmt (BCS_FN_BODY
);
3062 /* Finish the process of running a particular set of global constructors
3063 or destructors. Subroutine of do_[cd]tors. */
3066 finish_objects (int method_type
, int initp
, tree body
)
3071 finish_compound_stmt (body
);
3072 fn
= finish_function (0);
3074 if (method_type
== 'I')
3076 DECL_STATIC_CONSTRUCTOR (fn
) = 1;
3077 decl_init_priority_insert (fn
, initp
);
3081 DECL_STATIC_DESTRUCTOR (fn
) = 1;
3082 decl_fini_priority_insert (fn
, initp
);
3085 expand_or_defer_fn (fn
);
3088 /* The names of the parameters to the function created to handle
3089 initializations and destructions for objects with static storage
3091 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3092 #define PRIORITY_IDENTIFIER "__priority"
3094 /* The name of the function we create to handle initializations and
3095 destructions for objects with static storage duration. */
3096 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3098 /* The declaration for the __INITIALIZE_P argument. */
3099 static GTY(()) tree initialize_p_decl
;
3101 /* The declaration for the __PRIORITY argument. */
3102 static GTY(()) tree priority_decl
;
3104 /* The declaration for the static storage duration function. */
3105 static GTY(()) tree ssdf_decl
;
3107 /* All the static storage duration functions created in this
3108 translation unit. */
3109 static GTY(()) vec
<tree
, va_gc
> *ssdf_decls
;
3111 /* A map from priority levels to information about that priority
3112 level. There may be many such levels, so efficient lookup is
3114 static splay_tree priority_info_map
;
3116 /* Begins the generation of the function that will handle all
3117 initialization and destruction of objects with static storage
3118 duration. The function generated takes two parameters of type
3119 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3120 nonzero, it performs initializations. Otherwise, it performs
3121 destructions. It only performs those initializations or
3122 destructions with the indicated __PRIORITY. The generated function
3125 It is assumed that this function will only be called once per
3126 translation unit. */
3129 start_static_storage_duration_function (unsigned count
)
3133 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
3135 /* Create the identifier for this function. It will be of the form
3136 SSDF_IDENTIFIER_<number>. */
3137 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, count
);
3139 type
= build_function_type_list (void_type_node
,
3140 integer_type_node
, integer_type_node
,
3143 /* Create the FUNCTION_DECL itself. */
3144 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
3145 get_identifier (id
),
3147 TREE_PUBLIC (ssdf_decl
) = 0;
3148 DECL_ARTIFICIAL (ssdf_decl
) = 1;
3150 /* Put this function in the list of functions to be called from the
3151 static constructors and destructors. */
3154 vec_alloc (ssdf_decls
, 32);
3156 /* Take this opportunity to initialize the map from priority
3157 numbers to information about that priority level. */
3158 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
3159 /*delete_key_fn=*/0,
3160 /*delete_value_fn=*/
3161 (splay_tree_delete_value_fn
) &free
);
3163 /* We always need to generate functions for the
3164 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3165 priorities later, we'll be sure to find the
3166 DEFAULT_INIT_PRIORITY. */
3167 get_priority_info (DEFAULT_INIT_PRIORITY
);
3170 vec_safe_push (ssdf_decls
, ssdf_decl
);
3172 /* Create the argument list. */
3173 initialize_p_decl
= cp_build_parm_decl
3174 (get_identifier (INITIALIZE_P_IDENTIFIER
), integer_type_node
);
3175 DECL_CONTEXT (initialize_p_decl
) = ssdf_decl
;
3176 TREE_USED (initialize_p_decl
) = 1;
3177 priority_decl
= cp_build_parm_decl
3178 (get_identifier (PRIORITY_IDENTIFIER
), integer_type_node
);
3179 DECL_CONTEXT (priority_decl
) = ssdf_decl
;
3180 TREE_USED (priority_decl
) = 1;
3182 DECL_CHAIN (initialize_p_decl
) = priority_decl
;
3183 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
3185 /* Put the function in the global scope. */
3186 pushdecl (ssdf_decl
);
3188 /* Start the function itself. This is equivalent to declaring the
3191 static void __ssdf (int __initialize_p, init __priority_p);
3193 It is static because we only need to call this function from the
3194 various constructor and destructor functions for this module. */
3195 start_preparsed_function (ssdf_decl
,
3196 /*attrs=*/NULL_TREE
,
3199 /* Set up the scope of the outermost block in the function. */
3200 body
= begin_compound_stmt (BCS_FN_BODY
);
3205 /* Finish the generation of the function which performs initialization
3206 and destruction of objects with static storage duration. After
3207 this point, no more such objects can be created. */
3210 finish_static_storage_duration_function (tree body
)
3212 /* Close out the function. */
3213 finish_compound_stmt (body
);
3214 expand_or_defer_fn (finish_function (0));
3217 /* Return the information about the indicated PRIORITY level. If no
3218 code to handle this level has yet been generated, generate the
3219 appropriate prologue. */
3221 static priority_info
3222 get_priority_info (int priority
)
3227 n
= splay_tree_lookup (priority_info_map
,
3228 (splay_tree_key
) priority
);
3231 /* Create a new priority information structure, and insert it
3233 pi
= XNEW (struct priority_info_s
);
3234 pi
->initializations_p
= 0;
3235 pi
->destructions_p
= 0;
3236 splay_tree_insert (priority_info_map
,
3237 (splay_tree_key
) priority
,
3238 (splay_tree_value
) pi
);
3241 pi
= (priority_info
) n
->value
;
3246 /* The effective initialization priority of a DECL. */
3248 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3249 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3250 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3252 /* Whether a DECL needs a guard to protect it against multiple
3255 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3256 || DECL_ONE_ONLY (decl) \
3257 || DECL_WEAK (decl)))
3259 /* Called from one_static_initialization_or_destruction(),
3261 Walks the initializer list of a global variable and looks for
3262 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3263 and that have their DECL_CONTEXT() == NULL.
3264 For each such temporary variable, set their DECL_CONTEXT() to
3265 the current function. This is necessary because otherwise
3266 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3267 when trying to refer to a temporary variable that does not have
3268 it's DECL_CONTECT() properly set. */
3270 fix_temporary_vars_context_r (tree
*node
,
3274 gcc_assert (current_function_decl
);
3276 if (TREE_CODE (*node
) == BIND_EXPR
)
3280 for (var
= BIND_EXPR_VARS (*node
); var
; var
= DECL_CHAIN (var
))
3283 && DECL_ARTIFICIAL (var
)
3284 && !DECL_CONTEXT (var
))
3285 DECL_CONTEXT (var
) = current_function_decl
;
3291 /* Set up to handle the initialization or destruction of DECL. If
3292 INITP is nonzero, we are initializing the variable. Otherwise, we
3293 are destroying it. */
3296 one_static_initialization_or_destruction (tree decl
, tree init
, bool initp
)
3298 tree guard_if_stmt
= NULL_TREE
;
3301 /* If we are supposed to destruct and there's a trivial destructor,
3302 nothing has to be done. */
3304 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3307 /* Trick the compiler into thinking we are at the file and line
3308 where DECL was declared so that error-messages make sense, and so
3309 that the debugger will show somewhat sensible file and line
3311 input_location
= DECL_SOURCE_LOCATION (decl
);
3313 /* Make sure temporary variables in the initialiser all have
3314 their DECL_CONTEXT() set to a value different from NULL_TREE.
3315 This can happen when global variables initialisers are built.
3316 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3317 the temporary variables that might have been generated in the
3318 accompagning initialisers is NULL_TREE, meaning the variables have been
3319 declared in the global namespace.
3320 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3321 of the temporaries are set to the current function decl. */
3322 cp_walk_tree_without_duplicates (&init
,
3323 fix_temporary_vars_context_r
,
3330 Access control for implicit calls to the constructors,
3331 the conversion functions, or the destructor called to
3332 create and destroy a static data member is performed as
3333 if these calls appeared in the scope of the member's
3336 we pretend we are in a static member function of the class of
3337 which the DECL is a member. */
3338 if (member_p (decl
))
3340 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
3341 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
3344 /* Assume we don't need a guard. */
3346 /* We need a guard if this is an object with external linkage that
3347 might be initialized in more than one place. (For example, a
3348 static data member of a template, when the data member requires
3350 if (NEEDS_GUARD_P (decl
))
3354 guard
= get_guard (decl
);
3356 /* When using __cxa_atexit, we just check the GUARD as we would
3357 for a local static. */
3358 if (flag_use_cxa_atexit
)
3360 /* When using __cxa_atexit, we never try to destroy
3361 anything from a static destructor. */
3363 guard_cond
= get_guard_cond (guard
);
3365 /* If we don't have __cxa_atexit, then we will be running
3366 destructors from .fini sections, or their equivalents. So,
3367 we need to know how many times we've tried to initialize this
3368 object. We do initializations only if the GUARD is zero,
3369 i.e., if we are the first to initialize the variable. We do
3370 destructions only if the GUARD is one, i.e., if we are the
3371 last to destroy the variable. */
3374 = cp_build_binary_op (input_location
,
3376 cp_build_unary_op (PREINCREMENT_EXPR
,
3379 tf_warning_or_error
),
3381 tf_warning_or_error
);
3384 = cp_build_binary_op (input_location
,
3386 cp_build_unary_op (PREDECREMENT_EXPR
,
3389 tf_warning_or_error
),
3391 tf_warning_or_error
);
3393 guard_if_stmt
= begin_if_stmt ();
3394 finish_if_stmt_cond (guard_cond
, guard_if_stmt
);
3398 /* If we're using __cxa_atexit, we have not already set the GUARD,
3399 so we must do so now. */
3400 if (guard
&& initp
&& flag_use_cxa_atexit
)
3401 finish_expr_stmt (set_guard (guard
));
3403 /* Perform the initialization or destruction. */
3407 finish_expr_stmt (init
);
3409 /* If we're using __cxa_atexit, register a function that calls the
3410 destructor for the object. */
3411 if (flag_use_cxa_atexit
)
3412 finish_expr_stmt (register_dtor_fn (decl
));
3415 finish_expr_stmt (build_cleanup (decl
));
3417 /* Finish the guard if-stmt, if necessary. */
3420 finish_then_clause (guard_if_stmt
);
3421 finish_if_stmt (guard_if_stmt
);
3424 /* Now that we're done with DECL we don't need to pretend to be a
3425 member of its class any longer. */
3426 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
3427 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
3430 /* Generate code to do the initialization or destruction of the decls in VARS,
3431 a TREE_LIST of VAR_DECL with static storage duration.
3432 Whether initialization or destruction is performed is specified by INITP. */
3435 do_static_initialization_or_destruction (tree vars
, bool initp
)
3437 tree node
, init_if_stmt
, cond
;
3439 /* Build the outer if-stmt to check for initialization or destruction. */
3440 init_if_stmt
= begin_if_stmt ();
3441 cond
= initp
? integer_one_node
: integer_zero_node
;
3442 cond
= cp_build_binary_op (input_location
,
3446 tf_warning_or_error
);
3447 finish_if_stmt_cond (cond
, init_if_stmt
);
3451 tree decl
= TREE_VALUE (node
);
3452 tree priority_if_stmt
;
3456 /* If we don't need a destructor, there's nothing to do. Avoid
3457 creating a possibly empty if-stmt. */
3458 if (!initp
&& TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3460 node
= TREE_CHAIN (node
);
3464 /* Remember that we had an initialization or finalization at this
3466 priority
= DECL_EFFECTIVE_INIT_PRIORITY (decl
);
3467 pi
= get_priority_info (priority
);
3469 pi
->initializations_p
= 1;
3471 pi
->destructions_p
= 1;
3473 /* Conditionalize this initialization on being in the right priority
3474 and being initializing/finalizing appropriately. */
3475 priority_if_stmt
= begin_if_stmt ();
3476 cond
= cp_build_binary_op (input_location
,
3479 build_int_cst (NULL_TREE
, priority
),
3480 tf_warning_or_error
);
3481 finish_if_stmt_cond (cond
, priority_if_stmt
);
3483 /* Process initializers with same priority. */
3485 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node
)) == priority
;
3486 node
= TREE_CHAIN (node
))
3487 /* Do one initialization or destruction. */
3488 one_static_initialization_or_destruction (TREE_VALUE (node
),
3489 TREE_PURPOSE (node
), initp
);
3491 /* Finish up the priority if-stmt body. */
3492 finish_then_clause (priority_if_stmt
);
3493 finish_if_stmt (priority_if_stmt
);
3497 /* Finish up the init/destruct if-stmt body. */
3498 finish_then_clause (init_if_stmt
);
3499 finish_if_stmt (init_if_stmt
);
3502 /* VARS is a list of variables with static storage duration which may
3503 need initialization and/or finalization. Remove those variables
3504 that don't really need to be initialized or finalized, and return
3505 the resulting list. The order in which the variables appear in
3506 VARS is in reverse order of the order in which they should actually
3507 be initialized. The list we return is in the unreversed order;
3508 i.e., the first variable should be initialized first. */
3511 prune_vars_needing_no_initialization (tree
*vars
)
3514 tree result
= NULL_TREE
;
3519 tree decl
= TREE_VALUE (t
);
3520 tree init
= TREE_PURPOSE (t
);
3522 /* Deal gracefully with error. */
3523 if (decl
== error_mark_node
)
3525 var
= &TREE_CHAIN (t
);
3529 /* The only things that can be initialized are variables. */
3530 gcc_assert (VAR_P (decl
));
3532 /* If this object is not defined, we don't need to do anything
3534 if (DECL_EXTERNAL (decl
))
3536 var
= &TREE_CHAIN (t
);
3540 /* Also, if the initializer already contains errors, we can bail
3542 if (init
&& TREE_CODE (init
) == TREE_LIST
3543 && value_member (error_mark_node
, init
))
3545 var
= &TREE_CHAIN (t
);
3549 /* This variable is going to need initialization and/or
3550 finalization, so we add it to the list. */
3551 *var
= TREE_CHAIN (t
);
3552 TREE_CHAIN (t
) = result
;
3559 /* Make sure we have told the back end about all the variables in
3563 write_out_vars (tree vars
)
3567 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3569 tree var
= TREE_VALUE (v
);
3570 if (!var_finalized_p (var
))
3572 import_export_decl (var
);
3573 rest_of_decl_compilation (var
, 1, 1);
3578 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3579 (otherwise) that will initialize all global objects with static
3580 storage duration having the indicated PRIORITY. */
3583 generate_ctor_or_dtor_function (bool constructor_p
, int priority
,
3591 input_location
= *locus
;
3593 /* Was: locus->line++; */
3595 /* We use `I' to indicate initialization and `D' to indicate
3597 function_key
= constructor_p
? 'I' : 'D';
3599 /* We emit the function lazily, to avoid generating empty
3600 global constructors and destructors. */
3603 /* For Objective-C++, we may need to initialize metadata found in this module.
3604 This must be done _before_ any other static initializations. */
3605 if (c_dialect_objc () && (priority
== DEFAULT_INIT_PRIORITY
)
3606 && constructor_p
&& objc_static_init_needed_p ())
3608 body
= start_objects (function_key
, priority
);
3609 objc_generate_static_init_call (NULL_TREE
);
3612 /* Call the static storage duration function with appropriate
3614 FOR_EACH_VEC_SAFE_ELT (ssdf_decls
, i
, fndecl
)
3616 /* Calls to pure or const functions will expand to nothing. */
3617 if (! (flags_from_decl_or_type (fndecl
) & (ECF_CONST
| ECF_PURE
)))
3622 body
= start_objects (function_key
, priority
);
3624 call
= cp_build_function_call_nary (fndecl
, tf_warning_or_error
,
3625 build_int_cst (NULL_TREE
,
3627 build_int_cst (NULL_TREE
,
3630 finish_expr_stmt (call
);
3634 /* Close out the function. */
3636 finish_objects (function_key
, priority
, body
);
3639 /* Generate constructor and destructor functions for the priority
3643 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n
, void * data
)
3645 location_t
*locus
= (location_t
*) data
;
3646 int priority
= (int) n
->key
;
3647 priority_info pi
= (priority_info
) n
->value
;
3649 /* Generate the functions themselves, but only if they are really
3651 if (pi
->initializations_p
)
3652 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority
, locus
);
3653 if (pi
->destructions_p
)
3654 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority
, locus
);
3656 /* Keep iterating. */
3660 /* Java requires that we be able to reference a local address for a
3661 method, and not be confused by PLT entries. If hidden aliases are
3662 supported, collect and return all the functions for which we should
3663 emit a hidden alias. */
3665 static struct pointer_set_t
*
3666 collect_candidates_for_java_method_aliases (void)
3668 struct cgraph_node
*node
;
3669 struct pointer_set_t
*candidates
= NULL
;
3671 #ifndef HAVE_GAS_HIDDEN
3675 FOR_EACH_FUNCTION (node
)
3677 tree fndecl
= node
->symbol
.decl
;
3679 if (DECL_CLASS_SCOPE_P (fndecl
)
3680 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl
))
3681 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl
))
3683 if (candidates
== NULL
)
3684 candidates
= pointer_set_create ();
3685 pointer_set_insert (candidates
, fndecl
);
3693 /* Java requires that we be able to reference a local address for a
3694 method, and not be confused by PLT entries. If hidden aliases are
3695 supported, emit one for each java function that we've emitted.
3696 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3697 by collect_candidates_for_java_method_aliases. */
3700 build_java_method_aliases (struct pointer_set_t
*candidates
)
3702 struct cgraph_node
*node
;
3704 #ifndef HAVE_GAS_HIDDEN
3708 FOR_EACH_FUNCTION (node
)
3710 tree fndecl
= node
->symbol
.decl
;
3712 if (TREE_ASM_WRITTEN (fndecl
)
3713 && pointer_set_contains (candidates
, fndecl
))
3715 /* Mangle the name in a predictable way; we need to reference
3716 this from a java compiled object file. */
3717 tree oid
, nid
, alias
;
3721 oid
= DECL_ASSEMBLER_NAME (fndecl
);
3722 oname
= IDENTIFIER_POINTER (oid
);
3723 gcc_assert (oname
[0] == '_' && oname
[1] == 'Z');
3724 nname
= ACONCAT (("_ZGA", oname
+2, NULL
));
3725 nid
= get_identifier (nname
);
3727 alias
= make_alias_for (fndecl
, nid
);
3728 TREE_PUBLIC (alias
) = 1;
3729 DECL_VISIBILITY (alias
) = VISIBILITY_HIDDEN
;
3731 assemble_alias (alias
, oid
);
3736 /* Return C++ property of T, based on given operation OP. */
3739 cpp_check (tree t
, cpp_operation op
)
3744 return DECL_PURE_VIRTUAL_P (t
);
3745 case IS_CONSTRUCTOR
:
3746 return DECL_CONSTRUCTOR_P (t
);
3748 return DECL_DESTRUCTOR_P (t
);
3749 case IS_COPY_CONSTRUCTOR
:
3750 return DECL_COPY_CONSTRUCTOR_P (t
);
3752 return TREE_CODE (t
) == TEMPLATE_DECL
;
3758 /* Collect source file references recursively, starting from NAMESPC. */
3761 collect_source_refs (tree namespc
)
3768 /* Iterate over names in this name space. */
3769 for (t
= NAMESPACE_LEVEL (namespc
)->names
; t
; t
= TREE_CHAIN (t
))
3770 if (!DECL_IS_BUILTIN (t
) )
3771 collect_source_ref (DECL_SOURCE_FILE (t
));
3773 /* Dump siblings, if any */
3774 collect_source_refs (TREE_CHAIN (namespc
));
3776 /* Dump children, if any */
3777 collect_source_refs (NAMESPACE_LEVEL (namespc
)->namespaces
);
3780 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
3781 starting from NAMESPC. */
3784 collect_ada_namespace (tree namespc
, const char *source_file
)
3789 /* Collect decls from this namespace */
3790 collect_ada_nodes (NAMESPACE_LEVEL (namespc
)->names
, source_file
);
3792 /* Collect siblings, if any */
3793 collect_ada_namespace (TREE_CHAIN (namespc
), source_file
);
3795 /* Collect children, if any */
3796 collect_ada_namespace (NAMESPACE_LEVEL (namespc
)->namespaces
, source_file
);
3799 /* Returns true iff there is a definition available for variable or
3803 decl_defined_p (tree decl
)
3805 if (TREE_CODE (decl
) == FUNCTION_DECL
)
3806 return (DECL_INITIAL (decl
) != NULL_TREE
);
3809 gcc_assert (VAR_P (decl
));
3810 return !DECL_EXTERNAL (decl
);
3814 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
3818 An integral constant-expression can only involve ... const
3819 variables of integral or enumeration types initialized with
3820 constant expressions ...
3822 C++0x also allows constexpr variables and temporaries initialized
3823 with constant expressions. We handle the former here, but the latter
3824 are just folded away in cxx_eval_constant_expression.
3826 The standard does not require that the expression be non-volatile.
3827 G++ implements the proposed correction in DR 457. */
3830 decl_constant_var_p (tree decl
)
3832 if (!decl_maybe_constant_var_p (decl
))
3835 /* We don't know if a template static data member is initialized with
3836 a constant expression until we instantiate its initializer. Even
3837 in the case of a constexpr variable, we can't treat it as a
3838 constant until its initializer is complete in case it's used in
3839 its own initializer. */
3841 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
);
3844 /* Returns true if DECL could be a symbolic constant variable, depending on
3848 decl_maybe_constant_var_p (tree decl
)
3850 tree type
= TREE_TYPE (decl
);
3853 if (DECL_DECLARED_CONSTEXPR_P (decl
))
3855 return (CP_TYPE_CONST_NON_VOLATILE_P (type
)
3856 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
3859 /* Complain that DECL uses a type with no linkage but is never defined. */
3862 no_linkage_error (tree decl
)
3864 tree t
= no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false);
3865 if (TYPE_ANONYMOUS_P (t
))
3867 permerror (0, "%q+#D, declared using anonymous type, "
3868 "is used but never defined", decl
);
3869 if (is_typedef_decl (TYPE_NAME (t
)))
3870 permerror (0, "%q+#D does not refer to the unqualified type, "
3871 "so it is not used for linkage", TYPE_NAME (t
));
3874 permerror (0, "%q+#D, declared using local type %qT, "
3875 "is used but never defined", decl
, t
);
3878 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
3881 collect_all_refs (const char *source_file
)
3883 collect_ada_namespace (global_namespace
, source_file
);
3886 /* Clear DECL_EXTERNAL for NODE. */
3889 clear_decl_external (struct cgraph_node
*node
, void * /*data*/)
3891 DECL_EXTERNAL (node
->symbol
.decl
) = 0;
3895 /* Build up the function to run dynamic initializers for thread_local
3896 variables in this translation unit and alias the init functions for the
3897 individual variables to it. */
3900 handle_tls_init (void)
3902 tree vars
= prune_vars_needing_no_initialization (&tls_aggregates
);
3903 if (vars
== NULL_TREE
)
3906 location_t loc
= DECL_SOURCE_LOCATION (TREE_VALUE (vars
));
3908 write_out_vars (vars
);
3910 tree guard
= build_decl (loc
, VAR_DECL
, get_identifier ("__tls_guard"),
3912 TREE_PUBLIC (guard
) = false;
3913 TREE_STATIC (guard
) = true;
3914 DECL_ARTIFICIAL (guard
) = true;
3915 DECL_IGNORED_P (guard
) = true;
3916 TREE_USED (guard
) = true;
3917 DECL_TLS_MODEL (guard
) = decl_default_tls_model (guard
);
3918 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
3920 tree fn
= get_local_tls_init_fn ();
3921 start_preparsed_function (fn
, NULL_TREE
, SF_PRE_PARSED
);
3922 tree body
= begin_function_body ();
3923 tree if_stmt
= begin_if_stmt ();
3924 tree cond
= cp_build_unary_op (TRUTH_NOT_EXPR
, guard
, false,
3925 tf_warning_or_error
);
3926 finish_if_stmt_cond (cond
, if_stmt
);
3927 finish_expr_stmt (cp_build_modify_expr (guard
, NOP_EXPR
, boolean_true_node
,
3928 tf_warning_or_error
));
3929 for (; vars
; vars
= TREE_CHAIN (vars
))
3931 tree var
= TREE_VALUE (vars
);
3932 tree init
= TREE_PURPOSE (vars
);
3933 one_static_initialization_or_destruction (var
, init
, true);
3935 #ifdef ASM_OUTPUT_DEF
3936 /* Output init aliases even with -fno-extern-tls-init. */
3937 if (TREE_PUBLIC (var
))
3939 tree single_init_fn
= get_tls_init_fn (var
);
3941 = cgraph_same_body_alias (cgraph_get_create_node (fn
),
3942 single_init_fn
, fn
);
3943 gcc_assert (alias
!= NULL
);
3948 finish_then_clause (if_stmt
);
3949 finish_if_stmt (if_stmt
);
3950 finish_function_body (body
);
3951 expand_or_defer_fn (finish_function (0));
3954 /* This routine is called at the end of compilation.
3955 Its job is to create all the code needed to initialize and
3956 destroy the global aggregates. We do the destruction
3957 first, since that way we only need to reverse the decls once. */
3960 cp_write_global_declarations (void)
3966 unsigned ssdf_count
= 0;
3969 struct pointer_set_t
*candidates
;
3971 locus
= input_location
;
3974 /* Bad parse errors. Just forget about it. */
3975 if (! global_bindings_p () || current_class_type
3976 || !vec_safe_is_empty (decl_namespace_list
))
3979 /* This is the point to write out a PCH if we're doing that.
3980 In that case we do not want to do anything else. */
3983 c_common_write_pch ();
3987 cgraph_process_same_body_aliases ();
3989 /* Handle -fdump-ada-spec[-slim] */
3990 if (flag_dump_ada_spec
|| flag_dump_ada_spec_slim
)
3992 if (flag_dump_ada_spec_slim
)
3993 collect_source_ref (main_input_filename
);
3995 collect_source_refs (global_namespace
);
3997 dump_ada_specs (collect_all_refs
, cpp_check
);
4000 /* FIXME - huh? was input_line -= 1;*/
4002 timevar_start (TV_PHASE_DEFERRED
);
4004 /* We now have to write out all the stuff we put off writing out.
4007 o Template specializations that we have not yet instantiated,
4008 but which are needed.
4009 o Initialization and destruction for non-local objects with
4010 static storage duration. (Local objects with static storage
4011 duration are initialized when their scope is first entered,
4012 and are cleaned up via atexit.)
4013 o Virtual function tables.
4015 All of these may cause others to be needed. For example,
4016 instantiating one function may cause another to be needed, and
4017 generating the initializer for an object may cause templates to be
4018 instantiated, etc., etc. */
4020 emit_support_tinfos ();
4029 /* If there are templates that we've put off instantiating, do
4031 instantiate_pending_templates (retries
);
4034 /* Write out virtual tables as required. Note that writing out
4035 the virtual table for a template class may cause the
4036 instantiation of members of that class. If we write out
4037 vtables then we remove the class from our list so we don't
4038 have to look at it again. */
4040 while (keyed_classes
!= NULL_TREE
4041 && maybe_emit_vtables (TREE_VALUE (keyed_classes
)))
4044 keyed_classes
= TREE_CHAIN (keyed_classes
);
4050 tree next
= TREE_CHAIN (t
);
4054 if (maybe_emit_vtables (TREE_VALUE (next
)))
4057 TREE_CHAIN (t
) = TREE_CHAIN (next
);
4062 next
= TREE_CHAIN (t
);
4066 /* Write out needed type info variables. We have to be careful
4067 looping through unemitted decls, because emit_tinfo_decl may
4068 cause other variables to be needed. New elements will be
4069 appended, and we remove from the vector those that actually
4071 for (i
= unemitted_tinfo_decls
->length ();
4072 unemitted_tinfo_decls
->iterate (--i
, &t
);)
4073 if (emit_tinfo_decl (t
))
4076 unemitted_tinfo_decls
->unordered_remove (i
);
4079 /* The list of objects with static storage duration is built up
4080 in reverse order. We clear STATIC_AGGREGATES so that any new
4081 aggregates added during the initialization of these will be
4082 initialized in the correct order when we next come around the
4084 vars
= prune_vars_needing_no_initialization (&static_aggregates
);
4088 /* We need to start a new initialization function each time
4089 through the loop. That's because we need to know which
4090 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4091 isn't computed until a function is finished, and written
4092 out. That's a deficiency in the back end. When this is
4093 fixed, these initialization functions could all become
4094 inline, with resulting performance improvements. */
4097 /* Set the line and file, so that it is obviously not from
4099 input_location
= locus
;
4100 ssdf_body
= start_static_storage_duration_function (ssdf_count
);
4102 /* Make sure the back end knows about all the variables. */
4103 write_out_vars (vars
);
4105 /* First generate code to do all the initializations. */
4107 do_static_initialization_or_destruction (vars
, /*initp=*/true);
4109 /* Then, generate code to do all the destructions. Do these
4110 in reverse order so that the most recently constructed
4111 variable is the first destroyed. If we're using
4112 __cxa_atexit, then we don't need to do this; functions
4113 were registered at initialization time to destroy the
4115 if (!flag_use_cxa_atexit
&& vars
)
4117 vars
= nreverse (vars
);
4118 do_static_initialization_or_destruction (vars
, /*initp=*/false);
4123 /* Finish up the static storage duration function for this
4125 input_location
= locus
;
4126 finish_static_storage_duration_function (ssdf_body
);
4128 /* All those initializations and finalizations might cause
4129 us to need more inline functions, more template
4130 instantiations, etc. */
4133 /* ??? was: locus.line++; */
4136 /* Now do the same for thread_local variables. */
4139 /* Go through the set of inline functions whose bodies have not
4140 been emitted yet. If out-of-line copies of these functions
4141 are required, emit them. */
4142 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4144 /* Does it need synthesizing? */
4145 if (DECL_DEFAULTED_FN (decl
) && ! DECL_INITIAL (decl
)
4146 && (! DECL_REALLY_EXTERN (decl
) || possibly_inlined_p (decl
)))
4148 /* Even though we're already at the top-level, we push
4149 there again. That way, when we pop back a few lines
4150 hence, all of our state is restored. Otherwise,
4151 finish_function doesn't clean things up, and we end
4152 up with CURRENT_FUNCTION_DECL set. */
4153 push_to_top_level ();
4154 /* The decl's location will mark where it was first
4155 needed. Save that so synthesize method can indicate
4156 where it was needed from, in case of error */
4157 input_location
= DECL_SOURCE_LOCATION (decl
);
4158 synthesize_method (decl
);
4159 pop_from_top_level ();
4163 if (!DECL_INITIAL (decl
) && decl_tls_wrapper_p (decl
))
4164 generate_tls_wrapper (decl
);
4166 if (!DECL_SAVED_TREE (decl
))
4169 /* We lie to the back end, pretending that some functions
4170 are not defined when they really are. This keeps these
4171 functions from being put out unnecessarily. But, we must
4172 stop lying when the functions are referenced, or if they
4173 are not comdat since they need to be put out now. If
4174 DECL_INTERFACE_KNOWN, then we have already set
4175 DECL_EXTERNAL appropriately, so there's no need to check
4176 again, and we do not want to clear DECL_EXTERNAL if a
4177 previous call to import_export_decl set it.
4179 This is done in a separate for cycle, because if some
4180 deferred function is contained in another deferred
4181 function later in deferred_fns varray,
4182 rest_of_compilation would skip this function and we
4183 really cannot expand the same function twice. */
4184 import_export_decl (decl
);
4185 if (DECL_NOT_REALLY_EXTERN (decl
)
4186 && DECL_INITIAL (decl
)
4187 && decl_needed_p (decl
))
4189 struct cgraph_node
*node
, *next
;
4191 node
= cgraph_get_node (decl
);
4192 if (node
->same_body_alias
)
4193 node
= cgraph_alias_aliased_node (node
);
4195 cgraph_for_node_and_aliases (node
, clear_decl_external
,
4197 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4198 group, we need to mark all symbols in the same comdat group
4200 if (node
->symbol
.same_comdat_group
)
4201 for (next
= cgraph (node
->symbol
.same_comdat_group
);
4203 next
= cgraph (next
->symbol
.same_comdat_group
))
4204 cgraph_for_node_and_aliases (next
, clear_decl_external
,
4208 /* If we're going to need to write this function out, and
4209 there's already a body for it, create RTL for it now.
4210 (There might be no body if this is a method we haven't
4211 gotten around to synthesizing yet.) */
4212 if (!DECL_EXTERNAL (decl
)
4213 && decl_needed_p (decl
)
4214 && !TREE_ASM_WRITTEN (decl
)
4215 && !cgraph_get_node (decl
)->local
.finalized
)
4217 /* We will output the function; no longer consider it in this
4219 DECL_DEFER_OUTPUT (decl
) = 0;
4220 /* Generate RTL for this function now that we know we
4222 expand_or_defer_fn (decl
);
4223 /* If we're compiling -fsyntax-only pretend that this
4224 function has been written out so that we don't try to
4226 if (flag_syntax_only
)
4227 TREE_ASM_WRITTEN (decl
) = 1;
4232 if (walk_namespaces (wrapup_globals_for_namespace
, /*data=*/0))
4235 /* Static data members are just like namespace-scope globals. */
4236 FOR_EACH_VEC_SAFE_ELT (pending_statics
, i
, decl
)
4238 if (var_finalized_p (decl
) || DECL_REALLY_EXTERN (decl
)
4239 /* Don't write it out if we haven't seen a definition. */
4240 || DECL_IN_AGGR_P (decl
))
4242 import_export_decl (decl
);
4243 /* If this static data member is needed, provide it to the
4245 if (DECL_NOT_REALLY_EXTERN (decl
) && decl_needed_p (decl
))
4246 DECL_EXTERNAL (decl
) = 0;
4248 if (vec_safe_length (pending_statics
) != 0
4249 && wrapup_global_declarations (pending_statics
->address (),
4250 pending_statics
->length ()))
4257 /* All used inline functions must have a definition at this point. */
4258 FOR_EACH_VEC_SAFE_ELT (deferred_fns
, i
, decl
)
4260 if (/* Check online inline functions that were actually used. */
4261 DECL_ODR_USED (decl
) && DECL_DECLARED_INLINE_P (decl
)
4262 /* If the definition actually was available here, then the
4263 fact that the function was not defined merely represents
4264 that for some reason (use of a template repository,
4265 #pragma interface, etc.) we decided not to emit the
4267 && !DECL_INITIAL (decl
)
4268 /* Don't complain if the template was defined. */
4269 && !(DECL_TEMPLATE_INSTANTIATION (decl
)
4270 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4271 (template_for_substitution (decl
)))))
4273 warning (0, "inline function %q+D used but never defined", decl
);
4274 /* Avoid a duplicate warning from check_global_declaration_1. */
4275 TREE_NO_WARNING (decl
) = 1;
4279 /* So must decls that use a type with no linkage. */
4280 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls
, i
, decl
)
4281 if (!decl_defined_p (decl
))
4282 no_linkage_error (decl
);
4284 /* Then, do the Objective-C stuff. This is where all the
4285 Objective-C module stuff gets generated (symtab,
4286 class/protocol/selector lists etc). This must be done after C++
4287 templates, destructors etc. so that selectors used in C++
4288 templates are properly allocated. */
4289 if (c_dialect_objc ())
4290 objc_write_global_declarations ();
4292 /* We give C linkage to static constructors and destructors. */
4293 push_lang_context (lang_name_c
);
4295 /* Generate initialization and destruction functions for all
4296 priorities for which they are required. */
4297 if (priority_info_map
)
4298 splay_tree_foreach (priority_info_map
,
4299 generate_ctor_and_dtor_functions_for_priority
,
4301 else if (c_dialect_objc () && objc_static_init_needed_p ())
4302 /* If this is obj-c++ and we need a static init, call
4303 generate_ctor_or_dtor_function. */
4304 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4305 DEFAULT_INIT_PRIORITY
, &locus
);
4307 /* We're done with the splay-tree now. */
4308 if (priority_info_map
)
4309 splay_tree_delete (priority_info_map
);
4311 /* Generate any missing aliases. */
4312 maybe_apply_pending_pragma_weaks ();
4314 /* We're done with static constructors, so we can go back to "C++"
4316 pop_lang_context ();
4318 /* Collect candidates for Java hidden aliases. */
4319 candidates
= collect_candidates_for_java_method_aliases ();
4321 timevar_stop (TV_PHASE_DEFERRED
);
4322 timevar_start (TV_PHASE_OPT_GEN
);
4324 finalize_compilation_unit ();
4326 timevar_stop (TV_PHASE_OPT_GEN
);
4327 timevar_start (TV_PHASE_CHECK_DBGINFO
);
4329 /* Now, issue warnings about static, but not defined, functions,
4330 etc., and emit debugging information. */
4331 walk_namespaces (wrapup_globals_for_namespace
, /*data=*/&reconsider
);
4332 if (vec_safe_length (pending_statics
) != 0)
4334 check_global_declarations (pending_statics
->address (),
4335 pending_statics
->length ());
4336 emit_debug_global_declarations (pending_statics
->address (),
4337 pending_statics
->length ());
4340 perform_deferred_noexcept_checks ();
4342 /* Generate hidden aliases for Java. */
4345 build_java_method_aliases (candidates
);
4346 pointer_set_destroy (candidates
);
4351 /* The entire file is now complete. If requested, dump everything
4355 FILE *stream
= dump_begin (TDI_tu
, &flags
);
4359 dump_node (global_namespace
, flags
& ~TDF_SLIM
, stream
);
4360 dump_end (TDI_tu
, stream
);
4364 if (flag_detailed_statistics
)
4366 dump_tree_statistics ();
4367 dump_time_statistics ();
4369 input_location
= locus
;
4371 #ifdef ENABLE_CHECKING
4372 validate_conversion_obstack ();
4373 #endif /* ENABLE_CHECKING */
4375 timevar_stop (TV_PHASE_CHECK_DBGINFO
);
4378 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4379 function to call in parse-tree form; it has not yet been
4380 semantically analyzed. ARGS are the arguments to the function.
4381 They have already been semantically analyzed. This may change
4385 build_offset_ref_call_from_tree (tree fn
, vec
<tree
, va_gc
> **args
,
4386 tsubst_flags_t complain
)
4389 vec
<tree
, va_gc
> *orig_args
= NULL
;
4394 object
= TREE_OPERAND (fn
, 0);
4396 if (processing_template_decl
)
4398 gcc_assert (TREE_CODE (fn
) == DOTSTAR_EXPR
4399 || TREE_CODE (fn
) == MEMBER_REF
);
4400 if (type_dependent_expression_p (fn
)
4401 || any_type_dependent_arguments_p (*args
))
4402 return build_nt_call_vec (fn
, *args
);
4404 orig_args
= make_tree_vector_copy (*args
);
4406 /* Transform the arguments and add the implicit "this"
4407 parameter. That must be done before the FN is transformed
4408 because we depend on the form of FN. */
4409 make_args_non_dependent (*args
);
4410 object
= build_non_dependent_expr (object
);
4411 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4413 if (TREE_CODE (fn
) == DOTSTAR_EXPR
)
4414 object
= cp_build_addr_expr (object
, complain
);
4415 vec_safe_insert (*args
, 0, object
);
4417 /* Now that the arguments are done, transform FN. */
4418 fn
= build_non_dependent_expr (fn
);
4421 /* A qualified name corresponding to a bound pointer-to-member is
4422 represented as an OFFSET_REF:
4424 struct B { void g(); };
4426 void B::g() { (this->*p)(); } */
4427 if (TREE_CODE (fn
) == OFFSET_REF
)
4429 tree object_addr
= cp_build_addr_expr (object
, complain
);
4430 fn
= TREE_OPERAND (fn
, 1);
4431 fn
= get_member_function_from_ptrfunc (&object_addr
, fn
,
4433 vec_safe_insert (*args
, 0, object_addr
);
4436 if (CLASS_TYPE_P (TREE_TYPE (fn
)))
4437 expr
= build_op_call (fn
, args
, complain
);
4439 expr
= cp_build_function_call_vec (fn
, args
, complain
);
4440 if (processing_template_decl
&& expr
!= error_mark_node
)
4441 expr
= build_min_non_dep_call_vec (expr
, orig_fn
, orig_args
);
4443 if (orig_args
!= NULL
)
4444 release_tree_vector (orig_args
);
4451 check_default_args (tree x
)
4453 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
4454 bool saw_def
= false;
4455 int i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
4456 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
4458 if (TREE_PURPOSE (arg
))
4462 error ("default argument missing for parameter %P of %q+#D", i
, x
);
4463 TREE_PURPOSE (arg
) = error_mark_node
;
4468 /* Return true if function DECL can be inlined. This is used to force
4469 instantiation of methods that might be interesting for inlining. */
4471 possibly_inlined_p (tree decl
)
4473 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
4474 if (DECL_UNINLINABLE (decl
))
4476 if (!optimize
|| pragma_java_exceptions
)
4477 return DECL_DECLARED_INLINE_P (decl
);
4478 /* When optimizing, we might inline everything when flatten
4479 attribute or heuristics inlining for size or autoinlining
4484 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4485 If DECL is a specialization or implicitly declared class member,
4486 generate the actual definition. Return false if something goes
4487 wrong, true otherwise. */
4490 mark_used (tree decl
)
4492 /* If DECL is a BASELINK for a single function, then treat it just
4493 like the DECL for the function. Otherwise, if the BASELINK is
4494 for an overloaded function, we don't know which function was
4495 actually used until after overload resolution. */
4496 if (BASELINK_P (decl
))
4498 decl
= BASELINK_FUNCTIONS (decl
);
4499 if (really_overloaded_fn (decl
))
4501 decl
= OVL_CURRENT (decl
);
4504 /* Set TREE_USED for the benefit of -Wunused. */
4505 TREE_USED (decl
) = 1;
4506 if (DECL_CLONED_FUNCTION_P (decl
))
4507 TREE_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
4509 /* Mark enumeration types as used. */
4510 if (TREE_CODE (decl
) == CONST_DECL
)
4511 used_types_insert (DECL_CONTEXT (decl
));
4513 if (TREE_CODE (decl
) == FUNCTION_DECL
4514 && DECL_DELETED_FN (decl
))
4516 if (DECL_ARTIFICIAL (decl
))
4518 if (DECL_OVERLOADED_OPERATOR_P (decl
) == TYPE_EXPR
4519 && LAMBDA_TYPE_P (DECL_CONTEXT (decl
)))
4521 /* We mark a lambda conversion op as deleted if we can't
4522 generate it properly; see maybe_add_lambda_conv_op. */
4523 sorry ("converting lambda which uses %<...%> to "
4524 "function pointer");
4528 error ("use of deleted function %qD", decl
);
4529 if (!maybe_explain_implicit_delete (decl
))
4530 error_at (DECL_SOURCE_LOCATION (decl
), "declared here");
4534 /* We can only check DECL_ODR_USED on variables or functions with
4535 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4536 might need special handling for. */
4537 if (!VAR_OR_FUNCTION_DECL_P (decl
)
4538 || DECL_LANG_SPECIFIC (decl
) == NULL
4539 || DECL_THUNK_P (decl
))
4541 if (!processing_template_decl
&& type_uses_auto (TREE_TYPE (decl
)))
4543 error ("use of %qD before deduction of %<auto%>", decl
);
4549 /* We only want to do this processing once. We don't need to keep trying
4550 to instantiate inline templates, because unit-at-a-time will make sure
4551 we get them compiled before functions that want to inline them. */
4552 if (DECL_ODR_USED (decl
))
4555 /* If within finish_function, defer the rest until that function
4556 finishes, otherwise it might recurse. */
4557 if (defer_mark_used_calls
)
4559 vec_safe_push (deferred_mark_used_calls
, decl
);
4563 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4564 maybe_instantiate_noexcept (decl
);
4566 /* Normally, we can wait until instantiation-time to synthesize DECL.
4567 However, if DECL is a static data member initialized with a constant
4568 or a constexpr function, we need it right now because a reference to
4569 such a data member or a call to such function is not value-dependent.
4570 For a function that uses auto in the return type, we need to instantiate
4571 it to find out its type. */
4572 if ((decl_maybe_constant_var_p (decl
)
4573 || (TREE_CODE (decl
) == FUNCTION_DECL
4574 && DECL_DECLARED_CONSTEXPR_P (decl
))
4575 || type_uses_auto (TREE_TYPE (decl
)))
4576 && DECL_LANG_SPECIFIC (decl
)
4577 && DECL_TEMPLATE_INFO (decl
)
4578 && !uses_template_parms (DECL_TI_ARGS (decl
)))
4580 /* Instantiating a function will result in garbage collection. We
4581 must treat this situation as if we were within the body of a
4582 function so as to avoid collecting live data only referenced from
4583 the stack (such as overload resolution candidates). */
4585 instantiate_decl (decl
, /*defer_ok=*/false,
4586 /*expl_inst_class_mem_p=*/false);
4590 if (processing_template_decl
)
4593 /* Check this too in case we're within fold_non_dependent_expr. */
4594 if (DECL_TEMPLATE_INFO (decl
)
4595 && uses_template_parms (DECL_TI_ARGS (decl
)))
4598 if (type_uses_auto (TREE_TYPE (decl
)))
4600 error ("use of %qD before deduction of %<auto%>", decl
);
4604 /* If we don't need a value, then we don't need to synthesize DECL. */
4605 if (cp_unevaluated_operand
!= 0)
4608 DECL_ODR_USED (decl
) = 1;
4609 if (DECL_CLONED_FUNCTION_P (decl
))
4610 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl
)) = 1;
4612 /* DR 757: A type without linkage shall not be used as the type of a
4613 variable or function with linkage, unless
4614 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4615 o the variable or function is not used (3.2 [basic.def.odr]) or is
4616 defined in the same translation unit. */
4617 if (cxx_dialect
> cxx98
4618 && decl_linkage (decl
) != lk_none
4619 && !DECL_EXTERN_C_P (decl
)
4620 && !DECL_ARTIFICIAL (decl
)
4621 && !decl_defined_p (decl
)
4622 && no_linkage_check (TREE_TYPE (decl
), /*relaxed_p=*/false))
4624 if (is_local_extern (decl
))
4625 /* There's no way to define a local extern, and adding it to
4626 the vector interferes with GC, so give an error now. */
4627 no_linkage_error (decl
);
4629 vec_safe_push (no_linkage_decls
, decl
);
4632 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (decl
)
4633 && !DECL_INITIAL (decl
) && !DECL_ARTIFICIAL (decl
))
4634 /* Remember it, so we can check it was defined. */
4635 note_vague_linkage_fn (decl
);
4637 /* Is it a synthesized method that needs to be synthesized? */
4638 if (TREE_CODE (decl
) == FUNCTION_DECL
4639 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
4640 && DECL_DEFAULTED_FN (decl
)
4641 /* A function defaulted outside the class is synthesized either by
4642 cp_finish_decl or instantiate_decl. */
4643 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl
)
4644 && ! DECL_INITIAL (decl
))
4646 /* Defer virtual destructors so that thunks get the right
4648 if (DECL_VIRTUAL_P (decl
) && !at_eof
)
4650 note_vague_linkage_fn (decl
);
4654 /* Remember the current location for a function we will end up
4655 synthesizing. Then we can inform the user where it was
4656 required in the case of error. */
4657 DECL_SOURCE_LOCATION (decl
) = input_location
;
4659 /* Synthesizing an implicitly defined member function will result in
4660 garbage collection. We must treat this situation as if we were
4661 within the body of a function so as to avoid collecting live data
4662 on the stack (such as overload resolution candidates).
4664 We could just let cp_write_global_declarations handle synthesizing
4665 this function by adding it to deferred_fns, but doing
4666 it at the use site produces better error messages. */
4668 synthesize_method (decl
);
4670 /* If this is a synthesized method we don't need to
4671 do the instantiation test below. */
4673 else if (VAR_OR_FUNCTION_DECL_P (decl
)
4674 && DECL_TEMPLATE_INFO (decl
)
4675 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
4676 || always_instantiate_p (decl
)))
4677 /* If this is a function or variable that is an instance of some
4678 template, we now know that we will need to actually do the
4679 instantiation. We check that DECL is not an explicit
4680 instantiation because that is not checked in instantiate_decl.
4682 We put off instantiating functions in order to improve compile
4683 times. Maintaining a stack of active functions is expensive,
4684 and the inliner knows to instantiate any functions it might
4685 need. Therefore, we always try to defer instantiation. */
4688 instantiate_decl (decl
, /*defer_ok=*/true,
4689 /*expl_inst_class_mem_p=*/false);
4696 #include "gt-cp-decl2.h"