2014-07-12 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cp / decl2.c
blob0926dbc60fcdb4854edafeabf83ad4d556117a77
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;
110 /* Nonzero if we've instantiated everything used directly, and now want to
111 mark all virtual functions as used so that they are available for
112 devirtualization. */
113 static int mark_all_virtuals;
116 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
117 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
118 that apply to the function). */
120 tree
121 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
122 cp_ref_qualifier rqual)
124 tree raises;
125 tree attrs;
126 int type_quals;
127 bool late_return_type_p;
129 if (fntype == error_mark_node || ctype == error_mark_node)
130 return error_mark_node;
132 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
133 || TREE_CODE (fntype) == METHOD_TYPE);
135 type_quals = quals & ~TYPE_QUAL_RESTRICT;
136 ctype = cp_build_qualified_type (ctype, type_quals);
137 raises = TYPE_RAISES_EXCEPTIONS (fntype);
138 attrs = TYPE_ATTRIBUTES (fntype);
139 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
140 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
141 (TREE_CODE (fntype) == METHOD_TYPE
142 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
143 : TYPE_ARG_TYPES (fntype)));
144 if (attrs)
145 fntype = cp_build_type_attribute_variant (fntype, attrs);
146 if (rqual)
147 fntype = build_ref_qualified_type (fntype, rqual);
148 if (raises)
149 fntype = build_exception_variant (fntype, raises);
150 if (late_return_type_p)
151 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
153 return fntype;
156 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
157 return type changed to NEW_RET. */
159 tree
160 change_return_type (tree new_ret, tree fntype)
162 tree newtype;
163 tree args = TYPE_ARG_TYPES (fntype);
164 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
165 tree attrs = TYPE_ATTRIBUTES (fntype);
166 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
168 if (new_ret == error_mark_node)
169 return fntype;
171 if (same_type_p (new_ret, TREE_TYPE (fntype)))
172 return fntype;
174 if (TREE_CODE (fntype) == FUNCTION_TYPE)
176 newtype = build_function_type (new_ret, args);
177 newtype = apply_memfn_quals (newtype,
178 type_memfn_quals (fntype),
179 type_memfn_rqual (fntype));
181 else
182 newtype = build_method_type_directly
183 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
184 if (raises)
185 newtype = build_exception_variant (newtype, raises);
186 if (attrs)
187 newtype = cp_build_type_attribute_variant (newtype, attrs);
188 if (late_return_type_p)
189 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
191 return newtype;
194 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
195 appropriately. */
197 tree
198 cp_build_parm_decl (tree name, tree type)
200 tree parm = build_decl (input_location,
201 PARM_DECL, name, type);
202 /* DECL_ARG_TYPE is only used by the back end and the back end never
203 sees templates. */
204 if (!processing_template_decl)
205 DECL_ARG_TYPE (parm) = type_passed_as (type);
207 return parm;
210 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
211 indicated NAME. */
213 tree
214 build_artificial_parm (tree name, tree type)
216 tree parm = cp_build_parm_decl (name, type);
217 DECL_ARTIFICIAL (parm) = 1;
218 /* All our artificial parms are implicitly `const'; they cannot be
219 assigned to. */
220 TREE_READONLY (parm) = 1;
221 return parm;
224 /* Constructors for types with virtual baseclasses need an "in-charge" flag
225 saying whether this constructor is responsible for initialization of
226 virtual baseclasses or not. All destructors also need this "in-charge"
227 flag, which additionally determines whether or not the destructor should
228 free the memory for the object.
230 This function adds the "in-charge" flag to member function FN if
231 appropriate. It is called from grokclassfn and tsubst.
232 FN must be either a constructor or destructor.
234 The in-charge flag follows the 'this' parameter, and is followed by the
235 VTT parm (if any), then the user-written parms. */
237 void
238 maybe_retrofit_in_chrg (tree fn)
240 tree basetype, arg_types, parms, parm, fntype;
242 /* If we've already add the in-charge parameter don't do it again. */
243 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
244 return;
246 /* When processing templates we can't know, in general, whether or
247 not we're going to have virtual baseclasses. */
248 if (processing_template_decl)
249 return;
251 /* We don't need an in-charge parameter for constructors that don't
252 have virtual bases. */
253 if (DECL_CONSTRUCTOR_P (fn)
254 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
255 return;
257 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
258 basetype = TREE_TYPE (TREE_VALUE (arg_types));
259 arg_types = TREE_CHAIN (arg_types);
261 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
263 /* If this is a subobject constructor or destructor, our caller will
264 pass us a pointer to our VTT. */
265 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
267 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
269 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
270 DECL_CHAIN (parm) = parms;
271 parms = parm;
273 /* ...and then to TYPE_ARG_TYPES. */
274 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
276 DECL_HAS_VTT_PARM_P (fn) = 1;
279 /* Then add the in-charge parm (before the VTT parm). */
280 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
281 DECL_CHAIN (parm) = parms;
282 parms = parm;
283 arg_types = hash_tree_chain (integer_type_node, arg_types);
285 /* Insert our new parameter(s) into the list. */
286 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
288 /* And rebuild the function type. */
289 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
290 arg_types);
291 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
292 fntype = build_exception_variant (fntype,
293 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
294 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
295 fntype = (cp_build_type_attribute_variant
296 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
297 TREE_TYPE (fn) = fntype;
299 /* Now we've got the in-charge parameter. */
300 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
303 /* Classes overload their constituent function names automatically.
304 When a function name is declared in a record structure,
305 its name is changed to it overloaded name. Since names for
306 constructors and destructors can conflict, we place a leading
307 '$' for destructors.
309 CNAME is the name of the class we are grokking for.
311 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
313 FLAGS contains bits saying what's special about today's
314 arguments. DTOR_FLAG == DESTRUCTOR.
316 If FUNCTION is a destructor, then we must add the `auto-delete' field
317 as a second parameter. There is some hair associated with the fact
318 that we must "declare" this variable in the manner consistent with the
319 way the rest of the arguments were declared.
321 QUALS are the qualifiers for the this pointer. */
323 void
324 grokclassfn (tree ctype, tree function, enum overload_flags flags)
326 tree fn_name = DECL_NAME (function);
328 /* Even within an `extern "C"' block, members get C++ linkage. See
329 [dcl.link] for details. */
330 SET_DECL_LANGUAGE (function, lang_cplusplus);
332 if (fn_name == NULL_TREE)
334 error ("name missing for member function");
335 fn_name = get_identifier ("<anonymous>");
336 DECL_NAME (function) = fn_name;
339 DECL_CONTEXT (function) = ctype;
341 if (flags == DTOR_FLAG)
342 DECL_DESTRUCTOR_P (function) = 1;
344 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
345 maybe_retrofit_in_chrg (function);
348 /* Create an ARRAY_REF, checking for the user doing things backwards
349 along the way. DECLTYPE_P is for N3276, as in the parser. */
351 tree
352 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
353 bool decltype_p)
355 tree type;
356 tree expr;
357 tree orig_array_expr = array_expr;
358 tree orig_index_exp = index_exp;
360 if (error_operand_p (array_expr) || error_operand_p (index_exp))
361 return error_mark_node;
363 if (processing_template_decl)
365 if (type_dependent_expression_p (array_expr)
366 || type_dependent_expression_p (index_exp))
367 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
368 NULL_TREE, NULL_TREE);
369 array_expr = build_non_dependent_expr (array_expr);
370 index_exp = build_non_dependent_expr (index_exp);
373 type = TREE_TYPE (array_expr);
374 gcc_assert (type);
375 type = non_reference (type);
377 /* If they have an `operator[]', use that. */
378 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
380 tsubst_flags_t complain = tf_warning_or_error;
381 if (decltype_p)
382 complain |= tf_decltype;
383 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
384 index_exp, NULL_TREE, /*overload=*/NULL, complain);
386 else
388 tree p1, p2, i1, i2;
390 /* Otherwise, create an ARRAY_REF for a pointer or array type.
391 It is a little-known fact that, if `a' is an array and `i' is
392 an int, you can write `i[a]', which means the same thing as
393 `a[i]'. */
394 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
395 p1 = array_expr;
396 else
397 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
399 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
400 p2 = index_exp;
401 else
402 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
404 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
405 false);
406 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
407 false);
409 if ((p1 && i2) && (i1 && p2))
410 error ("ambiguous conversion for array subscript");
412 if (p1 && i2)
413 array_expr = p1, index_exp = i2;
414 else if (i1 && p2)
415 array_expr = p2, index_exp = i1;
416 else
418 error ("invalid types %<%T[%T]%> for array subscript",
419 type, TREE_TYPE (index_exp));
420 return error_mark_node;
423 if (array_expr == error_mark_node || index_exp == error_mark_node)
424 error ("ambiguous conversion for array subscript");
426 expr = build_array_ref (input_location, array_expr, index_exp);
428 if (processing_template_decl && expr != error_mark_node)
429 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
430 NULL_TREE, NULL_TREE);
431 return expr;
434 /* Given the cast expression EXP, checking out its validity. Either return
435 an error_mark_node if there was an unavoidable error, return a cast to
436 void for trying to delete a pointer w/ the value 0, or return the
437 call to delete. If DOING_VEC is true, we handle things differently
438 for doing an array delete.
439 Implements ARM $5.3.4. This is called from the parser. */
441 tree
442 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
443 tsubst_flags_t complain)
445 tree t, type;
447 if (exp == error_mark_node)
448 return exp;
450 if (processing_template_decl)
452 t = build_min (DELETE_EXPR, void_type_node, exp, size);
453 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
454 DELETE_EXPR_USE_VEC (t) = doing_vec;
455 TREE_SIDE_EFFECTS (t) = 1;
456 return t;
459 /* An array can't have been allocated by new, so complain. */
460 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
461 warning (0, "deleting array %q#E", exp);
463 t = build_expr_type_conversion (WANT_POINTER, exp, true);
465 if (t == NULL_TREE || t == error_mark_node)
467 error ("type %q#T argument given to %<delete%>, expected pointer",
468 TREE_TYPE (exp));
469 return error_mark_node;
472 type = TREE_TYPE (t);
474 /* As of Valley Forge, you can delete a pointer to const. */
476 /* You can't delete functions. */
477 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
479 error ("cannot delete a function. Only pointer-to-objects are "
480 "valid arguments to %<delete%>");
481 return error_mark_node;
484 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
485 if (VOID_TYPE_P (TREE_TYPE (type)))
487 warning (0, "deleting %qT is undefined", type);
488 doing_vec = 0;
491 /* Deleting a pointer with the value zero is valid and has no effect. */
492 if (integer_zerop (t))
493 return build1 (NOP_EXPR, void_type_node, t);
495 if (doing_vec)
496 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
497 sfk_deleting_destructor,
498 use_global_delete, complain);
499 else
500 return build_delete (type, t, sfk_deleting_destructor,
501 LOOKUP_NORMAL, use_global_delete,
502 complain);
505 /* Report an error if the indicated template declaration is not the
506 sort of thing that should be a member template. */
508 void
509 check_member_template (tree tmpl)
511 tree decl;
513 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
514 decl = DECL_TEMPLATE_RESULT (tmpl);
516 if (TREE_CODE (decl) == FUNCTION_DECL
517 || DECL_ALIAS_TEMPLATE_P (tmpl)
518 || (TREE_CODE (decl) == TYPE_DECL
519 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
521 /* The parser rejects template declarations in local classes
522 (with the exception of generic lambdas). */
523 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
524 /* The parser rejects any use of virtual in a function template. */
525 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
526 && DECL_VIRTUAL_P (decl)));
528 /* The debug-information generating code doesn't know what to do
529 with member templates. */
530 DECL_IGNORED_P (tmpl) = 1;
532 else
533 error ("template declaration of %q#D", decl);
536 /* Return true iff TYPE is a valid Java parameter or return type. */
538 static bool
539 acceptable_java_type (tree type)
541 if (type == error_mark_node)
542 return false;
544 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
545 return true;
546 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
548 type = TREE_TYPE (type);
549 if (TREE_CODE (type) == RECORD_TYPE)
551 tree args; int i;
552 if (! TYPE_FOR_JAVA (type))
553 return false;
554 if (! CLASSTYPE_TEMPLATE_INFO (type))
555 return true;
556 args = CLASSTYPE_TI_ARGS (type);
557 i = TREE_VEC_LENGTH (args);
558 while (--i >= 0)
560 type = TREE_VEC_ELT (args, i);
561 if (TYPE_PTR_P (type))
562 type = TREE_TYPE (type);
563 if (! TYPE_FOR_JAVA (type))
564 return false;
566 return true;
569 return false;
572 /* For a METHOD in a Java class CTYPE, return true if
573 the parameter and return types are valid Java types.
574 Otherwise, print appropriate error messages, and return false. */
576 bool
577 check_java_method (tree method)
579 bool jerr = false;
580 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
581 tree ret_type = TREE_TYPE (TREE_TYPE (method));
583 if (!acceptable_java_type (ret_type))
585 error ("Java method %qD has non-Java return type %qT",
586 method, ret_type);
587 jerr = true;
590 arg_types = TREE_CHAIN (arg_types);
591 if (DECL_HAS_IN_CHARGE_PARM_P (method))
592 arg_types = TREE_CHAIN (arg_types);
593 if (DECL_HAS_VTT_PARM_P (method))
594 arg_types = TREE_CHAIN (arg_types);
596 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
598 tree type = TREE_VALUE (arg_types);
599 if (!acceptable_java_type (type))
601 if (type != error_mark_node)
602 error ("Java method %qD has non-Java parameter type %qT",
603 method, type);
604 jerr = true;
607 return !jerr;
610 /* Sanity check: report error if this function FUNCTION is not
611 really a member of the class (CTYPE) it is supposed to belong to.
612 TEMPLATE_PARMS is used to specify the template parameters of a member
613 template passed as FUNCTION_DECL. If the member template is passed as a
614 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
615 from the declaration. If the function is not a function template, it
616 must be NULL.
617 It returns the original declaration for the function, NULL_TREE if
618 no declaration was found, error_mark_node if an error was emitted. */
620 tree
621 check_classfn (tree ctype, tree function, tree template_parms)
623 int ix;
624 bool is_template;
625 tree pushed_scope;
627 if (DECL_USE_TEMPLATE (function)
628 && !(TREE_CODE (function) == TEMPLATE_DECL
629 && DECL_TEMPLATE_SPECIALIZATION (function))
630 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
631 /* Since this is a specialization of a member template,
632 we're not going to find the declaration in the class.
633 For example, in:
635 struct S { template <typename T> void f(T); };
636 template <> void S::f(int);
638 we're not going to find `S::f(int)', but there's no
639 reason we should, either. We let our callers know we didn't
640 find the method, but we don't complain. */
641 return NULL_TREE;
643 /* Basic sanity check: for a template function, the template parameters
644 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
645 if (TREE_CODE (function) == TEMPLATE_DECL)
647 if (template_parms
648 && !comp_template_parms (template_parms,
649 DECL_TEMPLATE_PARMS (function)))
651 error ("template parameter lists provided don%'t match the "
652 "template parameters of %qD", function);
653 return error_mark_node;
655 template_parms = DECL_TEMPLATE_PARMS (function);
658 /* OK, is this a definition of a member template? */
659 is_template = (template_parms != NULL_TREE);
661 /* [temp.mem]
663 A destructor shall not be a member template. */
664 if (DECL_DESTRUCTOR_P (function) && is_template)
666 error ("destructor %qD declared as member template", function);
667 return error_mark_node;
670 /* We must enter the scope here, because conversion operators are
671 named by target type, and type equivalence relies on typenames
672 resolving within the scope of CTYPE. */
673 pushed_scope = push_scope (ctype);
674 ix = class_method_index_for_fn (complete_type (ctype), function);
675 if (ix >= 0)
677 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
678 tree fndecls, fndecl = 0;
679 bool is_conv_op;
680 const char *format = NULL;
682 for (fndecls = (*methods)[ix];
683 fndecls; fndecls = OVL_NEXT (fndecls))
685 tree p1, p2;
687 fndecl = OVL_CURRENT (fndecls);
688 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
689 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
691 /* We cannot simply call decls_match because this doesn't
692 work for static member functions that are pretending to
693 be methods, and because the name may have been changed by
694 asm("new_name"). */
696 /* Get rid of the this parameter on functions that become
697 static. */
698 if (DECL_STATIC_FUNCTION_P (fndecl)
699 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
700 p1 = TREE_CHAIN (p1);
702 /* A member template definition only matches a member template
703 declaration. */
704 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
705 continue;
707 /* ref-qualifier or absence of same must match. */
708 if (type_memfn_rqual (TREE_TYPE (function))
709 != type_memfn_rqual (TREE_TYPE (fndecl)))
710 continue;
712 /* While finding a match, same types and params are not enough
713 if the function is versioned. Also check version ("target")
714 attributes. */
715 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
716 TREE_TYPE (TREE_TYPE (fndecl)))
717 && compparms (p1, p2)
718 && !targetm.target_option.function_versions (function, fndecl)
719 && (!is_template
720 || comp_template_parms (template_parms,
721 DECL_TEMPLATE_PARMS (fndecl)))
722 && (DECL_TEMPLATE_SPECIALIZATION (function)
723 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
724 && (!DECL_TEMPLATE_SPECIALIZATION (function)
725 || (DECL_TI_TEMPLATE (function)
726 == DECL_TI_TEMPLATE (fndecl))))
727 break;
729 if (fndecls)
731 if (pushed_scope)
732 pop_scope (pushed_scope);
733 return OVL_CURRENT (fndecls);
736 error_at (DECL_SOURCE_LOCATION (function),
737 "prototype for %q#D does not match any in class %qT",
738 function, ctype);
739 is_conv_op = DECL_CONV_FN_P (fndecl);
741 if (is_conv_op)
742 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
743 fndecls = (*methods)[ix];
744 while (fndecls)
746 fndecl = OVL_CURRENT (fndecls);
747 fndecls = OVL_NEXT (fndecls);
749 if (!fndecls && is_conv_op)
751 if (methods->length () > (size_t) ++ix)
753 fndecls = (*methods)[ix];
754 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
756 fndecls = NULL_TREE;
757 is_conv_op = false;
760 else
761 is_conv_op = false;
763 if (format)
764 format = " %+#D";
765 else if (fndecls)
766 format = N_("candidates are: %+#D");
767 else
768 format = N_("candidate is: %+#D");
769 error (format, fndecl);
772 else if (!COMPLETE_TYPE_P (ctype))
773 cxx_incomplete_type_error (function, ctype);
774 else
775 error ("no %q#D member function declared in class %qT",
776 function, ctype);
778 if (pushed_scope)
779 pop_scope (pushed_scope);
780 return error_mark_node;
783 /* DECL is a function with vague linkage. Remember it so that at the
784 end of the translation unit we can decide whether or not to emit
785 it. */
787 void
788 note_vague_linkage_fn (tree decl)
790 DECL_DEFER_OUTPUT (decl) = 1;
791 vec_safe_push (deferred_fns, decl);
794 /* We have just processed the DECL, which is a static data member.
795 The other parameters are as for cp_finish_decl. */
797 void
798 finish_static_data_member_decl (tree decl,
799 tree init, bool init_const_expr_p,
800 tree asmspec_tree,
801 int flags)
803 DECL_CONTEXT (decl) = current_class_type;
805 /* We cannot call pushdecl here, because that would fill in the
806 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
807 the right thing, namely, to put this decl out straight away. */
809 if (! processing_template_decl)
810 vec_safe_push (pending_statics, decl);
812 if (LOCAL_CLASS_P (current_class_type)
813 /* We already complained about the template definition. */
814 && !DECL_TEMPLATE_INSTANTIATION (decl))
815 permerror (input_location, "local class %q#T shall not have static data member %q#D",
816 current_class_type, decl);
817 else
818 for (tree t = current_class_type; TYPE_P (t);
819 t = CP_TYPE_CONTEXT (t))
820 if (TYPE_ANONYMOUS_P (t))
822 if (permerror (DECL_SOURCE_LOCATION (decl),
823 "static data member %qD in unnamed class", decl))
824 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
825 "unnamed class defined here");
826 break;
829 DECL_IN_AGGR_P (decl) = 1;
831 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
832 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
833 SET_VAR_HAD_UNKNOWN_BOUND (decl);
835 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
838 /* DECLARATOR and DECLSPECS correspond to a class member. The other
839 parameters are as for cp_finish_decl. Return the DECL for the
840 class member declared. */
842 tree
843 grokfield (const cp_declarator *declarator,
844 cp_decl_specifier_seq *declspecs,
845 tree init, bool init_const_expr_p,
846 tree asmspec_tree,
847 tree attrlist)
849 tree value;
850 const char *asmspec = 0;
851 int flags;
852 tree name;
854 if (init
855 && TREE_CODE (init) == TREE_LIST
856 && TREE_VALUE (init) == error_mark_node
857 && TREE_CHAIN (init) == NULL_TREE)
858 init = NULL_TREE;
860 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
861 if (! value || value == error_mark_node)
862 /* friend or constructor went bad. */
863 return error_mark_node;
864 if (TREE_TYPE (value) == error_mark_node)
865 return value;
867 if (TREE_CODE (value) == TYPE_DECL && init)
869 error ("typedef %qD is initialized (use decltype instead)", value);
870 init = NULL_TREE;
873 /* Pass friendly classes back. */
874 if (value == void_type_node)
875 return value;
877 /* Pass friend decls back. */
878 if ((TREE_CODE (value) == FUNCTION_DECL
879 || TREE_CODE (value) == TEMPLATE_DECL)
880 && DECL_CONTEXT (value) != current_class_type)
881 return value;
883 name = DECL_NAME (value);
885 if (name != NULL_TREE)
887 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
889 error ("explicit template argument list not allowed");
890 return error_mark_node;
893 if (IDENTIFIER_POINTER (name)[0] == '_'
894 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
895 error ("member %qD conflicts with virtual function table field name",
896 value);
899 /* Stash away type declarations. */
900 if (TREE_CODE (value) == TYPE_DECL)
902 DECL_NONLOCAL (value) = 1;
903 DECL_CONTEXT (value) = current_class_type;
905 if (attrlist)
907 int attrflags = 0;
909 /* If this is a typedef that names the class for linkage purposes
910 (7.1.3p8), apply any attributes directly to the type. */
911 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
912 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
913 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
915 cplus_decl_attributes (&value, attrlist, attrflags);
918 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
919 && TREE_TYPE (value) != error_mark_node
920 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
921 set_underlying_type (value);
923 /* It's important that push_template_decl below follows
924 set_underlying_type above so that the created template
925 carries the properly set type of VALUE. */
926 if (processing_template_decl)
927 value = push_template_decl (value);
929 record_locally_defined_typedef (value);
930 return value;
933 if (DECL_IN_AGGR_P (value))
935 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
936 return void_type_node;
939 if (asmspec_tree && asmspec_tree != error_mark_node)
940 asmspec = TREE_STRING_POINTER (asmspec_tree);
942 if (init)
944 if (TREE_CODE (value) == FUNCTION_DECL)
946 /* Initializers for functions are rejected early in the parser.
947 If we get here, it must be a pure specifier for a method. */
948 if (init == ridpointers[(int)RID_DELETE])
950 DECL_DELETED_FN (value) = 1;
951 DECL_DECLARED_INLINE_P (value) = 1;
952 DECL_INITIAL (value) = error_mark_node;
954 else if (init == ridpointers[(int)RID_DEFAULT])
956 if (defaultable_fn_check (value))
958 DECL_DEFAULTED_FN (value) = 1;
959 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
960 DECL_DECLARED_INLINE_P (value) = 1;
963 else if (TREE_CODE (init) == DEFAULT_ARG)
964 error ("invalid initializer for member function %qD", value);
965 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
967 if (integer_zerop (init))
968 DECL_PURE_VIRTUAL_P (value) = 1;
969 else if (error_operand_p (init))
970 ; /* An error has already been reported. */
971 else
972 error ("invalid initializer for member function %qD",
973 value);
975 else
977 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
978 error ("initializer specified for static member function %qD",
979 value);
982 else if (TREE_CODE (value) == FIELD_DECL)
983 /* C++11 NSDMI, keep going. */;
984 else if (!VAR_P (value))
985 gcc_unreachable ();
988 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
990 value = push_template_decl (value);
991 if (error_operand_p (value))
992 return error_mark_node;
995 if (attrlist)
996 cplus_decl_attributes (&value, attrlist, 0);
998 if (init && DIRECT_LIST_INIT_P (init))
999 flags = LOOKUP_NORMAL;
1000 else
1001 flags = LOOKUP_IMPLICIT;
1003 switch (TREE_CODE (value))
1005 case VAR_DECL:
1006 finish_static_data_member_decl (value, init, init_const_expr_p,
1007 asmspec_tree, flags);
1008 return value;
1010 case FIELD_DECL:
1011 if (asmspec)
1012 error ("%<asm%> specifiers are not permitted on non-static data members");
1013 if (DECL_INITIAL (value) == error_mark_node)
1014 init = error_mark_node;
1015 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1016 NULL_TREE, flags);
1017 DECL_IN_AGGR_P (value) = 1;
1018 return value;
1020 case FUNCTION_DECL:
1021 if (asmspec)
1022 set_user_assembler_name (value, asmspec);
1024 cp_finish_decl (value,
1025 /*init=*/NULL_TREE,
1026 /*init_const_expr_p=*/false,
1027 asmspec_tree, flags);
1029 /* Pass friends back this way. */
1030 if (DECL_FRIEND_P (value))
1031 return void_type_node;
1033 DECL_IN_AGGR_P (value) = 1;
1034 return value;
1036 default:
1037 gcc_unreachable ();
1039 return NULL_TREE;
1042 /* Like `grokfield', but for bitfields.
1043 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1045 tree
1046 grokbitfield (const cp_declarator *declarator,
1047 cp_decl_specifier_seq *declspecs, tree width,
1048 tree attrlist)
1050 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1052 if (value == error_mark_node)
1053 return NULL_TREE; /* friends went bad. */
1054 if (TREE_TYPE (value) == error_mark_node)
1055 return value;
1057 /* Pass friendly classes back. */
1058 if (VOID_TYPE_P (value))
1059 return void_type_node;
1061 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1062 && (POINTER_TYPE_P (value)
1063 || !dependent_type_p (TREE_TYPE (value))))
1065 error ("bit-field %qD with non-integral type", value);
1066 return error_mark_node;
1069 if (TREE_CODE (value) == TYPE_DECL)
1071 error ("cannot declare %qD to be a bit-field type", value);
1072 return NULL_TREE;
1075 /* Usually, finish_struct_1 catches bitfields with invalid types.
1076 But, in the case of bitfields with function type, we confuse
1077 ourselves into thinking they are member functions, so we must
1078 check here. */
1079 if (TREE_CODE (value) == FUNCTION_DECL)
1081 error ("cannot declare bit-field %qD with function type",
1082 DECL_NAME (value));
1083 return NULL_TREE;
1086 if (DECL_IN_AGGR_P (value))
1088 error ("%qD is already defined in the class %qT", value,
1089 DECL_CONTEXT (value));
1090 return void_type_node;
1093 if (TREE_STATIC (value))
1095 error ("static member %qD cannot be a bit-field", value);
1096 return NULL_TREE;
1098 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1100 if (width != error_mark_node)
1102 /* The width must be an integer type. */
1103 if (!type_dependent_expression_p (width)
1104 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1105 error ("width of bit-field %qD has non-integral type %qT", value,
1106 TREE_TYPE (width));
1107 DECL_INITIAL (value) = width;
1108 SET_DECL_C_BIT_FIELD (value);
1111 DECL_IN_AGGR_P (value) = 1;
1113 if (attrlist)
1114 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1116 return value;
1120 /* Returns true iff ATTR is an attribute which needs to be applied at
1121 instantiation time rather than template definition time. */
1123 static bool
1124 is_late_template_attribute (tree attr, tree decl)
1126 tree name = get_attribute_name (attr);
1127 tree args = TREE_VALUE (attr);
1128 const struct attribute_spec *spec = lookup_attribute_spec (name);
1129 tree arg;
1131 if (!spec)
1132 /* Unknown attribute. */
1133 return false;
1135 /* Attribute weak handling wants to write out assembly right away. */
1136 if (is_attribute_p ("weak", name))
1137 return true;
1139 /* Attribute unused is applied directly, as it appertains to
1140 decls. */
1141 if (is_attribute_p ("unused", name))
1142 return false;
1144 /* #pragma omp declare simd attribute needs to be always deferred. */
1145 if (flag_openmp
1146 && is_attribute_p ("omp declare simd", name))
1147 return true;
1149 /* If any of the arguments are dependent expressions, we can't evaluate
1150 the attribute until instantiation time. */
1151 for (arg = args; arg; arg = TREE_CHAIN (arg))
1153 tree t = TREE_VALUE (arg);
1155 /* If the first attribute argument is an identifier, only consider
1156 second and following arguments. Attributes like mode, format,
1157 cleanup and several target specific attributes aren't late
1158 just because they have an IDENTIFIER_NODE as first argument. */
1159 if (arg == args && identifier_p (t))
1160 continue;
1162 if (value_dependent_expression_p (t)
1163 || type_dependent_expression_p (t))
1164 return true;
1167 if (TREE_CODE (decl) == TYPE_DECL
1168 || TYPE_P (decl)
1169 || spec->type_required)
1171 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1173 /* We can't apply any attributes to a completely unknown type until
1174 instantiation time. */
1175 enum tree_code code = TREE_CODE (type);
1176 if (code == TEMPLATE_TYPE_PARM
1177 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1178 || code == TYPENAME_TYPE)
1179 return true;
1180 /* Also defer most attributes on dependent types. This is not
1181 necessary in all cases, but is the better default. */
1182 else if (dependent_type_p (type)
1183 /* But attributes abi_tag and visibility specifically apply
1184 to templates. */
1185 && !is_attribute_p ("abi_tag", name)
1186 && !is_attribute_p ("visibility", name))
1187 return true;
1188 else
1189 return false;
1191 else
1192 return false;
1195 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1196 applied at instantiation time and return them. If IS_DEPENDENT is true,
1197 the declaration itself is dependent, so all attributes should be applied
1198 at instantiation time. */
1200 static tree
1201 splice_template_attributes (tree *attr_p, tree decl)
1203 tree *p = attr_p;
1204 tree late_attrs = NULL_TREE;
1205 tree *q = &late_attrs;
1207 if (!p)
1208 return NULL_TREE;
1210 for (; *p; )
1212 if (is_late_template_attribute (*p, decl))
1214 ATTR_IS_DEPENDENT (*p) = 1;
1215 *q = *p;
1216 *p = TREE_CHAIN (*p);
1217 q = &TREE_CHAIN (*q);
1218 *q = NULL_TREE;
1220 else
1221 p = &TREE_CHAIN (*p);
1224 return late_attrs;
1227 /* Remove any late attributes from the list in ATTR_P and attach them to
1228 DECL_P. */
1230 static void
1231 save_template_attributes (tree *attr_p, tree *decl_p)
1233 tree *q;
1235 if (attr_p && *attr_p == error_mark_node)
1236 return;
1238 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1239 if (!late_attrs)
1240 return;
1242 if (DECL_P (*decl_p))
1243 q = &DECL_ATTRIBUTES (*decl_p);
1244 else
1245 q = &TYPE_ATTRIBUTES (*decl_p);
1247 tree old_attrs = *q;
1249 /* Merge the late attributes at the beginning with the attribute
1250 list. */
1251 late_attrs = merge_attributes (late_attrs, *q);
1252 *q = late_attrs;
1254 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1256 /* We've added new attributes directly to the main variant, so
1257 now we need to update all of the other variants to include
1258 these new attributes. */
1259 tree variant;
1260 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1261 variant = TYPE_NEXT_VARIANT (variant))
1263 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1264 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1269 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1270 to a typedef which gives a previously anonymous class or enum a name for
1271 linkage purposes. */
1273 bool
1274 attributes_naming_typedef_ok (tree attrs)
1276 for (; attrs; attrs = TREE_CHAIN (attrs))
1278 tree name = get_attribute_name (attrs);
1279 if (is_attribute_p ("vector_size", name))
1280 return false;
1282 return true;
1285 /* Like reconstruct_complex_type, but handle also template trees. */
1287 tree
1288 cp_reconstruct_complex_type (tree type, tree bottom)
1290 tree inner, outer;
1291 bool late_return_type_p = false;
1293 if (TYPE_PTR_P (type))
1295 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1296 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1297 TYPE_REF_CAN_ALIAS_ALL (type));
1299 else if (TREE_CODE (type) == REFERENCE_TYPE)
1301 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1302 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1303 TYPE_REF_CAN_ALIAS_ALL (type));
1305 else if (TREE_CODE (type) == ARRAY_TYPE)
1307 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1308 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1309 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1310 element type qualification will be handled by the recursive
1311 cp_reconstruct_complex_type call and cp_build_qualified_type
1312 for ARRAY_TYPEs changes the element type. */
1313 return outer;
1315 else if (TREE_CODE (type) == FUNCTION_TYPE)
1317 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1318 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1319 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1320 outer = apply_memfn_quals (outer,
1321 type_memfn_quals (type),
1322 type_memfn_rqual (type));
1324 else if (TREE_CODE (type) == METHOD_TYPE)
1326 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1327 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1328 /* The build_method_type_directly() routine prepends 'this' to argument list,
1329 so we must compensate by getting rid of it. */
1330 outer
1331 = build_method_type_directly
1332 (class_of_this_parm (type), inner,
1333 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1335 else if (TREE_CODE (type) == OFFSET_TYPE)
1337 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1338 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1340 else
1341 return bottom;
1343 if (TYPE_ATTRIBUTES (type))
1344 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1345 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1347 if (late_return_type_p)
1348 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1350 return outer;
1353 /* Replaces any constexpr expression that may be into the attributes
1354 arguments with their reduced value. */
1356 static void
1357 cp_check_const_attributes (tree attributes)
1359 if (attributes == error_mark_node)
1360 return;
1362 tree attr;
1363 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1365 tree arg;
1366 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1368 tree expr = TREE_VALUE (arg);
1369 if (EXPR_P (expr))
1370 TREE_VALUE (arg) = maybe_constant_value (expr);
1375 /* Return true if TYPE is an OpenMP mappable type. */
1376 bool
1377 cp_omp_mappable_type (tree type)
1379 /* Mappable type has to be complete. */
1380 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1381 return false;
1382 /* Arrays have mappable type if the elements have mappable type. */
1383 while (TREE_CODE (type) == ARRAY_TYPE)
1384 type = TREE_TYPE (type);
1385 /* A mappable type cannot contain virtual members. */
1386 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1387 return false;
1388 /* All data members must be non-static. */
1389 if (CLASS_TYPE_P (type))
1391 tree field;
1392 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1393 if (TREE_CODE (field) == VAR_DECL)
1394 return false;
1395 /* All fields must have mappable types. */
1396 else if (TREE_CODE (field) == FIELD_DECL
1397 && !cp_omp_mappable_type (TREE_TYPE (field)))
1398 return false;
1400 return true;
1403 /* Like decl_attributes, but handle C++ complexity. */
1405 void
1406 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1408 if (*decl == NULL_TREE || *decl == void_type_node
1409 || *decl == error_mark_node)
1410 return;
1412 /* Add implicit "omp declare target" attribute if requested. */
1413 if (scope_chain->omp_declare_target_attribute
1414 && ((TREE_CODE (*decl) == VAR_DECL && TREE_STATIC (*decl))
1415 || TREE_CODE (*decl) == FUNCTION_DECL))
1417 if (TREE_CODE (*decl) == VAR_DECL
1418 && DECL_CLASS_SCOPE_P (*decl))
1419 error ("%q+D static data member inside of declare target directive",
1420 *decl);
1421 else if (TREE_CODE (*decl) == VAR_DECL
1422 && (DECL_FUNCTION_SCOPE_P (*decl)
1423 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1424 error ("%q+D in block scope inside of declare target directive",
1425 *decl);
1426 else if (!processing_template_decl
1427 && TREE_CODE (*decl) == VAR_DECL
1428 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1429 error ("%q+D in declare target directive does not have mappable type",
1430 *decl);
1431 else
1432 attributes = tree_cons (get_identifier ("omp declare target"),
1433 NULL_TREE, attributes);
1436 if (processing_template_decl)
1438 if (check_for_bare_parameter_packs (attributes))
1439 return;
1441 save_template_attributes (&attributes, decl);
1444 cp_check_const_attributes (attributes);
1446 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1447 decl = &DECL_TEMPLATE_RESULT (*decl);
1449 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1451 attributes
1452 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1453 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (*decl)),
1454 attributes, flags);
1456 else
1457 decl_attributes (decl, attributes, flags);
1459 if (TREE_CODE (*decl) == TYPE_DECL)
1460 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1463 /* Walks through the namespace- or function-scope anonymous union
1464 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1465 Returns one of the fields for use in the mangled name. */
1467 static tree
1468 build_anon_union_vars (tree type, tree object)
1470 tree main_decl = NULL_TREE;
1471 tree field;
1473 /* Rather than write the code to handle the non-union case,
1474 just give an error. */
1475 if (TREE_CODE (type) != UNION_TYPE)
1477 error ("anonymous struct not inside named type");
1478 return error_mark_node;
1481 for (field = TYPE_FIELDS (type);
1482 field != NULL_TREE;
1483 field = DECL_CHAIN (field))
1485 tree decl;
1486 tree ref;
1488 if (DECL_ARTIFICIAL (field))
1489 continue;
1490 if (TREE_CODE (field) != FIELD_DECL)
1492 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1493 "have non-static data members", field);
1494 continue;
1497 if (TREE_PRIVATE (field))
1498 permerror (input_location, "private member %q+#D in anonymous union", field);
1499 else if (TREE_PROTECTED (field))
1500 permerror (input_location, "protected member %q+#D in anonymous union", field);
1502 if (processing_template_decl)
1503 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1504 DECL_NAME (field), NULL_TREE);
1505 else
1506 ref = build_class_member_access_expr (object, field, NULL_TREE,
1507 false, tf_warning_or_error);
1509 if (DECL_NAME (field))
1511 tree base;
1513 decl = build_decl (input_location,
1514 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1515 DECL_ANON_UNION_VAR_P (decl) = 1;
1516 DECL_ARTIFICIAL (decl) = 1;
1518 base = get_base_address (object);
1519 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1520 TREE_STATIC (decl) = TREE_STATIC (base);
1521 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1523 SET_DECL_VALUE_EXPR (decl, ref);
1524 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1526 decl = pushdecl (decl);
1528 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1529 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1530 else
1531 decl = 0;
1533 if (main_decl == NULL_TREE)
1534 main_decl = decl;
1537 return main_decl;
1540 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1541 anonymous union, then all members must be laid out together. PUBLIC_P
1542 is nonzero if this union is not declared static. */
1544 void
1545 finish_anon_union (tree anon_union_decl)
1547 tree type;
1548 tree main_decl;
1549 bool public_p;
1551 if (anon_union_decl == error_mark_node)
1552 return;
1554 type = TREE_TYPE (anon_union_decl);
1555 public_p = TREE_PUBLIC (anon_union_decl);
1557 /* The VAR_DECL's context is the same as the TYPE's context. */
1558 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1560 if (TYPE_FIELDS (type) == NULL_TREE)
1561 return;
1563 if (public_p)
1565 error ("namespace-scope anonymous aggregates must be static");
1566 return;
1569 main_decl = build_anon_union_vars (type, anon_union_decl);
1570 if (main_decl == error_mark_node)
1571 return;
1572 if (main_decl == NULL_TREE)
1574 warning (0, "anonymous union with no members");
1575 return;
1578 if (!processing_template_decl)
1580 /* Use main_decl to set the mangled name. */
1581 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1582 maybe_commonize_var (anon_union_decl);
1583 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1584 mangle_decl (anon_union_decl);
1585 DECL_NAME (anon_union_decl) = NULL_TREE;
1588 pushdecl (anon_union_decl);
1589 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1592 /* Auxiliary functions to make type signatures for
1593 `operator new' and `operator delete' correspond to
1594 what compiler will be expecting. */
1596 tree
1597 coerce_new_type (tree type)
1599 int e = 0;
1600 tree args = TYPE_ARG_TYPES (type);
1602 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1604 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1606 e = 1;
1607 error ("%<operator new%> must return type %qT", ptr_type_node);
1610 if (args && args != void_list_node)
1612 if (TREE_PURPOSE (args))
1614 /* [basic.stc.dynamic.allocation]
1616 The first parameter shall not have an associated default
1617 argument. */
1618 error ("the first parameter of %<operator new%> cannot "
1619 "have a default argument");
1620 /* Throw away the default argument. */
1621 TREE_PURPOSE (args) = NULL_TREE;
1624 if (!same_type_p (TREE_VALUE (args), size_type_node))
1626 e = 2;
1627 args = TREE_CHAIN (args);
1630 else
1631 e = 2;
1633 if (e == 2)
1634 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1635 "as first parameter", size_type_node);
1637 switch (e)
1639 case 2:
1640 args = tree_cons (NULL_TREE, size_type_node, args);
1641 /* Fall through. */
1642 case 1:
1643 type = build_exception_variant
1644 (build_function_type (ptr_type_node, args),
1645 TYPE_RAISES_EXCEPTIONS (type));
1646 /* Fall through. */
1647 default:;
1649 return type;
1652 tree
1653 coerce_delete_type (tree type)
1655 int e = 0;
1656 tree args = TYPE_ARG_TYPES (type);
1658 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1660 if (!same_type_p (TREE_TYPE (type), void_type_node))
1662 e = 1;
1663 error ("%<operator delete%> must return type %qT", void_type_node);
1666 if (!args || args == void_list_node
1667 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1669 e = 2;
1670 if (args && args != void_list_node)
1671 args = TREE_CHAIN (args);
1672 error ("%<operator delete%> takes type %qT as first parameter",
1673 ptr_type_node);
1675 switch (e)
1677 case 2:
1678 args = tree_cons (NULL_TREE, ptr_type_node, args);
1679 /* Fall through. */
1680 case 1:
1681 type = build_exception_variant
1682 (build_function_type (void_type_node, args),
1683 TYPE_RAISES_EXCEPTIONS (type));
1684 /* Fall through. */
1685 default:;
1688 return type;
1691 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1692 and mark them as needed. */
1694 static void
1695 mark_vtable_entries (tree decl)
1697 tree fnaddr;
1698 unsigned HOST_WIDE_INT idx;
1700 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1701 idx, fnaddr)
1703 tree fn;
1705 STRIP_NOPS (fnaddr);
1707 if (TREE_CODE (fnaddr) != ADDR_EXPR
1708 && TREE_CODE (fnaddr) != FDESC_EXPR)
1709 /* This entry is an offset: a virtual base class offset, a
1710 virtual call offset, an RTTI offset, etc. */
1711 continue;
1713 fn = TREE_OPERAND (fnaddr, 0);
1714 TREE_ADDRESSABLE (fn) = 1;
1715 /* When we don't have vcall offsets, we output thunks whenever
1716 we output the vtables that contain them. With vcall offsets,
1717 we know all the thunks we'll need when we emit a virtual
1718 function, so we emit the thunks there instead. */
1719 if (DECL_THUNK_P (fn))
1720 use_thunk (fn, /*emit_p=*/0);
1721 mark_used (fn);
1725 /* Set DECL up to have the closest approximation of "initialized common"
1726 linkage available. */
1728 void
1729 comdat_linkage (tree decl)
1731 if (flag_weak)
1732 make_decl_one_only (decl, cxx_comdat_group (decl));
1733 else if (TREE_CODE (decl) == FUNCTION_DECL
1734 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1735 /* We can just emit function and compiler-generated variables
1736 statically; having multiple copies is (for the most part) only
1737 a waste of space.
1739 There are two correctness issues, however: the address of a
1740 template instantiation with external linkage should be the
1741 same, independent of what translation unit asks for the
1742 address, and this will not hold when we emit multiple copies of
1743 the function. However, there's little else we can do.
1745 Also, by default, the typeinfo implementation assumes that
1746 there will be only one copy of the string used as the name for
1747 each type. Therefore, if weak symbols are unavailable, the
1748 run-time library should perform a more conservative check; it
1749 should perform a string comparison, rather than an address
1750 comparison. */
1751 TREE_PUBLIC (decl) = 0;
1752 else
1754 /* Static data member template instantiations, however, cannot
1755 have multiple copies. */
1756 if (DECL_INITIAL (decl) == 0
1757 || DECL_INITIAL (decl) == error_mark_node)
1758 DECL_COMMON (decl) = 1;
1759 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1761 DECL_COMMON (decl) = 1;
1762 DECL_INITIAL (decl) = error_mark_node;
1764 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1766 /* We can't do anything useful; leave vars for explicit
1767 instantiation. */
1768 DECL_EXTERNAL (decl) = 1;
1769 DECL_NOT_REALLY_EXTERN (decl) = 0;
1773 DECL_COMDAT (decl) = 1;
1776 /* For win32 we also want to put explicit instantiations in
1777 linkonce sections, so that they will be merged with implicit
1778 instantiations; otherwise we get duplicate symbol errors.
1779 For Darwin we do not want explicit instantiations to be
1780 linkonce. */
1782 void
1783 maybe_make_one_only (tree decl)
1785 /* We used to say that this was not necessary on targets that support weak
1786 symbols, because the implicit instantiations will defer to the explicit
1787 one. However, that's not actually the case in SVR4; a strong definition
1788 after a weak one is an error. Also, not making explicit
1789 instantiations one_only means that we can end up with two copies of
1790 some template instantiations. */
1791 if (! flag_weak)
1792 return;
1794 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1795 we can get away with not emitting them if they aren't used. We need
1796 to for variables so that cp_finish_decl will update their linkage,
1797 because their DECL_INITIAL may not have been set properly yet. */
1799 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1800 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1801 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1803 make_decl_one_only (decl, cxx_comdat_group (decl));
1805 if (VAR_P (decl))
1807 varpool_node *node = varpool_node_for_decl (decl);
1808 DECL_COMDAT (decl) = 1;
1809 /* Mark it needed so we don't forget to emit it. */
1810 node->forced_by_abi = true;
1811 TREE_USED (decl) = 1;
1816 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1817 This predicate will give the right answer during parsing of the
1818 function, which other tests may not. */
1820 bool
1821 vague_linkage_p (tree decl)
1823 /* Unfortunately, import_export_decl has not always been called
1824 before the function is processed, so we cannot simply check
1825 DECL_COMDAT. */
1826 if (DECL_COMDAT (decl)
1827 || (((TREE_CODE (decl) == FUNCTION_DECL
1828 && DECL_DECLARED_INLINE_P (decl))
1829 || (DECL_LANG_SPECIFIC (decl)
1830 && DECL_TEMPLATE_INSTANTIATION (decl)))
1831 && TREE_PUBLIC (decl)))
1832 return true;
1833 else if (DECL_FUNCTION_SCOPE_P (decl))
1834 /* A local static in an inline effectively has vague linkage. */
1835 return (TREE_STATIC (decl)
1836 && vague_linkage_p (DECL_CONTEXT (decl)));
1837 else
1838 return false;
1841 /* Determine whether or not we want to specifically import or export CTYPE,
1842 using various heuristics. */
1844 static void
1845 import_export_class (tree ctype)
1847 /* -1 for imported, 1 for exported. */
1848 int import_export = 0;
1850 /* It only makes sense to call this function at EOF. The reason is
1851 that this function looks at whether or not the first non-inline
1852 non-abstract virtual member function has been defined in this
1853 translation unit. But, we can't possibly know that until we've
1854 seen the entire translation unit. */
1855 gcc_assert (at_eof);
1857 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1858 return;
1860 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1861 we will have CLASSTYPE_INTERFACE_ONLY set but not
1862 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1863 heuristic because someone will supply a #pragma implementation
1864 elsewhere, and deducing it here would produce a conflict. */
1865 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1866 return;
1868 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1869 import_export = -1;
1870 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1871 import_export = 1;
1872 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1873 && !flag_implicit_templates)
1874 /* For a template class, without -fimplicit-templates, check the
1875 repository. If the virtual table is assigned to this
1876 translation unit, then export the class; otherwise, import
1877 it. */
1878 import_export = repo_export_class_p (ctype) ? 1 : -1;
1879 else if (TYPE_POLYMORPHIC_P (ctype))
1881 /* The ABI specifies that the virtual table and associated
1882 information are emitted with the key method, if any. */
1883 tree method = CLASSTYPE_KEY_METHOD (ctype);
1884 /* If weak symbol support is not available, then we must be
1885 careful not to emit the vtable when the key function is
1886 inline. An inline function can be defined in multiple
1887 translation units. If we were to emit the vtable in each
1888 translation unit containing a definition, we would get
1889 multiple definition errors at link-time. */
1890 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1891 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1894 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1895 a definition anywhere else. */
1896 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1897 import_export = 0;
1899 /* Allow back ends the chance to overrule the decision. */
1900 if (targetm.cxx.import_export_class)
1901 import_export = targetm.cxx.import_export_class (ctype, import_export);
1903 if (import_export)
1905 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1906 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1910 /* Return true if VAR has already been provided to the back end; in that
1911 case VAR should not be modified further by the front end. */
1912 static bool
1913 var_finalized_p (tree var)
1915 return varpool_node_for_decl (var)->definition;
1918 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1919 must be emitted in this translation unit. Mark it as such. */
1921 void
1922 mark_needed (tree decl)
1924 TREE_USED (decl) = 1;
1925 if (TREE_CODE (decl) == FUNCTION_DECL)
1927 /* Extern inline functions don't become needed when referenced.
1928 If we know a method will be emitted in other TU and no new
1929 functions can be marked reachable, just use the external
1930 definition. */
1931 struct cgraph_node *node = cgraph_get_create_node (decl);
1932 node->forced_by_abi = true;
1934 else if (TREE_CODE (decl) == VAR_DECL)
1936 varpool_node *node = varpool_node_for_decl (decl);
1937 /* C++ frontend use mark_decl_references to force COMDAT variables
1938 to be output that might appear dead otherwise. */
1939 node->forced_by_abi = true;
1943 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1944 returns true if a definition of this entity should be provided in
1945 this object file. Callers use this function to determine whether
1946 or not to let the back end know that a definition of DECL is
1947 available in this translation unit. */
1949 bool
1950 decl_needed_p (tree decl)
1952 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1953 /* This function should only be called at the end of the translation
1954 unit. We cannot be sure of whether or not something will be
1955 COMDAT until that point. */
1956 gcc_assert (at_eof);
1958 /* All entities with external linkage that are not COMDAT should be
1959 emitted; they may be referred to from other object files. */
1960 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1961 return true;
1962 /* If this entity was used, let the back end see it; it will decide
1963 whether or not to emit it into the object file. */
1964 if (TREE_USED (decl))
1965 return true;
1966 /* Functions marked "dllexport" must be emitted so that they are
1967 visible to other DLLs. */
1968 if (flag_keep_inline_dllexport
1969 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1970 return true;
1971 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1972 reference to DECL might cause it to be emitted later. */
1973 return false;
1976 /* If necessary, write out the vtables for the dynamic class CTYPE.
1977 Returns true if any vtables were emitted. */
1979 static bool
1980 maybe_emit_vtables (tree ctype)
1982 tree vtbl;
1983 tree primary_vtbl;
1984 int needed = 0;
1985 varpool_node *current = NULL, *last = NULL;
1987 /* If the vtables for this class have already been emitted there is
1988 nothing more to do. */
1989 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1990 if (var_finalized_p (primary_vtbl))
1991 return false;
1992 /* Ignore dummy vtables made by get_vtable_decl. */
1993 if (TREE_TYPE (primary_vtbl) == void_type_node)
1994 return false;
1996 /* On some targets, we cannot determine the key method until the end
1997 of the translation unit -- which is when this function is
1998 called. */
1999 if (!targetm.cxx.key_method_may_be_inline ())
2000 determine_key_method (ctype);
2002 /* See if any of the vtables are needed. */
2003 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2005 import_export_decl (vtbl);
2006 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2007 needed = 1;
2009 if (!needed)
2011 /* If the references to this class' vtables are optimized away,
2012 still emit the appropriate debugging information. See
2013 dfs_debug_mark. */
2014 if (DECL_COMDAT (primary_vtbl)
2015 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2016 note_debug_info_needed (ctype);
2017 if (mark_all_virtuals)
2018 /* Make sure virtual functions get instantiated/synthesized so that
2019 they can be inlined after devirtualization even if the vtable is
2020 never emitted. */
2021 mark_vtable_entries (primary_vtbl);
2022 return false;
2025 /* The ABI requires that we emit all of the vtables if we emit any
2026 of them. */
2027 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2029 /* Mark entities references from the virtual table as used. */
2030 mark_vtable_entries (vtbl);
2032 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2034 vec<tree, va_gc> *cleanups = NULL;
2035 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2036 LOOKUP_NORMAL);
2038 /* It had better be all done at compile-time. */
2039 gcc_assert (!expr && !cleanups);
2042 /* Write it out. */
2043 DECL_EXTERNAL (vtbl) = 0;
2044 rest_of_decl_compilation (vtbl, 1, 1);
2046 /* Because we're only doing syntax-checking, we'll never end up
2047 actually marking the variable as written. */
2048 if (flag_syntax_only)
2049 TREE_ASM_WRITTEN (vtbl) = 1;
2050 else if (DECL_ONE_ONLY (vtbl))
2052 current = varpool_node_for_decl (vtbl);
2053 if (last)
2054 symtab_add_to_same_comdat_group (current, last);
2055 last = current;
2059 /* Since we're writing out the vtable here, also write the debug
2060 info. */
2061 note_debug_info_needed (ctype);
2063 return true;
2066 /* A special return value from type_visibility meaning internal
2067 linkage. */
2069 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2071 /* walk_tree helper function for type_visibility. */
2073 static tree
2074 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2076 int *vis_p = (int *)data;
2077 if (! TYPE_P (*tp))
2079 *walk_subtrees = 0;
2081 else if (OVERLOAD_TYPE_P (*tp)
2082 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2084 *vis_p = VISIBILITY_ANON;
2085 return *tp;
2087 else if (CLASS_TYPE_P (*tp)
2088 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2089 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2090 return NULL;
2093 /* Returns the visibility of TYPE, which is the minimum visibility of its
2094 component types. */
2096 static int
2097 type_visibility (tree type)
2099 int vis = VISIBILITY_DEFAULT;
2100 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2101 return vis;
2104 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2105 specified (or if VISIBILITY is static). If TMPL is true, this
2106 constraint is for a template argument, and takes precedence
2107 over explicitly-specified visibility on the template. */
2109 static void
2110 constrain_visibility (tree decl, int visibility, bool tmpl)
2112 if (visibility == VISIBILITY_ANON)
2114 /* extern "C" declarations aren't affected by the anonymous
2115 namespace. */
2116 if (!DECL_EXTERN_C_P (decl))
2118 TREE_PUBLIC (decl) = 0;
2119 DECL_WEAK (decl) = 0;
2120 DECL_COMMON (decl) = 0;
2121 if (TREE_CODE (decl) == FUNCTION_DECL
2122 || TREE_CODE (decl) == VAR_DECL)
2124 struct symtab_node *snode = symtab_get_node (decl);
2126 if (snode)
2127 snode->set_comdat_group (NULL);
2129 DECL_INTERFACE_KNOWN (decl) = 1;
2130 if (DECL_LANG_SPECIFIC (decl))
2131 DECL_NOT_REALLY_EXTERN (decl) = 1;
2134 else if (visibility > DECL_VISIBILITY (decl)
2135 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2137 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2138 /* This visibility was not specified. */
2139 DECL_VISIBILITY_SPECIFIED (decl) = false;
2143 /* Constrain the visibility of DECL based on the visibility of its template
2144 arguments. */
2146 static void
2147 constrain_visibility_for_template (tree decl, tree targs)
2149 /* If this is a template instantiation, check the innermost
2150 template args for visibility constraints. The outer template
2151 args are covered by the class check. */
2152 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2153 int i;
2154 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2156 int vis = 0;
2158 tree arg = TREE_VEC_ELT (args, i-1);
2159 if (TYPE_P (arg))
2160 vis = type_visibility (arg);
2161 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2163 STRIP_NOPS (arg);
2164 if (TREE_CODE (arg) == ADDR_EXPR)
2165 arg = TREE_OPERAND (arg, 0);
2166 if (VAR_OR_FUNCTION_DECL_P (arg))
2168 if (! TREE_PUBLIC (arg))
2169 vis = VISIBILITY_ANON;
2170 else
2171 vis = DECL_VISIBILITY (arg);
2174 if (vis)
2175 constrain_visibility (decl, vis, true);
2179 /* Like c_determine_visibility, but with additional C++-specific
2180 behavior.
2182 Function-scope entities can rely on the function's visibility because
2183 it is set in start_preparsed_function.
2185 Class-scope entities cannot rely on the class's visibility until the end
2186 of the enclosing class definition.
2188 Note that because namespaces have multiple independent definitions,
2189 namespace visibility is handled elsewhere using the #pragma visibility
2190 machinery rather than by decorating the namespace declaration.
2192 The goal is for constraints from the type to give a diagnostic, and
2193 other constraints to be applied silently. */
2195 void
2196 determine_visibility (tree decl)
2198 tree class_type = NULL_TREE;
2199 bool use_template;
2200 bool orig_visibility_specified;
2201 enum symbol_visibility orig_visibility;
2203 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2205 /* Only relevant for names with external linkage. */
2206 if (!TREE_PUBLIC (decl))
2207 return;
2209 /* Cloned constructors and destructors get the same visibility as
2210 the underlying function. That should be set up in
2211 maybe_clone_body. */
2212 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2214 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2215 orig_visibility = DECL_VISIBILITY (decl);
2217 if (TREE_CODE (decl) == TYPE_DECL)
2219 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2220 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2221 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2222 use_template = 1;
2223 else
2224 use_template = 0;
2226 else if (DECL_LANG_SPECIFIC (decl))
2227 use_template = DECL_USE_TEMPLATE (decl);
2228 else
2229 use_template = 0;
2231 /* If DECL is a member of a class, visibility specifiers on the
2232 class can influence the visibility of the DECL. */
2233 if (DECL_CLASS_SCOPE_P (decl))
2234 class_type = DECL_CONTEXT (decl);
2235 else
2237 /* Not a class member. */
2239 /* Virtual tables have DECL_CONTEXT set to their associated class,
2240 so they are automatically handled above. */
2241 gcc_assert (!VAR_P (decl)
2242 || !DECL_VTABLE_OR_VTT_P (decl));
2244 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2246 /* Local statics and classes get the visibility of their
2247 containing function by default, except that
2248 -fvisibility-inlines-hidden doesn't affect them. */
2249 tree fn = DECL_CONTEXT (decl);
2250 if (DECL_VISIBILITY_SPECIFIED (fn))
2252 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2253 DECL_VISIBILITY_SPECIFIED (decl) =
2254 DECL_VISIBILITY_SPECIFIED (fn);
2256 else
2258 if (DECL_CLASS_SCOPE_P (fn))
2259 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2260 else if (determine_hidden_inline (fn))
2262 DECL_VISIBILITY (decl) = default_visibility;
2263 DECL_VISIBILITY_SPECIFIED (decl) =
2264 visibility_options.inpragma;
2266 else
2268 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2269 DECL_VISIBILITY_SPECIFIED (decl) =
2270 DECL_VISIBILITY_SPECIFIED (fn);
2274 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2275 but have no TEMPLATE_INFO, so don't try to check it. */
2276 use_template = 0;
2278 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2279 && flag_visibility_ms_compat)
2281 /* Under -fvisibility-ms-compat, types are visible by default,
2282 even though their contents aren't. */
2283 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2284 int underlying_vis = type_visibility (underlying_type);
2285 if (underlying_vis == VISIBILITY_ANON
2286 || (CLASS_TYPE_P (underlying_type)
2287 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2288 constrain_visibility (decl, underlying_vis, false);
2289 else
2290 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2292 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2294 /* tinfo visibility is based on the type it's for. */
2295 constrain_visibility
2296 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2298 /* Give the target a chance to override the visibility associated
2299 with DECL. */
2300 if (TREE_PUBLIC (decl)
2301 && !DECL_REALLY_EXTERN (decl)
2302 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2303 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2304 targetm.cxx.determine_class_data_visibility (decl);
2306 else if (use_template)
2307 /* Template instantiations and specializations get visibility based
2308 on their template unless they override it with an attribute. */;
2309 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2311 if (determine_hidden_inline (decl))
2312 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2313 else
2315 /* Set default visibility to whatever the user supplied with
2316 #pragma GCC visibility or a namespace visibility attribute. */
2317 DECL_VISIBILITY (decl) = default_visibility;
2318 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2323 if (use_template)
2325 /* If the specialization doesn't specify visibility, use the
2326 visibility from the template. */
2327 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2328 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2329 : DECL_TEMPLATE_INFO (decl));
2330 tree args = TI_ARGS (tinfo);
2331 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2332 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2333 : DECL_ATTRIBUTES (decl));
2335 if (args != error_mark_node)
2337 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2339 if (!DECL_VISIBILITY_SPECIFIED (decl))
2341 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2342 && determine_hidden_inline (decl))
2343 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2344 else
2346 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2347 DECL_VISIBILITY_SPECIFIED (decl)
2348 = DECL_VISIBILITY_SPECIFIED (pattern);
2352 if (args
2353 /* Template argument visibility outweighs #pragma or namespace
2354 visibility, but not an explicit attribute. */
2355 && !lookup_attribute ("visibility", attribs))
2357 int depth = TMPL_ARGS_DEPTH (args);
2358 if (DECL_VISIBILITY_SPECIFIED (decl))
2360 /* A class template member with explicit visibility
2361 overrides the class visibility, so we need to apply
2362 all the levels of template args directly. */
2363 int i;
2364 for (i = 1; i <= depth; ++i)
2366 tree lev = TMPL_ARGS_LEVEL (args, i);
2367 constrain_visibility_for_template (decl, lev);
2370 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2371 /* Limit visibility based on its template arguments. */
2372 constrain_visibility_for_template (decl, args);
2377 if (class_type)
2378 determine_visibility_from_class (decl, class_type);
2380 if (decl_anon_ns_mem_p (decl))
2381 /* Names in an anonymous namespace get internal linkage.
2382 This might change once we implement export. */
2383 constrain_visibility (decl, VISIBILITY_ANON, false);
2384 else if (TREE_CODE (decl) != TYPE_DECL)
2386 /* Propagate anonymity from type to decl. */
2387 int tvis = type_visibility (TREE_TYPE (decl));
2388 if (tvis == VISIBILITY_ANON
2389 || ! DECL_VISIBILITY_SPECIFIED (decl))
2390 constrain_visibility (decl, tvis, false);
2392 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2393 /* DR 757: A type without linkage shall not be used as the type of a
2394 variable or function with linkage, unless
2395 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2396 o the variable or function is not used (3.2 [basic.def.odr]) or is
2397 defined in the same translation unit.
2399 Since non-extern "C" decls need to be defined in the same
2400 translation unit, we can make the type internal. */
2401 constrain_visibility (decl, VISIBILITY_ANON, false);
2403 /* If visibility changed and DECL already has DECL_RTL, ensure
2404 symbol flags are updated. */
2405 if ((DECL_VISIBILITY (decl) != orig_visibility
2406 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2407 && ((VAR_P (decl) && TREE_STATIC (decl))
2408 || TREE_CODE (decl) == FUNCTION_DECL)
2409 && DECL_RTL_SET_P (decl))
2410 make_decl_rtl (decl);
2413 /* By default, static data members and function members receive
2414 the visibility of their containing class. */
2416 static void
2417 determine_visibility_from_class (tree decl, tree class_type)
2419 if (DECL_VISIBILITY_SPECIFIED (decl))
2420 return;
2422 if (determine_hidden_inline (decl))
2423 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2424 else
2426 /* Default to the class visibility. */
2427 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2428 DECL_VISIBILITY_SPECIFIED (decl)
2429 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2432 /* Give the target a chance to override the visibility associated
2433 with DECL. */
2434 if (VAR_P (decl)
2435 && (DECL_TINFO_P (decl)
2436 || (DECL_VTABLE_OR_VTT_P (decl)
2437 /* Construction virtual tables are not exported because
2438 they cannot be referred to from other object files;
2439 their name is not standardized by the ABI. */
2440 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2441 && TREE_PUBLIC (decl)
2442 && !DECL_REALLY_EXTERN (decl)
2443 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2444 targetm.cxx.determine_class_data_visibility (decl);
2447 /* Returns true iff DECL is an inline that should get hidden visibility
2448 because of -fvisibility-inlines-hidden. */
2450 static bool
2451 determine_hidden_inline (tree decl)
2453 return (visibility_options.inlines_hidden
2454 /* Don't do this for inline templates; specializations might not be
2455 inline, and we don't want them to inherit the hidden
2456 visibility. We'll set it here for all inline instantiations. */
2457 && !processing_template_decl
2458 && TREE_CODE (decl) == FUNCTION_DECL
2459 && DECL_DECLARED_INLINE_P (decl)
2460 && (! DECL_LANG_SPECIFIC (decl)
2461 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2464 /* Constrain the visibility of a class TYPE based on the visibility of its
2465 field types. Warn if any fields require lesser visibility. */
2467 void
2468 constrain_class_visibility (tree type)
2470 tree binfo;
2471 tree t;
2472 int i;
2474 int vis = type_visibility (type);
2476 if (vis == VISIBILITY_ANON
2477 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2478 return;
2480 /* Don't warn about visibility if the class has explicit visibility. */
2481 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2482 vis = VISIBILITY_INTERNAL;
2484 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2485 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2487 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2488 int subvis = type_visibility (ftype);
2490 if (subvis == VISIBILITY_ANON)
2492 if (!in_main_input_context ())
2493 warning (0, "\
2494 %qT has a field %qD whose type uses the anonymous namespace",
2495 type, t);
2497 else if (MAYBE_CLASS_TYPE_P (ftype)
2498 && vis < VISIBILITY_HIDDEN
2499 && subvis >= VISIBILITY_HIDDEN)
2500 warning (OPT_Wattributes, "\
2501 %qT declared with greater visibility than the type of its field %qD",
2502 type, t);
2505 binfo = TYPE_BINFO (type);
2506 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2508 int subvis = type_visibility (TREE_TYPE (t));
2510 if (subvis == VISIBILITY_ANON)
2512 if (!in_main_input_context())
2513 warning (0, "\
2514 %qT has a base %qT whose type uses the anonymous namespace",
2515 type, TREE_TYPE (t));
2517 else if (vis < VISIBILITY_HIDDEN
2518 && subvis >= VISIBILITY_HIDDEN)
2519 warning (OPT_Wattributes, "\
2520 %qT declared with greater visibility than its base %qT",
2521 type, TREE_TYPE (t));
2525 /* Functions for adjusting the visibility of a tagged type and its nested
2526 types and declarations when it gets a name for linkage purposes from a
2527 typedef. */
2529 static void bt_reset_linkage_1 (binding_entry, void *);
2530 static void bt_reset_linkage_2 (binding_entry, void *);
2532 /* First reset the visibility of all the types. */
2534 static void
2535 reset_type_linkage_1 (tree type)
2537 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2538 if (CLASS_TYPE_P (type))
2539 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2540 bt_reset_linkage_1, NULL);
2542 static void
2543 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2545 reset_type_linkage_1 (b->type);
2548 /* Then reset the visibility of any static data members or member
2549 functions that use those types. */
2551 static void
2552 reset_decl_linkage (tree decl)
2554 if (TREE_PUBLIC (decl))
2555 return;
2556 if (DECL_CLONED_FUNCTION_P (decl))
2557 return;
2558 TREE_PUBLIC (decl) = true;
2559 DECL_INTERFACE_KNOWN (decl) = false;
2560 determine_visibility (decl);
2561 tentative_decl_linkage (decl);
2563 static void
2564 reset_type_linkage_2 (tree type)
2566 if (CLASS_TYPE_P (type))
2568 if (tree vt = CLASSTYPE_VTABLES (type))
2570 tree name = mangle_vtbl_for_type (type);
2571 DECL_NAME (vt) = name;
2572 SET_DECL_ASSEMBLER_NAME (vt, name);
2573 reset_decl_linkage (vt);
2575 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2577 tree name = mangle_typeinfo_for_type (type);
2578 DECL_NAME (ti) = name;
2579 SET_DECL_ASSEMBLER_NAME (ti, name);
2580 TREE_TYPE (name) = type;
2581 reset_decl_linkage (ti);
2583 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2584 if (TREE_CODE (m) == VAR_DECL)
2585 reset_decl_linkage (m);
2586 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2587 reset_decl_linkage (m);
2588 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2589 bt_reset_linkage_2, NULL);
2592 static void
2593 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2595 reset_type_linkage_2 (b->type);
2597 void
2598 reset_type_linkage (tree type)
2600 reset_type_linkage_1 (type);
2601 reset_type_linkage_2 (type);
2604 /* Set up our initial idea of what the linkage of DECL should be. */
2606 void
2607 tentative_decl_linkage (tree decl)
2609 if (DECL_INTERFACE_KNOWN (decl))
2610 /* We've already made a decision as to how this function will
2611 be handled. */;
2612 else if (vague_linkage_p (decl))
2614 if (TREE_CODE (decl) == FUNCTION_DECL
2615 && decl_defined_p (decl))
2617 DECL_EXTERNAL (decl) = 1;
2618 DECL_NOT_REALLY_EXTERN (decl) = 1;
2619 note_vague_linkage_fn (decl);
2620 /* A non-template inline function with external linkage will
2621 always be COMDAT. As we must eventually determine the
2622 linkage of all functions, and as that causes writes to
2623 the data mapped in from the PCH file, it's advantageous
2624 to mark the functions at this point. */
2625 if (DECL_DECLARED_INLINE_P (decl)
2626 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2627 || DECL_DEFAULTED_FN (decl)))
2629 /* This function must have external linkage, as
2630 otherwise DECL_INTERFACE_KNOWN would have been
2631 set. */
2632 gcc_assert (TREE_PUBLIC (decl));
2633 comdat_linkage (decl);
2634 DECL_INTERFACE_KNOWN (decl) = 1;
2637 else if (TREE_CODE (decl) == VAR_DECL)
2638 maybe_commonize_var (decl);
2642 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2643 for DECL has not already been determined, do so now by setting
2644 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2645 function is called entities with vague linkage whose definitions
2646 are available must have TREE_PUBLIC set.
2648 If this function decides to place DECL in COMDAT, it will set
2649 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2650 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2651 callers defer that decision until it is clear that DECL is actually
2652 required. */
2654 void
2655 import_export_decl (tree decl)
2657 int emit_p;
2658 bool comdat_p;
2659 bool import_p;
2660 tree class_type = NULL_TREE;
2662 if (DECL_INTERFACE_KNOWN (decl))
2663 return;
2665 /* We cannot determine what linkage to give to an entity with vague
2666 linkage until the end of the file. For example, a virtual table
2667 for a class will be defined if and only if the key method is
2668 defined in this translation unit. As a further example, consider
2669 that when compiling a translation unit that uses PCH file with
2670 "-frepo" it would be incorrect to make decisions about what
2671 entities to emit when building the PCH; those decisions must be
2672 delayed until the repository information has been processed. */
2673 gcc_assert (at_eof);
2674 /* Object file linkage for explicit instantiations is handled in
2675 mark_decl_instantiated. For static variables in functions with
2676 vague linkage, maybe_commonize_var is used.
2678 Therefore, the only declarations that should be provided to this
2679 function are those with external linkage that are:
2681 * implicit instantiations of function templates
2683 * inline function
2685 * implicit instantiations of static data members of class
2686 templates
2688 * virtual tables
2690 * typeinfo objects
2692 Furthermore, all entities that reach this point must have a
2693 definition available in this translation unit.
2695 The following assertions check these conditions. */
2696 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2697 /* Any code that creates entities with TREE_PUBLIC cleared should
2698 also set DECL_INTERFACE_KNOWN. */
2699 gcc_assert (TREE_PUBLIC (decl));
2700 if (TREE_CODE (decl) == FUNCTION_DECL)
2701 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2702 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2703 || DECL_DECLARED_INLINE_P (decl));
2704 else
2705 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2706 || DECL_VTABLE_OR_VTT_P (decl)
2707 || DECL_TINFO_P (decl));
2708 /* Check that a definition of DECL is available in this translation
2709 unit. */
2710 gcc_assert (!DECL_REALLY_EXTERN (decl));
2712 /* Assume that DECL will not have COMDAT linkage. */
2713 comdat_p = false;
2714 /* Assume that DECL will not be imported into this translation
2715 unit. */
2716 import_p = false;
2718 /* See if the repository tells us whether or not to emit DECL in
2719 this translation unit. */
2720 emit_p = repo_emit_p (decl);
2721 if (emit_p == 0)
2722 import_p = true;
2723 else if (emit_p == 1)
2725 /* The repository indicates that this entity should be defined
2726 here. Make sure the back end honors that request. */
2727 if (VAR_P (decl))
2728 mark_needed (decl);
2729 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2730 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2732 tree clone;
2733 FOR_EACH_CLONE (clone, decl)
2734 mark_needed (clone);
2736 else
2737 mark_needed (decl);
2738 /* Output the definition as an ordinary strong definition. */
2739 DECL_EXTERNAL (decl) = 0;
2740 DECL_INTERFACE_KNOWN (decl) = 1;
2741 return;
2744 if (import_p)
2745 /* We have already decided what to do with this DECL; there is no
2746 need to check anything further. */
2748 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2750 class_type = DECL_CONTEXT (decl);
2751 import_export_class (class_type);
2752 if (TYPE_FOR_JAVA (class_type))
2753 import_p = true;
2754 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2755 && CLASSTYPE_INTERFACE_ONLY (class_type))
2756 import_p = true;
2757 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2758 && !CLASSTYPE_USE_TEMPLATE (class_type)
2759 && CLASSTYPE_KEY_METHOD (class_type)
2760 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2761 /* The ABI requires that all virtual tables be emitted with
2762 COMDAT linkage. However, on systems where COMDAT symbols
2763 don't show up in the table of contents for a static
2764 archive, or on systems without weak symbols (where we
2765 approximate COMDAT linkage by using internal linkage), the
2766 linker will report errors about undefined symbols because
2767 it will not see the virtual table definition. Therefore,
2768 in the case that we know that the virtual table will be
2769 emitted in only one translation unit, we make the virtual
2770 table an ordinary definition with external linkage. */
2771 DECL_EXTERNAL (decl) = 0;
2772 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2774 /* CLASS_TYPE is being exported from this translation unit,
2775 so DECL should be defined here. */
2776 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2777 /* If a class is declared in a header with the "extern
2778 template" extension, then it will not be instantiated,
2779 even in translation units that would normally require
2780 it. Often such classes are explicitly instantiated in
2781 one translation unit. Therefore, the explicit
2782 instantiation must be made visible to other translation
2783 units. */
2784 DECL_EXTERNAL (decl) = 0;
2785 else
2787 /* The generic C++ ABI says that class data is always
2788 COMDAT, even if there is a key function. Some
2789 variants (e.g., the ARM EABI) says that class data
2790 only has COMDAT linkage if the class data might be
2791 emitted in more than one translation unit. When the
2792 key method can be inline and is inline, we still have
2793 to arrange for comdat even though
2794 class_data_always_comdat is false. */
2795 if (!CLASSTYPE_KEY_METHOD (class_type)
2796 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2797 || targetm.cxx.class_data_always_comdat ())
2799 /* The ABI requires COMDAT linkage. Normally, we
2800 only emit COMDAT things when they are needed;
2801 make sure that we realize that this entity is
2802 indeed needed. */
2803 comdat_p = true;
2804 mark_needed (decl);
2808 else if (!flag_implicit_templates
2809 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2810 import_p = true;
2811 else
2812 comdat_p = true;
2814 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2816 tree type = TREE_TYPE (DECL_NAME (decl));
2817 if (CLASS_TYPE_P (type))
2819 class_type = type;
2820 import_export_class (type);
2821 if (CLASSTYPE_INTERFACE_KNOWN (type)
2822 && TYPE_POLYMORPHIC_P (type)
2823 && CLASSTYPE_INTERFACE_ONLY (type)
2824 /* If -fno-rtti was specified, then we cannot be sure
2825 that RTTI information will be emitted with the
2826 virtual table of the class, so we must emit it
2827 wherever it is used. */
2828 && flag_rtti)
2829 import_p = true;
2830 else
2832 if (CLASSTYPE_INTERFACE_KNOWN (type)
2833 && !CLASSTYPE_INTERFACE_ONLY (type))
2835 comdat_p = (targetm.cxx.class_data_always_comdat ()
2836 || (CLASSTYPE_KEY_METHOD (type)
2837 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2838 mark_needed (decl);
2839 if (!flag_weak)
2841 comdat_p = false;
2842 DECL_EXTERNAL (decl) = 0;
2845 else
2846 comdat_p = true;
2849 else
2850 comdat_p = true;
2852 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2854 /* DECL is an implicit instantiation of a function or static
2855 data member. */
2856 if ((flag_implicit_templates
2857 && !flag_use_repository)
2858 || (flag_implicit_inline_templates
2859 && TREE_CODE (decl) == FUNCTION_DECL
2860 && DECL_DECLARED_INLINE_P (decl)))
2861 comdat_p = true;
2862 else
2863 /* If we are not implicitly generating templates, then mark
2864 this entity as undefined in this translation unit. */
2865 import_p = true;
2867 else if (DECL_FUNCTION_MEMBER_P (decl))
2869 if (!DECL_DECLARED_INLINE_P (decl))
2871 tree ctype = DECL_CONTEXT (decl);
2872 import_export_class (ctype);
2873 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2875 DECL_NOT_REALLY_EXTERN (decl)
2876 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2877 || (DECL_DECLARED_INLINE_P (decl)
2878 && ! flag_implement_inlines
2879 && !DECL_VINDEX (decl)));
2881 if (!DECL_NOT_REALLY_EXTERN (decl))
2882 DECL_EXTERNAL (decl) = 1;
2884 /* Always make artificials weak. */
2885 if (DECL_ARTIFICIAL (decl) && flag_weak)
2886 comdat_p = true;
2887 else
2888 maybe_make_one_only (decl);
2891 else
2892 comdat_p = true;
2894 else
2895 comdat_p = true;
2897 if (import_p)
2899 /* If we are importing DECL into this translation unit, mark is
2900 an undefined here. */
2901 DECL_EXTERNAL (decl) = 1;
2902 DECL_NOT_REALLY_EXTERN (decl) = 0;
2904 else if (comdat_p)
2906 /* If we decided to put DECL in COMDAT, mark it accordingly at
2907 this point. */
2908 comdat_linkage (decl);
2911 DECL_INTERFACE_KNOWN (decl) = 1;
2914 /* Return an expression that performs the destruction of DECL, which
2915 must be a VAR_DECL whose type has a non-trivial destructor, or is
2916 an array whose (innermost) elements have a non-trivial destructor. */
2918 tree
2919 build_cleanup (tree decl)
2921 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2922 gcc_assert (clean != NULL_TREE);
2923 return clean;
2926 /* Returns the initialization guard variable for the variable DECL,
2927 which has static storage duration. */
2929 tree
2930 get_guard (tree decl)
2932 tree sname;
2933 tree guard;
2935 sname = mangle_guard_variable (decl);
2936 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2937 if (! guard)
2939 tree guard_type;
2941 /* We use a type that is big enough to contain a mutex as well
2942 as an integer counter. */
2943 guard_type = targetm.cxx.guard_type ();
2944 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2945 VAR_DECL, sname, guard_type);
2947 /* The guard should have the same linkage as what it guards. */
2948 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2949 TREE_STATIC (guard) = TREE_STATIC (decl);
2950 DECL_COMMON (guard) = DECL_COMMON (decl);
2951 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2952 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2953 if (DECL_ONE_ONLY (decl))
2954 make_decl_one_only (guard, cxx_comdat_group (guard));
2955 if (TREE_PUBLIC (decl))
2956 DECL_WEAK (guard) = DECL_WEAK (decl);
2957 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2958 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2960 DECL_ARTIFICIAL (guard) = 1;
2961 DECL_IGNORED_P (guard) = 1;
2962 TREE_USED (guard) = 1;
2963 pushdecl_top_level_and_finish (guard, NULL_TREE);
2965 return guard;
2968 /* Return those bits of the GUARD variable that should be set when the
2969 guarded entity is actually initialized. */
2971 static tree
2972 get_guard_bits (tree guard)
2974 if (!targetm.cxx.guard_mask_bit ())
2976 /* We only set the first byte of the guard, in order to leave room
2977 for a mutex in the high-order bits. */
2978 guard = build1 (ADDR_EXPR,
2979 build_pointer_type (TREE_TYPE (guard)),
2980 guard);
2981 guard = build1 (NOP_EXPR,
2982 build_pointer_type (char_type_node),
2983 guard);
2984 guard = build1 (INDIRECT_REF, char_type_node, guard);
2987 return guard;
2990 /* Return an expression which determines whether or not the GUARD
2991 variable has already been initialized. */
2993 tree
2994 get_guard_cond (tree guard)
2996 tree guard_value;
2998 /* Check to see if the GUARD is zero. */
2999 guard = get_guard_bits (guard);
3001 /* Mask off all but the low bit. */
3002 if (targetm.cxx.guard_mask_bit ())
3004 guard_value = integer_one_node;
3005 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3006 guard_value = convert (TREE_TYPE (guard), guard_value);
3007 guard = cp_build_binary_op (input_location,
3008 BIT_AND_EXPR, guard, guard_value,
3009 tf_warning_or_error);
3012 guard_value = integer_zero_node;
3013 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3014 guard_value = convert (TREE_TYPE (guard), guard_value);
3015 return cp_build_binary_op (input_location,
3016 EQ_EXPR, guard, guard_value,
3017 tf_warning_or_error);
3020 /* Return an expression which sets the GUARD variable, indicating that
3021 the variable being guarded has been initialized. */
3023 tree
3024 set_guard (tree guard)
3026 tree guard_init;
3028 /* Set the GUARD to one. */
3029 guard = get_guard_bits (guard);
3030 guard_init = integer_one_node;
3031 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3032 guard_init = convert (TREE_TYPE (guard), guard_init);
3033 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3034 tf_warning_or_error);
3037 /* Returns true iff we can tell that VAR does not have a dynamic
3038 initializer. */
3040 static bool
3041 var_defined_without_dynamic_init (tree var)
3043 /* If it's defined in another TU, we can't tell. */
3044 if (DECL_EXTERNAL (var))
3045 return false;
3046 /* If it has a non-trivial destructor, registering the destructor
3047 counts as dynamic initialization. */
3048 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3049 return false;
3050 /* If it's in this TU, its initializer has been processed. */
3051 gcc_assert (DECL_INITIALIZED_P (var));
3052 /* If it has no initializer or a constant one, it's not dynamic. */
3053 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3054 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3057 /* Returns true iff VAR is a variable that needs uses to be
3058 wrapped for possible dynamic initialization. */
3060 static bool
3061 var_needs_tls_wrapper (tree var)
3063 return (!error_operand_p (var)
3064 && DECL_THREAD_LOCAL_P (var)
3065 && !DECL_GNU_TLS_P (var)
3066 && !DECL_FUNCTION_SCOPE_P (var)
3067 && !var_defined_without_dynamic_init (var));
3070 /* Get the FUNCTION_DECL for the shared TLS init function for this
3071 translation unit. */
3073 static tree
3074 get_local_tls_init_fn (void)
3076 tree sname = get_identifier ("__tls_init");
3077 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3078 if (!fn)
3080 fn = build_lang_decl (FUNCTION_DECL, sname,
3081 build_function_type (void_type_node,
3082 void_list_node));
3083 SET_DECL_LANGUAGE (fn, lang_c);
3084 TREE_PUBLIC (fn) = false;
3085 DECL_ARTIFICIAL (fn) = true;
3086 mark_used (fn);
3087 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3089 return fn;
3092 /* Get a FUNCTION_DECL for the init function for the thread_local
3093 variable VAR. The init function will be an alias to the function
3094 that initializes all the non-local TLS variables in the translation
3095 unit. The init function is only used by the wrapper function. */
3097 static tree
3098 get_tls_init_fn (tree var)
3100 /* Only C++11 TLS vars need this init fn. */
3101 if (!var_needs_tls_wrapper (var))
3102 return NULL_TREE;
3104 /* If -fno-extern-tls-init, assume that we don't need to call
3105 a tls init function for a variable defined in another TU. */
3106 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3107 return NULL_TREE;
3109 #ifdef ASM_OUTPUT_DEF
3110 /* If the variable is internal, or if we can't generate aliases,
3111 call the local init function directly. */
3112 if (!TREE_PUBLIC (var))
3113 #endif
3114 return get_local_tls_init_fn ();
3116 tree sname = mangle_tls_init_fn (var);
3117 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3118 if (!fn)
3120 fn = build_lang_decl (FUNCTION_DECL, sname,
3121 build_function_type (void_type_node,
3122 void_list_node));
3123 SET_DECL_LANGUAGE (fn, lang_c);
3124 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3125 DECL_ARTIFICIAL (fn) = true;
3126 DECL_COMDAT (fn) = DECL_COMDAT (var);
3127 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3128 if (DECL_ONE_ONLY (var))
3129 make_decl_one_only (fn, cxx_comdat_group (fn));
3130 if (TREE_PUBLIC (var))
3132 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3133 /* If the variable is defined somewhere else and might have static
3134 initialization, make the init function a weak reference. */
3135 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3136 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3137 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3138 && DECL_EXTERNAL (var))
3139 declare_weak (fn);
3140 else
3141 DECL_WEAK (fn) = DECL_WEAK (var);
3143 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3144 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3145 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3146 DECL_IGNORED_P (fn) = 1;
3147 mark_used (fn);
3149 DECL_BEFRIENDING_CLASSES (fn) = var;
3151 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3153 return fn;
3156 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3157 variable VAR. The wrapper function calls the init function (if any) for
3158 VAR and then returns a reference to VAR. The wrapper function is used
3159 in place of VAR everywhere VAR is mentioned. */
3161 tree
3162 get_tls_wrapper_fn (tree var)
3164 /* Only C++11 TLS vars need this wrapper fn. */
3165 if (!var_needs_tls_wrapper (var))
3166 return NULL_TREE;
3168 tree sname = mangle_tls_wrapper_fn (var);
3169 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3170 if (!fn)
3172 /* A named rvalue reference is an lvalue, so the wrapper should
3173 always return an lvalue reference. */
3174 tree type = non_reference (TREE_TYPE (var));
3175 type = build_reference_type (type);
3176 tree fntype = build_function_type (type, void_list_node);
3177 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3178 SET_DECL_LANGUAGE (fn, lang_c);
3179 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3180 DECL_ARTIFICIAL (fn) = true;
3181 DECL_IGNORED_P (fn) = 1;
3182 /* The wrapper is inline and emitted everywhere var is used. */
3183 DECL_DECLARED_INLINE_P (fn) = true;
3184 if (TREE_PUBLIC (var))
3186 comdat_linkage (fn);
3187 #ifdef HAVE_GAS_HIDDEN
3188 /* Make the wrapper bind locally; there's no reason to share
3189 the wrapper between multiple shared objects. */
3190 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3191 DECL_VISIBILITY_SPECIFIED (fn) = true;
3192 #endif
3194 if (!TREE_PUBLIC (fn))
3195 DECL_INTERFACE_KNOWN (fn) = true;
3196 mark_used (fn);
3197 note_vague_linkage_fn (fn);
3199 #if 0
3200 /* We want CSE to commonize calls to the wrapper, but marking it as
3201 pure is unsafe since it has side-effects. I guess we need a new
3202 ECF flag even weaker than ECF_PURE. FIXME! */
3203 DECL_PURE_P (fn) = true;
3204 #endif
3206 DECL_BEFRIENDING_CLASSES (fn) = var;
3208 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3210 return fn;
3213 /* At EOF, generate the definition for the TLS wrapper function FN:
3215 T& var_wrapper() {
3216 if (init_fn) init_fn();
3217 return var;
3218 } */
3220 static void
3221 generate_tls_wrapper (tree fn)
3223 tree var = DECL_BEFRIENDING_CLASSES (fn);
3225 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3226 tree body = begin_function_body ();
3227 /* Only call the init fn if there might be one. */
3228 if (tree init_fn = get_tls_init_fn (var))
3230 tree if_stmt = NULL_TREE;
3231 /* If init_fn is a weakref, make sure it exists before calling. */
3232 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3234 if_stmt = begin_if_stmt ();
3235 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3236 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3237 NE_EXPR, addr, nullptr_node,
3238 tf_warning_or_error);
3239 finish_if_stmt_cond (cond, if_stmt);
3241 finish_expr_stmt (build_cxx_call
3242 (init_fn, 0, NULL, tf_warning_or_error));
3243 if (if_stmt)
3245 finish_then_clause (if_stmt);
3246 finish_if_stmt (if_stmt);
3249 else
3250 /* If there's no initialization, the wrapper is a constant function. */
3251 TREE_READONLY (fn) = true;
3252 finish_return_stmt (convert_from_reference (var));
3253 finish_function_body (body);
3254 expand_or_defer_fn (finish_function (0));
3257 /* Start the process of running a particular set of global constructors
3258 or destructors. Subroutine of do_[cd]tors. Also called from
3259 vtv_start_verification_constructor_init_function. */
3261 static tree
3262 start_objects (int method_type, int initp)
3264 tree body;
3265 tree fndecl;
3266 char type[14];
3268 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3270 if (initp != DEFAULT_INIT_PRIORITY)
3272 char joiner;
3274 #ifdef JOINER
3275 joiner = JOINER;
3276 #else
3277 joiner = '_';
3278 #endif
3280 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3282 else
3283 sprintf (type, "sub_%c", method_type);
3285 fndecl = build_lang_decl (FUNCTION_DECL,
3286 get_file_function_name (type),
3287 build_function_type_list (void_type_node,
3288 NULL_TREE));
3289 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3291 TREE_PUBLIC (current_function_decl) = 0;
3293 /* Mark as artificial because it's not explicitly in the user's
3294 source code. */
3295 DECL_ARTIFICIAL (current_function_decl) = 1;
3297 /* Mark this declaration as used to avoid spurious warnings. */
3298 TREE_USED (current_function_decl) = 1;
3300 /* Mark this function as a global constructor or destructor. */
3301 if (method_type == 'I')
3302 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3303 else
3304 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3306 body = begin_compound_stmt (BCS_FN_BODY);
3308 return body;
3311 /* Finish the process of running a particular set of global constructors
3312 or destructors. Subroutine of do_[cd]tors. */
3314 static void
3315 finish_objects (int method_type, int initp, tree body)
3317 tree fn;
3319 /* Finish up. */
3320 finish_compound_stmt (body);
3321 fn = finish_function (0);
3323 if (method_type == 'I')
3325 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3326 decl_init_priority_insert (fn, initp);
3328 else
3330 DECL_STATIC_DESTRUCTOR (fn) = 1;
3331 decl_fini_priority_insert (fn, initp);
3334 expand_or_defer_fn (fn);
3337 /* The names of the parameters to the function created to handle
3338 initializations and destructions for objects with static storage
3339 duration. */
3340 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3341 #define PRIORITY_IDENTIFIER "__priority"
3343 /* The name of the function we create to handle initializations and
3344 destructions for objects with static storage duration. */
3345 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3347 /* The declaration for the __INITIALIZE_P argument. */
3348 static GTY(()) tree initialize_p_decl;
3350 /* The declaration for the __PRIORITY argument. */
3351 static GTY(()) tree priority_decl;
3353 /* The declaration for the static storage duration function. */
3354 static GTY(()) tree ssdf_decl;
3356 /* All the static storage duration functions created in this
3357 translation unit. */
3358 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3360 /* A map from priority levels to information about that priority
3361 level. There may be many such levels, so efficient lookup is
3362 important. */
3363 static splay_tree priority_info_map;
3365 /* Begins the generation of the function that will handle all
3366 initialization and destruction of objects with static storage
3367 duration. The function generated takes two parameters of type
3368 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3369 nonzero, it performs initializations. Otherwise, it performs
3370 destructions. It only performs those initializations or
3371 destructions with the indicated __PRIORITY. The generated function
3372 returns no value.
3374 It is assumed that this function will only be called once per
3375 translation unit. */
3377 static tree
3378 start_static_storage_duration_function (unsigned count)
3380 tree type;
3381 tree body;
3382 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3384 /* Create the identifier for this function. It will be of the form
3385 SSDF_IDENTIFIER_<number>. */
3386 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3388 type = build_function_type_list (void_type_node,
3389 integer_type_node, integer_type_node,
3390 NULL_TREE);
3392 /* Create the FUNCTION_DECL itself. */
3393 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3394 get_identifier (id),
3395 type);
3396 TREE_PUBLIC (ssdf_decl) = 0;
3397 DECL_ARTIFICIAL (ssdf_decl) = 1;
3399 /* Put this function in the list of functions to be called from the
3400 static constructors and destructors. */
3401 if (!ssdf_decls)
3403 vec_alloc (ssdf_decls, 32);
3405 /* Take this opportunity to initialize the map from priority
3406 numbers to information about that priority level. */
3407 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3408 /*delete_key_fn=*/0,
3409 /*delete_value_fn=*/
3410 (splay_tree_delete_value_fn) &free);
3412 /* We always need to generate functions for the
3413 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3414 priorities later, we'll be sure to find the
3415 DEFAULT_INIT_PRIORITY. */
3416 get_priority_info (DEFAULT_INIT_PRIORITY);
3419 vec_safe_push (ssdf_decls, ssdf_decl);
3421 /* Create the argument list. */
3422 initialize_p_decl = cp_build_parm_decl
3423 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3424 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3425 TREE_USED (initialize_p_decl) = 1;
3426 priority_decl = cp_build_parm_decl
3427 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3428 DECL_CONTEXT (priority_decl) = ssdf_decl;
3429 TREE_USED (priority_decl) = 1;
3431 DECL_CHAIN (initialize_p_decl) = priority_decl;
3432 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3434 /* Put the function in the global scope. */
3435 pushdecl (ssdf_decl);
3437 /* Start the function itself. This is equivalent to declaring the
3438 function as:
3440 static void __ssdf (int __initialize_p, init __priority_p);
3442 It is static because we only need to call this function from the
3443 various constructor and destructor functions for this module. */
3444 start_preparsed_function (ssdf_decl,
3445 /*attrs=*/NULL_TREE,
3446 SF_PRE_PARSED);
3448 /* Set up the scope of the outermost block in the function. */
3449 body = begin_compound_stmt (BCS_FN_BODY);
3451 return body;
3454 /* Finish the generation of the function which performs initialization
3455 and destruction of objects with static storage duration. After
3456 this point, no more such objects can be created. */
3458 static void
3459 finish_static_storage_duration_function (tree body)
3461 /* Close out the function. */
3462 finish_compound_stmt (body);
3463 expand_or_defer_fn (finish_function (0));
3466 /* Return the information about the indicated PRIORITY level. If no
3467 code to handle this level has yet been generated, generate the
3468 appropriate prologue. */
3470 static priority_info
3471 get_priority_info (int priority)
3473 priority_info pi;
3474 splay_tree_node n;
3476 n = splay_tree_lookup (priority_info_map,
3477 (splay_tree_key) priority);
3478 if (!n)
3480 /* Create a new priority information structure, and insert it
3481 into the map. */
3482 pi = XNEW (struct priority_info_s);
3483 pi->initializations_p = 0;
3484 pi->destructions_p = 0;
3485 splay_tree_insert (priority_info_map,
3486 (splay_tree_key) priority,
3487 (splay_tree_value) pi);
3489 else
3490 pi = (priority_info) n->value;
3492 return pi;
3495 /* The effective initialization priority of a DECL. */
3497 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3498 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3499 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3501 /* Whether a DECL needs a guard to protect it against multiple
3502 initialization. */
3504 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3505 || DECL_ONE_ONLY (decl) \
3506 || DECL_WEAK (decl)))
3508 /* Called from one_static_initialization_or_destruction(),
3509 via walk_tree.
3510 Walks the initializer list of a global variable and looks for
3511 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3512 and that have their DECL_CONTEXT() == NULL.
3513 For each such temporary variable, set their DECL_CONTEXT() to
3514 the current function. This is necessary because otherwise
3515 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3516 when trying to refer to a temporary variable that does not have
3517 it's DECL_CONTECT() properly set. */
3518 static tree
3519 fix_temporary_vars_context_r (tree *node,
3520 int * /*unused*/,
3521 void * /*unused1*/)
3523 gcc_assert (current_function_decl);
3525 if (TREE_CODE (*node) == BIND_EXPR)
3527 tree var;
3529 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3530 if (VAR_P (var)
3531 && !DECL_NAME (var)
3532 && DECL_ARTIFICIAL (var)
3533 && !DECL_CONTEXT (var))
3534 DECL_CONTEXT (var) = current_function_decl;
3537 return NULL_TREE;
3540 /* Set up to handle the initialization or destruction of DECL. If
3541 INITP is nonzero, we are initializing the variable. Otherwise, we
3542 are destroying it. */
3544 static void
3545 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3547 tree guard_if_stmt = NULL_TREE;
3548 tree guard;
3550 /* If we are supposed to destruct and there's a trivial destructor,
3551 nothing has to be done. */
3552 if (!initp
3553 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3554 return;
3556 /* Trick the compiler into thinking we are at the file and line
3557 where DECL was declared so that error-messages make sense, and so
3558 that the debugger will show somewhat sensible file and line
3559 information. */
3560 input_location = DECL_SOURCE_LOCATION (decl);
3562 /* Make sure temporary variables in the initialiser all have
3563 their DECL_CONTEXT() set to a value different from NULL_TREE.
3564 This can happen when global variables initialisers are built.
3565 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3566 the temporary variables that might have been generated in the
3567 accompagning initialisers is NULL_TREE, meaning the variables have been
3568 declared in the global namespace.
3569 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3570 of the temporaries are set to the current function decl. */
3571 cp_walk_tree_without_duplicates (&init,
3572 fix_temporary_vars_context_r,
3573 NULL);
3575 /* Because of:
3577 [class.access.spec]
3579 Access control for implicit calls to the constructors,
3580 the conversion functions, or the destructor called to
3581 create and destroy a static data member is performed as
3582 if these calls appeared in the scope of the member's
3583 class.
3585 we pretend we are in a static member function of the class of
3586 which the DECL is a member. */
3587 if (member_p (decl))
3589 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3590 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3593 /* Assume we don't need a guard. */
3594 guard = NULL_TREE;
3595 /* We need a guard if this is an object with external linkage that
3596 might be initialized in more than one place. (For example, a
3597 static data member of a template, when the data member requires
3598 construction.) */
3599 if (NEEDS_GUARD_P (decl))
3601 tree guard_cond;
3603 guard = get_guard (decl);
3605 /* When using __cxa_atexit, we just check the GUARD as we would
3606 for a local static. */
3607 if (flag_use_cxa_atexit)
3609 /* When using __cxa_atexit, we never try to destroy
3610 anything from a static destructor. */
3611 gcc_assert (initp);
3612 guard_cond = get_guard_cond (guard);
3614 /* If we don't have __cxa_atexit, then we will be running
3615 destructors from .fini sections, or their equivalents. So,
3616 we need to know how many times we've tried to initialize this
3617 object. We do initializations only if the GUARD is zero,
3618 i.e., if we are the first to initialize the variable. We do
3619 destructions only if the GUARD is one, i.e., if we are the
3620 last to destroy the variable. */
3621 else if (initp)
3622 guard_cond
3623 = cp_build_binary_op (input_location,
3624 EQ_EXPR,
3625 cp_build_unary_op (PREINCREMENT_EXPR,
3626 guard,
3627 /*noconvert=*/1,
3628 tf_warning_or_error),
3629 integer_one_node,
3630 tf_warning_or_error);
3631 else
3632 guard_cond
3633 = cp_build_binary_op (input_location,
3634 EQ_EXPR,
3635 cp_build_unary_op (PREDECREMENT_EXPR,
3636 guard,
3637 /*noconvert=*/1,
3638 tf_warning_or_error),
3639 integer_zero_node,
3640 tf_warning_or_error);
3642 guard_if_stmt = begin_if_stmt ();
3643 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3647 /* If we're using __cxa_atexit, we have not already set the GUARD,
3648 so we must do so now. */
3649 if (guard && initp && flag_use_cxa_atexit)
3650 finish_expr_stmt (set_guard (guard));
3652 /* Perform the initialization or destruction. */
3653 if (initp)
3655 if (init)
3657 finish_expr_stmt (init);
3658 if (flag_sanitize & SANITIZE_ADDRESS)
3660 varpool_node *vnode = varpool_get_node (decl);
3661 if (vnode)
3662 vnode->dynamically_initialized = 1;
3666 /* If we're using __cxa_atexit, register a function that calls the
3667 destructor for the object. */
3668 if (flag_use_cxa_atexit)
3669 finish_expr_stmt (register_dtor_fn (decl));
3671 else
3672 finish_expr_stmt (build_cleanup (decl));
3674 /* Finish the guard if-stmt, if necessary. */
3675 if (guard)
3677 finish_then_clause (guard_if_stmt);
3678 finish_if_stmt (guard_if_stmt);
3681 /* Now that we're done with DECL we don't need to pretend to be a
3682 member of its class any longer. */
3683 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3684 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3687 /* Generate code to do the initialization or destruction of the decls in VARS,
3688 a TREE_LIST of VAR_DECL with static storage duration.
3689 Whether initialization or destruction is performed is specified by INITP. */
3691 static void
3692 do_static_initialization_or_destruction (tree vars, bool initp)
3694 tree node, init_if_stmt, cond;
3696 /* Build the outer if-stmt to check for initialization or destruction. */
3697 init_if_stmt = begin_if_stmt ();
3698 cond = initp ? integer_one_node : integer_zero_node;
3699 cond = cp_build_binary_op (input_location,
3700 EQ_EXPR,
3701 initialize_p_decl,
3702 cond,
3703 tf_warning_or_error);
3704 finish_if_stmt_cond (cond, init_if_stmt);
3706 /* To make sure dynamic construction doesn't access globals from other
3707 compilation units where they might not be yet constructed, for
3708 -fsanitize=address insert __asan_before_dynamic_init call that
3709 prevents access to either all global variables that need construction
3710 in other compilation units, or at least those that haven't been
3711 initialized yet. Variables that need dynamic construction in
3712 the current compilation unit are kept accessible. */
3713 if (flag_sanitize & SANITIZE_ADDRESS)
3714 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3716 node = vars;
3717 do {
3718 tree decl = TREE_VALUE (node);
3719 tree priority_if_stmt;
3720 int priority;
3721 priority_info pi;
3723 /* If we don't need a destructor, there's nothing to do. Avoid
3724 creating a possibly empty if-stmt. */
3725 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3727 node = TREE_CHAIN (node);
3728 continue;
3731 /* Remember that we had an initialization or finalization at this
3732 priority. */
3733 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3734 pi = get_priority_info (priority);
3735 if (initp)
3736 pi->initializations_p = 1;
3737 else
3738 pi->destructions_p = 1;
3740 /* Conditionalize this initialization on being in the right priority
3741 and being initializing/finalizing appropriately. */
3742 priority_if_stmt = begin_if_stmt ();
3743 cond = cp_build_binary_op (input_location,
3744 EQ_EXPR,
3745 priority_decl,
3746 build_int_cst (NULL_TREE, priority),
3747 tf_warning_or_error);
3748 finish_if_stmt_cond (cond, priority_if_stmt);
3750 /* Process initializers with same priority. */
3751 for (; node
3752 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3753 node = TREE_CHAIN (node))
3754 /* Do one initialization or destruction. */
3755 one_static_initialization_or_destruction (TREE_VALUE (node),
3756 TREE_PURPOSE (node), initp);
3758 /* Finish up the priority if-stmt body. */
3759 finish_then_clause (priority_if_stmt);
3760 finish_if_stmt (priority_if_stmt);
3762 } while (node);
3764 /* Revert what __asan_before_dynamic_init did by calling
3765 __asan_after_dynamic_init. */
3766 if (flag_sanitize & SANITIZE_ADDRESS)
3767 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3769 /* Finish up the init/destruct if-stmt body. */
3770 finish_then_clause (init_if_stmt);
3771 finish_if_stmt (init_if_stmt);
3774 /* VARS is a list of variables with static storage duration which may
3775 need initialization and/or finalization. Remove those variables
3776 that don't really need to be initialized or finalized, and return
3777 the resulting list. The order in which the variables appear in
3778 VARS is in reverse order of the order in which they should actually
3779 be initialized. The list we return is in the unreversed order;
3780 i.e., the first variable should be initialized first. */
3782 static tree
3783 prune_vars_needing_no_initialization (tree *vars)
3785 tree *var = vars;
3786 tree result = NULL_TREE;
3788 while (*var)
3790 tree t = *var;
3791 tree decl = TREE_VALUE (t);
3792 tree init = TREE_PURPOSE (t);
3794 /* Deal gracefully with error. */
3795 if (decl == error_mark_node)
3797 var = &TREE_CHAIN (t);
3798 continue;
3801 /* The only things that can be initialized are variables. */
3802 gcc_assert (VAR_P (decl));
3804 /* If this object is not defined, we don't need to do anything
3805 here. */
3806 if (DECL_EXTERNAL (decl))
3808 var = &TREE_CHAIN (t);
3809 continue;
3812 /* Also, if the initializer already contains errors, we can bail
3813 out now. */
3814 if (init && TREE_CODE (init) == TREE_LIST
3815 && value_member (error_mark_node, init))
3817 var = &TREE_CHAIN (t);
3818 continue;
3821 /* This variable is going to need initialization and/or
3822 finalization, so we add it to the list. */
3823 *var = TREE_CHAIN (t);
3824 TREE_CHAIN (t) = result;
3825 result = t;
3828 return result;
3831 /* Make sure we have told the back end about all the variables in
3832 VARS. */
3834 static void
3835 write_out_vars (tree vars)
3837 tree v;
3839 for (v = vars; v; v = TREE_CHAIN (v))
3841 tree var = TREE_VALUE (v);
3842 if (!var_finalized_p (var))
3844 import_export_decl (var);
3845 rest_of_decl_compilation (var, 1, 1);
3850 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3851 (otherwise) that will initialize all global objects with static
3852 storage duration having the indicated PRIORITY. */
3854 static void
3855 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3856 location_t *locus)
3858 char function_key;
3859 tree fndecl;
3860 tree body;
3861 size_t i;
3863 input_location = *locus;
3864 /* ??? */
3865 /* Was: locus->line++; */
3867 /* We use `I' to indicate initialization and `D' to indicate
3868 destruction. */
3869 function_key = constructor_p ? 'I' : 'D';
3871 /* We emit the function lazily, to avoid generating empty
3872 global constructors and destructors. */
3873 body = NULL_TREE;
3875 /* For Objective-C++, we may need to initialize metadata found in this module.
3876 This must be done _before_ any other static initializations. */
3877 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3878 && constructor_p && objc_static_init_needed_p ())
3880 body = start_objects (function_key, priority);
3881 objc_generate_static_init_call (NULL_TREE);
3884 /* Call the static storage duration function with appropriate
3885 arguments. */
3886 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3888 /* Calls to pure or const functions will expand to nothing. */
3889 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3891 tree call;
3893 if (! body)
3894 body = start_objects (function_key, priority);
3896 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3897 build_int_cst (NULL_TREE,
3898 constructor_p),
3899 build_int_cst (NULL_TREE,
3900 priority),
3901 NULL_TREE);
3902 finish_expr_stmt (call);
3906 /* Close out the function. */
3907 if (body)
3908 finish_objects (function_key, priority, body);
3911 /* Generate constructor and destructor functions for the priority
3912 indicated by N. */
3914 static int
3915 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3917 location_t *locus = (location_t *) data;
3918 int priority = (int) n->key;
3919 priority_info pi = (priority_info) n->value;
3921 /* Generate the functions themselves, but only if they are really
3922 needed. */
3923 if (pi->initializations_p)
3924 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3925 if (pi->destructions_p)
3926 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3928 /* Keep iterating. */
3929 return 0;
3932 /* Java requires that we be able to reference a local address for a
3933 method, and not be confused by PLT entries. If hidden aliases are
3934 supported, collect and return all the functions for which we should
3935 emit a hidden alias. */
3937 static struct pointer_set_t *
3938 collect_candidates_for_java_method_aliases (void)
3940 struct cgraph_node *node;
3941 struct pointer_set_t *candidates = NULL;
3943 #ifndef HAVE_GAS_HIDDEN
3944 return candidates;
3945 #endif
3947 FOR_EACH_FUNCTION (node)
3949 tree fndecl = node->decl;
3951 if (DECL_CLASS_SCOPE_P (fndecl)
3952 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3953 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3955 if (candidates == NULL)
3956 candidates = pointer_set_create ();
3957 pointer_set_insert (candidates, fndecl);
3961 return candidates;
3965 /* Java requires that we be able to reference a local address for a
3966 method, and not be confused by PLT entries. If hidden aliases are
3967 supported, emit one for each java function that we've emitted.
3968 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3969 by collect_candidates_for_java_method_aliases. */
3971 static void
3972 build_java_method_aliases (struct pointer_set_t *candidates)
3974 struct cgraph_node *node;
3976 #ifndef HAVE_GAS_HIDDEN
3977 return;
3978 #endif
3980 FOR_EACH_FUNCTION (node)
3982 tree fndecl = node->decl;
3984 if (TREE_ASM_WRITTEN (fndecl)
3985 && pointer_set_contains (candidates, fndecl))
3987 /* Mangle the name in a predictable way; we need to reference
3988 this from a java compiled object file. */
3989 tree oid, nid, alias;
3990 const char *oname;
3991 char *nname;
3993 oid = DECL_ASSEMBLER_NAME (fndecl);
3994 oname = IDENTIFIER_POINTER (oid);
3995 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3996 nname = ACONCAT (("_ZGA", oname+2, NULL));
3997 nid = get_identifier (nname);
3999 alias = make_alias_for (fndecl, nid);
4000 TREE_PUBLIC (alias) = 1;
4001 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4003 assemble_alias (alias, oid);
4008 /* Return C++ property of T, based on given operation OP. */
4010 static int
4011 cpp_check (tree t, cpp_operation op)
4013 switch (op)
4015 case IS_ABSTRACT:
4016 return DECL_PURE_VIRTUAL_P (t);
4017 case IS_CONSTRUCTOR:
4018 return DECL_CONSTRUCTOR_P (t);
4019 case IS_DESTRUCTOR:
4020 return DECL_DESTRUCTOR_P (t);
4021 case IS_COPY_CONSTRUCTOR:
4022 return DECL_COPY_CONSTRUCTOR_P (t);
4023 case IS_TEMPLATE:
4024 return TREE_CODE (t) == TEMPLATE_DECL;
4025 case IS_TRIVIAL:
4026 return trivial_type_p (t);
4027 default:
4028 return 0;
4032 /* Collect source file references recursively, starting from NAMESPC. */
4034 static void
4035 collect_source_refs (tree namespc)
4037 tree t;
4039 if (!namespc)
4040 return;
4042 /* Iterate over names in this name space. */
4043 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4044 if (!DECL_IS_BUILTIN (t) )
4045 collect_source_ref (DECL_SOURCE_FILE (t));
4047 /* Dump siblings, if any */
4048 collect_source_refs (TREE_CHAIN (namespc));
4050 /* Dump children, if any */
4051 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4054 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4055 starting from NAMESPC. */
4057 static void
4058 collect_ada_namespace (tree namespc, const char *source_file)
4060 if (!namespc)
4061 return;
4063 /* Collect decls from this namespace */
4064 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4066 /* Collect siblings, if any */
4067 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4069 /* Collect children, if any */
4070 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4073 /* Returns true iff there is a definition available for variable or
4074 function DECL. */
4076 static bool
4077 decl_defined_p (tree decl)
4079 if (TREE_CODE (decl) == FUNCTION_DECL)
4080 return (DECL_INITIAL (decl) != NULL_TREE);
4081 else
4083 gcc_assert (VAR_P (decl));
4084 return !DECL_EXTERNAL (decl);
4088 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4090 [expr.const]
4092 An integral constant-expression can only involve ... const
4093 variables of integral or enumeration types initialized with
4094 constant expressions ...
4096 C++0x also allows constexpr variables and temporaries initialized
4097 with constant expressions. We handle the former here, but the latter
4098 are just folded away in cxx_eval_constant_expression.
4100 The standard does not require that the expression be non-volatile.
4101 G++ implements the proposed correction in DR 457. */
4103 bool
4104 decl_constant_var_p (tree decl)
4106 if (!decl_maybe_constant_var_p (decl))
4107 return false;
4109 /* We don't know if a template static data member is initialized with
4110 a constant expression until we instantiate its initializer. Even
4111 in the case of a constexpr variable, we can't treat it as a
4112 constant until its initializer is complete in case it's used in
4113 its own initializer. */
4114 mark_used (decl);
4115 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4118 /* Returns true if DECL could be a symbolic constant variable, depending on
4119 its initializer. */
4121 bool
4122 decl_maybe_constant_var_p (tree decl)
4124 tree type = TREE_TYPE (decl);
4125 if (!VAR_P (decl))
4126 return false;
4127 if (DECL_DECLARED_CONSTEXPR_P (decl))
4128 return true;
4129 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4130 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4133 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4134 called from grokfndecl and grokvardecl; in all modes it is called from
4135 cp_write_global_declarations. */
4137 void
4138 no_linkage_error (tree decl)
4140 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4141 /* In C++11 it's ok if the decl is defined. */
4142 return;
4143 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4144 if (t == NULL_TREE)
4145 /* The type that got us on no_linkage_decls must have gotten a name for
4146 linkage purposes. */;
4147 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4148 /* The type might end up having a typedef name for linkage purposes. */
4149 vec_safe_push (no_linkage_decls, decl);
4150 else if (TYPE_ANONYMOUS_P (t))
4152 bool d = false;
4153 if (cxx_dialect >= cxx11)
4154 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4155 "anonymous type, is used but never defined", decl);
4156 else if (DECL_EXTERN_C_P (decl))
4157 /* Allow this; it's pretty common in C. */;
4158 else if (TREE_CODE (decl) == VAR_DECL)
4159 /* DRs 132, 319 and 389 seem to indicate types with
4160 no linkage can only be used to declare extern "C"
4161 entities. Since it's not always an error in the
4162 ISO C++ 90 Standard, we only issue a warning. */
4163 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4164 "with no linkage used to declare variable %q#D with "
4165 "linkage", decl);
4166 else
4167 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4168 "linkage used to declare function %q#D with linkage",
4169 decl);
4170 if (d && is_typedef_decl (TYPE_NAME (t)))
4171 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4172 "to the unqualified type, so it is not used for linkage",
4173 TYPE_NAME (t));
4175 else if (cxx_dialect >= cxx11)
4176 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4177 "%qT, is used but never defined", decl, t);
4178 else if (TREE_CODE (decl) == VAR_DECL)
4179 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4180 "used to declare variable %q#D with linkage", t, decl);
4181 else
4182 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4183 "to declare function %q#D with linkage", t, decl);
4186 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4188 static void
4189 collect_all_refs (const char *source_file)
4191 collect_ada_namespace (global_namespace, source_file);
4194 /* Clear DECL_EXTERNAL for NODE. */
4196 static bool
4197 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4199 DECL_EXTERNAL (node->decl) = 0;
4200 return false;
4203 /* Build up the function to run dynamic initializers for thread_local
4204 variables in this translation unit and alias the init functions for the
4205 individual variables to it. */
4207 static void
4208 handle_tls_init (void)
4210 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4211 if (vars == NULL_TREE)
4212 return;
4214 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4216 write_out_vars (vars);
4218 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4219 boolean_type_node);
4220 TREE_PUBLIC (guard) = false;
4221 TREE_STATIC (guard) = true;
4222 DECL_ARTIFICIAL (guard) = true;
4223 DECL_IGNORED_P (guard) = true;
4224 TREE_USED (guard) = true;
4225 set_decl_tls_model (guard, decl_default_tls_model (guard));
4226 pushdecl_top_level_and_finish (guard, NULL_TREE);
4228 tree fn = get_local_tls_init_fn ();
4229 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4230 tree body = begin_function_body ();
4231 tree if_stmt = begin_if_stmt ();
4232 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4233 tf_warning_or_error);
4234 finish_if_stmt_cond (cond, if_stmt);
4235 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4236 tf_warning_or_error));
4237 for (; vars; vars = TREE_CHAIN (vars))
4239 tree var = TREE_VALUE (vars);
4240 tree init = TREE_PURPOSE (vars);
4241 one_static_initialization_or_destruction (var, init, true);
4243 #ifdef ASM_OUTPUT_DEF
4244 /* Output init aliases even with -fno-extern-tls-init. */
4245 if (TREE_PUBLIC (var))
4247 tree single_init_fn = get_tls_init_fn (var);
4248 if (single_init_fn == NULL_TREE)
4249 continue;
4250 cgraph_node *alias
4251 = cgraph_same_body_alias (cgraph_get_create_node (fn),
4252 single_init_fn, fn);
4253 gcc_assert (alias != NULL);
4255 #endif
4258 finish_then_clause (if_stmt);
4259 finish_if_stmt (if_stmt);
4260 finish_function_body (body);
4261 expand_or_defer_fn (finish_function (0));
4264 /* The entire file is now complete. If requested, dump everything
4265 to a file. */
4267 static void
4268 dump_tu (void)
4270 int flags;
4271 FILE *stream = dump_begin (TDI_tu, &flags);
4273 if (stream)
4275 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4276 dump_end (TDI_tu, stream);
4280 /* This routine is called at the end of compilation.
4281 Its job is to create all the code needed to initialize and
4282 destroy the global aggregates. We do the destruction
4283 first, since that way we only need to reverse the decls once. */
4285 void
4286 cp_write_global_declarations (void)
4288 tree vars;
4289 bool reconsider;
4290 size_t i;
4291 location_t locus;
4292 unsigned ssdf_count = 0;
4293 int retries = 0;
4294 tree decl;
4295 struct pointer_set_t *candidates;
4297 locus = input_location;
4298 at_eof = 1;
4300 /* Bad parse errors. Just forget about it. */
4301 if (! global_bindings_p () || current_class_type
4302 || !vec_safe_is_empty (decl_namespace_list))
4303 return;
4305 /* This is the point to write out a PCH if we're doing that.
4306 In that case we do not want to do anything else. */
4307 if (pch_file)
4309 c_common_write_pch ();
4310 dump_tu ();
4311 return;
4314 cgraph_process_same_body_aliases ();
4316 /* Handle -fdump-ada-spec[-slim] */
4317 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4319 if (flag_dump_ada_spec_slim)
4320 collect_source_ref (main_input_filename);
4321 else
4322 collect_source_refs (global_namespace);
4324 dump_ada_specs (collect_all_refs, cpp_check);
4327 /* FIXME - huh? was input_line -= 1;*/
4329 timevar_start (TV_PHASE_DEFERRED);
4331 /* We now have to write out all the stuff we put off writing out.
4332 These include:
4334 o Template specializations that we have not yet instantiated,
4335 but which are needed.
4336 o Initialization and destruction for non-local objects with
4337 static storage duration. (Local objects with static storage
4338 duration are initialized when their scope is first entered,
4339 and are cleaned up via atexit.)
4340 o Virtual function tables.
4342 All of these may cause others to be needed. For example,
4343 instantiating one function may cause another to be needed, and
4344 generating the initializer for an object may cause templates to be
4345 instantiated, etc., etc. */
4347 emit_support_tinfos ();
4348 int errs = errorcount + sorrycount;
4349 bool explained_devirt = false;
4353 tree t;
4354 tree decl;
4356 reconsider = false;
4358 /* If there are templates that we've put off instantiating, do
4359 them now. */
4360 instantiate_pending_templates (retries);
4361 ggc_collect ();
4363 /* Write out virtual tables as required. Note that writing out
4364 the virtual table for a template class may cause the
4365 instantiation of members of that class. If we write out
4366 vtables then we remove the class from our list so we don't
4367 have to look at it again. */
4369 while (keyed_classes != NULL_TREE
4370 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4372 reconsider = true;
4373 keyed_classes = TREE_CHAIN (keyed_classes);
4376 t = keyed_classes;
4377 if (t != NULL_TREE)
4379 tree next = TREE_CHAIN (t);
4381 while (next)
4383 if (maybe_emit_vtables (TREE_VALUE (next)))
4385 reconsider = true;
4386 TREE_CHAIN (t) = TREE_CHAIN (next);
4388 else
4389 t = next;
4391 next = TREE_CHAIN (t);
4395 /* Write out needed type info variables. We have to be careful
4396 looping through unemitted decls, because emit_tinfo_decl may
4397 cause other variables to be needed. New elements will be
4398 appended, and we remove from the vector those that actually
4399 get emitted. */
4400 for (i = unemitted_tinfo_decls->length ();
4401 unemitted_tinfo_decls->iterate (--i, &t);)
4402 if (emit_tinfo_decl (t))
4404 reconsider = true;
4405 unemitted_tinfo_decls->unordered_remove (i);
4408 /* The list of objects with static storage duration is built up
4409 in reverse order. We clear STATIC_AGGREGATES so that any new
4410 aggregates added during the initialization of these will be
4411 initialized in the correct order when we next come around the
4412 loop. */
4413 vars = prune_vars_needing_no_initialization (&static_aggregates);
4415 if (vars)
4417 /* We need to start a new initialization function each time
4418 through the loop. That's because we need to know which
4419 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4420 isn't computed until a function is finished, and written
4421 out. That's a deficiency in the back end. When this is
4422 fixed, these initialization functions could all become
4423 inline, with resulting performance improvements. */
4424 tree ssdf_body;
4426 /* Set the line and file, so that it is obviously not from
4427 the source file. */
4428 input_location = locus;
4429 ssdf_body = start_static_storage_duration_function (ssdf_count);
4431 /* Make sure the back end knows about all the variables. */
4432 write_out_vars (vars);
4434 /* First generate code to do all the initializations. */
4435 if (vars)
4436 do_static_initialization_or_destruction (vars, /*initp=*/true);
4438 /* Then, generate code to do all the destructions. Do these
4439 in reverse order so that the most recently constructed
4440 variable is the first destroyed. If we're using
4441 __cxa_atexit, then we don't need to do this; functions
4442 were registered at initialization time to destroy the
4443 local statics. */
4444 if (!flag_use_cxa_atexit && vars)
4446 vars = nreverse (vars);
4447 do_static_initialization_or_destruction (vars, /*initp=*/false);
4449 else
4450 vars = NULL_TREE;
4452 /* Finish up the static storage duration function for this
4453 round. */
4454 input_location = locus;
4455 finish_static_storage_duration_function (ssdf_body);
4457 /* All those initializations and finalizations might cause
4458 us to need more inline functions, more template
4459 instantiations, etc. */
4460 reconsider = true;
4461 ssdf_count++;
4462 /* ??? was: locus.line++; */
4465 /* Now do the same for thread_local variables. */
4466 handle_tls_init ();
4468 /* Go through the set of inline functions whose bodies have not
4469 been emitted yet. If out-of-line copies of these functions
4470 are required, emit them. */
4471 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4473 /* Does it need synthesizing? */
4474 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4475 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4477 /* Even though we're already at the top-level, we push
4478 there again. That way, when we pop back a few lines
4479 hence, all of our state is restored. Otherwise,
4480 finish_function doesn't clean things up, and we end
4481 up with CURRENT_FUNCTION_DECL set. */
4482 push_to_top_level ();
4483 /* The decl's location will mark where it was first
4484 needed. Save that so synthesize method can indicate
4485 where it was needed from, in case of error */
4486 input_location = DECL_SOURCE_LOCATION (decl);
4487 synthesize_method (decl);
4488 pop_from_top_level ();
4489 reconsider = true;
4492 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4493 generate_tls_wrapper (decl);
4495 if (!DECL_SAVED_TREE (decl))
4496 continue;
4498 /* We lie to the back end, pretending that some functions
4499 are not defined when they really are. This keeps these
4500 functions from being put out unnecessarily. But, we must
4501 stop lying when the functions are referenced, or if they
4502 are not comdat since they need to be put out now. If
4503 DECL_INTERFACE_KNOWN, then we have already set
4504 DECL_EXTERNAL appropriately, so there's no need to check
4505 again, and we do not want to clear DECL_EXTERNAL if a
4506 previous call to import_export_decl set it.
4508 This is done in a separate for cycle, because if some
4509 deferred function is contained in another deferred
4510 function later in deferred_fns varray,
4511 rest_of_compilation would skip this function and we
4512 really cannot expand the same function twice. */
4513 import_export_decl (decl);
4514 if (DECL_NOT_REALLY_EXTERN (decl)
4515 && DECL_INITIAL (decl)
4516 && decl_needed_p (decl))
4518 struct cgraph_node *node, *next;
4520 node = cgraph_get_node (decl);
4521 if (node->cpp_implicit_alias)
4522 node = cgraph_alias_target (node);
4524 cgraph_for_node_and_aliases (node, clear_decl_external,
4525 NULL, true);
4526 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4527 group, we need to mark all symbols in the same comdat group
4528 that way. */
4529 if (node->same_comdat_group)
4530 for (next = cgraph (node->same_comdat_group);
4531 next != node;
4532 next = cgraph (next->same_comdat_group))
4533 cgraph_for_node_and_aliases (next, clear_decl_external,
4534 NULL, true);
4537 /* If we're going to need to write this function out, and
4538 there's already a body for it, create RTL for it now.
4539 (There might be no body if this is a method we haven't
4540 gotten around to synthesizing yet.) */
4541 if (!DECL_EXTERNAL (decl)
4542 && decl_needed_p (decl)
4543 && !TREE_ASM_WRITTEN (decl)
4544 && !cgraph_get_node (decl)->definition)
4546 /* We will output the function; no longer consider it in this
4547 loop. */
4548 DECL_DEFER_OUTPUT (decl) = 0;
4549 /* Generate RTL for this function now that we know we
4550 need it. */
4551 expand_or_defer_fn (decl);
4552 /* If we're compiling -fsyntax-only pretend that this
4553 function has been written out so that we don't try to
4554 expand it again. */
4555 if (flag_syntax_only)
4556 TREE_ASM_WRITTEN (decl) = 1;
4557 reconsider = true;
4561 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4562 reconsider = true;
4564 /* Static data members are just like namespace-scope globals. */
4565 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4567 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4568 /* Don't write it out if we haven't seen a definition. */
4569 || DECL_IN_AGGR_P (decl))
4570 continue;
4571 import_export_decl (decl);
4572 /* If this static data member is needed, provide it to the
4573 back end. */
4574 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4575 DECL_EXTERNAL (decl) = 0;
4577 if (vec_safe_length (pending_statics) != 0
4578 && wrapup_global_declarations (pending_statics->address (),
4579 pending_statics->length ()))
4580 reconsider = true;
4582 if (flag_use_all_virtuals)
4584 if (!reconsider && !mark_all_virtuals)
4586 mark_all_virtuals = true;
4587 reconsider = true;
4588 errs = errorcount + sorrycount;
4590 else if (mark_all_virtuals
4591 && !explained_devirt
4592 && (errorcount + sorrycount > errs))
4594 inform (global_dc->last_location, "this error is seen due to "
4595 "instantiation of all virtual functions, which the C++ "
4596 "standard says are always considered used; this is done "
4597 "to support devirtualization optimizations, but can be "
4598 "disabled with -fno-use-all-virtuals");
4599 explained_devirt = true;
4603 retries++;
4605 while (reconsider);
4607 /* All used inline functions must have a definition at this point. */
4608 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4610 if (/* Check online inline functions that were actually used. */
4611 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4612 /* If the definition actually was available here, then the
4613 fact that the function was not defined merely represents
4614 that for some reason (use of a template repository,
4615 #pragma interface, etc.) we decided not to emit the
4616 definition here. */
4617 && !DECL_INITIAL (decl)
4618 /* Don't complain if the template was defined. */
4619 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4620 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4621 (template_for_substitution (decl)))))
4623 warning (0, "inline function %q+D used but never defined", decl);
4624 /* Avoid a duplicate warning from check_global_declaration_1. */
4625 TREE_NO_WARNING (decl) = 1;
4629 /* So must decls that use a type with no linkage. */
4630 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4631 no_linkage_error (decl);
4633 /* Then, do the Objective-C stuff. This is where all the
4634 Objective-C module stuff gets generated (symtab,
4635 class/protocol/selector lists etc). This must be done after C++
4636 templates, destructors etc. so that selectors used in C++
4637 templates are properly allocated. */
4638 if (c_dialect_objc ())
4639 objc_write_global_declarations ();
4641 /* We give C linkage to static constructors and destructors. */
4642 push_lang_context (lang_name_c);
4644 /* Generate initialization and destruction functions for all
4645 priorities for which they are required. */
4646 if (priority_info_map)
4647 splay_tree_foreach (priority_info_map,
4648 generate_ctor_and_dtor_functions_for_priority,
4649 /*data=*/&locus);
4650 else if (c_dialect_objc () && objc_static_init_needed_p ())
4651 /* If this is obj-c++ and we need a static init, call
4652 generate_ctor_or_dtor_function. */
4653 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4654 DEFAULT_INIT_PRIORITY, &locus);
4656 /* We're done with the splay-tree now. */
4657 if (priority_info_map)
4658 splay_tree_delete (priority_info_map);
4660 /* Generate any missing aliases. */
4661 maybe_apply_pending_pragma_weaks ();
4663 /* We're done with static constructors, so we can go back to "C++"
4664 linkage now. */
4665 pop_lang_context ();
4667 /* Collect candidates for Java hidden aliases. */
4668 candidates = collect_candidates_for_java_method_aliases ();
4670 timevar_stop (TV_PHASE_DEFERRED);
4671 timevar_start (TV_PHASE_OPT_GEN);
4673 if (flag_vtable_verify)
4675 vtv_recover_class_info ();
4676 vtv_compute_class_hierarchy_transitive_closure ();
4677 vtv_build_vtable_verify_fndecl ();
4680 finalize_compilation_unit ();
4682 if (flag_vtable_verify)
4684 /* Generate the special constructor initialization function that
4685 calls __VLTRegisterPairs, and give it a very high
4686 initialization priority. This must be done after
4687 finalize_compilation_unit so that we have accurate
4688 information about which vtable will actually be emitted. */
4689 vtv_generate_init_routine ();
4692 timevar_stop (TV_PHASE_OPT_GEN);
4693 timevar_start (TV_PHASE_CHECK_DBGINFO);
4695 /* Now, issue warnings about static, but not defined, functions,
4696 etc., and emit debugging information. */
4697 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4698 if (vec_safe_length (pending_statics) != 0)
4700 check_global_declarations (pending_statics->address (),
4701 pending_statics->length ());
4702 emit_debug_global_declarations (pending_statics->address (),
4703 pending_statics->length ());
4706 perform_deferred_noexcept_checks ();
4708 /* Generate hidden aliases for Java. */
4709 if (candidates)
4711 build_java_method_aliases (candidates);
4712 pointer_set_destroy (candidates);
4715 finish_repo ();
4717 /* The entire file is now complete. If requested, dump everything
4718 to a file. */
4719 dump_tu ();
4721 if (flag_detailed_statistics)
4723 dump_tree_statistics ();
4724 dump_time_statistics ();
4726 input_location = locus;
4728 #ifdef ENABLE_CHECKING
4729 validate_conversion_obstack ();
4730 #endif /* ENABLE_CHECKING */
4732 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4735 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4736 function to call in parse-tree form; it has not yet been
4737 semantically analyzed. ARGS are the arguments to the function.
4738 They have already been semantically analyzed. This may change
4739 ARGS. */
4741 tree
4742 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4743 tsubst_flags_t complain)
4745 tree orig_fn;
4746 vec<tree, va_gc> *orig_args = NULL;
4747 tree expr;
4748 tree object;
4750 orig_fn = fn;
4751 object = TREE_OPERAND (fn, 0);
4753 if (processing_template_decl)
4755 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4756 || TREE_CODE (fn) == MEMBER_REF);
4757 if (type_dependent_expression_p (fn)
4758 || any_type_dependent_arguments_p (*args))
4759 return build_nt_call_vec (fn, *args);
4761 orig_args = make_tree_vector_copy (*args);
4763 /* Transform the arguments and add the implicit "this"
4764 parameter. That must be done before the FN is transformed
4765 because we depend on the form of FN. */
4766 make_args_non_dependent (*args);
4767 object = build_non_dependent_expr (object);
4768 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4770 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4771 object = cp_build_addr_expr (object, complain);
4772 vec_safe_insert (*args, 0, object);
4774 /* Now that the arguments are done, transform FN. */
4775 fn = build_non_dependent_expr (fn);
4778 /* A qualified name corresponding to a bound pointer-to-member is
4779 represented as an OFFSET_REF:
4781 struct B { void g(); };
4782 void (B::*p)();
4783 void B::g() { (this->*p)(); } */
4784 if (TREE_CODE (fn) == OFFSET_REF)
4786 tree object_addr = cp_build_addr_expr (object, complain);
4787 fn = TREE_OPERAND (fn, 1);
4788 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4789 complain);
4790 vec_safe_insert (*args, 0, object_addr);
4793 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4794 expr = build_op_call (fn, args, complain);
4795 else
4796 expr = cp_build_function_call_vec (fn, args, complain);
4797 if (processing_template_decl && expr != error_mark_node)
4798 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4800 if (orig_args != NULL)
4801 release_tree_vector (orig_args);
4803 return expr;
4807 void
4808 check_default_args (tree x)
4810 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4811 bool saw_def = false;
4812 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4813 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4815 if (TREE_PURPOSE (arg))
4816 saw_def = true;
4817 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4819 error ("default argument missing for parameter %P of %q+#D", i, x);
4820 TREE_PURPOSE (arg) = error_mark_node;
4825 /* Return true if function DECL can be inlined. This is used to force
4826 instantiation of methods that might be interesting for inlining. */
4827 bool
4828 possibly_inlined_p (tree decl)
4830 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4831 if (DECL_UNINLINABLE (decl))
4832 return false;
4833 if (!optimize || pragma_java_exceptions)
4834 return DECL_DECLARED_INLINE_P (decl);
4835 /* When optimizing, we might inline everything when flatten
4836 attribute or heuristics inlining for size or autoinlining
4837 is used. */
4838 return true;
4841 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4842 If DECL is a specialization or implicitly declared class member,
4843 generate the actual definition. Return false if something goes
4844 wrong, true otherwise. */
4846 bool
4847 mark_used (tree decl, tsubst_flags_t complain)
4849 /* If DECL is a BASELINK for a single function, then treat it just
4850 like the DECL for the function. Otherwise, if the BASELINK is
4851 for an overloaded function, we don't know which function was
4852 actually used until after overload resolution. */
4853 if (BASELINK_P (decl))
4855 decl = BASELINK_FUNCTIONS (decl);
4856 if (really_overloaded_fn (decl))
4857 return true;
4858 decl = OVL_CURRENT (decl);
4861 /* Set TREE_USED for the benefit of -Wunused. */
4862 TREE_USED (decl) = 1;
4863 if (DECL_CLONED_FUNCTION_P (decl))
4864 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4866 /* Mark enumeration types as used. */
4867 if (TREE_CODE (decl) == CONST_DECL)
4868 used_types_insert (DECL_CONTEXT (decl));
4870 if (TREE_CODE (decl) == FUNCTION_DECL)
4871 maybe_instantiate_noexcept (decl);
4873 if (TREE_CODE (decl) == FUNCTION_DECL
4874 && DECL_DELETED_FN (decl))
4876 if (DECL_ARTIFICIAL (decl))
4878 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4879 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4881 /* We mark a lambda conversion op as deleted if we can't
4882 generate it properly; see maybe_add_lambda_conv_op. */
4883 sorry ("converting lambda which uses %<...%> to "
4884 "function pointer");
4885 return false;
4888 if (complain & tf_error)
4890 error ("use of deleted function %qD", decl);
4891 if (!maybe_explain_implicit_delete (decl))
4892 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4894 return false;
4897 /* We can only check DECL_ODR_USED on variables or functions with
4898 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4899 might need special handling for. */
4900 if (!VAR_OR_FUNCTION_DECL_P (decl)
4901 || DECL_LANG_SPECIFIC (decl) == NULL
4902 || DECL_THUNK_P (decl))
4904 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4906 if (complain & tf_error)
4907 error ("use of %qD before deduction of %<auto%>", decl);
4908 return false;
4910 return true;
4913 /* We only want to do this processing once. We don't need to keep trying
4914 to instantiate inline templates, because unit-at-a-time will make sure
4915 we get them compiled before functions that want to inline them. */
4916 if (DECL_ODR_USED (decl))
4917 return true;
4919 /* If within finish_function, defer the rest until that function
4920 finishes, otherwise it might recurse. */
4921 if (defer_mark_used_calls)
4923 vec_safe_push (deferred_mark_used_calls, decl);
4924 return true;
4927 /* Normally, we can wait until instantiation-time to synthesize DECL.
4928 However, if DECL is a static data member initialized with a constant
4929 or a constexpr function, we need it right now because a reference to
4930 such a data member or a call to such function is not value-dependent.
4931 For a function that uses auto in the return type, we need to instantiate
4932 it to find out its type. For OpenMP user defined reductions, we need
4933 them instantiated for reduction clauses which inline them by hand
4934 directly. */
4935 if (DECL_LANG_SPECIFIC (decl)
4936 && DECL_TEMPLATE_INFO (decl)
4937 && (decl_maybe_constant_var_p (decl)
4938 || (TREE_CODE (decl) == FUNCTION_DECL
4939 && (DECL_DECLARED_CONSTEXPR_P (decl)
4940 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4941 || undeduced_auto_decl (decl))
4942 && !uses_template_parms (DECL_TI_ARGS (decl)))
4944 /* Instantiating a function will result in garbage collection. We
4945 must treat this situation as if we were within the body of a
4946 function so as to avoid collecting live data only referenced from
4947 the stack (such as overload resolution candidates). */
4948 ++function_depth;
4949 instantiate_decl (decl, /*defer_ok=*/false,
4950 /*expl_inst_class_mem_p=*/false);
4951 --function_depth;
4954 if (processing_template_decl)
4955 return true;
4957 /* Check this too in case we're within fold_non_dependent_expr. */
4958 if (DECL_TEMPLATE_INFO (decl)
4959 && uses_template_parms (DECL_TI_ARGS (decl)))
4960 return true;
4962 require_deduced_type (decl);
4964 /* If we don't need a value, then we don't need to synthesize DECL. */
4965 if (cp_unevaluated_operand != 0)
4966 return true;
4968 DECL_ODR_USED (decl) = 1;
4969 if (DECL_CLONED_FUNCTION_P (decl))
4970 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4972 /* DR 757: A type without linkage shall not be used as the type of a
4973 variable or function with linkage, unless
4974 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4975 o the variable or function is not used (3.2 [basic.def.odr]) or is
4976 defined in the same translation unit. */
4977 if (cxx_dialect > cxx98
4978 && decl_linkage (decl) != lk_none
4979 && !DECL_EXTERN_C_P (decl)
4980 && !DECL_ARTIFICIAL (decl)
4981 && !decl_defined_p (decl)
4982 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4984 if (is_local_extern (decl))
4985 /* There's no way to define a local extern, and adding it to
4986 the vector interferes with GC, so give an error now. */
4987 no_linkage_error (decl);
4988 else
4989 vec_safe_push (no_linkage_decls, decl);
4992 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4993 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4994 /* Remember it, so we can check it was defined. */
4995 note_vague_linkage_fn (decl);
4997 /* Is it a synthesized method that needs to be synthesized? */
4998 if (TREE_CODE (decl) == FUNCTION_DECL
4999 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5000 && DECL_DEFAULTED_FN (decl)
5001 /* A function defaulted outside the class is synthesized either by
5002 cp_finish_decl or instantiate_decl. */
5003 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5004 && ! DECL_INITIAL (decl))
5006 /* Defer virtual destructors so that thunks get the right
5007 linkage. */
5008 if (DECL_VIRTUAL_P (decl) && !at_eof)
5010 note_vague_linkage_fn (decl);
5011 return true;
5014 /* Remember the current location for a function we will end up
5015 synthesizing. Then we can inform the user where it was
5016 required in the case of error. */
5017 DECL_SOURCE_LOCATION (decl) = input_location;
5019 /* Synthesizing an implicitly defined member function will result in
5020 garbage collection. We must treat this situation as if we were
5021 within the body of a function so as to avoid collecting live data
5022 on the stack (such as overload resolution candidates).
5024 We could just let cp_write_global_declarations handle synthesizing
5025 this function by adding it to deferred_fns, but doing
5026 it at the use site produces better error messages. */
5027 ++function_depth;
5028 synthesize_method (decl);
5029 --function_depth;
5030 /* If this is a synthesized method we don't need to
5031 do the instantiation test below. */
5033 else if (VAR_OR_FUNCTION_DECL_P (decl)
5034 && DECL_TEMPLATE_INFO (decl)
5035 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5036 || always_instantiate_p (decl)))
5037 /* If this is a function or variable that is an instance of some
5038 template, we now know that we will need to actually do the
5039 instantiation. We check that DECL is not an explicit
5040 instantiation because that is not checked in instantiate_decl.
5042 We put off instantiating functions in order to improve compile
5043 times. Maintaining a stack of active functions is expensive,
5044 and the inliner knows to instantiate any functions it might
5045 need. Therefore, we always try to defer instantiation. */
5047 ++function_depth;
5048 instantiate_decl (decl, /*defer_ok=*/true,
5049 /*expl_inst_class_mem_p=*/false);
5050 --function_depth;
5053 return true;
5056 bool
5057 mark_used (tree decl)
5059 return mark_used (decl, tf_warning_or_error);
5062 tree
5063 vtv_start_verification_constructor_init_function (void)
5065 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5068 tree
5069 vtv_finish_verification_constructor_init_function (tree function_body)
5071 tree fn;
5073 finish_compound_stmt (function_body);
5074 fn = finish_function (0);
5075 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5076 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5078 return fn;
5081 #include "gt-cp-decl2.h"