PR c++/61659
[official-gcc.git] / gcc / cp / decl2.c
blob98897f4fb9258e4c1dd61d15e5d33c3641dd89e8
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 "pointer-set.h"
40 #include "flags.h"
41 #include "cp-tree.h"
42 #include "decl.h"
43 #include "toplev.h"
44 #include "timevar.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "c-family/c-common.h"
48 #include "c-family/c-objc.h"
49 #include "cgraph.h"
50 #include "tree-inline.h"
51 #include "c-family/c-pragma.h"
52 #include "dumpfile.h"
53 #include "intl.h"
54 #include "splay-tree.h"
55 #include "langhooks.h"
56 #include "c-family/c-ada-spec.h"
57 #include "asan.h"
59 extern cpp_reader *parse_in;
61 /* This structure contains information about the initializations
62 and/or destructions required for a particular priority level. */
63 typedef struct priority_info_s {
64 /* Nonzero if there have been any initializations at this priority
65 throughout the translation unit. */
66 int initializations_p;
67 /* Nonzero if there have been any destructions at this priority
68 throughout the translation unit. */
69 int destructions_p;
70 } *priority_info;
72 static void mark_vtable_entries (tree);
73 static bool maybe_emit_vtables (tree);
74 static bool acceptable_java_type (tree);
75 static tree start_objects (int, int);
76 static void finish_objects (int, int, tree);
77 static tree start_static_storage_duration_function (unsigned);
78 static void finish_static_storage_duration_function (tree);
79 static priority_info get_priority_info (int);
80 static void do_static_initialization_or_destruction (tree, bool);
81 static void one_static_initialization_or_destruction (tree, tree, bool);
82 static void generate_ctor_or_dtor_function (bool, int, location_t *);
83 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
84 void *);
85 static tree prune_vars_needing_no_initialization (tree *);
86 static void write_out_vars (tree);
87 static void import_export_class (tree);
88 static tree get_guard_bits (tree);
89 static void determine_visibility_from_class (tree, tree);
90 static bool determine_hidden_inline (tree);
91 static bool decl_defined_p (tree);
93 /* A list of static class variables. This is needed, because a
94 static class variable can be declared inside the class without
95 an initializer, and then initialized, statically, outside the class. */
96 static GTY(()) vec<tree, va_gc> *pending_statics;
98 /* A list of functions which were declared inline, but which we
99 may need to emit outline anyway. */
100 static GTY(()) vec<tree, va_gc> *deferred_fns;
102 /* A list of decls that use types with no linkage, which we need to make
103 sure are defined. */
104 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
106 /* Nonzero if we're done parsing and into end-of-file activities. */
108 int at_eof;
111 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
112 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
113 that apply to the function). */
115 tree
116 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
117 cp_ref_qualifier rqual)
119 tree raises;
120 tree attrs;
121 int type_quals;
122 bool late_return_type_p;
124 if (fntype == error_mark_node || ctype == error_mark_node)
125 return error_mark_node;
127 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
128 || TREE_CODE (fntype) == METHOD_TYPE);
130 type_quals = quals & ~TYPE_QUAL_RESTRICT;
131 ctype = cp_build_qualified_type (ctype, type_quals);
132 raises = TYPE_RAISES_EXCEPTIONS (fntype);
133 attrs = TYPE_ATTRIBUTES (fntype);
134 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
135 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
136 (TREE_CODE (fntype) == METHOD_TYPE
137 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
138 : TYPE_ARG_TYPES (fntype)));
139 if (attrs)
140 fntype = cp_build_type_attribute_variant (fntype, attrs);
141 if (rqual)
142 fntype = build_ref_qualified_type (fntype, rqual);
143 if (raises)
144 fntype = build_exception_variant (fntype, raises);
145 if (late_return_type_p)
146 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
148 return fntype;
151 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
152 return type changed to NEW_RET. */
154 tree
155 change_return_type (tree new_ret, tree fntype)
157 tree newtype;
158 tree args = TYPE_ARG_TYPES (fntype);
159 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
160 tree attrs = TYPE_ATTRIBUTES (fntype);
161 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
163 if (new_ret == error_mark_node)
164 return fntype;
166 if (same_type_p (new_ret, TREE_TYPE (fntype)))
167 return fntype;
169 if (TREE_CODE (fntype) == FUNCTION_TYPE)
171 newtype = build_function_type (new_ret, args);
172 newtype = apply_memfn_quals (newtype,
173 type_memfn_quals (fntype),
174 type_memfn_rqual (fntype));
176 else
177 newtype = build_method_type_directly
178 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
179 if (raises)
180 newtype = build_exception_variant (newtype, raises);
181 if (attrs)
182 newtype = cp_build_type_attribute_variant (newtype, attrs);
183 if (late_return_type_p)
184 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
186 return newtype;
189 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
190 appropriately. */
192 tree
193 cp_build_parm_decl (tree name, tree type)
195 tree parm = build_decl (input_location,
196 PARM_DECL, name, type);
197 /* DECL_ARG_TYPE is only used by the back end and the back end never
198 sees templates. */
199 if (!processing_template_decl)
200 DECL_ARG_TYPE (parm) = type_passed_as (type);
202 return parm;
205 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
206 indicated NAME. */
208 tree
209 build_artificial_parm (tree name, tree type)
211 tree parm = cp_build_parm_decl (name, type);
212 DECL_ARTIFICIAL (parm) = 1;
213 /* All our artificial parms are implicitly `const'; they cannot be
214 assigned to. */
215 TREE_READONLY (parm) = 1;
216 return parm;
219 /* Constructors for types with virtual baseclasses need an "in-charge" flag
220 saying whether this constructor is responsible for initialization of
221 virtual baseclasses or not. All destructors also need this "in-charge"
222 flag, which additionally determines whether or not the destructor should
223 free the memory for the object.
225 This function adds the "in-charge" flag to member function FN if
226 appropriate. It is called from grokclassfn and tsubst.
227 FN must be either a constructor or destructor.
229 The in-charge flag follows the 'this' parameter, and is followed by the
230 VTT parm (if any), then the user-written parms. */
232 void
233 maybe_retrofit_in_chrg (tree fn)
235 tree basetype, arg_types, parms, parm, fntype;
237 /* If we've already add the in-charge parameter don't do it again. */
238 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
239 return;
241 /* When processing templates we can't know, in general, whether or
242 not we're going to have virtual baseclasses. */
243 if (processing_template_decl)
244 return;
246 /* We don't need an in-charge parameter for constructors that don't
247 have virtual bases. */
248 if (DECL_CONSTRUCTOR_P (fn)
249 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
250 return;
252 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
253 basetype = TREE_TYPE (TREE_VALUE (arg_types));
254 arg_types = TREE_CHAIN (arg_types);
256 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
258 /* If this is a subobject constructor or destructor, our caller will
259 pass us a pointer to our VTT. */
260 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
262 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
264 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
265 DECL_CHAIN (parm) = parms;
266 parms = parm;
268 /* ...and then to TYPE_ARG_TYPES. */
269 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
271 DECL_HAS_VTT_PARM_P (fn) = 1;
274 /* Then add the in-charge parm (before the VTT parm). */
275 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
276 DECL_CHAIN (parm) = parms;
277 parms = parm;
278 arg_types = hash_tree_chain (integer_type_node, arg_types);
280 /* Insert our new parameter(s) into the list. */
281 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
283 /* And rebuild the function type. */
284 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
285 arg_types);
286 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
287 fntype = build_exception_variant (fntype,
288 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
289 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
290 fntype = (cp_build_type_attribute_variant
291 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
292 TREE_TYPE (fn) = fntype;
294 /* Now we've got the in-charge parameter. */
295 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
298 /* Classes overload their constituent function names automatically.
299 When a function name is declared in a record structure,
300 its name is changed to it overloaded name. Since names for
301 constructors and destructors can conflict, we place a leading
302 '$' for destructors.
304 CNAME is the name of the class we are grokking for.
306 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
308 FLAGS contains bits saying what's special about today's
309 arguments. DTOR_FLAG == DESTRUCTOR.
311 If FUNCTION is a destructor, then we must add the `auto-delete' field
312 as a second parameter. There is some hair associated with the fact
313 that we must "declare" this variable in the manner consistent with the
314 way the rest of the arguments were declared.
316 QUALS are the qualifiers for the this pointer. */
318 void
319 grokclassfn (tree ctype, tree function, enum overload_flags flags)
321 tree fn_name = DECL_NAME (function);
323 /* Even within an `extern "C"' block, members get C++ linkage. See
324 [dcl.link] for details. */
325 SET_DECL_LANGUAGE (function, lang_cplusplus);
327 if (fn_name == NULL_TREE)
329 error ("name missing for member function");
330 fn_name = get_identifier ("<anonymous>");
331 DECL_NAME (function) = fn_name;
334 DECL_CONTEXT (function) = ctype;
336 if (flags == DTOR_FLAG)
337 DECL_DESTRUCTOR_P (function) = 1;
339 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
340 maybe_retrofit_in_chrg (function);
343 /* Create an ARRAY_REF, checking for the user doing things backwards
344 along the way. DECLTYPE_P is for N3276, as in the parser. */
346 tree
347 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
348 bool decltype_p)
350 tree type;
351 tree expr;
352 tree orig_array_expr = array_expr;
353 tree orig_index_exp = index_exp;
355 if (error_operand_p (array_expr) || error_operand_p (index_exp))
356 return error_mark_node;
358 if (processing_template_decl)
360 if (type_dependent_expression_p (array_expr)
361 || type_dependent_expression_p (index_exp))
362 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
363 NULL_TREE, NULL_TREE);
364 array_expr = build_non_dependent_expr (array_expr);
365 index_exp = build_non_dependent_expr (index_exp);
368 type = TREE_TYPE (array_expr);
369 gcc_assert (type);
370 type = non_reference (type);
372 /* If they have an `operator[]', use that. */
373 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
375 tsubst_flags_t complain = tf_warning_or_error;
376 if (decltype_p)
377 complain |= tf_decltype;
378 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
379 index_exp, NULL_TREE, /*overload=*/NULL, complain);
381 else
383 tree p1, p2, i1, i2;
385 /* Otherwise, create an ARRAY_REF for a pointer or array type.
386 It is a little-known fact that, if `a' is an array and `i' is
387 an int, you can write `i[a]', which means the same thing as
388 `a[i]'. */
389 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
390 p1 = array_expr;
391 else
392 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
394 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
395 p2 = index_exp;
396 else
397 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
399 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
400 false);
401 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
402 false);
404 if ((p1 && i2) && (i1 && p2))
405 error ("ambiguous conversion for array subscript");
407 if (p1 && i2)
408 array_expr = p1, index_exp = i2;
409 else if (i1 && p2)
410 array_expr = p2, index_exp = i1;
411 else
413 error ("invalid types %<%T[%T]%> for array subscript",
414 type, TREE_TYPE (index_exp));
415 return error_mark_node;
418 if (array_expr == error_mark_node || index_exp == error_mark_node)
419 error ("ambiguous conversion for array subscript");
421 expr = build_array_ref (input_location, array_expr, index_exp);
423 if (processing_template_decl && expr != error_mark_node)
424 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
425 NULL_TREE, NULL_TREE);
426 return expr;
429 /* Given the cast expression EXP, checking out its validity. Either return
430 an error_mark_node if there was an unavoidable error, return a cast to
431 void for trying to delete a pointer w/ the value 0, or return the
432 call to delete. If DOING_VEC is true, we handle things differently
433 for doing an array delete.
434 Implements ARM $5.3.4. This is called from the parser. */
436 tree
437 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
438 tsubst_flags_t complain)
440 tree t, type;
442 if (exp == error_mark_node)
443 return exp;
445 if (processing_template_decl)
447 t = build_min (DELETE_EXPR, void_type_node, exp, size);
448 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
449 DELETE_EXPR_USE_VEC (t) = doing_vec;
450 TREE_SIDE_EFFECTS (t) = 1;
451 return t;
454 /* An array can't have been allocated by new, so complain. */
455 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
456 warning (0, "deleting array %q#E", exp);
458 t = build_expr_type_conversion (WANT_POINTER, exp, true);
460 if (t == NULL_TREE || t == error_mark_node)
462 error ("type %q#T argument given to %<delete%>, expected pointer",
463 TREE_TYPE (exp));
464 return error_mark_node;
467 type = TREE_TYPE (t);
469 /* As of Valley Forge, you can delete a pointer to const. */
471 /* You can't delete functions. */
472 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
474 error ("cannot delete a function. Only pointer-to-objects are "
475 "valid arguments to %<delete%>");
476 return error_mark_node;
479 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
480 if (VOID_TYPE_P (TREE_TYPE (type)))
482 warning (0, "deleting %qT is undefined", type);
483 doing_vec = 0;
486 /* Deleting a pointer with the value zero is valid and has no effect. */
487 if (integer_zerop (t))
488 return build1 (NOP_EXPR, void_type_node, t);
490 if (doing_vec)
491 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
492 sfk_deleting_destructor,
493 use_global_delete, complain);
494 else
495 return build_delete (type, t, sfk_deleting_destructor,
496 LOOKUP_NORMAL, use_global_delete,
497 complain);
500 /* Report an error if the indicated template declaration is not the
501 sort of thing that should be a member template. */
503 void
504 check_member_template (tree tmpl)
506 tree decl;
508 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
509 decl = DECL_TEMPLATE_RESULT (tmpl);
511 if (TREE_CODE (decl) == FUNCTION_DECL
512 || DECL_ALIAS_TEMPLATE_P (tmpl)
513 || (TREE_CODE (decl) == TYPE_DECL
514 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
516 /* The parser rejects template declarations in local classes
517 (with the exception of generic lambdas). */
518 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
519 /* The parser rejects any use of virtual in a function template. */
520 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
521 && DECL_VIRTUAL_P (decl)));
523 /* The debug-information generating code doesn't know what to do
524 with member templates. */
525 DECL_IGNORED_P (tmpl) = 1;
527 else
528 error ("template declaration of %q#D", decl);
531 /* Return true iff TYPE is a valid Java parameter or return type. */
533 static bool
534 acceptable_java_type (tree type)
536 if (type == error_mark_node)
537 return false;
539 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
540 return true;
541 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
543 type = TREE_TYPE (type);
544 if (TREE_CODE (type) == RECORD_TYPE)
546 tree args; int i;
547 if (! TYPE_FOR_JAVA (type))
548 return false;
549 if (! CLASSTYPE_TEMPLATE_INFO (type))
550 return true;
551 args = CLASSTYPE_TI_ARGS (type);
552 i = TREE_VEC_LENGTH (args);
553 while (--i >= 0)
555 type = TREE_VEC_ELT (args, i);
556 if (TYPE_PTR_P (type))
557 type = TREE_TYPE (type);
558 if (! TYPE_FOR_JAVA (type))
559 return false;
561 return true;
564 return false;
567 /* For a METHOD in a Java class CTYPE, return true if
568 the parameter and return types are valid Java types.
569 Otherwise, print appropriate error messages, and return false. */
571 bool
572 check_java_method (tree method)
574 bool jerr = false;
575 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
576 tree ret_type = TREE_TYPE (TREE_TYPE (method));
578 if (!acceptable_java_type (ret_type))
580 error ("Java method %qD has non-Java return type %qT",
581 method, ret_type);
582 jerr = true;
585 arg_types = TREE_CHAIN (arg_types);
586 if (DECL_HAS_IN_CHARGE_PARM_P (method))
587 arg_types = TREE_CHAIN (arg_types);
588 if (DECL_HAS_VTT_PARM_P (method))
589 arg_types = TREE_CHAIN (arg_types);
591 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
593 tree type = TREE_VALUE (arg_types);
594 if (!acceptable_java_type (type))
596 if (type != error_mark_node)
597 error ("Java method %qD has non-Java parameter type %qT",
598 method, type);
599 jerr = true;
602 return !jerr;
605 /* Sanity check: report error if this function FUNCTION is not
606 really a member of the class (CTYPE) it is supposed to belong to.
607 TEMPLATE_PARMS is used to specify the template parameters of a member
608 template passed as FUNCTION_DECL. If the member template is passed as a
609 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
610 from the declaration. If the function is not a function template, it
611 must be NULL.
612 It returns the original declaration for the function, NULL_TREE if
613 no declaration was found, error_mark_node if an error was emitted. */
615 tree
616 check_classfn (tree ctype, tree function, tree template_parms)
618 int ix;
619 bool is_template;
620 tree pushed_scope;
622 if (DECL_USE_TEMPLATE (function)
623 && !(TREE_CODE (function) == TEMPLATE_DECL
624 && DECL_TEMPLATE_SPECIALIZATION (function))
625 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
626 /* Since this is a specialization of a member template,
627 we're not going to find the declaration in the class.
628 For example, in:
630 struct S { template <typename T> void f(T); };
631 template <> void S::f(int);
633 we're not going to find `S::f(int)', but there's no
634 reason we should, either. We let our callers know we didn't
635 find the method, but we don't complain. */
636 return NULL_TREE;
638 /* Basic sanity check: for a template function, the template parameters
639 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
640 if (TREE_CODE (function) == TEMPLATE_DECL)
642 if (template_parms
643 && !comp_template_parms (template_parms,
644 DECL_TEMPLATE_PARMS (function)))
646 error ("template parameter lists provided don%'t match the "
647 "template parameters of %qD", function);
648 return error_mark_node;
650 template_parms = DECL_TEMPLATE_PARMS (function);
653 /* OK, is this a definition of a member template? */
654 is_template = (template_parms != NULL_TREE);
656 /* [temp.mem]
658 A destructor shall not be a member template. */
659 if (DECL_DESTRUCTOR_P (function) && is_template)
661 error ("destructor %qD declared as member template", function);
662 return error_mark_node;
665 /* We must enter the scope here, because conversion operators are
666 named by target type, and type equivalence relies on typenames
667 resolving within the scope of CTYPE. */
668 pushed_scope = push_scope (ctype);
669 ix = class_method_index_for_fn (complete_type (ctype), function);
670 if (ix >= 0)
672 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
673 tree fndecls, fndecl = 0;
674 bool is_conv_op;
675 const char *format = NULL;
677 for (fndecls = (*methods)[ix];
678 fndecls; fndecls = OVL_NEXT (fndecls))
680 tree p1, p2;
682 fndecl = OVL_CURRENT (fndecls);
683 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
684 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
686 /* We cannot simply call decls_match because this doesn't
687 work for static member functions that are pretending to
688 be methods, and because the name may have been changed by
689 asm("new_name"). */
691 /* Get rid of the this parameter on functions that become
692 static. */
693 if (DECL_STATIC_FUNCTION_P (fndecl)
694 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
695 p1 = TREE_CHAIN (p1);
697 /* A member template definition only matches a member template
698 declaration. */
699 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
700 continue;
702 /* ref-qualifier or absence of same must match. */
703 if (type_memfn_rqual (TREE_TYPE (function))
704 != type_memfn_rqual (TREE_TYPE (fndecl)))
705 continue;
707 /* While finding a match, same types and params are not enough
708 if the function is versioned. Also check version ("target")
709 attributes. */
710 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
711 TREE_TYPE (TREE_TYPE (fndecl)))
712 && compparms (p1, p2)
713 && !targetm.target_option.function_versions (function, fndecl)
714 && (!is_template
715 || comp_template_parms (template_parms,
716 DECL_TEMPLATE_PARMS (fndecl)))
717 && (DECL_TEMPLATE_SPECIALIZATION (function)
718 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
719 && (!DECL_TEMPLATE_SPECIALIZATION (function)
720 || (DECL_TI_TEMPLATE (function)
721 == DECL_TI_TEMPLATE (fndecl))))
722 break;
724 if (fndecls)
726 if (pushed_scope)
727 pop_scope (pushed_scope);
728 return OVL_CURRENT (fndecls);
731 error_at (DECL_SOURCE_LOCATION (function),
732 "prototype for %q#D does not match any in class %qT",
733 function, ctype);
734 is_conv_op = DECL_CONV_FN_P (fndecl);
736 if (is_conv_op)
737 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
738 fndecls = (*methods)[ix];
739 while (fndecls)
741 fndecl = OVL_CURRENT (fndecls);
742 fndecls = OVL_NEXT (fndecls);
744 if (!fndecls && is_conv_op)
746 if (methods->length () > (size_t) ++ix)
748 fndecls = (*methods)[ix];
749 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
751 fndecls = NULL_TREE;
752 is_conv_op = false;
755 else
756 is_conv_op = false;
758 if (format)
759 format = " %+#D";
760 else if (fndecls)
761 format = N_("candidates are: %+#D");
762 else
763 format = N_("candidate is: %+#D");
764 error (format, fndecl);
767 else if (!COMPLETE_TYPE_P (ctype))
768 cxx_incomplete_type_error (function, ctype);
769 else
770 error ("no %q#D member function declared in class %qT",
771 function, ctype);
773 if (pushed_scope)
774 pop_scope (pushed_scope);
775 return error_mark_node;
778 /* DECL is a function with vague linkage. Remember it so that at the
779 end of the translation unit we can decide whether or not to emit
780 it. */
782 void
783 note_vague_linkage_fn (tree decl)
785 DECL_DEFER_OUTPUT (decl) = 1;
786 vec_safe_push (deferred_fns, decl);
789 /* We have just processed the DECL, which is a static data member.
790 The other parameters are as for cp_finish_decl. */
792 void
793 finish_static_data_member_decl (tree decl,
794 tree init, bool init_const_expr_p,
795 tree asmspec_tree,
796 int flags)
798 DECL_CONTEXT (decl) = current_class_type;
800 /* We cannot call pushdecl here, because that would fill in the
801 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
802 the right thing, namely, to put this decl out straight away. */
804 if (! processing_template_decl)
805 vec_safe_push (pending_statics, decl);
807 if (LOCAL_CLASS_P (current_class_type)
808 /* We already complained about the template definition. */
809 && !DECL_TEMPLATE_INSTANTIATION (decl))
810 permerror (input_location, "local class %q#T shall not have static data member %q#D",
811 current_class_type, decl);
812 else
813 for (tree t = current_class_type; TYPE_P (t);
814 t = CP_TYPE_CONTEXT (t))
815 if (TYPE_ANONYMOUS_P (t))
817 if (permerror (DECL_SOURCE_LOCATION (decl),
818 "static data member %qD in unnamed class", decl))
819 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
820 "unnamed class defined here");
821 break;
824 DECL_IN_AGGR_P (decl) = 1;
826 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
827 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
828 SET_VAR_HAD_UNKNOWN_BOUND (decl);
830 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
833 /* DECLARATOR and DECLSPECS correspond to a class member. The other
834 parameters are as for cp_finish_decl. Return the DECL for the
835 class member declared. */
837 tree
838 grokfield (const cp_declarator *declarator,
839 cp_decl_specifier_seq *declspecs,
840 tree init, bool init_const_expr_p,
841 tree asmspec_tree,
842 tree attrlist)
844 tree value;
845 const char *asmspec = 0;
846 int flags;
847 tree name;
849 if (init
850 && TREE_CODE (init) == TREE_LIST
851 && TREE_VALUE (init) == error_mark_node
852 && TREE_CHAIN (init) == NULL_TREE)
853 init = NULL_TREE;
855 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
856 if (! value || value == error_mark_node)
857 /* friend or constructor went bad. */
858 return error_mark_node;
859 if (TREE_TYPE (value) == error_mark_node)
860 return value;
862 if (TREE_CODE (value) == TYPE_DECL && init)
864 error ("typedef %qD is initialized (use decltype instead)", value);
865 init = NULL_TREE;
868 /* Pass friendly classes back. */
869 if (value == void_type_node)
870 return value;
872 /* Pass friend decls back. */
873 if ((TREE_CODE (value) == FUNCTION_DECL
874 || TREE_CODE (value) == TEMPLATE_DECL)
875 && DECL_CONTEXT (value) != current_class_type)
876 return value;
878 name = DECL_NAME (value);
880 if (name != NULL_TREE)
882 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
884 error ("explicit template argument list not allowed");
885 return error_mark_node;
888 if (IDENTIFIER_POINTER (name)[0] == '_'
889 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
890 error ("member %qD conflicts with virtual function table field name",
891 value);
894 /* Stash away type declarations. */
895 if (TREE_CODE (value) == TYPE_DECL)
897 DECL_NONLOCAL (value) = 1;
898 DECL_CONTEXT (value) = current_class_type;
900 if (attrlist)
902 int attrflags = 0;
904 /* If this is a typedef that names the class for linkage purposes
905 (7.1.3p8), apply any attributes directly to the type. */
906 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
907 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
908 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
910 cplus_decl_attributes (&value, attrlist, attrflags);
913 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
914 && TREE_TYPE (value) != error_mark_node
915 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
916 set_underlying_type (value);
918 /* It's important that push_template_decl below follows
919 set_underlying_type above so that the created template
920 carries the properly set type of VALUE. */
921 if (processing_template_decl)
922 value = push_template_decl (value);
924 record_locally_defined_typedef (value);
925 return value;
928 if (DECL_IN_AGGR_P (value))
930 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
931 return void_type_node;
934 if (asmspec_tree && asmspec_tree != error_mark_node)
935 asmspec = TREE_STRING_POINTER (asmspec_tree);
937 if (init)
939 if (TREE_CODE (value) == FUNCTION_DECL)
941 /* Initializers for functions are rejected early in the parser.
942 If we get here, it must be a pure specifier for a method. */
943 if (init == ridpointers[(int)RID_DELETE])
945 DECL_DELETED_FN (value) = 1;
946 DECL_DECLARED_INLINE_P (value) = 1;
947 DECL_INITIAL (value) = error_mark_node;
949 else if (init == ridpointers[(int)RID_DEFAULT])
951 if (defaultable_fn_check (value))
953 DECL_DEFAULTED_FN (value) = 1;
954 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
955 DECL_DECLARED_INLINE_P (value) = 1;
958 else if (TREE_CODE (init) == DEFAULT_ARG)
959 error ("invalid initializer for member function %qD", value);
960 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
962 if (integer_zerop (init))
963 DECL_PURE_VIRTUAL_P (value) = 1;
964 else if (error_operand_p (init))
965 ; /* An error has already been reported. */
966 else
967 error ("invalid initializer for member function %qD",
968 value);
970 else
972 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
973 error ("initializer specified for static member function %qD",
974 value);
977 else if (TREE_CODE (value) == FIELD_DECL)
978 /* C++11 NSDMI, keep going. */;
979 else if (!VAR_P (value))
980 gcc_unreachable ();
983 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
985 value = push_template_decl (value);
986 if (error_operand_p (value))
987 return error_mark_node;
990 if (attrlist)
991 cplus_decl_attributes (&value, attrlist, 0);
993 if (init && DIRECT_LIST_INIT_P (init))
994 flags = LOOKUP_NORMAL;
995 else
996 flags = LOOKUP_IMPLICIT;
998 switch (TREE_CODE (value))
1000 case VAR_DECL:
1001 finish_static_data_member_decl (value, init, init_const_expr_p,
1002 asmspec_tree, flags);
1003 return value;
1005 case FIELD_DECL:
1006 if (asmspec)
1007 error ("%<asm%> specifiers are not permitted on non-static data members");
1008 if (DECL_INITIAL (value) == error_mark_node)
1009 init = error_mark_node;
1010 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1011 NULL_TREE, flags);
1012 DECL_IN_AGGR_P (value) = 1;
1013 return value;
1015 case FUNCTION_DECL:
1016 if (asmspec)
1017 set_user_assembler_name (value, asmspec);
1019 cp_finish_decl (value,
1020 /*init=*/NULL_TREE,
1021 /*init_const_expr_p=*/false,
1022 asmspec_tree, flags);
1024 /* Pass friends back this way. */
1025 if (DECL_FRIEND_P (value))
1026 return void_type_node;
1028 DECL_IN_AGGR_P (value) = 1;
1029 return value;
1031 default:
1032 gcc_unreachable ();
1034 return NULL_TREE;
1037 /* Like `grokfield', but for bitfields.
1038 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1040 tree
1041 grokbitfield (const cp_declarator *declarator,
1042 cp_decl_specifier_seq *declspecs, tree width,
1043 tree attrlist)
1045 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1047 if (value == error_mark_node)
1048 return NULL_TREE; /* friends went bad. */
1049 if (TREE_TYPE (value) == error_mark_node)
1050 return value;
1052 /* Pass friendly classes back. */
1053 if (VOID_TYPE_P (value))
1054 return void_type_node;
1056 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1057 && (POINTER_TYPE_P (value)
1058 || !dependent_type_p (TREE_TYPE (value))))
1060 error ("bit-field %qD with non-integral type", value);
1061 return error_mark_node;
1064 if (TREE_CODE (value) == TYPE_DECL)
1066 error ("cannot declare %qD to be a bit-field type", value);
1067 return NULL_TREE;
1070 /* Usually, finish_struct_1 catches bitfields with invalid types.
1071 But, in the case of bitfields with function type, we confuse
1072 ourselves into thinking they are member functions, so we must
1073 check here. */
1074 if (TREE_CODE (value) == FUNCTION_DECL)
1076 error ("cannot declare bit-field %qD with function type",
1077 DECL_NAME (value));
1078 return NULL_TREE;
1081 if (DECL_IN_AGGR_P (value))
1083 error ("%qD is already defined in the class %qT", value,
1084 DECL_CONTEXT (value));
1085 return void_type_node;
1088 if (TREE_STATIC (value))
1090 error ("static member %qD cannot be a bit-field", value);
1091 return NULL_TREE;
1093 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1095 if (width != error_mark_node)
1097 /* The width must be an integer type. */
1098 if (!type_dependent_expression_p (width)
1099 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1100 error ("width of bit-field %qD has non-integral type %qT", value,
1101 TREE_TYPE (width));
1102 DECL_INITIAL (value) = width;
1103 SET_DECL_C_BIT_FIELD (value);
1106 DECL_IN_AGGR_P (value) = 1;
1108 if (attrlist)
1109 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1111 return value;
1115 /* Returns true iff ATTR is an attribute which needs to be applied at
1116 instantiation time rather than template definition time. */
1118 static bool
1119 is_late_template_attribute (tree attr, tree decl)
1121 tree name = get_attribute_name (attr);
1122 tree args = TREE_VALUE (attr);
1123 const struct attribute_spec *spec = lookup_attribute_spec (name);
1124 tree arg;
1126 if (!spec)
1127 /* Unknown attribute. */
1128 return false;
1130 /* Attribute weak handling wants to write out assembly right away. */
1131 if (is_attribute_p ("weak", name))
1132 return true;
1134 /* Attribute unused is applied directly, as it appertains to
1135 decls. */
1136 if (is_attribute_p ("unused", name))
1137 return false;
1139 /* #pragma omp declare simd attribute needs to be always deferred. */
1140 if (flag_openmp
1141 && is_attribute_p ("omp declare simd", name))
1142 return true;
1144 /* If any of the arguments are dependent expressions, we can't evaluate
1145 the attribute until instantiation time. */
1146 for (arg = args; arg; arg = TREE_CHAIN (arg))
1148 tree t = TREE_VALUE (arg);
1150 /* If the first attribute argument is an identifier, only consider
1151 second and following arguments. Attributes like mode, format,
1152 cleanup and several target specific attributes aren't late
1153 just because they have an IDENTIFIER_NODE as first argument. */
1154 if (arg == args && identifier_p (t))
1155 continue;
1157 if (value_dependent_expression_p (t)
1158 || type_dependent_expression_p (t))
1159 return true;
1162 if (TREE_CODE (decl) == TYPE_DECL
1163 || TYPE_P (decl)
1164 || spec->type_required)
1166 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1168 /* We can't apply any attributes to a completely unknown type until
1169 instantiation time. */
1170 enum tree_code code = TREE_CODE (type);
1171 if (code == TEMPLATE_TYPE_PARM
1172 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1173 || code == TYPENAME_TYPE)
1174 return true;
1175 /* Also defer most attributes on dependent types. This is not
1176 necessary in all cases, but is the better default. */
1177 else if (dependent_type_p (type)
1178 /* But attributes abi_tag and visibility specifically apply
1179 to templates. */
1180 && !is_attribute_p ("abi_tag", name)
1181 && !is_attribute_p ("visibility", name))
1182 return true;
1183 else
1184 return false;
1186 else
1187 return false;
1190 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1191 applied at instantiation time and return them. If IS_DEPENDENT is true,
1192 the declaration itself is dependent, so all attributes should be applied
1193 at instantiation time. */
1195 static tree
1196 splice_template_attributes (tree *attr_p, tree decl)
1198 tree *p = attr_p;
1199 tree late_attrs = NULL_TREE;
1200 tree *q = &late_attrs;
1202 if (!p)
1203 return NULL_TREE;
1205 for (; *p; )
1207 if (is_late_template_attribute (*p, decl))
1209 ATTR_IS_DEPENDENT (*p) = 1;
1210 *q = *p;
1211 *p = TREE_CHAIN (*p);
1212 q = &TREE_CHAIN (*q);
1213 *q = NULL_TREE;
1215 else
1216 p = &TREE_CHAIN (*p);
1219 return late_attrs;
1222 /* Remove any late attributes from the list in ATTR_P and attach them to
1223 DECL_P. */
1225 static void
1226 save_template_attributes (tree *attr_p, tree *decl_p)
1228 tree *q;
1230 if (attr_p && *attr_p == error_mark_node)
1231 return;
1233 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1234 if (!late_attrs)
1235 return;
1237 if (DECL_P (*decl_p))
1238 q = &DECL_ATTRIBUTES (*decl_p);
1239 else
1240 q = &TYPE_ATTRIBUTES (*decl_p);
1242 tree old_attrs = *q;
1244 /* Merge the late attributes at the beginning with the attribute
1245 list. */
1246 late_attrs = merge_attributes (late_attrs, *q);
1247 *q = late_attrs;
1249 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1251 /* We've added new attributes directly to the main variant, so
1252 now we need to update all of the other variants to include
1253 these new attributes. */
1254 tree variant;
1255 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1256 variant = TYPE_NEXT_VARIANT (variant))
1258 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1259 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1264 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1265 to a typedef which gives a previously anonymous class or enum a name for
1266 linkage purposes. */
1268 bool
1269 attributes_naming_typedef_ok (tree attrs)
1271 for (; attrs; attrs = TREE_CHAIN (attrs))
1273 tree name = get_attribute_name (attrs);
1274 if (is_attribute_p ("vector_size", name))
1275 return false;
1277 return true;
1280 /* Like reconstruct_complex_type, but handle also template trees. */
1282 tree
1283 cp_reconstruct_complex_type (tree type, tree bottom)
1285 tree inner, outer;
1286 bool late_return_type_p = false;
1288 if (TYPE_PTR_P (type))
1290 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1291 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1292 TYPE_REF_CAN_ALIAS_ALL (type));
1294 else if (TREE_CODE (type) == REFERENCE_TYPE)
1296 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1297 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1298 TYPE_REF_CAN_ALIAS_ALL (type));
1300 else if (TREE_CODE (type) == ARRAY_TYPE)
1302 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1303 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1304 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1305 element type qualification will be handled by the recursive
1306 cp_reconstruct_complex_type call and cp_build_qualified_type
1307 for ARRAY_TYPEs changes the element type. */
1308 return outer;
1310 else if (TREE_CODE (type) == FUNCTION_TYPE)
1312 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1313 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1314 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1315 outer = apply_memfn_quals (outer,
1316 type_memfn_quals (type),
1317 type_memfn_rqual (type));
1319 else if (TREE_CODE (type) == METHOD_TYPE)
1321 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1322 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1323 /* The build_method_type_directly() routine prepends 'this' to argument list,
1324 so we must compensate by getting rid of it. */
1325 outer
1326 = build_method_type_directly
1327 (class_of_this_parm (type), inner,
1328 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1330 else if (TREE_CODE (type) == OFFSET_TYPE)
1332 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1333 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1335 else
1336 return bottom;
1338 if (TYPE_ATTRIBUTES (type))
1339 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1340 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1342 if (late_return_type_p)
1343 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1345 return outer;
1348 /* Replaces any constexpr expression that may be into the attributes
1349 arguments with their reduced value. */
1351 static void
1352 cp_check_const_attributes (tree attributes)
1354 if (attributes == error_mark_node)
1355 return;
1357 tree attr;
1358 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1360 tree arg;
1361 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1363 tree expr = TREE_VALUE (arg);
1364 if (EXPR_P (expr))
1365 TREE_VALUE (arg) = maybe_constant_value (expr);
1370 /* Return true if TYPE is an OpenMP mappable type. */
1371 bool
1372 cp_omp_mappable_type (tree type)
1374 /* Mappable type has to be complete. */
1375 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1376 return false;
1377 /* Arrays have mappable type if the elements have mappable type. */
1378 while (TREE_CODE (type) == ARRAY_TYPE)
1379 type = TREE_TYPE (type);
1380 /* A mappable type cannot contain virtual members. */
1381 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1382 return false;
1383 /* All data members must be non-static. */
1384 if (CLASS_TYPE_P (type))
1386 tree field;
1387 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1388 if (TREE_CODE (field) == VAR_DECL)
1389 return false;
1390 /* All fields must have mappable types. */
1391 else if (TREE_CODE (field) == FIELD_DECL
1392 && !cp_omp_mappable_type (TREE_TYPE (field)))
1393 return false;
1395 return true;
1398 /* Like decl_attributes, but handle C++ complexity. */
1400 void
1401 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1403 if (*decl == NULL_TREE || *decl == void_type_node
1404 || *decl == error_mark_node)
1405 return;
1407 /* Add implicit "omp declare target" attribute if requested. */
1408 if (scope_chain->omp_declare_target_attribute
1409 && ((TREE_CODE (*decl) == VAR_DECL && TREE_STATIC (*decl))
1410 || TREE_CODE (*decl) == FUNCTION_DECL))
1412 if (TREE_CODE (*decl) == VAR_DECL
1413 && DECL_CLASS_SCOPE_P (*decl))
1414 error ("%q+D static data member inside of declare target directive",
1415 *decl);
1416 else if (TREE_CODE (*decl) == VAR_DECL
1417 && (DECL_FUNCTION_SCOPE_P (*decl)
1418 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1419 error ("%q+D in block scope inside of declare target directive",
1420 *decl);
1421 else if (!processing_template_decl
1422 && TREE_CODE (*decl) == VAR_DECL
1423 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1424 error ("%q+D in declare target directive does not have mappable type",
1425 *decl);
1426 else
1427 attributes = tree_cons (get_identifier ("omp declare target"),
1428 NULL_TREE, attributes);
1431 if (processing_template_decl)
1433 if (check_for_bare_parameter_packs (attributes))
1434 return;
1436 save_template_attributes (&attributes, decl);
1439 cp_check_const_attributes (attributes);
1441 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1442 decl = &DECL_TEMPLATE_RESULT (*decl);
1444 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1446 attributes
1447 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1448 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (*decl)),
1449 attributes, flags);
1451 else
1452 decl_attributes (decl, attributes, flags);
1454 if (TREE_CODE (*decl) == TYPE_DECL)
1455 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1458 /* Walks through the namespace- or function-scope anonymous union
1459 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1460 Returns one of the fields for use in the mangled name. */
1462 static tree
1463 build_anon_union_vars (tree type, tree object)
1465 tree main_decl = NULL_TREE;
1466 tree field;
1468 /* Rather than write the code to handle the non-union case,
1469 just give an error. */
1470 if (TREE_CODE (type) != UNION_TYPE)
1472 error ("anonymous struct not inside named type");
1473 return error_mark_node;
1476 for (field = TYPE_FIELDS (type);
1477 field != NULL_TREE;
1478 field = DECL_CHAIN (field))
1480 tree decl;
1481 tree ref;
1483 if (DECL_ARTIFICIAL (field))
1484 continue;
1485 if (TREE_CODE (field) != FIELD_DECL)
1487 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1488 "have non-static data members", field);
1489 continue;
1492 if (TREE_PRIVATE (field))
1493 permerror (input_location, "private member %q+#D in anonymous union", field);
1494 else if (TREE_PROTECTED (field))
1495 permerror (input_location, "protected member %q+#D in anonymous union", field);
1497 if (processing_template_decl)
1498 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1499 DECL_NAME (field), NULL_TREE);
1500 else
1501 ref = build_class_member_access_expr (object, field, NULL_TREE,
1502 false, tf_warning_or_error);
1504 if (DECL_NAME (field))
1506 tree base;
1508 decl = build_decl (input_location,
1509 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1510 DECL_ANON_UNION_VAR_P (decl) = 1;
1511 DECL_ARTIFICIAL (decl) = 1;
1513 base = get_base_address (object);
1514 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1515 TREE_STATIC (decl) = TREE_STATIC (base);
1516 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1518 SET_DECL_VALUE_EXPR (decl, ref);
1519 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1521 decl = pushdecl (decl);
1523 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1524 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1525 else
1526 decl = 0;
1528 if (main_decl == NULL_TREE)
1529 main_decl = decl;
1532 return main_decl;
1535 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1536 anonymous union, then all members must be laid out together. PUBLIC_P
1537 is nonzero if this union is not declared static. */
1539 void
1540 finish_anon_union (tree anon_union_decl)
1542 tree type;
1543 tree main_decl;
1544 bool public_p;
1546 if (anon_union_decl == error_mark_node)
1547 return;
1549 type = TREE_TYPE (anon_union_decl);
1550 public_p = TREE_PUBLIC (anon_union_decl);
1552 /* The VAR_DECL's context is the same as the TYPE's context. */
1553 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1555 if (TYPE_FIELDS (type) == NULL_TREE)
1556 return;
1558 if (public_p)
1560 error ("namespace-scope anonymous aggregates must be static");
1561 return;
1564 main_decl = build_anon_union_vars (type, anon_union_decl);
1565 if (main_decl == error_mark_node)
1566 return;
1567 if (main_decl == NULL_TREE)
1569 warning (0, "anonymous union with no members");
1570 return;
1573 if (!processing_template_decl)
1575 /* Use main_decl to set the mangled name. */
1576 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1577 maybe_commonize_var (anon_union_decl);
1578 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1579 mangle_decl (anon_union_decl);
1580 DECL_NAME (anon_union_decl) = NULL_TREE;
1583 pushdecl (anon_union_decl);
1584 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1587 /* Auxiliary functions to make type signatures for
1588 `operator new' and `operator delete' correspond to
1589 what compiler will be expecting. */
1591 tree
1592 coerce_new_type (tree type)
1594 int e = 0;
1595 tree args = TYPE_ARG_TYPES (type);
1597 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1599 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1601 e = 1;
1602 error ("%<operator new%> must return type %qT", ptr_type_node);
1605 if (args && args != void_list_node)
1607 if (TREE_PURPOSE (args))
1609 /* [basic.stc.dynamic.allocation]
1611 The first parameter shall not have an associated default
1612 argument. */
1613 error ("the first parameter of %<operator new%> cannot "
1614 "have a default argument");
1615 /* Throw away the default argument. */
1616 TREE_PURPOSE (args) = NULL_TREE;
1619 if (!same_type_p (TREE_VALUE (args), size_type_node))
1621 e = 2;
1622 args = TREE_CHAIN (args);
1625 else
1626 e = 2;
1628 if (e == 2)
1629 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1630 "as first parameter", size_type_node);
1632 switch (e)
1634 case 2:
1635 args = tree_cons (NULL_TREE, size_type_node, args);
1636 /* Fall through. */
1637 case 1:
1638 type = build_exception_variant
1639 (build_function_type (ptr_type_node, args),
1640 TYPE_RAISES_EXCEPTIONS (type));
1641 /* Fall through. */
1642 default:;
1644 return type;
1647 tree
1648 coerce_delete_type (tree type)
1650 int e = 0;
1651 tree args = TYPE_ARG_TYPES (type);
1653 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1655 if (!same_type_p (TREE_TYPE (type), void_type_node))
1657 e = 1;
1658 error ("%<operator delete%> must return type %qT", void_type_node);
1661 if (!args || args == void_list_node
1662 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1664 e = 2;
1665 if (args && args != void_list_node)
1666 args = TREE_CHAIN (args);
1667 error ("%<operator delete%> takes type %qT as first parameter",
1668 ptr_type_node);
1670 switch (e)
1672 case 2:
1673 args = tree_cons (NULL_TREE, ptr_type_node, args);
1674 /* Fall through. */
1675 case 1:
1676 type = build_exception_variant
1677 (build_function_type (void_type_node, args),
1678 TYPE_RAISES_EXCEPTIONS (type));
1679 /* Fall through. */
1680 default:;
1683 return type;
1686 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1687 and mark them as needed. */
1689 static void
1690 mark_vtable_entries (tree decl)
1692 tree fnaddr;
1693 unsigned HOST_WIDE_INT idx;
1695 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1696 idx, fnaddr)
1698 tree fn;
1700 STRIP_NOPS (fnaddr);
1702 if (TREE_CODE (fnaddr) != ADDR_EXPR
1703 && TREE_CODE (fnaddr) != FDESC_EXPR)
1704 /* This entry is an offset: a virtual base class offset, a
1705 virtual call offset, an RTTI offset, etc. */
1706 continue;
1708 fn = TREE_OPERAND (fnaddr, 0);
1709 TREE_ADDRESSABLE (fn) = 1;
1710 /* When we don't have vcall offsets, we output thunks whenever
1711 we output the vtables that contain them. With vcall offsets,
1712 we know all the thunks we'll need when we emit a virtual
1713 function, so we emit the thunks there instead. */
1714 if (DECL_THUNK_P (fn))
1715 use_thunk (fn, /*emit_p=*/0);
1716 mark_used (fn);
1720 /* Set DECL up to have the closest approximation of "initialized common"
1721 linkage available. */
1723 void
1724 comdat_linkage (tree decl)
1726 if (flag_weak)
1727 make_decl_one_only (decl, cxx_comdat_group (decl));
1728 else if (TREE_CODE (decl) == FUNCTION_DECL
1729 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1730 /* We can just emit function and compiler-generated variables
1731 statically; having multiple copies is (for the most part) only
1732 a waste of space.
1734 There are two correctness issues, however: the address of a
1735 template instantiation with external linkage should be the
1736 same, independent of what translation unit asks for the
1737 address, and this will not hold when we emit multiple copies of
1738 the function. However, there's little else we can do.
1740 Also, by default, the typeinfo implementation assumes that
1741 there will be only one copy of the string used as the name for
1742 each type. Therefore, if weak symbols are unavailable, the
1743 run-time library should perform a more conservative check; it
1744 should perform a string comparison, rather than an address
1745 comparison. */
1746 TREE_PUBLIC (decl) = 0;
1747 else
1749 /* Static data member template instantiations, however, cannot
1750 have multiple copies. */
1751 if (DECL_INITIAL (decl) == 0
1752 || DECL_INITIAL (decl) == error_mark_node)
1753 DECL_COMMON (decl) = 1;
1754 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1756 DECL_COMMON (decl) = 1;
1757 DECL_INITIAL (decl) = error_mark_node;
1759 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1761 /* We can't do anything useful; leave vars for explicit
1762 instantiation. */
1763 DECL_EXTERNAL (decl) = 1;
1764 DECL_NOT_REALLY_EXTERN (decl) = 0;
1768 DECL_COMDAT (decl) = 1;
1771 /* For win32 we also want to put explicit instantiations in
1772 linkonce sections, so that they will be merged with implicit
1773 instantiations; otherwise we get duplicate symbol errors.
1774 For Darwin we do not want explicit instantiations to be
1775 linkonce. */
1777 void
1778 maybe_make_one_only (tree decl)
1780 /* We used to say that this was not necessary on targets that support weak
1781 symbols, because the implicit instantiations will defer to the explicit
1782 one. However, that's not actually the case in SVR4; a strong definition
1783 after a weak one is an error. Also, not making explicit
1784 instantiations one_only means that we can end up with two copies of
1785 some template instantiations. */
1786 if (! flag_weak)
1787 return;
1789 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1790 we can get away with not emitting them if they aren't used. We need
1791 to for variables so that cp_finish_decl will update their linkage,
1792 because their DECL_INITIAL may not have been set properly yet. */
1794 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1795 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1796 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1798 make_decl_one_only (decl, cxx_comdat_group (decl));
1800 if (VAR_P (decl))
1802 varpool_node *node = varpool_node_for_decl (decl);
1803 DECL_COMDAT (decl) = 1;
1804 /* Mark it needed so we don't forget to emit it. */
1805 node->forced_by_abi = true;
1806 TREE_USED (decl) = 1;
1811 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1812 This predicate will give the right answer during parsing of the
1813 function, which other tests may not. */
1815 bool
1816 vague_linkage_p (tree decl)
1818 /* Unfortunately, import_export_decl has not always been called
1819 before the function is processed, so we cannot simply check
1820 DECL_COMDAT. */
1821 if (DECL_COMDAT (decl)
1822 || (((TREE_CODE (decl) == FUNCTION_DECL
1823 && DECL_DECLARED_INLINE_P (decl))
1824 || (DECL_LANG_SPECIFIC (decl)
1825 && DECL_TEMPLATE_INSTANTIATION (decl)))
1826 && TREE_PUBLIC (decl)))
1827 return true;
1828 else if (DECL_FUNCTION_SCOPE_P (decl))
1829 /* A local static in an inline effectively has vague linkage. */
1830 return (TREE_STATIC (decl)
1831 && vague_linkage_p (DECL_CONTEXT (decl)));
1832 else
1833 return false;
1836 /* Determine whether or not we want to specifically import or export CTYPE,
1837 using various heuristics. */
1839 static void
1840 import_export_class (tree ctype)
1842 /* -1 for imported, 1 for exported. */
1843 int import_export = 0;
1845 /* It only makes sense to call this function at EOF. The reason is
1846 that this function looks at whether or not the first non-inline
1847 non-abstract virtual member function has been defined in this
1848 translation unit. But, we can't possibly know that until we've
1849 seen the entire translation unit. */
1850 gcc_assert (at_eof);
1852 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1853 return;
1855 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1856 we will have CLASSTYPE_INTERFACE_ONLY set but not
1857 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1858 heuristic because someone will supply a #pragma implementation
1859 elsewhere, and deducing it here would produce a conflict. */
1860 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1861 return;
1863 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1864 import_export = -1;
1865 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1866 import_export = 1;
1867 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1868 && !flag_implicit_templates)
1869 /* For a template class, without -fimplicit-templates, check the
1870 repository. If the virtual table is assigned to this
1871 translation unit, then export the class; otherwise, import
1872 it. */
1873 import_export = repo_export_class_p (ctype) ? 1 : -1;
1874 else if (TYPE_POLYMORPHIC_P (ctype))
1876 /* The ABI specifies that the virtual table and associated
1877 information are emitted with the key method, if any. */
1878 tree method = CLASSTYPE_KEY_METHOD (ctype);
1879 /* If weak symbol support is not available, then we must be
1880 careful not to emit the vtable when the key function is
1881 inline. An inline function can be defined in multiple
1882 translation units. If we were to emit the vtable in each
1883 translation unit containing a definition, we would get
1884 multiple definition errors at link-time. */
1885 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1886 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1889 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1890 a definition anywhere else. */
1891 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1892 import_export = 0;
1894 /* Allow back ends the chance to overrule the decision. */
1895 if (targetm.cxx.import_export_class)
1896 import_export = targetm.cxx.import_export_class (ctype, import_export);
1898 if (import_export)
1900 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1901 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1905 /* Return true if VAR has already been provided to the back end; in that
1906 case VAR should not be modified further by the front end. */
1907 static bool
1908 var_finalized_p (tree var)
1910 return varpool_node_for_decl (var)->definition;
1913 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1914 must be emitted in this translation unit. Mark it as such. */
1916 void
1917 mark_needed (tree decl)
1919 TREE_USED (decl) = 1;
1920 if (TREE_CODE (decl) == FUNCTION_DECL)
1922 /* Extern inline functions don't become needed when referenced.
1923 If we know a method will be emitted in other TU and no new
1924 functions can be marked reachable, just use the external
1925 definition. */
1926 struct cgraph_node *node = cgraph_get_create_node (decl);
1927 node->forced_by_abi = true;
1929 else if (TREE_CODE (decl) == VAR_DECL)
1931 varpool_node *node = varpool_node_for_decl (decl);
1932 /* C++ frontend use mark_decl_references to force COMDAT variables
1933 to be output that might appear dead otherwise. */
1934 node->forced_by_abi = true;
1938 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1939 returns true if a definition of this entity should be provided in
1940 this object file. Callers use this function to determine whether
1941 or not to let the back end know that a definition of DECL is
1942 available in this translation unit. */
1944 bool
1945 decl_needed_p (tree decl)
1947 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1948 /* This function should only be called at the end of the translation
1949 unit. We cannot be sure of whether or not something will be
1950 COMDAT until that point. */
1951 gcc_assert (at_eof);
1953 /* All entities with external linkage that are not COMDAT should be
1954 emitted; they may be referred to from other object files. */
1955 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1956 return true;
1957 /* If this entity was used, let the back end see it; it will decide
1958 whether or not to emit it into the object file. */
1959 if (TREE_USED (decl))
1960 return true;
1961 /* Functions marked "dllexport" must be emitted so that they are
1962 visible to other DLLs. */
1963 if (flag_keep_inline_dllexport
1964 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1965 return true;
1966 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1967 reference to DECL might cause it to be emitted later. */
1968 return false;
1971 /* If necessary, write out the vtables for the dynamic class CTYPE.
1972 Returns true if any vtables were emitted. */
1974 static bool
1975 maybe_emit_vtables (tree ctype)
1977 tree vtbl;
1978 tree primary_vtbl;
1979 int needed = 0;
1980 varpool_node *current = NULL, *last = NULL;
1982 /* If the vtables for this class have already been emitted there is
1983 nothing more to do. */
1984 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1985 if (var_finalized_p (primary_vtbl))
1986 return false;
1987 /* Ignore dummy vtables made by get_vtable_decl. */
1988 if (TREE_TYPE (primary_vtbl) == void_type_node)
1989 return false;
1991 /* On some targets, we cannot determine the key method until the end
1992 of the translation unit -- which is when this function is
1993 called. */
1994 if (!targetm.cxx.key_method_may_be_inline ())
1995 determine_key_method (ctype);
1997 /* See if any of the vtables are needed. */
1998 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2000 import_export_decl (vtbl);
2001 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2002 needed = 1;
2004 if (!needed)
2006 /* If the references to this class' vtables are optimized away,
2007 still emit the appropriate debugging information. See
2008 dfs_debug_mark. */
2009 if (DECL_COMDAT (primary_vtbl)
2010 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2011 note_debug_info_needed (ctype);
2012 if (flag_devirtualize)
2013 /* Make sure virtual functions get instantiated/synthesized so that
2014 they can be inlined after devirtualization even if the vtable is
2015 never emitted. */
2016 mark_vtable_entries (primary_vtbl);
2017 return false;
2020 /* The ABI requires that we emit all of the vtables if we emit any
2021 of them. */
2022 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2024 /* Mark entities references from the virtual table as used. */
2025 mark_vtable_entries (vtbl);
2027 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2029 vec<tree, va_gc> *cleanups = NULL;
2030 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2031 LOOKUP_NORMAL);
2033 /* It had better be all done at compile-time. */
2034 gcc_assert (!expr && !cleanups);
2037 /* Write it out. */
2038 DECL_EXTERNAL (vtbl) = 0;
2039 rest_of_decl_compilation (vtbl, 1, 1);
2041 /* Because we're only doing syntax-checking, we'll never end up
2042 actually marking the variable as written. */
2043 if (flag_syntax_only)
2044 TREE_ASM_WRITTEN (vtbl) = 1;
2045 else if (DECL_ONE_ONLY (vtbl))
2047 current = varpool_node_for_decl (vtbl);
2048 if (last)
2049 symtab_add_to_same_comdat_group (current, last);
2050 last = current;
2054 /* Since we're writing out the vtable here, also write the debug
2055 info. */
2056 note_debug_info_needed (ctype);
2058 return true;
2061 /* A special return value from type_visibility meaning internal
2062 linkage. */
2064 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2066 /* walk_tree helper function for type_visibility. */
2068 static tree
2069 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2071 int *vis_p = (int *)data;
2072 if (! TYPE_P (*tp))
2074 *walk_subtrees = 0;
2076 else if (OVERLOAD_TYPE_P (*tp)
2077 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2079 *vis_p = VISIBILITY_ANON;
2080 return *tp;
2082 else if (CLASS_TYPE_P (*tp)
2083 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2084 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2085 return NULL;
2088 /* Returns the visibility of TYPE, which is the minimum visibility of its
2089 component types. */
2091 static int
2092 type_visibility (tree type)
2094 int vis = VISIBILITY_DEFAULT;
2095 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2096 return vis;
2099 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2100 specified (or if VISIBILITY is static). If TMPL is true, this
2101 constraint is for a template argument, and takes precedence
2102 over explicitly-specified visibility on the template. */
2104 static void
2105 constrain_visibility (tree decl, int visibility, bool tmpl)
2107 if (visibility == VISIBILITY_ANON)
2109 /* extern "C" declarations aren't affected by the anonymous
2110 namespace. */
2111 if (!DECL_EXTERN_C_P (decl))
2113 TREE_PUBLIC (decl) = 0;
2114 DECL_WEAK (decl) = 0;
2115 DECL_COMMON (decl) = 0;
2116 if (TREE_CODE (decl) == FUNCTION_DECL
2117 || TREE_CODE (decl) == VAR_DECL)
2119 struct symtab_node *snode = symtab_get_node (decl);
2121 if (snode)
2122 snode->set_comdat_group (NULL);
2124 DECL_INTERFACE_KNOWN (decl) = 1;
2125 if (DECL_LANG_SPECIFIC (decl))
2126 DECL_NOT_REALLY_EXTERN (decl) = 1;
2129 else if (visibility > DECL_VISIBILITY (decl)
2130 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2132 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2133 /* This visibility was not specified. */
2134 DECL_VISIBILITY_SPECIFIED (decl) = false;
2138 /* Constrain the visibility of DECL based on the visibility of its template
2139 arguments. */
2141 static void
2142 constrain_visibility_for_template (tree decl, tree targs)
2144 /* If this is a template instantiation, check the innermost
2145 template args for visibility constraints. The outer template
2146 args are covered by the class check. */
2147 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2148 int i;
2149 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2151 int vis = 0;
2153 tree arg = TREE_VEC_ELT (args, i-1);
2154 if (TYPE_P (arg))
2155 vis = type_visibility (arg);
2156 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2158 STRIP_NOPS (arg);
2159 if (TREE_CODE (arg) == ADDR_EXPR)
2160 arg = TREE_OPERAND (arg, 0);
2161 if (VAR_OR_FUNCTION_DECL_P (arg))
2163 if (! TREE_PUBLIC (arg))
2164 vis = VISIBILITY_ANON;
2165 else
2166 vis = DECL_VISIBILITY (arg);
2169 if (vis)
2170 constrain_visibility (decl, vis, true);
2174 /* Like c_determine_visibility, but with additional C++-specific
2175 behavior.
2177 Function-scope entities can rely on the function's visibility because
2178 it is set in start_preparsed_function.
2180 Class-scope entities cannot rely on the class's visibility until the end
2181 of the enclosing class definition.
2183 Note that because namespaces have multiple independent definitions,
2184 namespace visibility is handled elsewhere using the #pragma visibility
2185 machinery rather than by decorating the namespace declaration.
2187 The goal is for constraints from the type to give a diagnostic, and
2188 other constraints to be applied silently. */
2190 void
2191 determine_visibility (tree decl)
2193 tree class_type = NULL_TREE;
2194 bool use_template;
2195 bool orig_visibility_specified;
2196 enum symbol_visibility orig_visibility;
2198 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2200 /* Only relevant for names with external linkage. */
2201 if (!TREE_PUBLIC (decl))
2202 return;
2204 /* Cloned constructors and destructors get the same visibility as
2205 the underlying function. That should be set up in
2206 maybe_clone_body. */
2207 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2209 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2210 orig_visibility = DECL_VISIBILITY (decl);
2212 if (TREE_CODE (decl) == TYPE_DECL)
2214 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2215 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2216 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2217 use_template = 1;
2218 else
2219 use_template = 0;
2221 else if (DECL_LANG_SPECIFIC (decl))
2222 use_template = DECL_USE_TEMPLATE (decl);
2223 else
2224 use_template = 0;
2226 /* If DECL is a member of a class, visibility specifiers on the
2227 class can influence the visibility of the DECL. */
2228 if (DECL_CLASS_SCOPE_P (decl))
2229 class_type = DECL_CONTEXT (decl);
2230 else
2232 /* Not a class member. */
2234 /* Virtual tables have DECL_CONTEXT set to their associated class,
2235 so they are automatically handled above. */
2236 gcc_assert (!VAR_P (decl)
2237 || !DECL_VTABLE_OR_VTT_P (decl));
2239 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2241 /* Local statics and classes get the visibility of their
2242 containing function by default, except that
2243 -fvisibility-inlines-hidden doesn't affect them. */
2244 tree fn = DECL_CONTEXT (decl);
2245 if (DECL_VISIBILITY_SPECIFIED (fn))
2247 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2248 DECL_VISIBILITY_SPECIFIED (decl) =
2249 DECL_VISIBILITY_SPECIFIED (fn);
2251 else
2253 if (DECL_CLASS_SCOPE_P (fn))
2254 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2255 else if (determine_hidden_inline (fn))
2257 DECL_VISIBILITY (decl) = default_visibility;
2258 DECL_VISIBILITY_SPECIFIED (decl) =
2259 visibility_options.inpragma;
2261 else
2263 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2264 DECL_VISIBILITY_SPECIFIED (decl) =
2265 DECL_VISIBILITY_SPECIFIED (fn);
2269 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2270 but have no TEMPLATE_INFO, so don't try to check it. */
2271 use_template = 0;
2273 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2274 && flag_visibility_ms_compat)
2276 /* Under -fvisibility-ms-compat, types are visible by default,
2277 even though their contents aren't. */
2278 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2279 int underlying_vis = type_visibility (underlying_type);
2280 if (underlying_vis == VISIBILITY_ANON
2281 || (CLASS_TYPE_P (underlying_type)
2282 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2283 constrain_visibility (decl, underlying_vis, false);
2284 else
2285 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2287 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2289 /* tinfo visibility is based on the type it's for. */
2290 constrain_visibility
2291 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2293 /* Give the target a chance to override the visibility associated
2294 with DECL. */
2295 if (TREE_PUBLIC (decl)
2296 && !DECL_REALLY_EXTERN (decl)
2297 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2298 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2299 targetm.cxx.determine_class_data_visibility (decl);
2301 else if (use_template)
2302 /* Template instantiations and specializations get visibility based
2303 on their template unless they override it with an attribute. */;
2304 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2306 if (determine_hidden_inline (decl))
2307 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2308 else
2310 /* Set default visibility to whatever the user supplied with
2311 #pragma GCC visibility or a namespace visibility attribute. */
2312 DECL_VISIBILITY (decl) = default_visibility;
2313 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2318 if (use_template)
2320 /* If the specialization doesn't specify visibility, use the
2321 visibility from the template. */
2322 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2323 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2324 : DECL_TEMPLATE_INFO (decl));
2325 tree args = TI_ARGS (tinfo);
2326 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2327 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2328 : DECL_ATTRIBUTES (decl));
2330 if (args != error_mark_node)
2332 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2334 if (!DECL_VISIBILITY_SPECIFIED (decl))
2336 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2337 && determine_hidden_inline (decl))
2338 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2339 else
2341 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2342 DECL_VISIBILITY_SPECIFIED (decl)
2343 = DECL_VISIBILITY_SPECIFIED (pattern);
2347 if (args
2348 /* Template argument visibility outweighs #pragma or namespace
2349 visibility, but not an explicit attribute. */
2350 && !lookup_attribute ("visibility", attribs))
2352 int depth = TMPL_ARGS_DEPTH (args);
2353 if (DECL_VISIBILITY_SPECIFIED (decl))
2355 /* A class template member with explicit visibility
2356 overrides the class visibility, so we need to apply
2357 all the levels of template args directly. */
2358 int i;
2359 for (i = 1; i <= depth; ++i)
2361 tree lev = TMPL_ARGS_LEVEL (args, i);
2362 constrain_visibility_for_template (decl, lev);
2365 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2366 /* Limit visibility based on its template arguments. */
2367 constrain_visibility_for_template (decl, args);
2372 if (class_type)
2373 determine_visibility_from_class (decl, class_type);
2375 if (decl_anon_ns_mem_p (decl))
2376 /* Names in an anonymous namespace get internal linkage.
2377 This might change once we implement export. */
2378 constrain_visibility (decl, VISIBILITY_ANON, false);
2379 else if (TREE_CODE (decl) != TYPE_DECL)
2381 /* Propagate anonymity from type to decl. */
2382 int tvis = type_visibility (TREE_TYPE (decl));
2383 if (tvis == VISIBILITY_ANON
2384 || ! DECL_VISIBILITY_SPECIFIED (decl))
2385 constrain_visibility (decl, tvis, false);
2387 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2388 /* DR 757: A type without linkage shall not be used as the type of a
2389 variable or function with linkage, unless
2390 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2391 o the variable or function is not used (3.2 [basic.def.odr]) or is
2392 defined in the same translation unit.
2394 Since non-extern "C" decls need to be defined in the same
2395 translation unit, we can make the type internal. */
2396 constrain_visibility (decl, VISIBILITY_ANON, false);
2398 /* If visibility changed and DECL already has DECL_RTL, ensure
2399 symbol flags are updated. */
2400 if ((DECL_VISIBILITY (decl) != orig_visibility
2401 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2402 && ((VAR_P (decl) && TREE_STATIC (decl))
2403 || TREE_CODE (decl) == FUNCTION_DECL)
2404 && DECL_RTL_SET_P (decl))
2405 make_decl_rtl (decl);
2408 /* By default, static data members and function members receive
2409 the visibility of their containing class. */
2411 static void
2412 determine_visibility_from_class (tree decl, tree class_type)
2414 if (DECL_VISIBILITY_SPECIFIED (decl))
2415 return;
2417 if (determine_hidden_inline (decl))
2418 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2419 else
2421 /* Default to the class visibility. */
2422 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2423 DECL_VISIBILITY_SPECIFIED (decl)
2424 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2427 /* Give the target a chance to override the visibility associated
2428 with DECL. */
2429 if (VAR_P (decl)
2430 && (DECL_TINFO_P (decl)
2431 || (DECL_VTABLE_OR_VTT_P (decl)
2432 /* Construction virtual tables are not exported because
2433 they cannot be referred to from other object files;
2434 their name is not standardized by the ABI. */
2435 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2436 && TREE_PUBLIC (decl)
2437 && !DECL_REALLY_EXTERN (decl)
2438 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2439 targetm.cxx.determine_class_data_visibility (decl);
2442 /* Returns true iff DECL is an inline that should get hidden visibility
2443 because of -fvisibility-inlines-hidden. */
2445 static bool
2446 determine_hidden_inline (tree decl)
2448 return (visibility_options.inlines_hidden
2449 /* Don't do this for inline templates; specializations might not be
2450 inline, and we don't want them to inherit the hidden
2451 visibility. We'll set it here for all inline instantiations. */
2452 && !processing_template_decl
2453 && TREE_CODE (decl) == FUNCTION_DECL
2454 && DECL_DECLARED_INLINE_P (decl)
2455 && (! DECL_LANG_SPECIFIC (decl)
2456 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2459 /* Constrain the visibility of a class TYPE based on the visibility of its
2460 field types. Warn if any fields require lesser visibility. */
2462 void
2463 constrain_class_visibility (tree type)
2465 tree binfo;
2466 tree t;
2467 int i;
2469 int vis = type_visibility (type);
2471 if (vis == VISIBILITY_ANON
2472 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2473 return;
2475 /* Don't warn about visibility if the class has explicit visibility. */
2476 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2477 vis = VISIBILITY_INTERNAL;
2479 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2480 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2482 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2483 int subvis = type_visibility (ftype);
2485 if (subvis == VISIBILITY_ANON)
2487 if (!in_main_input_context ())
2488 warning (0, "\
2489 %qT has a field %qD whose type uses the anonymous namespace",
2490 type, t);
2492 else if (MAYBE_CLASS_TYPE_P (ftype)
2493 && vis < VISIBILITY_HIDDEN
2494 && subvis >= VISIBILITY_HIDDEN)
2495 warning (OPT_Wattributes, "\
2496 %qT declared with greater visibility than the type of its field %qD",
2497 type, t);
2500 binfo = TYPE_BINFO (type);
2501 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2503 int subvis = type_visibility (TREE_TYPE (t));
2505 if (subvis == VISIBILITY_ANON)
2507 if (!in_main_input_context())
2508 warning (0, "\
2509 %qT has a base %qT whose type uses the anonymous namespace",
2510 type, TREE_TYPE (t));
2512 else if (vis < VISIBILITY_HIDDEN
2513 && subvis >= VISIBILITY_HIDDEN)
2514 warning (OPT_Wattributes, "\
2515 %qT declared with greater visibility than its base %qT",
2516 type, TREE_TYPE (t));
2520 /* Functions for adjusting the visibility of a tagged type and its nested
2521 types and declarations when it gets a name for linkage purposes from a
2522 typedef. */
2524 static void bt_reset_linkage_1 (binding_entry, void *);
2525 static void bt_reset_linkage_2 (binding_entry, void *);
2527 /* First reset the visibility of all the types. */
2529 static void
2530 reset_type_linkage_1 (tree type)
2532 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2533 if (CLASS_TYPE_P (type))
2534 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2535 bt_reset_linkage_1, NULL);
2537 static void
2538 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2540 reset_type_linkage_1 (b->type);
2543 /* Then reset the visibility of any static data members or member
2544 functions that use those types. */
2546 static void
2547 reset_decl_linkage (tree decl)
2549 if (TREE_PUBLIC (decl))
2550 return;
2551 if (DECL_CLONED_FUNCTION_P (decl))
2552 return;
2553 TREE_PUBLIC (decl) = true;
2554 DECL_INTERFACE_KNOWN (decl) = false;
2555 determine_visibility (decl);
2556 tentative_decl_linkage (decl);
2558 static void
2559 reset_type_linkage_2 (tree type)
2561 if (CLASS_TYPE_P (type))
2563 if (tree vt = CLASSTYPE_VTABLES (type))
2565 tree name = mangle_vtbl_for_type (type);
2566 DECL_NAME (vt) = name;
2567 SET_DECL_ASSEMBLER_NAME (vt, name);
2568 reset_decl_linkage (vt);
2570 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2572 tree name = mangle_typeinfo_for_type (type);
2573 DECL_NAME (ti) = name;
2574 SET_DECL_ASSEMBLER_NAME (ti, name);
2575 TREE_TYPE (name) = type;
2576 reset_decl_linkage (ti);
2578 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2579 if (TREE_CODE (m) == VAR_DECL)
2580 reset_decl_linkage (m);
2581 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2582 reset_decl_linkage (m);
2583 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2584 bt_reset_linkage_2, NULL);
2587 static void
2588 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2590 reset_type_linkage_2 (b->type);
2592 void
2593 reset_type_linkage (tree type)
2595 reset_type_linkage_1 (type);
2596 reset_type_linkage_2 (type);
2599 /* Set up our initial idea of what the linkage of DECL should be. */
2601 void
2602 tentative_decl_linkage (tree decl)
2604 if (DECL_INTERFACE_KNOWN (decl))
2605 /* We've already made a decision as to how this function will
2606 be handled. */;
2607 else if (vague_linkage_p (decl))
2609 if (TREE_CODE (decl) == FUNCTION_DECL
2610 && decl_defined_p (decl))
2612 DECL_EXTERNAL (decl) = 1;
2613 DECL_NOT_REALLY_EXTERN (decl) = 1;
2614 note_vague_linkage_fn (decl);
2615 /* A non-template inline function with external linkage will
2616 always be COMDAT. As we must eventually determine the
2617 linkage of all functions, and as that causes writes to
2618 the data mapped in from the PCH file, it's advantageous
2619 to mark the functions at this point. */
2620 if (DECL_DECLARED_INLINE_P (decl)
2621 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2622 || DECL_DEFAULTED_FN (decl)))
2624 /* This function must have external linkage, as
2625 otherwise DECL_INTERFACE_KNOWN would have been
2626 set. */
2627 gcc_assert (TREE_PUBLIC (decl));
2628 comdat_linkage (decl);
2629 DECL_INTERFACE_KNOWN (decl) = 1;
2632 else if (TREE_CODE (decl) == VAR_DECL)
2633 maybe_commonize_var (decl);
2637 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2638 for DECL has not already been determined, do so now by setting
2639 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2640 function is called entities with vague linkage whose definitions
2641 are available must have TREE_PUBLIC set.
2643 If this function decides to place DECL in COMDAT, it will set
2644 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2645 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2646 callers defer that decision until it is clear that DECL is actually
2647 required. */
2649 void
2650 import_export_decl (tree decl)
2652 int emit_p;
2653 bool comdat_p;
2654 bool import_p;
2655 tree class_type = NULL_TREE;
2657 if (DECL_INTERFACE_KNOWN (decl))
2658 return;
2660 /* We cannot determine what linkage to give to an entity with vague
2661 linkage until the end of the file. For example, a virtual table
2662 for a class will be defined if and only if the key method is
2663 defined in this translation unit. As a further example, consider
2664 that when compiling a translation unit that uses PCH file with
2665 "-frepo" it would be incorrect to make decisions about what
2666 entities to emit when building the PCH; those decisions must be
2667 delayed until the repository information has been processed. */
2668 gcc_assert (at_eof);
2669 /* Object file linkage for explicit instantiations is handled in
2670 mark_decl_instantiated. For static variables in functions with
2671 vague linkage, maybe_commonize_var is used.
2673 Therefore, the only declarations that should be provided to this
2674 function are those with external linkage that are:
2676 * implicit instantiations of function templates
2678 * inline function
2680 * implicit instantiations of static data members of class
2681 templates
2683 * virtual tables
2685 * typeinfo objects
2687 Furthermore, all entities that reach this point must have a
2688 definition available in this translation unit.
2690 The following assertions check these conditions. */
2691 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2692 /* Any code that creates entities with TREE_PUBLIC cleared should
2693 also set DECL_INTERFACE_KNOWN. */
2694 gcc_assert (TREE_PUBLIC (decl));
2695 if (TREE_CODE (decl) == FUNCTION_DECL)
2696 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2697 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2698 || DECL_DECLARED_INLINE_P (decl));
2699 else
2700 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2701 || DECL_VTABLE_OR_VTT_P (decl)
2702 || DECL_TINFO_P (decl));
2703 /* Check that a definition of DECL is available in this translation
2704 unit. */
2705 gcc_assert (!DECL_REALLY_EXTERN (decl));
2707 /* Assume that DECL will not have COMDAT linkage. */
2708 comdat_p = false;
2709 /* Assume that DECL will not be imported into this translation
2710 unit. */
2711 import_p = false;
2713 /* See if the repository tells us whether or not to emit DECL in
2714 this translation unit. */
2715 emit_p = repo_emit_p (decl);
2716 if (emit_p == 0)
2717 import_p = true;
2718 else if (emit_p == 1)
2720 /* The repository indicates that this entity should be defined
2721 here. Make sure the back end honors that request. */
2722 if (VAR_P (decl))
2723 mark_needed (decl);
2724 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2725 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2727 tree clone;
2728 FOR_EACH_CLONE (clone, decl)
2729 mark_needed (clone);
2731 else
2732 mark_needed (decl);
2733 /* Output the definition as an ordinary strong definition. */
2734 DECL_EXTERNAL (decl) = 0;
2735 DECL_INTERFACE_KNOWN (decl) = 1;
2736 return;
2739 if (import_p)
2740 /* We have already decided what to do with this DECL; there is no
2741 need to check anything further. */
2743 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2745 class_type = DECL_CONTEXT (decl);
2746 import_export_class (class_type);
2747 if (TYPE_FOR_JAVA (class_type))
2748 import_p = true;
2749 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2750 && CLASSTYPE_INTERFACE_ONLY (class_type))
2751 import_p = true;
2752 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2753 && !CLASSTYPE_USE_TEMPLATE (class_type)
2754 && CLASSTYPE_KEY_METHOD (class_type)
2755 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2756 /* The ABI requires that all virtual tables be emitted with
2757 COMDAT linkage. However, on systems where COMDAT symbols
2758 don't show up in the table of contents for a static
2759 archive, or on systems without weak symbols (where we
2760 approximate COMDAT linkage by using internal linkage), the
2761 linker will report errors about undefined symbols because
2762 it will not see the virtual table definition. Therefore,
2763 in the case that we know that the virtual table will be
2764 emitted in only one translation unit, we make the virtual
2765 table an ordinary definition with external linkage. */
2766 DECL_EXTERNAL (decl) = 0;
2767 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2769 /* CLASS_TYPE is being exported from this translation unit,
2770 so DECL should be defined here. */
2771 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2772 /* If a class is declared in a header with the "extern
2773 template" extension, then it will not be instantiated,
2774 even in translation units that would normally require
2775 it. Often such classes are explicitly instantiated in
2776 one translation unit. Therefore, the explicit
2777 instantiation must be made visible to other translation
2778 units. */
2779 DECL_EXTERNAL (decl) = 0;
2780 else
2782 /* The generic C++ ABI says that class data is always
2783 COMDAT, even if there is a key function. Some
2784 variants (e.g., the ARM EABI) says that class data
2785 only has COMDAT linkage if the class data might be
2786 emitted in more than one translation unit. When the
2787 key method can be inline and is inline, we still have
2788 to arrange for comdat even though
2789 class_data_always_comdat is false. */
2790 if (!CLASSTYPE_KEY_METHOD (class_type)
2791 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2792 || targetm.cxx.class_data_always_comdat ())
2794 /* The ABI requires COMDAT linkage. Normally, we
2795 only emit COMDAT things when they are needed;
2796 make sure that we realize that this entity is
2797 indeed needed. */
2798 comdat_p = true;
2799 mark_needed (decl);
2803 else if (!flag_implicit_templates
2804 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2805 import_p = true;
2806 else
2807 comdat_p = true;
2809 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2811 tree type = TREE_TYPE (DECL_NAME (decl));
2812 if (CLASS_TYPE_P (type))
2814 class_type = type;
2815 import_export_class (type);
2816 if (CLASSTYPE_INTERFACE_KNOWN (type)
2817 && TYPE_POLYMORPHIC_P (type)
2818 && CLASSTYPE_INTERFACE_ONLY (type)
2819 /* If -fno-rtti was specified, then we cannot be sure
2820 that RTTI information will be emitted with the
2821 virtual table of the class, so we must emit it
2822 wherever it is used. */
2823 && flag_rtti)
2824 import_p = true;
2825 else
2827 if (CLASSTYPE_INTERFACE_KNOWN (type)
2828 && !CLASSTYPE_INTERFACE_ONLY (type))
2830 comdat_p = (targetm.cxx.class_data_always_comdat ()
2831 || (CLASSTYPE_KEY_METHOD (type)
2832 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2833 mark_needed (decl);
2834 if (!flag_weak)
2836 comdat_p = false;
2837 DECL_EXTERNAL (decl) = 0;
2840 else
2841 comdat_p = true;
2844 else
2845 comdat_p = true;
2847 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2849 /* DECL is an implicit instantiation of a function or static
2850 data member. */
2851 if ((flag_implicit_templates
2852 && !flag_use_repository)
2853 || (flag_implicit_inline_templates
2854 && TREE_CODE (decl) == FUNCTION_DECL
2855 && DECL_DECLARED_INLINE_P (decl)))
2856 comdat_p = true;
2857 else
2858 /* If we are not implicitly generating templates, then mark
2859 this entity as undefined in this translation unit. */
2860 import_p = true;
2862 else if (DECL_FUNCTION_MEMBER_P (decl))
2864 if (!DECL_DECLARED_INLINE_P (decl))
2866 tree ctype = DECL_CONTEXT (decl);
2867 import_export_class (ctype);
2868 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2870 DECL_NOT_REALLY_EXTERN (decl)
2871 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2872 || (DECL_DECLARED_INLINE_P (decl)
2873 && ! flag_implement_inlines
2874 && !DECL_VINDEX (decl)));
2876 if (!DECL_NOT_REALLY_EXTERN (decl))
2877 DECL_EXTERNAL (decl) = 1;
2879 /* Always make artificials weak. */
2880 if (DECL_ARTIFICIAL (decl) && flag_weak)
2881 comdat_p = true;
2882 else
2883 maybe_make_one_only (decl);
2886 else
2887 comdat_p = true;
2889 else
2890 comdat_p = true;
2892 if (import_p)
2894 /* If we are importing DECL into this translation unit, mark is
2895 an undefined here. */
2896 DECL_EXTERNAL (decl) = 1;
2897 DECL_NOT_REALLY_EXTERN (decl) = 0;
2899 else if (comdat_p)
2901 /* If we decided to put DECL in COMDAT, mark it accordingly at
2902 this point. */
2903 comdat_linkage (decl);
2906 DECL_INTERFACE_KNOWN (decl) = 1;
2909 /* Return an expression that performs the destruction of DECL, which
2910 must be a VAR_DECL whose type has a non-trivial destructor, or is
2911 an array whose (innermost) elements have a non-trivial destructor. */
2913 tree
2914 build_cleanup (tree decl)
2916 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2917 gcc_assert (clean != NULL_TREE);
2918 return clean;
2921 /* Returns the initialization guard variable for the variable DECL,
2922 which has static storage duration. */
2924 tree
2925 get_guard (tree decl)
2927 tree sname;
2928 tree guard;
2930 sname = mangle_guard_variable (decl);
2931 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2932 if (! guard)
2934 tree guard_type;
2936 /* We use a type that is big enough to contain a mutex as well
2937 as an integer counter. */
2938 guard_type = targetm.cxx.guard_type ();
2939 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2940 VAR_DECL, sname, guard_type);
2942 /* The guard should have the same linkage as what it guards. */
2943 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2944 TREE_STATIC (guard) = TREE_STATIC (decl);
2945 DECL_COMMON (guard) = DECL_COMMON (decl);
2946 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2947 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2948 if (DECL_ONE_ONLY (decl))
2949 make_decl_one_only (guard, cxx_comdat_group (guard));
2950 if (TREE_PUBLIC (decl))
2951 DECL_WEAK (guard) = DECL_WEAK (decl);
2952 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2953 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2955 DECL_ARTIFICIAL (guard) = 1;
2956 DECL_IGNORED_P (guard) = 1;
2957 TREE_USED (guard) = 1;
2958 pushdecl_top_level_and_finish (guard, NULL_TREE);
2960 return guard;
2963 /* Return those bits of the GUARD variable that should be set when the
2964 guarded entity is actually initialized. */
2966 static tree
2967 get_guard_bits (tree guard)
2969 if (!targetm.cxx.guard_mask_bit ())
2971 /* We only set the first byte of the guard, in order to leave room
2972 for a mutex in the high-order bits. */
2973 guard = build1 (ADDR_EXPR,
2974 build_pointer_type (TREE_TYPE (guard)),
2975 guard);
2976 guard = build1 (NOP_EXPR,
2977 build_pointer_type (char_type_node),
2978 guard);
2979 guard = build1 (INDIRECT_REF, char_type_node, guard);
2982 return guard;
2985 /* Return an expression which determines whether or not the GUARD
2986 variable has already been initialized. */
2988 tree
2989 get_guard_cond (tree guard)
2991 tree guard_value;
2993 /* Check to see if the GUARD is zero. */
2994 guard = get_guard_bits (guard);
2996 /* Mask off all but the low bit. */
2997 if (targetm.cxx.guard_mask_bit ())
2999 guard_value = integer_one_node;
3000 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3001 guard_value = convert (TREE_TYPE (guard), guard_value);
3002 guard = cp_build_binary_op (input_location,
3003 BIT_AND_EXPR, guard, guard_value,
3004 tf_warning_or_error);
3007 guard_value = integer_zero_node;
3008 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3009 guard_value = convert (TREE_TYPE (guard), guard_value);
3010 return cp_build_binary_op (input_location,
3011 EQ_EXPR, guard, guard_value,
3012 tf_warning_or_error);
3015 /* Return an expression which sets the GUARD variable, indicating that
3016 the variable being guarded has been initialized. */
3018 tree
3019 set_guard (tree guard)
3021 tree guard_init;
3023 /* Set the GUARD to one. */
3024 guard = get_guard_bits (guard);
3025 guard_init = integer_one_node;
3026 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3027 guard_init = convert (TREE_TYPE (guard), guard_init);
3028 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3029 tf_warning_or_error);
3032 /* Returns true iff we can tell that VAR does not have a dynamic
3033 initializer. */
3035 static bool
3036 var_defined_without_dynamic_init (tree var)
3038 /* If it's defined in another TU, we can't tell. */
3039 if (DECL_EXTERNAL (var))
3040 return false;
3041 /* If it has a non-trivial destructor, registering the destructor
3042 counts as dynamic initialization. */
3043 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3044 return false;
3045 /* If it's in this TU, its initializer has been processed. */
3046 gcc_assert (DECL_INITIALIZED_P (var));
3047 /* If it has no initializer or a constant one, it's not dynamic. */
3048 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3049 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3052 /* Returns true iff VAR is a variable that needs uses to be
3053 wrapped for possible dynamic initialization. */
3055 static bool
3056 var_needs_tls_wrapper (tree var)
3058 return (!error_operand_p (var)
3059 && DECL_THREAD_LOCAL_P (var)
3060 && !DECL_GNU_TLS_P (var)
3061 && !DECL_FUNCTION_SCOPE_P (var)
3062 && !var_defined_without_dynamic_init (var));
3065 /* Get the FUNCTION_DECL for the shared TLS init function for this
3066 translation unit. */
3068 static tree
3069 get_local_tls_init_fn (void)
3071 tree sname = get_identifier ("__tls_init");
3072 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3073 if (!fn)
3075 fn = build_lang_decl (FUNCTION_DECL, sname,
3076 build_function_type (void_type_node,
3077 void_list_node));
3078 SET_DECL_LANGUAGE (fn, lang_c);
3079 TREE_PUBLIC (fn) = false;
3080 DECL_ARTIFICIAL (fn) = true;
3081 mark_used (fn);
3082 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3084 return fn;
3087 /* Get a FUNCTION_DECL for the init function for the thread_local
3088 variable VAR. The init function will be an alias to the function
3089 that initializes all the non-local TLS variables in the translation
3090 unit. The init function is only used by the wrapper function. */
3092 static tree
3093 get_tls_init_fn (tree var)
3095 /* Only C++11 TLS vars need this init fn. */
3096 if (!var_needs_tls_wrapper (var))
3097 return NULL_TREE;
3099 /* If -fno-extern-tls-init, assume that we don't need to call
3100 a tls init function for a variable defined in another TU. */
3101 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3102 return NULL_TREE;
3104 #ifdef ASM_OUTPUT_DEF
3105 /* If the variable is internal, or if we can't generate aliases,
3106 call the local init function directly. */
3107 if (!TREE_PUBLIC (var))
3108 #endif
3109 return get_local_tls_init_fn ();
3111 tree sname = mangle_tls_init_fn (var);
3112 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3113 if (!fn)
3115 fn = build_lang_decl (FUNCTION_DECL, sname,
3116 build_function_type (void_type_node,
3117 void_list_node));
3118 SET_DECL_LANGUAGE (fn, lang_c);
3119 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3120 DECL_ARTIFICIAL (fn) = true;
3121 DECL_COMDAT (fn) = DECL_COMDAT (var);
3122 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3123 if (DECL_ONE_ONLY (var))
3124 make_decl_one_only (fn, cxx_comdat_group (fn));
3125 if (TREE_PUBLIC (var))
3127 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3128 /* If the variable is defined somewhere else and might have static
3129 initialization, make the init function a weak reference. */
3130 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3131 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3132 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3133 && DECL_EXTERNAL (var))
3134 declare_weak (fn);
3135 else
3136 DECL_WEAK (fn) = DECL_WEAK (var);
3138 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3139 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3140 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3141 DECL_IGNORED_P (fn) = 1;
3142 mark_used (fn);
3144 DECL_BEFRIENDING_CLASSES (fn) = var;
3146 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3148 return fn;
3151 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3152 variable VAR. The wrapper function calls the init function (if any) for
3153 VAR and then returns a reference to VAR. The wrapper function is used
3154 in place of VAR everywhere VAR is mentioned. */
3156 tree
3157 get_tls_wrapper_fn (tree var)
3159 /* Only C++11 TLS vars need this wrapper fn. */
3160 if (!var_needs_tls_wrapper (var))
3161 return NULL_TREE;
3163 tree sname = mangle_tls_wrapper_fn (var);
3164 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3165 if (!fn)
3167 /* A named rvalue reference is an lvalue, so the wrapper should
3168 always return an lvalue reference. */
3169 tree type = non_reference (TREE_TYPE (var));
3170 type = build_reference_type (type);
3171 tree fntype = build_function_type (type, void_list_node);
3172 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3173 SET_DECL_LANGUAGE (fn, lang_c);
3174 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3175 DECL_ARTIFICIAL (fn) = true;
3176 DECL_IGNORED_P (fn) = 1;
3177 /* The wrapper is inline and emitted everywhere var is used. */
3178 DECL_DECLARED_INLINE_P (fn) = true;
3179 if (TREE_PUBLIC (var))
3181 comdat_linkage (fn);
3182 #ifdef HAVE_GAS_HIDDEN
3183 /* Make the wrapper bind locally; there's no reason to share
3184 the wrapper between multiple shared objects. */
3185 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3186 DECL_VISIBILITY_SPECIFIED (fn) = true;
3187 #endif
3189 if (!TREE_PUBLIC (fn))
3190 DECL_INTERFACE_KNOWN (fn) = true;
3191 mark_used (fn);
3192 note_vague_linkage_fn (fn);
3194 #if 0
3195 /* We want CSE to commonize calls to the wrapper, but marking it as
3196 pure is unsafe since it has side-effects. I guess we need a new
3197 ECF flag even weaker than ECF_PURE. FIXME! */
3198 DECL_PURE_P (fn) = true;
3199 #endif
3201 DECL_BEFRIENDING_CLASSES (fn) = var;
3203 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3205 return fn;
3208 /* At EOF, generate the definition for the TLS wrapper function FN:
3210 T& var_wrapper() {
3211 if (init_fn) init_fn();
3212 return var;
3213 } */
3215 static void
3216 generate_tls_wrapper (tree fn)
3218 tree var = DECL_BEFRIENDING_CLASSES (fn);
3220 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3221 tree body = begin_function_body ();
3222 /* Only call the init fn if there might be one. */
3223 if (tree init_fn = get_tls_init_fn (var))
3225 tree if_stmt = NULL_TREE;
3226 /* If init_fn is a weakref, make sure it exists before calling. */
3227 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3229 if_stmt = begin_if_stmt ();
3230 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3231 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3232 NE_EXPR, addr, nullptr_node,
3233 tf_warning_or_error);
3234 finish_if_stmt_cond (cond, if_stmt);
3236 finish_expr_stmt (build_cxx_call
3237 (init_fn, 0, NULL, tf_warning_or_error));
3238 if (if_stmt)
3240 finish_then_clause (if_stmt);
3241 finish_if_stmt (if_stmt);
3244 else
3245 /* If there's no initialization, the wrapper is a constant function. */
3246 TREE_READONLY (fn) = true;
3247 finish_return_stmt (convert_from_reference (var));
3248 finish_function_body (body);
3249 expand_or_defer_fn (finish_function (0));
3252 /* Start the process of running a particular set of global constructors
3253 or destructors. Subroutine of do_[cd]tors. Also called from
3254 vtv_start_verification_constructor_init_function. */
3256 static tree
3257 start_objects (int method_type, int initp)
3259 tree body;
3260 tree fndecl;
3261 char type[14];
3263 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3265 if (initp != DEFAULT_INIT_PRIORITY)
3267 char joiner;
3269 #ifdef JOINER
3270 joiner = JOINER;
3271 #else
3272 joiner = '_';
3273 #endif
3275 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3277 else
3278 sprintf (type, "sub_%c", method_type);
3280 fndecl = build_lang_decl (FUNCTION_DECL,
3281 get_file_function_name (type),
3282 build_function_type_list (void_type_node,
3283 NULL_TREE));
3284 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3286 TREE_PUBLIC (current_function_decl) = 0;
3288 /* Mark as artificial because it's not explicitly in the user's
3289 source code. */
3290 DECL_ARTIFICIAL (current_function_decl) = 1;
3292 /* Mark this declaration as used to avoid spurious warnings. */
3293 TREE_USED (current_function_decl) = 1;
3295 /* Mark this function as a global constructor or destructor. */
3296 if (method_type == 'I')
3297 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3298 else
3299 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3301 body = begin_compound_stmt (BCS_FN_BODY);
3303 return body;
3306 /* Finish the process of running a particular set of global constructors
3307 or destructors. Subroutine of do_[cd]tors. */
3309 static void
3310 finish_objects (int method_type, int initp, tree body)
3312 tree fn;
3314 /* Finish up. */
3315 finish_compound_stmt (body);
3316 fn = finish_function (0);
3318 if (method_type == 'I')
3320 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3321 decl_init_priority_insert (fn, initp);
3323 else
3325 DECL_STATIC_DESTRUCTOR (fn) = 1;
3326 decl_fini_priority_insert (fn, initp);
3329 expand_or_defer_fn (fn);
3332 /* The names of the parameters to the function created to handle
3333 initializations and destructions for objects with static storage
3334 duration. */
3335 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3336 #define PRIORITY_IDENTIFIER "__priority"
3338 /* The name of the function we create to handle initializations and
3339 destructions for objects with static storage duration. */
3340 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3342 /* The declaration for the __INITIALIZE_P argument. */
3343 static GTY(()) tree initialize_p_decl;
3345 /* The declaration for the __PRIORITY argument. */
3346 static GTY(()) tree priority_decl;
3348 /* The declaration for the static storage duration function. */
3349 static GTY(()) tree ssdf_decl;
3351 /* All the static storage duration functions created in this
3352 translation unit. */
3353 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3355 /* A map from priority levels to information about that priority
3356 level. There may be many such levels, so efficient lookup is
3357 important. */
3358 static splay_tree priority_info_map;
3360 /* Begins the generation of the function that will handle all
3361 initialization and destruction of objects with static storage
3362 duration. The function generated takes two parameters of type
3363 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3364 nonzero, it performs initializations. Otherwise, it performs
3365 destructions. It only performs those initializations or
3366 destructions with the indicated __PRIORITY. The generated function
3367 returns no value.
3369 It is assumed that this function will only be called once per
3370 translation unit. */
3372 static tree
3373 start_static_storage_duration_function (unsigned count)
3375 tree type;
3376 tree body;
3377 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3379 /* Create the identifier for this function. It will be of the form
3380 SSDF_IDENTIFIER_<number>. */
3381 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3383 type = build_function_type_list (void_type_node,
3384 integer_type_node, integer_type_node,
3385 NULL_TREE);
3387 /* Create the FUNCTION_DECL itself. */
3388 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3389 get_identifier (id),
3390 type);
3391 TREE_PUBLIC (ssdf_decl) = 0;
3392 DECL_ARTIFICIAL (ssdf_decl) = 1;
3394 /* Put this function in the list of functions to be called from the
3395 static constructors and destructors. */
3396 if (!ssdf_decls)
3398 vec_alloc (ssdf_decls, 32);
3400 /* Take this opportunity to initialize the map from priority
3401 numbers to information about that priority level. */
3402 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3403 /*delete_key_fn=*/0,
3404 /*delete_value_fn=*/
3405 (splay_tree_delete_value_fn) &free);
3407 /* We always need to generate functions for the
3408 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3409 priorities later, we'll be sure to find the
3410 DEFAULT_INIT_PRIORITY. */
3411 get_priority_info (DEFAULT_INIT_PRIORITY);
3414 vec_safe_push (ssdf_decls, ssdf_decl);
3416 /* Create the argument list. */
3417 initialize_p_decl = cp_build_parm_decl
3418 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3419 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3420 TREE_USED (initialize_p_decl) = 1;
3421 priority_decl = cp_build_parm_decl
3422 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3423 DECL_CONTEXT (priority_decl) = ssdf_decl;
3424 TREE_USED (priority_decl) = 1;
3426 DECL_CHAIN (initialize_p_decl) = priority_decl;
3427 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3429 /* Put the function in the global scope. */
3430 pushdecl (ssdf_decl);
3432 /* Start the function itself. This is equivalent to declaring the
3433 function as:
3435 static void __ssdf (int __initialize_p, init __priority_p);
3437 It is static because we only need to call this function from the
3438 various constructor and destructor functions for this module. */
3439 start_preparsed_function (ssdf_decl,
3440 /*attrs=*/NULL_TREE,
3441 SF_PRE_PARSED);
3443 /* Set up the scope of the outermost block in the function. */
3444 body = begin_compound_stmt (BCS_FN_BODY);
3446 return body;
3449 /* Finish the generation of the function which performs initialization
3450 and destruction of objects with static storage duration. After
3451 this point, no more such objects can be created. */
3453 static void
3454 finish_static_storage_duration_function (tree body)
3456 /* Close out the function. */
3457 finish_compound_stmt (body);
3458 expand_or_defer_fn (finish_function (0));
3461 /* Return the information about the indicated PRIORITY level. If no
3462 code to handle this level has yet been generated, generate the
3463 appropriate prologue. */
3465 static priority_info
3466 get_priority_info (int priority)
3468 priority_info pi;
3469 splay_tree_node n;
3471 n = splay_tree_lookup (priority_info_map,
3472 (splay_tree_key) priority);
3473 if (!n)
3475 /* Create a new priority information structure, and insert it
3476 into the map. */
3477 pi = XNEW (struct priority_info_s);
3478 pi->initializations_p = 0;
3479 pi->destructions_p = 0;
3480 splay_tree_insert (priority_info_map,
3481 (splay_tree_key) priority,
3482 (splay_tree_value) pi);
3484 else
3485 pi = (priority_info) n->value;
3487 return pi;
3490 /* The effective initialization priority of a DECL. */
3492 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3493 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3494 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3496 /* Whether a DECL needs a guard to protect it against multiple
3497 initialization. */
3499 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3500 || DECL_ONE_ONLY (decl) \
3501 || DECL_WEAK (decl)))
3503 /* Called from one_static_initialization_or_destruction(),
3504 via walk_tree.
3505 Walks the initializer list of a global variable and looks for
3506 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3507 and that have their DECL_CONTEXT() == NULL.
3508 For each such temporary variable, set their DECL_CONTEXT() to
3509 the current function. This is necessary because otherwise
3510 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3511 when trying to refer to a temporary variable that does not have
3512 it's DECL_CONTECT() properly set. */
3513 static tree
3514 fix_temporary_vars_context_r (tree *node,
3515 int * /*unused*/,
3516 void * /*unused1*/)
3518 gcc_assert (current_function_decl);
3520 if (TREE_CODE (*node) == BIND_EXPR)
3522 tree var;
3524 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3525 if (VAR_P (var)
3526 && !DECL_NAME (var)
3527 && DECL_ARTIFICIAL (var)
3528 && !DECL_CONTEXT (var))
3529 DECL_CONTEXT (var) = current_function_decl;
3532 return NULL_TREE;
3535 /* Set up to handle the initialization or destruction of DECL. If
3536 INITP is nonzero, we are initializing the variable. Otherwise, we
3537 are destroying it. */
3539 static void
3540 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3542 tree guard_if_stmt = NULL_TREE;
3543 tree guard;
3545 /* If we are supposed to destruct and there's a trivial destructor,
3546 nothing has to be done. */
3547 if (!initp
3548 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3549 return;
3551 /* Trick the compiler into thinking we are at the file and line
3552 where DECL was declared so that error-messages make sense, and so
3553 that the debugger will show somewhat sensible file and line
3554 information. */
3555 input_location = DECL_SOURCE_LOCATION (decl);
3557 /* Make sure temporary variables in the initialiser all have
3558 their DECL_CONTEXT() set to a value different from NULL_TREE.
3559 This can happen when global variables initialisers are built.
3560 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3561 the temporary variables that might have been generated in the
3562 accompagning initialisers is NULL_TREE, meaning the variables have been
3563 declared in the global namespace.
3564 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3565 of the temporaries are set to the current function decl. */
3566 cp_walk_tree_without_duplicates (&init,
3567 fix_temporary_vars_context_r,
3568 NULL);
3570 /* Because of:
3572 [class.access.spec]
3574 Access control for implicit calls to the constructors,
3575 the conversion functions, or the destructor called to
3576 create and destroy a static data member is performed as
3577 if these calls appeared in the scope of the member's
3578 class.
3580 we pretend we are in a static member function of the class of
3581 which the DECL is a member. */
3582 if (member_p (decl))
3584 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3585 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3588 /* Assume we don't need a guard. */
3589 guard = NULL_TREE;
3590 /* We need a guard if this is an object with external linkage that
3591 might be initialized in more than one place. (For example, a
3592 static data member of a template, when the data member requires
3593 construction.) */
3594 if (NEEDS_GUARD_P (decl))
3596 tree guard_cond;
3598 guard = get_guard (decl);
3600 /* When using __cxa_atexit, we just check the GUARD as we would
3601 for a local static. */
3602 if (flag_use_cxa_atexit)
3604 /* When using __cxa_atexit, we never try to destroy
3605 anything from a static destructor. */
3606 gcc_assert (initp);
3607 guard_cond = get_guard_cond (guard);
3609 /* If we don't have __cxa_atexit, then we will be running
3610 destructors from .fini sections, or their equivalents. So,
3611 we need to know how many times we've tried to initialize this
3612 object. We do initializations only if the GUARD is zero,
3613 i.e., if we are the first to initialize the variable. We do
3614 destructions only if the GUARD is one, i.e., if we are the
3615 last to destroy the variable. */
3616 else if (initp)
3617 guard_cond
3618 = cp_build_binary_op (input_location,
3619 EQ_EXPR,
3620 cp_build_unary_op (PREINCREMENT_EXPR,
3621 guard,
3622 /*noconvert=*/1,
3623 tf_warning_or_error),
3624 integer_one_node,
3625 tf_warning_or_error);
3626 else
3627 guard_cond
3628 = cp_build_binary_op (input_location,
3629 EQ_EXPR,
3630 cp_build_unary_op (PREDECREMENT_EXPR,
3631 guard,
3632 /*noconvert=*/1,
3633 tf_warning_or_error),
3634 integer_zero_node,
3635 tf_warning_or_error);
3637 guard_if_stmt = begin_if_stmt ();
3638 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3642 /* If we're using __cxa_atexit, we have not already set the GUARD,
3643 so we must do so now. */
3644 if (guard && initp && flag_use_cxa_atexit)
3645 finish_expr_stmt (set_guard (guard));
3647 /* Perform the initialization or destruction. */
3648 if (initp)
3650 if (init)
3652 finish_expr_stmt (init);
3653 if (flag_sanitize & SANITIZE_ADDRESS)
3655 varpool_node *vnode = varpool_get_node (decl);
3656 if (vnode)
3657 vnode->dynamically_initialized = 1;
3661 /* If we're using __cxa_atexit, register a function that calls the
3662 destructor for the object. */
3663 if (flag_use_cxa_atexit)
3664 finish_expr_stmt (register_dtor_fn (decl));
3666 else
3667 finish_expr_stmt (build_cleanup (decl));
3669 /* Finish the guard if-stmt, if necessary. */
3670 if (guard)
3672 finish_then_clause (guard_if_stmt);
3673 finish_if_stmt (guard_if_stmt);
3676 /* Now that we're done with DECL we don't need to pretend to be a
3677 member of its class any longer. */
3678 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3679 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3682 /* Generate code to do the initialization or destruction of the decls in VARS,
3683 a TREE_LIST of VAR_DECL with static storage duration.
3684 Whether initialization or destruction is performed is specified by INITP. */
3686 static void
3687 do_static_initialization_or_destruction (tree vars, bool initp)
3689 tree node, init_if_stmt, cond;
3691 /* Build the outer if-stmt to check for initialization or destruction. */
3692 init_if_stmt = begin_if_stmt ();
3693 cond = initp ? integer_one_node : integer_zero_node;
3694 cond = cp_build_binary_op (input_location,
3695 EQ_EXPR,
3696 initialize_p_decl,
3697 cond,
3698 tf_warning_or_error);
3699 finish_if_stmt_cond (cond, init_if_stmt);
3701 /* To make sure dynamic construction doesn't access globals from other
3702 compilation units where they might not be yet constructed, for
3703 -fsanitize=address insert __asan_before_dynamic_init call that
3704 prevents access to either all global variables that need construction
3705 in other compilation units, or at least those that haven't been
3706 initialized yet. Variables that need dynamic construction in
3707 the current compilation unit are kept accessible. */
3708 if (flag_sanitize & SANITIZE_ADDRESS)
3709 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3711 node = vars;
3712 do {
3713 tree decl = TREE_VALUE (node);
3714 tree priority_if_stmt;
3715 int priority;
3716 priority_info pi;
3718 /* If we don't need a destructor, there's nothing to do. Avoid
3719 creating a possibly empty if-stmt. */
3720 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3722 node = TREE_CHAIN (node);
3723 continue;
3726 /* Remember that we had an initialization or finalization at this
3727 priority. */
3728 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3729 pi = get_priority_info (priority);
3730 if (initp)
3731 pi->initializations_p = 1;
3732 else
3733 pi->destructions_p = 1;
3735 /* Conditionalize this initialization on being in the right priority
3736 and being initializing/finalizing appropriately. */
3737 priority_if_stmt = begin_if_stmt ();
3738 cond = cp_build_binary_op (input_location,
3739 EQ_EXPR,
3740 priority_decl,
3741 build_int_cst (NULL_TREE, priority),
3742 tf_warning_or_error);
3743 finish_if_stmt_cond (cond, priority_if_stmt);
3745 /* Process initializers with same priority. */
3746 for (; node
3747 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3748 node = TREE_CHAIN (node))
3749 /* Do one initialization or destruction. */
3750 one_static_initialization_or_destruction (TREE_VALUE (node),
3751 TREE_PURPOSE (node), initp);
3753 /* Finish up the priority if-stmt body. */
3754 finish_then_clause (priority_if_stmt);
3755 finish_if_stmt (priority_if_stmt);
3757 } while (node);
3759 /* Revert what __asan_before_dynamic_init did by calling
3760 __asan_after_dynamic_init. */
3761 if (flag_sanitize & SANITIZE_ADDRESS)
3762 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3764 /* Finish up the init/destruct if-stmt body. */
3765 finish_then_clause (init_if_stmt);
3766 finish_if_stmt (init_if_stmt);
3769 /* VARS is a list of variables with static storage duration which may
3770 need initialization and/or finalization. Remove those variables
3771 that don't really need to be initialized or finalized, and return
3772 the resulting list. The order in which the variables appear in
3773 VARS is in reverse order of the order in which they should actually
3774 be initialized. The list we return is in the unreversed order;
3775 i.e., the first variable should be initialized first. */
3777 static tree
3778 prune_vars_needing_no_initialization (tree *vars)
3780 tree *var = vars;
3781 tree result = NULL_TREE;
3783 while (*var)
3785 tree t = *var;
3786 tree decl = TREE_VALUE (t);
3787 tree init = TREE_PURPOSE (t);
3789 /* Deal gracefully with error. */
3790 if (decl == error_mark_node)
3792 var = &TREE_CHAIN (t);
3793 continue;
3796 /* The only things that can be initialized are variables. */
3797 gcc_assert (VAR_P (decl));
3799 /* If this object is not defined, we don't need to do anything
3800 here. */
3801 if (DECL_EXTERNAL (decl))
3803 var = &TREE_CHAIN (t);
3804 continue;
3807 /* Also, if the initializer already contains errors, we can bail
3808 out now. */
3809 if (init && TREE_CODE (init) == TREE_LIST
3810 && value_member (error_mark_node, init))
3812 var = &TREE_CHAIN (t);
3813 continue;
3816 /* This variable is going to need initialization and/or
3817 finalization, so we add it to the list. */
3818 *var = TREE_CHAIN (t);
3819 TREE_CHAIN (t) = result;
3820 result = t;
3823 return result;
3826 /* Make sure we have told the back end about all the variables in
3827 VARS. */
3829 static void
3830 write_out_vars (tree vars)
3832 tree v;
3834 for (v = vars; v; v = TREE_CHAIN (v))
3836 tree var = TREE_VALUE (v);
3837 if (!var_finalized_p (var))
3839 import_export_decl (var);
3840 rest_of_decl_compilation (var, 1, 1);
3845 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3846 (otherwise) that will initialize all global objects with static
3847 storage duration having the indicated PRIORITY. */
3849 static void
3850 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3851 location_t *locus)
3853 char function_key;
3854 tree fndecl;
3855 tree body;
3856 size_t i;
3858 input_location = *locus;
3859 /* ??? */
3860 /* Was: locus->line++; */
3862 /* We use `I' to indicate initialization and `D' to indicate
3863 destruction. */
3864 function_key = constructor_p ? 'I' : 'D';
3866 /* We emit the function lazily, to avoid generating empty
3867 global constructors and destructors. */
3868 body = NULL_TREE;
3870 /* For Objective-C++, we may need to initialize metadata found in this module.
3871 This must be done _before_ any other static initializations. */
3872 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3873 && constructor_p && objc_static_init_needed_p ())
3875 body = start_objects (function_key, priority);
3876 objc_generate_static_init_call (NULL_TREE);
3879 /* Call the static storage duration function with appropriate
3880 arguments. */
3881 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3883 /* Calls to pure or const functions will expand to nothing. */
3884 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3886 tree call;
3888 if (! body)
3889 body = start_objects (function_key, priority);
3891 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3892 build_int_cst (NULL_TREE,
3893 constructor_p),
3894 build_int_cst (NULL_TREE,
3895 priority),
3896 NULL_TREE);
3897 finish_expr_stmt (call);
3901 /* Close out the function. */
3902 if (body)
3903 finish_objects (function_key, priority, body);
3906 /* Generate constructor and destructor functions for the priority
3907 indicated by N. */
3909 static int
3910 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3912 location_t *locus = (location_t *) data;
3913 int priority = (int) n->key;
3914 priority_info pi = (priority_info) n->value;
3916 /* Generate the functions themselves, but only if they are really
3917 needed. */
3918 if (pi->initializations_p)
3919 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3920 if (pi->destructions_p)
3921 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3923 /* Keep iterating. */
3924 return 0;
3927 /* Java requires that we be able to reference a local address for a
3928 method, and not be confused by PLT entries. If hidden aliases are
3929 supported, collect and return all the functions for which we should
3930 emit a hidden alias. */
3932 static struct pointer_set_t *
3933 collect_candidates_for_java_method_aliases (void)
3935 struct cgraph_node *node;
3936 struct pointer_set_t *candidates = NULL;
3938 #ifndef HAVE_GAS_HIDDEN
3939 return candidates;
3940 #endif
3942 FOR_EACH_FUNCTION (node)
3944 tree fndecl = node->decl;
3946 if (DECL_CLASS_SCOPE_P (fndecl)
3947 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3948 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3950 if (candidates == NULL)
3951 candidates = pointer_set_create ();
3952 pointer_set_insert (candidates, fndecl);
3956 return candidates;
3960 /* Java requires that we be able to reference a local address for a
3961 method, and not be confused by PLT entries. If hidden aliases are
3962 supported, emit one for each java function that we've emitted.
3963 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3964 by collect_candidates_for_java_method_aliases. */
3966 static void
3967 build_java_method_aliases (struct pointer_set_t *candidates)
3969 struct cgraph_node *node;
3971 #ifndef HAVE_GAS_HIDDEN
3972 return;
3973 #endif
3975 FOR_EACH_FUNCTION (node)
3977 tree fndecl = node->decl;
3979 if (TREE_ASM_WRITTEN (fndecl)
3980 && pointer_set_contains (candidates, fndecl))
3982 /* Mangle the name in a predictable way; we need to reference
3983 this from a java compiled object file. */
3984 tree oid, nid, alias;
3985 const char *oname;
3986 char *nname;
3988 oid = DECL_ASSEMBLER_NAME (fndecl);
3989 oname = IDENTIFIER_POINTER (oid);
3990 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3991 nname = ACONCAT (("_ZGA", oname+2, NULL));
3992 nid = get_identifier (nname);
3994 alias = make_alias_for (fndecl, nid);
3995 TREE_PUBLIC (alias) = 1;
3996 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3998 assemble_alias (alias, oid);
4003 /* Return C++ property of T, based on given operation OP. */
4005 static int
4006 cpp_check (tree t, cpp_operation op)
4008 switch (op)
4010 case IS_ABSTRACT:
4011 return DECL_PURE_VIRTUAL_P (t);
4012 case IS_CONSTRUCTOR:
4013 return DECL_CONSTRUCTOR_P (t);
4014 case IS_DESTRUCTOR:
4015 return DECL_DESTRUCTOR_P (t);
4016 case IS_COPY_CONSTRUCTOR:
4017 return DECL_COPY_CONSTRUCTOR_P (t);
4018 case IS_TEMPLATE:
4019 return TREE_CODE (t) == TEMPLATE_DECL;
4020 case IS_TRIVIAL:
4021 return trivial_type_p (t);
4022 default:
4023 return 0;
4027 /* Collect source file references recursively, starting from NAMESPC. */
4029 static void
4030 collect_source_refs (tree namespc)
4032 tree t;
4034 if (!namespc)
4035 return;
4037 /* Iterate over names in this name space. */
4038 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4039 if (!DECL_IS_BUILTIN (t) )
4040 collect_source_ref (DECL_SOURCE_FILE (t));
4042 /* Dump siblings, if any */
4043 collect_source_refs (TREE_CHAIN (namespc));
4045 /* Dump children, if any */
4046 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4049 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4050 starting from NAMESPC. */
4052 static void
4053 collect_ada_namespace (tree namespc, const char *source_file)
4055 if (!namespc)
4056 return;
4058 /* Collect decls from this namespace */
4059 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4061 /* Collect siblings, if any */
4062 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4064 /* Collect children, if any */
4065 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4068 /* Returns true iff there is a definition available for variable or
4069 function DECL. */
4071 static bool
4072 decl_defined_p (tree decl)
4074 if (TREE_CODE (decl) == FUNCTION_DECL)
4075 return (DECL_INITIAL (decl) != NULL_TREE);
4076 else
4078 gcc_assert (VAR_P (decl));
4079 return !DECL_EXTERNAL (decl);
4083 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4085 [expr.const]
4087 An integral constant-expression can only involve ... const
4088 variables of integral or enumeration types initialized with
4089 constant expressions ...
4091 C++0x also allows constexpr variables and temporaries initialized
4092 with constant expressions. We handle the former here, but the latter
4093 are just folded away in cxx_eval_constant_expression.
4095 The standard does not require that the expression be non-volatile.
4096 G++ implements the proposed correction in DR 457. */
4098 bool
4099 decl_constant_var_p (tree decl)
4101 if (!decl_maybe_constant_var_p (decl))
4102 return false;
4104 /* We don't know if a template static data member is initialized with
4105 a constant expression until we instantiate its initializer. Even
4106 in the case of a constexpr variable, we can't treat it as a
4107 constant until its initializer is complete in case it's used in
4108 its own initializer. */
4109 mark_used (decl);
4110 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4113 /* Returns true if DECL could be a symbolic constant variable, depending on
4114 its initializer. */
4116 bool
4117 decl_maybe_constant_var_p (tree decl)
4119 tree type = TREE_TYPE (decl);
4120 if (!VAR_P (decl))
4121 return false;
4122 if (DECL_DECLARED_CONSTEXPR_P (decl))
4123 return true;
4124 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4125 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4128 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4129 called from grokfndecl and grokvardecl; in all modes it is called from
4130 cp_write_global_declarations. */
4132 void
4133 no_linkage_error (tree decl)
4135 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4136 /* In C++11 it's ok if the decl is defined. */
4137 return;
4138 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4139 if (t == NULL_TREE)
4140 /* The type that got us on no_linkage_decls must have gotten a name for
4141 linkage purposes. */;
4142 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4143 /* The type might end up having a typedef name for linkage purposes. */
4144 vec_safe_push (no_linkage_decls, decl);
4145 else if (TYPE_ANONYMOUS_P (t))
4147 bool d = false;
4148 if (cxx_dialect >= cxx11)
4149 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4150 "anonymous type, is used but never defined", decl);
4151 else if (DECL_EXTERN_C_P (decl))
4152 /* Allow this; it's pretty common in C. */;
4153 else if (TREE_CODE (decl) == VAR_DECL)
4154 /* DRs 132, 319 and 389 seem to indicate types with
4155 no linkage can only be used to declare extern "C"
4156 entities. Since it's not always an error in the
4157 ISO C++ 90 Standard, we only issue a warning. */
4158 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4159 "with no linkage used to declare variable %q#D with "
4160 "linkage", decl);
4161 else
4162 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4163 "linkage used to declare function %q#D with linkage",
4164 decl);
4165 if (d && is_typedef_decl (TYPE_NAME (t)))
4166 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4167 "to the unqualified type, so it is not used for linkage",
4168 TYPE_NAME (t));
4170 else if (cxx_dialect >= cxx11)
4171 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4172 "%qT, is used but never defined", decl, t);
4173 else if (TREE_CODE (decl) == VAR_DECL)
4174 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4175 "used to declare variable %q#D with linkage", t, decl);
4176 else
4177 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4178 "to declare function %q#D with linkage", t, decl);
4181 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4183 static void
4184 collect_all_refs (const char *source_file)
4186 collect_ada_namespace (global_namespace, source_file);
4189 /* Clear DECL_EXTERNAL for NODE. */
4191 static bool
4192 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4194 DECL_EXTERNAL (node->decl) = 0;
4195 return false;
4198 /* Build up the function to run dynamic initializers for thread_local
4199 variables in this translation unit and alias the init functions for the
4200 individual variables to it. */
4202 static void
4203 handle_tls_init (void)
4205 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4206 if (vars == NULL_TREE)
4207 return;
4209 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4211 write_out_vars (vars);
4213 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4214 boolean_type_node);
4215 TREE_PUBLIC (guard) = false;
4216 TREE_STATIC (guard) = true;
4217 DECL_ARTIFICIAL (guard) = true;
4218 DECL_IGNORED_P (guard) = true;
4219 TREE_USED (guard) = true;
4220 set_decl_tls_model (guard, decl_default_tls_model (guard));
4221 pushdecl_top_level_and_finish (guard, NULL_TREE);
4223 tree fn = get_local_tls_init_fn ();
4224 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4225 tree body = begin_function_body ();
4226 tree if_stmt = begin_if_stmt ();
4227 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4228 tf_warning_or_error);
4229 finish_if_stmt_cond (cond, if_stmt);
4230 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4231 tf_warning_or_error));
4232 for (; vars; vars = TREE_CHAIN (vars))
4234 tree var = TREE_VALUE (vars);
4235 tree init = TREE_PURPOSE (vars);
4236 one_static_initialization_or_destruction (var, init, true);
4238 #ifdef ASM_OUTPUT_DEF
4239 /* Output init aliases even with -fno-extern-tls-init. */
4240 if (TREE_PUBLIC (var))
4242 tree single_init_fn = get_tls_init_fn (var);
4243 if (single_init_fn == NULL_TREE)
4244 continue;
4245 cgraph_node *alias
4246 = cgraph_same_body_alias (cgraph_get_create_node (fn),
4247 single_init_fn, fn);
4248 gcc_assert (alias != NULL);
4250 #endif
4253 finish_then_clause (if_stmt);
4254 finish_if_stmt (if_stmt);
4255 finish_function_body (body);
4256 expand_or_defer_fn (finish_function (0));
4259 /* The entire file is now complete. If requested, dump everything
4260 to a file. */
4262 static void
4263 dump_tu (void)
4265 int flags;
4266 FILE *stream = dump_begin (TDI_tu, &flags);
4268 if (stream)
4270 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4271 dump_end (TDI_tu, stream);
4275 /* This routine is called at the end of compilation.
4276 Its job is to create all the code needed to initialize and
4277 destroy the global aggregates. We do the destruction
4278 first, since that way we only need to reverse the decls once. */
4280 void
4281 cp_write_global_declarations (void)
4283 tree vars;
4284 bool reconsider;
4285 size_t i;
4286 location_t locus;
4287 unsigned ssdf_count = 0;
4288 int retries = 0;
4289 tree decl;
4290 struct pointer_set_t *candidates;
4292 locus = input_location;
4293 at_eof = 1;
4295 /* Bad parse errors. Just forget about it. */
4296 if (! global_bindings_p () || current_class_type
4297 || !vec_safe_is_empty (decl_namespace_list))
4298 return;
4300 /* This is the point to write out a PCH if we're doing that.
4301 In that case we do not want to do anything else. */
4302 if (pch_file)
4304 c_common_write_pch ();
4305 dump_tu ();
4306 return;
4309 cgraph_process_same_body_aliases ();
4311 /* Handle -fdump-ada-spec[-slim] */
4312 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4314 if (flag_dump_ada_spec_slim)
4315 collect_source_ref (main_input_filename);
4316 else
4317 collect_source_refs (global_namespace);
4319 dump_ada_specs (collect_all_refs, cpp_check);
4322 /* FIXME - huh? was input_line -= 1;*/
4324 timevar_start (TV_PHASE_DEFERRED);
4326 /* We now have to write out all the stuff we put off writing out.
4327 These include:
4329 o Template specializations that we have not yet instantiated,
4330 but which are needed.
4331 o Initialization and destruction for non-local objects with
4332 static storage duration. (Local objects with static storage
4333 duration are initialized when their scope is first entered,
4334 and are cleaned up via atexit.)
4335 o Virtual function tables.
4337 All of these may cause others to be needed. For example,
4338 instantiating one function may cause another to be needed, and
4339 generating the initializer for an object may cause templates to be
4340 instantiated, etc., etc. */
4342 emit_support_tinfos ();
4346 tree t;
4347 tree decl;
4349 reconsider = false;
4351 /* If there are templates that we've put off instantiating, do
4352 them now. */
4353 instantiate_pending_templates (retries);
4354 ggc_collect ();
4356 /* Write out virtual tables as required. Note that writing out
4357 the virtual table for a template class may cause the
4358 instantiation of members of that class. If we write out
4359 vtables then we remove the class from our list so we don't
4360 have to look at it again. */
4362 while (keyed_classes != NULL_TREE
4363 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4365 reconsider = true;
4366 keyed_classes = TREE_CHAIN (keyed_classes);
4369 t = keyed_classes;
4370 if (t != NULL_TREE)
4372 tree next = TREE_CHAIN (t);
4374 while (next)
4376 if (maybe_emit_vtables (TREE_VALUE (next)))
4378 reconsider = true;
4379 TREE_CHAIN (t) = TREE_CHAIN (next);
4381 else
4382 t = next;
4384 next = TREE_CHAIN (t);
4388 /* Write out needed type info variables. We have to be careful
4389 looping through unemitted decls, because emit_tinfo_decl may
4390 cause other variables to be needed. New elements will be
4391 appended, and we remove from the vector those that actually
4392 get emitted. */
4393 for (i = unemitted_tinfo_decls->length ();
4394 unemitted_tinfo_decls->iterate (--i, &t);)
4395 if (emit_tinfo_decl (t))
4397 reconsider = true;
4398 unemitted_tinfo_decls->unordered_remove (i);
4401 /* The list of objects with static storage duration is built up
4402 in reverse order. We clear STATIC_AGGREGATES so that any new
4403 aggregates added during the initialization of these will be
4404 initialized in the correct order when we next come around the
4405 loop. */
4406 vars = prune_vars_needing_no_initialization (&static_aggregates);
4408 if (vars)
4410 /* We need to start a new initialization function each time
4411 through the loop. That's because we need to know which
4412 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4413 isn't computed until a function is finished, and written
4414 out. That's a deficiency in the back end. When this is
4415 fixed, these initialization functions could all become
4416 inline, with resulting performance improvements. */
4417 tree ssdf_body;
4419 /* Set the line and file, so that it is obviously not from
4420 the source file. */
4421 input_location = locus;
4422 ssdf_body = start_static_storage_duration_function (ssdf_count);
4424 /* Make sure the back end knows about all the variables. */
4425 write_out_vars (vars);
4427 /* First generate code to do all the initializations. */
4428 if (vars)
4429 do_static_initialization_or_destruction (vars, /*initp=*/true);
4431 /* Then, generate code to do all the destructions. Do these
4432 in reverse order so that the most recently constructed
4433 variable is the first destroyed. If we're using
4434 __cxa_atexit, then we don't need to do this; functions
4435 were registered at initialization time to destroy the
4436 local statics. */
4437 if (!flag_use_cxa_atexit && vars)
4439 vars = nreverse (vars);
4440 do_static_initialization_or_destruction (vars, /*initp=*/false);
4442 else
4443 vars = NULL_TREE;
4445 /* Finish up the static storage duration function for this
4446 round. */
4447 input_location = locus;
4448 finish_static_storage_duration_function (ssdf_body);
4450 /* All those initializations and finalizations might cause
4451 us to need more inline functions, more template
4452 instantiations, etc. */
4453 reconsider = true;
4454 ssdf_count++;
4455 /* ??? was: locus.line++; */
4458 /* Now do the same for thread_local variables. */
4459 handle_tls_init ();
4461 /* Go through the set of inline functions whose bodies have not
4462 been emitted yet. If out-of-line copies of these functions
4463 are required, emit them. */
4464 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4466 /* Does it need synthesizing? */
4467 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4468 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4470 /* Even though we're already at the top-level, we push
4471 there again. That way, when we pop back a few lines
4472 hence, all of our state is restored. Otherwise,
4473 finish_function doesn't clean things up, and we end
4474 up with CURRENT_FUNCTION_DECL set. */
4475 push_to_top_level ();
4476 /* The decl's location will mark where it was first
4477 needed. Save that so synthesize method can indicate
4478 where it was needed from, in case of error */
4479 input_location = DECL_SOURCE_LOCATION (decl);
4480 synthesize_method (decl);
4481 pop_from_top_level ();
4482 reconsider = true;
4485 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4486 generate_tls_wrapper (decl);
4488 if (!DECL_SAVED_TREE (decl))
4489 continue;
4491 /* We lie to the back end, pretending that some functions
4492 are not defined when they really are. This keeps these
4493 functions from being put out unnecessarily. But, we must
4494 stop lying when the functions are referenced, or if they
4495 are not comdat since they need to be put out now. If
4496 DECL_INTERFACE_KNOWN, then we have already set
4497 DECL_EXTERNAL appropriately, so there's no need to check
4498 again, and we do not want to clear DECL_EXTERNAL if a
4499 previous call to import_export_decl set it.
4501 This is done in a separate for cycle, because if some
4502 deferred function is contained in another deferred
4503 function later in deferred_fns varray,
4504 rest_of_compilation would skip this function and we
4505 really cannot expand the same function twice. */
4506 import_export_decl (decl);
4507 if (DECL_NOT_REALLY_EXTERN (decl)
4508 && DECL_INITIAL (decl)
4509 && decl_needed_p (decl))
4511 struct cgraph_node *node, *next;
4513 node = cgraph_get_node (decl);
4514 if (node->cpp_implicit_alias)
4515 node = cgraph_alias_target (node);
4517 cgraph_for_node_and_aliases (node, clear_decl_external,
4518 NULL, true);
4519 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4520 group, we need to mark all symbols in the same comdat group
4521 that way. */
4522 if (node->same_comdat_group)
4523 for (next = cgraph (node->same_comdat_group);
4524 next != node;
4525 next = cgraph (next->same_comdat_group))
4526 cgraph_for_node_and_aliases (next, clear_decl_external,
4527 NULL, true);
4530 /* If we're going to need to write this function out, and
4531 there's already a body for it, create RTL for it now.
4532 (There might be no body if this is a method we haven't
4533 gotten around to synthesizing yet.) */
4534 if (!DECL_EXTERNAL (decl)
4535 && decl_needed_p (decl)
4536 && !TREE_ASM_WRITTEN (decl)
4537 && !cgraph_get_node (decl)->definition)
4539 /* We will output the function; no longer consider it in this
4540 loop. */
4541 DECL_DEFER_OUTPUT (decl) = 0;
4542 /* Generate RTL for this function now that we know we
4543 need it. */
4544 expand_or_defer_fn (decl);
4545 /* If we're compiling -fsyntax-only pretend that this
4546 function has been written out so that we don't try to
4547 expand it again. */
4548 if (flag_syntax_only)
4549 TREE_ASM_WRITTEN (decl) = 1;
4550 reconsider = true;
4554 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4555 reconsider = true;
4557 /* Static data members are just like namespace-scope globals. */
4558 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4560 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4561 /* Don't write it out if we haven't seen a definition. */
4562 || DECL_IN_AGGR_P (decl))
4563 continue;
4564 import_export_decl (decl);
4565 /* If this static data member is needed, provide it to the
4566 back end. */
4567 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4568 DECL_EXTERNAL (decl) = 0;
4570 if (vec_safe_length (pending_statics) != 0
4571 && wrapup_global_declarations (pending_statics->address (),
4572 pending_statics->length ()))
4573 reconsider = true;
4575 retries++;
4577 while (reconsider);
4579 /* All used inline functions must have a definition at this point. */
4580 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4582 if (/* Check online inline functions that were actually used. */
4583 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4584 /* If the definition actually was available here, then the
4585 fact that the function was not defined merely represents
4586 that for some reason (use of a template repository,
4587 #pragma interface, etc.) we decided not to emit the
4588 definition here. */
4589 && !DECL_INITIAL (decl)
4590 /* Don't complain if the template was defined. */
4591 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4592 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4593 (template_for_substitution (decl)))))
4595 warning (0, "inline function %q+D used but never defined", decl);
4596 /* Avoid a duplicate warning from check_global_declaration_1. */
4597 TREE_NO_WARNING (decl) = 1;
4601 /* So must decls that use a type with no linkage. */
4602 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4603 no_linkage_error (decl);
4605 /* Then, do the Objective-C stuff. This is where all the
4606 Objective-C module stuff gets generated (symtab,
4607 class/protocol/selector lists etc). This must be done after C++
4608 templates, destructors etc. so that selectors used in C++
4609 templates are properly allocated. */
4610 if (c_dialect_objc ())
4611 objc_write_global_declarations ();
4613 /* We give C linkage to static constructors and destructors. */
4614 push_lang_context (lang_name_c);
4616 /* Generate initialization and destruction functions for all
4617 priorities for which they are required. */
4618 if (priority_info_map)
4619 splay_tree_foreach (priority_info_map,
4620 generate_ctor_and_dtor_functions_for_priority,
4621 /*data=*/&locus);
4622 else if (c_dialect_objc () && objc_static_init_needed_p ())
4623 /* If this is obj-c++ and we need a static init, call
4624 generate_ctor_or_dtor_function. */
4625 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4626 DEFAULT_INIT_PRIORITY, &locus);
4628 /* We're done with the splay-tree now. */
4629 if (priority_info_map)
4630 splay_tree_delete (priority_info_map);
4632 /* Generate any missing aliases. */
4633 maybe_apply_pending_pragma_weaks ();
4635 /* We're done with static constructors, so we can go back to "C++"
4636 linkage now. */
4637 pop_lang_context ();
4639 /* Collect candidates for Java hidden aliases. */
4640 candidates = collect_candidates_for_java_method_aliases ();
4642 timevar_stop (TV_PHASE_DEFERRED);
4643 timevar_start (TV_PHASE_OPT_GEN);
4645 if (flag_vtable_verify)
4647 vtv_recover_class_info ();
4648 vtv_compute_class_hierarchy_transitive_closure ();
4649 vtv_build_vtable_verify_fndecl ();
4652 finalize_compilation_unit ();
4654 if (flag_vtable_verify)
4656 /* Generate the special constructor initialization function that
4657 calls __VLTRegisterPairs, and give it a very high
4658 initialization priority. This must be done after
4659 finalize_compilation_unit so that we have accurate
4660 information about which vtable will actually be emitted. */
4661 vtv_generate_init_routine ();
4664 timevar_stop (TV_PHASE_OPT_GEN);
4665 timevar_start (TV_PHASE_CHECK_DBGINFO);
4667 /* Now, issue warnings about static, but not defined, functions,
4668 etc., and emit debugging information. */
4669 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4670 if (vec_safe_length (pending_statics) != 0)
4672 check_global_declarations (pending_statics->address (),
4673 pending_statics->length ());
4674 emit_debug_global_declarations (pending_statics->address (),
4675 pending_statics->length ());
4678 perform_deferred_noexcept_checks ();
4680 /* Generate hidden aliases for Java. */
4681 if (candidates)
4683 build_java_method_aliases (candidates);
4684 pointer_set_destroy (candidates);
4687 finish_repo ();
4689 /* The entire file is now complete. If requested, dump everything
4690 to a file. */
4691 dump_tu ();
4693 if (flag_detailed_statistics)
4695 dump_tree_statistics ();
4696 dump_time_statistics ();
4698 input_location = locus;
4700 #ifdef ENABLE_CHECKING
4701 validate_conversion_obstack ();
4702 #endif /* ENABLE_CHECKING */
4704 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4707 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4708 function to call in parse-tree form; it has not yet been
4709 semantically analyzed. ARGS are the arguments to the function.
4710 They have already been semantically analyzed. This may change
4711 ARGS. */
4713 tree
4714 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4715 tsubst_flags_t complain)
4717 tree orig_fn;
4718 vec<tree, va_gc> *orig_args = NULL;
4719 tree expr;
4720 tree object;
4722 orig_fn = fn;
4723 object = TREE_OPERAND (fn, 0);
4725 if (processing_template_decl)
4727 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4728 || TREE_CODE (fn) == MEMBER_REF);
4729 if (type_dependent_expression_p (fn)
4730 || any_type_dependent_arguments_p (*args))
4731 return build_nt_call_vec (fn, *args);
4733 orig_args = make_tree_vector_copy (*args);
4735 /* Transform the arguments and add the implicit "this"
4736 parameter. That must be done before the FN is transformed
4737 because we depend on the form of FN. */
4738 make_args_non_dependent (*args);
4739 object = build_non_dependent_expr (object);
4740 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4742 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4743 object = cp_build_addr_expr (object, complain);
4744 vec_safe_insert (*args, 0, object);
4746 /* Now that the arguments are done, transform FN. */
4747 fn = build_non_dependent_expr (fn);
4750 /* A qualified name corresponding to a bound pointer-to-member is
4751 represented as an OFFSET_REF:
4753 struct B { void g(); };
4754 void (B::*p)();
4755 void B::g() { (this->*p)(); } */
4756 if (TREE_CODE (fn) == OFFSET_REF)
4758 tree object_addr = cp_build_addr_expr (object, complain);
4759 fn = TREE_OPERAND (fn, 1);
4760 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4761 complain);
4762 vec_safe_insert (*args, 0, object_addr);
4765 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4766 expr = build_op_call (fn, args, complain);
4767 else
4768 expr = cp_build_function_call_vec (fn, args, complain);
4769 if (processing_template_decl && expr != error_mark_node)
4770 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4772 if (orig_args != NULL)
4773 release_tree_vector (orig_args);
4775 return expr;
4779 void
4780 check_default_args (tree x)
4782 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4783 bool saw_def = false;
4784 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4785 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4787 if (TREE_PURPOSE (arg))
4788 saw_def = true;
4789 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4791 error ("default argument missing for parameter %P of %q+#D", i, x);
4792 TREE_PURPOSE (arg) = error_mark_node;
4797 /* Return true if function DECL can be inlined. This is used to force
4798 instantiation of methods that might be interesting for inlining. */
4799 bool
4800 possibly_inlined_p (tree decl)
4802 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4803 if (DECL_UNINLINABLE (decl))
4804 return false;
4805 if (!optimize || pragma_java_exceptions)
4806 return DECL_DECLARED_INLINE_P (decl);
4807 /* When optimizing, we might inline everything when flatten
4808 attribute or heuristics inlining for size or autoinlining
4809 is used. */
4810 return true;
4813 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4814 If DECL is a specialization or implicitly declared class member,
4815 generate the actual definition. Return false if something goes
4816 wrong, true otherwise. */
4818 bool
4819 mark_used (tree decl, tsubst_flags_t complain)
4821 /* If DECL is a BASELINK for a single function, then treat it just
4822 like the DECL for the function. Otherwise, if the BASELINK is
4823 for an overloaded function, we don't know which function was
4824 actually used until after overload resolution. */
4825 if (BASELINK_P (decl))
4827 decl = BASELINK_FUNCTIONS (decl);
4828 if (really_overloaded_fn (decl))
4829 return true;
4830 decl = OVL_CURRENT (decl);
4833 /* Set TREE_USED for the benefit of -Wunused. */
4834 TREE_USED (decl) = 1;
4835 if (DECL_CLONED_FUNCTION_P (decl))
4836 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4838 /* Mark enumeration types as used. */
4839 if (TREE_CODE (decl) == CONST_DECL)
4840 used_types_insert (DECL_CONTEXT (decl));
4842 if (TREE_CODE (decl) == FUNCTION_DECL)
4843 maybe_instantiate_noexcept (decl);
4845 if (TREE_CODE (decl) == FUNCTION_DECL
4846 && DECL_DELETED_FN (decl))
4848 if (DECL_ARTIFICIAL (decl))
4850 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4851 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4853 /* We mark a lambda conversion op as deleted if we can't
4854 generate it properly; see maybe_add_lambda_conv_op. */
4855 sorry ("converting lambda which uses %<...%> to "
4856 "function pointer");
4857 return false;
4860 if (complain & tf_error)
4862 error ("use of deleted function %qD", decl);
4863 if (!maybe_explain_implicit_delete (decl))
4864 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4866 return false;
4869 /* We can only check DECL_ODR_USED on variables or functions with
4870 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4871 might need special handling for. */
4872 if (!VAR_OR_FUNCTION_DECL_P (decl)
4873 || DECL_LANG_SPECIFIC (decl) == NULL
4874 || DECL_THUNK_P (decl))
4876 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4878 if (complain & tf_error)
4879 error ("use of %qD before deduction of %<auto%>", decl);
4880 return false;
4882 return true;
4885 /* We only want to do this processing once. We don't need to keep trying
4886 to instantiate inline templates, because unit-at-a-time will make sure
4887 we get them compiled before functions that want to inline them. */
4888 if (DECL_ODR_USED (decl))
4889 return true;
4891 /* If within finish_function, defer the rest until that function
4892 finishes, otherwise it might recurse. */
4893 if (defer_mark_used_calls)
4895 vec_safe_push (deferred_mark_used_calls, decl);
4896 return true;
4899 /* Normally, we can wait until instantiation-time to synthesize DECL.
4900 However, if DECL is a static data member initialized with a constant
4901 or a constexpr function, we need it right now because a reference to
4902 such a data member or a call to such function is not value-dependent.
4903 For a function that uses auto in the return type, we need to instantiate
4904 it to find out its type. For OpenMP user defined reductions, we need
4905 them instantiated for reduction clauses which inline them by hand
4906 directly. */
4907 if (DECL_LANG_SPECIFIC (decl)
4908 && DECL_TEMPLATE_INFO (decl)
4909 && (decl_maybe_constant_var_p (decl)
4910 || (TREE_CODE (decl) == FUNCTION_DECL
4911 && (DECL_DECLARED_CONSTEXPR_P (decl)
4912 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4913 || undeduced_auto_decl (decl))
4914 && !uses_template_parms (DECL_TI_ARGS (decl)))
4916 /* Instantiating a function will result in garbage collection. We
4917 must treat this situation as if we were within the body of a
4918 function so as to avoid collecting live data only referenced from
4919 the stack (such as overload resolution candidates). */
4920 ++function_depth;
4921 instantiate_decl (decl, /*defer_ok=*/false,
4922 /*expl_inst_class_mem_p=*/false);
4923 --function_depth;
4926 if (processing_template_decl)
4927 return true;
4929 /* Check this too in case we're within fold_non_dependent_expr. */
4930 if (DECL_TEMPLATE_INFO (decl)
4931 && uses_template_parms (DECL_TI_ARGS (decl)))
4932 return true;
4934 require_deduced_type (decl);
4936 /* If we don't need a value, then we don't need to synthesize DECL. */
4937 if (cp_unevaluated_operand != 0)
4938 return true;
4940 DECL_ODR_USED (decl) = 1;
4941 if (DECL_CLONED_FUNCTION_P (decl))
4942 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4944 /* DR 757: A type without linkage shall not be used as the type of a
4945 variable or function with linkage, unless
4946 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4947 o the variable or function is not used (3.2 [basic.def.odr]) or is
4948 defined in the same translation unit. */
4949 if (cxx_dialect > cxx98
4950 && decl_linkage (decl) != lk_none
4951 && !DECL_EXTERN_C_P (decl)
4952 && !DECL_ARTIFICIAL (decl)
4953 && !decl_defined_p (decl)
4954 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4956 if (is_local_extern (decl))
4957 /* There's no way to define a local extern, and adding it to
4958 the vector interferes with GC, so give an error now. */
4959 no_linkage_error (decl);
4960 else
4961 vec_safe_push (no_linkage_decls, decl);
4964 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4965 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4966 /* Remember it, so we can check it was defined. */
4967 note_vague_linkage_fn (decl);
4969 /* Is it a synthesized method that needs to be synthesized? */
4970 if (TREE_CODE (decl) == FUNCTION_DECL
4971 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4972 && DECL_DEFAULTED_FN (decl)
4973 /* A function defaulted outside the class is synthesized either by
4974 cp_finish_decl or instantiate_decl. */
4975 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
4976 && ! DECL_INITIAL (decl))
4978 /* Defer virtual destructors so that thunks get the right
4979 linkage. */
4980 if (DECL_VIRTUAL_P (decl) && !at_eof)
4982 note_vague_linkage_fn (decl);
4983 return true;
4986 /* Remember the current location for a function we will end up
4987 synthesizing. Then we can inform the user where it was
4988 required in the case of error. */
4989 DECL_SOURCE_LOCATION (decl) = input_location;
4991 /* Synthesizing an implicitly defined member function will result in
4992 garbage collection. We must treat this situation as if we were
4993 within the body of a function so as to avoid collecting live data
4994 on the stack (such as overload resolution candidates).
4996 We could just let cp_write_global_declarations handle synthesizing
4997 this function by adding it to deferred_fns, but doing
4998 it at the use site produces better error messages. */
4999 ++function_depth;
5000 synthesize_method (decl);
5001 --function_depth;
5002 /* If this is a synthesized method we don't need to
5003 do the instantiation test below. */
5005 else if (VAR_OR_FUNCTION_DECL_P (decl)
5006 && DECL_TEMPLATE_INFO (decl)
5007 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5008 || always_instantiate_p (decl)))
5009 /* If this is a function or variable that is an instance of some
5010 template, we now know that we will need to actually do the
5011 instantiation. We check that DECL is not an explicit
5012 instantiation because that is not checked in instantiate_decl.
5014 We put off instantiating functions in order to improve compile
5015 times. Maintaining a stack of active functions is expensive,
5016 and the inliner knows to instantiate any functions it might
5017 need. Therefore, we always try to defer instantiation. */
5019 ++function_depth;
5020 instantiate_decl (decl, /*defer_ok=*/true,
5021 /*expl_inst_class_mem_p=*/false);
5022 --function_depth;
5025 return true;
5028 bool
5029 mark_used (tree decl)
5031 return mark_used (decl, tf_warning_or_error);
5034 tree
5035 vtv_start_verification_constructor_init_function (void)
5037 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5040 tree
5041 vtv_finish_verification_constructor_init_function (tree function_body)
5043 tree fn;
5045 finish_compound_stmt (function_body);
5046 fn = finish_function (0);
5047 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5048 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5050 return fn;
5053 #include "gt-cp-decl2.h"