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 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
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 extern cpp_reader *parse_in;
57 /* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59 typedef struct priority_info_s {
60 /* Nonzero if there have been any initializations at this priority
61 throughout the translation unit. */
62 int initializations_p;
63 /* Nonzero if there have been any destructions at this priority
64 throughout the translation unit. */
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static bool acceptable_java_type (tree);
71 static tree start_objects (int, int);
72 static void finish_objects (int, int, tree);
73 static tree start_static_storage_duration_function (unsigned);
74 static void finish_static_storage_duration_function (tree);
75 static priority_info get_priority_info (int);
76 static void do_static_initialization_or_destruction (tree, bool);
77 static void one_static_initialization_or_destruction (tree, tree, bool);
78 static void generate_ctor_or_dtor_function (bool, int, location_t *);
79 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
81 static tree prune_vars_needing_no_initialization (tree *);
82 static void write_out_vars (tree);
83 static void import_export_class (tree);
84 static tree get_guard_bits (tree);
85 static void determine_visibility_from_class (tree, tree);
87 /* A list of static class variables. This is needed, because a
88 static class variable can be declared inside the class without
89 an initializer, and then initialized, statically, outside the class. */
90 static GTY(()) VEC(tree,gc) *pending_statics;
92 /* A list of functions which were declared inline, but which we
93 may need to emit outline anyway. */
94 static GTY(()) VEC(tree,gc) *deferred_fns;
96 /* Nonzero if we're done parsing and into end-of-file activities. */
100 /* Functions called along with real static constructors and destructors. */
106 /* Incorporate `const' and `volatile' qualifiers for member functions.
107 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
108 QUALS is a list of qualifiers. Returns any explicit
109 top-level qualifiers of the method's this pointer, anything other than
110 TYPE_UNQUALIFIED will be an extension. */
113 grok_method_quals (tree ctype, tree function, cp_cv_quals quals)
115 tree fntype = TREE_TYPE (function);
116 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
117 int type_quals = TYPE_UNQUALIFIED;
118 int this_quals = TYPE_UNQUALIFIED;
120 type_quals = quals & ~TYPE_QUAL_RESTRICT;
121 this_quals = quals & TYPE_QUAL_RESTRICT;
123 ctype = cp_build_qualified_type (ctype, type_quals);
124 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
125 (TREE_CODE (fntype) == METHOD_TYPE
126 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
127 : TYPE_ARG_TYPES (fntype)));
129 fntype = build_exception_variant (fntype, raises);
131 TREE_TYPE (function) = fntype;
135 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
139 cp_build_parm_decl (tree name, tree type)
141 tree parm = build_decl (PARM_DECL, name, type);
142 /* DECL_ARG_TYPE is only used by the back end and the back end never
144 if (!processing_template_decl)
145 DECL_ARG_TYPE (parm) = type_passed_as (type);
149 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
153 build_artificial_parm (tree name, tree type)
155 tree parm = cp_build_parm_decl (name, type);
156 DECL_ARTIFICIAL (parm) = 1;
157 /* All our artificial parms are implicitly `const'; they cannot be
159 TREE_READONLY (parm) = 1;
163 /* Constructors for types with virtual baseclasses need an "in-charge" flag
164 saying whether this constructor is responsible for initialization of
165 virtual baseclasses or not. All destructors also need this "in-charge"
166 flag, which additionally determines whether or not the destructor should
167 free the memory for the object.
169 This function adds the "in-charge" flag to member function FN if
170 appropriate. It is called from grokclassfn and tsubst.
171 FN must be either a constructor or destructor.
173 The in-charge flag follows the 'this' parameter, and is followed by the
174 VTT parm (if any), then the user-written parms. */
177 maybe_retrofit_in_chrg (tree fn)
179 tree basetype, arg_types, parms, parm, fntype;
181 /* If we've already add the in-charge parameter don't do it again. */
182 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
185 /* When processing templates we can't know, in general, whether or
186 not we're going to have virtual baseclasses. */
187 if (processing_template_decl)
190 /* We don't need an in-charge parameter for constructors that don't
191 have virtual bases. */
192 if (DECL_CONSTRUCTOR_P (fn)
193 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
196 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
197 basetype = TREE_TYPE (TREE_VALUE (arg_types));
198 arg_types = TREE_CHAIN (arg_types);
200 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
202 /* If this is a subobject constructor or destructor, our caller will
203 pass us a pointer to our VTT. */
204 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
206 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
208 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
209 TREE_CHAIN (parm) = parms;
212 /* ...and then to TYPE_ARG_TYPES. */
213 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
215 DECL_HAS_VTT_PARM_P (fn) = 1;
218 /* Then add the in-charge parm (before the VTT parm). */
219 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
220 TREE_CHAIN (parm) = parms;
222 arg_types = hash_tree_chain (integer_type_node, arg_types);
224 /* Insert our new parameter(s) into the list. */
225 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
227 /* And rebuild the function type. */
228 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
230 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
231 fntype = build_exception_variant (fntype,
232 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
233 TREE_TYPE (fn) = fntype;
235 /* Now we've got the in-charge parameter. */
236 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
239 /* Classes overload their constituent function names automatically.
240 When a function name is declared in a record structure,
241 its name is changed to it overloaded name. Since names for
242 constructors and destructors can conflict, we place a leading
245 CNAME is the name of the class we are grokking for.
247 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
249 FLAGS contains bits saying what's special about today's
250 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
252 If FUNCTION is a destructor, then we must add the `auto-delete' field
253 as a second parameter. There is some hair associated with the fact
254 that we must "declare" this variable in the manner consistent with the
255 way the rest of the arguments were declared.
257 QUALS are the qualifiers for the this pointer. */
260 grokclassfn (tree ctype, tree function, enum overload_flags flags,
263 tree fn_name = DECL_NAME (function);
264 cp_cv_quals this_quals = TYPE_UNQUALIFIED;
266 /* Even within an `extern "C"' block, members get C++ linkage. See
267 [dcl.link] for details. */
268 SET_DECL_LANGUAGE (function, lang_cplusplus);
270 if (fn_name == NULL_TREE)
272 error ("name missing for member function");
273 fn_name = get_identifier ("<anonymous>");
274 DECL_NAME (function) = fn_name;
278 this_quals = grok_method_quals (ctype, function, quals);
280 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
282 /* Must add the class instance variable up front. */
283 /* Right now we just make this a pointer. But later
284 we may wish to make it special. */
285 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
289 /* The `this' parameter is implicitly `const'; it cannot be
291 this_quals |= TYPE_QUAL_CONST;
292 qual_type = cp_build_qualified_type (type, this_quals);
293 parm = build_artificial_parm (this_identifier, qual_type);
294 cp_apply_type_quals_to_decl (this_quals, parm);
295 TREE_CHAIN (parm) = DECL_ARGUMENTS (function);
296 DECL_ARGUMENTS (function) = parm;
299 DECL_CONTEXT (function) = ctype;
301 if (flags == DTOR_FLAG)
302 DECL_DESTRUCTOR_P (function) = 1;
304 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
305 maybe_retrofit_in_chrg (function);
308 /* Create an ARRAY_REF, checking for the user doing things backwards
312 grok_array_decl (tree array_expr, tree index_exp)
316 tree orig_array_expr = array_expr;
317 tree orig_index_exp = index_exp;
319 if (error_operand_p (array_expr) || error_operand_p (index_exp))
320 return error_mark_node;
322 if (processing_template_decl)
324 if (type_dependent_expression_p (array_expr)
325 || type_dependent_expression_p (index_exp))
326 return build_min_nt (ARRAY_REF, array_expr, index_exp,
327 NULL_TREE, NULL_TREE);
328 array_expr = build_non_dependent_expr (array_expr);
329 index_exp = build_non_dependent_expr (index_exp);
332 type = TREE_TYPE (array_expr);
334 type = non_reference (type);
336 /* If they have an `operator[]', use that. */
337 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
338 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL,
339 array_expr, index_exp, NULL_TREE,
340 /*overloaded_p=*/NULL);
345 /* Otherwise, create an ARRAY_REF for a pointer or array type.
346 It is a little-known fact that, if `a' is an array and `i' is
347 an int, you can write `i[a]', which means the same thing as
349 if (TREE_CODE (type) == ARRAY_TYPE)
352 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
354 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
357 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
359 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
361 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
364 if ((p1 && i2) && (i1 && p2))
365 error ("ambiguous conversion for array subscript");
368 array_expr = p1, index_exp = i2;
370 array_expr = p2, index_exp = i1;
373 error ("invalid types %<%T[%T]%> for array subscript",
374 type, TREE_TYPE (index_exp));
375 return error_mark_node;
378 if (array_expr == error_mark_node || index_exp == error_mark_node)
379 error ("ambiguous conversion for array subscript");
381 expr = build_array_ref (array_expr, index_exp);
383 if (processing_template_decl && expr != error_mark_node)
384 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
385 NULL_TREE, NULL_TREE);
389 /* Given the cast expression EXP, checking out its validity. Either return
390 an error_mark_node if there was an unavoidable error, return a cast to
391 void for trying to delete a pointer w/ the value 0, or return the
392 call to delete. If DOING_VEC is true, we handle things differently
393 for doing an array delete.
394 Implements ARM $5.3.4. This is called from the parser. */
397 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete)
401 if (exp == error_mark_node)
404 if (processing_template_decl)
406 t = build_min (DELETE_EXPR, void_type_node, exp, size);
407 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
408 DELETE_EXPR_USE_VEC (t) = doing_vec;
409 TREE_SIDE_EFFECTS (t) = 1;
413 /* An array can't have been allocated by new, so complain. */
414 if (TREE_CODE (exp) == VAR_DECL
415 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
416 warning (0, "deleting array %q#D", exp);
418 t = build_expr_type_conversion (WANT_POINTER, exp, true);
420 if (t == NULL_TREE || t == error_mark_node)
422 error ("type %q#T argument given to %<delete%>, expected pointer",
424 return error_mark_node;
427 type = TREE_TYPE (t);
429 /* As of Valley Forge, you can delete a pointer to const. */
431 /* You can't delete functions. */
432 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
434 error ("cannot delete a function. Only pointer-to-objects are "
435 "valid arguments to %<delete%>");
436 return error_mark_node;
439 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
440 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
442 warning (0, "deleting %qT is undefined", type);
446 /* Deleting a pointer with the value zero is valid and has no effect. */
447 if (integer_zerop (t))
448 return build1 (NOP_EXPR, void_type_node, t);
451 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
452 sfk_deleting_destructor,
455 return build_delete (type, t, sfk_deleting_destructor,
456 LOOKUP_NORMAL, use_global_delete);
459 /* Report an error if the indicated template declaration is not the
460 sort of thing that should be a member template. */
463 check_member_template (tree tmpl)
467 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
468 decl = DECL_TEMPLATE_RESULT (tmpl);
470 if (TREE_CODE (decl) == FUNCTION_DECL
471 || (TREE_CODE (decl) == TYPE_DECL
472 && IS_AGGR_TYPE (TREE_TYPE (decl))))
474 if (current_function_decl)
475 /* 14.5.2.2 [temp.mem]
477 A local class shall not have member templates. */
478 error ("invalid declaration of member template %q#D in local class",
481 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
483 /* 14.5.2.3 [temp.mem]
485 A member function template shall not be virtual. */
487 ("invalid use of %<virtual%> in template declaration of %q#D",
489 DECL_VIRTUAL_P (decl) = 0;
492 /* The debug-information generating code doesn't know what to do
493 with member templates. */
494 DECL_IGNORED_P (tmpl) = 1;
497 error ("template declaration of %q#D", decl);
500 /* Return true iff TYPE is a valid Java parameter or return type. */
503 acceptable_java_type (tree type)
505 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
507 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
509 type = TREE_TYPE (type);
510 if (TREE_CODE (type) == RECORD_TYPE)
513 if (! TYPE_FOR_JAVA (type))
515 if (! CLASSTYPE_TEMPLATE_INFO (type))
517 args = CLASSTYPE_TI_ARGS (type);
518 i = TREE_VEC_LENGTH (args);
521 type = TREE_VEC_ELT (args, i);
522 if (TREE_CODE (type) == POINTER_TYPE)
523 type = TREE_TYPE (type);
524 if (! TYPE_FOR_JAVA (type))
533 /* For a METHOD in a Java class CTYPE, return true if
534 the parameter and return types are valid Java types.
535 Otherwise, print appropriate error messages, and return false. */
538 check_java_method (tree method)
541 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
542 tree ret_type = TREE_TYPE (TREE_TYPE (method));
544 if (!acceptable_java_type (ret_type))
546 error ("Java method %qD has non-Java return type %qT",
551 arg_types = TREE_CHAIN (arg_types);
552 if (DECL_HAS_IN_CHARGE_PARM_P (method))
553 arg_types = TREE_CHAIN (arg_types);
554 if (DECL_HAS_VTT_PARM_P (method))
555 arg_types = TREE_CHAIN (arg_types);
557 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
559 tree type = TREE_VALUE (arg_types);
560 if (!acceptable_java_type (type))
562 error ("Java method %qD has non-Java parameter type %qT",
570 /* Sanity check: report error if this function FUNCTION is not
571 really a member of the class (CTYPE) it is supposed to belong to.
572 TEMPLATE_PARMS is used to specify the template parameters of a member
573 template passed as FUNCTION_DECL. If the member template is passed as a
574 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
575 from the declaration. If the function is not a function template, it
577 It returns the original declaration for the function, or NULL_TREE
578 if no declaration was found (and an error was emitted). */
581 check_classfn (tree ctype, tree function, tree template_parms)
586 if (DECL_USE_TEMPLATE (function)
587 && !(TREE_CODE (function) == TEMPLATE_DECL
588 && DECL_TEMPLATE_SPECIALIZATION (function))
589 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
590 /* Since this is a specialization of a member template,
591 we're not going to find the declaration in the class.
594 struct S { template <typename T> void f(T); };
595 template <> void S::f(int);
597 we're not going to find `S::f(int)', but there's no
598 reason we should, either. We let our callers know we didn't
599 find the method, but we don't complain. */
602 /* Basic sanity check: for a template function, the template parameters
603 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
604 if (TREE_CODE (function) == TEMPLATE_DECL)
606 gcc_assert (!template_parms
607 || comp_template_parms (template_parms,
608 DECL_TEMPLATE_PARMS (function)));
609 template_parms = DECL_TEMPLATE_PARMS (function);
612 /* OK, is this a definition of a member template? */
613 is_template = (template_parms != NULL_TREE);
615 ix = class_method_index_for_fn (complete_type (ctype), function);
618 VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (ctype);
619 tree fndecls, fndecl = 0;
622 const char *format = NULL;
624 pushed_scope = push_scope (ctype);
625 for (fndecls = VEC_index (tree, methods, ix);
626 fndecls; fndecls = OVL_NEXT (fndecls))
630 fndecl = OVL_CURRENT (fndecls);
631 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
632 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
634 /* We cannot simply call decls_match because this doesn't
635 work for static member functions that are pretending to
636 be methods, and because the name may have been changed by
639 /* Get rid of the this parameter on functions that become
641 if (DECL_STATIC_FUNCTION_P (fndecl)
642 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
643 p1 = TREE_CHAIN (p1);
645 /* A member template definition only matches a member template
647 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
650 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
651 TREE_TYPE (TREE_TYPE (fndecl)))
652 && compparms (p1, p2)
654 || comp_template_parms (template_parms,
655 DECL_TEMPLATE_PARMS (fndecl)))
656 && (DECL_TEMPLATE_SPECIALIZATION (function)
657 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
658 && (!DECL_TEMPLATE_SPECIALIZATION (function)
659 || (DECL_TI_TEMPLATE (function)
660 == DECL_TI_TEMPLATE (fndecl))))
664 pop_scope (pushed_scope);
666 return OVL_CURRENT (fndecls);
667 error ("prototype for %q#D does not match any in class %qT",
669 is_conv_op = DECL_CONV_FN_P (fndecl);
672 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
673 fndecls = VEC_index (tree, methods, ix);
676 fndecl = OVL_CURRENT (fndecls);
677 fndecls = OVL_NEXT (fndecls);
679 if (!fndecls && is_conv_op)
681 if (VEC_length (tree, methods) > (size_t) ++ix)
683 fndecls = VEC_index (tree, methods, ix);
684 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
696 format = N_("candidates are: %+#D");
698 format = N_("candidate is: %+#D");
699 error (format, fndecl);
702 else if (!COMPLETE_TYPE_P (ctype))
703 cxx_incomplete_type_error (function, ctype);
705 error ("no %q#D member function declared in class %qT",
708 /* If we did not find the method in the class, add it to avoid
709 spurious errors (unless the CTYPE is not yet defined, in which
710 case we'll only confuse ourselves when the function is declared
711 properly within the class. */
712 if (COMPLETE_TYPE_P (ctype))
713 add_method (ctype, function, NULL_TREE);
717 /* DECL is a function with vague linkage. Remember it so that at the
718 end of the translation unit we can decide whether or not to emit
722 note_vague_linkage_fn (tree decl)
724 if (!DECL_DEFERRED_FN (decl))
726 DECL_DEFERRED_FN (decl) = 1;
727 DECL_DEFER_OUTPUT (decl) = 1;
728 VEC_safe_push (tree, gc, deferred_fns, decl);
732 /* Like note_vague_linkage_fn but for variables. */
735 note_vague_linkage_var (tree var)
737 VEC_safe_push (tree, gc, pending_statics, var);
740 /* We have just processed the DECL, which is a static data member.
741 The other parameters are as for cp_finish_decl. */
744 finish_static_data_member_decl (tree decl,
745 tree init, bool init_const_expr_p,
749 gcc_assert (TREE_PUBLIC (decl));
751 DECL_CONTEXT (decl) = current_class_type;
753 /* We cannot call pushdecl here, because that would fill in the
754 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
755 the right thing, namely, to put this decl out straight away. */
756 /* current_class_type can be NULL_TREE in case of error. */
757 if (!asmspec_tree && current_class_type)
758 DECL_INITIAL (decl) = error_mark_node;
760 if (! processing_template_decl)
761 note_vague_linkage_var (decl);
763 if (LOCAL_CLASS_P (current_class_type))
764 pedwarn ("local class %q#T shall not have static data member %q#D",
765 current_class_type, decl);
767 /* Static consts need not be initialized in the class definition. */
768 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
770 static int explained = 0;
772 error ("initializer invalid for static member with constructor");
775 error ("(an out of class initialization is required)");
780 /* Force the compiler to know when an uninitialized static const
781 member is being used. */
782 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
783 TREE_USED (decl) = 1;
784 DECL_INITIAL (decl) = init;
785 DECL_IN_AGGR_P (decl) = 1;
787 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
790 /* DECLARATOR and DECLSPECS correspond to a class member. The othe
791 parameters are as for cp_finish_decl. Return the DECL for the
792 class member declared. */
795 grokfield (const cp_declarator *declarator,
796 cp_decl_specifier_seq *declspecs,
797 tree init, bool init_const_expr_p,
802 const char *asmspec = 0;
803 int flags = LOOKUP_ONLYCONVERTING;
805 if (!declspecs->any_specifiers_p
806 && declarator->kind == cdk_id
807 && declarator->u.id.qualifying_scope
808 && TREE_CODE (declarator->u.id.unqualified_name) == IDENTIFIER_NODE)
809 /* Access declaration */
810 return do_class_using_decl (declarator->u.id.qualifying_scope,
811 declarator->u.id.unqualified_name);
814 && TREE_CODE (init) == TREE_LIST
815 && TREE_VALUE (init) == error_mark_node
816 && TREE_CHAIN (init) == NULL_TREE)
819 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
820 if (! value || error_operand_p (value))
821 /* friend or constructor went bad. */
822 return error_mark_node;
824 if (TREE_CODE (value) == TYPE_DECL && init)
826 error ("typedef %qD is initialized (use __typeof__ instead)", value);
830 /* Pass friendly classes back. */
831 if (value == void_type_node)
834 /* Pass friend decls back. */
835 if ((TREE_CODE (value) == FUNCTION_DECL
836 || TREE_CODE (value) == TEMPLATE_DECL)
837 && DECL_CONTEXT (value) != current_class_type)
840 if (DECL_NAME (value) != NULL_TREE
841 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
842 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
843 error ("member %qD conflicts with virtual function table field name",
846 /* Stash away type declarations. */
847 if (TREE_CODE (value) == TYPE_DECL)
849 DECL_NONLOCAL (value) = 1;
850 DECL_CONTEXT (value) = current_class_type;
852 if (processing_template_decl)
853 value = push_template_decl (value);
857 /* Avoid storing attributes in template parameters:
858 tsubst is not ready to handle them. */
859 tree type = TREE_TYPE (value);
860 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
861 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
862 sorry ("applying attributes to template parameters is not implemented");
864 cplus_decl_attributes (&value, attrlist, 0);
870 if (DECL_IN_AGGR_P (value))
872 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
873 return void_type_node;
877 asmspec = TREE_STRING_POINTER (asmspec_tree);
881 if (TREE_CODE (value) == FUNCTION_DECL)
883 /* Initializers for functions are rejected early in the parser.
884 If we get here, it must be a pure specifier for a method. */
885 if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
887 gcc_assert (error_operand_p (init) || integer_zerop (init));
888 DECL_PURE_VIRTUAL_P (value) = 1;
892 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
893 error ("initializer specified for static member function %qD",
897 else if (pedantic && TREE_CODE (value) != VAR_DECL)
898 /* Already complained in grokdeclarator. */
900 else if (!processing_template_decl)
902 if (TREE_CODE (init) == CONSTRUCTOR)
903 init = digest_init (TREE_TYPE (value), init);
905 init = integral_constant_value (init);
907 if (init != error_mark_node && !TREE_CONSTANT (init))
909 /* We can allow references to things that are effectively
910 static, since references are initialized with the
912 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
913 || (TREE_STATIC (init) == 0
914 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
916 error ("field initializer is not constant");
917 init = error_mark_node;
923 if (processing_template_decl
924 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
926 value = push_template_decl (value);
927 if (error_operand_p (value))
928 return error_mark_node;
932 cplus_decl_attributes (&value, attrlist, 0);
934 switch (TREE_CODE (value))
937 finish_static_data_member_decl (value, init, init_const_expr_p,
938 asmspec_tree, flags);
943 error ("%<asm%> specifiers are not permitted on non-static data members");
944 if (DECL_INITIAL (value) == error_mark_node)
945 init = error_mark_node;
946 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
948 DECL_INITIAL (value) = init;
949 DECL_IN_AGGR_P (value) = 1;
954 set_user_assembler_name (value, asmspec);
955 if (!DECL_FRIEND_P (value))
956 grok_special_member_properties (value);
958 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
959 asmspec_tree, flags);
961 /* Pass friends back this way. */
962 if (DECL_FRIEND_P (value))
963 return void_type_node;
965 DECL_IN_AGGR_P (value) = 1;
974 /* Like `grokfield', but for bitfields.
975 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
978 grokbitfield (const cp_declarator *declarator,
979 cp_decl_specifier_seq *declspecs, tree width)
981 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, NULL);
983 if (! value) return NULL_TREE; /* friends went bad. */
985 /* Pass friendly classes back. */
986 if (TREE_CODE (value) == VOID_TYPE)
987 return void_type_node;
989 if (TREE_CODE (value) == TYPE_DECL)
991 error ("cannot declare %qD to be a bit-field type", value);
995 /* Usually, finish_struct_1 catches bitfields with invalid types.
996 But, in the case of bitfields with function type, we confuse
997 ourselves into thinking they are member functions, so we must
999 if (TREE_CODE (value) == FUNCTION_DECL)
1001 error ("cannot declare bit-field %qD with function type",
1006 if (DECL_IN_AGGR_P (value))
1008 error ("%qD is already defined in the class %qT", value,
1009 DECL_CONTEXT (value));
1010 return void_type_node;
1013 if (TREE_STATIC (value))
1015 error ("static member %qD cannot be a bit-field", value);
1018 finish_decl (value, NULL_TREE, NULL_TREE);
1020 if (width != error_mark_node)
1022 constant_expression_warning (width);
1023 DECL_INITIAL (value) = width;
1024 SET_DECL_C_BIT_FIELD (value);
1027 DECL_IN_AGGR_P (value) = 1;
1033 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1035 if (*decl == NULL_TREE || *decl == void_type_node)
1038 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1039 decl = &DECL_TEMPLATE_RESULT (*decl);
1041 decl_attributes (decl, attributes, flags);
1043 if (TREE_CODE (*decl) == TYPE_DECL)
1044 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1047 /* Walks through the namespace- or function-scope anonymous union
1048 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1049 Returns one of the fields for use in the mangled name. */
1052 build_anon_union_vars (tree type, tree object)
1054 tree main_decl = NULL_TREE;
1057 /* Rather than write the code to handle the non-union case,
1058 just give an error. */
1059 if (TREE_CODE (type) != UNION_TYPE)
1060 error ("anonymous struct not inside named type");
1062 for (field = TYPE_FIELDS (type);
1064 field = TREE_CHAIN (field))
1069 if (DECL_ARTIFICIAL (field))
1071 if (TREE_CODE (field) != FIELD_DECL)
1073 pedwarn ("%q+#D invalid; an anonymous union can only "
1074 "have non-static data members", field);
1078 if (TREE_PRIVATE (field))
1079 pedwarn ("private member %q+#D in anonymous union", field);
1080 else if (TREE_PROTECTED (field))
1081 pedwarn ("protected member %q+#D in anonymous union", field);
1083 if (processing_template_decl)
1084 ref = build_min_nt (COMPONENT_REF, object,
1085 DECL_NAME (field), NULL_TREE);
1087 ref = build_class_member_access_expr (object, field, NULL_TREE,
1090 if (DECL_NAME (field))
1094 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1096 base = get_base_address (object);
1097 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1098 TREE_STATIC (decl) = TREE_STATIC (base);
1099 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1101 SET_DECL_VALUE_EXPR (decl, ref);
1102 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1104 decl = pushdecl (decl);
1106 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1107 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1111 if (main_decl == NULL_TREE)
1118 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1119 anonymous union, then all members must be laid out together. PUBLIC_P
1120 is nonzero if this union is not declared static. */
1123 finish_anon_union (tree anon_union_decl)
1129 if (anon_union_decl == error_mark_node)
1132 type = TREE_TYPE (anon_union_decl);
1133 public_p = TREE_PUBLIC (anon_union_decl);
1135 /* The VAR_DECL's context is the same as the TYPE's context. */
1136 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1138 if (TYPE_FIELDS (type) == NULL_TREE)
1143 error ("namespace-scope anonymous aggregates must be static");
1147 main_decl = build_anon_union_vars (type, anon_union_decl);
1148 if (main_decl == NULL_TREE)
1150 warning (0, "anonymous union with no members");
1154 if (!processing_template_decl)
1156 /* Use main_decl to set the mangled name. */
1157 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1158 mangle_decl (anon_union_decl);
1159 DECL_NAME (anon_union_decl) = NULL_TREE;
1162 pushdecl (anon_union_decl);
1163 if (building_stmt_tree ()
1164 && at_function_scope_p ())
1165 add_decl_expr (anon_union_decl);
1166 else if (!processing_template_decl)
1167 rest_of_decl_compilation (anon_union_decl,
1168 toplevel_bindings_p (), at_eof);
1171 /* Auxiliary functions to make type signatures for
1172 `operator new' and `operator delete' correspond to
1173 what compiler will be expecting. */
1176 coerce_new_type (tree type)
1179 tree args = TYPE_ARG_TYPES (type);
1181 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1183 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1186 error ("%<operator new%> must return type %qT", ptr_type_node);
1189 if (!args || args == void_list_node
1190 || !same_type_p (TREE_VALUE (args), size_type_node))
1193 if (args && args != void_list_node)
1194 args = TREE_CHAIN (args);
1195 pedwarn ("%<operator new%> takes type %<size_t%> (%qT) "
1196 "as first parameter", size_type_node);
1201 args = tree_cons (NULL_TREE, size_type_node, args);
1204 type = build_exception_variant
1205 (build_function_type (ptr_type_node, args),
1206 TYPE_RAISES_EXCEPTIONS (type));
1214 coerce_delete_type (tree type)
1217 tree args = TYPE_ARG_TYPES (type);
1219 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1221 if (!same_type_p (TREE_TYPE (type), void_type_node))
1224 error ("%<operator delete%> must return type %qT", void_type_node);
1227 if (!args || args == void_list_node
1228 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1231 if (args && args != void_list_node)
1232 args = TREE_CHAIN (args);
1233 error ("%<operator delete%> takes type %qT as first parameter",
1239 args = tree_cons (NULL_TREE, ptr_type_node, args);
1242 type = build_exception_variant
1243 (build_function_type (void_type_node, args),
1244 TYPE_RAISES_EXCEPTIONS (type));
1253 mark_vtable_entries (tree decl)
1256 unsigned HOST_WIDE_INT idx;
1258 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1263 STRIP_NOPS (fnaddr);
1265 if (TREE_CODE (fnaddr) != ADDR_EXPR
1266 && TREE_CODE (fnaddr) != FDESC_EXPR)
1267 /* This entry is an offset: a virtual base class offset, a
1268 virtual call offset, an RTTI offset, etc. */
1271 fn = TREE_OPERAND (fnaddr, 0);
1272 TREE_ADDRESSABLE (fn) = 1;
1273 /* When we don't have vcall offsets, we output thunks whenever
1274 we output the vtables that contain them. With vcall offsets,
1275 we know all the thunks we'll need when we emit a virtual
1276 function, so we emit the thunks there instead. */
1277 if (DECL_THUNK_P (fn))
1278 use_thunk (fn, /*emit_p=*/0);
1283 /* Set DECL up to have the closest approximation of "initialized common"
1284 linkage available. */
1287 comdat_linkage (tree decl)
1290 make_decl_one_only (decl);
1291 else if (TREE_CODE (decl) == FUNCTION_DECL
1292 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1293 /* We can just emit function and compiler-generated variables
1294 statically; having multiple copies is (for the most part) only
1297 There are two correctness issues, however: the address of a
1298 template instantiation with external linkage should be the
1299 same, independent of what translation unit asks for the
1300 address, and this will not hold when we emit multiple copies of
1301 the function. However, there's little else we can do.
1303 Also, by default, the typeinfo implementation assumes that
1304 there will be only one copy of the string used as the name for
1305 each type. Therefore, if weak symbols are unavailable, the
1306 run-time library should perform a more conservative check; it
1307 should perform a string comparison, rather than an address
1309 TREE_PUBLIC (decl) = 0;
1312 /* Static data member template instantiations, however, cannot
1313 have multiple copies. */
1314 if (DECL_INITIAL (decl) == 0
1315 || DECL_INITIAL (decl) == error_mark_node)
1316 DECL_COMMON (decl) = 1;
1317 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1319 DECL_COMMON (decl) = 1;
1320 DECL_INITIAL (decl) = error_mark_node;
1322 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1324 /* We can't do anything useful; leave vars for explicit
1326 DECL_EXTERNAL (decl) = 1;
1327 DECL_NOT_REALLY_EXTERN (decl) = 0;
1331 if (DECL_LANG_SPECIFIC (decl))
1332 DECL_COMDAT (decl) = 1;
1335 /* For win32 we also want to put explicit instantiations in
1336 linkonce sections, so that they will be merged with implicit
1337 instantiations; otherwise we get duplicate symbol errors.
1338 For Darwin we do not want explicit instantiations to be
1342 maybe_make_one_only (tree decl)
1344 /* We used to say that this was not necessary on targets that support weak
1345 symbols, because the implicit instantiations will defer to the explicit
1346 one. However, that's not actually the case in SVR4; a strong definition
1347 after a weak one is an error. Also, not making explicit
1348 instantiations one_only means that we can end up with two copies of
1349 some template instantiations. */
1353 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1354 we can get away with not emitting them if they aren't used. We need
1355 to for variables so that cp_finish_decl will update their linkage,
1356 because their DECL_INITIAL may not have been set properly yet. */
1358 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1359 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1360 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1362 make_decl_one_only (decl);
1364 if (TREE_CODE (decl) == VAR_DECL)
1366 DECL_COMDAT (decl) = 1;
1367 /* Mark it needed so we don't forget to emit it. */
1368 mark_decl_referenced (decl);
1373 /* Determine whether or not we want to specifically import or export CTYPE,
1374 using various heuristics. */
1377 import_export_class (tree ctype)
1379 /* -1 for imported, 1 for exported. */
1380 int import_export = 0;
1382 /* It only makes sense to call this function at EOF. The reason is
1383 that this function looks at whether or not the first non-inline
1384 non-abstract virtual member function has been defined in this
1385 translation unit. But, we can't possibly know that until we've
1386 seen the entire translation unit. */
1387 gcc_assert (at_eof);
1389 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1392 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1393 we will have CLASSTYPE_INTERFACE_ONLY set but not
1394 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1395 heuristic because someone will supply a #pragma implementation
1396 elsewhere, and deducing it here would produce a conflict. */
1397 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1400 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1402 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1404 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1405 && !flag_implicit_templates)
1406 /* For a template class, without -fimplicit-templates, check the
1407 repository. If the virtual table is assigned to this
1408 translation unit, then export the class; otherwise, import
1410 import_export = repo_export_class_p (ctype) ? 1 : -1;
1411 else if (TYPE_POLYMORPHIC_P (ctype))
1413 /* The ABI specifies that the virtual table and associated
1414 information are emitted with the key method, if any. */
1415 tree method = CLASSTYPE_KEY_METHOD (ctype);
1416 /* If weak symbol support is not available, then we must be
1417 careful not to emit the vtable when the key function is
1418 inline. An inline function can be defined in multiple
1419 translation units. If we were to emit the vtable in each
1420 translation unit containing a definition, we would get
1421 multiple definition errors at link-time. */
1422 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1423 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1426 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1427 a definition anywhere else. */
1428 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1431 /* Allow backends the chance to overrule the decision. */
1432 if (targetm.cxx.import_export_class)
1433 import_export = targetm.cxx.import_export_class (ctype, import_export);
1437 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1438 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1442 /* Return true if VAR has already been provided to the back end; in that
1443 case VAR should not be modified further by the front end. */
1445 var_finalized_p (tree var)
1447 return cgraph_varpool_node (var)->finalized;
1450 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1451 must be emitted in this translation unit. Mark it as such. */
1454 mark_needed (tree decl)
1456 /* It's possible that we no longer need to set
1457 TREE_SYMBOL_REFERENCED here directly, but doing so is
1459 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1460 mark_decl_referenced (decl);
1463 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1464 returns true if a definition of this entity should be provided in
1465 this object file. Callers use this function to determine whether
1466 or not to let the back end know that a definition of DECL is
1467 available in this translation unit. */
1470 decl_needed_p (tree decl)
1472 gcc_assert (TREE_CODE (decl) == VAR_DECL
1473 || TREE_CODE (decl) == FUNCTION_DECL);
1474 /* This function should only be called at the end of the translation
1475 unit. We cannot be sure of whether or not something will be
1476 COMDAT until that point. */
1477 gcc_assert (at_eof);
1479 /* All entities with external linkage that are not COMDAT should be
1480 emitted; they may be referred to from other object files. */
1481 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1483 /* If this entity was used, let the back-end see it; it will decide
1484 whether or not to emit it into the object file. */
1485 if (TREE_USED (decl)
1486 || (DECL_ASSEMBLER_NAME_SET_P (decl)
1487 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
1489 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1490 reference to DECL might cause it to be emitted later. */
1494 /* If necessary, write out the vtables for the dynamic class CTYPE.
1495 Returns true if any vtables were emitted. */
1498 maybe_emit_vtables (tree ctype)
1504 /* If the vtables for this class have already been emitted there is
1505 nothing more to do. */
1506 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1507 if (var_finalized_p (primary_vtbl))
1509 /* Ignore dummy vtables made by get_vtable_decl. */
1510 if (TREE_TYPE (primary_vtbl) == void_type_node)
1513 /* On some targets, we cannot determine the key method until the end
1514 of the translation unit -- which is when this function is
1516 if (!targetm.cxx.key_method_may_be_inline ())
1517 determine_key_method (ctype);
1519 /* See if any of the vtables are needed. */
1520 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1522 import_export_decl (vtbl);
1523 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1528 /* If the references to this class' vtables are optimized away,
1529 still emit the appropriate debugging information. See
1531 if (DECL_COMDAT (primary_vtbl)
1532 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1533 note_debug_info_needed (ctype);
1537 /* The ABI requires that we emit all of the vtables if we emit any
1539 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1541 /* Mark entities references from the virtual table as used. */
1542 mark_vtable_entries (vtbl);
1544 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1546 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl));
1548 /* It had better be all done at compile-time. */
1553 DECL_EXTERNAL (vtbl) = 0;
1554 rest_of_decl_compilation (vtbl, 1, 1);
1556 /* Because we're only doing syntax-checking, we'll never end up
1557 actually marking the variable as written. */
1558 if (flag_syntax_only)
1559 TREE_ASM_WRITTEN (vtbl) = 1;
1562 /* Since we're writing out the vtable here, also write the debug
1564 note_debug_info_needed (ctype);
1569 /* Like c_determine_visibility, but with additional C++-specific
1572 Function-scope entities can rely on the function's visibility because
1573 it is set in start_preparsed_function.
1575 Class-scope entities cannot rely on the class's visibility until the end
1576 of the enclosing class definition.
1578 Note that because namespaces have multiple independent definitions,
1579 namespace visibility is handled elsewhere using the #pragma visibility
1580 machinery rather than by decorating the namespace declaration. */
1583 determine_visibility (tree decl)
1587 /* Only relevant for names with external linkage. */
1588 if (!TREE_PUBLIC (decl))
1591 /* Cloned constructors and destructors get the same visibility as
1592 the underlying function. That should be set up in
1593 maybe_clone_body. */
1594 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
1596 /* Give the common code a chance to make a determination. */
1597 if (c_determine_visibility (decl))
1600 /* If DECL is a member of a class, visibility specifiers on the
1601 class can influence the visibility of the DECL. */
1602 if (DECL_CLASS_SCOPE_P (decl))
1603 class_type = DECL_CONTEXT (decl);
1604 else if (TREE_CODE (decl) == VAR_DECL
1605 && DECL_TINFO_P (decl)
1606 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl))))
1607 class_type = TREE_TYPE (DECL_NAME (decl));
1610 /* Virtual tables have DECL_CONTEXT set to their associated class,
1611 so they are automatically handled above. */
1612 gcc_assert (TREE_CODE (decl) != VAR_DECL
1613 || !DECL_VTABLE_OR_VTT_P (decl));
1615 if (DECL_FUNCTION_SCOPE_P (decl))
1617 tree fn = DECL_CONTEXT (decl);
1618 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
1619 DECL_VISIBILITY_SPECIFIED (decl) = DECL_VISIBILITY_SPECIFIED (fn);
1622 /* Entities not associated with any class just get the
1623 visibility specified by their attributes. */
1627 /* By default, static data members and function members receive
1628 the visibility of their containing class. */
1631 determine_visibility_from_class (decl, class_type);
1633 /* Give the target a chance to override the visibility associated
1635 if (TREE_CODE (decl) == VAR_DECL
1636 && (DECL_TINFO_P (decl)
1637 || (DECL_VTABLE_OR_VTT_P (decl)
1638 /* Construction virtual tables are not exported because
1639 they cannot be referred to from other object files;
1640 their name is not standardized by the ABI. */
1641 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
1642 && TREE_PUBLIC (decl)
1643 && !DECL_REALLY_EXTERN (decl)
1644 && DECL_VISIBILITY_SPECIFIED (decl)
1645 && (!class_type || !CLASSTYPE_VISIBILITY_SPECIFIED (class_type)))
1646 targetm.cxx.determine_class_data_visibility (decl);
1651 determine_visibility_from_class (tree decl, tree class_type)
1653 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1654 && lookup_attribute ("dllexport", TYPE_ATTRIBUTES (class_type)))
1656 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1657 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1659 else if (TREE_CODE (decl) == FUNCTION_DECL
1660 && DECL_DECLARED_INLINE_P (decl)
1661 && visibility_options.inlines_hidden)
1663 /* Don't change it if it has been set explicitly by user. */
1664 if (!DECL_VISIBILITY_SPECIFIED (decl))
1666 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1667 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1670 else if (CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
1672 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
1673 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1675 else if (TYPE_CLASS_SCOPE_P (class_type))
1676 determine_visibility_from_class (decl, TYPE_CONTEXT (class_type));
1677 else if (TYPE_FUNCTION_SCOPE_P (class_type))
1679 tree fn = TYPE_CONTEXT (class_type);
1680 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
1681 DECL_VISIBILITY_SPECIFIED (decl) = DECL_VISIBILITY_SPECIFIED (fn);
1683 else if (!DECL_VISIBILITY_SPECIFIED (decl))
1685 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
1686 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1690 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
1691 for DECL has not already been determined, do so now by setting
1692 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
1693 function is called entities with vague linkage whose definitions
1694 are available must have TREE_PUBLIC set.
1696 If this function decides to place DECL in COMDAT, it will set
1697 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
1698 the caller to decide whether or not to clear DECL_EXTERNAL. Some
1699 callers defer that decision until it is clear that DECL is actually
1703 import_export_decl (tree decl)
1708 tree class_type = NULL_TREE;
1710 if (DECL_INTERFACE_KNOWN (decl))
1713 /* We cannot determine what linkage to give to an entity with vague
1714 linkage until the end of the file. For example, a virtual table
1715 for a class will be defined if and only if the key method is
1716 defined in this translation unit. As a further example, consider
1717 that when compiling a translation unit that uses PCH file with
1718 "-frepo" it would be incorrect to make decisions about what
1719 entities to emit when building the PCH; those decisions must be
1720 delayed until the repository information has been processed. */
1721 gcc_assert (at_eof);
1722 /* Object file linkage for explicit instantiations is handled in
1723 mark_decl_instantiated. For static variables in functions with
1724 vague linkage, maybe_commonize_var is used.
1726 Therefore, the only declarations that should be provided to this
1727 function are those with external linkage that are:
1729 * implicit instantiations of function templates
1733 * implicit instantiations of static data members of class
1740 Furthermore, all entities that reach this point must have a
1741 definition available in this translation unit.
1743 The following assertions check these conditions. */
1744 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1745 || TREE_CODE (decl) == VAR_DECL);
1746 /* Any code that creates entities with TREE_PUBLIC cleared should
1747 also set DECL_INTERFACE_KNOWN. */
1748 gcc_assert (TREE_PUBLIC (decl));
1749 if (TREE_CODE (decl) == FUNCTION_DECL)
1750 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
1751 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
1752 || DECL_DECLARED_INLINE_P (decl));
1754 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
1755 || DECL_VTABLE_OR_VTT_P (decl)
1756 || DECL_TINFO_P (decl));
1757 /* Check that a definition of DECL is available in this translation
1759 gcc_assert (!DECL_REALLY_EXTERN (decl));
1761 /* Assume that DECL will not have COMDAT linkage. */
1763 /* Assume that DECL will not be imported into this translation
1767 /* See if the repository tells us whether or not to emit DECL in
1768 this translation unit. */
1769 emit_p = repo_emit_p (decl);
1772 else if (emit_p == 1)
1774 /* The repository indicates that this entity should be defined
1775 here. Make sure the back end honors that request. */
1776 if (TREE_CODE (decl) == VAR_DECL)
1778 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
1779 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
1782 FOR_EACH_CLONE (clone, decl)
1783 mark_needed (clone);
1787 /* Output the definition as an ordinary strong definition. */
1788 DECL_EXTERNAL (decl) = 0;
1789 DECL_INTERFACE_KNOWN (decl) = 1;
1794 /* We have already decided what to do with this DECL; there is no
1795 need to check anything further. */
1797 else if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
1799 class_type = DECL_CONTEXT (decl);
1800 import_export_class (class_type);
1801 if (TYPE_FOR_JAVA (class_type))
1803 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
1804 && CLASSTYPE_INTERFACE_ONLY (class_type))
1806 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
1807 && !CLASSTYPE_USE_TEMPLATE (class_type)
1808 && CLASSTYPE_KEY_METHOD (class_type)
1809 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
1810 /* The ABI requires that all virtual tables be emitted with
1811 COMDAT linkage. However, on systems where COMDAT symbols
1812 don't show up in the table of contents for a static
1813 archive, or on systems without weak symbols (where we
1814 approximate COMDAT linkage by using internal linkage), the
1815 linker will report errors about undefined symbols because
1816 it will not see the virtual table definition. Therefore,
1817 in the case that we know that the virtual table will be
1818 emitted in only one translation unit, we make the virtual
1819 table an ordinary definition with external linkage. */
1820 DECL_EXTERNAL (decl) = 0;
1821 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
1823 /* CLASS_TYPE is being exported from this translation unit,
1824 so DECL should be defined here. */
1825 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
1826 /* If a class is declared in a header with the "extern
1827 template" extension, then it will not be instantiated,
1828 even in translation units that would normally require
1829 it. Often such classes are explicitly instantiated in
1830 one translation unit. Therefore, the explicit
1831 instantiation must be made visible to other translation
1833 DECL_EXTERNAL (decl) = 0;
1836 /* The generic C++ ABI says that class data is always
1837 COMDAT, even if there is a key function. Some
1838 variants (e.g., the ARM EABI) says that class data
1839 only has COMDAT linkage if the class data might be
1840 emitted in more than one translation unit. When the
1841 key method can be inline and is inline, we still have
1842 to arrange for comdat even though
1843 class_data_always_comdat is false. */
1844 if (!CLASSTYPE_KEY_METHOD (class_type)
1845 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
1846 || targetm.cxx.class_data_always_comdat ())
1848 /* The ABI requires COMDAT linkage. Normally, we
1849 only emit COMDAT things when they are needed;
1850 make sure that we realize that this entity is
1857 else if (!flag_implicit_templates
1858 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
1863 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
1865 tree type = TREE_TYPE (DECL_NAME (decl));
1866 if (CLASS_TYPE_P (type))
1869 import_export_class (type);
1870 if (CLASSTYPE_INTERFACE_KNOWN (type)
1871 && TYPE_POLYMORPHIC_P (type)
1872 && CLASSTYPE_INTERFACE_ONLY (type)
1873 /* If -fno-rtti was specified, then we cannot be sure
1874 that RTTI information will be emitted with the
1875 virtual table of the class, so we must emit it
1876 wherever it is used. */
1881 if (CLASSTYPE_INTERFACE_KNOWN (type)
1882 && !CLASSTYPE_INTERFACE_ONLY (type))
1884 comdat_p = (targetm.cxx.class_data_always_comdat ()
1885 || (CLASSTYPE_KEY_METHOD (type)
1886 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
1891 DECL_EXTERNAL (decl) = 0;
1901 else if (DECL_TEMPLATE_INSTANTIATION (decl)
1902 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1904 /* DECL is an implicit instantiation of a function or static
1906 if (flag_implicit_templates
1907 || (flag_implicit_inline_templates
1908 && TREE_CODE (decl) == FUNCTION_DECL
1909 && DECL_DECLARED_INLINE_P (decl)))
1912 /* If we are not implicitly generating templates, then mark
1913 this entity as undefined in this translation unit. */
1916 else if (DECL_FUNCTION_MEMBER_P (decl))
1918 if (!DECL_DECLARED_INLINE_P (decl))
1920 tree ctype = DECL_CONTEXT (decl);
1921 import_export_class (ctype);
1922 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1924 DECL_NOT_REALLY_EXTERN (decl)
1925 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
1926 || (DECL_DECLARED_INLINE_P (decl)
1927 && ! flag_implement_inlines
1928 && !DECL_VINDEX (decl)));
1930 if (!DECL_NOT_REALLY_EXTERN (decl))
1931 DECL_EXTERNAL (decl) = 1;
1933 /* Always make artificials weak. */
1934 if (DECL_ARTIFICIAL (decl) && flag_weak)
1937 maybe_make_one_only (decl);
1948 /* If we are importing DECL into this translation unit, mark is
1949 an undefined here. */
1950 DECL_EXTERNAL (decl) = 1;
1951 DECL_NOT_REALLY_EXTERN (decl) = 0;
1955 /* If we decided to put DECL in COMDAT, mark it accordingly at
1957 comdat_linkage (decl);
1960 DECL_INTERFACE_KNOWN (decl) = 1;
1963 /* Return an expression that performs the destruction of DECL, which
1964 must be a VAR_DECL whose type has a non-trivial destructor, or is
1965 an array whose (innermost) elements have a non-trivial destructor. */
1968 build_cleanup (tree decl)
1971 tree type = TREE_TYPE (decl);
1973 /* This function should only be called for declarations that really
1974 require cleanups. */
1975 gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type));
1977 /* Treat all objects with destructors as used; the destructor may do
1978 something substantive. */
1981 if (TREE_CODE (type) == ARRAY_TYPE)
1985 cxx_mark_addressable (decl);
1986 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
1988 temp = build_delete (TREE_TYPE (temp), temp,
1989 sfk_complete_destructor,
1990 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
1994 /* Returns the initialization guard variable for the variable DECL,
1995 which has static storage duration. */
1998 get_guard (tree decl)
2003 sname = mangle_guard_variable (decl);
2004 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2009 /* We use a type that is big enough to contain a mutex as well
2010 as an integer counter. */
2011 guard_type = targetm.cxx.guard_type ();
2012 guard = build_decl (VAR_DECL, sname, guard_type);
2014 /* The guard should have the same linkage as what it guards. */
2015 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2016 TREE_STATIC (guard) = TREE_STATIC (decl);
2017 DECL_COMMON (guard) = DECL_COMMON (decl);
2018 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2019 if (TREE_PUBLIC (decl))
2020 DECL_WEAK (guard) = DECL_WEAK (decl);
2022 DECL_ARTIFICIAL (guard) = 1;
2023 DECL_IGNORED_P (guard) = 1;
2024 TREE_USED (guard) = 1;
2025 pushdecl_top_level_and_finish (guard, NULL_TREE);
2030 /* Return those bits of the GUARD variable that should be set when the
2031 guarded entity is actually initialized. */
2034 get_guard_bits (tree guard)
2036 if (!targetm.cxx.guard_mask_bit ())
2038 /* We only set the first byte of the guard, in order to leave room
2039 for a mutex in the high-order bits. */
2040 guard = build1 (ADDR_EXPR,
2041 build_pointer_type (TREE_TYPE (guard)),
2043 guard = build1 (NOP_EXPR,
2044 build_pointer_type (char_type_node),
2046 guard = build1 (INDIRECT_REF, char_type_node, guard);
2052 /* Return an expression which determines whether or not the GUARD
2053 variable has already been initialized. */
2056 get_guard_cond (tree guard)
2060 /* Check to see if the GUARD is zero. */
2061 guard = get_guard_bits (guard);
2063 /* Mask off all but the low bit. */
2064 if (targetm.cxx.guard_mask_bit ())
2066 guard_value = integer_one_node;
2067 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2068 guard_value = convert (TREE_TYPE (guard), guard_value);
2069 guard = cp_build_binary_op (BIT_AND_EXPR, guard, guard_value);
2072 guard_value = integer_zero_node;
2073 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2074 guard_value = convert (TREE_TYPE (guard), guard_value);
2075 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2078 /* Return an expression which sets the GUARD variable, indicating that
2079 the variable being guarded has been initialized. */
2082 set_guard (tree guard)
2086 /* Set the GUARD to one. */
2087 guard = get_guard_bits (guard);
2088 guard_init = integer_one_node;
2089 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2090 guard_init = convert (TREE_TYPE (guard), guard_init);
2091 return build_modify_expr (guard, NOP_EXPR, guard_init);
2094 /* Start the process of running a particular set of global constructors
2095 or destructors. Subroutine of do_[cd]tors. */
2098 start_objects (int method_type, int initp)
2104 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2106 if (initp != DEFAULT_INIT_PRIORITY)
2116 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2119 sprintf (type, "%c", method_type);
2121 fndecl = build_lang_decl (FUNCTION_DECL,
2122 get_file_function_name_long (type),
2123 build_function_type (void_type_node,
2125 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
2127 /* It can be a static function as long as collect2 does not have
2128 to scan the object file to find its ctor/dtor routine. */
2129 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
2131 /* Mark this declaration as used to avoid spurious warnings. */
2132 TREE_USED (current_function_decl) = 1;
2134 /* Mark this function as a global constructor or destructor. */
2135 if (method_type == 'I')
2136 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2138 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2139 DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
2141 body = begin_compound_stmt (BCS_FN_BODY);
2143 /* We cannot allow these functions to be elided, even if they do not
2144 have external linkage. And, there's no point in deferring
2145 compilation of thes functions; they're all going to have to be
2147 DECL_INLINE (current_function_decl) = 0;
2148 DECL_UNINLINABLE (current_function_decl) = 1;
2153 /* Finish the process of running a particular set of global constructors
2154 or destructors. Subroutine of do_[cd]tors. */
2157 finish_objects (int method_type, int initp, tree body)
2162 finish_compound_stmt (body);
2163 fn = finish_function (0);
2164 expand_or_defer_fn (fn);
2166 /* When only doing semantic analysis, and no RTL generation, we
2167 can't call functions that directly emit assembly code; there is
2168 no assembly file in which to put the code. */
2169 if (flag_syntax_only)
2172 if (targetm.have_ctors_dtors)
2174 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2175 cgraph_mark_needed_node (cgraph_node (fn));
2176 if (method_type == 'I')
2177 (* targetm.asm_out.constructor) (fnsym, initp);
2179 (* targetm.asm_out.destructor) (fnsym, initp);
2183 /* The names of the parameters to the function created to handle
2184 initializations and destructions for objects with static storage
2186 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2187 #define PRIORITY_IDENTIFIER "__priority"
2189 /* The name of the function we create to handle initializations and
2190 destructions for objects with static storage duration. */
2191 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2193 /* The declaration for the __INITIALIZE_P argument. */
2194 static GTY(()) tree initialize_p_decl;
2196 /* The declaration for the __PRIORITY argument. */
2197 static GTY(()) tree priority_decl;
2199 /* The declaration for the static storage duration function. */
2200 static GTY(()) tree ssdf_decl;
2202 /* All the static storage duration functions created in this
2203 translation unit. */
2204 static GTY(()) VEC(tree,gc) *ssdf_decls;
2206 /* A map from priority levels to information about that priority
2207 level. There may be many such levels, so efficient lookup is
2209 static splay_tree priority_info_map;
2211 /* Begins the generation of the function that will handle all
2212 initialization and destruction of objects with static storage
2213 duration. The function generated takes two parameters of type
2214 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2215 nonzero, it performs initializations. Otherwise, it performs
2216 destructions. It only performs those initializations or
2217 destructions with the indicated __PRIORITY. The generated function
2220 It is assumed that this function will only be called once per
2221 translation unit. */
2224 start_static_storage_duration_function (unsigned count)
2229 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2231 /* Create the identifier for this function. It will be of the form
2232 SSDF_IDENTIFIER_<number>. */
2233 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
2235 /* Create the parameters. */
2236 parm_types = void_list_node;
2237 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2238 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2239 type = build_function_type (void_type_node, parm_types);
2241 /* Create the FUNCTION_DECL itself. */
2242 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2243 get_identifier (id),
2245 TREE_PUBLIC (ssdf_decl) = 0;
2246 DECL_ARTIFICIAL (ssdf_decl) = 1;
2248 /* Put this function in the list of functions to be called from the
2249 static constructors and destructors. */
2252 ssdf_decls = VEC_alloc (tree, gc, 32);
2254 /* Take this opportunity to initialize the map from priority
2255 numbers to information about that priority level. */
2256 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2257 /*delete_key_fn=*/0,
2258 /*delete_value_fn=*/
2259 (splay_tree_delete_value_fn) &free);
2261 /* We always need to generate functions for the
2262 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2263 priorities later, we'll be sure to find the
2264 DEFAULT_INIT_PRIORITY. */
2265 get_priority_info (DEFAULT_INIT_PRIORITY);
2268 VEC_safe_push (tree, gc, ssdf_decls, ssdf_decl);
2270 /* Create the argument list. */
2271 initialize_p_decl = cp_build_parm_decl
2272 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2273 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2274 TREE_USED (initialize_p_decl) = 1;
2275 priority_decl = cp_build_parm_decl
2276 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2277 DECL_CONTEXT (priority_decl) = ssdf_decl;
2278 TREE_USED (priority_decl) = 1;
2280 TREE_CHAIN (initialize_p_decl) = priority_decl;
2281 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2283 /* Put the function in the global scope. */
2284 pushdecl (ssdf_decl);
2286 /* Start the function itself. This is equivalent to declaring the
2289 static void __ssdf (int __initialize_p, init __priority_p);
2291 It is static because we only need to call this function from the
2292 various constructor and destructor functions for this module. */
2293 start_preparsed_function (ssdf_decl,
2294 /*attrs=*/NULL_TREE,
2297 /* Set up the scope of the outermost block in the function. */
2298 body = begin_compound_stmt (BCS_FN_BODY);
2300 /* This function must not be deferred because we are depending on
2301 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2302 DECL_INLINE (ssdf_decl) = 0;
2303 DECL_UNINLINABLE (ssdf_decl) = 1;
2308 /* Finish the generation of the function which performs initialization
2309 and destruction of objects with static storage duration. After
2310 this point, no more such objects can be created. */
2313 finish_static_storage_duration_function (tree body)
2315 /* Close out the function. */
2316 finish_compound_stmt (body);
2317 expand_or_defer_fn (finish_function (0));
2320 /* Return the information about the indicated PRIORITY level. If no
2321 code to handle this level has yet been generated, generate the
2322 appropriate prologue. */
2324 static priority_info
2325 get_priority_info (int priority)
2330 n = splay_tree_lookup (priority_info_map,
2331 (splay_tree_key) priority);
2334 /* Create a new priority information structure, and insert it
2336 pi = XNEW (struct priority_info_s);
2337 pi->initializations_p = 0;
2338 pi->destructions_p = 0;
2339 splay_tree_insert (priority_info_map,
2340 (splay_tree_key) priority,
2341 (splay_tree_value) pi);
2344 pi = (priority_info) n->value;
2349 /* The effective initialization priority of a DECL. */
2351 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
2352 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
2353 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
2355 /* Whether a DECL needs a guard to protect it against multiple
2358 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
2359 || DECL_ONE_ONLY (decl) \
2360 || DECL_WEAK (decl)))
2362 /* Set up to handle the initialization or destruction of DECL. If
2363 INITP is nonzero, we are initializing the variable. Otherwise, we
2364 are destroying it. */
2367 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
2369 tree guard_if_stmt = NULL_TREE;
2372 /* If we are supposed to destruct and there's a trivial destructor,
2373 nothing has to be done. */
2375 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2378 /* Trick the compiler into thinking we are at the file and line
2379 where DECL was declared so that error-messages make sense, and so
2380 that the debugger will show somewhat sensible file and line
2382 input_location = DECL_SOURCE_LOCATION (decl);
2388 Access control for implicit calls to the constructors,
2389 the conversion functions, or the destructor called to
2390 create and destroy a static data member is performed as
2391 if these calls appeared in the scope of the member's
2394 we pretend we are in a static member function of the class of
2395 which the DECL is a member. */
2396 if (member_p (decl))
2398 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2399 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2402 /* Assume we don't need a guard. */
2404 /* We need a guard if this is an object with external linkage that
2405 might be initialized in more than one place. (For example, a
2406 static data member of a template, when the data member requires
2408 if (NEEDS_GUARD_P (decl))
2412 guard = get_guard (decl);
2414 /* When using __cxa_atexit, we just check the GUARD as we would
2415 for a local static. */
2416 if (flag_use_cxa_atexit)
2418 /* When using __cxa_atexit, we never try to destroy
2419 anything from a static destructor. */
2421 guard_cond = get_guard_cond (guard);
2423 /* If we don't have __cxa_atexit, then we will be running
2424 destructors from .fini sections, or their equivalents. So,
2425 we need to know how many times we've tried to initialize this
2426 object. We do initializations only if the GUARD is zero,
2427 i.e., if we are the first to initialize the variable. We do
2428 destructions only if the GUARD is one, i.e., if we are the
2429 last to destroy the variable. */
2432 = cp_build_binary_op (EQ_EXPR,
2433 build_unary_op (PREINCREMENT_EXPR,
2439 = cp_build_binary_op (EQ_EXPR,
2440 build_unary_op (PREDECREMENT_EXPR,
2445 guard_if_stmt = begin_if_stmt ();
2446 finish_if_stmt_cond (guard_cond, guard_if_stmt);
2450 /* If we're using __cxa_atexit, we have not already set the GUARD,
2451 so we must do so now. */
2452 if (guard && initp && flag_use_cxa_atexit)
2453 finish_expr_stmt (set_guard (guard));
2455 /* Perform the initialization or destruction. */
2459 finish_expr_stmt (init);
2461 /* If we're using __cxa_atexit, register a function that calls the
2462 destructor for the object. */
2463 if (flag_use_cxa_atexit)
2464 finish_expr_stmt (register_dtor_fn (decl));
2467 finish_expr_stmt (build_cleanup (decl));
2469 /* Finish the guard if-stmt, if necessary. */
2472 finish_then_clause (guard_if_stmt);
2473 finish_if_stmt (guard_if_stmt);
2476 /* Now that we're done with DECL we don't need to pretend to be a
2477 member of its class any longer. */
2478 DECL_CONTEXT (current_function_decl) = NULL_TREE;
2479 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
2482 /* Generate code to do the initialization or destruction of the decls in VARS,
2483 a TREE_LIST of VAR_DECL with static storage duration.
2484 Whether initialization or destruction is performed is specified by INITP. */
2487 do_static_initialization_or_destruction (tree vars, bool initp)
2489 tree node, init_if_stmt, cond;
2491 /* Build the outer if-stmt to check for initialization or destruction. */
2492 init_if_stmt = begin_if_stmt ();
2493 cond = initp ? integer_one_node : integer_zero_node;
2494 cond = cp_build_binary_op (EQ_EXPR,
2497 finish_if_stmt_cond (cond, init_if_stmt);
2501 tree decl = TREE_VALUE (node);
2502 tree priority_if_stmt;
2506 /* If we don't need a destructor, there's nothing to do. Avoid
2507 creating a possibly empty if-stmt. */
2508 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2510 node = TREE_CHAIN (node);
2514 /* Remember that we had an initialization or finalization at this
2516 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
2517 pi = get_priority_info (priority);
2519 pi->initializations_p = 1;
2521 pi->destructions_p = 1;
2523 /* Conditionalize this initialization on being in the right priority
2524 and being initializing/finalizing appropriately. */
2525 priority_if_stmt = begin_if_stmt ();
2526 cond = cp_build_binary_op (EQ_EXPR,
2528 build_int_cst (NULL_TREE, priority));
2529 finish_if_stmt_cond (cond, priority_if_stmt);
2531 /* Process initializers with same priority. */
2533 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
2534 node = TREE_CHAIN (node))
2535 /* Do one initialization or destruction. */
2536 one_static_initialization_or_destruction (TREE_VALUE (node),
2537 TREE_PURPOSE (node), initp);
2539 /* Finish up the priority if-stmt body. */
2540 finish_then_clause (priority_if_stmt);
2541 finish_if_stmt (priority_if_stmt);
2545 /* Finish up the init/destruct if-stmt body. */
2546 finish_then_clause (init_if_stmt);
2547 finish_if_stmt (init_if_stmt);
2550 /* VARS is a list of variables with static storage duration which may
2551 need initialization and/or finalization. Remove those variables
2552 that don't really need to be initialized or finalized, and return
2553 the resulting list. The order in which the variables appear in
2554 VARS is in reverse order of the order in which they should actually
2555 be initialized. The list we return is in the unreversed order;
2556 i.e., the first variable should be initialized first. */
2559 prune_vars_needing_no_initialization (tree *vars)
2562 tree result = NULL_TREE;
2567 tree decl = TREE_VALUE (t);
2568 tree init = TREE_PURPOSE (t);
2570 /* Deal gracefully with error. */
2571 if (decl == error_mark_node)
2573 var = &TREE_CHAIN (t);
2577 /* The only things that can be initialized are variables. */
2578 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2580 /* If this object is not defined, we don't need to do anything
2582 if (DECL_EXTERNAL (decl))
2584 var = &TREE_CHAIN (t);
2588 /* Also, if the initializer already contains errors, we can bail
2590 if (init && TREE_CODE (init) == TREE_LIST
2591 && value_member (error_mark_node, init))
2593 var = &TREE_CHAIN (t);
2597 /* This variable is going to need initialization and/or
2598 finalization, so we add it to the list. */
2599 *var = TREE_CHAIN (t);
2600 TREE_CHAIN (t) = result;
2607 /* Make sure we have told the back end about all the variables in
2611 write_out_vars (tree vars)
2615 for (v = vars; v; v = TREE_CHAIN (v))
2617 tree var = TREE_VALUE (v);
2618 if (!var_finalized_p (var))
2620 import_export_decl (var);
2621 rest_of_decl_compilation (var, 1, 1);
2626 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
2627 (otherwise) that will initialize all gobal objects with static
2628 storage duration having the indicated PRIORITY. */
2631 generate_ctor_or_dtor_function (bool constructor_p, int priority,
2640 input_location = *locus;
2641 #ifdef USE_MAPPED_LOCATION
2647 /* We use `I' to indicate initialization and `D' to indicate
2649 function_key = constructor_p ? 'I' : 'D';
2651 /* We emit the function lazily, to avoid generating empty
2652 global constructors and destructors. */
2655 /* For Objective-C++, we may need to initialize metadata found in this module.
2656 This must be done _before_ any other static initializations. */
2657 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
2658 && constructor_p && objc_static_init_needed_p ())
2660 body = start_objects (function_key, priority);
2661 static_ctors = objc_generate_static_init_call (static_ctors);
2664 /* Call the static storage duration function with appropriate
2666 for (i = 0; VEC_iterate (tree, ssdf_decls, i, fndecl); ++i)
2668 /* Calls to pure or const functions will expand to nothing. */
2669 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2672 body = start_objects (function_key, priority);
2674 arguments = tree_cons (NULL_TREE,
2675 build_int_cst (NULL_TREE, priority),
2677 arguments = tree_cons (NULL_TREE,
2678 build_int_cst (NULL_TREE, constructor_p),
2680 finish_expr_stmt (build_function_call (fndecl, arguments));
2684 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
2685 calls to any functions marked with attributes indicating that
2686 they should be called at initialization- or destruction-time. */
2687 if (priority == DEFAULT_INIT_PRIORITY)
2691 for (fns = constructor_p ? static_ctors : static_dtors;
2693 fns = TREE_CHAIN (fns))
2695 fndecl = TREE_VALUE (fns);
2697 /* Calls to pure/const functions will expand to nothing. */
2698 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2701 body = start_objects (function_key, priority);
2702 finish_expr_stmt (build_function_call (fndecl, NULL_TREE));
2707 /* Close out the function. */
2709 finish_objects (function_key, priority, body);
2712 /* Generate constructor and destructor functions for the priority
2716 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
2718 location_t *locus = (location_t *) data;
2719 int priority = (int) n->key;
2720 priority_info pi = (priority_info) n->value;
2722 /* Generate the functions themselves, but only if they are really
2724 if (pi->initializations_p
2725 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
2726 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
2727 if (pi->destructions_p
2728 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
2729 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
2731 /* Keep iterating. */
2735 /* Called via LANGHOOK_CALLGRAPH_ANALYZE_EXPR. It is supposed to mark
2736 decls referenced from frontend specific constructs; it will be called
2737 only for language-specific tree nodes.
2739 Here we must deal with member pointers. */
2742 cxx_callgraph_analyze_expr (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2743 tree from ATTRIBUTE_UNUSED)
2747 switch (TREE_CODE (t))
2750 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2751 cgraph_mark_needed_node (cgraph_node (PTRMEM_CST_MEMBER (t)));
2754 if (TREE_CODE (BASELINK_FUNCTIONS (t)) == FUNCTION_DECL)
2755 cgraph_mark_needed_node (cgraph_node (BASELINK_FUNCTIONS (t)));
2758 if (DECL_VTABLE_OR_VTT_P (t))
2760 /* The ABI requires that all virtual tables be emitted
2761 whenever one of them is. */
2763 for (vtbl = CLASSTYPE_VTABLES (DECL_CONTEXT (t));
2765 vtbl = TREE_CHAIN (vtbl))
2766 mark_decl_referenced (vtbl);
2768 else if (DECL_CONTEXT (t)
2769 && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
2770 /* If we need a static variable in a function, then we
2771 need the containing function. */
2772 mark_decl_referenced (DECL_CONTEXT (t));
2781 /* Java requires that we be able to reference a local address for a
2782 method, and not be confused by PLT entries. If hidden aliases are
2783 supported, emit one for each java function that we've emitted. */
2786 build_java_method_aliases (void)
2788 struct cgraph_node *node;
2790 #ifndef HAVE_GAS_HIDDEN
2794 for (node = cgraph_nodes; node ; node = node->next)
2796 tree fndecl = node->decl;
2798 if (TREE_ASM_WRITTEN (fndecl)
2799 && DECL_CONTEXT (fndecl)
2800 && TYPE_P (DECL_CONTEXT (fndecl))
2801 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
2802 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
2804 /* Mangle the name in a predictable way; we need to reference
2805 this from a java compiled object file. */
2806 tree oid, nid, alias;
2810 oid = DECL_ASSEMBLER_NAME (fndecl);
2811 oname = IDENTIFIER_POINTER (oid);
2812 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
2813 nname = ACONCAT (("_ZGA", oname+2, NULL));
2814 nid = get_identifier (nname);
2816 alias = make_alias_for (fndecl, nid);
2817 TREE_PUBLIC (alias) = 1;
2818 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
2820 assemble_alias (alias, oid);
2825 /* This routine is called from the last rule in yyparse ().
2826 Its job is to create all the code needed to initialize and
2827 destroy the global aggregates. We do the destruction
2828 first, since that way we only need to reverse the decls once. */
2831 cp_finish_file (void)
2837 unsigned ssdf_count = 0;
2841 locus = input_location;
2844 /* Bad parse errors. Just forget about it. */
2845 if (! global_bindings_p () || current_class_type || decl_namespace_list)
2849 c_common_write_pch ();
2851 #ifdef USE_MAPPED_LOCATION
2854 /* Otherwise, GDB can get confused, because in only knows
2855 about source for LINENO-1 lines. */
2859 /* We now have to write out all the stuff we put off writing out.
2862 o Template specializations that we have not yet instantiated,
2863 but which are needed.
2864 o Initialization and destruction for non-local objects with
2865 static storage duration. (Local objects with static storage
2866 duration are initialized when their scope is first entered,
2867 and are cleaned up via atexit.)
2868 o Virtual function tables.
2870 All of these may cause others to be needed. For example,
2871 instantiating one function may cause another to be needed, and
2872 generating the initializer for an object may cause templates to be
2873 instantiated, etc., etc. */
2875 timevar_push (TV_VARCONST);
2877 emit_support_tinfos ();
2886 /* If there are templates that we've put off instantiating, do
2888 instantiate_pending_templates (retries);
2891 /* Write out virtual tables as required. Note that writing out
2892 the virtual table for a template class may cause the
2893 instantiation of members of that class. If we write out
2894 vtables then we remove the class from our list so we don't
2895 have to look at it again. */
2897 while (keyed_classes != NULL_TREE
2898 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
2901 keyed_classes = TREE_CHAIN (keyed_classes);
2907 tree next = TREE_CHAIN (t);
2911 if (maybe_emit_vtables (TREE_VALUE (next)))
2914 TREE_CHAIN (t) = TREE_CHAIN (next);
2919 next = TREE_CHAIN (t);
2923 /* Write out needed type info variables. We have to be careful
2924 looping through unemitted decls, because emit_tinfo_decl may
2925 cause other variables to be needed. New elements will be
2926 appended, and we remove from the vector those that actually
2928 for (i = VEC_length (tree, unemitted_tinfo_decls);
2929 VEC_iterate (tree, unemitted_tinfo_decls, --i, t);)
2930 if (emit_tinfo_decl (t))
2933 VEC_unordered_remove (tree, unemitted_tinfo_decls, i);
2936 /* The list of objects with static storage duration is built up
2937 in reverse order. We clear STATIC_AGGREGATES so that any new
2938 aggregates added during the initialization of these will be
2939 initialized in the correct order when we next come around the
2941 vars = prune_vars_needing_no_initialization (&static_aggregates);
2945 /* We need to start a new initialization function each time
2946 through the loop. That's because we need to know which
2947 vtables have been referenced, and TREE_SYMBOL_REFERENCED
2948 isn't computed until a function is finished, and written
2949 out. That's a deficiency in the back-end. When this is
2950 fixed, these initialization functions could all become
2951 inline, with resulting performance improvements. */
2954 /* Set the line and file, so that it is obviously not from
2956 input_location = locus;
2957 ssdf_body = start_static_storage_duration_function (ssdf_count);
2959 /* Make sure the back end knows about all the variables. */
2960 write_out_vars (vars);
2962 /* First generate code to do all the initializations. */
2964 do_static_initialization_or_destruction (vars, /*initp=*/true);
2966 /* Then, generate code to do all the destructions. Do these
2967 in reverse order so that the most recently constructed
2968 variable is the first destroyed. If we're using
2969 __cxa_atexit, then we don't need to do this; functions
2970 were registered at initialization time to destroy the
2972 if (!flag_use_cxa_atexit && vars)
2974 vars = nreverse (vars);
2975 do_static_initialization_or_destruction (vars, /*initp=*/false);
2980 /* Finish up the static storage duration function for this
2982 input_location = locus;
2983 finish_static_storage_duration_function (ssdf_body);
2985 /* All those initializations and finalizations might cause
2986 us to need more inline functions, more template
2987 instantiations, etc. */
2990 #ifdef USE_MAPPED_LOCATION
2997 /* Go through the set of inline functions whose bodies have not
2998 been emitted yet. If out-of-line copies of these functions
2999 are required, emit them. */
3000 for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3002 /* Does it need synthesizing? */
3003 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3004 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3006 /* Even though we're already at the top-level, we push
3007 there again. That way, when we pop back a few lines
3008 hence, all of our state is restored. Otherwise,
3009 finish_function doesn't clean things up, and we end
3010 up with CURRENT_FUNCTION_DECL set. */
3011 push_to_top_level ();
3012 /* The decl's location will mark where it was first
3013 needed. Save that so synthesize method can indicate
3014 where it was needed from, in case of error */
3015 input_location = DECL_SOURCE_LOCATION (decl);
3016 synthesize_method (decl);
3017 pop_from_top_level ();
3021 if (!DECL_SAVED_TREE (decl))
3024 /* We lie to the back-end, pretending that some functions
3025 are not defined when they really are. This keeps these
3026 functions from being put out unnecessarily. But, we must
3027 stop lying when the functions are referenced, or if they
3028 are not comdat since they need to be put out now. If
3029 DECL_INTERFACE_KNOWN, then we have already set
3030 DECL_EXTERNAL appropriately, so there's no need to check
3031 again, and we do not want to clear DECL_EXTERNAL if a
3032 previous call to import_export_decl set it.
3034 This is done in a separate for cycle, because if some
3035 deferred function is contained in another deferred
3036 function later in deferred_fns varray,
3037 rest_of_compilation would skip this function and we
3038 really cannot expand the same function twice. */
3039 import_export_decl (decl);
3040 if (DECL_NOT_REALLY_EXTERN (decl)
3041 && DECL_INITIAL (decl)
3042 && decl_needed_p (decl))
3043 DECL_EXTERNAL (decl) = 0;
3045 /* If we're going to need to write this function out, and
3046 there's already a body for it, create RTL for it now.
3047 (There might be no body if this is a method we haven't
3048 gotten around to synthesizing yet.) */
3049 if (!DECL_EXTERNAL (decl)
3050 && decl_needed_p (decl)
3051 && !TREE_ASM_WRITTEN (decl)
3052 && !cgraph_node (decl)->local.finalized)
3054 /* We will output the function; no longer consider it in this
3056 DECL_DEFER_OUTPUT (decl) = 0;
3057 /* Generate RTL for this function now that we know we
3059 expand_or_defer_fn (decl);
3060 /* If we're compiling -fsyntax-only pretend that this
3061 function has been written out so that we don't try to
3063 if (flag_syntax_only)
3064 TREE_ASM_WRITTEN (decl) = 1;
3069 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3072 /* Static data members are just like namespace-scope globals. */
3073 for (i = 0; VEC_iterate (tree, pending_statics, i, decl); ++i)
3075 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl))
3077 import_export_decl (decl);
3078 /* If this static data member is needed, provide it to the
3080 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
3081 DECL_EXTERNAL (decl) = 0;
3083 if (VEC_length (tree, pending_statics) != 0
3084 && wrapup_global_declarations (VEC_address (tree, pending_statics),
3085 VEC_length (tree, pending_statics)))
3092 /* All used inline functions must have a definition at this point. */
3093 for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3095 if (/* Check online inline functions that were actually used. */
3096 TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
3097 /* If the definition actually was available here, then the
3098 fact that the function was not defined merely represents
3099 that for some reason (use of a template repository,
3100 #pragma interface, etc.) we decided not to emit the
3102 && !DECL_INITIAL (decl)
3103 /* An explicit instantiation can be used to specify
3104 that the body is in another unit. It will have
3105 already verified there was a definition. */
3106 && !DECL_EXPLICIT_INSTANTIATION (decl))
3108 warning (0, "inline function %q+D used but never defined", decl);
3109 /* Avoid a duplicate warning from check_global_declaration_1. */
3110 TREE_NO_WARNING (decl) = 1;
3114 /* We give C linkage to static constructors and destructors. */
3115 push_lang_context (lang_name_c);
3117 /* Generate initialization and destruction functions for all
3118 priorities for which they are required. */
3119 if (priority_info_map)
3120 splay_tree_foreach (priority_info_map,
3121 generate_ctor_and_dtor_functions_for_priority,
3125 /* If we have a ctor or this is obj-c++ and we need a static init,
3126 call generate_ctor_or_dtor_function. */
3127 if (static_ctors || (c_dialect_objc () && objc_static_init_needed_p ()))
3128 generate_ctor_or_dtor_function (/*constructor_p=*/true,
3129 DEFAULT_INIT_PRIORITY, &locus);
3131 generate_ctor_or_dtor_function (/*constructor_p=*/false,
3132 DEFAULT_INIT_PRIORITY, &locus);
3135 /* We're done with the splay-tree now. */
3136 if (priority_info_map)
3137 splay_tree_delete (priority_info_map);
3139 /* Generate any missing aliases. */
3140 maybe_apply_pending_pragma_weaks ();
3142 /* We're done with static constructors, so we can go back to "C++"
3144 pop_lang_context ();
3146 cgraph_finalize_compilation_unit ();
3149 /* Now, issue warnings about static, but not defined, functions,
3150 etc., and emit debugging information. */
3151 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3152 if (VEC_length (tree, pending_statics) != 0)
3154 check_global_declarations (VEC_address (tree, pending_statics),
3155 VEC_length (tree, pending_statics));
3156 emit_debug_global_declarations (VEC_address (tree, pending_statics),
3157 VEC_length (tree, pending_statics));
3160 /* Generate hidden aliases for Java. */
3161 build_java_method_aliases ();
3165 /* The entire file is now complete. If requested, dump everything
3169 FILE *stream = dump_begin (TDI_tu, &flags);
3173 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3174 dump_end (TDI_tu, stream);
3178 timevar_pop (TV_VARCONST);
3180 if (flag_detailed_statistics)
3182 dump_tree_statistics ();
3183 dump_time_statistics ();
3185 input_location = locus;
3187 #ifdef ENABLE_CHECKING
3188 validate_conversion_obstack ();
3189 #endif /* ENABLE_CHECKING */
3192 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
3193 function to call in parse-tree form; it has not yet been
3194 semantically analyzed. ARGS are the arguments to the function.
3195 They have already been semantically analyzed. */
3198 build_offset_ref_call_from_tree (tree fn, tree args)
3207 object = TREE_OPERAND (fn, 0);
3209 if (processing_template_decl)
3211 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
3212 || TREE_CODE (fn) == MEMBER_REF);
3213 if (type_dependent_expression_p (fn)
3214 || any_type_dependent_arguments_p (args))
3215 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
3217 /* Transform the arguments and add the implicit "this"
3218 parameter. That must be done before the FN is transformed
3219 because we depend on the form of FN. */
3220 args = build_non_dependent_args (args);
3221 if (TREE_CODE (fn) == DOTSTAR_EXPR)
3222 object = build_unary_op (ADDR_EXPR, object, 0);
3223 object = build_non_dependent_expr (object);
3224 args = tree_cons (NULL_TREE, object, args);
3225 /* Now that the arguments are done, transform FN. */
3226 fn = build_non_dependent_expr (fn);
3229 /* A qualified name corresponding to a bound pointer-to-member is
3230 represented as an OFFSET_REF:
3232 struct B { void g(); };
3234 void B::g() { (this->*p)(); } */
3235 if (TREE_CODE (fn) == OFFSET_REF)
3237 tree object_addr = build_unary_op (ADDR_EXPR, object, 0);
3238 fn = TREE_OPERAND (fn, 1);
3239 fn = get_member_function_from_ptrfunc (&object_addr, fn);
3240 args = tree_cons (NULL_TREE, object_addr, args);
3243 expr = build_function_call (fn, args);
3244 if (processing_template_decl && expr != error_mark_node)
3245 return build_min_non_dep (CALL_EXPR, expr, orig_fn, orig_args, NULL_TREE);
3251 check_default_args (tree x)
3253 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
3254 bool saw_def = false;
3255 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
3256 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
3258 if (TREE_PURPOSE (arg))
3262 error ("default argument missing for parameter %P of %q+#D", i, x);
3263 TREE_PURPOSE (arg) = error_mark_node;
3268 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
3269 If DECL is a specialization or implicitly declared class member,
3270 generate the actual definition. */
3273 mark_used (tree decl)
3275 HOST_WIDE_INT saved_processing_template_decl = 0;
3277 /* If DECL is a BASELINK for a single function, then treat it just
3278 like the DECL for the function. Otherwise, if the BASELINK is
3279 for an overloaded function, we don't know which function was
3280 actually used until after overload resolution. */
3281 if (TREE_CODE (decl) == BASELINK)
3283 decl = BASELINK_FUNCTIONS (decl);
3284 if (really_overloaded_fn (decl))
3286 decl = OVL_CURRENT (decl);
3289 TREE_USED (decl) = 1;
3290 /* If we don't need a value, then we don't need to synthesize DECL. */
3291 if (skip_evaluation)
3293 /* Normally, we can wait until instantiation-time to synthesize
3294 DECL. However, if DECL is a static data member initialized with
3295 a constant, we need the value right now because a reference to
3296 such a data member is not value-dependent. */
3297 if (TREE_CODE (decl) == VAR_DECL
3298 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
3299 && DECL_CLASS_SCOPE_P (decl))
3301 /* Don't try to instantiate members of dependent types. We
3302 cannot just use dependent_type_p here because this function
3303 may be called from fold_non_dependent_expr, and then we may
3304 see dependent types, even though processing_template_decl
3306 if (CLASSTYPE_TEMPLATE_INFO ((DECL_CONTEXT (decl)))
3307 && uses_template_parms (CLASSTYPE_TI_ARGS (DECL_CONTEXT (decl))))
3309 /* Pretend that we are not in a template, even if we are, so
3310 that the static data member initializer will be processed. */
3311 saved_processing_template_decl = processing_template_decl;
3312 processing_template_decl = 0;
3315 if (processing_template_decl)
3318 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
3319 && !TREE_ASM_WRITTEN (decl))
3320 /* Remember it, so we can check it was defined. */
3322 if (DECL_DEFERRED_FN (decl))
3325 /* Remember the current location for a function we will end up
3326 synthesizing. Then we can inform the user where it was
3327 required in the case of error. */
3328 if (DECL_ARTIFICIAL (decl) && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
3329 && !DECL_THUNK_P (decl))
3330 DECL_SOURCE_LOCATION (decl) = input_location;
3332 note_vague_linkage_fn (decl);
3335 assemble_external (decl);
3337 /* Is it a synthesized method that needs to be synthesized? */
3338 if (TREE_CODE (decl) == FUNCTION_DECL
3339 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
3340 && DECL_ARTIFICIAL (decl)
3341 && !DECL_THUNK_P (decl)
3342 && ! DECL_INITIAL (decl)
3343 /* Kludge: don't synthesize for default args. Unfortunately this
3344 rules out initializers of namespace-scoped objects too, but
3345 it's sort-of ok if the implicit ctor or dtor decl keeps
3346 pointing to the class location. */
3347 && current_function_decl)
3349 synthesize_method (decl);
3350 /* If we've already synthesized the method we don't need to
3351 instantiate it, so we can return right away. */
3355 /* If this is a function or variable that is an instance of some
3356 template, we now know that we will need to actually do the
3357 instantiation. We check that DECL is not an explicit
3358 instantiation because that is not checked in instantiate_decl. */
3359 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
3360 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3361 && (!DECL_EXPLICIT_INSTANTIATION (decl)
3362 || (TREE_CODE (decl) == FUNCTION_DECL
3363 && DECL_INLINE (DECL_TEMPLATE_RESULT
3364 (template_for_substitution (decl))))
3365 /* We need to instantiate static data members so that there
3366 initializers are available in integral constant
3368 || (TREE_CODE (decl) == VAR_DECL
3369 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))))
3370 /* We put off instantiating functions in order to improve compile
3371 times. Maintaining a stack of active functions is expensive,
3372 and the inliner knows to instantiate any functions it might
3374 instantiate_decl (decl, /*defer_ok=*/true,
3375 /*expl_inst_class_mem_p=*/false);
3377 processing_template_decl = saved_processing_template_decl;
3380 #include "gt-cp-decl2.h"