2015-06-25 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / cp / decl2.c
blobf045253a1ab8ed6dc3232e009e263332f4df850b
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "tree.h"
36 #include "stringpool.h"
37 #include "varasm.h"
38 #include "attribs.h"
39 #include "stor-layout.h"
40 #include "calls.h"
41 #include "flags.h"
42 #include "cp-tree.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "timevar.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "c-family/c-common.h"
49 #include "c-family/c-objc.h"
50 #include "hard-reg-set.h"
51 #include "function.h"
52 #include "cgraph.h"
53 #include "tree-inline.h"
54 #include "c-family/c-pragma.h"
55 #include "dumpfile.h"
56 #include "intl.h"
57 #include "splay-tree.h"
58 #include "langhooks.h"
59 #include "c-family/c-ada-spec.h"
60 #include "asan.h"
62 extern cpp_reader *parse_in;
64 /* This structure contains information about the initializations
65 and/or destructions required for a particular priority level. */
66 typedef struct priority_info_s {
67 /* Nonzero if there have been any initializations at this priority
68 throughout the translation unit. */
69 int initializations_p;
70 /* Nonzero if there have been any destructions at this priority
71 throughout the translation unit. */
72 int destructions_p;
73 } *priority_info;
75 static void mark_vtable_entries (tree);
76 static bool maybe_emit_vtables (tree);
77 static bool acceptable_java_type (tree);
78 static tree start_objects (int, int);
79 static void finish_objects (int, int, tree);
80 static tree start_static_storage_duration_function (unsigned);
81 static void finish_static_storage_duration_function (tree);
82 static priority_info get_priority_info (int);
83 static void do_static_initialization_or_destruction (tree, bool);
84 static void one_static_initialization_or_destruction (tree, tree, bool);
85 static void generate_ctor_or_dtor_function (bool, int, location_t *);
86 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
87 void *);
88 static tree prune_vars_needing_no_initialization (tree *);
89 static void write_out_vars (tree);
90 static void import_export_class (tree);
91 static tree get_guard_bits (tree);
92 static void determine_visibility_from_class (tree, tree);
93 static bool determine_hidden_inline (tree);
94 static bool decl_defined_p (tree);
96 /* A list of static class variables. This is needed, because a
97 static class variable can be declared inside the class without
98 an initializer, and then initialized, statically, outside the class. */
99 static GTY(()) vec<tree, va_gc> *pending_statics;
101 /* A list of functions which were declared inline, but which we
102 may need to emit outline anyway. */
103 static GTY(()) vec<tree, va_gc> *deferred_fns;
105 /* A list of decls that use types with no linkage, which we need to make
106 sure are defined. */
107 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
109 /* A vector of alternating decls and identifiers, where the latter
110 is to be an alias for the former if the former is defined. */
111 static GTY(()) vec<tree, va_gc> *mangling_aliases;
113 /* Nonzero if we're done parsing and into end-of-file activities. */
115 int at_eof;
118 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
119 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
120 that apply to the function). */
122 tree
123 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
124 cp_ref_qualifier rqual)
126 tree raises;
127 tree attrs;
128 int type_quals;
129 bool late_return_type_p;
131 if (fntype == error_mark_node || ctype == error_mark_node)
132 return error_mark_node;
134 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
135 || TREE_CODE (fntype) == METHOD_TYPE);
137 type_quals = quals & ~TYPE_QUAL_RESTRICT;
138 ctype = cp_build_qualified_type (ctype, type_quals);
139 raises = TYPE_RAISES_EXCEPTIONS (fntype);
140 attrs = TYPE_ATTRIBUTES (fntype);
141 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
142 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
143 (TREE_CODE (fntype) == METHOD_TYPE
144 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
145 : TYPE_ARG_TYPES (fntype)));
146 if (attrs)
147 fntype = cp_build_type_attribute_variant (fntype, attrs);
148 if (rqual)
149 fntype = build_ref_qualified_type (fntype, rqual);
150 if (raises)
151 fntype = build_exception_variant (fntype, raises);
152 if (late_return_type_p)
153 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
155 return fntype;
158 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
159 return type changed to NEW_RET. */
161 tree
162 change_return_type (tree new_ret, tree fntype)
164 tree newtype;
165 tree args = TYPE_ARG_TYPES (fntype);
166 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
167 tree attrs = TYPE_ATTRIBUTES (fntype);
168 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
170 if (new_ret == error_mark_node)
171 return fntype;
173 if (same_type_p (new_ret, TREE_TYPE (fntype)))
174 return fntype;
176 if (TREE_CODE (fntype) == FUNCTION_TYPE)
178 newtype = build_function_type (new_ret, args);
179 newtype = apply_memfn_quals (newtype,
180 type_memfn_quals (fntype),
181 type_memfn_rqual (fntype));
183 else
184 newtype = build_method_type_directly
185 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
186 if (FUNCTION_REF_QUALIFIED (fntype))
187 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
188 if (raises)
189 newtype = build_exception_variant (newtype, raises);
190 if (attrs)
191 newtype = cp_build_type_attribute_variant (newtype, attrs);
192 if (late_return_type_p)
193 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
195 return newtype;
198 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
199 appropriately. */
201 tree
202 cp_build_parm_decl (tree name, tree type)
204 tree parm = build_decl (input_location,
205 PARM_DECL, name, type);
206 /* DECL_ARG_TYPE is only used by the back end and the back end never
207 sees templates. */
208 if (!processing_template_decl)
209 DECL_ARG_TYPE (parm) = type_passed_as (type);
211 return parm;
214 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
215 indicated NAME. */
217 tree
218 build_artificial_parm (tree name, tree type)
220 tree parm = cp_build_parm_decl (name, type);
221 DECL_ARTIFICIAL (parm) = 1;
222 /* All our artificial parms are implicitly `const'; they cannot be
223 assigned to. */
224 TREE_READONLY (parm) = 1;
225 return parm;
228 /* Constructors for types with virtual baseclasses need an "in-charge" flag
229 saying whether this constructor is responsible for initialization of
230 virtual baseclasses or not. All destructors also need this "in-charge"
231 flag, which additionally determines whether or not the destructor should
232 free the memory for the object.
234 This function adds the "in-charge" flag to member function FN if
235 appropriate. It is called from grokclassfn and tsubst.
236 FN must be either a constructor or destructor.
238 The in-charge flag follows the 'this' parameter, and is followed by the
239 VTT parm (if any), then the user-written parms. */
241 void
242 maybe_retrofit_in_chrg (tree fn)
244 tree basetype, arg_types, parms, parm, fntype;
246 /* If we've already add the in-charge parameter don't do it again. */
247 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
248 return;
250 /* When processing templates we can't know, in general, whether or
251 not we're going to have virtual baseclasses. */
252 if (processing_template_decl)
253 return;
255 /* We don't need an in-charge parameter for constructors that don't
256 have virtual bases. */
257 if (DECL_CONSTRUCTOR_P (fn)
258 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
259 return;
261 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
262 basetype = TREE_TYPE (TREE_VALUE (arg_types));
263 arg_types = TREE_CHAIN (arg_types);
265 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
267 /* If this is a subobject constructor or destructor, our caller will
268 pass us a pointer to our VTT. */
269 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
271 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
273 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
274 DECL_CHAIN (parm) = parms;
275 parms = parm;
277 /* ...and then to TYPE_ARG_TYPES. */
278 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
280 DECL_HAS_VTT_PARM_P (fn) = 1;
283 /* Then add the in-charge parm (before the VTT parm). */
284 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
285 DECL_CHAIN (parm) = parms;
286 parms = parm;
287 arg_types = hash_tree_chain (integer_type_node, arg_types);
289 /* Insert our new parameter(s) into the list. */
290 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
292 /* And rebuild the function type. */
293 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
294 arg_types);
295 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
296 fntype = build_exception_variant (fntype,
297 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
298 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
299 fntype = (cp_build_type_attribute_variant
300 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
301 TREE_TYPE (fn) = fntype;
303 /* Now we've got the in-charge parameter. */
304 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
307 /* Classes overload their constituent function names automatically.
308 When a function name is declared in a record structure,
309 its name is changed to it overloaded name. Since names for
310 constructors and destructors can conflict, we place a leading
311 '$' for destructors.
313 CNAME is the name of the class we are grokking for.
315 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
317 FLAGS contains bits saying what's special about today's
318 arguments. DTOR_FLAG == DESTRUCTOR.
320 If FUNCTION is a destructor, then we must add the `auto-delete' field
321 as a second parameter. There is some hair associated with the fact
322 that we must "declare" this variable in the manner consistent with the
323 way the rest of the arguments were declared.
325 QUALS are the qualifiers for the this pointer. */
327 void
328 grokclassfn (tree ctype, tree function, enum overload_flags flags)
330 tree fn_name = DECL_NAME (function);
332 /* Even within an `extern "C"' block, members get C++ linkage. See
333 [dcl.link] for details. */
334 SET_DECL_LANGUAGE (function, lang_cplusplus);
336 if (fn_name == NULL_TREE)
338 error ("name missing for member function");
339 fn_name = get_identifier ("<anonymous>");
340 DECL_NAME (function) = fn_name;
343 DECL_CONTEXT (function) = ctype;
345 if (flags == DTOR_FLAG)
346 DECL_DESTRUCTOR_P (function) = 1;
348 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
349 maybe_retrofit_in_chrg (function);
352 /* Create an ARRAY_REF, checking for the user doing things backwards
353 along the way. DECLTYPE_P is for N3276, as in the parser. */
355 tree
356 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
357 bool decltype_p)
359 tree type;
360 tree expr;
361 tree orig_array_expr = array_expr;
362 tree orig_index_exp = index_exp;
364 if (error_operand_p (array_expr) || error_operand_p (index_exp))
365 return error_mark_node;
367 if (processing_template_decl)
369 if (type_dependent_expression_p (array_expr)
370 || type_dependent_expression_p (index_exp))
371 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
372 NULL_TREE, NULL_TREE);
373 array_expr = build_non_dependent_expr (array_expr);
374 index_exp = build_non_dependent_expr (index_exp);
377 type = TREE_TYPE (array_expr);
378 gcc_assert (type);
379 type = non_reference (type);
381 /* If they have an `operator[]', use that. */
382 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
384 tsubst_flags_t complain = tf_warning_or_error;
385 if (decltype_p)
386 complain |= tf_decltype;
387 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
388 index_exp, NULL_TREE, /*overload=*/NULL, complain);
390 else
392 tree p1, p2, i1, i2;
394 /* Otherwise, create an ARRAY_REF for a pointer or array type.
395 It is a little-known fact that, if `a' is an array and `i' is
396 an int, you can write `i[a]', which means the same thing as
397 `a[i]'. */
398 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
399 p1 = array_expr;
400 else
401 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
403 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
404 p2 = index_exp;
405 else
406 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
408 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
409 false);
410 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
411 false);
413 if ((p1 && i2) && (i1 && p2))
414 error ("ambiguous conversion for array subscript");
416 if (p1 && i2)
417 array_expr = p1, index_exp = i2;
418 else if (i1 && p2)
419 array_expr = p2, index_exp = i1;
420 else
422 error ("invalid types %<%T[%T]%> for array subscript",
423 type, TREE_TYPE (index_exp));
424 return error_mark_node;
427 if (array_expr == error_mark_node || index_exp == error_mark_node)
428 error ("ambiguous conversion for array subscript");
430 expr = build_array_ref (input_location, array_expr, index_exp);
432 if (processing_template_decl && expr != error_mark_node)
433 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
434 NULL_TREE, NULL_TREE);
435 return expr;
438 /* Given the cast expression EXP, checking out its validity. Either return
439 an error_mark_node if there was an unavoidable error, return a cast to
440 void for trying to delete a pointer w/ the value 0, or return the
441 call to delete. If DOING_VEC is true, we handle things differently
442 for doing an array delete.
443 Implements ARM $5.3.4. This is called from the parser. */
445 tree
446 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
447 tsubst_flags_t complain)
449 tree t, type;
451 if (exp == error_mark_node)
452 return exp;
454 if (processing_template_decl)
456 t = build_min (DELETE_EXPR, void_type_node, exp, size);
457 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
458 DELETE_EXPR_USE_VEC (t) = doing_vec;
459 TREE_SIDE_EFFECTS (t) = 1;
460 return t;
463 /* An array can't have been allocated by new, so complain. */
464 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
465 warning (0, "deleting array %q#E", exp);
467 t = build_expr_type_conversion (WANT_POINTER, exp, true);
469 if (t == NULL_TREE || t == error_mark_node)
471 error ("type %q#T argument given to %<delete%>, expected pointer",
472 TREE_TYPE (exp));
473 return error_mark_node;
476 type = TREE_TYPE (t);
478 /* As of Valley Forge, you can delete a pointer to const. */
480 /* You can't delete functions. */
481 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
483 error ("cannot delete a function. Only pointer-to-objects are "
484 "valid arguments to %<delete%>");
485 return error_mark_node;
488 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
489 if (VOID_TYPE_P (TREE_TYPE (type)))
491 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
492 doing_vec = 0;
495 /* Deleting a pointer with the value zero is valid and has no effect. */
496 if (integer_zerop (t))
497 return build1 (NOP_EXPR, void_type_node, t);
499 if (doing_vec)
500 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
501 sfk_deleting_destructor,
502 use_global_delete, complain);
503 else
504 return build_delete (type, t, sfk_deleting_destructor,
505 LOOKUP_NORMAL, use_global_delete,
506 complain);
509 /* Report an error if the indicated template declaration is not the
510 sort of thing that should be a member template. */
512 void
513 check_member_template (tree tmpl)
515 tree decl;
517 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
518 decl = DECL_TEMPLATE_RESULT (tmpl);
520 if (TREE_CODE (decl) == FUNCTION_DECL
521 || DECL_ALIAS_TEMPLATE_P (tmpl)
522 || (TREE_CODE (decl) == TYPE_DECL
523 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
525 /* The parser rejects template declarations in local classes
526 (with the exception of generic lambdas). */
527 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
528 /* The parser rejects any use of virtual in a function template. */
529 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
530 && DECL_VIRTUAL_P (decl)));
532 /* The debug-information generating code doesn't know what to do
533 with member templates. */
534 DECL_IGNORED_P (tmpl) = 1;
536 else if (variable_template_p (tmpl))
537 /* OK */;
538 else
539 error ("template declaration of %q#D", decl);
542 /* Return true iff TYPE is a valid Java parameter or return type. */
544 static bool
545 acceptable_java_type (tree type)
547 if (type == error_mark_node)
548 return false;
550 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
551 return true;
552 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
554 type = TREE_TYPE (type);
555 if (TREE_CODE (type) == RECORD_TYPE)
557 tree args; int i;
558 if (! TYPE_FOR_JAVA (type))
559 return false;
560 if (! CLASSTYPE_TEMPLATE_INFO (type))
561 return true;
562 args = CLASSTYPE_TI_ARGS (type);
563 i = TREE_VEC_LENGTH (args);
564 while (--i >= 0)
566 type = TREE_VEC_ELT (args, i);
567 if (TYPE_PTR_P (type))
568 type = TREE_TYPE (type);
569 if (! TYPE_FOR_JAVA (type))
570 return false;
572 return true;
575 return false;
578 /* For a METHOD in a Java class CTYPE, return true if
579 the parameter and return types are valid Java types.
580 Otherwise, print appropriate error messages, and return false. */
582 bool
583 check_java_method (tree method)
585 bool jerr = false;
586 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
587 tree ret_type = TREE_TYPE (TREE_TYPE (method));
589 if (!acceptable_java_type (ret_type))
591 error ("Java method %qD has non-Java return type %qT",
592 method, ret_type);
593 jerr = true;
596 arg_types = TREE_CHAIN (arg_types);
597 if (DECL_HAS_IN_CHARGE_PARM_P (method))
598 arg_types = TREE_CHAIN (arg_types);
599 if (DECL_HAS_VTT_PARM_P (method))
600 arg_types = TREE_CHAIN (arg_types);
602 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
604 tree type = TREE_VALUE (arg_types);
605 if (!acceptable_java_type (type))
607 if (type != error_mark_node)
608 error ("Java method %qD has non-Java parameter type %qT",
609 method, type);
610 jerr = true;
613 return !jerr;
616 /* Sanity check: report error if this function FUNCTION is not
617 really a member of the class (CTYPE) it is supposed to belong to.
618 TEMPLATE_PARMS is used to specify the template parameters of a member
619 template passed as FUNCTION_DECL. If the member template is passed as a
620 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
621 from the declaration. If the function is not a function template, it
622 must be NULL.
623 It returns the original declaration for the function, NULL_TREE if
624 no declaration was found, error_mark_node if an error was emitted. */
626 tree
627 check_classfn (tree ctype, tree function, tree template_parms)
629 int ix;
630 bool is_template;
631 tree pushed_scope;
633 if (DECL_USE_TEMPLATE (function)
634 && !(TREE_CODE (function) == TEMPLATE_DECL
635 && DECL_TEMPLATE_SPECIALIZATION (function))
636 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
637 /* Since this is a specialization of a member template,
638 we're not going to find the declaration in the class.
639 For example, in:
641 struct S { template <typename T> void f(T); };
642 template <> void S::f(int);
644 we're not going to find `S::f(int)', but there's no
645 reason we should, either. We let our callers know we didn't
646 find the method, but we don't complain. */
647 return NULL_TREE;
649 /* Basic sanity check: for a template function, the template parameters
650 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
651 if (TREE_CODE (function) == TEMPLATE_DECL)
653 if (template_parms
654 && !comp_template_parms (template_parms,
655 DECL_TEMPLATE_PARMS (function)))
657 error ("template parameter lists provided don%'t match the "
658 "template parameters of %qD", function);
659 return error_mark_node;
661 template_parms = DECL_TEMPLATE_PARMS (function);
664 /* OK, is this a definition of a member template? */
665 is_template = (template_parms != NULL_TREE);
667 /* [temp.mem]
669 A destructor shall not be a member template. */
670 if (DECL_DESTRUCTOR_P (function) && is_template)
672 error ("destructor %qD declared as member template", function);
673 return error_mark_node;
676 /* We must enter the scope here, because conversion operators are
677 named by target type, and type equivalence relies on typenames
678 resolving within the scope of CTYPE. */
679 pushed_scope = push_scope (ctype);
680 ix = class_method_index_for_fn (complete_type (ctype), function);
681 if (ix >= 0)
683 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
684 tree fndecls, fndecl = 0;
685 bool is_conv_op;
686 const char *format = NULL;
688 for (fndecls = (*methods)[ix];
689 fndecls; fndecls = OVL_NEXT (fndecls))
691 tree p1, p2;
693 fndecl = OVL_CURRENT (fndecls);
694 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
695 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
697 /* We cannot simply call decls_match because this doesn't
698 work for static member functions that are pretending to
699 be methods, and because the name may have been changed by
700 asm("new_name"). */
702 /* Get rid of the this parameter on functions that become
703 static. */
704 if (DECL_STATIC_FUNCTION_P (fndecl)
705 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
706 p1 = TREE_CHAIN (p1);
708 /* A member template definition only matches a member template
709 declaration. */
710 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
711 continue;
713 /* ref-qualifier or absence of same must match. */
714 if (type_memfn_rqual (TREE_TYPE (function))
715 != type_memfn_rqual (TREE_TYPE (fndecl)))
716 continue;
718 /* While finding a match, same types and params are not enough
719 if the function is versioned. Also check version ("target")
720 attributes. */
721 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
722 TREE_TYPE (TREE_TYPE (fndecl)))
723 && compparms (p1, p2)
724 && !targetm.target_option.function_versions (function, fndecl)
725 && (!is_template
726 || comp_template_parms (template_parms,
727 DECL_TEMPLATE_PARMS (fndecl)))
728 && (DECL_TEMPLATE_SPECIALIZATION (function)
729 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
730 && (!DECL_TEMPLATE_SPECIALIZATION (function)
731 || (DECL_TI_TEMPLATE (function)
732 == DECL_TI_TEMPLATE (fndecl))))
733 break;
735 if (fndecls)
737 if (pushed_scope)
738 pop_scope (pushed_scope);
739 return OVL_CURRENT (fndecls);
742 error_at (DECL_SOURCE_LOCATION (function),
743 "prototype for %q#D does not match any in class %qT",
744 function, ctype);
745 is_conv_op = DECL_CONV_FN_P (fndecl);
747 if (is_conv_op)
748 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
749 fndecls = (*methods)[ix];
750 while (fndecls)
752 fndecl = OVL_CURRENT (fndecls);
753 fndecls = OVL_NEXT (fndecls);
755 if (!fndecls && is_conv_op)
757 if (methods->length () > (size_t) ++ix)
759 fndecls = (*methods)[ix];
760 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
762 fndecls = NULL_TREE;
763 is_conv_op = false;
766 else
767 is_conv_op = false;
769 if (format)
770 format = " %+#D";
771 else if (fndecls)
772 format = N_("candidates are: %+#D");
773 else
774 format = N_("candidate is: %+#D");
775 error (format, fndecl);
778 else if (!COMPLETE_TYPE_P (ctype))
779 cxx_incomplete_type_error (function, ctype);
780 else
781 error ("no %q#D member function declared in class %qT",
782 function, ctype);
784 if (pushed_scope)
785 pop_scope (pushed_scope);
786 return error_mark_node;
789 /* DECL is a function with vague linkage. Remember it so that at the
790 end of the translation unit we can decide whether or not to emit
791 it. */
793 void
794 note_vague_linkage_fn (tree decl)
796 DECL_DEFER_OUTPUT (decl) = 1;
797 vec_safe_push (deferred_fns, decl);
800 /* As above, but for variable template instantiations. */
802 void
803 note_variable_template_instantiation (tree decl)
805 vec_safe_push (pending_statics, decl);
808 /* We have just processed the DECL, which is a static data member.
809 The other parameters are as for cp_finish_decl. */
811 void
812 finish_static_data_member_decl (tree decl,
813 tree init, bool init_const_expr_p,
814 tree asmspec_tree,
815 int flags)
817 DECL_CONTEXT (decl) = current_class_type;
819 /* We cannot call pushdecl here, because that would fill in the
820 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
821 the right thing, namely, to put this decl out straight away. */
823 if (! processing_template_decl)
824 vec_safe_push (pending_statics, decl);
826 if (LOCAL_CLASS_P (current_class_type)
827 /* We already complained about the template definition. */
828 && !DECL_TEMPLATE_INSTANTIATION (decl))
829 permerror (input_location, "local class %q#T shall not have static data member %q#D",
830 current_class_type, decl);
831 else
832 for (tree t = current_class_type; TYPE_P (t);
833 t = CP_TYPE_CONTEXT (t))
834 if (TYPE_ANONYMOUS_P (t))
836 if (permerror (DECL_SOURCE_LOCATION (decl),
837 "static data member %qD in unnamed class", decl))
838 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
839 "unnamed class defined here");
840 break;
843 DECL_IN_AGGR_P (decl) = 1;
845 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
846 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
847 SET_VAR_HAD_UNKNOWN_BOUND (decl);
849 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
852 /* DECLARATOR and DECLSPECS correspond to a class member. The other
853 parameters are as for cp_finish_decl. Return the DECL for the
854 class member declared. */
856 tree
857 grokfield (const cp_declarator *declarator,
858 cp_decl_specifier_seq *declspecs,
859 tree init, bool init_const_expr_p,
860 tree asmspec_tree,
861 tree attrlist)
863 tree value;
864 const char *asmspec = 0;
865 int flags;
866 tree name;
868 if (init
869 && TREE_CODE (init) == TREE_LIST
870 && TREE_VALUE (init) == error_mark_node
871 && TREE_CHAIN (init) == NULL_TREE)
872 init = NULL_TREE;
874 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
875 if (! value || value == error_mark_node)
876 /* friend or constructor went bad. */
877 return error_mark_node;
878 if (TREE_TYPE (value) == error_mark_node)
879 return value;
881 if (TREE_CODE (value) == TYPE_DECL && init)
883 error ("typedef %qD is initialized (use decltype instead)", value);
884 init = NULL_TREE;
887 /* Pass friendly classes back. */
888 if (value == void_type_node)
889 return value;
892 name = DECL_NAME (value);
894 if (name != NULL_TREE)
896 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
898 error ("explicit template argument list not allowed");
899 return error_mark_node;
902 if (IDENTIFIER_POINTER (name)[0] == '_'
903 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
904 error ("member %qD conflicts with virtual function table field name",
905 value);
908 /* Stash away type declarations. */
909 if (TREE_CODE (value) == TYPE_DECL)
911 DECL_NONLOCAL (value) = 1;
912 DECL_CONTEXT (value) = current_class_type;
914 if (attrlist)
916 int attrflags = 0;
918 /* If this is a typedef that names the class for linkage purposes
919 (7.1.3p8), apply any attributes directly to the type. */
920 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
921 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
922 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
924 cplus_decl_attributes (&value, attrlist, attrflags);
927 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
928 && TREE_TYPE (value) != error_mark_node
929 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
930 set_underlying_type (value);
932 /* It's important that push_template_decl below follows
933 set_underlying_type above so that the created template
934 carries the properly set type of VALUE. */
935 if (processing_template_decl)
936 value = push_template_decl (value);
938 record_locally_defined_typedef (value);
939 return value;
942 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
944 if (!friendp && DECL_IN_AGGR_P (value))
946 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
947 return void_type_node;
950 if (asmspec_tree && asmspec_tree != error_mark_node)
951 asmspec = TREE_STRING_POINTER (asmspec_tree);
953 if (init)
955 if (TREE_CODE (value) == FUNCTION_DECL)
957 if (init == ridpointers[(int)RID_DELETE])
959 DECL_DELETED_FN (value) = 1;
960 DECL_DECLARED_INLINE_P (value) = 1;
961 DECL_INITIAL (value) = error_mark_node;
963 else if (init == ridpointers[(int)RID_DEFAULT])
965 if (defaultable_fn_check (value))
967 DECL_DEFAULTED_FN (value) = 1;
968 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
969 DECL_DECLARED_INLINE_P (value) = 1;
972 else if (TREE_CODE (init) == DEFAULT_ARG)
973 error ("invalid initializer for member function %qD", value);
974 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
976 if (integer_zerop (init))
977 DECL_PURE_VIRTUAL_P (value) = 1;
978 else if (error_operand_p (init))
979 ; /* An error has already been reported. */
980 else
981 error ("invalid initializer for member function %qD",
982 value);
984 else
986 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
987 if (friendp)
988 error ("initializer specified for friend function %qD",
989 value);
990 else
991 error ("initializer specified for static member function %qD",
992 value);
995 else if (TREE_CODE (value) == FIELD_DECL)
996 /* C++11 NSDMI, keep going. */;
997 else if (!VAR_P (value))
998 gcc_unreachable ();
1001 /* Pass friend decls back. */
1002 if ((TREE_CODE (value) == FUNCTION_DECL
1003 || TREE_CODE (value) == TEMPLATE_DECL)
1004 && DECL_CONTEXT (value) != current_class_type)
1005 return value;
1007 /* Need to set this before push_template_decl. */
1008 if (TREE_CODE (value) == VAR_DECL)
1009 DECL_CONTEXT (value) = current_class_type;
1011 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1013 value = push_template_decl (value);
1014 if (error_operand_p (value))
1015 return error_mark_node;
1018 if (attrlist)
1019 cplus_decl_attributes (&value, attrlist, 0);
1021 if (init && DIRECT_LIST_INIT_P (init))
1022 flags = LOOKUP_NORMAL;
1023 else
1024 flags = LOOKUP_IMPLICIT;
1026 switch (TREE_CODE (value))
1028 case VAR_DECL:
1029 finish_static_data_member_decl (value, init, init_const_expr_p,
1030 asmspec_tree, flags);
1031 return value;
1033 case FIELD_DECL:
1034 if (asmspec)
1035 error ("%<asm%> specifiers are not permitted on non-static data members");
1036 if (DECL_INITIAL (value) == error_mark_node)
1037 init = error_mark_node;
1038 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1039 NULL_TREE, flags);
1040 DECL_IN_AGGR_P (value) = 1;
1041 return value;
1043 case FUNCTION_DECL:
1044 if (asmspec)
1045 set_user_assembler_name (value, asmspec);
1047 cp_finish_decl (value,
1048 /*init=*/NULL_TREE,
1049 /*init_const_expr_p=*/false,
1050 asmspec_tree, flags);
1052 /* Pass friends back this way. */
1053 if (DECL_FRIEND_P (value))
1054 return void_type_node;
1056 DECL_IN_AGGR_P (value) = 1;
1057 return value;
1059 default:
1060 gcc_unreachable ();
1062 return NULL_TREE;
1065 /* Like `grokfield', but for bitfields.
1066 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1068 tree
1069 grokbitfield (const cp_declarator *declarator,
1070 cp_decl_specifier_seq *declspecs, tree width,
1071 tree attrlist)
1073 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1075 if (value == error_mark_node)
1076 return NULL_TREE; /* friends went bad. */
1077 if (TREE_TYPE (value) == error_mark_node)
1078 return value;
1080 /* Pass friendly classes back. */
1081 if (VOID_TYPE_P (value))
1082 return void_type_node;
1084 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1085 && (POINTER_TYPE_P (value)
1086 || !dependent_type_p (TREE_TYPE (value))))
1088 error ("bit-field %qD with non-integral type", value);
1089 return error_mark_node;
1092 if (TREE_CODE (value) == TYPE_DECL)
1094 error ("cannot declare %qD to be a bit-field type", value);
1095 return NULL_TREE;
1098 /* Usually, finish_struct_1 catches bitfields with invalid types.
1099 But, in the case of bitfields with function type, we confuse
1100 ourselves into thinking they are member functions, so we must
1101 check here. */
1102 if (TREE_CODE (value) == FUNCTION_DECL)
1104 error ("cannot declare bit-field %qD with function type",
1105 DECL_NAME (value));
1106 return NULL_TREE;
1109 if (DECL_IN_AGGR_P (value))
1111 error ("%qD is already defined in the class %qT", value,
1112 DECL_CONTEXT (value));
1113 return void_type_node;
1116 if (TREE_STATIC (value))
1118 error ("static member %qD cannot be a bit-field", value);
1119 return NULL_TREE;
1121 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1123 if (width != error_mark_node)
1125 /* The width must be an integer type. */
1126 if (!type_dependent_expression_p (width)
1127 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1128 error ("width of bit-field %qD has non-integral type %qT", value,
1129 TREE_TYPE (width));
1130 DECL_INITIAL (value) = width;
1131 SET_DECL_C_BIT_FIELD (value);
1134 DECL_IN_AGGR_P (value) = 1;
1136 if (attrlist)
1137 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1139 return value;
1143 /* Returns true iff ATTR is an attribute which needs to be applied at
1144 instantiation time rather than template definition time. */
1146 static bool
1147 is_late_template_attribute (tree attr, tree decl)
1149 tree name = get_attribute_name (attr);
1150 tree args = TREE_VALUE (attr);
1151 const struct attribute_spec *spec = lookup_attribute_spec (name);
1152 tree arg;
1154 if (!spec)
1155 /* Unknown attribute. */
1156 return false;
1158 /* Attribute weak handling wants to write out assembly right away. */
1159 if (is_attribute_p ("weak", name))
1160 return true;
1162 /* Attribute unused is applied directly, as it appertains to
1163 decls. */
1164 if (is_attribute_p ("unused", name))
1165 return false;
1167 /* #pragma omp declare simd attribute needs to be always deferred. */
1168 if (flag_openmp
1169 && is_attribute_p ("omp declare simd", name))
1170 return true;
1172 /* An attribute pack is clearly dependent. */
1173 if (args && PACK_EXPANSION_P (args))
1174 return true;
1176 /* If any of the arguments are dependent expressions, we can't evaluate
1177 the attribute until instantiation time. */
1178 for (arg = args; arg; arg = TREE_CHAIN (arg))
1180 tree t = TREE_VALUE (arg);
1182 /* If the first attribute argument is an identifier, only consider
1183 second and following arguments. Attributes like mode, format,
1184 cleanup and several target specific attributes aren't late
1185 just because they have an IDENTIFIER_NODE as first argument. */
1186 if (arg == args && identifier_p (t))
1187 continue;
1189 if (value_dependent_expression_p (t)
1190 || type_dependent_expression_p (t))
1191 return true;
1194 if (TREE_CODE (decl) == TYPE_DECL
1195 || TYPE_P (decl)
1196 || spec->type_required)
1198 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1200 /* We can't apply any attributes to a completely unknown type until
1201 instantiation time. */
1202 enum tree_code code = TREE_CODE (type);
1203 if (code == TEMPLATE_TYPE_PARM
1204 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1205 || code == TYPENAME_TYPE)
1206 return true;
1207 /* Also defer most attributes on dependent types. This is not
1208 necessary in all cases, but is the better default. */
1209 else if (dependent_type_p (type)
1210 /* But some attributes specifically apply to templates. */
1211 && !is_attribute_p ("abi_tag", name)
1212 && !is_attribute_p ("deprecated", name)
1213 && !is_attribute_p ("visibility", name))
1214 return true;
1215 else
1216 return false;
1218 else
1219 return false;
1222 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1223 applied at instantiation time and return them. If IS_DEPENDENT is true,
1224 the declaration itself is dependent, so all attributes should be applied
1225 at instantiation time. */
1227 static tree
1228 splice_template_attributes (tree *attr_p, tree decl)
1230 tree *p = attr_p;
1231 tree late_attrs = NULL_TREE;
1232 tree *q = &late_attrs;
1234 if (!p)
1235 return NULL_TREE;
1237 for (; *p; )
1239 if (is_late_template_attribute (*p, decl))
1241 ATTR_IS_DEPENDENT (*p) = 1;
1242 *q = *p;
1243 *p = TREE_CHAIN (*p);
1244 q = &TREE_CHAIN (*q);
1245 *q = NULL_TREE;
1247 else
1248 p = &TREE_CHAIN (*p);
1251 return late_attrs;
1254 /* Remove any late attributes from the list in ATTR_P and attach them to
1255 DECL_P. */
1257 static void
1258 save_template_attributes (tree *attr_p, tree *decl_p)
1260 tree *q;
1262 if (attr_p && *attr_p == error_mark_node)
1263 return;
1265 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1266 if (!late_attrs)
1267 return;
1269 if (DECL_P (*decl_p))
1270 q = &DECL_ATTRIBUTES (*decl_p);
1271 else
1272 q = &TYPE_ATTRIBUTES (*decl_p);
1274 tree old_attrs = *q;
1276 /* Merge the late attributes at the beginning with the attribute
1277 list. */
1278 late_attrs = merge_attributes (late_attrs, *q);
1279 *q = late_attrs;
1281 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1283 /* We've added new attributes directly to the main variant, so
1284 now we need to update all of the other variants to include
1285 these new attributes. */
1286 tree variant;
1287 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1288 variant = TYPE_NEXT_VARIANT (variant))
1290 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1291 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1296 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1297 to a typedef which gives a previously anonymous class or enum a name for
1298 linkage purposes. */
1300 bool
1301 attributes_naming_typedef_ok (tree attrs)
1303 for (; attrs; attrs = TREE_CHAIN (attrs))
1305 tree name = get_attribute_name (attrs);
1306 if (is_attribute_p ("vector_size", name))
1307 return false;
1309 return true;
1312 /* Like reconstruct_complex_type, but handle also template trees. */
1314 tree
1315 cp_reconstruct_complex_type (tree type, tree bottom)
1317 tree inner, outer;
1318 bool late_return_type_p = false;
1320 if (TYPE_PTR_P (type))
1322 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1323 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1324 TYPE_REF_CAN_ALIAS_ALL (type));
1326 else if (TREE_CODE (type) == REFERENCE_TYPE)
1328 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1329 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1330 TYPE_REF_CAN_ALIAS_ALL (type));
1332 else if (TREE_CODE (type) == ARRAY_TYPE)
1334 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1335 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1336 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1337 element type qualification will be handled by the recursive
1338 cp_reconstruct_complex_type call and cp_build_qualified_type
1339 for ARRAY_TYPEs changes the element type. */
1340 return outer;
1342 else if (TREE_CODE (type) == FUNCTION_TYPE)
1344 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1345 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1346 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1347 outer = apply_memfn_quals (outer,
1348 type_memfn_quals (type),
1349 type_memfn_rqual (type));
1351 else if (TREE_CODE (type) == METHOD_TYPE)
1353 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1354 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1355 /* The build_method_type_directly() routine prepends 'this' to argument list,
1356 so we must compensate by getting rid of it. */
1357 outer
1358 = build_method_type_directly
1359 (class_of_this_parm (type), inner,
1360 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1362 else if (TREE_CODE (type) == OFFSET_TYPE)
1364 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1365 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1367 else
1368 return bottom;
1370 if (TYPE_ATTRIBUTES (type))
1371 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1372 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1374 if (late_return_type_p)
1375 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1377 return outer;
1380 /* Replaces any constexpr expression that may be into the attributes
1381 arguments with their reduced value. */
1383 static void
1384 cp_check_const_attributes (tree attributes)
1386 if (attributes == error_mark_node)
1387 return;
1389 tree attr;
1390 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1392 tree arg;
1393 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1395 tree expr = TREE_VALUE (arg);
1396 if (EXPR_P (expr))
1397 TREE_VALUE (arg) = maybe_constant_value (expr);
1402 /* Return true if TYPE is an OpenMP mappable type. */
1403 bool
1404 cp_omp_mappable_type (tree type)
1406 /* Mappable type has to be complete. */
1407 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1408 return false;
1409 /* Arrays have mappable type if the elements have mappable type. */
1410 while (TREE_CODE (type) == ARRAY_TYPE)
1411 type = TREE_TYPE (type);
1412 /* A mappable type cannot contain virtual members. */
1413 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1414 return false;
1415 /* All data members must be non-static. */
1416 if (CLASS_TYPE_P (type))
1418 tree field;
1419 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1420 if (TREE_CODE (field) == VAR_DECL)
1421 return false;
1422 /* All fields must have mappable types. */
1423 else if (TREE_CODE (field) == FIELD_DECL
1424 && !cp_omp_mappable_type (TREE_TYPE (field)))
1425 return false;
1427 return true;
1430 /* Like decl_attributes, but handle C++ complexity. */
1432 void
1433 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1435 if (*decl == NULL_TREE || *decl == void_type_node
1436 || *decl == error_mark_node)
1437 return;
1439 /* Add implicit "omp declare target" attribute if requested. */
1440 if (scope_chain->omp_declare_target_attribute
1441 && ((TREE_CODE (*decl) == VAR_DECL
1442 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1443 || TREE_CODE (*decl) == FUNCTION_DECL))
1445 if (TREE_CODE (*decl) == VAR_DECL
1446 && DECL_CLASS_SCOPE_P (*decl))
1447 error ("%q+D static data member inside of declare target directive",
1448 *decl);
1449 else if (TREE_CODE (*decl) == VAR_DECL
1450 && (DECL_FUNCTION_SCOPE_P (*decl)
1451 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1452 error ("%q+D in block scope inside of declare target directive",
1453 *decl);
1454 else if (!processing_template_decl
1455 && TREE_CODE (*decl) == VAR_DECL
1456 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1457 error ("%q+D in declare target directive does not have mappable type",
1458 *decl);
1459 else
1460 attributes = tree_cons (get_identifier ("omp declare target"),
1461 NULL_TREE, attributes);
1464 if (processing_template_decl)
1466 if (check_for_bare_parameter_packs (attributes))
1467 return;
1469 save_template_attributes (&attributes, decl);
1472 cp_check_const_attributes (attributes);
1474 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1475 decl = &DECL_TEMPLATE_RESULT (*decl);
1477 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1479 attributes
1480 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1481 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1482 attributes, flags);
1484 else
1485 decl_attributes (decl, attributes, flags);
1487 if (TREE_CODE (*decl) == TYPE_DECL)
1488 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1490 /* Propagate deprecation out to the template. */
1491 if (TREE_DEPRECATED (*decl))
1492 if (tree ti = get_template_info (*decl))
1494 tree tmpl = TI_TEMPLATE (ti);
1495 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1496 : DECL_TEMPLATE_RESULT (tmpl));
1497 if (*decl == pattern)
1498 TREE_DEPRECATED (tmpl) = true;
1502 /* Walks through the namespace- or function-scope anonymous union
1503 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1504 Returns one of the fields for use in the mangled name. */
1506 static tree
1507 build_anon_union_vars (tree type, tree object)
1509 tree main_decl = NULL_TREE;
1510 tree field;
1512 /* Rather than write the code to handle the non-union case,
1513 just give an error. */
1514 if (TREE_CODE (type) != UNION_TYPE)
1516 error ("anonymous struct not inside named type");
1517 return error_mark_node;
1520 for (field = TYPE_FIELDS (type);
1521 field != NULL_TREE;
1522 field = DECL_CHAIN (field))
1524 tree decl;
1525 tree ref;
1527 if (DECL_ARTIFICIAL (field))
1528 continue;
1529 if (TREE_CODE (field) != FIELD_DECL)
1531 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1532 "have non-static data members", field);
1533 continue;
1536 if (TREE_PRIVATE (field))
1537 permerror (input_location, "private member %q+#D in anonymous union", field);
1538 else if (TREE_PROTECTED (field))
1539 permerror (input_location, "protected member %q+#D in anonymous union", field);
1541 if (processing_template_decl)
1542 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1543 DECL_NAME (field), NULL_TREE);
1544 else
1545 ref = build_class_member_access_expr (object, field, NULL_TREE,
1546 false, tf_warning_or_error);
1548 if (DECL_NAME (field))
1550 tree base;
1552 decl = build_decl (input_location,
1553 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1554 DECL_ANON_UNION_VAR_P (decl) = 1;
1555 DECL_ARTIFICIAL (decl) = 1;
1557 base = get_base_address (object);
1558 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1559 TREE_STATIC (decl) = TREE_STATIC (base);
1560 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1562 SET_DECL_VALUE_EXPR (decl, ref);
1563 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1565 decl = pushdecl (decl);
1567 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1568 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1569 else
1570 decl = 0;
1572 if (main_decl == NULL_TREE)
1573 main_decl = decl;
1576 return main_decl;
1579 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1580 anonymous union, then all members must be laid out together. PUBLIC_P
1581 is nonzero if this union is not declared static. */
1583 void
1584 finish_anon_union (tree anon_union_decl)
1586 tree type;
1587 tree main_decl;
1588 bool public_p;
1590 if (anon_union_decl == error_mark_node)
1591 return;
1593 type = TREE_TYPE (anon_union_decl);
1594 public_p = TREE_PUBLIC (anon_union_decl);
1596 /* The VAR_DECL's context is the same as the TYPE's context. */
1597 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1599 if (TYPE_FIELDS (type) == NULL_TREE)
1600 return;
1602 if (public_p)
1604 error ("namespace-scope anonymous aggregates must be static");
1605 return;
1608 main_decl = build_anon_union_vars (type, anon_union_decl);
1609 if (main_decl == error_mark_node)
1610 return;
1611 if (main_decl == NULL_TREE)
1613 warning (0, "anonymous union with no members");
1614 return;
1617 if (!processing_template_decl)
1619 /* Use main_decl to set the mangled name. */
1620 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1621 maybe_commonize_var (anon_union_decl);
1622 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1623 mangle_decl (anon_union_decl);
1624 DECL_NAME (anon_union_decl) = NULL_TREE;
1627 pushdecl (anon_union_decl);
1628 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1631 /* Auxiliary functions to make type signatures for
1632 `operator new' and `operator delete' correspond to
1633 what compiler will be expecting. */
1635 tree
1636 coerce_new_type (tree type)
1638 int e = 0;
1639 tree args = TYPE_ARG_TYPES (type);
1641 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1643 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1645 e = 1;
1646 error ("%<operator new%> must return type %qT", ptr_type_node);
1649 if (args && args != void_list_node)
1651 if (TREE_PURPOSE (args))
1653 /* [basic.stc.dynamic.allocation]
1655 The first parameter shall not have an associated default
1656 argument. */
1657 error ("the first parameter of %<operator new%> cannot "
1658 "have a default argument");
1659 /* Throw away the default argument. */
1660 TREE_PURPOSE (args) = NULL_TREE;
1663 if (!same_type_p (TREE_VALUE (args), size_type_node))
1665 e = 2;
1666 args = TREE_CHAIN (args);
1669 else
1670 e = 2;
1672 if (e == 2)
1673 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1674 "as first parameter", size_type_node);
1676 switch (e)
1678 case 2:
1679 args = tree_cons (NULL_TREE, size_type_node, args);
1680 /* Fall through. */
1681 case 1:
1682 type = build_exception_variant
1683 (build_function_type (ptr_type_node, args),
1684 TYPE_RAISES_EXCEPTIONS (type));
1685 /* Fall through. */
1686 default:;
1688 return type;
1691 tree
1692 coerce_delete_type (tree type)
1694 int e = 0;
1695 tree args = TYPE_ARG_TYPES (type);
1697 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1699 if (!same_type_p (TREE_TYPE (type), void_type_node))
1701 e = 1;
1702 error ("%<operator delete%> must return type %qT", void_type_node);
1705 if (!args || args == void_list_node
1706 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1708 e = 2;
1709 if (args && args != void_list_node)
1710 args = TREE_CHAIN (args);
1711 error ("%<operator delete%> takes type %qT as first parameter",
1712 ptr_type_node);
1714 switch (e)
1716 case 2:
1717 args = tree_cons (NULL_TREE, ptr_type_node, args);
1718 /* Fall through. */
1719 case 1:
1720 type = build_exception_variant
1721 (build_function_type (void_type_node, args),
1722 TYPE_RAISES_EXCEPTIONS (type));
1723 /* Fall through. */
1724 default:;
1727 return type;
1730 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1731 and mark them as needed. */
1733 static void
1734 mark_vtable_entries (tree decl)
1736 tree fnaddr;
1737 unsigned HOST_WIDE_INT idx;
1739 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1740 idx, fnaddr)
1742 tree fn;
1744 STRIP_NOPS (fnaddr);
1746 if (TREE_CODE (fnaddr) != ADDR_EXPR
1747 && TREE_CODE (fnaddr) != FDESC_EXPR)
1748 /* This entry is an offset: a virtual base class offset, a
1749 virtual call offset, an RTTI offset, etc. */
1750 continue;
1752 fn = TREE_OPERAND (fnaddr, 0);
1753 TREE_ADDRESSABLE (fn) = 1;
1754 /* When we don't have vcall offsets, we output thunks whenever
1755 we output the vtables that contain them. With vcall offsets,
1756 we know all the thunks we'll need when we emit a virtual
1757 function, so we emit the thunks there instead. */
1758 if (DECL_THUNK_P (fn))
1759 use_thunk (fn, /*emit_p=*/0);
1760 mark_used (fn);
1764 /* Set DECL up to have the closest approximation of "initialized common"
1765 linkage available. */
1767 void
1768 comdat_linkage (tree decl)
1770 if (flag_weak)
1771 make_decl_one_only (decl, cxx_comdat_group (decl));
1772 else if (TREE_CODE (decl) == FUNCTION_DECL
1773 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1774 /* We can just emit function and compiler-generated variables
1775 statically; having multiple copies is (for the most part) only
1776 a waste of space.
1778 There are two correctness issues, however: the address of a
1779 template instantiation with external linkage should be the
1780 same, independent of what translation unit asks for the
1781 address, and this will not hold when we emit multiple copies of
1782 the function. However, there's little else we can do.
1784 Also, by default, the typeinfo implementation assumes that
1785 there will be only one copy of the string used as the name for
1786 each type. Therefore, if weak symbols are unavailable, the
1787 run-time library should perform a more conservative check; it
1788 should perform a string comparison, rather than an address
1789 comparison. */
1790 TREE_PUBLIC (decl) = 0;
1791 else
1793 /* Static data member template instantiations, however, cannot
1794 have multiple copies. */
1795 if (DECL_INITIAL (decl) == 0
1796 || DECL_INITIAL (decl) == error_mark_node)
1797 DECL_COMMON (decl) = 1;
1798 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1800 DECL_COMMON (decl) = 1;
1801 DECL_INITIAL (decl) = error_mark_node;
1803 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1805 /* We can't do anything useful; leave vars for explicit
1806 instantiation. */
1807 DECL_EXTERNAL (decl) = 1;
1808 DECL_NOT_REALLY_EXTERN (decl) = 0;
1812 DECL_COMDAT (decl) = 1;
1815 /* For win32 we also want to put explicit instantiations in
1816 linkonce sections, so that they will be merged with implicit
1817 instantiations; otherwise we get duplicate symbol errors.
1818 For Darwin we do not want explicit instantiations to be
1819 linkonce. */
1821 void
1822 maybe_make_one_only (tree decl)
1824 /* We used to say that this was not necessary on targets that support weak
1825 symbols, because the implicit instantiations will defer to the explicit
1826 one. However, that's not actually the case in SVR4; a strong definition
1827 after a weak one is an error. Also, not making explicit
1828 instantiations one_only means that we can end up with two copies of
1829 some template instantiations. */
1830 if (! flag_weak)
1831 return;
1833 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1834 we can get away with not emitting them if they aren't used. We need
1835 to for variables so that cp_finish_decl will update their linkage,
1836 because their DECL_INITIAL may not have been set properly yet. */
1838 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1839 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1840 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1842 make_decl_one_only (decl, cxx_comdat_group (decl));
1844 if (VAR_P (decl))
1846 varpool_node *node = varpool_node::get_create (decl);
1847 DECL_COMDAT (decl) = 1;
1848 /* Mark it needed so we don't forget to emit it. */
1849 node->forced_by_abi = true;
1850 TREE_USED (decl) = 1;
1855 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1856 This predicate will give the right answer during parsing of the
1857 function, which other tests may not. */
1859 bool
1860 vague_linkage_p (tree decl)
1862 if (!TREE_PUBLIC (decl))
1864 gcc_checking_assert (!DECL_COMDAT (decl));
1865 return false;
1867 /* Unfortunately, import_export_decl has not always been called
1868 before the function is processed, so we cannot simply check
1869 DECL_COMDAT. */
1870 if (DECL_COMDAT (decl)
1871 || (TREE_CODE (decl) == FUNCTION_DECL
1872 && DECL_DECLARED_INLINE_P (decl))
1873 || (DECL_LANG_SPECIFIC (decl)
1874 && DECL_TEMPLATE_INSTANTIATION (decl)))
1875 return true;
1876 else if (DECL_FUNCTION_SCOPE_P (decl))
1877 /* A local static in an inline effectively has vague linkage. */
1878 return (TREE_STATIC (decl)
1879 && vague_linkage_p (DECL_CONTEXT (decl)));
1880 else
1881 return false;
1884 /* Determine whether or not we want to specifically import or export CTYPE,
1885 using various heuristics. */
1887 static void
1888 import_export_class (tree ctype)
1890 /* -1 for imported, 1 for exported. */
1891 int import_export = 0;
1893 /* It only makes sense to call this function at EOF. The reason is
1894 that this function looks at whether or not the first non-inline
1895 non-abstract virtual member function has been defined in this
1896 translation unit. But, we can't possibly know that until we've
1897 seen the entire translation unit. */
1898 gcc_assert (at_eof);
1900 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1901 return;
1903 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1904 we will have CLASSTYPE_INTERFACE_ONLY set but not
1905 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1906 heuristic because someone will supply a #pragma implementation
1907 elsewhere, and deducing it here would produce a conflict. */
1908 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1909 return;
1911 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1912 import_export = -1;
1913 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1914 import_export = 1;
1915 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1916 && !flag_implicit_templates)
1917 /* For a template class, without -fimplicit-templates, check the
1918 repository. If the virtual table is assigned to this
1919 translation unit, then export the class; otherwise, import
1920 it. */
1921 import_export = repo_export_class_p (ctype) ? 1 : -1;
1922 else if (TYPE_POLYMORPHIC_P (ctype))
1924 /* The ABI specifies that the virtual table and associated
1925 information are emitted with the key method, if any. */
1926 tree method = CLASSTYPE_KEY_METHOD (ctype);
1927 /* If weak symbol support is not available, then we must be
1928 careful not to emit the vtable when the key function is
1929 inline. An inline function can be defined in multiple
1930 translation units. If we were to emit the vtable in each
1931 translation unit containing a definition, we would get
1932 multiple definition errors at link-time. */
1933 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1934 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1937 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1938 a definition anywhere else. */
1939 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1940 import_export = 0;
1942 /* Allow back ends the chance to overrule the decision. */
1943 if (targetm.cxx.import_export_class)
1944 import_export = targetm.cxx.import_export_class (ctype, import_export);
1946 if (import_export)
1948 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1949 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1953 /* Return true if VAR has already been provided to the back end; in that
1954 case VAR should not be modified further by the front end. */
1955 static bool
1956 var_finalized_p (tree var)
1958 return varpool_node::get_create (var)->definition;
1961 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1962 must be emitted in this translation unit. Mark it as such. */
1964 void
1965 mark_needed (tree decl)
1967 TREE_USED (decl) = 1;
1968 if (TREE_CODE (decl) == FUNCTION_DECL)
1970 /* Extern inline functions don't become needed when referenced.
1971 If we know a method will be emitted in other TU and no new
1972 functions can be marked reachable, just use the external
1973 definition. */
1974 struct cgraph_node *node = cgraph_node::get_create (decl);
1975 node->forced_by_abi = true;
1977 /* #pragma interface and -frepo code can call mark_needed for
1978 maybe-in-charge 'tors; mark the clones as well. */
1979 tree clone;
1980 FOR_EACH_CLONE (clone, decl)
1981 mark_needed (clone);
1983 else if (TREE_CODE (decl) == VAR_DECL)
1985 varpool_node *node = varpool_node::get_create (decl);
1986 /* C++ frontend use mark_decl_references to force COMDAT variables
1987 to be output that might appear dead otherwise. */
1988 node->forced_by_abi = true;
1992 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1993 returns true if a definition of this entity should be provided in
1994 this object file. Callers use this function to determine whether
1995 or not to let the back end know that a definition of DECL is
1996 available in this translation unit. */
1998 bool
1999 decl_needed_p (tree decl)
2001 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2002 /* This function should only be called at the end of the translation
2003 unit. We cannot be sure of whether or not something will be
2004 COMDAT until that point. */
2005 gcc_assert (at_eof);
2007 /* All entities with external linkage that are not COMDAT/EXTERN should be
2008 emitted; they may be referred to from other object files. */
2009 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2010 return true;
2011 /* Functions marked "dllexport" must be emitted so that they are
2012 visible to other DLLs. */
2013 if (flag_keep_inline_dllexport
2014 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2015 return true;
2017 /* When not optimizing, do not bother to produce definitions for extern
2018 symbols. */
2019 if (DECL_REALLY_EXTERN (decl)
2020 && ((TREE_CODE (decl) != FUNCTION_DECL
2021 && !optimize)
2022 || (TREE_CODE (decl) == FUNCTION_DECL
2023 && !opt_for_fn (decl, optimize)))
2024 && !lookup_attribute ("always_inline", decl))
2025 return false;
2027 /* If this entity was used, let the back end see it; it will decide
2028 whether or not to emit it into the object file. */
2029 if (TREE_USED (decl))
2030 return true;
2031 /* Virtual functions might be needed for devirtualization. */
2032 if (flag_devirtualize
2033 && TREE_CODE (decl) == FUNCTION_DECL
2034 && DECL_VIRTUAL_P (decl))
2035 return true;
2036 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2037 reference to DECL might cause it to be emitted later. */
2038 return false;
2041 /* If necessary, write out the vtables for the dynamic class CTYPE.
2042 Returns true if any vtables were emitted. */
2044 static bool
2045 maybe_emit_vtables (tree ctype)
2047 tree vtbl;
2048 tree primary_vtbl;
2049 int needed = 0;
2050 varpool_node *current = NULL, *last = NULL;
2052 /* If the vtables for this class have already been emitted there is
2053 nothing more to do. */
2054 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2055 if (var_finalized_p (primary_vtbl))
2056 return false;
2057 /* Ignore dummy vtables made by get_vtable_decl. */
2058 if (TREE_TYPE (primary_vtbl) == void_type_node)
2059 return false;
2061 /* On some targets, we cannot determine the key method until the end
2062 of the translation unit -- which is when this function is
2063 called. */
2064 if (!targetm.cxx.key_method_may_be_inline ())
2065 determine_key_method (ctype);
2067 /* See if any of the vtables are needed. */
2068 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2070 import_export_decl (vtbl);
2071 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2072 needed = 1;
2074 if (!needed)
2076 /* If the references to this class' vtables are optimized away,
2077 still emit the appropriate debugging information. See
2078 dfs_debug_mark. */
2079 if (DECL_COMDAT (primary_vtbl)
2080 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2081 note_debug_info_needed (ctype);
2082 return false;
2085 /* The ABI requires that we emit all of the vtables if we emit any
2086 of them. */
2087 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2089 /* Mark entities references from the virtual table as used. */
2090 mark_vtable_entries (vtbl);
2092 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2094 vec<tree, va_gc> *cleanups = NULL;
2095 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2096 LOOKUP_NORMAL);
2098 /* It had better be all done at compile-time. */
2099 gcc_assert (!expr && !cleanups);
2102 /* Write it out. */
2103 DECL_EXTERNAL (vtbl) = 0;
2104 rest_of_decl_compilation (vtbl, 1, 1);
2106 /* Because we're only doing syntax-checking, we'll never end up
2107 actually marking the variable as written. */
2108 if (flag_syntax_only)
2109 TREE_ASM_WRITTEN (vtbl) = 1;
2110 else if (DECL_ONE_ONLY (vtbl))
2112 current = varpool_node::get_create (vtbl);
2113 if (last)
2114 current->add_to_same_comdat_group (last);
2115 last = current;
2119 /* Since we're writing out the vtable here, also write the debug
2120 info. */
2121 note_debug_info_needed (ctype);
2123 return true;
2126 /* A special return value from type_visibility meaning internal
2127 linkage. */
2129 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2131 /* walk_tree helper function for type_visibility. */
2133 static tree
2134 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2136 int *vis_p = (int *)data;
2137 if (! TYPE_P (*tp))
2139 *walk_subtrees = 0;
2141 else if (OVERLOAD_TYPE_P (*tp)
2142 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2144 *vis_p = VISIBILITY_ANON;
2145 return *tp;
2147 else if (CLASS_TYPE_P (*tp)
2148 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2149 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2150 return NULL;
2153 /* Returns the visibility of TYPE, which is the minimum visibility of its
2154 component types. */
2156 static int
2157 type_visibility (tree type)
2159 int vis = VISIBILITY_DEFAULT;
2160 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2161 return vis;
2164 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2165 specified (or if VISIBILITY is static). If TMPL is true, this
2166 constraint is for a template argument, and takes precedence
2167 over explicitly-specified visibility on the template. */
2169 static void
2170 constrain_visibility (tree decl, int visibility, bool tmpl)
2172 if (visibility == VISIBILITY_ANON)
2174 /* extern "C" declarations aren't affected by the anonymous
2175 namespace. */
2176 if (!DECL_EXTERN_C_P (decl))
2178 TREE_PUBLIC (decl) = 0;
2179 DECL_WEAK (decl) = 0;
2180 DECL_COMMON (decl) = 0;
2181 DECL_COMDAT (decl) = false;
2182 if (TREE_CODE (decl) == FUNCTION_DECL
2183 || TREE_CODE (decl) == VAR_DECL)
2185 struct symtab_node *snode = symtab_node::get (decl);
2187 if (snode)
2188 snode->set_comdat_group (NULL);
2190 DECL_INTERFACE_KNOWN (decl) = 1;
2191 if (DECL_LANG_SPECIFIC (decl))
2192 DECL_NOT_REALLY_EXTERN (decl) = 1;
2195 else if (visibility > DECL_VISIBILITY (decl)
2196 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2198 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2199 /* This visibility was not specified. */
2200 DECL_VISIBILITY_SPECIFIED (decl) = false;
2204 /* Constrain the visibility of DECL based on the visibility of its template
2205 arguments. */
2207 static void
2208 constrain_visibility_for_template (tree decl, tree targs)
2210 /* If this is a template instantiation, check the innermost
2211 template args for visibility constraints. The outer template
2212 args are covered by the class check. */
2213 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2214 int i;
2215 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2217 int vis = 0;
2219 tree arg = TREE_VEC_ELT (args, i-1);
2220 if (TYPE_P (arg))
2221 vis = type_visibility (arg);
2222 else
2224 if (REFERENCE_REF_P (arg))
2225 arg = TREE_OPERAND (arg, 0);
2226 if (TREE_TYPE (arg))
2227 STRIP_NOPS (arg);
2228 if (TREE_CODE (arg) == ADDR_EXPR)
2229 arg = TREE_OPERAND (arg, 0);
2230 if (VAR_OR_FUNCTION_DECL_P (arg))
2232 if (! TREE_PUBLIC (arg))
2233 vis = VISIBILITY_ANON;
2234 else
2235 vis = DECL_VISIBILITY (arg);
2238 if (vis)
2239 constrain_visibility (decl, vis, true);
2243 /* Like c_determine_visibility, but with additional C++-specific
2244 behavior.
2246 Function-scope entities can rely on the function's visibility because
2247 it is set in start_preparsed_function.
2249 Class-scope entities cannot rely on the class's visibility until the end
2250 of the enclosing class definition.
2252 Note that because namespaces have multiple independent definitions,
2253 namespace visibility is handled elsewhere using the #pragma visibility
2254 machinery rather than by decorating the namespace declaration.
2256 The goal is for constraints from the type to give a diagnostic, and
2257 other constraints to be applied silently. */
2259 void
2260 determine_visibility (tree decl)
2262 tree class_type = NULL_TREE;
2263 bool use_template;
2264 bool orig_visibility_specified;
2265 enum symbol_visibility orig_visibility;
2267 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2269 /* Only relevant for names with external linkage. */
2270 if (!TREE_PUBLIC (decl))
2271 return;
2273 /* Cloned constructors and destructors get the same visibility as
2274 the underlying function. That should be set up in
2275 maybe_clone_body. */
2276 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2278 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2279 orig_visibility = DECL_VISIBILITY (decl);
2281 if (TREE_CODE (decl) == TYPE_DECL)
2283 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2284 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2285 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2286 use_template = 1;
2287 else
2288 use_template = 0;
2290 else if (DECL_LANG_SPECIFIC (decl))
2291 use_template = DECL_USE_TEMPLATE (decl);
2292 else
2293 use_template = 0;
2295 /* If DECL is a member of a class, visibility specifiers on the
2296 class can influence the visibility of the DECL. */
2297 if (DECL_CLASS_SCOPE_P (decl))
2298 class_type = DECL_CONTEXT (decl);
2299 else
2301 /* Not a class member. */
2303 /* Virtual tables have DECL_CONTEXT set to their associated class,
2304 so they are automatically handled above. */
2305 gcc_assert (!VAR_P (decl)
2306 || !DECL_VTABLE_OR_VTT_P (decl));
2308 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2310 /* Local statics and classes get the visibility of their
2311 containing function by default, except that
2312 -fvisibility-inlines-hidden doesn't affect them. */
2313 tree fn = DECL_CONTEXT (decl);
2314 if (DECL_VISIBILITY_SPECIFIED (fn))
2316 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2317 DECL_VISIBILITY_SPECIFIED (decl) =
2318 DECL_VISIBILITY_SPECIFIED (fn);
2320 else
2322 if (DECL_CLASS_SCOPE_P (fn))
2323 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2324 else if (determine_hidden_inline (fn))
2326 DECL_VISIBILITY (decl) = default_visibility;
2327 DECL_VISIBILITY_SPECIFIED (decl) =
2328 visibility_options.inpragma;
2330 else
2332 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2333 DECL_VISIBILITY_SPECIFIED (decl) =
2334 DECL_VISIBILITY_SPECIFIED (fn);
2338 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2339 but have no TEMPLATE_INFO, so don't try to check it. */
2340 use_template = 0;
2342 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2343 && flag_visibility_ms_compat)
2345 /* Under -fvisibility-ms-compat, types are visible by default,
2346 even though their contents aren't. */
2347 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2348 int underlying_vis = type_visibility (underlying_type);
2349 if (underlying_vis == VISIBILITY_ANON
2350 || (CLASS_TYPE_P (underlying_type)
2351 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2352 constrain_visibility (decl, underlying_vis, false);
2353 else
2354 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2356 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2358 /* tinfo visibility is based on the type it's for. */
2359 constrain_visibility
2360 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2362 /* Give the target a chance to override the visibility associated
2363 with DECL. */
2364 if (TREE_PUBLIC (decl)
2365 && !DECL_REALLY_EXTERN (decl)
2366 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2367 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2368 targetm.cxx.determine_class_data_visibility (decl);
2370 else if (use_template)
2371 /* Template instantiations and specializations get visibility based
2372 on their template unless they override it with an attribute. */;
2373 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2375 if (determine_hidden_inline (decl))
2376 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2377 else
2379 /* Set default visibility to whatever the user supplied with
2380 #pragma GCC visibility or a namespace visibility attribute. */
2381 DECL_VISIBILITY (decl) = default_visibility;
2382 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2387 if (use_template)
2389 /* If the specialization doesn't specify visibility, use the
2390 visibility from the template. */
2391 tree tinfo = get_template_info (decl);
2392 tree args = TI_ARGS (tinfo);
2393 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2394 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2395 : DECL_ATTRIBUTES (decl));
2397 if (args != error_mark_node)
2399 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2401 if (!DECL_VISIBILITY_SPECIFIED (decl))
2403 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2404 && determine_hidden_inline (decl))
2405 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2406 else
2408 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2409 DECL_VISIBILITY_SPECIFIED (decl)
2410 = DECL_VISIBILITY_SPECIFIED (pattern);
2414 if (args
2415 /* Template argument visibility outweighs #pragma or namespace
2416 visibility, but not an explicit attribute. */
2417 && !lookup_attribute ("visibility", attribs))
2419 int depth = TMPL_ARGS_DEPTH (args);
2420 if (DECL_VISIBILITY_SPECIFIED (decl))
2422 /* A class template member with explicit visibility
2423 overrides the class visibility, so we need to apply
2424 all the levels of template args directly. */
2425 int i;
2426 for (i = 1; i <= depth; ++i)
2428 tree lev = TMPL_ARGS_LEVEL (args, i);
2429 constrain_visibility_for_template (decl, lev);
2432 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2433 /* Limit visibility based on its template arguments. */
2434 constrain_visibility_for_template (decl, args);
2439 if (class_type)
2440 determine_visibility_from_class (decl, class_type);
2442 if (decl_anon_ns_mem_p (decl))
2443 /* Names in an anonymous namespace get internal linkage.
2444 This might change once we implement export. */
2445 constrain_visibility (decl, VISIBILITY_ANON, false);
2446 else if (TREE_CODE (decl) != TYPE_DECL)
2448 /* Propagate anonymity from type to decl. */
2449 int tvis = type_visibility (TREE_TYPE (decl));
2450 if (tvis == VISIBILITY_ANON
2451 || ! DECL_VISIBILITY_SPECIFIED (decl))
2452 constrain_visibility (decl, tvis, false);
2454 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2455 /* DR 757: A type without linkage shall not be used as the type of a
2456 variable or function with linkage, unless
2457 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2458 o the variable or function is not used (3.2 [basic.def.odr]) or is
2459 defined in the same translation unit.
2461 Since non-extern "C" decls need to be defined in the same
2462 translation unit, we can make the type internal. */
2463 constrain_visibility (decl, VISIBILITY_ANON, false);
2465 /* If visibility changed and DECL already has DECL_RTL, ensure
2466 symbol flags are updated. */
2467 if ((DECL_VISIBILITY (decl) != orig_visibility
2468 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2469 && ((VAR_P (decl) && TREE_STATIC (decl))
2470 || TREE_CODE (decl) == FUNCTION_DECL)
2471 && DECL_RTL_SET_P (decl))
2472 make_decl_rtl (decl);
2475 /* By default, static data members and function members receive
2476 the visibility of their containing class. */
2478 static void
2479 determine_visibility_from_class (tree decl, tree class_type)
2481 if (DECL_VISIBILITY_SPECIFIED (decl))
2482 return;
2484 if (determine_hidden_inline (decl))
2485 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2486 else
2488 /* Default to the class visibility. */
2489 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2490 DECL_VISIBILITY_SPECIFIED (decl)
2491 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2494 /* Give the target a chance to override the visibility associated
2495 with DECL. */
2496 if (VAR_P (decl)
2497 && (DECL_TINFO_P (decl)
2498 || (DECL_VTABLE_OR_VTT_P (decl)
2499 /* Construction virtual tables are not exported because
2500 they cannot be referred to from other object files;
2501 their name is not standardized by the ABI. */
2502 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2503 && TREE_PUBLIC (decl)
2504 && !DECL_REALLY_EXTERN (decl)
2505 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2506 targetm.cxx.determine_class_data_visibility (decl);
2509 /* Returns true iff DECL is an inline that should get hidden visibility
2510 because of -fvisibility-inlines-hidden. */
2512 static bool
2513 determine_hidden_inline (tree decl)
2515 return (visibility_options.inlines_hidden
2516 /* Don't do this for inline templates; specializations might not be
2517 inline, and we don't want them to inherit the hidden
2518 visibility. We'll set it here for all inline instantiations. */
2519 && !processing_template_decl
2520 && TREE_CODE (decl) == FUNCTION_DECL
2521 && DECL_DECLARED_INLINE_P (decl)
2522 && (! DECL_LANG_SPECIFIC (decl)
2523 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2526 /* Constrain the visibility of a class TYPE based on the visibility of its
2527 field types. Warn if any fields require lesser visibility. */
2529 void
2530 constrain_class_visibility (tree type)
2532 tree binfo;
2533 tree t;
2534 int i;
2536 int vis = type_visibility (type);
2538 if (vis == VISIBILITY_ANON
2539 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2540 return;
2542 /* Don't warn about visibility if the class has explicit visibility. */
2543 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2544 vis = VISIBILITY_INTERNAL;
2546 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2547 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2549 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2550 int subvis = type_visibility (ftype);
2552 if (subvis == VISIBILITY_ANON)
2554 if (!in_main_input_context ())
2555 warning (0, "\
2556 %qT has a field %qD whose type uses the anonymous namespace",
2557 type, t);
2559 else if (MAYBE_CLASS_TYPE_P (ftype)
2560 && vis < VISIBILITY_HIDDEN
2561 && subvis >= VISIBILITY_HIDDEN)
2562 warning (OPT_Wattributes, "\
2563 %qT declared with greater visibility than the type of its field %qD",
2564 type, t);
2567 binfo = TYPE_BINFO (type);
2568 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2570 int subvis = type_visibility (TREE_TYPE (t));
2572 if (subvis == VISIBILITY_ANON)
2574 if (!in_main_input_context())
2575 warning (0, "\
2576 %qT has a base %qT whose type uses the anonymous namespace",
2577 type, TREE_TYPE (t));
2579 else if (vis < VISIBILITY_HIDDEN
2580 && subvis >= VISIBILITY_HIDDEN)
2581 warning (OPT_Wattributes, "\
2582 %qT declared with greater visibility than its base %qT",
2583 type, TREE_TYPE (t));
2587 /* Functions for adjusting the visibility of a tagged type and its nested
2588 types and declarations when it gets a name for linkage purposes from a
2589 typedef. */
2591 static void bt_reset_linkage_1 (binding_entry, void *);
2592 static void bt_reset_linkage_2 (binding_entry, void *);
2594 /* First reset the visibility of all the types. */
2596 static void
2597 reset_type_linkage_1 (tree type)
2599 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2600 if (CLASS_TYPE_P (type))
2601 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2602 bt_reset_linkage_1, NULL);
2604 static void
2605 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2607 reset_type_linkage_1 (b->type);
2610 /* Then reset the visibility of any static data members or member
2611 functions that use those types. */
2613 static void
2614 reset_decl_linkage (tree decl)
2616 if (TREE_PUBLIC (decl))
2617 return;
2618 if (DECL_CLONED_FUNCTION_P (decl))
2619 return;
2620 TREE_PUBLIC (decl) = true;
2621 DECL_INTERFACE_KNOWN (decl) = false;
2622 determine_visibility (decl);
2623 tentative_decl_linkage (decl);
2625 static void
2626 reset_type_linkage_2 (tree type)
2628 if (CLASS_TYPE_P (type))
2630 if (tree vt = CLASSTYPE_VTABLES (type))
2632 tree name = mangle_vtbl_for_type (type);
2633 DECL_NAME (vt) = name;
2634 SET_DECL_ASSEMBLER_NAME (vt, name);
2635 reset_decl_linkage (vt);
2637 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2639 tree name = mangle_typeinfo_for_type (type);
2640 DECL_NAME (ti) = name;
2641 SET_DECL_ASSEMBLER_NAME (ti, name);
2642 TREE_TYPE (name) = type;
2643 reset_decl_linkage (ti);
2645 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2646 if (TREE_CODE (m) == VAR_DECL)
2647 reset_decl_linkage (m);
2648 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2650 reset_decl_linkage (m);
2651 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (m))
2652 /* Also update its name, for cxx_dwarf_name. */
2653 DECL_NAME (m) = TYPE_IDENTIFIER (type);
2655 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2656 bt_reset_linkage_2, NULL);
2659 static void
2660 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2662 reset_type_linkage_2 (b->type);
2664 void
2665 reset_type_linkage (tree type)
2667 reset_type_linkage_1 (type);
2668 reset_type_linkage_2 (type);
2671 /* Set up our initial idea of what the linkage of DECL should be. */
2673 void
2674 tentative_decl_linkage (tree decl)
2676 if (DECL_INTERFACE_KNOWN (decl))
2677 /* We've already made a decision as to how this function will
2678 be handled. */;
2679 else if (vague_linkage_p (decl))
2681 if (TREE_CODE (decl) == FUNCTION_DECL
2682 && decl_defined_p (decl))
2684 DECL_EXTERNAL (decl) = 1;
2685 DECL_NOT_REALLY_EXTERN (decl) = 1;
2686 note_vague_linkage_fn (decl);
2687 /* A non-template inline function with external linkage will
2688 always be COMDAT. As we must eventually determine the
2689 linkage of all functions, and as that causes writes to
2690 the data mapped in from the PCH file, it's advantageous
2691 to mark the functions at this point. */
2692 if (DECL_DECLARED_INLINE_P (decl)
2693 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2694 || DECL_DEFAULTED_FN (decl)))
2696 /* This function must have external linkage, as
2697 otherwise DECL_INTERFACE_KNOWN would have been
2698 set. */
2699 gcc_assert (TREE_PUBLIC (decl));
2700 comdat_linkage (decl);
2701 DECL_INTERFACE_KNOWN (decl) = 1;
2704 else if (TREE_CODE (decl) == VAR_DECL)
2705 maybe_commonize_var (decl);
2709 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2710 for DECL has not already been determined, do so now by setting
2711 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2712 function is called entities with vague linkage whose definitions
2713 are available must have TREE_PUBLIC set.
2715 If this function decides to place DECL in COMDAT, it will set
2716 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2717 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2718 callers defer that decision until it is clear that DECL is actually
2719 required. */
2721 void
2722 import_export_decl (tree decl)
2724 int emit_p;
2725 bool comdat_p;
2726 bool import_p;
2727 tree class_type = NULL_TREE;
2729 if (DECL_INTERFACE_KNOWN (decl))
2730 return;
2732 /* We cannot determine what linkage to give to an entity with vague
2733 linkage until the end of the file. For example, a virtual table
2734 for a class will be defined if and only if the key method is
2735 defined in this translation unit. As a further example, consider
2736 that when compiling a translation unit that uses PCH file with
2737 "-frepo" it would be incorrect to make decisions about what
2738 entities to emit when building the PCH; those decisions must be
2739 delayed until the repository information has been processed. */
2740 gcc_assert (at_eof);
2741 /* Object file linkage for explicit instantiations is handled in
2742 mark_decl_instantiated. For static variables in functions with
2743 vague linkage, maybe_commonize_var is used.
2745 Therefore, the only declarations that should be provided to this
2746 function are those with external linkage that are:
2748 * implicit instantiations of function templates
2750 * inline function
2752 * implicit instantiations of static data members of class
2753 templates
2755 * virtual tables
2757 * typeinfo objects
2759 Furthermore, all entities that reach this point must have a
2760 definition available in this translation unit.
2762 The following assertions check these conditions. */
2763 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2764 /* Any code that creates entities with TREE_PUBLIC cleared should
2765 also set DECL_INTERFACE_KNOWN. */
2766 gcc_assert (TREE_PUBLIC (decl));
2767 if (TREE_CODE (decl) == FUNCTION_DECL)
2768 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2769 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2770 || DECL_DECLARED_INLINE_P (decl));
2771 else
2772 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2773 || DECL_VTABLE_OR_VTT_P (decl)
2774 || DECL_TINFO_P (decl));
2775 /* Check that a definition of DECL is available in this translation
2776 unit. */
2777 gcc_assert (!DECL_REALLY_EXTERN (decl));
2779 /* Assume that DECL will not have COMDAT linkage. */
2780 comdat_p = false;
2781 /* Assume that DECL will not be imported into this translation
2782 unit. */
2783 import_p = false;
2785 /* See if the repository tells us whether or not to emit DECL in
2786 this translation unit. */
2787 emit_p = repo_emit_p (decl);
2788 if (emit_p == 0)
2789 import_p = true;
2790 else if (emit_p == 1)
2792 /* The repository indicates that this entity should be defined
2793 here. Make sure the back end honors that request. */
2794 mark_needed (decl);
2795 /* Output the definition as an ordinary strong definition. */
2796 DECL_EXTERNAL (decl) = 0;
2797 DECL_INTERFACE_KNOWN (decl) = 1;
2798 return;
2801 if (import_p)
2802 /* We have already decided what to do with this DECL; there is no
2803 need to check anything further. */
2805 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2807 class_type = DECL_CONTEXT (decl);
2808 import_export_class (class_type);
2809 if (TYPE_FOR_JAVA (class_type))
2810 import_p = true;
2811 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2812 && CLASSTYPE_INTERFACE_ONLY (class_type))
2813 import_p = true;
2814 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2815 && !CLASSTYPE_USE_TEMPLATE (class_type)
2816 && CLASSTYPE_KEY_METHOD (class_type)
2817 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2818 /* The ABI requires that all virtual tables be emitted with
2819 COMDAT linkage. However, on systems where COMDAT symbols
2820 don't show up in the table of contents for a static
2821 archive, or on systems without weak symbols (where we
2822 approximate COMDAT linkage by using internal linkage), the
2823 linker will report errors about undefined symbols because
2824 it will not see the virtual table definition. Therefore,
2825 in the case that we know that the virtual table will be
2826 emitted in only one translation unit, we make the virtual
2827 table an ordinary definition with external linkage. */
2828 DECL_EXTERNAL (decl) = 0;
2829 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2831 /* CLASS_TYPE is being exported from this translation unit,
2832 so DECL should be defined here. */
2833 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2834 /* If a class is declared in a header with the "extern
2835 template" extension, then it will not be instantiated,
2836 even in translation units that would normally require
2837 it. Often such classes are explicitly instantiated in
2838 one translation unit. Therefore, the explicit
2839 instantiation must be made visible to other translation
2840 units. */
2841 DECL_EXTERNAL (decl) = 0;
2842 else
2844 /* The generic C++ ABI says that class data is always
2845 COMDAT, even if there is a key function. Some
2846 variants (e.g., the ARM EABI) says that class data
2847 only has COMDAT linkage if the class data might be
2848 emitted in more than one translation unit. When the
2849 key method can be inline and is inline, we still have
2850 to arrange for comdat even though
2851 class_data_always_comdat is false. */
2852 if (!CLASSTYPE_KEY_METHOD (class_type)
2853 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2854 || targetm.cxx.class_data_always_comdat ())
2856 /* The ABI requires COMDAT linkage. Normally, we
2857 only emit COMDAT things when they are needed;
2858 make sure that we realize that this entity is
2859 indeed needed. */
2860 comdat_p = true;
2861 mark_needed (decl);
2865 else if (!flag_implicit_templates
2866 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2867 import_p = true;
2868 else
2869 comdat_p = true;
2871 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2873 tree type = TREE_TYPE (DECL_NAME (decl));
2874 if (CLASS_TYPE_P (type))
2876 class_type = type;
2877 import_export_class (type);
2878 if (CLASSTYPE_INTERFACE_KNOWN (type)
2879 && TYPE_POLYMORPHIC_P (type)
2880 && CLASSTYPE_INTERFACE_ONLY (type)
2881 /* If -fno-rtti was specified, then we cannot be sure
2882 that RTTI information will be emitted with the
2883 virtual table of the class, so we must emit it
2884 wherever it is used. */
2885 && flag_rtti)
2886 import_p = true;
2887 else
2889 if (CLASSTYPE_INTERFACE_KNOWN (type)
2890 && !CLASSTYPE_INTERFACE_ONLY (type))
2892 comdat_p = (targetm.cxx.class_data_always_comdat ()
2893 || (CLASSTYPE_KEY_METHOD (type)
2894 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2895 mark_needed (decl);
2896 if (!flag_weak)
2898 comdat_p = false;
2899 DECL_EXTERNAL (decl) = 0;
2902 else
2903 comdat_p = true;
2906 else
2907 comdat_p = true;
2909 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2911 /* DECL is an implicit instantiation of a function or static
2912 data member. */
2913 if ((flag_implicit_templates
2914 && !flag_use_repository)
2915 || (flag_implicit_inline_templates
2916 && TREE_CODE (decl) == FUNCTION_DECL
2917 && DECL_DECLARED_INLINE_P (decl)))
2918 comdat_p = true;
2919 else
2920 /* If we are not implicitly generating templates, then mark
2921 this entity as undefined in this translation unit. */
2922 import_p = true;
2924 else if (DECL_FUNCTION_MEMBER_P (decl))
2926 if (!DECL_DECLARED_INLINE_P (decl))
2928 tree ctype = DECL_CONTEXT (decl);
2929 import_export_class (ctype);
2930 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2932 DECL_NOT_REALLY_EXTERN (decl)
2933 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2934 || (DECL_DECLARED_INLINE_P (decl)
2935 && ! flag_implement_inlines
2936 && !DECL_VINDEX (decl)));
2938 if (!DECL_NOT_REALLY_EXTERN (decl))
2939 DECL_EXTERNAL (decl) = 1;
2941 /* Always make artificials weak. */
2942 if (DECL_ARTIFICIAL (decl) && flag_weak)
2943 comdat_p = true;
2944 else
2945 maybe_make_one_only (decl);
2948 else
2949 comdat_p = true;
2951 else
2952 comdat_p = true;
2954 if (import_p)
2956 /* If we are importing DECL into this translation unit, mark is
2957 an undefined here. */
2958 DECL_EXTERNAL (decl) = 1;
2959 DECL_NOT_REALLY_EXTERN (decl) = 0;
2961 else if (comdat_p)
2963 /* If we decided to put DECL in COMDAT, mark it accordingly at
2964 this point. */
2965 comdat_linkage (decl);
2968 DECL_INTERFACE_KNOWN (decl) = 1;
2971 /* Return an expression that performs the destruction of DECL, which
2972 must be a VAR_DECL whose type has a non-trivial destructor, or is
2973 an array whose (innermost) elements have a non-trivial destructor. */
2975 tree
2976 build_cleanup (tree decl)
2978 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2979 gcc_assert (clean != NULL_TREE);
2980 return clean;
2983 /* Returns the initialization guard variable for the variable DECL,
2984 which has static storage duration. */
2986 tree
2987 get_guard (tree decl)
2989 tree sname;
2990 tree guard;
2992 sname = mangle_guard_variable (decl);
2993 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2994 if (! guard)
2996 tree guard_type;
2998 /* We use a type that is big enough to contain a mutex as well
2999 as an integer counter. */
3000 guard_type = targetm.cxx.guard_type ();
3001 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3002 VAR_DECL, sname, guard_type);
3004 /* The guard should have the same linkage as what it guards. */
3005 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3006 TREE_STATIC (guard) = TREE_STATIC (decl);
3007 DECL_COMMON (guard) = DECL_COMMON (decl);
3008 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3009 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3010 if (DECL_ONE_ONLY (decl))
3011 make_decl_one_only (guard, cxx_comdat_group (guard));
3012 if (TREE_PUBLIC (decl))
3013 DECL_WEAK (guard) = DECL_WEAK (decl);
3014 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3015 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3017 DECL_ARTIFICIAL (guard) = 1;
3018 DECL_IGNORED_P (guard) = 1;
3019 TREE_USED (guard) = 1;
3020 pushdecl_top_level_and_finish (guard, NULL_TREE);
3022 return guard;
3025 /* Return an atomic load of src with the appropriate memory model. */
3027 static tree
3028 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3030 tree ptr_type = build_pointer_type (char_type_node);
3031 tree mem_model = build_int_cst (integer_type_node, model);
3032 tree t, addr, val;
3033 unsigned int size;
3034 int fncode;
3036 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3038 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3039 t = builtin_decl_implicit ((enum built_in_function) fncode);
3041 addr = build1 (ADDR_EXPR, ptr_type, src);
3042 val = build_call_expr (t, 2, addr, mem_model);
3043 return val;
3046 /* Return those bits of the GUARD variable that should be set when the
3047 guarded entity is actually initialized. */
3049 static tree
3050 get_guard_bits (tree guard)
3052 if (!targetm.cxx.guard_mask_bit ())
3054 /* We only set the first byte of the guard, in order to leave room
3055 for a mutex in the high-order bits. */
3056 guard = build1 (ADDR_EXPR,
3057 build_pointer_type (TREE_TYPE (guard)),
3058 guard);
3059 guard = build1 (NOP_EXPR,
3060 build_pointer_type (char_type_node),
3061 guard);
3062 guard = build1 (INDIRECT_REF, char_type_node, guard);
3065 return guard;
3068 /* Return an expression which determines whether or not the GUARD
3069 variable has already been initialized. */
3071 tree
3072 get_guard_cond (tree guard, bool thread_safe)
3074 tree guard_value;
3076 if (!thread_safe)
3077 guard = get_guard_bits (guard);
3078 else
3079 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3081 /* Mask off all but the low bit. */
3082 if (targetm.cxx.guard_mask_bit ())
3084 guard_value = integer_one_node;
3085 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3086 guard_value = convert (TREE_TYPE (guard), guard_value);
3087 guard = cp_build_binary_op (input_location,
3088 BIT_AND_EXPR, guard, guard_value,
3089 tf_warning_or_error);
3092 guard_value = integer_zero_node;
3093 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3094 guard_value = convert (TREE_TYPE (guard), guard_value);
3095 return cp_build_binary_op (input_location,
3096 EQ_EXPR, guard, guard_value,
3097 tf_warning_or_error);
3100 /* Return an expression which sets the GUARD variable, indicating that
3101 the variable being guarded has been initialized. */
3103 tree
3104 set_guard (tree guard)
3106 tree guard_init;
3108 /* Set the GUARD to one. */
3109 guard = get_guard_bits (guard);
3110 guard_init = integer_one_node;
3111 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3112 guard_init = convert (TREE_TYPE (guard), guard_init);
3113 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3114 tf_warning_or_error);
3117 /* Returns true iff we can tell that VAR does not have a dynamic
3118 initializer. */
3120 static bool
3121 var_defined_without_dynamic_init (tree var)
3123 /* If it's defined in another TU, we can't tell. */
3124 if (DECL_EXTERNAL (var))
3125 return false;
3126 /* If it has a non-trivial destructor, registering the destructor
3127 counts as dynamic initialization. */
3128 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3129 return false;
3130 /* If it's in this TU, its initializer has been processed, unless
3131 it's a case of self-initialization, then DECL_INITIALIZED_P is
3132 false while the initializer is handled by finish_id_expression. */
3133 if (!DECL_INITIALIZED_P (var))
3134 return false;
3135 /* If it has no initializer or a constant one, it's not dynamic. */
3136 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3137 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3140 /* Returns true iff VAR is a variable that needs uses to be
3141 wrapped for possible dynamic initialization. */
3143 static bool
3144 var_needs_tls_wrapper (tree var)
3146 return (!error_operand_p (var)
3147 && DECL_THREAD_LOCAL_P (var)
3148 && !DECL_GNU_TLS_P (var)
3149 && !DECL_FUNCTION_SCOPE_P (var)
3150 && !var_defined_without_dynamic_init (var));
3153 /* Get the FUNCTION_DECL for the shared TLS init function for this
3154 translation unit. */
3156 static tree
3157 get_local_tls_init_fn (void)
3159 tree sname = get_identifier ("__tls_init");
3160 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3161 if (!fn)
3163 fn = build_lang_decl (FUNCTION_DECL, sname,
3164 build_function_type (void_type_node,
3165 void_list_node));
3166 SET_DECL_LANGUAGE (fn, lang_c);
3167 TREE_PUBLIC (fn) = false;
3168 DECL_ARTIFICIAL (fn) = true;
3169 mark_used (fn);
3170 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3172 return fn;
3175 /* Get a FUNCTION_DECL for the init function for the thread_local
3176 variable VAR. The init function will be an alias to the function
3177 that initializes all the non-local TLS variables in the translation
3178 unit. The init function is only used by the wrapper function. */
3180 static tree
3181 get_tls_init_fn (tree var)
3183 /* Only C++11 TLS vars need this init fn. */
3184 if (!var_needs_tls_wrapper (var))
3185 return NULL_TREE;
3187 /* If -fno-extern-tls-init, assume that we don't need to call
3188 a tls init function for a variable defined in another TU. */
3189 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3190 return NULL_TREE;
3192 #ifdef ASM_OUTPUT_DEF
3193 /* If the variable is internal, or if we can't generate aliases,
3194 call the local init function directly. */
3195 if (!TREE_PUBLIC (var))
3196 #endif
3197 return get_local_tls_init_fn ();
3199 tree sname = mangle_tls_init_fn (var);
3200 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3201 if (!fn)
3203 fn = build_lang_decl (FUNCTION_DECL, sname,
3204 build_function_type (void_type_node,
3205 void_list_node));
3206 SET_DECL_LANGUAGE (fn, lang_c);
3207 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3208 DECL_ARTIFICIAL (fn) = true;
3209 DECL_COMDAT (fn) = DECL_COMDAT (var);
3210 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3211 if (DECL_ONE_ONLY (var))
3212 make_decl_one_only (fn, cxx_comdat_group (fn));
3213 if (TREE_PUBLIC (var))
3215 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3216 /* If the variable is defined somewhere else and might have static
3217 initialization, make the init function a weak reference. */
3218 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3219 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3220 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3221 && DECL_EXTERNAL (var))
3222 declare_weak (fn);
3223 else
3224 DECL_WEAK (fn) = DECL_WEAK (var);
3226 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3227 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3228 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3229 DECL_IGNORED_P (fn) = 1;
3230 mark_used (fn);
3232 DECL_BEFRIENDING_CLASSES (fn) = var;
3234 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3236 return fn;
3239 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3240 variable VAR. The wrapper function calls the init function (if any) for
3241 VAR and then returns a reference to VAR. The wrapper function is used
3242 in place of VAR everywhere VAR is mentioned. */
3244 tree
3245 get_tls_wrapper_fn (tree var)
3247 /* Only C++11 TLS vars need this wrapper fn. */
3248 if (!var_needs_tls_wrapper (var))
3249 return NULL_TREE;
3251 tree sname = mangle_tls_wrapper_fn (var);
3252 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3253 if (!fn)
3255 /* A named rvalue reference is an lvalue, so the wrapper should
3256 always return an lvalue reference. */
3257 tree type = non_reference (TREE_TYPE (var));
3258 type = build_reference_type (type);
3259 tree fntype = build_function_type (type, void_list_node);
3260 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3261 SET_DECL_LANGUAGE (fn, lang_c);
3262 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3263 DECL_ARTIFICIAL (fn) = true;
3264 DECL_IGNORED_P (fn) = 1;
3265 /* The wrapper is inline and emitted everywhere var is used. */
3266 DECL_DECLARED_INLINE_P (fn) = true;
3267 if (TREE_PUBLIC (var))
3269 comdat_linkage (fn);
3270 #ifdef HAVE_GAS_HIDDEN
3271 /* Make the wrapper bind locally; there's no reason to share
3272 the wrapper between multiple shared objects. */
3273 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3274 DECL_VISIBILITY_SPECIFIED (fn) = true;
3275 #endif
3277 if (!TREE_PUBLIC (fn))
3278 DECL_INTERFACE_KNOWN (fn) = true;
3279 mark_used (fn);
3280 note_vague_linkage_fn (fn);
3282 #if 0
3283 /* We want CSE to commonize calls to the wrapper, but marking it as
3284 pure is unsafe since it has side-effects. I guess we need a new
3285 ECF flag even weaker than ECF_PURE. FIXME! */
3286 DECL_PURE_P (fn) = true;
3287 #endif
3289 DECL_BEFRIENDING_CLASSES (fn) = var;
3291 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3293 return fn;
3296 /* At EOF, generate the definition for the TLS wrapper function FN:
3298 T& var_wrapper() {
3299 if (init_fn) init_fn();
3300 return var;
3301 } */
3303 static void
3304 generate_tls_wrapper (tree fn)
3306 tree var = DECL_BEFRIENDING_CLASSES (fn);
3308 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3309 tree body = begin_function_body ();
3310 /* Only call the init fn if there might be one. */
3311 if (tree init_fn = get_tls_init_fn (var))
3313 tree if_stmt = NULL_TREE;
3314 /* If init_fn is a weakref, make sure it exists before calling. */
3315 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3317 if_stmt = begin_if_stmt ();
3318 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3319 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3320 NE_EXPR, addr, nullptr_node,
3321 tf_warning_or_error);
3322 finish_if_stmt_cond (cond, if_stmt);
3324 finish_expr_stmt (build_cxx_call
3325 (init_fn, 0, NULL, tf_warning_or_error));
3326 if (if_stmt)
3328 finish_then_clause (if_stmt);
3329 finish_if_stmt (if_stmt);
3332 else
3333 /* If there's no initialization, the wrapper is a constant function. */
3334 TREE_READONLY (fn) = true;
3335 finish_return_stmt (convert_from_reference (var));
3336 finish_function_body (body);
3337 expand_or_defer_fn (finish_function (0));
3340 /* Start the process of running a particular set of global constructors
3341 or destructors. Subroutine of do_[cd]tors. Also called from
3342 vtv_start_verification_constructor_init_function. */
3344 static tree
3345 start_objects (int method_type, int initp)
3347 tree body;
3348 tree fndecl;
3349 char type[14];
3351 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3353 if (initp != DEFAULT_INIT_PRIORITY)
3355 char joiner;
3357 #ifdef JOINER
3358 joiner = JOINER;
3359 #else
3360 joiner = '_';
3361 #endif
3363 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3365 else
3366 sprintf (type, "sub_%c", method_type);
3368 fndecl = build_lang_decl (FUNCTION_DECL,
3369 get_file_function_name (type),
3370 build_function_type_list (void_type_node,
3371 NULL_TREE));
3372 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3374 TREE_PUBLIC (current_function_decl) = 0;
3376 /* Mark as artificial because it's not explicitly in the user's
3377 source code. */
3378 DECL_ARTIFICIAL (current_function_decl) = 1;
3380 /* Mark this declaration as used to avoid spurious warnings. */
3381 TREE_USED (current_function_decl) = 1;
3383 /* Mark this function as a global constructor or destructor. */
3384 if (method_type == 'I')
3385 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3386 else
3387 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3389 body = begin_compound_stmt (BCS_FN_BODY);
3391 return body;
3394 /* Finish the process of running a particular set of global constructors
3395 or destructors. Subroutine of do_[cd]tors. */
3397 static void
3398 finish_objects (int method_type, int initp, tree body)
3400 tree fn;
3402 /* Finish up. */
3403 finish_compound_stmt (body);
3404 fn = finish_function (0);
3406 if (method_type == 'I')
3408 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3409 decl_init_priority_insert (fn, initp);
3411 else
3413 DECL_STATIC_DESTRUCTOR (fn) = 1;
3414 decl_fini_priority_insert (fn, initp);
3417 expand_or_defer_fn (fn);
3420 /* The names of the parameters to the function created to handle
3421 initializations and destructions for objects with static storage
3422 duration. */
3423 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3424 #define PRIORITY_IDENTIFIER "__priority"
3426 /* The name of the function we create to handle initializations and
3427 destructions for objects with static storage duration. */
3428 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3430 /* The declaration for the __INITIALIZE_P argument. */
3431 static GTY(()) tree initialize_p_decl;
3433 /* The declaration for the __PRIORITY argument. */
3434 static GTY(()) tree priority_decl;
3436 /* The declaration for the static storage duration function. */
3437 static GTY(()) tree ssdf_decl;
3439 /* All the static storage duration functions created in this
3440 translation unit. */
3441 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3443 /* A map from priority levels to information about that priority
3444 level. There may be many such levels, so efficient lookup is
3445 important. */
3446 static splay_tree priority_info_map;
3448 /* Begins the generation of the function that will handle all
3449 initialization and destruction of objects with static storage
3450 duration. The function generated takes two parameters of type
3451 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3452 nonzero, it performs initializations. Otherwise, it performs
3453 destructions. It only performs those initializations or
3454 destructions with the indicated __PRIORITY. The generated function
3455 returns no value.
3457 It is assumed that this function will only be called once per
3458 translation unit. */
3460 static tree
3461 start_static_storage_duration_function (unsigned count)
3463 tree type;
3464 tree body;
3465 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3467 /* Create the identifier for this function. It will be of the form
3468 SSDF_IDENTIFIER_<number>. */
3469 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3471 type = build_function_type_list (void_type_node,
3472 integer_type_node, integer_type_node,
3473 NULL_TREE);
3475 /* Create the FUNCTION_DECL itself. */
3476 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3477 get_identifier (id),
3478 type);
3479 TREE_PUBLIC (ssdf_decl) = 0;
3480 DECL_ARTIFICIAL (ssdf_decl) = 1;
3482 /* Put this function in the list of functions to be called from the
3483 static constructors and destructors. */
3484 if (!ssdf_decls)
3486 vec_alloc (ssdf_decls, 32);
3488 /* Take this opportunity to initialize the map from priority
3489 numbers to information about that priority level. */
3490 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3491 /*delete_key_fn=*/0,
3492 /*delete_value_fn=*/
3493 (splay_tree_delete_value_fn) &free);
3495 /* We always need to generate functions for the
3496 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3497 priorities later, we'll be sure to find the
3498 DEFAULT_INIT_PRIORITY. */
3499 get_priority_info (DEFAULT_INIT_PRIORITY);
3502 vec_safe_push (ssdf_decls, ssdf_decl);
3504 /* Create the argument list. */
3505 initialize_p_decl = cp_build_parm_decl
3506 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3507 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3508 TREE_USED (initialize_p_decl) = 1;
3509 priority_decl = cp_build_parm_decl
3510 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3511 DECL_CONTEXT (priority_decl) = ssdf_decl;
3512 TREE_USED (priority_decl) = 1;
3514 DECL_CHAIN (initialize_p_decl) = priority_decl;
3515 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3517 /* Put the function in the global scope. */
3518 pushdecl (ssdf_decl);
3520 /* Start the function itself. This is equivalent to declaring the
3521 function as:
3523 static void __ssdf (int __initialize_p, init __priority_p);
3525 It is static because we only need to call this function from the
3526 various constructor and destructor functions for this module. */
3527 start_preparsed_function (ssdf_decl,
3528 /*attrs=*/NULL_TREE,
3529 SF_PRE_PARSED);
3531 /* Set up the scope of the outermost block in the function. */
3532 body = begin_compound_stmt (BCS_FN_BODY);
3534 return body;
3537 /* Finish the generation of the function which performs initialization
3538 and destruction of objects with static storage duration. After
3539 this point, no more such objects can be created. */
3541 static void
3542 finish_static_storage_duration_function (tree body)
3544 /* Close out the function. */
3545 finish_compound_stmt (body);
3546 expand_or_defer_fn (finish_function (0));
3549 /* Return the information about the indicated PRIORITY level. If no
3550 code to handle this level has yet been generated, generate the
3551 appropriate prologue. */
3553 static priority_info
3554 get_priority_info (int priority)
3556 priority_info pi;
3557 splay_tree_node n;
3559 n = splay_tree_lookup (priority_info_map,
3560 (splay_tree_key) priority);
3561 if (!n)
3563 /* Create a new priority information structure, and insert it
3564 into the map. */
3565 pi = XNEW (struct priority_info_s);
3566 pi->initializations_p = 0;
3567 pi->destructions_p = 0;
3568 splay_tree_insert (priority_info_map,
3569 (splay_tree_key) priority,
3570 (splay_tree_value) pi);
3572 else
3573 pi = (priority_info) n->value;
3575 return pi;
3578 /* The effective initialization priority of a DECL. */
3580 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3581 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3582 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3584 /* Whether a DECL needs a guard to protect it against multiple
3585 initialization. */
3587 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3588 || DECL_ONE_ONLY (decl) \
3589 || DECL_WEAK (decl)))
3591 /* Called from one_static_initialization_or_destruction(),
3592 via walk_tree.
3593 Walks the initializer list of a global variable and looks for
3594 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3595 and that have their DECL_CONTEXT() == NULL.
3596 For each such temporary variable, set their DECL_CONTEXT() to
3597 the current function. This is necessary because otherwise
3598 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3599 when trying to refer to a temporary variable that does not have
3600 it's DECL_CONTECT() properly set. */
3601 static tree
3602 fix_temporary_vars_context_r (tree *node,
3603 int * /*unused*/,
3604 void * /*unused1*/)
3606 gcc_assert (current_function_decl);
3608 if (TREE_CODE (*node) == BIND_EXPR)
3610 tree var;
3612 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3613 if (VAR_P (var)
3614 && !DECL_NAME (var)
3615 && DECL_ARTIFICIAL (var)
3616 && !DECL_CONTEXT (var))
3617 DECL_CONTEXT (var) = current_function_decl;
3620 return NULL_TREE;
3623 /* Set up to handle the initialization or destruction of DECL. If
3624 INITP is nonzero, we are initializing the variable. Otherwise, we
3625 are destroying it. */
3627 static void
3628 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3630 tree guard_if_stmt = NULL_TREE;
3631 tree guard;
3633 /* If we are supposed to destruct and there's a trivial destructor,
3634 nothing has to be done. */
3635 if (!initp
3636 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3637 return;
3639 /* Trick the compiler into thinking we are at the file and line
3640 where DECL was declared so that error-messages make sense, and so
3641 that the debugger will show somewhat sensible file and line
3642 information. */
3643 input_location = DECL_SOURCE_LOCATION (decl);
3645 /* Make sure temporary variables in the initialiser all have
3646 their DECL_CONTEXT() set to a value different from NULL_TREE.
3647 This can happen when global variables initialisers are built.
3648 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3649 the temporary variables that might have been generated in the
3650 accompagning initialisers is NULL_TREE, meaning the variables have been
3651 declared in the global namespace.
3652 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3653 of the temporaries are set to the current function decl. */
3654 cp_walk_tree_without_duplicates (&init,
3655 fix_temporary_vars_context_r,
3656 NULL);
3658 /* Because of:
3660 [class.access.spec]
3662 Access control for implicit calls to the constructors,
3663 the conversion functions, or the destructor called to
3664 create and destroy a static data member is performed as
3665 if these calls appeared in the scope of the member's
3666 class.
3668 we pretend we are in a static member function of the class of
3669 which the DECL is a member. */
3670 if (member_p (decl))
3672 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3673 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3676 /* Assume we don't need a guard. */
3677 guard = NULL_TREE;
3678 /* We need a guard if this is an object with external linkage that
3679 might be initialized in more than one place. (For example, a
3680 static data member of a template, when the data member requires
3681 construction.) */
3682 if (NEEDS_GUARD_P (decl))
3684 tree guard_cond;
3686 guard = get_guard (decl);
3688 /* When using __cxa_atexit, we just check the GUARD as we would
3689 for a local static. */
3690 if (flag_use_cxa_atexit)
3692 /* When using __cxa_atexit, we never try to destroy
3693 anything from a static destructor. */
3694 gcc_assert (initp);
3695 guard_cond = get_guard_cond (guard, false);
3697 /* If we don't have __cxa_atexit, then we will be running
3698 destructors from .fini sections, or their equivalents. So,
3699 we need to know how many times we've tried to initialize this
3700 object. We do initializations only if the GUARD is zero,
3701 i.e., if we are the first to initialize the variable. We do
3702 destructions only if the GUARD is one, i.e., if we are the
3703 last to destroy the variable. */
3704 else if (initp)
3705 guard_cond
3706 = cp_build_binary_op (input_location,
3707 EQ_EXPR,
3708 cp_build_unary_op (PREINCREMENT_EXPR,
3709 guard,
3710 /*noconvert=*/1,
3711 tf_warning_or_error),
3712 integer_one_node,
3713 tf_warning_or_error);
3714 else
3715 guard_cond
3716 = cp_build_binary_op (input_location,
3717 EQ_EXPR,
3718 cp_build_unary_op (PREDECREMENT_EXPR,
3719 guard,
3720 /*noconvert=*/1,
3721 tf_warning_or_error),
3722 integer_zero_node,
3723 tf_warning_or_error);
3725 guard_if_stmt = begin_if_stmt ();
3726 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3730 /* If we're using __cxa_atexit, we have not already set the GUARD,
3731 so we must do so now. */
3732 if (guard && initp && flag_use_cxa_atexit)
3733 finish_expr_stmt (set_guard (guard));
3735 /* Perform the initialization or destruction. */
3736 if (initp)
3738 if (init)
3740 finish_expr_stmt (init);
3741 if (flag_sanitize & SANITIZE_ADDRESS)
3743 varpool_node *vnode = varpool_node::get (decl);
3744 if (vnode)
3745 vnode->dynamically_initialized = 1;
3749 /* If we're using __cxa_atexit, register a function that calls the
3750 destructor for the object. */
3751 if (flag_use_cxa_atexit)
3752 finish_expr_stmt (register_dtor_fn (decl));
3754 else
3755 finish_expr_stmt (build_cleanup (decl));
3757 /* Finish the guard if-stmt, if necessary. */
3758 if (guard)
3760 finish_then_clause (guard_if_stmt);
3761 finish_if_stmt (guard_if_stmt);
3764 /* Now that we're done with DECL we don't need to pretend to be a
3765 member of its class any longer. */
3766 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3767 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3770 /* Generate code to do the initialization or destruction of the decls in VARS,
3771 a TREE_LIST of VAR_DECL with static storage duration.
3772 Whether initialization or destruction is performed is specified by INITP. */
3774 static void
3775 do_static_initialization_or_destruction (tree vars, bool initp)
3777 tree node, init_if_stmt, cond;
3779 /* Build the outer if-stmt to check for initialization or destruction. */
3780 init_if_stmt = begin_if_stmt ();
3781 cond = initp ? integer_one_node : integer_zero_node;
3782 cond = cp_build_binary_op (input_location,
3783 EQ_EXPR,
3784 initialize_p_decl,
3785 cond,
3786 tf_warning_or_error);
3787 finish_if_stmt_cond (cond, init_if_stmt);
3789 /* To make sure dynamic construction doesn't access globals from other
3790 compilation units where they might not be yet constructed, for
3791 -fsanitize=address insert __asan_before_dynamic_init call that
3792 prevents access to either all global variables that need construction
3793 in other compilation units, or at least those that haven't been
3794 initialized yet. Variables that need dynamic construction in
3795 the current compilation unit are kept accessible. */
3796 if (flag_sanitize & SANITIZE_ADDRESS)
3797 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3799 node = vars;
3800 do {
3801 tree decl = TREE_VALUE (node);
3802 tree priority_if_stmt;
3803 int priority;
3804 priority_info pi;
3806 /* If we don't need a destructor, there's nothing to do. Avoid
3807 creating a possibly empty if-stmt. */
3808 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3810 node = TREE_CHAIN (node);
3811 continue;
3814 /* Remember that we had an initialization or finalization at this
3815 priority. */
3816 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3817 pi = get_priority_info (priority);
3818 if (initp)
3819 pi->initializations_p = 1;
3820 else
3821 pi->destructions_p = 1;
3823 /* Conditionalize this initialization on being in the right priority
3824 and being initializing/finalizing appropriately. */
3825 priority_if_stmt = begin_if_stmt ();
3826 cond = cp_build_binary_op (input_location,
3827 EQ_EXPR,
3828 priority_decl,
3829 build_int_cst (NULL_TREE, priority),
3830 tf_warning_or_error);
3831 finish_if_stmt_cond (cond, priority_if_stmt);
3833 /* Process initializers with same priority. */
3834 for (; node
3835 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3836 node = TREE_CHAIN (node))
3837 /* Do one initialization or destruction. */
3838 one_static_initialization_or_destruction (TREE_VALUE (node),
3839 TREE_PURPOSE (node), initp);
3841 /* Finish up the priority if-stmt body. */
3842 finish_then_clause (priority_if_stmt);
3843 finish_if_stmt (priority_if_stmt);
3845 } while (node);
3847 /* Revert what __asan_before_dynamic_init did by calling
3848 __asan_after_dynamic_init. */
3849 if (flag_sanitize & SANITIZE_ADDRESS)
3850 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3852 /* Finish up the init/destruct if-stmt body. */
3853 finish_then_clause (init_if_stmt);
3854 finish_if_stmt (init_if_stmt);
3857 /* VARS is a list of variables with static storage duration which may
3858 need initialization and/or finalization. Remove those variables
3859 that don't really need to be initialized or finalized, and return
3860 the resulting list. The order in which the variables appear in
3861 VARS is in reverse order of the order in which they should actually
3862 be initialized. The list we return is in the unreversed order;
3863 i.e., the first variable should be initialized first. */
3865 static tree
3866 prune_vars_needing_no_initialization (tree *vars)
3868 tree *var = vars;
3869 tree result = NULL_TREE;
3871 while (*var)
3873 tree t = *var;
3874 tree decl = TREE_VALUE (t);
3875 tree init = TREE_PURPOSE (t);
3877 /* Deal gracefully with error. */
3878 if (decl == error_mark_node)
3880 var = &TREE_CHAIN (t);
3881 continue;
3884 /* The only things that can be initialized are variables. */
3885 gcc_assert (VAR_P (decl));
3887 /* If this object is not defined, we don't need to do anything
3888 here. */
3889 if (DECL_EXTERNAL (decl))
3891 var = &TREE_CHAIN (t);
3892 continue;
3895 /* Also, if the initializer already contains errors, we can bail
3896 out now. */
3897 if (init && TREE_CODE (init) == TREE_LIST
3898 && value_member (error_mark_node, init))
3900 var = &TREE_CHAIN (t);
3901 continue;
3904 /* This variable is going to need initialization and/or
3905 finalization, so we add it to the list. */
3906 *var = TREE_CHAIN (t);
3907 TREE_CHAIN (t) = result;
3908 result = t;
3911 return result;
3914 /* Make sure we have told the back end about all the variables in
3915 VARS. */
3917 static void
3918 write_out_vars (tree vars)
3920 tree v;
3922 for (v = vars; v; v = TREE_CHAIN (v))
3924 tree var = TREE_VALUE (v);
3925 if (!var_finalized_p (var))
3927 import_export_decl (var);
3928 rest_of_decl_compilation (var, 1, 1);
3933 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3934 (otherwise) that will initialize all global objects with static
3935 storage duration having the indicated PRIORITY. */
3937 static void
3938 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3939 location_t *locus)
3941 char function_key;
3942 tree fndecl;
3943 tree body;
3944 size_t i;
3946 input_location = *locus;
3947 /* ??? */
3948 /* Was: locus->line++; */
3950 /* We use `I' to indicate initialization and `D' to indicate
3951 destruction. */
3952 function_key = constructor_p ? 'I' : 'D';
3954 /* We emit the function lazily, to avoid generating empty
3955 global constructors and destructors. */
3956 body = NULL_TREE;
3958 /* For Objective-C++, we may need to initialize metadata found in this module.
3959 This must be done _before_ any other static initializations. */
3960 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3961 && constructor_p && objc_static_init_needed_p ())
3963 body = start_objects (function_key, priority);
3964 objc_generate_static_init_call (NULL_TREE);
3967 /* Call the static storage duration function with appropriate
3968 arguments. */
3969 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3971 /* Calls to pure or const functions will expand to nothing. */
3972 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3974 tree call;
3976 if (! body)
3977 body = start_objects (function_key, priority);
3979 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3980 build_int_cst (NULL_TREE,
3981 constructor_p),
3982 build_int_cst (NULL_TREE,
3983 priority),
3984 NULL_TREE);
3985 finish_expr_stmt (call);
3989 /* Close out the function. */
3990 if (body)
3991 finish_objects (function_key, priority, body);
3994 /* Generate constructor and destructor functions for the priority
3995 indicated by N. */
3997 static int
3998 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4000 location_t *locus = (location_t *) data;
4001 int priority = (int) n->key;
4002 priority_info pi = (priority_info) n->value;
4004 /* Generate the functions themselves, but only if they are really
4005 needed. */
4006 if (pi->initializations_p)
4007 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4008 if (pi->destructions_p)
4009 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4011 /* Keep iterating. */
4012 return 0;
4015 /* Java requires that we be able to reference a local address for a
4016 method, and not be confused by PLT entries. If supported, create a
4017 hidden alias for all such methods. */
4019 static void
4020 build_java_method_aliases (void)
4022 #ifndef HAVE_GAS_HIDDEN
4023 return;
4024 #endif
4026 struct cgraph_node *node;
4027 FOR_EACH_FUNCTION (node)
4029 tree fndecl = node->decl;
4031 if (DECL_CLASS_SCOPE_P (fndecl)
4032 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4033 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4035 /* Mangle the name in a predictable way; we need to reference
4036 this from a java compiled object file. */
4037 tree oid = DECL_ASSEMBLER_NAME (fndecl);
4038 const char *oname = IDENTIFIER_POINTER (oid);
4039 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4040 char *nname = ACONCAT (("_ZGA", oname + 2, NULL));
4042 tree alias = make_alias_for (fndecl, get_identifier (nname));
4043 TREE_PUBLIC (alias) = 1;
4044 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4046 cgraph_node::create_same_body_alias (alias, fndecl);
4051 /* Return C++ property of T, based on given operation OP. */
4053 static int
4054 cpp_check (tree t, cpp_operation op)
4056 switch (op)
4058 case HAS_DEPENDENT_TEMPLATE_ARGS:
4060 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4061 if (!ti)
4062 return 0;
4063 ++processing_template_decl;
4064 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4065 --processing_template_decl;
4066 return dep;
4068 case IS_ABSTRACT:
4069 return DECL_PURE_VIRTUAL_P (t);
4070 case IS_CONSTRUCTOR:
4071 return DECL_CONSTRUCTOR_P (t);
4072 case IS_DESTRUCTOR:
4073 return DECL_DESTRUCTOR_P (t);
4074 case IS_COPY_CONSTRUCTOR:
4075 return DECL_COPY_CONSTRUCTOR_P (t);
4076 case IS_TEMPLATE:
4077 return TREE_CODE (t) == TEMPLATE_DECL;
4078 case IS_TRIVIAL:
4079 return trivial_type_p (t);
4080 default:
4081 return 0;
4085 /* Collect source file references recursively, starting from NAMESPC. */
4087 static void
4088 collect_source_refs (tree namespc)
4090 tree t;
4092 if (!namespc)
4093 return;
4095 /* Iterate over names in this name space. */
4096 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4097 if (!DECL_IS_BUILTIN (t) )
4098 collect_source_ref (DECL_SOURCE_FILE (t));
4100 /* Dump siblings, if any */
4101 collect_source_refs (TREE_CHAIN (namespc));
4103 /* Dump children, if any */
4104 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4107 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4108 starting from NAMESPC. */
4110 static void
4111 collect_ada_namespace (tree namespc, const char *source_file)
4113 if (!namespc)
4114 return;
4116 /* Collect decls from this namespace */
4117 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4119 /* Collect siblings, if any */
4120 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4122 /* Collect children, if any */
4123 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4126 /* Returns true iff there is a definition available for variable or
4127 function DECL. */
4129 static bool
4130 decl_defined_p (tree decl)
4132 if (TREE_CODE (decl) == FUNCTION_DECL)
4133 return (DECL_INITIAL (decl) != NULL_TREE);
4134 else
4136 gcc_assert (VAR_P (decl));
4137 return !DECL_EXTERNAL (decl);
4141 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4143 [expr.const]
4145 An integral constant-expression can only involve ... const
4146 variables of integral or enumeration types initialized with
4147 constant expressions ...
4149 C++0x also allows constexpr variables and temporaries initialized
4150 with constant expressions. We handle the former here, but the latter
4151 are just folded away in cxx_eval_constant_expression.
4153 The standard does not require that the expression be non-volatile.
4154 G++ implements the proposed correction in DR 457. */
4156 bool
4157 decl_constant_var_p (tree decl)
4159 if (!decl_maybe_constant_var_p (decl))
4160 return false;
4162 /* We don't know if a template static data member is initialized with
4163 a constant expression until we instantiate its initializer. Even
4164 in the case of a constexpr variable, we can't treat it as a
4165 constant until its initializer is complete in case it's used in
4166 its own initializer. */
4167 mark_used (decl);
4168 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4171 /* Returns true if DECL could be a symbolic constant variable, depending on
4172 its initializer. */
4174 bool
4175 decl_maybe_constant_var_p (tree decl)
4177 tree type = TREE_TYPE (decl);
4178 if (!VAR_P (decl))
4179 return false;
4180 if (DECL_DECLARED_CONSTEXPR_P (decl))
4181 return true;
4182 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4183 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4186 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4187 called from grokfndecl and grokvardecl; in all modes it is called from
4188 cp_write_global_declarations. */
4190 void
4191 no_linkage_error (tree decl)
4193 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4194 /* In C++11 it's ok if the decl is defined. */
4195 return;
4196 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4197 if (t == NULL_TREE)
4198 /* The type that got us on no_linkage_decls must have gotten a name for
4199 linkage purposes. */;
4200 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4201 /* The type might end up having a typedef name for linkage purposes. */
4202 vec_safe_push (no_linkage_decls, decl);
4203 else if (TYPE_ANONYMOUS_P (t))
4205 bool d = false;
4206 if (cxx_dialect >= cxx11)
4207 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4208 "anonymous type, is used but never defined", decl);
4209 else if (DECL_EXTERN_C_P (decl))
4210 /* Allow this; it's pretty common in C. */;
4211 else if (TREE_CODE (decl) == VAR_DECL)
4212 /* DRs 132, 319 and 389 seem to indicate types with
4213 no linkage can only be used to declare extern "C"
4214 entities. Since it's not always an error in the
4215 ISO C++ 90 Standard, we only issue a warning. */
4216 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4217 "with no linkage used to declare variable %q#D with "
4218 "linkage", decl);
4219 else
4220 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4221 "linkage used to declare function %q#D with linkage",
4222 decl);
4223 if (d && is_typedef_decl (TYPE_NAME (t)))
4224 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4225 "to the unqualified type, so it is not used for linkage",
4226 TYPE_NAME (t));
4228 else if (cxx_dialect >= cxx11)
4230 if (TREE_CODE (decl) == VAR_DECL || !DECL_PURE_VIRTUAL_P (decl))
4231 permerror (DECL_SOURCE_LOCATION (decl),
4232 "%q#D, declared using local type "
4233 "%qT, is used but never defined", decl, t);
4235 else if (TREE_CODE (decl) == VAR_DECL)
4236 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4237 "used to declare variable %q#D with linkage", t, decl);
4238 else
4239 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4240 "to declare function %q#D with linkage", t, decl);
4243 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4245 static void
4246 collect_all_refs (const char *source_file)
4248 collect_ada_namespace (global_namespace, source_file);
4251 /* Clear DECL_EXTERNAL for NODE. */
4253 static bool
4254 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4256 DECL_EXTERNAL (node->decl) = 0;
4257 return false;
4260 /* Build up the function to run dynamic initializers for thread_local
4261 variables in this translation unit and alias the init functions for the
4262 individual variables to it. */
4264 static void
4265 handle_tls_init (void)
4267 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4268 if (vars == NULL_TREE)
4269 return;
4271 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4273 write_out_vars (vars);
4275 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4276 boolean_type_node);
4277 TREE_PUBLIC (guard) = false;
4278 TREE_STATIC (guard) = true;
4279 DECL_ARTIFICIAL (guard) = true;
4280 DECL_IGNORED_P (guard) = true;
4281 TREE_USED (guard) = true;
4282 set_decl_tls_model (guard, decl_default_tls_model (guard));
4283 pushdecl_top_level_and_finish (guard, NULL_TREE);
4285 tree fn = get_local_tls_init_fn ();
4286 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4287 tree body = begin_function_body ();
4288 tree if_stmt = begin_if_stmt ();
4289 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4290 tf_warning_or_error);
4291 finish_if_stmt_cond (cond, if_stmt);
4292 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4293 tf_warning_or_error));
4294 for (; vars; vars = TREE_CHAIN (vars))
4296 tree var = TREE_VALUE (vars);
4297 tree init = TREE_PURPOSE (vars);
4298 one_static_initialization_or_destruction (var, init, true);
4300 #ifdef ASM_OUTPUT_DEF
4301 /* Output init aliases even with -fno-extern-tls-init. */
4302 if (TREE_PUBLIC (var))
4304 tree single_init_fn = get_tls_init_fn (var);
4305 if (single_init_fn == NULL_TREE)
4306 continue;
4307 cgraph_node *alias
4308 = cgraph_node::get_create (fn)->create_same_body_alias
4309 (single_init_fn, fn);
4310 gcc_assert (alias != NULL);
4312 #endif
4315 finish_then_clause (if_stmt);
4316 finish_if_stmt (if_stmt);
4317 finish_function_body (body);
4318 expand_or_defer_fn (finish_function (0));
4321 /* We're at the end of compilation, so generate any mangling aliases that
4322 we've been saving up, if DECL is going to be output and ID2 isn't
4323 already taken by another declaration. */
4325 static void
4326 generate_mangling_alias (tree decl, tree id2)
4328 /* If there's a declaration already using this mangled name,
4329 don't create a compatibility alias that conflicts. */
4330 if (IDENTIFIER_GLOBAL_VALUE (id2))
4331 return;
4333 struct cgraph_node *n = NULL;
4334 if (TREE_CODE (decl) == FUNCTION_DECL
4335 && !(n = cgraph_node::get (decl)))
4336 /* Don't create an alias to an unreferenced function. */
4337 return;
4339 tree alias = make_alias_for (decl, id2);
4340 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4341 DECL_IGNORED_P (alias) = 1;
4342 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4343 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4344 if (vague_linkage_p (decl))
4345 DECL_WEAK (alias) = 1;
4346 if (TREE_CODE (decl) == FUNCTION_DECL)
4347 n->create_same_body_alias (alias, decl);
4348 else
4349 varpool_node::create_extra_name_alias (alias, decl);
4352 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4353 the end of translation, for compatibility across bugs in the mangling
4354 implementation. */
4356 void
4357 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4359 #ifdef ASM_OUTPUT_DEF
4360 if (at_eof)
4361 generate_mangling_alias (decl, id2);
4362 else
4364 vec_safe_push (mangling_aliases, decl);
4365 vec_safe_push (mangling_aliases, id2);
4367 #endif
4370 static void
4371 generate_mangling_aliases ()
4373 while (!vec_safe_is_empty (mangling_aliases))
4375 tree id2 = mangling_aliases->pop();
4376 tree decl = mangling_aliases->pop();
4377 generate_mangling_alias (decl, id2);
4381 /* The entire file is now complete. If requested, dump everything
4382 to a file. */
4384 static void
4385 dump_tu (void)
4387 int flags;
4388 FILE *stream = dump_begin (TDI_tu, &flags);
4390 if (stream)
4392 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4393 dump_end (TDI_tu, stream);
4397 static location_t locus_at_end_of_parsing;
4399 /* Check the deallocation functions for CODE to see if we want to warn that
4400 only one was defined. */
4402 static void
4403 maybe_warn_sized_delete (enum tree_code code)
4405 tree sized = NULL_TREE;
4406 tree unsized = NULL_TREE;
4408 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4409 ovl; ovl = OVL_NEXT (ovl))
4411 tree fn = OVL_CURRENT (ovl);
4412 /* We're only interested in usual deallocation functions. */
4413 if (!non_placement_deallocation_fn_p (fn))
4414 continue;
4415 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4416 unsized = fn;
4417 else
4418 sized = fn;
4420 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4421 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4422 "the program should also define %qD", sized);
4423 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4424 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4425 "the program should also define %qD", unsized);
4428 /* Check the global deallocation functions to see if we want to warn about
4429 defining unsized without sized (or vice versa). */
4431 static void
4432 maybe_warn_sized_delete ()
4434 if (!flag_sized_deallocation || !warn_sized_deallocation)
4435 return;
4436 maybe_warn_sized_delete (DELETE_EXPR);
4437 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4440 /* This routine is called at the end of compilation.
4441 Its job is to create all the code needed to initialize and
4442 destroy the global aggregates. We do the destruction
4443 first, since that way we only need to reverse the decls once. */
4445 void
4446 c_parse_final_cleanups (void)
4448 tree vars;
4449 bool reconsider;
4450 size_t i;
4451 unsigned ssdf_count = 0;
4452 int retries = 0;
4453 tree decl;
4455 locus_at_end_of_parsing = input_location;
4456 at_eof = 1;
4458 /* Bad parse errors. Just forget about it. */
4459 if (! global_bindings_p () || current_class_type
4460 || !vec_safe_is_empty (decl_namespace_list))
4461 return;
4463 /* This is the point to write out a PCH if we're doing that.
4464 In that case we do not want to do anything else. */
4465 if (pch_file)
4467 c_common_write_pch ();
4468 dump_tu ();
4469 return;
4472 timevar_stop (TV_PHASE_PARSING);
4473 timevar_start (TV_PHASE_DEFERRED);
4475 symtab->process_same_body_aliases ();
4477 /* Handle -fdump-ada-spec[-slim] */
4478 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4480 if (flag_dump_ada_spec_slim)
4481 collect_source_ref (main_input_filename);
4482 else
4483 collect_source_refs (global_namespace);
4485 dump_ada_specs (collect_all_refs, cpp_check);
4488 /* FIXME - huh? was input_line -= 1;*/
4490 /* We now have to write out all the stuff we put off writing out.
4491 These include:
4493 o Template specializations that we have not yet instantiated,
4494 but which are needed.
4495 o Initialization and destruction for non-local objects with
4496 static storage duration. (Local objects with static storage
4497 duration are initialized when their scope is first entered,
4498 and are cleaned up via atexit.)
4499 o Virtual function tables.
4501 All of these may cause others to be needed. For example,
4502 instantiating one function may cause another to be needed, and
4503 generating the initializer for an object may cause templates to be
4504 instantiated, etc., etc. */
4506 emit_support_tinfos ();
4510 tree t;
4511 tree decl;
4513 reconsider = false;
4515 /* If there are templates that we've put off instantiating, do
4516 them now. */
4517 instantiate_pending_templates (retries);
4518 ggc_collect ();
4520 /* Write out virtual tables as required. Note that writing out
4521 the virtual table for a template class may cause the
4522 instantiation of members of that class. If we write out
4523 vtables then we remove the class from our list so we don't
4524 have to look at it again. */
4526 while (keyed_classes != NULL_TREE
4527 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4529 reconsider = true;
4530 keyed_classes = TREE_CHAIN (keyed_classes);
4533 t = keyed_classes;
4534 if (t != NULL_TREE)
4536 tree next = TREE_CHAIN (t);
4538 while (next)
4540 if (maybe_emit_vtables (TREE_VALUE (next)))
4542 reconsider = true;
4543 TREE_CHAIN (t) = TREE_CHAIN (next);
4545 else
4546 t = next;
4548 next = TREE_CHAIN (t);
4552 /* Write out needed type info variables. We have to be careful
4553 looping through unemitted decls, because emit_tinfo_decl may
4554 cause other variables to be needed. New elements will be
4555 appended, and we remove from the vector those that actually
4556 get emitted. */
4557 for (i = unemitted_tinfo_decls->length ();
4558 unemitted_tinfo_decls->iterate (--i, &t);)
4559 if (emit_tinfo_decl (t))
4561 reconsider = true;
4562 unemitted_tinfo_decls->unordered_remove (i);
4565 /* The list of objects with static storage duration is built up
4566 in reverse order. We clear STATIC_AGGREGATES so that any new
4567 aggregates added during the initialization of these will be
4568 initialized in the correct order when we next come around the
4569 loop. */
4570 vars = prune_vars_needing_no_initialization (&static_aggregates);
4572 if (vars)
4574 /* We need to start a new initialization function each time
4575 through the loop. That's because we need to know which
4576 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4577 isn't computed until a function is finished, and written
4578 out. That's a deficiency in the back end. When this is
4579 fixed, these initialization functions could all become
4580 inline, with resulting performance improvements. */
4581 tree ssdf_body;
4583 /* Set the line and file, so that it is obviously not from
4584 the source file. */
4585 input_location = locus_at_end_of_parsing;
4586 ssdf_body = start_static_storage_duration_function (ssdf_count);
4588 /* Make sure the back end knows about all the variables. */
4589 write_out_vars (vars);
4591 /* First generate code to do all the initializations. */
4592 if (vars)
4593 do_static_initialization_or_destruction (vars, /*initp=*/true);
4595 /* Then, generate code to do all the destructions. Do these
4596 in reverse order so that the most recently constructed
4597 variable is the first destroyed. If we're using
4598 __cxa_atexit, then we don't need to do this; functions
4599 were registered at initialization time to destroy the
4600 local statics. */
4601 if (!flag_use_cxa_atexit && vars)
4603 vars = nreverse (vars);
4604 do_static_initialization_or_destruction (vars, /*initp=*/false);
4606 else
4607 vars = NULL_TREE;
4609 /* Finish up the static storage duration function for this
4610 round. */
4611 input_location = locus_at_end_of_parsing;
4612 finish_static_storage_duration_function (ssdf_body);
4614 /* All those initializations and finalizations might cause
4615 us to need more inline functions, more template
4616 instantiations, etc. */
4617 reconsider = true;
4618 ssdf_count++;
4619 /* ??? was: locus_at_end_of_parsing.line++; */
4622 /* Now do the same for thread_local variables. */
4623 handle_tls_init ();
4625 /* Go through the set of inline functions whose bodies have not
4626 been emitted yet. If out-of-line copies of these functions
4627 are required, emit them. */
4628 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4630 /* Does it need synthesizing? */
4631 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4632 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4634 /* Even though we're already at the top-level, we push
4635 there again. That way, when we pop back a few lines
4636 hence, all of our state is restored. Otherwise,
4637 finish_function doesn't clean things up, and we end
4638 up with CURRENT_FUNCTION_DECL set. */
4639 push_to_top_level ();
4640 /* The decl's location will mark where it was first
4641 needed. Save that so synthesize method can indicate
4642 where it was needed from, in case of error */
4643 input_location = DECL_SOURCE_LOCATION (decl);
4644 synthesize_method (decl);
4645 pop_from_top_level ();
4646 reconsider = true;
4649 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4650 generate_tls_wrapper (decl);
4652 if (!DECL_SAVED_TREE (decl))
4653 continue;
4655 /* We lie to the back end, pretending that some functions
4656 are not defined when they really are. This keeps these
4657 functions from being put out unnecessarily. But, we must
4658 stop lying when the functions are referenced, or if they
4659 are not comdat since they need to be put out now. If
4660 DECL_INTERFACE_KNOWN, then we have already set
4661 DECL_EXTERNAL appropriately, so there's no need to check
4662 again, and we do not want to clear DECL_EXTERNAL if a
4663 previous call to import_export_decl set it.
4665 This is done in a separate for cycle, because if some
4666 deferred function is contained in another deferred
4667 function later in deferred_fns varray,
4668 rest_of_compilation would skip this function and we
4669 really cannot expand the same function twice. */
4670 import_export_decl (decl);
4671 if (DECL_NOT_REALLY_EXTERN (decl)
4672 && DECL_INITIAL (decl)
4673 && decl_needed_p (decl))
4675 struct cgraph_node *node, *next;
4677 node = cgraph_node::get (decl);
4678 if (node->cpp_implicit_alias)
4679 node = node->get_alias_target ();
4681 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4682 NULL, true);
4683 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4684 group, we need to mark all symbols in the same comdat group
4685 that way. */
4686 if (node->same_comdat_group)
4687 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4688 next != node;
4689 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4690 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4691 NULL, true);
4694 /* If we're going to need to write this function out, and
4695 there's already a body for it, create RTL for it now.
4696 (There might be no body if this is a method we haven't
4697 gotten around to synthesizing yet.) */
4698 if (!DECL_EXTERNAL (decl)
4699 && decl_needed_p (decl)
4700 && !TREE_ASM_WRITTEN (decl)
4701 && !cgraph_node::get (decl)->definition)
4703 /* We will output the function; no longer consider it in this
4704 loop. */
4705 DECL_DEFER_OUTPUT (decl) = 0;
4706 /* Generate RTL for this function now that we know we
4707 need it. */
4708 expand_or_defer_fn (decl);
4709 /* If we're compiling -fsyntax-only pretend that this
4710 function has been written out so that we don't try to
4711 expand it again. */
4712 if (flag_syntax_only)
4713 TREE_ASM_WRITTEN (decl) = 1;
4714 reconsider = true;
4718 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4719 reconsider = true;
4721 /* Static data members are just like namespace-scope globals. */
4722 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4724 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4725 /* Don't write it out if we haven't seen a definition. */
4726 || DECL_IN_AGGR_P (decl))
4727 continue;
4728 import_export_decl (decl);
4729 /* If this static data member is needed, provide it to the
4730 back end. */
4731 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4732 DECL_EXTERNAL (decl) = 0;
4734 if (vec_safe_length (pending_statics) != 0
4735 && wrapup_global_declarations (pending_statics->address (),
4736 pending_statics->length ()))
4737 reconsider = true;
4739 retries++;
4741 while (reconsider);
4743 generate_mangling_aliases ();
4745 /* All used inline functions must have a definition at this point. */
4746 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4748 if (/* Check online inline functions that were actually used. */
4749 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4750 /* If the definition actually was available here, then the
4751 fact that the function was not defined merely represents
4752 that for some reason (use of a template repository,
4753 #pragma interface, etc.) we decided not to emit the
4754 definition here. */
4755 && !DECL_INITIAL (decl)
4756 /* Don't complain if the template was defined. */
4757 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4758 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4759 (template_for_substitution (decl)))))
4761 warning (0, "inline function %q+D used but never defined", decl);
4762 /* Avoid a duplicate warning from check_global_declaration. */
4763 TREE_NO_WARNING (decl) = 1;
4767 /* So must decls that use a type with no linkage. */
4768 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4769 no_linkage_error (decl);
4771 maybe_warn_sized_delete ();
4773 /* Then, do the Objective-C stuff. This is where all the
4774 Objective-C module stuff gets generated (symtab,
4775 class/protocol/selector lists etc). This must be done after C++
4776 templates, destructors etc. so that selectors used in C++
4777 templates are properly allocated. */
4778 if (c_dialect_objc ())
4779 objc_write_global_declarations ();
4781 /* We give C linkage to static constructors and destructors. */
4782 push_lang_context (lang_name_c);
4784 /* Generate initialization and destruction functions for all
4785 priorities for which they are required. */
4786 if (priority_info_map)
4787 splay_tree_foreach (priority_info_map,
4788 generate_ctor_and_dtor_functions_for_priority,
4789 /*data=*/&locus_at_end_of_parsing);
4790 else if (c_dialect_objc () && objc_static_init_needed_p ())
4791 /* If this is obj-c++ and we need a static init, call
4792 generate_ctor_or_dtor_function. */
4793 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4794 DEFAULT_INIT_PRIORITY,
4795 &locus_at_end_of_parsing);
4797 /* We're done with the splay-tree now. */
4798 if (priority_info_map)
4799 splay_tree_delete (priority_info_map);
4801 /* Generate any missing aliases. */
4802 maybe_apply_pending_pragma_weaks ();
4804 /* We're done with static constructors, so we can go back to "C++"
4805 linkage now. */
4806 pop_lang_context ();
4808 /* Generate Java hidden aliases. */
4809 build_java_method_aliases ();
4811 if (flag_vtable_verify)
4813 vtv_recover_class_info ();
4814 vtv_compute_class_hierarchy_transitive_closure ();
4815 vtv_build_vtable_verify_fndecl ();
4818 perform_deferred_noexcept_checks ();
4820 finish_repo ();
4822 /* The entire file is now complete. If requested, dump everything
4823 to a file. */
4824 dump_tu ();
4826 if (flag_detailed_statistics)
4828 dump_tree_statistics ();
4829 dump_time_statistics ();
4832 timevar_stop (TV_PHASE_DEFERRED);
4833 timevar_start (TV_PHASE_PARSING);
4836 /* Perform any post compilation-proper cleanups for the C++ front-end.
4837 This should really go away. No front-end should need to do
4838 anything past the compilation process. */
4840 void
4841 cxx_post_compilation_parsing_cleanups (void)
4843 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4845 if (flag_vtable_verify)
4847 /* Generate the special constructor initialization function that
4848 calls __VLTRegisterPairs, and give it a very high
4849 initialization priority. This must be done after
4850 finalize_compilation_unit so that we have accurate
4851 information about which vtable will actually be emitted. */
4852 vtv_generate_init_routine ();
4855 input_location = locus_at_end_of_parsing;
4857 #ifdef ENABLE_CHECKING
4858 validate_conversion_obstack ();
4859 #endif /* ENABLE_CHECKING */
4861 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4864 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4865 function to call in parse-tree form; it has not yet been
4866 semantically analyzed. ARGS are the arguments to the function.
4867 They have already been semantically analyzed. This may change
4868 ARGS. */
4870 tree
4871 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4872 tsubst_flags_t complain)
4874 tree orig_fn;
4875 vec<tree, va_gc> *orig_args = NULL;
4876 tree expr;
4877 tree object;
4879 orig_fn = fn;
4880 object = TREE_OPERAND (fn, 0);
4882 if (processing_template_decl)
4884 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4885 || TREE_CODE (fn) == MEMBER_REF);
4886 if (type_dependent_expression_p (fn)
4887 || any_type_dependent_arguments_p (*args))
4888 return build_nt_call_vec (fn, *args);
4890 orig_args = make_tree_vector_copy (*args);
4892 /* Transform the arguments and add the implicit "this"
4893 parameter. That must be done before the FN is transformed
4894 because we depend on the form of FN. */
4895 make_args_non_dependent (*args);
4896 object = build_non_dependent_expr (object);
4897 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4899 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4900 object = cp_build_addr_expr (object, complain);
4901 vec_safe_insert (*args, 0, object);
4903 /* Now that the arguments are done, transform FN. */
4904 fn = build_non_dependent_expr (fn);
4907 /* A qualified name corresponding to a bound pointer-to-member is
4908 represented as an OFFSET_REF:
4910 struct B { void g(); };
4911 void (B::*p)();
4912 void B::g() { (this->*p)(); } */
4913 if (TREE_CODE (fn) == OFFSET_REF)
4915 tree object_addr = cp_build_addr_expr (object, complain);
4916 fn = TREE_OPERAND (fn, 1);
4917 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4918 complain);
4919 vec_safe_insert (*args, 0, object_addr);
4922 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4923 expr = build_op_call (fn, args, complain);
4924 else
4925 expr = cp_build_function_call_vec (fn, args, complain);
4926 if (processing_template_decl && expr != error_mark_node)
4927 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4929 if (orig_args != NULL)
4930 release_tree_vector (orig_args);
4932 return expr;
4936 void
4937 check_default_args (tree x)
4939 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4940 bool saw_def = false;
4941 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4942 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4944 if (TREE_PURPOSE (arg))
4945 saw_def = true;
4946 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4948 error ("default argument missing for parameter %P of %q+#D", i, x);
4949 TREE_PURPOSE (arg) = error_mark_node;
4954 /* Return true if function DECL can be inlined. This is used to force
4955 instantiation of methods that might be interesting for inlining. */
4956 bool
4957 possibly_inlined_p (tree decl)
4959 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4960 if (DECL_UNINLINABLE (decl))
4961 return false;
4962 if (!optimize || pragma_java_exceptions)
4963 return DECL_DECLARED_INLINE_P (decl);
4964 /* When optimizing, we might inline everything when flatten
4965 attribute or heuristics inlining for size or autoinlining
4966 is used. */
4967 return true;
4970 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4971 If DECL is a specialization or implicitly declared class member,
4972 generate the actual definition. Return false if something goes
4973 wrong, true otherwise. */
4975 bool
4976 mark_used (tree decl, tsubst_flags_t complain)
4978 /* If DECL is a BASELINK for a single function, then treat it just
4979 like the DECL for the function. Otherwise, if the BASELINK is
4980 for an overloaded function, we don't know which function was
4981 actually used until after overload resolution. */
4982 if (BASELINK_P (decl))
4984 decl = BASELINK_FUNCTIONS (decl);
4985 if (really_overloaded_fn (decl))
4986 return true;
4987 decl = OVL_CURRENT (decl);
4990 /* Set TREE_USED for the benefit of -Wunused. */
4991 TREE_USED (decl) = 1;
4992 if (DECL_CLONED_FUNCTION_P (decl))
4993 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4995 /* Mark enumeration types as used. */
4996 if (TREE_CODE (decl) == CONST_DECL)
4997 used_types_insert (DECL_CONTEXT (decl));
4999 if (TREE_CODE (decl) == FUNCTION_DECL)
5000 maybe_instantiate_noexcept (decl);
5002 if (TREE_CODE (decl) == FUNCTION_DECL
5003 && DECL_DELETED_FN (decl))
5005 if (DECL_ARTIFICIAL (decl))
5007 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5008 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5010 /* We mark a lambda conversion op as deleted if we can't
5011 generate it properly; see maybe_add_lambda_conv_op. */
5012 sorry ("converting lambda which uses %<...%> to "
5013 "function pointer");
5014 return false;
5017 if (complain & tf_error)
5019 error ("use of deleted function %qD", decl);
5020 if (!maybe_explain_implicit_delete (decl))
5021 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5023 return false;
5026 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5027 && deprecated_state != DEPRECATED_SUPPRESS)
5028 warn_deprecated_use (decl, NULL_TREE);
5030 /* We can only check DECL_ODR_USED on variables or functions with
5031 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5032 might need special handling for. */
5033 if (!VAR_OR_FUNCTION_DECL_P (decl)
5034 || DECL_LANG_SPECIFIC (decl) == NULL
5035 || DECL_THUNK_P (decl))
5037 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
5039 if (complain & tf_error)
5040 error ("use of %qD before deduction of %<auto%>", decl);
5041 return false;
5043 return true;
5046 /* We only want to do this processing once. We don't need to keep trying
5047 to instantiate inline templates, because unit-at-a-time will make sure
5048 we get them compiled before functions that want to inline them. */
5049 if (DECL_ODR_USED (decl))
5050 return true;
5052 /* If within finish_function, defer the rest until that function
5053 finishes, otherwise it might recurse. */
5054 if (defer_mark_used_calls)
5056 vec_safe_push (deferred_mark_used_calls, decl);
5057 return true;
5060 /* Normally, we can wait until instantiation-time to synthesize DECL.
5061 However, if DECL is a static data member initialized with a constant
5062 or a constexpr function, we need it right now because a reference to
5063 such a data member or a call to such function is not value-dependent.
5064 For a function that uses auto in the return type, we need to instantiate
5065 it to find out its type. For OpenMP user defined reductions, we need
5066 them instantiated for reduction clauses which inline them by hand
5067 directly. */
5068 if (DECL_LANG_SPECIFIC (decl)
5069 && DECL_TEMPLATE_INFO (decl)
5070 && (decl_maybe_constant_var_p (decl)
5071 || (TREE_CODE (decl) == FUNCTION_DECL
5072 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5073 || undeduced_auto_decl (decl))
5074 && !uses_template_parms (DECL_TI_ARGS (decl)))
5076 /* Instantiating a function will result in garbage collection. We
5077 must treat this situation as if we were within the body of a
5078 function so as to avoid collecting live data only referenced from
5079 the stack (such as overload resolution candidates). */
5080 ++function_depth;
5081 instantiate_decl (decl, /*defer_ok=*/false,
5082 /*expl_inst_class_mem_p=*/false);
5083 --function_depth;
5086 if (processing_template_decl || in_template_function ())
5087 return true;
5089 /* Check this too in case we're within instantiate_non_dependent_expr. */
5090 if (DECL_TEMPLATE_INFO (decl)
5091 && uses_template_parms (DECL_TI_ARGS (decl)))
5092 return true;
5094 if (undeduced_auto_decl (decl))
5096 if (complain & tf_error)
5097 error ("use of %qD before deduction of %<auto%>", decl);
5098 return false;
5101 /* If we don't need a value, then we don't need to synthesize DECL. */
5102 if (cp_unevaluated_operand != 0)
5103 return true;
5105 DECL_ODR_USED (decl) = 1;
5106 if (DECL_CLONED_FUNCTION_P (decl))
5107 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5109 /* DR 757: A type without linkage shall not be used as the type of a
5110 variable or function with linkage, unless
5111 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5112 o the variable or function is not used (3.2 [basic.def.odr]) or is
5113 defined in the same translation unit. */
5114 if (cxx_dialect > cxx98
5115 && decl_linkage (decl) != lk_none
5116 && !DECL_EXTERN_C_P (decl)
5117 && !DECL_ARTIFICIAL (decl)
5118 && !decl_defined_p (decl)
5119 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5121 if (is_local_extern (decl))
5122 /* There's no way to define a local extern, and adding it to
5123 the vector interferes with GC, so give an error now. */
5124 no_linkage_error (decl);
5125 else
5126 vec_safe_push (no_linkage_decls, decl);
5129 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5130 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5131 /* Remember it, so we can check it was defined. */
5132 note_vague_linkage_fn (decl);
5134 /* Is it a synthesized method that needs to be synthesized? */
5135 if (TREE_CODE (decl) == FUNCTION_DECL
5136 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5137 && DECL_DEFAULTED_FN (decl)
5138 /* A function defaulted outside the class is synthesized either by
5139 cp_finish_decl or instantiate_decl. */
5140 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5141 && ! DECL_INITIAL (decl))
5143 /* Defer virtual destructors so that thunks get the right
5144 linkage. */
5145 if (DECL_VIRTUAL_P (decl) && !at_eof)
5147 note_vague_linkage_fn (decl);
5148 return true;
5151 /* Remember the current location for a function we will end up
5152 synthesizing. Then we can inform the user where it was
5153 required in the case of error. */
5154 DECL_SOURCE_LOCATION (decl) = input_location;
5156 /* Synthesizing an implicitly defined member function will result in
5157 garbage collection. We must treat this situation as if we were
5158 within the body of a function so as to avoid collecting live data
5159 on the stack (such as overload resolution candidates).
5161 We could just let cp_write_global_declarations handle synthesizing
5162 this function by adding it to deferred_fns, but doing
5163 it at the use site produces better error messages. */
5164 ++function_depth;
5165 synthesize_method (decl);
5166 --function_depth;
5167 /* If this is a synthesized method we don't need to
5168 do the instantiation test below. */
5170 else if (VAR_OR_FUNCTION_DECL_P (decl)
5171 && DECL_TEMPLATE_INFO (decl)
5172 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5173 || always_instantiate_p (decl)))
5174 /* If this is a function or variable that is an instance of some
5175 template, we now know that we will need to actually do the
5176 instantiation. We check that DECL is not an explicit
5177 instantiation because that is not checked in instantiate_decl.
5179 We put off instantiating functions in order to improve compile
5180 times. Maintaining a stack of active functions is expensive,
5181 and the inliner knows to instantiate any functions it might
5182 need. Therefore, we always try to defer instantiation. */
5184 ++function_depth;
5185 instantiate_decl (decl, /*defer_ok=*/true,
5186 /*expl_inst_class_mem_p=*/false);
5187 --function_depth;
5190 return true;
5193 bool
5194 mark_used (tree decl)
5196 return mark_used (decl, tf_warning_or_error);
5199 tree
5200 vtv_start_verification_constructor_init_function (void)
5202 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5205 tree
5206 vtv_finish_verification_constructor_init_function (tree function_body)
5208 tree fn;
5210 finish_compound_stmt (function_body);
5211 fn = finish_function (0);
5212 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5213 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5215 return fn;
5218 #include "gt-cp-decl2.h"