Daily bump.
[official-gcc.git] / gcc / cp / decl2.c
blobff407101f081e10e451a2a3e6878fe4758aab01f
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 attributes abi_tag and visibility specifically apply
1208 to templates. */
1209 && !is_attribute_p ("abi_tag", 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 (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));
1487 /* Walks through the namespace- or function-scope anonymous union
1488 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1489 Returns one of the fields for use in the mangled name. */
1491 static tree
1492 build_anon_union_vars (tree type, tree object)
1494 tree main_decl = NULL_TREE;
1495 tree field;
1497 /* Rather than write the code to handle the non-union case,
1498 just give an error. */
1499 if (TREE_CODE (type) != UNION_TYPE)
1501 error ("anonymous struct not inside named type");
1502 return error_mark_node;
1505 for (field = TYPE_FIELDS (type);
1506 field != NULL_TREE;
1507 field = DECL_CHAIN (field))
1509 tree decl;
1510 tree ref;
1512 if (DECL_ARTIFICIAL (field))
1513 continue;
1514 if (TREE_CODE (field) != FIELD_DECL)
1516 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1517 "have non-static data members", field);
1518 continue;
1521 if (TREE_PRIVATE (field))
1522 permerror (input_location, "private member %q+#D in anonymous union", field);
1523 else if (TREE_PROTECTED (field))
1524 permerror (input_location, "protected member %q+#D in anonymous union", field);
1526 if (processing_template_decl)
1527 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1528 DECL_NAME (field), NULL_TREE);
1529 else
1530 ref = build_class_member_access_expr (object, field, NULL_TREE,
1531 false, tf_warning_or_error);
1533 if (DECL_NAME (field))
1535 tree base;
1537 decl = build_decl (input_location,
1538 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1539 DECL_ANON_UNION_VAR_P (decl) = 1;
1540 DECL_ARTIFICIAL (decl) = 1;
1542 base = get_base_address (object);
1543 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1544 TREE_STATIC (decl) = TREE_STATIC (base);
1545 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1547 SET_DECL_VALUE_EXPR (decl, ref);
1548 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1550 decl = pushdecl (decl);
1552 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1553 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1554 else
1555 decl = 0;
1557 if (main_decl == NULL_TREE)
1558 main_decl = decl;
1561 return main_decl;
1564 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1565 anonymous union, then all members must be laid out together. PUBLIC_P
1566 is nonzero if this union is not declared static. */
1568 void
1569 finish_anon_union (tree anon_union_decl)
1571 tree type;
1572 tree main_decl;
1573 bool public_p;
1575 if (anon_union_decl == error_mark_node)
1576 return;
1578 type = TREE_TYPE (anon_union_decl);
1579 public_p = TREE_PUBLIC (anon_union_decl);
1581 /* The VAR_DECL's context is the same as the TYPE's context. */
1582 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1584 if (TYPE_FIELDS (type) == NULL_TREE)
1585 return;
1587 if (public_p)
1589 error ("namespace-scope anonymous aggregates must be static");
1590 return;
1593 main_decl = build_anon_union_vars (type, anon_union_decl);
1594 if (main_decl == error_mark_node)
1595 return;
1596 if (main_decl == NULL_TREE)
1598 warning (0, "anonymous union with no members");
1599 return;
1602 if (!processing_template_decl)
1604 /* Use main_decl to set the mangled name. */
1605 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1606 maybe_commonize_var (anon_union_decl);
1607 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1608 mangle_decl (anon_union_decl);
1609 DECL_NAME (anon_union_decl) = NULL_TREE;
1612 pushdecl (anon_union_decl);
1613 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1616 /* Auxiliary functions to make type signatures for
1617 `operator new' and `operator delete' correspond to
1618 what compiler will be expecting. */
1620 tree
1621 coerce_new_type (tree type)
1623 int e = 0;
1624 tree args = TYPE_ARG_TYPES (type);
1626 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1628 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1630 e = 1;
1631 error ("%<operator new%> must return type %qT", ptr_type_node);
1634 if (args && args != void_list_node)
1636 if (TREE_PURPOSE (args))
1638 /* [basic.stc.dynamic.allocation]
1640 The first parameter shall not have an associated default
1641 argument. */
1642 error ("the first parameter of %<operator new%> cannot "
1643 "have a default argument");
1644 /* Throw away the default argument. */
1645 TREE_PURPOSE (args) = NULL_TREE;
1648 if (!same_type_p (TREE_VALUE (args), size_type_node))
1650 e = 2;
1651 args = TREE_CHAIN (args);
1654 else
1655 e = 2;
1657 if (e == 2)
1658 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1659 "as first parameter", size_type_node);
1661 switch (e)
1663 case 2:
1664 args = tree_cons (NULL_TREE, size_type_node, args);
1665 /* Fall through. */
1666 case 1:
1667 type = build_exception_variant
1668 (build_function_type (ptr_type_node, args),
1669 TYPE_RAISES_EXCEPTIONS (type));
1670 /* Fall through. */
1671 default:;
1673 return type;
1676 tree
1677 coerce_delete_type (tree type)
1679 int e = 0;
1680 tree args = TYPE_ARG_TYPES (type);
1682 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1684 if (!same_type_p (TREE_TYPE (type), void_type_node))
1686 e = 1;
1687 error ("%<operator delete%> must return type %qT", void_type_node);
1690 if (!args || args == void_list_node
1691 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1693 e = 2;
1694 if (args && args != void_list_node)
1695 args = TREE_CHAIN (args);
1696 error ("%<operator delete%> takes type %qT as first parameter",
1697 ptr_type_node);
1699 switch (e)
1701 case 2:
1702 args = tree_cons (NULL_TREE, ptr_type_node, args);
1703 /* Fall through. */
1704 case 1:
1705 type = build_exception_variant
1706 (build_function_type (void_type_node, args),
1707 TYPE_RAISES_EXCEPTIONS (type));
1708 /* Fall through. */
1709 default:;
1712 return type;
1715 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1716 and mark them as needed. */
1718 static void
1719 mark_vtable_entries (tree decl)
1721 tree fnaddr;
1722 unsigned HOST_WIDE_INT idx;
1724 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1725 idx, fnaddr)
1727 tree fn;
1729 STRIP_NOPS (fnaddr);
1731 if (TREE_CODE (fnaddr) != ADDR_EXPR
1732 && TREE_CODE (fnaddr) != FDESC_EXPR)
1733 /* This entry is an offset: a virtual base class offset, a
1734 virtual call offset, an RTTI offset, etc. */
1735 continue;
1737 fn = TREE_OPERAND (fnaddr, 0);
1738 TREE_ADDRESSABLE (fn) = 1;
1739 /* When we don't have vcall offsets, we output thunks whenever
1740 we output the vtables that contain them. With vcall offsets,
1741 we know all the thunks we'll need when we emit a virtual
1742 function, so we emit the thunks there instead. */
1743 if (DECL_THUNK_P (fn))
1744 use_thunk (fn, /*emit_p=*/0);
1745 mark_used (fn);
1749 /* Set DECL up to have the closest approximation of "initialized common"
1750 linkage available. */
1752 void
1753 comdat_linkage (tree decl)
1755 if (flag_weak)
1756 make_decl_one_only (decl, cxx_comdat_group (decl));
1757 else if (TREE_CODE (decl) == FUNCTION_DECL
1758 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1759 /* We can just emit function and compiler-generated variables
1760 statically; having multiple copies is (for the most part) only
1761 a waste of space.
1763 There are two correctness issues, however: the address of a
1764 template instantiation with external linkage should be the
1765 same, independent of what translation unit asks for the
1766 address, and this will not hold when we emit multiple copies of
1767 the function. However, there's little else we can do.
1769 Also, by default, the typeinfo implementation assumes that
1770 there will be only one copy of the string used as the name for
1771 each type. Therefore, if weak symbols are unavailable, the
1772 run-time library should perform a more conservative check; it
1773 should perform a string comparison, rather than an address
1774 comparison. */
1775 TREE_PUBLIC (decl) = 0;
1776 else
1778 /* Static data member template instantiations, however, cannot
1779 have multiple copies. */
1780 if (DECL_INITIAL (decl) == 0
1781 || DECL_INITIAL (decl) == error_mark_node)
1782 DECL_COMMON (decl) = 1;
1783 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1785 DECL_COMMON (decl) = 1;
1786 DECL_INITIAL (decl) = error_mark_node;
1788 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1790 /* We can't do anything useful; leave vars for explicit
1791 instantiation. */
1792 DECL_EXTERNAL (decl) = 1;
1793 DECL_NOT_REALLY_EXTERN (decl) = 0;
1797 DECL_COMDAT (decl) = 1;
1800 /* For win32 we also want to put explicit instantiations in
1801 linkonce sections, so that they will be merged with implicit
1802 instantiations; otherwise we get duplicate symbol errors.
1803 For Darwin we do not want explicit instantiations to be
1804 linkonce. */
1806 void
1807 maybe_make_one_only (tree decl)
1809 /* We used to say that this was not necessary on targets that support weak
1810 symbols, because the implicit instantiations will defer to the explicit
1811 one. However, that's not actually the case in SVR4; a strong definition
1812 after a weak one is an error. Also, not making explicit
1813 instantiations one_only means that we can end up with two copies of
1814 some template instantiations. */
1815 if (! flag_weak)
1816 return;
1818 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1819 we can get away with not emitting them if they aren't used. We need
1820 to for variables so that cp_finish_decl will update their linkage,
1821 because their DECL_INITIAL may not have been set properly yet. */
1823 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1824 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1825 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1827 make_decl_one_only (decl, cxx_comdat_group (decl));
1829 if (VAR_P (decl))
1831 varpool_node *node = varpool_node::get_create (decl);
1832 DECL_COMDAT (decl) = 1;
1833 /* Mark it needed so we don't forget to emit it. */
1834 node->forced_by_abi = true;
1835 TREE_USED (decl) = 1;
1840 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1841 This predicate will give the right answer during parsing of the
1842 function, which other tests may not. */
1844 bool
1845 vague_linkage_p (tree decl)
1847 /* Unfortunately, import_export_decl has not always been called
1848 before the function is processed, so we cannot simply check
1849 DECL_COMDAT. */
1850 if (DECL_COMDAT (decl)
1851 || (((TREE_CODE (decl) == FUNCTION_DECL
1852 && DECL_DECLARED_INLINE_P (decl))
1853 || (DECL_LANG_SPECIFIC (decl)
1854 && DECL_TEMPLATE_INSTANTIATION (decl)))
1855 && TREE_PUBLIC (decl)))
1856 return true;
1857 else if (DECL_FUNCTION_SCOPE_P (decl))
1858 /* A local static in an inline effectively has vague linkage. */
1859 return (TREE_STATIC (decl)
1860 && vague_linkage_p (DECL_CONTEXT (decl)));
1861 else
1862 return false;
1865 /* Determine whether or not we want to specifically import or export CTYPE,
1866 using various heuristics. */
1868 static void
1869 import_export_class (tree ctype)
1871 /* -1 for imported, 1 for exported. */
1872 int import_export = 0;
1874 /* It only makes sense to call this function at EOF. The reason is
1875 that this function looks at whether or not the first non-inline
1876 non-abstract virtual member function has been defined in this
1877 translation unit. But, we can't possibly know that until we've
1878 seen the entire translation unit. */
1879 gcc_assert (at_eof);
1881 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1882 return;
1884 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1885 we will have CLASSTYPE_INTERFACE_ONLY set but not
1886 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1887 heuristic because someone will supply a #pragma implementation
1888 elsewhere, and deducing it here would produce a conflict. */
1889 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1890 return;
1892 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1893 import_export = -1;
1894 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1895 import_export = 1;
1896 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1897 && !flag_implicit_templates)
1898 /* For a template class, without -fimplicit-templates, check the
1899 repository. If the virtual table is assigned to this
1900 translation unit, then export the class; otherwise, import
1901 it. */
1902 import_export = repo_export_class_p (ctype) ? 1 : -1;
1903 else if (TYPE_POLYMORPHIC_P (ctype))
1905 /* The ABI specifies that the virtual table and associated
1906 information are emitted with the key method, if any. */
1907 tree method = CLASSTYPE_KEY_METHOD (ctype);
1908 /* If weak symbol support is not available, then we must be
1909 careful not to emit the vtable when the key function is
1910 inline. An inline function can be defined in multiple
1911 translation units. If we were to emit the vtable in each
1912 translation unit containing a definition, we would get
1913 multiple definition errors at link-time. */
1914 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1915 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1918 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1919 a definition anywhere else. */
1920 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1921 import_export = 0;
1923 /* Allow back ends the chance to overrule the decision. */
1924 if (targetm.cxx.import_export_class)
1925 import_export = targetm.cxx.import_export_class (ctype, import_export);
1927 if (import_export)
1929 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1930 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1934 /* Return true if VAR has already been provided to the back end; in that
1935 case VAR should not be modified further by the front end. */
1936 static bool
1937 var_finalized_p (tree var)
1939 return varpool_node::get_create (var)->definition;
1942 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1943 must be emitted in this translation unit. Mark it as such. */
1945 void
1946 mark_needed (tree decl)
1948 TREE_USED (decl) = 1;
1949 if (TREE_CODE (decl) == FUNCTION_DECL)
1951 /* Extern inline functions don't become needed when referenced.
1952 If we know a method will be emitted in other TU and no new
1953 functions can be marked reachable, just use the external
1954 definition. */
1955 struct cgraph_node *node = cgraph_node::get_create (decl);
1956 node->forced_by_abi = true;
1958 /* #pragma interface and -frepo code can call mark_needed for
1959 maybe-in-charge 'tors; mark the clones as well. */
1960 tree clone;
1961 FOR_EACH_CLONE (clone, decl)
1962 mark_needed (clone);
1964 else if (TREE_CODE (decl) == VAR_DECL)
1966 varpool_node *node = varpool_node::get_create (decl);
1967 /* C++ frontend use mark_decl_references to force COMDAT variables
1968 to be output that might appear dead otherwise. */
1969 node->forced_by_abi = true;
1973 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1974 returns true if a definition of this entity should be provided in
1975 this object file. Callers use this function to determine whether
1976 or not to let the back end know that a definition of DECL is
1977 available in this translation unit. */
1979 bool
1980 decl_needed_p (tree decl)
1982 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1983 /* This function should only be called at the end of the translation
1984 unit. We cannot be sure of whether or not something will be
1985 COMDAT until that point. */
1986 gcc_assert (at_eof);
1988 /* All entities with external linkage that are not COMDAT should be
1989 emitted; they may be referred to from other object files. */
1990 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1991 return true;
1992 /* If this entity was used, let the back end see it; it will decide
1993 whether or not to emit it into the object file. */
1994 if (TREE_USED (decl))
1995 return true;
1996 /* Functions marked "dllexport" must be emitted so that they are
1997 visible to other DLLs. */
1998 if (flag_keep_inline_dllexport
1999 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2000 return true;
2001 /* Virtual functions might be needed for devirtualization. */
2002 if (flag_devirtualize
2003 && TREE_CODE (decl) == FUNCTION_DECL
2004 && DECL_VIRTUAL_P (decl))
2005 return true;
2006 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2007 reference to DECL might cause it to be emitted later. */
2008 return false;
2011 /* If necessary, write out the vtables for the dynamic class CTYPE.
2012 Returns true if any vtables were emitted. */
2014 static bool
2015 maybe_emit_vtables (tree ctype)
2017 tree vtbl;
2018 tree primary_vtbl;
2019 int needed = 0;
2020 varpool_node *current = NULL, *last = NULL;
2022 /* If the vtables for this class have already been emitted there is
2023 nothing more to do. */
2024 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2025 if (var_finalized_p (primary_vtbl))
2026 return false;
2027 /* Ignore dummy vtables made by get_vtable_decl. */
2028 if (TREE_TYPE (primary_vtbl) == void_type_node)
2029 return false;
2031 /* On some targets, we cannot determine the key method until the end
2032 of the translation unit -- which is when this function is
2033 called. */
2034 if (!targetm.cxx.key_method_may_be_inline ())
2035 determine_key_method (ctype);
2037 /* See if any of the vtables are needed. */
2038 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2040 import_export_decl (vtbl);
2041 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2042 needed = 1;
2044 if (!needed)
2046 /* If the references to this class' vtables are optimized away,
2047 still emit the appropriate debugging information. See
2048 dfs_debug_mark. */
2049 if (DECL_COMDAT (primary_vtbl)
2050 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2051 note_debug_info_needed (ctype);
2052 return false;
2055 /* The ABI requires that we emit all of the vtables if we emit any
2056 of them. */
2057 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2059 /* Mark entities references from the virtual table as used. */
2060 mark_vtable_entries (vtbl);
2062 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2064 vec<tree, va_gc> *cleanups = NULL;
2065 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2066 LOOKUP_NORMAL);
2068 /* It had better be all done at compile-time. */
2069 gcc_assert (!expr && !cleanups);
2072 /* Write it out. */
2073 DECL_EXTERNAL (vtbl) = 0;
2074 rest_of_decl_compilation (vtbl, 1, 1);
2076 /* Because we're only doing syntax-checking, we'll never end up
2077 actually marking the variable as written. */
2078 if (flag_syntax_only)
2079 TREE_ASM_WRITTEN (vtbl) = 1;
2080 else if (DECL_ONE_ONLY (vtbl))
2082 current = varpool_node::get_create (vtbl);
2083 if (last)
2084 current->add_to_same_comdat_group (last);
2085 last = current;
2089 /* Since we're writing out the vtable here, also write the debug
2090 info. */
2091 note_debug_info_needed (ctype);
2093 return true;
2096 /* A special return value from type_visibility meaning internal
2097 linkage. */
2099 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2101 /* walk_tree helper function for type_visibility. */
2103 static tree
2104 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2106 int *vis_p = (int *)data;
2107 if (! TYPE_P (*tp))
2109 *walk_subtrees = 0;
2111 else if (OVERLOAD_TYPE_P (*tp)
2112 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2114 *vis_p = VISIBILITY_ANON;
2115 return *tp;
2117 else if (CLASS_TYPE_P (*tp)
2118 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2119 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2120 return NULL;
2123 /* Returns the visibility of TYPE, which is the minimum visibility of its
2124 component types. */
2126 static int
2127 type_visibility (tree type)
2129 int vis = VISIBILITY_DEFAULT;
2130 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2131 return vis;
2134 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2135 specified (or if VISIBILITY is static). If TMPL is true, this
2136 constraint is for a template argument, and takes precedence
2137 over explicitly-specified visibility on the template. */
2139 static void
2140 constrain_visibility (tree decl, int visibility, bool tmpl)
2142 if (visibility == VISIBILITY_ANON)
2144 /* extern "C" declarations aren't affected by the anonymous
2145 namespace. */
2146 if (!DECL_EXTERN_C_P (decl))
2148 TREE_PUBLIC (decl) = 0;
2149 DECL_WEAK (decl) = 0;
2150 DECL_COMMON (decl) = 0;
2151 if (TREE_CODE (decl) == FUNCTION_DECL
2152 || TREE_CODE (decl) == VAR_DECL)
2154 struct symtab_node *snode = symtab_node::get (decl);
2156 if (snode)
2157 snode->set_comdat_group (NULL);
2159 DECL_INTERFACE_KNOWN (decl) = 1;
2160 if (DECL_LANG_SPECIFIC (decl))
2161 DECL_NOT_REALLY_EXTERN (decl) = 1;
2164 else if (visibility > DECL_VISIBILITY (decl)
2165 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2167 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2168 /* This visibility was not specified. */
2169 DECL_VISIBILITY_SPECIFIED (decl) = false;
2173 /* Constrain the visibility of DECL based on the visibility of its template
2174 arguments. */
2176 static void
2177 constrain_visibility_for_template (tree decl, tree targs)
2179 /* If this is a template instantiation, check the innermost
2180 template args for visibility constraints. The outer template
2181 args are covered by the class check. */
2182 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2183 int i;
2184 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2186 int vis = 0;
2188 tree arg = TREE_VEC_ELT (args, i-1);
2189 if (TYPE_P (arg))
2190 vis = type_visibility (arg);
2191 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2193 STRIP_NOPS (arg);
2194 if (TREE_CODE (arg) == ADDR_EXPR)
2195 arg = TREE_OPERAND (arg, 0);
2196 if (VAR_OR_FUNCTION_DECL_P (arg))
2198 if (! TREE_PUBLIC (arg))
2199 vis = VISIBILITY_ANON;
2200 else
2201 vis = DECL_VISIBILITY (arg);
2204 if (vis)
2205 constrain_visibility (decl, vis, true);
2209 /* Like c_determine_visibility, but with additional C++-specific
2210 behavior.
2212 Function-scope entities can rely on the function's visibility because
2213 it is set in start_preparsed_function.
2215 Class-scope entities cannot rely on the class's visibility until the end
2216 of the enclosing class definition.
2218 Note that because namespaces have multiple independent definitions,
2219 namespace visibility is handled elsewhere using the #pragma visibility
2220 machinery rather than by decorating the namespace declaration.
2222 The goal is for constraints from the type to give a diagnostic, and
2223 other constraints to be applied silently. */
2225 void
2226 determine_visibility (tree decl)
2228 tree class_type = NULL_TREE;
2229 bool use_template;
2230 bool orig_visibility_specified;
2231 enum symbol_visibility orig_visibility;
2233 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2235 /* Only relevant for names with external linkage. */
2236 if (!TREE_PUBLIC (decl))
2237 return;
2239 /* Cloned constructors and destructors get the same visibility as
2240 the underlying function. That should be set up in
2241 maybe_clone_body. */
2242 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2244 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2245 orig_visibility = DECL_VISIBILITY (decl);
2247 if (TREE_CODE (decl) == TYPE_DECL)
2249 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2250 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2251 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2252 use_template = 1;
2253 else
2254 use_template = 0;
2256 else if (DECL_LANG_SPECIFIC (decl))
2257 use_template = DECL_USE_TEMPLATE (decl);
2258 else
2259 use_template = 0;
2261 /* If DECL is a member of a class, visibility specifiers on the
2262 class can influence the visibility of the DECL. */
2263 if (DECL_CLASS_SCOPE_P (decl))
2264 class_type = DECL_CONTEXT (decl);
2265 else
2267 /* Not a class member. */
2269 /* Virtual tables have DECL_CONTEXT set to their associated class,
2270 so they are automatically handled above. */
2271 gcc_assert (!VAR_P (decl)
2272 || !DECL_VTABLE_OR_VTT_P (decl));
2274 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2276 /* Local statics and classes get the visibility of their
2277 containing function by default, except that
2278 -fvisibility-inlines-hidden doesn't affect them. */
2279 tree fn = DECL_CONTEXT (decl);
2280 if (DECL_VISIBILITY_SPECIFIED (fn))
2282 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2283 DECL_VISIBILITY_SPECIFIED (decl) =
2284 DECL_VISIBILITY_SPECIFIED (fn);
2286 else
2288 if (DECL_CLASS_SCOPE_P (fn))
2289 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2290 else if (determine_hidden_inline (fn))
2292 DECL_VISIBILITY (decl) = default_visibility;
2293 DECL_VISIBILITY_SPECIFIED (decl) =
2294 visibility_options.inpragma;
2296 else
2298 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2299 DECL_VISIBILITY_SPECIFIED (decl) =
2300 DECL_VISIBILITY_SPECIFIED (fn);
2304 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2305 but have no TEMPLATE_INFO, so don't try to check it. */
2306 use_template = 0;
2308 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2309 && flag_visibility_ms_compat)
2311 /* Under -fvisibility-ms-compat, types are visible by default,
2312 even though their contents aren't. */
2313 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2314 int underlying_vis = type_visibility (underlying_type);
2315 if (underlying_vis == VISIBILITY_ANON
2316 || (CLASS_TYPE_P (underlying_type)
2317 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2318 constrain_visibility (decl, underlying_vis, false);
2319 else
2320 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2322 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2324 /* tinfo visibility is based on the type it's for. */
2325 constrain_visibility
2326 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2328 /* Give the target a chance to override the visibility associated
2329 with DECL. */
2330 if (TREE_PUBLIC (decl)
2331 && !DECL_REALLY_EXTERN (decl)
2332 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2333 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2334 targetm.cxx.determine_class_data_visibility (decl);
2336 else if (use_template)
2337 /* Template instantiations and specializations get visibility based
2338 on their template unless they override it with an attribute. */;
2339 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2341 if (determine_hidden_inline (decl))
2342 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2343 else
2345 /* Set default visibility to whatever the user supplied with
2346 #pragma GCC visibility or a namespace visibility attribute. */
2347 DECL_VISIBILITY (decl) = default_visibility;
2348 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2353 if (use_template)
2355 /* If the specialization doesn't specify visibility, use the
2356 visibility from the template. */
2357 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2358 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2359 : DECL_TEMPLATE_INFO (decl));
2360 tree args = TI_ARGS (tinfo);
2361 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2362 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2363 : DECL_ATTRIBUTES (decl));
2365 if (args != error_mark_node)
2367 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2369 if (!DECL_VISIBILITY_SPECIFIED (decl))
2371 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2372 && determine_hidden_inline (decl))
2373 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2374 else
2376 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2377 DECL_VISIBILITY_SPECIFIED (decl)
2378 = DECL_VISIBILITY_SPECIFIED (pattern);
2382 if (args
2383 /* Template argument visibility outweighs #pragma or namespace
2384 visibility, but not an explicit attribute. */
2385 && !lookup_attribute ("visibility", attribs))
2387 int depth = TMPL_ARGS_DEPTH (args);
2388 if (DECL_VISIBILITY_SPECIFIED (decl))
2390 /* A class template member with explicit visibility
2391 overrides the class visibility, so we need to apply
2392 all the levels of template args directly. */
2393 int i;
2394 for (i = 1; i <= depth; ++i)
2396 tree lev = TMPL_ARGS_LEVEL (args, i);
2397 constrain_visibility_for_template (decl, lev);
2400 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2401 /* Limit visibility based on its template arguments. */
2402 constrain_visibility_for_template (decl, args);
2407 if (class_type)
2408 determine_visibility_from_class (decl, class_type);
2410 if (decl_anon_ns_mem_p (decl))
2411 /* Names in an anonymous namespace get internal linkage.
2412 This might change once we implement export. */
2413 constrain_visibility (decl, VISIBILITY_ANON, false);
2414 else if (TREE_CODE (decl) != TYPE_DECL)
2416 /* Propagate anonymity from type to decl. */
2417 int tvis = type_visibility (TREE_TYPE (decl));
2418 if (tvis == VISIBILITY_ANON
2419 || ! DECL_VISIBILITY_SPECIFIED (decl))
2420 constrain_visibility (decl, tvis, false);
2422 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2423 /* DR 757: A type without linkage shall not be used as the type of a
2424 variable or function with linkage, unless
2425 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2426 o the variable or function is not used (3.2 [basic.def.odr]) or is
2427 defined in the same translation unit.
2429 Since non-extern "C" decls need to be defined in the same
2430 translation unit, we can make the type internal. */
2431 constrain_visibility (decl, VISIBILITY_ANON, false);
2433 /* If visibility changed and DECL already has DECL_RTL, ensure
2434 symbol flags are updated. */
2435 if ((DECL_VISIBILITY (decl) != orig_visibility
2436 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2437 && ((VAR_P (decl) && TREE_STATIC (decl))
2438 || TREE_CODE (decl) == FUNCTION_DECL)
2439 && DECL_RTL_SET_P (decl))
2440 make_decl_rtl (decl);
2443 /* By default, static data members and function members receive
2444 the visibility of their containing class. */
2446 static void
2447 determine_visibility_from_class (tree decl, tree class_type)
2449 if (DECL_VISIBILITY_SPECIFIED (decl))
2450 return;
2452 if (determine_hidden_inline (decl))
2453 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2454 else
2456 /* Default to the class visibility. */
2457 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2458 DECL_VISIBILITY_SPECIFIED (decl)
2459 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2462 /* Give the target a chance to override the visibility associated
2463 with DECL. */
2464 if (VAR_P (decl)
2465 && (DECL_TINFO_P (decl)
2466 || (DECL_VTABLE_OR_VTT_P (decl)
2467 /* Construction virtual tables are not exported because
2468 they cannot be referred to from other object files;
2469 their name is not standardized by the ABI. */
2470 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2471 && TREE_PUBLIC (decl)
2472 && !DECL_REALLY_EXTERN (decl)
2473 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2474 targetm.cxx.determine_class_data_visibility (decl);
2477 /* Returns true iff DECL is an inline that should get hidden visibility
2478 because of -fvisibility-inlines-hidden. */
2480 static bool
2481 determine_hidden_inline (tree decl)
2483 return (visibility_options.inlines_hidden
2484 /* Don't do this for inline templates; specializations might not be
2485 inline, and we don't want them to inherit the hidden
2486 visibility. We'll set it here for all inline instantiations. */
2487 && !processing_template_decl
2488 && TREE_CODE (decl) == FUNCTION_DECL
2489 && DECL_DECLARED_INLINE_P (decl)
2490 && (! DECL_LANG_SPECIFIC (decl)
2491 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2494 /* Constrain the visibility of a class TYPE based on the visibility of its
2495 field types. Warn if any fields require lesser visibility. */
2497 void
2498 constrain_class_visibility (tree type)
2500 tree binfo;
2501 tree t;
2502 int i;
2504 int vis = type_visibility (type);
2506 if (vis == VISIBILITY_ANON
2507 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2508 return;
2510 /* Don't warn about visibility if the class has explicit visibility. */
2511 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2512 vis = VISIBILITY_INTERNAL;
2514 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2515 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2517 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2518 int subvis = type_visibility (ftype);
2520 if (subvis == VISIBILITY_ANON)
2522 if (!in_main_input_context ())
2523 warning (0, "\
2524 %qT has a field %qD whose type uses the anonymous namespace",
2525 type, t);
2527 else if (MAYBE_CLASS_TYPE_P (ftype)
2528 && vis < VISIBILITY_HIDDEN
2529 && subvis >= VISIBILITY_HIDDEN)
2530 warning (OPT_Wattributes, "\
2531 %qT declared with greater visibility than the type of its field %qD",
2532 type, t);
2535 binfo = TYPE_BINFO (type);
2536 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2538 int subvis = type_visibility (TREE_TYPE (t));
2540 if (subvis == VISIBILITY_ANON)
2542 if (!in_main_input_context())
2543 warning (0, "\
2544 %qT has a base %qT whose type uses the anonymous namespace",
2545 type, TREE_TYPE (t));
2547 else if (vis < VISIBILITY_HIDDEN
2548 && subvis >= VISIBILITY_HIDDEN)
2549 warning (OPT_Wattributes, "\
2550 %qT declared with greater visibility than its base %qT",
2551 type, TREE_TYPE (t));
2555 /* Functions for adjusting the visibility of a tagged type and its nested
2556 types and declarations when it gets a name for linkage purposes from a
2557 typedef. */
2559 static void bt_reset_linkage_1 (binding_entry, void *);
2560 static void bt_reset_linkage_2 (binding_entry, void *);
2562 /* First reset the visibility of all the types. */
2564 static void
2565 reset_type_linkage_1 (tree type)
2567 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2568 if (CLASS_TYPE_P (type))
2569 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2570 bt_reset_linkage_1, NULL);
2572 static void
2573 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2575 reset_type_linkage_1 (b->type);
2578 /* Then reset the visibility of any static data members or member
2579 functions that use those types. */
2581 static void
2582 reset_decl_linkage (tree decl)
2584 if (TREE_PUBLIC (decl))
2585 return;
2586 if (DECL_CLONED_FUNCTION_P (decl))
2587 return;
2588 TREE_PUBLIC (decl) = true;
2589 DECL_INTERFACE_KNOWN (decl) = false;
2590 determine_visibility (decl);
2591 tentative_decl_linkage (decl);
2593 static void
2594 reset_type_linkage_2 (tree type)
2596 if (CLASS_TYPE_P (type))
2598 if (tree vt = CLASSTYPE_VTABLES (type))
2600 tree name = mangle_vtbl_for_type (type);
2601 DECL_NAME (vt) = name;
2602 SET_DECL_ASSEMBLER_NAME (vt, name);
2603 reset_decl_linkage (vt);
2605 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2607 tree name = mangle_typeinfo_for_type (type);
2608 DECL_NAME (ti) = name;
2609 SET_DECL_ASSEMBLER_NAME (ti, name);
2610 TREE_TYPE (name) = type;
2611 reset_decl_linkage (ti);
2613 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2614 if (TREE_CODE (m) == VAR_DECL)
2615 reset_decl_linkage (m);
2616 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2617 reset_decl_linkage (m);
2618 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2619 bt_reset_linkage_2, NULL);
2622 static void
2623 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2625 reset_type_linkage_2 (b->type);
2627 void
2628 reset_type_linkage (tree type)
2630 reset_type_linkage_1 (type);
2631 reset_type_linkage_2 (type);
2634 /* Set up our initial idea of what the linkage of DECL should be. */
2636 void
2637 tentative_decl_linkage (tree decl)
2639 if (DECL_INTERFACE_KNOWN (decl))
2640 /* We've already made a decision as to how this function will
2641 be handled. */;
2642 else if (vague_linkage_p (decl))
2644 if (TREE_CODE (decl) == FUNCTION_DECL
2645 && decl_defined_p (decl))
2647 DECL_EXTERNAL (decl) = 1;
2648 DECL_NOT_REALLY_EXTERN (decl) = 1;
2649 note_vague_linkage_fn (decl);
2650 /* A non-template inline function with external linkage will
2651 always be COMDAT. As we must eventually determine the
2652 linkage of all functions, and as that causes writes to
2653 the data mapped in from the PCH file, it's advantageous
2654 to mark the functions at this point. */
2655 if (DECL_DECLARED_INLINE_P (decl)
2656 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2657 || DECL_DEFAULTED_FN (decl)))
2659 /* This function must have external linkage, as
2660 otherwise DECL_INTERFACE_KNOWN would have been
2661 set. */
2662 gcc_assert (TREE_PUBLIC (decl));
2663 comdat_linkage (decl);
2664 DECL_INTERFACE_KNOWN (decl) = 1;
2667 else if (TREE_CODE (decl) == VAR_DECL)
2668 maybe_commonize_var (decl);
2672 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2673 for DECL has not already been determined, do so now by setting
2674 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2675 function is called entities with vague linkage whose definitions
2676 are available must have TREE_PUBLIC set.
2678 If this function decides to place DECL in COMDAT, it will set
2679 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2680 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2681 callers defer that decision until it is clear that DECL is actually
2682 required. */
2684 void
2685 import_export_decl (tree decl)
2687 int emit_p;
2688 bool comdat_p;
2689 bool import_p;
2690 tree class_type = NULL_TREE;
2692 if (DECL_INTERFACE_KNOWN (decl))
2693 return;
2695 /* We cannot determine what linkage to give to an entity with vague
2696 linkage until the end of the file. For example, a virtual table
2697 for a class will be defined if and only if the key method is
2698 defined in this translation unit. As a further example, consider
2699 that when compiling a translation unit that uses PCH file with
2700 "-frepo" it would be incorrect to make decisions about what
2701 entities to emit when building the PCH; those decisions must be
2702 delayed until the repository information has been processed. */
2703 gcc_assert (at_eof);
2704 /* Object file linkage for explicit instantiations is handled in
2705 mark_decl_instantiated. For static variables in functions with
2706 vague linkage, maybe_commonize_var is used.
2708 Therefore, the only declarations that should be provided to this
2709 function are those with external linkage that are:
2711 * implicit instantiations of function templates
2713 * inline function
2715 * implicit instantiations of static data members of class
2716 templates
2718 * virtual tables
2720 * typeinfo objects
2722 Furthermore, all entities that reach this point must have a
2723 definition available in this translation unit.
2725 The following assertions check these conditions. */
2726 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2727 /* Any code that creates entities with TREE_PUBLIC cleared should
2728 also set DECL_INTERFACE_KNOWN. */
2729 gcc_assert (TREE_PUBLIC (decl));
2730 if (TREE_CODE (decl) == FUNCTION_DECL)
2731 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2732 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2733 || DECL_DECLARED_INLINE_P (decl));
2734 else
2735 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2736 || DECL_VTABLE_OR_VTT_P (decl)
2737 || DECL_TINFO_P (decl));
2738 /* Check that a definition of DECL is available in this translation
2739 unit. */
2740 gcc_assert (!DECL_REALLY_EXTERN (decl));
2742 /* Assume that DECL will not have COMDAT linkage. */
2743 comdat_p = false;
2744 /* Assume that DECL will not be imported into this translation
2745 unit. */
2746 import_p = false;
2748 /* See if the repository tells us whether or not to emit DECL in
2749 this translation unit. */
2750 emit_p = repo_emit_p (decl);
2751 if (emit_p == 0)
2752 import_p = true;
2753 else if (emit_p == 1)
2755 /* The repository indicates that this entity should be defined
2756 here. Make sure the back end honors that request. */
2757 mark_needed (decl);
2758 /* Output the definition as an ordinary strong definition. */
2759 DECL_EXTERNAL (decl) = 0;
2760 DECL_INTERFACE_KNOWN (decl) = 1;
2761 return;
2764 if (import_p)
2765 /* We have already decided what to do with this DECL; there is no
2766 need to check anything further. */
2768 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2770 class_type = DECL_CONTEXT (decl);
2771 import_export_class (class_type);
2772 if (TYPE_FOR_JAVA (class_type))
2773 import_p = true;
2774 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2775 && CLASSTYPE_INTERFACE_ONLY (class_type))
2776 import_p = true;
2777 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2778 && !CLASSTYPE_USE_TEMPLATE (class_type)
2779 && CLASSTYPE_KEY_METHOD (class_type)
2780 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2781 /* The ABI requires that all virtual tables be emitted with
2782 COMDAT linkage. However, on systems where COMDAT symbols
2783 don't show up in the table of contents for a static
2784 archive, or on systems without weak symbols (where we
2785 approximate COMDAT linkage by using internal linkage), the
2786 linker will report errors about undefined symbols because
2787 it will not see the virtual table definition. Therefore,
2788 in the case that we know that the virtual table will be
2789 emitted in only one translation unit, we make the virtual
2790 table an ordinary definition with external linkage. */
2791 DECL_EXTERNAL (decl) = 0;
2792 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2794 /* CLASS_TYPE is being exported from this translation unit,
2795 so DECL should be defined here. */
2796 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2797 /* If a class is declared in a header with the "extern
2798 template" extension, then it will not be instantiated,
2799 even in translation units that would normally require
2800 it. Often such classes are explicitly instantiated in
2801 one translation unit. Therefore, the explicit
2802 instantiation must be made visible to other translation
2803 units. */
2804 DECL_EXTERNAL (decl) = 0;
2805 else
2807 /* The generic C++ ABI says that class data is always
2808 COMDAT, even if there is a key function. Some
2809 variants (e.g., the ARM EABI) says that class data
2810 only has COMDAT linkage if the class data might be
2811 emitted in more than one translation unit. When the
2812 key method can be inline and is inline, we still have
2813 to arrange for comdat even though
2814 class_data_always_comdat is false. */
2815 if (!CLASSTYPE_KEY_METHOD (class_type)
2816 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2817 || targetm.cxx.class_data_always_comdat ())
2819 /* The ABI requires COMDAT linkage. Normally, we
2820 only emit COMDAT things when they are needed;
2821 make sure that we realize that this entity is
2822 indeed needed. */
2823 comdat_p = true;
2824 mark_needed (decl);
2828 else if (!flag_implicit_templates
2829 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2830 import_p = true;
2831 else
2832 comdat_p = true;
2834 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2836 tree type = TREE_TYPE (DECL_NAME (decl));
2837 if (CLASS_TYPE_P (type))
2839 class_type = type;
2840 import_export_class (type);
2841 if (CLASSTYPE_INTERFACE_KNOWN (type)
2842 && TYPE_POLYMORPHIC_P (type)
2843 && CLASSTYPE_INTERFACE_ONLY (type)
2844 /* If -fno-rtti was specified, then we cannot be sure
2845 that RTTI information will be emitted with the
2846 virtual table of the class, so we must emit it
2847 wherever it is used. */
2848 && flag_rtti)
2849 import_p = true;
2850 else
2852 if (CLASSTYPE_INTERFACE_KNOWN (type)
2853 && !CLASSTYPE_INTERFACE_ONLY (type))
2855 comdat_p = (targetm.cxx.class_data_always_comdat ()
2856 || (CLASSTYPE_KEY_METHOD (type)
2857 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2858 mark_needed (decl);
2859 if (!flag_weak)
2861 comdat_p = false;
2862 DECL_EXTERNAL (decl) = 0;
2865 else
2866 comdat_p = true;
2869 else
2870 comdat_p = true;
2872 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2874 /* DECL is an implicit instantiation of a function or static
2875 data member. */
2876 if ((flag_implicit_templates
2877 && !flag_use_repository)
2878 || (flag_implicit_inline_templates
2879 && TREE_CODE (decl) == FUNCTION_DECL
2880 && DECL_DECLARED_INLINE_P (decl)))
2881 comdat_p = true;
2882 else
2883 /* If we are not implicitly generating templates, then mark
2884 this entity as undefined in this translation unit. */
2885 import_p = true;
2887 else if (DECL_FUNCTION_MEMBER_P (decl))
2889 if (!DECL_DECLARED_INLINE_P (decl))
2891 tree ctype = DECL_CONTEXT (decl);
2892 import_export_class (ctype);
2893 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2895 DECL_NOT_REALLY_EXTERN (decl)
2896 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2897 || (DECL_DECLARED_INLINE_P (decl)
2898 && ! flag_implement_inlines
2899 && !DECL_VINDEX (decl)));
2901 if (!DECL_NOT_REALLY_EXTERN (decl))
2902 DECL_EXTERNAL (decl) = 1;
2904 /* Always make artificials weak. */
2905 if (DECL_ARTIFICIAL (decl) && flag_weak)
2906 comdat_p = true;
2907 else
2908 maybe_make_one_only (decl);
2911 else
2912 comdat_p = true;
2914 else
2915 comdat_p = true;
2917 if (import_p)
2919 /* If we are importing DECL into this translation unit, mark is
2920 an undefined here. */
2921 DECL_EXTERNAL (decl) = 1;
2922 DECL_NOT_REALLY_EXTERN (decl) = 0;
2924 else if (comdat_p)
2926 /* If we decided to put DECL in COMDAT, mark it accordingly at
2927 this point. */
2928 comdat_linkage (decl);
2931 DECL_INTERFACE_KNOWN (decl) = 1;
2934 /* Return an expression that performs the destruction of DECL, which
2935 must be a VAR_DECL whose type has a non-trivial destructor, or is
2936 an array whose (innermost) elements have a non-trivial destructor. */
2938 tree
2939 build_cleanup (tree decl)
2941 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2942 gcc_assert (clean != NULL_TREE);
2943 return clean;
2946 /* Returns the initialization guard variable for the variable DECL,
2947 which has static storage duration. */
2949 tree
2950 get_guard (tree decl)
2952 tree sname;
2953 tree guard;
2955 sname = mangle_guard_variable (decl);
2956 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2957 if (! guard)
2959 tree guard_type;
2961 /* We use a type that is big enough to contain a mutex as well
2962 as an integer counter. */
2963 guard_type = targetm.cxx.guard_type ();
2964 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2965 VAR_DECL, sname, guard_type);
2967 /* The guard should have the same linkage as what it guards. */
2968 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2969 TREE_STATIC (guard) = TREE_STATIC (decl);
2970 DECL_COMMON (guard) = DECL_COMMON (decl);
2971 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2972 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2973 if (DECL_ONE_ONLY (decl))
2974 make_decl_one_only (guard, cxx_comdat_group (guard));
2975 if (TREE_PUBLIC (decl))
2976 DECL_WEAK (guard) = DECL_WEAK (decl);
2977 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2978 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2980 DECL_ARTIFICIAL (guard) = 1;
2981 DECL_IGNORED_P (guard) = 1;
2982 TREE_USED (guard) = 1;
2983 pushdecl_top_level_and_finish (guard, NULL_TREE);
2985 return guard;
2988 /* Return those bits of the GUARD variable that should be set when the
2989 guarded entity is actually initialized. */
2991 static tree
2992 get_guard_bits (tree guard)
2994 if (!targetm.cxx.guard_mask_bit ())
2996 /* We only set the first byte of the guard, in order to leave room
2997 for a mutex in the high-order bits. */
2998 guard = build1 (ADDR_EXPR,
2999 build_pointer_type (TREE_TYPE (guard)),
3000 guard);
3001 guard = build1 (NOP_EXPR,
3002 build_pointer_type (char_type_node),
3003 guard);
3004 guard = build1 (INDIRECT_REF, char_type_node, guard);
3007 return guard;
3010 /* Return an expression which determines whether or not the GUARD
3011 variable has already been initialized. */
3013 tree
3014 get_guard_cond (tree guard)
3016 tree guard_value;
3018 /* Check to see if the GUARD is zero. */
3019 guard = get_guard_bits (guard);
3021 /* Mask off all but the low bit. */
3022 if (targetm.cxx.guard_mask_bit ())
3024 guard_value = integer_one_node;
3025 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3026 guard_value = convert (TREE_TYPE (guard), guard_value);
3027 guard = cp_build_binary_op (input_location,
3028 BIT_AND_EXPR, guard, guard_value,
3029 tf_warning_or_error);
3032 guard_value = integer_zero_node;
3033 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3034 guard_value = convert (TREE_TYPE (guard), guard_value);
3035 return cp_build_binary_op (input_location,
3036 EQ_EXPR, guard, guard_value,
3037 tf_warning_or_error);
3040 /* Return an expression which sets the GUARD variable, indicating that
3041 the variable being guarded has been initialized. */
3043 tree
3044 set_guard (tree guard)
3046 tree guard_init;
3048 /* Set the GUARD to one. */
3049 guard = get_guard_bits (guard);
3050 guard_init = integer_one_node;
3051 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3052 guard_init = convert (TREE_TYPE (guard), guard_init);
3053 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3054 tf_warning_or_error);
3057 /* Returns true iff we can tell that VAR does not have a dynamic
3058 initializer. */
3060 static bool
3061 var_defined_without_dynamic_init (tree var)
3063 /* If it's defined in another TU, we can't tell. */
3064 if (DECL_EXTERNAL (var))
3065 return false;
3066 /* If it has a non-trivial destructor, registering the destructor
3067 counts as dynamic initialization. */
3068 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3069 return false;
3070 /* If it's in this TU, its initializer has been processed. */
3071 gcc_assert (DECL_INITIALIZED_P (var));
3072 /* If it has no initializer or a constant one, it's not dynamic. */
3073 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3074 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3077 /* Returns true iff VAR is a variable that needs uses to be
3078 wrapped for possible dynamic initialization. */
3080 static bool
3081 var_needs_tls_wrapper (tree var)
3083 return (!error_operand_p (var)
3084 && DECL_THREAD_LOCAL_P (var)
3085 && !DECL_GNU_TLS_P (var)
3086 && !DECL_FUNCTION_SCOPE_P (var)
3087 && !var_defined_without_dynamic_init (var));
3090 /* Get the FUNCTION_DECL for the shared TLS init function for this
3091 translation unit. */
3093 static tree
3094 get_local_tls_init_fn (void)
3096 tree sname = get_identifier ("__tls_init");
3097 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3098 if (!fn)
3100 fn = build_lang_decl (FUNCTION_DECL, sname,
3101 build_function_type (void_type_node,
3102 void_list_node));
3103 SET_DECL_LANGUAGE (fn, lang_c);
3104 TREE_PUBLIC (fn) = false;
3105 DECL_ARTIFICIAL (fn) = true;
3106 mark_used (fn);
3107 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3109 return fn;
3112 /* Get a FUNCTION_DECL for the init function for the thread_local
3113 variable VAR. The init function will be an alias to the function
3114 that initializes all the non-local TLS variables in the translation
3115 unit. The init function is only used by the wrapper function. */
3117 static tree
3118 get_tls_init_fn (tree var)
3120 /* Only C++11 TLS vars need this init fn. */
3121 if (!var_needs_tls_wrapper (var))
3122 return NULL_TREE;
3124 /* If -fno-extern-tls-init, assume that we don't need to call
3125 a tls init function for a variable defined in another TU. */
3126 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3127 return NULL_TREE;
3129 #ifdef ASM_OUTPUT_DEF
3130 /* If the variable is internal, or if we can't generate aliases,
3131 call the local init function directly. */
3132 if (!TREE_PUBLIC (var))
3133 #endif
3134 return get_local_tls_init_fn ();
3136 tree sname = mangle_tls_init_fn (var);
3137 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3138 if (!fn)
3140 fn = build_lang_decl (FUNCTION_DECL, sname,
3141 build_function_type (void_type_node,
3142 void_list_node));
3143 SET_DECL_LANGUAGE (fn, lang_c);
3144 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3145 DECL_ARTIFICIAL (fn) = true;
3146 DECL_COMDAT (fn) = DECL_COMDAT (var);
3147 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3148 if (DECL_ONE_ONLY (var))
3149 make_decl_one_only (fn, cxx_comdat_group (fn));
3150 if (TREE_PUBLIC (var))
3152 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3153 /* If the variable is defined somewhere else and might have static
3154 initialization, make the init function a weak reference. */
3155 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3156 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3157 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3158 && DECL_EXTERNAL (var))
3159 declare_weak (fn);
3160 else
3161 DECL_WEAK (fn) = DECL_WEAK (var);
3163 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3164 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3165 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3166 DECL_IGNORED_P (fn) = 1;
3167 mark_used (fn);
3169 DECL_BEFRIENDING_CLASSES (fn) = var;
3171 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3173 return fn;
3176 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3177 variable VAR. The wrapper function calls the init function (if any) for
3178 VAR and then returns a reference to VAR. The wrapper function is used
3179 in place of VAR everywhere VAR is mentioned. */
3181 tree
3182 get_tls_wrapper_fn (tree var)
3184 /* Only C++11 TLS vars need this wrapper fn. */
3185 if (!var_needs_tls_wrapper (var))
3186 return NULL_TREE;
3188 tree sname = mangle_tls_wrapper_fn (var);
3189 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3190 if (!fn)
3192 /* A named rvalue reference is an lvalue, so the wrapper should
3193 always return an lvalue reference. */
3194 tree type = non_reference (TREE_TYPE (var));
3195 type = build_reference_type (type);
3196 tree fntype = build_function_type (type, void_list_node);
3197 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3198 SET_DECL_LANGUAGE (fn, lang_c);
3199 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3200 DECL_ARTIFICIAL (fn) = true;
3201 DECL_IGNORED_P (fn) = 1;
3202 /* The wrapper is inline and emitted everywhere var is used. */
3203 DECL_DECLARED_INLINE_P (fn) = true;
3204 if (TREE_PUBLIC (var))
3206 comdat_linkage (fn);
3207 #ifdef HAVE_GAS_HIDDEN
3208 /* Make the wrapper bind locally; there's no reason to share
3209 the wrapper between multiple shared objects. */
3210 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3211 DECL_VISIBILITY_SPECIFIED (fn) = true;
3212 #endif
3214 if (!TREE_PUBLIC (fn))
3215 DECL_INTERFACE_KNOWN (fn) = true;
3216 mark_used (fn);
3217 note_vague_linkage_fn (fn);
3219 #if 0
3220 /* We want CSE to commonize calls to the wrapper, but marking it as
3221 pure is unsafe since it has side-effects. I guess we need a new
3222 ECF flag even weaker than ECF_PURE. FIXME! */
3223 DECL_PURE_P (fn) = true;
3224 #endif
3226 DECL_BEFRIENDING_CLASSES (fn) = var;
3228 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3230 return fn;
3233 /* At EOF, generate the definition for the TLS wrapper function FN:
3235 T& var_wrapper() {
3236 if (init_fn) init_fn();
3237 return var;
3238 } */
3240 static void
3241 generate_tls_wrapper (tree fn)
3243 tree var = DECL_BEFRIENDING_CLASSES (fn);
3245 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3246 tree body = begin_function_body ();
3247 /* Only call the init fn if there might be one. */
3248 if (tree init_fn = get_tls_init_fn (var))
3250 tree if_stmt = NULL_TREE;
3251 /* If init_fn is a weakref, make sure it exists before calling. */
3252 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3254 if_stmt = begin_if_stmt ();
3255 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3256 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3257 NE_EXPR, addr, nullptr_node,
3258 tf_warning_or_error);
3259 finish_if_stmt_cond (cond, if_stmt);
3261 finish_expr_stmt (build_cxx_call
3262 (init_fn, 0, NULL, tf_warning_or_error));
3263 if (if_stmt)
3265 finish_then_clause (if_stmt);
3266 finish_if_stmt (if_stmt);
3269 else
3270 /* If there's no initialization, the wrapper is a constant function. */
3271 TREE_READONLY (fn) = true;
3272 finish_return_stmt (convert_from_reference (var));
3273 finish_function_body (body);
3274 expand_or_defer_fn (finish_function (0));
3277 /* Start the process of running a particular set of global constructors
3278 or destructors. Subroutine of do_[cd]tors. Also called from
3279 vtv_start_verification_constructor_init_function. */
3281 static tree
3282 start_objects (int method_type, int initp)
3284 tree body;
3285 tree fndecl;
3286 char type[14];
3288 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3290 if (initp != DEFAULT_INIT_PRIORITY)
3292 char joiner;
3294 #ifdef JOINER
3295 joiner = JOINER;
3296 #else
3297 joiner = '_';
3298 #endif
3300 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3302 else
3303 sprintf (type, "sub_%c", method_type);
3305 fndecl = build_lang_decl (FUNCTION_DECL,
3306 get_file_function_name (type),
3307 build_function_type_list (void_type_node,
3308 NULL_TREE));
3309 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3311 TREE_PUBLIC (current_function_decl) = 0;
3313 /* Mark as artificial because it's not explicitly in the user's
3314 source code. */
3315 DECL_ARTIFICIAL (current_function_decl) = 1;
3317 /* Mark this declaration as used to avoid spurious warnings. */
3318 TREE_USED (current_function_decl) = 1;
3320 /* Mark this function as a global constructor or destructor. */
3321 if (method_type == 'I')
3322 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3323 else
3324 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3326 body = begin_compound_stmt (BCS_FN_BODY);
3328 return body;
3331 /* Finish the process of running a particular set of global constructors
3332 or destructors. Subroutine of do_[cd]tors. */
3334 static void
3335 finish_objects (int method_type, int initp, tree body)
3337 tree fn;
3339 /* Finish up. */
3340 finish_compound_stmt (body);
3341 fn = finish_function (0);
3343 if (method_type == 'I')
3345 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3346 decl_init_priority_insert (fn, initp);
3348 else
3350 DECL_STATIC_DESTRUCTOR (fn) = 1;
3351 decl_fini_priority_insert (fn, initp);
3354 expand_or_defer_fn (fn);
3357 /* The names of the parameters to the function created to handle
3358 initializations and destructions for objects with static storage
3359 duration. */
3360 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3361 #define PRIORITY_IDENTIFIER "__priority"
3363 /* The name of the function we create to handle initializations and
3364 destructions for objects with static storage duration. */
3365 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3367 /* The declaration for the __INITIALIZE_P argument. */
3368 static GTY(()) tree initialize_p_decl;
3370 /* The declaration for the __PRIORITY argument. */
3371 static GTY(()) tree priority_decl;
3373 /* The declaration for the static storage duration function. */
3374 static GTY(()) tree ssdf_decl;
3376 /* All the static storage duration functions created in this
3377 translation unit. */
3378 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3380 /* A map from priority levels to information about that priority
3381 level. There may be many such levels, so efficient lookup is
3382 important. */
3383 static splay_tree priority_info_map;
3385 /* Begins the generation of the function that will handle all
3386 initialization and destruction of objects with static storage
3387 duration. The function generated takes two parameters of type
3388 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3389 nonzero, it performs initializations. Otherwise, it performs
3390 destructions. It only performs those initializations or
3391 destructions with the indicated __PRIORITY. The generated function
3392 returns no value.
3394 It is assumed that this function will only be called once per
3395 translation unit. */
3397 static tree
3398 start_static_storage_duration_function (unsigned count)
3400 tree type;
3401 tree body;
3402 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3404 /* Create the identifier for this function. It will be of the form
3405 SSDF_IDENTIFIER_<number>. */
3406 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3408 type = build_function_type_list (void_type_node,
3409 integer_type_node, integer_type_node,
3410 NULL_TREE);
3412 /* Create the FUNCTION_DECL itself. */
3413 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3414 get_identifier (id),
3415 type);
3416 TREE_PUBLIC (ssdf_decl) = 0;
3417 DECL_ARTIFICIAL (ssdf_decl) = 1;
3419 /* Put this function in the list of functions to be called from the
3420 static constructors and destructors. */
3421 if (!ssdf_decls)
3423 vec_alloc (ssdf_decls, 32);
3425 /* Take this opportunity to initialize the map from priority
3426 numbers to information about that priority level. */
3427 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3428 /*delete_key_fn=*/0,
3429 /*delete_value_fn=*/
3430 (splay_tree_delete_value_fn) &free);
3432 /* We always need to generate functions for the
3433 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3434 priorities later, we'll be sure to find the
3435 DEFAULT_INIT_PRIORITY. */
3436 get_priority_info (DEFAULT_INIT_PRIORITY);
3439 vec_safe_push (ssdf_decls, ssdf_decl);
3441 /* Create the argument list. */
3442 initialize_p_decl = cp_build_parm_decl
3443 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3444 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3445 TREE_USED (initialize_p_decl) = 1;
3446 priority_decl = cp_build_parm_decl
3447 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3448 DECL_CONTEXT (priority_decl) = ssdf_decl;
3449 TREE_USED (priority_decl) = 1;
3451 DECL_CHAIN (initialize_p_decl) = priority_decl;
3452 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3454 /* Put the function in the global scope. */
3455 pushdecl (ssdf_decl);
3457 /* Start the function itself. This is equivalent to declaring the
3458 function as:
3460 static void __ssdf (int __initialize_p, init __priority_p);
3462 It is static because we only need to call this function from the
3463 various constructor and destructor functions for this module. */
3464 start_preparsed_function (ssdf_decl,
3465 /*attrs=*/NULL_TREE,
3466 SF_PRE_PARSED);
3468 /* Set up the scope of the outermost block in the function. */
3469 body = begin_compound_stmt (BCS_FN_BODY);
3471 return body;
3474 /* Finish the generation of the function which performs initialization
3475 and destruction of objects with static storage duration. After
3476 this point, no more such objects can be created. */
3478 static void
3479 finish_static_storage_duration_function (tree body)
3481 /* Close out the function. */
3482 finish_compound_stmt (body);
3483 expand_or_defer_fn (finish_function (0));
3486 /* Return the information about the indicated PRIORITY level. If no
3487 code to handle this level has yet been generated, generate the
3488 appropriate prologue. */
3490 static priority_info
3491 get_priority_info (int priority)
3493 priority_info pi;
3494 splay_tree_node n;
3496 n = splay_tree_lookup (priority_info_map,
3497 (splay_tree_key) priority);
3498 if (!n)
3500 /* Create a new priority information structure, and insert it
3501 into the map. */
3502 pi = XNEW (struct priority_info_s);
3503 pi->initializations_p = 0;
3504 pi->destructions_p = 0;
3505 splay_tree_insert (priority_info_map,
3506 (splay_tree_key) priority,
3507 (splay_tree_value) pi);
3509 else
3510 pi = (priority_info) n->value;
3512 return pi;
3515 /* The effective initialization priority of a DECL. */
3517 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3518 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3519 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3521 /* Whether a DECL needs a guard to protect it against multiple
3522 initialization. */
3524 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3525 || DECL_ONE_ONLY (decl) \
3526 || DECL_WEAK (decl)))
3528 /* Called from one_static_initialization_or_destruction(),
3529 via walk_tree.
3530 Walks the initializer list of a global variable and looks for
3531 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3532 and that have their DECL_CONTEXT() == NULL.
3533 For each such temporary variable, set their DECL_CONTEXT() to
3534 the current function. This is necessary because otherwise
3535 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3536 when trying to refer to a temporary variable that does not have
3537 it's DECL_CONTECT() properly set. */
3538 static tree
3539 fix_temporary_vars_context_r (tree *node,
3540 int * /*unused*/,
3541 void * /*unused1*/)
3543 gcc_assert (current_function_decl);
3545 if (TREE_CODE (*node) == BIND_EXPR)
3547 tree var;
3549 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3550 if (VAR_P (var)
3551 && !DECL_NAME (var)
3552 && DECL_ARTIFICIAL (var)
3553 && !DECL_CONTEXT (var))
3554 DECL_CONTEXT (var) = current_function_decl;
3557 return NULL_TREE;
3560 /* Set up to handle the initialization or destruction of DECL. If
3561 INITP is nonzero, we are initializing the variable. Otherwise, we
3562 are destroying it. */
3564 static void
3565 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3567 tree guard_if_stmt = NULL_TREE;
3568 tree guard;
3570 /* If we are supposed to destruct and there's a trivial destructor,
3571 nothing has to be done. */
3572 if (!initp
3573 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3574 return;
3576 /* Trick the compiler into thinking we are at the file and line
3577 where DECL was declared so that error-messages make sense, and so
3578 that the debugger will show somewhat sensible file and line
3579 information. */
3580 input_location = DECL_SOURCE_LOCATION (decl);
3582 /* Make sure temporary variables in the initialiser all have
3583 their DECL_CONTEXT() set to a value different from NULL_TREE.
3584 This can happen when global variables initialisers are built.
3585 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3586 the temporary variables that might have been generated in the
3587 accompagning initialisers is NULL_TREE, meaning the variables have been
3588 declared in the global namespace.
3589 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3590 of the temporaries are set to the current function decl. */
3591 cp_walk_tree_without_duplicates (&init,
3592 fix_temporary_vars_context_r,
3593 NULL);
3595 /* Because of:
3597 [class.access.spec]
3599 Access control for implicit calls to the constructors,
3600 the conversion functions, or the destructor called to
3601 create and destroy a static data member is performed as
3602 if these calls appeared in the scope of the member's
3603 class.
3605 we pretend we are in a static member function of the class of
3606 which the DECL is a member. */
3607 if (member_p (decl))
3609 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3610 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3613 /* Assume we don't need a guard. */
3614 guard = NULL_TREE;
3615 /* We need a guard if this is an object with external linkage that
3616 might be initialized in more than one place. (For example, a
3617 static data member of a template, when the data member requires
3618 construction.) */
3619 if (NEEDS_GUARD_P (decl))
3621 tree guard_cond;
3623 guard = get_guard (decl);
3625 /* When using __cxa_atexit, we just check the GUARD as we would
3626 for a local static. */
3627 if (flag_use_cxa_atexit)
3629 /* When using __cxa_atexit, we never try to destroy
3630 anything from a static destructor. */
3631 gcc_assert (initp);
3632 guard_cond = get_guard_cond (guard);
3634 /* If we don't have __cxa_atexit, then we will be running
3635 destructors from .fini sections, or their equivalents. So,
3636 we need to know how many times we've tried to initialize this
3637 object. We do initializations only if the GUARD is zero,
3638 i.e., if we are the first to initialize the variable. We do
3639 destructions only if the GUARD is one, i.e., if we are the
3640 last to destroy the variable. */
3641 else if (initp)
3642 guard_cond
3643 = cp_build_binary_op (input_location,
3644 EQ_EXPR,
3645 cp_build_unary_op (PREINCREMENT_EXPR,
3646 guard,
3647 /*noconvert=*/1,
3648 tf_warning_or_error),
3649 integer_one_node,
3650 tf_warning_or_error);
3651 else
3652 guard_cond
3653 = cp_build_binary_op (input_location,
3654 EQ_EXPR,
3655 cp_build_unary_op (PREDECREMENT_EXPR,
3656 guard,
3657 /*noconvert=*/1,
3658 tf_warning_or_error),
3659 integer_zero_node,
3660 tf_warning_or_error);
3662 guard_if_stmt = begin_if_stmt ();
3663 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3667 /* If we're using __cxa_atexit, we have not already set the GUARD,
3668 so we must do so now. */
3669 if (guard && initp && flag_use_cxa_atexit)
3670 finish_expr_stmt (set_guard (guard));
3672 /* Perform the initialization or destruction. */
3673 if (initp)
3675 if (init)
3677 finish_expr_stmt (init);
3678 if (flag_sanitize & SANITIZE_ADDRESS)
3680 varpool_node *vnode = varpool_node::get (decl);
3681 if (vnode)
3682 vnode->dynamically_initialized = 1;
3686 /* If we're using __cxa_atexit, register a function that calls the
3687 destructor for the object. */
3688 if (flag_use_cxa_atexit)
3689 finish_expr_stmt (register_dtor_fn (decl));
3691 else
3692 finish_expr_stmt (build_cleanup (decl));
3694 /* Finish the guard if-stmt, if necessary. */
3695 if (guard)
3697 finish_then_clause (guard_if_stmt);
3698 finish_if_stmt (guard_if_stmt);
3701 /* Now that we're done with DECL we don't need to pretend to be a
3702 member of its class any longer. */
3703 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3704 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3707 /* Generate code to do the initialization or destruction of the decls in VARS,
3708 a TREE_LIST of VAR_DECL with static storage duration.
3709 Whether initialization or destruction is performed is specified by INITP. */
3711 static void
3712 do_static_initialization_or_destruction (tree vars, bool initp)
3714 tree node, init_if_stmt, cond;
3716 /* Build the outer if-stmt to check for initialization or destruction. */
3717 init_if_stmt = begin_if_stmt ();
3718 cond = initp ? integer_one_node : integer_zero_node;
3719 cond = cp_build_binary_op (input_location,
3720 EQ_EXPR,
3721 initialize_p_decl,
3722 cond,
3723 tf_warning_or_error);
3724 finish_if_stmt_cond (cond, init_if_stmt);
3726 /* To make sure dynamic construction doesn't access globals from other
3727 compilation units where they might not be yet constructed, for
3728 -fsanitize=address insert __asan_before_dynamic_init call that
3729 prevents access to either all global variables that need construction
3730 in other compilation units, or at least those that haven't been
3731 initialized yet. Variables that need dynamic construction in
3732 the current compilation unit are kept accessible. */
3733 if (flag_sanitize & SANITIZE_ADDRESS)
3734 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3736 node = vars;
3737 do {
3738 tree decl = TREE_VALUE (node);
3739 tree priority_if_stmt;
3740 int priority;
3741 priority_info pi;
3743 /* If we don't need a destructor, there's nothing to do. Avoid
3744 creating a possibly empty if-stmt. */
3745 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3747 node = TREE_CHAIN (node);
3748 continue;
3751 /* Remember that we had an initialization or finalization at this
3752 priority. */
3753 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3754 pi = get_priority_info (priority);
3755 if (initp)
3756 pi->initializations_p = 1;
3757 else
3758 pi->destructions_p = 1;
3760 /* Conditionalize this initialization on being in the right priority
3761 and being initializing/finalizing appropriately. */
3762 priority_if_stmt = begin_if_stmt ();
3763 cond = cp_build_binary_op (input_location,
3764 EQ_EXPR,
3765 priority_decl,
3766 build_int_cst (NULL_TREE, priority),
3767 tf_warning_or_error);
3768 finish_if_stmt_cond (cond, priority_if_stmt);
3770 /* Process initializers with same priority. */
3771 for (; node
3772 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3773 node = TREE_CHAIN (node))
3774 /* Do one initialization or destruction. */
3775 one_static_initialization_or_destruction (TREE_VALUE (node),
3776 TREE_PURPOSE (node), initp);
3778 /* Finish up the priority if-stmt body. */
3779 finish_then_clause (priority_if_stmt);
3780 finish_if_stmt (priority_if_stmt);
3782 } while (node);
3784 /* Revert what __asan_before_dynamic_init did by calling
3785 __asan_after_dynamic_init. */
3786 if (flag_sanitize & SANITIZE_ADDRESS)
3787 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3789 /* Finish up the init/destruct if-stmt body. */
3790 finish_then_clause (init_if_stmt);
3791 finish_if_stmt (init_if_stmt);
3794 /* VARS is a list of variables with static storage duration which may
3795 need initialization and/or finalization. Remove those variables
3796 that don't really need to be initialized or finalized, and return
3797 the resulting list. The order in which the variables appear in
3798 VARS is in reverse order of the order in which they should actually
3799 be initialized. The list we return is in the unreversed order;
3800 i.e., the first variable should be initialized first. */
3802 static tree
3803 prune_vars_needing_no_initialization (tree *vars)
3805 tree *var = vars;
3806 tree result = NULL_TREE;
3808 while (*var)
3810 tree t = *var;
3811 tree decl = TREE_VALUE (t);
3812 tree init = TREE_PURPOSE (t);
3814 /* Deal gracefully with error. */
3815 if (decl == error_mark_node)
3817 var = &TREE_CHAIN (t);
3818 continue;
3821 /* The only things that can be initialized are variables. */
3822 gcc_assert (VAR_P (decl));
3824 /* If this object is not defined, we don't need to do anything
3825 here. */
3826 if (DECL_EXTERNAL (decl))
3828 var = &TREE_CHAIN (t);
3829 continue;
3832 /* Also, if the initializer already contains errors, we can bail
3833 out now. */
3834 if (init && TREE_CODE (init) == TREE_LIST
3835 && value_member (error_mark_node, init))
3837 var = &TREE_CHAIN (t);
3838 continue;
3841 /* This variable is going to need initialization and/or
3842 finalization, so we add it to the list. */
3843 *var = TREE_CHAIN (t);
3844 TREE_CHAIN (t) = result;
3845 result = t;
3848 return result;
3851 /* Make sure we have told the back end about all the variables in
3852 VARS. */
3854 static void
3855 write_out_vars (tree vars)
3857 tree v;
3859 for (v = vars; v; v = TREE_CHAIN (v))
3861 tree var = TREE_VALUE (v);
3862 if (!var_finalized_p (var))
3864 import_export_decl (var);
3865 rest_of_decl_compilation (var, 1, 1);
3870 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3871 (otherwise) that will initialize all global objects with static
3872 storage duration having the indicated PRIORITY. */
3874 static void
3875 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3876 location_t *locus)
3878 char function_key;
3879 tree fndecl;
3880 tree body;
3881 size_t i;
3883 input_location = *locus;
3884 /* ??? */
3885 /* Was: locus->line++; */
3887 /* We use `I' to indicate initialization and `D' to indicate
3888 destruction. */
3889 function_key = constructor_p ? 'I' : 'D';
3891 /* We emit the function lazily, to avoid generating empty
3892 global constructors and destructors. */
3893 body = NULL_TREE;
3895 /* For Objective-C++, we may need to initialize metadata found in this module.
3896 This must be done _before_ any other static initializations. */
3897 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3898 && constructor_p && objc_static_init_needed_p ())
3900 body = start_objects (function_key, priority);
3901 objc_generate_static_init_call (NULL_TREE);
3904 /* Call the static storage duration function with appropriate
3905 arguments. */
3906 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3908 /* Calls to pure or const functions will expand to nothing. */
3909 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3911 tree call;
3913 if (! body)
3914 body = start_objects (function_key, priority);
3916 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3917 build_int_cst (NULL_TREE,
3918 constructor_p),
3919 build_int_cst (NULL_TREE,
3920 priority),
3921 NULL_TREE);
3922 finish_expr_stmt (call);
3926 /* Close out the function. */
3927 if (body)
3928 finish_objects (function_key, priority, body);
3931 /* Generate constructor and destructor functions for the priority
3932 indicated by N. */
3934 static int
3935 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3937 location_t *locus = (location_t *) data;
3938 int priority = (int) n->key;
3939 priority_info pi = (priority_info) n->value;
3941 /* Generate the functions themselves, but only if they are really
3942 needed. */
3943 if (pi->initializations_p)
3944 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3945 if (pi->destructions_p)
3946 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3948 /* Keep iterating. */
3949 return 0;
3952 /* Java requires that we be able to reference a local address for a
3953 method, and not be confused by PLT entries. If hidden aliases are
3954 supported, collect and return all the functions for which we should
3955 emit a hidden alias. */
3957 static hash_set<tree> *
3958 collect_candidates_for_java_method_aliases (void)
3960 struct cgraph_node *node;
3961 hash_set<tree> *candidates = NULL;
3963 #ifndef HAVE_GAS_HIDDEN
3964 return candidates;
3965 #endif
3967 FOR_EACH_FUNCTION (node)
3969 tree fndecl = node->decl;
3971 if (DECL_CLASS_SCOPE_P (fndecl)
3972 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3973 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3975 if (candidates == NULL)
3976 candidates = new hash_set<tree>;
3977 candidates->add (fndecl);
3981 return candidates;
3985 /* Java requires that we be able to reference a local address for a
3986 method, and not be confused by PLT entries. If hidden aliases are
3987 supported, emit one for each java function that we've emitted.
3988 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3989 by collect_candidates_for_java_method_aliases. */
3991 static void
3992 build_java_method_aliases (hash_set<tree> *candidates)
3994 struct cgraph_node *node;
3996 #ifndef HAVE_GAS_HIDDEN
3997 return;
3998 #endif
4000 FOR_EACH_FUNCTION (node)
4002 tree fndecl = node->decl;
4004 if (TREE_ASM_WRITTEN (fndecl)
4005 && candidates->contains (fndecl))
4007 /* Mangle the name in a predictable way; we need to reference
4008 this from a java compiled object file. */
4009 tree oid, nid, alias;
4010 const char *oname;
4011 char *nname;
4013 oid = DECL_ASSEMBLER_NAME (fndecl);
4014 oname = IDENTIFIER_POINTER (oid);
4015 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4016 nname = ACONCAT (("_ZGA", oname+2, NULL));
4017 nid = get_identifier (nname);
4019 alias = make_alias_for (fndecl, nid);
4020 TREE_PUBLIC (alias) = 1;
4021 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4023 assemble_alias (alias, oid);
4028 /* Return C++ property of T, based on given operation OP. */
4030 static int
4031 cpp_check (tree t, cpp_operation op)
4033 switch (op)
4035 case IS_ABSTRACT:
4036 return DECL_PURE_VIRTUAL_P (t);
4037 case IS_CONSTRUCTOR:
4038 return DECL_CONSTRUCTOR_P (t);
4039 case IS_DESTRUCTOR:
4040 return DECL_DESTRUCTOR_P (t);
4041 case IS_COPY_CONSTRUCTOR:
4042 return DECL_COPY_CONSTRUCTOR_P (t);
4043 case IS_TEMPLATE:
4044 return TREE_CODE (t) == TEMPLATE_DECL;
4045 case IS_TRIVIAL:
4046 return trivial_type_p (t);
4047 default:
4048 return 0;
4052 /* Collect source file references recursively, starting from NAMESPC. */
4054 static void
4055 collect_source_refs (tree namespc)
4057 tree t;
4059 if (!namespc)
4060 return;
4062 /* Iterate over names in this name space. */
4063 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4064 if (!DECL_IS_BUILTIN (t) )
4065 collect_source_ref (DECL_SOURCE_FILE (t));
4067 /* Dump siblings, if any */
4068 collect_source_refs (TREE_CHAIN (namespc));
4070 /* Dump children, if any */
4071 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4074 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4075 starting from NAMESPC. */
4077 static void
4078 collect_ada_namespace (tree namespc, const char *source_file)
4080 if (!namespc)
4081 return;
4083 /* Collect decls from this namespace */
4084 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4086 /* Collect siblings, if any */
4087 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4089 /* Collect children, if any */
4090 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4093 /* Returns true iff there is a definition available for variable or
4094 function DECL. */
4096 static bool
4097 decl_defined_p (tree decl)
4099 if (TREE_CODE (decl) == FUNCTION_DECL)
4100 return (DECL_INITIAL (decl) != NULL_TREE);
4101 else
4103 gcc_assert (VAR_P (decl));
4104 return !DECL_EXTERNAL (decl);
4108 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4110 [expr.const]
4112 An integral constant-expression can only involve ... const
4113 variables of integral or enumeration types initialized with
4114 constant expressions ...
4116 C++0x also allows constexpr variables and temporaries initialized
4117 with constant expressions. We handle the former here, but the latter
4118 are just folded away in cxx_eval_constant_expression.
4120 The standard does not require that the expression be non-volatile.
4121 G++ implements the proposed correction in DR 457. */
4123 bool
4124 decl_constant_var_p (tree decl)
4126 if (!decl_maybe_constant_var_p (decl))
4127 return false;
4129 /* We don't know if a template static data member is initialized with
4130 a constant expression until we instantiate its initializer. Even
4131 in the case of a constexpr variable, we can't treat it as a
4132 constant until its initializer is complete in case it's used in
4133 its own initializer. */
4134 mark_used (decl);
4135 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4138 /* Returns true if DECL could be a symbolic constant variable, depending on
4139 its initializer. */
4141 bool
4142 decl_maybe_constant_var_p (tree decl)
4144 tree type = TREE_TYPE (decl);
4145 if (!VAR_P (decl))
4146 return false;
4147 if (DECL_DECLARED_CONSTEXPR_P (decl))
4148 return true;
4149 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4150 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4153 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4154 called from grokfndecl and grokvardecl; in all modes it is called from
4155 cp_write_global_declarations. */
4157 void
4158 no_linkage_error (tree decl)
4160 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4161 /* In C++11 it's ok if the decl is defined. */
4162 return;
4163 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4164 if (t == NULL_TREE)
4165 /* The type that got us on no_linkage_decls must have gotten a name for
4166 linkage purposes. */;
4167 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4168 /* The type might end up having a typedef name for linkage purposes. */
4169 vec_safe_push (no_linkage_decls, decl);
4170 else if (TYPE_ANONYMOUS_P (t))
4172 bool d = false;
4173 if (cxx_dialect >= cxx11)
4174 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4175 "anonymous type, is used but never defined", decl);
4176 else if (DECL_EXTERN_C_P (decl))
4177 /* Allow this; it's pretty common in C. */;
4178 else if (TREE_CODE (decl) == VAR_DECL)
4179 /* DRs 132, 319 and 389 seem to indicate types with
4180 no linkage can only be used to declare extern "C"
4181 entities. Since it's not always an error in the
4182 ISO C++ 90 Standard, we only issue a warning. */
4183 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4184 "with no linkage used to declare variable %q#D with "
4185 "linkage", decl);
4186 else
4187 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4188 "linkage used to declare function %q#D with linkage",
4189 decl);
4190 if (d && is_typedef_decl (TYPE_NAME (t)))
4191 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4192 "to the unqualified type, so it is not used for linkage",
4193 TYPE_NAME (t));
4195 else if (cxx_dialect >= cxx11)
4196 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4197 "%qT, is used but never defined", decl, t);
4198 else if (TREE_CODE (decl) == VAR_DECL)
4199 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4200 "used to declare variable %q#D with linkage", t, decl);
4201 else
4202 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4203 "to declare function %q#D with linkage", t, decl);
4206 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4208 static void
4209 collect_all_refs (const char *source_file)
4211 collect_ada_namespace (global_namespace, source_file);
4214 /* Clear DECL_EXTERNAL for NODE. */
4216 static bool
4217 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4219 DECL_EXTERNAL (node->decl) = 0;
4220 return false;
4223 /* Build up the function to run dynamic initializers for thread_local
4224 variables in this translation unit and alias the init functions for the
4225 individual variables to it. */
4227 static void
4228 handle_tls_init (void)
4230 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4231 if (vars == NULL_TREE)
4232 return;
4234 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4236 write_out_vars (vars);
4238 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4239 boolean_type_node);
4240 TREE_PUBLIC (guard) = false;
4241 TREE_STATIC (guard) = true;
4242 DECL_ARTIFICIAL (guard) = true;
4243 DECL_IGNORED_P (guard) = true;
4244 TREE_USED (guard) = true;
4245 set_decl_tls_model (guard, decl_default_tls_model (guard));
4246 pushdecl_top_level_and_finish (guard, NULL_TREE);
4248 tree fn = get_local_tls_init_fn ();
4249 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4250 tree body = begin_function_body ();
4251 tree if_stmt = begin_if_stmt ();
4252 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4253 tf_warning_or_error);
4254 finish_if_stmt_cond (cond, if_stmt);
4255 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4256 tf_warning_or_error));
4257 for (; vars; vars = TREE_CHAIN (vars))
4259 tree var = TREE_VALUE (vars);
4260 tree init = TREE_PURPOSE (vars);
4261 one_static_initialization_or_destruction (var, init, true);
4263 #ifdef ASM_OUTPUT_DEF
4264 /* Output init aliases even with -fno-extern-tls-init. */
4265 if (TREE_PUBLIC (var))
4267 tree single_init_fn = get_tls_init_fn (var);
4268 if (single_init_fn == NULL_TREE)
4269 continue;
4270 cgraph_node *alias
4271 = cgraph_node::get_create (fn)->create_same_body_alias
4272 (single_init_fn, fn);
4273 gcc_assert (alias != NULL);
4275 #endif
4278 finish_then_clause (if_stmt);
4279 finish_if_stmt (if_stmt);
4280 finish_function_body (body);
4281 expand_or_defer_fn (finish_function (0));
4284 /* The entire file is now complete. If requested, dump everything
4285 to a file. */
4287 static void
4288 dump_tu (void)
4290 int flags;
4291 FILE *stream = dump_begin (TDI_tu, &flags);
4293 if (stream)
4295 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4296 dump_end (TDI_tu, stream);
4300 /* This routine is called at the end of compilation.
4301 Its job is to create all the code needed to initialize and
4302 destroy the global aggregates. We do the destruction
4303 first, since that way we only need to reverse the decls once. */
4305 void
4306 cp_write_global_declarations (void)
4308 tree vars;
4309 bool reconsider;
4310 size_t i;
4311 location_t locus;
4312 unsigned ssdf_count = 0;
4313 int retries = 0;
4314 tree decl;
4315 hash_set<tree> *candidates;
4317 locus = input_location;
4318 at_eof = 1;
4320 /* Bad parse errors. Just forget about it. */
4321 if (! global_bindings_p () || current_class_type
4322 || !vec_safe_is_empty (decl_namespace_list))
4323 return;
4325 /* This is the point to write out a PCH if we're doing that.
4326 In that case we do not want to do anything else. */
4327 if (pch_file)
4329 c_common_write_pch ();
4330 dump_tu ();
4331 return;
4334 symtab->process_same_body_aliases ();
4336 /* Handle -fdump-ada-spec[-slim] */
4337 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4339 if (flag_dump_ada_spec_slim)
4340 collect_source_ref (main_input_filename);
4341 else
4342 collect_source_refs (global_namespace);
4344 dump_ada_specs (collect_all_refs, cpp_check);
4347 /* FIXME - huh? was input_line -= 1;*/
4349 timevar_start (TV_PHASE_DEFERRED);
4351 /* We now have to write out all the stuff we put off writing out.
4352 These include:
4354 o Template specializations that we have not yet instantiated,
4355 but which are needed.
4356 o Initialization and destruction for non-local objects with
4357 static storage duration. (Local objects with static storage
4358 duration are initialized when their scope is first entered,
4359 and are cleaned up via atexit.)
4360 o Virtual function tables.
4362 All of these may cause others to be needed. For example,
4363 instantiating one function may cause another to be needed, and
4364 generating the initializer for an object may cause templates to be
4365 instantiated, etc., etc. */
4367 emit_support_tinfos ();
4371 tree t;
4372 tree decl;
4374 reconsider = false;
4376 /* If there are templates that we've put off instantiating, do
4377 them now. */
4378 instantiate_pending_templates (retries);
4379 ggc_collect ();
4381 /* Write out virtual tables as required. Note that writing out
4382 the virtual table for a template class may cause the
4383 instantiation of members of that class. If we write out
4384 vtables then we remove the class from our list so we don't
4385 have to look at it again. */
4387 while (keyed_classes != NULL_TREE
4388 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4390 reconsider = true;
4391 keyed_classes = TREE_CHAIN (keyed_classes);
4394 t = keyed_classes;
4395 if (t != NULL_TREE)
4397 tree next = TREE_CHAIN (t);
4399 while (next)
4401 if (maybe_emit_vtables (TREE_VALUE (next)))
4403 reconsider = true;
4404 TREE_CHAIN (t) = TREE_CHAIN (next);
4406 else
4407 t = next;
4409 next = TREE_CHAIN (t);
4413 /* Write out needed type info variables. We have to be careful
4414 looping through unemitted decls, because emit_tinfo_decl may
4415 cause other variables to be needed. New elements will be
4416 appended, and we remove from the vector those that actually
4417 get emitted. */
4418 for (i = unemitted_tinfo_decls->length ();
4419 unemitted_tinfo_decls->iterate (--i, &t);)
4420 if (emit_tinfo_decl (t))
4422 reconsider = true;
4423 unemitted_tinfo_decls->unordered_remove (i);
4426 /* The list of objects with static storage duration is built up
4427 in reverse order. We clear STATIC_AGGREGATES so that any new
4428 aggregates added during the initialization of these will be
4429 initialized in the correct order when we next come around the
4430 loop. */
4431 vars = prune_vars_needing_no_initialization (&static_aggregates);
4433 if (vars)
4435 /* We need to start a new initialization function each time
4436 through the loop. That's because we need to know which
4437 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4438 isn't computed until a function is finished, and written
4439 out. That's a deficiency in the back end. When this is
4440 fixed, these initialization functions could all become
4441 inline, with resulting performance improvements. */
4442 tree ssdf_body;
4444 /* Set the line and file, so that it is obviously not from
4445 the source file. */
4446 input_location = locus;
4447 ssdf_body = start_static_storage_duration_function (ssdf_count);
4449 /* Make sure the back end knows about all the variables. */
4450 write_out_vars (vars);
4452 /* First generate code to do all the initializations. */
4453 if (vars)
4454 do_static_initialization_or_destruction (vars, /*initp=*/true);
4456 /* Then, generate code to do all the destructions. Do these
4457 in reverse order so that the most recently constructed
4458 variable is the first destroyed. If we're using
4459 __cxa_atexit, then we don't need to do this; functions
4460 were registered at initialization time to destroy the
4461 local statics. */
4462 if (!flag_use_cxa_atexit && vars)
4464 vars = nreverse (vars);
4465 do_static_initialization_or_destruction (vars, /*initp=*/false);
4467 else
4468 vars = NULL_TREE;
4470 /* Finish up the static storage duration function for this
4471 round. */
4472 input_location = locus;
4473 finish_static_storage_duration_function (ssdf_body);
4475 /* All those initializations and finalizations might cause
4476 us to need more inline functions, more template
4477 instantiations, etc. */
4478 reconsider = true;
4479 ssdf_count++;
4480 /* ??? was: locus.line++; */
4483 /* Now do the same for thread_local variables. */
4484 handle_tls_init ();
4486 /* Go through the set of inline functions whose bodies have not
4487 been emitted yet. If out-of-line copies of these functions
4488 are required, emit them. */
4489 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4491 /* Does it need synthesizing? */
4492 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4493 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4495 /* Even though we're already at the top-level, we push
4496 there again. That way, when we pop back a few lines
4497 hence, all of our state is restored. Otherwise,
4498 finish_function doesn't clean things up, and we end
4499 up with CURRENT_FUNCTION_DECL set. */
4500 push_to_top_level ();
4501 /* The decl's location will mark where it was first
4502 needed. Save that so synthesize method can indicate
4503 where it was needed from, in case of error */
4504 input_location = DECL_SOURCE_LOCATION (decl);
4505 synthesize_method (decl);
4506 pop_from_top_level ();
4507 reconsider = true;
4510 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4511 generate_tls_wrapper (decl);
4513 if (!DECL_SAVED_TREE (decl))
4514 continue;
4516 /* We lie to the back end, pretending that some functions
4517 are not defined when they really are. This keeps these
4518 functions from being put out unnecessarily. But, we must
4519 stop lying when the functions are referenced, or if they
4520 are not comdat since they need to be put out now. If
4521 DECL_INTERFACE_KNOWN, then we have already set
4522 DECL_EXTERNAL appropriately, so there's no need to check
4523 again, and we do not want to clear DECL_EXTERNAL if a
4524 previous call to import_export_decl set it.
4526 This is done in a separate for cycle, because if some
4527 deferred function is contained in another deferred
4528 function later in deferred_fns varray,
4529 rest_of_compilation would skip this function and we
4530 really cannot expand the same function twice. */
4531 import_export_decl (decl);
4532 if (DECL_NOT_REALLY_EXTERN (decl)
4533 && DECL_INITIAL (decl)
4534 && decl_needed_p (decl))
4536 struct cgraph_node *node, *next;
4538 node = cgraph_node::get (decl);
4539 if (node->cpp_implicit_alias)
4540 node = node->get_alias_target ();
4542 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4543 NULL, true);
4544 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4545 group, we need to mark all symbols in the same comdat group
4546 that way. */
4547 if (node->same_comdat_group)
4548 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4549 next != node;
4550 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4551 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4552 NULL, true);
4555 /* If we're going to need to write this function out, and
4556 there's already a body for it, create RTL for it now.
4557 (There might be no body if this is a method we haven't
4558 gotten around to synthesizing yet.) */
4559 if (!DECL_EXTERNAL (decl)
4560 && decl_needed_p (decl)
4561 && !TREE_ASM_WRITTEN (decl)
4562 && !cgraph_node::get (decl)->definition)
4564 /* We will output the function; no longer consider it in this
4565 loop. */
4566 DECL_DEFER_OUTPUT (decl) = 0;
4567 /* Generate RTL for this function now that we know we
4568 need it. */
4569 expand_or_defer_fn (decl);
4570 /* If we're compiling -fsyntax-only pretend that this
4571 function has been written out so that we don't try to
4572 expand it again. */
4573 if (flag_syntax_only)
4574 TREE_ASM_WRITTEN (decl) = 1;
4575 reconsider = true;
4579 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4580 reconsider = true;
4582 /* Static data members are just like namespace-scope globals. */
4583 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4585 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4586 /* Don't write it out if we haven't seen a definition. */
4587 || DECL_IN_AGGR_P (decl))
4588 continue;
4589 import_export_decl (decl);
4590 /* If this static data member is needed, provide it to the
4591 back end. */
4592 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4593 DECL_EXTERNAL (decl) = 0;
4595 if (vec_safe_length (pending_statics) != 0
4596 && wrapup_global_declarations (pending_statics->address (),
4597 pending_statics->length ()))
4598 reconsider = true;
4600 retries++;
4602 while (reconsider);
4604 /* All used inline functions must have a definition at this point. */
4605 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4607 if (/* Check online inline functions that were actually used. */
4608 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4609 /* If the definition actually was available here, then the
4610 fact that the function was not defined merely represents
4611 that for some reason (use of a template repository,
4612 #pragma interface, etc.) we decided not to emit the
4613 definition here. */
4614 && !DECL_INITIAL (decl)
4615 /* Don't complain if the template was defined. */
4616 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4617 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4618 (template_for_substitution (decl)))))
4620 warning (0, "inline function %q+D used but never defined", decl);
4621 /* Avoid a duplicate warning from check_global_declaration_1. */
4622 TREE_NO_WARNING (decl) = 1;
4626 /* So must decls that use a type with no linkage. */
4627 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4628 no_linkage_error (decl);
4630 /* Then, do the Objective-C stuff. This is where all the
4631 Objective-C module stuff gets generated (symtab,
4632 class/protocol/selector lists etc). This must be done after C++
4633 templates, destructors etc. so that selectors used in C++
4634 templates are properly allocated. */
4635 if (c_dialect_objc ())
4636 objc_write_global_declarations ();
4638 /* We give C linkage to static constructors and destructors. */
4639 push_lang_context (lang_name_c);
4641 /* Generate initialization and destruction functions for all
4642 priorities for which they are required. */
4643 if (priority_info_map)
4644 splay_tree_foreach (priority_info_map,
4645 generate_ctor_and_dtor_functions_for_priority,
4646 /*data=*/&locus);
4647 else if (c_dialect_objc () && objc_static_init_needed_p ())
4648 /* If this is obj-c++ and we need a static init, call
4649 generate_ctor_or_dtor_function. */
4650 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4651 DEFAULT_INIT_PRIORITY, &locus);
4653 /* We're done with the splay-tree now. */
4654 if (priority_info_map)
4655 splay_tree_delete (priority_info_map);
4657 /* Generate any missing aliases. */
4658 maybe_apply_pending_pragma_weaks ();
4660 /* We're done with static constructors, so we can go back to "C++"
4661 linkage now. */
4662 pop_lang_context ();
4664 /* Collect candidates for Java hidden aliases. */
4665 candidates = collect_candidates_for_java_method_aliases ();
4667 timevar_stop (TV_PHASE_DEFERRED);
4668 timevar_start (TV_PHASE_OPT_GEN);
4670 if (flag_vtable_verify)
4672 vtv_recover_class_info ();
4673 vtv_compute_class_hierarchy_transitive_closure ();
4674 vtv_build_vtable_verify_fndecl ();
4677 symtab->finalize_compilation_unit ();
4679 if (flag_vtable_verify)
4681 /* Generate the special constructor initialization function that
4682 calls __VLTRegisterPairs, and give it a very high
4683 initialization priority. This must be done after
4684 finalize_compilation_unit so that we have accurate
4685 information about which vtable will actually be emitted. */
4686 vtv_generate_init_routine ();
4689 timevar_stop (TV_PHASE_OPT_GEN);
4690 timevar_start (TV_PHASE_CHECK_DBGINFO);
4692 /* Now, issue warnings about static, but not defined, functions,
4693 etc., and emit debugging information. */
4694 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4695 if (vec_safe_length (pending_statics) != 0)
4697 check_global_declarations (pending_statics->address (),
4698 pending_statics->length ());
4699 emit_debug_global_declarations (pending_statics->address (),
4700 pending_statics->length ());
4703 perform_deferred_noexcept_checks ();
4705 /* Generate hidden aliases for Java. */
4706 if (candidates)
4708 build_java_method_aliases (candidates);
4709 delete candidates;
4712 finish_repo ();
4714 /* The entire file is now complete. If requested, dump everything
4715 to a file. */
4716 dump_tu ();
4718 if (flag_detailed_statistics)
4720 dump_tree_statistics ();
4721 dump_time_statistics ();
4723 input_location = locus;
4725 #ifdef ENABLE_CHECKING
4726 validate_conversion_obstack ();
4727 #endif /* ENABLE_CHECKING */
4729 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4732 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4733 function to call in parse-tree form; it has not yet been
4734 semantically analyzed. ARGS are the arguments to the function.
4735 They have already been semantically analyzed. This may change
4736 ARGS. */
4738 tree
4739 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4740 tsubst_flags_t complain)
4742 tree orig_fn;
4743 vec<tree, va_gc> *orig_args = NULL;
4744 tree expr;
4745 tree object;
4747 orig_fn = fn;
4748 object = TREE_OPERAND (fn, 0);
4750 if (processing_template_decl)
4752 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4753 || TREE_CODE (fn) == MEMBER_REF);
4754 if (type_dependent_expression_p (fn)
4755 || any_type_dependent_arguments_p (*args))
4756 return build_nt_call_vec (fn, *args);
4758 orig_args = make_tree_vector_copy (*args);
4760 /* Transform the arguments and add the implicit "this"
4761 parameter. That must be done before the FN is transformed
4762 because we depend on the form of FN. */
4763 make_args_non_dependent (*args);
4764 object = build_non_dependent_expr (object);
4765 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4767 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4768 object = cp_build_addr_expr (object, complain);
4769 vec_safe_insert (*args, 0, object);
4771 /* Now that the arguments are done, transform FN. */
4772 fn = build_non_dependent_expr (fn);
4775 /* A qualified name corresponding to a bound pointer-to-member is
4776 represented as an OFFSET_REF:
4778 struct B { void g(); };
4779 void (B::*p)();
4780 void B::g() { (this->*p)(); } */
4781 if (TREE_CODE (fn) == OFFSET_REF)
4783 tree object_addr = cp_build_addr_expr (object, complain);
4784 fn = TREE_OPERAND (fn, 1);
4785 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4786 complain);
4787 vec_safe_insert (*args, 0, object_addr);
4790 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4791 expr = build_op_call (fn, args, complain);
4792 else
4793 expr = cp_build_function_call_vec (fn, args, complain);
4794 if (processing_template_decl && expr != error_mark_node)
4795 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4797 if (orig_args != NULL)
4798 release_tree_vector (orig_args);
4800 return expr;
4804 void
4805 check_default_args (tree x)
4807 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4808 bool saw_def = false;
4809 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4810 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4812 if (TREE_PURPOSE (arg))
4813 saw_def = true;
4814 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4816 error ("default argument missing for parameter %P of %q+#D", i, x);
4817 TREE_PURPOSE (arg) = error_mark_node;
4822 /* Return true if function DECL can be inlined. This is used to force
4823 instantiation of methods that might be interesting for inlining. */
4824 bool
4825 possibly_inlined_p (tree decl)
4827 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4828 if (DECL_UNINLINABLE (decl))
4829 return false;
4830 if (!optimize || pragma_java_exceptions)
4831 return DECL_DECLARED_INLINE_P (decl);
4832 /* When optimizing, we might inline everything when flatten
4833 attribute or heuristics inlining for size or autoinlining
4834 is used. */
4835 return true;
4838 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4839 If DECL is a specialization or implicitly declared class member,
4840 generate the actual definition. Return false if something goes
4841 wrong, true otherwise. */
4843 bool
4844 mark_used (tree decl, tsubst_flags_t complain)
4846 /* If DECL is a BASELINK for a single function, then treat it just
4847 like the DECL for the function. Otherwise, if the BASELINK is
4848 for an overloaded function, we don't know which function was
4849 actually used until after overload resolution. */
4850 if (BASELINK_P (decl))
4852 decl = BASELINK_FUNCTIONS (decl);
4853 if (really_overloaded_fn (decl))
4854 return true;
4855 decl = OVL_CURRENT (decl);
4858 /* Set TREE_USED for the benefit of -Wunused. */
4859 TREE_USED (decl) = 1;
4860 if (DECL_CLONED_FUNCTION_P (decl))
4861 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4863 /* Mark enumeration types as used. */
4864 if (TREE_CODE (decl) == CONST_DECL)
4865 used_types_insert (DECL_CONTEXT (decl));
4867 if (TREE_CODE (decl) == FUNCTION_DECL)
4868 maybe_instantiate_noexcept (decl);
4870 if (TREE_CODE (decl) == FUNCTION_DECL
4871 && DECL_DELETED_FN (decl))
4873 if (DECL_ARTIFICIAL (decl))
4875 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4876 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4878 /* We mark a lambda conversion op as deleted if we can't
4879 generate it properly; see maybe_add_lambda_conv_op. */
4880 sorry ("converting lambda which uses %<...%> to "
4881 "function pointer");
4882 return false;
4885 if (complain & tf_error)
4887 error ("use of deleted function %qD", decl);
4888 if (!maybe_explain_implicit_delete (decl))
4889 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4891 return false;
4894 /* We can only check DECL_ODR_USED on variables or functions with
4895 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4896 might need special handling for. */
4897 if (!VAR_OR_FUNCTION_DECL_P (decl)
4898 || DECL_LANG_SPECIFIC (decl) == NULL
4899 || DECL_THUNK_P (decl))
4901 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4903 if (complain & tf_error)
4904 error ("use of %qD before deduction of %<auto%>", decl);
4905 return false;
4907 return true;
4910 /* We only want to do this processing once. We don't need to keep trying
4911 to instantiate inline templates, because unit-at-a-time will make sure
4912 we get them compiled before functions that want to inline them. */
4913 if (DECL_ODR_USED (decl))
4914 return true;
4916 /* If within finish_function, defer the rest until that function
4917 finishes, otherwise it might recurse. */
4918 if (defer_mark_used_calls)
4920 vec_safe_push (deferred_mark_used_calls, decl);
4921 return true;
4924 /* Normally, we can wait until instantiation-time to synthesize DECL.
4925 However, if DECL is a static data member initialized with a constant
4926 or a constexpr function, we need it right now because a reference to
4927 such a data member or a call to such function is not value-dependent.
4928 For a function that uses auto in the return type, we need to instantiate
4929 it to find out its type. For OpenMP user defined reductions, we need
4930 them instantiated for reduction clauses which inline them by hand
4931 directly. */
4932 if (DECL_LANG_SPECIFIC (decl)
4933 && DECL_TEMPLATE_INFO (decl)
4934 && (decl_maybe_constant_var_p (decl)
4935 || (TREE_CODE (decl) == FUNCTION_DECL
4936 && (DECL_DECLARED_CONSTEXPR_P (decl)
4937 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4938 || undeduced_auto_decl (decl))
4939 && !uses_template_parms (DECL_TI_ARGS (decl)))
4941 /* Instantiating a function will result in garbage collection. We
4942 must treat this situation as if we were within the body of a
4943 function so as to avoid collecting live data only referenced from
4944 the stack (such as overload resolution candidates). */
4945 ++function_depth;
4946 instantiate_decl (decl, /*defer_ok=*/false,
4947 /*expl_inst_class_mem_p=*/false);
4948 --function_depth;
4951 if (processing_template_decl)
4952 return true;
4954 /* Check this too in case we're within fold_non_dependent_expr. */
4955 if (DECL_TEMPLATE_INFO (decl)
4956 && uses_template_parms (DECL_TI_ARGS (decl)))
4957 return true;
4959 require_deduced_type (decl);
4961 /* If we don't need a value, then we don't need to synthesize DECL. */
4962 if (cp_unevaluated_operand != 0)
4963 return true;
4965 DECL_ODR_USED (decl) = 1;
4966 if (DECL_CLONED_FUNCTION_P (decl))
4967 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4969 /* DR 757: A type without linkage shall not be used as the type of a
4970 variable or function with linkage, unless
4971 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4972 o the variable or function is not used (3.2 [basic.def.odr]) or is
4973 defined in the same translation unit. */
4974 if (cxx_dialect > cxx98
4975 && decl_linkage (decl) != lk_none
4976 && !DECL_EXTERN_C_P (decl)
4977 && !DECL_ARTIFICIAL (decl)
4978 && !decl_defined_p (decl)
4979 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4981 if (is_local_extern (decl))
4982 /* There's no way to define a local extern, and adding it to
4983 the vector interferes with GC, so give an error now. */
4984 no_linkage_error (decl);
4985 else
4986 vec_safe_push (no_linkage_decls, decl);
4989 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4990 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4991 /* Remember it, so we can check it was defined. */
4992 note_vague_linkage_fn (decl);
4994 /* Is it a synthesized method that needs to be synthesized? */
4995 if (TREE_CODE (decl) == FUNCTION_DECL
4996 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4997 && DECL_DEFAULTED_FN (decl)
4998 /* A function defaulted outside the class is synthesized either by
4999 cp_finish_decl or instantiate_decl. */
5000 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5001 && ! DECL_INITIAL (decl))
5003 /* Defer virtual destructors so that thunks get the right
5004 linkage. */
5005 if (DECL_VIRTUAL_P (decl) && !at_eof)
5007 note_vague_linkage_fn (decl);
5008 return true;
5011 /* Remember the current location for a function we will end up
5012 synthesizing. Then we can inform the user where it was
5013 required in the case of error. */
5014 DECL_SOURCE_LOCATION (decl) = input_location;
5016 /* Synthesizing an implicitly defined member function will result in
5017 garbage collection. We must treat this situation as if we were
5018 within the body of a function so as to avoid collecting live data
5019 on the stack (such as overload resolution candidates).
5021 We could just let cp_write_global_declarations handle synthesizing
5022 this function by adding it to deferred_fns, but doing
5023 it at the use site produces better error messages. */
5024 ++function_depth;
5025 synthesize_method (decl);
5026 --function_depth;
5027 /* If this is a synthesized method we don't need to
5028 do the instantiation test below. */
5030 else if (VAR_OR_FUNCTION_DECL_P (decl)
5031 && DECL_TEMPLATE_INFO (decl)
5032 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5033 || always_instantiate_p (decl)))
5034 /* If this is a function or variable that is an instance of some
5035 template, we now know that we will need to actually do the
5036 instantiation. We check that DECL is not an explicit
5037 instantiation because that is not checked in instantiate_decl.
5039 We put off instantiating functions in order to improve compile
5040 times. Maintaining a stack of active functions is expensive,
5041 and the inliner knows to instantiate any functions it might
5042 need. Therefore, we always try to defer instantiation. */
5044 ++function_depth;
5045 instantiate_decl (decl, /*defer_ok=*/true,
5046 /*expl_inst_class_mem_p=*/false);
5047 --function_depth;
5050 return true;
5053 bool
5054 mark_used (tree decl)
5056 return mark_used (decl, tf_warning_or_error);
5059 tree
5060 vtv_start_verification_constructor_init_function (void)
5062 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5065 tree
5066 vtv_finish_verification_constructor_init_function (tree function_body)
5068 tree fn;
5070 finish_compound_stmt (function_body);
5071 fn = finish_function (0);
5072 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5073 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5075 return fn;
5078 #include "gt-cp-decl2.h"