N3778: Sized Deallocation
[official-gcc.git] / gcc / cp / decl2.c
blob07bdd92de93236ccbdea3fe862b20cab3b2ab3b0
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "stringpool.h"
35 #include "varasm.h"
36 #include "attribs.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "flags.h"
40 #include "cp-tree.h"
41 #include "decl.h"
42 #include "toplev.h"
43 #include "timevar.h"
44 #include "cpplib.h"
45 #include "target.h"
46 #include "c-family/c-common.h"
47 #include "c-family/c-objc.h"
48 #include "hash-map.h"
49 #include "is-a.h"
50 #include "plugin-api.h"
51 #include "vec.h"
52 #include "hashtab.h"
53 #include "hash-set.h"
54 #include "machmode.h"
55 #include "hard-reg-set.h"
56 #include "input.h"
57 #include "function.h"
58 #include "ipa-ref.h"
59 #include "cgraph.h"
60 #include "tree-inline.h"
61 #include "c-family/c-pragma.h"
62 #include "dumpfile.h"
63 #include "intl.h"
64 #include "splay-tree.h"
65 #include "langhooks.h"
66 #include "c-family/c-ada-spec.h"
67 #include "asan.h"
69 extern cpp_reader *parse_in;
71 /* This structure contains information about the initializations
72 and/or destructions required for a particular priority level. */
73 typedef struct priority_info_s {
74 /* Nonzero if there have been any initializations at this priority
75 throughout the translation unit. */
76 int initializations_p;
77 /* Nonzero if there have been any destructions at this priority
78 throughout the translation unit. */
79 int destructions_p;
80 } *priority_info;
82 static void mark_vtable_entries (tree);
83 static bool maybe_emit_vtables (tree);
84 static bool acceptable_java_type (tree);
85 static tree start_objects (int, int);
86 static void finish_objects (int, int, tree);
87 static tree start_static_storage_duration_function (unsigned);
88 static void finish_static_storage_duration_function (tree);
89 static priority_info get_priority_info (int);
90 static void do_static_initialization_or_destruction (tree, bool);
91 static void one_static_initialization_or_destruction (tree, tree, bool);
92 static void generate_ctor_or_dtor_function (bool, int, location_t *);
93 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
94 void *);
95 static tree prune_vars_needing_no_initialization (tree *);
96 static void write_out_vars (tree);
97 static void import_export_class (tree);
98 static tree get_guard_bits (tree);
99 static void determine_visibility_from_class (tree, tree);
100 static bool determine_hidden_inline (tree);
101 static bool decl_defined_p (tree);
103 /* A list of static class variables. This is needed, because a
104 static class variable can be declared inside the class without
105 an initializer, and then initialized, statically, outside the class. */
106 static GTY(()) vec<tree, va_gc> *pending_statics;
108 /* A list of functions which were declared inline, but which we
109 may need to emit outline anyway. */
110 static GTY(()) vec<tree, va_gc> *deferred_fns;
112 /* A list of decls that use types with no linkage, which we need to make
113 sure are defined. */
114 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
116 /* Nonzero if we're done parsing and into end-of-file activities. */
118 int at_eof;
121 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
122 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
123 that apply to the function). */
125 tree
126 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
127 cp_ref_qualifier rqual)
129 tree raises;
130 tree attrs;
131 int type_quals;
132 bool late_return_type_p;
134 if (fntype == error_mark_node || ctype == error_mark_node)
135 return error_mark_node;
137 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
138 || TREE_CODE (fntype) == METHOD_TYPE);
140 type_quals = quals & ~TYPE_QUAL_RESTRICT;
141 ctype = cp_build_qualified_type (ctype, type_quals);
142 raises = TYPE_RAISES_EXCEPTIONS (fntype);
143 attrs = TYPE_ATTRIBUTES (fntype);
144 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
145 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
146 (TREE_CODE (fntype) == METHOD_TYPE
147 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
148 : TYPE_ARG_TYPES (fntype)));
149 if (attrs)
150 fntype = cp_build_type_attribute_variant (fntype, attrs);
151 if (rqual)
152 fntype = build_ref_qualified_type (fntype, rqual);
153 if (raises)
154 fntype = build_exception_variant (fntype, raises);
155 if (late_return_type_p)
156 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
158 return fntype;
161 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
162 return type changed to NEW_RET. */
164 tree
165 change_return_type (tree new_ret, tree fntype)
167 tree newtype;
168 tree args = TYPE_ARG_TYPES (fntype);
169 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
170 tree attrs = TYPE_ATTRIBUTES (fntype);
171 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
173 if (new_ret == error_mark_node)
174 return fntype;
176 if (same_type_p (new_ret, TREE_TYPE (fntype)))
177 return fntype;
179 if (TREE_CODE (fntype) == FUNCTION_TYPE)
181 newtype = build_function_type (new_ret, args);
182 newtype = apply_memfn_quals (newtype,
183 type_memfn_quals (fntype),
184 type_memfn_rqual (fntype));
186 else
187 newtype = build_method_type_directly
188 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
189 if (raises)
190 newtype = build_exception_variant (newtype, raises);
191 if (attrs)
192 newtype = cp_build_type_attribute_variant (newtype, attrs);
193 if (late_return_type_p)
194 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
196 return newtype;
199 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
200 appropriately. */
202 tree
203 cp_build_parm_decl (tree name, tree type)
205 tree parm = build_decl (input_location,
206 PARM_DECL, name, type);
207 /* DECL_ARG_TYPE is only used by the back end and the back end never
208 sees templates. */
209 if (!processing_template_decl)
210 DECL_ARG_TYPE (parm) = type_passed_as (type);
212 return parm;
215 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
216 indicated NAME. */
218 tree
219 build_artificial_parm (tree name, tree type)
221 tree parm = cp_build_parm_decl (name, type);
222 DECL_ARTIFICIAL (parm) = 1;
223 /* All our artificial parms are implicitly `const'; they cannot be
224 assigned to. */
225 TREE_READONLY (parm) = 1;
226 return parm;
229 /* Constructors for types with virtual baseclasses need an "in-charge" flag
230 saying whether this constructor is responsible for initialization of
231 virtual baseclasses or not. All destructors also need this "in-charge"
232 flag, which additionally determines whether or not the destructor should
233 free the memory for the object.
235 This function adds the "in-charge" flag to member function FN if
236 appropriate. It is called from grokclassfn and tsubst.
237 FN must be either a constructor or destructor.
239 The in-charge flag follows the 'this' parameter, and is followed by the
240 VTT parm (if any), then the user-written parms. */
242 void
243 maybe_retrofit_in_chrg (tree fn)
245 tree basetype, arg_types, parms, parm, fntype;
247 /* If we've already add the in-charge parameter don't do it again. */
248 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
249 return;
251 /* When processing templates we can't know, in general, whether or
252 not we're going to have virtual baseclasses. */
253 if (processing_template_decl)
254 return;
256 /* We don't need an in-charge parameter for constructors that don't
257 have virtual bases. */
258 if (DECL_CONSTRUCTOR_P (fn)
259 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
260 return;
262 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
263 basetype = TREE_TYPE (TREE_VALUE (arg_types));
264 arg_types = TREE_CHAIN (arg_types);
266 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
268 /* If this is a subobject constructor or destructor, our caller will
269 pass us a pointer to our VTT. */
270 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
272 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
274 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
275 DECL_CHAIN (parm) = parms;
276 parms = parm;
278 /* ...and then to TYPE_ARG_TYPES. */
279 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
281 DECL_HAS_VTT_PARM_P (fn) = 1;
284 /* Then add the in-charge parm (before the VTT parm). */
285 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
286 DECL_CHAIN (parm) = parms;
287 parms = parm;
288 arg_types = hash_tree_chain (integer_type_node, arg_types);
290 /* Insert our new parameter(s) into the list. */
291 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
293 /* And rebuild the function type. */
294 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
295 arg_types);
296 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
297 fntype = build_exception_variant (fntype,
298 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
299 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
300 fntype = (cp_build_type_attribute_variant
301 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
302 TREE_TYPE (fn) = fntype;
304 /* Now we've got the in-charge parameter. */
305 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
308 /* Classes overload their constituent function names automatically.
309 When a function name is declared in a record structure,
310 its name is changed to it overloaded name. Since names for
311 constructors and destructors can conflict, we place a leading
312 '$' for destructors.
314 CNAME is the name of the class we are grokking for.
316 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
318 FLAGS contains bits saying what's special about today's
319 arguments. DTOR_FLAG == DESTRUCTOR.
321 If FUNCTION is a destructor, then we must add the `auto-delete' field
322 as a second parameter. There is some hair associated with the fact
323 that we must "declare" this variable in the manner consistent with the
324 way the rest of the arguments were declared.
326 QUALS are the qualifiers for the this pointer. */
328 void
329 grokclassfn (tree ctype, tree function, enum overload_flags flags)
331 tree fn_name = DECL_NAME (function);
333 /* Even within an `extern "C"' block, members get C++ linkage. See
334 [dcl.link] for details. */
335 SET_DECL_LANGUAGE (function, lang_cplusplus);
337 if (fn_name == NULL_TREE)
339 error ("name missing for member function");
340 fn_name = get_identifier ("<anonymous>");
341 DECL_NAME (function) = fn_name;
344 DECL_CONTEXT (function) = ctype;
346 if (flags == DTOR_FLAG)
347 DECL_DESTRUCTOR_P (function) = 1;
349 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
350 maybe_retrofit_in_chrg (function);
353 /* Create an ARRAY_REF, checking for the user doing things backwards
354 along the way. DECLTYPE_P is for N3276, as in the parser. */
356 tree
357 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
358 bool decltype_p)
360 tree type;
361 tree expr;
362 tree orig_array_expr = array_expr;
363 tree orig_index_exp = index_exp;
365 if (error_operand_p (array_expr) || error_operand_p (index_exp))
366 return error_mark_node;
368 if (processing_template_decl)
370 if (type_dependent_expression_p (array_expr)
371 || type_dependent_expression_p (index_exp))
372 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
373 NULL_TREE, NULL_TREE);
374 array_expr = build_non_dependent_expr (array_expr);
375 index_exp = build_non_dependent_expr (index_exp);
378 type = TREE_TYPE (array_expr);
379 gcc_assert (type);
380 type = non_reference (type);
382 /* If they have an `operator[]', use that. */
383 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
385 tsubst_flags_t complain = tf_warning_or_error;
386 if (decltype_p)
387 complain |= tf_decltype;
388 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
389 index_exp, NULL_TREE, /*overload=*/NULL, complain);
391 else
393 tree p1, p2, i1, i2;
395 /* Otherwise, create an ARRAY_REF for a pointer or array type.
396 It is a little-known fact that, if `a' is an array and `i' is
397 an int, you can write `i[a]', which means the same thing as
398 `a[i]'. */
399 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
400 p1 = array_expr;
401 else
402 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
404 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
405 p2 = index_exp;
406 else
407 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
409 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
410 false);
411 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
412 false);
414 if ((p1 && i2) && (i1 && p2))
415 error ("ambiguous conversion for array subscript");
417 if (p1 && i2)
418 array_expr = p1, index_exp = i2;
419 else if (i1 && p2)
420 array_expr = p2, index_exp = i1;
421 else
423 error ("invalid types %<%T[%T]%> for array subscript",
424 type, TREE_TYPE (index_exp));
425 return error_mark_node;
428 if (array_expr == error_mark_node || index_exp == error_mark_node)
429 error ("ambiguous conversion for array subscript");
431 expr = build_array_ref (input_location, array_expr, index_exp);
433 if (processing_template_decl && expr != error_mark_node)
434 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
435 NULL_TREE, NULL_TREE);
436 return expr;
439 /* Given the cast expression EXP, checking out its validity. Either return
440 an error_mark_node if there was an unavoidable error, return a cast to
441 void for trying to delete a pointer w/ the value 0, or return the
442 call to delete. If DOING_VEC is true, we handle things differently
443 for doing an array delete.
444 Implements ARM $5.3.4. This is called from the parser. */
446 tree
447 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
448 tsubst_flags_t complain)
450 tree t, type;
452 if (exp == error_mark_node)
453 return exp;
455 if (processing_template_decl)
457 t = build_min (DELETE_EXPR, void_type_node, exp, size);
458 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
459 DELETE_EXPR_USE_VEC (t) = doing_vec;
460 TREE_SIDE_EFFECTS (t) = 1;
461 return t;
464 /* An array can't have been allocated by new, so complain. */
465 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
466 warning (0, "deleting array %q#E", exp);
468 t = build_expr_type_conversion (WANT_POINTER, exp, true);
470 if (t == NULL_TREE || t == error_mark_node)
472 error ("type %q#T argument given to %<delete%>, expected pointer",
473 TREE_TYPE (exp));
474 return error_mark_node;
477 type = TREE_TYPE (t);
479 /* As of Valley Forge, you can delete a pointer to const. */
481 /* You can't delete functions. */
482 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
484 error ("cannot delete a function. Only pointer-to-objects are "
485 "valid arguments to %<delete%>");
486 return error_mark_node;
489 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
490 if (VOID_TYPE_P (TREE_TYPE (type)))
492 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
493 doing_vec = 0;
496 /* Deleting a pointer with the value zero is valid and has no effect. */
497 if (integer_zerop (t))
498 return build1 (NOP_EXPR, void_type_node, t);
500 if (doing_vec)
501 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
502 sfk_deleting_destructor,
503 use_global_delete, complain);
504 else
505 return build_delete (type, t, sfk_deleting_destructor,
506 LOOKUP_NORMAL, use_global_delete,
507 complain);
510 /* Report an error if the indicated template declaration is not the
511 sort of thing that should be a member template. */
513 void
514 check_member_template (tree tmpl)
516 tree decl;
518 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
519 decl = DECL_TEMPLATE_RESULT (tmpl);
521 if (TREE_CODE (decl) == FUNCTION_DECL
522 || DECL_ALIAS_TEMPLATE_P (tmpl)
523 || (TREE_CODE (decl) == TYPE_DECL
524 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
526 /* The parser rejects template declarations in local classes
527 (with the exception of generic lambdas). */
528 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
529 /* The parser rejects any use of virtual in a function template. */
530 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
531 && DECL_VIRTUAL_P (decl)));
533 /* The debug-information generating code doesn't know what to do
534 with member templates. */
535 DECL_IGNORED_P (tmpl) = 1;
537 else if (variable_template_p (tmpl))
538 /* OK */;
539 else
540 error ("template declaration of %q#D", decl);
543 /* Return true iff TYPE is a valid Java parameter or return type. */
545 static bool
546 acceptable_java_type (tree type)
548 if (type == error_mark_node)
549 return false;
551 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
552 return true;
553 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
555 type = TREE_TYPE (type);
556 if (TREE_CODE (type) == RECORD_TYPE)
558 tree args; int i;
559 if (! TYPE_FOR_JAVA (type))
560 return false;
561 if (! CLASSTYPE_TEMPLATE_INFO (type))
562 return true;
563 args = CLASSTYPE_TI_ARGS (type);
564 i = TREE_VEC_LENGTH (args);
565 while (--i >= 0)
567 type = TREE_VEC_ELT (args, i);
568 if (TYPE_PTR_P (type))
569 type = TREE_TYPE (type);
570 if (! TYPE_FOR_JAVA (type))
571 return false;
573 return true;
576 return false;
579 /* For a METHOD in a Java class CTYPE, return true if
580 the parameter and return types are valid Java types.
581 Otherwise, print appropriate error messages, and return false. */
583 bool
584 check_java_method (tree method)
586 bool jerr = false;
587 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
588 tree ret_type = TREE_TYPE (TREE_TYPE (method));
590 if (!acceptable_java_type (ret_type))
592 error ("Java method %qD has non-Java return type %qT",
593 method, ret_type);
594 jerr = true;
597 arg_types = TREE_CHAIN (arg_types);
598 if (DECL_HAS_IN_CHARGE_PARM_P (method))
599 arg_types = TREE_CHAIN (arg_types);
600 if (DECL_HAS_VTT_PARM_P (method))
601 arg_types = TREE_CHAIN (arg_types);
603 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
605 tree type = TREE_VALUE (arg_types);
606 if (!acceptable_java_type (type))
608 if (type != error_mark_node)
609 error ("Java method %qD has non-Java parameter type %qT",
610 method, type);
611 jerr = true;
614 return !jerr;
617 /* Sanity check: report error if this function FUNCTION is not
618 really a member of the class (CTYPE) it is supposed to belong to.
619 TEMPLATE_PARMS is used to specify the template parameters of a member
620 template passed as FUNCTION_DECL. If the member template is passed as a
621 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
622 from the declaration. If the function is not a function template, it
623 must be NULL.
624 It returns the original declaration for the function, NULL_TREE if
625 no declaration was found, error_mark_node if an error was emitted. */
627 tree
628 check_classfn (tree ctype, tree function, tree template_parms)
630 int ix;
631 bool is_template;
632 tree pushed_scope;
634 if (DECL_USE_TEMPLATE (function)
635 && !(TREE_CODE (function) == TEMPLATE_DECL
636 && DECL_TEMPLATE_SPECIALIZATION (function))
637 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
638 /* Since this is a specialization of a member template,
639 we're not going to find the declaration in the class.
640 For example, in:
642 struct S { template <typename T> void f(T); };
643 template <> void S::f(int);
645 we're not going to find `S::f(int)', but there's no
646 reason we should, either. We let our callers know we didn't
647 find the method, but we don't complain. */
648 return NULL_TREE;
650 /* Basic sanity check: for a template function, the template parameters
651 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
652 if (TREE_CODE (function) == TEMPLATE_DECL)
654 if (template_parms
655 && !comp_template_parms (template_parms,
656 DECL_TEMPLATE_PARMS (function)))
658 error ("template parameter lists provided don%'t match the "
659 "template parameters of %qD", function);
660 return error_mark_node;
662 template_parms = DECL_TEMPLATE_PARMS (function);
665 /* OK, is this a definition of a member template? */
666 is_template = (template_parms != NULL_TREE);
668 /* [temp.mem]
670 A destructor shall not be a member template. */
671 if (DECL_DESTRUCTOR_P (function) && is_template)
673 error ("destructor %qD declared as member template", function);
674 return error_mark_node;
677 /* We must enter the scope here, because conversion operators are
678 named by target type, and type equivalence relies on typenames
679 resolving within the scope of CTYPE. */
680 pushed_scope = push_scope (ctype);
681 ix = class_method_index_for_fn (complete_type (ctype), function);
682 if (ix >= 0)
684 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
685 tree fndecls, fndecl = 0;
686 bool is_conv_op;
687 const char *format = NULL;
689 for (fndecls = (*methods)[ix];
690 fndecls; fndecls = OVL_NEXT (fndecls))
692 tree p1, p2;
694 fndecl = OVL_CURRENT (fndecls);
695 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
696 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
698 /* We cannot simply call decls_match because this doesn't
699 work for static member functions that are pretending to
700 be methods, and because the name may have been changed by
701 asm("new_name"). */
703 /* Get rid of the this parameter on functions that become
704 static. */
705 if (DECL_STATIC_FUNCTION_P (fndecl)
706 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
707 p1 = TREE_CHAIN (p1);
709 /* A member template definition only matches a member template
710 declaration. */
711 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
712 continue;
714 /* ref-qualifier or absence of same must match. */
715 if (type_memfn_rqual (TREE_TYPE (function))
716 != type_memfn_rqual (TREE_TYPE (fndecl)))
717 continue;
719 /* While finding a match, same types and params are not enough
720 if the function is versioned. Also check version ("target")
721 attributes. */
722 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
723 TREE_TYPE (TREE_TYPE (fndecl)))
724 && compparms (p1, p2)
725 && !targetm.target_option.function_versions (function, fndecl)
726 && (!is_template
727 || comp_template_parms (template_parms,
728 DECL_TEMPLATE_PARMS (fndecl)))
729 && (DECL_TEMPLATE_SPECIALIZATION (function)
730 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
731 && (!DECL_TEMPLATE_SPECIALIZATION (function)
732 || (DECL_TI_TEMPLATE (function)
733 == DECL_TI_TEMPLATE (fndecl))))
734 break;
736 if (fndecls)
738 if (pushed_scope)
739 pop_scope (pushed_scope);
740 return OVL_CURRENT (fndecls);
743 error_at (DECL_SOURCE_LOCATION (function),
744 "prototype for %q#D does not match any in class %qT",
745 function, ctype);
746 is_conv_op = DECL_CONV_FN_P (fndecl);
748 if (is_conv_op)
749 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
750 fndecls = (*methods)[ix];
751 while (fndecls)
753 fndecl = OVL_CURRENT (fndecls);
754 fndecls = OVL_NEXT (fndecls);
756 if (!fndecls && is_conv_op)
758 if (methods->length () > (size_t) ++ix)
760 fndecls = (*methods)[ix];
761 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
763 fndecls = NULL_TREE;
764 is_conv_op = false;
767 else
768 is_conv_op = false;
770 if (format)
771 format = " %+#D";
772 else if (fndecls)
773 format = N_("candidates are: %+#D");
774 else
775 format = N_("candidate is: %+#D");
776 error (format, fndecl);
779 else if (!COMPLETE_TYPE_P (ctype))
780 cxx_incomplete_type_error (function, ctype);
781 else
782 error ("no %q#D member function declared in class %qT",
783 function, ctype);
785 if (pushed_scope)
786 pop_scope (pushed_scope);
787 return error_mark_node;
790 /* DECL is a function with vague linkage. Remember it so that at the
791 end of the translation unit we can decide whether or not to emit
792 it. */
794 void
795 note_vague_linkage_fn (tree decl)
797 DECL_DEFER_OUTPUT (decl) = 1;
798 vec_safe_push (deferred_fns, decl);
801 /* As above, but for variable template instantiations. */
803 void
804 note_variable_template_instantiation (tree decl)
806 vec_safe_push (pending_statics, decl);
809 /* We have just processed the DECL, which is a static data member.
810 The other parameters are as for cp_finish_decl. */
812 void
813 finish_static_data_member_decl (tree decl,
814 tree init, bool init_const_expr_p,
815 tree asmspec_tree,
816 int flags)
818 DECL_CONTEXT (decl) = current_class_type;
820 /* We cannot call pushdecl here, because that would fill in the
821 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
822 the right thing, namely, to put this decl out straight away. */
824 if (! processing_template_decl)
825 vec_safe_push (pending_statics, decl);
827 if (LOCAL_CLASS_P (current_class_type)
828 /* We already complained about the template definition. */
829 && !DECL_TEMPLATE_INSTANTIATION (decl))
830 permerror (input_location, "local class %q#T shall not have static data member %q#D",
831 current_class_type, decl);
832 else
833 for (tree t = current_class_type; TYPE_P (t);
834 t = CP_TYPE_CONTEXT (t))
835 if (TYPE_ANONYMOUS_P (t))
837 if (permerror (DECL_SOURCE_LOCATION (decl),
838 "static data member %qD in unnamed class", decl))
839 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
840 "unnamed class defined here");
841 break;
844 DECL_IN_AGGR_P (decl) = 1;
846 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
847 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
848 SET_VAR_HAD_UNKNOWN_BOUND (decl);
850 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
853 /* DECLARATOR and DECLSPECS correspond to a class member. The other
854 parameters are as for cp_finish_decl. Return the DECL for the
855 class member declared. */
857 tree
858 grokfield (const cp_declarator *declarator,
859 cp_decl_specifier_seq *declspecs,
860 tree init, bool init_const_expr_p,
861 tree asmspec_tree,
862 tree attrlist)
864 tree value;
865 const char *asmspec = 0;
866 int flags;
867 tree name;
869 if (init
870 && TREE_CODE (init) == TREE_LIST
871 && TREE_VALUE (init) == error_mark_node
872 && TREE_CHAIN (init) == NULL_TREE)
873 init = NULL_TREE;
875 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
876 if (! value || value == error_mark_node)
877 /* friend or constructor went bad. */
878 return error_mark_node;
879 if (TREE_TYPE (value) == error_mark_node)
880 return value;
882 if (TREE_CODE (value) == TYPE_DECL && init)
884 error ("typedef %qD is initialized (use decltype instead)", value);
885 init = NULL_TREE;
888 /* Pass friendly classes back. */
889 if (value == void_type_node)
890 return value;
893 name = DECL_NAME (value);
895 if (name != NULL_TREE)
897 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
899 error ("explicit template argument list not allowed");
900 return error_mark_node;
903 if (IDENTIFIER_POINTER (name)[0] == '_'
904 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
905 error ("member %qD conflicts with virtual function table field name",
906 value);
909 /* Stash away type declarations. */
910 if (TREE_CODE (value) == TYPE_DECL)
912 DECL_NONLOCAL (value) = 1;
913 DECL_CONTEXT (value) = current_class_type;
915 if (attrlist)
917 int attrflags = 0;
919 /* If this is a typedef that names the class for linkage purposes
920 (7.1.3p8), apply any attributes directly to the type. */
921 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
922 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
923 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
925 cplus_decl_attributes (&value, attrlist, attrflags);
928 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
929 && TREE_TYPE (value) != error_mark_node
930 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
931 set_underlying_type (value);
933 /* It's important that push_template_decl below follows
934 set_underlying_type above so that the created template
935 carries the properly set type of VALUE. */
936 if (processing_template_decl)
937 value = push_template_decl (value);
939 record_locally_defined_typedef (value);
940 return value;
943 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
945 if (!friendp && DECL_IN_AGGR_P (value))
947 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
948 return void_type_node;
951 if (asmspec_tree && asmspec_tree != error_mark_node)
952 asmspec = TREE_STRING_POINTER (asmspec_tree);
954 if (init)
956 if (TREE_CODE (value) == FUNCTION_DECL)
958 if (init == ridpointers[(int)RID_DELETE])
960 DECL_DELETED_FN (value) = 1;
961 DECL_DECLARED_INLINE_P (value) = 1;
962 DECL_INITIAL (value) = error_mark_node;
964 else if (init == ridpointers[(int)RID_DEFAULT])
966 if (defaultable_fn_check (value))
968 DECL_DEFAULTED_FN (value) = 1;
969 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
970 DECL_DECLARED_INLINE_P (value) = 1;
973 else if (TREE_CODE (init) == DEFAULT_ARG)
974 error ("invalid initializer for member function %qD", value);
975 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
977 if (integer_zerop (init))
978 DECL_PURE_VIRTUAL_P (value) = 1;
979 else if (error_operand_p (init))
980 ; /* An error has already been reported. */
981 else
982 error ("invalid initializer for member function %qD",
983 value);
985 else
987 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
988 if (friendp)
989 error ("initializer specified for friend function %qD",
990 value);
991 else
992 error ("initializer specified for static member function %qD",
993 value);
996 else if (TREE_CODE (value) == FIELD_DECL)
997 /* C++11 NSDMI, keep going. */;
998 else if (!VAR_P (value))
999 gcc_unreachable ();
1002 /* Pass friend decls back. */
1003 if ((TREE_CODE (value) == FUNCTION_DECL
1004 || TREE_CODE (value) == TEMPLATE_DECL)
1005 && DECL_CONTEXT (value) != current_class_type)
1006 return value;
1008 /* Need to set this before push_template_decl. */
1009 if (TREE_CODE (value) == VAR_DECL)
1010 DECL_CONTEXT (value) = current_class_type;
1012 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1014 value = push_template_decl (value);
1015 if (error_operand_p (value))
1016 return error_mark_node;
1019 if (attrlist)
1020 cplus_decl_attributes (&value, attrlist, 0);
1022 if (init && DIRECT_LIST_INIT_P (init))
1023 flags = LOOKUP_NORMAL;
1024 else
1025 flags = LOOKUP_IMPLICIT;
1027 switch (TREE_CODE (value))
1029 case VAR_DECL:
1030 finish_static_data_member_decl (value, init, init_const_expr_p,
1031 asmspec_tree, flags);
1032 return value;
1034 case FIELD_DECL:
1035 if (asmspec)
1036 error ("%<asm%> specifiers are not permitted on non-static data members");
1037 if (DECL_INITIAL (value) == error_mark_node)
1038 init = error_mark_node;
1039 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1040 NULL_TREE, flags);
1041 DECL_IN_AGGR_P (value) = 1;
1042 return value;
1044 case FUNCTION_DECL:
1045 if (asmspec)
1046 set_user_assembler_name (value, asmspec);
1048 cp_finish_decl (value,
1049 /*init=*/NULL_TREE,
1050 /*init_const_expr_p=*/false,
1051 asmspec_tree, flags);
1053 /* Pass friends back this way. */
1054 if (DECL_FRIEND_P (value))
1055 return void_type_node;
1057 DECL_IN_AGGR_P (value) = 1;
1058 return value;
1060 default:
1061 gcc_unreachable ();
1063 return NULL_TREE;
1066 /* Like `grokfield', but for bitfields.
1067 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1069 tree
1070 grokbitfield (const cp_declarator *declarator,
1071 cp_decl_specifier_seq *declspecs, tree width,
1072 tree attrlist)
1074 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1076 if (value == error_mark_node)
1077 return NULL_TREE; /* friends went bad. */
1078 if (TREE_TYPE (value) == error_mark_node)
1079 return value;
1081 /* Pass friendly classes back. */
1082 if (VOID_TYPE_P (value))
1083 return void_type_node;
1085 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1086 && (POINTER_TYPE_P (value)
1087 || !dependent_type_p (TREE_TYPE (value))))
1089 error ("bit-field %qD with non-integral type", value);
1090 return error_mark_node;
1093 if (TREE_CODE (value) == TYPE_DECL)
1095 error ("cannot declare %qD to be a bit-field type", value);
1096 return NULL_TREE;
1099 /* Usually, finish_struct_1 catches bitfields with invalid types.
1100 But, in the case of bitfields with function type, we confuse
1101 ourselves into thinking they are member functions, so we must
1102 check here. */
1103 if (TREE_CODE (value) == FUNCTION_DECL)
1105 error ("cannot declare bit-field %qD with function type",
1106 DECL_NAME (value));
1107 return NULL_TREE;
1110 if (DECL_IN_AGGR_P (value))
1112 error ("%qD is already defined in the class %qT", value,
1113 DECL_CONTEXT (value));
1114 return void_type_node;
1117 if (TREE_STATIC (value))
1119 error ("static member %qD cannot be a bit-field", value);
1120 return NULL_TREE;
1122 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1124 if (width != error_mark_node)
1126 /* The width must be an integer type. */
1127 if (!type_dependent_expression_p (width)
1128 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1129 error ("width of bit-field %qD has non-integral type %qT", value,
1130 TREE_TYPE (width));
1131 DECL_INITIAL (value) = width;
1132 SET_DECL_C_BIT_FIELD (value);
1135 DECL_IN_AGGR_P (value) = 1;
1137 if (attrlist)
1138 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1140 return value;
1144 /* Returns true iff ATTR is an attribute which needs to be applied at
1145 instantiation time rather than template definition time. */
1147 static bool
1148 is_late_template_attribute (tree attr, tree decl)
1150 tree name = get_attribute_name (attr);
1151 tree args = TREE_VALUE (attr);
1152 const struct attribute_spec *spec = lookup_attribute_spec (name);
1153 tree arg;
1155 if (!spec)
1156 /* Unknown attribute. */
1157 return false;
1159 /* Attribute weak handling wants to write out assembly right away. */
1160 if (is_attribute_p ("weak", name))
1161 return true;
1163 /* Attribute unused is applied directly, as it appertains to
1164 decls. */
1165 if (is_attribute_p ("unused", name))
1166 return false;
1168 /* #pragma omp declare simd attribute needs to be always deferred. */
1169 if (flag_openmp
1170 && is_attribute_p ("omp declare simd", name))
1171 return true;
1173 /* If any of the arguments are dependent expressions, we can't evaluate
1174 the attribute until instantiation time. */
1175 for (arg = args; arg; arg = TREE_CHAIN (arg))
1177 tree t = TREE_VALUE (arg);
1179 /* If the first attribute argument is an identifier, only consider
1180 second and following arguments. Attributes like mode, format,
1181 cleanup and several target specific attributes aren't late
1182 just because they have an IDENTIFIER_NODE as first argument. */
1183 if (arg == args && identifier_p (t))
1184 continue;
1186 if (value_dependent_expression_p (t)
1187 || type_dependent_expression_p (t))
1188 return true;
1191 if (TREE_CODE (decl) == TYPE_DECL
1192 || TYPE_P (decl)
1193 || spec->type_required)
1195 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1197 /* We can't apply any attributes to a completely unknown type until
1198 instantiation time. */
1199 enum tree_code code = TREE_CODE (type);
1200 if (code == TEMPLATE_TYPE_PARM
1201 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1202 || code == TYPENAME_TYPE)
1203 return true;
1204 /* Also defer most attributes on dependent types. This is not
1205 necessary in all cases, but is the better default. */
1206 else if (dependent_type_p (type)
1207 /* But some attributes specifically apply to templates. */
1208 && !is_attribute_p ("abi_tag", name)
1209 && !is_attribute_p ("deprecated", name)
1210 && !is_attribute_p ("visibility", name))
1211 return true;
1212 else
1213 return false;
1215 else
1216 return false;
1219 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1220 applied at instantiation time and return them. If IS_DEPENDENT is true,
1221 the declaration itself is dependent, so all attributes should be applied
1222 at instantiation time. */
1224 static tree
1225 splice_template_attributes (tree *attr_p, tree decl)
1227 tree *p = attr_p;
1228 tree late_attrs = NULL_TREE;
1229 tree *q = &late_attrs;
1231 if (!p)
1232 return NULL_TREE;
1234 for (; *p; )
1236 if (is_late_template_attribute (*p, decl))
1238 ATTR_IS_DEPENDENT (*p) = 1;
1239 *q = *p;
1240 *p = TREE_CHAIN (*p);
1241 q = &TREE_CHAIN (*q);
1242 *q = NULL_TREE;
1244 else
1245 p = &TREE_CHAIN (*p);
1248 return late_attrs;
1251 /* Remove any late attributes from the list in ATTR_P and attach them to
1252 DECL_P. */
1254 static void
1255 save_template_attributes (tree *attr_p, tree *decl_p)
1257 tree *q;
1259 if (attr_p && *attr_p == error_mark_node)
1260 return;
1262 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1263 if (!late_attrs)
1264 return;
1266 if (DECL_P (*decl_p))
1267 q = &DECL_ATTRIBUTES (*decl_p);
1268 else
1269 q = &TYPE_ATTRIBUTES (*decl_p);
1271 tree old_attrs = *q;
1273 /* Merge the late attributes at the beginning with the attribute
1274 list. */
1275 late_attrs = merge_attributes (late_attrs, *q);
1276 *q = late_attrs;
1278 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1280 /* We've added new attributes directly to the main variant, so
1281 now we need to update all of the other variants to include
1282 these new attributes. */
1283 tree variant;
1284 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1285 variant = TYPE_NEXT_VARIANT (variant))
1287 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1288 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1293 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1294 to a typedef which gives a previously anonymous class or enum a name for
1295 linkage purposes. */
1297 bool
1298 attributes_naming_typedef_ok (tree attrs)
1300 for (; attrs; attrs = TREE_CHAIN (attrs))
1302 tree name = get_attribute_name (attrs);
1303 if (is_attribute_p ("vector_size", name))
1304 return false;
1306 return true;
1309 /* Like reconstruct_complex_type, but handle also template trees. */
1311 tree
1312 cp_reconstruct_complex_type (tree type, tree bottom)
1314 tree inner, outer;
1315 bool late_return_type_p = false;
1317 if (TYPE_PTR_P (type))
1319 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1320 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1321 TYPE_REF_CAN_ALIAS_ALL (type));
1323 else if (TREE_CODE (type) == REFERENCE_TYPE)
1325 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1326 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1327 TYPE_REF_CAN_ALIAS_ALL (type));
1329 else if (TREE_CODE (type) == ARRAY_TYPE)
1331 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1332 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1333 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1334 element type qualification will be handled by the recursive
1335 cp_reconstruct_complex_type call and cp_build_qualified_type
1336 for ARRAY_TYPEs changes the element type. */
1337 return outer;
1339 else if (TREE_CODE (type) == FUNCTION_TYPE)
1341 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1342 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1343 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1344 outer = apply_memfn_quals (outer,
1345 type_memfn_quals (type),
1346 type_memfn_rqual (type));
1348 else if (TREE_CODE (type) == METHOD_TYPE)
1350 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1351 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1352 /* The build_method_type_directly() routine prepends 'this' to argument list,
1353 so we must compensate by getting rid of it. */
1354 outer
1355 = build_method_type_directly
1356 (class_of_this_parm (type), inner,
1357 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1359 else if (TREE_CODE (type) == OFFSET_TYPE)
1361 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1362 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1364 else
1365 return bottom;
1367 if (TYPE_ATTRIBUTES (type))
1368 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1369 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1371 if (late_return_type_p)
1372 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1374 return outer;
1377 /* Replaces any constexpr expression that may be into the attributes
1378 arguments with their reduced value. */
1380 static void
1381 cp_check_const_attributes (tree attributes)
1383 if (attributes == error_mark_node)
1384 return;
1386 tree attr;
1387 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1389 tree arg;
1390 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1392 tree expr = TREE_VALUE (arg);
1393 if (EXPR_P (expr))
1394 TREE_VALUE (arg) = maybe_constant_value (expr);
1399 /* Return true if TYPE is an OpenMP mappable type. */
1400 bool
1401 cp_omp_mappable_type (tree type)
1403 /* Mappable type has to be complete. */
1404 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1405 return false;
1406 /* Arrays have mappable type if the elements have mappable type. */
1407 while (TREE_CODE (type) == ARRAY_TYPE)
1408 type = TREE_TYPE (type);
1409 /* A mappable type cannot contain virtual members. */
1410 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1411 return false;
1412 /* All data members must be non-static. */
1413 if (CLASS_TYPE_P (type))
1415 tree field;
1416 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1417 if (TREE_CODE (field) == VAR_DECL)
1418 return false;
1419 /* All fields must have mappable types. */
1420 else if (TREE_CODE (field) == FIELD_DECL
1421 && !cp_omp_mappable_type (TREE_TYPE (field)))
1422 return false;
1424 return true;
1427 /* Like decl_attributes, but handle C++ complexity. */
1429 void
1430 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1432 if (*decl == NULL_TREE || *decl == void_type_node
1433 || *decl == error_mark_node)
1434 return;
1436 /* Add implicit "omp declare target" attribute if requested. */
1437 if (scope_chain->omp_declare_target_attribute
1438 && ((TREE_CODE (*decl) == VAR_DECL && TREE_STATIC (*decl))
1439 || TREE_CODE (*decl) == FUNCTION_DECL))
1441 if (TREE_CODE (*decl) == VAR_DECL
1442 && DECL_CLASS_SCOPE_P (*decl))
1443 error ("%q+D static data member inside of declare target directive",
1444 *decl);
1445 else if (TREE_CODE (*decl) == VAR_DECL
1446 && (DECL_FUNCTION_SCOPE_P (*decl)
1447 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1448 error ("%q+D in block scope inside of declare target directive",
1449 *decl);
1450 else if (!processing_template_decl
1451 && TREE_CODE (*decl) == VAR_DECL
1452 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1453 error ("%q+D in declare target directive does not have mappable type",
1454 *decl);
1455 else
1456 attributes = tree_cons (get_identifier ("omp declare target"),
1457 NULL_TREE, attributes);
1460 if (processing_template_decl)
1462 if (check_for_bare_parameter_packs (attributes))
1463 return;
1465 save_template_attributes (&attributes, decl);
1468 cp_check_const_attributes (attributes);
1470 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1471 decl = &DECL_TEMPLATE_RESULT (*decl);
1473 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1475 attributes
1476 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1477 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1478 attributes, flags);
1480 else
1481 decl_attributes (decl, attributes, flags);
1483 if (TREE_CODE (*decl) == TYPE_DECL)
1484 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1486 /* Propagate deprecation out to the template. */
1487 if (TREE_DEPRECATED (*decl))
1488 if (tree ti = get_template_info (*decl))
1490 tree tmpl = TI_TEMPLATE (ti);
1491 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1492 : DECL_TEMPLATE_RESULT (tmpl));
1493 if (*decl == pattern)
1494 TREE_DEPRECATED (tmpl) = true;
1498 /* Walks through the namespace- or function-scope anonymous union
1499 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1500 Returns one of the fields for use in the mangled name. */
1502 static tree
1503 build_anon_union_vars (tree type, tree object)
1505 tree main_decl = NULL_TREE;
1506 tree field;
1508 /* Rather than write the code to handle the non-union case,
1509 just give an error. */
1510 if (TREE_CODE (type) != UNION_TYPE)
1512 error ("anonymous struct not inside named type");
1513 return error_mark_node;
1516 for (field = TYPE_FIELDS (type);
1517 field != NULL_TREE;
1518 field = DECL_CHAIN (field))
1520 tree decl;
1521 tree ref;
1523 if (DECL_ARTIFICIAL (field))
1524 continue;
1525 if (TREE_CODE (field) != FIELD_DECL)
1527 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1528 "have non-static data members", field);
1529 continue;
1532 if (TREE_PRIVATE (field))
1533 permerror (input_location, "private member %q+#D in anonymous union", field);
1534 else if (TREE_PROTECTED (field))
1535 permerror (input_location, "protected member %q+#D in anonymous union", field);
1537 if (processing_template_decl)
1538 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1539 DECL_NAME (field), NULL_TREE);
1540 else
1541 ref = build_class_member_access_expr (object, field, NULL_TREE,
1542 false, tf_warning_or_error);
1544 if (DECL_NAME (field))
1546 tree base;
1548 decl = build_decl (input_location,
1549 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1550 DECL_ANON_UNION_VAR_P (decl) = 1;
1551 DECL_ARTIFICIAL (decl) = 1;
1553 base = get_base_address (object);
1554 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1555 TREE_STATIC (decl) = TREE_STATIC (base);
1556 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1558 SET_DECL_VALUE_EXPR (decl, ref);
1559 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1561 decl = pushdecl (decl);
1563 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1564 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1565 else
1566 decl = 0;
1568 if (main_decl == NULL_TREE)
1569 main_decl = decl;
1572 return main_decl;
1575 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1576 anonymous union, then all members must be laid out together. PUBLIC_P
1577 is nonzero if this union is not declared static. */
1579 void
1580 finish_anon_union (tree anon_union_decl)
1582 tree type;
1583 tree main_decl;
1584 bool public_p;
1586 if (anon_union_decl == error_mark_node)
1587 return;
1589 type = TREE_TYPE (anon_union_decl);
1590 public_p = TREE_PUBLIC (anon_union_decl);
1592 /* The VAR_DECL's context is the same as the TYPE's context. */
1593 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1595 if (TYPE_FIELDS (type) == NULL_TREE)
1596 return;
1598 if (public_p)
1600 error ("namespace-scope anonymous aggregates must be static");
1601 return;
1604 main_decl = build_anon_union_vars (type, anon_union_decl);
1605 if (main_decl == error_mark_node)
1606 return;
1607 if (main_decl == NULL_TREE)
1609 warning (0, "anonymous union with no members");
1610 return;
1613 if (!processing_template_decl)
1615 /* Use main_decl to set the mangled name. */
1616 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1617 maybe_commonize_var (anon_union_decl);
1618 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1619 mangle_decl (anon_union_decl);
1620 DECL_NAME (anon_union_decl) = NULL_TREE;
1623 pushdecl (anon_union_decl);
1624 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1627 /* Auxiliary functions to make type signatures for
1628 `operator new' and `operator delete' correspond to
1629 what compiler will be expecting. */
1631 tree
1632 coerce_new_type (tree type)
1634 int e = 0;
1635 tree args = TYPE_ARG_TYPES (type);
1637 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1639 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1641 e = 1;
1642 error ("%<operator new%> must return type %qT", ptr_type_node);
1645 if (args && args != void_list_node)
1647 if (TREE_PURPOSE (args))
1649 /* [basic.stc.dynamic.allocation]
1651 The first parameter shall not have an associated default
1652 argument. */
1653 error ("the first parameter of %<operator new%> cannot "
1654 "have a default argument");
1655 /* Throw away the default argument. */
1656 TREE_PURPOSE (args) = NULL_TREE;
1659 if (!same_type_p (TREE_VALUE (args), size_type_node))
1661 e = 2;
1662 args = TREE_CHAIN (args);
1665 else
1666 e = 2;
1668 if (e == 2)
1669 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1670 "as first parameter", size_type_node);
1672 switch (e)
1674 case 2:
1675 args = tree_cons (NULL_TREE, size_type_node, args);
1676 /* Fall through. */
1677 case 1:
1678 type = build_exception_variant
1679 (build_function_type (ptr_type_node, args),
1680 TYPE_RAISES_EXCEPTIONS (type));
1681 /* Fall through. */
1682 default:;
1684 return type;
1687 tree
1688 coerce_delete_type (tree type)
1690 int e = 0;
1691 tree args = TYPE_ARG_TYPES (type);
1693 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1695 if (!same_type_p (TREE_TYPE (type), void_type_node))
1697 e = 1;
1698 error ("%<operator delete%> must return type %qT", void_type_node);
1701 if (!args || args == void_list_node
1702 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1704 e = 2;
1705 if (args && args != void_list_node)
1706 args = TREE_CHAIN (args);
1707 error ("%<operator delete%> takes type %qT as first parameter",
1708 ptr_type_node);
1710 switch (e)
1712 case 2:
1713 args = tree_cons (NULL_TREE, ptr_type_node, args);
1714 /* Fall through. */
1715 case 1:
1716 type = build_exception_variant
1717 (build_function_type (void_type_node, args),
1718 TYPE_RAISES_EXCEPTIONS (type));
1719 /* Fall through. */
1720 default:;
1723 return type;
1726 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1727 and mark them as needed. */
1729 static void
1730 mark_vtable_entries (tree decl)
1732 tree fnaddr;
1733 unsigned HOST_WIDE_INT idx;
1735 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1736 idx, fnaddr)
1738 tree fn;
1740 STRIP_NOPS (fnaddr);
1742 if (TREE_CODE (fnaddr) != ADDR_EXPR
1743 && TREE_CODE (fnaddr) != FDESC_EXPR)
1744 /* This entry is an offset: a virtual base class offset, a
1745 virtual call offset, an RTTI offset, etc. */
1746 continue;
1748 fn = TREE_OPERAND (fnaddr, 0);
1749 TREE_ADDRESSABLE (fn) = 1;
1750 /* When we don't have vcall offsets, we output thunks whenever
1751 we output the vtables that contain them. With vcall offsets,
1752 we know all the thunks we'll need when we emit a virtual
1753 function, so we emit the thunks there instead. */
1754 if (DECL_THUNK_P (fn))
1755 use_thunk (fn, /*emit_p=*/0);
1756 mark_used (fn);
1760 /* Set DECL up to have the closest approximation of "initialized common"
1761 linkage available. */
1763 void
1764 comdat_linkage (tree decl)
1766 if (flag_weak)
1767 make_decl_one_only (decl, cxx_comdat_group (decl));
1768 else if (TREE_CODE (decl) == FUNCTION_DECL
1769 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1770 /* We can just emit function and compiler-generated variables
1771 statically; having multiple copies is (for the most part) only
1772 a waste of space.
1774 There are two correctness issues, however: the address of a
1775 template instantiation with external linkage should be the
1776 same, independent of what translation unit asks for the
1777 address, and this will not hold when we emit multiple copies of
1778 the function. However, there's little else we can do.
1780 Also, by default, the typeinfo implementation assumes that
1781 there will be only one copy of the string used as the name for
1782 each type. Therefore, if weak symbols are unavailable, the
1783 run-time library should perform a more conservative check; it
1784 should perform a string comparison, rather than an address
1785 comparison. */
1786 TREE_PUBLIC (decl) = 0;
1787 else
1789 /* Static data member template instantiations, however, cannot
1790 have multiple copies. */
1791 if (DECL_INITIAL (decl) == 0
1792 || DECL_INITIAL (decl) == error_mark_node)
1793 DECL_COMMON (decl) = 1;
1794 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1796 DECL_COMMON (decl) = 1;
1797 DECL_INITIAL (decl) = error_mark_node;
1799 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1801 /* We can't do anything useful; leave vars for explicit
1802 instantiation. */
1803 DECL_EXTERNAL (decl) = 1;
1804 DECL_NOT_REALLY_EXTERN (decl) = 0;
1808 DECL_COMDAT (decl) = 1;
1811 /* For win32 we also want to put explicit instantiations in
1812 linkonce sections, so that they will be merged with implicit
1813 instantiations; otherwise we get duplicate symbol errors.
1814 For Darwin we do not want explicit instantiations to be
1815 linkonce. */
1817 void
1818 maybe_make_one_only (tree decl)
1820 /* We used to say that this was not necessary on targets that support weak
1821 symbols, because the implicit instantiations will defer to the explicit
1822 one. However, that's not actually the case in SVR4; a strong definition
1823 after a weak one is an error. Also, not making explicit
1824 instantiations one_only means that we can end up with two copies of
1825 some template instantiations. */
1826 if (! flag_weak)
1827 return;
1829 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1830 we can get away with not emitting them if they aren't used. We need
1831 to for variables so that cp_finish_decl will update their linkage,
1832 because their DECL_INITIAL may not have been set properly yet. */
1834 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1835 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1836 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1838 make_decl_one_only (decl, cxx_comdat_group (decl));
1840 if (VAR_P (decl))
1842 varpool_node *node = varpool_node::get_create (decl);
1843 DECL_COMDAT (decl) = 1;
1844 /* Mark it needed so we don't forget to emit it. */
1845 node->forced_by_abi = true;
1846 TREE_USED (decl) = 1;
1851 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1852 This predicate will give the right answer during parsing of the
1853 function, which other tests may not. */
1855 bool
1856 vague_linkage_p (tree decl)
1858 /* Unfortunately, import_export_decl has not always been called
1859 before the function is processed, so we cannot simply check
1860 DECL_COMDAT. */
1861 if (DECL_COMDAT (decl)
1862 || (((TREE_CODE (decl) == FUNCTION_DECL
1863 && DECL_DECLARED_INLINE_P (decl))
1864 || (DECL_LANG_SPECIFIC (decl)
1865 && DECL_TEMPLATE_INSTANTIATION (decl)))
1866 && TREE_PUBLIC (decl)))
1867 return true;
1868 else if (DECL_FUNCTION_SCOPE_P (decl))
1869 /* A local static in an inline effectively has vague linkage. */
1870 return (TREE_STATIC (decl)
1871 && vague_linkage_p (DECL_CONTEXT (decl)));
1872 else
1873 return false;
1876 /* Determine whether or not we want to specifically import or export CTYPE,
1877 using various heuristics. */
1879 static void
1880 import_export_class (tree ctype)
1882 /* -1 for imported, 1 for exported. */
1883 int import_export = 0;
1885 /* It only makes sense to call this function at EOF. The reason is
1886 that this function looks at whether or not the first non-inline
1887 non-abstract virtual member function has been defined in this
1888 translation unit. But, we can't possibly know that until we've
1889 seen the entire translation unit. */
1890 gcc_assert (at_eof);
1892 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1893 return;
1895 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1896 we will have CLASSTYPE_INTERFACE_ONLY set but not
1897 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1898 heuristic because someone will supply a #pragma implementation
1899 elsewhere, and deducing it here would produce a conflict. */
1900 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1901 return;
1903 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1904 import_export = -1;
1905 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1906 import_export = 1;
1907 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1908 && !flag_implicit_templates)
1909 /* For a template class, without -fimplicit-templates, check the
1910 repository. If the virtual table is assigned to this
1911 translation unit, then export the class; otherwise, import
1912 it. */
1913 import_export = repo_export_class_p (ctype) ? 1 : -1;
1914 else if (TYPE_POLYMORPHIC_P (ctype))
1916 /* The ABI specifies that the virtual table and associated
1917 information are emitted with the key method, if any. */
1918 tree method = CLASSTYPE_KEY_METHOD (ctype);
1919 /* If weak symbol support is not available, then we must be
1920 careful not to emit the vtable when the key function is
1921 inline. An inline function can be defined in multiple
1922 translation units. If we were to emit the vtable in each
1923 translation unit containing a definition, we would get
1924 multiple definition errors at link-time. */
1925 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1926 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1929 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1930 a definition anywhere else. */
1931 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1932 import_export = 0;
1934 /* Allow back ends the chance to overrule the decision. */
1935 if (targetm.cxx.import_export_class)
1936 import_export = targetm.cxx.import_export_class (ctype, import_export);
1938 if (import_export)
1940 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1941 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1945 /* Return true if VAR has already been provided to the back end; in that
1946 case VAR should not be modified further by the front end. */
1947 static bool
1948 var_finalized_p (tree var)
1950 return varpool_node::get_create (var)->definition;
1953 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1954 must be emitted in this translation unit. Mark it as such. */
1956 void
1957 mark_needed (tree decl)
1959 TREE_USED (decl) = 1;
1960 if (TREE_CODE (decl) == FUNCTION_DECL)
1962 /* Extern inline functions don't become needed when referenced.
1963 If we know a method will be emitted in other TU and no new
1964 functions can be marked reachable, just use the external
1965 definition. */
1966 struct cgraph_node *node = cgraph_node::get_create (decl);
1967 node->forced_by_abi = true;
1969 /* #pragma interface and -frepo code can call mark_needed for
1970 maybe-in-charge 'tors; mark the clones as well. */
1971 tree clone;
1972 FOR_EACH_CLONE (clone, decl)
1973 mark_needed (clone);
1975 else if (TREE_CODE (decl) == VAR_DECL)
1977 varpool_node *node = varpool_node::get_create (decl);
1978 /* C++ frontend use mark_decl_references to force COMDAT variables
1979 to be output that might appear dead otherwise. */
1980 node->forced_by_abi = true;
1984 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1985 returns true if a definition of this entity should be provided in
1986 this object file. Callers use this function to determine whether
1987 or not to let the back end know that a definition of DECL is
1988 available in this translation unit. */
1990 bool
1991 decl_needed_p (tree decl)
1993 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1994 /* This function should only be called at the end of the translation
1995 unit. We cannot be sure of whether or not something will be
1996 COMDAT until that point. */
1997 gcc_assert (at_eof);
1999 /* All entities with external linkage that are not COMDAT should be
2000 emitted; they may be referred to from other object files. */
2001 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
2002 return true;
2003 /* If this entity was used, let the back end see it; it will decide
2004 whether or not to emit it into the object file. */
2005 if (TREE_USED (decl))
2006 return true;
2007 /* Functions marked "dllexport" must be emitted so that they are
2008 visible to other DLLs. */
2009 if (flag_keep_inline_dllexport
2010 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2011 return true;
2012 /* Virtual functions might be needed for devirtualization. */
2013 if (flag_devirtualize
2014 && TREE_CODE (decl) == FUNCTION_DECL
2015 && DECL_VIRTUAL_P (decl))
2016 return true;
2017 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2018 reference to DECL might cause it to be emitted later. */
2019 return false;
2022 /* If necessary, write out the vtables for the dynamic class CTYPE.
2023 Returns true if any vtables were emitted. */
2025 static bool
2026 maybe_emit_vtables (tree ctype)
2028 tree vtbl;
2029 tree primary_vtbl;
2030 int needed = 0;
2031 varpool_node *current = NULL, *last = NULL;
2033 /* If the vtables for this class have already been emitted there is
2034 nothing more to do. */
2035 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2036 if (var_finalized_p (primary_vtbl))
2037 return false;
2038 /* Ignore dummy vtables made by get_vtable_decl. */
2039 if (TREE_TYPE (primary_vtbl) == void_type_node)
2040 return false;
2042 /* On some targets, we cannot determine the key method until the end
2043 of the translation unit -- which is when this function is
2044 called. */
2045 if (!targetm.cxx.key_method_may_be_inline ())
2046 determine_key_method (ctype);
2048 /* See if any of the vtables are needed. */
2049 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2051 import_export_decl (vtbl);
2052 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2053 needed = 1;
2055 if (!needed)
2057 /* If the references to this class' vtables are optimized away,
2058 still emit the appropriate debugging information. See
2059 dfs_debug_mark. */
2060 if (DECL_COMDAT (primary_vtbl)
2061 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2062 note_debug_info_needed (ctype);
2063 return false;
2066 /* The ABI requires that we emit all of the vtables if we emit any
2067 of them. */
2068 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2070 /* Mark entities references from the virtual table as used. */
2071 mark_vtable_entries (vtbl);
2073 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2075 vec<tree, va_gc> *cleanups = NULL;
2076 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2077 LOOKUP_NORMAL);
2079 /* It had better be all done at compile-time. */
2080 gcc_assert (!expr && !cleanups);
2083 /* Write it out. */
2084 DECL_EXTERNAL (vtbl) = 0;
2085 rest_of_decl_compilation (vtbl, 1, 1);
2087 /* Because we're only doing syntax-checking, we'll never end up
2088 actually marking the variable as written. */
2089 if (flag_syntax_only)
2090 TREE_ASM_WRITTEN (vtbl) = 1;
2091 else if (DECL_ONE_ONLY (vtbl))
2093 current = varpool_node::get_create (vtbl);
2094 if (last)
2095 current->add_to_same_comdat_group (last);
2096 last = current;
2100 /* Since we're writing out the vtable here, also write the debug
2101 info. */
2102 note_debug_info_needed (ctype);
2104 return true;
2107 /* A special return value from type_visibility meaning internal
2108 linkage. */
2110 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2112 /* walk_tree helper function for type_visibility. */
2114 static tree
2115 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2117 int *vis_p = (int *)data;
2118 if (! TYPE_P (*tp))
2120 *walk_subtrees = 0;
2122 else if (OVERLOAD_TYPE_P (*tp)
2123 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2125 *vis_p = VISIBILITY_ANON;
2126 return *tp;
2128 else if (CLASS_TYPE_P (*tp)
2129 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2130 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2131 return NULL;
2134 /* Returns the visibility of TYPE, which is the minimum visibility of its
2135 component types. */
2137 static int
2138 type_visibility (tree type)
2140 int vis = VISIBILITY_DEFAULT;
2141 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2142 return vis;
2145 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2146 specified (or if VISIBILITY is static). If TMPL is true, this
2147 constraint is for a template argument, and takes precedence
2148 over explicitly-specified visibility on the template. */
2150 static void
2151 constrain_visibility (tree decl, int visibility, bool tmpl)
2153 if (visibility == VISIBILITY_ANON)
2155 /* extern "C" declarations aren't affected by the anonymous
2156 namespace. */
2157 if (!DECL_EXTERN_C_P (decl))
2159 TREE_PUBLIC (decl) = 0;
2160 DECL_WEAK (decl) = 0;
2161 DECL_COMMON (decl) = 0;
2162 if (TREE_CODE (decl) == FUNCTION_DECL
2163 || TREE_CODE (decl) == VAR_DECL)
2165 struct symtab_node *snode = symtab_node::get (decl);
2167 if (snode)
2168 snode->set_comdat_group (NULL);
2170 DECL_INTERFACE_KNOWN (decl) = 1;
2171 if (DECL_LANG_SPECIFIC (decl))
2172 DECL_NOT_REALLY_EXTERN (decl) = 1;
2175 else if (visibility > DECL_VISIBILITY (decl)
2176 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2178 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2179 /* This visibility was not specified. */
2180 DECL_VISIBILITY_SPECIFIED (decl) = false;
2184 /* Constrain the visibility of DECL based on the visibility of its template
2185 arguments. */
2187 static void
2188 constrain_visibility_for_template (tree decl, tree targs)
2190 /* If this is a template instantiation, check the innermost
2191 template args for visibility constraints. The outer template
2192 args are covered by the class check. */
2193 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2194 int i;
2195 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2197 int vis = 0;
2199 tree arg = TREE_VEC_ELT (args, i-1);
2200 if (TYPE_P (arg))
2201 vis = type_visibility (arg);
2202 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2204 STRIP_NOPS (arg);
2205 if (TREE_CODE (arg) == ADDR_EXPR)
2206 arg = TREE_OPERAND (arg, 0);
2207 if (VAR_OR_FUNCTION_DECL_P (arg))
2209 if (! TREE_PUBLIC (arg))
2210 vis = VISIBILITY_ANON;
2211 else
2212 vis = DECL_VISIBILITY (arg);
2215 if (vis)
2216 constrain_visibility (decl, vis, true);
2220 /* Like c_determine_visibility, but with additional C++-specific
2221 behavior.
2223 Function-scope entities can rely on the function's visibility because
2224 it is set in start_preparsed_function.
2226 Class-scope entities cannot rely on the class's visibility until the end
2227 of the enclosing class definition.
2229 Note that because namespaces have multiple independent definitions,
2230 namespace visibility is handled elsewhere using the #pragma visibility
2231 machinery rather than by decorating the namespace declaration.
2233 The goal is for constraints from the type to give a diagnostic, and
2234 other constraints to be applied silently. */
2236 void
2237 determine_visibility (tree decl)
2239 tree class_type = NULL_TREE;
2240 bool use_template;
2241 bool orig_visibility_specified;
2242 enum symbol_visibility orig_visibility;
2244 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2246 /* Only relevant for names with external linkage. */
2247 if (!TREE_PUBLIC (decl))
2248 return;
2250 /* Cloned constructors and destructors get the same visibility as
2251 the underlying function. That should be set up in
2252 maybe_clone_body. */
2253 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2255 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2256 orig_visibility = DECL_VISIBILITY (decl);
2258 if (TREE_CODE (decl) == TYPE_DECL)
2260 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2261 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2262 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2263 use_template = 1;
2264 else
2265 use_template = 0;
2267 else if (DECL_LANG_SPECIFIC (decl))
2268 use_template = DECL_USE_TEMPLATE (decl);
2269 else
2270 use_template = 0;
2272 /* If DECL is a member of a class, visibility specifiers on the
2273 class can influence the visibility of the DECL. */
2274 if (DECL_CLASS_SCOPE_P (decl))
2275 class_type = DECL_CONTEXT (decl);
2276 else
2278 /* Not a class member. */
2280 /* Virtual tables have DECL_CONTEXT set to their associated class,
2281 so they are automatically handled above. */
2282 gcc_assert (!VAR_P (decl)
2283 || !DECL_VTABLE_OR_VTT_P (decl));
2285 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2287 /* Local statics and classes get the visibility of their
2288 containing function by default, except that
2289 -fvisibility-inlines-hidden doesn't affect them. */
2290 tree fn = DECL_CONTEXT (decl);
2291 if (DECL_VISIBILITY_SPECIFIED (fn))
2293 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2294 DECL_VISIBILITY_SPECIFIED (decl) =
2295 DECL_VISIBILITY_SPECIFIED (fn);
2297 else
2299 if (DECL_CLASS_SCOPE_P (fn))
2300 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2301 else if (determine_hidden_inline (fn))
2303 DECL_VISIBILITY (decl) = default_visibility;
2304 DECL_VISIBILITY_SPECIFIED (decl) =
2305 visibility_options.inpragma;
2307 else
2309 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2310 DECL_VISIBILITY_SPECIFIED (decl) =
2311 DECL_VISIBILITY_SPECIFIED (fn);
2315 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2316 but have no TEMPLATE_INFO, so don't try to check it. */
2317 use_template = 0;
2319 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2320 && flag_visibility_ms_compat)
2322 /* Under -fvisibility-ms-compat, types are visible by default,
2323 even though their contents aren't. */
2324 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2325 int underlying_vis = type_visibility (underlying_type);
2326 if (underlying_vis == VISIBILITY_ANON
2327 || (CLASS_TYPE_P (underlying_type)
2328 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2329 constrain_visibility (decl, underlying_vis, false);
2330 else
2331 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2333 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2335 /* tinfo visibility is based on the type it's for. */
2336 constrain_visibility
2337 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2339 /* Give the target a chance to override the visibility associated
2340 with DECL. */
2341 if (TREE_PUBLIC (decl)
2342 && !DECL_REALLY_EXTERN (decl)
2343 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2344 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2345 targetm.cxx.determine_class_data_visibility (decl);
2347 else if (use_template)
2348 /* Template instantiations and specializations get visibility based
2349 on their template unless they override it with an attribute. */;
2350 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2352 if (determine_hidden_inline (decl))
2353 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2354 else
2356 /* Set default visibility to whatever the user supplied with
2357 #pragma GCC visibility or a namespace visibility attribute. */
2358 DECL_VISIBILITY (decl) = default_visibility;
2359 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2364 if (use_template)
2366 /* If the specialization doesn't specify visibility, use the
2367 visibility from the template. */
2368 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2369 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2370 : DECL_TEMPLATE_INFO (decl));
2371 tree args = TI_ARGS (tinfo);
2372 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2373 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2374 : DECL_ATTRIBUTES (decl));
2376 if (args != error_mark_node)
2378 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2380 if (!DECL_VISIBILITY_SPECIFIED (decl))
2382 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2383 && determine_hidden_inline (decl))
2384 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2385 else
2387 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2388 DECL_VISIBILITY_SPECIFIED (decl)
2389 = DECL_VISIBILITY_SPECIFIED (pattern);
2393 if (args
2394 /* Template argument visibility outweighs #pragma or namespace
2395 visibility, but not an explicit attribute. */
2396 && !lookup_attribute ("visibility", attribs))
2398 int depth = TMPL_ARGS_DEPTH (args);
2399 if (DECL_VISIBILITY_SPECIFIED (decl))
2401 /* A class template member with explicit visibility
2402 overrides the class visibility, so we need to apply
2403 all the levels of template args directly. */
2404 int i;
2405 for (i = 1; i <= depth; ++i)
2407 tree lev = TMPL_ARGS_LEVEL (args, i);
2408 constrain_visibility_for_template (decl, lev);
2411 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2412 /* Limit visibility based on its template arguments. */
2413 constrain_visibility_for_template (decl, args);
2418 if (class_type)
2419 determine_visibility_from_class (decl, class_type);
2421 if (decl_anon_ns_mem_p (decl))
2422 /* Names in an anonymous namespace get internal linkage.
2423 This might change once we implement export. */
2424 constrain_visibility (decl, VISIBILITY_ANON, false);
2425 else if (TREE_CODE (decl) != TYPE_DECL)
2427 /* Propagate anonymity from type to decl. */
2428 int tvis = type_visibility (TREE_TYPE (decl));
2429 if (tvis == VISIBILITY_ANON
2430 || ! DECL_VISIBILITY_SPECIFIED (decl))
2431 constrain_visibility (decl, tvis, false);
2433 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2434 /* DR 757: A type without linkage shall not be used as the type of a
2435 variable or function with linkage, unless
2436 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2437 o the variable or function is not used (3.2 [basic.def.odr]) or is
2438 defined in the same translation unit.
2440 Since non-extern "C" decls need to be defined in the same
2441 translation unit, we can make the type internal. */
2442 constrain_visibility (decl, VISIBILITY_ANON, false);
2444 /* If visibility changed and DECL already has DECL_RTL, ensure
2445 symbol flags are updated. */
2446 if ((DECL_VISIBILITY (decl) != orig_visibility
2447 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2448 && ((VAR_P (decl) && TREE_STATIC (decl))
2449 || TREE_CODE (decl) == FUNCTION_DECL)
2450 && DECL_RTL_SET_P (decl))
2451 make_decl_rtl (decl);
2454 /* By default, static data members and function members receive
2455 the visibility of their containing class. */
2457 static void
2458 determine_visibility_from_class (tree decl, tree class_type)
2460 if (DECL_VISIBILITY_SPECIFIED (decl))
2461 return;
2463 if (determine_hidden_inline (decl))
2464 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2465 else
2467 /* Default to the class visibility. */
2468 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2469 DECL_VISIBILITY_SPECIFIED (decl)
2470 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2473 /* Give the target a chance to override the visibility associated
2474 with DECL. */
2475 if (VAR_P (decl)
2476 && (DECL_TINFO_P (decl)
2477 || (DECL_VTABLE_OR_VTT_P (decl)
2478 /* Construction virtual tables are not exported because
2479 they cannot be referred to from other object files;
2480 their name is not standardized by the ABI. */
2481 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2482 && TREE_PUBLIC (decl)
2483 && !DECL_REALLY_EXTERN (decl)
2484 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2485 targetm.cxx.determine_class_data_visibility (decl);
2488 /* Returns true iff DECL is an inline that should get hidden visibility
2489 because of -fvisibility-inlines-hidden. */
2491 static bool
2492 determine_hidden_inline (tree decl)
2494 return (visibility_options.inlines_hidden
2495 /* Don't do this for inline templates; specializations might not be
2496 inline, and we don't want them to inherit the hidden
2497 visibility. We'll set it here for all inline instantiations. */
2498 && !processing_template_decl
2499 && TREE_CODE (decl) == FUNCTION_DECL
2500 && DECL_DECLARED_INLINE_P (decl)
2501 && (! DECL_LANG_SPECIFIC (decl)
2502 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2505 /* Constrain the visibility of a class TYPE based on the visibility of its
2506 field types. Warn if any fields require lesser visibility. */
2508 void
2509 constrain_class_visibility (tree type)
2511 tree binfo;
2512 tree t;
2513 int i;
2515 int vis = type_visibility (type);
2517 if (vis == VISIBILITY_ANON
2518 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2519 return;
2521 /* Don't warn about visibility if the class has explicit visibility. */
2522 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2523 vis = VISIBILITY_INTERNAL;
2525 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2526 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2528 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2529 int subvis = type_visibility (ftype);
2531 if (subvis == VISIBILITY_ANON)
2533 if (!in_main_input_context ())
2534 warning (0, "\
2535 %qT has a field %qD whose type uses the anonymous namespace",
2536 type, t);
2538 else if (MAYBE_CLASS_TYPE_P (ftype)
2539 && vis < VISIBILITY_HIDDEN
2540 && subvis >= VISIBILITY_HIDDEN)
2541 warning (OPT_Wattributes, "\
2542 %qT declared with greater visibility than the type of its field %qD",
2543 type, t);
2546 binfo = TYPE_BINFO (type);
2547 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2549 int subvis = type_visibility (TREE_TYPE (t));
2551 if (subvis == VISIBILITY_ANON)
2553 if (!in_main_input_context())
2554 warning (0, "\
2555 %qT has a base %qT whose type uses the anonymous namespace",
2556 type, TREE_TYPE (t));
2558 else if (vis < VISIBILITY_HIDDEN
2559 && subvis >= VISIBILITY_HIDDEN)
2560 warning (OPT_Wattributes, "\
2561 %qT declared with greater visibility than its base %qT",
2562 type, TREE_TYPE (t));
2566 /* Functions for adjusting the visibility of a tagged type and its nested
2567 types and declarations when it gets a name for linkage purposes from a
2568 typedef. */
2570 static void bt_reset_linkage_1 (binding_entry, void *);
2571 static void bt_reset_linkage_2 (binding_entry, void *);
2573 /* First reset the visibility of all the types. */
2575 static void
2576 reset_type_linkage_1 (tree type)
2578 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2579 if (CLASS_TYPE_P (type))
2580 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2581 bt_reset_linkage_1, NULL);
2583 static void
2584 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2586 reset_type_linkage_1 (b->type);
2589 /* Then reset the visibility of any static data members or member
2590 functions that use those types. */
2592 static void
2593 reset_decl_linkage (tree decl)
2595 if (TREE_PUBLIC (decl))
2596 return;
2597 if (DECL_CLONED_FUNCTION_P (decl))
2598 return;
2599 TREE_PUBLIC (decl) = true;
2600 DECL_INTERFACE_KNOWN (decl) = false;
2601 determine_visibility (decl);
2602 tentative_decl_linkage (decl);
2604 static void
2605 reset_type_linkage_2 (tree type)
2607 if (CLASS_TYPE_P (type))
2609 if (tree vt = CLASSTYPE_VTABLES (type))
2611 tree name = mangle_vtbl_for_type (type);
2612 DECL_NAME (vt) = name;
2613 SET_DECL_ASSEMBLER_NAME (vt, name);
2614 reset_decl_linkage (vt);
2616 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2618 tree name = mangle_typeinfo_for_type (type);
2619 DECL_NAME (ti) = name;
2620 SET_DECL_ASSEMBLER_NAME (ti, name);
2621 TREE_TYPE (name) = type;
2622 reset_decl_linkage (ti);
2624 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2625 if (TREE_CODE (m) == VAR_DECL)
2626 reset_decl_linkage (m);
2627 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2628 reset_decl_linkage (m);
2629 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2630 bt_reset_linkage_2, NULL);
2633 static void
2634 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2636 reset_type_linkage_2 (b->type);
2638 void
2639 reset_type_linkage (tree type)
2641 reset_type_linkage_1 (type);
2642 reset_type_linkage_2 (type);
2645 /* Set up our initial idea of what the linkage of DECL should be. */
2647 void
2648 tentative_decl_linkage (tree decl)
2650 if (DECL_INTERFACE_KNOWN (decl))
2651 /* We've already made a decision as to how this function will
2652 be handled. */;
2653 else if (vague_linkage_p (decl))
2655 if (TREE_CODE (decl) == FUNCTION_DECL
2656 && decl_defined_p (decl))
2658 DECL_EXTERNAL (decl) = 1;
2659 DECL_NOT_REALLY_EXTERN (decl) = 1;
2660 note_vague_linkage_fn (decl);
2661 /* A non-template inline function with external linkage will
2662 always be COMDAT. As we must eventually determine the
2663 linkage of all functions, and as that causes writes to
2664 the data mapped in from the PCH file, it's advantageous
2665 to mark the functions at this point. */
2666 if (DECL_DECLARED_INLINE_P (decl)
2667 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2668 || DECL_DEFAULTED_FN (decl)))
2670 /* This function must have external linkage, as
2671 otherwise DECL_INTERFACE_KNOWN would have been
2672 set. */
2673 gcc_assert (TREE_PUBLIC (decl));
2674 comdat_linkage (decl);
2675 DECL_INTERFACE_KNOWN (decl) = 1;
2678 else if (TREE_CODE (decl) == VAR_DECL)
2679 maybe_commonize_var (decl);
2683 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2684 for DECL has not already been determined, do so now by setting
2685 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2686 function is called entities with vague linkage whose definitions
2687 are available must have TREE_PUBLIC set.
2689 If this function decides to place DECL in COMDAT, it will set
2690 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2691 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2692 callers defer that decision until it is clear that DECL is actually
2693 required. */
2695 void
2696 import_export_decl (tree decl)
2698 int emit_p;
2699 bool comdat_p;
2700 bool import_p;
2701 tree class_type = NULL_TREE;
2703 if (DECL_INTERFACE_KNOWN (decl))
2704 return;
2706 /* We cannot determine what linkage to give to an entity with vague
2707 linkage until the end of the file. For example, a virtual table
2708 for a class will be defined if and only if the key method is
2709 defined in this translation unit. As a further example, consider
2710 that when compiling a translation unit that uses PCH file with
2711 "-frepo" it would be incorrect to make decisions about what
2712 entities to emit when building the PCH; those decisions must be
2713 delayed until the repository information has been processed. */
2714 gcc_assert (at_eof);
2715 /* Object file linkage for explicit instantiations is handled in
2716 mark_decl_instantiated. For static variables in functions with
2717 vague linkage, maybe_commonize_var is used.
2719 Therefore, the only declarations that should be provided to this
2720 function are those with external linkage that are:
2722 * implicit instantiations of function templates
2724 * inline function
2726 * implicit instantiations of static data members of class
2727 templates
2729 * virtual tables
2731 * typeinfo objects
2733 Furthermore, all entities that reach this point must have a
2734 definition available in this translation unit.
2736 The following assertions check these conditions. */
2737 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2738 /* Any code that creates entities with TREE_PUBLIC cleared should
2739 also set DECL_INTERFACE_KNOWN. */
2740 gcc_assert (TREE_PUBLIC (decl));
2741 if (TREE_CODE (decl) == FUNCTION_DECL)
2742 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2743 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2744 || DECL_DECLARED_INLINE_P (decl));
2745 else
2746 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2747 || DECL_VTABLE_OR_VTT_P (decl)
2748 || DECL_TINFO_P (decl));
2749 /* Check that a definition of DECL is available in this translation
2750 unit. */
2751 gcc_assert (!DECL_REALLY_EXTERN (decl));
2753 /* Assume that DECL will not have COMDAT linkage. */
2754 comdat_p = false;
2755 /* Assume that DECL will not be imported into this translation
2756 unit. */
2757 import_p = false;
2759 /* See if the repository tells us whether or not to emit DECL in
2760 this translation unit. */
2761 emit_p = repo_emit_p (decl);
2762 if (emit_p == 0)
2763 import_p = true;
2764 else if (emit_p == 1)
2766 /* The repository indicates that this entity should be defined
2767 here. Make sure the back end honors that request. */
2768 mark_needed (decl);
2769 /* Output the definition as an ordinary strong definition. */
2770 DECL_EXTERNAL (decl) = 0;
2771 DECL_INTERFACE_KNOWN (decl) = 1;
2772 return;
2775 if (import_p)
2776 /* We have already decided what to do with this DECL; there is no
2777 need to check anything further. */
2779 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2781 class_type = DECL_CONTEXT (decl);
2782 import_export_class (class_type);
2783 if (TYPE_FOR_JAVA (class_type))
2784 import_p = true;
2785 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2786 && CLASSTYPE_INTERFACE_ONLY (class_type))
2787 import_p = true;
2788 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2789 && !CLASSTYPE_USE_TEMPLATE (class_type)
2790 && CLASSTYPE_KEY_METHOD (class_type)
2791 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2792 /* The ABI requires that all virtual tables be emitted with
2793 COMDAT linkage. However, on systems where COMDAT symbols
2794 don't show up in the table of contents for a static
2795 archive, or on systems without weak symbols (where we
2796 approximate COMDAT linkage by using internal linkage), the
2797 linker will report errors about undefined symbols because
2798 it will not see the virtual table definition. Therefore,
2799 in the case that we know that the virtual table will be
2800 emitted in only one translation unit, we make the virtual
2801 table an ordinary definition with external linkage. */
2802 DECL_EXTERNAL (decl) = 0;
2803 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2805 /* CLASS_TYPE is being exported from this translation unit,
2806 so DECL should be defined here. */
2807 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2808 /* If a class is declared in a header with the "extern
2809 template" extension, then it will not be instantiated,
2810 even in translation units that would normally require
2811 it. Often such classes are explicitly instantiated in
2812 one translation unit. Therefore, the explicit
2813 instantiation must be made visible to other translation
2814 units. */
2815 DECL_EXTERNAL (decl) = 0;
2816 else
2818 /* The generic C++ ABI says that class data is always
2819 COMDAT, even if there is a key function. Some
2820 variants (e.g., the ARM EABI) says that class data
2821 only has COMDAT linkage if the class data might be
2822 emitted in more than one translation unit. When the
2823 key method can be inline and is inline, we still have
2824 to arrange for comdat even though
2825 class_data_always_comdat is false. */
2826 if (!CLASSTYPE_KEY_METHOD (class_type)
2827 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2828 || targetm.cxx.class_data_always_comdat ())
2830 /* The ABI requires COMDAT linkage. Normally, we
2831 only emit COMDAT things when they are needed;
2832 make sure that we realize that this entity is
2833 indeed needed. */
2834 comdat_p = true;
2835 mark_needed (decl);
2839 else if (!flag_implicit_templates
2840 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2841 import_p = true;
2842 else
2843 comdat_p = true;
2845 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2847 tree type = TREE_TYPE (DECL_NAME (decl));
2848 if (CLASS_TYPE_P (type))
2850 class_type = type;
2851 import_export_class (type);
2852 if (CLASSTYPE_INTERFACE_KNOWN (type)
2853 && TYPE_POLYMORPHIC_P (type)
2854 && CLASSTYPE_INTERFACE_ONLY (type)
2855 /* If -fno-rtti was specified, then we cannot be sure
2856 that RTTI information will be emitted with the
2857 virtual table of the class, so we must emit it
2858 wherever it is used. */
2859 && flag_rtti)
2860 import_p = true;
2861 else
2863 if (CLASSTYPE_INTERFACE_KNOWN (type)
2864 && !CLASSTYPE_INTERFACE_ONLY (type))
2866 comdat_p = (targetm.cxx.class_data_always_comdat ()
2867 || (CLASSTYPE_KEY_METHOD (type)
2868 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2869 mark_needed (decl);
2870 if (!flag_weak)
2872 comdat_p = false;
2873 DECL_EXTERNAL (decl) = 0;
2876 else
2877 comdat_p = true;
2880 else
2881 comdat_p = true;
2883 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2885 /* DECL is an implicit instantiation of a function or static
2886 data member. */
2887 if ((flag_implicit_templates
2888 && !flag_use_repository)
2889 || (flag_implicit_inline_templates
2890 && TREE_CODE (decl) == FUNCTION_DECL
2891 && DECL_DECLARED_INLINE_P (decl)))
2892 comdat_p = true;
2893 else
2894 /* If we are not implicitly generating templates, then mark
2895 this entity as undefined in this translation unit. */
2896 import_p = true;
2898 else if (DECL_FUNCTION_MEMBER_P (decl))
2900 if (!DECL_DECLARED_INLINE_P (decl))
2902 tree ctype = DECL_CONTEXT (decl);
2903 import_export_class (ctype);
2904 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2906 DECL_NOT_REALLY_EXTERN (decl)
2907 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2908 || (DECL_DECLARED_INLINE_P (decl)
2909 && ! flag_implement_inlines
2910 && !DECL_VINDEX (decl)));
2912 if (!DECL_NOT_REALLY_EXTERN (decl))
2913 DECL_EXTERNAL (decl) = 1;
2915 /* Always make artificials weak. */
2916 if (DECL_ARTIFICIAL (decl) && flag_weak)
2917 comdat_p = true;
2918 else
2919 maybe_make_one_only (decl);
2922 else
2923 comdat_p = true;
2925 else
2926 comdat_p = true;
2928 if (import_p)
2930 /* If we are importing DECL into this translation unit, mark is
2931 an undefined here. */
2932 DECL_EXTERNAL (decl) = 1;
2933 DECL_NOT_REALLY_EXTERN (decl) = 0;
2935 else if (comdat_p)
2937 /* If we decided to put DECL in COMDAT, mark it accordingly at
2938 this point. */
2939 comdat_linkage (decl);
2942 DECL_INTERFACE_KNOWN (decl) = 1;
2945 /* Return an expression that performs the destruction of DECL, which
2946 must be a VAR_DECL whose type has a non-trivial destructor, or is
2947 an array whose (innermost) elements have a non-trivial destructor. */
2949 tree
2950 build_cleanup (tree decl)
2952 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2953 gcc_assert (clean != NULL_TREE);
2954 return clean;
2957 /* Returns the initialization guard variable for the variable DECL,
2958 which has static storage duration. */
2960 tree
2961 get_guard (tree decl)
2963 tree sname;
2964 tree guard;
2966 sname = mangle_guard_variable (decl);
2967 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2968 if (! guard)
2970 tree guard_type;
2972 /* We use a type that is big enough to contain a mutex as well
2973 as an integer counter. */
2974 guard_type = targetm.cxx.guard_type ();
2975 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2976 VAR_DECL, sname, guard_type);
2978 /* The guard should have the same linkage as what it guards. */
2979 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2980 TREE_STATIC (guard) = TREE_STATIC (decl);
2981 DECL_COMMON (guard) = DECL_COMMON (decl);
2982 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2983 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2984 if (DECL_ONE_ONLY (decl))
2985 make_decl_one_only (guard, cxx_comdat_group (guard));
2986 if (TREE_PUBLIC (decl))
2987 DECL_WEAK (guard) = DECL_WEAK (decl);
2988 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2989 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2991 DECL_ARTIFICIAL (guard) = 1;
2992 DECL_IGNORED_P (guard) = 1;
2993 TREE_USED (guard) = 1;
2994 pushdecl_top_level_and_finish (guard, NULL_TREE);
2996 return guard;
2999 /* Return those bits of the GUARD variable that should be set when the
3000 guarded entity is actually initialized. */
3002 static tree
3003 get_guard_bits (tree guard)
3005 if (!targetm.cxx.guard_mask_bit ())
3007 /* We only set the first byte of the guard, in order to leave room
3008 for a mutex in the high-order bits. */
3009 guard = build1 (ADDR_EXPR,
3010 build_pointer_type (TREE_TYPE (guard)),
3011 guard);
3012 guard = build1 (NOP_EXPR,
3013 build_pointer_type (char_type_node),
3014 guard);
3015 guard = build1 (INDIRECT_REF, char_type_node, guard);
3018 return guard;
3021 /* Return an expression which determines whether or not the GUARD
3022 variable has already been initialized. */
3024 tree
3025 get_guard_cond (tree guard)
3027 tree guard_value;
3029 /* Check to see if the GUARD is zero. */
3030 guard = get_guard_bits (guard);
3032 /* Mask off all but the low bit. */
3033 if (targetm.cxx.guard_mask_bit ())
3035 guard_value = integer_one_node;
3036 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3037 guard_value = convert (TREE_TYPE (guard), guard_value);
3038 guard = cp_build_binary_op (input_location,
3039 BIT_AND_EXPR, guard, guard_value,
3040 tf_warning_or_error);
3043 guard_value = integer_zero_node;
3044 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3045 guard_value = convert (TREE_TYPE (guard), guard_value);
3046 return cp_build_binary_op (input_location,
3047 EQ_EXPR, guard, guard_value,
3048 tf_warning_or_error);
3051 /* Return an expression which sets the GUARD variable, indicating that
3052 the variable being guarded has been initialized. */
3054 tree
3055 set_guard (tree guard)
3057 tree guard_init;
3059 /* Set the GUARD to one. */
3060 guard = get_guard_bits (guard);
3061 guard_init = integer_one_node;
3062 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3063 guard_init = convert (TREE_TYPE (guard), guard_init);
3064 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3065 tf_warning_or_error);
3068 /* Returns true iff we can tell that VAR does not have a dynamic
3069 initializer. */
3071 static bool
3072 var_defined_without_dynamic_init (tree var)
3074 /* If it's defined in another TU, we can't tell. */
3075 if (DECL_EXTERNAL (var))
3076 return false;
3077 /* If it has a non-trivial destructor, registering the destructor
3078 counts as dynamic initialization. */
3079 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3080 return false;
3081 /* If it's in this TU, its initializer has been processed. */
3082 gcc_assert (DECL_INITIALIZED_P (var));
3083 /* If it has no initializer or a constant one, it's not dynamic. */
3084 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3085 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3088 /* Returns true iff VAR is a variable that needs uses to be
3089 wrapped for possible dynamic initialization. */
3091 static bool
3092 var_needs_tls_wrapper (tree var)
3094 return (!error_operand_p (var)
3095 && DECL_THREAD_LOCAL_P (var)
3096 && !DECL_GNU_TLS_P (var)
3097 && !DECL_FUNCTION_SCOPE_P (var)
3098 && !var_defined_without_dynamic_init (var));
3101 /* Get the FUNCTION_DECL for the shared TLS init function for this
3102 translation unit. */
3104 static tree
3105 get_local_tls_init_fn (void)
3107 tree sname = get_identifier ("__tls_init");
3108 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3109 if (!fn)
3111 fn = build_lang_decl (FUNCTION_DECL, sname,
3112 build_function_type (void_type_node,
3113 void_list_node));
3114 SET_DECL_LANGUAGE (fn, lang_c);
3115 TREE_PUBLIC (fn) = false;
3116 DECL_ARTIFICIAL (fn) = true;
3117 mark_used (fn);
3118 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3120 return fn;
3123 /* Get a FUNCTION_DECL for the init function for the thread_local
3124 variable VAR. The init function will be an alias to the function
3125 that initializes all the non-local TLS variables in the translation
3126 unit. The init function is only used by the wrapper function. */
3128 static tree
3129 get_tls_init_fn (tree var)
3131 /* Only C++11 TLS vars need this init fn. */
3132 if (!var_needs_tls_wrapper (var))
3133 return NULL_TREE;
3135 /* If -fno-extern-tls-init, assume that we don't need to call
3136 a tls init function for a variable defined in another TU. */
3137 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3138 return NULL_TREE;
3140 #ifdef ASM_OUTPUT_DEF
3141 /* If the variable is internal, or if we can't generate aliases,
3142 call the local init function directly. */
3143 if (!TREE_PUBLIC (var))
3144 #endif
3145 return get_local_tls_init_fn ();
3147 tree sname = mangle_tls_init_fn (var);
3148 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3149 if (!fn)
3151 fn = build_lang_decl (FUNCTION_DECL, sname,
3152 build_function_type (void_type_node,
3153 void_list_node));
3154 SET_DECL_LANGUAGE (fn, lang_c);
3155 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3156 DECL_ARTIFICIAL (fn) = true;
3157 DECL_COMDAT (fn) = DECL_COMDAT (var);
3158 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3159 if (DECL_ONE_ONLY (var))
3160 make_decl_one_only (fn, cxx_comdat_group (fn));
3161 if (TREE_PUBLIC (var))
3163 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3164 /* If the variable is defined somewhere else and might have static
3165 initialization, make the init function a weak reference. */
3166 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3167 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3168 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3169 && DECL_EXTERNAL (var))
3170 declare_weak (fn);
3171 else
3172 DECL_WEAK (fn) = DECL_WEAK (var);
3174 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3175 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3176 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3177 DECL_IGNORED_P (fn) = 1;
3178 mark_used (fn);
3180 DECL_BEFRIENDING_CLASSES (fn) = var;
3182 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3184 return fn;
3187 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3188 variable VAR. The wrapper function calls the init function (if any) for
3189 VAR and then returns a reference to VAR. The wrapper function is used
3190 in place of VAR everywhere VAR is mentioned. */
3192 tree
3193 get_tls_wrapper_fn (tree var)
3195 /* Only C++11 TLS vars need this wrapper fn. */
3196 if (!var_needs_tls_wrapper (var))
3197 return NULL_TREE;
3199 tree sname = mangle_tls_wrapper_fn (var);
3200 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3201 if (!fn)
3203 /* A named rvalue reference is an lvalue, so the wrapper should
3204 always return an lvalue reference. */
3205 tree type = non_reference (TREE_TYPE (var));
3206 type = build_reference_type (type);
3207 tree fntype = build_function_type (type, void_list_node);
3208 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3209 SET_DECL_LANGUAGE (fn, lang_c);
3210 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3211 DECL_ARTIFICIAL (fn) = true;
3212 DECL_IGNORED_P (fn) = 1;
3213 /* The wrapper is inline and emitted everywhere var is used. */
3214 DECL_DECLARED_INLINE_P (fn) = true;
3215 if (TREE_PUBLIC (var))
3217 comdat_linkage (fn);
3218 #ifdef HAVE_GAS_HIDDEN
3219 /* Make the wrapper bind locally; there's no reason to share
3220 the wrapper between multiple shared objects. */
3221 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3222 DECL_VISIBILITY_SPECIFIED (fn) = true;
3223 #endif
3225 if (!TREE_PUBLIC (fn))
3226 DECL_INTERFACE_KNOWN (fn) = true;
3227 mark_used (fn);
3228 note_vague_linkage_fn (fn);
3230 #if 0
3231 /* We want CSE to commonize calls to the wrapper, but marking it as
3232 pure is unsafe since it has side-effects. I guess we need a new
3233 ECF flag even weaker than ECF_PURE. FIXME! */
3234 DECL_PURE_P (fn) = true;
3235 #endif
3237 DECL_BEFRIENDING_CLASSES (fn) = var;
3239 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3241 return fn;
3244 /* At EOF, generate the definition for the TLS wrapper function FN:
3246 T& var_wrapper() {
3247 if (init_fn) init_fn();
3248 return var;
3249 } */
3251 static void
3252 generate_tls_wrapper (tree fn)
3254 tree var = DECL_BEFRIENDING_CLASSES (fn);
3256 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3257 tree body = begin_function_body ();
3258 /* Only call the init fn if there might be one. */
3259 if (tree init_fn = get_tls_init_fn (var))
3261 tree if_stmt = NULL_TREE;
3262 /* If init_fn is a weakref, make sure it exists before calling. */
3263 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3265 if_stmt = begin_if_stmt ();
3266 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3267 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3268 NE_EXPR, addr, nullptr_node,
3269 tf_warning_or_error);
3270 finish_if_stmt_cond (cond, if_stmt);
3272 finish_expr_stmt (build_cxx_call
3273 (init_fn, 0, NULL, tf_warning_or_error));
3274 if (if_stmt)
3276 finish_then_clause (if_stmt);
3277 finish_if_stmt (if_stmt);
3280 else
3281 /* If there's no initialization, the wrapper is a constant function. */
3282 TREE_READONLY (fn) = true;
3283 finish_return_stmt (convert_from_reference (var));
3284 finish_function_body (body);
3285 expand_or_defer_fn (finish_function (0));
3288 /* Start the process of running a particular set of global constructors
3289 or destructors. Subroutine of do_[cd]tors. Also called from
3290 vtv_start_verification_constructor_init_function. */
3292 static tree
3293 start_objects (int method_type, int initp)
3295 tree body;
3296 tree fndecl;
3297 char type[14];
3299 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3301 if (initp != DEFAULT_INIT_PRIORITY)
3303 char joiner;
3305 #ifdef JOINER
3306 joiner = JOINER;
3307 #else
3308 joiner = '_';
3309 #endif
3311 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3313 else
3314 sprintf (type, "sub_%c", method_type);
3316 fndecl = build_lang_decl (FUNCTION_DECL,
3317 get_file_function_name (type),
3318 build_function_type_list (void_type_node,
3319 NULL_TREE));
3320 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3322 TREE_PUBLIC (current_function_decl) = 0;
3324 /* Mark as artificial because it's not explicitly in the user's
3325 source code. */
3326 DECL_ARTIFICIAL (current_function_decl) = 1;
3328 /* Mark this declaration as used to avoid spurious warnings. */
3329 TREE_USED (current_function_decl) = 1;
3331 /* Mark this function as a global constructor or destructor. */
3332 if (method_type == 'I')
3333 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3334 else
3335 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3337 body = begin_compound_stmt (BCS_FN_BODY);
3339 return body;
3342 /* Finish the process of running a particular set of global constructors
3343 or destructors. Subroutine of do_[cd]tors. */
3345 static void
3346 finish_objects (int method_type, int initp, tree body)
3348 tree fn;
3350 /* Finish up. */
3351 finish_compound_stmt (body);
3352 fn = finish_function (0);
3354 if (method_type == 'I')
3356 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3357 decl_init_priority_insert (fn, initp);
3359 else
3361 DECL_STATIC_DESTRUCTOR (fn) = 1;
3362 decl_fini_priority_insert (fn, initp);
3365 expand_or_defer_fn (fn);
3368 /* The names of the parameters to the function created to handle
3369 initializations and destructions for objects with static storage
3370 duration. */
3371 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3372 #define PRIORITY_IDENTIFIER "__priority"
3374 /* The name of the function we create to handle initializations and
3375 destructions for objects with static storage duration. */
3376 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3378 /* The declaration for the __INITIALIZE_P argument. */
3379 static GTY(()) tree initialize_p_decl;
3381 /* The declaration for the __PRIORITY argument. */
3382 static GTY(()) tree priority_decl;
3384 /* The declaration for the static storage duration function. */
3385 static GTY(()) tree ssdf_decl;
3387 /* All the static storage duration functions created in this
3388 translation unit. */
3389 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3391 /* A map from priority levels to information about that priority
3392 level. There may be many such levels, so efficient lookup is
3393 important. */
3394 static splay_tree priority_info_map;
3396 /* Begins the generation of the function that will handle all
3397 initialization and destruction of objects with static storage
3398 duration. The function generated takes two parameters of type
3399 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3400 nonzero, it performs initializations. Otherwise, it performs
3401 destructions. It only performs those initializations or
3402 destructions with the indicated __PRIORITY. The generated function
3403 returns no value.
3405 It is assumed that this function will only be called once per
3406 translation unit. */
3408 static tree
3409 start_static_storage_duration_function (unsigned count)
3411 tree type;
3412 tree body;
3413 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3415 /* Create the identifier for this function. It will be of the form
3416 SSDF_IDENTIFIER_<number>. */
3417 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3419 type = build_function_type_list (void_type_node,
3420 integer_type_node, integer_type_node,
3421 NULL_TREE);
3423 /* Create the FUNCTION_DECL itself. */
3424 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3425 get_identifier (id),
3426 type);
3427 TREE_PUBLIC (ssdf_decl) = 0;
3428 DECL_ARTIFICIAL (ssdf_decl) = 1;
3430 /* Put this function in the list of functions to be called from the
3431 static constructors and destructors. */
3432 if (!ssdf_decls)
3434 vec_alloc (ssdf_decls, 32);
3436 /* Take this opportunity to initialize the map from priority
3437 numbers to information about that priority level. */
3438 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3439 /*delete_key_fn=*/0,
3440 /*delete_value_fn=*/
3441 (splay_tree_delete_value_fn) &free);
3443 /* We always need to generate functions for the
3444 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3445 priorities later, we'll be sure to find the
3446 DEFAULT_INIT_PRIORITY. */
3447 get_priority_info (DEFAULT_INIT_PRIORITY);
3450 vec_safe_push (ssdf_decls, ssdf_decl);
3452 /* Create the argument list. */
3453 initialize_p_decl = cp_build_parm_decl
3454 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3455 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3456 TREE_USED (initialize_p_decl) = 1;
3457 priority_decl = cp_build_parm_decl
3458 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3459 DECL_CONTEXT (priority_decl) = ssdf_decl;
3460 TREE_USED (priority_decl) = 1;
3462 DECL_CHAIN (initialize_p_decl) = priority_decl;
3463 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3465 /* Put the function in the global scope. */
3466 pushdecl (ssdf_decl);
3468 /* Start the function itself. This is equivalent to declaring the
3469 function as:
3471 static void __ssdf (int __initialize_p, init __priority_p);
3473 It is static because we only need to call this function from the
3474 various constructor and destructor functions for this module. */
3475 start_preparsed_function (ssdf_decl,
3476 /*attrs=*/NULL_TREE,
3477 SF_PRE_PARSED);
3479 /* Set up the scope of the outermost block in the function. */
3480 body = begin_compound_stmt (BCS_FN_BODY);
3482 return body;
3485 /* Finish the generation of the function which performs initialization
3486 and destruction of objects with static storage duration. After
3487 this point, no more such objects can be created. */
3489 static void
3490 finish_static_storage_duration_function (tree body)
3492 /* Close out the function. */
3493 finish_compound_stmt (body);
3494 expand_or_defer_fn (finish_function (0));
3497 /* Return the information about the indicated PRIORITY level. If no
3498 code to handle this level has yet been generated, generate the
3499 appropriate prologue. */
3501 static priority_info
3502 get_priority_info (int priority)
3504 priority_info pi;
3505 splay_tree_node n;
3507 n = splay_tree_lookup (priority_info_map,
3508 (splay_tree_key) priority);
3509 if (!n)
3511 /* Create a new priority information structure, and insert it
3512 into the map. */
3513 pi = XNEW (struct priority_info_s);
3514 pi->initializations_p = 0;
3515 pi->destructions_p = 0;
3516 splay_tree_insert (priority_info_map,
3517 (splay_tree_key) priority,
3518 (splay_tree_value) pi);
3520 else
3521 pi = (priority_info) n->value;
3523 return pi;
3526 /* The effective initialization priority of a DECL. */
3528 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3529 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3530 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3532 /* Whether a DECL needs a guard to protect it against multiple
3533 initialization. */
3535 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3536 || DECL_ONE_ONLY (decl) \
3537 || DECL_WEAK (decl)))
3539 /* Called from one_static_initialization_or_destruction(),
3540 via walk_tree.
3541 Walks the initializer list of a global variable and looks for
3542 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3543 and that have their DECL_CONTEXT() == NULL.
3544 For each such temporary variable, set their DECL_CONTEXT() to
3545 the current function. This is necessary because otherwise
3546 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3547 when trying to refer to a temporary variable that does not have
3548 it's DECL_CONTECT() properly set. */
3549 static tree
3550 fix_temporary_vars_context_r (tree *node,
3551 int * /*unused*/,
3552 void * /*unused1*/)
3554 gcc_assert (current_function_decl);
3556 if (TREE_CODE (*node) == BIND_EXPR)
3558 tree var;
3560 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3561 if (VAR_P (var)
3562 && !DECL_NAME (var)
3563 && DECL_ARTIFICIAL (var)
3564 && !DECL_CONTEXT (var))
3565 DECL_CONTEXT (var) = current_function_decl;
3568 return NULL_TREE;
3571 /* Set up to handle the initialization or destruction of DECL. If
3572 INITP is nonzero, we are initializing the variable. Otherwise, we
3573 are destroying it. */
3575 static void
3576 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3578 tree guard_if_stmt = NULL_TREE;
3579 tree guard;
3581 /* If we are supposed to destruct and there's a trivial destructor,
3582 nothing has to be done. */
3583 if (!initp
3584 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3585 return;
3587 /* Trick the compiler into thinking we are at the file and line
3588 where DECL was declared so that error-messages make sense, and so
3589 that the debugger will show somewhat sensible file and line
3590 information. */
3591 input_location = DECL_SOURCE_LOCATION (decl);
3593 /* Make sure temporary variables in the initialiser all have
3594 their DECL_CONTEXT() set to a value different from NULL_TREE.
3595 This can happen when global variables initialisers are built.
3596 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3597 the temporary variables that might have been generated in the
3598 accompagning initialisers is NULL_TREE, meaning the variables have been
3599 declared in the global namespace.
3600 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3601 of the temporaries are set to the current function decl. */
3602 cp_walk_tree_without_duplicates (&init,
3603 fix_temporary_vars_context_r,
3604 NULL);
3606 /* Because of:
3608 [class.access.spec]
3610 Access control for implicit calls to the constructors,
3611 the conversion functions, or the destructor called to
3612 create and destroy a static data member is performed as
3613 if these calls appeared in the scope of the member's
3614 class.
3616 we pretend we are in a static member function of the class of
3617 which the DECL is a member. */
3618 if (member_p (decl))
3620 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3621 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3624 /* Assume we don't need a guard. */
3625 guard = NULL_TREE;
3626 /* We need a guard if this is an object with external linkage that
3627 might be initialized in more than one place. (For example, a
3628 static data member of a template, when the data member requires
3629 construction.) */
3630 if (NEEDS_GUARD_P (decl))
3632 tree guard_cond;
3634 guard = get_guard (decl);
3636 /* When using __cxa_atexit, we just check the GUARD as we would
3637 for a local static. */
3638 if (flag_use_cxa_atexit)
3640 /* When using __cxa_atexit, we never try to destroy
3641 anything from a static destructor. */
3642 gcc_assert (initp);
3643 guard_cond = get_guard_cond (guard);
3645 /* If we don't have __cxa_atexit, then we will be running
3646 destructors from .fini sections, or their equivalents. So,
3647 we need to know how many times we've tried to initialize this
3648 object. We do initializations only if the GUARD is zero,
3649 i.e., if we are the first to initialize the variable. We do
3650 destructions only if the GUARD is one, i.e., if we are the
3651 last to destroy the variable. */
3652 else if (initp)
3653 guard_cond
3654 = cp_build_binary_op (input_location,
3655 EQ_EXPR,
3656 cp_build_unary_op (PREINCREMENT_EXPR,
3657 guard,
3658 /*noconvert=*/1,
3659 tf_warning_or_error),
3660 integer_one_node,
3661 tf_warning_or_error);
3662 else
3663 guard_cond
3664 = cp_build_binary_op (input_location,
3665 EQ_EXPR,
3666 cp_build_unary_op (PREDECREMENT_EXPR,
3667 guard,
3668 /*noconvert=*/1,
3669 tf_warning_or_error),
3670 integer_zero_node,
3671 tf_warning_or_error);
3673 guard_if_stmt = begin_if_stmt ();
3674 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3678 /* If we're using __cxa_atexit, we have not already set the GUARD,
3679 so we must do so now. */
3680 if (guard && initp && flag_use_cxa_atexit)
3681 finish_expr_stmt (set_guard (guard));
3683 /* Perform the initialization or destruction. */
3684 if (initp)
3686 if (init)
3688 finish_expr_stmt (init);
3689 if (flag_sanitize & SANITIZE_ADDRESS)
3691 varpool_node *vnode = varpool_node::get (decl);
3692 if (vnode)
3693 vnode->dynamically_initialized = 1;
3697 /* If we're using __cxa_atexit, register a function that calls the
3698 destructor for the object. */
3699 if (flag_use_cxa_atexit)
3700 finish_expr_stmt (register_dtor_fn (decl));
3702 else
3703 finish_expr_stmt (build_cleanup (decl));
3705 /* Finish the guard if-stmt, if necessary. */
3706 if (guard)
3708 finish_then_clause (guard_if_stmt);
3709 finish_if_stmt (guard_if_stmt);
3712 /* Now that we're done with DECL we don't need to pretend to be a
3713 member of its class any longer. */
3714 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3715 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3718 /* Generate code to do the initialization or destruction of the decls in VARS,
3719 a TREE_LIST of VAR_DECL with static storage duration.
3720 Whether initialization or destruction is performed is specified by INITP. */
3722 static void
3723 do_static_initialization_or_destruction (tree vars, bool initp)
3725 tree node, init_if_stmt, cond;
3727 /* Build the outer if-stmt to check for initialization or destruction. */
3728 init_if_stmt = begin_if_stmt ();
3729 cond = initp ? integer_one_node : integer_zero_node;
3730 cond = cp_build_binary_op (input_location,
3731 EQ_EXPR,
3732 initialize_p_decl,
3733 cond,
3734 tf_warning_or_error);
3735 finish_if_stmt_cond (cond, init_if_stmt);
3737 /* To make sure dynamic construction doesn't access globals from other
3738 compilation units where they might not be yet constructed, for
3739 -fsanitize=address insert __asan_before_dynamic_init call that
3740 prevents access to either all global variables that need construction
3741 in other compilation units, or at least those that haven't been
3742 initialized yet. Variables that need dynamic construction in
3743 the current compilation unit are kept accessible. */
3744 if (flag_sanitize & SANITIZE_ADDRESS)
3745 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3747 node = vars;
3748 do {
3749 tree decl = TREE_VALUE (node);
3750 tree priority_if_stmt;
3751 int priority;
3752 priority_info pi;
3754 /* If we don't need a destructor, there's nothing to do. Avoid
3755 creating a possibly empty if-stmt. */
3756 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3758 node = TREE_CHAIN (node);
3759 continue;
3762 /* Remember that we had an initialization or finalization at this
3763 priority. */
3764 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3765 pi = get_priority_info (priority);
3766 if (initp)
3767 pi->initializations_p = 1;
3768 else
3769 pi->destructions_p = 1;
3771 /* Conditionalize this initialization on being in the right priority
3772 and being initializing/finalizing appropriately. */
3773 priority_if_stmt = begin_if_stmt ();
3774 cond = cp_build_binary_op (input_location,
3775 EQ_EXPR,
3776 priority_decl,
3777 build_int_cst (NULL_TREE, priority),
3778 tf_warning_or_error);
3779 finish_if_stmt_cond (cond, priority_if_stmt);
3781 /* Process initializers with same priority. */
3782 for (; node
3783 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3784 node = TREE_CHAIN (node))
3785 /* Do one initialization or destruction. */
3786 one_static_initialization_or_destruction (TREE_VALUE (node),
3787 TREE_PURPOSE (node), initp);
3789 /* Finish up the priority if-stmt body. */
3790 finish_then_clause (priority_if_stmt);
3791 finish_if_stmt (priority_if_stmt);
3793 } while (node);
3795 /* Revert what __asan_before_dynamic_init did by calling
3796 __asan_after_dynamic_init. */
3797 if (flag_sanitize & SANITIZE_ADDRESS)
3798 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3800 /* Finish up the init/destruct if-stmt body. */
3801 finish_then_clause (init_if_stmt);
3802 finish_if_stmt (init_if_stmt);
3805 /* VARS is a list of variables with static storage duration which may
3806 need initialization and/or finalization. Remove those variables
3807 that don't really need to be initialized or finalized, and return
3808 the resulting list. The order in which the variables appear in
3809 VARS is in reverse order of the order in which they should actually
3810 be initialized. The list we return is in the unreversed order;
3811 i.e., the first variable should be initialized first. */
3813 static tree
3814 prune_vars_needing_no_initialization (tree *vars)
3816 tree *var = vars;
3817 tree result = NULL_TREE;
3819 while (*var)
3821 tree t = *var;
3822 tree decl = TREE_VALUE (t);
3823 tree init = TREE_PURPOSE (t);
3825 /* Deal gracefully with error. */
3826 if (decl == error_mark_node)
3828 var = &TREE_CHAIN (t);
3829 continue;
3832 /* The only things that can be initialized are variables. */
3833 gcc_assert (VAR_P (decl));
3835 /* If this object is not defined, we don't need to do anything
3836 here. */
3837 if (DECL_EXTERNAL (decl))
3839 var = &TREE_CHAIN (t);
3840 continue;
3843 /* Also, if the initializer already contains errors, we can bail
3844 out now. */
3845 if (init && TREE_CODE (init) == TREE_LIST
3846 && value_member (error_mark_node, init))
3848 var = &TREE_CHAIN (t);
3849 continue;
3852 /* This variable is going to need initialization and/or
3853 finalization, so we add it to the list. */
3854 *var = TREE_CHAIN (t);
3855 TREE_CHAIN (t) = result;
3856 result = t;
3859 return result;
3862 /* Make sure we have told the back end about all the variables in
3863 VARS. */
3865 static void
3866 write_out_vars (tree vars)
3868 tree v;
3870 for (v = vars; v; v = TREE_CHAIN (v))
3872 tree var = TREE_VALUE (v);
3873 if (!var_finalized_p (var))
3875 import_export_decl (var);
3876 rest_of_decl_compilation (var, 1, 1);
3881 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3882 (otherwise) that will initialize all global objects with static
3883 storage duration having the indicated PRIORITY. */
3885 static void
3886 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3887 location_t *locus)
3889 char function_key;
3890 tree fndecl;
3891 tree body;
3892 size_t i;
3894 input_location = *locus;
3895 /* ??? */
3896 /* Was: locus->line++; */
3898 /* We use `I' to indicate initialization and `D' to indicate
3899 destruction. */
3900 function_key = constructor_p ? 'I' : 'D';
3902 /* We emit the function lazily, to avoid generating empty
3903 global constructors and destructors. */
3904 body = NULL_TREE;
3906 /* For Objective-C++, we may need to initialize metadata found in this module.
3907 This must be done _before_ any other static initializations. */
3908 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3909 && constructor_p && objc_static_init_needed_p ())
3911 body = start_objects (function_key, priority);
3912 objc_generate_static_init_call (NULL_TREE);
3915 /* Call the static storage duration function with appropriate
3916 arguments. */
3917 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3919 /* Calls to pure or const functions will expand to nothing. */
3920 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3922 tree call;
3924 if (! body)
3925 body = start_objects (function_key, priority);
3927 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3928 build_int_cst (NULL_TREE,
3929 constructor_p),
3930 build_int_cst (NULL_TREE,
3931 priority),
3932 NULL_TREE);
3933 finish_expr_stmt (call);
3937 /* Close out the function. */
3938 if (body)
3939 finish_objects (function_key, priority, body);
3942 /* Generate constructor and destructor functions for the priority
3943 indicated by N. */
3945 static int
3946 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3948 location_t *locus = (location_t *) data;
3949 int priority = (int) n->key;
3950 priority_info pi = (priority_info) n->value;
3952 /* Generate the functions themselves, but only if they are really
3953 needed. */
3954 if (pi->initializations_p)
3955 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3956 if (pi->destructions_p)
3957 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3959 /* Keep iterating. */
3960 return 0;
3963 /* Java requires that we be able to reference a local address for a
3964 method, and not be confused by PLT entries. If hidden aliases are
3965 supported, collect and return all the functions for which we should
3966 emit a hidden alias. */
3968 static hash_set<tree> *
3969 collect_candidates_for_java_method_aliases (void)
3971 struct cgraph_node *node;
3972 hash_set<tree> *candidates = NULL;
3974 #ifndef HAVE_GAS_HIDDEN
3975 return candidates;
3976 #endif
3978 FOR_EACH_FUNCTION (node)
3980 tree fndecl = node->decl;
3982 if (DECL_CLASS_SCOPE_P (fndecl)
3983 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3984 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3986 if (candidates == NULL)
3987 candidates = new hash_set<tree>;
3988 candidates->add (fndecl);
3992 return candidates;
3996 /* Java requires that we be able to reference a local address for a
3997 method, and not be confused by PLT entries. If hidden aliases are
3998 supported, emit one for each java function that we've emitted.
3999 CANDIDATES is the set of FUNCTION_DECLs that were gathered
4000 by collect_candidates_for_java_method_aliases. */
4002 static void
4003 build_java_method_aliases (hash_set<tree> *candidates)
4005 struct cgraph_node *node;
4007 #ifndef HAVE_GAS_HIDDEN
4008 return;
4009 #endif
4011 FOR_EACH_FUNCTION (node)
4013 tree fndecl = node->decl;
4015 if (TREE_ASM_WRITTEN (fndecl)
4016 && candidates->contains (fndecl))
4018 /* Mangle the name in a predictable way; we need to reference
4019 this from a java compiled object file. */
4020 tree oid, nid, alias;
4021 const char *oname;
4022 char *nname;
4024 oid = DECL_ASSEMBLER_NAME (fndecl);
4025 oname = IDENTIFIER_POINTER (oid);
4026 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4027 nname = ACONCAT (("_ZGA", oname+2, NULL));
4028 nid = get_identifier (nname);
4030 alias = make_alias_for (fndecl, nid);
4031 TREE_PUBLIC (alias) = 1;
4032 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4034 assemble_alias (alias, oid);
4039 /* Return C++ property of T, based on given operation OP. */
4041 static int
4042 cpp_check (tree t, cpp_operation op)
4044 switch (op)
4046 case IS_ABSTRACT:
4047 return DECL_PURE_VIRTUAL_P (t);
4048 case IS_CONSTRUCTOR:
4049 return DECL_CONSTRUCTOR_P (t);
4050 case IS_DESTRUCTOR:
4051 return DECL_DESTRUCTOR_P (t);
4052 case IS_COPY_CONSTRUCTOR:
4053 return DECL_COPY_CONSTRUCTOR_P (t);
4054 case IS_TEMPLATE:
4055 return TREE_CODE (t) == TEMPLATE_DECL;
4056 case IS_TRIVIAL:
4057 return trivial_type_p (t);
4058 default:
4059 return 0;
4063 /* Collect source file references recursively, starting from NAMESPC. */
4065 static void
4066 collect_source_refs (tree namespc)
4068 tree t;
4070 if (!namespc)
4071 return;
4073 /* Iterate over names in this name space. */
4074 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4075 if (!DECL_IS_BUILTIN (t) )
4076 collect_source_ref (DECL_SOURCE_FILE (t));
4078 /* Dump siblings, if any */
4079 collect_source_refs (TREE_CHAIN (namespc));
4081 /* Dump children, if any */
4082 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4085 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4086 starting from NAMESPC. */
4088 static void
4089 collect_ada_namespace (tree namespc, const char *source_file)
4091 if (!namespc)
4092 return;
4094 /* Collect decls from this namespace */
4095 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4097 /* Collect siblings, if any */
4098 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4100 /* Collect children, if any */
4101 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4104 /* Returns true iff there is a definition available for variable or
4105 function DECL. */
4107 static bool
4108 decl_defined_p (tree decl)
4110 if (TREE_CODE (decl) == FUNCTION_DECL)
4111 return (DECL_INITIAL (decl) != NULL_TREE);
4112 else
4114 gcc_assert (VAR_P (decl));
4115 return !DECL_EXTERNAL (decl);
4119 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4121 [expr.const]
4123 An integral constant-expression can only involve ... const
4124 variables of integral or enumeration types initialized with
4125 constant expressions ...
4127 C++0x also allows constexpr variables and temporaries initialized
4128 with constant expressions. We handle the former here, but the latter
4129 are just folded away in cxx_eval_constant_expression.
4131 The standard does not require that the expression be non-volatile.
4132 G++ implements the proposed correction in DR 457. */
4134 bool
4135 decl_constant_var_p (tree decl)
4137 if (!decl_maybe_constant_var_p (decl))
4138 return false;
4140 /* We don't know if a template static data member is initialized with
4141 a constant expression until we instantiate its initializer. Even
4142 in the case of a constexpr variable, we can't treat it as a
4143 constant until its initializer is complete in case it's used in
4144 its own initializer. */
4145 mark_used (decl);
4146 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4149 /* Returns true if DECL could be a symbolic constant variable, depending on
4150 its initializer. */
4152 bool
4153 decl_maybe_constant_var_p (tree decl)
4155 tree type = TREE_TYPE (decl);
4156 if (!VAR_P (decl))
4157 return false;
4158 if (DECL_DECLARED_CONSTEXPR_P (decl))
4159 return true;
4160 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4161 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4164 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4165 called from grokfndecl and grokvardecl; in all modes it is called from
4166 cp_write_global_declarations. */
4168 void
4169 no_linkage_error (tree decl)
4171 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4172 /* In C++11 it's ok if the decl is defined. */
4173 return;
4174 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4175 if (t == NULL_TREE)
4176 /* The type that got us on no_linkage_decls must have gotten a name for
4177 linkage purposes. */;
4178 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4179 /* The type might end up having a typedef name for linkage purposes. */
4180 vec_safe_push (no_linkage_decls, decl);
4181 else if (TYPE_ANONYMOUS_P (t))
4183 bool d = false;
4184 if (cxx_dialect >= cxx11)
4185 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4186 "anonymous type, is used but never defined", decl);
4187 else if (DECL_EXTERN_C_P (decl))
4188 /* Allow this; it's pretty common in C. */;
4189 else if (TREE_CODE (decl) == VAR_DECL)
4190 /* DRs 132, 319 and 389 seem to indicate types with
4191 no linkage can only be used to declare extern "C"
4192 entities. Since it's not always an error in the
4193 ISO C++ 90 Standard, we only issue a warning. */
4194 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4195 "with no linkage used to declare variable %q#D with "
4196 "linkage", decl);
4197 else
4198 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4199 "linkage used to declare function %q#D with linkage",
4200 decl);
4201 if (d && is_typedef_decl (TYPE_NAME (t)))
4202 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4203 "to the unqualified type, so it is not used for linkage",
4204 TYPE_NAME (t));
4206 else if (cxx_dialect >= cxx11)
4207 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4208 "%qT, is used but never defined", decl, t);
4209 else if (TREE_CODE (decl) == VAR_DECL)
4210 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4211 "used to declare variable %q#D with linkage", t, decl);
4212 else
4213 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4214 "to declare function %q#D with linkage", t, decl);
4217 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4219 static void
4220 collect_all_refs (const char *source_file)
4222 collect_ada_namespace (global_namespace, source_file);
4225 /* Clear DECL_EXTERNAL for NODE. */
4227 static bool
4228 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4230 DECL_EXTERNAL (node->decl) = 0;
4231 return false;
4234 /* Build up the function to run dynamic initializers for thread_local
4235 variables in this translation unit and alias the init functions for the
4236 individual variables to it. */
4238 static void
4239 handle_tls_init (void)
4241 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4242 if (vars == NULL_TREE)
4243 return;
4245 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4247 write_out_vars (vars);
4249 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4250 boolean_type_node);
4251 TREE_PUBLIC (guard) = false;
4252 TREE_STATIC (guard) = true;
4253 DECL_ARTIFICIAL (guard) = true;
4254 DECL_IGNORED_P (guard) = true;
4255 TREE_USED (guard) = true;
4256 set_decl_tls_model (guard, decl_default_tls_model (guard));
4257 pushdecl_top_level_and_finish (guard, NULL_TREE);
4259 tree fn = get_local_tls_init_fn ();
4260 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4261 tree body = begin_function_body ();
4262 tree if_stmt = begin_if_stmt ();
4263 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4264 tf_warning_or_error);
4265 finish_if_stmt_cond (cond, if_stmt);
4266 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4267 tf_warning_or_error));
4268 for (; vars; vars = TREE_CHAIN (vars))
4270 tree var = TREE_VALUE (vars);
4271 tree init = TREE_PURPOSE (vars);
4272 one_static_initialization_or_destruction (var, init, true);
4274 #ifdef ASM_OUTPUT_DEF
4275 /* Output init aliases even with -fno-extern-tls-init. */
4276 if (TREE_PUBLIC (var))
4278 tree single_init_fn = get_tls_init_fn (var);
4279 if (single_init_fn == NULL_TREE)
4280 continue;
4281 cgraph_node *alias
4282 = cgraph_node::get_create (fn)->create_same_body_alias
4283 (single_init_fn, fn);
4284 gcc_assert (alias != NULL);
4286 #endif
4289 finish_then_clause (if_stmt);
4290 finish_if_stmt (if_stmt);
4291 finish_function_body (body);
4292 expand_or_defer_fn (finish_function (0));
4295 /* The entire file is now complete. If requested, dump everything
4296 to a file. */
4298 static void
4299 dump_tu (void)
4301 int flags;
4302 FILE *stream = dump_begin (TDI_tu, &flags);
4304 if (stream)
4306 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4307 dump_end (TDI_tu, stream);
4311 /* Check the deallocation functions for CODE to see if we want to warn that
4312 only one was defined. */
4314 static void
4315 maybe_warn_sized_delete (enum tree_code code)
4317 tree sized = NULL_TREE;
4318 tree unsized = NULL_TREE;
4320 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4321 ovl; ovl = OVL_NEXT (ovl))
4323 tree fn = OVL_CURRENT (ovl);
4324 /* We're only interested in usual deallocation functions. */
4325 if (!non_placement_deallocation_fn_p (fn))
4326 continue;
4327 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4328 unsized = fn;
4329 else
4330 sized = fn;
4332 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4333 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4334 "the program should also define %qD", sized);
4335 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4336 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4337 "the program should also define %qD", unsized);
4340 /* Check the global deallocation functions to see if we want to warn about
4341 defining unsized without sized (or vice versa). */
4343 static void
4344 maybe_warn_sized_delete ()
4346 if (!flag_sized_deallocation || !warn_sized_deallocation)
4347 return;
4348 maybe_warn_sized_delete (DELETE_EXPR);
4349 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4352 /* This routine is called at the end of compilation.
4353 Its job is to create all the code needed to initialize and
4354 destroy the global aggregates. We do the destruction
4355 first, since that way we only need to reverse the decls once. */
4357 void
4358 cp_write_global_declarations (void)
4360 tree vars;
4361 bool reconsider;
4362 size_t i;
4363 location_t locus;
4364 unsigned ssdf_count = 0;
4365 int retries = 0;
4366 tree decl;
4367 hash_set<tree> *candidates;
4369 locus = input_location;
4370 at_eof = 1;
4372 /* Bad parse errors. Just forget about it. */
4373 if (! global_bindings_p () || current_class_type
4374 || !vec_safe_is_empty (decl_namespace_list))
4375 return;
4377 /* This is the point to write out a PCH if we're doing that.
4378 In that case we do not want to do anything else. */
4379 if (pch_file)
4381 c_common_write_pch ();
4382 dump_tu ();
4383 return;
4386 symtab->process_same_body_aliases ();
4388 /* Handle -fdump-ada-spec[-slim] */
4389 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4391 if (flag_dump_ada_spec_slim)
4392 collect_source_ref (main_input_filename);
4393 else
4394 collect_source_refs (global_namespace);
4396 dump_ada_specs (collect_all_refs, cpp_check);
4399 /* FIXME - huh? was input_line -= 1;*/
4401 timevar_start (TV_PHASE_DEFERRED);
4403 /* We now have to write out all the stuff we put off writing out.
4404 These include:
4406 o Template specializations that we have not yet instantiated,
4407 but which are needed.
4408 o Initialization and destruction for non-local objects with
4409 static storage duration. (Local objects with static storage
4410 duration are initialized when their scope is first entered,
4411 and are cleaned up via atexit.)
4412 o Virtual function tables.
4414 All of these may cause others to be needed. For example,
4415 instantiating one function may cause another to be needed, and
4416 generating the initializer for an object may cause templates to be
4417 instantiated, etc., etc. */
4419 emit_support_tinfos ();
4423 tree t;
4424 tree decl;
4426 reconsider = false;
4428 /* If there are templates that we've put off instantiating, do
4429 them now. */
4430 instantiate_pending_templates (retries);
4431 ggc_collect ();
4433 /* Write out virtual tables as required. Note that writing out
4434 the virtual table for a template class may cause the
4435 instantiation of members of that class. If we write out
4436 vtables then we remove the class from our list so we don't
4437 have to look at it again. */
4439 while (keyed_classes != NULL_TREE
4440 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4442 reconsider = true;
4443 keyed_classes = TREE_CHAIN (keyed_classes);
4446 t = keyed_classes;
4447 if (t != NULL_TREE)
4449 tree next = TREE_CHAIN (t);
4451 while (next)
4453 if (maybe_emit_vtables (TREE_VALUE (next)))
4455 reconsider = true;
4456 TREE_CHAIN (t) = TREE_CHAIN (next);
4458 else
4459 t = next;
4461 next = TREE_CHAIN (t);
4465 /* Write out needed type info variables. We have to be careful
4466 looping through unemitted decls, because emit_tinfo_decl may
4467 cause other variables to be needed. New elements will be
4468 appended, and we remove from the vector those that actually
4469 get emitted. */
4470 for (i = unemitted_tinfo_decls->length ();
4471 unemitted_tinfo_decls->iterate (--i, &t);)
4472 if (emit_tinfo_decl (t))
4474 reconsider = true;
4475 unemitted_tinfo_decls->unordered_remove (i);
4478 /* The list of objects with static storage duration is built up
4479 in reverse order. We clear STATIC_AGGREGATES so that any new
4480 aggregates added during the initialization of these will be
4481 initialized in the correct order when we next come around the
4482 loop. */
4483 vars = prune_vars_needing_no_initialization (&static_aggregates);
4485 if (vars)
4487 /* We need to start a new initialization function each time
4488 through the loop. That's because we need to know which
4489 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4490 isn't computed until a function is finished, and written
4491 out. That's a deficiency in the back end. When this is
4492 fixed, these initialization functions could all become
4493 inline, with resulting performance improvements. */
4494 tree ssdf_body;
4496 /* Set the line and file, so that it is obviously not from
4497 the source file. */
4498 input_location = locus;
4499 ssdf_body = start_static_storage_duration_function (ssdf_count);
4501 /* Make sure the back end knows about all the variables. */
4502 write_out_vars (vars);
4504 /* First generate code to do all the initializations. */
4505 if (vars)
4506 do_static_initialization_or_destruction (vars, /*initp=*/true);
4508 /* Then, generate code to do all the destructions. Do these
4509 in reverse order so that the most recently constructed
4510 variable is the first destroyed. If we're using
4511 __cxa_atexit, then we don't need to do this; functions
4512 were registered at initialization time to destroy the
4513 local statics. */
4514 if (!flag_use_cxa_atexit && vars)
4516 vars = nreverse (vars);
4517 do_static_initialization_or_destruction (vars, /*initp=*/false);
4519 else
4520 vars = NULL_TREE;
4522 /* Finish up the static storage duration function for this
4523 round. */
4524 input_location = locus;
4525 finish_static_storage_duration_function (ssdf_body);
4527 /* All those initializations and finalizations might cause
4528 us to need more inline functions, more template
4529 instantiations, etc. */
4530 reconsider = true;
4531 ssdf_count++;
4532 /* ??? was: locus.line++; */
4535 /* Now do the same for thread_local variables. */
4536 handle_tls_init ();
4538 /* Go through the set of inline functions whose bodies have not
4539 been emitted yet. If out-of-line copies of these functions
4540 are required, emit them. */
4541 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4543 /* Does it need synthesizing? */
4544 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4545 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4547 /* Even though we're already at the top-level, we push
4548 there again. That way, when we pop back a few lines
4549 hence, all of our state is restored. Otherwise,
4550 finish_function doesn't clean things up, and we end
4551 up with CURRENT_FUNCTION_DECL set. */
4552 push_to_top_level ();
4553 /* The decl's location will mark where it was first
4554 needed. Save that so synthesize method can indicate
4555 where it was needed from, in case of error */
4556 input_location = DECL_SOURCE_LOCATION (decl);
4557 synthesize_method (decl);
4558 pop_from_top_level ();
4559 reconsider = true;
4562 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4563 generate_tls_wrapper (decl);
4565 if (!DECL_SAVED_TREE (decl))
4566 continue;
4568 /* We lie to the back end, pretending that some functions
4569 are not defined when they really are. This keeps these
4570 functions from being put out unnecessarily. But, we must
4571 stop lying when the functions are referenced, or if they
4572 are not comdat since they need to be put out now. If
4573 DECL_INTERFACE_KNOWN, then we have already set
4574 DECL_EXTERNAL appropriately, so there's no need to check
4575 again, and we do not want to clear DECL_EXTERNAL if a
4576 previous call to import_export_decl set it.
4578 This is done in a separate for cycle, because if some
4579 deferred function is contained in another deferred
4580 function later in deferred_fns varray,
4581 rest_of_compilation would skip this function and we
4582 really cannot expand the same function twice. */
4583 import_export_decl (decl);
4584 if (DECL_NOT_REALLY_EXTERN (decl)
4585 && DECL_INITIAL (decl)
4586 && decl_needed_p (decl))
4588 struct cgraph_node *node, *next;
4590 node = cgraph_node::get (decl);
4591 if (node->cpp_implicit_alias)
4592 node = node->get_alias_target ();
4594 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4595 NULL, true);
4596 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4597 group, we need to mark all symbols in the same comdat group
4598 that way. */
4599 if (node->same_comdat_group)
4600 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4601 next != node;
4602 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4603 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4604 NULL, true);
4607 /* If we're going to need to write this function out, and
4608 there's already a body for it, create RTL for it now.
4609 (There might be no body if this is a method we haven't
4610 gotten around to synthesizing yet.) */
4611 if (!DECL_EXTERNAL (decl)
4612 && decl_needed_p (decl)
4613 && !TREE_ASM_WRITTEN (decl)
4614 && !cgraph_node::get (decl)->definition)
4616 /* We will output the function; no longer consider it in this
4617 loop. */
4618 DECL_DEFER_OUTPUT (decl) = 0;
4619 /* Generate RTL for this function now that we know we
4620 need it. */
4621 expand_or_defer_fn (decl);
4622 /* If we're compiling -fsyntax-only pretend that this
4623 function has been written out so that we don't try to
4624 expand it again. */
4625 if (flag_syntax_only)
4626 TREE_ASM_WRITTEN (decl) = 1;
4627 reconsider = true;
4631 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4632 reconsider = true;
4634 /* Static data members are just like namespace-scope globals. */
4635 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4637 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4638 /* Don't write it out if we haven't seen a definition. */
4639 || DECL_IN_AGGR_P (decl))
4640 continue;
4641 import_export_decl (decl);
4642 /* If this static data member is needed, provide it to the
4643 back end. */
4644 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4645 DECL_EXTERNAL (decl) = 0;
4647 if (vec_safe_length (pending_statics) != 0
4648 && wrapup_global_declarations (pending_statics->address (),
4649 pending_statics->length ()))
4650 reconsider = true;
4652 retries++;
4654 while (reconsider);
4656 /* All used inline functions must have a definition at this point. */
4657 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4659 if (/* Check online inline functions that were actually used. */
4660 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4661 /* If the definition actually was available here, then the
4662 fact that the function was not defined merely represents
4663 that for some reason (use of a template repository,
4664 #pragma interface, etc.) we decided not to emit the
4665 definition here. */
4666 && !DECL_INITIAL (decl)
4667 /* Don't complain if the template was defined. */
4668 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4669 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4670 (template_for_substitution (decl)))))
4672 warning (0, "inline function %q+D used but never defined", decl);
4673 /* Avoid a duplicate warning from check_global_declaration_1. */
4674 TREE_NO_WARNING (decl) = 1;
4678 /* So must decls that use a type with no linkage. */
4679 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4680 no_linkage_error (decl);
4682 maybe_warn_sized_delete ();
4684 /* Then, do the Objective-C stuff. This is where all the
4685 Objective-C module stuff gets generated (symtab,
4686 class/protocol/selector lists etc). This must be done after C++
4687 templates, destructors etc. so that selectors used in C++
4688 templates are properly allocated. */
4689 if (c_dialect_objc ())
4690 objc_write_global_declarations ();
4692 /* We give C linkage to static constructors and destructors. */
4693 push_lang_context (lang_name_c);
4695 /* Generate initialization and destruction functions for all
4696 priorities for which they are required. */
4697 if (priority_info_map)
4698 splay_tree_foreach (priority_info_map,
4699 generate_ctor_and_dtor_functions_for_priority,
4700 /*data=*/&locus);
4701 else if (c_dialect_objc () && objc_static_init_needed_p ())
4702 /* If this is obj-c++ and we need a static init, call
4703 generate_ctor_or_dtor_function. */
4704 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4705 DEFAULT_INIT_PRIORITY, &locus);
4707 /* We're done with the splay-tree now. */
4708 if (priority_info_map)
4709 splay_tree_delete (priority_info_map);
4711 /* Generate any missing aliases. */
4712 maybe_apply_pending_pragma_weaks ();
4714 /* We're done with static constructors, so we can go back to "C++"
4715 linkage now. */
4716 pop_lang_context ();
4718 /* Collect candidates for Java hidden aliases. */
4719 candidates = collect_candidates_for_java_method_aliases ();
4721 timevar_stop (TV_PHASE_DEFERRED);
4722 timevar_start (TV_PHASE_OPT_GEN);
4724 if (flag_vtable_verify)
4726 vtv_recover_class_info ();
4727 vtv_compute_class_hierarchy_transitive_closure ();
4728 vtv_build_vtable_verify_fndecl ();
4731 symtab->finalize_compilation_unit ();
4733 if (flag_vtable_verify)
4735 /* Generate the special constructor initialization function that
4736 calls __VLTRegisterPairs, and give it a very high
4737 initialization priority. This must be done after
4738 finalize_compilation_unit so that we have accurate
4739 information about which vtable will actually be emitted. */
4740 vtv_generate_init_routine ();
4743 timevar_stop (TV_PHASE_OPT_GEN);
4744 timevar_start (TV_PHASE_CHECK_DBGINFO);
4746 /* Now, issue warnings about static, but not defined, functions,
4747 etc., and emit debugging information. */
4748 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4749 if (vec_safe_length (pending_statics) != 0)
4751 check_global_declarations (pending_statics->address (),
4752 pending_statics->length ());
4753 emit_debug_global_declarations (pending_statics->address (),
4754 pending_statics->length ());
4757 perform_deferred_noexcept_checks ();
4759 /* Generate hidden aliases for Java. */
4760 if (candidates)
4762 build_java_method_aliases (candidates);
4763 delete candidates;
4766 finish_repo ();
4768 /* The entire file is now complete. If requested, dump everything
4769 to a file. */
4770 dump_tu ();
4772 if (flag_detailed_statistics)
4774 dump_tree_statistics ();
4775 dump_time_statistics ();
4777 input_location = locus;
4779 #ifdef ENABLE_CHECKING
4780 validate_conversion_obstack ();
4781 #endif /* ENABLE_CHECKING */
4783 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4786 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4787 function to call in parse-tree form; it has not yet been
4788 semantically analyzed. ARGS are the arguments to the function.
4789 They have already been semantically analyzed. This may change
4790 ARGS. */
4792 tree
4793 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4794 tsubst_flags_t complain)
4796 tree orig_fn;
4797 vec<tree, va_gc> *orig_args = NULL;
4798 tree expr;
4799 tree object;
4801 orig_fn = fn;
4802 object = TREE_OPERAND (fn, 0);
4804 if (processing_template_decl)
4806 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4807 || TREE_CODE (fn) == MEMBER_REF);
4808 if (type_dependent_expression_p (fn)
4809 || any_type_dependent_arguments_p (*args))
4810 return build_nt_call_vec (fn, *args);
4812 orig_args = make_tree_vector_copy (*args);
4814 /* Transform the arguments and add the implicit "this"
4815 parameter. That must be done before the FN is transformed
4816 because we depend on the form of FN. */
4817 make_args_non_dependent (*args);
4818 object = build_non_dependent_expr (object);
4819 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4821 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4822 object = cp_build_addr_expr (object, complain);
4823 vec_safe_insert (*args, 0, object);
4825 /* Now that the arguments are done, transform FN. */
4826 fn = build_non_dependent_expr (fn);
4829 /* A qualified name corresponding to a bound pointer-to-member is
4830 represented as an OFFSET_REF:
4832 struct B { void g(); };
4833 void (B::*p)();
4834 void B::g() { (this->*p)(); } */
4835 if (TREE_CODE (fn) == OFFSET_REF)
4837 tree object_addr = cp_build_addr_expr (object, complain);
4838 fn = TREE_OPERAND (fn, 1);
4839 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4840 complain);
4841 vec_safe_insert (*args, 0, object_addr);
4844 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4845 expr = build_op_call (fn, args, complain);
4846 else
4847 expr = cp_build_function_call_vec (fn, args, complain);
4848 if (processing_template_decl && expr != error_mark_node)
4849 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4851 if (orig_args != NULL)
4852 release_tree_vector (orig_args);
4854 return expr;
4858 void
4859 check_default_args (tree x)
4861 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4862 bool saw_def = false;
4863 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4864 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4866 if (TREE_PURPOSE (arg))
4867 saw_def = true;
4868 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4870 error ("default argument missing for parameter %P of %q+#D", i, x);
4871 TREE_PURPOSE (arg) = error_mark_node;
4876 /* Return true if function DECL can be inlined. This is used to force
4877 instantiation of methods that might be interesting for inlining. */
4878 bool
4879 possibly_inlined_p (tree decl)
4881 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4882 if (DECL_UNINLINABLE (decl))
4883 return false;
4884 if (!optimize || pragma_java_exceptions)
4885 return DECL_DECLARED_INLINE_P (decl);
4886 /* When optimizing, we might inline everything when flatten
4887 attribute or heuristics inlining for size or autoinlining
4888 is used. */
4889 return true;
4892 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4893 If DECL is a specialization or implicitly declared class member,
4894 generate the actual definition. Return false if something goes
4895 wrong, true otherwise. */
4897 bool
4898 mark_used (tree decl, tsubst_flags_t complain)
4900 /* If DECL is a BASELINK for a single function, then treat it just
4901 like the DECL for the function. Otherwise, if the BASELINK is
4902 for an overloaded function, we don't know which function was
4903 actually used until after overload resolution. */
4904 if (BASELINK_P (decl))
4906 decl = BASELINK_FUNCTIONS (decl);
4907 if (really_overloaded_fn (decl))
4908 return true;
4909 decl = OVL_CURRENT (decl);
4912 /* Set TREE_USED for the benefit of -Wunused. */
4913 TREE_USED (decl) = 1;
4914 if (DECL_CLONED_FUNCTION_P (decl))
4915 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4917 /* Mark enumeration types as used. */
4918 if (TREE_CODE (decl) == CONST_DECL)
4919 used_types_insert (DECL_CONTEXT (decl));
4921 if (TREE_CODE (decl) == FUNCTION_DECL)
4922 maybe_instantiate_noexcept (decl);
4924 if (TREE_CODE (decl) == FUNCTION_DECL
4925 && DECL_DELETED_FN (decl))
4927 if (DECL_ARTIFICIAL (decl))
4929 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4930 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4932 /* We mark a lambda conversion op as deleted if we can't
4933 generate it properly; see maybe_add_lambda_conv_op. */
4934 sorry ("converting lambda which uses %<...%> to "
4935 "function pointer");
4936 return false;
4939 if (complain & tf_error)
4941 error ("use of deleted function %qD", decl);
4942 if (!maybe_explain_implicit_delete (decl))
4943 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4945 return false;
4948 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
4949 && deprecated_state != DEPRECATED_SUPPRESS)
4950 warn_deprecated_use (decl, NULL_TREE);
4952 /* We can only check DECL_ODR_USED on variables or functions with
4953 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4954 might need special handling for. */
4955 if (!VAR_OR_FUNCTION_DECL_P (decl)
4956 || DECL_LANG_SPECIFIC (decl) == NULL
4957 || DECL_THUNK_P (decl))
4959 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4961 if (complain & tf_error)
4962 error ("use of %qD before deduction of %<auto%>", decl);
4963 return false;
4965 return true;
4968 /* We only want to do this processing once. We don't need to keep trying
4969 to instantiate inline templates, because unit-at-a-time will make sure
4970 we get them compiled before functions that want to inline them. */
4971 if (DECL_ODR_USED (decl))
4972 return true;
4974 /* If within finish_function, defer the rest until that function
4975 finishes, otherwise it might recurse. */
4976 if (defer_mark_used_calls)
4978 vec_safe_push (deferred_mark_used_calls, decl);
4979 return true;
4982 /* Normally, we can wait until instantiation-time to synthesize DECL.
4983 However, if DECL is a static data member initialized with a constant
4984 or a constexpr function, we need it right now because a reference to
4985 such a data member or a call to such function is not value-dependent.
4986 For a function that uses auto in the return type, we need to instantiate
4987 it to find out its type. For OpenMP user defined reductions, we need
4988 them instantiated for reduction clauses which inline them by hand
4989 directly. */
4990 if (DECL_LANG_SPECIFIC (decl)
4991 && DECL_TEMPLATE_INFO (decl)
4992 && (decl_maybe_constant_var_p (decl)
4993 || (TREE_CODE (decl) == FUNCTION_DECL
4994 && (DECL_DECLARED_CONSTEXPR_P (decl)
4995 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4996 || undeduced_auto_decl (decl))
4997 && !uses_template_parms (DECL_TI_ARGS (decl)))
4999 /* Instantiating a function will result in garbage collection. We
5000 must treat this situation as if we were within the body of a
5001 function so as to avoid collecting live data only referenced from
5002 the stack (such as overload resolution candidates). */
5003 ++function_depth;
5004 instantiate_decl (decl, /*defer_ok=*/false,
5005 /*expl_inst_class_mem_p=*/false);
5006 --function_depth;
5009 if (processing_template_decl)
5010 return true;
5012 /* Check this too in case we're within instantiate_non_dependent_expr. */
5013 if (DECL_TEMPLATE_INFO (decl)
5014 && uses_template_parms (DECL_TI_ARGS (decl)))
5015 return true;
5017 require_deduced_type (decl);
5019 /* If we don't need a value, then we don't need to synthesize DECL. */
5020 if (cp_unevaluated_operand != 0)
5021 return true;
5023 DECL_ODR_USED (decl) = 1;
5024 if (DECL_CLONED_FUNCTION_P (decl))
5025 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5027 /* DR 757: A type without linkage shall not be used as the type of a
5028 variable or function with linkage, unless
5029 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5030 o the variable or function is not used (3.2 [basic.def.odr]) or is
5031 defined in the same translation unit. */
5032 if (cxx_dialect > cxx98
5033 && decl_linkage (decl) != lk_none
5034 && !DECL_EXTERN_C_P (decl)
5035 && !DECL_ARTIFICIAL (decl)
5036 && !decl_defined_p (decl)
5037 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5039 if (is_local_extern (decl))
5040 /* There's no way to define a local extern, and adding it to
5041 the vector interferes with GC, so give an error now. */
5042 no_linkage_error (decl);
5043 else
5044 vec_safe_push (no_linkage_decls, decl);
5047 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5048 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5049 /* Remember it, so we can check it was defined. */
5050 note_vague_linkage_fn (decl);
5052 /* Is it a synthesized method that needs to be synthesized? */
5053 if (TREE_CODE (decl) == FUNCTION_DECL
5054 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5055 && DECL_DEFAULTED_FN (decl)
5056 /* A function defaulted outside the class is synthesized either by
5057 cp_finish_decl or instantiate_decl. */
5058 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5059 && ! DECL_INITIAL (decl))
5061 /* Defer virtual destructors so that thunks get the right
5062 linkage. */
5063 if (DECL_VIRTUAL_P (decl) && !at_eof)
5065 note_vague_linkage_fn (decl);
5066 return true;
5069 /* Remember the current location for a function we will end up
5070 synthesizing. Then we can inform the user where it was
5071 required in the case of error. */
5072 DECL_SOURCE_LOCATION (decl) = input_location;
5074 /* Synthesizing an implicitly defined member function will result in
5075 garbage collection. We must treat this situation as if we were
5076 within the body of a function so as to avoid collecting live data
5077 on the stack (such as overload resolution candidates).
5079 We could just let cp_write_global_declarations handle synthesizing
5080 this function by adding it to deferred_fns, but doing
5081 it at the use site produces better error messages. */
5082 ++function_depth;
5083 synthesize_method (decl);
5084 --function_depth;
5085 /* If this is a synthesized method we don't need to
5086 do the instantiation test below. */
5088 else if (VAR_OR_FUNCTION_DECL_P (decl)
5089 && DECL_TEMPLATE_INFO (decl)
5090 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5091 || always_instantiate_p (decl)))
5092 /* If this is a function or variable that is an instance of some
5093 template, we now know that we will need to actually do the
5094 instantiation. We check that DECL is not an explicit
5095 instantiation because that is not checked in instantiate_decl.
5097 We put off instantiating functions in order to improve compile
5098 times. Maintaining a stack of active functions is expensive,
5099 and the inliner knows to instantiate any functions it might
5100 need. Therefore, we always try to defer instantiation. */
5102 ++function_depth;
5103 instantiate_decl (decl, /*defer_ok=*/true,
5104 /*expl_inst_class_mem_p=*/false);
5105 --function_depth;
5108 return true;
5111 bool
5112 mark_used (tree decl)
5114 return mark_used (decl, tf_warning_or_error);
5117 tree
5118 vtv_start_verification_constructor_init_function (void)
5120 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5123 tree
5124 vtv_finish_verification_constructor_init_function (tree function_body)
5126 tree fn;
5128 finish_compound_stmt (function_body);
5129 fn = finish_function (0);
5130 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5131 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5133 return fn;
5136 #include "gt-cp-decl2.h"