* include/bits/stl_list.h (_M_resize_pos(size_type&)): Declare.
[official-gcc.git] / gcc / cp / decl2.c
blob27bacabb8472033d09cc45665adc75c80a4b1746
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "stringpool.h"
44 #include "varasm.h"
45 #include "attribs.h"
46 #include "stor-layout.h"
47 #include "calls.h"
48 #include "flags.h"
49 #include "cp-tree.h"
50 #include "decl.h"
51 #include "toplev.h"
52 #include "timevar.h"
53 #include "cpplib.h"
54 #include "target.h"
55 #include "c-family/c-common.h"
56 #include "c-family/c-objc.h"
57 #include "hash-map.h"
58 #include "is-a.h"
59 #include "plugin-api.h"
60 #include "hard-reg-set.h"
61 #include "input.h"
62 #include "function.h"
63 #include "ipa-ref.h"
64 #include "cgraph.h"
65 #include "tree-inline.h"
66 #include "c-family/c-pragma.h"
67 #include "dumpfile.h"
68 #include "intl.h"
69 #include "splay-tree.h"
70 #include "langhooks.h"
71 #include "c-family/c-ada-spec.h"
72 #include "asan.h"
74 extern cpp_reader *parse_in;
76 /* This structure contains information about the initializations
77 and/or destructions required for a particular priority level. */
78 typedef struct priority_info_s {
79 /* Nonzero if there have been any initializations at this priority
80 throughout the translation unit. */
81 int initializations_p;
82 /* Nonzero if there have been any destructions at this priority
83 throughout the translation unit. */
84 int destructions_p;
85 } *priority_info;
87 static void mark_vtable_entries (tree);
88 static bool maybe_emit_vtables (tree);
89 static bool acceptable_java_type (tree);
90 static tree start_objects (int, int);
91 static void finish_objects (int, int, tree);
92 static tree start_static_storage_duration_function (unsigned);
93 static void finish_static_storage_duration_function (tree);
94 static priority_info get_priority_info (int);
95 static void do_static_initialization_or_destruction (tree, bool);
96 static void one_static_initialization_or_destruction (tree, tree, bool);
97 static void generate_ctor_or_dtor_function (bool, int, location_t *);
98 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
99 void *);
100 static tree prune_vars_needing_no_initialization (tree *);
101 static void write_out_vars (tree);
102 static void import_export_class (tree);
103 static tree get_guard_bits (tree);
104 static void determine_visibility_from_class (tree, tree);
105 static bool determine_hidden_inline (tree);
106 static bool decl_defined_p (tree);
108 /* A list of static class variables. This is needed, because a
109 static class variable can be declared inside the class without
110 an initializer, and then initialized, statically, outside the class. */
111 static GTY(()) vec<tree, va_gc> *pending_statics;
113 /* A list of functions which were declared inline, but which we
114 may need to emit outline anyway. */
115 static GTY(()) vec<tree, va_gc> *deferred_fns;
117 /* A list of decls that use types with no linkage, which we need to make
118 sure are defined. */
119 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
121 /* A vector of alternating decls and identifiers, where the latter
122 is to be an alias for the former if the former is defined. */
123 static GTY(()) vec<tree, va_gc> *mangling_aliases;
125 /* Nonzero if we're done parsing and into end-of-file activities. */
127 int at_eof;
130 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
131 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
132 that apply to the function). */
134 tree
135 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
136 cp_ref_qualifier rqual)
138 tree raises;
139 tree attrs;
140 int type_quals;
141 bool late_return_type_p;
143 if (fntype == error_mark_node || ctype == error_mark_node)
144 return error_mark_node;
146 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
147 || TREE_CODE (fntype) == METHOD_TYPE);
149 type_quals = quals & ~TYPE_QUAL_RESTRICT;
150 ctype = cp_build_qualified_type (ctype, type_quals);
151 raises = TYPE_RAISES_EXCEPTIONS (fntype);
152 attrs = TYPE_ATTRIBUTES (fntype);
153 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
154 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
155 (TREE_CODE (fntype) == METHOD_TYPE
156 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
157 : TYPE_ARG_TYPES (fntype)));
158 if (attrs)
159 fntype = cp_build_type_attribute_variant (fntype, attrs);
160 if (rqual)
161 fntype = build_ref_qualified_type (fntype, rqual);
162 if (raises)
163 fntype = build_exception_variant (fntype, raises);
164 if (late_return_type_p)
165 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
167 return fntype;
170 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
171 return type changed to NEW_RET. */
173 tree
174 change_return_type (tree new_ret, tree fntype)
176 tree newtype;
177 tree args = TYPE_ARG_TYPES (fntype);
178 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
179 tree attrs = TYPE_ATTRIBUTES (fntype);
180 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
182 if (new_ret == error_mark_node)
183 return fntype;
185 if (same_type_p (new_ret, TREE_TYPE (fntype)))
186 return fntype;
188 if (TREE_CODE (fntype) == FUNCTION_TYPE)
190 newtype = build_function_type (new_ret, args);
191 newtype = apply_memfn_quals (newtype,
192 type_memfn_quals (fntype),
193 type_memfn_rqual (fntype));
195 else
196 newtype = build_method_type_directly
197 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
198 if (raises)
199 newtype = build_exception_variant (newtype, raises);
200 if (attrs)
201 newtype = cp_build_type_attribute_variant (newtype, attrs);
202 if (late_return_type_p)
203 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
205 return newtype;
208 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
209 appropriately. */
211 tree
212 cp_build_parm_decl (tree name, tree type)
214 tree parm = build_decl (input_location,
215 PARM_DECL, name, type);
216 /* DECL_ARG_TYPE is only used by the back end and the back end never
217 sees templates. */
218 if (!processing_template_decl)
219 DECL_ARG_TYPE (parm) = type_passed_as (type);
221 return parm;
224 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
225 indicated NAME. */
227 tree
228 build_artificial_parm (tree name, tree type)
230 tree parm = cp_build_parm_decl (name, type);
231 DECL_ARTIFICIAL (parm) = 1;
232 /* All our artificial parms are implicitly `const'; they cannot be
233 assigned to. */
234 TREE_READONLY (parm) = 1;
235 return parm;
238 /* Constructors for types with virtual baseclasses need an "in-charge" flag
239 saying whether this constructor is responsible for initialization of
240 virtual baseclasses or not. All destructors also need this "in-charge"
241 flag, which additionally determines whether or not the destructor should
242 free the memory for the object.
244 This function adds the "in-charge" flag to member function FN if
245 appropriate. It is called from grokclassfn and tsubst.
246 FN must be either a constructor or destructor.
248 The in-charge flag follows the 'this' parameter, and is followed by the
249 VTT parm (if any), then the user-written parms. */
251 void
252 maybe_retrofit_in_chrg (tree fn)
254 tree basetype, arg_types, parms, parm, fntype;
256 /* If we've already add the in-charge parameter don't do it again. */
257 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
258 return;
260 /* When processing templates we can't know, in general, whether or
261 not we're going to have virtual baseclasses. */
262 if (processing_template_decl)
263 return;
265 /* We don't need an in-charge parameter for constructors that don't
266 have virtual bases. */
267 if (DECL_CONSTRUCTOR_P (fn)
268 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
269 return;
271 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
272 basetype = TREE_TYPE (TREE_VALUE (arg_types));
273 arg_types = TREE_CHAIN (arg_types);
275 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
277 /* If this is a subobject constructor or destructor, our caller will
278 pass us a pointer to our VTT. */
279 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
281 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
283 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
284 DECL_CHAIN (parm) = parms;
285 parms = parm;
287 /* ...and then to TYPE_ARG_TYPES. */
288 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
290 DECL_HAS_VTT_PARM_P (fn) = 1;
293 /* Then add the in-charge parm (before the VTT parm). */
294 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
295 DECL_CHAIN (parm) = parms;
296 parms = parm;
297 arg_types = hash_tree_chain (integer_type_node, arg_types);
299 /* Insert our new parameter(s) into the list. */
300 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
302 /* And rebuild the function type. */
303 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
304 arg_types);
305 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
306 fntype = build_exception_variant (fntype,
307 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
308 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
309 fntype = (cp_build_type_attribute_variant
310 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
311 TREE_TYPE (fn) = fntype;
313 /* Now we've got the in-charge parameter. */
314 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
317 /* Classes overload their constituent function names automatically.
318 When a function name is declared in a record structure,
319 its name is changed to it overloaded name. Since names for
320 constructors and destructors can conflict, we place a leading
321 '$' for destructors.
323 CNAME is the name of the class we are grokking for.
325 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
327 FLAGS contains bits saying what's special about today's
328 arguments. DTOR_FLAG == DESTRUCTOR.
330 If FUNCTION is a destructor, then we must add the `auto-delete' field
331 as a second parameter. There is some hair associated with the fact
332 that we must "declare" this variable in the manner consistent with the
333 way the rest of the arguments were declared.
335 QUALS are the qualifiers for the this pointer. */
337 void
338 grokclassfn (tree ctype, tree function, enum overload_flags flags)
340 tree fn_name = DECL_NAME (function);
342 /* Even within an `extern "C"' block, members get C++ linkage. See
343 [dcl.link] for details. */
344 SET_DECL_LANGUAGE (function, lang_cplusplus);
346 if (fn_name == NULL_TREE)
348 error ("name missing for member function");
349 fn_name = get_identifier ("<anonymous>");
350 DECL_NAME (function) = fn_name;
353 DECL_CONTEXT (function) = ctype;
355 if (flags == DTOR_FLAG)
356 DECL_DESTRUCTOR_P (function) = 1;
358 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
359 maybe_retrofit_in_chrg (function);
362 /* Create an ARRAY_REF, checking for the user doing things backwards
363 along the way. DECLTYPE_P is for N3276, as in the parser. */
365 tree
366 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
367 bool decltype_p)
369 tree type;
370 tree expr;
371 tree orig_array_expr = array_expr;
372 tree orig_index_exp = index_exp;
374 if (error_operand_p (array_expr) || error_operand_p (index_exp))
375 return error_mark_node;
377 if (processing_template_decl)
379 if (type_dependent_expression_p (array_expr)
380 || type_dependent_expression_p (index_exp))
381 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
382 NULL_TREE, NULL_TREE);
383 array_expr = build_non_dependent_expr (array_expr);
384 index_exp = build_non_dependent_expr (index_exp);
387 type = TREE_TYPE (array_expr);
388 gcc_assert (type);
389 type = non_reference (type);
391 /* If they have an `operator[]', use that. */
392 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
394 tsubst_flags_t complain = tf_warning_or_error;
395 if (decltype_p)
396 complain |= tf_decltype;
397 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
398 index_exp, NULL_TREE, /*overload=*/NULL, complain);
400 else
402 tree p1, p2, i1, i2;
404 /* Otherwise, create an ARRAY_REF for a pointer or array type.
405 It is a little-known fact that, if `a' is an array and `i' is
406 an int, you can write `i[a]', which means the same thing as
407 `a[i]'. */
408 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
409 p1 = array_expr;
410 else
411 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
413 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
414 p2 = index_exp;
415 else
416 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
418 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
419 false);
420 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
421 false);
423 if ((p1 && i2) && (i1 && p2))
424 error ("ambiguous conversion for array subscript");
426 if (p1 && i2)
427 array_expr = p1, index_exp = i2;
428 else if (i1 && p2)
429 array_expr = p2, index_exp = i1;
430 else
432 error ("invalid types %<%T[%T]%> for array subscript",
433 type, TREE_TYPE (index_exp));
434 return error_mark_node;
437 if (array_expr == error_mark_node || index_exp == error_mark_node)
438 error ("ambiguous conversion for array subscript");
440 expr = build_array_ref (input_location, array_expr, index_exp);
442 if (processing_template_decl && expr != error_mark_node)
443 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
444 NULL_TREE, NULL_TREE);
445 return expr;
448 /* Given the cast expression EXP, checking out its validity. Either return
449 an error_mark_node if there was an unavoidable error, return a cast to
450 void for trying to delete a pointer w/ the value 0, or return the
451 call to delete. If DOING_VEC is true, we handle things differently
452 for doing an array delete.
453 Implements ARM $5.3.4. This is called from the parser. */
455 tree
456 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
457 tsubst_flags_t complain)
459 tree t, type;
461 if (exp == error_mark_node)
462 return exp;
464 if (processing_template_decl)
466 t = build_min (DELETE_EXPR, void_type_node, exp, size);
467 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
468 DELETE_EXPR_USE_VEC (t) = doing_vec;
469 TREE_SIDE_EFFECTS (t) = 1;
470 return t;
473 /* An array can't have been allocated by new, so complain. */
474 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
475 warning (0, "deleting array %q#E", exp);
477 t = build_expr_type_conversion (WANT_POINTER, exp, true);
479 if (t == NULL_TREE || t == error_mark_node)
481 error ("type %q#T argument given to %<delete%>, expected pointer",
482 TREE_TYPE (exp));
483 return error_mark_node;
486 type = TREE_TYPE (t);
488 /* As of Valley Forge, you can delete a pointer to const. */
490 /* You can't delete functions. */
491 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
493 error ("cannot delete a function. Only pointer-to-objects are "
494 "valid arguments to %<delete%>");
495 return error_mark_node;
498 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
499 if (VOID_TYPE_P (TREE_TYPE (type)))
501 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
502 doing_vec = 0;
505 /* Deleting a pointer with the value zero is valid and has no effect. */
506 if (integer_zerop (t))
507 return build1 (NOP_EXPR, void_type_node, t);
509 if (doing_vec)
510 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
511 sfk_deleting_destructor,
512 use_global_delete, complain);
513 else
514 return build_delete (type, t, sfk_deleting_destructor,
515 LOOKUP_NORMAL, use_global_delete,
516 complain);
519 /* Report an error if the indicated template declaration is not the
520 sort of thing that should be a member template. */
522 void
523 check_member_template (tree tmpl)
525 tree decl;
527 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
528 decl = DECL_TEMPLATE_RESULT (tmpl);
530 if (TREE_CODE (decl) == FUNCTION_DECL
531 || DECL_ALIAS_TEMPLATE_P (tmpl)
532 || (TREE_CODE (decl) == TYPE_DECL
533 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
535 /* The parser rejects template declarations in local classes
536 (with the exception of generic lambdas). */
537 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
538 /* The parser rejects any use of virtual in a function template. */
539 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
540 && DECL_VIRTUAL_P (decl)));
542 /* The debug-information generating code doesn't know what to do
543 with member templates. */
544 DECL_IGNORED_P (tmpl) = 1;
546 else if (variable_template_p (tmpl))
547 /* OK */;
548 else
549 error ("template declaration of %q#D", decl);
552 /* Return true iff TYPE is a valid Java parameter or return type. */
554 static bool
555 acceptable_java_type (tree type)
557 if (type == error_mark_node)
558 return false;
560 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
561 return true;
562 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
564 type = TREE_TYPE (type);
565 if (TREE_CODE (type) == RECORD_TYPE)
567 tree args; int i;
568 if (! TYPE_FOR_JAVA (type))
569 return false;
570 if (! CLASSTYPE_TEMPLATE_INFO (type))
571 return true;
572 args = CLASSTYPE_TI_ARGS (type);
573 i = TREE_VEC_LENGTH (args);
574 while (--i >= 0)
576 type = TREE_VEC_ELT (args, i);
577 if (TYPE_PTR_P (type))
578 type = TREE_TYPE (type);
579 if (! TYPE_FOR_JAVA (type))
580 return false;
582 return true;
585 return false;
588 /* For a METHOD in a Java class CTYPE, return true if
589 the parameter and return types are valid Java types.
590 Otherwise, print appropriate error messages, and return false. */
592 bool
593 check_java_method (tree method)
595 bool jerr = false;
596 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
597 tree ret_type = TREE_TYPE (TREE_TYPE (method));
599 if (!acceptable_java_type (ret_type))
601 error ("Java method %qD has non-Java return type %qT",
602 method, ret_type);
603 jerr = true;
606 arg_types = TREE_CHAIN (arg_types);
607 if (DECL_HAS_IN_CHARGE_PARM_P (method))
608 arg_types = TREE_CHAIN (arg_types);
609 if (DECL_HAS_VTT_PARM_P (method))
610 arg_types = TREE_CHAIN (arg_types);
612 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
614 tree type = TREE_VALUE (arg_types);
615 if (!acceptable_java_type (type))
617 if (type != error_mark_node)
618 error ("Java method %qD has non-Java parameter type %qT",
619 method, type);
620 jerr = true;
623 return !jerr;
626 /* Sanity check: report error if this function FUNCTION is not
627 really a member of the class (CTYPE) it is supposed to belong to.
628 TEMPLATE_PARMS is used to specify the template parameters of a member
629 template passed as FUNCTION_DECL. If the member template is passed as a
630 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
631 from the declaration. If the function is not a function template, it
632 must be NULL.
633 It returns the original declaration for the function, NULL_TREE if
634 no declaration was found, error_mark_node if an error was emitted. */
636 tree
637 check_classfn (tree ctype, tree function, tree template_parms)
639 int ix;
640 bool is_template;
641 tree pushed_scope;
643 if (DECL_USE_TEMPLATE (function)
644 && !(TREE_CODE (function) == TEMPLATE_DECL
645 && DECL_TEMPLATE_SPECIALIZATION (function))
646 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
647 /* Since this is a specialization of a member template,
648 we're not going to find the declaration in the class.
649 For example, in:
651 struct S { template <typename T> void f(T); };
652 template <> void S::f(int);
654 we're not going to find `S::f(int)', but there's no
655 reason we should, either. We let our callers know we didn't
656 find the method, but we don't complain. */
657 return NULL_TREE;
659 /* Basic sanity check: for a template function, the template parameters
660 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
661 if (TREE_CODE (function) == TEMPLATE_DECL)
663 if (template_parms
664 && !comp_template_parms (template_parms,
665 DECL_TEMPLATE_PARMS (function)))
667 error ("template parameter lists provided don%'t match the "
668 "template parameters of %qD", function);
669 return error_mark_node;
671 template_parms = DECL_TEMPLATE_PARMS (function);
674 /* OK, is this a definition of a member template? */
675 is_template = (template_parms != NULL_TREE);
677 /* [temp.mem]
679 A destructor shall not be a member template. */
680 if (DECL_DESTRUCTOR_P (function) && is_template)
682 error ("destructor %qD declared as member template", function);
683 return error_mark_node;
686 /* We must enter the scope here, because conversion operators are
687 named by target type, and type equivalence relies on typenames
688 resolving within the scope of CTYPE. */
689 pushed_scope = push_scope (ctype);
690 ix = class_method_index_for_fn (complete_type (ctype), function);
691 if (ix >= 0)
693 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
694 tree fndecls, fndecl = 0;
695 bool is_conv_op;
696 const char *format = NULL;
698 for (fndecls = (*methods)[ix];
699 fndecls; fndecls = OVL_NEXT (fndecls))
701 tree p1, p2;
703 fndecl = OVL_CURRENT (fndecls);
704 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
705 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
707 /* We cannot simply call decls_match because this doesn't
708 work for static member functions that are pretending to
709 be methods, and because the name may have been changed by
710 asm("new_name"). */
712 /* Get rid of the this parameter on functions that become
713 static. */
714 if (DECL_STATIC_FUNCTION_P (fndecl)
715 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
716 p1 = TREE_CHAIN (p1);
718 /* A member template definition only matches a member template
719 declaration. */
720 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
721 continue;
723 /* ref-qualifier or absence of same must match. */
724 if (type_memfn_rqual (TREE_TYPE (function))
725 != type_memfn_rqual (TREE_TYPE (fndecl)))
726 continue;
728 /* While finding a match, same types and params are not enough
729 if the function is versioned. Also check version ("target")
730 attributes. */
731 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
732 TREE_TYPE (TREE_TYPE (fndecl)))
733 && compparms (p1, p2)
734 && !targetm.target_option.function_versions (function, fndecl)
735 && (!is_template
736 || comp_template_parms (template_parms,
737 DECL_TEMPLATE_PARMS (fndecl)))
738 && (DECL_TEMPLATE_SPECIALIZATION (function)
739 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
740 && (!DECL_TEMPLATE_SPECIALIZATION (function)
741 || (DECL_TI_TEMPLATE (function)
742 == DECL_TI_TEMPLATE (fndecl))))
743 break;
745 if (fndecls)
747 if (pushed_scope)
748 pop_scope (pushed_scope);
749 return OVL_CURRENT (fndecls);
752 error_at (DECL_SOURCE_LOCATION (function),
753 "prototype for %q#D does not match any in class %qT",
754 function, ctype);
755 is_conv_op = DECL_CONV_FN_P (fndecl);
757 if (is_conv_op)
758 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
759 fndecls = (*methods)[ix];
760 while (fndecls)
762 fndecl = OVL_CURRENT (fndecls);
763 fndecls = OVL_NEXT (fndecls);
765 if (!fndecls && is_conv_op)
767 if (methods->length () > (size_t) ++ix)
769 fndecls = (*methods)[ix];
770 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
772 fndecls = NULL_TREE;
773 is_conv_op = false;
776 else
777 is_conv_op = false;
779 if (format)
780 format = " %+#D";
781 else if (fndecls)
782 format = N_("candidates are: %+#D");
783 else
784 format = N_("candidate is: %+#D");
785 error (format, fndecl);
788 else if (!COMPLETE_TYPE_P (ctype))
789 cxx_incomplete_type_error (function, ctype);
790 else
791 error ("no %q#D member function declared in class %qT",
792 function, ctype);
794 if (pushed_scope)
795 pop_scope (pushed_scope);
796 return error_mark_node;
799 /* DECL is a function with vague linkage. Remember it so that at the
800 end of the translation unit we can decide whether or not to emit
801 it. */
803 void
804 note_vague_linkage_fn (tree decl)
806 DECL_DEFER_OUTPUT (decl) = 1;
807 vec_safe_push (deferred_fns, decl);
810 /* As above, but for variable template instantiations. */
812 void
813 note_variable_template_instantiation (tree decl)
815 vec_safe_push (pending_statics, decl);
818 /* We have just processed the DECL, which is a static data member.
819 The other parameters are as for cp_finish_decl. */
821 void
822 finish_static_data_member_decl (tree decl,
823 tree init, bool init_const_expr_p,
824 tree asmspec_tree,
825 int flags)
827 DECL_CONTEXT (decl) = current_class_type;
829 /* We cannot call pushdecl here, because that would fill in the
830 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
831 the right thing, namely, to put this decl out straight away. */
833 if (! processing_template_decl)
834 vec_safe_push (pending_statics, decl);
836 if (LOCAL_CLASS_P (current_class_type)
837 /* We already complained about the template definition. */
838 && !DECL_TEMPLATE_INSTANTIATION (decl))
839 permerror (input_location, "local class %q#T shall not have static data member %q#D",
840 current_class_type, decl);
841 else
842 for (tree t = current_class_type; TYPE_P (t);
843 t = CP_TYPE_CONTEXT (t))
844 if (TYPE_ANONYMOUS_P (t))
846 if (permerror (DECL_SOURCE_LOCATION (decl),
847 "static data member %qD in unnamed class", decl))
848 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
849 "unnamed class defined here");
850 break;
853 DECL_IN_AGGR_P (decl) = 1;
855 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
856 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
857 SET_VAR_HAD_UNKNOWN_BOUND (decl);
859 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
862 /* DECLARATOR and DECLSPECS correspond to a class member. The other
863 parameters are as for cp_finish_decl. Return the DECL for the
864 class member declared. */
866 tree
867 grokfield (const cp_declarator *declarator,
868 cp_decl_specifier_seq *declspecs,
869 tree init, bool init_const_expr_p,
870 tree asmspec_tree,
871 tree attrlist)
873 tree value;
874 const char *asmspec = 0;
875 int flags;
876 tree name;
878 if (init
879 && TREE_CODE (init) == TREE_LIST
880 && TREE_VALUE (init) == error_mark_node
881 && TREE_CHAIN (init) == NULL_TREE)
882 init = NULL_TREE;
884 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
885 if (! value || value == error_mark_node)
886 /* friend or constructor went bad. */
887 return error_mark_node;
888 if (TREE_TYPE (value) == error_mark_node)
889 return value;
891 if (TREE_CODE (value) == TYPE_DECL && init)
893 error ("typedef %qD is initialized (use decltype instead)", value);
894 init = NULL_TREE;
897 /* Pass friendly classes back. */
898 if (value == void_type_node)
899 return value;
902 name = DECL_NAME (value);
904 if (name != NULL_TREE)
906 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
908 error ("explicit template argument list not allowed");
909 return error_mark_node;
912 if (IDENTIFIER_POINTER (name)[0] == '_'
913 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
914 error ("member %qD conflicts with virtual function table field name",
915 value);
918 /* Stash away type declarations. */
919 if (TREE_CODE (value) == TYPE_DECL)
921 DECL_NONLOCAL (value) = 1;
922 DECL_CONTEXT (value) = current_class_type;
924 if (attrlist)
926 int attrflags = 0;
928 /* If this is a typedef that names the class for linkage purposes
929 (7.1.3p8), apply any attributes directly to the type. */
930 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
931 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
932 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
934 cplus_decl_attributes (&value, attrlist, attrflags);
937 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
938 && TREE_TYPE (value) != error_mark_node
939 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
940 set_underlying_type (value);
942 /* It's important that push_template_decl below follows
943 set_underlying_type above so that the created template
944 carries the properly set type of VALUE. */
945 if (processing_template_decl)
946 value = push_template_decl (value);
948 record_locally_defined_typedef (value);
949 return value;
952 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
954 if (!friendp && DECL_IN_AGGR_P (value))
956 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
957 return void_type_node;
960 if (asmspec_tree && asmspec_tree != error_mark_node)
961 asmspec = TREE_STRING_POINTER (asmspec_tree);
963 if (init)
965 if (TREE_CODE (value) == FUNCTION_DECL)
967 if (init == ridpointers[(int)RID_DELETE])
969 DECL_DELETED_FN (value) = 1;
970 DECL_DECLARED_INLINE_P (value) = 1;
971 DECL_INITIAL (value) = error_mark_node;
973 else if (init == ridpointers[(int)RID_DEFAULT])
975 if (defaultable_fn_check (value))
977 DECL_DEFAULTED_FN (value) = 1;
978 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
979 DECL_DECLARED_INLINE_P (value) = 1;
982 else if (TREE_CODE (init) == DEFAULT_ARG)
983 error ("invalid initializer for member function %qD", value);
984 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
986 if (integer_zerop (init))
987 DECL_PURE_VIRTUAL_P (value) = 1;
988 else if (error_operand_p (init))
989 ; /* An error has already been reported. */
990 else
991 error ("invalid initializer for member function %qD",
992 value);
994 else
996 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
997 if (friendp)
998 error ("initializer specified for friend function %qD",
999 value);
1000 else
1001 error ("initializer specified for static member function %qD",
1002 value);
1005 else if (TREE_CODE (value) == FIELD_DECL)
1006 /* C++11 NSDMI, keep going. */;
1007 else if (!VAR_P (value))
1008 gcc_unreachable ();
1011 /* Pass friend decls back. */
1012 if ((TREE_CODE (value) == FUNCTION_DECL
1013 || TREE_CODE (value) == TEMPLATE_DECL)
1014 && DECL_CONTEXT (value) != current_class_type)
1015 return value;
1017 /* Need to set this before push_template_decl. */
1018 if (TREE_CODE (value) == VAR_DECL)
1019 DECL_CONTEXT (value) = current_class_type;
1021 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1023 value = push_template_decl (value);
1024 if (error_operand_p (value))
1025 return error_mark_node;
1028 if (attrlist)
1029 cplus_decl_attributes (&value, attrlist, 0);
1031 if (init && DIRECT_LIST_INIT_P (init))
1032 flags = LOOKUP_NORMAL;
1033 else
1034 flags = LOOKUP_IMPLICIT;
1036 switch (TREE_CODE (value))
1038 case VAR_DECL:
1039 finish_static_data_member_decl (value, init, init_const_expr_p,
1040 asmspec_tree, flags);
1041 return value;
1043 case FIELD_DECL:
1044 if (asmspec)
1045 error ("%<asm%> specifiers are not permitted on non-static data members");
1046 if (DECL_INITIAL (value) == error_mark_node)
1047 init = error_mark_node;
1048 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1049 NULL_TREE, flags);
1050 DECL_IN_AGGR_P (value) = 1;
1051 return value;
1053 case FUNCTION_DECL:
1054 if (asmspec)
1055 set_user_assembler_name (value, asmspec);
1057 cp_finish_decl (value,
1058 /*init=*/NULL_TREE,
1059 /*init_const_expr_p=*/false,
1060 asmspec_tree, flags);
1062 /* Pass friends back this way. */
1063 if (DECL_FRIEND_P (value))
1064 return void_type_node;
1066 DECL_IN_AGGR_P (value) = 1;
1067 return value;
1069 default:
1070 gcc_unreachable ();
1072 return NULL_TREE;
1075 /* Like `grokfield', but for bitfields.
1076 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1078 tree
1079 grokbitfield (const cp_declarator *declarator,
1080 cp_decl_specifier_seq *declspecs, tree width,
1081 tree attrlist)
1083 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1085 if (value == error_mark_node)
1086 return NULL_TREE; /* friends went bad. */
1087 if (TREE_TYPE (value) == error_mark_node)
1088 return value;
1090 /* Pass friendly classes back. */
1091 if (VOID_TYPE_P (value))
1092 return void_type_node;
1094 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1095 && (POINTER_TYPE_P (value)
1096 || !dependent_type_p (TREE_TYPE (value))))
1098 error ("bit-field %qD with non-integral type", value);
1099 return error_mark_node;
1102 if (TREE_CODE (value) == TYPE_DECL)
1104 error ("cannot declare %qD to be a bit-field type", value);
1105 return NULL_TREE;
1108 /* Usually, finish_struct_1 catches bitfields with invalid types.
1109 But, in the case of bitfields with function type, we confuse
1110 ourselves into thinking they are member functions, so we must
1111 check here. */
1112 if (TREE_CODE (value) == FUNCTION_DECL)
1114 error ("cannot declare bit-field %qD with function type",
1115 DECL_NAME (value));
1116 return NULL_TREE;
1119 if (DECL_IN_AGGR_P (value))
1121 error ("%qD is already defined in the class %qT", value,
1122 DECL_CONTEXT (value));
1123 return void_type_node;
1126 if (TREE_STATIC (value))
1128 error ("static member %qD cannot be a bit-field", value);
1129 return NULL_TREE;
1131 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1133 if (width != error_mark_node)
1135 /* The width must be an integer type. */
1136 if (!type_dependent_expression_p (width)
1137 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1138 error ("width of bit-field %qD has non-integral type %qT", value,
1139 TREE_TYPE (width));
1140 DECL_INITIAL (value) = width;
1141 SET_DECL_C_BIT_FIELD (value);
1144 DECL_IN_AGGR_P (value) = 1;
1146 if (attrlist)
1147 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1149 return value;
1153 /* Returns true iff ATTR is an attribute which needs to be applied at
1154 instantiation time rather than template definition time. */
1156 static bool
1157 is_late_template_attribute (tree attr, tree decl)
1159 tree name = get_attribute_name (attr);
1160 tree args = TREE_VALUE (attr);
1161 const struct attribute_spec *spec = lookup_attribute_spec (name);
1162 tree arg;
1164 if (!spec)
1165 /* Unknown attribute. */
1166 return false;
1168 /* Attribute weak handling wants to write out assembly right away. */
1169 if (is_attribute_p ("weak", name))
1170 return true;
1172 /* Attribute unused is applied directly, as it appertains to
1173 decls. */
1174 if (is_attribute_p ("unused", name))
1175 return false;
1177 /* #pragma omp declare simd attribute needs to be always deferred. */
1178 if (flag_openmp
1179 && is_attribute_p ("omp declare simd", name))
1180 return true;
1182 /* An attribute pack is clearly dependent. */
1183 if (args && PACK_EXPANSION_P (args))
1184 return true;
1186 /* If any of the arguments are dependent expressions, we can't evaluate
1187 the attribute until instantiation time. */
1188 for (arg = args; arg; arg = TREE_CHAIN (arg))
1190 tree t = TREE_VALUE (arg);
1192 /* If the first attribute argument is an identifier, only consider
1193 second and following arguments. Attributes like mode, format,
1194 cleanup and several target specific attributes aren't late
1195 just because they have an IDENTIFIER_NODE as first argument. */
1196 if (arg == args && identifier_p (t))
1197 continue;
1199 if (value_dependent_expression_p (t)
1200 || type_dependent_expression_p (t))
1201 return true;
1204 if (TREE_CODE (decl) == TYPE_DECL
1205 || TYPE_P (decl)
1206 || spec->type_required)
1208 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1210 /* We can't apply any attributes to a completely unknown type until
1211 instantiation time. */
1212 enum tree_code code = TREE_CODE (type);
1213 if (code == TEMPLATE_TYPE_PARM
1214 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1215 || code == TYPENAME_TYPE)
1216 return true;
1217 /* Also defer most attributes on dependent types. This is not
1218 necessary in all cases, but is the better default. */
1219 else if (dependent_type_p (type)
1220 /* But some attributes specifically apply to templates. */
1221 && !is_attribute_p ("abi_tag", name)
1222 && !is_attribute_p ("deprecated", name)
1223 && !is_attribute_p ("visibility", name))
1224 return true;
1225 else
1226 return false;
1228 else
1229 return false;
1232 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1233 applied at instantiation time and return them. If IS_DEPENDENT is true,
1234 the declaration itself is dependent, so all attributes should be applied
1235 at instantiation time. */
1237 static tree
1238 splice_template_attributes (tree *attr_p, tree decl)
1240 tree *p = attr_p;
1241 tree late_attrs = NULL_TREE;
1242 tree *q = &late_attrs;
1244 if (!p)
1245 return NULL_TREE;
1247 for (; *p; )
1249 if (is_late_template_attribute (*p, decl))
1251 ATTR_IS_DEPENDENT (*p) = 1;
1252 *q = *p;
1253 *p = TREE_CHAIN (*p);
1254 q = &TREE_CHAIN (*q);
1255 *q = NULL_TREE;
1257 else
1258 p = &TREE_CHAIN (*p);
1261 return late_attrs;
1264 /* Remove any late attributes from the list in ATTR_P and attach them to
1265 DECL_P. */
1267 static void
1268 save_template_attributes (tree *attr_p, tree *decl_p)
1270 tree *q;
1272 if (attr_p && *attr_p == error_mark_node)
1273 return;
1275 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1276 if (!late_attrs)
1277 return;
1279 if (DECL_P (*decl_p))
1280 q = &DECL_ATTRIBUTES (*decl_p);
1281 else
1282 q = &TYPE_ATTRIBUTES (*decl_p);
1284 tree old_attrs = *q;
1286 /* Merge the late attributes at the beginning with the attribute
1287 list. */
1288 late_attrs = merge_attributes (late_attrs, *q);
1289 *q = late_attrs;
1291 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1293 /* We've added new attributes directly to the main variant, so
1294 now we need to update all of the other variants to include
1295 these new attributes. */
1296 tree variant;
1297 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1298 variant = TYPE_NEXT_VARIANT (variant))
1300 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1301 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1306 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1307 to a typedef which gives a previously anonymous class or enum a name for
1308 linkage purposes. */
1310 bool
1311 attributes_naming_typedef_ok (tree attrs)
1313 for (; attrs; attrs = TREE_CHAIN (attrs))
1315 tree name = get_attribute_name (attrs);
1316 if (is_attribute_p ("vector_size", name))
1317 return false;
1319 return true;
1322 /* Like reconstruct_complex_type, but handle also template trees. */
1324 tree
1325 cp_reconstruct_complex_type (tree type, tree bottom)
1327 tree inner, outer;
1328 bool late_return_type_p = false;
1330 if (TYPE_PTR_P (type))
1332 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1333 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1334 TYPE_REF_CAN_ALIAS_ALL (type));
1336 else if (TREE_CODE (type) == REFERENCE_TYPE)
1338 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1339 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1340 TYPE_REF_CAN_ALIAS_ALL (type));
1342 else if (TREE_CODE (type) == ARRAY_TYPE)
1344 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1345 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1346 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1347 element type qualification will be handled by the recursive
1348 cp_reconstruct_complex_type call and cp_build_qualified_type
1349 for ARRAY_TYPEs changes the element type. */
1350 return outer;
1352 else if (TREE_CODE (type) == FUNCTION_TYPE)
1354 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1355 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1356 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1357 outer = apply_memfn_quals (outer,
1358 type_memfn_quals (type),
1359 type_memfn_rqual (type));
1361 else if (TREE_CODE (type) == METHOD_TYPE)
1363 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1364 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1365 /* The build_method_type_directly() routine prepends 'this' to argument list,
1366 so we must compensate by getting rid of it. */
1367 outer
1368 = build_method_type_directly
1369 (class_of_this_parm (type), inner,
1370 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1372 else if (TREE_CODE (type) == OFFSET_TYPE)
1374 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1375 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1377 else
1378 return bottom;
1380 if (TYPE_ATTRIBUTES (type))
1381 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1382 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1384 if (late_return_type_p)
1385 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1387 return outer;
1390 /* Replaces any constexpr expression that may be into the attributes
1391 arguments with their reduced value. */
1393 static void
1394 cp_check_const_attributes (tree attributes)
1396 if (attributes == error_mark_node)
1397 return;
1399 tree attr;
1400 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1402 tree arg;
1403 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1405 tree expr = TREE_VALUE (arg);
1406 if (EXPR_P (expr))
1407 TREE_VALUE (arg) = maybe_constant_value (expr);
1412 /* Return true if TYPE is an OpenMP mappable type. */
1413 bool
1414 cp_omp_mappable_type (tree type)
1416 /* Mappable type has to be complete. */
1417 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1418 return false;
1419 /* Arrays have mappable type if the elements have mappable type. */
1420 while (TREE_CODE (type) == ARRAY_TYPE)
1421 type = TREE_TYPE (type);
1422 /* A mappable type cannot contain virtual members. */
1423 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1424 return false;
1425 /* All data members must be non-static. */
1426 if (CLASS_TYPE_P (type))
1428 tree field;
1429 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1430 if (TREE_CODE (field) == VAR_DECL)
1431 return false;
1432 /* All fields must have mappable types. */
1433 else if (TREE_CODE (field) == FIELD_DECL
1434 && !cp_omp_mappable_type (TREE_TYPE (field)))
1435 return false;
1437 return true;
1440 /* Like decl_attributes, but handle C++ complexity. */
1442 void
1443 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1445 if (*decl == NULL_TREE || *decl == void_type_node
1446 || *decl == error_mark_node)
1447 return;
1449 /* Add implicit "omp declare target" attribute if requested. */
1450 if (scope_chain->omp_declare_target_attribute
1451 && ((TREE_CODE (*decl) == VAR_DECL
1452 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1453 || TREE_CODE (*decl) == FUNCTION_DECL))
1455 if (TREE_CODE (*decl) == VAR_DECL
1456 && DECL_CLASS_SCOPE_P (*decl))
1457 error ("%q+D static data member inside of declare target directive",
1458 *decl);
1459 else if (TREE_CODE (*decl) == VAR_DECL
1460 && (DECL_FUNCTION_SCOPE_P (*decl)
1461 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1462 error ("%q+D in block scope inside of declare target directive",
1463 *decl);
1464 else if (!processing_template_decl
1465 && TREE_CODE (*decl) == VAR_DECL
1466 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1467 error ("%q+D in declare target directive does not have mappable type",
1468 *decl);
1469 else
1470 attributes = tree_cons (get_identifier ("omp declare target"),
1471 NULL_TREE, attributes);
1474 if (processing_template_decl)
1476 if (check_for_bare_parameter_packs (attributes))
1477 return;
1479 save_template_attributes (&attributes, decl);
1482 cp_check_const_attributes (attributes);
1484 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1485 decl = &DECL_TEMPLATE_RESULT (*decl);
1487 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1489 attributes
1490 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1491 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1492 attributes, flags);
1494 else
1495 decl_attributes (decl, attributes, flags);
1497 if (TREE_CODE (*decl) == TYPE_DECL)
1498 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1500 /* Propagate deprecation out to the template. */
1501 if (TREE_DEPRECATED (*decl))
1502 if (tree ti = get_template_info (*decl))
1504 tree tmpl = TI_TEMPLATE (ti);
1505 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1506 : DECL_TEMPLATE_RESULT (tmpl));
1507 if (*decl == pattern)
1508 TREE_DEPRECATED (tmpl) = true;
1512 /* Walks through the namespace- or function-scope anonymous union
1513 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1514 Returns one of the fields for use in the mangled name. */
1516 static tree
1517 build_anon_union_vars (tree type, tree object)
1519 tree main_decl = NULL_TREE;
1520 tree field;
1522 /* Rather than write the code to handle the non-union case,
1523 just give an error. */
1524 if (TREE_CODE (type) != UNION_TYPE)
1526 error ("anonymous struct not inside named type");
1527 return error_mark_node;
1530 for (field = TYPE_FIELDS (type);
1531 field != NULL_TREE;
1532 field = DECL_CHAIN (field))
1534 tree decl;
1535 tree ref;
1537 if (DECL_ARTIFICIAL (field))
1538 continue;
1539 if (TREE_CODE (field) != FIELD_DECL)
1541 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1542 "have non-static data members", field);
1543 continue;
1546 if (TREE_PRIVATE (field))
1547 permerror (input_location, "private member %q+#D in anonymous union", field);
1548 else if (TREE_PROTECTED (field))
1549 permerror (input_location, "protected member %q+#D in anonymous union", field);
1551 if (processing_template_decl)
1552 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1553 DECL_NAME (field), NULL_TREE);
1554 else
1555 ref = build_class_member_access_expr (object, field, NULL_TREE,
1556 false, tf_warning_or_error);
1558 if (DECL_NAME (field))
1560 tree base;
1562 decl = build_decl (input_location,
1563 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1564 DECL_ANON_UNION_VAR_P (decl) = 1;
1565 DECL_ARTIFICIAL (decl) = 1;
1567 base = get_base_address (object);
1568 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1569 TREE_STATIC (decl) = TREE_STATIC (base);
1570 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1572 SET_DECL_VALUE_EXPR (decl, ref);
1573 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1575 decl = pushdecl (decl);
1577 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1578 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1579 else
1580 decl = 0;
1582 if (main_decl == NULL_TREE)
1583 main_decl = decl;
1586 return main_decl;
1589 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1590 anonymous union, then all members must be laid out together. PUBLIC_P
1591 is nonzero if this union is not declared static. */
1593 void
1594 finish_anon_union (tree anon_union_decl)
1596 tree type;
1597 tree main_decl;
1598 bool public_p;
1600 if (anon_union_decl == error_mark_node)
1601 return;
1603 type = TREE_TYPE (anon_union_decl);
1604 public_p = TREE_PUBLIC (anon_union_decl);
1606 /* The VAR_DECL's context is the same as the TYPE's context. */
1607 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1609 if (TYPE_FIELDS (type) == NULL_TREE)
1610 return;
1612 if (public_p)
1614 error ("namespace-scope anonymous aggregates must be static");
1615 return;
1618 main_decl = build_anon_union_vars (type, anon_union_decl);
1619 if (main_decl == error_mark_node)
1620 return;
1621 if (main_decl == NULL_TREE)
1623 warning (0, "anonymous union with no members");
1624 return;
1627 if (!processing_template_decl)
1629 /* Use main_decl to set the mangled name. */
1630 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1631 maybe_commonize_var (anon_union_decl);
1632 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1633 mangle_decl (anon_union_decl);
1634 DECL_NAME (anon_union_decl) = NULL_TREE;
1637 pushdecl (anon_union_decl);
1638 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1641 /* Auxiliary functions to make type signatures for
1642 `operator new' and `operator delete' correspond to
1643 what compiler will be expecting. */
1645 tree
1646 coerce_new_type (tree type)
1648 int e = 0;
1649 tree args = TYPE_ARG_TYPES (type);
1651 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1653 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1655 e = 1;
1656 error ("%<operator new%> must return type %qT", ptr_type_node);
1659 if (args && args != void_list_node)
1661 if (TREE_PURPOSE (args))
1663 /* [basic.stc.dynamic.allocation]
1665 The first parameter shall not have an associated default
1666 argument. */
1667 error ("the first parameter of %<operator new%> cannot "
1668 "have a default argument");
1669 /* Throw away the default argument. */
1670 TREE_PURPOSE (args) = NULL_TREE;
1673 if (!same_type_p (TREE_VALUE (args), size_type_node))
1675 e = 2;
1676 args = TREE_CHAIN (args);
1679 else
1680 e = 2;
1682 if (e == 2)
1683 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1684 "as first parameter", size_type_node);
1686 switch (e)
1688 case 2:
1689 args = tree_cons (NULL_TREE, size_type_node, args);
1690 /* Fall through. */
1691 case 1:
1692 type = build_exception_variant
1693 (build_function_type (ptr_type_node, args),
1694 TYPE_RAISES_EXCEPTIONS (type));
1695 /* Fall through. */
1696 default:;
1698 return type;
1701 tree
1702 coerce_delete_type (tree type)
1704 int e = 0;
1705 tree args = TYPE_ARG_TYPES (type);
1707 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1709 if (!same_type_p (TREE_TYPE (type), void_type_node))
1711 e = 1;
1712 error ("%<operator delete%> must return type %qT", void_type_node);
1715 if (!args || args == void_list_node
1716 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1718 e = 2;
1719 if (args && args != void_list_node)
1720 args = TREE_CHAIN (args);
1721 error ("%<operator delete%> takes type %qT as first parameter",
1722 ptr_type_node);
1724 switch (e)
1726 case 2:
1727 args = tree_cons (NULL_TREE, ptr_type_node, args);
1728 /* Fall through. */
1729 case 1:
1730 type = build_exception_variant
1731 (build_function_type (void_type_node, args),
1732 TYPE_RAISES_EXCEPTIONS (type));
1733 /* Fall through. */
1734 default:;
1737 return type;
1740 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1741 and mark them as needed. */
1743 static void
1744 mark_vtable_entries (tree decl)
1746 tree fnaddr;
1747 unsigned HOST_WIDE_INT idx;
1749 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1750 idx, fnaddr)
1752 tree fn;
1754 STRIP_NOPS (fnaddr);
1756 if (TREE_CODE (fnaddr) != ADDR_EXPR
1757 && TREE_CODE (fnaddr) != FDESC_EXPR)
1758 /* This entry is an offset: a virtual base class offset, a
1759 virtual call offset, an RTTI offset, etc. */
1760 continue;
1762 fn = TREE_OPERAND (fnaddr, 0);
1763 TREE_ADDRESSABLE (fn) = 1;
1764 /* When we don't have vcall offsets, we output thunks whenever
1765 we output the vtables that contain them. With vcall offsets,
1766 we know all the thunks we'll need when we emit a virtual
1767 function, so we emit the thunks there instead. */
1768 if (DECL_THUNK_P (fn))
1769 use_thunk (fn, /*emit_p=*/0);
1770 mark_used (fn);
1774 /* Set DECL up to have the closest approximation of "initialized common"
1775 linkage available. */
1777 void
1778 comdat_linkage (tree decl)
1780 if (flag_weak)
1781 make_decl_one_only (decl, cxx_comdat_group (decl));
1782 else if (TREE_CODE (decl) == FUNCTION_DECL
1783 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1784 /* We can just emit function and compiler-generated variables
1785 statically; having multiple copies is (for the most part) only
1786 a waste of space.
1788 There are two correctness issues, however: the address of a
1789 template instantiation with external linkage should be the
1790 same, independent of what translation unit asks for the
1791 address, and this will not hold when we emit multiple copies of
1792 the function. However, there's little else we can do.
1794 Also, by default, the typeinfo implementation assumes that
1795 there will be only one copy of the string used as the name for
1796 each type. Therefore, if weak symbols are unavailable, the
1797 run-time library should perform a more conservative check; it
1798 should perform a string comparison, rather than an address
1799 comparison. */
1800 TREE_PUBLIC (decl) = 0;
1801 else
1803 /* Static data member template instantiations, however, cannot
1804 have multiple copies. */
1805 if (DECL_INITIAL (decl) == 0
1806 || DECL_INITIAL (decl) == error_mark_node)
1807 DECL_COMMON (decl) = 1;
1808 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1810 DECL_COMMON (decl) = 1;
1811 DECL_INITIAL (decl) = error_mark_node;
1813 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1815 /* We can't do anything useful; leave vars for explicit
1816 instantiation. */
1817 DECL_EXTERNAL (decl) = 1;
1818 DECL_NOT_REALLY_EXTERN (decl) = 0;
1822 DECL_COMDAT (decl) = 1;
1825 /* For win32 we also want to put explicit instantiations in
1826 linkonce sections, so that they will be merged with implicit
1827 instantiations; otherwise we get duplicate symbol errors.
1828 For Darwin we do not want explicit instantiations to be
1829 linkonce. */
1831 void
1832 maybe_make_one_only (tree decl)
1834 /* We used to say that this was not necessary on targets that support weak
1835 symbols, because the implicit instantiations will defer to the explicit
1836 one. However, that's not actually the case in SVR4; a strong definition
1837 after a weak one is an error. Also, not making explicit
1838 instantiations one_only means that we can end up with two copies of
1839 some template instantiations. */
1840 if (! flag_weak)
1841 return;
1843 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1844 we can get away with not emitting them if they aren't used. We need
1845 to for variables so that cp_finish_decl will update their linkage,
1846 because their DECL_INITIAL may not have been set properly yet. */
1848 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1849 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1850 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1852 make_decl_one_only (decl, cxx_comdat_group (decl));
1854 if (VAR_P (decl))
1856 varpool_node *node = varpool_node::get_create (decl);
1857 DECL_COMDAT (decl) = 1;
1858 /* Mark it needed so we don't forget to emit it. */
1859 node->forced_by_abi = true;
1860 TREE_USED (decl) = 1;
1865 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1866 This predicate will give the right answer during parsing of the
1867 function, which other tests may not. */
1869 bool
1870 vague_linkage_p (tree decl)
1872 if (!TREE_PUBLIC (decl))
1874 gcc_checking_assert (!DECL_COMDAT (decl));
1875 return false;
1877 /* Unfortunately, import_export_decl has not always been called
1878 before the function is processed, so we cannot simply check
1879 DECL_COMDAT. */
1880 if (DECL_COMDAT (decl)
1881 || (TREE_CODE (decl) == FUNCTION_DECL
1882 && DECL_DECLARED_INLINE_P (decl))
1883 || (DECL_LANG_SPECIFIC (decl)
1884 && DECL_TEMPLATE_INSTANTIATION (decl)))
1885 return true;
1886 else if (DECL_FUNCTION_SCOPE_P (decl))
1887 /* A local static in an inline effectively has vague linkage. */
1888 return (TREE_STATIC (decl)
1889 && vague_linkage_p (DECL_CONTEXT (decl)));
1890 else
1891 return false;
1894 /* Determine whether or not we want to specifically import or export CTYPE,
1895 using various heuristics. */
1897 static void
1898 import_export_class (tree ctype)
1900 /* -1 for imported, 1 for exported. */
1901 int import_export = 0;
1903 /* It only makes sense to call this function at EOF. The reason is
1904 that this function looks at whether or not the first non-inline
1905 non-abstract virtual member function has been defined in this
1906 translation unit. But, we can't possibly know that until we've
1907 seen the entire translation unit. */
1908 gcc_assert (at_eof);
1910 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1911 return;
1913 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1914 we will have CLASSTYPE_INTERFACE_ONLY set but not
1915 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1916 heuristic because someone will supply a #pragma implementation
1917 elsewhere, and deducing it here would produce a conflict. */
1918 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1919 return;
1921 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1922 import_export = -1;
1923 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1924 import_export = 1;
1925 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1926 && !flag_implicit_templates)
1927 /* For a template class, without -fimplicit-templates, check the
1928 repository. If the virtual table is assigned to this
1929 translation unit, then export the class; otherwise, import
1930 it. */
1931 import_export = repo_export_class_p (ctype) ? 1 : -1;
1932 else if (TYPE_POLYMORPHIC_P (ctype))
1934 /* The ABI specifies that the virtual table and associated
1935 information are emitted with the key method, if any. */
1936 tree method = CLASSTYPE_KEY_METHOD (ctype);
1937 /* If weak symbol support is not available, then we must be
1938 careful not to emit the vtable when the key function is
1939 inline. An inline function can be defined in multiple
1940 translation units. If we were to emit the vtable in each
1941 translation unit containing a definition, we would get
1942 multiple definition errors at link-time. */
1943 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1944 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1947 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1948 a definition anywhere else. */
1949 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1950 import_export = 0;
1952 /* Allow back ends the chance to overrule the decision. */
1953 if (targetm.cxx.import_export_class)
1954 import_export = targetm.cxx.import_export_class (ctype, import_export);
1956 if (import_export)
1958 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1959 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1963 /* Return true if VAR has already been provided to the back end; in that
1964 case VAR should not be modified further by the front end. */
1965 static bool
1966 var_finalized_p (tree var)
1968 return varpool_node::get_create (var)->definition;
1971 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1972 must be emitted in this translation unit. Mark it as such. */
1974 void
1975 mark_needed (tree decl)
1977 TREE_USED (decl) = 1;
1978 if (TREE_CODE (decl) == FUNCTION_DECL)
1980 /* Extern inline functions don't become needed when referenced.
1981 If we know a method will be emitted in other TU and no new
1982 functions can be marked reachable, just use the external
1983 definition. */
1984 struct cgraph_node *node = cgraph_node::get_create (decl);
1985 node->forced_by_abi = true;
1987 /* #pragma interface and -frepo code can call mark_needed for
1988 maybe-in-charge 'tors; mark the clones as well. */
1989 tree clone;
1990 FOR_EACH_CLONE (clone, decl)
1991 mark_needed (clone);
1993 else if (TREE_CODE (decl) == VAR_DECL)
1995 varpool_node *node = varpool_node::get_create (decl);
1996 /* C++ frontend use mark_decl_references to force COMDAT variables
1997 to be output that might appear dead otherwise. */
1998 node->forced_by_abi = true;
2002 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2003 returns true if a definition of this entity should be provided in
2004 this object file. Callers use this function to determine whether
2005 or not to let the back end know that a definition of DECL is
2006 available in this translation unit. */
2008 bool
2009 decl_needed_p (tree decl)
2011 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2012 /* This function should only be called at the end of the translation
2013 unit. We cannot be sure of whether or not something will be
2014 COMDAT until that point. */
2015 gcc_assert (at_eof);
2017 /* All entities with external linkage that are not COMDAT/EXTERN should be
2018 emitted; they may be referred to from other object files. */
2019 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2020 return true;
2021 /* Functions marked "dllexport" must be emitted so that they are
2022 visible to other DLLs. */
2023 if (flag_keep_inline_dllexport
2024 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2025 return true;
2027 /* When not optimizing, do not bother to produce definitions for extern
2028 symbols. */
2029 if (DECL_REALLY_EXTERN (decl)
2030 && ((TREE_CODE (decl) != FUNCTION_DECL
2031 && !optimize)
2032 || (TREE_CODE (decl) == FUNCTION_DECL
2033 && !opt_for_fn (decl, optimize)))
2034 && !lookup_attribute ("always_inline", decl))
2035 return false;
2037 /* If this entity was used, let the back end see it; it will decide
2038 whether or not to emit it into the object file. */
2039 if (TREE_USED (decl))
2040 return true;
2041 /* Virtual functions might be needed for devirtualization. */
2042 if (flag_devirtualize
2043 && TREE_CODE (decl) == FUNCTION_DECL
2044 && DECL_VIRTUAL_P (decl))
2045 return true;
2046 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2047 reference to DECL might cause it to be emitted later. */
2048 return false;
2051 /* If necessary, write out the vtables for the dynamic class CTYPE.
2052 Returns true if any vtables were emitted. */
2054 static bool
2055 maybe_emit_vtables (tree ctype)
2057 tree vtbl;
2058 tree primary_vtbl;
2059 int needed = 0;
2060 varpool_node *current = NULL, *last = NULL;
2062 /* If the vtables for this class have already been emitted there is
2063 nothing more to do. */
2064 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2065 if (var_finalized_p (primary_vtbl))
2066 return false;
2067 /* Ignore dummy vtables made by get_vtable_decl. */
2068 if (TREE_TYPE (primary_vtbl) == void_type_node)
2069 return false;
2071 /* On some targets, we cannot determine the key method until the end
2072 of the translation unit -- which is when this function is
2073 called. */
2074 if (!targetm.cxx.key_method_may_be_inline ())
2075 determine_key_method (ctype);
2077 /* See if any of the vtables are needed. */
2078 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2080 import_export_decl (vtbl);
2081 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2082 needed = 1;
2084 if (!needed)
2086 /* If the references to this class' vtables are optimized away,
2087 still emit the appropriate debugging information. See
2088 dfs_debug_mark. */
2089 if (DECL_COMDAT (primary_vtbl)
2090 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2091 note_debug_info_needed (ctype);
2092 return false;
2095 /* The ABI requires that we emit all of the vtables if we emit any
2096 of them. */
2097 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2099 /* Mark entities references from the virtual table as used. */
2100 mark_vtable_entries (vtbl);
2102 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2104 vec<tree, va_gc> *cleanups = NULL;
2105 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2106 LOOKUP_NORMAL);
2108 /* It had better be all done at compile-time. */
2109 gcc_assert (!expr && !cleanups);
2112 /* Write it out. */
2113 DECL_EXTERNAL (vtbl) = 0;
2114 rest_of_decl_compilation (vtbl, 1, 1);
2116 /* Because we're only doing syntax-checking, we'll never end up
2117 actually marking the variable as written. */
2118 if (flag_syntax_only)
2119 TREE_ASM_WRITTEN (vtbl) = 1;
2120 else if (DECL_ONE_ONLY (vtbl))
2122 current = varpool_node::get_create (vtbl);
2123 if (last)
2124 current->add_to_same_comdat_group (last);
2125 last = current;
2129 /* Since we're writing out the vtable here, also write the debug
2130 info. */
2131 note_debug_info_needed (ctype);
2133 return true;
2136 /* A special return value from type_visibility meaning internal
2137 linkage. */
2139 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2141 /* walk_tree helper function for type_visibility. */
2143 static tree
2144 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2146 int *vis_p = (int *)data;
2147 if (! TYPE_P (*tp))
2149 *walk_subtrees = 0;
2151 else if (OVERLOAD_TYPE_P (*tp)
2152 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2154 *vis_p = VISIBILITY_ANON;
2155 return *tp;
2157 else if (CLASS_TYPE_P (*tp)
2158 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2159 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2160 return NULL;
2163 /* Returns the visibility of TYPE, which is the minimum visibility of its
2164 component types. */
2166 static int
2167 type_visibility (tree type)
2169 int vis = VISIBILITY_DEFAULT;
2170 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2171 return vis;
2174 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2175 specified (or if VISIBILITY is static). If TMPL is true, this
2176 constraint is for a template argument, and takes precedence
2177 over explicitly-specified visibility on the template. */
2179 static void
2180 constrain_visibility (tree decl, int visibility, bool tmpl)
2182 if (visibility == VISIBILITY_ANON)
2184 /* extern "C" declarations aren't affected by the anonymous
2185 namespace. */
2186 if (!DECL_EXTERN_C_P (decl))
2188 TREE_PUBLIC (decl) = 0;
2189 DECL_WEAK (decl) = 0;
2190 DECL_COMMON (decl) = 0;
2191 DECL_COMDAT (decl) = false;
2192 if (TREE_CODE (decl) == FUNCTION_DECL
2193 || TREE_CODE (decl) == VAR_DECL)
2195 struct symtab_node *snode = symtab_node::get (decl);
2197 if (snode)
2198 snode->set_comdat_group (NULL);
2200 DECL_INTERFACE_KNOWN (decl) = 1;
2201 if (DECL_LANG_SPECIFIC (decl))
2202 DECL_NOT_REALLY_EXTERN (decl) = 1;
2205 else if (visibility > DECL_VISIBILITY (decl)
2206 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2208 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2209 /* This visibility was not specified. */
2210 DECL_VISIBILITY_SPECIFIED (decl) = false;
2214 /* Constrain the visibility of DECL based on the visibility of its template
2215 arguments. */
2217 static void
2218 constrain_visibility_for_template (tree decl, tree targs)
2220 /* If this is a template instantiation, check the innermost
2221 template args for visibility constraints. The outer template
2222 args are covered by the class check. */
2223 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2224 int i;
2225 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2227 int vis = 0;
2229 tree arg = TREE_VEC_ELT (args, i-1);
2230 if (TYPE_P (arg))
2231 vis = type_visibility (arg);
2232 else
2234 if (REFERENCE_REF_P (arg))
2235 arg = TREE_OPERAND (arg, 0);
2236 if (TREE_TYPE (arg))
2237 STRIP_NOPS (arg);
2238 if (TREE_CODE (arg) == ADDR_EXPR)
2239 arg = TREE_OPERAND (arg, 0);
2240 if (VAR_OR_FUNCTION_DECL_P (arg))
2242 if (! TREE_PUBLIC (arg))
2243 vis = VISIBILITY_ANON;
2244 else
2245 vis = DECL_VISIBILITY (arg);
2248 if (vis)
2249 constrain_visibility (decl, vis, true);
2253 /* Like c_determine_visibility, but with additional C++-specific
2254 behavior.
2256 Function-scope entities can rely on the function's visibility because
2257 it is set in start_preparsed_function.
2259 Class-scope entities cannot rely on the class's visibility until the end
2260 of the enclosing class definition.
2262 Note that because namespaces have multiple independent definitions,
2263 namespace visibility is handled elsewhere using the #pragma visibility
2264 machinery rather than by decorating the namespace declaration.
2266 The goal is for constraints from the type to give a diagnostic, and
2267 other constraints to be applied silently. */
2269 void
2270 determine_visibility (tree decl)
2272 tree class_type = NULL_TREE;
2273 bool use_template;
2274 bool orig_visibility_specified;
2275 enum symbol_visibility orig_visibility;
2277 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2279 /* Only relevant for names with external linkage. */
2280 if (!TREE_PUBLIC (decl))
2281 return;
2283 /* Cloned constructors and destructors get the same visibility as
2284 the underlying function. That should be set up in
2285 maybe_clone_body. */
2286 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2288 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2289 orig_visibility = DECL_VISIBILITY (decl);
2291 if (TREE_CODE (decl) == TYPE_DECL)
2293 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2294 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2295 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2296 use_template = 1;
2297 else
2298 use_template = 0;
2300 else if (DECL_LANG_SPECIFIC (decl))
2301 use_template = DECL_USE_TEMPLATE (decl);
2302 else
2303 use_template = 0;
2305 /* If DECL is a member of a class, visibility specifiers on the
2306 class can influence the visibility of the DECL. */
2307 if (DECL_CLASS_SCOPE_P (decl))
2308 class_type = DECL_CONTEXT (decl);
2309 else
2311 /* Not a class member. */
2313 /* Virtual tables have DECL_CONTEXT set to their associated class,
2314 so they are automatically handled above. */
2315 gcc_assert (!VAR_P (decl)
2316 || !DECL_VTABLE_OR_VTT_P (decl));
2318 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2320 /* Local statics and classes get the visibility of their
2321 containing function by default, except that
2322 -fvisibility-inlines-hidden doesn't affect them. */
2323 tree fn = DECL_CONTEXT (decl);
2324 if (DECL_VISIBILITY_SPECIFIED (fn))
2326 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2327 DECL_VISIBILITY_SPECIFIED (decl) =
2328 DECL_VISIBILITY_SPECIFIED (fn);
2330 else
2332 if (DECL_CLASS_SCOPE_P (fn))
2333 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2334 else if (determine_hidden_inline (fn))
2336 DECL_VISIBILITY (decl) = default_visibility;
2337 DECL_VISIBILITY_SPECIFIED (decl) =
2338 visibility_options.inpragma;
2340 else
2342 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2343 DECL_VISIBILITY_SPECIFIED (decl) =
2344 DECL_VISIBILITY_SPECIFIED (fn);
2348 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2349 but have no TEMPLATE_INFO, so don't try to check it. */
2350 use_template = 0;
2352 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2353 && flag_visibility_ms_compat)
2355 /* Under -fvisibility-ms-compat, types are visible by default,
2356 even though their contents aren't. */
2357 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2358 int underlying_vis = type_visibility (underlying_type);
2359 if (underlying_vis == VISIBILITY_ANON
2360 || (CLASS_TYPE_P (underlying_type)
2361 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2362 constrain_visibility (decl, underlying_vis, false);
2363 else
2364 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2366 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2368 /* tinfo visibility is based on the type it's for. */
2369 constrain_visibility
2370 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2372 /* Give the target a chance to override the visibility associated
2373 with DECL. */
2374 if (TREE_PUBLIC (decl)
2375 && !DECL_REALLY_EXTERN (decl)
2376 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2377 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2378 targetm.cxx.determine_class_data_visibility (decl);
2380 else if (use_template)
2381 /* Template instantiations and specializations get visibility based
2382 on their template unless they override it with an attribute. */;
2383 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2385 if (determine_hidden_inline (decl))
2386 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2387 else
2389 /* Set default visibility to whatever the user supplied with
2390 #pragma GCC visibility or a namespace visibility attribute. */
2391 DECL_VISIBILITY (decl) = default_visibility;
2392 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2397 if (use_template)
2399 /* If the specialization doesn't specify visibility, use the
2400 visibility from the template. */
2401 tree tinfo = get_template_info (decl);
2402 tree args = TI_ARGS (tinfo);
2403 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2404 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2405 : DECL_ATTRIBUTES (decl));
2407 if (args != error_mark_node)
2409 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2411 if (!DECL_VISIBILITY_SPECIFIED (decl))
2413 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2414 && determine_hidden_inline (decl))
2415 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2416 else
2418 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2419 DECL_VISIBILITY_SPECIFIED (decl)
2420 = DECL_VISIBILITY_SPECIFIED (pattern);
2424 if (args
2425 /* Template argument visibility outweighs #pragma or namespace
2426 visibility, but not an explicit attribute. */
2427 && !lookup_attribute ("visibility", attribs))
2429 int depth = TMPL_ARGS_DEPTH (args);
2430 if (DECL_VISIBILITY_SPECIFIED (decl))
2432 /* A class template member with explicit visibility
2433 overrides the class visibility, so we need to apply
2434 all the levels of template args directly. */
2435 int i;
2436 for (i = 1; i <= depth; ++i)
2438 tree lev = TMPL_ARGS_LEVEL (args, i);
2439 constrain_visibility_for_template (decl, lev);
2442 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2443 /* Limit visibility based on its template arguments. */
2444 constrain_visibility_for_template (decl, args);
2449 if (class_type)
2450 determine_visibility_from_class (decl, class_type);
2452 if (decl_anon_ns_mem_p (decl))
2453 /* Names in an anonymous namespace get internal linkage.
2454 This might change once we implement export. */
2455 constrain_visibility (decl, VISIBILITY_ANON, false);
2456 else if (TREE_CODE (decl) != TYPE_DECL)
2458 /* Propagate anonymity from type to decl. */
2459 int tvis = type_visibility (TREE_TYPE (decl));
2460 if (tvis == VISIBILITY_ANON
2461 || ! DECL_VISIBILITY_SPECIFIED (decl))
2462 constrain_visibility (decl, tvis, false);
2464 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2465 /* DR 757: A type without linkage shall not be used as the type of a
2466 variable or function with linkage, unless
2467 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2468 o the variable or function is not used (3.2 [basic.def.odr]) or is
2469 defined in the same translation unit.
2471 Since non-extern "C" decls need to be defined in the same
2472 translation unit, we can make the type internal. */
2473 constrain_visibility (decl, VISIBILITY_ANON, false);
2475 /* If visibility changed and DECL already has DECL_RTL, ensure
2476 symbol flags are updated. */
2477 if ((DECL_VISIBILITY (decl) != orig_visibility
2478 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2479 && ((VAR_P (decl) && TREE_STATIC (decl))
2480 || TREE_CODE (decl) == FUNCTION_DECL)
2481 && DECL_RTL_SET_P (decl))
2482 make_decl_rtl (decl);
2485 /* By default, static data members and function members receive
2486 the visibility of their containing class. */
2488 static void
2489 determine_visibility_from_class (tree decl, tree class_type)
2491 if (DECL_VISIBILITY_SPECIFIED (decl))
2492 return;
2494 if (determine_hidden_inline (decl))
2495 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2496 else
2498 /* Default to the class visibility. */
2499 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2500 DECL_VISIBILITY_SPECIFIED (decl)
2501 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2504 /* Give the target a chance to override the visibility associated
2505 with DECL. */
2506 if (VAR_P (decl)
2507 && (DECL_TINFO_P (decl)
2508 || (DECL_VTABLE_OR_VTT_P (decl)
2509 /* Construction virtual tables are not exported because
2510 they cannot be referred to from other object files;
2511 their name is not standardized by the ABI. */
2512 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2513 && TREE_PUBLIC (decl)
2514 && !DECL_REALLY_EXTERN (decl)
2515 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2516 targetm.cxx.determine_class_data_visibility (decl);
2519 /* Returns true iff DECL is an inline that should get hidden visibility
2520 because of -fvisibility-inlines-hidden. */
2522 static bool
2523 determine_hidden_inline (tree decl)
2525 return (visibility_options.inlines_hidden
2526 /* Don't do this for inline templates; specializations might not be
2527 inline, and we don't want them to inherit the hidden
2528 visibility. We'll set it here for all inline instantiations. */
2529 && !processing_template_decl
2530 && TREE_CODE (decl) == FUNCTION_DECL
2531 && DECL_DECLARED_INLINE_P (decl)
2532 && (! DECL_LANG_SPECIFIC (decl)
2533 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2536 /* Constrain the visibility of a class TYPE based on the visibility of its
2537 field types. Warn if any fields require lesser visibility. */
2539 void
2540 constrain_class_visibility (tree type)
2542 tree binfo;
2543 tree t;
2544 int i;
2546 int vis = type_visibility (type);
2548 if (vis == VISIBILITY_ANON
2549 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2550 return;
2552 /* Don't warn about visibility if the class has explicit visibility. */
2553 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2554 vis = VISIBILITY_INTERNAL;
2556 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2557 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2559 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2560 int subvis = type_visibility (ftype);
2562 if (subvis == VISIBILITY_ANON)
2564 if (!in_main_input_context ())
2565 warning (0, "\
2566 %qT has a field %qD whose type uses the anonymous namespace",
2567 type, t);
2569 else if (MAYBE_CLASS_TYPE_P (ftype)
2570 && vis < VISIBILITY_HIDDEN
2571 && subvis >= VISIBILITY_HIDDEN)
2572 warning (OPT_Wattributes, "\
2573 %qT declared with greater visibility than the type of its field %qD",
2574 type, t);
2577 binfo = TYPE_BINFO (type);
2578 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2580 int subvis = type_visibility (TREE_TYPE (t));
2582 if (subvis == VISIBILITY_ANON)
2584 if (!in_main_input_context())
2585 warning (0, "\
2586 %qT has a base %qT whose type uses the anonymous namespace",
2587 type, TREE_TYPE (t));
2589 else if (vis < VISIBILITY_HIDDEN
2590 && subvis >= VISIBILITY_HIDDEN)
2591 warning (OPT_Wattributes, "\
2592 %qT declared with greater visibility than its base %qT",
2593 type, TREE_TYPE (t));
2597 /* Functions for adjusting the visibility of a tagged type and its nested
2598 types and declarations when it gets a name for linkage purposes from a
2599 typedef. */
2601 static void bt_reset_linkage_1 (binding_entry, void *);
2602 static void bt_reset_linkage_2 (binding_entry, void *);
2604 /* First reset the visibility of all the types. */
2606 static void
2607 reset_type_linkage_1 (tree type)
2609 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2610 if (CLASS_TYPE_P (type))
2611 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2612 bt_reset_linkage_1, NULL);
2614 static void
2615 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2617 reset_type_linkage_1 (b->type);
2620 /* Then reset the visibility of any static data members or member
2621 functions that use those types. */
2623 static void
2624 reset_decl_linkage (tree decl)
2626 if (TREE_PUBLIC (decl))
2627 return;
2628 if (DECL_CLONED_FUNCTION_P (decl))
2629 return;
2630 TREE_PUBLIC (decl) = true;
2631 DECL_INTERFACE_KNOWN (decl) = false;
2632 determine_visibility (decl);
2633 tentative_decl_linkage (decl);
2635 static void
2636 reset_type_linkage_2 (tree type)
2638 if (CLASS_TYPE_P (type))
2640 if (tree vt = CLASSTYPE_VTABLES (type))
2642 tree name = mangle_vtbl_for_type (type);
2643 DECL_NAME (vt) = name;
2644 SET_DECL_ASSEMBLER_NAME (vt, name);
2645 reset_decl_linkage (vt);
2647 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2649 tree name = mangle_typeinfo_for_type (type);
2650 DECL_NAME (ti) = name;
2651 SET_DECL_ASSEMBLER_NAME (ti, name);
2652 TREE_TYPE (name) = type;
2653 reset_decl_linkage (ti);
2655 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2656 if (TREE_CODE (m) == VAR_DECL)
2657 reset_decl_linkage (m);
2658 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2660 reset_decl_linkage (m);
2661 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (m))
2662 /* Also update its name, for cxx_dwarf_name. */
2663 DECL_NAME (m) = TYPE_IDENTIFIER (type);
2665 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2666 bt_reset_linkage_2, NULL);
2669 static void
2670 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2672 reset_type_linkage_2 (b->type);
2674 void
2675 reset_type_linkage (tree type)
2677 reset_type_linkage_1 (type);
2678 reset_type_linkage_2 (type);
2681 /* Set up our initial idea of what the linkage of DECL should be. */
2683 void
2684 tentative_decl_linkage (tree decl)
2686 if (DECL_INTERFACE_KNOWN (decl))
2687 /* We've already made a decision as to how this function will
2688 be handled. */;
2689 else if (vague_linkage_p (decl))
2691 if (TREE_CODE (decl) == FUNCTION_DECL
2692 && decl_defined_p (decl))
2694 DECL_EXTERNAL (decl) = 1;
2695 DECL_NOT_REALLY_EXTERN (decl) = 1;
2696 note_vague_linkage_fn (decl);
2697 /* A non-template inline function with external linkage will
2698 always be COMDAT. As we must eventually determine the
2699 linkage of all functions, and as that causes writes to
2700 the data mapped in from the PCH file, it's advantageous
2701 to mark the functions at this point. */
2702 if (DECL_DECLARED_INLINE_P (decl)
2703 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2704 || DECL_DEFAULTED_FN (decl)))
2706 /* This function must have external linkage, as
2707 otherwise DECL_INTERFACE_KNOWN would have been
2708 set. */
2709 gcc_assert (TREE_PUBLIC (decl));
2710 comdat_linkage (decl);
2711 DECL_INTERFACE_KNOWN (decl) = 1;
2714 else if (TREE_CODE (decl) == VAR_DECL)
2715 maybe_commonize_var (decl);
2719 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2720 for DECL has not already been determined, do so now by setting
2721 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2722 function is called entities with vague linkage whose definitions
2723 are available must have TREE_PUBLIC set.
2725 If this function decides to place DECL in COMDAT, it will set
2726 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2727 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2728 callers defer that decision until it is clear that DECL is actually
2729 required. */
2731 void
2732 import_export_decl (tree decl)
2734 int emit_p;
2735 bool comdat_p;
2736 bool import_p;
2737 tree class_type = NULL_TREE;
2739 if (DECL_INTERFACE_KNOWN (decl))
2740 return;
2742 /* We cannot determine what linkage to give to an entity with vague
2743 linkage until the end of the file. For example, a virtual table
2744 for a class will be defined if and only if the key method is
2745 defined in this translation unit. As a further example, consider
2746 that when compiling a translation unit that uses PCH file with
2747 "-frepo" it would be incorrect to make decisions about what
2748 entities to emit when building the PCH; those decisions must be
2749 delayed until the repository information has been processed. */
2750 gcc_assert (at_eof);
2751 /* Object file linkage for explicit instantiations is handled in
2752 mark_decl_instantiated. For static variables in functions with
2753 vague linkage, maybe_commonize_var is used.
2755 Therefore, the only declarations that should be provided to this
2756 function are those with external linkage that are:
2758 * implicit instantiations of function templates
2760 * inline function
2762 * implicit instantiations of static data members of class
2763 templates
2765 * virtual tables
2767 * typeinfo objects
2769 Furthermore, all entities that reach this point must have a
2770 definition available in this translation unit.
2772 The following assertions check these conditions. */
2773 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2774 /* Any code that creates entities with TREE_PUBLIC cleared should
2775 also set DECL_INTERFACE_KNOWN. */
2776 gcc_assert (TREE_PUBLIC (decl));
2777 if (TREE_CODE (decl) == FUNCTION_DECL)
2778 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2779 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2780 || DECL_DECLARED_INLINE_P (decl));
2781 else
2782 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2783 || DECL_VTABLE_OR_VTT_P (decl)
2784 || DECL_TINFO_P (decl));
2785 /* Check that a definition of DECL is available in this translation
2786 unit. */
2787 gcc_assert (!DECL_REALLY_EXTERN (decl));
2789 /* Assume that DECL will not have COMDAT linkage. */
2790 comdat_p = false;
2791 /* Assume that DECL will not be imported into this translation
2792 unit. */
2793 import_p = false;
2795 /* See if the repository tells us whether or not to emit DECL in
2796 this translation unit. */
2797 emit_p = repo_emit_p (decl);
2798 if (emit_p == 0)
2799 import_p = true;
2800 else if (emit_p == 1)
2802 /* The repository indicates that this entity should be defined
2803 here. Make sure the back end honors that request. */
2804 mark_needed (decl);
2805 /* Output the definition as an ordinary strong definition. */
2806 DECL_EXTERNAL (decl) = 0;
2807 DECL_INTERFACE_KNOWN (decl) = 1;
2808 return;
2811 if (import_p)
2812 /* We have already decided what to do with this DECL; there is no
2813 need to check anything further. */
2815 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2817 class_type = DECL_CONTEXT (decl);
2818 import_export_class (class_type);
2819 if (TYPE_FOR_JAVA (class_type))
2820 import_p = true;
2821 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2822 && CLASSTYPE_INTERFACE_ONLY (class_type))
2823 import_p = true;
2824 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2825 && !CLASSTYPE_USE_TEMPLATE (class_type)
2826 && CLASSTYPE_KEY_METHOD (class_type)
2827 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2828 /* The ABI requires that all virtual tables be emitted with
2829 COMDAT linkage. However, on systems where COMDAT symbols
2830 don't show up in the table of contents for a static
2831 archive, or on systems without weak symbols (where we
2832 approximate COMDAT linkage by using internal linkage), the
2833 linker will report errors about undefined symbols because
2834 it will not see the virtual table definition. Therefore,
2835 in the case that we know that the virtual table will be
2836 emitted in only one translation unit, we make the virtual
2837 table an ordinary definition with external linkage. */
2838 DECL_EXTERNAL (decl) = 0;
2839 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2841 /* CLASS_TYPE is being exported from this translation unit,
2842 so DECL should be defined here. */
2843 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2844 /* If a class is declared in a header with the "extern
2845 template" extension, then it will not be instantiated,
2846 even in translation units that would normally require
2847 it. Often such classes are explicitly instantiated in
2848 one translation unit. Therefore, the explicit
2849 instantiation must be made visible to other translation
2850 units. */
2851 DECL_EXTERNAL (decl) = 0;
2852 else
2854 /* The generic C++ ABI says that class data is always
2855 COMDAT, even if there is a key function. Some
2856 variants (e.g., the ARM EABI) says that class data
2857 only has COMDAT linkage if the class data might be
2858 emitted in more than one translation unit. When the
2859 key method can be inline and is inline, we still have
2860 to arrange for comdat even though
2861 class_data_always_comdat is false. */
2862 if (!CLASSTYPE_KEY_METHOD (class_type)
2863 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2864 || targetm.cxx.class_data_always_comdat ())
2866 /* The ABI requires COMDAT linkage. Normally, we
2867 only emit COMDAT things when they are needed;
2868 make sure that we realize that this entity is
2869 indeed needed. */
2870 comdat_p = true;
2871 mark_needed (decl);
2875 else if (!flag_implicit_templates
2876 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2877 import_p = true;
2878 else
2879 comdat_p = true;
2881 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2883 tree type = TREE_TYPE (DECL_NAME (decl));
2884 if (CLASS_TYPE_P (type))
2886 class_type = type;
2887 import_export_class (type);
2888 if (CLASSTYPE_INTERFACE_KNOWN (type)
2889 && TYPE_POLYMORPHIC_P (type)
2890 && CLASSTYPE_INTERFACE_ONLY (type)
2891 /* If -fno-rtti was specified, then we cannot be sure
2892 that RTTI information will be emitted with the
2893 virtual table of the class, so we must emit it
2894 wherever it is used. */
2895 && flag_rtti)
2896 import_p = true;
2897 else
2899 if (CLASSTYPE_INTERFACE_KNOWN (type)
2900 && !CLASSTYPE_INTERFACE_ONLY (type))
2902 comdat_p = (targetm.cxx.class_data_always_comdat ()
2903 || (CLASSTYPE_KEY_METHOD (type)
2904 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2905 mark_needed (decl);
2906 if (!flag_weak)
2908 comdat_p = false;
2909 DECL_EXTERNAL (decl) = 0;
2912 else
2913 comdat_p = true;
2916 else
2917 comdat_p = true;
2919 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2921 /* DECL is an implicit instantiation of a function or static
2922 data member. */
2923 if ((flag_implicit_templates
2924 && !flag_use_repository)
2925 || (flag_implicit_inline_templates
2926 && TREE_CODE (decl) == FUNCTION_DECL
2927 && DECL_DECLARED_INLINE_P (decl)))
2928 comdat_p = true;
2929 else
2930 /* If we are not implicitly generating templates, then mark
2931 this entity as undefined in this translation unit. */
2932 import_p = true;
2934 else if (DECL_FUNCTION_MEMBER_P (decl))
2936 if (!DECL_DECLARED_INLINE_P (decl))
2938 tree ctype = DECL_CONTEXT (decl);
2939 import_export_class (ctype);
2940 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2942 DECL_NOT_REALLY_EXTERN (decl)
2943 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2944 || (DECL_DECLARED_INLINE_P (decl)
2945 && ! flag_implement_inlines
2946 && !DECL_VINDEX (decl)));
2948 if (!DECL_NOT_REALLY_EXTERN (decl))
2949 DECL_EXTERNAL (decl) = 1;
2951 /* Always make artificials weak. */
2952 if (DECL_ARTIFICIAL (decl) && flag_weak)
2953 comdat_p = true;
2954 else
2955 maybe_make_one_only (decl);
2958 else
2959 comdat_p = true;
2961 else
2962 comdat_p = true;
2964 if (import_p)
2966 /* If we are importing DECL into this translation unit, mark is
2967 an undefined here. */
2968 DECL_EXTERNAL (decl) = 1;
2969 DECL_NOT_REALLY_EXTERN (decl) = 0;
2971 else if (comdat_p)
2973 /* If we decided to put DECL in COMDAT, mark it accordingly at
2974 this point. */
2975 comdat_linkage (decl);
2978 DECL_INTERFACE_KNOWN (decl) = 1;
2981 /* Return an expression that performs the destruction of DECL, which
2982 must be a VAR_DECL whose type has a non-trivial destructor, or is
2983 an array whose (innermost) elements have a non-trivial destructor. */
2985 tree
2986 build_cleanup (tree decl)
2988 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2989 gcc_assert (clean != NULL_TREE);
2990 return clean;
2993 /* Returns the initialization guard variable for the variable DECL,
2994 which has static storage duration. */
2996 tree
2997 get_guard (tree decl)
2999 tree sname;
3000 tree guard;
3002 sname = mangle_guard_variable (decl);
3003 guard = IDENTIFIER_GLOBAL_VALUE (sname);
3004 if (! guard)
3006 tree guard_type;
3008 /* We use a type that is big enough to contain a mutex as well
3009 as an integer counter. */
3010 guard_type = targetm.cxx.guard_type ();
3011 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3012 VAR_DECL, sname, guard_type);
3014 /* The guard should have the same linkage as what it guards. */
3015 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3016 TREE_STATIC (guard) = TREE_STATIC (decl);
3017 DECL_COMMON (guard) = DECL_COMMON (decl);
3018 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3019 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3020 if (DECL_ONE_ONLY (decl))
3021 make_decl_one_only (guard, cxx_comdat_group (guard));
3022 if (TREE_PUBLIC (decl))
3023 DECL_WEAK (guard) = DECL_WEAK (decl);
3024 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3025 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3027 DECL_ARTIFICIAL (guard) = 1;
3028 DECL_IGNORED_P (guard) = 1;
3029 TREE_USED (guard) = 1;
3030 pushdecl_top_level_and_finish (guard, NULL_TREE);
3032 return guard;
3035 /* Return those bits of the GUARD variable that should be set when the
3036 guarded entity is actually initialized. */
3038 static tree
3039 get_guard_bits (tree guard)
3041 if (!targetm.cxx.guard_mask_bit ())
3043 /* We only set the first byte of the guard, in order to leave room
3044 for a mutex in the high-order bits. */
3045 guard = build1 (ADDR_EXPR,
3046 build_pointer_type (TREE_TYPE (guard)),
3047 guard);
3048 guard = build1 (NOP_EXPR,
3049 build_pointer_type (char_type_node),
3050 guard);
3051 guard = build1 (INDIRECT_REF, char_type_node, guard);
3054 return guard;
3057 /* Return an expression which determines whether or not the GUARD
3058 variable has already been initialized. */
3060 tree
3061 get_guard_cond (tree guard)
3063 tree guard_value;
3065 /* Check to see if the GUARD is zero. */
3066 guard = get_guard_bits (guard);
3068 /* Mask off all but the low bit. */
3069 if (targetm.cxx.guard_mask_bit ())
3071 guard_value = integer_one_node;
3072 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3073 guard_value = convert (TREE_TYPE (guard), guard_value);
3074 guard = cp_build_binary_op (input_location,
3075 BIT_AND_EXPR, guard, guard_value,
3076 tf_warning_or_error);
3079 guard_value = integer_zero_node;
3080 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3081 guard_value = convert (TREE_TYPE (guard), guard_value);
3082 return cp_build_binary_op (input_location,
3083 EQ_EXPR, guard, guard_value,
3084 tf_warning_or_error);
3087 /* Return an expression which sets the GUARD variable, indicating that
3088 the variable being guarded has been initialized. */
3090 tree
3091 set_guard (tree guard)
3093 tree guard_init;
3095 /* Set the GUARD to one. */
3096 guard = get_guard_bits (guard);
3097 guard_init = integer_one_node;
3098 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3099 guard_init = convert (TREE_TYPE (guard), guard_init);
3100 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3101 tf_warning_or_error);
3104 /* Returns true iff we can tell that VAR does not have a dynamic
3105 initializer. */
3107 static bool
3108 var_defined_without_dynamic_init (tree var)
3110 /* If it's defined in another TU, we can't tell. */
3111 if (DECL_EXTERNAL (var))
3112 return false;
3113 /* If it has a non-trivial destructor, registering the destructor
3114 counts as dynamic initialization. */
3115 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3116 return false;
3117 /* If it's in this TU, its initializer has been processed, unless
3118 it's a case of self-initialization, then DECL_INITIALIZED_P is
3119 false while the initializer is handled by finish_id_expression. */
3120 if (!DECL_INITIALIZED_P (var))
3121 return false;
3122 /* If it has no initializer or a constant one, it's not dynamic. */
3123 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3124 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3127 /* Returns true iff VAR is a variable that needs uses to be
3128 wrapped for possible dynamic initialization. */
3130 static bool
3131 var_needs_tls_wrapper (tree var)
3133 return (!error_operand_p (var)
3134 && DECL_THREAD_LOCAL_P (var)
3135 && !DECL_GNU_TLS_P (var)
3136 && !DECL_FUNCTION_SCOPE_P (var)
3137 && !var_defined_without_dynamic_init (var));
3140 /* Get the FUNCTION_DECL for the shared TLS init function for this
3141 translation unit. */
3143 static tree
3144 get_local_tls_init_fn (void)
3146 tree sname = get_identifier ("__tls_init");
3147 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3148 if (!fn)
3150 fn = build_lang_decl (FUNCTION_DECL, sname,
3151 build_function_type (void_type_node,
3152 void_list_node));
3153 SET_DECL_LANGUAGE (fn, lang_c);
3154 TREE_PUBLIC (fn) = false;
3155 DECL_ARTIFICIAL (fn) = true;
3156 mark_used (fn);
3157 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3159 return fn;
3162 /* Get a FUNCTION_DECL for the init function for the thread_local
3163 variable VAR. The init function will be an alias to the function
3164 that initializes all the non-local TLS variables in the translation
3165 unit. The init function is only used by the wrapper function. */
3167 static tree
3168 get_tls_init_fn (tree var)
3170 /* Only C++11 TLS vars need this init fn. */
3171 if (!var_needs_tls_wrapper (var))
3172 return NULL_TREE;
3174 /* If -fno-extern-tls-init, assume that we don't need to call
3175 a tls init function for a variable defined in another TU. */
3176 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3177 return NULL_TREE;
3179 #ifdef ASM_OUTPUT_DEF
3180 /* If the variable is internal, or if we can't generate aliases,
3181 call the local init function directly. */
3182 if (!TREE_PUBLIC (var))
3183 #endif
3184 return get_local_tls_init_fn ();
3186 tree sname = mangle_tls_init_fn (var);
3187 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3188 if (!fn)
3190 fn = build_lang_decl (FUNCTION_DECL, sname,
3191 build_function_type (void_type_node,
3192 void_list_node));
3193 SET_DECL_LANGUAGE (fn, lang_c);
3194 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3195 DECL_ARTIFICIAL (fn) = true;
3196 DECL_COMDAT (fn) = DECL_COMDAT (var);
3197 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3198 if (DECL_ONE_ONLY (var))
3199 make_decl_one_only (fn, cxx_comdat_group (fn));
3200 if (TREE_PUBLIC (var))
3202 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3203 /* If the variable is defined somewhere else and might have static
3204 initialization, make the init function a weak reference. */
3205 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3206 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3207 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3208 && DECL_EXTERNAL (var))
3209 declare_weak (fn);
3210 else
3211 DECL_WEAK (fn) = DECL_WEAK (var);
3213 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3214 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3215 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3216 DECL_IGNORED_P (fn) = 1;
3217 mark_used (fn);
3219 DECL_BEFRIENDING_CLASSES (fn) = var;
3221 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3223 return fn;
3226 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3227 variable VAR. The wrapper function calls the init function (if any) for
3228 VAR and then returns a reference to VAR. The wrapper function is used
3229 in place of VAR everywhere VAR is mentioned. */
3231 tree
3232 get_tls_wrapper_fn (tree var)
3234 /* Only C++11 TLS vars need this wrapper fn. */
3235 if (!var_needs_tls_wrapper (var))
3236 return NULL_TREE;
3238 tree sname = mangle_tls_wrapper_fn (var);
3239 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3240 if (!fn)
3242 /* A named rvalue reference is an lvalue, so the wrapper should
3243 always return an lvalue reference. */
3244 tree type = non_reference (TREE_TYPE (var));
3245 type = build_reference_type (type);
3246 tree fntype = build_function_type (type, void_list_node);
3247 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3248 SET_DECL_LANGUAGE (fn, lang_c);
3249 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3250 DECL_ARTIFICIAL (fn) = true;
3251 DECL_IGNORED_P (fn) = 1;
3252 /* The wrapper is inline and emitted everywhere var is used. */
3253 DECL_DECLARED_INLINE_P (fn) = true;
3254 if (TREE_PUBLIC (var))
3256 comdat_linkage (fn);
3257 #ifdef HAVE_GAS_HIDDEN
3258 /* Make the wrapper bind locally; there's no reason to share
3259 the wrapper between multiple shared objects. */
3260 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3261 DECL_VISIBILITY_SPECIFIED (fn) = true;
3262 #endif
3264 if (!TREE_PUBLIC (fn))
3265 DECL_INTERFACE_KNOWN (fn) = true;
3266 mark_used (fn);
3267 note_vague_linkage_fn (fn);
3269 #if 0
3270 /* We want CSE to commonize calls to the wrapper, but marking it as
3271 pure is unsafe since it has side-effects. I guess we need a new
3272 ECF flag even weaker than ECF_PURE. FIXME! */
3273 DECL_PURE_P (fn) = true;
3274 #endif
3276 DECL_BEFRIENDING_CLASSES (fn) = var;
3278 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3280 return fn;
3283 /* At EOF, generate the definition for the TLS wrapper function FN:
3285 T& var_wrapper() {
3286 if (init_fn) init_fn();
3287 return var;
3288 } */
3290 static void
3291 generate_tls_wrapper (tree fn)
3293 tree var = DECL_BEFRIENDING_CLASSES (fn);
3295 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3296 tree body = begin_function_body ();
3297 /* Only call the init fn if there might be one. */
3298 if (tree init_fn = get_tls_init_fn (var))
3300 tree if_stmt = NULL_TREE;
3301 /* If init_fn is a weakref, make sure it exists before calling. */
3302 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3304 if_stmt = begin_if_stmt ();
3305 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3306 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3307 NE_EXPR, addr, nullptr_node,
3308 tf_warning_or_error);
3309 finish_if_stmt_cond (cond, if_stmt);
3311 finish_expr_stmt (build_cxx_call
3312 (init_fn, 0, NULL, tf_warning_or_error));
3313 if (if_stmt)
3315 finish_then_clause (if_stmt);
3316 finish_if_stmt (if_stmt);
3319 else
3320 /* If there's no initialization, the wrapper is a constant function. */
3321 TREE_READONLY (fn) = true;
3322 finish_return_stmt (convert_from_reference (var));
3323 finish_function_body (body);
3324 expand_or_defer_fn (finish_function (0));
3327 /* Start the process of running a particular set of global constructors
3328 or destructors. Subroutine of do_[cd]tors. Also called from
3329 vtv_start_verification_constructor_init_function. */
3331 static tree
3332 start_objects (int method_type, int initp)
3334 tree body;
3335 tree fndecl;
3336 char type[14];
3338 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3340 if (initp != DEFAULT_INIT_PRIORITY)
3342 char joiner;
3344 #ifdef JOINER
3345 joiner = JOINER;
3346 #else
3347 joiner = '_';
3348 #endif
3350 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3352 else
3353 sprintf (type, "sub_%c", method_type);
3355 fndecl = build_lang_decl (FUNCTION_DECL,
3356 get_file_function_name (type),
3357 build_function_type_list (void_type_node,
3358 NULL_TREE));
3359 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3361 TREE_PUBLIC (current_function_decl) = 0;
3363 /* Mark as artificial because it's not explicitly in the user's
3364 source code. */
3365 DECL_ARTIFICIAL (current_function_decl) = 1;
3367 /* Mark this declaration as used to avoid spurious warnings. */
3368 TREE_USED (current_function_decl) = 1;
3370 /* Mark this function as a global constructor or destructor. */
3371 if (method_type == 'I')
3372 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3373 else
3374 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3376 body = begin_compound_stmt (BCS_FN_BODY);
3378 return body;
3381 /* Finish the process of running a particular set of global constructors
3382 or destructors. Subroutine of do_[cd]tors. */
3384 static void
3385 finish_objects (int method_type, int initp, tree body)
3387 tree fn;
3389 /* Finish up. */
3390 finish_compound_stmt (body);
3391 fn = finish_function (0);
3393 if (method_type == 'I')
3395 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3396 decl_init_priority_insert (fn, initp);
3398 else
3400 DECL_STATIC_DESTRUCTOR (fn) = 1;
3401 decl_fini_priority_insert (fn, initp);
3404 expand_or_defer_fn (fn);
3407 /* The names of the parameters to the function created to handle
3408 initializations and destructions for objects with static storage
3409 duration. */
3410 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3411 #define PRIORITY_IDENTIFIER "__priority"
3413 /* The name of the function we create to handle initializations and
3414 destructions for objects with static storage duration. */
3415 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3417 /* The declaration for the __INITIALIZE_P argument. */
3418 static GTY(()) tree initialize_p_decl;
3420 /* The declaration for the __PRIORITY argument. */
3421 static GTY(()) tree priority_decl;
3423 /* The declaration for the static storage duration function. */
3424 static GTY(()) tree ssdf_decl;
3426 /* All the static storage duration functions created in this
3427 translation unit. */
3428 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3430 /* A map from priority levels to information about that priority
3431 level. There may be many such levels, so efficient lookup is
3432 important. */
3433 static splay_tree priority_info_map;
3435 /* Begins the generation of the function that will handle all
3436 initialization and destruction of objects with static storage
3437 duration. The function generated takes two parameters of type
3438 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3439 nonzero, it performs initializations. Otherwise, it performs
3440 destructions. It only performs those initializations or
3441 destructions with the indicated __PRIORITY. The generated function
3442 returns no value.
3444 It is assumed that this function will only be called once per
3445 translation unit. */
3447 static tree
3448 start_static_storage_duration_function (unsigned count)
3450 tree type;
3451 tree body;
3452 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3454 /* Create the identifier for this function. It will be of the form
3455 SSDF_IDENTIFIER_<number>. */
3456 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3458 type = build_function_type_list (void_type_node,
3459 integer_type_node, integer_type_node,
3460 NULL_TREE);
3462 /* Create the FUNCTION_DECL itself. */
3463 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3464 get_identifier (id),
3465 type);
3466 TREE_PUBLIC (ssdf_decl) = 0;
3467 DECL_ARTIFICIAL (ssdf_decl) = 1;
3469 /* Put this function in the list of functions to be called from the
3470 static constructors and destructors. */
3471 if (!ssdf_decls)
3473 vec_alloc (ssdf_decls, 32);
3475 /* Take this opportunity to initialize the map from priority
3476 numbers to information about that priority level. */
3477 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3478 /*delete_key_fn=*/0,
3479 /*delete_value_fn=*/
3480 (splay_tree_delete_value_fn) &free);
3482 /* We always need to generate functions for the
3483 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3484 priorities later, we'll be sure to find the
3485 DEFAULT_INIT_PRIORITY. */
3486 get_priority_info (DEFAULT_INIT_PRIORITY);
3489 vec_safe_push (ssdf_decls, ssdf_decl);
3491 /* Create the argument list. */
3492 initialize_p_decl = cp_build_parm_decl
3493 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3494 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3495 TREE_USED (initialize_p_decl) = 1;
3496 priority_decl = cp_build_parm_decl
3497 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3498 DECL_CONTEXT (priority_decl) = ssdf_decl;
3499 TREE_USED (priority_decl) = 1;
3501 DECL_CHAIN (initialize_p_decl) = priority_decl;
3502 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3504 /* Put the function in the global scope. */
3505 pushdecl (ssdf_decl);
3507 /* Start the function itself. This is equivalent to declaring the
3508 function as:
3510 static void __ssdf (int __initialize_p, init __priority_p);
3512 It is static because we only need to call this function from the
3513 various constructor and destructor functions for this module. */
3514 start_preparsed_function (ssdf_decl,
3515 /*attrs=*/NULL_TREE,
3516 SF_PRE_PARSED);
3518 /* Set up the scope of the outermost block in the function. */
3519 body = begin_compound_stmt (BCS_FN_BODY);
3521 return body;
3524 /* Finish the generation of the function which performs initialization
3525 and destruction of objects with static storage duration. After
3526 this point, no more such objects can be created. */
3528 static void
3529 finish_static_storage_duration_function (tree body)
3531 /* Close out the function. */
3532 finish_compound_stmt (body);
3533 expand_or_defer_fn (finish_function (0));
3536 /* Return the information about the indicated PRIORITY level. If no
3537 code to handle this level has yet been generated, generate the
3538 appropriate prologue. */
3540 static priority_info
3541 get_priority_info (int priority)
3543 priority_info pi;
3544 splay_tree_node n;
3546 n = splay_tree_lookup (priority_info_map,
3547 (splay_tree_key) priority);
3548 if (!n)
3550 /* Create a new priority information structure, and insert it
3551 into the map. */
3552 pi = XNEW (struct priority_info_s);
3553 pi->initializations_p = 0;
3554 pi->destructions_p = 0;
3555 splay_tree_insert (priority_info_map,
3556 (splay_tree_key) priority,
3557 (splay_tree_value) pi);
3559 else
3560 pi = (priority_info) n->value;
3562 return pi;
3565 /* The effective initialization priority of a DECL. */
3567 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3568 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3569 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3571 /* Whether a DECL needs a guard to protect it against multiple
3572 initialization. */
3574 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3575 || DECL_ONE_ONLY (decl) \
3576 || DECL_WEAK (decl)))
3578 /* Called from one_static_initialization_or_destruction(),
3579 via walk_tree.
3580 Walks the initializer list of a global variable and looks for
3581 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3582 and that have their DECL_CONTEXT() == NULL.
3583 For each such temporary variable, set their DECL_CONTEXT() to
3584 the current function. This is necessary because otherwise
3585 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3586 when trying to refer to a temporary variable that does not have
3587 it's DECL_CONTECT() properly set. */
3588 static tree
3589 fix_temporary_vars_context_r (tree *node,
3590 int * /*unused*/,
3591 void * /*unused1*/)
3593 gcc_assert (current_function_decl);
3595 if (TREE_CODE (*node) == BIND_EXPR)
3597 tree var;
3599 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3600 if (VAR_P (var)
3601 && !DECL_NAME (var)
3602 && DECL_ARTIFICIAL (var)
3603 && !DECL_CONTEXT (var))
3604 DECL_CONTEXT (var) = current_function_decl;
3607 return NULL_TREE;
3610 /* Set up to handle the initialization or destruction of DECL. If
3611 INITP is nonzero, we are initializing the variable. Otherwise, we
3612 are destroying it. */
3614 static void
3615 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3617 tree guard_if_stmt = NULL_TREE;
3618 tree guard;
3620 /* If we are supposed to destruct and there's a trivial destructor,
3621 nothing has to be done. */
3622 if (!initp
3623 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3624 return;
3626 /* Trick the compiler into thinking we are at the file and line
3627 where DECL was declared so that error-messages make sense, and so
3628 that the debugger will show somewhat sensible file and line
3629 information. */
3630 input_location = DECL_SOURCE_LOCATION (decl);
3632 /* Make sure temporary variables in the initialiser all have
3633 their DECL_CONTEXT() set to a value different from NULL_TREE.
3634 This can happen when global variables initialisers are built.
3635 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3636 the temporary variables that might have been generated in the
3637 accompagning initialisers is NULL_TREE, meaning the variables have been
3638 declared in the global namespace.
3639 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3640 of the temporaries are set to the current function decl. */
3641 cp_walk_tree_without_duplicates (&init,
3642 fix_temporary_vars_context_r,
3643 NULL);
3645 /* Because of:
3647 [class.access.spec]
3649 Access control for implicit calls to the constructors,
3650 the conversion functions, or the destructor called to
3651 create and destroy a static data member is performed as
3652 if these calls appeared in the scope of the member's
3653 class.
3655 we pretend we are in a static member function of the class of
3656 which the DECL is a member. */
3657 if (member_p (decl))
3659 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3660 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3663 /* Assume we don't need a guard. */
3664 guard = NULL_TREE;
3665 /* We need a guard if this is an object with external linkage that
3666 might be initialized in more than one place. (For example, a
3667 static data member of a template, when the data member requires
3668 construction.) */
3669 if (NEEDS_GUARD_P (decl))
3671 tree guard_cond;
3673 guard = get_guard (decl);
3675 /* When using __cxa_atexit, we just check the GUARD as we would
3676 for a local static. */
3677 if (flag_use_cxa_atexit)
3679 /* When using __cxa_atexit, we never try to destroy
3680 anything from a static destructor. */
3681 gcc_assert (initp);
3682 guard_cond = get_guard_cond (guard);
3684 /* If we don't have __cxa_atexit, then we will be running
3685 destructors from .fini sections, or their equivalents. So,
3686 we need to know how many times we've tried to initialize this
3687 object. We do initializations only if the GUARD is zero,
3688 i.e., if we are the first to initialize the variable. We do
3689 destructions only if the GUARD is one, i.e., if we are the
3690 last to destroy the variable. */
3691 else if (initp)
3692 guard_cond
3693 = cp_build_binary_op (input_location,
3694 EQ_EXPR,
3695 cp_build_unary_op (PREINCREMENT_EXPR,
3696 guard,
3697 /*noconvert=*/1,
3698 tf_warning_or_error),
3699 integer_one_node,
3700 tf_warning_or_error);
3701 else
3702 guard_cond
3703 = cp_build_binary_op (input_location,
3704 EQ_EXPR,
3705 cp_build_unary_op (PREDECREMENT_EXPR,
3706 guard,
3707 /*noconvert=*/1,
3708 tf_warning_or_error),
3709 integer_zero_node,
3710 tf_warning_or_error);
3712 guard_if_stmt = begin_if_stmt ();
3713 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3717 /* If we're using __cxa_atexit, we have not already set the GUARD,
3718 so we must do so now. */
3719 if (guard && initp && flag_use_cxa_atexit)
3720 finish_expr_stmt (set_guard (guard));
3722 /* Perform the initialization or destruction. */
3723 if (initp)
3725 if (init)
3727 finish_expr_stmt (init);
3728 if (flag_sanitize & SANITIZE_ADDRESS)
3730 varpool_node *vnode = varpool_node::get (decl);
3731 if (vnode)
3732 vnode->dynamically_initialized = 1;
3736 /* If we're using __cxa_atexit, register a function that calls the
3737 destructor for the object. */
3738 if (flag_use_cxa_atexit)
3739 finish_expr_stmt (register_dtor_fn (decl));
3741 else
3742 finish_expr_stmt (build_cleanup (decl));
3744 /* Finish the guard if-stmt, if necessary. */
3745 if (guard)
3747 finish_then_clause (guard_if_stmt);
3748 finish_if_stmt (guard_if_stmt);
3751 /* Now that we're done with DECL we don't need to pretend to be a
3752 member of its class any longer. */
3753 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3754 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3757 /* Generate code to do the initialization or destruction of the decls in VARS,
3758 a TREE_LIST of VAR_DECL with static storage duration.
3759 Whether initialization or destruction is performed is specified by INITP. */
3761 static void
3762 do_static_initialization_or_destruction (tree vars, bool initp)
3764 tree node, init_if_stmt, cond;
3766 /* Build the outer if-stmt to check for initialization or destruction. */
3767 init_if_stmt = begin_if_stmt ();
3768 cond = initp ? integer_one_node : integer_zero_node;
3769 cond = cp_build_binary_op (input_location,
3770 EQ_EXPR,
3771 initialize_p_decl,
3772 cond,
3773 tf_warning_or_error);
3774 finish_if_stmt_cond (cond, init_if_stmt);
3776 /* To make sure dynamic construction doesn't access globals from other
3777 compilation units where they might not be yet constructed, for
3778 -fsanitize=address insert __asan_before_dynamic_init call that
3779 prevents access to either all global variables that need construction
3780 in other compilation units, or at least those that haven't been
3781 initialized yet. Variables that need dynamic construction in
3782 the current compilation unit are kept accessible. */
3783 if (flag_sanitize & SANITIZE_ADDRESS)
3784 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3786 node = vars;
3787 do {
3788 tree decl = TREE_VALUE (node);
3789 tree priority_if_stmt;
3790 int priority;
3791 priority_info pi;
3793 /* If we don't need a destructor, there's nothing to do. Avoid
3794 creating a possibly empty if-stmt. */
3795 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3797 node = TREE_CHAIN (node);
3798 continue;
3801 /* Remember that we had an initialization or finalization at this
3802 priority. */
3803 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3804 pi = get_priority_info (priority);
3805 if (initp)
3806 pi->initializations_p = 1;
3807 else
3808 pi->destructions_p = 1;
3810 /* Conditionalize this initialization on being in the right priority
3811 and being initializing/finalizing appropriately. */
3812 priority_if_stmt = begin_if_stmt ();
3813 cond = cp_build_binary_op (input_location,
3814 EQ_EXPR,
3815 priority_decl,
3816 build_int_cst (NULL_TREE, priority),
3817 tf_warning_or_error);
3818 finish_if_stmt_cond (cond, priority_if_stmt);
3820 /* Process initializers with same priority. */
3821 for (; node
3822 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3823 node = TREE_CHAIN (node))
3824 /* Do one initialization or destruction. */
3825 one_static_initialization_or_destruction (TREE_VALUE (node),
3826 TREE_PURPOSE (node), initp);
3828 /* Finish up the priority if-stmt body. */
3829 finish_then_clause (priority_if_stmt);
3830 finish_if_stmt (priority_if_stmt);
3832 } while (node);
3834 /* Revert what __asan_before_dynamic_init did by calling
3835 __asan_after_dynamic_init. */
3836 if (flag_sanitize & SANITIZE_ADDRESS)
3837 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3839 /* Finish up the init/destruct if-stmt body. */
3840 finish_then_clause (init_if_stmt);
3841 finish_if_stmt (init_if_stmt);
3844 /* VARS is a list of variables with static storage duration which may
3845 need initialization and/or finalization. Remove those variables
3846 that don't really need to be initialized or finalized, and return
3847 the resulting list. The order in which the variables appear in
3848 VARS is in reverse order of the order in which they should actually
3849 be initialized. The list we return is in the unreversed order;
3850 i.e., the first variable should be initialized first. */
3852 static tree
3853 prune_vars_needing_no_initialization (tree *vars)
3855 tree *var = vars;
3856 tree result = NULL_TREE;
3858 while (*var)
3860 tree t = *var;
3861 tree decl = TREE_VALUE (t);
3862 tree init = TREE_PURPOSE (t);
3864 /* Deal gracefully with error. */
3865 if (decl == error_mark_node)
3867 var = &TREE_CHAIN (t);
3868 continue;
3871 /* The only things that can be initialized are variables. */
3872 gcc_assert (VAR_P (decl));
3874 /* If this object is not defined, we don't need to do anything
3875 here. */
3876 if (DECL_EXTERNAL (decl))
3878 var = &TREE_CHAIN (t);
3879 continue;
3882 /* Also, if the initializer already contains errors, we can bail
3883 out now. */
3884 if (init && TREE_CODE (init) == TREE_LIST
3885 && value_member (error_mark_node, init))
3887 var = &TREE_CHAIN (t);
3888 continue;
3891 /* This variable is going to need initialization and/or
3892 finalization, so we add it to the list. */
3893 *var = TREE_CHAIN (t);
3894 TREE_CHAIN (t) = result;
3895 result = t;
3898 return result;
3901 /* Make sure we have told the back end about all the variables in
3902 VARS. */
3904 static void
3905 write_out_vars (tree vars)
3907 tree v;
3909 for (v = vars; v; v = TREE_CHAIN (v))
3911 tree var = TREE_VALUE (v);
3912 if (!var_finalized_p (var))
3914 import_export_decl (var);
3915 rest_of_decl_compilation (var, 1, 1);
3920 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3921 (otherwise) that will initialize all global objects with static
3922 storage duration having the indicated PRIORITY. */
3924 static void
3925 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3926 location_t *locus)
3928 char function_key;
3929 tree fndecl;
3930 tree body;
3931 size_t i;
3933 input_location = *locus;
3934 /* ??? */
3935 /* Was: locus->line++; */
3937 /* We use `I' to indicate initialization and `D' to indicate
3938 destruction. */
3939 function_key = constructor_p ? 'I' : 'D';
3941 /* We emit the function lazily, to avoid generating empty
3942 global constructors and destructors. */
3943 body = NULL_TREE;
3945 /* For Objective-C++, we may need to initialize metadata found in this module.
3946 This must be done _before_ any other static initializations. */
3947 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3948 && constructor_p && objc_static_init_needed_p ())
3950 body = start_objects (function_key, priority);
3951 objc_generate_static_init_call (NULL_TREE);
3954 /* Call the static storage duration function with appropriate
3955 arguments. */
3956 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3958 /* Calls to pure or const functions will expand to nothing. */
3959 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3961 tree call;
3963 if (! body)
3964 body = start_objects (function_key, priority);
3966 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3967 build_int_cst (NULL_TREE,
3968 constructor_p),
3969 build_int_cst (NULL_TREE,
3970 priority),
3971 NULL_TREE);
3972 finish_expr_stmt (call);
3976 /* Close out the function. */
3977 if (body)
3978 finish_objects (function_key, priority, body);
3981 /* Generate constructor and destructor functions for the priority
3982 indicated by N. */
3984 static int
3985 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3987 location_t *locus = (location_t *) data;
3988 int priority = (int) n->key;
3989 priority_info pi = (priority_info) n->value;
3991 /* Generate the functions themselves, but only if they are really
3992 needed. */
3993 if (pi->initializations_p)
3994 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3995 if (pi->destructions_p)
3996 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3998 /* Keep iterating. */
3999 return 0;
4002 /* Java requires that we be able to reference a local address for a
4003 method, and not be confused by PLT entries. If supported, create a
4004 hidden alias for all such methods. */
4006 static void
4007 build_java_method_aliases (void)
4009 #ifndef HAVE_GAS_HIDDEN
4010 return;
4011 #endif
4013 struct cgraph_node *node;
4014 FOR_EACH_FUNCTION (node)
4016 tree fndecl = node->decl;
4018 if (DECL_CLASS_SCOPE_P (fndecl)
4019 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4020 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4022 /* Mangle the name in a predictable way; we need to reference
4023 this from a java compiled object file. */
4024 tree oid = DECL_ASSEMBLER_NAME (fndecl);
4025 const char *oname = IDENTIFIER_POINTER (oid);
4026 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4027 char *nname = ACONCAT (("_ZGA", oname + 2, NULL));
4029 tree alias = make_alias_for (fndecl, get_identifier (nname));
4030 TREE_PUBLIC (alias) = 1;
4031 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4033 cgraph_node::create_same_body_alias (alias, fndecl);
4038 /* Return C++ property of T, based on given operation OP. */
4040 static int
4041 cpp_check (tree t, cpp_operation op)
4043 switch (op)
4045 case IS_ABSTRACT:
4046 return DECL_PURE_VIRTUAL_P (t);
4047 case IS_CONSTRUCTOR:
4048 return DECL_CONSTRUCTOR_P (t);
4049 case IS_DESTRUCTOR:
4050 return DECL_DESTRUCTOR_P (t);
4051 case IS_COPY_CONSTRUCTOR:
4052 return DECL_COPY_CONSTRUCTOR_P (t);
4053 case IS_TEMPLATE:
4054 return TREE_CODE (t) == TEMPLATE_DECL;
4055 case IS_TRIVIAL:
4056 return trivial_type_p (t);
4057 default:
4058 return 0;
4062 /* Collect source file references recursively, starting from NAMESPC. */
4064 static void
4065 collect_source_refs (tree namespc)
4067 tree t;
4069 if (!namespc)
4070 return;
4072 /* Iterate over names in this name space. */
4073 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4074 if (!DECL_IS_BUILTIN (t) )
4075 collect_source_ref (DECL_SOURCE_FILE (t));
4077 /* Dump siblings, if any */
4078 collect_source_refs (TREE_CHAIN (namespc));
4080 /* Dump children, if any */
4081 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4084 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4085 starting from NAMESPC. */
4087 static void
4088 collect_ada_namespace (tree namespc, const char *source_file)
4090 if (!namespc)
4091 return;
4093 /* Collect decls from this namespace */
4094 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4096 /* Collect siblings, if any */
4097 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4099 /* Collect children, if any */
4100 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4103 /* Returns true iff there is a definition available for variable or
4104 function DECL. */
4106 static bool
4107 decl_defined_p (tree decl)
4109 if (TREE_CODE (decl) == FUNCTION_DECL)
4110 return (DECL_INITIAL (decl) != NULL_TREE);
4111 else
4113 gcc_assert (VAR_P (decl));
4114 return !DECL_EXTERNAL (decl);
4118 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4120 [expr.const]
4122 An integral constant-expression can only involve ... const
4123 variables of integral or enumeration types initialized with
4124 constant expressions ...
4126 C++0x also allows constexpr variables and temporaries initialized
4127 with constant expressions. We handle the former here, but the latter
4128 are just folded away in cxx_eval_constant_expression.
4130 The standard does not require that the expression be non-volatile.
4131 G++ implements the proposed correction in DR 457. */
4133 bool
4134 decl_constant_var_p (tree decl)
4136 if (!decl_maybe_constant_var_p (decl))
4137 return false;
4139 /* We don't know if a template static data member is initialized with
4140 a constant expression until we instantiate its initializer. Even
4141 in the case of a constexpr variable, we can't treat it as a
4142 constant until its initializer is complete in case it's used in
4143 its own initializer. */
4144 mark_used (decl);
4145 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4148 /* Returns true if DECL could be a symbolic constant variable, depending on
4149 its initializer. */
4151 bool
4152 decl_maybe_constant_var_p (tree decl)
4154 tree type = TREE_TYPE (decl);
4155 if (!VAR_P (decl))
4156 return false;
4157 if (DECL_DECLARED_CONSTEXPR_P (decl))
4158 return true;
4159 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4160 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4163 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4164 called from grokfndecl and grokvardecl; in all modes it is called from
4165 cp_write_global_declarations. */
4167 void
4168 no_linkage_error (tree decl)
4170 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4171 /* In C++11 it's ok if the decl is defined. */
4172 return;
4173 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4174 if (t == NULL_TREE)
4175 /* The type that got us on no_linkage_decls must have gotten a name for
4176 linkage purposes. */;
4177 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4178 /* The type might end up having a typedef name for linkage purposes. */
4179 vec_safe_push (no_linkage_decls, decl);
4180 else if (TYPE_ANONYMOUS_P (t))
4182 bool d = false;
4183 if (cxx_dialect >= cxx11)
4184 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4185 "anonymous type, is used but never defined", decl);
4186 else if (DECL_EXTERN_C_P (decl))
4187 /* Allow this; it's pretty common in C. */;
4188 else if (TREE_CODE (decl) == VAR_DECL)
4189 /* DRs 132, 319 and 389 seem to indicate types with
4190 no linkage can only be used to declare extern "C"
4191 entities. Since it's not always an error in the
4192 ISO C++ 90 Standard, we only issue a warning. */
4193 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4194 "with no linkage used to declare variable %q#D with "
4195 "linkage", decl);
4196 else
4197 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4198 "linkage used to declare function %q#D with linkage",
4199 decl);
4200 if (d && is_typedef_decl (TYPE_NAME (t)))
4201 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4202 "to the unqualified type, so it is not used for linkage",
4203 TYPE_NAME (t));
4205 else if (cxx_dialect >= cxx11)
4206 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4207 "%qT, is used but never defined", decl, t);
4208 else if (TREE_CODE (decl) == VAR_DECL)
4209 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4210 "used to declare variable %q#D with linkage", t, decl);
4211 else
4212 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4213 "to declare function %q#D with linkage", t, decl);
4216 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4218 static void
4219 collect_all_refs (const char *source_file)
4221 collect_ada_namespace (global_namespace, source_file);
4224 /* Clear DECL_EXTERNAL for NODE. */
4226 static bool
4227 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4229 DECL_EXTERNAL (node->decl) = 0;
4230 return false;
4233 /* Build up the function to run dynamic initializers for thread_local
4234 variables in this translation unit and alias the init functions for the
4235 individual variables to it. */
4237 static void
4238 handle_tls_init (void)
4240 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4241 if (vars == NULL_TREE)
4242 return;
4244 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4246 write_out_vars (vars);
4248 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4249 boolean_type_node);
4250 TREE_PUBLIC (guard) = false;
4251 TREE_STATIC (guard) = true;
4252 DECL_ARTIFICIAL (guard) = true;
4253 DECL_IGNORED_P (guard) = true;
4254 TREE_USED (guard) = true;
4255 set_decl_tls_model (guard, decl_default_tls_model (guard));
4256 pushdecl_top_level_and_finish (guard, NULL_TREE);
4258 tree fn = get_local_tls_init_fn ();
4259 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4260 tree body = begin_function_body ();
4261 tree if_stmt = begin_if_stmt ();
4262 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4263 tf_warning_or_error);
4264 finish_if_stmt_cond (cond, if_stmt);
4265 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4266 tf_warning_or_error));
4267 for (; vars; vars = TREE_CHAIN (vars))
4269 tree var = TREE_VALUE (vars);
4270 tree init = TREE_PURPOSE (vars);
4271 one_static_initialization_or_destruction (var, init, true);
4273 #ifdef ASM_OUTPUT_DEF
4274 /* Output init aliases even with -fno-extern-tls-init. */
4275 if (TREE_PUBLIC (var))
4277 tree single_init_fn = get_tls_init_fn (var);
4278 if (single_init_fn == NULL_TREE)
4279 continue;
4280 cgraph_node *alias
4281 = cgraph_node::get_create (fn)->create_same_body_alias
4282 (single_init_fn, fn);
4283 gcc_assert (alias != NULL);
4285 #endif
4288 finish_then_clause (if_stmt);
4289 finish_if_stmt (if_stmt);
4290 finish_function_body (body);
4291 expand_or_defer_fn (finish_function (0));
4294 /* We're at the end of compilation, so generate any mangling aliases that
4295 we've been saving up, if DECL is going to be output and ID2 isn't
4296 already taken by another declaration. */
4298 static void
4299 generate_mangling_alias (tree decl, tree id2)
4301 /* If there's a declaration already using this mangled name,
4302 don't create a compatibility alias that conflicts. */
4303 if (IDENTIFIER_GLOBAL_VALUE (id2))
4304 return;
4306 struct cgraph_node *n = NULL;
4307 if (TREE_CODE (decl) == FUNCTION_DECL
4308 && !(n = cgraph_node::get (decl)))
4309 /* Don't create an alias to an unreferenced function. */
4310 return;
4312 tree alias = make_alias_for (decl, id2);
4313 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4314 DECL_IGNORED_P (alias) = 1;
4315 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4316 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4317 if (vague_linkage_p (decl))
4318 DECL_WEAK (alias) = 1;
4319 if (TREE_CODE (decl) == FUNCTION_DECL)
4320 n->create_same_body_alias (alias, decl);
4321 else
4322 varpool_node::create_extra_name_alias (alias, decl);
4325 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4326 the end of translation, for compatibility across bugs in the mangling
4327 implementation. */
4329 void
4330 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4332 #ifdef ASM_OUTPUT_DEF
4333 if (at_eof)
4334 generate_mangling_alias (decl, id2);
4335 else
4337 vec_safe_push (mangling_aliases, decl);
4338 vec_safe_push (mangling_aliases, id2);
4340 #endif
4343 static void
4344 generate_mangling_aliases ()
4346 while (!vec_safe_is_empty (mangling_aliases))
4348 tree id2 = mangling_aliases->pop();
4349 tree decl = mangling_aliases->pop();
4350 generate_mangling_alias (decl, id2);
4354 /* The entire file is now complete. If requested, dump everything
4355 to a file. */
4357 static void
4358 dump_tu (void)
4360 int flags;
4361 FILE *stream = dump_begin (TDI_tu, &flags);
4363 if (stream)
4365 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4366 dump_end (TDI_tu, stream);
4370 /* Check the deallocation functions for CODE to see if we want to warn that
4371 only one was defined. */
4373 static void
4374 maybe_warn_sized_delete (enum tree_code code)
4376 tree sized = NULL_TREE;
4377 tree unsized = NULL_TREE;
4379 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4380 ovl; ovl = OVL_NEXT (ovl))
4382 tree fn = OVL_CURRENT (ovl);
4383 /* We're only interested in usual deallocation functions. */
4384 if (!non_placement_deallocation_fn_p (fn))
4385 continue;
4386 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4387 unsized = fn;
4388 else
4389 sized = fn;
4391 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4392 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4393 "the program should also define %qD", sized);
4394 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4395 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4396 "the program should also define %qD", unsized);
4399 /* Check the global deallocation functions to see if we want to warn about
4400 defining unsized without sized (or vice versa). */
4402 static void
4403 maybe_warn_sized_delete ()
4405 if (!flag_sized_deallocation || !warn_sized_deallocation)
4406 return;
4407 maybe_warn_sized_delete (DELETE_EXPR);
4408 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4411 /* This routine is called at the end of compilation.
4412 Its job is to create all the code needed to initialize and
4413 destroy the global aggregates. We do the destruction
4414 first, since that way we only need to reverse the decls once. */
4416 void
4417 cp_write_global_declarations (void)
4419 tree vars;
4420 bool reconsider;
4421 size_t i;
4422 location_t locus;
4423 unsigned ssdf_count = 0;
4424 int retries = 0;
4425 tree decl;
4427 locus = input_location;
4428 at_eof = 1;
4430 /* Bad parse errors. Just forget about it. */
4431 if (! global_bindings_p () || current_class_type
4432 || !vec_safe_is_empty (decl_namespace_list))
4433 return;
4435 /* This is the point to write out a PCH if we're doing that.
4436 In that case we do not want to do anything else. */
4437 if (pch_file)
4439 c_common_write_pch ();
4440 dump_tu ();
4441 return;
4444 symtab->process_same_body_aliases ();
4446 /* Handle -fdump-ada-spec[-slim] */
4447 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4449 if (flag_dump_ada_spec_slim)
4450 collect_source_ref (main_input_filename);
4451 else
4452 collect_source_refs (global_namespace);
4454 dump_ada_specs (collect_all_refs, cpp_check);
4457 /* FIXME - huh? was input_line -= 1;*/
4459 timevar_start (TV_PHASE_DEFERRED);
4461 /* We now have to write out all the stuff we put off writing out.
4462 These include:
4464 o Template specializations that we have not yet instantiated,
4465 but which are needed.
4466 o Initialization and destruction for non-local objects with
4467 static storage duration. (Local objects with static storage
4468 duration are initialized when their scope is first entered,
4469 and are cleaned up via atexit.)
4470 o Virtual function tables.
4472 All of these may cause others to be needed. For example,
4473 instantiating one function may cause another to be needed, and
4474 generating the initializer for an object may cause templates to be
4475 instantiated, etc., etc. */
4477 emit_support_tinfos ();
4481 tree t;
4482 tree decl;
4484 reconsider = false;
4486 /* If there are templates that we've put off instantiating, do
4487 them now. */
4488 instantiate_pending_templates (retries);
4489 ggc_collect ();
4491 /* Write out virtual tables as required. Note that writing out
4492 the virtual table for a template class may cause the
4493 instantiation of members of that class. If we write out
4494 vtables then we remove the class from our list so we don't
4495 have to look at it again. */
4497 while (keyed_classes != NULL_TREE
4498 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4500 reconsider = true;
4501 keyed_classes = TREE_CHAIN (keyed_classes);
4504 t = keyed_classes;
4505 if (t != NULL_TREE)
4507 tree next = TREE_CHAIN (t);
4509 while (next)
4511 if (maybe_emit_vtables (TREE_VALUE (next)))
4513 reconsider = true;
4514 TREE_CHAIN (t) = TREE_CHAIN (next);
4516 else
4517 t = next;
4519 next = TREE_CHAIN (t);
4523 /* Write out needed type info variables. We have to be careful
4524 looping through unemitted decls, because emit_tinfo_decl may
4525 cause other variables to be needed. New elements will be
4526 appended, and we remove from the vector those that actually
4527 get emitted. */
4528 for (i = unemitted_tinfo_decls->length ();
4529 unemitted_tinfo_decls->iterate (--i, &t);)
4530 if (emit_tinfo_decl (t))
4532 reconsider = true;
4533 unemitted_tinfo_decls->unordered_remove (i);
4536 /* The list of objects with static storage duration is built up
4537 in reverse order. We clear STATIC_AGGREGATES so that any new
4538 aggregates added during the initialization of these will be
4539 initialized in the correct order when we next come around the
4540 loop. */
4541 vars = prune_vars_needing_no_initialization (&static_aggregates);
4543 if (vars)
4545 /* We need to start a new initialization function each time
4546 through the loop. That's because we need to know which
4547 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4548 isn't computed until a function is finished, and written
4549 out. That's a deficiency in the back end. When this is
4550 fixed, these initialization functions could all become
4551 inline, with resulting performance improvements. */
4552 tree ssdf_body;
4554 /* Set the line and file, so that it is obviously not from
4555 the source file. */
4556 input_location = locus;
4557 ssdf_body = start_static_storage_duration_function (ssdf_count);
4559 /* Make sure the back end knows about all the variables. */
4560 write_out_vars (vars);
4562 /* First generate code to do all the initializations. */
4563 if (vars)
4564 do_static_initialization_or_destruction (vars, /*initp=*/true);
4566 /* Then, generate code to do all the destructions. Do these
4567 in reverse order so that the most recently constructed
4568 variable is the first destroyed. If we're using
4569 __cxa_atexit, then we don't need to do this; functions
4570 were registered at initialization time to destroy the
4571 local statics. */
4572 if (!flag_use_cxa_atexit && vars)
4574 vars = nreverse (vars);
4575 do_static_initialization_or_destruction (vars, /*initp=*/false);
4577 else
4578 vars = NULL_TREE;
4580 /* Finish up the static storage duration function for this
4581 round. */
4582 input_location = locus;
4583 finish_static_storage_duration_function (ssdf_body);
4585 /* All those initializations and finalizations might cause
4586 us to need more inline functions, more template
4587 instantiations, etc. */
4588 reconsider = true;
4589 ssdf_count++;
4590 /* ??? was: locus.line++; */
4593 /* Now do the same for thread_local variables. */
4594 handle_tls_init ();
4596 /* Go through the set of inline functions whose bodies have not
4597 been emitted yet. If out-of-line copies of these functions
4598 are required, emit them. */
4599 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4601 /* Does it need synthesizing? */
4602 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4603 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4605 /* Even though we're already at the top-level, we push
4606 there again. That way, when we pop back a few lines
4607 hence, all of our state is restored. Otherwise,
4608 finish_function doesn't clean things up, and we end
4609 up with CURRENT_FUNCTION_DECL set. */
4610 push_to_top_level ();
4611 /* The decl's location will mark where it was first
4612 needed. Save that so synthesize method can indicate
4613 where it was needed from, in case of error */
4614 input_location = DECL_SOURCE_LOCATION (decl);
4615 synthesize_method (decl);
4616 pop_from_top_level ();
4617 reconsider = true;
4620 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4621 generate_tls_wrapper (decl);
4623 if (!DECL_SAVED_TREE (decl))
4624 continue;
4626 /* We lie to the back end, pretending that some functions
4627 are not defined when they really are. This keeps these
4628 functions from being put out unnecessarily. But, we must
4629 stop lying when the functions are referenced, or if they
4630 are not comdat since they need to be put out now. If
4631 DECL_INTERFACE_KNOWN, then we have already set
4632 DECL_EXTERNAL appropriately, so there's no need to check
4633 again, and we do not want to clear DECL_EXTERNAL if a
4634 previous call to import_export_decl set it.
4636 This is done in a separate for cycle, because if some
4637 deferred function is contained in another deferred
4638 function later in deferred_fns varray,
4639 rest_of_compilation would skip this function and we
4640 really cannot expand the same function twice. */
4641 import_export_decl (decl);
4642 if (DECL_NOT_REALLY_EXTERN (decl)
4643 && DECL_INITIAL (decl)
4644 && decl_needed_p (decl))
4646 struct cgraph_node *node, *next;
4648 node = cgraph_node::get (decl);
4649 if (node->cpp_implicit_alias)
4650 node = node->get_alias_target ();
4652 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4653 NULL, true);
4654 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4655 group, we need to mark all symbols in the same comdat group
4656 that way. */
4657 if (node->same_comdat_group)
4658 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4659 next != node;
4660 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4661 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4662 NULL, true);
4665 /* If we're going to need to write this function out, and
4666 there's already a body for it, create RTL for it now.
4667 (There might be no body if this is a method we haven't
4668 gotten around to synthesizing yet.) */
4669 if (!DECL_EXTERNAL (decl)
4670 && decl_needed_p (decl)
4671 && !TREE_ASM_WRITTEN (decl)
4672 && !cgraph_node::get (decl)->definition)
4674 /* We will output the function; no longer consider it in this
4675 loop. */
4676 DECL_DEFER_OUTPUT (decl) = 0;
4677 /* Generate RTL for this function now that we know we
4678 need it. */
4679 expand_or_defer_fn (decl);
4680 /* If we're compiling -fsyntax-only pretend that this
4681 function has been written out so that we don't try to
4682 expand it again. */
4683 if (flag_syntax_only)
4684 TREE_ASM_WRITTEN (decl) = 1;
4685 reconsider = true;
4689 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4690 reconsider = true;
4692 /* Static data members are just like namespace-scope globals. */
4693 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4695 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4696 /* Don't write it out if we haven't seen a definition. */
4697 || DECL_IN_AGGR_P (decl))
4698 continue;
4699 import_export_decl (decl);
4700 /* If this static data member is needed, provide it to the
4701 back end. */
4702 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4703 DECL_EXTERNAL (decl) = 0;
4705 if (vec_safe_length (pending_statics) != 0
4706 && wrapup_global_declarations (pending_statics->address (),
4707 pending_statics->length ()))
4708 reconsider = true;
4710 retries++;
4712 while (reconsider);
4714 generate_mangling_aliases ();
4716 /* All used inline functions must have a definition at this point. */
4717 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4719 if (/* Check online inline functions that were actually used. */
4720 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4721 /* If the definition actually was available here, then the
4722 fact that the function was not defined merely represents
4723 that for some reason (use of a template repository,
4724 #pragma interface, etc.) we decided not to emit the
4725 definition here. */
4726 && !DECL_INITIAL (decl)
4727 /* Don't complain if the template was defined. */
4728 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4729 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4730 (template_for_substitution (decl)))))
4732 warning (0, "inline function %q+D used but never defined", decl);
4733 /* Avoid a duplicate warning from check_global_declaration_1. */
4734 TREE_NO_WARNING (decl) = 1;
4738 /* So must decls that use a type with no linkage. */
4739 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4740 no_linkage_error (decl);
4742 maybe_warn_sized_delete ();
4744 /* Then, do the Objective-C stuff. This is where all the
4745 Objective-C module stuff gets generated (symtab,
4746 class/protocol/selector lists etc). This must be done after C++
4747 templates, destructors etc. so that selectors used in C++
4748 templates are properly allocated. */
4749 if (c_dialect_objc ())
4750 objc_write_global_declarations ();
4752 /* We give C linkage to static constructors and destructors. */
4753 push_lang_context (lang_name_c);
4755 /* Generate initialization and destruction functions for all
4756 priorities for which they are required. */
4757 if (priority_info_map)
4758 splay_tree_foreach (priority_info_map,
4759 generate_ctor_and_dtor_functions_for_priority,
4760 /*data=*/&locus);
4761 else if (c_dialect_objc () && objc_static_init_needed_p ())
4762 /* If this is obj-c++ and we need a static init, call
4763 generate_ctor_or_dtor_function. */
4764 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4765 DEFAULT_INIT_PRIORITY, &locus);
4767 /* We're done with the splay-tree now. */
4768 if (priority_info_map)
4769 splay_tree_delete (priority_info_map);
4771 /* Generate any missing aliases. */
4772 maybe_apply_pending_pragma_weaks ();
4774 /* We're done with static constructors, so we can go back to "C++"
4775 linkage now. */
4776 pop_lang_context ();
4778 /* Generate Java hidden aliases. */
4779 build_java_method_aliases ();
4781 timevar_stop (TV_PHASE_DEFERRED);
4782 timevar_start (TV_PHASE_OPT_GEN);
4784 if (flag_vtable_verify)
4786 vtv_recover_class_info ();
4787 vtv_compute_class_hierarchy_transitive_closure ();
4788 vtv_build_vtable_verify_fndecl ();
4791 symtab->finalize_compilation_unit ();
4793 if (flag_vtable_verify)
4795 /* Generate the special constructor initialization function that
4796 calls __VLTRegisterPairs, and give it a very high
4797 initialization priority. This must be done after
4798 finalize_compilation_unit so that we have accurate
4799 information about which vtable will actually be emitted. */
4800 vtv_generate_init_routine ();
4803 timevar_stop (TV_PHASE_OPT_GEN);
4804 timevar_start (TV_PHASE_CHECK_DBGINFO);
4806 /* Now, issue warnings about static, but not defined, functions,
4807 etc., and emit debugging information. */
4808 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4809 if (vec_safe_length (pending_statics) != 0)
4811 check_global_declarations (pending_statics->address (),
4812 pending_statics->length ());
4813 emit_debug_global_declarations (pending_statics->address (),
4814 pending_statics->length ());
4817 perform_deferred_noexcept_checks ();
4819 finish_repo ();
4821 /* The entire file is now complete. If requested, dump everything
4822 to a file. */
4823 dump_tu ();
4825 if (flag_detailed_statistics)
4827 dump_tree_statistics ();
4828 dump_time_statistics ();
4830 input_location = locus;
4832 #ifdef ENABLE_CHECKING
4833 validate_conversion_obstack ();
4834 #endif /* ENABLE_CHECKING */
4836 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4839 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4840 function to call in parse-tree form; it has not yet been
4841 semantically analyzed. ARGS are the arguments to the function.
4842 They have already been semantically analyzed. This may change
4843 ARGS. */
4845 tree
4846 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4847 tsubst_flags_t complain)
4849 tree orig_fn;
4850 vec<tree, va_gc> *orig_args = NULL;
4851 tree expr;
4852 tree object;
4854 orig_fn = fn;
4855 object = TREE_OPERAND (fn, 0);
4857 if (processing_template_decl)
4859 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4860 || TREE_CODE (fn) == MEMBER_REF);
4861 if (type_dependent_expression_p (fn)
4862 || any_type_dependent_arguments_p (*args))
4863 return build_nt_call_vec (fn, *args);
4865 orig_args = make_tree_vector_copy (*args);
4867 /* Transform the arguments and add the implicit "this"
4868 parameter. That must be done before the FN is transformed
4869 because we depend on the form of FN. */
4870 make_args_non_dependent (*args);
4871 object = build_non_dependent_expr (object);
4872 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4874 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4875 object = cp_build_addr_expr (object, complain);
4876 vec_safe_insert (*args, 0, object);
4878 /* Now that the arguments are done, transform FN. */
4879 fn = build_non_dependent_expr (fn);
4882 /* A qualified name corresponding to a bound pointer-to-member is
4883 represented as an OFFSET_REF:
4885 struct B { void g(); };
4886 void (B::*p)();
4887 void B::g() { (this->*p)(); } */
4888 if (TREE_CODE (fn) == OFFSET_REF)
4890 tree object_addr = cp_build_addr_expr (object, complain);
4891 fn = TREE_OPERAND (fn, 1);
4892 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4893 complain);
4894 vec_safe_insert (*args, 0, object_addr);
4897 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4898 expr = build_op_call (fn, args, complain);
4899 else
4900 expr = cp_build_function_call_vec (fn, args, complain);
4901 if (processing_template_decl && expr != error_mark_node)
4902 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4904 if (orig_args != NULL)
4905 release_tree_vector (orig_args);
4907 return expr;
4911 void
4912 check_default_args (tree x)
4914 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4915 bool saw_def = false;
4916 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4917 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4919 if (TREE_PURPOSE (arg))
4920 saw_def = true;
4921 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4923 error ("default argument missing for parameter %P of %q+#D", i, x);
4924 TREE_PURPOSE (arg) = error_mark_node;
4929 /* Return true if function DECL can be inlined. This is used to force
4930 instantiation of methods that might be interesting for inlining. */
4931 bool
4932 possibly_inlined_p (tree decl)
4934 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4935 if (DECL_UNINLINABLE (decl))
4936 return false;
4937 if (!optimize || pragma_java_exceptions)
4938 return DECL_DECLARED_INLINE_P (decl);
4939 /* When optimizing, we might inline everything when flatten
4940 attribute or heuristics inlining for size or autoinlining
4941 is used. */
4942 return true;
4945 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4946 If DECL is a specialization or implicitly declared class member,
4947 generate the actual definition. Return false if something goes
4948 wrong, true otherwise. */
4950 bool
4951 mark_used (tree decl, tsubst_flags_t complain)
4953 /* If DECL is a BASELINK for a single function, then treat it just
4954 like the DECL for the function. Otherwise, if the BASELINK is
4955 for an overloaded function, we don't know which function was
4956 actually used until after overload resolution. */
4957 if (BASELINK_P (decl))
4959 decl = BASELINK_FUNCTIONS (decl);
4960 if (really_overloaded_fn (decl))
4961 return true;
4962 decl = OVL_CURRENT (decl);
4965 /* Set TREE_USED for the benefit of -Wunused. */
4966 TREE_USED (decl) = 1;
4967 if (DECL_CLONED_FUNCTION_P (decl))
4968 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4970 /* Mark enumeration types as used. */
4971 if (TREE_CODE (decl) == CONST_DECL)
4972 used_types_insert (DECL_CONTEXT (decl));
4974 if (TREE_CODE (decl) == FUNCTION_DECL)
4975 maybe_instantiate_noexcept (decl);
4977 if (TREE_CODE (decl) == FUNCTION_DECL
4978 && DECL_DELETED_FN (decl))
4980 if (DECL_ARTIFICIAL (decl))
4982 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4983 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4985 /* We mark a lambda conversion op as deleted if we can't
4986 generate it properly; see maybe_add_lambda_conv_op. */
4987 sorry ("converting lambda which uses %<...%> to "
4988 "function pointer");
4989 return false;
4992 if (complain & tf_error)
4994 error ("use of deleted function %qD", decl);
4995 if (!maybe_explain_implicit_delete (decl))
4996 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4998 return false;
5001 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5002 && deprecated_state != DEPRECATED_SUPPRESS)
5003 warn_deprecated_use (decl, NULL_TREE);
5005 /* We can only check DECL_ODR_USED on variables or functions with
5006 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5007 might need special handling for. */
5008 if (!VAR_OR_FUNCTION_DECL_P (decl)
5009 || DECL_LANG_SPECIFIC (decl) == NULL
5010 || DECL_THUNK_P (decl))
5012 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
5014 if (complain & tf_error)
5015 error ("use of %qD before deduction of %<auto%>", decl);
5016 return false;
5018 return true;
5021 /* We only want to do this processing once. We don't need to keep trying
5022 to instantiate inline templates, because unit-at-a-time will make sure
5023 we get them compiled before functions that want to inline them. */
5024 if (DECL_ODR_USED (decl))
5025 return true;
5027 /* If within finish_function, defer the rest until that function
5028 finishes, otherwise it might recurse. */
5029 if (defer_mark_used_calls)
5031 vec_safe_push (deferred_mark_used_calls, decl);
5032 return true;
5035 /* Normally, we can wait until instantiation-time to synthesize DECL.
5036 However, if DECL is a static data member initialized with a constant
5037 or a constexpr function, we need it right now because a reference to
5038 such a data member or a call to such function is not value-dependent.
5039 For a function that uses auto in the return type, we need to instantiate
5040 it to find out its type. For OpenMP user defined reductions, we need
5041 them instantiated for reduction clauses which inline them by hand
5042 directly. */
5043 if (DECL_LANG_SPECIFIC (decl)
5044 && DECL_TEMPLATE_INFO (decl)
5045 && (decl_maybe_constant_var_p (decl)
5046 || (TREE_CODE (decl) == FUNCTION_DECL
5047 && (DECL_DECLARED_CONSTEXPR_P (decl)
5048 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
5049 || undeduced_auto_decl (decl))
5050 && !uses_template_parms (DECL_TI_ARGS (decl)))
5052 /* Instantiating a function will result in garbage collection. We
5053 must treat this situation as if we were within the body of a
5054 function so as to avoid collecting live data only referenced from
5055 the stack (such as overload resolution candidates). */
5056 ++function_depth;
5057 instantiate_decl (decl, /*defer_ok=*/false,
5058 /*expl_inst_class_mem_p=*/false);
5059 --function_depth;
5062 if (processing_template_decl || in_template_function ())
5063 return true;
5065 /* Check this too in case we're within instantiate_non_dependent_expr. */
5066 if (DECL_TEMPLATE_INFO (decl)
5067 && uses_template_parms (DECL_TI_ARGS (decl)))
5068 return true;
5070 if (undeduced_auto_decl (decl))
5072 if (complain & tf_error)
5073 error ("use of %qD before deduction of %<auto%>", decl);
5074 return false;
5077 /* If we don't need a value, then we don't need to synthesize DECL. */
5078 if (cp_unevaluated_operand != 0)
5079 return true;
5081 DECL_ODR_USED (decl) = 1;
5082 if (DECL_CLONED_FUNCTION_P (decl))
5083 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5085 /* DR 757: A type without linkage shall not be used as the type of a
5086 variable or function with linkage, unless
5087 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5088 o the variable or function is not used (3.2 [basic.def.odr]) or is
5089 defined in the same translation unit. */
5090 if (cxx_dialect > cxx98
5091 && decl_linkage (decl) != lk_none
5092 && !DECL_EXTERN_C_P (decl)
5093 && !DECL_ARTIFICIAL (decl)
5094 && !decl_defined_p (decl)
5095 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5097 if (is_local_extern (decl))
5098 /* There's no way to define a local extern, and adding it to
5099 the vector interferes with GC, so give an error now. */
5100 no_linkage_error (decl);
5101 else
5102 vec_safe_push (no_linkage_decls, decl);
5105 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5106 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5107 /* Remember it, so we can check it was defined. */
5108 note_vague_linkage_fn (decl);
5110 /* Is it a synthesized method that needs to be synthesized? */
5111 if (TREE_CODE (decl) == FUNCTION_DECL
5112 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5113 && DECL_DEFAULTED_FN (decl)
5114 /* A function defaulted outside the class is synthesized either by
5115 cp_finish_decl or instantiate_decl. */
5116 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5117 && ! DECL_INITIAL (decl))
5119 /* Defer virtual destructors so that thunks get the right
5120 linkage. */
5121 if (DECL_VIRTUAL_P (decl) && !at_eof)
5123 note_vague_linkage_fn (decl);
5124 return true;
5127 /* Remember the current location for a function we will end up
5128 synthesizing. Then we can inform the user where it was
5129 required in the case of error. */
5130 DECL_SOURCE_LOCATION (decl) = input_location;
5132 /* Synthesizing an implicitly defined member function will result in
5133 garbage collection. We must treat this situation as if we were
5134 within the body of a function so as to avoid collecting live data
5135 on the stack (such as overload resolution candidates).
5137 We could just let cp_write_global_declarations handle synthesizing
5138 this function by adding it to deferred_fns, but doing
5139 it at the use site produces better error messages. */
5140 ++function_depth;
5141 synthesize_method (decl);
5142 --function_depth;
5143 /* If this is a synthesized method we don't need to
5144 do the instantiation test below. */
5146 else if (VAR_OR_FUNCTION_DECL_P (decl)
5147 && DECL_TEMPLATE_INFO (decl)
5148 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5149 || always_instantiate_p (decl)))
5150 /* If this is a function or variable that is an instance of some
5151 template, we now know that we will need to actually do the
5152 instantiation. We check that DECL is not an explicit
5153 instantiation because that is not checked in instantiate_decl.
5155 We put off instantiating functions in order to improve compile
5156 times. Maintaining a stack of active functions is expensive,
5157 and the inliner knows to instantiate any functions it might
5158 need. Therefore, we always try to defer instantiation. */
5160 ++function_depth;
5161 instantiate_decl (decl, /*defer_ok=*/true,
5162 /*expl_inst_class_mem_p=*/false);
5163 --function_depth;
5166 return true;
5169 bool
5170 mark_used (tree decl)
5172 return mark_used (decl, tf_warning_or_error);
5175 tree
5176 vtv_start_verification_constructor_init_function (void)
5178 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5181 tree
5182 vtv_finish_verification_constructor_init_function (tree function_body)
5184 tree fn;
5186 finish_compound_stmt (function_body);
5187 fn = finish_function (0);
5188 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5189 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5191 return fn;
5194 #include "gt-cp-decl2.h"