2014-12-19 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / cp / decl2.c
blob4478c6cb15cd0e770d42fb51684d52f3b86b25bc
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "stringpool.h"
35 #include "varasm.h"
36 #include "attribs.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "flags.h"
40 #include "cp-tree.h"
41 #include "decl.h"
42 #include "toplev.h"
43 #include "timevar.h"
44 #include "cpplib.h"
45 #include "target.h"
46 #include "c-family/c-common.h"
47 #include "c-family/c-objc.h"
48 #include "hash-map.h"
49 #include "is-a.h"
50 #include "plugin-api.h"
51 #include "vec.h"
52 #include "hashtab.h"
53 #include "hash-set.h"
54 #include "machmode.h"
55 #include "hard-reg-set.h"
56 #include "input.h"
57 #include "function.h"
58 #include "ipa-ref.h"
59 #include "cgraph.h"
60 #include "tree-inline.h"
61 #include "c-family/c-pragma.h"
62 #include "dumpfile.h"
63 #include "intl.h"
64 #include "splay-tree.h"
65 #include "langhooks.h"
66 #include "c-family/c-ada-spec.h"
67 #include "asan.h"
69 extern cpp_reader *parse_in;
71 /* This structure contains information about the initializations
72 and/or destructions required for a particular priority level. */
73 typedef struct priority_info_s {
74 /* Nonzero if there have been any initializations at this priority
75 throughout the translation unit. */
76 int initializations_p;
77 /* Nonzero if there have been any destructions at this priority
78 throughout the translation unit. */
79 int destructions_p;
80 } *priority_info;
82 static void mark_vtable_entries (tree);
83 static bool maybe_emit_vtables (tree);
84 static bool acceptable_java_type (tree);
85 static tree start_objects (int, int);
86 static void finish_objects (int, int, tree);
87 static tree start_static_storage_duration_function (unsigned);
88 static void finish_static_storage_duration_function (tree);
89 static priority_info get_priority_info (int);
90 static void do_static_initialization_or_destruction (tree, bool);
91 static void one_static_initialization_or_destruction (tree, tree, bool);
92 static void generate_ctor_or_dtor_function (bool, int, location_t *);
93 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
94 void *);
95 static tree prune_vars_needing_no_initialization (tree *);
96 static void write_out_vars (tree);
97 static void import_export_class (tree);
98 static tree get_guard_bits (tree);
99 static void determine_visibility_from_class (tree, tree);
100 static bool determine_hidden_inline (tree);
101 static bool decl_defined_p (tree);
103 /* A list of static class variables. This is needed, because a
104 static class variable can be declared inside the class without
105 an initializer, and then initialized, statically, outside the class. */
106 static GTY(()) vec<tree, va_gc> *pending_statics;
108 /* A list of functions which were declared inline, but which we
109 may need to emit outline anyway. */
110 static GTY(()) vec<tree, va_gc> *deferred_fns;
112 /* A list of decls that use types with no linkage, which we need to make
113 sure are defined. */
114 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
116 /* Nonzero if we're done parsing and into end-of-file activities. */
118 int at_eof;
121 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
122 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
123 that apply to the function). */
125 tree
126 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
127 cp_ref_qualifier rqual)
129 tree raises;
130 tree attrs;
131 int type_quals;
132 bool late_return_type_p;
134 if (fntype == error_mark_node || ctype == error_mark_node)
135 return error_mark_node;
137 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
138 || TREE_CODE (fntype) == METHOD_TYPE);
140 type_quals = quals & ~TYPE_QUAL_RESTRICT;
141 ctype = cp_build_qualified_type (ctype, type_quals);
142 raises = TYPE_RAISES_EXCEPTIONS (fntype);
143 attrs = TYPE_ATTRIBUTES (fntype);
144 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
145 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
146 (TREE_CODE (fntype) == METHOD_TYPE
147 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
148 : TYPE_ARG_TYPES (fntype)));
149 if (attrs)
150 fntype = cp_build_type_attribute_variant (fntype, attrs);
151 if (rqual)
152 fntype = build_ref_qualified_type (fntype, rqual);
153 if (raises)
154 fntype = build_exception_variant (fntype, raises);
155 if (late_return_type_p)
156 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
158 return fntype;
161 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
162 return type changed to NEW_RET. */
164 tree
165 change_return_type (tree new_ret, tree fntype)
167 tree newtype;
168 tree args = TYPE_ARG_TYPES (fntype);
169 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
170 tree attrs = TYPE_ATTRIBUTES (fntype);
171 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
173 if (new_ret == error_mark_node)
174 return fntype;
176 if (same_type_p (new_ret, TREE_TYPE (fntype)))
177 return fntype;
179 if (TREE_CODE (fntype) == FUNCTION_TYPE)
181 newtype = build_function_type (new_ret, args);
182 newtype = apply_memfn_quals (newtype,
183 type_memfn_quals (fntype),
184 type_memfn_rqual (fntype));
186 else
187 newtype = build_method_type_directly
188 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
189 if (raises)
190 newtype = build_exception_variant (newtype, raises);
191 if (attrs)
192 newtype = cp_build_type_attribute_variant (newtype, attrs);
193 if (late_return_type_p)
194 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
196 return newtype;
199 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
200 appropriately. */
202 tree
203 cp_build_parm_decl (tree name, tree type)
205 tree parm = build_decl (input_location,
206 PARM_DECL, name, type);
207 /* DECL_ARG_TYPE is only used by the back end and the back end never
208 sees templates. */
209 if (!processing_template_decl)
210 DECL_ARG_TYPE (parm) = type_passed_as (type);
212 return parm;
215 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
216 indicated NAME. */
218 tree
219 build_artificial_parm (tree name, tree type)
221 tree parm = cp_build_parm_decl (name, type);
222 DECL_ARTIFICIAL (parm) = 1;
223 /* All our artificial parms are implicitly `const'; they cannot be
224 assigned to. */
225 TREE_READONLY (parm) = 1;
226 return parm;
229 /* Constructors for types with virtual baseclasses need an "in-charge" flag
230 saying whether this constructor is responsible for initialization of
231 virtual baseclasses or not. All destructors also need this "in-charge"
232 flag, which additionally determines whether or not the destructor should
233 free the memory for the object.
235 This function adds the "in-charge" flag to member function FN if
236 appropriate. It is called from grokclassfn and tsubst.
237 FN must be either a constructor or destructor.
239 The in-charge flag follows the 'this' parameter, and is followed by the
240 VTT parm (if any), then the user-written parms. */
242 void
243 maybe_retrofit_in_chrg (tree fn)
245 tree basetype, arg_types, parms, parm, fntype;
247 /* If we've already add the in-charge parameter don't do it again. */
248 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
249 return;
251 /* When processing templates we can't know, in general, whether or
252 not we're going to have virtual baseclasses. */
253 if (processing_template_decl)
254 return;
256 /* We don't need an in-charge parameter for constructors that don't
257 have virtual bases. */
258 if (DECL_CONSTRUCTOR_P (fn)
259 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
260 return;
262 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
263 basetype = TREE_TYPE (TREE_VALUE (arg_types));
264 arg_types = TREE_CHAIN (arg_types);
266 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
268 /* If this is a subobject constructor or destructor, our caller will
269 pass us a pointer to our VTT. */
270 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
272 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
274 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
275 DECL_CHAIN (parm) = parms;
276 parms = parm;
278 /* ...and then to TYPE_ARG_TYPES. */
279 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
281 DECL_HAS_VTT_PARM_P (fn) = 1;
284 /* Then add the in-charge parm (before the VTT parm). */
285 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
286 DECL_CHAIN (parm) = parms;
287 parms = parm;
288 arg_types = hash_tree_chain (integer_type_node, arg_types);
290 /* Insert our new parameter(s) into the list. */
291 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
293 /* And rebuild the function type. */
294 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
295 arg_types);
296 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
297 fntype = build_exception_variant (fntype,
298 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
299 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
300 fntype = (cp_build_type_attribute_variant
301 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
302 TREE_TYPE (fn) = fntype;
304 /* Now we've got the in-charge parameter. */
305 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
308 /* Classes overload their constituent function names automatically.
309 When a function name is declared in a record structure,
310 its name is changed to it overloaded name. Since names for
311 constructors and destructors can conflict, we place a leading
312 '$' for destructors.
314 CNAME is the name of the class we are grokking for.
316 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
318 FLAGS contains bits saying what's special about today's
319 arguments. DTOR_FLAG == DESTRUCTOR.
321 If FUNCTION is a destructor, then we must add the `auto-delete' field
322 as a second parameter. There is some hair associated with the fact
323 that we must "declare" this variable in the manner consistent with the
324 way the rest of the arguments were declared.
326 QUALS are the qualifiers for the this pointer. */
328 void
329 grokclassfn (tree ctype, tree function, enum overload_flags flags)
331 tree fn_name = DECL_NAME (function);
333 /* Even within an `extern "C"' block, members get C++ linkage. See
334 [dcl.link] for details. */
335 SET_DECL_LANGUAGE (function, lang_cplusplus);
337 if (fn_name == NULL_TREE)
339 error ("name missing for member function");
340 fn_name = get_identifier ("<anonymous>");
341 DECL_NAME (function) = fn_name;
344 DECL_CONTEXT (function) = ctype;
346 if (flags == DTOR_FLAG)
347 DECL_DESTRUCTOR_P (function) = 1;
349 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
350 maybe_retrofit_in_chrg (function);
353 /* Create an ARRAY_REF, checking for the user doing things backwards
354 along the way. DECLTYPE_P is for N3276, as in the parser. */
356 tree
357 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
358 bool decltype_p)
360 tree type;
361 tree expr;
362 tree orig_array_expr = array_expr;
363 tree orig_index_exp = index_exp;
365 if (error_operand_p (array_expr) || error_operand_p (index_exp))
366 return error_mark_node;
368 if (processing_template_decl)
370 if (type_dependent_expression_p (array_expr)
371 || type_dependent_expression_p (index_exp))
372 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
373 NULL_TREE, NULL_TREE);
374 array_expr = build_non_dependent_expr (array_expr);
375 index_exp = build_non_dependent_expr (index_exp);
378 type = TREE_TYPE (array_expr);
379 gcc_assert (type);
380 type = non_reference (type);
382 /* If they have an `operator[]', use that. */
383 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
385 tsubst_flags_t complain = tf_warning_or_error;
386 if (decltype_p)
387 complain |= tf_decltype;
388 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
389 index_exp, NULL_TREE, /*overload=*/NULL, complain);
391 else
393 tree p1, p2, i1, i2;
395 /* Otherwise, create an ARRAY_REF for a pointer or array type.
396 It is a little-known fact that, if `a' is an array and `i' is
397 an int, you can write `i[a]', which means the same thing as
398 `a[i]'. */
399 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
400 p1 = array_expr;
401 else
402 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
404 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
405 p2 = index_exp;
406 else
407 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
409 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
410 false);
411 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
412 false);
414 if ((p1 && i2) && (i1 && p2))
415 error ("ambiguous conversion for array subscript");
417 if (p1 && i2)
418 array_expr = p1, index_exp = i2;
419 else if (i1 && p2)
420 array_expr = p2, index_exp = i1;
421 else
423 error ("invalid types %<%T[%T]%> for array subscript",
424 type, TREE_TYPE (index_exp));
425 return error_mark_node;
428 if (array_expr == error_mark_node || index_exp == error_mark_node)
429 error ("ambiguous conversion for array subscript");
431 expr = build_array_ref (input_location, array_expr, index_exp);
433 if (processing_template_decl && expr != error_mark_node)
434 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
435 NULL_TREE, NULL_TREE);
436 return expr;
439 /* Given the cast expression EXP, checking out its validity. Either return
440 an error_mark_node if there was an unavoidable error, return a cast to
441 void for trying to delete a pointer w/ the value 0, or return the
442 call to delete. If DOING_VEC is true, we handle things differently
443 for doing an array delete.
444 Implements ARM $5.3.4. This is called from the parser. */
446 tree
447 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
448 tsubst_flags_t complain)
450 tree t, type;
452 if (exp == error_mark_node)
453 return exp;
455 if (processing_template_decl)
457 t = build_min (DELETE_EXPR, void_type_node, exp, size);
458 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
459 DELETE_EXPR_USE_VEC (t) = doing_vec;
460 TREE_SIDE_EFFECTS (t) = 1;
461 return t;
464 /* An array can't have been allocated by new, so complain. */
465 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
466 warning (0, "deleting array %q#E", exp);
468 t = build_expr_type_conversion (WANT_POINTER, exp, true);
470 if (t == NULL_TREE || t == error_mark_node)
472 error ("type %q#T argument given to %<delete%>, expected pointer",
473 TREE_TYPE (exp));
474 return error_mark_node;
477 type = TREE_TYPE (t);
479 /* As of Valley Forge, you can delete a pointer to const. */
481 /* You can't delete functions. */
482 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
484 error ("cannot delete a function. Only pointer-to-objects are "
485 "valid arguments to %<delete%>");
486 return error_mark_node;
489 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
490 if (VOID_TYPE_P (TREE_TYPE (type)))
492 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
493 doing_vec = 0;
496 /* Deleting a pointer with the value zero is valid and has no effect. */
497 if (integer_zerop (t))
498 return build1 (NOP_EXPR, void_type_node, t);
500 if (doing_vec)
501 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
502 sfk_deleting_destructor,
503 use_global_delete, complain);
504 else
505 return build_delete (type, t, sfk_deleting_destructor,
506 LOOKUP_NORMAL, use_global_delete,
507 complain);
510 /* Report an error if the indicated template declaration is not the
511 sort of thing that should be a member template. */
513 void
514 check_member_template (tree tmpl)
516 tree decl;
518 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
519 decl = DECL_TEMPLATE_RESULT (tmpl);
521 if (TREE_CODE (decl) == FUNCTION_DECL
522 || DECL_ALIAS_TEMPLATE_P (tmpl)
523 || (TREE_CODE (decl) == TYPE_DECL
524 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
526 /* The parser rejects template declarations in local classes
527 (with the exception of generic lambdas). */
528 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
529 /* The parser rejects any use of virtual in a function template. */
530 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
531 && DECL_VIRTUAL_P (decl)));
533 /* The debug-information generating code doesn't know what to do
534 with member templates. */
535 DECL_IGNORED_P (tmpl) = 1;
537 else if (variable_template_p (tmpl))
538 /* OK */;
539 else
540 error ("template declaration of %q#D", decl);
543 /* Return true iff TYPE is a valid Java parameter or return type. */
545 static bool
546 acceptable_java_type (tree type)
548 if (type == error_mark_node)
549 return false;
551 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
552 return true;
553 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
555 type = TREE_TYPE (type);
556 if (TREE_CODE (type) == RECORD_TYPE)
558 tree args; int i;
559 if (! TYPE_FOR_JAVA (type))
560 return false;
561 if (! CLASSTYPE_TEMPLATE_INFO (type))
562 return true;
563 args = CLASSTYPE_TI_ARGS (type);
564 i = TREE_VEC_LENGTH (args);
565 while (--i >= 0)
567 type = TREE_VEC_ELT (args, i);
568 if (TYPE_PTR_P (type))
569 type = TREE_TYPE (type);
570 if (! TYPE_FOR_JAVA (type))
571 return false;
573 return true;
576 return false;
579 /* For a METHOD in a Java class CTYPE, return true if
580 the parameter and return types are valid Java types.
581 Otherwise, print appropriate error messages, and return false. */
583 bool
584 check_java_method (tree method)
586 bool jerr = false;
587 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
588 tree ret_type = TREE_TYPE (TREE_TYPE (method));
590 if (!acceptable_java_type (ret_type))
592 error ("Java method %qD has non-Java return type %qT",
593 method, ret_type);
594 jerr = true;
597 arg_types = TREE_CHAIN (arg_types);
598 if (DECL_HAS_IN_CHARGE_PARM_P (method))
599 arg_types = TREE_CHAIN (arg_types);
600 if (DECL_HAS_VTT_PARM_P (method))
601 arg_types = TREE_CHAIN (arg_types);
603 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
605 tree type = TREE_VALUE (arg_types);
606 if (!acceptable_java_type (type))
608 if (type != error_mark_node)
609 error ("Java method %qD has non-Java parameter type %qT",
610 method, type);
611 jerr = true;
614 return !jerr;
617 /* Sanity check: report error if this function FUNCTION is not
618 really a member of the class (CTYPE) it is supposed to belong to.
619 TEMPLATE_PARMS is used to specify the template parameters of a member
620 template passed as FUNCTION_DECL. If the member template is passed as a
621 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
622 from the declaration. If the function is not a function template, it
623 must be NULL.
624 It returns the original declaration for the function, NULL_TREE if
625 no declaration was found, error_mark_node if an error was emitted. */
627 tree
628 check_classfn (tree ctype, tree function, tree template_parms)
630 int ix;
631 bool is_template;
632 tree pushed_scope;
634 if (DECL_USE_TEMPLATE (function)
635 && !(TREE_CODE (function) == TEMPLATE_DECL
636 && DECL_TEMPLATE_SPECIALIZATION (function))
637 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
638 /* Since this is a specialization of a member template,
639 we're not going to find the declaration in the class.
640 For example, in:
642 struct S { template <typename T> void f(T); };
643 template <> void S::f(int);
645 we're not going to find `S::f(int)', but there's no
646 reason we should, either. We let our callers know we didn't
647 find the method, but we don't complain. */
648 return NULL_TREE;
650 /* Basic sanity check: for a template function, the template parameters
651 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
652 if (TREE_CODE (function) == TEMPLATE_DECL)
654 if (template_parms
655 && !comp_template_parms (template_parms,
656 DECL_TEMPLATE_PARMS (function)))
658 error ("template parameter lists provided don%'t match the "
659 "template parameters of %qD", function);
660 return error_mark_node;
662 template_parms = DECL_TEMPLATE_PARMS (function);
665 /* OK, is this a definition of a member template? */
666 is_template = (template_parms != NULL_TREE);
668 /* [temp.mem]
670 A destructor shall not be a member template. */
671 if (DECL_DESTRUCTOR_P (function) && is_template)
673 error ("destructor %qD declared as member template", function);
674 return error_mark_node;
677 /* We must enter the scope here, because conversion operators are
678 named by target type, and type equivalence relies on typenames
679 resolving within the scope of CTYPE. */
680 pushed_scope = push_scope (ctype);
681 ix = class_method_index_for_fn (complete_type (ctype), function);
682 if (ix >= 0)
684 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
685 tree fndecls, fndecl = 0;
686 bool is_conv_op;
687 const char *format = NULL;
689 for (fndecls = (*methods)[ix];
690 fndecls; fndecls = OVL_NEXT (fndecls))
692 tree p1, p2;
694 fndecl = OVL_CURRENT (fndecls);
695 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
696 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
698 /* We cannot simply call decls_match because this doesn't
699 work for static member functions that are pretending to
700 be methods, and because the name may have been changed by
701 asm("new_name"). */
703 /* Get rid of the this parameter on functions that become
704 static. */
705 if (DECL_STATIC_FUNCTION_P (fndecl)
706 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
707 p1 = TREE_CHAIN (p1);
709 /* A member template definition only matches a member template
710 declaration. */
711 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
712 continue;
714 /* ref-qualifier or absence of same must match. */
715 if (type_memfn_rqual (TREE_TYPE (function))
716 != type_memfn_rqual (TREE_TYPE (fndecl)))
717 continue;
719 /* While finding a match, same types and params are not enough
720 if the function is versioned. Also check version ("target")
721 attributes. */
722 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
723 TREE_TYPE (TREE_TYPE (fndecl)))
724 && compparms (p1, p2)
725 && !targetm.target_option.function_versions (function, fndecl)
726 && (!is_template
727 || comp_template_parms (template_parms,
728 DECL_TEMPLATE_PARMS (fndecl)))
729 && (DECL_TEMPLATE_SPECIALIZATION (function)
730 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
731 && (!DECL_TEMPLATE_SPECIALIZATION (function)
732 || (DECL_TI_TEMPLATE (function)
733 == DECL_TI_TEMPLATE (fndecl))))
734 break;
736 if (fndecls)
738 if (pushed_scope)
739 pop_scope (pushed_scope);
740 return OVL_CURRENT (fndecls);
743 error_at (DECL_SOURCE_LOCATION (function),
744 "prototype for %q#D does not match any in class %qT",
745 function, ctype);
746 is_conv_op = DECL_CONV_FN_P (fndecl);
748 if (is_conv_op)
749 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
750 fndecls = (*methods)[ix];
751 while (fndecls)
753 fndecl = OVL_CURRENT (fndecls);
754 fndecls = OVL_NEXT (fndecls);
756 if (!fndecls && is_conv_op)
758 if (methods->length () > (size_t) ++ix)
760 fndecls = (*methods)[ix];
761 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
763 fndecls = NULL_TREE;
764 is_conv_op = false;
767 else
768 is_conv_op = false;
770 if (format)
771 format = " %+#D";
772 else if (fndecls)
773 format = N_("candidates are: %+#D");
774 else
775 format = N_("candidate is: %+#D");
776 error (format, fndecl);
779 else if (!COMPLETE_TYPE_P (ctype))
780 cxx_incomplete_type_error (function, ctype);
781 else
782 error ("no %q#D member function declared in class %qT",
783 function, ctype);
785 if (pushed_scope)
786 pop_scope (pushed_scope);
787 return error_mark_node;
790 /* DECL is a function with vague linkage. Remember it so that at the
791 end of the translation unit we can decide whether or not to emit
792 it. */
794 void
795 note_vague_linkage_fn (tree decl)
797 DECL_DEFER_OUTPUT (decl) = 1;
798 vec_safe_push (deferred_fns, decl);
801 /* As above, but for variable template instantiations. */
803 void
804 note_variable_template_instantiation (tree decl)
806 vec_safe_push (pending_statics, decl);
809 /* We have just processed the DECL, which is a static data member.
810 The other parameters are as for cp_finish_decl. */
812 void
813 finish_static_data_member_decl (tree decl,
814 tree init, bool init_const_expr_p,
815 tree asmspec_tree,
816 int flags)
818 DECL_CONTEXT (decl) = current_class_type;
820 /* We cannot call pushdecl here, because that would fill in the
821 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
822 the right thing, namely, to put this decl out straight away. */
824 if (! processing_template_decl)
825 vec_safe_push (pending_statics, decl);
827 if (LOCAL_CLASS_P (current_class_type)
828 /* We already complained about the template definition. */
829 && !DECL_TEMPLATE_INSTANTIATION (decl))
830 permerror (input_location, "local class %q#T shall not have static data member %q#D",
831 current_class_type, decl);
832 else
833 for (tree t = current_class_type; TYPE_P (t);
834 t = CP_TYPE_CONTEXT (t))
835 if (TYPE_ANONYMOUS_P (t))
837 if (permerror (DECL_SOURCE_LOCATION (decl),
838 "static data member %qD in unnamed class", decl))
839 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
840 "unnamed class defined here");
841 break;
844 DECL_IN_AGGR_P (decl) = 1;
846 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
847 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
848 SET_VAR_HAD_UNKNOWN_BOUND (decl);
850 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
853 /* DECLARATOR and DECLSPECS correspond to a class member. The other
854 parameters are as for cp_finish_decl. Return the DECL for the
855 class member declared. */
857 tree
858 grokfield (const cp_declarator *declarator,
859 cp_decl_specifier_seq *declspecs,
860 tree init, bool init_const_expr_p,
861 tree asmspec_tree,
862 tree attrlist)
864 tree value;
865 const char *asmspec = 0;
866 int flags;
867 tree name;
869 if (init
870 && TREE_CODE (init) == TREE_LIST
871 && TREE_VALUE (init) == error_mark_node
872 && TREE_CHAIN (init) == NULL_TREE)
873 init = NULL_TREE;
875 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
876 if (! value || value == error_mark_node)
877 /* friend or constructor went bad. */
878 return error_mark_node;
879 if (TREE_TYPE (value) == error_mark_node)
880 return value;
882 if (TREE_CODE (value) == TYPE_DECL && init)
884 error ("typedef %qD is initialized (use decltype instead)", value);
885 init = NULL_TREE;
888 /* Pass friendly classes back. */
889 if (value == void_type_node)
890 return value;
893 name = DECL_NAME (value);
895 if (name != NULL_TREE)
897 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
899 error ("explicit template argument list not allowed");
900 return error_mark_node;
903 if (IDENTIFIER_POINTER (name)[0] == '_'
904 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
905 error ("member %qD conflicts with virtual function table field name",
906 value);
909 /* Stash away type declarations. */
910 if (TREE_CODE (value) == TYPE_DECL)
912 DECL_NONLOCAL (value) = 1;
913 DECL_CONTEXT (value) = current_class_type;
915 if (attrlist)
917 int attrflags = 0;
919 /* If this is a typedef that names the class for linkage purposes
920 (7.1.3p8), apply any attributes directly to the type. */
921 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
922 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
923 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
925 cplus_decl_attributes (&value, attrlist, attrflags);
928 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
929 && TREE_TYPE (value) != error_mark_node
930 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
931 set_underlying_type (value);
933 /* It's important that push_template_decl below follows
934 set_underlying_type above so that the created template
935 carries the properly set type of VALUE. */
936 if (processing_template_decl)
937 value = push_template_decl (value);
939 record_locally_defined_typedef (value);
940 return value;
943 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
945 if (!friendp && DECL_IN_AGGR_P (value))
947 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
948 return void_type_node;
951 if (asmspec_tree && asmspec_tree != error_mark_node)
952 asmspec = TREE_STRING_POINTER (asmspec_tree);
954 if (init)
956 if (TREE_CODE (value) == FUNCTION_DECL)
958 if (init == ridpointers[(int)RID_DELETE])
960 DECL_DELETED_FN (value) = 1;
961 DECL_DECLARED_INLINE_P (value) = 1;
962 DECL_INITIAL (value) = error_mark_node;
964 else if (init == ridpointers[(int)RID_DEFAULT])
966 if (defaultable_fn_check (value))
968 DECL_DEFAULTED_FN (value) = 1;
969 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
970 DECL_DECLARED_INLINE_P (value) = 1;
973 else if (TREE_CODE (init) == DEFAULT_ARG)
974 error ("invalid initializer for member function %qD", value);
975 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
977 if (integer_zerop (init))
978 DECL_PURE_VIRTUAL_P (value) = 1;
979 else if (error_operand_p (init))
980 ; /* An error has already been reported. */
981 else
982 error ("invalid initializer for member function %qD",
983 value);
985 else
987 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
988 if (friendp)
989 error ("initializer specified for friend function %qD",
990 value);
991 else
992 error ("initializer specified for static member function %qD",
993 value);
996 else if (TREE_CODE (value) == FIELD_DECL)
997 /* C++11 NSDMI, keep going. */;
998 else if (!VAR_P (value))
999 gcc_unreachable ();
1002 /* Pass friend decls back. */
1003 if ((TREE_CODE (value) == FUNCTION_DECL
1004 || TREE_CODE (value) == TEMPLATE_DECL)
1005 && DECL_CONTEXT (value) != current_class_type)
1006 return value;
1008 /* Need to set this before push_template_decl. */
1009 if (TREE_CODE (value) == VAR_DECL)
1010 DECL_CONTEXT (value) = current_class_type;
1012 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1014 value = push_template_decl (value);
1015 if (error_operand_p (value))
1016 return error_mark_node;
1019 if (attrlist)
1020 cplus_decl_attributes (&value, attrlist, 0);
1022 if (init && DIRECT_LIST_INIT_P (init))
1023 flags = LOOKUP_NORMAL;
1024 else
1025 flags = LOOKUP_IMPLICIT;
1027 switch (TREE_CODE (value))
1029 case VAR_DECL:
1030 finish_static_data_member_decl (value, init, init_const_expr_p,
1031 asmspec_tree, flags);
1032 return value;
1034 case FIELD_DECL:
1035 if (asmspec)
1036 error ("%<asm%> specifiers are not permitted on non-static data members");
1037 if (DECL_INITIAL (value) == error_mark_node)
1038 init = error_mark_node;
1039 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1040 NULL_TREE, flags);
1041 DECL_IN_AGGR_P (value) = 1;
1042 return value;
1044 case FUNCTION_DECL:
1045 if (asmspec)
1046 set_user_assembler_name (value, asmspec);
1048 cp_finish_decl (value,
1049 /*init=*/NULL_TREE,
1050 /*init_const_expr_p=*/false,
1051 asmspec_tree, flags);
1053 /* Pass friends back this way. */
1054 if (DECL_FRIEND_P (value))
1055 return void_type_node;
1057 DECL_IN_AGGR_P (value) = 1;
1058 return value;
1060 default:
1061 gcc_unreachable ();
1063 return NULL_TREE;
1066 /* Like `grokfield', but for bitfields.
1067 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1069 tree
1070 grokbitfield (const cp_declarator *declarator,
1071 cp_decl_specifier_seq *declspecs, tree width,
1072 tree attrlist)
1074 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1076 if (value == error_mark_node)
1077 return NULL_TREE; /* friends went bad. */
1078 if (TREE_TYPE (value) == error_mark_node)
1079 return value;
1081 /* Pass friendly classes back. */
1082 if (VOID_TYPE_P (value))
1083 return void_type_node;
1085 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1086 && (POINTER_TYPE_P (value)
1087 || !dependent_type_p (TREE_TYPE (value))))
1089 error ("bit-field %qD with non-integral type", value);
1090 return error_mark_node;
1093 if (TREE_CODE (value) == TYPE_DECL)
1095 error ("cannot declare %qD to be a bit-field type", value);
1096 return NULL_TREE;
1099 /* Usually, finish_struct_1 catches bitfields with invalid types.
1100 But, in the case of bitfields with function type, we confuse
1101 ourselves into thinking they are member functions, so we must
1102 check here. */
1103 if (TREE_CODE (value) == FUNCTION_DECL)
1105 error ("cannot declare bit-field %qD with function type",
1106 DECL_NAME (value));
1107 return NULL_TREE;
1110 if (DECL_IN_AGGR_P (value))
1112 error ("%qD is already defined in the class %qT", value,
1113 DECL_CONTEXT (value));
1114 return void_type_node;
1117 if (TREE_STATIC (value))
1119 error ("static member %qD cannot be a bit-field", value);
1120 return NULL_TREE;
1122 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1124 if (width != error_mark_node)
1126 /* The width must be an integer type. */
1127 if (!type_dependent_expression_p (width)
1128 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1129 error ("width of bit-field %qD has non-integral type %qT", value,
1130 TREE_TYPE (width));
1131 DECL_INITIAL (value) = width;
1132 SET_DECL_C_BIT_FIELD (value);
1135 DECL_IN_AGGR_P (value) = 1;
1137 if (attrlist)
1138 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1140 return value;
1144 /* Returns true iff ATTR is an attribute which needs to be applied at
1145 instantiation time rather than template definition time. */
1147 static bool
1148 is_late_template_attribute (tree attr, tree decl)
1150 tree name = get_attribute_name (attr);
1151 tree args = TREE_VALUE (attr);
1152 const struct attribute_spec *spec = lookup_attribute_spec (name);
1153 tree arg;
1155 if (!spec)
1156 /* Unknown attribute. */
1157 return false;
1159 /* Attribute weak handling wants to write out assembly right away. */
1160 if (is_attribute_p ("weak", name))
1161 return true;
1163 /* Attribute unused is applied directly, as it appertains to
1164 decls. */
1165 if (is_attribute_p ("unused", name))
1166 return false;
1168 /* #pragma omp declare simd attribute needs to be always deferred. */
1169 if (flag_openmp
1170 && is_attribute_p ("omp declare simd", name))
1171 return true;
1173 /* If any of the arguments are dependent expressions, we can't evaluate
1174 the attribute until instantiation time. */
1175 for (arg = args; arg; arg = TREE_CHAIN (arg))
1177 tree t = TREE_VALUE (arg);
1179 /* If the first attribute argument is an identifier, only consider
1180 second and following arguments. Attributes like mode, format,
1181 cleanup and several target specific attributes aren't late
1182 just because they have an IDENTIFIER_NODE as first argument. */
1183 if (arg == args && identifier_p (t))
1184 continue;
1186 if (value_dependent_expression_p (t)
1187 || type_dependent_expression_p (t))
1188 return true;
1191 if (TREE_CODE (decl) == TYPE_DECL
1192 || TYPE_P (decl)
1193 || spec->type_required)
1195 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1197 /* We can't apply any attributes to a completely unknown type until
1198 instantiation time. */
1199 enum tree_code code = TREE_CODE (type);
1200 if (code == TEMPLATE_TYPE_PARM
1201 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1202 || code == TYPENAME_TYPE)
1203 return true;
1204 /* Also defer most attributes on dependent types. This is not
1205 necessary in all cases, but is the better default. */
1206 else if (dependent_type_p (type)
1207 /* But some attributes specifically apply to templates. */
1208 && !is_attribute_p ("abi_tag", name)
1209 && !is_attribute_p ("deprecated", name)
1210 && !is_attribute_p ("visibility", name))
1211 return true;
1212 else
1213 return false;
1215 else
1216 return false;
1219 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1220 applied at instantiation time and return them. If IS_DEPENDENT is true,
1221 the declaration itself is dependent, so all attributes should be applied
1222 at instantiation time. */
1224 static tree
1225 splice_template_attributes (tree *attr_p, tree decl)
1227 tree *p = attr_p;
1228 tree late_attrs = NULL_TREE;
1229 tree *q = &late_attrs;
1231 if (!p)
1232 return NULL_TREE;
1234 for (; *p; )
1236 if (is_late_template_attribute (*p, decl))
1238 ATTR_IS_DEPENDENT (*p) = 1;
1239 *q = *p;
1240 *p = TREE_CHAIN (*p);
1241 q = &TREE_CHAIN (*q);
1242 *q = NULL_TREE;
1244 else
1245 p = &TREE_CHAIN (*p);
1248 return late_attrs;
1251 /* Remove any late attributes from the list in ATTR_P and attach them to
1252 DECL_P. */
1254 static void
1255 save_template_attributes (tree *attr_p, tree *decl_p)
1257 tree *q;
1259 if (attr_p && *attr_p == error_mark_node)
1260 return;
1262 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1263 if (!late_attrs)
1264 return;
1266 if (DECL_P (*decl_p))
1267 q = &DECL_ATTRIBUTES (*decl_p);
1268 else
1269 q = &TYPE_ATTRIBUTES (*decl_p);
1271 tree old_attrs = *q;
1273 /* Merge the late attributes at the beginning with the attribute
1274 list. */
1275 late_attrs = merge_attributes (late_attrs, *q);
1276 *q = late_attrs;
1278 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1280 /* We've added new attributes directly to the main variant, so
1281 now we need to update all of the other variants to include
1282 these new attributes. */
1283 tree variant;
1284 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1285 variant = TYPE_NEXT_VARIANT (variant))
1287 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1288 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1293 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1294 to a typedef which gives a previously anonymous class or enum a name for
1295 linkage purposes. */
1297 bool
1298 attributes_naming_typedef_ok (tree attrs)
1300 for (; attrs; attrs = TREE_CHAIN (attrs))
1302 tree name = get_attribute_name (attrs);
1303 if (is_attribute_p ("vector_size", name))
1304 return false;
1306 return true;
1309 /* Like reconstruct_complex_type, but handle also template trees. */
1311 tree
1312 cp_reconstruct_complex_type (tree type, tree bottom)
1314 tree inner, outer;
1315 bool late_return_type_p = false;
1317 if (TYPE_PTR_P (type))
1319 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1320 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1321 TYPE_REF_CAN_ALIAS_ALL (type));
1323 else if (TREE_CODE (type) == REFERENCE_TYPE)
1325 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1326 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1327 TYPE_REF_CAN_ALIAS_ALL (type));
1329 else if (TREE_CODE (type) == ARRAY_TYPE)
1331 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1332 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1333 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1334 element type qualification will be handled by the recursive
1335 cp_reconstruct_complex_type call and cp_build_qualified_type
1336 for ARRAY_TYPEs changes the element type. */
1337 return outer;
1339 else if (TREE_CODE (type) == FUNCTION_TYPE)
1341 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1342 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1343 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1344 outer = apply_memfn_quals (outer,
1345 type_memfn_quals (type),
1346 type_memfn_rqual (type));
1348 else if (TREE_CODE (type) == METHOD_TYPE)
1350 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1351 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1352 /* The build_method_type_directly() routine prepends 'this' to argument list,
1353 so we must compensate by getting rid of it. */
1354 outer
1355 = build_method_type_directly
1356 (class_of_this_parm (type), inner,
1357 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1359 else if (TREE_CODE (type) == OFFSET_TYPE)
1361 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1362 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1364 else
1365 return bottom;
1367 if (TYPE_ATTRIBUTES (type))
1368 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1369 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1371 if (late_return_type_p)
1372 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1374 return outer;
1377 /* Replaces any constexpr expression that may be into the attributes
1378 arguments with their reduced value. */
1380 static void
1381 cp_check_const_attributes (tree attributes)
1383 if (attributes == error_mark_node)
1384 return;
1386 tree attr;
1387 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1389 tree arg;
1390 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1392 tree expr = TREE_VALUE (arg);
1393 if (EXPR_P (expr))
1394 TREE_VALUE (arg) = maybe_constant_value (expr);
1399 /* Return true if TYPE is an OpenMP mappable type. */
1400 bool
1401 cp_omp_mappable_type (tree type)
1403 /* Mappable type has to be complete. */
1404 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1405 return false;
1406 /* Arrays have mappable type if the elements have mappable type. */
1407 while (TREE_CODE (type) == ARRAY_TYPE)
1408 type = TREE_TYPE (type);
1409 /* A mappable type cannot contain virtual members. */
1410 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1411 return false;
1412 /* All data members must be non-static. */
1413 if (CLASS_TYPE_P (type))
1415 tree field;
1416 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1417 if (TREE_CODE (field) == VAR_DECL)
1418 return false;
1419 /* All fields must have mappable types. */
1420 else if (TREE_CODE (field) == FIELD_DECL
1421 && !cp_omp_mappable_type (TREE_TYPE (field)))
1422 return false;
1424 return true;
1427 /* Like decl_attributes, but handle C++ complexity. */
1429 void
1430 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1432 if (*decl == NULL_TREE || *decl == void_type_node
1433 || *decl == error_mark_node)
1434 return;
1436 /* Add implicit "omp declare target" attribute if requested. */
1437 if (scope_chain->omp_declare_target_attribute
1438 && ((TREE_CODE (*decl) == VAR_DECL && TREE_STATIC (*decl))
1439 || TREE_CODE (*decl) == FUNCTION_DECL))
1441 if (TREE_CODE (*decl) == VAR_DECL
1442 && DECL_CLASS_SCOPE_P (*decl))
1443 error ("%q+D static data member inside of declare target directive",
1444 *decl);
1445 else if (TREE_CODE (*decl) == VAR_DECL
1446 && (DECL_FUNCTION_SCOPE_P (*decl)
1447 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1448 error ("%q+D in block scope inside of declare target directive",
1449 *decl);
1450 else if (!processing_template_decl
1451 && TREE_CODE (*decl) == VAR_DECL
1452 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1453 error ("%q+D in declare target directive does not have mappable type",
1454 *decl);
1455 else
1456 attributes = tree_cons (get_identifier ("omp declare target"),
1457 NULL_TREE, attributes);
1460 if (processing_template_decl)
1462 if (check_for_bare_parameter_packs (attributes))
1463 return;
1465 save_template_attributes (&attributes, decl);
1468 cp_check_const_attributes (attributes);
1470 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1471 decl = &DECL_TEMPLATE_RESULT (*decl);
1473 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1475 if (TYPE_P (*decl))
1476 attributes
1477 = type_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1478 else
1479 attributes
1480 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1481 type_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1482 attributes, flags);
1484 else
1485 if (TYPE_P (*decl))
1486 type_attributes (decl, attributes, flags);
1487 else
1488 decl_attributes (decl, attributes, flags);
1490 if (TREE_CODE (*decl) == TYPE_DECL)
1491 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1493 /* Propagate deprecation out to the template. */
1494 if (TREE_DEPRECATED (*decl))
1495 if (tree ti = get_template_info (*decl))
1497 tree tmpl = TI_TEMPLATE (ti);
1498 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1499 : DECL_TEMPLATE_RESULT (tmpl));
1500 if (*decl == pattern)
1501 TREE_DEPRECATED (tmpl) = true;
1505 /* Walks through the namespace- or function-scope anonymous union
1506 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1507 Returns one of the fields for use in the mangled name. */
1509 static tree
1510 build_anon_union_vars (tree type, tree object)
1512 tree main_decl = NULL_TREE;
1513 tree field;
1515 /* Rather than write the code to handle the non-union case,
1516 just give an error. */
1517 if (TREE_CODE (type) != UNION_TYPE)
1519 error ("anonymous struct not inside named type");
1520 return error_mark_node;
1523 for (field = TYPE_FIELDS (type);
1524 field != NULL_TREE;
1525 field = DECL_CHAIN (field))
1527 tree decl;
1528 tree ref;
1530 if (DECL_ARTIFICIAL (field))
1531 continue;
1532 if (TREE_CODE (field) != FIELD_DECL)
1534 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1535 "have non-static data members", field);
1536 continue;
1539 if (TREE_PRIVATE (field))
1540 permerror (input_location, "private member %q+#D in anonymous union", field);
1541 else if (TREE_PROTECTED (field))
1542 permerror (input_location, "protected member %q+#D in anonymous union", field);
1544 if (processing_template_decl)
1545 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1546 DECL_NAME (field), NULL_TREE);
1547 else
1548 ref = build_class_member_access_expr (object, field, NULL_TREE,
1549 false, tf_warning_or_error);
1551 if (DECL_NAME (field))
1553 tree base;
1555 decl = build_decl (input_location,
1556 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1557 DECL_ANON_UNION_VAR_P (decl) = 1;
1558 DECL_ARTIFICIAL (decl) = 1;
1560 base = get_base_address (object);
1561 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1562 TREE_STATIC (decl) = TREE_STATIC (base);
1563 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1565 SET_DECL_VALUE_EXPR (decl, ref);
1566 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1568 decl = pushdecl (decl);
1570 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1571 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1572 else
1573 decl = 0;
1575 if (main_decl == NULL_TREE)
1576 main_decl = decl;
1579 return main_decl;
1582 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1583 anonymous union, then all members must be laid out together. PUBLIC_P
1584 is nonzero if this union is not declared static. */
1586 void
1587 finish_anon_union (tree anon_union_decl)
1589 tree type;
1590 tree main_decl;
1591 bool public_p;
1593 if (anon_union_decl == error_mark_node)
1594 return;
1596 type = TREE_TYPE (anon_union_decl);
1597 public_p = TREE_PUBLIC (anon_union_decl);
1599 /* The VAR_DECL's context is the same as the TYPE's context. */
1600 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1602 if (TYPE_FIELDS (type) == NULL_TREE)
1603 return;
1605 if (public_p)
1607 error ("namespace-scope anonymous aggregates must be static");
1608 return;
1611 main_decl = build_anon_union_vars (type, anon_union_decl);
1612 if (main_decl == error_mark_node)
1613 return;
1614 if (main_decl == NULL_TREE)
1616 warning (0, "anonymous union with no members");
1617 return;
1620 if (!processing_template_decl)
1622 /* Use main_decl to set the mangled name. */
1623 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1624 maybe_commonize_var (anon_union_decl);
1625 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1626 mangle_decl (anon_union_decl);
1627 DECL_NAME (anon_union_decl) = NULL_TREE;
1630 pushdecl (anon_union_decl);
1631 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1634 /* Auxiliary functions to make type signatures for
1635 `operator new' and `operator delete' correspond to
1636 what compiler will be expecting. */
1638 tree
1639 coerce_new_type (tree type)
1641 int e = 0;
1642 tree args = TYPE_ARG_TYPES (type);
1644 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1646 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1648 e = 1;
1649 error ("%<operator new%> must return type %qT", ptr_type_node);
1652 if (args && args != void_list_node)
1654 if (TREE_PURPOSE (args))
1656 /* [basic.stc.dynamic.allocation]
1658 The first parameter shall not have an associated default
1659 argument. */
1660 error ("the first parameter of %<operator new%> cannot "
1661 "have a default argument");
1662 /* Throw away the default argument. */
1663 TREE_PURPOSE (args) = NULL_TREE;
1666 if (!same_type_p (TREE_VALUE (args), size_type_node))
1668 e = 2;
1669 args = TREE_CHAIN (args);
1672 else
1673 e = 2;
1675 if (e == 2)
1676 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1677 "as first parameter", size_type_node);
1679 switch (e)
1681 case 2:
1682 args = tree_cons (NULL_TREE, size_type_node, args);
1683 /* Fall through. */
1684 case 1:
1685 type = build_exception_variant
1686 (build_function_type (ptr_type_node, args),
1687 TYPE_RAISES_EXCEPTIONS (type));
1688 /* Fall through. */
1689 default:;
1691 return type;
1694 tree
1695 coerce_delete_type (tree type)
1697 int e = 0;
1698 tree args = TYPE_ARG_TYPES (type);
1700 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1702 if (!same_type_p (TREE_TYPE (type), void_type_node))
1704 e = 1;
1705 error ("%<operator delete%> must return type %qT", void_type_node);
1708 if (!args || args == void_list_node
1709 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1711 e = 2;
1712 if (args && args != void_list_node)
1713 args = TREE_CHAIN (args);
1714 error ("%<operator delete%> takes type %qT as first parameter",
1715 ptr_type_node);
1717 switch (e)
1719 case 2:
1720 args = tree_cons (NULL_TREE, ptr_type_node, args);
1721 /* Fall through. */
1722 case 1:
1723 type = build_exception_variant
1724 (build_function_type (void_type_node, args),
1725 TYPE_RAISES_EXCEPTIONS (type));
1726 /* Fall through. */
1727 default:;
1730 return type;
1733 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1734 and mark them as needed. */
1736 static void
1737 mark_vtable_entries (tree decl)
1739 tree fnaddr;
1740 unsigned HOST_WIDE_INT idx;
1742 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1743 idx, fnaddr)
1745 tree fn;
1747 STRIP_NOPS (fnaddr);
1749 if (TREE_CODE (fnaddr) != ADDR_EXPR
1750 && TREE_CODE (fnaddr) != FDESC_EXPR)
1751 /* This entry is an offset: a virtual base class offset, a
1752 virtual call offset, an RTTI offset, etc. */
1753 continue;
1755 fn = TREE_OPERAND (fnaddr, 0);
1756 TREE_ADDRESSABLE (fn) = 1;
1757 /* When we don't have vcall offsets, we output thunks whenever
1758 we output the vtables that contain them. With vcall offsets,
1759 we know all the thunks we'll need when we emit a virtual
1760 function, so we emit the thunks there instead. */
1761 if (DECL_THUNK_P (fn))
1762 use_thunk (fn, /*emit_p=*/0);
1763 mark_used (fn);
1767 /* Set DECL up to have the closest approximation of "initialized common"
1768 linkage available. */
1770 void
1771 comdat_linkage (tree decl)
1773 if (flag_weak)
1774 make_decl_one_only (decl, cxx_comdat_group (decl));
1775 else if (TREE_CODE (decl) == FUNCTION_DECL
1776 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1777 /* We can just emit function and compiler-generated variables
1778 statically; having multiple copies is (for the most part) only
1779 a waste of space.
1781 There are two correctness issues, however: the address of a
1782 template instantiation with external linkage should be the
1783 same, independent of what translation unit asks for the
1784 address, and this will not hold when we emit multiple copies of
1785 the function. However, there's little else we can do.
1787 Also, by default, the typeinfo implementation assumes that
1788 there will be only one copy of the string used as the name for
1789 each type. Therefore, if weak symbols are unavailable, the
1790 run-time library should perform a more conservative check; it
1791 should perform a string comparison, rather than an address
1792 comparison. */
1793 TREE_PUBLIC (decl) = 0;
1794 else
1796 /* Static data member template instantiations, however, cannot
1797 have multiple copies. */
1798 if (DECL_INITIAL (decl) == 0
1799 || DECL_INITIAL (decl) == error_mark_node)
1800 DECL_COMMON (decl) = 1;
1801 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1803 DECL_COMMON (decl) = 1;
1804 DECL_INITIAL (decl) = error_mark_node;
1806 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1808 /* We can't do anything useful; leave vars for explicit
1809 instantiation. */
1810 DECL_EXTERNAL (decl) = 1;
1811 DECL_NOT_REALLY_EXTERN (decl) = 0;
1815 DECL_COMDAT (decl) = 1;
1818 /* For win32 we also want to put explicit instantiations in
1819 linkonce sections, so that they will be merged with implicit
1820 instantiations; otherwise we get duplicate symbol errors.
1821 For Darwin we do not want explicit instantiations to be
1822 linkonce. */
1824 void
1825 maybe_make_one_only (tree decl)
1827 /* We used to say that this was not necessary on targets that support weak
1828 symbols, because the implicit instantiations will defer to the explicit
1829 one. However, that's not actually the case in SVR4; a strong definition
1830 after a weak one is an error. Also, not making explicit
1831 instantiations one_only means that we can end up with two copies of
1832 some template instantiations. */
1833 if (! flag_weak)
1834 return;
1836 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1837 we can get away with not emitting them if they aren't used. We need
1838 to for variables so that cp_finish_decl will update their linkage,
1839 because their DECL_INITIAL may not have been set properly yet. */
1841 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1842 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1843 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1845 make_decl_one_only (decl, cxx_comdat_group (decl));
1847 if (VAR_P (decl))
1849 varpool_node *node = varpool_node::get_create (decl);
1850 DECL_COMDAT (decl) = 1;
1851 /* Mark it needed so we don't forget to emit it. */
1852 node->forced_by_abi = true;
1853 TREE_USED (decl) = 1;
1858 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1859 This predicate will give the right answer during parsing of the
1860 function, which other tests may not. */
1862 bool
1863 vague_linkage_p (tree decl)
1865 /* Unfortunately, import_export_decl has not always been called
1866 before the function is processed, so we cannot simply check
1867 DECL_COMDAT. */
1868 if (DECL_COMDAT (decl)
1869 || (((TREE_CODE (decl) == FUNCTION_DECL
1870 && DECL_DECLARED_INLINE_P (decl))
1871 || (DECL_LANG_SPECIFIC (decl)
1872 && DECL_TEMPLATE_INSTANTIATION (decl)))
1873 && TREE_PUBLIC (decl)))
1874 return true;
1875 else if (DECL_FUNCTION_SCOPE_P (decl))
1876 /* A local static in an inline effectively has vague linkage. */
1877 return (TREE_STATIC (decl)
1878 && vague_linkage_p (DECL_CONTEXT (decl)));
1879 else
1880 return false;
1883 /* Determine whether or not we want to specifically import or export CTYPE,
1884 using various heuristics. */
1886 static void
1887 import_export_class (tree ctype)
1889 /* -1 for imported, 1 for exported. */
1890 int import_export = 0;
1892 /* It only makes sense to call this function at EOF. The reason is
1893 that this function looks at whether or not the first non-inline
1894 non-abstract virtual member function has been defined in this
1895 translation unit. But, we can't possibly know that until we've
1896 seen the entire translation unit. */
1897 gcc_assert (at_eof);
1899 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1900 return;
1902 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1903 we will have CLASSTYPE_INTERFACE_ONLY set but not
1904 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1905 heuristic because someone will supply a #pragma implementation
1906 elsewhere, and deducing it here would produce a conflict. */
1907 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1908 return;
1910 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1911 import_export = -1;
1912 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1913 import_export = 1;
1914 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1915 && !flag_implicit_templates)
1916 /* For a template class, without -fimplicit-templates, check the
1917 repository. If the virtual table is assigned to this
1918 translation unit, then export the class; otherwise, import
1919 it. */
1920 import_export = repo_export_class_p (ctype) ? 1 : -1;
1921 else if (TYPE_POLYMORPHIC_P (ctype))
1923 /* The ABI specifies that the virtual table and associated
1924 information are emitted with the key method, if any. */
1925 tree method = CLASSTYPE_KEY_METHOD (ctype);
1926 /* If weak symbol support is not available, then we must be
1927 careful not to emit the vtable when the key function is
1928 inline. An inline function can be defined in multiple
1929 translation units. If we were to emit the vtable in each
1930 translation unit containing a definition, we would get
1931 multiple definition errors at link-time. */
1932 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1933 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1936 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1937 a definition anywhere else. */
1938 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1939 import_export = 0;
1941 /* Allow back ends the chance to overrule the decision. */
1942 if (targetm.cxx.import_export_class)
1943 import_export = targetm.cxx.import_export_class (ctype, import_export);
1945 if (import_export)
1947 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1948 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1952 /* Return true if VAR has already been provided to the back end; in that
1953 case VAR should not be modified further by the front end. */
1954 static bool
1955 var_finalized_p (tree var)
1957 return varpool_node::get_create (var)->definition;
1960 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1961 must be emitted in this translation unit. Mark it as such. */
1963 void
1964 mark_needed (tree decl)
1966 TREE_USED (decl) = 1;
1967 if (TREE_CODE (decl) == FUNCTION_DECL)
1969 /* Extern inline functions don't become needed when referenced.
1970 If we know a method will be emitted in other TU and no new
1971 functions can be marked reachable, just use the external
1972 definition. */
1973 struct cgraph_node *node = cgraph_node::get_create (decl);
1974 node->forced_by_abi = true;
1976 /* #pragma interface and -frepo code can call mark_needed for
1977 maybe-in-charge 'tors; mark the clones as well. */
1978 tree clone;
1979 FOR_EACH_CLONE (clone, decl)
1980 mark_needed (clone);
1982 else if (TREE_CODE (decl) == VAR_DECL)
1984 varpool_node *node = varpool_node::get_create (decl);
1985 /* C++ frontend use mark_decl_references to force COMDAT variables
1986 to be output that might appear dead otherwise. */
1987 node->forced_by_abi = true;
1991 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1992 returns true if a definition of this entity should be provided in
1993 this object file. Callers use this function to determine whether
1994 or not to let the back end know that a definition of DECL is
1995 available in this translation unit. */
1997 bool
1998 decl_needed_p (tree decl)
2000 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2001 /* This function should only be called at the end of the translation
2002 unit. We cannot be sure of whether or not something will be
2003 COMDAT until that point. */
2004 gcc_assert (at_eof);
2006 /* All entities with external linkage that are not COMDAT/EXTERN should be
2007 emitted; they may be referred to from other object files. */
2008 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2009 return true;
2010 /* Functions marked "dllexport" must be emitted so that they are
2011 visible to other DLLs. */
2012 if (flag_keep_inline_dllexport
2013 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2014 return true;
2016 /* When not optimizing, do not bother to produce definitions for extern
2017 symbols. */
2018 if (DECL_REALLY_EXTERN (decl)
2019 && ((TREE_CODE (decl) != FUNCTION_DECL
2020 && !optimize)
2021 || (TREE_CODE (decl) == FUNCTION_DECL
2022 && !opt_for_fn (decl, optimize)))
2023 && !lookup_attribute ("always_inline", decl))
2024 return false;
2026 /* If this entity was used, let the back end see it; it will decide
2027 whether or not to emit it into the object file. */
2028 if (TREE_USED (decl))
2029 return true;
2030 /* Virtual functions might be needed for devirtualization. */
2031 if (flag_devirtualize
2032 && TREE_CODE (decl) == FUNCTION_DECL
2033 && DECL_VIRTUAL_P (decl))
2034 return true;
2035 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2036 reference to DECL might cause it to be emitted later. */
2037 return false;
2040 /* If necessary, write out the vtables for the dynamic class CTYPE.
2041 Returns true if any vtables were emitted. */
2043 static bool
2044 maybe_emit_vtables (tree ctype)
2046 tree vtbl;
2047 tree primary_vtbl;
2048 int needed = 0;
2049 varpool_node *current = NULL, *last = NULL;
2051 /* If the vtables for this class have already been emitted there is
2052 nothing more to do. */
2053 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2054 if (var_finalized_p (primary_vtbl))
2055 return false;
2056 /* Ignore dummy vtables made by get_vtable_decl. */
2057 if (TREE_TYPE (primary_vtbl) == void_type_node)
2058 return false;
2060 /* On some targets, we cannot determine the key method until the end
2061 of the translation unit -- which is when this function is
2062 called. */
2063 if (!targetm.cxx.key_method_may_be_inline ())
2064 determine_key_method (ctype);
2066 /* See if any of the vtables are needed. */
2067 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2069 import_export_decl (vtbl);
2070 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2071 needed = 1;
2073 if (!needed)
2075 /* If the references to this class' vtables are optimized away,
2076 still emit the appropriate debugging information. See
2077 dfs_debug_mark. */
2078 if (DECL_COMDAT (primary_vtbl)
2079 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2080 note_debug_info_needed (ctype);
2081 return false;
2084 /* The ABI requires that we emit all of the vtables if we emit any
2085 of them. */
2086 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2088 /* Mark entities references from the virtual table as used. */
2089 mark_vtable_entries (vtbl);
2091 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2093 vec<tree, va_gc> *cleanups = NULL;
2094 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2095 LOOKUP_NORMAL);
2097 /* It had better be all done at compile-time. */
2098 gcc_assert (!expr && !cleanups);
2101 /* Write it out. */
2102 DECL_EXTERNAL (vtbl) = 0;
2103 rest_of_decl_compilation (vtbl, 1, 1);
2105 /* Because we're only doing syntax-checking, we'll never end up
2106 actually marking the variable as written. */
2107 if (flag_syntax_only)
2108 TREE_ASM_WRITTEN (vtbl) = 1;
2109 else if (DECL_ONE_ONLY (vtbl))
2111 current = varpool_node::get_create (vtbl);
2112 if (last)
2113 current->add_to_same_comdat_group (last);
2114 last = current;
2118 /* Since we're writing out the vtable here, also write the debug
2119 info. */
2120 note_debug_info_needed (ctype);
2122 return true;
2125 /* A special return value from type_visibility meaning internal
2126 linkage. */
2128 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2130 /* walk_tree helper function for type_visibility. */
2132 static tree
2133 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2135 int *vis_p = (int *)data;
2136 if (! TYPE_P (*tp))
2138 *walk_subtrees = 0;
2140 else if (OVERLOAD_TYPE_P (*tp)
2141 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2143 *vis_p = VISIBILITY_ANON;
2144 return *tp;
2146 else if (CLASS_TYPE_P (*tp)
2147 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2148 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2149 return NULL;
2152 /* Returns the visibility of TYPE, which is the minimum visibility of its
2153 component types. */
2155 static int
2156 type_visibility (tree type)
2158 int vis = VISIBILITY_DEFAULT;
2159 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2160 return vis;
2163 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2164 specified (or if VISIBILITY is static). If TMPL is true, this
2165 constraint is for a template argument, and takes precedence
2166 over explicitly-specified visibility on the template. */
2168 static void
2169 constrain_visibility (tree decl, int visibility, bool tmpl)
2171 if (visibility == VISIBILITY_ANON)
2173 /* extern "C" declarations aren't affected by the anonymous
2174 namespace. */
2175 if (!DECL_EXTERN_C_P (decl))
2177 TREE_PUBLIC (decl) = 0;
2178 DECL_WEAK (decl) = 0;
2179 DECL_COMMON (decl) = 0;
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 if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2222 STRIP_NOPS (arg);
2223 if (TREE_CODE (arg) == ADDR_EXPR)
2224 arg = TREE_OPERAND (arg, 0);
2225 if (VAR_OR_FUNCTION_DECL_P (arg))
2227 if (! TREE_PUBLIC (arg))
2228 vis = VISIBILITY_ANON;
2229 else
2230 vis = DECL_VISIBILITY (arg);
2233 if (vis)
2234 constrain_visibility (decl, vis, true);
2238 /* Like c_determine_visibility, but with additional C++-specific
2239 behavior.
2241 Function-scope entities can rely on the function's visibility because
2242 it is set in start_preparsed_function.
2244 Class-scope entities cannot rely on the class's visibility until the end
2245 of the enclosing class definition.
2247 Note that because namespaces have multiple independent definitions,
2248 namespace visibility is handled elsewhere using the #pragma visibility
2249 machinery rather than by decorating the namespace declaration.
2251 The goal is for constraints from the type to give a diagnostic, and
2252 other constraints to be applied silently. */
2254 void
2255 determine_visibility (tree decl)
2257 tree class_type = NULL_TREE;
2258 bool use_template;
2259 bool orig_visibility_specified;
2260 enum symbol_visibility orig_visibility;
2262 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2264 /* Only relevant for names with external linkage. */
2265 if (!TREE_PUBLIC (decl))
2266 return;
2268 /* Cloned constructors and destructors get the same visibility as
2269 the underlying function. That should be set up in
2270 maybe_clone_body. */
2271 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2273 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2274 orig_visibility = DECL_VISIBILITY (decl);
2276 if (TREE_CODE (decl) == TYPE_DECL)
2278 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2279 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2280 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2281 use_template = 1;
2282 else
2283 use_template = 0;
2285 else if (DECL_LANG_SPECIFIC (decl))
2286 use_template = DECL_USE_TEMPLATE (decl);
2287 else
2288 use_template = 0;
2290 /* If DECL is a member of a class, visibility specifiers on the
2291 class can influence the visibility of the DECL. */
2292 if (DECL_CLASS_SCOPE_P (decl))
2293 class_type = DECL_CONTEXT (decl);
2294 else
2296 /* Not a class member. */
2298 /* Virtual tables have DECL_CONTEXT set to their associated class,
2299 so they are automatically handled above. */
2300 gcc_assert (!VAR_P (decl)
2301 || !DECL_VTABLE_OR_VTT_P (decl));
2303 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2305 /* Local statics and classes get the visibility of their
2306 containing function by default, except that
2307 -fvisibility-inlines-hidden doesn't affect them. */
2308 tree fn = DECL_CONTEXT (decl);
2309 if (DECL_VISIBILITY_SPECIFIED (fn))
2311 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2312 DECL_VISIBILITY_SPECIFIED (decl) =
2313 DECL_VISIBILITY_SPECIFIED (fn);
2315 else
2317 if (DECL_CLASS_SCOPE_P (fn))
2318 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2319 else if (determine_hidden_inline (fn))
2321 DECL_VISIBILITY (decl) = default_visibility;
2322 DECL_VISIBILITY_SPECIFIED (decl) =
2323 visibility_options.inpragma;
2325 else
2327 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2328 DECL_VISIBILITY_SPECIFIED (decl) =
2329 DECL_VISIBILITY_SPECIFIED (fn);
2333 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2334 but have no TEMPLATE_INFO, so don't try to check it. */
2335 use_template = 0;
2337 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2338 && flag_visibility_ms_compat)
2340 /* Under -fvisibility-ms-compat, types are visible by default,
2341 even though their contents aren't. */
2342 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2343 int underlying_vis = type_visibility (underlying_type);
2344 if (underlying_vis == VISIBILITY_ANON
2345 || (CLASS_TYPE_P (underlying_type)
2346 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2347 constrain_visibility (decl, underlying_vis, false);
2348 else
2349 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2351 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2353 /* tinfo visibility is based on the type it's for. */
2354 constrain_visibility
2355 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2357 /* Give the target a chance to override the visibility associated
2358 with DECL. */
2359 if (TREE_PUBLIC (decl)
2360 && !DECL_REALLY_EXTERN (decl)
2361 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2362 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2363 targetm.cxx.determine_class_data_visibility (decl);
2365 else if (use_template)
2366 /* Template instantiations and specializations get visibility based
2367 on their template unless they override it with an attribute. */;
2368 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2370 if (determine_hidden_inline (decl))
2371 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2372 else
2374 /* Set default visibility to whatever the user supplied with
2375 #pragma GCC visibility or a namespace visibility attribute. */
2376 DECL_VISIBILITY (decl) = default_visibility;
2377 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2382 if (use_template)
2384 /* If the specialization doesn't specify visibility, use the
2385 visibility from the template. */
2386 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2387 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2388 : DECL_TEMPLATE_INFO (decl));
2389 tree args = TI_ARGS (tinfo);
2390 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2391 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2392 : DECL_ATTRIBUTES (decl));
2394 if (args != error_mark_node)
2396 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2398 if (!DECL_VISIBILITY_SPECIFIED (decl))
2400 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2401 && determine_hidden_inline (decl))
2402 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2403 else
2405 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2406 DECL_VISIBILITY_SPECIFIED (decl)
2407 = DECL_VISIBILITY_SPECIFIED (pattern);
2411 if (args
2412 /* Template argument visibility outweighs #pragma or namespace
2413 visibility, but not an explicit attribute. */
2414 && !lookup_attribute ("visibility", attribs))
2416 int depth = TMPL_ARGS_DEPTH (args);
2417 if (DECL_VISIBILITY_SPECIFIED (decl))
2419 /* A class template member with explicit visibility
2420 overrides the class visibility, so we need to apply
2421 all the levels of template args directly. */
2422 int i;
2423 for (i = 1; i <= depth; ++i)
2425 tree lev = TMPL_ARGS_LEVEL (args, i);
2426 constrain_visibility_for_template (decl, lev);
2429 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2430 /* Limit visibility based on its template arguments. */
2431 constrain_visibility_for_template (decl, args);
2436 if (class_type)
2437 determine_visibility_from_class (decl, class_type);
2439 if (decl_anon_ns_mem_p (decl))
2440 /* Names in an anonymous namespace get internal linkage.
2441 This might change once we implement export. */
2442 constrain_visibility (decl, VISIBILITY_ANON, false);
2443 else if (TREE_CODE (decl) != TYPE_DECL)
2445 /* Propagate anonymity from type to decl. */
2446 int tvis = type_visibility (TREE_TYPE (decl));
2447 if (tvis == VISIBILITY_ANON
2448 || ! DECL_VISIBILITY_SPECIFIED (decl))
2449 constrain_visibility (decl, tvis, false);
2451 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2452 /* DR 757: A type without linkage shall not be used as the type of a
2453 variable or function with linkage, unless
2454 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2455 o the variable or function is not used (3.2 [basic.def.odr]) or is
2456 defined in the same translation unit.
2458 Since non-extern "C" decls need to be defined in the same
2459 translation unit, we can make the type internal. */
2460 constrain_visibility (decl, VISIBILITY_ANON, false);
2462 /* If visibility changed and DECL already has DECL_RTL, ensure
2463 symbol flags are updated. */
2464 if ((DECL_VISIBILITY (decl) != orig_visibility
2465 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2466 && ((VAR_P (decl) && TREE_STATIC (decl))
2467 || TREE_CODE (decl) == FUNCTION_DECL)
2468 && DECL_RTL_SET_P (decl))
2469 make_decl_rtl (decl);
2472 /* By default, static data members and function members receive
2473 the visibility of their containing class. */
2475 static void
2476 determine_visibility_from_class (tree decl, tree class_type)
2478 if (DECL_VISIBILITY_SPECIFIED (decl))
2479 return;
2481 if (determine_hidden_inline (decl))
2482 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2483 else
2485 /* Default to the class visibility. */
2486 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2487 DECL_VISIBILITY_SPECIFIED (decl)
2488 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2491 /* Give the target a chance to override the visibility associated
2492 with DECL. */
2493 if (VAR_P (decl)
2494 && (DECL_TINFO_P (decl)
2495 || (DECL_VTABLE_OR_VTT_P (decl)
2496 /* Construction virtual tables are not exported because
2497 they cannot be referred to from other object files;
2498 their name is not standardized by the ABI. */
2499 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2500 && TREE_PUBLIC (decl)
2501 && !DECL_REALLY_EXTERN (decl)
2502 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2503 targetm.cxx.determine_class_data_visibility (decl);
2506 /* Returns true iff DECL is an inline that should get hidden visibility
2507 because of -fvisibility-inlines-hidden. */
2509 static bool
2510 determine_hidden_inline (tree decl)
2512 return (visibility_options.inlines_hidden
2513 /* Don't do this for inline templates; specializations might not be
2514 inline, and we don't want them to inherit the hidden
2515 visibility. We'll set it here for all inline instantiations. */
2516 && !processing_template_decl
2517 && TREE_CODE (decl) == FUNCTION_DECL
2518 && DECL_DECLARED_INLINE_P (decl)
2519 && (! DECL_LANG_SPECIFIC (decl)
2520 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2523 /* Constrain the visibility of a class TYPE based on the visibility of its
2524 field types. Warn if any fields require lesser visibility. */
2526 void
2527 constrain_class_visibility (tree type)
2529 tree binfo;
2530 tree t;
2531 int i;
2533 int vis = type_visibility (type);
2535 if (vis == VISIBILITY_ANON
2536 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2537 return;
2539 /* Don't warn about visibility if the class has explicit visibility. */
2540 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2541 vis = VISIBILITY_INTERNAL;
2543 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2544 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2546 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2547 int subvis = type_visibility (ftype);
2549 if (subvis == VISIBILITY_ANON)
2551 if (!in_main_input_context ())
2552 warning (0, "\
2553 %qT has a field %qD whose type uses the anonymous namespace",
2554 type, t);
2556 else if (MAYBE_CLASS_TYPE_P (ftype)
2557 && vis < VISIBILITY_HIDDEN
2558 && subvis >= VISIBILITY_HIDDEN)
2559 warning (OPT_Wattributes, "\
2560 %qT declared with greater visibility than the type of its field %qD",
2561 type, t);
2564 binfo = TYPE_BINFO (type);
2565 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2567 int subvis = type_visibility (TREE_TYPE (t));
2569 if (subvis == VISIBILITY_ANON)
2571 if (!in_main_input_context())
2572 warning (0, "\
2573 %qT has a base %qT whose type uses the anonymous namespace",
2574 type, TREE_TYPE (t));
2576 else if (vis < VISIBILITY_HIDDEN
2577 && subvis >= VISIBILITY_HIDDEN)
2578 warning (OPT_Wattributes, "\
2579 %qT declared with greater visibility than its base %qT",
2580 type, TREE_TYPE (t));
2584 /* Functions for adjusting the visibility of a tagged type and its nested
2585 types and declarations when it gets a name for linkage purposes from a
2586 typedef. */
2588 static void bt_reset_linkage_1 (binding_entry, void *);
2589 static void bt_reset_linkage_2 (binding_entry, void *);
2591 /* First reset the visibility of all the types. */
2593 static void
2594 reset_type_linkage_1 (tree type)
2596 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2597 if (CLASS_TYPE_P (type))
2598 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2599 bt_reset_linkage_1, NULL);
2601 static void
2602 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2604 reset_type_linkage_1 (b->type);
2607 /* Then reset the visibility of any static data members or member
2608 functions that use those types. */
2610 static void
2611 reset_decl_linkage (tree decl)
2613 if (TREE_PUBLIC (decl))
2614 return;
2615 if (DECL_CLONED_FUNCTION_P (decl))
2616 return;
2617 TREE_PUBLIC (decl) = true;
2618 DECL_INTERFACE_KNOWN (decl) = false;
2619 determine_visibility (decl);
2620 tentative_decl_linkage (decl);
2622 static void
2623 reset_type_linkage_2 (tree type)
2625 if (CLASS_TYPE_P (type))
2627 if (tree vt = CLASSTYPE_VTABLES (type))
2629 tree name = mangle_vtbl_for_type (type);
2630 DECL_NAME (vt) = name;
2631 SET_DECL_ASSEMBLER_NAME (vt, name);
2632 reset_decl_linkage (vt);
2634 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2636 tree name = mangle_typeinfo_for_type (type);
2637 DECL_NAME (ti) = name;
2638 SET_DECL_ASSEMBLER_NAME (ti, name);
2639 TREE_TYPE (name) = type;
2640 reset_decl_linkage (ti);
2642 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2643 if (TREE_CODE (m) == VAR_DECL)
2644 reset_decl_linkage (m);
2645 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2646 reset_decl_linkage (m);
2647 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2648 bt_reset_linkage_2, NULL);
2651 static void
2652 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2654 reset_type_linkage_2 (b->type);
2656 void
2657 reset_type_linkage (tree type)
2659 reset_type_linkage_1 (type);
2660 reset_type_linkage_2 (type);
2663 /* Set up our initial idea of what the linkage of DECL should be. */
2665 void
2666 tentative_decl_linkage (tree decl)
2668 if (DECL_INTERFACE_KNOWN (decl))
2669 /* We've already made a decision as to how this function will
2670 be handled. */;
2671 else if (vague_linkage_p (decl))
2673 if (TREE_CODE (decl) == FUNCTION_DECL
2674 && decl_defined_p (decl))
2676 DECL_EXTERNAL (decl) = 1;
2677 DECL_NOT_REALLY_EXTERN (decl) = 1;
2678 note_vague_linkage_fn (decl);
2679 /* A non-template inline function with external linkage will
2680 always be COMDAT. As we must eventually determine the
2681 linkage of all functions, and as that causes writes to
2682 the data mapped in from the PCH file, it's advantageous
2683 to mark the functions at this point. */
2684 if (DECL_DECLARED_INLINE_P (decl)
2685 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2686 || DECL_DEFAULTED_FN (decl)))
2688 /* This function must have external linkage, as
2689 otherwise DECL_INTERFACE_KNOWN would have been
2690 set. */
2691 gcc_assert (TREE_PUBLIC (decl));
2692 comdat_linkage (decl);
2693 DECL_INTERFACE_KNOWN (decl) = 1;
2696 else if (TREE_CODE (decl) == VAR_DECL)
2697 maybe_commonize_var (decl);
2701 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2702 for DECL has not already been determined, do so now by setting
2703 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2704 function is called entities with vague linkage whose definitions
2705 are available must have TREE_PUBLIC set.
2707 If this function decides to place DECL in COMDAT, it will set
2708 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2709 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2710 callers defer that decision until it is clear that DECL is actually
2711 required. */
2713 void
2714 import_export_decl (tree decl)
2716 int emit_p;
2717 bool comdat_p;
2718 bool import_p;
2719 tree class_type = NULL_TREE;
2721 if (DECL_INTERFACE_KNOWN (decl))
2722 return;
2724 /* We cannot determine what linkage to give to an entity with vague
2725 linkage until the end of the file. For example, a virtual table
2726 for a class will be defined if and only if the key method is
2727 defined in this translation unit. As a further example, consider
2728 that when compiling a translation unit that uses PCH file with
2729 "-frepo" it would be incorrect to make decisions about what
2730 entities to emit when building the PCH; those decisions must be
2731 delayed until the repository information has been processed. */
2732 gcc_assert (at_eof);
2733 /* Object file linkage for explicit instantiations is handled in
2734 mark_decl_instantiated. For static variables in functions with
2735 vague linkage, maybe_commonize_var is used.
2737 Therefore, the only declarations that should be provided to this
2738 function are those with external linkage that are:
2740 * implicit instantiations of function templates
2742 * inline function
2744 * implicit instantiations of static data members of class
2745 templates
2747 * virtual tables
2749 * typeinfo objects
2751 Furthermore, all entities that reach this point must have a
2752 definition available in this translation unit.
2754 The following assertions check these conditions. */
2755 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2756 /* Any code that creates entities with TREE_PUBLIC cleared should
2757 also set DECL_INTERFACE_KNOWN. */
2758 gcc_assert (TREE_PUBLIC (decl));
2759 if (TREE_CODE (decl) == FUNCTION_DECL)
2760 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2761 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2762 || DECL_DECLARED_INLINE_P (decl));
2763 else
2764 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2765 || DECL_VTABLE_OR_VTT_P (decl)
2766 || DECL_TINFO_P (decl));
2767 /* Check that a definition of DECL is available in this translation
2768 unit. */
2769 gcc_assert (!DECL_REALLY_EXTERN (decl));
2771 /* Assume that DECL will not have COMDAT linkage. */
2772 comdat_p = false;
2773 /* Assume that DECL will not be imported into this translation
2774 unit. */
2775 import_p = false;
2777 /* See if the repository tells us whether or not to emit DECL in
2778 this translation unit. */
2779 emit_p = repo_emit_p (decl);
2780 if (emit_p == 0)
2781 import_p = true;
2782 else if (emit_p == 1)
2784 /* The repository indicates that this entity should be defined
2785 here. Make sure the back end honors that request. */
2786 mark_needed (decl);
2787 /* Output the definition as an ordinary strong definition. */
2788 DECL_EXTERNAL (decl) = 0;
2789 DECL_INTERFACE_KNOWN (decl) = 1;
2790 return;
2793 if (import_p)
2794 /* We have already decided what to do with this DECL; there is no
2795 need to check anything further. */
2797 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2799 class_type = DECL_CONTEXT (decl);
2800 import_export_class (class_type);
2801 if (TYPE_FOR_JAVA (class_type))
2802 import_p = true;
2803 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2804 && CLASSTYPE_INTERFACE_ONLY (class_type))
2805 import_p = true;
2806 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2807 && !CLASSTYPE_USE_TEMPLATE (class_type)
2808 && CLASSTYPE_KEY_METHOD (class_type)
2809 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2810 /* The ABI requires that all virtual tables be emitted with
2811 COMDAT linkage. However, on systems where COMDAT symbols
2812 don't show up in the table of contents for a static
2813 archive, or on systems without weak symbols (where we
2814 approximate COMDAT linkage by using internal linkage), the
2815 linker will report errors about undefined symbols because
2816 it will not see the virtual table definition. Therefore,
2817 in the case that we know that the virtual table will be
2818 emitted in only one translation unit, we make the virtual
2819 table an ordinary definition with external linkage. */
2820 DECL_EXTERNAL (decl) = 0;
2821 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2823 /* CLASS_TYPE is being exported from this translation unit,
2824 so DECL should be defined here. */
2825 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2826 /* If a class is declared in a header with the "extern
2827 template" extension, then it will not be instantiated,
2828 even in translation units that would normally require
2829 it. Often such classes are explicitly instantiated in
2830 one translation unit. Therefore, the explicit
2831 instantiation must be made visible to other translation
2832 units. */
2833 DECL_EXTERNAL (decl) = 0;
2834 else
2836 /* The generic C++ ABI says that class data is always
2837 COMDAT, even if there is a key function. Some
2838 variants (e.g., the ARM EABI) says that class data
2839 only has COMDAT linkage if the class data might be
2840 emitted in more than one translation unit. When the
2841 key method can be inline and is inline, we still have
2842 to arrange for comdat even though
2843 class_data_always_comdat is false. */
2844 if (!CLASSTYPE_KEY_METHOD (class_type)
2845 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2846 || targetm.cxx.class_data_always_comdat ())
2848 /* The ABI requires COMDAT linkage. Normally, we
2849 only emit COMDAT things when they are needed;
2850 make sure that we realize that this entity is
2851 indeed needed. */
2852 comdat_p = true;
2853 mark_needed (decl);
2857 else if (!flag_implicit_templates
2858 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2859 import_p = true;
2860 else
2861 comdat_p = true;
2863 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2865 tree type = TREE_TYPE (DECL_NAME (decl));
2866 if (CLASS_TYPE_P (type))
2868 class_type = type;
2869 import_export_class (type);
2870 if (CLASSTYPE_INTERFACE_KNOWN (type)
2871 && TYPE_POLYMORPHIC_P (type)
2872 && CLASSTYPE_INTERFACE_ONLY (type)
2873 /* If -fno-rtti was specified, then we cannot be sure
2874 that RTTI information will be emitted with the
2875 virtual table of the class, so we must emit it
2876 wherever it is used. */
2877 && flag_rtti)
2878 import_p = true;
2879 else
2881 if (CLASSTYPE_INTERFACE_KNOWN (type)
2882 && !CLASSTYPE_INTERFACE_ONLY (type))
2884 comdat_p = (targetm.cxx.class_data_always_comdat ()
2885 || (CLASSTYPE_KEY_METHOD (type)
2886 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2887 mark_needed (decl);
2888 if (!flag_weak)
2890 comdat_p = false;
2891 DECL_EXTERNAL (decl) = 0;
2894 else
2895 comdat_p = true;
2898 else
2899 comdat_p = true;
2901 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2903 /* DECL is an implicit instantiation of a function or static
2904 data member. */
2905 if ((flag_implicit_templates
2906 && !flag_use_repository)
2907 || (flag_implicit_inline_templates
2908 && TREE_CODE (decl) == FUNCTION_DECL
2909 && DECL_DECLARED_INLINE_P (decl)))
2910 comdat_p = true;
2911 else
2912 /* If we are not implicitly generating templates, then mark
2913 this entity as undefined in this translation unit. */
2914 import_p = true;
2916 else if (DECL_FUNCTION_MEMBER_P (decl))
2918 if (!DECL_DECLARED_INLINE_P (decl))
2920 tree ctype = DECL_CONTEXT (decl);
2921 import_export_class (ctype);
2922 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2924 DECL_NOT_REALLY_EXTERN (decl)
2925 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2926 || (DECL_DECLARED_INLINE_P (decl)
2927 && ! flag_implement_inlines
2928 && !DECL_VINDEX (decl)));
2930 if (!DECL_NOT_REALLY_EXTERN (decl))
2931 DECL_EXTERNAL (decl) = 1;
2933 /* Always make artificials weak. */
2934 if (DECL_ARTIFICIAL (decl) && flag_weak)
2935 comdat_p = true;
2936 else
2937 maybe_make_one_only (decl);
2940 else
2941 comdat_p = true;
2943 else
2944 comdat_p = true;
2946 if (import_p)
2948 /* If we are importing DECL into this translation unit, mark is
2949 an undefined here. */
2950 DECL_EXTERNAL (decl) = 1;
2951 DECL_NOT_REALLY_EXTERN (decl) = 0;
2953 else if (comdat_p)
2955 /* If we decided to put DECL in COMDAT, mark it accordingly at
2956 this point. */
2957 comdat_linkage (decl);
2960 DECL_INTERFACE_KNOWN (decl) = 1;
2963 /* Return an expression that performs the destruction of DECL, which
2964 must be a VAR_DECL whose type has a non-trivial destructor, or is
2965 an array whose (innermost) elements have a non-trivial destructor. */
2967 tree
2968 build_cleanup (tree decl)
2970 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2971 gcc_assert (clean != NULL_TREE);
2972 return clean;
2975 /* Returns the initialization guard variable for the variable DECL,
2976 which has static storage duration. */
2978 tree
2979 get_guard (tree decl)
2981 tree sname;
2982 tree guard;
2984 sname = mangle_guard_variable (decl);
2985 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2986 if (! guard)
2988 tree guard_type;
2990 /* We use a type that is big enough to contain a mutex as well
2991 as an integer counter. */
2992 guard_type = targetm.cxx.guard_type ();
2993 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2994 VAR_DECL, sname, guard_type);
2996 /* The guard should have the same linkage as what it guards. */
2997 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2998 TREE_STATIC (guard) = TREE_STATIC (decl);
2999 DECL_COMMON (guard) = DECL_COMMON (decl);
3000 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3001 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3002 if (DECL_ONE_ONLY (decl))
3003 make_decl_one_only (guard, cxx_comdat_group (guard));
3004 if (TREE_PUBLIC (decl))
3005 DECL_WEAK (guard) = DECL_WEAK (decl);
3006 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3007 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3009 DECL_ARTIFICIAL (guard) = 1;
3010 DECL_IGNORED_P (guard) = 1;
3011 TREE_USED (guard) = 1;
3012 pushdecl_top_level_and_finish (guard, NULL_TREE);
3014 return guard;
3017 /* Return those bits of the GUARD variable that should be set when the
3018 guarded entity is actually initialized. */
3020 static tree
3021 get_guard_bits (tree guard)
3023 if (!targetm.cxx.guard_mask_bit ())
3025 /* We only set the first byte of the guard, in order to leave room
3026 for a mutex in the high-order bits. */
3027 guard = build1 (ADDR_EXPR,
3028 build_pointer_type (TREE_TYPE (guard)),
3029 guard);
3030 guard = build1 (NOP_EXPR,
3031 build_pointer_type (char_type_node),
3032 guard);
3033 guard = build1 (INDIRECT_REF, char_type_node, guard);
3036 return guard;
3039 /* Return an expression which determines whether or not the GUARD
3040 variable has already been initialized. */
3042 tree
3043 get_guard_cond (tree guard)
3045 tree guard_value;
3047 /* Check to see if the GUARD is zero. */
3048 guard = get_guard_bits (guard);
3050 /* Mask off all but the low bit. */
3051 if (targetm.cxx.guard_mask_bit ())
3053 guard_value = integer_one_node;
3054 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3055 guard_value = convert (TREE_TYPE (guard), guard_value);
3056 guard = cp_build_binary_op (input_location,
3057 BIT_AND_EXPR, guard, guard_value,
3058 tf_warning_or_error);
3061 guard_value = integer_zero_node;
3062 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3063 guard_value = convert (TREE_TYPE (guard), guard_value);
3064 return cp_build_binary_op (input_location,
3065 EQ_EXPR, guard, guard_value,
3066 tf_warning_or_error);
3069 /* Return an expression which sets the GUARD variable, indicating that
3070 the variable being guarded has been initialized. */
3072 tree
3073 set_guard (tree guard)
3075 tree guard_init;
3077 /* Set the GUARD to one. */
3078 guard = get_guard_bits (guard);
3079 guard_init = integer_one_node;
3080 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3081 guard_init = convert (TREE_TYPE (guard), guard_init);
3082 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3083 tf_warning_or_error);
3086 /* Returns true iff we can tell that VAR does not have a dynamic
3087 initializer. */
3089 static bool
3090 var_defined_without_dynamic_init (tree var)
3092 /* If it's defined in another TU, we can't tell. */
3093 if (DECL_EXTERNAL (var))
3094 return false;
3095 /* If it has a non-trivial destructor, registering the destructor
3096 counts as dynamic initialization. */
3097 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3098 return false;
3099 /* If it's in this TU, its initializer has been processed. */
3100 gcc_assert (DECL_INITIALIZED_P (var));
3101 /* If it has no initializer or a constant one, it's not dynamic. */
3102 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3103 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3106 /* Returns true iff VAR is a variable that needs uses to be
3107 wrapped for possible dynamic initialization. */
3109 static bool
3110 var_needs_tls_wrapper (tree var)
3112 return (!error_operand_p (var)
3113 && DECL_THREAD_LOCAL_P (var)
3114 && !DECL_GNU_TLS_P (var)
3115 && !DECL_FUNCTION_SCOPE_P (var)
3116 && !var_defined_without_dynamic_init (var));
3119 /* Get the FUNCTION_DECL for the shared TLS init function for this
3120 translation unit. */
3122 static tree
3123 get_local_tls_init_fn (void)
3125 tree sname = get_identifier ("__tls_init");
3126 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3127 if (!fn)
3129 fn = build_lang_decl (FUNCTION_DECL, sname,
3130 build_function_type (void_type_node,
3131 void_list_node));
3132 SET_DECL_LANGUAGE (fn, lang_c);
3133 TREE_PUBLIC (fn) = false;
3134 DECL_ARTIFICIAL (fn) = true;
3135 mark_used (fn);
3136 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3138 return fn;
3141 /* Get a FUNCTION_DECL for the init function for the thread_local
3142 variable VAR. The init function will be an alias to the function
3143 that initializes all the non-local TLS variables in the translation
3144 unit. The init function is only used by the wrapper function. */
3146 static tree
3147 get_tls_init_fn (tree var)
3149 /* Only C++11 TLS vars need this init fn. */
3150 if (!var_needs_tls_wrapper (var))
3151 return NULL_TREE;
3153 /* If -fno-extern-tls-init, assume that we don't need to call
3154 a tls init function for a variable defined in another TU. */
3155 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3156 return NULL_TREE;
3158 #ifdef ASM_OUTPUT_DEF
3159 /* If the variable is internal, or if we can't generate aliases,
3160 call the local init function directly. */
3161 if (!TREE_PUBLIC (var))
3162 #endif
3163 return get_local_tls_init_fn ();
3165 tree sname = mangle_tls_init_fn (var);
3166 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3167 if (!fn)
3169 fn = build_lang_decl (FUNCTION_DECL, sname,
3170 build_function_type (void_type_node,
3171 void_list_node));
3172 SET_DECL_LANGUAGE (fn, lang_c);
3173 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3174 DECL_ARTIFICIAL (fn) = true;
3175 DECL_COMDAT (fn) = DECL_COMDAT (var);
3176 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3177 if (DECL_ONE_ONLY (var))
3178 make_decl_one_only (fn, cxx_comdat_group (fn));
3179 if (TREE_PUBLIC (var))
3181 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3182 /* If the variable is defined somewhere else and might have static
3183 initialization, make the init function a weak reference. */
3184 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3185 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3186 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3187 && DECL_EXTERNAL (var))
3188 declare_weak (fn);
3189 else
3190 DECL_WEAK (fn) = DECL_WEAK (var);
3192 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3193 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3194 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3195 DECL_IGNORED_P (fn) = 1;
3196 mark_used (fn);
3198 DECL_BEFRIENDING_CLASSES (fn) = var;
3200 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3202 return fn;
3205 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3206 variable VAR. The wrapper function calls the init function (if any) for
3207 VAR and then returns a reference to VAR. The wrapper function is used
3208 in place of VAR everywhere VAR is mentioned. */
3210 tree
3211 get_tls_wrapper_fn (tree var)
3213 /* Only C++11 TLS vars need this wrapper fn. */
3214 if (!var_needs_tls_wrapper (var))
3215 return NULL_TREE;
3217 tree sname = mangle_tls_wrapper_fn (var);
3218 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3219 if (!fn)
3221 /* A named rvalue reference is an lvalue, so the wrapper should
3222 always return an lvalue reference. */
3223 tree type = non_reference (TREE_TYPE (var));
3224 type = build_reference_type (type);
3225 tree fntype = build_function_type (type, void_list_node);
3226 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3227 SET_DECL_LANGUAGE (fn, lang_c);
3228 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3229 DECL_ARTIFICIAL (fn) = true;
3230 DECL_IGNORED_P (fn) = 1;
3231 /* The wrapper is inline and emitted everywhere var is used. */
3232 DECL_DECLARED_INLINE_P (fn) = true;
3233 if (TREE_PUBLIC (var))
3235 comdat_linkage (fn);
3236 #ifdef HAVE_GAS_HIDDEN
3237 /* Make the wrapper bind locally; there's no reason to share
3238 the wrapper between multiple shared objects. */
3239 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3240 DECL_VISIBILITY_SPECIFIED (fn) = true;
3241 #endif
3243 if (!TREE_PUBLIC (fn))
3244 DECL_INTERFACE_KNOWN (fn) = true;
3245 mark_used (fn);
3246 note_vague_linkage_fn (fn);
3248 #if 0
3249 /* We want CSE to commonize calls to the wrapper, but marking it as
3250 pure is unsafe since it has side-effects. I guess we need a new
3251 ECF flag even weaker than ECF_PURE. FIXME! */
3252 DECL_PURE_P (fn) = true;
3253 #endif
3255 DECL_BEFRIENDING_CLASSES (fn) = var;
3257 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3259 return fn;
3262 /* At EOF, generate the definition for the TLS wrapper function FN:
3264 T& var_wrapper() {
3265 if (init_fn) init_fn();
3266 return var;
3267 } */
3269 static void
3270 generate_tls_wrapper (tree fn)
3272 tree var = DECL_BEFRIENDING_CLASSES (fn);
3274 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3275 tree body = begin_function_body ();
3276 /* Only call the init fn if there might be one. */
3277 if (tree init_fn = get_tls_init_fn (var))
3279 tree if_stmt = NULL_TREE;
3280 /* If init_fn is a weakref, make sure it exists before calling. */
3281 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3283 if_stmt = begin_if_stmt ();
3284 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3285 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3286 NE_EXPR, addr, nullptr_node,
3287 tf_warning_or_error);
3288 finish_if_stmt_cond (cond, if_stmt);
3290 finish_expr_stmt (build_cxx_call
3291 (init_fn, 0, NULL, tf_warning_or_error));
3292 if (if_stmt)
3294 finish_then_clause (if_stmt);
3295 finish_if_stmt (if_stmt);
3298 else
3299 /* If there's no initialization, the wrapper is a constant function. */
3300 TREE_READONLY (fn) = true;
3301 finish_return_stmt (convert_from_reference (var));
3302 finish_function_body (body);
3303 expand_or_defer_fn (finish_function (0));
3306 /* Start the process of running a particular set of global constructors
3307 or destructors. Subroutine of do_[cd]tors. Also called from
3308 vtv_start_verification_constructor_init_function. */
3310 static tree
3311 start_objects (int method_type, int initp)
3313 tree body;
3314 tree fndecl;
3315 char type[14];
3317 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3319 if (initp != DEFAULT_INIT_PRIORITY)
3321 char joiner;
3323 #ifdef JOINER
3324 joiner = JOINER;
3325 #else
3326 joiner = '_';
3327 #endif
3329 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3331 else
3332 sprintf (type, "sub_%c", method_type);
3334 fndecl = build_lang_decl (FUNCTION_DECL,
3335 get_file_function_name (type),
3336 build_function_type_list (void_type_node,
3337 NULL_TREE));
3338 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3340 TREE_PUBLIC (current_function_decl) = 0;
3342 /* Mark as artificial because it's not explicitly in the user's
3343 source code. */
3344 DECL_ARTIFICIAL (current_function_decl) = 1;
3346 /* Mark this declaration as used to avoid spurious warnings. */
3347 TREE_USED (current_function_decl) = 1;
3349 /* Mark this function as a global constructor or destructor. */
3350 if (method_type == 'I')
3351 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3352 else
3353 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3355 body = begin_compound_stmt (BCS_FN_BODY);
3357 return body;
3360 /* Finish the process of running a particular set of global constructors
3361 or destructors. Subroutine of do_[cd]tors. */
3363 static void
3364 finish_objects (int method_type, int initp, tree body)
3366 tree fn;
3368 /* Finish up. */
3369 finish_compound_stmt (body);
3370 fn = finish_function (0);
3372 if (method_type == 'I')
3374 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3375 decl_init_priority_insert (fn, initp);
3377 else
3379 DECL_STATIC_DESTRUCTOR (fn) = 1;
3380 decl_fini_priority_insert (fn, initp);
3383 expand_or_defer_fn (fn);
3386 /* The names of the parameters to the function created to handle
3387 initializations and destructions for objects with static storage
3388 duration. */
3389 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3390 #define PRIORITY_IDENTIFIER "__priority"
3392 /* The name of the function we create to handle initializations and
3393 destructions for objects with static storage duration. */
3394 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3396 /* The declaration for the __INITIALIZE_P argument. */
3397 static GTY(()) tree initialize_p_decl;
3399 /* The declaration for the __PRIORITY argument. */
3400 static GTY(()) tree priority_decl;
3402 /* The declaration for the static storage duration function. */
3403 static GTY(()) tree ssdf_decl;
3405 /* All the static storage duration functions created in this
3406 translation unit. */
3407 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3409 /* A map from priority levels to information about that priority
3410 level. There may be many such levels, so efficient lookup is
3411 important. */
3412 static splay_tree priority_info_map;
3414 /* Begins the generation of the function that will handle all
3415 initialization and destruction of objects with static storage
3416 duration. The function generated takes two parameters of type
3417 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3418 nonzero, it performs initializations. Otherwise, it performs
3419 destructions. It only performs those initializations or
3420 destructions with the indicated __PRIORITY. The generated function
3421 returns no value.
3423 It is assumed that this function will only be called once per
3424 translation unit. */
3426 static tree
3427 start_static_storage_duration_function (unsigned count)
3429 tree type;
3430 tree body;
3431 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3433 /* Create the identifier for this function. It will be of the form
3434 SSDF_IDENTIFIER_<number>. */
3435 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3437 type = build_function_type_list (void_type_node,
3438 integer_type_node, integer_type_node,
3439 NULL_TREE);
3441 /* Create the FUNCTION_DECL itself. */
3442 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3443 get_identifier (id),
3444 type);
3445 TREE_PUBLIC (ssdf_decl) = 0;
3446 DECL_ARTIFICIAL (ssdf_decl) = 1;
3448 /* Put this function in the list of functions to be called from the
3449 static constructors and destructors. */
3450 if (!ssdf_decls)
3452 vec_alloc (ssdf_decls, 32);
3454 /* Take this opportunity to initialize the map from priority
3455 numbers to information about that priority level. */
3456 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3457 /*delete_key_fn=*/0,
3458 /*delete_value_fn=*/
3459 (splay_tree_delete_value_fn) &free);
3461 /* We always need to generate functions for the
3462 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3463 priorities later, we'll be sure to find the
3464 DEFAULT_INIT_PRIORITY. */
3465 get_priority_info (DEFAULT_INIT_PRIORITY);
3468 vec_safe_push (ssdf_decls, ssdf_decl);
3470 /* Create the argument list. */
3471 initialize_p_decl = cp_build_parm_decl
3472 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3473 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3474 TREE_USED (initialize_p_decl) = 1;
3475 priority_decl = cp_build_parm_decl
3476 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3477 DECL_CONTEXT (priority_decl) = ssdf_decl;
3478 TREE_USED (priority_decl) = 1;
3480 DECL_CHAIN (initialize_p_decl) = priority_decl;
3481 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3483 /* Put the function in the global scope. */
3484 pushdecl (ssdf_decl);
3486 /* Start the function itself. This is equivalent to declaring the
3487 function as:
3489 static void __ssdf (int __initialize_p, init __priority_p);
3491 It is static because we only need to call this function from the
3492 various constructor and destructor functions for this module. */
3493 start_preparsed_function (ssdf_decl,
3494 /*attrs=*/NULL_TREE,
3495 SF_PRE_PARSED);
3497 /* Set up the scope of the outermost block in the function. */
3498 body = begin_compound_stmt (BCS_FN_BODY);
3500 return body;
3503 /* Finish the generation of the function which performs initialization
3504 and destruction of objects with static storage duration. After
3505 this point, no more such objects can be created. */
3507 static void
3508 finish_static_storage_duration_function (tree body)
3510 /* Close out the function. */
3511 finish_compound_stmt (body);
3512 expand_or_defer_fn (finish_function (0));
3515 /* Return the information about the indicated PRIORITY level. If no
3516 code to handle this level has yet been generated, generate the
3517 appropriate prologue. */
3519 static priority_info
3520 get_priority_info (int priority)
3522 priority_info pi;
3523 splay_tree_node n;
3525 n = splay_tree_lookup (priority_info_map,
3526 (splay_tree_key) priority);
3527 if (!n)
3529 /* Create a new priority information structure, and insert it
3530 into the map. */
3531 pi = XNEW (struct priority_info_s);
3532 pi->initializations_p = 0;
3533 pi->destructions_p = 0;
3534 splay_tree_insert (priority_info_map,
3535 (splay_tree_key) priority,
3536 (splay_tree_value) pi);
3538 else
3539 pi = (priority_info) n->value;
3541 return pi;
3544 /* The effective initialization priority of a DECL. */
3546 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3547 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3548 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3550 /* Whether a DECL needs a guard to protect it against multiple
3551 initialization. */
3553 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3554 || DECL_ONE_ONLY (decl) \
3555 || DECL_WEAK (decl)))
3557 /* Called from one_static_initialization_or_destruction(),
3558 via walk_tree.
3559 Walks the initializer list of a global variable and looks for
3560 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3561 and that have their DECL_CONTEXT() == NULL.
3562 For each such temporary variable, set their DECL_CONTEXT() to
3563 the current function. This is necessary because otherwise
3564 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3565 when trying to refer to a temporary variable that does not have
3566 it's DECL_CONTECT() properly set. */
3567 static tree
3568 fix_temporary_vars_context_r (tree *node,
3569 int * /*unused*/,
3570 void * /*unused1*/)
3572 gcc_assert (current_function_decl);
3574 if (TREE_CODE (*node) == BIND_EXPR)
3576 tree var;
3578 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3579 if (VAR_P (var)
3580 && !DECL_NAME (var)
3581 && DECL_ARTIFICIAL (var)
3582 && !DECL_CONTEXT (var))
3583 DECL_CONTEXT (var) = current_function_decl;
3586 return NULL_TREE;
3589 /* Set up to handle the initialization or destruction of DECL. If
3590 INITP is nonzero, we are initializing the variable. Otherwise, we
3591 are destroying it. */
3593 static void
3594 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3596 tree guard_if_stmt = NULL_TREE;
3597 tree guard;
3599 /* If we are supposed to destruct and there's a trivial destructor,
3600 nothing has to be done. */
3601 if (!initp
3602 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3603 return;
3605 /* Trick the compiler into thinking we are at the file and line
3606 where DECL was declared so that error-messages make sense, and so
3607 that the debugger will show somewhat sensible file and line
3608 information. */
3609 input_location = DECL_SOURCE_LOCATION (decl);
3611 /* Make sure temporary variables in the initialiser all have
3612 their DECL_CONTEXT() set to a value different from NULL_TREE.
3613 This can happen when global variables initialisers are built.
3614 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3615 the temporary variables that might have been generated in the
3616 accompagning initialisers is NULL_TREE, meaning the variables have been
3617 declared in the global namespace.
3618 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3619 of the temporaries are set to the current function decl. */
3620 cp_walk_tree_without_duplicates (&init,
3621 fix_temporary_vars_context_r,
3622 NULL);
3624 /* Because of:
3626 [class.access.spec]
3628 Access control for implicit calls to the constructors,
3629 the conversion functions, or the destructor called to
3630 create and destroy a static data member is performed as
3631 if these calls appeared in the scope of the member's
3632 class.
3634 we pretend we are in a static member function of the class of
3635 which the DECL is a member. */
3636 if (member_p (decl))
3638 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3639 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3642 /* Assume we don't need a guard. */
3643 guard = NULL_TREE;
3644 /* We need a guard if this is an object with external linkage that
3645 might be initialized in more than one place. (For example, a
3646 static data member of a template, when the data member requires
3647 construction.) */
3648 if (NEEDS_GUARD_P (decl))
3650 tree guard_cond;
3652 guard = get_guard (decl);
3654 /* When using __cxa_atexit, we just check the GUARD as we would
3655 for a local static. */
3656 if (flag_use_cxa_atexit)
3658 /* When using __cxa_atexit, we never try to destroy
3659 anything from a static destructor. */
3660 gcc_assert (initp);
3661 guard_cond = get_guard_cond (guard);
3663 /* If we don't have __cxa_atexit, then we will be running
3664 destructors from .fini sections, or their equivalents. So,
3665 we need to know how many times we've tried to initialize this
3666 object. We do initializations only if the GUARD is zero,
3667 i.e., if we are the first to initialize the variable. We do
3668 destructions only if the GUARD is one, i.e., if we are the
3669 last to destroy the variable. */
3670 else if (initp)
3671 guard_cond
3672 = cp_build_binary_op (input_location,
3673 EQ_EXPR,
3674 cp_build_unary_op (PREINCREMENT_EXPR,
3675 guard,
3676 /*noconvert=*/1,
3677 tf_warning_or_error),
3678 integer_one_node,
3679 tf_warning_or_error);
3680 else
3681 guard_cond
3682 = cp_build_binary_op (input_location,
3683 EQ_EXPR,
3684 cp_build_unary_op (PREDECREMENT_EXPR,
3685 guard,
3686 /*noconvert=*/1,
3687 tf_warning_or_error),
3688 integer_zero_node,
3689 tf_warning_or_error);
3691 guard_if_stmt = begin_if_stmt ();
3692 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3696 /* If we're using __cxa_atexit, we have not already set the GUARD,
3697 so we must do so now. */
3698 if (guard && initp && flag_use_cxa_atexit)
3699 finish_expr_stmt (set_guard (guard));
3701 /* Perform the initialization or destruction. */
3702 if (initp)
3704 if (init)
3706 finish_expr_stmt (init);
3707 if (flag_sanitize & SANITIZE_ADDRESS)
3709 varpool_node *vnode = varpool_node::get (decl);
3710 if (vnode)
3711 vnode->dynamically_initialized = 1;
3715 /* If we're using __cxa_atexit, register a function that calls the
3716 destructor for the object. */
3717 if (flag_use_cxa_atexit)
3718 finish_expr_stmt (register_dtor_fn (decl));
3720 else
3721 finish_expr_stmt (build_cleanup (decl));
3723 /* Finish the guard if-stmt, if necessary. */
3724 if (guard)
3726 finish_then_clause (guard_if_stmt);
3727 finish_if_stmt (guard_if_stmt);
3730 /* Now that we're done with DECL we don't need to pretend to be a
3731 member of its class any longer. */
3732 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3733 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3736 /* Generate code to do the initialization or destruction of the decls in VARS,
3737 a TREE_LIST of VAR_DECL with static storage duration.
3738 Whether initialization or destruction is performed is specified by INITP. */
3740 static void
3741 do_static_initialization_or_destruction (tree vars, bool initp)
3743 tree node, init_if_stmt, cond;
3745 /* Build the outer if-stmt to check for initialization or destruction. */
3746 init_if_stmt = begin_if_stmt ();
3747 cond = initp ? integer_one_node : integer_zero_node;
3748 cond = cp_build_binary_op (input_location,
3749 EQ_EXPR,
3750 initialize_p_decl,
3751 cond,
3752 tf_warning_or_error);
3753 finish_if_stmt_cond (cond, init_if_stmt);
3755 /* To make sure dynamic construction doesn't access globals from other
3756 compilation units where they might not be yet constructed, for
3757 -fsanitize=address insert __asan_before_dynamic_init call that
3758 prevents access to either all global variables that need construction
3759 in other compilation units, or at least those that haven't been
3760 initialized yet. Variables that need dynamic construction in
3761 the current compilation unit are kept accessible. */
3762 if (flag_sanitize & SANITIZE_ADDRESS)
3763 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3765 node = vars;
3766 do {
3767 tree decl = TREE_VALUE (node);
3768 tree priority_if_stmt;
3769 int priority;
3770 priority_info pi;
3772 /* If we don't need a destructor, there's nothing to do. Avoid
3773 creating a possibly empty if-stmt. */
3774 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3776 node = TREE_CHAIN (node);
3777 continue;
3780 /* Remember that we had an initialization or finalization at this
3781 priority. */
3782 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3783 pi = get_priority_info (priority);
3784 if (initp)
3785 pi->initializations_p = 1;
3786 else
3787 pi->destructions_p = 1;
3789 /* Conditionalize this initialization on being in the right priority
3790 and being initializing/finalizing appropriately. */
3791 priority_if_stmt = begin_if_stmt ();
3792 cond = cp_build_binary_op (input_location,
3793 EQ_EXPR,
3794 priority_decl,
3795 build_int_cst (NULL_TREE, priority),
3796 tf_warning_or_error);
3797 finish_if_stmt_cond (cond, priority_if_stmt);
3799 /* Process initializers with same priority. */
3800 for (; node
3801 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3802 node = TREE_CHAIN (node))
3803 /* Do one initialization or destruction. */
3804 one_static_initialization_or_destruction (TREE_VALUE (node),
3805 TREE_PURPOSE (node), initp);
3807 /* Finish up the priority if-stmt body. */
3808 finish_then_clause (priority_if_stmt);
3809 finish_if_stmt (priority_if_stmt);
3811 } while (node);
3813 /* Revert what __asan_before_dynamic_init did by calling
3814 __asan_after_dynamic_init. */
3815 if (flag_sanitize & SANITIZE_ADDRESS)
3816 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3818 /* Finish up the init/destruct if-stmt body. */
3819 finish_then_clause (init_if_stmt);
3820 finish_if_stmt (init_if_stmt);
3823 /* VARS is a list of variables with static storage duration which may
3824 need initialization and/or finalization. Remove those variables
3825 that don't really need to be initialized or finalized, and return
3826 the resulting list. The order in which the variables appear in
3827 VARS is in reverse order of the order in which they should actually
3828 be initialized. The list we return is in the unreversed order;
3829 i.e., the first variable should be initialized first. */
3831 static tree
3832 prune_vars_needing_no_initialization (tree *vars)
3834 tree *var = vars;
3835 tree result = NULL_TREE;
3837 while (*var)
3839 tree t = *var;
3840 tree decl = TREE_VALUE (t);
3841 tree init = TREE_PURPOSE (t);
3843 /* Deal gracefully with error. */
3844 if (decl == error_mark_node)
3846 var = &TREE_CHAIN (t);
3847 continue;
3850 /* The only things that can be initialized are variables. */
3851 gcc_assert (VAR_P (decl));
3853 /* If this object is not defined, we don't need to do anything
3854 here. */
3855 if (DECL_EXTERNAL (decl))
3857 var = &TREE_CHAIN (t);
3858 continue;
3861 /* Also, if the initializer already contains errors, we can bail
3862 out now. */
3863 if (init && TREE_CODE (init) == TREE_LIST
3864 && value_member (error_mark_node, init))
3866 var = &TREE_CHAIN (t);
3867 continue;
3870 /* This variable is going to need initialization and/or
3871 finalization, so we add it to the list. */
3872 *var = TREE_CHAIN (t);
3873 TREE_CHAIN (t) = result;
3874 result = t;
3877 return result;
3880 /* Make sure we have told the back end about all the variables in
3881 VARS. */
3883 static void
3884 write_out_vars (tree vars)
3886 tree v;
3888 for (v = vars; v; v = TREE_CHAIN (v))
3890 tree var = TREE_VALUE (v);
3891 if (!var_finalized_p (var))
3893 import_export_decl (var);
3894 rest_of_decl_compilation (var, 1, 1);
3899 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3900 (otherwise) that will initialize all global objects with static
3901 storage duration having the indicated PRIORITY. */
3903 static void
3904 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3905 location_t *locus)
3907 char function_key;
3908 tree fndecl;
3909 tree body;
3910 size_t i;
3912 input_location = *locus;
3913 /* ??? */
3914 /* Was: locus->line++; */
3916 /* We use `I' to indicate initialization and `D' to indicate
3917 destruction. */
3918 function_key = constructor_p ? 'I' : 'D';
3920 /* We emit the function lazily, to avoid generating empty
3921 global constructors and destructors. */
3922 body = NULL_TREE;
3924 /* For Objective-C++, we may need to initialize metadata found in this module.
3925 This must be done _before_ any other static initializations. */
3926 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3927 && constructor_p && objc_static_init_needed_p ())
3929 body = start_objects (function_key, priority);
3930 objc_generate_static_init_call (NULL_TREE);
3933 /* Call the static storage duration function with appropriate
3934 arguments. */
3935 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3937 /* Calls to pure or const functions will expand to nothing. */
3938 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3940 tree call;
3942 if (! body)
3943 body = start_objects (function_key, priority);
3945 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3946 build_int_cst (NULL_TREE,
3947 constructor_p),
3948 build_int_cst (NULL_TREE,
3949 priority),
3950 NULL_TREE);
3951 finish_expr_stmt (call);
3955 /* Close out the function. */
3956 if (body)
3957 finish_objects (function_key, priority, body);
3960 /* Generate constructor and destructor functions for the priority
3961 indicated by N. */
3963 static int
3964 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3966 location_t *locus = (location_t *) data;
3967 int priority = (int) n->key;
3968 priority_info pi = (priority_info) n->value;
3970 /* Generate the functions themselves, but only if they are really
3971 needed. */
3972 if (pi->initializations_p)
3973 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3974 if (pi->destructions_p)
3975 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3977 /* Keep iterating. */
3978 return 0;
3981 /* Java requires that we be able to reference a local address for a
3982 method, and not be confused by PLT entries. If hidden aliases are
3983 supported, collect and return all the functions for which we should
3984 emit a hidden alias. */
3986 static hash_set<tree> *
3987 collect_candidates_for_java_method_aliases (void)
3989 struct cgraph_node *node;
3990 hash_set<tree> *candidates = NULL;
3992 #ifndef HAVE_GAS_HIDDEN
3993 return candidates;
3994 #endif
3996 FOR_EACH_FUNCTION (node)
3998 tree fndecl = node->decl;
4000 if (DECL_CLASS_SCOPE_P (fndecl)
4001 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4002 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4004 if (candidates == NULL)
4005 candidates = new hash_set<tree>;
4006 candidates->add (fndecl);
4010 return candidates;
4014 /* Java requires that we be able to reference a local address for a
4015 method, and not be confused by PLT entries. If hidden aliases are
4016 supported, emit one for each java function that we've emitted.
4017 CANDIDATES is the set of FUNCTION_DECLs that were gathered
4018 by collect_candidates_for_java_method_aliases. */
4020 static void
4021 build_java_method_aliases (hash_set<tree> *candidates)
4023 struct cgraph_node *node;
4025 #ifndef HAVE_GAS_HIDDEN
4026 return;
4027 #endif
4029 FOR_EACH_FUNCTION (node)
4031 tree fndecl = node->decl;
4033 if (TREE_ASM_WRITTEN (fndecl)
4034 && candidates->contains (fndecl))
4036 /* Mangle the name in a predictable way; we need to reference
4037 this from a java compiled object file. */
4038 tree oid, nid, alias;
4039 const char *oname;
4040 char *nname;
4042 oid = DECL_ASSEMBLER_NAME (fndecl);
4043 oname = IDENTIFIER_POINTER (oid);
4044 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4045 nname = ACONCAT (("_ZGA", oname+2, NULL));
4046 nid = get_identifier (nname);
4048 alias = make_alias_for (fndecl, nid);
4049 TREE_PUBLIC (alias) = 1;
4050 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4052 assemble_alias (alias, oid);
4057 /* Return C++ property of T, based on given operation OP. */
4059 static int
4060 cpp_check (tree t, cpp_operation op)
4062 switch (op)
4064 case IS_ABSTRACT:
4065 return DECL_PURE_VIRTUAL_P (t);
4066 case IS_CONSTRUCTOR:
4067 return DECL_CONSTRUCTOR_P (t);
4068 case IS_DESTRUCTOR:
4069 return DECL_DESTRUCTOR_P (t);
4070 case IS_COPY_CONSTRUCTOR:
4071 return DECL_COPY_CONSTRUCTOR_P (t);
4072 case IS_TEMPLATE:
4073 return TREE_CODE (t) == TEMPLATE_DECL;
4074 case IS_TRIVIAL:
4075 return trivial_type_p (t);
4076 default:
4077 return 0;
4081 /* Collect source file references recursively, starting from NAMESPC. */
4083 static void
4084 collect_source_refs (tree namespc)
4086 tree t;
4088 if (!namespc)
4089 return;
4091 /* Iterate over names in this name space. */
4092 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4093 if (!DECL_IS_BUILTIN (t) )
4094 collect_source_ref (DECL_SOURCE_FILE (t));
4096 /* Dump siblings, if any */
4097 collect_source_refs (TREE_CHAIN (namespc));
4099 /* Dump children, if any */
4100 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4103 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4104 starting from NAMESPC. */
4106 static void
4107 collect_ada_namespace (tree namespc, const char *source_file)
4109 if (!namespc)
4110 return;
4112 /* Collect decls from this namespace */
4113 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4115 /* Collect siblings, if any */
4116 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4118 /* Collect children, if any */
4119 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4122 /* Returns true iff there is a definition available for variable or
4123 function DECL. */
4125 static bool
4126 decl_defined_p (tree decl)
4128 if (TREE_CODE (decl) == FUNCTION_DECL)
4129 return (DECL_INITIAL (decl) != NULL_TREE);
4130 else
4132 gcc_assert (VAR_P (decl));
4133 return !DECL_EXTERNAL (decl);
4137 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4139 [expr.const]
4141 An integral constant-expression can only involve ... const
4142 variables of integral or enumeration types initialized with
4143 constant expressions ...
4145 C++0x also allows constexpr variables and temporaries initialized
4146 with constant expressions. We handle the former here, but the latter
4147 are just folded away in cxx_eval_constant_expression.
4149 The standard does not require that the expression be non-volatile.
4150 G++ implements the proposed correction in DR 457. */
4152 bool
4153 decl_constant_var_p (tree decl)
4155 if (!decl_maybe_constant_var_p (decl))
4156 return false;
4158 /* We don't know if a template static data member is initialized with
4159 a constant expression until we instantiate its initializer. Even
4160 in the case of a constexpr variable, we can't treat it as a
4161 constant until its initializer is complete in case it's used in
4162 its own initializer. */
4163 mark_used (decl);
4164 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4167 /* Returns true if DECL could be a symbolic constant variable, depending on
4168 its initializer. */
4170 bool
4171 decl_maybe_constant_var_p (tree decl)
4173 tree type = TREE_TYPE (decl);
4174 if (!VAR_P (decl))
4175 return false;
4176 if (DECL_DECLARED_CONSTEXPR_P (decl))
4177 return true;
4178 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4179 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4182 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4183 called from grokfndecl and grokvardecl; in all modes it is called from
4184 cp_write_global_declarations. */
4186 void
4187 no_linkage_error (tree decl)
4189 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4190 /* In C++11 it's ok if the decl is defined. */
4191 return;
4192 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4193 if (t == NULL_TREE)
4194 /* The type that got us on no_linkage_decls must have gotten a name for
4195 linkage purposes. */;
4196 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4197 /* The type might end up having a typedef name for linkage purposes. */
4198 vec_safe_push (no_linkage_decls, decl);
4199 else if (TYPE_ANONYMOUS_P (t))
4201 bool d = false;
4202 if (cxx_dialect >= cxx11)
4203 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4204 "anonymous type, is used but never defined", decl);
4205 else if (DECL_EXTERN_C_P (decl))
4206 /* Allow this; it's pretty common in C. */;
4207 else if (TREE_CODE (decl) == VAR_DECL)
4208 /* DRs 132, 319 and 389 seem to indicate types with
4209 no linkage can only be used to declare extern "C"
4210 entities. Since it's not always an error in the
4211 ISO C++ 90 Standard, we only issue a warning. */
4212 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4213 "with no linkage used to declare variable %q#D with "
4214 "linkage", decl);
4215 else
4216 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4217 "linkage used to declare function %q#D with linkage",
4218 decl);
4219 if (d && is_typedef_decl (TYPE_NAME (t)))
4220 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4221 "to the unqualified type, so it is not used for linkage",
4222 TYPE_NAME (t));
4224 else if (cxx_dialect >= cxx11)
4225 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4226 "%qT, is used but never defined", decl, t);
4227 else if (TREE_CODE (decl) == VAR_DECL)
4228 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4229 "used to declare variable %q#D with linkage", t, decl);
4230 else
4231 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4232 "to declare function %q#D with linkage", t, decl);
4235 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4237 static void
4238 collect_all_refs (const char *source_file)
4240 collect_ada_namespace (global_namespace, source_file);
4243 /* Clear DECL_EXTERNAL for NODE. */
4245 static bool
4246 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4248 DECL_EXTERNAL (node->decl) = 0;
4249 return false;
4252 /* Build up the function to run dynamic initializers for thread_local
4253 variables in this translation unit and alias the init functions for the
4254 individual variables to it. */
4256 static void
4257 handle_tls_init (void)
4259 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4260 if (vars == NULL_TREE)
4261 return;
4263 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4265 write_out_vars (vars);
4267 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4268 boolean_type_node);
4269 TREE_PUBLIC (guard) = false;
4270 TREE_STATIC (guard) = true;
4271 DECL_ARTIFICIAL (guard) = true;
4272 DECL_IGNORED_P (guard) = true;
4273 TREE_USED (guard) = true;
4274 set_decl_tls_model (guard, decl_default_tls_model (guard));
4275 pushdecl_top_level_and_finish (guard, NULL_TREE);
4277 tree fn = get_local_tls_init_fn ();
4278 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4279 tree body = begin_function_body ();
4280 tree if_stmt = begin_if_stmt ();
4281 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4282 tf_warning_or_error);
4283 finish_if_stmt_cond (cond, if_stmt);
4284 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4285 tf_warning_or_error));
4286 for (; vars; vars = TREE_CHAIN (vars))
4288 tree var = TREE_VALUE (vars);
4289 tree init = TREE_PURPOSE (vars);
4290 one_static_initialization_or_destruction (var, init, true);
4292 #ifdef ASM_OUTPUT_DEF
4293 /* Output init aliases even with -fno-extern-tls-init. */
4294 if (TREE_PUBLIC (var))
4296 tree single_init_fn = get_tls_init_fn (var);
4297 if (single_init_fn == NULL_TREE)
4298 continue;
4299 cgraph_node *alias
4300 = cgraph_node::get_create (fn)->create_same_body_alias
4301 (single_init_fn, fn);
4302 gcc_assert (alias != NULL);
4304 #endif
4307 finish_then_clause (if_stmt);
4308 finish_if_stmt (if_stmt);
4309 finish_function_body (body);
4310 expand_or_defer_fn (finish_function (0));
4313 /* The entire file is now complete. If requested, dump everything
4314 to a file. */
4316 static void
4317 dump_tu (void)
4319 int flags;
4320 FILE *stream = dump_begin (TDI_tu, &flags);
4322 if (stream)
4324 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4325 dump_end (TDI_tu, stream);
4329 /* Check the deallocation functions for CODE to see if we want to warn that
4330 only one was defined. */
4332 static void
4333 maybe_warn_sized_delete (enum tree_code code)
4335 tree sized = NULL_TREE;
4336 tree unsized = NULL_TREE;
4338 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4339 ovl; ovl = OVL_NEXT (ovl))
4341 tree fn = OVL_CURRENT (ovl);
4342 /* We're only interested in usual deallocation functions. */
4343 if (!non_placement_deallocation_fn_p (fn))
4344 continue;
4345 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4346 unsized = fn;
4347 else
4348 sized = fn;
4350 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4351 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4352 "the program should also define %qD", sized);
4353 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4354 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4355 "the program should also define %qD", unsized);
4358 /* Check the global deallocation functions to see if we want to warn about
4359 defining unsized without sized (or vice versa). */
4361 static void
4362 maybe_warn_sized_delete ()
4364 if (!flag_sized_deallocation || !warn_sized_deallocation)
4365 return;
4366 maybe_warn_sized_delete (DELETE_EXPR);
4367 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4370 /* This routine is called at the end of compilation.
4371 Its job is to create all the code needed to initialize and
4372 destroy the global aggregates. We do the destruction
4373 first, since that way we only need to reverse the decls once. */
4375 void
4376 cp_write_global_declarations (void)
4378 tree vars;
4379 bool reconsider;
4380 size_t i;
4381 location_t locus;
4382 unsigned ssdf_count = 0;
4383 int retries = 0;
4384 tree decl;
4385 hash_set<tree> *candidates;
4387 locus = input_location;
4388 at_eof = 1;
4390 /* Bad parse errors. Just forget about it. */
4391 if (! global_bindings_p () || current_class_type
4392 || !vec_safe_is_empty (decl_namespace_list))
4393 return;
4395 /* This is the point to write out a PCH if we're doing that.
4396 In that case we do not want to do anything else. */
4397 if (pch_file)
4399 c_common_write_pch ();
4400 dump_tu ();
4401 return;
4404 symtab->process_same_body_aliases ();
4406 /* Handle -fdump-ada-spec[-slim] */
4407 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4409 if (flag_dump_ada_spec_slim)
4410 collect_source_ref (main_input_filename);
4411 else
4412 collect_source_refs (global_namespace);
4414 dump_ada_specs (collect_all_refs, cpp_check);
4417 /* FIXME - huh? was input_line -= 1;*/
4419 timevar_start (TV_PHASE_DEFERRED);
4421 /* We now have to write out all the stuff we put off writing out.
4422 These include:
4424 o Template specializations that we have not yet instantiated,
4425 but which are needed.
4426 o Initialization and destruction for non-local objects with
4427 static storage duration. (Local objects with static storage
4428 duration are initialized when their scope is first entered,
4429 and are cleaned up via atexit.)
4430 o Virtual function tables.
4432 All of these may cause others to be needed. For example,
4433 instantiating one function may cause another to be needed, and
4434 generating the initializer for an object may cause templates to be
4435 instantiated, etc., etc. */
4437 emit_support_tinfos ();
4441 tree t;
4442 tree decl;
4444 reconsider = false;
4446 /* If there are templates that we've put off instantiating, do
4447 them now. */
4448 instantiate_pending_templates (retries);
4449 ggc_collect ();
4451 /* Write out virtual tables as required. Note that writing out
4452 the virtual table for a template class may cause the
4453 instantiation of members of that class. If we write out
4454 vtables then we remove the class from our list so we don't
4455 have to look at it again. */
4457 while (keyed_classes != NULL_TREE
4458 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4460 reconsider = true;
4461 keyed_classes = TREE_CHAIN (keyed_classes);
4464 t = keyed_classes;
4465 if (t != NULL_TREE)
4467 tree next = TREE_CHAIN (t);
4469 while (next)
4471 if (maybe_emit_vtables (TREE_VALUE (next)))
4473 reconsider = true;
4474 TREE_CHAIN (t) = TREE_CHAIN (next);
4476 else
4477 t = next;
4479 next = TREE_CHAIN (t);
4483 /* Write out needed type info variables. We have to be careful
4484 looping through unemitted decls, because emit_tinfo_decl may
4485 cause other variables to be needed. New elements will be
4486 appended, and we remove from the vector those that actually
4487 get emitted. */
4488 for (i = unemitted_tinfo_decls->length ();
4489 unemitted_tinfo_decls->iterate (--i, &t);)
4490 if (emit_tinfo_decl (t))
4492 reconsider = true;
4493 unemitted_tinfo_decls->unordered_remove (i);
4496 /* The list of objects with static storage duration is built up
4497 in reverse order. We clear STATIC_AGGREGATES so that any new
4498 aggregates added during the initialization of these will be
4499 initialized in the correct order when we next come around the
4500 loop. */
4501 vars = prune_vars_needing_no_initialization (&static_aggregates);
4503 if (vars)
4505 /* We need to start a new initialization function each time
4506 through the loop. That's because we need to know which
4507 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4508 isn't computed until a function is finished, and written
4509 out. That's a deficiency in the back end. When this is
4510 fixed, these initialization functions could all become
4511 inline, with resulting performance improvements. */
4512 tree ssdf_body;
4514 /* Set the line and file, so that it is obviously not from
4515 the source file. */
4516 input_location = locus;
4517 ssdf_body = start_static_storage_duration_function (ssdf_count);
4519 /* Make sure the back end knows about all the variables. */
4520 write_out_vars (vars);
4522 /* First generate code to do all the initializations. */
4523 if (vars)
4524 do_static_initialization_or_destruction (vars, /*initp=*/true);
4526 /* Then, generate code to do all the destructions. Do these
4527 in reverse order so that the most recently constructed
4528 variable is the first destroyed. If we're using
4529 __cxa_atexit, then we don't need to do this; functions
4530 were registered at initialization time to destroy the
4531 local statics. */
4532 if (!flag_use_cxa_atexit && vars)
4534 vars = nreverse (vars);
4535 do_static_initialization_or_destruction (vars, /*initp=*/false);
4537 else
4538 vars = NULL_TREE;
4540 /* Finish up the static storage duration function for this
4541 round. */
4542 input_location = locus;
4543 finish_static_storage_duration_function (ssdf_body);
4545 /* All those initializations and finalizations might cause
4546 us to need more inline functions, more template
4547 instantiations, etc. */
4548 reconsider = true;
4549 ssdf_count++;
4550 /* ??? was: locus.line++; */
4553 /* Now do the same for thread_local variables. */
4554 handle_tls_init ();
4556 /* Go through the set of inline functions whose bodies have not
4557 been emitted yet. If out-of-line copies of these functions
4558 are required, emit them. */
4559 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4561 /* Does it need synthesizing? */
4562 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4563 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4565 /* Even though we're already at the top-level, we push
4566 there again. That way, when we pop back a few lines
4567 hence, all of our state is restored. Otherwise,
4568 finish_function doesn't clean things up, and we end
4569 up with CURRENT_FUNCTION_DECL set. */
4570 push_to_top_level ();
4571 /* The decl's location will mark where it was first
4572 needed. Save that so synthesize method can indicate
4573 where it was needed from, in case of error */
4574 input_location = DECL_SOURCE_LOCATION (decl);
4575 synthesize_method (decl);
4576 pop_from_top_level ();
4577 reconsider = true;
4580 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4581 generate_tls_wrapper (decl);
4583 if (!DECL_SAVED_TREE (decl))
4584 continue;
4586 /* We lie to the back end, pretending that some functions
4587 are not defined when they really are. This keeps these
4588 functions from being put out unnecessarily. But, we must
4589 stop lying when the functions are referenced, or if they
4590 are not comdat since they need to be put out now. If
4591 DECL_INTERFACE_KNOWN, then we have already set
4592 DECL_EXTERNAL appropriately, so there's no need to check
4593 again, and we do not want to clear DECL_EXTERNAL if a
4594 previous call to import_export_decl set it.
4596 This is done in a separate for cycle, because if some
4597 deferred function is contained in another deferred
4598 function later in deferred_fns varray,
4599 rest_of_compilation would skip this function and we
4600 really cannot expand the same function twice. */
4601 import_export_decl (decl);
4602 if (DECL_NOT_REALLY_EXTERN (decl)
4603 && DECL_INITIAL (decl)
4604 && decl_needed_p (decl))
4606 struct cgraph_node *node, *next;
4608 node = cgraph_node::get (decl);
4609 if (node->cpp_implicit_alias)
4610 node = node->get_alias_target ();
4612 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4613 NULL, true);
4614 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4615 group, we need to mark all symbols in the same comdat group
4616 that way. */
4617 if (node->same_comdat_group)
4618 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4619 next != node;
4620 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4621 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4622 NULL, true);
4625 /* If we're going to need to write this function out, and
4626 there's already a body for it, create RTL for it now.
4627 (There might be no body if this is a method we haven't
4628 gotten around to synthesizing yet.) */
4629 if (!DECL_EXTERNAL (decl)
4630 && decl_needed_p (decl)
4631 && !TREE_ASM_WRITTEN (decl)
4632 && !cgraph_node::get (decl)->definition)
4634 /* We will output the function; no longer consider it in this
4635 loop. */
4636 DECL_DEFER_OUTPUT (decl) = 0;
4637 /* Generate RTL for this function now that we know we
4638 need it. */
4639 expand_or_defer_fn (decl);
4640 /* If we're compiling -fsyntax-only pretend that this
4641 function has been written out so that we don't try to
4642 expand it again. */
4643 if (flag_syntax_only)
4644 TREE_ASM_WRITTEN (decl) = 1;
4645 reconsider = true;
4649 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4650 reconsider = true;
4652 /* Static data members are just like namespace-scope globals. */
4653 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4655 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4656 /* Don't write it out if we haven't seen a definition. */
4657 || DECL_IN_AGGR_P (decl))
4658 continue;
4659 import_export_decl (decl);
4660 /* If this static data member is needed, provide it to the
4661 back end. */
4662 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4663 DECL_EXTERNAL (decl) = 0;
4665 if (vec_safe_length (pending_statics) != 0
4666 && wrapup_global_declarations (pending_statics->address (),
4667 pending_statics->length ()))
4668 reconsider = true;
4670 retries++;
4672 while (reconsider);
4674 /* All used inline functions must have a definition at this point. */
4675 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4677 if (/* Check online inline functions that were actually used. */
4678 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4679 /* If the definition actually was available here, then the
4680 fact that the function was not defined merely represents
4681 that for some reason (use of a template repository,
4682 #pragma interface, etc.) we decided not to emit the
4683 definition here. */
4684 && !DECL_INITIAL (decl)
4685 /* Don't complain if the template was defined. */
4686 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4687 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4688 (template_for_substitution (decl)))))
4690 warning (0, "inline function %q+D used but never defined", decl);
4691 /* Avoid a duplicate warning from check_global_declaration_1. */
4692 TREE_NO_WARNING (decl) = 1;
4696 /* So must decls that use a type with no linkage. */
4697 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4698 no_linkage_error (decl);
4700 maybe_warn_sized_delete ();
4702 /* Then, do the Objective-C stuff. This is where all the
4703 Objective-C module stuff gets generated (symtab,
4704 class/protocol/selector lists etc). This must be done after C++
4705 templates, destructors etc. so that selectors used in C++
4706 templates are properly allocated. */
4707 if (c_dialect_objc ())
4708 objc_write_global_declarations ();
4710 /* We give C linkage to static constructors and destructors. */
4711 push_lang_context (lang_name_c);
4713 /* Generate initialization and destruction functions for all
4714 priorities for which they are required. */
4715 if (priority_info_map)
4716 splay_tree_foreach (priority_info_map,
4717 generate_ctor_and_dtor_functions_for_priority,
4718 /*data=*/&locus);
4719 else if (c_dialect_objc () && objc_static_init_needed_p ())
4720 /* If this is obj-c++ and we need a static init, call
4721 generate_ctor_or_dtor_function. */
4722 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4723 DEFAULT_INIT_PRIORITY, &locus);
4725 /* We're done with the splay-tree now. */
4726 if (priority_info_map)
4727 splay_tree_delete (priority_info_map);
4729 /* Generate any missing aliases. */
4730 maybe_apply_pending_pragma_weaks ();
4732 /* We're done with static constructors, so we can go back to "C++"
4733 linkage now. */
4734 pop_lang_context ();
4736 /* Collect candidates for Java hidden aliases. */
4737 candidates = collect_candidates_for_java_method_aliases ();
4739 timevar_stop (TV_PHASE_DEFERRED);
4740 timevar_start (TV_PHASE_OPT_GEN);
4742 if (flag_vtable_verify)
4744 vtv_recover_class_info ();
4745 vtv_compute_class_hierarchy_transitive_closure ();
4746 vtv_build_vtable_verify_fndecl ();
4749 symtab->finalize_compilation_unit ();
4751 if (flag_vtable_verify)
4753 /* Generate the special constructor initialization function that
4754 calls __VLTRegisterPairs, and give it a very high
4755 initialization priority. This must be done after
4756 finalize_compilation_unit so that we have accurate
4757 information about which vtable will actually be emitted. */
4758 vtv_generate_init_routine ();
4761 timevar_stop (TV_PHASE_OPT_GEN);
4762 timevar_start (TV_PHASE_CHECK_DBGINFO);
4764 /* Now, issue warnings about static, but not defined, functions,
4765 etc., and emit debugging information. */
4766 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4767 if (vec_safe_length (pending_statics) != 0)
4769 check_global_declarations (pending_statics->address (),
4770 pending_statics->length ());
4771 emit_debug_global_declarations (pending_statics->address (),
4772 pending_statics->length ());
4775 perform_deferred_noexcept_checks ();
4777 /* Generate hidden aliases for Java. */
4778 if (candidates)
4780 build_java_method_aliases (candidates);
4781 delete candidates;
4784 finish_repo ();
4786 /* The entire file is now complete. If requested, dump everything
4787 to a file. */
4788 dump_tu ();
4790 if (flag_detailed_statistics)
4792 dump_tree_statistics ();
4793 dump_time_statistics ();
4795 input_location = locus;
4797 #ifdef ENABLE_CHECKING
4798 validate_conversion_obstack ();
4799 #endif /* ENABLE_CHECKING */
4801 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4804 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4805 function to call in parse-tree form; it has not yet been
4806 semantically analyzed. ARGS are the arguments to the function.
4807 They have already been semantically analyzed. This may change
4808 ARGS. */
4810 tree
4811 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4812 tsubst_flags_t complain)
4814 tree orig_fn;
4815 vec<tree, va_gc> *orig_args = NULL;
4816 tree expr;
4817 tree object;
4819 orig_fn = fn;
4820 object = TREE_OPERAND (fn, 0);
4822 if (processing_template_decl)
4824 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4825 || TREE_CODE (fn) == MEMBER_REF);
4826 if (type_dependent_expression_p (fn)
4827 || any_type_dependent_arguments_p (*args))
4828 return build_nt_call_vec (fn, *args);
4830 orig_args = make_tree_vector_copy (*args);
4832 /* Transform the arguments and add the implicit "this"
4833 parameter. That must be done before the FN is transformed
4834 because we depend on the form of FN. */
4835 make_args_non_dependent (*args);
4836 object = build_non_dependent_expr (object);
4837 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4839 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4840 object = cp_build_addr_expr (object, complain);
4841 vec_safe_insert (*args, 0, object);
4843 /* Now that the arguments are done, transform FN. */
4844 fn = build_non_dependent_expr (fn);
4847 /* A qualified name corresponding to a bound pointer-to-member is
4848 represented as an OFFSET_REF:
4850 struct B { void g(); };
4851 void (B::*p)();
4852 void B::g() { (this->*p)(); } */
4853 if (TREE_CODE (fn) == OFFSET_REF)
4855 tree object_addr = cp_build_addr_expr (object, complain);
4856 fn = TREE_OPERAND (fn, 1);
4857 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4858 complain);
4859 vec_safe_insert (*args, 0, object_addr);
4862 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4863 expr = build_op_call (fn, args, complain);
4864 else
4865 expr = cp_build_function_call_vec (fn, args, complain);
4866 if (processing_template_decl && expr != error_mark_node)
4867 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4869 if (orig_args != NULL)
4870 release_tree_vector (orig_args);
4872 return expr;
4876 void
4877 check_default_args (tree x)
4879 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4880 bool saw_def = false;
4881 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4882 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4884 if (TREE_PURPOSE (arg))
4885 saw_def = true;
4886 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4888 error ("default argument missing for parameter %P of %q+#D", i, x);
4889 TREE_PURPOSE (arg) = error_mark_node;
4894 /* Return true if function DECL can be inlined. This is used to force
4895 instantiation of methods that might be interesting for inlining. */
4896 bool
4897 possibly_inlined_p (tree decl)
4899 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4900 if (DECL_UNINLINABLE (decl))
4901 return false;
4902 if (!optimize || pragma_java_exceptions)
4903 return DECL_DECLARED_INLINE_P (decl);
4904 /* When optimizing, we might inline everything when flatten
4905 attribute or heuristics inlining for size or autoinlining
4906 is used. */
4907 return true;
4910 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4911 If DECL is a specialization or implicitly declared class member,
4912 generate the actual definition. Return false if something goes
4913 wrong, true otherwise. */
4915 bool
4916 mark_used (tree decl, tsubst_flags_t complain)
4918 /* If DECL is a BASELINK for a single function, then treat it just
4919 like the DECL for the function. Otherwise, if the BASELINK is
4920 for an overloaded function, we don't know which function was
4921 actually used until after overload resolution. */
4922 if (BASELINK_P (decl))
4924 decl = BASELINK_FUNCTIONS (decl);
4925 if (really_overloaded_fn (decl))
4926 return true;
4927 decl = OVL_CURRENT (decl);
4930 /* Set TREE_USED for the benefit of -Wunused. */
4931 TREE_USED (decl) = 1;
4932 if (DECL_CLONED_FUNCTION_P (decl))
4933 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4935 /* Mark enumeration types as used. */
4936 if (TREE_CODE (decl) == CONST_DECL)
4937 used_types_insert (DECL_CONTEXT (decl));
4939 if (TREE_CODE (decl) == FUNCTION_DECL)
4940 maybe_instantiate_noexcept (decl);
4942 if (TREE_CODE (decl) == FUNCTION_DECL
4943 && DECL_DELETED_FN (decl))
4945 if (DECL_ARTIFICIAL (decl))
4947 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4948 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4950 /* We mark a lambda conversion op as deleted if we can't
4951 generate it properly; see maybe_add_lambda_conv_op. */
4952 sorry ("converting lambda which uses %<...%> to "
4953 "function pointer");
4954 return false;
4957 if (complain & tf_error)
4959 error ("use of deleted function %qD", decl);
4960 if (!maybe_explain_implicit_delete (decl))
4961 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4963 return false;
4966 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
4967 && deprecated_state != DEPRECATED_SUPPRESS)
4968 warn_deprecated_use (decl, NULL_TREE);
4970 /* We can only check DECL_ODR_USED on variables or functions with
4971 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4972 might need special handling for. */
4973 if (!VAR_OR_FUNCTION_DECL_P (decl)
4974 || DECL_LANG_SPECIFIC (decl) == NULL
4975 || DECL_THUNK_P (decl))
4977 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4979 if (complain & tf_error)
4980 error ("use of %qD before deduction of %<auto%>", decl);
4981 return false;
4983 return true;
4986 /* We only want to do this processing once. We don't need to keep trying
4987 to instantiate inline templates, because unit-at-a-time will make sure
4988 we get them compiled before functions that want to inline them. */
4989 if (DECL_ODR_USED (decl))
4990 return true;
4992 /* If within finish_function, defer the rest until that function
4993 finishes, otherwise it might recurse. */
4994 if (defer_mark_used_calls)
4996 vec_safe_push (deferred_mark_used_calls, decl);
4997 return true;
5000 /* Normally, we can wait until instantiation-time to synthesize DECL.
5001 However, if DECL is a static data member initialized with a constant
5002 or a constexpr function, we need it right now because a reference to
5003 such a data member or a call to such function is not value-dependent.
5004 For a function that uses auto in the return type, we need to instantiate
5005 it to find out its type. For OpenMP user defined reductions, we need
5006 them instantiated for reduction clauses which inline them by hand
5007 directly. */
5008 if (DECL_LANG_SPECIFIC (decl)
5009 && DECL_TEMPLATE_INFO (decl)
5010 && (decl_maybe_constant_var_p (decl)
5011 || (TREE_CODE (decl) == FUNCTION_DECL
5012 && (DECL_DECLARED_CONSTEXPR_P (decl)
5013 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
5014 || undeduced_auto_decl (decl))
5015 && !uses_template_parms (DECL_TI_ARGS (decl)))
5017 /* Instantiating a function will result in garbage collection. We
5018 must treat this situation as if we were within the body of a
5019 function so as to avoid collecting live data only referenced from
5020 the stack (such as overload resolution candidates). */
5021 ++function_depth;
5022 instantiate_decl (decl, /*defer_ok=*/false,
5023 /*expl_inst_class_mem_p=*/false);
5024 --function_depth;
5027 if (processing_template_decl)
5028 return true;
5030 /* Check this too in case we're within instantiate_non_dependent_expr. */
5031 if (DECL_TEMPLATE_INFO (decl)
5032 && uses_template_parms (DECL_TI_ARGS (decl)))
5033 return true;
5035 require_deduced_type (decl);
5037 /* If we don't need a value, then we don't need to synthesize DECL. */
5038 if (cp_unevaluated_operand != 0)
5039 return true;
5041 DECL_ODR_USED (decl) = 1;
5042 if (DECL_CLONED_FUNCTION_P (decl))
5043 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5045 /* DR 757: A type without linkage shall not be used as the type of a
5046 variable or function with linkage, unless
5047 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5048 o the variable or function is not used (3.2 [basic.def.odr]) or is
5049 defined in the same translation unit. */
5050 if (cxx_dialect > cxx98
5051 && decl_linkage (decl) != lk_none
5052 && !DECL_EXTERN_C_P (decl)
5053 && !DECL_ARTIFICIAL (decl)
5054 && !decl_defined_p (decl)
5055 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5057 if (is_local_extern (decl))
5058 /* There's no way to define a local extern, and adding it to
5059 the vector interferes with GC, so give an error now. */
5060 no_linkage_error (decl);
5061 else
5062 vec_safe_push (no_linkage_decls, decl);
5065 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5066 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5067 /* Remember it, so we can check it was defined. */
5068 note_vague_linkage_fn (decl);
5070 /* Is it a synthesized method that needs to be synthesized? */
5071 if (TREE_CODE (decl) == FUNCTION_DECL
5072 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5073 && DECL_DEFAULTED_FN (decl)
5074 /* A function defaulted outside the class is synthesized either by
5075 cp_finish_decl or instantiate_decl. */
5076 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5077 && ! DECL_INITIAL (decl))
5079 /* Defer virtual destructors so that thunks get the right
5080 linkage. */
5081 if (DECL_VIRTUAL_P (decl) && !at_eof)
5083 note_vague_linkage_fn (decl);
5084 return true;
5087 /* Remember the current location for a function we will end up
5088 synthesizing. Then we can inform the user where it was
5089 required in the case of error. */
5090 DECL_SOURCE_LOCATION (decl) = input_location;
5092 /* Synthesizing an implicitly defined member function will result in
5093 garbage collection. We must treat this situation as if we were
5094 within the body of a function so as to avoid collecting live data
5095 on the stack (such as overload resolution candidates).
5097 We could just let cp_write_global_declarations handle synthesizing
5098 this function by adding it to deferred_fns, but doing
5099 it at the use site produces better error messages. */
5100 ++function_depth;
5101 synthesize_method (decl);
5102 --function_depth;
5103 /* If this is a synthesized method we don't need to
5104 do the instantiation test below. */
5106 else if (VAR_OR_FUNCTION_DECL_P (decl)
5107 && DECL_TEMPLATE_INFO (decl)
5108 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5109 || always_instantiate_p (decl)))
5110 /* If this is a function or variable that is an instance of some
5111 template, we now know that we will need to actually do the
5112 instantiation. We check that DECL is not an explicit
5113 instantiation because that is not checked in instantiate_decl.
5115 We put off instantiating functions in order to improve compile
5116 times. Maintaining a stack of active functions is expensive,
5117 and the inliner knows to instantiate any functions it might
5118 need. Therefore, we always try to defer instantiation. */
5120 ++function_depth;
5121 instantiate_decl (decl, /*defer_ok=*/true,
5122 /*expl_inst_class_mem_p=*/false);
5123 --function_depth;
5126 return true;
5129 bool
5130 mark_used (tree decl)
5132 return mark_used (decl, tf_warning_or_error);
5135 tree
5136 vtv_start_verification_constructor_init_function (void)
5138 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5141 tree
5142 vtv_finish_verification_constructor_init_function (tree function_body)
5144 tree fn;
5146 finish_compound_stmt (function_body);
5147 fn = finish_function (0);
5148 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5149 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5151 return fn;
5154 #include "gt-cp-decl2.h"