PR c++/65209
[official-gcc.git] / gcc / cp / decl2.c
bloba4a5ebf58ea12426580ee37970e7b0f605bb0dfc
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "stringpool.h"
44 #include "varasm.h"
45 #include "attribs.h"
46 #include "stor-layout.h"
47 #include "calls.h"
48 #include "flags.h"
49 #include "cp-tree.h"
50 #include "decl.h"
51 #include "toplev.h"
52 #include "timevar.h"
53 #include "cpplib.h"
54 #include "target.h"
55 #include "c-family/c-common.h"
56 #include "c-family/c-objc.h"
57 #include "hash-map.h"
58 #include "is-a.h"
59 #include "plugin-api.h"
60 #include "hard-reg-set.h"
61 #include "input.h"
62 #include "function.h"
63 #include "ipa-ref.h"
64 #include "cgraph.h"
65 #include "tree-inline.h"
66 #include "c-family/c-pragma.h"
67 #include "dumpfile.h"
68 #include "intl.h"
69 #include "splay-tree.h"
70 #include "langhooks.h"
71 #include "c-family/c-ada-spec.h"
72 #include "asan.h"
74 extern cpp_reader *parse_in;
76 /* This structure contains information about the initializations
77 and/or destructions required for a particular priority level. */
78 typedef struct priority_info_s {
79 /* Nonzero if there have been any initializations at this priority
80 throughout the translation unit. */
81 int initializations_p;
82 /* Nonzero if there have been any destructions at this priority
83 throughout the translation unit. */
84 int destructions_p;
85 } *priority_info;
87 static void mark_vtable_entries (tree);
88 static bool maybe_emit_vtables (tree);
89 static bool acceptable_java_type (tree);
90 static tree start_objects (int, int);
91 static void finish_objects (int, int, tree);
92 static tree start_static_storage_duration_function (unsigned);
93 static void finish_static_storage_duration_function (tree);
94 static priority_info get_priority_info (int);
95 static void do_static_initialization_or_destruction (tree, bool);
96 static void one_static_initialization_or_destruction (tree, tree, bool);
97 static void generate_ctor_or_dtor_function (bool, int, location_t *);
98 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
99 void *);
100 static tree prune_vars_needing_no_initialization (tree *);
101 static void write_out_vars (tree);
102 static void import_export_class (tree);
103 static tree get_guard_bits (tree);
104 static void determine_visibility_from_class (tree, tree);
105 static bool determine_hidden_inline (tree);
106 static bool decl_defined_p (tree);
108 /* A list of static class variables. This is needed, because a
109 static class variable can be declared inside the class without
110 an initializer, and then initialized, statically, outside the class. */
111 static GTY(()) vec<tree, va_gc> *pending_statics;
113 /* A list of functions which were declared inline, but which we
114 may need to emit outline anyway. */
115 static GTY(()) vec<tree, va_gc> *deferred_fns;
117 /* A list of decls that use types with no linkage, which we need to make
118 sure are defined. */
119 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
121 /* Nonzero if we're done parsing and into end-of-file activities. */
123 int at_eof;
126 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
127 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
128 that apply to the function). */
130 tree
131 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
132 cp_ref_qualifier rqual)
134 tree raises;
135 tree attrs;
136 int type_quals;
137 bool late_return_type_p;
139 if (fntype == error_mark_node || ctype == error_mark_node)
140 return error_mark_node;
142 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
143 || TREE_CODE (fntype) == METHOD_TYPE);
145 type_quals = quals & ~TYPE_QUAL_RESTRICT;
146 ctype = cp_build_qualified_type (ctype, type_quals);
147 raises = TYPE_RAISES_EXCEPTIONS (fntype);
148 attrs = TYPE_ATTRIBUTES (fntype);
149 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
150 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
151 (TREE_CODE (fntype) == METHOD_TYPE
152 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
153 : TYPE_ARG_TYPES (fntype)));
154 if (attrs)
155 fntype = cp_build_type_attribute_variant (fntype, attrs);
156 if (rqual)
157 fntype = build_ref_qualified_type (fntype, rqual);
158 if (raises)
159 fntype = build_exception_variant (fntype, raises);
160 if (late_return_type_p)
161 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
163 return fntype;
166 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
167 return type changed to NEW_RET. */
169 tree
170 change_return_type (tree new_ret, tree fntype)
172 tree newtype;
173 tree args = TYPE_ARG_TYPES (fntype);
174 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
175 tree attrs = TYPE_ATTRIBUTES (fntype);
176 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
178 if (new_ret == error_mark_node)
179 return fntype;
181 if (same_type_p (new_ret, TREE_TYPE (fntype)))
182 return fntype;
184 if (TREE_CODE (fntype) == FUNCTION_TYPE)
186 newtype = build_function_type (new_ret, args);
187 newtype = apply_memfn_quals (newtype,
188 type_memfn_quals (fntype),
189 type_memfn_rqual (fntype));
191 else
192 newtype = build_method_type_directly
193 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
194 if (raises)
195 newtype = build_exception_variant (newtype, raises);
196 if (attrs)
197 newtype = cp_build_type_attribute_variant (newtype, attrs);
198 if (late_return_type_p)
199 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
201 return newtype;
204 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
205 appropriately. */
207 tree
208 cp_build_parm_decl (tree name, tree type)
210 tree parm = build_decl (input_location,
211 PARM_DECL, name, type);
212 /* DECL_ARG_TYPE is only used by the back end and the back end never
213 sees templates. */
214 if (!processing_template_decl)
215 DECL_ARG_TYPE (parm) = type_passed_as (type);
217 return parm;
220 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
221 indicated NAME. */
223 tree
224 build_artificial_parm (tree name, tree type)
226 tree parm = cp_build_parm_decl (name, type);
227 DECL_ARTIFICIAL (parm) = 1;
228 /* All our artificial parms are implicitly `const'; they cannot be
229 assigned to. */
230 TREE_READONLY (parm) = 1;
231 return parm;
234 /* Constructors for types with virtual baseclasses need an "in-charge" flag
235 saying whether this constructor is responsible for initialization of
236 virtual baseclasses or not. All destructors also need this "in-charge"
237 flag, which additionally determines whether or not the destructor should
238 free the memory for the object.
240 This function adds the "in-charge" flag to member function FN if
241 appropriate. It is called from grokclassfn and tsubst.
242 FN must be either a constructor or destructor.
244 The in-charge flag follows the 'this' parameter, and is followed by the
245 VTT parm (if any), then the user-written parms. */
247 void
248 maybe_retrofit_in_chrg (tree fn)
250 tree basetype, arg_types, parms, parm, fntype;
252 /* If we've already add the in-charge parameter don't do it again. */
253 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
254 return;
256 /* When processing templates we can't know, in general, whether or
257 not we're going to have virtual baseclasses. */
258 if (processing_template_decl)
259 return;
261 /* We don't need an in-charge parameter for constructors that don't
262 have virtual bases. */
263 if (DECL_CONSTRUCTOR_P (fn)
264 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
265 return;
267 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
268 basetype = TREE_TYPE (TREE_VALUE (arg_types));
269 arg_types = TREE_CHAIN (arg_types);
271 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
273 /* If this is a subobject constructor or destructor, our caller will
274 pass us a pointer to our VTT. */
275 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
277 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
279 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
280 DECL_CHAIN (parm) = parms;
281 parms = parm;
283 /* ...and then to TYPE_ARG_TYPES. */
284 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
286 DECL_HAS_VTT_PARM_P (fn) = 1;
289 /* Then add the in-charge parm (before the VTT parm). */
290 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
291 DECL_CHAIN (parm) = parms;
292 parms = parm;
293 arg_types = hash_tree_chain (integer_type_node, arg_types);
295 /* Insert our new parameter(s) into the list. */
296 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
298 /* And rebuild the function type. */
299 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
300 arg_types);
301 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
302 fntype = build_exception_variant (fntype,
303 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
304 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
305 fntype = (cp_build_type_attribute_variant
306 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
307 TREE_TYPE (fn) = fntype;
309 /* Now we've got the in-charge parameter. */
310 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
313 /* Classes overload their constituent function names automatically.
314 When a function name is declared in a record structure,
315 its name is changed to it overloaded name. Since names for
316 constructors and destructors can conflict, we place a leading
317 '$' for destructors.
319 CNAME is the name of the class we are grokking for.
321 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
323 FLAGS contains bits saying what's special about today's
324 arguments. DTOR_FLAG == DESTRUCTOR.
326 If FUNCTION is a destructor, then we must add the `auto-delete' field
327 as a second parameter. There is some hair associated with the fact
328 that we must "declare" this variable in the manner consistent with the
329 way the rest of the arguments were declared.
331 QUALS are the qualifiers for the this pointer. */
333 void
334 grokclassfn (tree ctype, tree function, enum overload_flags flags)
336 tree fn_name = DECL_NAME (function);
338 /* Even within an `extern "C"' block, members get C++ linkage. See
339 [dcl.link] for details. */
340 SET_DECL_LANGUAGE (function, lang_cplusplus);
342 if (fn_name == NULL_TREE)
344 error ("name missing for member function");
345 fn_name = get_identifier ("<anonymous>");
346 DECL_NAME (function) = fn_name;
349 DECL_CONTEXT (function) = ctype;
351 if (flags == DTOR_FLAG)
352 DECL_DESTRUCTOR_P (function) = 1;
354 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
355 maybe_retrofit_in_chrg (function);
358 /* Create an ARRAY_REF, checking for the user doing things backwards
359 along the way. DECLTYPE_P is for N3276, as in the parser. */
361 tree
362 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
363 bool decltype_p)
365 tree type;
366 tree expr;
367 tree orig_array_expr = array_expr;
368 tree orig_index_exp = index_exp;
370 if (error_operand_p (array_expr) || error_operand_p (index_exp))
371 return error_mark_node;
373 if (processing_template_decl)
375 if (type_dependent_expression_p (array_expr)
376 || type_dependent_expression_p (index_exp))
377 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
378 NULL_TREE, NULL_TREE);
379 array_expr = build_non_dependent_expr (array_expr);
380 index_exp = build_non_dependent_expr (index_exp);
383 type = TREE_TYPE (array_expr);
384 gcc_assert (type);
385 type = non_reference (type);
387 /* If they have an `operator[]', use that. */
388 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
390 tsubst_flags_t complain = tf_warning_or_error;
391 if (decltype_p)
392 complain |= tf_decltype;
393 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
394 index_exp, NULL_TREE, /*overload=*/NULL, complain);
396 else
398 tree p1, p2, i1, i2;
400 /* Otherwise, create an ARRAY_REF for a pointer or array type.
401 It is a little-known fact that, if `a' is an array and `i' is
402 an int, you can write `i[a]', which means the same thing as
403 `a[i]'. */
404 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
405 p1 = array_expr;
406 else
407 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
409 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
410 p2 = index_exp;
411 else
412 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
414 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
415 false);
416 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
417 false);
419 if ((p1 && i2) && (i1 && p2))
420 error ("ambiguous conversion for array subscript");
422 if (p1 && i2)
423 array_expr = p1, index_exp = i2;
424 else if (i1 && p2)
425 array_expr = p2, index_exp = i1;
426 else
428 error ("invalid types %<%T[%T]%> for array subscript",
429 type, TREE_TYPE (index_exp));
430 return error_mark_node;
433 if (array_expr == error_mark_node || index_exp == error_mark_node)
434 error ("ambiguous conversion for array subscript");
436 expr = build_array_ref (input_location, array_expr, index_exp);
438 if (processing_template_decl && expr != error_mark_node)
439 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
440 NULL_TREE, NULL_TREE);
441 return expr;
444 /* Given the cast expression EXP, checking out its validity. Either return
445 an error_mark_node if there was an unavoidable error, return a cast to
446 void for trying to delete a pointer w/ the value 0, or return the
447 call to delete. If DOING_VEC is true, we handle things differently
448 for doing an array delete.
449 Implements ARM $5.3.4. This is called from the parser. */
451 tree
452 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
453 tsubst_flags_t complain)
455 tree t, type;
457 if (exp == error_mark_node)
458 return exp;
460 if (processing_template_decl)
462 t = build_min (DELETE_EXPR, void_type_node, exp, size);
463 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
464 DELETE_EXPR_USE_VEC (t) = doing_vec;
465 TREE_SIDE_EFFECTS (t) = 1;
466 return t;
469 /* An array can't have been allocated by new, so complain. */
470 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
471 warning (0, "deleting array %q#E", exp);
473 t = build_expr_type_conversion (WANT_POINTER, exp, true);
475 if (t == NULL_TREE || t == error_mark_node)
477 error ("type %q#T argument given to %<delete%>, expected pointer",
478 TREE_TYPE (exp));
479 return error_mark_node;
482 type = TREE_TYPE (t);
484 /* As of Valley Forge, you can delete a pointer to const. */
486 /* You can't delete functions. */
487 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
489 error ("cannot delete a function. Only pointer-to-objects are "
490 "valid arguments to %<delete%>");
491 return error_mark_node;
494 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
495 if (VOID_TYPE_P (TREE_TYPE (type)))
497 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
498 doing_vec = 0;
501 /* Deleting a pointer with the value zero is valid and has no effect. */
502 if (integer_zerop (t))
503 return build1 (NOP_EXPR, void_type_node, t);
505 if (doing_vec)
506 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
507 sfk_deleting_destructor,
508 use_global_delete, complain);
509 else
510 return build_delete (type, t, sfk_deleting_destructor,
511 LOOKUP_NORMAL, use_global_delete,
512 complain);
515 /* Report an error if the indicated template declaration is not the
516 sort of thing that should be a member template. */
518 void
519 check_member_template (tree tmpl)
521 tree decl;
523 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
524 decl = DECL_TEMPLATE_RESULT (tmpl);
526 if (TREE_CODE (decl) == FUNCTION_DECL
527 || DECL_ALIAS_TEMPLATE_P (tmpl)
528 || (TREE_CODE (decl) == TYPE_DECL
529 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
531 /* The parser rejects template declarations in local classes
532 (with the exception of generic lambdas). */
533 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
534 /* The parser rejects any use of virtual in a function template. */
535 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
536 && DECL_VIRTUAL_P (decl)));
538 /* The debug-information generating code doesn't know what to do
539 with member templates. */
540 DECL_IGNORED_P (tmpl) = 1;
542 else if (variable_template_p (tmpl))
543 /* OK */;
544 else
545 error ("template declaration of %q#D", decl);
548 /* Return true iff TYPE is a valid Java parameter or return type. */
550 static bool
551 acceptable_java_type (tree type)
553 if (type == error_mark_node)
554 return false;
556 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
557 return true;
558 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
560 type = TREE_TYPE (type);
561 if (TREE_CODE (type) == RECORD_TYPE)
563 tree args; int i;
564 if (! TYPE_FOR_JAVA (type))
565 return false;
566 if (! CLASSTYPE_TEMPLATE_INFO (type))
567 return true;
568 args = CLASSTYPE_TI_ARGS (type);
569 i = TREE_VEC_LENGTH (args);
570 while (--i >= 0)
572 type = TREE_VEC_ELT (args, i);
573 if (TYPE_PTR_P (type))
574 type = TREE_TYPE (type);
575 if (! TYPE_FOR_JAVA (type))
576 return false;
578 return true;
581 return false;
584 /* For a METHOD in a Java class CTYPE, return true if
585 the parameter and return types are valid Java types.
586 Otherwise, print appropriate error messages, and return false. */
588 bool
589 check_java_method (tree method)
591 bool jerr = false;
592 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
593 tree ret_type = TREE_TYPE (TREE_TYPE (method));
595 if (!acceptable_java_type (ret_type))
597 error ("Java method %qD has non-Java return type %qT",
598 method, ret_type);
599 jerr = true;
602 arg_types = TREE_CHAIN (arg_types);
603 if (DECL_HAS_IN_CHARGE_PARM_P (method))
604 arg_types = TREE_CHAIN (arg_types);
605 if (DECL_HAS_VTT_PARM_P (method))
606 arg_types = TREE_CHAIN (arg_types);
608 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
610 tree type = TREE_VALUE (arg_types);
611 if (!acceptable_java_type (type))
613 if (type != error_mark_node)
614 error ("Java method %qD has non-Java parameter type %qT",
615 method, type);
616 jerr = true;
619 return !jerr;
622 /* Sanity check: report error if this function FUNCTION is not
623 really a member of the class (CTYPE) it is supposed to belong to.
624 TEMPLATE_PARMS is used to specify the template parameters of a member
625 template passed as FUNCTION_DECL. If the member template is passed as a
626 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
627 from the declaration. If the function is not a function template, it
628 must be NULL.
629 It returns the original declaration for the function, NULL_TREE if
630 no declaration was found, error_mark_node if an error was emitted. */
632 tree
633 check_classfn (tree ctype, tree function, tree template_parms)
635 int ix;
636 bool is_template;
637 tree pushed_scope;
639 if (DECL_USE_TEMPLATE (function)
640 && !(TREE_CODE (function) == TEMPLATE_DECL
641 && DECL_TEMPLATE_SPECIALIZATION (function))
642 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
643 /* Since this is a specialization of a member template,
644 we're not going to find the declaration in the class.
645 For example, in:
647 struct S { template <typename T> void f(T); };
648 template <> void S::f(int);
650 we're not going to find `S::f(int)', but there's no
651 reason we should, either. We let our callers know we didn't
652 find the method, but we don't complain. */
653 return NULL_TREE;
655 /* Basic sanity check: for a template function, the template parameters
656 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
657 if (TREE_CODE (function) == TEMPLATE_DECL)
659 if (template_parms
660 && !comp_template_parms (template_parms,
661 DECL_TEMPLATE_PARMS (function)))
663 error ("template parameter lists provided don%'t match the "
664 "template parameters of %qD", function);
665 return error_mark_node;
667 template_parms = DECL_TEMPLATE_PARMS (function);
670 /* OK, is this a definition of a member template? */
671 is_template = (template_parms != NULL_TREE);
673 /* [temp.mem]
675 A destructor shall not be a member template. */
676 if (DECL_DESTRUCTOR_P (function) && is_template)
678 error ("destructor %qD declared as member template", function);
679 return error_mark_node;
682 /* We must enter the scope here, because conversion operators are
683 named by target type, and type equivalence relies on typenames
684 resolving within the scope of CTYPE. */
685 pushed_scope = push_scope (ctype);
686 ix = class_method_index_for_fn (complete_type (ctype), function);
687 if (ix >= 0)
689 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
690 tree fndecls, fndecl = 0;
691 bool is_conv_op;
692 const char *format = NULL;
694 for (fndecls = (*methods)[ix];
695 fndecls; fndecls = OVL_NEXT (fndecls))
697 tree p1, p2;
699 fndecl = OVL_CURRENT (fndecls);
700 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
701 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
703 /* We cannot simply call decls_match because this doesn't
704 work for static member functions that are pretending to
705 be methods, and because the name may have been changed by
706 asm("new_name"). */
708 /* Get rid of the this parameter on functions that become
709 static. */
710 if (DECL_STATIC_FUNCTION_P (fndecl)
711 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
712 p1 = TREE_CHAIN (p1);
714 /* A member template definition only matches a member template
715 declaration. */
716 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
717 continue;
719 /* ref-qualifier or absence of same must match. */
720 if (type_memfn_rqual (TREE_TYPE (function))
721 != type_memfn_rqual (TREE_TYPE (fndecl)))
722 continue;
724 /* While finding a match, same types and params are not enough
725 if the function is versioned. Also check version ("target")
726 attributes. */
727 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
728 TREE_TYPE (TREE_TYPE (fndecl)))
729 && compparms (p1, p2)
730 && !targetm.target_option.function_versions (function, fndecl)
731 && (!is_template
732 || comp_template_parms (template_parms,
733 DECL_TEMPLATE_PARMS (fndecl)))
734 && (DECL_TEMPLATE_SPECIALIZATION (function)
735 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
736 && (!DECL_TEMPLATE_SPECIALIZATION (function)
737 || (DECL_TI_TEMPLATE (function)
738 == DECL_TI_TEMPLATE (fndecl))))
739 break;
741 if (fndecls)
743 if (pushed_scope)
744 pop_scope (pushed_scope);
745 return OVL_CURRENT (fndecls);
748 error_at (DECL_SOURCE_LOCATION (function),
749 "prototype for %q#D does not match any in class %qT",
750 function, ctype);
751 is_conv_op = DECL_CONV_FN_P (fndecl);
753 if (is_conv_op)
754 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
755 fndecls = (*methods)[ix];
756 while (fndecls)
758 fndecl = OVL_CURRENT (fndecls);
759 fndecls = OVL_NEXT (fndecls);
761 if (!fndecls && is_conv_op)
763 if (methods->length () > (size_t) ++ix)
765 fndecls = (*methods)[ix];
766 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
768 fndecls = NULL_TREE;
769 is_conv_op = false;
772 else
773 is_conv_op = false;
775 if (format)
776 format = " %+#D";
777 else if (fndecls)
778 format = N_("candidates are: %+#D");
779 else
780 format = N_("candidate is: %+#D");
781 error (format, fndecl);
784 else if (!COMPLETE_TYPE_P (ctype))
785 cxx_incomplete_type_error (function, ctype);
786 else
787 error ("no %q#D member function declared in class %qT",
788 function, ctype);
790 if (pushed_scope)
791 pop_scope (pushed_scope);
792 return error_mark_node;
795 /* DECL is a function with vague linkage. Remember it so that at the
796 end of the translation unit we can decide whether or not to emit
797 it. */
799 void
800 note_vague_linkage_fn (tree decl)
802 DECL_DEFER_OUTPUT (decl) = 1;
803 vec_safe_push (deferred_fns, decl);
806 /* As above, but for variable template instantiations. */
808 void
809 note_variable_template_instantiation (tree decl)
811 vec_safe_push (pending_statics, decl);
814 /* We have just processed the DECL, which is a static data member.
815 The other parameters are as for cp_finish_decl. */
817 void
818 finish_static_data_member_decl (tree decl,
819 tree init, bool init_const_expr_p,
820 tree asmspec_tree,
821 int flags)
823 DECL_CONTEXT (decl) = current_class_type;
825 /* We cannot call pushdecl here, because that would fill in the
826 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
827 the right thing, namely, to put this decl out straight away. */
829 if (! processing_template_decl)
830 vec_safe_push (pending_statics, decl);
832 if (LOCAL_CLASS_P (current_class_type)
833 /* We already complained about the template definition. */
834 && !DECL_TEMPLATE_INSTANTIATION (decl))
835 permerror (input_location, "local class %q#T shall not have static data member %q#D",
836 current_class_type, decl);
837 else
838 for (tree t = current_class_type; TYPE_P (t);
839 t = CP_TYPE_CONTEXT (t))
840 if (TYPE_ANONYMOUS_P (t))
842 if (permerror (DECL_SOURCE_LOCATION (decl),
843 "static data member %qD in unnamed class", decl))
844 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
845 "unnamed class defined here");
846 break;
849 DECL_IN_AGGR_P (decl) = 1;
851 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
852 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
853 SET_VAR_HAD_UNKNOWN_BOUND (decl);
855 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
858 /* DECLARATOR and DECLSPECS correspond to a class member. The other
859 parameters are as for cp_finish_decl. Return the DECL for the
860 class member declared. */
862 tree
863 grokfield (const cp_declarator *declarator,
864 cp_decl_specifier_seq *declspecs,
865 tree init, bool init_const_expr_p,
866 tree asmspec_tree,
867 tree attrlist)
869 tree value;
870 const char *asmspec = 0;
871 int flags;
872 tree name;
874 if (init
875 && TREE_CODE (init) == TREE_LIST
876 && TREE_VALUE (init) == error_mark_node
877 && TREE_CHAIN (init) == NULL_TREE)
878 init = NULL_TREE;
880 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
881 if (! value || value == error_mark_node)
882 /* friend or constructor went bad. */
883 return error_mark_node;
884 if (TREE_TYPE (value) == error_mark_node)
885 return value;
887 if (TREE_CODE (value) == TYPE_DECL && init)
889 error ("typedef %qD is initialized (use decltype instead)", value);
890 init = NULL_TREE;
893 /* Pass friendly classes back. */
894 if (value == void_type_node)
895 return value;
898 name = DECL_NAME (value);
900 if (name != NULL_TREE)
902 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
904 error ("explicit template argument list not allowed");
905 return error_mark_node;
908 if (IDENTIFIER_POINTER (name)[0] == '_'
909 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
910 error ("member %qD conflicts with virtual function table field name",
911 value);
914 /* Stash away type declarations. */
915 if (TREE_CODE (value) == TYPE_DECL)
917 DECL_NONLOCAL (value) = 1;
918 DECL_CONTEXT (value) = current_class_type;
920 if (attrlist)
922 int attrflags = 0;
924 /* If this is a typedef that names the class for linkage purposes
925 (7.1.3p8), apply any attributes directly to the type. */
926 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
927 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
928 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
930 cplus_decl_attributes (&value, attrlist, attrflags);
933 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
934 && TREE_TYPE (value) != error_mark_node
935 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
936 set_underlying_type (value);
938 /* It's important that push_template_decl below follows
939 set_underlying_type above so that the created template
940 carries the properly set type of VALUE. */
941 if (processing_template_decl)
942 value = push_template_decl (value);
944 record_locally_defined_typedef (value);
945 return value;
948 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
950 if (!friendp && DECL_IN_AGGR_P (value))
952 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
953 return void_type_node;
956 if (asmspec_tree && asmspec_tree != error_mark_node)
957 asmspec = TREE_STRING_POINTER (asmspec_tree);
959 if (init)
961 if (TREE_CODE (value) == FUNCTION_DECL)
963 if (init == ridpointers[(int)RID_DELETE])
965 DECL_DELETED_FN (value) = 1;
966 DECL_DECLARED_INLINE_P (value) = 1;
967 DECL_INITIAL (value) = error_mark_node;
969 else if (init == ridpointers[(int)RID_DEFAULT])
971 if (defaultable_fn_check (value))
973 DECL_DEFAULTED_FN (value) = 1;
974 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
975 DECL_DECLARED_INLINE_P (value) = 1;
978 else if (TREE_CODE (init) == DEFAULT_ARG)
979 error ("invalid initializer for member function %qD", value);
980 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
982 if (integer_zerop (init))
983 DECL_PURE_VIRTUAL_P (value) = 1;
984 else if (error_operand_p (init))
985 ; /* An error has already been reported. */
986 else
987 error ("invalid initializer for member function %qD",
988 value);
990 else
992 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
993 if (friendp)
994 error ("initializer specified for friend function %qD",
995 value);
996 else
997 error ("initializer specified for static member function %qD",
998 value);
1001 else if (TREE_CODE (value) == FIELD_DECL)
1002 /* C++11 NSDMI, keep going. */;
1003 else if (!VAR_P (value))
1004 gcc_unreachable ();
1007 /* Pass friend decls back. */
1008 if ((TREE_CODE (value) == FUNCTION_DECL
1009 || TREE_CODE (value) == TEMPLATE_DECL)
1010 && DECL_CONTEXT (value) != current_class_type)
1011 return value;
1013 /* Need to set this before push_template_decl. */
1014 if (TREE_CODE (value) == VAR_DECL)
1015 DECL_CONTEXT (value) = current_class_type;
1017 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1019 value = push_template_decl (value);
1020 if (error_operand_p (value))
1021 return error_mark_node;
1024 if (attrlist)
1025 cplus_decl_attributes (&value, attrlist, 0);
1027 if (init && DIRECT_LIST_INIT_P (init))
1028 flags = LOOKUP_NORMAL;
1029 else
1030 flags = LOOKUP_IMPLICIT;
1032 switch (TREE_CODE (value))
1034 case VAR_DECL:
1035 finish_static_data_member_decl (value, init, init_const_expr_p,
1036 asmspec_tree, flags);
1037 return value;
1039 case FIELD_DECL:
1040 if (asmspec)
1041 error ("%<asm%> specifiers are not permitted on non-static data members");
1042 if (DECL_INITIAL (value) == error_mark_node)
1043 init = error_mark_node;
1044 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1045 NULL_TREE, flags);
1046 DECL_IN_AGGR_P (value) = 1;
1047 return value;
1049 case FUNCTION_DECL:
1050 if (asmspec)
1051 set_user_assembler_name (value, asmspec);
1053 cp_finish_decl (value,
1054 /*init=*/NULL_TREE,
1055 /*init_const_expr_p=*/false,
1056 asmspec_tree, flags);
1058 /* Pass friends back this way. */
1059 if (DECL_FRIEND_P (value))
1060 return void_type_node;
1062 DECL_IN_AGGR_P (value) = 1;
1063 return value;
1065 default:
1066 gcc_unreachable ();
1068 return NULL_TREE;
1071 /* Like `grokfield', but for bitfields.
1072 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1074 tree
1075 grokbitfield (const cp_declarator *declarator,
1076 cp_decl_specifier_seq *declspecs, tree width,
1077 tree attrlist)
1079 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1081 if (value == error_mark_node)
1082 return NULL_TREE; /* friends went bad. */
1083 if (TREE_TYPE (value) == error_mark_node)
1084 return value;
1086 /* Pass friendly classes back. */
1087 if (VOID_TYPE_P (value))
1088 return void_type_node;
1090 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1091 && (POINTER_TYPE_P (value)
1092 || !dependent_type_p (TREE_TYPE (value))))
1094 error ("bit-field %qD with non-integral type", value);
1095 return error_mark_node;
1098 if (TREE_CODE (value) == TYPE_DECL)
1100 error ("cannot declare %qD to be a bit-field type", value);
1101 return NULL_TREE;
1104 /* Usually, finish_struct_1 catches bitfields with invalid types.
1105 But, in the case of bitfields with function type, we confuse
1106 ourselves into thinking they are member functions, so we must
1107 check here. */
1108 if (TREE_CODE (value) == FUNCTION_DECL)
1110 error ("cannot declare bit-field %qD with function type",
1111 DECL_NAME (value));
1112 return NULL_TREE;
1115 if (DECL_IN_AGGR_P (value))
1117 error ("%qD is already defined in the class %qT", value,
1118 DECL_CONTEXT (value));
1119 return void_type_node;
1122 if (TREE_STATIC (value))
1124 error ("static member %qD cannot be a bit-field", value);
1125 return NULL_TREE;
1127 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1129 if (width != error_mark_node)
1131 /* The width must be an integer type. */
1132 if (!type_dependent_expression_p (width)
1133 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1134 error ("width of bit-field %qD has non-integral type %qT", value,
1135 TREE_TYPE (width));
1136 DECL_INITIAL (value) = width;
1137 SET_DECL_C_BIT_FIELD (value);
1140 DECL_IN_AGGR_P (value) = 1;
1142 if (attrlist)
1143 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1145 return value;
1149 /* Returns true iff ATTR is an attribute which needs to be applied at
1150 instantiation time rather than template definition time. */
1152 static bool
1153 is_late_template_attribute (tree attr, tree decl)
1155 tree name = get_attribute_name (attr);
1156 tree args = TREE_VALUE (attr);
1157 const struct attribute_spec *spec = lookup_attribute_spec (name);
1158 tree arg;
1160 if (!spec)
1161 /* Unknown attribute. */
1162 return false;
1164 /* Attribute weak handling wants to write out assembly right away. */
1165 if (is_attribute_p ("weak", name))
1166 return true;
1168 /* Attribute unused is applied directly, as it appertains to
1169 decls. */
1170 if (is_attribute_p ("unused", name))
1171 return false;
1173 /* #pragma omp declare simd attribute needs to be always deferred. */
1174 if (flag_openmp
1175 && is_attribute_p ("omp declare simd", name))
1176 return true;
1178 /* If any of the arguments are dependent expressions, we can't evaluate
1179 the attribute until instantiation time. */
1180 for (arg = args; arg; arg = TREE_CHAIN (arg))
1182 tree t = TREE_VALUE (arg);
1184 /* If the first attribute argument is an identifier, only consider
1185 second and following arguments. Attributes like mode, format,
1186 cleanup and several target specific attributes aren't late
1187 just because they have an IDENTIFIER_NODE as first argument. */
1188 if (arg == args && identifier_p (t))
1189 continue;
1191 if (value_dependent_expression_p (t)
1192 || type_dependent_expression_p (t))
1193 return true;
1196 if (TREE_CODE (decl) == TYPE_DECL
1197 || TYPE_P (decl)
1198 || spec->type_required)
1200 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1202 /* We can't apply any attributes to a completely unknown type until
1203 instantiation time. */
1204 enum tree_code code = TREE_CODE (type);
1205 if (code == TEMPLATE_TYPE_PARM
1206 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1207 || code == TYPENAME_TYPE)
1208 return true;
1209 /* Also defer most attributes on dependent types. This is not
1210 necessary in all cases, but is the better default. */
1211 else if (dependent_type_p (type)
1212 /* But some attributes specifically apply to templates. */
1213 && !is_attribute_p ("abi_tag", name)
1214 && !is_attribute_p ("deprecated", name)
1215 && !is_attribute_p ("visibility", name))
1216 return true;
1217 else
1218 return false;
1220 else
1221 return false;
1224 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1225 applied at instantiation time and return them. If IS_DEPENDENT is true,
1226 the declaration itself is dependent, so all attributes should be applied
1227 at instantiation time. */
1229 static tree
1230 splice_template_attributes (tree *attr_p, tree decl)
1232 tree *p = attr_p;
1233 tree late_attrs = NULL_TREE;
1234 tree *q = &late_attrs;
1236 if (!p)
1237 return NULL_TREE;
1239 for (; *p; )
1241 if (is_late_template_attribute (*p, decl))
1243 ATTR_IS_DEPENDENT (*p) = 1;
1244 *q = *p;
1245 *p = TREE_CHAIN (*p);
1246 q = &TREE_CHAIN (*q);
1247 *q = NULL_TREE;
1249 else
1250 p = &TREE_CHAIN (*p);
1253 return late_attrs;
1256 /* Remove any late attributes from the list in ATTR_P and attach them to
1257 DECL_P. */
1259 static void
1260 save_template_attributes (tree *attr_p, tree *decl_p)
1262 tree *q;
1264 if (attr_p && *attr_p == error_mark_node)
1265 return;
1267 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1268 if (!late_attrs)
1269 return;
1271 if (DECL_P (*decl_p))
1272 q = &DECL_ATTRIBUTES (*decl_p);
1273 else
1274 q = &TYPE_ATTRIBUTES (*decl_p);
1276 tree old_attrs = *q;
1278 /* Merge the late attributes at the beginning with the attribute
1279 list. */
1280 late_attrs = merge_attributes (late_attrs, *q);
1281 *q = late_attrs;
1283 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1285 /* We've added new attributes directly to the main variant, so
1286 now we need to update all of the other variants to include
1287 these new attributes. */
1288 tree variant;
1289 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1290 variant = TYPE_NEXT_VARIANT (variant))
1292 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1293 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1298 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1299 to a typedef which gives a previously anonymous class or enum a name for
1300 linkage purposes. */
1302 bool
1303 attributes_naming_typedef_ok (tree attrs)
1305 for (; attrs; attrs = TREE_CHAIN (attrs))
1307 tree name = get_attribute_name (attrs);
1308 if (is_attribute_p ("vector_size", name))
1309 return false;
1311 return true;
1314 /* Like reconstruct_complex_type, but handle also template trees. */
1316 tree
1317 cp_reconstruct_complex_type (tree type, tree bottom)
1319 tree inner, outer;
1320 bool late_return_type_p = false;
1322 if (TYPE_PTR_P (type))
1324 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1325 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1326 TYPE_REF_CAN_ALIAS_ALL (type));
1328 else if (TREE_CODE (type) == REFERENCE_TYPE)
1330 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1331 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1332 TYPE_REF_CAN_ALIAS_ALL (type));
1334 else if (TREE_CODE (type) == ARRAY_TYPE)
1336 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1337 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1338 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1339 element type qualification will be handled by the recursive
1340 cp_reconstruct_complex_type call and cp_build_qualified_type
1341 for ARRAY_TYPEs changes the element type. */
1342 return outer;
1344 else if (TREE_CODE (type) == FUNCTION_TYPE)
1346 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1347 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1348 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1349 outer = apply_memfn_quals (outer,
1350 type_memfn_quals (type),
1351 type_memfn_rqual (type));
1353 else if (TREE_CODE (type) == METHOD_TYPE)
1355 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1356 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1357 /* The build_method_type_directly() routine prepends 'this' to argument list,
1358 so we must compensate by getting rid of it. */
1359 outer
1360 = build_method_type_directly
1361 (class_of_this_parm (type), inner,
1362 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1364 else if (TREE_CODE (type) == OFFSET_TYPE)
1366 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1367 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1369 else
1370 return bottom;
1372 if (TYPE_ATTRIBUTES (type))
1373 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1374 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1376 if (late_return_type_p)
1377 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1379 return outer;
1382 /* Replaces any constexpr expression that may be into the attributes
1383 arguments with their reduced value. */
1385 static void
1386 cp_check_const_attributes (tree attributes)
1388 if (attributes == error_mark_node)
1389 return;
1391 tree attr;
1392 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1394 tree arg;
1395 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1397 tree expr = TREE_VALUE (arg);
1398 if (EXPR_P (expr))
1399 TREE_VALUE (arg) = maybe_constant_value (expr);
1404 /* Return true if TYPE is an OpenMP mappable type. */
1405 bool
1406 cp_omp_mappable_type (tree type)
1408 /* Mappable type has to be complete. */
1409 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1410 return false;
1411 /* Arrays have mappable type if the elements have mappable type. */
1412 while (TREE_CODE (type) == ARRAY_TYPE)
1413 type = TREE_TYPE (type);
1414 /* A mappable type cannot contain virtual members. */
1415 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1416 return false;
1417 /* All data members must be non-static. */
1418 if (CLASS_TYPE_P (type))
1420 tree field;
1421 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1422 if (TREE_CODE (field) == VAR_DECL)
1423 return false;
1424 /* All fields must have mappable types. */
1425 else if (TREE_CODE (field) == FIELD_DECL
1426 && !cp_omp_mappable_type (TREE_TYPE (field)))
1427 return false;
1429 return true;
1432 /* Like decl_attributes, but handle C++ complexity. */
1434 void
1435 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1437 if (*decl == NULL_TREE || *decl == void_type_node
1438 || *decl == error_mark_node)
1439 return;
1441 /* Add implicit "omp declare target" attribute if requested. */
1442 if (scope_chain->omp_declare_target_attribute
1443 && ((TREE_CODE (*decl) == VAR_DECL && TREE_STATIC (*decl))
1444 || TREE_CODE (*decl) == FUNCTION_DECL))
1446 if (TREE_CODE (*decl) == VAR_DECL
1447 && DECL_CLASS_SCOPE_P (*decl))
1448 error ("%q+D static data member inside of declare target directive",
1449 *decl);
1450 else if (TREE_CODE (*decl) == VAR_DECL
1451 && (DECL_FUNCTION_SCOPE_P (*decl)
1452 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1453 error ("%q+D in block scope inside of declare target directive",
1454 *decl);
1455 else if (!processing_template_decl
1456 && TREE_CODE (*decl) == VAR_DECL
1457 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1458 error ("%q+D in declare target directive does not have mappable type",
1459 *decl);
1460 else
1461 attributes = tree_cons (get_identifier ("omp declare target"),
1462 NULL_TREE, attributes);
1465 if (processing_template_decl)
1467 if (check_for_bare_parameter_packs (attributes))
1468 return;
1470 save_template_attributes (&attributes, decl);
1473 cp_check_const_attributes (attributes);
1475 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1476 decl = &DECL_TEMPLATE_RESULT (*decl);
1478 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1480 attributes
1481 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1482 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1483 attributes, flags);
1485 else
1486 decl_attributes (decl, attributes, flags);
1488 if (TREE_CODE (*decl) == TYPE_DECL)
1489 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1491 /* Propagate deprecation out to the template. */
1492 if (TREE_DEPRECATED (*decl))
1493 if (tree ti = get_template_info (*decl))
1495 tree tmpl = TI_TEMPLATE (ti);
1496 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1497 : DECL_TEMPLATE_RESULT (tmpl));
1498 if (*decl == pattern)
1499 TREE_DEPRECATED (tmpl) = true;
1503 /* Walks through the namespace- or function-scope anonymous union
1504 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1505 Returns one of the fields for use in the mangled name. */
1507 static tree
1508 build_anon_union_vars (tree type, tree object)
1510 tree main_decl = NULL_TREE;
1511 tree field;
1513 /* Rather than write the code to handle the non-union case,
1514 just give an error. */
1515 if (TREE_CODE (type) != UNION_TYPE)
1517 error ("anonymous struct not inside named type");
1518 return error_mark_node;
1521 for (field = TYPE_FIELDS (type);
1522 field != NULL_TREE;
1523 field = DECL_CHAIN (field))
1525 tree decl;
1526 tree ref;
1528 if (DECL_ARTIFICIAL (field))
1529 continue;
1530 if (TREE_CODE (field) != FIELD_DECL)
1532 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1533 "have non-static data members", field);
1534 continue;
1537 if (TREE_PRIVATE (field))
1538 permerror (input_location, "private member %q+#D in anonymous union", field);
1539 else if (TREE_PROTECTED (field))
1540 permerror (input_location, "protected member %q+#D in anonymous union", field);
1542 if (processing_template_decl)
1543 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1544 DECL_NAME (field), NULL_TREE);
1545 else
1546 ref = build_class_member_access_expr (object, field, NULL_TREE,
1547 false, tf_warning_or_error);
1549 if (DECL_NAME (field))
1551 tree base;
1553 decl = build_decl (input_location,
1554 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1555 DECL_ANON_UNION_VAR_P (decl) = 1;
1556 DECL_ARTIFICIAL (decl) = 1;
1558 base = get_base_address (object);
1559 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1560 TREE_STATIC (decl) = TREE_STATIC (base);
1561 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1563 SET_DECL_VALUE_EXPR (decl, ref);
1564 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1566 decl = pushdecl (decl);
1568 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1569 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1570 else
1571 decl = 0;
1573 if (main_decl == NULL_TREE)
1574 main_decl = decl;
1577 return main_decl;
1580 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1581 anonymous union, then all members must be laid out together. PUBLIC_P
1582 is nonzero if this union is not declared static. */
1584 void
1585 finish_anon_union (tree anon_union_decl)
1587 tree type;
1588 tree main_decl;
1589 bool public_p;
1591 if (anon_union_decl == error_mark_node)
1592 return;
1594 type = TREE_TYPE (anon_union_decl);
1595 public_p = TREE_PUBLIC (anon_union_decl);
1597 /* The VAR_DECL's context is the same as the TYPE's context. */
1598 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1600 if (TYPE_FIELDS (type) == NULL_TREE)
1601 return;
1603 if (public_p)
1605 error ("namespace-scope anonymous aggregates must be static");
1606 return;
1609 main_decl = build_anon_union_vars (type, anon_union_decl);
1610 if (main_decl == error_mark_node)
1611 return;
1612 if (main_decl == NULL_TREE)
1614 warning (0, "anonymous union with no members");
1615 return;
1618 if (!processing_template_decl)
1620 /* Use main_decl to set the mangled name. */
1621 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1622 maybe_commonize_var (anon_union_decl);
1623 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1624 mangle_decl (anon_union_decl);
1625 DECL_NAME (anon_union_decl) = NULL_TREE;
1628 pushdecl (anon_union_decl);
1629 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1632 /* Auxiliary functions to make type signatures for
1633 `operator new' and `operator delete' correspond to
1634 what compiler will be expecting. */
1636 tree
1637 coerce_new_type (tree type)
1639 int e = 0;
1640 tree args = TYPE_ARG_TYPES (type);
1642 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1644 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1646 e = 1;
1647 error ("%<operator new%> must return type %qT", ptr_type_node);
1650 if (args && args != void_list_node)
1652 if (TREE_PURPOSE (args))
1654 /* [basic.stc.dynamic.allocation]
1656 The first parameter shall not have an associated default
1657 argument. */
1658 error ("the first parameter of %<operator new%> cannot "
1659 "have a default argument");
1660 /* Throw away the default argument. */
1661 TREE_PURPOSE (args) = NULL_TREE;
1664 if (!same_type_p (TREE_VALUE (args), size_type_node))
1666 e = 2;
1667 args = TREE_CHAIN (args);
1670 else
1671 e = 2;
1673 if (e == 2)
1674 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1675 "as first parameter", size_type_node);
1677 switch (e)
1679 case 2:
1680 args = tree_cons (NULL_TREE, size_type_node, args);
1681 /* Fall through. */
1682 case 1:
1683 type = build_exception_variant
1684 (build_function_type (ptr_type_node, args),
1685 TYPE_RAISES_EXCEPTIONS (type));
1686 /* Fall through. */
1687 default:;
1689 return type;
1692 tree
1693 coerce_delete_type (tree type)
1695 int e = 0;
1696 tree args = TYPE_ARG_TYPES (type);
1698 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1700 if (!same_type_p (TREE_TYPE (type), void_type_node))
1702 e = 1;
1703 error ("%<operator delete%> must return type %qT", void_type_node);
1706 if (!args || args == void_list_node
1707 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1709 e = 2;
1710 if (args && args != void_list_node)
1711 args = TREE_CHAIN (args);
1712 error ("%<operator delete%> takes type %qT as first parameter",
1713 ptr_type_node);
1715 switch (e)
1717 case 2:
1718 args = tree_cons (NULL_TREE, ptr_type_node, args);
1719 /* Fall through. */
1720 case 1:
1721 type = build_exception_variant
1722 (build_function_type (void_type_node, args),
1723 TYPE_RAISES_EXCEPTIONS (type));
1724 /* Fall through. */
1725 default:;
1728 return type;
1731 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1732 and mark them as needed. */
1734 static void
1735 mark_vtable_entries (tree decl)
1737 tree fnaddr;
1738 unsigned HOST_WIDE_INT idx;
1740 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1741 idx, fnaddr)
1743 tree fn;
1745 STRIP_NOPS (fnaddr);
1747 if (TREE_CODE (fnaddr) != ADDR_EXPR
1748 && TREE_CODE (fnaddr) != FDESC_EXPR)
1749 /* This entry is an offset: a virtual base class offset, a
1750 virtual call offset, an RTTI offset, etc. */
1751 continue;
1753 fn = TREE_OPERAND (fnaddr, 0);
1754 TREE_ADDRESSABLE (fn) = 1;
1755 /* When we don't have vcall offsets, we output thunks whenever
1756 we output the vtables that contain them. With vcall offsets,
1757 we know all the thunks we'll need when we emit a virtual
1758 function, so we emit the thunks there instead. */
1759 if (DECL_THUNK_P (fn))
1760 use_thunk (fn, /*emit_p=*/0);
1761 mark_used (fn);
1765 /* Set DECL up to have the closest approximation of "initialized common"
1766 linkage available. */
1768 void
1769 comdat_linkage (tree decl)
1771 if (flag_weak)
1772 make_decl_one_only (decl, cxx_comdat_group (decl));
1773 else if (TREE_CODE (decl) == FUNCTION_DECL
1774 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1775 /* We can just emit function and compiler-generated variables
1776 statically; having multiple copies is (for the most part) only
1777 a waste of space.
1779 There are two correctness issues, however: the address of a
1780 template instantiation with external linkage should be the
1781 same, independent of what translation unit asks for the
1782 address, and this will not hold when we emit multiple copies of
1783 the function. However, there's little else we can do.
1785 Also, by default, the typeinfo implementation assumes that
1786 there will be only one copy of the string used as the name for
1787 each type. Therefore, if weak symbols are unavailable, the
1788 run-time library should perform a more conservative check; it
1789 should perform a string comparison, rather than an address
1790 comparison. */
1791 TREE_PUBLIC (decl) = 0;
1792 else
1794 /* Static data member template instantiations, however, cannot
1795 have multiple copies. */
1796 if (DECL_INITIAL (decl) == 0
1797 || DECL_INITIAL (decl) == error_mark_node)
1798 DECL_COMMON (decl) = 1;
1799 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1801 DECL_COMMON (decl) = 1;
1802 DECL_INITIAL (decl) = error_mark_node;
1804 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1806 /* We can't do anything useful; leave vars for explicit
1807 instantiation. */
1808 DECL_EXTERNAL (decl) = 1;
1809 DECL_NOT_REALLY_EXTERN (decl) = 0;
1813 DECL_COMDAT (decl) = 1;
1816 /* For win32 we also want to put explicit instantiations in
1817 linkonce sections, so that they will be merged with implicit
1818 instantiations; otherwise we get duplicate symbol errors.
1819 For Darwin we do not want explicit instantiations to be
1820 linkonce. */
1822 void
1823 maybe_make_one_only (tree decl)
1825 /* We used to say that this was not necessary on targets that support weak
1826 symbols, because the implicit instantiations will defer to the explicit
1827 one. However, that's not actually the case in SVR4; a strong definition
1828 after a weak one is an error. Also, not making explicit
1829 instantiations one_only means that we can end up with two copies of
1830 some template instantiations. */
1831 if (! flag_weak)
1832 return;
1834 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1835 we can get away with not emitting them if they aren't used. We need
1836 to for variables so that cp_finish_decl will update their linkage,
1837 because their DECL_INITIAL may not have been set properly yet. */
1839 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1840 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1841 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1843 make_decl_one_only (decl, cxx_comdat_group (decl));
1845 if (VAR_P (decl))
1847 varpool_node *node = varpool_node::get_create (decl);
1848 DECL_COMDAT (decl) = 1;
1849 /* Mark it needed so we don't forget to emit it. */
1850 node->forced_by_abi = true;
1851 TREE_USED (decl) = 1;
1856 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1857 This predicate will give the right answer during parsing of the
1858 function, which other tests may not. */
1860 bool
1861 vague_linkage_p (tree decl)
1863 /* Unfortunately, import_export_decl has not always been called
1864 before the function is processed, so we cannot simply check
1865 DECL_COMDAT. */
1866 if (DECL_COMDAT (decl)
1867 || (((TREE_CODE (decl) == FUNCTION_DECL
1868 && DECL_DECLARED_INLINE_P (decl))
1869 || (DECL_LANG_SPECIFIC (decl)
1870 && DECL_TEMPLATE_INSTANTIATION (decl)))
1871 && TREE_PUBLIC (decl)))
1872 return true;
1873 else if (DECL_FUNCTION_SCOPE_P (decl))
1874 /* A local static in an inline effectively has vague linkage. */
1875 return (TREE_STATIC (decl)
1876 && vague_linkage_p (DECL_CONTEXT (decl)));
1877 else
1878 return false;
1881 /* Determine whether or not we want to specifically import or export CTYPE,
1882 using various heuristics. */
1884 static void
1885 import_export_class (tree ctype)
1887 /* -1 for imported, 1 for exported. */
1888 int import_export = 0;
1890 /* It only makes sense to call this function at EOF. The reason is
1891 that this function looks at whether or not the first non-inline
1892 non-abstract virtual member function has been defined in this
1893 translation unit. But, we can't possibly know that until we've
1894 seen the entire translation unit. */
1895 gcc_assert (at_eof);
1897 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1898 return;
1900 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1901 we will have CLASSTYPE_INTERFACE_ONLY set but not
1902 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1903 heuristic because someone will supply a #pragma implementation
1904 elsewhere, and deducing it here would produce a conflict. */
1905 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1906 return;
1908 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1909 import_export = -1;
1910 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1911 import_export = 1;
1912 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1913 && !flag_implicit_templates)
1914 /* For a template class, without -fimplicit-templates, check the
1915 repository. If the virtual table is assigned to this
1916 translation unit, then export the class; otherwise, import
1917 it. */
1918 import_export = repo_export_class_p (ctype) ? 1 : -1;
1919 else if (TYPE_POLYMORPHIC_P (ctype))
1921 /* The ABI specifies that the virtual table and associated
1922 information are emitted with the key method, if any. */
1923 tree method = CLASSTYPE_KEY_METHOD (ctype);
1924 /* If weak symbol support is not available, then we must be
1925 careful not to emit the vtable when the key function is
1926 inline. An inline function can be defined in multiple
1927 translation units. If we were to emit the vtable in each
1928 translation unit containing a definition, we would get
1929 multiple definition errors at link-time. */
1930 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1931 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1934 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1935 a definition anywhere else. */
1936 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1937 import_export = 0;
1939 /* Allow back ends the chance to overrule the decision. */
1940 if (targetm.cxx.import_export_class)
1941 import_export = targetm.cxx.import_export_class (ctype, import_export);
1943 if (import_export)
1945 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1946 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1950 /* Return true if VAR has already been provided to the back end; in that
1951 case VAR should not be modified further by the front end. */
1952 static bool
1953 var_finalized_p (tree var)
1955 return varpool_node::get_create (var)->definition;
1958 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1959 must be emitted in this translation unit. Mark it as such. */
1961 void
1962 mark_needed (tree decl)
1964 TREE_USED (decl) = 1;
1965 if (TREE_CODE (decl) == FUNCTION_DECL)
1967 /* Extern inline functions don't become needed when referenced.
1968 If we know a method will be emitted in other TU and no new
1969 functions can be marked reachable, just use the external
1970 definition. */
1971 struct cgraph_node *node = cgraph_node::get_create (decl);
1972 node->forced_by_abi = true;
1974 /* #pragma interface and -frepo code can call mark_needed for
1975 maybe-in-charge 'tors; mark the clones as well. */
1976 tree clone;
1977 FOR_EACH_CLONE (clone, decl)
1978 mark_needed (clone);
1980 else if (TREE_CODE (decl) == VAR_DECL)
1982 varpool_node *node = varpool_node::get_create (decl);
1983 /* C++ frontend use mark_decl_references to force COMDAT variables
1984 to be output that might appear dead otherwise. */
1985 node->forced_by_abi = true;
1989 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1990 returns true if a definition of this entity should be provided in
1991 this object file. Callers use this function to determine whether
1992 or not to let the back end know that a definition of DECL is
1993 available in this translation unit. */
1995 bool
1996 decl_needed_p (tree decl)
1998 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1999 /* This function should only be called at the end of the translation
2000 unit. We cannot be sure of whether or not something will be
2001 COMDAT until that point. */
2002 gcc_assert (at_eof);
2004 /* All entities with external linkage that are not COMDAT/EXTERN should be
2005 emitted; they may be referred to from other object files. */
2006 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2007 return true;
2008 /* Functions marked "dllexport" must be emitted so that they are
2009 visible to other DLLs. */
2010 if (flag_keep_inline_dllexport
2011 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2012 return true;
2014 /* When not optimizing, do not bother to produce definitions for extern
2015 symbols. */
2016 if (DECL_REALLY_EXTERN (decl)
2017 && ((TREE_CODE (decl) != FUNCTION_DECL
2018 && !optimize)
2019 || (TREE_CODE (decl) == FUNCTION_DECL
2020 && !opt_for_fn (decl, optimize)))
2021 && !lookup_attribute ("always_inline", decl))
2022 return false;
2024 /* If this entity was used, let the back end see it; it will decide
2025 whether or not to emit it into the object file. */
2026 if (TREE_USED (decl))
2027 return true;
2028 /* Virtual functions might be needed for devirtualization. */
2029 if (flag_devirtualize
2030 && TREE_CODE (decl) == FUNCTION_DECL
2031 && DECL_VIRTUAL_P (decl))
2032 return true;
2033 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2034 reference to DECL might cause it to be emitted later. */
2035 return false;
2038 /* If necessary, write out the vtables for the dynamic class CTYPE.
2039 Returns true if any vtables were emitted. */
2041 static bool
2042 maybe_emit_vtables (tree ctype)
2044 tree vtbl;
2045 tree primary_vtbl;
2046 int needed = 0;
2047 varpool_node *current = NULL, *last = NULL;
2049 /* If the vtables for this class have already been emitted there is
2050 nothing more to do. */
2051 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2052 if (var_finalized_p (primary_vtbl))
2053 return false;
2054 /* Ignore dummy vtables made by get_vtable_decl. */
2055 if (TREE_TYPE (primary_vtbl) == void_type_node)
2056 return false;
2058 /* On some targets, we cannot determine the key method until the end
2059 of the translation unit -- which is when this function is
2060 called. */
2061 if (!targetm.cxx.key_method_may_be_inline ())
2062 determine_key_method (ctype);
2064 /* See if any of the vtables are needed. */
2065 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2067 import_export_decl (vtbl);
2068 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2069 needed = 1;
2071 if (!needed)
2073 /* If the references to this class' vtables are optimized away,
2074 still emit the appropriate debugging information. See
2075 dfs_debug_mark. */
2076 if (DECL_COMDAT (primary_vtbl)
2077 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2078 note_debug_info_needed (ctype);
2079 return false;
2082 /* The ABI requires that we emit all of the vtables if we emit any
2083 of them. */
2084 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2086 /* Mark entities references from the virtual table as used. */
2087 mark_vtable_entries (vtbl);
2089 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2091 vec<tree, va_gc> *cleanups = NULL;
2092 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2093 LOOKUP_NORMAL);
2095 /* It had better be all done at compile-time. */
2096 gcc_assert (!expr && !cleanups);
2099 /* Write it out. */
2100 DECL_EXTERNAL (vtbl) = 0;
2101 rest_of_decl_compilation (vtbl, 1, 1);
2103 /* Because we're only doing syntax-checking, we'll never end up
2104 actually marking the variable as written. */
2105 if (flag_syntax_only)
2106 TREE_ASM_WRITTEN (vtbl) = 1;
2107 else if (DECL_ONE_ONLY (vtbl))
2109 current = varpool_node::get_create (vtbl);
2110 if (last)
2111 current->add_to_same_comdat_group (last);
2112 last = current;
2116 /* Since we're writing out the vtable here, also write the debug
2117 info. */
2118 note_debug_info_needed (ctype);
2120 return true;
2123 /* A special return value from type_visibility meaning internal
2124 linkage. */
2126 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2128 /* walk_tree helper function for type_visibility. */
2130 static tree
2131 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2133 int *vis_p = (int *)data;
2134 if (! TYPE_P (*tp))
2136 *walk_subtrees = 0;
2138 else if (OVERLOAD_TYPE_P (*tp)
2139 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2141 *vis_p = VISIBILITY_ANON;
2142 return *tp;
2144 else if (CLASS_TYPE_P (*tp)
2145 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2146 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2147 return NULL;
2150 /* Returns the visibility of TYPE, which is the minimum visibility of its
2151 component types. */
2153 static int
2154 type_visibility (tree type)
2156 int vis = VISIBILITY_DEFAULT;
2157 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2158 return vis;
2161 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2162 specified (or if VISIBILITY is static). If TMPL is true, this
2163 constraint is for a template argument, and takes precedence
2164 over explicitly-specified visibility on the template. */
2166 static void
2167 constrain_visibility (tree decl, int visibility, bool tmpl)
2169 if (visibility == VISIBILITY_ANON)
2171 /* extern "C" declarations aren't affected by the anonymous
2172 namespace. */
2173 if (!DECL_EXTERN_C_P (decl))
2175 TREE_PUBLIC (decl) = 0;
2176 DECL_WEAK (decl) = 0;
2177 DECL_COMMON (decl) = 0;
2178 DECL_COMDAT (decl) = false;
2179 if (TREE_CODE (decl) == FUNCTION_DECL
2180 || TREE_CODE (decl) == VAR_DECL)
2182 struct symtab_node *snode = symtab_node::get (decl);
2184 if (snode)
2185 snode->set_comdat_group (NULL);
2187 DECL_INTERFACE_KNOWN (decl) = 1;
2188 if (DECL_LANG_SPECIFIC (decl))
2189 DECL_NOT_REALLY_EXTERN (decl) = 1;
2192 else if (visibility > DECL_VISIBILITY (decl)
2193 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2195 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2196 /* This visibility was not specified. */
2197 DECL_VISIBILITY_SPECIFIED (decl) = false;
2201 /* Constrain the visibility of DECL based on the visibility of its template
2202 arguments. */
2204 static void
2205 constrain_visibility_for_template (tree decl, tree targs)
2207 /* If this is a template instantiation, check the innermost
2208 template args for visibility constraints. The outer template
2209 args are covered by the class check. */
2210 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2211 int i;
2212 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2214 int vis = 0;
2216 tree arg = TREE_VEC_ELT (args, i-1);
2217 if (TYPE_P (arg))
2218 vis = type_visibility (arg);
2219 else
2221 if (REFERENCE_REF_P (arg))
2222 arg = TREE_OPERAND (arg, 0);
2223 if (TREE_TYPE (arg))
2224 STRIP_NOPS (arg);
2225 if (TREE_CODE (arg) == ADDR_EXPR)
2226 arg = TREE_OPERAND (arg, 0);
2227 if (VAR_OR_FUNCTION_DECL_P (arg))
2229 if (! TREE_PUBLIC (arg))
2230 vis = VISIBILITY_ANON;
2231 else
2232 vis = DECL_VISIBILITY (arg);
2235 if (vis)
2236 constrain_visibility (decl, vis, true);
2240 /* Like c_determine_visibility, but with additional C++-specific
2241 behavior.
2243 Function-scope entities can rely on the function's visibility because
2244 it is set in start_preparsed_function.
2246 Class-scope entities cannot rely on the class's visibility until the end
2247 of the enclosing class definition.
2249 Note that because namespaces have multiple independent definitions,
2250 namespace visibility is handled elsewhere using the #pragma visibility
2251 machinery rather than by decorating the namespace declaration.
2253 The goal is for constraints from the type to give a diagnostic, and
2254 other constraints to be applied silently. */
2256 void
2257 determine_visibility (tree decl)
2259 tree class_type = NULL_TREE;
2260 bool use_template;
2261 bool orig_visibility_specified;
2262 enum symbol_visibility orig_visibility;
2264 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2266 /* Only relevant for names with external linkage. */
2267 if (!TREE_PUBLIC (decl))
2268 return;
2270 /* Cloned constructors and destructors get the same visibility as
2271 the underlying function. That should be set up in
2272 maybe_clone_body. */
2273 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2275 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2276 orig_visibility = DECL_VISIBILITY (decl);
2278 if (TREE_CODE (decl) == TYPE_DECL)
2280 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2281 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2282 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2283 use_template = 1;
2284 else
2285 use_template = 0;
2287 else if (DECL_LANG_SPECIFIC (decl))
2288 use_template = DECL_USE_TEMPLATE (decl);
2289 else
2290 use_template = 0;
2292 /* If DECL is a member of a class, visibility specifiers on the
2293 class can influence the visibility of the DECL. */
2294 if (DECL_CLASS_SCOPE_P (decl))
2295 class_type = DECL_CONTEXT (decl);
2296 else
2298 /* Not a class member. */
2300 /* Virtual tables have DECL_CONTEXT set to their associated class,
2301 so they are automatically handled above. */
2302 gcc_assert (!VAR_P (decl)
2303 || !DECL_VTABLE_OR_VTT_P (decl));
2305 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2307 /* Local statics and classes get the visibility of their
2308 containing function by default, except that
2309 -fvisibility-inlines-hidden doesn't affect them. */
2310 tree fn = DECL_CONTEXT (decl);
2311 if (DECL_VISIBILITY_SPECIFIED (fn))
2313 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2314 DECL_VISIBILITY_SPECIFIED (decl) =
2315 DECL_VISIBILITY_SPECIFIED (fn);
2317 else
2319 if (DECL_CLASS_SCOPE_P (fn))
2320 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2321 else if (determine_hidden_inline (fn))
2323 DECL_VISIBILITY (decl) = default_visibility;
2324 DECL_VISIBILITY_SPECIFIED (decl) =
2325 visibility_options.inpragma;
2327 else
2329 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2330 DECL_VISIBILITY_SPECIFIED (decl) =
2331 DECL_VISIBILITY_SPECIFIED (fn);
2335 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2336 but have no TEMPLATE_INFO, so don't try to check it. */
2337 use_template = 0;
2339 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2340 && flag_visibility_ms_compat)
2342 /* Under -fvisibility-ms-compat, types are visible by default,
2343 even though their contents aren't. */
2344 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2345 int underlying_vis = type_visibility (underlying_type);
2346 if (underlying_vis == VISIBILITY_ANON
2347 || (CLASS_TYPE_P (underlying_type)
2348 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2349 constrain_visibility (decl, underlying_vis, false);
2350 else
2351 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2353 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2355 /* tinfo visibility is based on the type it's for. */
2356 constrain_visibility
2357 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2359 /* Give the target a chance to override the visibility associated
2360 with DECL. */
2361 if (TREE_PUBLIC (decl)
2362 && !DECL_REALLY_EXTERN (decl)
2363 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2364 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2365 targetm.cxx.determine_class_data_visibility (decl);
2367 else if (use_template)
2368 /* Template instantiations and specializations get visibility based
2369 on their template unless they override it with an attribute. */;
2370 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2372 if (determine_hidden_inline (decl))
2373 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2374 else
2376 /* Set default visibility to whatever the user supplied with
2377 #pragma GCC visibility or a namespace visibility attribute. */
2378 DECL_VISIBILITY (decl) = default_visibility;
2379 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2384 if (use_template)
2386 /* If the specialization doesn't specify visibility, use the
2387 visibility from the template. */
2388 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2389 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2390 : DECL_TEMPLATE_INFO (decl));
2391 tree args = TI_ARGS (tinfo);
2392 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2393 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2394 : DECL_ATTRIBUTES (decl));
2396 if (args != error_mark_node)
2398 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2400 if (!DECL_VISIBILITY_SPECIFIED (decl))
2402 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2403 && determine_hidden_inline (decl))
2404 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2405 else
2407 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2408 DECL_VISIBILITY_SPECIFIED (decl)
2409 = DECL_VISIBILITY_SPECIFIED (pattern);
2413 if (args
2414 /* Template argument visibility outweighs #pragma or namespace
2415 visibility, but not an explicit attribute. */
2416 && !lookup_attribute ("visibility", attribs))
2418 int depth = TMPL_ARGS_DEPTH (args);
2419 if (DECL_VISIBILITY_SPECIFIED (decl))
2421 /* A class template member with explicit visibility
2422 overrides the class visibility, so we need to apply
2423 all the levels of template args directly. */
2424 int i;
2425 for (i = 1; i <= depth; ++i)
2427 tree lev = TMPL_ARGS_LEVEL (args, i);
2428 constrain_visibility_for_template (decl, lev);
2431 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2432 /* Limit visibility based on its template arguments. */
2433 constrain_visibility_for_template (decl, args);
2438 if (class_type)
2439 determine_visibility_from_class (decl, class_type);
2441 if (decl_anon_ns_mem_p (decl))
2442 /* Names in an anonymous namespace get internal linkage.
2443 This might change once we implement export. */
2444 constrain_visibility (decl, VISIBILITY_ANON, false);
2445 else if (TREE_CODE (decl) != TYPE_DECL)
2447 /* Propagate anonymity from type to decl. */
2448 int tvis = type_visibility (TREE_TYPE (decl));
2449 if (tvis == VISIBILITY_ANON
2450 || ! DECL_VISIBILITY_SPECIFIED (decl))
2451 constrain_visibility (decl, tvis, false);
2453 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2454 /* DR 757: A type without linkage shall not be used as the type of a
2455 variable or function with linkage, unless
2456 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2457 o the variable or function is not used (3.2 [basic.def.odr]) or is
2458 defined in the same translation unit.
2460 Since non-extern "C" decls need to be defined in the same
2461 translation unit, we can make the type internal. */
2462 constrain_visibility (decl, VISIBILITY_ANON, false);
2464 /* If visibility changed and DECL already has DECL_RTL, ensure
2465 symbol flags are updated. */
2466 if ((DECL_VISIBILITY (decl) != orig_visibility
2467 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2468 && ((VAR_P (decl) && TREE_STATIC (decl))
2469 || TREE_CODE (decl) == FUNCTION_DECL)
2470 && DECL_RTL_SET_P (decl))
2471 make_decl_rtl (decl);
2474 /* By default, static data members and function members receive
2475 the visibility of their containing class. */
2477 static void
2478 determine_visibility_from_class (tree decl, tree class_type)
2480 if (DECL_VISIBILITY_SPECIFIED (decl))
2481 return;
2483 if (determine_hidden_inline (decl))
2484 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2485 else
2487 /* Default to the class visibility. */
2488 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2489 DECL_VISIBILITY_SPECIFIED (decl)
2490 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2493 /* Give the target a chance to override the visibility associated
2494 with DECL. */
2495 if (VAR_P (decl)
2496 && (DECL_TINFO_P (decl)
2497 || (DECL_VTABLE_OR_VTT_P (decl)
2498 /* Construction virtual tables are not exported because
2499 they cannot be referred to from other object files;
2500 their name is not standardized by the ABI. */
2501 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2502 && TREE_PUBLIC (decl)
2503 && !DECL_REALLY_EXTERN (decl)
2504 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2505 targetm.cxx.determine_class_data_visibility (decl);
2508 /* Returns true iff DECL is an inline that should get hidden visibility
2509 because of -fvisibility-inlines-hidden. */
2511 static bool
2512 determine_hidden_inline (tree decl)
2514 return (visibility_options.inlines_hidden
2515 /* Don't do this for inline templates; specializations might not be
2516 inline, and we don't want them to inherit the hidden
2517 visibility. We'll set it here for all inline instantiations. */
2518 && !processing_template_decl
2519 && TREE_CODE (decl) == FUNCTION_DECL
2520 && DECL_DECLARED_INLINE_P (decl)
2521 && (! DECL_LANG_SPECIFIC (decl)
2522 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2525 /* Constrain the visibility of a class TYPE based on the visibility of its
2526 field types. Warn if any fields require lesser visibility. */
2528 void
2529 constrain_class_visibility (tree type)
2531 tree binfo;
2532 tree t;
2533 int i;
2535 int vis = type_visibility (type);
2537 if (vis == VISIBILITY_ANON
2538 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2539 return;
2541 /* Don't warn about visibility if the class has explicit visibility. */
2542 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2543 vis = VISIBILITY_INTERNAL;
2545 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2546 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2548 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2549 int subvis = type_visibility (ftype);
2551 if (subvis == VISIBILITY_ANON)
2553 if (!in_main_input_context ())
2554 warning (0, "\
2555 %qT has a field %qD whose type uses the anonymous namespace",
2556 type, t);
2558 else if (MAYBE_CLASS_TYPE_P (ftype)
2559 && vis < VISIBILITY_HIDDEN
2560 && subvis >= VISIBILITY_HIDDEN)
2561 warning (OPT_Wattributes, "\
2562 %qT declared with greater visibility than the type of its field %qD",
2563 type, t);
2566 binfo = TYPE_BINFO (type);
2567 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2569 int subvis = type_visibility (TREE_TYPE (t));
2571 if (subvis == VISIBILITY_ANON)
2573 if (!in_main_input_context())
2574 warning (0, "\
2575 %qT has a base %qT whose type uses the anonymous namespace",
2576 type, TREE_TYPE (t));
2578 else if (vis < VISIBILITY_HIDDEN
2579 && subvis >= VISIBILITY_HIDDEN)
2580 warning (OPT_Wattributes, "\
2581 %qT declared with greater visibility than its base %qT",
2582 type, TREE_TYPE (t));
2586 /* Functions for adjusting the visibility of a tagged type and its nested
2587 types and declarations when it gets a name for linkage purposes from a
2588 typedef. */
2590 static void bt_reset_linkage_1 (binding_entry, void *);
2591 static void bt_reset_linkage_2 (binding_entry, void *);
2593 /* First reset the visibility of all the types. */
2595 static void
2596 reset_type_linkage_1 (tree type)
2598 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2599 if (CLASS_TYPE_P (type))
2600 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2601 bt_reset_linkage_1, NULL);
2603 static void
2604 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2606 reset_type_linkage_1 (b->type);
2609 /* Then reset the visibility of any static data members or member
2610 functions that use those types. */
2612 static void
2613 reset_decl_linkage (tree decl)
2615 if (TREE_PUBLIC (decl))
2616 return;
2617 if (DECL_CLONED_FUNCTION_P (decl))
2618 return;
2619 TREE_PUBLIC (decl) = true;
2620 DECL_INTERFACE_KNOWN (decl) = false;
2621 determine_visibility (decl);
2622 tentative_decl_linkage (decl);
2624 static void
2625 reset_type_linkage_2 (tree type)
2627 if (CLASS_TYPE_P (type))
2629 if (tree vt = CLASSTYPE_VTABLES (type))
2631 tree name = mangle_vtbl_for_type (type);
2632 DECL_NAME (vt) = name;
2633 SET_DECL_ASSEMBLER_NAME (vt, name);
2634 reset_decl_linkage (vt);
2636 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2638 tree name = mangle_typeinfo_for_type (type);
2639 DECL_NAME (ti) = name;
2640 SET_DECL_ASSEMBLER_NAME (ti, name);
2641 TREE_TYPE (name) = type;
2642 reset_decl_linkage (ti);
2644 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2645 if (TREE_CODE (m) == VAR_DECL)
2646 reset_decl_linkage (m);
2647 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2648 reset_decl_linkage (m);
2649 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2650 bt_reset_linkage_2, NULL);
2653 static void
2654 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2656 reset_type_linkage_2 (b->type);
2658 void
2659 reset_type_linkage (tree type)
2661 reset_type_linkage_1 (type);
2662 reset_type_linkage_2 (type);
2665 /* Set up our initial idea of what the linkage of DECL should be. */
2667 void
2668 tentative_decl_linkage (tree decl)
2670 if (DECL_INTERFACE_KNOWN (decl))
2671 /* We've already made a decision as to how this function will
2672 be handled. */;
2673 else if (vague_linkage_p (decl))
2675 if (TREE_CODE (decl) == FUNCTION_DECL
2676 && decl_defined_p (decl))
2678 DECL_EXTERNAL (decl) = 1;
2679 DECL_NOT_REALLY_EXTERN (decl) = 1;
2680 note_vague_linkage_fn (decl);
2681 /* A non-template inline function with external linkage will
2682 always be COMDAT. As we must eventually determine the
2683 linkage of all functions, and as that causes writes to
2684 the data mapped in from the PCH file, it's advantageous
2685 to mark the functions at this point. */
2686 if (DECL_DECLARED_INLINE_P (decl)
2687 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2688 || DECL_DEFAULTED_FN (decl)))
2690 /* This function must have external linkage, as
2691 otherwise DECL_INTERFACE_KNOWN would have been
2692 set. */
2693 gcc_assert (TREE_PUBLIC (decl));
2694 comdat_linkage (decl);
2695 DECL_INTERFACE_KNOWN (decl) = 1;
2698 else if (TREE_CODE (decl) == VAR_DECL)
2699 maybe_commonize_var (decl);
2703 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2704 for DECL has not already been determined, do so now by setting
2705 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2706 function is called entities with vague linkage whose definitions
2707 are available must have TREE_PUBLIC set.
2709 If this function decides to place DECL in COMDAT, it will set
2710 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2711 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2712 callers defer that decision until it is clear that DECL is actually
2713 required. */
2715 void
2716 import_export_decl (tree decl)
2718 int emit_p;
2719 bool comdat_p;
2720 bool import_p;
2721 tree class_type = NULL_TREE;
2723 if (DECL_INTERFACE_KNOWN (decl))
2724 return;
2726 /* We cannot determine what linkage to give to an entity with vague
2727 linkage until the end of the file. For example, a virtual table
2728 for a class will be defined if and only if the key method is
2729 defined in this translation unit. As a further example, consider
2730 that when compiling a translation unit that uses PCH file with
2731 "-frepo" it would be incorrect to make decisions about what
2732 entities to emit when building the PCH; those decisions must be
2733 delayed until the repository information has been processed. */
2734 gcc_assert (at_eof);
2735 /* Object file linkage for explicit instantiations is handled in
2736 mark_decl_instantiated. For static variables in functions with
2737 vague linkage, maybe_commonize_var is used.
2739 Therefore, the only declarations that should be provided to this
2740 function are those with external linkage that are:
2742 * implicit instantiations of function templates
2744 * inline function
2746 * implicit instantiations of static data members of class
2747 templates
2749 * virtual tables
2751 * typeinfo objects
2753 Furthermore, all entities that reach this point must have a
2754 definition available in this translation unit.
2756 The following assertions check these conditions. */
2757 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2758 /* Any code that creates entities with TREE_PUBLIC cleared should
2759 also set DECL_INTERFACE_KNOWN. */
2760 gcc_assert (TREE_PUBLIC (decl));
2761 if (TREE_CODE (decl) == FUNCTION_DECL)
2762 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2763 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2764 || DECL_DECLARED_INLINE_P (decl));
2765 else
2766 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2767 || DECL_VTABLE_OR_VTT_P (decl)
2768 || DECL_TINFO_P (decl));
2769 /* Check that a definition of DECL is available in this translation
2770 unit. */
2771 gcc_assert (!DECL_REALLY_EXTERN (decl));
2773 /* Assume that DECL will not have COMDAT linkage. */
2774 comdat_p = false;
2775 /* Assume that DECL will not be imported into this translation
2776 unit. */
2777 import_p = false;
2779 /* See if the repository tells us whether or not to emit DECL in
2780 this translation unit. */
2781 emit_p = repo_emit_p (decl);
2782 if (emit_p == 0)
2783 import_p = true;
2784 else if (emit_p == 1)
2786 /* The repository indicates that this entity should be defined
2787 here. Make sure the back end honors that request. */
2788 mark_needed (decl);
2789 /* Output the definition as an ordinary strong definition. */
2790 DECL_EXTERNAL (decl) = 0;
2791 DECL_INTERFACE_KNOWN (decl) = 1;
2792 return;
2795 if (import_p)
2796 /* We have already decided what to do with this DECL; there is no
2797 need to check anything further. */
2799 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2801 class_type = DECL_CONTEXT (decl);
2802 import_export_class (class_type);
2803 if (TYPE_FOR_JAVA (class_type))
2804 import_p = true;
2805 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2806 && CLASSTYPE_INTERFACE_ONLY (class_type))
2807 import_p = true;
2808 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2809 && !CLASSTYPE_USE_TEMPLATE (class_type)
2810 && CLASSTYPE_KEY_METHOD (class_type)
2811 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2812 /* The ABI requires that all virtual tables be emitted with
2813 COMDAT linkage. However, on systems where COMDAT symbols
2814 don't show up in the table of contents for a static
2815 archive, or on systems without weak symbols (where we
2816 approximate COMDAT linkage by using internal linkage), the
2817 linker will report errors about undefined symbols because
2818 it will not see the virtual table definition. Therefore,
2819 in the case that we know that the virtual table will be
2820 emitted in only one translation unit, we make the virtual
2821 table an ordinary definition with external linkage. */
2822 DECL_EXTERNAL (decl) = 0;
2823 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2825 /* CLASS_TYPE is being exported from this translation unit,
2826 so DECL should be defined here. */
2827 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2828 /* If a class is declared in a header with the "extern
2829 template" extension, then it will not be instantiated,
2830 even in translation units that would normally require
2831 it. Often such classes are explicitly instantiated in
2832 one translation unit. Therefore, the explicit
2833 instantiation must be made visible to other translation
2834 units. */
2835 DECL_EXTERNAL (decl) = 0;
2836 else
2838 /* The generic C++ ABI says that class data is always
2839 COMDAT, even if there is a key function. Some
2840 variants (e.g., the ARM EABI) says that class data
2841 only has COMDAT linkage if the class data might be
2842 emitted in more than one translation unit. When the
2843 key method can be inline and is inline, we still have
2844 to arrange for comdat even though
2845 class_data_always_comdat is false. */
2846 if (!CLASSTYPE_KEY_METHOD (class_type)
2847 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2848 || targetm.cxx.class_data_always_comdat ())
2850 /* The ABI requires COMDAT linkage. Normally, we
2851 only emit COMDAT things when they are needed;
2852 make sure that we realize that this entity is
2853 indeed needed. */
2854 comdat_p = true;
2855 mark_needed (decl);
2859 else if (!flag_implicit_templates
2860 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2861 import_p = true;
2862 else
2863 comdat_p = true;
2865 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2867 tree type = TREE_TYPE (DECL_NAME (decl));
2868 if (CLASS_TYPE_P (type))
2870 class_type = type;
2871 import_export_class (type);
2872 if (CLASSTYPE_INTERFACE_KNOWN (type)
2873 && TYPE_POLYMORPHIC_P (type)
2874 && CLASSTYPE_INTERFACE_ONLY (type)
2875 /* If -fno-rtti was specified, then we cannot be sure
2876 that RTTI information will be emitted with the
2877 virtual table of the class, so we must emit it
2878 wherever it is used. */
2879 && flag_rtti)
2880 import_p = true;
2881 else
2883 if (CLASSTYPE_INTERFACE_KNOWN (type)
2884 && !CLASSTYPE_INTERFACE_ONLY (type))
2886 comdat_p = (targetm.cxx.class_data_always_comdat ()
2887 || (CLASSTYPE_KEY_METHOD (type)
2888 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2889 mark_needed (decl);
2890 if (!flag_weak)
2892 comdat_p = false;
2893 DECL_EXTERNAL (decl) = 0;
2896 else
2897 comdat_p = true;
2900 else
2901 comdat_p = true;
2903 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2905 /* DECL is an implicit instantiation of a function or static
2906 data member. */
2907 if ((flag_implicit_templates
2908 && !flag_use_repository)
2909 || (flag_implicit_inline_templates
2910 && TREE_CODE (decl) == FUNCTION_DECL
2911 && DECL_DECLARED_INLINE_P (decl)))
2912 comdat_p = true;
2913 else
2914 /* If we are not implicitly generating templates, then mark
2915 this entity as undefined in this translation unit. */
2916 import_p = true;
2918 else if (DECL_FUNCTION_MEMBER_P (decl))
2920 if (!DECL_DECLARED_INLINE_P (decl))
2922 tree ctype = DECL_CONTEXT (decl);
2923 import_export_class (ctype);
2924 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2926 DECL_NOT_REALLY_EXTERN (decl)
2927 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2928 || (DECL_DECLARED_INLINE_P (decl)
2929 && ! flag_implement_inlines
2930 && !DECL_VINDEX (decl)));
2932 if (!DECL_NOT_REALLY_EXTERN (decl))
2933 DECL_EXTERNAL (decl) = 1;
2935 /* Always make artificials weak. */
2936 if (DECL_ARTIFICIAL (decl) && flag_weak)
2937 comdat_p = true;
2938 else
2939 maybe_make_one_only (decl);
2942 else
2943 comdat_p = true;
2945 else
2946 comdat_p = true;
2948 if (import_p)
2950 /* If we are importing DECL into this translation unit, mark is
2951 an undefined here. */
2952 DECL_EXTERNAL (decl) = 1;
2953 DECL_NOT_REALLY_EXTERN (decl) = 0;
2955 else if (comdat_p)
2957 /* If we decided to put DECL in COMDAT, mark it accordingly at
2958 this point. */
2959 comdat_linkage (decl);
2962 DECL_INTERFACE_KNOWN (decl) = 1;
2965 /* Return an expression that performs the destruction of DECL, which
2966 must be a VAR_DECL whose type has a non-trivial destructor, or is
2967 an array whose (innermost) elements have a non-trivial destructor. */
2969 tree
2970 build_cleanup (tree decl)
2972 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2973 gcc_assert (clean != NULL_TREE);
2974 return clean;
2977 /* Returns the initialization guard variable for the variable DECL,
2978 which has static storage duration. */
2980 tree
2981 get_guard (tree decl)
2983 tree sname;
2984 tree guard;
2986 sname = mangle_guard_variable (decl);
2987 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2988 if (! guard)
2990 tree guard_type;
2992 /* We use a type that is big enough to contain a mutex as well
2993 as an integer counter. */
2994 guard_type = targetm.cxx.guard_type ();
2995 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2996 VAR_DECL, sname, guard_type);
2998 /* The guard should have the same linkage as what it guards. */
2999 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3000 TREE_STATIC (guard) = TREE_STATIC (decl);
3001 DECL_COMMON (guard) = DECL_COMMON (decl);
3002 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3003 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3004 if (DECL_ONE_ONLY (decl))
3005 make_decl_one_only (guard, cxx_comdat_group (guard));
3006 if (TREE_PUBLIC (decl))
3007 DECL_WEAK (guard) = DECL_WEAK (decl);
3008 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3009 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3011 DECL_ARTIFICIAL (guard) = 1;
3012 DECL_IGNORED_P (guard) = 1;
3013 TREE_USED (guard) = 1;
3014 pushdecl_top_level_and_finish (guard, NULL_TREE);
3016 return guard;
3019 /* Return those bits of the GUARD variable that should be set when the
3020 guarded entity is actually initialized. */
3022 static tree
3023 get_guard_bits (tree guard)
3025 if (!targetm.cxx.guard_mask_bit ())
3027 /* We only set the first byte of the guard, in order to leave room
3028 for a mutex in the high-order bits. */
3029 guard = build1 (ADDR_EXPR,
3030 build_pointer_type (TREE_TYPE (guard)),
3031 guard);
3032 guard = build1 (NOP_EXPR,
3033 build_pointer_type (char_type_node),
3034 guard);
3035 guard = build1 (INDIRECT_REF, char_type_node, guard);
3038 return guard;
3041 /* Return an expression which determines whether or not the GUARD
3042 variable has already been initialized. */
3044 tree
3045 get_guard_cond (tree guard)
3047 tree guard_value;
3049 /* Check to see if the GUARD is zero. */
3050 guard = get_guard_bits (guard);
3052 /* Mask off all but the low bit. */
3053 if (targetm.cxx.guard_mask_bit ())
3055 guard_value = integer_one_node;
3056 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3057 guard_value = convert (TREE_TYPE (guard), guard_value);
3058 guard = cp_build_binary_op (input_location,
3059 BIT_AND_EXPR, guard, guard_value,
3060 tf_warning_or_error);
3063 guard_value = integer_zero_node;
3064 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3065 guard_value = convert (TREE_TYPE (guard), guard_value);
3066 return cp_build_binary_op (input_location,
3067 EQ_EXPR, guard, guard_value,
3068 tf_warning_or_error);
3071 /* Return an expression which sets the GUARD variable, indicating that
3072 the variable being guarded has been initialized. */
3074 tree
3075 set_guard (tree guard)
3077 tree guard_init;
3079 /* Set the GUARD to one. */
3080 guard = get_guard_bits (guard);
3081 guard_init = integer_one_node;
3082 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3083 guard_init = convert (TREE_TYPE (guard), guard_init);
3084 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3085 tf_warning_or_error);
3088 /* Returns true iff we can tell that VAR does not have a dynamic
3089 initializer. */
3091 static bool
3092 var_defined_without_dynamic_init (tree var)
3094 /* If it's defined in another TU, we can't tell. */
3095 if (DECL_EXTERNAL (var))
3096 return false;
3097 /* If it has a non-trivial destructor, registering the destructor
3098 counts as dynamic initialization. */
3099 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3100 return false;
3101 /* If it's in this TU, its initializer has been processed, unless
3102 it's a case of self-initialization, then DECL_INITIALIZED_P is
3103 false while the initializer is handled by finish_id_expression. */
3104 if (!DECL_INITIALIZED_P (var))
3105 return false;
3106 /* If it has no initializer or a constant one, it's not dynamic. */
3107 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3108 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3111 /* Returns true iff VAR is a variable that needs uses to be
3112 wrapped for possible dynamic initialization. */
3114 static bool
3115 var_needs_tls_wrapper (tree var)
3117 return (!error_operand_p (var)
3118 && DECL_THREAD_LOCAL_P (var)
3119 && !DECL_GNU_TLS_P (var)
3120 && !DECL_FUNCTION_SCOPE_P (var)
3121 && !var_defined_without_dynamic_init (var));
3124 /* Get the FUNCTION_DECL for the shared TLS init function for this
3125 translation unit. */
3127 static tree
3128 get_local_tls_init_fn (void)
3130 tree sname = get_identifier ("__tls_init");
3131 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3132 if (!fn)
3134 fn = build_lang_decl (FUNCTION_DECL, sname,
3135 build_function_type (void_type_node,
3136 void_list_node));
3137 SET_DECL_LANGUAGE (fn, lang_c);
3138 TREE_PUBLIC (fn) = false;
3139 DECL_ARTIFICIAL (fn) = true;
3140 mark_used (fn);
3141 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3143 return fn;
3146 /* Get a FUNCTION_DECL for the init function for the thread_local
3147 variable VAR. The init function will be an alias to the function
3148 that initializes all the non-local TLS variables in the translation
3149 unit. The init function is only used by the wrapper function. */
3151 static tree
3152 get_tls_init_fn (tree var)
3154 /* Only C++11 TLS vars need this init fn. */
3155 if (!var_needs_tls_wrapper (var))
3156 return NULL_TREE;
3158 /* If -fno-extern-tls-init, assume that we don't need to call
3159 a tls init function for a variable defined in another TU. */
3160 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3161 return NULL_TREE;
3163 #ifdef ASM_OUTPUT_DEF
3164 /* If the variable is internal, or if we can't generate aliases,
3165 call the local init function directly. */
3166 if (!TREE_PUBLIC (var))
3167 #endif
3168 return get_local_tls_init_fn ();
3170 tree sname = mangle_tls_init_fn (var);
3171 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3172 if (!fn)
3174 fn = build_lang_decl (FUNCTION_DECL, sname,
3175 build_function_type (void_type_node,
3176 void_list_node));
3177 SET_DECL_LANGUAGE (fn, lang_c);
3178 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3179 DECL_ARTIFICIAL (fn) = true;
3180 DECL_COMDAT (fn) = DECL_COMDAT (var);
3181 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3182 if (DECL_ONE_ONLY (var))
3183 make_decl_one_only (fn, cxx_comdat_group (fn));
3184 if (TREE_PUBLIC (var))
3186 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3187 /* If the variable is defined somewhere else and might have static
3188 initialization, make the init function a weak reference. */
3189 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3190 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3191 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3192 && DECL_EXTERNAL (var))
3193 declare_weak (fn);
3194 else
3195 DECL_WEAK (fn) = DECL_WEAK (var);
3197 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3198 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3199 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3200 DECL_IGNORED_P (fn) = 1;
3201 mark_used (fn);
3203 DECL_BEFRIENDING_CLASSES (fn) = var;
3205 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3207 return fn;
3210 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3211 variable VAR. The wrapper function calls the init function (if any) for
3212 VAR and then returns a reference to VAR. The wrapper function is used
3213 in place of VAR everywhere VAR is mentioned. */
3215 tree
3216 get_tls_wrapper_fn (tree var)
3218 /* Only C++11 TLS vars need this wrapper fn. */
3219 if (!var_needs_tls_wrapper (var))
3220 return NULL_TREE;
3222 tree sname = mangle_tls_wrapper_fn (var);
3223 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3224 if (!fn)
3226 /* A named rvalue reference is an lvalue, so the wrapper should
3227 always return an lvalue reference. */
3228 tree type = non_reference (TREE_TYPE (var));
3229 type = build_reference_type (type);
3230 tree fntype = build_function_type (type, void_list_node);
3231 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3232 SET_DECL_LANGUAGE (fn, lang_c);
3233 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3234 DECL_ARTIFICIAL (fn) = true;
3235 DECL_IGNORED_P (fn) = 1;
3236 /* The wrapper is inline and emitted everywhere var is used. */
3237 DECL_DECLARED_INLINE_P (fn) = true;
3238 if (TREE_PUBLIC (var))
3240 comdat_linkage (fn);
3241 #ifdef HAVE_GAS_HIDDEN
3242 /* Make the wrapper bind locally; there's no reason to share
3243 the wrapper between multiple shared objects. */
3244 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3245 DECL_VISIBILITY_SPECIFIED (fn) = true;
3246 #endif
3248 if (!TREE_PUBLIC (fn))
3249 DECL_INTERFACE_KNOWN (fn) = true;
3250 mark_used (fn);
3251 note_vague_linkage_fn (fn);
3253 #if 0
3254 /* We want CSE to commonize calls to the wrapper, but marking it as
3255 pure is unsafe since it has side-effects. I guess we need a new
3256 ECF flag even weaker than ECF_PURE. FIXME! */
3257 DECL_PURE_P (fn) = true;
3258 #endif
3260 DECL_BEFRIENDING_CLASSES (fn) = var;
3262 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3264 return fn;
3267 /* At EOF, generate the definition for the TLS wrapper function FN:
3269 T& var_wrapper() {
3270 if (init_fn) init_fn();
3271 return var;
3272 } */
3274 static void
3275 generate_tls_wrapper (tree fn)
3277 tree var = DECL_BEFRIENDING_CLASSES (fn);
3279 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3280 tree body = begin_function_body ();
3281 /* Only call the init fn if there might be one. */
3282 if (tree init_fn = get_tls_init_fn (var))
3284 tree if_stmt = NULL_TREE;
3285 /* If init_fn is a weakref, make sure it exists before calling. */
3286 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3288 if_stmt = begin_if_stmt ();
3289 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3290 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3291 NE_EXPR, addr, nullptr_node,
3292 tf_warning_or_error);
3293 finish_if_stmt_cond (cond, if_stmt);
3295 finish_expr_stmt (build_cxx_call
3296 (init_fn, 0, NULL, tf_warning_or_error));
3297 if (if_stmt)
3299 finish_then_clause (if_stmt);
3300 finish_if_stmt (if_stmt);
3303 else
3304 /* If there's no initialization, the wrapper is a constant function. */
3305 TREE_READONLY (fn) = true;
3306 finish_return_stmt (convert_from_reference (var));
3307 finish_function_body (body);
3308 expand_or_defer_fn (finish_function (0));
3311 /* Start the process of running a particular set of global constructors
3312 or destructors. Subroutine of do_[cd]tors. Also called from
3313 vtv_start_verification_constructor_init_function. */
3315 static tree
3316 start_objects (int method_type, int initp)
3318 tree body;
3319 tree fndecl;
3320 char type[14];
3322 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3324 if (initp != DEFAULT_INIT_PRIORITY)
3326 char joiner;
3328 #ifdef JOINER
3329 joiner = JOINER;
3330 #else
3331 joiner = '_';
3332 #endif
3334 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3336 else
3337 sprintf (type, "sub_%c", method_type);
3339 fndecl = build_lang_decl (FUNCTION_DECL,
3340 get_file_function_name (type),
3341 build_function_type_list (void_type_node,
3342 NULL_TREE));
3343 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3345 TREE_PUBLIC (current_function_decl) = 0;
3347 /* Mark as artificial because it's not explicitly in the user's
3348 source code. */
3349 DECL_ARTIFICIAL (current_function_decl) = 1;
3351 /* Mark this declaration as used to avoid spurious warnings. */
3352 TREE_USED (current_function_decl) = 1;
3354 /* Mark this function as a global constructor or destructor. */
3355 if (method_type == 'I')
3356 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3357 else
3358 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3360 body = begin_compound_stmt (BCS_FN_BODY);
3362 return body;
3365 /* Finish the process of running a particular set of global constructors
3366 or destructors. Subroutine of do_[cd]tors. */
3368 static void
3369 finish_objects (int method_type, int initp, tree body)
3371 tree fn;
3373 /* Finish up. */
3374 finish_compound_stmt (body);
3375 fn = finish_function (0);
3377 if (method_type == 'I')
3379 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3380 decl_init_priority_insert (fn, initp);
3382 else
3384 DECL_STATIC_DESTRUCTOR (fn) = 1;
3385 decl_fini_priority_insert (fn, initp);
3388 expand_or_defer_fn (fn);
3391 /* The names of the parameters to the function created to handle
3392 initializations and destructions for objects with static storage
3393 duration. */
3394 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3395 #define PRIORITY_IDENTIFIER "__priority"
3397 /* The name of the function we create to handle initializations and
3398 destructions for objects with static storage duration. */
3399 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3401 /* The declaration for the __INITIALIZE_P argument. */
3402 static GTY(()) tree initialize_p_decl;
3404 /* The declaration for the __PRIORITY argument. */
3405 static GTY(()) tree priority_decl;
3407 /* The declaration for the static storage duration function. */
3408 static GTY(()) tree ssdf_decl;
3410 /* All the static storage duration functions created in this
3411 translation unit. */
3412 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3414 /* A map from priority levels to information about that priority
3415 level. There may be many such levels, so efficient lookup is
3416 important. */
3417 static splay_tree priority_info_map;
3419 /* Begins the generation of the function that will handle all
3420 initialization and destruction of objects with static storage
3421 duration. The function generated takes two parameters of type
3422 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3423 nonzero, it performs initializations. Otherwise, it performs
3424 destructions. It only performs those initializations or
3425 destructions with the indicated __PRIORITY. The generated function
3426 returns no value.
3428 It is assumed that this function will only be called once per
3429 translation unit. */
3431 static tree
3432 start_static_storage_duration_function (unsigned count)
3434 tree type;
3435 tree body;
3436 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3438 /* Create the identifier for this function. It will be of the form
3439 SSDF_IDENTIFIER_<number>. */
3440 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3442 type = build_function_type_list (void_type_node,
3443 integer_type_node, integer_type_node,
3444 NULL_TREE);
3446 /* Create the FUNCTION_DECL itself. */
3447 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3448 get_identifier (id),
3449 type);
3450 TREE_PUBLIC (ssdf_decl) = 0;
3451 DECL_ARTIFICIAL (ssdf_decl) = 1;
3453 /* Put this function in the list of functions to be called from the
3454 static constructors and destructors. */
3455 if (!ssdf_decls)
3457 vec_alloc (ssdf_decls, 32);
3459 /* Take this opportunity to initialize the map from priority
3460 numbers to information about that priority level. */
3461 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3462 /*delete_key_fn=*/0,
3463 /*delete_value_fn=*/
3464 (splay_tree_delete_value_fn) &free);
3466 /* We always need to generate functions for the
3467 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3468 priorities later, we'll be sure to find the
3469 DEFAULT_INIT_PRIORITY. */
3470 get_priority_info (DEFAULT_INIT_PRIORITY);
3473 vec_safe_push (ssdf_decls, ssdf_decl);
3475 /* Create the argument list. */
3476 initialize_p_decl = cp_build_parm_decl
3477 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3478 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3479 TREE_USED (initialize_p_decl) = 1;
3480 priority_decl = cp_build_parm_decl
3481 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3482 DECL_CONTEXT (priority_decl) = ssdf_decl;
3483 TREE_USED (priority_decl) = 1;
3485 DECL_CHAIN (initialize_p_decl) = priority_decl;
3486 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3488 /* Put the function in the global scope. */
3489 pushdecl (ssdf_decl);
3491 /* Start the function itself. This is equivalent to declaring the
3492 function as:
3494 static void __ssdf (int __initialize_p, init __priority_p);
3496 It is static because we only need to call this function from the
3497 various constructor and destructor functions for this module. */
3498 start_preparsed_function (ssdf_decl,
3499 /*attrs=*/NULL_TREE,
3500 SF_PRE_PARSED);
3502 /* Set up the scope of the outermost block in the function. */
3503 body = begin_compound_stmt (BCS_FN_BODY);
3505 return body;
3508 /* Finish the generation of the function which performs initialization
3509 and destruction of objects with static storage duration. After
3510 this point, no more such objects can be created. */
3512 static void
3513 finish_static_storage_duration_function (tree body)
3515 /* Close out the function. */
3516 finish_compound_stmt (body);
3517 expand_or_defer_fn (finish_function (0));
3520 /* Return the information about the indicated PRIORITY level. If no
3521 code to handle this level has yet been generated, generate the
3522 appropriate prologue. */
3524 static priority_info
3525 get_priority_info (int priority)
3527 priority_info pi;
3528 splay_tree_node n;
3530 n = splay_tree_lookup (priority_info_map,
3531 (splay_tree_key) priority);
3532 if (!n)
3534 /* Create a new priority information structure, and insert it
3535 into the map. */
3536 pi = XNEW (struct priority_info_s);
3537 pi->initializations_p = 0;
3538 pi->destructions_p = 0;
3539 splay_tree_insert (priority_info_map,
3540 (splay_tree_key) priority,
3541 (splay_tree_value) pi);
3543 else
3544 pi = (priority_info) n->value;
3546 return pi;
3549 /* The effective initialization priority of a DECL. */
3551 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3552 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3553 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3555 /* Whether a DECL needs a guard to protect it against multiple
3556 initialization. */
3558 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3559 || DECL_ONE_ONLY (decl) \
3560 || DECL_WEAK (decl)))
3562 /* Called from one_static_initialization_or_destruction(),
3563 via walk_tree.
3564 Walks the initializer list of a global variable and looks for
3565 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3566 and that have their DECL_CONTEXT() == NULL.
3567 For each such temporary variable, set their DECL_CONTEXT() to
3568 the current function. This is necessary because otherwise
3569 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3570 when trying to refer to a temporary variable that does not have
3571 it's DECL_CONTECT() properly set. */
3572 static tree
3573 fix_temporary_vars_context_r (tree *node,
3574 int * /*unused*/,
3575 void * /*unused1*/)
3577 gcc_assert (current_function_decl);
3579 if (TREE_CODE (*node) == BIND_EXPR)
3581 tree var;
3583 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3584 if (VAR_P (var)
3585 && !DECL_NAME (var)
3586 && DECL_ARTIFICIAL (var)
3587 && !DECL_CONTEXT (var))
3588 DECL_CONTEXT (var) = current_function_decl;
3591 return NULL_TREE;
3594 /* Set up to handle the initialization or destruction of DECL. If
3595 INITP is nonzero, we are initializing the variable. Otherwise, we
3596 are destroying it. */
3598 static void
3599 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3601 tree guard_if_stmt = NULL_TREE;
3602 tree guard;
3604 /* If we are supposed to destruct and there's a trivial destructor,
3605 nothing has to be done. */
3606 if (!initp
3607 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3608 return;
3610 /* Trick the compiler into thinking we are at the file and line
3611 where DECL was declared so that error-messages make sense, and so
3612 that the debugger will show somewhat sensible file and line
3613 information. */
3614 input_location = DECL_SOURCE_LOCATION (decl);
3616 /* Make sure temporary variables in the initialiser all have
3617 their DECL_CONTEXT() set to a value different from NULL_TREE.
3618 This can happen when global variables initialisers are built.
3619 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3620 the temporary variables that might have been generated in the
3621 accompagning initialisers is NULL_TREE, meaning the variables have been
3622 declared in the global namespace.
3623 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3624 of the temporaries are set to the current function decl. */
3625 cp_walk_tree_without_duplicates (&init,
3626 fix_temporary_vars_context_r,
3627 NULL);
3629 /* Because of:
3631 [class.access.spec]
3633 Access control for implicit calls to the constructors,
3634 the conversion functions, or the destructor called to
3635 create and destroy a static data member is performed as
3636 if these calls appeared in the scope of the member's
3637 class.
3639 we pretend we are in a static member function of the class of
3640 which the DECL is a member. */
3641 if (member_p (decl))
3643 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3644 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3647 /* Assume we don't need a guard. */
3648 guard = NULL_TREE;
3649 /* We need a guard if this is an object with external linkage that
3650 might be initialized in more than one place. (For example, a
3651 static data member of a template, when the data member requires
3652 construction.) */
3653 if (NEEDS_GUARD_P (decl))
3655 tree guard_cond;
3657 guard = get_guard (decl);
3659 /* When using __cxa_atexit, we just check the GUARD as we would
3660 for a local static. */
3661 if (flag_use_cxa_atexit)
3663 /* When using __cxa_atexit, we never try to destroy
3664 anything from a static destructor. */
3665 gcc_assert (initp);
3666 guard_cond = get_guard_cond (guard);
3668 /* If we don't have __cxa_atexit, then we will be running
3669 destructors from .fini sections, or their equivalents. So,
3670 we need to know how many times we've tried to initialize this
3671 object. We do initializations only if the GUARD is zero,
3672 i.e., if we are the first to initialize the variable. We do
3673 destructions only if the GUARD is one, i.e., if we are the
3674 last to destroy the variable. */
3675 else if (initp)
3676 guard_cond
3677 = cp_build_binary_op (input_location,
3678 EQ_EXPR,
3679 cp_build_unary_op (PREINCREMENT_EXPR,
3680 guard,
3681 /*noconvert=*/1,
3682 tf_warning_or_error),
3683 integer_one_node,
3684 tf_warning_or_error);
3685 else
3686 guard_cond
3687 = cp_build_binary_op (input_location,
3688 EQ_EXPR,
3689 cp_build_unary_op (PREDECREMENT_EXPR,
3690 guard,
3691 /*noconvert=*/1,
3692 tf_warning_or_error),
3693 integer_zero_node,
3694 tf_warning_or_error);
3696 guard_if_stmt = begin_if_stmt ();
3697 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3701 /* If we're using __cxa_atexit, we have not already set the GUARD,
3702 so we must do so now. */
3703 if (guard && initp && flag_use_cxa_atexit)
3704 finish_expr_stmt (set_guard (guard));
3706 /* Perform the initialization or destruction. */
3707 if (initp)
3709 if (init)
3711 finish_expr_stmt (init);
3712 if (flag_sanitize & SANITIZE_ADDRESS)
3714 varpool_node *vnode = varpool_node::get (decl);
3715 if (vnode)
3716 vnode->dynamically_initialized = 1;
3720 /* If we're using __cxa_atexit, register a function that calls the
3721 destructor for the object. */
3722 if (flag_use_cxa_atexit)
3723 finish_expr_stmt (register_dtor_fn (decl));
3725 else
3726 finish_expr_stmt (build_cleanup (decl));
3728 /* Finish the guard if-stmt, if necessary. */
3729 if (guard)
3731 finish_then_clause (guard_if_stmt);
3732 finish_if_stmt (guard_if_stmt);
3735 /* Now that we're done with DECL we don't need to pretend to be a
3736 member of its class any longer. */
3737 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3738 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3741 /* Generate code to do the initialization or destruction of the decls in VARS,
3742 a TREE_LIST of VAR_DECL with static storage duration.
3743 Whether initialization or destruction is performed is specified by INITP. */
3745 static void
3746 do_static_initialization_or_destruction (tree vars, bool initp)
3748 tree node, init_if_stmt, cond;
3750 /* Build the outer if-stmt to check for initialization or destruction. */
3751 init_if_stmt = begin_if_stmt ();
3752 cond = initp ? integer_one_node : integer_zero_node;
3753 cond = cp_build_binary_op (input_location,
3754 EQ_EXPR,
3755 initialize_p_decl,
3756 cond,
3757 tf_warning_or_error);
3758 finish_if_stmt_cond (cond, init_if_stmt);
3760 /* To make sure dynamic construction doesn't access globals from other
3761 compilation units where they might not be yet constructed, for
3762 -fsanitize=address insert __asan_before_dynamic_init call that
3763 prevents access to either all global variables that need construction
3764 in other compilation units, or at least those that haven't been
3765 initialized yet. Variables that need dynamic construction in
3766 the current compilation unit are kept accessible. */
3767 if (flag_sanitize & SANITIZE_ADDRESS)
3768 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3770 node = vars;
3771 do {
3772 tree decl = TREE_VALUE (node);
3773 tree priority_if_stmt;
3774 int priority;
3775 priority_info pi;
3777 /* If we don't need a destructor, there's nothing to do. Avoid
3778 creating a possibly empty if-stmt. */
3779 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3781 node = TREE_CHAIN (node);
3782 continue;
3785 /* Remember that we had an initialization or finalization at this
3786 priority. */
3787 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3788 pi = get_priority_info (priority);
3789 if (initp)
3790 pi->initializations_p = 1;
3791 else
3792 pi->destructions_p = 1;
3794 /* Conditionalize this initialization on being in the right priority
3795 and being initializing/finalizing appropriately. */
3796 priority_if_stmt = begin_if_stmt ();
3797 cond = cp_build_binary_op (input_location,
3798 EQ_EXPR,
3799 priority_decl,
3800 build_int_cst (NULL_TREE, priority),
3801 tf_warning_or_error);
3802 finish_if_stmt_cond (cond, priority_if_stmt);
3804 /* Process initializers with same priority. */
3805 for (; node
3806 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3807 node = TREE_CHAIN (node))
3808 /* Do one initialization or destruction. */
3809 one_static_initialization_or_destruction (TREE_VALUE (node),
3810 TREE_PURPOSE (node), initp);
3812 /* Finish up the priority if-stmt body. */
3813 finish_then_clause (priority_if_stmt);
3814 finish_if_stmt (priority_if_stmt);
3816 } while (node);
3818 /* Revert what __asan_before_dynamic_init did by calling
3819 __asan_after_dynamic_init. */
3820 if (flag_sanitize & SANITIZE_ADDRESS)
3821 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3823 /* Finish up the init/destruct if-stmt body. */
3824 finish_then_clause (init_if_stmt);
3825 finish_if_stmt (init_if_stmt);
3828 /* VARS is a list of variables with static storage duration which may
3829 need initialization and/or finalization. Remove those variables
3830 that don't really need to be initialized or finalized, and return
3831 the resulting list. The order in which the variables appear in
3832 VARS is in reverse order of the order in which they should actually
3833 be initialized. The list we return is in the unreversed order;
3834 i.e., the first variable should be initialized first. */
3836 static tree
3837 prune_vars_needing_no_initialization (tree *vars)
3839 tree *var = vars;
3840 tree result = NULL_TREE;
3842 while (*var)
3844 tree t = *var;
3845 tree decl = TREE_VALUE (t);
3846 tree init = TREE_PURPOSE (t);
3848 /* Deal gracefully with error. */
3849 if (decl == error_mark_node)
3851 var = &TREE_CHAIN (t);
3852 continue;
3855 /* The only things that can be initialized are variables. */
3856 gcc_assert (VAR_P (decl));
3858 /* If this object is not defined, we don't need to do anything
3859 here. */
3860 if (DECL_EXTERNAL (decl))
3862 var = &TREE_CHAIN (t);
3863 continue;
3866 /* Also, if the initializer already contains errors, we can bail
3867 out now. */
3868 if (init && TREE_CODE (init) == TREE_LIST
3869 && value_member (error_mark_node, init))
3871 var = &TREE_CHAIN (t);
3872 continue;
3875 /* This variable is going to need initialization and/or
3876 finalization, so we add it to the list. */
3877 *var = TREE_CHAIN (t);
3878 TREE_CHAIN (t) = result;
3879 result = t;
3882 return result;
3885 /* Make sure we have told the back end about all the variables in
3886 VARS. */
3888 static void
3889 write_out_vars (tree vars)
3891 tree v;
3893 for (v = vars; v; v = TREE_CHAIN (v))
3895 tree var = TREE_VALUE (v);
3896 if (!var_finalized_p (var))
3898 import_export_decl (var);
3899 rest_of_decl_compilation (var, 1, 1);
3904 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3905 (otherwise) that will initialize all global objects with static
3906 storage duration having the indicated PRIORITY. */
3908 static void
3909 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3910 location_t *locus)
3912 char function_key;
3913 tree fndecl;
3914 tree body;
3915 size_t i;
3917 input_location = *locus;
3918 /* ??? */
3919 /* Was: locus->line++; */
3921 /* We use `I' to indicate initialization and `D' to indicate
3922 destruction. */
3923 function_key = constructor_p ? 'I' : 'D';
3925 /* We emit the function lazily, to avoid generating empty
3926 global constructors and destructors. */
3927 body = NULL_TREE;
3929 /* For Objective-C++, we may need to initialize metadata found in this module.
3930 This must be done _before_ any other static initializations. */
3931 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3932 && constructor_p && objc_static_init_needed_p ())
3934 body = start_objects (function_key, priority);
3935 objc_generate_static_init_call (NULL_TREE);
3938 /* Call the static storage duration function with appropriate
3939 arguments. */
3940 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3942 /* Calls to pure or const functions will expand to nothing. */
3943 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3945 tree call;
3947 if (! body)
3948 body = start_objects (function_key, priority);
3950 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3951 build_int_cst (NULL_TREE,
3952 constructor_p),
3953 build_int_cst (NULL_TREE,
3954 priority),
3955 NULL_TREE);
3956 finish_expr_stmt (call);
3960 /* Close out the function. */
3961 if (body)
3962 finish_objects (function_key, priority, body);
3965 /* Generate constructor and destructor functions for the priority
3966 indicated by N. */
3968 static int
3969 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3971 location_t *locus = (location_t *) data;
3972 int priority = (int) n->key;
3973 priority_info pi = (priority_info) n->value;
3975 /* Generate the functions themselves, but only if they are really
3976 needed. */
3977 if (pi->initializations_p)
3978 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3979 if (pi->destructions_p)
3980 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3982 /* Keep iterating. */
3983 return 0;
3986 /* Java requires that we be able to reference a local address for a
3987 method, and not be confused by PLT entries. If hidden aliases are
3988 supported, collect and return all the functions for which we should
3989 emit a hidden alias. */
3991 static hash_set<tree> *
3992 collect_candidates_for_java_method_aliases (void)
3994 struct cgraph_node *node;
3995 hash_set<tree> *candidates = NULL;
3997 #ifndef HAVE_GAS_HIDDEN
3998 return candidates;
3999 #endif
4001 FOR_EACH_FUNCTION (node)
4003 tree fndecl = node->decl;
4005 if (DECL_CLASS_SCOPE_P (fndecl)
4006 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4007 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4009 if (candidates == NULL)
4010 candidates = new hash_set<tree>;
4011 candidates->add (fndecl);
4015 return candidates;
4019 /* Java requires that we be able to reference a local address for a
4020 method, and not be confused by PLT entries. If hidden aliases are
4021 supported, emit one for each java function that we've emitted.
4022 CANDIDATES is the set of FUNCTION_DECLs that were gathered
4023 by collect_candidates_for_java_method_aliases. */
4025 static void
4026 build_java_method_aliases (hash_set<tree> *candidates)
4028 struct cgraph_node *node;
4030 #ifndef HAVE_GAS_HIDDEN
4031 return;
4032 #endif
4034 FOR_EACH_FUNCTION (node)
4036 tree fndecl = node->decl;
4038 if (TREE_ASM_WRITTEN (fndecl)
4039 && candidates->contains (fndecl))
4041 /* Mangle the name in a predictable way; we need to reference
4042 this from a java compiled object file. */
4043 tree oid, nid, alias;
4044 const char *oname;
4045 char *nname;
4047 oid = DECL_ASSEMBLER_NAME (fndecl);
4048 oname = IDENTIFIER_POINTER (oid);
4049 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4050 nname = ACONCAT (("_ZGA", oname+2, NULL));
4051 nid = get_identifier (nname);
4053 alias = make_alias_for (fndecl, nid);
4054 TREE_PUBLIC (alias) = 1;
4055 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4057 assemble_alias (alias, oid);
4062 /* Return C++ property of T, based on given operation OP. */
4064 static int
4065 cpp_check (tree t, cpp_operation op)
4067 switch (op)
4069 case IS_ABSTRACT:
4070 return DECL_PURE_VIRTUAL_P (t);
4071 case IS_CONSTRUCTOR:
4072 return DECL_CONSTRUCTOR_P (t);
4073 case IS_DESTRUCTOR:
4074 return DECL_DESTRUCTOR_P (t);
4075 case IS_COPY_CONSTRUCTOR:
4076 return DECL_COPY_CONSTRUCTOR_P (t);
4077 case IS_TEMPLATE:
4078 return TREE_CODE (t) == TEMPLATE_DECL;
4079 case IS_TRIVIAL:
4080 return trivial_type_p (t);
4081 default:
4082 return 0;
4086 /* Collect source file references recursively, starting from NAMESPC. */
4088 static void
4089 collect_source_refs (tree namespc)
4091 tree t;
4093 if (!namespc)
4094 return;
4096 /* Iterate over names in this name space. */
4097 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4098 if (!DECL_IS_BUILTIN (t) )
4099 collect_source_ref (DECL_SOURCE_FILE (t));
4101 /* Dump siblings, if any */
4102 collect_source_refs (TREE_CHAIN (namespc));
4104 /* Dump children, if any */
4105 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4108 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4109 starting from NAMESPC. */
4111 static void
4112 collect_ada_namespace (tree namespc, const char *source_file)
4114 if (!namespc)
4115 return;
4117 /* Collect decls from this namespace */
4118 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4120 /* Collect siblings, if any */
4121 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4123 /* Collect children, if any */
4124 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4127 /* Returns true iff there is a definition available for variable or
4128 function DECL. */
4130 static bool
4131 decl_defined_p (tree decl)
4133 if (TREE_CODE (decl) == FUNCTION_DECL)
4134 return (DECL_INITIAL (decl) != NULL_TREE);
4135 else
4137 gcc_assert (VAR_P (decl));
4138 return !DECL_EXTERNAL (decl);
4142 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4144 [expr.const]
4146 An integral constant-expression can only involve ... const
4147 variables of integral or enumeration types initialized with
4148 constant expressions ...
4150 C++0x also allows constexpr variables and temporaries initialized
4151 with constant expressions. We handle the former here, but the latter
4152 are just folded away in cxx_eval_constant_expression.
4154 The standard does not require that the expression be non-volatile.
4155 G++ implements the proposed correction in DR 457. */
4157 bool
4158 decl_constant_var_p (tree decl)
4160 if (!decl_maybe_constant_var_p (decl))
4161 return false;
4163 /* We don't know if a template static data member is initialized with
4164 a constant expression until we instantiate its initializer. Even
4165 in the case of a constexpr variable, we can't treat it as a
4166 constant until its initializer is complete in case it's used in
4167 its own initializer. */
4168 mark_used (decl);
4169 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4172 /* Returns true if DECL could be a symbolic constant variable, depending on
4173 its initializer. */
4175 bool
4176 decl_maybe_constant_var_p (tree decl)
4178 tree type = TREE_TYPE (decl);
4179 if (!VAR_P (decl))
4180 return false;
4181 if (DECL_DECLARED_CONSTEXPR_P (decl))
4182 return true;
4183 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4184 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4187 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4188 called from grokfndecl and grokvardecl; in all modes it is called from
4189 cp_write_global_declarations. */
4191 void
4192 no_linkage_error (tree decl)
4194 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4195 /* In C++11 it's ok if the decl is defined. */
4196 return;
4197 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4198 if (t == NULL_TREE)
4199 /* The type that got us on no_linkage_decls must have gotten a name for
4200 linkage purposes. */;
4201 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4202 /* The type might end up having a typedef name for linkage purposes. */
4203 vec_safe_push (no_linkage_decls, decl);
4204 else if (TYPE_ANONYMOUS_P (t))
4206 bool d = false;
4207 if (cxx_dialect >= cxx11)
4208 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4209 "anonymous type, is used but never defined", decl);
4210 else if (DECL_EXTERN_C_P (decl))
4211 /* Allow this; it's pretty common in C. */;
4212 else if (TREE_CODE (decl) == VAR_DECL)
4213 /* DRs 132, 319 and 389 seem to indicate types with
4214 no linkage can only be used to declare extern "C"
4215 entities. Since it's not always an error in the
4216 ISO C++ 90 Standard, we only issue a warning. */
4217 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4218 "with no linkage used to declare variable %q#D with "
4219 "linkage", decl);
4220 else
4221 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4222 "linkage used to declare function %q#D with linkage",
4223 decl);
4224 if (d && is_typedef_decl (TYPE_NAME (t)))
4225 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4226 "to the unqualified type, so it is not used for linkage",
4227 TYPE_NAME (t));
4229 else if (cxx_dialect >= cxx11)
4230 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4231 "%qT, is used but never defined", decl, t);
4232 else if (TREE_CODE (decl) == VAR_DECL)
4233 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4234 "used to declare variable %q#D with linkage", t, decl);
4235 else
4236 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4237 "to declare function %q#D with linkage", t, decl);
4240 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4242 static void
4243 collect_all_refs (const char *source_file)
4245 collect_ada_namespace (global_namespace, source_file);
4248 /* Clear DECL_EXTERNAL for NODE. */
4250 static bool
4251 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4253 DECL_EXTERNAL (node->decl) = 0;
4254 return false;
4257 /* Build up the function to run dynamic initializers for thread_local
4258 variables in this translation unit and alias the init functions for the
4259 individual variables to it. */
4261 static void
4262 handle_tls_init (void)
4264 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4265 if (vars == NULL_TREE)
4266 return;
4268 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4270 write_out_vars (vars);
4272 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4273 boolean_type_node);
4274 TREE_PUBLIC (guard) = false;
4275 TREE_STATIC (guard) = true;
4276 DECL_ARTIFICIAL (guard) = true;
4277 DECL_IGNORED_P (guard) = true;
4278 TREE_USED (guard) = true;
4279 set_decl_tls_model (guard, decl_default_tls_model (guard));
4280 pushdecl_top_level_and_finish (guard, NULL_TREE);
4282 tree fn = get_local_tls_init_fn ();
4283 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4284 tree body = begin_function_body ();
4285 tree if_stmt = begin_if_stmt ();
4286 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4287 tf_warning_or_error);
4288 finish_if_stmt_cond (cond, if_stmt);
4289 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4290 tf_warning_or_error));
4291 for (; vars; vars = TREE_CHAIN (vars))
4293 tree var = TREE_VALUE (vars);
4294 tree init = TREE_PURPOSE (vars);
4295 one_static_initialization_or_destruction (var, init, true);
4297 #ifdef ASM_OUTPUT_DEF
4298 /* Output init aliases even with -fno-extern-tls-init. */
4299 if (TREE_PUBLIC (var))
4301 tree single_init_fn = get_tls_init_fn (var);
4302 if (single_init_fn == NULL_TREE)
4303 continue;
4304 cgraph_node *alias
4305 = cgraph_node::get_create (fn)->create_same_body_alias
4306 (single_init_fn, fn);
4307 gcc_assert (alias != NULL);
4309 #endif
4312 finish_then_clause (if_stmt);
4313 finish_if_stmt (if_stmt);
4314 finish_function_body (body);
4315 expand_or_defer_fn (finish_function (0));
4318 /* The entire file is now complete. If requested, dump everything
4319 to a file. */
4321 static void
4322 dump_tu (void)
4324 int flags;
4325 FILE *stream = dump_begin (TDI_tu, &flags);
4327 if (stream)
4329 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4330 dump_end (TDI_tu, stream);
4334 /* Check the deallocation functions for CODE to see if we want to warn that
4335 only one was defined. */
4337 static void
4338 maybe_warn_sized_delete (enum tree_code code)
4340 tree sized = NULL_TREE;
4341 tree unsized = NULL_TREE;
4343 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4344 ovl; ovl = OVL_NEXT (ovl))
4346 tree fn = OVL_CURRENT (ovl);
4347 /* We're only interested in usual deallocation functions. */
4348 if (!non_placement_deallocation_fn_p (fn))
4349 continue;
4350 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4351 unsized = fn;
4352 else
4353 sized = fn;
4355 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4356 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4357 "the program should also define %qD", sized);
4358 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4359 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4360 "the program should also define %qD", unsized);
4363 /* Check the global deallocation functions to see if we want to warn about
4364 defining unsized without sized (or vice versa). */
4366 static void
4367 maybe_warn_sized_delete ()
4369 if (!flag_sized_deallocation || !warn_sized_deallocation)
4370 return;
4371 maybe_warn_sized_delete (DELETE_EXPR);
4372 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4375 /* This routine is called at the end of compilation.
4376 Its job is to create all the code needed to initialize and
4377 destroy the global aggregates. We do the destruction
4378 first, since that way we only need to reverse the decls once. */
4380 void
4381 cp_write_global_declarations (void)
4383 tree vars;
4384 bool reconsider;
4385 size_t i;
4386 location_t locus;
4387 unsigned ssdf_count = 0;
4388 int retries = 0;
4389 tree decl;
4390 hash_set<tree> *candidates;
4392 locus = input_location;
4393 at_eof = 1;
4395 /* Bad parse errors. Just forget about it. */
4396 if (! global_bindings_p () || current_class_type
4397 || !vec_safe_is_empty (decl_namespace_list))
4398 return;
4400 /* This is the point to write out a PCH if we're doing that.
4401 In that case we do not want to do anything else. */
4402 if (pch_file)
4404 c_common_write_pch ();
4405 dump_tu ();
4406 return;
4409 symtab->process_same_body_aliases ();
4411 /* Handle -fdump-ada-spec[-slim] */
4412 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4414 if (flag_dump_ada_spec_slim)
4415 collect_source_ref (main_input_filename);
4416 else
4417 collect_source_refs (global_namespace);
4419 dump_ada_specs (collect_all_refs, cpp_check);
4422 /* FIXME - huh? was input_line -= 1;*/
4424 timevar_start (TV_PHASE_DEFERRED);
4426 /* We now have to write out all the stuff we put off writing out.
4427 These include:
4429 o Template specializations that we have not yet instantiated,
4430 but which are needed.
4431 o Initialization and destruction for non-local objects with
4432 static storage duration. (Local objects with static storage
4433 duration are initialized when their scope is first entered,
4434 and are cleaned up via atexit.)
4435 o Virtual function tables.
4437 All of these may cause others to be needed. For example,
4438 instantiating one function may cause another to be needed, and
4439 generating the initializer for an object may cause templates to be
4440 instantiated, etc., etc. */
4442 emit_support_tinfos ();
4446 tree t;
4447 tree decl;
4449 reconsider = false;
4451 /* If there are templates that we've put off instantiating, do
4452 them now. */
4453 instantiate_pending_templates (retries);
4454 ggc_collect ();
4456 /* Write out virtual tables as required. Note that writing out
4457 the virtual table for a template class may cause the
4458 instantiation of members of that class. If we write out
4459 vtables then we remove the class from our list so we don't
4460 have to look at it again. */
4462 while (keyed_classes != NULL_TREE
4463 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4465 reconsider = true;
4466 keyed_classes = TREE_CHAIN (keyed_classes);
4469 t = keyed_classes;
4470 if (t != NULL_TREE)
4472 tree next = TREE_CHAIN (t);
4474 while (next)
4476 if (maybe_emit_vtables (TREE_VALUE (next)))
4478 reconsider = true;
4479 TREE_CHAIN (t) = TREE_CHAIN (next);
4481 else
4482 t = next;
4484 next = TREE_CHAIN (t);
4488 /* Write out needed type info variables. We have to be careful
4489 looping through unemitted decls, because emit_tinfo_decl may
4490 cause other variables to be needed. New elements will be
4491 appended, and we remove from the vector those that actually
4492 get emitted. */
4493 for (i = unemitted_tinfo_decls->length ();
4494 unemitted_tinfo_decls->iterate (--i, &t);)
4495 if (emit_tinfo_decl (t))
4497 reconsider = true;
4498 unemitted_tinfo_decls->unordered_remove (i);
4501 /* The list of objects with static storage duration is built up
4502 in reverse order. We clear STATIC_AGGREGATES so that any new
4503 aggregates added during the initialization of these will be
4504 initialized in the correct order when we next come around the
4505 loop. */
4506 vars = prune_vars_needing_no_initialization (&static_aggregates);
4508 if (vars)
4510 /* We need to start a new initialization function each time
4511 through the loop. That's because we need to know which
4512 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4513 isn't computed until a function is finished, and written
4514 out. That's a deficiency in the back end. When this is
4515 fixed, these initialization functions could all become
4516 inline, with resulting performance improvements. */
4517 tree ssdf_body;
4519 /* Set the line and file, so that it is obviously not from
4520 the source file. */
4521 input_location = locus;
4522 ssdf_body = start_static_storage_duration_function (ssdf_count);
4524 /* Make sure the back end knows about all the variables. */
4525 write_out_vars (vars);
4527 /* First generate code to do all the initializations. */
4528 if (vars)
4529 do_static_initialization_or_destruction (vars, /*initp=*/true);
4531 /* Then, generate code to do all the destructions. Do these
4532 in reverse order so that the most recently constructed
4533 variable is the first destroyed. If we're using
4534 __cxa_atexit, then we don't need to do this; functions
4535 were registered at initialization time to destroy the
4536 local statics. */
4537 if (!flag_use_cxa_atexit && vars)
4539 vars = nreverse (vars);
4540 do_static_initialization_or_destruction (vars, /*initp=*/false);
4542 else
4543 vars = NULL_TREE;
4545 /* Finish up the static storage duration function for this
4546 round. */
4547 input_location = locus;
4548 finish_static_storage_duration_function (ssdf_body);
4550 /* All those initializations and finalizations might cause
4551 us to need more inline functions, more template
4552 instantiations, etc. */
4553 reconsider = true;
4554 ssdf_count++;
4555 /* ??? was: locus.line++; */
4558 /* Now do the same for thread_local variables. */
4559 handle_tls_init ();
4561 /* Go through the set of inline functions whose bodies have not
4562 been emitted yet. If out-of-line copies of these functions
4563 are required, emit them. */
4564 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4566 /* Does it need synthesizing? */
4567 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4568 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4570 /* Even though we're already at the top-level, we push
4571 there again. That way, when we pop back a few lines
4572 hence, all of our state is restored. Otherwise,
4573 finish_function doesn't clean things up, and we end
4574 up with CURRENT_FUNCTION_DECL set. */
4575 push_to_top_level ();
4576 /* The decl's location will mark where it was first
4577 needed. Save that so synthesize method can indicate
4578 where it was needed from, in case of error */
4579 input_location = DECL_SOURCE_LOCATION (decl);
4580 synthesize_method (decl);
4581 pop_from_top_level ();
4582 reconsider = true;
4585 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4586 generate_tls_wrapper (decl);
4588 if (!DECL_SAVED_TREE (decl))
4589 continue;
4591 /* We lie to the back end, pretending that some functions
4592 are not defined when they really are. This keeps these
4593 functions from being put out unnecessarily. But, we must
4594 stop lying when the functions are referenced, or if they
4595 are not comdat since they need to be put out now. If
4596 DECL_INTERFACE_KNOWN, then we have already set
4597 DECL_EXTERNAL appropriately, so there's no need to check
4598 again, and we do not want to clear DECL_EXTERNAL if a
4599 previous call to import_export_decl set it.
4601 This is done in a separate for cycle, because if some
4602 deferred function is contained in another deferred
4603 function later in deferred_fns varray,
4604 rest_of_compilation would skip this function and we
4605 really cannot expand the same function twice. */
4606 import_export_decl (decl);
4607 if (DECL_NOT_REALLY_EXTERN (decl)
4608 && DECL_INITIAL (decl)
4609 && decl_needed_p (decl))
4611 struct cgraph_node *node, *next;
4613 node = cgraph_node::get (decl);
4614 if (node->cpp_implicit_alias)
4615 node = node->get_alias_target ();
4617 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4618 NULL, true);
4619 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4620 group, we need to mark all symbols in the same comdat group
4621 that way. */
4622 if (node->same_comdat_group)
4623 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4624 next != node;
4625 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4626 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4627 NULL, true);
4630 /* If we're going to need to write this function out, and
4631 there's already a body for it, create RTL for it now.
4632 (There might be no body if this is a method we haven't
4633 gotten around to synthesizing yet.) */
4634 if (!DECL_EXTERNAL (decl)
4635 && decl_needed_p (decl)
4636 && !TREE_ASM_WRITTEN (decl)
4637 && !cgraph_node::get (decl)->definition)
4639 /* We will output the function; no longer consider it in this
4640 loop. */
4641 DECL_DEFER_OUTPUT (decl) = 0;
4642 /* Generate RTL for this function now that we know we
4643 need it. */
4644 expand_or_defer_fn (decl);
4645 /* If we're compiling -fsyntax-only pretend that this
4646 function has been written out so that we don't try to
4647 expand it again. */
4648 if (flag_syntax_only)
4649 TREE_ASM_WRITTEN (decl) = 1;
4650 reconsider = true;
4654 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4655 reconsider = true;
4657 /* Static data members are just like namespace-scope globals. */
4658 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4660 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4661 /* Don't write it out if we haven't seen a definition. */
4662 || DECL_IN_AGGR_P (decl))
4663 continue;
4664 import_export_decl (decl);
4665 /* If this static data member is needed, provide it to the
4666 back end. */
4667 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4668 DECL_EXTERNAL (decl) = 0;
4670 if (vec_safe_length (pending_statics) != 0
4671 && wrapup_global_declarations (pending_statics->address (),
4672 pending_statics->length ()))
4673 reconsider = true;
4675 retries++;
4677 while (reconsider);
4679 /* All used inline functions must have a definition at this point. */
4680 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4682 if (/* Check online inline functions that were actually used. */
4683 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4684 /* If the definition actually was available here, then the
4685 fact that the function was not defined merely represents
4686 that for some reason (use of a template repository,
4687 #pragma interface, etc.) we decided not to emit the
4688 definition here. */
4689 && !DECL_INITIAL (decl)
4690 /* Don't complain if the template was defined. */
4691 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4692 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4693 (template_for_substitution (decl)))))
4695 warning (0, "inline function %q+D used but never defined", decl);
4696 /* Avoid a duplicate warning from check_global_declaration_1. */
4697 TREE_NO_WARNING (decl) = 1;
4701 /* So must decls that use a type with no linkage. */
4702 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4703 no_linkage_error (decl);
4705 maybe_warn_sized_delete ();
4707 /* Then, do the Objective-C stuff. This is where all the
4708 Objective-C module stuff gets generated (symtab,
4709 class/protocol/selector lists etc). This must be done after C++
4710 templates, destructors etc. so that selectors used in C++
4711 templates are properly allocated. */
4712 if (c_dialect_objc ())
4713 objc_write_global_declarations ();
4715 /* We give C linkage to static constructors and destructors. */
4716 push_lang_context (lang_name_c);
4718 /* Generate initialization and destruction functions for all
4719 priorities for which they are required. */
4720 if (priority_info_map)
4721 splay_tree_foreach (priority_info_map,
4722 generate_ctor_and_dtor_functions_for_priority,
4723 /*data=*/&locus);
4724 else if (c_dialect_objc () && objc_static_init_needed_p ())
4725 /* If this is obj-c++ and we need a static init, call
4726 generate_ctor_or_dtor_function. */
4727 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4728 DEFAULT_INIT_PRIORITY, &locus);
4730 /* We're done with the splay-tree now. */
4731 if (priority_info_map)
4732 splay_tree_delete (priority_info_map);
4734 /* Generate any missing aliases. */
4735 maybe_apply_pending_pragma_weaks ();
4737 /* We're done with static constructors, so we can go back to "C++"
4738 linkage now. */
4739 pop_lang_context ();
4741 /* Collect candidates for Java hidden aliases. */
4742 candidates = collect_candidates_for_java_method_aliases ();
4744 timevar_stop (TV_PHASE_DEFERRED);
4745 timevar_start (TV_PHASE_OPT_GEN);
4747 if (flag_vtable_verify)
4749 vtv_recover_class_info ();
4750 vtv_compute_class_hierarchy_transitive_closure ();
4751 vtv_build_vtable_verify_fndecl ();
4754 symtab->finalize_compilation_unit ();
4756 if (flag_vtable_verify)
4758 /* Generate the special constructor initialization function that
4759 calls __VLTRegisterPairs, and give it a very high
4760 initialization priority. This must be done after
4761 finalize_compilation_unit so that we have accurate
4762 information about which vtable will actually be emitted. */
4763 vtv_generate_init_routine ();
4766 timevar_stop (TV_PHASE_OPT_GEN);
4767 timevar_start (TV_PHASE_CHECK_DBGINFO);
4769 /* Now, issue warnings about static, but not defined, functions,
4770 etc., and emit debugging information. */
4771 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4772 if (vec_safe_length (pending_statics) != 0)
4774 check_global_declarations (pending_statics->address (),
4775 pending_statics->length ());
4776 emit_debug_global_declarations (pending_statics->address (),
4777 pending_statics->length ());
4780 perform_deferred_noexcept_checks ();
4782 /* Generate hidden aliases for Java. */
4783 if (candidates)
4785 build_java_method_aliases (candidates);
4786 delete candidates;
4789 finish_repo ();
4791 /* The entire file is now complete. If requested, dump everything
4792 to a file. */
4793 dump_tu ();
4795 if (flag_detailed_statistics)
4797 dump_tree_statistics ();
4798 dump_time_statistics ();
4800 input_location = locus;
4802 #ifdef ENABLE_CHECKING
4803 validate_conversion_obstack ();
4804 #endif /* ENABLE_CHECKING */
4806 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4809 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4810 function to call in parse-tree form; it has not yet been
4811 semantically analyzed. ARGS are the arguments to the function.
4812 They have already been semantically analyzed. This may change
4813 ARGS. */
4815 tree
4816 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4817 tsubst_flags_t complain)
4819 tree orig_fn;
4820 vec<tree, va_gc> *orig_args = NULL;
4821 tree expr;
4822 tree object;
4824 orig_fn = fn;
4825 object = TREE_OPERAND (fn, 0);
4827 if (processing_template_decl)
4829 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4830 || TREE_CODE (fn) == MEMBER_REF);
4831 if (type_dependent_expression_p (fn)
4832 || any_type_dependent_arguments_p (*args))
4833 return build_nt_call_vec (fn, *args);
4835 orig_args = make_tree_vector_copy (*args);
4837 /* Transform the arguments and add the implicit "this"
4838 parameter. That must be done before the FN is transformed
4839 because we depend on the form of FN. */
4840 make_args_non_dependent (*args);
4841 object = build_non_dependent_expr (object);
4842 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4844 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4845 object = cp_build_addr_expr (object, complain);
4846 vec_safe_insert (*args, 0, object);
4848 /* Now that the arguments are done, transform FN. */
4849 fn = build_non_dependent_expr (fn);
4852 /* A qualified name corresponding to a bound pointer-to-member is
4853 represented as an OFFSET_REF:
4855 struct B { void g(); };
4856 void (B::*p)();
4857 void B::g() { (this->*p)(); } */
4858 if (TREE_CODE (fn) == OFFSET_REF)
4860 tree object_addr = cp_build_addr_expr (object, complain);
4861 fn = TREE_OPERAND (fn, 1);
4862 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4863 complain);
4864 vec_safe_insert (*args, 0, object_addr);
4867 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4868 expr = build_op_call (fn, args, complain);
4869 else
4870 expr = cp_build_function_call_vec (fn, args, complain);
4871 if (processing_template_decl && expr != error_mark_node)
4872 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4874 if (orig_args != NULL)
4875 release_tree_vector (orig_args);
4877 return expr;
4881 void
4882 check_default_args (tree x)
4884 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4885 bool saw_def = false;
4886 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4887 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4889 if (TREE_PURPOSE (arg))
4890 saw_def = true;
4891 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4893 error ("default argument missing for parameter %P of %q+#D", i, x);
4894 TREE_PURPOSE (arg) = error_mark_node;
4899 /* Return true if function DECL can be inlined. This is used to force
4900 instantiation of methods that might be interesting for inlining. */
4901 bool
4902 possibly_inlined_p (tree decl)
4904 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4905 if (DECL_UNINLINABLE (decl))
4906 return false;
4907 if (!optimize || pragma_java_exceptions)
4908 return DECL_DECLARED_INLINE_P (decl);
4909 /* When optimizing, we might inline everything when flatten
4910 attribute or heuristics inlining for size or autoinlining
4911 is used. */
4912 return true;
4915 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4916 If DECL is a specialization or implicitly declared class member,
4917 generate the actual definition. Return false if something goes
4918 wrong, true otherwise. */
4920 bool
4921 mark_used (tree decl, tsubst_flags_t complain)
4923 /* If DECL is a BASELINK for a single function, then treat it just
4924 like the DECL for the function. Otherwise, if the BASELINK is
4925 for an overloaded function, we don't know which function was
4926 actually used until after overload resolution. */
4927 if (BASELINK_P (decl))
4929 decl = BASELINK_FUNCTIONS (decl);
4930 if (really_overloaded_fn (decl))
4931 return true;
4932 decl = OVL_CURRENT (decl);
4935 /* Set TREE_USED for the benefit of -Wunused. */
4936 TREE_USED (decl) = 1;
4937 if (DECL_CLONED_FUNCTION_P (decl))
4938 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4940 /* Mark enumeration types as used. */
4941 if (TREE_CODE (decl) == CONST_DECL)
4942 used_types_insert (DECL_CONTEXT (decl));
4944 if (TREE_CODE (decl) == FUNCTION_DECL)
4945 maybe_instantiate_noexcept (decl);
4947 if (TREE_CODE (decl) == FUNCTION_DECL
4948 && DECL_DELETED_FN (decl))
4950 if (DECL_ARTIFICIAL (decl))
4952 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4953 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4955 /* We mark a lambda conversion op as deleted if we can't
4956 generate it properly; see maybe_add_lambda_conv_op. */
4957 sorry ("converting lambda which uses %<...%> to "
4958 "function pointer");
4959 return false;
4962 if (complain & tf_error)
4964 error ("use of deleted function %qD", decl);
4965 if (!maybe_explain_implicit_delete (decl))
4966 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4968 return false;
4971 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
4972 && deprecated_state != DEPRECATED_SUPPRESS)
4973 warn_deprecated_use (decl, NULL_TREE);
4975 /* We can only check DECL_ODR_USED on variables or functions with
4976 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4977 might need special handling for. */
4978 if (!VAR_OR_FUNCTION_DECL_P (decl)
4979 || DECL_LANG_SPECIFIC (decl) == NULL
4980 || DECL_THUNK_P (decl))
4982 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4984 if (complain & tf_error)
4985 error ("use of %qD before deduction of %<auto%>", decl);
4986 return false;
4988 return true;
4991 /* We only want to do this processing once. We don't need to keep trying
4992 to instantiate inline templates, because unit-at-a-time will make sure
4993 we get them compiled before functions that want to inline them. */
4994 if (DECL_ODR_USED (decl))
4995 return true;
4997 /* If within finish_function, defer the rest until that function
4998 finishes, otherwise it might recurse. */
4999 if (defer_mark_used_calls)
5001 vec_safe_push (deferred_mark_used_calls, decl);
5002 return true;
5005 /* Normally, we can wait until instantiation-time to synthesize DECL.
5006 However, if DECL is a static data member initialized with a constant
5007 or a constexpr function, we need it right now because a reference to
5008 such a data member or a call to such function is not value-dependent.
5009 For a function that uses auto in the return type, we need to instantiate
5010 it to find out its type. For OpenMP user defined reductions, we need
5011 them instantiated for reduction clauses which inline them by hand
5012 directly. */
5013 if (DECL_LANG_SPECIFIC (decl)
5014 && DECL_TEMPLATE_INFO (decl)
5015 && (decl_maybe_constant_var_p (decl)
5016 || (TREE_CODE (decl) == FUNCTION_DECL
5017 && (DECL_DECLARED_CONSTEXPR_P (decl)
5018 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
5019 || undeduced_auto_decl (decl))
5020 && !uses_template_parms (DECL_TI_ARGS (decl)))
5022 /* Instantiating a function will result in garbage collection. We
5023 must treat this situation as if we were within the body of a
5024 function so as to avoid collecting live data only referenced from
5025 the stack (such as overload resolution candidates). */
5026 ++function_depth;
5027 instantiate_decl (decl, /*defer_ok=*/false,
5028 /*expl_inst_class_mem_p=*/false);
5029 --function_depth;
5032 if (processing_template_decl || in_template_function ())
5033 return true;
5035 /* Check this too in case we're within instantiate_non_dependent_expr. */
5036 if (DECL_TEMPLATE_INFO (decl)
5037 && uses_template_parms (DECL_TI_ARGS (decl)))
5038 return true;
5040 require_deduced_type (decl);
5042 /* If we don't need a value, then we don't need to synthesize DECL. */
5043 if (cp_unevaluated_operand != 0)
5044 return true;
5046 DECL_ODR_USED (decl) = 1;
5047 if (DECL_CLONED_FUNCTION_P (decl))
5048 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5050 /* DR 757: A type without linkage shall not be used as the type of a
5051 variable or function with linkage, unless
5052 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5053 o the variable or function is not used (3.2 [basic.def.odr]) or is
5054 defined in the same translation unit. */
5055 if (cxx_dialect > cxx98
5056 && decl_linkage (decl) != lk_none
5057 && !DECL_EXTERN_C_P (decl)
5058 && !DECL_ARTIFICIAL (decl)
5059 && !decl_defined_p (decl)
5060 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5062 if (is_local_extern (decl))
5063 /* There's no way to define a local extern, and adding it to
5064 the vector interferes with GC, so give an error now. */
5065 no_linkage_error (decl);
5066 else
5067 vec_safe_push (no_linkage_decls, decl);
5070 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5071 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5072 /* Remember it, so we can check it was defined. */
5073 note_vague_linkage_fn (decl);
5075 /* Is it a synthesized method that needs to be synthesized? */
5076 if (TREE_CODE (decl) == FUNCTION_DECL
5077 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5078 && DECL_DEFAULTED_FN (decl)
5079 /* A function defaulted outside the class is synthesized either by
5080 cp_finish_decl or instantiate_decl. */
5081 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5082 && ! DECL_INITIAL (decl))
5084 /* Defer virtual destructors so that thunks get the right
5085 linkage. */
5086 if (DECL_VIRTUAL_P (decl) && !at_eof)
5088 note_vague_linkage_fn (decl);
5089 return true;
5092 /* Remember the current location for a function we will end up
5093 synthesizing. Then we can inform the user where it was
5094 required in the case of error. */
5095 DECL_SOURCE_LOCATION (decl) = input_location;
5097 /* Synthesizing an implicitly defined member function will result in
5098 garbage collection. We must treat this situation as if we were
5099 within the body of a function so as to avoid collecting live data
5100 on the stack (such as overload resolution candidates).
5102 We could just let cp_write_global_declarations handle synthesizing
5103 this function by adding it to deferred_fns, but doing
5104 it at the use site produces better error messages. */
5105 ++function_depth;
5106 synthesize_method (decl);
5107 --function_depth;
5108 /* If this is a synthesized method we don't need to
5109 do the instantiation test below. */
5111 else if (VAR_OR_FUNCTION_DECL_P (decl)
5112 && DECL_TEMPLATE_INFO (decl)
5113 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5114 || always_instantiate_p (decl)))
5115 /* If this is a function or variable that is an instance of some
5116 template, we now know that we will need to actually do the
5117 instantiation. We check that DECL is not an explicit
5118 instantiation because that is not checked in instantiate_decl.
5120 We put off instantiating functions in order to improve compile
5121 times. Maintaining a stack of active functions is expensive,
5122 and the inliner knows to instantiate any functions it might
5123 need. Therefore, we always try to defer instantiation. */
5125 ++function_depth;
5126 instantiate_decl (decl, /*defer_ok=*/true,
5127 /*expl_inst_class_mem_p=*/false);
5128 --function_depth;
5131 return true;
5134 bool
5135 mark_used (tree decl)
5137 return mark_used (decl, tf_warning_or_error);
5140 tree
5141 vtv_start_verification_constructor_init_function (void)
5143 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5146 tree
5147 vtv_finish_verification_constructor_init_function (tree function_body)
5149 tree fn;
5151 finish_compound_stmt (function_body);
5152 fn = finish_function (0);
5153 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5154 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5156 return fn;
5159 #include "gt-cp-decl2.h"