2013-06-20 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / decl2.c
blob1573cede8992a5a2e9188af6514e2bd91905c61b
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "decl.h"
37 #include "toplev.h"
38 #include "timevar.h"
39 #include "cpplib.h"
40 #include "target.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
43 #include "cgraph.h"
44 #include "tree-inline.h"
45 #include "c-family/c-pragma.h"
46 #include "dumpfile.h"
47 #include "intl.h"
48 #include "gimple.h"
49 #include "pointer-set.h"
50 #include "splay-tree.h"
51 #include "langhooks.h"
52 #include "c-family/c-ada-spec.h"
54 extern cpp_reader *parse_in;
56 /* This structure contains information about the initializations
57 and/or destructions required for a particular priority level. */
58 typedef struct priority_info_s {
59 /* Nonzero if there have been any initializations at this priority
60 throughout the translation unit. */
61 int initializations_p;
62 /* Nonzero if there have been any destructions at this priority
63 throughout the translation unit. */
64 int destructions_p;
65 } *priority_info;
67 static void mark_vtable_entries (tree);
68 static bool maybe_emit_vtables (tree);
69 static bool acceptable_java_type (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static bool decl_defined_p (tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec<tree, va_gc> *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec<tree, va_gc> *deferred_fns;
97 /* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
101 /* Nonzero if we're done parsing and into end-of-file activities. */
103 int at_eof;
107 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
108 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
109 that apply to the function). */
111 tree
112 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
113 cp_ref_qualifier rqual)
115 tree raises;
116 tree attrs;
117 int type_quals;
119 if (fntype == error_mark_node || ctype == error_mark_node)
120 return error_mark_node;
122 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
123 || TREE_CODE (fntype) == METHOD_TYPE);
125 type_quals = quals & ~TYPE_QUAL_RESTRICT;
126 ctype = cp_build_qualified_type (ctype, type_quals);
127 raises = TYPE_RAISES_EXCEPTIONS (fntype);
128 attrs = TYPE_ATTRIBUTES (fntype);
129 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
130 (TREE_CODE (fntype) == METHOD_TYPE
131 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
132 : TYPE_ARG_TYPES (fntype)));
133 if (attrs)
134 fntype = cp_build_type_attribute_variant (fntype, attrs);
135 if (rqual)
136 fntype = build_ref_qualified_type (fntype, rqual);
137 if (raises)
138 fntype = build_exception_variant (fntype, raises);
140 return fntype;
143 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
144 return type changed to NEW_RET. */
146 tree
147 change_return_type (tree new_ret, tree fntype)
149 tree newtype;
150 tree args = TYPE_ARG_TYPES (fntype);
151 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
152 tree attrs = TYPE_ATTRIBUTES (fntype);
154 if (new_ret == error_mark_node)
155 return fntype;
157 if (same_type_p (new_ret, TREE_TYPE (fntype)))
158 return fntype;
160 if (TREE_CODE (fntype) == FUNCTION_TYPE)
162 newtype = build_function_type (new_ret, args);
163 newtype = apply_memfn_quals (newtype,
164 type_memfn_quals (fntype),
165 type_memfn_rqual (fntype));
167 else
168 newtype = build_method_type_directly
169 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
170 if (raises)
171 newtype = build_exception_variant (newtype, raises);
172 if (attrs)
173 newtype = cp_build_type_attribute_variant (newtype, attrs);
175 return newtype;
178 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
179 appropriately. */
181 tree
182 cp_build_parm_decl (tree name, tree type)
184 tree parm = build_decl (input_location,
185 PARM_DECL, name, type);
186 /* DECL_ARG_TYPE is only used by the back end and the back end never
187 sees templates. */
188 if (!processing_template_decl)
189 DECL_ARG_TYPE (parm) = type_passed_as (type);
191 /* If the type is a pack expansion, then we have a function
192 parameter pack. */
193 if (type && TREE_CODE (type) == TYPE_PACK_EXPANSION)
194 FUNCTION_PARAMETER_PACK_P (parm) = 1;
196 return parm;
199 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
200 indicated NAME. */
202 tree
203 build_artificial_parm (tree name, tree type)
205 tree parm = cp_build_parm_decl (name, type);
206 DECL_ARTIFICIAL (parm) = 1;
207 /* All our artificial parms are implicitly `const'; they cannot be
208 assigned to. */
209 TREE_READONLY (parm) = 1;
210 return parm;
213 /* Constructors for types with virtual baseclasses need an "in-charge" flag
214 saying whether this constructor is responsible for initialization of
215 virtual baseclasses or not. All destructors also need this "in-charge"
216 flag, which additionally determines whether or not the destructor should
217 free the memory for the object.
219 This function adds the "in-charge" flag to member function FN if
220 appropriate. It is called from grokclassfn and tsubst.
221 FN must be either a constructor or destructor.
223 The in-charge flag follows the 'this' parameter, and is followed by the
224 VTT parm (if any), then the user-written parms. */
226 void
227 maybe_retrofit_in_chrg (tree fn)
229 tree basetype, arg_types, parms, parm, fntype;
231 /* If we've already add the in-charge parameter don't do it again. */
232 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
233 return;
235 /* When processing templates we can't know, in general, whether or
236 not we're going to have virtual baseclasses. */
237 if (processing_template_decl)
238 return;
240 /* We don't need an in-charge parameter for constructors that don't
241 have virtual bases. */
242 if (DECL_CONSTRUCTOR_P (fn)
243 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
244 return;
246 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
247 basetype = TREE_TYPE (TREE_VALUE (arg_types));
248 arg_types = TREE_CHAIN (arg_types);
250 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
252 /* If this is a subobject constructor or destructor, our caller will
253 pass us a pointer to our VTT. */
254 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
256 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
258 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
259 DECL_CHAIN (parm) = parms;
260 parms = parm;
262 /* ...and then to TYPE_ARG_TYPES. */
263 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
265 DECL_HAS_VTT_PARM_P (fn) = 1;
268 /* Then add the in-charge parm (before the VTT parm). */
269 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
270 DECL_CHAIN (parm) = parms;
271 parms = parm;
272 arg_types = hash_tree_chain (integer_type_node, arg_types);
274 /* Insert our new parameter(s) into the list. */
275 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
277 /* And rebuild the function type. */
278 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
279 arg_types);
280 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
281 fntype = build_exception_variant (fntype,
282 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
283 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
284 fntype = (cp_build_type_attribute_variant
285 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
286 TREE_TYPE (fn) = fntype;
288 /* Now we've got the in-charge parameter. */
289 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
292 /* Classes overload their constituent function names automatically.
293 When a function name is declared in a record structure,
294 its name is changed to it overloaded name. Since names for
295 constructors and destructors can conflict, we place a leading
296 '$' for destructors.
298 CNAME is the name of the class we are grokking for.
300 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
302 FLAGS contains bits saying what's special about today's
303 arguments. DTOR_FLAG == DESTRUCTOR.
305 If FUNCTION is a destructor, then we must add the `auto-delete' field
306 as a second parameter. There is some hair associated with the fact
307 that we must "declare" this variable in the manner consistent with the
308 way the rest of the arguments were declared.
310 QUALS are the qualifiers for the this pointer. */
312 void
313 grokclassfn (tree ctype, tree function, enum overload_flags flags)
315 tree fn_name = DECL_NAME (function);
317 /* Even within an `extern "C"' block, members get C++ linkage. See
318 [dcl.link] for details. */
319 SET_DECL_LANGUAGE (function, lang_cplusplus);
321 if (fn_name == NULL_TREE)
323 error ("name missing for member function");
324 fn_name = get_identifier ("<anonymous>");
325 DECL_NAME (function) = fn_name;
328 DECL_CONTEXT (function) = ctype;
330 if (flags == DTOR_FLAG)
331 DECL_DESTRUCTOR_P (function) = 1;
333 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
334 maybe_retrofit_in_chrg (function);
337 /* Create an ARRAY_REF, checking for the user doing things backwards
338 along the way. DECLTYPE_P is for N3276, as in the parser. */
340 tree
341 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
342 bool decltype_p)
344 tree type;
345 tree expr;
346 tree orig_array_expr = array_expr;
347 tree orig_index_exp = index_exp;
349 if (error_operand_p (array_expr) || error_operand_p (index_exp))
350 return error_mark_node;
352 if (processing_template_decl)
354 if (type_dependent_expression_p (array_expr)
355 || type_dependent_expression_p (index_exp))
356 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
357 NULL_TREE, NULL_TREE);
358 array_expr = build_non_dependent_expr (array_expr);
359 index_exp = build_non_dependent_expr (index_exp);
362 type = TREE_TYPE (array_expr);
363 gcc_assert (type);
364 type = non_reference (type);
366 /* If they have an `operator[]', use that. */
367 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
369 tsubst_flags_t complain = tf_warning_or_error;
370 if (decltype_p)
371 complain |= tf_decltype;
372 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
373 index_exp, NULL_TREE, /*overload=*/NULL, complain);
375 else
377 tree p1, p2, i1, i2;
379 /* Otherwise, create an ARRAY_REF for a pointer or array type.
380 It is a little-known fact that, if `a' is an array and `i' is
381 an int, you can write `i[a]', which means the same thing as
382 `a[i]'. */
383 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
384 p1 = array_expr;
385 else
386 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
388 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
389 p2 = index_exp;
390 else
391 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
393 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
394 false);
395 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
396 false);
398 if ((p1 && i2) && (i1 && p2))
399 error ("ambiguous conversion for array subscript");
401 if (p1 && i2)
402 array_expr = p1, index_exp = i2;
403 else if (i1 && p2)
404 array_expr = p2, index_exp = i1;
405 else
407 error ("invalid types %<%T[%T]%> for array subscript",
408 type, TREE_TYPE (index_exp));
409 return error_mark_node;
412 if (array_expr == error_mark_node || index_exp == error_mark_node)
413 error ("ambiguous conversion for array subscript");
415 expr = build_array_ref (input_location, array_expr, index_exp);
417 if (processing_template_decl && expr != error_mark_node)
418 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
419 NULL_TREE, NULL_TREE);
420 return expr;
423 /* Given the cast expression EXP, checking out its validity. Either return
424 an error_mark_node if there was an unavoidable error, return a cast to
425 void for trying to delete a pointer w/ the value 0, or return the
426 call to delete. If DOING_VEC is true, we handle things differently
427 for doing an array delete.
428 Implements ARM $5.3.4. This is called from the parser. */
430 tree
431 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
432 tsubst_flags_t complain)
434 tree t, type;
436 if (exp == error_mark_node)
437 return exp;
439 if (processing_template_decl)
441 t = build_min (DELETE_EXPR, void_type_node, exp, size);
442 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
443 DELETE_EXPR_USE_VEC (t) = doing_vec;
444 TREE_SIDE_EFFECTS (t) = 1;
445 return t;
448 /* An array can't have been allocated by new, so complain. */
449 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
450 warning (0, "deleting array %q#E", exp);
452 t = build_expr_type_conversion (WANT_POINTER, exp, true);
454 if (t == NULL_TREE || t == error_mark_node)
456 error ("type %q#T argument given to %<delete%>, expected pointer",
457 TREE_TYPE (exp));
458 return error_mark_node;
461 type = TREE_TYPE (t);
463 /* As of Valley Forge, you can delete a pointer to const. */
465 /* You can't delete functions. */
466 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
468 error ("cannot delete a function. Only pointer-to-objects are "
469 "valid arguments to %<delete%>");
470 return error_mark_node;
473 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
474 if (VOID_TYPE_P (TREE_TYPE (type)))
476 warning (0, "deleting %qT is undefined", type);
477 doing_vec = 0;
480 /* Deleting a pointer with the value zero is valid and has no effect. */
481 if (integer_zerop (t))
482 return build1 (NOP_EXPR, void_type_node, t);
484 if (doing_vec)
485 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
486 sfk_deleting_destructor,
487 use_global_delete, complain);
488 else
489 return build_delete (type, t, sfk_deleting_destructor,
490 LOOKUP_NORMAL, use_global_delete,
491 complain);
494 /* Report an error if the indicated template declaration is not the
495 sort of thing that should be a member template. */
497 void
498 check_member_template (tree tmpl)
500 tree decl;
502 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
503 decl = DECL_TEMPLATE_RESULT (tmpl);
505 if (TREE_CODE (decl) == FUNCTION_DECL
506 || DECL_ALIAS_TEMPLATE_P (tmpl)
507 || (TREE_CODE (decl) == TYPE_DECL
508 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
510 /* The parser rejects template declarations in local classes. */
511 gcc_assert (!current_function_decl);
512 /* The parser rejects any use of virtual in a function template. */
513 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
514 && DECL_VIRTUAL_P (decl)));
516 /* The debug-information generating code doesn't know what to do
517 with member templates. */
518 DECL_IGNORED_P (tmpl) = 1;
520 else
521 error ("template declaration of %q#D", decl);
524 /* Return true iff TYPE is a valid Java parameter or return type. */
526 static bool
527 acceptable_java_type (tree type)
529 if (type == error_mark_node)
530 return false;
532 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
533 return true;
534 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
536 type = TREE_TYPE (type);
537 if (TREE_CODE (type) == RECORD_TYPE)
539 tree args; int i;
540 if (! TYPE_FOR_JAVA (type))
541 return false;
542 if (! CLASSTYPE_TEMPLATE_INFO (type))
543 return true;
544 args = CLASSTYPE_TI_ARGS (type);
545 i = TREE_VEC_LENGTH (args);
546 while (--i >= 0)
548 type = TREE_VEC_ELT (args, i);
549 if (TYPE_PTR_P (type))
550 type = TREE_TYPE (type);
551 if (! TYPE_FOR_JAVA (type))
552 return false;
554 return true;
557 return false;
560 /* For a METHOD in a Java class CTYPE, return true if
561 the parameter and return types are valid Java types.
562 Otherwise, print appropriate error messages, and return false. */
564 bool
565 check_java_method (tree method)
567 bool jerr = false;
568 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
569 tree ret_type = TREE_TYPE (TREE_TYPE (method));
571 if (!acceptable_java_type (ret_type))
573 error ("Java method %qD has non-Java return type %qT",
574 method, ret_type);
575 jerr = true;
578 arg_types = TREE_CHAIN (arg_types);
579 if (DECL_HAS_IN_CHARGE_PARM_P (method))
580 arg_types = TREE_CHAIN (arg_types);
581 if (DECL_HAS_VTT_PARM_P (method))
582 arg_types = TREE_CHAIN (arg_types);
584 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
586 tree type = TREE_VALUE (arg_types);
587 if (!acceptable_java_type (type))
589 if (type != error_mark_node)
590 error ("Java method %qD has non-Java parameter type %qT",
591 method, type);
592 jerr = true;
595 return !jerr;
598 /* Sanity check: report error if this function FUNCTION is not
599 really a member of the class (CTYPE) it is supposed to belong to.
600 TEMPLATE_PARMS is used to specify the template parameters of a member
601 template passed as FUNCTION_DECL. If the member template is passed as a
602 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
603 from the declaration. If the function is not a function template, it
604 must be NULL.
605 It returns the original declaration for the function, NULL_TREE if
606 no declaration was found, error_mark_node if an error was emitted. */
608 tree
609 check_classfn (tree ctype, tree function, tree template_parms)
611 int ix;
612 bool is_template;
613 tree pushed_scope;
615 if (DECL_USE_TEMPLATE (function)
616 && !(TREE_CODE (function) == TEMPLATE_DECL
617 && DECL_TEMPLATE_SPECIALIZATION (function))
618 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
619 /* Since this is a specialization of a member template,
620 we're not going to find the declaration in the class.
621 For example, in:
623 struct S { template <typename T> void f(T); };
624 template <> void S::f(int);
626 we're not going to find `S::f(int)', but there's no
627 reason we should, either. We let our callers know we didn't
628 find the method, but we don't complain. */
629 return NULL_TREE;
631 /* Basic sanity check: for a template function, the template parameters
632 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
633 if (TREE_CODE (function) == TEMPLATE_DECL)
635 if (template_parms
636 && !comp_template_parms (template_parms,
637 DECL_TEMPLATE_PARMS (function)))
639 error ("template parameter lists provided don%'t match the "
640 "template parameters of %qD", function);
641 return error_mark_node;
643 template_parms = DECL_TEMPLATE_PARMS (function);
646 /* OK, is this a definition of a member template? */
647 is_template = (template_parms != NULL_TREE);
649 /* [temp.mem]
651 A destructor shall not be a member template. */
652 if (DECL_DESTRUCTOR_P (function) && is_template)
654 error ("destructor %qD declared as member template", function);
655 return error_mark_node;
658 /* We must enter the scope here, because conversion operators are
659 named by target type, and type equivalence relies on typenames
660 resolving within the scope of CTYPE. */
661 pushed_scope = push_scope (ctype);
662 ix = class_method_index_for_fn (complete_type (ctype), function);
663 if (ix >= 0)
665 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
666 tree fndecls, fndecl = 0;
667 bool is_conv_op;
668 const char *format = NULL;
670 for (fndecls = (*methods)[ix];
671 fndecls; fndecls = OVL_NEXT (fndecls))
673 tree p1, p2;
675 fndecl = OVL_CURRENT (fndecls);
676 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
677 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
679 /* We cannot simply call decls_match because this doesn't
680 work for static member functions that are pretending to
681 be methods, and because the name may have been changed by
682 asm("new_name"). */
684 /* Get rid of the this parameter on functions that become
685 static. */
686 if (DECL_STATIC_FUNCTION_P (fndecl)
687 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
688 p1 = TREE_CHAIN (p1);
690 /* A member template definition only matches a member template
691 declaration. */
692 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
693 continue;
695 /* ref-qualifier or absence of same must match. */
696 if (type_memfn_rqual (TREE_TYPE (function))
697 != type_memfn_rqual (TREE_TYPE (fndecl)))
698 continue;
700 /* While finding a match, same types and params are not enough
701 if the function is versioned. Also check version ("target")
702 attributes. */
703 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
704 TREE_TYPE (TREE_TYPE (fndecl)))
705 && compparms (p1, p2)
706 && !targetm.target_option.function_versions (function, fndecl)
707 && (!is_template
708 || comp_template_parms (template_parms,
709 DECL_TEMPLATE_PARMS (fndecl)))
710 && (DECL_TEMPLATE_SPECIALIZATION (function)
711 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
712 && (!DECL_TEMPLATE_SPECIALIZATION (function)
713 || (DECL_TI_TEMPLATE (function)
714 == DECL_TI_TEMPLATE (fndecl))))
715 break;
717 if (fndecls)
719 if (pushed_scope)
720 pop_scope (pushed_scope);
721 return OVL_CURRENT (fndecls);
724 error_at (DECL_SOURCE_LOCATION (function),
725 "prototype for %q#D does not match any in class %qT",
726 function, ctype);
727 is_conv_op = DECL_CONV_FN_P (fndecl);
729 if (is_conv_op)
730 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
731 fndecls = (*methods)[ix];
732 while (fndecls)
734 fndecl = OVL_CURRENT (fndecls);
735 fndecls = OVL_NEXT (fndecls);
737 if (!fndecls && is_conv_op)
739 if (methods->length () > (size_t) ++ix)
741 fndecls = (*methods)[ix];
742 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
744 fndecls = NULL_TREE;
745 is_conv_op = false;
748 else
749 is_conv_op = false;
751 if (format)
752 format = " %+#D";
753 else if (fndecls)
754 format = N_("candidates are: %+#D");
755 else
756 format = N_("candidate is: %+#D");
757 error (format, fndecl);
760 else if (!COMPLETE_TYPE_P (ctype))
761 cxx_incomplete_type_error (function, ctype);
762 else
763 error ("no %q#D member function declared in class %qT",
764 function, ctype);
766 if (pushed_scope)
767 pop_scope (pushed_scope);
768 return error_mark_node;
771 /* DECL is a function with vague linkage. Remember it so that at the
772 end of the translation unit we can decide whether or not to emit
773 it. */
775 void
776 note_vague_linkage_fn (tree decl)
778 DECL_DEFER_OUTPUT (decl) = 1;
779 vec_safe_push (deferred_fns, decl);
782 /* We have just processed the DECL, which is a static data member.
783 The other parameters are as for cp_finish_decl. */
785 void
786 finish_static_data_member_decl (tree decl,
787 tree init, bool init_const_expr_p,
788 tree asmspec_tree,
789 int flags)
791 DECL_CONTEXT (decl) = current_class_type;
793 /* We cannot call pushdecl here, because that would fill in the
794 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
795 the right thing, namely, to put this decl out straight away. */
797 if (! processing_template_decl)
798 vec_safe_push (pending_statics, decl);
800 if (LOCAL_CLASS_P (current_class_type)
801 /* We already complained about the template definition. */
802 && !DECL_TEMPLATE_INSTANTIATION (decl))
803 permerror (input_location, "local class %q#T shall not have static data member %q#D",
804 current_class_type, decl);
806 DECL_IN_AGGR_P (decl) = 1;
808 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
809 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
810 SET_VAR_HAD_UNKNOWN_BOUND (decl);
812 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
815 /* DECLARATOR and DECLSPECS correspond to a class member. The other
816 parameters are as for cp_finish_decl. Return the DECL for the
817 class member declared. */
819 tree
820 grokfield (const cp_declarator *declarator,
821 cp_decl_specifier_seq *declspecs,
822 tree init, bool init_const_expr_p,
823 tree asmspec_tree,
824 tree attrlist)
826 tree value;
827 const char *asmspec = 0;
828 int flags;
829 tree name;
831 if (init
832 && TREE_CODE (init) == TREE_LIST
833 && TREE_VALUE (init) == error_mark_node
834 && TREE_CHAIN (init) == NULL_TREE)
835 init = NULL_TREE;
837 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
838 if (! value || value == error_mark_node)
839 /* friend or constructor went bad. */
840 return error_mark_node;
841 if (TREE_TYPE (value) == error_mark_node)
842 return value;
844 if (TREE_CODE (value) == TYPE_DECL && init)
846 error ("typedef %qD is initialized (use decltype instead)", value);
847 init = NULL_TREE;
850 /* Pass friendly classes back. */
851 if (value == void_type_node)
852 return value;
854 /* Pass friend decls back. */
855 if ((TREE_CODE (value) == FUNCTION_DECL
856 || TREE_CODE (value) == TEMPLATE_DECL)
857 && DECL_CONTEXT (value) != current_class_type)
858 return value;
860 name = DECL_NAME (value);
862 if (name != NULL_TREE)
864 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
866 error ("explicit template argument list not allowed");
867 return error_mark_node;
870 if (IDENTIFIER_POINTER (name)[0] == '_'
871 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
872 error ("member %qD conflicts with virtual function table field name",
873 value);
876 /* Stash away type declarations. */
877 if (TREE_CODE (value) == TYPE_DECL)
879 DECL_NONLOCAL (value) = 1;
880 DECL_CONTEXT (value) = current_class_type;
882 if (attrlist)
884 int attrflags = 0;
886 /* If this is a typedef that names the class for linkage purposes
887 (7.1.3p8), apply any attributes directly to the type. */
888 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
889 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
890 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
892 cplus_decl_attributes (&value, attrlist, attrflags);
895 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
896 && TREE_TYPE (value) != error_mark_node
897 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
898 set_underlying_type (value);
900 /* It's important that push_template_decl below follows
901 set_underlying_type above so that the created template
902 carries the properly set type of VALUE. */
903 if (processing_template_decl)
904 value = push_template_decl (value);
906 record_locally_defined_typedef (value);
907 return value;
910 if (DECL_IN_AGGR_P (value))
912 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
913 return void_type_node;
916 if (asmspec_tree && asmspec_tree != error_mark_node)
917 asmspec = TREE_STRING_POINTER (asmspec_tree);
919 if (init)
921 if (TREE_CODE (value) == FUNCTION_DECL)
923 /* Initializers for functions are rejected early in the parser.
924 If we get here, it must be a pure specifier for a method. */
925 if (init == ridpointers[(int)RID_DELETE])
927 DECL_DELETED_FN (value) = 1;
928 DECL_DECLARED_INLINE_P (value) = 1;
929 DECL_INITIAL (value) = error_mark_node;
931 else if (init == ridpointers[(int)RID_DEFAULT])
933 if (defaultable_fn_check (value))
935 DECL_DEFAULTED_FN (value) = 1;
936 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
937 DECL_DECLARED_INLINE_P (value) = 1;
940 else if (TREE_CODE (init) == DEFAULT_ARG)
941 error ("invalid initializer for member function %qD", value);
942 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
944 if (integer_zerop (init))
945 DECL_PURE_VIRTUAL_P (value) = 1;
946 else if (error_operand_p (init))
947 ; /* An error has already been reported. */
948 else
949 error ("invalid initializer for member function %qD",
950 value);
952 else
954 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
955 error ("initializer specified for static member function %qD",
956 value);
959 else if (TREE_CODE (value) == FIELD_DECL)
960 /* C++11 NSDMI, keep going. */;
961 else if (!VAR_P (value))
962 gcc_unreachable ();
963 else if (!processing_template_decl)
965 if (TREE_CODE (init) == CONSTRUCTOR)
966 init = digest_init (TREE_TYPE (value), init, tf_warning_or_error);
967 init = maybe_constant_init (init);
969 if (init != error_mark_node && !TREE_CONSTANT (init))
971 /* We can allow references to things that are effectively
972 static, since references are initialized with the
973 address. */
974 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
975 || (TREE_STATIC (init) == 0
976 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
978 error ("field initializer is not constant");
979 init = error_mark_node;
985 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
987 value = push_template_decl (value);
988 if (error_operand_p (value))
989 return error_mark_node;
992 if (attrlist)
993 cplus_decl_attributes (&value, attrlist, 0);
995 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
996 && CONSTRUCTOR_IS_DIRECT_INIT (init))
997 flags = LOOKUP_NORMAL;
998 else
999 flags = LOOKUP_IMPLICIT;
1001 switch (TREE_CODE (value))
1003 case VAR_DECL:
1004 finish_static_data_member_decl (value, init, init_const_expr_p,
1005 asmspec_tree, flags);
1006 return value;
1008 case FIELD_DECL:
1009 if (asmspec)
1010 error ("%<asm%> specifiers are not permitted on non-static data members");
1011 if (DECL_INITIAL (value) == error_mark_node)
1012 init = error_mark_node;
1013 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1014 NULL_TREE, flags);
1015 DECL_IN_AGGR_P (value) = 1;
1016 return value;
1018 case FUNCTION_DECL:
1019 if (asmspec)
1020 set_user_assembler_name (value, asmspec);
1022 cp_finish_decl (value,
1023 /*init=*/NULL_TREE,
1024 /*init_const_expr_p=*/false,
1025 asmspec_tree, flags);
1027 /* Pass friends back this way. */
1028 if (DECL_FRIEND_P (value))
1029 return void_type_node;
1031 DECL_IN_AGGR_P (value) = 1;
1032 return value;
1034 default:
1035 gcc_unreachable ();
1037 return NULL_TREE;
1040 /* Like `grokfield', but for bitfields.
1041 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1043 tree
1044 grokbitfield (const cp_declarator *declarator,
1045 cp_decl_specifier_seq *declspecs, tree width,
1046 tree attrlist)
1048 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1050 if (value == error_mark_node)
1051 return NULL_TREE; /* friends went bad. */
1052 if (TREE_TYPE (value) == error_mark_node)
1053 return value;
1055 /* Pass friendly classes back. */
1056 if (VOID_TYPE_P (value))
1057 return void_type_node;
1059 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1060 && (POINTER_TYPE_P (value)
1061 || !dependent_type_p (TREE_TYPE (value))))
1063 error ("bit-field %qD with non-integral type", value);
1064 return error_mark_node;
1067 if (TREE_CODE (value) == TYPE_DECL)
1069 error ("cannot declare %qD to be a bit-field type", value);
1070 return NULL_TREE;
1073 /* Usually, finish_struct_1 catches bitfields with invalid types.
1074 But, in the case of bitfields with function type, we confuse
1075 ourselves into thinking they are member functions, so we must
1076 check here. */
1077 if (TREE_CODE (value) == FUNCTION_DECL)
1079 error ("cannot declare bit-field %qD with function type",
1080 DECL_NAME (value));
1081 return NULL_TREE;
1084 if (DECL_IN_AGGR_P (value))
1086 error ("%qD is already defined in the class %qT", value,
1087 DECL_CONTEXT (value));
1088 return void_type_node;
1091 if (TREE_STATIC (value))
1093 error ("static member %qD cannot be a bit-field", value);
1094 return NULL_TREE;
1096 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1098 if (width != error_mark_node)
1100 /* The width must be an integer type. */
1101 if (!type_dependent_expression_p (width)
1102 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1103 error ("width of bit-field %qD has non-integral type %qT", value,
1104 TREE_TYPE (width));
1105 DECL_INITIAL (value) = width;
1106 SET_DECL_C_BIT_FIELD (value);
1109 DECL_IN_AGGR_P (value) = 1;
1111 if (attrlist)
1112 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1114 return value;
1118 /* Returns true iff ATTR is an attribute which needs to be applied at
1119 instantiation time rather than template definition time. */
1121 static bool
1122 is_late_template_attribute (tree attr, tree decl)
1124 tree name = get_attribute_name (attr);
1125 tree args = TREE_VALUE (attr);
1126 const struct attribute_spec *spec = lookup_attribute_spec (name);
1127 tree arg;
1129 if (!spec)
1130 /* Unknown attribute. */
1131 return false;
1133 /* Attribute weak handling wants to write out assembly right away. */
1134 if (is_attribute_p ("weak", name))
1135 return true;
1137 /* Attribute unused is applied directly, as it appertains to
1138 decls. */
1139 if (is_attribute_p ("unused", name))
1140 return false;
1142 /* If any of the arguments are dependent expressions, we can't evaluate
1143 the attribute until instantiation time. */
1144 for (arg = args; arg; arg = TREE_CHAIN (arg))
1146 tree t = TREE_VALUE (arg);
1148 /* If the first attribute argument is an identifier, only consider
1149 second and following arguments. Attributes like mode, format,
1150 cleanup and several target specific attributes aren't late
1151 just because they have an IDENTIFIER_NODE as first argument. */
1152 if (arg == args && identifier_p (t))
1153 continue;
1155 if (value_dependent_expression_p (t)
1156 || type_dependent_expression_p (t))
1157 return true;
1160 if (TREE_CODE (decl) == TYPE_DECL
1161 || TYPE_P (decl)
1162 || spec->type_required)
1164 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1166 /* We can't apply any attributes to a completely unknown type until
1167 instantiation time. */
1168 enum tree_code code = TREE_CODE (type);
1169 if (code == TEMPLATE_TYPE_PARM
1170 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1171 || code == TYPENAME_TYPE)
1172 return true;
1173 /* Also defer most attributes on dependent types. This is not
1174 necessary in all cases, but is the better default. */
1175 else if (dependent_type_p (type)
1176 /* But attribute visibility specifically works on
1177 templates. */
1178 && !is_attribute_p ("visibility", name))
1179 return true;
1180 else
1181 return false;
1183 else
1184 return false;
1187 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1188 applied at instantiation time and return them. If IS_DEPENDENT is true,
1189 the declaration itself is dependent, so all attributes should be applied
1190 at instantiation time. */
1192 static tree
1193 splice_template_attributes (tree *attr_p, tree decl)
1195 tree *p = attr_p;
1196 tree late_attrs = NULL_TREE;
1197 tree *q = &late_attrs;
1199 if (!p)
1200 return NULL_TREE;
1202 for (; *p; )
1204 if (is_late_template_attribute (*p, decl))
1206 ATTR_IS_DEPENDENT (*p) = 1;
1207 *q = *p;
1208 *p = TREE_CHAIN (*p);
1209 q = &TREE_CHAIN (*q);
1210 *q = NULL_TREE;
1212 else
1213 p = &TREE_CHAIN (*p);
1216 return late_attrs;
1219 /* Remove any late attributes from the list in ATTR_P and attach them to
1220 DECL_P. */
1222 static void
1223 save_template_attributes (tree *attr_p, tree *decl_p)
1225 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1226 tree *q;
1227 tree old_attrs = NULL_TREE;
1229 if (!late_attrs)
1230 return;
1232 if (DECL_P (*decl_p))
1233 q = &DECL_ATTRIBUTES (*decl_p);
1234 else
1235 q = &TYPE_ATTRIBUTES (*decl_p);
1237 old_attrs = *q;
1239 /* Merge the late attributes at the beginning with the attribute
1240 list. */
1241 late_attrs = merge_attributes (late_attrs, *q);
1242 *q = late_attrs;
1244 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1246 /* We've added new attributes directly to the main variant, so
1247 now we need to update all of the other variants to include
1248 these new attributes. */
1249 tree variant;
1250 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1251 variant = TYPE_NEXT_VARIANT (variant))
1253 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1254 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1259 /* Like reconstruct_complex_type, but handle also template trees. */
1261 tree
1262 cp_reconstruct_complex_type (tree type, tree bottom)
1264 tree inner, outer;
1266 if (TYPE_PTR_P (type))
1268 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1269 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1270 TYPE_REF_CAN_ALIAS_ALL (type));
1272 else if (TREE_CODE (type) == REFERENCE_TYPE)
1274 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1275 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1276 TYPE_REF_CAN_ALIAS_ALL (type));
1278 else if (TREE_CODE (type) == ARRAY_TYPE)
1280 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1281 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1282 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1283 element type qualification will be handled by the recursive
1284 cp_reconstruct_complex_type call and cp_build_qualified_type
1285 for ARRAY_TYPEs changes the element type. */
1286 return outer;
1288 else if (TREE_CODE (type) == FUNCTION_TYPE)
1290 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1291 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1292 outer = apply_memfn_quals (outer,
1293 type_memfn_quals (type),
1294 type_memfn_rqual (type));
1296 else if (TREE_CODE (type) == METHOD_TYPE)
1298 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1299 /* The build_method_type_directly() routine prepends 'this' to argument list,
1300 so we must compensate by getting rid of it. */
1301 outer
1302 = build_method_type_directly
1303 (class_of_this_parm (type), inner,
1304 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1306 else if (TREE_CODE (type) == OFFSET_TYPE)
1308 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1309 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1311 else
1312 return bottom;
1314 if (TYPE_ATTRIBUTES (type))
1315 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1316 return cp_build_qualified_type (outer, cp_type_quals (type));
1319 /* Replaces any constexpr expression that may be into the attributes
1320 arguments with their reduced value. */
1322 static void
1323 cp_check_const_attributes (tree attributes)
1325 tree attr;
1326 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1328 tree arg;
1329 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1331 tree expr = TREE_VALUE (arg);
1332 if (EXPR_P (expr))
1333 TREE_VALUE (arg) = maybe_constant_value (expr);
1338 /* Like decl_attributes, but handle C++ complexity. */
1340 void
1341 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1343 if (*decl == NULL_TREE || *decl == void_type_node
1344 || *decl == error_mark_node)
1345 return;
1347 if (processing_template_decl)
1349 if (check_for_bare_parameter_packs (attributes))
1350 return;
1352 save_template_attributes (&attributes, decl);
1355 cp_check_const_attributes (attributes);
1357 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1358 decl = &DECL_TEMPLATE_RESULT (*decl);
1360 decl_attributes (decl, attributes, flags);
1362 if (TREE_CODE (*decl) == TYPE_DECL)
1363 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1366 /* Walks through the namespace- or function-scope anonymous union
1367 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1368 Returns one of the fields for use in the mangled name. */
1370 static tree
1371 build_anon_union_vars (tree type, tree object)
1373 tree main_decl = NULL_TREE;
1374 tree field;
1376 /* Rather than write the code to handle the non-union case,
1377 just give an error. */
1378 if (TREE_CODE (type) != UNION_TYPE)
1380 error ("anonymous struct not inside named type");
1381 return error_mark_node;
1384 for (field = TYPE_FIELDS (type);
1385 field != NULL_TREE;
1386 field = DECL_CHAIN (field))
1388 tree decl;
1389 tree ref;
1391 if (DECL_ARTIFICIAL (field))
1392 continue;
1393 if (TREE_CODE (field) != FIELD_DECL)
1395 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1396 "have non-static data members", field);
1397 continue;
1400 if (TREE_PRIVATE (field))
1401 permerror (input_location, "private member %q+#D in anonymous union", field);
1402 else if (TREE_PROTECTED (field))
1403 permerror (input_location, "protected member %q+#D in anonymous union", field);
1405 if (processing_template_decl)
1406 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1407 DECL_NAME (field), NULL_TREE);
1408 else
1409 ref = build_class_member_access_expr (object, field, NULL_TREE,
1410 false, tf_warning_or_error);
1412 if (DECL_NAME (field))
1414 tree base;
1416 decl = build_decl (input_location,
1417 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1418 DECL_ANON_UNION_VAR_P (decl) = 1;
1419 DECL_ARTIFICIAL (decl) = 1;
1421 base = get_base_address (object);
1422 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1423 TREE_STATIC (decl) = TREE_STATIC (base);
1424 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1426 SET_DECL_VALUE_EXPR (decl, ref);
1427 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1429 decl = pushdecl (decl);
1431 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1432 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1433 else
1434 decl = 0;
1436 if (main_decl == NULL_TREE)
1437 main_decl = decl;
1440 return main_decl;
1443 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1444 anonymous union, then all members must be laid out together. PUBLIC_P
1445 is nonzero if this union is not declared static. */
1447 void
1448 finish_anon_union (tree anon_union_decl)
1450 tree type;
1451 tree main_decl;
1452 bool public_p;
1454 if (anon_union_decl == error_mark_node)
1455 return;
1457 type = TREE_TYPE (anon_union_decl);
1458 public_p = TREE_PUBLIC (anon_union_decl);
1460 /* The VAR_DECL's context is the same as the TYPE's context. */
1461 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1463 if (TYPE_FIELDS (type) == NULL_TREE)
1464 return;
1466 if (public_p)
1468 error ("namespace-scope anonymous aggregates must be static");
1469 return;
1472 main_decl = build_anon_union_vars (type, anon_union_decl);
1473 if (main_decl == error_mark_node)
1474 return;
1475 if (main_decl == NULL_TREE)
1477 warning (0, "anonymous union with no members");
1478 return;
1481 if (!processing_template_decl)
1483 /* Use main_decl to set the mangled name. */
1484 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1485 maybe_commonize_var (anon_union_decl);
1486 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1487 mangle_decl (anon_union_decl);
1488 DECL_NAME (anon_union_decl) = NULL_TREE;
1491 pushdecl (anon_union_decl);
1492 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1495 /* Auxiliary functions to make type signatures for
1496 `operator new' and `operator delete' correspond to
1497 what compiler will be expecting. */
1499 tree
1500 coerce_new_type (tree type)
1502 int e = 0;
1503 tree args = TYPE_ARG_TYPES (type);
1505 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1507 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1509 e = 1;
1510 error ("%<operator new%> must return type %qT", ptr_type_node);
1513 if (args && args != void_list_node)
1515 if (TREE_PURPOSE (args))
1517 /* [basic.stc.dynamic.allocation]
1519 The first parameter shall not have an associated default
1520 argument. */
1521 error ("the first parameter of %<operator new%> cannot "
1522 "have a default argument");
1523 /* Throw away the default argument. */
1524 TREE_PURPOSE (args) = NULL_TREE;
1527 if (!same_type_p (TREE_VALUE (args), size_type_node))
1529 e = 2;
1530 args = TREE_CHAIN (args);
1533 else
1534 e = 2;
1536 if (e == 2)
1537 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1538 "as first parameter", size_type_node);
1540 switch (e)
1542 case 2:
1543 args = tree_cons (NULL_TREE, size_type_node, args);
1544 /* Fall through. */
1545 case 1:
1546 type = build_exception_variant
1547 (build_function_type (ptr_type_node, args),
1548 TYPE_RAISES_EXCEPTIONS (type));
1549 /* Fall through. */
1550 default:;
1552 return type;
1555 tree
1556 coerce_delete_type (tree type)
1558 int e = 0;
1559 tree args = TYPE_ARG_TYPES (type);
1561 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1563 if (!same_type_p (TREE_TYPE (type), void_type_node))
1565 e = 1;
1566 error ("%<operator delete%> must return type %qT", void_type_node);
1569 if (!args || args == void_list_node
1570 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1572 e = 2;
1573 if (args && args != void_list_node)
1574 args = TREE_CHAIN (args);
1575 error ("%<operator delete%> takes type %qT as first parameter",
1576 ptr_type_node);
1578 switch (e)
1580 case 2:
1581 args = tree_cons (NULL_TREE, ptr_type_node, args);
1582 /* Fall through. */
1583 case 1:
1584 type = build_exception_variant
1585 (build_function_type (void_type_node, args),
1586 TYPE_RAISES_EXCEPTIONS (type));
1587 /* Fall through. */
1588 default:;
1591 return type;
1594 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1595 and mark them as needed. */
1597 static void
1598 mark_vtable_entries (tree decl)
1600 tree fnaddr;
1601 unsigned HOST_WIDE_INT idx;
1603 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1604 idx, fnaddr)
1606 tree fn;
1608 STRIP_NOPS (fnaddr);
1610 if (TREE_CODE (fnaddr) != ADDR_EXPR
1611 && TREE_CODE (fnaddr) != FDESC_EXPR)
1612 /* This entry is an offset: a virtual base class offset, a
1613 virtual call offset, an RTTI offset, etc. */
1614 continue;
1616 fn = TREE_OPERAND (fnaddr, 0);
1617 TREE_ADDRESSABLE (fn) = 1;
1618 /* When we don't have vcall offsets, we output thunks whenever
1619 we output the vtables that contain them. With vcall offsets,
1620 we know all the thunks we'll need when we emit a virtual
1621 function, so we emit the thunks there instead. */
1622 if (DECL_THUNK_P (fn))
1623 use_thunk (fn, /*emit_p=*/0);
1624 mark_used (fn);
1628 /* Set DECL up to have the closest approximation of "initialized common"
1629 linkage available. */
1631 void
1632 comdat_linkage (tree decl)
1634 if (flag_weak)
1635 make_decl_one_only (decl, cxx_comdat_group (decl));
1636 else if (TREE_CODE (decl) == FUNCTION_DECL
1637 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1638 /* We can just emit function and compiler-generated variables
1639 statically; having multiple copies is (for the most part) only
1640 a waste of space.
1642 There are two correctness issues, however: the address of a
1643 template instantiation with external linkage should be the
1644 same, independent of what translation unit asks for the
1645 address, and this will not hold when we emit multiple copies of
1646 the function. However, there's little else we can do.
1648 Also, by default, the typeinfo implementation assumes that
1649 there will be only one copy of the string used as the name for
1650 each type. Therefore, if weak symbols are unavailable, the
1651 run-time library should perform a more conservative check; it
1652 should perform a string comparison, rather than an address
1653 comparison. */
1654 TREE_PUBLIC (decl) = 0;
1655 else
1657 /* Static data member template instantiations, however, cannot
1658 have multiple copies. */
1659 if (DECL_INITIAL (decl) == 0
1660 || DECL_INITIAL (decl) == error_mark_node)
1661 DECL_COMMON (decl) = 1;
1662 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1664 DECL_COMMON (decl) = 1;
1665 DECL_INITIAL (decl) = error_mark_node;
1667 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1669 /* We can't do anything useful; leave vars for explicit
1670 instantiation. */
1671 DECL_EXTERNAL (decl) = 1;
1672 DECL_NOT_REALLY_EXTERN (decl) = 0;
1676 DECL_COMDAT (decl) = 1;
1679 /* For win32 we also want to put explicit instantiations in
1680 linkonce sections, so that they will be merged with implicit
1681 instantiations; otherwise we get duplicate symbol errors.
1682 For Darwin we do not want explicit instantiations to be
1683 linkonce. */
1685 void
1686 maybe_make_one_only (tree decl)
1688 /* We used to say that this was not necessary on targets that support weak
1689 symbols, because the implicit instantiations will defer to the explicit
1690 one. However, that's not actually the case in SVR4; a strong definition
1691 after a weak one is an error. Also, not making explicit
1692 instantiations one_only means that we can end up with two copies of
1693 some template instantiations. */
1694 if (! flag_weak)
1695 return;
1697 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1698 we can get away with not emitting them if they aren't used. We need
1699 to for variables so that cp_finish_decl will update their linkage,
1700 because their DECL_INITIAL may not have been set properly yet. */
1702 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1703 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1704 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1706 make_decl_one_only (decl, cxx_comdat_group (decl));
1708 if (VAR_P (decl))
1710 struct varpool_node *node = varpool_node_for_decl (decl);
1711 DECL_COMDAT (decl) = 1;
1712 /* Mark it needed so we don't forget to emit it. */
1713 node->symbol.forced_by_abi = true;
1714 TREE_USED (decl) = 1;
1719 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1720 This predicate will give the right answer during parsing of the
1721 function, which other tests may not. */
1723 bool
1724 vague_linkage_p (tree decl)
1726 /* Unfortunately, import_export_decl has not always been called
1727 before the function is processed, so we cannot simply check
1728 DECL_COMDAT. */
1729 return (DECL_COMDAT (decl)
1730 || (((TREE_CODE (decl) == FUNCTION_DECL
1731 && DECL_DECLARED_INLINE_P (decl))
1732 || (DECL_LANG_SPECIFIC (decl)
1733 && DECL_TEMPLATE_INSTANTIATION (decl)))
1734 && TREE_PUBLIC (decl)));
1737 /* Determine whether or not we want to specifically import or export CTYPE,
1738 using various heuristics. */
1740 static void
1741 import_export_class (tree ctype)
1743 /* -1 for imported, 1 for exported. */
1744 int import_export = 0;
1746 /* It only makes sense to call this function at EOF. The reason is
1747 that this function looks at whether or not the first non-inline
1748 non-abstract virtual member function has been defined in this
1749 translation unit. But, we can't possibly know that until we've
1750 seen the entire translation unit. */
1751 gcc_assert (at_eof);
1753 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1754 return;
1756 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1757 we will have CLASSTYPE_INTERFACE_ONLY set but not
1758 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1759 heuristic because someone will supply a #pragma implementation
1760 elsewhere, and deducing it here would produce a conflict. */
1761 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1762 return;
1764 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1765 import_export = -1;
1766 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1767 import_export = 1;
1768 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1769 && !flag_implicit_templates)
1770 /* For a template class, without -fimplicit-templates, check the
1771 repository. If the virtual table is assigned to this
1772 translation unit, then export the class; otherwise, import
1773 it. */
1774 import_export = repo_export_class_p (ctype) ? 1 : -1;
1775 else if (TYPE_POLYMORPHIC_P (ctype))
1777 /* The ABI specifies that the virtual table and associated
1778 information are emitted with the key method, if any. */
1779 tree method = CLASSTYPE_KEY_METHOD (ctype);
1780 /* If weak symbol support is not available, then we must be
1781 careful not to emit the vtable when the key function is
1782 inline. An inline function can be defined in multiple
1783 translation units. If we were to emit the vtable in each
1784 translation unit containing a definition, we would get
1785 multiple definition errors at link-time. */
1786 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1787 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1790 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1791 a definition anywhere else. */
1792 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1793 import_export = 0;
1795 /* Allow back ends the chance to overrule the decision. */
1796 if (targetm.cxx.import_export_class)
1797 import_export = targetm.cxx.import_export_class (ctype, import_export);
1799 if (import_export)
1801 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1802 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1806 /* Return true if VAR has already been provided to the back end; in that
1807 case VAR should not be modified further by the front end. */
1808 static bool
1809 var_finalized_p (tree var)
1811 return varpool_node_for_decl (var)->symbol.definition;
1814 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1815 must be emitted in this translation unit. Mark it as such. */
1817 void
1818 mark_needed (tree decl)
1820 TREE_USED (decl) = 1;
1821 if (TREE_CODE (decl) == FUNCTION_DECL)
1823 /* Extern inline functions don't become needed when referenced.
1824 If we know a method will be emitted in other TU and no new
1825 functions can be marked reachable, just use the external
1826 definition. */
1827 struct cgraph_node *node = cgraph_get_create_node (decl);
1828 node->symbol.forced_by_abi = true;
1830 else if (TREE_CODE (decl) == VAR_DECL)
1832 struct varpool_node *node = varpool_node_for_decl (decl);
1833 /* C++ frontend use mark_decl_references to force COMDAT variables
1834 to be output that might appear dead otherwise. */
1835 node->symbol.forced_by_abi = true;
1839 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1840 returns true if a definition of this entity should be provided in
1841 this object file. Callers use this function to determine whether
1842 or not to let the back end know that a definition of DECL is
1843 available in this translation unit. */
1845 bool
1846 decl_needed_p (tree decl)
1848 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1849 /* This function should only be called at the end of the translation
1850 unit. We cannot be sure of whether or not something will be
1851 COMDAT until that point. */
1852 gcc_assert (at_eof);
1854 /* All entities with external linkage that are not COMDAT should be
1855 emitted; they may be referred to from other object files. */
1856 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1857 return true;
1858 /* If this entity was used, let the back end see it; it will decide
1859 whether or not to emit it into the object file. */
1860 if (TREE_USED (decl))
1861 return true;
1862 /* Functions marked "dllexport" must be emitted so that they are
1863 visible to other DLLs. */
1864 if (flag_keep_inline_dllexport
1865 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1866 return true;
1867 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1868 reference to DECL might cause it to be emitted later. */
1869 return false;
1872 /* If necessary, write out the vtables for the dynamic class CTYPE.
1873 Returns true if any vtables were emitted. */
1875 static bool
1876 maybe_emit_vtables (tree ctype)
1878 tree vtbl;
1879 tree primary_vtbl;
1880 int needed = 0;
1881 struct varpool_node *current = NULL, *last = NULL;
1883 /* If the vtables for this class have already been emitted there is
1884 nothing more to do. */
1885 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1886 if (var_finalized_p (primary_vtbl))
1887 return false;
1888 /* Ignore dummy vtables made by get_vtable_decl. */
1889 if (TREE_TYPE (primary_vtbl) == void_type_node)
1890 return false;
1892 /* On some targets, we cannot determine the key method until the end
1893 of the translation unit -- which is when this function is
1894 called. */
1895 if (!targetm.cxx.key_method_may_be_inline ())
1896 determine_key_method (ctype);
1898 /* See if any of the vtables are needed. */
1899 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1901 import_export_decl (vtbl);
1902 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1903 needed = 1;
1905 if (!needed)
1907 /* If the references to this class' vtables are optimized away,
1908 still emit the appropriate debugging information. See
1909 dfs_debug_mark. */
1910 if (DECL_COMDAT (primary_vtbl)
1911 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1912 note_debug_info_needed (ctype);
1913 return false;
1916 /* The ABI requires that we emit all of the vtables if we emit any
1917 of them. */
1918 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1920 /* Mark entities references from the virtual table as used. */
1921 mark_vtable_entries (vtbl);
1923 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1925 vec<tree, va_gc> *cleanups = NULL;
1926 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
1927 LOOKUP_NORMAL);
1929 /* It had better be all done at compile-time. */
1930 gcc_assert (!expr && !cleanups);
1933 /* Write it out. */
1934 DECL_EXTERNAL (vtbl) = 0;
1935 rest_of_decl_compilation (vtbl, 1, 1);
1937 /* Because we're only doing syntax-checking, we'll never end up
1938 actually marking the variable as written. */
1939 if (flag_syntax_only)
1940 TREE_ASM_WRITTEN (vtbl) = 1;
1941 else if (DECL_ONE_ONLY (vtbl))
1943 current = varpool_node_for_decl (vtbl);
1944 if (last)
1945 symtab_add_to_same_comdat_group ((symtab_node) current, (symtab_node) last);
1946 last = current;
1950 /* Since we're writing out the vtable here, also write the debug
1951 info. */
1952 note_debug_info_needed (ctype);
1954 return true;
1957 /* A special return value from type_visibility meaning internal
1958 linkage. */
1960 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
1962 /* walk_tree helper function for type_visibility. */
1964 static tree
1965 min_vis_r (tree *tp, int *walk_subtrees, void *data)
1967 int *vis_p = (int *)data;
1968 if (! TYPE_P (*tp))
1970 *walk_subtrees = 0;
1972 else if (OVERLOAD_TYPE_P (*tp)
1973 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
1975 *vis_p = VISIBILITY_ANON;
1976 return *tp;
1978 else if (CLASS_TYPE_P (*tp)
1979 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
1980 *vis_p = CLASSTYPE_VISIBILITY (*tp);
1981 return NULL;
1984 /* Returns the visibility of TYPE, which is the minimum visibility of its
1985 component types. */
1987 static int
1988 type_visibility (tree type)
1990 int vis = VISIBILITY_DEFAULT;
1991 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
1992 return vis;
1995 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
1996 specified (or if VISIBILITY is static). If TMPL is true, this
1997 constraint is for a template argument, and takes precedence
1998 over explicitly-specified visibility on the template. */
2000 static void
2001 constrain_visibility (tree decl, int visibility, bool tmpl)
2003 if (visibility == VISIBILITY_ANON)
2005 /* extern "C" declarations aren't affected by the anonymous
2006 namespace. */
2007 if (!DECL_EXTERN_C_P (decl))
2009 TREE_PUBLIC (decl) = 0;
2010 DECL_WEAK (decl) = 0;
2011 DECL_COMMON (decl) = 0;
2012 DECL_COMDAT_GROUP (decl) = NULL_TREE;
2013 DECL_INTERFACE_KNOWN (decl) = 1;
2014 if (DECL_LANG_SPECIFIC (decl))
2015 DECL_NOT_REALLY_EXTERN (decl) = 1;
2018 else if (visibility > DECL_VISIBILITY (decl)
2019 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2021 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2022 /* This visibility was not specified. */
2023 DECL_VISIBILITY_SPECIFIED (decl) = false;
2027 /* Constrain the visibility of DECL based on the visibility of its template
2028 arguments. */
2030 static void
2031 constrain_visibility_for_template (tree decl, tree targs)
2033 /* If this is a template instantiation, check the innermost
2034 template args for visibility constraints. The outer template
2035 args are covered by the class check. */
2036 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2037 int i;
2038 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2040 int vis = 0;
2042 tree arg = TREE_VEC_ELT (args, i-1);
2043 if (TYPE_P (arg))
2044 vis = type_visibility (arg);
2045 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2047 STRIP_NOPS (arg);
2048 if (TREE_CODE (arg) == ADDR_EXPR)
2049 arg = TREE_OPERAND (arg, 0);
2050 if (VAR_OR_FUNCTION_DECL_P (arg))
2052 if (! TREE_PUBLIC (arg))
2053 vis = VISIBILITY_ANON;
2054 else
2055 vis = DECL_VISIBILITY (arg);
2058 if (vis)
2059 constrain_visibility (decl, vis, true);
2063 /* Like c_determine_visibility, but with additional C++-specific
2064 behavior.
2066 Function-scope entities can rely on the function's visibility because
2067 it is set in start_preparsed_function.
2069 Class-scope entities cannot rely on the class's visibility until the end
2070 of the enclosing class definition.
2072 Note that because namespaces have multiple independent definitions,
2073 namespace visibility is handled elsewhere using the #pragma visibility
2074 machinery rather than by decorating the namespace declaration.
2076 The goal is for constraints from the type to give a diagnostic, and
2077 other constraints to be applied silently. */
2079 void
2080 determine_visibility (tree decl)
2082 tree class_type = NULL_TREE;
2083 bool use_template;
2084 bool orig_visibility_specified;
2085 enum symbol_visibility orig_visibility;
2087 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2089 /* Only relevant for names with external linkage. */
2090 if (!TREE_PUBLIC (decl))
2091 return;
2093 /* Cloned constructors and destructors get the same visibility as
2094 the underlying function. That should be set up in
2095 maybe_clone_body. */
2096 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2098 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2099 orig_visibility = DECL_VISIBILITY (decl);
2101 if (TREE_CODE (decl) == TYPE_DECL)
2103 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2104 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2105 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2106 use_template = 1;
2107 else
2108 use_template = 0;
2110 else if (DECL_LANG_SPECIFIC (decl))
2111 use_template = DECL_USE_TEMPLATE (decl);
2112 else
2113 use_template = 0;
2115 /* If DECL is a member of a class, visibility specifiers on the
2116 class can influence the visibility of the DECL. */
2117 if (DECL_CLASS_SCOPE_P (decl))
2118 class_type = DECL_CONTEXT (decl);
2119 else
2121 /* Not a class member. */
2123 /* Virtual tables have DECL_CONTEXT set to their associated class,
2124 so they are automatically handled above. */
2125 gcc_assert (!VAR_P (decl)
2126 || !DECL_VTABLE_OR_VTT_P (decl));
2128 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2130 /* Local statics and classes get the visibility of their
2131 containing function by default, except that
2132 -fvisibility-inlines-hidden doesn't affect them. */
2133 tree fn = DECL_CONTEXT (decl);
2134 if (DECL_VISIBILITY_SPECIFIED (fn))
2136 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2137 DECL_VISIBILITY_SPECIFIED (decl) =
2138 DECL_VISIBILITY_SPECIFIED (fn);
2140 else
2142 if (DECL_CLASS_SCOPE_P (fn))
2143 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2144 else if (determine_hidden_inline (fn))
2146 DECL_VISIBILITY (decl) = default_visibility;
2147 DECL_VISIBILITY_SPECIFIED (decl) =
2148 visibility_options.inpragma;
2150 else
2152 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2153 DECL_VISIBILITY_SPECIFIED (decl) =
2154 DECL_VISIBILITY_SPECIFIED (fn);
2158 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2159 but have no TEMPLATE_INFO, so don't try to check it. */
2160 use_template = 0;
2162 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2163 && flag_visibility_ms_compat)
2165 /* Under -fvisibility-ms-compat, types are visible by default,
2166 even though their contents aren't. */
2167 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2168 int underlying_vis = type_visibility (underlying_type);
2169 if (underlying_vis == VISIBILITY_ANON
2170 || (CLASS_TYPE_P (underlying_type)
2171 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2172 constrain_visibility (decl, underlying_vis, false);
2173 else
2174 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2176 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2178 /* tinfo visibility is based on the type it's for. */
2179 constrain_visibility
2180 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2182 /* Give the target a chance to override the visibility associated
2183 with DECL. */
2184 if (TREE_PUBLIC (decl)
2185 && !DECL_REALLY_EXTERN (decl)
2186 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2187 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2188 targetm.cxx.determine_class_data_visibility (decl);
2190 else if (use_template)
2191 /* Template instantiations and specializations get visibility based
2192 on their template unless they override it with an attribute. */;
2193 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2195 if (determine_hidden_inline (decl))
2196 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2197 else
2199 /* Set default visibility to whatever the user supplied with
2200 #pragma GCC visibility or a namespace visibility attribute. */
2201 DECL_VISIBILITY (decl) = default_visibility;
2202 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2207 if (use_template)
2209 /* If the specialization doesn't specify visibility, use the
2210 visibility from the template. */
2211 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2212 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2213 : DECL_TEMPLATE_INFO (decl));
2214 tree args = TI_ARGS (tinfo);
2215 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2216 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2217 : DECL_ATTRIBUTES (decl));
2219 if (args != error_mark_node)
2221 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2223 if (!DECL_VISIBILITY_SPECIFIED (decl))
2225 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2226 && determine_hidden_inline (decl))
2227 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2228 else
2230 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2231 DECL_VISIBILITY_SPECIFIED (decl)
2232 = DECL_VISIBILITY_SPECIFIED (pattern);
2236 if (args
2237 /* Template argument visibility outweighs #pragma or namespace
2238 visibility, but not an explicit attribute. */
2239 && !lookup_attribute ("visibility", attribs))
2241 int depth = TMPL_ARGS_DEPTH (args);
2242 if (DECL_VISIBILITY_SPECIFIED (decl))
2244 /* A class template member with explicit visibility
2245 overrides the class visibility, so we need to apply
2246 all the levels of template args directly. */
2247 int i;
2248 for (i = 1; i <= depth; ++i)
2250 tree lev = TMPL_ARGS_LEVEL (args, i);
2251 constrain_visibility_for_template (decl, lev);
2254 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2255 /* Limit visibility based on its template arguments. */
2256 constrain_visibility_for_template (decl, args);
2261 if (class_type)
2262 determine_visibility_from_class (decl, class_type);
2264 if (decl_anon_ns_mem_p (decl))
2265 /* Names in an anonymous namespace get internal linkage.
2266 This might change once we implement export. */
2267 constrain_visibility (decl, VISIBILITY_ANON, false);
2268 else if (TREE_CODE (decl) != TYPE_DECL)
2270 /* Propagate anonymity from type to decl. */
2271 int tvis = type_visibility (TREE_TYPE (decl));
2272 if (tvis == VISIBILITY_ANON
2273 || ! DECL_VISIBILITY_SPECIFIED (decl))
2274 constrain_visibility (decl, tvis, false);
2276 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2277 /* DR 757: A type without linkage shall not be used as the type of a
2278 variable or function with linkage, unless
2279 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2280 o the variable or function is not used (3.2 [basic.def.odr]) or is
2281 defined in the same translation unit.
2283 Since non-extern "C" decls need to be defined in the same
2284 translation unit, we can make the type internal. */
2285 constrain_visibility (decl, VISIBILITY_ANON, false);
2287 /* If visibility changed and DECL already has DECL_RTL, ensure
2288 symbol flags are updated. */
2289 if ((DECL_VISIBILITY (decl) != orig_visibility
2290 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2291 && ((VAR_P (decl) && TREE_STATIC (decl))
2292 || TREE_CODE (decl) == FUNCTION_DECL)
2293 && DECL_RTL_SET_P (decl))
2294 make_decl_rtl (decl);
2297 /* By default, static data members and function members receive
2298 the visibility of their containing class. */
2300 static void
2301 determine_visibility_from_class (tree decl, tree class_type)
2303 if (DECL_VISIBILITY_SPECIFIED (decl))
2304 return;
2306 if (determine_hidden_inline (decl))
2307 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2308 else
2310 /* Default to the class visibility. */
2311 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2312 DECL_VISIBILITY_SPECIFIED (decl)
2313 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2316 /* Give the target a chance to override the visibility associated
2317 with DECL. */
2318 if (VAR_P (decl)
2319 && (DECL_TINFO_P (decl)
2320 || (DECL_VTABLE_OR_VTT_P (decl)
2321 /* Construction virtual tables are not exported because
2322 they cannot be referred to from other object files;
2323 their name is not standardized by the ABI. */
2324 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2325 && TREE_PUBLIC (decl)
2326 && !DECL_REALLY_EXTERN (decl)
2327 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2328 targetm.cxx.determine_class_data_visibility (decl);
2331 /* Returns true iff DECL is an inline that should get hidden visibility
2332 because of -fvisibility-inlines-hidden. */
2334 static bool
2335 determine_hidden_inline (tree decl)
2337 return (visibility_options.inlines_hidden
2338 /* Don't do this for inline templates; specializations might not be
2339 inline, and we don't want them to inherit the hidden
2340 visibility. We'll set it here for all inline instantiations. */
2341 && !processing_template_decl
2342 && TREE_CODE (decl) == FUNCTION_DECL
2343 && DECL_DECLARED_INLINE_P (decl)
2344 && (! DECL_LANG_SPECIFIC (decl)
2345 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2348 /* Constrain the visibility of a class TYPE based on the visibility of its
2349 field types. Warn if any fields require lesser visibility. */
2351 void
2352 constrain_class_visibility (tree type)
2354 tree binfo;
2355 tree t;
2356 int i;
2358 int vis = type_visibility (type);
2360 if (vis == VISIBILITY_ANON
2361 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2362 return;
2364 /* Don't warn about visibility if the class has explicit visibility. */
2365 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2366 vis = VISIBILITY_INTERNAL;
2368 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2369 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2371 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2372 int subvis = type_visibility (ftype);
2374 if (subvis == VISIBILITY_ANON)
2376 if (!in_main_input_context ())
2377 warning (0, "\
2378 %qT has a field %qD whose type uses the anonymous namespace",
2379 type, t);
2381 else if (MAYBE_CLASS_TYPE_P (ftype)
2382 && vis < VISIBILITY_HIDDEN
2383 && subvis >= VISIBILITY_HIDDEN)
2384 warning (OPT_Wattributes, "\
2385 %qT declared with greater visibility than the type of its field %qD",
2386 type, t);
2389 binfo = TYPE_BINFO (type);
2390 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2392 int subvis = type_visibility (TREE_TYPE (t));
2394 if (subvis == VISIBILITY_ANON)
2396 if (!in_main_input_context())
2397 warning (0, "\
2398 %qT has a base %qT whose type uses the anonymous namespace",
2399 type, TREE_TYPE (t));
2401 else if (vis < VISIBILITY_HIDDEN
2402 && subvis >= VISIBILITY_HIDDEN)
2403 warning (OPT_Wattributes, "\
2404 %qT declared with greater visibility than its base %qT",
2405 type, TREE_TYPE (t));
2409 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2410 for DECL has not already been determined, do so now by setting
2411 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2412 function is called entities with vague linkage whose definitions
2413 are available must have TREE_PUBLIC set.
2415 If this function decides to place DECL in COMDAT, it will set
2416 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2417 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2418 callers defer that decision until it is clear that DECL is actually
2419 required. */
2421 void
2422 import_export_decl (tree decl)
2424 int emit_p;
2425 bool comdat_p;
2426 bool import_p;
2427 tree class_type = NULL_TREE;
2429 if (DECL_INTERFACE_KNOWN (decl))
2430 return;
2432 /* We cannot determine what linkage to give to an entity with vague
2433 linkage until the end of the file. For example, a virtual table
2434 for a class will be defined if and only if the key method is
2435 defined in this translation unit. As a further example, consider
2436 that when compiling a translation unit that uses PCH file with
2437 "-frepo" it would be incorrect to make decisions about what
2438 entities to emit when building the PCH; those decisions must be
2439 delayed until the repository information has been processed. */
2440 gcc_assert (at_eof);
2441 /* Object file linkage for explicit instantiations is handled in
2442 mark_decl_instantiated. For static variables in functions with
2443 vague linkage, maybe_commonize_var is used.
2445 Therefore, the only declarations that should be provided to this
2446 function are those with external linkage that are:
2448 * implicit instantiations of function templates
2450 * inline function
2452 * implicit instantiations of static data members of class
2453 templates
2455 * virtual tables
2457 * typeinfo objects
2459 Furthermore, all entities that reach this point must have a
2460 definition available in this translation unit.
2462 The following assertions check these conditions. */
2463 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2464 /* Any code that creates entities with TREE_PUBLIC cleared should
2465 also set DECL_INTERFACE_KNOWN. */
2466 gcc_assert (TREE_PUBLIC (decl));
2467 if (TREE_CODE (decl) == FUNCTION_DECL)
2468 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2469 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2470 || DECL_DECLARED_INLINE_P (decl));
2471 else
2472 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2473 || DECL_VTABLE_OR_VTT_P (decl)
2474 || DECL_TINFO_P (decl));
2475 /* Check that a definition of DECL is available in this translation
2476 unit. */
2477 gcc_assert (!DECL_REALLY_EXTERN (decl));
2479 /* Assume that DECL will not have COMDAT linkage. */
2480 comdat_p = false;
2481 /* Assume that DECL will not be imported into this translation
2482 unit. */
2483 import_p = false;
2485 /* See if the repository tells us whether or not to emit DECL in
2486 this translation unit. */
2487 emit_p = repo_emit_p (decl);
2488 if (emit_p == 0)
2489 import_p = true;
2490 else if (emit_p == 1)
2492 /* The repository indicates that this entity should be defined
2493 here. Make sure the back end honors that request. */
2494 if (VAR_P (decl))
2495 mark_needed (decl);
2496 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2497 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2499 tree clone;
2500 FOR_EACH_CLONE (clone, decl)
2501 mark_needed (clone);
2503 else
2504 mark_needed (decl);
2505 /* Output the definition as an ordinary strong definition. */
2506 DECL_EXTERNAL (decl) = 0;
2507 DECL_INTERFACE_KNOWN (decl) = 1;
2508 return;
2511 if (import_p)
2512 /* We have already decided what to do with this DECL; there is no
2513 need to check anything further. */
2515 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2517 class_type = DECL_CONTEXT (decl);
2518 import_export_class (class_type);
2519 if (TYPE_FOR_JAVA (class_type))
2520 import_p = true;
2521 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2522 && CLASSTYPE_INTERFACE_ONLY (class_type))
2523 import_p = true;
2524 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2525 && !CLASSTYPE_USE_TEMPLATE (class_type)
2526 && CLASSTYPE_KEY_METHOD (class_type)
2527 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2528 /* The ABI requires that all virtual tables be emitted with
2529 COMDAT linkage. However, on systems where COMDAT symbols
2530 don't show up in the table of contents for a static
2531 archive, or on systems without weak symbols (where we
2532 approximate COMDAT linkage by using internal linkage), the
2533 linker will report errors about undefined symbols because
2534 it will not see the virtual table definition. Therefore,
2535 in the case that we know that the virtual table will be
2536 emitted in only one translation unit, we make the virtual
2537 table an ordinary definition with external linkage. */
2538 DECL_EXTERNAL (decl) = 0;
2539 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2541 /* CLASS_TYPE is being exported from this translation unit,
2542 so DECL should be defined here. */
2543 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2544 /* If a class is declared in a header with the "extern
2545 template" extension, then it will not be instantiated,
2546 even in translation units that would normally require
2547 it. Often such classes are explicitly instantiated in
2548 one translation unit. Therefore, the explicit
2549 instantiation must be made visible to other translation
2550 units. */
2551 DECL_EXTERNAL (decl) = 0;
2552 else
2554 /* The generic C++ ABI says that class data is always
2555 COMDAT, even if there is a key function. Some
2556 variants (e.g., the ARM EABI) says that class data
2557 only has COMDAT linkage if the class data might be
2558 emitted in more than one translation unit. When the
2559 key method can be inline and is inline, we still have
2560 to arrange for comdat even though
2561 class_data_always_comdat is false. */
2562 if (!CLASSTYPE_KEY_METHOD (class_type)
2563 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2564 || targetm.cxx.class_data_always_comdat ())
2566 /* The ABI requires COMDAT linkage. Normally, we
2567 only emit COMDAT things when they are needed;
2568 make sure that we realize that this entity is
2569 indeed needed. */
2570 comdat_p = true;
2571 mark_needed (decl);
2575 else if (!flag_implicit_templates
2576 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2577 import_p = true;
2578 else
2579 comdat_p = true;
2581 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2583 tree type = TREE_TYPE (DECL_NAME (decl));
2584 if (CLASS_TYPE_P (type))
2586 class_type = type;
2587 import_export_class (type);
2588 if (CLASSTYPE_INTERFACE_KNOWN (type)
2589 && TYPE_POLYMORPHIC_P (type)
2590 && CLASSTYPE_INTERFACE_ONLY (type)
2591 /* If -fno-rtti was specified, then we cannot be sure
2592 that RTTI information will be emitted with the
2593 virtual table of the class, so we must emit it
2594 wherever it is used. */
2595 && flag_rtti)
2596 import_p = true;
2597 else
2599 if (CLASSTYPE_INTERFACE_KNOWN (type)
2600 && !CLASSTYPE_INTERFACE_ONLY (type))
2602 comdat_p = (targetm.cxx.class_data_always_comdat ()
2603 || (CLASSTYPE_KEY_METHOD (type)
2604 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2605 mark_needed (decl);
2606 if (!flag_weak)
2608 comdat_p = false;
2609 DECL_EXTERNAL (decl) = 0;
2612 else
2613 comdat_p = true;
2616 else
2617 comdat_p = true;
2619 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2621 /* DECL is an implicit instantiation of a function or static
2622 data member. */
2623 if ((flag_implicit_templates
2624 && !flag_use_repository)
2625 || (flag_implicit_inline_templates
2626 && TREE_CODE (decl) == FUNCTION_DECL
2627 && DECL_DECLARED_INLINE_P (decl)))
2628 comdat_p = true;
2629 else
2630 /* If we are not implicitly generating templates, then mark
2631 this entity as undefined in this translation unit. */
2632 import_p = true;
2634 else if (DECL_FUNCTION_MEMBER_P (decl))
2636 if (!DECL_DECLARED_INLINE_P (decl))
2638 tree ctype = DECL_CONTEXT (decl);
2639 import_export_class (ctype);
2640 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2642 DECL_NOT_REALLY_EXTERN (decl)
2643 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2644 || (DECL_DECLARED_INLINE_P (decl)
2645 && ! flag_implement_inlines
2646 && !DECL_VINDEX (decl)));
2648 if (!DECL_NOT_REALLY_EXTERN (decl))
2649 DECL_EXTERNAL (decl) = 1;
2651 /* Always make artificials weak. */
2652 if (DECL_ARTIFICIAL (decl) && flag_weak)
2653 comdat_p = true;
2654 else
2655 maybe_make_one_only (decl);
2658 else
2659 comdat_p = true;
2661 else
2662 comdat_p = true;
2664 if (import_p)
2666 /* If we are importing DECL into this translation unit, mark is
2667 an undefined here. */
2668 DECL_EXTERNAL (decl) = 1;
2669 DECL_NOT_REALLY_EXTERN (decl) = 0;
2671 else if (comdat_p)
2673 /* If we decided to put DECL in COMDAT, mark it accordingly at
2674 this point. */
2675 comdat_linkage (decl);
2678 DECL_INTERFACE_KNOWN (decl) = 1;
2681 /* Return an expression that performs the destruction of DECL, which
2682 must be a VAR_DECL whose type has a non-trivial destructor, or is
2683 an array whose (innermost) elements have a non-trivial destructor. */
2685 tree
2686 build_cleanup (tree decl)
2688 tree temp;
2689 tree type = TREE_TYPE (decl);
2691 /* This function should only be called for declarations that really
2692 require cleanups. */
2693 gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type));
2695 /* Treat all objects with destructors as used; the destructor may do
2696 something substantive. */
2697 mark_used (decl);
2699 if (TREE_CODE (type) == ARRAY_TYPE)
2700 temp = decl;
2701 else
2702 temp = build_address (decl);
2703 temp = build_delete (TREE_TYPE (temp), temp,
2704 sfk_complete_destructor,
2705 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
2706 tf_warning_or_error);
2707 return temp;
2710 /* Returns the initialization guard variable for the variable DECL,
2711 which has static storage duration. */
2713 tree
2714 get_guard (tree decl)
2716 tree sname;
2717 tree guard;
2719 sname = mangle_guard_variable (decl);
2720 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2721 if (! guard)
2723 tree guard_type;
2725 /* We use a type that is big enough to contain a mutex as well
2726 as an integer counter. */
2727 guard_type = targetm.cxx.guard_type ();
2728 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2729 VAR_DECL, sname, guard_type);
2731 /* The guard should have the same linkage as what it guards. */
2732 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2733 TREE_STATIC (guard) = TREE_STATIC (decl);
2734 DECL_COMMON (guard) = DECL_COMMON (decl);
2735 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2736 DECL_TLS_MODEL (guard) = DECL_TLS_MODEL (decl);
2737 if (DECL_ONE_ONLY (decl))
2738 make_decl_one_only (guard, cxx_comdat_group (guard));
2739 if (TREE_PUBLIC (decl))
2740 DECL_WEAK (guard) = DECL_WEAK (decl);
2741 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2742 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2744 DECL_ARTIFICIAL (guard) = 1;
2745 DECL_IGNORED_P (guard) = 1;
2746 TREE_USED (guard) = 1;
2747 pushdecl_top_level_and_finish (guard, NULL_TREE);
2749 return guard;
2752 /* Return those bits of the GUARD variable that should be set when the
2753 guarded entity is actually initialized. */
2755 static tree
2756 get_guard_bits (tree guard)
2758 if (!targetm.cxx.guard_mask_bit ())
2760 /* We only set the first byte of the guard, in order to leave room
2761 for a mutex in the high-order bits. */
2762 guard = build1 (ADDR_EXPR,
2763 build_pointer_type (TREE_TYPE (guard)),
2764 guard);
2765 guard = build1 (NOP_EXPR,
2766 build_pointer_type (char_type_node),
2767 guard);
2768 guard = build1 (INDIRECT_REF, char_type_node, guard);
2771 return guard;
2774 /* Return an expression which determines whether or not the GUARD
2775 variable has already been initialized. */
2777 tree
2778 get_guard_cond (tree guard)
2780 tree guard_value;
2782 /* Check to see if the GUARD is zero. */
2783 guard = get_guard_bits (guard);
2785 /* Mask off all but the low bit. */
2786 if (targetm.cxx.guard_mask_bit ())
2788 guard_value = integer_one_node;
2789 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2790 guard_value = convert (TREE_TYPE (guard), guard_value);
2791 guard = cp_build_binary_op (input_location,
2792 BIT_AND_EXPR, guard, guard_value,
2793 tf_warning_or_error);
2796 guard_value = integer_zero_node;
2797 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2798 guard_value = convert (TREE_TYPE (guard), guard_value);
2799 return cp_build_binary_op (input_location,
2800 EQ_EXPR, guard, guard_value,
2801 tf_warning_or_error);
2804 /* Return an expression which sets the GUARD variable, indicating that
2805 the variable being guarded has been initialized. */
2807 tree
2808 set_guard (tree guard)
2810 tree guard_init;
2812 /* Set the GUARD to one. */
2813 guard = get_guard_bits (guard);
2814 guard_init = integer_one_node;
2815 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2816 guard_init = convert (TREE_TYPE (guard), guard_init);
2817 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2818 tf_warning_or_error);
2821 /* Returns true iff we can tell that VAR does not have a dynamic
2822 initializer. */
2824 static bool
2825 var_defined_without_dynamic_init (tree var)
2827 /* If it's defined in another TU, we can't tell. */
2828 if (DECL_EXTERNAL (var))
2829 return false;
2830 /* If it has a non-trivial destructor, registering the destructor
2831 counts as dynamic initialization. */
2832 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
2833 return false;
2834 /* If it's in this TU, its initializer has been processed. */
2835 gcc_assert (DECL_INITIALIZED_P (var));
2836 /* If it has no initializer or a constant one, it's not dynamic. */
2837 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
2838 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
2841 /* Returns true iff VAR is a variable that needs uses to be
2842 wrapped for possible dynamic initialization. */
2844 static bool
2845 var_needs_tls_wrapper (tree var)
2847 return (!error_operand_p (var)
2848 && DECL_THREAD_LOCAL_P (var)
2849 && !DECL_GNU_TLS_P (var)
2850 && !DECL_FUNCTION_SCOPE_P (var)
2851 && !var_defined_without_dynamic_init (var));
2854 /* Get the FUNCTION_DECL for the shared TLS init function for this
2855 translation unit. */
2857 static tree
2858 get_local_tls_init_fn (void)
2860 tree sname = get_identifier ("__tls_init");
2861 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
2862 if (!fn)
2864 fn = build_lang_decl (FUNCTION_DECL, sname,
2865 build_function_type (void_type_node,
2866 void_list_node));
2867 SET_DECL_LANGUAGE (fn, lang_c);
2868 TREE_PUBLIC (fn) = false;
2869 DECL_ARTIFICIAL (fn) = true;
2870 mark_used (fn);
2871 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
2873 return fn;
2876 /* Get a FUNCTION_DECL for the init function for the thread_local
2877 variable VAR. The init function will be an alias to the function
2878 that initializes all the non-local TLS variables in the translation
2879 unit. The init function is only used by the wrapper function. */
2881 static tree
2882 get_tls_init_fn (tree var)
2884 /* Only C++11 TLS vars need this init fn. */
2885 if (!var_needs_tls_wrapper (var))
2886 return NULL_TREE;
2888 /* If -fno-extern-tls-init, assume that we don't need to call
2889 a tls init function for a variable defined in another TU. */
2890 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
2891 return NULL_TREE;
2893 #ifdef ASM_OUTPUT_DEF
2894 /* If the variable is internal, or if we can't generate aliases,
2895 call the local init function directly. */
2896 if (!TREE_PUBLIC (var))
2897 #endif
2898 return get_local_tls_init_fn ();
2900 tree sname = mangle_tls_init_fn (var);
2901 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
2902 if (!fn)
2904 fn = build_lang_decl (FUNCTION_DECL, sname,
2905 build_function_type (void_type_node,
2906 void_list_node));
2907 SET_DECL_LANGUAGE (fn, lang_c);
2908 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
2909 DECL_ARTIFICIAL (fn) = true;
2910 DECL_COMDAT (fn) = DECL_COMDAT (var);
2911 DECL_EXTERNAL (fn) = true;
2912 if (DECL_ONE_ONLY (var))
2913 make_decl_one_only (fn, cxx_comdat_group (fn));
2914 if (TREE_PUBLIC (var))
2916 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
2917 /* If the variable is defined somewhere else and might have static
2918 initialization, make the init function a weak reference. */
2919 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
2920 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
2921 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
2922 && DECL_EXTERNAL (var))
2923 declare_weak (fn);
2924 else
2925 DECL_WEAK (fn) = DECL_WEAK (var);
2927 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
2928 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
2929 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
2930 DECL_IGNORED_P (fn) = 1;
2931 mark_used (fn);
2933 DECL_BEFRIENDING_CLASSES (fn) = var;
2935 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
2937 return fn;
2940 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
2941 variable VAR. The wrapper function calls the init function (if any) for
2942 VAR and then returns a reference to VAR. The wrapper function is used
2943 in place of VAR everywhere VAR is mentioned. */
2945 tree
2946 get_tls_wrapper_fn (tree var)
2948 /* Only C++11 TLS vars need this wrapper fn. */
2949 if (!var_needs_tls_wrapper (var))
2950 return NULL_TREE;
2952 tree sname = mangle_tls_wrapper_fn (var);
2953 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
2954 if (!fn)
2956 /* A named rvalue reference is an lvalue, so the wrapper should
2957 always return an lvalue reference. */
2958 tree type = non_reference (TREE_TYPE (var));
2959 type = build_reference_type (type);
2960 tree fntype = build_function_type (type, void_list_node);
2961 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
2962 SET_DECL_LANGUAGE (fn, lang_c);
2963 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
2964 DECL_ARTIFICIAL (fn) = true;
2965 DECL_IGNORED_P (fn) = 1;
2966 /* The wrapper is inline and emitted everywhere var is used. */
2967 DECL_DECLARED_INLINE_P (fn) = true;
2968 if (TREE_PUBLIC (var))
2970 comdat_linkage (fn);
2971 #ifdef HAVE_GAS_HIDDEN
2972 /* Make the wrapper bind locally; there's no reason to share
2973 the wrapper between multiple shared objects. */
2974 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
2975 DECL_VISIBILITY_SPECIFIED (fn) = true;
2976 #endif
2978 if (!TREE_PUBLIC (fn))
2979 DECL_INTERFACE_KNOWN (fn) = true;
2980 mark_used (fn);
2981 note_vague_linkage_fn (fn);
2983 #if 0
2984 /* We want CSE to commonize calls to the wrapper, but marking it as
2985 pure is unsafe since it has side-effects. I guess we need a new
2986 ECF flag even weaker than ECF_PURE. FIXME! */
2987 DECL_PURE_P (fn) = true;
2988 #endif
2990 DECL_BEFRIENDING_CLASSES (fn) = var;
2992 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
2994 return fn;
2997 /* At EOF, generate the definition for the TLS wrapper function FN:
2999 T& var_wrapper() {
3000 if (init_fn) init_fn();
3001 return var;
3002 } */
3004 static void
3005 generate_tls_wrapper (tree fn)
3007 tree var = DECL_BEFRIENDING_CLASSES (fn);
3009 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3010 tree body = begin_function_body ();
3011 /* Only call the init fn if there might be one. */
3012 if (tree init_fn = get_tls_init_fn (var))
3014 tree if_stmt = NULL_TREE;
3015 /* If init_fn is a weakref, make sure it exists before calling. */
3016 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3018 if_stmt = begin_if_stmt ();
3019 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3020 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3021 NE_EXPR, addr, nullptr_node,
3022 tf_warning_or_error);
3023 finish_if_stmt_cond (cond, if_stmt);
3025 finish_expr_stmt (build_cxx_call
3026 (init_fn, 0, NULL, tf_warning_or_error));
3027 if (if_stmt)
3029 finish_then_clause (if_stmt);
3030 finish_if_stmt (if_stmt);
3033 else
3034 /* If there's no initialization, the wrapper is a constant function. */
3035 TREE_READONLY (fn) = true;
3036 finish_return_stmt (convert_from_reference (var));
3037 finish_function_body (body);
3038 expand_or_defer_fn (finish_function (0));
3041 /* Start the process of running a particular set of global constructors
3042 or destructors. Subroutine of do_[cd]tors. */
3044 static tree
3045 start_objects (int method_type, int initp)
3047 tree body;
3048 tree fndecl;
3049 char type[14];
3051 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3053 if (initp != DEFAULT_INIT_PRIORITY)
3055 char joiner;
3057 #ifdef JOINER
3058 joiner = JOINER;
3059 #else
3060 joiner = '_';
3061 #endif
3063 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3065 else
3066 sprintf (type, "sub_%c", method_type);
3068 fndecl = build_lang_decl (FUNCTION_DECL,
3069 get_file_function_name (type),
3070 build_function_type_list (void_type_node,
3071 NULL_TREE));
3072 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3074 TREE_PUBLIC (current_function_decl) = 0;
3076 /* Mark as artificial because it's not explicitly in the user's
3077 source code. */
3078 DECL_ARTIFICIAL (current_function_decl) = 1;
3080 /* Mark this declaration as used to avoid spurious warnings. */
3081 TREE_USED (current_function_decl) = 1;
3083 /* Mark this function as a global constructor or destructor. */
3084 if (method_type == 'I')
3085 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3086 else
3087 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3089 body = begin_compound_stmt (BCS_FN_BODY);
3091 return body;
3094 /* Finish the process of running a particular set of global constructors
3095 or destructors. Subroutine of do_[cd]tors. */
3097 static void
3098 finish_objects (int method_type, int initp, tree body)
3100 tree fn;
3102 /* Finish up. */
3103 finish_compound_stmt (body);
3104 fn = finish_function (0);
3106 if (method_type == 'I')
3108 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3109 decl_init_priority_insert (fn, initp);
3111 else
3113 DECL_STATIC_DESTRUCTOR (fn) = 1;
3114 decl_fini_priority_insert (fn, initp);
3117 expand_or_defer_fn (fn);
3120 /* The names of the parameters to the function created to handle
3121 initializations and destructions for objects with static storage
3122 duration. */
3123 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3124 #define PRIORITY_IDENTIFIER "__priority"
3126 /* The name of the function we create to handle initializations and
3127 destructions for objects with static storage duration. */
3128 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3130 /* The declaration for the __INITIALIZE_P argument. */
3131 static GTY(()) tree initialize_p_decl;
3133 /* The declaration for the __PRIORITY argument. */
3134 static GTY(()) tree priority_decl;
3136 /* The declaration for the static storage duration function. */
3137 static GTY(()) tree ssdf_decl;
3139 /* All the static storage duration functions created in this
3140 translation unit. */
3141 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3143 /* A map from priority levels to information about that priority
3144 level. There may be many such levels, so efficient lookup is
3145 important. */
3146 static splay_tree priority_info_map;
3148 /* Begins the generation of the function that will handle all
3149 initialization and destruction of objects with static storage
3150 duration. The function generated takes two parameters of type
3151 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3152 nonzero, it performs initializations. Otherwise, it performs
3153 destructions. It only performs those initializations or
3154 destructions with the indicated __PRIORITY. The generated function
3155 returns no value.
3157 It is assumed that this function will only be called once per
3158 translation unit. */
3160 static tree
3161 start_static_storage_duration_function (unsigned count)
3163 tree type;
3164 tree body;
3165 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3167 /* Create the identifier for this function. It will be of the form
3168 SSDF_IDENTIFIER_<number>. */
3169 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3171 type = build_function_type_list (void_type_node,
3172 integer_type_node, integer_type_node,
3173 NULL_TREE);
3175 /* Create the FUNCTION_DECL itself. */
3176 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3177 get_identifier (id),
3178 type);
3179 TREE_PUBLIC (ssdf_decl) = 0;
3180 DECL_ARTIFICIAL (ssdf_decl) = 1;
3182 /* Put this function in the list of functions to be called from the
3183 static constructors and destructors. */
3184 if (!ssdf_decls)
3186 vec_alloc (ssdf_decls, 32);
3188 /* Take this opportunity to initialize the map from priority
3189 numbers to information about that priority level. */
3190 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3191 /*delete_key_fn=*/0,
3192 /*delete_value_fn=*/
3193 (splay_tree_delete_value_fn) &free);
3195 /* We always need to generate functions for the
3196 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3197 priorities later, we'll be sure to find the
3198 DEFAULT_INIT_PRIORITY. */
3199 get_priority_info (DEFAULT_INIT_PRIORITY);
3202 vec_safe_push (ssdf_decls, ssdf_decl);
3204 /* Create the argument list. */
3205 initialize_p_decl = cp_build_parm_decl
3206 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3207 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3208 TREE_USED (initialize_p_decl) = 1;
3209 priority_decl = cp_build_parm_decl
3210 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3211 DECL_CONTEXT (priority_decl) = ssdf_decl;
3212 TREE_USED (priority_decl) = 1;
3214 DECL_CHAIN (initialize_p_decl) = priority_decl;
3215 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3217 /* Put the function in the global scope. */
3218 pushdecl (ssdf_decl);
3220 /* Start the function itself. This is equivalent to declaring the
3221 function as:
3223 static void __ssdf (int __initialize_p, init __priority_p);
3225 It is static because we only need to call this function from the
3226 various constructor and destructor functions for this module. */
3227 start_preparsed_function (ssdf_decl,
3228 /*attrs=*/NULL_TREE,
3229 SF_PRE_PARSED);
3231 /* Set up the scope of the outermost block in the function. */
3232 body = begin_compound_stmt (BCS_FN_BODY);
3234 return body;
3237 /* Finish the generation of the function which performs initialization
3238 and destruction of objects with static storage duration. After
3239 this point, no more such objects can be created. */
3241 static void
3242 finish_static_storage_duration_function (tree body)
3244 /* Close out the function. */
3245 finish_compound_stmt (body);
3246 expand_or_defer_fn (finish_function (0));
3249 /* Return the information about the indicated PRIORITY level. If no
3250 code to handle this level has yet been generated, generate the
3251 appropriate prologue. */
3253 static priority_info
3254 get_priority_info (int priority)
3256 priority_info pi;
3257 splay_tree_node n;
3259 n = splay_tree_lookup (priority_info_map,
3260 (splay_tree_key) priority);
3261 if (!n)
3263 /* Create a new priority information structure, and insert it
3264 into the map. */
3265 pi = XNEW (struct priority_info_s);
3266 pi->initializations_p = 0;
3267 pi->destructions_p = 0;
3268 splay_tree_insert (priority_info_map,
3269 (splay_tree_key) priority,
3270 (splay_tree_value) pi);
3272 else
3273 pi = (priority_info) n->value;
3275 return pi;
3278 /* The effective initialization priority of a DECL. */
3280 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3281 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3282 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3284 /* Whether a DECL needs a guard to protect it against multiple
3285 initialization. */
3287 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3288 || DECL_ONE_ONLY (decl) \
3289 || DECL_WEAK (decl)))
3291 /* Called from one_static_initialization_or_destruction(),
3292 via walk_tree.
3293 Walks the initializer list of a global variable and looks for
3294 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3295 and that have their DECL_CONTEXT() == NULL.
3296 For each such temporary variable, set their DECL_CONTEXT() to
3297 the current function. This is necessary because otherwise
3298 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3299 when trying to refer to a temporary variable that does not have
3300 it's DECL_CONTECT() properly set. */
3301 static tree
3302 fix_temporary_vars_context_r (tree *node,
3303 int * /*unused*/,
3304 void * /*unused1*/)
3306 gcc_assert (current_function_decl);
3308 if (TREE_CODE (*node) == BIND_EXPR)
3310 tree var;
3312 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3313 if (VAR_P (var)
3314 && !DECL_NAME (var)
3315 && DECL_ARTIFICIAL (var)
3316 && !DECL_CONTEXT (var))
3317 DECL_CONTEXT (var) = current_function_decl;
3320 return NULL_TREE;
3323 /* Set up to handle the initialization or destruction of DECL. If
3324 INITP is nonzero, we are initializing the variable. Otherwise, we
3325 are destroying it. */
3327 static void
3328 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3330 tree guard_if_stmt = NULL_TREE;
3331 tree guard;
3333 /* If we are supposed to destruct and there's a trivial destructor,
3334 nothing has to be done. */
3335 if (!initp
3336 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3337 return;
3339 /* Trick the compiler into thinking we are at the file and line
3340 where DECL was declared so that error-messages make sense, and so
3341 that the debugger will show somewhat sensible file and line
3342 information. */
3343 input_location = DECL_SOURCE_LOCATION (decl);
3345 /* Make sure temporary variables in the initialiser all have
3346 their DECL_CONTEXT() set to a value different from NULL_TREE.
3347 This can happen when global variables initialisers are built.
3348 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3349 the temporary variables that might have been generated in the
3350 accompagning initialisers is NULL_TREE, meaning the variables have been
3351 declared in the global namespace.
3352 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3353 of the temporaries are set to the current function decl. */
3354 cp_walk_tree_without_duplicates (&init,
3355 fix_temporary_vars_context_r,
3356 NULL);
3358 /* Because of:
3360 [class.access.spec]
3362 Access control for implicit calls to the constructors,
3363 the conversion functions, or the destructor called to
3364 create and destroy a static data member is performed as
3365 if these calls appeared in the scope of the member's
3366 class.
3368 we pretend we are in a static member function of the class of
3369 which the DECL is a member. */
3370 if (member_p (decl))
3372 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3373 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3376 /* Assume we don't need a guard. */
3377 guard = NULL_TREE;
3378 /* We need a guard if this is an object with external linkage that
3379 might be initialized in more than one place. (For example, a
3380 static data member of a template, when the data member requires
3381 construction.) */
3382 if (NEEDS_GUARD_P (decl))
3384 tree guard_cond;
3386 guard = get_guard (decl);
3388 /* When using __cxa_atexit, we just check the GUARD as we would
3389 for a local static. */
3390 if (flag_use_cxa_atexit)
3392 /* When using __cxa_atexit, we never try to destroy
3393 anything from a static destructor. */
3394 gcc_assert (initp);
3395 guard_cond = get_guard_cond (guard);
3397 /* If we don't have __cxa_atexit, then we will be running
3398 destructors from .fini sections, or their equivalents. So,
3399 we need to know how many times we've tried to initialize this
3400 object. We do initializations only if the GUARD is zero,
3401 i.e., if we are the first to initialize the variable. We do
3402 destructions only if the GUARD is one, i.e., if we are the
3403 last to destroy the variable. */
3404 else if (initp)
3405 guard_cond
3406 = cp_build_binary_op (input_location,
3407 EQ_EXPR,
3408 cp_build_unary_op (PREINCREMENT_EXPR,
3409 guard,
3410 /*noconvert=*/1,
3411 tf_warning_or_error),
3412 integer_one_node,
3413 tf_warning_or_error);
3414 else
3415 guard_cond
3416 = cp_build_binary_op (input_location,
3417 EQ_EXPR,
3418 cp_build_unary_op (PREDECREMENT_EXPR,
3419 guard,
3420 /*noconvert=*/1,
3421 tf_warning_or_error),
3422 integer_zero_node,
3423 tf_warning_or_error);
3425 guard_if_stmt = begin_if_stmt ();
3426 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3430 /* If we're using __cxa_atexit, we have not already set the GUARD,
3431 so we must do so now. */
3432 if (guard && initp && flag_use_cxa_atexit)
3433 finish_expr_stmt (set_guard (guard));
3435 /* Perform the initialization or destruction. */
3436 if (initp)
3438 if (init)
3439 finish_expr_stmt (init);
3441 /* If we're using __cxa_atexit, register a function that calls the
3442 destructor for the object. */
3443 if (flag_use_cxa_atexit)
3444 finish_expr_stmt (register_dtor_fn (decl));
3446 else
3447 finish_expr_stmt (build_cleanup (decl));
3449 /* Finish the guard if-stmt, if necessary. */
3450 if (guard)
3452 finish_then_clause (guard_if_stmt);
3453 finish_if_stmt (guard_if_stmt);
3456 /* Now that we're done with DECL we don't need to pretend to be a
3457 member of its class any longer. */
3458 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3459 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3462 /* Generate code to do the initialization or destruction of the decls in VARS,
3463 a TREE_LIST of VAR_DECL with static storage duration.
3464 Whether initialization or destruction is performed is specified by INITP. */
3466 static void
3467 do_static_initialization_or_destruction (tree vars, bool initp)
3469 tree node, init_if_stmt, cond;
3471 /* Build the outer if-stmt to check for initialization or destruction. */
3472 init_if_stmt = begin_if_stmt ();
3473 cond = initp ? integer_one_node : integer_zero_node;
3474 cond = cp_build_binary_op (input_location,
3475 EQ_EXPR,
3476 initialize_p_decl,
3477 cond,
3478 tf_warning_or_error);
3479 finish_if_stmt_cond (cond, init_if_stmt);
3481 node = vars;
3482 do {
3483 tree decl = TREE_VALUE (node);
3484 tree priority_if_stmt;
3485 int priority;
3486 priority_info pi;
3488 /* If we don't need a destructor, there's nothing to do. Avoid
3489 creating a possibly empty if-stmt. */
3490 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3492 node = TREE_CHAIN (node);
3493 continue;
3496 /* Remember that we had an initialization or finalization at this
3497 priority. */
3498 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3499 pi = get_priority_info (priority);
3500 if (initp)
3501 pi->initializations_p = 1;
3502 else
3503 pi->destructions_p = 1;
3505 /* Conditionalize this initialization on being in the right priority
3506 and being initializing/finalizing appropriately. */
3507 priority_if_stmt = begin_if_stmt ();
3508 cond = cp_build_binary_op (input_location,
3509 EQ_EXPR,
3510 priority_decl,
3511 build_int_cst (NULL_TREE, priority),
3512 tf_warning_or_error);
3513 finish_if_stmt_cond (cond, priority_if_stmt);
3515 /* Process initializers with same priority. */
3516 for (; node
3517 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3518 node = TREE_CHAIN (node))
3519 /* Do one initialization or destruction. */
3520 one_static_initialization_or_destruction (TREE_VALUE (node),
3521 TREE_PURPOSE (node), initp);
3523 /* Finish up the priority if-stmt body. */
3524 finish_then_clause (priority_if_stmt);
3525 finish_if_stmt (priority_if_stmt);
3527 } while (node);
3529 /* Finish up the init/destruct if-stmt body. */
3530 finish_then_clause (init_if_stmt);
3531 finish_if_stmt (init_if_stmt);
3534 /* VARS is a list of variables with static storage duration which may
3535 need initialization and/or finalization. Remove those variables
3536 that don't really need to be initialized or finalized, and return
3537 the resulting list. The order in which the variables appear in
3538 VARS is in reverse order of the order in which they should actually
3539 be initialized. The list we return is in the unreversed order;
3540 i.e., the first variable should be initialized first. */
3542 static tree
3543 prune_vars_needing_no_initialization (tree *vars)
3545 tree *var = vars;
3546 tree result = NULL_TREE;
3548 while (*var)
3550 tree t = *var;
3551 tree decl = TREE_VALUE (t);
3552 tree init = TREE_PURPOSE (t);
3554 /* Deal gracefully with error. */
3555 if (decl == error_mark_node)
3557 var = &TREE_CHAIN (t);
3558 continue;
3561 /* The only things that can be initialized are variables. */
3562 gcc_assert (VAR_P (decl));
3564 /* If this object is not defined, we don't need to do anything
3565 here. */
3566 if (DECL_EXTERNAL (decl))
3568 var = &TREE_CHAIN (t);
3569 continue;
3572 /* Also, if the initializer already contains errors, we can bail
3573 out now. */
3574 if (init && TREE_CODE (init) == TREE_LIST
3575 && value_member (error_mark_node, init))
3577 var = &TREE_CHAIN (t);
3578 continue;
3581 /* This variable is going to need initialization and/or
3582 finalization, so we add it to the list. */
3583 *var = TREE_CHAIN (t);
3584 TREE_CHAIN (t) = result;
3585 result = t;
3588 return result;
3591 /* Make sure we have told the back end about all the variables in
3592 VARS. */
3594 static void
3595 write_out_vars (tree vars)
3597 tree v;
3599 for (v = vars; v; v = TREE_CHAIN (v))
3601 tree var = TREE_VALUE (v);
3602 if (!var_finalized_p (var))
3604 import_export_decl (var);
3605 rest_of_decl_compilation (var, 1, 1);
3610 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3611 (otherwise) that will initialize all global objects with static
3612 storage duration having the indicated PRIORITY. */
3614 static void
3615 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3616 location_t *locus)
3618 char function_key;
3619 tree fndecl;
3620 tree body;
3621 size_t i;
3623 input_location = *locus;
3624 /* ??? */
3625 /* Was: locus->line++; */
3627 /* We use `I' to indicate initialization and `D' to indicate
3628 destruction. */
3629 function_key = constructor_p ? 'I' : 'D';
3631 /* We emit the function lazily, to avoid generating empty
3632 global constructors and destructors. */
3633 body = NULL_TREE;
3635 /* For Objective-C++, we may need to initialize metadata found in this module.
3636 This must be done _before_ any other static initializations. */
3637 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3638 && constructor_p && objc_static_init_needed_p ())
3640 body = start_objects (function_key, priority);
3641 objc_generate_static_init_call (NULL_TREE);
3644 /* Call the static storage duration function with appropriate
3645 arguments. */
3646 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3648 /* Calls to pure or const functions will expand to nothing. */
3649 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3651 tree call;
3653 if (! body)
3654 body = start_objects (function_key, priority);
3656 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3657 build_int_cst (NULL_TREE,
3658 constructor_p),
3659 build_int_cst (NULL_TREE,
3660 priority),
3661 NULL_TREE);
3662 finish_expr_stmt (call);
3666 /* Close out the function. */
3667 if (body)
3668 finish_objects (function_key, priority, body);
3671 /* Generate constructor and destructor functions for the priority
3672 indicated by N. */
3674 static int
3675 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3677 location_t *locus = (location_t *) data;
3678 int priority = (int) n->key;
3679 priority_info pi = (priority_info) n->value;
3681 /* Generate the functions themselves, but only if they are really
3682 needed. */
3683 if (pi->initializations_p)
3684 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3685 if (pi->destructions_p)
3686 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3688 /* Keep iterating. */
3689 return 0;
3692 /* Java requires that we be able to reference a local address for a
3693 method, and not be confused by PLT entries. If hidden aliases are
3694 supported, collect and return all the functions for which we should
3695 emit a hidden alias. */
3697 static struct pointer_set_t *
3698 collect_candidates_for_java_method_aliases (void)
3700 struct cgraph_node *node;
3701 struct pointer_set_t *candidates = NULL;
3703 #ifndef HAVE_GAS_HIDDEN
3704 return candidates;
3705 #endif
3707 FOR_EACH_FUNCTION (node)
3709 tree fndecl = node->symbol.decl;
3711 if (DECL_CLASS_SCOPE_P (fndecl)
3712 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3713 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3715 if (candidates == NULL)
3716 candidates = pointer_set_create ();
3717 pointer_set_insert (candidates, fndecl);
3721 return candidates;
3725 /* Java requires that we be able to reference a local address for a
3726 method, and not be confused by PLT entries. If hidden aliases are
3727 supported, emit one for each java function that we've emitted.
3728 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3729 by collect_candidates_for_java_method_aliases. */
3731 static void
3732 build_java_method_aliases (struct pointer_set_t *candidates)
3734 struct cgraph_node *node;
3736 #ifndef HAVE_GAS_HIDDEN
3737 return;
3738 #endif
3740 FOR_EACH_FUNCTION (node)
3742 tree fndecl = node->symbol.decl;
3744 if (TREE_ASM_WRITTEN (fndecl)
3745 && pointer_set_contains (candidates, fndecl))
3747 /* Mangle the name in a predictable way; we need to reference
3748 this from a java compiled object file. */
3749 tree oid, nid, alias;
3750 const char *oname;
3751 char *nname;
3753 oid = DECL_ASSEMBLER_NAME (fndecl);
3754 oname = IDENTIFIER_POINTER (oid);
3755 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3756 nname = ACONCAT (("_ZGA", oname+2, NULL));
3757 nid = get_identifier (nname);
3759 alias = make_alias_for (fndecl, nid);
3760 TREE_PUBLIC (alias) = 1;
3761 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3763 assemble_alias (alias, oid);
3768 /* Return C++ property of T, based on given operation OP. */
3770 static int
3771 cpp_check (tree t, cpp_operation op)
3773 switch (op)
3775 case IS_ABSTRACT:
3776 return DECL_PURE_VIRTUAL_P (t);
3777 case IS_CONSTRUCTOR:
3778 return DECL_CONSTRUCTOR_P (t);
3779 case IS_DESTRUCTOR:
3780 return DECL_DESTRUCTOR_P (t);
3781 case IS_COPY_CONSTRUCTOR:
3782 return DECL_COPY_CONSTRUCTOR_P (t);
3783 case IS_TEMPLATE:
3784 return TREE_CODE (t) == TEMPLATE_DECL;
3785 default:
3786 return 0;
3790 /* Collect source file references recursively, starting from NAMESPC. */
3792 static void
3793 collect_source_refs (tree namespc)
3795 tree t;
3797 if (!namespc)
3798 return;
3800 /* Iterate over names in this name space. */
3801 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
3802 if (!DECL_IS_BUILTIN (t) )
3803 collect_source_ref (DECL_SOURCE_FILE (t));
3805 /* Dump siblings, if any */
3806 collect_source_refs (TREE_CHAIN (namespc));
3808 /* Dump children, if any */
3809 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
3812 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
3813 starting from NAMESPC. */
3815 static void
3816 collect_ada_namespace (tree namespc, const char *source_file)
3818 if (!namespc)
3819 return;
3821 /* Collect decls from this namespace */
3822 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
3824 /* Collect siblings, if any */
3825 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
3827 /* Collect children, if any */
3828 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
3831 /* Returns true iff there is a definition available for variable or
3832 function DECL. */
3834 static bool
3835 decl_defined_p (tree decl)
3837 if (TREE_CODE (decl) == FUNCTION_DECL)
3838 return (DECL_INITIAL (decl) != NULL_TREE);
3839 else
3841 gcc_assert (VAR_P (decl));
3842 return !DECL_EXTERNAL (decl);
3846 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
3848 [expr.const]
3850 An integral constant-expression can only involve ... const
3851 variables of integral or enumeration types initialized with
3852 constant expressions ...
3854 C++0x also allows constexpr variables and temporaries initialized
3855 with constant expressions. We handle the former here, but the latter
3856 are just folded away in cxx_eval_constant_expression.
3858 The standard does not require that the expression be non-volatile.
3859 G++ implements the proposed correction in DR 457. */
3861 bool
3862 decl_constant_var_p (tree decl)
3864 if (!decl_maybe_constant_var_p (decl))
3865 return false;
3867 /* We don't know if a template static data member is initialized with
3868 a constant expression until we instantiate its initializer. Even
3869 in the case of a constexpr variable, we can't treat it as a
3870 constant until its initializer is complete in case it's used in
3871 its own initializer. */
3872 mark_used (decl);
3873 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
3876 /* Returns true if DECL could be a symbolic constant variable, depending on
3877 its initializer. */
3879 bool
3880 decl_maybe_constant_var_p (tree decl)
3882 tree type = TREE_TYPE (decl);
3883 if (!VAR_P (decl))
3884 return false;
3885 if (DECL_DECLARED_CONSTEXPR_P (decl))
3886 return true;
3887 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
3888 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3891 /* Complain that DECL uses a type with no linkage but is never defined. */
3893 static void
3894 no_linkage_error (tree decl)
3896 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
3897 if (TYPE_ANONYMOUS_P (t))
3899 permerror (0, "%q+#D, declared using anonymous type, "
3900 "is used but never defined", decl);
3901 if (is_typedef_decl (TYPE_NAME (t)))
3902 permerror (0, "%q+#D does not refer to the unqualified type, "
3903 "so it is not used for linkage", TYPE_NAME (t));
3905 else
3906 permerror (0, "%q+#D, declared using local type %qT, "
3907 "is used but never defined", decl, t);
3910 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
3912 static void
3913 collect_all_refs (const char *source_file)
3915 collect_ada_namespace (global_namespace, source_file);
3918 /* Clear DECL_EXTERNAL for NODE. */
3920 static bool
3921 clear_decl_external (struct cgraph_node *node, void * /*data*/)
3923 DECL_EXTERNAL (node->symbol.decl) = 0;
3924 return false;
3927 /* Build up the function to run dynamic initializers for thread_local
3928 variables in this translation unit and alias the init functions for the
3929 individual variables to it. */
3931 static void
3932 handle_tls_init (void)
3934 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
3935 if (vars == NULL_TREE)
3936 return;
3938 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
3940 write_out_vars (vars);
3942 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
3943 boolean_type_node);
3944 TREE_PUBLIC (guard) = false;
3945 TREE_STATIC (guard) = true;
3946 DECL_ARTIFICIAL (guard) = true;
3947 DECL_IGNORED_P (guard) = true;
3948 TREE_USED (guard) = true;
3949 DECL_TLS_MODEL (guard) = decl_default_tls_model (guard);
3950 pushdecl_top_level_and_finish (guard, NULL_TREE);
3952 tree fn = get_local_tls_init_fn ();
3953 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
3954 tree body = begin_function_body ();
3955 tree if_stmt = begin_if_stmt ();
3956 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
3957 tf_warning_or_error);
3958 finish_if_stmt_cond (cond, if_stmt);
3959 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
3960 tf_warning_or_error));
3961 for (; vars; vars = TREE_CHAIN (vars))
3963 tree var = TREE_VALUE (vars);
3964 tree init = TREE_PURPOSE (vars);
3965 one_static_initialization_or_destruction (var, init, true);
3967 #ifdef ASM_OUTPUT_DEF
3968 /* Output init aliases even with -fno-extern-tls-init. */
3969 if (TREE_PUBLIC (var))
3971 tree single_init_fn = get_tls_init_fn (var);
3972 cgraph_node *alias
3973 = cgraph_same_body_alias (cgraph_get_create_node (fn),
3974 single_init_fn, fn);
3975 gcc_assert (alias != NULL);
3977 #endif
3980 finish_then_clause (if_stmt);
3981 finish_if_stmt (if_stmt);
3982 finish_function_body (body);
3983 expand_or_defer_fn (finish_function (0));
3986 /* This routine is called at the end of compilation.
3987 Its job is to create all the code needed to initialize and
3988 destroy the global aggregates. We do the destruction
3989 first, since that way we only need to reverse the decls once. */
3991 void
3992 cp_write_global_declarations (void)
3994 tree vars;
3995 bool reconsider;
3996 size_t i;
3997 location_t locus;
3998 unsigned ssdf_count = 0;
3999 int retries = 0;
4000 tree decl;
4001 struct pointer_set_t *candidates;
4003 locus = input_location;
4004 at_eof = 1;
4006 /* Bad parse errors. Just forget about it. */
4007 if (! global_bindings_p () || current_class_type
4008 || !vec_safe_is_empty (decl_namespace_list))
4009 return;
4011 /* This is the point to write out a PCH if we're doing that.
4012 In that case we do not want to do anything else. */
4013 if (pch_file)
4015 c_common_write_pch ();
4016 return;
4019 cgraph_process_same_body_aliases ();
4021 /* Handle -fdump-ada-spec[-slim] */
4022 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4024 if (flag_dump_ada_spec_slim)
4025 collect_source_ref (main_input_filename);
4026 else
4027 collect_source_refs (global_namespace);
4029 dump_ada_specs (collect_all_refs, cpp_check);
4032 /* FIXME - huh? was input_line -= 1;*/
4034 timevar_start (TV_PHASE_DEFERRED);
4036 /* We now have to write out all the stuff we put off writing out.
4037 These include:
4039 o Template specializations that we have not yet instantiated,
4040 but which are needed.
4041 o Initialization and destruction for non-local objects with
4042 static storage duration. (Local objects with static storage
4043 duration are initialized when their scope is first entered,
4044 and are cleaned up via atexit.)
4045 o Virtual function tables.
4047 All of these may cause others to be needed. For example,
4048 instantiating one function may cause another to be needed, and
4049 generating the initializer for an object may cause templates to be
4050 instantiated, etc., etc. */
4052 emit_support_tinfos ();
4056 tree t;
4057 tree decl;
4059 reconsider = false;
4061 /* If there are templates that we've put off instantiating, do
4062 them now. */
4063 instantiate_pending_templates (retries);
4064 ggc_collect ();
4066 /* Write out virtual tables as required. Note that writing out
4067 the virtual table for a template class may cause the
4068 instantiation of members of that class. If we write out
4069 vtables then we remove the class from our list so we don't
4070 have to look at it again. */
4072 while (keyed_classes != NULL_TREE
4073 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4075 reconsider = true;
4076 keyed_classes = TREE_CHAIN (keyed_classes);
4079 t = keyed_classes;
4080 if (t != NULL_TREE)
4082 tree next = TREE_CHAIN (t);
4084 while (next)
4086 if (maybe_emit_vtables (TREE_VALUE (next)))
4088 reconsider = true;
4089 TREE_CHAIN (t) = TREE_CHAIN (next);
4091 else
4092 t = next;
4094 next = TREE_CHAIN (t);
4098 /* Write out needed type info variables. We have to be careful
4099 looping through unemitted decls, because emit_tinfo_decl may
4100 cause other variables to be needed. New elements will be
4101 appended, and we remove from the vector those that actually
4102 get emitted. */
4103 for (i = unemitted_tinfo_decls->length ();
4104 unemitted_tinfo_decls->iterate (--i, &t);)
4105 if (emit_tinfo_decl (t))
4107 reconsider = true;
4108 unemitted_tinfo_decls->unordered_remove (i);
4111 /* The list of objects with static storage duration is built up
4112 in reverse order. We clear STATIC_AGGREGATES so that any new
4113 aggregates added during the initialization of these will be
4114 initialized in the correct order when we next come around the
4115 loop. */
4116 vars = prune_vars_needing_no_initialization (&static_aggregates);
4118 if (vars)
4120 /* We need to start a new initialization function each time
4121 through the loop. That's because we need to know which
4122 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4123 isn't computed until a function is finished, and written
4124 out. That's a deficiency in the back end. When this is
4125 fixed, these initialization functions could all become
4126 inline, with resulting performance improvements. */
4127 tree ssdf_body;
4129 /* Set the line and file, so that it is obviously not from
4130 the source file. */
4131 input_location = locus;
4132 ssdf_body = start_static_storage_duration_function (ssdf_count);
4134 /* Make sure the back end knows about all the variables. */
4135 write_out_vars (vars);
4137 /* First generate code to do all the initializations. */
4138 if (vars)
4139 do_static_initialization_or_destruction (vars, /*initp=*/true);
4141 /* Then, generate code to do all the destructions. Do these
4142 in reverse order so that the most recently constructed
4143 variable is the first destroyed. If we're using
4144 __cxa_atexit, then we don't need to do this; functions
4145 were registered at initialization time to destroy the
4146 local statics. */
4147 if (!flag_use_cxa_atexit && vars)
4149 vars = nreverse (vars);
4150 do_static_initialization_or_destruction (vars, /*initp=*/false);
4152 else
4153 vars = NULL_TREE;
4155 /* Finish up the static storage duration function for this
4156 round. */
4157 input_location = locus;
4158 finish_static_storage_duration_function (ssdf_body);
4160 /* All those initializations and finalizations might cause
4161 us to need more inline functions, more template
4162 instantiations, etc. */
4163 reconsider = true;
4164 ssdf_count++;
4165 /* ??? was: locus.line++; */
4168 /* Now do the same for thread_local variables. */
4169 handle_tls_init ();
4171 /* Go through the set of inline functions whose bodies have not
4172 been emitted yet. If out-of-line copies of these functions
4173 are required, emit them. */
4174 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4176 /* Does it need synthesizing? */
4177 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4178 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4180 /* Even though we're already at the top-level, we push
4181 there again. That way, when we pop back a few lines
4182 hence, all of our state is restored. Otherwise,
4183 finish_function doesn't clean things up, and we end
4184 up with CURRENT_FUNCTION_DECL set. */
4185 push_to_top_level ();
4186 /* The decl's location will mark where it was first
4187 needed. Save that so synthesize method can indicate
4188 where it was needed from, in case of error */
4189 input_location = DECL_SOURCE_LOCATION (decl);
4190 synthesize_method (decl);
4191 pop_from_top_level ();
4192 reconsider = true;
4195 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4196 generate_tls_wrapper (decl);
4198 if (!DECL_SAVED_TREE (decl))
4199 continue;
4201 /* We lie to the back end, pretending that some functions
4202 are not defined when they really are. This keeps these
4203 functions from being put out unnecessarily. But, we must
4204 stop lying when the functions are referenced, or if they
4205 are not comdat since they need to be put out now. If
4206 DECL_INTERFACE_KNOWN, then we have already set
4207 DECL_EXTERNAL appropriately, so there's no need to check
4208 again, and we do not want to clear DECL_EXTERNAL if a
4209 previous call to import_export_decl set it.
4211 This is done in a separate for cycle, because if some
4212 deferred function is contained in another deferred
4213 function later in deferred_fns varray,
4214 rest_of_compilation would skip this function and we
4215 really cannot expand the same function twice. */
4216 import_export_decl (decl);
4217 if (DECL_NOT_REALLY_EXTERN (decl)
4218 && DECL_INITIAL (decl)
4219 && decl_needed_p (decl))
4221 struct cgraph_node *node, *next;
4223 node = cgraph_get_node (decl);
4224 if (node->symbol.cpp_implicit_alias)
4225 node = cgraph_alias_target (node);
4227 cgraph_for_node_and_aliases (node, clear_decl_external,
4228 NULL, true);
4229 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4230 group, we need to mark all symbols in the same comdat group
4231 that way. */
4232 if (node->symbol.same_comdat_group)
4233 for (next = cgraph (node->symbol.same_comdat_group);
4234 next != node;
4235 next = cgraph (next->symbol.same_comdat_group))
4236 cgraph_for_node_and_aliases (next, clear_decl_external,
4237 NULL, true);
4240 /* If we're going to need to write this function out, and
4241 there's already a body for it, create RTL for it now.
4242 (There might be no body if this is a method we haven't
4243 gotten around to synthesizing yet.) */
4244 if (!DECL_EXTERNAL (decl)
4245 && decl_needed_p (decl)
4246 && !TREE_ASM_WRITTEN (decl)
4247 && !cgraph_get_node (decl)->symbol.definition)
4249 /* We will output the function; no longer consider it in this
4250 loop. */
4251 DECL_DEFER_OUTPUT (decl) = 0;
4252 /* Generate RTL for this function now that we know we
4253 need it. */
4254 expand_or_defer_fn (decl);
4255 /* If we're compiling -fsyntax-only pretend that this
4256 function has been written out so that we don't try to
4257 expand it again. */
4258 if (flag_syntax_only)
4259 TREE_ASM_WRITTEN (decl) = 1;
4260 reconsider = true;
4264 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4265 reconsider = true;
4267 /* Static data members are just like namespace-scope globals. */
4268 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4270 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4271 /* Don't write it out if we haven't seen a definition. */
4272 || DECL_IN_AGGR_P (decl))
4273 continue;
4274 import_export_decl (decl);
4275 /* If this static data member is needed, provide it to the
4276 back end. */
4277 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4278 DECL_EXTERNAL (decl) = 0;
4280 if (vec_safe_length (pending_statics) != 0
4281 && wrapup_global_declarations (pending_statics->address (),
4282 pending_statics->length ()))
4283 reconsider = true;
4285 retries++;
4287 while (reconsider);
4289 /* All used inline functions must have a definition at this point. */
4290 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4292 if (/* Check online inline functions that were actually used. */
4293 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4294 /* If the definition actually was available here, then the
4295 fact that the function was not defined merely represents
4296 that for some reason (use of a template repository,
4297 #pragma interface, etc.) we decided not to emit the
4298 definition here. */
4299 && !DECL_INITIAL (decl)
4300 /* Don't complain if the template was defined. */
4301 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4302 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4303 (template_for_substitution (decl)))))
4305 warning (0, "inline function %q+D used but never defined", decl);
4306 /* Avoid a duplicate warning from check_global_declaration_1. */
4307 TREE_NO_WARNING (decl) = 1;
4311 /* So must decls that use a type with no linkage. */
4312 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4313 if (!decl_defined_p (decl))
4314 no_linkage_error (decl);
4316 /* Then, do the Objective-C stuff. This is where all the
4317 Objective-C module stuff gets generated (symtab,
4318 class/protocol/selector lists etc). This must be done after C++
4319 templates, destructors etc. so that selectors used in C++
4320 templates are properly allocated. */
4321 if (c_dialect_objc ())
4322 objc_write_global_declarations ();
4324 /* We give C linkage to static constructors and destructors. */
4325 push_lang_context (lang_name_c);
4327 /* Generate initialization and destruction functions for all
4328 priorities for which they are required. */
4329 if (priority_info_map)
4330 splay_tree_foreach (priority_info_map,
4331 generate_ctor_and_dtor_functions_for_priority,
4332 /*data=*/&locus);
4333 else if (c_dialect_objc () && objc_static_init_needed_p ())
4334 /* If this is obj-c++ and we need a static init, call
4335 generate_ctor_or_dtor_function. */
4336 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4337 DEFAULT_INIT_PRIORITY, &locus);
4339 /* We're done with the splay-tree now. */
4340 if (priority_info_map)
4341 splay_tree_delete (priority_info_map);
4343 /* Generate any missing aliases. */
4344 maybe_apply_pending_pragma_weaks ();
4346 /* We're done with static constructors, so we can go back to "C++"
4347 linkage now. */
4348 pop_lang_context ();
4350 /* Collect candidates for Java hidden aliases. */
4351 candidates = collect_candidates_for_java_method_aliases ();
4353 timevar_stop (TV_PHASE_DEFERRED);
4354 timevar_start (TV_PHASE_OPT_GEN);
4356 finalize_compilation_unit ();
4358 timevar_stop (TV_PHASE_OPT_GEN);
4359 timevar_start (TV_PHASE_CHECK_DBGINFO);
4361 /* Now, issue warnings about static, but not defined, functions,
4362 etc., and emit debugging information. */
4363 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4364 if (vec_safe_length (pending_statics) != 0)
4366 check_global_declarations (pending_statics->address (),
4367 pending_statics->length ());
4368 emit_debug_global_declarations (pending_statics->address (),
4369 pending_statics->length ());
4372 perform_deferred_noexcept_checks ();
4374 /* Generate hidden aliases for Java. */
4375 if (candidates)
4377 build_java_method_aliases (candidates);
4378 pointer_set_destroy (candidates);
4381 finish_repo ();
4383 /* The entire file is now complete. If requested, dump everything
4384 to a file. */
4386 int flags;
4387 FILE *stream = dump_begin (TDI_tu, &flags);
4389 if (stream)
4391 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4392 dump_end (TDI_tu, stream);
4396 if (flag_detailed_statistics)
4398 dump_tree_statistics ();
4399 dump_time_statistics ();
4401 input_location = locus;
4403 #ifdef ENABLE_CHECKING
4404 validate_conversion_obstack ();
4405 #endif /* ENABLE_CHECKING */
4407 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4410 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4411 function to call in parse-tree form; it has not yet been
4412 semantically analyzed. ARGS are the arguments to the function.
4413 They have already been semantically analyzed. This may change
4414 ARGS. */
4416 tree
4417 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4418 tsubst_flags_t complain)
4420 tree orig_fn;
4421 vec<tree, va_gc> *orig_args = NULL;
4422 tree expr;
4423 tree object;
4425 orig_fn = fn;
4426 object = TREE_OPERAND (fn, 0);
4428 if (processing_template_decl)
4430 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4431 || TREE_CODE (fn) == MEMBER_REF);
4432 if (type_dependent_expression_p (fn)
4433 || any_type_dependent_arguments_p (*args))
4434 return build_nt_call_vec (fn, *args);
4436 orig_args = make_tree_vector_copy (*args);
4438 /* Transform the arguments and add the implicit "this"
4439 parameter. That must be done before the FN is transformed
4440 because we depend on the form of FN. */
4441 make_args_non_dependent (*args);
4442 object = build_non_dependent_expr (object);
4443 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4445 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4446 object = cp_build_addr_expr (object, complain);
4447 vec_safe_insert (*args, 0, object);
4449 /* Now that the arguments are done, transform FN. */
4450 fn = build_non_dependent_expr (fn);
4453 /* A qualified name corresponding to a bound pointer-to-member is
4454 represented as an OFFSET_REF:
4456 struct B { void g(); };
4457 void (B::*p)();
4458 void B::g() { (this->*p)(); } */
4459 if (TREE_CODE (fn) == OFFSET_REF)
4461 tree object_addr = cp_build_addr_expr (object, complain);
4462 fn = TREE_OPERAND (fn, 1);
4463 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4464 complain);
4465 vec_safe_insert (*args, 0, object_addr);
4468 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4469 expr = build_op_call (fn, args, complain);
4470 else
4471 expr = cp_build_function_call_vec (fn, args, complain);
4472 if (processing_template_decl && expr != error_mark_node)
4473 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4475 if (orig_args != NULL)
4476 release_tree_vector (orig_args);
4478 return expr;
4482 void
4483 check_default_args (tree x)
4485 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4486 bool saw_def = false;
4487 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4488 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4490 if (TREE_PURPOSE (arg))
4491 saw_def = true;
4492 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4494 error ("default argument missing for parameter %P of %q+#D", i, x);
4495 TREE_PURPOSE (arg) = error_mark_node;
4500 /* Return true if function DECL can be inlined. This is used to force
4501 instantiation of methods that might be interesting for inlining. */
4502 bool
4503 possibly_inlined_p (tree decl)
4505 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4506 if (DECL_UNINLINABLE (decl))
4507 return false;
4508 if (!optimize || pragma_java_exceptions)
4509 return DECL_DECLARED_INLINE_P (decl);
4510 /* When optimizing, we might inline everything when flatten
4511 attribute or heuristics inlining for size or autoinlining
4512 is used. */
4513 return true;
4516 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4517 If DECL is a specialization or implicitly declared class member,
4518 generate the actual definition. Return false if something goes
4519 wrong, true otherwise. */
4521 bool
4522 mark_used (tree decl, tsubst_flags_t complain)
4524 /* If DECL is a BASELINK for a single function, then treat it just
4525 like the DECL for the function. Otherwise, if the BASELINK is
4526 for an overloaded function, we don't know which function was
4527 actually used until after overload resolution. */
4528 if (BASELINK_P (decl))
4530 decl = BASELINK_FUNCTIONS (decl);
4531 if (really_overloaded_fn (decl))
4532 return true;
4533 decl = OVL_CURRENT (decl);
4536 /* Set TREE_USED for the benefit of -Wunused. */
4537 TREE_USED (decl) = 1;
4538 if (DECL_CLONED_FUNCTION_P (decl))
4539 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4541 /* Mark enumeration types as used. */
4542 if (TREE_CODE (decl) == CONST_DECL)
4543 used_types_insert (DECL_CONTEXT (decl));
4545 if (TREE_CODE (decl) == FUNCTION_DECL
4546 && DECL_DELETED_FN (decl))
4548 if (DECL_ARTIFICIAL (decl))
4550 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4551 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4553 /* We mark a lambda conversion op as deleted if we can't
4554 generate it properly; see maybe_add_lambda_conv_op. */
4555 sorry ("converting lambda which uses %<...%> to "
4556 "function pointer");
4557 return false;
4560 if (complain & tf_error)
4562 error ("use of deleted function %qD", decl);
4563 if (!maybe_explain_implicit_delete (decl))
4564 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4566 return false;
4569 /* We can only check DECL_ODR_USED on variables or functions with
4570 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4571 might need special handling for. */
4572 if (!VAR_OR_FUNCTION_DECL_P (decl)
4573 || DECL_LANG_SPECIFIC (decl) == NULL
4574 || DECL_THUNK_P (decl))
4576 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4578 if (complain & tf_error)
4579 error ("use of %qD before deduction of %<auto%>", decl);
4580 return false;
4582 return true;
4585 /* We only want to do this processing once. We don't need to keep trying
4586 to instantiate inline templates, because unit-at-a-time will make sure
4587 we get them compiled before functions that want to inline them. */
4588 if (DECL_ODR_USED (decl))
4589 return true;
4591 /* If within finish_function, defer the rest until that function
4592 finishes, otherwise it might recurse. */
4593 if (defer_mark_used_calls)
4595 vec_safe_push (deferred_mark_used_calls, decl);
4596 return true;
4599 if (TREE_CODE (decl) == FUNCTION_DECL)
4600 maybe_instantiate_noexcept (decl);
4602 /* Normally, we can wait until instantiation-time to synthesize DECL.
4603 However, if DECL is a static data member initialized with a constant
4604 or a constexpr function, we need it right now because a reference to
4605 such a data member or a call to such function is not value-dependent.
4606 For a function that uses auto in the return type, we need to instantiate
4607 it to find out its type. */
4608 if ((decl_maybe_constant_var_p (decl)
4609 || (TREE_CODE (decl) == FUNCTION_DECL
4610 && DECL_DECLARED_CONSTEXPR_P (decl))
4611 || undeduced_auto_decl (decl))
4612 && DECL_LANG_SPECIFIC (decl)
4613 && DECL_TEMPLATE_INFO (decl)
4614 && !uses_template_parms (DECL_TI_ARGS (decl)))
4616 /* Instantiating a function will result in garbage collection. We
4617 must treat this situation as if we were within the body of a
4618 function so as to avoid collecting live data only referenced from
4619 the stack (such as overload resolution candidates). */
4620 ++function_depth;
4621 instantiate_decl (decl, /*defer_ok=*/false,
4622 /*expl_inst_class_mem_p=*/false);
4623 --function_depth;
4626 if (processing_template_decl)
4627 return true;
4629 /* Check this too in case we're within fold_non_dependent_expr. */
4630 if (DECL_TEMPLATE_INFO (decl)
4631 && uses_template_parms (DECL_TI_ARGS (decl)))
4632 return true;
4634 require_deduced_type (decl);
4636 /* If we don't need a value, then we don't need to synthesize DECL. */
4637 if (cp_unevaluated_operand != 0)
4638 return true;
4640 DECL_ODR_USED (decl) = 1;
4641 if (DECL_CLONED_FUNCTION_P (decl))
4642 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4644 /* DR 757: A type without linkage shall not be used as the type of a
4645 variable or function with linkage, unless
4646 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4647 o the variable or function is not used (3.2 [basic.def.odr]) or is
4648 defined in the same translation unit. */
4649 if (cxx_dialect > cxx98
4650 && decl_linkage (decl) != lk_none
4651 && !DECL_EXTERN_C_P (decl)
4652 && !DECL_ARTIFICIAL (decl)
4653 && !decl_defined_p (decl)
4654 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4656 if (is_local_extern (decl))
4657 /* There's no way to define a local extern, and adding it to
4658 the vector interferes with GC, so give an error now. */
4659 no_linkage_error (decl);
4660 else
4661 vec_safe_push (no_linkage_decls, decl);
4664 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4665 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4666 /* Remember it, so we can check it was defined. */
4667 note_vague_linkage_fn (decl);
4669 /* Is it a synthesized method that needs to be synthesized? */
4670 if (TREE_CODE (decl) == FUNCTION_DECL
4671 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4672 && DECL_DEFAULTED_FN (decl)
4673 /* A function defaulted outside the class is synthesized either by
4674 cp_finish_decl or instantiate_decl. */
4675 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
4676 && ! DECL_INITIAL (decl))
4678 /* Defer virtual destructors so that thunks get the right
4679 linkage. */
4680 if (DECL_VIRTUAL_P (decl) && !at_eof)
4682 note_vague_linkage_fn (decl);
4683 return true;
4686 /* Remember the current location for a function we will end up
4687 synthesizing. Then we can inform the user where it was
4688 required in the case of error. */
4689 DECL_SOURCE_LOCATION (decl) = input_location;
4691 /* Synthesizing an implicitly defined member function will result in
4692 garbage collection. We must treat this situation as if we were
4693 within the body of a function so as to avoid collecting live data
4694 on the stack (such as overload resolution candidates).
4696 We could just let cp_write_global_declarations handle synthesizing
4697 this function by adding it to deferred_fns, but doing
4698 it at the use site produces better error messages. */
4699 ++function_depth;
4700 synthesize_method (decl);
4701 --function_depth;
4702 /* If this is a synthesized method we don't need to
4703 do the instantiation test below. */
4705 else if (VAR_OR_FUNCTION_DECL_P (decl)
4706 && DECL_TEMPLATE_INFO (decl)
4707 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4708 || always_instantiate_p (decl)))
4709 /* If this is a function or variable that is an instance of some
4710 template, we now know that we will need to actually do the
4711 instantiation. We check that DECL is not an explicit
4712 instantiation because that is not checked in instantiate_decl.
4714 We put off instantiating functions in order to improve compile
4715 times. Maintaining a stack of active functions is expensive,
4716 and the inliner knows to instantiate any functions it might
4717 need. Therefore, we always try to defer instantiation. */
4719 ++function_depth;
4720 instantiate_decl (decl, /*defer_ok=*/true,
4721 /*expl_inst_class_mem_p=*/false);
4722 --function_depth;
4725 return true;
4728 bool
4729 mark_used (tree decl)
4731 return mark_used (decl, tf_warning_or_error);
4734 #include "gt-cp-decl2.h"