Merge from trunk:
[official-gcc.git] / main / gcc / cp / decl2.c
blob9bbb2f55601f297f728f3241b586708ef855728b
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;
107 extern int at_eof;
110 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
111 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
112 that apply to the function). */
114 tree
115 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
116 cp_ref_qualifier rqual)
118 tree raises;
119 tree attrs;
120 int type_quals;
121 bool late_return_type_p;
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 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
134 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
135 (TREE_CODE (fntype) == METHOD_TYPE
136 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
137 : TYPE_ARG_TYPES (fntype)));
138 if (attrs)
139 fntype = cp_build_type_attribute_variant (fntype, attrs);
140 if (rqual)
141 fntype = build_ref_qualified_type (fntype, rqual);
142 if (raises)
143 fntype = build_exception_variant (fntype, raises);
144 if (late_return_type_p)
145 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
147 return fntype;
150 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
151 return type changed to NEW_RET. */
153 tree
154 change_return_type (tree new_ret, tree fntype)
156 tree newtype;
157 tree args = TYPE_ARG_TYPES (fntype);
158 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
159 tree attrs = TYPE_ATTRIBUTES (fntype);
160 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
162 if (new_ret == error_mark_node)
163 return fntype;
165 if (same_type_p (new_ret, TREE_TYPE (fntype)))
166 return fntype;
168 if (TREE_CODE (fntype) == FUNCTION_TYPE)
170 newtype = build_function_type (new_ret, args);
171 newtype = apply_memfn_quals (newtype,
172 type_memfn_quals (fntype),
173 type_memfn_rqual (fntype));
175 else
176 newtype = build_method_type_directly
177 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
178 if (raises)
179 newtype = build_exception_variant (newtype, raises);
180 if (attrs)
181 newtype = cp_build_type_attribute_variant (newtype, attrs);
182 if (late_return_type_p)
183 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
185 return newtype;
188 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
189 appropriately. */
191 tree
192 cp_build_parm_decl (tree name, tree type)
194 tree parm = build_decl (input_location,
195 PARM_DECL, name, type);
196 /* DECL_ARG_TYPE is only used by the back end and the back end never
197 sees templates. */
198 if (!processing_template_decl)
199 DECL_ARG_TYPE (parm) = type_passed_as (type);
201 return parm;
204 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
205 indicated NAME. */
207 tree
208 build_artificial_parm (tree name, tree type)
210 tree parm = cp_build_parm_decl (name, type);
211 DECL_ARTIFICIAL (parm) = 1;
212 /* All our artificial parms are implicitly `const'; they cannot be
213 assigned to. */
214 TREE_READONLY (parm) = 1;
215 return parm;
218 /* Constructors for types with virtual baseclasses need an "in-charge" flag
219 saying whether this constructor is responsible for initialization of
220 virtual baseclasses or not. All destructors also need this "in-charge"
221 flag, which additionally determines whether or not the destructor should
222 free the memory for the object.
224 This function adds the "in-charge" flag to member function FN if
225 appropriate. It is called from grokclassfn and tsubst.
226 FN must be either a constructor or destructor.
228 The in-charge flag follows the 'this' parameter, and is followed by the
229 VTT parm (if any), then the user-written parms. */
231 void
232 maybe_retrofit_in_chrg (tree fn)
234 tree basetype, arg_types, parms, parm, fntype;
236 /* If we've already add the in-charge parameter don't do it again. */
237 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
238 return;
240 /* When processing templates we can't know, in general, whether or
241 not we're going to have virtual baseclasses. */
242 if (processing_template_decl)
243 return;
245 /* We don't need an in-charge parameter for constructors that don't
246 have virtual bases. */
247 if (DECL_CONSTRUCTOR_P (fn)
248 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
249 return;
251 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
252 basetype = TREE_TYPE (TREE_VALUE (arg_types));
253 arg_types = TREE_CHAIN (arg_types);
255 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
257 /* If this is a subobject constructor or destructor, our caller will
258 pass us a pointer to our VTT. */
259 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
261 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
263 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
264 DECL_CHAIN (parm) = parms;
265 parms = parm;
267 /* ...and then to TYPE_ARG_TYPES. */
268 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
270 DECL_HAS_VTT_PARM_P (fn) = 1;
273 /* Then add the in-charge parm (before the VTT parm). */
274 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
275 DECL_CHAIN (parm) = parms;
276 parms = parm;
277 arg_types = hash_tree_chain (integer_type_node, arg_types);
279 /* Insert our new parameter(s) into the list. */
280 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
282 /* And rebuild the function type. */
283 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
284 arg_types);
285 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
286 fntype = build_exception_variant (fntype,
287 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
288 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
289 fntype = (cp_build_type_attribute_variant
290 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
291 TREE_TYPE (fn) = fntype;
293 /* Now we've got the in-charge parameter. */
294 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
297 /* Classes overload their constituent function names automatically.
298 When a function name is declared in a record structure,
299 its name is changed to it overloaded name. Since names for
300 constructors and destructors can conflict, we place a leading
301 '$' for destructors.
303 CNAME is the name of the class we are grokking for.
305 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
307 FLAGS contains bits saying what's special about today's
308 arguments. DTOR_FLAG == DESTRUCTOR.
310 If FUNCTION is a destructor, then we must add the `auto-delete' field
311 as a second parameter. There is some hair associated with the fact
312 that we must "declare" this variable in the manner consistent with the
313 way the rest of the arguments were declared.
315 QUALS are the qualifiers for the this pointer. */
317 void
318 grokclassfn (tree ctype, tree function, enum overload_flags flags)
320 tree fn_name = DECL_NAME (function);
322 /* Even within an `extern "C"' block, members get C++ linkage. See
323 [dcl.link] for details. */
324 SET_DECL_LANGUAGE (function, lang_cplusplus);
326 if (fn_name == NULL_TREE)
328 error ("name missing for member function");
329 fn_name = get_identifier ("<anonymous>");
330 DECL_NAME (function) = fn_name;
333 DECL_CONTEXT (function) = ctype;
335 if (flags == DTOR_FLAG)
336 DECL_DESTRUCTOR_P (function) = 1;
338 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
339 maybe_retrofit_in_chrg (function);
342 /* Create an ARRAY_REF, checking for the user doing things backwards
343 along the way. DECLTYPE_P is for N3276, as in the parser. */
345 tree
346 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
347 bool decltype_p)
349 tree type;
350 tree expr;
351 tree orig_array_expr = array_expr;
352 tree orig_index_exp = index_exp;
354 if (error_operand_p (array_expr) || error_operand_p (index_exp))
355 return error_mark_node;
357 if (processing_template_decl)
359 if (type_dependent_expression_p (array_expr)
360 || type_dependent_expression_p (index_exp))
361 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
362 NULL_TREE, NULL_TREE);
363 array_expr = build_non_dependent_expr (array_expr);
364 index_exp = build_non_dependent_expr (index_exp);
367 type = TREE_TYPE (array_expr);
368 gcc_assert (type);
369 type = non_reference (type);
371 /* If they have an `operator[]', use that. */
372 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
374 tsubst_flags_t complain = tf_warning_or_error;
375 if (decltype_p)
376 complain |= tf_decltype;
377 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
378 index_exp, NULL_TREE, /*overload=*/NULL, complain);
380 else
382 tree p1, p2, i1, i2;
384 /* Otherwise, create an ARRAY_REF for a pointer or array type.
385 It is a little-known fact that, if `a' is an array and `i' is
386 an int, you can write `i[a]', which means the same thing as
387 `a[i]'. */
388 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
389 p1 = array_expr;
390 else
391 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
393 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
394 p2 = index_exp;
395 else
396 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
398 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
399 false);
400 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
401 false);
403 if ((p1 && i2) && (i1 && p2))
404 error ("ambiguous conversion for array subscript");
406 if (p1 && i2)
407 array_expr = p1, index_exp = i2;
408 else if (i1 && p2)
409 array_expr = p2, index_exp = i1;
410 else
412 error ("invalid types %<%T[%T]%> for array subscript",
413 type, TREE_TYPE (index_exp));
414 return error_mark_node;
417 if (array_expr == error_mark_node || index_exp == error_mark_node)
418 error ("ambiguous conversion for array subscript");
420 expr = build_array_ref (input_location, array_expr, index_exp);
422 if (processing_template_decl && expr != error_mark_node)
423 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
424 NULL_TREE, NULL_TREE);
425 return expr;
428 /* Given the cast expression EXP, checking out its validity. Either return
429 an error_mark_node if there was an unavoidable error, return a cast to
430 void for trying to delete a pointer w/ the value 0, or return the
431 call to delete. If DOING_VEC is true, we handle things differently
432 for doing an array delete.
433 Implements ARM $5.3.4. This is called from the parser. */
435 tree
436 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
437 tsubst_flags_t complain)
439 tree t, type;
441 if (exp == error_mark_node)
442 return exp;
444 if (processing_template_decl)
446 t = build_min (DELETE_EXPR, void_type_node, exp, size);
447 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
448 DELETE_EXPR_USE_VEC (t) = doing_vec;
449 TREE_SIDE_EFFECTS (t) = 1;
450 return t;
453 /* An array can't have been allocated by new, so complain. */
454 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
455 warning (0, "deleting array %q#E", exp);
457 t = build_expr_type_conversion (WANT_POINTER, exp, true);
459 if (t == NULL_TREE || t == error_mark_node)
461 error ("type %q#T argument given to %<delete%>, expected pointer",
462 TREE_TYPE (exp));
463 return error_mark_node;
466 type = TREE_TYPE (t);
468 /* As of Valley Forge, you can delete a pointer to const. */
470 /* You can't delete functions. */
471 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
473 error ("cannot delete a function. Only pointer-to-objects are "
474 "valid arguments to %<delete%>");
475 return error_mark_node;
478 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
479 if (VOID_TYPE_P (TREE_TYPE (type)))
481 warning (0, "deleting %qT is undefined", type);
482 doing_vec = 0;
485 /* Deleting a pointer with the value zero is valid and has no effect. */
486 if (integer_zerop (t))
487 return build1 (NOP_EXPR, void_type_node, t);
489 if (doing_vec)
490 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
491 sfk_deleting_destructor,
492 use_global_delete, complain);
493 else
494 return build_delete (type, t, sfk_deleting_destructor,
495 LOOKUP_NORMAL, use_global_delete,
496 complain);
499 /* Report an error if the indicated template declaration is not the
500 sort of thing that should be a member template. */
502 void
503 check_member_template (tree tmpl)
505 tree decl;
507 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
508 decl = DECL_TEMPLATE_RESULT (tmpl);
510 if (TREE_CODE (decl) == FUNCTION_DECL
511 || DECL_ALIAS_TEMPLATE_P (tmpl)
512 || (TREE_CODE (decl) == TYPE_DECL
513 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
515 /* The parser rejects template declarations in local classes
516 (with the exception of generic lambdas). */
517 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
518 /* The parser rejects any use of virtual in a function template. */
519 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
520 && DECL_VIRTUAL_P (decl)));
522 /* The debug-information generating code doesn't know what to do
523 with member templates. */
524 DECL_IGNORED_P (tmpl) = 1;
526 else
527 error ("template declaration of %q#D", decl);
530 /* Return true iff TYPE is a valid Java parameter or return type. */
532 static bool
533 acceptable_java_type (tree type)
535 if (type == error_mark_node)
536 return false;
538 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
539 return true;
540 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
542 type = TREE_TYPE (type);
543 if (TREE_CODE (type) == RECORD_TYPE)
545 tree args; int i;
546 if (! TYPE_FOR_JAVA (type))
547 return false;
548 if (! CLASSTYPE_TEMPLATE_INFO (type))
549 return true;
550 args = CLASSTYPE_TI_ARGS (type);
551 i = TREE_VEC_LENGTH (args);
552 while (--i >= 0)
554 type = TREE_VEC_ELT (args, i);
555 if (TYPE_PTR_P (type))
556 type = TREE_TYPE (type);
557 if (! TYPE_FOR_JAVA (type))
558 return false;
560 return true;
563 return false;
566 /* For a METHOD in a Java class CTYPE, return true if
567 the parameter and return types are valid Java types.
568 Otherwise, print appropriate error messages, and return false. */
570 bool
571 check_java_method (tree method)
573 bool jerr = false;
574 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
575 tree ret_type = TREE_TYPE (TREE_TYPE (method));
577 if (!acceptable_java_type (ret_type))
579 error ("Java method %qD has non-Java return type %qT",
580 method, ret_type);
581 jerr = true;
584 arg_types = TREE_CHAIN (arg_types);
585 if (DECL_HAS_IN_CHARGE_PARM_P (method))
586 arg_types = TREE_CHAIN (arg_types);
587 if (DECL_HAS_VTT_PARM_P (method))
588 arg_types = TREE_CHAIN (arg_types);
590 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
592 tree type = TREE_VALUE (arg_types);
593 if (!acceptable_java_type (type))
595 if (type != error_mark_node)
596 error ("Java method %qD has non-Java parameter type %qT",
597 method, type);
598 jerr = true;
601 return !jerr;
604 /* Sanity check: report error if this function FUNCTION is not
605 really a member of the class (CTYPE) it is supposed to belong to.
606 TEMPLATE_PARMS is used to specify the template parameters of a member
607 template passed as FUNCTION_DECL. If the member template is passed as a
608 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
609 from the declaration. If the function is not a function template, it
610 must be NULL.
611 It returns the original declaration for the function, NULL_TREE if
612 no declaration was found, error_mark_node if an error was emitted. */
614 tree
615 check_classfn (tree ctype, tree function, tree template_parms)
617 int ix;
618 bool is_template;
619 tree pushed_scope;
621 if (DECL_USE_TEMPLATE (function)
622 && !(TREE_CODE (function) == TEMPLATE_DECL
623 && DECL_TEMPLATE_SPECIALIZATION (function))
624 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
625 /* Since this is a specialization of a member template,
626 we're not going to find the declaration in the class.
627 For example, in:
629 struct S { template <typename T> void f(T); };
630 template <> void S::f(int);
632 we're not going to find `S::f(int)', but there's no
633 reason we should, either. We let our callers know we didn't
634 find the method, but we don't complain. */
635 return NULL_TREE;
637 /* Basic sanity check: for a template function, the template parameters
638 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
639 if (TREE_CODE (function) == TEMPLATE_DECL)
641 if (template_parms
642 && !comp_template_parms (template_parms,
643 DECL_TEMPLATE_PARMS (function)))
645 error ("template parameter lists provided don%'t match the "
646 "template parameters of %qD", function);
647 return error_mark_node;
649 template_parms = DECL_TEMPLATE_PARMS (function);
652 /* OK, is this a definition of a member template? */
653 is_template = (template_parms != NULL_TREE);
655 /* [temp.mem]
657 A destructor shall not be a member template. */
658 if (DECL_DESTRUCTOR_P (function) && is_template)
660 error ("destructor %qD declared as member template", function);
661 return error_mark_node;
664 /* We must enter the scope here, because conversion operators are
665 named by target type, and type equivalence relies on typenames
666 resolving within the scope of CTYPE. */
667 pushed_scope = push_scope (ctype);
668 ix = class_method_index_for_fn (complete_type (ctype), function);
669 if (ix >= 0)
671 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
672 tree fndecls, fndecl = 0;
673 bool is_conv_op;
674 const char *format = NULL;
676 for (fndecls = (*methods)[ix];
677 fndecls; fndecls = OVL_NEXT (fndecls))
679 tree p1, p2;
681 fndecl = OVL_CURRENT (fndecls);
682 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
683 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
685 /* We cannot simply call decls_match because this doesn't
686 work for static member functions that are pretending to
687 be methods, and because the name may have been changed by
688 asm("new_name"). */
690 /* Get rid of the this parameter on functions that become
691 static. */
692 if (DECL_STATIC_FUNCTION_P (fndecl)
693 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
694 p1 = TREE_CHAIN (p1);
696 /* A member template definition only matches a member template
697 declaration. */
698 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
699 continue;
701 /* ref-qualifier or absence of same must match. */
702 if (type_memfn_rqual (TREE_TYPE (function))
703 != type_memfn_rqual (TREE_TYPE (fndecl)))
704 continue;
706 /* While finding a match, same types and params are not enough
707 if the function is versioned. Also check version ("target")
708 attributes. */
709 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
710 TREE_TYPE (TREE_TYPE (fndecl)))
711 && compparms (p1, p2)
712 && !targetm.target_option.function_versions (function, fndecl)
713 && (!is_template
714 || comp_template_parms (template_parms,
715 DECL_TEMPLATE_PARMS (fndecl)))
716 && (DECL_TEMPLATE_SPECIALIZATION (function)
717 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
718 && (!DECL_TEMPLATE_SPECIALIZATION (function)
719 || (DECL_TI_TEMPLATE (function)
720 == DECL_TI_TEMPLATE (fndecl))))
721 break;
723 if (fndecls)
725 if (pushed_scope)
726 pop_scope (pushed_scope);
727 return OVL_CURRENT (fndecls);
730 error_at (DECL_SOURCE_LOCATION (function),
731 "prototype for %q#D does not match any in class %qT",
732 function, ctype);
733 is_conv_op = DECL_CONV_FN_P (fndecl);
735 if (is_conv_op)
736 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
737 fndecls = (*methods)[ix];
738 while (fndecls)
740 fndecl = OVL_CURRENT (fndecls);
741 fndecls = OVL_NEXT (fndecls);
743 if (!fndecls && is_conv_op)
745 if (methods->length () > (size_t) ++ix)
747 fndecls = (*methods)[ix];
748 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
750 fndecls = NULL_TREE;
751 is_conv_op = false;
754 else
755 is_conv_op = false;
757 if (format)
758 format = " %+#D";
759 else if (fndecls)
760 format = N_("candidates are: %+#D");
761 else
762 format = N_("candidate is: %+#D");
763 error (format, fndecl);
766 else if (!COMPLETE_TYPE_P (ctype))
767 cxx_incomplete_type_error (function, ctype);
768 else
769 error ("no %q#D member function declared in class %qT",
770 function, ctype);
772 if (pushed_scope)
773 pop_scope (pushed_scope);
774 return error_mark_node;
777 /* DECL is a function with vague linkage. Remember it so that at the
778 end of the translation unit we can decide whether or not to emit
779 it. */
781 void
782 note_vague_linkage_fn (tree decl)
784 DECL_DEFER_OUTPUT (decl) = 1;
785 vec_safe_push (deferred_fns, decl);
788 /* We have just processed the DECL, which is a static data member.
789 The other parameters are as for cp_finish_decl. */
791 void
792 finish_static_data_member_decl (tree decl,
793 tree init, bool init_const_expr_p,
794 tree asmspec_tree,
795 int flags)
797 DECL_CONTEXT (decl) = current_class_type;
799 /* We cannot call pushdecl here, because that would fill in the
800 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
801 the right thing, namely, to put this decl out straight away. */
803 if (! processing_template_decl)
804 vec_safe_push (pending_statics, decl);
806 if (LOCAL_CLASS_P (current_class_type)
807 /* We already complained about the template definition. */
808 && !DECL_TEMPLATE_INSTANTIATION (decl))
809 permerror (input_location, "local class %q#T shall not have static data member %q#D",
810 current_class_type, decl);
811 else
812 for (tree t = current_class_type; TYPE_P (t);
813 t = CP_TYPE_CONTEXT (t))
814 if (TYPE_ANONYMOUS_P (t))
816 if (permerror (DECL_SOURCE_LOCATION (decl),
817 "static data member %qD in unnamed class", decl))
818 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
819 "unnamed class defined here");
820 break;
823 DECL_IN_AGGR_P (decl) = 1;
825 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
826 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
827 SET_VAR_HAD_UNKNOWN_BOUND (decl);
829 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
832 /* DECLARATOR and DECLSPECS correspond to a class member. The other
833 parameters are as for cp_finish_decl. Return the DECL for the
834 class member declared. */
836 tree
837 grokfield (const cp_declarator *declarator,
838 cp_decl_specifier_seq *declspecs,
839 tree init, bool init_const_expr_p,
840 tree asmspec_tree,
841 tree attrlist)
843 tree value;
844 const char *asmspec = 0;
845 int flags;
846 tree name;
848 if (init
849 && TREE_CODE (init) == TREE_LIST
850 && TREE_VALUE (init) == error_mark_node
851 && TREE_CHAIN (init) == NULL_TREE)
852 init = NULL_TREE;
854 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
855 if (! value || value == error_mark_node)
856 /* friend or constructor went bad. */
857 return error_mark_node;
858 if (TREE_TYPE (value) == error_mark_node)
859 return value;
861 if (TREE_CODE (value) == TYPE_DECL && init)
863 error ("typedef %qD is initialized (use decltype instead)", value);
864 init = NULL_TREE;
867 /* Pass friendly classes back. */
868 if (value == void_type_node)
869 return value;
871 /* Pass friend decls back. */
872 if ((TREE_CODE (value) == FUNCTION_DECL
873 || TREE_CODE (value) == TEMPLATE_DECL)
874 && DECL_CONTEXT (value) != current_class_type)
875 return value;
877 name = DECL_NAME (value);
879 if (name != NULL_TREE)
881 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
883 error ("explicit template argument list not allowed");
884 return error_mark_node;
887 if (IDENTIFIER_POINTER (name)[0] == '_'
888 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
889 error ("member %qD conflicts with virtual function table field name",
890 value);
893 /* Stash away type declarations. */
894 if (TREE_CODE (value) == TYPE_DECL)
896 DECL_NONLOCAL (value) = 1;
897 DECL_CONTEXT (value) = current_class_type;
899 if (attrlist)
901 int attrflags = 0;
903 /* If this is a typedef that names the class for linkage purposes
904 (7.1.3p8), apply any attributes directly to the type. */
905 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
906 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
907 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
909 cplus_decl_attributes (&value, attrlist, attrflags);
912 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
913 && TREE_TYPE (value) != error_mark_node
914 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
915 set_underlying_type (value);
917 /* It's important that push_template_decl below follows
918 set_underlying_type above so that the created template
919 carries the properly set type of VALUE. */
920 if (processing_template_decl)
921 value = push_template_decl (value);
923 record_locally_defined_typedef (value);
924 return value;
927 if (DECL_IN_AGGR_P (value))
929 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
930 return void_type_node;
933 if (asmspec_tree && asmspec_tree != error_mark_node)
934 asmspec = TREE_STRING_POINTER (asmspec_tree);
936 if (init)
938 if (TREE_CODE (value) == FUNCTION_DECL)
940 /* Initializers for functions are rejected early in the parser.
941 If we get here, it must be a pure specifier for a method. */
942 if (init == ridpointers[(int)RID_DELETE])
944 DECL_DELETED_FN (value) = 1;
945 DECL_DECLARED_INLINE_P (value) = 1;
946 DECL_INITIAL (value) = error_mark_node;
948 else if (init == ridpointers[(int)RID_DEFAULT])
950 if (defaultable_fn_check (value))
952 DECL_DEFAULTED_FN (value) = 1;
953 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
954 DECL_DECLARED_INLINE_P (value) = 1;
957 else if (TREE_CODE (init) == DEFAULT_ARG)
958 error ("invalid initializer for member function %qD", value);
959 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
961 if (integer_zerop (init))
962 DECL_PURE_VIRTUAL_P (value) = 1;
963 else if (error_operand_p (init))
964 ; /* An error has already been reported. */
965 else
966 error ("invalid initializer for member function %qD",
967 value);
969 else
971 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
972 error ("initializer specified for static member function %qD",
973 value);
976 else if (TREE_CODE (value) == FIELD_DECL)
977 /* C++11 NSDMI, keep going. */;
978 else if (!VAR_P (value))
979 gcc_unreachable ();
982 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
984 value = push_template_decl (value);
985 if (error_operand_p (value))
986 return error_mark_node;
989 if (attrlist)
990 cplus_decl_attributes (&value, attrlist, 0);
992 if (init && DIRECT_LIST_INIT_P (init))
993 flags = LOOKUP_NORMAL;
994 else
995 flags = LOOKUP_IMPLICIT;
997 switch (TREE_CODE (value))
999 case VAR_DECL:
1000 finish_static_data_member_decl (value, init, init_const_expr_p,
1001 asmspec_tree, flags);
1002 return value;
1004 case FIELD_DECL:
1005 if (asmspec)
1006 error ("%<asm%> specifiers are not permitted on non-static data members");
1007 if (DECL_INITIAL (value) == error_mark_node)
1008 init = error_mark_node;
1009 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1010 NULL_TREE, flags);
1011 DECL_IN_AGGR_P (value) = 1;
1012 return value;
1014 case FUNCTION_DECL:
1015 if (asmspec)
1016 set_user_assembler_name (value, asmspec);
1018 cp_finish_decl (value,
1019 /*init=*/NULL_TREE,
1020 /*init_const_expr_p=*/false,
1021 asmspec_tree, flags);
1023 /* Pass friends back this way. */
1024 if (DECL_FRIEND_P (value))
1025 return void_type_node;
1027 DECL_IN_AGGR_P (value) = 1;
1028 return value;
1030 default:
1031 gcc_unreachable ();
1033 return NULL_TREE;
1036 /* Like `grokfield', but for bitfields.
1037 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1039 tree
1040 grokbitfield (const cp_declarator *declarator,
1041 cp_decl_specifier_seq *declspecs, tree width,
1042 tree attrlist)
1044 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1046 if (value == error_mark_node)
1047 return NULL_TREE; /* friends went bad. */
1048 if (TREE_TYPE (value) == error_mark_node)
1049 return value;
1051 /* Pass friendly classes back. */
1052 if (VOID_TYPE_P (value))
1053 return void_type_node;
1055 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1056 && (POINTER_TYPE_P (value)
1057 || !dependent_type_p (TREE_TYPE (value))))
1059 error ("bit-field %qD with non-integral type", value);
1060 return error_mark_node;
1063 if (TREE_CODE (value) == TYPE_DECL)
1065 error ("cannot declare %qD to be a bit-field type", value);
1066 return NULL_TREE;
1069 /* Usually, finish_struct_1 catches bitfields with invalid types.
1070 But, in the case of bitfields with function type, we confuse
1071 ourselves into thinking they are member functions, so we must
1072 check here. */
1073 if (TREE_CODE (value) == FUNCTION_DECL)
1075 error ("cannot declare bit-field %qD with function type",
1076 DECL_NAME (value));
1077 return NULL_TREE;
1080 if (DECL_IN_AGGR_P (value))
1082 error ("%qD is already defined in the class %qT", value,
1083 DECL_CONTEXT (value));
1084 return void_type_node;
1087 if (TREE_STATIC (value))
1089 error ("static member %qD cannot be a bit-field", value);
1090 return NULL_TREE;
1092 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1094 if (width != error_mark_node)
1096 /* The width must be an integer type. */
1097 if (!type_dependent_expression_p (width)
1098 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1099 error ("width of bit-field %qD has non-integral type %qT", value,
1100 TREE_TYPE (width));
1101 DECL_INITIAL (value) = width;
1102 SET_DECL_C_BIT_FIELD (value);
1105 DECL_IN_AGGR_P (value) = 1;
1107 if (attrlist)
1108 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1110 return value;
1114 /* Returns true iff ATTR is an attribute which needs to be applied at
1115 instantiation time rather than template definition time. */
1117 static bool
1118 is_late_template_attribute (tree attr, tree decl)
1120 tree name = get_attribute_name (attr);
1121 tree args = TREE_VALUE (attr);
1122 const struct attribute_spec *spec = lookup_attribute_spec (name);
1123 tree arg;
1125 if (!spec)
1126 /* Unknown attribute. */
1127 return false;
1129 /* Attribute weak handling wants to write out assembly right away. */
1130 if (is_attribute_p ("weak", name))
1131 return true;
1133 /* Attribute unused is applied directly, as it appertains to
1134 decls. */
1135 if (is_attribute_p ("unused", name))
1136 return false;
1138 /* #pragma omp declare simd attribute needs to be always deferred. */
1139 if (flag_openmp
1140 && is_attribute_p ("omp declare simd", name))
1141 return true;
1143 /* If any of the arguments are dependent expressions, we can't evaluate
1144 the attribute until instantiation time. */
1145 for (arg = args; arg; arg = TREE_CHAIN (arg))
1147 tree t = TREE_VALUE (arg);
1149 /* If the first attribute argument is an identifier, only consider
1150 second and following arguments. Attributes like mode, format,
1151 cleanup and several target specific attributes aren't late
1152 just because they have an IDENTIFIER_NODE as first argument. */
1153 if (arg == args && identifier_p (t))
1154 continue;
1156 if (value_dependent_expression_p (t)
1157 || type_dependent_expression_p (t))
1158 return true;
1161 if (TREE_CODE (decl) == TYPE_DECL
1162 || TYPE_P (decl)
1163 || spec->type_required)
1165 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1167 /* We can't apply any attributes to a completely unknown type until
1168 instantiation time. */
1169 enum tree_code code = TREE_CODE (type);
1170 if (code == TEMPLATE_TYPE_PARM
1171 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1172 || code == TYPENAME_TYPE)
1173 return true;
1174 /* Also defer most attributes on dependent types. This is not
1175 necessary in all cases, but is the better default. */
1176 else if (dependent_type_p (type)
1177 /* But attributes abi_tag and visibility specifically apply
1178 to templates. */
1179 && !is_attribute_p ("abi_tag", name)
1180 && !is_attribute_p ("visibility", name))
1181 return true;
1182 else
1183 return false;
1185 else
1186 return false;
1189 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1190 applied at instantiation time and return them. If IS_DEPENDENT is true,
1191 the declaration itself is dependent, so all attributes should be applied
1192 at instantiation time. */
1194 static tree
1195 splice_template_attributes (tree *attr_p, tree decl)
1197 tree *p = attr_p;
1198 tree late_attrs = NULL_TREE;
1199 tree *q = &late_attrs;
1201 if (!p)
1202 return NULL_TREE;
1204 for (; *p; )
1206 if (is_late_template_attribute (*p, decl))
1208 ATTR_IS_DEPENDENT (*p) = 1;
1209 *q = *p;
1210 *p = TREE_CHAIN (*p);
1211 q = &TREE_CHAIN (*q);
1212 *q = NULL_TREE;
1214 else
1215 p = &TREE_CHAIN (*p);
1218 return late_attrs;
1221 /* Remove any late attributes from the list in ATTR_P and attach them to
1222 DECL_P. */
1224 static void
1225 save_template_attributes (tree *attr_p, tree *decl_p)
1227 tree *q;
1229 if (attr_p && *attr_p == error_mark_node)
1230 return;
1232 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1233 if (!late_attrs)
1234 return;
1236 if (DECL_P (*decl_p))
1237 q = &DECL_ATTRIBUTES (*decl_p);
1238 else
1239 q = &TYPE_ATTRIBUTES (*decl_p);
1241 tree old_attrs = *q;
1243 /* Merge the late attributes at the beginning with the attribute
1244 list. */
1245 late_attrs = merge_attributes (late_attrs, *q);
1246 *q = late_attrs;
1248 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1250 /* We've added new attributes directly to the main variant, so
1251 now we need to update all of the other variants to include
1252 these new attributes. */
1253 tree variant;
1254 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1255 variant = TYPE_NEXT_VARIANT (variant))
1257 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1258 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1263 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1264 to a typedef which gives a previously anonymous class or enum a name for
1265 linkage purposes. */
1267 bool
1268 attributes_naming_typedef_ok (tree attrs)
1270 for (; attrs; attrs = TREE_CHAIN (attrs))
1272 tree name = get_attribute_name (attrs);
1273 if (is_attribute_p ("vector_size", name))
1274 return false;
1276 return true;
1279 /* Like reconstruct_complex_type, but handle also template trees. */
1281 tree
1282 cp_reconstruct_complex_type (tree type, tree bottom)
1284 tree inner, outer;
1285 bool late_return_type_p = false;
1287 if (TYPE_PTR_P (type))
1289 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1290 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1291 TYPE_REF_CAN_ALIAS_ALL (type));
1293 else if (TREE_CODE (type) == REFERENCE_TYPE)
1295 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1296 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1297 TYPE_REF_CAN_ALIAS_ALL (type));
1299 else if (TREE_CODE (type) == ARRAY_TYPE)
1301 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1302 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1303 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1304 element type qualification will be handled by the recursive
1305 cp_reconstruct_complex_type call and cp_build_qualified_type
1306 for ARRAY_TYPEs changes the element type. */
1307 return outer;
1309 else if (TREE_CODE (type) == FUNCTION_TYPE)
1311 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1312 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1313 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1314 outer = apply_memfn_quals (outer,
1315 type_memfn_quals (type),
1316 type_memfn_rqual (type));
1318 else if (TREE_CODE (type) == METHOD_TYPE)
1320 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1321 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1322 /* The build_method_type_directly() routine prepends 'this' to argument list,
1323 so we must compensate by getting rid of it. */
1324 outer
1325 = build_method_type_directly
1326 (class_of_this_parm (type), inner,
1327 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1329 else if (TREE_CODE (type) == OFFSET_TYPE)
1331 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1332 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1334 else
1335 return bottom;
1337 if (TYPE_ATTRIBUTES (type))
1338 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1339 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1341 if (late_return_type_p)
1342 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1344 return outer;
1347 /* Replaces any constexpr expression that may be into the attributes
1348 arguments with their reduced value. */
1350 static void
1351 cp_check_const_attributes (tree attributes)
1353 if (attributes == error_mark_node)
1354 return;
1356 tree attr;
1357 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1359 tree arg;
1360 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1362 tree expr = TREE_VALUE (arg);
1363 if (EXPR_P (expr))
1364 TREE_VALUE (arg) = maybe_constant_value (expr);
1369 /* Return true if TYPE is an OpenMP mappable type. */
1370 bool
1371 cp_omp_mappable_type (tree type)
1373 /* Mappable type has to be complete. */
1374 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1375 return false;
1376 /* Arrays have mappable type if the elements have mappable type. */
1377 while (TREE_CODE (type) == ARRAY_TYPE)
1378 type = TREE_TYPE (type);
1379 /* A mappable type cannot contain virtual members. */
1380 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1381 return false;
1382 /* All data members must be non-static. */
1383 if (CLASS_TYPE_P (type))
1385 tree field;
1386 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1387 if (TREE_CODE (field) == VAR_DECL)
1388 return false;
1389 /* All fields must have mappable types. */
1390 else if (TREE_CODE (field) == FIELD_DECL
1391 && !cp_omp_mappable_type (TREE_TYPE (field)))
1392 return false;
1394 return true;
1397 /* Like decl_attributes, but handle C++ complexity. */
1399 void
1400 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1402 if (*decl == NULL_TREE || *decl == void_type_node
1403 || *decl == error_mark_node)
1404 return;
1406 /* Add implicit "omp declare target" attribute if requested. */
1407 if (scope_chain->omp_declare_target_attribute
1408 && ((TREE_CODE (*decl) == VAR_DECL && TREE_STATIC (*decl))
1409 || TREE_CODE (*decl) == FUNCTION_DECL))
1411 if (TREE_CODE (*decl) == VAR_DECL
1412 && DECL_CLASS_SCOPE_P (*decl))
1413 error ("%q+D static data member inside of declare target directive",
1414 *decl);
1415 else if (TREE_CODE (*decl) == VAR_DECL
1416 && (DECL_FUNCTION_SCOPE_P (*decl)
1417 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1418 error ("%q+D in block scope inside of declare target directive",
1419 *decl);
1420 else if (!processing_template_decl
1421 && TREE_CODE (*decl) == VAR_DECL
1422 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1423 error ("%q+D in declare target directive does not have mappable type",
1424 *decl);
1425 else
1426 attributes = tree_cons (get_identifier ("omp declare target"),
1427 NULL_TREE, attributes);
1430 if (processing_template_decl)
1432 if (check_for_bare_parameter_packs (attributes))
1433 return;
1435 save_template_attributes (&attributes, decl);
1438 cp_check_const_attributes (attributes);
1440 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1441 decl = &DECL_TEMPLATE_RESULT (*decl);
1443 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1445 attributes
1446 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1447 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (*decl)),
1448 attributes, flags);
1450 else
1451 decl_attributes (decl, attributes, flags);
1453 if (TREE_CODE (*decl) == TYPE_DECL)
1454 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1457 /* Walks through the namespace- or function-scope anonymous union
1458 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1459 Returns one of the fields for use in the mangled name. */
1461 static tree
1462 build_anon_union_vars (tree type, tree object)
1464 tree main_decl = NULL_TREE;
1465 tree field;
1467 /* Rather than write the code to handle the non-union case,
1468 just give an error. */
1469 if (TREE_CODE (type) != UNION_TYPE)
1471 error ("anonymous struct not inside named type");
1472 return error_mark_node;
1475 for (field = TYPE_FIELDS (type);
1476 field != NULL_TREE;
1477 field = DECL_CHAIN (field))
1479 tree decl;
1480 tree ref;
1482 if (DECL_ARTIFICIAL (field))
1483 continue;
1484 if (TREE_CODE (field) != FIELD_DECL)
1486 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1487 "have non-static data members", field);
1488 continue;
1491 if (TREE_PRIVATE (field))
1492 permerror (input_location, "private member %q+#D in anonymous union", field);
1493 else if (TREE_PROTECTED (field))
1494 permerror (input_location, "protected member %q+#D in anonymous union", field);
1496 if (processing_template_decl)
1497 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1498 DECL_NAME (field), NULL_TREE);
1499 else
1500 ref = build_class_member_access_expr (object, field, NULL_TREE,
1501 false, tf_warning_or_error);
1503 if (DECL_NAME (field))
1505 tree base;
1507 decl = build_decl (input_location,
1508 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1509 DECL_ANON_UNION_VAR_P (decl) = 1;
1510 DECL_ARTIFICIAL (decl) = 1;
1512 base = get_base_address (object);
1513 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1514 TREE_STATIC (decl) = TREE_STATIC (base);
1515 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1517 SET_DECL_VALUE_EXPR (decl, ref);
1518 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1520 decl = pushdecl (decl);
1522 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1523 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1524 else
1525 decl = 0;
1527 if (main_decl == NULL_TREE)
1528 main_decl = decl;
1531 return main_decl;
1534 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1535 anonymous union, then all members must be laid out together. PUBLIC_P
1536 is nonzero if this union is not declared static. */
1538 void
1539 finish_anon_union (tree anon_union_decl)
1541 tree type;
1542 tree main_decl;
1543 bool public_p;
1545 if (anon_union_decl == error_mark_node)
1546 return;
1548 type = TREE_TYPE (anon_union_decl);
1549 public_p = TREE_PUBLIC (anon_union_decl);
1551 /* The VAR_DECL's context is the same as the TYPE's context. */
1552 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1554 if (TYPE_FIELDS (type) == NULL_TREE)
1555 return;
1557 if (public_p)
1559 error ("namespace-scope anonymous aggregates must be static");
1560 return;
1563 main_decl = build_anon_union_vars (type, anon_union_decl);
1564 if (main_decl == error_mark_node)
1565 return;
1566 if (main_decl == NULL_TREE)
1568 warning (0, "anonymous union with no members");
1569 return;
1572 if (!processing_template_decl)
1574 /* Use main_decl to set the mangled name. */
1575 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1576 maybe_commonize_var (anon_union_decl);
1577 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1578 mangle_decl (anon_union_decl);
1579 DECL_NAME (anon_union_decl) = NULL_TREE;
1582 pushdecl (anon_union_decl);
1583 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1586 /* Auxiliary functions to make type signatures for
1587 `operator new' and `operator delete' correspond to
1588 what compiler will be expecting. */
1590 tree
1591 coerce_new_type (tree type)
1593 int e = 0;
1594 tree args = TYPE_ARG_TYPES (type);
1596 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1598 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1600 e = 1;
1601 error ("%<operator new%> must return type %qT", ptr_type_node);
1604 if (args && args != void_list_node)
1606 if (TREE_PURPOSE (args))
1608 /* [basic.stc.dynamic.allocation]
1610 The first parameter shall not have an associated default
1611 argument. */
1612 error ("the first parameter of %<operator new%> cannot "
1613 "have a default argument");
1614 /* Throw away the default argument. */
1615 TREE_PURPOSE (args) = NULL_TREE;
1618 if (!same_type_p (TREE_VALUE (args), size_type_node))
1620 e = 2;
1621 args = TREE_CHAIN (args);
1624 else
1625 e = 2;
1627 if (e == 2)
1628 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1629 "as first parameter", size_type_node);
1631 switch (e)
1633 case 2:
1634 args = tree_cons (NULL_TREE, size_type_node, args);
1635 /* Fall through. */
1636 case 1:
1637 type = build_exception_variant
1638 (build_function_type (ptr_type_node, args),
1639 TYPE_RAISES_EXCEPTIONS (type));
1640 /* Fall through. */
1641 default:;
1643 return type;
1646 tree
1647 coerce_delete_type (tree type)
1649 int e = 0;
1650 tree args = TYPE_ARG_TYPES (type);
1652 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1654 if (!same_type_p (TREE_TYPE (type), void_type_node))
1656 e = 1;
1657 error ("%<operator delete%> must return type %qT", void_type_node);
1660 if (!args || args == void_list_node
1661 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1663 e = 2;
1664 if (args && args != void_list_node)
1665 args = TREE_CHAIN (args);
1666 error ("%<operator delete%> takes type %qT as first parameter",
1667 ptr_type_node);
1669 switch (e)
1671 case 2:
1672 args = tree_cons (NULL_TREE, ptr_type_node, args);
1673 /* Fall through. */
1674 case 1:
1675 type = build_exception_variant
1676 (build_function_type (void_type_node, args),
1677 TYPE_RAISES_EXCEPTIONS (type));
1678 /* Fall through. */
1679 default:;
1682 return type;
1685 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1686 and mark them as needed. */
1688 static void
1689 mark_vtable_entries (tree decl)
1691 tree fnaddr;
1692 unsigned HOST_WIDE_INT idx;
1694 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1695 idx, fnaddr)
1697 tree fn;
1699 STRIP_NOPS (fnaddr);
1701 if (TREE_CODE (fnaddr) != ADDR_EXPR
1702 && TREE_CODE (fnaddr) != FDESC_EXPR)
1703 /* This entry is an offset: a virtual base class offset, a
1704 virtual call offset, an RTTI offset, etc. */
1705 continue;
1707 fn = TREE_OPERAND (fnaddr, 0);
1708 TREE_ADDRESSABLE (fn) = 1;
1709 /* When we don't have vcall offsets, we output thunks whenever
1710 we output the vtables that contain them. With vcall offsets,
1711 we know all the thunks we'll need when we emit a virtual
1712 function, so we emit the thunks there instead. */
1713 if (DECL_THUNK_P (fn))
1714 use_thunk (fn, /*emit_p=*/0);
1715 mark_used (fn);
1719 /* Set DECL up to have the closest approximation of "initialized common"
1720 linkage available. */
1722 void
1723 comdat_linkage (tree decl)
1725 if (flag_weak)
1726 make_decl_one_only (decl, cxx_comdat_group (decl));
1727 else if (TREE_CODE (decl) == FUNCTION_DECL
1728 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1729 /* We can just emit function and compiler-generated variables
1730 statically; having multiple copies is (for the most part) only
1731 a waste of space.
1733 There are two correctness issues, however: the address of a
1734 template instantiation with external linkage should be the
1735 same, independent of what translation unit asks for the
1736 address, and this will not hold when we emit multiple copies of
1737 the function. However, there's little else we can do.
1739 Also, by default, the typeinfo implementation assumes that
1740 there will be only one copy of the string used as the name for
1741 each type. Therefore, if weak symbols are unavailable, the
1742 run-time library should perform a more conservative check; it
1743 should perform a string comparison, rather than an address
1744 comparison. */
1745 TREE_PUBLIC (decl) = 0;
1746 else
1748 /* Static data member template instantiations, however, cannot
1749 have multiple copies. */
1750 if (DECL_INITIAL (decl) == 0
1751 || DECL_INITIAL (decl) == error_mark_node)
1752 DECL_COMMON (decl) = 1;
1753 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1755 DECL_COMMON (decl) = 1;
1756 DECL_INITIAL (decl) = error_mark_node;
1758 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1760 /* We can't do anything useful; leave vars for explicit
1761 instantiation. */
1762 DECL_EXTERNAL (decl) = 1;
1763 DECL_NOT_REALLY_EXTERN (decl) = 0;
1767 DECL_COMDAT (decl) = 1;
1770 /* For win32 we also want to put explicit instantiations in
1771 linkonce sections, so that they will be merged with implicit
1772 instantiations; otherwise we get duplicate symbol errors.
1773 For Darwin we do not want explicit instantiations to be
1774 linkonce. */
1776 void
1777 maybe_make_one_only (tree decl)
1779 /* We used to say that this was not necessary on targets that support weak
1780 symbols, because the implicit instantiations will defer to the explicit
1781 one. However, that's not actually the case in SVR4; a strong definition
1782 after a weak one is an error. Also, not making explicit
1783 instantiations one_only means that we can end up with two copies of
1784 some template instantiations. */
1785 if (! flag_weak)
1786 return;
1788 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1789 we can get away with not emitting them if they aren't used. We need
1790 to for variables so that cp_finish_decl will update their linkage,
1791 because their DECL_INITIAL may not have been set properly yet. */
1793 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1794 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1795 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1797 make_decl_one_only (decl, cxx_comdat_group (decl));
1799 if (VAR_P (decl))
1801 varpool_node *node = varpool_node::get_create (decl);
1802 DECL_COMDAT (decl) = 1;
1803 /* Mark it needed so we don't forget to emit it. */
1804 node->forced_by_abi = true;
1805 TREE_USED (decl) = 1;
1810 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1811 This predicate will give the right answer during parsing of the
1812 function, which other tests may not. */
1814 bool
1815 vague_linkage_p (tree decl)
1817 /* Unfortunately, import_export_decl has not always been called
1818 before the function is processed, so we cannot simply check
1819 DECL_COMDAT. */
1820 if (DECL_COMDAT (decl)
1821 || (((TREE_CODE (decl) == FUNCTION_DECL
1822 && DECL_DECLARED_INLINE_P (decl))
1823 || (DECL_LANG_SPECIFIC (decl)
1824 && DECL_TEMPLATE_INSTANTIATION (decl)))
1825 && TREE_PUBLIC (decl)))
1826 return true;
1827 else if (DECL_FUNCTION_SCOPE_P (decl))
1828 /* A local static in an inline effectively has vague linkage. */
1829 return (TREE_STATIC (decl)
1830 && vague_linkage_p (DECL_CONTEXT (decl)));
1831 else
1832 return false;
1835 /* Determine whether or not we want to specifically import or export CTYPE,
1836 using various heuristics. */
1838 static void
1839 import_export_class (tree ctype)
1841 /* -1 for imported, 1 for exported. */
1842 int import_export = 0;
1844 /* It only makes sense to call this function at EOF. The reason is
1845 that this function looks at whether or not the first non-inline
1846 non-abstract virtual member function has been defined in this
1847 translation unit. But, we can't possibly know that until we've
1848 seen the entire translation unit. */
1849 gcc_assert (at_eof);
1851 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1852 return;
1854 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1855 we will have CLASSTYPE_INTERFACE_ONLY set but not
1856 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1857 heuristic because someone will supply a #pragma implementation
1858 elsewhere, and deducing it here would produce a conflict. */
1859 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1860 return;
1862 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1863 import_export = -1;
1864 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1865 import_export = 1;
1866 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1867 && !flag_implicit_templates)
1868 /* For a template class, without -fimplicit-templates, check the
1869 repository. If the virtual table is assigned to this
1870 translation unit, then export the class; otherwise, import
1871 it. */
1872 import_export = repo_export_class_p (ctype) ? 1 : -1;
1873 else if (TYPE_POLYMORPHIC_P (ctype))
1875 /* The ABI specifies that the virtual table and associated
1876 information are emitted with the key method, if any. */
1877 tree method = CLASSTYPE_KEY_METHOD (ctype);
1878 /* If weak symbol support is not available, then we must be
1879 careful not to emit the vtable when the key function is
1880 inline. An inline function can be defined in multiple
1881 translation units. If we were to emit the vtable in each
1882 translation unit containing a definition, we would get
1883 multiple definition errors at link-time. */
1884 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1885 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1888 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1889 a definition anywhere else. */
1890 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1891 import_export = 0;
1893 /* Allow back ends the chance to overrule the decision. */
1894 if (targetm.cxx.import_export_class)
1895 import_export = targetm.cxx.import_export_class (ctype, import_export);
1897 if (import_export)
1899 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1900 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1904 /* Return true if VAR has already been provided to the back end; in that
1905 case VAR should not be modified further by the front end. */
1906 static bool
1907 var_finalized_p (tree var)
1909 return varpool_node::get_create (var)->definition;
1912 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1913 must be emitted in this translation unit. Mark it as such. */
1915 void
1916 mark_needed (tree decl)
1918 TREE_USED (decl) = 1;
1919 if (TREE_CODE (decl) == FUNCTION_DECL)
1921 /* Extern inline functions don't become needed when referenced.
1922 If we know a method will be emitted in other TU and no new
1923 functions can be marked reachable, just use the external
1924 definition. */
1925 struct cgraph_node *node = cgraph_node::get_create (decl);
1926 node->forced_by_abi = true;
1928 /* #pragma interface and -frepo code can call mark_needed for
1929 maybe-in-charge 'tors; mark the clones as well. */
1930 tree clone;
1931 FOR_EACH_CLONE (clone, decl)
1932 mark_needed (clone);
1934 else if (TREE_CODE (decl) == VAR_DECL)
1936 varpool_node *node = varpool_node::get_create (decl);
1937 /* C++ frontend use mark_decl_references to force COMDAT variables
1938 to be output that might appear dead otherwise. */
1939 node->forced_by_abi = true;
1943 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1944 returns true if a definition of this entity should be provided in
1945 this object file. Callers use this function to determine whether
1946 or not to let the back end know that a definition of DECL is
1947 available in this translation unit. */
1949 bool
1950 decl_needed_p (tree decl)
1952 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1953 /* This function should only be called at the end of the translation
1954 unit. We cannot be sure of whether or not something will be
1955 COMDAT until that point. */
1956 gcc_assert (at_eof);
1958 /* All entities with external linkage that are not COMDAT should be
1959 emitted; they may be referred to from other object files. */
1960 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1961 return true;
1962 /* If this entity was used, let the back end see it; it will decide
1963 whether or not to emit it into the object file. */
1964 if (TREE_USED (decl))
1965 return true;
1966 /* Functions marked "dllexport" must be emitted so that they are
1967 visible to other DLLs. */
1968 if (flag_keep_inline_dllexport
1969 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1970 return true;
1971 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1972 reference to DECL might cause it to be emitted later. */
1973 return false;
1976 /* If necessary, write out the vtables for the dynamic class CTYPE.
1977 Returns true if any vtables were emitted. */
1979 static bool
1980 maybe_emit_vtables (tree ctype)
1982 tree vtbl;
1983 tree primary_vtbl;
1984 int needed = 0;
1985 varpool_node *current = NULL, *last = NULL;
1987 /* If the vtables for this class have already been emitted there is
1988 nothing more to do. */
1989 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1990 if (var_finalized_p (primary_vtbl))
1991 return false;
1992 /* Ignore dummy vtables made by get_vtable_decl. */
1993 if (TREE_TYPE (primary_vtbl) == void_type_node)
1994 return false;
1996 /* On some targets, we cannot determine the key method until the end
1997 of the translation unit -- which is when this function is
1998 called. */
1999 if (!targetm.cxx.key_method_may_be_inline ())
2000 determine_key_method (ctype);
2002 /* See if any of the vtables are needed. */
2003 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2005 import_export_decl (vtbl);
2006 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2007 needed = 1;
2009 if (!needed)
2011 /* If the references to this class' vtables are optimized away,
2012 still emit the appropriate debugging information. See
2013 dfs_debug_mark. */
2014 if (DECL_COMDAT (primary_vtbl)
2015 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2016 note_debug_info_needed (ctype);
2017 return false;
2020 /* The ABI requires that we emit all of the vtables if we emit any
2021 of them. */
2022 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2024 /* Mark entities references from the virtual table as used. */
2025 mark_vtable_entries (vtbl);
2027 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2029 vec<tree, va_gc> *cleanups = NULL;
2030 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2031 LOOKUP_NORMAL);
2033 /* It had better be all done at compile-time. */
2034 gcc_assert (!expr && !cleanups);
2037 /* Write it out. */
2038 DECL_EXTERNAL (vtbl) = 0;
2039 rest_of_decl_compilation (vtbl, 1, 1);
2041 /* Because we're only doing syntax-checking, we'll never end up
2042 actually marking the variable as written. */
2043 if (flag_syntax_only)
2044 TREE_ASM_WRITTEN (vtbl) = 1;
2045 else if (DECL_ONE_ONLY (vtbl))
2047 current = varpool_node::get_create (vtbl);
2048 if (last)
2049 current->add_to_same_comdat_group (last);
2050 last = current;
2054 /* Since we're writing out the vtable here, also write the debug
2055 info. */
2056 note_debug_info_needed (ctype);
2058 return true;
2061 /* A special return value from type_visibility meaning internal
2062 linkage. */
2064 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2066 /* walk_tree helper function for type_visibility. */
2068 static tree
2069 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2071 int *vis_p = (int *)data;
2072 if (! TYPE_P (*tp))
2074 *walk_subtrees = 0;
2076 else if (OVERLOAD_TYPE_P (*tp)
2077 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2079 *vis_p = VISIBILITY_ANON;
2080 return *tp;
2082 else if (CLASS_TYPE_P (*tp)
2083 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2084 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2085 return NULL;
2088 /* Returns the visibility of TYPE, which is the minimum visibility of its
2089 component types. */
2091 static int
2092 type_visibility (tree type)
2094 int vis = VISIBILITY_DEFAULT;
2095 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2096 return vis;
2099 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2100 specified (or if VISIBILITY is static). If TMPL is true, this
2101 constraint is for a template argument, and takes precedence
2102 over explicitly-specified visibility on the template. */
2104 static void
2105 constrain_visibility (tree decl, int visibility, bool tmpl)
2107 if (visibility == VISIBILITY_ANON)
2109 /* extern "C" declarations aren't affected by the anonymous
2110 namespace. */
2111 if (!DECL_EXTERN_C_P (decl))
2113 TREE_PUBLIC (decl) = 0;
2114 DECL_WEAK (decl) = 0;
2115 DECL_COMMON (decl) = 0;
2116 if (TREE_CODE (decl) == FUNCTION_DECL
2117 || TREE_CODE (decl) == VAR_DECL)
2119 struct symtab_node *snode = symtab_node::get (decl);
2121 if (snode)
2122 snode->set_comdat_group (NULL);
2124 DECL_INTERFACE_KNOWN (decl) = 1;
2125 if (DECL_LANG_SPECIFIC (decl))
2126 DECL_NOT_REALLY_EXTERN (decl) = 1;
2129 else if (visibility > DECL_VISIBILITY (decl)
2130 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2132 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2133 /* This visibility was not specified. */
2134 DECL_VISIBILITY_SPECIFIED (decl) = false;
2138 /* Constrain the visibility of DECL based on the visibility of its template
2139 arguments. */
2141 static void
2142 constrain_visibility_for_template (tree decl, tree targs)
2144 /* If this is a template instantiation, check the innermost
2145 template args for visibility constraints. The outer template
2146 args are covered by the class check. */
2147 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2148 int i;
2149 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2151 int vis = 0;
2153 tree arg = TREE_VEC_ELT (args, i-1);
2154 if (TYPE_P (arg))
2155 vis = type_visibility (arg);
2156 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2158 STRIP_NOPS (arg);
2159 if (TREE_CODE (arg) == ADDR_EXPR)
2160 arg = TREE_OPERAND (arg, 0);
2161 if (VAR_OR_FUNCTION_DECL_P (arg))
2163 if (! TREE_PUBLIC (arg))
2164 vis = VISIBILITY_ANON;
2165 else
2166 vis = DECL_VISIBILITY (arg);
2169 if (vis)
2170 constrain_visibility (decl, vis, true);
2174 /* Like c_determine_visibility, but with additional C++-specific
2175 behavior.
2177 Function-scope entities can rely on the function's visibility because
2178 it is set in start_preparsed_function.
2180 Class-scope entities cannot rely on the class's visibility until the end
2181 of the enclosing class definition.
2183 Note that because namespaces have multiple independent definitions,
2184 namespace visibility is handled elsewhere using the #pragma visibility
2185 machinery rather than by decorating the namespace declaration.
2187 The goal is for constraints from the type to give a diagnostic, and
2188 other constraints to be applied silently. */
2190 void
2191 determine_visibility (tree decl)
2193 tree class_type = NULL_TREE;
2194 bool use_template;
2195 bool orig_visibility_specified;
2196 enum symbol_visibility orig_visibility;
2198 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2200 /* Only relevant for names with external linkage. */
2201 if (!TREE_PUBLIC (decl))
2202 return;
2204 /* Cloned constructors and destructors get the same visibility as
2205 the underlying function. That should be set up in
2206 maybe_clone_body. */
2207 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2209 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2210 orig_visibility = DECL_VISIBILITY (decl);
2212 if (TREE_CODE (decl) == TYPE_DECL)
2214 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2215 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2216 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2217 use_template = 1;
2218 else
2219 use_template = 0;
2221 else if (DECL_LANG_SPECIFIC (decl))
2222 use_template = DECL_USE_TEMPLATE (decl);
2223 else
2224 use_template = 0;
2226 /* If DECL is a member of a class, visibility specifiers on the
2227 class can influence the visibility of the DECL. */
2228 if (DECL_CLASS_SCOPE_P (decl))
2229 class_type = DECL_CONTEXT (decl);
2230 else
2232 /* Not a class member. */
2234 /* Virtual tables have DECL_CONTEXT set to their associated class,
2235 so they are automatically handled above. */
2236 gcc_assert (!VAR_P (decl)
2237 || !DECL_VTABLE_OR_VTT_P (decl));
2239 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2241 /* Local statics and classes get the visibility of their
2242 containing function by default, except that
2243 -fvisibility-inlines-hidden doesn't affect them. */
2244 tree fn = DECL_CONTEXT (decl);
2245 if (DECL_VISIBILITY_SPECIFIED (fn))
2247 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2248 DECL_VISIBILITY_SPECIFIED (decl) =
2249 DECL_VISIBILITY_SPECIFIED (fn);
2251 else
2253 if (DECL_CLASS_SCOPE_P (fn))
2254 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2255 else if (determine_hidden_inline (fn))
2257 DECL_VISIBILITY (decl) = default_visibility;
2258 DECL_VISIBILITY_SPECIFIED (decl) =
2259 visibility_options.inpragma;
2261 else
2263 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2264 DECL_VISIBILITY_SPECIFIED (decl) =
2265 DECL_VISIBILITY_SPECIFIED (fn);
2269 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2270 but have no TEMPLATE_INFO, so don't try to check it. */
2271 use_template = 0;
2273 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2274 && flag_visibility_ms_compat)
2276 /* Under -fvisibility-ms-compat, types are visible by default,
2277 even though their contents aren't. */
2278 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2279 int underlying_vis = type_visibility (underlying_type);
2280 if (underlying_vis == VISIBILITY_ANON
2281 || (CLASS_TYPE_P (underlying_type)
2282 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2283 constrain_visibility (decl, underlying_vis, false);
2284 else
2285 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2287 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2289 /* tinfo visibility is based on the type it's for. */
2290 constrain_visibility
2291 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2293 /* Give the target a chance to override the visibility associated
2294 with DECL. */
2295 if (TREE_PUBLIC (decl)
2296 && !DECL_REALLY_EXTERN (decl)
2297 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2298 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2299 targetm.cxx.determine_class_data_visibility (decl);
2301 else if (use_template)
2302 /* Template instantiations and specializations get visibility based
2303 on their template unless they override it with an attribute. */;
2304 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2306 if (determine_hidden_inline (decl))
2307 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2308 else
2310 /* Set default visibility to whatever the user supplied with
2311 #pragma GCC visibility or a namespace visibility attribute. */
2312 DECL_VISIBILITY (decl) = default_visibility;
2313 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2318 if (use_template)
2320 /* If the specialization doesn't specify visibility, use the
2321 visibility from the template. */
2322 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2323 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2324 : DECL_TEMPLATE_INFO (decl));
2325 tree args = TI_ARGS (tinfo);
2326 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2327 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2328 : DECL_ATTRIBUTES (decl));
2330 if (args != error_mark_node)
2332 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2334 if (!DECL_VISIBILITY_SPECIFIED (decl))
2336 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2337 && determine_hidden_inline (decl))
2338 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2339 else
2341 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2342 DECL_VISIBILITY_SPECIFIED (decl)
2343 = DECL_VISIBILITY_SPECIFIED (pattern);
2347 if (args
2348 /* Template argument visibility outweighs #pragma or namespace
2349 visibility, but not an explicit attribute. */
2350 && !lookup_attribute ("visibility", attribs))
2352 int depth = TMPL_ARGS_DEPTH (args);
2353 if (DECL_VISIBILITY_SPECIFIED (decl))
2355 /* A class template member with explicit visibility
2356 overrides the class visibility, so we need to apply
2357 all the levels of template args directly. */
2358 int i;
2359 for (i = 1; i <= depth; ++i)
2361 tree lev = TMPL_ARGS_LEVEL (args, i);
2362 constrain_visibility_for_template (decl, lev);
2365 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2366 /* Limit visibility based on its template arguments. */
2367 constrain_visibility_for_template (decl, args);
2372 if (class_type)
2373 determine_visibility_from_class (decl, class_type);
2375 if (decl_anon_ns_mem_p (decl))
2376 /* Names in an anonymous namespace get internal linkage.
2377 This might change once we implement export. */
2378 constrain_visibility (decl, VISIBILITY_ANON, false);
2379 else if (TREE_CODE (decl) != TYPE_DECL)
2381 /* Propagate anonymity from type to decl. */
2382 int tvis = type_visibility (TREE_TYPE (decl));
2383 if (tvis == VISIBILITY_ANON
2384 || ! DECL_VISIBILITY_SPECIFIED (decl))
2385 constrain_visibility (decl, tvis, false);
2387 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2388 /* DR 757: A type without linkage shall not be used as the type of a
2389 variable or function with linkage, unless
2390 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2391 o the variable or function is not used (3.2 [basic.def.odr]) or is
2392 defined in the same translation unit.
2394 Since non-extern "C" decls need to be defined in the same
2395 translation unit, we can make the type internal. */
2396 constrain_visibility (decl, VISIBILITY_ANON, false);
2398 /* If visibility changed and DECL already has DECL_RTL, ensure
2399 symbol flags are updated. */
2400 if ((DECL_VISIBILITY (decl) != orig_visibility
2401 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2402 && ((VAR_P (decl) && TREE_STATIC (decl))
2403 || TREE_CODE (decl) == FUNCTION_DECL)
2404 && DECL_RTL_SET_P (decl))
2405 make_decl_rtl (decl);
2408 /* By default, static data members and function members receive
2409 the visibility of their containing class. */
2411 static void
2412 determine_visibility_from_class (tree decl, tree class_type)
2414 if (DECL_VISIBILITY_SPECIFIED (decl))
2415 return;
2417 if (determine_hidden_inline (decl))
2418 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2419 else
2421 /* Default to the class visibility. */
2422 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2423 DECL_VISIBILITY_SPECIFIED (decl)
2424 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2427 /* Give the target a chance to override the visibility associated
2428 with DECL. */
2429 if (VAR_P (decl)
2430 && (DECL_TINFO_P (decl)
2431 || (DECL_VTABLE_OR_VTT_P (decl)
2432 /* Construction virtual tables are not exported because
2433 they cannot be referred to from other object files;
2434 their name is not standardized by the ABI. */
2435 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2436 && TREE_PUBLIC (decl)
2437 && !DECL_REALLY_EXTERN (decl)
2438 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2439 targetm.cxx.determine_class_data_visibility (decl);
2442 /* Returns true iff DECL is an inline that should get hidden visibility
2443 because of -fvisibility-inlines-hidden. */
2445 static bool
2446 determine_hidden_inline (tree decl)
2448 return (visibility_options.inlines_hidden
2449 /* Don't do this for inline templates; specializations might not be
2450 inline, and we don't want them to inherit the hidden
2451 visibility. We'll set it here for all inline instantiations. */
2452 && !processing_template_decl
2453 && TREE_CODE (decl) == FUNCTION_DECL
2454 && DECL_DECLARED_INLINE_P (decl)
2455 && (! DECL_LANG_SPECIFIC (decl)
2456 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2459 /* Constrain the visibility of a class TYPE based on the visibility of its
2460 field types. Warn if any fields require lesser visibility. */
2462 void
2463 constrain_class_visibility (tree type)
2465 tree binfo;
2466 tree t;
2467 int i;
2469 int vis = type_visibility (type);
2471 if (vis == VISIBILITY_ANON
2472 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2473 return;
2475 /* Don't warn about visibility if the class has explicit visibility. */
2476 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2477 vis = VISIBILITY_INTERNAL;
2479 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2480 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2482 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2483 int subvis = type_visibility (ftype);
2485 if (subvis == VISIBILITY_ANON)
2487 if (!in_main_input_context ())
2488 warning (0, "\
2489 %qT has a field %qD whose type uses the anonymous namespace",
2490 type, t);
2492 else if (MAYBE_CLASS_TYPE_P (ftype)
2493 && vis < VISIBILITY_HIDDEN
2494 && subvis >= VISIBILITY_HIDDEN)
2495 warning (OPT_Wattributes, "\
2496 %qT declared with greater visibility than the type of its field %qD",
2497 type, t);
2500 binfo = TYPE_BINFO (type);
2501 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2503 int subvis = type_visibility (TREE_TYPE (t));
2505 if (subvis == VISIBILITY_ANON)
2507 if (!in_main_input_context())
2508 warning (0, "\
2509 %qT has a base %qT whose type uses the anonymous namespace",
2510 type, TREE_TYPE (t));
2512 else if (vis < VISIBILITY_HIDDEN
2513 && subvis >= VISIBILITY_HIDDEN)
2514 warning (OPT_Wattributes, "\
2515 %qT declared with greater visibility than its base %qT",
2516 type, TREE_TYPE (t));
2520 /* Functions for adjusting the visibility of a tagged type and its nested
2521 types and declarations when it gets a name for linkage purposes from a
2522 typedef. */
2524 static void bt_reset_linkage_1 (binding_entry, void *);
2525 static void bt_reset_linkage_2 (binding_entry, void *);
2527 /* First reset the visibility of all the types. */
2529 static void
2530 reset_type_linkage_1 (tree type)
2532 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2533 if (CLASS_TYPE_P (type))
2534 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2535 bt_reset_linkage_1, NULL);
2537 static void
2538 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2540 reset_type_linkage_1 (b->type);
2543 /* Then reset the visibility of any static data members or member
2544 functions that use those types. */
2546 static void
2547 reset_decl_linkage (tree decl)
2549 if (TREE_PUBLIC (decl))
2550 return;
2551 if (DECL_CLONED_FUNCTION_P (decl))
2552 return;
2553 TREE_PUBLIC (decl) = true;
2554 DECL_INTERFACE_KNOWN (decl) = false;
2555 determine_visibility (decl);
2556 tentative_decl_linkage (decl);
2558 static void
2559 reset_type_linkage_2 (tree type)
2561 if (CLASS_TYPE_P (type))
2563 if (tree vt = CLASSTYPE_VTABLES (type))
2565 tree name = mangle_vtbl_for_type (type);
2566 DECL_NAME (vt) = name;
2567 SET_DECL_ASSEMBLER_NAME (vt, name);
2568 reset_decl_linkage (vt);
2570 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2572 tree name = mangle_typeinfo_for_type (type);
2573 DECL_NAME (ti) = name;
2574 SET_DECL_ASSEMBLER_NAME (ti, name);
2575 TREE_TYPE (name) = type;
2576 reset_decl_linkage (ti);
2578 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2579 if (TREE_CODE (m) == VAR_DECL)
2580 reset_decl_linkage (m);
2581 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2582 reset_decl_linkage (m);
2583 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2584 bt_reset_linkage_2, NULL);
2587 static void
2588 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2590 reset_type_linkage_2 (b->type);
2592 void
2593 reset_type_linkage (tree type)
2595 reset_type_linkage_1 (type);
2596 reset_type_linkage_2 (type);
2599 /* Set up our initial idea of what the linkage of DECL should be. */
2601 void
2602 tentative_decl_linkage (tree decl)
2604 if (DECL_INTERFACE_KNOWN (decl))
2605 /* We've already made a decision as to how this function will
2606 be handled. */;
2607 else if (vague_linkage_p (decl))
2609 if (TREE_CODE (decl) == FUNCTION_DECL
2610 && decl_defined_p (decl))
2612 DECL_EXTERNAL (decl) = 1;
2613 DECL_NOT_REALLY_EXTERN (decl) = 1;
2614 note_vague_linkage_fn (decl);
2615 /* A non-template inline function with external linkage will
2616 always be COMDAT. As we must eventually determine the
2617 linkage of all functions, and as that causes writes to
2618 the data mapped in from the PCH file, it's advantageous
2619 to mark the functions at this point. */
2620 if (DECL_DECLARED_INLINE_P (decl)
2621 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2622 || DECL_DEFAULTED_FN (decl)))
2624 /* This function must have external linkage, as
2625 otherwise DECL_INTERFACE_KNOWN would have been
2626 set. */
2627 gcc_assert (TREE_PUBLIC (decl));
2628 comdat_linkage (decl);
2629 DECL_INTERFACE_KNOWN (decl) = 1;
2632 else if (TREE_CODE (decl) == VAR_DECL)
2633 maybe_commonize_var (decl);
2637 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2638 for DECL has not already been determined, do so now by setting
2639 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2640 function is called entities with vague linkage whose definitions
2641 are available must have TREE_PUBLIC set.
2643 If this function decides to place DECL in COMDAT, it will set
2644 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2645 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2646 callers defer that decision until it is clear that DECL is actually
2647 required. */
2649 void
2650 import_export_decl (tree decl)
2652 int emit_p;
2653 bool comdat_p;
2654 bool import_p;
2655 tree class_type = NULL_TREE;
2657 if (DECL_INTERFACE_KNOWN (decl))
2658 return;
2660 /* We cannot determine what linkage to give to an entity with vague
2661 linkage until the end of the file. For example, a virtual table
2662 for a class will be defined if and only if the key method is
2663 defined in this translation unit. As a further example, consider
2664 that when compiling a translation unit that uses PCH file with
2665 "-frepo" it would be incorrect to make decisions about what
2666 entities to emit when building the PCH; those decisions must be
2667 delayed until the repository information has been processed. */
2668 gcc_assert (at_eof);
2669 /* Object file linkage for explicit instantiations is handled in
2670 mark_decl_instantiated. For static variables in functions with
2671 vague linkage, maybe_commonize_var is used.
2673 Therefore, the only declarations that should be provided to this
2674 function are those with external linkage that are:
2676 * implicit instantiations of function templates
2678 * inline function
2680 * implicit instantiations of static data members of class
2681 templates
2683 * virtual tables
2685 * typeinfo objects
2687 Furthermore, all entities that reach this point must have a
2688 definition available in this translation unit.
2690 The following assertions check these conditions. */
2691 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2692 /* Any code that creates entities with TREE_PUBLIC cleared should
2693 also set DECL_INTERFACE_KNOWN. */
2694 gcc_assert (TREE_PUBLIC (decl));
2695 if (TREE_CODE (decl) == FUNCTION_DECL)
2696 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2697 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2698 || DECL_DECLARED_INLINE_P (decl));
2699 else
2700 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2701 || DECL_VTABLE_OR_VTT_P (decl)
2702 || DECL_TINFO_P (decl));
2703 /* Check that a definition of DECL is available in this translation
2704 unit. */
2705 gcc_assert (!DECL_REALLY_EXTERN (decl));
2707 /* Assume that DECL will not have COMDAT linkage. */
2708 comdat_p = false;
2709 /* Assume that DECL will not be imported into this translation
2710 unit. */
2711 import_p = false;
2713 /* See if the repository tells us whether or not to emit DECL in
2714 this translation unit. */
2715 emit_p = repo_emit_p (decl);
2716 if (emit_p == 0)
2717 import_p = true;
2718 else if (emit_p == 1)
2720 /* The repository indicates that this entity should be defined
2721 here. Make sure the back end honors that request. */
2722 mark_needed (decl);
2723 /* Output the definition as an ordinary strong definition. */
2724 DECL_EXTERNAL (decl) = 0;
2725 DECL_INTERFACE_KNOWN (decl) = 1;
2726 return;
2729 if (import_p)
2730 /* We have already decided what to do with this DECL; there is no
2731 need to check anything further. */
2733 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2735 class_type = DECL_CONTEXT (decl);
2736 import_export_class (class_type);
2737 if (TYPE_FOR_JAVA (class_type))
2738 import_p = true;
2739 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2740 && CLASSTYPE_INTERFACE_ONLY (class_type))
2741 import_p = true;
2742 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2743 && !CLASSTYPE_USE_TEMPLATE (class_type)
2744 && CLASSTYPE_KEY_METHOD (class_type)
2745 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2746 /* The ABI requires that all virtual tables be emitted with
2747 COMDAT linkage. However, on systems where COMDAT symbols
2748 don't show up in the table of contents for a static
2749 archive, or on systems without weak symbols (where we
2750 approximate COMDAT linkage by using internal linkage), the
2751 linker will report errors about undefined symbols because
2752 it will not see the virtual table definition. Therefore,
2753 in the case that we know that the virtual table will be
2754 emitted in only one translation unit, we make the virtual
2755 table an ordinary definition with external linkage. */
2756 DECL_EXTERNAL (decl) = 0;
2757 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2759 /* CLASS_TYPE is being exported from this translation unit,
2760 so DECL should be defined here. */
2761 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2762 /* If a class is declared in a header with the "extern
2763 template" extension, then it will not be instantiated,
2764 even in translation units that would normally require
2765 it. Often such classes are explicitly instantiated in
2766 one translation unit. Therefore, the explicit
2767 instantiation must be made visible to other translation
2768 units. */
2769 DECL_EXTERNAL (decl) = 0;
2770 else
2772 /* The generic C++ ABI says that class data is always
2773 COMDAT, even if there is a key function. Some
2774 variants (e.g., the ARM EABI) says that class data
2775 only has COMDAT linkage if the class data might be
2776 emitted in more than one translation unit. When the
2777 key method can be inline and is inline, we still have
2778 to arrange for comdat even though
2779 class_data_always_comdat is false. */
2780 if (!CLASSTYPE_KEY_METHOD (class_type)
2781 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2782 || targetm.cxx.class_data_always_comdat ())
2784 /* The ABI requires COMDAT linkage. Normally, we
2785 only emit COMDAT things when they are needed;
2786 make sure that we realize that this entity is
2787 indeed needed. */
2788 comdat_p = true;
2789 mark_needed (decl);
2793 else if (!flag_implicit_templates
2794 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2795 import_p = true;
2796 else
2797 comdat_p = true;
2799 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2801 tree type = TREE_TYPE (DECL_NAME (decl));
2802 if (CLASS_TYPE_P (type))
2804 class_type = type;
2805 import_export_class (type);
2806 if (CLASSTYPE_INTERFACE_KNOWN (type)
2807 && TYPE_POLYMORPHIC_P (type)
2808 && CLASSTYPE_INTERFACE_ONLY (type)
2809 /* If -fno-rtti was specified, then we cannot be sure
2810 that RTTI information will be emitted with the
2811 virtual table of the class, so we must emit it
2812 wherever it is used. */
2813 && flag_rtti)
2814 import_p = true;
2815 else
2817 if (CLASSTYPE_INTERFACE_KNOWN (type)
2818 && !CLASSTYPE_INTERFACE_ONLY (type))
2820 comdat_p = (targetm.cxx.class_data_always_comdat ()
2821 || (CLASSTYPE_KEY_METHOD (type)
2822 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2823 mark_needed (decl);
2824 if (!flag_weak)
2826 comdat_p = false;
2827 DECL_EXTERNAL (decl) = 0;
2830 else
2831 comdat_p = true;
2834 else
2835 comdat_p = true;
2837 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2839 /* DECL is an implicit instantiation of a function or static
2840 data member. */
2841 if ((flag_implicit_templates
2842 && !flag_use_repository)
2843 || (flag_implicit_inline_templates
2844 && TREE_CODE (decl) == FUNCTION_DECL
2845 && DECL_DECLARED_INLINE_P (decl)))
2846 comdat_p = true;
2847 else
2848 /* If we are not implicitly generating templates, then mark
2849 this entity as undefined in this translation unit. */
2850 import_p = true;
2852 else if (DECL_FUNCTION_MEMBER_P (decl))
2854 if (!DECL_DECLARED_INLINE_P (decl))
2856 tree ctype = DECL_CONTEXT (decl);
2857 import_export_class (ctype);
2858 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2860 DECL_NOT_REALLY_EXTERN (decl)
2861 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2862 || (DECL_DECLARED_INLINE_P (decl)
2863 && ! flag_implement_inlines
2864 && !DECL_VINDEX (decl)));
2866 if (!DECL_NOT_REALLY_EXTERN (decl))
2867 DECL_EXTERNAL (decl) = 1;
2869 /* Always make artificials weak. */
2870 if (DECL_ARTIFICIAL (decl) && flag_weak)
2871 comdat_p = true;
2872 else
2873 maybe_make_one_only (decl);
2876 else
2877 comdat_p = true;
2879 else
2880 comdat_p = true;
2882 if (import_p)
2884 /* If we are importing DECL into this translation unit, mark is
2885 an undefined here. */
2886 DECL_EXTERNAL (decl) = 1;
2887 DECL_NOT_REALLY_EXTERN (decl) = 0;
2889 else if (comdat_p)
2891 /* If we decided to put DECL in COMDAT, mark it accordingly at
2892 this point. */
2893 comdat_linkage (decl);
2896 DECL_INTERFACE_KNOWN (decl) = 1;
2899 /* Return an expression that performs the destruction of DECL, which
2900 must be a VAR_DECL whose type has a non-trivial destructor, or is
2901 an array whose (innermost) elements have a non-trivial destructor. */
2903 tree
2904 build_cleanup (tree decl)
2906 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2907 gcc_assert (clean != NULL_TREE);
2908 return clean;
2911 /* Returns the initialization guard variable for the variable DECL,
2912 which has static storage duration. */
2914 tree
2915 get_guard (tree decl)
2917 tree sname;
2918 tree guard;
2920 sname = mangle_guard_variable (decl);
2921 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2922 if (! guard)
2924 tree guard_type;
2926 /* We use a type that is big enough to contain a mutex as well
2927 as an integer counter. */
2928 guard_type = targetm.cxx.guard_type ();
2929 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2930 VAR_DECL, sname, guard_type);
2932 /* The guard should have the same linkage as what it guards. */
2933 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2934 TREE_STATIC (guard) = TREE_STATIC (decl);
2935 DECL_COMMON (guard) = DECL_COMMON (decl);
2936 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2937 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2938 if (DECL_ONE_ONLY (decl))
2939 make_decl_one_only (guard, cxx_comdat_group (guard));
2940 if (TREE_PUBLIC (decl))
2941 DECL_WEAK (guard) = DECL_WEAK (decl);
2942 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2943 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2945 DECL_ARTIFICIAL (guard) = 1;
2946 DECL_IGNORED_P (guard) = 1;
2947 TREE_USED (guard) = 1;
2948 pushdecl_top_level_and_finish (guard, NULL_TREE);
2950 return guard;
2953 /* Return those bits of the GUARD variable that should be set when the
2954 guarded entity is actually initialized. */
2956 static tree
2957 get_guard_bits (tree guard)
2959 if (!targetm.cxx.guard_mask_bit ())
2961 /* We only set the first byte of the guard, in order to leave room
2962 for a mutex in the high-order bits. */
2963 guard = build1 (ADDR_EXPR,
2964 build_pointer_type (TREE_TYPE (guard)),
2965 guard);
2966 guard = build1 (NOP_EXPR,
2967 build_pointer_type (char_type_node),
2968 guard);
2969 guard = build1 (INDIRECT_REF, char_type_node, guard);
2972 return guard;
2975 /* Return an expression which determines whether or not the GUARD
2976 variable has already been initialized. */
2978 tree
2979 get_guard_cond (tree guard)
2981 tree guard_value;
2983 /* Check to see if the GUARD is zero. */
2984 guard = get_guard_bits (guard);
2986 /* Mask off all but the low bit. */
2987 if (targetm.cxx.guard_mask_bit ())
2989 guard_value = integer_one_node;
2990 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2991 guard_value = convert (TREE_TYPE (guard), guard_value);
2992 guard = cp_build_binary_op (input_location,
2993 BIT_AND_EXPR, guard, guard_value,
2994 tf_warning_or_error);
2997 guard_value = integer_zero_node;
2998 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2999 guard_value = convert (TREE_TYPE (guard), guard_value);
3000 return cp_build_binary_op (input_location,
3001 EQ_EXPR, guard, guard_value,
3002 tf_warning_or_error);
3005 /* Return an expression which sets the GUARD variable, indicating that
3006 the variable being guarded has been initialized. */
3008 tree
3009 set_guard (tree guard)
3011 tree guard_init;
3013 /* Set the GUARD to one. */
3014 guard = get_guard_bits (guard);
3015 guard_init = integer_one_node;
3016 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3017 guard_init = convert (TREE_TYPE (guard), guard_init);
3018 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3019 tf_warning_or_error);
3022 /* Returns true iff we can tell that VAR does not have a dynamic
3023 initializer. */
3025 static bool
3026 var_defined_without_dynamic_init (tree var)
3028 /* If it's defined in another TU, we can't tell. */
3029 if (DECL_EXTERNAL (var))
3030 return false;
3031 /* If it has a non-trivial destructor, registering the destructor
3032 counts as dynamic initialization. */
3033 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3034 return false;
3035 /* If it's in this TU, its initializer has been processed. */
3036 gcc_assert (DECL_INITIALIZED_P (var));
3037 /* If it has no initializer or a constant one, it's not dynamic. */
3038 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3039 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3042 /* Returns true iff VAR is a variable that needs uses to be
3043 wrapped for possible dynamic initialization. */
3045 static bool
3046 var_needs_tls_wrapper (tree var)
3048 return (!error_operand_p (var)
3049 && DECL_THREAD_LOCAL_P (var)
3050 && !DECL_GNU_TLS_P (var)
3051 && !DECL_FUNCTION_SCOPE_P (var)
3052 && !var_defined_without_dynamic_init (var));
3055 /* Get the FUNCTION_DECL for the shared TLS init function for this
3056 translation unit. */
3058 static tree
3059 get_local_tls_init_fn (void)
3061 tree sname = get_identifier ("__tls_init");
3062 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3063 if (!fn)
3065 fn = build_lang_decl (FUNCTION_DECL, sname,
3066 build_function_type (void_type_node,
3067 void_list_node));
3068 SET_DECL_LANGUAGE (fn, lang_c);
3069 TREE_PUBLIC (fn) = false;
3070 DECL_ARTIFICIAL (fn) = true;
3071 mark_used (fn);
3072 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3074 return fn;
3077 /* Get a FUNCTION_DECL for the init function for the thread_local
3078 variable VAR. The init function will be an alias to the function
3079 that initializes all the non-local TLS variables in the translation
3080 unit. The init function is only used by the wrapper function. */
3082 static tree
3083 get_tls_init_fn (tree var)
3085 /* Only C++11 TLS vars need this init fn. */
3086 if (!var_needs_tls_wrapper (var))
3087 return NULL_TREE;
3089 /* If -fno-extern-tls-init, assume that we don't need to call
3090 a tls init function for a variable defined in another TU. */
3091 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3092 return NULL_TREE;
3094 #ifdef ASM_OUTPUT_DEF
3095 /* If the variable is internal, or if we can't generate aliases,
3096 call the local init function directly. */
3097 if (!TREE_PUBLIC (var))
3098 #endif
3099 return get_local_tls_init_fn ();
3101 tree sname = mangle_tls_init_fn (var);
3102 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3103 if (!fn)
3105 fn = build_lang_decl (FUNCTION_DECL, sname,
3106 build_function_type (void_type_node,
3107 void_list_node));
3108 SET_DECL_LANGUAGE (fn, lang_c);
3109 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3110 DECL_ARTIFICIAL (fn) = true;
3111 DECL_COMDAT (fn) = DECL_COMDAT (var);
3112 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3113 if (DECL_ONE_ONLY (var))
3114 make_decl_one_only (fn, cxx_comdat_group (fn));
3115 if (TREE_PUBLIC (var))
3117 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3118 /* If the variable is defined somewhere else and might have static
3119 initialization, make the init function a weak reference. */
3120 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3121 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3122 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3123 && DECL_EXTERNAL (var))
3124 declare_weak (fn);
3125 else
3126 DECL_WEAK (fn) = DECL_WEAK (var);
3128 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3129 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3130 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3131 DECL_IGNORED_P (fn) = 1;
3132 mark_used (fn);
3134 DECL_BEFRIENDING_CLASSES (fn) = var;
3136 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3138 return fn;
3141 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3142 variable VAR. The wrapper function calls the init function (if any) for
3143 VAR and then returns a reference to VAR. The wrapper function is used
3144 in place of VAR everywhere VAR is mentioned. */
3146 tree
3147 get_tls_wrapper_fn (tree var)
3149 /* Only C++11 TLS vars need this wrapper fn. */
3150 if (!var_needs_tls_wrapper (var))
3151 return NULL_TREE;
3153 tree sname = mangle_tls_wrapper_fn (var);
3154 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3155 if (!fn)
3157 /* A named rvalue reference is an lvalue, so the wrapper should
3158 always return an lvalue reference. */
3159 tree type = non_reference (TREE_TYPE (var));
3160 type = build_reference_type (type);
3161 tree fntype = build_function_type (type, void_list_node);
3162 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3163 SET_DECL_LANGUAGE (fn, lang_c);
3164 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3165 DECL_ARTIFICIAL (fn) = true;
3166 DECL_IGNORED_P (fn) = 1;
3167 /* The wrapper is inline and emitted everywhere var is used. */
3168 DECL_DECLARED_INLINE_P (fn) = true;
3169 if (TREE_PUBLIC (var))
3171 comdat_linkage (fn);
3172 #ifdef HAVE_GAS_HIDDEN
3173 /* Make the wrapper bind locally; there's no reason to share
3174 the wrapper between multiple shared objects. */
3175 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3176 DECL_VISIBILITY_SPECIFIED (fn) = true;
3177 #endif
3179 if (!TREE_PUBLIC (fn))
3180 DECL_INTERFACE_KNOWN (fn) = true;
3181 mark_used (fn);
3182 note_vague_linkage_fn (fn);
3184 #if 0
3185 /* We want CSE to commonize calls to the wrapper, but marking it as
3186 pure is unsafe since it has side-effects. I guess we need a new
3187 ECF flag even weaker than ECF_PURE. FIXME! */
3188 DECL_PURE_P (fn) = true;
3189 #endif
3191 DECL_BEFRIENDING_CLASSES (fn) = var;
3193 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3195 return fn;
3198 /* At EOF, generate the definition for the TLS wrapper function FN:
3200 T& var_wrapper() {
3201 if (init_fn) init_fn();
3202 return var;
3203 } */
3205 static void
3206 generate_tls_wrapper (tree fn)
3208 tree var = DECL_BEFRIENDING_CLASSES (fn);
3210 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3211 tree body = begin_function_body ();
3212 /* Only call the init fn if there might be one. */
3213 if (tree init_fn = get_tls_init_fn (var))
3215 tree if_stmt = NULL_TREE;
3216 /* If init_fn is a weakref, make sure it exists before calling. */
3217 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3219 if_stmt = begin_if_stmt ();
3220 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3221 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3222 NE_EXPR, addr, nullptr_node,
3223 tf_warning_or_error);
3224 finish_if_stmt_cond (cond, if_stmt);
3226 finish_expr_stmt (build_cxx_call
3227 (init_fn, 0, NULL, tf_warning_or_error));
3228 if (if_stmt)
3230 finish_then_clause (if_stmt);
3231 finish_if_stmt (if_stmt);
3234 else
3235 /* If there's no initialization, the wrapper is a constant function. */
3236 TREE_READONLY (fn) = true;
3237 finish_return_stmt (convert_from_reference (var));
3238 finish_function_body (body);
3239 expand_or_defer_fn (finish_function (0));
3242 /* Start the process of running a particular set of global constructors
3243 or destructors. Subroutine of do_[cd]tors. Also called from
3244 vtv_start_verification_constructor_init_function. */
3246 static tree
3247 start_objects (int method_type, int initp)
3249 tree body;
3250 tree fndecl;
3251 char type[14];
3253 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3255 if (initp != DEFAULT_INIT_PRIORITY)
3257 char joiner;
3259 #ifdef JOINER
3260 joiner = JOINER;
3261 #else
3262 joiner = '_';
3263 #endif
3265 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3267 else
3268 sprintf (type, "sub_%c", method_type);
3270 fndecl = build_lang_decl (FUNCTION_DECL,
3271 get_file_function_name (type),
3272 build_function_type_list (void_type_node,
3273 NULL_TREE));
3274 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3276 TREE_PUBLIC (current_function_decl) = 0;
3278 /* Mark as artificial because it's not explicitly in the user's
3279 source code. */
3280 DECL_ARTIFICIAL (current_function_decl) = 1;
3282 /* Mark this declaration as used to avoid spurious warnings. */
3283 TREE_USED (current_function_decl) = 1;
3285 /* Mark this function as a global constructor or destructor. */
3286 if (method_type == 'I')
3287 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3288 else
3289 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3291 body = begin_compound_stmt (BCS_FN_BODY);
3293 return body;
3296 /* Finish the process of running a particular set of global constructors
3297 or destructors. Subroutine of do_[cd]tors. */
3299 static void
3300 finish_objects (int method_type, int initp, tree body)
3302 tree fn;
3304 /* Finish up. */
3305 finish_compound_stmt (body);
3306 fn = finish_function (0);
3308 if (method_type == 'I')
3310 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3311 decl_init_priority_insert (fn, initp);
3313 else
3315 DECL_STATIC_DESTRUCTOR (fn) = 1;
3316 decl_fini_priority_insert (fn, initp);
3319 expand_or_defer_fn (fn);
3322 /* The names of the parameters to the function created to handle
3323 initializations and destructions for objects with static storage
3324 duration. */
3325 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3326 #define PRIORITY_IDENTIFIER "__priority"
3328 /* The name of the function we create to handle initializations and
3329 destructions for objects with static storage duration. */
3330 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3332 /* The declaration for the __INITIALIZE_P argument. */
3333 static GTY(()) tree initialize_p_decl;
3335 /* The declaration for the __PRIORITY argument. */
3336 static GTY(()) tree priority_decl;
3338 /* The declaration for the static storage duration function. */
3339 static GTY(()) tree ssdf_decl;
3341 /* All the static storage duration functions created in this
3342 translation unit. */
3343 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3345 /* A map from priority levels to information about that priority
3346 level. There may be many such levels, so efficient lookup is
3347 important. */
3348 static splay_tree priority_info_map;
3350 /* Begins the generation of the function that will handle all
3351 initialization and destruction of objects with static storage
3352 duration. The function generated takes two parameters of type
3353 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3354 nonzero, it performs initializations. Otherwise, it performs
3355 destructions. It only performs those initializations or
3356 destructions with the indicated __PRIORITY. The generated function
3357 returns no value.
3359 It is assumed that this function will only be called once per
3360 translation unit. */
3362 static tree
3363 start_static_storage_duration_function (unsigned count)
3365 tree type;
3366 tree body;
3367 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 64];
3369 /* Create the identifier for this function. It will be of the form
3370 SSDF_IDENTIFIER_<number>. */
3371 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3372 if (L_IPO_IS_AUXILIARY_MODULE)
3373 sprintf (id, "%s.cmo.%u", id, current_module_id);
3375 type = build_function_type_list (void_type_node,
3376 integer_type_node, integer_type_node,
3377 NULL_TREE);
3379 /* Create the FUNCTION_DECL itself. */
3380 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3381 get_identifier (id),
3382 type);
3383 TREE_PUBLIC (ssdf_decl) = 0;
3384 DECL_ARTIFICIAL (ssdf_decl) = 1;
3386 /* Put this function in the list of functions to be called from the
3387 static constructors and destructors. */
3388 if (!ssdf_decls)
3390 vec_alloc (ssdf_decls, 32);
3392 /* Take this opportunity to initialize the map from priority
3393 numbers to information about that priority level. */
3394 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3395 /*delete_key_fn=*/0,
3396 /*delete_value_fn=*/
3397 (splay_tree_delete_value_fn) &free);
3399 /* We always need to generate functions for the
3400 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3401 priorities later, we'll be sure to find the
3402 DEFAULT_INIT_PRIORITY. */
3403 get_priority_info (DEFAULT_INIT_PRIORITY);
3406 vec_safe_push (ssdf_decls, ssdf_decl);
3408 /* Create the argument list. */
3409 initialize_p_decl = cp_build_parm_decl
3410 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3411 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3412 TREE_USED (initialize_p_decl) = 1;
3413 priority_decl = cp_build_parm_decl
3414 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3415 DECL_CONTEXT (priority_decl) = ssdf_decl;
3416 TREE_USED (priority_decl) = 1;
3418 DECL_CHAIN (initialize_p_decl) = priority_decl;
3419 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3421 /* Put the function in the global scope. */
3422 pushdecl (ssdf_decl);
3424 /* Start the function itself. This is equivalent to declaring the
3425 function as:
3427 static void __ssdf (int __initialize_p, init __priority_p);
3429 It is static because we only need to call this function from the
3430 various constructor and destructor functions for this module. */
3431 start_preparsed_function (ssdf_decl,
3432 /*attrs=*/NULL_TREE,
3433 SF_PRE_PARSED);
3435 /* Set up the scope of the outermost block in the function. */
3436 body = begin_compound_stmt (BCS_FN_BODY);
3438 return body;
3441 /* Finish the generation of the function which performs initialization
3442 and destruction of objects with static storage duration. After
3443 this point, no more such objects can be created. */
3445 static void
3446 finish_static_storage_duration_function (tree body)
3448 /* Close out the function. */
3449 finish_compound_stmt (body);
3450 expand_or_defer_fn (finish_function (0));
3453 /* Return the information about the indicated PRIORITY level. If no
3454 code to handle this level has yet been generated, generate the
3455 appropriate prologue. */
3457 static priority_info
3458 get_priority_info (int priority)
3460 priority_info pi;
3461 splay_tree_node n;
3463 n = splay_tree_lookup (priority_info_map,
3464 (splay_tree_key) priority);
3465 if (!n)
3467 /* Create a new priority information structure, and insert it
3468 into the map. */
3469 pi = XNEW (struct priority_info_s);
3470 pi->initializations_p = 0;
3471 pi->destructions_p = 0;
3472 splay_tree_insert (priority_info_map,
3473 (splay_tree_key) priority,
3474 (splay_tree_value) pi);
3476 else
3477 pi = (priority_info) n->value;
3479 return pi;
3482 /* The effective initialization priority of a DECL. */
3484 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3485 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3486 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3488 /* Whether a DECL needs a guard to protect it against multiple
3489 initialization. */
3491 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3492 || DECL_ONE_ONLY (decl) \
3493 || DECL_WEAK (decl)))
3495 /* Called from one_static_initialization_or_destruction(),
3496 via walk_tree.
3497 Walks the initializer list of a global variable and looks for
3498 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3499 and that have their DECL_CONTEXT() == NULL.
3500 For each such temporary variable, set their DECL_CONTEXT() to
3501 the current function. This is necessary because otherwise
3502 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3503 when trying to refer to a temporary variable that does not have
3504 it's DECL_CONTECT() properly set. */
3505 static tree
3506 fix_temporary_vars_context_r (tree *node,
3507 int * /*unused*/,
3508 void * /*unused1*/)
3510 gcc_assert (current_function_decl);
3512 if (TREE_CODE (*node) == BIND_EXPR)
3514 tree var;
3516 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3517 if (VAR_P (var)
3518 && !DECL_NAME (var)
3519 && DECL_ARTIFICIAL (var)
3520 && !DECL_CONTEXT (var))
3521 DECL_CONTEXT (var) = current_function_decl;
3524 return NULL_TREE;
3527 /* Set up to handle the initialization or destruction of DECL. If
3528 INITP is nonzero, we are initializing the variable. Otherwise, we
3529 are destroying it. */
3531 static void
3532 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3534 tree guard_if_stmt = NULL_TREE;
3535 tree guard;
3537 /* If we are supposed to destruct and there's a trivial destructor,
3538 nothing has to be done. */
3539 if (!initp
3540 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3541 return;
3543 /* Trick the compiler into thinking we are at the file and line
3544 where DECL was declared so that error-messages make sense, and so
3545 that the debugger will show somewhat sensible file and line
3546 information. */
3547 input_location = DECL_SOURCE_LOCATION (decl);
3549 /* Make sure temporary variables in the initialiser all have
3550 their DECL_CONTEXT() set to a value different from NULL_TREE.
3551 This can happen when global variables initialisers are built.
3552 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3553 the temporary variables that might have been generated in the
3554 accompagning initialisers is NULL_TREE, meaning the variables have been
3555 declared in the global namespace.
3556 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3557 of the temporaries are set to the current function decl. */
3558 cp_walk_tree_without_duplicates (&init,
3559 fix_temporary_vars_context_r,
3560 NULL);
3562 /* Because of:
3564 [class.access.spec]
3566 Access control for implicit calls to the constructors,
3567 the conversion functions, or the destructor called to
3568 create and destroy a static data member is performed as
3569 if these calls appeared in the scope of the member's
3570 class.
3572 we pretend we are in a static member function of the class of
3573 which the DECL is a member. */
3574 if (member_p (decl))
3576 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3577 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3580 /* Assume we don't need a guard. */
3581 guard = NULL_TREE;
3582 /* We need a guard if this is an object with external linkage that
3583 might be initialized in more than one place. (For example, a
3584 static data member of a template, when the data member requires
3585 construction.) */
3586 if (NEEDS_GUARD_P (decl))
3588 tree guard_cond;
3590 guard = get_guard (decl);
3592 /* When using __cxa_atexit, we just check the GUARD as we would
3593 for a local static. */
3594 if (flag_use_cxa_atexit)
3596 /* When using __cxa_atexit, we never try to destroy
3597 anything from a static destructor. */
3598 gcc_assert (initp);
3599 guard_cond = get_guard_cond (guard);
3601 /* If we don't have __cxa_atexit, then we will be running
3602 destructors from .fini sections, or their equivalents. So,
3603 we need to know how many times we've tried to initialize this
3604 object. We do initializations only if the GUARD is zero,
3605 i.e., if we are the first to initialize the variable. We do
3606 destructions only if the GUARD is one, i.e., if we are the
3607 last to destroy the variable. */
3608 else if (initp)
3609 guard_cond
3610 = cp_build_binary_op (input_location,
3611 EQ_EXPR,
3612 cp_build_unary_op (PREINCREMENT_EXPR,
3613 guard,
3614 /*noconvert=*/1,
3615 tf_warning_or_error),
3616 integer_one_node,
3617 tf_warning_or_error);
3618 else
3619 guard_cond
3620 = cp_build_binary_op (input_location,
3621 EQ_EXPR,
3622 cp_build_unary_op (PREDECREMENT_EXPR,
3623 guard,
3624 /*noconvert=*/1,
3625 tf_warning_or_error),
3626 integer_zero_node,
3627 tf_warning_or_error);
3629 guard_if_stmt = begin_if_stmt ();
3630 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3634 /* If we're using __cxa_atexit, we have not already set the GUARD,
3635 so we must do so now. */
3636 if (guard && initp && flag_use_cxa_atexit)
3637 finish_expr_stmt (set_guard (guard));
3639 /* Perform the initialization or destruction. */
3640 if (initp)
3642 if (init)
3644 finish_expr_stmt (init);
3645 if (flag_sanitize & SANITIZE_ADDRESS)
3647 varpool_node *vnode = varpool_node::get (decl);
3648 if (vnode)
3649 vnode->dynamically_initialized = 1;
3653 /* If we're using __cxa_atexit, register a function that calls the
3654 destructor for the object. */
3655 if (flag_use_cxa_atexit)
3656 finish_expr_stmt (register_dtor_fn (decl));
3658 else
3659 finish_expr_stmt (build_cleanup (decl));
3661 /* Finish the guard if-stmt, if necessary. */
3662 if (guard)
3664 finish_then_clause (guard_if_stmt);
3665 finish_if_stmt (guard_if_stmt);
3668 /* Now that we're done with DECL we don't need to pretend to be a
3669 member of its class any longer. */
3670 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3671 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3674 /* Generate code to do the initialization or destruction of the decls in VARS,
3675 a TREE_LIST of VAR_DECL with static storage duration.
3676 Whether initialization or destruction is performed is specified by INITP. */
3678 static void
3679 do_static_initialization_or_destruction (tree vars, bool initp)
3681 tree node, init_if_stmt, cond;
3683 /* Build the outer if-stmt to check for initialization or destruction. */
3684 init_if_stmt = begin_if_stmt ();
3685 cond = initp ? integer_one_node : integer_zero_node;
3686 cond = cp_build_binary_op (input_location,
3687 EQ_EXPR,
3688 initialize_p_decl,
3689 cond,
3690 tf_warning_or_error);
3691 finish_if_stmt_cond (cond, init_if_stmt);
3693 /* To make sure dynamic construction doesn't access globals from other
3694 compilation units where they might not be yet constructed, for
3695 -fsanitize=address insert __asan_before_dynamic_init call that
3696 prevents access to either all global variables that need construction
3697 in other compilation units, or at least those that haven't been
3698 initialized yet. Variables that need dynamic construction in
3699 the current compilation unit are kept accessible. */
3700 if (flag_sanitize & SANITIZE_ADDRESS)
3701 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3703 node = vars;
3704 do {
3705 tree decl = TREE_VALUE (node);
3706 tree priority_if_stmt;
3707 int priority;
3708 priority_info pi;
3710 /* If we don't need a destructor, there's nothing to do. Avoid
3711 creating a possibly empty if-stmt. */
3712 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3714 node = TREE_CHAIN (node);
3715 continue;
3718 /* Remember that we had an initialization or finalization at this
3719 priority. */
3720 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3721 pi = get_priority_info (priority);
3722 if (initp)
3723 pi->initializations_p = 1;
3724 else
3725 pi->destructions_p = 1;
3727 /* Conditionalize this initialization on being in the right priority
3728 and being initializing/finalizing appropriately. */
3729 priority_if_stmt = begin_if_stmt ();
3730 cond = cp_build_binary_op (input_location,
3731 EQ_EXPR,
3732 priority_decl,
3733 build_int_cst (NULL_TREE, priority),
3734 tf_warning_or_error);
3735 finish_if_stmt_cond (cond, priority_if_stmt);
3737 /* Process initializers with same priority. */
3738 for (; node
3739 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3740 node = TREE_CHAIN (node))
3741 /* Do one initialization or destruction. */
3742 one_static_initialization_or_destruction (TREE_VALUE (node),
3743 TREE_PURPOSE (node), initp);
3745 /* Finish up the priority if-stmt body. */
3746 finish_then_clause (priority_if_stmt);
3747 finish_if_stmt (priority_if_stmt);
3749 } while (node);
3751 /* Revert what __asan_before_dynamic_init did by calling
3752 __asan_after_dynamic_init. */
3753 if (flag_sanitize & SANITIZE_ADDRESS)
3754 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3756 /* Finish up the init/destruct if-stmt body. */
3757 finish_then_clause (init_if_stmt);
3758 finish_if_stmt (init_if_stmt);
3761 /* VARS is a list of variables with static storage duration which may
3762 need initialization and/or finalization. Remove those variables
3763 that don't really need to be initialized or finalized, and return
3764 the resulting list. The order in which the variables appear in
3765 VARS is in reverse order of the order in which they should actually
3766 be initialized. The list we return is in the unreversed order;
3767 i.e., the first variable should be initialized first. */
3769 static tree
3770 prune_vars_needing_no_initialization (tree *vars)
3772 tree *var = vars;
3773 tree result = NULL_TREE;
3775 while (*var)
3777 tree t = *var;
3778 tree decl = TREE_VALUE (t);
3779 tree init = TREE_PURPOSE (t);
3781 /* Deal gracefully with error. */
3782 if (decl == error_mark_node)
3784 var = &TREE_CHAIN (t);
3785 continue;
3788 /* The only things that can be initialized are variables. */
3789 gcc_assert (VAR_P (decl));
3791 /* If this object is not defined, we don't need to do anything
3792 here. */
3793 if (DECL_EXTERNAL (decl))
3795 var = &TREE_CHAIN (t);
3796 continue;
3799 /* Also, if the initializer already contains errors, we can bail
3800 out now. */
3801 if (init && TREE_CODE (init) == TREE_LIST
3802 && value_member (error_mark_node, init))
3804 var = &TREE_CHAIN (t);
3805 continue;
3808 gcc_assert (!L_IPO_IS_AUXILIARY_MODULE
3809 || varpool_is_auxiliary (varpool_node::get_create (decl)));
3811 /* This variable is going to need initialization and/or
3812 finalization, so we add it to the list. */
3813 *var = TREE_CHAIN (t);
3814 TREE_CHAIN (t) = result;
3815 result = t;
3818 return result;
3821 /* Make sure we have told the back end about all the variables in
3822 VARS. */
3824 static void
3825 write_out_vars (tree vars)
3827 tree v;
3829 for (v = vars; v; v = TREE_CHAIN (v))
3831 tree var = TREE_VALUE (v);
3832 if (!var_finalized_p (var))
3834 import_export_decl (var);
3835 rest_of_decl_compilation (var, 1, 1);
3840 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3841 (otherwise) that will initialize all global objects with static
3842 storage duration having the indicated PRIORITY. */
3844 static void
3845 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3846 location_t *locus)
3848 char function_key;
3849 tree fndecl;
3850 tree body;
3851 size_t i;
3853 input_location = *locus;
3854 /* ??? */
3855 /* Was: locus->line++; */
3857 /* We use `I' to indicate initialization and `D' to indicate
3858 destruction. */
3859 function_key = constructor_p ? 'I' : 'D';
3861 /* We emit the function lazily, to avoid generating empty
3862 global constructors and destructors. */
3863 body = NULL_TREE;
3865 /* For Objective-C++, we may need to initialize metadata found in this module.
3866 This must be done _before_ any other static initializations. */
3867 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3868 && constructor_p && objc_static_init_needed_p ())
3870 body = start_objects (function_key, priority);
3871 objc_generate_static_init_call (NULL_TREE);
3874 /* Call the static storage duration function with appropriate
3875 arguments. */
3876 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3878 /* Calls to pure or const functions will expand to nothing. */
3879 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3881 tree call;
3883 if (! body)
3884 body = start_objects (function_key, priority);
3886 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3887 build_int_cst (NULL_TREE,
3888 constructor_p),
3889 build_int_cst (NULL_TREE,
3890 priority),
3891 NULL_TREE);
3892 finish_expr_stmt (call);
3896 /* Close out the function. */
3897 if (body)
3898 finish_objects (function_key, priority, body);
3901 /* Generate constructor and destructor functions for the priority
3902 indicated by N. */
3904 static int
3905 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3907 location_t *locus = (location_t *) data;
3908 int priority = (int) n->key;
3909 priority_info pi = (priority_info) n->value;
3911 /* Generate the functions themselves, but only if they are really
3912 needed. */
3913 if (pi->initializations_p)
3914 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3915 if (pi->destructions_p)
3916 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3918 /* Keep iterating. */
3919 return 0;
3922 /* Java requires that we be able to reference a local address for a
3923 method, and not be confused by PLT entries. If hidden aliases are
3924 supported, collect and return all the functions for which we should
3925 emit a hidden alias. */
3927 static hash_set<tree> *
3928 collect_candidates_for_java_method_aliases (void)
3930 struct cgraph_node *node;
3931 hash_set<tree> *candidates = NULL;
3933 #ifndef HAVE_GAS_HIDDEN
3934 return candidates;
3935 #endif
3937 FOR_EACH_FUNCTION (node)
3939 tree fndecl = node->decl;
3941 if (DECL_CLASS_SCOPE_P (fndecl)
3942 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3943 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3945 if (candidates == NULL)
3946 candidates = new hash_set<tree>;
3947 candidates->add (fndecl);
3951 return candidates;
3955 /* Java requires that we be able to reference a local address for a
3956 method, and not be confused by PLT entries. If hidden aliases are
3957 supported, emit one for each java function that we've emitted.
3958 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3959 by collect_candidates_for_java_method_aliases. */
3961 static void
3962 build_java_method_aliases (hash_set<tree> *candidates)
3964 struct cgraph_node *node;
3966 #ifndef HAVE_GAS_HIDDEN
3967 return;
3968 #endif
3970 FOR_EACH_FUNCTION (node)
3972 tree fndecl = node->decl;
3974 if (TREE_ASM_WRITTEN (fndecl)
3975 && candidates->contains (fndecl))
3977 /* Mangle the name in a predictable way; we need to reference
3978 this from a java compiled object file. */
3979 tree oid, nid, alias;
3980 const char *oname;
3981 char *nname;
3983 oid = DECL_ASSEMBLER_NAME (fndecl);
3984 oname = IDENTIFIER_POINTER (oid);
3985 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3986 nname = ACONCAT (("_ZGA", oname+2, NULL));
3987 nid = get_identifier (nname);
3989 alias = make_alias_for (fndecl, nid);
3990 TREE_PUBLIC (alias) = 1;
3991 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3993 assemble_alias (alias, oid);
3998 /* Return C++ property of T, based on given operation OP. */
4000 static int
4001 cpp_check (tree t, cpp_operation op)
4003 switch (op)
4005 case IS_ABSTRACT:
4006 return DECL_PURE_VIRTUAL_P (t);
4007 case IS_CONSTRUCTOR:
4008 return DECL_CONSTRUCTOR_P (t);
4009 case IS_DESTRUCTOR:
4010 return DECL_DESTRUCTOR_P (t);
4011 case IS_COPY_CONSTRUCTOR:
4012 return DECL_COPY_CONSTRUCTOR_P (t);
4013 case IS_TEMPLATE:
4014 return TREE_CODE (t) == TEMPLATE_DECL;
4015 case IS_TRIVIAL:
4016 return trivial_type_p (t);
4017 default:
4018 return 0;
4022 /* Collect source file references recursively, starting from NAMESPC. */
4024 static void
4025 collect_source_refs (tree namespc)
4027 tree t;
4029 if (!namespc)
4030 return;
4032 /* Iterate over names in this name space. */
4033 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4034 if (!DECL_IS_BUILTIN (t) )
4035 collect_source_ref (DECL_SOURCE_FILE (t));
4037 /* Dump siblings, if any */
4038 collect_source_refs (TREE_CHAIN (namespc));
4040 /* Dump children, if any */
4041 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4044 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4045 starting from NAMESPC. */
4047 static void
4048 collect_ada_namespace (tree namespc, const char *source_file)
4050 if (!namespc)
4051 return;
4053 /* Collect decls from this namespace */
4054 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4056 /* Collect siblings, if any */
4057 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4059 /* Collect children, if any */
4060 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4063 /* Returns true iff there is a definition available for variable or
4064 function DECL. */
4066 static bool
4067 decl_defined_p (tree decl)
4069 if (TREE_CODE (decl) == FUNCTION_DECL)
4070 return (DECL_INITIAL (decl) != NULL_TREE);
4071 else
4073 gcc_assert (VAR_P (decl));
4074 return !DECL_EXTERNAL (decl);
4078 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4080 [expr.const]
4082 An integral constant-expression can only involve ... const
4083 variables of integral or enumeration types initialized with
4084 constant expressions ...
4086 C++0x also allows constexpr variables and temporaries initialized
4087 with constant expressions. We handle the former here, but the latter
4088 are just folded away in cxx_eval_constant_expression.
4090 The standard does not require that the expression be non-volatile.
4091 G++ implements the proposed correction in DR 457. */
4093 bool
4094 decl_constant_var_p (tree decl)
4096 if (!decl_maybe_constant_var_p (decl))
4097 return false;
4099 /* We don't know if a template static data member is initialized with
4100 a constant expression until we instantiate its initializer. Even
4101 in the case of a constexpr variable, we can't treat it as a
4102 constant until its initializer is complete in case it's used in
4103 its own initializer. */
4104 mark_used (decl);
4105 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4108 /* Returns true if DECL could be a symbolic constant variable, depending on
4109 its initializer. */
4111 bool
4112 decl_maybe_constant_var_p (tree decl)
4114 tree type = TREE_TYPE (decl);
4115 if (!VAR_P (decl))
4116 return false;
4117 if (DECL_DECLARED_CONSTEXPR_P (decl))
4118 return true;
4119 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4120 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4123 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4124 called from grokfndecl and grokvardecl; in all modes it is called from
4125 cp_write_global_declarations. */
4127 void
4128 no_linkage_error (tree decl)
4130 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4131 /* In C++11 it's ok if the decl is defined. */
4132 return;
4133 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4134 if (t == NULL_TREE)
4135 /* The type that got us on no_linkage_decls must have gotten a name for
4136 linkage purposes. */;
4137 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4138 /* The type might end up having a typedef name for linkage purposes. */
4139 vec_safe_push (no_linkage_decls, decl);
4140 else if (TYPE_ANONYMOUS_P (t))
4142 bool d = false;
4143 if (cxx_dialect >= cxx11)
4144 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4145 "anonymous type, is used but never defined", decl);
4146 else if (DECL_EXTERN_C_P (decl))
4147 /* Allow this; it's pretty common in C. */;
4148 else if (TREE_CODE (decl) == VAR_DECL)
4149 /* DRs 132, 319 and 389 seem to indicate types with
4150 no linkage can only be used to declare extern "C"
4151 entities. Since it's not always an error in the
4152 ISO C++ 90 Standard, we only issue a warning. */
4153 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4154 "with no linkage used to declare variable %q#D with "
4155 "linkage", decl);
4156 else
4157 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4158 "linkage used to declare function %q#D with linkage",
4159 decl);
4160 if (d && is_typedef_decl (TYPE_NAME (t)))
4161 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4162 "to the unqualified type, so it is not used for linkage",
4163 TYPE_NAME (t));
4165 else if (cxx_dialect >= cxx11)
4166 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4167 "%qT, is used but never defined", decl, t);
4168 else if (TREE_CODE (decl) == VAR_DECL)
4169 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4170 "used to declare variable %q#D with linkage", t, decl);
4171 else
4172 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4173 "to declare function %q#D with linkage", t, decl);
4176 /* Clear the list of deferred functions. */
4178 void
4179 cp_clear_deferred_fns (void)
4181 vec_free (deferred_fns);
4182 deferred_fns = NULL;
4183 keyed_classes = NULL;
4184 vec_free (no_linkage_decls);
4185 no_linkage_decls = NULL;
4186 cp_clear_constexpr_hashtable ();
4187 clear_pending_templates ();
4188 reset_anon_name ();
4189 reset_temp_count ();
4192 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4194 static void
4195 collect_all_refs (const char *source_file)
4197 collect_ada_namespace (global_namespace, source_file);
4200 /* Clear DECL_EXTERNAL for NODE. */
4202 static bool
4203 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4205 DECL_EXTERNAL (node->decl) = 0;
4206 return false;
4209 /* Build up the function to run dynamic initializers for thread_local
4210 variables in this translation unit and alias the init functions for the
4211 individual variables to it. */
4213 static void
4214 handle_tls_init (void)
4216 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4217 if (vars == NULL_TREE)
4218 return;
4220 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4222 write_out_vars (vars);
4224 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4225 boolean_type_node);
4226 TREE_PUBLIC (guard) = false;
4227 TREE_STATIC (guard) = true;
4228 DECL_ARTIFICIAL (guard) = true;
4229 DECL_IGNORED_P (guard) = true;
4230 TREE_USED (guard) = true;
4231 set_decl_tls_model (guard, decl_default_tls_model (guard));
4232 pushdecl_top_level_and_finish (guard, NULL_TREE);
4234 tree fn = get_local_tls_init_fn ();
4235 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4236 tree body = begin_function_body ();
4237 tree if_stmt = begin_if_stmt ();
4238 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4239 tf_warning_or_error);
4240 finish_if_stmt_cond (cond, if_stmt);
4241 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4242 tf_warning_or_error));
4243 for (; vars; vars = TREE_CHAIN (vars))
4245 tree var = TREE_VALUE (vars);
4246 tree init = TREE_PURPOSE (vars);
4247 one_static_initialization_or_destruction (var, init, true);
4249 #ifdef ASM_OUTPUT_DEF
4250 /* Output init aliases even with -fno-extern-tls-init. */
4251 if (TREE_PUBLIC (var))
4253 tree single_init_fn = get_tls_init_fn (var);
4254 if (single_init_fn == NULL_TREE)
4255 continue;
4256 cgraph_node *alias
4257 = cgraph_node::get_create (fn)->create_same_body_alias
4258 (single_init_fn, fn);
4259 gcc_assert (alias != NULL);
4261 #endif
4264 finish_then_clause (if_stmt);
4265 finish_if_stmt (if_stmt);
4266 finish_function_body (body);
4267 expand_or_defer_fn (finish_function (0));
4270 /* The entire file is now complete. If requested, dump everything
4271 to a file. */
4273 static void
4274 dump_tu (void)
4276 int flags;
4277 FILE *stream = dump_begin (TDI_tu, &flags);
4279 if (stream)
4281 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4282 dump_end (TDI_tu, stream);
4286 /* This routine is called at the end of compilation.
4287 Its job is to create all the code needed to initialize and
4288 destroy the global aggregates. We do the destruction
4289 first, since that way we only need to reverse the decls once. */
4291 void
4292 cp_process_pending_declarations (location_t locus)
4294 tree vars, decl;
4295 bool reconsider;
4296 size_t i;
4297 unsigned ssdf_count = 0;
4298 int retries = 0;
4300 timevar_start (TV_PHASE_DEFERRED);
4305 tree t;
4306 tree decl;
4308 reconsider = false;
4310 /* If there are templates that we've put off instantiating, do
4311 them now. */
4312 instantiate_pending_templates (retries);
4313 ggc_collect ();
4315 /* Write out virtual tables as required. Note that writing out
4316 the virtual table for a template class may cause the
4317 instantiation of members of that class. If we write out
4318 vtables then we remove the class from our list so we don't
4319 have to look at it again. */
4321 while (keyed_classes != NULL_TREE
4322 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4324 reconsider = true;
4325 keyed_classes = TREE_CHAIN (keyed_classes);
4328 t = keyed_classes;
4329 if (t != NULL_TREE)
4331 tree next = TREE_CHAIN (t);
4333 while (next)
4335 if (maybe_emit_vtables (TREE_VALUE (next)))
4337 reconsider = true;
4338 TREE_CHAIN (t) = TREE_CHAIN (next);
4340 else
4341 t = next;
4343 next = TREE_CHAIN (t);
4347 /* Write out needed type info variables. We have to be careful
4348 looping through unemitted decls, because emit_tinfo_decl may
4349 cause other variables to be needed. New elements will be
4350 appended, and we remove from the vector those that actually
4351 get emitted. */
4352 for (i = unemitted_tinfo_decls->length ();
4353 unemitted_tinfo_decls->iterate (--i, &t);)
4354 if (emit_tinfo_decl (t))
4356 reconsider = true;
4357 unemitted_tinfo_decls->unordered_remove (i);
4360 /* The list of objects with static storage duration is built up
4361 in reverse order. We clear STATIC_AGGREGATES so that any new
4362 aggregates added during the initialization of these will be
4363 initialized in the correct order when we next come around the
4364 loop. */
4365 vars = prune_vars_needing_no_initialization (&static_aggregates);
4367 if (vars)
4369 /* We need to start a new initialization function each time
4370 through the loop. That's because we need to know which
4371 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4372 isn't computed until a function is finished, and written
4373 out. That's a deficiency in the back end. When this is
4374 fixed, these initialization functions could all become
4375 inline, with resulting performance improvements. */
4376 tree ssdf_body;
4378 /* Set the line and file, so that it is obviously not from
4379 the source file. */
4380 input_location = locus;
4381 ssdf_body = start_static_storage_duration_function (ssdf_count);
4383 /* Make sure the back end knows about all the variables. */
4384 write_out_vars (vars);
4386 /* First generate code to do all the initializations. */
4387 if (vars)
4388 do_static_initialization_or_destruction (vars, /*initp=*/true);
4390 /* Then, generate code to do all the destructions. Do these
4391 in reverse order so that the most recently constructed
4392 variable is the first destroyed. If we're using
4393 __cxa_atexit, then we don't need to do this; functions
4394 were registered at initialization time to destroy the
4395 local statics. */
4396 if (!flag_use_cxa_atexit && vars)
4398 vars = nreverse (vars);
4399 do_static_initialization_or_destruction (vars, /*initp=*/false);
4401 else
4402 vars = NULL_TREE;
4404 /* Finish up the static storage duration function for this
4405 round. */
4406 input_location = locus;
4407 finish_static_storage_duration_function (ssdf_body);
4409 /* All those initializations and finalizations might cause
4410 us to need more inline functions, more template
4411 instantiations, etc. */
4412 reconsider = true;
4413 ssdf_count++;
4414 /* ??? was: locus.line++; */
4417 /* Now do the same for thread_local variables. */
4418 handle_tls_init ();
4420 /* Go through the set of inline functions whose bodies have not
4421 been emitted yet. If out-of-line copies of these functions
4422 are required, emit them. */
4423 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4425 /* Does it need synthesizing? */
4426 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4427 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4429 /* Even though we're already at the top-level, we push
4430 there again. That way, when we pop back a few lines
4431 hence, all of our state is restored. Otherwise,
4432 finish_function doesn't clean things up, and we end
4433 up with CURRENT_FUNCTION_DECL set. */
4434 push_to_top_level ();
4435 /* The decl's location will mark where it was first
4436 needed. Save that so synthesize method can indicate
4437 where it was needed from, in case of error */
4438 input_location = DECL_SOURCE_LOCATION (decl);
4439 synthesize_method (decl);
4440 pop_from_top_level ();
4441 reconsider = true;
4444 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4445 generate_tls_wrapper (decl);
4447 if (!DECL_SAVED_TREE (decl))
4448 continue;
4450 /* We lie to the back end, pretending that some functions
4451 are not defined when they really are. This keeps these
4452 functions from being put out unnecessarily. But, we must
4453 stop lying when the functions are referenced, or if they
4454 are not comdat since they need to be put out now. If
4455 DECL_INTERFACE_KNOWN, then we have already set
4456 DECL_EXTERNAL appropriately, so there's no need to check
4457 again, and we do not want to clear DECL_EXTERNAL if a
4458 previous call to import_export_decl set it.
4460 This is done in a separate for cycle, because if some
4461 deferred function is contained in another deferred
4462 function later in deferred_fns varray,
4463 rest_of_compilation would skip this function and we
4464 really cannot expand the same function twice. */
4465 import_export_decl (decl);
4466 if (DECL_NOT_REALLY_EXTERN (decl)
4467 && DECL_INITIAL (decl)
4468 && decl_needed_p (decl))
4470 struct cgraph_node *node, *next;
4472 node = cgraph_node::get (decl);
4473 if (node->cpp_implicit_alias)
4474 node = node->get_alias_target ();
4476 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4477 NULL, true);
4478 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4479 group, we need to mark all symbols in the same comdat group
4480 that way. */
4481 if (node->same_comdat_group)
4482 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4483 next != node;
4484 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4485 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4486 NULL, true);
4489 /* If we're going to need to write this function out, and
4490 there's already a body for it, create RTL for it now.
4491 (There might be no body if this is a method we haven't
4492 gotten around to synthesizing yet.) */
4493 if (!DECL_EXTERNAL (decl)
4494 && decl_needed_p (decl)
4495 && !TREE_ASM_WRITTEN (decl)
4496 && !cgraph_node::get (decl)->definition)
4498 /* We will output the function; no longer consider it in this
4499 loop. */
4500 DECL_DEFER_OUTPUT (decl) = 0;
4501 /* Generate RTL for this function now that we know we
4502 need it. */
4503 expand_or_defer_fn (decl);
4504 /* If we're compiling -fsyntax-only pretend that this
4505 function has been written out so that we don't try to
4506 expand it again. */
4507 if (flag_syntax_only)
4508 TREE_ASM_WRITTEN (decl) = 1;
4509 reconsider = true;
4513 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4514 reconsider = true;
4516 /* Static data members are just like namespace-scope globals. */
4517 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4519 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4520 /* Don't write it out if we haven't seen a definition. */
4521 || DECL_IN_AGGR_P (decl))
4522 continue;
4523 import_export_decl (decl);
4524 /* If this static data member is needed, provide it to the
4525 back end. */
4526 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4527 DECL_EXTERNAL (decl) = 0;
4529 if (vec_safe_length (pending_statics) != 0
4530 && wrapup_global_declarations (pending_statics->address (),
4531 pending_statics->length ()))
4532 reconsider = true;
4534 retries++;
4536 while (reconsider);
4538 if (L_IPO_IS_AUXILIARY_MODULE)
4540 tree fndecl;
4541 int i;
4543 gcc_assert (flag_dyn_ipa && L_IPO_COMP_MODE);
4545 /* Do some cleanup -- we do not really need static init function
4546 to be created for auxiliary modules -- they are created to keep
4547 funcdef_no consistent between profile use and profile gen. */
4548 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4549 /* Such ssdf_decls are not called from GLOBAL ctor/dtor, mark
4550 them reachable to avoid being eliminated too early before
4551 gimplication. */
4552 cgraph_enqueue_node (cgraph_node::get_create (fndecl));
4553 ssdf_decls = NULL;
4554 timevar_stop (TV_PHASE_DEFERRED);
4555 return;
4558 /* All used inline functions must have a definition at this point. */
4559 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4561 if (/* Check online inline functions that were actually used. */
4562 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4563 /* If the definition actually was available here, then the
4564 fact that the function was not defined merely represents
4565 that for some reason (use of a template repository,
4566 #pragma interface, etc.) we decided not to emit the
4567 definition here. */
4568 && !DECL_INITIAL (decl)
4569 /* Don't complain if the template was defined. */
4570 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4571 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4572 (template_for_substitution (decl)))))
4574 warning (0, "inline function %q+D used but never defined", decl);
4575 /* Avoid a duplicate warning from check_global_declaration_1. */
4576 TREE_NO_WARNING (decl) = 1;
4580 /* So must decls that use a type with no linkage. */
4581 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4582 no_linkage_error (decl);
4584 /* Then, do the Objective-C stuff. This is where all the
4585 Objective-C module stuff gets generated (symtab,
4586 class/protocol/selector lists etc). This must be done after C++
4587 templates, destructors etc. so that selectors used in C++
4588 templates are properly allocated. */
4589 if (c_dialect_objc ())
4590 objc_write_global_declarations ();
4592 /* We give C linkage to static constructors and destructors. */
4593 push_lang_context (lang_name_c);
4595 /* Generate initialization and destruction functions for all
4596 priorities for which they are required. */
4597 if (priority_info_map)
4598 splay_tree_foreach (priority_info_map,
4599 generate_ctor_and_dtor_functions_for_priority,
4600 /*data=*/&locus);
4601 else if (c_dialect_objc () && objc_static_init_needed_p ())
4602 /* If this is obj-c++ and we need a static init, call
4603 generate_ctor_or_dtor_function. */
4604 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4605 DEFAULT_INIT_PRIORITY, &locus);
4607 /* We're done with the splay-tree now. */
4608 if (priority_info_map)
4610 splay_tree_delete (priority_info_map);
4611 priority_info_map = NULL;
4614 /* Generate any missing aliases. */
4615 maybe_apply_pending_pragma_weaks ();
4617 /* We're done with static constructors, so we can go back to "C++"
4618 linkage now. */
4619 pop_lang_context ();
4621 ssdf_decls = NULL;
4622 timevar_stop (TV_PHASE_DEFERRED);
4625 /* This routine is called at the end of compilation.
4626 Its job is to create all the code needed to initialize and
4627 destroy the global aggregates. We do the destruction
4628 first, since that way we only need to reverse the decls once. */
4630 void
4631 cp_write_global_declarations (void)
4633 bool reconsider = false;
4634 location_t locus;
4635 hash_set<tree> *candidates;
4637 locus = input_location;
4638 at_eof = 1;
4640 /* Bad parse errors. Just forget about it. */
4641 if (! global_bindings_p () || current_class_type
4642 || !vec_safe_is_empty (decl_namespace_list))
4643 return;
4645 if (pch_file)
4647 c_common_write_pch ();
4648 return;
4651 cgraph_process_same_body_aliases ();
4653 /* Handle -fdump-ada-spec[-slim] */
4654 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4656 if (flag_dump_ada_spec_slim)
4657 collect_source_ref (main_input_filename);
4658 else
4659 collect_source_refs (global_namespace);
4661 dump_ada_specs (collect_all_refs, cpp_check);
4664 /* FIXME - huh? was input_line -= 1;*/
4666 /* We now have to write out all the stuff we put off writing out.
4667 These include:
4669 o Template specializations that we have not yet instantiated,
4670 but which are needed.
4671 o Initialization and destruction for non-local objects with
4672 static storage duration. (Local objects with static storage
4673 duration are initialized when their scope is first entered,
4674 and are cleaned up via atexit.)
4675 o Virtual function tables.
4677 All of these may cause others to be needed. For example,
4678 instantiating one function may cause another to be needed, and
4679 generating the initializer for an object may cause templates to be
4680 instantiated, etc., etc. */
4682 emit_support_tinfos ();
4684 if (!L_IPO_COMP_MODE)
4685 cp_process_pending_declarations (locus);
4687 /* Collect candidates for Java hidden aliases. */
4688 candidates = collect_candidates_for_java_method_aliases ();
4691 timevar_start (TV_PHASE_OPT_GEN);
4693 if (flag_vtable_verify)
4695 vtv_recover_class_info ();
4696 vtv_compute_class_hierarchy_transitive_closure ();
4697 vtv_build_vtable_verify_fndecl ();
4700 finalize_compilation_unit ();
4702 if (flag_vtable_verify)
4704 /* Generate the special constructor initialization function that
4705 calls __VLTRegisterPairs, and give it a very high
4706 initialization priority. This must be done after
4707 finalize_compilation_unit so that we have accurate
4708 information about which vtable will actually be emitted. */
4709 vtv_generate_init_routine ();
4712 timevar_stop (TV_PHASE_OPT_GEN);
4713 timevar_start (TV_PHASE_CHECK_DBGINFO);
4715 /* Now, issue warnings about static, but not defined, functions,
4716 etc., and emit debugging information. */
4717 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4718 if (vec_safe_length (pending_statics) != 0)
4720 check_global_declarations (pending_statics->address (),
4721 pending_statics->length ());
4722 emit_debug_global_declarations (pending_statics->address (),
4723 pending_statics->length ());
4726 perform_deferred_noexcept_checks ();
4728 /* Generate hidden aliases for Java. */
4729 if (candidates)
4731 build_java_method_aliases (candidates);
4732 delete candidates;
4735 finish_repo ();
4737 /* The entire file is now complete. If requested, dump everything
4738 to a file. */
4739 dump_tu ();
4741 if (flag_detailed_statistics)
4743 dump_tree_statistics ();
4744 dump_time_statistics ();
4746 input_location = locus;
4748 #ifdef ENABLE_CHECKING
4749 validate_conversion_obstack ();
4750 #endif /* ENABLE_CHECKING */
4752 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4755 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4756 function to call in parse-tree form; it has not yet been
4757 semantically analyzed. ARGS are the arguments to the function.
4758 They have already been semantically analyzed. This may change
4759 ARGS. */
4761 tree
4762 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4763 tsubst_flags_t complain)
4765 tree orig_fn;
4766 vec<tree, va_gc> *orig_args = NULL;
4767 tree expr;
4768 tree object;
4770 orig_fn = fn;
4771 object = TREE_OPERAND (fn, 0);
4773 if (processing_template_decl)
4775 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4776 || TREE_CODE (fn) == MEMBER_REF);
4777 if (type_dependent_expression_p (fn)
4778 || any_type_dependent_arguments_p (*args))
4779 return build_nt_call_vec (fn, *args);
4781 orig_args = make_tree_vector_copy (*args);
4783 /* Transform the arguments and add the implicit "this"
4784 parameter. That must be done before the FN is transformed
4785 because we depend on the form of FN. */
4786 make_args_non_dependent (*args);
4787 object = build_non_dependent_expr (object);
4788 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4790 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4791 object = cp_build_addr_expr (object, complain);
4792 vec_safe_insert (*args, 0, object);
4794 /* Now that the arguments are done, transform FN. */
4795 fn = build_non_dependent_expr (fn);
4798 /* A qualified name corresponding to a bound pointer-to-member is
4799 represented as an OFFSET_REF:
4801 struct B { void g(); };
4802 void (B::*p)();
4803 void B::g() { (this->*p)(); } */
4804 if (TREE_CODE (fn) == OFFSET_REF)
4806 tree object_addr = cp_build_addr_expr (object, complain);
4807 fn = TREE_OPERAND (fn, 1);
4808 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4809 complain);
4810 vec_safe_insert (*args, 0, object_addr);
4813 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4814 expr = build_op_call (fn, args, complain);
4815 else
4816 expr = cp_build_function_call_vec (fn, args, complain);
4817 if (processing_template_decl && expr != error_mark_node)
4818 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4820 if (orig_args != NULL)
4821 release_tree_vector (orig_args);
4823 return expr;
4827 void
4828 check_default_args (tree x)
4830 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4831 bool saw_def = false;
4832 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4833 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4835 if (TREE_PURPOSE (arg))
4836 saw_def = true;
4837 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4839 error ("default argument missing for parameter %P of %q+#D", i, x);
4840 TREE_PURPOSE (arg) = error_mark_node;
4845 /* Return true if function DECL can be inlined. This is used to force
4846 instantiation of methods that might be interesting for inlining. */
4847 bool
4848 possibly_inlined_p (tree decl)
4850 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4851 if (DECL_UNINLINABLE (decl))
4852 return false;
4853 if (!optimize || pragma_java_exceptions)
4854 return DECL_DECLARED_INLINE_P (decl);
4855 /* When optimizing, we might inline everything when flatten
4856 attribute or heuristics inlining for size or autoinlining
4857 is used. */
4858 return true;
4861 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4862 If DECL is a specialization or implicitly declared class member,
4863 generate the actual definition. Return false if something goes
4864 wrong, true otherwise. */
4866 bool
4867 mark_used (tree decl, tsubst_flags_t complain)
4869 /* If DECL is a BASELINK for a single function, then treat it just
4870 like the DECL for the function. Otherwise, if the BASELINK is
4871 for an overloaded function, we don't know which function was
4872 actually used until after overload resolution. */
4873 if (BASELINK_P (decl))
4875 decl = BASELINK_FUNCTIONS (decl);
4876 if (really_overloaded_fn (decl))
4877 return true;
4878 decl = OVL_CURRENT (decl);
4881 /* Set TREE_USED for the benefit of -Wunused. */
4882 TREE_USED (decl) = 1;
4883 if (DECL_CLONED_FUNCTION_P (decl))
4884 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4886 /* Mark enumeration types as used. */
4887 if (TREE_CODE (decl) == CONST_DECL)
4888 used_types_insert (DECL_CONTEXT (decl));
4890 if (TREE_CODE (decl) == FUNCTION_DECL)
4891 maybe_instantiate_noexcept (decl);
4893 if (TREE_CODE (decl) == FUNCTION_DECL
4894 && DECL_DELETED_FN (decl))
4896 if (DECL_ARTIFICIAL (decl))
4898 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4899 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4901 /* We mark a lambda conversion op as deleted if we can't
4902 generate it properly; see maybe_add_lambda_conv_op. */
4903 sorry ("converting lambda which uses %<...%> to "
4904 "function pointer");
4905 return false;
4908 if (complain & tf_error)
4910 error ("use of deleted function %qD", decl);
4911 if (!maybe_explain_implicit_delete (decl))
4912 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4914 return false;
4917 /* We can only check DECL_ODR_USED on variables or functions with
4918 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4919 might need special handling for. */
4920 if (!VAR_OR_FUNCTION_DECL_P (decl)
4921 || DECL_LANG_SPECIFIC (decl) == NULL
4922 || DECL_THUNK_P (decl))
4924 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4926 if (complain & tf_error)
4927 error ("use of %qD before deduction of %<auto%>", decl);
4928 return false;
4930 return true;
4933 /* We only want to do this processing once. We don't need to keep trying
4934 to instantiate inline templates, because unit-at-a-time will make sure
4935 we get them compiled before functions that want to inline them. */
4936 if (DECL_ODR_USED (decl))
4937 return true;
4939 /* If within finish_function, defer the rest until that function
4940 finishes, otherwise it might recurse. */
4941 if (defer_mark_used_calls)
4943 vec_safe_push (deferred_mark_used_calls, decl);
4944 return true;
4947 /* Normally, we can wait until instantiation-time to synthesize DECL.
4948 However, if DECL is a static data member initialized with a constant
4949 or a constexpr function, we need it right now because a reference to
4950 such a data member or a call to such function is not value-dependent.
4951 For a function that uses auto in the return type, we need to instantiate
4952 it to find out its type. For OpenMP user defined reductions, we need
4953 them instantiated for reduction clauses which inline them by hand
4954 directly. */
4955 if (DECL_LANG_SPECIFIC (decl)
4956 && DECL_TEMPLATE_INFO (decl)
4957 && (decl_maybe_constant_var_p (decl)
4958 || (TREE_CODE (decl) == FUNCTION_DECL
4959 && (DECL_DECLARED_CONSTEXPR_P (decl)
4960 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4961 || undeduced_auto_decl (decl))
4962 && !uses_template_parms (DECL_TI_ARGS (decl)))
4964 /* Instantiating a function will result in garbage collection. We
4965 must treat this situation as if we were within the body of a
4966 function so as to avoid collecting live data only referenced from
4967 the stack (such as overload resolution candidates). */
4968 ++function_depth;
4969 instantiate_decl (decl, /*defer_ok=*/false,
4970 /*expl_inst_class_mem_p=*/false);
4971 --function_depth;
4974 if (processing_template_decl)
4975 return true;
4977 /* Check this too in case we're within fold_non_dependent_expr. */
4978 if (DECL_TEMPLATE_INFO (decl)
4979 && uses_template_parms (DECL_TI_ARGS (decl)))
4980 return true;
4982 require_deduced_type (decl);
4984 /* If we don't need a value, then we don't need to synthesize DECL. */
4985 if (cp_unevaluated_operand != 0)
4986 return true;
4988 DECL_ODR_USED (decl) = 1;
4989 if (DECL_CLONED_FUNCTION_P (decl))
4990 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4992 /* DR 757: A type without linkage shall not be used as the type of a
4993 variable or function with linkage, unless
4994 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4995 o the variable or function is not used (3.2 [basic.def.odr]) or is
4996 defined in the same translation unit. */
4997 if (cxx_dialect > cxx98
4998 && decl_linkage (decl) != lk_none
4999 && !DECL_EXTERN_C_P (decl)
5000 && !DECL_ARTIFICIAL (decl)
5001 && !decl_defined_p (decl)
5002 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5004 if (is_local_extern (decl))
5005 /* There's no way to define a local extern, and adding it to
5006 the vector interferes with GC, so give an error now. */
5007 no_linkage_error (decl);
5008 else
5009 vec_safe_push (no_linkage_decls, decl);
5012 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5013 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5014 /* Remember it, so we can check it was defined. */
5015 note_vague_linkage_fn (decl);
5017 /* Is it a synthesized method that needs to be synthesized? */
5018 if (TREE_CODE (decl) == FUNCTION_DECL
5019 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5020 && DECL_DEFAULTED_FN (decl)
5021 /* A function defaulted outside the class is synthesized either by
5022 cp_finish_decl or instantiate_decl. */
5023 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5024 && ! DECL_INITIAL (decl))
5026 /* Defer virtual destructors so that thunks get the right
5027 linkage. */
5028 if (DECL_VIRTUAL_P (decl) && !at_eof)
5030 note_vague_linkage_fn (decl);
5031 return true;
5034 /* Remember the current location for a function we will end up
5035 synthesizing. Then we can inform the user where it was
5036 required in the case of error. */
5037 DECL_SOURCE_LOCATION (decl) = input_location;
5039 /* Synthesizing an implicitly defined member function will result in
5040 garbage collection. We must treat this situation as if we were
5041 within the body of a function so as to avoid collecting live data
5042 on the stack (such as overload resolution candidates).
5044 We could just let cp_write_global_declarations handle synthesizing
5045 this function by adding it to deferred_fns, but doing
5046 it at the use site produces better error messages. */
5047 ++function_depth;
5048 synthesize_method (decl);
5049 --function_depth;
5050 /* If this is a synthesized method we don't need to
5051 do the instantiation test below. */
5053 else if (VAR_OR_FUNCTION_DECL_P (decl)
5054 && DECL_TEMPLATE_INFO (decl)
5055 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5056 || always_instantiate_p (decl)))
5057 /* If this is a function or variable that is an instance of some
5058 template, we now know that we will need to actually do the
5059 instantiation. We check that DECL is not an explicit
5060 instantiation because that is not checked in instantiate_decl.
5062 We put off instantiating functions in order to improve compile
5063 times. Maintaining a stack of active functions is expensive,
5064 and the inliner knows to instantiate any functions it might
5065 need. Therefore, we always try to defer instantiation. */
5067 ++function_depth;
5068 instantiate_decl (decl, /*defer_ok=*/true,
5069 /*expl_inst_class_mem_p=*/false);
5070 --function_depth;
5073 return true;
5076 bool
5077 mark_used (tree decl)
5079 return mark_used (decl, tf_warning_or_error);
5082 tree
5083 vtv_start_verification_constructor_init_function (void)
5085 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5088 tree
5089 vtv_finish_verification_constructor_init_function (tree function_body)
5091 tree fn;
5093 finish_compound_stmt (function_body);
5094 fn = finish_function (0);
5095 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5096 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5098 return fn;
5101 #include "gt-cp-decl2.h"