Upgrade GCC from 4.4.7 snapshot 2011-10-25 to 4.4.7-RELEASE
[dragonfly.git] / contrib / gcc-4.4 / gcc / cp / decl2.c
blob744209e87ecc49d03c3e2369e84ccfb13ba842e4
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* Process declarations and symbol lookup for C++ front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
28 /* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "decl.h"
41 #include "output.h"
42 #include "except.h"
43 #include "toplev.h"
44 #include "timevar.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "c-common.h"
48 #include "tree-mudflap.h"
49 #include "cgraph.h"
50 #include "tree-inline.h"
51 #include "c-pragma.h"
52 #include "tree-dump.h"
53 #include "intl.h"
54 #include "gimple.h"
56 extern cpp_reader *parse_in;
58 /* This structure contains information about the initializations
59 and/or destructions required for a particular priority level. */
60 typedef struct priority_info_s {
61 /* Nonzero if there have been any initializations at this priority
62 throughout the translation unit. */
63 int initializations_p;
64 /* Nonzero if there have been any destructions at this priority
65 throughout the translation unit. */
66 int destructions_p;
67 } *priority_info;
69 static void mark_vtable_entries (tree);
70 static bool maybe_emit_vtables (tree);
71 static bool acceptable_java_type (tree);
72 static tree start_objects (int, int);
73 static void finish_objects (int, int, tree);
74 static tree start_static_storage_duration_function (unsigned);
75 static void finish_static_storage_duration_function (tree);
76 static priority_info get_priority_info (int);
77 static void do_static_initialization_or_destruction (tree, bool);
78 static void one_static_initialization_or_destruction (tree, tree, bool);
79 static void generate_ctor_or_dtor_function (bool, int, location_t *);
80 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
81 void *);
82 static tree prune_vars_needing_no_initialization (tree *);
83 static void write_out_vars (tree);
84 static void import_export_class (tree);
85 static tree get_guard_bits (tree);
86 static void determine_visibility_from_class (tree, tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) VEC(tree,gc) *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) VEC(tree,gc) *deferred_fns;
97 /* Nonzero if we're done parsing and into end-of-file activities. */
99 int at_eof;
103 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
104 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
105 that apply to the function). */
107 tree
108 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals)
110 tree raises;
111 tree attrs;
112 int type_quals;
114 if (fntype == error_mark_node || ctype == error_mark_node)
115 return error_mark_node;
117 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
118 || TREE_CODE (fntype) == METHOD_TYPE);
120 type_quals = quals & ~TYPE_QUAL_RESTRICT;
121 ctype = cp_build_qualified_type (ctype, type_quals);
122 raises = TYPE_RAISES_EXCEPTIONS (fntype);
123 attrs = TYPE_ATTRIBUTES (fntype);
124 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
125 (TREE_CODE (fntype) == METHOD_TYPE
126 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
127 : TYPE_ARG_TYPES (fntype)));
128 if (raises)
129 fntype = build_exception_variant (fntype, raises);
130 if (attrs)
131 fntype = cp_build_type_attribute_variant (fntype, attrs);
133 return fntype;
136 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
137 appropriately. */
139 tree
140 cp_build_parm_decl (tree name, tree type)
142 tree parm = build_decl (PARM_DECL, name, type);
143 /* DECL_ARG_TYPE is only used by the back end and the back end never
144 sees templates. */
145 if (!processing_template_decl)
146 DECL_ARG_TYPE (parm) = type_passed_as (type);
148 /* If the type is a pack expansion, then we have a function
149 parameter pack. */
150 if (type && TREE_CODE (type) == TYPE_PACK_EXPANSION)
151 FUNCTION_PARAMETER_PACK_P (parm) = 1;
153 return parm;
156 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
157 indicated NAME. */
159 tree
160 build_artificial_parm (tree name, tree type)
162 tree parm = cp_build_parm_decl (name, type);
163 DECL_ARTIFICIAL (parm) = 1;
164 /* All our artificial parms are implicitly `const'; they cannot be
165 assigned to. */
166 TREE_READONLY (parm) = 1;
167 return parm;
170 /* Constructors for types with virtual baseclasses need an "in-charge" flag
171 saying whether this constructor is responsible for initialization of
172 virtual baseclasses or not. All destructors also need this "in-charge"
173 flag, which additionally determines whether or not the destructor should
174 free the memory for the object.
176 This function adds the "in-charge" flag to member function FN if
177 appropriate. It is called from grokclassfn and tsubst.
178 FN must be either a constructor or destructor.
180 The in-charge flag follows the 'this' parameter, and is followed by the
181 VTT parm (if any), then the user-written parms. */
183 void
184 maybe_retrofit_in_chrg (tree fn)
186 tree basetype, arg_types, parms, parm, fntype;
188 /* If we've already add the in-charge parameter don't do it again. */
189 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
190 return;
192 /* When processing templates we can't know, in general, whether or
193 not we're going to have virtual baseclasses. */
194 if (processing_template_decl)
195 return;
197 /* We don't need an in-charge parameter for constructors that don't
198 have virtual bases. */
199 if (DECL_CONSTRUCTOR_P (fn)
200 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
201 return;
203 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
204 basetype = TREE_TYPE (TREE_VALUE (arg_types));
205 arg_types = TREE_CHAIN (arg_types);
207 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
209 /* If this is a subobject constructor or destructor, our caller will
210 pass us a pointer to our VTT. */
211 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
213 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
215 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
216 TREE_CHAIN (parm) = parms;
217 parms = parm;
219 /* ...and then to TYPE_ARG_TYPES. */
220 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
222 DECL_HAS_VTT_PARM_P (fn) = 1;
225 /* Then add the in-charge parm (before the VTT parm). */
226 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
227 TREE_CHAIN (parm) = parms;
228 parms = parm;
229 arg_types = hash_tree_chain (integer_type_node, arg_types);
231 /* Insert our new parameter(s) into the list. */
232 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
234 /* And rebuild the function type. */
235 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
236 arg_types);
237 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
238 fntype = build_exception_variant (fntype,
239 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
240 TREE_TYPE (fn) = fntype;
242 /* Now we've got the in-charge parameter. */
243 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
246 /* Classes overload their constituent function names automatically.
247 When a function name is declared in a record structure,
248 its name is changed to it overloaded name. Since names for
249 constructors and destructors can conflict, we place a leading
250 '$' for destructors.
252 CNAME is the name of the class we are grokking for.
254 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
256 FLAGS contains bits saying what's special about today's
257 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
259 If FUNCTION is a destructor, then we must add the `auto-delete' field
260 as a second parameter. There is some hair associated with the fact
261 that we must "declare" this variable in the manner consistent with the
262 way the rest of the arguments were declared.
264 QUALS are the qualifiers for the this pointer. */
266 void
267 grokclassfn (tree ctype, tree function, enum overload_flags flags)
269 tree fn_name = DECL_NAME (function);
271 /* Even within an `extern "C"' block, members get C++ linkage. See
272 [dcl.link] for details. */
273 SET_DECL_LANGUAGE (function, lang_cplusplus);
275 if (fn_name == NULL_TREE)
277 error ("name missing for member function");
278 fn_name = get_identifier ("<anonymous>");
279 DECL_NAME (function) = fn_name;
282 DECL_CONTEXT (function) = ctype;
284 if (flags == DTOR_FLAG)
285 DECL_DESTRUCTOR_P (function) = 1;
287 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
288 maybe_retrofit_in_chrg (function);
291 /* Create an ARRAY_REF, checking for the user doing things backwards
292 along the way. */
294 tree
295 grok_array_decl (tree array_expr, tree index_exp)
297 tree type;
298 tree expr;
299 tree orig_array_expr = array_expr;
300 tree orig_index_exp = index_exp;
302 if (error_operand_p (array_expr) || error_operand_p (index_exp))
303 return error_mark_node;
305 if (processing_template_decl)
307 if (type_dependent_expression_p (array_expr)
308 || type_dependent_expression_p (index_exp))
309 return build_min_nt (ARRAY_REF, array_expr, index_exp,
310 NULL_TREE, NULL_TREE);
311 array_expr = build_non_dependent_expr (array_expr);
312 index_exp = build_non_dependent_expr (index_exp);
315 type = TREE_TYPE (array_expr);
316 gcc_assert (type);
317 type = non_reference (type);
319 /* If they have an `operator[]', use that. */
320 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
321 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL,
322 array_expr, index_exp, NULL_TREE,
323 /*overloaded_p=*/NULL, tf_warning_or_error);
324 else
326 tree p1, p2, i1, i2;
328 /* Otherwise, create an ARRAY_REF for a pointer or array type.
329 It is a little-known fact that, if `a' is an array and `i' is
330 an int, you can write `i[a]', which means the same thing as
331 `a[i]'. */
332 if (TREE_CODE (type) == ARRAY_TYPE)
333 p1 = array_expr;
334 else
335 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
337 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
338 p2 = index_exp;
339 else
340 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
342 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
343 false);
344 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
345 false);
347 if ((p1 && i2) && (i1 && p2))
348 error ("ambiguous conversion for array subscript");
350 if (p1 && i2)
351 array_expr = p1, index_exp = i2;
352 else if (i1 && p2)
353 array_expr = p2, index_exp = i1;
354 else
356 error ("invalid types %<%T[%T]%> for array subscript",
357 type, TREE_TYPE (index_exp));
358 return error_mark_node;
361 if (array_expr == error_mark_node || index_exp == error_mark_node)
362 error ("ambiguous conversion for array subscript");
364 expr = build_array_ref (array_expr, index_exp, input_location);
366 if (processing_template_decl && expr != error_mark_node)
367 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
368 NULL_TREE, NULL_TREE);
369 return expr;
372 /* Given the cast expression EXP, checking out its validity. Either return
373 an error_mark_node if there was an unavoidable error, return a cast to
374 void for trying to delete a pointer w/ the value 0, or return the
375 call to delete. If DOING_VEC is true, we handle things differently
376 for doing an array delete.
377 Implements ARM $5.3.4. This is called from the parser. */
379 tree
380 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete)
382 tree t, type;
384 if (exp == error_mark_node)
385 return exp;
387 if (processing_template_decl)
389 t = build_min (DELETE_EXPR, void_type_node, exp, size);
390 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
391 DELETE_EXPR_USE_VEC (t) = doing_vec;
392 TREE_SIDE_EFFECTS (t) = 1;
393 return t;
396 /* An array can't have been allocated by new, so complain. */
397 if (TREE_CODE (exp) == VAR_DECL
398 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
399 warning (0, "deleting array %q#D", exp);
401 t = build_expr_type_conversion (WANT_POINTER, exp, true);
403 if (t == NULL_TREE || t == error_mark_node)
405 error ("type %q#T argument given to %<delete%>, expected pointer",
406 TREE_TYPE (exp));
407 return error_mark_node;
410 type = TREE_TYPE (t);
412 /* As of Valley Forge, you can delete a pointer to const. */
414 /* You can't delete functions. */
415 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
417 error ("cannot delete a function. Only pointer-to-objects are "
418 "valid arguments to %<delete%>");
419 return error_mark_node;
422 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
423 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
425 warning (0, "deleting %qT is undefined", type);
426 doing_vec = 0;
429 /* Deleting a pointer with the value zero is valid and has no effect. */
430 if (integer_zerop (t))
431 return build1 (NOP_EXPR, void_type_node, t);
433 if (doing_vec)
434 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
435 sfk_deleting_destructor,
436 use_global_delete);
437 else
438 return build_delete (type, t, sfk_deleting_destructor,
439 LOOKUP_NORMAL, use_global_delete);
442 /* Report an error if the indicated template declaration is not the
443 sort of thing that should be a member template. */
445 void
446 check_member_template (tree tmpl)
448 tree decl;
450 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
451 decl = DECL_TEMPLATE_RESULT (tmpl);
453 if (TREE_CODE (decl) == FUNCTION_DECL
454 || (TREE_CODE (decl) == TYPE_DECL
455 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
457 /* The parser rejects template declarations in local classes. */
458 gcc_assert (!current_function_decl);
459 /* The parser rejects any use of virtual in a function template. */
460 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
461 && DECL_VIRTUAL_P (decl)));
463 /* The debug-information generating code doesn't know what to do
464 with member templates. */
465 DECL_IGNORED_P (tmpl) = 1;
467 else
468 error ("template declaration of %q#D", decl);
471 /* Return true iff TYPE is a valid Java parameter or return type. */
473 static bool
474 acceptable_java_type (tree type)
476 if (type == error_mark_node)
477 return false;
479 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
480 return true;
481 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
483 type = TREE_TYPE (type);
484 if (TREE_CODE (type) == RECORD_TYPE)
486 tree args; int i;
487 if (! TYPE_FOR_JAVA (type))
488 return false;
489 if (! CLASSTYPE_TEMPLATE_INFO (type))
490 return true;
491 args = CLASSTYPE_TI_ARGS (type);
492 i = TREE_VEC_LENGTH (args);
493 while (--i >= 0)
495 type = TREE_VEC_ELT (args, i);
496 if (TREE_CODE (type) == POINTER_TYPE)
497 type = TREE_TYPE (type);
498 if (! TYPE_FOR_JAVA (type))
499 return false;
501 return true;
504 return false;
507 /* For a METHOD in a Java class CTYPE, return true if
508 the parameter and return types are valid Java types.
509 Otherwise, print appropriate error messages, and return false. */
511 bool
512 check_java_method (tree method)
514 bool jerr = false;
515 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
516 tree ret_type = TREE_TYPE (TREE_TYPE (method));
518 if (!acceptable_java_type (ret_type))
520 error ("Java method %qD has non-Java return type %qT",
521 method, ret_type);
522 jerr = true;
525 arg_types = TREE_CHAIN (arg_types);
526 if (DECL_HAS_IN_CHARGE_PARM_P (method))
527 arg_types = TREE_CHAIN (arg_types);
528 if (DECL_HAS_VTT_PARM_P (method))
529 arg_types = TREE_CHAIN (arg_types);
531 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
533 tree type = TREE_VALUE (arg_types);
534 if (!acceptable_java_type (type))
536 if (type != error_mark_node)
537 error ("Java method %qD has non-Java parameter type %qT",
538 method, type);
539 jerr = true;
542 return !jerr;
545 /* Sanity check: report error if this function FUNCTION is not
546 really a member of the class (CTYPE) it is supposed to belong to.
547 TEMPLATE_PARMS is used to specify the template parameters of a member
548 template passed as FUNCTION_DECL. If the member template is passed as a
549 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
550 from the declaration. If the function is not a function template, it
551 must be NULL.
552 It returns the original declaration for the function, NULL_TREE if
553 no declaration was found, error_mark_node if an error was emitted. */
555 tree
556 check_classfn (tree ctype, tree function, tree template_parms)
558 int ix;
559 bool is_template;
560 tree pushed_scope;
562 if (DECL_USE_TEMPLATE (function)
563 && !(TREE_CODE (function) == TEMPLATE_DECL
564 && DECL_TEMPLATE_SPECIALIZATION (function))
565 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
566 /* Since this is a specialization of a member template,
567 we're not going to find the declaration in the class.
568 For example, in:
570 struct S { template <typename T> void f(T); };
571 template <> void S::f(int);
573 we're not going to find `S::f(int)', but there's no
574 reason we should, either. We let our callers know we didn't
575 find the method, but we don't complain. */
576 return NULL_TREE;
578 /* Basic sanity check: for a template function, the template parameters
579 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
580 if (TREE_CODE (function) == TEMPLATE_DECL)
582 if (template_parms
583 && !comp_template_parms (template_parms,
584 DECL_TEMPLATE_PARMS (function)))
586 error ("template parameter lists provided don't match the "
587 "template parameters of %qD", function);
588 return error_mark_node;
590 template_parms = DECL_TEMPLATE_PARMS (function);
593 /* OK, is this a definition of a member template? */
594 is_template = (template_parms != NULL_TREE);
596 /* We must enter the scope here, because conversion operators are
597 named by target type, and type equivalence relies on typenames
598 resolving within the scope of CTYPE. */
599 pushed_scope = push_scope (ctype);
600 ix = class_method_index_for_fn (complete_type (ctype), function);
601 if (ix >= 0)
603 VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (ctype);
604 tree fndecls, fndecl = 0;
605 bool is_conv_op;
606 const char *format = NULL;
608 for (fndecls = VEC_index (tree, methods, ix);
609 fndecls; fndecls = OVL_NEXT (fndecls))
611 tree p1, p2;
613 fndecl = OVL_CURRENT (fndecls);
614 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
615 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
617 /* We cannot simply call decls_match because this doesn't
618 work for static member functions that are pretending to
619 be methods, and because the name may have been changed by
620 asm("new_name"). */
622 /* Get rid of the this parameter on functions that become
623 static. */
624 if (DECL_STATIC_FUNCTION_P (fndecl)
625 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
626 p1 = TREE_CHAIN (p1);
628 /* A member template definition only matches a member template
629 declaration. */
630 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
631 continue;
633 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
634 TREE_TYPE (TREE_TYPE (fndecl)))
635 && compparms (p1, p2)
636 && (!is_template
637 || comp_template_parms (template_parms,
638 DECL_TEMPLATE_PARMS (fndecl)))
639 && (DECL_TEMPLATE_SPECIALIZATION (function)
640 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
641 && (!DECL_TEMPLATE_SPECIALIZATION (function)
642 || (DECL_TI_TEMPLATE (function)
643 == DECL_TI_TEMPLATE (fndecl))))
644 break;
646 if (fndecls)
648 if (pushed_scope)
649 pop_scope (pushed_scope);
650 return OVL_CURRENT (fndecls);
653 error_at (DECL_SOURCE_LOCATION (function),
654 "prototype for %q#D does not match any in class %qT",
655 function, ctype);
656 is_conv_op = DECL_CONV_FN_P (fndecl);
658 if (is_conv_op)
659 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
660 fndecls = VEC_index (tree, methods, ix);
661 while (fndecls)
663 fndecl = OVL_CURRENT (fndecls);
664 fndecls = OVL_NEXT (fndecls);
666 if (!fndecls && is_conv_op)
668 if (VEC_length (tree, methods) > (size_t) ++ix)
670 fndecls = VEC_index (tree, methods, ix);
671 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
673 fndecls = NULL_TREE;
674 is_conv_op = false;
677 else
678 is_conv_op = false;
680 if (format)
681 format = " %+#D";
682 else if (fndecls)
683 format = N_("candidates are: %+#D");
684 else
685 format = N_("candidate is: %+#D");
686 error (format, fndecl);
689 else if (!COMPLETE_TYPE_P (ctype))
690 cxx_incomplete_type_error (function, ctype);
691 else
692 error ("no %q#D member function declared in class %qT",
693 function, ctype);
695 if (pushed_scope)
696 pop_scope (pushed_scope);
697 return error_mark_node;
700 /* DECL is a function with vague linkage. Remember it so that at the
701 end of the translation unit we can decide whether or not to emit
702 it. */
704 void
705 note_vague_linkage_fn (tree decl)
707 if (!DECL_DEFERRED_FN (decl))
709 DECL_DEFERRED_FN (decl) = 1;
710 DECL_DEFER_OUTPUT (decl) = 1;
711 VEC_safe_push (tree, gc, deferred_fns, decl);
715 /* We have just processed the DECL, which is a static data member.
716 The other parameters are as for cp_finish_decl. */
718 void
719 finish_static_data_member_decl (tree decl,
720 tree init, bool init_const_expr_p,
721 tree asmspec_tree,
722 int flags)
724 DECL_CONTEXT (decl) = current_class_type;
726 /* We cannot call pushdecl here, because that would fill in the
727 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
728 the right thing, namely, to put this decl out straight away. */
730 if (! processing_template_decl)
731 VEC_safe_push (tree, gc, pending_statics, decl);
733 if (LOCAL_CLASS_P (current_class_type))
734 permerror (input_location, "local class %q#T shall not have static data member %q#D",
735 current_class_type, decl);
737 /* Static consts need not be initialized in the class definition. */
738 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
740 static int explained = 0;
742 error ("initializer invalid for static member with constructor");
743 if (!explained)
745 error ("(an out of class initialization is required)");
746 explained = 1;
748 init = NULL_TREE;
750 /* Force the compiler to know when an uninitialized static const
751 member is being used. */
752 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
753 TREE_USED (decl) = 1;
754 DECL_INITIAL (decl) = init;
755 DECL_IN_AGGR_P (decl) = 1;
757 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
760 /* DECLARATOR and DECLSPECS correspond to a class member. The other
761 parameters are as for cp_finish_decl. Return the DECL for the
762 class member declared. */
764 tree
765 grokfield (const cp_declarator *declarator,
766 cp_decl_specifier_seq *declspecs,
767 tree init, bool init_const_expr_p,
768 tree asmspec_tree,
769 tree attrlist)
771 tree value;
772 const char *asmspec = 0;
773 int flags = LOOKUP_ONLYCONVERTING;
775 if (init
776 && TREE_CODE (init) == TREE_LIST
777 && TREE_VALUE (init) == error_mark_node
778 && TREE_CHAIN (init) == NULL_TREE)
779 init = NULL_TREE;
781 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
782 if (! value || error_operand_p (value))
783 /* friend or constructor went bad. */
784 return error_mark_node;
786 if (TREE_CODE (value) == TYPE_DECL && init)
788 error ("typedef %qD is initialized (use __typeof__ instead)", value);
789 init = NULL_TREE;
792 /* Pass friendly classes back. */
793 if (value == void_type_node)
794 return value;
796 /* Pass friend decls back. */
797 if ((TREE_CODE (value) == FUNCTION_DECL
798 || TREE_CODE (value) == TEMPLATE_DECL)
799 && DECL_CONTEXT (value) != current_class_type)
800 return value;
802 if (DECL_NAME (value) != NULL_TREE
803 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
804 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
805 error ("member %qD conflicts with virtual function table field name",
806 value);
808 /* Stash away type declarations. */
809 if (TREE_CODE (value) == TYPE_DECL)
811 DECL_NONLOCAL (value) = 1;
812 DECL_CONTEXT (value) = current_class_type;
814 if (processing_template_decl)
815 value = push_template_decl (value);
817 if (attrlist)
819 int attrflags = 0;
821 /* If this is a typedef that names the class for linkage purposes
822 (7.1.3p8), apply any attributes directly to the type. */
823 if (TAGGED_TYPE_P (TREE_TYPE (value))
824 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
825 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
827 cplus_decl_attributes (&value, attrlist, attrflags);
830 return value;
833 if (DECL_IN_AGGR_P (value))
835 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
836 return void_type_node;
839 if (asmspec_tree && asmspec_tree != error_mark_node)
840 asmspec = TREE_STRING_POINTER (asmspec_tree);
842 if (init)
844 if (TREE_CODE (value) == FUNCTION_DECL)
846 /* Initializers for functions are rejected early in the parser.
847 If we get here, it must be a pure specifier for a method. */
848 if (init == ridpointers[(int)RID_DELETE])
850 DECL_DELETED_FN (value) = 1;
851 DECL_DECLARED_INLINE_P (value) = 1;
852 DECL_INITIAL (value) = error_mark_node;
854 else if (init == ridpointers[(int)RID_DEFAULT])
856 if (!defaultable_fn_p (value))
857 error ("%qD cannot be defaulted", value);
858 else
860 DECL_DEFAULTED_FN (value) = 1;
861 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
862 DECL_DECLARED_INLINE_P (value) = 1;
865 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
867 gcc_assert (error_operand_p (init) || integer_zerop (init));
868 DECL_PURE_VIRTUAL_P (value) = 1;
870 else
872 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
873 error ("initializer specified for static member function %qD",
874 value);
877 else if (pedantic && TREE_CODE (value) != VAR_DECL)
878 /* Already complained in grokdeclarator. */
879 init = NULL_TREE;
880 else if (!processing_template_decl)
882 if (TREE_CODE (init) == CONSTRUCTOR)
883 init = digest_init (TREE_TYPE (value), init);
884 else
885 init = integral_constant_value (init);
887 if (init != error_mark_node && !TREE_CONSTANT (init))
889 /* We can allow references to things that are effectively
890 static, since references are initialized with the
891 address. */
892 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
893 || (TREE_STATIC (init) == 0
894 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
896 error ("field initializer is not constant");
897 init = error_mark_node;
903 if (processing_template_decl
904 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
906 value = push_template_decl (value);
907 if (error_operand_p (value))
908 return error_mark_node;
911 if (attrlist)
912 cplus_decl_attributes (&value, attrlist, 0);
914 switch (TREE_CODE (value))
916 case VAR_DECL:
917 finish_static_data_member_decl (value, init, init_const_expr_p,
918 asmspec_tree, flags);
919 return value;
921 case FIELD_DECL:
922 if (asmspec)
923 error ("%<asm%> specifiers are not permitted on non-static data members");
924 if (DECL_INITIAL (value) == error_mark_node)
925 init = error_mark_node;
926 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
927 NULL_TREE, flags);
928 DECL_INITIAL (value) = init;
929 DECL_IN_AGGR_P (value) = 1;
930 return value;
932 case FUNCTION_DECL:
933 if (asmspec)
934 set_user_assembler_name (value, asmspec);
936 cp_finish_decl (value,
937 /*init=*/NULL_TREE,
938 /*init_const_expr_p=*/false,
939 asmspec_tree, flags);
941 /* Pass friends back this way. */
942 if (DECL_FRIEND_P (value))
943 return void_type_node;
945 DECL_IN_AGGR_P (value) = 1;
946 return value;
948 default:
949 gcc_unreachable ();
951 return NULL_TREE;
954 /* Like `grokfield', but for bitfields.
955 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
957 tree
958 grokbitfield (const cp_declarator *declarator,
959 cp_decl_specifier_seq *declspecs, tree width,
960 tree attrlist)
962 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
964 if (value == error_mark_node)
965 return NULL_TREE; /* friends went bad. */
967 /* Pass friendly classes back. */
968 if (TREE_CODE (value) == VOID_TYPE)
969 return void_type_node;
971 if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
972 && (POINTER_TYPE_P (value)
973 || !dependent_type_p (TREE_TYPE (value))))
975 error ("bit-field %qD with non-integral type", value);
976 return error_mark_node;
979 if (TREE_CODE (value) == TYPE_DECL)
981 error ("cannot declare %qD to be a bit-field type", value);
982 return NULL_TREE;
985 /* Usually, finish_struct_1 catches bitfields with invalid types.
986 But, in the case of bitfields with function type, we confuse
987 ourselves into thinking they are member functions, so we must
988 check here. */
989 if (TREE_CODE (value) == FUNCTION_DECL)
991 error ("cannot declare bit-field %qD with function type",
992 DECL_NAME (value));
993 return NULL_TREE;
996 if (DECL_IN_AGGR_P (value))
998 error ("%qD is already defined in the class %qT", value,
999 DECL_CONTEXT (value));
1000 return void_type_node;
1003 if (TREE_STATIC (value))
1005 error ("static member %qD cannot be a bit-field", value);
1006 return NULL_TREE;
1008 finish_decl (value, NULL_TREE, NULL_TREE);
1010 if (width != error_mark_node)
1012 constant_expression_warning (width);
1013 DECL_INITIAL (value) = width;
1014 SET_DECL_C_BIT_FIELD (value);
1017 DECL_IN_AGGR_P (value) = 1;
1019 if (attrlist)
1020 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1022 return value;
1026 /* Returns true iff ATTR is an attribute which needs to be applied at
1027 instantiation time rather than template definition time. */
1029 static bool
1030 is_late_template_attribute (tree attr, tree decl)
1032 tree name = TREE_PURPOSE (attr);
1033 tree args = TREE_VALUE (attr);
1034 const struct attribute_spec *spec = lookup_attribute_spec (name);
1035 tree arg;
1037 if (!spec)
1038 /* Unknown attribute. */
1039 return false;
1041 /* Attribute weak handling wants to write out assembly right away. */
1042 if (is_attribute_p ("weak", name))
1043 return true;
1045 /* If any of the arguments are dependent expressions, we can't evaluate
1046 the attribute until instantiation time. */
1047 for (arg = args; arg; arg = TREE_CHAIN (arg))
1049 tree t = TREE_VALUE (arg);
1051 /* If the first attribute argument is an identifier, only consider
1052 second and following arguments. Attributes like mode, format,
1053 cleanup and several target specific attributes aren't late
1054 just because they have an IDENTIFIER_NODE as first argument. */
1055 if (arg == args && TREE_CODE (t) == IDENTIFIER_NODE)
1056 continue;
1058 if (value_dependent_expression_p (t)
1059 || type_dependent_expression_p (t))
1060 return true;
1063 if (TREE_CODE (decl) == TYPE_DECL
1064 || TYPE_P (decl)
1065 || spec->type_required)
1067 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1069 /* We can't apply any attributes to a completely unknown type until
1070 instantiation time. */
1071 enum tree_code code = TREE_CODE (type);
1072 if (code == TEMPLATE_TYPE_PARM
1073 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1074 || code == TYPENAME_TYPE)
1075 return true;
1076 /* Also defer most attributes on dependent types. This is not
1077 necessary in all cases, but is the better default. */
1078 else if (dependent_type_p (type)
1079 /* But attribute visibility specifically works on
1080 templates. */
1081 && !is_attribute_p ("visibility", name))
1082 return true;
1083 else
1084 return false;
1086 else
1087 return false;
1090 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1091 applied at instantiation time and return them. If IS_DEPENDENT is true,
1092 the declaration itself is dependent, so all attributes should be applied
1093 at instantiation time. */
1095 static tree
1096 splice_template_attributes (tree *attr_p, tree decl)
1098 tree *p = attr_p;
1099 tree late_attrs = NULL_TREE;
1100 tree *q = &late_attrs;
1102 if (!p)
1103 return NULL_TREE;
1105 for (; *p; )
1107 if (is_late_template_attribute (*p, decl))
1109 ATTR_IS_DEPENDENT (*p) = 1;
1110 *q = *p;
1111 *p = TREE_CHAIN (*p);
1112 q = &TREE_CHAIN (*q);
1113 *q = NULL_TREE;
1115 else
1116 p = &TREE_CHAIN (*p);
1119 return late_attrs;
1122 /* Remove any late attributes from the list in ATTR_P and attach them to
1123 DECL_P. */
1125 static void
1126 save_template_attributes (tree *attr_p, tree *decl_p)
1128 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1129 tree *q;
1130 tree old_attrs = NULL_TREE;
1132 if (!late_attrs)
1133 return;
1135 /* Give this type a name so we know to look it up again at instantiation
1136 time. */
1137 if (TREE_CODE (*decl_p) == TYPE_DECL
1138 && DECL_ORIGINAL_TYPE (*decl_p) == NULL_TREE)
1140 tree oldt = TREE_TYPE (*decl_p);
1141 tree newt = build_variant_type_copy (oldt);
1142 DECL_ORIGINAL_TYPE (*decl_p) = oldt;
1143 TREE_TYPE (*decl_p) = newt;
1144 TYPE_NAME (newt) = *decl_p;
1145 TREE_USED (newt) = TREE_USED (*decl_p);
1148 if (DECL_P (*decl_p))
1149 q = &DECL_ATTRIBUTES (*decl_p);
1150 else
1151 q = &TYPE_ATTRIBUTES (*decl_p);
1153 old_attrs = *q;
1155 /* Merge the late attributes at the beginning with the attribute
1156 list. */
1157 late_attrs = merge_attributes (late_attrs, *q);
1158 *q = late_attrs;
1160 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1162 /* We've added new attributes directly to the main variant, so
1163 now we need to update all of the other variants to include
1164 these new attributes. */
1165 tree variant;
1166 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1167 variant = TYPE_NEXT_VARIANT (variant))
1169 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1170 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1175 /* Like reconstruct_complex_type, but handle also template trees. */
1177 tree
1178 cp_reconstruct_complex_type (tree type, tree bottom)
1180 tree inner, outer;
1182 if (TREE_CODE (type) == POINTER_TYPE)
1184 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1185 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1186 TYPE_REF_CAN_ALIAS_ALL (type));
1188 else if (TREE_CODE (type) == REFERENCE_TYPE)
1190 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1191 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1192 TYPE_REF_CAN_ALIAS_ALL (type));
1194 else if (TREE_CODE (type) == ARRAY_TYPE)
1196 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1197 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1198 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1199 element type qualification will be handled by the recursive
1200 cp_reconstruct_complex_type call and cp_build_qualified_type
1201 for ARRAY_TYPEs changes the element type. */
1202 return outer;
1204 else if (TREE_CODE (type) == FUNCTION_TYPE)
1206 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1207 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1209 else if (TREE_CODE (type) == METHOD_TYPE)
1211 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1212 /* The build_method_type_directly() routine prepends 'this' to argument list,
1213 so we must compensate by getting rid of it. */
1214 outer
1215 = build_method_type_directly
1216 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
1217 inner,
1218 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1220 else if (TREE_CODE (type) == OFFSET_TYPE)
1222 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1223 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1225 else
1226 return bottom;
1228 return cp_build_qualified_type (outer, TYPE_QUALS (type));
1231 /* Like decl_attributes, but handle C++ complexity. */
1233 void
1234 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1236 if (*decl == NULL_TREE || *decl == void_type_node
1237 || *decl == error_mark_node
1238 || attributes == NULL_TREE)
1239 return;
1241 if (processing_template_decl)
1243 if (check_for_bare_parameter_packs (attributes))
1244 return;
1246 save_template_attributes (&attributes, decl);
1247 if (attributes == NULL_TREE)
1248 return;
1251 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1252 decl = &DECL_TEMPLATE_RESULT (*decl);
1254 decl_attributes (decl, attributes, flags);
1256 if (TREE_CODE (*decl) == TYPE_DECL)
1257 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1260 /* Walks through the namespace- or function-scope anonymous union
1261 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1262 Returns one of the fields for use in the mangled name. */
1264 static tree
1265 build_anon_union_vars (tree type, tree object)
1267 tree main_decl = NULL_TREE;
1268 tree field;
1270 /* Rather than write the code to handle the non-union case,
1271 just give an error. */
1272 if (TREE_CODE (type) != UNION_TYPE)
1273 error ("anonymous struct not inside named type");
1275 for (field = TYPE_FIELDS (type);
1276 field != NULL_TREE;
1277 field = TREE_CHAIN (field))
1279 tree decl;
1280 tree ref;
1282 if (DECL_ARTIFICIAL (field))
1283 continue;
1284 if (TREE_CODE (field) != FIELD_DECL)
1286 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1287 "have non-static data members", field);
1288 continue;
1291 if (TREE_PRIVATE (field))
1292 permerror (input_location, "private member %q+#D in anonymous union", field);
1293 else if (TREE_PROTECTED (field))
1294 permerror (input_location, "protected member %q+#D in anonymous union", field);
1296 if (processing_template_decl)
1297 ref = build_min_nt (COMPONENT_REF, object,
1298 DECL_NAME (field), NULL_TREE);
1299 else
1300 ref = build_class_member_access_expr (object, field, NULL_TREE,
1301 false, tf_warning_or_error);
1303 if (DECL_NAME (field))
1305 tree base;
1307 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1308 DECL_ANON_UNION_VAR_P (decl) = 1;
1310 base = get_base_address (object);
1311 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1312 TREE_STATIC (decl) = TREE_STATIC (base);
1313 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1315 SET_DECL_VALUE_EXPR (decl, ref);
1316 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1318 decl = pushdecl (decl);
1320 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1321 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1322 else
1323 decl = 0;
1325 if (main_decl == NULL_TREE)
1326 main_decl = decl;
1329 return main_decl;
1332 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1333 anonymous union, then all members must be laid out together. PUBLIC_P
1334 is nonzero if this union is not declared static. */
1336 void
1337 finish_anon_union (tree anon_union_decl)
1339 tree type;
1340 tree main_decl;
1341 bool public_p;
1343 if (anon_union_decl == error_mark_node)
1344 return;
1346 type = TREE_TYPE (anon_union_decl);
1347 public_p = TREE_PUBLIC (anon_union_decl);
1349 /* The VAR_DECL's context is the same as the TYPE's context. */
1350 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1352 if (TYPE_FIELDS (type) == NULL_TREE)
1353 return;
1355 if (public_p)
1357 error ("namespace-scope anonymous aggregates must be static");
1358 return;
1361 main_decl = build_anon_union_vars (type, anon_union_decl);
1362 if (main_decl == error_mark_node)
1363 return;
1364 if (main_decl == NULL_TREE)
1366 warning (0, "anonymous union with no members");
1367 return;
1370 if (!processing_template_decl)
1372 /* Use main_decl to set the mangled name. */
1373 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1374 maybe_commonize_var (anon_union_decl);
1375 mangle_decl (anon_union_decl);
1376 DECL_NAME (anon_union_decl) = NULL_TREE;
1379 pushdecl (anon_union_decl);
1380 if (building_stmt_tree ()
1381 && at_function_scope_p ())
1382 add_decl_expr (anon_union_decl);
1383 else if (!processing_template_decl)
1384 rest_of_decl_compilation (anon_union_decl,
1385 toplevel_bindings_p (), at_eof);
1388 /* Auxiliary functions to make type signatures for
1389 `operator new' and `operator delete' correspond to
1390 what compiler will be expecting. */
1392 tree
1393 coerce_new_type (tree type)
1395 int e = 0;
1396 tree args = TYPE_ARG_TYPES (type);
1398 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1400 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1402 e = 1;
1403 error ("%<operator new%> must return type %qT", ptr_type_node);
1406 if (args && args != void_list_node)
1408 if (TREE_PURPOSE (args))
1410 /* [basic.stc.dynamic.allocation]
1412 The first parameter shall not have an associated default
1413 argument. */
1414 error ("the first parameter of %<operator new%> cannot "
1415 "have a default argument");
1416 /* Throw away the default argument. */
1417 TREE_PURPOSE (args) = NULL_TREE;
1420 if (!same_type_p (TREE_VALUE (args), size_type_node))
1422 e = 2;
1423 args = TREE_CHAIN (args);
1426 else
1427 e = 2;
1429 if (e == 2)
1430 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1431 "as first parameter", size_type_node);
1433 switch (e)
1435 case 2:
1436 args = tree_cons (NULL_TREE, size_type_node, args);
1437 /* Fall through. */
1438 case 1:
1439 type = build_exception_variant
1440 (build_function_type (ptr_type_node, args),
1441 TYPE_RAISES_EXCEPTIONS (type));
1442 /* Fall through. */
1443 default:;
1445 return type;
1448 tree
1449 coerce_delete_type (tree type)
1451 int e = 0;
1452 tree args = TYPE_ARG_TYPES (type);
1454 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1456 if (!same_type_p (TREE_TYPE (type), void_type_node))
1458 e = 1;
1459 error ("%<operator delete%> must return type %qT", void_type_node);
1462 if (!args || args == void_list_node
1463 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1465 e = 2;
1466 if (args && args != void_list_node)
1467 args = TREE_CHAIN (args);
1468 error ("%<operator delete%> takes type %qT as first parameter",
1469 ptr_type_node);
1471 switch (e)
1473 case 2:
1474 args = tree_cons (NULL_TREE, ptr_type_node, args);
1475 /* Fall through. */
1476 case 1:
1477 type = build_exception_variant
1478 (build_function_type (void_type_node, args),
1479 TYPE_RAISES_EXCEPTIONS (type));
1480 /* Fall through. */
1481 default:;
1484 return type;
1487 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1488 and mark them as needed. */
1490 static void
1491 mark_vtable_entries (tree decl)
1493 tree fnaddr;
1494 unsigned HOST_WIDE_INT idx;
1496 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1497 idx, fnaddr)
1499 tree fn;
1501 STRIP_NOPS (fnaddr);
1503 if (TREE_CODE (fnaddr) != ADDR_EXPR
1504 && TREE_CODE (fnaddr) != FDESC_EXPR)
1505 /* This entry is an offset: a virtual base class offset, a
1506 virtual call offset, an RTTI offset, etc. */
1507 continue;
1509 fn = TREE_OPERAND (fnaddr, 0);
1510 TREE_ADDRESSABLE (fn) = 1;
1511 /* When we don't have vcall offsets, we output thunks whenever
1512 we output the vtables that contain them. With vcall offsets,
1513 we know all the thunks we'll need when we emit a virtual
1514 function, so we emit the thunks there instead. */
1515 if (DECL_THUNK_P (fn))
1516 use_thunk (fn, /*emit_p=*/0);
1517 mark_used (fn);
1521 /* Set DECL up to have the closest approximation of "initialized common"
1522 linkage available. */
1524 void
1525 comdat_linkage (tree decl)
1527 if (flag_weak)
1528 make_decl_one_only (decl);
1529 else if (TREE_CODE (decl) == FUNCTION_DECL
1530 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1531 /* We can just emit function and compiler-generated variables
1532 statically; having multiple copies is (for the most part) only
1533 a waste of space.
1535 There are two correctness issues, however: the address of a
1536 template instantiation with external linkage should be the
1537 same, independent of what translation unit asks for the
1538 address, and this will not hold when we emit multiple copies of
1539 the function. However, there's little else we can do.
1541 Also, by default, the typeinfo implementation assumes that
1542 there will be only one copy of the string used as the name for
1543 each type. Therefore, if weak symbols are unavailable, the
1544 run-time library should perform a more conservative check; it
1545 should perform a string comparison, rather than an address
1546 comparison. */
1547 TREE_PUBLIC (decl) = 0;
1548 else
1550 /* Static data member template instantiations, however, cannot
1551 have multiple copies. */
1552 if (DECL_INITIAL (decl) == 0
1553 || DECL_INITIAL (decl) == error_mark_node)
1554 DECL_COMMON (decl) = 1;
1555 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1557 DECL_COMMON (decl) = 1;
1558 DECL_INITIAL (decl) = error_mark_node;
1560 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1562 /* We can't do anything useful; leave vars for explicit
1563 instantiation. */
1564 DECL_EXTERNAL (decl) = 1;
1565 DECL_NOT_REALLY_EXTERN (decl) = 0;
1569 if (DECL_LANG_SPECIFIC (decl))
1570 DECL_COMDAT (decl) = 1;
1573 /* For win32 we also want to put explicit instantiations in
1574 linkonce sections, so that they will be merged with implicit
1575 instantiations; otherwise we get duplicate symbol errors.
1576 For Darwin we do not want explicit instantiations to be
1577 linkonce. */
1579 void
1580 maybe_make_one_only (tree decl)
1582 /* We used to say that this was not necessary on targets that support weak
1583 symbols, because the implicit instantiations will defer to the explicit
1584 one. However, that's not actually the case in SVR4; a strong definition
1585 after a weak one is an error. Also, not making explicit
1586 instantiations one_only means that we can end up with two copies of
1587 some template instantiations. */
1588 if (! flag_weak)
1589 return;
1591 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1592 we can get away with not emitting them if they aren't used. We need
1593 to for variables so that cp_finish_decl will update their linkage,
1594 because their DECL_INITIAL may not have been set properly yet. */
1596 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1597 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1598 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1600 make_decl_one_only (decl);
1602 if (TREE_CODE (decl) == VAR_DECL)
1604 DECL_COMDAT (decl) = 1;
1605 /* Mark it needed so we don't forget to emit it. */
1606 mark_decl_referenced (decl);
1611 /* Determine whether or not we want to specifically import or export CTYPE,
1612 using various heuristics. */
1614 static void
1615 import_export_class (tree ctype)
1617 /* -1 for imported, 1 for exported. */
1618 int import_export = 0;
1620 /* It only makes sense to call this function at EOF. The reason is
1621 that this function looks at whether or not the first non-inline
1622 non-abstract virtual member function has been defined in this
1623 translation unit. But, we can't possibly know that until we've
1624 seen the entire translation unit. */
1625 gcc_assert (at_eof);
1627 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1628 return;
1630 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1631 we will have CLASSTYPE_INTERFACE_ONLY set but not
1632 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1633 heuristic because someone will supply a #pragma implementation
1634 elsewhere, and deducing it here would produce a conflict. */
1635 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1636 return;
1638 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1639 import_export = -1;
1640 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1641 import_export = 1;
1642 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1643 && !flag_implicit_templates)
1644 /* For a template class, without -fimplicit-templates, check the
1645 repository. If the virtual table is assigned to this
1646 translation unit, then export the class; otherwise, import
1647 it. */
1648 import_export = repo_export_class_p (ctype) ? 1 : -1;
1649 else if (TYPE_POLYMORPHIC_P (ctype))
1651 /* The ABI specifies that the virtual table and associated
1652 information are emitted with the key method, if any. */
1653 tree method = CLASSTYPE_KEY_METHOD (ctype);
1654 /* If weak symbol support is not available, then we must be
1655 careful not to emit the vtable when the key function is
1656 inline. An inline function can be defined in multiple
1657 translation units. If we were to emit the vtable in each
1658 translation unit containing a definition, we would get
1659 multiple definition errors at link-time. */
1660 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1661 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1664 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1665 a definition anywhere else. */
1666 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1667 import_export = 0;
1669 /* Allow back ends the chance to overrule the decision. */
1670 if (targetm.cxx.import_export_class)
1671 import_export = targetm.cxx.import_export_class (ctype, import_export);
1673 if (import_export)
1675 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1676 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1680 /* Return true if VAR has already been provided to the back end; in that
1681 case VAR should not be modified further by the front end. */
1682 static bool
1683 var_finalized_p (tree var)
1685 return varpool_node (var)->finalized;
1688 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1689 must be emitted in this translation unit. Mark it as such. */
1691 void
1692 mark_needed (tree decl)
1694 /* It's possible that we no longer need to set
1695 TREE_SYMBOL_REFERENCED here directly, but doing so is
1696 harmless. */
1697 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1698 mark_decl_referenced (decl);
1701 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1702 returns true if a definition of this entity should be provided in
1703 this object file. Callers use this function to determine whether
1704 or not to let the back end know that a definition of DECL is
1705 available in this translation unit. */
1707 bool
1708 decl_needed_p (tree decl)
1710 gcc_assert (TREE_CODE (decl) == VAR_DECL
1711 || TREE_CODE (decl) == FUNCTION_DECL);
1712 /* This function should only be called at the end of the translation
1713 unit. We cannot be sure of whether or not something will be
1714 COMDAT until that point. */
1715 gcc_assert (at_eof);
1717 /* All entities with external linkage that are not COMDAT should be
1718 emitted; they may be referred to from other object files. */
1719 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1720 return true;
1721 /* If this entity was used, let the back end see it; it will decide
1722 whether or not to emit it into the object file. */
1723 if (TREE_USED (decl)
1724 || (DECL_ASSEMBLER_NAME_SET_P (decl)
1725 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
1726 return true;
1727 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1728 reference to DECL might cause it to be emitted later. */
1729 return false;
1732 /* If necessary, write out the vtables for the dynamic class CTYPE.
1733 Returns true if any vtables were emitted. */
1735 static bool
1736 maybe_emit_vtables (tree ctype)
1738 tree vtbl;
1739 tree primary_vtbl;
1740 int needed = 0;
1742 /* If the vtables for this class have already been emitted there is
1743 nothing more to do. */
1744 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1745 if (var_finalized_p (primary_vtbl))
1746 return false;
1747 /* Ignore dummy vtables made by get_vtable_decl. */
1748 if (TREE_TYPE (primary_vtbl) == void_type_node)
1749 return false;
1751 /* On some targets, we cannot determine the key method until the end
1752 of the translation unit -- which is when this function is
1753 called. */
1754 if (!targetm.cxx.key_method_may_be_inline ())
1755 determine_key_method (ctype);
1757 /* See if any of the vtables are needed. */
1758 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1760 import_export_decl (vtbl);
1761 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1762 needed = 1;
1764 if (!needed)
1766 /* If the references to this class' vtables are optimized away,
1767 still emit the appropriate debugging information. See
1768 dfs_debug_mark. */
1769 if (DECL_COMDAT (primary_vtbl)
1770 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1771 note_debug_info_needed (ctype);
1772 return false;
1775 /* The ABI requires that we emit all of the vtables if we emit any
1776 of them. */
1777 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1779 /* Mark entities references from the virtual table as used. */
1780 mark_vtable_entries (vtbl);
1782 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1784 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl));
1786 /* It had better be all done at compile-time. */
1787 gcc_assert (!expr);
1790 /* Write it out. */
1791 DECL_EXTERNAL (vtbl) = 0;
1792 rest_of_decl_compilation (vtbl, 1, 1);
1794 /* Because we're only doing syntax-checking, we'll never end up
1795 actually marking the variable as written. */
1796 if (flag_syntax_only)
1797 TREE_ASM_WRITTEN (vtbl) = 1;
1800 /* Since we're writing out the vtable here, also write the debug
1801 info. */
1802 note_debug_info_needed (ctype);
1804 return true;
1807 /* A special return value from type_visibility meaning internal
1808 linkage. */
1810 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
1812 /* walk_tree helper function for type_visibility. */
1814 static tree
1815 min_vis_r (tree *tp, int *walk_subtrees, void *data)
1817 int *vis_p = (int *)data;
1818 if (! TYPE_P (*tp))
1820 *walk_subtrees = 0;
1822 else if (CLASS_TYPE_P (*tp))
1824 if (!TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
1826 *vis_p = VISIBILITY_ANON;
1827 return *tp;
1829 else if (CLASSTYPE_VISIBILITY (*tp) > *vis_p)
1830 *vis_p = CLASSTYPE_VISIBILITY (*tp);
1832 return NULL;
1835 /* Returns the visibility of TYPE, which is the minimum visibility of its
1836 component types. */
1838 static int
1839 type_visibility (tree type)
1841 int vis = VISIBILITY_DEFAULT;
1842 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
1843 return vis;
1846 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
1847 specified (or if VISIBILITY is static). */
1849 static bool
1850 constrain_visibility (tree decl, int visibility)
1852 if (visibility == VISIBILITY_ANON)
1854 /* extern "C" declarations aren't affected by the anonymous
1855 namespace. */
1856 if (!DECL_EXTERN_C_P (decl))
1858 TREE_PUBLIC (decl) = 0;
1859 DECL_ONE_ONLY (decl) = 0;
1860 DECL_INTERFACE_KNOWN (decl) = 1;
1861 if (DECL_LANG_SPECIFIC (decl))
1862 DECL_NOT_REALLY_EXTERN (decl) = 1;
1865 else if (visibility > DECL_VISIBILITY (decl)
1866 && !DECL_VISIBILITY_SPECIFIED (decl))
1868 DECL_VISIBILITY (decl) = visibility;
1869 return true;
1871 return false;
1874 /* Constrain the visibility of DECL based on the visibility of its template
1875 arguments. */
1877 static void
1878 constrain_visibility_for_template (tree decl, tree targs)
1880 /* If this is a template instantiation, check the innermost
1881 template args for visibility constraints. The outer template
1882 args are covered by the class check. */
1883 tree args = INNERMOST_TEMPLATE_ARGS (targs);
1884 int i;
1885 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
1887 int vis = 0;
1889 tree arg = TREE_VEC_ELT (args, i-1);
1890 if (TYPE_P (arg))
1891 vis = type_visibility (arg);
1892 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
1894 STRIP_NOPS (arg);
1895 if (TREE_CODE (arg) == ADDR_EXPR)
1896 arg = TREE_OPERAND (arg, 0);
1897 if (TREE_CODE (arg) == VAR_DECL
1898 || TREE_CODE (arg) == FUNCTION_DECL)
1900 if (! TREE_PUBLIC (arg))
1901 vis = VISIBILITY_ANON;
1902 else
1903 vis = DECL_VISIBILITY (arg);
1906 if (vis)
1907 constrain_visibility (decl, vis);
1911 /* Like c_determine_visibility, but with additional C++-specific
1912 behavior.
1914 Function-scope entities can rely on the function's visibility because
1915 it is set in start_preparsed_function.
1917 Class-scope entities cannot rely on the class's visibility until the end
1918 of the enclosing class definition.
1920 Note that because namespaces have multiple independent definitions,
1921 namespace visibility is handled elsewhere using the #pragma visibility
1922 machinery rather than by decorating the namespace declaration.
1924 The goal is for constraints from the type to give a diagnostic, and
1925 other constraints to be applied silently. */
1927 void
1928 determine_visibility (tree decl)
1930 tree class_type = NULL_TREE;
1931 bool use_template;
1932 bool orig_visibility_specified;
1933 enum symbol_visibility orig_visibility;
1935 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
1937 /* Only relevant for names with external linkage. */
1938 if (!TREE_PUBLIC (decl))
1939 return;
1941 /* Cloned constructors and destructors get the same visibility as
1942 the underlying function. That should be set up in
1943 maybe_clone_body. */
1944 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
1946 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
1947 orig_visibility = DECL_VISIBILITY (decl);
1949 if (TREE_CODE (decl) == TYPE_DECL)
1951 if (CLASS_TYPE_P (TREE_TYPE (decl)))
1952 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
1953 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
1954 use_template = 1;
1955 else
1956 use_template = 0;
1958 else if (DECL_LANG_SPECIFIC (decl))
1959 use_template = DECL_USE_TEMPLATE (decl);
1960 else
1961 use_template = 0;
1963 /* If DECL is a member of a class, visibility specifiers on the
1964 class can influence the visibility of the DECL. */
1965 if (DECL_CLASS_SCOPE_P (decl))
1966 class_type = DECL_CONTEXT (decl);
1967 else
1969 /* Not a class member. */
1971 /* Virtual tables have DECL_CONTEXT set to their associated class,
1972 so they are automatically handled above. */
1973 gcc_assert (TREE_CODE (decl) != VAR_DECL
1974 || !DECL_VTABLE_OR_VTT_P (decl));
1976 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
1978 /* Local statics and classes get the visibility of their
1979 containing function by default, except that
1980 -fvisibility-inlines-hidden doesn't affect them. */
1981 tree fn = DECL_CONTEXT (decl);
1982 if (DECL_VISIBILITY_SPECIFIED (fn) || ! DECL_CLASS_SCOPE_P (fn))
1984 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
1985 DECL_VISIBILITY_SPECIFIED (decl) =
1986 DECL_VISIBILITY_SPECIFIED (fn);
1988 else
1989 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
1991 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
1992 but have no TEMPLATE_INFO, so don't try to check it. */
1993 use_template = 0;
1995 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl)
1996 && flag_visibility_ms_compat)
1998 /* Under -fvisibility-ms-compat, types are visible by default,
1999 even though their contents aren't. */
2000 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2001 int underlying_vis = type_visibility (underlying_type);
2002 if (underlying_vis == VISIBILITY_ANON
2003 || CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type))
2004 constrain_visibility (decl, underlying_vis);
2005 else
2006 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2008 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2010 /* tinfo visibility is based on the type it's for. */
2011 constrain_visibility
2012 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))));
2014 /* Give the target a chance to override the visibility associated
2015 with DECL. */
2016 if (TREE_PUBLIC (decl)
2017 && !DECL_REALLY_EXTERN (decl)
2018 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2019 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2020 targetm.cxx.determine_class_data_visibility (decl);
2022 else if (use_template)
2023 /* Template instantiations and specializations get visibility based
2024 on their template unless they override it with an attribute. */;
2025 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2027 /* Set default visibility to whatever the user supplied with
2028 #pragma GCC visibility or a namespace visibility attribute. */
2029 DECL_VISIBILITY (decl) = default_visibility;
2030 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2034 if (use_template)
2036 /* If the specialization doesn't specify visibility, use the
2037 visibility from the template. */
2038 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2039 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2040 : DECL_TEMPLATE_INFO (decl));
2041 tree args = TI_ARGS (tinfo);
2043 if (args != error_mark_node)
2045 int depth = TMPL_ARGS_DEPTH (args);
2046 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2048 if (!DECL_VISIBILITY_SPECIFIED (decl))
2050 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2051 DECL_VISIBILITY_SPECIFIED (decl)
2052 = DECL_VISIBILITY_SPECIFIED (pattern);
2055 /* FIXME should TMPL_ARGS_DEPTH really return 1 for null input? */
2056 if (args && depth > template_class_depth (class_type))
2057 /* Limit visibility based on its template arguments. */
2058 constrain_visibility_for_template (decl, args);
2062 if (class_type)
2063 determine_visibility_from_class (decl, class_type);
2065 if (decl_anon_ns_mem_p (decl))
2066 /* Names in an anonymous namespace get internal linkage.
2067 This might change once we implement export. */
2068 constrain_visibility (decl, VISIBILITY_ANON);
2069 else if (TREE_CODE (decl) != TYPE_DECL)
2071 /* Propagate anonymity from type to decl. */
2072 int tvis = type_visibility (TREE_TYPE (decl));
2073 if (tvis == VISIBILITY_ANON
2074 || ! DECL_VISIBILITY_SPECIFIED (decl))
2075 constrain_visibility (decl, tvis);
2078 /* If visibility changed and DECL already has DECL_RTL, ensure
2079 symbol flags are updated. */
2080 if ((DECL_VISIBILITY (decl) != orig_visibility
2081 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2082 && ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2083 || TREE_CODE (decl) == FUNCTION_DECL)
2084 && DECL_RTL_SET_P (decl))
2085 make_decl_rtl (decl);
2088 /* By default, static data members and function members receive
2089 the visibility of their containing class. */
2091 static void
2092 determine_visibility_from_class (tree decl, tree class_type)
2094 if (DECL_VISIBILITY_SPECIFIED (decl))
2095 return;
2097 if (visibility_options.inlines_hidden
2098 /* Don't do this for inline templates; specializations might not be
2099 inline, and we don't want them to inherit the hidden
2100 visibility. We'll set it here for all inline instantiations. */
2101 && !processing_template_decl
2102 && TREE_CODE (decl) == FUNCTION_DECL
2103 && DECL_DECLARED_INLINE_P (decl)
2104 && (! DECL_LANG_SPECIFIC (decl)
2105 || ! DECL_EXPLICIT_INSTANTIATION (decl)))
2106 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2107 else
2109 /* Default to the class visibility. */
2110 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2111 DECL_VISIBILITY_SPECIFIED (decl)
2112 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2115 /* Give the target a chance to override the visibility associated
2116 with DECL. */
2117 if (TREE_CODE (decl) == VAR_DECL
2118 && (DECL_TINFO_P (decl)
2119 || (DECL_VTABLE_OR_VTT_P (decl)
2120 /* Construction virtual tables are not exported because
2121 they cannot be referred to from other object files;
2122 their name is not standardized by the ABI. */
2123 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2124 && TREE_PUBLIC (decl)
2125 && !DECL_REALLY_EXTERN (decl)
2126 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2127 targetm.cxx.determine_class_data_visibility (decl);
2130 /* Constrain the visibility of a class TYPE based on the visibility of its
2131 field types. Warn if any fields require lesser visibility. */
2133 void
2134 constrain_class_visibility (tree type)
2136 tree binfo;
2137 tree t;
2138 int i;
2140 int vis = type_visibility (type);
2142 if (vis == VISIBILITY_ANON
2143 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2144 return;
2146 /* Don't warn about visibility if the class has explicit visibility. */
2147 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2148 vis = VISIBILITY_INTERNAL;
2150 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
2151 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2153 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2154 int subvis = type_visibility (ftype);
2156 if (subvis == VISIBILITY_ANON)
2158 if (!in_main_input_context ())
2159 warning (0, "\
2160 %qT has a field %qD whose type uses the anonymous namespace",
2161 type, t);
2163 else if (MAYBE_CLASS_TYPE_P (ftype)
2164 && vis < VISIBILITY_HIDDEN
2165 && subvis >= VISIBILITY_HIDDEN)
2166 warning (OPT_Wattributes, "\
2167 %qT declared with greater visibility than the type of its field %qD",
2168 type, t);
2171 binfo = TYPE_BINFO (type);
2172 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2174 int subvis = type_visibility (TREE_TYPE (t));
2176 if (subvis == VISIBILITY_ANON)
2178 if (!in_main_input_context())
2179 warning (0, "\
2180 %qT has a base %qT whose type uses the anonymous namespace",
2181 type, TREE_TYPE (t));
2183 else if (vis < VISIBILITY_HIDDEN
2184 && subvis >= VISIBILITY_HIDDEN)
2185 warning (OPT_Wattributes, "\
2186 %qT declared with greater visibility than its base %qT",
2187 type, TREE_TYPE (t));
2191 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2192 for DECL has not already been determined, do so now by setting
2193 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2194 function is called entities with vague linkage whose definitions
2195 are available must have TREE_PUBLIC set.
2197 If this function decides to place DECL in COMDAT, it will set
2198 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2199 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2200 callers defer that decision until it is clear that DECL is actually
2201 required. */
2203 void
2204 import_export_decl (tree decl)
2206 int emit_p;
2207 bool comdat_p;
2208 bool import_p;
2209 tree class_type = NULL_TREE;
2211 if (DECL_INTERFACE_KNOWN (decl))
2212 return;
2214 /* We cannot determine what linkage to give to an entity with vague
2215 linkage until the end of the file. For example, a virtual table
2216 for a class will be defined if and only if the key method is
2217 defined in this translation unit. As a further example, consider
2218 that when compiling a translation unit that uses PCH file with
2219 "-frepo" it would be incorrect to make decisions about what
2220 entities to emit when building the PCH; those decisions must be
2221 delayed until the repository information has been processed. */
2222 gcc_assert (at_eof);
2223 /* Object file linkage for explicit instantiations is handled in
2224 mark_decl_instantiated. For static variables in functions with
2225 vague linkage, maybe_commonize_var is used.
2227 Therefore, the only declarations that should be provided to this
2228 function are those with external linkage that are:
2230 * implicit instantiations of function templates
2232 * inline function
2234 * implicit instantiations of static data members of class
2235 templates
2237 * virtual tables
2239 * typeinfo objects
2241 Furthermore, all entities that reach this point must have a
2242 definition available in this translation unit.
2244 The following assertions check these conditions. */
2245 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
2246 || TREE_CODE (decl) == VAR_DECL);
2247 /* Any code that creates entities with TREE_PUBLIC cleared should
2248 also set DECL_INTERFACE_KNOWN. */
2249 gcc_assert (TREE_PUBLIC (decl));
2250 if (TREE_CODE (decl) == FUNCTION_DECL)
2251 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2252 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2253 || DECL_DECLARED_INLINE_P (decl));
2254 else
2255 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2256 || DECL_VTABLE_OR_VTT_P (decl)
2257 || DECL_TINFO_P (decl));
2258 /* Check that a definition of DECL is available in this translation
2259 unit. */
2260 gcc_assert (!DECL_REALLY_EXTERN (decl));
2262 /* Assume that DECL will not have COMDAT linkage. */
2263 comdat_p = false;
2264 /* Assume that DECL will not be imported into this translation
2265 unit. */
2266 import_p = false;
2268 /* See if the repository tells us whether or not to emit DECL in
2269 this translation unit. */
2270 emit_p = repo_emit_p (decl);
2271 if (emit_p == 0)
2272 import_p = true;
2273 else if (emit_p == 1)
2275 /* The repository indicates that this entity should be defined
2276 here. Make sure the back end honors that request. */
2277 if (TREE_CODE (decl) == VAR_DECL)
2278 mark_needed (decl);
2279 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2280 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2282 tree clone;
2283 FOR_EACH_CLONE (clone, decl)
2284 mark_needed (clone);
2286 else
2287 mark_needed (decl);
2288 /* Output the definition as an ordinary strong definition. */
2289 DECL_EXTERNAL (decl) = 0;
2290 DECL_INTERFACE_KNOWN (decl) = 1;
2291 return;
2294 if (import_p)
2295 /* We have already decided what to do with this DECL; there is no
2296 need to check anything further. */
2298 else if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
2300 class_type = DECL_CONTEXT (decl);
2301 import_export_class (class_type);
2302 if (TYPE_FOR_JAVA (class_type))
2303 import_p = true;
2304 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2305 && CLASSTYPE_INTERFACE_ONLY (class_type))
2306 import_p = true;
2307 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2308 && !CLASSTYPE_USE_TEMPLATE (class_type)
2309 && CLASSTYPE_KEY_METHOD (class_type)
2310 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2311 /* The ABI requires that all virtual tables be emitted with
2312 COMDAT linkage. However, on systems where COMDAT symbols
2313 don't show up in the table of contents for a static
2314 archive, or on systems without weak symbols (where we
2315 approximate COMDAT linkage by using internal linkage), the
2316 linker will report errors about undefined symbols because
2317 it will not see the virtual table definition. Therefore,
2318 in the case that we know that the virtual table will be
2319 emitted in only one translation unit, we make the virtual
2320 table an ordinary definition with external linkage. */
2321 DECL_EXTERNAL (decl) = 0;
2322 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2324 /* CLASS_TYPE is being exported from this translation unit,
2325 so DECL should be defined here. */
2326 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2327 /* If a class is declared in a header with the "extern
2328 template" extension, then it will not be instantiated,
2329 even in translation units that would normally require
2330 it. Often such classes are explicitly instantiated in
2331 one translation unit. Therefore, the explicit
2332 instantiation must be made visible to other translation
2333 units. */
2334 DECL_EXTERNAL (decl) = 0;
2335 else
2337 /* The generic C++ ABI says that class data is always
2338 COMDAT, even if there is a key function. Some
2339 variants (e.g., the ARM EABI) says that class data
2340 only has COMDAT linkage if the class data might be
2341 emitted in more than one translation unit. When the
2342 key method can be inline and is inline, we still have
2343 to arrange for comdat even though
2344 class_data_always_comdat is false. */
2345 if (!CLASSTYPE_KEY_METHOD (class_type)
2346 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2347 || targetm.cxx.class_data_always_comdat ())
2349 /* The ABI requires COMDAT linkage. Normally, we
2350 only emit COMDAT things when they are needed;
2351 make sure that we realize that this entity is
2352 indeed needed. */
2353 comdat_p = true;
2354 mark_needed (decl);
2358 else if (!flag_implicit_templates
2359 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2360 import_p = true;
2361 else
2362 comdat_p = true;
2364 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2366 tree type = TREE_TYPE (DECL_NAME (decl));
2367 if (CLASS_TYPE_P (type))
2369 class_type = type;
2370 import_export_class (type);
2371 if (CLASSTYPE_INTERFACE_KNOWN (type)
2372 && TYPE_POLYMORPHIC_P (type)
2373 && CLASSTYPE_INTERFACE_ONLY (type)
2374 /* If -fno-rtti was specified, then we cannot be sure
2375 that RTTI information will be emitted with the
2376 virtual table of the class, so we must emit it
2377 wherever it is used. */
2378 && flag_rtti)
2379 import_p = true;
2380 else
2382 if (CLASSTYPE_INTERFACE_KNOWN (type)
2383 && !CLASSTYPE_INTERFACE_ONLY (type))
2385 comdat_p = (targetm.cxx.class_data_always_comdat ()
2386 || (CLASSTYPE_KEY_METHOD (type)
2387 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2388 mark_needed (decl);
2389 if (!flag_weak)
2391 comdat_p = false;
2392 DECL_EXTERNAL (decl) = 0;
2395 else
2396 comdat_p = true;
2399 else
2400 comdat_p = true;
2402 else if (DECL_TEMPLATE_INSTANTIATION (decl)
2403 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2405 /* DECL is an implicit instantiation of a function or static
2406 data member. */
2407 if ((flag_implicit_templates
2408 && !flag_use_repository)
2409 || (flag_implicit_inline_templates
2410 && TREE_CODE (decl) == FUNCTION_DECL
2411 && DECL_DECLARED_INLINE_P (decl)))
2412 comdat_p = true;
2413 else
2414 /* If we are not implicitly generating templates, then mark
2415 this entity as undefined in this translation unit. */
2416 import_p = true;
2418 else if (DECL_FUNCTION_MEMBER_P (decl))
2420 if (!DECL_DECLARED_INLINE_P (decl))
2422 tree ctype = DECL_CONTEXT (decl);
2423 import_export_class (ctype);
2424 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2426 DECL_NOT_REALLY_EXTERN (decl)
2427 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2428 || (DECL_DECLARED_INLINE_P (decl)
2429 && ! flag_implement_inlines
2430 && !DECL_VINDEX (decl)));
2432 if (!DECL_NOT_REALLY_EXTERN (decl))
2433 DECL_EXTERNAL (decl) = 1;
2435 /* Always make artificials weak. */
2436 if (DECL_ARTIFICIAL (decl) && flag_weak)
2437 comdat_p = true;
2438 else
2439 maybe_make_one_only (decl);
2442 else
2443 comdat_p = true;
2445 else
2446 comdat_p = true;
2448 if (import_p)
2450 /* If we are importing DECL into this translation unit, mark is
2451 an undefined here. */
2452 DECL_EXTERNAL (decl) = 1;
2453 DECL_NOT_REALLY_EXTERN (decl) = 0;
2455 else if (comdat_p)
2457 /* If we decided to put DECL in COMDAT, mark it accordingly at
2458 this point. */
2459 comdat_linkage (decl);
2462 DECL_INTERFACE_KNOWN (decl) = 1;
2465 /* Return an expression that performs the destruction of DECL, which
2466 must be a VAR_DECL whose type has a non-trivial destructor, or is
2467 an array whose (innermost) elements have a non-trivial destructor. */
2469 tree
2470 build_cleanup (tree decl)
2472 tree temp;
2473 tree type = TREE_TYPE (decl);
2475 /* This function should only be called for declarations that really
2476 require cleanups. */
2477 gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type));
2479 /* Treat all objects with destructors as used; the destructor may do
2480 something substantive. */
2481 mark_used (decl);
2483 if (TREE_CODE (type) == ARRAY_TYPE)
2484 temp = decl;
2485 else
2486 temp = build_address (decl);
2487 temp = build_delete (TREE_TYPE (temp), temp,
2488 sfk_complete_destructor,
2489 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2490 return temp;
2493 /* Returns the initialization guard variable for the variable DECL,
2494 which has static storage duration. */
2496 tree
2497 get_guard (tree decl)
2499 tree sname;
2500 tree guard;
2502 sname = mangle_guard_variable (decl);
2503 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2504 if (! guard)
2506 tree guard_type;
2508 /* We use a type that is big enough to contain a mutex as well
2509 as an integer counter. */
2510 guard_type = targetm.cxx.guard_type ();
2511 guard = build_decl (VAR_DECL, sname, guard_type);
2513 /* The guard should have the same linkage as what it guards. */
2514 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2515 TREE_STATIC (guard) = TREE_STATIC (decl);
2516 DECL_COMMON (guard) = DECL_COMMON (decl);
2517 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2518 if (TREE_PUBLIC (decl))
2519 DECL_WEAK (guard) = DECL_WEAK (decl);
2520 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2521 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2523 DECL_ARTIFICIAL (guard) = 1;
2524 DECL_IGNORED_P (guard) = 1;
2525 TREE_USED (guard) = 1;
2526 pushdecl_top_level_and_finish (guard, NULL_TREE);
2528 return guard;
2531 /* Return those bits of the GUARD variable that should be set when the
2532 guarded entity is actually initialized. */
2534 static tree
2535 get_guard_bits (tree guard)
2537 if (!targetm.cxx.guard_mask_bit ())
2539 /* We only set the first byte of the guard, in order to leave room
2540 for a mutex in the high-order bits. */
2541 guard = build1 (ADDR_EXPR,
2542 build_pointer_type (TREE_TYPE (guard)),
2543 guard);
2544 guard = build1 (NOP_EXPR,
2545 build_pointer_type (char_type_node),
2546 guard);
2547 guard = build1 (INDIRECT_REF, char_type_node, guard);
2550 return guard;
2553 /* Return an expression which determines whether or not the GUARD
2554 variable has already been initialized. */
2556 tree
2557 get_guard_cond (tree guard)
2559 tree guard_value;
2561 /* Check to see if the GUARD is zero. */
2562 guard = get_guard_bits (guard);
2564 /* Mask off all but the low bit. */
2565 if (targetm.cxx.guard_mask_bit ())
2567 guard_value = integer_one_node;
2568 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2569 guard_value = convert (TREE_TYPE (guard), guard_value);
2570 guard = cp_build_binary_op (input_location,
2571 BIT_AND_EXPR, guard, guard_value,
2572 tf_warning_or_error);
2575 guard_value = integer_zero_node;
2576 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2577 guard_value = convert (TREE_TYPE (guard), guard_value);
2578 return cp_build_binary_op (input_location,
2579 EQ_EXPR, guard, guard_value,
2580 tf_warning_or_error);
2583 /* Return an expression which sets the GUARD variable, indicating that
2584 the variable being guarded has been initialized. */
2586 tree
2587 set_guard (tree guard)
2589 tree guard_init;
2591 /* Set the GUARD to one. */
2592 guard = get_guard_bits (guard);
2593 guard_init = integer_one_node;
2594 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2595 guard_init = convert (TREE_TYPE (guard), guard_init);
2596 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2597 tf_warning_or_error);
2600 /* Start the process of running a particular set of global constructors
2601 or destructors. Subroutine of do_[cd]tors. */
2603 static tree
2604 start_objects (int method_type, int initp)
2606 tree body;
2607 tree fndecl;
2608 char type[10];
2610 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2612 if (initp != DEFAULT_INIT_PRIORITY)
2614 char joiner;
2616 #ifdef JOINER
2617 joiner = JOINER;
2618 #else
2619 joiner = '_';
2620 #endif
2622 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2624 else
2625 sprintf (type, "%c", method_type);
2627 fndecl = build_lang_decl (FUNCTION_DECL,
2628 get_file_function_name (type),
2629 build_function_type (void_type_node,
2630 void_list_node));
2631 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
2633 TREE_PUBLIC (current_function_decl) = 0;
2635 /* Mark as artificial because it's not explicitly in the user's
2636 source code. */
2637 DECL_ARTIFICIAL (current_function_decl) = 1;
2639 /* Mark this declaration as used to avoid spurious warnings. */
2640 TREE_USED (current_function_decl) = 1;
2642 /* Mark this function as a global constructor or destructor. */
2643 if (method_type == 'I')
2644 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2645 else
2646 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2647 DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
2649 body = begin_compound_stmt (BCS_FN_BODY);
2651 return body;
2654 /* Finish the process of running a particular set of global constructors
2655 or destructors. Subroutine of do_[cd]tors. */
2657 static void
2658 finish_objects (int method_type, int initp, tree body)
2660 tree fn;
2662 /* Finish up. */
2663 finish_compound_stmt (body);
2664 fn = finish_function (0);
2666 if (method_type == 'I')
2668 DECL_STATIC_CONSTRUCTOR (fn) = 1;
2669 decl_init_priority_insert (fn, initp);
2671 else
2673 DECL_STATIC_DESTRUCTOR (fn) = 1;
2674 decl_fini_priority_insert (fn, initp);
2677 expand_or_defer_fn (fn);
2680 /* The names of the parameters to the function created to handle
2681 initializations and destructions for objects with static storage
2682 duration. */
2683 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2684 #define PRIORITY_IDENTIFIER "__priority"
2686 /* The name of the function we create to handle initializations and
2687 destructions for objects with static storage duration. */
2688 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2690 /* The declaration for the __INITIALIZE_P argument. */
2691 static GTY(()) tree initialize_p_decl;
2693 /* The declaration for the __PRIORITY argument. */
2694 static GTY(()) tree priority_decl;
2696 /* The declaration for the static storage duration function. */
2697 static GTY(()) tree ssdf_decl;
2699 /* All the static storage duration functions created in this
2700 translation unit. */
2701 static GTY(()) VEC(tree,gc) *ssdf_decls;
2703 /* A map from priority levels to information about that priority
2704 level. There may be many such levels, so efficient lookup is
2705 important. */
2706 static splay_tree priority_info_map;
2708 /* Begins the generation of the function that will handle all
2709 initialization and destruction of objects with static storage
2710 duration. The function generated takes two parameters of type
2711 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2712 nonzero, it performs initializations. Otherwise, it performs
2713 destructions. It only performs those initializations or
2714 destructions with the indicated __PRIORITY. The generated function
2715 returns no value.
2717 It is assumed that this function will only be called once per
2718 translation unit. */
2720 static tree
2721 start_static_storage_duration_function (unsigned count)
2723 tree parm_types;
2724 tree type;
2725 tree body;
2726 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2728 /* Create the identifier for this function. It will be of the form
2729 SSDF_IDENTIFIER_<number>. */
2730 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
2732 /* Create the parameters. */
2733 parm_types = void_list_node;
2734 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2735 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2736 type = build_function_type (void_type_node, parm_types);
2738 /* Create the FUNCTION_DECL itself. */
2739 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2740 get_identifier (id),
2741 type);
2742 TREE_PUBLIC (ssdf_decl) = 0;
2743 DECL_ARTIFICIAL (ssdf_decl) = 1;
2745 /* Put this function in the list of functions to be called from the
2746 static constructors and destructors. */
2747 if (!ssdf_decls)
2749 ssdf_decls = VEC_alloc (tree, gc, 32);
2751 /* Take this opportunity to initialize the map from priority
2752 numbers to information about that priority level. */
2753 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2754 /*delete_key_fn=*/0,
2755 /*delete_value_fn=*/
2756 (splay_tree_delete_value_fn) &free);
2758 /* We always need to generate functions for the
2759 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2760 priorities later, we'll be sure to find the
2761 DEFAULT_INIT_PRIORITY. */
2762 get_priority_info (DEFAULT_INIT_PRIORITY);
2765 VEC_safe_push (tree, gc, ssdf_decls, ssdf_decl);
2767 /* Create the argument list. */
2768 initialize_p_decl = cp_build_parm_decl
2769 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2770 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2771 TREE_USED (initialize_p_decl) = 1;
2772 priority_decl = cp_build_parm_decl
2773 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2774 DECL_CONTEXT (priority_decl) = ssdf_decl;
2775 TREE_USED (priority_decl) = 1;
2777 TREE_CHAIN (initialize_p_decl) = priority_decl;
2778 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2780 /* Put the function in the global scope. */
2781 pushdecl (ssdf_decl);
2783 /* Start the function itself. This is equivalent to declaring the
2784 function as:
2786 static void __ssdf (int __initialize_p, init __priority_p);
2788 It is static because we only need to call this function from the
2789 various constructor and destructor functions for this module. */
2790 start_preparsed_function (ssdf_decl,
2791 /*attrs=*/NULL_TREE,
2792 SF_PRE_PARSED);
2794 /* Set up the scope of the outermost block in the function. */
2795 body = begin_compound_stmt (BCS_FN_BODY);
2797 return body;
2800 /* Finish the generation of the function which performs initialization
2801 and destruction of objects with static storage duration. After
2802 this point, no more such objects can be created. */
2804 static void
2805 finish_static_storage_duration_function (tree body)
2807 /* Close out the function. */
2808 finish_compound_stmt (body);
2809 expand_or_defer_fn (finish_function (0));
2812 /* Return the information about the indicated PRIORITY level. If no
2813 code to handle this level has yet been generated, generate the
2814 appropriate prologue. */
2816 static priority_info
2817 get_priority_info (int priority)
2819 priority_info pi;
2820 splay_tree_node n;
2822 n = splay_tree_lookup (priority_info_map,
2823 (splay_tree_key) priority);
2824 if (!n)
2826 /* Create a new priority information structure, and insert it
2827 into the map. */
2828 pi = XNEW (struct priority_info_s);
2829 pi->initializations_p = 0;
2830 pi->destructions_p = 0;
2831 splay_tree_insert (priority_info_map,
2832 (splay_tree_key) priority,
2833 (splay_tree_value) pi);
2835 else
2836 pi = (priority_info) n->value;
2838 return pi;
2841 /* The effective initialization priority of a DECL. */
2843 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
2844 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
2845 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
2847 /* Whether a DECL needs a guard to protect it against multiple
2848 initialization. */
2850 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
2851 || DECL_ONE_ONLY (decl) \
2852 || DECL_WEAK (decl)))
2854 /* Called from one_static_initialization_or_destruction(),
2855 via walk_tree.
2856 Walks the initializer list of a global variable and looks for
2857 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
2858 and that have their DECL_CONTEXT() == NULL.
2859 For each such temporary variable, set their DECL_CONTEXT() to
2860 the current function. This is necessary because otherwise
2861 some optimizers (enabled by -O2 -fprofile-arcs) might crash
2862 when trying to refer to a temporary variable that does not have
2863 it's DECL_CONTECT() properly set. */
2864 static tree
2865 fix_temporary_vars_context_r (tree *node,
2866 int *unused ATTRIBUTE_UNUSED,
2867 void *unused1 ATTRIBUTE_UNUSED)
2869 gcc_assert (current_function_decl);
2871 if (TREE_CODE (*node) == BIND_EXPR)
2873 tree var;
2875 for (var = BIND_EXPR_VARS (*node); var; var = TREE_CHAIN (var))
2876 if (TREE_CODE (var) == VAR_DECL
2877 && !DECL_NAME (var)
2878 && DECL_ARTIFICIAL (var)
2879 && !DECL_CONTEXT (var))
2880 DECL_CONTEXT (var) = current_function_decl;
2883 return NULL_TREE;
2886 /* Set up to handle the initialization or destruction of DECL. If
2887 INITP is nonzero, we are initializing the variable. Otherwise, we
2888 are destroying it. */
2890 static void
2891 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
2893 tree guard_if_stmt = NULL_TREE;
2894 tree guard;
2896 /* If we are supposed to destruct and there's a trivial destructor,
2897 nothing has to be done. */
2898 if (!initp
2899 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2900 return;
2902 /* Trick the compiler into thinking we are at the file and line
2903 where DECL was declared so that error-messages make sense, and so
2904 that the debugger will show somewhat sensible file and line
2905 information. */
2906 input_location = DECL_SOURCE_LOCATION (decl);
2908 /* Make sure temporary variables in the initialiser all have
2909 their DECL_CONTEXT() set to a value different from NULL_TREE.
2910 This can happen when global variables initialisers are built.
2911 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
2912 the temporary variables that might have been generated in the
2913 accompagning initialisers is NULL_TREE, meaning the variables have been
2914 declared in the global namespace.
2915 What we want to do here is to fix that and make sure the DECL_CONTEXT()
2916 of the temporaries are set to the current function decl. */
2917 cp_walk_tree_without_duplicates (&init,
2918 fix_temporary_vars_context_r,
2919 NULL);
2921 /* Because of:
2923 [class.access.spec]
2925 Access control for implicit calls to the constructors,
2926 the conversion functions, or the destructor called to
2927 create and destroy a static data member is performed as
2928 if these calls appeared in the scope of the member's
2929 class.
2931 we pretend we are in a static member function of the class of
2932 which the DECL is a member. */
2933 if (member_p (decl))
2935 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2936 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2939 /* Assume we don't need a guard. */
2940 guard = NULL_TREE;
2941 /* We need a guard if this is an object with external linkage that
2942 might be initialized in more than one place. (For example, a
2943 static data member of a template, when the data member requires
2944 construction.) */
2945 if (NEEDS_GUARD_P (decl))
2947 tree guard_cond;
2949 guard = get_guard (decl);
2951 /* When using __cxa_atexit, we just check the GUARD as we would
2952 for a local static. */
2953 if (flag_use_cxa_atexit)
2955 /* When using __cxa_atexit, we never try to destroy
2956 anything from a static destructor. */
2957 gcc_assert (initp);
2958 guard_cond = get_guard_cond (guard);
2960 /* If we don't have __cxa_atexit, then we will be running
2961 destructors from .fini sections, or their equivalents. So,
2962 we need to know how many times we've tried to initialize this
2963 object. We do initializations only if the GUARD is zero,
2964 i.e., if we are the first to initialize the variable. We do
2965 destructions only if the GUARD is one, i.e., if we are the
2966 last to destroy the variable. */
2967 else if (initp)
2968 guard_cond
2969 = cp_build_binary_op (input_location,
2970 EQ_EXPR,
2971 cp_build_unary_op (PREINCREMENT_EXPR,
2972 guard,
2973 /*noconvert=*/1,
2974 tf_warning_or_error),
2975 integer_one_node,
2976 tf_warning_or_error);
2977 else
2978 guard_cond
2979 = cp_build_binary_op (input_location,
2980 EQ_EXPR,
2981 cp_build_unary_op (PREDECREMENT_EXPR,
2982 guard,
2983 /*noconvert=*/1,
2984 tf_warning_or_error),
2985 integer_zero_node,
2986 tf_warning_or_error);
2988 guard_if_stmt = begin_if_stmt ();
2989 finish_if_stmt_cond (guard_cond, guard_if_stmt);
2993 /* If we're using __cxa_atexit, we have not already set the GUARD,
2994 so we must do so now. */
2995 if (guard && initp && flag_use_cxa_atexit)
2996 finish_expr_stmt (set_guard (guard));
2998 /* Perform the initialization or destruction. */
2999 if (initp)
3001 if (init)
3002 finish_expr_stmt (init);
3004 /* If we're using __cxa_atexit, register a function that calls the
3005 destructor for the object. */
3006 if (flag_use_cxa_atexit)
3007 finish_expr_stmt (register_dtor_fn (decl));
3009 else
3010 finish_expr_stmt (build_cleanup (decl));
3012 /* Finish the guard if-stmt, if necessary. */
3013 if (guard)
3015 finish_then_clause (guard_if_stmt);
3016 finish_if_stmt (guard_if_stmt);
3019 /* Now that we're done with DECL we don't need to pretend to be a
3020 member of its class any longer. */
3021 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3022 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3025 /* Generate code to do the initialization or destruction of the decls in VARS,
3026 a TREE_LIST of VAR_DECL with static storage duration.
3027 Whether initialization or destruction is performed is specified by INITP. */
3029 static void
3030 do_static_initialization_or_destruction (tree vars, bool initp)
3032 tree node, init_if_stmt, cond;
3034 /* Build the outer if-stmt to check for initialization or destruction. */
3035 init_if_stmt = begin_if_stmt ();
3036 cond = initp ? integer_one_node : integer_zero_node;
3037 cond = cp_build_binary_op (input_location,
3038 EQ_EXPR,
3039 initialize_p_decl,
3040 cond,
3041 tf_warning_or_error);
3042 finish_if_stmt_cond (cond, init_if_stmt);
3044 node = vars;
3045 do {
3046 tree decl = TREE_VALUE (node);
3047 tree priority_if_stmt;
3048 int priority;
3049 priority_info pi;
3051 /* If we don't need a destructor, there's nothing to do. Avoid
3052 creating a possibly empty if-stmt. */
3053 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3055 node = TREE_CHAIN (node);
3056 continue;
3059 /* Remember that we had an initialization or finalization at this
3060 priority. */
3061 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3062 pi = get_priority_info (priority);
3063 if (initp)
3064 pi->initializations_p = 1;
3065 else
3066 pi->destructions_p = 1;
3068 /* Conditionalize this initialization on being in the right priority
3069 and being initializing/finalizing appropriately. */
3070 priority_if_stmt = begin_if_stmt ();
3071 cond = cp_build_binary_op (input_location,
3072 EQ_EXPR,
3073 priority_decl,
3074 build_int_cst (NULL_TREE, priority),
3075 tf_warning_or_error);
3076 finish_if_stmt_cond (cond, priority_if_stmt);
3078 /* Process initializers with same priority. */
3079 for (; node
3080 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3081 node = TREE_CHAIN (node))
3082 /* Do one initialization or destruction. */
3083 one_static_initialization_or_destruction (TREE_VALUE (node),
3084 TREE_PURPOSE (node), initp);
3086 /* Finish up the priority if-stmt body. */
3087 finish_then_clause (priority_if_stmt);
3088 finish_if_stmt (priority_if_stmt);
3090 } while (node);
3092 /* Finish up the init/destruct if-stmt body. */
3093 finish_then_clause (init_if_stmt);
3094 finish_if_stmt (init_if_stmt);
3097 /* VARS is a list of variables with static storage duration which may
3098 need initialization and/or finalization. Remove those variables
3099 that don't really need to be initialized or finalized, and return
3100 the resulting list. The order in which the variables appear in
3101 VARS is in reverse order of the order in which they should actually
3102 be initialized. The list we return is in the unreversed order;
3103 i.e., the first variable should be initialized first. */
3105 static tree
3106 prune_vars_needing_no_initialization (tree *vars)
3108 tree *var = vars;
3109 tree result = NULL_TREE;
3111 while (*var)
3113 tree t = *var;
3114 tree decl = TREE_VALUE (t);
3115 tree init = TREE_PURPOSE (t);
3117 /* Deal gracefully with error. */
3118 if (decl == error_mark_node)
3120 var = &TREE_CHAIN (t);
3121 continue;
3124 /* The only things that can be initialized are variables. */
3125 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3127 /* If this object is not defined, we don't need to do anything
3128 here. */
3129 if (DECL_EXTERNAL (decl))
3131 var = &TREE_CHAIN (t);
3132 continue;
3135 /* Also, if the initializer already contains errors, we can bail
3136 out now. */
3137 if (init && TREE_CODE (init) == TREE_LIST
3138 && value_member (error_mark_node, init))
3140 var = &TREE_CHAIN (t);
3141 continue;
3144 /* This variable is going to need initialization and/or
3145 finalization, so we add it to the list. */
3146 *var = TREE_CHAIN (t);
3147 TREE_CHAIN (t) = result;
3148 result = t;
3151 return result;
3154 /* Make sure we have told the back end about all the variables in
3155 VARS. */
3157 static void
3158 write_out_vars (tree vars)
3160 tree v;
3162 for (v = vars; v; v = TREE_CHAIN (v))
3164 tree var = TREE_VALUE (v);
3165 if (!var_finalized_p (var))
3167 import_export_decl (var);
3168 rest_of_decl_compilation (var, 1, 1);
3173 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3174 (otherwise) that will initialize all global objects with static
3175 storage duration having the indicated PRIORITY. */
3177 static void
3178 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3179 location_t *locus)
3181 char function_key;
3182 tree arguments;
3183 tree fndecl;
3184 tree body;
3185 size_t i;
3187 input_location = *locus;
3188 /* ??? */
3189 /* Was: locus->line++; */
3191 /* We use `I' to indicate initialization and `D' to indicate
3192 destruction. */
3193 function_key = constructor_p ? 'I' : 'D';
3195 /* We emit the function lazily, to avoid generating empty
3196 global constructors and destructors. */
3197 body = NULL_TREE;
3199 /* For Objective-C++, we may need to initialize metadata found in this module.
3200 This must be done _before_ any other static initializations. */
3201 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3202 && constructor_p && objc_static_init_needed_p ())
3204 body = start_objects (function_key, priority);
3205 objc_generate_static_init_call (NULL_TREE);
3208 /* Call the static storage duration function with appropriate
3209 arguments. */
3210 for (i = 0; VEC_iterate (tree, ssdf_decls, i, fndecl); ++i)
3212 /* Calls to pure or const functions will expand to nothing. */
3213 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3215 if (! body)
3216 body = start_objects (function_key, priority);
3218 arguments = tree_cons (NULL_TREE,
3219 build_int_cst (NULL_TREE, priority),
3220 NULL_TREE);
3221 arguments = tree_cons (NULL_TREE,
3222 build_int_cst (NULL_TREE, constructor_p),
3223 arguments);
3224 finish_expr_stmt (cp_build_function_call (fndecl, arguments,
3225 tf_warning_or_error));
3229 /* Close out the function. */
3230 if (body)
3231 finish_objects (function_key, priority, body);
3234 /* Generate constructor and destructor functions for the priority
3235 indicated by N. */
3237 static int
3238 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3240 location_t *locus = (location_t *) data;
3241 int priority = (int) n->key;
3242 priority_info pi = (priority_info) n->value;
3244 /* Generate the functions themselves, but only if they are really
3245 needed. */
3246 if (pi->initializations_p)
3247 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3248 if (pi->destructions_p)
3249 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3251 /* Keep iterating. */
3252 return 0;
3255 /* Called via LANGHOOK_CALLGRAPH_ANALYZE_EXPR. It is supposed to mark
3256 decls referenced from front-end specific constructs; it will be called
3257 only for language-specific tree nodes.
3259 Here we must deal with member pointers. */
3261 tree
3262 cxx_callgraph_analyze_expr (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED)
3264 tree t = *tp;
3266 switch (TREE_CODE (t))
3268 case PTRMEM_CST:
3269 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3270 cgraph_mark_needed_node (cgraph_node (PTRMEM_CST_MEMBER (t)));
3271 break;
3272 case BASELINK:
3273 if (TREE_CODE (BASELINK_FUNCTIONS (t)) == FUNCTION_DECL)
3274 cgraph_mark_needed_node (cgraph_node (BASELINK_FUNCTIONS (t)));
3275 break;
3276 case VAR_DECL:
3277 if (DECL_VTABLE_OR_VTT_P (t))
3279 /* The ABI requires that all virtual tables be emitted
3280 whenever one of them is. */
3281 tree vtbl;
3282 for (vtbl = CLASSTYPE_VTABLES (DECL_CONTEXT (t));
3283 vtbl;
3284 vtbl = TREE_CHAIN (vtbl))
3285 mark_decl_referenced (vtbl);
3287 else if (DECL_CONTEXT (t)
3288 && flag_use_repository
3289 && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
3290 /* If we need a static variable in a function, then we
3291 need the containing function. */
3292 mark_decl_referenced (DECL_CONTEXT (t));
3293 break;
3294 default:
3295 break;
3298 return NULL;
3301 /* Java requires that we be able to reference a local address for a
3302 method, and not be confused by PLT entries. If hidden aliases are
3303 supported, emit one for each java function that we've emitted. */
3305 static void
3306 build_java_method_aliases (void)
3308 struct cgraph_node *node;
3310 #ifndef HAVE_GAS_HIDDEN
3311 return;
3312 #endif
3314 for (node = cgraph_nodes; node ; node = node->next)
3316 tree fndecl = node->decl;
3318 if (TREE_ASM_WRITTEN (fndecl)
3319 && DECL_CONTEXT (fndecl)
3320 && TYPE_P (DECL_CONTEXT (fndecl))
3321 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3322 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3324 /* Mangle the name in a predictable way; we need to reference
3325 this from a java compiled object file. */
3326 tree oid, nid, alias;
3327 const char *oname;
3328 char *nname;
3330 oid = DECL_ASSEMBLER_NAME (fndecl);
3331 oname = IDENTIFIER_POINTER (oid);
3332 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3333 nname = ACONCAT (("_ZGA", oname+2, NULL));
3334 nid = get_identifier (nname);
3336 alias = make_alias_for (fndecl, nid);
3337 TREE_PUBLIC (alias) = 1;
3338 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3340 assemble_alias (alias, oid);
3345 /* This routine is called at the end of compilation.
3346 Its job is to create all the code needed to initialize and
3347 destroy the global aggregates. We do the destruction
3348 first, since that way we only need to reverse the decls once. */
3350 void
3351 cp_write_global_declarations (void)
3353 tree vars;
3354 bool reconsider;
3355 size_t i;
3356 location_t locus;
3357 unsigned ssdf_count = 0;
3358 int retries = 0;
3359 tree decl;
3361 locus = input_location;
3362 at_eof = 1;
3364 /* Bad parse errors. Just forget about it. */
3365 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3366 return;
3368 if (pch_file)
3369 c_common_write_pch ();
3371 /* FIXME - huh? was input_line -= 1;*/
3373 /* We now have to write out all the stuff we put off writing out.
3374 These include:
3376 o Template specializations that we have not yet instantiated,
3377 but which are needed.
3378 o Initialization and destruction for non-local objects with
3379 static storage duration. (Local objects with static storage
3380 duration are initialized when their scope is first entered,
3381 and are cleaned up via atexit.)
3382 o Virtual function tables.
3384 All of these may cause others to be needed. For example,
3385 instantiating one function may cause another to be needed, and
3386 generating the initializer for an object may cause templates to be
3387 instantiated, etc., etc. */
3389 timevar_push (TV_VARCONST);
3391 emit_support_tinfos ();
3395 tree t;
3396 tree decl;
3398 reconsider = false;
3400 /* If there are templates that we've put off instantiating, do
3401 them now. */
3402 instantiate_pending_templates (retries);
3403 ggc_collect ();
3405 /* Write out virtual tables as required. Note that writing out
3406 the virtual table for a template class may cause the
3407 instantiation of members of that class. If we write out
3408 vtables then we remove the class from our list so we don't
3409 have to look at it again. */
3411 while (keyed_classes != NULL_TREE
3412 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
3414 reconsider = true;
3415 keyed_classes = TREE_CHAIN (keyed_classes);
3418 t = keyed_classes;
3419 if (t != NULL_TREE)
3421 tree next = TREE_CHAIN (t);
3423 while (next)
3425 if (maybe_emit_vtables (TREE_VALUE (next)))
3427 reconsider = true;
3428 TREE_CHAIN (t) = TREE_CHAIN (next);
3430 else
3431 t = next;
3433 next = TREE_CHAIN (t);
3437 /* Write out needed type info variables. We have to be careful
3438 looping through unemitted decls, because emit_tinfo_decl may
3439 cause other variables to be needed. New elements will be
3440 appended, and we remove from the vector those that actually
3441 get emitted. */
3442 for (i = VEC_length (tree, unemitted_tinfo_decls);
3443 VEC_iterate (tree, unemitted_tinfo_decls, --i, t);)
3444 if (emit_tinfo_decl (t))
3446 reconsider = true;
3447 VEC_unordered_remove (tree, unemitted_tinfo_decls, i);
3450 /* The list of objects with static storage duration is built up
3451 in reverse order. We clear STATIC_AGGREGATES so that any new
3452 aggregates added during the initialization of these will be
3453 initialized in the correct order when we next come around the
3454 loop. */
3455 vars = prune_vars_needing_no_initialization (&static_aggregates);
3457 if (vars)
3459 /* We need to start a new initialization function each time
3460 through the loop. That's because we need to know which
3461 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3462 isn't computed until a function is finished, and written
3463 out. That's a deficiency in the back end. When this is
3464 fixed, these initialization functions could all become
3465 inline, with resulting performance improvements. */
3466 tree ssdf_body;
3468 /* Set the line and file, so that it is obviously not from
3469 the source file. */
3470 input_location = locus;
3471 ssdf_body = start_static_storage_duration_function (ssdf_count);
3473 /* Make sure the back end knows about all the variables. */
3474 write_out_vars (vars);
3476 /* First generate code to do all the initializations. */
3477 if (vars)
3478 do_static_initialization_or_destruction (vars, /*initp=*/true);
3480 /* Then, generate code to do all the destructions. Do these
3481 in reverse order so that the most recently constructed
3482 variable is the first destroyed. If we're using
3483 __cxa_atexit, then we don't need to do this; functions
3484 were registered at initialization time to destroy the
3485 local statics. */
3486 if (!flag_use_cxa_atexit && vars)
3488 vars = nreverse (vars);
3489 do_static_initialization_or_destruction (vars, /*initp=*/false);
3491 else
3492 vars = NULL_TREE;
3494 /* Finish up the static storage duration function for this
3495 round. */
3496 input_location = locus;
3497 finish_static_storage_duration_function (ssdf_body);
3499 /* All those initializations and finalizations might cause
3500 us to need more inline functions, more template
3501 instantiations, etc. */
3502 reconsider = true;
3503 ssdf_count++;
3504 /* ??? was: locus.line++; */
3507 /* Go through the set of inline functions whose bodies have not
3508 been emitted yet. If out-of-line copies of these functions
3509 are required, emit them. */
3510 for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3512 /* Does it need synthesizing? */
3513 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
3514 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
3516 /* Even though we're already at the top-level, we push
3517 there again. That way, when we pop back a few lines
3518 hence, all of our state is restored. Otherwise,
3519 finish_function doesn't clean things up, and we end
3520 up with CURRENT_FUNCTION_DECL set. */
3521 push_to_top_level ();
3522 /* The decl's location will mark where it was first
3523 needed. Save that so synthesize method can indicate
3524 where it was needed from, in case of error */
3525 input_location = DECL_SOURCE_LOCATION (decl);
3526 synthesize_method (decl);
3527 pop_from_top_level ();
3528 reconsider = true;
3531 if (!gimple_body (decl))
3532 continue;
3534 /* We lie to the back end, pretending that some functions
3535 are not defined when they really are. This keeps these
3536 functions from being put out unnecessarily. But, we must
3537 stop lying when the functions are referenced, or if they
3538 are not comdat since they need to be put out now. If
3539 DECL_INTERFACE_KNOWN, then we have already set
3540 DECL_EXTERNAL appropriately, so there's no need to check
3541 again, and we do not want to clear DECL_EXTERNAL if a
3542 previous call to import_export_decl set it.
3544 This is done in a separate for cycle, because if some
3545 deferred function is contained in another deferred
3546 function later in deferred_fns varray,
3547 rest_of_compilation would skip this function and we
3548 really cannot expand the same function twice. */
3549 import_export_decl (decl);
3550 if (DECL_NOT_REALLY_EXTERN (decl)
3551 && DECL_INITIAL (decl)
3552 && decl_needed_p (decl))
3553 DECL_EXTERNAL (decl) = 0;
3555 /* If we're going to need to write this function out, and
3556 there's already a body for it, create RTL for it now.
3557 (There might be no body if this is a method we haven't
3558 gotten around to synthesizing yet.) */
3559 if (!DECL_EXTERNAL (decl)
3560 && decl_needed_p (decl)
3561 && !TREE_ASM_WRITTEN (decl)
3562 && !cgraph_node (decl)->local.finalized)
3564 /* We will output the function; no longer consider it in this
3565 loop. */
3566 DECL_DEFER_OUTPUT (decl) = 0;
3567 /* Generate RTL for this function now that we know we
3568 need it. */
3569 expand_or_defer_fn (decl);
3570 /* If we're compiling -fsyntax-only pretend that this
3571 function has been written out so that we don't try to
3572 expand it again. */
3573 if (flag_syntax_only)
3574 TREE_ASM_WRITTEN (decl) = 1;
3575 reconsider = true;
3579 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3580 reconsider = true;
3582 /* Static data members are just like namespace-scope globals. */
3583 for (i = 0; VEC_iterate (tree, pending_statics, i, decl); ++i)
3585 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
3586 /* Don't write it out if we haven't seen a definition. */
3587 || DECL_IN_AGGR_P (decl))
3588 continue;
3589 import_export_decl (decl);
3590 /* If this static data member is needed, provide it to the
3591 back end. */
3592 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
3593 DECL_EXTERNAL (decl) = 0;
3595 if (VEC_length (tree, pending_statics) != 0
3596 && wrapup_global_declarations (VEC_address (tree, pending_statics),
3597 VEC_length (tree, pending_statics)))
3598 reconsider = true;
3600 retries++;
3602 while (reconsider);
3604 /* All used inline functions must have a definition at this point. */
3605 for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3607 if (/* Check online inline functions that were actually used. */
3608 TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
3609 /* If the definition actually was available here, then the
3610 fact that the function was not defined merely represents
3611 that for some reason (use of a template repository,
3612 #pragma interface, etc.) we decided not to emit the
3613 definition here. */
3614 && !DECL_INITIAL (decl)
3615 /* An explicit instantiation can be used to specify
3616 that the body is in another unit. It will have
3617 already verified there was a definition. */
3618 && !DECL_EXPLICIT_INSTANTIATION (decl))
3620 warning (0, "inline function %q+D used but never defined", decl);
3621 /* Avoid a duplicate warning from check_global_declaration_1. */
3622 TREE_NO_WARNING (decl) = 1;
3626 /* We give C linkage to static constructors and destructors. */
3627 push_lang_context (lang_name_c);
3629 /* Generate initialization and destruction functions for all
3630 priorities for which they are required. */
3631 if (priority_info_map)
3632 splay_tree_foreach (priority_info_map,
3633 generate_ctor_and_dtor_functions_for_priority,
3634 /*data=*/&locus);
3635 else if (c_dialect_objc () && objc_static_init_needed_p ())
3636 /* If this is obj-c++ and we need a static init, call
3637 generate_ctor_or_dtor_function. */
3638 generate_ctor_or_dtor_function (/*constructor_p=*/true,
3639 DEFAULT_INIT_PRIORITY, &locus);
3641 /* We're done with the splay-tree now. */
3642 if (priority_info_map)
3643 splay_tree_delete (priority_info_map);
3645 /* Generate any missing aliases. */
3646 maybe_apply_pending_pragma_weaks ();
3648 /* We're done with static constructors, so we can go back to "C++"
3649 linkage now. */
3650 pop_lang_context ();
3652 cgraph_finalize_compilation_unit ();
3653 cgraph_optimize ();
3655 /* Now, issue warnings about static, but not defined, functions,
3656 etc., and emit debugging information. */
3657 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3658 if (VEC_length (tree, pending_statics) != 0)
3660 check_global_declarations (VEC_address (tree, pending_statics),
3661 VEC_length (tree, pending_statics));
3662 emit_debug_global_declarations (VEC_address (tree, pending_statics),
3663 VEC_length (tree, pending_statics));
3666 /* Generate hidden aliases for Java. */
3667 build_java_method_aliases ();
3669 finish_repo ();
3671 /* The entire file is now complete. If requested, dump everything
3672 to a file. */
3674 int flags;
3675 FILE *stream = dump_begin (TDI_tu, &flags);
3677 if (stream)
3679 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3680 dump_end (TDI_tu, stream);
3684 timevar_pop (TV_VARCONST);
3686 if (flag_detailed_statistics)
3688 dump_tree_statistics ();
3689 dump_time_statistics ();
3691 input_location = locus;
3693 #ifdef ENABLE_CHECKING
3694 validate_conversion_obstack ();
3695 #endif /* ENABLE_CHECKING */
3698 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
3699 function to call in parse-tree form; it has not yet been
3700 semantically analyzed. ARGS are the arguments to the function.
3701 They have already been semantically analyzed. */
3703 tree
3704 build_offset_ref_call_from_tree (tree fn, tree args)
3706 tree orig_fn;
3707 tree orig_args;
3708 tree expr;
3709 tree object;
3711 orig_fn = fn;
3712 orig_args = args;
3713 object = TREE_OPERAND (fn, 0);
3715 if (processing_template_decl)
3717 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
3718 || TREE_CODE (fn) == MEMBER_REF);
3719 if (type_dependent_expression_p (fn)
3720 || any_type_dependent_arguments_p (args))
3721 return build_nt_call_list (fn, args);
3723 /* Transform the arguments and add the implicit "this"
3724 parameter. That must be done before the FN is transformed
3725 because we depend on the form of FN. */
3726 args = build_non_dependent_args (args);
3727 object = build_non_dependent_expr (object);
3728 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3730 if (TREE_CODE (fn) == DOTSTAR_EXPR)
3731 object = cp_build_unary_op (ADDR_EXPR, object, 0, tf_warning_or_error);
3732 args = tree_cons (NULL_TREE, object, args);
3734 /* Now that the arguments are done, transform FN. */
3735 fn = build_non_dependent_expr (fn);
3738 /* A qualified name corresponding to a bound pointer-to-member is
3739 represented as an OFFSET_REF:
3741 struct B { void g(); };
3742 void (B::*p)();
3743 void B::g() { (this->*p)(); } */
3744 if (TREE_CODE (fn) == OFFSET_REF)
3746 tree object_addr = cp_build_unary_op (ADDR_EXPR, object, 0,
3747 tf_warning_or_error);
3748 fn = TREE_OPERAND (fn, 1);
3749 fn = get_member_function_from_ptrfunc (&object_addr, fn);
3750 args = tree_cons (NULL_TREE, object_addr, args);
3753 if (CLASS_TYPE_P (TREE_TYPE (fn)))
3754 expr = build_object_call (fn, args, tf_warning_or_error);
3755 else
3756 expr = cp_build_function_call (fn, args, tf_warning_or_error);
3757 if (processing_template_decl && expr != error_mark_node)
3758 return build_min_non_dep_call_list (expr, orig_fn, orig_args);
3759 return expr;
3763 void
3764 check_default_args (tree x)
3766 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
3767 bool saw_def = false;
3768 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
3769 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
3771 if (TREE_PURPOSE (arg))
3772 saw_def = true;
3773 else if (saw_def)
3775 error ("default argument missing for parameter %P of %q+#D", i, x);
3776 TREE_PURPOSE (arg) = error_mark_node;
3781 /* Return true if function DECL can be inlined. This is used to force
3782 instantiation of methods that might be interesting for inlining. */
3783 bool
3784 possibly_inlined_p (tree decl)
3786 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
3787 if (DECL_UNINLINABLE (decl))
3788 return false;
3789 if (!optimize || pragma_java_exceptions)
3790 return DECL_DECLARED_INLINE_P (decl);
3791 /* When optimizing, we might inline everything when flatten
3792 attribute or heuristics inlining for size or autoinlining
3793 is used. */
3794 return true;
3797 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
3798 If DECL is a specialization or implicitly declared class member,
3799 generate the actual definition. */
3801 void
3802 mark_used (tree decl)
3804 /* If DECL is a BASELINK for a single function, then treat it just
3805 like the DECL for the function. Otherwise, if the BASELINK is
3806 for an overloaded function, we don't know which function was
3807 actually used until after overload resolution. */
3808 if (TREE_CODE (decl) == BASELINK)
3810 decl = BASELINK_FUNCTIONS (decl);
3811 if (really_overloaded_fn (decl))
3812 return;
3813 decl = OVL_CURRENT (decl);
3816 TREE_USED (decl) = 1;
3817 if (DECL_CLONED_FUNCTION_P (decl))
3818 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
3819 if (TREE_CODE (decl) == FUNCTION_DECL
3820 && DECL_DELETED_FN (decl))
3822 error ("deleted function %q+D", decl);
3823 error ("used here");
3824 return;
3827 /* If within finish_function, defer the rest until that function
3828 finishes, otherwise it might recurse. */
3829 if (defer_mark_used_calls)
3831 VEC_safe_push (tree, gc, deferred_mark_used_calls, decl);
3832 return;
3835 /* Normally, we can wait until instantiation-time to synthesize
3836 DECL. However, if DECL is a static data member initialized with
3837 a constant, we need the value right now because a reference to
3838 such a data member is not value-dependent. */
3839 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl)
3840 && !DECL_INITIAL (decl)
3841 && DECL_LANG_SPECIFIC (decl)
3842 && DECL_TEMPLATE_INSTANTIATION (decl))
3844 /* Don't try to instantiate members of dependent types. We
3845 cannot just use dependent_type_p here because this function
3846 may be called from fold_non_dependent_expr, and then we may
3847 see dependent types, even though processing_template_decl
3848 will not be set. */
3849 if (CLASSTYPE_TEMPLATE_INFO ((DECL_CONTEXT (decl)))
3850 && uses_template_parms (CLASSTYPE_TI_ARGS (DECL_CONTEXT (decl))))
3851 return;
3852 instantiate_decl (decl, /*defer_ok=*/false,
3853 /*expl_inst_class_mem_p=*/false);
3856 /* If we don't need a value, then we don't need to synthesize DECL. */
3857 if (skip_evaluation)
3858 return;
3860 if (processing_template_decl)
3861 return;
3863 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
3864 && !TREE_ASM_WRITTEN (decl))
3865 /* Remember it, so we can check it was defined. */
3867 if (DECL_DEFERRED_FN (decl))
3868 return;
3870 /* Remember the current location for a function we will end up
3871 synthesizing. Then we can inform the user where it was
3872 required in the case of error. */
3873 if (DECL_ARTIFICIAL (decl) && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
3874 && !DECL_THUNK_P (decl))
3875 DECL_SOURCE_LOCATION (decl) = input_location;
3877 note_vague_linkage_fn (decl);
3880 /* Is it a synthesized method that needs to be synthesized? */
3881 if (TREE_CODE (decl) == FUNCTION_DECL
3882 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
3883 && DECL_DEFAULTED_FN (decl)
3884 && !DECL_THUNK_P (decl)
3885 && ! DECL_INITIAL (decl)
3886 /* Kludge: don't synthesize for default args. Unfortunately this
3887 rules out initializers of namespace-scoped objects too, but
3888 it's sort-of ok if the implicit ctor or dtor decl keeps
3889 pointing to the class location. */
3890 && current_function_decl)
3892 synthesize_method (decl);
3893 /* If we've already synthesized the method we don't need to
3894 do the instantiation test below. */
3896 else if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
3897 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3898 && (!DECL_EXPLICIT_INSTANTIATION (decl)
3899 || (TREE_CODE (decl) == FUNCTION_DECL
3900 && possibly_inlined_p
3901 (DECL_TEMPLATE_RESULT (
3902 template_for_substitution (decl))))
3903 /* We need to instantiate static data members so that there
3904 initializers are available in integral constant
3905 expressions. */
3906 || (TREE_CODE (decl) == VAR_DECL
3907 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))))
3908 /* If this is a function or variable that is an instance of some
3909 template, we now know that we will need to actually do the
3910 instantiation. We check that DECL is not an explicit
3911 instantiation because that is not checked in instantiate_decl.
3913 We put off instantiating functions in order to improve compile
3914 times. Maintaining a stack of active functions is expensive,
3915 and the inliner knows to instantiate any functions it might
3916 need. Therefore, we always try to defer instantiation. */
3917 instantiate_decl (decl, /*defer_ok=*/true,
3918 /*expl_inst_class_mem_p=*/false);
3921 /* Given function PARM_DECL PARM, return its index in the function's list
3922 of parameters, beginning with 1. */
3925 parm_index (tree parm)
3927 int index;
3928 tree arg;
3930 for (index = 1, arg = DECL_ARGUMENTS (DECL_CONTEXT (parm));
3931 arg;
3932 ++index, arg = TREE_CHAIN (arg))
3934 if (DECL_NAME (parm) == DECL_NAME (arg))
3935 break;
3936 if (DECL_ARTIFICIAL (arg))
3937 --index;
3940 gcc_assert (arg);
3941 return index;
3944 #include "gt-cp-decl2.h"