* gcc.dg/vmx/unpack.c: Use dg-additional-options rather than
[official-gcc.git] / gcc / cp / decl2.c
blobcac0508f881af2e0cd21a135b6d594787d30b328
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 "tree.h"
35 #include "stringpool.h"
36 #include "varasm.h"
37 #include "attribs.h"
38 #include "stor-layout.h"
39 #include "calls.h"
40 #include "flags.h"
41 #include "cp-tree.h"
42 #include "decl.h"
43 #include "toplev.h"
44 #include "timevar.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "c-family/c-common.h"
48 #include "c-family/c-objc.h"
49 #include "hard-reg-set.h"
50 #include "function.h"
51 #include "cgraph.h"
52 #include "tree-inline.h"
53 #include "c-family/c-pragma.h"
54 #include "dumpfile.h"
55 #include "intl.h"
56 #include "splay-tree.h"
57 #include "langhooks.h"
58 #include "c-family/c-ada-spec.h"
59 #include "asan.h"
61 extern cpp_reader *parse_in;
63 /* This structure contains information about the initializations
64 and/or destructions required for a particular priority level. */
65 typedef struct priority_info_s {
66 /* Nonzero if there have been any initializations at this priority
67 throughout the translation unit. */
68 int initializations_p;
69 /* Nonzero if there have been any destructions at this priority
70 throughout the translation unit. */
71 int destructions_p;
72 } *priority_info;
74 static void mark_vtable_entries (tree);
75 static bool maybe_emit_vtables (tree);
76 static bool acceptable_java_type (tree);
77 static tree start_objects (int, int);
78 static void finish_objects (int, int, tree);
79 static tree start_static_storage_duration_function (unsigned);
80 static void finish_static_storage_duration_function (tree);
81 static priority_info get_priority_info (int);
82 static void do_static_initialization_or_destruction (tree, bool);
83 static void one_static_initialization_or_destruction (tree, tree, bool);
84 static void generate_ctor_or_dtor_function (bool, int, location_t *);
85 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
86 void *);
87 static tree prune_vars_needing_no_initialization (tree *);
88 static void write_out_vars (tree);
89 static void import_export_class (tree);
90 static tree get_guard_bits (tree);
91 static void determine_visibility_from_class (tree, tree);
92 static bool determine_hidden_inline (tree);
93 static bool decl_defined_p (tree);
95 /* A list of static class variables. This is needed, because a
96 static class variable can be declared inside the class without
97 an initializer, and then initialized, statically, outside the class. */
98 static GTY(()) vec<tree, va_gc> *pending_statics;
100 /* A list of functions which were declared inline, but which we
101 may need to emit outline anyway. */
102 static GTY(()) vec<tree, va_gc> *deferred_fns;
104 /* A list of decls that use types with no linkage, which we need to make
105 sure are defined. */
106 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
108 /* A vector of alternating decls and identifiers, where the latter
109 is to be an alias for the former if the former is defined. */
110 static GTY(()) vec<tree, va_gc> *mangling_aliases;
112 /* Nonzero if we're done parsing and into end-of-file activities. */
114 int at_eof;
117 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
118 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
119 that apply to the function). */
121 tree
122 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
123 cp_ref_qualifier rqual)
125 tree raises;
126 tree attrs;
127 int type_quals;
128 bool late_return_type_p;
130 if (fntype == error_mark_node || ctype == error_mark_node)
131 return error_mark_node;
133 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
134 || TREE_CODE (fntype) == METHOD_TYPE);
136 type_quals = quals & ~TYPE_QUAL_RESTRICT;
137 ctype = cp_build_qualified_type (ctype, type_quals);
138 raises = TYPE_RAISES_EXCEPTIONS (fntype);
139 attrs = TYPE_ATTRIBUTES (fntype);
140 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
141 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
142 (TREE_CODE (fntype) == METHOD_TYPE
143 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
144 : TYPE_ARG_TYPES (fntype)));
145 if (attrs)
146 fntype = cp_build_type_attribute_variant (fntype, attrs);
147 if (rqual)
148 fntype = build_ref_qualified_type (fntype, rqual);
149 if (raises)
150 fntype = build_exception_variant (fntype, raises);
151 if (late_return_type_p)
152 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
154 return fntype;
157 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
158 return type changed to NEW_RET. */
160 tree
161 change_return_type (tree new_ret, tree fntype)
163 tree newtype;
164 tree args = TYPE_ARG_TYPES (fntype);
165 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
166 tree attrs = TYPE_ATTRIBUTES (fntype);
167 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
169 if (new_ret == error_mark_node)
170 return fntype;
172 if (same_type_p (new_ret, TREE_TYPE (fntype)))
173 return fntype;
175 if (TREE_CODE (fntype) == FUNCTION_TYPE)
177 newtype = build_function_type (new_ret, args);
178 newtype = apply_memfn_quals (newtype,
179 type_memfn_quals (fntype),
180 type_memfn_rqual (fntype));
182 else
183 newtype = build_method_type_directly
184 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
185 if (FUNCTION_REF_QUALIFIED (fntype))
186 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
187 if (raises)
188 newtype = build_exception_variant (newtype, raises);
189 if (attrs)
190 newtype = cp_build_type_attribute_variant (newtype, attrs);
191 if (late_return_type_p)
192 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
194 return newtype;
197 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
198 appropriately. */
200 tree
201 cp_build_parm_decl (tree name, tree type)
203 tree parm = build_decl (input_location,
204 PARM_DECL, name, type);
205 /* DECL_ARG_TYPE is only used by the back end and the back end never
206 sees templates. */
207 if (!processing_template_decl)
208 DECL_ARG_TYPE (parm) = type_passed_as (type);
210 return parm;
213 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
214 indicated NAME. */
216 tree
217 build_artificial_parm (tree name, tree type)
219 tree parm = cp_build_parm_decl (name, type);
220 DECL_ARTIFICIAL (parm) = 1;
221 /* All our artificial parms are implicitly `const'; they cannot be
222 assigned to. */
223 TREE_READONLY (parm) = 1;
224 return parm;
227 /* Constructors for types with virtual baseclasses need an "in-charge" flag
228 saying whether this constructor is responsible for initialization of
229 virtual baseclasses or not. All destructors also need this "in-charge"
230 flag, which additionally determines whether or not the destructor should
231 free the memory for the object.
233 This function adds the "in-charge" flag to member function FN if
234 appropriate. It is called from grokclassfn and tsubst.
235 FN must be either a constructor or destructor.
237 The in-charge flag follows the 'this' parameter, and is followed by the
238 VTT parm (if any), then the user-written parms. */
240 void
241 maybe_retrofit_in_chrg (tree fn)
243 tree basetype, arg_types, parms, parm, fntype;
245 /* If we've already add the in-charge parameter don't do it again. */
246 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
247 return;
249 /* When processing templates we can't know, in general, whether or
250 not we're going to have virtual baseclasses. */
251 if (processing_template_decl)
252 return;
254 /* We don't need an in-charge parameter for constructors that don't
255 have virtual bases. */
256 if (DECL_CONSTRUCTOR_P (fn)
257 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
258 return;
260 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
261 basetype = TREE_TYPE (TREE_VALUE (arg_types));
262 arg_types = TREE_CHAIN (arg_types);
264 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
266 /* If this is a subobject constructor or destructor, our caller will
267 pass us a pointer to our VTT. */
268 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
270 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
272 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
273 DECL_CHAIN (parm) = parms;
274 parms = parm;
276 /* ...and then to TYPE_ARG_TYPES. */
277 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
279 DECL_HAS_VTT_PARM_P (fn) = 1;
282 /* Then add the in-charge parm (before the VTT parm). */
283 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
284 DECL_CHAIN (parm) = parms;
285 parms = parm;
286 arg_types = hash_tree_chain (integer_type_node, arg_types);
288 /* Insert our new parameter(s) into the list. */
289 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
291 /* And rebuild the function type. */
292 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
293 arg_types);
294 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
295 fntype = build_exception_variant (fntype,
296 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
297 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
298 fntype = (cp_build_type_attribute_variant
299 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
300 TREE_TYPE (fn) = fntype;
302 /* Now we've got the in-charge parameter. */
303 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
306 /* Classes overload their constituent function names automatically.
307 When a function name is declared in a record structure,
308 its name is changed to it overloaded name. Since names for
309 constructors and destructors can conflict, we place a leading
310 '$' for destructors.
312 CNAME is the name of the class we are grokking for.
314 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
316 FLAGS contains bits saying what's special about today's
317 arguments. DTOR_FLAG == DESTRUCTOR.
319 If FUNCTION is a destructor, then we must add the `auto-delete' field
320 as a second parameter. There is some hair associated with the fact
321 that we must "declare" this variable in the manner consistent with the
322 way the rest of the arguments were declared.
324 QUALS are the qualifiers for the this pointer. */
326 void
327 grokclassfn (tree ctype, tree function, enum overload_flags flags)
329 tree fn_name = DECL_NAME (function);
331 /* Even within an `extern "C"' block, members get C++ linkage. See
332 [dcl.link] for details. */
333 SET_DECL_LANGUAGE (function, lang_cplusplus);
335 if (fn_name == NULL_TREE)
337 error ("name missing for member function");
338 fn_name = get_identifier ("<anonymous>");
339 DECL_NAME (function) = fn_name;
342 DECL_CONTEXT (function) = ctype;
344 if (flags == DTOR_FLAG)
345 DECL_DESTRUCTOR_P (function) = 1;
347 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
348 maybe_retrofit_in_chrg (function);
351 /* Create an ARRAY_REF, checking for the user doing things backwards
352 along the way. DECLTYPE_P is for N3276, as in the parser. */
354 tree
355 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
356 bool decltype_p)
358 tree type;
359 tree expr;
360 tree orig_array_expr = array_expr;
361 tree orig_index_exp = index_exp;
363 if (error_operand_p (array_expr) || error_operand_p (index_exp))
364 return error_mark_node;
366 if (processing_template_decl)
368 if (type_dependent_expression_p (array_expr)
369 || type_dependent_expression_p (index_exp))
370 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
371 NULL_TREE, NULL_TREE);
372 array_expr = build_non_dependent_expr (array_expr);
373 index_exp = build_non_dependent_expr (index_exp);
376 type = TREE_TYPE (array_expr);
377 gcc_assert (type);
378 type = non_reference (type);
380 /* If they have an `operator[]', use that. */
381 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
383 tsubst_flags_t complain = tf_warning_or_error;
384 if (decltype_p)
385 complain |= tf_decltype;
386 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
387 index_exp, NULL_TREE, /*overload=*/NULL, complain);
389 else
391 tree p1, p2, i1, i2;
393 /* Otherwise, create an ARRAY_REF for a pointer or array type.
394 It is a little-known fact that, if `a' is an array and `i' is
395 an int, you can write `i[a]', which means the same thing as
396 `a[i]'. */
397 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
398 p1 = array_expr;
399 else
400 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
402 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
403 p2 = index_exp;
404 else
405 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
407 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
408 false);
409 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
410 false);
412 if ((p1 && i2) && (i1 && p2))
413 error ("ambiguous conversion for array subscript");
415 if (p1 && i2)
416 array_expr = p1, index_exp = i2;
417 else if (i1 && p2)
418 array_expr = p2, index_exp = i1;
419 else
421 error ("invalid types %<%T[%T]%> for array subscript",
422 type, TREE_TYPE (index_exp));
423 return error_mark_node;
426 if (array_expr == error_mark_node || index_exp == error_mark_node)
427 error ("ambiguous conversion for array subscript");
429 expr = build_array_ref (input_location, array_expr, index_exp);
431 if (processing_template_decl && expr != error_mark_node)
432 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
433 NULL_TREE, NULL_TREE);
434 return expr;
437 /* Given the cast expression EXP, checking out its validity. Either return
438 an error_mark_node if there was an unavoidable error, return a cast to
439 void for trying to delete a pointer w/ the value 0, or return the
440 call to delete. If DOING_VEC is true, we handle things differently
441 for doing an array delete.
442 Implements ARM $5.3.4. This is called from the parser. */
444 tree
445 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
446 tsubst_flags_t complain)
448 tree t, type;
450 if (exp == error_mark_node)
451 return exp;
453 if (processing_template_decl)
455 t = build_min (DELETE_EXPR, void_type_node, exp, size);
456 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
457 DELETE_EXPR_USE_VEC (t) = doing_vec;
458 TREE_SIDE_EFFECTS (t) = 1;
459 return t;
462 /* An array can't have been allocated by new, so complain. */
463 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
464 warning (0, "deleting array %q#E", exp);
466 t = build_expr_type_conversion (WANT_POINTER, exp, true);
468 if (t == NULL_TREE || t == error_mark_node)
470 error ("type %q#T argument given to %<delete%>, expected pointer",
471 TREE_TYPE (exp));
472 return error_mark_node;
475 type = TREE_TYPE (t);
477 /* As of Valley Forge, you can delete a pointer to const. */
479 /* You can't delete functions. */
480 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
482 error ("cannot delete a function. Only pointer-to-objects are "
483 "valid arguments to %<delete%>");
484 return error_mark_node;
487 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
488 if (VOID_TYPE_P (TREE_TYPE (type)))
490 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
491 doing_vec = 0;
494 /* Deleting a pointer with the value zero is valid and has no effect. */
495 if (integer_zerop (t))
496 return build1 (NOP_EXPR, void_type_node, t);
498 if (doing_vec)
499 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
500 sfk_deleting_destructor,
501 use_global_delete, complain);
502 else
503 return build_delete (type, t, sfk_deleting_destructor,
504 LOOKUP_NORMAL, use_global_delete,
505 complain);
508 /* Report an error if the indicated template declaration is not the
509 sort of thing that should be a member template. */
511 void
512 check_member_template (tree tmpl)
514 tree decl;
516 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
517 decl = DECL_TEMPLATE_RESULT (tmpl);
519 if (TREE_CODE (decl) == FUNCTION_DECL
520 || DECL_ALIAS_TEMPLATE_P (tmpl)
521 || (TREE_CODE (decl) == TYPE_DECL
522 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
524 /* The parser rejects template declarations in local classes
525 (with the exception of generic lambdas). */
526 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
527 /* The parser rejects any use of virtual in a function template. */
528 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
529 && DECL_VIRTUAL_P (decl)));
531 /* The debug-information generating code doesn't know what to do
532 with member templates. */
533 DECL_IGNORED_P (tmpl) = 1;
535 else if (variable_template_p (tmpl))
536 /* OK */;
537 else
538 error ("template declaration of %q#D", decl);
541 /* Return true iff TYPE is a valid Java parameter or return type. */
543 static bool
544 acceptable_java_type (tree type)
546 if (type == error_mark_node)
547 return false;
549 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
550 return true;
551 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
553 type = TREE_TYPE (type);
554 if (TREE_CODE (type) == RECORD_TYPE)
556 tree args; int i;
557 if (! TYPE_FOR_JAVA (type))
558 return false;
559 if (! CLASSTYPE_TEMPLATE_INFO (type))
560 return true;
561 args = CLASSTYPE_TI_ARGS (type);
562 i = TREE_VEC_LENGTH (args);
563 while (--i >= 0)
565 type = TREE_VEC_ELT (args, i);
566 if (TYPE_PTR_P (type))
567 type = TREE_TYPE (type);
568 if (! TYPE_FOR_JAVA (type))
569 return false;
571 return true;
574 return false;
577 /* For a METHOD in a Java class CTYPE, return true if
578 the parameter and return types are valid Java types.
579 Otherwise, print appropriate error messages, and return false. */
581 bool
582 check_java_method (tree method)
584 bool jerr = false;
585 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
586 tree ret_type = TREE_TYPE (TREE_TYPE (method));
588 if (!acceptable_java_type (ret_type))
590 error ("Java method %qD has non-Java return type %qT",
591 method, ret_type);
592 jerr = true;
595 arg_types = TREE_CHAIN (arg_types);
596 if (DECL_HAS_IN_CHARGE_PARM_P (method))
597 arg_types = TREE_CHAIN (arg_types);
598 if (DECL_HAS_VTT_PARM_P (method))
599 arg_types = TREE_CHAIN (arg_types);
601 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
603 tree type = TREE_VALUE (arg_types);
604 if (!acceptable_java_type (type))
606 if (type != error_mark_node)
607 error ("Java method %qD has non-Java parameter type %qT",
608 method, type);
609 jerr = true;
612 return !jerr;
615 /* Sanity check: report error if this function FUNCTION is not
616 really a member of the class (CTYPE) it is supposed to belong to.
617 TEMPLATE_PARMS is used to specify the template parameters of a member
618 template passed as FUNCTION_DECL. If the member template is passed as a
619 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
620 from the declaration. If the function is not a function template, it
621 must be NULL.
622 It returns the original declaration for the function, NULL_TREE if
623 no declaration was found, error_mark_node if an error was emitted. */
625 tree
626 check_classfn (tree ctype, tree function, tree template_parms)
628 int ix;
629 bool is_template;
630 tree pushed_scope;
632 if (DECL_USE_TEMPLATE (function)
633 && !(TREE_CODE (function) == TEMPLATE_DECL
634 && DECL_TEMPLATE_SPECIALIZATION (function))
635 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
636 /* Since this is a specialization of a member template,
637 we're not going to find the declaration in the class.
638 For example, in:
640 struct S { template <typename T> void f(T); };
641 template <> void S::f(int);
643 we're not going to find `S::f(int)', but there's no
644 reason we should, either. We let our callers know we didn't
645 find the method, but we don't complain. */
646 return NULL_TREE;
648 /* Basic sanity check: for a template function, the template parameters
649 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
650 if (TREE_CODE (function) == TEMPLATE_DECL)
652 if (template_parms
653 && !comp_template_parms (template_parms,
654 DECL_TEMPLATE_PARMS (function)))
656 error ("template parameter lists provided don%'t match the "
657 "template parameters of %qD", function);
658 return error_mark_node;
660 template_parms = DECL_TEMPLATE_PARMS (function);
663 /* OK, is this a definition of a member template? */
664 is_template = (template_parms != NULL_TREE);
666 /* [temp.mem]
668 A destructor shall not be a member template. */
669 if (DECL_DESTRUCTOR_P (function) && is_template)
671 error ("destructor %qD declared as member template", function);
672 return error_mark_node;
675 /* We must enter the scope here, because conversion operators are
676 named by target type, and type equivalence relies on typenames
677 resolving within the scope of CTYPE. */
678 pushed_scope = push_scope (ctype);
679 ix = class_method_index_for_fn (complete_type (ctype), function);
680 if (ix >= 0)
682 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
683 tree fndecls, fndecl = 0;
684 bool is_conv_op;
685 const char *format = NULL;
687 for (fndecls = (*methods)[ix];
688 fndecls; fndecls = OVL_NEXT (fndecls))
690 tree p1, p2;
692 fndecl = OVL_CURRENT (fndecls);
693 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
694 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
696 /* We cannot simply call decls_match because this doesn't
697 work for static member functions that are pretending to
698 be methods, and because the name may have been changed by
699 asm("new_name"). */
701 /* Get rid of the this parameter on functions that become
702 static. */
703 if (DECL_STATIC_FUNCTION_P (fndecl)
704 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
705 p1 = TREE_CHAIN (p1);
707 /* A member template definition only matches a member template
708 declaration. */
709 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
710 continue;
712 /* ref-qualifier or absence of same must match. */
713 if (type_memfn_rqual (TREE_TYPE (function))
714 != type_memfn_rqual (TREE_TYPE (fndecl)))
715 continue;
717 /* While finding a match, same types and params are not enough
718 if the function is versioned. Also check version ("target")
719 attributes. */
720 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
721 TREE_TYPE (TREE_TYPE (fndecl)))
722 && compparms (p1, p2)
723 && !targetm.target_option.function_versions (function, fndecl)
724 && (!is_template
725 || comp_template_parms (template_parms,
726 DECL_TEMPLATE_PARMS (fndecl)))
727 && (DECL_TEMPLATE_SPECIALIZATION (function)
728 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
729 && (!DECL_TEMPLATE_SPECIALIZATION (function)
730 || (DECL_TI_TEMPLATE (function)
731 == DECL_TI_TEMPLATE (fndecl))))
732 break;
734 if (fndecls)
736 if (pushed_scope)
737 pop_scope (pushed_scope);
738 return OVL_CURRENT (fndecls);
741 error_at (DECL_SOURCE_LOCATION (function),
742 "prototype for %q#D does not match any in class %qT",
743 function, ctype);
744 is_conv_op = DECL_CONV_FN_P (fndecl);
746 if (is_conv_op)
747 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
748 fndecls = (*methods)[ix];
749 while (fndecls)
751 fndecl = OVL_CURRENT (fndecls);
752 fndecls = OVL_NEXT (fndecls);
754 if (!fndecls && is_conv_op)
756 if (methods->length () > (size_t) ++ix)
758 fndecls = (*methods)[ix];
759 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
761 fndecls = NULL_TREE;
762 is_conv_op = false;
765 else
766 is_conv_op = false;
768 if (format)
769 format = " %+#D";
770 else if (fndecls)
771 format = N_("candidates are: %+#D");
772 else
773 format = N_("candidate is: %+#D");
774 error (format, fndecl);
777 else if (!COMPLETE_TYPE_P (ctype))
778 cxx_incomplete_type_error (function, ctype);
779 else
780 error ("no %q#D member function declared in class %qT",
781 function, ctype);
783 if (pushed_scope)
784 pop_scope (pushed_scope);
785 return error_mark_node;
788 /* DECL is a function with vague linkage. Remember it so that at the
789 end of the translation unit we can decide whether or not to emit
790 it. */
792 void
793 note_vague_linkage_fn (tree decl)
795 DECL_DEFER_OUTPUT (decl) = 1;
796 vec_safe_push (deferred_fns, decl);
799 /* As above, but for variable template instantiations. */
801 void
802 note_variable_template_instantiation (tree decl)
804 vec_safe_push (pending_statics, decl);
807 /* We have just processed the DECL, which is a static data member.
808 The other parameters are as for cp_finish_decl. */
810 void
811 finish_static_data_member_decl (tree decl,
812 tree init, bool init_const_expr_p,
813 tree asmspec_tree,
814 int flags)
816 DECL_CONTEXT (decl) = current_class_type;
818 /* We cannot call pushdecl here, because that would fill in the
819 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
820 the right thing, namely, to put this decl out straight away. */
822 if (! processing_template_decl)
823 vec_safe_push (pending_statics, decl);
825 if (LOCAL_CLASS_P (current_class_type)
826 /* We already complained about the template definition. */
827 && !DECL_TEMPLATE_INSTANTIATION (decl))
828 permerror (input_location, "local class %q#T shall not have static data member %q#D",
829 current_class_type, decl);
830 else
831 for (tree t = current_class_type; TYPE_P (t);
832 t = CP_TYPE_CONTEXT (t))
833 if (TYPE_ANONYMOUS_P (t))
835 if (permerror (DECL_SOURCE_LOCATION (decl),
836 "static data member %qD in unnamed class", decl))
837 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
838 "unnamed class defined here");
839 break;
842 DECL_IN_AGGR_P (decl) = 1;
844 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
845 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
846 SET_VAR_HAD_UNKNOWN_BOUND (decl);
848 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
851 /* DECLARATOR and DECLSPECS correspond to a class member. The other
852 parameters are as for cp_finish_decl. Return the DECL for the
853 class member declared. */
855 tree
856 grokfield (const cp_declarator *declarator,
857 cp_decl_specifier_seq *declspecs,
858 tree init, bool init_const_expr_p,
859 tree asmspec_tree,
860 tree attrlist)
862 tree value;
863 const char *asmspec = 0;
864 int flags;
865 tree name;
867 if (init
868 && TREE_CODE (init) == TREE_LIST
869 && TREE_VALUE (init) == error_mark_node
870 && TREE_CHAIN (init) == NULL_TREE)
871 init = NULL_TREE;
873 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
874 if (! value || value == error_mark_node)
875 /* friend or constructor went bad. */
876 return error_mark_node;
877 if (TREE_TYPE (value) == error_mark_node)
878 return value;
880 if (TREE_CODE (value) == TYPE_DECL && init)
882 error ("typedef %qD is initialized (use decltype instead)", value);
883 init = NULL_TREE;
886 /* Pass friendly classes back. */
887 if (value == void_type_node)
888 return value;
891 name = DECL_NAME (value);
893 if (name != NULL_TREE)
895 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
897 error ("explicit template argument list not allowed");
898 return error_mark_node;
901 if (IDENTIFIER_POINTER (name)[0] == '_'
902 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
903 error ("member %qD conflicts with virtual function table field name",
904 value);
907 /* Stash away type declarations. */
908 if (TREE_CODE (value) == TYPE_DECL)
910 DECL_NONLOCAL (value) = 1;
911 DECL_CONTEXT (value) = current_class_type;
913 if (attrlist)
915 int attrflags = 0;
917 /* If this is a typedef that names the class for linkage purposes
918 (7.1.3p8), apply any attributes directly to the type. */
919 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
920 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
921 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
923 cplus_decl_attributes (&value, attrlist, attrflags);
926 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
927 && TREE_TYPE (value) != error_mark_node
928 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
929 set_underlying_type (value);
931 /* It's important that push_template_decl below follows
932 set_underlying_type above so that the created template
933 carries the properly set type of VALUE. */
934 if (processing_template_decl)
935 value = push_template_decl (value);
937 record_locally_defined_typedef (value);
938 return value;
941 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
943 if (!friendp && DECL_IN_AGGR_P (value))
945 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
946 return void_type_node;
949 if (asmspec_tree && asmspec_tree != error_mark_node)
950 asmspec = TREE_STRING_POINTER (asmspec_tree);
952 if (init)
954 if (TREE_CODE (value) == FUNCTION_DECL)
956 if (init == ridpointers[(int)RID_DELETE])
958 DECL_DELETED_FN (value) = 1;
959 DECL_DECLARED_INLINE_P (value) = 1;
960 DECL_INITIAL (value) = error_mark_node;
962 else if (init == ridpointers[(int)RID_DEFAULT])
964 if (defaultable_fn_check (value))
966 DECL_DEFAULTED_FN (value) = 1;
967 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
968 DECL_DECLARED_INLINE_P (value) = 1;
971 else if (TREE_CODE (init) == DEFAULT_ARG)
972 error ("invalid initializer for member function %qD", value);
973 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
975 if (integer_zerop (init))
976 DECL_PURE_VIRTUAL_P (value) = 1;
977 else if (error_operand_p (init))
978 ; /* An error has already been reported. */
979 else
980 error ("invalid initializer for member function %qD",
981 value);
983 else
985 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
986 if (friendp)
987 error ("initializer specified for friend function %qD",
988 value);
989 else
990 error ("initializer specified for static member function %qD",
991 value);
994 else if (TREE_CODE (value) == FIELD_DECL)
995 /* C++11 NSDMI, keep going. */;
996 else if (!VAR_P (value))
997 gcc_unreachable ();
1000 /* Pass friend decls back. */
1001 if ((TREE_CODE (value) == FUNCTION_DECL
1002 || TREE_CODE (value) == TEMPLATE_DECL)
1003 && DECL_CONTEXT (value) != current_class_type)
1004 return value;
1006 /* Need to set this before push_template_decl. */
1007 if (VAR_P (value))
1008 DECL_CONTEXT (value) = current_class_type;
1010 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1012 value = push_template_decl (value);
1013 if (error_operand_p (value))
1014 return error_mark_node;
1017 if (attrlist)
1018 cplus_decl_attributes (&value, attrlist, 0);
1020 if (init && DIRECT_LIST_INIT_P (init))
1021 flags = LOOKUP_NORMAL;
1022 else
1023 flags = LOOKUP_IMPLICIT;
1025 switch (TREE_CODE (value))
1027 case VAR_DECL:
1028 finish_static_data_member_decl (value, init, init_const_expr_p,
1029 asmspec_tree, flags);
1030 return value;
1032 case FIELD_DECL:
1033 if (asmspec)
1034 error ("%<asm%> specifiers are not permitted on non-static data members");
1035 if (DECL_INITIAL (value) == error_mark_node)
1036 init = error_mark_node;
1037 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1038 NULL_TREE, flags);
1039 DECL_IN_AGGR_P (value) = 1;
1040 return value;
1042 case FUNCTION_DECL:
1043 if (asmspec)
1044 set_user_assembler_name (value, asmspec);
1046 cp_finish_decl (value,
1047 /*init=*/NULL_TREE,
1048 /*init_const_expr_p=*/false,
1049 asmspec_tree, flags);
1051 /* Pass friends back this way. */
1052 if (DECL_FRIEND_P (value))
1053 return void_type_node;
1055 DECL_IN_AGGR_P (value) = 1;
1056 return value;
1058 default:
1059 gcc_unreachable ();
1061 return NULL_TREE;
1064 /* Like `grokfield', but for bitfields.
1065 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1067 tree
1068 grokbitfield (const cp_declarator *declarator,
1069 cp_decl_specifier_seq *declspecs, tree width,
1070 tree attrlist)
1072 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1074 if (value == error_mark_node)
1075 return NULL_TREE; /* friends went bad. */
1076 if (TREE_TYPE (value) == error_mark_node)
1077 return value;
1079 /* Pass friendly classes back. */
1080 if (VOID_TYPE_P (value))
1081 return void_type_node;
1083 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1084 && (POINTER_TYPE_P (value)
1085 || !dependent_type_p (TREE_TYPE (value))))
1087 error ("bit-field %qD with non-integral type", value);
1088 return error_mark_node;
1091 if (TREE_CODE (value) == TYPE_DECL)
1093 error ("cannot declare %qD to be a bit-field type", value);
1094 return NULL_TREE;
1097 /* Usually, finish_struct_1 catches bitfields with invalid types.
1098 But, in the case of bitfields with function type, we confuse
1099 ourselves into thinking they are member functions, so we must
1100 check here. */
1101 if (TREE_CODE (value) == FUNCTION_DECL)
1103 error ("cannot declare bit-field %qD with function type",
1104 DECL_NAME (value));
1105 return NULL_TREE;
1108 if (DECL_IN_AGGR_P (value))
1110 error ("%qD is already defined in the class %qT", value,
1111 DECL_CONTEXT (value));
1112 return void_type_node;
1115 if (TREE_STATIC (value))
1117 error ("static member %qD cannot be a bit-field", value);
1118 return NULL_TREE;
1120 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1122 if (width != error_mark_node)
1124 /* The width must be an integer type. */
1125 if (!type_dependent_expression_p (width)
1126 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1127 error ("width of bit-field %qD has non-integral type %qT", value,
1128 TREE_TYPE (width));
1129 DECL_INITIAL (value) = width;
1130 SET_DECL_C_BIT_FIELD (value);
1133 DECL_IN_AGGR_P (value) = 1;
1135 if (attrlist)
1136 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1138 return value;
1142 /* Returns true iff ATTR is an attribute which needs to be applied at
1143 instantiation time rather than template definition time. */
1145 static bool
1146 is_late_template_attribute (tree attr, tree decl)
1148 tree name = get_attribute_name (attr);
1149 tree args = TREE_VALUE (attr);
1150 const struct attribute_spec *spec = lookup_attribute_spec (name);
1151 tree arg;
1153 if (!spec)
1154 /* Unknown attribute. */
1155 return false;
1157 /* Attribute weak handling wants to write out assembly right away. */
1158 if (is_attribute_p ("weak", name))
1159 return true;
1161 /* Attribute unused is applied directly, as it appertains to
1162 decls. */
1163 if (is_attribute_p ("unused", name))
1164 return false;
1166 /* Attribute tls_model wants to modify the symtab. */
1167 if (is_attribute_p ("tls_model", name))
1168 return true;
1170 /* #pragma omp declare simd attribute needs to be always deferred. */
1171 if (flag_openmp
1172 && is_attribute_p ("omp declare simd", name))
1173 return true;
1175 /* An attribute pack is clearly dependent. */
1176 if (args && PACK_EXPANSION_P (args))
1177 return true;
1179 /* If any of the arguments are dependent expressions, we can't evaluate
1180 the attribute until instantiation time. */
1181 for (arg = args; arg; arg = TREE_CHAIN (arg))
1183 tree t = TREE_VALUE (arg);
1185 /* If the first attribute argument is an identifier, only consider
1186 second and following arguments. Attributes like mode, format,
1187 cleanup and several target specific attributes aren't late
1188 just because they have an IDENTIFIER_NODE as first argument. */
1189 if (arg == args && identifier_p (t))
1190 continue;
1192 if (value_dependent_expression_p (t)
1193 || type_dependent_expression_p (t))
1194 return true;
1197 if (TREE_CODE (decl) == TYPE_DECL
1198 || TYPE_P (decl)
1199 || spec->type_required)
1201 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1203 /* We can't apply any attributes to a completely unknown type until
1204 instantiation time. */
1205 enum tree_code code = TREE_CODE (type);
1206 if (code == TEMPLATE_TYPE_PARM
1207 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1208 || code == TYPENAME_TYPE)
1209 return true;
1210 /* Also defer most attributes on dependent types. This is not
1211 necessary in all cases, but is the better default. */
1212 else if (dependent_type_p (type)
1213 /* But some attributes specifically apply to templates. */
1214 && !is_attribute_p ("abi_tag", name)
1215 && !is_attribute_p ("deprecated", name)
1216 && !is_attribute_p ("visibility", name))
1217 return true;
1218 else
1219 return false;
1221 else
1222 return false;
1225 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1226 applied at instantiation time and return them. If IS_DEPENDENT is true,
1227 the declaration itself is dependent, so all attributes should be applied
1228 at instantiation time. */
1230 static tree
1231 splice_template_attributes (tree *attr_p, tree decl)
1233 tree *p = attr_p;
1234 tree late_attrs = NULL_TREE;
1235 tree *q = &late_attrs;
1237 if (!p)
1238 return NULL_TREE;
1240 for (; *p; )
1242 if (is_late_template_attribute (*p, decl))
1244 ATTR_IS_DEPENDENT (*p) = 1;
1245 *q = *p;
1246 *p = TREE_CHAIN (*p);
1247 q = &TREE_CHAIN (*q);
1248 *q = NULL_TREE;
1250 else
1251 p = &TREE_CHAIN (*p);
1254 return late_attrs;
1257 /* Remove any late attributes from the list in ATTR_P and attach them to
1258 DECL_P. */
1260 static void
1261 save_template_attributes (tree *attr_p, tree *decl_p)
1263 tree *q;
1265 if (attr_p && *attr_p == error_mark_node)
1266 return;
1268 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1269 if (!late_attrs)
1270 return;
1272 if (DECL_P (*decl_p))
1273 q = &DECL_ATTRIBUTES (*decl_p);
1274 else
1275 q = &TYPE_ATTRIBUTES (*decl_p);
1277 tree old_attrs = *q;
1279 /* Merge the late attributes at the beginning with the attribute
1280 list. */
1281 late_attrs = merge_attributes (late_attrs, *q);
1282 *q = late_attrs;
1284 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1286 /* We've added new attributes directly to the main variant, so
1287 now we need to update all of the other variants to include
1288 these new attributes. */
1289 tree variant;
1290 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1291 variant = TYPE_NEXT_VARIANT (variant))
1293 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1294 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1299 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1300 to a typedef which gives a previously anonymous class or enum a name for
1301 linkage purposes. */
1303 bool
1304 attributes_naming_typedef_ok (tree attrs)
1306 for (; attrs; attrs = TREE_CHAIN (attrs))
1308 tree name = get_attribute_name (attrs);
1309 if (is_attribute_p ("vector_size", name))
1310 return false;
1312 return true;
1315 /* Like reconstruct_complex_type, but handle also template trees. */
1317 tree
1318 cp_reconstruct_complex_type (tree type, tree bottom)
1320 tree inner, outer;
1321 bool late_return_type_p = false;
1323 if (TYPE_PTR_P (type))
1325 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1326 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1327 TYPE_REF_CAN_ALIAS_ALL (type));
1329 else if (TREE_CODE (type) == REFERENCE_TYPE)
1331 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1332 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1333 TYPE_REF_CAN_ALIAS_ALL (type));
1335 else if (TREE_CODE (type) == ARRAY_TYPE)
1337 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1338 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1339 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1340 element type qualification will be handled by the recursive
1341 cp_reconstruct_complex_type call and cp_build_qualified_type
1342 for ARRAY_TYPEs changes the element type. */
1343 return outer;
1345 else if (TREE_CODE (type) == FUNCTION_TYPE)
1347 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1348 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1349 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1350 outer = apply_memfn_quals (outer,
1351 type_memfn_quals (type),
1352 type_memfn_rqual (type));
1354 else if (TREE_CODE (type) == METHOD_TYPE)
1356 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1357 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1358 /* The build_method_type_directly() routine prepends 'this' to argument list,
1359 so we must compensate by getting rid of it. */
1360 outer
1361 = build_method_type_directly
1362 (class_of_this_parm (type), inner,
1363 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1365 else if (TREE_CODE (type) == OFFSET_TYPE)
1367 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1368 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1370 else
1371 return bottom;
1373 if (TYPE_ATTRIBUTES (type))
1374 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1375 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1377 if (late_return_type_p)
1378 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1380 return outer;
1383 /* Replaces any constexpr expression that may be into the attributes
1384 arguments with their reduced value. */
1386 static void
1387 cp_check_const_attributes (tree attributes)
1389 if (attributes == error_mark_node)
1390 return;
1392 tree attr;
1393 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1395 tree arg;
1396 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1398 tree expr = TREE_VALUE (arg);
1399 if (EXPR_P (expr))
1400 TREE_VALUE (arg) = maybe_constant_value (expr);
1405 /* Return true if TYPE is an OpenMP mappable type. */
1406 bool
1407 cp_omp_mappable_type (tree type)
1409 /* Mappable type has to be complete. */
1410 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1411 return false;
1412 /* Arrays have mappable type if the elements have mappable type. */
1413 while (TREE_CODE (type) == ARRAY_TYPE)
1414 type = TREE_TYPE (type);
1415 /* A mappable type cannot contain virtual members. */
1416 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1417 return false;
1418 /* All data members must be non-static. */
1419 if (CLASS_TYPE_P (type))
1421 tree field;
1422 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1423 if (VAR_P (field))
1424 return false;
1425 /* All fields must have mappable types. */
1426 else if (TREE_CODE (field) == FIELD_DECL
1427 && !cp_omp_mappable_type (TREE_TYPE (field)))
1428 return false;
1430 return true;
1433 /* Like decl_attributes, but handle C++ complexity. */
1435 void
1436 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1438 if (*decl == NULL_TREE || *decl == void_type_node
1439 || *decl == error_mark_node)
1440 return;
1442 /* Add implicit "omp declare target" attribute if requested. */
1443 if (scope_chain->omp_declare_target_attribute
1444 && ((VAR_P (*decl)
1445 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1446 || TREE_CODE (*decl) == FUNCTION_DECL))
1448 if (VAR_P (*decl)
1449 && DECL_CLASS_SCOPE_P (*decl))
1450 error ("%q+D static data member inside of declare target directive",
1451 *decl);
1452 else if (VAR_P (*decl)
1453 && (DECL_FUNCTION_SCOPE_P (*decl)
1454 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1455 error ("%q+D in block scope inside of declare target directive",
1456 *decl);
1457 else if (!processing_template_decl
1458 && VAR_P (*decl)
1459 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1460 error ("%q+D in declare target directive does not have mappable type",
1461 *decl);
1462 else
1463 attributes = tree_cons (get_identifier ("omp declare target"),
1464 NULL_TREE, attributes);
1467 if (processing_template_decl)
1469 if (check_for_bare_parameter_packs (attributes))
1470 return;
1472 save_template_attributes (&attributes, decl);
1475 cp_check_const_attributes (attributes);
1477 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1478 decl = &DECL_TEMPLATE_RESULT (*decl);
1480 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1482 attributes
1483 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1484 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1485 attributes, flags);
1487 else
1488 decl_attributes (decl, attributes, flags);
1490 if (TREE_CODE (*decl) == TYPE_DECL)
1491 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1493 /* Propagate deprecation out to the template. */
1494 if (TREE_DEPRECATED (*decl))
1495 if (tree ti = get_template_info (*decl))
1497 tree tmpl = TI_TEMPLATE (ti);
1498 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1499 : DECL_TEMPLATE_RESULT (tmpl));
1500 if (*decl == pattern)
1501 TREE_DEPRECATED (tmpl) = true;
1505 /* Walks through the namespace- or function-scope anonymous union
1506 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1507 Returns one of the fields for use in the mangled name. */
1509 static tree
1510 build_anon_union_vars (tree type, tree object)
1512 tree main_decl = NULL_TREE;
1513 tree field;
1515 /* Rather than write the code to handle the non-union case,
1516 just give an error. */
1517 if (TREE_CODE (type) != UNION_TYPE)
1519 error ("anonymous struct not inside named type");
1520 return error_mark_node;
1523 for (field = TYPE_FIELDS (type);
1524 field != NULL_TREE;
1525 field = DECL_CHAIN (field))
1527 tree decl;
1528 tree ref;
1530 if (DECL_ARTIFICIAL (field))
1531 continue;
1532 if (TREE_CODE (field) != FIELD_DECL)
1534 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1535 "have non-static data members", field);
1536 continue;
1539 if (TREE_PRIVATE (field))
1540 permerror (input_location, "private member %q+#D in anonymous union", field);
1541 else if (TREE_PROTECTED (field))
1542 permerror (input_location, "protected member %q+#D in anonymous union", field);
1544 if (processing_template_decl)
1545 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1546 DECL_NAME (field), NULL_TREE);
1547 else
1548 ref = build_class_member_access_expr (object, field, NULL_TREE,
1549 false, tf_warning_or_error);
1551 if (DECL_NAME (field))
1553 tree base;
1555 decl = build_decl (input_location,
1556 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1557 DECL_ANON_UNION_VAR_P (decl) = 1;
1558 DECL_ARTIFICIAL (decl) = 1;
1560 base = get_base_address (object);
1561 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1562 TREE_STATIC (decl) = TREE_STATIC (base);
1563 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1565 SET_DECL_VALUE_EXPR (decl, ref);
1566 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1568 decl = pushdecl (decl);
1570 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1571 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1572 else
1573 decl = 0;
1575 if (main_decl == NULL_TREE)
1576 main_decl = decl;
1579 return main_decl;
1582 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1583 anonymous union, then all members must be laid out together. PUBLIC_P
1584 is nonzero if this union is not declared static. */
1586 void
1587 finish_anon_union (tree anon_union_decl)
1589 tree type;
1590 tree main_decl;
1591 bool public_p;
1593 if (anon_union_decl == error_mark_node)
1594 return;
1596 type = TREE_TYPE (anon_union_decl);
1597 public_p = TREE_PUBLIC (anon_union_decl);
1599 /* The VAR_DECL's context is the same as the TYPE's context. */
1600 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1602 if (TYPE_FIELDS (type) == NULL_TREE)
1603 return;
1605 if (public_p)
1607 error ("namespace-scope anonymous aggregates must be static");
1608 return;
1611 main_decl = build_anon_union_vars (type, anon_union_decl);
1612 if (main_decl == error_mark_node)
1613 return;
1614 if (main_decl == NULL_TREE)
1616 warning (0, "anonymous union with no members");
1617 return;
1620 if (!processing_template_decl)
1622 /* Use main_decl to set the mangled name. */
1623 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1624 maybe_commonize_var (anon_union_decl);
1625 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1626 mangle_decl (anon_union_decl);
1627 DECL_NAME (anon_union_decl) = NULL_TREE;
1630 pushdecl (anon_union_decl);
1631 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1634 /* Auxiliary functions to make type signatures for
1635 `operator new' and `operator delete' correspond to
1636 what compiler will be expecting. */
1638 tree
1639 coerce_new_type (tree type)
1641 int e = 0;
1642 tree args = TYPE_ARG_TYPES (type);
1644 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1646 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1648 e = 1;
1649 error ("%<operator new%> must return type %qT", ptr_type_node);
1652 if (args && args != void_list_node)
1654 if (TREE_PURPOSE (args))
1656 /* [basic.stc.dynamic.allocation]
1658 The first parameter shall not have an associated default
1659 argument. */
1660 error ("the first parameter of %<operator new%> cannot "
1661 "have a default argument");
1662 /* Throw away the default argument. */
1663 TREE_PURPOSE (args) = NULL_TREE;
1666 if (!same_type_p (TREE_VALUE (args), size_type_node))
1668 e = 2;
1669 args = TREE_CHAIN (args);
1672 else
1673 e = 2;
1675 if (e == 2)
1676 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1677 "as first parameter", size_type_node);
1679 switch (e)
1681 case 2:
1682 args = tree_cons (NULL_TREE, size_type_node, args);
1683 /* Fall through. */
1684 case 1:
1685 type = build_exception_variant
1686 (build_function_type (ptr_type_node, args),
1687 TYPE_RAISES_EXCEPTIONS (type));
1688 /* Fall through. */
1689 default:;
1691 return type;
1694 tree
1695 coerce_delete_type (tree type)
1697 int e = 0;
1698 tree args = TYPE_ARG_TYPES (type);
1700 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1702 if (!same_type_p (TREE_TYPE (type), void_type_node))
1704 e = 1;
1705 error ("%<operator delete%> must return type %qT", void_type_node);
1708 if (!args || args == void_list_node
1709 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1711 e = 2;
1712 if (args && args != void_list_node)
1713 args = TREE_CHAIN (args);
1714 error ("%<operator delete%> takes type %qT as first parameter",
1715 ptr_type_node);
1717 switch (e)
1719 case 2:
1720 args = tree_cons (NULL_TREE, ptr_type_node, args);
1721 /* Fall through. */
1722 case 1:
1723 type = build_exception_variant
1724 (build_function_type (void_type_node, args),
1725 TYPE_RAISES_EXCEPTIONS (type));
1726 /* Fall through. */
1727 default:;
1730 return type;
1733 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1734 and mark them as needed. */
1736 static void
1737 mark_vtable_entries (tree decl)
1739 tree fnaddr;
1740 unsigned HOST_WIDE_INT idx;
1742 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1743 idx, fnaddr)
1745 tree fn;
1747 STRIP_NOPS (fnaddr);
1749 if (TREE_CODE (fnaddr) != ADDR_EXPR
1750 && TREE_CODE (fnaddr) != FDESC_EXPR)
1751 /* This entry is an offset: a virtual base class offset, a
1752 virtual call offset, an RTTI offset, etc. */
1753 continue;
1755 fn = TREE_OPERAND (fnaddr, 0);
1756 TREE_ADDRESSABLE (fn) = 1;
1757 /* When we don't have vcall offsets, we output thunks whenever
1758 we output the vtables that contain them. With vcall offsets,
1759 we know all the thunks we'll need when we emit a virtual
1760 function, so we emit the thunks there instead. */
1761 if (DECL_THUNK_P (fn))
1762 use_thunk (fn, /*emit_p=*/0);
1763 mark_used (fn);
1767 /* Set DECL up to have the closest approximation of "initialized common"
1768 linkage available. */
1770 void
1771 comdat_linkage (tree decl)
1773 if (flag_weak)
1774 make_decl_one_only (decl, cxx_comdat_group (decl));
1775 else if (TREE_CODE (decl) == FUNCTION_DECL
1776 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1777 /* We can just emit function and compiler-generated variables
1778 statically; having multiple copies is (for the most part) only
1779 a waste of space.
1781 There are two correctness issues, however: the address of a
1782 template instantiation with external linkage should be the
1783 same, independent of what translation unit asks for the
1784 address, and this will not hold when we emit multiple copies of
1785 the function. However, there's little else we can do.
1787 Also, by default, the typeinfo implementation assumes that
1788 there will be only one copy of the string used as the name for
1789 each type. Therefore, if weak symbols are unavailable, the
1790 run-time library should perform a more conservative check; it
1791 should perform a string comparison, rather than an address
1792 comparison. */
1793 TREE_PUBLIC (decl) = 0;
1794 else
1796 /* Static data member template instantiations, however, cannot
1797 have multiple copies. */
1798 if (DECL_INITIAL (decl) == 0
1799 || DECL_INITIAL (decl) == error_mark_node)
1800 DECL_COMMON (decl) = 1;
1801 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1803 DECL_COMMON (decl) = 1;
1804 DECL_INITIAL (decl) = error_mark_node;
1806 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1808 /* We can't do anything useful; leave vars for explicit
1809 instantiation. */
1810 DECL_EXTERNAL (decl) = 1;
1811 DECL_NOT_REALLY_EXTERN (decl) = 0;
1815 DECL_COMDAT (decl) = 1;
1818 /* For win32 we also want to put explicit instantiations in
1819 linkonce sections, so that they will be merged with implicit
1820 instantiations; otherwise we get duplicate symbol errors.
1821 For Darwin we do not want explicit instantiations to be
1822 linkonce. */
1824 void
1825 maybe_make_one_only (tree decl)
1827 /* We used to say that this was not necessary on targets that support weak
1828 symbols, because the implicit instantiations will defer to the explicit
1829 one. However, that's not actually the case in SVR4; a strong definition
1830 after a weak one is an error. Also, not making explicit
1831 instantiations one_only means that we can end up with two copies of
1832 some template instantiations. */
1833 if (! flag_weak)
1834 return;
1836 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1837 we can get away with not emitting them if they aren't used. We need
1838 to for variables so that cp_finish_decl will update their linkage,
1839 because their DECL_INITIAL may not have been set properly yet. */
1841 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1842 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1843 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1845 make_decl_one_only (decl, cxx_comdat_group (decl));
1847 if (VAR_P (decl))
1849 varpool_node *node = varpool_node::get_create (decl);
1850 DECL_COMDAT (decl) = 1;
1851 /* Mark it needed so we don't forget to emit it. */
1852 node->forced_by_abi = true;
1853 TREE_USED (decl) = 1;
1858 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1859 This predicate will give the right answer during parsing of the
1860 function, which other tests may not. */
1862 bool
1863 vague_linkage_p (tree decl)
1865 if (!TREE_PUBLIC (decl))
1867 gcc_checking_assert (!DECL_COMDAT (decl));
1868 return false;
1870 /* Unfortunately, import_export_decl has not always been called
1871 before the function is processed, so we cannot simply check
1872 DECL_COMDAT. */
1873 if (DECL_COMDAT (decl)
1874 || (TREE_CODE (decl) == FUNCTION_DECL
1875 && DECL_DECLARED_INLINE_P (decl))
1876 || (DECL_LANG_SPECIFIC (decl)
1877 && DECL_TEMPLATE_INSTANTIATION (decl)))
1878 return true;
1879 else if (DECL_FUNCTION_SCOPE_P (decl))
1880 /* A local static in an inline effectively has vague linkage. */
1881 return (TREE_STATIC (decl)
1882 && vague_linkage_p (DECL_CONTEXT (decl)));
1883 else
1884 return false;
1887 /* Determine whether or not we want to specifically import or export CTYPE,
1888 using various heuristics. */
1890 static void
1891 import_export_class (tree ctype)
1893 /* -1 for imported, 1 for exported. */
1894 int import_export = 0;
1896 /* It only makes sense to call this function at EOF. The reason is
1897 that this function looks at whether or not the first non-inline
1898 non-abstract virtual member function has been defined in this
1899 translation unit. But, we can't possibly know that until we've
1900 seen the entire translation unit. */
1901 gcc_assert (at_eof);
1903 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1904 return;
1906 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1907 we will have CLASSTYPE_INTERFACE_ONLY set but not
1908 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1909 heuristic because someone will supply a #pragma implementation
1910 elsewhere, and deducing it here would produce a conflict. */
1911 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1912 return;
1914 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1915 import_export = -1;
1916 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1917 import_export = 1;
1918 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1919 && !flag_implicit_templates)
1920 /* For a template class, without -fimplicit-templates, check the
1921 repository. If the virtual table is assigned to this
1922 translation unit, then export the class; otherwise, import
1923 it. */
1924 import_export = repo_export_class_p (ctype) ? 1 : -1;
1925 else if (TYPE_POLYMORPHIC_P (ctype))
1927 /* The ABI specifies that the virtual table and associated
1928 information are emitted with the key method, if any. */
1929 tree method = CLASSTYPE_KEY_METHOD (ctype);
1930 /* If weak symbol support is not available, then we must be
1931 careful not to emit the vtable when the key function is
1932 inline. An inline function can be defined in multiple
1933 translation units. If we were to emit the vtable in each
1934 translation unit containing a definition, we would get
1935 multiple definition errors at link-time. */
1936 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1937 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1940 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1941 a definition anywhere else. */
1942 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1943 import_export = 0;
1945 /* Allow back ends the chance to overrule the decision. */
1946 if (targetm.cxx.import_export_class)
1947 import_export = targetm.cxx.import_export_class (ctype, import_export);
1949 if (import_export)
1951 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1952 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1956 /* Return true if VAR has already been provided to the back end; in that
1957 case VAR should not be modified further by the front end. */
1958 static bool
1959 var_finalized_p (tree var)
1961 return varpool_node::get_create (var)->definition;
1964 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1965 must be emitted in this translation unit. Mark it as such. */
1967 void
1968 mark_needed (tree decl)
1970 TREE_USED (decl) = 1;
1971 if (TREE_CODE (decl) == FUNCTION_DECL)
1973 /* Extern inline functions don't become needed when referenced.
1974 If we know a method will be emitted in other TU and no new
1975 functions can be marked reachable, just use the external
1976 definition. */
1977 struct cgraph_node *node = cgraph_node::get_create (decl);
1978 node->forced_by_abi = true;
1980 /* #pragma interface and -frepo code can call mark_needed for
1981 maybe-in-charge 'tors; mark the clones as well. */
1982 tree clone;
1983 FOR_EACH_CLONE (clone, decl)
1984 mark_needed (clone);
1986 else if (VAR_P (decl))
1988 varpool_node *node = varpool_node::get_create (decl);
1989 /* C++ frontend use mark_decl_references to force COMDAT variables
1990 to be output that might appear dead otherwise. */
1991 node->forced_by_abi = true;
1995 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1996 returns true if a definition of this entity should be provided in
1997 this object file. Callers use this function to determine whether
1998 or not to let the back end know that a definition of DECL is
1999 available in this translation unit. */
2001 bool
2002 decl_needed_p (tree decl)
2004 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2005 /* This function should only be called at the end of the translation
2006 unit. We cannot be sure of whether or not something will be
2007 COMDAT until that point. */
2008 gcc_assert (at_eof);
2010 /* All entities with external linkage that are not COMDAT/EXTERN should be
2011 emitted; they may be referred to from other object files. */
2012 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2013 return true;
2014 /* Functions marked "dllexport" must be emitted so that they are
2015 visible to other DLLs. */
2016 if (flag_keep_inline_dllexport
2017 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2018 return true;
2020 /* When not optimizing, do not bother to produce definitions for extern
2021 symbols. */
2022 if (DECL_REALLY_EXTERN (decl)
2023 && ((TREE_CODE (decl) != FUNCTION_DECL
2024 && !optimize)
2025 || (TREE_CODE (decl) == FUNCTION_DECL
2026 && !opt_for_fn (decl, optimize)))
2027 && !lookup_attribute ("always_inline", decl))
2028 return false;
2030 /* If this entity was used, let the back end see it; it will decide
2031 whether or not to emit it into the object file. */
2032 if (TREE_USED (decl))
2033 return true;
2034 /* Virtual functions might be needed for devirtualization. */
2035 if (flag_devirtualize
2036 && TREE_CODE (decl) == FUNCTION_DECL
2037 && DECL_VIRTUAL_P (decl))
2038 return true;
2039 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2040 reference to DECL might cause it to be emitted later. */
2041 return false;
2044 /* If necessary, write out the vtables for the dynamic class CTYPE.
2045 Returns true if any vtables were emitted. */
2047 static bool
2048 maybe_emit_vtables (tree ctype)
2050 tree vtbl;
2051 tree primary_vtbl;
2052 int needed = 0;
2053 varpool_node *current = NULL, *last = NULL;
2055 /* If the vtables for this class have already been emitted there is
2056 nothing more to do. */
2057 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2058 if (var_finalized_p (primary_vtbl))
2059 return false;
2060 /* Ignore dummy vtables made by get_vtable_decl. */
2061 if (TREE_TYPE (primary_vtbl) == void_type_node)
2062 return false;
2064 /* On some targets, we cannot determine the key method until the end
2065 of the translation unit -- which is when this function is
2066 called. */
2067 if (!targetm.cxx.key_method_may_be_inline ())
2068 determine_key_method (ctype);
2070 /* See if any of the vtables are needed. */
2071 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2073 import_export_decl (vtbl);
2074 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2075 needed = 1;
2077 if (!needed)
2079 /* If the references to this class' vtables are optimized away,
2080 still emit the appropriate debugging information. See
2081 dfs_debug_mark. */
2082 if (DECL_COMDAT (primary_vtbl)
2083 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2084 note_debug_info_needed (ctype);
2085 return false;
2088 /* The ABI requires that we emit all of the vtables if we emit any
2089 of them. */
2090 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2092 /* Mark entities references from the virtual table as used. */
2093 mark_vtable_entries (vtbl);
2095 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2097 vec<tree, va_gc> *cleanups = NULL;
2098 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2099 LOOKUP_NORMAL);
2101 /* It had better be all done at compile-time. */
2102 gcc_assert (!expr && !cleanups);
2105 /* Write it out. */
2106 DECL_EXTERNAL (vtbl) = 0;
2107 rest_of_decl_compilation (vtbl, 1, 1);
2109 /* Because we're only doing syntax-checking, we'll never end up
2110 actually marking the variable as written. */
2111 if (flag_syntax_only)
2112 TREE_ASM_WRITTEN (vtbl) = 1;
2113 else if (DECL_ONE_ONLY (vtbl))
2115 current = varpool_node::get_create (vtbl);
2116 if (last)
2117 current->add_to_same_comdat_group (last);
2118 last = current;
2122 /* Since we're writing out the vtable here, also write the debug
2123 info. */
2124 note_debug_info_needed (ctype);
2126 return true;
2129 /* A special return value from type_visibility meaning internal
2130 linkage. */
2132 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2134 /* walk_tree helper function for type_visibility. */
2136 static tree
2137 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2139 int *vis_p = (int *)data;
2140 if (! TYPE_P (*tp))
2142 *walk_subtrees = 0;
2144 else if (OVERLOAD_TYPE_P (*tp)
2145 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2147 *vis_p = VISIBILITY_ANON;
2148 return *tp;
2150 else if (CLASS_TYPE_P (*tp)
2151 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2152 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2153 return NULL;
2156 /* Returns the visibility of TYPE, which is the minimum visibility of its
2157 component types. */
2159 static int
2160 type_visibility (tree type)
2162 int vis = VISIBILITY_DEFAULT;
2163 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2164 return vis;
2167 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2168 specified (or if VISIBILITY is static). If TMPL is true, this
2169 constraint is for a template argument, and takes precedence
2170 over explicitly-specified visibility on the template. */
2172 static void
2173 constrain_visibility (tree decl, int visibility, bool tmpl)
2175 if (visibility == VISIBILITY_ANON)
2177 /* extern "C" declarations aren't affected by the anonymous
2178 namespace. */
2179 if (!DECL_EXTERN_C_P (decl))
2181 TREE_PUBLIC (decl) = 0;
2182 DECL_WEAK (decl) = 0;
2183 DECL_COMMON (decl) = 0;
2184 DECL_COMDAT (decl) = false;
2185 if (VAR_OR_FUNCTION_DECL_P (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 (VAR_P (m))
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 (VAR_P (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 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3012 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3013 if (DECL_ONE_ONLY (decl))
3014 make_decl_one_only (guard, cxx_comdat_group (guard));
3015 if (TREE_PUBLIC (decl))
3016 DECL_WEAK (guard) = DECL_WEAK (decl);
3017 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3018 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3020 DECL_ARTIFICIAL (guard) = 1;
3021 DECL_IGNORED_P (guard) = 1;
3022 TREE_USED (guard) = 1;
3023 pushdecl_top_level_and_finish (guard, NULL_TREE);
3025 return guard;
3028 /* Return an atomic load of src with the appropriate memory model. */
3030 static tree
3031 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3033 tree ptr_type = build_pointer_type (char_type_node);
3034 tree mem_model = build_int_cst (integer_type_node, model);
3035 tree t, addr, val;
3036 unsigned int size;
3037 int fncode;
3039 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3041 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3042 t = builtin_decl_implicit ((enum built_in_function) fncode);
3044 addr = build1 (ADDR_EXPR, ptr_type, src);
3045 val = build_call_expr (t, 2, addr, mem_model);
3046 return val;
3049 /* Return those bits of the GUARD variable that should be set when the
3050 guarded entity is actually initialized. */
3052 static tree
3053 get_guard_bits (tree guard)
3055 if (!targetm.cxx.guard_mask_bit ())
3057 /* We only set the first byte of the guard, in order to leave room
3058 for a mutex in the high-order bits. */
3059 guard = build1 (ADDR_EXPR,
3060 build_pointer_type (TREE_TYPE (guard)),
3061 guard);
3062 guard = build1 (NOP_EXPR,
3063 build_pointer_type (char_type_node),
3064 guard);
3065 guard = build1 (INDIRECT_REF, char_type_node, guard);
3068 return guard;
3071 /* Return an expression which determines whether or not the GUARD
3072 variable has already been initialized. */
3074 tree
3075 get_guard_cond (tree guard, bool thread_safe)
3077 tree guard_value;
3079 if (!thread_safe)
3080 guard = get_guard_bits (guard);
3081 else
3082 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3084 /* Mask off all but the low bit. */
3085 if (targetm.cxx.guard_mask_bit ())
3087 guard_value = integer_one_node;
3088 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3089 guard_value = convert (TREE_TYPE (guard), guard_value);
3090 guard = cp_build_binary_op (input_location,
3091 BIT_AND_EXPR, guard, guard_value,
3092 tf_warning_or_error);
3095 guard_value = integer_zero_node;
3096 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3097 guard_value = convert (TREE_TYPE (guard), guard_value);
3098 return cp_build_binary_op (input_location,
3099 EQ_EXPR, guard, guard_value,
3100 tf_warning_or_error);
3103 /* Return an expression which sets the GUARD variable, indicating that
3104 the variable being guarded has been initialized. */
3106 tree
3107 set_guard (tree guard)
3109 tree guard_init;
3111 /* Set the GUARD to one. */
3112 guard = get_guard_bits (guard);
3113 guard_init = integer_one_node;
3114 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3115 guard_init = convert (TREE_TYPE (guard), guard_init);
3116 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3117 tf_warning_or_error);
3120 /* Returns true iff we can tell that VAR does not have a dynamic
3121 initializer. */
3123 static bool
3124 var_defined_without_dynamic_init (tree var)
3126 /* If it's defined in another TU, we can't tell. */
3127 if (DECL_EXTERNAL (var))
3128 return false;
3129 /* If it has a non-trivial destructor, registering the destructor
3130 counts as dynamic initialization. */
3131 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3132 return false;
3133 /* If it's in this TU, its initializer has been processed, unless
3134 it's a case of self-initialization, then DECL_INITIALIZED_P is
3135 false while the initializer is handled by finish_id_expression. */
3136 if (!DECL_INITIALIZED_P (var))
3137 return false;
3138 /* If it has no initializer or a constant one, it's not dynamic. */
3139 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3140 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3143 /* Returns true iff VAR is a variable that needs uses to be
3144 wrapped for possible dynamic initialization. */
3146 static bool
3147 var_needs_tls_wrapper (tree var)
3149 return (!error_operand_p (var)
3150 && CP_DECL_THREAD_LOCAL_P (var)
3151 && !DECL_GNU_TLS_P (var)
3152 && !DECL_FUNCTION_SCOPE_P (var)
3153 && !var_defined_without_dynamic_init (var));
3156 /* Get the FUNCTION_DECL for the shared TLS init function for this
3157 translation unit. */
3159 static tree
3160 get_local_tls_init_fn (void)
3162 tree sname = get_identifier ("__tls_init");
3163 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3164 if (!fn)
3166 fn = build_lang_decl (FUNCTION_DECL, sname,
3167 build_function_type (void_type_node,
3168 void_list_node));
3169 SET_DECL_LANGUAGE (fn, lang_c);
3170 TREE_PUBLIC (fn) = false;
3171 DECL_ARTIFICIAL (fn) = true;
3172 mark_used (fn);
3173 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3175 return fn;
3178 /* Get a FUNCTION_DECL for the init function for the thread_local
3179 variable VAR. The init function will be an alias to the function
3180 that initializes all the non-local TLS variables in the translation
3181 unit. The init function is only used by the wrapper function. */
3183 static tree
3184 get_tls_init_fn (tree var)
3186 /* Only C++11 TLS vars need this init fn. */
3187 if (!var_needs_tls_wrapper (var))
3188 return NULL_TREE;
3190 /* If -fno-extern-tls-init, assume that we don't need to call
3191 a tls init function for a variable defined in another TU. */
3192 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3193 return NULL_TREE;
3195 #ifdef ASM_OUTPUT_DEF
3196 /* If the variable is internal, or if we can't generate aliases,
3197 call the local init function directly. */
3198 if (!TREE_PUBLIC (var))
3199 #endif
3200 return get_local_tls_init_fn ();
3202 tree sname = mangle_tls_init_fn (var);
3203 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3204 if (!fn)
3206 fn = build_lang_decl (FUNCTION_DECL, sname,
3207 build_function_type (void_type_node,
3208 void_list_node));
3209 SET_DECL_LANGUAGE (fn, lang_c);
3210 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3211 DECL_ARTIFICIAL (fn) = true;
3212 DECL_COMDAT (fn) = DECL_COMDAT (var);
3213 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3214 if (DECL_ONE_ONLY (var))
3215 make_decl_one_only (fn, cxx_comdat_group (fn));
3216 if (TREE_PUBLIC (var))
3218 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3219 /* If the variable is defined somewhere else and might have static
3220 initialization, make the init function a weak reference. */
3221 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3222 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3223 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3224 && DECL_EXTERNAL (var))
3225 declare_weak (fn);
3226 else
3227 DECL_WEAK (fn) = DECL_WEAK (var);
3229 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3230 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3231 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3232 DECL_IGNORED_P (fn) = 1;
3233 mark_used (fn);
3235 DECL_BEFRIENDING_CLASSES (fn) = var;
3237 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3239 return fn;
3242 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3243 variable VAR. The wrapper function calls the init function (if any) for
3244 VAR and then returns a reference to VAR. The wrapper function is used
3245 in place of VAR everywhere VAR is mentioned. */
3247 tree
3248 get_tls_wrapper_fn (tree var)
3250 /* Only C++11 TLS vars need this wrapper fn. */
3251 if (!var_needs_tls_wrapper (var))
3252 return NULL_TREE;
3254 tree sname = mangle_tls_wrapper_fn (var);
3255 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3256 if (!fn)
3258 /* A named rvalue reference is an lvalue, so the wrapper should
3259 always return an lvalue reference. */
3260 tree type = non_reference (TREE_TYPE (var));
3261 type = build_reference_type (type);
3262 tree fntype = build_function_type (type, void_list_node);
3263 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3264 SET_DECL_LANGUAGE (fn, lang_c);
3265 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3266 DECL_ARTIFICIAL (fn) = true;
3267 DECL_IGNORED_P (fn) = 1;
3268 /* The wrapper is inline and emitted everywhere var is used. */
3269 DECL_DECLARED_INLINE_P (fn) = true;
3270 if (TREE_PUBLIC (var))
3272 comdat_linkage (fn);
3273 #ifdef HAVE_GAS_HIDDEN
3274 /* Make the wrapper bind locally; there's no reason to share
3275 the wrapper between multiple shared objects. */
3276 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3277 DECL_VISIBILITY_SPECIFIED (fn) = true;
3278 #endif
3280 if (!TREE_PUBLIC (fn))
3281 DECL_INTERFACE_KNOWN (fn) = true;
3282 mark_used (fn);
3283 note_vague_linkage_fn (fn);
3285 #if 0
3286 /* We want CSE to commonize calls to the wrapper, but marking it as
3287 pure is unsafe since it has side-effects. I guess we need a new
3288 ECF flag even weaker than ECF_PURE. FIXME! */
3289 DECL_PURE_P (fn) = true;
3290 #endif
3292 DECL_BEFRIENDING_CLASSES (fn) = var;
3294 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3296 return fn;
3299 /* At EOF, generate the definition for the TLS wrapper function FN:
3301 T& var_wrapper() {
3302 if (init_fn) init_fn();
3303 return var;
3304 } */
3306 static void
3307 generate_tls_wrapper (tree fn)
3309 tree var = DECL_BEFRIENDING_CLASSES (fn);
3311 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3312 tree body = begin_function_body ();
3313 /* Only call the init fn if there might be one. */
3314 if (tree init_fn = get_tls_init_fn (var))
3316 tree if_stmt = NULL_TREE;
3317 /* If init_fn is a weakref, make sure it exists before calling. */
3318 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3320 if_stmt = begin_if_stmt ();
3321 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3322 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3323 NE_EXPR, addr, nullptr_node,
3324 tf_warning_or_error);
3325 finish_if_stmt_cond (cond, if_stmt);
3327 finish_expr_stmt (build_cxx_call
3328 (init_fn, 0, NULL, tf_warning_or_error));
3329 if (if_stmt)
3331 finish_then_clause (if_stmt);
3332 finish_if_stmt (if_stmt);
3335 else
3336 /* If there's no initialization, the wrapper is a constant function. */
3337 TREE_READONLY (fn) = true;
3338 finish_return_stmt (convert_from_reference (var));
3339 finish_function_body (body);
3340 expand_or_defer_fn (finish_function (0));
3343 /* Start the process of running a particular set of global constructors
3344 or destructors. Subroutine of do_[cd]tors. Also called from
3345 vtv_start_verification_constructor_init_function. */
3347 static tree
3348 start_objects (int method_type, int initp)
3350 tree body;
3351 tree fndecl;
3352 char type[14];
3354 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3356 if (initp != DEFAULT_INIT_PRIORITY)
3358 char joiner;
3360 #ifdef JOINER
3361 joiner = JOINER;
3362 #else
3363 joiner = '_';
3364 #endif
3366 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3368 else
3369 sprintf (type, "sub_%c", method_type);
3371 fndecl = build_lang_decl (FUNCTION_DECL,
3372 get_file_function_name (type),
3373 build_function_type_list (void_type_node,
3374 NULL_TREE));
3375 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3377 TREE_PUBLIC (current_function_decl) = 0;
3379 /* Mark as artificial because it's not explicitly in the user's
3380 source code. */
3381 DECL_ARTIFICIAL (current_function_decl) = 1;
3383 /* Mark this declaration as used to avoid spurious warnings. */
3384 TREE_USED (current_function_decl) = 1;
3386 /* Mark this function as a global constructor or destructor. */
3387 if (method_type == 'I')
3388 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3389 else
3390 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3392 body = begin_compound_stmt (BCS_FN_BODY);
3394 return body;
3397 /* Finish the process of running a particular set of global constructors
3398 or destructors. Subroutine of do_[cd]tors. */
3400 static void
3401 finish_objects (int method_type, int initp, tree body)
3403 tree fn;
3405 /* Finish up. */
3406 finish_compound_stmt (body);
3407 fn = finish_function (0);
3409 if (method_type == 'I')
3411 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3412 decl_init_priority_insert (fn, initp);
3414 else
3416 DECL_STATIC_DESTRUCTOR (fn) = 1;
3417 decl_fini_priority_insert (fn, initp);
3420 expand_or_defer_fn (fn);
3423 /* The names of the parameters to the function created to handle
3424 initializations and destructions for objects with static storage
3425 duration. */
3426 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3427 #define PRIORITY_IDENTIFIER "__priority"
3429 /* The name of the function we create to handle initializations and
3430 destructions for objects with static storage duration. */
3431 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3433 /* The declaration for the __INITIALIZE_P argument. */
3434 static GTY(()) tree initialize_p_decl;
3436 /* The declaration for the __PRIORITY argument. */
3437 static GTY(()) tree priority_decl;
3439 /* The declaration for the static storage duration function. */
3440 static GTY(()) tree ssdf_decl;
3442 /* All the static storage duration functions created in this
3443 translation unit. */
3444 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3446 /* A map from priority levels to information about that priority
3447 level. There may be many such levels, so efficient lookup is
3448 important. */
3449 static splay_tree priority_info_map;
3451 /* Begins the generation of the function that will handle all
3452 initialization and destruction of objects with static storage
3453 duration. The function generated takes two parameters of type
3454 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3455 nonzero, it performs initializations. Otherwise, it performs
3456 destructions. It only performs those initializations or
3457 destructions with the indicated __PRIORITY. The generated function
3458 returns no value.
3460 It is assumed that this function will only be called once per
3461 translation unit. */
3463 static tree
3464 start_static_storage_duration_function (unsigned count)
3466 tree type;
3467 tree body;
3468 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3470 /* Create the identifier for this function. It will be of the form
3471 SSDF_IDENTIFIER_<number>. */
3472 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3474 type = build_function_type_list (void_type_node,
3475 integer_type_node, integer_type_node,
3476 NULL_TREE);
3478 /* Create the FUNCTION_DECL itself. */
3479 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3480 get_identifier (id),
3481 type);
3482 TREE_PUBLIC (ssdf_decl) = 0;
3483 DECL_ARTIFICIAL (ssdf_decl) = 1;
3485 /* Put this function in the list of functions to be called from the
3486 static constructors and destructors. */
3487 if (!ssdf_decls)
3489 vec_alloc (ssdf_decls, 32);
3491 /* Take this opportunity to initialize the map from priority
3492 numbers to information about that priority level. */
3493 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3494 /*delete_key_fn=*/0,
3495 /*delete_value_fn=*/
3496 (splay_tree_delete_value_fn) &free);
3498 /* We always need to generate functions for the
3499 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3500 priorities later, we'll be sure to find the
3501 DEFAULT_INIT_PRIORITY. */
3502 get_priority_info (DEFAULT_INIT_PRIORITY);
3505 vec_safe_push (ssdf_decls, ssdf_decl);
3507 /* Create the argument list. */
3508 initialize_p_decl = cp_build_parm_decl
3509 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3510 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3511 TREE_USED (initialize_p_decl) = 1;
3512 priority_decl = cp_build_parm_decl
3513 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3514 DECL_CONTEXT (priority_decl) = ssdf_decl;
3515 TREE_USED (priority_decl) = 1;
3517 DECL_CHAIN (initialize_p_decl) = priority_decl;
3518 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3520 /* Put the function in the global scope. */
3521 pushdecl (ssdf_decl);
3523 /* Start the function itself. This is equivalent to declaring the
3524 function as:
3526 static void __ssdf (int __initialize_p, init __priority_p);
3528 It is static because we only need to call this function from the
3529 various constructor and destructor functions for this module. */
3530 start_preparsed_function (ssdf_decl,
3531 /*attrs=*/NULL_TREE,
3532 SF_PRE_PARSED);
3534 /* Set up the scope of the outermost block in the function. */
3535 body = begin_compound_stmt (BCS_FN_BODY);
3537 return body;
3540 /* Finish the generation of the function which performs initialization
3541 and destruction of objects with static storage duration. After
3542 this point, no more such objects can be created. */
3544 static void
3545 finish_static_storage_duration_function (tree body)
3547 /* Close out the function. */
3548 finish_compound_stmt (body);
3549 expand_or_defer_fn (finish_function (0));
3552 /* Return the information about the indicated PRIORITY level. If no
3553 code to handle this level has yet been generated, generate the
3554 appropriate prologue. */
3556 static priority_info
3557 get_priority_info (int priority)
3559 priority_info pi;
3560 splay_tree_node n;
3562 n = splay_tree_lookup (priority_info_map,
3563 (splay_tree_key) priority);
3564 if (!n)
3566 /* Create a new priority information structure, and insert it
3567 into the map. */
3568 pi = XNEW (struct priority_info_s);
3569 pi->initializations_p = 0;
3570 pi->destructions_p = 0;
3571 splay_tree_insert (priority_info_map,
3572 (splay_tree_key) priority,
3573 (splay_tree_value) pi);
3575 else
3576 pi = (priority_info) n->value;
3578 return pi;
3581 /* The effective initialization priority of a DECL. */
3583 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3584 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3585 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3587 /* Whether a DECL needs a guard to protect it against multiple
3588 initialization. */
3590 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3591 || DECL_ONE_ONLY (decl) \
3592 || DECL_WEAK (decl)))
3594 /* Called from one_static_initialization_or_destruction(),
3595 via walk_tree.
3596 Walks the initializer list of a global variable and looks for
3597 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3598 and that have their DECL_CONTEXT() == NULL.
3599 For each such temporary variable, set their DECL_CONTEXT() to
3600 the current function. This is necessary because otherwise
3601 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3602 when trying to refer to a temporary variable that does not have
3603 it's DECL_CONTECT() properly set. */
3604 static tree
3605 fix_temporary_vars_context_r (tree *node,
3606 int * /*unused*/,
3607 void * /*unused1*/)
3609 gcc_assert (current_function_decl);
3611 if (TREE_CODE (*node) == BIND_EXPR)
3613 tree var;
3615 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3616 if (VAR_P (var)
3617 && !DECL_NAME (var)
3618 && DECL_ARTIFICIAL (var)
3619 && !DECL_CONTEXT (var))
3620 DECL_CONTEXT (var) = current_function_decl;
3623 return NULL_TREE;
3626 /* Set up to handle the initialization or destruction of DECL. If
3627 INITP is nonzero, we are initializing the variable. Otherwise, we
3628 are destroying it. */
3630 static void
3631 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3633 tree guard_if_stmt = NULL_TREE;
3634 tree guard;
3636 /* If we are supposed to destruct and there's a trivial destructor,
3637 nothing has to be done. */
3638 if (!initp
3639 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3640 return;
3642 /* Trick the compiler into thinking we are at the file and line
3643 where DECL was declared so that error-messages make sense, and so
3644 that the debugger will show somewhat sensible file and line
3645 information. */
3646 input_location = DECL_SOURCE_LOCATION (decl);
3648 /* Make sure temporary variables in the initialiser all have
3649 their DECL_CONTEXT() set to a value different from NULL_TREE.
3650 This can happen when global variables initialisers are built.
3651 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3652 the temporary variables that might have been generated in the
3653 accompagning initialisers is NULL_TREE, meaning the variables have been
3654 declared in the global namespace.
3655 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3656 of the temporaries are set to the current function decl. */
3657 cp_walk_tree_without_duplicates (&init,
3658 fix_temporary_vars_context_r,
3659 NULL);
3661 /* Because of:
3663 [class.access.spec]
3665 Access control for implicit calls to the constructors,
3666 the conversion functions, or the destructor called to
3667 create and destroy a static data member is performed as
3668 if these calls appeared in the scope of the member's
3669 class.
3671 we pretend we are in a static member function of the class of
3672 which the DECL is a member. */
3673 if (member_p (decl))
3675 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3676 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3679 /* Assume we don't need a guard. */
3680 guard = NULL_TREE;
3681 /* We need a guard if this is an object with external linkage that
3682 might be initialized in more than one place. (For example, a
3683 static data member of a template, when the data member requires
3684 construction.) */
3685 if (NEEDS_GUARD_P (decl))
3687 tree guard_cond;
3689 guard = get_guard (decl);
3691 /* When using __cxa_atexit, we just check the GUARD as we would
3692 for a local static. */
3693 if (flag_use_cxa_atexit)
3695 /* When using __cxa_atexit, we never try to destroy
3696 anything from a static destructor. */
3697 gcc_assert (initp);
3698 guard_cond = get_guard_cond (guard, false);
3700 /* If we don't have __cxa_atexit, then we will be running
3701 destructors from .fini sections, or their equivalents. So,
3702 we need to know how many times we've tried to initialize this
3703 object. We do initializations only if the GUARD is zero,
3704 i.e., if we are the first to initialize the variable. We do
3705 destructions only if the GUARD is one, i.e., if we are the
3706 last to destroy the variable. */
3707 else if (initp)
3708 guard_cond
3709 = cp_build_binary_op (input_location,
3710 EQ_EXPR,
3711 cp_build_unary_op (PREINCREMENT_EXPR,
3712 guard,
3713 /*noconvert=*/1,
3714 tf_warning_or_error),
3715 integer_one_node,
3716 tf_warning_or_error);
3717 else
3718 guard_cond
3719 = cp_build_binary_op (input_location,
3720 EQ_EXPR,
3721 cp_build_unary_op (PREDECREMENT_EXPR,
3722 guard,
3723 /*noconvert=*/1,
3724 tf_warning_or_error),
3725 integer_zero_node,
3726 tf_warning_or_error);
3728 guard_if_stmt = begin_if_stmt ();
3729 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3733 /* If we're using __cxa_atexit, we have not already set the GUARD,
3734 so we must do so now. */
3735 if (guard && initp && flag_use_cxa_atexit)
3736 finish_expr_stmt (set_guard (guard));
3738 /* Perform the initialization or destruction. */
3739 if (initp)
3741 if (init)
3743 finish_expr_stmt (init);
3744 if (flag_sanitize & SANITIZE_ADDRESS)
3746 varpool_node *vnode = varpool_node::get (decl);
3747 if (vnode)
3748 vnode->dynamically_initialized = 1;
3752 /* If we're using __cxa_atexit, register a function that calls the
3753 destructor for the object. */
3754 if (flag_use_cxa_atexit)
3755 finish_expr_stmt (register_dtor_fn (decl));
3757 else
3758 finish_expr_stmt (build_cleanup (decl));
3760 /* Finish the guard if-stmt, if necessary. */
3761 if (guard)
3763 finish_then_clause (guard_if_stmt);
3764 finish_if_stmt (guard_if_stmt);
3767 /* Now that we're done with DECL we don't need to pretend to be a
3768 member of its class any longer. */
3769 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3770 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3773 /* Generate code to do the initialization or destruction of the decls in VARS,
3774 a TREE_LIST of VAR_DECL with static storage duration.
3775 Whether initialization or destruction is performed is specified by INITP. */
3777 static void
3778 do_static_initialization_or_destruction (tree vars, bool initp)
3780 tree node, init_if_stmt, cond;
3782 /* Build the outer if-stmt to check for initialization or destruction. */
3783 init_if_stmt = begin_if_stmt ();
3784 cond = initp ? integer_one_node : integer_zero_node;
3785 cond = cp_build_binary_op (input_location,
3786 EQ_EXPR,
3787 initialize_p_decl,
3788 cond,
3789 tf_warning_or_error);
3790 finish_if_stmt_cond (cond, init_if_stmt);
3792 /* To make sure dynamic construction doesn't access globals from other
3793 compilation units where they might not be yet constructed, for
3794 -fsanitize=address insert __asan_before_dynamic_init call that
3795 prevents access to either all global variables that need construction
3796 in other compilation units, or at least those that haven't been
3797 initialized yet. Variables that need dynamic construction in
3798 the current compilation unit are kept accessible. */
3799 if (flag_sanitize & SANITIZE_ADDRESS)
3800 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3802 node = vars;
3803 do {
3804 tree decl = TREE_VALUE (node);
3805 tree priority_if_stmt;
3806 int priority;
3807 priority_info pi;
3809 /* If we don't need a destructor, there's nothing to do. Avoid
3810 creating a possibly empty if-stmt. */
3811 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3813 node = TREE_CHAIN (node);
3814 continue;
3817 /* Remember that we had an initialization or finalization at this
3818 priority. */
3819 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3820 pi = get_priority_info (priority);
3821 if (initp)
3822 pi->initializations_p = 1;
3823 else
3824 pi->destructions_p = 1;
3826 /* Conditionalize this initialization on being in the right priority
3827 and being initializing/finalizing appropriately. */
3828 priority_if_stmt = begin_if_stmt ();
3829 cond = cp_build_binary_op (input_location,
3830 EQ_EXPR,
3831 priority_decl,
3832 build_int_cst (NULL_TREE, priority),
3833 tf_warning_or_error);
3834 finish_if_stmt_cond (cond, priority_if_stmt);
3836 /* Process initializers with same priority. */
3837 for (; node
3838 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3839 node = TREE_CHAIN (node))
3840 /* Do one initialization or destruction. */
3841 one_static_initialization_or_destruction (TREE_VALUE (node),
3842 TREE_PURPOSE (node), initp);
3844 /* Finish up the priority if-stmt body. */
3845 finish_then_clause (priority_if_stmt);
3846 finish_if_stmt (priority_if_stmt);
3848 } while (node);
3850 /* Revert what __asan_before_dynamic_init did by calling
3851 __asan_after_dynamic_init. */
3852 if (flag_sanitize & SANITIZE_ADDRESS)
3853 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3855 /* Finish up the init/destruct if-stmt body. */
3856 finish_then_clause (init_if_stmt);
3857 finish_if_stmt (init_if_stmt);
3860 /* VARS is a list of variables with static storage duration which may
3861 need initialization and/or finalization. Remove those variables
3862 that don't really need to be initialized or finalized, and return
3863 the resulting list. The order in which the variables appear in
3864 VARS is in reverse order of the order in which they should actually
3865 be initialized. The list we return is in the unreversed order;
3866 i.e., the first variable should be initialized first. */
3868 static tree
3869 prune_vars_needing_no_initialization (tree *vars)
3871 tree *var = vars;
3872 tree result = NULL_TREE;
3874 while (*var)
3876 tree t = *var;
3877 tree decl = TREE_VALUE (t);
3878 tree init = TREE_PURPOSE (t);
3880 /* Deal gracefully with error. */
3881 if (decl == error_mark_node)
3883 var = &TREE_CHAIN (t);
3884 continue;
3887 /* The only things that can be initialized are variables. */
3888 gcc_assert (VAR_P (decl));
3890 /* If this object is not defined, we don't need to do anything
3891 here. */
3892 if (DECL_EXTERNAL (decl))
3894 var = &TREE_CHAIN (t);
3895 continue;
3898 /* Also, if the initializer already contains errors, we can bail
3899 out now. */
3900 if (init && TREE_CODE (init) == TREE_LIST
3901 && value_member (error_mark_node, init))
3903 var = &TREE_CHAIN (t);
3904 continue;
3907 /* This variable is going to need initialization and/or
3908 finalization, so we add it to the list. */
3909 *var = TREE_CHAIN (t);
3910 TREE_CHAIN (t) = result;
3911 result = t;
3914 return result;
3917 /* Make sure we have told the back end about all the variables in
3918 VARS. */
3920 static void
3921 write_out_vars (tree vars)
3923 tree v;
3925 for (v = vars; v; v = TREE_CHAIN (v))
3927 tree var = TREE_VALUE (v);
3928 if (!var_finalized_p (var))
3930 import_export_decl (var);
3931 rest_of_decl_compilation (var, 1, 1);
3936 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3937 (otherwise) that will initialize all global objects with static
3938 storage duration having the indicated PRIORITY. */
3940 static void
3941 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3942 location_t *locus)
3944 char function_key;
3945 tree fndecl;
3946 tree body;
3947 size_t i;
3949 input_location = *locus;
3950 /* ??? */
3951 /* Was: locus->line++; */
3953 /* We use `I' to indicate initialization and `D' to indicate
3954 destruction. */
3955 function_key = constructor_p ? 'I' : 'D';
3957 /* We emit the function lazily, to avoid generating empty
3958 global constructors and destructors. */
3959 body = NULL_TREE;
3961 /* For Objective-C++, we may need to initialize metadata found in this module.
3962 This must be done _before_ any other static initializations. */
3963 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3964 && constructor_p && objc_static_init_needed_p ())
3966 body = start_objects (function_key, priority);
3967 objc_generate_static_init_call (NULL_TREE);
3970 /* Call the static storage duration function with appropriate
3971 arguments. */
3972 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3974 /* Calls to pure or const functions will expand to nothing. */
3975 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3977 tree call;
3979 if (! body)
3980 body = start_objects (function_key, priority);
3982 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3983 build_int_cst (NULL_TREE,
3984 constructor_p),
3985 build_int_cst (NULL_TREE,
3986 priority),
3987 NULL_TREE);
3988 finish_expr_stmt (call);
3992 /* Close out the function. */
3993 if (body)
3994 finish_objects (function_key, priority, body);
3997 /* Generate constructor and destructor functions for the priority
3998 indicated by N. */
4000 static int
4001 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4003 location_t *locus = (location_t *) data;
4004 int priority = (int) n->key;
4005 priority_info pi = (priority_info) n->value;
4007 /* Generate the functions themselves, but only if they are really
4008 needed. */
4009 if (pi->initializations_p)
4010 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4011 if (pi->destructions_p)
4012 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4014 /* Keep iterating. */
4015 return 0;
4018 /* Java requires that we be able to reference a local address for a
4019 method, and not be confused by PLT entries. If supported, create a
4020 hidden alias for all such methods. */
4022 static void
4023 build_java_method_aliases (void)
4025 #ifndef HAVE_GAS_HIDDEN
4026 return;
4027 #endif
4029 struct cgraph_node *node;
4030 FOR_EACH_FUNCTION (node)
4032 tree fndecl = node->decl;
4034 if (DECL_CLASS_SCOPE_P (fndecl)
4035 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4036 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4038 /* Mangle the name in a predictable way; we need to reference
4039 this from a java compiled object file. */
4040 tree oid = DECL_ASSEMBLER_NAME (fndecl);
4041 const char *oname = IDENTIFIER_POINTER (oid);
4042 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4043 char *nname = ACONCAT (("_ZGA", oname + 2, NULL));
4045 tree alias = make_alias_for (fndecl, get_identifier (nname));
4046 TREE_PUBLIC (alias) = 1;
4047 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4049 cgraph_node::create_same_body_alias (alias, fndecl);
4054 /* Return C++ property of T, based on given operation OP. */
4056 static int
4057 cpp_check (tree t, cpp_operation op)
4059 switch (op)
4061 case HAS_DEPENDENT_TEMPLATE_ARGS:
4063 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4064 if (!ti)
4065 return 0;
4066 ++processing_template_decl;
4067 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4068 --processing_template_decl;
4069 return dep;
4071 case IS_ABSTRACT:
4072 return DECL_PURE_VIRTUAL_P (t);
4073 case IS_CONSTRUCTOR:
4074 return DECL_CONSTRUCTOR_P (t);
4075 case IS_DESTRUCTOR:
4076 return DECL_DESTRUCTOR_P (t);
4077 case IS_COPY_CONSTRUCTOR:
4078 return DECL_COPY_CONSTRUCTOR_P (t);
4079 case IS_MOVE_CONSTRUCTOR:
4080 return DECL_MOVE_CONSTRUCTOR_P (t);
4081 case IS_TEMPLATE:
4082 return TREE_CODE (t) == TEMPLATE_DECL;
4083 case IS_TRIVIAL:
4084 return trivial_type_p (t);
4085 default:
4086 return 0;
4090 /* Collect source file references recursively, starting from NAMESPC. */
4092 static void
4093 collect_source_refs (tree namespc)
4095 tree t;
4097 if (!namespc)
4098 return;
4100 /* Iterate over names in this name space. */
4101 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4102 if (!DECL_IS_BUILTIN (t) )
4103 collect_source_ref (DECL_SOURCE_FILE (t));
4105 /* Dump siblings, if any */
4106 collect_source_refs (TREE_CHAIN (namespc));
4108 /* Dump children, if any */
4109 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4112 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4113 starting from NAMESPC. */
4115 static void
4116 collect_ada_namespace (tree namespc, const char *source_file)
4118 if (!namespc)
4119 return;
4121 /* Collect decls from this namespace */
4122 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4124 /* Collect siblings, if any */
4125 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4127 /* Collect children, if any */
4128 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4131 /* Returns true iff there is a definition available for variable or
4132 function DECL. */
4134 static bool
4135 decl_defined_p (tree decl)
4137 if (TREE_CODE (decl) == FUNCTION_DECL)
4138 return (DECL_INITIAL (decl) != NULL_TREE);
4139 else
4141 gcc_assert (VAR_P (decl));
4142 return !DECL_EXTERNAL (decl);
4146 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4148 [expr.const]
4150 An integral constant-expression can only involve ... const
4151 variables of integral or enumeration types initialized with
4152 constant expressions ...
4154 C++0x also allows constexpr variables and temporaries initialized
4155 with constant expressions. We handle the former here, but the latter
4156 are just folded away in cxx_eval_constant_expression.
4158 The standard does not require that the expression be non-volatile.
4159 G++ implements the proposed correction in DR 457. */
4161 bool
4162 decl_constant_var_p (tree decl)
4164 if (!decl_maybe_constant_var_p (decl))
4165 return false;
4167 /* We don't know if a template static data member is initialized with
4168 a constant expression until we instantiate its initializer. Even
4169 in the case of a constexpr variable, we can't treat it as a
4170 constant until its initializer is complete in case it's used in
4171 its own initializer. */
4172 mark_used (decl);
4173 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4176 /* Returns true if DECL could be a symbolic constant variable, depending on
4177 its initializer. */
4179 bool
4180 decl_maybe_constant_var_p (tree decl)
4182 tree type = TREE_TYPE (decl);
4183 if (!VAR_P (decl))
4184 return false;
4185 if (DECL_DECLARED_CONSTEXPR_P (decl))
4186 return true;
4187 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4188 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4191 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4192 called from grokfndecl and grokvardecl; in all modes it is called from
4193 cp_write_global_declarations. */
4195 void
4196 no_linkage_error (tree decl)
4198 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4199 /* In C++11 it's ok if the decl is defined. */
4200 return;
4201 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4202 if (t == NULL_TREE)
4203 /* The type that got us on no_linkage_decls must have gotten a name for
4204 linkage purposes. */;
4205 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4206 /* The type might end up having a typedef name for linkage purposes. */
4207 vec_safe_push (no_linkage_decls, decl);
4208 else if (TYPE_ANONYMOUS_P (t))
4210 bool d = false;
4211 if (cxx_dialect >= cxx11)
4212 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4213 "anonymous type, is used but never defined", decl);
4214 else if (DECL_EXTERN_C_P (decl))
4215 /* Allow this; it's pretty common in C. */;
4216 else if (VAR_P (decl))
4217 /* DRs 132, 319 and 389 seem to indicate types with
4218 no linkage can only be used to declare extern "C"
4219 entities. Since it's not always an error in the
4220 ISO C++ 90 Standard, we only issue a warning. */
4221 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4222 "with no linkage used to declare variable %q#D with "
4223 "linkage", decl);
4224 else
4225 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4226 "linkage used to declare function %q#D with linkage",
4227 decl);
4228 if (d && is_typedef_decl (TYPE_NAME (t)))
4229 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4230 "to the unqualified type, so it is not used for linkage",
4231 TYPE_NAME (t));
4233 else if (cxx_dialect >= cxx11)
4235 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4236 permerror (DECL_SOURCE_LOCATION (decl),
4237 "%q#D, declared using local type "
4238 "%qT, is used but never defined", decl, t);
4240 else if (VAR_P (decl))
4241 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4242 "used to declare variable %q#D with linkage", t, decl);
4243 else
4244 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4245 "to declare function %q#D with linkage", t, decl);
4248 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4250 static void
4251 collect_all_refs (const char *source_file)
4253 collect_ada_namespace (global_namespace, source_file);
4256 /* Clear DECL_EXTERNAL for NODE. */
4258 static bool
4259 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4261 DECL_EXTERNAL (node->decl) = 0;
4262 return false;
4265 /* Build up the function to run dynamic initializers for thread_local
4266 variables in this translation unit and alias the init functions for the
4267 individual variables to it. */
4269 static void
4270 handle_tls_init (void)
4272 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4273 if (vars == NULL_TREE)
4274 return;
4276 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4278 write_out_vars (vars);
4280 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4281 boolean_type_node);
4282 TREE_PUBLIC (guard) = false;
4283 TREE_STATIC (guard) = true;
4284 DECL_ARTIFICIAL (guard) = true;
4285 DECL_IGNORED_P (guard) = true;
4286 TREE_USED (guard) = true;
4287 CP_DECL_THREAD_LOCAL_P (guard) = true;
4288 set_decl_tls_model (guard, decl_default_tls_model (guard));
4289 pushdecl_top_level_and_finish (guard, NULL_TREE);
4291 tree fn = get_local_tls_init_fn ();
4292 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4293 tree body = begin_function_body ();
4294 tree if_stmt = begin_if_stmt ();
4295 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4296 tf_warning_or_error);
4297 finish_if_stmt_cond (cond, if_stmt);
4298 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4299 tf_warning_or_error));
4300 for (; vars; vars = TREE_CHAIN (vars))
4302 tree var = TREE_VALUE (vars);
4303 tree init = TREE_PURPOSE (vars);
4304 one_static_initialization_or_destruction (var, init, true);
4306 #ifdef ASM_OUTPUT_DEF
4307 /* Output init aliases even with -fno-extern-tls-init. */
4308 if (TREE_PUBLIC (var))
4310 tree single_init_fn = get_tls_init_fn (var);
4311 if (single_init_fn == NULL_TREE)
4312 continue;
4313 cgraph_node *alias
4314 = cgraph_node::get_create (fn)->create_same_body_alias
4315 (single_init_fn, fn);
4316 gcc_assert (alias != NULL);
4318 #endif
4321 finish_then_clause (if_stmt);
4322 finish_if_stmt (if_stmt);
4323 finish_function_body (body);
4324 expand_or_defer_fn (finish_function (0));
4327 /* We're at the end of compilation, so generate any mangling aliases that
4328 we've been saving up, if DECL is going to be output and ID2 isn't
4329 already taken by another declaration. */
4331 static void
4332 generate_mangling_alias (tree decl, tree id2)
4334 /* If there's a declaration already using this mangled name,
4335 don't create a compatibility alias that conflicts. */
4336 if (IDENTIFIER_GLOBAL_VALUE (id2))
4337 return;
4339 struct cgraph_node *n = NULL;
4340 if (TREE_CODE (decl) == FUNCTION_DECL
4341 && !(n = cgraph_node::get (decl)))
4342 /* Don't create an alias to an unreferenced function. */
4343 return;
4345 tree alias = make_alias_for (decl, id2);
4346 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4347 DECL_IGNORED_P (alias) = 1;
4348 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4349 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4350 if (vague_linkage_p (decl))
4351 DECL_WEAK (alias) = 1;
4352 if (TREE_CODE (decl) == FUNCTION_DECL)
4353 n->create_same_body_alias (alias, decl);
4354 else
4355 varpool_node::create_extra_name_alias (alias, decl);
4358 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4359 the end of translation, for compatibility across bugs in the mangling
4360 implementation. */
4362 void
4363 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4365 #ifdef ASM_OUTPUT_DEF
4366 if (at_eof)
4367 generate_mangling_alias (decl, id2);
4368 else
4370 vec_safe_push (mangling_aliases, decl);
4371 vec_safe_push (mangling_aliases, id2);
4373 #endif
4376 static void
4377 generate_mangling_aliases ()
4379 while (!vec_safe_is_empty (mangling_aliases))
4381 tree id2 = mangling_aliases->pop();
4382 tree decl = mangling_aliases->pop();
4383 generate_mangling_alias (decl, id2);
4387 /* The entire file is now complete. If requested, dump everything
4388 to a file. */
4390 static void
4391 dump_tu (void)
4393 int flags;
4394 FILE *stream = dump_begin (TDI_tu, &flags);
4396 if (stream)
4398 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4399 dump_end (TDI_tu, stream);
4403 static location_t locus_at_end_of_parsing;
4405 /* Check the deallocation functions for CODE to see if we want to warn that
4406 only one was defined. */
4408 static void
4409 maybe_warn_sized_delete (enum tree_code code)
4411 tree sized = NULL_TREE;
4412 tree unsized = NULL_TREE;
4414 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4415 ovl; ovl = OVL_NEXT (ovl))
4417 tree fn = OVL_CURRENT (ovl);
4418 /* We're only interested in usual deallocation functions. */
4419 if (!non_placement_deallocation_fn_p (fn))
4420 continue;
4421 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4422 unsized = fn;
4423 else
4424 sized = fn;
4426 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4427 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4428 "the program should also define %qD", sized);
4429 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4430 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4431 "the program should also define %qD", unsized);
4434 /* Check the global deallocation functions to see if we want to warn about
4435 defining unsized without sized (or vice versa). */
4437 static void
4438 maybe_warn_sized_delete ()
4440 if (!flag_sized_deallocation || !warn_sized_deallocation)
4441 return;
4442 maybe_warn_sized_delete (DELETE_EXPR);
4443 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4446 /* This routine is called at the end of compilation.
4447 Its job is to create all the code needed to initialize and
4448 destroy the global aggregates. We do the destruction
4449 first, since that way we only need to reverse the decls once. */
4451 void
4452 c_parse_final_cleanups (void)
4454 tree vars;
4455 bool reconsider;
4456 size_t i;
4457 unsigned ssdf_count = 0;
4458 int retries = 0;
4459 tree decl;
4461 locus_at_end_of_parsing = input_location;
4462 at_eof = 1;
4464 /* Bad parse errors. Just forget about it. */
4465 if (! global_bindings_p () || current_class_type
4466 || !vec_safe_is_empty (decl_namespace_list))
4467 return;
4469 /* This is the point to write out a PCH if we're doing that.
4470 In that case we do not want to do anything else. */
4471 if (pch_file)
4473 c_common_write_pch ();
4474 dump_tu ();
4475 return;
4478 timevar_stop (TV_PHASE_PARSING);
4479 timevar_start (TV_PHASE_DEFERRED);
4481 symtab->process_same_body_aliases ();
4483 /* Handle -fdump-ada-spec[-slim] */
4484 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4486 if (flag_dump_ada_spec_slim)
4487 collect_source_ref (main_input_filename);
4488 else
4489 collect_source_refs (global_namespace);
4491 dump_ada_specs (collect_all_refs, cpp_check);
4494 /* FIXME - huh? was input_line -= 1;*/
4496 /* We now have to write out all the stuff we put off writing out.
4497 These include:
4499 o Template specializations that we have not yet instantiated,
4500 but which are needed.
4501 o Initialization and destruction for non-local objects with
4502 static storage duration. (Local objects with static storage
4503 duration are initialized when their scope is first entered,
4504 and are cleaned up via atexit.)
4505 o Virtual function tables.
4507 All of these may cause others to be needed. For example,
4508 instantiating one function may cause another to be needed, and
4509 generating the initializer for an object may cause templates to be
4510 instantiated, etc., etc. */
4512 emit_support_tinfos ();
4516 tree t;
4517 tree decl;
4519 reconsider = false;
4521 /* If there are templates that we've put off instantiating, do
4522 them now. */
4523 instantiate_pending_templates (retries);
4524 ggc_collect ();
4526 /* Write out virtual tables as required. Note that writing out
4527 the virtual table for a template class may cause the
4528 instantiation of members of that class. If we write out
4529 vtables then we remove the class from our list so we don't
4530 have to look at it again. */
4532 while (keyed_classes != NULL_TREE
4533 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4535 reconsider = true;
4536 keyed_classes = TREE_CHAIN (keyed_classes);
4539 t = keyed_classes;
4540 if (t != NULL_TREE)
4542 tree next = TREE_CHAIN (t);
4544 while (next)
4546 if (maybe_emit_vtables (TREE_VALUE (next)))
4548 reconsider = true;
4549 TREE_CHAIN (t) = TREE_CHAIN (next);
4551 else
4552 t = next;
4554 next = TREE_CHAIN (t);
4558 /* Write out needed type info variables. We have to be careful
4559 looping through unemitted decls, because emit_tinfo_decl may
4560 cause other variables to be needed. New elements will be
4561 appended, and we remove from the vector those that actually
4562 get emitted. */
4563 for (i = unemitted_tinfo_decls->length ();
4564 unemitted_tinfo_decls->iterate (--i, &t);)
4565 if (emit_tinfo_decl (t))
4567 reconsider = true;
4568 unemitted_tinfo_decls->unordered_remove (i);
4571 /* The list of objects with static storage duration is built up
4572 in reverse order. We clear STATIC_AGGREGATES so that any new
4573 aggregates added during the initialization of these will be
4574 initialized in the correct order when we next come around the
4575 loop. */
4576 vars = prune_vars_needing_no_initialization (&static_aggregates);
4578 if (vars)
4580 /* We need to start a new initialization function each time
4581 through the loop. That's because we need to know which
4582 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4583 isn't computed until a function is finished, and written
4584 out. That's a deficiency in the back end. When this is
4585 fixed, these initialization functions could all become
4586 inline, with resulting performance improvements. */
4587 tree ssdf_body;
4589 /* Set the line and file, so that it is obviously not from
4590 the source file. */
4591 input_location = locus_at_end_of_parsing;
4592 ssdf_body = start_static_storage_duration_function (ssdf_count);
4594 /* Make sure the back end knows about all the variables. */
4595 write_out_vars (vars);
4597 /* First generate code to do all the initializations. */
4598 if (vars)
4599 do_static_initialization_or_destruction (vars, /*initp=*/true);
4601 /* Then, generate code to do all the destructions. Do these
4602 in reverse order so that the most recently constructed
4603 variable is the first destroyed. If we're using
4604 __cxa_atexit, then we don't need to do this; functions
4605 were registered at initialization time to destroy the
4606 local statics. */
4607 if (!flag_use_cxa_atexit && vars)
4609 vars = nreverse (vars);
4610 do_static_initialization_or_destruction (vars, /*initp=*/false);
4612 else
4613 vars = NULL_TREE;
4615 /* Finish up the static storage duration function for this
4616 round. */
4617 input_location = locus_at_end_of_parsing;
4618 finish_static_storage_duration_function (ssdf_body);
4620 /* All those initializations and finalizations might cause
4621 us to need more inline functions, more template
4622 instantiations, etc. */
4623 reconsider = true;
4624 ssdf_count++;
4625 /* ??? was: locus_at_end_of_parsing.line++; */
4628 /* Now do the same for thread_local variables. */
4629 handle_tls_init ();
4631 /* Go through the set of inline functions whose bodies have not
4632 been emitted yet. If out-of-line copies of these functions
4633 are required, emit them. */
4634 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4636 /* Does it need synthesizing? */
4637 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4638 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4640 /* Even though we're already at the top-level, we push
4641 there again. That way, when we pop back a few lines
4642 hence, all of our state is restored. Otherwise,
4643 finish_function doesn't clean things up, and we end
4644 up with CURRENT_FUNCTION_DECL set. */
4645 push_to_top_level ();
4646 /* The decl's location will mark where it was first
4647 needed. Save that so synthesize method can indicate
4648 where it was needed from, in case of error */
4649 input_location = DECL_SOURCE_LOCATION (decl);
4650 synthesize_method (decl);
4651 pop_from_top_level ();
4652 reconsider = true;
4655 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4656 generate_tls_wrapper (decl);
4658 if (!DECL_SAVED_TREE (decl))
4659 continue;
4661 /* We lie to the back end, pretending that some functions
4662 are not defined when they really are. This keeps these
4663 functions from being put out unnecessarily. But, we must
4664 stop lying when the functions are referenced, or if they
4665 are not comdat since they need to be put out now. If
4666 DECL_INTERFACE_KNOWN, then we have already set
4667 DECL_EXTERNAL appropriately, so there's no need to check
4668 again, and we do not want to clear DECL_EXTERNAL if a
4669 previous call to import_export_decl set it.
4671 This is done in a separate for cycle, because if some
4672 deferred function is contained in another deferred
4673 function later in deferred_fns varray,
4674 rest_of_compilation would skip this function and we
4675 really cannot expand the same function twice. */
4676 import_export_decl (decl);
4677 if (DECL_NOT_REALLY_EXTERN (decl)
4678 && DECL_INITIAL (decl)
4679 && decl_needed_p (decl))
4681 struct cgraph_node *node, *next;
4683 node = cgraph_node::get (decl);
4684 if (node->cpp_implicit_alias)
4685 node = node->get_alias_target ();
4687 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4688 NULL, true);
4689 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4690 group, we need to mark all symbols in the same comdat group
4691 that way. */
4692 if (node->same_comdat_group)
4693 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4694 next != node;
4695 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4696 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4697 NULL, true);
4700 /* If we're going to need to write this function out, and
4701 there's already a body for it, create RTL for it now.
4702 (There might be no body if this is a method we haven't
4703 gotten around to synthesizing yet.) */
4704 if (!DECL_EXTERNAL (decl)
4705 && decl_needed_p (decl)
4706 && !TREE_ASM_WRITTEN (decl)
4707 && !cgraph_node::get (decl)->definition)
4709 /* We will output the function; no longer consider it in this
4710 loop. */
4711 DECL_DEFER_OUTPUT (decl) = 0;
4712 /* Generate RTL for this function now that we know we
4713 need it. */
4714 expand_or_defer_fn (decl);
4715 /* If we're compiling -fsyntax-only pretend that this
4716 function has been written out so that we don't try to
4717 expand it again. */
4718 if (flag_syntax_only)
4719 TREE_ASM_WRITTEN (decl) = 1;
4720 reconsider = true;
4724 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4725 reconsider = true;
4727 /* Static data members are just like namespace-scope globals. */
4728 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4730 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4731 /* Don't write it out if we haven't seen a definition. */
4732 || DECL_IN_AGGR_P (decl))
4733 continue;
4734 import_export_decl (decl);
4735 /* If this static data member is needed, provide it to the
4736 back end. */
4737 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4738 DECL_EXTERNAL (decl) = 0;
4740 if (vec_safe_length (pending_statics) != 0
4741 && wrapup_global_declarations (pending_statics->address (),
4742 pending_statics->length ()))
4743 reconsider = true;
4745 retries++;
4747 while (reconsider);
4749 generate_mangling_aliases ();
4751 /* All used inline functions must have a definition at this point. */
4752 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4754 if (/* Check online inline functions that were actually used. */
4755 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4756 /* If the definition actually was available here, then the
4757 fact that the function was not defined merely represents
4758 that for some reason (use of a template repository,
4759 #pragma interface, etc.) we decided not to emit the
4760 definition here. */
4761 && !DECL_INITIAL (decl)
4762 /* Don't complain if the template was defined. */
4763 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4764 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4765 (template_for_substitution (decl)))))
4767 warning (0, "inline function %q+D used but never defined", decl);
4768 /* Avoid a duplicate warning from check_global_declaration. */
4769 TREE_NO_WARNING (decl) = 1;
4773 /* So must decls that use a type with no linkage. */
4774 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4775 no_linkage_error (decl);
4777 maybe_warn_sized_delete ();
4779 /* Then, do the Objective-C stuff. This is where all the
4780 Objective-C module stuff gets generated (symtab,
4781 class/protocol/selector lists etc). This must be done after C++
4782 templates, destructors etc. so that selectors used in C++
4783 templates are properly allocated. */
4784 if (c_dialect_objc ())
4785 objc_write_global_declarations ();
4787 /* We give C linkage to static constructors and destructors. */
4788 push_lang_context (lang_name_c);
4790 /* Generate initialization and destruction functions for all
4791 priorities for which they are required. */
4792 if (priority_info_map)
4793 splay_tree_foreach (priority_info_map,
4794 generate_ctor_and_dtor_functions_for_priority,
4795 /*data=*/&locus_at_end_of_parsing);
4796 else if (c_dialect_objc () && objc_static_init_needed_p ())
4797 /* If this is obj-c++ and we need a static init, call
4798 generate_ctor_or_dtor_function. */
4799 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4800 DEFAULT_INIT_PRIORITY,
4801 &locus_at_end_of_parsing);
4803 /* We're done with the splay-tree now. */
4804 if (priority_info_map)
4805 splay_tree_delete (priority_info_map);
4807 /* Generate any missing aliases. */
4808 maybe_apply_pending_pragma_weaks ();
4810 /* We're done with static constructors, so we can go back to "C++"
4811 linkage now. */
4812 pop_lang_context ();
4814 /* Generate Java hidden aliases. */
4815 build_java_method_aliases ();
4817 if (flag_vtable_verify)
4819 vtv_recover_class_info ();
4820 vtv_compute_class_hierarchy_transitive_closure ();
4821 vtv_build_vtable_verify_fndecl ();
4824 perform_deferred_noexcept_checks ();
4826 finish_repo ();
4828 /* The entire file is now complete. If requested, dump everything
4829 to a file. */
4830 dump_tu ();
4832 if (flag_detailed_statistics)
4834 dump_tree_statistics ();
4835 dump_time_statistics ();
4838 timevar_stop (TV_PHASE_DEFERRED);
4839 timevar_start (TV_PHASE_PARSING);
4842 /* Perform any post compilation-proper cleanups for the C++ front-end.
4843 This should really go away. No front-end should need to do
4844 anything past the compilation process. */
4846 void
4847 cxx_post_compilation_parsing_cleanups (void)
4849 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4851 if (flag_vtable_verify)
4853 /* Generate the special constructor initialization function that
4854 calls __VLTRegisterPairs, and give it a very high
4855 initialization priority. This must be done after
4856 finalize_compilation_unit so that we have accurate
4857 information about which vtable will actually be emitted. */
4858 vtv_generate_init_routine ();
4861 input_location = locus_at_end_of_parsing;
4863 #ifdef ENABLE_CHECKING
4864 validate_conversion_obstack ();
4865 #endif /* ENABLE_CHECKING */
4867 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4870 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4871 function to call in parse-tree form; it has not yet been
4872 semantically analyzed. ARGS are the arguments to the function.
4873 They have already been semantically analyzed. This may change
4874 ARGS. */
4876 tree
4877 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4878 tsubst_flags_t complain)
4880 tree orig_fn;
4881 vec<tree, va_gc> *orig_args = NULL;
4882 tree expr;
4883 tree object;
4885 orig_fn = fn;
4886 object = TREE_OPERAND (fn, 0);
4888 if (processing_template_decl)
4890 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4891 || TREE_CODE (fn) == MEMBER_REF);
4892 if (type_dependent_expression_p (fn)
4893 || any_type_dependent_arguments_p (*args))
4894 return build_nt_call_vec (fn, *args);
4896 orig_args = make_tree_vector_copy (*args);
4898 /* Transform the arguments and add the implicit "this"
4899 parameter. That must be done before the FN is transformed
4900 because we depend on the form of FN. */
4901 make_args_non_dependent (*args);
4902 object = build_non_dependent_expr (object);
4903 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4905 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4906 object = cp_build_addr_expr (object, complain);
4907 vec_safe_insert (*args, 0, object);
4909 /* Now that the arguments are done, transform FN. */
4910 fn = build_non_dependent_expr (fn);
4913 /* A qualified name corresponding to a bound pointer-to-member is
4914 represented as an OFFSET_REF:
4916 struct B { void g(); };
4917 void (B::*p)();
4918 void B::g() { (this->*p)(); } */
4919 if (TREE_CODE (fn) == OFFSET_REF)
4921 tree object_addr = cp_build_addr_expr (object, complain);
4922 fn = TREE_OPERAND (fn, 1);
4923 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4924 complain);
4925 vec_safe_insert (*args, 0, object_addr);
4928 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4929 expr = build_op_call (fn, args, complain);
4930 else
4931 expr = cp_build_function_call_vec (fn, args, complain);
4932 if (processing_template_decl && expr != error_mark_node)
4933 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4935 if (orig_args != NULL)
4936 release_tree_vector (orig_args);
4938 return expr;
4942 void
4943 check_default_args (tree x)
4945 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4946 bool saw_def = false;
4947 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4948 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4950 if (TREE_PURPOSE (arg))
4951 saw_def = true;
4952 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4954 error ("default argument missing for parameter %P of %q+#D", i, x);
4955 TREE_PURPOSE (arg) = error_mark_node;
4960 /* Return true if function DECL can be inlined. This is used to force
4961 instantiation of methods that might be interesting for inlining. */
4962 bool
4963 possibly_inlined_p (tree decl)
4965 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4966 if (DECL_UNINLINABLE (decl))
4967 return false;
4968 if (!optimize || pragma_java_exceptions)
4969 return DECL_DECLARED_INLINE_P (decl);
4970 /* When optimizing, we might inline everything when flatten
4971 attribute or heuristics inlining for size or autoinlining
4972 is used. */
4973 return true;
4976 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4977 If DECL is a specialization or implicitly declared class member,
4978 generate the actual definition. Return false if something goes
4979 wrong, true otherwise. */
4981 bool
4982 mark_used (tree decl, tsubst_flags_t complain)
4984 /* If DECL is a BASELINK for a single function, then treat it just
4985 like the DECL for the function. Otherwise, if the BASELINK is
4986 for an overloaded function, we don't know which function was
4987 actually used until after overload resolution. */
4988 if (BASELINK_P (decl))
4990 decl = BASELINK_FUNCTIONS (decl);
4991 if (really_overloaded_fn (decl))
4992 return true;
4993 decl = OVL_CURRENT (decl);
4996 /* Set TREE_USED for the benefit of -Wunused. */
4997 TREE_USED (decl) = 1;
4998 if (DECL_CLONED_FUNCTION_P (decl))
4999 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5001 /* Mark enumeration types as used. */
5002 if (TREE_CODE (decl) == CONST_DECL)
5003 used_types_insert (DECL_CONTEXT (decl));
5005 if (TREE_CODE (decl) == FUNCTION_DECL)
5006 maybe_instantiate_noexcept (decl);
5008 if (TREE_CODE (decl) == FUNCTION_DECL
5009 && DECL_DELETED_FN (decl))
5011 if (DECL_ARTIFICIAL (decl))
5013 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5014 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5016 /* We mark a lambda conversion op as deleted if we can't
5017 generate it properly; see maybe_add_lambda_conv_op. */
5018 sorry ("converting lambda which uses %<...%> to "
5019 "function pointer");
5020 return false;
5023 if (complain & tf_error)
5025 error ("use of deleted function %qD", decl);
5026 if (!maybe_explain_implicit_delete (decl))
5027 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5029 return false;
5032 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5033 && deprecated_state != DEPRECATED_SUPPRESS)
5034 warn_deprecated_use (decl, NULL_TREE);
5036 /* We can only check DECL_ODR_USED on variables or functions with
5037 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5038 might need special handling for. */
5039 if (!VAR_OR_FUNCTION_DECL_P (decl)
5040 || DECL_LANG_SPECIFIC (decl) == NULL
5041 || DECL_THUNK_P (decl))
5043 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
5045 if (complain & tf_error)
5046 error ("use of %qD before deduction of %<auto%>", decl);
5047 return false;
5049 return true;
5052 /* We only want to do this processing once. We don't need to keep trying
5053 to instantiate inline templates, because unit-at-a-time will make sure
5054 we get them compiled before functions that want to inline them. */
5055 if (DECL_ODR_USED (decl))
5056 return true;
5058 /* If within finish_function, defer the rest until that function
5059 finishes, otherwise it might recurse. */
5060 if (defer_mark_used_calls)
5062 vec_safe_push (deferred_mark_used_calls, decl);
5063 return true;
5066 /* Normally, we can wait until instantiation-time to synthesize DECL.
5067 However, if DECL is a static data member initialized with a constant
5068 or a constexpr function, we need it right now because a reference to
5069 such a data member or a call to such function is not value-dependent.
5070 For a function that uses auto in the return type, we need to instantiate
5071 it to find out its type. For OpenMP user defined reductions, we need
5072 them instantiated for reduction clauses which inline them by hand
5073 directly. */
5074 if (DECL_LANG_SPECIFIC (decl)
5075 && DECL_TEMPLATE_INFO (decl)
5076 && (decl_maybe_constant_var_p (decl)
5077 || (TREE_CODE (decl) == FUNCTION_DECL
5078 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5079 || undeduced_auto_decl (decl))
5080 && !uses_template_parms (DECL_TI_ARGS (decl)))
5082 /* Instantiating a function will result in garbage collection. We
5083 must treat this situation as if we were within the body of a
5084 function so as to avoid collecting live data only referenced from
5085 the stack (such as overload resolution candidates). */
5086 ++function_depth;
5087 instantiate_decl (decl, /*defer_ok=*/false,
5088 /*expl_inst_class_mem_p=*/false);
5089 --function_depth;
5092 if (processing_template_decl || in_template_function ())
5093 return true;
5095 /* Check this too in case we're within instantiate_non_dependent_expr. */
5096 if (DECL_TEMPLATE_INFO (decl)
5097 && uses_template_parms (DECL_TI_ARGS (decl)))
5098 return true;
5100 if (undeduced_auto_decl (decl))
5102 if (complain & tf_error)
5103 error ("use of %qD before deduction of %<auto%>", decl);
5104 return false;
5107 /* If we don't need a value, then we don't need to synthesize DECL. */
5108 if (cp_unevaluated_operand != 0)
5109 return true;
5111 DECL_ODR_USED (decl) = 1;
5112 if (DECL_CLONED_FUNCTION_P (decl))
5113 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5115 /* DR 757: A type without linkage shall not be used as the type of a
5116 variable or function with linkage, unless
5117 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5118 o the variable or function is not used (3.2 [basic.def.odr]) or is
5119 defined in the same translation unit. */
5120 if (cxx_dialect > cxx98
5121 && decl_linkage (decl) != lk_none
5122 && !DECL_EXTERN_C_P (decl)
5123 && !DECL_ARTIFICIAL (decl)
5124 && !decl_defined_p (decl)
5125 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5127 if (is_local_extern (decl))
5128 /* There's no way to define a local extern, and adding it to
5129 the vector interferes with GC, so give an error now. */
5130 no_linkage_error (decl);
5131 else
5132 vec_safe_push (no_linkage_decls, decl);
5135 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5136 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5137 /* Remember it, so we can check it was defined. */
5138 note_vague_linkage_fn (decl);
5140 /* Is it a synthesized method that needs to be synthesized? */
5141 if (TREE_CODE (decl) == FUNCTION_DECL
5142 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5143 && DECL_DEFAULTED_FN (decl)
5144 /* A function defaulted outside the class is synthesized either by
5145 cp_finish_decl or instantiate_decl. */
5146 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5147 && ! DECL_INITIAL (decl))
5149 /* Defer virtual destructors so that thunks get the right
5150 linkage. */
5151 if (DECL_VIRTUAL_P (decl) && !at_eof)
5153 note_vague_linkage_fn (decl);
5154 return true;
5157 /* Remember the current location for a function we will end up
5158 synthesizing. Then we can inform the user where it was
5159 required in the case of error. */
5160 DECL_SOURCE_LOCATION (decl) = input_location;
5162 /* Synthesizing an implicitly defined member function will result in
5163 garbage collection. We must treat this situation as if we were
5164 within the body of a function so as to avoid collecting live data
5165 on the stack (such as overload resolution candidates).
5167 We could just let cp_write_global_declarations handle synthesizing
5168 this function by adding it to deferred_fns, but doing
5169 it at the use site produces better error messages. */
5170 ++function_depth;
5171 synthesize_method (decl);
5172 --function_depth;
5173 /* If this is a synthesized method we don't need to
5174 do the instantiation test below. */
5176 else if (VAR_OR_FUNCTION_DECL_P (decl)
5177 && DECL_TEMPLATE_INFO (decl)
5178 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5179 || always_instantiate_p (decl)))
5180 /* If this is a function or variable that is an instance of some
5181 template, we now know that we will need to actually do the
5182 instantiation. We check that DECL is not an explicit
5183 instantiation because that is not checked in instantiate_decl.
5185 We put off instantiating functions in order to improve compile
5186 times. Maintaining a stack of active functions is expensive,
5187 and the inliner knows to instantiate any functions it might
5188 need. Therefore, we always try to defer instantiation. */
5190 ++function_depth;
5191 instantiate_decl (decl, /*defer_ok=*/true,
5192 /*expl_inst_class_mem_p=*/false);
5193 --function_depth;
5196 return true;
5199 bool
5200 mark_used (tree decl)
5202 return mark_used (decl, tf_warning_or_error);
5205 tree
5206 vtv_start_verification_constructor_init_function (void)
5208 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5211 tree
5212 vtv_finish_verification_constructor_init_function (tree function_body)
5214 tree fn;
5216 finish_compound_stmt (function_body);
5217 fn = finish_function (0);
5218 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5219 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5221 return fn;
5224 #include "gt-cp-decl2.h"