2008-04-30 Doug Kwan <dougkwan@google.com>
[official-gcc.git] / gcc / cp / decl2.c
blob8d6fdd8d1251de9b191388ea1a9e1129e0f09ff5
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008 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 3, or (at your option)
11 any later version.
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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* Process declarations and symbol lookup for C++ front end.
24 Also constructs types; the standard scalar types at initialization,
25 and structure, union, array and enum types when they are declared. */
27 /* ??? not all decl nodes are given the most useful possible
28 line numbers. For example, the CONST_DECLs for enum values. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "tree.h"
35 #include "rtl.h"
36 #include "expr.h"
37 #include "flags.h"
38 #include "cp-tree.h"
39 #include "decl.h"
40 #include "output.h"
41 #include "except.h"
42 #include "toplev.h"
43 #include "timevar.h"
44 #include "cpplib.h"
45 #include "target.h"
46 #include "c-common.h"
47 #include "tree-mudflap.h"
48 #include "cgraph.h"
49 #include "tree-inline.h"
50 #include "c-pragma.h"
51 #include "tree-dump.h"
52 #include "intl.h"
53 #include "gimple.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. */
65 int destructions_p;
66 } *priority_info;
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,
80 void *);
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. */
98 int at_eof;
102 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
103 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
104 that apply to the function). */
106 tree
107 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals)
109 tree raises;
110 int type_quals;
112 if (fntype == error_mark_node || ctype == error_mark_node)
113 return error_mark_node;
115 type_quals = quals & ~TYPE_QUAL_RESTRICT;
116 ctype = cp_build_qualified_type (ctype, type_quals);
117 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
118 (TREE_CODE (fntype) == METHOD_TYPE
119 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
120 : TYPE_ARG_TYPES (fntype)));
121 raises = TYPE_RAISES_EXCEPTIONS (fntype);
122 if (raises)
123 fntype = build_exception_variant (fntype, raises);
125 return fntype;
128 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
129 appropriately. */
131 tree
132 cp_build_parm_decl (tree name, tree type)
134 tree parm = build_decl (PARM_DECL, name, type);
135 /* DECL_ARG_TYPE is only used by the back end and the back end never
136 sees templates. */
137 if (!processing_template_decl)
138 DECL_ARG_TYPE (parm) = type_passed_as (type);
140 /* If the type is a pack expansion, then we have a function
141 parameter pack. */
142 if (type && TREE_CODE (type) == TYPE_PACK_EXPANSION)
143 FUNCTION_PARAMETER_PACK_P (parm) = 1;
145 return parm;
148 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
149 indicated NAME. */
151 tree
152 build_artificial_parm (tree name, tree type)
154 tree parm = cp_build_parm_decl (name, type);
155 DECL_ARTIFICIAL (parm) = 1;
156 /* All our artificial parms are implicitly `const'; they cannot be
157 assigned to. */
158 TREE_READONLY (parm) = 1;
159 return parm;
162 /* Constructors for types with virtual baseclasses need an "in-charge" flag
163 saying whether this constructor is responsible for initialization of
164 virtual baseclasses or not. All destructors also need this "in-charge"
165 flag, which additionally determines whether or not the destructor should
166 free the memory for the object.
168 This function adds the "in-charge" flag to member function FN if
169 appropriate. It is called from grokclassfn and tsubst.
170 FN must be either a constructor or destructor.
172 The in-charge flag follows the 'this' parameter, and is followed by the
173 VTT parm (if any), then the user-written parms. */
175 void
176 maybe_retrofit_in_chrg (tree fn)
178 tree basetype, arg_types, parms, parm, fntype;
180 /* If we've already add the in-charge parameter don't do it again. */
181 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
182 return;
184 /* When processing templates we can't know, in general, whether or
185 not we're going to have virtual baseclasses. */
186 if (processing_template_decl)
187 return;
189 /* We don't need an in-charge parameter for constructors that don't
190 have virtual bases. */
191 if (DECL_CONSTRUCTOR_P (fn)
192 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
193 return;
195 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
196 basetype = TREE_TYPE (TREE_VALUE (arg_types));
197 arg_types = TREE_CHAIN (arg_types);
199 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
201 /* If this is a subobject constructor or destructor, our caller will
202 pass us a pointer to our VTT. */
203 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
205 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
207 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
208 TREE_CHAIN (parm) = parms;
209 parms = parm;
211 /* ...and then to TYPE_ARG_TYPES. */
212 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
214 DECL_HAS_VTT_PARM_P (fn) = 1;
217 /* Then add the in-charge parm (before the VTT parm). */
218 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
219 TREE_CHAIN (parm) = parms;
220 parms = parm;
221 arg_types = hash_tree_chain (integer_type_node, arg_types);
223 /* Insert our new parameter(s) into the list. */
224 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
226 /* And rebuild the function type. */
227 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
228 arg_types);
229 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
230 fntype = build_exception_variant (fntype,
231 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
232 TREE_TYPE (fn) = fntype;
234 /* Now we've got the in-charge parameter. */
235 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
238 /* Classes overload their constituent function names automatically.
239 When a function name is declared in a record structure,
240 its name is changed to it overloaded name. Since names for
241 constructors and destructors can conflict, we place a leading
242 '$' for destructors.
244 CNAME is the name of the class we are grokking for.
246 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
248 FLAGS contains bits saying what's special about today's
249 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
251 If FUNCTION is a destructor, then we must add the `auto-delete' field
252 as a second parameter. There is some hair associated with the fact
253 that we must "declare" this variable in the manner consistent with the
254 way the rest of the arguments were declared.
256 QUALS are the qualifiers for the this pointer. */
258 void
259 grokclassfn (tree ctype, tree function, enum overload_flags flags)
261 tree fn_name = DECL_NAME (function);
263 /* Even within an `extern "C"' block, members get C++ linkage. See
264 [dcl.link] for details. */
265 SET_DECL_LANGUAGE (function, lang_cplusplus);
267 if (fn_name == NULL_TREE)
269 error ("name missing for member function");
270 fn_name = get_identifier ("<anonymous>");
271 DECL_NAME (function) = fn_name;
274 DECL_CONTEXT (function) = ctype;
276 if (flags == DTOR_FLAG)
277 DECL_DESTRUCTOR_P (function) = 1;
279 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
280 maybe_retrofit_in_chrg (function);
283 /* Create an ARRAY_REF, checking for the user doing things backwards
284 along the way. */
286 tree
287 grok_array_decl (tree array_expr, tree index_exp)
289 tree type;
290 tree expr;
291 tree orig_array_expr = array_expr;
292 tree orig_index_exp = index_exp;
294 if (error_operand_p (array_expr) || error_operand_p (index_exp))
295 return error_mark_node;
297 if (processing_template_decl)
299 if (type_dependent_expression_p (array_expr)
300 || type_dependent_expression_p (index_exp))
301 return build_min_nt (ARRAY_REF, array_expr, index_exp,
302 NULL_TREE, NULL_TREE);
303 array_expr = build_non_dependent_expr (array_expr);
304 index_exp = build_non_dependent_expr (index_exp);
307 type = TREE_TYPE (array_expr);
308 gcc_assert (type);
309 type = non_reference (type);
311 /* If they have an `operator[]', use that. */
312 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
313 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL,
314 array_expr, index_exp, NULL_TREE,
315 /*overloaded_p=*/NULL, tf_warning_or_error);
316 else
318 tree p1, p2, i1, i2;
320 /* Otherwise, create an ARRAY_REF for a pointer or array type.
321 It is a little-known fact that, if `a' is an array and `i' is
322 an int, you can write `i[a]', which means the same thing as
323 `a[i]'. */
324 if (TREE_CODE (type) == ARRAY_TYPE)
325 p1 = array_expr;
326 else
327 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
329 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
330 p2 = index_exp;
331 else
332 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
334 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
335 false);
336 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
337 false);
339 if ((p1 && i2) && (i1 && p2))
340 error ("ambiguous conversion for array subscript");
342 if (p1 && i2)
343 array_expr = p1, index_exp = i2;
344 else if (i1 && p2)
345 array_expr = p2, index_exp = i1;
346 else
348 error ("invalid types %<%T[%T]%> for array subscript",
349 type, TREE_TYPE (index_exp));
350 return error_mark_node;
353 if (array_expr == error_mark_node || index_exp == error_mark_node)
354 error ("ambiguous conversion for array subscript");
356 expr = build_array_ref (array_expr, index_exp);
358 if (processing_template_decl && expr != error_mark_node)
359 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
360 NULL_TREE, NULL_TREE);
361 return expr;
364 /* Given the cast expression EXP, checking out its validity. Either return
365 an error_mark_node if there was an unavoidable error, return a cast to
366 void for trying to delete a pointer w/ the value 0, or return the
367 call to delete. If DOING_VEC is true, we handle things differently
368 for doing an array delete.
369 Implements ARM $5.3.4. This is called from the parser. */
371 tree
372 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete)
374 tree t, type;
376 if (exp == error_mark_node)
377 return exp;
379 if (processing_template_decl)
381 t = build_min (DELETE_EXPR, void_type_node, exp, size);
382 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
383 DELETE_EXPR_USE_VEC (t) = doing_vec;
384 TREE_SIDE_EFFECTS (t) = 1;
385 return t;
388 /* An array can't have been allocated by new, so complain. */
389 if (TREE_CODE (exp) == VAR_DECL
390 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
391 warning (0, "deleting array %q#D", exp);
393 t = build_expr_type_conversion (WANT_POINTER, exp, true);
395 if (t == NULL_TREE || t == error_mark_node)
397 error ("type %q#T argument given to %<delete%>, expected pointer",
398 TREE_TYPE (exp));
399 return error_mark_node;
402 type = TREE_TYPE (t);
404 /* As of Valley Forge, you can delete a pointer to const. */
406 /* You can't delete functions. */
407 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
409 error ("cannot delete a function. Only pointer-to-objects are "
410 "valid arguments to %<delete%>");
411 return error_mark_node;
414 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
415 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
417 warning (0, "deleting %qT is undefined", type);
418 doing_vec = 0;
421 /* Deleting a pointer with the value zero is valid and has no effect. */
422 if (integer_zerop (t))
423 return build1 (NOP_EXPR, void_type_node, t);
425 if (doing_vec)
426 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
427 sfk_deleting_destructor,
428 use_global_delete);
429 else
430 return build_delete (type, t, sfk_deleting_destructor,
431 LOOKUP_NORMAL, use_global_delete);
434 /* Report an error if the indicated template declaration is not the
435 sort of thing that should be a member template. */
437 void
438 check_member_template (tree tmpl)
440 tree decl;
442 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
443 decl = DECL_TEMPLATE_RESULT (tmpl);
445 if (TREE_CODE (decl) == FUNCTION_DECL
446 || (TREE_CODE (decl) == TYPE_DECL
447 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
449 /* The parser rejects template declarations in local classes. */
450 gcc_assert (!current_function_decl);
451 /* The parser rejects any use of virtual in a function template. */
452 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
453 && DECL_VIRTUAL_P (decl)));
455 /* The debug-information generating code doesn't know what to do
456 with member templates. */
457 DECL_IGNORED_P (tmpl) = 1;
459 else
460 error ("template declaration of %q#D", decl);
463 /* Return true iff TYPE is a valid Java parameter or return type. */
465 static bool
466 acceptable_java_type (tree type)
468 if (type == error_mark_node)
469 return false;
471 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
472 return true;
473 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
475 type = TREE_TYPE (type);
476 if (TREE_CODE (type) == RECORD_TYPE)
478 tree args; int i;
479 if (! TYPE_FOR_JAVA (type))
480 return false;
481 if (! CLASSTYPE_TEMPLATE_INFO (type))
482 return true;
483 args = CLASSTYPE_TI_ARGS (type);
484 i = TREE_VEC_LENGTH (args);
485 while (--i >= 0)
487 type = TREE_VEC_ELT (args, i);
488 if (TREE_CODE (type) == POINTER_TYPE)
489 type = TREE_TYPE (type);
490 if (! TYPE_FOR_JAVA (type))
491 return false;
493 return true;
496 return false;
499 /* For a METHOD in a Java class CTYPE, return true if
500 the parameter and return types are valid Java types.
501 Otherwise, print appropriate error messages, and return false. */
503 bool
504 check_java_method (tree method)
506 bool jerr = false;
507 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
508 tree ret_type = TREE_TYPE (TREE_TYPE (method));
510 if (!acceptable_java_type (ret_type))
512 error ("Java method %qD has non-Java return type %qT",
513 method, ret_type);
514 jerr = true;
517 arg_types = TREE_CHAIN (arg_types);
518 if (DECL_HAS_IN_CHARGE_PARM_P (method))
519 arg_types = TREE_CHAIN (arg_types);
520 if (DECL_HAS_VTT_PARM_P (method))
521 arg_types = TREE_CHAIN (arg_types);
523 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
525 tree type = TREE_VALUE (arg_types);
526 if (!acceptable_java_type (type))
528 if (type != error_mark_node)
529 error ("Java method %qD has non-Java parameter type %qT",
530 method, type);
531 jerr = true;
534 return !jerr;
537 /* Sanity check: report error if this function FUNCTION is not
538 really a member of the class (CTYPE) it is supposed to belong to.
539 TEMPLATE_PARMS is used to specify the template parameters of a member
540 template passed as FUNCTION_DECL. If the member template is passed as a
541 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
542 from the declaration. If the function is not a function template, it
543 must be NULL.
544 It returns the original declaration for the function, NULL_TREE if
545 no declaration was found, error_mark_node if an error was emitted. */
547 tree
548 check_classfn (tree ctype, tree function, tree template_parms)
550 int ix;
551 bool is_template;
552 tree pushed_scope;
554 if (DECL_USE_TEMPLATE (function)
555 && !(TREE_CODE (function) == TEMPLATE_DECL
556 && DECL_TEMPLATE_SPECIALIZATION (function))
557 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
558 /* Since this is a specialization of a member template,
559 we're not going to find the declaration in the class.
560 For example, in:
562 struct S { template <typename T> void f(T); };
563 template <> void S::f(int);
565 we're not going to find `S::f(int)', but there's no
566 reason we should, either. We let our callers know we didn't
567 find the method, but we don't complain. */
568 return NULL_TREE;
570 /* Basic sanity check: for a template function, the template parameters
571 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
572 if (TREE_CODE (function) == TEMPLATE_DECL)
574 gcc_assert (!template_parms
575 || comp_template_parms (template_parms,
576 DECL_TEMPLATE_PARMS (function)));
577 template_parms = DECL_TEMPLATE_PARMS (function);
580 /* OK, is this a definition of a member template? */
581 is_template = (template_parms != NULL_TREE);
583 /* We must enter the scope here, because conversion operators are
584 named by target type, and type equivalence relies on typenames
585 resolving within the scope of CTYPE. */
586 pushed_scope = push_scope (ctype);
587 ix = class_method_index_for_fn (complete_type (ctype), function);
588 if (ix >= 0)
590 VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (ctype);
591 tree fndecls, fndecl = 0;
592 bool is_conv_op;
593 const char *format = NULL;
595 for (fndecls = VEC_index (tree, methods, ix);
596 fndecls; fndecls = OVL_NEXT (fndecls))
598 tree p1, p2;
600 fndecl = OVL_CURRENT (fndecls);
601 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
602 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
604 /* We cannot simply call decls_match because this doesn't
605 work for static member functions that are pretending to
606 be methods, and because the name may have been changed by
607 asm("new_name"). */
609 /* Get rid of the this parameter on functions that become
610 static. */
611 if (DECL_STATIC_FUNCTION_P (fndecl)
612 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
613 p1 = TREE_CHAIN (p1);
615 /* A member template definition only matches a member template
616 declaration. */
617 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
618 continue;
620 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
621 TREE_TYPE (TREE_TYPE (fndecl)))
622 && compparms (p1, p2)
623 && (!is_template
624 || comp_template_parms (template_parms,
625 DECL_TEMPLATE_PARMS (fndecl)))
626 && (DECL_TEMPLATE_SPECIALIZATION (function)
627 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
628 && (!DECL_TEMPLATE_SPECIALIZATION (function)
629 || (DECL_TI_TEMPLATE (function)
630 == DECL_TI_TEMPLATE (fndecl))))
631 break;
633 if (fndecls)
635 if (pushed_scope)
636 pop_scope (pushed_scope);
637 return OVL_CURRENT (fndecls);
640 error ("prototype for %q#D does not match any in class %qT",
641 function, ctype);
642 is_conv_op = DECL_CONV_FN_P (fndecl);
644 if (is_conv_op)
645 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
646 fndecls = VEC_index (tree, methods, ix);
647 while (fndecls)
649 fndecl = OVL_CURRENT (fndecls);
650 fndecls = OVL_NEXT (fndecls);
652 if (!fndecls && is_conv_op)
654 if (VEC_length (tree, methods) > (size_t) ++ix)
656 fndecls = VEC_index (tree, methods, ix);
657 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
659 fndecls = NULL_TREE;
660 is_conv_op = false;
663 else
664 is_conv_op = false;
666 if (format)
667 format = " %+#D";
668 else if (fndecls)
669 format = N_("candidates are: %+#D");
670 else
671 format = N_("candidate is: %+#D");
672 error (format, fndecl);
675 else if (!COMPLETE_TYPE_P (ctype))
676 cxx_incomplete_type_error (function, ctype);
677 else
678 error ("no %q#D member function declared in class %qT",
679 function, ctype);
681 if (pushed_scope)
682 pop_scope (pushed_scope);
683 return error_mark_node;
686 /* DECL is a function with vague linkage. Remember it so that at the
687 end of the translation unit we can decide whether or not to emit
688 it. */
690 void
691 note_vague_linkage_fn (tree decl)
693 if (!DECL_DEFERRED_FN (decl))
695 DECL_DEFERRED_FN (decl) = 1;
696 DECL_DEFER_OUTPUT (decl) = 1;
697 VEC_safe_push (tree, gc, deferred_fns, decl);
701 /* We have just processed the DECL, which is a static data member.
702 The other parameters are as for cp_finish_decl. */
704 void
705 finish_static_data_member_decl (tree decl,
706 tree init, bool init_const_expr_p,
707 tree asmspec_tree,
708 int flags)
710 DECL_CONTEXT (decl) = current_class_type;
712 /* We cannot call pushdecl here, because that would fill in the
713 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
714 the right thing, namely, to put this decl out straight away. */
716 if (! processing_template_decl)
717 VEC_safe_push (tree, gc, pending_statics, decl);
719 if (LOCAL_CLASS_P (current_class_type))
720 pedwarn ("local class %q#T shall not have static data member %q#D",
721 current_class_type, decl);
723 /* Static consts need not be initialized in the class definition. */
724 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
726 static int explained = 0;
728 error ("initializer invalid for static member with constructor");
729 if (!explained)
731 error ("(an out of class initialization is required)");
732 explained = 1;
734 init = NULL_TREE;
736 /* Force the compiler to know when an uninitialized static const
737 member is being used. */
738 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
739 TREE_USED (decl) = 1;
740 DECL_INITIAL (decl) = init;
741 DECL_IN_AGGR_P (decl) = 1;
743 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
746 /* DECLARATOR and DECLSPECS correspond to a class member. The other
747 parameters are as for cp_finish_decl. Return the DECL for the
748 class member declared. */
750 tree
751 grokfield (const cp_declarator *declarator,
752 cp_decl_specifier_seq *declspecs,
753 tree init, bool init_const_expr_p,
754 tree asmspec_tree,
755 tree attrlist)
757 tree value;
758 const char *asmspec = 0;
759 int flags = LOOKUP_ONLYCONVERTING;
761 if (init
762 && TREE_CODE (init) == TREE_LIST
763 && TREE_VALUE (init) == error_mark_node
764 && TREE_CHAIN (init) == NULL_TREE)
765 init = NULL_TREE;
767 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
768 if (! value || error_operand_p (value))
769 /* friend or constructor went bad. */
770 return error_mark_node;
772 if (TREE_CODE (value) == TYPE_DECL && init)
774 error ("typedef %qD is initialized (use __typeof__ instead)", value);
775 init = NULL_TREE;
778 /* Pass friendly classes back. */
779 if (value == void_type_node)
780 return value;
782 /* Pass friend decls back. */
783 if ((TREE_CODE (value) == FUNCTION_DECL
784 || TREE_CODE (value) == TEMPLATE_DECL)
785 && DECL_CONTEXT (value) != current_class_type)
786 return value;
788 if (DECL_NAME (value) != NULL_TREE
789 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
790 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
791 error ("member %qD conflicts with virtual function table field name",
792 value);
794 /* Stash away type declarations. */
795 if (TREE_CODE (value) == TYPE_DECL)
797 DECL_NONLOCAL (value) = 1;
798 DECL_CONTEXT (value) = current_class_type;
800 if (processing_template_decl)
801 value = push_template_decl (value);
803 if (attrlist)
804 cplus_decl_attributes (&value, attrlist, 0);
806 return value;
809 if (DECL_IN_AGGR_P (value))
811 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
812 return void_type_node;
815 if (asmspec_tree && asmspec_tree != error_mark_node)
816 asmspec = TREE_STRING_POINTER (asmspec_tree);
818 if (init)
820 if (TREE_CODE (value) == FUNCTION_DECL)
822 /* Initializers for functions are rejected early in the parser.
823 If we get here, it must be a pure specifier for a method. */
824 if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
826 gcc_assert (error_operand_p (init) || integer_zerop (init));
827 DECL_PURE_VIRTUAL_P (value) = 1;
829 else
831 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
832 error ("initializer specified for static member function %qD",
833 value);
836 else if (pedantic && TREE_CODE (value) != VAR_DECL)
837 /* Already complained in grokdeclarator. */
838 init = NULL_TREE;
839 else if (!processing_template_decl)
841 if (TREE_CODE (init) == CONSTRUCTOR)
842 init = digest_init (TREE_TYPE (value), init);
843 else
844 init = integral_constant_value (init);
846 if (init != error_mark_node && !TREE_CONSTANT (init))
848 /* We can allow references to things that are effectively
849 static, since references are initialized with the
850 address. */
851 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
852 || (TREE_STATIC (init) == 0
853 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
855 error ("field initializer is not constant");
856 init = error_mark_node;
862 if (processing_template_decl
863 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
865 value = push_template_decl (value);
866 if (error_operand_p (value))
867 return error_mark_node;
870 if (attrlist)
871 cplus_decl_attributes (&value, attrlist, 0);
873 switch (TREE_CODE (value))
875 case VAR_DECL:
876 finish_static_data_member_decl (value, init, init_const_expr_p,
877 asmspec_tree, flags);
878 return value;
880 case FIELD_DECL:
881 if (asmspec)
882 error ("%<asm%> specifiers are not permitted on non-static data members");
883 if (DECL_INITIAL (value) == error_mark_node)
884 init = error_mark_node;
885 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
886 NULL_TREE, flags);
887 DECL_INITIAL (value) = init;
888 DECL_IN_AGGR_P (value) = 1;
889 return value;
891 case FUNCTION_DECL:
892 if (asmspec)
893 set_user_assembler_name (value, asmspec);
895 cp_finish_decl (value,
896 /*init=*/NULL_TREE,
897 /*init_const_expr_p=*/false,
898 asmspec_tree, flags);
900 /* Pass friends back this way. */
901 if (DECL_FRIEND_P (value))
902 return void_type_node;
904 DECL_IN_AGGR_P (value) = 1;
905 return value;
907 default:
908 gcc_unreachable ();
910 return NULL_TREE;
913 /* Like `grokfield', but for bitfields.
914 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
916 tree
917 grokbitfield (const cp_declarator *declarator,
918 cp_decl_specifier_seq *declspecs, tree width)
920 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, NULL);
922 if (value == error_mark_node)
923 return NULL_TREE; /* friends went bad. */
925 /* Pass friendly classes back. */
926 if (TREE_CODE (value) == VOID_TYPE)
927 return void_type_node;
929 if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
930 && (POINTER_TYPE_P (value)
931 || !dependent_type_p (TREE_TYPE (value))))
933 error ("bit-field %qD with non-integral type", value);
934 return error_mark_node;
937 if (TREE_CODE (value) == TYPE_DECL)
939 error ("cannot declare %qD to be a bit-field type", value);
940 return NULL_TREE;
943 /* Usually, finish_struct_1 catches bitfields with invalid types.
944 But, in the case of bitfields with function type, we confuse
945 ourselves into thinking they are member functions, so we must
946 check here. */
947 if (TREE_CODE (value) == FUNCTION_DECL)
949 error ("cannot declare bit-field %qD with function type",
950 DECL_NAME (value));
951 return NULL_TREE;
954 if (DECL_IN_AGGR_P (value))
956 error ("%qD is already defined in the class %qT", value,
957 DECL_CONTEXT (value));
958 return void_type_node;
961 if (TREE_STATIC (value))
963 error ("static member %qD cannot be a bit-field", value);
964 return NULL_TREE;
966 finish_decl (value, NULL_TREE, NULL_TREE);
968 if (width != error_mark_node)
970 constant_expression_warning (width);
971 DECL_INITIAL (value) = width;
972 SET_DECL_C_BIT_FIELD (value);
975 DECL_IN_AGGR_P (value) = 1;
976 return value;
980 /* Returns true iff ATTR is an attribute which needs to be applied at
981 instantiation time rather than template definition time. */
983 static bool
984 is_late_template_attribute (tree attr, tree decl)
986 tree name = TREE_PURPOSE (attr);
987 tree args = TREE_VALUE (attr);
988 const struct attribute_spec *spec = lookup_attribute_spec (name);
989 tree arg;
991 if (!spec)
992 /* Unknown attribute. */
993 return false;
995 /* Attribute weak handling wants to write out assembly right away. */
996 if (is_attribute_p ("weak", name))
997 return true;
999 /* If any of the arguments are dependent expressions, we can't evaluate
1000 the attribute until instantiation time. */
1001 for (arg = args; arg; arg = TREE_CHAIN (arg))
1003 tree t = TREE_VALUE (arg);
1004 if (value_dependent_expression_p (t)
1005 || type_dependent_expression_p (t))
1006 return true;
1009 if (TREE_CODE (decl) == TYPE_DECL
1010 || TYPE_P (decl)
1011 || spec->type_required)
1013 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1015 /* We can't apply any attributes to a completely unknown type until
1016 instantiation time. */
1017 enum tree_code code = TREE_CODE (type);
1018 if (code == TEMPLATE_TYPE_PARM
1019 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1020 || code == TYPENAME_TYPE)
1021 return true;
1022 /* Also defer most attributes on dependent types. This is not
1023 necessary in all cases, but is the better default. */
1024 else if (dependent_type_p (type)
1025 /* But attribute visibility specifically works on
1026 templates. */
1027 && !is_attribute_p ("visibility", name))
1028 return true;
1029 else
1030 return false;
1032 else
1033 return false;
1036 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1037 applied at instantiation time and return them. If IS_DEPENDENT is true,
1038 the declaration itself is dependent, so all attributes should be applied
1039 at instantiation time. */
1041 static tree
1042 splice_template_attributes (tree *attr_p, tree decl)
1044 tree *p = attr_p;
1045 tree late_attrs = NULL_TREE;
1046 tree *q = &late_attrs;
1048 if (!p)
1049 return NULL_TREE;
1051 for (; *p; )
1053 if (is_late_template_attribute (*p, decl))
1055 ATTR_IS_DEPENDENT (*p) = 1;
1056 *q = *p;
1057 *p = TREE_CHAIN (*p);
1058 q = &TREE_CHAIN (*q);
1059 *q = NULL_TREE;
1061 else
1062 p = &TREE_CHAIN (*p);
1065 return late_attrs;
1068 /* Remove any late attributes from the list in ATTR_P and attach them to
1069 DECL_P. */
1071 static void
1072 save_template_attributes (tree *attr_p, tree *decl_p)
1074 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1075 tree *q;
1076 tree old_attrs = NULL_TREE;
1078 if (!late_attrs)
1079 return;
1081 /* Give this type a name so we know to look it up again at instantiation
1082 time. */
1083 if (TREE_CODE (*decl_p) == TYPE_DECL
1084 && DECL_ORIGINAL_TYPE (*decl_p) == NULL_TREE)
1086 tree oldt = TREE_TYPE (*decl_p);
1087 tree newt = build_variant_type_copy (oldt);
1088 DECL_ORIGINAL_TYPE (*decl_p) = oldt;
1089 TREE_TYPE (*decl_p) = newt;
1090 TYPE_NAME (newt) = *decl_p;
1091 TREE_USED (newt) = TREE_USED (*decl_p);
1094 if (DECL_P (*decl_p))
1095 q = &DECL_ATTRIBUTES (*decl_p);
1096 else
1097 q = &TYPE_ATTRIBUTES (*decl_p);
1099 old_attrs = *q;
1101 /* Place the late attributes at the beginning of the attribute
1102 list. */
1103 TREE_CHAIN (tree_last (late_attrs)) = *q;
1104 *q = late_attrs;
1106 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1108 /* We've added new attributes directly to the main variant, so
1109 now we need to update all of the other variants to include
1110 these new attributes. */
1111 tree variant;
1112 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1113 variant = TYPE_NEXT_VARIANT (variant))
1115 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1116 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1121 /* Like reconstruct_complex_type, but handle also template trees. */
1123 tree
1124 cp_reconstruct_complex_type (tree type, tree bottom)
1126 tree inner, outer;
1128 if (TREE_CODE (type) == POINTER_TYPE)
1130 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1131 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1132 TYPE_REF_CAN_ALIAS_ALL (type));
1134 else if (TREE_CODE (type) == REFERENCE_TYPE)
1136 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1137 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1138 TYPE_REF_CAN_ALIAS_ALL (type));
1140 else if (TREE_CODE (type) == ARRAY_TYPE)
1142 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1143 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1144 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1145 element type qualification will be handled by the recursive
1146 cp_reconstruct_complex_type call and cp_build_qualified_type
1147 for ARRAY_TYPEs changes the element type. */
1148 return outer;
1150 else if (TREE_CODE (type) == FUNCTION_TYPE)
1152 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1153 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1155 else if (TREE_CODE (type) == METHOD_TYPE)
1157 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1158 /* The build_method_type_directly() routine prepends 'this' to argument list,
1159 so we must compensate by getting rid of it. */
1160 outer
1161 = build_method_type_directly
1162 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
1163 inner,
1164 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1166 else if (TREE_CODE (type) == OFFSET_TYPE)
1168 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1169 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1171 else
1172 return bottom;
1174 return cp_build_qualified_type (outer, TYPE_QUALS (type));
1177 /* Like decl_attributes, but handle C++ complexity. */
1179 void
1180 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1182 if (*decl == NULL_TREE || *decl == void_type_node
1183 || *decl == error_mark_node
1184 || attributes == NULL_TREE)
1185 return;
1187 if (processing_template_decl)
1189 if (check_for_bare_parameter_packs (attributes))
1190 return;
1192 save_template_attributes (&attributes, decl);
1193 if (attributes == NULL_TREE)
1194 return;
1197 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1198 decl = &DECL_TEMPLATE_RESULT (*decl);
1200 decl_attributes (decl, attributes, flags);
1202 if (TREE_CODE (*decl) == TYPE_DECL)
1203 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1206 /* Walks through the namespace- or function-scope anonymous union
1207 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1208 Returns one of the fields for use in the mangled name. */
1210 static tree
1211 build_anon_union_vars (tree type, tree object)
1213 tree main_decl = NULL_TREE;
1214 tree field;
1216 /* Rather than write the code to handle the non-union case,
1217 just give an error. */
1218 if (TREE_CODE (type) != UNION_TYPE)
1219 error ("anonymous struct not inside named type");
1221 for (field = TYPE_FIELDS (type);
1222 field != NULL_TREE;
1223 field = TREE_CHAIN (field))
1225 tree decl;
1226 tree ref;
1228 if (DECL_ARTIFICIAL (field))
1229 continue;
1230 if (TREE_CODE (field) != FIELD_DECL)
1232 pedwarn ("%q+#D invalid; an anonymous union can only "
1233 "have non-static data members", field);
1234 continue;
1237 if (TREE_PRIVATE (field))
1238 pedwarn ("private member %q+#D in anonymous union", field);
1239 else if (TREE_PROTECTED (field))
1240 pedwarn ("protected member %q+#D in anonymous union", field);
1242 if (processing_template_decl)
1243 ref = build_min_nt (COMPONENT_REF, object,
1244 DECL_NAME (field), NULL_TREE);
1245 else
1246 ref = build_class_member_access_expr (object, field, NULL_TREE,
1247 false, tf_warning_or_error);
1249 if (DECL_NAME (field))
1251 tree base;
1253 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1254 DECL_ANON_UNION_VAR_P (decl) = 1;
1256 base = get_base_address (object);
1257 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1258 TREE_STATIC (decl) = TREE_STATIC (base);
1259 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1261 SET_DECL_VALUE_EXPR (decl, ref);
1262 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1264 decl = pushdecl (decl);
1266 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1267 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1268 else
1269 decl = 0;
1271 if (main_decl == NULL_TREE)
1272 main_decl = decl;
1275 return main_decl;
1278 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1279 anonymous union, then all members must be laid out together. PUBLIC_P
1280 is nonzero if this union is not declared static. */
1282 void
1283 finish_anon_union (tree anon_union_decl)
1285 tree type;
1286 tree main_decl;
1287 bool public_p;
1289 if (anon_union_decl == error_mark_node)
1290 return;
1292 type = TREE_TYPE (anon_union_decl);
1293 public_p = TREE_PUBLIC (anon_union_decl);
1295 /* The VAR_DECL's context is the same as the TYPE's context. */
1296 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1298 if (TYPE_FIELDS (type) == NULL_TREE)
1299 return;
1301 if (public_p)
1303 error ("namespace-scope anonymous aggregates must be static");
1304 return;
1307 main_decl = build_anon_union_vars (type, anon_union_decl);
1308 if (main_decl == error_mark_node)
1309 return;
1310 if (main_decl == NULL_TREE)
1312 warning (0, "anonymous union with no members");
1313 return;
1316 if (!processing_template_decl)
1318 /* Use main_decl to set the mangled name. */
1319 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1320 mangle_decl (anon_union_decl);
1321 DECL_NAME (anon_union_decl) = NULL_TREE;
1324 pushdecl (anon_union_decl);
1325 if (building_stmt_tree ()
1326 && at_function_scope_p ())
1327 add_decl_expr (anon_union_decl);
1328 else if (!processing_template_decl)
1329 rest_of_decl_compilation (anon_union_decl,
1330 toplevel_bindings_p (), at_eof);
1333 /* Auxiliary functions to make type signatures for
1334 `operator new' and `operator delete' correspond to
1335 what compiler will be expecting. */
1337 tree
1338 coerce_new_type (tree type)
1340 int e = 0;
1341 tree args = TYPE_ARG_TYPES (type);
1343 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1345 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1347 e = 1;
1348 error ("%<operator new%> must return type %qT", ptr_type_node);
1351 if (args && args != void_list_node)
1353 if (TREE_PURPOSE (args))
1355 /* [basic.stc.dynamic.allocation]
1357 The first parameter shall not have an associated default
1358 argument. */
1359 error ("the first parameter of %<operator new%> cannot "
1360 "have a default argument");
1361 /* Throw away the default argument. */
1362 TREE_PURPOSE (args) = NULL_TREE;
1365 if (!same_type_p (TREE_VALUE (args), size_type_node))
1367 e = 2;
1368 args = TREE_CHAIN (args);
1371 else
1372 e = 2;
1374 if (e == 2)
1375 pedwarn ("%<operator new%> takes type %<size_t%> (%qT) "
1376 "as first parameter", size_type_node);
1378 switch (e)
1380 case 2:
1381 args = tree_cons (NULL_TREE, size_type_node, args);
1382 /* Fall through. */
1383 case 1:
1384 type = build_exception_variant
1385 (build_function_type (ptr_type_node, args),
1386 TYPE_RAISES_EXCEPTIONS (type));
1387 /* Fall through. */
1388 default:;
1390 return type;
1393 tree
1394 coerce_delete_type (tree type)
1396 int e = 0;
1397 tree args = TYPE_ARG_TYPES (type);
1399 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1401 if (!same_type_p (TREE_TYPE (type), void_type_node))
1403 e = 1;
1404 error ("%<operator delete%> must return type %qT", void_type_node);
1407 if (!args || args == void_list_node
1408 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1410 e = 2;
1411 if (args && args != void_list_node)
1412 args = TREE_CHAIN (args);
1413 error ("%<operator delete%> takes type %qT as first parameter",
1414 ptr_type_node);
1416 switch (e)
1418 case 2:
1419 args = tree_cons (NULL_TREE, ptr_type_node, args);
1420 /* Fall through. */
1421 case 1:
1422 type = build_exception_variant
1423 (build_function_type (void_type_node, args),
1424 TYPE_RAISES_EXCEPTIONS (type));
1425 /* Fall through. */
1426 default:;
1429 return type;
1432 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1433 and mark them as needed. */
1435 static void
1436 mark_vtable_entries (tree decl)
1438 tree fnaddr;
1439 unsigned HOST_WIDE_INT idx;
1441 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1442 idx, fnaddr)
1444 tree fn;
1446 STRIP_NOPS (fnaddr);
1448 if (TREE_CODE (fnaddr) != ADDR_EXPR
1449 && TREE_CODE (fnaddr) != FDESC_EXPR)
1450 /* This entry is an offset: a virtual base class offset, a
1451 virtual call offset, an RTTI offset, etc. */
1452 continue;
1454 fn = TREE_OPERAND (fnaddr, 0);
1455 TREE_ADDRESSABLE (fn) = 1;
1456 /* When we don't have vcall offsets, we output thunks whenever
1457 we output the vtables that contain them. With vcall offsets,
1458 we know all the thunks we'll need when we emit a virtual
1459 function, so we emit the thunks there instead. */
1460 if (DECL_THUNK_P (fn))
1461 use_thunk (fn, /*emit_p=*/0);
1462 mark_used (fn);
1466 /* Set DECL up to have the closest approximation of "initialized common"
1467 linkage available. */
1469 void
1470 comdat_linkage (tree decl)
1472 if (flag_weak)
1473 make_decl_one_only (decl);
1474 else if (TREE_CODE (decl) == FUNCTION_DECL
1475 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1476 /* We can just emit function and compiler-generated variables
1477 statically; having multiple copies is (for the most part) only
1478 a waste of space.
1480 There are two correctness issues, however: the address of a
1481 template instantiation with external linkage should be the
1482 same, independent of what translation unit asks for the
1483 address, and this will not hold when we emit multiple copies of
1484 the function. However, there's little else we can do.
1486 Also, by default, the typeinfo implementation assumes that
1487 there will be only one copy of the string used as the name for
1488 each type. Therefore, if weak symbols are unavailable, the
1489 run-time library should perform a more conservative check; it
1490 should perform a string comparison, rather than an address
1491 comparison. */
1492 TREE_PUBLIC (decl) = 0;
1493 else
1495 /* Static data member template instantiations, however, cannot
1496 have multiple copies. */
1497 if (DECL_INITIAL (decl) == 0
1498 || DECL_INITIAL (decl) == error_mark_node)
1499 DECL_COMMON (decl) = 1;
1500 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1502 DECL_COMMON (decl) = 1;
1503 DECL_INITIAL (decl) = error_mark_node;
1505 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1507 /* We can't do anything useful; leave vars for explicit
1508 instantiation. */
1509 DECL_EXTERNAL (decl) = 1;
1510 DECL_NOT_REALLY_EXTERN (decl) = 0;
1514 if (DECL_LANG_SPECIFIC (decl))
1515 DECL_COMDAT (decl) = 1;
1518 /* For win32 we also want to put explicit instantiations in
1519 linkonce sections, so that they will be merged with implicit
1520 instantiations; otherwise we get duplicate symbol errors.
1521 For Darwin we do not want explicit instantiations to be
1522 linkonce. */
1524 void
1525 maybe_make_one_only (tree decl)
1527 /* We used to say that this was not necessary on targets that support weak
1528 symbols, because the implicit instantiations will defer to the explicit
1529 one. However, that's not actually the case in SVR4; a strong definition
1530 after a weak one is an error. Also, not making explicit
1531 instantiations one_only means that we can end up with two copies of
1532 some template instantiations. */
1533 if (! flag_weak)
1534 return;
1536 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1537 we can get away with not emitting them if they aren't used. We need
1538 to for variables so that cp_finish_decl will update their linkage,
1539 because their DECL_INITIAL may not have been set properly yet. */
1541 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1542 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1543 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1545 make_decl_one_only (decl);
1547 if (TREE_CODE (decl) == VAR_DECL)
1549 DECL_COMDAT (decl) = 1;
1550 /* Mark it needed so we don't forget to emit it. */
1551 mark_decl_referenced (decl);
1556 /* Determine whether or not we want to specifically import or export CTYPE,
1557 using various heuristics. */
1559 static void
1560 import_export_class (tree ctype)
1562 /* -1 for imported, 1 for exported. */
1563 int import_export = 0;
1565 /* It only makes sense to call this function at EOF. The reason is
1566 that this function looks at whether or not the first non-inline
1567 non-abstract virtual member function has been defined in this
1568 translation unit. But, we can't possibly know that until we've
1569 seen the entire translation unit. */
1570 gcc_assert (at_eof);
1572 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1573 return;
1575 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1576 we will have CLASSTYPE_INTERFACE_ONLY set but not
1577 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1578 heuristic because someone will supply a #pragma implementation
1579 elsewhere, and deducing it here would produce a conflict. */
1580 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1581 return;
1583 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1584 import_export = -1;
1585 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1586 import_export = 1;
1587 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1588 && !flag_implicit_templates)
1589 /* For a template class, without -fimplicit-templates, check the
1590 repository. If the virtual table is assigned to this
1591 translation unit, then export the class; otherwise, import
1592 it. */
1593 import_export = repo_export_class_p (ctype) ? 1 : -1;
1594 else if (TYPE_POLYMORPHIC_P (ctype))
1596 /* The ABI specifies that the virtual table and associated
1597 information are emitted with the key method, if any. */
1598 tree method = CLASSTYPE_KEY_METHOD (ctype);
1599 /* If weak symbol support is not available, then we must be
1600 careful not to emit the vtable when the key function is
1601 inline. An inline function can be defined in multiple
1602 translation units. If we were to emit the vtable in each
1603 translation unit containing a definition, we would get
1604 multiple definition errors at link-time. */
1605 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1606 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1609 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1610 a definition anywhere else. */
1611 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1612 import_export = 0;
1614 /* Allow back ends the chance to overrule the decision. */
1615 if (targetm.cxx.import_export_class)
1616 import_export = targetm.cxx.import_export_class (ctype, import_export);
1618 if (import_export)
1620 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1621 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1625 /* Return true if VAR has already been provided to the back end; in that
1626 case VAR should not be modified further by the front end. */
1627 static bool
1628 var_finalized_p (tree var)
1630 return varpool_node (var)->finalized;
1633 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1634 must be emitted in this translation unit. Mark it as such. */
1636 void
1637 mark_needed (tree decl)
1639 /* It's possible that we no longer need to set
1640 TREE_SYMBOL_REFERENCED here directly, but doing so is
1641 harmless. */
1642 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1643 mark_decl_referenced (decl);
1646 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1647 returns true if a definition of this entity should be provided in
1648 this object file. Callers use this function to determine whether
1649 or not to let the back end know that a definition of DECL is
1650 available in this translation unit. */
1652 bool
1653 decl_needed_p (tree decl)
1655 gcc_assert (TREE_CODE (decl) == VAR_DECL
1656 || TREE_CODE (decl) == FUNCTION_DECL);
1657 /* This function should only be called at the end of the translation
1658 unit. We cannot be sure of whether or not something will be
1659 COMDAT until that point. */
1660 gcc_assert (at_eof);
1662 /* All entities with external linkage that are not COMDAT should be
1663 emitted; they may be referred to from other object files. */
1664 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1665 return true;
1666 /* If this entity was used, let the back end see it; it will decide
1667 whether or not to emit it into the object file. */
1668 if (TREE_USED (decl)
1669 || (DECL_ASSEMBLER_NAME_SET_P (decl)
1670 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
1671 return true;
1672 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1673 reference to DECL might cause it to be emitted later. */
1674 return false;
1677 /* If necessary, write out the vtables for the dynamic class CTYPE.
1678 Returns true if any vtables were emitted. */
1680 static bool
1681 maybe_emit_vtables (tree ctype)
1683 tree vtbl;
1684 tree primary_vtbl;
1685 int needed = 0;
1687 /* If the vtables for this class have already been emitted there is
1688 nothing more to do. */
1689 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1690 if (var_finalized_p (primary_vtbl))
1691 return false;
1692 /* Ignore dummy vtables made by get_vtable_decl. */
1693 if (TREE_TYPE (primary_vtbl) == void_type_node)
1694 return false;
1696 /* On some targets, we cannot determine the key method until the end
1697 of the translation unit -- which is when this function is
1698 called. */
1699 if (!targetm.cxx.key_method_may_be_inline ())
1700 determine_key_method (ctype);
1702 /* See if any of the vtables are needed. */
1703 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1705 import_export_decl (vtbl);
1706 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1707 needed = 1;
1709 if (!needed)
1711 /* If the references to this class' vtables are optimized away,
1712 still emit the appropriate debugging information. See
1713 dfs_debug_mark. */
1714 if (DECL_COMDAT (primary_vtbl)
1715 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1716 note_debug_info_needed (ctype);
1717 return false;
1720 /* The ABI requires that we emit all of the vtables if we emit any
1721 of them. */
1722 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1724 /* Mark entities references from the virtual table as used. */
1725 mark_vtable_entries (vtbl);
1727 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1729 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl));
1731 /* It had better be all done at compile-time. */
1732 gcc_assert (!expr);
1735 /* Write it out. */
1736 DECL_EXTERNAL (vtbl) = 0;
1737 rest_of_decl_compilation (vtbl, 1, 1);
1739 /* Because we're only doing syntax-checking, we'll never end up
1740 actually marking the variable as written. */
1741 if (flag_syntax_only)
1742 TREE_ASM_WRITTEN (vtbl) = 1;
1745 /* Since we're writing out the vtable here, also write the debug
1746 info. */
1747 note_debug_info_needed (ctype);
1749 return true;
1752 /* A special return value from type_visibility meaning internal
1753 linkage. */
1755 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
1757 /* walk_tree helper function for type_visibility. */
1759 static tree
1760 min_vis_r (tree *tp, int *walk_subtrees, void *data)
1762 int *vis_p = (int *)data;
1763 if (! TYPE_P (*tp))
1765 *walk_subtrees = 0;
1767 else if (CLASS_TYPE_P (*tp))
1769 if (!TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
1771 *vis_p = VISIBILITY_ANON;
1772 return *tp;
1774 else if (CLASSTYPE_VISIBILITY (*tp) > *vis_p)
1775 *vis_p = CLASSTYPE_VISIBILITY (*tp);
1777 return NULL;
1780 /* Returns the visibility of TYPE, which is the minimum visibility of its
1781 component types. */
1783 static int
1784 type_visibility (tree type)
1786 int vis = VISIBILITY_DEFAULT;
1787 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
1788 return vis;
1791 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
1792 specified (or if VISIBILITY is static). */
1794 static bool
1795 constrain_visibility (tree decl, int visibility)
1797 if (visibility == VISIBILITY_ANON)
1799 /* extern "C" declarations aren't affected by the anonymous
1800 namespace. */
1801 if (!DECL_EXTERN_C_P (decl))
1803 TREE_PUBLIC (decl) = 0;
1804 DECL_ONE_ONLY (decl) = 0;
1805 DECL_INTERFACE_KNOWN (decl) = 1;
1806 if (DECL_LANG_SPECIFIC (decl))
1807 DECL_NOT_REALLY_EXTERN (decl) = 1;
1810 else if (visibility > DECL_VISIBILITY (decl)
1811 && !DECL_VISIBILITY_SPECIFIED (decl))
1813 DECL_VISIBILITY (decl) = visibility;
1814 return true;
1816 return false;
1819 /* Constrain the visibility of DECL based on the visibility of its template
1820 arguments. */
1822 static void
1823 constrain_visibility_for_template (tree decl, tree targs)
1825 /* If this is a template instantiation, check the innermost
1826 template args for visibility constraints. The outer template
1827 args are covered by the class check. */
1828 tree args = INNERMOST_TEMPLATE_ARGS (targs);
1829 int i;
1830 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
1832 int vis = 0;
1834 tree arg = TREE_VEC_ELT (args, i-1);
1835 if (TYPE_P (arg))
1836 vis = type_visibility (arg);
1837 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
1839 STRIP_NOPS (arg);
1840 if (TREE_CODE (arg) == ADDR_EXPR)
1841 arg = TREE_OPERAND (arg, 0);
1842 if (TREE_CODE (arg) == VAR_DECL
1843 || TREE_CODE (arg) == FUNCTION_DECL)
1845 if (! TREE_PUBLIC (arg))
1846 vis = VISIBILITY_ANON;
1847 else
1848 vis = DECL_VISIBILITY (arg);
1851 if (vis)
1852 constrain_visibility (decl, vis);
1856 /* Like c_determine_visibility, but with additional C++-specific
1857 behavior.
1859 Function-scope entities can rely on the function's visibility because
1860 it is set in start_preparsed_function.
1862 Class-scope entities cannot rely on the class's visibility until the end
1863 of the enclosing class definition.
1865 Note that because namespaces have multiple independent definitions,
1866 namespace visibility is handled elsewhere using the #pragma visibility
1867 machinery rather than by decorating the namespace declaration.
1869 The goal is for constraints from the type to give a diagnostic, and
1870 other constraints to be applied silently. */
1872 void
1873 determine_visibility (tree decl)
1875 tree class_type = NULL_TREE;
1876 bool use_template;
1878 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
1880 /* Only relevant for names with external linkage. */
1881 if (!TREE_PUBLIC (decl))
1882 return;
1884 /* Cloned constructors and destructors get the same visibility as
1885 the underlying function. That should be set up in
1886 maybe_clone_body. */
1887 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
1889 if (TREE_CODE (decl) == TYPE_DECL)
1891 if (CLASS_TYPE_P (TREE_TYPE (decl)))
1892 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
1893 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
1894 use_template = 1;
1895 else
1896 use_template = 0;
1898 else if (DECL_LANG_SPECIFIC (decl))
1899 use_template = DECL_USE_TEMPLATE (decl);
1900 else
1901 use_template = 0;
1903 /* If DECL is a member of a class, visibility specifiers on the
1904 class can influence the visibility of the DECL. */
1905 if (DECL_CLASS_SCOPE_P (decl))
1906 class_type = DECL_CONTEXT (decl);
1907 else
1909 /* Not a class member. */
1911 /* Virtual tables have DECL_CONTEXT set to their associated class,
1912 so they are automatically handled above. */
1913 gcc_assert (TREE_CODE (decl) != VAR_DECL
1914 || !DECL_VTABLE_OR_VTT_P (decl));
1916 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
1918 /* Local statics and classes get the visibility of their
1919 containing function by default, except that
1920 -fvisibility-inlines-hidden doesn't affect them. */
1921 tree fn = DECL_CONTEXT (decl);
1922 if (DECL_VISIBILITY_SPECIFIED (fn) || ! DECL_CLASS_SCOPE_P (fn))
1924 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
1925 DECL_VISIBILITY_SPECIFIED (decl) =
1926 DECL_VISIBILITY_SPECIFIED (fn);
1928 else
1929 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
1931 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
1932 but have no TEMPLATE_INFO, so don't try to check it. */
1933 use_template = 0;
1935 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl)
1936 && flag_visibility_ms_compat)
1938 /* Under -fvisibility-ms-compat, types are visible by default,
1939 even though their contents aren't. */
1940 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
1941 int underlying_vis = type_visibility (underlying_type);
1942 if (underlying_vis == VISIBILITY_ANON
1943 || CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type))
1944 constrain_visibility (decl, underlying_vis);
1945 else
1946 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1948 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
1950 /* tinfo visibility is based on the type it's for. */
1951 constrain_visibility
1952 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))));
1954 else if (use_template)
1955 /* Template instantiations and specializations get visibility based
1956 on their template unless they override it with an attribute. */;
1957 else if (! DECL_VISIBILITY_SPECIFIED (decl))
1959 /* Set default visibility to whatever the user supplied with
1960 #pragma GCC visibility or a namespace visibility attribute. */
1961 DECL_VISIBILITY (decl) = default_visibility;
1962 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
1966 if (use_template)
1968 /* If the specialization doesn't specify visibility, use the
1969 visibility from the template. */
1970 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
1971 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
1972 : DECL_TEMPLATE_INFO (decl));
1973 tree args = TI_ARGS (tinfo);
1975 if (args != error_mark_node)
1977 int depth = TMPL_ARGS_DEPTH (args);
1978 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
1980 if (!DECL_VISIBILITY_SPECIFIED (decl))
1982 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
1983 DECL_VISIBILITY_SPECIFIED (decl)
1984 = DECL_VISIBILITY_SPECIFIED (pattern);
1987 /* FIXME should TMPL_ARGS_DEPTH really return 1 for null input? */
1988 if (args && depth > template_class_depth (class_type))
1989 /* Limit visibility based on its template arguments. */
1990 constrain_visibility_for_template (decl, args);
1994 if (class_type)
1995 determine_visibility_from_class (decl, class_type);
1997 if (decl_anon_ns_mem_p (decl))
1998 /* Names in an anonymous namespace get internal linkage.
1999 This might change once we implement export. */
2000 constrain_visibility (decl, VISIBILITY_ANON);
2001 else if (TREE_CODE (decl) != TYPE_DECL)
2003 /* Propagate anonymity from type to decl. */
2004 int tvis = type_visibility (TREE_TYPE (decl));
2005 if (tvis == VISIBILITY_ANON
2006 || ! DECL_VISIBILITY_SPECIFIED (decl))
2007 constrain_visibility (decl, tvis);
2011 /* By default, static data members and function members receive
2012 the visibility of their containing class. */
2014 static void
2015 determine_visibility_from_class (tree decl, tree class_type)
2017 if (DECL_VISIBILITY_SPECIFIED (decl))
2018 return;
2020 if (visibility_options.inlines_hidden
2021 /* Don't do this for inline templates; specializations might not be
2022 inline, and we don't want them to inherit the hidden
2023 visibility. We'll set it here for all inline instantiations. */
2024 && !processing_template_decl
2025 && TREE_CODE (decl) == FUNCTION_DECL
2026 && DECL_DECLARED_INLINE_P (decl)
2027 && (! DECL_LANG_SPECIFIC (decl)
2028 || ! DECL_EXPLICIT_INSTANTIATION (decl)))
2029 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2030 else
2032 /* Default to the class visibility. */
2033 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2034 DECL_VISIBILITY_SPECIFIED (decl)
2035 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2038 /* Give the target a chance to override the visibility associated
2039 with DECL. */
2040 if (TREE_CODE (decl) == VAR_DECL
2041 && (DECL_TINFO_P (decl)
2042 || (DECL_VTABLE_OR_VTT_P (decl)
2043 /* Construction virtual tables are not exported because
2044 they cannot be referred to from other object files;
2045 their name is not standardized by the ABI. */
2046 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2047 && TREE_PUBLIC (decl)
2048 && !DECL_REALLY_EXTERN (decl)
2049 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2050 targetm.cxx.determine_class_data_visibility (decl);
2053 /* Constrain the visibility of a class TYPE based on the visibility of its
2054 field types. Warn if any fields require lesser visibility. */
2056 void
2057 constrain_class_visibility (tree type)
2059 tree binfo;
2060 tree t;
2061 int i;
2063 int vis = type_visibility (type);
2065 if (vis == VISIBILITY_ANON
2066 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2067 return;
2069 /* Don't warn about visibility if the class has explicit visibility. */
2070 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2071 vis = VISIBILITY_INTERNAL;
2073 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
2074 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2076 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2077 int subvis = type_visibility (ftype);
2079 if (subvis == VISIBILITY_ANON)
2081 if (!in_main_input_context ())
2082 warning (0, "\
2083 %qT has a field %qD whose type uses the anonymous namespace",
2084 type, t);
2086 else if (MAYBE_CLASS_TYPE_P (ftype)
2087 && vis < VISIBILITY_HIDDEN
2088 && subvis >= VISIBILITY_HIDDEN)
2089 warning (OPT_Wattributes, "\
2090 %qT declared with greater visibility than the type of its field %qD",
2091 type, t);
2094 binfo = TYPE_BINFO (type);
2095 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2097 int subvis = type_visibility (TREE_TYPE (t));
2099 if (subvis == VISIBILITY_ANON)
2101 if (!in_main_input_context())
2102 warning (0, "\
2103 %qT has a base %qT whose type uses the anonymous namespace",
2104 type, TREE_TYPE (t));
2106 else if (vis < VISIBILITY_HIDDEN
2107 && subvis >= VISIBILITY_HIDDEN)
2108 warning (OPT_Wattributes, "\
2109 %qT declared with greater visibility than its base %qT",
2110 type, TREE_TYPE (t));
2114 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2115 for DECL has not already been determined, do so now by setting
2116 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2117 function is called entities with vague linkage whose definitions
2118 are available must have TREE_PUBLIC set.
2120 If this function decides to place DECL in COMDAT, it will set
2121 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2122 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2123 callers defer that decision until it is clear that DECL is actually
2124 required. */
2126 void
2127 import_export_decl (tree decl)
2129 int emit_p;
2130 bool comdat_p;
2131 bool import_p;
2132 tree class_type = NULL_TREE;
2134 if (DECL_INTERFACE_KNOWN (decl))
2135 return;
2137 /* We cannot determine what linkage to give to an entity with vague
2138 linkage until the end of the file. For example, a virtual table
2139 for a class will be defined if and only if the key method is
2140 defined in this translation unit. As a further example, consider
2141 that when compiling a translation unit that uses PCH file with
2142 "-frepo" it would be incorrect to make decisions about what
2143 entities to emit when building the PCH; those decisions must be
2144 delayed until the repository information has been processed. */
2145 gcc_assert (at_eof);
2146 /* Object file linkage for explicit instantiations is handled in
2147 mark_decl_instantiated. For static variables in functions with
2148 vague linkage, maybe_commonize_var is used.
2150 Therefore, the only declarations that should be provided to this
2151 function are those with external linkage that are:
2153 * implicit instantiations of function templates
2155 * inline function
2157 * implicit instantiations of static data members of class
2158 templates
2160 * virtual tables
2162 * typeinfo objects
2164 Furthermore, all entities that reach this point must have a
2165 definition available in this translation unit.
2167 The following assertions check these conditions. */
2168 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
2169 || TREE_CODE (decl) == VAR_DECL);
2170 /* Any code that creates entities with TREE_PUBLIC cleared should
2171 also set DECL_INTERFACE_KNOWN. */
2172 gcc_assert (TREE_PUBLIC (decl));
2173 if (TREE_CODE (decl) == FUNCTION_DECL)
2174 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2175 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2176 || DECL_DECLARED_INLINE_P (decl));
2177 else
2178 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2179 || DECL_VTABLE_OR_VTT_P (decl)
2180 || DECL_TINFO_P (decl));
2181 /* Check that a definition of DECL is available in this translation
2182 unit. */
2183 gcc_assert (!DECL_REALLY_EXTERN (decl));
2185 /* Assume that DECL will not have COMDAT linkage. */
2186 comdat_p = false;
2187 /* Assume that DECL will not be imported into this translation
2188 unit. */
2189 import_p = false;
2191 /* See if the repository tells us whether or not to emit DECL in
2192 this translation unit. */
2193 emit_p = repo_emit_p (decl);
2194 if (emit_p == 0)
2195 import_p = true;
2196 else if (emit_p == 1)
2198 /* The repository indicates that this entity should be defined
2199 here. Make sure the back end honors that request. */
2200 if (TREE_CODE (decl) == VAR_DECL)
2201 mark_needed (decl);
2202 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2203 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2205 tree clone;
2206 FOR_EACH_CLONE (clone, decl)
2207 mark_needed (clone);
2209 else
2210 mark_needed (decl);
2211 /* Output the definition as an ordinary strong definition. */
2212 DECL_EXTERNAL (decl) = 0;
2213 DECL_INTERFACE_KNOWN (decl) = 1;
2214 return;
2217 if (import_p)
2218 /* We have already decided what to do with this DECL; there is no
2219 need to check anything further. */
2221 else if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
2223 class_type = DECL_CONTEXT (decl);
2224 import_export_class (class_type);
2225 if (TYPE_FOR_JAVA (class_type))
2226 import_p = true;
2227 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2228 && CLASSTYPE_INTERFACE_ONLY (class_type))
2229 import_p = true;
2230 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2231 && !CLASSTYPE_USE_TEMPLATE (class_type)
2232 && CLASSTYPE_KEY_METHOD (class_type)
2233 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2234 /* The ABI requires that all virtual tables be emitted with
2235 COMDAT linkage. However, on systems where COMDAT symbols
2236 don't show up in the table of contents for a static
2237 archive, or on systems without weak symbols (where we
2238 approximate COMDAT linkage by using internal linkage), the
2239 linker will report errors about undefined symbols because
2240 it will not see the virtual table definition. Therefore,
2241 in the case that we know that the virtual table will be
2242 emitted in only one translation unit, we make the virtual
2243 table an ordinary definition with external linkage. */
2244 DECL_EXTERNAL (decl) = 0;
2245 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2247 /* CLASS_TYPE is being exported from this translation unit,
2248 so DECL should be defined here. */
2249 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2250 /* If a class is declared in a header with the "extern
2251 template" extension, then it will not be instantiated,
2252 even in translation units that would normally require
2253 it. Often such classes are explicitly instantiated in
2254 one translation unit. Therefore, the explicit
2255 instantiation must be made visible to other translation
2256 units. */
2257 DECL_EXTERNAL (decl) = 0;
2258 else
2260 /* The generic C++ ABI says that class data is always
2261 COMDAT, even if there is a key function. Some
2262 variants (e.g., the ARM EABI) says that class data
2263 only has COMDAT linkage if the class data might be
2264 emitted in more than one translation unit. When the
2265 key method can be inline and is inline, we still have
2266 to arrange for comdat even though
2267 class_data_always_comdat is false. */
2268 if (!CLASSTYPE_KEY_METHOD (class_type)
2269 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2270 || targetm.cxx.class_data_always_comdat ())
2272 /* The ABI requires COMDAT linkage. Normally, we
2273 only emit COMDAT things when they are needed;
2274 make sure that we realize that this entity is
2275 indeed needed. */
2276 comdat_p = true;
2277 mark_needed (decl);
2281 else if (!flag_implicit_templates
2282 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2283 import_p = true;
2284 else
2285 comdat_p = true;
2287 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2289 tree type = TREE_TYPE (DECL_NAME (decl));
2290 if (CLASS_TYPE_P (type))
2292 class_type = type;
2293 import_export_class (type);
2294 if (CLASSTYPE_INTERFACE_KNOWN (type)
2295 && TYPE_POLYMORPHIC_P (type)
2296 && CLASSTYPE_INTERFACE_ONLY (type)
2297 /* If -fno-rtti was specified, then we cannot be sure
2298 that RTTI information will be emitted with the
2299 virtual table of the class, so we must emit it
2300 wherever it is used. */
2301 && flag_rtti)
2302 import_p = true;
2303 else
2305 if (CLASSTYPE_INTERFACE_KNOWN (type)
2306 && !CLASSTYPE_INTERFACE_ONLY (type))
2308 comdat_p = (targetm.cxx.class_data_always_comdat ()
2309 || (CLASSTYPE_KEY_METHOD (type)
2310 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2311 mark_needed (decl);
2312 if (!flag_weak)
2314 comdat_p = false;
2315 DECL_EXTERNAL (decl) = 0;
2318 else
2319 comdat_p = true;
2322 else
2323 comdat_p = true;
2325 else if (DECL_TEMPLATE_INSTANTIATION (decl)
2326 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2328 /* DECL is an implicit instantiation of a function or static
2329 data member. */
2330 if ((flag_implicit_templates
2331 && !flag_use_repository)
2332 || (flag_implicit_inline_templates
2333 && TREE_CODE (decl) == FUNCTION_DECL
2334 && DECL_DECLARED_INLINE_P (decl)))
2335 comdat_p = true;
2336 else
2337 /* If we are not implicitly generating templates, then mark
2338 this entity as undefined in this translation unit. */
2339 import_p = true;
2341 else if (DECL_FUNCTION_MEMBER_P (decl))
2343 if (!DECL_DECLARED_INLINE_P (decl))
2345 tree ctype = DECL_CONTEXT (decl);
2346 import_export_class (ctype);
2347 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2349 DECL_NOT_REALLY_EXTERN (decl)
2350 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2351 || (DECL_DECLARED_INLINE_P (decl)
2352 && ! flag_implement_inlines
2353 && !DECL_VINDEX (decl)));
2355 if (!DECL_NOT_REALLY_EXTERN (decl))
2356 DECL_EXTERNAL (decl) = 1;
2358 /* Always make artificials weak. */
2359 if (DECL_ARTIFICIAL (decl) && flag_weak)
2360 comdat_p = true;
2361 else
2362 maybe_make_one_only (decl);
2365 else
2366 comdat_p = true;
2368 else
2369 comdat_p = true;
2371 if (import_p)
2373 /* If we are importing DECL into this translation unit, mark is
2374 an undefined here. */
2375 DECL_EXTERNAL (decl) = 1;
2376 DECL_NOT_REALLY_EXTERN (decl) = 0;
2378 else if (comdat_p)
2380 /* If we decided to put DECL in COMDAT, mark it accordingly at
2381 this point. */
2382 comdat_linkage (decl);
2385 DECL_INTERFACE_KNOWN (decl) = 1;
2388 /* Return an expression that performs the destruction of DECL, which
2389 must be a VAR_DECL whose type has a non-trivial destructor, or is
2390 an array whose (innermost) elements have a non-trivial destructor. */
2392 tree
2393 build_cleanup (tree decl)
2395 tree temp;
2396 tree type = TREE_TYPE (decl);
2398 /* This function should only be called for declarations that really
2399 require cleanups. */
2400 gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type));
2402 /* Treat all objects with destructors as used; the destructor may do
2403 something substantive. */
2404 mark_used (decl);
2406 if (TREE_CODE (type) == ARRAY_TYPE)
2407 temp = decl;
2408 else
2409 temp = build_address (decl);
2410 temp = build_delete (TREE_TYPE (temp), temp,
2411 sfk_complete_destructor,
2412 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2413 return temp;
2416 /* Returns the initialization guard variable for the variable DECL,
2417 which has static storage duration. */
2419 tree
2420 get_guard (tree decl)
2422 tree sname;
2423 tree guard;
2425 sname = mangle_guard_variable (decl);
2426 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2427 if (! guard)
2429 tree guard_type;
2431 /* We use a type that is big enough to contain a mutex as well
2432 as an integer counter. */
2433 guard_type = targetm.cxx.guard_type ();
2434 guard = build_decl (VAR_DECL, sname, guard_type);
2436 /* The guard should have the same linkage as what it guards. */
2437 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2438 TREE_STATIC (guard) = TREE_STATIC (decl);
2439 DECL_COMMON (guard) = DECL_COMMON (decl);
2440 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2441 if (TREE_PUBLIC (decl))
2442 DECL_WEAK (guard) = DECL_WEAK (decl);
2443 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2444 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2446 DECL_ARTIFICIAL (guard) = 1;
2447 DECL_IGNORED_P (guard) = 1;
2448 TREE_USED (guard) = 1;
2449 pushdecl_top_level_and_finish (guard, NULL_TREE);
2451 return guard;
2454 /* Return those bits of the GUARD variable that should be set when the
2455 guarded entity is actually initialized. */
2457 static tree
2458 get_guard_bits (tree guard)
2460 if (!targetm.cxx.guard_mask_bit ())
2462 /* We only set the first byte of the guard, in order to leave room
2463 for a mutex in the high-order bits. */
2464 guard = build1 (ADDR_EXPR,
2465 build_pointer_type (TREE_TYPE (guard)),
2466 guard);
2467 guard = build1 (NOP_EXPR,
2468 build_pointer_type (char_type_node),
2469 guard);
2470 guard = build1 (INDIRECT_REF, char_type_node, guard);
2473 return guard;
2476 /* Return an expression which determines whether or not the GUARD
2477 variable has already been initialized. */
2479 tree
2480 get_guard_cond (tree guard)
2482 tree guard_value;
2484 /* Check to see if the GUARD is zero. */
2485 guard = get_guard_bits (guard);
2487 /* Mask off all but the low bit. */
2488 if (targetm.cxx.guard_mask_bit ())
2490 guard_value = integer_one_node;
2491 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2492 guard_value = convert (TREE_TYPE (guard), guard_value);
2493 guard = cp_build_binary_op (BIT_AND_EXPR, guard, guard_value,
2494 tf_warning_or_error);
2497 guard_value = integer_zero_node;
2498 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2499 guard_value = convert (TREE_TYPE (guard), guard_value);
2500 return cp_build_binary_op (EQ_EXPR, guard, guard_value,
2501 tf_warning_or_error);
2504 /* Return an expression which sets the GUARD variable, indicating that
2505 the variable being guarded has been initialized. */
2507 tree
2508 set_guard (tree guard)
2510 tree guard_init;
2512 /* Set the GUARD to one. */
2513 guard = get_guard_bits (guard);
2514 guard_init = integer_one_node;
2515 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2516 guard_init = convert (TREE_TYPE (guard), guard_init);
2517 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2518 tf_warning_or_error);
2521 /* Start the process of running a particular set of global constructors
2522 or destructors. Subroutine of do_[cd]tors. */
2524 static tree
2525 start_objects (int method_type, int initp)
2527 tree body;
2528 tree fndecl;
2529 char type[10];
2531 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2533 if (initp != DEFAULT_INIT_PRIORITY)
2535 char joiner;
2537 #ifdef JOINER
2538 joiner = JOINER;
2539 #else
2540 joiner = '_';
2541 #endif
2543 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2545 else
2546 sprintf (type, "%c", method_type);
2548 fndecl = build_lang_decl (FUNCTION_DECL,
2549 get_file_function_name (type),
2550 build_function_type (void_type_node,
2551 void_list_node));
2552 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
2554 TREE_PUBLIC (current_function_decl) = 0;
2556 /* Mark as artificial because it's not explicitly in the user's
2557 source code. */
2558 DECL_ARTIFICIAL (current_function_decl) = 1;
2560 /* Mark this declaration as used to avoid spurious warnings. */
2561 TREE_USED (current_function_decl) = 1;
2563 /* Mark this function as a global constructor or destructor. */
2564 if (method_type == 'I')
2565 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2566 else
2567 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2568 DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
2570 body = begin_compound_stmt (BCS_FN_BODY);
2572 return body;
2575 /* Finish the process of running a particular set of global constructors
2576 or destructors. Subroutine of do_[cd]tors. */
2578 static void
2579 finish_objects (int method_type, int initp, tree body)
2581 tree fn;
2583 /* Finish up. */
2584 finish_compound_stmt (body);
2585 fn = finish_function (0);
2587 if (method_type == 'I')
2589 DECL_STATIC_CONSTRUCTOR (fn) = 1;
2590 decl_init_priority_insert (fn, initp);
2592 else
2594 DECL_STATIC_DESTRUCTOR (fn) = 1;
2595 decl_fini_priority_insert (fn, initp);
2598 expand_or_defer_fn (fn);
2601 /* The names of the parameters to the function created to handle
2602 initializations and destructions for objects with static storage
2603 duration. */
2604 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2605 #define PRIORITY_IDENTIFIER "__priority"
2607 /* The name of the function we create to handle initializations and
2608 destructions for objects with static storage duration. */
2609 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2611 /* The declaration for the __INITIALIZE_P argument. */
2612 static GTY(()) tree initialize_p_decl;
2614 /* The declaration for the __PRIORITY argument. */
2615 static GTY(()) tree priority_decl;
2617 /* The declaration for the static storage duration function. */
2618 static GTY(()) tree ssdf_decl;
2620 /* All the static storage duration functions created in this
2621 translation unit. */
2622 static GTY(()) VEC(tree,gc) *ssdf_decls;
2624 /* A map from priority levels to information about that priority
2625 level. There may be many such levels, so efficient lookup is
2626 important. */
2627 static splay_tree priority_info_map;
2629 /* Begins the generation of the function that will handle all
2630 initialization and destruction of objects with static storage
2631 duration. The function generated takes two parameters of type
2632 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2633 nonzero, it performs initializations. Otherwise, it performs
2634 destructions. It only performs those initializations or
2635 destructions with the indicated __PRIORITY. The generated function
2636 returns no value.
2638 It is assumed that this function will only be called once per
2639 translation unit. */
2641 static tree
2642 start_static_storage_duration_function (unsigned count)
2644 tree parm_types;
2645 tree type;
2646 tree body;
2647 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2649 /* Create the identifier for this function. It will be of the form
2650 SSDF_IDENTIFIER_<number>. */
2651 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
2653 /* Create the parameters. */
2654 parm_types = void_list_node;
2655 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2656 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2657 type = build_function_type (void_type_node, parm_types);
2659 /* Create the FUNCTION_DECL itself. */
2660 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2661 get_identifier (id),
2662 type);
2663 TREE_PUBLIC (ssdf_decl) = 0;
2664 DECL_ARTIFICIAL (ssdf_decl) = 1;
2665 DECL_INLINE (ssdf_decl) = 1;
2667 /* Put this function in the list of functions to be called from the
2668 static constructors and destructors. */
2669 if (!ssdf_decls)
2671 ssdf_decls = VEC_alloc (tree, gc, 32);
2673 /* Take this opportunity to initialize the map from priority
2674 numbers to information about that priority level. */
2675 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2676 /*delete_key_fn=*/0,
2677 /*delete_value_fn=*/
2678 (splay_tree_delete_value_fn) &free);
2680 /* We always need to generate functions for the
2681 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2682 priorities later, we'll be sure to find the
2683 DEFAULT_INIT_PRIORITY. */
2684 get_priority_info (DEFAULT_INIT_PRIORITY);
2687 VEC_safe_push (tree, gc, ssdf_decls, ssdf_decl);
2689 /* Create the argument list. */
2690 initialize_p_decl = cp_build_parm_decl
2691 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2692 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2693 TREE_USED (initialize_p_decl) = 1;
2694 priority_decl = cp_build_parm_decl
2695 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2696 DECL_CONTEXT (priority_decl) = ssdf_decl;
2697 TREE_USED (priority_decl) = 1;
2699 TREE_CHAIN (initialize_p_decl) = priority_decl;
2700 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2702 /* Put the function in the global scope. */
2703 pushdecl (ssdf_decl);
2705 /* Start the function itself. This is equivalent to declaring the
2706 function as:
2708 static void __ssdf (int __initialize_p, init __priority_p);
2710 It is static because we only need to call this function from the
2711 various constructor and destructor functions for this module. */
2712 start_preparsed_function (ssdf_decl,
2713 /*attrs=*/NULL_TREE,
2714 SF_PRE_PARSED);
2716 /* Set up the scope of the outermost block in the function. */
2717 body = begin_compound_stmt (BCS_FN_BODY);
2719 return body;
2722 /* Finish the generation of the function which performs initialization
2723 and destruction of objects with static storage duration. After
2724 this point, no more such objects can be created. */
2726 static void
2727 finish_static_storage_duration_function (tree body)
2729 /* Close out the function. */
2730 finish_compound_stmt (body);
2731 expand_or_defer_fn (finish_function (0));
2734 /* Return the information about the indicated PRIORITY level. If no
2735 code to handle this level has yet been generated, generate the
2736 appropriate prologue. */
2738 static priority_info
2739 get_priority_info (int priority)
2741 priority_info pi;
2742 splay_tree_node n;
2744 n = splay_tree_lookup (priority_info_map,
2745 (splay_tree_key) priority);
2746 if (!n)
2748 /* Create a new priority information structure, and insert it
2749 into the map. */
2750 pi = XNEW (struct priority_info_s);
2751 pi->initializations_p = 0;
2752 pi->destructions_p = 0;
2753 splay_tree_insert (priority_info_map,
2754 (splay_tree_key) priority,
2755 (splay_tree_value) pi);
2757 else
2758 pi = (priority_info) n->value;
2760 return pi;
2763 /* The effective initialization priority of a DECL. */
2765 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
2766 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
2767 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
2769 /* Whether a DECL needs a guard to protect it against multiple
2770 initialization. */
2772 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
2773 || DECL_ONE_ONLY (decl) \
2774 || DECL_WEAK (decl)))
2776 /* Set up to handle the initialization or destruction of DECL. If
2777 INITP is nonzero, we are initializing the variable. Otherwise, we
2778 are destroying it. */
2780 static void
2781 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
2783 tree guard_if_stmt = NULL_TREE;
2784 tree guard;
2786 /* If we are supposed to destruct and there's a trivial destructor,
2787 nothing has to be done. */
2788 if (!initp
2789 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2790 return;
2792 /* Trick the compiler into thinking we are at the file and line
2793 where DECL was declared so that error-messages make sense, and so
2794 that the debugger will show somewhat sensible file and line
2795 information. */
2796 input_location = DECL_SOURCE_LOCATION (decl);
2798 /* Because of:
2800 [class.access.spec]
2802 Access control for implicit calls to the constructors,
2803 the conversion functions, or the destructor called to
2804 create and destroy a static data member is performed as
2805 if these calls appeared in the scope of the member's
2806 class.
2808 we pretend we are in a static member function of the class of
2809 which the DECL is a member. */
2810 if (member_p (decl))
2812 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2813 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2816 /* Assume we don't need a guard. */
2817 guard = NULL_TREE;
2818 /* We need a guard if this is an object with external linkage that
2819 might be initialized in more than one place. (For example, a
2820 static data member of a template, when the data member requires
2821 construction.) */
2822 if (NEEDS_GUARD_P (decl))
2824 tree guard_cond;
2826 guard = get_guard (decl);
2828 /* When using __cxa_atexit, we just check the GUARD as we would
2829 for a local static. */
2830 if (flag_use_cxa_atexit)
2832 /* When using __cxa_atexit, we never try to destroy
2833 anything from a static destructor. */
2834 gcc_assert (initp);
2835 guard_cond = get_guard_cond (guard);
2837 /* If we don't have __cxa_atexit, then we will be running
2838 destructors from .fini sections, or their equivalents. So,
2839 we need to know how many times we've tried to initialize this
2840 object. We do initializations only if the GUARD is zero,
2841 i.e., if we are the first to initialize the variable. We do
2842 destructions only if the GUARD is one, i.e., if we are the
2843 last to destroy the variable. */
2844 else if (initp)
2845 guard_cond
2846 = cp_build_binary_op (EQ_EXPR,
2847 cp_build_unary_op (PREINCREMENT_EXPR,
2848 guard,
2849 /*noconvert=*/1,
2850 tf_warning_or_error),
2851 integer_one_node,
2852 tf_warning_or_error);
2853 else
2854 guard_cond
2855 = cp_build_binary_op (EQ_EXPR,
2856 cp_build_unary_op (PREDECREMENT_EXPR,
2857 guard,
2858 /*noconvert=*/1,
2859 tf_warning_or_error),
2860 integer_zero_node,
2861 tf_warning_or_error);
2863 guard_if_stmt = begin_if_stmt ();
2864 finish_if_stmt_cond (guard_cond, guard_if_stmt);
2868 /* If we're using __cxa_atexit, we have not already set the GUARD,
2869 so we must do so now. */
2870 if (guard && initp && flag_use_cxa_atexit)
2871 finish_expr_stmt (set_guard (guard));
2873 /* Perform the initialization or destruction. */
2874 if (initp)
2876 if (init)
2877 finish_expr_stmt (init);
2879 /* If we're using __cxa_atexit, register a function that calls the
2880 destructor for the object. */
2881 if (flag_use_cxa_atexit)
2882 finish_expr_stmt (register_dtor_fn (decl));
2884 else
2885 finish_expr_stmt (build_cleanup (decl));
2887 /* Finish the guard if-stmt, if necessary. */
2888 if (guard)
2890 finish_then_clause (guard_if_stmt);
2891 finish_if_stmt (guard_if_stmt);
2894 /* Now that we're done with DECL we don't need to pretend to be a
2895 member of its class any longer. */
2896 DECL_CONTEXT (current_function_decl) = NULL_TREE;
2897 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
2900 /* Generate code to do the initialization or destruction of the decls in VARS,
2901 a TREE_LIST of VAR_DECL with static storage duration.
2902 Whether initialization or destruction is performed is specified by INITP. */
2904 static void
2905 do_static_initialization_or_destruction (tree vars, bool initp)
2907 tree node, init_if_stmt, cond;
2909 /* Build the outer if-stmt to check for initialization or destruction. */
2910 init_if_stmt = begin_if_stmt ();
2911 cond = initp ? integer_one_node : integer_zero_node;
2912 cond = cp_build_binary_op (EQ_EXPR,
2913 initialize_p_decl,
2914 cond,
2915 tf_warning_or_error);
2916 finish_if_stmt_cond (cond, init_if_stmt);
2918 node = vars;
2919 do {
2920 tree decl = TREE_VALUE (node);
2921 tree priority_if_stmt;
2922 int priority;
2923 priority_info pi;
2925 /* If we don't need a destructor, there's nothing to do. Avoid
2926 creating a possibly empty if-stmt. */
2927 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2929 node = TREE_CHAIN (node);
2930 continue;
2933 /* Remember that we had an initialization or finalization at this
2934 priority. */
2935 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
2936 pi = get_priority_info (priority);
2937 if (initp)
2938 pi->initializations_p = 1;
2939 else
2940 pi->destructions_p = 1;
2942 /* Conditionalize this initialization on being in the right priority
2943 and being initializing/finalizing appropriately. */
2944 priority_if_stmt = begin_if_stmt ();
2945 cond = cp_build_binary_op (EQ_EXPR,
2946 priority_decl,
2947 build_int_cst (NULL_TREE, priority),
2948 tf_warning_or_error);
2949 finish_if_stmt_cond (cond, priority_if_stmt);
2951 /* Process initializers with same priority. */
2952 for (; node
2953 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
2954 node = TREE_CHAIN (node))
2955 /* Do one initialization or destruction. */
2956 one_static_initialization_or_destruction (TREE_VALUE (node),
2957 TREE_PURPOSE (node), initp);
2959 /* Finish up the priority if-stmt body. */
2960 finish_then_clause (priority_if_stmt);
2961 finish_if_stmt (priority_if_stmt);
2963 } while (node);
2965 /* Finish up the init/destruct if-stmt body. */
2966 finish_then_clause (init_if_stmt);
2967 finish_if_stmt (init_if_stmt);
2970 /* VARS is a list of variables with static storage duration which may
2971 need initialization and/or finalization. Remove those variables
2972 that don't really need to be initialized or finalized, and return
2973 the resulting list. The order in which the variables appear in
2974 VARS is in reverse order of the order in which they should actually
2975 be initialized. The list we return is in the unreversed order;
2976 i.e., the first variable should be initialized first. */
2978 static tree
2979 prune_vars_needing_no_initialization (tree *vars)
2981 tree *var = vars;
2982 tree result = NULL_TREE;
2984 while (*var)
2986 tree t = *var;
2987 tree decl = TREE_VALUE (t);
2988 tree init = TREE_PURPOSE (t);
2990 /* Deal gracefully with error. */
2991 if (decl == error_mark_node)
2993 var = &TREE_CHAIN (t);
2994 continue;
2997 /* The only things that can be initialized are variables. */
2998 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3000 /* If this object is not defined, we don't need to do anything
3001 here. */
3002 if (DECL_EXTERNAL (decl))
3004 var = &TREE_CHAIN (t);
3005 continue;
3008 /* Also, if the initializer already contains errors, we can bail
3009 out now. */
3010 if (init && TREE_CODE (init) == TREE_LIST
3011 && value_member (error_mark_node, init))
3013 var = &TREE_CHAIN (t);
3014 continue;
3017 /* This variable is going to need initialization and/or
3018 finalization, so we add it to the list. */
3019 *var = TREE_CHAIN (t);
3020 TREE_CHAIN (t) = result;
3021 result = t;
3024 return result;
3027 /* Make sure we have told the back end about all the variables in
3028 VARS. */
3030 static void
3031 write_out_vars (tree vars)
3033 tree v;
3035 for (v = vars; v; v = TREE_CHAIN (v))
3037 tree var = TREE_VALUE (v);
3038 if (!var_finalized_p (var))
3040 import_export_decl (var);
3041 rest_of_decl_compilation (var, 1, 1);
3046 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3047 (otherwise) that will initialize all global objects with static
3048 storage duration having the indicated PRIORITY. */
3050 static void
3051 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3052 location_t *locus)
3054 char function_key;
3055 tree arguments;
3056 tree fndecl;
3057 tree body;
3058 size_t i;
3060 input_location = *locus;
3061 /* ??? */
3062 /* Was: locus->line++; */
3064 /* We use `I' to indicate initialization and `D' to indicate
3065 destruction. */
3066 function_key = constructor_p ? 'I' : 'D';
3068 /* We emit the function lazily, to avoid generating empty
3069 global constructors and destructors. */
3070 body = NULL_TREE;
3072 /* For Objective-C++, we may need to initialize metadata found in this module.
3073 This must be done _before_ any other static initializations. */
3074 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3075 && constructor_p && objc_static_init_needed_p ())
3077 body = start_objects (function_key, priority);
3078 objc_generate_static_init_call (NULL_TREE);
3081 /* Call the static storage duration function with appropriate
3082 arguments. */
3083 for (i = 0; VEC_iterate (tree, ssdf_decls, i, fndecl); ++i)
3085 /* Calls to pure or const functions will expand to nothing. */
3086 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3088 if (! body)
3089 body = start_objects (function_key, priority);
3091 arguments = tree_cons (NULL_TREE,
3092 build_int_cst (NULL_TREE, priority),
3093 NULL_TREE);
3094 arguments = tree_cons (NULL_TREE,
3095 build_int_cst (NULL_TREE, constructor_p),
3096 arguments);
3097 finish_expr_stmt (cp_build_function_call (fndecl, arguments,
3098 tf_warning_or_error));
3102 /* Close out the function. */
3103 if (body)
3104 finish_objects (function_key, priority, body);
3107 /* Generate constructor and destructor functions for the priority
3108 indicated by N. */
3110 static int
3111 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3113 location_t *locus = (location_t *) data;
3114 int priority = (int) n->key;
3115 priority_info pi = (priority_info) n->value;
3117 /* Generate the functions themselves, but only if they are really
3118 needed. */
3119 if (pi->initializations_p)
3120 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3121 if (pi->destructions_p)
3122 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3124 /* Keep iterating. */
3125 return 0;
3128 /* Called via LANGHOOK_CALLGRAPH_ANALYZE_EXPR. It is supposed to mark
3129 decls referenced from front-end specific constructs; it will be called
3130 only for language-specific tree nodes.
3132 Here we must deal with member pointers. */
3134 tree
3135 cxx_callgraph_analyze_expr (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED)
3137 tree t = *tp;
3139 switch (TREE_CODE (t))
3141 case PTRMEM_CST:
3142 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3143 cgraph_mark_needed_node (cgraph_node (PTRMEM_CST_MEMBER (t)));
3144 break;
3145 case BASELINK:
3146 if (TREE_CODE (BASELINK_FUNCTIONS (t)) == FUNCTION_DECL)
3147 cgraph_mark_needed_node (cgraph_node (BASELINK_FUNCTIONS (t)));
3148 break;
3149 case VAR_DECL:
3150 if (DECL_VTABLE_OR_VTT_P (t))
3152 /* The ABI requires that all virtual tables be emitted
3153 whenever one of them is. */
3154 tree vtbl;
3155 for (vtbl = CLASSTYPE_VTABLES (DECL_CONTEXT (t));
3156 vtbl;
3157 vtbl = TREE_CHAIN (vtbl))
3158 mark_decl_referenced (vtbl);
3160 else if (DECL_CONTEXT (t)
3161 && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
3162 /* If we need a static variable in a function, then we
3163 need the containing function. */
3164 mark_decl_referenced (DECL_CONTEXT (t));
3165 break;
3166 default:
3167 break;
3170 return NULL;
3173 /* Java requires that we be able to reference a local address for a
3174 method, and not be confused by PLT entries. If hidden aliases are
3175 supported, emit one for each java function that we've emitted. */
3177 static void
3178 build_java_method_aliases (void)
3180 struct cgraph_node *node;
3182 #ifndef HAVE_GAS_HIDDEN
3183 return;
3184 #endif
3186 for (node = cgraph_nodes; node ; node = node->next)
3188 tree fndecl = node->decl;
3190 if (TREE_ASM_WRITTEN (fndecl)
3191 && DECL_CONTEXT (fndecl)
3192 && TYPE_P (DECL_CONTEXT (fndecl))
3193 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3194 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3196 /* Mangle the name in a predictable way; we need to reference
3197 this from a java compiled object file. */
3198 tree oid, nid, alias;
3199 const char *oname;
3200 char *nname;
3202 oid = DECL_ASSEMBLER_NAME (fndecl);
3203 oname = IDENTIFIER_POINTER (oid);
3204 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3205 nname = ACONCAT (("_ZGA", oname+2, NULL));
3206 nid = get_identifier (nname);
3208 alias = make_alias_for (fndecl, nid);
3209 TREE_PUBLIC (alias) = 1;
3210 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3212 assemble_alias (alias, oid);
3217 /* This routine is called at the end of compilation.
3218 Its job is to create all the code needed to initialize and
3219 destroy the global aggregates. We do the destruction
3220 first, since that way we only need to reverse the decls once. */
3222 void
3223 cp_write_global_declarations (void)
3225 tree vars;
3226 bool reconsider;
3227 size_t i;
3228 location_t locus;
3229 unsigned ssdf_count = 0;
3230 int retries = 0;
3231 tree decl;
3233 locus = input_location;
3234 at_eof = 1;
3236 /* Bad parse errors. Just forget about it. */
3237 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3238 return;
3240 if (pch_file)
3241 c_common_write_pch ();
3243 /* FIXME - huh? was input_line -= 1;*/
3245 /* We now have to write out all the stuff we put off writing out.
3246 These include:
3248 o Template specializations that we have not yet instantiated,
3249 but which are needed.
3250 o Initialization and destruction for non-local objects with
3251 static storage duration. (Local objects with static storage
3252 duration are initialized when their scope is first entered,
3253 and are cleaned up via atexit.)
3254 o Virtual function tables.
3256 All of these may cause others to be needed. For example,
3257 instantiating one function may cause another to be needed, and
3258 generating the initializer for an object may cause templates to be
3259 instantiated, etc., etc. */
3261 timevar_push (TV_VARCONST);
3263 emit_support_tinfos ();
3267 tree t;
3268 tree decl;
3270 reconsider = false;
3272 /* If there are templates that we've put off instantiating, do
3273 them now. */
3274 instantiate_pending_templates (retries);
3275 ggc_collect ();
3277 /* Write out virtual tables as required. Note that writing out
3278 the virtual table for a template class may cause the
3279 instantiation of members of that class. If we write out
3280 vtables then we remove the class from our list so we don't
3281 have to look at it again. */
3283 while (keyed_classes != NULL_TREE
3284 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
3286 reconsider = true;
3287 keyed_classes = TREE_CHAIN (keyed_classes);
3290 t = keyed_classes;
3291 if (t != NULL_TREE)
3293 tree next = TREE_CHAIN (t);
3295 while (next)
3297 if (maybe_emit_vtables (TREE_VALUE (next)))
3299 reconsider = true;
3300 TREE_CHAIN (t) = TREE_CHAIN (next);
3302 else
3303 t = next;
3305 next = TREE_CHAIN (t);
3309 /* Write out needed type info variables. We have to be careful
3310 looping through unemitted decls, because emit_tinfo_decl may
3311 cause other variables to be needed. New elements will be
3312 appended, and we remove from the vector those that actually
3313 get emitted. */
3314 for (i = VEC_length (tree, unemitted_tinfo_decls);
3315 VEC_iterate (tree, unemitted_tinfo_decls, --i, t);)
3316 if (emit_tinfo_decl (t))
3318 reconsider = true;
3319 VEC_unordered_remove (tree, unemitted_tinfo_decls, i);
3322 /* The list of objects with static storage duration is built up
3323 in reverse order. We clear STATIC_AGGREGATES so that any new
3324 aggregates added during the initialization of these will be
3325 initialized in the correct order when we next come around the
3326 loop. */
3327 vars = prune_vars_needing_no_initialization (&static_aggregates);
3329 if (vars)
3331 /* We need to start a new initialization function each time
3332 through the loop. That's because we need to know which
3333 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3334 isn't computed until a function is finished, and written
3335 out. That's a deficiency in the back end. When this is
3336 fixed, these initialization functions could all become
3337 inline, with resulting performance improvements. */
3338 tree ssdf_body;
3340 /* Set the line and file, so that it is obviously not from
3341 the source file. */
3342 input_location = locus;
3343 ssdf_body = start_static_storage_duration_function (ssdf_count);
3345 /* Make sure the back end knows about all the variables. */
3346 write_out_vars (vars);
3348 /* First generate code to do all the initializations. */
3349 if (vars)
3350 do_static_initialization_or_destruction (vars, /*initp=*/true);
3352 /* Then, generate code to do all the destructions. Do these
3353 in reverse order so that the most recently constructed
3354 variable is the first destroyed. If we're using
3355 __cxa_atexit, then we don't need to do this; functions
3356 were registered at initialization time to destroy the
3357 local statics. */
3358 if (!flag_use_cxa_atexit && vars)
3360 vars = nreverse (vars);
3361 do_static_initialization_or_destruction (vars, /*initp=*/false);
3363 else
3364 vars = NULL_TREE;
3366 /* Finish up the static storage duration function for this
3367 round. */
3368 input_location = locus;
3369 finish_static_storage_duration_function (ssdf_body);
3371 /* All those initializations and finalizations might cause
3372 us to need more inline functions, more template
3373 instantiations, etc. */
3374 reconsider = true;
3375 ssdf_count++;
3376 /* ??? was: locus.line++; */
3379 /* Go through the set of inline functions whose bodies have not
3380 been emitted yet. If out-of-line copies of these functions
3381 are required, emit them. */
3382 for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3384 /* Does it need synthesizing? */
3385 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3386 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3388 /* Even though we're already at the top-level, we push
3389 there again. That way, when we pop back a few lines
3390 hence, all of our state is restored. Otherwise,
3391 finish_function doesn't clean things up, and we end
3392 up with CURRENT_FUNCTION_DECL set. */
3393 push_to_top_level ();
3394 /* The decl's location will mark where it was first
3395 needed. Save that so synthesize method can indicate
3396 where it was needed from, in case of error */
3397 input_location = DECL_SOURCE_LOCATION (decl);
3398 synthesize_method (decl);
3399 pop_from_top_level ();
3400 reconsider = true;
3403 if (!gimple_body (decl))
3404 continue;
3406 /* We lie to the back end, pretending that some functions
3407 are not defined when they really are. This keeps these
3408 functions from being put out unnecessarily. But, we must
3409 stop lying when the functions are referenced, or if they
3410 are not comdat since they need to be put out now. If
3411 DECL_INTERFACE_KNOWN, then we have already set
3412 DECL_EXTERNAL appropriately, so there's no need to check
3413 again, and we do not want to clear DECL_EXTERNAL if a
3414 previous call to import_export_decl set it.
3416 This is done in a separate for cycle, because if some
3417 deferred function is contained in another deferred
3418 function later in deferred_fns varray,
3419 rest_of_compilation would skip this function and we
3420 really cannot expand the same function twice. */
3421 import_export_decl (decl);
3422 if (DECL_NOT_REALLY_EXTERN (decl)
3423 && DECL_INITIAL (decl)
3424 && decl_needed_p (decl))
3425 DECL_EXTERNAL (decl) = 0;
3427 /* If we're going to need to write this function out, and
3428 there's already a body for it, create RTL for it now.
3429 (There might be no body if this is a method we haven't
3430 gotten around to synthesizing yet.) */
3431 if (!DECL_EXTERNAL (decl)
3432 && decl_needed_p (decl)
3433 && !TREE_ASM_WRITTEN (decl)
3434 && !cgraph_node (decl)->local.finalized)
3436 /* We will output the function; no longer consider it in this
3437 loop. */
3438 DECL_DEFER_OUTPUT (decl) = 0;
3439 /* Generate RTL for this function now that we know we
3440 need it. */
3441 expand_or_defer_fn (decl);
3442 /* If we're compiling -fsyntax-only pretend that this
3443 function has been written out so that we don't try to
3444 expand it again. */
3445 if (flag_syntax_only)
3446 TREE_ASM_WRITTEN (decl) = 1;
3447 reconsider = true;
3451 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3452 reconsider = true;
3454 /* Static data members are just like namespace-scope globals. */
3455 for (i = 0; VEC_iterate (tree, pending_statics, i, decl); ++i)
3457 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
3458 /* Don't write it out if we haven't seen a definition. */
3459 || DECL_IN_AGGR_P (decl))
3460 continue;
3461 import_export_decl (decl);
3462 /* If this static data member is needed, provide it to the
3463 back end. */
3464 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
3465 DECL_EXTERNAL (decl) = 0;
3467 if (VEC_length (tree, pending_statics) != 0
3468 && wrapup_global_declarations (VEC_address (tree, pending_statics),
3469 VEC_length (tree, pending_statics)))
3470 reconsider = true;
3472 retries++;
3474 while (reconsider);
3476 /* All used inline functions must have a definition at this point. */
3477 for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3479 if (/* Check online inline functions that were actually used. */
3480 TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
3481 /* If the definition actually was available here, then the
3482 fact that the function was not defined merely represents
3483 that for some reason (use of a template repository,
3484 #pragma interface, etc.) we decided not to emit the
3485 definition here. */
3486 && !DECL_INITIAL (decl)
3487 /* An explicit instantiation can be used to specify
3488 that the body is in another unit. It will have
3489 already verified there was a definition. */
3490 && !DECL_EXPLICIT_INSTANTIATION (decl))
3492 warning (0, "inline function %q+D used but never defined", decl);
3493 /* Avoid a duplicate warning from check_global_declaration_1. */
3494 TREE_NO_WARNING (decl) = 1;
3498 /* We give C linkage to static constructors and destructors. */
3499 push_lang_context (lang_name_c);
3501 /* Generate initialization and destruction functions for all
3502 priorities for which they are required. */
3503 if (priority_info_map)
3504 splay_tree_foreach (priority_info_map,
3505 generate_ctor_and_dtor_functions_for_priority,
3506 /*data=*/&locus);
3507 else if (c_dialect_objc () && objc_static_init_needed_p ())
3508 /* If this is obj-c++ and we need a static init, call
3509 generate_ctor_or_dtor_function. */
3510 generate_ctor_or_dtor_function (/*constructor_p=*/true,
3511 DEFAULT_INIT_PRIORITY, &locus);
3513 /* We're done with the splay-tree now. */
3514 if (priority_info_map)
3515 splay_tree_delete (priority_info_map);
3517 /* Generate any missing aliases. */
3518 maybe_apply_pending_pragma_weaks ();
3520 /* We're done with static constructors, so we can go back to "C++"
3521 linkage now. */
3522 pop_lang_context ();
3524 cgraph_finalize_compilation_unit ();
3525 cgraph_optimize ();
3527 /* Now, issue warnings about static, but not defined, functions,
3528 etc., and emit debugging information. */
3529 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3530 if (VEC_length (tree, pending_statics) != 0)
3532 check_global_declarations (VEC_address (tree, pending_statics),
3533 VEC_length (tree, pending_statics));
3534 emit_debug_global_declarations (VEC_address (tree, pending_statics),
3535 VEC_length (tree, pending_statics));
3538 /* Generate hidden aliases for Java. */
3539 build_java_method_aliases ();
3541 finish_repo ();
3543 /* The entire file is now complete. If requested, dump everything
3544 to a file. */
3546 int flags;
3547 FILE *stream = dump_begin (TDI_tu, &flags);
3549 if (stream)
3551 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3552 dump_end (TDI_tu, stream);
3556 timevar_pop (TV_VARCONST);
3558 if (flag_detailed_statistics)
3560 dump_tree_statistics ();
3561 dump_time_statistics ();
3563 input_location = locus;
3565 #ifdef ENABLE_CHECKING
3566 validate_conversion_obstack ();
3567 #endif /* ENABLE_CHECKING */
3570 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
3571 function to call in parse-tree form; it has not yet been
3572 semantically analyzed. ARGS are the arguments to the function.
3573 They have already been semantically analyzed. */
3575 tree
3576 build_offset_ref_call_from_tree (tree fn, tree args)
3578 tree orig_fn;
3579 tree orig_args;
3580 tree expr;
3581 tree object;
3583 orig_fn = fn;
3584 orig_args = args;
3585 object = TREE_OPERAND (fn, 0);
3587 if (processing_template_decl)
3589 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
3590 || TREE_CODE (fn) == MEMBER_REF);
3591 if (type_dependent_expression_p (fn)
3592 || any_type_dependent_arguments_p (args))
3593 return build_nt_call_list (fn, args);
3595 /* Transform the arguments and add the implicit "this"
3596 parameter. That must be done before the FN is transformed
3597 because we depend on the form of FN. */
3598 args = build_non_dependent_args (args);
3599 object = build_non_dependent_expr (object);
3600 if (TREE_CODE (fn) == DOTSTAR_EXPR)
3601 object = cp_build_unary_op (ADDR_EXPR, object, 0, tf_warning_or_error);
3602 args = tree_cons (NULL_TREE, object, args);
3603 /* Now that the arguments are done, transform FN. */
3604 fn = build_non_dependent_expr (fn);
3607 /* A qualified name corresponding to a bound pointer-to-member is
3608 represented as an OFFSET_REF:
3610 struct B { void g(); };
3611 void (B::*p)();
3612 void B::g() { (this->*p)(); } */
3613 if (TREE_CODE (fn) == OFFSET_REF)
3615 tree object_addr = cp_build_unary_op (ADDR_EXPR, object, 0,
3616 tf_warning_or_error);
3617 fn = TREE_OPERAND (fn, 1);
3618 fn = get_member_function_from_ptrfunc (&object_addr, fn);
3619 args = tree_cons (NULL_TREE, object_addr, args);
3622 expr = cp_build_function_call (fn, args, tf_warning_or_error);
3623 if (processing_template_decl && expr != error_mark_node)
3624 return build_min_non_dep_call_list (expr, orig_fn, orig_args);
3625 return expr;
3629 void
3630 check_default_args (tree x)
3632 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
3633 bool saw_def = false;
3634 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
3635 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
3637 if (TREE_PURPOSE (arg))
3638 saw_def = true;
3639 else if (saw_def)
3641 error ("default argument missing for parameter %P of %q+#D", i, x);
3642 TREE_PURPOSE (arg) = error_mark_node;
3647 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
3648 If DECL is a specialization or implicitly declared class member,
3649 generate the actual definition. */
3651 void
3652 mark_used (tree decl)
3654 HOST_WIDE_INT saved_processing_template_decl = 0;
3656 /* If DECL is a BASELINK for a single function, then treat it just
3657 like the DECL for the function. Otherwise, if the BASELINK is
3658 for an overloaded function, we don't know which function was
3659 actually used until after overload resolution. */
3660 if (TREE_CODE (decl) == BASELINK)
3662 decl = BASELINK_FUNCTIONS (decl);
3663 if (really_overloaded_fn (decl))
3664 return;
3665 decl = OVL_CURRENT (decl);
3668 TREE_USED (decl) = 1;
3669 if (DECL_CLONED_FUNCTION_P (decl))
3670 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
3671 /* If we don't need a value, then we don't need to synthesize DECL. */
3672 if (skip_evaluation)
3673 return;
3674 /* Normally, we can wait until instantiation-time to synthesize
3675 DECL. However, if DECL is a static data member initialized with
3676 a constant, we need the value right now because a reference to
3677 such a data member is not value-dependent. */
3678 if (TREE_CODE (decl) == VAR_DECL
3679 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
3680 && DECL_CLASS_SCOPE_P (decl))
3682 /* Don't try to instantiate members of dependent types. We
3683 cannot just use dependent_type_p here because this function
3684 may be called from fold_non_dependent_expr, and then we may
3685 see dependent types, even though processing_template_decl
3686 will not be set. */
3687 if (CLASSTYPE_TEMPLATE_INFO ((DECL_CONTEXT (decl)))
3688 && uses_template_parms (CLASSTYPE_TI_ARGS (DECL_CONTEXT (decl))))
3689 return;
3690 /* Pretend that we are not in a template, even if we are, so
3691 that the static data member initializer will be processed. */
3692 saved_processing_template_decl = processing_template_decl;
3693 processing_template_decl = 0;
3696 if (processing_template_decl)
3697 return;
3699 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
3700 && !TREE_ASM_WRITTEN (decl))
3701 /* Remember it, so we can check it was defined. */
3703 if (DECL_DEFERRED_FN (decl))
3704 return;
3706 /* Remember the current location for a function we will end up
3707 synthesizing. Then we can inform the user where it was
3708 required in the case of error. */
3709 if (DECL_ARTIFICIAL (decl) && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
3710 && !DECL_THUNK_P (decl))
3711 DECL_SOURCE_LOCATION (decl) = input_location;
3713 note_vague_linkage_fn (decl);
3716 assemble_external (decl);
3718 /* Is it a synthesized method that needs to be synthesized? */
3719 if (TREE_CODE (decl) == FUNCTION_DECL
3720 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
3721 && DECL_ARTIFICIAL (decl)
3722 && !DECL_THUNK_P (decl)
3723 && ! DECL_INITIAL (decl)
3724 /* Kludge: don't synthesize for default args. Unfortunately this
3725 rules out initializers of namespace-scoped objects too, but
3726 it's sort-of ok if the implicit ctor or dtor decl keeps
3727 pointing to the class location. */
3728 && current_function_decl)
3730 synthesize_method (decl);
3731 /* If we've already synthesized the method we don't need to
3732 do the instantiation test below. */
3734 else if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
3735 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3736 && (!DECL_EXPLICIT_INSTANTIATION (decl)
3737 || (TREE_CODE (decl) == FUNCTION_DECL
3738 && DECL_INLINE (DECL_TEMPLATE_RESULT
3739 (template_for_substitution (decl))))
3740 /* We need to instantiate static data members so that there
3741 initializers are available in integral constant
3742 expressions. */
3743 || (TREE_CODE (decl) == VAR_DECL
3744 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))))
3745 /* If this is a function or variable that is an instance of some
3746 template, we now know that we will need to actually do the
3747 instantiation. We check that DECL is not an explicit
3748 instantiation because that is not checked in instantiate_decl.
3750 We put off instantiating functions in order to improve compile
3751 times. Maintaining a stack of active functions is expensive,
3752 and the inliner knows to instantiate any functions it might
3753 need. Therefore, we always try to defer instantiation. */
3754 instantiate_decl (decl, /*defer_ok=*/true,
3755 /*expl_inst_class_mem_p=*/false);
3757 processing_template_decl = saved_processing_template_decl;
3760 #include "gt-cp-decl2.h"