Merge from trunk
[official-gcc.git] / gcc / cp / decl2.c
blob7c3edddeaf73f1e9066547c8630be965b230f8b3
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 // Use the current template requirements as the constraints
659 // for function. Unfortunately, when this is called, FUNCTION
660 // does not have a TEMPLATE_DECL yet.
661 tree constr = NULL_TREE;
662 if (current_template_reqs)
663 constr = current_template_reqs;
664 else if (template_parms)
665 constr = TREE_TYPE (template_parms);
667 /* We must enter the scope here, because conversion operators are
668 named by target type, and type equivalence relies on typenames
669 resolving within the scope of CTYPE. */
670 pushed_scope = push_scope (ctype);
671 ix = class_method_index_for_fn (complete_type (ctype), function);
672 if (ix >= 0)
674 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
675 tree fndecls, fndecl = 0;
676 bool is_conv_op;
677 const char *format = NULL;
679 for (fndecls = (*methods)[ix];
680 fndecls; fndecls = OVL_NEXT (fndecls))
682 tree p1, p2;
684 fndecl = OVL_CURRENT (fndecls);
685 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
686 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
688 /* We cannot simply call decls_match because this doesn't
689 work for static member functions that are pretending to
690 be methods, and because the name may have been changed by
691 asm("new_name"). */
693 /* Get rid of the this parameter on functions that become
694 static. */
695 if (DECL_STATIC_FUNCTION_P (fndecl)
696 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
697 p1 = TREE_CHAIN (p1);
699 /* A member template definition only matches a member template
700 declaration. */
701 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
702 continue;
704 /* ref-qualifier or absence of same must match. */
705 if (type_memfn_rqual (TREE_TYPE (function))
706 != type_memfn_rqual (TREE_TYPE (fndecl)))
707 continue;
709 /* While finding a match, same types and params are not enough
710 if the function is versioned. Also check version ("target")
711 attributes. */
712 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
713 TREE_TYPE (TREE_TYPE (fndecl)))
714 && compparms (p1, p2)
715 && !targetm.target_option.function_versions (function, fndecl)
716 && (!is_template
717 || comp_template_parms (template_parms,
718 DECL_TEMPLATE_PARMS (fndecl)))
719 && equivalent_constraints (constr, get_constraints (fndecl))
720 && (DECL_TEMPLATE_SPECIALIZATION (function)
721 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
722 && (!DECL_TEMPLATE_SPECIALIZATION (function)
723 || (DECL_TI_TEMPLATE (function)
724 == DECL_TI_TEMPLATE (fndecl))))
725 break;
727 if (fndecls)
729 if (pushed_scope)
730 pop_scope (pushed_scope);
731 return OVL_CURRENT (fndecls);
734 error_at (DECL_SOURCE_LOCATION (function),
735 "prototype for %q#D does not match any in class %qT",
736 function, ctype);
737 is_conv_op = DECL_CONV_FN_P (fndecl);
739 if (is_conv_op)
740 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
741 fndecls = (*methods)[ix];
742 while (fndecls)
744 fndecl = OVL_CURRENT (fndecls);
745 fndecls = OVL_NEXT (fndecls);
747 if (!fndecls && is_conv_op)
749 if (methods->length () > (size_t) ++ix)
751 fndecls = (*methods)[ix];
752 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
754 fndecls = NULL_TREE;
755 is_conv_op = false;
758 else
759 is_conv_op = false;
761 if (format)
762 format = " %+#D";
763 else if (fndecls)
764 format = N_("candidates are: %+#D");
765 else
766 format = N_("candidate is: %+#D");
767 error (format, fndecl);
770 else if (!COMPLETE_TYPE_P (ctype))
771 cxx_incomplete_type_error (function, ctype);
772 else
773 error ("no %q#D member function declared in class %qT",
774 function, ctype);
776 if (pushed_scope)
777 pop_scope (pushed_scope);
778 return error_mark_node;
781 /* DECL is a function with vague linkage. Remember it so that at the
782 end of the translation unit we can decide whether or not to emit
783 it. */
785 void
786 note_vague_linkage_fn (tree decl)
788 DECL_DEFER_OUTPUT (decl) = 1;
789 vec_safe_push (deferred_fns, decl);
792 /* We have just processed the DECL, which is a static data member.
793 The other parameters are as for cp_finish_decl. */
795 void
796 finish_static_data_member_decl (tree decl,
797 tree init, bool init_const_expr_p,
798 tree asmspec_tree,
799 int flags)
801 DECL_CONTEXT (decl) = current_class_type;
803 /* We cannot call pushdecl here, because that would fill in the
804 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
805 the right thing, namely, to put this decl out straight away. */
807 if (! processing_template_decl)
808 vec_safe_push (pending_statics, decl);
810 if (LOCAL_CLASS_P (current_class_type)
811 /* We already complained about the template definition. */
812 && !DECL_TEMPLATE_INSTANTIATION (decl))
813 permerror (input_location, "local class %q#T shall not have static data member %q#D",
814 current_class_type, decl);
815 else
816 for (tree t = current_class_type; TYPE_P (t);
817 t = CP_TYPE_CONTEXT (t))
818 if (TYPE_ANONYMOUS_P (t))
820 if (permerror (DECL_SOURCE_LOCATION (decl),
821 "static data member %qD in unnamed class", decl))
822 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
823 "unnamed class defined here");
824 break;
827 DECL_IN_AGGR_P (decl) = 1;
829 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
830 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
831 SET_VAR_HAD_UNKNOWN_BOUND (decl);
833 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
836 /* DECLARATOR and DECLSPECS correspond to a class member. The other
837 parameters are as for cp_finish_decl. Return the DECL for the
838 class member declared. */
840 tree
841 grokfield (const cp_declarator *declarator,
842 cp_decl_specifier_seq *declspecs,
843 tree init, bool init_const_expr_p,
844 tree asmspec_tree,
845 tree attrlist)
847 tree value;
848 const char *asmspec = 0;
849 int flags;
850 tree name;
852 if (init
853 && TREE_CODE (init) == TREE_LIST
854 && TREE_VALUE (init) == error_mark_node
855 && TREE_CHAIN (init) == NULL_TREE)
856 init = NULL_TREE;
858 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
859 if (! value || value == error_mark_node)
860 /* friend or constructor went bad. */
861 return error_mark_node;
862 if (TREE_TYPE (value) == error_mark_node)
863 return value;
865 if (TREE_CODE (value) == TYPE_DECL && init)
867 error ("typedef %qD is initialized (use decltype instead)", value);
868 init = NULL_TREE;
871 /* Pass friendly classes back. */
872 if (value == void_type_node)
873 return value;
875 /* Pass friend decls back. */
876 if ((TREE_CODE (value) == FUNCTION_DECL
877 || TREE_CODE (value) == TEMPLATE_DECL)
878 && DECL_CONTEXT (value) != current_class_type)
879 return value;
881 name = DECL_NAME (value);
883 if (name != NULL_TREE)
885 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
887 error ("explicit template argument list not allowed");
888 return error_mark_node;
891 if (IDENTIFIER_POINTER (name)[0] == '_'
892 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
893 error ("member %qD conflicts with virtual function table field name",
894 value);
897 /* Stash away type declarations. */
898 if (TREE_CODE (value) == TYPE_DECL)
900 DECL_NONLOCAL (value) = 1;
901 DECL_CONTEXT (value) = current_class_type;
903 if (attrlist)
905 int attrflags = 0;
907 /* If this is a typedef that names the class for linkage purposes
908 (7.1.3p8), apply any attributes directly to the type. */
909 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
910 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
911 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
913 cplus_decl_attributes (&value, attrlist, attrflags);
916 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
917 && TREE_TYPE (value) != error_mark_node
918 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
919 set_underlying_type (value);
921 /* It's important that push_template_decl below follows
922 set_underlying_type above so that the created template
923 carries the properly set type of VALUE. */
924 if (processing_template_decl)
925 value = push_template_decl (value);
927 record_locally_defined_typedef (value);
928 return value;
931 if (DECL_IN_AGGR_P (value))
933 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
934 return void_type_node;
937 if (asmspec_tree && asmspec_tree != error_mark_node)
938 asmspec = TREE_STRING_POINTER (asmspec_tree);
940 if (init)
942 if (TREE_CODE (value) == FUNCTION_DECL)
944 /* Initializers for functions are rejected early in the parser.
945 If we get here, it must be a pure specifier for a method. */
946 if (init == ridpointers[(int)RID_DELETE])
948 DECL_DELETED_FN (value) = 1;
949 DECL_DECLARED_INLINE_P (value) = 1;
950 DECL_INITIAL (value) = error_mark_node;
952 else if (init == ridpointers[(int)RID_DEFAULT])
954 if (defaultable_fn_check (value))
956 DECL_DEFAULTED_FN (value) = 1;
957 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
958 DECL_DECLARED_INLINE_P (value) = 1;
961 else if (TREE_CODE (init) == DEFAULT_ARG)
962 error ("invalid initializer for member function %qD", value);
963 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
965 if (integer_zerop (init))
966 DECL_PURE_VIRTUAL_P (value) = 1;
967 else if (error_operand_p (init))
968 ; /* An error has already been reported. */
969 else
970 error ("invalid initializer for member function %qD",
971 value);
973 else
975 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
976 error ("initializer specified for static member function %qD",
977 value);
980 else if (TREE_CODE (value) == FIELD_DECL)
981 /* C++11 NSDMI, keep going. */;
982 else if (!VAR_P (value))
983 gcc_unreachable ();
986 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
988 value = push_template_decl (value);
989 if (error_operand_p (value))
990 return error_mark_node;
993 if (attrlist)
994 cplus_decl_attributes (&value, attrlist, 0);
996 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
997 && CONSTRUCTOR_IS_DIRECT_INIT (init))
998 flags = LOOKUP_NORMAL;
999 else
1000 flags = LOOKUP_IMPLICIT;
1002 switch (TREE_CODE (value))
1004 case VAR_DECL:
1005 finish_static_data_member_decl (value, init, init_const_expr_p,
1006 asmspec_tree, flags);
1007 return value;
1009 case FIELD_DECL:
1010 if (asmspec)
1011 error ("%<asm%> specifiers are not permitted on non-static data members");
1012 if (DECL_INITIAL (value) == error_mark_node)
1013 init = error_mark_node;
1014 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1015 NULL_TREE, flags);
1016 DECL_IN_AGGR_P (value) = 1;
1017 return value;
1019 case FUNCTION_DECL:
1020 if (asmspec)
1021 set_user_assembler_name (value, asmspec);
1023 cp_finish_decl (value,
1024 /*init=*/NULL_TREE,
1025 /*init_const_expr_p=*/false,
1026 asmspec_tree, flags);
1028 /* Pass friends back this way. */
1029 if (DECL_FRIEND_P (value))
1030 return void_type_node;
1032 DECL_IN_AGGR_P (value) = 1;
1033 return value;
1035 default:
1036 gcc_unreachable ();
1038 return NULL_TREE;
1041 /* Like `grokfield', but for bitfields.
1042 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1044 tree
1045 grokbitfield (const cp_declarator *declarator,
1046 cp_decl_specifier_seq *declspecs, tree width,
1047 tree attrlist)
1049 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1051 if (value == error_mark_node)
1052 return NULL_TREE; /* friends went bad. */
1053 if (TREE_TYPE (value) == error_mark_node)
1054 return value;
1056 /* Pass friendly classes back. */
1057 if (VOID_TYPE_P (value))
1058 return void_type_node;
1060 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1061 && (POINTER_TYPE_P (value)
1062 || !dependent_type_p (TREE_TYPE (value))))
1064 error ("bit-field %qD with non-integral type", value);
1065 return error_mark_node;
1068 if (TREE_CODE (value) == TYPE_DECL)
1070 error ("cannot declare %qD to be a bit-field type", value);
1071 return NULL_TREE;
1074 /* Usually, finish_struct_1 catches bitfields with invalid types.
1075 But, in the case of bitfields with function type, we confuse
1076 ourselves into thinking they are member functions, so we must
1077 check here. */
1078 if (TREE_CODE (value) == FUNCTION_DECL)
1080 error ("cannot declare bit-field %qD with function type",
1081 DECL_NAME (value));
1082 return NULL_TREE;
1085 if (DECL_IN_AGGR_P (value))
1087 error ("%qD is already defined in the class %qT", value,
1088 DECL_CONTEXT (value));
1089 return void_type_node;
1092 if (TREE_STATIC (value))
1094 error ("static member %qD cannot be a bit-field", value);
1095 return NULL_TREE;
1097 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1099 if (width != error_mark_node)
1101 /* The width must be an integer type. */
1102 if (!type_dependent_expression_p (width)
1103 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1104 error ("width of bit-field %qD has non-integral type %qT", value,
1105 TREE_TYPE (width));
1106 DECL_INITIAL (value) = width;
1107 SET_DECL_C_BIT_FIELD (value);
1110 DECL_IN_AGGR_P (value) = 1;
1112 if (attrlist)
1113 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1115 return value;
1119 /* Returns true iff ATTR is an attribute which needs to be applied at
1120 instantiation time rather than template definition time. */
1122 static bool
1123 is_late_template_attribute (tree attr, tree decl)
1125 tree name = get_attribute_name (attr);
1126 tree args = TREE_VALUE (attr);
1127 const struct attribute_spec *spec = lookup_attribute_spec (name);
1128 tree arg;
1130 if (!spec)
1131 /* Unknown attribute. */
1132 return false;
1134 /* Attribute weak handling wants to write out assembly right away. */
1135 if (is_attribute_p ("weak", name))
1136 return true;
1138 /* Attribute unused is applied directly, as it appertains to
1139 decls. */
1140 if (is_attribute_p ("unused", name))
1141 return false;
1143 /* #pragma omp declare simd attribute needs to be always deferred. */
1144 if (flag_openmp
1145 && is_attribute_p ("omp declare simd", name))
1146 return true;
1148 /* If any of the arguments are dependent expressions, we can't evaluate
1149 the attribute until instantiation time. */
1150 for (arg = args; arg; arg = TREE_CHAIN (arg))
1152 tree t = TREE_VALUE (arg);
1154 /* If the first attribute argument is an identifier, only consider
1155 second and following arguments. Attributes like mode, format,
1156 cleanup and several target specific attributes aren't late
1157 just because they have an IDENTIFIER_NODE as first argument. */
1158 if (arg == args && identifier_p (t))
1159 continue;
1161 if (value_dependent_expression_p (t)
1162 || type_dependent_expression_p (t))
1163 return true;
1166 if (TREE_CODE (decl) == TYPE_DECL
1167 || TYPE_P (decl)
1168 || spec->type_required)
1170 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1172 /* We can't apply any attributes to a completely unknown type until
1173 instantiation time. */
1174 enum tree_code code = TREE_CODE (type);
1175 if (code == TEMPLATE_TYPE_PARM
1176 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1177 || code == TYPENAME_TYPE)
1178 return true;
1179 /* Also defer most attributes on dependent types. This is not
1180 necessary in all cases, but is the better default. */
1181 else if (dependent_type_p (type)
1182 /* But attribute visibility specifically works on
1183 templates. */
1184 && !is_attribute_p ("visibility", name))
1185 return true;
1186 else
1187 return false;
1189 else
1190 return false;
1193 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1194 applied at instantiation time and return them. If IS_DEPENDENT is true,
1195 the declaration itself is dependent, so all attributes should be applied
1196 at instantiation time. */
1198 static tree
1199 splice_template_attributes (tree *attr_p, tree decl)
1201 tree *p = attr_p;
1202 tree late_attrs = NULL_TREE;
1203 tree *q = &late_attrs;
1205 if (!p)
1206 return NULL_TREE;
1208 for (; *p; )
1210 if (is_late_template_attribute (*p, decl))
1212 ATTR_IS_DEPENDENT (*p) = 1;
1213 *q = *p;
1214 *p = TREE_CHAIN (*p);
1215 q = &TREE_CHAIN (*q);
1216 *q = NULL_TREE;
1218 else
1219 p = &TREE_CHAIN (*p);
1222 return late_attrs;
1225 /* Remove any late attributes from the list in ATTR_P and attach them to
1226 DECL_P. */
1228 static void
1229 save_template_attributes (tree *attr_p, tree *decl_p)
1231 tree *q;
1233 if (attr_p && *attr_p == error_mark_node)
1234 return;
1236 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1237 if (!late_attrs)
1238 return;
1240 if (DECL_P (*decl_p))
1241 q = &DECL_ATTRIBUTES (*decl_p);
1242 else
1243 q = &TYPE_ATTRIBUTES (*decl_p);
1245 tree old_attrs = *q;
1247 /* Merge the late attributes at the beginning with the attribute
1248 list. */
1249 late_attrs = merge_attributes (late_attrs, *q);
1250 *q = late_attrs;
1252 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1254 /* We've added new attributes directly to the main variant, so
1255 now we need to update all of the other variants to include
1256 these new attributes. */
1257 tree variant;
1258 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1259 variant = TYPE_NEXT_VARIANT (variant))
1261 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1262 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1267 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1268 to a typedef which gives a previously anonymous class or enum a name for
1269 linkage purposes. */
1271 bool
1272 attributes_naming_typedef_ok (tree attrs)
1274 for (; attrs; attrs = TREE_CHAIN (attrs))
1276 tree name = get_attribute_name (attrs);
1277 if (is_attribute_p ("vector_size", name))
1278 return false;
1280 return true;
1283 /* Like reconstruct_complex_type, but handle also template trees. */
1285 tree
1286 cp_reconstruct_complex_type (tree type, tree bottom)
1288 tree inner, outer;
1290 if (TYPE_PTR_P (type))
1292 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1293 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1294 TYPE_REF_CAN_ALIAS_ALL (type));
1296 else if (TREE_CODE (type) == REFERENCE_TYPE)
1298 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1299 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1300 TYPE_REF_CAN_ALIAS_ALL (type));
1302 else if (TREE_CODE (type) == ARRAY_TYPE)
1304 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1305 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1306 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1307 element type qualification will be handled by the recursive
1308 cp_reconstruct_complex_type call and cp_build_qualified_type
1309 for ARRAY_TYPEs changes the element type. */
1310 return outer;
1312 else if (TREE_CODE (type) == FUNCTION_TYPE)
1314 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1315 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1316 outer = apply_memfn_quals (outer,
1317 type_memfn_quals (type),
1318 type_memfn_rqual (type));
1320 else if (TREE_CODE (type) == METHOD_TYPE)
1322 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1323 /* The build_method_type_directly() routine prepends 'this' to argument list,
1324 so we must compensate by getting rid of it. */
1325 outer
1326 = build_method_type_directly
1327 (class_of_this_parm (type), inner,
1328 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1330 else if (TREE_CODE (type) == OFFSET_TYPE)
1332 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1333 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1335 else
1336 return bottom;
1338 if (TYPE_ATTRIBUTES (type))
1339 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1340 return cp_build_qualified_type (outer, cp_type_quals (type));
1343 /* Replaces any constexpr expression that may be into the attributes
1344 arguments with their reduced value. */
1346 static void
1347 cp_check_const_attributes (tree attributes)
1349 if (attributes == error_mark_node)
1350 return;
1352 tree attr;
1353 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1355 tree arg;
1356 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1358 tree expr = TREE_VALUE (arg);
1359 if (EXPR_P (expr))
1360 TREE_VALUE (arg) = maybe_constant_value (expr);
1365 /* Return true if TYPE is an OpenMP mappable type. */
1366 bool
1367 cp_omp_mappable_type (tree type)
1369 /* Mappable type has to be complete. */
1370 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1371 return false;
1372 /* Arrays have mappable type if the elements have mappable type. */
1373 while (TREE_CODE (type) == ARRAY_TYPE)
1374 type = TREE_TYPE (type);
1375 /* A mappable type cannot contain virtual members. */
1376 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1377 return false;
1378 /* All data members must be non-static. */
1379 if (CLASS_TYPE_P (type))
1381 tree field;
1382 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1383 if (TREE_CODE (field) == VAR_DECL)
1384 return false;
1385 /* All fields must have mappable types. */
1386 else if (TREE_CODE (field) == FIELD_DECL
1387 && !cp_omp_mappable_type (TREE_TYPE (field)))
1388 return false;
1390 return true;
1393 /* Like decl_attributes, but handle C++ complexity. */
1395 void
1396 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1398 if (*decl == NULL_TREE || *decl == void_type_node
1399 || *decl == error_mark_node)
1400 return;
1402 /* Add implicit "omp declare target" attribute if requested. */
1403 if (scope_chain->omp_declare_target_attribute
1404 && ((TREE_CODE (*decl) == VAR_DECL && TREE_STATIC (*decl))
1405 || TREE_CODE (*decl) == FUNCTION_DECL))
1407 if (TREE_CODE (*decl) == VAR_DECL
1408 && DECL_CLASS_SCOPE_P (*decl))
1409 error ("%q+D static data member inside of declare target directive",
1410 *decl);
1411 else if (TREE_CODE (*decl) == VAR_DECL
1412 && (DECL_FUNCTION_SCOPE_P (*decl)
1413 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1414 error ("%q+D in block scope inside of declare target directive",
1415 *decl);
1416 else if (!processing_template_decl
1417 && TREE_CODE (*decl) == VAR_DECL
1418 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1419 error ("%q+D in declare target directive does not have mappable type",
1420 *decl);
1421 else
1422 attributes = tree_cons (get_identifier ("omp declare target"),
1423 NULL_TREE, attributes);
1426 if (processing_template_decl)
1428 if (check_for_bare_parameter_packs (attributes))
1429 return;
1431 save_template_attributes (&attributes, decl);
1434 cp_check_const_attributes (attributes);
1436 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1437 decl = &DECL_TEMPLATE_RESULT (*decl);
1439 decl_attributes (decl, attributes, flags);
1441 if (TREE_CODE (*decl) == TYPE_DECL)
1442 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1445 /* Walks through the namespace- or function-scope anonymous union
1446 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1447 Returns one of the fields for use in the mangled name. */
1449 static tree
1450 build_anon_union_vars (tree type, tree object)
1452 tree main_decl = NULL_TREE;
1453 tree field;
1455 /* Rather than write the code to handle the non-union case,
1456 just give an error. */
1457 if (TREE_CODE (type) != UNION_TYPE)
1459 error ("anonymous struct not inside named type");
1460 return error_mark_node;
1463 for (field = TYPE_FIELDS (type);
1464 field != NULL_TREE;
1465 field = DECL_CHAIN (field))
1467 tree decl;
1468 tree ref;
1470 if (DECL_ARTIFICIAL (field))
1471 continue;
1472 if (TREE_CODE (field) != FIELD_DECL)
1474 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1475 "have non-static data members", field);
1476 continue;
1479 if (TREE_PRIVATE (field))
1480 permerror (input_location, "private member %q+#D in anonymous union", field);
1481 else if (TREE_PROTECTED (field))
1482 permerror (input_location, "protected member %q+#D in anonymous union", field);
1484 if (processing_template_decl)
1485 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1486 DECL_NAME (field), NULL_TREE);
1487 else
1488 ref = build_class_member_access_expr (object, field, NULL_TREE,
1489 false, tf_warning_or_error);
1491 if (DECL_NAME (field))
1493 tree base;
1495 decl = build_decl (input_location,
1496 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1497 DECL_ANON_UNION_VAR_P (decl) = 1;
1498 DECL_ARTIFICIAL (decl) = 1;
1500 base = get_base_address (object);
1501 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1502 TREE_STATIC (decl) = TREE_STATIC (base);
1503 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1505 SET_DECL_VALUE_EXPR (decl, ref);
1506 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1508 decl = pushdecl (decl);
1510 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1511 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1512 else
1513 decl = 0;
1515 if (main_decl == NULL_TREE)
1516 main_decl = decl;
1519 return main_decl;
1522 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1523 anonymous union, then all members must be laid out together. PUBLIC_P
1524 is nonzero if this union is not declared static. */
1526 void
1527 finish_anon_union (tree anon_union_decl)
1529 tree type;
1530 tree main_decl;
1531 bool public_p;
1533 if (anon_union_decl == error_mark_node)
1534 return;
1536 type = TREE_TYPE (anon_union_decl);
1537 public_p = TREE_PUBLIC (anon_union_decl);
1539 /* The VAR_DECL's context is the same as the TYPE's context. */
1540 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1542 if (TYPE_FIELDS (type) == NULL_TREE)
1543 return;
1545 if (public_p)
1547 error ("namespace-scope anonymous aggregates must be static");
1548 return;
1551 main_decl = build_anon_union_vars (type, anon_union_decl);
1552 if (main_decl == error_mark_node)
1553 return;
1554 if (main_decl == NULL_TREE)
1556 warning (0, "anonymous union with no members");
1557 return;
1560 if (!processing_template_decl)
1562 /* Use main_decl to set the mangled name. */
1563 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1564 maybe_commonize_var (anon_union_decl);
1565 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1566 mangle_decl (anon_union_decl);
1567 DECL_NAME (anon_union_decl) = NULL_TREE;
1570 pushdecl (anon_union_decl);
1571 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1574 /* Auxiliary functions to make type signatures for
1575 `operator new' and `operator delete' correspond to
1576 what compiler will be expecting. */
1578 tree
1579 coerce_new_type (tree type)
1581 int e = 0;
1582 tree args = TYPE_ARG_TYPES (type);
1584 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1586 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1588 e = 1;
1589 error ("%<operator new%> must return type %qT", ptr_type_node);
1592 if (args && args != void_list_node)
1594 if (TREE_PURPOSE (args))
1596 /* [basic.stc.dynamic.allocation]
1598 The first parameter shall not have an associated default
1599 argument. */
1600 error ("the first parameter of %<operator new%> cannot "
1601 "have a default argument");
1602 /* Throw away the default argument. */
1603 TREE_PURPOSE (args) = NULL_TREE;
1606 if (!same_type_p (TREE_VALUE (args), size_type_node))
1608 e = 2;
1609 args = TREE_CHAIN (args);
1612 else
1613 e = 2;
1615 if (e == 2)
1616 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1617 "as first parameter", size_type_node);
1619 switch (e)
1621 case 2:
1622 args = tree_cons (NULL_TREE, size_type_node, args);
1623 /* Fall through. */
1624 case 1:
1625 type = build_exception_variant
1626 (build_function_type (ptr_type_node, args),
1627 TYPE_RAISES_EXCEPTIONS (type));
1628 /* Fall through. */
1629 default:;
1631 return type;
1634 tree
1635 coerce_delete_type (tree type)
1637 int e = 0;
1638 tree args = TYPE_ARG_TYPES (type);
1640 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1642 if (!same_type_p (TREE_TYPE (type), void_type_node))
1644 e = 1;
1645 error ("%<operator delete%> must return type %qT", void_type_node);
1648 if (!args || args == void_list_node
1649 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1651 e = 2;
1652 if (args && args != void_list_node)
1653 args = TREE_CHAIN (args);
1654 error ("%<operator delete%> takes type %qT as first parameter",
1655 ptr_type_node);
1657 switch (e)
1659 case 2:
1660 args = tree_cons (NULL_TREE, ptr_type_node, args);
1661 /* Fall through. */
1662 case 1:
1663 type = build_exception_variant
1664 (build_function_type (void_type_node, args),
1665 TYPE_RAISES_EXCEPTIONS (type));
1666 /* Fall through. */
1667 default:;
1670 return type;
1673 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1674 and mark them as needed. */
1676 static void
1677 mark_vtable_entries (tree decl)
1679 tree fnaddr;
1680 unsigned HOST_WIDE_INT idx;
1682 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1683 idx, fnaddr)
1685 tree fn;
1687 STRIP_NOPS (fnaddr);
1689 if (TREE_CODE (fnaddr) != ADDR_EXPR
1690 && TREE_CODE (fnaddr) != FDESC_EXPR)
1691 /* This entry is an offset: a virtual base class offset, a
1692 virtual call offset, an RTTI offset, etc. */
1693 continue;
1695 fn = TREE_OPERAND (fnaddr, 0);
1696 TREE_ADDRESSABLE (fn) = 1;
1697 /* When we don't have vcall offsets, we output thunks whenever
1698 we output the vtables that contain them. With vcall offsets,
1699 we know all the thunks we'll need when we emit a virtual
1700 function, so we emit the thunks there instead. */
1701 if (DECL_THUNK_P (fn))
1702 use_thunk (fn, /*emit_p=*/0);
1703 mark_used (fn);
1707 /* Set DECL up to have the closest approximation of "initialized common"
1708 linkage available. */
1710 void
1711 comdat_linkage (tree decl)
1713 if (flag_weak)
1714 make_decl_one_only (decl, cxx_comdat_group (decl));
1715 else if (TREE_CODE (decl) == FUNCTION_DECL
1716 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1717 /* We can just emit function and compiler-generated variables
1718 statically; having multiple copies is (for the most part) only
1719 a waste of space.
1721 There are two correctness issues, however: the address of a
1722 template instantiation with external linkage should be the
1723 same, independent of what translation unit asks for the
1724 address, and this will not hold when we emit multiple copies of
1725 the function. However, there's little else we can do.
1727 Also, by default, the typeinfo implementation assumes that
1728 there will be only one copy of the string used as the name for
1729 each type. Therefore, if weak symbols are unavailable, the
1730 run-time library should perform a more conservative check; it
1731 should perform a string comparison, rather than an address
1732 comparison. */
1733 TREE_PUBLIC (decl) = 0;
1734 else
1736 /* Static data member template instantiations, however, cannot
1737 have multiple copies. */
1738 if (DECL_INITIAL (decl) == 0
1739 || DECL_INITIAL (decl) == error_mark_node)
1740 DECL_COMMON (decl) = 1;
1741 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1743 DECL_COMMON (decl) = 1;
1744 DECL_INITIAL (decl) = error_mark_node;
1746 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1748 /* We can't do anything useful; leave vars for explicit
1749 instantiation. */
1750 DECL_EXTERNAL (decl) = 1;
1751 DECL_NOT_REALLY_EXTERN (decl) = 0;
1755 DECL_COMDAT (decl) = 1;
1758 /* For win32 we also want to put explicit instantiations in
1759 linkonce sections, so that they will be merged with implicit
1760 instantiations; otherwise we get duplicate symbol errors.
1761 For Darwin we do not want explicit instantiations to be
1762 linkonce. */
1764 void
1765 maybe_make_one_only (tree decl)
1767 /* We used to say that this was not necessary on targets that support weak
1768 symbols, because the implicit instantiations will defer to the explicit
1769 one. However, that's not actually the case in SVR4; a strong definition
1770 after a weak one is an error. Also, not making explicit
1771 instantiations one_only means that we can end up with two copies of
1772 some template instantiations. */
1773 if (! flag_weak)
1774 return;
1776 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1777 we can get away with not emitting them if they aren't used. We need
1778 to for variables so that cp_finish_decl will update their linkage,
1779 because their DECL_INITIAL may not have been set properly yet. */
1781 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1782 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1783 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1785 make_decl_one_only (decl, cxx_comdat_group (decl));
1787 if (VAR_P (decl))
1789 varpool_node *node = varpool_node_for_decl (decl);
1790 DECL_COMDAT (decl) = 1;
1791 /* Mark it needed so we don't forget to emit it. */
1792 node->forced_by_abi = true;
1793 TREE_USED (decl) = 1;
1798 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1799 This predicate will give the right answer during parsing of the
1800 function, which other tests may not. */
1802 bool
1803 vague_linkage_p (tree decl)
1805 /* Unfortunately, import_export_decl has not always been called
1806 before the function is processed, so we cannot simply check
1807 DECL_COMDAT. */
1808 return (DECL_COMDAT (decl)
1809 || (((TREE_CODE (decl) == FUNCTION_DECL
1810 && DECL_DECLARED_INLINE_P (decl))
1811 || (DECL_LANG_SPECIFIC (decl)
1812 && DECL_TEMPLATE_INSTANTIATION (decl)))
1813 && TREE_PUBLIC (decl)));
1816 /* Determine whether or not we want to specifically import or export CTYPE,
1817 using various heuristics. */
1819 static void
1820 import_export_class (tree ctype)
1822 /* -1 for imported, 1 for exported. */
1823 int import_export = 0;
1825 /* It only makes sense to call this function at EOF. The reason is
1826 that this function looks at whether or not the first non-inline
1827 non-abstract virtual member function has been defined in this
1828 translation unit. But, we can't possibly know that until we've
1829 seen the entire translation unit. */
1830 gcc_assert (at_eof);
1832 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1833 return;
1835 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1836 we will have CLASSTYPE_INTERFACE_ONLY set but not
1837 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1838 heuristic because someone will supply a #pragma implementation
1839 elsewhere, and deducing it here would produce a conflict. */
1840 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1841 return;
1843 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1844 import_export = -1;
1845 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1846 import_export = 1;
1847 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1848 && !flag_implicit_templates)
1849 /* For a template class, without -fimplicit-templates, check the
1850 repository. If the virtual table is assigned to this
1851 translation unit, then export the class; otherwise, import
1852 it. */
1853 import_export = repo_export_class_p (ctype) ? 1 : -1;
1854 else if (TYPE_POLYMORPHIC_P (ctype))
1856 /* The ABI specifies that the virtual table and associated
1857 information are emitted with the key method, if any. */
1858 tree method = CLASSTYPE_KEY_METHOD (ctype);
1859 /* If weak symbol support is not available, then we must be
1860 careful not to emit the vtable when the key function is
1861 inline. An inline function can be defined in multiple
1862 translation units. If we were to emit the vtable in each
1863 translation unit containing a definition, we would get
1864 multiple definition errors at link-time. */
1865 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1866 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1869 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1870 a definition anywhere else. */
1871 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1872 import_export = 0;
1874 /* Allow back ends the chance to overrule the decision. */
1875 if (targetm.cxx.import_export_class)
1876 import_export = targetm.cxx.import_export_class (ctype, import_export);
1878 if (import_export)
1880 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1881 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1885 /* Return true if VAR has already been provided to the back end; in that
1886 case VAR should not be modified further by the front end. */
1887 static bool
1888 var_finalized_p (tree var)
1890 return varpool_node_for_decl (var)->definition;
1893 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1894 must be emitted in this translation unit. Mark it as such. */
1896 void
1897 mark_needed (tree decl)
1899 TREE_USED (decl) = 1;
1900 if (TREE_CODE (decl) == FUNCTION_DECL)
1902 /* Extern inline functions don't become needed when referenced.
1903 If we know a method will be emitted in other TU and no new
1904 functions can be marked reachable, just use the external
1905 definition. */
1906 struct cgraph_node *node = cgraph_get_create_node (decl);
1907 node->forced_by_abi = true;
1909 else if (TREE_CODE (decl) == VAR_DECL)
1911 varpool_node *node = varpool_node_for_decl (decl);
1912 /* C++ frontend use mark_decl_references to force COMDAT variables
1913 to be output that might appear dead otherwise. */
1914 node->forced_by_abi = true;
1918 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1919 returns true if a definition of this entity should be provided in
1920 this object file. Callers use this function to determine whether
1921 or not to let the back end know that a definition of DECL is
1922 available in this translation unit. */
1924 bool
1925 decl_needed_p (tree decl)
1927 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1928 /* This function should only be called at the end of the translation
1929 unit. We cannot be sure of whether or not something will be
1930 COMDAT until that point. */
1931 gcc_assert (at_eof);
1933 /* All entities with external linkage that are not COMDAT should be
1934 emitted; they may be referred to from other object files. */
1935 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1936 return true;
1937 /* If this entity was used, let the back end see it; it will decide
1938 whether or not to emit it into the object file. */
1939 if (TREE_USED (decl))
1940 return true;
1941 /* Functions marked "dllexport" must be emitted so that they are
1942 visible to other DLLs. */
1943 if (flag_keep_inline_dllexport
1944 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1945 return true;
1946 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1947 reference to DECL might cause it to be emitted later. */
1948 return false;
1951 /* If necessary, write out the vtables for the dynamic class CTYPE.
1952 Returns true if any vtables were emitted. */
1954 static bool
1955 maybe_emit_vtables (tree ctype)
1957 tree vtbl;
1958 tree primary_vtbl;
1959 int needed = 0;
1960 varpool_node *current = NULL, *last = NULL;
1962 /* If the vtables for this class have already been emitted there is
1963 nothing more to do. */
1964 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1965 if (var_finalized_p (primary_vtbl))
1966 return false;
1967 /* Ignore dummy vtables made by get_vtable_decl. */
1968 if (TREE_TYPE (primary_vtbl) == void_type_node)
1969 return false;
1971 /* On some targets, we cannot determine the key method until the end
1972 of the translation unit -- which is when this function is
1973 called. */
1974 if (!targetm.cxx.key_method_may_be_inline ())
1975 determine_key_method (ctype);
1977 /* See if any of the vtables are needed. */
1978 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1980 import_export_decl (vtbl);
1981 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1982 needed = 1;
1984 if (!needed)
1986 /* If the references to this class' vtables are optimized away,
1987 still emit the appropriate debugging information. See
1988 dfs_debug_mark. */
1989 if (DECL_COMDAT (primary_vtbl)
1990 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1991 note_debug_info_needed (ctype);
1992 return false;
1995 /* The ABI requires that we emit all of the vtables if we emit any
1996 of them. */
1997 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1999 /* Mark entities references from the virtual table as used. */
2000 mark_vtable_entries (vtbl);
2002 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2004 vec<tree, va_gc> *cleanups = NULL;
2005 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2006 LOOKUP_NORMAL);
2008 /* It had better be all done at compile-time. */
2009 gcc_assert (!expr && !cleanups);
2012 /* Write it out. */
2013 DECL_EXTERNAL (vtbl) = 0;
2014 rest_of_decl_compilation (vtbl, 1, 1);
2016 /* Because we're only doing syntax-checking, we'll never end up
2017 actually marking the variable as written. */
2018 if (flag_syntax_only)
2019 TREE_ASM_WRITTEN (vtbl) = 1;
2020 else if (DECL_ONE_ONLY (vtbl))
2022 current = varpool_node_for_decl (vtbl);
2023 if (last)
2024 symtab_add_to_same_comdat_group (current, last);
2025 last = current;
2029 /* Since we're writing out the vtable here, also write the debug
2030 info. */
2031 note_debug_info_needed (ctype);
2033 return true;
2036 /* A special return value from type_visibility meaning internal
2037 linkage. */
2039 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2041 /* walk_tree helper function for type_visibility. */
2043 static tree
2044 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2046 int *vis_p = (int *)data;
2047 if (! TYPE_P (*tp))
2049 *walk_subtrees = 0;
2051 else if (OVERLOAD_TYPE_P (*tp)
2052 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2054 *vis_p = VISIBILITY_ANON;
2055 return *tp;
2057 else if (CLASS_TYPE_P (*tp)
2058 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2059 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2060 return NULL;
2063 /* Returns the visibility of TYPE, which is the minimum visibility of its
2064 component types. */
2066 static int
2067 type_visibility (tree type)
2069 int vis = VISIBILITY_DEFAULT;
2070 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2071 return vis;
2074 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2075 specified (or if VISIBILITY is static). If TMPL is true, this
2076 constraint is for a template argument, and takes precedence
2077 over explicitly-specified visibility on the template. */
2079 static void
2080 constrain_visibility (tree decl, int visibility, bool tmpl)
2082 if (visibility == VISIBILITY_ANON)
2084 /* extern "C" declarations aren't affected by the anonymous
2085 namespace. */
2086 if (!DECL_EXTERN_C_P (decl))
2088 TREE_PUBLIC (decl) = 0;
2089 DECL_WEAK (decl) = 0;
2090 DECL_COMMON (decl) = 0;
2091 DECL_COMDAT_GROUP (decl) = NULL_TREE;
2092 DECL_INTERFACE_KNOWN (decl) = 1;
2093 if (DECL_LANG_SPECIFIC (decl))
2094 DECL_NOT_REALLY_EXTERN (decl) = 1;
2097 else if (visibility > DECL_VISIBILITY (decl)
2098 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2100 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2101 /* This visibility was not specified. */
2102 DECL_VISIBILITY_SPECIFIED (decl) = false;
2106 /* Constrain the visibility of DECL based on the visibility of its template
2107 arguments. */
2109 static void
2110 constrain_visibility_for_template (tree decl, tree targs)
2112 /* If this is a template instantiation, check the innermost
2113 template args for visibility constraints. The outer template
2114 args are covered by the class check. */
2115 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2116 int i;
2117 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2119 int vis = 0;
2121 tree arg = TREE_VEC_ELT (args, i-1);
2122 if (TYPE_P (arg))
2123 vis = type_visibility (arg);
2124 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2126 STRIP_NOPS (arg);
2127 if (TREE_CODE (arg) == ADDR_EXPR)
2128 arg = TREE_OPERAND (arg, 0);
2129 if (VAR_OR_FUNCTION_DECL_P (arg))
2131 if (! TREE_PUBLIC (arg))
2132 vis = VISIBILITY_ANON;
2133 else
2134 vis = DECL_VISIBILITY (arg);
2137 if (vis)
2138 constrain_visibility (decl, vis, true);
2142 /* Like c_determine_visibility, but with additional C++-specific
2143 behavior.
2145 Function-scope entities can rely on the function's visibility because
2146 it is set in start_preparsed_function.
2148 Class-scope entities cannot rely on the class's visibility until the end
2149 of the enclosing class definition.
2151 Note that because namespaces have multiple independent definitions,
2152 namespace visibility is handled elsewhere using the #pragma visibility
2153 machinery rather than by decorating the namespace declaration.
2155 The goal is for constraints from the type to give a diagnostic, and
2156 other constraints to be applied silently. */
2158 void
2159 determine_visibility (tree decl)
2161 tree class_type = NULL_TREE;
2162 bool use_template;
2163 bool orig_visibility_specified;
2164 enum symbol_visibility orig_visibility;
2166 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2168 /* Only relevant for names with external linkage. */
2169 if (!TREE_PUBLIC (decl))
2170 return;
2172 /* Cloned constructors and destructors get the same visibility as
2173 the underlying function. That should be set up in
2174 maybe_clone_body. */
2175 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2177 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2178 orig_visibility = DECL_VISIBILITY (decl);
2180 if (TREE_CODE (decl) == TYPE_DECL)
2182 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2183 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2184 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2185 use_template = 1;
2186 else
2187 use_template = 0;
2189 else if (DECL_LANG_SPECIFIC (decl))
2190 use_template = DECL_USE_TEMPLATE (decl);
2191 else
2192 use_template = 0;
2194 /* If DECL is a member of a class, visibility specifiers on the
2195 class can influence the visibility of the DECL. */
2196 if (DECL_CLASS_SCOPE_P (decl))
2197 class_type = DECL_CONTEXT (decl);
2198 else
2200 /* Not a class member. */
2202 /* Virtual tables have DECL_CONTEXT set to their associated class,
2203 so they are automatically handled above. */
2204 gcc_assert (!VAR_P (decl)
2205 || !DECL_VTABLE_OR_VTT_P (decl));
2207 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2209 /* Local statics and classes get the visibility of their
2210 containing function by default, except that
2211 -fvisibility-inlines-hidden doesn't affect them. */
2212 tree fn = DECL_CONTEXT (decl);
2213 if (DECL_VISIBILITY_SPECIFIED (fn))
2215 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2216 DECL_VISIBILITY_SPECIFIED (decl) =
2217 DECL_VISIBILITY_SPECIFIED (fn);
2219 else
2221 if (DECL_CLASS_SCOPE_P (fn))
2222 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2223 else if (determine_hidden_inline (fn))
2225 DECL_VISIBILITY (decl) = default_visibility;
2226 DECL_VISIBILITY_SPECIFIED (decl) =
2227 visibility_options.inpragma;
2229 else
2231 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2232 DECL_VISIBILITY_SPECIFIED (decl) =
2233 DECL_VISIBILITY_SPECIFIED (fn);
2237 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2238 but have no TEMPLATE_INFO, so don't try to check it. */
2239 use_template = 0;
2241 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2242 && flag_visibility_ms_compat)
2244 /* Under -fvisibility-ms-compat, types are visible by default,
2245 even though their contents aren't. */
2246 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2247 int underlying_vis = type_visibility (underlying_type);
2248 if (underlying_vis == VISIBILITY_ANON
2249 || (CLASS_TYPE_P (underlying_type)
2250 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2251 constrain_visibility (decl, underlying_vis, false);
2252 else
2253 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2255 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2257 /* tinfo visibility is based on the type it's for. */
2258 constrain_visibility
2259 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2261 /* Give the target a chance to override the visibility associated
2262 with DECL. */
2263 if (TREE_PUBLIC (decl)
2264 && !DECL_REALLY_EXTERN (decl)
2265 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2266 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2267 targetm.cxx.determine_class_data_visibility (decl);
2269 else if (use_template)
2270 /* Template instantiations and specializations get visibility based
2271 on their template unless they override it with an attribute. */;
2272 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2274 if (determine_hidden_inline (decl))
2275 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2276 else
2278 /* Set default visibility to whatever the user supplied with
2279 #pragma GCC visibility or a namespace visibility attribute. */
2280 DECL_VISIBILITY (decl) = default_visibility;
2281 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2286 if (use_template)
2288 /* If the specialization doesn't specify visibility, use the
2289 visibility from the template. */
2290 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2291 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2292 : DECL_TEMPLATE_INFO (decl));
2293 tree args = TI_ARGS (tinfo);
2294 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2295 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2296 : DECL_ATTRIBUTES (decl));
2298 if (args != error_mark_node)
2300 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2302 if (!DECL_VISIBILITY_SPECIFIED (decl))
2304 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2305 && determine_hidden_inline (decl))
2306 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2307 else
2309 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2310 DECL_VISIBILITY_SPECIFIED (decl)
2311 = DECL_VISIBILITY_SPECIFIED (pattern);
2315 if (args
2316 /* Template argument visibility outweighs #pragma or namespace
2317 visibility, but not an explicit attribute. */
2318 && !lookup_attribute ("visibility", attribs))
2320 int depth = TMPL_ARGS_DEPTH (args);
2321 if (DECL_VISIBILITY_SPECIFIED (decl))
2323 /* A class template member with explicit visibility
2324 overrides the class visibility, so we need to apply
2325 all the levels of template args directly. */
2326 int i;
2327 for (i = 1; i <= depth; ++i)
2329 tree lev = TMPL_ARGS_LEVEL (args, i);
2330 constrain_visibility_for_template (decl, lev);
2333 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2334 /* Limit visibility based on its template arguments. */
2335 constrain_visibility_for_template (decl, args);
2340 if (class_type)
2341 determine_visibility_from_class (decl, class_type);
2343 if (decl_anon_ns_mem_p (decl))
2344 /* Names in an anonymous namespace get internal linkage.
2345 This might change once we implement export. */
2346 constrain_visibility (decl, VISIBILITY_ANON, false);
2347 else if (TREE_CODE (decl) != TYPE_DECL)
2349 /* Propagate anonymity from type to decl. */
2350 int tvis = type_visibility (TREE_TYPE (decl));
2351 if (tvis == VISIBILITY_ANON
2352 || ! DECL_VISIBILITY_SPECIFIED (decl))
2353 constrain_visibility (decl, tvis, false);
2355 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2356 /* DR 757: A type without linkage shall not be used as the type of a
2357 variable or function with linkage, unless
2358 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2359 o the variable or function is not used (3.2 [basic.def.odr]) or is
2360 defined in the same translation unit.
2362 Since non-extern "C" decls need to be defined in the same
2363 translation unit, we can make the type internal. */
2364 constrain_visibility (decl, VISIBILITY_ANON, false);
2366 /* If visibility changed and DECL already has DECL_RTL, ensure
2367 symbol flags are updated. */
2368 if ((DECL_VISIBILITY (decl) != orig_visibility
2369 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2370 && ((VAR_P (decl) && TREE_STATIC (decl))
2371 || TREE_CODE (decl) == FUNCTION_DECL)
2372 && DECL_RTL_SET_P (decl))
2373 make_decl_rtl (decl);
2376 /* By default, static data members and function members receive
2377 the visibility of their containing class. */
2379 static void
2380 determine_visibility_from_class (tree decl, tree class_type)
2382 if (DECL_VISIBILITY_SPECIFIED (decl))
2383 return;
2385 if (determine_hidden_inline (decl))
2386 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2387 else
2389 /* Default to the class visibility. */
2390 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2391 DECL_VISIBILITY_SPECIFIED (decl)
2392 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2395 /* Give the target a chance to override the visibility associated
2396 with DECL. */
2397 if (VAR_P (decl)
2398 && (DECL_TINFO_P (decl)
2399 || (DECL_VTABLE_OR_VTT_P (decl)
2400 /* Construction virtual tables are not exported because
2401 they cannot be referred to from other object files;
2402 their name is not standardized by the ABI. */
2403 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2404 && TREE_PUBLIC (decl)
2405 && !DECL_REALLY_EXTERN (decl)
2406 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2407 targetm.cxx.determine_class_data_visibility (decl);
2410 /* Returns true iff DECL is an inline that should get hidden visibility
2411 because of -fvisibility-inlines-hidden. */
2413 static bool
2414 determine_hidden_inline (tree decl)
2416 return (visibility_options.inlines_hidden
2417 /* Don't do this for inline templates; specializations might not be
2418 inline, and we don't want them to inherit the hidden
2419 visibility. We'll set it here for all inline instantiations. */
2420 && !processing_template_decl
2421 && TREE_CODE (decl) == FUNCTION_DECL
2422 && DECL_DECLARED_INLINE_P (decl)
2423 && (! DECL_LANG_SPECIFIC (decl)
2424 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2427 /* Constrain the visibility of a class TYPE based on the visibility of its
2428 field types. Warn if any fields require lesser visibility. */
2430 void
2431 constrain_class_visibility (tree type)
2433 tree binfo;
2434 tree t;
2435 int i;
2437 int vis = type_visibility (type);
2439 if (vis == VISIBILITY_ANON
2440 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2441 return;
2443 /* Don't warn about visibility if the class has explicit visibility. */
2444 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2445 vis = VISIBILITY_INTERNAL;
2447 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2448 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2450 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2451 int subvis = type_visibility (ftype);
2453 if (subvis == VISIBILITY_ANON)
2455 if (!in_main_input_context ())
2456 warning (0, "\
2457 %qT has a field %qD whose type uses the anonymous namespace",
2458 type, t);
2460 else if (MAYBE_CLASS_TYPE_P (ftype)
2461 && vis < VISIBILITY_HIDDEN
2462 && subvis >= VISIBILITY_HIDDEN)
2463 warning (OPT_Wattributes, "\
2464 %qT declared with greater visibility than the type of its field %qD",
2465 type, t);
2468 binfo = TYPE_BINFO (type);
2469 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2471 int subvis = type_visibility (TREE_TYPE (t));
2473 if (subvis == VISIBILITY_ANON)
2475 if (!in_main_input_context())
2476 warning (0, "\
2477 %qT has a base %qT whose type uses the anonymous namespace",
2478 type, TREE_TYPE (t));
2480 else if (vis < VISIBILITY_HIDDEN
2481 && subvis >= VISIBILITY_HIDDEN)
2482 warning (OPT_Wattributes, "\
2483 %qT declared with greater visibility than its base %qT",
2484 type, TREE_TYPE (t));
2488 /* Functions for adjusting the visibility of a tagged type and its nested
2489 types and declarations when it gets a name for linkage purposes from a
2490 typedef. */
2492 static void bt_reset_linkage_1 (binding_entry, void *);
2493 static void bt_reset_linkage_2 (binding_entry, void *);
2495 /* First reset the visibility of all the types. */
2497 static void
2498 reset_type_linkage_1 (tree type)
2500 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2501 if (CLASS_TYPE_P (type))
2502 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2503 bt_reset_linkage_1, NULL);
2505 static void
2506 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2508 reset_type_linkage_1 (b->type);
2511 /* Then reset the visibility of any static data members or member
2512 functions that use those types. */
2514 static void
2515 reset_decl_linkage (tree decl)
2517 if (TREE_PUBLIC (decl))
2518 return;
2519 if (DECL_CLONED_FUNCTION_P (decl))
2520 return;
2521 TREE_PUBLIC (decl) = true;
2522 DECL_INTERFACE_KNOWN (decl) = false;
2523 determine_visibility (decl);
2524 tentative_decl_linkage (decl);
2526 static void
2527 reset_type_linkage_2 (tree type)
2529 if (CLASS_TYPE_P (type))
2531 if (tree vt = CLASSTYPE_VTABLES (type))
2533 tree name = mangle_vtbl_for_type (type);
2534 DECL_NAME (vt) = name;
2535 SET_DECL_ASSEMBLER_NAME (vt, name);
2536 reset_decl_linkage (vt);
2538 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2540 tree name = mangle_typeinfo_for_type (type);
2541 DECL_NAME (ti) = name;
2542 SET_DECL_ASSEMBLER_NAME (ti, name);
2543 TREE_TYPE (name) = type;
2544 reset_decl_linkage (ti);
2546 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2547 if (TREE_CODE (m) == VAR_DECL)
2548 reset_decl_linkage (m);
2549 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2550 reset_decl_linkage (m);
2551 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2552 bt_reset_linkage_2, NULL);
2555 static void
2556 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2558 reset_type_linkage_2 (b->type);
2560 void
2561 reset_type_linkage (tree type)
2563 reset_type_linkage_1 (type);
2564 reset_type_linkage_2 (type);
2567 /* Set up our initial idea of what the linkage of DECL should be. */
2569 void
2570 tentative_decl_linkage (tree decl)
2572 if (DECL_INTERFACE_KNOWN (decl))
2573 /* We've already made a decision as to how this function will
2574 be handled. */;
2575 else if (vague_linkage_p (decl))
2577 if (TREE_CODE (decl) == FUNCTION_DECL
2578 && decl_defined_p (decl))
2580 DECL_EXTERNAL (decl) = 1;
2581 DECL_NOT_REALLY_EXTERN (decl) = 1;
2582 note_vague_linkage_fn (decl);
2583 /* A non-template inline function with external linkage will
2584 always be COMDAT. As we must eventually determine the
2585 linkage of all functions, and as that causes writes to
2586 the data mapped in from the PCH file, it's advantageous
2587 to mark the functions at this point. */
2588 if (DECL_DECLARED_INLINE_P (decl)
2589 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2590 || DECL_DEFAULTED_FN (decl)))
2592 /* This function must have external linkage, as
2593 otherwise DECL_INTERFACE_KNOWN would have been
2594 set. */
2595 gcc_assert (TREE_PUBLIC (decl));
2596 comdat_linkage (decl);
2597 DECL_INTERFACE_KNOWN (decl) = 1;
2600 else if (TREE_CODE (decl) == VAR_DECL)
2601 maybe_commonize_var (decl);
2605 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2606 for DECL has not already been determined, do so now by setting
2607 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2608 function is called entities with vague linkage whose definitions
2609 are available must have TREE_PUBLIC set.
2611 If this function decides to place DECL in COMDAT, it will set
2612 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2613 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2614 callers defer that decision until it is clear that DECL is actually
2615 required. */
2617 void
2618 import_export_decl (tree decl)
2620 int emit_p;
2621 bool comdat_p;
2622 bool import_p;
2623 tree class_type = NULL_TREE;
2625 if (DECL_INTERFACE_KNOWN (decl))
2626 return;
2628 /* We cannot determine what linkage to give to an entity with vague
2629 linkage until the end of the file. For example, a virtual table
2630 for a class will be defined if and only if the key method is
2631 defined in this translation unit. As a further example, consider
2632 that when compiling a translation unit that uses PCH file with
2633 "-frepo" it would be incorrect to make decisions about what
2634 entities to emit when building the PCH; those decisions must be
2635 delayed until the repository information has been processed. */
2636 gcc_assert (at_eof);
2637 /* Object file linkage for explicit instantiations is handled in
2638 mark_decl_instantiated. For static variables in functions with
2639 vague linkage, maybe_commonize_var is used.
2641 Therefore, the only declarations that should be provided to this
2642 function are those with external linkage that are:
2644 * implicit instantiations of function templates
2646 * inline function
2648 * implicit instantiations of static data members of class
2649 templates
2651 * virtual tables
2653 * typeinfo objects
2655 Furthermore, all entities that reach this point must have a
2656 definition available in this translation unit.
2658 The following assertions check these conditions. */
2659 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2660 /* Any code that creates entities with TREE_PUBLIC cleared should
2661 also set DECL_INTERFACE_KNOWN. */
2662 gcc_assert (TREE_PUBLIC (decl));
2663 if (TREE_CODE (decl) == FUNCTION_DECL)
2664 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2665 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2666 || DECL_DECLARED_INLINE_P (decl));
2667 else
2668 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2669 || DECL_VTABLE_OR_VTT_P (decl)
2670 || DECL_TINFO_P (decl));
2671 /* Check that a definition of DECL is available in this translation
2672 unit. */
2673 gcc_assert (!DECL_REALLY_EXTERN (decl));
2675 /* Assume that DECL will not have COMDAT linkage. */
2676 comdat_p = false;
2677 /* Assume that DECL will not be imported into this translation
2678 unit. */
2679 import_p = false;
2681 /* See if the repository tells us whether or not to emit DECL in
2682 this translation unit. */
2683 emit_p = repo_emit_p (decl);
2684 if (emit_p == 0)
2685 import_p = true;
2686 else if (emit_p == 1)
2688 /* The repository indicates that this entity should be defined
2689 here. Make sure the back end honors that request. */
2690 if (VAR_P (decl))
2691 mark_needed (decl);
2692 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2693 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2695 tree clone;
2696 FOR_EACH_CLONE (clone, decl)
2697 mark_needed (clone);
2699 else
2700 mark_needed (decl);
2701 /* Output the definition as an ordinary strong definition. */
2702 DECL_EXTERNAL (decl) = 0;
2703 DECL_INTERFACE_KNOWN (decl) = 1;
2704 return;
2707 if (import_p)
2708 /* We have already decided what to do with this DECL; there is no
2709 need to check anything further. */
2711 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2713 class_type = DECL_CONTEXT (decl);
2714 import_export_class (class_type);
2715 if (TYPE_FOR_JAVA (class_type))
2716 import_p = true;
2717 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2718 && CLASSTYPE_INTERFACE_ONLY (class_type))
2719 import_p = true;
2720 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2721 && !CLASSTYPE_USE_TEMPLATE (class_type)
2722 && CLASSTYPE_KEY_METHOD (class_type)
2723 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2724 /* The ABI requires that all virtual tables be emitted with
2725 COMDAT linkage. However, on systems where COMDAT symbols
2726 don't show up in the table of contents for a static
2727 archive, or on systems without weak symbols (where we
2728 approximate COMDAT linkage by using internal linkage), the
2729 linker will report errors about undefined symbols because
2730 it will not see the virtual table definition. Therefore,
2731 in the case that we know that the virtual table will be
2732 emitted in only one translation unit, we make the virtual
2733 table an ordinary definition with external linkage. */
2734 DECL_EXTERNAL (decl) = 0;
2735 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2737 /* CLASS_TYPE is being exported from this translation unit,
2738 so DECL should be defined here. */
2739 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2740 /* If a class is declared in a header with the "extern
2741 template" extension, then it will not be instantiated,
2742 even in translation units that would normally require
2743 it. Often such classes are explicitly instantiated in
2744 one translation unit. Therefore, the explicit
2745 instantiation must be made visible to other translation
2746 units. */
2747 DECL_EXTERNAL (decl) = 0;
2748 else
2750 /* The generic C++ ABI says that class data is always
2751 COMDAT, even if there is a key function. Some
2752 variants (e.g., the ARM EABI) says that class data
2753 only has COMDAT linkage if the class data might be
2754 emitted in more than one translation unit. When the
2755 key method can be inline and is inline, we still have
2756 to arrange for comdat even though
2757 class_data_always_comdat is false. */
2758 if (!CLASSTYPE_KEY_METHOD (class_type)
2759 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2760 || targetm.cxx.class_data_always_comdat ())
2762 /* The ABI requires COMDAT linkage. Normally, we
2763 only emit COMDAT things when they are needed;
2764 make sure that we realize that this entity is
2765 indeed needed. */
2766 comdat_p = true;
2767 mark_needed (decl);
2771 else if (!flag_implicit_templates
2772 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2773 import_p = true;
2774 else
2775 comdat_p = true;
2777 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2779 tree type = TREE_TYPE (DECL_NAME (decl));
2780 if (CLASS_TYPE_P (type))
2782 class_type = type;
2783 import_export_class (type);
2784 if (CLASSTYPE_INTERFACE_KNOWN (type)
2785 && TYPE_POLYMORPHIC_P (type)
2786 && CLASSTYPE_INTERFACE_ONLY (type)
2787 /* If -fno-rtti was specified, then we cannot be sure
2788 that RTTI information will be emitted with the
2789 virtual table of the class, so we must emit it
2790 wherever it is used. */
2791 && flag_rtti)
2792 import_p = true;
2793 else
2795 if (CLASSTYPE_INTERFACE_KNOWN (type)
2796 && !CLASSTYPE_INTERFACE_ONLY (type))
2798 comdat_p = (targetm.cxx.class_data_always_comdat ()
2799 || (CLASSTYPE_KEY_METHOD (type)
2800 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2801 mark_needed (decl);
2802 if (!flag_weak)
2804 comdat_p = false;
2805 DECL_EXTERNAL (decl) = 0;
2808 else
2809 comdat_p = true;
2812 else
2813 comdat_p = true;
2815 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2817 /* DECL is an implicit instantiation of a function or static
2818 data member. */
2819 if ((flag_implicit_templates
2820 && !flag_use_repository)
2821 || (flag_implicit_inline_templates
2822 && TREE_CODE (decl) == FUNCTION_DECL
2823 && DECL_DECLARED_INLINE_P (decl)))
2824 comdat_p = true;
2825 else
2826 /* If we are not implicitly generating templates, then mark
2827 this entity as undefined in this translation unit. */
2828 import_p = true;
2830 else if (DECL_FUNCTION_MEMBER_P (decl))
2832 if (!DECL_DECLARED_INLINE_P (decl))
2834 tree ctype = DECL_CONTEXT (decl);
2835 import_export_class (ctype);
2836 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2838 DECL_NOT_REALLY_EXTERN (decl)
2839 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2840 || (DECL_DECLARED_INLINE_P (decl)
2841 && ! flag_implement_inlines
2842 && !DECL_VINDEX (decl)));
2844 if (!DECL_NOT_REALLY_EXTERN (decl))
2845 DECL_EXTERNAL (decl) = 1;
2847 /* Always make artificials weak. */
2848 if (DECL_ARTIFICIAL (decl) && flag_weak)
2849 comdat_p = true;
2850 else
2851 maybe_make_one_only (decl);
2854 else
2855 comdat_p = true;
2857 else
2858 comdat_p = true;
2860 if (import_p)
2862 /* If we are importing DECL into this translation unit, mark is
2863 an undefined here. */
2864 DECL_EXTERNAL (decl) = 1;
2865 DECL_NOT_REALLY_EXTERN (decl) = 0;
2867 else if (comdat_p)
2869 /* If we decided to put DECL in COMDAT, mark it accordingly at
2870 this point. */
2871 comdat_linkage (decl);
2874 DECL_INTERFACE_KNOWN (decl) = 1;
2877 /* Return an expression that performs the destruction of DECL, which
2878 must be a VAR_DECL whose type has a non-trivial destructor, or is
2879 an array whose (innermost) elements have a non-trivial destructor. */
2881 tree
2882 build_cleanup (tree decl)
2884 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2885 gcc_assert (clean != NULL_TREE);
2886 return clean;
2889 /* Returns the initialization guard variable for the variable DECL,
2890 which has static storage duration. */
2892 tree
2893 get_guard (tree decl)
2895 tree sname;
2896 tree guard;
2898 sname = mangle_guard_variable (decl);
2899 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2900 if (! guard)
2902 tree guard_type;
2904 /* We use a type that is big enough to contain a mutex as well
2905 as an integer counter. */
2906 guard_type = targetm.cxx.guard_type ();
2907 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2908 VAR_DECL, sname, guard_type);
2910 /* The guard should have the same linkage as what it guards. */
2911 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2912 TREE_STATIC (guard) = TREE_STATIC (decl);
2913 DECL_COMMON (guard) = DECL_COMMON (decl);
2914 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2915 DECL_TLS_MODEL (guard) = DECL_TLS_MODEL (decl);
2916 if (DECL_ONE_ONLY (decl))
2917 make_decl_one_only (guard, cxx_comdat_group (guard));
2918 if (TREE_PUBLIC (decl))
2919 DECL_WEAK (guard) = DECL_WEAK (decl);
2920 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2921 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2923 DECL_ARTIFICIAL (guard) = 1;
2924 DECL_IGNORED_P (guard) = 1;
2925 TREE_USED (guard) = 1;
2926 pushdecl_top_level_and_finish (guard, NULL_TREE);
2928 return guard;
2931 /* Return those bits of the GUARD variable that should be set when the
2932 guarded entity is actually initialized. */
2934 static tree
2935 get_guard_bits (tree guard)
2937 if (!targetm.cxx.guard_mask_bit ())
2939 /* We only set the first byte of the guard, in order to leave room
2940 for a mutex in the high-order bits. */
2941 guard = build1 (ADDR_EXPR,
2942 build_pointer_type (TREE_TYPE (guard)),
2943 guard);
2944 guard = build1 (NOP_EXPR,
2945 build_pointer_type (char_type_node),
2946 guard);
2947 guard = build1 (INDIRECT_REF, char_type_node, guard);
2950 return guard;
2953 /* Return an expression which determines whether or not the GUARD
2954 variable has already been initialized. */
2956 tree
2957 get_guard_cond (tree guard)
2959 tree guard_value;
2961 /* Check to see if the GUARD is zero. */
2962 guard = get_guard_bits (guard);
2964 /* Mask off all but the low bit. */
2965 if (targetm.cxx.guard_mask_bit ())
2967 guard_value = integer_one_node;
2968 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2969 guard_value = convert (TREE_TYPE (guard), guard_value);
2970 guard = cp_build_binary_op (input_location,
2971 BIT_AND_EXPR, guard, guard_value,
2972 tf_warning_or_error);
2975 guard_value = integer_zero_node;
2976 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2977 guard_value = convert (TREE_TYPE (guard), guard_value);
2978 return cp_build_binary_op (input_location,
2979 EQ_EXPR, guard, guard_value,
2980 tf_warning_or_error);
2983 /* Return an expression which sets the GUARD variable, indicating that
2984 the variable being guarded has been initialized. */
2986 tree
2987 set_guard (tree guard)
2989 tree guard_init;
2991 /* Set the GUARD to one. */
2992 guard = get_guard_bits (guard);
2993 guard_init = integer_one_node;
2994 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2995 guard_init = convert (TREE_TYPE (guard), guard_init);
2996 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2997 tf_warning_or_error);
3000 /* Returns true iff we can tell that VAR does not have a dynamic
3001 initializer. */
3003 static bool
3004 var_defined_without_dynamic_init (tree var)
3006 /* If it's defined in another TU, we can't tell. */
3007 if (DECL_EXTERNAL (var))
3008 return false;
3009 /* If it has a non-trivial destructor, registering the destructor
3010 counts as dynamic initialization. */
3011 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3012 return false;
3013 /* If it's in this TU, its initializer has been processed. */
3014 gcc_assert (DECL_INITIALIZED_P (var));
3015 /* If it has no initializer or a constant one, it's not dynamic. */
3016 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3017 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3020 /* Returns true iff VAR is a variable that needs uses to be
3021 wrapped for possible dynamic initialization. */
3023 static bool
3024 var_needs_tls_wrapper (tree var)
3026 return (!error_operand_p (var)
3027 && DECL_THREAD_LOCAL_P (var)
3028 && !DECL_GNU_TLS_P (var)
3029 && !DECL_FUNCTION_SCOPE_P (var)
3030 && !var_defined_without_dynamic_init (var));
3033 /* Get the FUNCTION_DECL for the shared TLS init function for this
3034 translation unit. */
3036 static tree
3037 get_local_tls_init_fn (void)
3039 tree sname = get_identifier ("__tls_init");
3040 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3041 if (!fn)
3043 fn = build_lang_decl (FUNCTION_DECL, sname,
3044 build_function_type (void_type_node,
3045 void_list_node));
3046 SET_DECL_LANGUAGE (fn, lang_c);
3047 TREE_PUBLIC (fn) = false;
3048 DECL_ARTIFICIAL (fn) = true;
3049 mark_used (fn);
3050 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3052 return fn;
3055 /* Get a FUNCTION_DECL for the init function for the thread_local
3056 variable VAR. The init function will be an alias to the function
3057 that initializes all the non-local TLS variables in the translation
3058 unit. The init function is only used by the wrapper function. */
3060 static tree
3061 get_tls_init_fn (tree var)
3063 /* Only C++11 TLS vars need this init fn. */
3064 if (!var_needs_tls_wrapper (var))
3065 return NULL_TREE;
3067 /* If -fno-extern-tls-init, assume that we don't need to call
3068 a tls init function for a variable defined in another TU. */
3069 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3070 return NULL_TREE;
3072 #ifdef ASM_OUTPUT_DEF
3073 /* If the variable is internal, or if we can't generate aliases,
3074 call the local init function directly. */
3075 if (!TREE_PUBLIC (var))
3076 #endif
3077 return get_local_tls_init_fn ();
3079 tree sname = mangle_tls_init_fn (var);
3080 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3081 if (!fn)
3083 fn = build_lang_decl (FUNCTION_DECL, sname,
3084 build_function_type (void_type_node,
3085 void_list_node));
3086 SET_DECL_LANGUAGE (fn, lang_c);
3087 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3088 DECL_ARTIFICIAL (fn) = true;
3089 DECL_COMDAT (fn) = DECL_COMDAT (var);
3090 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3091 if (DECL_ONE_ONLY (var))
3092 make_decl_one_only (fn, cxx_comdat_group (fn));
3093 if (TREE_PUBLIC (var))
3095 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3096 /* If the variable is defined somewhere else and might have static
3097 initialization, make the init function a weak reference. */
3098 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3099 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3100 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3101 && DECL_EXTERNAL (var))
3102 declare_weak (fn);
3103 else
3104 DECL_WEAK (fn) = DECL_WEAK (var);
3106 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3107 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3108 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3109 DECL_IGNORED_P (fn) = 1;
3110 mark_used (fn);
3112 DECL_BEFRIENDING_CLASSES (fn) = var;
3114 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3116 return fn;
3119 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3120 variable VAR. The wrapper function calls the init function (if any) for
3121 VAR and then returns a reference to VAR. The wrapper function is used
3122 in place of VAR everywhere VAR is mentioned. */
3124 tree
3125 get_tls_wrapper_fn (tree var)
3127 /* Only C++11 TLS vars need this wrapper fn. */
3128 if (!var_needs_tls_wrapper (var))
3129 return NULL_TREE;
3131 tree sname = mangle_tls_wrapper_fn (var);
3132 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3133 if (!fn)
3135 /* A named rvalue reference is an lvalue, so the wrapper should
3136 always return an lvalue reference. */
3137 tree type = non_reference (TREE_TYPE (var));
3138 type = build_reference_type (type);
3139 tree fntype = build_function_type (type, void_list_node);
3140 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3141 SET_DECL_LANGUAGE (fn, lang_c);
3142 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3143 DECL_ARTIFICIAL (fn) = true;
3144 DECL_IGNORED_P (fn) = 1;
3145 /* The wrapper is inline and emitted everywhere var is used. */
3146 DECL_DECLARED_INLINE_P (fn) = true;
3147 if (TREE_PUBLIC (var))
3149 comdat_linkage (fn);
3150 #ifdef HAVE_GAS_HIDDEN
3151 /* Make the wrapper bind locally; there's no reason to share
3152 the wrapper between multiple shared objects. */
3153 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3154 DECL_VISIBILITY_SPECIFIED (fn) = true;
3155 #endif
3157 if (!TREE_PUBLIC (fn))
3158 DECL_INTERFACE_KNOWN (fn) = true;
3159 mark_used (fn);
3160 note_vague_linkage_fn (fn);
3162 #if 0
3163 /* We want CSE to commonize calls to the wrapper, but marking it as
3164 pure is unsafe since it has side-effects. I guess we need a new
3165 ECF flag even weaker than ECF_PURE. FIXME! */
3166 DECL_PURE_P (fn) = true;
3167 #endif
3169 DECL_BEFRIENDING_CLASSES (fn) = var;
3171 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3173 return fn;
3176 /* At EOF, generate the definition for the TLS wrapper function FN:
3178 T& var_wrapper() {
3179 if (init_fn) init_fn();
3180 return var;
3181 } */
3183 static void
3184 generate_tls_wrapper (tree fn)
3186 tree var = DECL_BEFRIENDING_CLASSES (fn);
3188 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3189 tree body = begin_function_body ();
3190 /* Only call the init fn if there might be one. */
3191 if (tree init_fn = get_tls_init_fn (var))
3193 tree if_stmt = NULL_TREE;
3194 /* If init_fn is a weakref, make sure it exists before calling. */
3195 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3197 if_stmt = begin_if_stmt ();
3198 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3199 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3200 NE_EXPR, addr, nullptr_node,
3201 tf_warning_or_error);
3202 finish_if_stmt_cond (cond, if_stmt);
3204 finish_expr_stmt (build_cxx_call
3205 (init_fn, 0, NULL, tf_warning_or_error));
3206 if (if_stmt)
3208 finish_then_clause (if_stmt);
3209 finish_if_stmt (if_stmt);
3212 else
3213 /* If there's no initialization, the wrapper is a constant function. */
3214 TREE_READONLY (fn) = true;
3215 finish_return_stmt (convert_from_reference (var));
3216 finish_function_body (body);
3217 expand_or_defer_fn (finish_function (0));
3220 /* Start the process of running a particular set of global constructors
3221 or destructors. Subroutine of do_[cd]tors. Also called from
3222 vtv_start_verification_constructor_init_function. */
3224 static tree
3225 start_objects (int method_type, int initp)
3227 tree body;
3228 tree fndecl;
3229 char type[14];
3231 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3233 if (initp != DEFAULT_INIT_PRIORITY)
3235 char joiner;
3237 #ifdef JOINER
3238 joiner = JOINER;
3239 #else
3240 joiner = '_';
3241 #endif
3243 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3245 else
3246 sprintf (type, "sub_%c", method_type);
3248 fndecl = build_lang_decl (FUNCTION_DECL,
3249 get_file_function_name (type),
3250 build_function_type_list (void_type_node,
3251 NULL_TREE));
3252 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3254 TREE_PUBLIC (current_function_decl) = 0;
3256 /* Mark as artificial because it's not explicitly in the user's
3257 source code. */
3258 DECL_ARTIFICIAL (current_function_decl) = 1;
3260 /* Mark this declaration as used to avoid spurious warnings. */
3261 TREE_USED (current_function_decl) = 1;
3263 /* Mark this function as a global constructor or destructor. */
3264 if (method_type == 'I')
3265 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3266 else
3267 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3269 body = begin_compound_stmt (BCS_FN_BODY);
3271 return body;
3274 /* Finish the process of running a particular set of global constructors
3275 or destructors. Subroutine of do_[cd]tors. */
3277 static void
3278 finish_objects (int method_type, int initp, tree body)
3280 tree fn;
3282 /* Finish up. */
3283 finish_compound_stmt (body);
3284 fn = finish_function (0);
3286 if (method_type == 'I')
3288 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3289 decl_init_priority_insert (fn, initp);
3291 else
3293 DECL_STATIC_DESTRUCTOR (fn) = 1;
3294 decl_fini_priority_insert (fn, initp);
3297 expand_or_defer_fn (fn);
3300 /* The names of the parameters to the function created to handle
3301 initializations and destructions for objects with static storage
3302 duration. */
3303 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3304 #define PRIORITY_IDENTIFIER "__priority"
3306 /* The name of the function we create to handle initializations and
3307 destructions for objects with static storage duration. */
3308 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3310 /* The declaration for the __INITIALIZE_P argument. */
3311 static GTY(()) tree initialize_p_decl;
3313 /* The declaration for the __PRIORITY argument. */
3314 static GTY(()) tree priority_decl;
3316 /* The declaration for the static storage duration function. */
3317 static GTY(()) tree ssdf_decl;
3319 /* All the static storage duration functions created in this
3320 translation unit. */
3321 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3323 /* A map from priority levels to information about that priority
3324 level. There may be many such levels, so efficient lookup is
3325 important. */
3326 static splay_tree priority_info_map;
3328 /* Begins the generation of the function that will handle all
3329 initialization and destruction of objects with static storage
3330 duration. The function generated takes two parameters of type
3331 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3332 nonzero, it performs initializations. Otherwise, it performs
3333 destructions. It only performs those initializations or
3334 destructions with the indicated __PRIORITY. The generated function
3335 returns no value.
3337 It is assumed that this function will only be called once per
3338 translation unit. */
3340 static tree
3341 start_static_storage_duration_function (unsigned count)
3343 tree type;
3344 tree body;
3345 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3347 /* Create the identifier for this function. It will be of the form
3348 SSDF_IDENTIFIER_<number>. */
3349 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3351 type = build_function_type_list (void_type_node,
3352 integer_type_node, integer_type_node,
3353 NULL_TREE);
3355 /* Create the FUNCTION_DECL itself. */
3356 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3357 get_identifier (id),
3358 type);
3359 TREE_PUBLIC (ssdf_decl) = 0;
3360 DECL_ARTIFICIAL (ssdf_decl) = 1;
3362 /* Put this function in the list of functions to be called from the
3363 static constructors and destructors. */
3364 if (!ssdf_decls)
3366 vec_alloc (ssdf_decls, 32);
3368 /* Take this opportunity to initialize the map from priority
3369 numbers to information about that priority level. */
3370 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3371 /*delete_key_fn=*/0,
3372 /*delete_value_fn=*/
3373 (splay_tree_delete_value_fn) &free);
3375 /* We always need to generate functions for the
3376 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3377 priorities later, we'll be sure to find the
3378 DEFAULT_INIT_PRIORITY. */
3379 get_priority_info (DEFAULT_INIT_PRIORITY);
3382 vec_safe_push (ssdf_decls, ssdf_decl);
3384 /* Create the argument list. */
3385 initialize_p_decl = cp_build_parm_decl
3386 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3387 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3388 TREE_USED (initialize_p_decl) = 1;
3389 priority_decl = cp_build_parm_decl
3390 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3391 DECL_CONTEXT (priority_decl) = ssdf_decl;
3392 TREE_USED (priority_decl) = 1;
3394 DECL_CHAIN (initialize_p_decl) = priority_decl;
3395 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3397 /* Put the function in the global scope. */
3398 pushdecl (ssdf_decl);
3400 /* Start the function itself. This is equivalent to declaring the
3401 function as:
3403 static void __ssdf (int __initialize_p, init __priority_p);
3405 It is static because we only need to call this function from the
3406 various constructor and destructor functions for this module. */
3407 start_preparsed_function (ssdf_decl,
3408 /*attrs=*/NULL_TREE,
3409 SF_PRE_PARSED);
3411 /* Set up the scope of the outermost block in the function. */
3412 body = begin_compound_stmt (BCS_FN_BODY);
3414 return body;
3417 /* Finish the generation of the function which performs initialization
3418 and destruction of objects with static storage duration. After
3419 this point, no more such objects can be created. */
3421 static void
3422 finish_static_storage_duration_function (tree body)
3424 /* Close out the function. */
3425 finish_compound_stmt (body);
3426 expand_or_defer_fn (finish_function (0));
3429 /* Return the information about the indicated PRIORITY level. If no
3430 code to handle this level has yet been generated, generate the
3431 appropriate prologue. */
3433 static priority_info
3434 get_priority_info (int priority)
3436 priority_info pi;
3437 splay_tree_node n;
3439 n = splay_tree_lookup (priority_info_map,
3440 (splay_tree_key) priority);
3441 if (!n)
3443 /* Create a new priority information structure, and insert it
3444 into the map. */
3445 pi = XNEW (struct priority_info_s);
3446 pi->initializations_p = 0;
3447 pi->destructions_p = 0;
3448 splay_tree_insert (priority_info_map,
3449 (splay_tree_key) priority,
3450 (splay_tree_value) pi);
3452 else
3453 pi = (priority_info) n->value;
3455 return pi;
3458 /* The effective initialization priority of a DECL. */
3460 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3461 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3462 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3464 /* Whether a DECL needs a guard to protect it against multiple
3465 initialization. */
3467 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3468 || DECL_ONE_ONLY (decl) \
3469 || DECL_WEAK (decl)))
3471 /* Called from one_static_initialization_or_destruction(),
3472 via walk_tree.
3473 Walks the initializer list of a global variable and looks for
3474 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3475 and that have their DECL_CONTEXT() == NULL.
3476 For each such temporary variable, set their DECL_CONTEXT() to
3477 the current function. This is necessary because otherwise
3478 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3479 when trying to refer to a temporary variable that does not have
3480 it's DECL_CONTECT() properly set. */
3481 static tree
3482 fix_temporary_vars_context_r (tree *node,
3483 int * /*unused*/,
3484 void * /*unused1*/)
3486 gcc_assert (current_function_decl);
3488 if (TREE_CODE (*node) == BIND_EXPR)
3490 tree var;
3492 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3493 if (VAR_P (var)
3494 && !DECL_NAME (var)
3495 && DECL_ARTIFICIAL (var)
3496 && !DECL_CONTEXT (var))
3497 DECL_CONTEXT (var) = current_function_decl;
3500 return NULL_TREE;
3503 /* Set up to handle the initialization or destruction of DECL. If
3504 INITP is nonzero, we are initializing the variable. Otherwise, we
3505 are destroying it. */
3507 static void
3508 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3510 tree guard_if_stmt = NULL_TREE;
3511 tree guard;
3513 /* If we are supposed to destruct and there's a trivial destructor,
3514 nothing has to be done. */
3515 if (!initp
3516 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3517 return;
3519 /* Trick the compiler into thinking we are at the file and line
3520 where DECL was declared so that error-messages make sense, and so
3521 that the debugger will show somewhat sensible file and line
3522 information. */
3523 input_location = DECL_SOURCE_LOCATION (decl);
3525 /* Make sure temporary variables in the initialiser all have
3526 their DECL_CONTEXT() set to a value different from NULL_TREE.
3527 This can happen when global variables initialisers are built.
3528 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3529 the temporary variables that might have been generated in the
3530 accompagning initialisers is NULL_TREE, meaning the variables have been
3531 declared in the global namespace.
3532 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3533 of the temporaries are set to the current function decl. */
3534 cp_walk_tree_without_duplicates (&init,
3535 fix_temporary_vars_context_r,
3536 NULL);
3538 /* Because of:
3540 [class.access.spec]
3542 Access control for implicit calls to the constructors,
3543 the conversion functions, or the destructor called to
3544 create and destroy a static data member is performed as
3545 if these calls appeared in the scope of the member's
3546 class.
3548 we pretend we are in a static member function of the class of
3549 which the DECL is a member. */
3550 if (member_p (decl))
3552 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3553 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3556 /* Assume we don't need a guard. */
3557 guard = NULL_TREE;
3558 /* We need a guard if this is an object with external linkage that
3559 might be initialized in more than one place. (For example, a
3560 static data member of a template, when the data member requires
3561 construction.) */
3562 if (NEEDS_GUARD_P (decl))
3564 tree guard_cond;
3566 guard = get_guard (decl);
3568 /* When using __cxa_atexit, we just check the GUARD as we would
3569 for a local static. */
3570 if (flag_use_cxa_atexit)
3572 /* When using __cxa_atexit, we never try to destroy
3573 anything from a static destructor. */
3574 gcc_assert (initp);
3575 guard_cond = get_guard_cond (guard);
3577 /* If we don't have __cxa_atexit, then we will be running
3578 destructors from .fini sections, or their equivalents. So,
3579 we need to know how many times we've tried to initialize this
3580 object. We do initializations only if the GUARD is zero,
3581 i.e., if we are the first to initialize the variable. We do
3582 destructions only if the GUARD is one, i.e., if we are the
3583 last to destroy the variable. */
3584 else if (initp)
3585 guard_cond
3586 = cp_build_binary_op (input_location,
3587 EQ_EXPR,
3588 cp_build_unary_op (PREINCREMENT_EXPR,
3589 guard,
3590 /*noconvert=*/1,
3591 tf_warning_or_error),
3592 integer_one_node,
3593 tf_warning_or_error);
3594 else
3595 guard_cond
3596 = cp_build_binary_op (input_location,
3597 EQ_EXPR,
3598 cp_build_unary_op (PREDECREMENT_EXPR,
3599 guard,
3600 /*noconvert=*/1,
3601 tf_warning_or_error),
3602 integer_zero_node,
3603 tf_warning_or_error);
3605 guard_if_stmt = begin_if_stmt ();
3606 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3610 /* If we're using __cxa_atexit, we have not already set the GUARD,
3611 so we must do so now. */
3612 if (guard && initp && flag_use_cxa_atexit)
3613 finish_expr_stmt (set_guard (guard));
3615 /* Perform the initialization or destruction. */
3616 if (initp)
3618 if (init)
3620 finish_expr_stmt (init);
3621 if (flag_sanitize & SANITIZE_ADDRESS)
3623 varpool_node *vnode = varpool_get_node (decl);
3624 if (vnode)
3625 vnode->dynamically_initialized = 1;
3629 /* If we're using __cxa_atexit, register a function that calls the
3630 destructor for the object. */
3631 if (flag_use_cxa_atexit)
3632 finish_expr_stmt (register_dtor_fn (decl));
3634 else
3635 finish_expr_stmt (build_cleanup (decl));
3637 /* Finish the guard if-stmt, if necessary. */
3638 if (guard)
3640 finish_then_clause (guard_if_stmt);
3641 finish_if_stmt (guard_if_stmt);
3644 /* Now that we're done with DECL we don't need to pretend to be a
3645 member of its class any longer. */
3646 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3647 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3650 /* Generate code to do the initialization or destruction of the decls in VARS,
3651 a TREE_LIST of VAR_DECL with static storage duration.
3652 Whether initialization or destruction is performed is specified by INITP. */
3654 static void
3655 do_static_initialization_or_destruction (tree vars, bool initp)
3657 tree node, init_if_stmt, cond;
3659 /* Build the outer if-stmt to check for initialization or destruction. */
3660 init_if_stmt = begin_if_stmt ();
3661 cond = initp ? integer_one_node : integer_zero_node;
3662 cond = cp_build_binary_op (input_location,
3663 EQ_EXPR,
3664 initialize_p_decl,
3665 cond,
3666 tf_warning_or_error);
3667 finish_if_stmt_cond (cond, init_if_stmt);
3669 /* To make sure dynamic construction doesn't access globals from other
3670 compilation units where they might not be yet constructed, for
3671 -fsanitize=address insert __asan_before_dynamic_init call that
3672 prevents access to either all global variables that need construction
3673 in other compilation units, or at least those that haven't been
3674 initialized yet. Variables that need dynamic construction in
3675 the current compilation unit are kept accessible. */
3676 if (flag_sanitize & SANITIZE_ADDRESS)
3677 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3679 node = vars;
3680 do {
3681 tree decl = TREE_VALUE (node);
3682 tree priority_if_stmt;
3683 int priority;
3684 priority_info pi;
3686 /* If we don't need a destructor, there's nothing to do. Avoid
3687 creating a possibly empty if-stmt. */
3688 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3690 node = TREE_CHAIN (node);
3691 continue;
3694 /* Remember that we had an initialization or finalization at this
3695 priority. */
3696 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3697 pi = get_priority_info (priority);
3698 if (initp)
3699 pi->initializations_p = 1;
3700 else
3701 pi->destructions_p = 1;
3703 /* Conditionalize this initialization on being in the right priority
3704 and being initializing/finalizing appropriately. */
3705 priority_if_stmt = begin_if_stmt ();
3706 cond = cp_build_binary_op (input_location,
3707 EQ_EXPR,
3708 priority_decl,
3709 build_int_cst (NULL_TREE, priority),
3710 tf_warning_or_error);
3711 finish_if_stmt_cond (cond, priority_if_stmt);
3713 /* Process initializers with same priority. */
3714 for (; node
3715 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3716 node = TREE_CHAIN (node))
3717 /* Do one initialization or destruction. */
3718 one_static_initialization_or_destruction (TREE_VALUE (node),
3719 TREE_PURPOSE (node), initp);
3721 /* Finish up the priority if-stmt body. */
3722 finish_then_clause (priority_if_stmt);
3723 finish_if_stmt (priority_if_stmt);
3725 } while (node);
3727 /* Revert what __asan_before_dynamic_init did by calling
3728 __asan_after_dynamic_init. */
3729 if (flag_sanitize & SANITIZE_ADDRESS)
3730 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3732 /* Finish up the init/destruct if-stmt body. */
3733 finish_then_clause (init_if_stmt);
3734 finish_if_stmt (init_if_stmt);
3737 /* VARS is a list of variables with static storage duration which may
3738 need initialization and/or finalization. Remove those variables
3739 that don't really need to be initialized or finalized, and return
3740 the resulting list. The order in which the variables appear in
3741 VARS is in reverse order of the order in which they should actually
3742 be initialized. The list we return is in the unreversed order;
3743 i.e., the first variable should be initialized first. */
3745 static tree
3746 prune_vars_needing_no_initialization (tree *vars)
3748 tree *var = vars;
3749 tree result = NULL_TREE;
3751 while (*var)
3753 tree t = *var;
3754 tree decl = TREE_VALUE (t);
3755 tree init = TREE_PURPOSE (t);
3757 /* Deal gracefully with error. */
3758 if (decl == error_mark_node)
3760 var = &TREE_CHAIN (t);
3761 continue;
3764 /* The only things that can be initialized are variables. */
3765 gcc_assert (VAR_P (decl));
3767 /* If this object is not defined, we don't need to do anything
3768 here. */
3769 if (DECL_EXTERNAL (decl))
3771 var = &TREE_CHAIN (t);
3772 continue;
3775 /* Also, if the initializer already contains errors, we can bail
3776 out now. */
3777 if (init && TREE_CODE (init) == TREE_LIST
3778 && value_member (error_mark_node, init))
3780 var = &TREE_CHAIN (t);
3781 continue;
3784 /* This variable is going to need initialization and/or
3785 finalization, so we add it to the list. */
3786 *var = TREE_CHAIN (t);
3787 TREE_CHAIN (t) = result;
3788 result = t;
3791 return result;
3794 /* Make sure we have told the back end about all the variables in
3795 VARS. */
3797 static void
3798 write_out_vars (tree vars)
3800 tree v;
3802 for (v = vars; v; v = TREE_CHAIN (v))
3804 tree var = TREE_VALUE (v);
3805 if (!var_finalized_p (var))
3807 import_export_decl (var);
3808 rest_of_decl_compilation (var, 1, 1);
3813 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3814 (otherwise) that will initialize all global objects with static
3815 storage duration having the indicated PRIORITY. */
3817 static void
3818 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3819 location_t *locus)
3821 char function_key;
3822 tree fndecl;
3823 tree body;
3824 size_t i;
3826 input_location = *locus;
3827 /* ??? */
3828 /* Was: locus->line++; */
3830 /* We use `I' to indicate initialization and `D' to indicate
3831 destruction. */
3832 function_key = constructor_p ? 'I' : 'D';
3834 /* We emit the function lazily, to avoid generating empty
3835 global constructors and destructors. */
3836 body = NULL_TREE;
3838 /* For Objective-C++, we may need to initialize metadata found in this module.
3839 This must be done _before_ any other static initializations. */
3840 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3841 && constructor_p && objc_static_init_needed_p ())
3843 body = start_objects (function_key, priority);
3844 objc_generate_static_init_call (NULL_TREE);
3847 /* Call the static storage duration function with appropriate
3848 arguments. */
3849 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3851 /* Calls to pure or const functions will expand to nothing. */
3852 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3854 tree call;
3856 if (! body)
3857 body = start_objects (function_key, priority);
3859 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3860 build_int_cst (NULL_TREE,
3861 constructor_p),
3862 build_int_cst (NULL_TREE,
3863 priority),
3864 NULL_TREE);
3865 finish_expr_stmt (call);
3869 /* Close out the function. */
3870 if (body)
3871 finish_objects (function_key, priority, body);
3874 /* Generate constructor and destructor functions for the priority
3875 indicated by N. */
3877 static int
3878 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3880 location_t *locus = (location_t *) data;
3881 int priority = (int) n->key;
3882 priority_info pi = (priority_info) n->value;
3884 /* Generate the functions themselves, but only if they are really
3885 needed. */
3886 if (pi->initializations_p)
3887 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3888 if (pi->destructions_p)
3889 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3891 /* Keep iterating. */
3892 return 0;
3895 /* Java requires that we be able to reference a local address for a
3896 method, and not be confused by PLT entries. If hidden aliases are
3897 supported, collect and return all the functions for which we should
3898 emit a hidden alias. */
3900 static struct pointer_set_t *
3901 collect_candidates_for_java_method_aliases (void)
3903 struct cgraph_node *node;
3904 struct pointer_set_t *candidates = NULL;
3906 #ifndef HAVE_GAS_HIDDEN
3907 return candidates;
3908 #endif
3910 FOR_EACH_FUNCTION (node)
3912 tree fndecl = node->decl;
3914 if (DECL_CLASS_SCOPE_P (fndecl)
3915 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3916 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3918 if (candidates == NULL)
3919 candidates = pointer_set_create ();
3920 pointer_set_insert (candidates, fndecl);
3924 return candidates;
3928 /* Java requires that we be able to reference a local address for a
3929 method, and not be confused by PLT entries. If hidden aliases are
3930 supported, emit one for each java function that we've emitted.
3931 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3932 by collect_candidates_for_java_method_aliases. */
3934 static void
3935 build_java_method_aliases (struct pointer_set_t *candidates)
3937 struct cgraph_node *node;
3939 #ifndef HAVE_GAS_HIDDEN
3940 return;
3941 #endif
3943 FOR_EACH_FUNCTION (node)
3945 tree fndecl = node->decl;
3947 if (TREE_ASM_WRITTEN (fndecl)
3948 && pointer_set_contains (candidates, fndecl))
3950 /* Mangle the name in a predictable way; we need to reference
3951 this from a java compiled object file. */
3952 tree oid, nid, alias;
3953 const char *oname;
3954 char *nname;
3956 oid = DECL_ASSEMBLER_NAME (fndecl);
3957 oname = IDENTIFIER_POINTER (oid);
3958 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3959 nname = ACONCAT (("_ZGA", oname+2, NULL));
3960 nid = get_identifier (nname);
3962 alias = make_alias_for (fndecl, nid);
3963 TREE_PUBLIC (alias) = 1;
3964 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3966 assemble_alias (alias, oid);
3971 /* Return C++ property of T, based on given operation OP. */
3973 static int
3974 cpp_check (tree t, cpp_operation op)
3976 switch (op)
3978 case IS_ABSTRACT:
3979 return DECL_PURE_VIRTUAL_P (t);
3980 case IS_CONSTRUCTOR:
3981 return DECL_CONSTRUCTOR_P (t);
3982 case IS_DESTRUCTOR:
3983 return DECL_DESTRUCTOR_P (t);
3984 case IS_COPY_CONSTRUCTOR:
3985 return DECL_COPY_CONSTRUCTOR_P (t);
3986 case IS_TEMPLATE:
3987 return TREE_CODE (t) == TEMPLATE_DECL;
3988 case IS_TRIVIAL:
3989 return trivial_type_p (t);
3990 default:
3991 return 0;
3995 /* Collect source file references recursively, starting from NAMESPC. */
3997 static void
3998 collect_source_refs (tree namespc)
4000 tree t;
4002 if (!namespc)
4003 return;
4005 /* Iterate over names in this name space. */
4006 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4007 if (!DECL_IS_BUILTIN (t) )
4008 collect_source_ref (DECL_SOURCE_FILE (t));
4010 /* Dump siblings, if any */
4011 collect_source_refs (TREE_CHAIN (namespc));
4013 /* Dump children, if any */
4014 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4017 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4018 starting from NAMESPC. */
4020 static void
4021 collect_ada_namespace (tree namespc, const char *source_file)
4023 if (!namespc)
4024 return;
4026 /* Collect decls from this namespace */
4027 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4029 /* Collect siblings, if any */
4030 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4032 /* Collect children, if any */
4033 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4036 /* Returns true iff there is a definition available for variable or
4037 function DECL. */
4039 static bool
4040 decl_defined_p (tree decl)
4042 if (TREE_CODE (decl) == FUNCTION_DECL)
4043 return (DECL_INITIAL (decl) != NULL_TREE);
4044 else
4046 gcc_assert (VAR_P (decl));
4047 return !DECL_EXTERNAL (decl);
4051 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4053 [expr.const]
4055 An integral constant-expression can only involve ... const
4056 variables of integral or enumeration types initialized with
4057 constant expressions ...
4059 C++0x also allows constexpr variables and temporaries initialized
4060 with constant expressions. We handle the former here, but the latter
4061 are just folded away in cxx_eval_constant_expression.
4063 The standard does not require that the expression be non-volatile.
4064 G++ implements the proposed correction in DR 457. */
4066 bool
4067 decl_constant_var_p (tree decl)
4069 if (!decl_maybe_constant_var_p (decl))
4070 return false;
4072 /* We don't know if a template static data member is initialized with
4073 a constant expression until we instantiate its initializer. Even
4074 in the case of a constexpr variable, we can't treat it as a
4075 constant until its initializer is complete in case it's used in
4076 its own initializer. */
4077 mark_used (decl);
4078 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4081 /* Returns true if DECL could be a symbolic constant variable, depending on
4082 its initializer. */
4084 bool
4085 decl_maybe_constant_var_p (tree decl)
4087 tree type = TREE_TYPE (decl);
4088 if (!VAR_P (decl))
4089 return false;
4090 if (DECL_DECLARED_CONSTEXPR_P (decl))
4091 return true;
4092 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4093 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4096 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4097 called from grokfndecl and grokvardecl; in all modes it is called from
4098 cp_write_global_declarations. */
4100 void
4101 no_linkage_error (tree decl)
4103 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4104 /* In C++11 it's ok if the decl is defined. */
4105 return;
4106 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4107 if (t == NULL_TREE)
4108 /* The type that got us on no_linkage_decls must have gotten a name for
4109 linkage purposes. */;
4110 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4111 /* The type might end up having a typedef name for linkage purposes. */
4112 vec_safe_push (no_linkage_decls, decl);
4113 else if (TYPE_ANONYMOUS_P (t))
4115 bool d = false;
4116 if (cxx_dialect >= cxx11)
4117 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4118 "anonymous type, is used but never defined", decl);
4119 else if (DECL_EXTERN_C_P (decl))
4120 /* Allow this; it's pretty common in C. */;
4121 else if (TREE_CODE (decl) == VAR_DECL)
4122 /* DRs 132, 319 and 389 seem to indicate types with
4123 no linkage can only be used to declare extern "C"
4124 entities. Since it's not always an error in the
4125 ISO C++ 90 Standard, we only issue a warning. */
4126 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4127 "with no linkage used to declare variable %q#D with "
4128 "linkage", decl);
4129 else
4130 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4131 "linkage used to declare function %q#D with linkage",
4132 decl);
4133 if (d && is_typedef_decl (TYPE_NAME (t)))
4134 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4135 "to the unqualified type, so it is not used for linkage",
4136 TYPE_NAME (t));
4138 else if (cxx_dialect >= cxx11)
4139 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4140 "%qT, is used but never defined", decl, t);
4141 else if (TREE_CODE (decl) == VAR_DECL)
4142 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4143 "used to declare variable %q#D with linkage", t, decl);
4144 else
4145 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4146 "to declare function %q#D with linkage", t, decl);
4149 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4151 static void
4152 collect_all_refs (const char *source_file)
4154 collect_ada_namespace (global_namespace, source_file);
4157 /* Clear DECL_EXTERNAL for NODE. */
4159 static bool
4160 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4162 DECL_EXTERNAL (node->decl) = 0;
4163 return false;
4166 /* Build up the function to run dynamic initializers for thread_local
4167 variables in this translation unit and alias the init functions for the
4168 individual variables to it. */
4170 static void
4171 handle_tls_init (void)
4173 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4174 if (vars == NULL_TREE)
4175 return;
4177 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4179 write_out_vars (vars);
4181 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4182 boolean_type_node);
4183 TREE_PUBLIC (guard) = false;
4184 TREE_STATIC (guard) = true;
4185 DECL_ARTIFICIAL (guard) = true;
4186 DECL_IGNORED_P (guard) = true;
4187 TREE_USED (guard) = true;
4188 DECL_TLS_MODEL (guard) = decl_default_tls_model (guard);
4189 pushdecl_top_level_and_finish (guard, NULL_TREE);
4191 tree fn = get_local_tls_init_fn ();
4192 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4193 tree body = begin_function_body ();
4194 tree if_stmt = begin_if_stmt ();
4195 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4196 tf_warning_or_error);
4197 finish_if_stmt_cond (cond, if_stmt);
4198 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4199 tf_warning_or_error));
4200 for (; vars; vars = TREE_CHAIN (vars))
4202 tree var = TREE_VALUE (vars);
4203 tree init = TREE_PURPOSE (vars);
4204 one_static_initialization_or_destruction (var, init, true);
4206 #ifdef ASM_OUTPUT_DEF
4207 /* Output init aliases even with -fno-extern-tls-init. */
4208 if (TREE_PUBLIC (var))
4210 tree single_init_fn = get_tls_init_fn (var);
4211 if (single_init_fn == NULL_TREE)
4212 continue;
4213 cgraph_node *alias
4214 = cgraph_same_body_alias (cgraph_get_create_node (fn),
4215 single_init_fn, fn);
4216 gcc_assert (alias != NULL);
4218 #endif
4221 finish_then_clause (if_stmt);
4222 finish_if_stmt (if_stmt);
4223 finish_function_body (body);
4224 expand_or_defer_fn (finish_function (0));
4227 /* The entire file is now complete. If requested, dump everything
4228 to a file. */
4230 static void
4231 dump_tu (void)
4233 int flags;
4234 FILE *stream = dump_begin (TDI_tu, &flags);
4236 if (stream)
4238 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4239 dump_end (TDI_tu, stream);
4243 /* This routine is called at the end of compilation.
4244 Its job is to create all the code needed to initialize and
4245 destroy the global aggregates. We do the destruction
4246 first, since that way we only need to reverse the decls once. */
4248 void
4249 cp_write_global_declarations (void)
4251 tree vars;
4252 bool reconsider;
4253 size_t i;
4254 location_t locus;
4255 unsigned ssdf_count = 0;
4256 int retries = 0;
4257 tree decl;
4258 struct pointer_set_t *candidates;
4260 locus = input_location;
4261 at_eof = 1;
4263 /* Bad parse errors. Just forget about it. */
4264 if (! global_bindings_p () || current_class_type
4265 || !vec_safe_is_empty (decl_namespace_list))
4266 return;
4268 /* This is the point to write out a PCH if we're doing that.
4269 In that case we do not want to do anything else. */
4270 if (pch_file)
4272 c_common_write_pch ();
4273 dump_tu ();
4274 return;
4277 cgraph_process_same_body_aliases ();
4279 /* Handle -fdump-ada-spec[-slim] */
4280 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4282 if (flag_dump_ada_spec_slim)
4283 collect_source_ref (main_input_filename);
4284 else
4285 collect_source_refs (global_namespace);
4287 dump_ada_specs (collect_all_refs, cpp_check);
4290 /* FIXME - huh? was input_line -= 1;*/
4292 timevar_start (TV_PHASE_DEFERRED);
4294 /* We now have to write out all the stuff we put off writing out.
4295 These include:
4297 o Template specializations that we have not yet instantiated,
4298 but which are needed.
4299 o Initialization and destruction for non-local objects with
4300 static storage duration. (Local objects with static storage
4301 duration are initialized when their scope is first entered,
4302 and are cleaned up via atexit.)
4303 o Virtual function tables.
4305 All of these may cause others to be needed. For example,
4306 instantiating one function may cause another to be needed, and
4307 generating the initializer for an object may cause templates to be
4308 instantiated, etc., etc. */
4310 emit_support_tinfos ();
4314 tree t;
4315 tree decl;
4317 reconsider = false;
4319 /* If there are templates that we've put off instantiating, do
4320 them now. */
4321 instantiate_pending_templates (retries);
4322 ggc_collect ();
4324 /* Write out virtual tables as required. Note that writing out
4325 the virtual table for a template class may cause the
4326 instantiation of members of that class. If we write out
4327 vtables then we remove the class from our list so we don't
4328 have to look at it again. */
4330 while (keyed_classes != NULL_TREE
4331 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4333 reconsider = true;
4334 keyed_classes = TREE_CHAIN (keyed_classes);
4337 t = keyed_classes;
4338 if (t != NULL_TREE)
4340 tree next = TREE_CHAIN (t);
4342 while (next)
4344 if (maybe_emit_vtables (TREE_VALUE (next)))
4346 reconsider = true;
4347 TREE_CHAIN (t) = TREE_CHAIN (next);
4349 else
4350 t = next;
4352 next = TREE_CHAIN (t);
4356 /* Write out needed type info variables. We have to be careful
4357 looping through unemitted decls, because emit_tinfo_decl may
4358 cause other variables to be needed. New elements will be
4359 appended, and we remove from the vector those that actually
4360 get emitted. */
4361 for (i = unemitted_tinfo_decls->length ();
4362 unemitted_tinfo_decls->iterate (--i, &t);)
4363 if (emit_tinfo_decl (t))
4365 reconsider = true;
4366 unemitted_tinfo_decls->unordered_remove (i);
4369 /* The list of objects with static storage duration is built up
4370 in reverse order. We clear STATIC_AGGREGATES so that any new
4371 aggregates added during the initialization of these will be
4372 initialized in the correct order when we next come around the
4373 loop. */
4374 vars = prune_vars_needing_no_initialization (&static_aggregates);
4376 if (vars)
4378 /* We need to start a new initialization function each time
4379 through the loop. That's because we need to know which
4380 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4381 isn't computed until a function is finished, and written
4382 out. That's a deficiency in the back end. When this is
4383 fixed, these initialization functions could all become
4384 inline, with resulting performance improvements. */
4385 tree ssdf_body;
4387 /* Set the line and file, so that it is obviously not from
4388 the source file. */
4389 input_location = locus;
4390 ssdf_body = start_static_storage_duration_function (ssdf_count);
4392 /* Make sure the back end knows about all the variables. */
4393 write_out_vars (vars);
4395 /* First generate code to do all the initializations. */
4396 if (vars)
4397 do_static_initialization_or_destruction (vars, /*initp=*/true);
4399 /* Then, generate code to do all the destructions. Do these
4400 in reverse order so that the most recently constructed
4401 variable is the first destroyed. If we're using
4402 __cxa_atexit, then we don't need to do this; functions
4403 were registered at initialization time to destroy the
4404 local statics. */
4405 if (!flag_use_cxa_atexit && vars)
4407 vars = nreverse (vars);
4408 do_static_initialization_or_destruction (vars, /*initp=*/false);
4410 else
4411 vars = NULL_TREE;
4413 /* Finish up the static storage duration function for this
4414 round. */
4415 input_location = locus;
4416 finish_static_storage_duration_function (ssdf_body);
4418 /* All those initializations and finalizations might cause
4419 us to need more inline functions, more template
4420 instantiations, etc. */
4421 reconsider = true;
4422 ssdf_count++;
4423 /* ??? was: locus.line++; */
4426 /* Now do the same for thread_local variables. */
4427 handle_tls_init ();
4429 /* Go through the set of inline functions whose bodies have not
4430 been emitted yet. If out-of-line copies of these functions
4431 are required, emit them. */
4432 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4434 /* Does it need synthesizing? */
4435 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4436 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4438 /* Even though we're already at the top-level, we push
4439 there again. That way, when we pop back a few lines
4440 hence, all of our state is restored. Otherwise,
4441 finish_function doesn't clean things up, and we end
4442 up with CURRENT_FUNCTION_DECL set. */
4443 push_to_top_level ();
4444 /* The decl's location will mark where it was first
4445 needed. Save that so synthesize method can indicate
4446 where it was needed from, in case of error */
4447 input_location = DECL_SOURCE_LOCATION (decl);
4448 synthesize_method (decl);
4449 pop_from_top_level ();
4450 reconsider = true;
4453 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4454 generate_tls_wrapper (decl);
4456 if (!DECL_SAVED_TREE (decl))
4457 continue;
4459 /* We lie to the back end, pretending that some functions
4460 are not defined when they really are. This keeps these
4461 functions from being put out unnecessarily. But, we must
4462 stop lying when the functions are referenced, or if they
4463 are not comdat since they need to be put out now. If
4464 DECL_INTERFACE_KNOWN, then we have already set
4465 DECL_EXTERNAL appropriately, so there's no need to check
4466 again, and we do not want to clear DECL_EXTERNAL if a
4467 previous call to import_export_decl set it.
4469 This is done in a separate for cycle, because if some
4470 deferred function is contained in another deferred
4471 function later in deferred_fns varray,
4472 rest_of_compilation would skip this function and we
4473 really cannot expand the same function twice. */
4474 import_export_decl (decl);
4475 if (DECL_NOT_REALLY_EXTERN (decl)
4476 && DECL_INITIAL (decl)
4477 && decl_needed_p (decl))
4479 struct cgraph_node *node, *next;
4481 node = cgraph_get_node (decl);
4482 if (node->cpp_implicit_alias)
4483 node = cgraph_alias_target (node);
4485 cgraph_for_node_and_aliases (node, clear_decl_external,
4486 NULL, true);
4487 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4488 group, we need to mark all symbols in the same comdat group
4489 that way. */
4490 if (node->same_comdat_group)
4491 for (next = cgraph (node->same_comdat_group);
4492 next != node;
4493 next = cgraph (next->same_comdat_group))
4494 cgraph_for_node_and_aliases (next, clear_decl_external,
4495 NULL, true);
4498 /* If we're going to need to write this function out, and
4499 there's already a body for it, create RTL for it now.
4500 (There might be no body if this is a method we haven't
4501 gotten around to synthesizing yet.) */
4502 if (!DECL_EXTERNAL (decl)
4503 && decl_needed_p (decl)
4504 && !TREE_ASM_WRITTEN (decl)
4505 && !cgraph_get_node (decl)->definition)
4507 /* We will output the function; no longer consider it in this
4508 loop. */
4509 DECL_DEFER_OUTPUT (decl) = 0;
4510 /* Generate RTL for this function now that we know we
4511 need it. */
4512 expand_or_defer_fn (decl);
4513 /* If we're compiling -fsyntax-only pretend that this
4514 function has been written out so that we don't try to
4515 expand it again. */
4516 if (flag_syntax_only)
4517 TREE_ASM_WRITTEN (decl) = 1;
4518 reconsider = true;
4522 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4523 reconsider = true;
4525 /* Static data members are just like namespace-scope globals. */
4526 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4528 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4529 /* Don't write it out if we haven't seen a definition. */
4530 || DECL_IN_AGGR_P (decl))
4531 continue;
4532 import_export_decl (decl);
4533 /* If this static data member is needed, provide it to the
4534 back end. */
4535 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4536 DECL_EXTERNAL (decl) = 0;
4538 if (vec_safe_length (pending_statics) != 0
4539 && wrapup_global_declarations (pending_statics->address (),
4540 pending_statics->length ()))
4541 reconsider = true;
4543 retries++;
4545 while (reconsider);
4547 /* All used inline functions must have a definition at this point. */
4548 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4550 if (/* Check online inline functions that were actually used. */
4551 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4552 /* If the definition actually was available here, then the
4553 fact that the function was not defined merely represents
4554 that for some reason (use of a template repository,
4555 #pragma interface, etc.) we decided not to emit the
4556 definition here. */
4557 && !DECL_INITIAL (decl)
4558 /* Don't complain if the template was defined. */
4559 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4560 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4561 (template_for_substitution (decl)))))
4563 warning (0, "inline function %q+D used but never defined", decl);
4564 /* Avoid a duplicate warning from check_global_declaration_1. */
4565 TREE_NO_WARNING (decl) = 1;
4569 /* So must decls that use a type with no linkage. */
4570 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4571 no_linkage_error (decl);
4573 /* Then, do the Objective-C stuff. This is where all the
4574 Objective-C module stuff gets generated (symtab,
4575 class/protocol/selector lists etc). This must be done after C++
4576 templates, destructors etc. so that selectors used in C++
4577 templates are properly allocated. */
4578 if (c_dialect_objc ())
4579 objc_write_global_declarations ();
4581 /* We give C linkage to static constructors and destructors. */
4582 push_lang_context (lang_name_c);
4584 /* Generate initialization and destruction functions for all
4585 priorities for which they are required. */
4586 if (priority_info_map)
4587 splay_tree_foreach (priority_info_map,
4588 generate_ctor_and_dtor_functions_for_priority,
4589 /*data=*/&locus);
4590 else if (c_dialect_objc () && objc_static_init_needed_p ())
4591 /* If this is obj-c++ and we need a static init, call
4592 generate_ctor_or_dtor_function. */
4593 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4594 DEFAULT_INIT_PRIORITY, &locus);
4596 /* We're done with the splay-tree now. */
4597 if (priority_info_map)
4598 splay_tree_delete (priority_info_map);
4600 /* Generate any missing aliases. */
4601 maybe_apply_pending_pragma_weaks ();
4603 /* We're done with static constructors, so we can go back to "C++"
4604 linkage now. */
4605 pop_lang_context ();
4607 /* Collect candidates for Java hidden aliases. */
4608 candidates = collect_candidates_for_java_method_aliases ();
4610 timevar_stop (TV_PHASE_DEFERRED);
4611 timevar_start (TV_PHASE_OPT_GEN);
4613 if (flag_vtable_verify)
4615 vtv_recover_class_info ();
4616 vtv_compute_class_hierarchy_transitive_closure ();
4617 vtv_build_vtable_verify_fndecl ();
4620 finalize_compilation_unit ();
4622 if (flag_vtable_verify)
4624 /* Generate the special constructor initialization function that
4625 calls __VLTRegisterPairs, and give it a very high
4626 initialization priority. This must be done after
4627 finalize_compilation_unit so that we have accurate
4628 information about which vtable will actually be emitted. */
4629 vtv_generate_init_routine ();
4632 timevar_stop (TV_PHASE_OPT_GEN);
4633 timevar_start (TV_PHASE_CHECK_DBGINFO);
4635 /* Now, issue warnings about static, but not defined, functions,
4636 etc., and emit debugging information. */
4637 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4638 if (vec_safe_length (pending_statics) != 0)
4640 check_global_declarations (pending_statics->address (),
4641 pending_statics->length ());
4642 emit_debug_global_declarations (pending_statics->address (),
4643 pending_statics->length ());
4646 perform_deferred_noexcept_checks ();
4648 /* Generate hidden aliases for Java. */
4649 if (candidates)
4651 build_java_method_aliases (candidates);
4652 pointer_set_destroy (candidates);
4655 finish_repo ();
4657 /* The entire file is now complete. If requested, dump everything
4658 to a file. */
4659 dump_tu ();
4661 if (flag_detailed_statistics)
4663 dump_tree_statistics ();
4664 dump_time_statistics ();
4666 input_location = locus;
4668 #ifdef ENABLE_CHECKING
4669 validate_conversion_obstack ();
4670 #endif /* ENABLE_CHECKING */
4672 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4675 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4676 function to call in parse-tree form; it has not yet been
4677 semantically analyzed. ARGS are the arguments to the function.
4678 They have already been semantically analyzed. This may change
4679 ARGS. */
4681 tree
4682 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4683 tsubst_flags_t complain)
4685 tree orig_fn;
4686 vec<tree, va_gc> *orig_args = NULL;
4687 tree expr;
4688 tree object;
4690 orig_fn = fn;
4691 object = TREE_OPERAND (fn, 0);
4693 if (processing_template_decl)
4695 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4696 || TREE_CODE (fn) == MEMBER_REF);
4697 if (type_dependent_expression_p (fn)
4698 || any_type_dependent_arguments_p (*args))
4699 return build_nt_call_vec (fn, *args);
4701 orig_args = make_tree_vector_copy (*args);
4703 /* Transform the arguments and add the implicit "this"
4704 parameter. That must be done before the FN is transformed
4705 because we depend on the form of FN. */
4706 make_args_non_dependent (*args);
4707 object = build_non_dependent_expr (object);
4708 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4710 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4711 object = cp_build_addr_expr (object, complain);
4712 vec_safe_insert (*args, 0, object);
4714 /* Now that the arguments are done, transform FN. */
4715 fn = build_non_dependent_expr (fn);
4718 /* A qualified name corresponding to a bound pointer-to-member is
4719 represented as an OFFSET_REF:
4721 struct B { void g(); };
4722 void (B::*p)();
4723 void B::g() { (this->*p)(); } */
4724 if (TREE_CODE (fn) == OFFSET_REF)
4726 tree object_addr = cp_build_addr_expr (object, complain);
4727 fn = TREE_OPERAND (fn, 1);
4728 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4729 complain);
4730 vec_safe_insert (*args, 0, object_addr);
4733 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4734 expr = build_op_call (fn, args, complain);
4735 else
4736 expr = cp_build_function_call_vec (fn, args, complain);
4737 if (processing_template_decl && expr != error_mark_node)
4738 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4740 if (orig_args != NULL)
4741 release_tree_vector (orig_args);
4743 return expr;
4747 void
4748 check_default_args (tree x)
4750 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4751 bool saw_def = false;
4752 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4753 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4755 if (TREE_PURPOSE (arg))
4756 saw_def = true;
4757 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4759 error ("default argument missing for parameter %P of %q+#D", i, x);
4760 TREE_PURPOSE (arg) = error_mark_node;
4765 /* Return true if function DECL can be inlined. This is used to force
4766 instantiation of methods that might be interesting for inlining. */
4767 bool
4768 possibly_inlined_p (tree decl)
4770 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4771 if (DECL_UNINLINABLE (decl))
4772 return false;
4773 if (!optimize || pragma_java_exceptions)
4774 return DECL_DECLARED_INLINE_P (decl);
4775 /* When optimizing, we might inline everything when flatten
4776 attribute or heuristics inlining for size or autoinlining
4777 is used. */
4778 return true;
4781 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4782 If DECL is a specialization or implicitly declared class member,
4783 generate the actual definition. Return false if something goes
4784 wrong, true otherwise. */
4786 bool
4787 mark_used (tree decl, tsubst_flags_t complain)
4789 /* If DECL is a BASELINK for a single function, then treat it just
4790 like the DECL for the function. Otherwise, if the BASELINK is
4791 for an overloaded function, we don't know which function was
4792 actually used until after overload resolution. */
4793 if (BASELINK_P (decl))
4795 decl = BASELINK_FUNCTIONS (decl);
4796 if (really_overloaded_fn (decl))
4797 return true;
4798 decl = OVL_CURRENT (decl);
4801 /* Set TREE_USED for the benefit of -Wunused. */
4802 TREE_USED (decl) = 1;
4803 if (DECL_CLONED_FUNCTION_P (decl))
4804 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4806 /* Mark enumeration types as used. */
4807 if (TREE_CODE (decl) == CONST_DECL)
4808 used_types_insert (DECL_CONTEXT (decl));
4810 if (TREE_CODE (decl) == FUNCTION_DECL
4811 && DECL_DELETED_FN (decl))
4813 if (DECL_ARTIFICIAL (decl))
4815 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4816 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4818 /* We mark a lambda conversion op as deleted if we can't
4819 generate it properly; see maybe_add_lambda_conv_op. */
4820 sorry ("converting lambda which uses %<...%> to "
4821 "function pointer");
4822 return false;
4825 if (complain & tf_error)
4827 error ("use of deleted function %qD", decl);
4828 if (!maybe_explain_implicit_delete (decl))
4829 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4831 return false;
4834 /* We can only check DECL_ODR_USED on variables or functions with
4835 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4836 might need special handling for. */
4837 if (!VAR_OR_FUNCTION_DECL_P (decl)
4838 || DECL_LANG_SPECIFIC (decl) == NULL
4839 || DECL_THUNK_P (decl))
4841 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4843 if (complain & tf_error)
4844 error ("use of %qD before deduction of %<auto%>", decl);
4845 return false;
4847 return true;
4850 /* We only want to do this processing once. We don't need to keep trying
4851 to instantiate inline templates, because unit-at-a-time will make sure
4852 we get them compiled before functions that want to inline them. */
4853 if (DECL_ODR_USED (decl))
4854 return true;
4856 /* If within finish_function, defer the rest until that function
4857 finishes, otherwise it might recurse. */
4858 if (defer_mark_used_calls)
4860 vec_safe_push (deferred_mark_used_calls, decl);
4861 return true;
4864 if (TREE_CODE (decl) == FUNCTION_DECL)
4865 maybe_instantiate_noexcept (decl);
4867 /* Normally, we can wait until instantiation-time to synthesize DECL.
4868 However, if DECL is a static data member initialized with a constant
4869 or a constexpr function, we need it right now because a reference to
4870 such a data member or a call to such function is not value-dependent.
4871 For a function that uses auto in the return type, we need to instantiate
4872 it to find out its type. For OpenMP user defined reductions, we need
4873 them instantiated for reduction clauses which inline them by hand
4874 directly. */
4875 if (DECL_LANG_SPECIFIC (decl)
4876 && DECL_TEMPLATE_INFO (decl)
4877 && (decl_maybe_constant_var_p (decl)
4878 || (TREE_CODE (decl) == FUNCTION_DECL
4879 && (DECL_DECLARED_CONSTEXPR_P (decl)
4880 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4881 || undeduced_auto_decl (decl))
4882 && !uses_template_parms (DECL_TI_ARGS (decl)))
4884 /* Instantiating a function will result in garbage collection. We
4885 must treat this situation as if we were within the body of a
4886 function so as to avoid collecting live data only referenced from
4887 the stack (such as overload resolution candidates). */
4888 ++function_depth;
4889 instantiate_decl (decl, /*defer_ok=*/false,
4890 /*expl_inst_class_mem_p=*/false);
4891 --function_depth;
4894 if (processing_template_decl)
4895 return true;
4897 /* Check this too in case we're within fold_non_dependent_expr. */
4898 if (DECL_TEMPLATE_INFO (decl)
4899 && uses_template_parms (DECL_TI_ARGS (decl)))
4900 return true;
4902 require_deduced_type (decl);
4904 /* If we don't need a value, then we don't need to synthesize DECL. */
4905 if (cp_unevaluated_operand != 0)
4906 return true;
4908 DECL_ODR_USED (decl) = 1;
4909 if (DECL_CLONED_FUNCTION_P (decl))
4910 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4912 /* DR 757: A type without linkage shall not be used as the type of a
4913 variable or function with linkage, unless
4914 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4915 o the variable or function is not used (3.2 [basic.def.odr]) or is
4916 defined in the same translation unit. */
4917 if (cxx_dialect > cxx98
4918 && decl_linkage (decl) != lk_none
4919 && !DECL_EXTERN_C_P (decl)
4920 && !DECL_ARTIFICIAL (decl)
4921 && !decl_defined_p (decl)
4922 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4924 if (is_local_extern (decl))
4925 /* There's no way to define a local extern, and adding it to
4926 the vector interferes with GC, so give an error now. */
4927 no_linkage_error (decl);
4928 else
4929 vec_safe_push (no_linkage_decls, decl);
4932 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4933 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4934 /* Remember it, so we can check it was defined. */
4935 note_vague_linkage_fn (decl);
4937 /* Is it a synthesized method that needs to be synthesized? */
4938 if (TREE_CODE (decl) == FUNCTION_DECL
4939 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4940 && DECL_DEFAULTED_FN (decl)
4941 /* A function defaulted outside the class is synthesized either by
4942 cp_finish_decl or instantiate_decl. */
4943 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
4944 && ! DECL_INITIAL (decl))
4946 /* Defer virtual destructors so that thunks get the right
4947 linkage. */
4948 if (DECL_VIRTUAL_P (decl) && !at_eof)
4950 note_vague_linkage_fn (decl);
4951 return true;
4954 /* Remember the current location for a function we will end up
4955 synthesizing. Then we can inform the user where it was
4956 required in the case of error. */
4957 DECL_SOURCE_LOCATION (decl) = input_location;
4959 /* Synthesizing an implicitly defined member function will result in
4960 garbage collection. We must treat this situation as if we were
4961 within the body of a function so as to avoid collecting live data
4962 on the stack (such as overload resolution candidates).
4964 We could just let cp_write_global_declarations handle synthesizing
4965 this function by adding it to deferred_fns, but doing
4966 it at the use site produces better error messages. */
4967 ++function_depth;
4968 synthesize_method (decl);
4969 --function_depth;
4970 /* If this is a synthesized method we don't need to
4971 do the instantiation test below. */
4973 else if (VAR_OR_FUNCTION_DECL_P (decl)
4974 && DECL_TEMPLATE_INFO (decl)
4975 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4976 || always_instantiate_p (decl)))
4977 /* If this is a function or variable that is an instance of some
4978 template, we now know that we will need to actually do the
4979 instantiation. We check that DECL is not an explicit
4980 instantiation because that is not checked in instantiate_decl.
4982 We put off instantiating functions in order to improve compile
4983 times. Maintaining a stack of active functions is expensive,
4984 and the inliner knows to instantiate any functions it might
4985 need. Therefore, we always try to defer instantiation. */
4987 ++function_depth;
4988 instantiate_decl (decl, /*defer_ok=*/true,
4989 /*expl_inst_class_mem_p=*/false);
4990 --function_depth;
4993 return true;
4996 bool
4997 mark_used (tree decl)
4999 return mark_used (decl, tf_warning_or_error);
5002 tree
5003 vtv_start_verification_constructor_init_function (void)
5005 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5008 tree
5009 vtv_finish_verification_constructor_init_function (tree function_body)
5011 tree fn;
5013 finish_compound_stmt (function_body);
5014 fn = finish_function (0);
5015 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5016 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5018 return fn;
5021 #include "gt-cp-decl2.h"