1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* Process declarations and symbol lookup for C++ front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
28 /* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
33 #include "coretypes.h"
48 #include "tree-mudflap.h"
50 #include "tree-inline.h"
52 #include "tree-dump.h"
55 #include "pointer-set.h"
57 extern cpp_reader *parse_in;
59 /* This structure contains information about the initializations
60 and/or destructions required for a particular priority level. */
61 typedef struct priority_info_s {
62 /* Nonzero if there have been any initializations at this priority
63 throughout the translation unit. */
64 int initializations_p;
65 /* Nonzero if there have been any destructions at this priority
66 throughout the translation unit. */
70 static void mark_vtable_entries (tree);
71 static bool maybe_emit_vtables (tree);
72 static bool acceptable_java_type (tree);
73 static tree start_objects (int, int);
74 static void finish_objects (int, int, tree);
75 static tree start_static_storage_duration_function (unsigned);
76 static void finish_static_storage_duration_function (tree);
77 static priority_info get_priority_info (int);
78 static void do_static_initialization_or_destruction (tree, bool);
79 static void one_static_initialization_or_destruction (tree, tree, bool);
80 static void generate_ctor_or_dtor_function (bool, int, location_t *);
81 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
83 static tree prune_vars_needing_no_initialization (tree *);
84 static void write_out_vars (tree);
85 static void import_export_class (tree);
86 static tree get_guard_bits (tree);
87 static void determine_visibility_from_class (tree, tree);
88 static bool decl_defined_p (tree);
90 /* A list of static class variables. This is needed, because a
91 static class variable can be declared inside the class without
92 an initializer, and then initialized, statically, outside the class. */
93 static GTY(()) VEC(tree,gc) *pending_statics;
95 /* A list of functions which were declared inline, but which we
96 may need to emit outline anyway. */
97 static GTY(()) VEC(tree,gc) *deferred_fns;
99 /* A list of decls that use types with no linkage, which we need to make
101 static GTY(()) VEC(tree,gc) *no_linkage_decls;
103 /* Nonzero if we're done parsing and into end-of-file activities. */
109 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
110 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
111 that apply to the function). */
114 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals)
120 if (fntype == error_mark_node || ctype == error_mark_node)
121 return error_mark_node;
123 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
124 || TREE_CODE (fntype) == METHOD_TYPE);
126 type_quals = quals & ~TYPE_QUAL_RESTRICT;
127 ctype = cp_build_qualified_type (ctype, type_quals);
128 raises = TYPE_RAISES_EXCEPTIONS (fntype);
129 attrs = TYPE_ATTRIBUTES (fntype);
130 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
131 (TREE_CODE (fntype) == METHOD_TYPE
132 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
133 : TYPE_ARG_TYPES (fntype)));
135 fntype = build_exception_variant (fntype, raises);
137 fntype = cp_build_type_attribute_variant (fntype, attrs);
142 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
143 return type changed to NEW_RET. */
146 change_return_type (tree new_ret, tree fntype)
149 tree args = TYPE_ARG_TYPES (fntype);
150 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
151 tree attrs = TYPE_ATTRIBUTES (fntype);
153 if (same_type_p (new_ret, TREE_TYPE (fntype)))
156 if (TREE_CODE (fntype) == FUNCTION_TYPE)
157 newtype = build_function_type (new_ret, args);
159 newtype = build_method_type_directly
160 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))),
161 new_ret, TREE_CHAIN (args));
163 newtype = build_exception_variant (newtype, raises);
165 newtype = cp_build_type_attribute_variant (newtype, attrs);
170 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
174 cp_build_parm_decl (tree name, tree type)
176 tree parm = build_decl (input_location,
177 PARM_DECL, name, type);
178 /* DECL_ARG_TYPE is only used by the back end and the back end never
180 if (!processing_template_decl)
181 DECL_ARG_TYPE (parm) = type_passed_as (type);
183 /* If the type is a pack expansion, then we have a function
185 if (type && TREE_CODE (type) == TYPE_PACK_EXPANSION)
186 FUNCTION_PARAMETER_PACK_P (parm) = 1;
191 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
195 build_artificial_parm (tree name, tree type)
197 tree parm = cp_build_parm_decl (name, type);
198 DECL_ARTIFICIAL (parm) = 1;
199 /* All our artificial parms are implicitly `const'; they cannot be
201 TREE_READONLY (parm) = 1;
205 /* Constructors for types with virtual baseclasses need an "in-charge" flag
206 saying whether this constructor is responsible for initialization of
207 virtual baseclasses or not. All destructors also need this "in-charge"
208 flag, which additionally determines whether or not the destructor should
209 free the memory for the object.
211 This function adds the "in-charge" flag to member function FN if
212 appropriate. It is called from grokclassfn and tsubst.
213 FN must be either a constructor or destructor.
215 The in-charge flag follows the 'this' parameter, and is followed by the
216 VTT parm (if any), then the user-written parms. */
219 maybe_retrofit_in_chrg (tree fn)
221 tree basetype, arg_types, parms, parm, fntype;
223 /* If we've already add the in-charge parameter don't do it again. */
224 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
227 /* When processing templates we can't know, in general, whether or
228 not we're going to have virtual baseclasses. */
229 if (processing_template_decl)
232 /* We don't need an in-charge parameter for constructors that don't
233 have virtual bases. */
234 if (DECL_CONSTRUCTOR_P (fn)
235 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
238 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
239 basetype = TREE_TYPE (TREE_VALUE (arg_types));
240 arg_types = TREE_CHAIN (arg_types);
242 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
244 /* If this is a subobject constructor or destructor, our caller will
245 pass us a pointer to our VTT. */
246 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
248 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
250 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
251 TREE_CHAIN (parm) = parms;
254 /* ...and then to TYPE_ARG_TYPES. */
255 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
257 DECL_HAS_VTT_PARM_P (fn) = 1;
260 /* Then add the in-charge parm (before the VTT parm). */
261 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
262 TREE_CHAIN (parm) = parms;
264 arg_types = hash_tree_chain (integer_type_node, arg_types);
266 /* Insert our new parameter(s) into the list. */
267 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
269 /* And rebuild the function type. */
270 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
272 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
273 fntype = build_exception_variant (fntype,
274 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
275 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
276 fntype = (cp_build_type_attribute_variant
277 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
278 TREE_TYPE (fn) = fntype;
280 /* Now we've got the in-charge parameter. */
281 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
284 /* Classes overload their constituent function names automatically.
285 When a function name is declared in a record structure,
286 its name is changed to it overloaded name. Since names for
287 constructors and destructors can conflict, we place a leading
290 CNAME is the name of the class we are grokking for.
292 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
294 FLAGS contains bits saying what's special about today's
295 arguments. DTOR_FLAG == DESTRUCTOR.
297 If FUNCTION is a destructor, then we must add the `auto-delete' field
298 as a second parameter. There is some hair associated with the fact
299 that we must "declare" this variable in the manner consistent with the
300 way the rest of the arguments were declared.
302 QUALS are the qualifiers for the this pointer. */
305 grokclassfn (tree ctype, tree function, enum overload_flags flags)
307 tree fn_name = DECL_NAME (function);
309 /* Even within an `extern "C"' block, members get C++ linkage. See
310 [dcl.link] for details. */
311 SET_DECL_LANGUAGE (function, lang_cplusplus);
313 if (fn_name == NULL_TREE)
315 error ("name missing for member function");
316 fn_name = get_identifier ("<anonymous>");
317 DECL_NAME (function) = fn_name;
320 DECL_CONTEXT (function) = ctype;
322 if (flags == DTOR_FLAG)
323 DECL_DESTRUCTOR_P (function) = 1;
325 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
326 maybe_retrofit_in_chrg (function);
329 /* Create an ARRAY_REF, checking for the user doing things backwards
333 grok_array_decl (tree array_expr, tree index_exp)
337 tree orig_array_expr = array_expr;
338 tree orig_index_exp = index_exp;
340 if (error_operand_p (array_expr) || error_operand_p (index_exp))
341 return error_mark_node;
343 if (processing_template_decl)
345 if (type_dependent_expression_p (array_expr)
346 || type_dependent_expression_p (index_exp))
347 return build_min_nt (ARRAY_REF, array_expr, index_exp,
348 NULL_TREE, NULL_TREE);
349 array_expr = build_non_dependent_expr (array_expr);
350 index_exp = build_non_dependent_expr (index_exp);
353 type = TREE_TYPE (array_expr);
355 type = non_reference (type);
357 /* If they have an `operator[]', use that. */
358 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
359 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL,
360 array_expr, index_exp, NULL_TREE,
361 /*overloaded_p=*/NULL, tf_warning_or_error);
366 /* Otherwise, create an ARRAY_REF for a pointer or array type.
367 It is a little-known fact that, if `a' is an array and `i' is
368 an int, you can write `i[a]', which means the same thing as
370 if (TREE_CODE (type) == ARRAY_TYPE)
373 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
375 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
378 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
380 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
382 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
385 if ((p1 && i2) && (i1 && p2))
386 error ("ambiguous conversion for array subscript");
389 array_expr = p1, index_exp = i2;
391 array_expr = p2, index_exp = i1;
394 error ("invalid types %<%T[%T]%> for array subscript",
395 type, TREE_TYPE (index_exp));
396 return error_mark_node;
399 if (array_expr == error_mark_node || index_exp == error_mark_node)
400 error ("ambiguous conversion for array subscript");
402 expr = build_array_ref (input_location, array_expr, index_exp);
404 if (processing_template_decl && expr != error_mark_node)
405 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
406 NULL_TREE, NULL_TREE);
410 /* Given the cast expression EXP, checking out its validity. Either return
411 an error_mark_node if there was an unavoidable error, return a cast to
412 void for trying to delete a pointer w/ the value 0, or return the
413 call to delete. If DOING_VEC is true, we handle things differently
414 for doing an array delete.
415 Implements ARM $5.3.4. This is called from the parser. */
418 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete)
422 if (exp == error_mark_node)
425 if (processing_template_decl)
427 t = build_min (DELETE_EXPR, void_type_node, exp, size);
428 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
429 DELETE_EXPR_USE_VEC (t) = doing_vec;
430 TREE_SIDE_EFFECTS (t) = 1;
434 /* An array can't have been allocated by new, so complain. */
435 if (TREE_CODE (exp) == VAR_DECL
436 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
437 warning (0, "deleting array %q#D", exp);
439 t = build_expr_type_conversion (WANT_POINTER, exp, true);
441 if (t == NULL_TREE || t == error_mark_node)
443 error ("type %q#T argument given to %<delete%>, expected pointer",
445 return error_mark_node;
448 type = TREE_TYPE (t);
450 /* As of Valley Forge, you can delete a pointer to const. */
452 /* You can't delete functions. */
453 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
455 error ("cannot delete a function. Only pointer-to-objects are "
456 "valid arguments to %<delete%>");
457 return error_mark_node;
460 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
461 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
463 warning (0, "deleting %qT is undefined", type);
467 /* Deleting a pointer with the value zero is valid and has no effect. */
468 if (integer_zerop (t))
469 return build1 (NOP_EXPR, void_type_node, t);
472 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
473 sfk_deleting_destructor,
476 return build_delete (type, t, sfk_deleting_destructor,
477 LOOKUP_NORMAL, use_global_delete);
480 /* Report an error if the indicated template declaration is not the
481 sort of thing that should be a member template. */
484 check_member_template (tree tmpl)
488 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
489 decl = DECL_TEMPLATE_RESULT (tmpl);
491 if (TREE_CODE (decl) == FUNCTION_DECL
492 || (TREE_CODE (decl) == TYPE_DECL
493 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
495 /* The parser rejects template declarations in local classes. */
496 gcc_assert (!current_function_decl);
497 /* The parser rejects any use of virtual in a function template. */
498 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
499 && DECL_VIRTUAL_P (decl)));
501 /* The debug-information generating code doesn't know what to do
502 with member templates. */
503 DECL_IGNORED_P (tmpl) = 1;
506 error ("template declaration of %q#D", decl);
509 /* Return true iff TYPE is a valid Java parameter or return type. */
512 acceptable_java_type (tree type)
514 if (type == error_mark_node)
517 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
519 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
521 type = TREE_TYPE (type);
522 if (TREE_CODE (type) == RECORD_TYPE)
525 if (! TYPE_FOR_JAVA (type))
527 if (! CLASSTYPE_TEMPLATE_INFO (type))
529 args = CLASSTYPE_TI_ARGS (type);
530 i = TREE_VEC_LENGTH (args);
533 type = TREE_VEC_ELT (args, i);
534 if (TREE_CODE (type) == POINTER_TYPE)
535 type = TREE_TYPE (type);
536 if (! TYPE_FOR_JAVA (type))
545 /* For a METHOD in a Java class CTYPE, return true if
546 the parameter and return types are valid Java types.
547 Otherwise, print appropriate error messages, and return false. */
550 check_java_method (tree method)
553 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
554 tree ret_type = TREE_TYPE (TREE_TYPE (method));
556 if (!acceptable_java_type (ret_type))
558 error ("Java method %qD has non-Java return type %qT",
563 arg_types = TREE_CHAIN (arg_types);
564 if (DECL_HAS_IN_CHARGE_PARM_P (method))
565 arg_types = TREE_CHAIN (arg_types);
566 if (DECL_HAS_VTT_PARM_P (method))
567 arg_types = TREE_CHAIN (arg_types);
569 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
571 tree type = TREE_VALUE (arg_types);
572 if (!acceptable_java_type (type))
574 if (type != error_mark_node)
575 error ("Java method %qD has non-Java parameter type %qT",
583 /* Sanity check: report error if this function FUNCTION is not
584 really a member of the class (CTYPE) it is supposed to belong to.
585 TEMPLATE_PARMS is used to specify the template parameters of a member
586 template passed as FUNCTION_DECL. If the member template is passed as a
587 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
588 from the declaration. If the function is not a function template, it
590 It returns the original declaration for the function, NULL_TREE if
591 no declaration was found, error_mark_node if an error was emitted. */
594 check_classfn (tree ctype, tree function, tree template_parms)
600 if (DECL_USE_TEMPLATE (function)
601 && !(TREE_CODE (function) == TEMPLATE_DECL
602 && DECL_TEMPLATE_SPECIALIZATION (function))
603 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
604 /* Since this is a specialization of a member template,
605 we're not going to find the declaration in the class.
608 struct S { template <typename T> void f(T); };
609 template <> void S::f(int);
611 we're not going to find `S::f(int)', but there's no
612 reason we should, either. We let our callers know we didn't
613 find the method, but we don't complain. */
616 /* Basic sanity check: for a template function, the template parameters
617 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
618 if (TREE_CODE (function) == TEMPLATE_DECL)
621 && !comp_template_parms (template_parms,
622 DECL_TEMPLATE_PARMS (function)))
624 error ("template parameter lists provided don't match the "
625 "template parameters of %qD", function);
626 return error_mark_node;
628 template_parms = DECL_TEMPLATE_PARMS (function);
631 /* OK, is this a definition of a member template? */
632 is_template = (template_parms != NULL_TREE);
634 /* We must enter the scope here, because conversion operators are
635 named by target type, and type equivalence relies on typenames
636 resolving within the scope of CTYPE. */
637 pushed_scope = push_scope (ctype);
638 ix = class_method_index_for_fn (complete_type (ctype), function);
641 VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (ctype);
642 tree fndecls, fndecl = 0;
644 const char *format = NULL;
646 for (fndecls = VEC_index (tree, methods, ix);
647 fndecls; fndecls = OVL_NEXT (fndecls))
651 fndecl = OVL_CURRENT (fndecls);
652 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
653 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
655 /* We cannot simply call decls_match because this doesn't
656 work for static member functions that are pretending to
657 be methods, and because the name may have been changed by
660 /* Get rid of the this parameter on functions that become
662 if (DECL_STATIC_FUNCTION_P (fndecl)
663 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
664 p1 = TREE_CHAIN (p1);
666 /* A member template definition only matches a member template
668 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
671 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
672 TREE_TYPE (TREE_TYPE (fndecl)))
673 && compparms (p1, p2)
675 || comp_template_parms (template_parms,
676 DECL_TEMPLATE_PARMS (fndecl)))
677 && (DECL_TEMPLATE_SPECIALIZATION (function)
678 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
679 && (!DECL_TEMPLATE_SPECIALIZATION (function)
680 || (DECL_TI_TEMPLATE (function)
681 == DECL_TI_TEMPLATE (fndecl))))
687 pop_scope (pushed_scope);
688 return OVL_CURRENT (fndecls);
691 error_at (DECL_SOURCE_LOCATION (function),
692 "prototype for %q#D does not match any in class %qT",
694 is_conv_op = DECL_CONV_FN_P (fndecl);
697 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
698 fndecls = VEC_index (tree, methods, ix);
701 fndecl = OVL_CURRENT (fndecls);
702 fndecls = OVL_NEXT (fndecls);
704 if (!fndecls && is_conv_op)
706 if (VEC_length (tree, methods) > (size_t) ++ix)
708 fndecls = VEC_index (tree, methods, ix);
709 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
721 format = N_("candidates are: %+#D");
723 format = N_("candidate is: %+#D");
724 error (format, fndecl);
727 else if (!COMPLETE_TYPE_P (ctype))
728 cxx_incomplete_type_error (function, ctype);
730 error ("no %q#D member function declared in class %qT",
734 pop_scope (pushed_scope);
735 return error_mark_node;
738 /* DECL is a function with vague linkage. Remember it so that at the
739 end of the translation unit we can decide whether or not to emit
743 note_vague_linkage_fn (tree decl)
745 DECL_DEFER_OUTPUT (decl) = 1;
746 VEC_safe_push (tree, gc, deferred_fns, decl);
749 /* We have just processed the DECL, which is a static data member.
750 The other parameters are as for cp_finish_decl. */
753 finish_static_data_member_decl (tree decl,
754 tree init, bool init_const_expr_p,
758 DECL_CONTEXT (decl) = current_class_type;
760 /* We cannot call pushdecl here, because that would fill in the
761 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
762 the right thing, namely, to put this decl out straight away. */
764 if (! processing_template_decl)
765 VEC_safe_push (tree, gc, pending_statics, decl);
767 if (LOCAL_CLASS_P (current_class_type))
768 permerror (input_location, "local class %q#T shall not have static data member %q#D",
769 current_class_type, decl);
771 /* Static consts need not be initialized in the class definition. */
772 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
774 static int explained = 0;
776 error ("initializer invalid for static member with constructor");
779 error ("(an out of class initialization is required)");
785 DECL_INITIAL (decl) = init;
786 DECL_IN_AGGR_P (decl) = 1;
788 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
789 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
790 SET_VAR_HAD_UNKNOWN_BOUND (decl);
792 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
795 /* DECLARATOR and DECLSPECS correspond to a class member. The other
796 parameters are as for cp_finish_decl. Return the DECL for the
797 class member declared. */
800 grokfield (const cp_declarator *declarator,
801 cp_decl_specifier_seq *declspecs,
802 tree init, bool init_const_expr_p,
807 const char *asmspec = 0;
808 int flags = LOOKUP_ONLYCONVERTING;
812 && TREE_CODE (init) == TREE_LIST
813 && TREE_VALUE (init) == error_mark_node
814 && TREE_CHAIN (init) == NULL_TREE)
817 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
818 if (! value || error_operand_p (value))
819 /* friend or constructor went bad. */
820 return error_mark_node;
822 if (TREE_CODE (value) == TYPE_DECL && init)
824 error ("typedef %qD is initialized (use decltype instead)", value);
828 /* Pass friendly classes back. */
829 if (value == void_type_node)
832 /* Pass friend decls back. */
833 if ((TREE_CODE (value) == FUNCTION_DECL
834 || TREE_CODE (value) == TEMPLATE_DECL)
835 && DECL_CONTEXT (value) != current_class_type)
838 name = DECL_NAME (value);
840 if (name != NULL_TREE)
842 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
844 error ("explicit template argument list not allowed");
845 return error_mark_node;
848 if (IDENTIFIER_POINTER (name)[0] == '_'
849 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
850 error ("member %qD conflicts with virtual function table field name",
854 /* Stash away type declarations. */
855 if (TREE_CODE (value) == TYPE_DECL)
857 DECL_NONLOCAL (value) = 1;
858 DECL_CONTEXT (value) = current_class_type;
860 if (processing_template_decl)
861 value = push_template_decl (value);
867 /* If this is a typedef that names the class for linkage purposes
868 (7.1.3p8), apply any attributes directly to the type. */
869 if (TAGGED_TYPE_P (TREE_TYPE (value))
870 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
871 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
873 cplus_decl_attributes (&value, attrlist, attrflags);
876 if (declspecs->specs[(int)ds_typedef]
877 && TREE_TYPE (value) != error_mark_node
878 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
879 cp_set_underlying_type (value);
884 if (DECL_IN_AGGR_P (value))
886 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
887 return void_type_node;
890 if (asmspec_tree && asmspec_tree != error_mark_node)
891 asmspec = TREE_STRING_POINTER (asmspec_tree);
895 if (TREE_CODE (value) == FUNCTION_DECL)
897 /* Initializers for functions are rejected early in the parser.
898 If we get here, it must be a pure specifier for a method. */
899 if (init == ridpointers[(int)RID_DELETE])
901 DECL_DELETED_FN (value) = 1;
902 DECL_DECLARED_INLINE_P (value) = 1;
903 DECL_INITIAL (value) = error_mark_node;
905 else if (init == ridpointers[(int)RID_DEFAULT])
907 if (defaultable_fn_check (value))
909 DECL_DEFAULTED_FN (value) = 1;
910 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
911 DECL_DECLARED_INLINE_P (value) = 1;
914 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
916 if (integer_zerop (init))
917 DECL_PURE_VIRTUAL_P (value) = 1;
918 else if (error_operand_p (init))
919 ; /* An error has already been reported. */
921 error ("invalid initializer for member function %qD",
926 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
927 error ("initializer specified for static member function %qD",
931 else if (pedantic && TREE_CODE (value) != VAR_DECL)
932 /* Already complained in grokdeclarator. */
934 else if (!processing_template_decl)
936 if (TREE_CODE (init) == CONSTRUCTOR)
937 init = digest_init (TREE_TYPE (value), init);
939 init = integral_constant_value (init);
941 if (init != error_mark_node && !TREE_CONSTANT (init))
943 /* We can allow references to things that are effectively
944 static, since references are initialized with the
946 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
947 || (TREE_STATIC (init) == 0
948 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
950 error ("field initializer is not constant");
951 init = error_mark_node;
957 if (processing_template_decl
958 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
960 value = push_template_decl (value);
961 if (error_operand_p (value))
962 return error_mark_node;
966 cplus_decl_attributes (&value, attrlist, 0);
968 switch (TREE_CODE (value))
971 finish_static_data_member_decl (value, init, init_const_expr_p,
972 asmspec_tree, flags);
977 error ("%<asm%> specifiers are not permitted on non-static data members");
978 if (DECL_INITIAL (value) == error_mark_node)
979 init = error_mark_node;
980 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
982 DECL_INITIAL (value) = init;
983 DECL_IN_AGGR_P (value) = 1;
988 set_user_assembler_name (value, asmspec);
990 cp_finish_decl (value,
992 /*init_const_expr_p=*/false,
993 asmspec_tree, flags);
995 /* Pass friends back this way. */
996 if (DECL_FRIEND_P (value))
997 return void_type_node;
999 DECL_IN_AGGR_P (value) = 1;
1008 /* Like `grokfield', but for bitfields.
1009 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1012 grokbitfield (const cp_declarator *declarator,
1013 cp_decl_specifier_seq *declspecs, tree width,
1016 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1018 if (value == error_mark_node)
1019 return NULL_TREE; /* friends went bad. */
1021 /* Pass friendly classes back. */
1022 if (TREE_CODE (value) == VOID_TYPE)
1023 return void_type_node;
1025 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))
1026 && (POINTER_TYPE_P (value)
1027 || !dependent_type_p (TREE_TYPE (value))))
1029 error ("bit-field %qD with non-integral type", value);
1030 return error_mark_node;
1033 if (TREE_CODE (value) == TYPE_DECL)
1035 error ("cannot declare %qD to be a bit-field type", value);
1039 /* Usually, finish_struct_1 catches bitfields with invalid types.
1040 But, in the case of bitfields with function type, we confuse
1041 ourselves into thinking they are member functions, so we must
1043 if (TREE_CODE (value) == FUNCTION_DECL)
1045 error ("cannot declare bit-field %qD with function type",
1050 if (DECL_IN_AGGR_P (value))
1052 error ("%qD is already defined in the class %qT", value,
1053 DECL_CONTEXT (value));
1054 return void_type_node;
1057 if (TREE_STATIC (value))
1059 error ("static member %qD cannot be a bit-field", value);
1062 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1064 if (width != error_mark_node)
1066 constant_expression_warning (width);
1067 DECL_INITIAL (value) = width;
1068 SET_DECL_C_BIT_FIELD (value);
1071 DECL_IN_AGGR_P (value) = 1;
1074 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1080 /* Returns true iff ATTR is an attribute which needs to be applied at
1081 instantiation time rather than template definition time. */
1084 is_late_template_attribute (tree attr, tree decl)
1086 tree name = TREE_PURPOSE (attr);
1087 tree args = TREE_VALUE (attr);
1088 const struct attribute_spec *spec = lookup_attribute_spec (name);
1092 /* Unknown attribute. */
1095 /* Attribute weak handling wants to write out assembly right away. */
1096 if (is_attribute_p ("weak", name))
1099 /* If any of the arguments are dependent expressions, we can't evaluate
1100 the attribute until instantiation time. */
1101 for (arg = args; arg; arg = TREE_CHAIN (arg))
1103 tree t = TREE_VALUE (arg);
1105 /* If the first attribute argument is an identifier, only consider
1106 second and following arguments. Attributes like mode, format,
1107 cleanup and several target specific attributes aren't late
1108 just because they have an IDENTIFIER_NODE as first argument. */
1109 if (arg == args && TREE_CODE (t) == IDENTIFIER_NODE)
1112 if (value_dependent_expression_p (t)
1113 || type_dependent_expression_p (t))
1117 if (TREE_CODE (decl) == TYPE_DECL
1119 || spec->type_required)
1121 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1123 /* We can't apply any attributes to a completely unknown type until
1124 instantiation time. */
1125 enum tree_code code = TREE_CODE (type);
1126 if (code == TEMPLATE_TYPE_PARM
1127 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1128 || code == TYPENAME_TYPE)
1130 /* Also defer most attributes on dependent types. This is not
1131 necessary in all cases, but is the better default. */
1132 else if (dependent_type_p (type)
1133 /* But attribute visibility specifically works on
1135 && !is_attribute_p ("visibility", name))
1144 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1145 applied at instantiation time and return them. If IS_DEPENDENT is true,
1146 the declaration itself is dependent, so all attributes should be applied
1147 at instantiation time. */
1150 splice_template_attributes (tree *attr_p, tree decl)
1153 tree late_attrs = NULL_TREE;
1154 tree *q = &late_attrs;
1161 if (is_late_template_attribute (*p, decl))
1163 ATTR_IS_DEPENDENT (*p) = 1;
1165 *p = TREE_CHAIN (*p);
1166 q = &TREE_CHAIN (*q);
1170 p = &TREE_CHAIN (*p);
1176 /* Remove any late attributes from the list in ATTR_P and attach them to
1180 save_template_attributes (tree *attr_p, tree *decl_p)
1182 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1184 tree old_attrs = NULL_TREE;
1189 if (DECL_P (*decl_p))
1190 q = &DECL_ATTRIBUTES (*decl_p);
1192 q = &TYPE_ATTRIBUTES (*decl_p);
1196 /* Place the late attributes at the beginning of the attribute
1198 TREE_CHAIN (tree_last (late_attrs)) = *q;
1201 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1203 /* We've added new attributes directly to the main variant, so
1204 now we need to update all of the other variants to include
1205 these new attributes. */
1207 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1208 variant = TYPE_NEXT_VARIANT (variant))
1210 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1211 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1216 /* Like reconstruct_complex_type, but handle also template trees. */
1219 cp_reconstruct_complex_type (tree type, tree bottom)
1223 if (TREE_CODE (type) == POINTER_TYPE)
1225 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1226 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1227 TYPE_REF_CAN_ALIAS_ALL (type));
1229 else if (TREE_CODE (type) == REFERENCE_TYPE)
1231 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1232 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1233 TYPE_REF_CAN_ALIAS_ALL (type));
1235 else if (TREE_CODE (type) == ARRAY_TYPE)
1237 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1238 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1239 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1240 element type qualification will be handled by the recursive
1241 cp_reconstruct_complex_type call and cp_build_qualified_type
1242 for ARRAY_TYPEs changes the element type. */
1245 else if (TREE_CODE (type) == FUNCTION_TYPE)
1247 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1248 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1250 else if (TREE_CODE (type) == METHOD_TYPE)
1252 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1253 /* The build_method_type_directly() routine prepends 'this' to argument list,
1254 so we must compensate by getting rid of it. */
1256 = build_method_type_directly
1257 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
1259 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1261 else if (TREE_CODE (type) == OFFSET_TYPE)
1263 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1264 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1269 if (TYPE_ATTRIBUTES (type))
1270 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1271 return cp_build_qualified_type (outer, TYPE_QUALS (type));
1274 /* Like decl_attributes, but handle C++ complexity. */
1277 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1279 if (*decl == NULL_TREE || *decl == void_type_node
1280 || *decl == error_mark_node
1281 || attributes == NULL_TREE)
1284 if (processing_template_decl)
1286 if (check_for_bare_parameter_packs (attributes))
1289 save_template_attributes (&attributes, decl);
1290 if (attributes == NULL_TREE)
1294 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1295 decl = &DECL_TEMPLATE_RESULT (*decl);
1297 decl_attributes (decl, attributes, flags);
1299 if (TREE_CODE (*decl) == TYPE_DECL)
1300 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1303 /* Walks through the namespace- or function-scope anonymous union
1304 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1305 Returns one of the fields for use in the mangled name. */
1308 build_anon_union_vars (tree type, tree object)
1310 tree main_decl = NULL_TREE;
1313 /* Rather than write the code to handle the non-union case,
1314 just give an error. */
1315 if (TREE_CODE (type) != UNION_TYPE)
1316 error ("anonymous struct not inside named type");
1318 for (field = TYPE_FIELDS (type);
1320 field = TREE_CHAIN (field))
1325 if (DECL_ARTIFICIAL (field))
1327 if (TREE_CODE (field) != FIELD_DECL)
1329 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1330 "have non-static data members", field);
1334 if (TREE_PRIVATE (field))
1335 permerror (input_location, "private member %q+#D in anonymous union", field);
1336 else if (TREE_PROTECTED (field))
1337 permerror (input_location, "protected member %q+#D in anonymous union", field);
1339 if (processing_template_decl)
1340 ref = build_min_nt (COMPONENT_REF, object,
1341 DECL_NAME (field), NULL_TREE);
1343 ref = build_class_member_access_expr (object, field, NULL_TREE,
1344 false, tf_warning_or_error);
1346 if (DECL_NAME (field))
1350 decl = build_decl (input_location,
1351 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1352 DECL_ANON_UNION_VAR_P (decl) = 1;
1353 DECL_ARTIFICIAL (decl) = 1;
1355 base = get_base_address (object);
1356 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1357 TREE_STATIC (decl) = TREE_STATIC (base);
1358 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1360 SET_DECL_VALUE_EXPR (decl, ref);
1361 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1363 decl = pushdecl (decl);
1365 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1366 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1370 if (main_decl == NULL_TREE)
1377 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1378 anonymous union, then all members must be laid out together. PUBLIC_P
1379 is nonzero if this union is not declared static. */
1382 finish_anon_union (tree anon_union_decl)
1388 if (anon_union_decl == error_mark_node)
1391 type = TREE_TYPE (anon_union_decl);
1392 public_p = TREE_PUBLIC (anon_union_decl);
1394 /* The VAR_DECL's context is the same as the TYPE's context. */
1395 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1397 if (TYPE_FIELDS (type) == NULL_TREE)
1402 error ("namespace-scope anonymous aggregates must be static");
1406 main_decl = build_anon_union_vars (type, anon_union_decl);
1407 if (main_decl == error_mark_node)
1409 if (main_decl == NULL_TREE)
1411 warning (0, "anonymous union with no members");
1415 if (!processing_template_decl)
1417 /* Use main_decl to set the mangled name. */
1418 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1419 maybe_commonize_var (anon_union_decl);
1420 mangle_decl (anon_union_decl);
1421 DECL_NAME (anon_union_decl) = NULL_TREE;
1424 pushdecl (anon_union_decl);
1425 if (building_stmt_tree ()
1426 && at_function_scope_p ())
1427 add_decl_expr (anon_union_decl);
1428 else if (!processing_template_decl)
1429 rest_of_decl_compilation (anon_union_decl,
1430 toplevel_bindings_p (), at_eof);
1433 /* Auxiliary functions to make type signatures for
1434 `operator new' and `operator delete' correspond to
1435 what compiler will be expecting. */
1438 coerce_new_type (tree type)
1441 tree args = TYPE_ARG_TYPES (type);
1443 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1445 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1448 error ("%<operator new%> must return type %qT", ptr_type_node);
1451 if (args && args != void_list_node)
1453 if (TREE_PURPOSE (args))
1455 /* [basic.stc.dynamic.allocation]
1457 The first parameter shall not have an associated default
1459 error ("the first parameter of %<operator new%> cannot "
1460 "have a default argument");
1461 /* Throw away the default argument. */
1462 TREE_PURPOSE (args) = NULL_TREE;
1465 if (!same_type_p (TREE_VALUE (args), size_type_node))
1468 args = TREE_CHAIN (args);
1475 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1476 "as first parameter", size_type_node);
1481 args = tree_cons (NULL_TREE, size_type_node, args);
1484 type = build_exception_variant
1485 (build_function_type (ptr_type_node, args),
1486 TYPE_RAISES_EXCEPTIONS (type));
1494 coerce_delete_type (tree type)
1497 tree args = TYPE_ARG_TYPES (type);
1499 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1501 if (!same_type_p (TREE_TYPE (type), void_type_node))
1504 error ("%<operator delete%> must return type %qT", void_type_node);
1507 if (!args || args == void_list_node
1508 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1511 if (args && args != void_list_node)
1512 args = TREE_CHAIN (args);
1513 error ("%<operator delete%> takes type %qT as first parameter",
1519 args = tree_cons (NULL_TREE, ptr_type_node, args);
1522 type = build_exception_variant
1523 (build_function_type (void_type_node, args),
1524 TYPE_RAISES_EXCEPTIONS (type));
1532 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1533 and mark them as needed. */
1536 mark_vtable_entries (tree decl)
1539 unsigned HOST_WIDE_INT idx;
1541 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1546 STRIP_NOPS (fnaddr);
1548 if (TREE_CODE (fnaddr) != ADDR_EXPR
1549 && TREE_CODE (fnaddr) != FDESC_EXPR)
1550 /* This entry is an offset: a virtual base class offset, a
1551 virtual call offset, an RTTI offset, etc. */
1554 fn = TREE_OPERAND (fnaddr, 0);
1555 TREE_ADDRESSABLE (fn) = 1;
1556 /* When we don't have vcall offsets, we output thunks whenever
1557 we output the vtables that contain them. With vcall offsets,
1558 we know all the thunks we'll need when we emit a virtual
1559 function, so we emit the thunks there instead. */
1560 if (DECL_THUNK_P (fn))
1561 use_thunk (fn, /*emit_p=*/0);
1566 /* Set DECL up to have the closest approximation of "initialized common"
1567 linkage available. */
1570 comdat_linkage (tree decl)
1573 make_decl_one_only (decl, cxx_comdat_group (decl));
1574 else if (TREE_CODE (decl) == FUNCTION_DECL
1575 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1576 /* We can just emit function and compiler-generated variables
1577 statically; having multiple copies is (for the most part) only
1580 There are two correctness issues, however: the address of a
1581 template instantiation with external linkage should be the
1582 same, independent of what translation unit asks for the
1583 address, and this will not hold when we emit multiple copies of
1584 the function. However, there's little else we can do.
1586 Also, by default, the typeinfo implementation assumes that
1587 there will be only one copy of the string used as the name for
1588 each type. Therefore, if weak symbols are unavailable, the
1589 run-time library should perform a more conservative check; it
1590 should perform a string comparison, rather than an address
1592 TREE_PUBLIC (decl) = 0;
1595 /* Static data member template instantiations, however, cannot
1596 have multiple copies. */
1597 if (DECL_INITIAL (decl) == 0
1598 || DECL_INITIAL (decl) == error_mark_node)
1599 DECL_COMMON (decl) = 1;
1600 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1602 DECL_COMMON (decl) = 1;
1603 DECL_INITIAL (decl) = error_mark_node;
1605 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1607 /* We can't do anything useful; leave vars for explicit
1609 DECL_EXTERNAL (decl) = 1;
1610 DECL_NOT_REALLY_EXTERN (decl) = 0;
1614 DECL_COMDAT (decl) = 1;
1617 /* For win32 we also want to put explicit instantiations in
1618 linkonce sections, so that they will be merged with implicit
1619 instantiations; otherwise we get duplicate symbol errors.
1620 For Darwin we do not want explicit instantiations to be
1624 maybe_make_one_only (tree decl)
1626 /* We used to say that this was not necessary on targets that support weak
1627 symbols, because the implicit instantiations will defer to the explicit
1628 one. However, that's not actually the case in SVR4; a strong definition
1629 after a weak one is an error. Also, not making explicit
1630 instantiations one_only means that we can end up with two copies of
1631 some template instantiations. */
1635 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1636 we can get away with not emitting them if they aren't used. We need
1637 to for variables so that cp_finish_decl will update their linkage,
1638 because their DECL_INITIAL may not have been set properly yet. */
1640 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1641 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1642 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1644 make_decl_one_only (decl, cxx_comdat_group (decl));
1646 if (TREE_CODE (decl) == VAR_DECL)
1648 DECL_COMDAT (decl) = 1;
1649 /* Mark it needed so we don't forget to emit it. */
1650 mark_decl_referenced (decl);
1655 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1656 This predicate will give the right answer during parsing of the
1657 function, which other tests may not. */
1660 vague_linkage_p (tree decl)
1662 /* Unfortunately, import_export_decl has not always been called
1663 before the function is processed, so we cannot simply check
1665 return (DECL_COMDAT (decl)
1666 || (((TREE_CODE (decl) == FUNCTION_DECL
1667 && DECL_DECLARED_INLINE_P (decl))
1668 || (DECL_LANG_SPECIFIC (decl)
1669 && DECL_TEMPLATE_INSTANTIATION (decl)))
1670 && TREE_PUBLIC (decl)));
1673 /* Determine whether or not we want to specifically import or export CTYPE,
1674 using various heuristics. */
1677 import_export_class (tree ctype)
1679 /* -1 for imported, 1 for exported. */
1680 int import_export = 0;
1682 /* It only makes sense to call this function at EOF. The reason is
1683 that this function looks at whether or not the first non-inline
1684 non-abstract virtual member function has been defined in this
1685 translation unit. But, we can't possibly know that until we've
1686 seen the entire translation unit. */
1687 gcc_assert (at_eof);
1689 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1692 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1693 we will have CLASSTYPE_INTERFACE_ONLY set but not
1694 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1695 heuristic because someone will supply a #pragma implementation
1696 elsewhere, and deducing it here would produce a conflict. */
1697 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1700 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1702 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1704 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1705 && !flag_implicit_templates)
1706 /* For a template class, without -fimplicit-templates, check the
1707 repository. If the virtual table is assigned to this
1708 translation unit, then export the class; otherwise, import
1710 import_export = repo_export_class_p (ctype) ? 1 : -1;
1711 else if (TYPE_POLYMORPHIC_P (ctype))
1713 /* The ABI specifies that the virtual table and associated
1714 information are emitted with the key method, if any. */
1715 tree method = CLASSTYPE_KEY_METHOD (ctype);
1716 /* If weak symbol support is not available, then we must be
1717 careful not to emit the vtable when the key function is
1718 inline. An inline function can be defined in multiple
1719 translation units. If we were to emit the vtable in each
1720 translation unit containing a definition, we would get
1721 multiple definition errors at link-time. */
1722 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1723 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1726 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1727 a definition anywhere else. */
1728 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1731 /* Allow back ends the chance to overrule the decision. */
1732 if (targetm.cxx.import_export_class)
1733 import_export = targetm.cxx.import_export_class (ctype, import_export);
1737 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1738 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1742 /* Return true if VAR has already been provided to the back end; in that
1743 case VAR should not be modified further by the front end. */
1745 var_finalized_p (tree var)
1747 return varpool_node (var)->finalized;
1750 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1751 must be emitted in this translation unit. Mark it as such. */
1754 mark_needed (tree decl)
1756 /* It's possible that we no longer need to set
1757 TREE_SYMBOL_REFERENCED here directly, but doing so is
1759 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1760 mark_decl_referenced (decl);
1763 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1764 returns true if a definition of this entity should be provided in
1765 this object file. Callers use this function to determine whether
1766 or not to let the back end know that a definition of DECL is
1767 available in this translation unit. */
1770 decl_needed_p (tree decl)
1772 gcc_assert (TREE_CODE (decl) == VAR_DECL
1773 || TREE_CODE (decl) == FUNCTION_DECL);
1774 /* This function should only be called at the end of the translation
1775 unit. We cannot be sure of whether or not something will be
1776 COMDAT until that point. */
1777 gcc_assert (at_eof);
1779 /* All entities with external linkage that are not COMDAT should be
1780 emitted; they may be referred to from other object files. */
1781 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1783 /* If this entity was used, let the back end see it; it will decide
1784 whether or not to emit it into the object file. */
1785 if (TREE_USED (decl)
1786 || (DECL_ASSEMBLER_NAME_SET_P (decl)
1787 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
1789 /* Functions marked "dllexport" must be emitted so that they are
1790 visible to other DLLs. */
1791 if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1793 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1794 reference to DECL might cause it to be emitted later. */
1798 /* If necessary, write out the vtables for the dynamic class CTYPE.
1799 Returns true if any vtables were emitted. */
1802 maybe_emit_vtables (tree ctype)
1808 /* If the vtables for this class have already been emitted there is
1809 nothing more to do. */
1810 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1811 if (var_finalized_p (primary_vtbl))
1813 /* Ignore dummy vtables made by get_vtable_decl. */
1814 if (TREE_TYPE (primary_vtbl) == void_type_node)
1817 /* On some targets, we cannot determine the key method until the end
1818 of the translation unit -- which is when this function is
1820 if (!targetm.cxx.key_method_may_be_inline ())
1821 determine_key_method (ctype);
1823 /* See if any of the vtables are needed. */
1824 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1826 import_export_decl (vtbl);
1827 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1832 /* If the references to this class' vtables are optimized away,
1833 still emit the appropriate debugging information. See
1835 if (DECL_COMDAT (primary_vtbl)
1836 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1837 note_debug_info_needed (ctype);
1841 /* The ABI requires that we emit all of the vtables if we emit any
1843 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1845 /* Mark entities references from the virtual table as used. */
1846 mark_vtable_entries (vtbl);
1848 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1850 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), LOOKUP_NORMAL);
1852 /* It had better be all done at compile-time. */
1857 DECL_EXTERNAL (vtbl) = 0;
1858 rest_of_decl_compilation (vtbl, 1, 1);
1860 /* Because we're only doing syntax-checking, we'll never end up
1861 actually marking the variable as written. */
1862 if (flag_syntax_only)
1863 TREE_ASM_WRITTEN (vtbl) = 1;
1866 /* Since we're writing out the vtable here, also write the debug
1868 note_debug_info_needed (ctype);
1873 /* A special return value from type_visibility meaning internal
1876 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
1878 /* walk_tree helper function for type_visibility. */
1881 min_vis_r (tree *tp, int *walk_subtrees, void *data)
1883 int *vis_p = (int *)data;
1888 else if (CLASS_TYPE_P (*tp))
1890 if (!TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
1892 *vis_p = VISIBILITY_ANON;
1895 else if (CLASSTYPE_VISIBILITY (*tp) > *vis_p)
1896 *vis_p = CLASSTYPE_VISIBILITY (*tp);
1901 /* Returns the visibility of TYPE, which is the minimum visibility of its
1905 type_visibility (tree type)
1907 int vis = VISIBILITY_DEFAULT;
1908 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
1912 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
1913 specified (or if VISIBILITY is static). */
1916 constrain_visibility (tree decl, int visibility)
1918 if (visibility == VISIBILITY_ANON)
1920 /* extern "C" declarations aren't affected by the anonymous
1922 if (!DECL_EXTERN_C_P (decl))
1924 TREE_PUBLIC (decl) = 0;
1925 DECL_WEAK (decl) = 0;
1926 DECL_COMMON (decl) = 0;
1927 DECL_COMDAT_GROUP (decl) = NULL_TREE;
1928 DECL_INTERFACE_KNOWN (decl) = 1;
1929 if (DECL_LANG_SPECIFIC (decl))
1930 DECL_NOT_REALLY_EXTERN (decl) = 1;
1933 else if (visibility > DECL_VISIBILITY (decl)
1934 && !DECL_VISIBILITY_SPECIFIED (decl))
1936 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
1942 /* Constrain the visibility of DECL based on the visibility of its template
1946 constrain_visibility_for_template (tree decl, tree targs)
1948 /* If this is a template instantiation, check the innermost
1949 template args for visibility constraints. The outer template
1950 args are covered by the class check. */
1951 tree args = INNERMOST_TEMPLATE_ARGS (targs);
1953 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
1957 tree arg = TREE_VEC_ELT (args, i-1);
1959 vis = type_visibility (arg);
1960 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
1963 if (TREE_CODE (arg) == ADDR_EXPR)
1964 arg = TREE_OPERAND (arg, 0);
1965 if (TREE_CODE (arg) == VAR_DECL
1966 || TREE_CODE (arg) == FUNCTION_DECL)
1968 if (! TREE_PUBLIC (arg))
1969 vis = VISIBILITY_ANON;
1971 vis = DECL_VISIBILITY (arg);
1975 constrain_visibility (decl, vis);
1979 /* Like c_determine_visibility, but with additional C++-specific
1982 Function-scope entities can rely on the function's visibility because
1983 it is set in start_preparsed_function.
1985 Class-scope entities cannot rely on the class's visibility until the end
1986 of the enclosing class definition.
1988 Note that because namespaces have multiple independent definitions,
1989 namespace visibility is handled elsewhere using the #pragma visibility
1990 machinery rather than by decorating the namespace declaration.
1992 The goal is for constraints from the type to give a diagnostic, and
1993 other constraints to be applied silently. */
1996 determine_visibility (tree decl)
1998 tree class_type = NULL_TREE;
2000 bool orig_visibility_specified;
2001 enum symbol_visibility orig_visibility;
2003 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2005 /* Only relevant for names with external linkage. */
2006 if (!TREE_PUBLIC (decl))
2009 /* Cloned constructors and destructors get the same visibility as
2010 the underlying function. That should be set up in
2011 maybe_clone_body. */
2012 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2014 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2015 orig_visibility = DECL_VISIBILITY (decl);
2017 if (TREE_CODE (decl) == TYPE_DECL)
2019 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2020 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2021 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2026 else if (DECL_LANG_SPECIFIC (decl))
2027 use_template = DECL_USE_TEMPLATE (decl);
2031 /* If DECL is a member of a class, visibility specifiers on the
2032 class can influence the visibility of the DECL. */
2033 if (DECL_CLASS_SCOPE_P (decl))
2034 class_type = DECL_CONTEXT (decl);
2037 /* Not a class member. */
2039 /* Virtual tables have DECL_CONTEXT set to their associated class,
2040 so they are automatically handled above. */
2041 gcc_assert (TREE_CODE (decl) != VAR_DECL
2042 || !DECL_VTABLE_OR_VTT_P (decl));
2044 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2046 /* Local statics and classes get the visibility of their
2047 containing function by default, except that
2048 -fvisibility-inlines-hidden doesn't affect them. */
2049 tree fn = DECL_CONTEXT (decl);
2050 if (DECL_VISIBILITY_SPECIFIED (fn) || ! DECL_CLASS_SCOPE_P (fn))
2052 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2053 DECL_VISIBILITY_SPECIFIED (decl) =
2054 DECL_VISIBILITY_SPECIFIED (fn);
2057 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2059 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2060 but have no TEMPLATE_INFO, so don't try to check it. */
2063 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl)
2064 && flag_visibility_ms_compat)
2066 /* Under -fvisibility-ms-compat, types are visible by default,
2067 even though their contents aren't. */
2068 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2069 int underlying_vis = type_visibility (underlying_type);
2070 if (underlying_vis == VISIBILITY_ANON
2071 || CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type))
2072 constrain_visibility (decl, underlying_vis);
2074 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2076 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2078 /* tinfo visibility is based on the type it's for. */
2079 constrain_visibility
2080 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))));
2082 /* Give the target a chance to override the visibility associated
2084 if (TREE_PUBLIC (decl)
2085 && !DECL_REALLY_EXTERN (decl)
2086 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2087 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2088 targetm.cxx.determine_class_data_visibility (decl);
2090 else if (use_template)
2091 /* Template instantiations and specializations get visibility based
2092 on their template unless they override it with an attribute. */;
2093 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2095 /* Set default visibility to whatever the user supplied with
2096 #pragma GCC visibility or a namespace visibility attribute. */
2097 DECL_VISIBILITY (decl) = default_visibility;
2098 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2104 /* If the specialization doesn't specify visibility, use the
2105 visibility from the template. */
2106 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2107 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2108 : DECL_TEMPLATE_INFO (decl));
2109 tree args = TI_ARGS (tinfo);
2111 if (args != error_mark_node)
2113 int depth = TMPL_ARGS_DEPTH (args);
2114 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2116 if (!DECL_VISIBILITY_SPECIFIED (decl))
2118 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2119 DECL_VISIBILITY_SPECIFIED (decl)
2120 = DECL_VISIBILITY_SPECIFIED (pattern);
2123 /* FIXME should TMPL_ARGS_DEPTH really return 1 for null input? */
2124 if (args && depth > template_class_depth (class_type))
2125 /* Limit visibility based on its template arguments. */
2126 constrain_visibility_for_template (decl, args);
2131 determine_visibility_from_class (decl, class_type);
2133 if (decl_anon_ns_mem_p (decl))
2134 /* Names in an anonymous namespace get internal linkage.
2135 This might change once we implement export. */
2136 constrain_visibility (decl, VISIBILITY_ANON);
2137 else if (TREE_CODE (decl) != TYPE_DECL)
2139 /* Propagate anonymity from type to decl. */
2140 int tvis = type_visibility (TREE_TYPE (decl));
2141 if (tvis == VISIBILITY_ANON
2142 || ! DECL_VISIBILITY_SPECIFIED (decl))
2143 constrain_visibility (decl, tvis);
2145 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2146 /* DR 757: A type without linkage shall not be used as the type of a
2147 variable or function with linkage, unless
2148 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2149 o the variable or function is not used (3.2 [basic.def.odr]) or is
2150 defined in the same translation unit.
2152 Since non-extern "C" decls need to be defined in the same
2153 translation unit, we can make the type internal. */
2154 constrain_visibility (decl, VISIBILITY_ANON);
2156 /* If visibility changed and DECL already has DECL_RTL, ensure
2157 symbol flags are updated. */
2158 if ((DECL_VISIBILITY (decl) != orig_visibility
2159 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2160 && ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2161 || TREE_CODE (decl) == FUNCTION_DECL)
2162 && DECL_RTL_SET_P (decl))
2163 make_decl_rtl (decl);
2166 /* By default, static data members and function members receive
2167 the visibility of their containing class. */
2170 determine_visibility_from_class (tree decl, tree class_type)
2172 if (DECL_VISIBILITY_SPECIFIED (decl))
2175 if (visibility_options.inlines_hidden
2176 /* Don't do this for inline templates; specializations might not be
2177 inline, and we don't want them to inherit the hidden
2178 visibility. We'll set it here for all inline instantiations. */
2179 && !processing_template_decl
2180 && TREE_CODE (decl) == FUNCTION_DECL
2181 && DECL_DECLARED_INLINE_P (decl)
2182 && (! DECL_LANG_SPECIFIC (decl)
2183 || ! DECL_EXPLICIT_INSTANTIATION (decl)))
2184 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2187 /* Default to the class visibility. */
2188 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2189 DECL_VISIBILITY_SPECIFIED (decl)
2190 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2193 /* Give the target a chance to override the visibility associated
2195 if (TREE_CODE (decl) == VAR_DECL
2196 && (DECL_TINFO_P (decl)
2197 || (DECL_VTABLE_OR_VTT_P (decl)
2198 /* Construction virtual tables are not exported because
2199 they cannot be referred to from other object files;
2200 their name is not standardized by the ABI. */
2201 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2202 && TREE_PUBLIC (decl)
2203 && !DECL_REALLY_EXTERN (decl)
2204 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2205 targetm.cxx.determine_class_data_visibility (decl);
2208 /* Constrain the visibility of a class TYPE based on the visibility of its
2209 field types. Warn if any fields require lesser visibility. */
2212 constrain_class_visibility (tree type)
2218 int vis = type_visibility (type);
2220 if (vis == VISIBILITY_ANON
2221 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2224 /* Don't warn about visibility if the class has explicit visibility. */
2225 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2226 vis = VISIBILITY_INTERNAL;
2228 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
2229 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2231 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2232 int subvis = type_visibility (ftype);
2234 if (subvis == VISIBILITY_ANON)
2236 if (!in_main_input_context ())
2238 %qT has a field %qD whose type uses the anonymous namespace",
2241 else if (MAYBE_CLASS_TYPE_P (ftype)
2242 && vis < VISIBILITY_HIDDEN
2243 && subvis >= VISIBILITY_HIDDEN)
2244 warning (OPT_Wattributes, "\
2245 %qT declared with greater visibility than the type of its field %qD",
2249 binfo = TYPE_BINFO (type);
2250 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2252 int subvis = type_visibility (TREE_TYPE (t));
2254 if (subvis == VISIBILITY_ANON)
2256 if (!in_main_input_context())
2258 %qT has a base %qT whose type uses the anonymous namespace",
2259 type, TREE_TYPE (t));
2261 else if (vis < VISIBILITY_HIDDEN
2262 && subvis >= VISIBILITY_HIDDEN)
2263 warning (OPT_Wattributes, "\
2264 %qT declared with greater visibility than its base %qT",
2265 type, TREE_TYPE (t));
2269 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2270 for DECL has not already been determined, do so now by setting
2271 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2272 function is called entities with vague linkage whose definitions
2273 are available must have TREE_PUBLIC set.
2275 If this function decides to place DECL in COMDAT, it will set
2276 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2277 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2278 callers defer that decision until it is clear that DECL is actually
2282 import_export_decl (tree decl)
2287 tree class_type = NULL_TREE;
2289 if (DECL_INTERFACE_KNOWN (decl))
2292 /* We cannot determine what linkage to give to an entity with vague
2293 linkage until the end of the file. For example, a virtual table
2294 for a class will be defined if and only if the key method is
2295 defined in this translation unit. As a further example, consider
2296 that when compiling a translation unit that uses PCH file with
2297 "-frepo" it would be incorrect to make decisions about what
2298 entities to emit when building the PCH; those decisions must be
2299 delayed until the repository information has been processed. */
2300 gcc_assert (at_eof);
2301 /* Object file linkage for explicit instantiations is handled in
2302 mark_decl_instantiated. For static variables in functions with
2303 vague linkage, maybe_commonize_var is used.
2305 Therefore, the only declarations that should be provided to this
2306 function are those with external linkage that are:
2308 * implicit instantiations of function templates
2312 * implicit instantiations of static data members of class
2319 Furthermore, all entities that reach this point must have a
2320 definition available in this translation unit.
2322 The following assertions check these conditions. */
2323 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
2324 || TREE_CODE (decl) == VAR_DECL);
2325 /* Any code that creates entities with TREE_PUBLIC cleared should
2326 also set DECL_INTERFACE_KNOWN. */
2327 gcc_assert (TREE_PUBLIC (decl));
2328 if (TREE_CODE (decl) == FUNCTION_DECL)
2329 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2330 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2331 || DECL_DECLARED_INLINE_P (decl));
2333 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2334 || DECL_VTABLE_OR_VTT_P (decl)
2335 || DECL_TINFO_P (decl));
2336 /* Check that a definition of DECL is available in this translation
2338 gcc_assert (!DECL_REALLY_EXTERN (decl));
2340 /* Assume that DECL will not have COMDAT linkage. */
2342 /* Assume that DECL will not be imported into this translation
2346 /* See if the repository tells us whether or not to emit DECL in
2347 this translation unit. */
2348 emit_p = repo_emit_p (decl);
2351 else if (emit_p == 1)
2353 /* The repository indicates that this entity should be defined
2354 here. Make sure the back end honors that request. */
2355 if (TREE_CODE (decl) == VAR_DECL)
2357 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2358 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2361 FOR_EACH_CLONE (clone, decl)
2362 mark_needed (clone);
2366 /* Output the definition as an ordinary strong definition. */
2367 DECL_EXTERNAL (decl) = 0;
2368 DECL_INTERFACE_KNOWN (decl) = 1;
2373 /* We have already decided what to do with this DECL; there is no
2374 need to check anything further. */
2376 else if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
2378 class_type = DECL_CONTEXT (decl);
2379 import_export_class (class_type);
2380 if (TYPE_FOR_JAVA (class_type))
2382 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2383 && CLASSTYPE_INTERFACE_ONLY (class_type))
2385 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2386 && !CLASSTYPE_USE_TEMPLATE (class_type)
2387 && CLASSTYPE_KEY_METHOD (class_type)
2388 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2389 /* The ABI requires that all virtual tables be emitted with
2390 COMDAT linkage. However, on systems where COMDAT symbols
2391 don't show up in the table of contents for a static
2392 archive, or on systems without weak symbols (where we
2393 approximate COMDAT linkage by using internal linkage), the
2394 linker will report errors about undefined symbols because
2395 it will not see the virtual table definition. Therefore,
2396 in the case that we know that the virtual table will be
2397 emitted in only one translation unit, we make the virtual
2398 table an ordinary definition with external linkage. */
2399 DECL_EXTERNAL (decl) = 0;
2400 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2402 /* CLASS_TYPE is being exported from this translation unit,
2403 so DECL should be defined here. */
2404 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2405 /* If a class is declared in a header with the "extern
2406 template" extension, then it will not be instantiated,
2407 even in translation units that would normally require
2408 it. Often such classes are explicitly instantiated in
2409 one translation unit. Therefore, the explicit
2410 instantiation must be made visible to other translation
2412 DECL_EXTERNAL (decl) = 0;
2415 /* The generic C++ ABI says that class data is always
2416 COMDAT, even if there is a key function. Some
2417 variants (e.g., the ARM EABI) says that class data
2418 only has COMDAT linkage if the class data might be
2419 emitted in more than one translation unit. When the
2420 key method can be inline and is inline, we still have
2421 to arrange for comdat even though
2422 class_data_always_comdat is false. */
2423 if (!CLASSTYPE_KEY_METHOD (class_type)
2424 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2425 || targetm.cxx.class_data_always_comdat ())
2427 /* The ABI requires COMDAT linkage. Normally, we
2428 only emit COMDAT things when they are needed;
2429 make sure that we realize that this entity is
2436 else if (!flag_implicit_templates
2437 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2442 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2444 tree type = TREE_TYPE (DECL_NAME (decl));
2445 if (CLASS_TYPE_P (type))
2448 import_export_class (type);
2449 if (CLASSTYPE_INTERFACE_KNOWN (type)
2450 && TYPE_POLYMORPHIC_P (type)
2451 && CLASSTYPE_INTERFACE_ONLY (type)
2452 /* If -fno-rtti was specified, then we cannot be sure
2453 that RTTI information will be emitted with the
2454 virtual table of the class, so we must emit it
2455 wherever it is used. */
2460 if (CLASSTYPE_INTERFACE_KNOWN (type)
2461 && !CLASSTYPE_INTERFACE_ONLY (type))
2463 comdat_p = (targetm.cxx.class_data_always_comdat ()
2464 || (CLASSTYPE_KEY_METHOD (type)
2465 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2470 DECL_EXTERNAL (decl) = 0;
2480 else if (DECL_TEMPLATE_INSTANTIATION (decl)
2481 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2483 /* DECL is an implicit instantiation of a function or static
2485 if ((flag_implicit_templates
2486 && !flag_use_repository)
2487 || (flag_implicit_inline_templates
2488 && TREE_CODE (decl) == FUNCTION_DECL
2489 && DECL_DECLARED_INLINE_P (decl)))
2492 /* If we are not implicitly generating templates, then mark
2493 this entity as undefined in this translation unit. */
2496 else if (DECL_FUNCTION_MEMBER_P (decl))
2498 if (!DECL_DECLARED_INLINE_P (decl))
2500 tree ctype = DECL_CONTEXT (decl);
2501 import_export_class (ctype);
2502 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2504 DECL_NOT_REALLY_EXTERN (decl)
2505 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2506 || (DECL_DECLARED_INLINE_P (decl)
2507 && ! flag_implement_inlines
2508 && !DECL_VINDEX (decl)));
2510 if (!DECL_NOT_REALLY_EXTERN (decl))
2511 DECL_EXTERNAL (decl) = 1;
2513 /* Always make artificials weak. */
2514 if (DECL_ARTIFICIAL (decl) && flag_weak)
2517 maybe_make_one_only (decl);
2528 /* If we are importing DECL into this translation unit, mark is
2529 an undefined here. */
2530 DECL_EXTERNAL (decl) = 1;
2531 DECL_NOT_REALLY_EXTERN (decl) = 0;
2535 /* If we decided to put DECL in COMDAT, mark it accordingly at
2537 comdat_linkage (decl);
2540 DECL_INTERFACE_KNOWN (decl) = 1;
2543 /* Return an expression that performs the destruction of DECL, which
2544 must be a VAR_DECL whose type has a non-trivial destructor, or is
2545 an array whose (innermost) elements have a non-trivial destructor. */
2548 build_cleanup (tree decl)
2551 tree type = TREE_TYPE (decl);
2553 /* This function should only be called for declarations that really
2554 require cleanups. */
2555 gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type));
2557 /* Treat all objects with destructors as used; the destructor may do
2558 something substantive. */
2561 if (TREE_CODE (type) == ARRAY_TYPE)
2564 temp = build_address (decl);
2565 temp = build_delete (TREE_TYPE (temp), temp,
2566 sfk_complete_destructor,
2567 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2571 /* Returns the initialization guard variable for the variable DECL,
2572 which has static storage duration. */
2575 get_guard (tree decl)
2580 sname = mangle_guard_variable (decl);
2581 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2586 /* We use a type that is big enough to contain a mutex as well
2587 as an integer counter. */
2588 guard_type = targetm.cxx.guard_type ();
2589 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2590 VAR_DECL, sname, guard_type);
2592 /* The guard should have the same linkage as what it guards. */
2593 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2594 TREE_STATIC (guard) = TREE_STATIC (decl);
2595 DECL_COMMON (guard) = DECL_COMMON (decl);
2596 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2597 if (DECL_ONE_ONLY (decl))
2598 make_decl_one_only (guard, cxx_comdat_group (guard));
2599 if (TREE_PUBLIC (decl))
2600 DECL_WEAK (guard) = DECL_WEAK (decl);
2601 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2602 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2604 DECL_ARTIFICIAL (guard) = 1;
2605 DECL_IGNORED_P (guard) = 1;
2606 TREE_USED (guard) = 1;
2607 pushdecl_top_level_and_finish (guard, NULL_TREE);
2612 /* Return those bits of the GUARD variable that should be set when the
2613 guarded entity is actually initialized. */
2616 get_guard_bits (tree guard)
2618 if (!targetm.cxx.guard_mask_bit ())
2620 /* We only set the first byte of the guard, in order to leave room
2621 for a mutex in the high-order bits. */
2622 guard = build1 (ADDR_EXPR,
2623 build_pointer_type (TREE_TYPE (guard)),
2625 guard = build1 (NOP_EXPR,
2626 build_pointer_type (char_type_node),
2628 guard = build1 (INDIRECT_REF, char_type_node, guard);
2634 /* Return an expression which determines whether or not the GUARD
2635 variable has already been initialized. */
2638 get_guard_cond (tree guard)
2642 /* Check to see if the GUARD is zero. */
2643 guard = get_guard_bits (guard);
2645 /* Mask off all but the low bit. */
2646 if (targetm.cxx.guard_mask_bit ())
2648 guard_value = integer_one_node;
2649 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2650 guard_value = convert (TREE_TYPE (guard), guard_value);
2651 guard = cp_build_binary_op (input_location,
2652 BIT_AND_EXPR, guard, guard_value,
2653 tf_warning_or_error);
2656 guard_value = integer_zero_node;
2657 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2658 guard_value = convert (TREE_TYPE (guard), guard_value);
2659 return cp_build_binary_op (input_location,
2660 EQ_EXPR, guard, guard_value,
2661 tf_warning_or_error);
2664 /* Return an expression which sets the GUARD variable, indicating that
2665 the variable being guarded has been initialized. */
2668 set_guard (tree guard)
2672 /* Set the GUARD to one. */
2673 guard = get_guard_bits (guard);
2674 guard_init = integer_one_node;
2675 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2676 guard_init = convert (TREE_TYPE (guard), guard_init);
2677 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2678 tf_warning_or_error);
2681 /* Start the process of running a particular set of global constructors
2682 or destructors. Subroutine of do_[cd]tors. */
2685 start_objects (int method_type, int initp)
2691 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2693 if (initp != DEFAULT_INIT_PRIORITY)
2703 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2706 sprintf (type, "%c", method_type);
2708 fndecl = build_lang_decl (FUNCTION_DECL,
2709 get_file_function_name (type),
2710 build_function_type (void_type_node,
2712 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
2714 TREE_PUBLIC (current_function_decl) = 0;
2716 /* Mark as artificial because it's not explicitly in the user's
2718 DECL_ARTIFICIAL (current_function_decl) = 1;
2720 /* Mark this declaration as used to avoid spurious warnings. */
2721 TREE_USED (current_function_decl) = 1;
2723 /* Mark this function as a global constructor or destructor. */
2724 if (method_type == 'I')
2725 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2727 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2729 body = begin_compound_stmt (BCS_FN_BODY);
2734 /* Finish the process of running a particular set of global constructors
2735 or destructors. Subroutine of do_[cd]tors. */
2738 finish_objects (int method_type, int initp, tree body)
2743 finish_compound_stmt (body);
2744 fn = finish_function (0);
2746 if (method_type == 'I')
2748 DECL_STATIC_CONSTRUCTOR (fn) = 1;
2749 decl_init_priority_insert (fn, initp);
2753 DECL_STATIC_DESTRUCTOR (fn) = 1;
2754 decl_fini_priority_insert (fn, initp);
2757 expand_or_defer_fn (fn);
2760 /* The names of the parameters to the function created to handle
2761 initializations and destructions for objects with static storage
2763 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2764 #define PRIORITY_IDENTIFIER "__priority"
2766 /* The name of the function we create to handle initializations and
2767 destructions for objects with static storage duration. */
2768 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2770 /* The declaration for the __INITIALIZE_P argument. */
2771 static GTY(()) tree initialize_p_decl;
2773 /* The declaration for the __PRIORITY argument. */
2774 static GTY(()) tree priority_decl;
2776 /* The declaration for the static storage duration function. */
2777 static GTY(()) tree ssdf_decl;
2779 /* All the static storage duration functions created in this
2780 translation unit. */
2781 static GTY(()) VEC(tree,gc) *ssdf_decls;
2783 /* A map from priority levels to information about that priority
2784 level. There may be many such levels, so efficient lookup is
2786 static splay_tree priority_info_map;
2788 /* Begins the generation of the function that will handle all
2789 initialization and destruction of objects with static storage
2790 duration. The function generated takes two parameters of type
2791 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2792 nonzero, it performs initializations. Otherwise, it performs
2793 destructions. It only performs those initializations or
2794 destructions with the indicated __PRIORITY. The generated function
2797 It is assumed that this function will only be called once per
2798 translation unit. */
2801 start_static_storage_duration_function (unsigned count)
2806 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2808 /* Create the identifier for this function. It will be of the form
2809 SSDF_IDENTIFIER_<number>. */
2810 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
2812 /* Create the parameters. */
2813 parm_types = void_list_node;
2814 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2815 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2816 type = build_function_type (void_type_node, parm_types);
2818 /* Create the FUNCTION_DECL itself. */
2819 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2820 get_identifier (id),
2822 TREE_PUBLIC (ssdf_decl) = 0;
2823 DECL_ARTIFICIAL (ssdf_decl) = 1;
2825 /* Put this function in the list of functions to be called from the
2826 static constructors and destructors. */
2829 ssdf_decls = VEC_alloc (tree, gc, 32);
2831 /* Take this opportunity to initialize the map from priority
2832 numbers to information about that priority level. */
2833 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2834 /*delete_key_fn=*/0,
2835 /*delete_value_fn=*/
2836 (splay_tree_delete_value_fn) &free);
2838 /* We always need to generate functions for the
2839 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2840 priorities later, we'll be sure to find the
2841 DEFAULT_INIT_PRIORITY. */
2842 get_priority_info (DEFAULT_INIT_PRIORITY);
2845 VEC_safe_push (tree, gc, ssdf_decls, ssdf_decl);
2847 /* Create the argument list. */
2848 initialize_p_decl = cp_build_parm_decl
2849 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2850 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2851 TREE_USED (initialize_p_decl) = 1;
2852 priority_decl = cp_build_parm_decl
2853 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2854 DECL_CONTEXT (priority_decl) = ssdf_decl;
2855 TREE_USED (priority_decl) = 1;
2857 TREE_CHAIN (initialize_p_decl) = priority_decl;
2858 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2860 /* Put the function in the global scope. */
2861 pushdecl (ssdf_decl);
2863 /* Start the function itself. This is equivalent to declaring the
2866 static void __ssdf (int __initialize_p, init __priority_p);
2868 It is static because we only need to call this function from the
2869 various constructor and destructor functions for this module. */
2870 start_preparsed_function (ssdf_decl,
2871 /*attrs=*/NULL_TREE,
2874 /* Set up the scope of the outermost block in the function. */
2875 body = begin_compound_stmt (BCS_FN_BODY);
2880 /* Finish the generation of the function which performs initialization
2881 and destruction of objects with static storage duration. After
2882 this point, no more such objects can be created. */
2885 finish_static_storage_duration_function (tree body)
2887 /* Close out the function. */
2888 finish_compound_stmt (body);
2889 expand_or_defer_fn (finish_function (0));
2892 /* Return the information about the indicated PRIORITY level. If no
2893 code to handle this level has yet been generated, generate the
2894 appropriate prologue. */
2896 static priority_info
2897 get_priority_info (int priority)
2902 n = splay_tree_lookup (priority_info_map,
2903 (splay_tree_key) priority);
2906 /* Create a new priority information structure, and insert it
2908 pi = XNEW (struct priority_info_s);
2909 pi->initializations_p = 0;
2910 pi->destructions_p = 0;
2911 splay_tree_insert (priority_info_map,
2912 (splay_tree_key) priority,
2913 (splay_tree_value) pi);
2916 pi = (priority_info) n->value;
2921 /* The effective initialization priority of a DECL. */
2923 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
2924 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
2925 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
2927 /* Whether a DECL needs a guard to protect it against multiple
2930 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
2931 || DECL_ONE_ONLY (decl) \
2932 || DECL_WEAK (decl)))
2934 /* Called from one_static_initialization_or_destruction(),
2936 Walks the initializer list of a global variable and looks for
2937 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
2938 and that have their DECL_CONTEXT() == NULL.
2939 For each such temporary variable, set their DECL_CONTEXT() to
2940 the current function. This is necessary because otherwise
2941 some optimizers (enabled by -O2 -fprofile-arcs) might crash
2942 when trying to refer to a temporary variable that does not have
2943 it's DECL_CONTECT() properly set. */
2945 fix_temporary_vars_context_r (tree *node,
2946 int *unused ATTRIBUTE_UNUSED,
2947 void *unused1 ATTRIBUTE_UNUSED)
2949 gcc_assert (current_function_decl);
2951 if (TREE_CODE (*node) == BIND_EXPR)
2955 for (var = BIND_EXPR_VARS (*node); var; var = TREE_CHAIN (var))
2956 if (TREE_CODE (var) == VAR_DECL
2958 && DECL_ARTIFICIAL (var)
2959 && !DECL_CONTEXT (var))
2960 DECL_CONTEXT (var) = current_function_decl;
2966 /* Set up to handle the initialization or destruction of DECL. If
2967 INITP is nonzero, we are initializing the variable. Otherwise, we
2968 are destroying it. */
2971 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
2973 tree guard_if_stmt = NULL_TREE;
2976 /* If we are supposed to destruct and there's a trivial destructor,
2977 nothing has to be done. */
2979 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2982 /* Trick the compiler into thinking we are at the file and line
2983 where DECL was declared so that error-messages make sense, and so
2984 that the debugger will show somewhat sensible file and line
2986 input_location = DECL_SOURCE_LOCATION (decl);
2988 /* Make sure temporary variables in the initialiser all have
2989 their DECL_CONTEXT() set to a value different from NULL_TREE.
2990 This can happen when global variables initialisers are built.
2991 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
2992 the temporary variables that might have been generated in the
2993 accompagning initialisers is NULL_TREE, meaning the variables have been
2994 declared in the global namespace.
2995 What we want to do here is to fix that and make sure the DECL_CONTEXT()
2996 of the temporaries are set to the current function decl. */
2997 cp_walk_tree_without_duplicates (&init,
2998 fix_temporary_vars_context_r,
3005 Access control for implicit calls to the constructors,
3006 the conversion functions, or the destructor called to
3007 create and destroy a static data member is performed as
3008 if these calls appeared in the scope of the member's
3011 we pretend we are in a static member function of the class of
3012 which the DECL is a member. */
3013 if (member_p (decl))
3015 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3016 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3019 /* Assume we don't need a guard. */
3021 /* We need a guard if this is an object with external linkage that
3022 might be initialized in more than one place. (For example, a
3023 static data member of a template, when the data member requires
3025 if (NEEDS_GUARD_P (decl))
3029 guard = get_guard (decl);
3031 /* When using __cxa_atexit, we just check the GUARD as we would
3032 for a local static. */
3033 if (flag_use_cxa_atexit)
3035 /* When using __cxa_atexit, we never try to destroy
3036 anything from a static destructor. */
3038 guard_cond = get_guard_cond (guard);
3040 /* If we don't have __cxa_atexit, then we will be running
3041 destructors from .fini sections, or their equivalents. So,
3042 we need to know how many times we've tried to initialize this
3043 object. We do initializations only if the GUARD is zero,
3044 i.e., if we are the first to initialize the variable. We do
3045 destructions only if the GUARD is one, i.e., if we are the
3046 last to destroy the variable. */
3049 = cp_build_binary_op (input_location,
3051 cp_build_unary_op (PREINCREMENT_EXPR,
3054 tf_warning_or_error),
3056 tf_warning_or_error);
3059 = cp_build_binary_op (input_location,
3061 cp_build_unary_op (PREDECREMENT_EXPR,
3064 tf_warning_or_error),
3066 tf_warning_or_error);
3068 guard_if_stmt = begin_if_stmt ();
3069 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3073 /* If we're using __cxa_atexit, we have not already set the GUARD,
3074 so we must do so now. */
3075 if (guard && initp && flag_use_cxa_atexit)
3076 finish_expr_stmt (set_guard (guard));
3078 /* Perform the initialization or destruction. */
3082 finish_expr_stmt (init);
3084 /* If we're using __cxa_atexit, register a function that calls the
3085 destructor for the object. */
3086 if (flag_use_cxa_atexit)
3087 finish_expr_stmt (register_dtor_fn (decl));
3090 finish_expr_stmt (build_cleanup (decl));
3092 /* Finish the guard if-stmt, if necessary. */
3095 finish_then_clause (guard_if_stmt);
3096 finish_if_stmt (guard_if_stmt);
3099 /* Now that we're done with DECL we don't need to pretend to be a
3100 member of its class any longer. */
3101 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3102 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3105 /* Generate code to do the initialization or destruction of the decls in VARS,
3106 a TREE_LIST of VAR_DECL with static storage duration.
3107 Whether initialization or destruction is performed is specified by INITP. */
3110 do_static_initialization_or_destruction (tree vars, bool initp)
3112 tree node, init_if_stmt, cond;
3114 /* Build the outer if-stmt to check for initialization or destruction. */
3115 init_if_stmt = begin_if_stmt ();
3116 cond = initp ? integer_one_node : integer_zero_node;
3117 cond = cp_build_binary_op (input_location,
3121 tf_warning_or_error);
3122 finish_if_stmt_cond (cond, init_if_stmt);
3126 tree decl = TREE_VALUE (node);
3127 tree priority_if_stmt;
3131 /* If we don't need a destructor, there's nothing to do. Avoid
3132 creating a possibly empty if-stmt. */
3133 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3135 node = TREE_CHAIN (node);
3139 /* Remember that we had an initialization or finalization at this
3141 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3142 pi = get_priority_info (priority);
3144 pi->initializations_p = 1;
3146 pi->destructions_p = 1;
3148 /* Conditionalize this initialization on being in the right priority
3149 and being initializing/finalizing appropriately. */
3150 priority_if_stmt = begin_if_stmt ();
3151 cond = cp_build_binary_op (input_location,
3154 build_int_cst (NULL_TREE, priority),
3155 tf_warning_or_error);
3156 finish_if_stmt_cond (cond, priority_if_stmt);
3158 /* Process initializers with same priority. */
3160 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3161 node = TREE_CHAIN (node))
3162 /* Do one initialization or destruction. */
3163 one_static_initialization_or_destruction (TREE_VALUE (node),
3164 TREE_PURPOSE (node), initp);
3166 /* Finish up the priority if-stmt body. */
3167 finish_then_clause (priority_if_stmt);
3168 finish_if_stmt (priority_if_stmt);
3172 /* Finish up the init/destruct if-stmt body. */
3173 finish_then_clause (init_if_stmt);
3174 finish_if_stmt (init_if_stmt);
3177 /* VARS is a list of variables with static storage duration which may
3178 need initialization and/or finalization. Remove those variables
3179 that don't really need to be initialized or finalized, and return
3180 the resulting list. The order in which the variables appear in
3181 VARS is in reverse order of the order in which they should actually
3182 be initialized. The list we return is in the unreversed order;
3183 i.e., the first variable should be initialized first. */
3186 prune_vars_needing_no_initialization (tree *vars)
3189 tree result = NULL_TREE;
3194 tree decl = TREE_VALUE (t);
3195 tree init = TREE_PURPOSE (t);
3197 /* Deal gracefully with error. */
3198 if (decl == error_mark_node)
3200 var = &TREE_CHAIN (t);
3204 /* The only things that can be initialized are variables. */
3205 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3207 /* If this object is not defined, we don't need to do anything
3209 if (DECL_EXTERNAL (decl))
3211 var = &TREE_CHAIN (t);
3215 /* Also, if the initializer already contains errors, we can bail
3217 if (init && TREE_CODE (init) == TREE_LIST
3218 && value_member (error_mark_node, init))
3220 var = &TREE_CHAIN (t);
3224 /* This variable is going to need initialization and/or
3225 finalization, so we add it to the list. */
3226 *var = TREE_CHAIN (t);
3227 TREE_CHAIN (t) = result;
3234 /* Make sure we have told the back end about all the variables in
3238 write_out_vars (tree vars)
3242 for (v = vars; v; v = TREE_CHAIN (v))
3244 tree var = TREE_VALUE (v);
3245 if (!var_finalized_p (var))
3247 import_export_decl (var);
3248 rest_of_decl_compilation (var, 1, 1);
3253 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3254 (otherwise) that will initialize all global objects with static
3255 storage duration having the indicated PRIORITY. */
3258 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3267 input_location = *locus;
3269 /* Was: locus->line++; */
3271 /* We use `I' to indicate initialization and `D' to indicate
3273 function_key = constructor_p ? 'I' : 'D';
3275 /* We emit the function lazily, to avoid generating empty
3276 global constructors and destructors. */
3279 /* For Objective-C++, we may need to initialize metadata found in this module.
3280 This must be done _before_ any other static initializations. */
3281 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3282 && constructor_p && objc_static_init_needed_p ())
3284 body = start_objects (function_key, priority);
3285 objc_generate_static_init_call (NULL_TREE);
3288 /* Call the static storage duration function with appropriate
3290 for (i = 0; VEC_iterate (tree, ssdf_decls, i, fndecl); ++i)
3292 /* Calls to pure or const functions will expand to nothing. */
3293 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3296 body = start_objects (function_key, priority);
3298 arguments = tree_cons (NULL_TREE,
3299 build_int_cst (NULL_TREE, priority),
3301 arguments = tree_cons (NULL_TREE,
3302 build_int_cst (NULL_TREE, constructor_p),
3304 finish_expr_stmt (cp_build_function_call (fndecl, arguments,
3305 tf_warning_or_error));
3309 /* Close out the function. */
3311 finish_objects (function_key, priority, body);
3314 /* Generate constructor and destructor functions for the priority
3318 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3320 location_t *locus = (location_t *) data;
3321 int priority = (int) n->key;
3322 priority_info pi = (priority_info) n->value;
3324 /* Generate the functions themselves, but only if they are really
3326 if (pi->initializations_p)
3327 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3328 if (pi->destructions_p)
3329 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3331 /* Keep iterating. */
3335 /* Called via LANGHOOK_CALLGRAPH_ANALYZE_EXPR. It is supposed to mark
3336 decls referenced from front-end specific constructs; it will be called
3337 only for language-specific tree nodes.
3339 Here we must deal with member pointers. */
3342 cxx_callgraph_analyze_expr (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED)
3346 switch (TREE_CODE (t))
3349 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3350 cgraph_mark_address_taken_node (cgraph_node (PTRMEM_CST_MEMBER (t)));
3353 if (TREE_CODE (BASELINK_FUNCTIONS (t)) == FUNCTION_DECL)
3354 cgraph_mark_address_taken_node (cgraph_node (BASELINK_FUNCTIONS (t)));
3357 if (DECL_VTABLE_OR_VTT_P (t))
3359 /* The ABI requires that all virtual tables be emitted
3360 whenever one of them is. */
3362 for (vtbl = CLASSTYPE_VTABLES (DECL_CONTEXT (t));
3364 vtbl = TREE_CHAIN (vtbl))
3365 mark_decl_referenced (vtbl);
3367 else if (DECL_CONTEXT (t)
3368 && flag_use_repository
3369 && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
3370 /* If we need a static variable in a function, then we
3371 need the containing function. */
3372 mark_decl_referenced (DECL_CONTEXT (t));
3381 /* Java requires that we be able to reference a local address for a
3382 method, and not be confused by PLT entries. If hidden aliases are
3383 supported, collect and return all the functions for which we should
3384 emit a hidden alias. */
3386 static struct pointer_set_t *
3387 collect_candidates_for_java_method_aliases (void)
3389 struct cgraph_node *node;
3390 struct pointer_set_t *candidates = NULL;
3392 #ifndef HAVE_GAS_HIDDEN
3396 for (node = cgraph_nodes; node ; node = node->next)
3398 tree fndecl = node->decl;
3400 if (DECL_CONTEXT (fndecl)
3401 && TYPE_P (DECL_CONTEXT (fndecl))
3402 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3403 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3405 if (candidates == NULL)
3406 candidates = pointer_set_create ();
3407 pointer_set_insert (candidates, fndecl);
3415 /* Java requires that we be able to reference a local address for a
3416 method, and not be confused by PLT entries. If hidden aliases are
3417 supported, emit one for each java function that we've emitted.
3418 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3419 by collect_candidates_for_java_method_aliases. */
3422 build_java_method_aliases (struct pointer_set_t *candidates)
3424 struct cgraph_node *node;
3426 #ifndef HAVE_GAS_HIDDEN
3430 for (node = cgraph_nodes; node ; node = node->next)
3432 tree fndecl = node->decl;
3434 if (TREE_ASM_WRITTEN (fndecl)
3435 && pointer_set_contains (candidates, fndecl))
3437 /* Mangle the name in a predictable way; we need to reference
3438 this from a java compiled object file. */
3439 tree oid, nid, alias;
3443 oid = DECL_ASSEMBLER_NAME (fndecl);
3444 oname = IDENTIFIER_POINTER (oid);
3445 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3446 nname = ACONCAT (("_ZGA", oname+2, NULL));
3447 nid = get_identifier (nname);
3449 alias = make_alias_for (fndecl, nid);
3450 TREE_PUBLIC (alias) = 1;
3451 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3453 assemble_alias (alias, oid);
3458 /* Returns true iff there is a definition available for variable or
3462 decl_defined_p (tree decl)
3464 if (TREE_CODE (decl) == FUNCTION_DECL)
3465 return (DECL_INITIAL (decl) != NULL_TREE);
3468 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3469 return !DECL_EXTERNAL (decl);
3473 /* Complain that DECL uses a type with no linkage but is never defined. */
3476 no_linkage_error (tree decl)
3478 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
3479 if (TYPE_ANONYMOUS_P (t))
3481 permerror (0, "%q+#D, declared using anonymous type, "
3482 "is used but never defined", decl);
3483 if (is_typedef_decl (TYPE_NAME (t)))
3484 permerror (0, "%q+#D does not refer to the unqualified type, "
3485 "so it is not used for linkage", TYPE_NAME (t));
3488 permerror (0, "%q+#D, declared using local type %qT, "
3489 "is used but never defined", decl, t);
3492 /* This routine is called at the end of compilation.
3493 Its job is to create all the code needed to initialize and
3494 destroy the global aggregates. We do the destruction
3495 first, since that way we only need to reverse the decls once. */
3498 cp_write_global_declarations (void)
3504 unsigned ssdf_count = 0;
3507 struct pointer_set_t *candidates;
3509 locus = input_location;
3512 /* Bad parse errors. Just forget about it. */
3513 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3517 c_common_write_pch ();
3519 /* FIXME - huh? was input_line -= 1;*/
3521 /* We now have to write out all the stuff we put off writing out.
3524 o Template specializations that we have not yet instantiated,
3525 but which are needed.
3526 o Initialization and destruction for non-local objects with
3527 static storage duration. (Local objects with static storage
3528 duration are initialized when their scope is first entered,
3529 and are cleaned up via atexit.)
3530 o Virtual function tables.
3532 All of these may cause others to be needed. For example,
3533 instantiating one function may cause another to be needed, and
3534 generating the initializer for an object may cause templates to be
3535 instantiated, etc., etc. */
3537 timevar_push (TV_VARCONST);
3539 emit_support_tinfos ();
3548 /* If there are templates that we've put off instantiating, do
3550 instantiate_pending_templates (retries);
3553 /* Write out virtual tables as required. Note that writing out