2014-10-24 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cp / decl2.c
blob60c8a63e1b97ae696694121904df69821c0a1e48
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "stringpool.h"
35 #include "varasm.h"
36 #include "attribs.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "flags.h"
40 #include "cp-tree.h"
41 #include "decl.h"
42 #include "toplev.h"
43 #include "timevar.h"
44 #include "cpplib.h"
45 #include "target.h"
46 #include "c-family/c-common.h"
47 #include "c-family/c-objc.h"
48 #include "cgraph.h"
49 #include "tree-inline.h"
50 #include "c-family/c-pragma.h"
51 #include "dumpfile.h"
52 #include "intl.h"
53 #include "splay-tree.h"
54 #include "langhooks.h"
55 #include "c-family/c-ada-spec.h"
56 #include "asan.h"
58 extern cpp_reader *parse_in;
60 /* This structure contains information about the initializations
61 and/or destructions required for a particular priority level. */
62 typedef struct priority_info_s {
63 /* Nonzero if there have been any initializations at this priority
64 throughout the translation unit. */
65 int initializations_p;
66 /* Nonzero if there have been any destructions at this priority
67 throughout the translation unit. */
68 int destructions_p;
69 } *priority_info;
71 static void mark_vtable_entries (tree);
72 static bool maybe_emit_vtables (tree);
73 static bool acceptable_java_type (tree);
74 static tree start_objects (int, int);
75 static void finish_objects (int, int, tree);
76 static tree start_static_storage_duration_function (unsigned);
77 static void finish_static_storage_duration_function (tree);
78 static priority_info get_priority_info (int);
79 static void do_static_initialization_or_destruction (tree, bool);
80 static void one_static_initialization_or_destruction (tree, tree, bool);
81 static void generate_ctor_or_dtor_function (bool, int, location_t *);
82 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
83 void *);
84 static tree prune_vars_needing_no_initialization (tree *);
85 static void write_out_vars (tree);
86 static void import_export_class (tree);
87 static tree get_guard_bits (tree);
88 static void determine_visibility_from_class (tree, tree);
89 static bool determine_hidden_inline (tree);
90 static bool decl_defined_p (tree);
92 /* A list of static class variables. This is needed, because a
93 static class variable can be declared inside the class without
94 an initializer, and then initialized, statically, outside the class. */
95 static GTY(()) vec<tree, va_gc> *pending_statics;
97 /* A list of functions which were declared inline, but which we
98 may need to emit outline anyway. */
99 static GTY(()) vec<tree, va_gc> *deferred_fns;
101 /* A list of decls that use types with no linkage, which we need to make
102 sure are defined. */
103 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
105 /* Nonzero if we're done parsing and into end-of-file activities. */
107 int at_eof;
110 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
111 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
112 that apply to the function). */
114 tree
115 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
116 cp_ref_qualifier rqual)
118 tree raises;
119 tree attrs;
120 int type_quals;
121 bool late_return_type_p;
123 if (fntype == error_mark_node || ctype == error_mark_node)
124 return error_mark_node;
126 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
127 || TREE_CODE (fntype) == METHOD_TYPE);
129 type_quals = quals & ~TYPE_QUAL_RESTRICT;
130 ctype = cp_build_qualified_type (ctype, type_quals);
131 raises = TYPE_RAISES_EXCEPTIONS (fntype);
132 attrs = TYPE_ATTRIBUTES (fntype);
133 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
134 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
135 (TREE_CODE (fntype) == METHOD_TYPE
136 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
137 : TYPE_ARG_TYPES (fntype)));
138 if (attrs)
139 fntype = cp_build_type_attribute_variant (fntype, attrs);
140 if (rqual)
141 fntype = build_ref_qualified_type (fntype, rqual);
142 if (raises)
143 fntype = build_exception_variant (fntype, raises);
144 if (late_return_type_p)
145 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
147 return fntype;
150 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
151 return type changed to NEW_RET. */
153 tree
154 change_return_type (tree new_ret, tree fntype)
156 tree newtype;
157 tree args = TYPE_ARG_TYPES (fntype);
158 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
159 tree attrs = TYPE_ATTRIBUTES (fntype);
160 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
162 if (new_ret == error_mark_node)
163 return fntype;
165 if (same_type_p (new_ret, TREE_TYPE (fntype)))
166 return fntype;
168 if (TREE_CODE (fntype) == FUNCTION_TYPE)
170 newtype = build_function_type (new_ret, args);
171 newtype = apply_memfn_quals (newtype,
172 type_memfn_quals (fntype),
173 type_memfn_rqual (fntype));
175 else
176 newtype = build_method_type_directly
177 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
178 if (raises)
179 newtype = build_exception_variant (newtype, raises);
180 if (attrs)
181 newtype = cp_build_type_attribute_variant (newtype, attrs);
182 if (late_return_type_p)
183 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
185 return newtype;
188 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
189 appropriately. */
191 tree
192 cp_build_parm_decl (tree name, tree type)
194 tree parm = build_decl (input_location,
195 PARM_DECL, name, type);
196 /* DECL_ARG_TYPE is only used by the back end and the back end never
197 sees templates. */
198 if (!processing_template_decl)
199 DECL_ARG_TYPE (parm) = type_passed_as (type);
201 return parm;
204 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
205 indicated NAME. */
207 tree
208 build_artificial_parm (tree name, tree type)
210 tree parm = cp_build_parm_decl (name, type);
211 DECL_ARTIFICIAL (parm) = 1;
212 /* All our artificial parms are implicitly `const'; they cannot be
213 assigned to. */
214 TREE_READONLY (parm) = 1;
215 return parm;
218 /* Constructors for types with virtual baseclasses need an "in-charge" flag
219 saying whether this constructor is responsible for initialization of
220 virtual baseclasses or not. All destructors also need this "in-charge"
221 flag, which additionally determines whether or not the destructor should
222 free the memory for the object.
224 This function adds the "in-charge" flag to member function FN if
225 appropriate. It is called from grokclassfn and tsubst.
226 FN must be either a constructor or destructor.
228 The in-charge flag follows the 'this' parameter, and is followed by the
229 VTT parm (if any), then the user-written parms. */
231 void
232 maybe_retrofit_in_chrg (tree fn)
234 tree basetype, arg_types, parms, parm, fntype;
236 /* If we've already add the in-charge parameter don't do it again. */
237 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
238 return;
240 /* When processing templates we can't know, in general, whether or
241 not we're going to have virtual baseclasses. */
242 if (processing_template_decl)
243 return;
245 /* We don't need an in-charge parameter for constructors that don't
246 have virtual bases. */
247 if (DECL_CONSTRUCTOR_P (fn)
248 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
249 return;
251 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
252 basetype = TREE_TYPE (TREE_VALUE (arg_types));
253 arg_types = TREE_CHAIN (arg_types);
255 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
257 /* If this is a subobject constructor or destructor, our caller will
258 pass us a pointer to our VTT. */
259 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
261 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
263 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
264 DECL_CHAIN (parm) = parms;
265 parms = parm;
267 /* ...and then to TYPE_ARG_TYPES. */
268 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
270 DECL_HAS_VTT_PARM_P (fn) = 1;
273 /* Then add the in-charge parm (before the VTT parm). */
274 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
275 DECL_CHAIN (parm) = parms;
276 parms = parm;
277 arg_types = hash_tree_chain (integer_type_node, arg_types);
279 /* Insert our new parameter(s) into the list. */
280 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
282 /* And rebuild the function type. */
283 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
284 arg_types);
285 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
286 fntype = build_exception_variant (fntype,
287 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
288 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
289 fntype = (cp_build_type_attribute_variant
290 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
291 TREE_TYPE (fn) = fntype;
293 /* Now we've got the in-charge parameter. */
294 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
297 /* Classes overload their constituent function names automatically.
298 When a function name is declared in a record structure,
299 its name is changed to it overloaded name. Since names for
300 constructors and destructors can conflict, we place a leading
301 '$' for destructors.
303 CNAME is the name of the class we are grokking for.
305 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
307 FLAGS contains bits saying what's special about today's
308 arguments. DTOR_FLAG == DESTRUCTOR.
310 If FUNCTION is a destructor, then we must add the `auto-delete' field
311 as a second parameter. There is some hair associated with the fact
312 that we must "declare" this variable in the manner consistent with the
313 way the rest of the arguments were declared.
315 QUALS are the qualifiers for the this pointer. */
317 void
318 grokclassfn (tree ctype, tree function, enum overload_flags flags)
320 tree fn_name = DECL_NAME (function);
322 /* Even within an `extern "C"' block, members get C++ linkage. See
323 [dcl.link] for details. */
324 SET_DECL_LANGUAGE (function, lang_cplusplus);
326 if (fn_name == NULL_TREE)
328 error ("name missing for member function");
329 fn_name = get_identifier ("<anonymous>");
330 DECL_NAME (function) = fn_name;
333 DECL_CONTEXT (function) = ctype;
335 if (flags == DTOR_FLAG)
336 DECL_DESTRUCTOR_P (function) = 1;
338 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
339 maybe_retrofit_in_chrg (function);
342 /* Create an ARRAY_REF, checking for the user doing things backwards
343 along the way. DECLTYPE_P is for N3276, as in the parser. */
345 tree
346 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
347 bool decltype_p)
349 tree type;
350 tree expr;
351 tree orig_array_expr = array_expr;
352 tree orig_index_exp = index_exp;
354 if (error_operand_p (array_expr) || error_operand_p (index_exp))
355 return error_mark_node;
357 if (processing_template_decl)
359 if (type_dependent_expression_p (array_expr)
360 || type_dependent_expression_p (index_exp))
361 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
362 NULL_TREE, NULL_TREE);
363 array_expr = build_non_dependent_expr (array_expr);
364 index_exp = build_non_dependent_expr (index_exp);
367 type = TREE_TYPE (array_expr);
368 gcc_assert (type);
369 type = non_reference (type);
371 /* If they have an `operator[]', use that. */
372 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
374 tsubst_flags_t complain = tf_warning_or_error;
375 if (decltype_p)
376 complain |= tf_decltype;
377 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
378 index_exp, NULL_TREE, /*overload=*/NULL, complain);
380 else
382 tree p1, p2, i1, i2;
384 /* Otherwise, create an ARRAY_REF for a pointer or array type.
385 It is a little-known fact that, if `a' is an array and `i' is
386 an int, you can write `i[a]', which means the same thing as
387 `a[i]'. */
388 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
389 p1 = array_expr;
390 else
391 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
393 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
394 p2 = index_exp;
395 else
396 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
398 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
399 false);
400 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
401 false);
403 if ((p1 && i2) && (i1 && p2))
404 error ("ambiguous conversion for array subscript");
406 if (p1 && i2)
407 array_expr = p1, index_exp = i2;
408 else if (i1 && p2)
409 array_expr = p2, index_exp = i1;
410 else
412 error ("invalid types %<%T[%T]%> for array subscript",
413 type, TREE_TYPE (index_exp));
414 return error_mark_node;
417 if (array_expr == error_mark_node || index_exp == error_mark_node)
418 error ("ambiguous conversion for array subscript");
420 expr = build_array_ref (input_location, array_expr, index_exp);
422 if (processing_template_decl && expr != error_mark_node)
423 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
424 NULL_TREE, NULL_TREE);
425 return expr;
428 /* Given the cast expression EXP, checking out its validity. Either return
429 an error_mark_node if there was an unavoidable error, return a cast to
430 void for trying to delete a pointer w/ the value 0, or return the
431 call to delete. If DOING_VEC is true, we handle things differently
432 for doing an array delete.
433 Implements ARM $5.3.4. This is called from the parser. */
435 tree
436 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
437 tsubst_flags_t complain)
439 tree t, type;
441 if (exp == error_mark_node)
442 return exp;
444 if (processing_template_decl)
446 t = build_min (DELETE_EXPR, void_type_node, exp, size);
447 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
448 DELETE_EXPR_USE_VEC (t) = doing_vec;
449 TREE_SIDE_EFFECTS (t) = 1;
450 return t;
453 /* An array can't have been allocated by new, so complain. */
454 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
455 warning (0, "deleting array %q#E", exp);
457 t = build_expr_type_conversion (WANT_POINTER, exp, true);
459 if (t == NULL_TREE || t == error_mark_node)
461 error ("type %q#T argument given to %<delete%>, expected pointer",
462 TREE_TYPE (exp));
463 return error_mark_node;
466 type = TREE_TYPE (t);
468 /* As of Valley Forge, you can delete a pointer to const. */
470 /* You can't delete functions. */
471 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
473 error ("cannot delete a function. Only pointer-to-objects are "
474 "valid arguments to %<delete%>");
475 return error_mark_node;
478 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
479 if (VOID_TYPE_P (TREE_TYPE (type)))
481 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
482 doing_vec = 0;
485 /* Deleting a pointer with the value zero is valid and has no effect. */
486 if (integer_zerop (t))
487 return build1 (NOP_EXPR, void_type_node, t);
489 if (doing_vec)
490 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
491 sfk_deleting_destructor,
492 use_global_delete, complain);
493 else
494 return build_delete (type, t, sfk_deleting_destructor,
495 LOOKUP_NORMAL, use_global_delete,
496 complain);
499 /* Report an error if the indicated template declaration is not the
500 sort of thing that should be a member template. */
502 void
503 check_member_template (tree tmpl)
505 tree decl;
507 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
508 decl = DECL_TEMPLATE_RESULT (tmpl);
510 if (TREE_CODE (decl) == FUNCTION_DECL
511 || DECL_ALIAS_TEMPLATE_P (tmpl)
512 || (TREE_CODE (decl) == TYPE_DECL
513 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
515 /* The parser rejects template declarations in local classes
516 (with the exception of generic lambdas). */
517 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
518 /* The parser rejects any use of virtual in a function template. */
519 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
520 && DECL_VIRTUAL_P (decl)));
522 /* The debug-information generating code doesn't know what to do
523 with member templates. */
524 DECL_IGNORED_P (tmpl) = 1;
526 else if (variable_template_p (tmpl))
527 /* OK */;
528 else
529 error ("template declaration of %q#D", decl);
532 /* Return true iff TYPE is a valid Java parameter or return type. */
534 static bool
535 acceptable_java_type (tree type)
537 if (type == error_mark_node)
538 return false;
540 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
541 return true;
542 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
544 type = TREE_TYPE (type);
545 if (TREE_CODE (type) == RECORD_TYPE)
547 tree args; int i;
548 if (! TYPE_FOR_JAVA (type))
549 return false;
550 if (! CLASSTYPE_TEMPLATE_INFO (type))
551 return true;
552 args = CLASSTYPE_TI_ARGS (type);
553 i = TREE_VEC_LENGTH (args);
554 while (--i >= 0)
556 type = TREE_VEC_ELT (args, i);
557 if (TYPE_PTR_P (type))
558 type = TREE_TYPE (type);
559 if (! TYPE_FOR_JAVA (type))
560 return false;
562 return true;
565 return false;
568 /* For a METHOD in a Java class CTYPE, return true if
569 the parameter and return types are valid Java types.
570 Otherwise, print appropriate error messages, and return false. */
572 bool
573 check_java_method (tree method)
575 bool jerr = false;
576 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
577 tree ret_type = TREE_TYPE (TREE_TYPE (method));
579 if (!acceptable_java_type (ret_type))
581 error ("Java method %qD has non-Java return type %qT",
582 method, ret_type);
583 jerr = true;
586 arg_types = TREE_CHAIN (arg_types);
587 if (DECL_HAS_IN_CHARGE_PARM_P (method))
588 arg_types = TREE_CHAIN (arg_types);
589 if (DECL_HAS_VTT_PARM_P (method))
590 arg_types = TREE_CHAIN (arg_types);
592 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
594 tree type = TREE_VALUE (arg_types);
595 if (!acceptable_java_type (type))
597 if (type != error_mark_node)
598 error ("Java method %qD has non-Java parameter type %qT",
599 method, type);
600 jerr = true;
603 return !jerr;
606 /* Sanity check: report error if this function FUNCTION is not
607 really a member of the class (CTYPE) it is supposed to belong to.
608 TEMPLATE_PARMS is used to specify the template parameters of a member
609 template passed as FUNCTION_DECL. If the member template is passed as a
610 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
611 from the declaration. If the function is not a function template, it
612 must be NULL.
613 It returns the original declaration for the function, NULL_TREE if
614 no declaration was found, error_mark_node if an error was emitted. */
616 tree
617 check_classfn (tree ctype, tree function, tree template_parms)
619 int ix;
620 bool is_template;
621 tree pushed_scope;
623 if (DECL_USE_TEMPLATE (function)
624 && !(TREE_CODE (function) == TEMPLATE_DECL
625 && DECL_TEMPLATE_SPECIALIZATION (function))
626 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
627 /* Since this is a specialization of a member template,
628 we're not going to find the declaration in the class.
629 For example, in:
631 struct S { template <typename T> void f(T); };
632 template <> void S::f(int);
634 we're not going to find `S::f(int)', but there's no
635 reason we should, either. We let our callers know we didn't
636 find the method, but we don't complain. */
637 return NULL_TREE;
639 /* Basic sanity check: for a template function, the template parameters
640 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
641 if (TREE_CODE (function) == TEMPLATE_DECL)
643 if (template_parms
644 && !comp_template_parms (template_parms,
645 DECL_TEMPLATE_PARMS (function)))
647 error ("template parameter lists provided don%'t match the "
648 "template parameters of %qD", function);
649 return error_mark_node;
651 template_parms = DECL_TEMPLATE_PARMS (function);
654 /* OK, is this a definition of a member template? */
655 is_template = (template_parms != NULL_TREE);
657 /* [temp.mem]
659 A destructor shall not be a member template. */
660 if (DECL_DESTRUCTOR_P (function) && is_template)
662 error ("destructor %qD declared as member template", function);
663 return error_mark_node;
666 /* We must enter the scope here, because conversion operators are
667 named by target type, and type equivalence relies on typenames
668 resolving within the scope of CTYPE. */
669 pushed_scope = push_scope (ctype);
670 ix = class_method_index_for_fn (complete_type (ctype), function);
671 if (ix >= 0)
673 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
674 tree fndecls, fndecl = 0;
675 bool is_conv_op;
676 const char *format = NULL;
678 for (fndecls = (*methods)[ix];
679 fndecls; fndecls = OVL_NEXT (fndecls))
681 tree p1, p2;
683 fndecl = OVL_CURRENT (fndecls);
684 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
685 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
687 /* We cannot simply call decls_match because this doesn't
688 work for static member functions that are pretending to
689 be methods, and because the name may have been changed by
690 asm("new_name"). */
692 /* Get rid of the this parameter on functions that become
693 static. */
694 if (DECL_STATIC_FUNCTION_P (fndecl)
695 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
696 p1 = TREE_CHAIN (p1);
698 /* A member template definition only matches a member template
699 declaration. */
700 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
701 continue;
703 /* ref-qualifier or absence of same must match. */
704 if (type_memfn_rqual (TREE_TYPE (function))
705 != type_memfn_rqual (TREE_TYPE (fndecl)))
706 continue;
708 /* While finding a match, same types and params are not enough
709 if the function is versioned. Also check version ("target")
710 attributes. */
711 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
712 TREE_TYPE (TREE_TYPE (fndecl)))
713 && compparms (p1, p2)
714 && !targetm.target_option.function_versions (function, fndecl)
715 && (!is_template
716 || comp_template_parms (template_parms,
717 DECL_TEMPLATE_PARMS (fndecl)))
718 && (DECL_TEMPLATE_SPECIALIZATION (function)
719 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
720 && (!DECL_TEMPLATE_SPECIALIZATION (function)
721 || (DECL_TI_TEMPLATE (function)
722 == DECL_TI_TEMPLATE (fndecl))))
723 break;
725 if (fndecls)
727 if (pushed_scope)
728 pop_scope (pushed_scope);
729 return OVL_CURRENT (fndecls);
732 error_at (DECL_SOURCE_LOCATION (function),
733 "prototype for %q#D does not match any in class %qT",
734 function, ctype);
735 is_conv_op = DECL_CONV_FN_P (fndecl);
737 if (is_conv_op)
738 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
739 fndecls = (*methods)[ix];
740 while (fndecls)
742 fndecl = OVL_CURRENT (fndecls);
743 fndecls = OVL_NEXT (fndecls);
745 if (!fndecls && is_conv_op)
747 if (methods->length () > (size_t) ++ix)
749 fndecls = (*methods)[ix];
750 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
752 fndecls = NULL_TREE;
753 is_conv_op = false;
756 else
757 is_conv_op = false;
759 if (format)
760 format = " %+#D";
761 else if (fndecls)
762 format = N_("candidates are: %+#D");
763 else
764 format = N_("candidate is: %+#D");
765 error (format, fndecl);
768 else if (!COMPLETE_TYPE_P (ctype))
769 cxx_incomplete_type_error (function, ctype);
770 else
771 error ("no %q#D member function declared in class %qT",
772 function, ctype);
774 if (pushed_scope)
775 pop_scope (pushed_scope);
776 return error_mark_node;
779 /* DECL is a function with vague linkage. Remember it so that at the
780 end of the translation unit we can decide whether or not to emit
781 it. */
783 void
784 note_vague_linkage_fn (tree decl)
786 DECL_DEFER_OUTPUT (decl) = 1;
787 vec_safe_push (deferred_fns, decl);
790 /* As above, but for variable template instantiations. */
792 void
793 note_variable_template_instantiation (tree decl)
795 vec_safe_push (pending_statics, decl);
798 /* We have just processed the DECL, which is a static data member.
799 The other parameters are as for cp_finish_decl. */
801 void
802 finish_static_data_member_decl (tree decl,
803 tree init, bool init_const_expr_p,
804 tree asmspec_tree,
805 int flags)
807 DECL_CONTEXT (decl) = current_class_type;
809 /* We cannot call pushdecl here, because that would fill in the
810 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
811 the right thing, namely, to put this decl out straight away. */
813 if (! processing_template_decl)
814 vec_safe_push (pending_statics, decl);
816 if (LOCAL_CLASS_P (current_class_type)
817 /* We already complained about the template definition. */
818 && !DECL_TEMPLATE_INSTANTIATION (decl))
819 permerror (input_location, "local class %q#T shall not have static data member %q#D",
820 current_class_type, decl);
821 else
822 for (tree t = current_class_type; TYPE_P (t);
823 t = CP_TYPE_CONTEXT (t))
824 if (TYPE_ANONYMOUS_P (t))
826 if (permerror (DECL_SOURCE_LOCATION (decl),
827 "static data member %qD in unnamed class", decl))
828 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
829 "unnamed class defined here");
830 break;
833 DECL_IN_AGGR_P (decl) = 1;
835 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
836 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
837 SET_VAR_HAD_UNKNOWN_BOUND (decl);
839 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
842 /* DECLARATOR and DECLSPECS correspond to a class member. The other
843 parameters are as for cp_finish_decl. Return the DECL for the
844 class member declared. */
846 tree
847 grokfield (const cp_declarator *declarator,
848 cp_decl_specifier_seq *declspecs,
849 tree init, bool init_const_expr_p,
850 tree asmspec_tree,
851 tree attrlist)
853 tree value;
854 const char *asmspec = 0;
855 int flags;
856 tree name;
858 if (init
859 && TREE_CODE (init) == TREE_LIST
860 && TREE_VALUE (init) == error_mark_node
861 && TREE_CHAIN (init) == NULL_TREE)
862 init = NULL_TREE;
864 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
865 if (! value || value == error_mark_node)
866 /* friend or constructor went bad. */
867 return error_mark_node;
868 if (TREE_TYPE (value) == error_mark_node)
869 return value;
871 if (TREE_CODE (value) == TYPE_DECL && init)
873 error ("typedef %qD is initialized (use decltype instead)", value);
874 init = NULL_TREE;
877 /* Pass friendly classes back. */
878 if (value == void_type_node)
879 return value;
882 name = DECL_NAME (value);
884 if (name != NULL_TREE)
886 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
888 error ("explicit template argument list not allowed");
889 return error_mark_node;
892 if (IDENTIFIER_POINTER (name)[0] == '_'
893 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
894 error ("member %qD conflicts with virtual function table field name",
895 value);
898 /* Stash away type declarations. */
899 if (TREE_CODE (value) == TYPE_DECL)
901 DECL_NONLOCAL (value) = 1;
902 DECL_CONTEXT (value) = current_class_type;
904 if (attrlist)
906 int attrflags = 0;
908 /* If this is a typedef that names the class for linkage purposes
909 (7.1.3p8), apply any attributes directly to the type. */
910 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
911 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
912 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
914 cplus_decl_attributes (&value, attrlist, attrflags);
917 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
918 && TREE_TYPE (value) != error_mark_node
919 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
920 set_underlying_type (value);
922 /* It's important that push_template_decl below follows
923 set_underlying_type above so that the created template
924 carries the properly set type of VALUE. */
925 if (processing_template_decl)
926 value = push_template_decl (value);
928 record_locally_defined_typedef (value);
929 return value;
932 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
934 if (!friendp && DECL_IN_AGGR_P (value))
936 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
937 return void_type_node;
940 if (asmspec_tree && asmspec_tree != error_mark_node)
941 asmspec = TREE_STRING_POINTER (asmspec_tree);
943 if (init)
945 if (TREE_CODE (value) == FUNCTION_DECL)
947 if (init == ridpointers[(int)RID_DELETE])
949 DECL_DELETED_FN (value) = 1;
950 DECL_DECLARED_INLINE_P (value) = 1;
951 DECL_INITIAL (value) = error_mark_node;
953 else if (init == ridpointers[(int)RID_DEFAULT])
955 if (defaultable_fn_check (value))
957 DECL_DEFAULTED_FN (value) = 1;
958 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
959 DECL_DECLARED_INLINE_P (value) = 1;
962 else if (TREE_CODE (init) == DEFAULT_ARG)
963 error ("invalid initializer for member function %qD", value);
964 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
966 if (integer_zerop (init))
967 DECL_PURE_VIRTUAL_P (value) = 1;
968 else if (error_operand_p (init))
969 ; /* An error has already been reported. */
970 else
971 error ("invalid initializer for member function %qD",
972 value);
974 else
976 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
977 if (friendp)
978 error ("initializer specified for friend function %qD",
979 value);
980 else
981 error ("initializer specified for static member function %qD",
982 value);
985 else if (TREE_CODE (value) == FIELD_DECL)
986 /* C++11 NSDMI, keep going. */;
987 else if (!VAR_P (value))
988 gcc_unreachable ();
991 /* Pass friend decls back. */
992 if ((TREE_CODE (value) == FUNCTION_DECL
993 || TREE_CODE (value) == TEMPLATE_DECL)
994 && DECL_CONTEXT (value) != current_class_type)
995 return value;
997 /* Need to set this before push_template_decl. */
998 if (TREE_CODE (value) == VAR_DECL)
999 DECL_CONTEXT (value) = current_class_type;
1001 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1003 value = push_template_decl (value);
1004 if (error_operand_p (value))
1005 return error_mark_node;
1008 if (attrlist)
1009 cplus_decl_attributes (&value, attrlist, 0);
1011 if (init && DIRECT_LIST_INIT_P (init))
1012 flags = LOOKUP_NORMAL;
1013 else
1014 flags = LOOKUP_IMPLICIT;
1016 switch (TREE_CODE (value))
1018 case VAR_DECL:
1019 finish_static_data_member_decl (value, init, init_const_expr_p,
1020 asmspec_tree, flags);
1021 return value;
1023 case FIELD_DECL:
1024 if (asmspec)
1025 error ("%<asm%> specifiers are not permitted on non-static data members");
1026 if (DECL_INITIAL (value) == error_mark_node)
1027 init = error_mark_node;
1028 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1029 NULL_TREE, flags);
1030 DECL_IN_AGGR_P (value) = 1;
1031 return value;
1033 case FUNCTION_DECL:
1034 if (asmspec)
1035 set_user_assembler_name (value, asmspec);
1037 cp_finish_decl (value,
1038 /*init=*/NULL_TREE,
1039 /*init_const_expr_p=*/false,
1040 asmspec_tree, flags);
1042 /* Pass friends back this way. */
1043 if (DECL_FRIEND_P (value))
1044 return void_type_node;
1046 DECL_IN_AGGR_P (value) = 1;
1047 return value;
1049 default:
1050 gcc_unreachable ();
1052 return NULL_TREE;
1055 /* Like `grokfield', but for bitfields.
1056 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1058 tree
1059 grokbitfield (const cp_declarator *declarator,
1060 cp_decl_specifier_seq *declspecs, tree width,
1061 tree attrlist)
1063 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1065 if (value == error_mark_node)
1066 return NULL_TREE; /* friends went bad. */
1067 if (TREE_TYPE (value) == error_mark_node)
1068 return value;
1070 /* Pass friendly classes back. */
1071 if (VOID_TYPE_P (value))
1072 return void_type_node;
1074 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1075 && (POINTER_TYPE_P (value)
1076 || !dependent_type_p (TREE_TYPE (value))))
1078 error ("bit-field %qD with non-integral type", value);
1079 return error_mark_node;
1082 if (TREE_CODE (value) == TYPE_DECL)
1084 error ("cannot declare %qD to be a bit-field type", value);
1085 return NULL_TREE;
1088 /* Usually, finish_struct_1 catches bitfields with invalid types.
1089 But, in the case of bitfields with function type, we confuse
1090 ourselves into thinking they are member functions, so we must
1091 check here. */
1092 if (TREE_CODE (value) == FUNCTION_DECL)
1094 error ("cannot declare bit-field %qD with function type",
1095 DECL_NAME (value));
1096 return NULL_TREE;
1099 if (DECL_IN_AGGR_P (value))
1101 error ("%qD is already defined in the class %qT", value,
1102 DECL_CONTEXT (value));
1103 return void_type_node;
1106 if (TREE_STATIC (value))
1108 error ("static member %qD cannot be a bit-field", value);
1109 return NULL_TREE;
1111 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1113 if (width != error_mark_node)
1115 /* The width must be an integer type. */
1116 if (!type_dependent_expression_p (width)
1117 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1118 error ("width of bit-field %qD has non-integral type %qT", value,
1119 TREE_TYPE (width));
1120 DECL_INITIAL (value) = width;
1121 SET_DECL_C_BIT_FIELD (value);
1124 DECL_IN_AGGR_P (value) = 1;
1126 if (attrlist)
1127 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1129 return value;
1133 /* Returns true iff ATTR is an attribute which needs to be applied at
1134 instantiation time rather than template definition time. */
1136 static bool
1137 is_late_template_attribute (tree attr, tree decl)
1139 tree name = get_attribute_name (attr);
1140 tree args = TREE_VALUE (attr);
1141 const struct attribute_spec *spec = lookup_attribute_spec (name);
1142 tree arg;
1144 if (!spec)
1145 /* Unknown attribute. */
1146 return false;
1148 /* Attribute weak handling wants to write out assembly right away. */
1149 if (is_attribute_p ("weak", name))
1150 return true;
1152 /* Attribute unused is applied directly, as it appertains to
1153 decls. */
1154 if (is_attribute_p ("unused", name))
1155 return false;
1157 /* #pragma omp declare simd attribute needs to be always deferred. */
1158 if (flag_openmp
1159 && is_attribute_p ("omp declare simd", name))
1160 return true;
1162 /* If any of the arguments are dependent expressions, we can't evaluate
1163 the attribute until instantiation time. */
1164 for (arg = args; arg; arg = TREE_CHAIN (arg))
1166 tree t = TREE_VALUE (arg);
1168 /* If the first attribute argument is an identifier, only consider
1169 second and following arguments. Attributes like mode, format,
1170 cleanup and several target specific attributes aren't late
1171 just because they have an IDENTIFIER_NODE as first argument. */
1172 if (arg == args && identifier_p (t))
1173 continue;
1175 if (value_dependent_expression_p (t)
1176 || type_dependent_expression_p (t))
1177 return true;
1180 if (TREE_CODE (decl) == TYPE_DECL
1181 || TYPE_P (decl)
1182 || spec->type_required)
1184 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1186 /* We can't apply any attributes to a completely unknown type until
1187 instantiation time. */
1188 enum tree_code code = TREE_CODE (type);
1189 if (code == TEMPLATE_TYPE_PARM
1190 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1191 || code == TYPENAME_TYPE)
1192 return true;
1193 /* Also defer most attributes on dependent types. This is not
1194 necessary in all cases, but is the better default. */
1195 else if (dependent_type_p (type)
1196 /* But attributes abi_tag and visibility specifically apply
1197 to templates. */
1198 && !is_attribute_p ("abi_tag", name)
1199 && !is_attribute_p ("visibility", name))
1200 return true;
1201 else
1202 return false;
1204 else
1205 return false;
1208 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1209 applied at instantiation time and return them. If IS_DEPENDENT is true,
1210 the declaration itself is dependent, so all attributes should be applied
1211 at instantiation time. */
1213 static tree
1214 splice_template_attributes (tree *attr_p, tree decl)
1216 tree *p = attr_p;
1217 tree late_attrs = NULL_TREE;
1218 tree *q = &late_attrs;
1220 if (!p)
1221 return NULL_TREE;
1223 for (; *p; )
1225 if (is_late_template_attribute (*p, decl))
1227 ATTR_IS_DEPENDENT (*p) = 1;
1228 *q = *p;
1229 *p = TREE_CHAIN (*p);
1230 q = &TREE_CHAIN (*q);
1231 *q = NULL_TREE;
1233 else
1234 p = &TREE_CHAIN (*p);
1237 return late_attrs;
1240 /* Remove any late attributes from the list in ATTR_P and attach them to
1241 DECL_P. */
1243 static void
1244 save_template_attributes (tree *attr_p, tree *decl_p)
1246 tree *q;
1248 if (attr_p && *attr_p == error_mark_node)
1249 return;
1251 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1252 if (!late_attrs)
1253 return;
1255 if (DECL_P (*decl_p))
1256 q = &DECL_ATTRIBUTES (*decl_p);
1257 else
1258 q = &TYPE_ATTRIBUTES (*decl_p);
1260 tree old_attrs = *q;
1262 /* Merge the late attributes at the beginning with the attribute
1263 list. */
1264 late_attrs = merge_attributes (late_attrs, *q);
1265 *q = late_attrs;
1267 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1269 /* We've added new attributes directly to the main variant, so
1270 now we need to update all of the other variants to include
1271 these new attributes. */
1272 tree variant;
1273 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1274 variant = TYPE_NEXT_VARIANT (variant))
1276 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1277 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1282 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1283 to a typedef which gives a previously anonymous class or enum a name for
1284 linkage purposes. */
1286 bool
1287 attributes_naming_typedef_ok (tree attrs)
1289 for (; attrs; attrs = TREE_CHAIN (attrs))
1291 tree name = get_attribute_name (attrs);
1292 if (is_attribute_p ("vector_size", name))
1293 return false;
1295 return true;
1298 /* Like reconstruct_complex_type, but handle also template trees. */
1300 tree
1301 cp_reconstruct_complex_type (tree type, tree bottom)
1303 tree inner, outer;
1304 bool late_return_type_p = false;
1306 if (TYPE_PTR_P (type))
1308 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1309 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1310 TYPE_REF_CAN_ALIAS_ALL (type));
1312 else if (TREE_CODE (type) == REFERENCE_TYPE)
1314 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1315 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1316 TYPE_REF_CAN_ALIAS_ALL (type));
1318 else if (TREE_CODE (type) == ARRAY_TYPE)
1320 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1321 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1322 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1323 element type qualification will be handled by the recursive
1324 cp_reconstruct_complex_type call and cp_build_qualified_type
1325 for ARRAY_TYPEs changes the element type. */
1326 return outer;
1328 else if (TREE_CODE (type) == FUNCTION_TYPE)
1330 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1331 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1332 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1333 outer = apply_memfn_quals (outer,
1334 type_memfn_quals (type),
1335 type_memfn_rqual (type));
1337 else if (TREE_CODE (type) == METHOD_TYPE)
1339 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1340 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1341 /* The build_method_type_directly() routine prepends 'this' to argument list,
1342 so we must compensate by getting rid of it. */
1343 outer
1344 = build_method_type_directly
1345 (class_of_this_parm (type), inner,
1346 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1348 else if (TREE_CODE (type) == OFFSET_TYPE)
1350 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1351 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1353 else
1354 return bottom;
1356 if (TYPE_ATTRIBUTES (type))
1357 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1358 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1360 if (late_return_type_p)
1361 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1363 return outer;
1366 /* Replaces any constexpr expression that may be into the attributes
1367 arguments with their reduced value. */
1369 static void
1370 cp_check_const_attributes (tree attributes)
1372 if (attributes == error_mark_node)
1373 return;
1375 tree attr;
1376 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1378 tree arg;
1379 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1381 tree expr = TREE_VALUE (arg);
1382 if (EXPR_P (expr))
1383 TREE_VALUE (arg) = maybe_constant_value (expr);
1388 /* Return true if TYPE is an OpenMP mappable type. */
1389 bool
1390 cp_omp_mappable_type (tree type)
1392 /* Mappable type has to be complete. */
1393 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1394 return false;
1395 /* Arrays have mappable type if the elements have mappable type. */
1396 while (TREE_CODE (type) == ARRAY_TYPE)
1397 type = TREE_TYPE (type);
1398 /* A mappable type cannot contain virtual members. */
1399 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1400 return false;
1401 /* All data members must be non-static. */
1402 if (CLASS_TYPE_P (type))
1404 tree field;
1405 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1406 if (TREE_CODE (field) == VAR_DECL)
1407 return false;
1408 /* All fields must have mappable types. */
1409 else if (TREE_CODE (field) == FIELD_DECL
1410 && !cp_omp_mappable_type (TREE_TYPE (field)))
1411 return false;
1413 return true;
1416 /* Like decl_attributes, but handle C++ complexity. */
1418 void
1419 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1421 if (*decl == NULL_TREE || *decl == void_type_node
1422 || *decl == error_mark_node)
1423 return;
1425 /* Add implicit "omp declare target" attribute if requested. */
1426 if (scope_chain->omp_declare_target_attribute
1427 && ((TREE_CODE (*decl) == VAR_DECL && TREE_STATIC (*decl))
1428 || TREE_CODE (*decl) == FUNCTION_DECL))
1430 if (TREE_CODE (*decl) == VAR_DECL
1431 && DECL_CLASS_SCOPE_P (*decl))
1432 error ("%q+D static data member inside of declare target directive",
1433 *decl);
1434 else if (TREE_CODE (*decl) == VAR_DECL
1435 && (DECL_FUNCTION_SCOPE_P (*decl)
1436 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1437 error ("%q+D in block scope inside of declare target directive",
1438 *decl);
1439 else if (!processing_template_decl
1440 && TREE_CODE (*decl) == VAR_DECL
1441 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1442 error ("%q+D in declare target directive does not have mappable type",
1443 *decl);
1444 else
1445 attributes = tree_cons (get_identifier ("omp declare target"),
1446 NULL_TREE, attributes);
1449 if (processing_template_decl)
1451 if (check_for_bare_parameter_packs (attributes))
1452 return;
1454 save_template_attributes (&attributes, decl);
1457 cp_check_const_attributes (attributes);
1459 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1460 decl = &DECL_TEMPLATE_RESULT (*decl);
1462 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1464 attributes
1465 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1466 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (*decl)),
1467 attributes, flags);
1469 else
1470 decl_attributes (decl, attributes, flags);
1472 if (TREE_CODE (*decl) == TYPE_DECL)
1473 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1476 /* Walks through the namespace- or function-scope anonymous union
1477 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1478 Returns one of the fields for use in the mangled name. */
1480 static tree
1481 build_anon_union_vars (tree type, tree object)
1483 tree main_decl = NULL_TREE;
1484 tree field;
1486 /* Rather than write the code to handle the non-union case,
1487 just give an error. */
1488 if (TREE_CODE (type) != UNION_TYPE)
1490 error ("anonymous struct not inside named type");
1491 return error_mark_node;
1494 for (field = TYPE_FIELDS (type);
1495 field != NULL_TREE;
1496 field = DECL_CHAIN (field))
1498 tree decl;
1499 tree ref;
1501 if (DECL_ARTIFICIAL (field))
1502 continue;
1503 if (TREE_CODE (field) != FIELD_DECL)
1505 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1506 "have non-static data members", field);
1507 continue;
1510 if (TREE_PRIVATE (field))
1511 permerror (input_location, "private member %q+#D in anonymous union", field);
1512 else if (TREE_PROTECTED (field))
1513 permerror (input_location, "protected member %q+#D in anonymous union", field);
1515 if (processing_template_decl)
1516 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1517 DECL_NAME (field), NULL_TREE);
1518 else
1519 ref = build_class_member_access_expr (object, field, NULL_TREE,
1520 false, tf_warning_or_error);
1522 if (DECL_NAME (field))
1524 tree base;
1526 decl = build_decl (input_location,
1527 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1528 DECL_ANON_UNION_VAR_P (decl) = 1;
1529 DECL_ARTIFICIAL (decl) = 1;
1531 base = get_base_address (object);
1532 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1533 TREE_STATIC (decl) = TREE_STATIC (base);
1534 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1536 SET_DECL_VALUE_EXPR (decl, ref);
1537 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1539 decl = pushdecl (decl);
1541 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1542 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1543 else
1544 decl = 0;
1546 if (main_decl == NULL_TREE)
1547 main_decl = decl;
1550 return main_decl;
1553 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1554 anonymous union, then all members must be laid out together. PUBLIC_P
1555 is nonzero if this union is not declared static. */
1557 void
1558 finish_anon_union (tree anon_union_decl)
1560 tree type;
1561 tree main_decl;
1562 bool public_p;
1564 if (anon_union_decl == error_mark_node)
1565 return;
1567 type = TREE_TYPE (anon_union_decl);
1568 public_p = TREE_PUBLIC (anon_union_decl);
1570 /* The VAR_DECL's context is the same as the TYPE's context. */
1571 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1573 if (TYPE_FIELDS (type) == NULL_TREE)
1574 return;
1576 if (public_p)
1578 error ("namespace-scope anonymous aggregates must be static");
1579 return;
1582 main_decl = build_anon_union_vars (type, anon_union_decl);
1583 if (main_decl == error_mark_node)
1584 return;
1585 if (main_decl == NULL_TREE)
1587 warning (0, "anonymous union with no members");
1588 return;
1591 if (!processing_template_decl)
1593 /* Use main_decl to set the mangled name. */
1594 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1595 maybe_commonize_var (anon_union_decl);
1596 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1597 mangle_decl (anon_union_decl);
1598 DECL_NAME (anon_union_decl) = NULL_TREE;
1601 pushdecl (anon_union_decl);
1602 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1605 /* Auxiliary functions to make type signatures for
1606 `operator new' and `operator delete' correspond to
1607 what compiler will be expecting. */
1609 tree
1610 coerce_new_type (tree type)
1612 int e = 0;
1613 tree args = TYPE_ARG_TYPES (type);
1615 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1617 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1619 e = 1;
1620 error ("%<operator new%> must return type %qT", ptr_type_node);
1623 if (args && args != void_list_node)
1625 if (TREE_PURPOSE (args))
1627 /* [basic.stc.dynamic.allocation]
1629 The first parameter shall not have an associated default
1630 argument. */
1631 error ("the first parameter of %<operator new%> cannot "
1632 "have a default argument");
1633 /* Throw away the default argument. */
1634 TREE_PURPOSE (args) = NULL_TREE;
1637 if (!same_type_p (TREE_VALUE (args), size_type_node))
1639 e = 2;
1640 args = TREE_CHAIN (args);
1643 else
1644 e = 2;
1646 if (e == 2)
1647 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1648 "as first parameter", size_type_node);
1650 switch (e)
1652 case 2:
1653 args = tree_cons (NULL_TREE, size_type_node, args);
1654 /* Fall through. */
1655 case 1:
1656 type = build_exception_variant
1657 (build_function_type (ptr_type_node, args),
1658 TYPE_RAISES_EXCEPTIONS (type));
1659 /* Fall through. */
1660 default:;
1662 return type;
1665 tree
1666 coerce_delete_type (tree type)
1668 int e = 0;
1669 tree args = TYPE_ARG_TYPES (type);
1671 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1673 if (!same_type_p (TREE_TYPE (type), void_type_node))
1675 e = 1;
1676 error ("%<operator delete%> must return type %qT", void_type_node);
1679 if (!args || args == void_list_node
1680 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1682 e = 2;
1683 if (args && args != void_list_node)
1684 args = TREE_CHAIN (args);
1685 error ("%<operator delete%> takes type %qT as first parameter",
1686 ptr_type_node);
1688 switch (e)
1690 case 2:
1691 args = tree_cons (NULL_TREE, ptr_type_node, args);
1692 /* Fall through. */
1693 case 1:
1694 type = build_exception_variant
1695 (build_function_type (void_type_node, args),
1696 TYPE_RAISES_EXCEPTIONS (type));
1697 /* Fall through. */
1698 default:;
1701 return type;
1704 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1705 and mark them as needed. */
1707 static void
1708 mark_vtable_entries (tree decl)
1710 tree fnaddr;
1711 unsigned HOST_WIDE_INT idx;
1713 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1714 idx, fnaddr)
1716 tree fn;
1718 STRIP_NOPS (fnaddr);
1720 if (TREE_CODE (fnaddr) != ADDR_EXPR
1721 && TREE_CODE (fnaddr) != FDESC_EXPR)
1722 /* This entry is an offset: a virtual base class offset, a
1723 virtual call offset, an RTTI offset, etc. */
1724 continue;
1726 fn = TREE_OPERAND (fnaddr, 0);
1727 TREE_ADDRESSABLE (fn) = 1;
1728 /* When we don't have vcall offsets, we output thunks whenever
1729 we output the vtables that contain them. With vcall offsets,
1730 we know all the thunks we'll need when we emit a virtual
1731 function, so we emit the thunks there instead. */
1732 if (DECL_THUNK_P (fn))
1733 use_thunk (fn, /*emit_p=*/0);
1734 mark_used (fn);
1738 /* Set DECL up to have the closest approximation of "initialized common"
1739 linkage available. */
1741 void
1742 comdat_linkage (tree decl)
1744 if (flag_weak)
1745 make_decl_one_only (decl, cxx_comdat_group (decl));
1746 else if (TREE_CODE (decl) == FUNCTION_DECL
1747 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1748 /* We can just emit function and compiler-generated variables
1749 statically; having multiple copies is (for the most part) only
1750 a waste of space.
1752 There are two correctness issues, however: the address of a
1753 template instantiation with external linkage should be the
1754 same, independent of what translation unit asks for the
1755 address, and this will not hold when we emit multiple copies of
1756 the function. However, there's little else we can do.
1758 Also, by default, the typeinfo implementation assumes that
1759 there will be only one copy of the string used as the name for
1760 each type. Therefore, if weak symbols are unavailable, the
1761 run-time library should perform a more conservative check; it
1762 should perform a string comparison, rather than an address
1763 comparison. */
1764 TREE_PUBLIC (decl) = 0;
1765 else
1767 /* Static data member template instantiations, however, cannot
1768 have multiple copies. */
1769 if (DECL_INITIAL (decl) == 0
1770 || DECL_INITIAL (decl) == error_mark_node)
1771 DECL_COMMON (decl) = 1;
1772 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1774 DECL_COMMON (decl) = 1;
1775 DECL_INITIAL (decl) = error_mark_node;
1777 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1779 /* We can't do anything useful; leave vars for explicit
1780 instantiation. */
1781 DECL_EXTERNAL (decl) = 1;
1782 DECL_NOT_REALLY_EXTERN (decl) = 0;
1786 DECL_COMDAT (decl) = 1;
1789 /* For win32 we also want to put explicit instantiations in
1790 linkonce sections, so that they will be merged with implicit
1791 instantiations; otherwise we get duplicate symbol errors.
1792 For Darwin we do not want explicit instantiations to be
1793 linkonce. */
1795 void
1796 maybe_make_one_only (tree decl)
1798 /* We used to say that this was not necessary on targets that support weak
1799 symbols, because the implicit instantiations will defer to the explicit
1800 one. However, that's not actually the case in SVR4; a strong definition
1801 after a weak one is an error. Also, not making explicit
1802 instantiations one_only means that we can end up with two copies of
1803 some template instantiations. */
1804 if (! flag_weak)
1805 return;
1807 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1808 we can get away with not emitting them if they aren't used. We need
1809 to for variables so that cp_finish_decl will update their linkage,
1810 because their DECL_INITIAL may not have been set properly yet. */
1812 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1813 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1814 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1816 make_decl_one_only (decl, cxx_comdat_group (decl));
1818 if (VAR_P (decl))
1820 varpool_node *node = varpool_node::get_create (decl);
1821 DECL_COMDAT (decl) = 1;
1822 /* Mark it needed so we don't forget to emit it. */
1823 node->forced_by_abi = true;
1824 TREE_USED (decl) = 1;
1829 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1830 This predicate will give the right answer during parsing of the
1831 function, which other tests may not. */
1833 bool
1834 vague_linkage_p (tree decl)
1836 /* Unfortunately, import_export_decl has not always been called
1837 before the function is processed, so we cannot simply check
1838 DECL_COMDAT. */
1839 if (DECL_COMDAT (decl)
1840 || (((TREE_CODE (decl) == FUNCTION_DECL
1841 && DECL_DECLARED_INLINE_P (decl))
1842 || (DECL_LANG_SPECIFIC (decl)
1843 && DECL_TEMPLATE_INSTANTIATION (decl)))
1844 && TREE_PUBLIC (decl)))
1845 return true;
1846 else if (DECL_FUNCTION_SCOPE_P (decl))
1847 /* A local static in an inline effectively has vague linkage. */
1848 return (TREE_STATIC (decl)
1849 && vague_linkage_p (DECL_CONTEXT (decl)));
1850 else
1851 return false;
1854 /* Determine whether or not we want to specifically import or export CTYPE,
1855 using various heuristics. */
1857 static void
1858 import_export_class (tree ctype)
1860 /* -1 for imported, 1 for exported. */
1861 int import_export = 0;
1863 /* It only makes sense to call this function at EOF. The reason is
1864 that this function looks at whether or not the first non-inline
1865 non-abstract virtual member function has been defined in this
1866 translation unit. But, we can't possibly know that until we've
1867 seen the entire translation unit. */
1868 gcc_assert (at_eof);
1870 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1871 return;
1873 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1874 we will have CLASSTYPE_INTERFACE_ONLY set but not
1875 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1876 heuristic because someone will supply a #pragma implementation
1877 elsewhere, and deducing it here would produce a conflict. */
1878 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1879 return;
1881 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1882 import_export = -1;
1883 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1884 import_export = 1;
1885 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1886 && !flag_implicit_templates)
1887 /* For a template class, without -fimplicit-templates, check the
1888 repository. If the virtual table is assigned to this
1889 translation unit, then export the class; otherwise, import
1890 it. */
1891 import_export = repo_export_class_p (ctype) ? 1 : -1;
1892 else if (TYPE_POLYMORPHIC_P (ctype))
1894 /* The ABI specifies that the virtual table and associated
1895 information are emitted with the key method, if any. */
1896 tree method = CLASSTYPE_KEY_METHOD (ctype);
1897 /* If weak symbol support is not available, then we must be
1898 careful not to emit the vtable when the key function is
1899 inline. An inline function can be defined in multiple
1900 translation units. If we were to emit the vtable in each
1901 translation unit containing a definition, we would get
1902 multiple definition errors at link-time. */
1903 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1904 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1907 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1908 a definition anywhere else. */
1909 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1910 import_export = 0;
1912 /* Allow back ends the chance to overrule the decision. */
1913 if (targetm.cxx.import_export_class)
1914 import_export = targetm.cxx.import_export_class (ctype, import_export);
1916 if (import_export)
1918 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1919 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1923 /* Return true if VAR has already been provided to the back end; in that
1924 case VAR should not be modified further by the front end. */
1925 static bool
1926 var_finalized_p (tree var)
1928 return varpool_node::get_create (var)->definition;
1931 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1932 must be emitted in this translation unit. Mark it as such. */
1934 void
1935 mark_needed (tree decl)
1937 TREE_USED (decl) = 1;
1938 if (TREE_CODE (decl) == FUNCTION_DECL)
1940 /* Extern inline functions don't become needed when referenced.
1941 If we know a method will be emitted in other TU and no new
1942 functions can be marked reachable, just use the external
1943 definition. */
1944 struct cgraph_node *node = cgraph_node::get_create (decl);
1945 node->forced_by_abi = true;
1947 /* #pragma interface and -frepo code can call mark_needed for
1948 maybe-in-charge 'tors; mark the clones as well. */
1949 tree clone;
1950 FOR_EACH_CLONE (clone, decl)
1951 mark_needed (clone);
1953 else if (TREE_CODE (decl) == VAR_DECL)
1955 varpool_node *node = varpool_node::get_create (decl);
1956 /* C++ frontend use mark_decl_references to force COMDAT variables
1957 to be output that might appear dead otherwise. */
1958 node->forced_by_abi = true;
1962 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1963 returns true if a definition of this entity should be provided in
1964 this object file. Callers use this function to determine whether
1965 or not to let the back end know that a definition of DECL is
1966 available in this translation unit. */
1968 bool
1969 decl_needed_p (tree decl)
1971 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1972 /* This function should only be called at the end of the translation
1973 unit. We cannot be sure of whether or not something will be
1974 COMDAT until that point. */
1975 gcc_assert (at_eof);
1977 /* All entities with external linkage that are not COMDAT should be
1978 emitted; they may be referred to from other object files. */
1979 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1980 return true;
1981 /* If this entity was used, let the back end see it; it will decide
1982 whether or not to emit it into the object file. */
1983 if (TREE_USED (decl))
1984 return true;
1985 /* Functions marked "dllexport" must be emitted so that they are
1986 visible to other DLLs. */
1987 if (flag_keep_inline_dllexport
1988 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1989 return true;
1990 /* Virtual functions might be needed for devirtualization. */
1991 if (flag_devirtualize
1992 && TREE_CODE (decl) == FUNCTION_DECL
1993 && DECL_VIRTUAL_P (decl))
1994 return true;
1995 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1996 reference to DECL might cause it to be emitted later. */
1997 return false;
2000 /* If necessary, write out the vtables for the dynamic class CTYPE.
2001 Returns true if any vtables were emitted. */
2003 static bool
2004 maybe_emit_vtables (tree ctype)
2006 tree vtbl;
2007 tree primary_vtbl;
2008 int needed = 0;
2009 varpool_node *current = NULL, *last = NULL;
2011 /* If the vtables for this class have already been emitted there is
2012 nothing more to do. */
2013 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2014 if (var_finalized_p (primary_vtbl))
2015 return false;
2016 /* Ignore dummy vtables made by get_vtable_decl. */
2017 if (TREE_TYPE (primary_vtbl) == void_type_node)
2018 return false;
2020 /* On some targets, we cannot determine the key method until the end
2021 of the translation unit -- which is when this function is
2022 called. */
2023 if (!targetm.cxx.key_method_may_be_inline ())
2024 determine_key_method (ctype);
2026 /* See if any of the vtables are needed. */
2027 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2029 import_export_decl (vtbl);
2030 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2031 needed = 1;
2033 if (!needed)
2035 /* If the references to this class' vtables are optimized away,
2036 still emit the appropriate debugging information. See
2037 dfs_debug_mark. */
2038 if (DECL_COMDAT (primary_vtbl)
2039 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2040 note_debug_info_needed (ctype);
2041 return false;
2044 /* The ABI requires that we emit all of the vtables if we emit any
2045 of them. */
2046 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2048 /* Mark entities references from the virtual table as used. */
2049 mark_vtable_entries (vtbl);
2051 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2053 vec<tree, va_gc> *cleanups = NULL;
2054 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2055 LOOKUP_NORMAL);
2057 /* It had better be all done at compile-time. */
2058 gcc_assert (!expr && !cleanups);
2061 /* Write it out. */
2062 DECL_EXTERNAL (vtbl) = 0;
2063 rest_of_decl_compilation (vtbl, 1, 1);
2065 /* Because we're only doing syntax-checking, we'll never end up
2066 actually marking the variable as written. */
2067 if (flag_syntax_only)
2068 TREE_ASM_WRITTEN (vtbl) = 1;
2069 else if (DECL_ONE_ONLY (vtbl))
2071 current = varpool_node::get_create (vtbl);
2072 if (last)
2073 current->add_to_same_comdat_group (last);
2074 last = current;
2078 /* Since we're writing out the vtable here, also write the debug
2079 info. */
2080 note_debug_info_needed (ctype);
2082 return true;
2085 /* A special return value from type_visibility meaning internal
2086 linkage. */
2088 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2090 /* walk_tree helper function for type_visibility. */
2092 static tree
2093 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2095 int *vis_p = (int *)data;
2096 if (! TYPE_P (*tp))
2098 *walk_subtrees = 0;
2100 else if (OVERLOAD_TYPE_P (*tp)
2101 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2103 *vis_p = VISIBILITY_ANON;
2104 return *tp;
2106 else if (CLASS_TYPE_P (*tp)
2107 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2108 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2109 return NULL;
2112 /* Returns the visibility of TYPE, which is the minimum visibility of its
2113 component types. */
2115 static int
2116 type_visibility (tree type)
2118 int vis = VISIBILITY_DEFAULT;
2119 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2120 return vis;
2123 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2124 specified (or if VISIBILITY is static). If TMPL is true, this
2125 constraint is for a template argument, and takes precedence
2126 over explicitly-specified visibility on the template. */
2128 static void
2129 constrain_visibility (tree decl, int visibility, bool tmpl)
2131 if (visibility == VISIBILITY_ANON)
2133 /* extern "C" declarations aren't affected by the anonymous
2134 namespace. */
2135 if (!DECL_EXTERN_C_P (decl))
2137 TREE_PUBLIC (decl) = 0;
2138 DECL_WEAK (decl) = 0;
2139 DECL_COMMON (decl) = 0;
2140 if (TREE_CODE (decl) == FUNCTION_DECL
2141 || TREE_CODE (decl) == VAR_DECL)
2143 struct symtab_node *snode = symtab_node::get (decl);
2145 if (snode)
2146 snode->set_comdat_group (NULL);
2148 DECL_INTERFACE_KNOWN (decl) = 1;
2149 if (DECL_LANG_SPECIFIC (decl))
2150 DECL_NOT_REALLY_EXTERN (decl) = 1;
2153 else if (visibility > DECL_VISIBILITY (decl)
2154 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2156 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2157 /* This visibility was not specified. */
2158 DECL_VISIBILITY_SPECIFIED (decl) = false;
2162 /* Constrain the visibility of DECL based on the visibility of its template
2163 arguments. */
2165 static void
2166 constrain_visibility_for_template (tree decl, tree targs)
2168 /* If this is a template instantiation, check the innermost
2169 template args for visibility constraints. The outer template
2170 args are covered by the class check. */
2171 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2172 int i;
2173 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2175 int vis = 0;
2177 tree arg = TREE_VEC_ELT (args, i-1);
2178 if (TYPE_P (arg))
2179 vis = type_visibility (arg);
2180 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2182 STRIP_NOPS (arg);
2183 if (TREE_CODE (arg) == ADDR_EXPR)
2184 arg = TREE_OPERAND (arg, 0);
2185 if (VAR_OR_FUNCTION_DECL_P (arg))
2187 if (! TREE_PUBLIC (arg))
2188 vis = VISIBILITY_ANON;
2189 else
2190 vis = DECL_VISIBILITY (arg);
2193 if (vis)
2194 constrain_visibility (decl, vis, true);
2198 /* Like c_determine_visibility, but with additional C++-specific
2199 behavior.
2201 Function-scope entities can rely on the function's visibility because
2202 it is set in start_preparsed_function.
2204 Class-scope entities cannot rely on the class's visibility until the end
2205 of the enclosing class definition.
2207 Note that because namespaces have multiple independent definitions,
2208 namespace visibility is handled elsewhere using the #pragma visibility
2209 machinery rather than by decorating the namespace declaration.
2211 The goal is for constraints from the type to give a diagnostic, and
2212 other constraints to be applied silently. */
2214 void
2215 determine_visibility (tree decl)
2217 tree class_type = NULL_TREE;
2218 bool use_template;
2219 bool orig_visibility_specified;
2220 enum symbol_visibility orig_visibility;
2222 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2224 /* Only relevant for names with external linkage. */
2225 if (!TREE_PUBLIC (decl))
2226 return;
2228 /* Cloned constructors and destructors get the same visibility as
2229 the underlying function. That should be set up in
2230 maybe_clone_body. */
2231 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2233 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2234 orig_visibility = DECL_VISIBILITY (decl);
2236 if (TREE_CODE (decl) == TYPE_DECL)
2238 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2239 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2240 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2241 use_template = 1;
2242 else
2243 use_template = 0;
2245 else if (DECL_LANG_SPECIFIC (decl))
2246 use_template = DECL_USE_TEMPLATE (decl);
2247 else
2248 use_template = 0;
2250 /* If DECL is a member of a class, visibility specifiers on the
2251 class can influence the visibility of the DECL. */
2252 if (DECL_CLASS_SCOPE_P (decl))
2253 class_type = DECL_CONTEXT (decl);
2254 else
2256 /* Not a class member. */
2258 /* Virtual tables have DECL_CONTEXT set to their associated class,
2259 so they are automatically handled above. */
2260 gcc_assert (!VAR_P (decl)
2261 || !DECL_VTABLE_OR_VTT_P (decl));
2263 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2265 /* Local statics and classes get the visibility of their
2266 containing function by default, except that
2267 -fvisibility-inlines-hidden doesn't affect them. */
2268 tree fn = DECL_CONTEXT (decl);
2269 if (DECL_VISIBILITY_SPECIFIED (fn))
2271 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2272 DECL_VISIBILITY_SPECIFIED (decl) =
2273 DECL_VISIBILITY_SPECIFIED (fn);
2275 else
2277 if (DECL_CLASS_SCOPE_P (fn))
2278 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2279 else if (determine_hidden_inline (fn))
2281 DECL_VISIBILITY (decl) = default_visibility;
2282 DECL_VISIBILITY_SPECIFIED (decl) =
2283 visibility_options.inpragma;
2285 else
2287 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2288 DECL_VISIBILITY_SPECIFIED (decl) =
2289 DECL_VISIBILITY_SPECIFIED (fn);
2293 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2294 but have no TEMPLATE_INFO, so don't try to check it. */
2295 use_template = 0;
2297 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2298 && flag_visibility_ms_compat)
2300 /* Under -fvisibility-ms-compat, types are visible by default,
2301 even though their contents aren't. */
2302 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2303 int underlying_vis = type_visibility (underlying_type);
2304 if (underlying_vis == VISIBILITY_ANON
2305 || (CLASS_TYPE_P (underlying_type)
2306 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2307 constrain_visibility (decl, underlying_vis, false);
2308 else
2309 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2311 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2313 /* tinfo visibility is based on the type it's for. */
2314 constrain_visibility
2315 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2317 /* Give the target a chance to override the visibility associated
2318 with DECL. */
2319 if (TREE_PUBLIC (decl)
2320 && !DECL_REALLY_EXTERN (decl)
2321 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2322 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2323 targetm.cxx.determine_class_data_visibility (decl);
2325 else if (use_template)
2326 /* Template instantiations and specializations get visibility based
2327 on their template unless they override it with an attribute. */;
2328 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2330 if (determine_hidden_inline (decl))
2331 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2332 else
2334 /* Set default visibility to whatever the user supplied with
2335 #pragma GCC visibility or a namespace visibility attribute. */
2336 DECL_VISIBILITY (decl) = default_visibility;
2337 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2342 if (use_template)
2344 /* If the specialization doesn't specify visibility, use the
2345 visibility from the template. */
2346 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2347 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2348 : DECL_TEMPLATE_INFO (decl));
2349 tree args = TI_ARGS (tinfo);
2350 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2351 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2352 : DECL_ATTRIBUTES (decl));
2354 if (args != error_mark_node)
2356 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2358 if (!DECL_VISIBILITY_SPECIFIED (decl))
2360 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2361 && determine_hidden_inline (decl))
2362 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2363 else
2365 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2366 DECL_VISIBILITY_SPECIFIED (decl)
2367 = DECL_VISIBILITY_SPECIFIED (pattern);
2371 if (args
2372 /* Template argument visibility outweighs #pragma or namespace
2373 visibility, but not an explicit attribute. */
2374 && !lookup_attribute ("visibility", attribs))
2376 int depth = TMPL_ARGS_DEPTH (args);
2377 if (DECL_VISIBILITY_SPECIFIED (decl))
2379 /* A class template member with explicit visibility
2380 overrides the class visibility, so we need to apply
2381 all the levels of template args directly. */
2382 int i;
2383 for (i = 1; i <= depth; ++i)
2385 tree lev = TMPL_ARGS_LEVEL (args, i);
2386 constrain_visibility_for_template (decl, lev);
2389 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2390 /* Limit visibility based on its template arguments. */
2391 constrain_visibility_for_template (decl, args);
2396 if (class_type)
2397 determine_visibility_from_class (decl, class_type);
2399 if (decl_anon_ns_mem_p (decl))
2400 /* Names in an anonymous namespace get internal linkage.
2401 This might change once we implement export. */
2402 constrain_visibility (decl, VISIBILITY_ANON, false);
2403 else if (TREE_CODE (decl) != TYPE_DECL)
2405 /* Propagate anonymity from type to decl. */
2406 int tvis = type_visibility (TREE_TYPE (decl));
2407 if (tvis == VISIBILITY_ANON
2408 || ! DECL_VISIBILITY_SPECIFIED (decl))
2409 constrain_visibility (decl, tvis, false);
2411 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2412 /* DR 757: A type without linkage shall not be used as the type of a
2413 variable or function with linkage, unless
2414 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2415 o the variable or function is not used (3.2 [basic.def.odr]) or is
2416 defined in the same translation unit.
2418 Since non-extern "C" decls need to be defined in the same
2419 translation unit, we can make the type internal. */
2420 constrain_visibility (decl, VISIBILITY_ANON, false);
2422 /* If visibility changed and DECL already has DECL_RTL, ensure
2423 symbol flags are updated. */
2424 if ((DECL_VISIBILITY (decl) != orig_visibility
2425 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2426 && ((VAR_P (decl) && TREE_STATIC (decl))
2427 || TREE_CODE (decl) == FUNCTION_DECL)
2428 && DECL_RTL_SET_P (decl))
2429 make_decl_rtl (decl);
2432 /* By default, static data members and function members receive
2433 the visibility of their containing class. */
2435 static void
2436 determine_visibility_from_class (tree decl, tree class_type)
2438 if (DECL_VISIBILITY_SPECIFIED (decl))
2439 return;
2441 if (determine_hidden_inline (decl))
2442 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2443 else
2445 /* Default to the class visibility. */
2446 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2447 DECL_VISIBILITY_SPECIFIED (decl)
2448 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2451 /* Give the target a chance to override the visibility associated
2452 with DECL. */
2453 if (VAR_P (decl)
2454 && (DECL_TINFO_P (decl)
2455 || (DECL_VTABLE_OR_VTT_P (decl)
2456 /* Construction virtual tables are not exported because
2457 they cannot be referred to from other object files;
2458 their name is not standardized by the ABI. */
2459 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2460 && TREE_PUBLIC (decl)
2461 && !DECL_REALLY_EXTERN (decl)
2462 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2463 targetm.cxx.determine_class_data_visibility (decl);
2466 /* Returns true iff DECL is an inline that should get hidden visibility
2467 because of -fvisibility-inlines-hidden. */
2469 static bool
2470 determine_hidden_inline (tree decl)
2472 return (visibility_options.inlines_hidden
2473 /* Don't do this for inline templates; specializations might not be
2474 inline, and we don't want them to inherit the hidden
2475 visibility. We'll set it here for all inline instantiations. */
2476 && !processing_template_decl
2477 && TREE_CODE (decl) == FUNCTION_DECL
2478 && DECL_DECLARED_INLINE_P (decl)
2479 && (! DECL_LANG_SPECIFIC (decl)
2480 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2483 /* Constrain the visibility of a class TYPE based on the visibility of its
2484 field types. Warn if any fields require lesser visibility. */
2486 void
2487 constrain_class_visibility (tree type)
2489 tree binfo;
2490 tree t;
2491 int i;
2493 int vis = type_visibility (type);
2495 if (vis == VISIBILITY_ANON
2496 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2497 return;
2499 /* Don't warn about visibility if the class has explicit visibility. */
2500 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2501 vis = VISIBILITY_INTERNAL;
2503 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2504 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2506 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2507 int subvis = type_visibility (ftype);
2509 if (subvis == VISIBILITY_ANON)
2511 if (!in_main_input_context ())
2512 warning (0, "\
2513 %qT has a field %qD whose type uses the anonymous namespace",
2514 type, t);
2516 else if (MAYBE_CLASS_TYPE_P (ftype)
2517 && vis < VISIBILITY_HIDDEN
2518 && subvis >= VISIBILITY_HIDDEN)
2519 warning (OPT_Wattributes, "\
2520 %qT declared with greater visibility than the type of its field %qD",
2521 type, t);
2524 binfo = TYPE_BINFO (type);
2525 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2527 int subvis = type_visibility (TREE_TYPE (t));
2529 if (subvis == VISIBILITY_ANON)
2531 if (!in_main_input_context())
2532 warning (0, "\
2533 %qT has a base %qT whose type uses the anonymous namespace",
2534 type, TREE_TYPE (t));
2536 else if (vis < VISIBILITY_HIDDEN
2537 && subvis >= VISIBILITY_HIDDEN)
2538 warning (OPT_Wattributes, "\
2539 %qT declared with greater visibility than its base %qT",
2540 type, TREE_TYPE (t));
2544 /* Functions for adjusting the visibility of a tagged type and its nested
2545 types and declarations when it gets a name for linkage purposes from a
2546 typedef. */
2548 static void bt_reset_linkage_1 (binding_entry, void *);
2549 static void bt_reset_linkage_2 (binding_entry, void *);
2551 /* First reset the visibility of all the types. */
2553 static void
2554 reset_type_linkage_1 (tree type)
2556 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2557 if (CLASS_TYPE_P (type))
2558 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2559 bt_reset_linkage_1, NULL);
2561 static void
2562 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2564 reset_type_linkage_1 (b->type);
2567 /* Then reset the visibility of any static data members or member
2568 functions that use those types. */
2570 static void
2571 reset_decl_linkage (tree decl)
2573 if (TREE_PUBLIC (decl))
2574 return;
2575 if (DECL_CLONED_FUNCTION_P (decl))
2576 return;
2577 TREE_PUBLIC (decl) = true;
2578 DECL_INTERFACE_KNOWN (decl) = false;
2579 determine_visibility (decl);
2580 tentative_decl_linkage (decl);
2582 static void
2583 reset_type_linkage_2 (tree type)
2585 if (CLASS_TYPE_P (type))
2587 if (tree vt = CLASSTYPE_VTABLES (type))
2589 tree name = mangle_vtbl_for_type (type);
2590 DECL_NAME (vt) = name;
2591 SET_DECL_ASSEMBLER_NAME (vt, name);
2592 reset_decl_linkage (vt);
2594 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2596 tree name = mangle_typeinfo_for_type (type);
2597 DECL_NAME (ti) = name;
2598 SET_DECL_ASSEMBLER_NAME (ti, name);
2599 TREE_TYPE (name) = type;
2600 reset_decl_linkage (ti);
2602 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2603 if (TREE_CODE (m) == VAR_DECL)
2604 reset_decl_linkage (m);
2605 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2606 reset_decl_linkage (m);
2607 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2608 bt_reset_linkage_2, NULL);
2611 static void
2612 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2614 reset_type_linkage_2 (b->type);
2616 void
2617 reset_type_linkage (tree type)
2619 reset_type_linkage_1 (type);
2620 reset_type_linkage_2 (type);
2623 /* Set up our initial idea of what the linkage of DECL should be. */
2625 void
2626 tentative_decl_linkage (tree decl)
2628 if (DECL_INTERFACE_KNOWN (decl))
2629 /* We've already made a decision as to how this function will
2630 be handled. */;
2631 else if (vague_linkage_p (decl))
2633 if (TREE_CODE (decl) == FUNCTION_DECL
2634 && decl_defined_p (decl))
2636 DECL_EXTERNAL (decl) = 1;
2637 DECL_NOT_REALLY_EXTERN (decl) = 1;
2638 note_vague_linkage_fn (decl);
2639 /* A non-template inline function with external linkage will
2640 always be COMDAT. As we must eventually determine the
2641 linkage of all functions, and as that causes writes to
2642 the data mapped in from the PCH file, it's advantageous
2643 to mark the functions at this point. */
2644 if (DECL_DECLARED_INLINE_P (decl)
2645 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2646 || DECL_DEFAULTED_FN (decl)))
2648 /* This function must have external linkage, as
2649 otherwise DECL_INTERFACE_KNOWN would have been
2650 set. */
2651 gcc_assert (TREE_PUBLIC (decl));
2652 comdat_linkage (decl);
2653 DECL_INTERFACE_KNOWN (decl) = 1;
2656 else if (TREE_CODE (decl) == VAR_DECL)
2657 maybe_commonize_var (decl);
2661 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2662 for DECL has not already been determined, do so now by setting
2663 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2664 function is called entities with vague linkage whose definitions
2665 are available must have TREE_PUBLIC set.
2667 If this function decides to place DECL in COMDAT, it will set
2668 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2669 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2670 callers defer that decision until it is clear that DECL is actually
2671 required. */
2673 void
2674 import_export_decl (tree decl)
2676 int emit_p;
2677 bool comdat_p;
2678 bool import_p;
2679 tree class_type = NULL_TREE;
2681 if (DECL_INTERFACE_KNOWN (decl))
2682 return;
2684 /* We cannot determine what linkage to give to an entity with vague
2685 linkage until the end of the file. For example, a virtual table
2686 for a class will be defined if and only if the key method is
2687 defined in this translation unit. As a further example, consider
2688 that when compiling a translation unit that uses PCH file with
2689 "-frepo" it would be incorrect to make decisions about what
2690 entities to emit when building the PCH; those decisions must be
2691 delayed until the repository information has been processed. */
2692 gcc_assert (at_eof);
2693 /* Object file linkage for explicit instantiations is handled in
2694 mark_decl_instantiated. For static variables in functions with
2695 vague linkage, maybe_commonize_var is used.
2697 Therefore, the only declarations that should be provided to this
2698 function are those with external linkage that are:
2700 * implicit instantiations of function templates
2702 * inline function
2704 * implicit instantiations of static data members of class
2705 templates
2707 * virtual tables
2709 * typeinfo objects
2711 Furthermore, all entities that reach this point must have a
2712 definition available in this translation unit.
2714 The following assertions check these conditions. */
2715 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2716 /* Any code that creates entities with TREE_PUBLIC cleared should
2717 also set DECL_INTERFACE_KNOWN. */
2718 gcc_assert (TREE_PUBLIC (decl));
2719 if (TREE_CODE (decl) == FUNCTION_DECL)
2720 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2721 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2722 || DECL_DECLARED_INLINE_P (decl));
2723 else
2724 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2725 || DECL_VTABLE_OR_VTT_P (decl)
2726 || DECL_TINFO_P (decl));
2727 /* Check that a definition of DECL is available in this translation
2728 unit. */
2729 gcc_assert (!DECL_REALLY_EXTERN (decl));
2731 /* Assume that DECL will not have COMDAT linkage. */
2732 comdat_p = false;
2733 /* Assume that DECL will not be imported into this translation
2734 unit. */
2735 import_p = false;
2737 /* See if the repository tells us whether or not to emit DECL in
2738 this translation unit. */
2739 emit_p = repo_emit_p (decl);
2740 if (emit_p == 0)
2741 import_p = true;
2742 else if (emit_p == 1)
2744 /* The repository indicates that this entity should be defined
2745 here. Make sure the back end honors that request. */
2746 mark_needed (decl);
2747 /* Output the definition as an ordinary strong definition. */
2748 DECL_EXTERNAL (decl) = 0;
2749 DECL_INTERFACE_KNOWN (decl) = 1;
2750 return;
2753 if (import_p)
2754 /* We have already decided what to do with this DECL; there is no
2755 need to check anything further. */
2757 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2759 class_type = DECL_CONTEXT (decl);
2760 import_export_class (class_type);
2761 if (TYPE_FOR_JAVA (class_type))
2762 import_p = true;
2763 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2764 && CLASSTYPE_INTERFACE_ONLY (class_type))
2765 import_p = true;
2766 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2767 && !CLASSTYPE_USE_TEMPLATE (class_type)
2768 && CLASSTYPE_KEY_METHOD (class_type)
2769 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2770 /* The ABI requires that all virtual tables be emitted with
2771 COMDAT linkage. However, on systems where COMDAT symbols
2772 don't show up in the table of contents for a static
2773 archive, or on systems without weak symbols (where we
2774 approximate COMDAT linkage by using internal linkage), the
2775 linker will report errors about undefined symbols because
2776 it will not see the virtual table definition. Therefore,
2777 in the case that we know that the virtual table will be
2778 emitted in only one translation unit, we make the virtual
2779 table an ordinary definition with external linkage. */
2780 DECL_EXTERNAL (decl) = 0;
2781 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2783 /* CLASS_TYPE is being exported from this translation unit,
2784 so DECL should be defined here. */
2785 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2786 /* If a class is declared in a header with the "extern
2787 template" extension, then it will not be instantiated,
2788 even in translation units that would normally require
2789 it. Often such classes are explicitly instantiated in
2790 one translation unit. Therefore, the explicit
2791 instantiation must be made visible to other translation
2792 units. */
2793 DECL_EXTERNAL (decl) = 0;
2794 else
2796 /* The generic C++ ABI says that class data is always
2797 COMDAT, even if there is a key function. Some
2798 variants (e.g., the ARM EABI) says that class data
2799 only has COMDAT linkage if the class data might be
2800 emitted in more than one translation unit. When the
2801 key method can be inline and is inline, we still have
2802 to arrange for comdat even though
2803 class_data_always_comdat is false. */
2804 if (!CLASSTYPE_KEY_METHOD (class_type)
2805 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2806 || targetm.cxx.class_data_always_comdat ())
2808 /* The ABI requires COMDAT linkage. Normally, we
2809 only emit COMDAT things when they are needed;
2810 make sure that we realize that this entity is
2811 indeed needed. */
2812 comdat_p = true;
2813 mark_needed (decl);
2817 else if (!flag_implicit_templates
2818 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2819 import_p = true;
2820 else
2821 comdat_p = true;
2823 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2825 tree type = TREE_TYPE (DECL_NAME (decl));
2826 if (CLASS_TYPE_P (type))
2828 class_type = type;
2829 import_export_class (type);
2830 if (CLASSTYPE_INTERFACE_KNOWN (type)
2831 && TYPE_POLYMORPHIC_P (type)
2832 && CLASSTYPE_INTERFACE_ONLY (type)
2833 /* If -fno-rtti was specified, then we cannot be sure
2834 that RTTI information will be emitted with the
2835 virtual table of the class, so we must emit it
2836 wherever it is used. */
2837 && flag_rtti)
2838 import_p = true;
2839 else
2841 if (CLASSTYPE_INTERFACE_KNOWN (type)
2842 && !CLASSTYPE_INTERFACE_ONLY (type))
2844 comdat_p = (targetm.cxx.class_data_always_comdat ()
2845 || (CLASSTYPE_KEY_METHOD (type)
2846 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2847 mark_needed (decl);
2848 if (!flag_weak)
2850 comdat_p = false;
2851 DECL_EXTERNAL (decl) = 0;
2854 else
2855 comdat_p = true;
2858 else
2859 comdat_p = true;
2861 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2863 /* DECL is an implicit instantiation of a function or static
2864 data member. */
2865 if ((flag_implicit_templates
2866 && !flag_use_repository)
2867 || (flag_implicit_inline_templates
2868 && TREE_CODE (decl) == FUNCTION_DECL
2869 && DECL_DECLARED_INLINE_P (decl)))
2870 comdat_p = true;
2871 else
2872 /* If we are not implicitly generating templates, then mark
2873 this entity as undefined in this translation unit. */
2874 import_p = true;
2876 else if (DECL_FUNCTION_MEMBER_P (decl))
2878 if (!DECL_DECLARED_INLINE_P (decl))
2880 tree ctype = DECL_CONTEXT (decl);
2881 import_export_class (ctype);
2882 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2884 DECL_NOT_REALLY_EXTERN (decl)
2885 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2886 || (DECL_DECLARED_INLINE_P (decl)
2887 && ! flag_implement_inlines
2888 && !DECL_VINDEX (decl)));
2890 if (!DECL_NOT_REALLY_EXTERN (decl))
2891 DECL_EXTERNAL (decl) = 1;
2893 /* Always make artificials weak. */
2894 if (DECL_ARTIFICIAL (decl) && flag_weak)
2895 comdat_p = true;
2896 else
2897 maybe_make_one_only (decl);
2900 else
2901 comdat_p = true;
2903 else
2904 comdat_p = true;
2906 if (import_p)
2908 /* If we are importing DECL into this translation unit, mark is
2909 an undefined here. */
2910 DECL_EXTERNAL (decl) = 1;
2911 DECL_NOT_REALLY_EXTERN (decl) = 0;
2913 else if (comdat_p)
2915 /* If we decided to put DECL in COMDAT, mark it accordingly at
2916 this point. */
2917 comdat_linkage (decl);
2920 DECL_INTERFACE_KNOWN (decl) = 1;
2923 /* Return an expression that performs the destruction of DECL, which
2924 must be a VAR_DECL whose type has a non-trivial destructor, or is
2925 an array whose (innermost) elements have a non-trivial destructor. */
2927 tree
2928 build_cleanup (tree decl)
2930 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2931 gcc_assert (clean != NULL_TREE);
2932 return clean;
2935 /* Returns the initialization guard variable for the variable DECL,
2936 which has static storage duration. */
2938 tree
2939 get_guard (tree decl)
2941 tree sname;
2942 tree guard;
2944 sname = mangle_guard_variable (decl);
2945 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2946 if (! guard)
2948 tree guard_type;
2950 /* We use a type that is big enough to contain a mutex as well
2951 as an integer counter. */
2952 guard_type = targetm.cxx.guard_type ();
2953 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2954 VAR_DECL, sname, guard_type);
2956 /* The guard should have the same linkage as what it guards. */
2957 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2958 TREE_STATIC (guard) = TREE_STATIC (decl);
2959 DECL_COMMON (guard) = DECL_COMMON (decl);
2960 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2961 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2962 if (DECL_ONE_ONLY (decl))
2963 make_decl_one_only (guard, cxx_comdat_group (guard));
2964 if (TREE_PUBLIC (decl))
2965 DECL_WEAK (guard) = DECL_WEAK (decl);
2966 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2967 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2969 DECL_ARTIFICIAL (guard) = 1;
2970 DECL_IGNORED_P (guard) = 1;
2971 TREE_USED (guard) = 1;
2972 pushdecl_top_level_and_finish (guard, NULL_TREE);
2974 return guard;
2977 /* Return those bits of the GUARD variable that should be set when the
2978 guarded entity is actually initialized. */
2980 static tree
2981 get_guard_bits (tree guard)
2983 if (!targetm.cxx.guard_mask_bit ())
2985 /* We only set the first byte of the guard, in order to leave room
2986 for a mutex in the high-order bits. */
2987 guard = build1 (ADDR_EXPR,
2988 build_pointer_type (TREE_TYPE (guard)),
2989 guard);
2990 guard = build1 (NOP_EXPR,
2991 build_pointer_type (char_type_node),
2992 guard);
2993 guard = build1 (INDIRECT_REF, char_type_node, guard);
2996 return guard;
2999 /* Return an expression which determines whether or not the GUARD
3000 variable has already been initialized. */
3002 tree
3003 get_guard_cond (tree guard)
3005 tree guard_value;
3007 /* Check to see if the GUARD is zero. */
3008 guard = get_guard_bits (guard);
3010 /* Mask off all but the low bit. */
3011 if (targetm.cxx.guard_mask_bit ())
3013 guard_value = integer_one_node;
3014 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3015 guard_value = convert (TREE_TYPE (guard), guard_value);
3016 guard = cp_build_binary_op (input_location,
3017 BIT_AND_EXPR, guard, guard_value,
3018 tf_warning_or_error);
3021 guard_value = integer_zero_node;
3022 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3023 guard_value = convert (TREE_TYPE (guard), guard_value);
3024 return cp_build_binary_op (input_location,
3025 EQ_EXPR, guard, guard_value,
3026 tf_warning_or_error);
3029 /* Return an expression which sets the GUARD variable, indicating that
3030 the variable being guarded has been initialized. */
3032 tree
3033 set_guard (tree guard)
3035 tree guard_init;
3037 /* Set the GUARD to one. */
3038 guard = get_guard_bits (guard);
3039 guard_init = integer_one_node;
3040 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3041 guard_init = convert (TREE_TYPE (guard), guard_init);
3042 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3043 tf_warning_or_error);
3046 /* Returns true iff we can tell that VAR does not have a dynamic
3047 initializer. */
3049 static bool
3050 var_defined_without_dynamic_init (tree var)
3052 /* If it's defined in another TU, we can't tell. */
3053 if (DECL_EXTERNAL (var))
3054 return false;
3055 /* If it has a non-trivial destructor, registering the destructor
3056 counts as dynamic initialization. */
3057 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3058 return false;
3059 /* If it's in this TU, its initializer has been processed. */
3060 gcc_assert (DECL_INITIALIZED_P (var));
3061 /* If it has no initializer or a constant one, it's not dynamic. */
3062 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3063 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3066 /* Returns true iff VAR is a variable that needs uses to be
3067 wrapped for possible dynamic initialization. */
3069 static bool
3070 var_needs_tls_wrapper (tree var)
3072 return (!error_operand_p (var)
3073 && DECL_THREAD_LOCAL_P (var)
3074 && !DECL_GNU_TLS_P (var)
3075 && !DECL_FUNCTION_SCOPE_P (var)
3076 && !var_defined_without_dynamic_init (var));
3079 /* Get the FUNCTION_DECL for the shared TLS init function for this
3080 translation unit. */
3082 static tree
3083 get_local_tls_init_fn (void)
3085 tree sname = get_identifier ("__tls_init");
3086 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3087 if (!fn)
3089 fn = build_lang_decl (FUNCTION_DECL, sname,
3090 build_function_type (void_type_node,
3091 void_list_node));
3092 SET_DECL_LANGUAGE (fn, lang_c);
3093 TREE_PUBLIC (fn) = false;
3094 DECL_ARTIFICIAL (fn) = true;
3095 mark_used (fn);
3096 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3098 return fn;
3101 /* Get a FUNCTION_DECL for the init function for the thread_local
3102 variable VAR. The init function will be an alias to the function
3103 that initializes all the non-local TLS variables in the translation
3104 unit. The init function is only used by the wrapper function. */
3106 static tree
3107 get_tls_init_fn (tree var)
3109 /* Only C++11 TLS vars need this init fn. */
3110 if (!var_needs_tls_wrapper (var))
3111 return NULL_TREE;
3113 /* If -fno-extern-tls-init, assume that we don't need to call
3114 a tls init function for a variable defined in another TU. */
3115 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3116 return NULL_TREE;
3118 #ifdef ASM_OUTPUT_DEF
3119 /* If the variable is internal, or if we can't generate aliases,
3120 call the local init function directly. */
3121 if (!TREE_PUBLIC (var))
3122 #endif
3123 return get_local_tls_init_fn ();
3125 tree sname = mangle_tls_init_fn (var);
3126 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3127 if (!fn)
3129 fn = build_lang_decl (FUNCTION_DECL, sname,
3130 build_function_type (void_type_node,
3131 void_list_node));
3132 SET_DECL_LANGUAGE (fn, lang_c);
3133 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3134 DECL_ARTIFICIAL (fn) = true;
3135 DECL_COMDAT (fn) = DECL_COMDAT (var);
3136 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3137 if (DECL_ONE_ONLY (var))
3138 make_decl_one_only (fn, cxx_comdat_group (fn));
3139 if (TREE_PUBLIC (var))
3141 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3142 /* If the variable is defined somewhere else and might have static
3143 initialization, make the init function a weak reference. */
3144 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3145 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3146 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3147 && DECL_EXTERNAL (var))
3148 declare_weak (fn);
3149 else
3150 DECL_WEAK (fn) = DECL_WEAK (var);
3152 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3153 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3154 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3155 DECL_IGNORED_P (fn) = 1;
3156 mark_used (fn);
3158 DECL_BEFRIENDING_CLASSES (fn) = var;
3160 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3162 return fn;
3165 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3166 variable VAR. The wrapper function calls the init function (if any) for
3167 VAR and then returns a reference to VAR. The wrapper function is used
3168 in place of VAR everywhere VAR is mentioned. */
3170 tree
3171 get_tls_wrapper_fn (tree var)
3173 /* Only C++11 TLS vars need this wrapper fn. */
3174 if (!var_needs_tls_wrapper (var))
3175 return NULL_TREE;
3177 tree sname = mangle_tls_wrapper_fn (var);
3178 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3179 if (!fn)
3181 /* A named rvalue reference is an lvalue, so the wrapper should
3182 always return an lvalue reference. */
3183 tree type = non_reference (TREE_TYPE (var));
3184 type = build_reference_type (type);
3185 tree fntype = build_function_type (type, void_list_node);
3186 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3187 SET_DECL_LANGUAGE (fn, lang_c);
3188 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3189 DECL_ARTIFICIAL (fn) = true;
3190 DECL_IGNORED_P (fn) = 1;
3191 /* The wrapper is inline and emitted everywhere var is used. */
3192 DECL_DECLARED_INLINE_P (fn) = true;
3193 if (TREE_PUBLIC (var))
3195 comdat_linkage (fn);
3196 #ifdef HAVE_GAS_HIDDEN
3197 /* Make the wrapper bind locally; there's no reason to share
3198 the wrapper between multiple shared objects. */
3199 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3200 DECL_VISIBILITY_SPECIFIED (fn) = true;
3201 #endif
3203 if (!TREE_PUBLIC (fn))
3204 DECL_INTERFACE_KNOWN (fn) = true;
3205 mark_used (fn);
3206 note_vague_linkage_fn (fn);
3208 #if 0
3209 /* We want CSE to commonize calls to the wrapper, but marking it as
3210 pure is unsafe since it has side-effects. I guess we need a new
3211 ECF flag even weaker than ECF_PURE. FIXME! */
3212 DECL_PURE_P (fn) = true;
3213 #endif
3215 DECL_BEFRIENDING_CLASSES (fn) = var;
3217 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3219 return fn;
3222 /* At EOF, generate the definition for the TLS wrapper function FN:
3224 T& var_wrapper() {
3225 if (init_fn) init_fn();
3226 return var;
3227 } */
3229 static void
3230 generate_tls_wrapper (tree fn)
3232 tree var = DECL_BEFRIENDING_CLASSES (fn);
3234 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3235 tree body = begin_function_body ();
3236 /* Only call the init fn if there might be one. */
3237 if (tree init_fn = get_tls_init_fn (var))
3239 tree if_stmt = NULL_TREE;
3240 /* If init_fn is a weakref, make sure it exists before calling. */
3241 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3243 if_stmt = begin_if_stmt ();
3244 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3245 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3246 NE_EXPR, addr, nullptr_node,
3247 tf_warning_or_error);
3248 finish_if_stmt_cond (cond, if_stmt);
3250 finish_expr_stmt (build_cxx_call
3251 (init_fn, 0, NULL, tf_warning_or_error));
3252 if (if_stmt)
3254 finish_then_clause (if_stmt);
3255 finish_if_stmt (if_stmt);
3258 else
3259 /* If there's no initialization, the wrapper is a constant function. */
3260 TREE_READONLY (fn) = true;
3261 finish_return_stmt (convert_from_reference (var));
3262 finish_function_body (body);
3263 expand_or_defer_fn (finish_function (0));
3266 /* Start the process of running a particular set of global constructors
3267 or destructors. Subroutine of do_[cd]tors. Also called from
3268 vtv_start_verification_constructor_init_function. */
3270 static tree
3271 start_objects (int method_type, int initp)
3273 tree body;
3274 tree fndecl;
3275 char type[14];
3277 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3279 if (initp != DEFAULT_INIT_PRIORITY)
3281 char joiner;
3283 #ifdef JOINER
3284 joiner = JOINER;
3285 #else
3286 joiner = '_';
3287 #endif
3289 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3291 else
3292 sprintf (type, "sub_%c", method_type);
3294 fndecl = build_lang_decl (FUNCTION_DECL,
3295 get_file_function_name (type),
3296 build_function_type_list (void_type_node,
3297 NULL_TREE));
3298 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3300 TREE_PUBLIC (current_function_decl) = 0;
3302 /* Mark as artificial because it's not explicitly in the user's
3303 source code. */
3304 DECL_ARTIFICIAL (current_function_decl) = 1;
3306 /* Mark this declaration as used to avoid spurious warnings. */
3307 TREE_USED (current_function_decl) = 1;
3309 /* Mark this function as a global constructor or destructor. */
3310 if (method_type == 'I')
3311 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3312 else
3313 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3315 body = begin_compound_stmt (BCS_FN_BODY);
3317 return body;
3320 /* Finish the process of running a particular set of global constructors
3321 or destructors. Subroutine of do_[cd]tors. */
3323 static void
3324 finish_objects (int method_type, int initp, tree body)
3326 tree fn;
3328 /* Finish up. */
3329 finish_compound_stmt (body);
3330 fn = finish_function (0);
3332 if (method_type == 'I')
3334 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3335 decl_init_priority_insert (fn, initp);
3337 else
3339 DECL_STATIC_DESTRUCTOR (fn) = 1;
3340 decl_fini_priority_insert (fn, initp);
3343 expand_or_defer_fn (fn);
3346 /* The names of the parameters to the function created to handle
3347 initializations and destructions for objects with static storage
3348 duration. */
3349 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3350 #define PRIORITY_IDENTIFIER "__priority"
3352 /* The name of the function we create to handle initializations and
3353 destructions for objects with static storage duration. */
3354 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3356 /* The declaration for the __INITIALIZE_P argument. */
3357 static GTY(()) tree initialize_p_decl;
3359 /* The declaration for the __PRIORITY argument. */
3360 static GTY(()) tree priority_decl;
3362 /* The declaration for the static storage duration function. */
3363 static GTY(()) tree ssdf_decl;
3365 /* All the static storage duration functions created in this
3366 translation unit. */
3367 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3369 /* A map from priority levels to information about that priority
3370 level. There may be many such levels, so efficient lookup is
3371 important. */
3372 static splay_tree priority_info_map;
3374 /* Begins the generation of the function that will handle all
3375 initialization and destruction of objects with static storage
3376 duration. The function generated takes two parameters of type
3377 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3378 nonzero, it performs initializations. Otherwise, it performs
3379 destructions. It only performs those initializations or
3380 destructions with the indicated __PRIORITY. The generated function
3381 returns no value.
3383 It is assumed that this function will only be called once per
3384 translation unit. */
3386 static tree
3387 start_static_storage_duration_function (unsigned count)
3389 tree type;
3390 tree body;
3391 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3393 /* Create the identifier for this function. It will be of the form
3394 SSDF_IDENTIFIER_<number>. */
3395 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3397 type = build_function_type_list (void_type_node,
3398 integer_type_node, integer_type_node,
3399 NULL_TREE);
3401 /* Create the FUNCTION_DECL itself. */
3402 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3403 get_identifier (id),
3404 type);
3405 TREE_PUBLIC (ssdf_decl) = 0;
3406 DECL_ARTIFICIAL (ssdf_decl) = 1;
3408 /* Put this function in the list of functions to be called from the
3409 static constructors and destructors. */
3410 if (!ssdf_decls)
3412 vec_alloc (ssdf_decls, 32);
3414 /* Take this opportunity to initialize the map from priority
3415 numbers to information about that priority level. */
3416 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3417 /*delete_key_fn=*/0,
3418 /*delete_value_fn=*/
3419 (splay_tree_delete_value_fn) &free);
3421 /* We always need to generate functions for the
3422 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3423 priorities later, we'll be sure to find the
3424 DEFAULT_INIT_PRIORITY. */
3425 get_priority_info (DEFAULT_INIT_PRIORITY);
3428 vec_safe_push (ssdf_decls, ssdf_decl);
3430 /* Create the argument list. */
3431 initialize_p_decl = cp_build_parm_decl
3432 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3433 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3434 TREE_USED (initialize_p_decl) = 1;
3435 priority_decl = cp_build_parm_decl
3436 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3437 DECL_CONTEXT (priority_decl) = ssdf_decl;
3438 TREE_USED (priority_decl) = 1;
3440 DECL_CHAIN (initialize_p_decl) = priority_decl;
3441 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3443 /* Put the function in the global scope. */
3444 pushdecl (ssdf_decl);
3446 /* Start the function itself. This is equivalent to declaring the
3447 function as:
3449 static void __ssdf (int __initialize_p, init __priority_p);
3451 It is static because we only need to call this function from the
3452 various constructor and destructor functions for this module. */
3453 start_preparsed_function (ssdf_decl,
3454 /*attrs=*/NULL_TREE,
3455 SF_PRE_PARSED);
3457 /* Set up the scope of the outermost block in the function. */
3458 body = begin_compound_stmt (BCS_FN_BODY);
3460 return body;
3463 /* Finish the generation of the function which performs initialization
3464 and destruction of objects with static storage duration. After
3465 this point, no more such objects can be created. */
3467 static void
3468 finish_static_storage_duration_function (tree body)
3470 /* Close out the function. */
3471 finish_compound_stmt (body);
3472 expand_or_defer_fn (finish_function (0));
3475 /* Return the information about the indicated PRIORITY level. If no
3476 code to handle this level has yet been generated, generate the
3477 appropriate prologue. */
3479 static priority_info
3480 get_priority_info (int priority)
3482 priority_info pi;
3483 splay_tree_node n;
3485 n = splay_tree_lookup (priority_info_map,
3486 (splay_tree_key) priority);
3487 if (!n)
3489 /* Create a new priority information structure, and insert it
3490 into the map. */
3491 pi = XNEW (struct priority_info_s);
3492 pi->initializations_p = 0;
3493 pi->destructions_p = 0;
3494 splay_tree_insert (priority_info_map,
3495 (splay_tree_key) priority,
3496 (splay_tree_value) pi);
3498 else
3499 pi = (priority_info) n->value;
3501 return pi;
3504 /* The effective initialization priority of a DECL. */
3506 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3507 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3508 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3510 /* Whether a DECL needs a guard to protect it against multiple
3511 initialization. */
3513 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3514 || DECL_ONE_ONLY (decl) \
3515 || DECL_WEAK (decl)))
3517 /* Called from one_static_initialization_or_destruction(),
3518 via walk_tree.
3519 Walks the initializer list of a global variable and looks for
3520 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3521 and that have their DECL_CONTEXT() == NULL.
3522 For each such temporary variable, set their DECL_CONTEXT() to
3523 the current function. This is necessary because otherwise
3524 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3525 when trying to refer to a temporary variable that does not have
3526 it's DECL_CONTECT() properly set. */
3527 static tree
3528 fix_temporary_vars_context_r (tree *node,
3529 int * /*unused*/,
3530 void * /*unused1*/)
3532 gcc_assert (current_function_decl);
3534 if (TREE_CODE (*node) == BIND_EXPR)
3536 tree var;
3538 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3539 if (VAR_P (var)
3540 && !DECL_NAME (var)
3541 && DECL_ARTIFICIAL (var)
3542 && !DECL_CONTEXT (var))
3543 DECL_CONTEXT (var) = current_function_decl;
3546 return NULL_TREE;
3549 /* Set up to handle the initialization or destruction of DECL. If
3550 INITP is nonzero, we are initializing the variable. Otherwise, we
3551 are destroying it. */
3553 static void
3554 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3556 tree guard_if_stmt = NULL_TREE;
3557 tree guard;
3559 /* If we are supposed to destruct and there's a trivial destructor,
3560 nothing has to be done. */
3561 if (!initp
3562 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3563 return;
3565 /* Trick the compiler into thinking we are at the file and line
3566 where DECL was declared so that error-messages make sense, and so
3567 that the debugger will show somewhat sensible file and line
3568 information. */
3569 input_location = DECL_SOURCE_LOCATION (decl);
3571 /* Make sure temporary variables in the initialiser all have
3572 their DECL_CONTEXT() set to a value different from NULL_TREE.
3573 This can happen when global variables initialisers are built.
3574 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3575 the temporary variables that might have been generated in the
3576 accompagning initialisers is NULL_TREE, meaning the variables have been
3577 declared in the global namespace.
3578 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3579 of the temporaries are set to the current function decl. */
3580 cp_walk_tree_without_duplicates (&init,
3581 fix_temporary_vars_context_r,
3582 NULL);
3584 /* Because of:
3586 [class.access.spec]
3588 Access control for implicit calls to the constructors,
3589 the conversion functions, or the destructor called to
3590 create and destroy a static data member is performed as
3591 if these calls appeared in the scope of the member's
3592 class.
3594 we pretend we are in a static member function of the class of
3595 which the DECL is a member. */
3596 if (member_p (decl))
3598 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3599 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3602 /* Assume we don't need a guard. */
3603 guard = NULL_TREE;
3604 /* We need a guard if this is an object with external linkage that
3605 might be initialized in more than one place. (For example, a
3606 static data member of a template, when the data member requires
3607 construction.) */
3608 if (NEEDS_GUARD_P (decl))
3610 tree guard_cond;
3612 guard = get_guard (decl);
3614 /* When using __cxa_atexit, we just check the GUARD as we would
3615 for a local static. */
3616 if (flag_use_cxa_atexit)
3618 /* When using __cxa_atexit, we never try to destroy
3619 anything from a static destructor. */
3620 gcc_assert (initp);
3621 guard_cond = get_guard_cond (guard);
3623 /* If we don't have __cxa_atexit, then we will be running
3624 destructors from .fini sections, or their equivalents. So,
3625 we need to know how many times we've tried to initialize this
3626 object. We do initializations only if the GUARD is zero,
3627 i.e., if we are the first to initialize the variable. We do
3628 destructions only if the GUARD is one, i.e., if we are the
3629 last to destroy the variable. */
3630 else if (initp)
3631 guard_cond
3632 = cp_build_binary_op (input_location,
3633 EQ_EXPR,
3634 cp_build_unary_op (PREINCREMENT_EXPR,
3635 guard,
3636 /*noconvert=*/1,
3637 tf_warning_or_error),
3638 integer_one_node,
3639 tf_warning_or_error);
3640 else
3641 guard_cond
3642 = cp_build_binary_op (input_location,
3643 EQ_EXPR,
3644 cp_build_unary_op (PREDECREMENT_EXPR,
3645 guard,
3646 /*noconvert=*/1,
3647 tf_warning_or_error),
3648 integer_zero_node,
3649 tf_warning_or_error);
3651 guard_if_stmt = begin_if_stmt ();
3652 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3656 /* If we're using __cxa_atexit, we have not already set the GUARD,
3657 so we must do so now. */
3658 if (guard && initp && flag_use_cxa_atexit)
3659 finish_expr_stmt (set_guard (guard));
3661 /* Perform the initialization or destruction. */
3662 if (initp)
3664 if (init)
3666 finish_expr_stmt (init);
3667 if (flag_sanitize & SANITIZE_ADDRESS)
3669 varpool_node *vnode = varpool_node::get (decl);
3670 if (vnode)
3671 vnode->dynamically_initialized = 1;
3675 /* If we're using __cxa_atexit, register a function that calls the
3676 destructor for the object. */
3677 if (flag_use_cxa_atexit)
3678 finish_expr_stmt (register_dtor_fn (decl));
3680 else
3681 finish_expr_stmt (build_cleanup (decl));
3683 /* Finish the guard if-stmt, if necessary. */
3684 if (guard)
3686 finish_then_clause (guard_if_stmt);
3687 finish_if_stmt (guard_if_stmt);
3690 /* Now that we're done with DECL we don't need to pretend to be a
3691 member of its class any longer. */
3692 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3693 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3696 /* Generate code to do the initialization or destruction of the decls in VARS,
3697 a TREE_LIST of VAR_DECL with static storage duration.
3698 Whether initialization or destruction is performed is specified by INITP. */
3700 static void
3701 do_static_initialization_or_destruction (tree vars, bool initp)
3703 tree node, init_if_stmt, cond;
3705 /* Build the outer if-stmt to check for initialization or destruction. */
3706 init_if_stmt = begin_if_stmt ();
3707 cond = initp ? integer_one_node : integer_zero_node;
3708 cond = cp_build_binary_op (input_location,
3709 EQ_EXPR,
3710 initialize_p_decl,
3711 cond,
3712 tf_warning_or_error);
3713 finish_if_stmt_cond (cond, init_if_stmt);
3715 /* To make sure dynamic construction doesn't access globals from other
3716 compilation units where they might not be yet constructed, for
3717 -fsanitize=address insert __asan_before_dynamic_init call that
3718 prevents access to either all global variables that need construction
3719 in other compilation units, or at least those that haven't been
3720 initialized yet. Variables that need dynamic construction in
3721 the current compilation unit are kept accessible. */
3722 if (flag_sanitize & SANITIZE_ADDRESS)
3723 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3725 node = vars;
3726 do {
3727 tree decl = TREE_VALUE (node);
3728 tree priority_if_stmt;
3729 int priority;
3730 priority_info pi;
3732 /* If we don't need a destructor, there's nothing to do. Avoid
3733 creating a possibly empty if-stmt. */
3734 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3736 node = TREE_CHAIN (node);
3737 continue;
3740 /* Remember that we had an initialization or finalization at this
3741 priority. */
3742 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3743 pi = get_priority_info (priority);
3744 if (initp)
3745 pi->initializations_p = 1;
3746 else
3747 pi->destructions_p = 1;
3749 /* Conditionalize this initialization on being in the right priority
3750 and being initializing/finalizing appropriately. */
3751 priority_if_stmt = begin_if_stmt ();
3752 cond = cp_build_binary_op (input_location,
3753 EQ_EXPR,
3754 priority_decl,
3755 build_int_cst (NULL_TREE, priority),
3756 tf_warning_or_error);
3757 finish_if_stmt_cond (cond, priority_if_stmt);
3759 /* Process initializers with same priority. */
3760 for (; node
3761 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3762 node = TREE_CHAIN (node))
3763 /* Do one initialization or destruction. */
3764 one_static_initialization_or_destruction (TREE_VALUE (node),
3765 TREE_PURPOSE (node), initp);
3767 /* Finish up the priority if-stmt body. */
3768 finish_then_clause (priority_if_stmt);
3769 finish_if_stmt (priority_if_stmt);
3771 } while (node);
3773 /* Revert what __asan_before_dynamic_init did by calling
3774 __asan_after_dynamic_init. */
3775 if (flag_sanitize & SANITIZE_ADDRESS)
3776 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3778 /* Finish up the init/destruct if-stmt body. */
3779 finish_then_clause (init_if_stmt);
3780 finish_if_stmt (init_if_stmt);
3783 /* VARS is a list of variables with static storage duration which may
3784 need initialization and/or finalization. Remove those variables
3785 that don't really need to be initialized or finalized, and return
3786 the resulting list. The order in which the variables appear in
3787 VARS is in reverse order of the order in which they should actually
3788 be initialized. The list we return is in the unreversed order;
3789 i.e., the first variable should be initialized first. */
3791 static tree
3792 prune_vars_needing_no_initialization (tree *vars)
3794 tree *var = vars;
3795 tree result = NULL_TREE;
3797 while (*var)
3799 tree t = *var;
3800 tree decl = TREE_VALUE (t);
3801 tree init = TREE_PURPOSE (t);
3803 /* Deal gracefully with error. */
3804 if (decl == error_mark_node)
3806 var = &TREE_CHAIN (t);
3807 continue;
3810 /* The only things that can be initialized are variables. */
3811 gcc_assert (VAR_P (decl));
3813 /* If this object is not defined, we don't need to do anything
3814 here. */
3815 if (DECL_EXTERNAL (decl))
3817 var = &TREE_CHAIN (t);
3818 continue;
3821 /* Also, if the initializer already contains errors, we can bail
3822 out now. */
3823 if (init && TREE_CODE (init) == TREE_LIST
3824 && value_member (error_mark_node, init))
3826 var = &TREE_CHAIN (t);
3827 continue;
3830 /* This variable is going to need initialization and/or
3831 finalization, so we add it to the list. */
3832 *var = TREE_CHAIN (t);
3833 TREE_CHAIN (t) = result;
3834 result = t;
3837 return result;
3840 /* Make sure we have told the back end about all the variables in
3841 VARS. */
3843 static void
3844 write_out_vars (tree vars)
3846 tree v;
3848 for (v = vars; v; v = TREE_CHAIN (v))
3850 tree var = TREE_VALUE (v);
3851 if (!var_finalized_p (var))
3853 import_export_decl (var);
3854 rest_of_decl_compilation (var, 1, 1);
3859 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3860 (otherwise) that will initialize all global objects with static
3861 storage duration having the indicated PRIORITY. */
3863 static void
3864 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3865 location_t *locus)
3867 char function_key;
3868 tree fndecl;
3869 tree body;
3870 size_t i;
3872 input_location = *locus;
3873 /* ??? */
3874 /* Was: locus->line++; */
3876 /* We use `I' to indicate initialization and `D' to indicate
3877 destruction. */
3878 function_key = constructor_p ? 'I' : 'D';
3880 /* We emit the function lazily, to avoid generating empty
3881 global constructors and destructors. */
3882 body = NULL_TREE;
3884 /* For Objective-C++, we may need to initialize metadata found in this module.
3885 This must be done _before_ any other static initializations. */
3886 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3887 && constructor_p && objc_static_init_needed_p ())
3889 body = start_objects (function_key, priority);
3890 objc_generate_static_init_call (NULL_TREE);
3893 /* Call the static storage duration function with appropriate
3894 arguments. */
3895 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3897 /* Calls to pure or const functions will expand to nothing. */
3898 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3900 tree call;
3902 if (! body)
3903 body = start_objects (function_key, priority);
3905 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3906 build_int_cst (NULL_TREE,
3907 constructor_p),
3908 build_int_cst (NULL_TREE,
3909 priority),
3910 NULL_TREE);
3911 finish_expr_stmt (call);
3915 /* Close out the function. */
3916 if (body)
3917 finish_objects (function_key, priority, body);
3920 /* Generate constructor and destructor functions for the priority
3921 indicated by N. */
3923 static int
3924 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3926 location_t *locus = (location_t *) data;
3927 int priority = (int) n->key;
3928 priority_info pi = (priority_info) n->value;
3930 /* Generate the functions themselves, but only if they are really
3931 needed. */
3932 if (pi->initializations_p)
3933 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3934 if (pi->destructions_p)
3935 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3937 /* Keep iterating. */
3938 return 0;
3941 /* Java requires that we be able to reference a local address for a
3942 method, and not be confused by PLT entries. If hidden aliases are
3943 supported, collect and return all the functions for which we should
3944 emit a hidden alias. */
3946 static hash_set<tree> *
3947 collect_candidates_for_java_method_aliases (void)
3949 struct cgraph_node *node;
3950 hash_set<tree> *candidates = NULL;
3952 #ifndef HAVE_GAS_HIDDEN
3953 return candidates;
3954 #endif
3956 FOR_EACH_FUNCTION (node)
3958 tree fndecl = node->decl;
3960 if (DECL_CLASS_SCOPE_P (fndecl)
3961 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3962 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3964 if (candidates == NULL)
3965 candidates = new hash_set<tree>;
3966 candidates->add (fndecl);
3970 return candidates;
3974 /* Java requires that we be able to reference a local address for a
3975 method, and not be confused by PLT entries. If hidden aliases are
3976 supported, emit one for each java function that we've emitted.
3977 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3978 by collect_candidates_for_java_method_aliases. */
3980 static void
3981 build_java_method_aliases (hash_set<tree> *candidates)
3983 struct cgraph_node *node;
3985 #ifndef HAVE_GAS_HIDDEN
3986 return;
3987 #endif
3989 FOR_EACH_FUNCTION (node)
3991 tree fndecl = node->decl;
3993 if (TREE_ASM_WRITTEN (fndecl)
3994 && candidates->contains (fndecl))
3996 /* Mangle the name in a predictable way; we need to reference
3997 this from a java compiled object file. */
3998 tree oid, nid, alias;
3999 const char *oname;
4000 char *nname;
4002 oid = DECL_ASSEMBLER_NAME (fndecl);
4003 oname = IDENTIFIER_POINTER (oid);
4004 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4005 nname = ACONCAT (("_ZGA", oname+2, NULL));
4006 nid = get_identifier (nname);
4008 alias = make_alias_for (fndecl, nid);
4009 TREE_PUBLIC (alias) = 1;
4010 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4012 assemble_alias (alias, oid);
4017 /* Return C++ property of T, based on given operation OP. */
4019 static int
4020 cpp_check (tree t, cpp_operation op)
4022 switch (op)
4024 case IS_ABSTRACT:
4025 return DECL_PURE_VIRTUAL_P (t);
4026 case IS_CONSTRUCTOR:
4027 return DECL_CONSTRUCTOR_P (t);
4028 case IS_DESTRUCTOR:
4029 return DECL_DESTRUCTOR_P (t);
4030 case IS_COPY_CONSTRUCTOR:
4031 return DECL_COPY_CONSTRUCTOR_P (t);
4032 case IS_TEMPLATE:
4033 return TREE_CODE (t) == TEMPLATE_DECL;
4034 case IS_TRIVIAL:
4035 return trivial_type_p (t);
4036 default:
4037 return 0;
4041 /* Collect source file references recursively, starting from NAMESPC. */
4043 static void
4044 collect_source_refs (tree namespc)
4046 tree t;
4048 if (!namespc)
4049 return;
4051 /* Iterate over names in this name space. */
4052 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4053 if (!DECL_IS_BUILTIN (t) )
4054 collect_source_ref (DECL_SOURCE_FILE (t));
4056 /* Dump siblings, if any */
4057 collect_source_refs (TREE_CHAIN (namespc));
4059 /* Dump children, if any */
4060 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4063 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4064 starting from NAMESPC. */
4066 static void
4067 collect_ada_namespace (tree namespc, const char *source_file)
4069 if (!namespc)
4070 return;
4072 /* Collect decls from this namespace */
4073 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4075 /* Collect siblings, if any */
4076 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4078 /* Collect children, if any */
4079 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4082 /* Returns true iff there is a definition available for variable or
4083 function DECL. */
4085 static bool
4086 decl_defined_p (tree decl)
4088 if (TREE_CODE (decl) == FUNCTION_DECL)
4089 return (DECL_INITIAL (decl) != NULL_TREE);
4090 else
4092 gcc_assert (VAR_P (decl));
4093 return !DECL_EXTERNAL (decl);
4097 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4099 [expr.const]
4101 An integral constant-expression can only involve ... const
4102 variables of integral or enumeration types initialized with
4103 constant expressions ...
4105 C++0x also allows constexpr variables and temporaries initialized
4106 with constant expressions. We handle the former here, but the latter
4107 are just folded away in cxx_eval_constant_expression.
4109 The standard does not require that the expression be non-volatile.
4110 G++ implements the proposed correction in DR 457. */
4112 bool
4113 decl_constant_var_p (tree decl)
4115 if (!decl_maybe_constant_var_p (decl))
4116 return false;
4118 /* We don't know if a template static data member is initialized with
4119 a constant expression until we instantiate its initializer. Even
4120 in the case of a constexpr variable, we can't treat it as a
4121 constant until its initializer is complete in case it's used in
4122 its own initializer. */
4123 mark_used (decl);
4124 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4127 /* Returns true if DECL could be a symbolic constant variable, depending on
4128 its initializer. */
4130 bool
4131 decl_maybe_constant_var_p (tree decl)
4133 tree type = TREE_TYPE (decl);
4134 if (!VAR_P (decl))
4135 return false;
4136 if (DECL_DECLARED_CONSTEXPR_P (decl))
4137 return true;
4138 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4139 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4142 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4143 called from grokfndecl and grokvardecl; in all modes it is called from
4144 cp_write_global_declarations. */
4146 void
4147 no_linkage_error (tree decl)
4149 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4150 /* In C++11 it's ok if the decl is defined. */
4151 return;
4152 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4153 if (t == NULL_TREE)
4154 /* The type that got us on no_linkage_decls must have gotten a name for
4155 linkage purposes. */;
4156 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4157 /* The type might end up having a typedef name for linkage purposes. */
4158 vec_safe_push (no_linkage_decls, decl);
4159 else if (TYPE_ANONYMOUS_P (t))
4161 bool d = false;
4162 if (cxx_dialect >= cxx11)
4163 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4164 "anonymous type, is used but never defined", decl);
4165 else if (DECL_EXTERN_C_P (decl))
4166 /* Allow this; it's pretty common in C. */;
4167 else if (TREE_CODE (decl) == VAR_DECL)
4168 /* DRs 132, 319 and 389 seem to indicate types with
4169 no linkage can only be used to declare extern "C"
4170 entities. Since it's not always an error in the
4171 ISO C++ 90 Standard, we only issue a warning. */
4172 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4173 "with no linkage used to declare variable %q#D with "
4174 "linkage", decl);
4175 else
4176 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4177 "linkage used to declare function %q#D with linkage",
4178 decl);
4179 if (d && is_typedef_decl (TYPE_NAME (t)))
4180 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4181 "to the unqualified type, so it is not used for linkage",
4182 TYPE_NAME (t));
4184 else if (cxx_dialect >= cxx11)
4185 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4186 "%qT, is used but never defined", decl, t);
4187 else if (TREE_CODE (decl) == VAR_DECL)
4188 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4189 "used to declare variable %q#D with linkage", t, decl);
4190 else
4191 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4192 "to declare function %q#D with linkage", t, decl);
4195 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4197 static void
4198 collect_all_refs (const char *source_file)
4200 collect_ada_namespace (global_namespace, source_file);
4203 /* Clear DECL_EXTERNAL for NODE. */
4205 static bool
4206 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4208 DECL_EXTERNAL (node->decl) = 0;
4209 return false;
4212 /* Build up the function to run dynamic initializers for thread_local
4213 variables in this translation unit and alias the init functions for the
4214 individual variables to it. */
4216 static void
4217 handle_tls_init (void)
4219 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4220 if (vars == NULL_TREE)
4221 return;
4223 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4225 write_out_vars (vars);
4227 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4228 boolean_type_node);
4229 TREE_PUBLIC (guard) = false;
4230 TREE_STATIC (guard) = true;
4231 DECL_ARTIFICIAL (guard) = true;
4232 DECL_IGNORED_P (guard) = true;
4233 TREE_USED (guard) = true;
4234 set_decl_tls_model (guard, decl_default_tls_model (guard));
4235 pushdecl_top_level_and_finish (guard, NULL_TREE);
4237 tree fn = get_local_tls_init_fn ();
4238 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4239 tree body = begin_function_body ();
4240 tree if_stmt = begin_if_stmt ();
4241 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4242 tf_warning_or_error);
4243 finish_if_stmt_cond (cond, if_stmt);
4244 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4245 tf_warning_or_error));
4246 for (; vars; vars = TREE_CHAIN (vars))
4248 tree var = TREE_VALUE (vars);
4249 tree init = TREE_PURPOSE (vars);
4250 one_static_initialization_or_destruction (var, init, true);
4252 #ifdef ASM_OUTPUT_DEF
4253 /* Output init aliases even with -fno-extern-tls-init. */
4254 if (TREE_PUBLIC (var))
4256 tree single_init_fn = get_tls_init_fn (var);
4257 if (single_init_fn == NULL_TREE)
4258 continue;
4259 cgraph_node *alias
4260 = cgraph_node::get_create (fn)->create_same_body_alias
4261 (single_init_fn, fn);
4262 gcc_assert (alias != NULL);
4264 #endif
4267 finish_then_clause (if_stmt);
4268 finish_if_stmt (if_stmt);
4269 finish_function_body (body);
4270 expand_or_defer_fn (finish_function (0));
4273 /* The entire file is now complete. If requested, dump everything
4274 to a file. */
4276 static void
4277 dump_tu (void)
4279 int flags;
4280 FILE *stream = dump_begin (TDI_tu, &flags);
4282 if (stream)
4284 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4285 dump_end (TDI_tu, stream);
4289 /* This routine is called at the end of compilation.
4290 Its job is to create all the code needed to initialize and
4291 destroy the global aggregates. We do the destruction
4292 first, since that way we only need to reverse the decls once. */
4294 void
4295 cp_write_global_declarations (void)
4297 tree vars;
4298 bool reconsider;
4299 size_t i;
4300 location_t locus;
4301 unsigned ssdf_count = 0;
4302 int retries = 0;
4303 tree decl;
4304 hash_set<tree> *candidates;
4306 locus = input_location;
4307 at_eof = 1;
4309 /* Bad parse errors. Just forget about it. */
4310 if (! global_bindings_p () || current_class_type
4311 || !vec_safe_is_empty (decl_namespace_list))
4312 return;
4314 /* This is the point to write out a PCH if we're doing that.
4315 In that case we do not want to do anything else. */
4316 if (pch_file)
4318 c_common_write_pch ();
4319 dump_tu ();
4320 return;
4323 symtab->process_same_body_aliases ();
4325 /* Handle -fdump-ada-spec[-slim] */
4326 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4328 if (flag_dump_ada_spec_slim)
4329 collect_source_ref (main_input_filename);
4330 else
4331 collect_source_refs (global_namespace);
4333 dump_ada_specs (collect_all_refs, cpp_check);
4336 /* FIXME - huh? was input_line -= 1;*/
4338 timevar_start (TV_PHASE_DEFERRED);
4340 /* We now have to write out all the stuff we put off writing out.
4341 These include:
4343 o Template specializations that we have not yet instantiated,
4344 but which are needed.
4345 o Initialization and destruction for non-local objects with
4346 static storage duration. (Local objects with static storage
4347 duration are initialized when their scope is first entered,
4348 and are cleaned up via atexit.)
4349 o Virtual function tables.
4351 All of these may cause others to be needed. For example,
4352 instantiating one function may cause another to be needed, and
4353 generating the initializer for an object may cause templates to be
4354 instantiated, etc., etc. */
4356 emit_support_tinfos ();
4360 tree t;
4361 tree decl;
4363 reconsider = false;
4365 /* If there are templates that we've put off instantiating, do
4366 them now. */
4367 instantiate_pending_templates (retries);
4368 ggc_collect ();
4370 /* Write out virtual tables as required. Note that writing out
4371 the virtual table for a template class may cause the
4372 instantiation of members of that class. If we write out
4373 vtables then we remove the class from our list so we don't
4374 have to look at it again. */
4376 while (keyed_classes != NULL_TREE
4377 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4379 reconsider = true;
4380 keyed_classes = TREE_CHAIN (keyed_classes);
4383 t = keyed_classes;
4384 if (t != NULL_TREE)
4386 tree next = TREE_CHAIN (t);
4388 while (next)
4390 if (maybe_emit_vtables (TREE_VALUE (next)))
4392 reconsider = true;
4393 TREE_CHAIN (t) = TREE_CHAIN (next);
4395 else
4396 t = next;
4398 next = TREE_CHAIN (t);
4402 /* Write out needed type info variables. We have to be careful
4403 looping through unemitted decls, because emit_tinfo_decl may
4404 cause other variables to be needed. New elements will be
4405 appended, and we remove from the vector those that actually
4406 get emitted. */
4407 for (i = unemitted_tinfo_decls->length ();
4408 unemitted_tinfo_decls->iterate (--i, &t);)
4409 if (emit_tinfo_decl (t))
4411 reconsider = true;
4412 unemitted_tinfo_decls->unordered_remove (i);
4415 /* The list of objects with static storage duration is built up
4416 in reverse order. We clear STATIC_AGGREGATES so that any new
4417 aggregates added during the initialization of these will be
4418 initialized in the correct order when we next come around the
4419 loop. */
4420 vars = prune_vars_needing_no_initialization (&static_aggregates);
4422 if (vars)
4424 /* We need to start a new initialization function each time
4425 through the loop. That's because we need to know which
4426 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4427 isn't computed until a function is finished, and written
4428 out. That's a deficiency in the back end. When this is
4429 fixed, these initialization functions could all become
4430 inline, with resulting performance improvements. */
4431 tree ssdf_body;
4433 /* Set the line and file, so that it is obviously not from
4434 the source file. */
4435 input_location = locus;
4436 ssdf_body = start_static_storage_duration_function (ssdf_count);
4438 /* Make sure the back end knows about all the variables. */
4439 write_out_vars (vars);
4441 /* First generate code to do all the initializations. */
4442 if (vars)
4443 do_static_initialization_or_destruction (vars, /*initp=*/true);
4445 /* Then, generate code to do all the destructions. Do these
4446 in reverse order so that the most recently constructed
4447 variable is the first destroyed. If we're using
4448 __cxa_atexit, then we don't need to do this; functions
4449 were registered at initialization time to destroy the
4450 local statics. */
4451 if (!flag_use_cxa_atexit && vars)
4453 vars = nreverse (vars);
4454 do_static_initialization_or_destruction (vars, /*initp=*/false);
4456 else
4457 vars = NULL_TREE;
4459 /* Finish up the static storage duration function for this
4460 round. */
4461 input_location = locus;
4462 finish_static_storage_duration_function (ssdf_body);
4464 /* All those initializations and finalizations might cause
4465 us to need more inline functions, more template
4466 instantiations, etc. */
4467 reconsider = true;
4468 ssdf_count++;
4469 /* ??? was: locus.line++; */
4472 /* Now do the same for thread_local variables. */
4473 handle_tls_init ();
4475 /* Go through the set of inline functions whose bodies have not
4476 been emitted yet. If out-of-line copies of these functions
4477 are required, emit them. */
4478 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4480 /* Does it need synthesizing? */
4481 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4482 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4484 /* Even though we're already at the top-level, we push
4485 there again. That way, when we pop back a few lines
4486 hence, all of our state is restored. Otherwise,
4487 finish_function doesn't clean things up, and we end
4488 up with CURRENT_FUNCTION_DECL set. */
4489 push_to_top_level ();
4490 /* The decl's location will mark where it was first
4491 needed. Save that so synthesize method can indicate
4492 where it was needed from, in case of error */
4493 input_location = DECL_SOURCE_LOCATION (decl);
4494 synthesize_method (decl);
4495 pop_from_top_level ();
4496 reconsider = true;
4499 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4500 generate_tls_wrapper (decl);
4502 if (!DECL_SAVED_TREE (decl))
4503 continue;
4505 /* We lie to the back end, pretending that some functions
4506 are not defined when they really are. This keeps these
4507 functions from being put out unnecessarily. But, we must
4508 stop lying when the functions are referenced, or if they
4509 are not comdat since they need to be put out now. If
4510 DECL_INTERFACE_KNOWN, then we have already set
4511 DECL_EXTERNAL appropriately, so there's no need to check
4512 again, and we do not want to clear DECL_EXTERNAL if a
4513 previous call to import_export_decl set it.
4515 This is done in a separate for cycle, because if some
4516 deferred function is contained in another deferred
4517 function later in deferred_fns varray,
4518 rest_of_compilation would skip this function and we
4519 really cannot expand the same function twice. */
4520 import_export_decl (decl);
4521 if (DECL_NOT_REALLY_EXTERN (decl)
4522 && DECL_INITIAL (decl)
4523 && decl_needed_p (decl))
4525 struct cgraph_node *node, *next;
4527 node = cgraph_node::get (decl);
4528 if (node->cpp_implicit_alias)
4529 node = node->get_alias_target ();
4531 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4532 NULL, true);
4533 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4534 group, we need to mark all symbols in the same comdat group
4535 that way. */
4536 if (node->same_comdat_group)
4537 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4538 next != node;
4539 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4540 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4541 NULL, true);
4544 /* If we're going to need to write this function out, and
4545 there's already a body for it, create RTL for it now.
4546 (There might be no body if this is a method we haven't
4547 gotten around to synthesizing yet.) */
4548 if (!DECL_EXTERNAL (decl)
4549 && decl_needed_p (decl)
4550 && !TREE_ASM_WRITTEN (decl)
4551 && !cgraph_node::get (decl)->definition)
4553 /* We will output the function; no longer consider it in this
4554 loop. */
4555 DECL_DEFER_OUTPUT (decl) = 0;
4556 /* Generate RTL for this function now that we know we
4557 need it. */
4558 expand_or_defer_fn (decl);
4559 /* If we're compiling -fsyntax-only pretend that this
4560 function has been written out so that we don't try to
4561 expand it again. */
4562 if (flag_syntax_only)
4563 TREE_ASM_WRITTEN (decl) = 1;
4564 reconsider = true;
4568 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4569 reconsider = true;
4571 /* Static data members are just like namespace-scope globals. */
4572 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4574 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4575 /* Don't write it out if we haven't seen a definition. */
4576 || DECL_IN_AGGR_P (decl))
4577 continue;
4578 import_export_decl (decl);
4579 /* If this static data member is needed, provide it to the
4580 back end. */
4581 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4582 DECL_EXTERNAL (decl) = 0;
4584 if (vec_safe_length (pending_statics) != 0
4585 && wrapup_global_declarations (pending_statics->address (),
4586 pending_statics->length ()))
4587 reconsider = true;
4589 retries++;
4591 while (reconsider);
4593 /* All used inline functions must have a definition at this point. */
4594 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4596 if (/* Check online inline functions that were actually used. */
4597 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4598 /* If the definition actually was available here, then the
4599 fact that the function was not defined merely represents
4600 that for some reason (use of a template repository,
4601 #pragma interface, etc.) we decided not to emit the
4602 definition here. */
4603 && !DECL_INITIAL (decl)
4604 /* Don't complain if the template was defined. */
4605 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4606 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4607 (template_for_substitution (decl)))))
4609 warning (0, "inline function %q+D used but never defined", decl);
4610 /* Avoid a duplicate warning from check_global_declaration_1. */
4611 TREE_NO_WARNING (decl) = 1;
4615 /* So must decls that use a type with no linkage. */
4616 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4617 no_linkage_error (decl);
4619 /* Then, do the Objective-C stuff. This is where all the
4620 Objective-C module stuff gets generated (symtab,
4621 class/protocol/selector lists etc). This must be done after C++
4622 templates, destructors etc. so that selectors used in C++
4623 templates are properly allocated. */
4624 if (c_dialect_objc ())
4625 objc_write_global_declarations ();
4627 /* We give C linkage to static constructors and destructors. */
4628 push_lang_context (lang_name_c);
4630 /* Generate initialization and destruction functions for all
4631 priorities for which they are required. */
4632 if (priority_info_map)
4633 splay_tree_foreach (priority_info_map,
4634 generate_ctor_and_dtor_functions_for_priority,
4635 /*data=*/&locus);
4636 else if (c_dialect_objc () && objc_static_init_needed_p ())
4637 /* If this is obj-c++ and we need a static init, call
4638 generate_ctor_or_dtor_function. */
4639 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4640 DEFAULT_INIT_PRIORITY, &locus);
4642 /* We're done with the splay-tree now. */
4643 if (priority_info_map)
4644 splay_tree_delete (priority_info_map);
4646 /* Generate any missing aliases. */
4647 maybe_apply_pending_pragma_weaks ();
4649 /* We're done with static constructors, so we can go back to "C++"
4650 linkage now. */
4651 pop_lang_context ();
4653 /* Collect candidates for Java hidden aliases. */
4654 candidates = collect_candidates_for_java_method_aliases ();
4656 timevar_stop (TV_PHASE_DEFERRED);
4657 timevar_start (TV_PHASE_OPT_GEN);
4659 if (flag_vtable_verify)
4661 vtv_recover_class_info ();
4662 vtv_compute_class_hierarchy_transitive_closure ();
4663 vtv_build_vtable_verify_fndecl ();
4666 symtab->finalize_compilation_unit ();
4668 if (flag_vtable_verify)
4670 /* Generate the special constructor initialization function that
4671 calls __VLTRegisterPairs, and give it a very high
4672 initialization priority. This must be done after
4673 finalize_compilation_unit so that we have accurate
4674 information about which vtable will actually be emitted. */
4675 vtv_generate_init_routine ();
4678 timevar_stop (TV_PHASE_OPT_GEN);
4679 timevar_start (TV_PHASE_CHECK_DBGINFO);
4681 /* Now, issue warnings about static, but not defined, functions,
4682 etc., and emit debugging information. */
4683 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4684 if (vec_safe_length (pending_statics) != 0)
4686 check_global_declarations (pending_statics->address (),
4687 pending_statics->length ());
4688 emit_debug_global_declarations (pending_statics->address (),
4689 pending_statics->length ());
4692 perform_deferred_noexcept_checks ();
4694 /* Generate hidden aliases for Java. */
4695 if (candidates)
4697 build_java_method_aliases (candidates);
4698 delete candidates;
4701 finish_repo ();
4703 /* The entire file is now complete. If requested, dump everything
4704 to a file. */
4705 dump_tu ();
4707 if (flag_detailed_statistics)
4709 dump_tree_statistics ();
4710 dump_time_statistics ();
4712 input_location = locus;
4714 #ifdef ENABLE_CHECKING
4715 validate_conversion_obstack ();
4716 #endif /* ENABLE_CHECKING */
4718 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4721 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4722 function to call in parse-tree form; it has not yet been
4723 semantically analyzed. ARGS are the arguments to the function.
4724 They have already been semantically analyzed. This may change
4725 ARGS. */
4727 tree
4728 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4729 tsubst_flags_t complain)
4731 tree orig_fn;
4732 vec<tree, va_gc> *orig_args = NULL;
4733 tree expr;
4734 tree object;
4736 orig_fn = fn;
4737 object = TREE_OPERAND (fn, 0);
4739 if (processing_template_decl)
4741 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4742 || TREE_CODE (fn) == MEMBER_REF);
4743 if (type_dependent_expression_p (fn)
4744 || any_type_dependent_arguments_p (*args))
4745 return build_nt_call_vec (fn, *args);
4747 orig_args = make_tree_vector_copy (*args);
4749 /* Transform the arguments and add the implicit "this"
4750 parameter. That must be done before the FN is transformed
4751 because we depend on the form of FN. */
4752 make_args_non_dependent (*args);
4753 object = build_non_dependent_expr (object);
4754 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4756 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4757 object = cp_build_addr_expr (object, complain);
4758 vec_safe_insert (*args, 0, object);
4760 /* Now that the arguments are done, transform FN. */
4761 fn = build_non_dependent_expr (fn);
4764 /* A qualified name corresponding to a bound pointer-to-member is
4765 represented as an OFFSET_REF:
4767 struct B { void g(); };
4768 void (B::*p)();
4769 void B::g() { (this->*p)(); } */
4770 if (TREE_CODE (fn) == OFFSET_REF)
4772 tree object_addr = cp_build_addr_expr (object, complain);
4773 fn = TREE_OPERAND (fn, 1);
4774 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4775 complain);
4776 vec_safe_insert (*args, 0, object_addr);
4779 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4780 expr = build_op_call (fn, args, complain);
4781 else
4782 expr = cp_build_function_call_vec (fn, args, complain);
4783 if (processing_template_decl && expr != error_mark_node)
4784 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4786 if (orig_args != NULL)
4787 release_tree_vector (orig_args);
4789 return expr;
4793 void
4794 check_default_args (tree x)
4796 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4797 bool saw_def = false;
4798 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4799 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4801 if (TREE_PURPOSE (arg))
4802 saw_def = true;
4803 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4805 error ("default argument missing for parameter %P of %q+#D", i, x);
4806 TREE_PURPOSE (arg) = error_mark_node;
4811 /* Return true if function DECL can be inlined. This is used to force
4812 instantiation of methods that might be interesting for inlining. */
4813 bool
4814 possibly_inlined_p (tree decl)
4816 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4817 if (DECL_UNINLINABLE (decl))
4818 return false;
4819 if (!optimize || pragma_java_exceptions)
4820 return DECL_DECLARED_INLINE_P (decl);
4821 /* When optimizing, we might inline everything when flatten
4822 attribute or heuristics inlining for size or autoinlining
4823 is used. */
4824 return true;
4827 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4828 If DECL is a specialization or implicitly declared class member,
4829 generate the actual definition. Return false if something goes
4830 wrong, true otherwise. */
4832 bool
4833 mark_used (tree decl, tsubst_flags_t complain)
4835 /* If DECL is a BASELINK for a single function, then treat it just
4836 like the DECL for the function. Otherwise, if the BASELINK is
4837 for an overloaded function, we don't know which function was
4838 actually used until after overload resolution. */
4839 if (BASELINK_P (decl))
4841 decl = BASELINK_FUNCTIONS (decl);
4842 if (really_overloaded_fn (decl))
4843 return true;
4844 decl = OVL_CURRENT (decl);
4847 /* Set TREE_USED for the benefit of -Wunused. */
4848 TREE_USED (decl) = 1;
4849 if (DECL_CLONED_FUNCTION_P (decl))
4850 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4852 /* Mark enumeration types as used. */
4853 if (TREE_CODE (decl) == CONST_DECL)
4854 used_types_insert (DECL_CONTEXT (decl));
4856 if (TREE_CODE (decl) == FUNCTION_DECL)
4857 maybe_instantiate_noexcept (decl);
4859 if (TREE_CODE (decl) == FUNCTION_DECL
4860 && DECL_DELETED_FN (decl))
4862 if (DECL_ARTIFICIAL (decl))
4864 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4865 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4867 /* We mark a lambda conversion op as deleted if we can't
4868 generate it properly; see maybe_add_lambda_conv_op. */
4869 sorry ("converting lambda which uses %<...%> to "
4870 "function pointer");
4871 return false;
4874 if (complain & tf_error)
4876 error ("use of deleted function %qD", decl);
4877 if (!maybe_explain_implicit_delete (decl))
4878 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4880 return false;
4883 /* We can only check DECL_ODR_USED on variables or functions with
4884 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4885 might need special handling for. */
4886 if (!VAR_OR_FUNCTION_DECL_P (decl)
4887 || DECL_LANG_SPECIFIC (decl) == NULL
4888 || DECL_THUNK_P (decl))
4890 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4892 if (complain & tf_error)
4893 error ("use of %qD before deduction of %<auto%>", decl);
4894 return false;
4896 return true;
4899 /* We only want to do this processing once. We don't need to keep trying
4900 to instantiate inline templates, because unit-at-a-time will make sure
4901 we get them compiled before functions that want to inline them. */
4902 if (DECL_ODR_USED (decl))
4903 return true;
4905 /* If within finish_function, defer the rest until that function
4906 finishes, otherwise it might recurse. */
4907 if (defer_mark_used_calls)
4909 vec_safe_push (deferred_mark_used_calls, decl);
4910 return true;
4913 /* Normally, we can wait until instantiation-time to synthesize DECL.
4914 However, if DECL is a static data member initialized with a constant
4915 or a constexpr function, we need it right now because a reference to
4916 such a data member or a call to such function is not value-dependent.
4917 For a function that uses auto in the return type, we need to instantiate
4918 it to find out its type. For OpenMP user defined reductions, we need
4919 them instantiated for reduction clauses which inline them by hand
4920 directly. */
4921 if (DECL_LANG_SPECIFIC (decl)
4922 && DECL_TEMPLATE_INFO (decl)
4923 && (decl_maybe_constant_var_p (decl)
4924 || (TREE_CODE (decl) == FUNCTION_DECL
4925 && (DECL_DECLARED_CONSTEXPR_P (decl)
4926 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4927 || undeduced_auto_decl (decl))
4928 && !uses_template_parms (DECL_TI_ARGS (decl)))
4930 /* Instantiating a function will result in garbage collection. We
4931 must treat this situation as if we were within the body of a
4932 function so as to avoid collecting live data only referenced from
4933 the stack (such as overload resolution candidates). */
4934 ++function_depth;
4935 instantiate_decl (decl, /*defer_ok=*/false,
4936 /*expl_inst_class_mem_p=*/false);
4937 --function_depth;
4940 if (processing_template_decl)
4941 return true;
4943 /* Check this too in case we're within fold_non_dependent_expr. */
4944 if (DECL_TEMPLATE_INFO (decl)
4945 && uses_template_parms (DECL_TI_ARGS (decl)))
4946 return true;
4948 require_deduced_type (decl);
4950 /* If we don't need a value, then we don't need to synthesize DECL. */
4951 if (cp_unevaluated_operand != 0)
4952 return true;
4954 DECL_ODR_USED (decl) = 1;
4955 if (DECL_CLONED_FUNCTION_P (decl))
4956 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4958 /* DR 757: A type without linkage shall not be used as the type of a
4959 variable or function with linkage, unless
4960 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4961 o the variable or function is not used (3.2 [basic.def.odr]) or is
4962 defined in the same translation unit. */
4963 if (cxx_dialect > cxx98
4964 && decl_linkage (decl) != lk_none
4965 && !DECL_EXTERN_C_P (decl)
4966 && !DECL_ARTIFICIAL (decl)
4967 && !decl_defined_p (decl)
4968 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4970 if (is_local_extern (decl))
4971 /* There's no way to define a local extern, and adding it to
4972 the vector interferes with GC, so give an error now. */
4973 no_linkage_error (decl);
4974 else
4975 vec_safe_push (no_linkage_decls, decl);
4978 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4979 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4980 /* Remember it, so we can check it was defined. */
4981 note_vague_linkage_fn (decl);
4983 /* Is it a synthesized method that needs to be synthesized? */
4984 if (TREE_CODE (decl) == FUNCTION_DECL
4985 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4986 && DECL_DEFAULTED_FN (decl)
4987 /* A function defaulted outside the class is synthesized either by
4988 cp_finish_decl or instantiate_decl. */
4989 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
4990 && ! DECL_INITIAL (decl))
4992 /* Defer virtual destructors so that thunks get the right
4993 linkage. */
4994 if (DECL_VIRTUAL_P (decl) && !at_eof)
4996 note_vague_linkage_fn (decl);
4997 return true;
5000 /* Remember the current location for a function we will end up
5001 synthesizing. Then we can inform the user where it was
5002 required in the case of error. */
5003 DECL_SOURCE_LOCATION (decl) = input_location;
5005 /* Synthesizing an implicitly defined member function will result in
5006 garbage collection. We must treat this situation as if we were
5007 within the body of a function so as to avoid collecting live data
5008 on the stack (such as overload resolution candidates).
5010 We could just let cp_write_global_declarations handle synthesizing
5011 this function by adding it to deferred_fns, but doing
5012 it at the use site produces better error messages. */
5013 ++function_depth;
5014 synthesize_method (decl);
5015 --function_depth;
5016 /* If this is a synthesized method we don't need to
5017 do the instantiation test below. */
5019 else if (VAR_OR_FUNCTION_DECL_P (decl)
5020 && DECL_TEMPLATE_INFO (decl)
5021 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5022 || always_instantiate_p (decl)))
5023 /* If this is a function or variable that is an instance of some
5024 template, we now know that we will need to actually do the
5025 instantiation. We check that DECL is not an explicit
5026 instantiation because that is not checked in instantiate_decl.
5028 We put off instantiating functions in order to improve compile
5029 times. Maintaining a stack of active functions is expensive,
5030 and the inliner knows to instantiate any functions it might
5031 need. Therefore, we always try to defer instantiation. */
5033 ++function_depth;
5034 instantiate_decl (decl, /*defer_ok=*/true,
5035 /*expl_inst_class_mem_p=*/false);
5036 --function_depth;
5039 return true;
5042 bool
5043 mark_used (tree decl)
5045 return mark_used (decl, tf_warning_or_error);
5048 tree
5049 vtv_start_verification_constructor_init_function (void)
5051 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5054 tree
5055 vtv_finish_verification_constructor_init_function (tree function_body)
5057 tree fn;
5059 finish_compound_stmt (function_body);
5060 fn = finish_function (0);
5061 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5062 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5064 return fn;
5067 #include "gt-cp-decl2.h"