* c-decl.c (c_decl_attributes): Also add "omp declare target"
[official-gcc.git] / gcc / cp / decl2.c
blob989a0303babc785c3e9748124b797502a7701704
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "stringpool.h"
44 #include "varasm.h"
45 #include "attribs.h"
46 #include "stor-layout.h"
47 #include "calls.h"
48 #include "flags.h"
49 #include "cp-tree.h"
50 #include "decl.h"
51 #include "toplev.h"
52 #include "timevar.h"
53 #include "cpplib.h"
54 #include "target.h"
55 #include "c-family/c-common.h"
56 #include "c-family/c-objc.h"
57 #include "hash-map.h"
58 #include "is-a.h"
59 #include "plugin-api.h"
60 #include "hard-reg-set.h"
61 #include "input.h"
62 #include "function.h"
63 #include "ipa-ref.h"
64 #include "cgraph.h"
65 #include "tree-inline.h"
66 #include "c-family/c-pragma.h"
67 #include "dumpfile.h"
68 #include "intl.h"
69 #include "splay-tree.h"
70 #include "langhooks.h"
71 #include "c-family/c-ada-spec.h"
72 #include "asan.h"
74 extern cpp_reader *parse_in;
76 /* This structure contains information about the initializations
77 and/or destructions required for a particular priority level. */
78 typedef struct priority_info_s {
79 /* Nonzero if there have been any initializations at this priority
80 throughout the translation unit. */
81 int initializations_p;
82 /* Nonzero if there have been any destructions at this priority
83 throughout the translation unit. */
84 int destructions_p;
85 } *priority_info;
87 static void mark_vtable_entries (tree);
88 static bool maybe_emit_vtables (tree);
89 static bool acceptable_java_type (tree);
90 static tree start_objects (int, int);
91 static void finish_objects (int, int, tree);
92 static tree start_static_storage_duration_function (unsigned);
93 static void finish_static_storage_duration_function (tree);
94 static priority_info get_priority_info (int);
95 static void do_static_initialization_or_destruction (tree, bool);
96 static void one_static_initialization_or_destruction (tree, tree, bool);
97 static void generate_ctor_or_dtor_function (bool, int, location_t *);
98 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
99 void *);
100 static tree prune_vars_needing_no_initialization (tree *);
101 static void write_out_vars (tree);
102 static void import_export_class (tree);
103 static tree get_guard_bits (tree);
104 static void determine_visibility_from_class (tree, tree);
105 static bool determine_hidden_inline (tree);
106 static bool decl_defined_p (tree);
108 /* A list of static class variables. This is needed, because a
109 static class variable can be declared inside the class without
110 an initializer, and then initialized, statically, outside the class. */
111 static GTY(()) vec<tree, va_gc> *pending_statics;
113 /* A list of functions which were declared inline, but which we
114 may need to emit outline anyway. */
115 static GTY(()) vec<tree, va_gc> *deferred_fns;
117 /* A list of decls that use types with no linkage, which we need to make
118 sure are defined. */
119 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
121 /* Nonzero if we're done parsing and into end-of-file activities. */
123 int at_eof;
126 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
127 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
128 that apply to the function). */
130 tree
131 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
132 cp_ref_qualifier rqual)
134 tree raises;
135 tree attrs;
136 int type_quals;
137 bool late_return_type_p;
139 if (fntype == error_mark_node || ctype == error_mark_node)
140 return error_mark_node;
142 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
143 || TREE_CODE (fntype) == METHOD_TYPE);
145 type_quals = quals & ~TYPE_QUAL_RESTRICT;
146 ctype = cp_build_qualified_type (ctype, type_quals);
147 raises = TYPE_RAISES_EXCEPTIONS (fntype);
148 attrs = TYPE_ATTRIBUTES (fntype);
149 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
150 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
151 (TREE_CODE (fntype) == METHOD_TYPE
152 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
153 : TYPE_ARG_TYPES (fntype)));
154 if (attrs)
155 fntype = cp_build_type_attribute_variant (fntype, attrs);
156 if (rqual)
157 fntype = build_ref_qualified_type (fntype, rqual);
158 if (raises)
159 fntype = build_exception_variant (fntype, raises);
160 if (late_return_type_p)
161 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
163 return fntype;
166 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
167 return type changed to NEW_RET. */
169 tree
170 change_return_type (tree new_ret, tree fntype)
172 tree newtype;
173 tree args = TYPE_ARG_TYPES (fntype);
174 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
175 tree attrs = TYPE_ATTRIBUTES (fntype);
176 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
178 if (new_ret == error_mark_node)
179 return fntype;
181 if (same_type_p (new_ret, TREE_TYPE (fntype)))
182 return fntype;
184 if (TREE_CODE (fntype) == FUNCTION_TYPE)
186 newtype = build_function_type (new_ret, args);
187 newtype = apply_memfn_quals (newtype,
188 type_memfn_quals (fntype),
189 type_memfn_rqual (fntype));
191 else
192 newtype = build_method_type_directly
193 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
194 if (raises)
195 newtype = build_exception_variant (newtype, raises);
196 if (attrs)
197 newtype = cp_build_type_attribute_variant (newtype, attrs);
198 if (late_return_type_p)
199 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
201 return newtype;
204 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
205 appropriately. */
207 tree
208 cp_build_parm_decl (tree name, tree type)
210 tree parm = build_decl (input_location,
211 PARM_DECL, name, type);
212 /* DECL_ARG_TYPE is only used by the back end and the back end never
213 sees templates. */
214 if (!processing_template_decl)
215 DECL_ARG_TYPE (parm) = type_passed_as (type);
217 return parm;
220 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
221 indicated NAME. */
223 tree
224 build_artificial_parm (tree name, tree type)
226 tree parm = cp_build_parm_decl (name, type);
227 DECL_ARTIFICIAL (parm) = 1;
228 /* All our artificial parms are implicitly `const'; they cannot be
229 assigned to. */
230 TREE_READONLY (parm) = 1;
231 return parm;
234 /* Constructors for types with virtual baseclasses need an "in-charge" flag
235 saying whether this constructor is responsible for initialization of
236 virtual baseclasses or not. All destructors also need this "in-charge"
237 flag, which additionally determines whether or not the destructor should
238 free the memory for the object.
240 This function adds the "in-charge" flag to member function FN if
241 appropriate. It is called from grokclassfn and tsubst.
242 FN must be either a constructor or destructor.
244 The in-charge flag follows the 'this' parameter, and is followed by the
245 VTT parm (if any), then the user-written parms. */
247 void
248 maybe_retrofit_in_chrg (tree fn)
250 tree basetype, arg_types, parms, parm, fntype;
252 /* If we've already add the in-charge parameter don't do it again. */
253 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
254 return;
256 /* When processing templates we can't know, in general, whether or
257 not we're going to have virtual baseclasses. */
258 if (processing_template_decl)
259 return;
261 /* We don't need an in-charge parameter for constructors that don't
262 have virtual bases. */
263 if (DECL_CONSTRUCTOR_P (fn)
264 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
265 return;
267 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
268 basetype = TREE_TYPE (TREE_VALUE (arg_types));
269 arg_types = TREE_CHAIN (arg_types);
271 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
273 /* If this is a subobject constructor or destructor, our caller will
274 pass us a pointer to our VTT. */
275 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
277 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
279 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
280 DECL_CHAIN (parm) = parms;
281 parms = parm;
283 /* ...and then to TYPE_ARG_TYPES. */
284 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
286 DECL_HAS_VTT_PARM_P (fn) = 1;
289 /* Then add the in-charge parm (before the VTT parm). */
290 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
291 DECL_CHAIN (parm) = parms;
292 parms = parm;
293 arg_types = hash_tree_chain (integer_type_node, arg_types);
295 /* Insert our new parameter(s) into the list. */
296 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
298 /* And rebuild the function type. */
299 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
300 arg_types);
301 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
302 fntype = build_exception_variant (fntype,
303 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
304 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
305 fntype = (cp_build_type_attribute_variant
306 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
307 TREE_TYPE (fn) = fntype;
309 /* Now we've got the in-charge parameter. */
310 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
313 /* Classes overload their constituent function names automatically.
314 When a function name is declared in a record structure,
315 its name is changed to it overloaded name. Since names for
316 constructors and destructors can conflict, we place a leading
317 '$' for destructors.
319 CNAME is the name of the class we are grokking for.
321 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
323 FLAGS contains bits saying what's special about today's
324 arguments. DTOR_FLAG == DESTRUCTOR.
326 If FUNCTION is a destructor, then we must add the `auto-delete' field
327 as a second parameter. There is some hair associated with the fact
328 that we must "declare" this variable in the manner consistent with the
329 way the rest of the arguments were declared.
331 QUALS are the qualifiers for the this pointer. */
333 void
334 grokclassfn (tree ctype, tree function, enum overload_flags flags)
336 tree fn_name = DECL_NAME (function);
338 /* Even within an `extern "C"' block, members get C++ linkage. See
339 [dcl.link] for details. */
340 SET_DECL_LANGUAGE (function, lang_cplusplus);
342 if (fn_name == NULL_TREE)
344 error ("name missing for member function");
345 fn_name = get_identifier ("<anonymous>");
346 DECL_NAME (function) = fn_name;
349 DECL_CONTEXT (function) = ctype;
351 if (flags == DTOR_FLAG)
352 DECL_DESTRUCTOR_P (function) = 1;
354 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
355 maybe_retrofit_in_chrg (function);
358 /* Create an ARRAY_REF, checking for the user doing things backwards
359 along the way. DECLTYPE_P is for N3276, as in the parser. */
361 tree
362 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
363 bool decltype_p)
365 tree type;
366 tree expr;
367 tree orig_array_expr = array_expr;
368 tree orig_index_exp = index_exp;
370 if (error_operand_p (array_expr) || error_operand_p (index_exp))
371 return error_mark_node;
373 if (processing_template_decl)
375 if (type_dependent_expression_p (array_expr)
376 || type_dependent_expression_p (index_exp))
377 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
378 NULL_TREE, NULL_TREE);
379 array_expr = build_non_dependent_expr (array_expr);
380 index_exp = build_non_dependent_expr (index_exp);
383 type = TREE_TYPE (array_expr);
384 gcc_assert (type);
385 type = non_reference (type);
387 /* If they have an `operator[]', use that. */
388 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
390 tsubst_flags_t complain = tf_warning_or_error;
391 if (decltype_p)
392 complain |= tf_decltype;
393 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
394 index_exp, NULL_TREE, /*overload=*/NULL, complain);
396 else
398 tree p1, p2, i1, i2;
400 /* Otherwise, create an ARRAY_REF for a pointer or array type.
401 It is a little-known fact that, if `a' is an array and `i' is
402 an int, you can write `i[a]', which means the same thing as
403 `a[i]'. */
404 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
405 p1 = array_expr;
406 else
407 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
409 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
410 p2 = index_exp;
411 else
412 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
414 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
415 false);
416 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
417 false);
419 if ((p1 && i2) && (i1 && p2))
420 error ("ambiguous conversion for array subscript");
422 if (p1 && i2)
423 array_expr = p1, index_exp = i2;
424 else if (i1 && p2)
425 array_expr = p2, index_exp = i1;
426 else
428 error ("invalid types %<%T[%T]%> for array subscript",
429 type, TREE_TYPE (index_exp));
430 return error_mark_node;
433 if (array_expr == error_mark_node || index_exp == error_mark_node)
434 error ("ambiguous conversion for array subscript");
436 expr = build_array_ref (input_location, array_expr, index_exp);
438 if (processing_template_decl && expr != error_mark_node)
439 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
440 NULL_TREE, NULL_TREE);
441 return expr;
444 /* Given the cast expression EXP, checking out its validity. Either return
445 an error_mark_node if there was an unavoidable error, return a cast to
446 void for trying to delete a pointer w/ the value 0, or return the
447 call to delete. If DOING_VEC is true, we handle things differently
448 for doing an array delete.
449 Implements ARM $5.3.4. This is called from the parser. */
451 tree
452 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
453 tsubst_flags_t complain)
455 tree t, type;
457 if (exp == error_mark_node)
458 return exp;
460 if (processing_template_decl)
462 t = build_min (DELETE_EXPR, void_type_node, exp, size);
463 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
464 DELETE_EXPR_USE_VEC (t) = doing_vec;
465 TREE_SIDE_EFFECTS (t) = 1;
466 return t;
469 /* An array can't have been allocated by new, so complain. */
470 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
471 warning (0, "deleting array %q#E", exp);
473 t = build_expr_type_conversion (WANT_POINTER, exp, true);
475 if (t == NULL_TREE || t == error_mark_node)
477 error ("type %q#T argument given to %<delete%>, expected pointer",
478 TREE_TYPE (exp));
479 return error_mark_node;
482 type = TREE_TYPE (t);
484 /* As of Valley Forge, you can delete a pointer to const. */
486 /* You can't delete functions. */
487 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
489 error ("cannot delete a function. Only pointer-to-objects are "
490 "valid arguments to %<delete%>");
491 return error_mark_node;
494 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
495 if (VOID_TYPE_P (TREE_TYPE (type)))
497 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
498 doing_vec = 0;
501 /* Deleting a pointer with the value zero is valid and has no effect. */
502 if (integer_zerop (t))
503 return build1 (NOP_EXPR, void_type_node, t);
505 if (doing_vec)
506 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
507 sfk_deleting_destructor,
508 use_global_delete, complain);
509 else
510 return build_delete (type, t, sfk_deleting_destructor,
511 LOOKUP_NORMAL, use_global_delete,
512 complain);
515 /* Report an error if the indicated template declaration is not the
516 sort of thing that should be a member template. */
518 void
519 check_member_template (tree tmpl)
521 tree decl;
523 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
524 decl = DECL_TEMPLATE_RESULT (tmpl);
526 if (TREE_CODE (decl) == FUNCTION_DECL
527 || DECL_ALIAS_TEMPLATE_P (tmpl)
528 || (TREE_CODE (decl) == TYPE_DECL
529 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
531 /* The parser rejects template declarations in local classes
532 (with the exception of generic lambdas). */
533 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
534 /* The parser rejects any use of virtual in a function template. */
535 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
536 && DECL_VIRTUAL_P (decl)));
538 /* The debug-information generating code doesn't know what to do
539 with member templates. */
540 DECL_IGNORED_P (tmpl) = 1;
542 else if (variable_template_p (tmpl))
543 /* OK */;
544 else
545 error ("template declaration of %q#D", decl);
548 /* Return true iff TYPE is a valid Java parameter or return type. */
550 static bool
551 acceptable_java_type (tree type)
553 if (type == error_mark_node)
554 return false;
556 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
557 return true;
558 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
560 type = TREE_TYPE (type);
561 if (TREE_CODE (type) == RECORD_TYPE)
563 tree args; int i;
564 if (! TYPE_FOR_JAVA (type))
565 return false;
566 if (! CLASSTYPE_TEMPLATE_INFO (type))
567 return true;
568 args = CLASSTYPE_TI_ARGS (type);
569 i = TREE_VEC_LENGTH (args);
570 while (--i >= 0)
572 type = TREE_VEC_ELT (args, i);
573 if (TYPE_PTR_P (type))
574 type = TREE_TYPE (type);
575 if (! TYPE_FOR_JAVA (type))
576 return false;
578 return true;
581 return false;
584 /* For a METHOD in a Java class CTYPE, return true if
585 the parameter and return types are valid Java types.
586 Otherwise, print appropriate error messages, and return false. */
588 bool
589 check_java_method (tree method)
591 bool jerr = false;
592 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
593 tree ret_type = TREE_TYPE (TREE_TYPE (method));
595 if (!acceptable_java_type (ret_type))
597 error ("Java method %qD has non-Java return type %qT",
598 method, ret_type);
599 jerr = true;
602 arg_types = TREE_CHAIN (arg_types);
603 if (DECL_HAS_IN_CHARGE_PARM_P (method))
604 arg_types = TREE_CHAIN (arg_types);
605 if (DECL_HAS_VTT_PARM_P (method))
606 arg_types = TREE_CHAIN (arg_types);
608 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
610 tree type = TREE_VALUE (arg_types);
611 if (!acceptable_java_type (type))
613 if (type != error_mark_node)
614 error ("Java method %qD has non-Java parameter type %qT",
615 method, type);
616 jerr = true;
619 return !jerr;
622 /* Sanity check: report error if this function FUNCTION is not
623 really a member of the class (CTYPE) it is supposed to belong to.
624 TEMPLATE_PARMS is used to specify the template parameters of a member
625 template passed as FUNCTION_DECL. If the member template is passed as a
626 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
627 from the declaration. If the function is not a function template, it
628 must be NULL.
629 It returns the original declaration for the function, NULL_TREE if
630 no declaration was found, error_mark_node if an error was emitted. */
632 tree
633 check_classfn (tree ctype, tree function, tree template_parms)
635 int ix;
636 bool is_template;
637 tree pushed_scope;
639 if (DECL_USE_TEMPLATE (function)
640 && !(TREE_CODE (function) == TEMPLATE_DECL
641 && DECL_TEMPLATE_SPECIALIZATION (function))
642 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
643 /* Since this is a specialization of a member template,
644 we're not going to find the declaration in the class.
645 For example, in:
647 struct S { template <typename T> void f(T); };
648 template <> void S::f(int);
650 we're not going to find `S::f(int)', but there's no
651 reason we should, either. We let our callers know we didn't
652 find the method, but we don't complain. */
653 return NULL_TREE;
655 /* Basic sanity check: for a template function, the template parameters
656 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
657 if (TREE_CODE (function) == TEMPLATE_DECL)
659 if (template_parms
660 && !comp_template_parms (template_parms,
661 DECL_TEMPLATE_PARMS (function)))
663 error ("template parameter lists provided don%'t match the "
664 "template parameters of %qD", function);
665 return error_mark_node;
667 template_parms = DECL_TEMPLATE_PARMS (function);
670 /* OK, is this a definition of a member template? */
671 is_template = (template_parms != NULL_TREE);
673 /* [temp.mem]
675 A destructor shall not be a member template. */
676 if (DECL_DESTRUCTOR_P (function) && is_template)
678 error ("destructor %qD declared as member template", function);
679 return error_mark_node;
682 /* We must enter the scope here, because conversion operators are
683 named by target type, and type equivalence relies on typenames
684 resolving within the scope of CTYPE. */
685 pushed_scope = push_scope (ctype);
686 ix = class_method_index_for_fn (complete_type (ctype), function);
687 if (ix >= 0)
689 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
690 tree fndecls, fndecl = 0;
691 bool is_conv_op;
692 const char *format = NULL;
694 for (fndecls = (*methods)[ix];
695 fndecls; fndecls = OVL_NEXT (fndecls))
697 tree p1, p2;
699 fndecl = OVL_CURRENT (fndecls);
700 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
701 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
703 /* We cannot simply call decls_match because this doesn't
704 work for static member functions that are pretending to
705 be methods, and because the name may have been changed by
706 asm("new_name"). */
708 /* Get rid of the this parameter on functions that become
709 static. */
710 if (DECL_STATIC_FUNCTION_P (fndecl)
711 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
712 p1 = TREE_CHAIN (p1);
714 /* A member template definition only matches a member template
715 declaration. */
716 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
717 continue;
719 /* ref-qualifier or absence of same must match. */
720 if (type_memfn_rqual (TREE_TYPE (function))
721 != type_memfn_rqual (TREE_TYPE (fndecl)))
722 continue;
724 /* While finding a match, same types and params are not enough
725 if the function is versioned. Also check version ("target")
726 attributes. */
727 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
728 TREE_TYPE (TREE_TYPE (fndecl)))
729 && compparms (p1, p2)
730 && !targetm.target_option.function_versions (function, fndecl)
731 && (!is_template
732 || comp_template_parms (template_parms,
733 DECL_TEMPLATE_PARMS (fndecl)))
734 && (DECL_TEMPLATE_SPECIALIZATION (function)
735 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
736 && (!DECL_TEMPLATE_SPECIALIZATION (function)
737 || (DECL_TI_TEMPLATE (function)
738 == DECL_TI_TEMPLATE (fndecl))))
739 break;
741 if (fndecls)
743 if (pushed_scope)
744 pop_scope (pushed_scope);
745 return OVL_CURRENT (fndecls);
748 error_at (DECL_SOURCE_LOCATION (function),
749 "prototype for %q#D does not match any in class %qT",
750 function, ctype);
751 is_conv_op = DECL_CONV_FN_P (fndecl);
753 if (is_conv_op)
754 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
755 fndecls = (*methods)[ix];
756 while (fndecls)
758 fndecl = OVL_CURRENT (fndecls);
759 fndecls = OVL_NEXT (fndecls);
761 if (!fndecls && is_conv_op)
763 if (methods->length () > (size_t) ++ix)
765 fndecls = (*methods)[ix];
766 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
768 fndecls = NULL_TREE;
769 is_conv_op = false;
772 else
773 is_conv_op = false;
775 if (format)
776 format = " %+#D";
777 else if (fndecls)
778 format = N_("candidates are: %+#D");
779 else
780 format = N_("candidate is: %+#D");
781 error (format, fndecl);
784 else if (!COMPLETE_TYPE_P (ctype))
785 cxx_incomplete_type_error (function, ctype);
786 else
787 error ("no %q#D member function declared in class %qT",
788 function, ctype);
790 if (pushed_scope)
791 pop_scope (pushed_scope);
792 return error_mark_node;
795 /* DECL is a function with vague linkage. Remember it so that at the
796 end of the translation unit we can decide whether or not to emit
797 it. */
799 void
800 note_vague_linkage_fn (tree decl)
802 DECL_DEFER_OUTPUT (decl) = 1;
803 vec_safe_push (deferred_fns, decl);
806 /* As above, but for variable template instantiations. */
808 void
809 note_variable_template_instantiation (tree decl)
811 vec_safe_push (pending_statics, decl);
814 /* We have just processed the DECL, which is a static data member.
815 The other parameters are as for cp_finish_decl. */
817 void
818 finish_static_data_member_decl (tree decl,
819 tree init, bool init_const_expr_p,
820 tree asmspec_tree,
821 int flags)
823 DECL_CONTEXT (decl) = current_class_type;
825 /* We cannot call pushdecl here, because that would fill in the
826 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
827 the right thing, namely, to put this decl out straight away. */
829 if (! processing_template_decl)
830 vec_safe_push (pending_statics, decl);
832 if (LOCAL_CLASS_P (current_class_type)
833 /* We already complained about the template definition. */
834 && !DECL_TEMPLATE_INSTANTIATION (decl))
835 permerror (input_location, "local class %q#T shall not have static data member %q#D",
836 current_class_type, decl);
837 else
838 for (tree t = current_class_type; TYPE_P (t);
839 t = CP_TYPE_CONTEXT (t))
840 if (TYPE_ANONYMOUS_P (t))
842 if (permerror (DECL_SOURCE_LOCATION (decl),
843 "static data member %qD in unnamed class", decl))
844 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
845 "unnamed class defined here");
846 break;
849 DECL_IN_AGGR_P (decl) = 1;
851 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
852 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
853 SET_VAR_HAD_UNKNOWN_BOUND (decl);
855 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
858 /* DECLARATOR and DECLSPECS correspond to a class member. The other
859 parameters are as for cp_finish_decl. Return the DECL for the
860 class member declared. */
862 tree
863 grokfield (const cp_declarator *declarator,
864 cp_decl_specifier_seq *declspecs,
865 tree init, bool init_const_expr_p,
866 tree asmspec_tree,
867 tree attrlist)
869 tree value;
870 const char *asmspec = 0;
871 int flags;
872 tree name;
874 if (init
875 && TREE_CODE (init) == TREE_LIST
876 && TREE_VALUE (init) == error_mark_node
877 && TREE_CHAIN (init) == NULL_TREE)
878 init = NULL_TREE;
880 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
881 if (! value || value == error_mark_node)
882 /* friend or constructor went bad. */
883 return error_mark_node;
884 if (TREE_TYPE (value) == error_mark_node)
885 return value;
887 if (TREE_CODE (value) == TYPE_DECL && init)
889 error ("typedef %qD is initialized (use decltype instead)", value);
890 init = NULL_TREE;
893 /* Pass friendly classes back. */
894 if (value == void_type_node)
895 return value;
898 name = DECL_NAME (value);
900 if (name != NULL_TREE)
902 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
904 error ("explicit template argument list not allowed");
905 return error_mark_node;
908 if (IDENTIFIER_POINTER (name)[0] == '_'
909 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
910 error ("member %qD conflicts with virtual function table field name",
911 value);
914 /* Stash away type declarations. */
915 if (TREE_CODE (value) == TYPE_DECL)
917 DECL_NONLOCAL (value) = 1;
918 DECL_CONTEXT (value) = current_class_type;
920 if (attrlist)
922 int attrflags = 0;
924 /* If this is a typedef that names the class for linkage purposes
925 (7.1.3p8), apply any attributes directly to the type. */
926 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
927 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
928 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
930 cplus_decl_attributes (&value, attrlist, attrflags);
933 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
934 && TREE_TYPE (value) != error_mark_node
935 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
936 set_underlying_type (value);
938 /* It's important that push_template_decl below follows
939 set_underlying_type above so that the created template
940 carries the properly set type of VALUE. */
941 if (processing_template_decl)
942 value = push_template_decl (value);
944 record_locally_defined_typedef (value);
945 return value;
948 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
950 if (!friendp && DECL_IN_AGGR_P (value))
952 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
953 return void_type_node;
956 if (asmspec_tree && asmspec_tree != error_mark_node)
957 asmspec = TREE_STRING_POINTER (asmspec_tree);
959 if (init)
961 if (TREE_CODE (value) == FUNCTION_DECL)
963 if (init == ridpointers[(int)RID_DELETE])
965 DECL_DELETED_FN (value) = 1;
966 DECL_DECLARED_INLINE_P (value) = 1;
967 DECL_INITIAL (value) = error_mark_node;
969 else if (init == ridpointers[(int)RID_DEFAULT])
971 if (defaultable_fn_check (value))
973 DECL_DEFAULTED_FN (value) = 1;
974 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
975 DECL_DECLARED_INLINE_P (value) = 1;
978 else if (TREE_CODE (init) == DEFAULT_ARG)
979 error ("invalid initializer for member function %qD", value);
980 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
982 if (integer_zerop (init))
983 DECL_PURE_VIRTUAL_P (value) = 1;
984 else if (error_operand_p (init))
985 ; /* An error has already been reported. */
986 else
987 error ("invalid initializer for member function %qD",
988 value);
990 else
992 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
993 if (friendp)
994 error ("initializer specified for friend function %qD",
995 value);
996 else
997 error ("initializer specified for static member function %qD",
998 value);
1001 else if (TREE_CODE (value) == FIELD_DECL)
1002 /* C++11 NSDMI, keep going. */;
1003 else if (!VAR_P (value))
1004 gcc_unreachable ();
1007 /* Pass friend decls back. */
1008 if ((TREE_CODE (value) == FUNCTION_DECL
1009 || TREE_CODE (value) == TEMPLATE_DECL)
1010 && DECL_CONTEXT (value) != current_class_type)
1011 return value;
1013 /* Need to set this before push_template_decl. */
1014 if (TREE_CODE (value) == VAR_DECL)
1015 DECL_CONTEXT (value) = current_class_type;
1017 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1019 value = push_template_decl (value);
1020 if (error_operand_p (value))
1021 return error_mark_node;
1024 if (attrlist)
1025 cplus_decl_attributes (&value, attrlist, 0);
1027 if (init && DIRECT_LIST_INIT_P (init))
1028 flags = LOOKUP_NORMAL;
1029 else
1030 flags = LOOKUP_IMPLICIT;
1032 switch (TREE_CODE (value))
1034 case VAR_DECL:
1035 finish_static_data_member_decl (value, init, init_const_expr_p,
1036 asmspec_tree, flags);
1037 return value;
1039 case FIELD_DECL:
1040 if (asmspec)
1041 error ("%<asm%> specifiers are not permitted on non-static data members");
1042 if (DECL_INITIAL (value) == error_mark_node)
1043 init = error_mark_node;
1044 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1045 NULL_TREE, flags);
1046 DECL_IN_AGGR_P (value) = 1;
1047 return value;
1049 case FUNCTION_DECL:
1050 if (asmspec)
1051 set_user_assembler_name (value, asmspec);
1053 cp_finish_decl (value,
1054 /*init=*/NULL_TREE,
1055 /*init_const_expr_p=*/false,
1056 asmspec_tree, flags);
1058 /* Pass friends back this way. */
1059 if (DECL_FRIEND_P (value))
1060 return void_type_node;
1062 DECL_IN_AGGR_P (value) = 1;
1063 return value;
1065 default:
1066 gcc_unreachable ();
1068 return NULL_TREE;
1071 /* Like `grokfield', but for bitfields.
1072 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1074 tree
1075 grokbitfield (const cp_declarator *declarator,
1076 cp_decl_specifier_seq *declspecs, tree width,
1077 tree attrlist)
1079 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1081 if (value == error_mark_node)
1082 return NULL_TREE; /* friends went bad. */
1083 if (TREE_TYPE (value) == error_mark_node)
1084 return value;
1086 /* Pass friendly classes back. */
1087 if (VOID_TYPE_P (value))
1088 return void_type_node;
1090 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1091 && (POINTER_TYPE_P (value)
1092 || !dependent_type_p (TREE_TYPE (value))))
1094 error ("bit-field %qD with non-integral type", value);
1095 return error_mark_node;
1098 if (TREE_CODE (value) == TYPE_DECL)
1100 error ("cannot declare %qD to be a bit-field type", value);
1101 return NULL_TREE;
1104 /* Usually, finish_struct_1 catches bitfields with invalid types.
1105 But, in the case of bitfields with function type, we confuse
1106 ourselves into thinking they are member functions, so we must
1107 check here. */
1108 if (TREE_CODE (value) == FUNCTION_DECL)
1110 error ("cannot declare bit-field %qD with function type",
1111 DECL_NAME (value));
1112 return NULL_TREE;
1115 if (DECL_IN_AGGR_P (value))
1117 error ("%qD is already defined in the class %qT", value,
1118 DECL_CONTEXT (value));
1119 return void_type_node;
1122 if (TREE_STATIC (value))
1124 error ("static member %qD cannot be a bit-field", value);
1125 return NULL_TREE;
1127 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1129 if (width != error_mark_node)
1131 /* The width must be an integer type. */
1132 if (!type_dependent_expression_p (width)
1133 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1134 error ("width of bit-field %qD has non-integral type %qT", value,
1135 TREE_TYPE (width));
1136 DECL_INITIAL (value) = width;
1137 SET_DECL_C_BIT_FIELD (value);
1140 DECL_IN_AGGR_P (value) = 1;
1142 if (attrlist)
1143 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1145 return value;
1149 /* Returns true iff ATTR is an attribute which needs to be applied at
1150 instantiation time rather than template definition time. */
1152 static bool
1153 is_late_template_attribute (tree attr, tree decl)
1155 tree name = get_attribute_name (attr);
1156 tree args = TREE_VALUE (attr);
1157 const struct attribute_spec *spec = lookup_attribute_spec (name);
1158 tree arg;
1160 if (!spec)
1161 /* Unknown attribute. */
1162 return false;
1164 /* Attribute weak handling wants to write out assembly right away. */
1165 if (is_attribute_p ("weak", name))
1166 return true;
1168 /* Attribute unused is applied directly, as it appertains to
1169 decls. */
1170 if (is_attribute_p ("unused", name))
1171 return false;
1173 /* #pragma omp declare simd attribute needs to be always deferred. */
1174 if (flag_openmp
1175 && is_attribute_p ("omp declare simd", name))
1176 return true;
1178 /* If any of the arguments are dependent expressions, we can't evaluate
1179 the attribute until instantiation time. */
1180 for (arg = args; arg; arg = TREE_CHAIN (arg))
1182 tree t = TREE_VALUE (arg);
1184 /* If the first attribute argument is an identifier, only consider
1185 second and following arguments. Attributes like mode, format,
1186 cleanup and several target specific attributes aren't late
1187 just because they have an IDENTIFIER_NODE as first argument. */
1188 if (arg == args && identifier_p (t))
1189 continue;
1191 if (value_dependent_expression_p (t)
1192 || type_dependent_expression_p (t))
1193 return true;
1196 if (TREE_CODE (decl) == TYPE_DECL
1197 || TYPE_P (decl)
1198 || spec->type_required)
1200 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1202 /* We can't apply any attributes to a completely unknown type until
1203 instantiation time. */
1204 enum tree_code code = TREE_CODE (type);
1205 if (code == TEMPLATE_TYPE_PARM
1206 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1207 || code == TYPENAME_TYPE)
1208 return true;
1209 /* Also defer most attributes on dependent types. This is not
1210 necessary in all cases, but is the better default. */
1211 else if (dependent_type_p (type)
1212 /* But some attributes specifically apply to templates. */
1213 && !is_attribute_p ("abi_tag", name)
1214 && !is_attribute_p ("deprecated", name)
1215 && !is_attribute_p ("visibility", name))
1216 return true;
1217 else
1218 return false;
1220 else
1221 return false;
1224 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1225 applied at instantiation time and return them. If IS_DEPENDENT is true,
1226 the declaration itself is dependent, so all attributes should be applied
1227 at instantiation time. */
1229 static tree
1230 splice_template_attributes (tree *attr_p, tree decl)
1232 tree *p = attr_p;
1233 tree late_attrs = NULL_TREE;
1234 tree *q = &late_attrs;
1236 if (!p)
1237 return NULL_TREE;
1239 for (; *p; )
1241 if (is_late_template_attribute (*p, decl))
1243 ATTR_IS_DEPENDENT (*p) = 1;
1244 *q = *p;
1245 *p = TREE_CHAIN (*p);
1246 q = &TREE_CHAIN (*q);
1247 *q = NULL_TREE;
1249 else
1250 p = &TREE_CHAIN (*p);
1253 return late_attrs;
1256 /* Remove any late attributes from the list in ATTR_P and attach them to
1257 DECL_P. */
1259 static void
1260 save_template_attributes (tree *attr_p, tree *decl_p)
1262 tree *q;
1264 if (attr_p && *attr_p == error_mark_node)
1265 return;
1267 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1268 if (!late_attrs)
1269 return;
1271 if (DECL_P (*decl_p))
1272 q = &DECL_ATTRIBUTES (*decl_p);
1273 else
1274 q = &TYPE_ATTRIBUTES (*decl_p);
1276 tree old_attrs = *q;
1278 /* Merge the late attributes at the beginning with the attribute
1279 list. */
1280 late_attrs = merge_attributes (late_attrs, *q);
1281 *q = late_attrs;
1283 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1285 /* We've added new attributes directly to the main variant, so
1286 now we need to update all of the other variants to include
1287 these new attributes. */
1288 tree variant;
1289 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1290 variant = TYPE_NEXT_VARIANT (variant))
1292 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1293 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1298 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1299 to a typedef which gives a previously anonymous class or enum a name for
1300 linkage purposes. */
1302 bool
1303 attributes_naming_typedef_ok (tree attrs)
1305 for (; attrs; attrs = TREE_CHAIN (attrs))
1307 tree name = get_attribute_name (attrs);
1308 if (is_attribute_p ("vector_size", name))
1309 return false;
1311 return true;
1314 /* Like reconstruct_complex_type, but handle also template trees. */
1316 tree
1317 cp_reconstruct_complex_type (tree type, tree bottom)
1319 tree inner, outer;
1320 bool late_return_type_p = false;
1322 if (TYPE_PTR_P (type))
1324 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1325 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1326 TYPE_REF_CAN_ALIAS_ALL (type));
1328 else if (TREE_CODE (type) == REFERENCE_TYPE)
1330 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1331 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1332 TYPE_REF_CAN_ALIAS_ALL (type));
1334 else if (TREE_CODE (type) == ARRAY_TYPE)
1336 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1337 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1338 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1339 element type qualification will be handled by the recursive
1340 cp_reconstruct_complex_type call and cp_build_qualified_type
1341 for ARRAY_TYPEs changes the element type. */
1342 return outer;
1344 else if (TREE_CODE (type) == FUNCTION_TYPE)
1346 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1347 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1348 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1349 outer = apply_memfn_quals (outer,
1350 type_memfn_quals (type),
1351 type_memfn_rqual (type));
1353 else if (TREE_CODE (type) == METHOD_TYPE)
1355 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1356 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1357 /* The build_method_type_directly() routine prepends 'this' to argument list,
1358 so we must compensate by getting rid of it. */
1359 outer
1360 = build_method_type_directly
1361 (class_of_this_parm (type), inner,
1362 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1364 else if (TREE_CODE (type) == OFFSET_TYPE)
1366 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1367 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1369 else
1370 return bottom;
1372 if (TYPE_ATTRIBUTES (type))
1373 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1374 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1376 if (late_return_type_p)
1377 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1379 return outer;
1382 /* Replaces any constexpr expression that may be into the attributes
1383 arguments with their reduced value. */
1385 static void
1386 cp_check_const_attributes (tree attributes)
1388 if (attributes == error_mark_node)
1389 return;
1391 tree attr;
1392 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1394 tree arg;
1395 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1397 tree expr = TREE_VALUE (arg);
1398 if (EXPR_P (expr))
1399 TREE_VALUE (arg) = maybe_constant_value (expr);
1404 /* Return true if TYPE is an OpenMP mappable type. */
1405 bool
1406 cp_omp_mappable_type (tree type)
1408 /* Mappable type has to be complete. */
1409 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1410 return false;
1411 /* Arrays have mappable type if the elements have mappable type. */
1412 while (TREE_CODE (type) == ARRAY_TYPE)
1413 type = TREE_TYPE (type);
1414 /* A mappable type cannot contain virtual members. */
1415 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1416 return false;
1417 /* All data members must be non-static. */
1418 if (CLASS_TYPE_P (type))
1420 tree field;
1421 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1422 if (TREE_CODE (field) == VAR_DECL)
1423 return false;
1424 /* All fields must have mappable types. */
1425 else if (TREE_CODE (field) == FIELD_DECL
1426 && !cp_omp_mappable_type (TREE_TYPE (field)))
1427 return false;
1429 return true;
1432 /* Like decl_attributes, but handle C++ complexity. */
1434 void
1435 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1437 if (*decl == NULL_TREE || *decl == void_type_node
1438 || *decl == error_mark_node)
1439 return;
1441 /* Add implicit "omp declare target" attribute if requested. */
1442 if (scope_chain->omp_declare_target_attribute
1443 && ((TREE_CODE (*decl) == VAR_DECL
1444 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1445 || TREE_CODE (*decl) == FUNCTION_DECL))
1447 if (TREE_CODE (*decl) == VAR_DECL
1448 && DECL_CLASS_SCOPE_P (*decl))
1449 error ("%q+D static data member inside of declare target directive",
1450 *decl);
1451 else if (TREE_CODE (*decl) == VAR_DECL
1452 && (DECL_FUNCTION_SCOPE_P (*decl)
1453 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1454 error ("%q+D in block scope inside of declare target directive",
1455 *decl);
1456 else if (!processing_template_decl
1457 && TREE_CODE (*decl) == VAR_DECL
1458 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1459 error ("%q+D in declare target directive does not have mappable type",
1460 *decl);
1461 else
1462 attributes = tree_cons (get_identifier ("omp declare target"),
1463 NULL_TREE, attributes);
1466 if (processing_template_decl)
1468 if (check_for_bare_parameter_packs (attributes))
1469 return;
1471 save_template_attributes (&attributes, decl);
1474 cp_check_const_attributes (attributes);
1476 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1477 decl = &DECL_TEMPLATE_RESULT (*decl);
1479 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1481 attributes
1482 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1483 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1484 attributes, flags);
1486 else
1487 decl_attributes (decl, attributes, flags);
1489 if (TREE_CODE (*decl) == TYPE_DECL)
1490 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1492 /* Propagate deprecation out to the template. */
1493 if (TREE_DEPRECATED (*decl))
1494 if (tree ti = get_template_info (*decl))
1496 tree tmpl = TI_TEMPLATE (ti);
1497 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1498 : DECL_TEMPLATE_RESULT (tmpl));
1499 if (*decl == pattern)
1500 TREE_DEPRECATED (tmpl) = true;
1504 /* Walks through the namespace- or function-scope anonymous union
1505 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1506 Returns one of the fields for use in the mangled name. */
1508 static tree
1509 build_anon_union_vars (tree type, tree object)
1511 tree main_decl = NULL_TREE;
1512 tree field;
1514 /* Rather than write the code to handle the non-union case,
1515 just give an error. */
1516 if (TREE_CODE (type) != UNION_TYPE)
1518 error ("anonymous struct not inside named type");
1519 return error_mark_node;
1522 for (field = TYPE_FIELDS (type);
1523 field != NULL_TREE;
1524 field = DECL_CHAIN (field))
1526 tree decl;
1527 tree ref;
1529 if (DECL_ARTIFICIAL (field))
1530 continue;
1531 if (TREE_CODE (field) != FIELD_DECL)
1533 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1534 "have non-static data members", field);
1535 continue;
1538 if (TREE_PRIVATE (field))
1539 permerror (input_location, "private member %q+#D in anonymous union", field);
1540 else if (TREE_PROTECTED (field))
1541 permerror (input_location, "protected member %q+#D in anonymous union", field);
1543 if (processing_template_decl)
1544 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1545 DECL_NAME (field), NULL_TREE);
1546 else
1547 ref = build_class_member_access_expr (object, field, NULL_TREE,
1548 false, tf_warning_or_error);
1550 if (DECL_NAME (field))
1552 tree base;
1554 decl = build_decl (input_location,
1555 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1556 DECL_ANON_UNION_VAR_P (decl) = 1;
1557 DECL_ARTIFICIAL (decl) = 1;
1559 base = get_base_address (object);
1560 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1561 TREE_STATIC (decl) = TREE_STATIC (base);
1562 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1564 SET_DECL_VALUE_EXPR (decl, ref);
1565 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1567 decl = pushdecl (decl);
1569 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1570 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1571 else
1572 decl = 0;
1574 if (main_decl == NULL_TREE)
1575 main_decl = decl;
1578 return main_decl;
1581 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1582 anonymous union, then all members must be laid out together. PUBLIC_P
1583 is nonzero if this union is not declared static. */
1585 void
1586 finish_anon_union (tree anon_union_decl)
1588 tree type;
1589 tree main_decl;
1590 bool public_p;
1592 if (anon_union_decl == error_mark_node)
1593 return;
1595 type = TREE_TYPE (anon_union_decl);
1596 public_p = TREE_PUBLIC (anon_union_decl);
1598 /* The VAR_DECL's context is the same as the TYPE's context. */
1599 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1601 if (TYPE_FIELDS (type) == NULL_TREE)
1602 return;
1604 if (public_p)
1606 error ("namespace-scope anonymous aggregates must be static");
1607 return;
1610 main_decl = build_anon_union_vars (type, anon_union_decl);
1611 if (main_decl == error_mark_node)
1612 return;
1613 if (main_decl == NULL_TREE)
1615 warning (0, "anonymous union with no members");
1616 return;
1619 if (!processing_template_decl)
1621 /* Use main_decl to set the mangled name. */
1622 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1623 maybe_commonize_var (anon_union_decl);
1624 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1625 mangle_decl (anon_union_decl);
1626 DECL_NAME (anon_union_decl) = NULL_TREE;
1629 pushdecl (anon_union_decl);
1630 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1633 /* Auxiliary functions to make type signatures for
1634 `operator new' and `operator delete' correspond to
1635 what compiler will be expecting. */
1637 tree
1638 coerce_new_type (tree type)
1640 int e = 0;
1641 tree args = TYPE_ARG_TYPES (type);
1643 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1645 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1647 e = 1;
1648 error ("%<operator new%> must return type %qT", ptr_type_node);
1651 if (args && args != void_list_node)
1653 if (TREE_PURPOSE (args))
1655 /* [basic.stc.dynamic.allocation]
1657 The first parameter shall not have an associated default
1658 argument. */
1659 error ("the first parameter of %<operator new%> cannot "
1660 "have a default argument");
1661 /* Throw away the default argument. */
1662 TREE_PURPOSE (args) = NULL_TREE;
1665 if (!same_type_p (TREE_VALUE (args), size_type_node))
1667 e = 2;
1668 args = TREE_CHAIN (args);
1671 else
1672 e = 2;
1674 if (e == 2)
1675 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1676 "as first parameter", size_type_node);
1678 switch (e)
1680 case 2:
1681 args = tree_cons (NULL_TREE, size_type_node, args);
1682 /* Fall through. */
1683 case 1:
1684 type = build_exception_variant
1685 (build_function_type (ptr_type_node, args),
1686 TYPE_RAISES_EXCEPTIONS (type));
1687 /* Fall through. */
1688 default:;
1690 return type;
1693 tree
1694 coerce_delete_type (tree type)
1696 int e = 0;
1697 tree args = TYPE_ARG_TYPES (type);
1699 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1701 if (!same_type_p (TREE_TYPE (type), void_type_node))
1703 e = 1;
1704 error ("%<operator delete%> must return type %qT", void_type_node);
1707 if (!args || args == void_list_node
1708 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1710 e = 2;
1711 if (args && args != void_list_node)
1712 args = TREE_CHAIN (args);
1713 error ("%<operator delete%> takes type %qT as first parameter",
1714 ptr_type_node);
1716 switch (e)
1718 case 2:
1719 args = tree_cons (NULL_TREE, ptr_type_node, args);
1720 /* Fall through. */
1721 case 1:
1722 type = build_exception_variant
1723 (build_function_type (void_type_node, args),
1724 TYPE_RAISES_EXCEPTIONS (type));
1725 /* Fall through. */
1726 default:;
1729 return type;
1732 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1733 and mark them as needed. */
1735 static void
1736 mark_vtable_entries (tree decl)
1738 tree fnaddr;
1739 unsigned HOST_WIDE_INT idx;
1741 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1742 idx, fnaddr)
1744 tree fn;
1746 STRIP_NOPS (fnaddr);
1748 if (TREE_CODE (fnaddr) != ADDR_EXPR
1749 && TREE_CODE (fnaddr) != FDESC_EXPR)
1750 /* This entry is an offset: a virtual base class offset, a
1751 virtual call offset, an RTTI offset, etc. */
1752 continue;
1754 fn = TREE_OPERAND (fnaddr, 0);
1755 TREE_ADDRESSABLE (fn) = 1;
1756 /* When we don't have vcall offsets, we output thunks whenever
1757 we output the vtables that contain them. With vcall offsets,
1758 we know all the thunks we'll need when we emit a virtual
1759 function, so we emit the thunks there instead. */
1760 if (DECL_THUNK_P (fn))
1761 use_thunk (fn, /*emit_p=*/0);
1762 mark_used (fn);
1766 /* Set DECL up to have the closest approximation of "initialized common"
1767 linkage available. */
1769 void
1770 comdat_linkage (tree decl)
1772 if (flag_weak)
1773 make_decl_one_only (decl, cxx_comdat_group (decl));
1774 else if (TREE_CODE (decl) == FUNCTION_DECL
1775 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1776 /* We can just emit function and compiler-generated variables
1777 statically; having multiple copies is (for the most part) only
1778 a waste of space.
1780 There are two correctness issues, however: the address of a
1781 template instantiation with external linkage should be the
1782 same, independent of what translation unit asks for the
1783 address, and this will not hold when we emit multiple copies of
1784 the function. However, there's little else we can do.
1786 Also, by default, the typeinfo implementation assumes that
1787 there will be only one copy of the string used as the name for
1788 each type. Therefore, if weak symbols are unavailable, the
1789 run-time library should perform a more conservative check; it
1790 should perform a string comparison, rather than an address
1791 comparison. */
1792 TREE_PUBLIC (decl) = 0;
1793 else
1795 /* Static data member template instantiations, however, cannot
1796 have multiple copies. */
1797 if (DECL_INITIAL (decl) == 0
1798 || DECL_INITIAL (decl) == error_mark_node)
1799 DECL_COMMON (decl) = 1;
1800 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1802 DECL_COMMON (decl) = 1;
1803 DECL_INITIAL (decl) = error_mark_node;
1805 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1807 /* We can't do anything useful; leave vars for explicit
1808 instantiation. */
1809 DECL_EXTERNAL (decl) = 1;
1810 DECL_NOT_REALLY_EXTERN (decl) = 0;
1814 DECL_COMDAT (decl) = 1;
1817 /* For win32 we also want to put explicit instantiations in
1818 linkonce sections, so that they will be merged with implicit
1819 instantiations; otherwise we get duplicate symbol errors.
1820 For Darwin we do not want explicit instantiations to be
1821 linkonce. */
1823 void
1824 maybe_make_one_only (tree decl)
1826 /* We used to say that this was not necessary on targets that support weak
1827 symbols, because the implicit instantiations will defer to the explicit
1828 one. However, that's not actually the case in SVR4; a strong definition
1829 after a weak one is an error. Also, not making explicit
1830 instantiations one_only means that we can end up with two copies of
1831 some template instantiations. */
1832 if (! flag_weak)
1833 return;
1835 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1836 we can get away with not emitting them if they aren't used. We need
1837 to for variables so that cp_finish_decl will update their linkage,
1838 because their DECL_INITIAL may not have been set properly yet. */
1840 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1841 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1842 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1844 make_decl_one_only (decl, cxx_comdat_group (decl));
1846 if (VAR_P (decl))
1848 varpool_node *node = varpool_node::get_create (decl);
1849 DECL_COMDAT (decl) = 1;
1850 /* Mark it needed so we don't forget to emit it. */
1851 node->forced_by_abi = true;
1852 TREE_USED (decl) = 1;
1857 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1858 This predicate will give the right answer during parsing of the
1859 function, which other tests may not. */
1861 bool
1862 vague_linkage_p (tree decl)
1864 /* Unfortunately, import_export_decl has not always been called
1865 before the function is processed, so we cannot simply check
1866 DECL_COMDAT. */
1867 if (DECL_COMDAT (decl)
1868 || (((TREE_CODE (decl) == FUNCTION_DECL
1869 && DECL_DECLARED_INLINE_P (decl))
1870 || (DECL_LANG_SPECIFIC (decl)
1871 && DECL_TEMPLATE_INSTANTIATION (decl)))
1872 && TREE_PUBLIC (decl)))
1873 return true;
1874 else if (DECL_FUNCTION_SCOPE_P (decl))
1875 /* A local static in an inline effectively has vague linkage. */
1876 return (TREE_STATIC (decl)
1877 && vague_linkage_p (DECL_CONTEXT (decl)));
1878 else
1879 return false;
1882 /* Determine whether or not we want to specifically import or export CTYPE,
1883 using various heuristics. */
1885 static void
1886 import_export_class (tree ctype)
1888 /* -1 for imported, 1 for exported. */
1889 int import_export = 0;
1891 /* It only makes sense to call this function at EOF. The reason is
1892 that this function looks at whether or not the first non-inline
1893 non-abstract virtual member function has been defined in this
1894 translation unit. But, we can't possibly know that until we've
1895 seen the entire translation unit. */
1896 gcc_assert (at_eof);
1898 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1899 return;
1901 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1902 we will have CLASSTYPE_INTERFACE_ONLY set but not
1903 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1904 heuristic because someone will supply a #pragma implementation
1905 elsewhere, and deducing it here would produce a conflict. */
1906 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1907 return;
1909 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1910 import_export = -1;
1911 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1912 import_export = 1;
1913 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1914 && !flag_implicit_templates)
1915 /* For a template class, without -fimplicit-templates, check the
1916 repository. If the virtual table is assigned to this
1917 translation unit, then export the class; otherwise, import
1918 it. */
1919 import_export = repo_export_class_p (ctype) ? 1 : -1;
1920 else if (TYPE_POLYMORPHIC_P (ctype))
1922 /* The ABI specifies that the virtual table and associated
1923 information are emitted with the key method, if any. */
1924 tree method = CLASSTYPE_KEY_METHOD (ctype);
1925 /* If weak symbol support is not available, then we must be
1926 careful not to emit the vtable when the key function is
1927 inline. An inline function can be defined in multiple
1928 translation units. If we were to emit the vtable in each
1929 translation unit containing a definition, we would get
1930 multiple definition errors at link-time. */
1931 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1932 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1935 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1936 a definition anywhere else. */
1937 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1938 import_export = 0;
1940 /* Allow back ends the chance to overrule the decision. */
1941 if (targetm.cxx.import_export_class)
1942 import_export = targetm.cxx.import_export_class (ctype, import_export);
1944 if (import_export)
1946 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1947 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1951 /* Return true if VAR has already been provided to the back end; in that
1952 case VAR should not be modified further by the front end. */
1953 static bool
1954 var_finalized_p (tree var)
1956 return varpool_node::get_create (var)->definition;
1959 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1960 must be emitted in this translation unit. Mark it as such. */
1962 void
1963 mark_needed (tree decl)
1965 TREE_USED (decl) = 1;
1966 if (TREE_CODE (decl) == FUNCTION_DECL)
1968 /* Extern inline functions don't become needed when referenced.
1969 If we know a method will be emitted in other TU and no new
1970 functions can be marked reachable, just use the external
1971 definition. */
1972 struct cgraph_node *node = cgraph_node::get_create (decl);
1973 node->forced_by_abi = true;
1975 /* #pragma interface and -frepo code can call mark_needed for
1976 maybe-in-charge 'tors; mark the clones as well. */
1977 tree clone;
1978 FOR_EACH_CLONE (clone, decl)
1979 mark_needed (clone);
1981 else if (TREE_CODE (decl) == VAR_DECL)
1983 varpool_node *node = varpool_node::get_create (decl);
1984 /* C++ frontend use mark_decl_references to force COMDAT variables
1985 to be output that might appear dead otherwise. */
1986 node->forced_by_abi = true;
1990 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1991 returns true if a definition of this entity should be provided in
1992 this object file. Callers use this function to determine whether
1993 or not to let the back end know that a definition of DECL is
1994 available in this translation unit. */
1996 bool
1997 decl_needed_p (tree decl)
1999 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2000 /* This function should only be called at the end of the translation
2001 unit. We cannot be sure of whether or not something will be
2002 COMDAT until that point. */
2003 gcc_assert (at_eof);
2005 /* All entities with external linkage that are not COMDAT/EXTERN should be
2006 emitted; they may be referred to from other object files. */
2007 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2008 return true;
2009 /* Functions marked "dllexport" must be emitted so that they are
2010 visible to other DLLs. */
2011 if (flag_keep_inline_dllexport
2012 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2013 return true;
2015 /* When not optimizing, do not bother to produce definitions for extern
2016 symbols. */
2017 if (DECL_REALLY_EXTERN (decl)
2018 && ((TREE_CODE (decl) != FUNCTION_DECL
2019 && !optimize)
2020 || (TREE_CODE (decl) == FUNCTION_DECL
2021 && !opt_for_fn (decl, optimize)))
2022 && !lookup_attribute ("always_inline", decl))
2023 return false;
2025 /* If this entity was used, let the back end see it; it will decide
2026 whether or not to emit it into the object file. */
2027 if (TREE_USED (decl))
2028 return true;
2029 /* Virtual functions might be needed for devirtualization. */
2030 if (flag_devirtualize
2031 && TREE_CODE (decl) == FUNCTION_DECL
2032 && DECL_VIRTUAL_P (decl))
2033 return true;
2034 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2035 reference to DECL might cause it to be emitted later. */
2036 return false;
2039 /* If necessary, write out the vtables for the dynamic class CTYPE.
2040 Returns true if any vtables were emitted. */
2042 static bool
2043 maybe_emit_vtables (tree ctype)
2045 tree vtbl;
2046 tree primary_vtbl;
2047 int needed = 0;
2048 varpool_node *current = NULL, *last = NULL;
2050 /* If the vtables for this class have already been emitted there is
2051 nothing more to do. */
2052 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2053 if (var_finalized_p (primary_vtbl))
2054 return false;
2055 /* Ignore dummy vtables made by get_vtable_decl. */
2056 if (TREE_TYPE (primary_vtbl) == void_type_node)
2057 return false;
2059 /* On some targets, we cannot determine the key method until the end
2060 of the translation unit -- which is when this function is
2061 called. */
2062 if (!targetm.cxx.key_method_may_be_inline ())
2063 determine_key_method (ctype);
2065 /* See if any of the vtables are needed. */
2066 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2068 import_export_decl (vtbl);
2069 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2070 needed = 1;
2072 if (!needed)
2074 /* If the references to this class' vtables are optimized away,
2075 still emit the appropriate debugging information. See
2076 dfs_debug_mark. */
2077 if (DECL_COMDAT (primary_vtbl)
2078 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2079 note_debug_info_needed (ctype);
2080 return false;
2083 /* The ABI requires that we emit all of the vtables if we emit any
2084 of them. */
2085 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2087 /* Mark entities references from the virtual table as used. */
2088 mark_vtable_entries (vtbl);
2090 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2092 vec<tree, va_gc> *cleanups = NULL;
2093 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2094 LOOKUP_NORMAL);
2096 /* It had better be all done at compile-time. */
2097 gcc_assert (!expr && !cleanups);
2100 /* Write it out. */
2101 DECL_EXTERNAL (vtbl) = 0;
2102 rest_of_decl_compilation (vtbl, 1, 1);
2104 /* Because we're only doing syntax-checking, we'll never end up
2105 actually marking the variable as written. */
2106 if (flag_syntax_only)
2107 TREE_ASM_WRITTEN (vtbl) = 1;
2108 else if (DECL_ONE_ONLY (vtbl))
2110 current = varpool_node::get_create (vtbl);
2111 if (last)
2112 current->add_to_same_comdat_group (last);
2113 last = current;
2117 /* Since we're writing out the vtable here, also write the debug
2118 info. */
2119 note_debug_info_needed (ctype);
2121 return true;
2124 /* A special return value from type_visibility meaning internal
2125 linkage. */
2127 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2129 /* walk_tree helper function for type_visibility. */
2131 static tree
2132 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2134 int *vis_p = (int *)data;
2135 if (! TYPE_P (*tp))
2137 *walk_subtrees = 0;
2139 else if (OVERLOAD_TYPE_P (*tp)
2140 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2142 *vis_p = VISIBILITY_ANON;
2143 return *tp;
2145 else if (CLASS_TYPE_P (*tp)
2146 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2147 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2148 return NULL;
2151 /* Returns the visibility of TYPE, which is the minimum visibility of its
2152 component types. */
2154 static int
2155 type_visibility (tree type)
2157 int vis = VISIBILITY_DEFAULT;
2158 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2159 return vis;
2162 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2163 specified (or if VISIBILITY is static). If TMPL is true, this
2164 constraint is for a template argument, and takes precedence
2165 over explicitly-specified visibility on the template. */
2167 static void
2168 constrain_visibility (tree decl, int visibility, bool tmpl)
2170 if (visibility == VISIBILITY_ANON)
2172 /* extern "C" declarations aren't affected by the anonymous
2173 namespace. */
2174 if (!DECL_EXTERN_C_P (decl))
2176 TREE_PUBLIC (decl) = 0;
2177 DECL_WEAK (decl) = 0;
2178 DECL_COMMON (decl) = 0;
2179 DECL_COMDAT (decl) = false;
2180 if (TREE_CODE (decl) == FUNCTION_DECL
2181 || TREE_CODE (decl) == VAR_DECL)
2183 struct symtab_node *snode = symtab_node::get (decl);
2185 if (snode)
2186 snode->set_comdat_group (NULL);
2188 DECL_INTERFACE_KNOWN (decl) = 1;
2189 if (DECL_LANG_SPECIFIC (decl))
2190 DECL_NOT_REALLY_EXTERN (decl) = 1;
2193 else if (visibility > DECL_VISIBILITY (decl)
2194 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2196 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2197 /* This visibility was not specified. */
2198 DECL_VISIBILITY_SPECIFIED (decl) = false;
2202 /* Constrain the visibility of DECL based on the visibility of its template
2203 arguments. */
2205 static void
2206 constrain_visibility_for_template (tree decl, tree targs)
2208 /* If this is a template instantiation, check the innermost
2209 template args for visibility constraints. The outer template
2210 args are covered by the class check. */
2211 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2212 int i;
2213 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2215 int vis = 0;
2217 tree arg = TREE_VEC_ELT (args, i-1);
2218 if (TYPE_P (arg))
2219 vis = type_visibility (arg);
2220 else
2222 if (REFERENCE_REF_P (arg))
2223 arg = TREE_OPERAND (arg, 0);
2224 if (TREE_TYPE (arg))
2225 STRIP_NOPS (arg);
2226 if (TREE_CODE (arg) == ADDR_EXPR)
2227 arg = TREE_OPERAND (arg, 0);
2228 if (VAR_OR_FUNCTION_DECL_P (arg))
2230 if (! TREE_PUBLIC (arg))
2231 vis = VISIBILITY_ANON;
2232 else
2233 vis = DECL_VISIBILITY (arg);
2236 if (vis)
2237 constrain_visibility (decl, vis, true);
2241 /* Like c_determine_visibility, but with additional C++-specific
2242 behavior.
2244 Function-scope entities can rely on the function's visibility because
2245 it is set in start_preparsed_function.
2247 Class-scope entities cannot rely on the class's visibility until the end
2248 of the enclosing class definition.
2250 Note that because namespaces have multiple independent definitions,
2251 namespace visibility is handled elsewhere using the #pragma visibility
2252 machinery rather than by decorating the namespace declaration.
2254 The goal is for constraints from the type to give a diagnostic, and
2255 other constraints to be applied silently. */
2257 void
2258 determine_visibility (tree decl)
2260 tree class_type = NULL_TREE;
2261 bool use_template;
2262 bool orig_visibility_specified;
2263 enum symbol_visibility orig_visibility;
2265 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2267 /* Only relevant for names with external linkage. */
2268 if (!TREE_PUBLIC (decl))
2269 return;
2271 /* Cloned constructors and destructors get the same visibility as
2272 the underlying function. That should be set up in
2273 maybe_clone_body. */
2274 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2276 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2277 orig_visibility = DECL_VISIBILITY (decl);
2279 if (TREE_CODE (decl) == TYPE_DECL)
2281 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2282 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2283 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2284 use_template = 1;
2285 else
2286 use_template = 0;
2288 else if (DECL_LANG_SPECIFIC (decl))
2289 use_template = DECL_USE_TEMPLATE (decl);
2290 else
2291 use_template = 0;
2293 /* If DECL is a member of a class, visibility specifiers on the
2294 class can influence the visibility of the DECL. */
2295 if (DECL_CLASS_SCOPE_P (decl))
2296 class_type = DECL_CONTEXT (decl);
2297 else
2299 /* Not a class member. */
2301 /* Virtual tables have DECL_CONTEXT set to their associated class,
2302 so they are automatically handled above. */
2303 gcc_assert (!VAR_P (decl)
2304 || !DECL_VTABLE_OR_VTT_P (decl));
2306 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2308 /* Local statics and classes get the visibility of their
2309 containing function by default, except that
2310 -fvisibility-inlines-hidden doesn't affect them. */
2311 tree fn = DECL_CONTEXT (decl);
2312 if (DECL_VISIBILITY_SPECIFIED (fn))
2314 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2315 DECL_VISIBILITY_SPECIFIED (decl) =
2316 DECL_VISIBILITY_SPECIFIED (fn);
2318 else
2320 if (DECL_CLASS_SCOPE_P (fn))
2321 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2322 else if (determine_hidden_inline (fn))
2324 DECL_VISIBILITY (decl) = default_visibility;
2325 DECL_VISIBILITY_SPECIFIED (decl) =
2326 visibility_options.inpragma;
2328 else
2330 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2331 DECL_VISIBILITY_SPECIFIED (decl) =
2332 DECL_VISIBILITY_SPECIFIED (fn);
2336 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2337 but have no TEMPLATE_INFO, so don't try to check it. */
2338 use_template = 0;
2340 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2341 && flag_visibility_ms_compat)
2343 /* Under -fvisibility-ms-compat, types are visible by default,
2344 even though their contents aren't. */
2345 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2346 int underlying_vis = type_visibility (underlying_type);
2347 if (underlying_vis == VISIBILITY_ANON
2348 || (CLASS_TYPE_P (underlying_type)
2349 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2350 constrain_visibility (decl, underlying_vis, false);
2351 else
2352 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2354 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2356 /* tinfo visibility is based on the type it's for. */
2357 constrain_visibility
2358 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2360 /* Give the target a chance to override the visibility associated
2361 with DECL. */
2362 if (TREE_PUBLIC (decl)
2363 && !DECL_REALLY_EXTERN (decl)
2364 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2365 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2366 targetm.cxx.determine_class_data_visibility (decl);
2368 else if (use_template)
2369 /* Template instantiations and specializations get visibility based
2370 on their template unless they override it with an attribute. */;
2371 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2373 if (determine_hidden_inline (decl))
2374 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2375 else
2377 /* Set default visibility to whatever the user supplied with
2378 #pragma GCC visibility or a namespace visibility attribute. */
2379 DECL_VISIBILITY (decl) = default_visibility;
2380 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2385 if (use_template)
2387 /* If the specialization doesn't specify visibility, use the
2388 visibility from the template. */
2389 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2390 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2391 : DECL_TEMPLATE_INFO (decl));
2392 tree args = TI_ARGS (tinfo);
2393 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2394 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2395 : DECL_ATTRIBUTES (decl));
2397 if (args != error_mark_node)
2399 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2401 if (!DECL_VISIBILITY_SPECIFIED (decl))
2403 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2404 && determine_hidden_inline (decl))
2405 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2406 else
2408 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2409 DECL_VISIBILITY_SPECIFIED (decl)
2410 = DECL_VISIBILITY_SPECIFIED (pattern);
2414 if (args
2415 /* Template argument visibility outweighs #pragma or namespace
2416 visibility, but not an explicit attribute. */
2417 && !lookup_attribute ("visibility", attribs))
2419 int depth = TMPL_ARGS_DEPTH (args);
2420 if (DECL_VISIBILITY_SPECIFIED (decl))
2422 /* A class template member with explicit visibility
2423 overrides the class visibility, so we need to apply
2424 all the levels of template args directly. */
2425 int i;
2426 for (i = 1; i <= depth; ++i)
2428 tree lev = TMPL_ARGS_LEVEL (args, i);
2429 constrain_visibility_for_template (decl, lev);
2432 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2433 /* Limit visibility based on its template arguments. */
2434 constrain_visibility_for_template (decl, args);
2439 if (class_type)
2440 determine_visibility_from_class (decl, class_type);
2442 if (decl_anon_ns_mem_p (decl))
2443 /* Names in an anonymous namespace get internal linkage.
2444 This might change once we implement export. */
2445 constrain_visibility (decl, VISIBILITY_ANON, false);
2446 else if (TREE_CODE (decl) != TYPE_DECL)
2448 /* Propagate anonymity from type to decl. */
2449 int tvis = type_visibility (TREE_TYPE (decl));
2450 if (tvis == VISIBILITY_ANON
2451 || ! DECL_VISIBILITY_SPECIFIED (decl))
2452 constrain_visibility (decl, tvis, false);
2454 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2455 /* DR 757: A type without linkage shall not be used as the type of a
2456 variable or function with linkage, unless
2457 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2458 o the variable or function is not used (3.2 [basic.def.odr]) or is
2459 defined in the same translation unit.
2461 Since non-extern "C" decls need to be defined in the same
2462 translation unit, we can make the type internal. */
2463 constrain_visibility (decl, VISIBILITY_ANON, false);
2465 /* If visibility changed and DECL already has DECL_RTL, ensure
2466 symbol flags are updated. */
2467 if ((DECL_VISIBILITY (decl) != orig_visibility
2468 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2469 && ((VAR_P (decl) && TREE_STATIC (decl))
2470 || TREE_CODE (decl) == FUNCTION_DECL)
2471 && DECL_RTL_SET_P (decl))
2472 make_decl_rtl (decl);
2475 /* By default, static data members and function members receive
2476 the visibility of their containing class. */
2478 static void
2479 determine_visibility_from_class (tree decl, tree class_type)
2481 if (DECL_VISIBILITY_SPECIFIED (decl))
2482 return;
2484 if (determine_hidden_inline (decl))
2485 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2486 else
2488 /* Default to the class visibility. */
2489 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2490 DECL_VISIBILITY_SPECIFIED (decl)
2491 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2494 /* Give the target a chance to override the visibility associated
2495 with DECL. */
2496 if (VAR_P (decl)
2497 && (DECL_TINFO_P (decl)
2498 || (DECL_VTABLE_OR_VTT_P (decl)
2499 /* Construction virtual tables are not exported because
2500 they cannot be referred to from other object files;
2501 their name is not standardized by the ABI. */
2502 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2503 && TREE_PUBLIC (decl)
2504 && !DECL_REALLY_EXTERN (decl)
2505 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2506 targetm.cxx.determine_class_data_visibility (decl);
2509 /* Returns true iff DECL is an inline that should get hidden visibility
2510 because of -fvisibility-inlines-hidden. */
2512 static bool
2513 determine_hidden_inline (tree decl)
2515 return (visibility_options.inlines_hidden
2516 /* Don't do this for inline templates; specializations might not be
2517 inline, and we don't want them to inherit the hidden
2518 visibility. We'll set it here for all inline instantiations. */
2519 && !processing_template_decl
2520 && TREE_CODE (decl) == FUNCTION_DECL
2521 && DECL_DECLARED_INLINE_P (decl)
2522 && (! DECL_LANG_SPECIFIC (decl)
2523 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2526 /* Constrain the visibility of a class TYPE based on the visibility of its
2527 field types. Warn if any fields require lesser visibility. */
2529 void
2530 constrain_class_visibility (tree type)
2532 tree binfo;
2533 tree t;
2534 int i;
2536 int vis = type_visibility (type);
2538 if (vis == VISIBILITY_ANON
2539 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2540 return;
2542 /* Don't warn about visibility if the class has explicit visibility. */
2543 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2544 vis = VISIBILITY_INTERNAL;
2546 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2547 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2549 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2550 int subvis = type_visibility (ftype);
2552 if (subvis == VISIBILITY_ANON)
2554 if (!in_main_input_context ())
2555 warning (0, "\
2556 %qT has a field %qD whose type uses the anonymous namespace",
2557 type, t);
2559 else if (MAYBE_CLASS_TYPE_P (ftype)
2560 && vis < VISIBILITY_HIDDEN
2561 && subvis >= VISIBILITY_HIDDEN)
2562 warning (OPT_Wattributes, "\
2563 %qT declared with greater visibility than the type of its field %qD",
2564 type, t);
2567 binfo = TYPE_BINFO (type);
2568 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2570 int subvis = type_visibility (TREE_TYPE (t));
2572 if (subvis == VISIBILITY_ANON)
2574 if (!in_main_input_context())
2575 warning (0, "\
2576 %qT has a base %qT whose type uses the anonymous namespace",
2577 type, TREE_TYPE (t));
2579 else if (vis < VISIBILITY_HIDDEN
2580 && subvis >= VISIBILITY_HIDDEN)
2581 warning (OPT_Wattributes, "\
2582 %qT declared with greater visibility than its base %qT",
2583 type, TREE_TYPE (t));
2587 /* Functions for adjusting the visibility of a tagged type and its nested
2588 types and declarations when it gets a name for linkage purposes from a
2589 typedef. */
2591 static void bt_reset_linkage_1 (binding_entry, void *);
2592 static void bt_reset_linkage_2 (binding_entry, void *);
2594 /* First reset the visibility of all the types. */
2596 static void
2597 reset_type_linkage_1 (tree type)
2599 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2600 if (CLASS_TYPE_P (type))
2601 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2602 bt_reset_linkage_1, NULL);
2604 static void
2605 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2607 reset_type_linkage_1 (b->type);
2610 /* Then reset the visibility of any static data members or member
2611 functions that use those types. */
2613 static void
2614 reset_decl_linkage (tree decl)
2616 if (TREE_PUBLIC (decl))
2617 return;
2618 if (DECL_CLONED_FUNCTION_P (decl))
2619 return;
2620 TREE_PUBLIC (decl) = true;
2621 DECL_INTERFACE_KNOWN (decl) = false;
2622 determine_visibility (decl);
2623 tentative_decl_linkage (decl);
2625 static void
2626 reset_type_linkage_2 (tree type)
2628 if (CLASS_TYPE_P (type))
2630 if (tree vt = CLASSTYPE_VTABLES (type))
2632 tree name = mangle_vtbl_for_type (type);
2633 DECL_NAME (vt) = name;
2634 SET_DECL_ASSEMBLER_NAME (vt, name);
2635 reset_decl_linkage (vt);
2637 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2639 tree name = mangle_typeinfo_for_type (type);
2640 DECL_NAME (ti) = name;
2641 SET_DECL_ASSEMBLER_NAME (ti, name);
2642 TREE_TYPE (name) = type;
2643 reset_decl_linkage (ti);
2645 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2646 if (TREE_CODE (m) == VAR_DECL)
2647 reset_decl_linkage (m);
2648 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2649 reset_decl_linkage (m);
2650 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2651 bt_reset_linkage_2, NULL);
2654 static void
2655 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2657 reset_type_linkage_2 (b->type);
2659 void
2660 reset_type_linkage (tree type)
2662 reset_type_linkage_1 (type);
2663 reset_type_linkage_2 (type);
2666 /* Set up our initial idea of what the linkage of DECL should be. */
2668 void
2669 tentative_decl_linkage (tree decl)
2671 if (DECL_INTERFACE_KNOWN (decl))
2672 /* We've already made a decision as to how this function will
2673 be handled. */;
2674 else if (vague_linkage_p (decl))
2676 if (TREE_CODE (decl) == FUNCTION_DECL
2677 && decl_defined_p (decl))
2679 DECL_EXTERNAL (decl) = 1;
2680 DECL_NOT_REALLY_EXTERN (decl) = 1;
2681 note_vague_linkage_fn (decl);
2682 /* A non-template inline function with external linkage will
2683 always be COMDAT. As we must eventually determine the
2684 linkage of all functions, and as that causes writes to
2685 the data mapped in from the PCH file, it's advantageous
2686 to mark the functions at this point. */
2687 if (DECL_DECLARED_INLINE_P (decl)
2688 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2689 || DECL_DEFAULTED_FN (decl)))
2691 /* This function must have external linkage, as
2692 otherwise DECL_INTERFACE_KNOWN would have been
2693 set. */
2694 gcc_assert (TREE_PUBLIC (decl));
2695 comdat_linkage (decl);
2696 DECL_INTERFACE_KNOWN (decl) = 1;
2699 else if (TREE_CODE (decl) == VAR_DECL)
2700 maybe_commonize_var (decl);
2704 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2705 for DECL has not already been determined, do so now by setting
2706 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2707 function is called entities with vague linkage whose definitions
2708 are available must have TREE_PUBLIC set.
2710 If this function decides to place DECL in COMDAT, it will set
2711 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2712 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2713 callers defer that decision until it is clear that DECL is actually
2714 required. */
2716 void
2717 import_export_decl (tree decl)
2719 int emit_p;
2720 bool comdat_p;
2721 bool import_p;
2722 tree class_type = NULL_TREE;
2724 if (DECL_INTERFACE_KNOWN (decl))
2725 return;
2727 /* We cannot determine what linkage to give to an entity with vague
2728 linkage until the end of the file. For example, a virtual table
2729 for a class will be defined if and only if the key method is
2730 defined in this translation unit. As a further example, consider
2731 that when compiling a translation unit that uses PCH file with
2732 "-frepo" it would be incorrect to make decisions about what
2733 entities to emit when building the PCH; those decisions must be
2734 delayed until the repository information has been processed. */
2735 gcc_assert (at_eof);
2736 /* Object file linkage for explicit instantiations is handled in
2737 mark_decl_instantiated. For static variables in functions with
2738 vague linkage, maybe_commonize_var is used.
2740 Therefore, the only declarations that should be provided to this
2741 function are those with external linkage that are:
2743 * implicit instantiations of function templates
2745 * inline function
2747 * implicit instantiations of static data members of class
2748 templates
2750 * virtual tables
2752 * typeinfo objects
2754 Furthermore, all entities that reach this point must have a
2755 definition available in this translation unit.
2757 The following assertions check these conditions. */
2758 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2759 /* Any code that creates entities with TREE_PUBLIC cleared should
2760 also set DECL_INTERFACE_KNOWN. */
2761 gcc_assert (TREE_PUBLIC (decl));
2762 if (TREE_CODE (decl) == FUNCTION_DECL)
2763 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2764 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2765 || DECL_DECLARED_INLINE_P (decl));
2766 else
2767 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2768 || DECL_VTABLE_OR_VTT_P (decl)
2769 || DECL_TINFO_P (decl));
2770 /* Check that a definition of DECL is available in this translation
2771 unit. */
2772 gcc_assert (!DECL_REALLY_EXTERN (decl));
2774 /* Assume that DECL will not have COMDAT linkage. */
2775 comdat_p = false;
2776 /* Assume that DECL will not be imported into this translation
2777 unit. */
2778 import_p = false;
2780 /* See if the repository tells us whether or not to emit DECL in
2781 this translation unit. */
2782 emit_p = repo_emit_p (decl);
2783 if (emit_p == 0)
2784 import_p = true;
2785 else if (emit_p == 1)
2787 /* The repository indicates that this entity should be defined
2788 here. Make sure the back end honors that request. */
2789 mark_needed (decl);
2790 /* Output the definition as an ordinary strong definition. */
2791 DECL_EXTERNAL (decl) = 0;
2792 DECL_INTERFACE_KNOWN (decl) = 1;
2793 return;
2796 if (import_p)
2797 /* We have already decided what to do with this DECL; there is no
2798 need to check anything further. */
2800 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2802 class_type = DECL_CONTEXT (decl);
2803 import_export_class (class_type);
2804 if (TYPE_FOR_JAVA (class_type))
2805 import_p = true;
2806 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2807 && CLASSTYPE_INTERFACE_ONLY (class_type))
2808 import_p = true;
2809 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2810 && !CLASSTYPE_USE_TEMPLATE (class_type)
2811 && CLASSTYPE_KEY_METHOD (class_type)
2812 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2813 /* The ABI requires that all virtual tables be emitted with
2814 COMDAT linkage. However, on systems where COMDAT symbols
2815 don't show up in the table of contents for a static
2816 archive, or on systems without weak symbols (where we
2817 approximate COMDAT linkage by using internal linkage), the
2818 linker will report errors about undefined symbols because
2819 it will not see the virtual table definition. Therefore,
2820 in the case that we know that the virtual table will be
2821 emitted in only one translation unit, we make the virtual
2822 table an ordinary definition with external linkage. */
2823 DECL_EXTERNAL (decl) = 0;
2824 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2826 /* CLASS_TYPE is being exported from this translation unit,
2827 so DECL should be defined here. */
2828 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2829 /* If a class is declared in a header with the "extern
2830 template" extension, then it will not be instantiated,
2831 even in translation units that would normally require
2832 it. Often such classes are explicitly instantiated in
2833 one translation unit. Therefore, the explicit
2834 instantiation must be made visible to other translation
2835 units. */
2836 DECL_EXTERNAL (decl) = 0;
2837 else
2839 /* The generic C++ ABI says that class data is always
2840 COMDAT, even if there is a key function. Some
2841 variants (e.g., the ARM EABI) says that class data
2842 only has COMDAT linkage if the class data might be
2843 emitted in more than one translation unit. When the
2844 key method can be inline and is inline, we still have
2845 to arrange for comdat even though
2846 class_data_always_comdat is false. */
2847 if (!CLASSTYPE_KEY_METHOD (class_type)
2848 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2849 || targetm.cxx.class_data_always_comdat ())
2851 /* The ABI requires COMDAT linkage. Normally, we
2852 only emit COMDAT things when they are needed;
2853 make sure that we realize that this entity is
2854 indeed needed. */
2855 comdat_p = true;
2856 mark_needed (decl);
2860 else if (!flag_implicit_templates
2861 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2862 import_p = true;
2863 else
2864 comdat_p = true;
2866 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2868 tree type = TREE_TYPE (DECL_NAME (decl));
2869 if (CLASS_TYPE_P (type))
2871 class_type = type;
2872 import_export_class (type);
2873 if (CLASSTYPE_INTERFACE_KNOWN (type)
2874 && TYPE_POLYMORPHIC_P (type)
2875 && CLASSTYPE_INTERFACE_ONLY (type)
2876 /* If -fno-rtti was specified, then we cannot be sure
2877 that RTTI information will be emitted with the
2878 virtual table of the class, so we must emit it
2879 wherever it is used. */
2880 && flag_rtti)
2881 import_p = true;
2882 else
2884 if (CLASSTYPE_INTERFACE_KNOWN (type)
2885 && !CLASSTYPE_INTERFACE_ONLY (type))
2887 comdat_p = (targetm.cxx.class_data_always_comdat ()
2888 || (CLASSTYPE_KEY_METHOD (type)
2889 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2890 mark_needed (decl);
2891 if (!flag_weak)
2893 comdat_p = false;
2894 DECL_EXTERNAL (decl) = 0;
2897 else
2898 comdat_p = true;
2901 else
2902 comdat_p = true;
2904 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2906 /* DECL is an implicit instantiation of a function or static
2907 data member. */
2908 if ((flag_implicit_templates
2909 && !flag_use_repository)
2910 || (flag_implicit_inline_templates
2911 && TREE_CODE (decl) == FUNCTION_DECL
2912 && DECL_DECLARED_INLINE_P (decl)))
2913 comdat_p = true;
2914 else
2915 /* If we are not implicitly generating templates, then mark
2916 this entity as undefined in this translation unit. */
2917 import_p = true;
2919 else if (DECL_FUNCTION_MEMBER_P (decl))
2921 if (!DECL_DECLARED_INLINE_P (decl))
2923 tree ctype = DECL_CONTEXT (decl);
2924 import_export_class (ctype);
2925 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2927 DECL_NOT_REALLY_EXTERN (decl)
2928 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2929 || (DECL_DECLARED_INLINE_P (decl)
2930 && ! flag_implement_inlines
2931 && !DECL_VINDEX (decl)));
2933 if (!DECL_NOT_REALLY_EXTERN (decl))
2934 DECL_EXTERNAL (decl) = 1;
2936 /* Always make artificials weak. */
2937 if (DECL_ARTIFICIAL (decl) && flag_weak)
2938 comdat_p = true;
2939 else
2940 maybe_make_one_only (decl);
2943 else
2944 comdat_p = true;
2946 else
2947 comdat_p = true;
2949 if (import_p)
2951 /* If we are importing DECL into this translation unit, mark is
2952 an undefined here. */
2953 DECL_EXTERNAL (decl) = 1;
2954 DECL_NOT_REALLY_EXTERN (decl) = 0;
2956 else if (comdat_p)
2958 /* If we decided to put DECL in COMDAT, mark it accordingly at
2959 this point. */
2960 comdat_linkage (decl);
2963 DECL_INTERFACE_KNOWN (decl) = 1;
2966 /* Return an expression that performs the destruction of DECL, which
2967 must be a VAR_DECL whose type has a non-trivial destructor, or is
2968 an array whose (innermost) elements have a non-trivial destructor. */
2970 tree
2971 build_cleanup (tree decl)
2973 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2974 gcc_assert (clean != NULL_TREE);
2975 return clean;
2978 /* Returns the initialization guard variable for the variable DECL,
2979 which has static storage duration. */
2981 tree
2982 get_guard (tree decl)
2984 tree sname;
2985 tree guard;
2987 sname = mangle_guard_variable (decl);
2988 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2989 if (! guard)
2991 tree guard_type;
2993 /* We use a type that is big enough to contain a mutex as well
2994 as an integer counter. */
2995 guard_type = targetm.cxx.guard_type ();
2996 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2997 VAR_DECL, sname, guard_type);
2999 /* The guard should have the same linkage as what it guards. */
3000 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3001 TREE_STATIC (guard) = TREE_STATIC (decl);
3002 DECL_COMMON (guard) = DECL_COMMON (decl);
3003 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3004 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3005 if (DECL_ONE_ONLY (decl))
3006 make_decl_one_only (guard, cxx_comdat_group (guard));
3007 if (TREE_PUBLIC (decl))
3008 DECL_WEAK (guard) = DECL_WEAK (decl);
3009 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3010 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3012 DECL_ARTIFICIAL (guard) = 1;
3013 DECL_IGNORED_P (guard) = 1;
3014 TREE_USED (guard) = 1;
3015 pushdecl_top_level_and_finish (guard, NULL_TREE);
3017 return guard;
3020 /* Return those bits of the GUARD variable that should be set when the
3021 guarded entity is actually initialized. */
3023 static tree
3024 get_guard_bits (tree guard)
3026 if (!targetm.cxx.guard_mask_bit ())
3028 /* We only set the first byte of the guard, in order to leave room
3029 for a mutex in the high-order bits. */
3030 guard = build1 (ADDR_EXPR,
3031 build_pointer_type (TREE_TYPE (guard)),
3032 guard);
3033 guard = build1 (NOP_EXPR,
3034 build_pointer_type (char_type_node),
3035 guard);
3036 guard = build1 (INDIRECT_REF, char_type_node, guard);
3039 return guard;
3042 /* Return an expression which determines whether or not the GUARD
3043 variable has already been initialized. */
3045 tree
3046 get_guard_cond (tree guard)
3048 tree guard_value;
3050 /* Check to see if the GUARD is zero. */
3051 guard = get_guard_bits (guard);
3053 /* Mask off all but the low bit. */
3054 if (targetm.cxx.guard_mask_bit ())
3056 guard_value = integer_one_node;
3057 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3058 guard_value = convert (TREE_TYPE (guard), guard_value);
3059 guard = cp_build_binary_op (input_location,
3060 BIT_AND_EXPR, guard, guard_value,
3061 tf_warning_or_error);
3064 guard_value = integer_zero_node;
3065 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3066 guard_value = convert (TREE_TYPE (guard), guard_value);
3067 return cp_build_binary_op (input_location,
3068 EQ_EXPR, guard, guard_value,
3069 tf_warning_or_error);
3072 /* Return an expression which sets the GUARD variable, indicating that
3073 the variable being guarded has been initialized. */
3075 tree
3076 set_guard (tree guard)
3078 tree guard_init;
3080 /* Set the GUARD to one. */
3081 guard = get_guard_bits (guard);
3082 guard_init = integer_one_node;
3083 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3084 guard_init = convert (TREE_TYPE (guard), guard_init);
3085 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3086 tf_warning_or_error);
3089 /* Returns true iff we can tell that VAR does not have a dynamic
3090 initializer. */
3092 static bool
3093 var_defined_without_dynamic_init (tree var)
3095 /* If it's defined in another TU, we can't tell. */
3096 if (DECL_EXTERNAL (var))
3097 return false;
3098 /* If it has a non-trivial destructor, registering the destructor
3099 counts as dynamic initialization. */
3100 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3101 return false;
3102 /* If it's in this TU, its initializer has been processed, unless
3103 it's a case of self-initialization, then DECL_INITIALIZED_P is
3104 false while the initializer is handled by finish_id_expression. */
3105 if (!DECL_INITIALIZED_P (var))
3106 return false;
3107 /* If it has no initializer or a constant one, it's not dynamic. */
3108 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3109 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3112 /* Returns true iff VAR is a variable that needs uses to be
3113 wrapped for possible dynamic initialization. */
3115 static bool
3116 var_needs_tls_wrapper (tree var)
3118 return (!error_operand_p (var)
3119 && DECL_THREAD_LOCAL_P (var)
3120 && !DECL_GNU_TLS_P (var)
3121 && !DECL_FUNCTION_SCOPE_P (var)
3122 && !var_defined_without_dynamic_init (var));
3125 /* Get the FUNCTION_DECL for the shared TLS init function for this
3126 translation unit. */
3128 static tree
3129 get_local_tls_init_fn (void)
3131 tree sname = get_identifier ("__tls_init");
3132 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3133 if (!fn)
3135 fn = build_lang_decl (FUNCTION_DECL, sname,
3136 build_function_type (void_type_node,
3137 void_list_node));
3138 SET_DECL_LANGUAGE (fn, lang_c);
3139 TREE_PUBLIC (fn) = false;
3140 DECL_ARTIFICIAL (fn) = true;
3141 mark_used (fn);
3142 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3144 return fn;
3147 /* Get a FUNCTION_DECL for the init function for the thread_local
3148 variable VAR. The init function will be an alias to the function
3149 that initializes all the non-local TLS variables in the translation
3150 unit. The init function is only used by the wrapper function. */
3152 static tree
3153 get_tls_init_fn (tree var)
3155 /* Only C++11 TLS vars need this init fn. */
3156 if (!var_needs_tls_wrapper (var))
3157 return NULL_TREE;
3159 /* If -fno-extern-tls-init, assume that we don't need to call
3160 a tls init function for a variable defined in another TU. */
3161 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3162 return NULL_TREE;
3164 #ifdef ASM_OUTPUT_DEF
3165 /* If the variable is internal, or if we can't generate aliases,
3166 call the local init function directly. */
3167 if (!TREE_PUBLIC (var))
3168 #endif
3169 return get_local_tls_init_fn ();
3171 tree sname = mangle_tls_init_fn (var);
3172 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3173 if (!fn)
3175 fn = build_lang_decl (FUNCTION_DECL, sname,
3176 build_function_type (void_type_node,
3177 void_list_node));
3178 SET_DECL_LANGUAGE (fn, lang_c);
3179 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3180 DECL_ARTIFICIAL (fn) = true;
3181 DECL_COMDAT (fn) = DECL_COMDAT (var);
3182 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3183 if (DECL_ONE_ONLY (var))
3184 make_decl_one_only (fn, cxx_comdat_group (fn));
3185 if (TREE_PUBLIC (var))
3187 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3188 /* If the variable is defined somewhere else and might have static
3189 initialization, make the init function a weak reference. */
3190 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3191 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3192 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3193 && DECL_EXTERNAL (var))
3194 declare_weak (fn);
3195 else
3196 DECL_WEAK (fn) = DECL_WEAK (var);
3198 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3199 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3200 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3201 DECL_IGNORED_P (fn) = 1;
3202 mark_used (fn);
3204 DECL_BEFRIENDING_CLASSES (fn) = var;
3206 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3208 return fn;
3211 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3212 variable VAR. The wrapper function calls the init function (if any) for
3213 VAR and then returns a reference to VAR. The wrapper function is used
3214 in place of VAR everywhere VAR is mentioned. */
3216 tree
3217 get_tls_wrapper_fn (tree var)
3219 /* Only C++11 TLS vars need this wrapper fn. */
3220 if (!var_needs_tls_wrapper (var))
3221 return NULL_TREE;
3223 tree sname = mangle_tls_wrapper_fn (var);
3224 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3225 if (!fn)
3227 /* A named rvalue reference is an lvalue, so the wrapper should
3228 always return an lvalue reference. */
3229 tree type = non_reference (TREE_TYPE (var));
3230 type = build_reference_type (type);
3231 tree fntype = build_function_type (type, void_list_node);
3232 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3233 SET_DECL_LANGUAGE (fn, lang_c);
3234 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3235 DECL_ARTIFICIAL (fn) = true;
3236 DECL_IGNORED_P (fn) = 1;
3237 /* The wrapper is inline and emitted everywhere var is used. */
3238 DECL_DECLARED_INLINE_P (fn) = true;
3239 if (TREE_PUBLIC (var))
3241 comdat_linkage (fn);
3242 #ifdef HAVE_GAS_HIDDEN
3243 /* Make the wrapper bind locally; there's no reason to share
3244 the wrapper between multiple shared objects. */
3245 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3246 DECL_VISIBILITY_SPECIFIED (fn) = true;
3247 #endif
3249 if (!TREE_PUBLIC (fn))
3250 DECL_INTERFACE_KNOWN (fn) = true;
3251 mark_used (fn);
3252 note_vague_linkage_fn (fn);
3254 #if 0
3255 /* We want CSE to commonize calls to the wrapper, but marking it as
3256 pure is unsafe since it has side-effects. I guess we need a new
3257 ECF flag even weaker than ECF_PURE. FIXME! */
3258 DECL_PURE_P (fn) = true;
3259 #endif
3261 DECL_BEFRIENDING_CLASSES (fn) = var;
3263 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3265 return fn;
3268 /* At EOF, generate the definition for the TLS wrapper function FN:
3270 T& var_wrapper() {
3271 if (init_fn) init_fn();
3272 return var;
3273 } */
3275 static void
3276 generate_tls_wrapper (tree fn)
3278 tree var = DECL_BEFRIENDING_CLASSES (fn);
3280 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3281 tree body = begin_function_body ();
3282 /* Only call the init fn if there might be one. */
3283 if (tree init_fn = get_tls_init_fn (var))
3285 tree if_stmt = NULL_TREE;
3286 /* If init_fn is a weakref, make sure it exists before calling. */
3287 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3289 if_stmt = begin_if_stmt ();
3290 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3291 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3292 NE_EXPR, addr, nullptr_node,
3293 tf_warning_or_error);
3294 finish_if_stmt_cond (cond, if_stmt);
3296 finish_expr_stmt (build_cxx_call
3297 (init_fn, 0, NULL, tf_warning_or_error));
3298 if (if_stmt)
3300 finish_then_clause (if_stmt);
3301 finish_if_stmt (if_stmt);
3304 else
3305 /* If there's no initialization, the wrapper is a constant function. */
3306 TREE_READONLY (fn) = true;
3307 finish_return_stmt (convert_from_reference (var));
3308 finish_function_body (body);
3309 expand_or_defer_fn (finish_function (0));
3312 /* Start the process of running a particular set of global constructors
3313 or destructors. Subroutine of do_[cd]tors. Also called from
3314 vtv_start_verification_constructor_init_function. */
3316 static tree
3317 start_objects (int method_type, int initp)
3319 tree body;
3320 tree fndecl;
3321 char type[14];
3323 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3325 if (initp != DEFAULT_INIT_PRIORITY)
3327 char joiner;
3329 #ifdef JOINER
3330 joiner = JOINER;
3331 #else
3332 joiner = '_';
3333 #endif
3335 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3337 else
3338 sprintf (type, "sub_%c", method_type);
3340 fndecl = build_lang_decl (FUNCTION_DECL,
3341 get_file_function_name (type),
3342 build_function_type_list (void_type_node,
3343 NULL_TREE));
3344 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3346 TREE_PUBLIC (current_function_decl) = 0;
3348 /* Mark as artificial because it's not explicitly in the user's
3349 source code. */
3350 DECL_ARTIFICIAL (current_function_decl) = 1;
3352 /* Mark this declaration as used to avoid spurious warnings. */
3353 TREE_USED (current_function_decl) = 1;
3355 /* Mark this function as a global constructor or destructor. */
3356 if (method_type == 'I')
3357 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3358 else
3359 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3361 body = begin_compound_stmt (BCS_FN_BODY);
3363 return body;
3366 /* Finish the process of running a particular set of global constructors
3367 or destructors. Subroutine of do_[cd]tors. */
3369 static void
3370 finish_objects (int method_type, int initp, tree body)
3372 tree fn;
3374 /* Finish up. */
3375 finish_compound_stmt (body);
3376 fn = finish_function (0);
3378 if (method_type == 'I')
3380 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3381 decl_init_priority_insert (fn, initp);
3383 else
3385 DECL_STATIC_DESTRUCTOR (fn) = 1;
3386 decl_fini_priority_insert (fn, initp);
3389 expand_or_defer_fn (fn);
3392 /* The names of the parameters to the function created to handle
3393 initializations and destructions for objects with static storage
3394 duration. */
3395 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3396 #define PRIORITY_IDENTIFIER "__priority"
3398 /* The name of the function we create to handle initializations and
3399 destructions for objects with static storage duration. */
3400 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3402 /* The declaration for the __INITIALIZE_P argument. */
3403 static GTY(()) tree initialize_p_decl;
3405 /* The declaration for the __PRIORITY argument. */
3406 static GTY(()) tree priority_decl;
3408 /* The declaration for the static storage duration function. */
3409 static GTY(()) tree ssdf_decl;
3411 /* All the static storage duration functions created in this
3412 translation unit. */
3413 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3415 /* A map from priority levels to information about that priority
3416 level. There may be many such levels, so efficient lookup is
3417 important. */
3418 static splay_tree priority_info_map;
3420 /* Begins the generation of the function that will handle all
3421 initialization and destruction of objects with static storage
3422 duration. The function generated takes two parameters of type
3423 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3424 nonzero, it performs initializations. Otherwise, it performs
3425 destructions. It only performs those initializations or
3426 destructions with the indicated __PRIORITY. The generated function
3427 returns no value.
3429 It is assumed that this function will only be called once per
3430 translation unit. */
3432 static tree
3433 start_static_storage_duration_function (unsigned count)
3435 tree type;
3436 tree body;
3437 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3439 /* Create the identifier for this function. It will be of the form
3440 SSDF_IDENTIFIER_<number>. */
3441 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3443 type = build_function_type_list (void_type_node,
3444 integer_type_node, integer_type_node,
3445 NULL_TREE);
3447 /* Create the FUNCTION_DECL itself. */
3448 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3449 get_identifier (id),
3450 type);
3451 TREE_PUBLIC (ssdf_decl) = 0;
3452 DECL_ARTIFICIAL (ssdf_decl) = 1;
3454 /* Put this function in the list of functions to be called from the
3455 static constructors and destructors. */
3456 if (!ssdf_decls)
3458 vec_alloc (ssdf_decls, 32);
3460 /* Take this opportunity to initialize the map from priority
3461 numbers to information about that priority level. */
3462 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3463 /*delete_key_fn=*/0,
3464 /*delete_value_fn=*/
3465 (splay_tree_delete_value_fn) &free);
3467 /* We always need to generate functions for the
3468 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3469 priorities later, we'll be sure to find the
3470 DEFAULT_INIT_PRIORITY. */
3471 get_priority_info (DEFAULT_INIT_PRIORITY);
3474 vec_safe_push (ssdf_decls, ssdf_decl);
3476 /* Create the argument list. */
3477 initialize_p_decl = cp_build_parm_decl
3478 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3479 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3480 TREE_USED (initialize_p_decl) = 1;
3481 priority_decl = cp_build_parm_decl
3482 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3483 DECL_CONTEXT (priority_decl) = ssdf_decl;
3484 TREE_USED (priority_decl) = 1;
3486 DECL_CHAIN (initialize_p_decl) = priority_decl;
3487 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3489 /* Put the function in the global scope. */
3490 pushdecl (ssdf_decl);
3492 /* Start the function itself. This is equivalent to declaring the
3493 function as:
3495 static void __ssdf (int __initialize_p, init __priority_p);
3497 It is static because we only need to call this function from the
3498 various constructor and destructor functions for this module. */
3499 start_preparsed_function (ssdf_decl,
3500 /*attrs=*/NULL_TREE,
3501 SF_PRE_PARSED);
3503 /* Set up the scope of the outermost block in the function. */
3504 body = begin_compound_stmt (BCS_FN_BODY);
3506 return body;
3509 /* Finish the generation of the function which performs initialization
3510 and destruction of objects with static storage duration. After
3511 this point, no more such objects can be created. */
3513 static void
3514 finish_static_storage_duration_function (tree body)
3516 /* Close out the function. */
3517 finish_compound_stmt (body);
3518 expand_or_defer_fn (finish_function (0));
3521 /* Return the information about the indicated PRIORITY level. If no
3522 code to handle this level has yet been generated, generate the
3523 appropriate prologue. */
3525 static priority_info
3526 get_priority_info (int priority)
3528 priority_info pi;
3529 splay_tree_node n;
3531 n = splay_tree_lookup (priority_info_map,
3532 (splay_tree_key) priority);
3533 if (!n)
3535 /* Create a new priority information structure, and insert it
3536 into the map. */
3537 pi = XNEW (struct priority_info_s);
3538 pi->initializations_p = 0;
3539 pi->destructions_p = 0;
3540 splay_tree_insert (priority_info_map,
3541 (splay_tree_key) priority,
3542 (splay_tree_value) pi);
3544 else
3545 pi = (priority_info) n->value;
3547 return pi;
3550 /* The effective initialization priority of a DECL. */
3552 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3553 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3554 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3556 /* Whether a DECL needs a guard to protect it against multiple
3557 initialization. */
3559 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3560 || DECL_ONE_ONLY (decl) \
3561 || DECL_WEAK (decl)))
3563 /* Called from one_static_initialization_or_destruction(),
3564 via walk_tree.
3565 Walks the initializer list of a global variable and looks for
3566 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3567 and that have their DECL_CONTEXT() == NULL.
3568 For each such temporary variable, set their DECL_CONTEXT() to
3569 the current function. This is necessary because otherwise
3570 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3571 when trying to refer to a temporary variable that does not have
3572 it's DECL_CONTECT() properly set. */
3573 static tree
3574 fix_temporary_vars_context_r (tree *node,
3575 int * /*unused*/,
3576 void * /*unused1*/)
3578 gcc_assert (current_function_decl);
3580 if (TREE_CODE (*node) == BIND_EXPR)
3582 tree var;
3584 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3585 if (VAR_P (var)
3586 && !DECL_NAME (var)
3587 && DECL_ARTIFICIAL (var)
3588 && !DECL_CONTEXT (var))
3589 DECL_CONTEXT (var) = current_function_decl;
3592 return NULL_TREE;
3595 /* Set up to handle the initialization or destruction of DECL. If
3596 INITP is nonzero, we are initializing the variable. Otherwise, we
3597 are destroying it. */
3599 static void
3600 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3602 tree guard_if_stmt = NULL_TREE;
3603 tree guard;
3605 /* If we are supposed to destruct and there's a trivial destructor,
3606 nothing has to be done. */
3607 if (!initp
3608 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3609 return;
3611 /* Trick the compiler into thinking we are at the file and line
3612 where DECL was declared so that error-messages make sense, and so
3613 that the debugger will show somewhat sensible file and line
3614 information. */
3615 input_location = DECL_SOURCE_LOCATION (decl);
3617 /* Make sure temporary variables in the initialiser all have
3618 their DECL_CONTEXT() set to a value different from NULL_TREE.
3619 This can happen when global variables initialisers are built.
3620 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3621 the temporary variables that might have been generated in the
3622 accompagning initialisers is NULL_TREE, meaning the variables have been
3623 declared in the global namespace.
3624 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3625 of the temporaries are set to the current function decl. */
3626 cp_walk_tree_without_duplicates (&init,
3627 fix_temporary_vars_context_r,
3628 NULL);
3630 /* Because of:
3632 [class.access.spec]
3634 Access control for implicit calls to the constructors,
3635 the conversion functions, or the destructor called to
3636 create and destroy a static data member is performed as
3637 if these calls appeared in the scope of the member's
3638 class.
3640 we pretend we are in a static member function of the class of
3641 which the DECL is a member. */
3642 if (member_p (decl))
3644 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3645 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3648 /* Assume we don't need a guard. */
3649 guard = NULL_TREE;
3650 /* We need a guard if this is an object with external linkage that
3651 might be initialized in more than one place. (For example, a
3652 static data member of a template, when the data member requires
3653 construction.) */
3654 if (NEEDS_GUARD_P (decl))
3656 tree guard_cond;
3658 guard = get_guard (decl);
3660 /* When using __cxa_atexit, we just check the GUARD as we would
3661 for a local static. */
3662 if (flag_use_cxa_atexit)
3664 /* When using __cxa_atexit, we never try to destroy
3665 anything from a static destructor. */
3666 gcc_assert (initp);
3667 guard_cond = get_guard_cond (guard);
3669 /* If we don't have __cxa_atexit, then we will be running
3670 destructors from .fini sections, or their equivalents. So,
3671 we need to know how many times we've tried to initialize this
3672 object. We do initializations only if the GUARD is zero,
3673 i.e., if we are the first to initialize the variable. We do
3674 destructions only if the GUARD is one, i.e., if we are the
3675 last to destroy the variable. */
3676 else if (initp)
3677 guard_cond
3678 = cp_build_binary_op (input_location,
3679 EQ_EXPR,
3680 cp_build_unary_op (PREINCREMENT_EXPR,
3681 guard,
3682 /*noconvert=*/1,
3683 tf_warning_or_error),
3684 integer_one_node,
3685 tf_warning_or_error);
3686 else
3687 guard_cond
3688 = cp_build_binary_op (input_location,
3689 EQ_EXPR,
3690 cp_build_unary_op (PREDECREMENT_EXPR,
3691 guard,
3692 /*noconvert=*/1,
3693 tf_warning_or_error),
3694 integer_zero_node,
3695 tf_warning_or_error);
3697 guard_if_stmt = begin_if_stmt ();
3698 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3702 /* If we're using __cxa_atexit, we have not already set the GUARD,
3703 so we must do so now. */
3704 if (guard && initp && flag_use_cxa_atexit)
3705 finish_expr_stmt (set_guard (guard));
3707 /* Perform the initialization or destruction. */
3708 if (initp)
3710 if (init)
3712 finish_expr_stmt (init);
3713 if (flag_sanitize & SANITIZE_ADDRESS)
3715 varpool_node *vnode = varpool_node::get (decl);
3716 if (vnode)
3717 vnode->dynamically_initialized = 1;
3721 /* If we're using __cxa_atexit, register a function that calls the
3722 destructor for the object. */
3723 if (flag_use_cxa_atexit)
3724 finish_expr_stmt (register_dtor_fn (decl));
3726 else
3727 finish_expr_stmt (build_cleanup (decl));
3729 /* Finish the guard if-stmt, if necessary. */
3730 if (guard)
3732 finish_then_clause (guard_if_stmt);
3733 finish_if_stmt (guard_if_stmt);
3736 /* Now that we're done with DECL we don't need to pretend to be a
3737 member of its class any longer. */
3738 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3739 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3742 /* Generate code to do the initialization or destruction of the decls in VARS,
3743 a TREE_LIST of VAR_DECL with static storage duration.
3744 Whether initialization or destruction is performed is specified by INITP. */
3746 static void
3747 do_static_initialization_or_destruction (tree vars, bool initp)
3749 tree node, init_if_stmt, cond;
3751 /* Build the outer if-stmt to check for initialization or destruction. */
3752 init_if_stmt = begin_if_stmt ();
3753 cond = initp ? integer_one_node : integer_zero_node;
3754 cond = cp_build_binary_op (input_location,
3755 EQ_EXPR,
3756 initialize_p_decl,
3757 cond,
3758 tf_warning_or_error);
3759 finish_if_stmt_cond (cond, init_if_stmt);
3761 /* To make sure dynamic construction doesn't access globals from other
3762 compilation units where they might not be yet constructed, for
3763 -fsanitize=address insert __asan_before_dynamic_init call that
3764 prevents access to either all global variables that need construction
3765 in other compilation units, or at least those that haven't been
3766 initialized yet. Variables that need dynamic construction in
3767 the current compilation unit are kept accessible. */
3768 if (flag_sanitize & SANITIZE_ADDRESS)
3769 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3771 node = vars;
3772 do {
3773 tree decl = TREE_VALUE (node);
3774 tree priority_if_stmt;
3775 int priority;
3776 priority_info pi;
3778 /* If we don't need a destructor, there's nothing to do. Avoid
3779 creating a possibly empty if-stmt. */
3780 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3782 node = TREE_CHAIN (node);
3783 continue;
3786 /* Remember that we had an initialization or finalization at this
3787 priority. */
3788 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3789 pi = get_priority_info (priority);
3790 if (initp)
3791 pi->initializations_p = 1;
3792 else
3793 pi->destructions_p = 1;
3795 /* Conditionalize this initialization on being in the right priority
3796 and being initializing/finalizing appropriately. */
3797 priority_if_stmt = begin_if_stmt ();
3798 cond = cp_build_binary_op (input_location,
3799 EQ_EXPR,
3800 priority_decl,
3801 build_int_cst (NULL_TREE, priority),
3802 tf_warning_or_error);
3803 finish_if_stmt_cond (cond, priority_if_stmt);
3805 /* Process initializers with same priority. */
3806 for (; node
3807 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3808 node = TREE_CHAIN (node))
3809 /* Do one initialization or destruction. */
3810 one_static_initialization_or_destruction (TREE_VALUE (node),
3811 TREE_PURPOSE (node), initp);
3813 /* Finish up the priority if-stmt body. */
3814 finish_then_clause (priority_if_stmt);
3815 finish_if_stmt (priority_if_stmt);
3817 } while (node);
3819 /* Revert what __asan_before_dynamic_init did by calling
3820 __asan_after_dynamic_init. */
3821 if (flag_sanitize & SANITIZE_ADDRESS)
3822 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3824 /* Finish up the init/destruct if-stmt body. */
3825 finish_then_clause (init_if_stmt);
3826 finish_if_stmt (init_if_stmt);
3829 /* VARS is a list of variables with static storage duration which may
3830 need initialization and/or finalization. Remove those variables
3831 that don't really need to be initialized or finalized, and return
3832 the resulting list. The order in which the variables appear in
3833 VARS is in reverse order of the order in which they should actually
3834 be initialized. The list we return is in the unreversed order;
3835 i.e., the first variable should be initialized first. */
3837 static tree
3838 prune_vars_needing_no_initialization (tree *vars)
3840 tree *var = vars;
3841 tree result = NULL_TREE;
3843 while (*var)
3845 tree t = *var;
3846 tree decl = TREE_VALUE (t);
3847 tree init = TREE_PURPOSE (t);
3849 /* Deal gracefully with error. */
3850 if (decl == error_mark_node)
3852 var = &TREE_CHAIN (t);
3853 continue;
3856 /* The only things that can be initialized are variables. */
3857 gcc_assert (VAR_P (decl));
3859 /* If this object is not defined, we don't need to do anything
3860 here. */
3861 if (DECL_EXTERNAL (decl))
3863 var = &TREE_CHAIN (t);
3864 continue;
3867 /* Also, if the initializer already contains errors, we can bail
3868 out now. */
3869 if (init && TREE_CODE (init) == TREE_LIST
3870 && value_member (error_mark_node, init))
3872 var = &TREE_CHAIN (t);
3873 continue;
3876 /* This variable is going to need initialization and/or
3877 finalization, so we add it to the list. */
3878 *var = TREE_CHAIN (t);
3879 TREE_CHAIN (t) = result;
3880 result = t;
3883 return result;
3886 /* Make sure we have told the back end about all the variables in
3887 VARS. */
3889 static void
3890 write_out_vars (tree vars)
3892 tree v;
3894 for (v = vars; v; v = TREE_CHAIN (v))
3896 tree var = TREE_VALUE (v);
3897 if (!var_finalized_p (var))
3899 import_export_decl (var);
3900 rest_of_decl_compilation (var, 1, 1);
3905 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3906 (otherwise) that will initialize all global objects with static
3907 storage duration having the indicated PRIORITY. */
3909 static void
3910 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3911 location_t *locus)
3913 char function_key;
3914 tree fndecl;
3915 tree body;
3916 size_t i;
3918 input_location = *locus;
3919 /* ??? */
3920 /* Was: locus->line++; */
3922 /* We use `I' to indicate initialization and `D' to indicate
3923 destruction. */
3924 function_key = constructor_p ? 'I' : 'D';
3926 /* We emit the function lazily, to avoid generating empty
3927 global constructors and destructors. */
3928 body = NULL_TREE;
3930 /* For Objective-C++, we may need to initialize metadata found in this module.
3931 This must be done _before_ any other static initializations. */
3932 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3933 && constructor_p && objc_static_init_needed_p ())
3935 body = start_objects (function_key, priority);
3936 objc_generate_static_init_call (NULL_TREE);
3939 /* Call the static storage duration function with appropriate
3940 arguments. */
3941 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3943 /* Calls to pure or const functions will expand to nothing. */
3944 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3946 tree call;
3948 if (! body)
3949 body = start_objects (function_key, priority);
3951 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3952 build_int_cst (NULL_TREE,
3953 constructor_p),
3954 build_int_cst (NULL_TREE,
3955 priority),
3956 NULL_TREE);
3957 finish_expr_stmt (call);
3961 /* Close out the function. */
3962 if (body)
3963 finish_objects (function_key, priority, body);
3966 /* Generate constructor and destructor functions for the priority
3967 indicated by N. */
3969 static int
3970 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3972 location_t *locus = (location_t *) data;
3973 int priority = (int) n->key;
3974 priority_info pi = (priority_info) n->value;
3976 /* Generate the functions themselves, but only if they are really
3977 needed. */
3978 if (pi->initializations_p)
3979 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3980 if (pi->destructions_p)
3981 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3983 /* Keep iterating. */
3984 return 0;
3987 /* Java requires that we be able to reference a local address for a
3988 method, and not be confused by PLT entries. If hidden aliases are
3989 supported, collect and return all the functions for which we should
3990 emit a hidden alias. */
3992 static hash_set<tree> *
3993 collect_candidates_for_java_method_aliases (void)
3995 struct cgraph_node *node;
3996 hash_set<tree> *candidates = NULL;
3998 #ifndef HAVE_GAS_HIDDEN
3999 return candidates;
4000 #endif
4002 FOR_EACH_FUNCTION (node)
4004 tree fndecl = node->decl;
4006 if (DECL_CLASS_SCOPE_P (fndecl)
4007 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4008 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4010 if (candidates == NULL)
4011 candidates = new hash_set<tree>;
4012 candidates->add (fndecl);
4016 return candidates;
4020 /* Java requires that we be able to reference a local address for a
4021 method, and not be confused by PLT entries. If hidden aliases are
4022 supported, emit one for each java function that we've emitted.
4023 CANDIDATES is the set of FUNCTION_DECLs that were gathered
4024 by collect_candidates_for_java_method_aliases. */
4026 static void
4027 build_java_method_aliases (hash_set<tree> *candidates)
4029 struct cgraph_node *node;
4031 #ifndef HAVE_GAS_HIDDEN
4032 return;
4033 #endif
4035 FOR_EACH_FUNCTION (node)
4037 tree fndecl = node->decl;
4039 if (TREE_ASM_WRITTEN (fndecl)
4040 && candidates->contains (fndecl))
4042 /* Mangle the name in a predictable way; we need to reference
4043 this from a java compiled object file. */
4044 tree oid, nid, alias;
4045 const char *oname;
4046 char *nname;
4048 oid = DECL_ASSEMBLER_NAME (fndecl);
4049 oname = IDENTIFIER_POINTER (oid);
4050 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4051 nname = ACONCAT (("_ZGA", oname+2, NULL));
4052 nid = get_identifier (nname);
4054 alias = make_alias_for (fndecl, nid);
4055 TREE_PUBLIC (alias) = 1;
4056 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4058 assemble_alias (alias, oid);
4063 /* Return C++ property of T, based on given operation OP. */
4065 static int
4066 cpp_check (tree t, cpp_operation op)
4068 switch (op)
4070 case IS_ABSTRACT:
4071 return DECL_PURE_VIRTUAL_P (t);
4072 case IS_CONSTRUCTOR:
4073 return DECL_CONSTRUCTOR_P (t);
4074 case IS_DESTRUCTOR:
4075 return DECL_DESTRUCTOR_P (t);
4076 case IS_COPY_CONSTRUCTOR:
4077 return DECL_COPY_CONSTRUCTOR_P (t);
4078 case IS_TEMPLATE:
4079 return TREE_CODE (t) == TEMPLATE_DECL;
4080 case IS_TRIVIAL:
4081 return trivial_type_p (t);
4082 default:
4083 return 0;
4087 /* Collect source file references recursively, starting from NAMESPC. */
4089 static void
4090 collect_source_refs (tree namespc)
4092 tree t;
4094 if (!namespc)
4095 return;
4097 /* Iterate over names in this name space. */
4098 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4099 if (!DECL_IS_BUILTIN (t) )
4100 collect_source_ref (DECL_SOURCE_FILE (t));
4102 /* Dump siblings, if any */
4103 collect_source_refs (TREE_CHAIN (namespc));
4105 /* Dump children, if any */
4106 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4109 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4110 starting from NAMESPC. */
4112 static void
4113 collect_ada_namespace (tree namespc, const char *source_file)
4115 if (!namespc)
4116 return;
4118 /* Collect decls from this namespace */
4119 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4121 /* Collect siblings, if any */
4122 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4124 /* Collect children, if any */
4125 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4128 /* Returns true iff there is a definition available for variable or
4129 function DECL. */
4131 static bool
4132 decl_defined_p (tree decl)
4134 if (TREE_CODE (decl) == FUNCTION_DECL)
4135 return (DECL_INITIAL (decl) != NULL_TREE);
4136 else
4138 gcc_assert (VAR_P (decl));
4139 return !DECL_EXTERNAL (decl);
4143 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4145 [expr.const]
4147 An integral constant-expression can only involve ... const
4148 variables of integral or enumeration types initialized with
4149 constant expressions ...
4151 C++0x also allows constexpr variables and temporaries initialized
4152 with constant expressions. We handle the former here, but the latter
4153 are just folded away in cxx_eval_constant_expression.
4155 The standard does not require that the expression be non-volatile.
4156 G++ implements the proposed correction in DR 457. */
4158 bool
4159 decl_constant_var_p (tree decl)
4161 if (!decl_maybe_constant_var_p (decl))
4162 return false;
4164 /* We don't know if a template static data member is initialized with
4165 a constant expression until we instantiate its initializer. Even
4166 in the case of a constexpr variable, we can't treat it as a
4167 constant until its initializer is complete in case it's used in
4168 its own initializer. */
4169 mark_used (decl);
4170 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4173 /* Returns true if DECL could be a symbolic constant variable, depending on
4174 its initializer. */
4176 bool
4177 decl_maybe_constant_var_p (tree decl)
4179 tree type = TREE_TYPE (decl);
4180 if (!VAR_P (decl))
4181 return false;
4182 if (DECL_DECLARED_CONSTEXPR_P (decl))
4183 return true;
4184 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4185 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4188 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4189 called from grokfndecl and grokvardecl; in all modes it is called from
4190 cp_write_global_declarations. */
4192 void
4193 no_linkage_error (tree decl)
4195 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4196 /* In C++11 it's ok if the decl is defined. */
4197 return;
4198 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4199 if (t == NULL_TREE)
4200 /* The type that got us on no_linkage_decls must have gotten a name for
4201 linkage purposes. */;
4202 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4203 /* The type might end up having a typedef name for linkage purposes. */
4204 vec_safe_push (no_linkage_decls, decl);
4205 else if (TYPE_ANONYMOUS_P (t))
4207 bool d = false;
4208 if (cxx_dialect >= cxx11)
4209 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4210 "anonymous type, is used but never defined", decl);
4211 else if (DECL_EXTERN_C_P (decl))
4212 /* Allow this; it's pretty common in C. */;
4213 else if (TREE_CODE (decl) == VAR_DECL)
4214 /* DRs 132, 319 and 389 seem to indicate types with
4215 no linkage can only be used to declare extern "C"
4216 entities. Since it's not always an error in the
4217 ISO C++ 90 Standard, we only issue a warning. */
4218 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4219 "with no linkage used to declare variable %q#D with "
4220 "linkage", decl);
4221 else
4222 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4223 "linkage used to declare function %q#D with linkage",
4224 decl);
4225 if (d && is_typedef_decl (TYPE_NAME (t)))
4226 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4227 "to the unqualified type, so it is not used for linkage",
4228 TYPE_NAME (t));
4230 else if (cxx_dialect >= cxx11)
4231 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4232 "%qT, is used but never defined", decl, t);
4233 else if (TREE_CODE (decl) == VAR_DECL)
4234 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4235 "used to declare variable %q#D with linkage", t, decl);
4236 else
4237 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4238 "to declare function %q#D with linkage", t, decl);
4241 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4243 static void
4244 collect_all_refs (const char *source_file)
4246 collect_ada_namespace (global_namespace, source_file);
4249 /* Clear DECL_EXTERNAL for NODE. */
4251 static bool
4252 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4254 DECL_EXTERNAL (node->decl) = 0;
4255 return false;
4258 /* Build up the function to run dynamic initializers for thread_local
4259 variables in this translation unit and alias the init functions for the
4260 individual variables to it. */
4262 static void
4263 handle_tls_init (void)
4265 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4266 if (vars == NULL_TREE)
4267 return;
4269 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4271 write_out_vars (vars);
4273 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4274 boolean_type_node);
4275 TREE_PUBLIC (guard) = false;
4276 TREE_STATIC (guard) = true;
4277 DECL_ARTIFICIAL (guard) = true;
4278 DECL_IGNORED_P (guard) = true;
4279 TREE_USED (guard) = true;
4280 set_decl_tls_model (guard, decl_default_tls_model (guard));
4281 pushdecl_top_level_and_finish (guard, NULL_TREE);
4283 tree fn = get_local_tls_init_fn ();
4284 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4285 tree body = begin_function_body ();
4286 tree if_stmt = begin_if_stmt ();
4287 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4288 tf_warning_or_error);
4289 finish_if_stmt_cond (cond, if_stmt);
4290 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4291 tf_warning_or_error));
4292 for (; vars; vars = TREE_CHAIN (vars))
4294 tree var = TREE_VALUE (vars);
4295 tree init = TREE_PURPOSE (vars);
4296 one_static_initialization_or_destruction (var, init, true);
4298 #ifdef ASM_OUTPUT_DEF
4299 /* Output init aliases even with -fno-extern-tls-init. */
4300 if (TREE_PUBLIC (var))
4302 tree single_init_fn = get_tls_init_fn (var);
4303 if (single_init_fn == NULL_TREE)
4304 continue;
4305 cgraph_node *alias
4306 = cgraph_node::get_create (fn)->create_same_body_alias
4307 (single_init_fn, fn);
4308 gcc_assert (alias != NULL);
4310 #endif
4313 finish_then_clause (if_stmt);
4314 finish_if_stmt (if_stmt);
4315 finish_function_body (body);
4316 expand_or_defer_fn (finish_function (0));
4319 /* The entire file is now complete. If requested, dump everything
4320 to a file. */
4322 static void
4323 dump_tu (void)
4325 int flags;
4326 FILE *stream = dump_begin (TDI_tu, &flags);
4328 if (stream)
4330 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4331 dump_end (TDI_tu, stream);
4335 /* Check the deallocation functions for CODE to see if we want to warn that
4336 only one was defined. */
4338 static void
4339 maybe_warn_sized_delete (enum tree_code code)
4341 tree sized = NULL_TREE;
4342 tree unsized = NULL_TREE;
4344 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4345 ovl; ovl = OVL_NEXT (ovl))
4347 tree fn = OVL_CURRENT (ovl);
4348 /* We're only interested in usual deallocation functions. */
4349 if (!non_placement_deallocation_fn_p (fn))
4350 continue;
4351 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4352 unsized = fn;
4353 else
4354 sized = fn;
4356 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4357 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4358 "the program should also define %qD", sized);
4359 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4360 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4361 "the program should also define %qD", unsized);
4364 /* Check the global deallocation functions to see if we want to warn about
4365 defining unsized without sized (or vice versa). */
4367 static void
4368 maybe_warn_sized_delete ()
4370 if (!flag_sized_deallocation || !warn_sized_deallocation)
4371 return;
4372 maybe_warn_sized_delete (DELETE_EXPR);
4373 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4376 /* This routine is called at the end of compilation.
4377 Its job is to create all the code needed to initialize and
4378 destroy the global aggregates. We do the destruction
4379 first, since that way we only need to reverse the decls once. */
4381 void
4382 cp_write_global_declarations (void)
4384 tree vars;
4385 bool reconsider;
4386 size_t i;
4387 location_t locus;
4388 unsigned ssdf_count = 0;
4389 int retries = 0;
4390 tree decl;
4391 hash_set<tree> *candidates;
4393 locus = input_location;
4394 at_eof = 1;
4396 /* Bad parse errors. Just forget about it. */
4397 if (! global_bindings_p () || current_class_type
4398 || !vec_safe_is_empty (decl_namespace_list))
4399 return;
4401 /* This is the point to write out a PCH if we're doing that.
4402 In that case we do not want to do anything else. */
4403 if (pch_file)
4405 c_common_write_pch ();
4406 dump_tu ();
4407 return;
4410 symtab->process_same_body_aliases ();
4412 /* Handle -fdump-ada-spec[-slim] */
4413 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4415 if (flag_dump_ada_spec_slim)
4416 collect_source_ref (main_input_filename);
4417 else
4418 collect_source_refs (global_namespace);
4420 dump_ada_specs (collect_all_refs, cpp_check);
4423 /* FIXME - huh? was input_line -= 1;*/
4425 timevar_start (TV_PHASE_DEFERRED);
4427 /* We now have to write out all the stuff we put off writing out.
4428 These include:
4430 o Template specializations that we have not yet instantiated,
4431 but which are needed.
4432 o Initialization and destruction for non-local objects with
4433 static storage duration. (Local objects with static storage
4434 duration are initialized when their scope is first entered,
4435 and are cleaned up via atexit.)
4436 o Virtual function tables.
4438 All of these may cause others to be needed. For example,
4439 instantiating one function may cause another to be needed, and
4440 generating the initializer for an object may cause templates to be
4441 instantiated, etc., etc. */
4443 emit_support_tinfos ();
4447 tree t;
4448 tree decl;
4450 reconsider = false;
4452 /* If there are templates that we've put off instantiating, do
4453 them now. */
4454 instantiate_pending_templates (retries);
4455 ggc_collect ();
4457 /* Write out virtual tables as required. Note that writing out
4458 the virtual table for a template class may cause the
4459 instantiation of members of that class. If we write out
4460 vtables then we remove the class from our list so we don't
4461 have to look at it again. */
4463 while (keyed_classes != NULL_TREE
4464 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4466 reconsider = true;
4467 keyed_classes = TREE_CHAIN (keyed_classes);
4470 t = keyed_classes;
4471 if (t != NULL_TREE)
4473 tree next = TREE_CHAIN (t);
4475 while (next)
4477 if (maybe_emit_vtables (TREE_VALUE (next)))
4479 reconsider = true;
4480 TREE_CHAIN (t) = TREE_CHAIN (next);
4482 else
4483 t = next;
4485 next = TREE_CHAIN (t);
4489 /* Write out needed type info variables. We have to be careful
4490 looping through unemitted decls, because emit_tinfo_decl may
4491 cause other variables to be needed. New elements will be
4492 appended, and we remove from the vector those that actually
4493 get emitted. */
4494 for (i = unemitted_tinfo_decls->length ();
4495 unemitted_tinfo_decls->iterate (--i, &t);)
4496 if (emit_tinfo_decl (t))
4498 reconsider = true;
4499 unemitted_tinfo_decls->unordered_remove (i);
4502 /* The list of objects with static storage duration is built up
4503 in reverse order. We clear STATIC_AGGREGATES so that any new
4504 aggregates added during the initialization of these will be
4505 initialized in the correct order when we next come around the
4506 loop. */
4507 vars = prune_vars_needing_no_initialization (&static_aggregates);
4509 if (vars)
4511 /* We need to start a new initialization function each time
4512 through the loop. That's because we need to know which
4513 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4514 isn't computed until a function is finished, and written
4515 out. That's a deficiency in the back end. When this is
4516 fixed, these initialization functions could all become
4517 inline, with resulting performance improvements. */
4518 tree ssdf_body;
4520 /* Set the line and file, so that it is obviously not from
4521 the source file. */
4522 input_location = locus;
4523 ssdf_body = start_static_storage_duration_function (ssdf_count);
4525 /* Make sure the back end knows about all the variables. */
4526 write_out_vars (vars);
4528 /* First generate code to do all the initializations. */
4529 if (vars)
4530 do_static_initialization_or_destruction (vars, /*initp=*/true);
4532 /* Then, generate code to do all the destructions. Do these
4533 in reverse order so that the most recently constructed
4534 variable is the first destroyed. If we're using
4535 __cxa_atexit, then we don't need to do this; functions
4536 were registered at initialization time to destroy the
4537 local statics. */
4538 if (!flag_use_cxa_atexit && vars)
4540 vars = nreverse (vars);
4541 do_static_initialization_or_destruction (vars, /*initp=*/false);
4543 else
4544 vars = NULL_TREE;
4546 /* Finish up the static storage duration function for this
4547 round. */
4548 input_location = locus;
4549 finish_static_storage_duration_function (ssdf_body);
4551 /* All those initializations and finalizations might cause
4552 us to need more inline functions, more template
4553 instantiations, etc. */
4554 reconsider = true;
4555 ssdf_count++;
4556 /* ??? was: locus.line++; */
4559 /* Now do the same for thread_local variables. */
4560 handle_tls_init ();
4562 /* Go through the set of inline functions whose bodies have not
4563 been emitted yet. If out-of-line copies of these functions
4564 are required, emit them. */
4565 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4567 /* Does it need synthesizing? */
4568 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4569 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4571 /* Even though we're already at the top-level, we push
4572 there again. That way, when we pop back a few lines
4573 hence, all of our state is restored. Otherwise,
4574 finish_function doesn't clean things up, and we end
4575 up with CURRENT_FUNCTION_DECL set. */
4576 push_to_top_level ();
4577 /* The decl's location will mark where it was first
4578 needed. Save that so synthesize method can indicate
4579 where it was needed from, in case of error */
4580 input_location = DECL_SOURCE_LOCATION (decl);
4581 synthesize_method (decl);
4582 pop_from_top_level ();
4583 reconsider = true;
4586 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4587 generate_tls_wrapper (decl);
4589 if (!DECL_SAVED_TREE (decl))
4590 continue;
4592 /* We lie to the back end, pretending that some functions
4593 are not defined when they really are. This keeps these
4594 functions from being put out unnecessarily. But, we must
4595 stop lying when the functions are referenced, or if they
4596 are not comdat since they need to be put out now. If
4597 DECL_INTERFACE_KNOWN, then we have already set
4598 DECL_EXTERNAL appropriately, so there's no need to check
4599 again, and we do not want to clear DECL_EXTERNAL if a
4600 previous call to import_export_decl set it.
4602 This is done in a separate for cycle, because if some
4603 deferred function is contained in another deferred
4604 function later in deferred_fns varray,
4605 rest_of_compilation would skip this function and we
4606 really cannot expand the same function twice. */
4607 import_export_decl (decl);
4608 if (DECL_NOT_REALLY_EXTERN (decl)
4609 && DECL_INITIAL (decl)
4610 && decl_needed_p (decl))
4612 struct cgraph_node *node, *next;
4614 node = cgraph_node::get (decl);
4615 if (node->cpp_implicit_alias)
4616 node = node->get_alias_target ();
4618 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4619 NULL, true);
4620 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4621 group, we need to mark all symbols in the same comdat group
4622 that way. */
4623 if (node->same_comdat_group)
4624 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4625 next != node;
4626 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4627 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4628 NULL, true);
4631 /* If we're going to need to write this function out, and
4632 there's already a body for it, create RTL for it now.
4633 (There might be no body if this is a method we haven't
4634 gotten around to synthesizing yet.) */
4635 if (!DECL_EXTERNAL (decl)
4636 && decl_needed_p (decl)
4637 && !TREE_ASM_WRITTEN (decl)
4638 && !cgraph_node::get (decl)->definition)
4640 /* We will output the function; no longer consider it in this
4641 loop. */
4642 DECL_DEFER_OUTPUT (decl) = 0;
4643 /* Generate RTL for this function now that we know we
4644 need it. */
4645 expand_or_defer_fn (decl);
4646 /* If we're compiling -fsyntax-only pretend that this
4647 function has been written out so that we don't try to
4648 expand it again. */
4649 if (flag_syntax_only)
4650 TREE_ASM_WRITTEN (decl) = 1;
4651 reconsider = true;
4655 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4656 reconsider = true;
4658 /* Static data members are just like namespace-scope globals. */
4659 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4661 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4662 /* Don't write it out if we haven't seen a definition. */
4663 || DECL_IN_AGGR_P (decl))
4664 continue;
4665 import_export_decl (decl);
4666 /* If this static data member is needed, provide it to the
4667 back end. */
4668 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4669 DECL_EXTERNAL (decl) = 0;
4671 if (vec_safe_length (pending_statics) != 0
4672 && wrapup_global_declarations (pending_statics->address (),
4673 pending_statics->length ()))
4674 reconsider = true;
4676 retries++;
4678 while (reconsider);
4680 /* All used inline functions must have a definition at this point. */
4681 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4683 if (/* Check online inline functions that were actually used. */
4684 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4685 /* If the definition actually was available here, then the
4686 fact that the function was not defined merely represents
4687 that for some reason (use of a template repository,
4688 #pragma interface, etc.) we decided not to emit the
4689 definition here. */
4690 && !DECL_INITIAL (decl)
4691 /* Don't complain if the template was defined. */
4692 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4693 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4694 (template_for_substitution (decl)))))
4696 warning (0, "inline function %q+D used but never defined", decl);
4697 /* Avoid a duplicate warning from check_global_declaration_1. */
4698 TREE_NO_WARNING (decl) = 1;
4702 /* So must decls that use a type with no linkage. */
4703 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4704 no_linkage_error (decl);
4706 maybe_warn_sized_delete ();
4708 /* Then, do the Objective-C stuff. This is where all the
4709 Objective-C module stuff gets generated (symtab,
4710 class/protocol/selector lists etc). This must be done after C++
4711 templates, destructors etc. so that selectors used in C++
4712 templates are properly allocated. */
4713 if (c_dialect_objc ())
4714 objc_write_global_declarations ();
4716 /* We give C linkage to static constructors and destructors. */
4717 push_lang_context (lang_name_c);
4719 /* Generate initialization and destruction functions for all
4720 priorities for which they are required. */
4721 if (priority_info_map)
4722 splay_tree_foreach (priority_info_map,
4723 generate_ctor_and_dtor_functions_for_priority,
4724 /*data=*/&locus);
4725 else if (c_dialect_objc () && objc_static_init_needed_p ())
4726 /* If this is obj-c++ and we need a static init, call
4727 generate_ctor_or_dtor_function. */
4728 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4729 DEFAULT_INIT_PRIORITY, &locus);
4731 /* We're done with the splay-tree now. */
4732 if (priority_info_map)
4733 splay_tree_delete (priority_info_map);
4735 /* Generate any missing aliases. */
4736 maybe_apply_pending_pragma_weaks ();
4738 /* We're done with static constructors, so we can go back to "C++"
4739 linkage now. */
4740 pop_lang_context ();
4742 /* Collect candidates for Java hidden aliases. */
4743 candidates = collect_candidates_for_java_method_aliases ();
4745 timevar_stop (TV_PHASE_DEFERRED);
4746 timevar_start (TV_PHASE_OPT_GEN);
4748 if (flag_vtable_verify)
4750 vtv_recover_class_info ();
4751 vtv_compute_class_hierarchy_transitive_closure ();
4752 vtv_build_vtable_verify_fndecl ();
4755 symtab->finalize_compilation_unit ();
4757 if (flag_vtable_verify)
4759 /* Generate the special constructor initialization function that
4760 calls __VLTRegisterPairs, and give it a very high
4761 initialization priority. This must be done after
4762 finalize_compilation_unit so that we have accurate
4763 information about which vtable will actually be emitted. */
4764 vtv_generate_init_routine ();
4767 timevar_stop (TV_PHASE_OPT_GEN);
4768 timevar_start (TV_PHASE_CHECK_DBGINFO);
4770 /* Now, issue warnings about static, but not defined, functions,
4771 etc., and emit debugging information. */
4772 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4773 if (vec_safe_length (pending_statics) != 0)
4775 check_global_declarations (pending_statics->address (),
4776 pending_statics->length ());
4777 emit_debug_global_declarations (pending_statics->address (),
4778 pending_statics->length ());
4781 perform_deferred_noexcept_checks ();
4783 /* Generate hidden aliases for Java. */
4784 if (candidates)
4786 build_java_method_aliases (candidates);
4787 delete candidates;
4790 finish_repo ();
4792 /* The entire file is now complete. If requested, dump everything
4793 to a file. */
4794 dump_tu ();
4796 if (flag_detailed_statistics)
4798 dump_tree_statistics ();
4799 dump_time_statistics ();
4801 input_location = locus;
4803 #ifdef ENABLE_CHECKING
4804 validate_conversion_obstack ();
4805 #endif /* ENABLE_CHECKING */
4807 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4810 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4811 function to call in parse-tree form; it has not yet been
4812 semantically analyzed. ARGS are the arguments to the function.
4813 They have already been semantically analyzed. This may change
4814 ARGS. */
4816 tree
4817 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4818 tsubst_flags_t complain)
4820 tree orig_fn;
4821 vec<tree, va_gc> *orig_args = NULL;
4822 tree expr;
4823 tree object;
4825 orig_fn = fn;
4826 object = TREE_OPERAND (fn, 0);
4828 if (processing_template_decl)
4830 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4831 || TREE_CODE (fn) == MEMBER_REF);
4832 if (type_dependent_expression_p (fn)
4833 || any_type_dependent_arguments_p (*args))
4834 return build_nt_call_vec (fn, *args);
4836 orig_args = make_tree_vector_copy (*args);
4838 /* Transform the arguments and add the implicit "this"
4839 parameter. That must be done before the FN is transformed
4840 because we depend on the form of FN. */
4841 make_args_non_dependent (*args);
4842 object = build_non_dependent_expr (object);
4843 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4845 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4846 object = cp_build_addr_expr (object, complain);
4847 vec_safe_insert (*args, 0, object);
4849 /* Now that the arguments are done, transform FN. */
4850 fn = build_non_dependent_expr (fn);
4853 /* A qualified name corresponding to a bound pointer-to-member is
4854 represented as an OFFSET_REF:
4856 struct B { void g(); };
4857 void (B::*p)();
4858 void B::g() { (this->*p)(); } */
4859 if (TREE_CODE (fn) == OFFSET_REF)
4861 tree object_addr = cp_build_addr_expr (object, complain);
4862 fn = TREE_OPERAND (fn, 1);
4863 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4864 complain);
4865 vec_safe_insert (*args, 0, object_addr);
4868 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4869 expr = build_op_call (fn, args, complain);
4870 else
4871 expr = cp_build_function_call_vec (fn, args, complain);
4872 if (processing_template_decl && expr != error_mark_node)
4873 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4875 if (orig_args != NULL)
4876 release_tree_vector (orig_args);
4878 return expr;
4882 void
4883 check_default_args (tree x)
4885 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4886 bool saw_def = false;
4887 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4888 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4890 if (TREE_PURPOSE (arg))
4891 saw_def = true;
4892 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4894 error ("default argument missing for parameter %P of %q+#D", i, x);
4895 TREE_PURPOSE (arg) = error_mark_node;
4900 /* Return true if function DECL can be inlined. This is used to force
4901 instantiation of methods that might be interesting for inlining. */
4902 bool
4903 possibly_inlined_p (tree decl)
4905 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4906 if (DECL_UNINLINABLE (decl))
4907 return false;
4908 if (!optimize || pragma_java_exceptions)
4909 return DECL_DECLARED_INLINE_P (decl);
4910 /* When optimizing, we might inline everything when flatten
4911 attribute or heuristics inlining for size or autoinlining
4912 is used. */
4913 return true;
4916 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4917 If DECL is a specialization or implicitly declared class member,
4918 generate the actual definition. Return false if something goes
4919 wrong, true otherwise. */
4921 bool
4922 mark_used (tree decl, tsubst_flags_t complain)
4924 /* If DECL is a BASELINK for a single function, then treat it just
4925 like the DECL for the function. Otherwise, if the BASELINK is
4926 for an overloaded function, we don't know which function was
4927 actually used until after overload resolution. */
4928 if (BASELINK_P (decl))
4930 decl = BASELINK_FUNCTIONS (decl);
4931 if (really_overloaded_fn (decl))
4932 return true;
4933 decl = OVL_CURRENT (decl);
4936 /* Set TREE_USED for the benefit of -Wunused. */
4937 TREE_USED (decl) = 1;
4938 if (DECL_CLONED_FUNCTION_P (decl))
4939 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4941 /* Mark enumeration types as used. */
4942 if (TREE_CODE (decl) == CONST_DECL)
4943 used_types_insert (DECL_CONTEXT (decl));
4945 if (TREE_CODE (decl) == FUNCTION_DECL)
4946 maybe_instantiate_noexcept (decl);
4948 if (TREE_CODE (decl) == FUNCTION_DECL
4949 && DECL_DELETED_FN (decl))
4951 if (DECL_ARTIFICIAL (decl))
4953 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4954 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4956 /* We mark a lambda conversion op as deleted if we can't
4957 generate it properly; see maybe_add_lambda_conv_op. */
4958 sorry ("converting lambda which uses %<...%> to "
4959 "function pointer");
4960 return false;
4963 if (complain & tf_error)
4965 error ("use of deleted function %qD", decl);
4966 if (!maybe_explain_implicit_delete (decl))
4967 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4969 return false;
4972 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
4973 && deprecated_state != DEPRECATED_SUPPRESS)
4974 warn_deprecated_use (decl, NULL_TREE);
4976 /* We can only check DECL_ODR_USED on variables or functions with
4977 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4978 might need special handling for. */
4979 if (!VAR_OR_FUNCTION_DECL_P (decl)
4980 || DECL_LANG_SPECIFIC (decl) == NULL
4981 || DECL_THUNK_P (decl))
4983 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4985 if (complain & tf_error)
4986 error ("use of %qD before deduction of %<auto%>", decl);
4987 return false;
4989 return true;
4992 /* We only want to do this processing once. We don't need to keep trying
4993 to instantiate inline templates, because unit-at-a-time will make sure
4994 we get them compiled before functions that want to inline them. */
4995 if (DECL_ODR_USED (decl))
4996 return true;
4998 /* If within finish_function, defer the rest until that function
4999 finishes, otherwise it might recurse. */
5000 if (defer_mark_used_calls)
5002 vec_safe_push (deferred_mark_used_calls, decl);
5003 return true;
5006 /* Normally, we can wait until instantiation-time to synthesize DECL.
5007 However, if DECL is a static data member initialized with a constant
5008 or a constexpr function, we need it right now because a reference to
5009 such a data member or a call to such function is not value-dependent.
5010 For a function that uses auto in the return type, we need to instantiate
5011 it to find out its type. For OpenMP user defined reductions, we need
5012 them instantiated for reduction clauses which inline them by hand
5013 directly. */
5014 if (DECL_LANG_SPECIFIC (decl)
5015 && DECL_TEMPLATE_INFO (decl)
5016 && (decl_maybe_constant_var_p (decl)
5017 || (TREE_CODE (decl) == FUNCTION_DECL
5018 && (DECL_DECLARED_CONSTEXPR_P (decl)
5019 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
5020 || undeduced_auto_decl (decl))
5021 && !uses_template_parms (DECL_TI_ARGS (decl)))
5023 /* Instantiating a function will result in garbage collection. We
5024 must treat this situation as if we were within the body of a
5025 function so as to avoid collecting live data only referenced from
5026 the stack (such as overload resolution candidates). */
5027 ++function_depth;
5028 instantiate_decl (decl, /*defer_ok=*/false,
5029 /*expl_inst_class_mem_p=*/false);
5030 --function_depth;
5033 if (processing_template_decl || in_template_function ())
5034 return true;
5036 /* Check this too in case we're within instantiate_non_dependent_expr. */
5037 if (DECL_TEMPLATE_INFO (decl)
5038 && uses_template_parms (DECL_TI_ARGS (decl)))
5039 return true;
5041 if (undeduced_auto_decl (decl))
5043 if (complain & tf_error)
5044 error ("use of %qD before deduction of %<auto%>", decl);
5045 return false;
5048 /* If we don't need a value, then we don't need to synthesize DECL. */
5049 if (cp_unevaluated_operand != 0)
5050 return true;
5052 DECL_ODR_USED (decl) = 1;
5053 if (DECL_CLONED_FUNCTION_P (decl))
5054 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5056 /* DR 757: A type without linkage shall not be used as the type of a
5057 variable or function with linkage, unless
5058 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5059 o the variable or function is not used (3.2 [basic.def.odr]) or is
5060 defined in the same translation unit. */
5061 if (cxx_dialect > cxx98
5062 && decl_linkage (decl) != lk_none
5063 && !DECL_EXTERN_C_P (decl)
5064 && !DECL_ARTIFICIAL (decl)
5065 && !decl_defined_p (decl)
5066 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5068 if (is_local_extern (decl))
5069 /* There's no way to define a local extern, and adding it to
5070 the vector interferes with GC, so give an error now. */
5071 no_linkage_error (decl);
5072 else
5073 vec_safe_push (no_linkage_decls, decl);
5076 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5077 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5078 /* Remember it, so we can check it was defined. */
5079 note_vague_linkage_fn (decl);
5081 /* Is it a synthesized method that needs to be synthesized? */
5082 if (TREE_CODE (decl) == FUNCTION_DECL
5083 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5084 && DECL_DEFAULTED_FN (decl)
5085 /* A function defaulted outside the class is synthesized either by
5086 cp_finish_decl or instantiate_decl. */
5087 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5088 && ! DECL_INITIAL (decl))
5090 /* Defer virtual destructors so that thunks get the right
5091 linkage. */
5092 if (DECL_VIRTUAL_P (decl) && !at_eof)
5094 note_vague_linkage_fn (decl);
5095 return true;
5098 /* Remember the current location for a function we will end up
5099 synthesizing. Then we can inform the user where it was
5100 required in the case of error. */
5101 DECL_SOURCE_LOCATION (decl) = input_location;
5103 /* Synthesizing an implicitly defined member function will result in
5104 garbage collection. We must treat this situation as if we were
5105 within the body of a function so as to avoid collecting live data
5106 on the stack (such as overload resolution candidates).
5108 We could just let cp_write_global_declarations handle synthesizing
5109 this function by adding it to deferred_fns, but doing
5110 it at the use site produces better error messages. */
5111 ++function_depth;
5112 synthesize_method (decl);
5113 --function_depth;
5114 /* If this is a synthesized method we don't need to
5115 do the instantiation test below. */
5117 else if (VAR_OR_FUNCTION_DECL_P (decl)
5118 && DECL_TEMPLATE_INFO (decl)
5119 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5120 || always_instantiate_p (decl)))
5121 /* If this is a function or variable that is an instance of some
5122 template, we now know that we will need to actually do the
5123 instantiation. We check that DECL is not an explicit
5124 instantiation because that is not checked in instantiate_decl.
5126 We put off instantiating functions in order to improve compile
5127 times. Maintaining a stack of active functions is expensive,
5128 and the inliner knows to instantiate any functions it might
5129 need. Therefore, we always try to defer instantiation. */
5131 ++function_depth;
5132 instantiate_decl (decl, /*defer_ok=*/true,
5133 /*expl_inst_class_mem_p=*/false);
5134 --function_depth;
5137 return true;
5140 bool
5141 mark_used (tree decl)
5143 return mark_used (decl, tf_warning_or_error);
5146 tree
5147 vtv_start_verification_constructor_init_function (void)
5149 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5152 tree
5153 vtv_finish_verification_constructor_init_function (tree function_body)
5155 tree fn;
5157 finish_compound_stmt (function_body);
5158 fn = finish_function (0);
5159 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5160 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5162 return fn;
5165 #include "gt-cp-decl2.h"