2014-04-16 Patrick Palka <patrick@parcs.ath.cx>
[official-gcc.git] / gcc / cp / decl2.c
blob8a7a8369f19a48b268347c30101907db2e87722d
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 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1432 attributes
1433 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1434 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (*decl)),
1435 attributes, flags);
1437 else
1438 decl_attributes (decl, attributes, flags);
1440 if (TREE_CODE (*decl) == TYPE_DECL)
1441 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1444 /* Walks through the namespace- or function-scope anonymous union
1445 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1446 Returns one of the fields for use in the mangled name. */
1448 static tree
1449 build_anon_union_vars (tree type, tree object)
1451 tree main_decl = NULL_TREE;
1452 tree field;
1454 /* Rather than write the code to handle the non-union case,
1455 just give an error. */
1456 if (TREE_CODE (type) != UNION_TYPE)
1458 error ("anonymous struct not inside named type");
1459 return error_mark_node;
1462 for (field = TYPE_FIELDS (type);
1463 field != NULL_TREE;
1464 field = DECL_CHAIN (field))
1466 tree decl;
1467 tree ref;
1469 if (DECL_ARTIFICIAL (field))
1470 continue;
1471 if (TREE_CODE (field) != FIELD_DECL)
1473 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1474 "have non-static data members", field);
1475 continue;
1478 if (TREE_PRIVATE (field))
1479 permerror (input_location, "private member %q+#D in anonymous union", field);
1480 else if (TREE_PROTECTED (field))
1481 permerror (input_location, "protected member %q+#D in anonymous union", field);
1483 if (processing_template_decl)
1484 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1485 DECL_NAME (field), NULL_TREE);
1486 else
1487 ref = build_class_member_access_expr (object, field, NULL_TREE,
1488 false, tf_warning_or_error);
1490 if (DECL_NAME (field))
1492 tree base;
1494 decl = build_decl (input_location,
1495 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1496 DECL_ANON_UNION_VAR_P (decl) = 1;
1497 DECL_ARTIFICIAL (decl) = 1;
1499 base = get_base_address (object);
1500 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1501 TREE_STATIC (decl) = TREE_STATIC (base);
1502 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1504 SET_DECL_VALUE_EXPR (decl, ref);
1505 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1507 decl = pushdecl (decl);
1509 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1510 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1511 else
1512 decl = 0;
1514 if (main_decl == NULL_TREE)
1515 main_decl = decl;
1518 return main_decl;
1521 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1522 anonymous union, then all members must be laid out together. PUBLIC_P
1523 is nonzero if this union is not declared static. */
1525 void
1526 finish_anon_union (tree anon_union_decl)
1528 tree type;
1529 tree main_decl;
1530 bool public_p;
1532 if (anon_union_decl == error_mark_node)
1533 return;
1535 type = TREE_TYPE (anon_union_decl);
1536 public_p = TREE_PUBLIC (anon_union_decl);
1538 /* The VAR_DECL's context is the same as the TYPE's context. */
1539 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1541 if (TYPE_FIELDS (type) == NULL_TREE)
1542 return;
1544 if (public_p)
1546 error ("namespace-scope anonymous aggregates must be static");
1547 return;
1550 main_decl = build_anon_union_vars (type, anon_union_decl);
1551 if (main_decl == error_mark_node)
1552 return;
1553 if (main_decl == NULL_TREE)
1555 warning (0, "anonymous union with no members");
1556 return;
1559 if (!processing_template_decl)
1561 /* Use main_decl to set the mangled name. */
1562 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1563 maybe_commonize_var (anon_union_decl);
1564 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1565 mangle_decl (anon_union_decl);
1566 DECL_NAME (anon_union_decl) = NULL_TREE;
1569 pushdecl (anon_union_decl);
1570 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1573 /* Auxiliary functions to make type signatures for
1574 `operator new' and `operator delete' correspond to
1575 what compiler will be expecting. */
1577 tree
1578 coerce_new_type (tree type)
1580 int e = 0;
1581 tree args = TYPE_ARG_TYPES (type);
1583 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1585 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1587 e = 1;
1588 error ("%<operator new%> must return type %qT", ptr_type_node);
1591 if (args && args != void_list_node)
1593 if (TREE_PURPOSE (args))
1595 /* [basic.stc.dynamic.allocation]
1597 The first parameter shall not have an associated default
1598 argument. */
1599 error ("the first parameter of %<operator new%> cannot "
1600 "have a default argument");
1601 /* Throw away the default argument. */
1602 TREE_PURPOSE (args) = NULL_TREE;
1605 if (!same_type_p (TREE_VALUE (args), size_type_node))
1607 e = 2;
1608 args = TREE_CHAIN (args);
1611 else
1612 e = 2;
1614 if (e == 2)
1615 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1616 "as first parameter", size_type_node);
1618 switch (e)
1620 case 2:
1621 args = tree_cons (NULL_TREE, size_type_node, args);
1622 /* Fall through. */
1623 case 1:
1624 type = build_exception_variant
1625 (build_function_type (ptr_type_node, args),
1626 TYPE_RAISES_EXCEPTIONS (type));
1627 /* Fall through. */
1628 default:;
1630 return type;
1633 tree
1634 coerce_delete_type (tree type)
1636 int e = 0;
1637 tree args = TYPE_ARG_TYPES (type);
1639 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1641 if (!same_type_p (TREE_TYPE (type), void_type_node))
1643 e = 1;
1644 error ("%<operator delete%> must return type %qT", void_type_node);
1647 if (!args || args == void_list_node
1648 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1650 e = 2;
1651 if (args && args != void_list_node)
1652 args = TREE_CHAIN (args);
1653 error ("%<operator delete%> takes type %qT as first parameter",
1654 ptr_type_node);
1656 switch (e)
1658 case 2:
1659 args = tree_cons (NULL_TREE, ptr_type_node, args);
1660 /* Fall through. */
1661 case 1:
1662 type = build_exception_variant
1663 (build_function_type (void_type_node, args),
1664 TYPE_RAISES_EXCEPTIONS (type));
1665 /* Fall through. */
1666 default:;
1669 return type;
1672 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1673 and mark them as needed. */
1675 static void
1676 mark_vtable_entries (tree decl)
1678 tree fnaddr;
1679 unsigned HOST_WIDE_INT idx;
1681 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1682 idx, fnaddr)
1684 tree fn;
1686 STRIP_NOPS (fnaddr);
1688 if (TREE_CODE (fnaddr) != ADDR_EXPR
1689 && TREE_CODE (fnaddr) != FDESC_EXPR)
1690 /* This entry is an offset: a virtual base class offset, a
1691 virtual call offset, an RTTI offset, etc. */
1692 continue;
1694 fn = TREE_OPERAND (fnaddr, 0);
1695 TREE_ADDRESSABLE (fn) = 1;
1696 /* When we don't have vcall offsets, we output thunks whenever
1697 we output the vtables that contain them. With vcall offsets,
1698 we know all the thunks we'll need when we emit a virtual
1699 function, so we emit the thunks there instead. */
1700 if (DECL_THUNK_P (fn))
1701 use_thunk (fn, /*emit_p=*/0);
1702 mark_used (fn);
1706 /* Set DECL up to have the closest approximation of "initialized common"
1707 linkage available. */
1709 void
1710 comdat_linkage (tree decl)
1712 if (flag_weak)
1713 make_decl_one_only (decl, cxx_comdat_group (decl));
1714 else if (TREE_CODE (decl) == FUNCTION_DECL
1715 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1716 /* We can just emit function and compiler-generated variables
1717 statically; having multiple copies is (for the most part) only
1718 a waste of space.
1720 There are two correctness issues, however: the address of a
1721 template instantiation with external linkage should be the
1722 same, independent of what translation unit asks for the
1723 address, and this will not hold when we emit multiple copies of
1724 the function. However, there's little else we can do.
1726 Also, by default, the typeinfo implementation assumes that
1727 there will be only one copy of the string used as the name for
1728 each type. Therefore, if weak symbols are unavailable, the
1729 run-time library should perform a more conservative check; it
1730 should perform a string comparison, rather than an address
1731 comparison. */
1732 TREE_PUBLIC (decl) = 0;
1733 else
1735 /* Static data member template instantiations, however, cannot
1736 have multiple copies. */
1737 if (DECL_INITIAL (decl) == 0
1738 || DECL_INITIAL (decl) == error_mark_node)
1739 DECL_COMMON (decl) = 1;
1740 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1742 DECL_COMMON (decl) = 1;
1743 DECL_INITIAL (decl) = error_mark_node;
1745 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1747 /* We can't do anything useful; leave vars for explicit
1748 instantiation. */
1749 DECL_EXTERNAL (decl) = 1;
1750 DECL_NOT_REALLY_EXTERN (decl) = 0;
1754 DECL_COMDAT (decl) = 1;
1757 /* For win32 we also want to put explicit instantiations in
1758 linkonce sections, so that they will be merged with implicit
1759 instantiations; otherwise we get duplicate symbol errors.
1760 For Darwin we do not want explicit instantiations to be
1761 linkonce. */
1763 void
1764 maybe_make_one_only (tree decl)
1766 /* We used to say that this was not necessary on targets that support weak
1767 symbols, because the implicit instantiations will defer to the explicit
1768 one. However, that's not actually the case in SVR4; a strong definition
1769 after a weak one is an error. Also, not making explicit
1770 instantiations one_only means that we can end up with two copies of
1771 some template instantiations. */
1772 if (! flag_weak)
1773 return;
1775 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1776 we can get away with not emitting them if they aren't used. We need
1777 to for variables so that cp_finish_decl will update their linkage,
1778 because their DECL_INITIAL may not have been set properly yet. */
1780 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1781 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1782 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1784 make_decl_one_only (decl, cxx_comdat_group (decl));
1786 if (VAR_P (decl))
1788 varpool_node *node = varpool_node_for_decl (decl);
1789 DECL_COMDAT (decl) = 1;
1790 /* Mark it needed so we don't forget to emit it. */
1791 node->forced_by_abi = true;
1792 TREE_USED (decl) = 1;
1797 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1798 This predicate will give the right answer during parsing of the
1799 function, which other tests may not. */
1801 bool
1802 vague_linkage_p (tree decl)
1804 /* Unfortunately, import_export_decl has not always been called
1805 before the function is processed, so we cannot simply check
1806 DECL_COMDAT. */
1807 return (DECL_COMDAT (decl)
1808 || (((TREE_CODE (decl) == FUNCTION_DECL
1809 && DECL_DECLARED_INLINE_P (decl))
1810 || (DECL_LANG_SPECIFIC (decl)
1811 && DECL_TEMPLATE_INSTANTIATION (decl)))
1812 && TREE_PUBLIC (decl)));
1815 /* Determine whether or not we want to specifically import or export CTYPE,
1816 using various heuristics. */
1818 static void
1819 import_export_class (tree ctype)
1821 /* -1 for imported, 1 for exported. */
1822 int import_export = 0;
1824 /* It only makes sense to call this function at EOF. The reason is
1825 that this function looks at whether or not the first non-inline
1826 non-abstract virtual member function has been defined in this
1827 translation unit. But, we can't possibly know that until we've
1828 seen the entire translation unit. */
1829 gcc_assert (at_eof);
1831 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1832 return;
1834 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1835 we will have CLASSTYPE_INTERFACE_ONLY set but not
1836 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1837 heuristic because someone will supply a #pragma implementation
1838 elsewhere, and deducing it here would produce a conflict. */
1839 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1840 return;
1842 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1843 import_export = -1;
1844 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1845 import_export = 1;
1846 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1847 && !flag_implicit_templates)
1848 /* For a template class, without -fimplicit-templates, check the
1849 repository. If the virtual table is assigned to this
1850 translation unit, then export the class; otherwise, import
1851 it. */
1852 import_export = repo_export_class_p (ctype) ? 1 : -1;
1853 else if (TYPE_POLYMORPHIC_P (ctype))
1855 /* The ABI specifies that the virtual table and associated
1856 information are emitted with the key method, if any. */
1857 tree method = CLASSTYPE_KEY_METHOD (ctype);
1858 /* If weak symbol support is not available, then we must be
1859 careful not to emit the vtable when the key function is
1860 inline. An inline function can be defined in multiple
1861 translation units. If we were to emit the vtable in each
1862 translation unit containing a definition, we would get
1863 multiple definition errors at link-time. */
1864 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1865 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1868 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1869 a definition anywhere else. */
1870 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1871 import_export = 0;
1873 /* Allow back ends the chance to overrule the decision. */
1874 if (targetm.cxx.import_export_class)
1875 import_export = targetm.cxx.import_export_class (ctype, import_export);
1877 if (import_export)
1879 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1880 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1884 /* Return true if VAR has already been provided to the back end; in that
1885 case VAR should not be modified further by the front end. */
1886 static bool
1887 var_finalized_p (tree var)
1889 return varpool_node_for_decl (var)->definition;
1892 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1893 must be emitted in this translation unit. Mark it as such. */
1895 void
1896 mark_needed (tree decl)
1898 TREE_USED (decl) = 1;
1899 if (TREE_CODE (decl) == FUNCTION_DECL)
1901 /* Extern inline functions don't become needed when referenced.
1902 If we know a method will be emitted in other TU and no new
1903 functions can be marked reachable, just use the external
1904 definition. */
1905 struct cgraph_node *node = cgraph_get_create_node (decl);
1906 node->forced_by_abi = true;
1908 else if (TREE_CODE (decl) == VAR_DECL)
1910 varpool_node *node = varpool_node_for_decl (decl);
1911 /* C++ frontend use mark_decl_references to force COMDAT variables
1912 to be output that might appear dead otherwise. */
1913 node->forced_by_abi = true;
1917 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1918 returns true if a definition of this entity should be provided in
1919 this object file. Callers use this function to determine whether
1920 or not to let the back end know that a definition of DECL is
1921 available in this translation unit. */
1923 bool
1924 decl_needed_p (tree decl)
1926 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1927 /* This function should only be called at the end of the translation
1928 unit. We cannot be sure of whether or not something will be
1929 COMDAT until that point. */
1930 gcc_assert (at_eof);
1932 /* All entities with external linkage that are not COMDAT should be
1933 emitted; they may be referred to from other object files. */
1934 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1935 return true;
1936 /* If this entity was used, let the back end see it; it will decide
1937 whether or not to emit it into the object file. */
1938 if (TREE_USED (decl))
1939 return true;
1940 /* Functions marked "dllexport" must be emitted so that they are
1941 visible to other DLLs. */
1942 if (flag_keep_inline_dllexport
1943 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1944 return true;
1945 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1946 reference to DECL might cause it to be emitted later. */
1947 return false;
1950 /* If necessary, write out the vtables for the dynamic class CTYPE.
1951 Returns true if any vtables were emitted. */
1953 static bool
1954 maybe_emit_vtables (tree ctype)
1956 tree vtbl;
1957 tree primary_vtbl;
1958 int needed = 0;
1959 varpool_node *current = NULL, *last = NULL;
1961 /* If the vtables for this class have already been emitted there is
1962 nothing more to do. */
1963 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1964 if (var_finalized_p (primary_vtbl))
1965 return false;
1966 /* Ignore dummy vtables made by get_vtable_decl. */
1967 if (TREE_TYPE (primary_vtbl) == void_type_node)
1968 return false;
1970 /* On some targets, we cannot determine the key method until the end
1971 of the translation unit -- which is when this function is
1972 called. */
1973 if (!targetm.cxx.key_method_may_be_inline ())
1974 determine_key_method (ctype);
1976 /* See if any of the vtables are needed. */
1977 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1979 import_export_decl (vtbl);
1980 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1981 needed = 1;
1983 if (!needed)
1985 /* If the references to this class' vtables are optimized away,
1986 still emit the appropriate debugging information. See
1987 dfs_debug_mark. */
1988 if (DECL_COMDAT (primary_vtbl)
1989 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1990 note_debug_info_needed (ctype);
1991 return false;
1994 /* The ABI requires that we emit all of the vtables if we emit any
1995 of them. */
1996 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1998 /* Mark entities references from the virtual table as used. */
1999 mark_vtable_entries (vtbl);
2001 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2003 vec<tree, va_gc> *cleanups = NULL;
2004 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2005 LOOKUP_NORMAL);
2007 /* It had better be all done at compile-time. */
2008 gcc_assert (!expr && !cleanups);
2011 /* Write it out. */
2012 DECL_EXTERNAL (vtbl) = 0;
2013 rest_of_decl_compilation (vtbl, 1, 1);
2015 /* Because we're only doing syntax-checking, we'll never end up
2016 actually marking the variable as written. */
2017 if (flag_syntax_only)
2018 TREE_ASM_WRITTEN (vtbl) = 1;
2019 else if (DECL_ONE_ONLY (vtbl))
2021 current = varpool_node_for_decl (vtbl);
2022 if (last)
2023 symtab_add_to_same_comdat_group (current, last);
2024 last = current;
2028 /* Since we're writing out the vtable here, also write the debug
2029 info. */
2030 note_debug_info_needed (ctype);
2032 return true;
2035 /* A special return value from type_visibility meaning internal
2036 linkage. */
2038 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2040 /* walk_tree helper function for type_visibility. */
2042 static tree
2043 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2045 int *vis_p = (int *)data;
2046 if (! TYPE_P (*tp))
2048 *walk_subtrees = 0;
2050 else if (OVERLOAD_TYPE_P (*tp)
2051 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2053 *vis_p = VISIBILITY_ANON;
2054 return *tp;
2056 else if (CLASS_TYPE_P (*tp)
2057 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2058 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2059 return NULL;
2062 /* Returns the visibility of TYPE, which is the minimum visibility of its
2063 component types. */
2065 static int
2066 type_visibility (tree type)
2068 int vis = VISIBILITY_DEFAULT;
2069 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2070 return vis;
2073 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2074 specified (or if VISIBILITY is static). If TMPL is true, this
2075 constraint is for a template argument, and takes precedence
2076 over explicitly-specified visibility on the template. */
2078 static void
2079 constrain_visibility (tree decl, int visibility, bool tmpl)
2081 if (visibility == VISIBILITY_ANON)
2083 /* extern "C" declarations aren't affected by the anonymous
2084 namespace. */
2085 if (!DECL_EXTERN_C_P (decl))
2087 TREE_PUBLIC (decl) = 0;
2088 DECL_WEAK (decl) = 0;
2089 DECL_COMMON (decl) = 0;
2090 DECL_COMDAT_GROUP (decl) = NULL_TREE;
2091 DECL_INTERFACE_KNOWN (decl) = 1;
2092 if (DECL_LANG_SPECIFIC (decl))
2093 DECL_NOT_REALLY_EXTERN (decl) = 1;
2096 else if (visibility > DECL_VISIBILITY (decl)
2097 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2099 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2100 /* This visibility was not specified. */
2101 DECL_VISIBILITY_SPECIFIED (decl) = false;
2105 /* Constrain the visibility of DECL based on the visibility of its template
2106 arguments. */
2108 static void
2109 constrain_visibility_for_template (tree decl, tree targs)
2111 /* If this is a template instantiation, check the innermost
2112 template args for visibility constraints. The outer template
2113 args are covered by the class check. */
2114 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2115 int i;
2116 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2118 int vis = 0;
2120 tree arg = TREE_VEC_ELT (args, i-1);
2121 if (TYPE_P (arg))
2122 vis = type_visibility (arg);
2123 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2125 STRIP_NOPS (arg);
2126 if (TREE_CODE (arg) == ADDR_EXPR)
2127 arg = TREE_OPERAND (arg, 0);
2128 if (VAR_OR_FUNCTION_DECL_P (arg))
2130 if (! TREE_PUBLIC (arg))
2131 vis = VISIBILITY_ANON;
2132 else
2133 vis = DECL_VISIBILITY (arg);
2136 if (vis)
2137 constrain_visibility (decl, vis, true);
2141 /* Like c_determine_visibility, but with additional C++-specific
2142 behavior.
2144 Function-scope entities can rely on the function's visibility because
2145 it is set in start_preparsed_function.
2147 Class-scope entities cannot rely on the class's visibility until the end
2148 of the enclosing class definition.
2150 Note that because namespaces have multiple independent definitions,
2151 namespace visibility is handled elsewhere using the #pragma visibility
2152 machinery rather than by decorating the namespace declaration.
2154 The goal is for constraints from the type to give a diagnostic, and
2155 other constraints to be applied silently. */
2157 void
2158 determine_visibility (tree decl)
2160 tree class_type = NULL_TREE;
2161 bool use_template;
2162 bool orig_visibility_specified;
2163 enum symbol_visibility orig_visibility;
2165 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2167 /* Only relevant for names with external linkage. */
2168 if (!TREE_PUBLIC (decl))
2169 return;
2171 /* Cloned constructors and destructors get the same visibility as
2172 the underlying function. That should be set up in
2173 maybe_clone_body. */
2174 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2176 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2177 orig_visibility = DECL_VISIBILITY (decl);
2179 if (TREE_CODE (decl) == TYPE_DECL)
2181 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2182 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2183 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2184 use_template = 1;
2185 else
2186 use_template = 0;
2188 else if (DECL_LANG_SPECIFIC (decl))
2189 use_template = DECL_USE_TEMPLATE (decl);
2190 else
2191 use_template = 0;
2193 /* If DECL is a member of a class, visibility specifiers on the
2194 class can influence the visibility of the DECL. */
2195 if (DECL_CLASS_SCOPE_P (decl))
2196 class_type = DECL_CONTEXT (decl);
2197 else
2199 /* Not a class member. */
2201 /* Virtual tables have DECL_CONTEXT set to their associated class,
2202 so they are automatically handled above. */
2203 gcc_assert (!VAR_P (decl)
2204 || !DECL_VTABLE_OR_VTT_P (decl));
2206 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2208 /* Local statics and classes get the visibility of their
2209 containing function by default, except that
2210 -fvisibility-inlines-hidden doesn't affect them. */
2211 tree fn = DECL_CONTEXT (decl);
2212 if (DECL_VISIBILITY_SPECIFIED (fn))
2214 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2215 DECL_VISIBILITY_SPECIFIED (decl) =
2216 DECL_VISIBILITY_SPECIFIED (fn);
2218 else
2220 if (DECL_CLASS_SCOPE_P (fn))
2221 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2222 else if (determine_hidden_inline (fn))
2224 DECL_VISIBILITY (decl) = default_visibility;
2225 DECL_VISIBILITY_SPECIFIED (decl) =
2226 visibility_options.inpragma;
2228 else
2230 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2231 DECL_VISIBILITY_SPECIFIED (decl) =
2232 DECL_VISIBILITY_SPECIFIED (fn);
2236 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2237 but have no TEMPLATE_INFO, so don't try to check it. */
2238 use_template = 0;
2240 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2241 && flag_visibility_ms_compat)
2243 /* Under -fvisibility-ms-compat, types are visible by default,
2244 even though their contents aren't. */
2245 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2246 int underlying_vis = type_visibility (underlying_type);
2247 if (underlying_vis == VISIBILITY_ANON
2248 || (CLASS_TYPE_P (underlying_type)
2249 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2250 constrain_visibility (decl, underlying_vis, false);
2251 else
2252 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2254 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2256 /* tinfo visibility is based on the type it's for. */
2257 constrain_visibility
2258 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2260 /* Give the target a chance to override the visibility associated
2261 with DECL. */
2262 if (TREE_PUBLIC (decl)
2263 && !DECL_REALLY_EXTERN (decl)
2264 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2265 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2266 targetm.cxx.determine_class_data_visibility (decl);
2268 else if (use_template)
2269 /* Template instantiations and specializations get visibility based
2270 on their template unless they override it with an attribute. */;
2271 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2273 if (determine_hidden_inline (decl))
2274 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2275 else
2277 /* Set default visibility to whatever the user supplied with
2278 #pragma GCC visibility or a namespace visibility attribute. */
2279 DECL_VISIBILITY (decl) = default_visibility;
2280 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2285 if (use_template)
2287 /* If the specialization doesn't specify visibility, use the
2288 visibility from the template. */
2289 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2290 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2291 : DECL_TEMPLATE_INFO (decl));
2292 tree args = TI_ARGS (tinfo);
2293 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2294 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2295 : DECL_ATTRIBUTES (decl));
2297 if (args != error_mark_node)
2299 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2301 if (!DECL_VISIBILITY_SPECIFIED (decl))
2303 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2304 && determine_hidden_inline (decl))
2305 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2306 else
2308 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2309 DECL_VISIBILITY_SPECIFIED (decl)
2310 = DECL_VISIBILITY_SPECIFIED (pattern);
2314 if (args
2315 /* Template argument visibility outweighs #pragma or namespace
2316 visibility, but not an explicit attribute. */
2317 && !lookup_attribute ("visibility", attribs))
2319 int depth = TMPL_ARGS_DEPTH (args);
2320 if (DECL_VISIBILITY_SPECIFIED (decl))
2322 /* A class template member with explicit visibility
2323 overrides the class visibility, so we need to apply
2324 all the levels of template args directly. */
2325 int i;
2326 for (i = 1; i <= depth; ++i)
2328 tree lev = TMPL_ARGS_LEVEL (args, i);
2329 constrain_visibility_for_template (decl, lev);
2332 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2333 /* Limit visibility based on its template arguments. */
2334 constrain_visibility_for_template (decl, args);
2339 if (class_type)
2340 determine_visibility_from_class (decl, class_type);
2342 if (decl_anon_ns_mem_p (decl))
2343 /* Names in an anonymous namespace get internal linkage.
2344 This might change once we implement export. */
2345 constrain_visibility (decl, VISIBILITY_ANON, false);
2346 else if (TREE_CODE (decl) != TYPE_DECL)
2348 /* Propagate anonymity from type to decl. */
2349 int tvis = type_visibility (TREE_TYPE (decl));
2350 if (tvis == VISIBILITY_ANON
2351 || ! DECL_VISIBILITY_SPECIFIED (decl))
2352 constrain_visibility (decl, tvis, false);
2354 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2355 /* DR 757: A type without linkage shall not be used as the type of a
2356 variable or function with linkage, unless
2357 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2358 o the variable or function is not used (3.2 [basic.def.odr]) or is
2359 defined in the same translation unit.
2361 Since non-extern "C" decls need to be defined in the same
2362 translation unit, we can make the type internal. */
2363 constrain_visibility (decl, VISIBILITY_ANON, false);
2365 /* If visibility changed and DECL already has DECL_RTL, ensure
2366 symbol flags are updated. */
2367 if ((DECL_VISIBILITY (decl) != orig_visibility
2368 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2369 && ((VAR_P (decl) && TREE_STATIC (decl))
2370 || TREE_CODE (decl) == FUNCTION_DECL)
2371 && DECL_RTL_SET_P (decl))
2372 make_decl_rtl (decl);
2375 /* By default, static data members and function members receive
2376 the visibility of their containing class. */
2378 static void
2379 determine_visibility_from_class (tree decl, tree class_type)
2381 if (DECL_VISIBILITY_SPECIFIED (decl))
2382 return;
2384 if (determine_hidden_inline (decl))
2385 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2386 else
2388 /* Default to the class visibility. */
2389 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2390 DECL_VISIBILITY_SPECIFIED (decl)
2391 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2394 /* Give the target a chance to override the visibility associated
2395 with DECL. */
2396 if (VAR_P (decl)
2397 && (DECL_TINFO_P (decl)
2398 || (DECL_VTABLE_OR_VTT_P (decl)
2399 /* Construction virtual tables are not exported because
2400 they cannot be referred to from other object files;
2401 their name is not standardized by the ABI. */
2402 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2403 && TREE_PUBLIC (decl)
2404 && !DECL_REALLY_EXTERN (decl)
2405 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2406 targetm.cxx.determine_class_data_visibility (decl);
2409 /* Returns true iff DECL is an inline that should get hidden visibility
2410 because of -fvisibility-inlines-hidden. */
2412 static bool
2413 determine_hidden_inline (tree decl)
2415 return (visibility_options.inlines_hidden
2416 /* Don't do this for inline templates; specializations might not be
2417 inline, and we don't want them to inherit the hidden
2418 visibility. We'll set it here for all inline instantiations. */
2419 && !processing_template_decl
2420 && TREE_CODE (decl) == FUNCTION_DECL
2421 && DECL_DECLARED_INLINE_P (decl)
2422 && (! DECL_LANG_SPECIFIC (decl)
2423 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2426 /* Constrain the visibility of a class TYPE based on the visibility of its
2427 field types. Warn if any fields require lesser visibility. */
2429 void
2430 constrain_class_visibility (tree type)
2432 tree binfo;
2433 tree t;
2434 int i;
2436 int vis = type_visibility (type);
2438 if (vis == VISIBILITY_ANON
2439 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2440 return;
2442 /* Don't warn about visibility if the class has explicit visibility. */
2443 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2444 vis = VISIBILITY_INTERNAL;
2446 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2447 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2449 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2450 int subvis = type_visibility (ftype);
2452 if (subvis == VISIBILITY_ANON)
2454 if (!in_main_input_context ())
2455 warning (0, "\
2456 %qT has a field %qD whose type uses the anonymous namespace",
2457 type, t);
2459 else if (MAYBE_CLASS_TYPE_P (ftype)
2460 && vis < VISIBILITY_HIDDEN
2461 && subvis >= VISIBILITY_HIDDEN)
2462 warning (OPT_Wattributes, "\
2463 %qT declared with greater visibility than the type of its field %qD",
2464 type, t);
2467 binfo = TYPE_BINFO (type);
2468 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2470 int subvis = type_visibility (TREE_TYPE (t));
2472 if (subvis == VISIBILITY_ANON)
2474 if (!in_main_input_context())
2475 warning (0, "\
2476 %qT has a base %qT whose type uses the anonymous namespace",
2477 type, TREE_TYPE (t));
2479 else if (vis < VISIBILITY_HIDDEN
2480 && subvis >= VISIBILITY_HIDDEN)
2481 warning (OPT_Wattributes, "\
2482 %qT declared with greater visibility than its base %qT",
2483 type, TREE_TYPE (t));
2487 /* Functions for adjusting the visibility of a tagged type and its nested
2488 types and declarations when it gets a name for linkage purposes from a
2489 typedef. */
2491 static void bt_reset_linkage_1 (binding_entry, void *);
2492 static void bt_reset_linkage_2 (binding_entry, void *);
2494 /* First reset the visibility of all the types. */
2496 static void
2497 reset_type_linkage_1 (tree type)
2499 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2500 if (CLASS_TYPE_P (type))
2501 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2502 bt_reset_linkage_1, NULL);
2504 static void
2505 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2507 reset_type_linkage_1 (b->type);
2510 /* Then reset the visibility of any static data members or member
2511 functions that use those types. */
2513 static void
2514 reset_decl_linkage (tree decl)
2516 if (TREE_PUBLIC (decl))
2517 return;
2518 if (DECL_CLONED_FUNCTION_P (decl))
2519 return;
2520 TREE_PUBLIC (decl) = true;
2521 DECL_INTERFACE_KNOWN (decl) = false;
2522 determine_visibility (decl);
2523 tentative_decl_linkage (decl);
2525 static void
2526 reset_type_linkage_2 (tree type)
2528 if (CLASS_TYPE_P (type))
2530 if (tree vt = CLASSTYPE_VTABLES (type))
2532 tree name = mangle_vtbl_for_type (type);
2533 DECL_NAME (vt) = name;
2534 SET_DECL_ASSEMBLER_NAME (vt, name);
2535 reset_decl_linkage (vt);
2537 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2539 tree name = mangle_typeinfo_for_type (type);
2540 DECL_NAME (ti) = name;
2541 SET_DECL_ASSEMBLER_NAME (ti, name);
2542 TREE_TYPE (name) = type;
2543 reset_decl_linkage (ti);
2545 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2546 if (TREE_CODE (m) == VAR_DECL)
2547 reset_decl_linkage (m);
2548 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2549 reset_decl_linkage (m);
2550 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2551 bt_reset_linkage_2, NULL);
2554 static void
2555 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2557 reset_type_linkage_2 (b->type);
2559 void
2560 reset_type_linkage (tree type)
2562 reset_type_linkage_1 (type);
2563 reset_type_linkage_2 (type);
2566 /* Set up our initial idea of what the linkage of DECL should be. */
2568 void
2569 tentative_decl_linkage (tree decl)
2571 if (DECL_INTERFACE_KNOWN (decl))
2572 /* We've already made a decision as to how this function will
2573 be handled. */;
2574 else if (vague_linkage_p (decl))
2576 if (TREE_CODE (decl) == FUNCTION_DECL
2577 && decl_defined_p (decl))
2579 DECL_EXTERNAL (decl) = 1;
2580 DECL_NOT_REALLY_EXTERN (decl) = 1;
2581 note_vague_linkage_fn (decl);
2582 /* A non-template inline function with external linkage will
2583 always be COMDAT. As we must eventually determine the
2584 linkage of all functions, and as that causes writes to
2585 the data mapped in from the PCH file, it's advantageous
2586 to mark the functions at this point. */
2587 if (DECL_DECLARED_INLINE_P (decl)
2588 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2589 || DECL_DEFAULTED_FN (decl)))
2591 /* This function must have external linkage, as
2592 otherwise DECL_INTERFACE_KNOWN would have been
2593 set. */
2594 gcc_assert (TREE_PUBLIC (decl));
2595 comdat_linkage (decl);
2596 DECL_INTERFACE_KNOWN (decl) = 1;
2599 else if (TREE_CODE (decl) == VAR_DECL)
2600 maybe_commonize_var (decl);
2604 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2605 for DECL has not already been determined, do so now by setting
2606 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2607 function is called entities with vague linkage whose definitions
2608 are available must have TREE_PUBLIC set.
2610 If this function decides to place DECL in COMDAT, it will set
2611 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2612 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2613 callers defer that decision until it is clear that DECL is actually
2614 required. */
2616 void
2617 import_export_decl (tree decl)
2619 int emit_p;
2620 bool comdat_p;
2621 bool import_p;
2622 tree class_type = NULL_TREE;
2624 if (DECL_INTERFACE_KNOWN (decl))
2625 return;
2627 /* We cannot determine what linkage to give to an entity with vague
2628 linkage until the end of the file. For example, a virtual table
2629 for a class will be defined if and only if the key method is
2630 defined in this translation unit. As a further example, consider
2631 that when compiling a translation unit that uses PCH file with
2632 "-frepo" it would be incorrect to make decisions about what
2633 entities to emit when building the PCH; those decisions must be
2634 delayed until the repository information has been processed. */
2635 gcc_assert (at_eof);
2636 /* Object file linkage for explicit instantiations is handled in
2637 mark_decl_instantiated. For static variables in functions with
2638 vague linkage, maybe_commonize_var is used.
2640 Therefore, the only declarations that should be provided to this
2641 function are those with external linkage that are:
2643 * implicit instantiations of function templates
2645 * inline function
2647 * implicit instantiations of static data members of class
2648 templates
2650 * virtual tables
2652 * typeinfo objects
2654 Furthermore, all entities that reach this point must have a
2655 definition available in this translation unit.
2657 The following assertions check these conditions. */
2658 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2659 /* Any code that creates entities with TREE_PUBLIC cleared should
2660 also set DECL_INTERFACE_KNOWN. */
2661 gcc_assert (TREE_PUBLIC (decl));
2662 if (TREE_CODE (decl) == FUNCTION_DECL)
2663 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2664 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2665 || DECL_DECLARED_INLINE_P (decl));
2666 else
2667 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2668 || DECL_VTABLE_OR_VTT_P (decl)
2669 || DECL_TINFO_P (decl));
2670 /* Check that a definition of DECL is available in this translation
2671 unit. */
2672 gcc_assert (!DECL_REALLY_EXTERN (decl));
2674 /* Assume that DECL will not have COMDAT linkage. */
2675 comdat_p = false;
2676 /* Assume that DECL will not be imported into this translation
2677 unit. */
2678 import_p = false;
2680 /* See if the repository tells us whether or not to emit DECL in
2681 this translation unit. */
2682 emit_p = repo_emit_p (decl);
2683 if (emit_p == 0)
2684 import_p = true;
2685 else if (emit_p == 1)
2687 /* The repository indicates that this entity should be defined
2688 here. Make sure the back end honors that request. */
2689 if (VAR_P (decl))
2690 mark_needed (decl);
2691 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2692 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2694 tree clone;
2695 FOR_EACH_CLONE (clone, decl)
2696 mark_needed (clone);
2698 else
2699 mark_needed (decl);
2700 /* Output the definition as an ordinary strong definition. */
2701 DECL_EXTERNAL (decl) = 0;
2702 DECL_INTERFACE_KNOWN (decl) = 1;
2703 return;
2706 if (import_p)
2707 /* We have already decided what to do with this DECL; there is no
2708 need to check anything further. */
2710 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2712 class_type = DECL_CONTEXT (decl);
2713 import_export_class (class_type);
2714 if (TYPE_FOR_JAVA (class_type))
2715 import_p = true;
2716 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2717 && CLASSTYPE_INTERFACE_ONLY (class_type))
2718 import_p = true;
2719 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2720 && !CLASSTYPE_USE_TEMPLATE (class_type)
2721 && CLASSTYPE_KEY_METHOD (class_type)
2722 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2723 /* The ABI requires that all virtual tables be emitted with
2724 COMDAT linkage. However, on systems where COMDAT symbols
2725 don't show up in the table of contents for a static
2726 archive, or on systems without weak symbols (where we
2727 approximate COMDAT linkage by using internal linkage), the
2728 linker will report errors about undefined symbols because
2729 it will not see the virtual table definition. Therefore,
2730 in the case that we know that the virtual table will be
2731 emitted in only one translation unit, we make the virtual
2732 table an ordinary definition with external linkage. */
2733 DECL_EXTERNAL (decl) = 0;
2734 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2736 /* CLASS_TYPE is being exported from this translation unit,
2737 so DECL should be defined here. */
2738 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2739 /* If a class is declared in a header with the "extern
2740 template" extension, then it will not be instantiated,
2741 even in translation units that would normally require
2742 it. Often such classes are explicitly instantiated in
2743 one translation unit. Therefore, the explicit
2744 instantiation must be made visible to other translation
2745 units. */
2746 DECL_EXTERNAL (decl) = 0;
2747 else
2749 /* The generic C++ ABI says that class data is always
2750 COMDAT, even if there is a key function. Some
2751 variants (e.g., the ARM EABI) says that class data
2752 only has COMDAT linkage if the class data might be
2753 emitted in more than one translation unit. When the
2754 key method can be inline and is inline, we still have
2755 to arrange for comdat even though
2756 class_data_always_comdat is false. */
2757 if (!CLASSTYPE_KEY_METHOD (class_type)
2758 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2759 || targetm.cxx.class_data_always_comdat ())
2761 /* The ABI requires COMDAT linkage. Normally, we
2762 only emit COMDAT things when they are needed;
2763 make sure that we realize that this entity is
2764 indeed needed. */
2765 comdat_p = true;
2766 mark_needed (decl);
2770 else if (!flag_implicit_templates
2771 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2772 import_p = true;
2773 else
2774 comdat_p = true;
2776 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2778 tree type = TREE_TYPE (DECL_NAME (decl));
2779 if (CLASS_TYPE_P (type))
2781 class_type = type;
2782 import_export_class (type);
2783 if (CLASSTYPE_INTERFACE_KNOWN (type)
2784 && TYPE_POLYMORPHIC_P (type)
2785 && CLASSTYPE_INTERFACE_ONLY (type)
2786 /* If -fno-rtti was specified, then we cannot be sure
2787 that RTTI information will be emitted with the
2788 virtual table of the class, so we must emit it
2789 wherever it is used. */
2790 && flag_rtti)
2791 import_p = true;
2792 else
2794 if (CLASSTYPE_INTERFACE_KNOWN (type)
2795 && !CLASSTYPE_INTERFACE_ONLY (type))
2797 comdat_p = (targetm.cxx.class_data_always_comdat ()
2798 || (CLASSTYPE_KEY_METHOD (type)
2799 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2800 mark_needed (decl);
2801 if (!flag_weak)
2803 comdat_p = false;
2804 DECL_EXTERNAL (decl) = 0;
2807 else
2808 comdat_p = true;
2811 else
2812 comdat_p = true;
2814 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2816 /* DECL is an implicit instantiation of a function or static
2817 data member. */
2818 if ((flag_implicit_templates
2819 && !flag_use_repository)
2820 || (flag_implicit_inline_templates
2821 && TREE_CODE (decl) == FUNCTION_DECL
2822 && DECL_DECLARED_INLINE_P (decl)))
2823 comdat_p = true;
2824 else
2825 /* If we are not implicitly generating templates, then mark
2826 this entity as undefined in this translation unit. */
2827 import_p = true;
2829 else if (DECL_FUNCTION_MEMBER_P (decl))
2831 if (!DECL_DECLARED_INLINE_P (decl))
2833 tree ctype = DECL_CONTEXT (decl);
2834 import_export_class (ctype);
2835 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2837 DECL_NOT_REALLY_EXTERN (decl)
2838 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2839 || (DECL_DECLARED_INLINE_P (decl)
2840 && ! flag_implement_inlines
2841 && !DECL_VINDEX (decl)));
2843 if (!DECL_NOT_REALLY_EXTERN (decl))
2844 DECL_EXTERNAL (decl) = 1;
2846 /* Always make artificials weak. */
2847 if (DECL_ARTIFICIAL (decl) && flag_weak)
2848 comdat_p = true;
2849 else
2850 maybe_make_one_only (decl);
2853 else
2854 comdat_p = true;
2856 else
2857 comdat_p = true;
2859 if (import_p)
2861 /* If we are importing DECL into this translation unit, mark is
2862 an undefined here. */
2863 DECL_EXTERNAL (decl) = 1;
2864 DECL_NOT_REALLY_EXTERN (decl) = 0;
2866 else if (comdat_p)
2868 /* If we decided to put DECL in COMDAT, mark it accordingly at
2869 this point. */
2870 comdat_linkage (decl);
2873 DECL_INTERFACE_KNOWN (decl) = 1;
2876 /* Return an expression that performs the destruction of DECL, which
2877 must be a VAR_DECL whose type has a non-trivial destructor, or is
2878 an array whose (innermost) elements have a non-trivial destructor. */
2880 tree
2881 build_cleanup (tree decl)
2883 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2884 gcc_assert (clean != NULL_TREE);
2885 return clean;
2888 /* Returns the initialization guard variable for the variable DECL,
2889 which has static storage duration. */
2891 tree
2892 get_guard (tree decl)
2894 tree sname;
2895 tree guard;
2897 sname = mangle_guard_variable (decl);
2898 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2899 if (! guard)
2901 tree guard_type;
2903 /* We use a type that is big enough to contain a mutex as well
2904 as an integer counter. */
2905 guard_type = targetm.cxx.guard_type ();
2906 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2907 VAR_DECL, sname, guard_type);
2909 /* The guard should have the same linkage as what it guards. */
2910 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2911 TREE_STATIC (guard) = TREE_STATIC (decl);
2912 DECL_COMMON (guard) = DECL_COMMON (decl);
2913 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2914 DECL_TLS_MODEL (guard) = DECL_TLS_MODEL (decl);
2915 if (DECL_ONE_ONLY (decl))
2916 make_decl_one_only (guard, cxx_comdat_group (guard));
2917 if (TREE_PUBLIC (decl))
2918 DECL_WEAK (guard) = DECL_WEAK (decl);
2919 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2920 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2922 DECL_ARTIFICIAL (guard) = 1;
2923 DECL_IGNORED_P (guard) = 1;
2924 TREE_USED (guard) = 1;
2925 pushdecl_top_level_and_finish (guard, NULL_TREE);
2927 return guard;
2930 /* Return those bits of the GUARD variable that should be set when the
2931 guarded entity is actually initialized. */
2933 static tree
2934 get_guard_bits (tree guard)
2936 if (!targetm.cxx.guard_mask_bit ())
2938 /* We only set the first byte of the guard, in order to leave room
2939 for a mutex in the high-order bits. */
2940 guard = build1 (ADDR_EXPR,
2941 build_pointer_type (TREE_TYPE (guard)),
2942 guard);
2943 guard = build1 (NOP_EXPR,
2944 build_pointer_type (char_type_node),
2945 guard);
2946 guard = build1 (INDIRECT_REF, char_type_node, guard);
2949 return guard;
2952 /* Return an expression which determines whether or not the GUARD
2953 variable has already been initialized. */
2955 tree
2956 get_guard_cond (tree guard)
2958 tree guard_value;
2960 /* Check to see if the GUARD is zero. */
2961 guard = get_guard_bits (guard);
2963 /* Mask off all but the low bit. */
2964 if (targetm.cxx.guard_mask_bit ())
2966 guard_value = integer_one_node;
2967 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2968 guard_value = convert (TREE_TYPE (guard), guard_value);
2969 guard = cp_build_binary_op (input_location,
2970 BIT_AND_EXPR, guard, guard_value,
2971 tf_warning_or_error);
2974 guard_value = integer_zero_node;
2975 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2976 guard_value = convert (TREE_TYPE (guard), guard_value);
2977 return cp_build_binary_op (input_location,
2978 EQ_EXPR, guard, guard_value,
2979 tf_warning_or_error);
2982 /* Return an expression which sets the GUARD variable, indicating that
2983 the variable being guarded has been initialized. */
2985 tree
2986 set_guard (tree guard)
2988 tree guard_init;
2990 /* Set the GUARD to one. */
2991 guard = get_guard_bits (guard);
2992 guard_init = integer_one_node;
2993 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2994 guard_init = convert (TREE_TYPE (guard), guard_init);
2995 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2996 tf_warning_or_error);
2999 /* Returns true iff we can tell that VAR does not have a dynamic
3000 initializer. */
3002 static bool
3003 var_defined_without_dynamic_init (tree var)
3005 /* If it's defined in another TU, we can't tell. */
3006 if (DECL_EXTERNAL (var))
3007 return false;
3008 /* If it has a non-trivial destructor, registering the destructor
3009 counts as dynamic initialization. */
3010 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3011 return false;
3012 /* If it's in this TU, its initializer has been processed. */
3013 gcc_assert (DECL_INITIALIZED_P (var));
3014 /* If it has no initializer or a constant one, it's not dynamic. */
3015 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3016 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3019 /* Returns true iff VAR is a variable that needs uses to be
3020 wrapped for possible dynamic initialization. */
3022 static bool
3023 var_needs_tls_wrapper (tree var)
3025 return (!error_operand_p (var)
3026 && DECL_THREAD_LOCAL_P (var)
3027 && !DECL_GNU_TLS_P (var)
3028 && !DECL_FUNCTION_SCOPE_P (var)
3029 && !var_defined_without_dynamic_init (var));
3032 /* Get the FUNCTION_DECL for the shared TLS init function for this
3033 translation unit. */
3035 static tree
3036 get_local_tls_init_fn (void)
3038 tree sname = get_identifier ("__tls_init");
3039 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3040 if (!fn)
3042 fn = build_lang_decl (FUNCTION_DECL, sname,
3043 build_function_type (void_type_node,
3044 void_list_node));
3045 SET_DECL_LANGUAGE (fn, lang_c);
3046 TREE_PUBLIC (fn) = false;
3047 DECL_ARTIFICIAL (fn) = true;
3048 mark_used (fn);
3049 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3051 return fn;
3054 /* Get a FUNCTION_DECL for the init function for the thread_local
3055 variable VAR. The init function will be an alias to the function
3056 that initializes all the non-local TLS variables in the translation
3057 unit. The init function is only used by the wrapper function. */
3059 static tree
3060 get_tls_init_fn (tree var)
3062 /* Only C++11 TLS vars need this init fn. */
3063 if (!var_needs_tls_wrapper (var))
3064 return NULL_TREE;
3066 /* If -fno-extern-tls-init, assume that we don't need to call
3067 a tls init function for a variable defined in another TU. */
3068 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3069 return NULL_TREE;
3071 #ifdef ASM_OUTPUT_DEF
3072 /* If the variable is internal, or if we can't generate aliases,
3073 call the local init function directly. */
3074 if (!TREE_PUBLIC (var))
3075 #endif
3076 return get_local_tls_init_fn ();
3078 tree sname = mangle_tls_init_fn (var);
3079 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3080 if (!fn)
3082 fn = build_lang_decl (FUNCTION_DECL, sname,
3083 build_function_type (void_type_node,
3084 void_list_node));
3085 SET_DECL_LANGUAGE (fn, lang_c);
3086 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3087 DECL_ARTIFICIAL (fn) = true;
3088 DECL_COMDAT (fn) = DECL_COMDAT (var);
3089 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3090 if (DECL_ONE_ONLY (var))
3091 make_decl_one_only (fn, cxx_comdat_group (fn));
3092 if (TREE_PUBLIC (var))
3094 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3095 /* If the variable is defined somewhere else and might have static
3096 initialization, make the init function a weak reference. */
3097 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3098 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3099 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3100 && DECL_EXTERNAL (var))
3101 declare_weak (fn);
3102 else
3103 DECL_WEAK (fn) = DECL_WEAK (var);
3105 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3106 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3107 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3108 DECL_IGNORED_P (fn) = 1;
3109 mark_used (fn);
3111 DECL_BEFRIENDING_CLASSES (fn) = var;
3113 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3115 return fn;
3118 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3119 variable VAR. The wrapper function calls the init function (if any) for
3120 VAR and then returns a reference to VAR. The wrapper function is used
3121 in place of VAR everywhere VAR is mentioned. */
3123 tree
3124 get_tls_wrapper_fn (tree var)
3126 /* Only C++11 TLS vars need this wrapper fn. */
3127 if (!var_needs_tls_wrapper (var))
3128 return NULL_TREE;
3130 tree sname = mangle_tls_wrapper_fn (var);
3131 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3132 if (!fn)
3134 /* A named rvalue reference is an lvalue, so the wrapper should
3135 always return an lvalue reference. */
3136 tree type = non_reference (TREE_TYPE (var));
3137 type = build_reference_type (type);
3138 tree fntype = build_function_type (type, void_list_node);
3139 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3140 SET_DECL_LANGUAGE (fn, lang_c);
3141 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3142 DECL_ARTIFICIAL (fn) = true;
3143 DECL_IGNORED_P (fn) = 1;
3144 /* The wrapper is inline and emitted everywhere var is used. */
3145 DECL_DECLARED_INLINE_P (fn) = true;
3146 if (TREE_PUBLIC (var))
3148 comdat_linkage (fn);
3149 #ifdef HAVE_GAS_HIDDEN
3150 /* Make the wrapper bind locally; there's no reason to share
3151 the wrapper between multiple shared objects. */
3152 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3153 DECL_VISIBILITY_SPECIFIED (fn) = true;
3154 #endif
3156 if (!TREE_PUBLIC (fn))
3157 DECL_INTERFACE_KNOWN (fn) = true;
3158 mark_used (fn);
3159 note_vague_linkage_fn (fn);
3161 #if 0
3162 /* We want CSE to commonize calls to the wrapper, but marking it as
3163 pure is unsafe since it has side-effects. I guess we need a new
3164 ECF flag even weaker than ECF_PURE. FIXME! */
3165 DECL_PURE_P (fn) = true;
3166 #endif
3168 DECL_BEFRIENDING_CLASSES (fn) = var;
3170 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3172 return fn;
3175 /* At EOF, generate the definition for the TLS wrapper function FN:
3177 T& var_wrapper() {
3178 if (init_fn) init_fn();
3179 return var;
3180 } */
3182 static void
3183 generate_tls_wrapper (tree fn)
3185 tree var = DECL_BEFRIENDING_CLASSES (fn);
3187 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3188 tree body = begin_function_body ();
3189 /* Only call the init fn if there might be one. */
3190 if (tree init_fn = get_tls_init_fn (var))
3192 tree if_stmt = NULL_TREE;
3193 /* If init_fn is a weakref, make sure it exists before calling. */
3194 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3196 if_stmt = begin_if_stmt ();
3197 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3198 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3199 NE_EXPR, addr, nullptr_node,
3200 tf_warning_or_error);
3201 finish_if_stmt_cond (cond, if_stmt);
3203 finish_expr_stmt (build_cxx_call
3204 (init_fn, 0, NULL, tf_warning_or_error));
3205 if (if_stmt)
3207 finish_then_clause (if_stmt);
3208 finish_if_stmt (if_stmt);
3211 else
3212 /* If there's no initialization, the wrapper is a constant function. */
3213 TREE_READONLY (fn) = true;
3214 finish_return_stmt (convert_from_reference (var));
3215 finish_function_body (body);
3216 expand_or_defer_fn (finish_function (0));
3219 /* Start the process of running a particular set of global constructors
3220 or destructors. Subroutine of do_[cd]tors. Also called from
3221 vtv_start_verification_constructor_init_function. */
3223 static tree
3224 start_objects (int method_type, int initp)
3226 tree body;
3227 tree fndecl;
3228 char type[14];
3230 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3232 if (initp != DEFAULT_INIT_PRIORITY)
3234 char joiner;
3236 #ifdef JOINER
3237 joiner = JOINER;
3238 #else
3239 joiner = '_';
3240 #endif
3242 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3244 else
3245 sprintf (type, "sub_%c", method_type);
3247 fndecl = build_lang_decl (FUNCTION_DECL,
3248 get_file_function_name (type),
3249 build_function_type_list (void_type_node,
3250 NULL_TREE));
3251 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3253 TREE_PUBLIC (current_function_decl) = 0;
3255 /* Mark as artificial because it's not explicitly in the user's
3256 source code. */
3257 DECL_ARTIFICIAL (current_function_decl) = 1;
3259 /* Mark this declaration as used to avoid spurious warnings. */
3260 TREE_USED (current_function_decl) = 1;
3262 /* Mark this function as a global constructor or destructor. */
3263 if (method_type == 'I')
3264 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3265 else
3266 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3268 body = begin_compound_stmt (BCS_FN_BODY);
3270 return body;
3273 /* Finish the process of running a particular set of global constructors
3274 or destructors. Subroutine of do_[cd]tors. */
3276 static void
3277 finish_objects (int method_type, int initp, tree body)
3279 tree fn;
3281 /* Finish up. */
3282 finish_compound_stmt (body);
3283 fn = finish_function (0);
3285 if (method_type == 'I')
3287 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3288 decl_init_priority_insert (fn, initp);
3290 else
3292 DECL_STATIC_DESTRUCTOR (fn) = 1;
3293 decl_fini_priority_insert (fn, initp);
3296 expand_or_defer_fn (fn);
3299 /* The names of the parameters to the function created to handle
3300 initializations and destructions for objects with static storage
3301 duration. */
3302 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3303 #define PRIORITY_IDENTIFIER "__priority"
3305 /* The name of the function we create to handle initializations and
3306 destructions for objects with static storage duration. */
3307 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3309 /* The declaration for the __INITIALIZE_P argument. */
3310 static GTY(()) tree initialize_p_decl;
3312 /* The declaration for the __PRIORITY argument. */
3313 static GTY(()) tree priority_decl;
3315 /* The declaration for the static storage duration function. */
3316 static GTY(()) tree ssdf_decl;
3318 /* All the static storage duration functions created in this
3319 translation unit. */
3320 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3322 /* A map from priority levels to information about that priority
3323 level. There may be many such levels, so efficient lookup is
3324 important. */
3325 static splay_tree priority_info_map;
3327 /* Begins the generation of the function that will handle all
3328 initialization and destruction of objects with static storage
3329 duration. The function generated takes two parameters of type
3330 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3331 nonzero, it performs initializations. Otherwise, it performs
3332 destructions. It only performs those initializations or
3333 destructions with the indicated __PRIORITY. The generated function
3334 returns no value.
3336 It is assumed that this function will only be called once per
3337 translation unit. */
3339 static tree
3340 start_static_storage_duration_function (unsigned count)
3342 tree type;
3343 tree body;
3344 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3346 /* Create the identifier for this function. It will be of the form
3347 SSDF_IDENTIFIER_<number>. */
3348 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3350 type = build_function_type_list (void_type_node,
3351 integer_type_node, integer_type_node,
3352 NULL_TREE);
3354 /* Create the FUNCTION_DECL itself. */
3355 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3356 get_identifier (id),
3357 type);
3358 TREE_PUBLIC (ssdf_decl) = 0;
3359 DECL_ARTIFICIAL (ssdf_decl) = 1;
3361 /* Put this function in the list of functions to be called from the
3362 static constructors and destructors. */
3363 if (!ssdf_decls)
3365 vec_alloc (ssdf_decls, 32);
3367 /* Take this opportunity to initialize the map from priority
3368 numbers to information about that priority level. */
3369 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3370 /*delete_key_fn=*/0,
3371 /*delete_value_fn=*/
3372 (splay_tree_delete_value_fn) &free);
3374 /* We always need to generate functions for the
3375 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3376 priorities later, we'll be sure to find the
3377 DEFAULT_INIT_PRIORITY. */
3378 get_priority_info (DEFAULT_INIT_PRIORITY);
3381 vec_safe_push (ssdf_decls, ssdf_decl);
3383 /* Create the argument list. */
3384 initialize_p_decl = cp_build_parm_decl
3385 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3386 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3387 TREE_USED (initialize_p_decl) = 1;
3388 priority_decl = cp_build_parm_decl
3389 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3390 DECL_CONTEXT (priority_decl) = ssdf_decl;
3391 TREE_USED (priority_decl) = 1;
3393 DECL_CHAIN (initialize_p_decl) = priority_decl;
3394 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3396 /* Put the function in the global scope. */
3397 pushdecl (ssdf_decl);
3399 /* Start the function itself. This is equivalent to declaring the
3400 function as:
3402 static void __ssdf (int __initialize_p, init __priority_p);
3404 It is static because we only need to call this function from the
3405 various constructor and destructor functions for this module. */
3406 start_preparsed_function (ssdf_decl,
3407 /*attrs=*/NULL_TREE,
3408 SF_PRE_PARSED);
3410 /* Set up the scope of the outermost block in the function. */
3411 body = begin_compound_stmt (BCS_FN_BODY);
3413 return body;
3416 /* Finish the generation of the function which performs initialization
3417 and destruction of objects with static storage duration. After
3418 this point, no more such objects can be created. */
3420 static void
3421 finish_static_storage_duration_function (tree body)
3423 /* Close out the function. */
3424 finish_compound_stmt (body);
3425 expand_or_defer_fn (finish_function (0));
3428 /* Return the information about the indicated PRIORITY level. If no
3429 code to handle this level has yet been generated, generate the
3430 appropriate prologue. */
3432 static priority_info
3433 get_priority_info (int priority)
3435 priority_info pi;
3436 splay_tree_node n;
3438 n = splay_tree_lookup (priority_info_map,
3439 (splay_tree_key) priority);
3440 if (!n)
3442 /* Create a new priority information structure, and insert it
3443 into the map. */
3444 pi = XNEW (struct priority_info_s);
3445 pi->initializations_p = 0;
3446 pi->destructions_p = 0;
3447 splay_tree_insert (priority_info_map,
3448 (splay_tree_key) priority,
3449 (splay_tree_value) pi);
3451 else
3452 pi = (priority_info) n->value;
3454 return pi;
3457 /* The effective initialization priority of a DECL. */
3459 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3460 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3461 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3463 /* Whether a DECL needs a guard to protect it against multiple
3464 initialization. */
3466 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3467 || DECL_ONE_ONLY (decl) \
3468 || DECL_WEAK (decl)))
3470 /* Called from one_static_initialization_or_destruction(),
3471 via walk_tree.
3472 Walks the initializer list of a global variable and looks for
3473 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3474 and that have their DECL_CONTEXT() == NULL.
3475 For each such temporary variable, set their DECL_CONTEXT() to
3476 the current function. This is necessary because otherwise
3477 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3478 when trying to refer to a temporary variable that does not have
3479 it's DECL_CONTECT() properly set. */
3480 static tree
3481 fix_temporary_vars_context_r (tree *node,
3482 int * /*unused*/,
3483 void * /*unused1*/)
3485 gcc_assert (current_function_decl);
3487 if (TREE_CODE (*node) == BIND_EXPR)
3489 tree var;
3491 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3492 if (VAR_P (var)
3493 && !DECL_NAME (var)
3494 && DECL_ARTIFICIAL (var)
3495 && !DECL_CONTEXT (var))
3496 DECL_CONTEXT (var) = current_function_decl;
3499 return NULL_TREE;
3502 /* Set up to handle the initialization or destruction of DECL. If
3503 INITP is nonzero, we are initializing the variable. Otherwise, we
3504 are destroying it. */
3506 static void
3507 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3509 tree guard_if_stmt = NULL_TREE;
3510 tree guard;
3512 /* If we are supposed to destruct and there's a trivial destructor,
3513 nothing has to be done. */
3514 if (!initp
3515 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3516 return;
3518 /* Trick the compiler into thinking we are at the file and line
3519 where DECL was declared so that error-messages make sense, and so
3520 that the debugger will show somewhat sensible file and line
3521 information. */
3522 input_location = DECL_SOURCE_LOCATION (decl);
3524 /* Make sure temporary variables in the initialiser all have
3525 their DECL_CONTEXT() set to a value different from NULL_TREE.
3526 This can happen when global variables initialisers are built.
3527 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3528 the temporary variables that might have been generated in the
3529 accompagning initialisers is NULL_TREE, meaning the variables have been
3530 declared in the global namespace.
3531 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3532 of the temporaries are set to the current function decl. */
3533 cp_walk_tree_without_duplicates (&init,
3534 fix_temporary_vars_context_r,
3535 NULL);
3537 /* Because of:
3539 [class.access.spec]
3541 Access control for implicit calls to the constructors,
3542 the conversion functions, or the destructor called to
3543 create and destroy a static data member is performed as
3544 if these calls appeared in the scope of the member's
3545 class.
3547 we pretend we are in a static member function of the class of
3548 which the DECL is a member. */
3549 if (member_p (decl))
3551 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3552 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3555 /* Assume we don't need a guard. */
3556 guard = NULL_TREE;
3557 /* We need a guard if this is an object with external linkage that
3558 might be initialized in more than one place. (For example, a
3559 static data member of a template, when the data member requires
3560 construction.) */
3561 if (NEEDS_GUARD_P (decl))
3563 tree guard_cond;
3565 guard = get_guard (decl);
3567 /* When using __cxa_atexit, we just check the GUARD as we would
3568 for a local static. */
3569 if (flag_use_cxa_atexit)
3571 /* When using __cxa_atexit, we never try to destroy
3572 anything from a static destructor. */
3573 gcc_assert (initp);
3574 guard_cond = get_guard_cond (guard);
3576 /* If we don't have __cxa_atexit, then we will be running
3577 destructors from .fini sections, or their equivalents. So,
3578 we need to know how many times we've tried to initialize this
3579 object. We do initializations only if the GUARD is zero,
3580 i.e., if we are the first to initialize the variable. We do
3581 destructions only if the GUARD is one, i.e., if we are the
3582 last to destroy the variable. */
3583 else if (initp)
3584 guard_cond
3585 = cp_build_binary_op (input_location,
3586 EQ_EXPR,
3587 cp_build_unary_op (PREINCREMENT_EXPR,
3588 guard,
3589 /*noconvert=*/1,
3590 tf_warning_or_error),
3591 integer_one_node,
3592 tf_warning_or_error);
3593 else
3594 guard_cond
3595 = cp_build_binary_op (input_location,
3596 EQ_EXPR,
3597 cp_build_unary_op (PREDECREMENT_EXPR,
3598 guard,
3599 /*noconvert=*/1,
3600 tf_warning_or_error),
3601 integer_zero_node,
3602 tf_warning_or_error);
3604 guard_if_stmt = begin_if_stmt ();
3605 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3609 /* If we're using __cxa_atexit, we have not already set the GUARD,
3610 so we must do so now. */
3611 if (guard && initp && flag_use_cxa_atexit)
3612 finish_expr_stmt (set_guard (guard));
3614 /* Perform the initialization or destruction. */
3615 if (initp)
3617 if (init)
3619 finish_expr_stmt (init);
3620 if (flag_sanitize & SANITIZE_ADDRESS)
3622 varpool_node *vnode = varpool_get_node (decl);
3623 if (vnode)
3624 vnode->dynamically_initialized = 1;
3628 /* If we're using __cxa_atexit, register a function that calls the
3629 destructor for the object. */
3630 if (flag_use_cxa_atexit)
3631 finish_expr_stmt (register_dtor_fn (decl));
3633 else
3634 finish_expr_stmt (build_cleanup (decl));
3636 /* Finish the guard if-stmt, if necessary. */
3637 if (guard)
3639 finish_then_clause (guard_if_stmt);
3640 finish_if_stmt (guard_if_stmt);
3643 /* Now that we're done with DECL we don't need to pretend to be a
3644 member of its class any longer. */
3645 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3646 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3649 /* Generate code to do the initialization or destruction of the decls in VARS,
3650 a TREE_LIST of VAR_DECL with static storage duration.
3651 Whether initialization or destruction is performed is specified by INITP. */
3653 static void
3654 do_static_initialization_or_destruction (tree vars, bool initp)
3656 tree node, init_if_stmt, cond;
3658 /* Build the outer if-stmt to check for initialization or destruction. */
3659 init_if_stmt = begin_if_stmt ();
3660 cond = initp ? integer_one_node : integer_zero_node;
3661 cond = cp_build_binary_op (input_location,
3662 EQ_EXPR,
3663 initialize_p_decl,
3664 cond,
3665 tf_warning_or_error);
3666 finish_if_stmt_cond (cond, init_if_stmt);
3668 /* To make sure dynamic construction doesn't access globals from other
3669 compilation units where they might not be yet constructed, for
3670 -fsanitize=address insert __asan_before_dynamic_init call that
3671 prevents access to either all global variables that need construction
3672 in other compilation units, or at least those that haven't been
3673 initialized yet. Variables that need dynamic construction in
3674 the current compilation unit are kept accessible. */
3675 if (flag_sanitize & SANITIZE_ADDRESS)
3676 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3678 node = vars;
3679 do {
3680 tree decl = TREE_VALUE (node);
3681 tree priority_if_stmt;
3682 int priority;
3683 priority_info pi;
3685 /* If we don't need a destructor, there's nothing to do. Avoid
3686 creating a possibly empty if-stmt. */
3687 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3689 node = TREE_CHAIN (node);
3690 continue;
3693 /* Remember that we had an initialization or finalization at this
3694 priority. */
3695 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3696 pi = get_priority_info (priority);
3697 if (initp)
3698 pi->initializations_p = 1;
3699 else
3700 pi->destructions_p = 1;
3702 /* Conditionalize this initialization on being in the right priority
3703 and being initializing/finalizing appropriately. */
3704 priority_if_stmt = begin_if_stmt ();
3705 cond = cp_build_binary_op (input_location,
3706 EQ_EXPR,
3707 priority_decl,
3708 build_int_cst (NULL_TREE, priority),
3709 tf_warning_or_error);
3710 finish_if_stmt_cond (cond, priority_if_stmt);
3712 /* Process initializers with same priority. */
3713 for (; node
3714 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3715 node = TREE_CHAIN (node))
3716 /* Do one initialization or destruction. */
3717 one_static_initialization_or_destruction (TREE_VALUE (node),
3718 TREE_PURPOSE (node), initp);
3720 /* Finish up the priority if-stmt body. */
3721 finish_then_clause (priority_if_stmt);
3722 finish_if_stmt (priority_if_stmt);
3724 } while (node);
3726 /* Revert what __asan_before_dynamic_init did by calling
3727 __asan_after_dynamic_init. */
3728 if (flag_sanitize & SANITIZE_ADDRESS)
3729 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3731 /* Finish up the init/destruct if-stmt body. */
3732 finish_then_clause (init_if_stmt);
3733 finish_if_stmt (init_if_stmt);
3736 /* VARS is a list of variables with static storage duration which may
3737 need initialization and/or finalization. Remove those variables
3738 that don't really need to be initialized or finalized, and return
3739 the resulting list. The order in which the variables appear in
3740 VARS is in reverse order of the order in which they should actually
3741 be initialized. The list we return is in the unreversed order;
3742 i.e., the first variable should be initialized first. */
3744 static tree
3745 prune_vars_needing_no_initialization (tree *vars)
3747 tree *var = vars;
3748 tree result = NULL_TREE;
3750 while (*var)
3752 tree t = *var;
3753 tree decl = TREE_VALUE (t);
3754 tree init = TREE_PURPOSE (t);
3756 /* Deal gracefully with error. */
3757 if (decl == error_mark_node)
3759 var = &TREE_CHAIN (t);
3760 continue;
3763 /* The only things that can be initialized are variables. */
3764 gcc_assert (VAR_P (decl));
3766 /* If this object is not defined, we don't need to do anything
3767 here. */
3768 if (DECL_EXTERNAL (decl))
3770 var = &TREE_CHAIN (t);
3771 continue;
3774 /* Also, if the initializer already contains errors, we can bail
3775 out now. */
3776 if (init && TREE_CODE (init) == TREE_LIST
3777 && value_member (error_mark_node, init))
3779 var = &TREE_CHAIN (t);
3780 continue;
3783 /* This variable is going to need initialization and/or
3784 finalization, so we add it to the list. */
3785 *var = TREE_CHAIN (t);
3786 TREE_CHAIN (t) = result;
3787 result = t;
3790 return result;
3793 /* Make sure we have told the back end about all the variables in
3794 VARS. */
3796 static void
3797 write_out_vars (tree vars)
3799 tree v;
3801 for (v = vars; v; v = TREE_CHAIN (v))
3803 tree var = TREE_VALUE (v);
3804 if (!var_finalized_p (var))
3806 import_export_decl (var);
3807 rest_of_decl_compilation (var, 1, 1);
3812 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3813 (otherwise) that will initialize all global objects with static
3814 storage duration having the indicated PRIORITY. */
3816 static void
3817 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3818 location_t *locus)
3820 char function_key;
3821 tree fndecl;
3822 tree body;
3823 size_t i;
3825 input_location = *locus;
3826 /* ??? */
3827 /* Was: locus->line++; */
3829 /* We use `I' to indicate initialization and `D' to indicate
3830 destruction. */
3831 function_key = constructor_p ? 'I' : 'D';
3833 /* We emit the function lazily, to avoid generating empty
3834 global constructors and destructors. */
3835 body = NULL_TREE;
3837 /* For Objective-C++, we may need to initialize metadata found in this module.
3838 This must be done _before_ any other static initializations. */
3839 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3840 && constructor_p && objc_static_init_needed_p ())
3842 body = start_objects (function_key, priority);
3843 objc_generate_static_init_call (NULL_TREE);
3846 /* Call the static storage duration function with appropriate
3847 arguments. */
3848 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3850 /* Calls to pure or const functions will expand to nothing. */
3851 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3853 tree call;
3855 if (! body)
3856 body = start_objects (function_key, priority);
3858 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3859 build_int_cst (NULL_TREE,
3860 constructor_p),
3861 build_int_cst (NULL_TREE,
3862 priority),
3863 NULL_TREE);
3864 finish_expr_stmt (call);
3868 /* Close out the function. */
3869 if (body)
3870 finish_objects (function_key, priority, body);
3873 /* Generate constructor and destructor functions for the priority
3874 indicated by N. */
3876 static int
3877 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3879 location_t *locus = (location_t *) data;
3880 int priority = (int) n->key;
3881 priority_info pi = (priority_info) n->value;
3883 /* Generate the functions themselves, but only if they are really
3884 needed. */
3885 if (pi->initializations_p)
3886 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3887 if (pi->destructions_p)
3888 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3890 /* Keep iterating. */
3891 return 0;
3894 /* Java requires that we be able to reference a local address for a
3895 method, and not be confused by PLT entries. If hidden aliases are
3896 supported, collect and return all the functions for which we should
3897 emit a hidden alias. */
3899 static struct pointer_set_t *
3900 collect_candidates_for_java_method_aliases (void)
3902 struct cgraph_node *node;
3903 struct pointer_set_t *candidates = NULL;
3905 #ifndef HAVE_GAS_HIDDEN
3906 return candidates;
3907 #endif
3909 FOR_EACH_FUNCTION (node)
3911 tree fndecl = node->decl;
3913 if (DECL_CLASS_SCOPE_P (fndecl)
3914 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3915 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3917 if (candidates == NULL)
3918 candidates = pointer_set_create ();
3919 pointer_set_insert (candidates, fndecl);
3923 return candidates;
3927 /* Java requires that we be able to reference a local address for a
3928 method, and not be confused by PLT entries. If hidden aliases are
3929 supported, emit one for each java function that we've emitted.
3930 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3931 by collect_candidates_for_java_method_aliases. */
3933 static void
3934 build_java_method_aliases (struct pointer_set_t *candidates)
3936 struct cgraph_node *node;
3938 #ifndef HAVE_GAS_HIDDEN
3939 return;
3940 #endif
3942 FOR_EACH_FUNCTION (node)
3944 tree fndecl = node->decl;
3946 if (TREE_ASM_WRITTEN (fndecl)
3947 && pointer_set_contains (candidates, fndecl))
3949 /* Mangle the name in a predictable way; we need to reference
3950 this from a java compiled object file. */
3951 tree oid, nid, alias;
3952 const char *oname;
3953 char *nname;
3955 oid = DECL_ASSEMBLER_NAME (fndecl);
3956 oname = IDENTIFIER_POINTER (oid);
3957 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3958 nname = ACONCAT (("_ZGA", oname+2, NULL));
3959 nid = get_identifier (nname);
3961 alias = make_alias_for (fndecl, nid);
3962 TREE_PUBLIC (alias) = 1;
3963 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3965 assemble_alias (alias, oid);
3970 /* Return C++ property of T, based on given operation OP. */
3972 static int
3973 cpp_check (tree t, cpp_operation op)
3975 switch (op)
3977 case IS_ABSTRACT:
3978 return DECL_PURE_VIRTUAL_P (t);
3979 case IS_CONSTRUCTOR:
3980 return DECL_CONSTRUCTOR_P (t);
3981 case IS_DESTRUCTOR:
3982 return DECL_DESTRUCTOR_P (t);
3983 case IS_COPY_CONSTRUCTOR:
3984 return DECL_COPY_CONSTRUCTOR_P (t);
3985 case IS_TEMPLATE:
3986 return TREE_CODE (t) == TEMPLATE_DECL;
3987 case IS_TRIVIAL:
3988 return trivial_type_p (t);
3989 default:
3990 return 0;
3994 /* Collect source file references recursively, starting from NAMESPC. */
3996 static void
3997 collect_source_refs (tree namespc)
3999 tree t;
4001 if (!namespc)
4002 return;
4004 /* Iterate over names in this name space. */
4005 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4006 if (!DECL_IS_BUILTIN (t) )
4007 collect_source_ref (DECL_SOURCE_FILE (t));
4009 /* Dump siblings, if any */
4010 collect_source_refs (TREE_CHAIN (namespc));
4012 /* Dump children, if any */
4013 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4016 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4017 starting from NAMESPC. */
4019 static void
4020 collect_ada_namespace (tree namespc, const char *source_file)
4022 if (!namespc)
4023 return;
4025 /* Collect decls from this namespace */
4026 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4028 /* Collect siblings, if any */
4029 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4031 /* Collect children, if any */
4032 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4035 /* Returns true iff there is a definition available for variable or
4036 function DECL. */
4038 static bool
4039 decl_defined_p (tree decl)
4041 if (TREE_CODE (decl) == FUNCTION_DECL)
4042 return (DECL_INITIAL (decl) != NULL_TREE);
4043 else
4045 gcc_assert (VAR_P (decl));
4046 return !DECL_EXTERNAL (decl);
4050 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4052 [expr.const]
4054 An integral constant-expression can only involve ... const
4055 variables of integral or enumeration types initialized with
4056 constant expressions ...
4058 C++0x also allows constexpr variables and temporaries initialized
4059 with constant expressions. We handle the former here, but the latter
4060 are just folded away in cxx_eval_constant_expression.
4062 The standard does not require that the expression be non-volatile.
4063 G++ implements the proposed correction in DR 457. */
4065 bool
4066 decl_constant_var_p (tree decl)
4068 if (!decl_maybe_constant_var_p (decl))
4069 return false;
4071 /* We don't know if a template static data member is initialized with
4072 a constant expression until we instantiate its initializer. Even
4073 in the case of a constexpr variable, we can't treat it as a
4074 constant until its initializer is complete in case it's used in
4075 its own initializer. */
4076 mark_used (decl);
4077 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4080 /* Returns true if DECL could be a symbolic constant variable, depending on
4081 its initializer. */
4083 bool
4084 decl_maybe_constant_var_p (tree decl)
4086 tree type = TREE_TYPE (decl);
4087 if (!VAR_P (decl))
4088 return false;
4089 if (DECL_DECLARED_CONSTEXPR_P (decl))
4090 return true;
4091 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4092 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4095 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4096 called from grokfndecl and grokvardecl; in all modes it is called from
4097 cp_write_global_declarations. */
4099 void
4100 no_linkage_error (tree decl)
4102 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4103 /* In C++11 it's ok if the decl is defined. */
4104 return;
4105 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4106 if (t == NULL_TREE)
4107 /* The type that got us on no_linkage_decls must have gotten a name for
4108 linkage purposes. */;
4109 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4110 /* The type might end up having a typedef name for linkage purposes. */
4111 vec_safe_push (no_linkage_decls, decl);
4112 else if (TYPE_ANONYMOUS_P (t))
4114 bool d = false;
4115 if (cxx_dialect >= cxx11)
4116 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4117 "anonymous type, is used but never defined", decl);
4118 else if (DECL_EXTERN_C_P (decl))
4119 /* Allow this; it's pretty common in C. */;
4120 else if (TREE_CODE (decl) == VAR_DECL)
4121 /* DRs 132, 319 and 389 seem to indicate types with
4122 no linkage can only be used to declare extern "C"
4123 entities. Since it's not always an error in the
4124 ISO C++ 90 Standard, we only issue a warning. */
4125 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4126 "with no linkage used to declare variable %q#D with "
4127 "linkage", decl);
4128 else
4129 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4130 "linkage used to declare function %q#D with linkage",
4131 decl);
4132 if (d && is_typedef_decl (TYPE_NAME (t)))
4133 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4134 "to the unqualified type, so it is not used for linkage",
4135 TYPE_NAME (t));
4137 else if (cxx_dialect >= cxx11)
4138 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4139 "%qT, is used but never defined", decl, t);
4140 else if (TREE_CODE (decl) == VAR_DECL)
4141 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4142 "used to declare variable %q#D with linkage", t, decl);
4143 else
4144 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4145 "to declare function %q#D with linkage", t, decl);
4148 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4150 static void
4151 collect_all_refs (const char *source_file)
4153 collect_ada_namespace (global_namespace, source_file);
4156 /* Clear DECL_EXTERNAL for NODE. */
4158 static bool
4159 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4161 DECL_EXTERNAL (node->decl) = 0;
4162 return false;
4165 /* Build up the function to run dynamic initializers for thread_local
4166 variables in this translation unit and alias the init functions for the
4167 individual variables to it. */
4169 static void
4170 handle_tls_init (void)
4172 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4173 if (vars == NULL_TREE)
4174 return;
4176 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4178 write_out_vars (vars);
4180 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4181 boolean_type_node);
4182 TREE_PUBLIC (guard) = false;
4183 TREE_STATIC (guard) = true;
4184 DECL_ARTIFICIAL (guard) = true;
4185 DECL_IGNORED_P (guard) = true;
4186 TREE_USED (guard) = true;
4187 DECL_TLS_MODEL (guard) = decl_default_tls_model (guard);
4188 pushdecl_top_level_and_finish (guard, NULL_TREE);
4190 tree fn = get_local_tls_init_fn ();
4191 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4192 tree body = begin_function_body ();
4193 tree if_stmt = begin_if_stmt ();
4194 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4195 tf_warning_or_error);
4196 finish_if_stmt_cond (cond, if_stmt);
4197 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4198 tf_warning_or_error));
4199 for (; vars; vars = TREE_CHAIN (vars))
4201 tree var = TREE_VALUE (vars);
4202 tree init = TREE_PURPOSE (vars);
4203 one_static_initialization_or_destruction (var, init, true);
4205 #ifdef ASM_OUTPUT_DEF
4206 /* Output init aliases even with -fno-extern-tls-init. */
4207 if (TREE_PUBLIC (var))
4209 tree single_init_fn = get_tls_init_fn (var);
4210 if (single_init_fn == NULL_TREE)
4211 continue;
4212 cgraph_node *alias
4213 = cgraph_same_body_alias (cgraph_get_create_node (fn),
4214 single_init_fn, fn);
4215 gcc_assert (alias != NULL);
4217 #endif
4220 finish_then_clause (if_stmt);
4221 finish_if_stmt (if_stmt);
4222 finish_function_body (body);
4223 expand_or_defer_fn (finish_function (0));
4226 /* The entire file is now complete. If requested, dump everything
4227 to a file. */
4229 static void
4230 dump_tu (void)
4232 int flags;
4233 FILE *stream = dump_begin (TDI_tu, &flags);
4235 if (stream)
4237 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4238 dump_end (TDI_tu, stream);
4242 /* This routine is called at the end of compilation.
4243 Its job is to create all the code needed to initialize and
4244 destroy the global aggregates. We do the destruction
4245 first, since that way we only need to reverse the decls once. */
4247 void
4248 cp_write_global_declarations (void)
4250 tree vars;
4251 bool reconsider;
4252 size_t i;
4253 location_t locus;
4254 unsigned ssdf_count = 0;
4255 int retries = 0;
4256 tree decl;
4257 struct pointer_set_t *candidates;
4259 locus = input_location;
4260 at_eof = 1;
4262 /* Bad parse errors. Just forget about it. */
4263 if (! global_bindings_p () || current_class_type
4264 || !vec_safe_is_empty (decl_namespace_list))
4265 return;
4267 /* This is the point to write out a PCH if we're doing that.
4268 In that case we do not want to do anything else. */
4269 if (pch_file)
4271 c_common_write_pch ();
4272 dump_tu ();
4273 return;
4276 cgraph_process_same_body_aliases ();
4278 /* Handle -fdump-ada-spec[-slim] */
4279 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4281 if (flag_dump_ada_spec_slim)
4282 collect_source_ref (main_input_filename);
4283 else
4284 collect_source_refs (global_namespace);
4286 dump_ada_specs (collect_all_refs, cpp_check);
4289 /* FIXME - huh? was input_line -= 1;*/
4291 timevar_start (TV_PHASE_DEFERRED);
4293 /* We now have to write out all the stuff we put off writing out.
4294 These include:
4296 o Template specializations that we have not yet instantiated,
4297 but which are needed.
4298 o Initialization and destruction for non-local objects with
4299 static storage duration. (Local objects with static storage
4300 duration are initialized when their scope is first entered,
4301 and are cleaned up via atexit.)
4302 o Virtual function tables.
4304 All of these may cause others to be needed. For example,
4305 instantiating one function may cause another to be needed, and
4306 generating the initializer for an object may cause templates to be
4307 instantiated, etc., etc. */
4309 emit_support_tinfos ();
4313 tree t;
4314 tree decl;
4316 reconsider = false;
4318 /* If there are templates that we've put off instantiating, do
4319 them now. */
4320 instantiate_pending_templates (retries);
4321 ggc_collect ();
4323 /* Write out virtual tables as required. Note that writing out
4324 the virtual table for a template class may cause the
4325 instantiation of members of that class. If we write out
4326 vtables then we remove the class from our list so we don't
4327 have to look at it again. */
4329 while (keyed_classes != NULL_TREE
4330 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4332 reconsider = true;
4333 keyed_classes = TREE_CHAIN (keyed_classes);
4336 t = keyed_classes;
4337 if (t != NULL_TREE)
4339 tree next = TREE_CHAIN (t);
4341 while (next)
4343 if (maybe_emit_vtables (TREE_VALUE (next)))
4345 reconsider = true;
4346 TREE_CHAIN (t) = TREE_CHAIN (next);
4348 else
4349 t = next;
4351 next = TREE_CHAIN (t);
4355 /* Write out needed type info variables. We have to be careful
4356 looping through unemitted decls, because emit_tinfo_decl may
4357 cause other variables to be needed. New elements will be
4358 appended, and we remove from the vector those that actually
4359 get emitted. */
4360 for (i = unemitted_tinfo_decls->length ();
4361 unemitted_tinfo_decls->iterate (--i, &t);)
4362 if (emit_tinfo_decl (t))
4364 reconsider = true;
4365 unemitted_tinfo_decls->unordered_remove (i);
4368 /* The list of objects with static storage duration is built up
4369 in reverse order. We clear STATIC_AGGREGATES so that any new
4370 aggregates added during the initialization of these will be
4371 initialized in the correct order when we next come around the
4372 loop. */
4373 vars = prune_vars_needing_no_initialization (&static_aggregates);
4375 if (vars)
4377 /* We need to start a new initialization function each time
4378 through the loop. That's because we need to know which
4379 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4380 isn't computed until a function is finished, and written
4381 out. That's a deficiency in the back end. When this is
4382 fixed, these initialization functions could all become
4383 inline, with resulting performance improvements. */
4384 tree ssdf_body;
4386 /* Set the line and file, so that it is obviously not from
4387 the source file. */
4388 input_location = locus;
4389 ssdf_body = start_static_storage_duration_function (ssdf_count);
4391 /* Make sure the back end knows about all the variables. */
4392 write_out_vars (vars);
4394 /* First generate code to do all the initializations. */
4395 if (vars)
4396 do_static_initialization_or_destruction (vars, /*initp=*/true);
4398 /* Then, generate code to do all the destructions. Do these
4399 in reverse order so that the most recently constructed
4400 variable is the first destroyed. If we're using
4401 __cxa_atexit, then we don't need to do this; functions
4402 were registered at initialization time to destroy the
4403 local statics. */
4404 if (!flag_use_cxa_atexit && vars)
4406 vars = nreverse (vars);
4407 do_static_initialization_or_destruction (vars, /*initp=*/false);
4409 else
4410 vars = NULL_TREE;
4412 /* Finish up the static storage duration function for this
4413 round. */
4414 input_location = locus;
4415 finish_static_storage_duration_function (ssdf_body);
4417 /* All those initializations and finalizations might cause
4418 us to need more inline functions, more template
4419 instantiations, etc. */
4420 reconsider = true;
4421 ssdf_count++;
4422 /* ??? was: locus.line++; */
4425 /* Now do the same for thread_local variables. */
4426 handle_tls_init ();
4428 /* Go through the set of inline functions whose bodies have not
4429 been emitted yet. If out-of-line copies of these functions
4430 are required, emit them. */
4431 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4433 /* Does it need synthesizing? */
4434 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4435 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4437 /* Even though we're already at the top-level, we push
4438 there again. That way, when we pop back a few lines
4439 hence, all of our state is restored. Otherwise,
4440 finish_function doesn't clean things up, and we end
4441 up with CURRENT_FUNCTION_DECL set. */
4442 push_to_top_level ();
4443 /* The decl's location will mark where it was first
4444 needed. Save that so synthesize method can indicate
4445 where it was needed from, in case of error */
4446 input_location = DECL_SOURCE_LOCATION (decl);
4447 synthesize_method (decl);
4448 pop_from_top_level ();
4449 reconsider = true;
4452 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4453 generate_tls_wrapper (decl);
4455 if (!DECL_SAVED_TREE (decl))
4456 continue;
4458 /* We lie to the back end, pretending that some functions
4459 are not defined when they really are. This keeps these
4460 functions from being put out unnecessarily. But, we must
4461 stop lying when the functions are referenced, or if they
4462 are not comdat since they need to be put out now. If
4463 DECL_INTERFACE_KNOWN, then we have already set
4464 DECL_EXTERNAL appropriately, so there's no need to check
4465 again, and we do not want to clear DECL_EXTERNAL if a
4466 previous call to import_export_decl set it.
4468 This is done in a separate for cycle, because if some
4469 deferred function is contained in another deferred
4470 function later in deferred_fns varray,
4471 rest_of_compilation would skip this function and we
4472 really cannot expand the same function twice. */
4473 import_export_decl (decl);
4474 if (DECL_NOT_REALLY_EXTERN (decl)
4475 && DECL_INITIAL (decl)
4476 && decl_needed_p (decl))
4478 struct cgraph_node *node, *next;
4480 node = cgraph_get_node (decl);
4481 if (node->cpp_implicit_alias)
4482 node = cgraph_alias_target (node);
4484 cgraph_for_node_and_aliases (node, clear_decl_external,
4485 NULL, true);
4486 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4487 group, we need to mark all symbols in the same comdat group
4488 that way. */
4489 if (node->same_comdat_group)
4490 for (next = cgraph (node->same_comdat_group);
4491 next != node;
4492 next = cgraph (next->same_comdat_group))
4493 cgraph_for_node_and_aliases (next, clear_decl_external,
4494 NULL, true);
4497 /* If we're going to need to write this function out, and
4498 there's already a body for it, create RTL for it now.
4499 (There might be no body if this is a method we haven't
4500 gotten around to synthesizing yet.) */
4501 if (!DECL_EXTERNAL (decl)
4502 && decl_needed_p (decl)
4503 && !TREE_ASM_WRITTEN (decl)
4504 && !cgraph_get_node (decl)->definition)
4506 /* We will output the function; no longer consider it in this
4507 loop. */
4508 DECL_DEFER_OUTPUT (decl) = 0;
4509 /* Generate RTL for this function now that we know we
4510 need it. */
4511 expand_or_defer_fn (decl);
4512 /* If we're compiling -fsyntax-only pretend that this
4513 function has been written out so that we don't try to
4514 expand it again. */
4515 if (flag_syntax_only)
4516 TREE_ASM_WRITTEN (decl) = 1;
4517 reconsider = true;
4521 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4522 reconsider = true;
4524 /* Static data members are just like namespace-scope globals. */
4525 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4527 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4528 /* Don't write it out if we haven't seen a definition. */
4529 || DECL_IN_AGGR_P (decl))
4530 continue;
4531 import_export_decl (decl);
4532 /* If this static data member is needed, provide it to the
4533 back end. */
4534 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4535 DECL_EXTERNAL (decl) = 0;
4537 if (vec_safe_length (pending_statics) != 0
4538 && wrapup_global_declarations (pending_statics->address (),
4539 pending_statics->length ()))
4540 reconsider = true;
4542 retries++;
4544 while (reconsider);
4546 /* All used inline functions must have a definition at this point. */
4547 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4549 if (/* Check online inline functions that were actually used. */
4550 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4551 /* If the definition actually was available here, then the
4552 fact that the function was not defined merely represents
4553 that for some reason (use of a template repository,
4554 #pragma interface, etc.) we decided not to emit the
4555 definition here. */
4556 && !DECL_INITIAL (decl)
4557 /* Don't complain if the template was defined. */
4558 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4559 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4560 (template_for_substitution (decl)))))
4562 warning (0, "inline function %q+D used but never defined", decl);
4563 /* Avoid a duplicate warning from check_global_declaration_1. */
4564 TREE_NO_WARNING (decl) = 1;
4568 /* So must decls that use a type with no linkage. */
4569 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4570 no_linkage_error (decl);
4572 /* Then, do the Objective-C stuff. This is where all the
4573 Objective-C module stuff gets generated (symtab,
4574 class/protocol/selector lists etc). This must be done after C++
4575 templates, destructors etc. so that selectors used in C++
4576 templates are properly allocated. */
4577 if (c_dialect_objc ())
4578 objc_write_global_declarations ();
4580 /* We give C linkage to static constructors and destructors. */
4581 push_lang_context (lang_name_c);
4583 /* Generate initialization and destruction functions for all
4584 priorities for which they are required. */
4585 if (priority_info_map)
4586 splay_tree_foreach (priority_info_map,
4587 generate_ctor_and_dtor_functions_for_priority,
4588 /*data=*/&locus);
4589 else if (c_dialect_objc () && objc_static_init_needed_p ())
4590 /* If this is obj-c++ and we need a static init, call
4591 generate_ctor_or_dtor_function. */
4592 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4593 DEFAULT_INIT_PRIORITY, &locus);
4595 /* We're done with the splay-tree now. */
4596 if (priority_info_map)
4597 splay_tree_delete (priority_info_map);
4599 /* Generate any missing aliases. */
4600 maybe_apply_pending_pragma_weaks ();
4602 /* We're done with static constructors, so we can go back to "C++"
4603 linkage now. */
4604 pop_lang_context ();
4606 /* Collect candidates for Java hidden aliases. */
4607 candidates = collect_candidates_for_java_method_aliases ();
4609 timevar_stop (TV_PHASE_DEFERRED);
4610 timevar_start (TV_PHASE_OPT_GEN);
4612 if (flag_vtable_verify)
4614 vtv_recover_class_info ();
4615 vtv_compute_class_hierarchy_transitive_closure ();
4616 vtv_build_vtable_verify_fndecl ();
4619 finalize_compilation_unit ();
4621 if (flag_vtable_verify)
4623 /* Generate the special constructor initialization function that
4624 calls __VLTRegisterPairs, and give it a very high
4625 initialization priority. This must be done after
4626 finalize_compilation_unit so that we have accurate
4627 information about which vtable will actually be emitted. */
4628 vtv_generate_init_routine ();
4631 timevar_stop (TV_PHASE_OPT_GEN);
4632 timevar_start (TV_PHASE_CHECK_DBGINFO);
4634 /* Now, issue warnings about static, but not defined, functions,
4635 etc., and emit debugging information. */
4636 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4637 if (vec_safe_length (pending_statics) != 0)
4639 check_global_declarations (pending_statics->address (),
4640 pending_statics->length ());
4641 emit_debug_global_declarations (pending_statics->address (),
4642 pending_statics->length ());
4645 perform_deferred_noexcept_checks ();
4647 /* Generate hidden aliases for Java. */
4648 if (candidates)
4650 build_java_method_aliases (candidates);
4651 pointer_set_destroy (candidates);
4654 finish_repo ();
4656 /* The entire file is now complete. If requested, dump everything
4657 to a file. */
4658 dump_tu ();
4660 if (flag_detailed_statistics)
4662 dump_tree_statistics ();
4663 dump_time_statistics ();
4665 input_location = locus;
4667 #ifdef ENABLE_CHECKING
4668 validate_conversion_obstack ();
4669 #endif /* ENABLE_CHECKING */
4671 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4674 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4675 function to call in parse-tree form; it has not yet been
4676 semantically analyzed. ARGS are the arguments to the function.
4677 They have already been semantically analyzed. This may change
4678 ARGS. */
4680 tree
4681 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4682 tsubst_flags_t complain)
4684 tree orig_fn;
4685 vec<tree, va_gc> *orig_args = NULL;
4686 tree expr;
4687 tree object;
4689 orig_fn = fn;
4690 object = TREE_OPERAND (fn, 0);
4692 if (processing_template_decl)
4694 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4695 || TREE_CODE (fn) == MEMBER_REF);
4696 if (type_dependent_expression_p (fn)
4697 || any_type_dependent_arguments_p (*args))
4698 return build_nt_call_vec (fn, *args);
4700 orig_args = make_tree_vector_copy (*args);
4702 /* Transform the arguments and add the implicit "this"
4703 parameter. That must be done before the FN is transformed
4704 because we depend on the form of FN. */
4705 make_args_non_dependent (*args);
4706 object = build_non_dependent_expr (object);
4707 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4709 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4710 object = cp_build_addr_expr (object, complain);
4711 vec_safe_insert (*args, 0, object);
4713 /* Now that the arguments are done, transform FN. */
4714 fn = build_non_dependent_expr (fn);
4717 /* A qualified name corresponding to a bound pointer-to-member is
4718 represented as an OFFSET_REF:
4720 struct B { void g(); };
4721 void (B::*p)();
4722 void B::g() { (this->*p)(); } */
4723 if (TREE_CODE (fn) == OFFSET_REF)
4725 tree object_addr = cp_build_addr_expr (object, complain);
4726 fn = TREE_OPERAND (fn, 1);
4727 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4728 complain);
4729 vec_safe_insert (*args, 0, object_addr);
4732 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4733 expr = build_op_call (fn, args, complain);
4734 else
4735 expr = cp_build_function_call_vec (fn, args, complain);
4736 if (processing_template_decl && expr != error_mark_node)
4737 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4739 if (orig_args != NULL)
4740 release_tree_vector (orig_args);
4742 return expr;
4746 void
4747 check_default_args (tree x)
4749 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4750 bool saw_def = false;
4751 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4752 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4754 if (TREE_PURPOSE (arg))
4755 saw_def = true;
4756 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4758 error ("default argument missing for parameter %P of %q+#D", i, x);
4759 TREE_PURPOSE (arg) = error_mark_node;
4764 /* Return true if function DECL can be inlined. This is used to force
4765 instantiation of methods that might be interesting for inlining. */
4766 bool
4767 possibly_inlined_p (tree decl)
4769 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4770 if (DECL_UNINLINABLE (decl))
4771 return false;
4772 if (!optimize || pragma_java_exceptions)
4773 return DECL_DECLARED_INLINE_P (decl);
4774 /* When optimizing, we might inline everything when flatten
4775 attribute or heuristics inlining for size or autoinlining
4776 is used. */
4777 return true;
4780 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4781 If DECL is a specialization or implicitly declared class member,
4782 generate the actual definition. Return false if something goes
4783 wrong, true otherwise. */
4785 bool
4786 mark_used (tree decl, tsubst_flags_t complain)
4788 /* If DECL is a BASELINK for a single function, then treat it just
4789 like the DECL for the function. Otherwise, if the BASELINK is
4790 for an overloaded function, we don't know which function was
4791 actually used until after overload resolution. */
4792 if (BASELINK_P (decl))
4794 decl = BASELINK_FUNCTIONS (decl);
4795 if (really_overloaded_fn (decl))
4796 return true;
4797 decl = OVL_CURRENT (decl);
4800 /* Set TREE_USED for the benefit of -Wunused. */
4801 TREE_USED (decl) = 1;
4802 if (DECL_CLONED_FUNCTION_P (decl))
4803 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4805 /* Mark enumeration types as used. */
4806 if (TREE_CODE (decl) == CONST_DECL)
4807 used_types_insert (DECL_CONTEXT (decl));
4809 if (TREE_CODE (decl) == FUNCTION_DECL
4810 && DECL_DELETED_FN (decl))
4812 if (DECL_ARTIFICIAL (decl))
4814 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4815 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4817 /* We mark a lambda conversion op as deleted if we can't
4818 generate it properly; see maybe_add_lambda_conv_op. */
4819 sorry ("converting lambda which uses %<...%> to "
4820 "function pointer");
4821 return false;
4824 if (complain & tf_error)
4826 error ("use of deleted function %qD", decl);
4827 if (!maybe_explain_implicit_delete (decl))
4828 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4830 return false;
4833 /* We can only check DECL_ODR_USED on variables or functions with
4834 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4835 might need special handling for. */
4836 if (!VAR_OR_FUNCTION_DECL_P (decl)
4837 || DECL_LANG_SPECIFIC (decl) == NULL
4838 || DECL_THUNK_P (decl))
4840 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4842 if (complain & tf_error)
4843 error ("use of %qD before deduction of %<auto%>", decl);
4844 return false;
4846 return true;
4849 /* We only want to do this processing once. We don't need to keep trying
4850 to instantiate inline templates, because unit-at-a-time will make sure
4851 we get them compiled before functions that want to inline them. */
4852 if (DECL_ODR_USED (decl))
4853 return true;
4855 /* If within finish_function, defer the rest until that function
4856 finishes, otherwise it might recurse. */
4857 if (defer_mark_used_calls)
4859 vec_safe_push (deferred_mark_used_calls, decl);
4860 return true;
4863 if (TREE_CODE (decl) == FUNCTION_DECL)
4864 maybe_instantiate_noexcept (decl);
4866 /* Normally, we can wait until instantiation-time to synthesize DECL.
4867 However, if DECL is a static data member initialized with a constant
4868 or a constexpr function, we need it right now because a reference to
4869 such a data member or a call to such function is not value-dependent.
4870 For a function that uses auto in the return type, we need to instantiate
4871 it to find out its type. For OpenMP user defined reductions, we need
4872 them instantiated for reduction clauses which inline them by hand
4873 directly. */
4874 if (DECL_LANG_SPECIFIC (decl)
4875 && DECL_TEMPLATE_INFO (decl)
4876 && (decl_maybe_constant_var_p (decl)
4877 || (TREE_CODE (decl) == FUNCTION_DECL
4878 && (DECL_DECLARED_CONSTEXPR_P (decl)
4879 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4880 || undeduced_auto_decl (decl))
4881 && !uses_template_parms (DECL_TI_ARGS (decl)))
4883 /* Instantiating a function will result in garbage collection. We
4884 must treat this situation as if we were within the body of a
4885 function so as to avoid collecting live data only referenced from
4886 the stack (such as overload resolution candidates). */
4887 ++function_depth;
4888 instantiate_decl (decl, /*defer_ok=*/false,
4889 /*expl_inst_class_mem_p=*/false);
4890 --function_depth;
4893 if (processing_template_decl)
4894 return true;
4896 /* Check this too in case we're within fold_non_dependent_expr. */
4897 if (DECL_TEMPLATE_INFO (decl)
4898 && uses_template_parms (DECL_TI_ARGS (decl)))
4899 return true;
4901 require_deduced_type (decl);
4903 /* If we don't need a value, then we don't need to synthesize DECL. */
4904 if (cp_unevaluated_operand != 0)
4905 return true;
4907 DECL_ODR_USED (decl) = 1;
4908 if (DECL_CLONED_FUNCTION_P (decl))
4909 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4911 /* DR 757: A type without linkage shall not be used as the type of a
4912 variable or function with linkage, unless
4913 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4914 o the variable or function is not used (3.2 [basic.def.odr]) or is
4915 defined in the same translation unit. */
4916 if (cxx_dialect > cxx98
4917 && decl_linkage (decl) != lk_none
4918 && !DECL_EXTERN_C_P (decl)
4919 && !DECL_ARTIFICIAL (decl)
4920 && !decl_defined_p (decl)
4921 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4923 if (is_local_extern (decl))
4924 /* There's no way to define a local extern, and adding it to
4925 the vector interferes with GC, so give an error now. */
4926 no_linkage_error (decl);
4927 else
4928 vec_safe_push (no_linkage_decls, decl);
4931 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4932 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4933 /* Remember it, so we can check it was defined. */
4934 note_vague_linkage_fn (decl);
4936 /* Is it a synthesized method that needs to be synthesized? */
4937 if (TREE_CODE (decl) == FUNCTION_DECL
4938 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4939 && DECL_DEFAULTED_FN (decl)
4940 /* A function defaulted outside the class is synthesized either by
4941 cp_finish_decl or instantiate_decl. */
4942 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
4943 && ! DECL_INITIAL (decl))
4945 /* Defer virtual destructors so that thunks get the right
4946 linkage. */
4947 if (DECL_VIRTUAL_P (decl) && !at_eof)
4949 note_vague_linkage_fn (decl);
4950 return true;
4953 /* Remember the current location for a function we will end up
4954 synthesizing. Then we can inform the user where it was
4955 required in the case of error. */
4956 DECL_SOURCE_LOCATION (decl) = input_location;
4958 /* Synthesizing an implicitly defined member function will result in
4959 garbage collection. We must treat this situation as if we were
4960 within the body of a function so as to avoid collecting live data
4961 on the stack (such as overload resolution candidates).
4963 We could just let cp_write_global_declarations handle synthesizing
4964 this function by adding it to deferred_fns, but doing
4965 it at the use site produces better error messages. */
4966 ++function_depth;
4967 synthesize_method (decl);
4968 --function_depth;
4969 /* If this is a synthesized method we don't need to
4970 do the instantiation test below. */
4972 else if (VAR_OR_FUNCTION_DECL_P (decl)
4973 && DECL_TEMPLATE_INFO (decl)
4974 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4975 || always_instantiate_p (decl)))
4976 /* If this is a function or variable that is an instance of some
4977 template, we now know that we will need to actually do the
4978 instantiation. We check that DECL is not an explicit
4979 instantiation because that is not checked in instantiate_decl.
4981 We put off instantiating functions in order to improve compile
4982 times. Maintaining a stack of active functions is expensive,
4983 and the inliner knows to instantiate any functions it might
4984 need. Therefore, we always try to defer instantiation. */
4986 ++function_depth;
4987 instantiate_decl (decl, /*defer_ok=*/true,
4988 /*expl_inst_class_mem_p=*/false);
4989 --function_depth;
4992 return true;
4995 bool
4996 mark_used (tree decl)
4998 return mark_used (decl, tf_warning_or_error);
5001 tree
5002 vtv_start_verification_constructor_init_function (void)
5004 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5007 tree
5008 vtv_finish_verification_constructor_init_function (tree function_body)
5010 tree fn;
5012 finish_compound_stmt (function_body);
5013 fn = finish_function (0);
5014 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5015 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5017 return fn;
5020 #include "gt-cp-decl2.h"