PR target/60811
[official-gcc.git] / gcc / cp / decl2.c
blob6c52e53bca017fef0e0f0dccb8b633aae24e7dc2
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2014 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 "stringpool.h"
35 #include "varasm.h"
36 #include "attribs.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "pointer-set.h"
40 #include "flags.h"
41 #include "cp-tree.h"
42 #include "decl.h"
43 #include "toplev.h"
44 #include "timevar.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "c-family/c-common.h"
48 #include "c-family/c-objc.h"
49 #include "cgraph.h"
50 #include "tree-inline.h"
51 #include "c-family/c-pragma.h"
52 #include "dumpfile.h"
53 #include "intl.h"
54 #include "splay-tree.h"
55 #include "langhooks.h"
56 #include "c-family/c-ada-spec.h"
57 #include "asan.h"
59 extern cpp_reader *parse_in;
61 /* This structure contains information about the initializations
62 and/or destructions required for a particular priority level. */
63 typedef struct priority_info_s {
64 /* Nonzero if there have been any initializations at this priority
65 throughout the translation unit. */
66 int initializations_p;
67 /* Nonzero if there have been any destructions at this priority
68 throughout the translation unit. */
69 int destructions_p;
70 } *priority_info;
72 static void mark_vtable_entries (tree);
73 static bool maybe_emit_vtables (tree);
74 static bool acceptable_java_type (tree);
75 static tree start_objects (int, int);
76 static void finish_objects (int, int, tree);
77 static tree start_static_storage_duration_function (unsigned);
78 static void finish_static_storage_duration_function (tree);
79 static priority_info get_priority_info (int);
80 static void do_static_initialization_or_destruction (tree, bool);
81 static void one_static_initialization_or_destruction (tree, tree, bool);
82 static void generate_ctor_or_dtor_function (bool, int, location_t *);
83 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
84 void *);
85 static tree prune_vars_needing_no_initialization (tree *);
86 static void write_out_vars (tree);
87 static void import_export_class (tree);
88 static tree get_guard_bits (tree);
89 static void determine_visibility_from_class (tree, tree);
90 static bool determine_hidden_inline (tree);
91 static bool decl_defined_p (tree);
93 /* A list of static class variables. This is needed, because a
94 static class variable can be declared inside the class without
95 an initializer, and then initialized, statically, outside the class. */
96 static GTY(()) vec<tree, va_gc> *pending_statics;
98 /* A list of functions which were declared inline, but which we
99 may need to emit outline anyway. */
100 static GTY(()) vec<tree, va_gc> *deferred_fns;
102 /* A list of decls that use types with no linkage, which we need to make
103 sure are defined. */
104 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
106 /* Nonzero if we're done parsing and into end-of-file activities. */
108 int at_eof;
111 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
112 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
113 that apply to the function). */
115 tree
116 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
117 cp_ref_qualifier rqual)
119 tree raises;
120 tree attrs;
121 int type_quals;
123 if (fntype == error_mark_node || ctype == error_mark_node)
124 return error_mark_node;
126 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
127 || TREE_CODE (fntype) == METHOD_TYPE);
129 type_quals = quals & ~TYPE_QUAL_RESTRICT;
130 ctype = cp_build_qualified_type (ctype, type_quals);
131 raises = TYPE_RAISES_EXCEPTIONS (fntype);
132 attrs = TYPE_ATTRIBUTES (fntype);
133 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
134 (TREE_CODE (fntype) == METHOD_TYPE
135 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
136 : TYPE_ARG_TYPES (fntype)));
137 if (attrs)
138 fntype = cp_build_type_attribute_variant (fntype, attrs);
139 if (rqual)
140 fntype = build_ref_qualified_type (fntype, rqual);
141 if (raises)
142 fntype = build_exception_variant (fntype, raises);
144 return fntype;
147 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
148 return type changed to NEW_RET. */
150 tree
151 change_return_type (tree new_ret, tree fntype)
153 tree newtype;
154 tree args = TYPE_ARG_TYPES (fntype);
155 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
156 tree attrs = TYPE_ATTRIBUTES (fntype);
158 if (new_ret == error_mark_node)
159 return fntype;
161 if (same_type_p (new_ret, TREE_TYPE (fntype)))
162 return fntype;
164 if (TREE_CODE (fntype) == FUNCTION_TYPE)
166 newtype = build_function_type (new_ret, args);
167 newtype = apply_memfn_quals (newtype,
168 type_memfn_quals (fntype),
169 type_memfn_rqual (fntype));
171 else
172 newtype = build_method_type_directly
173 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
174 if (raises)
175 newtype = build_exception_variant (newtype, raises);
176 if (attrs)
177 newtype = cp_build_type_attribute_variant (newtype, attrs);
179 return newtype;
182 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
183 appropriately. */
185 tree
186 cp_build_parm_decl (tree name, tree type)
188 tree parm = build_decl (input_location,
189 PARM_DECL, name, type);
190 /* DECL_ARG_TYPE is only used by the back end and the back end never
191 sees templates. */
192 if (!processing_template_decl)
193 DECL_ARG_TYPE (parm) = type_passed_as (type);
195 return parm;
198 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
199 indicated NAME. */
201 tree
202 build_artificial_parm (tree name, tree type)
204 tree parm = cp_build_parm_decl (name, type);
205 DECL_ARTIFICIAL (parm) = 1;
206 /* All our artificial parms are implicitly `const'; they cannot be
207 assigned to. */
208 TREE_READONLY (parm) = 1;
209 return parm;
212 /* Constructors for types with virtual baseclasses need an "in-charge" flag
213 saying whether this constructor is responsible for initialization of
214 virtual baseclasses or not. All destructors also need this "in-charge"
215 flag, which additionally determines whether or not the destructor should
216 free the memory for the object.
218 This function adds the "in-charge" flag to member function FN if
219 appropriate. It is called from grokclassfn and tsubst.
220 FN must be either a constructor or destructor.
222 The in-charge flag follows the 'this' parameter, and is followed by the
223 VTT parm (if any), then the user-written parms. */
225 void
226 maybe_retrofit_in_chrg (tree fn)
228 tree basetype, arg_types, parms, parm, fntype;
230 /* If we've already add the in-charge parameter don't do it again. */
231 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
232 return;
234 /* When processing templates we can't know, in general, whether or
235 not we're going to have virtual baseclasses. */
236 if (processing_template_decl)
237 return;
239 /* We don't need an in-charge parameter for constructors that don't
240 have virtual bases. */
241 if (DECL_CONSTRUCTOR_P (fn)
242 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
243 return;
245 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
246 basetype = TREE_TYPE (TREE_VALUE (arg_types));
247 arg_types = TREE_CHAIN (arg_types);
249 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
251 /* If this is a subobject constructor or destructor, our caller will
252 pass us a pointer to our VTT. */
253 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
255 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
257 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
258 DECL_CHAIN (parm) = parms;
259 parms = parm;
261 /* ...and then to TYPE_ARG_TYPES. */
262 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
264 DECL_HAS_VTT_PARM_P (fn) = 1;
267 /* Then add the in-charge parm (before the VTT parm). */
268 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
269 DECL_CHAIN (parm) = parms;
270 parms = parm;
271 arg_types = hash_tree_chain (integer_type_node, arg_types);
273 /* Insert our new parameter(s) into the list. */
274 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
276 /* And rebuild the function type. */
277 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
278 arg_types);
279 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
280 fntype = build_exception_variant (fntype,
281 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
282 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
283 fntype = (cp_build_type_attribute_variant
284 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
285 TREE_TYPE (fn) = fntype;
287 /* Now we've got the in-charge parameter. */
288 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
291 /* Classes overload their constituent function names automatically.
292 When a function name is declared in a record structure,
293 its name is changed to it overloaded name. Since names for
294 constructors and destructors can conflict, we place a leading
295 '$' for destructors.
297 CNAME is the name of the class we are grokking for.
299 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
301 FLAGS contains bits saying what's special about today's
302 arguments. DTOR_FLAG == DESTRUCTOR.
304 If FUNCTION is a destructor, then we must add the `auto-delete' field
305 as a second parameter. There is some hair associated with the fact
306 that we must "declare" this variable in the manner consistent with the
307 way the rest of the arguments were declared.
309 QUALS are the qualifiers for the this pointer. */
311 void
312 grokclassfn (tree ctype, tree function, enum overload_flags flags)
314 tree fn_name = DECL_NAME (function);
316 /* Even within an `extern "C"' block, members get C++ linkage. See
317 [dcl.link] for details. */
318 SET_DECL_LANGUAGE (function, lang_cplusplus);
320 if (fn_name == NULL_TREE)
322 error ("name missing for member function");
323 fn_name = get_identifier ("<anonymous>");
324 DECL_NAME (function) = fn_name;
327 DECL_CONTEXT (function) = ctype;
329 if (flags == DTOR_FLAG)
330 DECL_DESTRUCTOR_P (function) = 1;
332 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
333 maybe_retrofit_in_chrg (function);
336 /* Create an ARRAY_REF, checking for the user doing things backwards
337 along the way. DECLTYPE_P is for N3276, as in the parser. */
339 tree
340 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
341 bool decltype_p)
343 tree type;
344 tree expr;
345 tree orig_array_expr = array_expr;
346 tree orig_index_exp = index_exp;
348 if (error_operand_p (array_expr) || error_operand_p (index_exp))
349 return error_mark_node;
351 if (processing_template_decl)
353 if (type_dependent_expression_p (array_expr)
354 || type_dependent_expression_p (index_exp))
355 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
356 NULL_TREE, NULL_TREE);
357 array_expr = build_non_dependent_expr (array_expr);
358 index_exp = build_non_dependent_expr (index_exp);
361 type = TREE_TYPE (array_expr);
362 gcc_assert (type);
363 type = non_reference (type);
365 /* If they have an `operator[]', use that. */
366 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
368 tsubst_flags_t complain = tf_warning_or_error;
369 if (decltype_p)
370 complain |= tf_decltype;
371 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
372 index_exp, NULL_TREE, /*overload=*/NULL, complain);
374 else
376 tree p1, p2, i1, i2;
378 /* Otherwise, create an ARRAY_REF for a pointer or array type.
379 It is a little-known fact that, if `a' is an array and `i' is
380 an int, you can write `i[a]', which means the same thing as
381 `a[i]'. */
382 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
383 p1 = array_expr;
384 else
385 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
387 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
388 p2 = index_exp;
389 else
390 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
392 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
393 false);
394 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
395 false);
397 if ((p1 && i2) && (i1 && p2))
398 error ("ambiguous conversion for array subscript");
400 if (p1 && i2)
401 array_expr = p1, index_exp = i2;
402 else if (i1 && p2)
403 array_expr = p2, index_exp = i1;
404 else
406 error ("invalid types %<%T[%T]%> for array subscript",
407 type, TREE_TYPE (index_exp));
408 return error_mark_node;
411 if (array_expr == error_mark_node || index_exp == error_mark_node)
412 error ("ambiguous conversion for array subscript");
414 expr = build_array_ref (input_location, array_expr, index_exp);
416 if (processing_template_decl && expr != error_mark_node)
417 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
418 NULL_TREE, NULL_TREE);
419 return expr;
422 /* Given the cast expression EXP, checking out its validity. Either return
423 an error_mark_node if there was an unavoidable error, return a cast to
424 void for trying to delete a pointer w/ the value 0, or return the
425 call to delete. If DOING_VEC is true, we handle things differently
426 for doing an array delete.
427 Implements ARM $5.3.4. This is called from the parser. */
429 tree
430 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
431 tsubst_flags_t complain)
433 tree t, type;
435 if (exp == error_mark_node)
436 return exp;
438 if (processing_template_decl)
440 t = build_min (DELETE_EXPR, void_type_node, exp, size);
441 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
442 DELETE_EXPR_USE_VEC (t) = doing_vec;
443 TREE_SIDE_EFFECTS (t) = 1;
444 return t;
447 /* An array can't have been allocated by new, so complain. */
448 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
449 warning (0, "deleting array %q#E", exp);
451 t = build_expr_type_conversion (WANT_POINTER, exp, true);
453 if (t == NULL_TREE || t == error_mark_node)
455 error ("type %q#T argument given to %<delete%>, expected pointer",
456 TREE_TYPE (exp));
457 return error_mark_node;
460 type = TREE_TYPE (t);
462 /* As of Valley Forge, you can delete a pointer to const. */
464 /* You can't delete functions. */
465 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
467 error ("cannot delete a function. Only pointer-to-objects are "
468 "valid arguments to %<delete%>");
469 return error_mark_node;
472 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
473 if (VOID_TYPE_P (TREE_TYPE (type)))
475 warning (0, "deleting %qT is undefined", type);
476 doing_vec = 0;
479 /* Deleting a pointer with the value zero is valid and has no effect. */
480 if (integer_zerop (t))
481 return build1 (NOP_EXPR, void_type_node, t);
483 if (doing_vec)
484 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
485 sfk_deleting_destructor,
486 use_global_delete, complain);
487 else
488 return build_delete (type, t, sfk_deleting_destructor,
489 LOOKUP_NORMAL, use_global_delete,
490 complain);
493 /* Report an error if the indicated template declaration is not the
494 sort of thing that should be a member template. */
496 void
497 check_member_template (tree tmpl)
499 tree decl;
501 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
502 decl = DECL_TEMPLATE_RESULT (tmpl);
504 if (TREE_CODE (decl) == FUNCTION_DECL
505 || DECL_ALIAS_TEMPLATE_P (tmpl)
506 || (TREE_CODE (decl) == TYPE_DECL
507 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
509 /* The parser rejects template declarations in local classes
510 (with the exception of generic lambdas). */
511 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (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);
805 else
806 for (tree t = current_class_type; TYPE_P (t);
807 t = CP_TYPE_CONTEXT (t))
808 if (TYPE_ANONYMOUS_P (t))
810 if (permerror (DECL_SOURCE_LOCATION (decl),
811 "static data member %qD in unnamed class", decl))
812 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
813 "unnamed class defined here");
814 break;
817 DECL_IN_AGGR_P (decl) = 1;
819 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
820 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
821 SET_VAR_HAD_UNKNOWN_BOUND (decl);
823 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
826 /* DECLARATOR and DECLSPECS correspond to a class member. The other
827 parameters are as for cp_finish_decl. Return the DECL for the
828 class member declared. */
830 tree
831 grokfield (const cp_declarator *declarator,
832 cp_decl_specifier_seq *declspecs,
833 tree init, bool init_const_expr_p,
834 tree asmspec_tree,
835 tree attrlist)
837 tree value;
838 const char *asmspec = 0;
839 int flags;
840 tree name;
842 if (init
843 && TREE_CODE (init) == TREE_LIST
844 && TREE_VALUE (init) == error_mark_node
845 && TREE_CHAIN (init) == NULL_TREE)
846 init = NULL_TREE;
848 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
849 if (! value || value == error_mark_node)
850 /* friend or constructor went bad. */
851 return error_mark_node;
852 if (TREE_TYPE (value) == error_mark_node)
853 return value;
855 if (TREE_CODE (value) == TYPE_DECL && init)
857 error ("typedef %qD is initialized (use decltype instead)", value);
858 init = NULL_TREE;
861 /* Pass friendly classes back. */
862 if (value == void_type_node)
863 return value;
865 /* Pass friend decls back. */
866 if ((TREE_CODE (value) == FUNCTION_DECL
867 || TREE_CODE (value) == TEMPLATE_DECL)
868 && DECL_CONTEXT (value) != current_class_type)
869 return value;
871 name = DECL_NAME (value);
873 if (name != NULL_TREE)
875 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
877 error ("explicit template argument list not allowed");
878 return error_mark_node;
881 if (IDENTIFIER_POINTER (name)[0] == '_'
882 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
883 error ("member %qD conflicts with virtual function table field name",
884 value);
887 /* Stash away type declarations. */
888 if (TREE_CODE (value) == TYPE_DECL)
890 DECL_NONLOCAL (value) = 1;
891 DECL_CONTEXT (value) = current_class_type;
893 if (attrlist)
895 int attrflags = 0;
897 /* If this is a typedef that names the class for linkage purposes
898 (7.1.3p8), apply any attributes directly to the type. */
899 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
900 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
901 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
903 cplus_decl_attributes (&value, attrlist, attrflags);
906 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
907 && TREE_TYPE (value) != error_mark_node
908 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
909 set_underlying_type (value);
911 /* It's important that push_template_decl below follows
912 set_underlying_type above so that the created template
913 carries the properly set type of VALUE. */
914 if (processing_template_decl)
915 value = push_template_decl (value);
917 record_locally_defined_typedef (value);
918 return value;
921 if (DECL_IN_AGGR_P (value))
923 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
924 return void_type_node;
927 if (asmspec_tree && asmspec_tree != error_mark_node)
928 asmspec = TREE_STRING_POINTER (asmspec_tree);
930 if (init)
932 if (TREE_CODE (value) == FUNCTION_DECL)
934 /* Initializers for functions are rejected early in the parser.
935 If we get here, it must be a pure specifier for a method. */
936 if (init == ridpointers[(int)RID_DELETE])
938 DECL_DELETED_FN (value) = 1;
939 DECL_DECLARED_INLINE_P (value) = 1;
940 DECL_INITIAL (value) = error_mark_node;
942 else if (init == ridpointers[(int)RID_DEFAULT])
944 if (defaultable_fn_check (value))
946 DECL_DEFAULTED_FN (value) = 1;
947 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
948 DECL_DECLARED_INLINE_P (value) = 1;
951 else if (TREE_CODE (init) == DEFAULT_ARG)
952 error ("invalid initializer for member function %qD", value);
953 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
955 if (integer_zerop (init))
956 DECL_PURE_VIRTUAL_P (value) = 1;
957 else if (error_operand_p (init))
958 ; /* An error has already been reported. */
959 else
960 error ("invalid initializer for member function %qD",
961 value);
963 else
965 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
966 error ("initializer specified for static member function %qD",
967 value);
970 else if (TREE_CODE (value) == FIELD_DECL)
971 /* C++11 NSDMI, keep going. */;
972 else if (!VAR_P (value))
973 gcc_unreachable ();
976 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
978 value = push_template_decl (value);
979 if (error_operand_p (value))
980 return error_mark_node;
983 if (attrlist)
984 cplus_decl_attributes (&value, attrlist, 0);
986 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
987 && CONSTRUCTOR_IS_DIRECT_INIT (init))
988 flags = LOOKUP_NORMAL;
989 else
990 flags = LOOKUP_IMPLICIT;
992 switch (TREE_CODE (value))
994 case VAR_DECL:
995 finish_static_data_member_decl (value, init, init_const_expr_p,
996 asmspec_tree, flags);
997 return value;
999 case FIELD_DECL:
1000 if (asmspec)
1001 error ("%<asm%> specifiers are not permitted on non-static data members");
1002 if (DECL_INITIAL (value) == error_mark_node)
1003 init = error_mark_node;
1004 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1005 NULL_TREE, flags);
1006 DECL_IN_AGGR_P (value) = 1;
1007 return value;
1009 case FUNCTION_DECL:
1010 if (asmspec)
1011 set_user_assembler_name (value, asmspec);
1013 cp_finish_decl (value,
1014 /*init=*/NULL_TREE,
1015 /*init_const_expr_p=*/false,
1016 asmspec_tree, flags);
1018 /* Pass friends back this way. */
1019 if (DECL_FRIEND_P (value))
1020 return void_type_node;
1022 DECL_IN_AGGR_P (value) = 1;
1023 return value;
1025 default:
1026 gcc_unreachable ();
1028 return NULL_TREE;
1031 /* Like `grokfield', but for bitfields.
1032 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1034 tree
1035 grokbitfield (const cp_declarator *declarator,
1036 cp_decl_specifier_seq *declspecs, tree width,
1037 tree attrlist)
1039 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1041 if (value == error_mark_node)
1042 return NULL_TREE; /* friends went bad. */
1043 if (TREE_TYPE (value) == error_mark_node)
1044 return value;
1046 /* Pass friendly classes back. */
1047 if (VOID_TYPE_P (value))
1048 return void_type_node;
1050 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1051 && (POINTER_TYPE_P (value)
1052 || !dependent_type_p (TREE_TYPE (value))))
1054 error ("bit-field %qD with non-integral type", value);
1055 return error_mark_node;
1058 if (TREE_CODE (value) == TYPE_DECL)
1060 error ("cannot declare %qD to be a bit-field type", value);
1061 return NULL_TREE;
1064 /* Usually, finish_struct_1 catches bitfields with invalid types.
1065 But, in the case of bitfields with function type, we confuse
1066 ourselves into thinking they are member functions, so we must
1067 check here. */
1068 if (TREE_CODE (value) == FUNCTION_DECL)
1070 error ("cannot declare bit-field %qD with function type",
1071 DECL_NAME (value));
1072 return NULL_TREE;
1075 if (DECL_IN_AGGR_P (value))
1077 error ("%qD is already defined in the class %qT", value,
1078 DECL_CONTEXT (value));
1079 return void_type_node;
1082 if (TREE_STATIC (value))
1084 error ("static member %qD cannot be a bit-field", value);
1085 return NULL_TREE;
1087 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1089 if (width != error_mark_node)
1091 /* The width must be an integer type. */
1092 if (!type_dependent_expression_p (width)
1093 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1094 error ("width of bit-field %qD has non-integral type %qT", value,
1095 TREE_TYPE (width));
1096 DECL_INITIAL (value) = width;
1097 SET_DECL_C_BIT_FIELD (value);
1100 DECL_IN_AGGR_P (value) = 1;
1102 if (attrlist)
1103 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1105 return value;
1109 /* Returns true iff ATTR is an attribute which needs to be applied at
1110 instantiation time rather than template definition time. */
1112 static bool
1113 is_late_template_attribute (tree attr, tree decl)
1115 tree name = get_attribute_name (attr);
1116 tree args = TREE_VALUE (attr);
1117 const struct attribute_spec *spec = lookup_attribute_spec (name);
1118 tree arg;
1120 if (!spec)
1121 /* Unknown attribute. */
1122 return false;
1124 /* Attribute weak handling wants to write out assembly right away. */
1125 if (is_attribute_p ("weak", name))
1126 return true;
1128 /* Attribute unused is applied directly, as it appertains to
1129 decls. */
1130 if (is_attribute_p ("unused", name))
1131 return false;
1133 /* #pragma omp declare simd attribute needs to be always deferred. */
1134 if (flag_openmp
1135 && is_attribute_p ("omp declare simd", name))
1136 return true;
1138 /* If any of the arguments are dependent expressions, we can't evaluate
1139 the attribute until instantiation time. */
1140 for (arg = args; arg; arg = TREE_CHAIN (arg))
1142 tree t = TREE_VALUE (arg);
1144 /* If the first attribute argument is an identifier, only consider
1145 second and following arguments. Attributes like mode, format,
1146 cleanup and several target specific attributes aren't late
1147 just because they have an IDENTIFIER_NODE as first argument. */
1148 if (arg == args && identifier_p (t))
1149 continue;
1151 if (value_dependent_expression_p (t)
1152 || type_dependent_expression_p (t))
1153 return true;
1156 if (TREE_CODE (decl) == TYPE_DECL
1157 || TYPE_P (decl)
1158 || spec->type_required)
1160 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1162 /* We can't apply any attributes to a completely unknown type until
1163 instantiation time. */
1164 enum tree_code code = TREE_CODE (type);
1165 if (code == TEMPLATE_TYPE_PARM
1166 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1167 || code == TYPENAME_TYPE)
1168 return true;
1169 /* Also defer most attributes on dependent types. This is not
1170 necessary in all cases, but is the better default. */
1171 else if (dependent_type_p (type)
1172 /* But attributes abi_tag and visibility specifically apply
1173 to templates. */
1174 && !is_attribute_p ("abi_tag", name)
1175 && !is_attribute_p ("visibility", name))
1176 return true;
1177 else
1178 return false;
1180 else
1181 return false;
1184 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1185 applied at instantiation time and return them. If IS_DEPENDENT is true,
1186 the declaration itself is dependent, so all attributes should be applied
1187 at instantiation time. */
1189 static tree
1190 splice_template_attributes (tree *attr_p, tree decl)
1192 tree *p = attr_p;
1193 tree late_attrs = NULL_TREE;
1194 tree *q = &late_attrs;
1196 if (!p)
1197 return NULL_TREE;
1199 for (; *p; )
1201 if (is_late_template_attribute (*p, decl))
1203 ATTR_IS_DEPENDENT (*p) = 1;
1204 *q = *p;
1205 *p = TREE_CHAIN (*p);
1206 q = &TREE_CHAIN (*q);
1207 *q = NULL_TREE;
1209 else
1210 p = &TREE_CHAIN (*p);
1213 return late_attrs;
1216 /* Remove any late attributes from the list in ATTR_P and attach them to
1217 DECL_P. */
1219 static void
1220 save_template_attributes (tree *attr_p, tree *decl_p)
1222 tree *q;
1224 if (attr_p && *attr_p == error_mark_node)
1225 return;
1227 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1228 if (!late_attrs)
1229 return;
1231 if (DECL_P (*decl_p))
1232 q = &DECL_ATTRIBUTES (*decl_p);
1233 else
1234 q = &TYPE_ATTRIBUTES (*decl_p);
1236 tree old_attrs = *q;
1238 /* Merge the late attributes at the beginning with the attribute
1239 list. */
1240 late_attrs = merge_attributes (late_attrs, *q);
1241 *q = late_attrs;
1243 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1245 /* We've added new attributes directly to the main variant, so
1246 now we need to update all of the other variants to include
1247 these new attributes. */
1248 tree variant;
1249 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1250 variant = TYPE_NEXT_VARIANT (variant))
1252 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1253 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1258 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1259 to a typedef which gives a previously anonymous class or enum a name for
1260 linkage purposes. */
1262 bool
1263 attributes_naming_typedef_ok (tree attrs)
1265 for (; attrs; attrs = TREE_CHAIN (attrs))
1267 tree name = get_attribute_name (attrs);
1268 if (is_attribute_p ("vector_size", name))
1269 return false;
1271 return true;
1274 /* Like reconstruct_complex_type, but handle also template trees. */
1276 tree
1277 cp_reconstruct_complex_type (tree type, tree bottom)
1279 tree inner, outer;
1281 if (TYPE_PTR_P (type))
1283 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1284 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1285 TYPE_REF_CAN_ALIAS_ALL (type));
1287 else if (TREE_CODE (type) == REFERENCE_TYPE)
1289 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1290 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1291 TYPE_REF_CAN_ALIAS_ALL (type));
1293 else if (TREE_CODE (type) == ARRAY_TYPE)
1295 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1296 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1297 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1298 element type qualification will be handled by the recursive
1299 cp_reconstruct_complex_type call and cp_build_qualified_type
1300 for ARRAY_TYPEs changes the element type. */
1301 return outer;
1303 else if (TREE_CODE (type) == FUNCTION_TYPE)
1305 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1306 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1307 outer = apply_memfn_quals (outer,
1308 type_memfn_quals (type),
1309 type_memfn_rqual (type));
1311 else if (TREE_CODE (type) == METHOD_TYPE)
1313 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1314 /* The build_method_type_directly() routine prepends 'this' to argument list,
1315 so we must compensate by getting rid of it. */
1316 outer
1317 = build_method_type_directly
1318 (class_of_this_parm (type), inner,
1319 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1321 else if (TREE_CODE (type) == OFFSET_TYPE)
1323 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1324 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1326 else
1327 return bottom;
1329 if (TYPE_ATTRIBUTES (type))
1330 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1331 return cp_build_qualified_type (outer, cp_type_quals (type));
1334 /* Replaces any constexpr expression that may be into the attributes
1335 arguments with their reduced value. */
1337 static void
1338 cp_check_const_attributes (tree attributes)
1340 if (attributes == error_mark_node)
1341 return;
1343 tree attr;
1344 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1346 tree arg;
1347 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1349 tree expr = TREE_VALUE (arg);
1350 if (EXPR_P (expr))
1351 TREE_VALUE (arg) = maybe_constant_value (expr);
1356 /* Return true if TYPE is an OpenMP mappable type. */
1357 bool
1358 cp_omp_mappable_type (tree type)
1360 /* Mappable type has to be complete. */
1361 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1362 return false;
1363 /* Arrays have mappable type if the elements have mappable type. */
1364 while (TREE_CODE (type) == ARRAY_TYPE)
1365 type = TREE_TYPE (type);
1366 /* A mappable type cannot contain virtual members. */
1367 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1368 return false;
1369 /* All data members must be non-static. */
1370 if (CLASS_TYPE_P (type))
1372 tree field;
1373 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1374 if (TREE_CODE (field) == VAR_DECL)
1375 return false;
1376 /* All fields must have mappable types. */
1377 else if (TREE_CODE (field) == FIELD_DECL
1378 && !cp_omp_mappable_type (TREE_TYPE (field)))
1379 return false;
1381 return true;
1384 /* Like decl_attributes, but handle C++ complexity. */
1386 void
1387 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1389 if (*decl == NULL_TREE || *decl == void_type_node
1390 || *decl == error_mark_node)
1391 return;
1393 /* Add implicit "omp declare target" attribute if requested. */
1394 if (scope_chain->omp_declare_target_attribute
1395 && ((TREE_CODE (*decl) == VAR_DECL && TREE_STATIC (*decl))
1396 || TREE_CODE (*decl) == FUNCTION_DECL))
1398 if (TREE_CODE (*decl) == VAR_DECL
1399 && DECL_CLASS_SCOPE_P (*decl))
1400 error ("%q+D static data member inside of declare target directive",
1401 *decl);
1402 else if (TREE_CODE (*decl) == VAR_DECL
1403 && (DECL_FUNCTION_SCOPE_P (*decl)
1404 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1405 error ("%q+D in block scope inside of declare target directive",
1406 *decl);
1407 else if (!processing_template_decl
1408 && TREE_CODE (*decl) == VAR_DECL
1409 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1410 error ("%q+D in declare target directive does not have mappable type",
1411 *decl);
1412 else
1413 attributes = tree_cons (get_identifier ("omp declare target"),
1414 NULL_TREE, attributes);
1417 if (processing_template_decl)
1419 if (check_for_bare_parameter_packs (attributes))
1420 return;
1422 save_template_attributes (&attributes, decl);
1425 cp_check_const_attributes (attributes);
1427 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1428 decl = &DECL_TEMPLATE_RESULT (*decl);
1430 decl_attributes (decl, attributes, flags);
1432 if (TREE_CODE (*decl) == TYPE_DECL)
1433 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1436 /* Walks through the namespace- or function-scope anonymous union
1437 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1438 Returns one of the fields for use in the mangled name. */
1440 static tree
1441 build_anon_union_vars (tree type, tree object)
1443 tree main_decl = NULL_TREE;
1444 tree field;
1446 /* Rather than write the code to handle the non-union case,
1447 just give an error. */
1448 if (TREE_CODE (type) != UNION_TYPE)
1450 error ("anonymous struct not inside named type");
1451 return error_mark_node;
1454 for (field = TYPE_FIELDS (type);
1455 field != NULL_TREE;
1456 field = DECL_CHAIN (field))
1458 tree decl;
1459 tree ref;
1461 if (DECL_ARTIFICIAL (field))
1462 continue;
1463 if (TREE_CODE (field) != FIELD_DECL)
1465 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1466 "have non-static data members", field);
1467 continue;
1470 if (TREE_PRIVATE (field))
1471 permerror (input_location, "private member %q+#D in anonymous union", field);
1472 else if (TREE_PROTECTED (field))
1473 permerror (input_location, "protected member %q+#D in anonymous union", field);
1475 if (processing_template_decl)
1476 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1477 DECL_NAME (field), NULL_TREE);
1478 else
1479 ref = build_class_member_access_expr (object, field, NULL_TREE,
1480 false, tf_warning_or_error);
1482 if (DECL_NAME (field))
1484 tree base;
1486 decl = build_decl (input_location,
1487 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1488 DECL_ANON_UNION_VAR_P (decl) = 1;
1489 DECL_ARTIFICIAL (decl) = 1;
1491 base = get_base_address (object);
1492 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1493 TREE_STATIC (decl) = TREE_STATIC (base);
1494 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1496 SET_DECL_VALUE_EXPR (decl, ref);
1497 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1499 decl = pushdecl (decl);
1501 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1502 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1503 else
1504 decl = 0;
1506 if (main_decl == NULL_TREE)
1507 main_decl = decl;
1510 return main_decl;
1513 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1514 anonymous union, then all members must be laid out together. PUBLIC_P
1515 is nonzero if this union is not declared static. */
1517 void
1518 finish_anon_union (tree anon_union_decl)
1520 tree type;
1521 tree main_decl;
1522 bool public_p;
1524 if (anon_union_decl == error_mark_node)
1525 return;
1527 type = TREE_TYPE (anon_union_decl);
1528 public_p = TREE_PUBLIC (anon_union_decl);
1530 /* The VAR_DECL's context is the same as the TYPE's context. */
1531 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1533 if (TYPE_FIELDS (type) == NULL_TREE)
1534 return;
1536 if (public_p)
1538 error ("namespace-scope anonymous aggregates must be static");
1539 return;
1542 main_decl = build_anon_union_vars (type, anon_union_decl);
1543 if (main_decl == error_mark_node)
1544 return;
1545 if (main_decl == NULL_TREE)
1547 warning (0, "anonymous union with no members");
1548 return;
1551 if (!processing_template_decl)
1553 /* Use main_decl to set the mangled name. */
1554 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1555 maybe_commonize_var (anon_union_decl);
1556 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1557 mangle_decl (anon_union_decl);
1558 DECL_NAME (anon_union_decl) = NULL_TREE;
1561 pushdecl (anon_union_decl);
1562 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1565 /* Auxiliary functions to make type signatures for
1566 `operator new' and `operator delete' correspond to
1567 what compiler will be expecting. */
1569 tree
1570 coerce_new_type (tree type)
1572 int e = 0;
1573 tree args = TYPE_ARG_TYPES (type);
1575 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1577 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1579 e = 1;
1580 error ("%<operator new%> must return type %qT", ptr_type_node);
1583 if (args && args != void_list_node)
1585 if (TREE_PURPOSE (args))
1587 /* [basic.stc.dynamic.allocation]
1589 The first parameter shall not have an associated default
1590 argument. */
1591 error ("the first parameter of %<operator new%> cannot "
1592 "have a default argument");
1593 /* Throw away the default argument. */
1594 TREE_PURPOSE (args) = NULL_TREE;
1597 if (!same_type_p (TREE_VALUE (args), size_type_node))
1599 e = 2;
1600 args = TREE_CHAIN (args);
1603 else
1604 e = 2;
1606 if (e == 2)
1607 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1608 "as first parameter", size_type_node);
1610 switch (e)
1612 case 2:
1613 args = tree_cons (NULL_TREE, size_type_node, args);
1614 /* Fall through. */
1615 case 1:
1616 type = build_exception_variant
1617 (build_function_type (ptr_type_node, args),
1618 TYPE_RAISES_EXCEPTIONS (type));
1619 /* Fall through. */
1620 default:;
1622 return type;
1625 tree
1626 coerce_delete_type (tree type)
1628 int e = 0;
1629 tree args = TYPE_ARG_TYPES (type);
1631 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1633 if (!same_type_p (TREE_TYPE (type), void_type_node))
1635 e = 1;
1636 error ("%<operator delete%> must return type %qT", void_type_node);
1639 if (!args || args == void_list_node
1640 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1642 e = 2;
1643 if (args && args != void_list_node)
1644 args = TREE_CHAIN (args);
1645 error ("%<operator delete%> takes type %qT as first parameter",
1646 ptr_type_node);
1648 switch (e)
1650 case 2:
1651 args = tree_cons (NULL_TREE, ptr_type_node, args);
1652 /* Fall through. */
1653 case 1:
1654 type = build_exception_variant
1655 (build_function_type (void_type_node, args),
1656 TYPE_RAISES_EXCEPTIONS (type));
1657 /* Fall through. */
1658 default:;
1661 return type;
1664 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1665 and mark them as needed. */
1667 static void
1668 mark_vtable_entries (tree decl)
1670 tree fnaddr;
1671 unsigned HOST_WIDE_INT idx;
1673 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1674 idx, fnaddr)
1676 tree fn;
1678 STRIP_NOPS (fnaddr);
1680 if (TREE_CODE (fnaddr) != ADDR_EXPR
1681 && TREE_CODE (fnaddr) != FDESC_EXPR)
1682 /* This entry is an offset: a virtual base class offset, a
1683 virtual call offset, an RTTI offset, etc. */
1684 continue;
1686 fn = TREE_OPERAND (fnaddr, 0);
1687 TREE_ADDRESSABLE (fn) = 1;
1688 /* When we don't have vcall offsets, we output thunks whenever
1689 we output the vtables that contain them. With vcall offsets,
1690 we know all the thunks we'll need when we emit a virtual
1691 function, so we emit the thunks there instead. */
1692 if (DECL_THUNK_P (fn))
1693 use_thunk (fn, /*emit_p=*/0);
1694 mark_used (fn);
1698 /* Set DECL up to have the closest approximation of "initialized common"
1699 linkage available. */
1701 void
1702 comdat_linkage (tree decl)
1704 if (flag_weak)
1705 make_decl_one_only (decl, cxx_comdat_group (decl));
1706 else if (TREE_CODE (decl) == FUNCTION_DECL
1707 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1708 /* We can just emit function and compiler-generated variables
1709 statically; having multiple copies is (for the most part) only
1710 a waste of space.
1712 There are two correctness issues, however: the address of a
1713 template instantiation with external linkage should be the
1714 same, independent of what translation unit asks for the
1715 address, and this will not hold when we emit multiple copies of
1716 the function. However, there's little else we can do.
1718 Also, by default, the typeinfo implementation assumes that
1719 there will be only one copy of the string used as the name for
1720 each type. Therefore, if weak symbols are unavailable, the
1721 run-time library should perform a more conservative check; it
1722 should perform a string comparison, rather than an address
1723 comparison. */
1724 TREE_PUBLIC (decl) = 0;
1725 else
1727 /* Static data member template instantiations, however, cannot
1728 have multiple copies. */
1729 if (DECL_INITIAL (decl) == 0
1730 || DECL_INITIAL (decl) == error_mark_node)
1731 DECL_COMMON (decl) = 1;
1732 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1734 DECL_COMMON (decl) = 1;
1735 DECL_INITIAL (decl) = error_mark_node;
1737 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1739 /* We can't do anything useful; leave vars for explicit
1740 instantiation. */
1741 DECL_EXTERNAL (decl) = 1;
1742 DECL_NOT_REALLY_EXTERN (decl) = 0;
1746 DECL_COMDAT (decl) = 1;
1749 /* For win32 we also want to put explicit instantiations in
1750 linkonce sections, so that they will be merged with implicit
1751 instantiations; otherwise we get duplicate symbol errors.
1752 For Darwin we do not want explicit instantiations to be
1753 linkonce. */
1755 void
1756 maybe_make_one_only (tree decl)
1758 /* We used to say that this was not necessary on targets that support weak
1759 symbols, because the implicit instantiations will defer to the explicit
1760 one. However, that's not actually the case in SVR4; a strong definition
1761 after a weak one is an error. Also, not making explicit
1762 instantiations one_only means that we can end up with two copies of
1763 some template instantiations. */
1764 if (! flag_weak)
1765 return;
1767 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1768 we can get away with not emitting them if they aren't used. We need
1769 to for variables so that cp_finish_decl will update their linkage,
1770 because their DECL_INITIAL may not have been set properly yet. */
1772 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1773 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1774 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1776 make_decl_one_only (decl, cxx_comdat_group (decl));
1778 if (VAR_P (decl))
1780 varpool_node *node = varpool_node_for_decl (decl);
1781 DECL_COMDAT (decl) = 1;
1782 /* Mark it needed so we don't forget to emit it. */
1783 node->forced_by_abi = true;
1784 TREE_USED (decl) = 1;
1789 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1790 This predicate will give the right answer during parsing of the
1791 function, which other tests may not. */
1793 bool
1794 vague_linkage_p (tree decl)
1796 /* Unfortunately, import_export_decl has not always been called
1797 before the function is processed, so we cannot simply check
1798 DECL_COMDAT. */
1799 return (DECL_COMDAT (decl)
1800 || (((TREE_CODE (decl) == FUNCTION_DECL
1801 && DECL_DECLARED_INLINE_P (decl))
1802 || (DECL_LANG_SPECIFIC (decl)
1803 && DECL_TEMPLATE_INSTANTIATION (decl)))
1804 && TREE_PUBLIC (decl)));
1807 /* Determine whether or not we want to specifically import or export CTYPE,
1808 using various heuristics. */
1810 static void
1811 import_export_class (tree ctype)
1813 /* -1 for imported, 1 for exported. */
1814 int import_export = 0;
1816 /* It only makes sense to call this function at EOF. The reason is
1817 that this function looks at whether or not the first non-inline
1818 non-abstract virtual member function has been defined in this
1819 translation unit. But, we can't possibly know that until we've
1820 seen the entire translation unit. */
1821 gcc_assert (at_eof);
1823 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1824 return;
1826 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1827 we will have CLASSTYPE_INTERFACE_ONLY set but not
1828 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1829 heuristic because someone will supply a #pragma implementation
1830 elsewhere, and deducing it here would produce a conflict. */
1831 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1832 return;
1834 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1835 import_export = -1;
1836 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1837 import_export = 1;
1838 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1839 && !flag_implicit_templates)
1840 /* For a template class, without -fimplicit-templates, check the
1841 repository. If the virtual table is assigned to this
1842 translation unit, then export the class; otherwise, import
1843 it. */
1844 import_export = repo_export_class_p (ctype) ? 1 : -1;
1845 else if (TYPE_POLYMORPHIC_P (ctype))
1847 /* The ABI specifies that the virtual table and associated
1848 information are emitted with the key method, if any. */
1849 tree method = CLASSTYPE_KEY_METHOD (ctype);
1850 /* If weak symbol support is not available, then we must be
1851 careful not to emit the vtable when the key function is
1852 inline. An inline function can be defined in multiple
1853 translation units. If we were to emit the vtable in each
1854 translation unit containing a definition, we would get
1855 multiple definition errors at link-time. */
1856 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1857 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1860 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1861 a definition anywhere else. */
1862 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1863 import_export = 0;
1865 /* Allow back ends the chance to overrule the decision. */
1866 if (targetm.cxx.import_export_class)
1867 import_export = targetm.cxx.import_export_class (ctype, import_export);
1869 if (import_export)
1871 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1872 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1876 /* Return true if VAR has already been provided to the back end; in that
1877 case VAR should not be modified further by the front end. */
1878 static bool
1879 var_finalized_p (tree var)
1881 return varpool_node_for_decl (var)->definition;
1884 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1885 must be emitted in this translation unit. Mark it as such. */
1887 void
1888 mark_needed (tree decl)
1890 TREE_USED (decl) = 1;
1891 if (TREE_CODE (decl) == FUNCTION_DECL)
1893 /* Extern inline functions don't become needed when referenced.
1894 If we know a method will be emitted in other TU and no new
1895 functions can be marked reachable, just use the external
1896 definition. */
1897 struct cgraph_node *node = cgraph_get_create_node (decl);
1898 node->forced_by_abi = true;
1900 else if (TREE_CODE (decl) == VAR_DECL)
1902 varpool_node *node = varpool_node_for_decl (decl);
1903 /* C++ frontend use mark_decl_references to force COMDAT variables
1904 to be output that might appear dead otherwise. */
1905 node->forced_by_abi = true;
1909 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1910 returns true if a definition of this entity should be provided in
1911 this object file. Callers use this function to determine whether
1912 or not to let the back end know that a definition of DECL is
1913 available in this translation unit. */
1915 bool
1916 decl_needed_p (tree decl)
1918 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1919 /* This function should only be called at the end of the translation
1920 unit. We cannot be sure of whether or not something will be
1921 COMDAT until that point. */
1922 gcc_assert (at_eof);
1924 /* All entities with external linkage that are not COMDAT should be
1925 emitted; they may be referred to from other object files. */
1926 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1927 return true;
1928 /* If this entity was used, let the back end see it; it will decide
1929 whether or not to emit it into the object file. */
1930 if (TREE_USED (decl))
1931 return true;
1932 /* Functions marked "dllexport" must be emitted so that they are
1933 visible to other DLLs. */
1934 if (flag_keep_inline_dllexport
1935 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1936 return true;
1937 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1938 reference to DECL might cause it to be emitted later. */
1939 return false;
1942 /* If necessary, write out the vtables for the dynamic class CTYPE.
1943 Returns true if any vtables were emitted. */
1945 static bool
1946 maybe_emit_vtables (tree ctype)
1948 tree vtbl;
1949 tree primary_vtbl;
1950 int needed = 0;
1951 varpool_node *current = NULL, *last = NULL;
1953 /* If the vtables for this class have already been emitted there is
1954 nothing more to do. */
1955 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1956 if (var_finalized_p (primary_vtbl))
1957 return false;
1958 /* Ignore dummy vtables made by get_vtable_decl. */
1959 if (TREE_TYPE (primary_vtbl) == void_type_node)
1960 return false;
1962 /* On some targets, we cannot determine the key method until the end
1963 of the translation unit -- which is when this function is
1964 called. */
1965 if (!targetm.cxx.key_method_may_be_inline ())
1966 determine_key_method (ctype);
1968 /* See if any of the vtables are needed. */
1969 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1971 import_export_decl (vtbl);
1972 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1973 needed = 1;
1975 if (!needed)
1977 /* If the references to this class' vtables are optimized away,
1978 still emit the appropriate debugging information. See
1979 dfs_debug_mark. */
1980 if (DECL_COMDAT (primary_vtbl)
1981 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1982 note_debug_info_needed (ctype);
1983 return false;
1986 /* The ABI requires that we emit all of the vtables if we emit any
1987 of them. */
1988 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1990 /* Mark entities references from the virtual table as used. */
1991 mark_vtable_entries (vtbl);
1993 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1995 vec<tree, va_gc> *cleanups = NULL;
1996 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
1997 LOOKUP_NORMAL);
1999 /* It had better be all done at compile-time. */
2000 gcc_assert (!expr && !cleanups);
2003 /* Write it out. */
2004 DECL_EXTERNAL (vtbl) = 0;
2005 rest_of_decl_compilation (vtbl, 1, 1);
2007 /* Because we're only doing syntax-checking, we'll never end up
2008 actually marking the variable as written. */
2009 if (flag_syntax_only)
2010 TREE_ASM_WRITTEN (vtbl) = 1;
2011 else if (DECL_ONE_ONLY (vtbl))
2013 current = varpool_node_for_decl (vtbl);
2014 if (last)
2015 symtab_add_to_same_comdat_group (current, last);
2016 last = current;
2020 /* Since we're writing out the vtable here, also write the debug
2021 info. */
2022 note_debug_info_needed (ctype);
2024 return true;
2027 /* A special return value from type_visibility meaning internal
2028 linkage. */
2030 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2032 /* walk_tree helper function for type_visibility. */
2034 static tree
2035 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2037 int *vis_p = (int *)data;
2038 if (! TYPE_P (*tp))
2040 *walk_subtrees = 0;
2042 else if (OVERLOAD_TYPE_P (*tp)
2043 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2045 *vis_p = VISIBILITY_ANON;
2046 return *tp;
2048 else if (CLASS_TYPE_P (*tp)
2049 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2050 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2051 return NULL;
2054 /* Returns the visibility of TYPE, which is the minimum visibility of its
2055 component types. */
2057 static int
2058 type_visibility (tree type)
2060 int vis = VISIBILITY_DEFAULT;
2061 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2062 return vis;
2065 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2066 specified (or if VISIBILITY is static). If TMPL is true, this
2067 constraint is for a template argument, and takes precedence
2068 over explicitly-specified visibility on the template. */
2070 static void
2071 constrain_visibility (tree decl, int visibility, bool tmpl)
2073 if (visibility == VISIBILITY_ANON)
2075 /* extern "C" declarations aren't affected by the anonymous
2076 namespace. */
2077 if (!DECL_EXTERN_C_P (decl))
2079 TREE_PUBLIC (decl) = 0;
2080 DECL_WEAK (decl) = 0;
2081 DECL_COMMON (decl) = 0;
2082 DECL_COMDAT_GROUP (decl) = NULL_TREE;
2083 DECL_INTERFACE_KNOWN (decl) = 1;
2084 if (DECL_LANG_SPECIFIC (decl))
2085 DECL_NOT_REALLY_EXTERN (decl) = 1;
2088 else if (visibility > DECL_VISIBILITY (decl)
2089 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2091 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2092 /* This visibility was not specified. */
2093 DECL_VISIBILITY_SPECIFIED (decl) = false;
2097 /* Constrain the visibility of DECL based on the visibility of its template
2098 arguments. */
2100 static void
2101 constrain_visibility_for_template (tree decl, tree targs)
2103 /* If this is a template instantiation, check the innermost
2104 template args for visibility constraints. The outer template
2105 args are covered by the class check. */
2106 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2107 int i;
2108 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2110 int vis = 0;
2112 tree arg = TREE_VEC_ELT (args, i-1);
2113 if (TYPE_P (arg))
2114 vis = type_visibility (arg);
2115 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2117 STRIP_NOPS (arg);
2118 if (TREE_CODE (arg) == ADDR_EXPR)
2119 arg = TREE_OPERAND (arg, 0);
2120 if (VAR_OR_FUNCTION_DECL_P (arg))
2122 if (! TREE_PUBLIC (arg))
2123 vis = VISIBILITY_ANON;
2124 else
2125 vis = DECL_VISIBILITY (arg);
2128 if (vis)
2129 constrain_visibility (decl, vis, true);
2133 /* Like c_determine_visibility, but with additional C++-specific
2134 behavior.
2136 Function-scope entities can rely on the function's visibility because
2137 it is set in start_preparsed_function.
2139 Class-scope entities cannot rely on the class's visibility until the end
2140 of the enclosing class definition.
2142 Note that because namespaces have multiple independent definitions,
2143 namespace visibility is handled elsewhere using the #pragma visibility
2144 machinery rather than by decorating the namespace declaration.
2146 The goal is for constraints from the type to give a diagnostic, and
2147 other constraints to be applied silently. */
2149 void
2150 determine_visibility (tree decl)
2152 tree class_type = NULL_TREE;
2153 bool use_template;
2154 bool orig_visibility_specified;
2155 enum symbol_visibility orig_visibility;
2157 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2159 /* Only relevant for names with external linkage. */
2160 if (!TREE_PUBLIC (decl))
2161 return;
2163 /* Cloned constructors and destructors get the same visibility as
2164 the underlying function. That should be set up in
2165 maybe_clone_body. */
2166 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2168 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2169 orig_visibility = DECL_VISIBILITY (decl);
2171 if (TREE_CODE (decl) == TYPE_DECL)
2173 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2174 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2175 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2176 use_template = 1;
2177 else
2178 use_template = 0;
2180 else if (DECL_LANG_SPECIFIC (decl))
2181 use_template = DECL_USE_TEMPLATE (decl);
2182 else
2183 use_template = 0;
2185 /* If DECL is a member of a class, visibility specifiers on the
2186 class can influence the visibility of the DECL. */
2187 if (DECL_CLASS_SCOPE_P (decl))
2188 class_type = DECL_CONTEXT (decl);
2189 else
2191 /* Not a class member. */
2193 /* Virtual tables have DECL_CONTEXT set to their associated class,
2194 so they are automatically handled above. */
2195 gcc_assert (!VAR_P (decl)
2196 || !DECL_VTABLE_OR_VTT_P (decl));
2198 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2200 /* Local statics and classes get the visibility of their
2201 containing function by default, except that
2202 -fvisibility-inlines-hidden doesn't affect them. */
2203 tree fn = DECL_CONTEXT (decl);
2204 if (DECL_VISIBILITY_SPECIFIED (fn))
2206 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2207 DECL_VISIBILITY_SPECIFIED (decl) =
2208 DECL_VISIBILITY_SPECIFIED (fn);
2210 else
2212 if (DECL_CLASS_SCOPE_P (fn))
2213 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2214 else if (determine_hidden_inline (fn))
2216 DECL_VISIBILITY (decl) = default_visibility;
2217 DECL_VISIBILITY_SPECIFIED (decl) =
2218 visibility_options.inpragma;
2220 else
2222 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2223 DECL_VISIBILITY_SPECIFIED (decl) =
2224 DECL_VISIBILITY_SPECIFIED (fn);
2228 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2229 but have no TEMPLATE_INFO, so don't try to check it. */
2230 use_template = 0;
2232 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2233 && flag_visibility_ms_compat)
2235 /* Under -fvisibility-ms-compat, types are visible by default,
2236 even though their contents aren't. */
2237 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2238 int underlying_vis = type_visibility (underlying_type);
2239 if (underlying_vis == VISIBILITY_ANON
2240 || (CLASS_TYPE_P (underlying_type)
2241 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2242 constrain_visibility (decl, underlying_vis, false);
2243 else
2244 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2246 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2248 /* tinfo visibility is based on the type it's for. */
2249 constrain_visibility
2250 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2252 /* Give the target a chance to override the visibility associated
2253 with DECL. */
2254 if (TREE_PUBLIC (decl)
2255 && !DECL_REALLY_EXTERN (decl)
2256 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2257 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2258 targetm.cxx.determine_class_data_visibility (decl);
2260 else if (use_template)
2261 /* Template instantiations and specializations get visibility based
2262 on their template unless they override it with an attribute. */;
2263 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2265 if (determine_hidden_inline (decl))
2266 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2267 else
2269 /* Set default visibility to whatever the user supplied with
2270 #pragma GCC visibility or a namespace visibility attribute. */
2271 DECL_VISIBILITY (decl) = default_visibility;
2272 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2277 if (use_template)
2279 /* If the specialization doesn't specify visibility, use the
2280 visibility from the template. */
2281 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2282 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2283 : DECL_TEMPLATE_INFO (decl));
2284 tree args = TI_ARGS (tinfo);
2285 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2286 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2287 : DECL_ATTRIBUTES (decl));
2289 if (args != error_mark_node)
2291 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2293 if (!DECL_VISIBILITY_SPECIFIED (decl))
2295 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2296 && determine_hidden_inline (decl))
2297 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2298 else
2300 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2301 DECL_VISIBILITY_SPECIFIED (decl)
2302 = DECL_VISIBILITY_SPECIFIED (pattern);
2306 if (args
2307 /* Template argument visibility outweighs #pragma or namespace
2308 visibility, but not an explicit attribute. */
2309 && !lookup_attribute ("visibility", attribs))
2311 int depth = TMPL_ARGS_DEPTH (args);
2312 if (DECL_VISIBILITY_SPECIFIED (decl))
2314 /* A class template member with explicit visibility
2315 overrides the class visibility, so we need to apply
2316 all the levels of template args directly. */
2317 int i;
2318 for (i = 1; i <= depth; ++i)
2320 tree lev = TMPL_ARGS_LEVEL (args, i);
2321 constrain_visibility_for_template (decl, lev);
2324 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2325 /* Limit visibility based on its template arguments. */
2326 constrain_visibility_for_template (decl, args);
2331 if (class_type)
2332 determine_visibility_from_class (decl, class_type);
2334 if (decl_anon_ns_mem_p (decl))
2335 /* Names in an anonymous namespace get internal linkage.
2336 This might change once we implement export. */
2337 constrain_visibility (decl, VISIBILITY_ANON, false);
2338 else if (TREE_CODE (decl) != TYPE_DECL)
2340 /* Propagate anonymity from type to decl. */
2341 int tvis = type_visibility (TREE_TYPE (decl));
2342 if (tvis == VISIBILITY_ANON
2343 || ! DECL_VISIBILITY_SPECIFIED (decl))
2344 constrain_visibility (decl, tvis, false);
2346 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2347 /* DR 757: A type without linkage shall not be used as the type of a
2348 variable or function with linkage, unless
2349 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2350 o the variable or function is not used (3.2 [basic.def.odr]) or is
2351 defined in the same translation unit.
2353 Since non-extern "C" decls need to be defined in the same
2354 translation unit, we can make the type internal. */
2355 constrain_visibility (decl, VISIBILITY_ANON, false);
2357 /* If visibility changed and DECL already has DECL_RTL, ensure
2358 symbol flags are updated. */
2359 if ((DECL_VISIBILITY (decl) != orig_visibility
2360 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2361 && ((VAR_P (decl) && TREE_STATIC (decl))
2362 || TREE_CODE (decl) == FUNCTION_DECL)
2363 && DECL_RTL_SET_P (decl))
2364 make_decl_rtl (decl);
2367 /* By default, static data members and function members receive
2368 the visibility of their containing class. */
2370 static void
2371 determine_visibility_from_class (tree decl, tree class_type)
2373 if (DECL_VISIBILITY_SPECIFIED (decl))
2374 return;
2376 if (determine_hidden_inline (decl))
2377 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2378 else
2380 /* Default to the class visibility. */
2381 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2382 DECL_VISIBILITY_SPECIFIED (decl)
2383 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2386 /* Give the target a chance to override the visibility associated
2387 with DECL. */
2388 if (VAR_P (decl)
2389 && (DECL_TINFO_P (decl)
2390 || (DECL_VTABLE_OR_VTT_P (decl)
2391 /* Construction virtual tables are not exported because
2392 they cannot be referred to from other object files;
2393 their name is not standardized by the ABI. */
2394 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2395 && TREE_PUBLIC (decl)
2396 && !DECL_REALLY_EXTERN (decl)
2397 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2398 targetm.cxx.determine_class_data_visibility (decl);
2401 /* Returns true iff DECL is an inline that should get hidden visibility
2402 because of -fvisibility-inlines-hidden. */
2404 static bool
2405 determine_hidden_inline (tree decl)
2407 return (visibility_options.inlines_hidden
2408 /* Don't do this for inline templates; specializations might not be
2409 inline, and we don't want them to inherit the hidden
2410 visibility. We'll set it here for all inline instantiations. */
2411 && !processing_template_decl
2412 && TREE_CODE (decl) == FUNCTION_DECL
2413 && DECL_DECLARED_INLINE_P (decl)
2414 && (! DECL_LANG_SPECIFIC (decl)
2415 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2418 /* Constrain the visibility of a class TYPE based on the visibility of its
2419 field types. Warn if any fields require lesser visibility. */
2421 void
2422 constrain_class_visibility (tree type)
2424 tree binfo;
2425 tree t;
2426 int i;
2428 int vis = type_visibility (type);
2430 if (vis == VISIBILITY_ANON
2431 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2432 return;
2434 /* Don't warn about visibility if the class has explicit visibility. */
2435 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2436 vis = VISIBILITY_INTERNAL;
2438 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2439 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2441 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2442 int subvis = type_visibility (ftype);
2444 if (subvis == VISIBILITY_ANON)
2446 if (!in_main_input_context ())
2447 warning (0, "\
2448 %qT has a field %qD whose type uses the anonymous namespace",
2449 type, t);
2451 else if (MAYBE_CLASS_TYPE_P (ftype)
2452 && vis < VISIBILITY_HIDDEN
2453 && subvis >= VISIBILITY_HIDDEN)
2454 warning (OPT_Wattributes, "\
2455 %qT declared with greater visibility than the type of its field %qD",
2456 type, t);
2459 binfo = TYPE_BINFO (type);
2460 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2462 int subvis = type_visibility (TREE_TYPE (t));
2464 if (subvis == VISIBILITY_ANON)
2466 if (!in_main_input_context())
2467 warning (0, "\
2468 %qT has a base %qT whose type uses the anonymous namespace",
2469 type, TREE_TYPE (t));
2471 else if (vis < VISIBILITY_HIDDEN
2472 && subvis >= VISIBILITY_HIDDEN)
2473 warning (OPT_Wattributes, "\
2474 %qT declared with greater visibility than its base %qT",
2475 type, TREE_TYPE (t));
2479 /* Functions for adjusting the visibility of a tagged type and its nested
2480 types and declarations when it gets a name for linkage purposes from a
2481 typedef. */
2483 static void bt_reset_linkage_1 (binding_entry, void *);
2484 static void bt_reset_linkage_2 (binding_entry, void *);
2486 /* First reset the visibility of all the types. */
2488 static void
2489 reset_type_linkage_1 (tree type)
2491 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2492 if (CLASS_TYPE_P (type))
2493 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2494 bt_reset_linkage_1, NULL);
2496 static void
2497 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2499 reset_type_linkage_1 (b->type);
2502 /* Then reset the visibility of any static data members or member
2503 functions that use those types. */
2505 static void
2506 reset_decl_linkage (tree decl)
2508 if (TREE_PUBLIC (decl))
2509 return;
2510 if (DECL_CLONED_FUNCTION_P (decl))
2511 return;
2512 TREE_PUBLIC (decl) = true;
2513 DECL_INTERFACE_KNOWN (decl) = false;
2514 determine_visibility (decl);
2515 tentative_decl_linkage (decl);
2517 static void
2518 reset_type_linkage_2 (tree type)
2520 if (CLASS_TYPE_P (type))
2522 if (tree vt = CLASSTYPE_VTABLES (type))
2524 tree name = mangle_vtbl_for_type (type);
2525 DECL_NAME (vt) = name;
2526 SET_DECL_ASSEMBLER_NAME (vt, name);
2527 reset_decl_linkage (vt);
2529 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2531 tree name = mangle_typeinfo_for_type (type);
2532 DECL_NAME (ti) = name;
2533 SET_DECL_ASSEMBLER_NAME (ti, name);
2534 TREE_TYPE (name) = type;
2535 reset_decl_linkage (ti);
2537 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2538 if (TREE_CODE (m) == VAR_DECL)
2539 reset_decl_linkage (m);
2540 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2541 reset_decl_linkage (m);
2542 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2543 bt_reset_linkage_2, NULL);
2546 static void
2547 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2549 reset_type_linkage_2 (b->type);
2551 void
2552 reset_type_linkage (tree type)
2554 reset_type_linkage_1 (type);
2555 reset_type_linkage_2 (type);
2558 /* Set up our initial idea of what the linkage of DECL should be. */
2560 void
2561 tentative_decl_linkage (tree decl)
2563 if (DECL_INTERFACE_KNOWN (decl))
2564 /* We've already made a decision as to how this function will
2565 be handled. */;
2566 else if (vague_linkage_p (decl))
2568 if (TREE_CODE (decl) == FUNCTION_DECL
2569 && decl_defined_p (decl))
2571 DECL_EXTERNAL (decl) = 1;
2572 DECL_NOT_REALLY_EXTERN (decl) = 1;
2573 note_vague_linkage_fn (decl);
2574 /* A non-template inline function with external linkage will
2575 always be COMDAT. As we must eventually determine the
2576 linkage of all functions, and as that causes writes to
2577 the data mapped in from the PCH file, it's advantageous
2578 to mark the functions at this point. */
2579 if (DECL_DECLARED_INLINE_P (decl)
2580 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2581 || DECL_DEFAULTED_FN (decl)))
2583 /* This function must have external linkage, as
2584 otherwise DECL_INTERFACE_KNOWN would have been
2585 set. */
2586 gcc_assert (TREE_PUBLIC (decl));
2587 comdat_linkage (decl);
2588 DECL_INTERFACE_KNOWN (decl) = 1;
2591 else if (TREE_CODE (decl) == VAR_DECL)
2592 maybe_commonize_var (decl);
2596 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2597 for DECL has not already been determined, do so now by setting
2598 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2599 function is called entities with vague linkage whose definitions
2600 are available must have TREE_PUBLIC set.
2602 If this function decides to place DECL in COMDAT, it will set
2603 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2604 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2605 callers defer that decision until it is clear that DECL is actually
2606 required. */
2608 void
2609 import_export_decl (tree decl)
2611 int emit_p;
2612 bool comdat_p;
2613 bool import_p;
2614 tree class_type = NULL_TREE;
2616 if (DECL_INTERFACE_KNOWN (decl))
2617 return;
2619 /* We cannot determine what linkage to give to an entity with vague
2620 linkage until the end of the file. For example, a virtual table
2621 for a class will be defined if and only if the key method is
2622 defined in this translation unit. As a further example, consider
2623 that when compiling a translation unit that uses PCH file with
2624 "-frepo" it would be incorrect to make decisions about what
2625 entities to emit when building the PCH; those decisions must be
2626 delayed until the repository information has been processed. */
2627 gcc_assert (at_eof);
2628 /* Object file linkage for explicit instantiations is handled in
2629 mark_decl_instantiated. For static variables in functions with
2630 vague linkage, maybe_commonize_var is used.
2632 Therefore, the only declarations that should be provided to this
2633 function are those with external linkage that are:
2635 * implicit instantiations of function templates
2637 * inline function
2639 * implicit instantiations of static data members of class
2640 templates
2642 * virtual tables
2644 * typeinfo objects
2646 Furthermore, all entities that reach this point must have a
2647 definition available in this translation unit.
2649 The following assertions check these conditions. */
2650 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2651 /* Any code that creates entities with TREE_PUBLIC cleared should
2652 also set DECL_INTERFACE_KNOWN. */
2653 gcc_assert (TREE_PUBLIC (decl));
2654 if (TREE_CODE (decl) == FUNCTION_DECL)
2655 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2656 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2657 || DECL_DECLARED_INLINE_P (decl));
2658 else
2659 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2660 || DECL_VTABLE_OR_VTT_P (decl)
2661 || DECL_TINFO_P (decl));
2662 /* Check that a definition of DECL is available in this translation
2663 unit. */
2664 gcc_assert (!DECL_REALLY_EXTERN (decl));
2666 /* Assume that DECL will not have COMDAT linkage. */
2667 comdat_p = false;
2668 /* Assume that DECL will not be imported into this translation
2669 unit. */
2670 import_p = false;
2672 /* See if the repository tells us whether or not to emit DECL in
2673 this translation unit. */
2674 emit_p = repo_emit_p (decl);
2675 if (emit_p == 0)
2676 import_p = true;
2677 else if (emit_p == 1)
2679 /* The repository indicates that this entity should be defined
2680 here. Make sure the back end honors that request. */
2681 if (VAR_P (decl))
2682 mark_needed (decl);
2683 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2684 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2686 tree clone;
2687 FOR_EACH_CLONE (clone, decl)
2688 mark_needed (clone);
2690 else
2691 mark_needed (decl);
2692 /* Output the definition as an ordinary strong definition. */
2693 DECL_EXTERNAL (decl) = 0;
2694 DECL_INTERFACE_KNOWN (decl) = 1;
2695 return;
2698 if (import_p)
2699 /* We have already decided what to do with this DECL; there is no
2700 need to check anything further. */
2702 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2704 class_type = DECL_CONTEXT (decl);
2705 import_export_class (class_type);
2706 if (TYPE_FOR_JAVA (class_type))
2707 import_p = true;
2708 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2709 && CLASSTYPE_INTERFACE_ONLY (class_type))
2710 import_p = true;
2711 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2712 && !CLASSTYPE_USE_TEMPLATE (class_type)
2713 && CLASSTYPE_KEY_METHOD (class_type)
2714 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2715 /* The ABI requires that all virtual tables be emitted with
2716 COMDAT linkage. However, on systems where COMDAT symbols
2717 don't show up in the table of contents for a static
2718 archive, or on systems without weak symbols (where we
2719 approximate COMDAT linkage by using internal linkage), the
2720 linker will report errors about undefined symbols because
2721 it will not see the virtual table definition. Therefore,
2722 in the case that we know that the virtual table will be
2723 emitted in only one translation unit, we make the virtual
2724 table an ordinary definition with external linkage. */
2725 DECL_EXTERNAL (decl) = 0;
2726 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2728 /* CLASS_TYPE is being exported from this translation unit,
2729 so DECL should be defined here. */
2730 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2731 /* If a class is declared in a header with the "extern
2732 template" extension, then it will not be instantiated,
2733 even in translation units that would normally require
2734 it. Often such classes are explicitly instantiated in
2735 one translation unit. Therefore, the explicit
2736 instantiation must be made visible to other translation
2737 units. */
2738 DECL_EXTERNAL (decl) = 0;
2739 else
2741 /* The generic C++ ABI says that class data is always
2742 COMDAT, even if there is a key function. Some
2743 variants (e.g., the ARM EABI) says that class data
2744 only has COMDAT linkage if the class data might be
2745 emitted in more than one translation unit. When the
2746 key method can be inline and is inline, we still have
2747 to arrange for comdat even though
2748 class_data_always_comdat is false. */
2749 if (!CLASSTYPE_KEY_METHOD (class_type)
2750 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2751 || targetm.cxx.class_data_always_comdat ())
2753 /* The ABI requires COMDAT linkage. Normally, we
2754 only emit COMDAT things when they are needed;
2755 make sure that we realize that this entity is
2756 indeed needed. */
2757 comdat_p = true;
2758 mark_needed (decl);
2762 else if (!flag_implicit_templates
2763 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2764 import_p = true;
2765 else
2766 comdat_p = true;
2768 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2770 tree type = TREE_TYPE (DECL_NAME (decl));
2771 if (CLASS_TYPE_P (type))
2773 class_type = type;
2774 import_export_class (type);
2775 if (CLASSTYPE_INTERFACE_KNOWN (type)
2776 && TYPE_POLYMORPHIC_P (type)
2777 && CLASSTYPE_INTERFACE_ONLY (type)
2778 /* If -fno-rtti was specified, then we cannot be sure
2779 that RTTI information will be emitted with the
2780 virtual table of the class, so we must emit it
2781 wherever it is used. */
2782 && flag_rtti)
2783 import_p = true;
2784 else
2786 if (CLASSTYPE_INTERFACE_KNOWN (type)
2787 && !CLASSTYPE_INTERFACE_ONLY (type))
2789 comdat_p = (targetm.cxx.class_data_always_comdat ()
2790 || (CLASSTYPE_KEY_METHOD (type)
2791 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2792 mark_needed (decl);
2793 if (!flag_weak)
2795 comdat_p = false;
2796 DECL_EXTERNAL (decl) = 0;
2799 else
2800 comdat_p = true;
2803 else
2804 comdat_p = true;
2806 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2808 /* DECL is an implicit instantiation of a function or static
2809 data member. */
2810 if ((flag_implicit_templates
2811 && !flag_use_repository)
2812 || (flag_implicit_inline_templates
2813 && TREE_CODE (decl) == FUNCTION_DECL
2814 && DECL_DECLARED_INLINE_P (decl)))
2815 comdat_p = true;
2816 else
2817 /* If we are not implicitly generating templates, then mark
2818 this entity as undefined in this translation unit. */
2819 import_p = true;
2821 else if (DECL_FUNCTION_MEMBER_P (decl))
2823 if (!DECL_DECLARED_INLINE_P (decl))
2825 tree ctype = DECL_CONTEXT (decl);
2826 import_export_class (ctype);
2827 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2829 DECL_NOT_REALLY_EXTERN (decl)
2830 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2831 || (DECL_DECLARED_INLINE_P (decl)
2832 && ! flag_implement_inlines
2833 && !DECL_VINDEX (decl)));
2835 if (!DECL_NOT_REALLY_EXTERN (decl))
2836 DECL_EXTERNAL (decl) = 1;
2838 /* Always make artificials weak. */
2839 if (DECL_ARTIFICIAL (decl) && flag_weak)
2840 comdat_p = true;
2841 else
2842 maybe_make_one_only (decl);
2845 else
2846 comdat_p = true;
2848 else
2849 comdat_p = true;
2851 if (import_p)
2853 /* If we are importing DECL into this translation unit, mark is
2854 an undefined here. */
2855 DECL_EXTERNAL (decl) = 1;
2856 DECL_NOT_REALLY_EXTERN (decl) = 0;
2858 else if (comdat_p)
2860 /* If we decided to put DECL in COMDAT, mark it accordingly at
2861 this point. */
2862 comdat_linkage (decl);
2865 DECL_INTERFACE_KNOWN (decl) = 1;
2868 /* Return an expression that performs the destruction of DECL, which
2869 must be a VAR_DECL whose type has a non-trivial destructor, or is
2870 an array whose (innermost) elements have a non-trivial destructor. */
2872 tree
2873 build_cleanup (tree decl)
2875 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2876 gcc_assert (clean != NULL_TREE);
2877 return clean;
2880 /* Returns the initialization guard variable for the variable DECL,
2881 which has static storage duration. */
2883 tree
2884 get_guard (tree decl)
2886 tree sname;
2887 tree guard;
2889 sname = mangle_guard_variable (decl);
2890 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2891 if (! guard)
2893 tree guard_type;
2895 /* We use a type that is big enough to contain a mutex as well
2896 as an integer counter. */
2897 guard_type = targetm.cxx.guard_type ();
2898 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2899 VAR_DECL, sname, guard_type);
2901 /* The guard should have the same linkage as what it guards. */
2902 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2903 TREE_STATIC (guard) = TREE_STATIC (decl);
2904 DECL_COMMON (guard) = DECL_COMMON (decl);
2905 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2906 DECL_TLS_MODEL (guard) = DECL_TLS_MODEL (decl);
2907 if (DECL_ONE_ONLY (decl))
2908 make_decl_one_only (guard, cxx_comdat_group (guard));
2909 if (TREE_PUBLIC (decl))
2910 DECL_WEAK (guard) = DECL_WEAK (decl);
2911 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2912 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2914 DECL_ARTIFICIAL (guard) = 1;
2915 DECL_IGNORED_P (guard) = 1;
2916 TREE_USED (guard) = 1;
2917 pushdecl_top_level_and_finish (guard, NULL_TREE);
2919 return guard;
2922 /* Return those bits of the GUARD variable that should be set when the
2923 guarded entity is actually initialized. */
2925 static tree
2926 get_guard_bits (tree guard)
2928 if (!targetm.cxx.guard_mask_bit ())
2930 /* We only set the first byte of the guard, in order to leave room
2931 for a mutex in the high-order bits. */
2932 guard = build1 (ADDR_EXPR,
2933 build_pointer_type (TREE_TYPE (guard)),
2934 guard);
2935 guard = build1 (NOP_EXPR,
2936 build_pointer_type (char_type_node),
2937 guard);
2938 guard = build1 (INDIRECT_REF, char_type_node, guard);
2941 return guard;
2944 /* Return an expression which determines whether or not the GUARD
2945 variable has already been initialized. */
2947 tree
2948 get_guard_cond (tree guard)
2950 tree guard_value;
2952 /* Check to see if the GUARD is zero. */
2953 guard = get_guard_bits (guard);
2955 /* Mask off all but the low bit. */
2956 if (targetm.cxx.guard_mask_bit ())
2958 guard_value = integer_one_node;
2959 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2960 guard_value = convert (TREE_TYPE (guard), guard_value);
2961 guard = cp_build_binary_op (input_location,
2962 BIT_AND_EXPR, guard, guard_value,
2963 tf_warning_or_error);
2966 guard_value = integer_zero_node;
2967 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2968 guard_value = convert (TREE_TYPE (guard), guard_value);
2969 return cp_build_binary_op (input_location,
2970 EQ_EXPR, guard, guard_value,
2971 tf_warning_or_error);
2974 /* Return an expression which sets the GUARD variable, indicating that
2975 the variable being guarded has been initialized. */
2977 tree
2978 set_guard (tree guard)
2980 tree guard_init;
2982 /* Set the GUARD to one. */
2983 guard = get_guard_bits (guard);
2984 guard_init = integer_one_node;
2985 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2986 guard_init = convert (TREE_TYPE (guard), guard_init);
2987 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2988 tf_warning_or_error);
2991 /* Returns true iff we can tell that VAR does not have a dynamic
2992 initializer. */
2994 static bool
2995 var_defined_without_dynamic_init (tree var)
2997 /* If it's defined in another TU, we can't tell. */
2998 if (DECL_EXTERNAL (var))
2999 return false;
3000 /* If it has a non-trivial destructor, registering the destructor
3001 counts as dynamic initialization. */
3002 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3003 return false;
3004 /* If it's in this TU, its initializer has been processed. */
3005 gcc_assert (DECL_INITIALIZED_P (var));
3006 /* If it has no initializer or a constant one, it's not dynamic. */
3007 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3008 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3011 /* Returns true iff VAR is a variable that needs uses to be
3012 wrapped for possible dynamic initialization. */
3014 static bool
3015 var_needs_tls_wrapper (tree var)
3017 return (!error_operand_p (var)
3018 && DECL_THREAD_LOCAL_P (var)
3019 && !DECL_GNU_TLS_P (var)
3020 && !DECL_FUNCTION_SCOPE_P (var)
3021 && !var_defined_without_dynamic_init (var));
3024 /* Get the FUNCTION_DECL for the shared TLS init function for this
3025 translation unit. */
3027 static tree
3028 get_local_tls_init_fn (void)
3030 tree sname = get_identifier ("__tls_init");
3031 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3032 if (!fn)
3034 fn = build_lang_decl (FUNCTION_DECL, sname,
3035 build_function_type (void_type_node,
3036 void_list_node));
3037 SET_DECL_LANGUAGE (fn, lang_c);
3038 TREE_PUBLIC (fn) = false;
3039 DECL_ARTIFICIAL (fn) = true;
3040 mark_used (fn);
3041 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3043 return fn;
3046 /* Get a FUNCTION_DECL for the init function for the thread_local
3047 variable VAR. The init function will be an alias to the function
3048 that initializes all the non-local TLS variables in the translation
3049 unit. The init function is only used by the wrapper function. */
3051 static tree
3052 get_tls_init_fn (tree var)
3054 /* Only C++11 TLS vars need this init fn. */
3055 if (!var_needs_tls_wrapper (var))
3056 return NULL_TREE;
3058 /* If -fno-extern-tls-init, assume that we don't need to call
3059 a tls init function for a variable defined in another TU. */
3060 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3061 return NULL_TREE;
3063 #ifdef ASM_OUTPUT_DEF
3064 /* If the variable is internal, or if we can't generate aliases,
3065 call the local init function directly. */
3066 if (!TREE_PUBLIC (var))
3067 #endif
3068 return get_local_tls_init_fn ();
3070 tree sname = mangle_tls_init_fn (var);
3071 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3072 if (!fn)
3074 fn = build_lang_decl (FUNCTION_DECL, sname,
3075 build_function_type (void_type_node,
3076 void_list_node));
3077 SET_DECL_LANGUAGE (fn, lang_c);
3078 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3079 DECL_ARTIFICIAL (fn) = true;
3080 DECL_COMDAT (fn) = DECL_COMDAT (var);
3081 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3082 if (DECL_ONE_ONLY (var))
3083 make_decl_one_only (fn, cxx_comdat_group (fn));
3084 if (TREE_PUBLIC (var))
3086 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3087 /* If the variable is defined somewhere else and might have static
3088 initialization, make the init function a weak reference. */
3089 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3090 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3091 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3092 && DECL_EXTERNAL (var))
3093 declare_weak (fn);
3094 else
3095 DECL_WEAK (fn) = DECL_WEAK (var);
3097 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3098 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3099 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3100 DECL_IGNORED_P (fn) = 1;
3101 mark_used (fn);
3103 DECL_BEFRIENDING_CLASSES (fn) = var;
3105 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3107 return fn;
3110 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3111 variable VAR. The wrapper function calls the init function (if any) for
3112 VAR and then returns a reference to VAR. The wrapper function is used
3113 in place of VAR everywhere VAR is mentioned. */
3115 tree
3116 get_tls_wrapper_fn (tree var)
3118 /* Only C++11 TLS vars need this wrapper fn. */
3119 if (!var_needs_tls_wrapper (var))
3120 return NULL_TREE;
3122 tree sname = mangle_tls_wrapper_fn (var);
3123 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3124 if (!fn)
3126 /* A named rvalue reference is an lvalue, so the wrapper should
3127 always return an lvalue reference. */
3128 tree type = non_reference (TREE_TYPE (var));
3129 type = build_reference_type (type);
3130 tree fntype = build_function_type (type, void_list_node);
3131 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3132 SET_DECL_LANGUAGE (fn, lang_c);
3133 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3134 DECL_ARTIFICIAL (fn) = true;
3135 DECL_IGNORED_P (fn) = 1;
3136 /* The wrapper is inline and emitted everywhere var is used. */
3137 DECL_DECLARED_INLINE_P (fn) = true;
3138 if (TREE_PUBLIC (var))
3140 comdat_linkage (fn);
3141 #ifdef HAVE_GAS_HIDDEN
3142 /* Make the wrapper bind locally; there's no reason to share
3143 the wrapper between multiple shared objects. */
3144 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3145 DECL_VISIBILITY_SPECIFIED (fn) = true;
3146 #endif
3148 if (!TREE_PUBLIC (fn))
3149 DECL_INTERFACE_KNOWN (fn) = true;
3150 mark_used (fn);
3151 note_vague_linkage_fn (fn);
3153 #if 0
3154 /* We want CSE to commonize calls to the wrapper, but marking it as
3155 pure is unsafe since it has side-effects. I guess we need a new
3156 ECF flag even weaker than ECF_PURE. FIXME! */
3157 DECL_PURE_P (fn) = true;
3158 #endif
3160 DECL_BEFRIENDING_CLASSES (fn) = var;
3162 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3164 return fn;
3167 /* At EOF, generate the definition for the TLS wrapper function FN:
3169 T& var_wrapper() {
3170 if (init_fn) init_fn();
3171 return var;
3172 } */
3174 static void
3175 generate_tls_wrapper (tree fn)
3177 tree var = DECL_BEFRIENDING_CLASSES (fn);
3179 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3180 tree body = begin_function_body ();
3181 /* Only call the init fn if there might be one. */
3182 if (tree init_fn = get_tls_init_fn (var))
3184 tree if_stmt = NULL_TREE;
3185 /* If init_fn is a weakref, make sure it exists before calling. */
3186 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3188 if_stmt = begin_if_stmt ();
3189 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3190 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3191 NE_EXPR, addr, nullptr_node,
3192 tf_warning_or_error);
3193 finish_if_stmt_cond (cond, if_stmt);
3195 finish_expr_stmt (build_cxx_call
3196 (init_fn, 0, NULL, tf_warning_or_error));
3197 if (if_stmt)
3199 finish_then_clause (if_stmt);
3200 finish_if_stmt (if_stmt);
3203 else
3204 /* If there's no initialization, the wrapper is a constant function. */
3205 TREE_READONLY (fn) = true;
3206 finish_return_stmt (convert_from_reference (var));
3207 finish_function_body (body);
3208 expand_or_defer_fn (finish_function (0));
3211 /* Start the process of running a particular set of global constructors
3212 or destructors. Subroutine of do_[cd]tors. Also called from
3213 vtv_start_verification_constructor_init_function. */
3215 static tree
3216 start_objects (int method_type, int initp)
3218 tree body;
3219 tree fndecl;
3220 char type[14];
3222 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3224 if (initp != DEFAULT_INIT_PRIORITY)
3226 char joiner;
3228 #ifdef JOINER
3229 joiner = JOINER;
3230 #else
3231 joiner = '_';
3232 #endif
3234 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3236 else
3237 sprintf (type, "sub_%c", method_type);
3239 fndecl = build_lang_decl (FUNCTION_DECL,
3240 get_file_function_name (type),
3241 build_function_type_list (void_type_node,
3242 NULL_TREE));
3243 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3245 TREE_PUBLIC (current_function_decl) = 0;
3247 /* Mark as artificial because it's not explicitly in the user's
3248 source code. */
3249 DECL_ARTIFICIAL (current_function_decl) = 1;
3251 /* Mark this declaration as used to avoid spurious warnings. */
3252 TREE_USED (current_function_decl) = 1;
3254 /* Mark this function as a global constructor or destructor. */
3255 if (method_type == 'I')
3256 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3257 else
3258 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3260 body = begin_compound_stmt (BCS_FN_BODY);
3262 return body;
3265 /* Finish the process of running a particular set of global constructors
3266 or destructors. Subroutine of do_[cd]tors. */
3268 static void
3269 finish_objects (int method_type, int initp, tree body)
3271 tree fn;
3273 /* Finish up. */
3274 finish_compound_stmt (body);
3275 fn = finish_function (0);
3277 if (method_type == 'I')
3279 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3280 decl_init_priority_insert (fn, initp);
3282 else
3284 DECL_STATIC_DESTRUCTOR (fn) = 1;
3285 decl_fini_priority_insert (fn, initp);
3288 expand_or_defer_fn (fn);
3291 /* The names of the parameters to the function created to handle
3292 initializations and destructions for objects with static storage
3293 duration. */
3294 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3295 #define PRIORITY_IDENTIFIER "__priority"
3297 /* The name of the function we create to handle initializations and
3298 destructions for objects with static storage duration. */
3299 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3301 /* The declaration for the __INITIALIZE_P argument. */
3302 static GTY(()) tree initialize_p_decl;
3304 /* The declaration for the __PRIORITY argument. */
3305 static GTY(()) tree priority_decl;
3307 /* The declaration for the static storage duration function. */
3308 static GTY(()) tree ssdf_decl;
3310 /* All the static storage duration functions created in this
3311 translation unit. */
3312 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3314 /* A map from priority levels to information about that priority
3315 level. There may be many such levels, so efficient lookup is
3316 important. */
3317 static splay_tree priority_info_map;
3319 /* Begins the generation of the function that will handle all
3320 initialization and destruction of objects with static storage
3321 duration. The function generated takes two parameters of type
3322 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3323 nonzero, it performs initializations. Otherwise, it performs
3324 destructions. It only performs those initializations or
3325 destructions with the indicated __PRIORITY. The generated function
3326 returns no value.
3328 It is assumed that this function will only be called once per
3329 translation unit. */
3331 static tree
3332 start_static_storage_duration_function (unsigned count)
3334 tree type;
3335 tree body;
3336 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3338 /* Create the identifier for this function. It will be of the form
3339 SSDF_IDENTIFIER_<number>. */
3340 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3342 type = build_function_type_list (void_type_node,
3343 integer_type_node, integer_type_node,
3344 NULL_TREE);
3346 /* Create the FUNCTION_DECL itself. */
3347 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3348 get_identifier (id),
3349 type);
3350 TREE_PUBLIC (ssdf_decl) = 0;
3351 DECL_ARTIFICIAL (ssdf_decl) = 1;
3353 /* Put this function in the list of functions to be called from the
3354 static constructors and destructors. */
3355 if (!ssdf_decls)
3357 vec_alloc (ssdf_decls, 32);
3359 /* Take this opportunity to initialize the map from priority
3360 numbers to information about that priority level. */
3361 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3362 /*delete_key_fn=*/0,
3363 /*delete_value_fn=*/
3364 (splay_tree_delete_value_fn) &free);
3366 /* We always need to generate functions for the
3367 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3368 priorities later, we'll be sure to find the
3369 DEFAULT_INIT_PRIORITY. */
3370 get_priority_info (DEFAULT_INIT_PRIORITY);
3373 vec_safe_push (ssdf_decls, ssdf_decl);
3375 /* Create the argument list. */
3376 initialize_p_decl = cp_build_parm_decl
3377 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3378 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3379 TREE_USED (initialize_p_decl) = 1;
3380 priority_decl = cp_build_parm_decl
3381 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3382 DECL_CONTEXT (priority_decl) = ssdf_decl;
3383 TREE_USED (priority_decl) = 1;
3385 DECL_CHAIN (initialize_p_decl) = priority_decl;
3386 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3388 /* Put the function in the global scope. */
3389 pushdecl (ssdf_decl);
3391 /* Start the function itself. This is equivalent to declaring the
3392 function as:
3394 static void __ssdf (int __initialize_p, init __priority_p);
3396 It is static because we only need to call this function from the
3397 various constructor and destructor functions for this module. */
3398 start_preparsed_function (ssdf_decl,
3399 /*attrs=*/NULL_TREE,
3400 SF_PRE_PARSED);
3402 /* Set up the scope of the outermost block in the function. */
3403 body = begin_compound_stmt (BCS_FN_BODY);
3405 return body;
3408 /* Finish the generation of the function which performs initialization
3409 and destruction of objects with static storage duration. After
3410 this point, no more such objects can be created. */
3412 static void
3413 finish_static_storage_duration_function (tree body)
3415 /* Close out the function. */
3416 finish_compound_stmt (body);
3417 expand_or_defer_fn (finish_function (0));
3420 /* Return the information about the indicated PRIORITY level. If no
3421 code to handle this level has yet been generated, generate the
3422 appropriate prologue. */
3424 static priority_info
3425 get_priority_info (int priority)
3427 priority_info pi;
3428 splay_tree_node n;
3430 n = splay_tree_lookup (priority_info_map,
3431 (splay_tree_key) priority);
3432 if (!n)
3434 /* Create a new priority information structure, and insert it
3435 into the map. */
3436 pi = XNEW (struct priority_info_s);
3437 pi->initializations_p = 0;
3438 pi->destructions_p = 0;
3439 splay_tree_insert (priority_info_map,
3440 (splay_tree_key) priority,
3441 (splay_tree_value) pi);
3443 else
3444 pi = (priority_info) n->value;
3446 return pi;
3449 /* The effective initialization priority of a DECL. */
3451 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3452 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3453 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3455 /* Whether a DECL needs a guard to protect it against multiple
3456 initialization. */
3458 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3459 || DECL_ONE_ONLY (decl) \
3460 || DECL_WEAK (decl)))
3462 /* Called from one_static_initialization_or_destruction(),
3463 via walk_tree.
3464 Walks the initializer list of a global variable and looks for
3465 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3466 and that have their DECL_CONTEXT() == NULL.
3467 For each such temporary variable, set their DECL_CONTEXT() to
3468 the current function. This is necessary because otherwise
3469 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3470 when trying to refer to a temporary variable that does not have
3471 it's DECL_CONTECT() properly set. */
3472 static tree
3473 fix_temporary_vars_context_r (tree *node,
3474 int * /*unused*/,
3475 void * /*unused1*/)
3477 gcc_assert (current_function_decl);
3479 if (TREE_CODE (*node) == BIND_EXPR)
3481 tree var;
3483 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3484 if (VAR_P (var)
3485 && !DECL_NAME (var)
3486 && DECL_ARTIFICIAL (var)
3487 && !DECL_CONTEXT (var))
3488 DECL_CONTEXT (var) = current_function_decl;
3491 return NULL_TREE;
3494 /* Set up to handle the initialization or destruction of DECL. If
3495 INITP is nonzero, we are initializing the variable. Otherwise, we
3496 are destroying it. */
3498 static void
3499 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3501 tree guard_if_stmt = NULL_TREE;
3502 tree guard;
3504 /* If we are supposed to destruct and there's a trivial destructor,
3505 nothing has to be done. */
3506 if (!initp
3507 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3508 return;
3510 /* Trick the compiler into thinking we are at the file and line
3511 where DECL was declared so that error-messages make sense, and so
3512 that the debugger will show somewhat sensible file and line
3513 information. */
3514 input_location = DECL_SOURCE_LOCATION (decl);
3516 /* Make sure temporary variables in the initialiser all have
3517 their DECL_CONTEXT() set to a value different from NULL_TREE.
3518 This can happen when global variables initialisers are built.
3519 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3520 the temporary variables that might have been generated in the
3521 accompagning initialisers is NULL_TREE, meaning the variables have been
3522 declared in the global namespace.
3523 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3524 of the temporaries are set to the current function decl. */
3525 cp_walk_tree_without_duplicates (&init,
3526 fix_temporary_vars_context_r,
3527 NULL);
3529 /* Because of:
3531 [class.access.spec]
3533 Access control for implicit calls to the constructors,
3534 the conversion functions, or the destructor called to
3535 create and destroy a static data member is performed as
3536 if these calls appeared in the scope of the member's
3537 class.
3539 we pretend we are in a static member function of the class of
3540 which the DECL is a member. */
3541 if (member_p (decl))
3543 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3544 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3547 /* Assume we don't need a guard. */
3548 guard = NULL_TREE;
3549 /* We need a guard if this is an object with external linkage that
3550 might be initialized in more than one place. (For example, a
3551 static data member of a template, when the data member requires
3552 construction.) */
3553 if (NEEDS_GUARD_P (decl))
3555 tree guard_cond;
3557 guard = get_guard (decl);
3559 /* When using __cxa_atexit, we just check the GUARD as we would
3560 for a local static. */
3561 if (flag_use_cxa_atexit)
3563 /* When using __cxa_atexit, we never try to destroy
3564 anything from a static destructor. */
3565 gcc_assert (initp);
3566 guard_cond = get_guard_cond (guard);
3568 /* If we don't have __cxa_atexit, then we will be running
3569 destructors from .fini sections, or their equivalents. So,
3570 we need to know how many times we've tried to initialize this
3571 object. We do initializations only if the GUARD is zero,
3572 i.e., if we are the first to initialize the variable. We do
3573 destructions only if the GUARD is one, i.e., if we are the
3574 last to destroy the variable. */
3575 else if (initp)
3576 guard_cond
3577 = cp_build_binary_op (input_location,
3578 EQ_EXPR,
3579 cp_build_unary_op (PREINCREMENT_EXPR,
3580 guard,
3581 /*noconvert=*/1,
3582 tf_warning_or_error),
3583 integer_one_node,
3584 tf_warning_or_error);
3585 else
3586 guard_cond
3587 = cp_build_binary_op (input_location,
3588 EQ_EXPR,
3589 cp_build_unary_op (PREDECREMENT_EXPR,
3590 guard,
3591 /*noconvert=*/1,
3592 tf_warning_or_error),
3593 integer_zero_node,
3594 tf_warning_or_error);
3596 guard_if_stmt = begin_if_stmt ();
3597 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3601 /* If we're using __cxa_atexit, we have not already set the GUARD,
3602 so we must do so now. */
3603 if (guard && initp && flag_use_cxa_atexit)
3604 finish_expr_stmt (set_guard (guard));
3606 /* Perform the initialization or destruction. */
3607 if (initp)
3609 if (init)
3611 finish_expr_stmt (init);
3612 if (flag_sanitize & SANITIZE_ADDRESS)
3614 varpool_node *vnode = varpool_get_node (decl);
3615 if (vnode)
3616 vnode->dynamically_initialized = 1;
3620 /* If we're using __cxa_atexit, register a function that calls the
3621 destructor for the object. */
3622 if (flag_use_cxa_atexit)
3623 finish_expr_stmt (register_dtor_fn (decl));
3625 else
3626 finish_expr_stmt (build_cleanup (decl));
3628 /* Finish the guard if-stmt, if necessary. */
3629 if (guard)
3631 finish_then_clause (guard_if_stmt);
3632 finish_if_stmt (guard_if_stmt);
3635 /* Now that we're done with DECL we don't need to pretend to be a
3636 member of its class any longer. */
3637 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3638 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3641 /* Generate code to do the initialization or destruction of the decls in VARS,
3642 a TREE_LIST of VAR_DECL with static storage duration.
3643 Whether initialization or destruction is performed is specified by INITP. */
3645 static void
3646 do_static_initialization_or_destruction (tree vars, bool initp)
3648 tree node, init_if_stmt, cond;
3650 /* Build the outer if-stmt to check for initialization or destruction. */
3651 init_if_stmt = begin_if_stmt ();
3652 cond = initp ? integer_one_node : integer_zero_node;
3653 cond = cp_build_binary_op (input_location,
3654 EQ_EXPR,
3655 initialize_p_decl,
3656 cond,
3657 tf_warning_or_error);
3658 finish_if_stmt_cond (cond, init_if_stmt);
3660 /* To make sure dynamic construction doesn't access globals from other
3661 compilation units where they might not be yet constructed, for
3662 -fsanitize=address insert __asan_before_dynamic_init call that
3663 prevents access to either all global variables that need construction
3664 in other compilation units, or at least those that haven't been
3665 initialized yet. Variables that need dynamic construction in
3666 the current compilation unit are kept accessible. */
3667 if (flag_sanitize & SANITIZE_ADDRESS)
3668 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3670 node = vars;
3671 do {
3672 tree decl = TREE_VALUE (node);
3673 tree priority_if_stmt;
3674 int priority;
3675 priority_info pi;
3677 /* If we don't need a destructor, there's nothing to do. Avoid
3678 creating a possibly empty if-stmt. */
3679 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3681 node = TREE_CHAIN (node);
3682 continue;
3685 /* Remember that we had an initialization or finalization at this
3686 priority. */
3687 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3688 pi = get_priority_info (priority);
3689 if (initp)
3690 pi->initializations_p = 1;
3691 else
3692 pi->destructions_p = 1;
3694 /* Conditionalize this initialization on being in the right priority
3695 and being initializing/finalizing appropriately. */
3696 priority_if_stmt = begin_if_stmt ();
3697 cond = cp_build_binary_op (input_location,
3698 EQ_EXPR,
3699 priority_decl,
3700 build_int_cst (NULL_TREE, priority),
3701 tf_warning_or_error);
3702 finish_if_stmt_cond (cond, priority_if_stmt);
3704 /* Process initializers with same priority. */
3705 for (; node
3706 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3707 node = TREE_CHAIN (node))
3708 /* Do one initialization or destruction. */
3709 one_static_initialization_or_destruction (TREE_VALUE (node),
3710 TREE_PURPOSE (node), initp);
3712 /* Finish up the priority if-stmt body. */
3713 finish_then_clause (priority_if_stmt);
3714 finish_if_stmt (priority_if_stmt);
3716 } while (node);
3718 /* Revert what __asan_before_dynamic_init did by calling
3719 __asan_after_dynamic_init. */
3720 if (flag_sanitize & SANITIZE_ADDRESS)
3721 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3723 /* Finish up the init/destruct if-stmt body. */
3724 finish_then_clause (init_if_stmt);
3725 finish_if_stmt (init_if_stmt);
3728 /* VARS is a list of variables with static storage duration which may
3729 need initialization and/or finalization. Remove those variables
3730 that don't really need to be initialized or finalized, and return
3731 the resulting list. The order in which the variables appear in
3732 VARS is in reverse order of the order in which they should actually
3733 be initialized. The list we return is in the unreversed order;
3734 i.e., the first variable should be initialized first. */
3736 static tree
3737 prune_vars_needing_no_initialization (tree *vars)
3739 tree *var = vars;
3740 tree result = NULL_TREE;
3742 while (*var)
3744 tree t = *var;
3745 tree decl = TREE_VALUE (t);
3746 tree init = TREE_PURPOSE (t);
3748 /* Deal gracefully with error. */
3749 if (decl == error_mark_node)
3751 var = &TREE_CHAIN (t);
3752 continue;
3755 /* The only things that can be initialized are variables. */
3756 gcc_assert (VAR_P (decl));
3758 /* If this object is not defined, we don't need to do anything
3759 here. */
3760 if (DECL_EXTERNAL (decl))
3762 var = &TREE_CHAIN (t);
3763 continue;
3766 /* Also, if the initializer already contains errors, we can bail
3767 out now. */
3768 if (init && TREE_CODE (init) == TREE_LIST
3769 && value_member (error_mark_node, init))
3771 var = &TREE_CHAIN (t);
3772 continue;
3775 /* This variable is going to need initialization and/or
3776 finalization, so we add it to the list. */
3777 *var = TREE_CHAIN (t);
3778 TREE_CHAIN (t) = result;
3779 result = t;
3782 return result;
3785 /* Make sure we have told the back end about all the variables in
3786 VARS. */
3788 static void
3789 write_out_vars (tree vars)
3791 tree v;
3793 for (v = vars; v; v = TREE_CHAIN (v))
3795 tree var = TREE_VALUE (v);
3796 if (!var_finalized_p (var))
3798 import_export_decl (var);
3799 rest_of_decl_compilation (var, 1, 1);
3804 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3805 (otherwise) that will initialize all global objects with static
3806 storage duration having the indicated PRIORITY. */
3808 static void
3809 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3810 location_t *locus)
3812 char function_key;
3813 tree fndecl;
3814 tree body;
3815 size_t i;
3817 input_location = *locus;
3818 /* ??? */
3819 /* Was: locus->line++; */
3821 /* We use `I' to indicate initialization and `D' to indicate
3822 destruction. */
3823 function_key = constructor_p ? 'I' : 'D';
3825 /* We emit the function lazily, to avoid generating empty
3826 global constructors and destructors. */
3827 body = NULL_TREE;
3829 /* For Objective-C++, we may need to initialize metadata found in this module.
3830 This must be done _before_ any other static initializations. */
3831 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3832 && constructor_p && objc_static_init_needed_p ())
3834 body = start_objects (function_key, priority);
3835 objc_generate_static_init_call (NULL_TREE);
3838 /* Call the static storage duration function with appropriate
3839 arguments. */
3840 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3842 /* Calls to pure or const functions will expand to nothing. */
3843 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3845 tree call;
3847 if (! body)
3848 body = start_objects (function_key, priority);
3850 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3851 build_int_cst (NULL_TREE,
3852 constructor_p),
3853 build_int_cst (NULL_TREE,
3854 priority),
3855 NULL_TREE);
3856 finish_expr_stmt (call);
3860 /* Close out the function. */
3861 if (body)
3862 finish_objects (function_key, priority, body);
3865 /* Generate constructor and destructor functions for the priority
3866 indicated by N. */
3868 static int
3869 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3871 location_t *locus = (location_t *) data;
3872 int priority = (int) n->key;
3873 priority_info pi = (priority_info) n->value;
3875 /* Generate the functions themselves, but only if they are really
3876 needed. */
3877 if (pi->initializations_p)
3878 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3879 if (pi->destructions_p)
3880 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3882 /* Keep iterating. */
3883 return 0;
3886 /* Java requires that we be able to reference a local address for a
3887 method, and not be confused by PLT entries. If hidden aliases are
3888 supported, collect and return all the functions for which we should
3889 emit a hidden alias. */
3891 static struct pointer_set_t *
3892 collect_candidates_for_java_method_aliases (void)
3894 struct cgraph_node *node;
3895 struct pointer_set_t *candidates = NULL;
3897 #ifndef HAVE_GAS_HIDDEN
3898 return candidates;
3899 #endif
3901 FOR_EACH_FUNCTION (node)
3903 tree fndecl = node->decl;
3905 if (DECL_CLASS_SCOPE_P (fndecl)
3906 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3907 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3909 if (candidates == NULL)
3910 candidates = pointer_set_create ();
3911 pointer_set_insert (candidates, fndecl);
3915 return candidates;
3919 /* Java requires that we be able to reference a local address for a
3920 method, and not be confused by PLT entries. If hidden aliases are
3921 supported, emit one for each java function that we've emitted.
3922 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3923 by collect_candidates_for_java_method_aliases. */
3925 static void
3926 build_java_method_aliases (struct pointer_set_t *candidates)
3928 struct cgraph_node *node;
3930 #ifndef HAVE_GAS_HIDDEN
3931 return;
3932 #endif
3934 FOR_EACH_FUNCTION (node)
3936 tree fndecl = node->decl;
3938 if (TREE_ASM_WRITTEN (fndecl)
3939 && pointer_set_contains (candidates, fndecl))
3941 /* Mangle the name in a predictable way; we need to reference
3942 this from a java compiled object file. */
3943 tree oid, nid, alias;
3944 const char *oname;
3945 char *nname;
3947 oid = DECL_ASSEMBLER_NAME (fndecl);
3948 oname = IDENTIFIER_POINTER (oid);
3949 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3950 nname = ACONCAT (("_ZGA", oname+2, NULL));
3951 nid = get_identifier (nname);
3953 alias = make_alias_for (fndecl, nid);
3954 TREE_PUBLIC (alias) = 1;
3955 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3957 assemble_alias (alias, oid);
3962 /* Return C++ property of T, based on given operation OP. */
3964 static int
3965 cpp_check (tree t, cpp_operation op)
3967 switch (op)
3969 case IS_ABSTRACT:
3970 return DECL_PURE_VIRTUAL_P (t);
3971 case IS_CONSTRUCTOR:
3972 return DECL_CONSTRUCTOR_P (t);
3973 case IS_DESTRUCTOR:
3974 return DECL_DESTRUCTOR_P (t);
3975 case IS_COPY_CONSTRUCTOR:
3976 return DECL_COPY_CONSTRUCTOR_P (t);
3977 case IS_TEMPLATE:
3978 return TREE_CODE (t) == TEMPLATE_DECL;
3979 case IS_TRIVIAL:
3980 return trivial_type_p (t);
3981 default:
3982 return 0;
3986 /* Collect source file references recursively, starting from NAMESPC. */
3988 static void
3989 collect_source_refs (tree namespc)
3991 tree t;
3993 if (!namespc)
3994 return;
3996 /* Iterate over names in this name space. */
3997 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
3998 if (!DECL_IS_BUILTIN (t) )
3999 collect_source_ref (DECL_SOURCE_FILE (t));
4001 /* Dump siblings, if any */
4002 collect_source_refs (TREE_CHAIN (namespc));
4004 /* Dump children, if any */
4005 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4008 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4009 starting from NAMESPC. */
4011 static void
4012 collect_ada_namespace (tree namespc, const char *source_file)
4014 if (!namespc)
4015 return;
4017 /* Collect decls from this namespace */
4018 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4020 /* Collect siblings, if any */
4021 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4023 /* Collect children, if any */
4024 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4027 /* Returns true iff there is a definition available for variable or
4028 function DECL. */
4030 static bool
4031 decl_defined_p (tree decl)
4033 if (TREE_CODE (decl) == FUNCTION_DECL)
4034 return (DECL_INITIAL (decl) != NULL_TREE);
4035 else
4037 gcc_assert (VAR_P (decl));
4038 return !DECL_EXTERNAL (decl);
4042 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4044 [expr.const]
4046 An integral constant-expression can only involve ... const
4047 variables of integral or enumeration types initialized with
4048 constant expressions ...
4050 C++0x also allows constexpr variables and temporaries initialized
4051 with constant expressions. We handle the former here, but the latter
4052 are just folded away in cxx_eval_constant_expression.
4054 The standard does not require that the expression be non-volatile.
4055 G++ implements the proposed correction in DR 457. */
4057 bool
4058 decl_constant_var_p (tree decl)
4060 if (!decl_maybe_constant_var_p (decl))
4061 return false;
4063 /* We don't know if a template static data member is initialized with
4064 a constant expression until we instantiate its initializer. Even
4065 in the case of a constexpr variable, we can't treat it as a
4066 constant until its initializer is complete in case it's used in
4067 its own initializer. */
4068 mark_used (decl);
4069 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4072 /* Returns true if DECL could be a symbolic constant variable, depending on
4073 its initializer. */
4075 bool
4076 decl_maybe_constant_var_p (tree decl)
4078 tree type = TREE_TYPE (decl);
4079 if (!VAR_P (decl))
4080 return false;
4081 if (DECL_DECLARED_CONSTEXPR_P (decl))
4082 return true;
4083 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4084 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4087 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4088 called from grokfndecl and grokvardecl; in all modes it is called from
4089 cp_write_global_declarations. */
4091 void
4092 no_linkage_error (tree decl)
4094 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4095 /* In C++11 it's ok if the decl is defined. */
4096 return;
4097 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4098 if (t == NULL_TREE)
4099 /* The type that got us on no_linkage_decls must have gotten a name for
4100 linkage purposes. */;
4101 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4102 /* The type might end up having a typedef name for linkage purposes. */
4103 vec_safe_push (no_linkage_decls, decl);
4104 else if (TYPE_ANONYMOUS_P (t))
4106 bool d = false;
4107 if (cxx_dialect >= cxx11)
4108 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4109 "anonymous type, is used but never defined", decl);
4110 else if (DECL_EXTERN_C_P (decl))
4111 /* Allow this; it's pretty common in C. */;
4112 else if (TREE_CODE (decl) == VAR_DECL)
4113 /* DRs 132, 319 and 389 seem to indicate types with
4114 no linkage can only be used to declare extern "C"
4115 entities. Since it's not always an error in the
4116 ISO C++ 90 Standard, we only issue a warning. */
4117 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4118 "with no linkage used to declare variable %q#D with "
4119 "linkage", decl);
4120 else
4121 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4122 "linkage used to declare function %q#D with linkage",
4123 decl);
4124 if (d && is_typedef_decl (TYPE_NAME (t)))
4125 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4126 "to the unqualified type, so it is not used for linkage",
4127 TYPE_NAME (t));
4129 else if (cxx_dialect >= cxx11)
4130 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4131 "%qT, is used but never defined", decl, t);
4132 else if (TREE_CODE (decl) == VAR_DECL)
4133 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4134 "used to declare variable %q#D with linkage", t, decl);
4135 else
4136 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4137 "to declare function %q#D with linkage", t, decl);
4140 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4142 static void
4143 collect_all_refs (const char *source_file)
4145 collect_ada_namespace (global_namespace, source_file);
4148 /* Clear DECL_EXTERNAL for NODE. */
4150 static bool
4151 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4153 DECL_EXTERNAL (node->decl) = 0;
4154 return false;
4157 /* Build up the function to run dynamic initializers for thread_local
4158 variables in this translation unit and alias the init functions for the
4159 individual variables to it. */
4161 static void
4162 handle_tls_init (void)
4164 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4165 if (vars == NULL_TREE)
4166 return;
4168 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4170 write_out_vars (vars);
4172 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4173 boolean_type_node);
4174 TREE_PUBLIC (guard) = false;
4175 TREE_STATIC (guard) = true;
4176 DECL_ARTIFICIAL (guard) = true;
4177 DECL_IGNORED_P (guard) = true;
4178 TREE_USED (guard) = true;
4179 DECL_TLS_MODEL (guard) = decl_default_tls_model (guard);
4180 pushdecl_top_level_and_finish (guard, NULL_TREE);
4182 tree fn = get_local_tls_init_fn ();
4183 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4184 tree body = begin_function_body ();
4185 tree if_stmt = begin_if_stmt ();
4186 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4187 tf_warning_or_error);
4188 finish_if_stmt_cond (cond, if_stmt);
4189 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4190 tf_warning_or_error));
4191 for (; vars; vars = TREE_CHAIN (vars))
4193 tree var = TREE_VALUE (vars);
4194 tree init = TREE_PURPOSE (vars);
4195 one_static_initialization_or_destruction (var, init, true);
4197 #ifdef ASM_OUTPUT_DEF
4198 /* Output init aliases even with -fno-extern-tls-init. */
4199 if (TREE_PUBLIC (var))
4201 tree single_init_fn = get_tls_init_fn (var);
4202 if (single_init_fn == NULL_TREE)
4203 continue;
4204 cgraph_node *alias
4205 = cgraph_same_body_alias (cgraph_get_create_node (fn),
4206 single_init_fn, fn);
4207 gcc_assert (alias != NULL);
4209 #endif
4212 finish_then_clause (if_stmt);
4213 finish_if_stmt (if_stmt);
4214 finish_function_body (body);
4215 expand_or_defer_fn (finish_function (0));
4218 /* The entire file is now complete. If requested, dump everything
4219 to a file. */
4221 static void
4222 dump_tu (void)
4224 int flags;
4225 FILE *stream = dump_begin (TDI_tu, &flags);
4227 if (stream)
4229 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4230 dump_end (TDI_tu, stream);
4234 /* This routine is called at the end of compilation.
4235 Its job is to create all the code needed to initialize and
4236 destroy the global aggregates. We do the destruction
4237 first, since that way we only need to reverse the decls once. */
4239 void
4240 cp_write_global_declarations (void)
4242 tree vars;
4243 bool reconsider;
4244 size_t i;
4245 location_t locus;
4246 unsigned ssdf_count = 0;
4247 int retries = 0;
4248 tree decl;
4249 struct pointer_set_t *candidates;
4251 locus = input_location;
4252 at_eof = 1;
4254 /* Bad parse errors. Just forget about it. */
4255 if (! global_bindings_p () || current_class_type
4256 || !vec_safe_is_empty (decl_namespace_list))
4257 return;
4259 /* This is the point to write out a PCH if we're doing that.
4260 In that case we do not want to do anything else. */
4261 if (pch_file)
4263 c_common_write_pch ();
4264 dump_tu ();
4265 return;
4268 cgraph_process_same_body_aliases ();
4270 /* Handle -fdump-ada-spec[-slim] */
4271 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4273 if (flag_dump_ada_spec_slim)
4274 collect_source_ref (main_input_filename);
4275 else
4276 collect_source_refs (global_namespace);
4278 dump_ada_specs (collect_all_refs, cpp_check);
4281 /* FIXME - huh? was input_line -= 1;*/
4283 timevar_start (TV_PHASE_DEFERRED);
4285 /* We now have to write out all the stuff we put off writing out.
4286 These include:
4288 o Template specializations that we have not yet instantiated,
4289 but which are needed.
4290 o Initialization and destruction for non-local objects with
4291 static storage duration. (Local objects with static storage
4292 duration are initialized when their scope is first entered,
4293 and are cleaned up via atexit.)
4294 o Virtual function tables.
4296 All of these may cause others to be needed. For example,
4297 instantiating one function may cause another to be needed, and
4298 generating the initializer for an object may cause templates to be
4299 instantiated, etc., etc. */
4301 emit_support_tinfos ();
4305 tree t;
4306 tree decl;
4308 reconsider = false;
4310 /* If there are templates that we've put off instantiating, do
4311 them now. */
4312 instantiate_pending_templates (retries);
4313 ggc_collect ();
4315 /* Write out virtual tables as required. Note that writing out
4316 the virtual table for a template class may cause the
4317 instantiation of members of that class. If we write out
4318 vtables then we remove the class from our list so we don't
4319 have to look at it again. */
4321 while (keyed_classes != NULL_TREE
4322 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4324 reconsider = true;
4325 keyed_classes = TREE_CHAIN (keyed_classes);
4328 t = keyed_classes;
4329 if (t != NULL_TREE)
4331 tree next = TREE_CHAIN (t);
4333 while (next)
4335 if (maybe_emit_vtables (TREE_VALUE (next)))
4337 reconsider = true;
4338 TREE_CHAIN (t) = TREE_CHAIN (next);
4340 else
4341 t = next;
4343 next = TREE_CHAIN (t);
4347 /* Write out needed type info variables. We have to be careful
4348 looping through unemitted decls, because emit_tinfo_decl may
4349 cause other variables to be needed. New elements will be
4350 appended, and we remove from the vector those that actually
4351 get emitted. */
4352 for (i = unemitted_tinfo_decls->length ();
4353 unemitted_tinfo_decls->iterate (--i, &t);)
4354 if (emit_tinfo_decl (t))
4356 reconsider = true;
4357 unemitted_tinfo_decls->unordered_remove (i);
4360 /* The list of objects with static storage duration is built up
4361 in reverse order. We clear STATIC_AGGREGATES so that any new
4362 aggregates added during the initialization of these will be
4363 initialized in the correct order when we next come around the
4364 loop. */
4365 vars = prune_vars_needing_no_initialization (&static_aggregates);
4367 if (vars)
4369 /* We need to start a new initialization function each time
4370 through the loop. That's because we need to know which
4371 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4372 isn't computed until a function is finished, and written
4373 out. That's a deficiency in the back end. When this is
4374 fixed, these initialization functions could all become
4375 inline, with resulting performance improvements. */
4376 tree ssdf_body;
4378 /* Set the line and file, so that it is obviously not from
4379 the source file. */
4380 input_location = locus;
4381 ssdf_body = start_static_storage_duration_function (ssdf_count);
4383 /* Make sure the back end knows about all the variables. */
4384 write_out_vars (vars);
4386 /* First generate code to do all the initializations. */
4387 if (vars)
4388 do_static_initialization_or_destruction (vars, /*initp=*/true);
4390 /* Then, generate code to do all the destructions. Do these
4391 in reverse order so that the most recently constructed
4392 variable is the first destroyed. If we're using
4393 __cxa_atexit, then we don't need to do this; functions
4394 were registered at initialization time to destroy the
4395 local statics. */
4396 if (!flag_use_cxa_atexit && vars)
4398 vars = nreverse (vars);
4399 do_static_initialization_or_destruction (vars, /*initp=*/false);
4401 else
4402 vars = NULL_TREE;
4404 /* Finish up the static storage duration function for this
4405 round. */
4406 input_location = locus;
4407 finish_static_storage_duration_function (ssdf_body);
4409 /* All those initializations and finalizations might cause
4410 us to need more inline functions, more template
4411 instantiations, etc. */
4412 reconsider = true;
4413 ssdf_count++;
4414 /* ??? was: locus.line++; */
4417 /* Now do the same for thread_local variables. */
4418 handle_tls_init ();
4420 /* Go through the set of inline functions whose bodies have not
4421 been emitted yet. If out-of-line copies of these functions
4422 are required, emit them. */
4423 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4425 /* Does it need synthesizing? */
4426 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4427 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4429 /* Even though we're already at the top-level, we push
4430 there again. That way, when we pop back a few lines
4431 hence, all of our state is restored. Otherwise,
4432 finish_function doesn't clean things up, and we end
4433 up with CURRENT_FUNCTION_DECL set. */
4434 push_to_top_level ();
4435 /* The decl's location will mark where it was first
4436 needed. Save that so synthesize method can indicate
4437 where it was needed from, in case of error */
4438 input_location = DECL_SOURCE_LOCATION (decl);
4439 synthesize_method (decl);
4440 pop_from_top_level ();
4441 reconsider = true;
4444 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4445 generate_tls_wrapper (decl);
4447 if (!DECL_SAVED_TREE (decl))
4448 continue;
4450 /* We lie to the back end, pretending that some functions
4451 are not defined when they really are. This keeps these
4452 functions from being put out unnecessarily. But, we must
4453 stop lying when the functions are referenced, or if they
4454 are not comdat since they need to be put out now. If
4455 DECL_INTERFACE_KNOWN, then we have already set
4456 DECL_EXTERNAL appropriately, so there's no need to check
4457 again, and we do not want to clear DECL_EXTERNAL if a
4458 previous call to import_export_decl set it.
4460 This is done in a separate for cycle, because if some
4461 deferred function is contained in another deferred
4462 function later in deferred_fns varray,
4463 rest_of_compilation would skip this function and we
4464 really cannot expand the same function twice. */
4465 import_export_decl (decl);
4466 if (DECL_NOT_REALLY_EXTERN (decl)
4467 && DECL_INITIAL (decl)
4468 && decl_needed_p (decl))
4470 struct cgraph_node *node, *next;
4472 node = cgraph_get_node (decl);
4473 if (node->cpp_implicit_alias)
4474 node = cgraph_alias_target (node);
4476 cgraph_for_node_and_aliases (node, clear_decl_external,
4477 NULL, true);
4478 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4479 group, we need to mark all symbols in the same comdat group
4480 that way. */
4481 if (node->same_comdat_group)
4482 for (next = cgraph (node->same_comdat_group);
4483 next != node;
4484 next = cgraph (next->same_comdat_group))
4485 cgraph_for_node_and_aliases (next, clear_decl_external,
4486 NULL, true);
4489 /* If we're going to need to write this function out, and
4490 there's already a body for it, create RTL for it now.
4491 (There might be no body if this is a method we haven't
4492 gotten around to synthesizing yet.) */
4493 if (!DECL_EXTERNAL (decl)
4494 && decl_needed_p (decl)
4495 && !TREE_ASM_WRITTEN (decl)
4496 && !cgraph_get_node (decl)->definition)
4498 /* We will output the function; no longer consider it in this
4499 loop. */
4500 DECL_DEFER_OUTPUT (decl) = 0;
4501 /* Generate RTL for this function now that we know we
4502 need it. */
4503 expand_or_defer_fn (decl);
4504 /* If we're compiling -fsyntax-only pretend that this
4505 function has been written out so that we don't try to
4506 expand it again. */
4507 if (flag_syntax_only)
4508 TREE_ASM_WRITTEN (decl) = 1;
4509 reconsider = true;
4513 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4514 reconsider = true;
4516 /* Static data members are just like namespace-scope globals. */
4517 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4519 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4520 /* Don't write it out if we haven't seen a definition. */
4521 || DECL_IN_AGGR_P (decl))
4522 continue;
4523 import_export_decl (decl);
4524 /* If this static data member is needed, provide it to the
4525 back end. */
4526 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4527 DECL_EXTERNAL (decl) = 0;
4529 if (vec_safe_length (pending_statics) != 0
4530 && wrapup_global_declarations (pending_statics->address (),
4531 pending_statics->length ()))
4532 reconsider = true;
4534 retries++;
4536 while (reconsider);
4538 /* All used inline functions must have a definition at this point. */
4539 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4541 if (/* Check online inline functions that were actually used. */
4542 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4543 /* If the definition actually was available here, then the
4544 fact that the function was not defined merely represents
4545 that for some reason (use of a template repository,
4546 #pragma interface, etc.) we decided not to emit the
4547 definition here. */
4548 && !DECL_INITIAL (decl)
4549 /* Don't complain if the template was defined. */
4550 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4551 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4552 (template_for_substitution (decl)))))
4554 warning (0, "inline function %q+D used but never defined", decl);
4555 /* Avoid a duplicate warning from check_global_declaration_1. */
4556 TREE_NO_WARNING (decl) = 1;
4560 /* So must decls that use a type with no linkage. */
4561 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4562 no_linkage_error (decl);
4564 /* Then, do the Objective-C stuff. This is where all the
4565 Objective-C module stuff gets generated (symtab,
4566 class/protocol/selector lists etc). This must be done after C++
4567 templates, destructors etc. so that selectors used in C++
4568 templates are properly allocated. */
4569 if (c_dialect_objc ())
4570 objc_write_global_declarations ();
4572 /* We give C linkage to static constructors and destructors. */
4573 push_lang_context (lang_name_c);
4575 /* Generate initialization and destruction functions for all
4576 priorities for which they are required. */
4577 if (priority_info_map)
4578 splay_tree_foreach (priority_info_map,
4579 generate_ctor_and_dtor_functions_for_priority,
4580 /*data=*/&locus);
4581 else if (c_dialect_objc () && objc_static_init_needed_p ())
4582 /* If this is obj-c++ and we need a static init, call
4583 generate_ctor_or_dtor_function. */
4584 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4585 DEFAULT_INIT_PRIORITY, &locus);
4587 /* We're done with the splay-tree now. */
4588 if (priority_info_map)
4589 splay_tree_delete (priority_info_map);
4591 /* Generate any missing aliases. */
4592 maybe_apply_pending_pragma_weaks ();
4594 /* We're done with static constructors, so we can go back to "C++"
4595 linkage now. */
4596 pop_lang_context ();
4598 /* Collect candidates for Java hidden aliases. */
4599 candidates = collect_candidates_for_java_method_aliases ();
4601 timevar_stop (TV_PHASE_DEFERRED);
4602 timevar_start (TV_PHASE_OPT_GEN);
4604 if (flag_vtable_verify)
4606 vtv_recover_class_info ();
4607 vtv_compute_class_hierarchy_transitive_closure ();
4608 vtv_build_vtable_verify_fndecl ();
4611 finalize_compilation_unit ();
4613 if (flag_vtable_verify)
4615 /* Generate the special constructor initialization function that
4616 calls __VLTRegisterPairs, and give it a very high
4617 initialization priority. This must be done after
4618 finalize_compilation_unit so that we have accurate
4619 information about which vtable will actually be emitted. */
4620 vtv_generate_init_routine ();
4623 timevar_stop (TV_PHASE_OPT_GEN);
4624 timevar_start (TV_PHASE_CHECK_DBGINFO);
4626 /* Now, issue warnings about static, but not defined, functions,
4627 etc., and emit debugging information. */
4628 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4629 if (vec_safe_length (pending_statics) != 0)
4631 check_global_declarations (pending_statics->address (),
4632 pending_statics->length ());
4633 emit_debug_global_declarations (pending_statics->address (),
4634 pending_statics->length ());
4637 perform_deferred_noexcept_checks ();
4639 /* Generate hidden aliases for Java. */
4640 if (candidates)
4642 build_java_method_aliases (candidates);
4643 pointer_set_destroy (candidates);
4646 finish_repo ();
4648 /* The entire file is now complete. If requested, dump everything
4649 to a file. */
4650 dump_tu ();
4652 if (flag_detailed_statistics)
4654 dump_tree_statistics ();
4655 dump_time_statistics ();
4657 input_location = locus;
4659 #ifdef ENABLE_CHECKING
4660 validate_conversion_obstack ();
4661 #endif /* ENABLE_CHECKING */
4663 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4666 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4667 function to call in parse-tree form; it has not yet been
4668 semantically analyzed. ARGS are the arguments to the function.
4669 They have already been semantically analyzed. This may change
4670 ARGS. */
4672 tree
4673 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4674 tsubst_flags_t complain)
4676 tree orig_fn;
4677 vec<tree, va_gc> *orig_args = NULL;
4678 tree expr;
4679 tree object;
4681 orig_fn = fn;
4682 object = TREE_OPERAND (fn, 0);
4684 if (processing_template_decl)
4686 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4687 || TREE_CODE (fn) == MEMBER_REF);
4688 if (type_dependent_expression_p (fn)
4689 || any_type_dependent_arguments_p (*args))
4690 return build_nt_call_vec (fn, *args);
4692 orig_args = make_tree_vector_copy (*args);
4694 /* Transform the arguments and add the implicit "this"
4695 parameter. That must be done before the FN is transformed
4696 because we depend on the form of FN. */
4697 make_args_non_dependent (*args);
4698 object = build_non_dependent_expr (object);
4699 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4701 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4702 object = cp_build_addr_expr (object, complain);
4703 vec_safe_insert (*args, 0, object);
4705 /* Now that the arguments are done, transform FN. */
4706 fn = build_non_dependent_expr (fn);
4709 /* A qualified name corresponding to a bound pointer-to-member is
4710 represented as an OFFSET_REF:
4712 struct B { void g(); };
4713 void (B::*p)();
4714 void B::g() { (this->*p)(); } */
4715 if (TREE_CODE (fn) == OFFSET_REF)
4717 tree object_addr = cp_build_addr_expr (object, complain);
4718 fn = TREE_OPERAND (fn, 1);
4719 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4720 complain);
4721 vec_safe_insert (*args, 0, object_addr);
4724 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4725 expr = build_op_call (fn, args, complain);
4726 else
4727 expr = cp_build_function_call_vec (fn, args, complain);
4728 if (processing_template_decl && expr != error_mark_node)
4729 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4731 if (orig_args != NULL)
4732 release_tree_vector (orig_args);
4734 return expr;
4738 void
4739 check_default_args (tree x)
4741 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4742 bool saw_def = false;
4743 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4744 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4746 if (TREE_PURPOSE (arg))
4747 saw_def = true;
4748 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4750 error ("default argument missing for parameter %P of %q+#D", i, x);
4751 TREE_PURPOSE (arg) = error_mark_node;
4756 /* Return true if function DECL can be inlined. This is used to force
4757 instantiation of methods that might be interesting for inlining. */
4758 bool
4759 possibly_inlined_p (tree decl)
4761 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4762 if (DECL_UNINLINABLE (decl))
4763 return false;
4764 if (!optimize || pragma_java_exceptions)
4765 return DECL_DECLARED_INLINE_P (decl);
4766 /* When optimizing, we might inline everything when flatten
4767 attribute or heuristics inlining for size or autoinlining
4768 is used. */
4769 return true;
4772 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4773 If DECL is a specialization or implicitly declared class member,
4774 generate the actual definition. Return false if something goes
4775 wrong, true otherwise. */
4777 bool
4778 mark_used (tree decl, tsubst_flags_t complain)
4780 /* If DECL is a BASELINK for a single function, then treat it just
4781 like the DECL for the function. Otherwise, if the BASELINK is
4782 for an overloaded function, we don't know which function was
4783 actually used until after overload resolution. */
4784 if (BASELINK_P (decl))
4786 decl = BASELINK_FUNCTIONS (decl);
4787 if (really_overloaded_fn (decl))
4788 return true;
4789 decl = OVL_CURRENT (decl);
4792 /* Set TREE_USED for the benefit of -Wunused. */
4793 TREE_USED (decl) = 1;
4794 if (DECL_CLONED_FUNCTION_P (decl))
4795 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4797 /* Mark enumeration types as used. */
4798 if (TREE_CODE (decl) == CONST_DECL)
4799 used_types_insert (DECL_CONTEXT (decl));
4801 if (TREE_CODE (decl) == FUNCTION_DECL
4802 && DECL_DELETED_FN (decl))
4804 if (DECL_ARTIFICIAL (decl))
4806 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4807 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4809 /* We mark a lambda conversion op as deleted if we can't
4810 generate it properly; see maybe_add_lambda_conv_op. */
4811 sorry ("converting lambda which uses %<...%> to "
4812 "function pointer");
4813 return false;
4816 if (complain & tf_error)
4818 error ("use of deleted function %qD", decl);
4819 if (!maybe_explain_implicit_delete (decl))
4820 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4822 return false;
4825 /* We can only check DECL_ODR_USED on variables or functions with
4826 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4827 might need special handling for. */
4828 if (!VAR_OR_FUNCTION_DECL_P (decl)
4829 || DECL_LANG_SPECIFIC (decl) == NULL
4830 || DECL_THUNK_P (decl))
4832 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4834 if (complain & tf_error)
4835 error ("use of %qD before deduction of %<auto%>", decl);
4836 return false;
4838 return true;
4841 /* We only want to do this processing once. We don't need to keep trying
4842 to instantiate inline templates, because unit-at-a-time will make sure
4843 we get them compiled before functions that want to inline them. */
4844 if (DECL_ODR_USED (decl))
4845 return true;
4847 /* If within finish_function, defer the rest until that function
4848 finishes, otherwise it might recurse. */
4849 if (defer_mark_used_calls)
4851 vec_safe_push (deferred_mark_used_calls, decl);
4852 return true;
4855 if (TREE_CODE (decl) == FUNCTION_DECL)
4856 maybe_instantiate_noexcept (decl);
4858 /* Normally, we can wait until instantiation-time to synthesize DECL.
4859 However, if DECL is a static data member initialized with a constant
4860 or a constexpr function, we need it right now because a reference to
4861 such a data member or a call to such function is not value-dependent.
4862 For a function that uses auto in the return type, we need to instantiate
4863 it to find out its type. For OpenMP user defined reductions, we need
4864 them instantiated for reduction clauses which inline them by hand
4865 directly. */
4866 if (DECL_LANG_SPECIFIC (decl)
4867 && DECL_TEMPLATE_INFO (decl)
4868 && (decl_maybe_constant_var_p (decl)
4869 || (TREE_CODE (decl) == FUNCTION_DECL
4870 && (DECL_DECLARED_CONSTEXPR_P (decl)
4871 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4872 || undeduced_auto_decl (decl))
4873 && !uses_template_parms (DECL_TI_ARGS (decl)))
4875 /* Instantiating a function will result in garbage collection. We
4876 must treat this situation as if we were within the body of a
4877 function so as to avoid collecting live data only referenced from
4878 the stack (such as overload resolution candidates). */
4879 ++function_depth;
4880 instantiate_decl (decl, /*defer_ok=*/false,
4881 /*expl_inst_class_mem_p=*/false);
4882 --function_depth;
4885 if (processing_template_decl)
4886 return true;
4888 /* Check this too in case we're within fold_non_dependent_expr. */
4889 if (DECL_TEMPLATE_INFO (decl)
4890 && uses_template_parms (DECL_TI_ARGS (decl)))
4891 return true;
4893 require_deduced_type (decl);
4895 /* If we don't need a value, then we don't need to synthesize DECL. */
4896 if (cp_unevaluated_operand != 0)
4897 return true;
4899 DECL_ODR_USED (decl) = 1;
4900 if (DECL_CLONED_FUNCTION_P (decl))
4901 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4903 /* DR 757: A type without linkage shall not be used as the type of a
4904 variable or function with linkage, unless
4905 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4906 o the variable or function is not used (3.2 [basic.def.odr]) or is
4907 defined in the same translation unit. */
4908 if (cxx_dialect > cxx98
4909 && decl_linkage (decl) != lk_none
4910 && !DECL_EXTERN_C_P (decl)
4911 && !DECL_ARTIFICIAL (decl)
4912 && !decl_defined_p (decl)
4913 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4915 if (is_local_extern (decl))
4916 /* There's no way to define a local extern, and adding it to
4917 the vector interferes with GC, so give an error now. */
4918 no_linkage_error (decl);
4919 else
4920 vec_safe_push (no_linkage_decls, decl);
4923 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4924 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4925 /* Remember it, so we can check it was defined. */
4926 note_vague_linkage_fn (decl);
4928 /* Is it a synthesized method that needs to be synthesized? */
4929 if (TREE_CODE (decl) == FUNCTION_DECL
4930 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4931 && DECL_DEFAULTED_FN (decl)
4932 /* A function defaulted outside the class is synthesized either by
4933 cp_finish_decl or instantiate_decl. */
4934 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
4935 && ! DECL_INITIAL (decl))
4937 /* Defer virtual destructors so that thunks get the right
4938 linkage. */
4939 if (DECL_VIRTUAL_P (decl) && !at_eof)
4941 note_vague_linkage_fn (decl);
4942 return true;
4945 /* Remember the current location for a function we will end up
4946 synthesizing. Then we can inform the user where it was
4947 required in the case of error. */
4948 DECL_SOURCE_LOCATION (decl) = input_location;
4950 /* Synthesizing an implicitly defined member function will result in
4951 garbage collection. We must treat this situation as if we were
4952 within the body of a function so as to avoid collecting live data
4953 on the stack (such as overload resolution candidates).
4955 We could just let cp_write_global_declarations handle synthesizing
4956 this function by adding it to deferred_fns, but doing
4957 it at the use site produces better error messages. */
4958 ++function_depth;
4959 synthesize_method (decl);
4960 --function_depth;
4961 /* If this is a synthesized method we don't need to
4962 do the instantiation test below. */
4964 else if (VAR_OR_FUNCTION_DECL_P (decl)
4965 && DECL_TEMPLATE_INFO (decl)
4966 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4967 || always_instantiate_p (decl)))
4968 /* If this is a function or variable that is an instance of some
4969 template, we now know that we will need to actually do the
4970 instantiation. We check that DECL is not an explicit
4971 instantiation because that is not checked in instantiate_decl.
4973 We put off instantiating functions in order to improve compile
4974 times. Maintaining a stack of active functions is expensive,
4975 and the inliner knows to instantiate any functions it might
4976 need. Therefore, we always try to defer instantiation. */
4978 ++function_depth;
4979 instantiate_decl (decl, /*defer_ok=*/true,
4980 /*expl_inst_class_mem_p=*/false);
4981 --function_depth;
4984 return true;
4987 bool
4988 mark_used (tree decl)
4990 return mark_used (decl, tf_warning_or_error);
4993 tree
4994 vtv_start_verification_constructor_init_function (void)
4996 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
4999 tree
5000 vtv_finish_verification_constructor_init_function (tree function_body)
5002 tree fn;
5004 finish_compound_stmt (function_body);
5005 fn = finish_function (0);
5006 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5007 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5009 return fn;
5012 #include "gt-cp-decl2.h"