PR target/66563
[official-gcc.git] / gcc / cp / decl2.c
blobe24d2d66c078ad701396ba3f9d5df3a7ce679aea
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "tree.h"
36 #include "stringpool.h"
37 #include "varasm.h"
38 #include "attribs.h"
39 #include "stor-layout.h"
40 #include "calls.h"
41 #include "flags.h"
42 #include "cp-tree.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "timevar.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "c-family/c-common.h"
49 #include "c-family/c-objc.h"
50 #include "plugin-api.h"
51 #include "hard-reg-set.h"
52 #include "function.h"
53 #include "ipa-ref.h"
54 #include "cgraph.h"
55 #include "tree-inline.h"
56 #include "c-family/c-pragma.h"
57 #include "dumpfile.h"
58 #include "intl.h"
59 #include "splay-tree.h"
60 #include "langhooks.h"
61 #include "c-family/c-ada-spec.h"
62 #include "asan.h"
64 extern cpp_reader *parse_in;
66 /* This structure contains information about the initializations
67 and/or destructions required for a particular priority level. */
68 typedef struct priority_info_s {
69 /* Nonzero if there have been any initializations at this priority
70 throughout the translation unit. */
71 int initializations_p;
72 /* Nonzero if there have been any destructions at this priority
73 throughout the translation unit. */
74 int destructions_p;
75 } *priority_info;
77 static void mark_vtable_entries (tree);
78 static bool maybe_emit_vtables (tree);
79 static bool acceptable_java_type (tree);
80 static tree start_objects (int, int);
81 static void finish_objects (int, int, tree);
82 static tree start_static_storage_duration_function (unsigned);
83 static void finish_static_storage_duration_function (tree);
84 static priority_info get_priority_info (int);
85 static void do_static_initialization_or_destruction (tree, bool);
86 static void one_static_initialization_or_destruction (tree, tree, bool);
87 static void generate_ctor_or_dtor_function (bool, int, location_t *);
88 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
89 void *);
90 static tree prune_vars_needing_no_initialization (tree *);
91 static void write_out_vars (tree);
92 static void import_export_class (tree);
93 static tree get_guard_bits (tree);
94 static void determine_visibility_from_class (tree, tree);
95 static bool determine_hidden_inline (tree);
96 static bool decl_defined_p (tree);
98 /* A list of static class variables. This is needed, because a
99 static class variable can be declared inside the class without
100 an initializer, and then initialized, statically, outside the class. */
101 static GTY(()) vec<tree, va_gc> *pending_statics;
103 /* A list of functions which were declared inline, but which we
104 may need to emit outline anyway. */
105 static GTY(()) vec<tree, va_gc> *deferred_fns;
107 /* A list of decls that use types with no linkage, which we need to make
108 sure are defined. */
109 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
111 /* A vector of alternating decls and identifiers, where the latter
112 is to be an alias for the former if the former is defined. */
113 static GTY(()) vec<tree, va_gc> *mangling_aliases;
115 /* Nonzero if we're done parsing and into end-of-file activities. */
117 int at_eof;
120 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
121 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
122 that apply to the function). */
124 tree
125 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
126 cp_ref_qualifier rqual)
128 tree raises;
129 tree attrs;
130 int type_quals;
131 bool late_return_type_p;
133 if (fntype == error_mark_node || ctype == error_mark_node)
134 return error_mark_node;
136 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
137 || TREE_CODE (fntype) == METHOD_TYPE);
139 type_quals = quals & ~TYPE_QUAL_RESTRICT;
140 ctype = cp_build_qualified_type (ctype, type_quals);
141 raises = TYPE_RAISES_EXCEPTIONS (fntype);
142 attrs = TYPE_ATTRIBUTES (fntype);
143 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
144 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
145 (TREE_CODE (fntype) == METHOD_TYPE
146 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
147 : TYPE_ARG_TYPES (fntype)));
148 if (attrs)
149 fntype = cp_build_type_attribute_variant (fntype, attrs);
150 if (rqual)
151 fntype = build_ref_qualified_type (fntype, rqual);
152 if (raises)
153 fntype = build_exception_variant (fntype, raises);
154 if (late_return_type_p)
155 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
157 return fntype;
160 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
161 return type changed to NEW_RET. */
163 tree
164 change_return_type (tree new_ret, tree fntype)
166 tree newtype;
167 tree args = TYPE_ARG_TYPES (fntype);
168 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
169 tree attrs = TYPE_ATTRIBUTES (fntype);
170 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
172 if (new_ret == error_mark_node)
173 return fntype;
175 if (same_type_p (new_ret, TREE_TYPE (fntype)))
176 return fntype;
178 if (TREE_CODE (fntype) == FUNCTION_TYPE)
180 newtype = build_function_type (new_ret, args);
181 newtype = apply_memfn_quals (newtype,
182 type_memfn_quals (fntype),
183 type_memfn_rqual (fntype));
185 else
186 newtype = build_method_type_directly
187 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
188 if (FUNCTION_REF_QUALIFIED (fntype))
189 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
190 if (raises)
191 newtype = build_exception_variant (newtype, raises);
192 if (attrs)
193 newtype = cp_build_type_attribute_variant (newtype, attrs);
194 if (late_return_type_p)
195 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
197 return newtype;
200 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
201 appropriately. */
203 tree
204 cp_build_parm_decl (tree name, tree type)
206 tree parm = build_decl (input_location,
207 PARM_DECL, name, type);
208 /* DECL_ARG_TYPE is only used by the back end and the back end never
209 sees templates. */
210 if (!processing_template_decl)
211 DECL_ARG_TYPE (parm) = type_passed_as (type);
213 return parm;
216 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
217 indicated NAME. */
219 tree
220 build_artificial_parm (tree name, tree type)
222 tree parm = cp_build_parm_decl (name, type);
223 DECL_ARTIFICIAL (parm) = 1;
224 /* All our artificial parms are implicitly `const'; they cannot be
225 assigned to. */
226 TREE_READONLY (parm) = 1;
227 return parm;
230 /* Constructors for types with virtual baseclasses need an "in-charge" flag
231 saying whether this constructor is responsible for initialization of
232 virtual baseclasses or not. All destructors also need this "in-charge"
233 flag, which additionally determines whether or not the destructor should
234 free the memory for the object.
236 This function adds the "in-charge" flag to member function FN if
237 appropriate. It is called from grokclassfn and tsubst.
238 FN must be either a constructor or destructor.
240 The in-charge flag follows the 'this' parameter, and is followed by the
241 VTT parm (if any), then the user-written parms. */
243 void
244 maybe_retrofit_in_chrg (tree fn)
246 tree basetype, arg_types, parms, parm, fntype;
248 /* If we've already add the in-charge parameter don't do it again. */
249 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
250 return;
252 /* When processing templates we can't know, in general, whether or
253 not we're going to have virtual baseclasses. */
254 if (processing_template_decl)
255 return;
257 /* We don't need an in-charge parameter for constructors that don't
258 have virtual bases. */
259 if (DECL_CONSTRUCTOR_P (fn)
260 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
261 return;
263 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
264 basetype = TREE_TYPE (TREE_VALUE (arg_types));
265 arg_types = TREE_CHAIN (arg_types);
267 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
269 /* If this is a subobject constructor or destructor, our caller will
270 pass us a pointer to our VTT. */
271 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
273 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
275 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
276 DECL_CHAIN (parm) = parms;
277 parms = parm;
279 /* ...and then to TYPE_ARG_TYPES. */
280 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
282 DECL_HAS_VTT_PARM_P (fn) = 1;
285 /* Then add the in-charge parm (before the VTT parm). */
286 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
287 DECL_CHAIN (parm) = parms;
288 parms = parm;
289 arg_types = hash_tree_chain (integer_type_node, arg_types);
291 /* Insert our new parameter(s) into the list. */
292 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
294 /* And rebuild the function type. */
295 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
296 arg_types);
297 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
298 fntype = build_exception_variant (fntype,
299 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
300 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
301 fntype = (cp_build_type_attribute_variant
302 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
303 TREE_TYPE (fn) = fntype;
305 /* Now we've got the in-charge parameter. */
306 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
309 /* Classes overload their constituent function names automatically.
310 When a function name is declared in a record structure,
311 its name is changed to it overloaded name. Since names for
312 constructors and destructors can conflict, we place a leading
313 '$' for destructors.
315 CNAME is the name of the class we are grokking for.
317 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
319 FLAGS contains bits saying what's special about today's
320 arguments. DTOR_FLAG == DESTRUCTOR.
322 If FUNCTION is a destructor, then we must add the `auto-delete' field
323 as a second parameter. There is some hair associated with the fact
324 that we must "declare" this variable in the manner consistent with the
325 way the rest of the arguments were declared.
327 QUALS are the qualifiers for the this pointer. */
329 void
330 grokclassfn (tree ctype, tree function, enum overload_flags flags)
332 tree fn_name = DECL_NAME (function);
334 /* Even within an `extern "C"' block, members get C++ linkage. See
335 [dcl.link] for details. */
336 SET_DECL_LANGUAGE (function, lang_cplusplus);
338 if (fn_name == NULL_TREE)
340 error ("name missing for member function");
341 fn_name = get_identifier ("<anonymous>");
342 DECL_NAME (function) = fn_name;
345 DECL_CONTEXT (function) = ctype;
347 if (flags == DTOR_FLAG)
348 DECL_DESTRUCTOR_P (function) = 1;
350 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
351 maybe_retrofit_in_chrg (function);
354 /* Create an ARRAY_REF, checking for the user doing things backwards
355 along the way. DECLTYPE_P is for N3276, as in the parser. */
357 tree
358 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
359 bool decltype_p)
361 tree type;
362 tree expr;
363 tree orig_array_expr = array_expr;
364 tree orig_index_exp = index_exp;
366 if (error_operand_p (array_expr) || error_operand_p (index_exp))
367 return error_mark_node;
369 if (processing_template_decl)
371 if (type_dependent_expression_p (array_expr)
372 || type_dependent_expression_p (index_exp))
373 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
374 NULL_TREE, NULL_TREE);
375 array_expr = build_non_dependent_expr (array_expr);
376 index_exp = build_non_dependent_expr (index_exp);
379 type = TREE_TYPE (array_expr);
380 gcc_assert (type);
381 type = non_reference (type);
383 /* If they have an `operator[]', use that. */
384 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
386 tsubst_flags_t complain = tf_warning_or_error;
387 if (decltype_p)
388 complain |= tf_decltype;
389 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
390 index_exp, NULL_TREE, /*overload=*/NULL, complain);
392 else
394 tree p1, p2, i1, i2;
396 /* Otherwise, create an ARRAY_REF for a pointer or array type.
397 It is a little-known fact that, if `a' is an array and `i' is
398 an int, you can write `i[a]', which means the same thing as
399 `a[i]'. */
400 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
401 p1 = array_expr;
402 else
403 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
405 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
406 p2 = index_exp;
407 else
408 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
410 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
411 false);
412 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
413 false);
415 if ((p1 && i2) && (i1 && p2))
416 error ("ambiguous conversion for array subscript");
418 if (p1 && i2)
419 array_expr = p1, index_exp = i2;
420 else if (i1 && p2)
421 array_expr = p2, index_exp = i1;
422 else
424 error ("invalid types %<%T[%T]%> for array subscript",
425 type, TREE_TYPE (index_exp));
426 return error_mark_node;
429 if (array_expr == error_mark_node || index_exp == error_mark_node)
430 error ("ambiguous conversion for array subscript");
432 expr = build_array_ref (input_location, array_expr, index_exp);
434 if (processing_template_decl && expr != error_mark_node)
435 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
436 NULL_TREE, NULL_TREE);
437 return expr;
440 /* Given the cast expression EXP, checking out its validity. Either return
441 an error_mark_node if there was an unavoidable error, return a cast to
442 void for trying to delete a pointer w/ the value 0, or return the
443 call to delete. If DOING_VEC is true, we handle things differently
444 for doing an array delete.
445 Implements ARM $5.3.4. This is called from the parser. */
447 tree
448 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
449 tsubst_flags_t complain)
451 tree t, type;
453 if (exp == error_mark_node)
454 return exp;
456 if (processing_template_decl)
458 t = build_min (DELETE_EXPR, void_type_node, exp, size);
459 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
460 DELETE_EXPR_USE_VEC (t) = doing_vec;
461 TREE_SIDE_EFFECTS (t) = 1;
462 return t;
465 /* An array can't have been allocated by new, so complain. */
466 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
467 warning (0, "deleting array %q#E", exp);
469 t = build_expr_type_conversion (WANT_POINTER, exp, true);
471 if (t == NULL_TREE || t == error_mark_node)
473 error ("type %q#T argument given to %<delete%>, expected pointer",
474 TREE_TYPE (exp));
475 return error_mark_node;
478 type = TREE_TYPE (t);
480 /* As of Valley Forge, you can delete a pointer to const. */
482 /* You can't delete functions. */
483 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
485 error ("cannot delete a function. Only pointer-to-objects are "
486 "valid arguments to %<delete%>");
487 return error_mark_node;
490 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
491 if (VOID_TYPE_P (TREE_TYPE (type)))
493 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
494 doing_vec = 0;
497 /* Deleting a pointer with the value zero is valid and has no effect. */
498 if (integer_zerop (t))
499 return build1 (NOP_EXPR, void_type_node, t);
501 if (doing_vec)
502 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
503 sfk_deleting_destructor,
504 use_global_delete, complain);
505 else
506 return build_delete (type, t, sfk_deleting_destructor,
507 LOOKUP_NORMAL, use_global_delete,
508 complain);
511 /* Report an error if the indicated template declaration is not the
512 sort of thing that should be a member template. */
514 void
515 check_member_template (tree tmpl)
517 tree decl;
519 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
520 decl = DECL_TEMPLATE_RESULT (tmpl);
522 if (TREE_CODE (decl) == FUNCTION_DECL
523 || DECL_ALIAS_TEMPLATE_P (tmpl)
524 || (TREE_CODE (decl) == TYPE_DECL
525 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
527 /* The parser rejects template declarations in local classes
528 (with the exception of generic lambdas). */
529 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
530 /* The parser rejects any use of virtual in a function template. */
531 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
532 && DECL_VIRTUAL_P (decl)));
534 /* The debug-information generating code doesn't know what to do
535 with member templates. */
536 DECL_IGNORED_P (tmpl) = 1;
538 else if (variable_template_p (tmpl))
539 /* OK */;
540 else
541 error ("template declaration of %q#D", decl);
544 /* Return true iff TYPE is a valid Java parameter or return type. */
546 static bool
547 acceptable_java_type (tree type)
549 if (type == error_mark_node)
550 return false;
552 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
553 return true;
554 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
556 type = TREE_TYPE (type);
557 if (TREE_CODE (type) == RECORD_TYPE)
559 tree args; int i;
560 if (! TYPE_FOR_JAVA (type))
561 return false;
562 if (! CLASSTYPE_TEMPLATE_INFO (type))
563 return true;
564 args = CLASSTYPE_TI_ARGS (type);
565 i = TREE_VEC_LENGTH (args);
566 while (--i >= 0)
568 type = TREE_VEC_ELT (args, i);
569 if (TYPE_PTR_P (type))
570 type = TREE_TYPE (type);
571 if (! TYPE_FOR_JAVA (type))
572 return false;
574 return true;
577 return false;
580 /* For a METHOD in a Java class CTYPE, return true if
581 the parameter and return types are valid Java types.
582 Otherwise, print appropriate error messages, and return false. */
584 bool
585 check_java_method (tree method)
587 bool jerr = false;
588 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
589 tree ret_type = TREE_TYPE (TREE_TYPE (method));
591 if (!acceptable_java_type (ret_type))
593 error ("Java method %qD has non-Java return type %qT",
594 method, ret_type);
595 jerr = true;
598 arg_types = TREE_CHAIN (arg_types);
599 if (DECL_HAS_IN_CHARGE_PARM_P (method))
600 arg_types = TREE_CHAIN (arg_types);
601 if (DECL_HAS_VTT_PARM_P (method))
602 arg_types = TREE_CHAIN (arg_types);
604 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
606 tree type = TREE_VALUE (arg_types);
607 if (!acceptable_java_type (type))
609 if (type != error_mark_node)
610 error ("Java method %qD has non-Java parameter type %qT",
611 method, type);
612 jerr = true;
615 return !jerr;
618 /* Sanity check: report error if this function FUNCTION is not
619 really a member of the class (CTYPE) it is supposed to belong to.
620 TEMPLATE_PARMS is used to specify the template parameters of a member
621 template passed as FUNCTION_DECL. If the member template is passed as a
622 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
623 from the declaration. If the function is not a function template, it
624 must be NULL.
625 It returns the original declaration for the function, NULL_TREE if
626 no declaration was found, error_mark_node if an error was emitted. */
628 tree
629 check_classfn (tree ctype, tree function, tree template_parms)
631 int ix;
632 bool is_template;
633 tree pushed_scope;
635 if (DECL_USE_TEMPLATE (function)
636 && !(TREE_CODE (function) == TEMPLATE_DECL
637 && DECL_TEMPLATE_SPECIALIZATION (function))
638 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
639 /* Since this is a specialization of a member template,
640 we're not going to find the declaration in the class.
641 For example, in:
643 struct S { template <typename T> void f(T); };
644 template <> void S::f(int);
646 we're not going to find `S::f(int)', but there's no
647 reason we should, either. We let our callers know we didn't
648 find the method, but we don't complain. */
649 return NULL_TREE;
651 /* Basic sanity check: for a template function, the template parameters
652 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
653 if (TREE_CODE (function) == TEMPLATE_DECL)
655 if (template_parms
656 && !comp_template_parms (template_parms,
657 DECL_TEMPLATE_PARMS (function)))
659 error ("template parameter lists provided don%'t match the "
660 "template parameters of %qD", function);
661 return error_mark_node;
663 template_parms = DECL_TEMPLATE_PARMS (function);
666 /* OK, is this a definition of a member template? */
667 is_template = (template_parms != NULL_TREE);
669 /* [temp.mem]
671 A destructor shall not be a member template. */
672 if (DECL_DESTRUCTOR_P (function) && is_template)
674 error ("destructor %qD declared as member template", function);
675 return error_mark_node;
678 /* We must enter the scope here, because conversion operators are
679 named by target type, and type equivalence relies on typenames
680 resolving within the scope of CTYPE. */
681 pushed_scope = push_scope (ctype);
682 ix = class_method_index_for_fn (complete_type (ctype), function);
683 if (ix >= 0)
685 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
686 tree fndecls, fndecl = 0;
687 bool is_conv_op;
688 const char *format = NULL;
690 for (fndecls = (*methods)[ix];
691 fndecls; fndecls = OVL_NEXT (fndecls))
693 tree p1, p2;
695 fndecl = OVL_CURRENT (fndecls);
696 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
697 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
699 /* We cannot simply call decls_match because this doesn't
700 work for static member functions that are pretending to
701 be methods, and because the name may have been changed by
702 asm("new_name"). */
704 /* Get rid of the this parameter on functions that become
705 static. */
706 if (DECL_STATIC_FUNCTION_P (fndecl)
707 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
708 p1 = TREE_CHAIN (p1);
710 /* A member template definition only matches a member template
711 declaration. */
712 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
713 continue;
715 /* ref-qualifier or absence of same must match. */
716 if (type_memfn_rqual (TREE_TYPE (function))
717 != type_memfn_rqual (TREE_TYPE (fndecl)))
718 continue;
720 /* While finding a match, same types and params are not enough
721 if the function is versioned. Also check version ("target")
722 attributes. */
723 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
724 TREE_TYPE (TREE_TYPE (fndecl)))
725 && compparms (p1, p2)
726 && !targetm.target_option.function_versions (function, fndecl)
727 && (!is_template
728 || comp_template_parms (template_parms,
729 DECL_TEMPLATE_PARMS (fndecl)))
730 && (DECL_TEMPLATE_SPECIALIZATION (function)
731 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
732 && (!DECL_TEMPLATE_SPECIALIZATION (function)
733 || (DECL_TI_TEMPLATE (function)
734 == DECL_TI_TEMPLATE (fndecl))))
735 break;
737 if (fndecls)
739 if (pushed_scope)
740 pop_scope (pushed_scope);
741 return OVL_CURRENT (fndecls);
744 error_at (DECL_SOURCE_LOCATION (function),
745 "prototype for %q#D does not match any in class %qT",
746 function, ctype);
747 is_conv_op = DECL_CONV_FN_P (fndecl);
749 if (is_conv_op)
750 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
751 fndecls = (*methods)[ix];
752 while (fndecls)
754 fndecl = OVL_CURRENT (fndecls);
755 fndecls = OVL_NEXT (fndecls);
757 if (!fndecls && is_conv_op)
759 if (methods->length () > (size_t) ++ix)
761 fndecls = (*methods)[ix];
762 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
764 fndecls = NULL_TREE;
765 is_conv_op = false;
768 else
769 is_conv_op = false;
771 if (format)
772 format = " %+#D";
773 else if (fndecls)
774 format = N_("candidates are: %+#D");
775 else
776 format = N_("candidate is: %+#D");
777 error (format, fndecl);
780 else if (!COMPLETE_TYPE_P (ctype))
781 cxx_incomplete_type_error (function, ctype);
782 else
783 error ("no %q#D member function declared in class %qT",
784 function, ctype);
786 if (pushed_scope)
787 pop_scope (pushed_scope);
788 return error_mark_node;
791 /* DECL is a function with vague linkage. Remember it so that at the
792 end of the translation unit we can decide whether or not to emit
793 it. */
795 void
796 note_vague_linkage_fn (tree decl)
798 DECL_DEFER_OUTPUT (decl) = 1;
799 vec_safe_push (deferred_fns, decl);
802 /* As above, but for variable template instantiations. */
804 void
805 note_variable_template_instantiation (tree decl)
807 vec_safe_push (pending_statics, decl);
810 /* We have just processed the DECL, which is a static data member.
811 The other parameters are as for cp_finish_decl. */
813 void
814 finish_static_data_member_decl (tree decl,
815 tree init, bool init_const_expr_p,
816 tree asmspec_tree,
817 int flags)
819 DECL_CONTEXT (decl) = current_class_type;
821 /* We cannot call pushdecl here, because that would fill in the
822 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
823 the right thing, namely, to put this decl out straight away. */
825 if (! processing_template_decl)
826 vec_safe_push (pending_statics, decl);
828 if (LOCAL_CLASS_P (current_class_type)
829 /* We already complained about the template definition. */
830 && !DECL_TEMPLATE_INSTANTIATION (decl))
831 permerror (input_location, "local class %q#T shall not have static data member %q#D",
832 current_class_type, decl);
833 else
834 for (tree t = current_class_type; TYPE_P (t);
835 t = CP_TYPE_CONTEXT (t))
836 if (TYPE_ANONYMOUS_P (t))
838 if (permerror (DECL_SOURCE_LOCATION (decl),
839 "static data member %qD in unnamed class", decl))
840 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
841 "unnamed class defined here");
842 break;
845 DECL_IN_AGGR_P (decl) = 1;
847 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
848 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
849 SET_VAR_HAD_UNKNOWN_BOUND (decl);
851 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
854 /* DECLARATOR and DECLSPECS correspond to a class member. The other
855 parameters are as for cp_finish_decl. Return the DECL for the
856 class member declared. */
858 tree
859 grokfield (const cp_declarator *declarator,
860 cp_decl_specifier_seq *declspecs,
861 tree init, bool init_const_expr_p,
862 tree asmspec_tree,
863 tree attrlist)
865 tree value;
866 const char *asmspec = 0;
867 int flags;
868 tree name;
870 if (init
871 && TREE_CODE (init) == TREE_LIST
872 && TREE_VALUE (init) == error_mark_node
873 && TREE_CHAIN (init) == NULL_TREE)
874 init = NULL_TREE;
876 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
877 if (! value || value == error_mark_node)
878 /* friend or constructor went bad. */
879 return error_mark_node;
880 if (TREE_TYPE (value) == error_mark_node)
881 return value;
883 if (TREE_CODE (value) == TYPE_DECL && init)
885 error ("typedef %qD is initialized (use decltype instead)", value);
886 init = NULL_TREE;
889 /* Pass friendly classes back. */
890 if (value == void_type_node)
891 return value;
894 name = DECL_NAME (value);
896 if (name != NULL_TREE)
898 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
900 error ("explicit template argument list not allowed");
901 return error_mark_node;
904 if (IDENTIFIER_POINTER (name)[0] == '_'
905 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
906 error ("member %qD conflicts with virtual function table field name",
907 value);
910 /* Stash away type declarations. */
911 if (TREE_CODE (value) == TYPE_DECL)
913 DECL_NONLOCAL (value) = 1;
914 DECL_CONTEXT (value) = current_class_type;
916 if (attrlist)
918 int attrflags = 0;
920 /* If this is a typedef that names the class for linkage purposes
921 (7.1.3p8), apply any attributes directly to the type. */
922 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
923 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
924 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
926 cplus_decl_attributes (&value, attrlist, attrflags);
929 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
930 && TREE_TYPE (value) != error_mark_node
931 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
932 set_underlying_type (value);
934 /* It's important that push_template_decl below follows
935 set_underlying_type above so that the created template
936 carries the properly set type of VALUE. */
937 if (processing_template_decl)
938 value = push_template_decl (value);
940 record_locally_defined_typedef (value);
941 return value;
944 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
946 if (!friendp && DECL_IN_AGGR_P (value))
948 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
949 return void_type_node;
952 if (asmspec_tree && asmspec_tree != error_mark_node)
953 asmspec = TREE_STRING_POINTER (asmspec_tree);
955 if (init)
957 if (TREE_CODE (value) == FUNCTION_DECL)
959 if (init == ridpointers[(int)RID_DELETE])
961 DECL_DELETED_FN (value) = 1;
962 DECL_DECLARED_INLINE_P (value) = 1;
963 DECL_INITIAL (value) = error_mark_node;
965 else if (init == ridpointers[(int)RID_DEFAULT])
967 if (defaultable_fn_check (value))
969 DECL_DEFAULTED_FN (value) = 1;
970 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
971 DECL_DECLARED_INLINE_P (value) = 1;
974 else if (TREE_CODE (init) == DEFAULT_ARG)
975 error ("invalid initializer for member function %qD", value);
976 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
978 if (integer_zerop (init))
979 DECL_PURE_VIRTUAL_P (value) = 1;
980 else if (error_operand_p (init))
981 ; /* An error has already been reported. */
982 else
983 error ("invalid initializer for member function %qD",
984 value);
986 else
988 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
989 if (friendp)
990 error ("initializer specified for friend function %qD",
991 value);
992 else
993 error ("initializer specified for static member function %qD",
994 value);
997 else if (TREE_CODE (value) == FIELD_DECL)
998 /* C++11 NSDMI, keep going. */;
999 else if (!VAR_P (value))
1000 gcc_unreachable ();
1003 /* Pass friend decls back. */
1004 if ((TREE_CODE (value) == FUNCTION_DECL
1005 || TREE_CODE (value) == TEMPLATE_DECL)
1006 && DECL_CONTEXT (value) != current_class_type)
1007 return value;
1009 /* Need to set this before push_template_decl. */
1010 if (TREE_CODE (value) == VAR_DECL)
1011 DECL_CONTEXT (value) = current_class_type;
1013 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1015 value = push_template_decl (value);
1016 if (error_operand_p (value))
1017 return error_mark_node;
1020 if (attrlist)
1021 cplus_decl_attributes (&value, attrlist, 0);
1023 if (init && DIRECT_LIST_INIT_P (init))
1024 flags = LOOKUP_NORMAL;
1025 else
1026 flags = LOOKUP_IMPLICIT;
1028 switch (TREE_CODE (value))
1030 case VAR_DECL:
1031 finish_static_data_member_decl (value, init, init_const_expr_p,
1032 asmspec_tree, flags);
1033 return value;
1035 case FIELD_DECL:
1036 if (asmspec)
1037 error ("%<asm%> specifiers are not permitted on non-static data members");
1038 if (DECL_INITIAL (value) == error_mark_node)
1039 init = error_mark_node;
1040 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1041 NULL_TREE, flags);
1042 DECL_IN_AGGR_P (value) = 1;
1043 return value;
1045 case FUNCTION_DECL:
1046 if (asmspec)
1047 set_user_assembler_name (value, asmspec);
1049 cp_finish_decl (value,
1050 /*init=*/NULL_TREE,
1051 /*init_const_expr_p=*/false,
1052 asmspec_tree, flags);
1054 /* Pass friends back this way. */
1055 if (DECL_FRIEND_P (value))
1056 return void_type_node;
1058 DECL_IN_AGGR_P (value) = 1;
1059 return value;
1061 default:
1062 gcc_unreachable ();
1064 return NULL_TREE;
1067 /* Like `grokfield', but for bitfields.
1068 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1070 tree
1071 grokbitfield (const cp_declarator *declarator,
1072 cp_decl_specifier_seq *declspecs, tree width,
1073 tree attrlist)
1075 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1077 if (value == error_mark_node)
1078 return NULL_TREE; /* friends went bad. */
1079 if (TREE_TYPE (value) == error_mark_node)
1080 return value;
1082 /* Pass friendly classes back. */
1083 if (VOID_TYPE_P (value))
1084 return void_type_node;
1086 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1087 && (POINTER_TYPE_P (value)
1088 || !dependent_type_p (TREE_TYPE (value))))
1090 error ("bit-field %qD with non-integral type", value);
1091 return error_mark_node;
1094 if (TREE_CODE (value) == TYPE_DECL)
1096 error ("cannot declare %qD to be a bit-field type", value);
1097 return NULL_TREE;
1100 /* Usually, finish_struct_1 catches bitfields with invalid types.
1101 But, in the case of bitfields with function type, we confuse
1102 ourselves into thinking they are member functions, so we must
1103 check here. */
1104 if (TREE_CODE (value) == FUNCTION_DECL)
1106 error ("cannot declare bit-field %qD with function type",
1107 DECL_NAME (value));
1108 return NULL_TREE;
1111 if (DECL_IN_AGGR_P (value))
1113 error ("%qD is already defined in the class %qT", value,
1114 DECL_CONTEXT (value));
1115 return void_type_node;
1118 if (TREE_STATIC (value))
1120 error ("static member %qD cannot be a bit-field", value);
1121 return NULL_TREE;
1123 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1125 if (width != error_mark_node)
1127 /* The width must be an integer type. */
1128 if (!type_dependent_expression_p (width)
1129 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1130 error ("width of bit-field %qD has non-integral type %qT", value,
1131 TREE_TYPE (width));
1132 DECL_INITIAL (value) = width;
1133 SET_DECL_C_BIT_FIELD (value);
1136 DECL_IN_AGGR_P (value) = 1;
1138 if (attrlist)
1139 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1141 return value;
1145 /* Returns true iff ATTR is an attribute which needs to be applied at
1146 instantiation time rather than template definition time. */
1148 static bool
1149 is_late_template_attribute (tree attr, tree decl)
1151 tree name = get_attribute_name (attr);
1152 tree args = TREE_VALUE (attr);
1153 const struct attribute_spec *spec = lookup_attribute_spec (name);
1154 tree arg;
1156 if (!spec)
1157 /* Unknown attribute. */
1158 return false;
1160 /* Attribute weak handling wants to write out assembly right away. */
1161 if (is_attribute_p ("weak", name))
1162 return true;
1164 /* Attribute unused is applied directly, as it appertains to
1165 decls. */
1166 if (is_attribute_p ("unused", name))
1167 return false;
1169 /* #pragma omp declare simd attribute needs to be always deferred. */
1170 if (flag_openmp
1171 && is_attribute_p ("omp declare simd", name))
1172 return true;
1174 /* An attribute pack is clearly dependent. */
1175 if (args && PACK_EXPANSION_P (args))
1176 return true;
1178 /* If any of the arguments are dependent expressions, we can't evaluate
1179 the attribute until instantiation time. */
1180 for (arg = args; arg; arg = TREE_CHAIN (arg))
1182 tree t = TREE_VALUE (arg);
1184 /* If the first attribute argument is an identifier, only consider
1185 second and following arguments. Attributes like mode, format,
1186 cleanup and several target specific attributes aren't late
1187 just because they have an IDENTIFIER_NODE as first argument. */
1188 if (arg == args && identifier_p (t))
1189 continue;
1191 if (value_dependent_expression_p (t)
1192 || type_dependent_expression_p (t))
1193 return true;
1196 if (TREE_CODE (decl) == TYPE_DECL
1197 || TYPE_P (decl)
1198 || spec->type_required)
1200 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1202 /* We can't apply any attributes to a completely unknown type until
1203 instantiation time. */
1204 enum tree_code code = TREE_CODE (type);
1205 if (code == TEMPLATE_TYPE_PARM
1206 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1207 || code == TYPENAME_TYPE)
1208 return true;
1209 /* Also defer most attributes on dependent types. This is not
1210 necessary in all cases, but is the better default. */
1211 else if (dependent_type_p (type)
1212 /* But some attributes specifically apply to templates. */
1213 && !is_attribute_p ("abi_tag", name)
1214 && !is_attribute_p ("deprecated", name)
1215 && !is_attribute_p ("visibility", name))
1216 return true;
1217 else
1218 return false;
1220 else
1221 return false;
1224 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1225 applied at instantiation time and return them. If IS_DEPENDENT is true,
1226 the declaration itself is dependent, so all attributes should be applied
1227 at instantiation time. */
1229 static tree
1230 splice_template_attributes (tree *attr_p, tree decl)
1232 tree *p = attr_p;
1233 tree late_attrs = NULL_TREE;
1234 tree *q = &late_attrs;
1236 if (!p)
1237 return NULL_TREE;
1239 for (; *p; )
1241 if (is_late_template_attribute (*p, decl))
1243 ATTR_IS_DEPENDENT (*p) = 1;
1244 *q = *p;
1245 *p = TREE_CHAIN (*p);
1246 q = &TREE_CHAIN (*q);
1247 *q = NULL_TREE;
1249 else
1250 p = &TREE_CHAIN (*p);
1253 return late_attrs;
1256 /* Remove any late attributes from the list in ATTR_P and attach them to
1257 DECL_P. */
1259 static void
1260 save_template_attributes (tree *attr_p, tree *decl_p)
1262 tree *q;
1264 if (attr_p && *attr_p == error_mark_node)
1265 return;
1267 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1268 if (!late_attrs)
1269 return;
1271 if (DECL_P (*decl_p))
1272 q = &DECL_ATTRIBUTES (*decl_p);
1273 else
1274 q = &TYPE_ATTRIBUTES (*decl_p);
1276 tree old_attrs = *q;
1278 /* Merge the late attributes at the beginning with the attribute
1279 list. */
1280 late_attrs = merge_attributes (late_attrs, *q);
1281 *q = late_attrs;
1283 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1285 /* We've added new attributes directly to the main variant, so
1286 now we need to update all of the other variants to include
1287 these new attributes. */
1288 tree variant;
1289 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1290 variant = TYPE_NEXT_VARIANT (variant))
1292 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1293 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1298 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1299 to a typedef which gives a previously anonymous class or enum a name for
1300 linkage purposes. */
1302 bool
1303 attributes_naming_typedef_ok (tree attrs)
1305 for (; attrs; attrs = TREE_CHAIN (attrs))
1307 tree name = get_attribute_name (attrs);
1308 if (is_attribute_p ("vector_size", name))
1309 return false;
1311 return true;
1314 /* Like reconstruct_complex_type, but handle also template trees. */
1316 tree
1317 cp_reconstruct_complex_type (tree type, tree bottom)
1319 tree inner, outer;
1320 bool late_return_type_p = false;
1322 if (TYPE_PTR_P (type))
1324 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1325 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1326 TYPE_REF_CAN_ALIAS_ALL (type));
1328 else if (TREE_CODE (type) == REFERENCE_TYPE)
1330 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1331 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1332 TYPE_REF_CAN_ALIAS_ALL (type));
1334 else if (TREE_CODE (type) == ARRAY_TYPE)
1336 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1337 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1338 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1339 element type qualification will be handled by the recursive
1340 cp_reconstruct_complex_type call and cp_build_qualified_type
1341 for ARRAY_TYPEs changes the element type. */
1342 return outer;
1344 else if (TREE_CODE (type) == FUNCTION_TYPE)
1346 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1347 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1348 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1349 outer = apply_memfn_quals (outer,
1350 type_memfn_quals (type),
1351 type_memfn_rqual (type));
1353 else if (TREE_CODE (type) == METHOD_TYPE)
1355 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1356 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1357 /* The build_method_type_directly() routine prepends 'this' to argument list,
1358 so we must compensate by getting rid of it. */
1359 outer
1360 = build_method_type_directly
1361 (class_of_this_parm (type), inner,
1362 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1364 else if (TREE_CODE (type) == OFFSET_TYPE)
1366 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1367 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1369 else
1370 return bottom;
1372 if (TYPE_ATTRIBUTES (type))
1373 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1374 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1376 if (late_return_type_p)
1377 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1379 return outer;
1382 /* Replaces any constexpr expression that may be into the attributes
1383 arguments with their reduced value. */
1385 static void
1386 cp_check_const_attributes (tree attributes)
1388 if (attributes == error_mark_node)
1389 return;
1391 tree attr;
1392 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1394 tree arg;
1395 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1397 tree expr = TREE_VALUE (arg);
1398 if (EXPR_P (expr))
1399 TREE_VALUE (arg) = maybe_constant_value (expr);
1404 /* Return true if TYPE is an OpenMP mappable type. */
1405 bool
1406 cp_omp_mappable_type (tree type)
1408 /* Mappable type has to be complete. */
1409 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1410 return false;
1411 /* Arrays have mappable type if the elements have mappable type. */
1412 while (TREE_CODE (type) == ARRAY_TYPE)
1413 type = TREE_TYPE (type);
1414 /* A mappable type cannot contain virtual members. */
1415 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1416 return false;
1417 /* All data members must be non-static. */
1418 if (CLASS_TYPE_P (type))
1420 tree field;
1421 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1422 if (TREE_CODE (field) == VAR_DECL)
1423 return false;
1424 /* All fields must have mappable types. */
1425 else if (TREE_CODE (field) == FIELD_DECL
1426 && !cp_omp_mappable_type (TREE_TYPE (field)))
1427 return false;
1429 return true;
1432 /* Like decl_attributes, but handle C++ complexity. */
1434 void
1435 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1437 if (*decl == NULL_TREE || *decl == void_type_node
1438 || *decl == error_mark_node)
1439 return;
1441 /* Add implicit "omp declare target" attribute if requested. */
1442 if (scope_chain->omp_declare_target_attribute
1443 && ((TREE_CODE (*decl) == VAR_DECL
1444 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1445 || TREE_CODE (*decl) == FUNCTION_DECL))
1447 if (TREE_CODE (*decl) == VAR_DECL
1448 && DECL_CLASS_SCOPE_P (*decl))
1449 error ("%q+D static data member inside of declare target directive",
1450 *decl);
1451 else if (TREE_CODE (*decl) == VAR_DECL
1452 && (DECL_FUNCTION_SCOPE_P (*decl)
1453 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1454 error ("%q+D in block scope inside of declare target directive",
1455 *decl);
1456 else if (!processing_template_decl
1457 && TREE_CODE (*decl) == VAR_DECL
1458 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1459 error ("%q+D in declare target directive does not have mappable type",
1460 *decl);
1461 else
1462 attributes = tree_cons (get_identifier ("omp declare target"),
1463 NULL_TREE, attributes);
1466 if (processing_template_decl)
1468 if (check_for_bare_parameter_packs (attributes))
1469 return;
1471 save_template_attributes (&attributes, decl);
1474 cp_check_const_attributes (attributes);
1476 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1477 decl = &DECL_TEMPLATE_RESULT (*decl);
1479 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1481 attributes
1482 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1483 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1484 attributes, flags);
1486 else
1487 decl_attributes (decl, attributes, flags);
1489 if (TREE_CODE (*decl) == TYPE_DECL)
1490 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1492 /* Propagate deprecation out to the template. */
1493 if (TREE_DEPRECATED (*decl))
1494 if (tree ti = get_template_info (*decl))
1496 tree tmpl = TI_TEMPLATE (ti);
1497 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1498 : DECL_TEMPLATE_RESULT (tmpl));
1499 if (*decl == pattern)
1500 TREE_DEPRECATED (tmpl) = true;
1504 /* Walks through the namespace- or function-scope anonymous union
1505 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1506 Returns one of the fields for use in the mangled name. */
1508 static tree
1509 build_anon_union_vars (tree type, tree object)
1511 tree main_decl = NULL_TREE;
1512 tree field;
1514 /* Rather than write the code to handle the non-union case,
1515 just give an error. */
1516 if (TREE_CODE (type) != UNION_TYPE)
1518 error ("anonymous struct not inside named type");
1519 return error_mark_node;
1522 for (field = TYPE_FIELDS (type);
1523 field != NULL_TREE;
1524 field = DECL_CHAIN (field))
1526 tree decl;
1527 tree ref;
1529 if (DECL_ARTIFICIAL (field))
1530 continue;
1531 if (TREE_CODE (field) != FIELD_DECL)
1533 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1534 "have non-static data members", field);
1535 continue;
1538 if (TREE_PRIVATE (field))
1539 permerror (input_location, "private member %q+#D in anonymous union", field);
1540 else if (TREE_PROTECTED (field))
1541 permerror (input_location, "protected member %q+#D in anonymous union", field);
1543 if (processing_template_decl)
1544 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1545 DECL_NAME (field), NULL_TREE);
1546 else
1547 ref = build_class_member_access_expr (object, field, NULL_TREE,
1548 false, tf_warning_or_error);
1550 if (DECL_NAME (field))
1552 tree base;
1554 decl = build_decl (input_location,
1555 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1556 DECL_ANON_UNION_VAR_P (decl) = 1;
1557 DECL_ARTIFICIAL (decl) = 1;
1559 base = get_base_address (object);
1560 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1561 TREE_STATIC (decl) = TREE_STATIC (base);
1562 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1564 SET_DECL_VALUE_EXPR (decl, ref);
1565 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1567 decl = pushdecl (decl);
1569 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1570 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1571 else
1572 decl = 0;
1574 if (main_decl == NULL_TREE)
1575 main_decl = decl;
1578 return main_decl;
1581 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1582 anonymous union, then all members must be laid out together. PUBLIC_P
1583 is nonzero if this union is not declared static. */
1585 void
1586 finish_anon_union (tree anon_union_decl)
1588 tree type;
1589 tree main_decl;
1590 bool public_p;
1592 if (anon_union_decl == error_mark_node)
1593 return;
1595 type = TREE_TYPE (anon_union_decl);
1596 public_p = TREE_PUBLIC (anon_union_decl);
1598 /* The VAR_DECL's context is the same as the TYPE's context. */
1599 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1601 if (TYPE_FIELDS (type) == NULL_TREE)
1602 return;
1604 if (public_p)
1606 error ("namespace-scope anonymous aggregates must be static");
1607 return;
1610 main_decl = build_anon_union_vars (type, anon_union_decl);
1611 if (main_decl == error_mark_node)
1612 return;
1613 if (main_decl == NULL_TREE)
1615 warning (0, "anonymous union with no members");
1616 return;
1619 if (!processing_template_decl)
1621 /* Use main_decl to set the mangled name. */
1622 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1623 maybe_commonize_var (anon_union_decl);
1624 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1625 mangle_decl (anon_union_decl);
1626 DECL_NAME (anon_union_decl) = NULL_TREE;
1629 pushdecl (anon_union_decl);
1630 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1633 /* Auxiliary functions to make type signatures for
1634 `operator new' and `operator delete' correspond to
1635 what compiler will be expecting. */
1637 tree
1638 coerce_new_type (tree type)
1640 int e = 0;
1641 tree args = TYPE_ARG_TYPES (type);
1643 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1645 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1647 e = 1;
1648 error ("%<operator new%> must return type %qT", ptr_type_node);
1651 if (args && args != void_list_node)
1653 if (TREE_PURPOSE (args))
1655 /* [basic.stc.dynamic.allocation]
1657 The first parameter shall not have an associated default
1658 argument. */
1659 error ("the first parameter of %<operator new%> cannot "
1660 "have a default argument");
1661 /* Throw away the default argument. */
1662 TREE_PURPOSE (args) = NULL_TREE;
1665 if (!same_type_p (TREE_VALUE (args), size_type_node))
1667 e = 2;
1668 args = TREE_CHAIN (args);
1671 else
1672 e = 2;
1674 if (e == 2)
1675 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1676 "as first parameter", size_type_node);
1678 switch (e)
1680 case 2:
1681 args = tree_cons (NULL_TREE, size_type_node, args);
1682 /* Fall through. */
1683 case 1:
1684 type = build_exception_variant
1685 (build_function_type (ptr_type_node, args),
1686 TYPE_RAISES_EXCEPTIONS (type));
1687 /* Fall through. */
1688 default:;
1690 return type;
1693 tree
1694 coerce_delete_type (tree type)
1696 int e = 0;
1697 tree args = TYPE_ARG_TYPES (type);
1699 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1701 if (!same_type_p (TREE_TYPE (type), void_type_node))
1703 e = 1;
1704 error ("%<operator delete%> must return type %qT", void_type_node);
1707 if (!args || args == void_list_node
1708 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1710 e = 2;
1711 if (args && args != void_list_node)
1712 args = TREE_CHAIN (args);
1713 error ("%<operator delete%> takes type %qT as first parameter",
1714 ptr_type_node);
1716 switch (e)
1718 case 2:
1719 args = tree_cons (NULL_TREE, ptr_type_node, args);
1720 /* Fall through. */
1721 case 1:
1722 type = build_exception_variant
1723 (build_function_type (void_type_node, args),
1724 TYPE_RAISES_EXCEPTIONS (type));
1725 /* Fall through. */
1726 default:;
1729 return type;
1732 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1733 and mark them as needed. */
1735 static void
1736 mark_vtable_entries (tree decl)
1738 tree fnaddr;
1739 unsigned HOST_WIDE_INT idx;
1741 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1742 idx, fnaddr)
1744 tree fn;
1746 STRIP_NOPS (fnaddr);
1748 if (TREE_CODE (fnaddr) != ADDR_EXPR
1749 && TREE_CODE (fnaddr) != FDESC_EXPR)
1750 /* This entry is an offset: a virtual base class offset, a
1751 virtual call offset, an RTTI offset, etc. */
1752 continue;
1754 fn = TREE_OPERAND (fnaddr, 0);
1755 TREE_ADDRESSABLE (fn) = 1;
1756 /* When we don't have vcall offsets, we output thunks whenever
1757 we output the vtables that contain them. With vcall offsets,
1758 we know all the thunks we'll need when we emit a virtual
1759 function, so we emit the thunks there instead. */
1760 if (DECL_THUNK_P (fn))
1761 use_thunk (fn, /*emit_p=*/0);
1762 mark_used (fn);
1766 /* Set DECL up to have the closest approximation of "initialized common"
1767 linkage available. */
1769 void
1770 comdat_linkage (tree decl)
1772 if (flag_weak)
1773 make_decl_one_only (decl, cxx_comdat_group (decl));
1774 else if (TREE_CODE (decl) == FUNCTION_DECL
1775 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1776 /* We can just emit function and compiler-generated variables
1777 statically; having multiple copies is (for the most part) only
1778 a waste of space.
1780 There are two correctness issues, however: the address of a
1781 template instantiation with external linkage should be the
1782 same, independent of what translation unit asks for the
1783 address, and this will not hold when we emit multiple copies of
1784 the function. However, there's little else we can do.
1786 Also, by default, the typeinfo implementation assumes that
1787 there will be only one copy of the string used as the name for
1788 each type. Therefore, if weak symbols are unavailable, the
1789 run-time library should perform a more conservative check; it
1790 should perform a string comparison, rather than an address
1791 comparison. */
1792 TREE_PUBLIC (decl) = 0;
1793 else
1795 /* Static data member template instantiations, however, cannot
1796 have multiple copies. */
1797 if (DECL_INITIAL (decl) == 0
1798 || DECL_INITIAL (decl) == error_mark_node)
1799 DECL_COMMON (decl) = 1;
1800 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1802 DECL_COMMON (decl) = 1;
1803 DECL_INITIAL (decl) = error_mark_node;
1805 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1807 /* We can't do anything useful; leave vars for explicit
1808 instantiation. */
1809 DECL_EXTERNAL (decl) = 1;
1810 DECL_NOT_REALLY_EXTERN (decl) = 0;
1814 DECL_COMDAT (decl) = 1;
1817 /* For win32 we also want to put explicit instantiations in
1818 linkonce sections, so that they will be merged with implicit
1819 instantiations; otherwise we get duplicate symbol errors.
1820 For Darwin we do not want explicit instantiations to be
1821 linkonce. */
1823 void
1824 maybe_make_one_only (tree decl)
1826 /* We used to say that this was not necessary on targets that support weak
1827 symbols, because the implicit instantiations will defer to the explicit
1828 one. However, that's not actually the case in SVR4; a strong definition
1829 after a weak one is an error. Also, not making explicit
1830 instantiations one_only means that we can end up with two copies of
1831 some template instantiations. */
1832 if (! flag_weak)
1833 return;
1835 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1836 we can get away with not emitting them if they aren't used. We need
1837 to for variables so that cp_finish_decl will update their linkage,
1838 because their DECL_INITIAL may not have been set properly yet. */
1840 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1841 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1842 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1844 make_decl_one_only (decl, cxx_comdat_group (decl));
1846 if (VAR_P (decl))
1848 varpool_node *node = varpool_node::get_create (decl);
1849 DECL_COMDAT (decl) = 1;
1850 /* Mark it needed so we don't forget to emit it. */
1851 node->forced_by_abi = true;
1852 TREE_USED (decl) = 1;
1857 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1858 This predicate will give the right answer during parsing of the
1859 function, which other tests may not. */
1861 bool
1862 vague_linkage_p (tree decl)
1864 if (!TREE_PUBLIC (decl))
1866 gcc_checking_assert (!DECL_COMDAT (decl));
1867 return false;
1869 /* Unfortunately, import_export_decl has not always been called
1870 before the function is processed, so we cannot simply check
1871 DECL_COMDAT. */
1872 if (DECL_COMDAT (decl)
1873 || (TREE_CODE (decl) == FUNCTION_DECL
1874 && DECL_DECLARED_INLINE_P (decl))
1875 || (DECL_LANG_SPECIFIC (decl)
1876 && DECL_TEMPLATE_INSTANTIATION (decl)))
1877 return true;
1878 else if (DECL_FUNCTION_SCOPE_P (decl))
1879 /* A local static in an inline effectively has vague linkage. */
1880 return (TREE_STATIC (decl)
1881 && vague_linkage_p (DECL_CONTEXT (decl)));
1882 else
1883 return false;
1886 /* Determine whether or not we want to specifically import or export CTYPE,
1887 using various heuristics. */
1889 static void
1890 import_export_class (tree ctype)
1892 /* -1 for imported, 1 for exported. */
1893 int import_export = 0;
1895 /* It only makes sense to call this function at EOF. The reason is
1896 that this function looks at whether or not the first non-inline
1897 non-abstract virtual member function has been defined in this
1898 translation unit. But, we can't possibly know that until we've
1899 seen the entire translation unit. */
1900 gcc_assert (at_eof);
1902 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1903 return;
1905 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1906 we will have CLASSTYPE_INTERFACE_ONLY set but not
1907 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1908 heuristic because someone will supply a #pragma implementation
1909 elsewhere, and deducing it here would produce a conflict. */
1910 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1911 return;
1913 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1914 import_export = -1;
1915 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1916 import_export = 1;
1917 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1918 && !flag_implicit_templates)
1919 /* For a template class, without -fimplicit-templates, check the
1920 repository. If the virtual table is assigned to this
1921 translation unit, then export the class; otherwise, import
1922 it. */
1923 import_export = repo_export_class_p (ctype) ? 1 : -1;
1924 else if (TYPE_POLYMORPHIC_P (ctype))
1926 /* The ABI specifies that the virtual table and associated
1927 information are emitted with the key method, if any. */
1928 tree method = CLASSTYPE_KEY_METHOD (ctype);
1929 /* If weak symbol support is not available, then we must be
1930 careful not to emit the vtable when the key function is
1931 inline. An inline function can be defined in multiple
1932 translation units. If we were to emit the vtable in each
1933 translation unit containing a definition, we would get
1934 multiple definition errors at link-time. */
1935 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1936 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1939 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1940 a definition anywhere else. */
1941 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1942 import_export = 0;
1944 /* Allow back ends the chance to overrule the decision. */
1945 if (targetm.cxx.import_export_class)
1946 import_export = targetm.cxx.import_export_class (ctype, import_export);
1948 if (import_export)
1950 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1951 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1955 /* Return true if VAR has already been provided to the back end; in that
1956 case VAR should not be modified further by the front end. */
1957 static bool
1958 var_finalized_p (tree var)
1960 return varpool_node::get_create (var)->definition;
1963 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1964 must be emitted in this translation unit. Mark it as such. */
1966 void
1967 mark_needed (tree decl)
1969 TREE_USED (decl) = 1;
1970 if (TREE_CODE (decl) == FUNCTION_DECL)
1972 /* Extern inline functions don't become needed when referenced.
1973 If we know a method will be emitted in other TU and no new
1974 functions can be marked reachable, just use the external
1975 definition. */
1976 struct cgraph_node *node = cgraph_node::get_create (decl);
1977 node->forced_by_abi = true;
1979 /* #pragma interface and -frepo code can call mark_needed for
1980 maybe-in-charge 'tors; mark the clones as well. */
1981 tree clone;
1982 FOR_EACH_CLONE (clone, decl)
1983 mark_needed (clone);
1985 else if (TREE_CODE (decl) == VAR_DECL)
1987 varpool_node *node = varpool_node::get_create (decl);
1988 /* C++ frontend use mark_decl_references to force COMDAT variables
1989 to be output that might appear dead otherwise. */
1990 node->forced_by_abi = true;
1994 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1995 returns true if a definition of this entity should be provided in
1996 this object file. Callers use this function to determine whether
1997 or not to let the back end know that a definition of DECL is
1998 available in this translation unit. */
2000 bool
2001 decl_needed_p (tree decl)
2003 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2004 /* This function should only be called at the end of the translation
2005 unit. We cannot be sure of whether or not something will be
2006 COMDAT until that point. */
2007 gcc_assert (at_eof);
2009 /* All entities with external linkage that are not COMDAT/EXTERN should be
2010 emitted; they may be referred to from other object files. */
2011 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2012 return true;
2013 /* Functions marked "dllexport" must be emitted so that they are
2014 visible to other DLLs. */
2015 if (flag_keep_inline_dllexport
2016 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2017 return true;
2019 /* When not optimizing, do not bother to produce definitions for extern
2020 symbols. */
2021 if (DECL_REALLY_EXTERN (decl)
2022 && ((TREE_CODE (decl) != FUNCTION_DECL
2023 && !optimize)
2024 || (TREE_CODE (decl) == FUNCTION_DECL
2025 && !opt_for_fn (decl, optimize)))
2026 && !lookup_attribute ("always_inline", decl))
2027 return false;
2029 /* If this entity was used, let the back end see it; it will decide
2030 whether or not to emit it into the object file. */
2031 if (TREE_USED (decl))
2032 return true;
2033 /* Virtual functions might be needed for devirtualization. */
2034 if (flag_devirtualize
2035 && TREE_CODE (decl) == FUNCTION_DECL
2036 && DECL_VIRTUAL_P (decl))
2037 return true;
2038 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2039 reference to DECL might cause it to be emitted later. */
2040 return false;
2043 /* If necessary, write out the vtables for the dynamic class CTYPE.
2044 Returns true if any vtables were emitted. */
2046 static bool
2047 maybe_emit_vtables (tree ctype)
2049 tree vtbl;
2050 tree primary_vtbl;
2051 int needed = 0;
2052 varpool_node *current = NULL, *last = NULL;
2054 /* If the vtables for this class have already been emitted there is
2055 nothing more to do. */
2056 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2057 if (var_finalized_p (primary_vtbl))
2058 return false;
2059 /* Ignore dummy vtables made by get_vtable_decl. */
2060 if (TREE_TYPE (primary_vtbl) == void_type_node)
2061 return false;
2063 /* On some targets, we cannot determine the key method until the end
2064 of the translation unit -- which is when this function is
2065 called. */
2066 if (!targetm.cxx.key_method_may_be_inline ())
2067 determine_key_method (ctype);
2069 /* See if any of the vtables are needed. */
2070 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2072 import_export_decl (vtbl);
2073 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2074 needed = 1;
2076 if (!needed)
2078 /* If the references to this class' vtables are optimized away,
2079 still emit the appropriate debugging information. See
2080 dfs_debug_mark. */
2081 if (DECL_COMDAT (primary_vtbl)
2082 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2083 note_debug_info_needed (ctype);
2084 return false;
2087 /* The ABI requires that we emit all of the vtables if we emit any
2088 of them. */
2089 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2091 /* Mark entities references from the virtual table as used. */
2092 mark_vtable_entries (vtbl);
2094 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2096 vec<tree, va_gc> *cleanups = NULL;
2097 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2098 LOOKUP_NORMAL);
2100 /* It had better be all done at compile-time. */
2101 gcc_assert (!expr && !cleanups);
2104 /* Write it out. */
2105 DECL_EXTERNAL (vtbl) = 0;
2106 rest_of_decl_compilation (vtbl, 1, 1);
2108 /* Because we're only doing syntax-checking, we'll never end up
2109 actually marking the variable as written. */
2110 if (flag_syntax_only)
2111 TREE_ASM_WRITTEN (vtbl) = 1;
2112 else if (DECL_ONE_ONLY (vtbl))
2114 current = varpool_node::get_create (vtbl);
2115 if (last)
2116 current->add_to_same_comdat_group (last);
2117 last = current;
2121 /* Since we're writing out the vtable here, also write the debug
2122 info. */
2123 note_debug_info_needed (ctype);
2125 return true;
2128 /* A special return value from type_visibility meaning internal
2129 linkage. */
2131 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2133 /* walk_tree helper function for type_visibility. */
2135 static tree
2136 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2138 int *vis_p = (int *)data;
2139 if (! TYPE_P (*tp))
2141 *walk_subtrees = 0;
2143 else if (OVERLOAD_TYPE_P (*tp)
2144 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2146 *vis_p = VISIBILITY_ANON;
2147 return *tp;
2149 else if (CLASS_TYPE_P (*tp)
2150 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2151 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2152 return NULL;
2155 /* Returns the visibility of TYPE, which is the minimum visibility of its
2156 component types. */
2158 static int
2159 type_visibility (tree type)
2161 int vis = VISIBILITY_DEFAULT;
2162 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2163 return vis;
2166 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2167 specified (or if VISIBILITY is static). If TMPL is true, this
2168 constraint is for a template argument, and takes precedence
2169 over explicitly-specified visibility on the template. */
2171 static void
2172 constrain_visibility (tree decl, int visibility, bool tmpl)
2174 if (visibility == VISIBILITY_ANON)
2176 /* extern "C" declarations aren't affected by the anonymous
2177 namespace. */
2178 if (!DECL_EXTERN_C_P (decl))
2180 TREE_PUBLIC (decl) = 0;
2181 DECL_WEAK (decl) = 0;
2182 DECL_COMMON (decl) = 0;
2183 DECL_COMDAT (decl) = false;
2184 if (TREE_CODE (decl) == FUNCTION_DECL
2185 || TREE_CODE (decl) == VAR_DECL)
2187 struct symtab_node *snode = symtab_node::get (decl);
2189 if (snode)
2190 snode->set_comdat_group (NULL);
2192 DECL_INTERFACE_KNOWN (decl) = 1;
2193 if (DECL_LANG_SPECIFIC (decl))
2194 DECL_NOT_REALLY_EXTERN (decl) = 1;
2197 else if (visibility > DECL_VISIBILITY (decl)
2198 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2200 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2201 /* This visibility was not specified. */
2202 DECL_VISIBILITY_SPECIFIED (decl) = false;
2206 /* Constrain the visibility of DECL based on the visibility of its template
2207 arguments. */
2209 static void
2210 constrain_visibility_for_template (tree decl, tree targs)
2212 /* If this is a template instantiation, check the innermost
2213 template args for visibility constraints. The outer template
2214 args are covered by the class check. */
2215 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2216 int i;
2217 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2219 int vis = 0;
2221 tree arg = TREE_VEC_ELT (args, i-1);
2222 if (TYPE_P (arg))
2223 vis = type_visibility (arg);
2224 else
2226 if (REFERENCE_REF_P (arg))
2227 arg = TREE_OPERAND (arg, 0);
2228 if (TREE_TYPE (arg))
2229 STRIP_NOPS (arg);
2230 if (TREE_CODE (arg) == ADDR_EXPR)
2231 arg = TREE_OPERAND (arg, 0);
2232 if (VAR_OR_FUNCTION_DECL_P (arg))
2234 if (! TREE_PUBLIC (arg))
2235 vis = VISIBILITY_ANON;
2236 else
2237 vis = DECL_VISIBILITY (arg);
2240 if (vis)
2241 constrain_visibility (decl, vis, true);
2245 /* Like c_determine_visibility, but with additional C++-specific
2246 behavior.
2248 Function-scope entities can rely on the function's visibility because
2249 it is set in start_preparsed_function.
2251 Class-scope entities cannot rely on the class's visibility until the end
2252 of the enclosing class definition.
2254 Note that because namespaces have multiple independent definitions,
2255 namespace visibility is handled elsewhere using the #pragma visibility
2256 machinery rather than by decorating the namespace declaration.
2258 The goal is for constraints from the type to give a diagnostic, and
2259 other constraints to be applied silently. */
2261 void
2262 determine_visibility (tree decl)
2264 tree class_type = NULL_TREE;
2265 bool use_template;
2266 bool orig_visibility_specified;
2267 enum symbol_visibility orig_visibility;
2269 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2271 /* Only relevant for names with external linkage. */
2272 if (!TREE_PUBLIC (decl))
2273 return;
2275 /* Cloned constructors and destructors get the same visibility as
2276 the underlying function. That should be set up in
2277 maybe_clone_body. */
2278 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2280 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2281 orig_visibility = DECL_VISIBILITY (decl);
2283 if (TREE_CODE (decl) == TYPE_DECL)
2285 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2286 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2287 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2288 use_template = 1;
2289 else
2290 use_template = 0;
2292 else if (DECL_LANG_SPECIFIC (decl))
2293 use_template = DECL_USE_TEMPLATE (decl);
2294 else
2295 use_template = 0;
2297 /* If DECL is a member of a class, visibility specifiers on the
2298 class can influence the visibility of the DECL. */
2299 if (DECL_CLASS_SCOPE_P (decl))
2300 class_type = DECL_CONTEXT (decl);
2301 else
2303 /* Not a class member. */
2305 /* Virtual tables have DECL_CONTEXT set to their associated class,
2306 so they are automatically handled above. */
2307 gcc_assert (!VAR_P (decl)
2308 || !DECL_VTABLE_OR_VTT_P (decl));
2310 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2312 /* Local statics and classes get the visibility of their
2313 containing function by default, except that
2314 -fvisibility-inlines-hidden doesn't affect them. */
2315 tree fn = DECL_CONTEXT (decl);
2316 if (DECL_VISIBILITY_SPECIFIED (fn))
2318 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2319 DECL_VISIBILITY_SPECIFIED (decl) =
2320 DECL_VISIBILITY_SPECIFIED (fn);
2322 else
2324 if (DECL_CLASS_SCOPE_P (fn))
2325 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2326 else if (determine_hidden_inline (fn))
2328 DECL_VISIBILITY (decl) = default_visibility;
2329 DECL_VISIBILITY_SPECIFIED (decl) =
2330 visibility_options.inpragma;
2332 else
2334 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2335 DECL_VISIBILITY_SPECIFIED (decl) =
2336 DECL_VISIBILITY_SPECIFIED (fn);
2340 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2341 but have no TEMPLATE_INFO, so don't try to check it. */
2342 use_template = 0;
2344 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2345 && flag_visibility_ms_compat)
2347 /* Under -fvisibility-ms-compat, types are visible by default,
2348 even though their contents aren't. */
2349 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2350 int underlying_vis = type_visibility (underlying_type);
2351 if (underlying_vis == VISIBILITY_ANON
2352 || (CLASS_TYPE_P (underlying_type)
2353 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2354 constrain_visibility (decl, underlying_vis, false);
2355 else
2356 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2358 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2360 /* tinfo visibility is based on the type it's for. */
2361 constrain_visibility
2362 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2364 /* Give the target a chance to override the visibility associated
2365 with DECL. */
2366 if (TREE_PUBLIC (decl)
2367 && !DECL_REALLY_EXTERN (decl)
2368 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2369 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2370 targetm.cxx.determine_class_data_visibility (decl);
2372 else if (use_template)
2373 /* Template instantiations and specializations get visibility based
2374 on their template unless they override it with an attribute. */;
2375 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2377 if (determine_hidden_inline (decl))
2378 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2379 else
2381 /* Set default visibility to whatever the user supplied with
2382 #pragma GCC visibility or a namespace visibility attribute. */
2383 DECL_VISIBILITY (decl) = default_visibility;
2384 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2389 if (use_template)
2391 /* If the specialization doesn't specify visibility, use the
2392 visibility from the template. */
2393 tree tinfo = get_template_info (decl);
2394 tree args = TI_ARGS (tinfo);
2395 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2396 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2397 : DECL_ATTRIBUTES (decl));
2399 if (args != error_mark_node)
2401 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2403 if (!DECL_VISIBILITY_SPECIFIED (decl))
2405 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2406 && determine_hidden_inline (decl))
2407 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2408 else
2410 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2411 DECL_VISIBILITY_SPECIFIED (decl)
2412 = DECL_VISIBILITY_SPECIFIED (pattern);
2416 if (args
2417 /* Template argument visibility outweighs #pragma or namespace
2418 visibility, but not an explicit attribute. */
2419 && !lookup_attribute ("visibility", attribs))
2421 int depth = TMPL_ARGS_DEPTH (args);
2422 if (DECL_VISIBILITY_SPECIFIED (decl))
2424 /* A class template member with explicit visibility
2425 overrides the class visibility, so we need to apply
2426 all the levels of template args directly. */
2427 int i;
2428 for (i = 1; i <= depth; ++i)
2430 tree lev = TMPL_ARGS_LEVEL (args, i);
2431 constrain_visibility_for_template (decl, lev);
2434 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2435 /* Limit visibility based on its template arguments. */
2436 constrain_visibility_for_template (decl, args);
2441 if (class_type)
2442 determine_visibility_from_class (decl, class_type);
2444 if (decl_anon_ns_mem_p (decl))
2445 /* Names in an anonymous namespace get internal linkage.
2446 This might change once we implement export. */
2447 constrain_visibility (decl, VISIBILITY_ANON, false);
2448 else if (TREE_CODE (decl) != TYPE_DECL)
2450 /* Propagate anonymity from type to decl. */
2451 int tvis = type_visibility (TREE_TYPE (decl));
2452 if (tvis == VISIBILITY_ANON
2453 || ! DECL_VISIBILITY_SPECIFIED (decl))
2454 constrain_visibility (decl, tvis, false);
2456 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2457 /* DR 757: A type without linkage shall not be used as the type of a
2458 variable or function with linkage, unless
2459 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2460 o the variable or function is not used (3.2 [basic.def.odr]) or is
2461 defined in the same translation unit.
2463 Since non-extern "C" decls need to be defined in the same
2464 translation unit, we can make the type internal. */
2465 constrain_visibility (decl, VISIBILITY_ANON, false);
2467 /* If visibility changed and DECL already has DECL_RTL, ensure
2468 symbol flags are updated. */
2469 if ((DECL_VISIBILITY (decl) != orig_visibility
2470 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2471 && ((VAR_P (decl) && TREE_STATIC (decl))
2472 || TREE_CODE (decl) == FUNCTION_DECL)
2473 && DECL_RTL_SET_P (decl))
2474 make_decl_rtl (decl);
2477 /* By default, static data members and function members receive
2478 the visibility of their containing class. */
2480 static void
2481 determine_visibility_from_class (tree decl, tree class_type)
2483 if (DECL_VISIBILITY_SPECIFIED (decl))
2484 return;
2486 if (determine_hidden_inline (decl))
2487 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2488 else
2490 /* Default to the class visibility. */
2491 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2492 DECL_VISIBILITY_SPECIFIED (decl)
2493 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2496 /* Give the target a chance to override the visibility associated
2497 with DECL. */
2498 if (VAR_P (decl)
2499 && (DECL_TINFO_P (decl)
2500 || (DECL_VTABLE_OR_VTT_P (decl)
2501 /* Construction virtual tables are not exported because
2502 they cannot be referred to from other object files;
2503 their name is not standardized by the ABI. */
2504 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2505 && TREE_PUBLIC (decl)
2506 && !DECL_REALLY_EXTERN (decl)
2507 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2508 targetm.cxx.determine_class_data_visibility (decl);
2511 /* Returns true iff DECL is an inline that should get hidden visibility
2512 because of -fvisibility-inlines-hidden. */
2514 static bool
2515 determine_hidden_inline (tree decl)
2517 return (visibility_options.inlines_hidden
2518 /* Don't do this for inline templates; specializations might not be
2519 inline, and we don't want them to inherit the hidden
2520 visibility. We'll set it here for all inline instantiations. */
2521 && !processing_template_decl
2522 && TREE_CODE (decl) == FUNCTION_DECL
2523 && DECL_DECLARED_INLINE_P (decl)
2524 && (! DECL_LANG_SPECIFIC (decl)
2525 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2528 /* Constrain the visibility of a class TYPE based on the visibility of its
2529 field types. Warn if any fields require lesser visibility. */
2531 void
2532 constrain_class_visibility (tree type)
2534 tree binfo;
2535 tree t;
2536 int i;
2538 int vis = type_visibility (type);
2540 if (vis == VISIBILITY_ANON
2541 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2542 return;
2544 /* Don't warn about visibility if the class has explicit visibility. */
2545 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2546 vis = VISIBILITY_INTERNAL;
2548 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2549 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2551 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2552 int subvis = type_visibility (ftype);
2554 if (subvis == VISIBILITY_ANON)
2556 if (!in_main_input_context ())
2557 warning (0, "\
2558 %qT has a field %qD whose type uses the anonymous namespace",
2559 type, t);
2561 else if (MAYBE_CLASS_TYPE_P (ftype)
2562 && vis < VISIBILITY_HIDDEN
2563 && subvis >= VISIBILITY_HIDDEN)
2564 warning (OPT_Wattributes, "\
2565 %qT declared with greater visibility than the type of its field %qD",
2566 type, t);
2569 binfo = TYPE_BINFO (type);
2570 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2572 int subvis = type_visibility (TREE_TYPE (t));
2574 if (subvis == VISIBILITY_ANON)
2576 if (!in_main_input_context())
2577 warning (0, "\
2578 %qT has a base %qT whose type uses the anonymous namespace",
2579 type, TREE_TYPE (t));
2581 else if (vis < VISIBILITY_HIDDEN
2582 && subvis >= VISIBILITY_HIDDEN)
2583 warning (OPT_Wattributes, "\
2584 %qT declared with greater visibility than its base %qT",
2585 type, TREE_TYPE (t));
2589 /* Functions for adjusting the visibility of a tagged type and its nested
2590 types and declarations when it gets a name for linkage purposes from a
2591 typedef. */
2593 static void bt_reset_linkage_1 (binding_entry, void *);
2594 static void bt_reset_linkage_2 (binding_entry, void *);
2596 /* First reset the visibility of all the types. */
2598 static void
2599 reset_type_linkage_1 (tree type)
2601 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2602 if (CLASS_TYPE_P (type))
2603 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2604 bt_reset_linkage_1, NULL);
2606 static void
2607 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2609 reset_type_linkage_1 (b->type);
2612 /* Then reset the visibility of any static data members or member
2613 functions that use those types. */
2615 static void
2616 reset_decl_linkage (tree decl)
2618 if (TREE_PUBLIC (decl))
2619 return;
2620 if (DECL_CLONED_FUNCTION_P (decl))
2621 return;
2622 TREE_PUBLIC (decl) = true;
2623 DECL_INTERFACE_KNOWN (decl) = false;
2624 determine_visibility (decl);
2625 tentative_decl_linkage (decl);
2627 static void
2628 reset_type_linkage_2 (tree type)
2630 if (CLASS_TYPE_P (type))
2632 if (tree vt = CLASSTYPE_VTABLES (type))
2634 tree name = mangle_vtbl_for_type (type);
2635 DECL_NAME (vt) = name;
2636 SET_DECL_ASSEMBLER_NAME (vt, name);
2637 reset_decl_linkage (vt);
2639 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2641 tree name = mangle_typeinfo_for_type (type);
2642 DECL_NAME (ti) = name;
2643 SET_DECL_ASSEMBLER_NAME (ti, name);
2644 TREE_TYPE (name) = type;
2645 reset_decl_linkage (ti);
2647 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2648 if (TREE_CODE (m) == VAR_DECL)
2649 reset_decl_linkage (m);
2650 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2652 reset_decl_linkage (m);
2653 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (m))
2654 /* Also update its name, for cxx_dwarf_name. */
2655 DECL_NAME (m) = TYPE_IDENTIFIER (type);
2657 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2658 bt_reset_linkage_2, NULL);
2661 static void
2662 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2664 reset_type_linkage_2 (b->type);
2666 void
2667 reset_type_linkage (tree type)
2669 reset_type_linkage_1 (type);
2670 reset_type_linkage_2 (type);
2673 /* Set up our initial idea of what the linkage of DECL should be. */
2675 void
2676 tentative_decl_linkage (tree decl)
2678 if (DECL_INTERFACE_KNOWN (decl))
2679 /* We've already made a decision as to how this function will
2680 be handled. */;
2681 else if (vague_linkage_p (decl))
2683 if (TREE_CODE (decl) == FUNCTION_DECL
2684 && decl_defined_p (decl))
2686 DECL_EXTERNAL (decl) = 1;
2687 DECL_NOT_REALLY_EXTERN (decl) = 1;
2688 note_vague_linkage_fn (decl);
2689 /* A non-template inline function with external linkage will
2690 always be COMDAT. As we must eventually determine the
2691 linkage of all functions, and as that causes writes to
2692 the data mapped in from the PCH file, it's advantageous
2693 to mark the functions at this point. */
2694 if (DECL_DECLARED_INLINE_P (decl)
2695 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2696 || DECL_DEFAULTED_FN (decl)))
2698 /* This function must have external linkage, as
2699 otherwise DECL_INTERFACE_KNOWN would have been
2700 set. */
2701 gcc_assert (TREE_PUBLIC (decl));
2702 comdat_linkage (decl);
2703 DECL_INTERFACE_KNOWN (decl) = 1;
2706 else if (TREE_CODE (decl) == VAR_DECL)
2707 maybe_commonize_var (decl);
2711 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2712 for DECL has not already been determined, do so now by setting
2713 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2714 function is called entities with vague linkage whose definitions
2715 are available must have TREE_PUBLIC set.
2717 If this function decides to place DECL in COMDAT, it will set
2718 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2719 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2720 callers defer that decision until it is clear that DECL is actually
2721 required. */
2723 void
2724 import_export_decl (tree decl)
2726 int emit_p;
2727 bool comdat_p;
2728 bool import_p;
2729 tree class_type = NULL_TREE;
2731 if (DECL_INTERFACE_KNOWN (decl))
2732 return;
2734 /* We cannot determine what linkage to give to an entity with vague
2735 linkage until the end of the file. For example, a virtual table
2736 for a class will be defined if and only if the key method is
2737 defined in this translation unit. As a further example, consider
2738 that when compiling a translation unit that uses PCH file with
2739 "-frepo" it would be incorrect to make decisions about what
2740 entities to emit when building the PCH; those decisions must be
2741 delayed until the repository information has been processed. */
2742 gcc_assert (at_eof);
2743 /* Object file linkage for explicit instantiations is handled in
2744 mark_decl_instantiated. For static variables in functions with
2745 vague linkage, maybe_commonize_var is used.
2747 Therefore, the only declarations that should be provided to this
2748 function are those with external linkage that are:
2750 * implicit instantiations of function templates
2752 * inline function
2754 * implicit instantiations of static data members of class
2755 templates
2757 * virtual tables
2759 * typeinfo objects
2761 Furthermore, all entities that reach this point must have a
2762 definition available in this translation unit.
2764 The following assertions check these conditions. */
2765 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2766 /* Any code that creates entities with TREE_PUBLIC cleared should
2767 also set DECL_INTERFACE_KNOWN. */
2768 gcc_assert (TREE_PUBLIC (decl));
2769 if (TREE_CODE (decl) == FUNCTION_DECL)
2770 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2771 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2772 || DECL_DECLARED_INLINE_P (decl));
2773 else
2774 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2775 || DECL_VTABLE_OR_VTT_P (decl)
2776 || DECL_TINFO_P (decl));
2777 /* Check that a definition of DECL is available in this translation
2778 unit. */
2779 gcc_assert (!DECL_REALLY_EXTERN (decl));
2781 /* Assume that DECL will not have COMDAT linkage. */
2782 comdat_p = false;
2783 /* Assume that DECL will not be imported into this translation
2784 unit. */
2785 import_p = false;
2787 /* See if the repository tells us whether or not to emit DECL in
2788 this translation unit. */
2789 emit_p = repo_emit_p (decl);
2790 if (emit_p == 0)
2791 import_p = true;
2792 else if (emit_p == 1)
2794 /* The repository indicates that this entity should be defined
2795 here. Make sure the back end honors that request. */
2796 mark_needed (decl);
2797 /* Output the definition as an ordinary strong definition. */
2798 DECL_EXTERNAL (decl) = 0;
2799 DECL_INTERFACE_KNOWN (decl) = 1;
2800 return;
2803 if (import_p)
2804 /* We have already decided what to do with this DECL; there is no
2805 need to check anything further. */
2807 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2809 class_type = DECL_CONTEXT (decl);
2810 import_export_class (class_type);
2811 if (TYPE_FOR_JAVA (class_type))
2812 import_p = true;
2813 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2814 && CLASSTYPE_INTERFACE_ONLY (class_type))
2815 import_p = true;
2816 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2817 && !CLASSTYPE_USE_TEMPLATE (class_type)
2818 && CLASSTYPE_KEY_METHOD (class_type)
2819 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2820 /* The ABI requires that all virtual tables be emitted with
2821 COMDAT linkage. However, on systems where COMDAT symbols
2822 don't show up in the table of contents for a static
2823 archive, or on systems without weak symbols (where we
2824 approximate COMDAT linkage by using internal linkage), the
2825 linker will report errors about undefined symbols because
2826 it will not see the virtual table definition. Therefore,
2827 in the case that we know that the virtual table will be
2828 emitted in only one translation unit, we make the virtual
2829 table an ordinary definition with external linkage. */
2830 DECL_EXTERNAL (decl) = 0;
2831 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2833 /* CLASS_TYPE is being exported from this translation unit,
2834 so DECL should be defined here. */
2835 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2836 /* If a class is declared in a header with the "extern
2837 template" extension, then it will not be instantiated,
2838 even in translation units that would normally require
2839 it. Often such classes are explicitly instantiated in
2840 one translation unit. Therefore, the explicit
2841 instantiation must be made visible to other translation
2842 units. */
2843 DECL_EXTERNAL (decl) = 0;
2844 else
2846 /* The generic C++ ABI says that class data is always
2847 COMDAT, even if there is a key function. Some
2848 variants (e.g., the ARM EABI) says that class data
2849 only has COMDAT linkage if the class data might be
2850 emitted in more than one translation unit. When the
2851 key method can be inline and is inline, we still have
2852 to arrange for comdat even though
2853 class_data_always_comdat is false. */
2854 if (!CLASSTYPE_KEY_METHOD (class_type)
2855 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2856 || targetm.cxx.class_data_always_comdat ())
2858 /* The ABI requires COMDAT linkage. Normally, we
2859 only emit COMDAT things when they are needed;
2860 make sure that we realize that this entity is
2861 indeed needed. */
2862 comdat_p = true;
2863 mark_needed (decl);
2867 else if (!flag_implicit_templates
2868 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2869 import_p = true;
2870 else
2871 comdat_p = true;
2873 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2875 tree type = TREE_TYPE (DECL_NAME (decl));
2876 if (CLASS_TYPE_P (type))
2878 class_type = type;
2879 import_export_class (type);
2880 if (CLASSTYPE_INTERFACE_KNOWN (type)
2881 && TYPE_POLYMORPHIC_P (type)
2882 && CLASSTYPE_INTERFACE_ONLY (type)
2883 /* If -fno-rtti was specified, then we cannot be sure
2884 that RTTI information will be emitted with the
2885 virtual table of the class, so we must emit it
2886 wherever it is used. */
2887 && flag_rtti)
2888 import_p = true;
2889 else
2891 if (CLASSTYPE_INTERFACE_KNOWN (type)
2892 && !CLASSTYPE_INTERFACE_ONLY (type))
2894 comdat_p = (targetm.cxx.class_data_always_comdat ()
2895 || (CLASSTYPE_KEY_METHOD (type)
2896 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2897 mark_needed (decl);
2898 if (!flag_weak)
2900 comdat_p = false;
2901 DECL_EXTERNAL (decl) = 0;
2904 else
2905 comdat_p = true;
2908 else
2909 comdat_p = true;
2911 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2913 /* DECL is an implicit instantiation of a function or static
2914 data member. */
2915 if ((flag_implicit_templates
2916 && !flag_use_repository)
2917 || (flag_implicit_inline_templates
2918 && TREE_CODE (decl) == FUNCTION_DECL
2919 && DECL_DECLARED_INLINE_P (decl)))
2920 comdat_p = true;
2921 else
2922 /* If we are not implicitly generating templates, then mark
2923 this entity as undefined in this translation unit. */
2924 import_p = true;
2926 else if (DECL_FUNCTION_MEMBER_P (decl))
2928 if (!DECL_DECLARED_INLINE_P (decl))
2930 tree ctype = DECL_CONTEXT (decl);
2931 import_export_class (ctype);
2932 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2934 DECL_NOT_REALLY_EXTERN (decl)
2935 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2936 || (DECL_DECLARED_INLINE_P (decl)
2937 && ! flag_implement_inlines
2938 && !DECL_VINDEX (decl)));
2940 if (!DECL_NOT_REALLY_EXTERN (decl))
2941 DECL_EXTERNAL (decl) = 1;
2943 /* Always make artificials weak. */
2944 if (DECL_ARTIFICIAL (decl) && flag_weak)
2945 comdat_p = true;
2946 else
2947 maybe_make_one_only (decl);
2950 else
2951 comdat_p = true;
2953 else
2954 comdat_p = true;
2956 if (import_p)
2958 /* If we are importing DECL into this translation unit, mark is
2959 an undefined here. */
2960 DECL_EXTERNAL (decl) = 1;
2961 DECL_NOT_REALLY_EXTERN (decl) = 0;
2963 else if (comdat_p)
2965 /* If we decided to put DECL in COMDAT, mark it accordingly at
2966 this point. */
2967 comdat_linkage (decl);
2970 DECL_INTERFACE_KNOWN (decl) = 1;
2973 /* Return an expression that performs the destruction of DECL, which
2974 must be a VAR_DECL whose type has a non-trivial destructor, or is
2975 an array whose (innermost) elements have a non-trivial destructor. */
2977 tree
2978 build_cleanup (tree decl)
2980 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2981 gcc_assert (clean != NULL_TREE);
2982 return clean;
2985 /* Returns the initialization guard variable for the variable DECL,
2986 which has static storage duration. */
2988 tree
2989 get_guard (tree decl)
2991 tree sname;
2992 tree guard;
2994 sname = mangle_guard_variable (decl);
2995 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2996 if (! guard)
2998 tree guard_type;
3000 /* We use a type that is big enough to contain a mutex as well
3001 as an integer counter. */
3002 guard_type = targetm.cxx.guard_type ();
3003 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3004 VAR_DECL, sname, guard_type);
3006 /* The guard should have the same linkage as what it guards. */
3007 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3008 TREE_STATIC (guard) = TREE_STATIC (decl);
3009 DECL_COMMON (guard) = DECL_COMMON (decl);
3010 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3011 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3012 if (DECL_ONE_ONLY (decl))
3013 make_decl_one_only (guard, cxx_comdat_group (guard));
3014 if (TREE_PUBLIC (decl))
3015 DECL_WEAK (guard) = DECL_WEAK (decl);
3016 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3017 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3019 DECL_ARTIFICIAL (guard) = 1;
3020 DECL_IGNORED_P (guard) = 1;
3021 TREE_USED (guard) = 1;
3022 pushdecl_top_level_and_finish (guard, NULL_TREE);
3024 return guard;
3027 /* Return an atomic load of src with the appropriate memory model. */
3029 static tree
3030 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3032 tree ptr_type = build_pointer_type (char_type_node);
3033 tree mem_model = build_int_cst (integer_type_node, model);
3034 tree t, addr, val;
3035 unsigned int size;
3036 int fncode;
3038 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3040 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3041 t = builtin_decl_implicit ((enum built_in_function) fncode);
3043 addr = build1 (ADDR_EXPR, ptr_type, src);
3044 val = build_call_expr (t, 2, addr, mem_model);
3045 return val;
3048 /* Return those bits of the GUARD variable that should be set when the
3049 guarded entity is actually initialized. */
3051 static tree
3052 get_guard_bits (tree guard)
3054 if (!targetm.cxx.guard_mask_bit ())
3056 /* We only set the first byte of the guard, in order to leave room
3057 for a mutex in the high-order bits. */
3058 guard = build1 (ADDR_EXPR,
3059 build_pointer_type (TREE_TYPE (guard)),
3060 guard);
3061 guard = build1 (NOP_EXPR,
3062 build_pointer_type (char_type_node),
3063 guard);
3064 guard = build1 (INDIRECT_REF, char_type_node, guard);
3067 return guard;
3070 /* Return an expression which determines whether or not the GUARD
3071 variable has already been initialized. */
3073 tree
3074 get_guard_cond (tree guard, bool thread_safe)
3076 tree guard_value;
3078 if (!thread_safe)
3079 guard = get_guard_bits (guard);
3080 else
3081 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3083 /* Mask off all but the low bit. */
3084 if (targetm.cxx.guard_mask_bit ())
3086 guard_value = integer_one_node;
3087 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3088 guard_value = convert (TREE_TYPE (guard), guard_value);
3089 guard = cp_build_binary_op (input_location,
3090 BIT_AND_EXPR, guard, guard_value,
3091 tf_warning_or_error);
3094 guard_value = integer_zero_node;
3095 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3096 guard_value = convert (TREE_TYPE (guard), guard_value);
3097 return cp_build_binary_op (input_location,
3098 EQ_EXPR, guard, guard_value,
3099 tf_warning_or_error);
3102 /* Return an expression which sets the GUARD variable, indicating that
3103 the variable being guarded has been initialized. */
3105 tree
3106 set_guard (tree guard)
3108 tree guard_init;
3110 /* Set the GUARD to one. */
3111 guard = get_guard_bits (guard);
3112 guard_init = integer_one_node;
3113 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3114 guard_init = convert (TREE_TYPE (guard), guard_init);
3115 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3116 tf_warning_or_error);
3119 /* Returns true iff we can tell that VAR does not have a dynamic
3120 initializer. */
3122 static bool
3123 var_defined_without_dynamic_init (tree var)
3125 /* If it's defined in another TU, we can't tell. */
3126 if (DECL_EXTERNAL (var))
3127 return false;
3128 /* If it has a non-trivial destructor, registering the destructor
3129 counts as dynamic initialization. */
3130 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3131 return false;
3132 /* If it's in this TU, its initializer has been processed, unless
3133 it's a case of self-initialization, then DECL_INITIALIZED_P is
3134 false while the initializer is handled by finish_id_expression. */
3135 if (!DECL_INITIALIZED_P (var))
3136 return false;
3137 /* If it has no initializer or a constant one, it's not dynamic. */
3138 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3139 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3142 /* Returns true iff VAR is a variable that needs uses to be
3143 wrapped for possible dynamic initialization. */
3145 static bool
3146 var_needs_tls_wrapper (tree var)
3148 return (!error_operand_p (var)
3149 && DECL_THREAD_LOCAL_P (var)
3150 && !DECL_GNU_TLS_P (var)
3151 && !DECL_FUNCTION_SCOPE_P (var)
3152 && !var_defined_without_dynamic_init (var));
3155 /* Get the FUNCTION_DECL for the shared TLS init function for this
3156 translation unit. */
3158 static tree
3159 get_local_tls_init_fn (void)
3161 tree sname = get_identifier ("__tls_init");
3162 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3163 if (!fn)
3165 fn = build_lang_decl (FUNCTION_DECL, sname,
3166 build_function_type (void_type_node,
3167 void_list_node));
3168 SET_DECL_LANGUAGE (fn, lang_c);
3169 TREE_PUBLIC (fn) = false;
3170 DECL_ARTIFICIAL (fn) = true;
3171 mark_used (fn);
3172 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3174 return fn;
3177 /* Get a FUNCTION_DECL for the init function for the thread_local
3178 variable VAR. The init function will be an alias to the function
3179 that initializes all the non-local TLS variables in the translation
3180 unit. The init function is only used by the wrapper function. */
3182 static tree
3183 get_tls_init_fn (tree var)
3185 /* Only C++11 TLS vars need this init fn. */
3186 if (!var_needs_tls_wrapper (var))
3187 return NULL_TREE;
3189 /* If -fno-extern-tls-init, assume that we don't need to call
3190 a tls init function for a variable defined in another TU. */
3191 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3192 return NULL_TREE;
3194 #ifdef ASM_OUTPUT_DEF
3195 /* If the variable is internal, or if we can't generate aliases,
3196 call the local init function directly. */
3197 if (!TREE_PUBLIC (var))
3198 #endif
3199 return get_local_tls_init_fn ();
3201 tree sname = mangle_tls_init_fn (var);
3202 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3203 if (!fn)
3205 fn = build_lang_decl (FUNCTION_DECL, sname,
3206 build_function_type (void_type_node,
3207 void_list_node));
3208 SET_DECL_LANGUAGE (fn, lang_c);
3209 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3210 DECL_ARTIFICIAL (fn) = true;
3211 DECL_COMDAT (fn) = DECL_COMDAT (var);
3212 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3213 if (DECL_ONE_ONLY (var))
3214 make_decl_one_only (fn, cxx_comdat_group (fn));
3215 if (TREE_PUBLIC (var))
3217 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3218 /* If the variable is defined somewhere else and might have static
3219 initialization, make the init function a weak reference. */
3220 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3221 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3222 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3223 && DECL_EXTERNAL (var))
3224 declare_weak (fn);
3225 else
3226 DECL_WEAK (fn) = DECL_WEAK (var);
3228 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3229 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3230 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3231 DECL_IGNORED_P (fn) = 1;
3232 mark_used (fn);
3234 DECL_BEFRIENDING_CLASSES (fn) = var;
3236 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3238 return fn;
3241 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3242 variable VAR. The wrapper function calls the init function (if any) for
3243 VAR and then returns a reference to VAR. The wrapper function is used
3244 in place of VAR everywhere VAR is mentioned. */
3246 tree
3247 get_tls_wrapper_fn (tree var)
3249 /* Only C++11 TLS vars need this wrapper fn. */
3250 if (!var_needs_tls_wrapper (var))
3251 return NULL_TREE;
3253 tree sname = mangle_tls_wrapper_fn (var);
3254 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3255 if (!fn)
3257 /* A named rvalue reference is an lvalue, so the wrapper should
3258 always return an lvalue reference. */
3259 tree type = non_reference (TREE_TYPE (var));
3260 type = build_reference_type (type);
3261 tree fntype = build_function_type (type, void_list_node);
3262 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3263 SET_DECL_LANGUAGE (fn, lang_c);
3264 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3265 DECL_ARTIFICIAL (fn) = true;
3266 DECL_IGNORED_P (fn) = 1;
3267 /* The wrapper is inline and emitted everywhere var is used. */
3268 DECL_DECLARED_INLINE_P (fn) = true;
3269 if (TREE_PUBLIC (var))
3271 comdat_linkage (fn);
3272 #ifdef HAVE_GAS_HIDDEN
3273 /* Make the wrapper bind locally; there's no reason to share
3274 the wrapper between multiple shared objects. */
3275 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3276 DECL_VISIBILITY_SPECIFIED (fn) = true;
3277 #endif
3279 if (!TREE_PUBLIC (fn))
3280 DECL_INTERFACE_KNOWN (fn) = true;
3281 mark_used (fn);
3282 note_vague_linkage_fn (fn);
3284 #if 0
3285 /* We want CSE to commonize calls to the wrapper, but marking it as
3286 pure is unsafe since it has side-effects. I guess we need a new
3287 ECF flag even weaker than ECF_PURE. FIXME! */
3288 DECL_PURE_P (fn) = true;
3289 #endif
3291 DECL_BEFRIENDING_CLASSES (fn) = var;
3293 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3295 return fn;
3298 /* At EOF, generate the definition for the TLS wrapper function FN:
3300 T& var_wrapper() {
3301 if (init_fn) init_fn();
3302 return var;
3303 } */
3305 static void
3306 generate_tls_wrapper (tree fn)
3308 tree var = DECL_BEFRIENDING_CLASSES (fn);
3310 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3311 tree body = begin_function_body ();
3312 /* Only call the init fn if there might be one. */
3313 if (tree init_fn = get_tls_init_fn (var))
3315 tree if_stmt = NULL_TREE;
3316 /* If init_fn is a weakref, make sure it exists before calling. */
3317 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3319 if_stmt = begin_if_stmt ();
3320 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3321 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3322 NE_EXPR, addr, nullptr_node,
3323 tf_warning_or_error);
3324 finish_if_stmt_cond (cond, if_stmt);
3326 finish_expr_stmt (build_cxx_call
3327 (init_fn, 0, NULL, tf_warning_or_error));
3328 if (if_stmt)
3330 finish_then_clause (if_stmt);
3331 finish_if_stmt (if_stmt);
3334 else
3335 /* If there's no initialization, the wrapper is a constant function. */
3336 TREE_READONLY (fn) = true;
3337 finish_return_stmt (convert_from_reference (var));
3338 finish_function_body (body);
3339 expand_or_defer_fn (finish_function (0));
3342 /* Start the process of running a particular set of global constructors
3343 or destructors. Subroutine of do_[cd]tors. Also called from
3344 vtv_start_verification_constructor_init_function. */
3346 static tree
3347 start_objects (int method_type, int initp)
3349 tree body;
3350 tree fndecl;
3351 char type[14];
3353 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3355 if (initp != DEFAULT_INIT_PRIORITY)
3357 char joiner;
3359 #ifdef JOINER
3360 joiner = JOINER;
3361 #else
3362 joiner = '_';
3363 #endif
3365 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3367 else
3368 sprintf (type, "sub_%c", method_type);
3370 fndecl = build_lang_decl (FUNCTION_DECL,
3371 get_file_function_name (type),
3372 build_function_type_list (void_type_node,
3373 NULL_TREE));
3374 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3376 TREE_PUBLIC (current_function_decl) = 0;
3378 /* Mark as artificial because it's not explicitly in the user's
3379 source code. */
3380 DECL_ARTIFICIAL (current_function_decl) = 1;
3382 /* Mark this declaration as used to avoid spurious warnings. */
3383 TREE_USED (current_function_decl) = 1;
3385 /* Mark this function as a global constructor or destructor. */
3386 if (method_type == 'I')
3387 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3388 else
3389 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3391 body = begin_compound_stmt (BCS_FN_BODY);
3393 return body;
3396 /* Finish the process of running a particular set of global constructors
3397 or destructors. Subroutine of do_[cd]tors. */
3399 static void
3400 finish_objects (int method_type, int initp, tree body)
3402 tree fn;
3404 /* Finish up. */
3405 finish_compound_stmt (body);
3406 fn = finish_function (0);
3408 if (method_type == 'I')
3410 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3411 decl_init_priority_insert (fn, initp);
3413 else
3415 DECL_STATIC_DESTRUCTOR (fn) = 1;
3416 decl_fini_priority_insert (fn, initp);
3419 expand_or_defer_fn (fn);
3422 /* The names of the parameters to the function created to handle
3423 initializations and destructions for objects with static storage
3424 duration. */
3425 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3426 #define PRIORITY_IDENTIFIER "__priority"
3428 /* The name of the function we create to handle initializations and
3429 destructions for objects with static storage duration. */
3430 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3432 /* The declaration for the __INITIALIZE_P argument. */
3433 static GTY(()) tree initialize_p_decl;
3435 /* The declaration for the __PRIORITY argument. */
3436 static GTY(()) tree priority_decl;
3438 /* The declaration for the static storage duration function. */
3439 static GTY(()) tree ssdf_decl;
3441 /* All the static storage duration functions created in this
3442 translation unit. */
3443 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3445 /* A map from priority levels to information about that priority
3446 level. There may be many such levels, so efficient lookup is
3447 important. */
3448 static splay_tree priority_info_map;
3450 /* Begins the generation of the function that will handle all
3451 initialization and destruction of objects with static storage
3452 duration. The function generated takes two parameters of type
3453 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3454 nonzero, it performs initializations. Otherwise, it performs
3455 destructions. It only performs those initializations or
3456 destructions with the indicated __PRIORITY. The generated function
3457 returns no value.
3459 It is assumed that this function will only be called once per
3460 translation unit. */
3462 static tree
3463 start_static_storage_duration_function (unsigned count)
3465 tree type;
3466 tree body;
3467 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3469 /* Create the identifier for this function. It will be of the form
3470 SSDF_IDENTIFIER_<number>. */
3471 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3473 type = build_function_type_list (void_type_node,
3474 integer_type_node, integer_type_node,
3475 NULL_TREE);
3477 /* Create the FUNCTION_DECL itself. */
3478 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3479 get_identifier (id),
3480 type);
3481 TREE_PUBLIC (ssdf_decl) = 0;
3482 DECL_ARTIFICIAL (ssdf_decl) = 1;
3484 /* Put this function in the list of functions to be called from the
3485 static constructors and destructors. */
3486 if (!ssdf_decls)
3488 vec_alloc (ssdf_decls, 32);
3490 /* Take this opportunity to initialize the map from priority
3491 numbers to information about that priority level. */
3492 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3493 /*delete_key_fn=*/0,
3494 /*delete_value_fn=*/
3495 (splay_tree_delete_value_fn) &free);
3497 /* We always need to generate functions for the
3498 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3499 priorities later, we'll be sure to find the
3500 DEFAULT_INIT_PRIORITY. */
3501 get_priority_info (DEFAULT_INIT_PRIORITY);
3504 vec_safe_push (ssdf_decls, ssdf_decl);
3506 /* Create the argument list. */
3507 initialize_p_decl = cp_build_parm_decl
3508 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3509 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3510 TREE_USED (initialize_p_decl) = 1;
3511 priority_decl = cp_build_parm_decl
3512 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3513 DECL_CONTEXT (priority_decl) = ssdf_decl;
3514 TREE_USED (priority_decl) = 1;
3516 DECL_CHAIN (initialize_p_decl) = priority_decl;
3517 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3519 /* Put the function in the global scope. */
3520 pushdecl (ssdf_decl);
3522 /* Start the function itself. This is equivalent to declaring the
3523 function as:
3525 static void __ssdf (int __initialize_p, init __priority_p);
3527 It is static because we only need to call this function from the
3528 various constructor and destructor functions for this module. */
3529 start_preparsed_function (ssdf_decl,
3530 /*attrs=*/NULL_TREE,
3531 SF_PRE_PARSED);
3533 /* Set up the scope of the outermost block in the function. */
3534 body = begin_compound_stmt (BCS_FN_BODY);
3536 return body;
3539 /* Finish the generation of the function which performs initialization
3540 and destruction of objects with static storage duration. After
3541 this point, no more such objects can be created. */
3543 static void
3544 finish_static_storage_duration_function (tree body)
3546 /* Close out the function. */
3547 finish_compound_stmt (body);
3548 expand_or_defer_fn (finish_function (0));
3551 /* Return the information about the indicated PRIORITY level. If no
3552 code to handle this level has yet been generated, generate the
3553 appropriate prologue. */
3555 static priority_info
3556 get_priority_info (int priority)
3558 priority_info pi;
3559 splay_tree_node n;
3561 n = splay_tree_lookup (priority_info_map,
3562 (splay_tree_key) priority);
3563 if (!n)
3565 /* Create a new priority information structure, and insert it
3566 into the map. */
3567 pi = XNEW (struct priority_info_s);
3568 pi->initializations_p = 0;
3569 pi->destructions_p = 0;
3570 splay_tree_insert (priority_info_map,
3571 (splay_tree_key) priority,
3572 (splay_tree_value) pi);
3574 else
3575 pi = (priority_info) n->value;
3577 return pi;
3580 /* The effective initialization priority of a DECL. */
3582 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3583 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3584 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3586 /* Whether a DECL needs a guard to protect it against multiple
3587 initialization. */
3589 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3590 || DECL_ONE_ONLY (decl) \
3591 || DECL_WEAK (decl)))
3593 /* Called from one_static_initialization_or_destruction(),
3594 via walk_tree.
3595 Walks the initializer list of a global variable and looks for
3596 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3597 and that have their DECL_CONTEXT() == NULL.
3598 For each such temporary variable, set their DECL_CONTEXT() to
3599 the current function. This is necessary because otherwise
3600 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3601 when trying to refer to a temporary variable that does not have
3602 it's DECL_CONTECT() properly set. */
3603 static tree
3604 fix_temporary_vars_context_r (tree *node,
3605 int * /*unused*/,
3606 void * /*unused1*/)
3608 gcc_assert (current_function_decl);
3610 if (TREE_CODE (*node) == BIND_EXPR)
3612 tree var;
3614 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3615 if (VAR_P (var)
3616 && !DECL_NAME (var)
3617 && DECL_ARTIFICIAL (var)
3618 && !DECL_CONTEXT (var))
3619 DECL_CONTEXT (var) = current_function_decl;
3622 return NULL_TREE;
3625 /* Set up to handle the initialization or destruction of DECL. If
3626 INITP is nonzero, we are initializing the variable. Otherwise, we
3627 are destroying it. */
3629 static void
3630 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3632 tree guard_if_stmt = NULL_TREE;
3633 tree guard;
3635 /* If we are supposed to destruct and there's a trivial destructor,
3636 nothing has to be done. */
3637 if (!initp
3638 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3639 return;
3641 /* Trick the compiler into thinking we are at the file and line
3642 where DECL was declared so that error-messages make sense, and so
3643 that the debugger will show somewhat sensible file and line
3644 information. */
3645 input_location = DECL_SOURCE_LOCATION (decl);
3647 /* Make sure temporary variables in the initialiser all have
3648 their DECL_CONTEXT() set to a value different from NULL_TREE.
3649 This can happen when global variables initialisers are built.
3650 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3651 the temporary variables that might have been generated in the
3652 accompagning initialisers is NULL_TREE, meaning the variables have been
3653 declared in the global namespace.
3654 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3655 of the temporaries are set to the current function decl. */
3656 cp_walk_tree_without_duplicates (&init,
3657 fix_temporary_vars_context_r,
3658 NULL);
3660 /* Because of:
3662 [class.access.spec]
3664 Access control for implicit calls to the constructors,
3665 the conversion functions, or the destructor called to
3666 create and destroy a static data member is performed as
3667 if these calls appeared in the scope of the member's
3668 class.
3670 we pretend we are in a static member function of the class of
3671 which the DECL is a member. */
3672 if (member_p (decl))
3674 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3675 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3678 /* Assume we don't need a guard. */
3679 guard = NULL_TREE;
3680 /* We need a guard if this is an object with external linkage that
3681 might be initialized in more than one place. (For example, a
3682 static data member of a template, when the data member requires
3683 construction.) */
3684 if (NEEDS_GUARD_P (decl))
3686 tree guard_cond;
3688 guard = get_guard (decl);
3690 /* When using __cxa_atexit, we just check the GUARD as we would
3691 for a local static. */
3692 if (flag_use_cxa_atexit)
3694 /* When using __cxa_atexit, we never try to destroy
3695 anything from a static destructor. */
3696 gcc_assert (initp);
3697 guard_cond = get_guard_cond (guard, false);
3699 /* If we don't have __cxa_atexit, then we will be running
3700 destructors from .fini sections, or their equivalents. So,
3701 we need to know how many times we've tried to initialize this
3702 object. We do initializations only if the GUARD is zero,
3703 i.e., if we are the first to initialize the variable. We do
3704 destructions only if the GUARD is one, i.e., if we are the
3705 last to destroy the variable. */
3706 else if (initp)
3707 guard_cond
3708 = cp_build_binary_op (input_location,
3709 EQ_EXPR,
3710 cp_build_unary_op (PREINCREMENT_EXPR,
3711 guard,
3712 /*noconvert=*/1,
3713 tf_warning_or_error),
3714 integer_one_node,
3715 tf_warning_or_error);
3716 else
3717 guard_cond
3718 = cp_build_binary_op (input_location,
3719 EQ_EXPR,
3720 cp_build_unary_op (PREDECREMENT_EXPR,
3721 guard,
3722 /*noconvert=*/1,
3723 tf_warning_or_error),
3724 integer_zero_node,
3725 tf_warning_or_error);
3727 guard_if_stmt = begin_if_stmt ();
3728 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3732 /* If we're using __cxa_atexit, we have not already set the GUARD,
3733 so we must do so now. */
3734 if (guard && initp && flag_use_cxa_atexit)
3735 finish_expr_stmt (set_guard (guard));
3737 /* Perform the initialization or destruction. */
3738 if (initp)
3740 if (init)
3742 finish_expr_stmt (init);
3743 if (flag_sanitize & SANITIZE_ADDRESS)
3745 varpool_node *vnode = varpool_node::get (decl);
3746 if (vnode)
3747 vnode->dynamically_initialized = 1;
3751 /* If we're using __cxa_atexit, register a function that calls the
3752 destructor for the object. */
3753 if (flag_use_cxa_atexit)
3754 finish_expr_stmt (register_dtor_fn (decl));
3756 else
3757 finish_expr_stmt (build_cleanup (decl));
3759 /* Finish the guard if-stmt, if necessary. */
3760 if (guard)
3762 finish_then_clause (guard_if_stmt);
3763 finish_if_stmt (guard_if_stmt);
3766 /* Now that we're done with DECL we don't need to pretend to be a
3767 member of its class any longer. */
3768 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3769 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3772 /* Generate code to do the initialization or destruction of the decls in VARS,
3773 a TREE_LIST of VAR_DECL with static storage duration.
3774 Whether initialization or destruction is performed is specified by INITP. */
3776 static void
3777 do_static_initialization_or_destruction (tree vars, bool initp)
3779 tree node, init_if_stmt, cond;
3781 /* Build the outer if-stmt to check for initialization or destruction. */
3782 init_if_stmt = begin_if_stmt ();
3783 cond = initp ? integer_one_node : integer_zero_node;
3784 cond = cp_build_binary_op (input_location,
3785 EQ_EXPR,
3786 initialize_p_decl,
3787 cond,
3788 tf_warning_or_error);
3789 finish_if_stmt_cond (cond, init_if_stmt);
3791 /* To make sure dynamic construction doesn't access globals from other
3792 compilation units where they might not be yet constructed, for
3793 -fsanitize=address insert __asan_before_dynamic_init call that
3794 prevents access to either all global variables that need construction
3795 in other compilation units, or at least those that haven't been
3796 initialized yet. Variables that need dynamic construction in
3797 the current compilation unit are kept accessible. */
3798 if (flag_sanitize & SANITIZE_ADDRESS)
3799 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3801 node = vars;
3802 do {
3803 tree decl = TREE_VALUE (node);
3804 tree priority_if_stmt;
3805 int priority;
3806 priority_info pi;
3808 /* If we don't need a destructor, there's nothing to do. Avoid
3809 creating a possibly empty if-stmt. */
3810 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3812 node = TREE_CHAIN (node);
3813 continue;
3816 /* Remember that we had an initialization or finalization at this
3817 priority. */
3818 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3819 pi = get_priority_info (priority);
3820 if (initp)
3821 pi->initializations_p = 1;
3822 else
3823 pi->destructions_p = 1;
3825 /* Conditionalize this initialization on being in the right priority
3826 and being initializing/finalizing appropriately. */
3827 priority_if_stmt = begin_if_stmt ();
3828 cond = cp_build_binary_op (input_location,
3829 EQ_EXPR,
3830 priority_decl,
3831 build_int_cst (NULL_TREE, priority),
3832 tf_warning_or_error);
3833 finish_if_stmt_cond (cond, priority_if_stmt);
3835 /* Process initializers with same priority. */
3836 for (; node
3837 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3838 node = TREE_CHAIN (node))
3839 /* Do one initialization or destruction. */
3840 one_static_initialization_or_destruction (TREE_VALUE (node),
3841 TREE_PURPOSE (node), initp);
3843 /* Finish up the priority if-stmt body. */
3844 finish_then_clause (priority_if_stmt);
3845 finish_if_stmt (priority_if_stmt);
3847 } while (node);
3849 /* Revert what __asan_before_dynamic_init did by calling
3850 __asan_after_dynamic_init. */
3851 if (flag_sanitize & SANITIZE_ADDRESS)
3852 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3854 /* Finish up the init/destruct if-stmt body. */
3855 finish_then_clause (init_if_stmt);
3856 finish_if_stmt (init_if_stmt);
3859 /* VARS is a list of variables with static storage duration which may
3860 need initialization and/or finalization. Remove those variables
3861 that don't really need to be initialized or finalized, and return
3862 the resulting list. The order in which the variables appear in
3863 VARS is in reverse order of the order in which they should actually
3864 be initialized. The list we return is in the unreversed order;
3865 i.e., the first variable should be initialized first. */
3867 static tree
3868 prune_vars_needing_no_initialization (tree *vars)
3870 tree *var = vars;
3871 tree result = NULL_TREE;
3873 while (*var)
3875 tree t = *var;
3876 tree decl = TREE_VALUE (t);
3877 tree init = TREE_PURPOSE (t);
3879 /* Deal gracefully with error. */
3880 if (decl == error_mark_node)
3882 var = &TREE_CHAIN (t);
3883 continue;
3886 /* The only things that can be initialized are variables. */
3887 gcc_assert (VAR_P (decl));
3889 /* If this object is not defined, we don't need to do anything
3890 here. */
3891 if (DECL_EXTERNAL (decl))
3893 var = &TREE_CHAIN (t);
3894 continue;
3897 /* Also, if the initializer already contains errors, we can bail
3898 out now. */
3899 if (init && TREE_CODE (init) == TREE_LIST
3900 && value_member (error_mark_node, init))
3902 var = &TREE_CHAIN (t);
3903 continue;
3906 /* This variable is going to need initialization and/or
3907 finalization, so we add it to the list. */
3908 *var = TREE_CHAIN (t);
3909 TREE_CHAIN (t) = result;
3910 result = t;
3913 return result;
3916 /* Make sure we have told the back end about all the variables in
3917 VARS. */
3919 static void
3920 write_out_vars (tree vars)
3922 tree v;
3924 for (v = vars; v; v = TREE_CHAIN (v))
3926 tree var = TREE_VALUE (v);
3927 if (!var_finalized_p (var))
3929 import_export_decl (var);
3930 rest_of_decl_compilation (var, 1, 1);
3935 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3936 (otherwise) that will initialize all global objects with static
3937 storage duration having the indicated PRIORITY. */
3939 static void
3940 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3941 location_t *locus)
3943 char function_key;
3944 tree fndecl;
3945 tree body;
3946 size_t i;
3948 input_location = *locus;
3949 /* ??? */
3950 /* Was: locus->line++; */
3952 /* We use `I' to indicate initialization and `D' to indicate
3953 destruction. */
3954 function_key = constructor_p ? 'I' : 'D';
3956 /* We emit the function lazily, to avoid generating empty
3957 global constructors and destructors. */
3958 body = NULL_TREE;
3960 /* For Objective-C++, we may need to initialize metadata found in this module.
3961 This must be done _before_ any other static initializations. */
3962 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3963 && constructor_p && objc_static_init_needed_p ())
3965 body = start_objects (function_key, priority);
3966 objc_generate_static_init_call (NULL_TREE);
3969 /* Call the static storage duration function with appropriate
3970 arguments. */
3971 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3973 /* Calls to pure or const functions will expand to nothing. */
3974 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3976 tree call;
3978 if (! body)
3979 body = start_objects (function_key, priority);
3981 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3982 build_int_cst (NULL_TREE,
3983 constructor_p),
3984 build_int_cst (NULL_TREE,
3985 priority),
3986 NULL_TREE);
3987 finish_expr_stmt (call);
3991 /* Close out the function. */
3992 if (body)
3993 finish_objects (function_key, priority, body);
3996 /* Generate constructor and destructor functions for the priority
3997 indicated by N. */
3999 static int
4000 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4002 location_t *locus = (location_t *) data;
4003 int priority = (int) n->key;
4004 priority_info pi = (priority_info) n->value;
4006 /* Generate the functions themselves, but only if they are really
4007 needed. */
4008 if (pi->initializations_p)
4009 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4010 if (pi->destructions_p)
4011 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4013 /* Keep iterating. */
4014 return 0;
4017 /* Java requires that we be able to reference a local address for a
4018 method, and not be confused by PLT entries. If supported, create a
4019 hidden alias for all such methods. */
4021 static void
4022 build_java_method_aliases (void)
4024 #ifndef HAVE_GAS_HIDDEN
4025 return;
4026 #endif
4028 struct cgraph_node *node;
4029 FOR_EACH_FUNCTION (node)
4031 tree fndecl = node->decl;
4033 if (DECL_CLASS_SCOPE_P (fndecl)
4034 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4035 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4037 /* Mangle the name in a predictable way; we need to reference
4038 this from a java compiled object file. */
4039 tree oid = DECL_ASSEMBLER_NAME (fndecl);
4040 const char *oname = IDENTIFIER_POINTER (oid);
4041 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4042 char *nname = ACONCAT (("_ZGA", oname + 2, NULL));
4044 tree alias = make_alias_for (fndecl, get_identifier (nname));
4045 TREE_PUBLIC (alias) = 1;
4046 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4048 cgraph_node::create_same_body_alias (alias, fndecl);
4053 /* Return C++ property of T, based on given operation OP. */
4055 static int
4056 cpp_check (tree t, cpp_operation op)
4058 switch (op)
4060 case HAS_DEPENDENT_TEMPLATE_ARGS:
4062 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4063 if (!ti)
4064 return 0;
4065 ++processing_template_decl;
4066 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4067 --processing_template_decl;
4068 return dep;
4070 case IS_ABSTRACT:
4071 return DECL_PURE_VIRTUAL_P (t);
4072 case IS_CONSTRUCTOR:
4073 return DECL_CONSTRUCTOR_P (t);
4074 case IS_DESTRUCTOR:
4075 return DECL_DESTRUCTOR_P (t);
4076 case IS_COPY_CONSTRUCTOR:
4077 return DECL_COPY_CONSTRUCTOR_P (t);
4078 case IS_TEMPLATE:
4079 return TREE_CODE (t) == TEMPLATE_DECL;
4080 case IS_TRIVIAL:
4081 return trivial_type_p (t);
4082 default:
4083 return 0;
4087 /* Collect source file references recursively, starting from NAMESPC. */
4089 static void
4090 collect_source_refs (tree namespc)
4092 tree t;
4094 if (!namespc)
4095 return;
4097 /* Iterate over names in this name space. */
4098 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4099 if (!DECL_IS_BUILTIN (t) )
4100 collect_source_ref (DECL_SOURCE_FILE (t));
4102 /* Dump siblings, if any */
4103 collect_source_refs (TREE_CHAIN (namespc));
4105 /* Dump children, if any */
4106 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4109 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4110 starting from NAMESPC. */
4112 static void
4113 collect_ada_namespace (tree namespc, const char *source_file)
4115 if (!namespc)
4116 return;
4118 /* Collect decls from this namespace */
4119 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4121 /* Collect siblings, if any */
4122 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4124 /* Collect children, if any */
4125 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4128 /* Returns true iff there is a definition available for variable or
4129 function DECL. */
4131 static bool
4132 decl_defined_p (tree decl)
4134 if (TREE_CODE (decl) == FUNCTION_DECL)
4135 return (DECL_INITIAL (decl) != NULL_TREE);
4136 else
4138 gcc_assert (VAR_P (decl));
4139 return !DECL_EXTERNAL (decl);
4143 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4145 [expr.const]
4147 An integral constant-expression can only involve ... const
4148 variables of integral or enumeration types initialized with
4149 constant expressions ...
4151 C++0x also allows constexpr variables and temporaries initialized
4152 with constant expressions. We handle the former here, but the latter
4153 are just folded away in cxx_eval_constant_expression.
4155 The standard does not require that the expression be non-volatile.
4156 G++ implements the proposed correction in DR 457. */
4158 bool
4159 decl_constant_var_p (tree decl)
4161 if (!decl_maybe_constant_var_p (decl))
4162 return false;
4164 /* We don't know if a template static data member is initialized with
4165 a constant expression until we instantiate its initializer. Even
4166 in the case of a constexpr variable, we can't treat it as a
4167 constant until its initializer is complete in case it's used in
4168 its own initializer. */
4169 mark_used (decl);
4170 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4173 /* Returns true if DECL could be a symbolic constant variable, depending on
4174 its initializer. */
4176 bool
4177 decl_maybe_constant_var_p (tree decl)
4179 tree type = TREE_TYPE (decl);
4180 if (!VAR_P (decl))
4181 return false;
4182 if (DECL_DECLARED_CONSTEXPR_P (decl))
4183 return true;
4184 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4185 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4188 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4189 called from grokfndecl and grokvardecl; in all modes it is called from
4190 cp_write_global_declarations. */
4192 void
4193 no_linkage_error (tree decl)
4195 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4196 /* In C++11 it's ok if the decl is defined. */
4197 return;
4198 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4199 if (t == NULL_TREE)
4200 /* The type that got us on no_linkage_decls must have gotten a name for
4201 linkage purposes. */;
4202 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4203 /* The type might end up having a typedef name for linkage purposes. */
4204 vec_safe_push (no_linkage_decls, decl);
4205 else if (TYPE_ANONYMOUS_P (t))
4207 bool d = false;
4208 if (cxx_dialect >= cxx11)
4209 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4210 "anonymous type, is used but never defined", decl);
4211 else if (DECL_EXTERN_C_P (decl))
4212 /* Allow this; it's pretty common in C. */;
4213 else if (TREE_CODE (decl) == VAR_DECL)
4214 /* DRs 132, 319 and 389 seem to indicate types with
4215 no linkage can only be used to declare extern "C"
4216 entities. Since it's not always an error in the
4217 ISO C++ 90 Standard, we only issue a warning. */
4218 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4219 "with no linkage used to declare variable %q#D with "
4220 "linkage", decl);
4221 else
4222 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4223 "linkage used to declare function %q#D with linkage",
4224 decl);
4225 if (d && is_typedef_decl (TYPE_NAME (t)))
4226 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4227 "to the unqualified type, so it is not used for linkage",
4228 TYPE_NAME (t));
4230 else if (cxx_dialect >= cxx11)
4232 if (TREE_CODE (decl) == VAR_DECL || !DECL_PURE_VIRTUAL_P (decl))
4233 permerror (DECL_SOURCE_LOCATION (decl),
4234 "%q#D, declared using local type "
4235 "%qT, is used but never defined", decl, t);
4237 else if (TREE_CODE (decl) == VAR_DECL)
4238 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4239 "used to declare variable %q#D with linkage", t, decl);
4240 else
4241 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4242 "to declare function %q#D with linkage", t, decl);
4245 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4247 static void
4248 collect_all_refs (const char *source_file)
4250 collect_ada_namespace (global_namespace, source_file);
4253 /* Clear DECL_EXTERNAL for NODE. */
4255 static bool
4256 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4258 DECL_EXTERNAL (node->decl) = 0;
4259 return false;
4262 /* Build up the function to run dynamic initializers for thread_local
4263 variables in this translation unit and alias the init functions for the
4264 individual variables to it. */
4266 static void
4267 handle_tls_init (void)
4269 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4270 if (vars == NULL_TREE)
4271 return;
4273 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4275 write_out_vars (vars);
4277 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4278 boolean_type_node);
4279 TREE_PUBLIC (guard) = false;
4280 TREE_STATIC (guard) = true;
4281 DECL_ARTIFICIAL (guard) = true;
4282 DECL_IGNORED_P (guard) = true;
4283 TREE_USED (guard) = true;
4284 set_decl_tls_model (guard, decl_default_tls_model (guard));
4285 pushdecl_top_level_and_finish (guard, NULL_TREE);
4287 tree fn = get_local_tls_init_fn ();
4288 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4289 tree body = begin_function_body ();
4290 tree if_stmt = begin_if_stmt ();
4291 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4292 tf_warning_or_error);
4293 finish_if_stmt_cond (cond, if_stmt);
4294 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4295 tf_warning_or_error));
4296 for (; vars; vars = TREE_CHAIN (vars))
4298 tree var = TREE_VALUE (vars);
4299 tree init = TREE_PURPOSE (vars);
4300 one_static_initialization_or_destruction (var, init, true);
4302 #ifdef ASM_OUTPUT_DEF
4303 /* Output init aliases even with -fno-extern-tls-init. */
4304 if (TREE_PUBLIC (var))
4306 tree single_init_fn = get_tls_init_fn (var);
4307 if (single_init_fn == NULL_TREE)
4308 continue;
4309 cgraph_node *alias
4310 = cgraph_node::get_create (fn)->create_same_body_alias
4311 (single_init_fn, fn);
4312 gcc_assert (alias != NULL);
4314 #endif
4317 finish_then_clause (if_stmt);
4318 finish_if_stmt (if_stmt);
4319 finish_function_body (body);
4320 expand_or_defer_fn (finish_function (0));
4323 /* We're at the end of compilation, so generate any mangling aliases that
4324 we've been saving up, if DECL is going to be output and ID2 isn't
4325 already taken by another declaration. */
4327 static void
4328 generate_mangling_alias (tree decl, tree id2)
4330 /* If there's a declaration already using this mangled name,
4331 don't create a compatibility alias that conflicts. */
4332 if (IDENTIFIER_GLOBAL_VALUE (id2))
4333 return;
4335 struct cgraph_node *n = NULL;
4336 if (TREE_CODE (decl) == FUNCTION_DECL
4337 && !(n = cgraph_node::get (decl)))
4338 /* Don't create an alias to an unreferenced function. */
4339 return;
4341 tree alias = make_alias_for (decl, id2);
4342 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4343 DECL_IGNORED_P (alias) = 1;
4344 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4345 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4346 if (vague_linkage_p (decl))
4347 DECL_WEAK (alias) = 1;
4348 if (TREE_CODE (decl) == FUNCTION_DECL)
4349 n->create_same_body_alias (alias, decl);
4350 else
4351 varpool_node::create_extra_name_alias (alias, decl);
4354 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4355 the end of translation, for compatibility across bugs in the mangling
4356 implementation. */
4358 void
4359 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4361 #ifdef ASM_OUTPUT_DEF
4362 if (at_eof)
4363 generate_mangling_alias (decl, id2);
4364 else
4366 vec_safe_push (mangling_aliases, decl);
4367 vec_safe_push (mangling_aliases, id2);
4369 #endif
4372 static void
4373 generate_mangling_aliases ()
4375 while (!vec_safe_is_empty (mangling_aliases))
4377 tree id2 = mangling_aliases->pop();
4378 tree decl = mangling_aliases->pop();
4379 generate_mangling_alias (decl, id2);
4383 /* The entire file is now complete. If requested, dump everything
4384 to a file. */
4386 static void
4387 dump_tu (void)
4389 int flags;
4390 FILE *stream = dump_begin (TDI_tu, &flags);
4392 if (stream)
4394 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4395 dump_end (TDI_tu, stream);
4399 static location_t locus_at_end_of_parsing;
4401 /* Check the deallocation functions for CODE to see if we want to warn that
4402 only one was defined. */
4404 static void
4405 maybe_warn_sized_delete (enum tree_code code)
4407 tree sized = NULL_TREE;
4408 tree unsized = NULL_TREE;
4410 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4411 ovl; ovl = OVL_NEXT (ovl))
4413 tree fn = OVL_CURRENT (ovl);
4414 /* We're only interested in usual deallocation functions. */
4415 if (!non_placement_deallocation_fn_p (fn))
4416 continue;
4417 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4418 unsized = fn;
4419 else
4420 sized = fn;
4422 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4423 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4424 "the program should also define %qD", sized);
4425 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4426 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4427 "the program should also define %qD", unsized);
4430 /* Check the global deallocation functions to see if we want to warn about
4431 defining unsized without sized (or vice versa). */
4433 static void
4434 maybe_warn_sized_delete ()
4436 if (!flag_sized_deallocation || !warn_sized_deallocation)
4437 return;
4438 maybe_warn_sized_delete (DELETE_EXPR);
4439 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4442 /* This routine is called at the end of compilation.
4443 Its job is to create all the code needed to initialize and
4444 destroy the global aggregates. We do the destruction
4445 first, since that way we only need to reverse the decls once. */
4447 void
4448 c_parse_final_cleanups (void)
4450 tree vars;
4451 bool reconsider;
4452 size_t i;
4453 unsigned ssdf_count = 0;
4454 int retries = 0;
4455 tree decl;
4457 locus_at_end_of_parsing = input_location;
4458 at_eof = 1;
4460 /* Bad parse errors. Just forget about it. */
4461 if (! global_bindings_p () || current_class_type
4462 || !vec_safe_is_empty (decl_namespace_list))
4463 return;
4465 /* This is the point to write out a PCH if we're doing that.
4466 In that case we do not want to do anything else. */
4467 if (pch_file)
4469 c_common_write_pch ();
4470 dump_tu ();
4471 return;
4474 timevar_stop (TV_PHASE_PARSING);
4475 timevar_start (TV_PHASE_DEFERRED);
4477 symtab->process_same_body_aliases ();
4479 /* Handle -fdump-ada-spec[-slim] */
4480 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4482 if (flag_dump_ada_spec_slim)
4483 collect_source_ref (main_input_filename);
4484 else
4485 collect_source_refs (global_namespace);
4487 dump_ada_specs (collect_all_refs, cpp_check);
4490 /* FIXME - huh? was input_line -= 1;*/
4492 /* We now have to write out all the stuff we put off writing out.
4493 These include:
4495 o Template specializations that we have not yet instantiated,
4496 but which are needed.
4497 o Initialization and destruction for non-local objects with
4498 static storage duration. (Local objects with static storage
4499 duration are initialized when their scope is first entered,
4500 and are cleaned up via atexit.)
4501 o Virtual function tables.
4503 All of these may cause others to be needed. For example,
4504 instantiating one function may cause another to be needed, and
4505 generating the initializer for an object may cause templates to be
4506 instantiated, etc., etc. */
4508 emit_support_tinfos ();
4512 tree t;
4513 tree decl;
4515 reconsider = false;
4517 /* If there are templates that we've put off instantiating, do
4518 them now. */
4519 instantiate_pending_templates (retries);
4520 ggc_collect ();
4522 /* Write out virtual tables as required. Note that writing out
4523 the virtual table for a template class may cause the
4524 instantiation of members of that class. If we write out
4525 vtables then we remove the class from our list so we don't
4526 have to look at it again. */
4528 while (keyed_classes != NULL_TREE
4529 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4531 reconsider = true;
4532 keyed_classes = TREE_CHAIN (keyed_classes);
4535 t = keyed_classes;
4536 if (t != NULL_TREE)
4538 tree next = TREE_CHAIN (t);
4540 while (next)
4542 if (maybe_emit_vtables (TREE_VALUE (next)))
4544 reconsider = true;
4545 TREE_CHAIN (t) = TREE_CHAIN (next);
4547 else
4548 t = next;
4550 next = TREE_CHAIN (t);
4554 /* Write out needed type info variables. We have to be careful
4555 looping through unemitted decls, because emit_tinfo_decl may
4556 cause other variables to be needed. New elements will be
4557 appended, and we remove from the vector those that actually
4558 get emitted. */
4559 for (i = unemitted_tinfo_decls->length ();
4560 unemitted_tinfo_decls->iterate (--i, &t);)
4561 if (emit_tinfo_decl (t))
4563 reconsider = true;
4564 unemitted_tinfo_decls->unordered_remove (i);
4567 /* The list of objects with static storage duration is built up
4568 in reverse order. We clear STATIC_AGGREGATES so that any new
4569 aggregates added during the initialization of these will be
4570 initialized in the correct order when we next come around the
4571 loop. */
4572 vars = prune_vars_needing_no_initialization (&static_aggregates);
4574 if (vars)
4576 /* We need to start a new initialization function each time
4577 through the loop. That's because we need to know which
4578 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4579 isn't computed until a function is finished, and written
4580 out. That's a deficiency in the back end. When this is
4581 fixed, these initialization functions could all become
4582 inline, with resulting performance improvements. */
4583 tree ssdf_body;
4585 /* Set the line and file, so that it is obviously not from
4586 the source file. */
4587 input_location = locus_at_end_of_parsing;
4588 ssdf_body = start_static_storage_duration_function (ssdf_count);
4590 /* Make sure the back end knows about all the variables. */
4591 write_out_vars (vars);
4593 /* First generate code to do all the initializations. */
4594 if (vars)
4595 do_static_initialization_or_destruction (vars, /*initp=*/true);
4597 /* Then, generate code to do all the destructions. Do these
4598 in reverse order so that the most recently constructed
4599 variable is the first destroyed. If we're using
4600 __cxa_atexit, then we don't need to do this; functions
4601 were registered at initialization time to destroy the
4602 local statics. */
4603 if (!flag_use_cxa_atexit && vars)
4605 vars = nreverse (vars);
4606 do_static_initialization_or_destruction (vars, /*initp=*/false);
4608 else
4609 vars = NULL_TREE;
4611 /* Finish up the static storage duration function for this
4612 round. */
4613 input_location = locus_at_end_of_parsing;
4614 finish_static_storage_duration_function (ssdf_body);
4616 /* All those initializations and finalizations might cause
4617 us to need more inline functions, more template
4618 instantiations, etc. */
4619 reconsider = true;
4620 ssdf_count++;
4621 /* ??? was: locus_at_end_of_parsing.line++; */
4624 /* Now do the same for thread_local variables. */
4625 handle_tls_init ();
4627 /* Go through the set of inline functions whose bodies have not
4628 been emitted yet. If out-of-line copies of these functions
4629 are required, emit them. */
4630 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4632 /* Does it need synthesizing? */
4633 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4634 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4636 /* Even though we're already at the top-level, we push
4637 there again. That way, when we pop back a few lines
4638 hence, all of our state is restored. Otherwise,
4639 finish_function doesn't clean things up, and we end
4640 up with CURRENT_FUNCTION_DECL set. */
4641 push_to_top_level ();
4642 /* The decl's location will mark where it was first
4643 needed. Save that so synthesize method can indicate
4644 where it was needed from, in case of error */
4645 input_location = DECL_SOURCE_LOCATION (decl);
4646 synthesize_method (decl);
4647 pop_from_top_level ();
4648 reconsider = true;
4651 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4652 generate_tls_wrapper (decl);
4654 if (!DECL_SAVED_TREE (decl))
4655 continue;
4657 /* We lie to the back end, pretending that some functions
4658 are not defined when they really are. This keeps these
4659 functions from being put out unnecessarily. But, we must
4660 stop lying when the functions are referenced, or if they
4661 are not comdat since they need to be put out now. If
4662 DECL_INTERFACE_KNOWN, then we have already set
4663 DECL_EXTERNAL appropriately, so there's no need to check
4664 again, and we do not want to clear DECL_EXTERNAL if a
4665 previous call to import_export_decl set it.
4667 This is done in a separate for cycle, because if some
4668 deferred function is contained in another deferred
4669 function later in deferred_fns varray,
4670 rest_of_compilation would skip this function and we
4671 really cannot expand the same function twice. */
4672 import_export_decl (decl);
4673 if (DECL_NOT_REALLY_EXTERN (decl)
4674 && DECL_INITIAL (decl)
4675 && decl_needed_p (decl))
4677 struct cgraph_node *node, *next;
4679 node = cgraph_node::get (decl);
4680 if (node->cpp_implicit_alias)
4681 node = node->get_alias_target ();
4683 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4684 NULL, true);
4685 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4686 group, we need to mark all symbols in the same comdat group
4687 that way. */
4688 if (node->same_comdat_group)
4689 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4690 next != node;
4691 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4692 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4693 NULL, true);
4696 /* If we're going to need to write this function out, and
4697 there's already a body for it, create RTL for it now.
4698 (There might be no body if this is a method we haven't
4699 gotten around to synthesizing yet.) */
4700 if (!DECL_EXTERNAL (decl)
4701 && decl_needed_p (decl)
4702 && !TREE_ASM_WRITTEN (decl)
4703 && !cgraph_node::get (decl)->definition)
4705 /* We will output the function; no longer consider it in this
4706 loop. */
4707 DECL_DEFER_OUTPUT (decl) = 0;
4708 /* Generate RTL for this function now that we know we
4709 need it. */
4710 expand_or_defer_fn (decl);
4711 /* If we're compiling -fsyntax-only pretend that this
4712 function has been written out so that we don't try to
4713 expand it again. */
4714 if (flag_syntax_only)
4715 TREE_ASM_WRITTEN (decl) = 1;
4716 reconsider = true;
4720 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4721 reconsider = true;
4723 /* Static data members are just like namespace-scope globals. */
4724 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4726 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4727 /* Don't write it out if we haven't seen a definition. */
4728 || DECL_IN_AGGR_P (decl))
4729 continue;
4730 import_export_decl (decl);
4731 /* If this static data member is needed, provide it to the
4732 back end. */
4733 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4734 DECL_EXTERNAL (decl) = 0;
4736 if (vec_safe_length (pending_statics) != 0
4737 && wrapup_global_declarations (pending_statics->address (),
4738 pending_statics->length ()))
4739 reconsider = true;
4741 retries++;
4743 while (reconsider);
4745 generate_mangling_aliases ();
4747 /* All used inline functions must have a definition at this point. */
4748 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4750 if (/* Check online inline functions that were actually used. */
4751 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4752 /* If the definition actually was available here, then the
4753 fact that the function was not defined merely represents
4754 that for some reason (use of a template repository,
4755 #pragma interface, etc.) we decided not to emit the
4756 definition here. */
4757 && !DECL_INITIAL (decl)
4758 /* Don't complain if the template was defined. */
4759 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4760 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4761 (template_for_substitution (decl)))))
4763 warning (0, "inline function %q+D used but never defined", decl);
4764 /* Avoid a duplicate warning from check_global_declaration. */
4765 TREE_NO_WARNING (decl) = 1;
4769 /* So must decls that use a type with no linkage. */
4770 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4771 no_linkage_error (decl);
4773 maybe_warn_sized_delete ();
4775 /* Then, do the Objective-C stuff. This is where all the
4776 Objective-C module stuff gets generated (symtab,
4777 class/protocol/selector lists etc). This must be done after C++
4778 templates, destructors etc. so that selectors used in C++
4779 templates are properly allocated. */
4780 if (c_dialect_objc ())
4781 objc_write_global_declarations ();
4783 /* We give C linkage to static constructors and destructors. */
4784 push_lang_context (lang_name_c);
4786 /* Generate initialization and destruction functions for all
4787 priorities for which they are required. */
4788 if (priority_info_map)
4789 splay_tree_foreach (priority_info_map,
4790 generate_ctor_and_dtor_functions_for_priority,
4791 /*data=*/&locus_at_end_of_parsing);
4792 else if (c_dialect_objc () && objc_static_init_needed_p ())
4793 /* If this is obj-c++ and we need a static init, call
4794 generate_ctor_or_dtor_function. */
4795 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4796 DEFAULT_INIT_PRIORITY,
4797 &locus_at_end_of_parsing);
4799 /* We're done with the splay-tree now. */
4800 if (priority_info_map)
4801 splay_tree_delete (priority_info_map);
4803 /* Generate any missing aliases. */
4804 maybe_apply_pending_pragma_weaks ();
4806 /* We're done with static constructors, so we can go back to "C++"
4807 linkage now. */
4808 pop_lang_context ();
4810 /* Generate Java hidden aliases. */
4811 build_java_method_aliases ();
4813 if (flag_vtable_verify)
4815 vtv_recover_class_info ();
4816 vtv_compute_class_hierarchy_transitive_closure ();
4817 vtv_build_vtable_verify_fndecl ();
4820 perform_deferred_noexcept_checks ();
4822 finish_repo ();
4824 /* The entire file is now complete. If requested, dump everything
4825 to a file. */
4826 dump_tu ();
4828 if (flag_detailed_statistics)
4830 dump_tree_statistics ();
4831 dump_time_statistics ();
4834 timevar_stop (TV_PHASE_DEFERRED);
4835 timevar_start (TV_PHASE_PARSING);
4838 /* Perform any post compilation-proper cleanups for the C++ front-end.
4839 This should really go away. No front-end should need to do
4840 anything past the compilation process. */
4842 void
4843 cxx_post_compilation_parsing_cleanups (void)
4845 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4847 if (flag_vtable_verify)
4849 /* Generate the special constructor initialization function that
4850 calls __VLTRegisterPairs, and give it a very high
4851 initialization priority. This must be done after
4852 finalize_compilation_unit so that we have accurate
4853 information about which vtable will actually be emitted. */
4854 vtv_generate_init_routine ();
4857 input_location = locus_at_end_of_parsing;
4859 #ifdef ENABLE_CHECKING
4860 validate_conversion_obstack ();
4861 #endif /* ENABLE_CHECKING */
4863 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4866 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4867 function to call in parse-tree form; it has not yet been
4868 semantically analyzed. ARGS are the arguments to the function.
4869 They have already been semantically analyzed. This may change
4870 ARGS. */
4872 tree
4873 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4874 tsubst_flags_t complain)
4876 tree orig_fn;
4877 vec<tree, va_gc> *orig_args = NULL;
4878 tree expr;
4879 tree object;
4881 orig_fn = fn;
4882 object = TREE_OPERAND (fn, 0);
4884 if (processing_template_decl)
4886 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4887 || TREE_CODE (fn) == MEMBER_REF);
4888 if (type_dependent_expression_p (fn)
4889 || any_type_dependent_arguments_p (*args))
4890 return build_nt_call_vec (fn, *args);
4892 orig_args = make_tree_vector_copy (*args);
4894 /* Transform the arguments and add the implicit "this"
4895 parameter. That must be done before the FN is transformed
4896 because we depend on the form of FN. */
4897 make_args_non_dependent (*args);
4898 object = build_non_dependent_expr (object);
4899 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4901 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4902 object = cp_build_addr_expr (object, complain);
4903 vec_safe_insert (*args, 0, object);
4905 /* Now that the arguments are done, transform FN. */
4906 fn = build_non_dependent_expr (fn);
4909 /* A qualified name corresponding to a bound pointer-to-member is
4910 represented as an OFFSET_REF:
4912 struct B { void g(); };
4913 void (B::*p)();
4914 void B::g() { (this->*p)(); } */
4915 if (TREE_CODE (fn) == OFFSET_REF)
4917 tree object_addr = cp_build_addr_expr (object, complain);
4918 fn = TREE_OPERAND (fn, 1);
4919 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4920 complain);
4921 vec_safe_insert (*args, 0, object_addr);
4924 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4925 expr = build_op_call (fn, args, complain);
4926 else
4927 expr = cp_build_function_call_vec (fn, args, complain);
4928 if (processing_template_decl && expr != error_mark_node)
4929 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4931 if (orig_args != NULL)
4932 release_tree_vector (orig_args);
4934 return expr;
4938 void
4939 check_default_args (tree x)
4941 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4942 bool saw_def = false;
4943 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4944 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4946 if (TREE_PURPOSE (arg))
4947 saw_def = true;
4948 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4950 error ("default argument missing for parameter %P of %q+#D", i, x);
4951 TREE_PURPOSE (arg) = error_mark_node;
4956 /* Return true if function DECL can be inlined. This is used to force
4957 instantiation of methods that might be interesting for inlining. */
4958 bool
4959 possibly_inlined_p (tree decl)
4961 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4962 if (DECL_UNINLINABLE (decl))
4963 return false;
4964 if (!optimize || pragma_java_exceptions)
4965 return DECL_DECLARED_INLINE_P (decl);
4966 /* When optimizing, we might inline everything when flatten
4967 attribute or heuristics inlining for size or autoinlining
4968 is used. */
4969 return true;
4972 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4973 If DECL is a specialization or implicitly declared class member,
4974 generate the actual definition. Return false if something goes
4975 wrong, true otherwise. */
4977 bool
4978 mark_used (tree decl, tsubst_flags_t complain)
4980 /* If DECL is a BASELINK for a single function, then treat it just
4981 like the DECL for the function. Otherwise, if the BASELINK is
4982 for an overloaded function, we don't know which function was
4983 actually used until after overload resolution. */
4984 if (BASELINK_P (decl))
4986 decl = BASELINK_FUNCTIONS (decl);
4987 if (really_overloaded_fn (decl))
4988 return true;
4989 decl = OVL_CURRENT (decl);
4992 /* Set TREE_USED for the benefit of -Wunused. */
4993 TREE_USED (decl) = 1;
4994 if (DECL_CLONED_FUNCTION_P (decl))
4995 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4997 /* Mark enumeration types as used. */
4998 if (TREE_CODE (decl) == CONST_DECL)
4999 used_types_insert (DECL_CONTEXT (decl));
5001 if (TREE_CODE (decl) == FUNCTION_DECL)
5002 maybe_instantiate_noexcept (decl);
5004 if (TREE_CODE (decl) == FUNCTION_DECL
5005 && DECL_DELETED_FN (decl))
5007 if (DECL_ARTIFICIAL (decl))
5009 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5010 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5012 /* We mark a lambda conversion op as deleted if we can't
5013 generate it properly; see maybe_add_lambda_conv_op. */
5014 sorry ("converting lambda which uses %<...%> to "
5015 "function pointer");
5016 return false;
5019 if (complain & tf_error)
5021 error ("use of deleted function %qD", decl);
5022 if (!maybe_explain_implicit_delete (decl))
5023 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5025 return false;
5028 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5029 && deprecated_state != DEPRECATED_SUPPRESS)
5030 warn_deprecated_use (decl, NULL_TREE);
5032 /* We can only check DECL_ODR_USED on variables or functions with
5033 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5034 might need special handling for. */
5035 if (!VAR_OR_FUNCTION_DECL_P (decl)
5036 || DECL_LANG_SPECIFIC (decl) == NULL
5037 || DECL_THUNK_P (decl))
5039 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
5041 if (complain & tf_error)
5042 error ("use of %qD before deduction of %<auto%>", decl);
5043 return false;
5045 return true;
5048 /* We only want to do this processing once. We don't need to keep trying
5049 to instantiate inline templates, because unit-at-a-time will make sure
5050 we get them compiled before functions that want to inline them. */
5051 if (DECL_ODR_USED (decl))
5052 return true;
5054 /* If within finish_function, defer the rest until that function
5055 finishes, otherwise it might recurse. */
5056 if (defer_mark_used_calls)
5058 vec_safe_push (deferred_mark_used_calls, decl);
5059 return true;
5062 /* Normally, we can wait until instantiation-time to synthesize DECL.
5063 However, if DECL is a static data member initialized with a constant
5064 or a constexpr function, we need it right now because a reference to
5065 such a data member or a call to such function is not value-dependent.
5066 For a function that uses auto in the return type, we need to instantiate
5067 it to find out its type. For OpenMP user defined reductions, we need
5068 them instantiated for reduction clauses which inline them by hand
5069 directly. */
5070 if (DECL_LANG_SPECIFIC (decl)
5071 && DECL_TEMPLATE_INFO (decl)
5072 && (decl_maybe_constant_var_p (decl)
5073 || (TREE_CODE (decl) == FUNCTION_DECL
5074 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5075 || undeduced_auto_decl (decl))
5076 && !uses_template_parms (DECL_TI_ARGS (decl)))
5078 /* Instantiating a function will result in garbage collection. We
5079 must treat this situation as if we were within the body of a
5080 function so as to avoid collecting live data only referenced from
5081 the stack (such as overload resolution candidates). */
5082 ++function_depth;
5083 instantiate_decl (decl, /*defer_ok=*/false,
5084 /*expl_inst_class_mem_p=*/false);
5085 --function_depth;
5088 if (processing_template_decl || in_template_function ())
5089 return true;
5091 /* Check this too in case we're within instantiate_non_dependent_expr. */
5092 if (DECL_TEMPLATE_INFO (decl)
5093 && uses_template_parms (DECL_TI_ARGS (decl)))
5094 return true;
5096 if (undeduced_auto_decl (decl))
5098 if (complain & tf_error)
5099 error ("use of %qD before deduction of %<auto%>", decl);
5100 return false;
5103 /* If we don't need a value, then we don't need to synthesize DECL. */
5104 if (cp_unevaluated_operand != 0)
5105 return true;
5107 DECL_ODR_USED (decl) = 1;
5108 if (DECL_CLONED_FUNCTION_P (decl))
5109 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5111 /* DR 757: A type without linkage shall not be used as the type of a
5112 variable or function with linkage, unless
5113 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5114 o the variable or function is not used (3.2 [basic.def.odr]) or is
5115 defined in the same translation unit. */
5116 if (cxx_dialect > cxx98
5117 && decl_linkage (decl) != lk_none
5118 && !DECL_EXTERN_C_P (decl)
5119 && !DECL_ARTIFICIAL (decl)
5120 && !decl_defined_p (decl)
5121 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5123 if (is_local_extern (decl))
5124 /* There's no way to define a local extern, and adding it to
5125 the vector interferes with GC, so give an error now. */
5126 no_linkage_error (decl);
5127 else
5128 vec_safe_push (no_linkage_decls, decl);
5131 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5132 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5133 /* Remember it, so we can check it was defined. */
5134 note_vague_linkage_fn (decl);
5136 /* Is it a synthesized method that needs to be synthesized? */
5137 if (TREE_CODE (decl) == FUNCTION_DECL
5138 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5139 && DECL_DEFAULTED_FN (decl)
5140 /* A function defaulted outside the class is synthesized either by
5141 cp_finish_decl or instantiate_decl. */
5142 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5143 && ! DECL_INITIAL (decl))
5145 /* Defer virtual destructors so that thunks get the right
5146 linkage. */
5147 if (DECL_VIRTUAL_P (decl) && !at_eof)
5149 note_vague_linkage_fn (decl);
5150 return true;
5153 /* Remember the current location for a function we will end up
5154 synthesizing. Then we can inform the user where it was
5155 required in the case of error. */
5156 DECL_SOURCE_LOCATION (decl) = input_location;
5158 /* Synthesizing an implicitly defined member function will result in
5159 garbage collection. We must treat this situation as if we were
5160 within the body of a function so as to avoid collecting live data
5161 on the stack (such as overload resolution candidates).
5163 We could just let cp_write_global_declarations handle synthesizing
5164 this function by adding it to deferred_fns, but doing
5165 it at the use site produces better error messages. */
5166 ++function_depth;
5167 synthesize_method (decl);
5168 --function_depth;
5169 /* If this is a synthesized method we don't need to
5170 do the instantiation test below. */
5172 else if (VAR_OR_FUNCTION_DECL_P (decl)
5173 && DECL_TEMPLATE_INFO (decl)
5174 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5175 || always_instantiate_p (decl)))
5176 /* If this is a function or variable that is an instance of some
5177 template, we now know that we will need to actually do the
5178 instantiation. We check that DECL is not an explicit
5179 instantiation because that is not checked in instantiate_decl.
5181 We put off instantiating functions in order to improve compile
5182 times. Maintaining a stack of active functions is expensive,
5183 and the inliner knows to instantiate any functions it might
5184 need. Therefore, we always try to defer instantiation. */
5186 ++function_depth;
5187 instantiate_decl (decl, /*defer_ok=*/true,
5188 /*expl_inst_class_mem_p=*/false);
5189 --function_depth;
5192 return true;
5195 bool
5196 mark_used (tree decl)
5198 return mark_used (decl, tf_warning_or_error);
5201 tree
5202 vtv_start_verification_constructor_init_function (void)
5204 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5207 tree
5208 vtv_finish_verification_constructor_init_function (tree function_body)
5210 tree fn;
5212 finish_compound_stmt (function_body);
5213 fn = finish_function (0);
5214 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5215 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5217 return fn;
5220 #include "gt-cp-decl2.h"