* ipa-devirt.c: Include gimple-pretty-print.h
[official-gcc.git] / gcc / cp / decl2.c
blob1740a2e7340acf474e8c25c32decb0ebc24c899a
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 "flags.h"
40 #include "cp-tree.h"
41 #include "decl.h"
42 #include "toplev.h"
43 #include "timevar.h"
44 #include "cpplib.h"
45 #include "target.h"
46 #include "c-family/c-common.h"
47 #include "c-family/c-objc.h"
48 #include "cgraph.h"
49 #include "tree-inline.h"
50 #include "c-family/c-pragma.h"
51 #include "dumpfile.h"
52 #include "intl.h"
53 #include "splay-tree.h"
54 #include "langhooks.h"
55 #include "c-family/c-ada-spec.h"
56 #include "asan.h"
58 extern cpp_reader *parse_in;
60 /* This structure contains information about the initializations
61 and/or destructions required for a particular priority level. */
62 typedef struct priority_info_s {
63 /* Nonzero if there have been any initializations at this priority
64 throughout the translation unit. */
65 int initializations_p;
66 /* Nonzero if there have been any destructions at this priority
67 throughout the translation unit. */
68 int destructions_p;
69 } *priority_info;
71 static void mark_vtable_entries (tree);
72 static bool maybe_emit_vtables (tree);
73 static bool acceptable_java_type (tree);
74 static tree start_objects (int, int);
75 static void finish_objects (int, int, tree);
76 static tree start_static_storage_duration_function (unsigned);
77 static void finish_static_storage_duration_function (tree);
78 static priority_info get_priority_info (int);
79 static void do_static_initialization_or_destruction (tree, bool);
80 static void one_static_initialization_or_destruction (tree, tree, bool);
81 static void generate_ctor_or_dtor_function (bool, int, location_t *);
82 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
83 void *);
84 static tree prune_vars_needing_no_initialization (tree *);
85 static void write_out_vars (tree);
86 static void import_export_class (tree);
87 static tree get_guard_bits (tree);
88 static void determine_visibility_from_class (tree, tree);
89 static bool determine_hidden_inline (tree);
90 static bool decl_defined_p (tree);
92 /* A list of static class variables. This is needed, because a
93 static class variable can be declared inside the class without
94 an initializer, and then initialized, statically, outside the class. */
95 static GTY(()) vec<tree, va_gc> *pending_statics;
97 /* A list of functions which were declared inline, but which we
98 may need to emit outline anyway. */
99 static GTY(()) vec<tree, va_gc> *deferred_fns;
101 /* A list of decls that use types with no linkage, which we need to make
102 sure are defined. */
103 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
105 /* Nonzero if we're done parsing and into end-of-file activities. */
107 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 if (variable_template_p (tmpl))
527 /* OK */;
528 else
529 error ("template declaration of %q#D", decl);
532 /* Return true iff TYPE is a valid Java parameter or return type. */
534 static bool
535 acceptable_java_type (tree type)
537 if (type == error_mark_node)
538 return false;
540 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
541 return true;
542 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
544 type = TREE_TYPE (type);
545 if (TREE_CODE (type) == RECORD_TYPE)
547 tree args; int i;
548 if (! TYPE_FOR_JAVA (type))
549 return false;
550 if (! CLASSTYPE_TEMPLATE_INFO (type))
551 return true;
552 args = CLASSTYPE_TI_ARGS (type);
553 i = TREE_VEC_LENGTH (args);
554 while (--i >= 0)
556 type = TREE_VEC_ELT (args, i);
557 if (TYPE_PTR_P (type))
558 type = TREE_TYPE (type);
559 if (! TYPE_FOR_JAVA (type))
560 return false;
562 return true;
565 return false;
568 /* For a METHOD in a Java class CTYPE, return true if
569 the parameter and return types are valid Java types.
570 Otherwise, print appropriate error messages, and return false. */
572 bool
573 check_java_method (tree method)
575 bool jerr = false;
576 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
577 tree ret_type = TREE_TYPE (TREE_TYPE (method));
579 if (!acceptable_java_type (ret_type))
581 error ("Java method %qD has non-Java return type %qT",
582 method, ret_type);
583 jerr = true;
586 arg_types = TREE_CHAIN (arg_types);
587 if (DECL_HAS_IN_CHARGE_PARM_P (method))
588 arg_types = TREE_CHAIN (arg_types);
589 if (DECL_HAS_VTT_PARM_P (method))
590 arg_types = TREE_CHAIN (arg_types);
592 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
594 tree type = TREE_VALUE (arg_types);
595 if (!acceptable_java_type (type))
597 if (type != error_mark_node)
598 error ("Java method %qD has non-Java parameter type %qT",
599 method, type);
600 jerr = true;
603 return !jerr;
606 /* Sanity check: report error if this function FUNCTION is not
607 really a member of the class (CTYPE) it is supposed to belong to.
608 TEMPLATE_PARMS is used to specify the template parameters of a member
609 template passed as FUNCTION_DECL. If the member template is passed as a
610 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
611 from the declaration. If the function is not a function template, it
612 must be NULL.
613 It returns the original declaration for the function, NULL_TREE if
614 no declaration was found, error_mark_node if an error was emitted. */
616 tree
617 check_classfn (tree ctype, tree function, tree template_parms)
619 int ix;
620 bool is_template;
621 tree pushed_scope;
623 if (DECL_USE_TEMPLATE (function)
624 && !(TREE_CODE (function) == TEMPLATE_DECL
625 && DECL_TEMPLATE_SPECIALIZATION (function))
626 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
627 /* Since this is a specialization of a member template,
628 we're not going to find the declaration in the class.
629 For example, in:
631 struct S { template <typename T> void f(T); };
632 template <> void S::f(int);
634 we're not going to find `S::f(int)', but there's no
635 reason we should, either. We let our callers know we didn't
636 find the method, but we don't complain. */
637 return NULL_TREE;
639 /* Basic sanity check: for a template function, the template parameters
640 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
641 if (TREE_CODE (function) == TEMPLATE_DECL)
643 if (template_parms
644 && !comp_template_parms (template_parms,
645 DECL_TEMPLATE_PARMS (function)))
647 error ("template parameter lists provided don%'t match the "
648 "template parameters of %qD", function);
649 return error_mark_node;
651 template_parms = DECL_TEMPLATE_PARMS (function);
654 /* OK, is this a definition of a member template? */
655 is_template = (template_parms != NULL_TREE);
657 /* [temp.mem]
659 A destructor shall not be a member template. */
660 if (DECL_DESTRUCTOR_P (function) && is_template)
662 error ("destructor %qD declared as member template", function);
663 return error_mark_node;
666 /* We must enter the scope here, because conversion operators are
667 named by target type, and type equivalence relies on typenames
668 resolving within the scope of CTYPE. */
669 pushed_scope = push_scope (ctype);
670 ix = class_method_index_for_fn (complete_type (ctype), function);
671 if (ix >= 0)
673 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
674 tree fndecls, fndecl = 0;
675 bool is_conv_op;
676 const char *format = NULL;
678 for (fndecls = (*methods)[ix];
679 fndecls; fndecls = OVL_NEXT (fndecls))
681 tree p1, p2;
683 fndecl = OVL_CURRENT (fndecls);
684 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
685 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
687 /* We cannot simply call decls_match because this doesn't
688 work for static member functions that are pretending to
689 be methods, and because the name may have been changed by
690 asm("new_name"). */
692 /* Get rid of the this parameter on functions that become
693 static. */
694 if (DECL_STATIC_FUNCTION_P (fndecl)
695 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
696 p1 = TREE_CHAIN (p1);
698 /* A member template definition only matches a member template
699 declaration. */
700 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
701 continue;
703 /* ref-qualifier or absence of same must match. */
704 if (type_memfn_rqual (TREE_TYPE (function))
705 != type_memfn_rqual (TREE_TYPE (fndecl)))
706 continue;
708 /* While finding a match, same types and params are not enough
709 if the function is versioned. Also check version ("target")
710 attributes. */
711 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
712 TREE_TYPE (TREE_TYPE (fndecl)))
713 && compparms (p1, p2)
714 && !targetm.target_option.function_versions (function, fndecl)
715 && (!is_template
716 || comp_template_parms (template_parms,
717 DECL_TEMPLATE_PARMS (fndecl)))
718 && (DECL_TEMPLATE_SPECIALIZATION (function)
719 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
720 && (!DECL_TEMPLATE_SPECIALIZATION (function)
721 || (DECL_TI_TEMPLATE (function)
722 == DECL_TI_TEMPLATE (fndecl))))
723 break;
725 if (fndecls)
727 if (pushed_scope)
728 pop_scope (pushed_scope);
729 return OVL_CURRENT (fndecls);
732 error_at (DECL_SOURCE_LOCATION (function),
733 "prototype for %q#D does not match any in class %qT",
734 function, ctype);
735 is_conv_op = DECL_CONV_FN_P (fndecl);
737 if (is_conv_op)
738 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
739 fndecls = (*methods)[ix];
740 while (fndecls)
742 fndecl = OVL_CURRENT (fndecls);
743 fndecls = OVL_NEXT (fndecls);
745 if (!fndecls && is_conv_op)
747 if (methods->length () > (size_t) ++ix)
749 fndecls = (*methods)[ix];
750 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
752 fndecls = NULL_TREE;
753 is_conv_op = false;
756 else
757 is_conv_op = false;
759 if (format)
760 format = " %+#D";
761 else if (fndecls)
762 format = N_("candidates are: %+#D");
763 else
764 format = N_("candidate is: %+#D");
765 error (format, fndecl);
768 else if (!COMPLETE_TYPE_P (ctype))
769 cxx_incomplete_type_error (function, ctype);
770 else
771 error ("no %q#D member function declared in class %qT",
772 function, ctype);
774 if (pushed_scope)
775 pop_scope (pushed_scope);
776 return error_mark_node;
779 /* DECL is a function with vague linkage. Remember it so that at the
780 end of the translation unit we can decide whether or not to emit
781 it. */
783 void
784 note_vague_linkage_fn (tree decl)
786 DECL_DEFER_OUTPUT (decl) = 1;
787 vec_safe_push (deferred_fns, decl);
790 /* We have just processed the DECL, which is a static data member.
791 The other parameters are as for cp_finish_decl. */
793 void
794 finish_static_data_member_decl (tree decl,
795 tree init, bool init_const_expr_p,
796 tree asmspec_tree,
797 int flags)
799 DECL_CONTEXT (decl) = current_class_type;
801 /* We cannot call pushdecl here, because that would fill in the
802 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
803 the right thing, namely, to put this decl out straight away. */
805 if (! processing_template_decl)
806 vec_safe_push (pending_statics, decl);
808 if (LOCAL_CLASS_P (current_class_type)
809 /* We already complained about the template definition. */
810 && !DECL_TEMPLATE_INSTANTIATION (decl))
811 permerror (input_location, "local class %q#T shall not have static data member %q#D",
812 current_class_type, decl);
813 else
814 for (tree t = current_class_type; TYPE_P (t);
815 t = CP_TYPE_CONTEXT (t))
816 if (TYPE_ANONYMOUS_P (t))
818 if (permerror (DECL_SOURCE_LOCATION (decl),
819 "static data member %qD in unnamed class", decl))
820 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
821 "unnamed class defined here");
822 break;
825 DECL_IN_AGGR_P (decl) = 1;
827 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
828 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
829 SET_VAR_HAD_UNKNOWN_BOUND (decl);
831 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
834 /* DECLARATOR and DECLSPECS correspond to a class member. The other
835 parameters are as for cp_finish_decl. Return the DECL for the
836 class member declared. */
838 tree
839 grokfield (const cp_declarator *declarator,
840 cp_decl_specifier_seq *declspecs,
841 tree init, bool init_const_expr_p,
842 tree asmspec_tree,
843 tree attrlist)
845 tree value;
846 const char *asmspec = 0;
847 int flags;
848 tree name;
850 if (init
851 && TREE_CODE (init) == TREE_LIST
852 && TREE_VALUE (init) == error_mark_node
853 && TREE_CHAIN (init) == NULL_TREE)
854 init = NULL_TREE;
856 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
857 if (! value || value == error_mark_node)
858 /* friend or constructor went bad. */
859 return error_mark_node;
860 if (TREE_TYPE (value) == error_mark_node)
861 return value;
863 if (TREE_CODE (value) == TYPE_DECL && init)
865 error ("typedef %qD is initialized (use decltype instead)", value);
866 init = NULL_TREE;
869 /* Pass friendly classes back. */
870 if (value == void_type_node)
871 return value;
873 /* Pass friend decls back. */
874 if ((TREE_CODE (value) == FUNCTION_DECL
875 || TREE_CODE (value) == TEMPLATE_DECL)
876 && DECL_CONTEXT (value) != current_class_type)
877 return value;
879 name = DECL_NAME (value);
881 if (name != NULL_TREE)
883 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
885 error ("explicit template argument list not allowed");
886 return error_mark_node;
889 if (IDENTIFIER_POINTER (name)[0] == '_'
890 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
891 error ("member %qD conflicts with virtual function table field name",
892 value);
895 /* Stash away type declarations. */
896 if (TREE_CODE (value) == TYPE_DECL)
898 DECL_NONLOCAL (value) = 1;
899 DECL_CONTEXT (value) = current_class_type;
901 if (attrlist)
903 int attrflags = 0;
905 /* If this is a typedef that names the class for linkage purposes
906 (7.1.3p8), apply any attributes directly to the type. */
907 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
908 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
909 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
911 cplus_decl_attributes (&value, attrlist, attrflags);
914 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
915 && TREE_TYPE (value) != error_mark_node
916 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
917 set_underlying_type (value);
919 /* It's important that push_template_decl below follows
920 set_underlying_type above so that the created template
921 carries the properly set type of VALUE. */
922 if (processing_template_decl)
923 value = push_template_decl (value);
925 record_locally_defined_typedef (value);
926 return value;
929 if (DECL_IN_AGGR_P (value))
931 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
932 return void_type_node;
935 if (asmspec_tree && asmspec_tree != error_mark_node)
936 asmspec = TREE_STRING_POINTER (asmspec_tree);
938 if (init)
940 if (TREE_CODE (value) == FUNCTION_DECL)
942 /* Initializers for functions are rejected early in the parser.
943 If we get here, it must be a pure specifier for a method. */
944 if (init == ridpointers[(int)RID_DELETE])
946 DECL_DELETED_FN (value) = 1;
947 DECL_DECLARED_INLINE_P (value) = 1;
948 DECL_INITIAL (value) = error_mark_node;
950 else if (init == ridpointers[(int)RID_DEFAULT])
952 if (defaultable_fn_check (value))
954 DECL_DEFAULTED_FN (value) = 1;
955 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
956 DECL_DECLARED_INLINE_P (value) = 1;
959 else if (TREE_CODE (init) == DEFAULT_ARG)
960 error ("invalid initializer for member function %qD", value);
961 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
963 if (integer_zerop (init))
964 DECL_PURE_VIRTUAL_P (value) = 1;
965 else if (error_operand_p (init))
966 ; /* An error has already been reported. */
967 else
968 error ("invalid initializer for member function %qD",
969 value);
971 else
973 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
974 error ("initializer specified for static member function %qD",
975 value);
978 else if (TREE_CODE (value) == FIELD_DECL)
979 /* C++11 NSDMI, keep going. */;
980 else if (!VAR_P (value))
981 gcc_unreachable ();
984 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
986 value = push_template_decl (value);
987 if (error_operand_p (value))
988 return error_mark_node;
991 if (attrlist)
992 cplus_decl_attributes (&value, attrlist, 0);
994 if (init && DIRECT_LIST_INIT_P (init))
995 flags = LOOKUP_NORMAL;
996 else
997 flags = LOOKUP_IMPLICIT;
999 switch (TREE_CODE (value))
1001 case VAR_DECL:
1002 finish_static_data_member_decl (value, init, init_const_expr_p,
1003 asmspec_tree, flags);
1004 return value;
1006 case FIELD_DECL:
1007 if (asmspec)
1008 error ("%<asm%> specifiers are not permitted on non-static data members");
1009 if (DECL_INITIAL (value) == error_mark_node)
1010 init = error_mark_node;
1011 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1012 NULL_TREE, flags);
1013 DECL_IN_AGGR_P (value) = 1;
1014 return value;
1016 case FUNCTION_DECL:
1017 if (asmspec)
1018 set_user_assembler_name (value, asmspec);
1020 cp_finish_decl (value,
1021 /*init=*/NULL_TREE,
1022 /*init_const_expr_p=*/false,
1023 asmspec_tree, flags);
1025 /* Pass friends back this way. */
1026 if (DECL_FRIEND_P (value))
1027 return void_type_node;
1029 DECL_IN_AGGR_P (value) = 1;
1030 return value;
1032 default:
1033 gcc_unreachable ();
1035 return NULL_TREE;
1038 /* Like `grokfield', but for bitfields.
1039 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1041 tree
1042 grokbitfield (const cp_declarator *declarator,
1043 cp_decl_specifier_seq *declspecs, tree width,
1044 tree attrlist)
1046 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1048 if (value == error_mark_node)
1049 return NULL_TREE; /* friends went bad. */
1050 if (TREE_TYPE (value) == error_mark_node)
1051 return value;
1053 /* Pass friendly classes back. */
1054 if (VOID_TYPE_P (value))
1055 return void_type_node;
1057 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1058 && (POINTER_TYPE_P (value)
1059 || !dependent_type_p (TREE_TYPE (value))))
1061 error ("bit-field %qD with non-integral type", value);
1062 return error_mark_node;
1065 if (TREE_CODE (value) == TYPE_DECL)
1067 error ("cannot declare %qD to be a bit-field type", value);
1068 return NULL_TREE;
1071 /* Usually, finish_struct_1 catches bitfields with invalid types.
1072 But, in the case of bitfields with function type, we confuse
1073 ourselves into thinking they are member functions, so we must
1074 check here. */
1075 if (TREE_CODE (value) == FUNCTION_DECL)
1077 error ("cannot declare bit-field %qD with function type",
1078 DECL_NAME (value));
1079 return NULL_TREE;
1082 if (DECL_IN_AGGR_P (value))
1084 error ("%qD is already defined in the class %qT", value,
1085 DECL_CONTEXT (value));
1086 return void_type_node;
1089 if (TREE_STATIC (value))
1091 error ("static member %qD cannot be a bit-field", value);
1092 return NULL_TREE;
1094 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1096 if (width != error_mark_node)
1098 /* The width must be an integer type. */
1099 if (!type_dependent_expression_p (width)
1100 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1101 error ("width of bit-field %qD has non-integral type %qT", value,
1102 TREE_TYPE (width));
1103 DECL_INITIAL (value) = width;
1104 SET_DECL_C_BIT_FIELD (value);
1107 DECL_IN_AGGR_P (value) = 1;
1109 if (attrlist)
1110 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1112 return value;
1116 /* Returns true iff ATTR is an attribute which needs to be applied at
1117 instantiation time rather than template definition time. */
1119 static bool
1120 is_late_template_attribute (tree attr, tree decl)
1122 tree name = get_attribute_name (attr);
1123 tree args = TREE_VALUE (attr);
1124 const struct attribute_spec *spec = lookup_attribute_spec (name);
1125 tree arg;
1127 if (!spec)
1128 /* Unknown attribute. */
1129 return false;
1131 /* Attribute weak handling wants to write out assembly right away. */
1132 if (is_attribute_p ("weak", name))
1133 return true;
1135 /* Attribute unused is applied directly, as it appertains to
1136 decls. */
1137 if (is_attribute_p ("unused", name))
1138 return false;
1140 /* #pragma omp declare simd attribute needs to be always deferred. */
1141 if (flag_openmp
1142 && is_attribute_p ("omp declare simd", name))
1143 return true;
1145 /* If any of the arguments are dependent expressions, we can't evaluate
1146 the attribute until instantiation time. */
1147 for (arg = args; arg; arg = TREE_CHAIN (arg))
1149 tree t = TREE_VALUE (arg);
1151 /* If the first attribute argument is an identifier, only consider
1152 second and following arguments. Attributes like mode, format,
1153 cleanup and several target specific attributes aren't late
1154 just because they have an IDENTIFIER_NODE as first argument. */
1155 if (arg == args && identifier_p (t))
1156 continue;
1158 if (value_dependent_expression_p (t)
1159 || type_dependent_expression_p (t))
1160 return true;
1163 if (TREE_CODE (decl) == TYPE_DECL
1164 || TYPE_P (decl)
1165 || spec->type_required)
1167 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1169 /* We can't apply any attributes to a completely unknown type until
1170 instantiation time. */
1171 enum tree_code code = TREE_CODE (type);
1172 if (code == TEMPLATE_TYPE_PARM
1173 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1174 || code == TYPENAME_TYPE)
1175 return true;
1176 /* Also defer most attributes on dependent types. This is not
1177 necessary in all cases, but is the better default. */
1178 else if (dependent_type_p (type)
1179 /* But attributes abi_tag and visibility specifically apply
1180 to templates. */
1181 && !is_attribute_p ("abi_tag", name)
1182 && !is_attribute_p ("visibility", name))
1183 return true;
1184 else
1185 return false;
1187 else
1188 return false;
1191 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1192 applied at instantiation time and return them. If IS_DEPENDENT is true,
1193 the declaration itself is dependent, so all attributes should be applied
1194 at instantiation time. */
1196 static tree
1197 splice_template_attributes (tree *attr_p, tree decl)
1199 tree *p = attr_p;
1200 tree late_attrs = NULL_TREE;
1201 tree *q = &late_attrs;
1203 if (!p)
1204 return NULL_TREE;
1206 for (; *p; )
1208 if (is_late_template_attribute (*p, decl))
1210 ATTR_IS_DEPENDENT (*p) = 1;
1211 *q = *p;
1212 *p = TREE_CHAIN (*p);
1213 q = &TREE_CHAIN (*q);
1214 *q = NULL_TREE;
1216 else
1217 p = &TREE_CHAIN (*p);
1220 return late_attrs;
1223 /* Remove any late attributes from the list in ATTR_P and attach them to
1224 DECL_P. */
1226 static void
1227 save_template_attributes (tree *attr_p, tree *decl_p)
1229 tree *q;
1231 if (attr_p && *attr_p == error_mark_node)
1232 return;
1234 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1235 if (!late_attrs)
1236 return;
1238 if (DECL_P (*decl_p))
1239 q = &DECL_ATTRIBUTES (*decl_p);
1240 else
1241 q = &TYPE_ATTRIBUTES (*decl_p);
1243 tree old_attrs = *q;
1245 /* Merge the late attributes at the beginning with the attribute
1246 list. */
1247 late_attrs = merge_attributes (late_attrs, *q);
1248 *q = late_attrs;
1250 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1252 /* We've added new attributes directly to the main variant, so
1253 now we need to update all of the other variants to include
1254 these new attributes. */
1255 tree variant;
1256 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1257 variant = TYPE_NEXT_VARIANT (variant))
1259 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1260 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1265 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1266 to a typedef which gives a previously anonymous class or enum a name for
1267 linkage purposes. */
1269 bool
1270 attributes_naming_typedef_ok (tree attrs)
1272 for (; attrs; attrs = TREE_CHAIN (attrs))
1274 tree name = get_attribute_name (attrs);
1275 if (is_attribute_p ("vector_size", name))
1276 return false;
1278 return true;
1281 /* Like reconstruct_complex_type, but handle also template trees. */
1283 tree
1284 cp_reconstruct_complex_type (tree type, tree bottom)
1286 tree inner, outer;
1287 bool late_return_type_p = false;
1289 if (TYPE_PTR_P (type))
1291 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1292 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1293 TYPE_REF_CAN_ALIAS_ALL (type));
1295 else if (TREE_CODE (type) == REFERENCE_TYPE)
1297 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1298 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1299 TYPE_REF_CAN_ALIAS_ALL (type));
1301 else if (TREE_CODE (type) == ARRAY_TYPE)
1303 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1304 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1305 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1306 element type qualification will be handled by the recursive
1307 cp_reconstruct_complex_type call and cp_build_qualified_type
1308 for ARRAY_TYPEs changes the element type. */
1309 return outer;
1311 else if (TREE_CODE (type) == FUNCTION_TYPE)
1313 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1314 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1315 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1316 outer = apply_memfn_quals (outer,
1317 type_memfn_quals (type),
1318 type_memfn_rqual (type));
1320 else if (TREE_CODE (type) == METHOD_TYPE)
1322 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1323 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1324 /* The build_method_type_directly() routine prepends 'this' to argument list,
1325 so we must compensate by getting rid of it. */
1326 outer
1327 = build_method_type_directly
1328 (class_of_this_parm (type), inner,
1329 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1331 else if (TREE_CODE (type) == OFFSET_TYPE)
1333 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1334 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1336 else
1337 return bottom;
1339 if (TYPE_ATTRIBUTES (type))
1340 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1341 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1343 if (late_return_type_p)
1344 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1346 return outer;
1349 /* Replaces any constexpr expression that may be into the attributes
1350 arguments with their reduced value. */
1352 static void
1353 cp_check_const_attributes (tree attributes)
1355 if (attributes == error_mark_node)
1356 return;
1358 tree attr;
1359 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1361 tree arg;
1362 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1364 tree expr = TREE_VALUE (arg);
1365 if (EXPR_P (expr))
1366 TREE_VALUE (arg) = maybe_constant_value (expr);
1371 /* Return true if TYPE is an OpenMP mappable type. */
1372 bool
1373 cp_omp_mappable_type (tree type)
1375 /* Mappable type has to be complete. */
1376 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1377 return false;
1378 /* Arrays have mappable type if the elements have mappable type. */
1379 while (TREE_CODE (type) == ARRAY_TYPE)
1380 type = TREE_TYPE (type);
1381 /* A mappable type cannot contain virtual members. */
1382 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1383 return false;
1384 /* All data members must be non-static. */
1385 if (CLASS_TYPE_P (type))
1387 tree field;
1388 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1389 if (TREE_CODE (field) == VAR_DECL)
1390 return false;
1391 /* All fields must have mappable types. */
1392 else if (TREE_CODE (field) == FIELD_DECL
1393 && !cp_omp_mappable_type (TREE_TYPE (field)))
1394 return false;
1396 return true;
1399 /* Like decl_attributes, but handle C++ complexity. */
1401 void
1402 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1404 if (*decl == NULL_TREE || *decl == void_type_node
1405 || *decl == error_mark_node)
1406 return;
1408 /* Add implicit "omp declare target" attribute if requested. */
1409 if (scope_chain->omp_declare_target_attribute
1410 && ((TREE_CODE (*decl) == VAR_DECL && TREE_STATIC (*decl))
1411 || TREE_CODE (*decl) == FUNCTION_DECL))
1413 if (TREE_CODE (*decl) == VAR_DECL
1414 && DECL_CLASS_SCOPE_P (*decl))
1415 error ("%q+D static data member inside of declare target directive",
1416 *decl);
1417 else if (TREE_CODE (*decl) == VAR_DECL
1418 && (DECL_FUNCTION_SCOPE_P (*decl)
1419 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1420 error ("%q+D in block scope inside of declare target directive",
1421 *decl);
1422 else if (!processing_template_decl
1423 && TREE_CODE (*decl) == VAR_DECL
1424 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1425 error ("%q+D in declare target directive does not have mappable type",
1426 *decl);
1427 else
1428 attributes = tree_cons (get_identifier ("omp declare target"),
1429 NULL_TREE, attributes);
1432 if (processing_template_decl)
1434 if (check_for_bare_parameter_packs (attributes))
1435 return;
1437 save_template_attributes (&attributes, decl);
1440 cp_check_const_attributes (attributes);
1442 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1443 decl = &DECL_TEMPLATE_RESULT (*decl);
1445 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1447 attributes
1448 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1449 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (*decl)),
1450 attributes, flags);
1452 else
1453 decl_attributes (decl, attributes, flags);
1455 if (TREE_CODE (*decl) == TYPE_DECL)
1456 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1459 /* Walks through the namespace- or function-scope anonymous union
1460 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1461 Returns one of the fields for use in the mangled name. */
1463 static tree
1464 build_anon_union_vars (tree type, tree object)
1466 tree main_decl = NULL_TREE;
1467 tree field;
1469 /* Rather than write the code to handle the non-union case,
1470 just give an error. */
1471 if (TREE_CODE (type) != UNION_TYPE)
1473 error ("anonymous struct not inside named type");
1474 return error_mark_node;
1477 for (field = TYPE_FIELDS (type);
1478 field != NULL_TREE;
1479 field = DECL_CHAIN (field))
1481 tree decl;
1482 tree ref;
1484 if (DECL_ARTIFICIAL (field))
1485 continue;
1486 if (TREE_CODE (field) != FIELD_DECL)
1488 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1489 "have non-static data members", field);
1490 continue;
1493 if (TREE_PRIVATE (field))
1494 permerror (input_location, "private member %q+#D in anonymous union", field);
1495 else if (TREE_PROTECTED (field))
1496 permerror (input_location, "protected member %q+#D in anonymous union", field);
1498 if (processing_template_decl)
1499 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1500 DECL_NAME (field), NULL_TREE);
1501 else
1502 ref = build_class_member_access_expr (object, field, NULL_TREE,
1503 false, tf_warning_or_error);
1505 if (DECL_NAME (field))
1507 tree base;
1509 decl = build_decl (input_location,
1510 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1511 DECL_ANON_UNION_VAR_P (decl) = 1;
1512 DECL_ARTIFICIAL (decl) = 1;
1514 base = get_base_address (object);
1515 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1516 TREE_STATIC (decl) = TREE_STATIC (base);
1517 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1519 SET_DECL_VALUE_EXPR (decl, ref);
1520 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1522 decl = pushdecl (decl);
1524 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1525 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1526 else
1527 decl = 0;
1529 if (main_decl == NULL_TREE)
1530 main_decl = decl;
1533 return main_decl;
1536 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1537 anonymous union, then all members must be laid out together. PUBLIC_P
1538 is nonzero if this union is not declared static. */
1540 void
1541 finish_anon_union (tree anon_union_decl)
1543 tree type;
1544 tree main_decl;
1545 bool public_p;
1547 if (anon_union_decl == error_mark_node)
1548 return;
1550 type = TREE_TYPE (anon_union_decl);
1551 public_p = TREE_PUBLIC (anon_union_decl);
1553 /* The VAR_DECL's context is the same as the TYPE's context. */
1554 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1556 if (TYPE_FIELDS (type) == NULL_TREE)
1557 return;
1559 if (public_p)
1561 error ("namespace-scope anonymous aggregates must be static");
1562 return;
1565 main_decl = build_anon_union_vars (type, anon_union_decl);
1566 if (main_decl == error_mark_node)
1567 return;
1568 if (main_decl == NULL_TREE)
1570 warning (0, "anonymous union with no members");
1571 return;
1574 if (!processing_template_decl)
1576 /* Use main_decl to set the mangled name. */
1577 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1578 maybe_commonize_var (anon_union_decl);
1579 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1580 mangle_decl (anon_union_decl);
1581 DECL_NAME (anon_union_decl) = NULL_TREE;
1584 pushdecl (anon_union_decl);
1585 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1588 /* Auxiliary functions to make type signatures for
1589 `operator new' and `operator delete' correspond to
1590 what compiler will be expecting. */
1592 tree
1593 coerce_new_type (tree type)
1595 int e = 0;
1596 tree args = TYPE_ARG_TYPES (type);
1598 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1600 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1602 e = 1;
1603 error ("%<operator new%> must return type %qT", ptr_type_node);
1606 if (args && args != void_list_node)
1608 if (TREE_PURPOSE (args))
1610 /* [basic.stc.dynamic.allocation]
1612 The first parameter shall not have an associated default
1613 argument. */
1614 error ("the first parameter of %<operator new%> cannot "
1615 "have a default argument");
1616 /* Throw away the default argument. */
1617 TREE_PURPOSE (args) = NULL_TREE;
1620 if (!same_type_p (TREE_VALUE (args), size_type_node))
1622 e = 2;
1623 args = TREE_CHAIN (args);
1626 else
1627 e = 2;
1629 if (e == 2)
1630 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1631 "as first parameter", size_type_node);
1633 switch (e)
1635 case 2:
1636 args = tree_cons (NULL_TREE, size_type_node, args);
1637 /* Fall through. */
1638 case 1:
1639 type = build_exception_variant
1640 (build_function_type (ptr_type_node, args),
1641 TYPE_RAISES_EXCEPTIONS (type));
1642 /* Fall through. */
1643 default:;
1645 return type;
1648 tree
1649 coerce_delete_type (tree type)
1651 int e = 0;
1652 tree args = TYPE_ARG_TYPES (type);
1654 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1656 if (!same_type_p (TREE_TYPE (type), void_type_node))
1658 e = 1;
1659 error ("%<operator delete%> must return type %qT", void_type_node);
1662 if (!args || args == void_list_node
1663 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1665 e = 2;
1666 if (args && args != void_list_node)
1667 args = TREE_CHAIN (args);
1668 error ("%<operator delete%> takes type %qT as first parameter",
1669 ptr_type_node);
1671 switch (e)
1673 case 2:
1674 args = tree_cons (NULL_TREE, ptr_type_node, args);
1675 /* Fall through. */
1676 case 1:
1677 type = build_exception_variant
1678 (build_function_type (void_type_node, args),
1679 TYPE_RAISES_EXCEPTIONS (type));
1680 /* Fall through. */
1681 default:;
1684 return type;
1687 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1688 and mark them as needed. */
1690 static void
1691 mark_vtable_entries (tree decl)
1693 tree fnaddr;
1694 unsigned HOST_WIDE_INT idx;
1696 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1697 idx, fnaddr)
1699 tree fn;
1701 STRIP_NOPS (fnaddr);
1703 if (TREE_CODE (fnaddr) != ADDR_EXPR
1704 && TREE_CODE (fnaddr) != FDESC_EXPR)
1705 /* This entry is an offset: a virtual base class offset, a
1706 virtual call offset, an RTTI offset, etc. */
1707 continue;
1709 fn = TREE_OPERAND (fnaddr, 0);
1710 TREE_ADDRESSABLE (fn) = 1;
1711 /* When we don't have vcall offsets, we output thunks whenever
1712 we output the vtables that contain them. With vcall offsets,
1713 we know all the thunks we'll need when we emit a virtual
1714 function, so we emit the thunks there instead. */
1715 if (DECL_THUNK_P (fn))
1716 use_thunk (fn, /*emit_p=*/0);
1717 mark_used (fn);
1721 /* Set DECL up to have the closest approximation of "initialized common"
1722 linkage available. */
1724 void
1725 comdat_linkage (tree decl)
1727 if (flag_weak)
1728 make_decl_one_only (decl, cxx_comdat_group (decl));
1729 else if (TREE_CODE (decl) == FUNCTION_DECL
1730 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1731 /* We can just emit function and compiler-generated variables
1732 statically; having multiple copies is (for the most part) only
1733 a waste of space.
1735 There are two correctness issues, however: the address of a
1736 template instantiation with external linkage should be the
1737 same, independent of what translation unit asks for the
1738 address, and this will not hold when we emit multiple copies of
1739 the function. However, there's little else we can do.
1741 Also, by default, the typeinfo implementation assumes that
1742 there will be only one copy of the string used as the name for
1743 each type. Therefore, if weak symbols are unavailable, the
1744 run-time library should perform a more conservative check; it
1745 should perform a string comparison, rather than an address
1746 comparison. */
1747 TREE_PUBLIC (decl) = 0;
1748 else
1750 /* Static data member template instantiations, however, cannot
1751 have multiple copies. */
1752 if (DECL_INITIAL (decl) == 0
1753 || DECL_INITIAL (decl) == error_mark_node)
1754 DECL_COMMON (decl) = 1;
1755 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1757 DECL_COMMON (decl) = 1;
1758 DECL_INITIAL (decl) = error_mark_node;
1760 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1762 /* We can't do anything useful; leave vars for explicit
1763 instantiation. */
1764 DECL_EXTERNAL (decl) = 1;
1765 DECL_NOT_REALLY_EXTERN (decl) = 0;
1769 DECL_COMDAT (decl) = 1;
1772 /* For win32 we also want to put explicit instantiations in
1773 linkonce sections, so that they will be merged with implicit
1774 instantiations; otherwise we get duplicate symbol errors.
1775 For Darwin we do not want explicit instantiations to be
1776 linkonce. */
1778 void
1779 maybe_make_one_only (tree decl)
1781 /* We used to say that this was not necessary on targets that support weak
1782 symbols, because the implicit instantiations will defer to the explicit
1783 one. However, that's not actually the case in SVR4; a strong definition
1784 after a weak one is an error. Also, not making explicit
1785 instantiations one_only means that we can end up with two copies of
1786 some template instantiations. */
1787 if (! flag_weak)
1788 return;
1790 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1791 we can get away with not emitting them if they aren't used. We need
1792 to for variables so that cp_finish_decl will update their linkage,
1793 because their DECL_INITIAL may not have been set properly yet. */
1795 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1796 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1797 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1799 make_decl_one_only (decl, cxx_comdat_group (decl));
1801 if (VAR_P (decl))
1803 varpool_node *node = varpool_node::get_create (decl);
1804 DECL_COMDAT (decl) = 1;
1805 /* Mark it needed so we don't forget to emit it. */
1806 node->forced_by_abi = true;
1807 TREE_USED (decl) = 1;
1812 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1813 This predicate will give the right answer during parsing of the
1814 function, which other tests may not. */
1816 bool
1817 vague_linkage_p (tree decl)
1819 /* Unfortunately, import_export_decl has not always been called
1820 before the function is processed, so we cannot simply check
1821 DECL_COMDAT. */
1822 if (DECL_COMDAT (decl)
1823 || (((TREE_CODE (decl) == FUNCTION_DECL
1824 && DECL_DECLARED_INLINE_P (decl))
1825 || (DECL_LANG_SPECIFIC (decl)
1826 && DECL_TEMPLATE_INSTANTIATION (decl)))
1827 && TREE_PUBLIC (decl)))
1828 return true;
1829 else if (DECL_FUNCTION_SCOPE_P (decl))
1830 /* A local static in an inline effectively has vague linkage. */
1831 return (TREE_STATIC (decl)
1832 && vague_linkage_p (DECL_CONTEXT (decl)));
1833 else
1834 return false;
1837 /* Determine whether or not we want to specifically import or export CTYPE,
1838 using various heuristics. */
1840 static void
1841 import_export_class (tree ctype)
1843 /* -1 for imported, 1 for exported. */
1844 int import_export = 0;
1846 /* It only makes sense to call this function at EOF. The reason is
1847 that this function looks at whether or not the first non-inline
1848 non-abstract virtual member function has been defined in this
1849 translation unit. But, we can't possibly know that until we've
1850 seen the entire translation unit. */
1851 gcc_assert (at_eof);
1853 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1854 return;
1856 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1857 we will have CLASSTYPE_INTERFACE_ONLY set but not
1858 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1859 heuristic because someone will supply a #pragma implementation
1860 elsewhere, and deducing it here would produce a conflict. */
1861 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1862 return;
1864 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1865 import_export = -1;
1866 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1867 import_export = 1;
1868 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1869 && !flag_implicit_templates)
1870 /* For a template class, without -fimplicit-templates, check the
1871 repository. If the virtual table is assigned to this
1872 translation unit, then export the class; otherwise, import
1873 it. */
1874 import_export = repo_export_class_p (ctype) ? 1 : -1;
1875 else if (TYPE_POLYMORPHIC_P (ctype))
1877 /* The ABI specifies that the virtual table and associated
1878 information are emitted with the key method, if any. */
1879 tree method = CLASSTYPE_KEY_METHOD (ctype);
1880 /* If weak symbol support is not available, then we must be
1881 careful not to emit the vtable when the key function is
1882 inline. An inline function can be defined in multiple
1883 translation units. If we were to emit the vtable in each
1884 translation unit containing a definition, we would get
1885 multiple definition errors at link-time. */
1886 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1887 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1890 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1891 a definition anywhere else. */
1892 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1893 import_export = 0;
1895 /* Allow back ends the chance to overrule the decision. */
1896 if (targetm.cxx.import_export_class)
1897 import_export = targetm.cxx.import_export_class (ctype, import_export);
1899 if (import_export)
1901 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1902 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1906 /* Return true if VAR has already been provided to the back end; in that
1907 case VAR should not be modified further by the front end. */
1908 static bool
1909 var_finalized_p (tree var)
1911 return varpool_node::get_create (var)->definition;
1914 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1915 must be emitted in this translation unit. Mark it as such. */
1917 void
1918 mark_needed (tree decl)
1920 TREE_USED (decl) = 1;
1921 if (TREE_CODE (decl) == FUNCTION_DECL)
1923 /* Extern inline functions don't become needed when referenced.
1924 If we know a method will be emitted in other TU and no new
1925 functions can be marked reachable, just use the external
1926 definition. */
1927 struct cgraph_node *node = cgraph_node::get_create (decl);
1928 node->forced_by_abi = true;
1930 /* #pragma interface and -frepo code can call mark_needed for
1931 maybe-in-charge 'tors; mark the clones as well. */
1932 tree clone;
1933 FOR_EACH_CLONE (clone, decl)
1934 mark_needed (clone);
1936 else if (TREE_CODE (decl) == VAR_DECL)
1938 varpool_node *node = varpool_node::get_create (decl);
1939 /* C++ frontend use mark_decl_references to force COMDAT variables
1940 to be output that might appear dead otherwise. */
1941 node->forced_by_abi = true;
1945 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1946 returns true if a definition of this entity should be provided in
1947 this object file. Callers use this function to determine whether
1948 or not to let the back end know that a definition of DECL is
1949 available in this translation unit. */
1951 bool
1952 decl_needed_p (tree decl)
1954 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1955 /* This function should only be called at the end of the translation
1956 unit. We cannot be sure of whether or not something will be
1957 COMDAT until that point. */
1958 gcc_assert (at_eof);
1960 /* All entities with external linkage that are not COMDAT should be
1961 emitted; they may be referred to from other object files. */
1962 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1963 return true;
1964 /* If this entity was used, let the back end see it; it will decide
1965 whether or not to emit it into the object file. */
1966 if (TREE_USED (decl))
1967 return true;
1968 /* Functions marked "dllexport" must be emitted so that they are
1969 visible to other DLLs. */
1970 if (flag_keep_inline_dllexport
1971 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1972 return true;
1973 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1974 reference to DECL might cause it to be emitted later. */
1975 return false;
1978 /* If necessary, write out the vtables for the dynamic class CTYPE.
1979 Returns true if any vtables were emitted. */
1981 static bool
1982 maybe_emit_vtables (tree ctype)
1984 tree vtbl;
1985 tree primary_vtbl;
1986 int needed = 0;
1987 varpool_node *current = NULL, *last = NULL;
1989 /* If the vtables for this class have already been emitted there is
1990 nothing more to do. */
1991 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1992 if (var_finalized_p (primary_vtbl))
1993 return false;
1994 /* Ignore dummy vtables made by get_vtable_decl. */
1995 if (TREE_TYPE (primary_vtbl) == void_type_node)
1996 return false;
1998 /* On some targets, we cannot determine the key method until the end
1999 of the translation unit -- which is when this function is
2000 called. */
2001 if (!targetm.cxx.key_method_may_be_inline ())
2002 determine_key_method (ctype);
2004 /* See if any of the vtables are needed. */
2005 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2007 import_export_decl (vtbl);
2008 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2009 needed = 1;
2011 if (!needed)
2013 /* If the references to this class' vtables are optimized away,
2014 still emit the appropriate debugging information. See
2015 dfs_debug_mark. */
2016 if (DECL_COMDAT (primary_vtbl)
2017 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2018 note_debug_info_needed (ctype);
2019 return false;
2022 /* The ABI requires that we emit all of the vtables if we emit any
2023 of them. */
2024 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2026 /* Mark entities references from the virtual table as used. */
2027 mark_vtable_entries (vtbl);
2029 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2031 vec<tree, va_gc> *cleanups = NULL;
2032 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2033 LOOKUP_NORMAL);
2035 /* It had better be all done at compile-time. */
2036 gcc_assert (!expr && !cleanups);
2039 /* Write it out. */
2040 DECL_EXTERNAL (vtbl) = 0;
2041 rest_of_decl_compilation (vtbl, 1, 1);
2043 /* Because we're only doing syntax-checking, we'll never end up
2044 actually marking the variable as written. */
2045 if (flag_syntax_only)
2046 TREE_ASM_WRITTEN (vtbl) = 1;
2047 else if (DECL_ONE_ONLY (vtbl))
2049 current = varpool_node::get_create (vtbl);
2050 if (last)
2051 current->add_to_same_comdat_group (last);
2052 last = current;
2056 /* Since we're writing out the vtable here, also write the debug
2057 info. */
2058 note_debug_info_needed (ctype);
2060 return true;
2063 /* A special return value from type_visibility meaning internal
2064 linkage. */
2066 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2068 /* walk_tree helper function for type_visibility. */
2070 static tree
2071 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2073 int *vis_p = (int *)data;
2074 if (! TYPE_P (*tp))
2076 *walk_subtrees = 0;
2078 else if (OVERLOAD_TYPE_P (*tp)
2079 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2081 *vis_p = VISIBILITY_ANON;
2082 return *tp;
2084 else if (CLASS_TYPE_P (*tp)
2085 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2086 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2087 return NULL;
2090 /* Returns the visibility of TYPE, which is the minimum visibility of its
2091 component types. */
2093 static int
2094 type_visibility (tree type)
2096 int vis = VISIBILITY_DEFAULT;
2097 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2098 return vis;
2101 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2102 specified (or if VISIBILITY is static). If TMPL is true, this
2103 constraint is for a template argument, and takes precedence
2104 over explicitly-specified visibility on the template. */
2106 static void
2107 constrain_visibility (tree decl, int visibility, bool tmpl)
2109 if (visibility == VISIBILITY_ANON)
2111 /* extern "C" declarations aren't affected by the anonymous
2112 namespace. */
2113 if (!DECL_EXTERN_C_P (decl))
2115 TREE_PUBLIC (decl) = 0;
2116 DECL_WEAK (decl) = 0;
2117 DECL_COMMON (decl) = 0;
2118 if (TREE_CODE (decl) == FUNCTION_DECL
2119 || TREE_CODE (decl) == VAR_DECL)
2121 struct symtab_node *snode = symtab_node::get (decl);
2123 if (snode)
2124 snode->set_comdat_group (NULL);
2126 DECL_INTERFACE_KNOWN (decl) = 1;
2127 if (DECL_LANG_SPECIFIC (decl))
2128 DECL_NOT_REALLY_EXTERN (decl) = 1;
2131 else if (visibility > DECL_VISIBILITY (decl)
2132 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2134 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2135 /* This visibility was not specified. */
2136 DECL_VISIBILITY_SPECIFIED (decl) = false;
2140 /* Constrain the visibility of DECL based on the visibility of its template
2141 arguments. */
2143 static void
2144 constrain_visibility_for_template (tree decl, tree targs)
2146 /* If this is a template instantiation, check the innermost
2147 template args for visibility constraints. The outer template
2148 args are covered by the class check. */
2149 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2150 int i;
2151 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2153 int vis = 0;
2155 tree arg = TREE_VEC_ELT (args, i-1);
2156 if (TYPE_P (arg))
2157 vis = type_visibility (arg);
2158 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2160 STRIP_NOPS (arg);
2161 if (TREE_CODE (arg) == ADDR_EXPR)
2162 arg = TREE_OPERAND (arg, 0);
2163 if (VAR_OR_FUNCTION_DECL_P (arg))
2165 if (! TREE_PUBLIC (arg))
2166 vis = VISIBILITY_ANON;
2167 else
2168 vis = DECL_VISIBILITY (arg);
2171 if (vis)
2172 constrain_visibility (decl, vis, true);
2176 /* Like c_determine_visibility, but with additional C++-specific
2177 behavior.
2179 Function-scope entities can rely on the function's visibility because
2180 it is set in start_preparsed_function.
2182 Class-scope entities cannot rely on the class's visibility until the end
2183 of the enclosing class definition.
2185 Note that because namespaces have multiple independent definitions,
2186 namespace visibility is handled elsewhere using the #pragma visibility
2187 machinery rather than by decorating the namespace declaration.
2189 The goal is for constraints from the type to give a diagnostic, and
2190 other constraints to be applied silently. */
2192 void
2193 determine_visibility (tree decl)
2195 tree class_type = NULL_TREE;
2196 bool use_template;
2197 bool orig_visibility_specified;
2198 enum symbol_visibility orig_visibility;
2200 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2202 /* Only relevant for names with external linkage. */
2203 if (!TREE_PUBLIC (decl))
2204 return;
2206 /* Cloned constructors and destructors get the same visibility as
2207 the underlying function. That should be set up in
2208 maybe_clone_body. */
2209 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2211 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2212 orig_visibility = DECL_VISIBILITY (decl);
2214 if (TREE_CODE (decl) == TYPE_DECL)
2216 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2217 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2218 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2219 use_template = 1;
2220 else
2221 use_template = 0;
2223 else if (DECL_LANG_SPECIFIC (decl))
2224 use_template = DECL_USE_TEMPLATE (decl);
2225 else
2226 use_template = 0;
2228 /* If DECL is a member of a class, visibility specifiers on the
2229 class can influence the visibility of the DECL. */
2230 if (DECL_CLASS_SCOPE_P (decl))
2231 class_type = DECL_CONTEXT (decl);
2232 else
2234 /* Not a class member. */
2236 /* Virtual tables have DECL_CONTEXT set to their associated class,
2237 so they are automatically handled above. */
2238 gcc_assert (!VAR_P (decl)
2239 || !DECL_VTABLE_OR_VTT_P (decl));
2241 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2243 /* Local statics and classes get the visibility of their
2244 containing function by default, except that
2245 -fvisibility-inlines-hidden doesn't affect them. */
2246 tree fn = DECL_CONTEXT (decl);
2247 if (DECL_VISIBILITY_SPECIFIED (fn))
2249 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2250 DECL_VISIBILITY_SPECIFIED (decl) =
2251 DECL_VISIBILITY_SPECIFIED (fn);
2253 else
2255 if (DECL_CLASS_SCOPE_P (fn))
2256 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2257 else if (determine_hidden_inline (fn))
2259 DECL_VISIBILITY (decl) = default_visibility;
2260 DECL_VISIBILITY_SPECIFIED (decl) =
2261 visibility_options.inpragma;
2263 else
2265 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2266 DECL_VISIBILITY_SPECIFIED (decl) =
2267 DECL_VISIBILITY_SPECIFIED (fn);
2271 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2272 but have no TEMPLATE_INFO, so don't try to check it. */
2273 use_template = 0;
2275 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2276 && flag_visibility_ms_compat)
2278 /* Under -fvisibility-ms-compat, types are visible by default,
2279 even though their contents aren't. */
2280 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2281 int underlying_vis = type_visibility (underlying_type);
2282 if (underlying_vis == VISIBILITY_ANON
2283 || (CLASS_TYPE_P (underlying_type)
2284 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2285 constrain_visibility (decl, underlying_vis, false);
2286 else
2287 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2289 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2291 /* tinfo visibility is based on the type it's for. */
2292 constrain_visibility
2293 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2295 /* Give the target a chance to override the visibility associated
2296 with DECL. */
2297 if (TREE_PUBLIC (decl)
2298 && !DECL_REALLY_EXTERN (decl)
2299 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2300 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2301 targetm.cxx.determine_class_data_visibility (decl);
2303 else if (use_template)
2304 /* Template instantiations and specializations get visibility based
2305 on their template unless they override it with an attribute. */;
2306 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2308 if (determine_hidden_inline (decl))
2309 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2310 else
2312 /* Set default visibility to whatever the user supplied with
2313 #pragma GCC visibility or a namespace visibility attribute. */
2314 DECL_VISIBILITY (decl) = default_visibility;
2315 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2320 if (use_template)
2322 /* If the specialization doesn't specify visibility, use the
2323 visibility from the template. */
2324 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2325 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2326 : DECL_TEMPLATE_INFO (decl));
2327 tree args = TI_ARGS (tinfo);
2328 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2329 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2330 : DECL_ATTRIBUTES (decl));
2332 if (args != error_mark_node)
2334 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2336 if (!DECL_VISIBILITY_SPECIFIED (decl))
2338 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2339 && determine_hidden_inline (decl))
2340 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2341 else
2343 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2344 DECL_VISIBILITY_SPECIFIED (decl)
2345 = DECL_VISIBILITY_SPECIFIED (pattern);
2349 if (args
2350 /* Template argument visibility outweighs #pragma or namespace
2351 visibility, but not an explicit attribute. */
2352 && !lookup_attribute ("visibility", attribs))
2354 int depth = TMPL_ARGS_DEPTH (args);
2355 if (DECL_VISIBILITY_SPECIFIED (decl))
2357 /* A class template member with explicit visibility
2358 overrides the class visibility, so we need to apply
2359 all the levels of template args directly. */
2360 int i;
2361 for (i = 1; i <= depth; ++i)
2363 tree lev = TMPL_ARGS_LEVEL (args, i);
2364 constrain_visibility_for_template (decl, lev);
2367 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2368 /* Limit visibility based on its template arguments. */
2369 constrain_visibility_for_template (decl, args);
2374 if (class_type)
2375 determine_visibility_from_class (decl, class_type);
2377 if (decl_anon_ns_mem_p (decl))
2378 /* Names in an anonymous namespace get internal linkage.
2379 This might change once we implement export. */
2380 constrain_visibility (decl, VISIBILITY_ANON, false);
2381 else if (TREE_CODE (decl) != TYPE_DECL)
2383 /* Propagate anonymity from type to decl. */
2384 int tvis = type_visibility (TREE_TYPE (decl));
2385 if (tvis == VISIBILITY_ANON
2386 || ! DECL_VISIBILITY_SPECIFIED (decl))
2387 constrain_visibility (decl, tvis, false);
2389 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2390 /* DR 757: A type without linkage shall not be used as the type of a
2391 variable or function with linkage, unless
2392 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2393 o the variable or function is not used (3.2 [basic.def.odr]) or is
2394 defined in the same translation unit.
2396 Since non-extern "C" decls need to be defined in the same
2397 translation unit, we can make the type internal. */
2398 constrain_visibility (decl, VISIBILITY_ANON, false);
2400 /* If visibility changed and DECL already has DECL_RTL, ensure
2401 symbol flags are updated. */
2402 if ((DECL_VISIBILITY (decl) != orig_visibility
2403 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2404 && ((VAR_P (decl) && TREE_STATIC (decl))
2405 || TREE_CODE (decl) == FUNCTION_DECL)
2406 && DECL_RTL_SET_P (decl))
2407 make_decl_rtl (decl);
2410 /* By default, static data members and function members receive
2411 the visibility of their containing class. */
2413 static void
2414 determine_visibility_from_class (tree decl, tree class_type)
2416 if (DECL_VISIBILITY_SPECIFIED (decl))
2417 return;
2419 if (determine_hidden_inline (decl))
2420 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2421 else
2423 /* Default to the class visibility. */
2424 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2425 DECL_VISIBILITY_SPECIFIED (decl)
2426 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2429 /* Give the target a chance to override the visibility associated
2430 with DECL. */
2431 if (VAR_P (decl)
2432 && (DECL_TINFO_P (decl)
2433 || (DECL_VTABLE_OR_VTT_P (decl)
2434 /* Construction virtual tables are not exported because
2435 they cannot be referred to from other object files;
2436 their name is not standardized by the ABI. */
2437 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2438 && TREE_PUBLIC (decl)
2439 && !DECL_REALLY_EXTERN (decl)
2440 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2441 targetm.cxx.determine_class_data_visibility (decl);
2444 /* Returns true iff DECL is an inline that should get hidden visibility
2445 because of -fvisibility-inlines-hidden. */
2447 static bool
2448 determine_hidden_inline (tree decl)
2450 return (visibility_options.inlines_hidden
2451 /* Don't do this for inline templates; specializations might not be
2452 inline, and we don't want them to inherit the hidden
2453 visibility. We'll set it here for all inline instantiations. */
2454 && !processing_template_decl
2455 && TREE_CODE (decl) == FUNCTION_DECL
2456 && DECL_DECLARED_INLINE_P (decl)
2457 && (! DECL_LANG_SPECIFIC (decl)
2458 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2461 /* Constrain the visibility of a class TYPE based on the visibility of its
2462 field types. Warn if any fields require lesser visibility. */
2464 void
2465 constrain_class_visibility (tree type)
2467 tree binfo;
2468 tree t;
2469 int i;
2471 int vis = type_visibility (type);
2473 if (vis == VISIBILITY_ANON
2474 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2475 return;
2477 /* Don't warn about visibility if the class has explicit visibility. */
2478 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2479 vis = VISIBILITY_INTERNAL;
2481 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2482 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2484 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2485 int subvis = type_visibility (ftype);
2487 if (subvis == VISIBILITY_ANON)
2489 if (!in_main_input_context ())
2490 warning (0, "\
2491 %qT has a field %qD whose type uses the anonymous namespace",
2492 type, t);
2494 else if (MAYBE_CLASS_TYPE_P (ftype)
2495 && vis < VISIBILITY_HIDDEN
2496 && subvis >= VISIBILITY_HIDDEN)
2497 warning (OPT_Wattributes, "\
2498 %qT declared with greater visibility than the type of its field %qD",
2499 type, t);
2502 binfo = TYPE_BINFO (type);
2503 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2505 int subvis = type_visibility (TREE_TYPE (t));
2507 if (subvis == VISIBILITY_ANON)
2509 if (!in_main_input_context())
2510 warning (0, "\
2511 %qT has a base %qT whose type uses the anonymous namespace",
2512 type, TREE_TYPE (t));
2514 else if (vis < VISIBILITY_HIDDEN
2515 && subvis >= VISIBILITY_HIDDEN)
2516 warning (OPT_Wattributes, "\
2517 %qT declared with greater visibility than its base %qT",
2518 type, TREE_TYPE (t));
2522 /* Functions for adjusting the visibility of a tagged type and its nested
2523 types and declarations when it gets a name for linkage purposes from a
2524 typedef. */
2526 static void bt_reset_linkage_1 (binding_entry, void *);
2527 static void bt_reset_linkage_2 (binding_entry, void *);
2529 /* First reset the visibility of all the types. */
2531 static void
2532 reset_type_linkage_1 (tree type)
2534 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2535 if (CLASS_TYPE_P (type))
2536 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2537 bt_reset_linkage_1, NULL);
2539 static void
2540 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2542 reset_type_linkage_1 (b->type);
2545 /* Then reset the visibility of any static data members or member
2546 functions that use those types. */
2548 static void
2549 reset_decl_linkage (tree decl)
2551 if (TREE_PUBLIC (decl))
2552 return;
2553 if (DECL_CLONED_FUNCTION_P (decl))
2554 return;
2555 TREE_PUBLIC (decl) = true;
2556 DECL_INTERFACE_KNOWN (decl) = false;
2557 determine_visibility (decl);
2558 tentative_decl_linkage (decl);
2560 static void
2561 reset_type_linkage_2 (tree type)
2563 if (CLASS_TYPE_P (type))
2565 if (tree vt = CLASSTYPE_VTABLES (type))
2567 tree name = mangle_vtbl_for_type (type);
2568 DECL_NAME (vt) = name;
2569 SET_DECL_ASSEMBLER_NAME (vt, name);
2570 reset_decl_linkage (vt);
2572 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2574 tree name = mangle_typeinfo_for_type (type);
2575 DECL_NAME (ti) = name;
2576 SET_DECL_ASSEMBLER_NAME (ti, name);
2577 TREE_TYPE (name) = type;
2578 reset_decl_linkage (ti);
2580 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2581 if (TREE_CODE (m) == VAR_DECL)
2582 reset_decl_linkage (m);
2583 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2584 reset_decl_linkage (m);
2585 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2586 bt_reset_linkage_2, NULL);
2589 static void
2590 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2592 reset_type_linkage_2 (b->type);
2594 void
2595 reset_type_linkage (tree type)
2597 reset_type_linkage_1 (type);
2598 reset_type_linkage_2 (type);
2601 /* Set up our initial idea of what the linkage of DECL should be. */
2603 void
2604 tentative_decl_linkage (tree decl)
2606 if (DECL_INTERFACE_KNOWN (decl))
2607 /* We've already made a decision as to how this function will
2608 be handled. */;
2609 else if (vague_linkage_p (decl))
2611 if (TREE_CODE (decl) == FUNCTION_DECL
2612 && decl_defined_p (decl))
2614 DECL_EXTERNAL (decl) = 1;
2615 DECL_NOT_REALLY_EXTERN (decl) = 1;
2616 note_vague_linkage_fn (decl);
2617 /* A non-template inline function with external linkage will
2618 always be COMDAT. As we must eventually determine the
2619 linkage of all functions, and as that causes writes to
2620 the data mapped in from the PCH file, it's advantageous
2621 to mark the functions at this point. */
2622 if (DECL_DECLARED_INLINE_P (decl)
2623 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2624 || DECL_DEFAULTED_FN (decl)))
2626 /* This function must have external linkage, as
2627 otherwise DECL_INTERFACE_KNOWN would have been
2628 set. */
2629 gcc_assert (TREE_PUBLIC (decl));
2630 comdat_linkage (decl);
2631 DECL_INTERFACE_KNOWN (decl) = 1;
2634 else if (TREE_CODE (decl) == VAR_DECL)
2635 maybe_commonize_var (decl);
2639 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2640 for DECL has not already been determined, do so now by setting
2641 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2642 function is called entities with vague linkage whose definitions
2643 are available must have TREE_PUBLIC set.
2645 If this function decides to place DECL in COMDAT, it will set
2646 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2647 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2648 callers defer that decision until it is clear that DECL is actually
2649 required. */
2651 void
2652 import_export_decl (tree decl)
2654 int emit_p;
2655 bool comdat_p;
2656 bool import_p;
2657 tree class_type = NULL_TREE;
2659 if (DECL_INTERFACE_KNOWN (decl))
2660 return;
2662 /* We cannot determine what linkage to give to an entity with vague
2663 linkage until the end of the file. For example, a virtual table
2664 for a class will be defined if and only if the key method is
2665 defined in this translation unit. As a further example, consider
2666 that when compiling a translation unit that uses PCH file with
2667 "-frepo" it would be incorrect to make decisions about what
2668 entities to emit when building the PCH; those decisions must be
2669 delayed until the repository information has been processed. */
2670 gcc_assert (at_eof);
2671 /* Object file linkage for explicit instantiations is handled in
2672 mark_decl_instantiated. For static variables in functions with
2673 vague linkage, maybe_commonize_var is used.
2675 Therefore, the only declarations that should be provided to this
2676 function are those with external linkage that are:
2678 * implicit instantiations of function templates
2680 * inline function
2682 * implicit instantiations of static data members of class
2683 templates
2685 * virtual tables
2687 * typeinfo objects
2689 Furthermore, all entities that reach this point must have a
2690 definition available in this translation unit.
2692 The following assertions check these conditions. */
2693 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2694 /* Any code that creates entities with TREE_PUBLIC cleared should
2695 also set DECL_INTERFACE_KNOWN. */
2696 gcc_assert (TREE_PUBLIC (decl));
2697 if (TREE_CODE (decl) == FUNCTION_DECL)
2698 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2699 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2700 || DECL_DECLARED_INLINE_P (decl));
2701 else
2702 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2703 || DECL_VTABLE_OR_VTT_P (decl)
2704 || DECL_TINFO_P (decl));
2705 /* Check that a definition of DECL is available in this translation
2706 unit. */
2707 gcc_assert (!DECL_REALLY_EXTERN (decl));
2709 /* Assume that DECL will not have COMDAT linkage. */
2710 comdat_p = false;
2711 /* Assume that DECL will not be imported into this translation
2712 unit. */
2713 import_p = false;
2715 /* See if the repository tells us whether or not to emit DECL in
2716 this translation unit. */
2717 emit_p = repo_emit_p (decl);
2718 if (emit_p == 0)
2719 import_p = true;
2720 else if (emit_p == 1)
2722 /* The repository indicates that this entity should be defined
2723 here. Make sure the back end honors that request. */
2724 mark_needed (decl);
2725 /* Output the definition as an ordinary strong definition. */
2726 DECL_EXTERNAL (decl) = 0;
2727 DECL_INTERFACE_KNOWN (decl) = 1;
2728 return;
2731 if (import_p)
2732 /* We have already decided what to do with this DECL; there is no
2733 need to check anything further. */
2735 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2737 class_type = DECL_CONTEXT (decl);
2738 import_export_class (class_type);
2739 if (TYPE_FOR_JAVA (class_type))
2740 import_p = true;
2741 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2742 && CLASSTYPE_INTERFACE_ONLY (class_type))
2743 import_p = true;
2744 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2745 && !CLASSTYPE_USE_TEMPLATE (class_type)
2746 && CLASSTYPE_KEY_METHOD (class_type)
2747 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2748 /* The ABI requires that all virtual tables be emitted with
2749 COMDAT linkage. However, on systems where COMDAT symbols
2750 don't show up in the table of contents for a static
2751 archive, or on systems without weak symbols (where we
2752 approximate COMDAT linkage by using internal linkage), the
2753 linker will report errors about undefined symbols because
2754 it will not see the virtual table definition. Therefore,
2755 in the case that we know that the virtual table will be
2756 emitted in only one translation unit, we make the virtual
2757 table an ordinary definition with external linkage. */
2758 DECL_EXTERNAL (decl) = 0;
2759 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2761 /* CLASS_TYPE is being exported from this translation unit,
2762 so DECL should be defined here. */
2763 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2764 /* If a class is declared in a header with the "extern
2765 template" extension, then it will not be instantiated,
2766 even in translation units that would normally require
2767 it. Often such classes are explicitly instantiated in
2768 one translation unit. Therefore, the explicit
2769 instantiation must be made visible to other translation
2770 units. */
2771 DECL_EXTERNAL (decl) = 0;
2772 else
2774 /* The generic C++ ABI says that class data is always
2775 COMDAT, even if there is a key function. Some
2776 variants (e.g., the ARM EABI) says that class data
2777 only has COMDAT linkage if the class data might be
2778 emitted in more than one translation unit. When the
2779 key method can be inline and is inline, we still have
2780 to arrange for comdat even though
2781 class_data_always_comdat is false. */
2782 if (!CLASSTYPE_KEY_METHOD (class_type)
2783 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2784 || targetm.cxx.class_data_always_comdat ())
2786 /* The ABI requires COMDAT linkage. Normally, we
2787 only emit COMDAT things when they are needed;
2788 make sure that we realize that this entity is
2789 indeed needed. */
2790 comdat_p = true;
2791 mark_needed (decl);
2795 else if (!flag_implicit_templates
2796 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2797 import_p = true;
2798 else
2799 comdat_p = true;
2801 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2803 tree type = TREE_TYPE (DECL_NAME (decl));
2804 if (CLASS_TYPE_P (type))
2806 class_type = type;
2807 import_export_class (type);
2808 if (CLASSTYPE_INTERFACE_KNOWN (type)
2809 && TYPE_POLYMORPHIC_P (type)
2810 && CLASSTYPE_INTERFACE_ONLY (type)
2811 /* If -fno-rtti was specified, then we cannot be sure
2812 that RTTI information will be emitted with the
2813 virtual table of the class, so we must emit it
2814 wherever it is used. */
2815 && flag_rtti)
2816 import_p = true;
2817 else
2819 if (CLASSTYPE_INTERFACE_KNOWN (type)
2820 && !CLASSTYPE_INTERFACE_ONLY (type))
2822 comdat_p = (targetm.cxx.class_data_always_comdat ()
2823 || (CLASSTYPE_KEY_METHOD (type)
2824 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2825 mark_needed (decl);
2826 if (!flag_weak)
2828 comdat_p = false;
2829 DECL_EXTERNAL (decl) = 0;
2832 else
2833 comdat_p = true;
2836 else
2837 comdat_p = true;
2839 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2841 /* DECL is an implicit instantiation of a function or static
2842 data member. */
2843 if ((flag_implicit_templates
2844 && !flag_use_repository)
2845 || (flag_implicit_inline_templates
2846 && TREE_CODE (decl) == FUNCTION_DECL
2847 && DECL_DECLARED_INLINE_P (decl)))
2848 comdat_p = true;
2849 else
2850 /* If we are not implicitly generating templates, then mark
2851 this entity as undefined in this translation unit. */
2852 import_p = true;
2854 else if (DECL_FUNCTION_MEMBER_P (decl))
2856 if (!DECL_DECLARED_INLINE_P (decl))
2858 tree ctype = DECL_CONTEXT (decl);
2859 import_export_class (ctype);
2860 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2862 DECL_NOT_REALLY_EXTERN (decl)
2863 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2864 || (DECL_DECLARED_INLINE_P (decl)
2865 && ! flag_implement_inlines
2866 && !DECL_VINDEX (decl)));
2868 if (!DECL_NOT_REALLY_EXTERN (decl))
2869 DECL_EXTERNAL (decl) = 1;
2871 /* Always make artificials weak. */
2872 if (DECL_ARTIFICIAL (decl) && flag_weak)
2873 comdat_p = true;
2874 else
2875 maybe_make_one_only (decl);
2878 else
2879 comdat_p = true;
2881 else
2882 comdat_p = true;
2884 if (import_p)
2886 /* If we are importing DECL into this translation unit, mark is
2887 an undefined here. */
2888 DECL_EXTERNAL (decl) = 1;
2889 DECL_NOT_REALLY_EXTERN (decl) = 0;
2891 else if (comdat_p)
2893 /* If we decided to put DECL in COMDAT, mark it accordingly at
2894 this point. */
2895 comdat_linkage (decl);
2898 DECL_INTERFACE_KNOWN (decl) = 1;
2901 /* Return an expression that performs the destruction of DECL, which
2902 must be a VAR_DECL whose type has a non-trivial destructor, or is
2903 an array whose (innermost) elements have a non-trivial destructor. */
2905 tree
2906 build_cleanup (tree decl)
2908 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2909 gcc_assert (clean != NULL_TREE);
2910 return clean;
2913 /* Returns the initialization guard variable for the variable DECL,
2914 which has static storage duration. */
2916 tree
2917 get_guard (tree decl)
2919 tree sname;
2920 tree guard;
2922 sname = mangle_guard_variable (decl);
2923 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2924 if (! guard)
2926 tree guard_type;
2928 /* We use a type that is big enough to contain a mutex as well
2929 as an integer counter. */
2930 guard_type = targetm.cxx.guard_type ();
2931 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2932 VAR_DECL, sname, guard_type);
2934 /* The guard should have the same linkage as what it guards. */
2935 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2936 TREE_STATIC (guard) = TREE_STATIC (decl);
2937 DECL_COMMON (guard) = DECL_COMMON (decl);
2938 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2939 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2940 if (DECL_ONE_ONLY (decl))
2941 make_decl_one_only (guard, cxx_comdat_group (guard));
2942 if (TREE_PUBLIC (decl))
2943 DECL_WEAK (guard) = DECL_WEAK (decl);
2944 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2945 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2947 DECL_ARTIFICIAL (guard) = 1;
2948 DECL_IGNORED_P (guard) = 1;
2949 TREE_USED (guard) = 1;
2950 pushdecl_top_level_and_finish (guard, NULL_TREE);
2952 return guard;
2955 /* Return those bits of the GUARD variable that should be set when the
2956 guarded entity is actually initialized. */
2958 static tree
2959 get_guard_bits (tree guard)
2961 if (!targetm.cxx.guard_mask_bit ())
2963 /* We only set the first byte of the guard, in order to leave room
2964 for a mutex in the high-order bits. */
2965 guard = build1 (ADDR_EXPR,
2966 build_pointer_type (TREE_TYPE (guard)),
2967 guard);
2968 guard = build1 (NOP_EXPR,
2969 build_pointer_type (char_type_node),
2970 guard);
2971 guard = build1 (INDIRECT_REF, char_type_node, guard);
2974 return guard;
2977 /* Return an expression which determines whether or not the GUARD
2978 variable has already been initialized. */
2980 tree
2981 get_guard_cond (tree guard)
2983 tree guard_value;
2985 /* Check to see if the GUARD is zero. */
2986 guard = get_guard_bits (guard);
2988 /* Mask off all but the low bit. */
2989 if (targetm.cxx.guard_mask_bit ())
2991 guard_value = integer_one_node;
2992 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2993 guard_value = convert (TREE_TYPE (guard), guard_value);
2994 guard = cp_build_binary_op (input_location,
2995 BIT_AND_EXPR, guard, guard_value,
2996 tf_warning_or_error);
2999 guard_value = integer_zero_node;
3000 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3001 guard_value = convert (TREE_TYPE (guard), guard_value);
3002 return cp_build_binary_op (input_location,
3003 EQ_EXPR, guard, guard_value,
3004 tf_warning_or_error);
3007 /* Return an expression which sets the GUARD variable, indicating that
3008 the variable being guarded has been initialized. */
3010 tree
3011 set_guard (tree guard)
3013 tree guard_init;
3015 /* Set the GUARD to one. */
3016 guard = get_guard_bits (guard);
3017 guard_init = integer_one_node;
3018 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3019 guard_init = convert (TREE_TYPE (guard), guard_init);
3020 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3021 tf_warning_or_error);
3024 /* Returns true iff we can tell that VAR does not have a dynamic
3025 initializer. */
3027 static bool
3028 var_defined_without_dynamic_init (tree var)
3030 /* If it's defined in another TU, we can't tell. */
3031 if (DECL_EXTERNAL (var))
3032 return false;
3033 /* If it has a non-trivial destructor, registering the destructor
3034 counts as dynamic initialization. */
3035 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3036 return false;
3037 /* If it's in this TU, its initializer has been processed. */
3038 gcc_assert (DECL_INITIALIZED_P (var));
3039 /* If it has no initializer or a constant one, it's not dynamic. */
3040 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3041 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3044 /* Returns true iff VAR is a variable that needs uses to be
3045 wrapped for possible dynamic initialization. */
3047 static bool
3048 var_needs_tls_wrapper (tree var)
3050 return (!error_operand_p (var)
3051 && DECL_THREAD_LOCAL_P (var)
3052 && !DECL_GNU_TLS_P (var)
3053 && !DECL_FUNCTION_SCOPE_P (var)
3054 && !var_defined_without_dynamic_init (var));
3057 /* Get the FUNCTION_DECL for the shared TLS init function for this
3058 translation unit. */
3060 static tree
3061 get_local_tls_init_fn (void)
3063 tree sname = get_identifier ("__tls_init");
3064 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3065 if (!fn)
3067 fn = build_lang_decl (FUNCTION_DECL, sname,
3068 build_function_type (void_type_node,
3069 void_list_node));
3070 SET_DECL_LANGUAGE (fn, lang_c);
3071 TREE_PUBLIC (fn) = false;
3072 DECL_ARTIFICIAL (fn) = true;
3073 mark_used (fn);
3074 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3076 return fn;
3079 /* Get a FUNCTION_DECL for the init function for the thread_local
3080 variable VAR. The init function will be an alias to the function
3081 that initializes all the non-local TLS variables in the translation
3082 unit. The init function is only used by the wrapper function. */
3084 static tree
3085 get_tls_init_fn (tree var)
3087 /* Only C++11 TLS vars need this init fn. */
3088 if (!var_needs_tls_wrapper (var))
3089 return NULL_TREE;
3091 /* If -fno-extern-tls-init, assume that we don't need to call
3092 a tls init function for a variable defined in another TU. */
3093 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3094 return NULL_TREE;
3096 #ifdef ASM_OUTPUT_DEF
3097 /* If the variable is internal, or if we can't generate aliases,
3098 call the local init function directly. */
3099 if (!TREE_PUBLIC (var))
3100 #endif
3101 return get_local_tls_init_fn ();
3103 tree sname = mangle_tls_init_fn (var);
3104 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3105 if (!fn)
3107 fn = build_lang_decl (FUNCTION_DECL, sname,
3108 build_function_type (void_type_node,
3109 void_list_node));
3110 SET_DECL_LANGUAGE (fn, lang_c);
3111 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3112 DECL_ARTIFICIAL (fn) = true;
3113 DECL_COMDAT (fn) = DECL_COMDAT (var);
3114 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3115 if (DECL_ONE_ONLY (var))
3116 make_decl_one_only (fn, cxx_comdat_group (fn));
3117 if (TREE_PUBLIC (var))
3119 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3120 /* If the variable is defined somewhere else and might have static
3121 initialization, make the init function a weak reference. */
3122 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3123 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3124 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3125 && DECL_EXTERNAL (var))
3126 declare_weak (fn);
3127 else
3128 DECL_WEAK (fn) = DECL_WEAK (var);
3130 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3131 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3132 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3133 DECL_IGNORED_P (fn) = 1;
3134 mark_used (fn);
3136 DECL_BEFRIENDING_CLASSES (fn) = var;
3138 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3140 return fn;
3143 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3144 variable VAR. The wrapper function calls the init function (if any) for
3145 VAR and then returns a reference to VAR. The wrapper function is used
3146 in place of VAR everywhere VAR is mentioned. */
3148 tree
3149 get_tls_wrapper_fn (tree var)
3151 /* Only C++11 TLS vars need this wrapper fn. */
3152 if (!var_needs_tls_wrapper (var))
3153 return NULL_TREE;
3155 tree sname = mangle_tls_wrapper_fn (var);
3156 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3157 if (!fn)
3159 /* A named rvalue reference is an lvalue, so the wrapper should
3160 always return an lvalue reference. */
3161 tree type = non_reference (TREE_TYPE (var));
3162 type = build_reference_type (type);
3163 tree fntype = build_function_type (type, void_list_node);
3164 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3165 SET_DECL_LANGUAGE (fn, lang_c);
3166 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3167 DECL_ARTIFICIAL (fn) = true;
3168 DECL_IGNORED_P (fn) = 1;
3169 /* The wrapper is inline and emitted everywhere var is used. */
3170 DECL_DECLARED_INLINE_P (fn) = true;
3171 if (TREE_PUBLIC (var))
3173 comdat_linkage (fn);
3174 #ifdef HAVE_GAS_HIDDEN
3175 /* Make the wrapper bind locally; there's no reason to share
3176 the wrapper between multiple shared objects. */
3177 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3178 DECL_VISIBILITY_SPECIFIED (fn) = true;
3179 #endif
3181 if (!TREE_PUBLIC (fn))
3182 DECL_INTERFACE_KNOWN (fn) = true;
3183 mark_used (fn);
3184 note_vague_linkage_fn (fn);
3186 #if 0
3187 /* We want CSE to commonize calls to the wrapper, but marking it as
3188 pure is unsafe since it has side-effects. I guess we need a new
3189 ECF flag even weaker than ECF_PURE. FIXME! */
3190 DECL_PURE_P (fn) = true;
3191 #endif
3193 DECL_BEFRIENDING_CLASSES (fn) = var;
3195 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3197 return fn;
3200 /* At EOF, generate the definition for the TLS wrapper function FN:
3202 T& var_wrapper() {
3203 if (init_fn) init_fn();
3204 return var;
3205 } */
3207 static void
3208 generate_tls_wrapper (tree fn)
3210 tree var = DECL_BEFRIENDING_CLASSES (fn);
3212 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3213 tree body = begin_function_body ();
3214 /* Only call the init fn if there might be one. */
3215 if (tree init_fn = get_tls_init_fn (var))
3217 tree if_stmt = NULL_TREE;
3218 /* If init_fn is a weakref, make sure it exists before calling. */
3219 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3221 if_stmt = begin_if_stmt ();
3222 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3223 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3224 NE_EXPR, addr, nullptr_node,
3225 tf_warning_or_error);
3226 finish_if_stmt_cond (cond, if_stmt);
3228 finish_expr_stmt (build_cxx_call
3229 (init_fn, 0, NULL, tf_warning_or_error));
3230 if (if_stmt)
3232 finish_then_clause (if_stmt);
3233 finish_if_stmt (if_stmt);
3236 else
3237 /* If there's no initialization, the wrapper is a constant function. */
3238 TREE_READONLY (fn) = true;
3239 finish_return_stmt (convert_from_reference (var));
3240 finish_function_body (body);
3241 expand_or_defer_fn (finish_function (0));
3244 /* Start the process of running a particular set of global constructors
3245 or destructors. Subroutine of do_[cd]tors. Also called from
3246 vtv_start_verification_constructor_init_function. */
3248 static tree
3249 start_objects (int method_type, int initp)
3251 tree body;
3252 tree fndecl;
3253 char type[14];
3255 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3257 if (initp != DEFAULT_INIT_PRIORITY)
3259 char joiner;
3261 #ifdef JOINER
3262 joiner = JOINER;
3263 #else
3264 joiner = '_';
3265 #endif
3267 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3269 else
3270 sprintf (type, "sub_%c", method_type);
3272 fndecl = build_lang_decl (FUNCTION_DECL,
3273 get_file_function_name (type),
3274 build_function_type_list (void_type_node,
3275 NULL_TREE));
3276 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3278 TREE_PUBLIC (current_function_decl) = 0;
3280 /* Mark as artificial because it's not explicitly in the user's
3281 source code. */
3282 DECL_ARTIFICIAL (current_function_decl) = 1;
3284 /* Mark this declaration as used to avoid spurious warnings. */
3285 TREE_USED (current_function_decl) = 1;
3287 /* Mark this function as a global constructor or destructor. */
3288 if (method_type == 'I')
3289 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3290 else
3291 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3293 body = begin_compound_stmt (BCS_FN_BODY);
3295 return body;
3298 /* Finish the process of running a particular set of global constructors
3299 or destructors. Subroutine of do_[cd]tors. */
3301 static void
3302 finish_objects (int method_type, int initp, tree body)
3304 tree fn;
3306 /* Finish up. */
3307 finish_compound_stmt (body);
3308 fn = finish_function (0);
3310 if (method_type == 'I')
3312 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3313 decl_init_priority_insert (fn, initp);
3315 else
3317 DECL_STATIC_DESTRUCTOR (fn) = 1;
3318 decl_fini_priority_insert (fn, initp);
3321 expand_or_defer_fn (fn);
3324 /* The names of the parameters to the function created to handle
3325 initializations and destructions for objects with static storage
3326 duration. */
3327 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3328 #define PRIORITY_IDENTIFIER "__priority"
3330 /* The name of the function we create to handle initializations and
3331 destructions for objects with static storage duration. */
3332 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3334 /* The declaration for the __INITIALIZE_P argument. */
3335 static GTY(()) tree initialize_p_decl;
3337 /* The declaration for the __PRIORITY argument. */
3338 static GTY(()) tree priority_decl;
3340 /* The declaration for the static storage duration function. */
3341 static GTY(()) tree ssdf_decl;
3343 /* All the static storage duration functions created in this
3344 translation unit. */
3345 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3347 /* A map from priority levels to information about that priority
3348 level. There may be many such levels, so efficient lookup is
3349 important. */
3350 static splay_tree priority_info_map;
3352 /* Begins the generation of the function that will handle all
3353 initialization and destruction of objects with static storage
3354 duration. The function generated takes two parameters of type
3355 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3356 nonzero, it performs initializations. Otherwise, it performs
3357 destructions. It only performs those initializations or
3358 destructions with the indicated __PRIORITY. The generated function
3359 returns no value.
3361 It is assumed that this function will only be called once per
3362 translation unit. */
3364 static tree
3365 start_static_storage_duration_function (unsigned count)
3367 tree type;
3368 tree body;
3369 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3371 /* Create the identifier for this function. It will be of the form
3372 SSDF_IDENTIFIER_<number>. */
3373 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
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 /* This variable is going to need initialization and/or
3809 finalization, so we add it to the list. */
3810 *var = TREE_CHAIN (t);
3811 TREE_CHAIN (t) = result;
3812 result = t;
3815 return result;
3818 /* Make sure we have told the back end about all the variables in
3819 VARS. */
3821 static void
3822 write_out_vars (tree vars)
3824 tree v;
3826 for (v = vars; v; v = TREE_CHAIN (v))
3828 tree var = TREE_VALUE (v);
3829 if (!var_finalized_p (var))
3831 import_export_decl (var);
3832 rest_of_decl_compilation (var, 1, 1);
3837 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3838 (otherwise) that will initialize all global objects with static
3839 storage duration having the indicated PRIORITY. */
3841 static void
3842 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3843 location_t *locus)
3845 char function_key;
3846 tree fndecl;
3847 tree body;
3848 size_t i;
3850 input_location = *locus;
3851 /* ??? */
3852 /* Was: locus->line++; */
3854 /* We use `I' to indicate initialization and `D' to indicate
3855 destruction. */
3856 function_key = constructor_p ? 'I' : 'D';
3858 /* We emit the function lazily, to avoid generating empty
3859 global constructors and destructors. */
3860 body = NULL_TREE;
3862 /* For Objective-C++, we may need to initialize metadata found in this module.
3863 This must be done _before_ any other static initializations. */
3864 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3865 && constructor_p && objc_static_init_needed_p ())
3867 body = start_objects (function_key, priority);
3868 objc_generate_static_init_call (NULL_TREE);
3871 /* Call the static storage duration function with appropriate
3872 arguments. */
3873 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3875 /* Calls to pure or const functions will expand to nothing. */
3876 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3878 tree call;
3880 if (! body)
3881 body = start_objects (function_key, priority);
3883 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3884 build_int_cst (NULL_TREE,
3885 constructor_p),
3886 build_int_cst (NULL_TREE,
3887 priority),
3888 NULL_TREE);
3889 finish_expr_stmt (call);
3893 /* Close out the function. */
3894 if (body)
3895 finish_objects (function_key, priority, body);
3898 /* Generate constructor and destructor functions for the priority
3899 indicated by N. */
3901 static int
3902 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3904 location_t *locus = (location_t *) data;
3905 int priority = (int) n->key;
3906 priority_info pi = (priority_info) n->value;
3908 /* Generate the functions themselves, but only if they are really
3909 needed. */
3910 if (pi->initializations_p)
3911 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3912 if (pi->destructions_p)
3913 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3915 /* Keep iterating. */
3916 return 0;
3919 /* Java requires that we be able to reference a local address for a
3920 method, and not be confused by PLT entries. If hidden aliases are
3921 supported, collect and return all the functions for which we should
3922 emit a hidden alias. */
3924 static hash_set<tree> *
3925 collect_candidates_for_java_method_aliases (void)
3927 struct cgraph_node *node;
3928 hash_set<tree> *candidates = NULL;
3930 #ifndef HAVE_GAS_HIDDEN
3931 return candidates;
3932 #endif
3934 FOR_EACH_FUNCTION (node)
3936 tree fndecl = node->decl;
3938 if (DECL_CLASS_SCOPE_P (fndecl)
3939 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3940 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3942 if (candidates == NULL)
3943 candidates = new hash_set<tree>;
3944 candidates->add (fndecl);
3948 return candidates;
3952 /* Java requires that we be able to reference a local address for a
3953 method, and not be confused by PLT entries. If hidden aliases are
3954 supported, emit one for each java function that we've emitted.
3955 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3956 by collect_candidates_for_java_method_aliases. */
3958 static void
3959 build_java_method_aliases (hash_set<tree> *candidates)
3961 struct cgraph_node *node;
3963 #ifndef HAVE_GAS_HIDDEN
3964 return;
3965 #endif
3967 FOR_EACH_FUNCTION (node)
3969 tree fndecl = node->decl;
3971 if (TREE_ASM_WRITTEN (fndecl)
3972 && candidates->contains (fndecl))
3974 /* Mangle the name in a predictable way; we need to reference
3975 this from a java compiled object file. */
3976 tree oid, nid, alias;
3977 const char *oname;
3978 char *nname;
3980 oid = DECL_ASSEMBLER_NAME (fndecl);
3981 oname = IDENTIFIER_POINTER (oid);
3982 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3983 nname = ACONCAT (("_ZGA", oname+2, NULL));
3984 nid = get_identifier (nname);
3986 alias = make_alias_for (fndecl, nid);
3987 TREE_PUBLIC (alias) = 1;
3988 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3990 assemble_alias (alias, oid);
3995 /* Return C++ property of T, based on given operation OP. */
3997 static int
3998 cpp_check (tree t, cpp_operation op)
4000 switch (op)
4002 case IS_ABSTRACT:
4003 return DECL_PURE_VIRTUAL_P (t);
4004 case IS_CONSTRUCTOR:
4005 return DECL_CONSTRUCTOR_P (t);
4006 case IS_DESTRUCTOR:
4007 return DECL_DESTRUCTOR_P (t);
4008 case IS_COPY_CONSTRUCTOR:
4009 return DECL_COPY_CONSTRUCTOR_P (t);
4010 case IS_TEMPLATE:
4011 return TREE_CODE (t) == TEMPLATE_DECL;
4012 case IS_TRIVIAL:
4013 return trivial_type_p (t);
4014 default:
4015 return 0;
4019 /* Collect source file references recursively, starting from NAMESPC. */
4021 static void
4022 collect_source_refs (tree namespc)
4024 tree t;
4026 if (!namespc)
4027 return;
4029 /* Iterate over names in this name space. */
4030 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4031 if (!DECL_IS_BUILTIN (t) )
4032 collect_source_ref (DECL_SOURCE_FILE (t));
4034 /* Dump siblings, if any */
4035 collect_source_refs (TREE_CHAIN (namespc));
4037 /* Dump children, if any */
4038 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4041 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4042 starting from NAMESPC. */
4044 static void
4045 collect_ada_namespace (tree namespc, const char *source_file)
4047 if (!namespc)
4048 return;
4050 /* Collect decls from this namespace */
4051 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4053 /* Collect siblings, if any */
4054 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4056 /* Collect children, if any */
4057 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4060 /* Returns true iff there is a definition available for variable or
4061 function DECL. */
4063 static bool
4064 decl_defined_p (tree decl)
4066 if (TREE_CODE (decl) == FUNCTION_DECL)
4067 return (DECL_INITIAL (decl) != NULL_TREE);
4068 else
4070 gcc_assert (VAR_P (decl));
4071 return !DECL_EXTERNAL (decl);
4075 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4077 [expr.const]
4079 An integral constant-expression can only involve ... const
4080 variables of integral or enumeration types initialized with
4081 constant expressions ...
4083 C++0x also allows constexpr variables and temporaries initialized
4084 with constant expressions. We handle the former here, but the latter
4085 are just folded away in cxx_eval_constant_expression.
4087 The standard does not require that the expression be non-volatile.
4088 G++ implements the proposed correction in DR 457. */
4090 bool
4091 decl_constant_var_p (tree decl)
4093 if (!decl_maybe_constant_var_p (decl))
4094 return false;
4096 /* We don't know if a template static data member is initialized with
4097 a constant expression until we instantiate its initializer. Even
4098 in the case of a constexpr variable, we can't treat it as a
4099 constant until its initializer is complete in case it's used in
4100 its own initializer. */
4101 mark_used (decl);
4102 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4105 /* Returns true if DECL could be a symbolic constant variable, depending on
4106 its initializer. */
4108 bool
4109 decl_maybe_constant_var_p (tree decl)
4111 tree type = TREE_TYPE (decl);
4112 if (!VAR_P (decl))
4113 return false;
4114 if (DECL_DECLARED_CONSTEXPR_P (decl))
4115 return true;
4116 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4117 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4120 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4121 called from grokfndecl and grokvardecl; in all modes it is called from
4122 cp_write_global_declarations. */
4124 void
4125 no_linkage_error (tree decl)
4127 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4128 /* In C++11 it's ok if the decl is defined. */
4129 return;
4130 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4131 if (t == NULL_TREE)
4132 /* The type that got us on no_linkage_decls must have gotten a name for
4133 linkage purposes. */;
4134 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4135 /* The type might end up having a typedef name for linkage purposes. */
4136 vec_safe_push (no_linkage_decls, decl);
4137 else if (TYPE_ANONYMOUS_P (t))
4139 bool d = false;
4140 if (cxx_dialect >= cxx11)
4141 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4142 "anonymous type, is used but never defined", decl);
4143 else if (DECL_EXTERN_C_P (decl))
4144 /* Allow this; it's pretty common in C. */;
4145 else if (TREE_CODE (decl) == VAR_DECL)
4146 /* DRs 132, 319 and 389 seem to indicate types with
4147 no linkage can only be used to declare extern "C"
4148 entities. Since it's not always an error in the
4149 ISO C++ 90 Standard, we only issue a warning. */
4150 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4151 "with no linkage used to declare variable %q#D with "
4152 "linkage", decl);
4153 else
4154 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4155 "linkage used to declare function %q#D with linkage",
4156 decl);
4157 if (d && is_typedef_decl (TYPE_NAME (t)))
4158 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4159 "to the unqualified type, so it is not used for linkage",
4160 TYPE_NAME (t));
4162 else if (cxx_dialect >= cxx11)
4163 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4164 "%qT, is used but never defined", decl, t);
4165 else if (TREE_CODE (decl) == VAR_DECL)
4166 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4167 "used to declare variable %q#D with linkage", t, decl);
4168 else
4169 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4170 "to declare function %q#D with linkage", t, decl);
4173 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4175 static void
4176 collect_all_refs (const char *source_file)
4178 collect_ada_namespace (global_namespace, source_file);
4181 /* Clear DECL_EXTERNAL for NODE. */
4183 static bool
4184 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4186 DECL_EXTERNAL (node->decl) = 0;
4187 return false;
4190 /* Build up the function to run dynamic initializers for thread_local
4191 variables in this translation unit and alias the init functions for the
4192 individual variables to it. */
4194 static void
4195 handle_tls_init (void)
4197 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4198 if (vars == NULL_TREE)
4199 return;
4201 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4203 write_out_vars (vars);
4205 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4206 boolean_type_node);
4207 TREE_PUBLIC (guard) = false;
4208 TREE_STATIC (guard) = true;
4209 DECL_ARTIFICIAL (guard) = true;
4210 DECL_IGNORED_P (guard) = true;
4211 TREE_USED (guard) = true;
4212 set_decl_tls_model (guard, decl_default_tls_model (guard));
4213 pushdecl_top_level_and_finish (guard, NULL_TREE);
4215 tree fn = get_local_tls_init_fn ();
4216 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4217 tree body = begin_function_body ();
4218 tree if_stmt = begin_if_stmt ();
4219 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4220 tf_warning_or_error);
4221 finish_if_stmt_cond (cond, if_stmt);
4222 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4223 tf_warning_or_error));
4224 for (; vars; vars = TREE_CHAIN (vars))
4226 tree var = TREE_VALUE (vars);
4227 tree init = TREE_PURPOSE (vars);
4228 one_static_initialization_or_destruction (var, init, true);
4230 #ifdef ASM_OUTPUT_DEF
4231 /* Output init aliases even with -fno-extern-tls-init. */
4232 if (TREE_PUBLIC (var))
4234 tree single_init_fn = get_tls_init_fn (var);
4235 if (single_init_fn == NULL_TREE)
4236 continue;
4237 cgraph_node *alias
4238 = cgraph_node::get_create (fn)->create_same_body_alias
4239 (single_init_fn, fn);
4240 gcc_assert (alias != NULL);
4242 #endif
4245 finish_then_clause (if_stmt);
4246 finish_if_stmt (if_stmt);
4247 finish_function_body (body);
4248 expand_or_defer_fn (finish_function (0));
4251 /* The entire file is now complete. If requested, dump everything
4252 to a file. */
4254 static void
4255 dump_tu (void)
4257 int flags;
4258 FILE *stream = dump_begin (TDI_tu, &flags);
4260 if (stream)
4262 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4263 dump_end (TDI_tu, stream);
4267 /* This routine is called at the end of compilation.
4268 Its job is to create all the code needed to initialize and
4269 destroy the global aggregates. We do the destruction
4270 first, since that way we only need to reverse the decls once. */
4272 void
4273 cp_write_global_declarations (void)
4275 tree vars;
4276 bool reconsider;
4277 size_t i;
4278 location_t locus;
4279 unsigned ssdf_count = 0;
4280 int retries = 0;
4281 tree decl;
4282 hash_set<tree> *candidates;
4284 locus = input_location;
4285 at_eof = 1;
4287 /* Bad parse errors. Just forget about it. */
4288 if (! global_bindings_p () || current_class_type
4289 || !vec_safe_is_empty (decl_namespace_list))
4290 return;
4292 /* This is the point to write out a PCH if we're doing that.
4293 In that case we do not want to do anything else. */
4294 if (pch_file)
4296 c_common_write_pch ();
4297 dump_tu ();
4298 return;
4301 cgraph_process_same_body_aliases ();
4303 /* Handle -fdump-ada-spec[-slim] */
4304 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4306 if (flag_dump_ada_spec_slim)
4307 collect_source_ref (main_input_filename);
4308 else
4309 collect_source_refs (global_namespace);
4311 dump_ada_specs (collect_all_refs, cpp_check);
4314 /* FIXME - huh? was input_line -= 1;*/
4316 timevar_start (TV_PHASE_DEFERRED);
4318 /* We now have to write out all the stuff we put off writing out.
4319 These include:
4321 o Template specializations that we have not yet instantiated,
4322 but which are needed.
4323 o Initialization and destruction for non-local objects with
4324 static storage duration. (Local objects with static storage
4325 duration are initialized when their scope is first entered,
4326 and are cleaned up via atexit.)
4327 o Virtual function tables.
4329 All of these may cause others to be needed. For example,
4330 instantiating one function may cause another to be needed, and
4331 generating the initializer for an object may cause templates to be
4332 instantiated, etc., etc. */
4334 emit_support_tinfos ();
4338 tree t;
4339 tree decl;
4341 reconsider = false;
4343 /* If there are templates that we've put off instantiating, do
4344 them now. */
4345 instantiate_pending_templates (retries);
4346 ggc_collect ();
4348 /* Write out virtual tables as required. Note that writing out
4349 the virtual table for a template class may cause the
4350 instantiation of members of that class. If we write out
4351 vtables then we remove the class from our list so we don't
4352 have to look at it again. */
4354 while (keyed_classes != NULL_TREE
4355 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4357 reconsider = true;
4358 keyed_classes = TREE_CHAIN (keyed_classes);
4361 t = keyed_classes;
4362 if (t != NULL_TREE)
4364 tree next = TREE_CHAIN (t);
4366 while (next)
4368 if (maybe_emit_vtables (TREE_VALUE (next)))
4370 reconsider = true;
4371 TREE_CHAIN (t) = TREE_CHAIN (next);
4373 else
4374 t = next;
4376 next = TREE_CHAIN (t);
4380 /* Write out needed type info variables. We have to be careful
4381 looping through unemitted decls, because emit_tinfo_decl may
4382 cause other variables to be needed. New elements will be
4383 appended, and we remove from the vector those that actually
4384 get emitted. */
4385 for (i = unemitted_tinfo_decls->length ();
4386 unemitted_tinfo_decls->iterate (--i, &t);)
4387 if (emit_tinfo_decl (t))
4389 reconsider = true;
4390 unemitted_tinfo_decls->unordered_remove (i);
4393 /* The list of objects with static storage duration is built up
4394 in reverse order. We clear STATIC_AGGREGATES so that any new
4395 aggregates added during the initialization of these will be
4396 initialized in the correct order when we next come around the
4397 loop. */
4398 vars = prune_vars_needing_no_initialization (&static_aggregates);
4400 if (vars)
4402 /* We need to start a new initialization function each time
4403 through the loop. That's because we need to know which
4404 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4405 isn't computed until a function is finished, and written
4406 out. That's a deficiency in the back end. When this is
4407 fixed, these initialization functions could all become
4408 inline, with resulting performance improvements. */
4409 tree ssdf_body;
4411 /* Set the line and file, so that it is obviously not from
4412 the source file. */
4413 input_location = locus;
4414 ssdf_body = start_static_storage_duration_function (ssdf_count);
4416 /* Make sure the back end knows about all the variables. */
4417 write_out_vars (vars);
4419 /* First generate code to do all the initializations. */
4420 if (vars)
4421 do_static_initialization_or_destruction (vars, /*initp=*/true);
4423 /* Then, generate code to do all the destructions. Do these
4424 in reverse order so that the most recently constructed
4425 variable is the first destroyed. If we're using
4426 __cxa_atexit, then we don't need to do this; functions
4427 were registered at initialization time to destroy the
4428 local statics. */
4429 if (!flag_use_cxa_atexit && vars)
4431 vars = nreverse (vars);
4432 do_static_initialization_or_destruction (vars, /*initp=*/false);
4434 else
4435 vars = NULL_TREE;
4437 /* Finish up the static storage duration function for this
4438 round. */
4439 input_location = locus;
4440 finish_static_storage_duration_function (ssdf_body);
4442 /* All those initializations and finalizations might cause
4443 us to need more inline functions, more template
4444 instantiations, etc. */
4445 reconsider = true;
4446 ssdf_count++;
4447 /* ??? was: locus.line++; */
4450 /* Now do the same for thread_local variables. */
4451 handle_tls_init ();
4453 /* Go through the set of inline functions whose bodies have not
4454 been emitted yet. If out-of-line copies of these functions
4455 are required, emit them. */
4456 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4458 /* Does it need synthesizing? */
4459 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4460 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4462 /* Even though we're already at the top-level, we push
4463 there again. That way, when we pop back a few lines
4464 hence, all of our state is restored. Otherwise,
4465 finish_function doesn't clean things up, and we end
4466 up with CURRENT_FUNCTION_DECL set. */
4467 push_to_top_level ();
4468 /* The decl's location will mark where it was first
4469 needed. Save that so synthesize method can indicate
4470 where it was needed from, in case of error */
4471 input_location = DECL_SOURCE_LOCATION (decl);
4472 synthesize_method (decl);
4473 pop_from_top_level ();
4474 reconsider = true;
4477 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4478 generate_tls_wrapper (decl);
4480 if (!DECL_SAVED_TREE (decl))
4481 continue;
4483 /* We lie to the back end, pretending that some functions
4484 are not defined when they really are. This keeps these
4485 functions from being put out unnecessarily. But, we must
4486 stop lying when the functions are referenced, or if they
4487 are not comdat since they need to be put out now. If
4488 DECL_INTERFACE_KNOWN, then we have already set
4489 DECL_EXTERNAL appropriately, so there's no need to check
4490 again, and we do not want to clear DECL_EXTERNAL if a
4491 previous call to import_export_decl set it.
4493 This is done in a separate for cycle, because if some
4494 deferred function is contained in another deferred
4495 function later in deferred_fns varray,
4496 rest_of_compilation would skip this function and we
4497 really cannot expand the same function twice. */
4498 import_export_decl (decl);
4499 if (DECL_NOT_REALLY_EXTERN (decl)
4500 && DECL_INITIAL (decl)
4501 && decl_needed_p (decl))
4503 struct cgraph_node *node, *next;
4505 node = cgraph_node::get (decl);
4506 if (node->cpp_implicit_alias)
4507 node = node->get_alias_target ();
4509 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4510 NULL, true);
4511 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4512 group, we need to mark all symbols in the same comdat group
4513 that way. */
4514 if (node->same_comdat_group)
4515 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4516 next != node;
4517 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4518 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4519 NULL, true);
4522 /* If we're going to need to write this function out, and
4523 there's already a body for it, create RTL for it now.
4524 (There might be no body if this is a method we haven't
4525 gotten around to synthesizing yet.) */
4526 if (!DECL_EXTERNAL (decl)
4527 && decl_needed_p (decl)
4528 && !TREE_ASM_WRITTEN (decl)
4529 && !cgraph_node::get (decl)->definition)
4531 /* We will output the function; no longer consider it in this
4532 loop. */
4533 DECL_DEFER_OUTPUT (decl) = 0;
4534 /* Generate RTL for this function now that we know we
4535 need it. */
4536 expand_or_defer_fn (decl);
4537 /* If we're compiling -fsyntax-only pretend that this
4538 function has been written out so that we don't try to
4539 expand it again. */
4540 if (flag_syntax_only)
4541 TREE_ASM_WRITTEN (decl) = 1;
4542 reconsider = true;
4546 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4547 reconsider = true;
4549 /* Static data members are just like namespace-scope globals. */
4550 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4552 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4553 /* Don't write it out if we haven't seen a definition. */
4554 || DECL_IN_AGGR_P (decl))
4555 continue;
4556 import_export_decl (decl);
4557 /* If this static data member is needed, provide it to the
4558 back end. */
4559 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4560 DECL_EXTERNAL (decl) = 0;
4562 if (vec_safe_length (pending_statics) != 0
4563 && wrapup_global_declarations (pending_statics->address (),
4564 pending_statics->length ()))
4565 reconsider = true;
4567 retries++;
4569 while (reconsider);
4571 /* All used inline functions must have a definition at this point. */
4572 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4574 if (/* Check online inline functions that were actually used. */
4575 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4576 /* If the definition actually was available here, then the
4577 fact that the function was not defined merely represents
4578 that for some reason (use of a template repository,
4579 #pragma interface, etc.) we decided not to emit the
4580 definition here. */
4581 && !DECL_INITIAL (decl)
4582 /* Don't complain if the template was defined. */
4583 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4584 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4585 (template_for_substitution (decl)))))
4587 warning (0, "inline function %q+D used but never defined", decl);
4588 /* Avoid a duplicate warning from check_global_declaration_1. */
4589 TREE_NO_WARNING (decl) = 1;
4593 /* So must decls that use a type with no linkage. */
4594 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4595 no_linkage_error (decl);
4597 /* Then, do the Objective-C stuff. This is where all the
4598 Objective-C module stuff gets generated (symtab,
4599 class/protocol/selector lists etc). This must be done after C++
4600 templates, destructors etc. so that selectors used in C++
4601 templates are properly allocated. */
4602 if (c_dialect_objc ())
4603 objc_write_global_declarations ();
4605 /* We give C linkage to static constructors and destructors. */
4606 push_lang_context (lang_name_c);
4608 /* Generate initialization and destruction functions for all
4609 priorities for which they are required. */
4610 if (priority_info_map)
4611 splay_tree_foreach (priority_info_map,
4612 generate_ctor_and_dtor_functions_for_priority,
4613 /*data=*/&locus);
4614 else if (c_dialect_objc () && objc_static_init_needed_p ())
4615 /* If this is obj-c++ and we need a static init, call
4616 generate_ctor_or_dtor_function. */
4617 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4618 DEFAULT_INIT_PRIORITY, &locus);
4620 /* We're done with the splay-tree now. */
4621 if (priority_info_map)
4622 splay_tree_delete (priority_info_map);
4624 /* Generate any missing aliases. */
4625 maybe_apply_pending_pragma_weaks ();
4627 /* We're done with static constructors, so we can go back to "C++"
4628 linkage now. */
4629 pop_lang_context ();
4631 /* Collect candidates for Java hidden aliases. */
4632 candidates = collect_candidates_for_java_method_aliases ();
4634 timevar_stop (TV_PHASE_DEFERRED);
4635 timevar_start (TV_PHASE_OPT_GEN);
4637 if (flag_vtable_verify)
4639 vtv_recover_class_info ();
4640 vtv_compute_class_hierarchy_transitive_closure ();
4641 vtv_build_vtable_verify_fndecl ();
4644 finalize_compilation_unit ();
4646 if (flag_vtable_verify)
4648 /* Generate the special constructor initialization function that
4649 calls __VLTRegisterPairs, and give it a very high
4650 initialization priority. This must be done after
4651 finalize_compilation_unit so that we have accurate
4652 information about which vtable will actually be emitted. */
4653 vtv_generate_init_routine ();
4656 timevar_stop (TV_PHASE_OPT_GEN);
4657 timevar_start (TV_PHASE_CHECK_DBGINFO);
4659 /* Now, issue warnings about static, but not defined, functions,
4660 etc., and emit debugging information. */
4661 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4662 if (vec_safe_length (pending_statics) != 0)
4664 check_global_declarations (pending_statics->address (),
4665 pending_statics->length ());
4666 emit_debug_global_declarations (pending_statics->address (),
4667 pending_statics->length ());
4670 perform_deferred_noexcept_checks ();
4672 /* Generate hidden aliases for Java. */
4673 if (candidates)
4675 build_java_method_aliases (candidates);
4676 delete candidates;
4679 finish_repo ();
4681 /* The entire file is now complete. If requested, dump everything
4682 to a file. */
4683 dump_tu ();
4685 if (flag_detailed_statistics)
4687 dump_tree_statistics ();
4688 dump_time_statistics ();
4690 input_location = locus;
4692 #ifdef ENABLE_CHECKING
4693 validate_conversion_obstack ();
4694 #endif /* ENABLE_CHECKING */
4696 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4699 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4700 function to call in parse-tree form; it has not yet been
4701 semantically analyzed. ARGS are the arguments to the function.
4702 They have already been semantically analyzed. This may change
4703 ARGS. */
4705 tree
4706 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4707 tsubst_flags_t complain)
4709 tree orig_fn;
4710 vec<tree, va_gc> *orig_args = NULL;
4711 tree expr;
4712 tree object;
4714 orig_fn = fn;
4715 object = TREE_OPERAND (fn, 0);
4717 if (processing_template_decl)
4719 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4720 || TREE_CODE (fn) == MEMBER_REF);
4721 if (type_dependent_expression_p (fn)
4722 || any_type_dependent_arguments_p (*args))
4723 return build_nt_call_vec (fn, *args);
4725 orig_args = make_tree_vector_copy (*args);
4727 /* Transform the arguments and add the implicit "this"
4728 parameter. That must be done before the FN is transformed
4729 because we depend on the form of FN. */
4730 make_args_non_dependent (*args);
4731 object = build_non_dependent_expr (object);
4732 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4734 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4735 object = cp_build_addr_expr (object, complain);
4736 vec_safe_insert (*args, 0, object);
4738 /* Now that the arguments are done, transform FN. */
4739 fn = build_non_dependent_expr (fn);
4742 /* A qualified name corresponding to a bound pointer-to-member is
4743 represented as an OFFSET_REF:
4745 struct B { void g(); };
4746 void (B::*p)();
4747 void B::g() { (this->*p)(); } */
4748 if (TREE_CODE (fn) == OFFSET_REF)
4750 tree object_addr = cp_build_addr_expr (object, complain);
4751 fn = TREE_OPERAND (fn, 1);
4752 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4753 complain);
4754 vec_safe_insert (*args, 0, object_addr);
4757 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4758 expr = build_op_call (fn, args, complain);
4759 else
4760 expr = cp_build_function_call_vec (fn, args, complain);
4761 if (processing_template_decl && expr != error_mark_node)
4762 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4764 if (orig_args != NULL)
4765 release_tree_vector (orig_args);
4767 return expr;
4771 void
4772 check_default_args (tree x)
4774 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4775 bool saw_def = false;
4776 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4777 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4779 if (TREE_PURPOSE (arg))
4780 saw_def = true;
4781 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4783 error ("default argument missing for parameter %P of %q+#D", i, x);
4784 TREE_PURPOSE (arg) = error_mark_node;
4789 /* Return true if function DECL can be inlined. This is used to force
4790 instantiation of methods that might be interesting for inlining. */
4791 bool
4792 possibly_inlined_p (tree decl)
4794 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4795 if (DECL_UNINLINABLE (decl))
4796 return false;
4797 if (!optimize || pragma_java_exceptions)
4798 return DECL_DECLARED_INLINE_P (decl);
4799 /* When optimizing, we might inline everything when flatten
4800 attribute or heuristics inlining for size or autoinlining
4801 is used. */
4802 return true;
4805 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4806 If DECL is a specialization or implicitly declared class member,
4807 generate the actual definition. Return false if something goes
4808 wrong, true otherwise. */
4810 bool
4811 mark_used (tree decl, tsubst_flags_t complain)
4813 /* If DECL is a BASELINK for a single function, then treat it just
4814 like the DECL for the function. Otherwise, if the BASELINK is
4815 for an overloaded function, we don't know which function was
4816 actually used until after overload resolution. */
4817 if (BASELINK_P (decl))
4819 decl = BASELINK_FUNCTIONS (decl);
4820 if (really_overloaded_fn (decl))
4821 return true;
4822 decl = OVL_CURRENT (decl);
4825 /* Set TREE_USED for the benefit of -Wunused. */
4826 TREE_USED (decl) = 1;
4827 if (DECL_CLONED_FUNCTION_P (decl))
4828 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4830 /* Mark enumeration types as used. */
4831 if (TREE_CODE (decl) == CONST_DECL)
4832 used_types_insert (DECL_CONTEXT (decl));
4834 if (TREE_CODE (decl) == FUNCTION_DECL)
4835 maybe_instantiate_noexcept (decl);
4837 if (TREE_CODE (decl) == FUNCTION_DECL
4838 && DECL_DELETED_FN (decl))
4840 if (DECL_ARTIFICIAL (decl))
4842 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4843 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4845 /* We mark a lambda conversion op as deleted if we can't
4846 generate it properly; see maybe_add_lambda_conv_op. */
4847 sorry ("converting lambda which uses %<...%> to "
4848 "function pointer");
4849 return false;
4852 if (complain & tf_error)
4854 error ("use of deleted function %qD", decl);
4855 if (!maybe_explain_implicit_delete (decl))
4856 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4858 return false;
4861 /* We can only check DECL_ODR_USED on variables or functions with
4862 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4863 might need special handling for. */
4864 if (!VAR_OR_FUNCTION_DECL_P (decl)
4865 || DECL_LANG_SPECIFIC (decl) == NULL
4866 || DECL_THUNK_P (decl))
4868 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4870 if (complain & tf_error)
4871 error ("use of %qD before deduction of %<auto%>", decl);
4872 return false;
4874 return true;
4877 /* We only want to do this processing once. We don't need to keep trying
4878 to instantiate inline templates, because unit-at-a-time will make sure
4879 we get them compiled before functions that want to inline them. */
4880 if (DECL_ODR_USED (decl))
4881 return true;
4883 /* If within finish_function, defer the rest until that function
4884 finishes, otherwise it might recurse. */
4885 if (defer_mark_used_calls)
4887 vec_safe_push (deferred_mark_used_calls, decl);
4888 return true;
4891 /* Normally, we can wait until instantiation-time to synthesize DECL.
4892 However, if DECL is a static data member initialized with a constant
4893 or a constexpr function, we need it right now because a reference to
4894 such a data member or a call to such function is not value-dependent.
4895 For a function that uses auto in the return type, we need to instantiate
4896 it to find out its type. For OpenMP user defined reductions, we need
4897 them instantiated for reduction clauses which inline them by hand
4898 directly. */
4899 if (DECL_LANG_SPECIFIC (decl)
4900 && DECL_TEMPLATE_INFO (decl)
4901 && (decl_maybe_constant_var_p (decl)
4902 || (TREE_CODE (decl) == FUNCTION_DECL
4903 && (DECL_DECLARED_CONSTEXPR_P (decl)
4904 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4905 || undeduced_auto_decl (decl))
4906 && !uses_template_parms (DECL_TI_ARGS (decl)))
4908 /* Instantiating a function will result in garbage collection. We
4909 must treat this situation as if we were within the body of a
4910 function so as to avoid collecting live data only referenced from
4911 the stack (such as overload resolution candidates). */
4912 ++function_depth;
4913 instantiate_decl (decl, /*defer_ok=*/false,
4914 /*expl_inst_class_mem_p=*/false);
4915 --function_depth;
4918 if (processing_template_decl)
4919 return true;
4921 /* Check this too in case we're within fold_non_dependent_expr. */
4922 if (DECL_TEMPLATE_INFO (decl)
4923 && uses_template_parms (DECL_TI_ARGS (decl)))
4924 return true;
4926 require_deduced_type (decl);
4928 /* If we don't need a value, then we don't need to synthesize DECL. */
4929 if (cp_unevaluated_operand != 0)
4930 return true;
4932 DECL_ODR_USED (decl) = 1;
4933 if (DECL_CLONED_FUNCTION_P (decl))
4934 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4936 /* DR 757: A type without linkage shall not be used as the type of a
4937 variable or function with linkage, unless
4938 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4939 o the variable or function is not used (3.2 [basic.def.odr]) or is
4940 defined in the same translation unit. */
4941 if (cxx_dialect > cxx98
4942 && decl_linkage (decl) != lk_none
4943 && !DECL_EXTERN_C_P (decl)
4944 && !DECL_ARTIFICIAL (decl)
4945 && !decl_defined_p (decl)
4946 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4948 if (is_local_extern (decl))
4949 /* There's no way to define a local extern, and adding it to
4950 the vector interferes with GC, so give an error now. */
4951 no_linkage_error (decl);
4952 else
4953 vec_safe_push (no_linkage_decls, decl);
4956 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4957 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4958 /* Remember it, so we can check it was defined. */
4959 note_vague_linkage_fn (decl);
4961 /* Is it a synthesized method that needs to be synthesized? */
4962 if (TREE_CODE (decl) == FUNCTION_DECL
4963 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4964 && DECL_DEFAULTED_FN (decl)
4965 /* A function defaulted outside the class is synthesized either by
4966 cp_finish_decl or instantiate_decl. */
4967 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
4968 && ! DECL_INITIAL (decl))
4970 /* Defer virtual destructors so that thunks get the right
4971 linkage. */
4972 if (DECL_VIRTUAL_P (decl) && !at_eof)
4974 note_vague_linkage_fn (decl);
4975 return true;
4978 /* Remember the current location for a function we will end up
4979 synthesizing. Then we can inform the user where it was
4980 required in the case of error. */
4981 DECL_SOURCE_LOCATION (decl) = input_location;
4983 /* Synthesizing an implicitly defined member function will result in
4984 garbage collection. We must treat this situation as if we were
4985 within the body of a function so as to avoid collecting live data
4986 on the stack (such as overload resolution candidates).
4988 We could just let cp_write_global_declarations handle synthesizing
4989 this function by adding it to deferred_fns, but doing
4990 it at the use site produces better error messages. */
4991 ++function_depth;
4992 synthesize_method (decl);
4993 --function_depth;
4994 /* If this is a synthesized method we don't need to
4995 do the instantiation test below. */
4997 else if (VAR_OR_FUNCTION_DECL_P (decl)
4998 && DECL_TEMPLATE_INFO (decl)
4999 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5000 || always_instantiate_p (decl)))
5001 /* If this is a function or variable that is an instance of some
5002 template, we now know that we will need to actually do the
5003 instantiation. We check that DECL is not an explicit
5004 instantiation because that is not checked in instantiate_decl.
5006 We put off instantiating functions in order to improve compile
5007 times. Maintaining a stack of active functions is expensive,
5008 and the inliner knows to instantiate any functions it might
5009 need. Therefore, we always try to defer instantiation. */
5011 ++function_depth;
5012 instantiate_decl (decl, /*defer_ok=*/true,
5013 /*expl_inst_class_mem_p=*/false);
5014 --function_depth;
5017 return true;
5020 bool
5021 mark_used (tree decl)
5023 return mark_used (decl, tf_warning_or_error);
5026 tree
5027 vtv_start_verification_constructor_init_function (void)
5029 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5032 tree
5033 vtv_finish_verification_constructor_init_function (tree function_body)
5035 tree fn;
5037 finish_compound_stmt (function_body);
5038 fn = finish_function (0);
5039 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5040 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5042 return fn;
5045 #include "gt-cp-decl2.h"