* decl2.c (collect_candidates_for_java_method_aliases): Remove.
[official-gcc.git] / gcc / cp / decl2.c
blobdcc5e1f10152b9d196b409d8d6220503e1c65941
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2015 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 "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "stringpool.h"
44 #include "varasm.h"
45 #include "attribs.h"
46 #include "stor-layout.h"
47 #include "calls.h"
48 #include "flags.h"
49 #include "cp-tree.h"
50 #include "decl.h"
51 #include "toplev.h"
52 #include "timevar.h"
53 #include "cpplib.h"
54 #include "target.h"
55 #include "c-family/c-common.h"
56 #include "c-family/c-objc.h"
57 #include "hash-map.h"
58 #include "is-a.h"
59 #include "plugin-api.h"
60 #include "hard-reg-set.h"
61 #include "input.h"
62 #include "function.h"
63 #include "ipa-ref.h"
64 #include "cgraph.h"
65 #include "tree-inline.h"
66 #include "c-family/c-pragma.h"
67 #include "dumpfile.h"
68 #include "intl.h"
69 #include "splay-tree.h"
70 #include "langhooks.h"
71 #include "c-family/c-ada-spec.h"
72 #include "asan.h"
74 extern cpp_reader *parse_in;
76 /* This structure contains information about the initializations
77 and/or destructions required for a particular priority level. */
78 typedef struct priority_info_s {
79 /* Nonzero if there have been any initializations at this priority
80 throughout the translation unit. */
81 int initializations_p;
82 /* Nonzero if there have been any destructions at this priority
83 throughout the translation unit. */
84 int destructions_p;
85 } *priority_info;
87 static void mark_vtable_entries (tree);
88 static bool maybe_emit_vtables (tree);
89 static bool acceptable_java_type (tree);
90 static tree start_objects (int, int);
91 static void finish_objects (int, int, tree);
92 static tree start_static_storage_duration_function (unsigned);
93 static void finish_static_storage_duration_function (tree);
94 static priority_info get_priority_info (int);
95 static void do_static_initialization_or_destruction (tree, bool);
96 static void one_static_initialization_or_destruction (tree, tree, bool);
97 static void generate_ctor_or_dtor_function (bool, int, location_t *);
98 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
99 void *);
100 static tree prune_vars_needing_no_initialization (tree *);
101 static void write_out_vars (tree);
102 static void import_export_class (tree);
103 static tree get_guard_bits (tree);
104 static void determine_visibility_from_class (tree, tree);
105 static bool determine_hidden_inline (tree);
106 static bool decl_defined_p (tree);
108 /* A list of static class variables. This is needed, because a
109 static class variable can be declared inside the class without
110 an initializer, and then initialized, statically, outside the class. */
111 static GTY(()) vec<tree, va_gc> *pending_statics;
113 /* A list of functions which were declared inline, but which we
114 may need to emit outline anyway. */
115 static GTY(()) vec<tree, va_gc> *deferred_fns;
117 /* A list of decls that use types with no linkage, which we need to make
118 sure are defined. */
119 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
121 /* Nonzero if we're done parsing and into end-of-file activities. */
123 int at_eof;
126 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
127 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
128 that apply to the function). */
130 tree
131 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
132 cp_ref_qualifier rqual)
134 tree raises;
135 tree attrs;
136 int type_quals;
137 bool late_return_type_p;
139 if (fntype == error_mark_node || ctype == error_mark_node)
140 return error_mark_node;
142 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
143 || TREE_CODE (fntype) == METHOD_TYPE);
145 type_quals = quals & ~TYPE_QUAL_RESTRICT;
146 ctype = cp_build_qualified_type (ctype, type_quals);
147 raises = TYPE_RAISES_EXCEPTIONS (fntype);
148 attrs = TYPE_ATTRIBUTES (fntype);
149 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
150 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
151 (TREE_CODE (fntype) == METHOD_TYPE
152 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
153 : TYPE_ARG_TYPES (fntype)));
154 if (attrs)
155 fntype = cp_build_type_attribute_variant (fntype, attrs);
156 if (rqual)
157 fntype = build_ref_qualified_type (fntype, rqual);
158 if (raises)
159 fntype = build_exception_variant (fntype, raises);
160 if (late_return_type_p)
161 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
163 return fntype;
166 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
167 return type changed to NEW_RET. */
169 tree
170 change_return_type (tree new_ret, tree fntype)
172 tree newtype;
173 tree args = TYPE_ARG_TYPES (fntype);
174 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
175 tree attrs = TYPE_ATTRIBUTES (fntype);
176 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
178 if (new_ret == error_mark_node)
179 return fntype;
181 if (same_type_p (new_ret, TREE_TYPE (fntype)))
182 return fntype;
184 if (TREE_CODE (fntype) == FUNCTION_TYPE)
186 newtype = build_function_type (new_ret, args);
187 newtype = apply_memfn_quals (newtype,
188 type_memfn_quals (fntype),
189 type_memfn_rqual (fntype));
191 else
192 newtype = build_method_type_directly
193 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
194 if (raises)
195 newtype = build_exception_variant (newtype, raises);
196 if (attrs)
197 newtype = cp_build_type_attribute_variant (newtype, attrs);
198 if (late_return_type_p)
199 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
201 return newtype;
204 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
205 appropriately. */
207 tree
208 cp_build_parm_decl (tree name, tree type)
210 tree parm = build_decl (input_location,
211 PARM_DECL, name, type);
212 /* DECL_ARG_TYPE is only used by the back end and the back end never
213 sees templates. */
214 if (!processing_template_decl)
215 DECL_ARG_TYPE (parm) = type_passed_as (type);
217 return parm;
220 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
221 indicated NAME. */
223 tree
224 build_artificial_parm (tree name, tree type)
226 tree parm = cp_build_parm_decl (name, type);
227 DECL_ARTIFICIAL (parm) = 1;
228 /* All our artificial parms are implicitly `const'; they cannot be
229 assigned to. */
230 TREE_READONLY (parm) = 1;
231 return parm;
234 /* Constructors for types with virtual baseclasses need an "in-charge" flag
235 saying whether this constructor is responsible for initialization of
236 virtual baseclasses or not. All destructors also need this "in-charge"
237 flag, which additionally determines whether or not the destructor should
238 free the memory for the object.
240 This function adds the "in-charge" flag to member function FN if
241 appropriate. It is called from grokclassfn and tsubst.
242 FN must be either a constructor or destructor.
244 The in-charge flag follows the 'this' parameter, and is followed by the
245 VTT parm (if any), then the user-written parms. */
247 void
248 maybe_retrofit_in_chrg (tree fn)
250 tree basetype, arg_types, parms, parm, fntype;
252 /* If we've already add the in-charge parameter don't do it again. */
253 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
254 return;
256 /* When processing templates we can't know, in general, whether or
257 not we're going to have virtual baseclasses. */
258 if (processing_template_decl)
259 return;
261 /* We don't need an in-charge parameter for constructors that don't
262 have virtual bases. */
263 if (DECL_CONSTRUCTOR_P (fn)
264 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
265 return;
267 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
268 basetype = TREE_TYPE (TREE_VALUE (arg_types));
269 arg_types = TREE_CHAIN (arg_types);
271 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
273 /* If this is a subobject constructor or destructor, our caller will
274 pass us a pointer to our VTT. */
275 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
277 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
279 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
280 DECL_CHAIN (parm) = parms;
281 parms = parm;
283 /* ...and then to TYPE_ARG_TYPES. */
284 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
286 DECL_HAS_VTT_PARM_P (fn) = 1;
289 /* Then add the in-charge parm (before the VTT parm). */
290 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
291 DECL_CHAIN (parm) = parms;
292 parms = parm;
293 arg_types = hash_tree_chain (integer_type_node, arg_types);
295 /* Insert our new parameter(s) into the list. */
296 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
298 /* And rebuild the function type. */
299 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
300 arg_types);
301 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
302 fntype = build_exception_variant (fntype,
303 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
304 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
305 fntype = (cp_build_type_attribute_variant
306 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
307 TREE_TYPE (fn) = fntype;
309 /* Now we've got the in-charge parameter. */
310 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
313 /* Classes overload their constituent function names automatically.
314 When a function name is declared in a record structure,
315 its name is changed to it overloaded name. Since names for
316 constructors and destructors can conflict, we place a leading
317 '$' for destructors.
319 CNAME is the name of the class we are grokking for.
321 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
323 FLAGS contains bits saying what's special about today's
324 arguments. DTOR_FLAG == DESTRUCTOR.
326 If FUNCTION is a destructor, then we must add the `auto-delete' field
327 as a second parameter. There is some hair associated with the fact
328 that we must "declare" this variable in the manner consistent with the
329 way the rest of the arguments were declared.
331 QUALS are the qualifiers for the this pointer. */
333 void
334 grokclassfn (tree ctype, tree function, enum overload_flags flags)
336 tree fn_name = DECL_NAME (function);
338 /* Even within an `extern "C"' block, members get C++ linkage. See
339 [dcl.link] for details. */
340 SET_DECL_LANGUAGE (function, lang_cplusplus);
342 if (fn_name == NULL_TREE)
344 error ("name missing for member function");
345 fn_name = get_identifier ("<anonymous>");
346 DECL_NAME (function) = fn_name;
349 DECL_CONTEXT (function) = ctype;
351 if (flags == DTOR_FLAG)
352 DECL_DESTRUCTOR_P (function) = 1;
354 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
355 maybe_retrofit_in_chrg (function);
358 /* Create an ARRAY_REF, checking for the user doing things backwards
359 along the way. DECLTYPE_P is for N3276, as in the parser. */
361 tree
362 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
363 bool decltype_p)
365 tree type;
366 tree expr;
367 tree orig_array_expr = array_expr;
368 tree orig_index_exp = index_exp;
370 if (error_operand_p (array_expr) || error_operand_p (index_exp))
371 return error_mark_node;
373 if (processing_template_decl)
375 if (type_dependent_expression_p (array_expr)
376 || type_dependent_expression_p (index_exp))
377 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
378 NULL_TREE, NULL_TREE);
379 array_expr = build_non_dependent_expr (array_expr);
380 index_exp = build_non_dependent_expr (index_exp);
383 type = TREE_TYPE (array_expr);
384 gcc_assert (type);
385 type = non_reference (type);
387 /* If they have an `operator[]', use that. */
388 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
390 tsubst_flags_t complain = tf_warning_or_error;
391 if (decltype_p)
392 complain |= tf_decltype;
393 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
394 index_exp, NULL_TREE, /*overload=*/NULL, complain);
396 else
398 tree p1, p2, i1, i2;
400 /* Otherwise, create an ARRAY_REF for a pointer or array type.
401 It is a little-known fact that, if `a' is an array and `i' is
402 an int, you can write `i[a]', which means the same thing as
403 `a[i]'. */
404 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
405 p1 = array_expr;
406 else
407 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
409 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
410 p2 = index_exp;
411 else
412 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
414 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
415 false);
416 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
417 false);
419 if ((p1 && i2) && (i1 && p2))
420 error ("ambiguous conversion for array subscript");
422 if (p1 && i2)
423 array_expr = p1, index_exp = i2;
424 else if (i1 && p2)
425 array_expr = p2, index_exp = i1;
426 else
428 error ("invalid types %<%T[%T]%> for array subscript",
429 type, TREE_TYPE (index_exp));
430 return error_mark_node;
433 if (array_expr == error_mark_node || index_exp == error_mark_node)
434 error ("ambiguous conversion for array subscript");
436 expr = build_array_ref (input_location, array_expr, index_exp);
438 if (processing_template_decl && expr != error_mark_node)
439 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
440 NULL_TREE, NULL_TREE);
441 return expr;
444 /* Given the cast expression EXP, checking out its validity. Either return
445 an error_mark_node if there was an unavoidable error, return a cast to
446 void for trying to delete a pointer w/ the value 0, or return the
447 call to delete. If DOING_VEC is true, we handle things differently
448 for doing an array delete.
449 Implements ARM $5.3.4. This is called from the parser. */
451 tree
452 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
453 tsubst_flags_t complain)
455 tree t, type;
457 if (exp == error_mark_node)
458 return exp;
460 if (processing_template_decl)
462 t = build_min (DELETE_EXPR, void_type_node, exp, size);
463 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
464 DELETE_EXPR_USE_VEC (t) = doing_vec;
465 TREE_SIDE_EFFECTS (t) = 1;
466 return t;
469 /* An array can't have been allocated by new, so complain. */
470 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
471 warning (0, "deleting array %q#E", exp);
473 t = build_expr_type_conversion (WANT_POINTER, exp, true);
475 if (t == NULL_TREE || t == error_mark_node)
477 error ("type %q#T argument given to %<delete%>, expected pointer",
478 TREE_TYPE (exp));
479 return error_mark_node;
482 type = TREE_TYPE (t);
484 /* As of Valley Forge, you can delete a pointer to const. */
486 /* You can't delete functions. */
487 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
489 error ("cannot delete a function. Only pointer-to-objects are "
490 "valid arguments to %<delete%>");
491 return error_mark_node;
494 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
495 if (VOID_TYPE_P (TREE_TYPE (type)))
497 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
498 doing_vec = 0;
501 /* Deleting a pointer with the value zero is valid and has no effect. */
502 if (integer_zerop (t))
503 return build1 (NOP_EXPR, void_type_node, t);
505 if (doing_vec)
506 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
507 sfk_deleting_destructor,
508 use_global_delete, complain);
509 else
510 return build_delete (type, t, sfk_deleting_destructor,
511 LOOKUP_NORMAL, use_global_delete,
512 complain);
515 /* Report an error if the indicated template declaration is not the
516 sort of thing that should be a member template. */
518 void
519 check_member_template (tree tmpl)
521 tree decl;
523 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
524 decl = DECL_TEMPLATE_RESULT (tmpl);
526 if (TREE_CODE (decl) == FUNCTION_DECL
527 || DECL_ALIAS_TEMPLATE_P (tmpl)
528 || (TREE_CODE (decl) == TYPE_DECL
529 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
531 /* The parser rejects template declarations in local classes
532 (with the exception of generic lambdas). */
533 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
534 /* The parser rejects any use of virtual in a function template. */
535 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
536 && DECL_VIRTUAL_P (decl)));
538 /* The debug-information generating code doesn't know what to do
539 with member templates. */
540 DECL_IGNORED_P (tmpl) = 1;
542 else if (variable_template_p (tmpl))
543 /* OK */;
544 else
545 error ("template declaration of %q#D", decl);
548 /* Return true iff TYPE is a valid Java parameter or return type. */
550 static bool
551 acceptable_java_type (tree type)
553 if (type == error_mark_node)
554 return false;
556 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
557 return true;
558 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
560 type = TREE_TYPE (type);
561 if (TREE_CODE (type) == RECORD_TYPE)
563 tree args; int i;
564 if (! TYPE_FOR_JAVA (type))
565 return false;
566 if (! CLASSTYPE_TEMPLATE_INFO (type))
567 return true;
568 args = CLASSTYPE_TI_ARGS (type);
569 i = TREE_VEC_LENGTH (args);
570 while (--i >= 0)
572 type = TREE_VEC_ELT (args, i);
573 if (TYPE_PTR_P (type))
574 type = TREE_TYPE (type);
575 if (! TYPE_FOR_JAVA (type))
576 return false;
578 return true;
581 return false;
584 /* For a METHOD in a Java class CTYPE, return true if
585 the parameter and return types are valid Java types.
586 Otherwise, print appropriate error messages, and return false. */
588 bool
589 check_java_method (tree method)
591 bool jerr = false;
592 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
593 tree ret_type = TREE_TYPE (TREE_TYPE (method));
595 if (!acceptable_java_type (ret_type))
597 error ("Java method %qD has non-Java return type %qT",
598 method, ret_type);
599 jerr = true;
602 arg_types = TREE_CHAIN (arg_types);
603 if (DECL_HAS_IN_CHARGE_PARM_P (method))
604 arg_types = TREE_CHAIN (arg_types);
605 if (DECL_HAS_VTT_PARM_P (method))
606 arg_types = TREE_CHAIN (arg_types);
608 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
610 tree type = TREE_VALUE (arg_types);
611 if (!acceptable_java_type (type))
613 if (type != error_mark_node)
614 error ("Java method %qD has non-Java parameter type %qT",
615 method, type);
616 jerr = true;
619 return !jerr;
622 /* Sanity check: report error if this function FUNCTION is not
623 really a member of the class (CTYPE) it is supposed to belong to.
624 TEMPLATE_PARMS is used to specify the template parameters of a member
625 template passed as FUNCTION_DECL. If the member template is passed as a
626 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
627 from the declaration. If the function is not a function template, it
628 must be NULL.
629 It returns the original declaration for the function, NULL_TREE if
630 no declaration was found, error_mark_node if an error was emitted. */
632 tree
633 check_classfn (tree ctype, tree function, tree template_parms)
635 int ix;
636 bool is_template;
637 tree pushed_scope;
639 if (DECL_USE_TEMPLATE (function)
640 && !(TREE_CODE (function) == TEMPLATE_DECL
641 && DECL_TEMPLATE_SPECIALIZATION (function))
642 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
643 /* Since this is a specialization of a member template,
644 we're not going to find the declaration in the class.
645 For example, in:
647 struct S { template <typename T> void f(T); };
648 template <> void S::f(int);
650 we're not going to find `S::f(int)', but there's no
651 reason we should, either. We let our callers know we didn't
652 find the method, but we don't complain. */
653 return NULL_TREE;
655 /* Basic sanity check: for a template function, the template parameters
656 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
657 if (TREE_CODE (function) == TEMPLATE_DECL)
659 if (template_parms
660 && !comp_template_parms (template_parms,
661 DECL_TEMPLATE_PARMS (function)))
663 error ("template parameter lists provided don%'t match the "
664 "template parameters of %qD", function);
665 return error_mark_node;
667 template_parms = DECL_TEMPLATE_PARMS (function);
670 /* OK, is this a definition of a member template? */
671 is_template = (template_parms != NULL_TREE);
673 /* [temp.mem]
675 A destructor shall not be a member template. */
676 if (DECL_DESTRUCTOR_P (function) && is_template)
678 error ("destructor %qD declared as member template", function);
679 return error_mark_node;
682 /* We must enter the scope here, because conversion operators are
683 named by target type, and type equivalence relies on typenames
684 resolving within the scope of CTYPE. */
685 pushed_scope = push_scope (ctype);
686 ix = class_method_index_for_fn (complete_type (ctype), function);
687 if (ix >= 0)
689 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
690 tree fndecls, fndecl = 0;
691 bool is_conv_op;
692 const char *format = NULL;
694 for (fndecls = (*methods)[ix];
695 fndecls; fndecls = OVL_NEXT (fndecls))
697 tree p1, p2;
699 fndecl = OVL_CURRENT (fndecls);
700 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
701 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
703 /* We cannot simply call decls_match because this doesn't
704 work for static member functions that are pretending to
705 be methods, and because the name may have been changed by
706 asm("new_name"). */
708 /* Get rid of the this parameter on functions that become
709 static. */
710 if (DECL_STATIC_FUNCTION_P (fndecl)
711 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
712 p1 = TREE_CHAIN (p1);
714 /* A member template definition only matches a member template
715 declaration. */
716 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
717 continue;
719 /* ref-qualifier or absence of same must match. */
720 if (type_memfn_rqual (TREE_TYPE (function))
721 != type_memfn_rqual (TREE_TYPE (fndecl)))
722 continue;
724 /* While finding a match, same types and params are not enough
725 if the function is versioned. Also check version ("target")
726 attributes. */
727 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
728 TREE_TYPE (TREE_TYPE (fndecl)))
729 && compparms (p1, p2)
730 && !targetm.target_option.function_versions (function, fndecl)
731 && (!is_template
732 || comp_template_parms (template_parms,
733 DECL_TEMPLATE_PARMS (fndecl)))
734 && (DECL_TEMPLATE_SPECIALIZATION (function)
735 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
736 && (!DECL_TEMPLATE_SPECIALIZATION (function)
737 || (DECL_TI_TEMPLATE (function)
738 == DECL_TI_TEMPLATE (fndecl))))
739 break;
741 if (fndecls)
743 if (pushed_scope)
744 pop_scope (pushed_scope);
745 return OVL_CURRENT (fndecls);
748 error_at (DECL_SOURCE_LOCATION (function),
749 "prototype for %q#D does not match any in class %qT",
750 function, ctype);
751 is_conv_op = DECL_CONV_FN_P (fndecl);
753 if (is_conv_op)
754 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
755 fndecls = (*methods)[ix];
756 while (fndecls)
758 fndecl = OVL_CURRENT (fndecls);
759 fndecls = OVL_NEXT (fndecls);
761 if (!fndecls && is_conv_op)
763 if (methods->length () > (size_t) ++ix)
765 fndecls = (*methods)[ix];
766 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
768 fndecls = NULL_TREE;
769 is_conv_op = false;
772 else
773 is_conv_op = false;
775 if (format)
776 format = " %+#D";
777 else if (fndecls)
778 format = N_("candidates are: %+#D");
779 else
780 format = N_("candidate is: %+#D");
781 error (format, fndecl);
784 else if (!COMPLETE_TYPE_P (ctype))
785 cxx_incomplete_type_error (function, ctype);
786 else
787 error ("no %q#D member function declared in class %qT",
788 function, ctype);
790 if (pushed_scope)
791 pop_scope (pushed_scope);
792 return error_mark_node;
795 /* DECL is a function with vague linkage. Remember it so that at the
796 end of the translation unit we can decide whether or not to emit
797 it. */
799 void
800 note_vague_linkage_fn (tree decl)
802 DECL_DEFER_OUTPUT (decl) = 1;
803 vec_safe_push (deferred_fns, decl);
806 /* As above, but for variable template instantiations. */
808 void
809 note_variable_template_instantiation (tree decl)
811 vec_safe_push (pending_statics, decl);
814 /* We have just processed the DECL, which is a static data member.
815 The other parameters are as for cp_finish_decl. */
817 void
818 finish_static_data_member_decl (tree decl,
819 tree init, bool init_const_expr_p,
820 tree asmspec_tree,
821 int flags)
823 DECL_CONTEXT (decl) = current_class_type;
825 /* We cannot call pushdecl here, because that would fill in the
826 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
827 the right thing, namely, to put this decl out straight away. */
829 if (! processing_template_decl)
830 vec_safe_push (pending_statics, decl);
832 if (LOCAL_CLASS_P (current_class_type)
833 /* We already complained about the template definition. */
834 && !DECL_TEMPLATE_INSTANTIATION (decl))
835 permerror (input_location, "local class %q#T shall not have static data member %q#D",
836 current_class_type, decl);
837 else
838 for (tree t = current_class_type; TYPE_P (t);
839 t = CP_TYPE_CONTEXT (t))
840 if (TYPE_ANONYMOUS_P (t))
842 if (permerror (DECL_SOURCE_LOCATION (decl),
843 "static data member %qD in unnamed class", decl))
844 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
845 "unnamed class defined here");
846 break;
849 DECL_IN_AGGR_P (decl) = 1;
851 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
852 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
853 SET_VAR_HAD_UNKNOWN_BOUND (decl);
855 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
858 /* DECLARATOR and DECLSPECS correspond to a class member. The other
859 parameters are as for cp_finish_decl. Return the DECL for the
860 class member declared. */
862 tree
863 grokfield (const cp_declarator *declarator,
864 cp_decl_specifier_seq *declspecs,
865 tree init, bool init_const_expr_p,
866 tree asmspec_tree,
867 tree attrlist)
869 tree value;
870 const char *asmspec = 0;
871 int flags;
872 tree name;
874 if (init
875 && TREE_CODE (init) == TREE_LIST
876 && TREE_VALUE (init) == error_mark_node
877 && TREE_CHAIN (init) == NULL_TREE)
878 init = NULL_TREE;
880 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
881 if (! value || value == error_mark_node)
882 /* friend or constructor went bad. */
883 return error_mark_node;
884 if (TREE_TYPE (value) == error_mark_node)
885 return value;
887 if (TREE_CODE (value) == TYPE_DECL && init)
889 error ("typedef %qD is initialized (use decltype instead)", value);
890 init = NULL_TREE;
893 /* Pass friendly classes back. */
894 if (value == void_type_node)
895 return value;
898 name = DECL_NAME (value);
900 if (name != NULL_TREE)
902 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
904 error ("explicit template argument list not allowed");
905 return error_mark_node;
908 if (IDENTIFIER_POINTER (name)[0] == '_'
909 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
910 error ("member %qD conflicts with virtual function table field name",
911 value);
914 /* Stash away type declarations. */
915 if (TREE_CODE (value) == TYPE_DECL)
917 DECL_NONLOCAL (value) = 1;
918 DECL_CONTEXT (value) = current_class_type;
920 if (attrlist)
922 int attrflags = 0;
924 /* If this is a typedef that names the class for linkage purposes
925 (7.1.3p8), apply any attributes directly to the type. */
926 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
927 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
928 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
930 cplus_decl_attributes (&value, attrlist, attrflags);
933 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
934 && TREE_TYPE (value) != error_mark_node
935 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
936 set_underlying_type (value);
938 /* It's important that push_template_decl below follows
939 set_underlying_type above so that the created template
940 carries the properly set type of VALUE. */
941 if (processing_template_decl)
942 value = push_template_decl (value);
944 record_locally_defined_typedef (value);
945 return value;
948 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
950 if (!friendp && DECL_IN_AGGR_P (value))
952 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
953 return void_type_node;
956 if (asmspec_tree && asmspec_tree != error_mark_node)
957 asmspec = TREE_STRING_POINTER (asmspec_tree);
959 if (init)
961 if (TREE_CODE (value) == FUNCTION_DECL)
963 if (init == ridpointers[(int)RID_DELETE])
965 DECL_DELETED_FN (value) = 1;
966 DECL_DECLARED_INLINE_P (value) = 1;
967 DECL_INITIAL (value) = error_mark_node;
969 else if (init == ridpointers[(int)RID_DEFAULT])
971 if (defaultable_fn_check (value))
973 DECL_DEFAULTED_FN (value) = 1;
974 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
975 DECL_DECLARED_INLINE_P (value) = 1;
978 else if (TREE_CODE (init) == DEFAULT_ARG)
979 error ("invalid initializer for member function %qD", value);
980 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
982 if (integer_zerop (init))
983 DECL_PURE_VIRTUAL_P (value) = 1;
984 else if (error_operand_p (init))
985 ; /* An error has already been reported. */
986 else
987 error ("invalid initializer for member function %qD",
988 value);
990 else
992 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
993 if (friendp)
994 error ("initializer specified for friend function %qD",
995 value);
996 else
997 error ("initializer specified for static member function %qD",
998 value);
1001 else if (TREE_CODE (value) == FIELD_DECL)
1002 /* C++11 NSDMI, keep going. */;
1003 else if (!VAR_P (value))
1004 gcc_unreachable ();
1007 /* Pass friend decls back. */
1008 if ((TREE_CODE (value) == FUNCTION_DECL
1009 || TREE_CODE (value) == TEMPLATE_DECL)
1010 && DECL_CONTEXT (value) != current_class_type)
1011 return value;
1013 /* Need to set this before push_template_decl. */
1014 if (TREE_CODE (value) == VAR_DECL)
1015 DECL_CONTEXT (value) = current_class_type;
1017 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1019 value = push_template_decl (value);
1020 if (error_operand_p (value))
1021 return error_mark_node;
1024 if (attrlist)
1025 cplus_decl_attributes (&value, attrlist, 0);
1027 if (init && DIRECT_LIST_INIT_P (init))
1028 flags = LOOKUP_NORMAL;
1029 else
1030 flags = LOOKUP_IMPLICIT;
1032 switch (TREE_CODE (value))
1034 case VAR_DECL:
1035 finish_static_data_member_decl (value, init, init_const_expr_p,
1036 asmspec_tree, flags);
1037 return value;
1039 case FIELD_DECL:
1040 if (asmspec)
1041 error ("%<asm%> specifiers are not permitted on non-static data members");
1042 if (DECL_INITIAL (value) == error_mark_node)
1043 init = error_mark_node;
1044 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1045 NULL_TREE, flags);
1046 DECL_IN_AGGR_P (value) = 1;
1047 return value;
1049 case FUNCTION_DECL:
1050 if (asmspec)
1051 set_user_assembler_name (value, asmspec);
1053 cp_finish_decl (value,
1054 /*init=*/NULL_TREE,
1055 /*init_const_expr_p=*/false,
1056 asmspec_tree, flags);
1058 /* Pass friends back this way. */
1059 if (DECL_FRIEND_P (value))
1060 return void_type_node;
1062 DECL_IN_AGGR_P (value) = 1;
1063 return value;
1065 default:
1066 gcc_unreachable ();
1068 return NULL_TREE;
1071 /* Like `grokfield', but for bitfields.
1072 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1074 tree
1075 grokbitfield (const cp_declarator *declarator,
1076 cp_decl_specifier_seq *declspecs, tree width,
1077 tree attrlist)
1079 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1081 if (value == error_mark_node)
1082 return NULL_TREE; /* friends went bad. */
1083 if (TREE_TYPE (value) == error_mark_node)
1084 return value;
1086 /* Pass friendly classes back. */
1087 if (VOID_TYPE_P (value))
1088 return void_type_node;
1090 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1091 && (POINTER_TYPE_P (value)
1092 || !dependent_type_p (TREE_TYPE (value))))
1094 error ("bit-field %qD with non-integral type", value);
1095 return error_mark_node;
1098 if (TREE_CODE (value) == TYPE_DECL)
1100 error ("cannot declare %qD to be a bit-field type", value);
1101 return NULL_TREE;
1104 /* Usually, finish_struct_1 catches bitfields with invalid types.
1105 But, in the case of bitfields with function type, we confuse
1106 ourselves into thinking they are member functions, so we must
1107 check here. */
1108 if (TREE_CODE (value) == FUNCTION_DECL)
1110 error ("cannot declare bit-field %qD with function type",
1111 DECL_NAME (value));
1112 return NULL_TREE;
1115 if (DECL_IN_AGGR_P (value))
1117 error ("%qD is already defined in the class %qT", value,
1118 DECL_CONTEXT (value));
1119 return void_type_node;
1122 if (TREE_STATIC (value))
1124 error ("static member %qD cannot be a bit-field", value);
1125 return NULL_TREE;
1127 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1129 if (width != error_mark_node)
1131 /* The width must be an integer type. */
1132 if (!type_dependent_expression_p (width)
1133 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1134 error ("width of bit-field %qD has non-integral type %qT", value,
1135 TREE_TYPE (width));
1136 DECL_INITIAL (value) = width;
1137 SET_DECL_C_BIT_FIELD (value);
1140 DECL_IN_AGGR_P (value) = 1;
1142 if (attrlist)
1143 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1145 return value;
1149 /* Returns true iff ATTR is an attribute which needs to be applied at
1150 instantiation time rather than template definition time. */
1152 static bool
1153 is_late_template_attribute (tree attr, tree decl)
1155 tree name = get_attribute_name (attr);
1156 tree args = TREE_VALUE (attr);
1157 const struct attribute_spec *spec = lookup_attribute_spec (name);
1158 tree arg;
1160 if (!spec)
1161 /* Unknown attribute. */
1162 return false;
1164 /* Attribute weak handling wants to write out assembly right away. */
1165 if (is_attribute_p ("weak", name))
1166 return true;
1168 /* Attribute unused is applied directly, as it appertains to
1169 decls. */
1170 if (is_attribute_p ("unused", name))
1171 return false;
1173 /* #pragma omp declare simd attribute needs to be always deferred. */
1174 if (flag_openmp
1175 && is_attribute_p ("omp declare simd", name))
1176 return true;
1178 /* An attribute pack is clearly dependent. */
1179 if (args && PACK_EXPANSION_P (args))
1180 return true;
1182 /* If any of the arguments are dependent expressions, we can't evaluate
1183 the attribute until instantiation time. */
1184 for (arg = args; arg; arg = TREE_CHAIN (arg))
1186 tree t = TREE_VALUE (arg);
1188 /* If the first attribute argument is an identifier, only consider
1189 second and following arguments. Attributes like mode, format,
1190 cleanup and several target specific attributes aren't late
1191 just because they have an IDENTIFIER_NODE as first argument. */
1192 if (arg == args && identifier_p (t))
1193 continue;
1195 if (value_dependent_expression_p (t)
1196 || type_dependent_expression_p (t))
1197 return true;
1200 if (TREE_CODE (decl) == TYPE_DECL
1201 || TYPE_P (decl)
1202 || spec->type_required)
1204 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1206 /* We can't apply any attributes to a completely unknown type until
1207 instantiation time. */
1208 enum tree_code code = TREE_CODE (type);
1209 if (code == TEMPLATE_TYPE_PARM
1210 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1211 || code == TYPENAME_TYPE)
1212 return true;
1213 /* Also defer most attributes on dependent types. This is not
1214 necessary in all cases, but is the better default. */
1215 else if (dependent_type_p (type)
1216 /* But some attributes specifically apply to templates. */
1217 && !is_attribute_p ("abi_tag", name)
1218 && !is_attribute_p ("deprecated", name)
1219 && !is_attribute_p ("visibility", name))
1220 return true;
1221 else
1222 return false;
1224 else
1225 return false;
1228 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1229 applied at instantiation time and return them. If IS_DEPENDENT is true,
1230 the declaration itself is dependent, so all attributes should be applied
1231 at instantiation time. */
1233 static tree
1234 splice_template_attributes (tree *attr_p, tree decl)
1236 tree *p = attr_p;
1237 tree late_attrs = NULL_TREE;
1238 tree *q = &late_attrs;
1240 if (!p)
1241 return NULL_TREE;
1243 for (; *p; )
1245 if (is_late_template_attribute (*p, decl))
1247 ATTR_IS_DEPENDENT (*p) = 1;
1248 *q = *p;
1249 *p = TREE_CHAIN (*p);
1250 q = &TREE_CHAIN (*q);
1251 *q = NULL_TREE;
1253 else
1254 p = &TREE_CHAIN (*p);
1257 return late_attrs;
1260 /* Remove any late attributes from the list in ATTR_P and attach them to
1261 DECL_P. */
1263 static void
1264 save_template_attributes (tree *attr_p, tree *decl_p)
1266 tree *q;
1268 if (attr_p && *attr_p == error_mark_node)
1269 return;
1271 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1272 if (!late_attrs)
1273 return;
1275 if (DECL_P (*decl_p))
1276 q = &DECL_ATTRIBUTES (*decl_p);
1277 else
1278 q = &TYPE_ATTRIBUTES (*decl_p);
1280 tree old_attrs = *q;
1282 /* Merge the late attributes at the beginning with the attribute
1283 list. */
1284 late_attrs = merge_attributes (late_attrs, *q);
1285 *q = late_attrs;
1287 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1289 /* We've added new attributes directly to the main variant, so
1290 now we need to update all of the other variants to include
1291 these new attributes. */
1292 tree variant;
1293 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1294 variant = TYPE_NEXT_VARIANT (variant))
1296 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1297 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1302 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1303 to a typedef which gives a previously anonymous class or enum a name for
1304 linkage purposes. */
1306 bool
1307 attributes_naming_typedef_ok (tree attrs)
1309 for (; attrs; attrs = TREE_CHAIN (attrs))
1311 tree name = get_attribute_name (attrs);
1312 if (is_attribute_p ("vector_size", name))
1313 return false;
1315 return true;
1318 /* Like reconstruct_complex_type, but handle also template trees. */
1320 tree
1321 cp_reconstruct_complex_type (tree type, tree bottom)
1323 tree inner, outer;
1324 bool late_return_type_p = false;
1326 if (TYPE_PTR_P (type))
1328 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1329 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1330 TYPE_REF_CAN_ALIAS_ALL (type));
1332 else if (TREE_CODE (type) == REFERENCE_TYPE)
1334 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1335 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1336 TYPE_REF_CAN_ALIAS_ALL (type));
1338 else if (TREE_CODE (type) == ARRAY_TYPE)
1340 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1341 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1342 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1343 element type qualification will be handled by the recursive
1344 cp_reconstruct_complex_type call and cp_build_qualified_type
1345 for ARRAY_TYPEs changes the element type. */
1346 return outer;
1348 else if (TREE_CODE (type) == FUNCTION_TYPE)
1350 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1351 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1352 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1353 outer = apply_memfn_quals (outer,
1354 type_memfn_quals (type),
1355 type_memfn_rqual (type));
1357 else if (TREE_CODE (type) == METHOD_TYPE)
1359 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1360 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1361 /* The build_method_type_directly() routine prepends 'this' to argument list,
1362 so we must compensate by getting rid of it. */
1363 outer
1364 = build_method_type_directly
1365 (class_of_this_parm (type), inner,
1366 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1368 else if (TREE_CODE (type) == OFFSET_TYPE)
1370 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1371 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1373 else
1374 return bottom;
1376 if (TYPE_ATTRIBUTES (type))
1377 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1378 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1380 if (late_return_type_p)
1381 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1383 return outer;
1386 /* Replaces any constexpr expression that may be into the attributes
1387 arguments with their reduced value. */
1389 static void
1390 cp_check_const_attributes (tree attributes)
1392 if (attributes == error_mark_node)
1393 return;
1395 tree attr;
1396 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1398 tree arg;
1399 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1401 tree expr = TREE_VALUE (arg);
1402 if (EXPR_P (expr))
1403 TREE_VALUE (arg) = maybe_constant_value (expr);
1408 /* Return true if TYPE is an OpenMP mappable type. */
1409 bool
1410 cp_omp_mappable_type (tree type)
1412 /* Mappable type has to be complete. */
1413 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1414 return false;
1415 /* Arrays have mappable type if the elements have mappable type. */
1416 while (TREE_CODE (type) == ARRAY_TYPE)
1417 type = TREE_TYPE (type);
1418 /* A mappable type cannot contain virtual members. */
1419 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1420 return false;
1421 /* All data members must be non-static. */
1422 if (CLASS_TYPE_P (type))
1424 tree field;
1425 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1426 if (TREE_CODE (field) == VAR_DECL)
1427 return false;
1428 /* All fields must have mappable types. */
1429 else if (TREE_CODE (field) == FIELD_DECL
1430 && !cp_omp_mappable_type (TREE_TYPE (field)))
1431 return false;
1433 return true;
1436 /* Like decl_attributes, but handle C++ complexity. */
1438 void
1439 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1441 if (*decl == NULL_TREE || *decl == void_type_node
1442 || *decl == error_mark_node)
1443 return;
1445 /* Add implicit "omp declare target" attribute if requested. */
1446 if (scope_chain->omp_declare_target_attribute
1447 && ((TREE_CODE (*decl) == VAR_DECL
1448 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1449 || TREE_CODE (*decl) == FUNCTION_DECL))
1451 if (TREE_CODE (*decl) == VAR_DECL
1452 && DECL_CLASS_SCOPE_P (*decl))
1453 error ("%q+D static data member inside of declare target directive",
1454 *decl);
1455 else if (TREE_CODE (*decl) == VAR_DECL
1456 && (DECL_FUNCTION_SCOPE_P (*decl)
1457 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1458 error ("%q+D in block scope inside of declare target directive",
1459 *decl);
1460 else if (!processing_template_decl
1461 && TREE_CODE (*decl) == VAR_DECL
1462 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1463 error ("%q+D in declare target directive does not have mappable type",
1464 *decl);
1465 else
1466 attributes = tree_cons (get_identifier ("omp declare target"),
1467 NULL_TREE, attributes);
1470 if (processing_template_decl)
1472 if (check_for_bare_parameter_packs (attributes))
1473 return;
1475 save_template_attributes (&attributes, decl);
1478 cp_check_const_attributes (attributes);
1480 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1481 decl = &DECL_TEMPLATE_RESULT (*decl);
1483 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1485 attributes
1486 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1487 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1488 attributes, flags);
1490 else
1491 decl_attributes (decl, attributes, flags);
1493 if (TREE_CODE (*decl) == TYPE_DECL)
1494 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1496 /* Propagate deprecation out to the template. */
1497 if (TREE_DEPRECATED (*decl))
1498 if (tree ti = get_template_info (*decl))
1500 tree tmpl = TI_TEMPLATE (ti);
1501 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1502 : DECL_TEMPLATE_RESULT (tmpl));
1503 if (*decl == pattern)
1504 TREE_DEPRECATED (tmpl) = true;
1508 /* Walks through the namespace- or function-scope anonymous union
1509 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1510 Returns one of the fields for use in the mangled name. */
1512 static tree
1513 build_anon_union_vars (tree type, tree object)
1515 tree main_decl = NULL_TREE;
1516 tree field;
1518 /* Rather than write the code to handle the non-union case,
1519 just give an error. */
1520 if (TREE_CODE (type) != UNION_TYPE)
1522 error ("anonymous struct not inside named type");
1523 return error_mark_node;
1526 for (field = TYPE_FIELDS (type);
1527 field != NULL_TREE;
1528 field = DECL_CHAIN (field))
1530 tree decl;
1531 tree ref;
1533 if (DECL_ARTIFICIAL (field))
1534 continue;
1535 if (TREE_CODE (field) != FIELD_DECL)
1537 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1538 "have non-static data members", field);
1539 continue;
1542 if (TREE_PRIVATE (field))
1543 permerror (input_location, "private member %q+#D in anonymous union", field);
1544 else if (TREE_PROTECTED (field))
1545 permerror (input_location, "protected member %q+#D in anonymous union", field);
1547 if (processing_template_decl)
1548 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1549 DECL_NAME (field), NULL_TREE);
1550 else
1551 ref = build_class_member_access_expr (object, field, NULL_TREE,
1552 false, tf_warning_or_error);
1554 if (DECL_NAME (field))
1556 tree base;
1558 decl = build_decl (input_location,
1559 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1560 DECL_ANON_UNION_VAR_P (decl) = 1;
1561 DECL_ARTIFICIAL (decl) = 1;
1563 base = get_base_address (object);
1564 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1565 TREE_STATIC (decl) = TREE_STATIC (base);
1566 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1568 SET_DECL_VALUE_EXPR (decl, ref);
1569 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1571 decl = pushdecl (decl);
1573 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1574 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1575 else
1576 decl = 0;
1578 if (main_decl == NULL_TREE)
1579 main_decl = decl;
1582 return main_decl;
1585 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1586 anonymous union, then all members must be laid out together. PUBLIC_P
1587 is nonzero if this union is not declared static. */
1589 void
1590 finish_anon_union (tree anon_union_decl)
1592 tree type;
1593 tree main_decl;
1594 bool public_p;
1596 if (anon_union_decl == error_mark_node)
1597 return;
1599 type = TREE_TYPE (anon_union_decl);
1600 public_p = TREE_PUBLIC (anon_union_decl);
1602 /* The VAR_DECL's context is the same as the TYPE's context. */
1603 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1605 if (TYPE_FIELDS (type) == NULL_TREE)
1606 return;
1608 if (public_p)
1610 error ("namespace-scope anonymous aggregates must be static");
1611 return;
1614 main_decl = build_anon_union_vars (type, anon_union_decl);
1615 if (main_decl == error_mark_node)
1616 return;
1617 if (main_decl == NULL_TREE)
1619 warning (0, "anonymous union with no members");
1620 return;
1623 if (!processing_template_decl)
1625 /* Use main_decl to set the mangled name. */
1626 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1627 maybe_commonize_var (anon_union_decl);
1628 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1629 mangle_decl (anon_union_decl);
1630 DECL_NAME (anon_union_decl) = NULL_TREE;
1633 pushdecl (anon_union_decl);
1634 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1637 /* Auxiliary functions to make type signatures for
1638 `operator new' and `operator delete' correspond to
1639 what compiler will be expecting. */
1641 tree
1642 coerce_new_type (tree type)
1644 int e = 0;
1645 tree args = TYPE_ARG_TYPES (type);
1647 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1649 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1651 e = 1;
1652 error ("%<operator new%> must return type %qT", ptr_type_node);
1655 if (args && args != void_list_node)
1657 if (TREE_PURPOSE (args))
1659 /* [basic.stc.dynamic.allocation]
1661 The first parameter shall not have an associated default
1662 argument. */
1663 error ("the first parameter of %<operator new%> cannot "
1664 "have a default argument");
1665 /* Throw away the default argument. */
1666 TREE_PURPOSE (args) = NULL_TREE;
1669 if (!same_type_p (TREE_VALUE (args), size_type_node))
1671 e = 2;
1672 args = TREE_CHAIN (args);
1675 else
1676 e = 2;
1678 if (e == 2)
1679 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1680 "as first parameter", size_type_node);
1682 switch (e)
1684 case 2:
1685 args = tree_cons (NULL_TREE, size_type_node, args);
1686 /* Fall through. */
1687 case 1:
1688 type = build_exception_variant
1689 (build_function_type (ptr_type_node, args),
1690 TYPE_RAISES_EXCEPTIONS (type));
1691 /* Fall through. */
1692 default:;
1694 return type;
1697 tree
1698 coerce_delete_type (tree type)
1700 int e = 0;
1701 tree args = TYPE_ARG_TYPES (type);
1703 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1705 if (!same_type_p (TREE_TYPE (type), void_type_node))
1707 e = 1;
1708 error ("%<operator delete%> must return type %qT", void_type_node);
1711 if (!args || args == void_list_node
1712 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1714 e = 2;
1715 if (args && args != void_list_node)
1716 args = TREE_CHAIN (args);
1717 error ("%<operator delete%> takes type %qT as first parameter",
1718 ptr_type_node);
1720 switch (e)
1722 case 2:
1723 args = tree_cons (NULL_TREE, ptr_type_node, args);
1724 /* Fall through. */
1725 case 1:
1726 type = build_exception_variant
1727 (build_function_type (void_type_node, args),
1728 TYPE_RAISES_EXCEPTIONS (type));
1729 /* Fall through. */
1730 default:;
1733 return type;
1736 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1737 and mark them as needed. */
1739 static void
1740 mark_vtable_entries (tree decl)
1742 tree fnaddr;
1743 unsigned HOST_WIDE_INT idx;
1745 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1746 idx, fnaddr)
1748 tree fn;
1750 STRIP_NOPS (fnaddr);
1752 if (TREE_CODE (fnaddr) != ADDR_EXPR
1753 && TREE_CODE (fnaddr) != FDESC_EXPR)
1754 /* This entry is an offset: a virtual base class offset, a
1755 virtual call offset, an RTTI offset, etc. */
1756 continue;
1758 fn = TREE_OPERAND (fnaddr, 0);
1759 TREE_ADDRESSABLE (fn) = 1;
1760 /* When we don't have vcall offsets, we output thunks whenever
1761 we output the vtables that contain them. With vcall offsets,
1762 we know all the thunks we'll need when we emit a virtual
1763 function, so we emit the thunks there instead. */
1764 if (DECL_THUNK_P (fn))
1765 use_thunk (fn, /*emit_p=*/0);
1766 mark_used (fn);
1770 /* Set DECL up to have the closest approximation of "initialized common"
1771 linkage available. */
1773 void
1774 comdat_linkage (tree decl)
1776 if (flag_weak)
1777 make_decl_one_only (decl, cxx_comdat_group (decl));
1778 else if (TREE_CODE (decl) == FUNCTION_DECL
1779 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1780 /* We can just emit function and compiler-generated variables
1781 statically; having multiple copies is (for the most part) only
1782 a waste of space.
1784 There are two correctness issues, however: the address of a
1785 template instantiation with external linkage should be the
1786 same, independent of what translation unit asks for the
1787 address, and this will not hold when we emit multiple copies of
1788 the function. However, there's little else we can do.
1790 Also, by default, the typeinfo implementation assumes that
1791 there will be only one copy of the string used as the name for
1792 each type. Therefore, if weak symbols are unavailable, the
1793 run-time library should perform a more conservative check; it
1794 should perform a string comparison, rather than an address
1795 comparison. */
1796 TREE_PUBLIC (decl) = 0;
1797 else
1799 /* Static data member template instantiations, however, cannot
1800 have multiple copies. */
1801 if (DECL_INITIAL (decl) == 0
1802 || DECL_INITIAL (decl) == error_mark_node)
1803 DECL_COMMON (decl) = 1;
1804 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1806 DECL_COMMON (decl) = 1;
1807 DECL_INITIAL (decl) = error_mark_node;
1809 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1811 /* We can't do anything useful; leave vars for explicit
1812 instantiation. */
1813 DECL_EXTERNAL (decl) = 1;
1814 DECL_NOT_REALLY_EXTERN (decl) = 0;
1818 DECL_COMDAT (decl) = 1;
1821 /* For win32 we also want to put explicit instantiations in
1822 linkonce sections, so that they will be merged with implicit
1823 instantiations; otherwise we get duplicate symbol errors.
1824 For Darwin we do not want explicit instantiations to be
1825 linkonce. */
1827 void
1828 maybe_make_one_only (tree decl)
1830 /* We used to say that this was not necessary on targets that support weak
1831 symbols, because the implicit instantiations will defer to the explicit
1832 one. However, that's not actually the case in SVR4; a strong definition
1833 after a weak one is an error. Also, not making explicit
1834 instantiations one_only means that we can end up with two copies of
1835 some template instantiations. */
1836 if (! flag_weak)
1837 return;
1839 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1840 we can get away with not emitting them if they aren't used. We need
1841 to for variables so that cp_finish_decl will update their linkage,
1842 because their DECL_INITIAL may not have been set properly yet. */
1844 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1845 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1846 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1848 make_decl_one_only (decl, cxx_comdat_group (decl));
1850 if (VAR_P (decl))
1852 varpool_node *node = varpool_node::get_create (decl);
1853 DECL_COMDAT (decl) = 1;
1854 /* Mark it needed so we don't forget to emit it. */
1855 node->forced_by_abi = true;
1856 TREE_USED (decl) = 1;
1861 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1862 This predicate will give the right answer during parsing of the
1863 function, which other tests may not. */
1865 bool
1866 vague_linkage_p (tree decl)
1868 if (!TREE_PUBLIC (decl))
1870 gcc_checking_assert (!DECL_COMDAT (decl));
1871 return false;
1873 /* Unfortunately, import_export_decl has not always been called
1874 before the function is processed, so we cannot simply check
1875 DECL_COMDAT. */
1876 if (DECL_COMDAT (decl)
1877 || (TREE_CODE (decl) == FUNCTION_DECL
1878 && DECL_DECLARED_INLINE_P (decl))
1879 || (DECL_LANG_SPECIFIC (decl)
1880 && DECL_TEMPLATE_INSTANTIATION (decl)))
1881 return true;
1882 else if (DECL_FUNCTION_SCOPE_P (decl))
1883 /* A local static in an inline effectively has vague linkage. */
1884 return (TREE_STATIC (decl)
1885 && vague_linkage_p (DECL_CONTEXT (decl)));
1886 else
1887 return false;
1890 /* Determine whether or not we want to specifically import or export CTYPE,
1891 using various heuristics. */
1893 static void
1894 import_export_class (tree ctype)
1896 /* -1 for imported, 1 for exported. */
1897 int import_export = 0;
1899 /* It only makes sense to call this function at EOF. The reason is
1900 that this function looks at whether or not the first non-inline
1901 non-abstract virtual member function has been defined in this
1902 translation unit. But, we can't possibly know that until we've
1903 seen the entire translation unit. */
1904 gcc_assert (at_eof);
1906 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1907 return;
1909 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1910 we will have CLASSTYPE_INTERFACE_ONLY set but not
1911 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1912 heuristic because someone will supply a #pragma implementation
1913 elsewhere, and deducing it here would produce a conflict. */
1914 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1915 return;
1917 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1918 import_export = -1;
1919 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1920 import_export = 1;
1921 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1922 && !flag_implicit_templates)
1923 /* For a template class, without -fimplicit-templates, check the
1924 repository. If the virtual table is assigned to this
1925 translation unit, then export the class; otherwise, import
1926 it. */
1927 import_export = repo_export_class_p (ctype) ? 1 : -1;
1928 else if (TYPE_POLYMORPHIC_P (ctype))
1930 /* The ABI specifies that the virtual table and associated
1931 information are emitted with the key method, if any. */
1932 tree method = CLASSTYPE_KEY_METHOD (ctype);
1933 /* If weak symbol support is not available, then we must be
1934 careful not to emit the vtable when the key function is
1935 inline. An inline function can be defined in multiple
1936 translation units. If we were to emit the vtable in each
1937 translation unit containing a definition, we would get
1938 multiple definition errors at link-time. */
1939 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1940 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1943 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1944 a definition anywhere else. */
1945 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1946 import_export = 0;
1948 /* Allow back ends the chance to overrule the decision. */
1949 if (targetm.cxx.import_export_class)
1950 import_export = targetm.cxx.import_export_class (ctype, import_export);
1952 if (import_export)
1954 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1955 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1959 /* Return true if VAR has already been provided to the back end; in that
1960 case VAR should not be modified further by the front end. */
1961 static bool
1962 var_finalized_p (tree var)
1964 return varpool_node::get_create (var)->definition;
1967 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1968 must be emitted in this translation unit. Mark it as such. */
1970 void
1971 mark_needed (tree decl)
1973 TREE_USED (decl) = 1;
1974 if (TREE_CODE (decl) == FUNCTION_DECL)
1976 /* Extern inline functions don't become needed when referenced.
1977 If we know a method will be emitted in other TU and no new
1978 functions can be marked reachable, just use the external
1979 definition. */
1980 struct cgraph_node *node = cgraph_node::get_create (decl);
1981 node->forced_by_abi = true;
1983 /* #pragma interface and -frepo code can call mark_needed for
1984 maybe-in-charge 'tors; mark the clones as well. */
1985 tree clone;
1986 FOR_EACH_CLONE (clone, decl)
1987 mark_needed (clone);
1989 else if (TREE_CODE (decl) == VAR_DECL)
1991 varpool_node *node = varpool_node::get_create (decl);
1992 /* C++ frontend use mark_decl_references to force COMDAT variables
1993 to be output that might appear dead otherwise. */
1994 node->forced_by_abi = true;
1998 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1999 returns true if a definition of this entity should be provided in
2000 this object file. Callers use this function to determine whether
2001 or not to let the back end know that a definition of DECL is
2002 available in this translation unit. */
2004 bool
2005 decl_needed_p (tree decl)
2007 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2008 /* This function should only be called at the end of the translation
2009 unit. We cannot be sure of whether or not something will be
2010 COMDAT until that point. */
2011 gcc_assert (at_eof);
2013 /* All entities with external linkage that are not COMDAT/EXTERN should be
2014 emitted; they may be referred to from other object files. */
2015 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2016 return true;
2017 /* Functions marked "dllexport" must be emitted so that they are
2018 visible to other DLLs. */
2019 if (flag_keep_inline_dllexport
2020 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2021 return true;
2023 /* When not optimizing, do not bother to produce definitions for extern
2024 symbols. */
2025 if (DECL_REALLY_EXTERN (decl)
2026 && ((TREE_CODE (decl) != FUNCTION_DECL
2027 && !optimize)
2028 || (TREE_CODE (decl) == FUNCTION_DECL
2029 && !opt_for_fn (decl, optimize)))
2030 && !lookup_attribute ("always_inline", decl))
2031 return false;
2033 /* If this entity was used, let the back end see it; it will decide
2034 whether or not to emit it into the object file. */
2035 if (TREE_USED (decl))
2036 return true;
2037 /* Virtual functions might be needed for devirtualization. */
2038 if (flag_devirtualize
2039 && TREE_CODE (decl) == FUNCTION_DECL
2040 && DECL_VIRTUAL_P (decl))
2041 return true;
2042 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2043 reference to DECL might cause it to be emitted later. */
2044 return false;
2047 /* If necessary, write out the vtables for the dynamic class CTYPE.
2048 Returns true if any vtables were emitted. */
2050 static bool
2051 maybe_emit_vtables (tree ctype)
2053 tree vtbl;
2054 tree primary_vtbl;
2055 int needed = 0;
2056 varpool_node *current = NULL, *last = NULL;
2058 /* If the vtables for this class have already been emitted there is
2059 nothing more to do. */
2060 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2061 if (var_finalized_p (primary_vtbl))
2062 return false;
2063 /* Ignore dummy vtables made by get_vtable_decl. */
2064 if (TREE_TYPE (primary_vtbl) == void_type_node)
2065 return false;
2067 /* On some targets, we cannot determine the key method until the end
2068 of the translation unit -- which is when this function is
2069 called. */
2070 if (!targetm.cxx.key_method_may_be_inline ())
2071 determine_key_method (ctype);
2073 /* See if any of the vtables are needed. */
2074 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2076 import_export_decl (vtbl);
2077 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2078 needed = 1;
2080 if (!needed)
2082 /* If the references to this class' vtables are optimized away,
2083 still emit the appropriate debugging information. See
2084 dfs_debug_mark. */
2085 if (DECL_COMDAT (primary_vtbl)
2086 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2087 note_debug_info_needed (ctype);
2088 return false;
2091 /* The ABI requires that we emit all of the vtables if we emit any
2092 of them. */
2093 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2095 /* Mark entities references from the virtual table as used. */
2096 mark_vtable_entries (vtbl);
2098 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2100 vec<tree, va_gc> *cleanups = NULL;
2101 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2102 LOOKUP_NORMAL);
2104 /* It had better be all done at compile-time. */
2105 gcc_assert (!expr && !cleanups);
2108 /* Write it out. */
2109 DECL_EXTERNAL (vtbl) = 0;
2110 rest_of_decl_compilation (vtbl, 1, 1);
2112 /* Because we're only doing syntax-checking, we'll never end up
2113 actually marking the variable as written. */
2114 if (flag_syntax_only)
2115 TREE_ASM_WRITTEN (vtbl) = 1;
2116 else if (DECL_ONE_ONLY (vtbl))
2118 current = varpool_node::get_create (vtbl);
2119 if (last)
2120 current->add_to_same_comdat_group (last);
2121 last = current;
2125 /* Since we're writing out the vtable here, also write the debug
2126 info. */
2127 note_debug_info_needed (ctype);
2129 return true;
2132 /* A special return value from type_visibility meaning internal
2133 linkage. */
2135 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2137 /* walk_tree helper function for type_visibility. */
2139 static tree
2140 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2142 int *vis_p = (int *)data;
2143 if (! TYPE_P (*tp))
2145 *walk_subtrees = 0;
2147 else if (OVERLOAD_TYPE_P (*tp)
2148 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2150 *vis_p = VISIBILITY_ANON;
2151 return *tp;
2153 else if (CLASS_TYPE_P (*tp)
2154 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2155 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2156 return NULL;
2159 /* Returns the visibility of TYPE, which is the minimum visibility of its
2160 component types. */
2162 static int
2163 type_visibility (tree type)
2165 int vis = VISIBILITY_DEFAULT;
2166 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2167 return vis;
2170 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2171 specified (or if VISIBILITY is static). If TMPL is true, this
2172 constraint is for a template argument, and takes precedence
2173 over explicitly-specified visibility on the template. */
2175 static void
2176 constrain_visibility (tree decl, int visibility, bool tmpl)
2178 if (visibility == VISIBILITY_ANON)
2180 /* extern "C" declarations aren't affected by the anonymous
2181 namespace. */
2182 if (!DECL_EXTERN_C_P (decl))
2184 TREE_PUBLIC (decl) = 0;
2185 DECL_WEAK (decl) = 0;
2186 DECL_COMMON (decl) = 0;
2187 DECL_COMDAT (decl) = false;
2188 if (TREE_CODE (decl) == FUNCTION_DECL
2189 || TREE_CODE (decl) == VAR_DECL)
2191 struct symtab_node *snode = symtab_node::get (decl);
2193 if (snode)
2194 snode->set_comdat_group (NULL);
2196 DECL_INTERFACE_KNOWN (decl) = 1;
2197 if (DECL_LANG_SPECIFIC (decl))
2198 DECL_NOT_REALLY_EXTERN (decl) = 1;
2201 else if (visibility > DECL_VISIBILITY (decl)
2202 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2204 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2205 /* This visibility was not specified. */
2206 DECL_VISIBILITY_SPECIFIED (decl) = false;
2210 /* Constrain the visibility of DECL based on the visibility of its template
2211 arguments. */
2213 static void
2214 constrain_visibility_for_template (tree decl, tree targs)
2216 /* If this is a template instantiation, check the innermost
2217 template args for visibility constraints. The outer template
2218 args are covered by the class check. */
2219 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2220 int i;
2221 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2223 int vis = 0;
2225 tree arg = TREE_VEC_ELT (args, i-1);
2226 if (TYPE_P (arg))
2227 vis = type_visibility (arg);
2228 else
2230 if (REFERENCE_REF_P (arg))
2231 arg = TREE_OPERAND (arg, 0);
2232 if (TREE_TYPE (arg))
2233 STRIP_NOPS (arg);
2234 if (TREE_CODE (arg) == ADDR_EXPR)
2235 arg = TREE_OPERAND (arg, 0);
2236 if (VAR_OR_FUNCTION_DECL_P (arg))
2238 if (! TREE_PUBLIC (arg))
2239 vis = VISIBILITY_ANON;
2240 else
2241 vis = DECL_VISIBILITY (arg);
2244 if (vis)
2245 constrain_visibility (decl, vis, true);
2249 /* Like c_determine_visibility, but with additional C++-specific
2250 behavior.
2252 Function-scope entities can rely on the function's visibility because
2253 it is set in start_preparsed_function.
2255 Class-scope entities cannot rely on the class's visibility until the end
2256 of the enclosing class definition.
2258 Note that because namespaces have multiple independent definitions,
2259 namespace visibility is handled elsewhere using the #pragma visibility
2260 machinery rather than by decorating the namespace declaration.
2262 The goal is for constraints from the type to give a diagnostic, and
2263 other constraints to be applied silently. */
2265 void
2266 determine_visibility (tree decl)
2268 tree class_type = NULL_TREE;
2269 bool use_template;
2270 bool orig_visibility_specified;
2271 enum symbol_visibility orig_visibility;
2273 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2275 /* Only relevant for names with external linkage. */
2276 if (!TREE_PUBLIC (decl))
2277 return;
2279 /* Cloned constructors and destructors get the same visibility as
2280 the underlying function. That should be set up in
2281 maybe_clone_body. */
2282 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2284 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2285 orig_visibility = DECL_VISIBILITY (decl);
2287 if (TREE_CODE (decl) == TYPE_DECL)
2289 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2290 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2291 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2292 use_template = 1;
2293 else
2294 use_template = 0;
2296 else if (DECL_LANG_SPECIFIC (decl))
2297 use_template = DECL_USE_TEMPLATE (decl);
2298 else
2299 use_template = 0;
2301 /* If DECL is a member of a class, visibility specifiers on the
2302 class can influence the visibility of the DECL. */
2303 if (DECL_CLASS_SCOPE_P (decl))
2304 class_type = DECL_CONTEXT (decl);
2305 else
2307 /* Not a class member. */
2309 /* Virtual tables have DECL_CONTEXT set to their associated class,
2310 so they are automatically handled above. */
2311 gcc_assert (!VAR_P (decl)
2312 || !DECL_VTABLE_OR_VTT_P (decl));
2314 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2316 /* Local statics and classes get the visibility of their
2317 containing function by default, except that
2318 -fvisibility-inlines-hidden doesn't affect them. */
2319 tree fn = DECL_CONTEXT (decl);
2320 if (DECL_VISIBILITY_SPECIFIED (fn))
2322 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2323 DECL_VISIBILITY_SPECIFIED (decl) =
2324 DECL_VISIBILITY_SPECIFIED (fn);
2326 else
2328 if (DECL_CLASS_SCOPE_P (fn))
2329 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2330 else if (determine_hidden_inline (fn))
2332 DECL_VISIBILITY (decl) = default_visibility;
2333 DECL_VISIBILITY_SPECIFIED (decl) =
2334 visibility_options.inpragma;
2336 else
2338 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2339 DECL_VISIBILITY_SPECIFIED (decl) =
2340 DECL_VISIBILITY_SPECIFIED (fn);
2344 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2345 but have no TEMPLATE_INFO, so don't try to check it. */
2346 use_template = 0;
2348 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2349 && flag_visibility_ms_compat)
2351 /* Under -fvisibility-ms-compat, types are visible by default,
2352 even though their contents aren't. */
2353 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2354 int underlying_vis = type_visibility (underlying_type);
2355 if (underlying_vis == VISIBILITY_ANON
2356 || (CLASS_TYPE_P (underlying_type)
2357 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2358 constrain_visibility (decl, underlying_vis, false);
2359 else
2360 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2362 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2364 /* tinfo visibility is based on the type it's for. */
2365 constrain_visibility
2366 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2368 /* Give the target a chance to override the visibility associated
2369 with DECL. */
2370 if (TREE_PUBLIC (decl)
2371 && !DECL_REALLY_EXTERN (decl)
2372 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2373 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2374 targetm.cxx.determine_class_data_visibility (decl);
2376 else if (use_template)
2377 /* Template instantiations and specializations get visibility based
2378 on their template unless they override it with an attribute. */;
2379 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2381 if (determine_hidden_inline (decl))
2382 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2383 else
2385 /* Set default visibility to whatever the user supplied with
2386 #pragma GCC visibility or a namespace visibility attribute. */
2387 DECL_VISIBILITY (decl) = default_visibility;
2388 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2393 if (use_template)
2395 /* If the specialization doesn't specify visibility, use the
2396 visibility from the template. */
2397 tree tinfo = get_template_info (decl);
2398 tree args = TI_ARGS (tinfo);
2399 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2400 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2401 : DECL_ATTRIBUTES (decl));
2403 if (args != error_mark_node)
2405 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2407 if (!DECL_VISIBILITY_SPECIFIED (decl))
2409 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2410 && determine_hidden_inline (decl))
2411 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2412 else
2414 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2415 DECL_VISIBILITY_SPECIFIED (decl)
2416 = DECL_VISIBILITY_SPECIFIED (pattern);
2420 if (args
2421 /* Template argument visibility outweighs #pragma or namespace
2422 visibility, but not an explicit attribute. */
2423 && !lookup_attribute ("visibility", attribs))
2425 int depth = TMPL_ARGS_DEPTH (args);
2426 if (DECL_VISIBILITY_SPECIFIED (decl))
2428 /* A class template member with explicit visibility
2429 overrides the class visibility, so we need to apply
2430 all the levels of template args directly. */
2431 int i;
2432 for (i = 1; i <= depth; ++i)
2434 tree lev = TMPL_ARGS_LEVEL (args, i);
2435 constrain_visibility_for_template (decl, lev);
2438 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2439 /* Limit visibility based on its template arguments. */
2440 constrain_visibility_for_template (decl, args);
2445 if (class_type)
2446 determine_visibility_from_class (decl, class_type);
2448 if (decl_anon_ns_mem_p (decl))
2449 /* Names in an anonymous namespace get internal linkage.
2450 This might change once we implement export. */
2451 constrain_visibility (decl, VISIBILITY_ANON, false);
2452 else if (TREE_CODE (decl) != TYPE_DECL)
2454 /* Propagate anonymity from type to decl. */
2455 int tvis = type_visibility (TREE_TYPE (decl));
2456 if (tvis == VISIBILITY_ANON
2457 || ! DECL_VISIBILITY_SPECIFIED (decl))
2458 constrain_visibility (decl, tvis, false);
2460 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2461 /* DR 757: A type without linkage shall not be used as the type of a
2462 variable or function with linkage, unless
2463 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2464 o the variable or function is not used (3.2 [basic.def.odr]) or is
2465 defined in the same translation unit.
2467 Since non-extern "C" decls need to be defined in the same
2468 translation unit, we can make the type internal. */
2469 constrain_visibility (decl, VISIBILITY_ANON, false);
2471 /* If visibility changed and DECL already has DECL_RTL, ensure
2472 symbol flags are updated. */
2473 if ((DECL_VISIBILITY (decl) != orig_visibility
2474 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2475 && ((VAR_P (decl) && TREE_STATIC (decl))
2476 || TREE_CODE (decl) == FUNCTION_DECL)
2477 && DECL_RTL_SET_P (decl))
2478 make_decl_rtl (decl);
2481 /* By default, static data members and function members receive
2482 the visibility of their containing class. */
2484 static void
2485 determine_visibility_from_class (tree decl, tree class_type)
2487 if (DECL_VISIBILITY_SPECIFIED (decl))
2488 return;
2490 if (determine_hidden_inline (decl))
2491 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2492 else
2494 /* Default to the class visibility. */
2495 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2496 DECL_VISIBILITY_SPECIFIED (decl)
2497 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2500 /* Give the target a chance to override the visibility associated
2501 with DECL. */
2502 if (VAR_P (decl)
2503 && (DECL_TINFO_P (decl)
2504 || (DECL_VTABLE_OR_VTT_P (decl)
2505 /* Construction virtual tables are not exported because
2506 they cannot be referred to from other object files;
2507 their name is not standardized by the ABI. */
2508 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2509 && TREE_PUBLIC (decl)
2510 && !DECL_REALLY_EXTERN (decl)
2511 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2512 targetm.cxx.determine_class_data_visibility (decl);
2515 /* Returns true iff DECL is an inline that should get hidden visibility
2516 because of -fvisibility-inlines-hidden. */
2518 static bool
2519 determine_hidden_inline (tree decl)
2521 return (visibility_options.inlines_hidden
2522 /* Don't do this for inline templates; specializations might not be
2523 inline, and we don't want them to inherit the hidden
2524 visibility. We'll set it here for all inline instantiations. */
2525 && !processing_template_decl
2526 && TREE_CODE (decl) == FUNCTION_DECL
2527 && DECL_DECLARED_INLINE_P (decl)
2528 && (! DECL_LANG_SPECIFIC (decl)
2529 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2532 /* Constrain the visibility of a class TYPE based on the visibility of its
2533 field types. Warn if any fields require lesser visibility. */
2535 void
2536 constrain_class_visibility (tree type)
2538 tree binfo;
2539 tree t;
2540 int i;
2542 int vis = type_visibility (type);
2544 if (vis == VISIBILITY_ANON
2545 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2546 return;
2548 /* Don't warn about visibility if the class has explicit visibility. */
2549 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2550 vis = VISIBILITY_INTERNAL;
2552 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2553 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2555 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2556 int subvis = type_visibility (ftype);
2558 if (subvis == VISIBILITY_ANON)
2560 if (!in_main_input_context ())
2561 warning (0, "\
2562 %qT has a field %qD whose type uses the anonymous namespace",
2563 type, t);
2565 else if (MAYBE_CLASS_TYPE_P (ftype)
2566 && vis < VISIBILITY_HIDDEN
2567 && subvis >= VISIBILITY_HIDDEN)
2568 warning (OPT_Wattributes, "\
2569 %qT declared with greater visibility than the type of its field %qD",
2570 type, t);
2573 binfo = TYPE_BINFO (type);
2574 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2576 int subvis = type_visibility (TREE_TYPE (t));
2578 if (subvis == VISIBILITY_ANON)
2580 if (!in_main_input_context())
2581 warning (0, "\
2582 %qT has a base %qT whose type uses the anonymous namespace",
2583 type, TREE_TYPE (t));
2585 else if (vis < VISIBILITY_HIDDEN
2586 && subvis >= VISIBILITY_HIDDEN)
2587 warning (OPT_Wattributes, "\
2588 %qT declared with greater visibility than its base %qT",
2589 type, TREE_TYPE (t));
2593 /* Functions for adjusting the visibility of a tagged type and its nested
2594 types and declarations when it gets a name for linkage purposes from a
2595 typedef. */
2597 static void bt_reset_linkage_1 (binding_entry, void *);
2598 static void bt_reset_linkage_2 (binding_entry, void *);
2600 /* First reset the visibility of all the types. */
2602 static void
2603 reset_type_linkage_1 (tree type)
2605 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2606 if (CLASS_TYPE_P (type))
2607 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2608 bt_reset_linkage_1, NULL);
2610 static void
2611 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2613 reset_type_linkage_1 (b->type);
2616 /* Then reset the visibility of any static data members or member
2617 functions that use those types. */
2619 static void
2620 reset_decl_linkage (tree decl)
2622 if (TREE_PUBLIC (decl))
2623 return;
2624 if (DECL_CLONED_FUNCTION_P (decl))
2625 return;
2626 TREE_PUBLIC (decl) = true;
2627 DECL_INTERFACE_KNOWN (decl) = false;
2628 determine_visibility (decl);
2629 tentative_decl_linkage (decl);
2631 static void
2632 reset_type_linkage_2 (tree type)
2634 if (CLASS_TYPE_P (type))
2636 if (tree vt = CLASSTYPE_VTABLES (type))
2638 tree name = mangle_vtbl_for_type (type);
2639 DECL_NAME (vt) = name;
2640 SET_DECL_ASSEMBLER_NAME (vt, name);
2641 reset_decl_linkage (vt);
2643 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2645 tree name = mangle_typeinfo_for_type (type);
2646 DECL_NAME (ti) = name;
2647 SET_DECL_ASSEMBLER_NAME (ti, name);
2648 TREE_TYPE (name) = type;
2649 reset_decl_linkage (ti);
2651 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2652 if (TREE_CODE (m) == VAR_DECL)
2653 reset_decl_linkage (m);
2654 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2656 reset_decl_linkage (m);
2657 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (m))
2658 /* Also update its name, for cxx_dwarf_name. */
2659 DECL_NAME (m) = TYPE_IDENTIFIER (type);
2661 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2662 bt_reset_linkage_2, NULL);
2665 static void
2666 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2668 reset_type_linkage_2 (b->type);
2670 void
2671 reset_type_linkage (tree type)
2673 reset_type_linkage_1 (type);
2674 reset_type_linkage_2 (type);
2677 /* Set up our initial idea of what the linkage of DECL should be. */
2679 void
2680 tentative_decl_linkage (tree decl)
2682 if (DECL_INTERFACE_KNOWN (decl))
2683 /* We've already made a decision as to how this function will
2684 be handled. */;
2685 else if (vague_linkage_p (decl))
2687 if (TREE_CODE (decl) == FUNCTION_DECL
2688 && decl_defined_p (decl))
2690 DECL_EXTERNAL (decl) = 1;
2691 DECL_NOT_REALLY_EXTERN (decl) = 1;
2692 note_vague_linkage_fn (decl);
2693 /* A non-template inline function with external linkage will
2694 always be COMDAT. As we must eventually determine the
2695 linkage of all functions, and as that causes writes to
2696 the data mapped in from the PCH file, it's advantageous
2697 to mark the functions at this point. */
2698 if (DECL_DECLARED_INLINE_P (decl)
2699 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2700 || DECL_DEFAULTED_FN (decl)))
2702 /* This function must have external linkage, as
2703 otherwise DECL_INTERFACE_KNOWN would have been
2704 set. */
2705 gcc_assert (TREE_PUBLIC (decl));
2706 comdat_linkage (decl);
2707 DECL_INTERFACE_KNOWN (decl) = 1;
2710 else if (TREE_CODE (decl) == VAR_DECL)
2711 maybe_commonize_var (decl);
2715 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2716 for DECL has not already been determined, do so now by setting
2717 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2718 function is called entities with vague linkage whose definitions
2719 are available must have TREE_PUBLIC set.
2721 If this function decides to place DECL in COMDAT, it will set
2722 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2723 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2724 callers defer that decision until it is clear that DECL is actually
2725 required. */
2727 void
2728 import_export_decl (tree decl)
2730 int emit_p;
2731 bool comdat_p;
2732 bool import_p;
2733 tree class_type = NULL_TREE;
2735 if (DECL_INTERFACE_KNOWN (decl))
2736 return;
2738 /* We cannot determine what linkage to give to an entity with vague
2739 linkage until the end of the file. For example, a virtual table
2740 for a class will be defined if and only if the key method is
2741 defined in this translation unit. As a further example, consider
2742 that when compiling a translation unit that uses PCH file with
2743 "-frepo" it would be incorrect to make decisions about what
2744 entities to emit when building the PCH; those decisions must be
2745 delayed until the repository information has been processed. */
2746 gcc_assert (at_eof);
2747 /* Object file linkage for explicit instantiations is handled in
2748 mark_decl_instantiated. For static variables in functions with
2749 vague linkage, maybe_commonize_var is used.
2751 Therefore, the only declarations that should be provided to this
2752 function are those with external linkage that are:
2754 * implicit instantiations of function templates
2756 * inline function
2758 * implicit instantiations of static data members of class
2759 templates
2761 * virtual tables
2763 * typeinfo objects
2765 Furthermore, all entities that reach this point must have a
2766 definition available in this translation unit.
2768 The following assertions check these conditions. */
2769 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2770 /* Any code that creates entities with TREE_PUBLIC cleared should
2771 also set DECL_INTERFACE_KNOWN. */
2772 gcc_assert (TREE_PUBLIC (decl));
2773 if (TREE_CODE (decl) == FUNCTION_DECL)
2774 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2775 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2776 || DECL_DECLARED_INLINE_P (decl));
2777 else
2778 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2779 || DECL_VTABLE_OR_VTT_P (decl)
2780 || DECL_TINFO_P (decl));
2781 /* Check that a definition of DECL is available in this translation
2782 unit. */
2783 gcc_assert (!DECL_REALLY_EXTERN (decl));
2785 /* Assume that DECL will not have COMDAT linkage. */
2786 comdat_p = false;
2787 /* Assume that DECL will not be imported into this translation
2788 unit. */
2789 import_p = false;
2791 /* See if the repository tells us whether or not to emit DECL in
2792 this translation unit. */
2793 emit_p = repo_emit_p (decl);
2794 if (emit_p == 0)
2795 import_p = true;
2796 else if (emit_p == 1)
2798 /* The repository indicates that this entity should be defined
2799 here. Make sure the back end honors that request. */
2800 mark_needed (decl);
2801 /* Output the definition as an ordinary strong definition. */
2802 DECL_EXTERNAL (decl) = 0;
2803 DECL_INTERFACE_KNOWN (decl) = 1;
2804 return;
2807 if (import_p)
2808 /* We have already decided what to do with this DECL; there is no
2809 need to check anything further. */
2811 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2813 class_type = DECL_CONTEXT (decl);
2814 import_export_class (class_type);
2815 if (TYPE_FOR_JAVA (class_type))
2816 import_p = true;
2817 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2818 && CLASSTYPE_INTERFACE_ONLY (class_type))
2819 import_p = true;
2820 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2821 && !CLASSTYPE_USE_TEMPLATE (class_type)
2822 && CLASSTYPE_KEY_METHOD (class_type)
2823 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2824 /* The ABI requires that all virtual tables be emitted with
2825 COMDAT linkage. However, on systems where COMDAT symbols
2826 don't show up in the table of contents for a static
2827 archive, or on systems without weak symbols (where we
2828 approximate COMDAT linkage by using internal linkage), the
2829 linker will report errors about undefined symbols because
2830 it will not see the virtual table definition. Therefore,
2831 in the case that we know that the virtual table will be
2832 emitted in only one translation unit, we make the virtual
2833 table an ordinary definition with external linkage. */
2834 DECL_EXTERNAL (decl) = 0;
2835 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2837 /* CLASS_TYPE is being exported from this translation unit,
2838 so DECL should be defined here. */
2839 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2840 /* If a class is declared in a header with the "extern
2841 template" extension, then it will not be instantiated,
2842 even in translation units that would normally require
2843 it. Often such classes are explicitly instantiated in
2844 one translation unit. Therefore, the explicit
2845 instantiation must be made visible to other translation
2846 units. */
2847 DECL_EXTERNAL (decl) = 0;
2848 else
2850 /* The generic C++ ABI says that class data is always
2851 COMDAT, even if there is a key function. Some
2852 variants (e.g., the ARM EABI) says that class data
2853 only has COMDAT linkage if the class data might be
2854 emitted in more than one translation unit. When the
2855 key method can be inline and is inline, we still have
2856 to arrange for comdat even though
2857 class_data_always_comdat is false. */
2858 if (!CLASSTYPE_KEY_METHOD (class_type)
2859 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2860 || targetm.cxx.class_data_always_comdat ())
2862 /* The ABI requires COMDAT linkage. Normally, we
2863 only emit COMDAT things when they are needed;
2864 make sure that we realize that this entity is
2865 indeed needed. */
2866 comdat_p = true;
2867 mark_needed (decl);
2871 else if (!flag_implicit_templates
2872 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2873 import_p = true;
2874 else
2875 comdat_p = true;
2877 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2879 tree type = TREE_TYPE (DECL_NAME (decl));
2880 if (CLASS_TYPE_P (type))
2882 class_type = type;
2883 import_export_class (type);
2884 if (CLASSTYPE_INTERFACE_KNOWN (type)
2885 && TYPE_POLYMORPHIC_P (type)
2886 && CLASSTYPE_INTERFACE_ONLY (type)
2887 /* If -fno-rtti was specified, then we cannot be sure
2888 that RTTI information will be emitted with the
2889 virtual table of the class, so we must emit it
2890 wherever it is used. */
2891 && flag_rtti)
2892 import_p = true;
2893 else
2895 if (CLASSTYPE_INTERFACE_KNOWN (type)
2896 && !CLASSTYPE_INTERFACE_ONLY (type))
2898 comdat_p = (targetm.cxx.class_data_always_comdat ()
2899 || (CLASSTYPE_KEY_METHOD (type)
2900 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2901 mark_needed (decl);
2902 if (!flag_weak)
2904 comdat_p = false;
2905 DECL_EXTERNAL (decl) = 0;
2908 else
2909 comdat_p = true;
2912 else
2913 comdat_p = true;
2915 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2917 /* DECL is an implicit instantiation of a function or static
2918 data member. */
2919 if ((flag_implicit_templates
2920 && !flag_use_repository)
2921 || (flag_implicit_inline_templates
2922 && TREE_CODE (decl) == FUNCTION_DECL
2923 && DECL_DECLARED_INLINE_P (decl)))
2924 comdat_p = true;
2925 else
2926 /* If we are not implicitly generating templates, then mark
2927 this entity as undefined in this translation unit. */
2928 import_p = true;
2930 else if (DECL_FUNCTION_MEMBER_P (decl))
2932 if (!DECL_DECLARED_INLINE_P (decl))
2934 tree ctype = DECL_CONTEXT (decl);
2935 import_export_class (ctype);
2936 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2938 DECL_NOT_REALLY_EXTERN (decl)
2939 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2940 || (DECL_DECLARED_INLINE_P (decl)
2941 && ! flag_implement_inlines
2942 && !DECL_VINDEX (decl)));
2944 if (!DECL_NOT_REALLY_EXTERN (decl))
2945 DECL_EXTERNAL (decl) = 1;
2947 /* Always make artificials weak. */
2948 if (DECL_ARTIFICIAL (decl) && flag_weak)
2949 comdat_p = true;
2950 else
2951 maybe_make_one_only (decl);
2954 else
2955 comdat_p = true;
2957 else
2958 comdat_p = true;
2960 if (import_p)
2962 /* If we are importing DECL into this translation unit, mark is
2963 an undefined here. */
2964 DECL_EXTERNAL (decl) = 1;
2965 DECL_NOT_REALLY_EXTERN (decl) = 0;
2967 else if (comdat_p)
2969 /* If we decided to put DECL in COMDAT, mark it accordingly at
2970 this point. */
2971 comdat_linkage (decl);
2974 DECL_INTERFACE_KNOWN (decl) = 1;
2977 /* Return an expression that performs the destruction of DECL, which
2978 must be a VAR_DECL whose type has a non-trivial destructor, or is
2979 an array whose (innermost) elements have a non-trivial destructor. */
2981 tree
2982 build_cleanup (tree decl)
2984 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2985 gcc_assert (clean != NULL_TREE);
2986 return clean;
2989 /* Returns the initialization guard variable for the variable DECL,
2990 which has static storage duration. */
2992 tree
2993 get_guard (tree decl)
2995 tree sname;
2996 tree guard;
2998 sname = mangle_guard_variable (decl);
2999 guard = IDENTIFIER_GLOBAL_VALUE (sname);
3000 if (! guard)
3002 tree guard_type;
3004 /* We use a type that is big enough to contain a mutex as well
3005 as an integer counter. */
3006 guard_type = targetm.cxx.guard_type ();
3007 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3008 VAR_DECL, sname, guard_type);
3010 /* The guard should have the same linkage as what it guards. */
3011 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3012 TREE_STATIC (guard) = TREE_STATIC (decl);
3013 DECL_COMMON (guard) = DECL_COMMON (decl);
3014 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3015 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3016 if (DECL_ONE_ONLY (decl))
3017 make_decl_one_only (guard, cxx_comdat_group (guard));
3018 if (TREE_PUBLIC (decl))
3019 DECL_WEAK (guard) = DECL_WEAK (decl);
3020 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3021 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3023 DECL_ARTIFICIAL (guard) = 1;
3024 DECL_IGNORED_P (guard) = 1;
3025 TREE_USED (guard) = 1;
3026 pushdecl_top_level_and_finish (guard, NULL_TREE);
3028 return guard;
3031 /* Return those bits of the GUARD variable that should be set when the
3032 guarded entity is actually initialized. */
3034 static tree
3035 get_guard_bits (tree guard)
3037 if (!targetm.cxx.guard_mask_bit ())
3039 /* We only set the first byte of the guard, in order to leave room
3040 for a mutex in the high-order bits. */
3041 guard = build1 (ADDR_EXPR,
3042 build_pointer_type (TREE_TYPE (guard)),
3043 guard);
3044 guard = build1 (NOP_EXPR,
3045 build_pointer_type (char_type_node),
3046 guard);
3047 guard = build1 (INDIRECT_REF, char_type_node, guard);
3050 return guard;
3053 /* Return an expression which determines whether or not the GUARD
3054 variable has already been initialized. */
3056 tree
3057 get_guard_cond (tree guard)
3059 tree guard_value;
3061 /* Check to see if the GUARD is zero. */
3062 guard = get_guard_bits (guard);
3064 /* Mask off all but the low bit. */
3065 if (targetm.cxx.guard_mask_bit ())
3067 guard_value = integer_one_node;
3068 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3069 guard_value = convert (TREE_TYPE (guard), guard_value);
3070 guard = cp_build_binary_op (input_location,
3071 BIT_AND_EXPR, guard, guard_value,
3072 tf_warning_or_error);
3075 guard_value = integer_zero_node;
3076 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3077 guard_value = convert (TREE_TYPE (guard), guard_value);
3078 return cp_build_binary_op (input_location,
3079 EQ_EXPR, guard, guard_value,
3080 tf_warning_or_error);
3083 /* Return an expression which sets the GUARD variable, indicating that
3084 the variable being guarded has been initialized. */
3086 tree
3087 set_guard (tree guard)
3089 tree guard_init;
3091 /* Set the GUARD to one. */
3092 guard = get_guard_bits (guard);
3093 guard_init = integer_one_node;
3094 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3095 guard_init = convert (TREE_TYPE (guard), guard_init);
3096 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3097 tf_warning_or_error);
3100 /* Returns true iff we can tell that VAR does not have a dynamic
3101 initializer. */
3103 static bool
3104 var_defined_without_dynamic_init (tree var)
3106 /* If it's defined in another TU, we can't tell. */
3107 if (DECL_EXTERNAL (var))
3108 return false;
3109 /* If it has a non-trivial destructor, registering the destructor
3110 counts as dynamic initialization. */
3111 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3112 return false;
3113 /* If it's in this TU, its initializer has been processed, unless
3114 it's a case of self-initialization, then DECL_INITIALIZED_P is
3115 false while the initializer is handled by finish_id_expression. */
3116 if (!DECL_INITIALIZED_P (var))
3117 return false;
3118 /* If it has no initializer or a constant one, it's not dynamic. */
3119 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3120 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3123 /* Returns true iff VAR is a variable that needs uses to be
3124 wrapped for possible dynamic initialization. */
3126 static bool
3127 var_needs_tls_wrapper (tree var)
3129 return (!error_operand_p (var)
3130 && DECL_THREAD_LOCAL_P (var)
3131 && !DECL_GNU_TLS_P (var)
3132 && !DECL_FUNCTION_SCOPE_P (var)
3133 && !var_defined_without_dynamic_init (var));
3136 /* Get the FUNCTION_DECL for the shared TLS init function for this
3137 translation unit. */
3139 static tree
3140 get_local_tls_init_fn (void)
3142 tree sname = get_identifier ("__tls_init");
3143 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3144 if (!fn)
3146 fn = build_lang_decl (FUNCTION_DECL, sname,
3147 build_function_type (void_type_node,
3148 void_list_node));
3149 SET_DECL_LANGUAGE (fn, lang_c);
3150 TREE_PUBLIC (fn) = false;
3151 DECL_ARTIFICIAL (fn) = true;
3152 mark_used (fn);
3153 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3155 return fn;
3158 /* Get a FUNCTION_DECL for the init function for the thread_local
3159 variable VAR. The init function will be an alias to the function
3160 that initializes all the non-local TLS variables in the translation
3161 unit. The init function is only used by the wrapper function. */
3163 static tree
3164 get_tls_init_fn (tree var)
3166 /* Only C++11 TLS vars need this init fn. */
3167 if (!var_needs_tls_wrapper (var))
3168 return NULL_TREE;
3170 /* If -fno-extern-tls-init, assume that we don't need to call
3171 a tls init function for a variable defined in another TU. */
3172 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3173 return NULL_TREE;
3175 #ifdef ASM_OUTPUT_DEF
3176 /* If the variable is internal, or if we can't generate aliases,
3177 call the local init function directly. */
3178 if (!TREE_PUBLIC (var))
3179 #endif
3180 return get_local_tls_init_fn ();
3182 tree sname = mangle_tls_init_fn (var);
3183 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3184 if (!fn)
3186 fn = build_lang_decl (FUNCTION_DECL, sname,
3187 build_function_type (void_type_node,
3188 void_list_node));
3189 SET_DECL_LANGUAGE (fn, lang_c);
3190 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3191 DECL_ARTIFICIAL (fn) = true;
3192 DECL_COMDAT (fn) = DECL_COMDAT (var);
3193 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3194 if (DECL_ONE_ONLY (var))
3195 make_decl_one_only (fn, cxx_comdat_group (fn));
3196 if (TREE_PUBLIC (var))
3198 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3199 /* If the variable is defined somewhere else and might have static
3200 initialization, make the init function a weak reference. */
3201 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3202 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3203 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3204 && DECL_EXTERNAL (var))
3205 declare_weak (fn);
3206 else
3207 DECL_WEAK (fn) = DECL_WEAK (var);
3209 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3210 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3211 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3212 DECL_IGNORED_P (fn) = 1;
3213 mark_used (fn);
3215 DECL_BEFRIENDING_CLASSES (fn) = var;
3217 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3219 return fn;
3222 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3223 variable VAR. The wrapper function calls the init function (if any) for
3224 VAR and then returns a reference to VAR. The wrapper function is used
3225 in place of VAR everywhere VAR is mentioned. */
3227 tree
3228 get_tls_wrapper_fn (tree var)
3230 /* Only C++11 TLS vars need this wrapper fn. */
3231 if (!var_needs_tls_wrapper (var))
3232 return NULL_TREE;
3234 tree sname = mangle_tls_wrapper_fn (var);
3235 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3236 if (!fn)
3238 /* A named rvalue reference is an lvalue, so the wrapper should
3239 always return an lvalue reference. */
3240 tree type = non_reference (TREE_TYPE (var));
3241 type = build_reference_type (type);
3242 tree fntype = build_function_type (type, void_list_node);
3243 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3244 SET_DECL_LANGUAGE (fn, lang_c);
3245 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3246 DECL_ARTIFICIAL (fn) = true;
3247 DECL_IGNORED_P (fn) = 1;
3248 /* The wrapper is inline and emitted everywhere var is used. */
3249 DECL_DECLARED_INLINE_P (fn) = true;
3250 if (TREE_PUBLIC (var))
3252 comdat_linkage (fn);
3253 #ifdef HAVE_GAS_HIDDEN
3254 /* Make the wrapper bind locally; there's no reason to share
3255 the wrapper between multiple shared objects. */
3256 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3257 DECL_VISIBILITY_SPECIFIED (fn) = true;
3258 #endif
3260 if (!TREE_PUBLIC (fn))
3261 DECL_INTERFACE_KNOWN (fn) = true;
3262 mark_used (fn);
3263 note_vague_linkage_fn (fn);
3265 #if 0
3266 /* We want CSE to commonize calls to the wrapper, but marking it as
3267 pure is unsafe since it has side-effects. I guess we need a new
3268 ECF flag even weaker than ECF_PURE. FIXME! */
3269 DECL_PURE_P (fn) = true;
3270 #endif
3272 DECL_BEFRIENDING_CLASSES (fn) = var;
3274 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3276 return fn;
3279 /* At EOF, generate the definition for the TLS wrapper function FN:
3281 T& var_wrapper() {
3282 if (init_fn) init_fn();
3283 return var;
3284 } */
3286 static void
3287 generate_tls_wrapper (tree fn)
3289 tree var = DECL_BEFRIENDING_CLASSES (fn);
3291 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3292 tree body = begin_function_body ();
3293 /* Only call the init fn if there might be one. */
3294 if (tree init_fn = get_tls_init_fn (var))
3296 tree if_stmt = NULL_TREE;
3297 /* If init_fn is a weakref, make sure it exists before calling. */
3298 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3300 if_stmt = begin_if_stmt ();
3301 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3302 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3303 NE_EXPR, addr, nullptr_node,
3304 tf_warning_or_error);
3305 finish_if_stmt_cond (cond, if_stmt);
3307 finish_expr_stmt (build_cxx_call
3308 (init_fn, 0, NULL, tf_warning_or_error));
3309 if (if_stmt)
3311 finish_then_clause (if_stmt);
3312 finish_if_stmt (if_stmt);
3315 else
3316 /* If there's no initialization, the wrapper is a constant function. */
3317 TREE_READONLY (fn) = true;
3318 finish_return_stmt (convert_from_reference (var));
3319 finish_function_body (body);
3320 expand_or_defer_fn (finish_function (0));
3323 /* Start the process of running a particular set of global constructors
3324 or destructors. Subroutine of do_[cd]tors. Also called from
3325 vtv_start_verification_constructor_init_function. */
3327 static tree
3328 start_objects (int method_type, int initp)
3330 tree body;
3331 tree fndecl;
3332 char type[14];
3334 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3336 if (initp != DEFAULT_INIT_PRIORITY)
3338 char joiner;
3340 #ifdef JOINER
3341 joiner = JOINER;
3342 #else
3343 joiner = '_';
3344 #endif
3346 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3348 else
3349 sprintf (type, "sub_%c", method_type);
3351 fndecl = build_lang_decl (FUNCTION_DECL,
3352 get_file_function_name (type),
3353 build_function_type_list (void_type_node,
3354 NULL_TREE));
3355 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3357 TREE_PUBLIC (current_function_decl) = 0;
3359 /* Mark as artificial because it's not explicitly in the user's
3360 source code. */
3361 DECL_ARTIFICIAL (current_function_decl) = 1;
3363 /* Mark this declaration as used to avoid spurious warnings. */
3364 TREE_USED (current_function_decl) = 1;
3366 /* Mark this function as a global constructor or destructor. */
3367 if (method_type == 'I')
3368 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3369 else
3370 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3372 body = begin_compound_stmt (BCS_FN_BODY);
3374 return body;
3377 /* Finish the process of running a particular set of global constructors
3378 or destructors. Subroutine of do_[cd]tors. */
3380 static void
3381 finish_objects (int method_type, int initp, tree body)
3383 tree fn;
3385 /* Finish up. */
3386 finish_compound_stmt (body);
3387 fn = finish_function (0);
3389 if (method_type == 'I')
3391 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3392 decl_init_priority_insert (fn, initp);
3394 else
3396 DECL_STATIC_DESTRUCTOR (fn) = 1;
3397 decl_fini_priority_insert (fn, initp);
3400 expand_or_defer_fn (fn);
3403 /* The names of the parameters to the function created to handle
3404 initializations and destructions for objects with static storage
3405 duration. */
3406 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3407 #define PRIORITY_IDENTIFIER "__priority"
3409 /* The name of the function we create to handle initializations and
3410 destructions for objects with static storage duration. */
3411 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3413 /* The declaration for the __INITIALIZE_P argument. */
3414 static GTY(()) tree initialize_p_decl;
3416 /* The declaration for the __PRIORITY argument. */
3417 static GTY(()) tree priority_decl;
3419 /* The declaration for the static storage duration function. */
3420 static GTY(()) tree ssdf_decl;
3422 /* All the static storage duration functions created in this
3423 translation unit. */
3424 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3426 /* A map from priority levels to information about that priority
3427 level. There may be many such levels, so efficient lookup is
3428 important. */
3429 static splay_tree priority_info_map;
3431 /* Begins the generation of the function that will handle all
3432 initialization and destruction of objects with static storage
3433 duration. The function generated takes two parameters of type
3434 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3435 nonzero, it performs initializations. Otherwise, it performs
3436 destructions. It only performs those initializations or
3437 destructions with the indicated __PRIORITY. The generated function
3438 returns no value.
3440 It is assumed that this function will only be called once per
3441 translation unit. */
3443 static tree
3444 start_static_storage_duration_function (unsigned count)
3446 tree type;
3447 tree body;
3448 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3450 /* Create the identifier for this function. It will be of the form
3451 SSDF_IDENTIFIER_<number>. */
3452 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3454 type = build_function_type_list (void_type_node,
3455 integer_type_node, integer_type_node,
3456 NULL_TREE);
3458 /* Create the FUNCTION_DECL itself. */
3459 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3460 get_identifier (id),
3461 type);
3462 TREE_PUBLIC (ssdf_decl) = 0;
3463 DECL_ARTIFICIAL (ssdf_decl) = 1;
3465 /* Put this function in the list of functions to be called from the
3466 static constructors and destructors. */
3467 if (!ssdf_decls)
3469 vec_alloc (ssdf_decls, 32);
3471 /* Take this opportunity to initialize the map from priority
3472 numbers to information about that priority level. */
3473 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3474 /*delete_key_fn=*/0,
3475 /*delete_value_fn=*/
3476 (splay_tree_delete_value_fn) &free);
3478 /* We always need to generate functions for the
3479 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3480 priorities later, we'll be sure to find the
3481 DEFAULT_INIT_PRIORITY. */
3482 get_priority_info (DEFAULT_INIT_PRIORITY);
3485 vec_safe_push (ssdf_decls, ssdf_decl);
3487 /* Create the argument list. */
3488 initialize_p_decl = cp_build_parm_decl
3489 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3490 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3491 TREE_USED (initialize_p_decl) = 1;
3492 priority_decl = cp_build_parm_decl
3493 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3494 DECL_CONTEXT (priority_decl) = ssdf_decl;
3495 TREE_USED (priority_decl) = 1;
3497 DECL_CHAIN (initialize_p_decl) = priority_decl;
3498 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3500 /* Put the function in the global scope. */
3501 pushdecl (ssdf_decl);
3503 /* Start the function itself. This is equivalent to declaring the
3504 function as:
3506 static void __ssdf (int __initialize_p, init __priority_p);
3508 It is static because we only need to call this function from the
3509 various constructor and destructor functions for this module. */
3510 start_preparsed_function (ssdf_decl,
3511 /*attrs=*/NULL_TREE,
3512 SF_PRE_PARSED);
3514 /* Set up the scope of the outermost block in the function. */
3515 body = begin_compound_stmt (BCS_FN_BODY);
3517 return body;
3520 /* Finish the generation of the function which performs initialization
3521 and destruction of objects with static storage duration. After
3522 this point, no more such objects can be created. */
3524 static void
3525 finish_static_storage_duration_function (tree body)
3527 /* Close out the function. */
3528 finish_compound_stmt (body);
3529 expand_or_defer_fn (finish_function (0));
3532 /* Return the information about the indicated PRIORITY level. If no
3533 code to handle this level has yet been generated, generate the
3534 appropriate prologue. */
3536 static priority_info
3537 get_priority_info (int priority)
3539 priority_info pi;
3540 splay_tree_node n;
3542 n = splay_tree_lookup (priority_info_map,
3543 (splay_tree_key) priority);
3544 if (!n)
3546 /* Create a new priority information structure, and insert it
3547 into the map. */
3548 pi = XNEW (struct priority_info_s);
3549 pi->initializations_p = 0;
3550 pi->destructions_p = 0;
3551 splay_tree_insert (priority_info_map,
3552 (splay_tree_key) priority,
3553 (splay_tree_value) pi);
3555 else
3556 pi = (priority_info) n->value;
3558 return pi;
3561 /* The effective initialization priority of a DECL. */
3563 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3564 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3565 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3567 /* Whether a DECL needs a guard to protect it against multiple
3568 initialization. */
3570 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3571 || DECL_ONE_ONLY (decl) \
3572 || DECL_WEAK (decl)))
3574 /* Called from one_static_initialization_or_destruction(),
3575 via walk_tree.
3576 Walks the initializer list of a global variable and looks for
3577 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3578 and that have their DECL_CONTEXT() == NULL.
3579 For each such temporary variable, set their DECL_CONTEXT() to
3580 the current function. This is necessary because otherwise
3581 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3582 when trying to refer to a temporary variable that does not have
3583 it's DECL_CONTECT() properly set. */
3584 static tree
3585 fix_temporary_vars_context_r (tree *node,
3586 int * /*unused*/,
3587 void * /*unused1*/)
3589 gcc_assert (current_function_decl);
3591 if (TREE_CODE (*node) == BIND_EXPR)
3593 tree var;
3595 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3596 if (VAR_P (var)
3597 && !DECL_NAME (var)
3598 && DECL_ARTIFICIAL (var)
3599 && !DECL_CONTEXT (var))
3600 DECL_CONTEXT (var) = current_function_decl;
3603 return NULL_TREE;
3606 /* Set up to handle the initialization or destruction of DECL. If
3607 INITP is nonzero, we are initializing the variable. Otherwise, we
3608 are destroying it. */
3610 static void
3611 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3613 tree guard_if_stmt = NULL_TREE;
3614 tree guard;
3616 /* If we are supposed to destruct and there's a trivial destructor,
3617 nothing has to be done. */
3618 if (!initp
3619 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3620 return;
3622 /* Trick the compiler into thinking we are at the file and line
3623 where DECL was declared so that error-messages make sense, and so
3624 that the debugger will show somewhat sensible file and line
3625 information. */
3626 input_location = DECL_SOURCE_LOCATION (decl);
3628 /* Make sure temporary variables in the initialiser all have
3629 their DECL_CONTEXT() set to a value different from NULL_TREE.
3630 This can happen when global variables initialisers are built.
3631 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3632 the temporary variables that might have been generated in the
3633 accompagning initialisers is NULL_TREE, meaning the variables have been
3634 declared in the global namespace.
3635 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3636 of the temporaries are set to the current function decl. */
3637 cp_walk_tree_without_duplicates (&init,
3638 fix_temporary_vars_context_r,
3639 NULL);
3641 /* Because of:
3643 [class.access.spec]
3645 Access control for implicit calls to the constructors,
3646 the conversion functions, or the destructor called to
3647 create and destroy a static data member is performed as
3648 if these calls appeared in the scope of the member's
3649 class.
3651 we pretend we are in a static member function of the class of
3652 which the DECL is a member. */
3653 if (member_p (decl))
3655 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3656 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3659 /* Assume we don't need a guard. */
3660 guard = NULL_TREE;
3661 /* We need a guard if this is an object with external linkage that
3662 might be initialized in more than one place. (For example, a
3663 static data member of a template, when the data member requires
3664 construction.) */
3665 if (NEEDS_GUARD_P (decl))
3667 tree guard_cond;
3669 guard = get_guard (decl);
3671 /* When using __cxa_atexit, we just check the GUARD as we would
3672 for a local static. */
3673 if (flag_use_cxa_atexit)
3675 /* When using __cxa_atexit, we never try to destroy
3676 anything from a static destructor. */
3677 gcc_assert (initp);
3678 guard_cond = get_guard_cond (guard);
3680 /* If we don't have __cxa_atexit, then we will be running
3681 destructors from .fini sections, or their equivalents. So,
3682 we need to know how many times we've tried to initialize this
3683 object. We do initializations only if the GUARD is zero,
3684 i.e., if we are the first to initialize the variable. We do
3685 destructions only if the GUARD is one, i.e., if we are the
3686 last to destroy the variable. */
3687 else if (initp)
3688 guard_cond
3689 = cp_build_binary_op (input_location,
3690 EQ_EXPR,
3691 cp_build_unary_op (PREINCREMENT_EXPR,
3692 guard,
3693 /*noconvert=*/1,
3694 tf_warning_or_error),
3695 integer_one_node,
3696 tf_warning_or_error);
3697 else
3698 guard_cond
3699 = cp_build_binary_op (input_location,
3700 EQ_EXPR,
3701 cp_build_unary_op (PREDECREMENT_EXPR,
3702 guard,
3703 /*noconvert=*/1,
3704 tf_warning_or_error),
3705 integer_zero_node,
3706 tf_warning_or_error);
3708 guard_if_stmt = begin_if_stmt ();
3709 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3713 /* If we're using __cxa_atexit, we have not already set the GUARD,
3714 so we must do so now. */
3715 if (guard && initp && flag_use_cxa_atexit)
3716 finish_expr_stmt (set_guard (guard));
3718 /* Perform the initialization or destruction. */
3719 if (initp)
3721 if (init)
3723 finish_expr_stmt (init);
3724 if (flag_sanitize & SANITIZE_ADDRESS)
3726 varpool_node *vnode = varpool_node::get (decl);
3727 if (vnode)
3728 vnode->dynamically_initialized = 1;
3732 /* If we're using __cxa_atexit, register a function that calls the
3733 destructor for the object. */
3734 if (flag_use_cxa_atexit)
3735 finish_expr_stmt (register_dtor_fn (decl));
3737 else
3738 finish_expr_stmt (build_cleanup (decl));
3740 /* Finish the guard if-stmt, if necessary. */
3741 if (guard)
3743 finish_then_clause (guard_if_stmt);
3744 finish_if_stmt (guard_if_stmt);
3747 /* Now that we're done with DECL we don't need to pretend to be a
3748 member of its class any longer. */
3749 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3750 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3753 /* Generate code to do the initialization or destruction of the decls in VARS,
3754 a TREE_LIST of VAR_DECL with static storage duration.
3755 Whether initialization or destruction is performed is specified by INITP. */
3757 static void
3758 do_static_initialization_or_destruction (tree vars, bool initp)
3760 tree node, init_if_stmt, cond;
3762 /* Build the outer if-stmt to check for initialization or destruction. */
3763 init_if_stmt = begin_if_stmt ();
3764 cond = initp ? integer_one_node : integer_zero_node;
3765 cond = cp_build_binary_op (input_location,
3766 EQ_EXPR,
3767 initialize_p_decl,
3768 cond,
3769 tf_warning_or_error);
3770 finish_if_stmt_cond (cond, init_if_stmt);
3772 /* To make sure dynamic construction doesn't access globals from other
3773 compilation units where they might not be yet constructed, for
3774 -fsanitize=address insert __asan_before_dynamic_init call that
3775 prevents access to either all global variables that need construction
3776 in other compilation units, or at least those that haven't been
3777 initialized yet. Variables that need dynamic construction in
3778 the current compilation unit are kept accessible. */
3779 if (flag_sanitize & SANITIZE_ADDRESS)
3780 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3782 node = vars;
3783 do {
3784 tree decl = TREE_VALUE (node);
3785 tree priority_if_stmt;
3786 int priority;
3787 priority_info pi;
3789 /* If we don't need a destructor, there's nothing to do. Avoid
3790 creating a possibly empty if-stmt. */
3791 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3793 node = TREE_CHAIN (node);
3794 continue;
3797 /* Remember that we had an initialization or finalization at this
3798 priority. */
3799 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3800 pi = get_priority_info (priority);
3801 if (initp)
3802 pi->initializations_p = 1;
3803 else
3804 pi->destructions_p = 1;
3806 /* Conditionalize this initialization on being in the right priority
3807 and being initializing/finalizing appropriately. */
3808 priority_if_stmt = begin_if_stmt ();
3809 cond = cp_build_binary_op (input_location,
3810 EQ_EXPR,
3811 priority_decl,
3812 build_int_cst (NULL_TREE, priority),
3813 tf_warning_or_error);
3814 finish_if_stmt_cond (cond, priority_if_stmt);
3816 /* Process initializers with same priority. */
3817 for (; node
3818 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3819 node = TREE_CHAIN (node))
3820 /* Do one initialization or destruction. */
3821 one_static_initialization_or_destruction (TREE_VALUE (node),
3822 TREE_PURPOSE (node), initp);
3824 /* Finish up the priority if-stmt body. */
3825 finish_then_clause (priority_if_stmt);
3826 finish_if_stmt (priority_if_stmt);
3828 } while (node);
3830 /* Revert what __asan_before_dynamic_init did by calling
3831 __asan_after_dynamic_init. */
3832 if (flag_sanitize & SANITIZE_ADDRESS)
3833 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3835 /* Finish up the init/destruct if-stmt body. */
3836 finish_then_clause (init_if_stmt);
3837 finish_if_stmt (init_if_stmt);
3840 /* VARS is a list of variables with static storage duration which may
3841 need initialization and/or finalization. Remove those variables
3842 that don't really need to be initialized or finalized, and return
3843 the resulting list. The order in which the variables appear in
3844 VARS is in reverse order of the order in which they should actually
3845 be initialized. The list we return is in the unreversed order;
3846 i.e., the first variable should be initialized first. */
3848 static tree
3849 prune_vars_needing_no_initialization (tree *vars)
3851 tree *var = vars;
3852 tree result = NULL_TREE;
3854 while (*var)
3856 tree t = *var;
3857 tree decl = TREE_VALUE (t);
3858 tree init = TREE_PURPOSE (t);
3860 /* Deal gracefully with error. */
3861 if (decl == error_mark_node)
3863 var = &TREE_CHAIN (t);
3864 continue;
3867 /* The only things that can be initialized are variables. */
3868 gcc_assert (VAR_P (decl));
3870 /* If this object is not defined, we don't need to do anything
3871 here. */
3872 if (DECL_EXTERNAL (decl))
3874 var = &TREE_CHAIN (t);
3875 continue;
3878 /* Also, if the initializer already contains errors, we can bail
3879 out now. */
3880 if (init && TREE_CODE (init) == TREE_LIST
3881 && value_member (error_mark_node, init))
3883 var = &TREE_CHAIN (t);
3884 continue;
3887 /* This variable is going to need initialization and/or
3888 finalization, so we add it to the list. */
3889 *var = TREE_CHAIN (t);
3890 TREE_CHAIN (t) = result;
3891 result = t;
3894 return result;
3897 /* Make sure we have told the back end about all the variables in
3898 VARS. */
3900 static void
3901 write_out_vars (tree vars)
3903 tree v;
3905 for (v = vars; v; v = TREE_CHAIN (v))
3907 tree var = TREE_VALUE (v);
3908 if (!var_finalized_p (var))
3910 import_export_decl (var);
3911 rest_of_decl_compilation (var, 1, 1);
3916 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3917 (otherwise) that will initialize all global objects with static
3918 storage duration having the indicated PRIORITY. */
3920 static void
3921 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3922 location_t *locus)
3924 char function_key;
3925 tree fndecl;
3926 tree body;
3927 size_t i;
3929 input_location = *locus;
3930 /* ??? */
3931 /* Was: locus->line++; */
3933 /* We use `I' to indicate initialization and `D' to indicate
3934 destruction. */
3935 function_key = constructor_p ? 'I' : 'D';
3937 /* We emit the function lazily, to avoid generating empty
3938 global constructors and destructors. */
3939 body = NULL_TREE;
3941 /* For Objective-C++, we may need to initialize metadata found in this module.
3942 This must be done _before_ any other static initializations. */
3943 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3944 && constructor_p && objc_static_init_needed_p ())
3946 body = start_objects (function_key, priority);
3947 objc_generate_static_init_call (NULL_TREE);
3950 /* Call the static storage duration function with appropriate
3951 arguments. */
3952 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3954 /* Calls to pure or const functions will expand to nothing. */
3955 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3957 tree call;
3959 if (! body)
3960 body = start_objects (function_key, priority);
3962 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3963 build_int_cst (NULL_TREE,
3964 constructor_p),
3965 build_int_cst (NULL_TREE,
3966 priority),
3967 NULL_TREE);
3968 finish_expr_stmt (call);
3972 /* Close out the function. */
3973 if (body)
3974 finish_objects (function_key, priority, body);
3977 /* Generate constructor and destructor functions for the priority
3978 indicated by N. */
3980 static int
3981 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3983 location_t *locus = (location_t *) data;
3984 int priority = (int) n->key;
3985 priority_info pi = (priority_info) n->value;
3987 /* Generate the functions themselves, but only if they are really
3988 needed. */
3989 if (pi->initializations_p)
3990 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3991 if (pi->destructions_p)
3992 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3994 /* Keep iterating. */
3995 return 0;
3998 /* Java requires that we be able to reference a local address for a
3999 method, and not be confused by PLT entries. If supported, create a
4000 hidden alias for all such methods. */
4002 static void
4003 build_java_method_aliases (void)
4005 #ifndef HAVE_GAS_HIDDEN
4006 return;
4007 #endif
4009 struct cgraph_node *node;
4010 FOR_EACH_FUNCTION (node)
4012 tree fndecl = node->decl;
4014 if (DECL_CLASS_SCOPE_P (fndecl)
4015 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4016 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4018 /* Mangle the name in a predictable way; we need to reference
4019 this from a java compiled object file. */
4020 tree oid = DECL_ASSEMBLER_NAME (fndecl);
4021 const char *oname = IDENTIFIER_POINTER (oid);
4022 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4023 char *nname = ACONCAT (("_ZGA", oname + 2, NULL));
4025 tree alias = make_alias_for (fndecl, get_identifier (nname));
4026 TREE_PUBLIC (alias) = 1;
4027 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4029 cgraph_node::create_same_body_alias (alias, fndecl);
4034 /* Return C++ property of T, based on given operation OP. */
4036 static int
4037 cpp_check (tree t, cpp_operation op)
4039 switch (op)
4041 case IS_ABSTRACT:
4042 return DECL_PURE_VIRTUAL_P (t);
4043 case IS_CONSTRUCTOR:
4044 return DECL_CONSTRUCTOR_P (t);
4045 case IS_DESTRUCTOR:
4046 return DECL_DESTRUCTOR_P (t);
4047 case IS_COPY_CONSTRUCTOR:
4048 return DECL_COPY_CONSTRUCTOR_P (t);
4049 case IS_TEMPLATE:
4050 return TREE_CODE (t) == TEMPLATE_DECL;
4051 case IS_TRIVIAL:
4052 return trivial_type_p (t);
4053 default:
4054 return 0;
4058 /* Collect source file references recursively, starting from NAMESPC. */
4060 static void
4061 collect_source_refs (tree namespc)
4063 tree t;
4065 if (!namespc)
4066 return;
4068 /* Iterate over names in this name space. */
4069 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4070 if (!DECL_IS_BUILTIN (t) )
4071 collect_source_ref (DECL_SOURCE_FILE (t));
4073 /* Dump siblings, if any */
4074 collect_source_refs (TREE_CHAIN (namespc));
4076 /* Dump children, if any */
4077 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4080 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4081 starting from NAMESPC. */
4083 static void
4084 collect_ada_namespace (tree namespc, const char *source_file)
4086 if (!namespc)
4087 return;
4089 /* Collect decls from this namespace */
4090 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4092 /* Collect siblings, if any */
4093 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4095 /* Collect children, if any */
4096 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4099 /* Returns true iff there is a definition available for variable or
4100 function DECL. */
4102 static bool
4103 decl_defined_p (tree decl)
4105 if (TREE_CODE (decl) == FUNCTION_DECL)
4106 return (DECL_INITIAL (decl) != NULL_TREE);
4107 else
4109 gcc_assert (VAR_P (decl));
4110 return !DECL_EXTERNAL (decl);
4114 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4116 [expr.const]
4118 An integral constant-expression can only involve ... const
4119 variables of integral or enumeration types initialized with
4120 constant expressions ...
4122 C++0x also allows constexpr variables and temporaries initialized
4123 with constant expressions. We handle the former here, but the latter
4124 are just folded away in cxx_eval_constant_expression.
4126 The standard does not require that the expression be non-volatile.
4127 G++ implements the proposed correction in DR 457. */
4129 bool
4130 decl_constant_var_p (tree decl)
4132 if (!decl_maybe_constant_var_p (decl))
4133 return false;
4135 /* We don't know if a template static data member is initialized with
4136 a constant expression until we instantiate its initializer. Even
4137 in the case of a constexpr variable, we can't treat it as a
4138 constant until its initializer is complete in case it's used in
4139 its own initializer. */
4140 mark_used (decl);
4141 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4144 /* Returns true if DECL could be a symbolic constant variable, depending on
4145 its initializer. */
4147 bool
4148 decl_maybe_constant_var_p (tree decl)
4150 tree type = TREE_TYPE (decl);
4151 if (!VAR_P (decl))
4152 return false;
4153 if (DECL_DECLARED_CONSTEXPR_P (decl))
4154 return true;
4155 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4156 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4159 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4160 called from grokfndecl and grokvardecl; in all modes it is called from
4161 cp_write_global_declarations. */
4163 void
4164 no_linkage_error (tree decl)
4166 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4167 /* In C++11 it's ok if the decl is defined. */
4168 return;
4169 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4170 if (t == NULL_TREE)
4171 /* The type that got us on no_linkage_decls must have gotten a name for
4172 linkage purposes. */;
4173 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4174 /* The type might end up having a typedef name for linkage purposes. */
4175 vec_safe_push (no_linkage_decls, decl);
4176 else if (TYPE_ANONYMOUS_P (t))
4178 bool d = false;
4179 if (cxx_dialect >= cxx11)
4180 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4181 "anonymous type, is used but never defined", decl);
4182 else if (DECL_EXTERN_C_P (decl))
4183 /* Allow this; it's pretty common in C. */;
4184 else if (TREE_CODE (decl) == VAR_DECL)
4185 /* DRs 132, 319 and 389 seem to indicate types with
4186 no linkage can only be used to declare extern "C"
4187 entities. Since it's not always an error in the
4188 ISO C++ 90 Standard, we only issue a warning. */
4189 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4190 "with no linkage used to declare variable %q#D with "
4191 "linkage", decl);
4192 else
4193 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4194 "linkage used to declare function %q#D with linkage",
4195 decl);
4196 if (d && is_typedef_decl (TYPE_NAME (t)))
4197 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4198 "to the unqualified type, so it is not used for linkage",
4199 TYPE_NAME (t));
4201 else if (cxx_dialect >= cxx11)
4202 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4203 "%qT, is used but never defined", decl, t);
4204 else if (TREE_CODE (decl) == VAR_DECL)
4205 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4206 "used to declare variable %q#D with linkage", t, decl);
4207 else
4208 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4209 "to declare function %q#D with linkage", t, decl);
4212 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4214 static void
4215 collect_all_refs (const char *source_file)
4217 collect_ada_namespace (global_namespace, source_file);
4220 /* Clear DECL_EXTERNAL for NODE. */
4222 static bool
4223 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4225 DECL_EXTERNAL (node->decl) = 0;
4226 return false;
4229 /* Build up the function to run dynamic initializers for thread_local
4230 variables in this translation unit and alias the init functions for the
4231 individual variables to it. */
4233 static void
4234 handle_tls_init (void)
4236 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4237 if (vars == NULL_TREE)
4238 return;
4240 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4242 write_out_vars (vars);
4244 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4245 boolean_type_node);
4246 TREE_PUBLIC (guard) = false;
4247 TREE_STATIC (guard) = true;
4248 DECL_ARTIFICIAL (guard) = true;
4249 DECL_IGNORED_P (guard) = true;
4250 TREE_USED (guard) = true;
4251 set_decl_tls_model (guard, decl_default_tls_model (guard));
4252 pushdecl_top_level_and_finish (guard, NULL_TREE);
4254 tree fn = get_local_tls_init_fn ();
4255 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4256 tree body = begin_function_body ();
4257 tree if_stmt = begin_if_stmt ();
4258 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4259 tf_warning_or_error);
4260 finish_if_stmt_cond (cond, if_stmt);
4261 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4262 tf_warning_or_error));
4263 for (; vars; vars = TREE_CHAIN (vars))
4265 tree var = TREE_VALUE (vars);
4266 tree init = TREE_PURPOSE (vars);
4267 one_static_initialization_or_destruction (var, init, true);
4269 #ifdef ASM_OUTPUT_DEF
4270 /* Output init aliases even with -fno-extern-tls-init. */
4271 if (TREE_PUBLIC (var))
4273 tree single_init_fn = get_tls_init_fn (var);
4274 if (single_init_fn == NULL_TREE)
4275 continue;
4276 cgraph_node *alias
4277 = cgraph_node::get_create (fn)->create_same_body_alias
4278 (single_init_fn, fn);
4279 gcc_assert (alias != NULL);
4281 #endif
4284 finish_then_clause (if_stmt);
4285 finish_if_stmt (if_stmt);
4286 finish_function_body (body);
4287 expand_or_defer_fn (finish_function (0));
4290 /* The entire file is now complete. If requested, dump everything
4291 to a file. */
4293 static void
4294 dump_tu (void)
4296 int flags;
4297 FILE *stream = dump_begin (TDI_tu, &flags);
4299 if (stream)
4301 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4302 dump_end (TDI_tu, stream);
4306 /* Check the deallocation functions for CODE to see if we want to warn that
4307 only one was defined. */
4309 static void
4310 maybe_warn_sized_delete (enum tree_code code)
4312 tree sized = NULL_TREE;
4313 tree unsized = NULL_TREE;
4315 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4316 ovl; ovl = OVL_NEXT (ovl))
4318 tree fn = OVL_CURRENT (ovl);
4319 /* We're only interested in usual deallocation functions. */
4320 if (!non_placement_deallocation_fn_p (fn))
4321 continue;
4322 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4323 unsized = fn;
4324 else
4325 sized = fn;
4327 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4328 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4329 "the program should also define %qD", sized);
4330 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4331 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4332 "the program should also define %qD", unsized);
4335 /* Check the global deallocation functions to see if we want to warn about
4336 defining unsized without sized (or vice versa). */
4338 static void
4339 maybe_warn_sized_delete ()
4341 if (!flag_sized_deallocation || !warn_sized_deallocation)
4342 return;
4343 maybe_warn_sized_delete (DELETE_EXPR);
4344 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4347 /* This routine is called at the end of compilation.
4348 Its job is to create all the code needed to initialize and
4349 destroy the global aggregates. We do the destruction
4350 first, since that way we only need to reverse the decls once. */
4352 void
4353 cp_write_global_declarations (void)
4355 tree vars;
4356 bool reconsider;
4357 size_t i;
4358 location_t locus;
4359 unsigned ssdf_count = 0;
4360 int retries = 0;
4361 tree decl;
4363 locus = input_location;
4364 at_eof = 1;
4366 /* Bad parse errors. Just forget about it. */
4367 if (! global_bindings_p () || current_class_type
4368 || !vec_safe_is_empty (decl_namespace_list))
4369 return;
4371 /* This is the point to write out a PCH if we're doing that.
4372 In that case we do not want to do anything else. */
4373 if (pch_file)
4375 c_common_write_pch ();
4376 dump_tu ();
4377 return;
4380 symtab->process_same_body_aliases ();
4382 /* Handle -fdump-ada-spec[-slim] */
4383 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4385 if (flag_dump_ada_spec_slim)
4386 collect_source_ref (main_input_filename);
4387 else
4388 collect_source_refs (global_namespace);
4390 dump_ada_specs (collect_all_refs, cpp_check);
4393 /* FIXME - huh? was input_line -= 1;*/
4395 timevar_start (TV_PHASE_DEFERRED);
4397 /* We now have to write out all the stuff we put off writing out.
4398 These include:
4400 o Template specializations that we have not yet instantiated,
4401 but which are needed.
4402 o Initialization and destruction for non-local objects with
4403 static storage duration. (Local objects with static storage
4404 duration are initialized when their scope is first entered,
4405 and are cleaned up via atexit.)
4406 o Virtual function tables.
4408 All of these may cause others to be needed. For example,
4409 instantiating one function may cause another to be needed, and
4410 generating the initializer for an object may cause templates to be
4411 instantiated, etc., etc. */
4413 emit_support_tinfos ();
4417 tree t;
4418 tree decl;
4420 reconsider = false;
4422 /* If there are templates that we've put off instantiating, do
4423 them now. */
4424 instantiate_pending_templates (retries);
4425 ggc_collect ();
4427 /* Write out virtual tables as required. Note that writing out
4428 the virtual table for a template class may cause the
4429 instantiation of members of that class. If we write out
4430 vtables then we remove the class from our list so we don't
4431 have to look at it again. */
4433 while (keyed_classes != NULL_TREE
4434 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4436 reconsider = true;
4437 keyed_classes = TREE_CHAIN (keyed_classes);
4440 t = keyed_classes;
4441 if (t != NULL_TREE)
4443 tree next = TREE_CHAIN (t);
4445 while (next)
4447 if (maybe_emit_vtables (TREE_VALUE (next)))
4449 reconsider = true;
4450 TREE_CHAIN (t) = TREE_CHAIN (next);
4452 else
4453 t = next;
4455 next = TREE_CHAIN (t);
4459 /* Write out needed type info variables. We have to be careful
4460 looping through unemitted decls, because emit_tinfo_decl may
4461 cause other variables to be needed. New elements will be
4462 appended, and we remove from the vector those that actually
4463 get emitted. */
4464 for (i = unemitted_tinfo_decls->length ();
4465 unemitted_tinfo_decls->iterate (--i, &t);)
4466 if (emit_tinfo_decl (t))
4468 reconsider = true;
4469 unemitted_tinfo_decls->unordered_remove (i);
4472 /* The list of objects with static storage duration is built up
4473 in reverse order. We clear STATIC_AGGREGATES so that any new
4474 aggregates added during the initialization of these will be
4475 initialized in the correct order when we next come around the
4476 loop. */
4477 vars = prune_vars_needing_no_initialization (&static_aggregates);
4479 if (vars)
4481 /* We need to start a new initialization function each time
4482 through the loop. That's because we need to know which
4483 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4484 isn't computed until a function is finished, and written
4485 out. That's a deficiency in the back end. When this is
4486 fixed, these initialization functions could all become
4487 inline, with resulting performance improvements. */
4488 tree ssdf_body;
4490 /* Set the line and file, so that it is obviously not from
4491 the source file. */
4492 input_location = locus;
4493 ssdf_body = start_static_storage_duration_function (ssdf_count);
4495 /* Make sure the back end knows about all the variables. */
4496 write_out_vars (vars);
4498 /* First generate code to do all the initializations. */
4499 if (vars)
4500 do_static_initialization_or_destruction (vars, /*initp=*/true);
4502 /* Then, generate code to do all the destructions. Do these
4503 in reverse order so that the most recently constructed
4504 variable is the first destroyed. If we're using
4505 __cxa_atexit, then we don't need to do this; functions
4506 were registered at initialization time to destroy the
4507 local statics. */
4508 if (!flag_use_cxa_atexit && vars)
4510 vars = nreverse (vars);
4511 do_static_initialization_or_destruction (vars, /*initp=*/false);
4513 else
4514 vars = NULL_TREE;
4516 /* Finish up the static storage duration function for this
4517 round. */
4518 input_location = locus;
4519 finish_static_storage_duration_function (ssdf_body);
4521 /* All those initializations and finalizations might cause
4522 us to need more inline functions, more template
4523 instantiations, etc. */
4524 reconsider = true;
4525 ssdf_count++;
4526 /* ??? was: locus.line++; */
4529 /* Now do the same for thread_local variables. */
4530 handle_tls_init ();
4532 /* Go through the set of inline functions whose bodies have not
4533 been emitted yet. If out-of-line copies of these functions
4534 are required, emit them. */
4535 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4537 /* Does it need synthesizing? */
4538 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4539 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4541 /* Even though we're already at the top-level, we push
4542 there again. That way, when we pop back a few lines
4543 hence, all of our state is restored. Otherwise,
4544 finish_function doesn't clean things up, and we end
4545 up with CURRENT_FUNCTION_DECL set. */
4546 push_to_top_level ();
4547 /* The decl's location will mark where it was first
4548 needed. Save that so synthesize method can indicate
4549 where it was needed from, in case of error */
4550 input_location = DECL_SOURCE_LOCATION (decl);
4551 synthesize_method (decl);
4552 pop_from_top_level ();
4553 reconsider = true;
4556 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4557 generate_tls_wrapper (decl);
4559 if (!DECL_SAVED_TREE (decl))
4560 continue;
4562 /* We lie to the back end, pretending that some functions
4563 are not defined when they really are. This keeps these
4564 functions from being put out unnecessarily. But, we must
4565 stop lying when the functions are referenced, or if they
4566 are not comdat since they need to be put out now. If
4567 DECL_INTERFACE_KNOWN, then we have already set
4568 DECL_EXTERNAL appropriately, so there's no need to check
4569 again, and we do not want to clear DECL_EXTERNAL if a
4570 previous call to import_export_decl set it.
4572 This is done in a separate for cycle, because if some
4573 deferred function is contained in another deferred
4574 function later in deferred_fns varray,
4575 rest_of_compilation would skip this function and we
4576 really cannot expand the same function twice. */
4577 import_export_decl (decl);
4578 if (DECL_NOT_REALLY_EXTERN (decl)
4579 && DECL_INITIAL (decl)
4580 && decl_needed_p (decl))
4582 struct cgraph_node *node, *next;
4584 node = cgraph_node::get (decl);
4585 if (node->cpp_implicit_alias)
4586 node = node->get_alias_target ();
4588 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4589 NULL, true);
4590 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4591 group, we need to mark all symbols in the same comdat group
4592 that way. */
4593 if (node->same_comdat_group)
4594 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4595 next != node;
4596 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4597 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4598 NULL, true);
4601 /* If we're going to need to write this function out, and
4602 there's already a body for it, create RTL for it now.
4603 (There might be no body if this is a method we haven't
4604 gotten around to synthesizing yet.) */
4605 if (!DECL_EXTERNAL (decl)
4606 && decl_needed_p (decl)
4607 && !TREE_ASM_WRITTEN (decl)
4608 && !cgraph_node::get (decl)->definition)
4610 /* We will output the function; no longer consider it in this
4611 loop. */
4612 DECL_DEFER_OUTPUT (decl) = 0;
4613 /* Generate RTL for this function now that we know we
4614 need it. */
4615 expand_or_defer_fn (decl);
4616 /* If we're compiling -fsyntax-only pretend that this
4617 function has been written out so that we don't try to
4618 expand it again. */
4619 if (flag_syntax_only)
4620 TREE_ASM_WRITTEN (decl) = 1;
4621 reconsider = true;
4625 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4626 reconsider = true;
4628 /* Static data members are just like namespace-scope globals. */
4629 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4631 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4632 /* Don't write it out if we haven't seen a definition. */
4633 || DECL_IN_AGGR_P (decl))
4634 continue;
4635 import_export_decl (decl);
4636 /* If this static data member is needed, provide it to the
4637 back end. */
4638 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4639 DECL_EXTERNAL (decl) = 0;
4641 if (vec_safe_length (pending_statics) != 0
4642 && wrapup_global_declarations (pending_statics->address (),
4643 pending_statics->length ()))
4644 reconsider = true;
4646 retries++;
4648 while (reconsider);
4650 /* All used inline functions must have a definition at this point. */
4651 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4653 if (/* Check online inline functions that were actually used. */
4654 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4655 /* If the definition actually was available here, then the
4656 fact that the function was not defined merely represents
4657 that for some reason (use of a template repository,
4658 #pragma interface, etc.) we decided not to emit the
4659 definition here. */
4660 && !DECL_INITIAL (decl)
4661 /* Don't complain if the template was defined. */
4662 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4663 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4664 (template_for_substitution (decl)))))
4666 warning (0, "inline function %q+D used but never defined", decl);
4667 /* Avoid a duplicate warning from check_global_declaration_1. */
4668 TREE_NO_WARNING (decl) = 1;
4672 /* So must decls that use a type with no linkage. */
4673 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4674 no_linkage_error (decl);
4676 maybe_warn_sized_delete ();
4678 /* Then, do the Objective-C stuff. This is where all the
4679 Objective-C module stuff gets generated (symtab,
4680 class/protocol/selector lists etc). This must be done after C++
4681 templates, destructors etc. so that selectors used in C++
4682 templates are properly allocated. */
4683 if (c_dialect_objc ())
4684 objc_write_global_declarations ();
4686 /* We give C linkage to static constructors and destructors. */
4687 push_lang_context (lang_name_c);
4689 /* Generate initialization and destruction functions for all
4690 priorities for which they are required. */
4691 if (priority_info_map)
4692 splay_tree_foreach (priority_info_map,
4693 generate_ctor_and_dtor_functions_for_priority,
4694 /*data=*/&locus);
4695 else if (c_dialect_objc () && objc_static_init_needed_p ())
4696 /* If this is obj-c++ and we need a static init, call
4697 generate_ctor_or_dtor_function. */
4698 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4699 DEFAULT_INIT_PRIORITY, &locus);
4701 /* We're done with the splay-tree now. */
4702 if (priority_info_map)
4703 splay_tree_delete (priority_info_map);
4705 /* Generate any missing aliases. */
4706 maybe_apply_pending_pragma_weaks ();
4708 /* We're done with static constructors, so we can go back to "C++"
4709 linkage now. */
4710 pop_lang_context ();
4712 /* Generate Java hidden aliases. */
4713 build_java_method_aliases ();
4715 timevar_stop (TV_PHASE_DEFERRED);
4716 timevar_start (TV_PHASE_OPT_GEN);
4718 if (flag_vtable_verify)
4720 vtv_recover_class_info ();
4721 vtv_compute_class_hierarchy_transitive_closure ();
4722 vtv_build_vtable_verify_fndecl ();
4725 symtab->finalize_compilation_unit ();
4727 if (flag_vtable_verify)
4729 /* Generate the special constructor initialization function that
4730 calls __VLTRegisterPairs, and give it a very high
4731 initialization priority. This must be done after
4732 finalize_compilation_unit so that we have accurate
4733 information about which vtable will actually be emitted. */
4734 vtv_generate_init_routine ();
4737 timevar_stop (TV_PHASE_OPT_GEN);
4738 timevar_start (TV_PHASE_CHECK_DBGINFO);
4740 /* Now, issue warnings about static, but not defined, functions,
4741 etc., and emit debugging information. */
4742 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4743 if (vec_safe_length (pending_statics) != 0)
4745 check_global_declarations (pending_statics->address (),
4746 pending_statics->length ());
4747 emit_debug_global_declarations (pending_statics->address (),
4748 pending_statics->length ());
4751 perform_deferred_noexcept_checks ();
4753 finish_repo ();
4755 /* The entire file is now complete. If requested, dump everything
4756 to a file. */
4757 dump_tu ();
4759 if (flag_detailed_statistics)
4761 dump_tree_statistics ();
4762 dump_time_statistics ();
4764 input_location = locus;
4766 #ifdef ENABLE_CHECKING
4767 validate_conversion_obstack ();
4768 #endif /* ENABLE_CHECKING */
4770 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4773 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4774 function to call in parse-tree form; it has not yet been
4775 semantically analyzed. ARGS are the arguments to the function.
4776 They have already been semantically analyzed. This may change
4777 ARGS. */
4779 tree
4780 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4781 tsubst_flags_t complain)
4783 tree orig_fn;
4784 vec<tree, va_gc> *orig_args = NULL;
4785 tree expr;
4786 tree object;
4788 orig_fn = fn;
4789 object = TREE_OPERAND (fn, 0);
4791 if (processing_template_decl)
4793 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4794 || TREE_CODE (fn) == MEMBER_REF);
4795 if (type_dependent_expression_p (fn)
4796 || any_type_dependent_arguments_p (*args))
4797 return build_nt_call_vec (fn, *args);
4799 orig_args = make_tree_vector_copy (*args);
4801 /* Transform the arguments and add the implicit "this"
4802 parameter. That must be done before the FN is transformed
4803 because we depend on the form of FN. */
4804 make_args_non_dependent (*args);
4805 object = build_non_dependent_expr (object);
4806 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4808 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4809 object = cp_build_addr_expr (object, complain);
4810 vec_safe_insert (*args, 0, object);
4812 /* Now that the arguments are done, transform FN. */
4813 fn = build_non_dependent_expr (fn);
4816 /* A qualified name corresponding to a bound pointer-to-member is
4817 represented as an OFFSET_REF:
4819 struct B { void g(); };
4820 void (B::*p)();
4821 void B::g() { (this->*p)(); } */
4822 if (TREE_CODE (fn) == OFFSET_REF)
4824 tree object_addr = cp_build_addr_expr (object, complain);
4825 fn = TREE_OPERAND (fn, 1);
4826 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4827 complain);
4828 vec_safe_insert (*args, 0, object_addr);
4831 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4832 expr = build_op_call (fn, args, complain);
4833 else
4834 expr = cp_build_function_call_vec (fn, args, complain);
4835 if (processing_template_decl && expr != error_mark_node)
4836 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4838 if (orig_args != NULL)
4839 release_tree_vector (orig_args);
4841 return expr;
4845 void
4846 check_default_args (tree x)
4848 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4849 bool saw_def = false;
4850 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4851 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4853 if (TREE_PURPOSE (arg))
4854 saw_def = true;
4855 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4857 error ("default argument missing for parameter %P of %q+#D", i, x);
4858 TREE_PURPOSE (arg) = error_mark_node;
4863 /* Return true if function DECL can be inlined. This is used to force
4864 instantiation of methods that might be interesting for inlining. */
4865 bool
4866 possibly_inlined_p (tree decl)
4868 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4869 if (DECL_UNINLINABLE (decl))
4870 return false;
4871 if (!optimize || pragma_java_exceptions)
4872 return DECL_DECLARED_INLINE_P (decl);
4873 /* When optimizing, we might inline everything when flatten
4874 attribute or heuristics inlining for size or autoinlining
4875 is used. */
4876 return true;
4879 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4880 If DECL is a specialization or implicitly declared class member,
4881 generate the actual definition. Return false if something goes
4882 wrong, true otherwise. */
4884 bool
4885 mark_used (tree decl, tsubst_flags_t complain)
4887 /* If DECL is a BASELINK for a single function, then treat it just
4888 like the DECL for the function. Otherwise, if the BASELINK is
4889 for an overloaded function, we don't know which function was
4890 actually used until after overload resolution. */
4891 if (BASELINK_P (decl))
4893 decl = BASELINK_FUNCTIONS (decl);
4894 if (really_overloaded_fn (decl))
4895 return true;
4896 decl = OVL_CURRENT (decl);
4899 /* Set TREE_USED for the benefit of -Wunused. */
4900 TREE_USED (decl) = 1;
4901 if (DECL_CLONED_FUNCTION_P (decl))
4902 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4904 /* Mark enumeration types as used. */
4905 if (TREE_CODE (decl) == CONST_DECL)
4906 used_types_insert (DECL_CONTEXT (decl));
4908 if (TREE_CODE (decl) == FUNCTION_DECL)
4909 maybe_instantiate_noexcept (decl);
4911 if (TREE_CODE (decl) == FUNCTION_DECL
4912 && DECL_DELETED_FN (decl))
4914 if (DECL_ARTIFICIAL (decl))
4916 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4917 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4919 /* We mark a lambda conversion op as deleted if we can't
4920 generate it properly; see maybe_add_lambda_conv_op. */
4921 sorry ("converting lambda which uses %<...%> to "
4922 "function pointer");
4923 return false;
4926 if (complain & tf_error)
4928 error ("use of deleted function %qD", decl);
4929 if (!maybe_explain_implicit_delete (decl))
4930 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4932 return false;
4935 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
4936 && deprecated_state != DEPRECATED_SUPPRESS)
4937 warn_deprecated_use (decl, NULL_TREE);
4939 /* We can only check DECL_ODR_USED on variables or functions with
4940 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4941 might need special handling for. */
4942 if (!VAR_OR_FUNCTION_DECL_P (decl)
4943 || DECL_LANG_SPECIFIC (decl) == NULL
4944 || DECL_THUNK_P (decl))
4946 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4948 if (complain & tf_error)
4949 error ("use of %qD before deduction of %<auto%>", decl);
4950 return false;
4952 return true;
4955 /* We only want to do this processing once. We don't need to keep trying
4956 to instantiate inline templates, because unit-at-a-time will make sure
4957 we get them compiled before functions that want to inline them. */
4958 if (DECL_ODR_USED (decl))
4959 return true;
4961 /* If within finish_function, defer the rest until that function
4962 finishes, otherwise it might recurse. */
4963 if (defer_mark_used_calls)
4965 vec_safe_push (deferred_mark_used_calls, decl);
4966 return true;
4969 /* Normally, we can wait until instantiation-time to synthesize DECL.
4970 However, if DECL is a static data member initialized with a constant
4971 or a constexpr function, we need it right now because a reference to
4972 such a data member or a call to such function is not value-dependent.
4973 For a function that uses auto in the return type, we need to instantiate
4974 it to find out its type. For OpenMP user defined reductions, we need
4975 them instantiated for reduction clauses which inline them by hand
4976 directly. */
4977 if (DECL_LANG_SPECIFIC (decl)
4978 && DECL_TEMPLATE_INFO (decl)
4979 && (decl_maybe_constant_var_p (decl)
4980 || (TREE_CODE (decl) == FUNCTION_DECL
4981 && (DECL_DECLARED_CONSTEXPR_P (decl)
4982 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4983 || undeduced_auto_decl (decl))
4984 && !uses_template_parms (DECL_TI_ARGS (decl)))
4986 /* Instantiating a function will result in garbage collection. We
4987 must treat this situation as if we were within the body of a
4988 function so as to avoid collecting live data only referenced from
4989 the stack (such as overload resolution candidates). */
4990 ++function_depth;
4991 instantiate_decl (decl, /*defer_ok=*/false,
4992 /*expl_inst_class_mem_p=*/false);
4993 --function_depth;
4996 if (processing_template_decl || in_template_function ())
4997 return true;
4999 /* Check this too in case we're within instantiate_non_dependent_expr. */
5000 if (DECL_TEMPLATE_INFO (decl)
5001 && uses_template_parms (DECL_TI_ARGS (decl)))
5002 return true;
5004 if (undeduced_auto_decl (decl))
5006 if (complain & tf_error)
5007 error ("use of %qD before deduction of %<auto%>", decl);
5008 return false;
5011 /* If we don't need a value, then we don't need to synthesize DECL. */
5012 if (cp_unevaluated_operand != 0)
5013 return true;
5015 DECL_ODR_USED (decl) = 1;
5016 if (DECL_CLONED_FUNCTION_P (decl))
5017 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5019 /* DR 757: A type without linkage shall not be used as the type of a
5020 variable or function with linkage, unless
5021 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5022 o the variable or function is not used (3.2 [basic.def.odr]) or is
5023 defined in the same translation unit. */
5024 if (cxx_dialect > cxx98
5025 && decl_linkage (decl) != lk_none
5026 && !DECL_EXTERN_C_P (decl)
5027 && !DECL_ARTIFICIAL (decl)
5028 && !decl_defined_p (decl)
5029 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5031 if (is_local_extern (decl))
5032 /* There's no way to define a local extern, and adding it to
5033 the vector interferes with GC, so give an error now. */
5034 no_linkage_error (decl);
5035 else
5036 vec_safe_push (no_linkage_decls, decl);
5039 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5040 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5041 /* Remember it, so we can check it was defined. */
5042 note_vague_linkage_fn (decl);
5044 /* Is it a synthesized method that needs to be synthesized? */
5045 if (TREE_CODE (decl) == FUNCTION_DECL
5046 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5047 && DECL_DEFAULTED_FN (decl)
5048 /* A function defaulted outside the class is synthesized either by
5049 cp_finish_decl or instantiate_decl. */
5050 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5051 && ! DECL_INITIAL (decl))
5053 /* Defer virtual destructors so that thunks get the right
5054 linkage. */
5055 if (DECL_VIRTUAL_P (decl) && !at_eof)
5057 note_vague_linkage_fn (decl);
5058 return true;
5061 /* Remember the current location for a function we will end up
5062 synthesizing. Then we can inform the user where it was
5063 required in the case of error. */
5064 DECL_SOURCE_LOCATION (decl) = input_location;
5066 /* Synthesizing an implicitly defined member function will result in
5067 garbage collection. We must treat this situation as if we were
5068 within the body of a function so as to avoid collecting live data
5069 on the stack (such as overload resolution candidates).
5071 We could just let cp_write_global_declarations handle synthesizing
5072 this function by adding it to deferred_fns, but doing
5073 it at the use site produces better error messages. */
5074 ++function_depth;
5075 synthesize_method (decl);
5076 --function_depth;
5077 /* If this is a synthesized method we don't need to
5078 do the instantiation test below. */
5080 else if (VAR_OR_FUNCTION_DECL_P (decl)
5081 && DECL_TEMPLATE_INFO (decl)
5082 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5083 || always_instantiate_p (decl)))
5084 /* If this is a function or variable that is an instance of some
5085 template, we now know that we will need to actually do the
5086 instantiation. We check that DECL is not an explicit
5087 instantiation because that is not checked in instantiate_decl.
5089 We put off instantiating functions in order to improve compile
5090 times. Maintaining a stack of active functions is expensive,
5091 and the inliner knows to instantiate any functions it might
5092 need. Therefore, we always try to defer instantiation. */
5094 ++function_depth;
5095 instantiate_decl (decl, /*defer_ok=*/true,
5096 /*expl_inst_class_mem_p=*/false);
5097 --function_depth;
5100 return true;
5103 bool
5104 mark_used (tree decl)
5106 return mark_used (decl, tf_warning_or_error);
5109 tree
5110 vtv_start_verification_constructor_init_function (void)
5112 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5115 tree
5116 vtv_finish_verification_constructor_init_function (tree function_body)
5118 tree fn;
5120 finish_compound_stmt (function_body);
5121 fn = finish_function (0);
5122 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5123 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5125 return fn;
5128 #include "gt-cp-decl2.h"