2014-07-30 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / cp / decl2.c
blob8fa31451c6064ffdb6cc7d24498a7e4ef4bd7871
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::get_create (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::get_create (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_node::get_create (decl);
1932 node->forced_by_abi = true;
1934 else if (TREE_CODE (decl) == VAR_DECL)
1936 varpool_node *node = varpool_node::get_create (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 && !DECL_ODR_USED (primary_vtbl))
2019 /* Make sure virtual functions get instantiated/synthesized so that
2020 they can be inlined after devirtualization even if the vtable is
2021 never emitted. */
2022 mark_used (primary_vtbl);
2023 mark_vtable_entries (primary_vtbl);
2024 return true;
2026 return false;
2029 /* The ABI requires that we emit all of the vtables if we emit any
2030 of them. */
2031 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2033 /* Mark entities references from the virtual table as used. */
2034 mark_vtable_entries (vtbl);
2036 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2038 vec<tree, va_gc> *cleanups = NULL;
2039 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2040 LOOKUP_NORMAL);
2042 /* It had better be all done at compile-time. */
2043 gcc_assert (!expr && !cleanups);
2046 /* Write it out. */
2047 DECL_EXTERNAL (vtbl) = 0;
2048 rest_of_decl_compilation (vtbl, 1, 1);
2050 /* Because we're only doing syntax-checking, we'll never end up
2051 actually marking the variable as written. */
2052 if (flag_syntax_only)
2053 TREE_ASM_WRITTEN (vtbl) = 1;
2054 else if (DECL_ONE_ONLY (vtbl))
2056 current = varpool_node::get_create (vtbl);
2057 if (last)
2058 current->add_to_same_comdat_group (last);
2059 last = current;
2063 /* Since we're writing out the vtable here, also write the debug
2064 info. */
2065 note_debug_info_needed (ctype);
2067 return true;
2070 /* A special return value from type_visibility meaning internal
2071 linkage. */
2073 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2075 /* walk_tree helper function for type_visibility. */
2077 static tree
2078 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2080 int *vis_p = (int *)data;
2081 if (! TYPE_P (*tp))
2083 *walk_subtrees = 0;
2085 else if (OVERLOAD_TYPE_P (*tp)
2086 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2088 *vis_p = VISIBILITY_ANON;
2089 return *tp;
2091 else if (CLASS_TYPE_P (*tp)
2092 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2093 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2094 return NULL;
2097 /* Returns the visibility of TYPE, which is the minimum visibility of its
2098 component types. */
2100 static int
2101 type_visibility (tree type)
2103 int vis = VISIBILITY_DEFAULT;
2104 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2105 return vis;
2108 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2109 specified (or if VISIBILITY is static). If TMPL is true, this
2110 constraint is for a template argument, and takes precedence
2111 over explicitly-specified visibility on the template. */
2113 static void
2114 constrain_visibility (tree decl, int visibility, bool tmpl)
2116 if (visibility == VISIBILITY_ANON)
2118 /* extern "C" declarations aren't affected by the anonymous
2119 namespace. */
2120 if (!DECL_EXTERN_C_P (decl))
2122 TREE_PUBLIC (decl) = 0;
2123 DECL_WEAK (decl) = 0;
2124 DECL_COMMON (decl) = 0;
2125 if (TREE_CODE (decl) == FUNCTION_DECL
2126 || TREE_CODE (decl) == VAR_DECL)
2128 struct symtab_node *snode = symtab_node::get (decl);
2130 if (snode)
2131 snode->set_comdat_group (NULL);
2133 DECL_INTERFACE_KNOWN (decl) = 1;
2134 if (DECL_LANG_SPECIFIC (decl))
2135 DECL_NOT_REALLY_EXTERN (decl) = 1;
2138 else if (visibility > DECL_VISIBILITY (decl)
2139 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2141 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2142 /* This visibility was not specified. */
2143 DECL_VISIBILITY_SPECIFIED (decl) = false;
2147 /* Constrain the visibility of DECL based on the visibility of its template
2148 arguments. */
2150 static void
2151 constrain_visibility_for_template (tree decl, tree targs)
2153 /* If this is a template instantiation, check the innermost
2154 template args for visibility constraints. The outer template
2155 args are covered by the class check. */
2156 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2157 int i;
2158 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2160 int vis = 0;
2162 tree arg = TREE_VEC_ELT (args, i-1);
2163 if (TYPE_P (arg))
2164 vis = type_visibility (arg);
2165 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2167 STRIP_NOPS (arg);
2168 if (TREE_CODE (arg) == ADDR_EXPR)
2169 arg = TREE_OPERAND (arg, 0);
2170 if (VAR_OR_FUNCTION_DECL_P (arg))
2172 if (! TREE_PUBLIC (arg))
2173 vis = VISIBILITY_ANON;
2174 else
2175 vis = DECL_VISIBILITY (arg);
2178 if (vis)
2179 constrain_visibility (decl, vis, true);
2183 /* Like c_determine_visibility, but with additional C++-specific
2184 behavior.
2186 Function-scope entities can rely on the function's visibility because
2187 it is set in start_preparsed_function.
2189 Class-scope entities cannot rely on the class's visibility until the end
2190 of the enclosing class definition.
2192 Note that because namespaces have multiple independent definitions,
2193 namespace visibility is handled elsewhere using the #pragma visibility
2194 machinery rather than by decorating the namespace declaration.
2196 The goal is for constraints from the type to give a diagnostic, and
2197 other constraints to be applied silently. */
2199 void
2200 determine_visibility (tree decl)
2202 tree class_type = NULL_TREE;
2203 bool use_template;
2204 bool orig_visibility_specified;
2205 enum symbol_visibility orig_visibility;
2207 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2209 /* Only relevant for names with external linkage. */
2210 if (!TREE_PUBLIC (decl))
2211 return;
2213 /* Cloned constructors and destructors get the same visibility as
2214 the underlying function. That should be set up in
2215 maybe_clone_body. */
2216 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2218 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2219 orig_visibility = DECL_VISIBILITY (decl);
2221 if (TREE_CODE (decl) == TYPE_DECL)
2223 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2224 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2225 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2226 use_template = 1;
2227 else
2228 use_template = 0;
2230 else if (DECL_LANG_SPECIFIC (decl))
2231 use_template = DECL_USE_TEMPLATE (decl);
2232 else
2233 use_template = 0;
2235 /* If DECL is a member of a class, visibility specifiers on the
2236 class can influence the visibility of the DECL. */
2237 if (DECL_CLASS_SCOPE_P (decl))
2238 class_type = DECL_CONTEXT (decl);
2239 else
2241 /* Not a class member. */
2243 /* Virtual tables have DECL_CONTEXT set to their associated class,
2244 so they are automatically handled above. */
2245 gcc_assert (!VAR_P (decl)
2246 || !DECL_VTABLE_OR_VTT_P (decl));
2248 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2250 /* Local statics and classes get the visibility of their
2251 containing function by default, except that
2252 -fvisibility-inlines-hidden doesn't affect them. */
2253 tree fn = DECL_CONTEXT (decl);
2254 if (DECL_VISIBILITY_SPECIFIED (fn))
2256 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2257 DECL_VISIBILITY_SPECIFIED (decl) =
2258 DECL_VISIBILITY_SPECIFIED (fn);
2260 else
2262 if (DECL_CLASS_SCOPE_P (fn))
2263 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2264 else if (determine_hidden_inline (fn))
2266 DECL_VISIBILITY (decl) = default_visibility;
2267 DECL_VISIBILITY_SPECIFIED (decl) =
2268 visibility_options.inpragma;
2270 else
2272 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2273 DECL_VISIBILITY_SPECIFIED (decl) =
2274 DECL_VISIBILITY_SPECIFIED (fn);
2278 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2279 but have no TEMPLATE_INFO, so don't try to check it. */
2280 use_template = 0;
2282 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2283 && flag_visibility_ms_compat)
2285 /* Under -fvisibility-ms-compat, types are visible by default,
2286 even though their contents aren't. */
2287 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2288 int underlying_vis = type_visibility (underlying_type);
2289 if (underlying_vis == VISIBILITY_ANON
2290 || (CLASS_TYPE_P (underlying_type)
2291 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2292 constrain_visibility (decl, underlying_vis, false);
2293 else
2294 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2296 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2298 /* tinfo visibility is based on the type it's for. */
2299 constrain_visibility
2300 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2302 /* Give the target a chance to override the visibility associated
2303 with DECL. */
2304 if (TREE_PUBLIC (decl)
2305 && !DECL_REALLY_EXTERN (decl)
2306 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2307 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2308 targetm.cxx.determine_class_data_visibility (decl);
2310 else if (use_template)
2311 /* Template instantiations and specializations get visibility based
2312 on their template unless they override it with an attribute. */;
2313 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2315 if (determine_hidden_inline (decl))
2316 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2317 else
2319 /* Set default visibility to whatever the user supplied with
2320 #pragma GCC visibility or a namespace visibility attribute. */
2321 DECL_VISIBILITY (decl) = default_visibility;
2322 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2327 if (use_template)
2329 /* If the specialization doesn't specify visibility, use the
2330 visibility from the template. */
2331 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2332 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2333 : DECL_TEMPLATE_INFO (decl));
2334 tree args = TI_ARGS (tinfo);
2335 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2336 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2337 : DECL_ATTRIBUTES (decl));
2339 if (args != error_mark_node)
2341 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2343 if (!DECL_VISIBILITY_SPECIFIED (decl))
2345 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2346 && determine_hidden_inline (decl))
2347 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2348 else
2350 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2351 DECL_VISIBILITY_SPECIFIED (decl)
2352 = DECL_VISIBILITY_SPECIFIED (pattern);
2356 if (args
2357 /* Template argument visibility outweighs #pragma or namespace
2358 visibility, but not an explicit attribute. */
2359 && !lookup_attribute ("visibility", attribs))
2361 int depth = TMPL_ARGS_DEPTH (args);
2362 if (DECL_VISIBILITY_SPECIFIED (decl))
2364 /* A class template member with explicit visibility
2365 overrides the class visibility, so we need to apply
2366 all the levels of template args directly. */
2367 int i;
2368 for (i = 1; i <= depth; ++i)
2370 tree lev = TMPL_ARGS_LEVEL (args, i);
2371 constrain_visibility_for_template (decl, lev);
2374 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2375 /* Limit visibility based on its template arguments. */
2376 constrain_visibility_for_template (decl, args);
2381 if (class_type)
2382 determine_visibility_from_class (decl, class_type);
2384 if (decl_anon_ns_mem_p (decl))
2385 /* Names in an anonymous namespace get internal linkage.
2386 This might change once we implement export. */
2387 constrain_visibility (decl, VISIBILITY_ANON, false);
2388 else if (TREE_CODE (decl) != TYPE_DECL)
2390 /* Propagate anonymity from type to decl. */
2391 int tvis = type_visibility (TREE_TYPE (decl));
2392 if (tvis == VISIBILITY_ANON
2393 || ! DECL_VISIBILITY_SPECIFIED (decl))
2394 constrain_visibility (decl, tvis, false);
2396 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2397 /* DR 757: A type without linkage shall not be used as the type of a
2398 variable or function with linkage, unless
2399 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2400 o the variable or function is not used (3.2 [basic.def.odr]) or is
2401 defined in the same translation unit.
2403 Since non-extern "C" decls need to be defined in the same
2404 translation unit, we can make the type internal. */
2405 constrain_visibility (decl, VISIBILITY_ANON, false);
2407 /* If visibility changed and DECL already has DECL_RTL, ensure
2408 symbol flags are updated. */
2409 if ((DECL_VISIBILITY (decl) != orig_visibility
2410 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2411 && ((VAR_P (decl) && TREE_STATIC (decl))
2412 || TREE_CODE (decl) == FUNCTION_DECL)
2413 && DECL_RTL_SET_P (decl))
2414 make_decl_rtl (decl);
2417 /* By default, static data members and function members receive
2418 the visibility of their containing class. */
2420 static void
2421 determine_visibility_from_class (tree decl, tree class_type)
2423 if (DECL_VISIBILITY_SPECIFIED (decl))
2424 return;
2426 if (determine_hidden_inline (decl))
2427 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2428 else
2430 /* Default to the class visibility. */
2431 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2432 DECL_VISIBILITY_SPECIFIED (decl)
2433 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2436 /* Give the target a chance to override the visibility associated
2437 with DECL. */
2438 if (VAR_P (decl)
2439 && (DECL_TINFO_P (decl)
2440 || (DECL_VTABLE_OR_VTT_P (decl)
2441 /* Construction virtual tables are not exported because
2442 they cannot be referred to from other object files;
2443 their name is not standardized by the ABI. */
2444 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2445 && TREE_PUBLIC (decl)
2446 && !DECL_REALLY_EXTERN (decl)
2447 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2448 targetm.cxx.determine_class_data_visibility (decl);
2451 /* Returns true iff DECL is an inline that should get hidden visibility
2452 because of -fvisibility-inlines-hidden. */
2454 static bool
2455 determine_hidden_inline (tree decl)
2457 return (visibility_options.inlines_hidden
2458 /* Don't do this for inline templates; specializations might not be
2459 inline, and we don't want them to inherit the hidden
2460 visibility. We'll set it here for all inline instantiations. */
2461 && !processing_template_decl
2462 && TREE_CODE (decl) == FUNCTION_DECL
2463 && DECL_DECLARED_INLINE_P (decl)
2464 && (! DECL_LANG_SPECIFIC (decl)
2465 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2468 /* Constrain the visibility of a class TYPE based on the visibility of its
2469 field types. Warn if any fields require lesser visibility. */
2471 void
2472 constrain_class_visibility (tree type)
2474 tree binfo;
2475 tree t;
2476 int i;
2478 int vis = type_visibility (type);
2480 if (vis == VISIBILITY_ANON
2481 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2482 return;
2484 /* Don't warn about visibility if the class has explicit visibility. */
2485 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2486 vis = VISIBILITY_INTERNAL;
2488 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2489 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2491 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2492 int subvis = type_visibility (ftype);
2494 if (subvis == VISIBILITY_ANON)
2496 if (!in_main_input_context ())
2497 warning (0, "\
2498 %qT has a field %qD whose type uses the anonymous namespace",
2499 type, t);
2501 else if (MAYBE_CLASS_TYPE_P (ftype)
2502 && vis < VISIBILITY_HIDDEN
2503 && subvis >= VISIBILITY_HIDDEN)
2504 warning (OPT_Wattributes, "\
2505 %qT declared with greater visibility than the type of its field %qD",
2506 type, t);
2509 binfo = TYPE_BINFO (type);
2510 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2512 int subvis = type_visibility (TREE_TYPE (t));
2514 if (subvis == VISIBILITY_ANON)
2516 if (!in_main_input_context())
2517 warning (0, "\
2518 %qT has a base %qT whose type uses the anonymous namespace",
2519 type, TREE_TYPE (t));
2521 else if (vis < VISIBILITY_HIDDEN
2522 && subvis >= VISIBILITY_HIDDEN)
2523 warning (OPT_Wattributes, "\
2524 %qT declared with greater visibility than its base %qT",
2525 type, TREE_TYPE (t));
2529 /* Functions for adjusting the visibility of a tagged type and its nested
2530 types and declarations when it gets a name for linkage purposes from a
2531 typedef. */
2533 static void bt_reset_linkage_1 (binding_entry, void *);
2534 static void bt_reset_linkage_2 (binding_entry, void *);
2536 /* First reset the visibility of all the types. */
2538 static void
2539 reset_type_linkage_1 (tree type)
2541 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2542 if (CLASS_TYPE_P (type))
2543 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2544 bt_reset_linkage_1, NULL);
2546 static void
2547 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2549 reset_type_linkage_1 (b->type);
2552 /* Then reset the visibility of any static data members or member
2553 functions that use those types. */
2555 static void
2556 reset_decl_linkage (tree decl)
2558 if (TREE_PUBLIC (decl))
2559 return;
2560 if (DECL_CLONED_FUNCTION_P (decl))
2561 return;
2562 TREE_PUBLIC (decl) = true;
2563 DECL_INTERFACE_KNOWN (decl) = false;
2564 determine_visibility (decl);
2565 tentative_decl_linkage (decl);
2567 static void
2568 reset_type_linkage_2 (tree type)
2570 if (CLASS_TYPE_P (type))
2572 if (tree vt = CLASSTYPE_VTABLES (type))
2574 tree name = mangle_vtbl_for_type (type);
2575 DECL_NAME (vt) = name;
2576 SET_DECL_ASSEMBLER_NAME (vt, name);
2577 reset_decl_linkage (vt);
2579 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2581 tree name = mangle_typeinfo_for_type (type);
2582 DECL_NAME (ti) = name;
2583 SET_DECL_ASSEMBLER_NAME (ti, name);
2584 TREE_TYPE (name) = type;
2585 reset_decl_linkage (ti);
2587 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2588 if (TREE_CODE (m) == VAR_DECL)
2589 reset_decl_linkage (m);
2590 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2591 reset_decl_linkage (m);
2592 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2593 bt_reset_linkage_2, NULL);
2596 static void
2597 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2599 reset_type_linkage_2 (b->type);
2601 void
2602 reset_type_linkage (tree type)
2604 reset_type_linkage_1 (type);
2605 reset_type_linkage_2 (type);
2608 /* Set up our initial idea of what the linkage of DECL should be. */
2610 void
2611 tentative_decl_linkage (tree decl)
2613 if (DECL_INTERFACE_KNOWN (decl))
2614 /* We've already made a decision as to how this function will
2615 be handled. */;
2616 else if (vague_linkage_p (decl))
2618 if (TREE_CODE (decl) == FUNCTION_DECL
2619 && decl_defined_p (decl))
2621 DECL_EXTERNAL (decl) = 1;
2622 DECL_NOT_REALLY_EXTERN (decl) = 1;
2623 note_vague_linkage_fn (decl);
2624 /* A non-template inline function with external linkage will
2625 always be COMDAT. As we must eventually determine the
2626 linkage of all functions, and as that causes writes to
2627 the data mapped in from the PCH file, it's advantageous
2628 to mark the functions at this point. */
2629 if (DECL_DECLARED_INLINE_P (decl)
2630 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2631 || DECL_DEFAULTED_FN (decl)))
2633 /* This function must have external linkage, as
2634 otherwise DECL_INTERFACE_KNOWN would have been
2635 set. */
2636 gcc_assert (TREE_PUBLIC (decl));
2637 comdat_linkage (decl);
2638 DECL_INTERFACE_KNOWN (decl) = 1;
2641 else if (TREE_CODE (decl) == VAR_DECL)
2642 maybe_commonize_var (decl);
2646 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2647 for DECL has not already been determined, do so now by setting
2648 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2649 function is called entities with vague linkage whose definitions
2650 are available must have TREE_PUBLIC set.
2652 If this function decides to place DECL in COMDAT, it will set
2653 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2654 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2655 callers defer that decision until it is clear that DECL is actually
2656 required. */
2658 void
2659 import_export_decl (tree decl)
2661 int emit_p;
2662 bool comdat_p;
2663 bool import_p;
2664 tree class_type = NULL_TREE;
2666 if (DECL_INTERFACE_KNOWN (decl))
2667 return;
2669 /* We cannot determine what linkage to give to an entity with vague
2670 linkage until the end of the file. For example, a virtual table
2671 for a class will be defined if and only if the key method is
2672 defined in this translation unit. As a further example, consider
2673 that when compiling a translation unit that uses PCH file with
2674 "-frepo" it would be incorrect to make decisions about what
2675 entities to emit when building the PCH; those decisions must be
2676 delayed until the repository information has been processed. */
2677 gcc_assert (at_eof);
2678 /* Object file linkage for explicit instantiations is handled in
2679 mark_decl_instantiated. For static variables in functions with
2680 vague linkage, maybe_commonize_var is used.
2682 Therefore, the only declarations that should be provided to this
2683 function are those with external linkage that are:
2685 * implicit instantiations of function templates
2687 * inline function
2689 * implicit instantiations of static data members of class
2690 templates
2692 * virtual tables
2694 * typeinfo objects
2696 Furthermore, all entities that reach this point must have a
2697 definition available in this translation unit.
2699 The following assertions check these conditions. */
2700 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2701 /* Any code that creates entities with TREE_PUBLIC cleared should
2702 also set DECL_INTERFACE_KNOWN. */
2703 gcc_assert (TREE_PUBLIC (decl));
2704 if (TREE_CODE (decl) == FUNCTION_DECL)
2705 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2706 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2707 || DECL_DECLARED_INLINE_P (decl));
2708 else
2709 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2710 || DECL_VTABLE_OR_VTT_P (decl)
2711 || DECL_TINFO_P (decl));
2712 /* Check that a definition of DECL is available in this translation
2713 unit. */
2714 gcc_assert (!DECL_REALLY_EXTERN (decl));
2716 /* Assume that DECL will not have COMDAT linkage. */
2717 comdat_p = false;
2718 /* Assume that DECL will not be imported into this translation
2719 unit. */
2720 import_p = false;
2722 /* See if the repository tells us whether or not to emit DECL in
2723 this translation unit. */
2724 emit_p = repo_emit_p (decl);
2725 if (emit_p == 0)
2726 import_p = true;
2727 else if (emit_p == 1)
2729 /* The repository indicates that this entity should be defined
2730 here. Make sure the back end honors that request. */
2731 if (VAR_P (decl))
2732 mark_needed (decl);
2733 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2734 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2736 tree clone;
2737 FOR_EACH_CLONE (clone, decl)
2738 mark_needed (clone);
2740 else
2741 mark_needed (decl);
2742 /* Output the definition as an ordinary strong definition. */
2743 DECL_EXTERNAL (decl) = 0;
2744 DECL_INTERFACE_KNOWN (decl) = 1;
2745 return;
2748 if (import_p)
2749 /* We have already decided what to do with this DECL; there is no
2750 need to check anything further. */
2752 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2754 class_type = DECL_CONTEXT (decl);
2755 import_export_class (class_type);
2756 if (TYPE_FOR_JAVA (class_type))
2757 import_p = true;
2758 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2759 && CLASSTYPE_INTERFACE_ONLY (class_type))
2760 import_p = true;
2761 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2762 && !CLASSTYPE_USE_TEMPLATE (class_type)
2763 && CLASSTYPE_KEY_METHOD (class_type)
2764 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2765 /* The ABI requires that all virtual tables be emitted with
2766 COMDAT linkage. However, on systems where COMDAT symbols
2767 don't show up in the table of contents for a static
2768 archive, or on systems without weak symbols (where we
2769 approximate COMDAT linkage by using internal linkage), the
2770 linker will report errors about undefined symbols because
2771 it will not see the virtual table definition. Therefore,
2772 in the case that we know that the virtual table will be
2773 emitted in only one translation unit, we make the virtual
2774 table an ordinary definition with external linkage. */
2775 DECL_EXTERNAL (decl) = 0;
2776 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2778 /* CLASS_TYPE is being exported from this translation unit,
2779 so DECL should be defined here. */
2780 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2781 /* If a class is declared in a header with the "extern
2782 template" extension, then it will not be instantiated,
2783 even in translation units that would normally require
2784 it. Often such classes are explicitly instantiated in
2785 one translation unit. Therefore, the explicit
2786 instantiation must be made visible to other translation
2787 units. */
2788 DECL_EXTERNAL (decl) = 0;
2789 else
2791 /* The generic C++ ABI says that class data is always
2792 COMDAT, even if there is a key function. Some
2793 variants (e.g., the ARM EABI) says that class data
2794 only has COMDAT linkage if the class data might be
2795 emitted in more than one translation unit. When the
2796 key method can be inline and is inline, we still have
2797 to arrange for comdat even though
2798 class_data_always_comdat is false. */
2799 if (!CLASSTYPE_KEY_METHOD (class_type)
2800 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2801 || targetm.cxx.class_data_always_comdat ())
2803 /* The ABI requires COMDAT linkage. Normally, we
2804 only emit COMDAT things when they are needed;
2805 make sure that we realize that this entity is
2806 indeed needed. */
2807 comdat_p = true;
2808 mark_needed (decl);
2812 else if (!flag_implicit_templates
2813 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2814 import_p = true;
2815 else
2816 comdat_p = true;
2818 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2820 tree type = TREE_TYPE (DECL_NAME (decl));
2821 if (CLASS_TYPE_P (type))
2823 class_type = type;
2824 import_export_class (type);
2825 if (CLASSTYPE_INTERFACE_KNOWN (type)
2826 && TYPE_POLYMORPHIC_P (type)
2827 && CLASSTYPE_INTERFACE_ONLY (type)
2828 /* If -fno-rtti was specified, then we cannot be sure
2829 that RTTI information will be emitted with the
2830 virtual table of the class, so we must emit it
2831 wherever it is used. */
2832 && flag_rtti)
2833 import_p = true;
2834 else
2836 if (CLASSTYPE_INTERFACE_KNOWN (type)
2837 && !CLASSTYPE_INTERFACE_ONLY (type))
2839 comdat_p = (targetm.cxx.class_data_always_comdat ()
2840 || (CLASSTYPE_KEY_METHOD (type)
2841 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2842 mark_needed (decl);
2843 if (!flag_weak)
2845 comdat_p = false;
2846 DECL_EXTERNAL (decl) = 0;
2849 else
2850 comdat_p = true;
2853 else
2854 comdat_p = true;
2856 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2858 /* DECL is an implicit instantiation of a function or static
2859 data member. */
2860 if ((flag_implicit_templates
2861 && !flag_use_repository)
2862 || (flag_implicit_inline_templates
2863 && TREE_CODE (decl) == FUNCTION_DECL
2864 && DECL_DECLARED_INLINE_P (decl)))
2865 comdat_p = true;
2866 else
2867 /* If we are not implicitly generating templates, then mark
2868 this entity as undefined in this translation unit. */
2869 import_p = true;
2871 else if (DECL_FUNCTION_MEMBER_P (decl))
2873 if (!DECL_DECLARED_INLINE_P (decl))
2875 tree ctype = DECL_CONTEXT (decl);
2876 import_export_class (ctype);
2877 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2879 DECL_NOT_REALLY_EXTERN (decl)
2880 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2881 || (DECL_DECLARED_INLINE_P (decl)
2882 && ! flag_implement_inlines
2883 && !DECL_VINDEX (decl)));
2885 if (!DECL_NOT_REALLY_EXTERN (decl))
2886 DECL_EXTERNAL (decl) = 1;
2888 /* Always make artificials weak. */
2889 if (DECL_ARTIFICIAL (decl) && flag_weak)
2890 comdat_p = true;
2891 else
2892 maybe_make_one_only (decl);
2895 else
2896 comdat_p = true;
2898 else
2899 comdat_p = true;
2901 if (import_p)
2903 /* If we are importing DECL into this translation unit, mark is
2904 an undefined here. */
2905 DECL_EXTERNAL (decl) = 1;
2906 DECL_NOT_REALLY_EXTERN (decl) = 0;
2908 else if (comdat_p)
2910 /* If we decided to put DECL in COMDAT, mark it accordingly at
2911 this point. */
2912 comdat_linkage (decl);
2915 DECL_INTERFACE_KNOWN (decl) = 1;
2918 /* Return an expression that performs the destruction of DECL, which
2919 must be a VAR_DECL whose type has a non-trivial destructor, or is
2920 an array whose (innermost) elements have a non-trivial destructor. */
2922 tree
2923 build_cleanup (tree decl)
2925 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2926 gcc_assert (clean != NULL_TREE);
2927 return clean;
2930 /* Returns the initialization guard variable for the variable DECL,
2931 which has static storage duration. */
2933 tree
2934 get_guard (tree decl)
2936 tree sname;
2937 tree guard;
2939 sname = mangle_guard_variable (decl);
2940 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2941 if (! guard)
2943 tree guard_type;
2945 /* We use a type that is big enough to contain a mutex as well
2946 as an integer counter. */
2947 guard_type = targetm.cxx.guard_type ();
2948 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2949 VAR_DECL, sname, guard_type);
2951 /* The guard should have the same linkage as what it guards. */
2952 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2953 TREE_STATIC (guard) = TREE_STATIC (decl);
2954 DECL_COMMON (guard) = DECL_COMMON (decl);
2955 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2956 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2957 if (DECL_ONE_ONLY (decl))
2958 make_decl_one_only (guard, cxx_comdat_group (guard));
2959 if (TREE_PUBLIC (decl))
2960 DECL_WEAK (guard) = DECL_WEAK (decl);
2961 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2962 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2964 DECL_ARTIFICIAL (guard) = 1;
2965 DECL_IGNORED_P (guard) = 1;
2966 TREE_USED (guard) = 1;
2967 pushdecl_top_level_and_finish (guard, NULL_TREE);
2969 return guard;
2972 /* Return those bits of the GUARD variable that should be set when the
2973 guarded entity is actually initialized. */
2975 static tree
2976 get_guard_bits (tree guard)
2978 if (!targetm.cxx.guard_mask_bit ())
2980 /* We only set the first byte of the guard, in order to leave room
2981 for a mutex in the high-order bits. */
2982 guard = build1 (ADDR_EXPR,
2983 build_pointer_type (TREE_TYPE (guard)),
2984 guard);
2985 guard = build1 (NOP_EXPR,
2986 build_pointer_type (char_type_node),
2987 guard);
2988 guard = build1 (INDIRECT_REF, char_type_node, guard);
2991 return guard;
2994 /* Return an expression which determines whether or not the GUARD
2995 variable has already been initialized. */
2997 tree
2998 get_guard_cond (tree guard)
3000 tree guard_value;
3002 /* Check to see if the GUARD is zero. */
3003 guard = get_guard_bits (guard);
3005 /* Mask off all but the low bit. */
3006 if (targetm.cxx.guard_mask_bit ())
3008 guard_value = integer_one_node;
3009 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3010 guard_value = convert (TREE_TYPE (guard), guard_value);
3011 guard = cp_build_binary_op (input_location,
3012 BIT_AND_EXPR, guard, guard_value,
3013 tf_warning_or_error);
3016 guard_value = integer_zero_node;
3017 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3018 guard_value = convert (TREE_TYPE (guard), guard_value);
3019 return cp_build_binary_op (input_location,
3020 EQ_EXPR, guard, guard_value,
3021 tf_warning_or_error);
3024 /* Return an expression which sets the GUARD variable, indicating that
3025 the variable being guarded has been initialized. */
3027 tree
3028 set_guard (tree guard)
3030 tree guard_init;
3032 /* Set the GUARD to one. */
3033 guard = get_guard_bits (guard);
3034 guard_init = integer_one_node;
3035 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3036 guard_init = convert (TREE_TYPE (guard), guard_init);
3037 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3038 tf_warning_or_error);
3041 /* Returns true iff we can tell that VAR does not have a dynamic
3042 initializer. */
3044 static bool
3045 var_defined_without_dynamic_init (tree var)
3047 /* If it's defined in another TU, we can't tell. */
3048 if (DECL_EXTERNAL (var))
3049 return false;
3050 /* If it has a non-trivial destructor, registering the destructor
3051 counts as dynamic initialization. */
3052 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3053 return false;
3054 /* If it's in this TU, its initializer has been processed. */
3055 gcc_assert (DECL_INITIALIZED_P (var));
3056 /* If it has no initializer or a constant one, it's not dynamic. */
3057 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3058 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3061 /* Returns true iff VAR is a variable that needs uses to be
3062 wrapped for possible dynamic initialization. */
3064 static bool
3065 var_needs_tls_wrapper (tree var)
3067 return (!error_operand_p (var)
3068 && DECL_THREAD_LOCAL_P (var)
3069 && !DECL_GNU_TLS_P (var)
3070 && !DECL_FUNCTION_SCOPE_P (var)
3071 && !var_defined_without_dynamic_init (var));
3074 /* Get the FUNCTION_DECL for the shared TLS init function for this
3075 translation unit. */
3077 static tree
3078 get_local_tls_init_fn (void)
3080 tree sname = get_identifier ("__tls_init");
3081 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3082 if (!fn)
3084 fn = build_lang_decl (FUNCTION_DECL, sname,
3085 build_function_type (void_type_node,
3086 void_list_node));
3087 SET_DECL_LANGUAGE (fn, lang_c);
3088 TREE_PUBLIC (fn) = false;
3089 DECL_ARTIFICIAL (fn) = true;
3090 mark_used (fn);
3091 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3093 return fn;
3096 /* Get a FUNCTION_DECL for the init function for the thread_local
3097 variable VAR. The init function will be an alias to the function
3098 that initializes all the non-local TLS variables in the translation
3099 unit. The init function is only used by the wrapper function. */
3101 static tree
3102 get_tls_init_fn (tree var)
3104 /* Only C++11 TLS vars need this init fn. */
3105 if (!var_needs_tls_wrapper (var))
3106 return NULL_TREE;
3108 /* If -fno-extern-tls-init, assume that we don't need to call
3109 a tls init function for a variable defined in another TU. */
3110 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3111 return NULL_TREE;
3113 #ifdef ASM_OUTPUT_DEF
3114 /* If the variable is internal, or if we can't generate aliases,
3115 call the local init function directly. */
3116 if (!TREE_PUBLIC (var))
3117 #endif
3118 return get_local_tls_init_fn ();
3120 tree sname = mangle_tls_init_fn (var);
3121 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3122 if (!fn)
3124 fn = build_lang_decl (FUNCTION_DECL, sname,
3125 build_function_type (void_type_node,
3126 void_list_node));
3127 SET_DECL_LANGUAGE (fn, lang_c);
3128 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3129 DECL_ARTIFICIAL (fn) = true;
3130 DECL_COMDAT (fn) = DECL_COMDAT (var);
3131 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3132 if (DECL_ONE_ONLY (var))
3133 make_decl_one_only (fn, cxx_comdat_group (fn));
3134 if (TREE_PUBLIC (var))
3136 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3137 /* If the variable is defined somewhere else and might have static
3138 initialization, make the init function a weak reference. */
3139 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3140 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3141 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3142 && DECL_EXTERNAL (var))
3143 declare_weak (fn);
3144 else
3145 DECL_WEAK (fn) = DECL_WEAK (var);
3147 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3148 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3149 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3150 DECL_IGNORED_P (fn) = 1;
3151 mark_used (fn);
3153 DECL_BEFRIENDING_CLASSES (fn) = var;
3155 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3157 return fn;
3160 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3161 variable VAR. The wrapper function calls the init function (if any) for
3162 VAR and then returns a reference to VAR. The wrapper function is used
3163 in place of VAR everywhere VAR is mentioned. */
3165 tree
3166 get_tls_wrapper_fn (tree var)
3168 /* Only C++11 TLS vars need this wrapper fn. */
3169 if (!var_needs_tls_wrapper (var))
3170 return NULL_TREE;
3172 tree sname = mangle_tls_wrapper_fn (var);
3173 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3174 if (!fn)
3176 /* A named rvalue reference is an lvalue, so the wrapper should
3177 always return an lvalue reference. */
3178 tree type = non_reference (TREE_TYPE (var));
3179 type = build_reference_type (type);
3180 tree fntype = build_function_type (type, void_list_node);
3181 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3182 SET_DECL_LANGUAGE (fn, lang_c);
3183 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3184 DECL_ARTIFICIAL (fn) = true;
3185 DECL_IGNORED_P (fn) = 1;
3186 /* The wrapper is inline and emitted everywhere var is used. */
3187 DECL_DECLARED_INLINE_P (fn) = true;
3188 if (TREE_PUBLIC (var))
3190 comdat_linkage (fn);
3191 #ifdef HAVE_GAS_HIDDEN
3192 /* Make the wrapper bind locally; there's no reason to share
3193 the wrapper between multiple shared objects. */
3194 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3195 DECL_VISIBILITY_SPECIFIED (fn) = true;
3196 #endif
3198 if (!TREE_PUBLIC (fn))
3199 DECL_INTERFACE_KNOWN (fn) = true;
3200 mark_used (fn);
3201 note_vague_linkage_fn (fn);
3203 #if 0
3204 /* We want CSE to commonize calls to the wrapper, but marking it as
3205 pure is unsafe since it has side-effects. I guess we need a new
3206 ECF flag even weaker than ECF_PURE. FIXME! */
3207 DECL_PURE_P (fn) = true;
3208 #endif
3210 DECL_BEFRIENDING_CLASSES (fn) = var;
3212 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3214 return fn;
3217 /* At EOF, generate the definition for the TLS wrapper function FN:
3219 T& var_wrapper() {
3220 if (init_fn) init_fn();
3221 return var;
3222 } */
3224 static void
3225 generate_tls_wrapper (tree fn)
3227 tree var = DECL_BEFRIENDING_CLASSES (fn);
3229 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3230 tree body = begin_function_body ();
3231 /* Only call the init fn if there might be one. */
3232 if (tree init_fn = get_tls_init_fn (var))
3234 tree if_stmt = NULL_TREE;
3235 /* If init_fn is a weakref, make sure it exists before calling. */
3236 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3238 if_stmt = begin_if_stmt ();
3239 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3240 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3241 NE_EXPR, addr, nullptr_node,
3242 tf_warning_or_error);
3243 finish_if_stmt_cond (cond, if_stmt);
3245 finish_expr_stmt (build_cxx_call
3246 (init_fn, 0, NULL, tf_warning_or_error));
3247 if (if_stmt)
3249 finish_then_clause (if_stmt);
3250 finish_if_stmt (if_stmt);
3253 else
3254 /* If there's no initialization, the wrapper is a constant function. */
3255 TREE_READONLY (fn) = true;
3256 finish_return_stmt (convert_from_reference (var));
3257 finish_function_body (body);
3258 expand_or_defer_fn (finish_function (0));
3261 /* Start the process of running a particular set of global constructors
3262 or destructors. Subroutine of do_[cd]tors. Also called from
3263 vtv_start_verification_constructor_init_function. */
3265 static tree
3266 start_objects (int method_type, int initp)
3268 tree body;
3269 tree fndecl;
3270 char type[14];
3272 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3274 if (initp != DEFAULT_INIT_PRIORITY)
3276 char joiner;
3278 #ifdef JOINER
3279 joiner = JOINER;
3280 #else
3281 joiner = '_';
3282 #endif
3284 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3286 else
3287 sprintf (type, "sub_%c", method_type);
3289 fndecl = build_lang_decl (FUNCTION_DECL,
3290 get_file_function_name (type),
3291 build_function_type_list (void_type_node,
3292 NULL_TREE));
3293 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3295 TREE_PUBLIC (current_function_decl) = 0;
3297 /* Mark as artificial because it's not explicitly in the user's
3298 source code. */
3299 DECL_ARTIFICIAL (current_function_decl) = 1;
3301 /* Mark this declaration as used to avoid spurious warnings. */
3302 TREE_USED (current_function_decl) = 1;
3304 /* Mark this function as a global constructor or destructor. */
3305 if (method_type == 'I')
3306 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3307 else
3308 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3310 body = begin_compound_stmt (BCS_FN_BODY);
3312 return body;
3315 /* Finish the process of running a particular set of global constructors
3316 or destructors. Subroutine of do_[cd]tors. */
3318 static void
3319 finish_objects (int method_type, int initp, tree body)
3321 tree fn;
3323 /* Finish up. */
3324 finish_compound_stmt (body);
3325 fn = finish_function (0);
3327 if (method_type == 'I')
3329 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3330 decl_init_priority_insert (fn, initp);
3332 else
3334 DECL_STATIC_DESTRUCTOR (fn) = 1;
3335 decl_fini_priority_insert (fn, initp);
3338 expand_or_defer_fn (fn);
3341 /* The names of the parameters to the function created to handle
3342 initializations and destructions for objects with static storage
3343 duration. */
3344 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3345 #define PRIORITY_IDENTIFIER "__priority"
3347 /* The name of the function we create to handle initializations and
3348 destructions for objects with static storage duration. */
3349 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3351 /* The declaration for the __INITIALIZE_P argument. */
3352 static GTY(()) tree initialize_p_decl;
3354 /* The declaration for the __PRIORITY argument. */
3355 static GTY(()) tree priority_decl;
3357 /* The declaration for the static storage duration function. */
3358 static GTY(()) tree ssdf_decl;
3360 /* All the static storage duration functions created in this
3361 translation unit. */
3362 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3364 /* A map from priority levels to information about that priority
3365 level. There may be many such levels, so efficient lookup is
3366 important. */
3367 static splay_tree priority_info_map;
3369 /* Begins the generation of the function that will handle all
3370 initialization and destruction of objects with static storage
3371 duration. The function generated takes two parameters of type
3372 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3373 nonzero, it performs initializations. Otherwise, it performs
3374 destructions. It only performs those initializations or
3375 destructions with the indicated __PRIORITY. The generated function
3376 returns no value.
3378 It is assumed that this function will only be called once per
3379 translation unit. */
3381 static tree
3382 start_static_storage_duration_function (unsigned count)
3384 tree type;
3385 tree body;
3386 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3388 /* Create the identifier for this function. It will be of the form
3389 SSDF_IDENTIFIER_<number>. */
3390 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3392 type = build_function_type_list (void_type_node,
3393 integer_type_node, integer_type_node,
3394 NULL_TREE);
3396 /* Create the FUNCTION_DECL itself. */
3397 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3398 get_identifier (id),
3399 type);
3400 TREE_PUBLIC (ssdf_decl) = 0;
3401 DECL_ARTIFICIAL (ssdf_decl) = 1;
3403 /* Put this function in the list of functions to be called from the
3404 static constructors and destructors. */
3405 if (!ssdf_decls)
3407 vec_alloc (ssdf_decls, 32);
3409 /* Take this opportunity to initialize the map from priority
3410 numbers to information about that priority level. */
3411 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3412 /*delete_key_fn=*/0,
3413 /*delete_value_fn=*/
3414 (splay_tree_delete_value_fn) &free);
3416 /* We always need to generate functions for the
3417 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3418 priorities later, we'll be sure to find the
3419 DEFAULT_INIT_PRIORITY. */
3420 get_priority_info (DEFAULT_INIT_PRIORITY);
3423 vec_safe_push (ssdf_decls, ssdf_decl);
3425 /* Create the argument list. */
3426 initialize_p_decl = cp_build_parm_decl
3427 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3428 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3429 TREE_USED (initialize_p_decl) = 1;
3430 priority_decl = cp_build_parm_decl
3431 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3432 DECL_CONTEXT (priority_decl) = ssdf_decl;
3433 TREE_USED (priority_decl) = 1;
3435 DECL_CHAIN (initialize_p_decl) = priority_decl;
3436 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3438 /* Put the function in the global scope. */
3439 pushdecl (ssdf_decl);
3441 /* Start the function itself. This is equivalent to declaring the
3442 function as:
3444 static void __ssdf (int __initialize_p, init __priority_p);
3446 It is static because we only need to call this function from the
3447 various constructor and destructor functions for this module. */
3448 start_preparsed_function (ssdf_decl,
3449 /*attrs=*/NULL_TREE,
3450 SF_PRE_PARSED);
3452 /* Set up the scope of the outermost block in the function. */
3453 body = begin_compound_stmt (BCS_FN_BODY);
3455 return body;
3458 /* Finish the generation of the function which performs initialization
3459 and destruction of objects with static storage duration. After
3460 this point, no more such objects can be created. */
3462 static void
3463 finish_static_storage_duration_function (tree body)
3465 /* Close out the function. */
3466 finish_compound_stmt (body);
3467 expand_or_defer_fn (finish_function (0));
3470 /* Return the information about the indicated PRIORITY level. If no
3471 code to handle this level has yet been generated, generate the
3472 appropriate prologue. */
3474 static priority_info
3475 get_priority_info (int priority)
3477 priority_info pi;
3478 splay_tree_node n;
3480 n = splay_tree_lookup (priority_info_map,
3481 (splay_tree_key) priority);
3482 if (!n)
3484 /* Create a new priority information structure, and insert it
3485 into the map. */
3486 pi = XNEW (struct priority_info_s);
3487 pi->initializations_p = 0;
3488 pi->destructions_p = 0;
3489 splay_tree_insert (priority_info_map,
3490 (splay_tree_key) priority,
3491 (splay_tree_value) pi);
3493 else
3494 pi = (priority_info) n->value;
3496 return pi;
3499 /* The effective initialization priority of a DECL. */
3501 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3502 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3503 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3505 /* Whether a DECL needs a guard to protect it against multiple
3506 initialization. */
3508 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3509 || DECL_ONE_ONLY (decl) \
3510 || DECL_WEAK (decl)))
3512 /* Called from one_static_initialization_or_destruction(),
3513 via walk_tree.
3514 Walks the initializer list of a global variable and looks for
3515 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3516 and that have their DECL_CONTEXT() == NULL.
3517 For each such temporary variable, set their DECL_CONTEXT() to
3518 the current function. This is necessary because otherwise
3519 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3520 when trying to refer to a temporary variable that does not have
3521 it's DECL_CONTECT() properly set. */
3522 static tree
3523 fix_temporary_vars_context_r (tree *node,
3524 int * /*unused*/,
3525 void * /*unused1*/)
3527 gcc_assert (current_function_decl);
3529 if (TREE_CODE (*node) == BIND_EXPR)
3531 tree var;
3533 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3534 if (VAR_P (var)
3535 && !DECL_NAME (var)
3536 && DECL_ARTIFICIAL (var)
3537 && !DECL_CONTEXT (var))
3538 DECL_CONTEXT (var) = current_function_decl;
3541 return NULL_TREE;
3544 /* Set up to handle the initialization or destruction of DECL. If
3545 INITP is nonzero, we are initializing the variable. Otherwise, we
3546 are destroying it. */
3548 static void
3549 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3551 tree guard_if_stmt = NULL_TREE;
3552 tree guard;
3554 /* If we are supposed to destruct and there's a trivial destructor,
3555 nothing has to be done. */
3556 if (!initp
3557 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3558 return;
3560 /* Trick the compiler into thinking we are at the file and line
3561 where DECL was declared so that error-messages make sense, and so
3562 that the debugger will show somewhat sensible file and line
3563 information. */
3564 input_location = DECL_SOURCE_LOCATION (decl);
3566 /* Make sure temporary variables in the initialiser all have
3567 their DECL_CONTEXT() set to a value different from NULL_TREE.
3568 This can happen when global variables initialisers are built.
3569 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3570 the temporary variables that might have been generated in the
3571 accompagning initialisers is NULL_TREE, meaning the variables have been
3572 declared in the global namespace.
3573 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3574 of the temporaries are set to the current function decl. */
3575 cp_walk_tree_without_duplicates (&init,
3576 fix_temporary_vars_context_r,
3577 NULL);
3579 /* Because of:
3581 [class.access.spec]
3583 Access control for implicit calls to the constructors,
3584 the conversion functions, or the destructor called to
3585 create and destroy a static data member is performed as
3586 if these calls appeared in the scope of the member's
3587 class.
3589 we pretend we are in a static member function of the class of
3590 which the DECL is a member. */
3591 if (member_p (decl))
3593 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3594 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3597 /* Assume we don't need a guard. */
3598 guard = NULL_TREE;
3599 /* We need a guard if this is an object with external linkage that
3600 might be initialized in more than one place. (For example, a
3601 static data member of a template, when the data member requires
3602 construction.) */
3603 if (NEEDS_GUARD_P (decl))
3605 tree guard_cond;
3607 guard = get_guard (decl);
3609 /* When using __cxa_atexit, we just check the GUARD as we would
3610 for a local static. */
3611 if (flag_use_cxa_atexit)
3613 /* When using __cxa_atexit, we never try to destroy
3614 anything from a static destructor. */
3615 gcc_assert (initp);
3616 guard_cond = get_guard_cond (guard);
3618 /* If we don't have __cxa_atexit, then we will be running
3619 destructors from .fini sections, or their equivalents. So,
3620 we need to know how many times we've tried to initialize this
3621 object. We do initializations only if the GUARD is zero,
3622 i.e., if we are the first to initialize the variable. We do
3623 destructions only if the GUARD is one, i.e., if we are the
3624 last to destroy the variable. */
3625 else if (initp)
3626 guard_cond
3627 = cp_build_binary_op (input_location,
3628 EQ_EXPR,
3629 cp_build_unary_op (PREINCREMENT_EXPR,
3630 guard,
3631 /*noconvert=*/1,
3632 tf_warning_or_error),
3633 integer_one_node,
3634 tf_warning_or_error);
3635 else
3636 guard_cond
3637 = cp_build_binary_op (input_location,
3638 EQ_EXPR,
3639 cp_build_unary_op (PREDECREMENT_EXPR,
3640 guard,
3641 /*noconvert=*/1,
3642 tf_warning_or_error),
3643 integer_zero_node,
3644 tf_warning_or_error);
3646 guard_if_stmt = begin_if_stmt ();
3647 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3651 /* If we're using __cxa_atexit, we have not already set the GUARD,
3652 so we must do so now. */
3653 if (guard && initp && flag_use_cxa_atexit)
3654 finish_expr_stmt (set_guard (guard));
3656 /* Perform the initialization or destruction. */
3657 if (initp)
3659 if (init)
3661 finish_expr_stmt (init);
3662 if (flag_sanitize & SANITIZE_ADDRESS)
3664 varpool_node *vnode = varpool_node::get (decl);
3665 if (vnode)
3666 vnode->dynamically_initialized = 1;
3670 /* If we're using __cxa_atexit, register a function that calls the
3671 destructor for the object. */
3672 if (flag_use_cxa_atexit)
3673 finish_expr_stmt (register_dtor_fn (decl));
3675 else
3676 finish_expr_stmt (build_cleanup (decl));
3678 /* Finish the guard if-stmt, if necessary. */
3679 if (guard)
3681 finish_then_clause (guard_if_stmt);
3682 finish_if_stmt (guard_if_stmt);
3685 /* Now that we're done with DECL we don't need to pretend to be a
3686 member of its class any longer. */
3687 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3688 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3691 /* Generate code to do the initialization or destruction of the decls in VARS,
3692 a TREE_LIST of VAR_DECL with static storage duration.
3693 Whether initialization or destruction is performed is specified by INITP. */
3695 static void
3696 do_static_initialization_or_destruction (tree vars, bool initp)
3698 tree node, init_if_stmt, cond;
3700 /* Build the outer if-stmt to check for initialization or destruction. */
3701 init_if_stmt = begin_if_stmt ();
3702 cond = initp ? integer_one_node : integer_zero_node;
3703 cond = cp_build_binary_op (input_location,
3704 EQ_EXPR,
3705 initialize_p_decl,
3706 cond,
3707 tf_warning_or_error);
3708 finish_if_stmt_cond (cond, init_if_stmt);
3710 /* To make sure dynamic construction doesn't access globals from other
3711 compilation units where they might not be yet constructed, for
3712 -fsanitize=address insert __asan_before_dynamic_init call that
3713 prevents access to either all global variables that need construction
3714 in other compilation units, or at least those that haven't been
3715 initialized yet. Variables that need dynamic construction in
3716 the current compilation unit are kept accessible. */
3717 if (flag_sanitize & SANITIZE_ADDRESS)
3718 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3720 node = vars;
3721 do {
3722 tree decl = TREE_VALUE (node);
3723 tree priority_if_stmt;
3724 int priority;
3725 priority_info pi;
3727 /* If we don't need a destructor, there's nothing to do. Avoid
3728 creating a possibly empty if-stmt. */
3729 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3731 node = TREE_CHAIN (node);
3732 continue;
3735 /* Remember that we had an initialization or finalization at this
3736 priority. */
3737 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3738 pi = get_priority_info (priority);
3739 if (initp)
3740 pi->initializations_p = 1;
3741 else
3742 pi->destructions_p = 1;
3744 /* Conditionalize this initialization on being in the right priority
3745 and being initializing/finalizing appropriately. */
3746 priority_if_stmt = begin_if_stmt ();
3747 cond = cp_build_binary_op (input_location,
3748 EQ_EXPR,
3749 priority_decl,
3750 build_int_cst (NULL_TREE, priority),
3751 tf_warning_or_error);
3752 finish_if_stmt_cond (cond, priority_if_stmt);
3754 /* Process initializers with same priority. */
3755 for (; node
3756 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3757 node = TREE_CHAIN (node))
3758 /* Do one initialization or destruction. */
3759 one_static_initialization_or_destruction (TREE_VALUE (node),
3760 TREE_PURPOSE (node), initp);
3762 /* Finish up the priority if-stmt body. */
3763 finish_then_clause (priority_if_stmt);
3764 finish_if_stmt (priority_if_stmt);
3766 } while (node);
3768 /* Revert what __asan_before_dynamic_init did by calling
3769 __asan_after_dynamic_init. */
3770 if (flag_sanitize & SANITIZE_ADDRESS)
3771 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3773 /* Finish up the init/destruct if-stmt body. */
3774 finish_then_clause (init_if_stmt);
3775 finish_if_stmt (init_if_stmt);
3778 /* VARS is a list of variables with static storage duration which may
3779 need initialization and/or finalization. Remove those variables
3780 that don't really need to be initialized or finalized, and return
3781 the resulting list. The order in which the variables appear in
3782 VARS is in reverse order of the order in which they should actually
3783 be initialized. The list we return is in the unreversed order;
3784 i.e., the first variable should be initialized first. */
3786 static tree
3787 prune_vars_needing_no_initialization (tree *vars)
3789 tree *var = vars;
3790 tree result = NULL_TREE;
3792 while (*var)
3794 tree t = *var;
3795 tree decl = TREE_VALUE (t);
3796 tree init = TREE_PURPOSE (t);
3798 /* Deal gracefully with error. */
3799 if (decl == error_mark_node)
3801 var = &TREE_CHAIN (t);
3802 continue;
3805 /* The only things that can be initialized are variables. */
3806 gcc_assert (VAR_P (decl));
3808 /* If this object is not defined, we don't need to do anything
3809 here. */
3810 if (DECL_EXTERNAL (decl))
3812 var = &TREE_CHAIN (t);
3813 continue;
3816 /* Also, if the initializer already contains errors, we can bail
3817 out now. */
3818 if (init && TREE_CODE (init) == TREE_LIST
3819 && value_member (error_mark_node, init))
3821 var = &TREE_CHAIN (t);
3822 continue;
3825 /* This variable is going to need initialization and/or
3826 finalization, so we add it to the list. */
3827 *var = TREE_CHAIN (t);
3828 TREE_CHAIN (t) = result;
3829 result = t;
3832 return result;
3835 /* Make sure we have told the back end about all the variables in
3836 VARS. */
3838 static void
3839 write_out_vars (tree vars)
3841 tree v;
3843 for (v = vars; v; v = TREE_CHAIN (v))
3845 tree var = TREE_VALUE (v);
3846 if (!var_finalized_p (var))
3848 import_export_decl (var);
3849 rest_of_decl_compilation (var, 1, 1);
3854 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3855 (otherwise) that will initialize all global objects with static
3856 storage duration having the indicated PRIORITY. */
3858 static void
3859 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3860 location_t *locus)
3862 char function_key;
3863 tree fndecl;
3864 tree body;
3865 size_t i;
3867 input_location = *locus;
3868 /* ??? */
3869 /* Was: locus->line++; */
3871 /* We use `I' to indicate initialization and `D' to indicate
3872 destruction. */
3873 function_key = constructor_p ? 'I' : 'D';
3875 /* We emit the function lazily, to avoid generating empty
3876 global constructors and destructors. */
3877 body = NULL_TREE;
3879 /* For Objective-C++, we may need to initialize metadata found in this module.
3880 This must be done _before_ any other static initializations. */
3881 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3882 && constructor_p && objc_static_init_needed_p ())
3884 body = start_objects (function_key, priority);
3885 objc_generate_static_init_call (NULL_TREE);
3888 /* Call the static storage duration function with appropriate
3889 arguments. */
3890 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3892 /* Calls to pure or const functions will expand to nothing. */
3893 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3895 tree call;
3897 if (! body)
3898 body = start_objects (function_key, priority);
3900 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3901 build_int_cst (NULL_TREE,
3902 constructor_p),
3903 build_int_cst (NULL_TREE,
3904 priority),
3905 NULL_TREE);
3906 finish_expr_stmt (call);
3910 /* Close out the function. */
3911 if (body)
3912 finish_objects (function_key, priority, body);
3915 /* Generate constructor and destructor functions for the priority
3916 indicated by N. */
3918 static int
3919 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3921 location_t *locus = (location_t *) data;
3922 int priority = (int) n->key;
3923 priority_info pi = (priority_info) n->value;
3925 /* Generate the functions themselves, but only if they are really
3926 needed. */
3927 if (pi->initializations_p)
3928 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3929 if (pi->destructions_p)
3930 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3932 /* Keep iterating. */
3933 return 0;
3936 /* Java requires that we be able to reference a local address for a
3937 method, and not be confused by PLT entries. If hidden aliases are
3938 supported, collect and return all the functions for which we should
3939 emit a hidden alias. */
3941 static struct pointer_set_t *
3942 collect_candidates_for_java_method_aliases (void)
3944 struct cgraph_node *node;
3945 struct pointer_set_t *candidates = NULL;
3947 #ifndef HAVE_GAS_HIDDEN
3948 return candidates;
3949 #endif
3951 FOR_EACH_FUNCTION (node)
3953 tree fndecl = node->decl;
3955 if (DECL_CLASS_SCOPE_P (fndecl)
3956 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3957 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3959 if (candidates == NULL)
3960 candidates = pointer_set_create ();
3961 pointer_set_insert (candidates, fndecl);
3965 return candidates;
3969 /* Java requires that we be able to reference a local address for a
3970 method, and not be confused by PLT entries. If hidden aliases are
3971 supported, emit one for each java function that we've emitted.
3972 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3973 by collect_candidates_for_java_method_aliases. */
3975 static void
3976 build_java_method_aliases (struct pointer_set_t *candidates)
3978 struct cgraph_node *node;
3980 #ifndef HAVE_GAS_HIDDEN
3981 return;
3982 #endif
3984 FOR_EACH_FUNCTION (node)
3986 tree fndecl = node->decl;
3988 if (TREE_ASM_WRITTEN (fndecl)
3989 && pointer_set_contains (candidates, fndecl))
3991 /* Mangle the name in a predictable way; we need to reference
3992 this from a java compiled object file. */
3993 tree oid, nid, alias;
3994 const char *oname;
3995 char *nname;
3997 oid = DECL_ASSEMBLER_NAME (fndecl);
3998 oname = IDENTIFIER_POINTER (oid);
3999 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4000 nname = ACONCAT (("_ZGA", oname+2, NULL));
4001 nid = get_identifier (nname);
4003 alias = make_alias_for (fndecl, nid);
4004 TREE_PUBLIC (alias) = 1;
4005 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4007 assemble_alias (alias, oid);
4012 /* Return C++ property of T, based on given operation OP. */
4014 static int
4015 cpp_check (tree t, cpp_operation op)
4017 switch (op)
4019 case IS_ABSTRACT:
4020 return DECL_PURE_VIRTUAL_P (t);
4021 case IS_CONSTRUCTOR:
4022 return DECL_CONSTRUCTOR_P (t);
4023 case IS_DESTRUCTOR:
4024 return DECL_DESTRUCTOR_P (t);
4025 case IS_COPY_CONSTRUCTOR:
4026 return DECL_COPY_CONSTRUCTOR_P (t);
4027 case IS_TEMPLATE:
4028 return TREE_CODE (t) == TEMPLATE_DECL;
4029 case IS_TRIVIAL:
4030 return trivial_type_p (t);
4031 default:
4032 return 0;
4036 /* Collect source file references recursively, starting from NAMESPC. */
4038 static void
4039 collect_source_refs (tree namespc)
4041 tree t;
4043 if (!namespc)
4044 return;
4046 /* Iterate over names in this name space. */
4047 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4048 if (!DECL_IS_BUILTIN (t) )
4049 collect_source_ref (DECL_SOURCE_FILE (t));
4051 /* Dump siblings, if any */
4052 collect_source_refs (TREE_CHAIN (namespc));
4054 /* Dump children, if any */
4055 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4058 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4059 starting from NAMESPC. */
4061 static void
4062 collect_ada_namespace (tree namespc, const char *source_file)
4064 if (!namespc)
4065 return;
4067 /* Collect decls from this namespace */
4068 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4070 /* Collect siblings, if any */
4071 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4073 /* Collect children, if any */
4074 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4077 /* Returns true iff there is a definition available for variable or
4078 function DECL. */
4080 static bool
4081 decl_defined_p (tree decl)
4083 if (TREE_CODE (decl) == FUNCTION_DECL)
4084 return (DECL_INITIAL (decl) != NULL_TREE);
4085 else
4087 gcc_assert (VAR_P (decl));
4088 return !DECL_EXTERNAL (decl);
4092 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4094 [expr.const]
4096 An integral constant-expression can only involve ... const
4097 variables of integral or enumeration types initialized with
4098 constant expressions ...
4100 C++0x also allows constexpr variables and temporaries initialized
4101 with constant expressions. We handle the former here, but the latter
4102 are just folded away in cxx_eval_constant_expression.
4104 The standard does not require that the expression be non-volatile.
4105 G++ implements the proposed correction in DR 457. */
4107 bool
4108 decl_constant_var_p (tree decl)
4110 if (!decl_maybe_constant_var_p (decl))
4111 return false;
4113 /* We don't know if a template static data member is initialized with
4114 a constant expression until we instantiate its initializer. Even
4115 in the case of a constexpr variable, we can't treat it as a
4116 constant until its initializer is complete in case it's used in
4117 its own initializer. */
4118 mark_used (decl);
4119 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4122 /* Returns true if DECL could be a symbolic constant variable, depending on
4123 its initializer. */
4125 bool
4126 decl_maybe_constant_var_p (tree decl)
4128 tree type = TREE_TYPE (decl);
4129 if (!VAR_P (decl))
4130 return false;
4131 if (DECL_DECLARED_CONSTEXPR_P (decl))
4132 return true;
4133 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4134 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4137 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4138 called from grokfndecl and grokvardecl; in all modes it is called from
4139 cp_write_global_declarations. */
4141 void
4142 no_linkage_error (tree decl)
4144 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4145 /* In C++11 it's ok if the decl is defined. */
4146 return;
4147 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4148 if (t == NULL_TREE)
4149 /* The type that got us on no_linkage_decls must have gotten a name for
4150 linkage purposes. */;
4151 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4152 /* The type might end up having a typedef name for linkage purposes. */
4153 vec_safe_push (no_linkage_decls, decl);
4154 else if (TYPE_ANONYMOUS_P (t))
4156 bool d = false;
4157 if (cxx_dialect >= cxx11)
4158 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4159 "anonymous type, is used but never defined", decl);
4160 else if (DECL_EXTERN_C_P (decl))
4161 /* Allow this; it's pretty common in C. */;
4162 else if (TREE_CODE (decl) == VAR_DECL)
4163 /* DRs 132, 319 and 389 seem to indicate types with
4164 no linkage can only be used to declare extern "C"
4165 entities. Since it's not always an error in the
4166 ISO C++ 90 Standard, we only issue a warning. */
4167 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4168 "with no linkage used to declare variable %q#D with "
4169 "linkage", decl);
4170 else
4171 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4172 "linkage used to declare function %q#D with linkage",
4173 decl);
4174 if (d && is_typedef_decl (TYPE_NAME (t)))
4175 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4176 "to the unqualified type, so it is not used for linkage",
4177 TYPE_NAME (t));
4179 else if (cxx_dialect >= cxx11)
4180 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4181 "%qT, is used but never defined", decl, t);
4182 else if (TREE_CODE (decl) == VAR_DECL)
4183 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4184 "used to declare variable %q#D with linkage", t, decl);
4185 else
4186 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4187 "to declare function %q#D with linkage", t, decl);
4190 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4192 static void
4193 collect_all_refs (const char *source_file)
4195 collect_ada_namespace (global_namespace, source_file);
4198 /* Clear DECL_EXTERNAL for NODE. */
4200 static bool
4201 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4203 DECL_EXTERNAL (node->decl) = 0;
4204 return false;
4207 /* Build up the function to run dynamic initializers for thread_local
4208 variables in this translation unit and alias the init functions for the
4209 individual variables to it. */
4211 static void
4212 handle_tls_init (void)
4214 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4215 if (vars == NULL_TREE)
4216 return;
4218 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4220 write_out_vars (vars);
4222 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4223 boolean_type_node);
4224 TREE_PUBLIC (guard) = false;
4225 TREE_STATIC (guard) = true;
4226 DECL_ARTIFICIAL (guard) = true;
4227 DECL_IGNORED_P (guard) = true;
4228 TREE_USED (guard) = true;
4229 set_decl_tls_model (guard, decl_default_tls_model (guard));
4230 pushdecl_top_level_and_finish (guard, NULL_TREE);
4232 tree fn = get_local_tls_init_fn ();
4233 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4234 tree body = begin_function_body ();
4235 tree if_stmt = begin_if_stmt ();
4236 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4237 tf_warning_or_error);
4238 finish_if_stmt_cond (cond, if_stmt);
4239 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4240 tf_warning_or_error));
4241 for (; vars; vars = TREE_CHAIN (vars))
4243 tree var = TREE_VALUE (vars);
4244 tree init = TREE_PURPOSE (vars);
4245 one_static_initialization_or_destruction (var, init, true);
4247 #ifdef ASM_OUTPUT_DEF
4248 /* Output init aliases even with -fno-extern-tls-init. */
4249 if (TREE_PUBLIC (var))
4251 tree single_init_fn = get_tls_init_fn (var);
4252 if (single_init_fn == NULL_TREE)
4253 continue;
4254 cgraph_node *alias
4255 = cgraph_node::get_create (fn)->create_same_body_alias
4256 (single_init_fn, fn);
4257 gcc_assert (alias != NULL);
4259 #endif
4262 finish_then_clause (if_stmt);
4263 finish_if_stmt (if_stmt);
4264 finish_function_body (body);
4265 expand_or_defer_fn (finish_function (0));
4268 /* The entire file is now complete. If requested, dump everything
4269 to a file. */
4271 static void
4272 dump_tu (void)
4274 int flags;
4275 FILE *stream = dump_begin (TDI_tu, &flags);
4277 if (stream)
4279 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4280 dump_end (TDI_tu, stream);
4284 /* This routine is called at the end of compilation.
4285 Its job is to create all the code needed to initialize and
4286 destroy the global aggregates. We do the destruction
4287 first, since that way we only need to reverse the decls once. */
4289 void
4290 cp_write_global_declarations (void)
4292 tree vars;
4293 bool reconsider;
4294 size_t i;
4295 location_t locus;
4296 unsigned ssdf_count = 0;
4297 int retries = 0;
4298 tree decl;
4299 struct pointer_set_t *candidates;
4301 locus = input_location;
4302 at_eof = 1;
4304 /* Bad parse errors. Just forget about it. */
4305 if (! global_bindings_p () || current_class_type
4306 || !vec_safe_is_empty (decl_namespace_list))
4307 return;
4309 /* This is the point to write out a PCH if we're doing that.
4310 In that case we do not want to do anything else. */
4311 if (pch_file)
4313 c_common_write_pch ();
4314 dump_tu ();
4315 return;
4318 cgraph_process_same_body_aliases ();
4320 /* Handle -fdump-ada-spec[-slim] */
4321 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4323 if (flag_dump_ada_spec_slim)
4324 collect_source_ref (main_input_filename);
4325 else
4326 collect_source_refs (global_namespace);
4328 dump_ada_specs (collect_all_refs, cpp_check);
4331 /* FIXME - huh? was input_line -= 1;*/
4333 timevar_start (TV_PHASE_DEFERRED);
4335 /* We now have to write out all the stuff we put off writing out.
4336 These include:
4338 o Template specializations that we have not yet instantiated,
4339 but which are needed.
4340 o Initialization and destruction for non-local objects with
4341 static storage duration. (Local objects with static storage
4342 duration are initialized when their scope is first entered,
4343 and are cleaned up via atexit.)
4344 o Virtual function tables.
4346 All of these may cause others to be needed. For example,
4347 instantiating one function may cause another to be needed, and
4348 generating the initializer for an object may cause templates to be
4349 instantiated, etc., etc. */
4351 emit_support_tinfos ();
4352 int errs = errorcount + sorrycount;
4353 bool explained_devirt = false;
4357 tree t;
4358 tree decl;
4360 reconsider = false;
4362 /* If there are templates that we've put off instantiating, do
4363 them now. */
4364 instantiate_pending_templates (retries);
4365 ggc_collect ();
4367 /* Write out virtual tables as required. Note that writing out
4368 the virtual table for a template class may cause the
4369 instantiation of members of that class. If we write out
4370 vtables then we remove the class from our list so we don't
4371 have to look at it again. */
4373 while (keyed_classes != NULL_TREE
4374 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4376 reconsider = true;
4377 keyed_classes = TREE_CHAIN (keyed_classes);
4380 t = keyed_classes;
4381 if (t != NULL_TREE)
4383 tree next = TREE_CHAIN (t);
4385 while (next)
4387 if (maybe_emit_vtables (TREE_VALUE (next)))
4389 reconsider = true;
4390 TREE_CHAIN (t) = TREE_CHAIN (next);
4392 else
4393 t = next;
4395 next = TREE_CHAIN (t);
4399 /* Write out needed type info variables. We have to be careful
4400 looping through unemitted decls, because emit_tinfo_decl may
4401 cause other variables to be needed. New elements will be
4402 appended, and we remove from the vector those that actually
4403 get emitted. */
4404 for (i = unemitted_tinfo_decls->length ();
4405 unemitted_tinfo_decls->iterate (--i, &t);)
4406 if (emit_tinfo_decl (t))
4408 reconsider = true;
4409 unemitted_tinfo_decls->unordered_remove (i);
4412 /* The list of objects with static storage duration is built up
4413 in reverse order. We clear STATIC_AGGREGATES so that any new
4414 aggregates added during the initialization of these will be
4415 initialized in the correct order when we next come around the
4416 loop. */
4417 vars = prune_vars_needing_no_initialization (&static_aggregates);
4419 if (vars)
4421 /* We need to start a new initialization function each time
4422 through the loop. That's because we need to know which
4423 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4424 isn't computed until a function is finished, and written
4425 out. That's a deficiency in the back end. When this is
4426 fixed, these initialization functions could all become
4427 inline, with resulting performance improvements. */
4428 tree ssdf_body;
4430 /* Set the line and file, so that it is obviously not from
4431 the source file. */
4432 input_location = locus;
4433 ssdf_body = start_static_storage_duration_function (ssdf_count);
4435 /* Make sure the back end knows about all the variables. */
4436 write_out_vars (vars);
4438 /* First generate code to do all the initializations. */
4439 if (vars)
4440 do_static_initialization_or_destruction (vars, /*initp=*/true);
4442 /* Then, generate code to do all the destructions. Do these
4443 in reverse order so that the most recently constructed
4444 variable is the first destroyed. If we're using
4445 __cxa_atexit, then we don't need to do this; functions
4446 were registered at initialization time to destroy the
4447 local statics. */
4448 if (!flag_use_cxa_atexit && vars)
4450 vars = nreverse (vars);
4451 do_static_initialization_or_destruction (vars, /*initp=*/false);
4453 else
4454 vars = NULL_TREE;
4456 /* Finish up the static storage duration function for this
4457 round. */
4458 input_location = locus;
4459 finish_static_storage_duration_function (ssdf_body);
4461 /* All those initializations and finalizations might cause
4462 us to need more inline functions, more template
4463 instantiations, etc. */
4464 reconsider = true;
4465 ssdf_count++;
4466 /* ??? was: locus.line++; */
4469 /* Now do the same for thread_local variables. */
4470 handle_tls_init ();
4472 /* Go through the set of inline functions whose bodies have not
4473 been emitted yet. If out-of-line copies of these functions
4474 are required, emit them. */
4475 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4477 /* Does it need synthesizing? */
4478 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4479 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4481 /* Even though we're already at the top-level, we push
4482 there again. That way, when we pop back a few lines
4483 hence, all of our state is restored. Otherwise,
4484 finish_function doesn't clean things up, and we end
4485 up with CURRENT_FUNCTION_DECL set. */
4486 push_to_top_level ();
4487 /* The decl's location will mark where it was first
4488 needed. Save that so synthesize method can indicate
4489 where it was needed from, in case of error */
4490 input_location = DECL_SOURCE_LOCATION (decl);
4491 synthesize_method (decl);
4492 pop_from_top_level ();
4493 reconsider = true;
4496 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4497 generate_tls_wrapper (decl);
4499 if (!DECL_SAVED_TREE (decl))
4500 continue;
4502 /* We lie to the back end, pretending that some functions
4503 are not defined when they really are. This keeps these
4504 functions from being put out unnecessarily. But, we must
4505 stop lying when the functions are referenced, or if they
4506 are not comdat since they need to be put out now. If
4507 DECL_INTERFACE_KNOWN, then we have already set
4508 DECL_EXTERNAL appropriately, so there's no need to check
4509 again, and we do not want to clear DECL_EXTERNAL if a
4510 previous call to import_export_decl set it.
4512 This is done in a separate for cycle, because if some
4513 deferred function is contained in another deferred
4514 function later in deferred_fns varray,
4515 rest_of_compilation would skip this function and we
4516 really cannot expand the same function twice. */
4517 import_export_decl (decl);
4518 if (DECL_NOT_REALLY_EXTERN (decl)
4519 && DECL_INITIAL (decl)
4520 && decl_needed_p (decl))
4522 struct cgraph_node *node, *next;
4524 node = cgraph_node::get (decl);
4525 if (node->cpp_implicit_alias)
4526 node = node->get_alias_target ();
4528 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4529 NULL, true);
4530 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4531 group, we need to mark all symbols in the same comdat group
4532 that way. */
4533 if (node->same_comdat_group)
4534 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4535 next != node;
4536 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4537 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4538 NULL, true);
4541 /* If we're going to need to write this function out, and
4542 there's already a body for it, create RTL for it now.
4543 (There might be no body if this is a method we haven't
4544 gotten around to synthesizing yet.) */
4545 if (!DECL_EXTERNAL (decl)
4546 && decl_needed_p (decl)
4547 && !TREE_ASM_WRITTEN (decl)
4548 && !cgraph_node::get (decl)->definition)
4550 /* We will output the function; no longer consider it in this
4551 loop. */
4552 DECL_DEFER_OUTPUT (decl) = 0;
4553 /* Generate RTL for this function now that we know we
4554 need it. */
4555 expand_or_defer_fn (decl);
4556 /* If we're compiling -fsyntax-only pretend that this
4557 function has been written out so that we don't try to
4558 expand it again. */
4559 if (flag_syntax_only)
4560 TREE_ASM_WRITTEN (decl) = 1;
4561 reconsider = true;
4565 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4566 reconsider = true;
4568 /* Static data members are just like namespace-scope globals. */
4569 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4571 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4572 /* Don't write it out if we haven't seen a definition. */
4573 || DECL_IN_AGGR_P (decl))
4574 continue;
4575 import_export_decl (decl);
4576 /* If this static data member is needed, provide it to the
4577 back end. */
4578 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4579 DECL_EXTERNAL (decl) = 0;
4581 if (vec_safe_length (pending_statics) != 0
4582 && wrapup_global_declarations (pending_statics->address (),
4583 pending_statics->length ()))
4584 reconsider = true;
4586 if (flag_use_all_virtuals)
4588 if (!reconsider && !mark_all_virtuals)
4590 mark_all_virtuals = true;
4591 reconsider = true;
4592 errs = errorcount + sorrycount;
4594 else if (mark_all_virtuals
4595 && !explained_devirt
4596 && (errorcount + sorrycount > errs))
4598 inform (global_dc->last_location, "this error is seen due to "
4599 "instantiation of all virtual functions, which the C++ "
4600 "standard says are always considered used; this is done "
4601 "to support devirtualization optimizations, but can be "
4602 "disabled with -fno-use-all-virtuals");
4603 explained_devirt = true;
4607 retries++;
4609 while (reconsider);
4611 /* All used inline functions must have a definition at this point. */
4612 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4614 if (/* Check online inline functions that were actually used. */
4615 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4616 /* If the definition actually was available here, then the
4617 fact that the function was not defined merely represents
4618 that for some reason (use of a template repository,
4619 #pragma interface, etc.) we decided not to emit the
4620 definition here. */
4621 && !DECL_INITIAL (decl)
4622 /* Don't complain if the template was defined. */
4623 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4624 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4625 (template_for_substitution (decl)))))
4627 warning (0, "inline function %q+D used but never defined", decl);
4628 /* Avoid a duplicate warning from check_global_declaration_1. */
4629 TREE_NO_WARNING (decl) = 1;
4633 /* So must decls that use a type with no linkage. */
4634 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4635 no_linkage_error (decl);
4637 /* Then, do the Objective-C stuff. This is where all the
4638 Objective-C module stuff gets generated (symtab,
4639 class/protocol/selector lists etc). This must be done after C++
4640 templates, destructors etc. so that selectors used in C++
4641 templates are properly allocated. */
4642 if (c_dialect_objc ())
4643 objc_write_global_declarations ();
4645 /* We give C linkage to static constructors and destructors. */
4646 push_lang_context (lang_name_c);
4648 /* Generate initialization and destruction functions for all
4649 priorities for which they are required. */
4650 if (priority_info_map)
4651 splay_tree_foreach (priority_info_map,
4652 generate_ctor_and_dtor_functions_for_priority,
4653 /*data=*/&locus);
4654 else if (c_dialect_objc () && objc_static_init_needed_p ())
4655 /* If this is obj-c++ and we need a static init, call
4656 generate_ctor_or_dtor_function. */
4657 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4658 DEFAULT_INIT_PRIORITY, &locus);
4660 /* We're done with the splay-tree now. */
4661 if (priority_info_map)
4662 splay_tree_delete (priority_info_map);
4664 /* Generate any missing aliases. */
4665 maybe_apply_pending_pragma_weaks ();
4667 /* We're done with static constructors, so we can go back to "C++"
4668 linkage now. */
4669 pop_lang_context ();
4671 /* Collect candidates for Java hidden aliases. */
4672 candidates = collect_candidates_for_java_method_aliases ();
4674 timevar_stop (TV_PHASE_DEFERRED);
4675 timevar_start (TV_PHASE_OPT_GEN);
4677 if (flag_vtable_verify)
4679 vtv_recover_class_info ();
4680 vtv_compute_class_hierarchy_transitive_closure ();
4681 vtv_build_vtable_verify_fndecl ();
4684 finalize_compilation_unit ();
4686 if (flag_vtable_verify)
4688 /* Generate the special constructor initialization function that
4689 calls __VLTRegisterPairs, and give it a very high
4690 initialization priority. This must be done after
4691 finalize_compilation_unit so that we have accurate
4692 information about which vtable will actually be emitted. */
4693 vtv_generate_init_routine ();
4696 timevar_stop (TV_PHASE_OPT_GEN);
4697 timevar_start (TV_PHASE_CHECK_DBGINFO);
4699 /* Now, issue warnings about static, but not defined, functions,
4700 etc., and emit debugging information. */
4701 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4702 if (vec_safe_length (pending_statics) != 0)
4704 check_global_declarations (pending_statics->address (),
4705 pending_statics->length ());
4706 emit_debug_global_declarations (pending_statics->address (),
4707 pending_statics->length ());
4710 perform_deferred_noexcept_checks ();
4712 /* Generate hidden aliases for Java. */
4713 if (candidates)
4715 build_java_method_aliases (candidates);
4716 pointer_set_destroy (candidates);
4719 finish_repo ();
4721 /* The entire file is now complete. If requested, dump everything
4722 to a file. */
4723 dump_tu ();
4725 if (flag_detailed_statistics)
4727 dump_tree_statistics ();
4728 dump_time_statistics ();
4730 input_location = locus;
4732 #ifdef ENABLE_CHECKING
4733 validate_conversion_obstack ();
4734 #endif /* ENABLE_CHECKING */
4736 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4739 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4740 function to call in parse-tree form; it has not yet been
4741 semantically analyzed. ARGS are the arguments to the function.
4742 They have already been semantically analyzed. This may change
4743 ARGS. */
4745 tree
4746 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4747 tsubst_flags_t complain)
4749 tree orig_fn;
4750 vec<tree, va_gc> *orig_args = NULL;
4751 tree expr;
4752 tree object;
4754 orig_fn = fn;
4755 object = TREE_OPERAND (fn, 0);
4757 if (processing_template_decl)
4759 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4760 || TREE_CODE (fn) == MEMBER_REF);
4761 if (type_dependent_expression_p (fn)
4762 || any_type_dependent_arguments_p (*args))
4763 return build_nt_call_vec (fn, *args);
4765 orig_args = make_tree_vector_copy (*args);
4767 /* Transform the arguments and add the implicit "this"
4768 parameter. That must be done before the FN is transformed
4769 because we depend on the form of FN. */
4770 make_args_non_dependent (*args);
4771 object = build_non_dependent_expr (object);
4772 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4774 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4775 object = cp_build_addr_expr (object, complain);
4776 vec_safe_insert (*args, 0, object);
4778 /* Now that the arguments are done, transform FN. */
4779 fn = build_non_dependent_expr (fn);
4782 /* A qualified name corresponding to a bound pointer-to-member is
4783 represented as an OFFSET_REF:
4785 struct B { void g(); };
4786 void (B::*p)();
4787 void B::g() { (this->*p)(); } */
4788 if (TREE_CODE (fn) == OFFSET_REF)
4790 tree object_addr = cp_build_addr_expr (object, complain);
4791 fn = TREE_OPERAND (fn, 1);
4792 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4793 complain);
4794 vec_safe_insert (*args, 0, object_addr);
4797 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4798 expr = build_op_call (fn, args, complain);
4799 else
4800 expr = cp_build_function_call_vec (fn, args, complain);
4801 if (processing_template_decl && expr != error_mark_node)
4802 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4804 if (orig_args != NULL)
4805 release_tree_vector (orig_args);
4807 return expr;
4811 void
4812 check_default_args (tree x)
4814 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4815 bool saw_def = false;
4816 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4817 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4819 if (TREE_PURPOSE (arg))
4820 saw_def = true;
4821 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4823 error ("default argument missing for parameter %P of %q+#D", i, x);
4824 TREE_PURPOSE (arg) = error_mark_node;
4829 /* Return true if function DECL can be inlined. This is used to force
4830 instantiation of methods that might be interesting for inlining. */
4831 bool
4832 possibly_inlined_p (tree decl)
4834 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4835 if (DECL_UNINLINABLE (decl))
4836 return false;
4837 if (!optimize || pragma_java_exceptions)
4838 return DECL_DECLARED_INLINE_P (decl);
4839 /* When optimizing, we might inline everything when flatten
4840 attribute or heuristics inlining for size or autoinlining
4841 is used. */
4842 return true;
4845 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4846 If DECL is a specialization or implicitly declared class member,
4847 generate the actual definition. Return false if something goes
4848 wrong, true otherwise. */
4850 bool
4851 mark_used (tree decl, tsubst_flags_t complain)
4853 /* If DECL is a BASELINK for a single function, then treat it just
4854 like the DECL for the function. Otherwise, if the BASELINK is
4855 for an overloaded function, we don't know which function was
4856 actually used until after overload resolution. */
4857 if (BASELINK_P (decl))
4859 decl = BASELINK_FUNCTIONS (decl);
4860 if (really_overloaded_fn (decl))
4861 return true;
4862 decl = OVL_CURRENT (decl);
4865 /* Set TREE_USED for the benefit of -Wunused. */
4866 TREE_USED (decl) = 1;
4867 if (DECL_CLONED_FUNCTION_P (decl))
4868 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4870 /* Mark enumeration types as used. */
4871 if (TREE_CODE (decl) == CONST_DECL)
4872 used_types_insert (DECL_CONTEXT (decl));
4874 if (TREE_CODE (decl) == FUNCTION_DECL)
4875 maybe_instantiate_noexcept (decl);
4877 if (TREE_CODE (decl) == FUNCTION_DECL
4878 && DECL_DELETED_FN (decl))
4880 if (DECL_ARTIFICIAL (decl))
4882 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4883 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4885 /* We mark a lambda conversion op as deleted if we can't
4886 generate it properly; see maybe_add_lambda_conv_op. */
4887 sorry ("converting lambda which uses %<...%> to "
4888 "function pointer");
4889 return false;
4892 if (complain & tf_error)
4894 error ("use of deleted function %qD", decl);
4895 if (!maybe_explain_implicit_delete (decl))
4896 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4898 return false;
4901 /* We can only check DECL_ODR_USED on variables or functions with
4902 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4903 might need special handling for. */
4904 if (!VAR_OR_FUNCTION_DECL_P (decl)
4905 || DECL_LANG_SPECIFIC (decl) == NULL
4906 || DECL_THUNK_P (decl))
4908 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4910 if (complain & tf_error)
4911 error ("use of %qD before deduction of %<auto%>", decl);
4912 return false;
4914 return true;
4917 /* We only want to do this processing once. We don't need to keep trying
4918 to instantiate inline templates, because unit-at-a-time will make sure
4919 we get them compiled before functions that want to inline them. */
4920 if (DECL_ODR_USED (decl))
4921 return true;
4923 /* If within finish_function, defer the rest until that function
4924 finishes, otherwise it might recurse. */
4925 if (defer_mark_used_calls)
4927 vec_safe_push (deferred_mark_used_calls, decl);
4928 return true;
4931 /* Normally, we can wait until instantiation-time to synthesize DECL.
4932 However, if DECL is a static data member initialized with a constant
4933 or a constexpr function, we need it right now because a reference to
4934 such a data member or a call to such function is not value-dependent.
4935 For a function that uses auto in the return type, we need to instantiate
4936 it to find out its type. For OpenMP user defined reductions, we need
4937 them instantiated for reduction clauses which inline them by hand
4938 directly. */
4939 if (DECL_LANG_SPECIFIC (decl)
4940 && DECL_TEMPLATE_INFO (decl)
4941 && (decl_maybe_constant_var_p (decl)
4942 || (TREE_CODE (decl) == FUNCTION_DECL
4943 && (DECL_DECLARED_CONSTEXPR_P (decl)
4944 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4945 || undeduced_auto_decl (decl))
4946 && !uses_template_parms (DECL_TI_ARGS (decl)))
4948 /* Instantiating a function will result in garbage collection. We
4949 must treat this situation as if we were within the body of a
4950 function so as to avoid collecting live data only referenced from
4951 the stack (such as overload resolution candidates). */
4952 ++function_depth;
4953 instantiate_decl (decl, /*defer_ok=*/false,
4954 /*expl_inst_class_mem_p=*/false);
4955 --function_depth;
4958 if (processing_template_decl)
4959 return true;
4961 /* Check this too in case we're within fold_non_dependent_expr. */
4962 if (DECL_TEMPLATE_INFO (decl)
4963 && uses_template_parms (DECL_TI_ARGS (decl)))
4964 return true;
4966 require_deduced_type (decl);
4968 /* If we don't need a value, then we don't need to synthesize DECL. */
4969 if (cp_unevaluated_operand != 0)
4970 return true;
4972 DECL_ODR_USED (decl) = 1;
4973 if (DECL_CLONED_FUNCTION_P (decl))
4974 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4976 /* DR 757: A type without linkage shall not be used as the type of a
4977 variable or function with linkage, unless
4978 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4979 o the variable or function is not used (3.2 [basic.def.odr]) or is
4980 defined in the same translation unit. */
4981 if (cxx_dialect > cxx98
4982 && decl_linkage (decl) != lk_none
4983 && !DECL_EXTERN_C_P (decl)
4984 && !DECL_ARTIFICIAL (decl)
4985 && !decl_defined_p (decl)
4986 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4988 if (is_local_extern (decl))
4989 /* There's no way to define a local extern, and adding it to
4990 the vector interferes with GC, so give an error now. */
4991 no_linkage_error (decl);
4992 else
4993 vec_safe_push (no_linkage_decls, decl);
4996 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4997 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4998 /* Remember it, so we can check it was defined. */
4999 note_vague_linkage_fn (decl);
5001 /* Is it a synthesized method that needs to be synthesized? */
5002 if (TREE_CODE (decl) == FUNCTION_DECL
5003 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5004 && DECL_DEFAULTED_FN (decl)
5005 /* A function defaulted outside the class is synthesized either by
5006 cp_finish_decl or instantiate_decl. */
5007 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5008 && ! DECL_INITIAL (decl))
5010 /* Defer virtual destructors so that thunks get the right
5011 linkage. */
5012 if (DECL_VIRTUAL_P (decl) && !at_eof)
5014 note_vague_linkage_fn (decl);
5015 return true;
5018 /* Remember the current location for a function we will end up
5019 synthesizing. Then we can inform the user where it was
5020 required in the case of error. */
5021 DECL_SOURCE_LOCATION (decl) = input_location;
5023 /* Synthesizing an implicitly defined member function will result in
5024 garbage collection. We must treat this situation as if we were
5025 within the body of a function so as to avoid collecting live data
5026 on the stack (such as overload resolution candidates).
5028 We could just let cp_write_global_declarations handle synthesizing
5029 this function by adding it to deferred_fns, but doing
5030 it at the use site produces better error messages. */
5031 ++function_depth;
5032 synthesize_method (decl);
5033 --function_depth;
5034 /* If this is a synthesized method we don't need to
5035 do the instantiation test below. */
5037 else if (VAR_OR_FUNCTION_DECL_P (decl)
5038 && DECL_TEMPLATE_INFO (decl)
5039 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5040 || always_instantiate_p (decl)))
5041 /* If this is a function or variable that is an instance of some
5042 template, we now know that we will need to actually do the
5043 instantiation. We check that DECL is not an explicit
5044 instantiation because that is not checked in instantiate_decl.
5046 We put off instantiating functions in order to improve compile
5047 times. Maintaining a stack of active functions is expensive,
5048 and the inliner knows to instantiate any functions it might
5049 need. Therefore, we always try to defer instantiation. */
5051 ++function_depth;
5052 instantiate_decl (decl, /*defer_ok=*/true,
5053 /*expl_inst_class_mem_p=*/false);
5054 --function_depth;
5057 return true;
5060 bool
5061 mark_used (tree decl)
5063 return mark_used (decl, tf_warning_or_error);
5066 tree
5067 vtv_start_verification_constructor_init_function (void)
5069 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5072 tree
5073 vtv_finish_verification_constructor_init_function (tree function_body)
5075 tree fn;
5077 finish_compound_stmt (function_body);
5078 fn = finish_function (0);
5079 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5080 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5082 return fn;
5085 #include "gt-cp-decl2.h"