2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / cp / decl2.c
bloba5b44e02c141a66d6e319028fc4de004043743eb
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 "tree.h"
35 #include "stringpool.h"
36 #include "varasm.h"
37 #include "attribs.h"
38 #include "stor-layout.h"
39 #include "calls.h"
40 #include "flags.h"
41 #include "cp-tree.h"
42 #include "decl.h"
43 #include "toplev.h"
44 #include "timevar.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "c-family/c-common.h"
48 #include "c-family/c-objc.h"
49 #include "hard-reg-set.h"
50 #include "function.h"
51 #include "cgraph.h"
52 #include "tree-inline.h"
53 #include "c-family/c-pragma.h"
54 #include "dumpfile.h"
55 #include "intl.h"
56 #include "splay-tree.h"
57 #include "langhooks.h"
58 #include "c-family/c-ada-spec.h"
59 #include "asan.h"
61 extern cpp_reader *parse_in;
63 /* This structure contains information about the initializations
64 and/or destructions required for a particular priority level. */
65 typedef struct priority_info_s {
66 /* Nonzero if there have been any initializations at this priority
67 throughout the translation unit. */
68 int initializations_p;
69 /* Nonzero if there have been any destructions at this priority
70 throughout the translation unit. */
71 int destructions_p;
72 } *priority_info;
74 static void mark_vtable_entries (tree);
75 static bool maybe_emit_vtables (tree);
76 static bool acceptable_java_type (tree);
77 static tree start_objects (int, int);
78 static void finish_objects (int, int, tree);
79 static tree start_static_storage_duration_function (unsigned);
80 static void finish_static_storage_duration_function (tree);
81 static priority_info get_priority_info (int);
82 static void do_static_initialization_or_destruction (tree, bool);
83 static void one_static_initialization_or_destruction (tree, tree, bool);
84 static void generate_ctor_or_dtor_function (bool, int, location_t *);
85 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
86 void *);
87 static tree prune_vars_needing_no_initialization (tree *);
88 static void write_out_vars (tree);
89 static void import_export_class (tree);
90 static tree get_guard_bits (tree);
91 static void determine_visibility_from_class (tree, tree);
92 static bool determine_hidden_inline (tree);
93 static bool decl_defined_p (tree);
95 /* A list of static class variables. This is needed, because a
96 static class variable can be declared inside the class without
97 an initializer, and then initialized, statically, outside the class. */
98 static GTY(()) vec<tree, va_gc> *pending_statics;
100 /* A list of functions which were declared inline, but which we
101 may need to emit outline anyway. */
102 static GTY(()) vec<tree, va_gc> *deferred_fns;
104 /* A list of decls that use types with no linkage, which we need to make
105 sure are defined. */
106 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
108 /* A vector of alternating decls and identifiers, where the latter
109 is to be an alias for the former if the former is defined. */
110 static GTY(()) vec<tree, va_gc> *mangling_aliases;
112 /* Nonzero if we're done parsing and into end-of-file activities. */
114 int at_eof;
117 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
118 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
119 that apply to the function). */
121 tree
122 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
123 cp_ref_qualifier rqual)
125 tree raises;
126 tree attrs;
127 int type_quals;
128 bool late_return_type_p;
130 if (fntype == error_mark_node || ctype == error_mark_node)
131 return error_mark_node;
133 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
134 || TREE_CODE (fntype) == METHOD_TYPE);
136 type_quals = quals & ~TYPE_QUAL_RESTRICT;
137 ctype = cp_build_qualified_type (ctype, type_quals);
138 raises = TYPE_RAISES_EXCEPTIONS (fntype);
139 attrs = TYPE_ATTRIBUTES (fntype);
140 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
141 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
142 (TREE_CODE (fntype) == METHOD_TYPE
143 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
144 : TYPE_ARG_TYPES (fntype)));
145 if (attrs)
146 fntype = cp_build_type_attribute_variant (fntype, attrs);
147 if (rqual)
148 fntype = build_ref_qualified_type (fntype, rqual);
149 if (raises)
150 fntype = build_exception_variant (fntype, raises);
151 if (late_return_type_p)
152 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
154 return fntype;
157 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
158 return type changed to NEW_RET. */
160 tree
161 change_return_type (tree new_ret, tree fntype)
163 tree newtype;
164 tree args = TYPE_ARG_TYPES (fntype);
165 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
166 tree attrs = TYPE_ATTRIBUTES (fntype);
167 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
169 if (new_ret == error_mark_node)
170 return fntype;
172 if (same_type_p (new_ret, TREE_TYPE (fntype)))
173 return fntype;
175 if (TREE_CODE (fntype) == FUNCTION_TYPE)
177 newtype = build_function_type (new_ret, args);
178 newtype = apply_memfn_quals (newtype,
179 type_memfn_quals (fntype),
180 type_memfn_rqual (fntype));
182 else
183 newtype = build_method_type_directly
184 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
185 if (FUNCTION_REF_QUALIFIED (fntype))
186 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
187 if (raises)
188 newtype = build_exception_variant (newtype, raises);
189 if (attrs)
190 newtype = cp_build_type_attribute_variant (newtype, attrs);
191 if (late_return_type_p)
192 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
194 return newtype;
197 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
198 appropriately. */
200 tree
201 cp_build_parm_decl (tree name, tree type)
203 tree parm = build_decl (input_location,
204 PARM_DECL, name, type);
205 /* DECL_ARG_TYPE is only used by the back end and the back end never
206 sees templates. */
207 if (!processing_template_decl)
208 DECL_ARG_TYPE (parm) = type_passed_as (type);
210 return parm;
213 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
214 indicated NAME. */
216 tree
217 build_artificial_parm (tree name, tree type)
219 tree parm = cp_build_parm_decl (name, type);
220 DECL_ARTIFICIAL (parm) = 1;
221 /* All our artificial parms are implicitly `const'; they cannot be
222 assigned to. */
223 TREE_READONLY (parm) = 1;
224 return parm;
227 /* Constructors for types with virtual baseclasses need an "in-charge" flag
228 saying whether this constructor is responsible for initialization of
229 virtual baseclasses or not. All destructors also need this "in-charge"
230 flag, which additionally determines whether or not the destructor should
231 free the memory for the object.
233 This function adds the "in-charge" flag to member function FN if
234 appropriate. It is called from grokclassfn and tsubst.
235 FN must be either a constructor or destructor.
237 The in-charge flag follows the 'this' parameter, and is followed by the
238 VTT parm (if any), then the user-written parms. */
240 void
241 maybe_retrofit_in_chrg (tree fn)
243 tree basetype, arg_types, parms, parm, fntype;
245 /* If we've already add the in-charge parameter don't do it again. */
246 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
247 return;
249 /* When processing templates we can't know, in general, whether or
250 not we're going to have virtual baseclasses. */
251 if (processing_template_decl)
252 return;
254 /* We don't need an in-charge parameter for constructors that don't
255 have virtual bases. */
256 if (DECL_CONSTRUCTOR_P (fn)
257 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
258 return;
260 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
261 basetype = TREE_TYPE (TREE_VALUE (arg_types));
262 arg_types = TREE_CHAIN (arg_types);
264 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
266 /* If this is a subobject constructor or destructor, our caller will
267 pass us a pointer to our VTT. */
268 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
270 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
272 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
273 DECL_CHAIN (parm) = parms;
274 parms = parm;
276 /* ...and then to TYPE_ARG_TYPES. */
277 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
279 DECL_HAS_VTT_PARM_P (fn) = 1;
282 /* Then add the in-charge parm (before the VTT parm). */
283 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
284 DECL_CHAIN (parm) = parms;
285 parms = parm;
286 arg_types = hash_tree_chain (integer_type_node, arg_types);
288 /* Insert our new parameter(s) into the list. */
289 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
291 /* And rebuild the function type. */
292 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
293 arg_types);
294 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
295 fntype = build_exception_variant (fntype,
296 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
297 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
298 fntype = (cp_build_type_attribute_variant
299 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
300 TREE_TYPE (fn) = fntype;
302 /* Now we've got the in-charge parameter. */
303 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
306 /* Classes overload their constituent function names automatically.
307 When a function name is declared in a record structure,
308 its name is changed to it overloaded name. Since names for
309 constructors and destructors can conflict, we place a leading
310 '$' for destructors.
312 CNAME is the name of the class we are grokking for.
314 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
316 FLAGS contains bits saying what's special about today's
317 arguments. DTOR_FLAG == DESTRUCTOR.
319 If FUNCTION is a destructor, then we must add the `auto-delete' field
320 as a second parameter. There is some hair associated with the fact
321 that we must "declare" this variable in the manner consistent with the
322 way the rest of the arguments were declared.
324 QUALS are the qualifiers for the this pointer. */
326 void
327 grokclassfn (tree ctype, tree function, enum overload_flags flags)
329 tree fn_name = DECL_NAME (function);
331 /* Even within an `extern "C"' block, members get C++ linkage. See
332 [dcl.link] for details. */
333 SET_DECL_LANGUAGE (function, lang_cplusplus);
335 if (fn_name == NULL_TREE)
337 error ("name missing for member function");
338 fn_name = get_identifier ("<anonymous>");
339 DECL_NAME (function) = fn_name;
342 DECL_CONTEXT (function) = ctype;
344 if (flags == DTOR_FLAG)
345 DECL_DESTRUCTOR_P (function) = 1;
347 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
348 maybe_retrofit_in_chrg (function);
351 /* Create an ARRAY_REF, checking for the user doing things backwards
352 along the way. DECLTYPE_P is for N3276, as in the parser. */
354 tree
355 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
356 bool decltype_p)
358 tree type;
359 tree expr;
360 tree orig_array_expr = array_expr;
361 tree orig_index_exp = index_exp;
363 if (error_operand_p (array_expr) || error_operand_p (index_exp))
364 return error_mark_node;
366 if (processing_template_decl)
368 if (type_dependent_expression_p (array_expr)
369 || type_dependent_expression_p (index_exp))
370 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
371 NULL_TREE, NULL_TREE);
372 array_expr = build_non_dependent_expr (array_expr);
373 index_exp = build_non_dependent_expr (index_exp);
376 type = TREE_TYPE (array_expr);
377 gcc_assert (type);
378 type = non_reference (type);
380 /* If they have an `operator[]', use that. */
381 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
383 tsubst_flags_t complain = tf_warning_or_error;
384 if (decltype_p)
385 complain |= tf_decltype;
386 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
387 index_exp, NULL_TREE, /*overload=*/NULL, complain);
389 else
391 tree p1, p2, i1, i2;
393 /* Otherwise, create an ARRAY_REF for a pointer or array type.
394 It is a little-known fact that, if `a' is an array and `i' is
395 an int, you can write `i[a]', which means the same thing as
396 `a[i]'. */
397 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
398 p1 = array_expr;
399 else
400 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
402 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
403 p2 = index_exp;
404 else
405 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
407 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
408 false);
409 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
410 false);
412 if ((p1 && i2) && (i1 && p2))
413 error ("ambiguous conversion for array subscript");
415 if (p1 && i2)
416 array_expr = p1, index_exp = i2;
417 else if (i1 && p2)
418 array_expr = p2, index_exp = i1;
419 else
421 error ("invalid types %<%T[%T]%> for array subscript",
422 type, TREE_TYPE (index_exp));
423 return error_mark_node;
426 if (array_expr == error_mark_node || index_exp == error_mark_node)
427 error ("ambiguous conversion for array subscript");
429 expr = build_array_ref (input_location, array_expr, index_exp);
431 if (processing_template_decl && expr != error_mark_node)
432 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
433 NULL_TREE, NULL_TREE);
434 return expr;
437 /* Given the cast expression EXP, checking out its validity. Either return
438 an error_mark_node if there was an unavoidable error, return a cast to
439 void for trying to delete a pointer w/ the value 0, or return the
440 call to delete. If DOING_VEC is true, we handle things differently
441 for doing an array delete.
442 Implements ARM $5.3.4. This is called from the parser. */
444 tree
445 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
446 tsubst_flags_t complain)
448 tree t, type;
450 if (exp == error_mark_node)
451 return exp;
453 if (processing_template_decl)
455 t = build_min (DELETE_EXPR, void_type_node, exp, size);
456 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
457 DELETE_EXPR_USE_VEC (t) = doing_vec;
458 TREE_SIDE_EFFECTS (t) = 1;
459 return t;
462 /* An array can't have been allocated by new, so complain. */
463 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
464 warning (0, "deleting array %q#E", exp);
466 t = build_expr_type_conversion (WANT_POINTER, exp, true);
468 if (t == NULL_TREE || t == error_mark_node)
470 error ("type %q#T argument given to %<delete%>, expected pointer",
471 TREE_TYPE (exp));
472 return error_mark_node;
475 type = TREE_TYPE (t);
477 /* As of Valley Forge, you can delete a pointer to const. */
479 /* You can't delete functions. */
480 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
482 error ("cannot delete a function. Only pointer-to-objects are "
483 "valid arguments to %<delete%>");
484 return error_mark_node;
487 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
488 if (VOID_TYPE_P (TREE_TYPE (type)))
490 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
491 doing_vec = 0;
494 /* Deleting a pointer with the value zero is valid and has no effect. */
495 if (integer_zerop (t))
496 return build1 (NOP_EXPR, void_type_node, t);
498 if (doing_vec)
499 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
500 sfk_deleting_destructor,
501 use_global_delete, complain);
502 else
503 return build_delete (type, t, sfk_deleting_destructor,
504 LOOKUP_NORMAL, use_global_delete,
505 complain);
508 /* Report an error if the indicated template declaration is not the
509 sort of thing that should be a member template. */
511 void
512 check_member_template (tree tmpl)
514 tree decl;
516 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
517 decl = DECL_TEMPLATE_RESULT (tmpl);
519 if (TREE_CODE (decl) == FUNCTION_DECL
520 || DECL_ALIAS_TEMPLATE_P (tmpl)
521 || (TREE_CODE (decl) == TYPE_DECL
522 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
524 /* The parser rejects template declarations in local classes
525 (with the exception of generic lambdas). */
526 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
527 /* The parser rejects any use of virtual in a function template. */
528 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
529 && DECL_VIRTUAL_P (decl)));
531 /* The debug-information generating code doesn't know what to do
532 with member templates. */
533 DECL_IGNORED_P (tmpl) = 1;
535 else if (variable_template_p (tmpl))
536 /* OK */;
537 else
538 error ("template declaration of %q#D", decl);
541 /* Return true iff TYPE is a valid Java parameter or return type. */
543 static bool
544 acceptable_java_type (tree type)
546 if (type == error_mark_node)
547 return false;
549 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
550 return true;
551 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
553 type = TREE_TYPE (type);
554 if (TREE_CODE (type) == RECORD_TYPE)
556 tree args; int i;
557 if (! TYPE_FOR_JAVA (type))
558 return false;
559 if (! CLASSTYPE_TEMPLATE_INFO (type))
560 return true;
561 args = CLASSTYPE_TI_ARGS (type);
562 i = TREE_VEC_LENGTH (args);
563 while (--i >= 0)
565 type = TREE_VEC_ELT (args, i);
566 if (TYPE_PTR_P (type))
567 type = TREE_TYPE (type);
568 if (! TYPE_FOR_JAVA (type))
569 return false;
571 return true;
574 return false;
577 /* For a METHOD in a Java class CTYPE, return true if
578 the parameter and return types are valid Java types.
579 Otherwise, print appropriate error messages, and return false. */
581 bool
582 check_java_method (tree method)
584 bool jerr = false;
585 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
586 tree ret_type = TREE_TYPE (TREE_TYPE (method));
588 if (!acceptable_java_type (ret_type))
590 error ("Java method %qD has non-Java return type %qT",
591 method, ret_type);
592 jerr = true;
595 arg_types = TREE_CHAIN (arg_types);
596 if (DECL_HAS_IN_CHARGE_PARM_P (method))
597 arg_types = TREE_CHAIN (arg_types);
598 if (DECL_HAS_VTT_PARM_P (method))
599 arg_types = TREE_CHAIN (arg_types);
601 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
603 tree type = TREE_VALUE (arg_types);
604 if (!acceptable_java_type (type))
606 if (type != error_mark_node)
607 error ("Java method %qD has non-Java parameter type %qT",
608 method, type);
609 jerr = true;
612 return !jerr;
615 /* Sanity check: report error if this function FUNCTION is not
616 really a member of the class (CTYPE) it is supposed to belong to.
617 TEMPLATE_PARMS is used to specify the template parameters of a member
618 template passed as FUNCTION_DECL. If the member template is passed as a
619 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
620 from the declaration. If the function is not a function template, it
621 must be NULL.
622 It returns the original declaration for the function, NULL_TREE if
623 no declaration was found, error_mark_node if an error was emitted. */
625 tree
626 check_classfn (tree ctype, tree function, tree template_parms)
628 int ix;
629 bool is_template;
630 tree pushed_scope;
632 if (DECL_USE_TEMPLATE (function)
633 && !(TREE_CODE (function) == TEMPLATE_DECL
634 && DECL_TEMPLATE_SPECIALIZATION (function))
635 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
636 /* Since this is a specialization of a member template,
637 we're not going to find the declaration in the class.
638 For example, in:
640 struct S { template <typename T> void f(T); };
641 template <> void S::f(int);
643 we're not going to find `S::f(int)', but there's no
644 reason we should, either. We let our callers know we didn't
645 find the method, but we don't complain. */
646 return NULL_TREE;
648 /* Basic sanity check: for a template function, the template parameters
649 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
650 if (TREE_CODE (function) == TEMPLATE_DECL)
652 if (template_parms
653 && !comp_template_parms (template_parms,
654 DECL_TEMPLATE_PARMS (function)))
656 error ("template parameter lists provided don%'t match the "
657 "template parameters of %qD", function);
658 return error_mark_node;
660 template_parms = DECL_TEMPLATE_PARMS (function);
663 /* OK, is this a definition of a member template? */
664 is_template = (template_parms != NULL_TREE);
666 /* [temp.mem]
668 A destructor shall not be a member template. */
669 if (DECL_DESTRUCTOR_P (function) && is_template)
671 error ("destructor %qD declared as member template", function);
672 return error_mark_node;
675 /* We must enter the scope here, because conversion operators are
676 named by target type, and type equivalence relies on typenames
677 resolving within the scope of CTYPE. */
678 pushed_scope = push_scope (ctype);
679 ix = class_method_index_for_fn (complete_type (ctype), function);
680 if (ix >= 0)
682 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
683 tree fndecls, fndecl = 0;
684 bool is_conv_op;
685 const char *format = NULL;
687 for (fndecls = (*methods)[ix];
688 fndecls; fndecls = OVL_NEXT (fndecls))
690 tree p1, p2;
692 fndecl = OVL_CURRENT (fndecls);
693 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
694 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
696 /* We cannot simply call decls_match because this doesn't
697 work for static member functions that are pretending to
698 be methods, and because the name may have been changed by
699 asm("new_name"). */
701 /* Get rid of the this parameter on functions that become
702 static. */
703 if (DECL_STATIC_FUNCTION_P (fndecl)
704 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
705 p1 = TREE_CHAIN (p1);
707 /* A member template definition only matches a member template
708 declaration. */
709 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
710 continue;
712 /* ref-qualifier or absence of same must match. */
713 if (type_memfn_rqual (TREE_TYPE (function))
714 != type_memfn_rqual (TREE_TYPE (fndecl)))
715 continue;
717 // Include constraints in the match.
718 tree c1 = get_constraints (function);
719 tree c2 = get_constraints (fndecl);
721 /* While finding a match, same types and params are not enough
722 if the function is versioned. Also check version ("target")
723 attributes. */
724 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
725 TREE_TYPE (TREE_TYPE (fndecl)))
726 && compparms (p1, p2)
727 && !targetm.target_option.function_versions (function, fndecl)
728 && (!is_template
729 || comp_template_parms (template_parms,
730 DECL_TEMPLATE_PARMS (fndecl)))
731 && equivalent_constraints (c1, c2)
732 && (DECL_TEMPLATE_SPECIALIZATION (function)
733 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
734 && (!DECL_TEMPLATE_SPECIALIZATION (function)
735 || (DECL_TI_TEMPLATE (function)
736 == DECL_TI_TEMPLATE (fndecl))))
737 break;
739 if (fndecls)
741 if (pushed_scope)
742 pop_scope (pushed_scope);
743 return OVL_CURRENT (fndecls);
746 error_at (DECL_SOURCE_LOCATION (function),
747 "prototype for %q#D does not match any in class %qT",
748 function, ctype);
749 is_conv_op = DECL_CONV_FN_P (fndecl);
751 if (is_conv_op)
752 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
753 fndecls = (*methods)[ix];
754 while (fndecls)
756 fndecl = OVL_CURRENT (fndecls);
757 fndecls = OVL_NEXT (fndecls);
759 if (!fndecls && is_conv_op)
761 if (methods->length () > (size_t) ++ix)
763 fndecls = (*methods)[ix];
764 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
766 fndecls = NULL_TREE;
767 is_conv_op = false;
770 else
771 is_conv_op = false;
773 if (format)
774 format = " %+#D";
775 else if (fndecls)
776 format = N_("candidates are: %+#D");
777 else
778 format = N_("candidate is: %+#D");
779 error (format, fndecl);
782 else if (!COMPLETE_TYPE_P (ctype))
783 cxx_incomplete_type_error (function, ctype);
784 else
785 error ("no %q#D member function declared in class %qT",
786 function, ctype);
788 if (pushed_scope)
789 pop_scope (pushed_scope);
790 return error_mark_node;
793 /* DECL is a function with vague linkage. Remember it so that at the
794 end of the translation unit we can decide whether or not to emit
795 it. */
797 void
798 note_vague_linkage_fn (tree decl)
800 DECL_DEFER_OUTPUT (decl) = 1;
801 vec_safe_push (deferred_fns, decl);
804 /* As above, but for variable template instantiations. */
806 void
807 note_variable_template_instantiation (tree decl)
809 vec_safe_push (pending_statics, decl);
812 /* We have just processed the DECL, which is a static data member.
813 The other parameters are as for cp_finish_decl. */
815 void
816 finish_static_data_member_decl (tree decl,
817 tree init, bool init_const_expr_p,
818 tree asmspec_tree,
819 int flags)
821 DECL_CONTEXT (decl) = current_class_type;
823 /* We cannot call pushdecl here, because that would fill in the
824 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
825 the right thing, namely, to put this decl out straight away. */
827 if (! processing_template_decl)
828 vec_safe_push (pending_statics, decl);
830 if (LOCAL_CLASS_P (current_class_type)
831 /* We already complained about the template definition. */
832 && !DECL_TEMPLATE_INSTANTIATION (decl))
833 permerror (input_location, "local class %q#T shall not have static data member %q#D",
834 current_class_type, decl);
835 else
836 for (tree t = current_class_type; TYPE_P (t);
837 t = CP_TYPE_CONTEXT (t))
838 if (TYPE_ANONYMOUS_P (t))
840 if (permerror (DECL_SOURCE_LOCATION (decl),
841 "static data member %qD in unnamed class", decl))
842 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
843 "unnamed class defined here");
844 break;
847 DECL_IN_AGGR_P (decl) = 1;
849 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
850 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
851 SET_VAR_HAD_UNKNOWN_BOUND (decl);
853 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
856 /* DECLARATOR and DECLSPECS correspond to a class member. The other
857 parameters are as for cp_finish_decl. Return the DECL for the
858 class member declared. */
860 tree
861 grokfield (const cp_declarator *declarator,
862 cp_decl_specifier_seq *declspecs,
863 tree init, bool init_const_expr_p,
864 tree asmspec_tree,
865 tree attrlist)
867 tree value;
868 const char *asmspec = 0;
869 int flags;
870 tree name;
872 if (init
873 && TREE_CODE (init) == TREE_LIST
874 && TREE_VALUE (init) == error_mark_node
875 && TREE_CHAIN (init) == NULL_TREE)
876 init = NULL_TREE;
878 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
879 if (! value || value == error_mark_node)
880 /* friend or constructor went bad. */
881 return error_mark_node;
882 if (TREE_TYPE (value) == error_mark_node)
883 return value;
885 if (TREE_CODE (value) == TYPE_DECL && init)
887 error ("typedef %qD is initialized (use decltype instead)", value);
888 init = NULL_TREE;
891 /* Pass friendly classes back. */
892 if (value == void_type_node)
893 return value;
896 name = DECL_NAME (value);
898 if (name != NULL_TREE)
900 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
902 error ("explicit template argument list not allowed");
903 return error_mark_node;
906 if (IDENTIFIER_POINTER (name)[0] == '_'
907 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
908 error ("member %qD conflicts with virtual function table field name",
909 value);
912 /* Stash away type declarations. */
913 if (TREE_CODE (value) == TYPE_DECL)
915 DECL_NONLOCAL (value) = 1;
916 DECL_CONTEXT (value) = current_class_type;
918 if (attrlist)
920 int attrflags = 0;
922 /* If this is a typedef that names the class for linkage purposes
923 (7.1.3p8), apply any attributes directly to the type. */
924 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
925 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
926 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
928 cplus_decl_attributes (&value, attrlist, attrflags);
931 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
932 && TREE_TYPE (value) != error_mark_node
933 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
934 set_underlying_type (value);
936 /* It's important that push_template_decl below follows
937 set_underlying_type above so that the created template
938 carries the properly set type of VALUE. */
939 if (processing_template_decl)
940 value = push_template_decl (value);
942 record_locally_defined_typedef (value);
943 return value;
946 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
948 if (!friendp && DECL_IN_AGGR_P (value))
950 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
951 return void_type_node;
954 if (asmspec_tree && asmspec_tree != error_mark_node)
955 asmspec = TREE_STRING_POINTER (asmspec_tree);
957 if (init)
959 if (TREE_CODE (value) == FUNCTION_DECL)
961 if (init == ridpointers[(int)RID_DELETE])
963 DECL_DELETED_FN (value) = 1;
964 DECL_DECLARED_INLINE_P (value) = 1;
965 DECL_INITIAL (value) = error_mark_node;
967 else if (init == ridpointers[(int)RID_DEFAULT])
969 if (defaultable_fn_check (value))
971 DECL_DEFAULTED_FN (value) = 1;
972 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
973 DECL_DECLARED_INLINE_P (value) = 1;
976 else if (TREE_CODE (init) == DEFAULT_ARG)
977 error ("invalid initializer for member function %qD", value);
978 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
980 if (integer_zerop (init))
981 DECL_PURE_VIRTUAL_P (value) = 1;
982 else if (error_operand_p (init))
983 ; /* An error has already been reported. */
984 else
985 error ("invalid initializer for member function %qD",
986 value);
988 else
990 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
991 if (friendp)
992 error ("initializer specified for friend function %qD",
993 value);
994 else
995 error ("initializer specified for static member function %qD",
996 value);
999 else if (TREE_CODE (value) == FIELD_DECL)
1000 /* C++11 NSDMI, keep going. */;
1001 else if (!VAR_P (value))
1002 gcc_unreachable ();
1005 /* Pass friend decls back. */
1006 if ((TREE_CODE (value) == FUNCTION_DECL
1007 || TREE_CODE (value) == TEMPLATE_DECL)
1008 && DECL_CONTEXT (value) != current_class_type)
1009 return value;
1011 /* Need to set this before push_template_decl. */
1012 if (VAR_P (value))
1013 DECL_CONTEXT (value) = current_class_type;
1015 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1017 value = push_template_decl (value);
1018 if (error_operand_p (value))
1019 return error_mark_node;
1022 if (attrlist)
1023 cplus_decl_attributes (&value, attrlist, 0);
1025 if (init && DIRECT_LIST_INIT_P (init))
1026 flags = LOOKUP_NORMAL;
1027 else
1028 flags = LOOKUP_IMPLICIT;
1030 switch (TREE_CODE (value))
1032 case VAR_DECL:
1033 finish_static_data_member_decl (value, init, init_const_expr_p,
1034 asmspec_tree, flags);
1035 return value;
1037 case FIELD_DECL:
1038 if (asmspec)
1039 error ("%<asm%> specifiers are not permitted on non-static data members");
1040 if (DECL_INITIAL (value) == error_mark_node)
1041 init = error_mark_node;
1042 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1043 NULL_TREE, flags);
1044 DECL_IN_AGGR_P (value) = 1;
1045 return value;
1047 case FUNCTION_DECL:
1048 if (asmspec)
1049 set_user_assembler_name (value, asmspec);
1051 cp_finish_decl (value,
1052 /*init=*/NULL_TREE,
1053 /*init_const_expr_p=*/false,
1054 asmspec_tree, flags);
1056 /* Pass friends back this way. */
1057 if (DECL_FRIEND_P (value))
1058 return void_type_node;
1060 DECL_IN_AGGR_P (value) = 1;
1061 return value;
1063 default:
1064 gcc_unreachable ();
1066 return NULL_TREE;
1069 /* Like `grokfield', but for bitfields.
1070 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1072 tree
1073 grokbitfield (const cp_declarator *declarator,
1074 cp_decl_specifier_seq *declspecs, tree width,
1075 tree attrlist)
1077 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1079 if (value == error_mark_node)
1080 return NULL_TREE; /* friends went bad. */
1081 if (TREE_TYPE (value) == error_mark_node)
1082 return value;
1084 /* Pass friendly classes back. */
1085 if (VOID_TYPE_P (value))
1086 return void_type_node;
1088 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1089 && (POINTER_TYPE_P (value)
1090 || !dependent_type_p (TREE_TYPE (value))))
1092 error ("bit-field %qD with non-integral type", value);
1093 return error_mark_node;
1096 if (TREE_CODE (value) == TYPE_DECL)
1098 error ("cannot declare %qD to be a bit-field type", value);
1099 return NULL_TREE;
1102 /* Usually, finish_struct_1 catches bitfields with invalid types.
1103 But, in the case of bitfields with function type, we confuse
1104 ourselves into thinking they are member functions, so we must
1105 check here. */
1106 if (TREE_CODE (value) == FUNCTION_DECL)
1108 error ("cannot declare bit-field %qD with function type",
1109 DECL_NAME (value));
1110 return NULL_TREE;
1113 if (DECL_IN_AGGR_P (value))
1115 error ("%qD is already defined in the class %qT", value,
1116 DECL_CONTEXT (value));
1117 return void_type_node;
1120 if (TREE_STATIC (value))
1122 error ("static member %qD cannot be a bit-field", value);
1123 return NULL_TREE;
1125 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1127 if (width != error_mark_node)
1129 /* The width must be an integer type. */
1130 if (!type_dependent_expression_p (width)
1131 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1132 error ("width of bit-field %qD has non-integral type %qT", value,
1133 TREE_TYPE (width));
1134 DECL_INITIAL (value) = width;
1135 SET_DECL_C_BIT_FIELD (value);
1138 DECL_IN_AGGR_P (value) = 1;
1140 if (attrlist)
1141 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1143 return value;
1147 /* Returns true iff ATTR is an attribute which needs to be applied at
1148 instantiation time rather than template definition time. */
1150 static bool
1151 is_late_template_attribute (tree attr, tree decl)
1153 tree name = get_attribute_name (attr);
1154 tree args = TREE_VALUE (attr);
1155 const struct attribute_spec *spec = lookup_attribute_spec (name);
1156 tree arg;
1158 if (!spec)
1159 /* Unknown attribute. */
1160 return false;
1162 /* Attribute weak handling wants to write out assembly right away. */
1163 if (is_attribute_p ("weak", name))
1164 return true;
1166 /* Attribute unused is applied directly, as it appertains to
1167 decls. */
1168 if (is_attribute_p ("unused", name))
1169 return false;
1171 /* Attribute tls_model wants to modify the symtab. */
1172 if (is_attribute_p ("tls_model", name))
1173 return true;
1175 /* #pragma omp declare simd attribute needs to be always deferred. */
1176 if (flag_openmp
1177 && is_attribute_p ("omp declare simd", name))
1178 return true;
1180 /* An attribute pack is clearly dependent. */
1181 if (args && PACK_EXPANSION_P (args))
1182 return true;
1184 /* If any of the arguments are dependent expressions, we can't evaluate
1185 the attribute until instantiation time. */
1186 for (arg = args; arg; arg = TREE_CHAIN (arg))
1188 tree t = TREE_VALUE (arg);
1190 /* If the first attribute argument is an identifier, only consider
1191 second and following arguments. Attributes like mode, format,
1192 cleanup and several target specific attributes aren't late
1193 just because they have an IDENTIFIER_NODE as first argument. */
1194 if (arg == args && identifier_p (t))
1195 continue;
1197 if (value_dependent_expression_p (t)
1198 || type_dependent_expression_p (t))
1199 return true;
1202 if (TREE_CODE (decl) == TYPE_DECL
1203 || TYPE_P (decl)
1204 || spec->type_required)
1206 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1208 /* We can't apply any attributes to a completely unknown type until
1209 instantiation time. */
1210 enum tree_code code = TREE_CODE (type);
1211 if (code == TEMPLATE_TYPE_PARM
1212 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1213 || code == TYPENAME_TYPE)
1214 return true;
1215 /* Also defer most attributes on dependent types. This is not
1216 necessary in all cases, but is the better default. */
1217 else if (dependent_type_p (type)
1218 /* But some attributes specifically apply to templates. */
1219 && !is_attribute_p ("abi_tag", name)
1220 && !is_attribute_p ("deprecated", name)
1221 && !is_attribute_p ("visibility", name))
1222 return true;
1223 else
1224 return false;
1226 else
1227 return false;
1230 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1231 applied at instantiation time and return them. If IS_DEPENDENT is true,
1232 the declaration itself is dependent, so all attributes should be applied
1233 at instantiation time. */
1235 static tree
1236 splice_template_attributes (tree *attr_p, tree decl)
1238 tree *p = attr_p;
1239 tree late_attrs = NULL_TREE;
1240 tree *q = &late_attrs;
1242 if (!p)
1243 return NULL_TREE;
1245 for (; *p; )
1247 if (is_late_template_attribute (*p, decl))
1249 ATTR_IS_DEPENDENT (*p) = 1;
1250 *q = *p;
1251 *p = TREE_CHAIN (*p);
1252 q = &TREE_CHAIN (*q);
1253 *q = NULL_TREE;
1255 else
1256 p = &TREE_CHAIN (*p);
1259 return late_attrs;
1262 /* Remove any late attributes from the list in ATTR_P and attach them to
1263 DECL_P. */
1265 static void
1266 save_template_attributes (tree *attr_p, tree *decl_p)
1268 tree *q;
1270 if (attr_p && *attr_p == error_mark_node)
1271 return;
1273 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1274 if (!late_attrs)
1275 return;
1277 if (DECL_P (*decl_p))
1278 q = &DECL_ATTRIBUTES (*decl_p);
1279 else
1280 q = &TYPE_ATTRIBUTES (*decl_p);
1282 tree old_attrs = *q;
1284 /* Merge the late attributes at the beginning with the attribute
1285 list. */
1286 late_attrs = merge_attributes (late_attrs, *q);
1287 *q = late_attrs;
1289 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1291 /* We've added new attributes directly to the main variant, so
1292 now we need to update all of the other variants to include
1293 these new attributes. */
1294 tree variant;
1295 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1296 variant = TYPE_NEXT_VARIANT (variant))
1298 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1299 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1304 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1305 to a typedef which gives a previously anonymous class or enum a name for
1306 linkage purposes. */
1308 bool
1309 attributes_naming_typedef_ok (tree attrs)
1311 for (; attrs; attrs = TREE_CHAIN (attrs))
1313 tree name = get_attribute_name (attrs);
1314 if (is_attribute_p ("vector_size", name))
1315 return false;
1317 return true;
1320 /* Like reconstruct_complex_type, but handle also template trees. */
1322 tree
1323 cp_reconstruct_complex_type (tree type, tree bottom)
1325 tree inner, outer;
1326 bool late_return_type_p = false;
1328 if (TYPE_PTR_P (type))
1330 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1331 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1332 TYPE_REF_CAN_ALIAS_ALL (type));
1334 else if (TREE_CODE (type) == REFERENCE_TYPE)
1336 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1337 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1338 TYPE_REF_CAN_ALIAS_ALL (type));
1340 else if (TREE_CODE (type) == ARRAY_TYPE)
1342 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1343 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1344 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1345 element type qualification will be handled by the recursive
1346 cp_reconstruct_complex_type call and cp_build_qualified_type
1347 for ARRAY_TYPEs changes the element type. */
1348 return outer;
1350 else if (TREE_CODE (type) == FUNCTION_TYPE)
1352 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1353 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1354 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1355 outer = apply_memfn_quals (outer,
1356 type_memfn_quals (type),
1357 type_memfn_rqual (type));
1359 else if (TREE_CODE (type) == METHOD_TYPE)
1361 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1362 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1363 /* The build_method_type_directly() routine prepends 'this' to argument list,
1364 so we must compensate by getting rid of it. */
1365 outer
1366 = build_method_type_directly
1367 (class_of_this_parm (type), inner,
1368 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1370 else if (TREE_CODE (type) == OFFSET_TYPE)
1372 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1373 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1375 else
1376 return bottom;
1378 if (TYPE_ATTRIBUTES (type))
1379 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1380 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1382 if (late_return_type_p)
1383 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1385 return outer;
1388 /* Replaces any constexpr expression that may be into the attributes
1389 arguments with their reduced value. */
1391 static void
1392 cp_check_const_attributes (tree attributes)
1394 if (attributes == error_mark_node)
1395 return;
1397 tree attr;
1398 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1400 tree arg;
1401 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1403 tree expr = TREE_VALUE (arg);
1404 if (EXPR_P (expr))
1405 TREE_VALUE (arg) = maybe_constant_value (expr);
1410 /* Return true if TYPE is an OpenMP mappable type. */
1411 bool
1412 cp_omp_mappable_type (tree type)
1414 /* Mappable type has to be complete. */
1415 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1416 return false;
1417 /* Arrays have mappable type if the elements have mappable type. */
1418 while (TREE_CODE (type) == ARRAY_TYPE)
1419 type = TREE_TYPE (type);
1420 /* A mappable type cannot contain virtual members. */
1421 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1422 return false;
1423 /* All data members must be non-static. */
1424 if (CLASS_TYPE_P (type))
1426 tree field;
1427 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1428 if (VAR_P (field))
1429 return false;
1430 /* All fields must have mappable types. */
1431 else if (TREE_CODE (field) == FIELD_DECL
1432 && !cp_omp_mappable_type (TREE_TYPE (field)))
1433 return false;
1435 return true;
1438 /* Like decl_attributes, but handle C++ complexity. */
1440 void
1441 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1443 if (*decl == NULL_TREE || *decl == void_type_node
1444 || *decl == error_mark_node)
1445 return;
1447 /* Add implicit "omp declare target" attribute if requested. */
1448 if (scope_chain->omp_declare_target_attribute
1449 && ((VAR_P (*decl)
1450 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1451 || TREE_CODE (*decl) == FUNCTION_DECL))
1453 if (VAR_P (*decl)
1454 && DECL_CLASS_SCOPE_P (*decl))
1455 error ("%q+D static data member inside of declare target directive",
1456 *decl);
1457 else if (VAR_P (*decl)
1458 && (DECL_FUNCTION_SCOPE_P (*decl)
1459 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1460 error ("%q+D in block scope inside of declare target directive",
1461 *decl);
1462 else if (!processing_template_decl
1463 && VAR_P (*decl)
1464 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1465 error ("%q+D in declare target directive does not have mappable type",
1466 *decl);
1467 else
1468 attributes = tree_cons (get_identifier ("omp declare target"),
1469 NULL_TREE, attributes);
1472 if (processing_template_decl)
1474 if (check_for_bare_parameter_packs (attributes))
1475 return;
1477 save_template_attributes (&attributes, decl);
1480 cp_check_const_attributes (attributes);
1482 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1483 decl = &DECL_TEMPLATE_RESULT (*decl);
1485 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1487 attributes
1488 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1489 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1490 attributes, flags);
1492 else
1493 decl_attributes (decl, attributes, flags);
1495 if (TREE_CODE (*decl) == TYPE_DECL)
1496 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1498 /* Propagate deprecation out to the template. */
1499 if (TREE_DEPRECATED (*decl))
1500 if (tree ti = get_template_info (*decl))
1502 tree tmpl = TI_TEMPLATE (ti);
1503 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1504 : DECL_TEMPLATE_RESULT (tmpl));
1505 if (*decl == pattern)
1506 TREE_DEPRECATED (tmpl) = true;
1510 /* Walks through the namespace- or function-scope anonymous union
1511 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1512 Returns one of the fields for use in the mangled name. */
1514 static tree
1515 build_anon_union_vars (tree type, tree object)
1517 tree main_decl = NULL_TREE;
1518 tree field;
1520 /* Rather than write the code to handle the non-union case,
1521 just give an error. */
1522 if (TREE_CODE (type) != UNION_TYPE)
1524 error ("anonymous struct not inside named type");
1525 return error_mark_node;
1528 for (field = TYPE_FIELDS (type);
1529 field != NULL_TREE;
1530 field = DECL_CHAIN (field))
1532 tree decl;
1533 tree ref;
1535 if (DECL_ARTIFICIAL (field))
1536 continue;
1537 if (TREE_CODE (field) != FIELD_DECL)
1539 permerror (DECL_SOURCE_LOCATION (field),
1540 "%q#D invalid; an anonymous union can only "
1541 "have non-static data members", field);
1542 continue;
1545 if (TREE_PRIVATE (field))
1546 permerror (DECL_SOURCE_LOCATION (field),
1547 "private member %q#D in anonymous union", field);
1548 else if (TREE_PROTECTED (field))
1549 permerror (DECL_SOURCE_LOCATION (field),
1550 "protected member %q#D in anonymous union", field);
1552 if (processing_template_decl)
1553 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1554 DECL_NAME (field), NULL_TREE);
1555 else
1556 ref = build_class_member_access_expr (object, field, NULL_TREE,
1557 false, tf_warning_or_error);
1559 if (DECL_NAME (field))
1561 tree base;
1563 decl = build_decl (input_location,
1564 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1565 DECL_ANON_UNION_VAR_P (decl) = 1;
1566 DECL_ARTIFICIAL (decl) = 1;
1568 base = get_base_address (object);
1569 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1570 TREE_STATIC (decl) = TREE_STATIC (base);
1571 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1573 SET_DECL_VALUE_EXPR (decl, ref);
1574 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1576 decl = pushdecl (decl);
1578 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1579 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1580 else
1581 decl = 0;
1583 if (main_decl == NULL_TREE)
1584 main_decl = decl;
1587 return main_decl;
1590 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1591 anonymous union, then all members must be laid out together. PUBLIC_P
1592 is nonzero if this union is not declared static. */
1594 void
1595 finish_anon_union (tree anon_union_decl)
1597 tree type;
1598 tree main_decl;
1599 bool public_p;
1601 if (anon_union_decl == error_mark_node)
1602 return;
1604 type = TREE_TYPE (anon_union_decl);
1605 public_p = TREE_PUBLIC (anon_union_decl);
1607 /* The VAR_DECL's context is the same as the TYPE's context. */
1608 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1610 if (TYPE_FIELDS (type) == NULL_TREE)
1611 return;
1613 if (public_p)
1615 error ("namespace-scope anonymous aggregates must be static");
1616 return;
1619 main_decl = build_anon_union_vars (type, anon_union_decl);
1620 if (main_decl == error_mark_node)
1621 return;
1622 if (main_decl == NULL_TREE)
1624 warning (0, "anonymous union with no members");
1625 return;
1628 if (!processing_template_decl)
1630 /* Use main_decl to set the mangled name. */
1631 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1632 maybe_commonize_var (anon_union_decl);
1633 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1634 mangle_decl (anon_union_decl);
1635 DECL_NAME (anon_union_decl) = NULL_TREE;
1638 pushdecl (anon_union_decl);
1639 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1642 /* Auxiliary functions to make type signatures for
1643 `operator new' and `operator delete' correspond to
1644 what compiler will be expecting. */
1646 tree
1647 coerce_new_type (tree type)
1649 int e = 0;
1650 tree args = TYPE_ARG_TYPES (type);
1652 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1654 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1656 e = 1;
1657 error ("%<operator new%> must return type %qT", ptr_type_node);
1660 if (args && args != void_list_node)
1662 if (TREE_PURPOSE (args))
1664 /* [basic.stc.dynamic.allocation]
1666 The first parameter shall not have an associated default
1667 argument. */
1668 error ("the first parameter of %<operator new%> cannot "
1669 "have a default argument");
1670 /* Throw away the default argument. */
1671 TREE_PURPOSE (args) = NULL_TREE;
1674 if (!same_type_p (TREE_VALUE (args), size_type_node))
1676 e = 2;
1677 args = TREE_CHAIN (args);
1680 else
1681 e = 2;
1683 if (e == 2)
1684 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1685 "as first parameter", size_type_node);
1687 switch (e)
1689 case 2:
1690 args = tree_cons (NULL_TREE, size_type_node, args);
1691 /* Fall through. */
1692 case 1:
1693 type = build_exception_variant
1694 (build_function_type (ptr_type_node, args),
1695 TYPE_RAISES_EXCEPTIONS (type));
1696 /* Fall through. */
1697 default:;
1699 return type;
1702 tree
1703 coerce_delete_type (tree type)
1705 int e = 0;
1706 tree args = TYPE_ARG_TYPES (type);
1708 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1710 if (!same_type_p (TREE_TYPE (type), void_type_node))
1712 e = 1;
1713 error ("%<operator delete%> must return type %qT", void_type_node);
1716 if (!args || args == void_list_node
1717 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1719 e = 2;
1720 if (args && args != void_list_node)
1721 args = TREE_CHAIN (args);
1722 error ("%<operator delete%> takes type %qT as first parameter",
1723 ptr_type_node);
1725 switch (e)
1727 case 2:
1728 args = tree_cons (NULL_TREE, ptr_type_node, args);
1729 /* Fall through. */
1730 case 1:
1731 type = build_exception_variant
1732 (build_function_type (void_type_node, args),
1733 TYPE_RAISES_EXCEPTIONS (type));
1734 /* Fall through. */
1735 default:;
1738 return type;
1741 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1742 and mark them as needed. */
1744 static void
1745 mark_vtable_entries (tree decl)
1747 tree fnaddr;
1748 unsigned HOST_WIDE_INT idx;
1750 /* It's OK for the vtable to refer to deprecated virtual functions. */
1751 warning_sentinel w(warn_deprecated_decl);
1753 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1754 idx, fnaddr)
1756 tree fn;
1758 STRIP_NOPS (fnaddr);
1760 if (TREE_CODE (fnaddr) != ADDR_EXPR
1761 && TREE_CODE (fnaddr) != FDESC_EXPR)
1762 /* This entry is an offset: a virtual base class offset, a
1763 virtual call offset, an RTTI offset, etc. */
1764 continue;
1766 fn = TREE_OPERAND (fnaddr, 0);
1767 TREE_ADDRESSABLE (fn) = 1;
1768 /* When we don't have vcall offsets, we output thunks whenever
1769 we output the vtables that contain them. With vcall offsets,
1770 we know all the thunks we'll need when we emit a virtual
1771 function, so we emit the thunks there instead. */
1772 if (DECL_THUNK_P (fn))
1773 use_thunk (fn, /*emit_p=*/0);
1774 mark_used (fn);
1778 /* Set DECL up to have the closest approximation of "initialized common"
1779 linkage available. */
1781 void
1782 comdat_linkage (tree decl)
1784 if (flag_weak)
1785 make_decl_one_only (decl, cxx_comdat_group (decl));
1786 else if (TREE_CODE (decl) == FUNCTION_DECL
1787 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1788 /* We can just emit function and compiler-generated variables
1789 statically; having multiple copies is (for the most part) only
1790 a waste of space.
1792 There are two correctness issues, however: the address of a
1793 template instantiation with external linkage should be the
1794 same, independent of what translation unit asks for the
1795 address, and this will not hold when we emit multiple copies of
1796 the function. However, there's little else we can do.
1798 Also, by default, the typeinfo implementation assumes that
1799 there will be only one copy of the string used as the name for
1800 each type. Therefore, if weak symbols are unavailable, the
1801 run-time library should perform a more conservative check; it
1802 should perform a string comparison, rather than an address
1803 comparison. */
1804 TREE_PUBLIC (decl) = 0;
1805 else
1807 /* Static data member template instantiations, however, cannot
1808 have multiple copies. */
1809 if (DECL_INITIAL (decl) == 0
1810 || DECL_INITIAL (decl) == error_mark_node)
1811 DECL_COMMON (decl) = 1;
1812 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1814 DECL_COMMON (decl) = 1;
1815 DECL_INITIAL (decl) = error_mark_node;
1817 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1819 /* We can't do anything useful; leave vars for explicit
1820 instantiation. */
1821 DECL_EXTERNAL (decl) = 1;
1822 DECL_NOT_REALLY_EXTERN (decl) = 0;
1826 DECL_COMDAT (decl) = 1;
1829 /* For win32 we also want to put explicit instantiations in
1830 linkonce sections, so that they will be merged with implicit
1831 instantiations; otherwise we get duplicate symbol errors.
1832 For Darwin we do not want explicit instantiations to be
1833 linkonce. */
1835 void
1836 maybe_make_one_only (tree decl)
1838 /* We used to say that this was not necessary on targets that support weak
1839 symbols, because the implicit instantiations will defer to the explicit
1840 one. However, that's not actually the case in SVR4; a strong definition
1841 after a weak one is an error. Also, not making explicit
1842 instantiations one_only means that we can end up with two copies of
1843 some template instantiations. */
1844 if (! flag_weak)
1845 return;
1847 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1848 we can get away with not emitting them if they aren't used. We need
1849 to for variables so that cp_finish_decl will update their linkage,
1850 because their DECL_INITIAL may not have been set properly yet. */
1852 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1853 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1854 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1856 make_decl_one_only (decl, cxx_comdat_group (decl));
1858 if (VAR_P (decl))
1860 varpool_node *node = varpool_node::get_create (decl);
1861 DECL_COMDAT (decl) = 1;
1862 /* Mark it needed so we don't forget to emit it. */
1863 node->forced_by_abi = true;
1864 TREE_USED (decl) = 1;
1869 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1870 This predicate will give the right answer during parsing of the
1871 function, which other tests may not. */
1873 bool
1874 vague_linkage_p (tree decl)
1876 if (!TREE_PUBLIC (decl))
1878 gcc_checking_assert (!DECL_COMDAT (decl));
1879 return false;
1881 /* Unfortunately, import_export_decl has not always been called
1882 before the function is processed, so we cannot simply check
1883 DECL_COMDAT. */
1884 if (DECL_COMDAT (decl)
1885 || (TREE_CODE (decl) == FUNCTION_DECL
1886 && DECL_DECLARED_INLINE_P (decl))
1887 || (DECL_LANG_SPECIFIC (decl)
1888 && DECL_TEMPLATE_INSTANTIATION (decl)))
1889 return true;
1890 else if (DECL_FUNCTION_SCOPE_P (decl))
1891 /* A local static in an inline effectively has vague linkage. */
1892 return (TREE_STATIC (decl)
1893 && vague_linkage_p (DECL_CONTEXT (decl)));
1894 else
1895 return false;
1898 /* Determine whether or not we want to specifically import or export CTYPE,
1899 using various heuristics. */
1901 static void
1902 import_export_class (tree ctype)
1904 /* -1 for imported, 1 for exported. */
1905 int import_export = 0;
1907 /* It only makes sense to call this function at EOF. The reason is
1908 that this function looks at whether or not the first non-inline
1909 non-abstract virtual member function has been defined in this
1910 translation unit. But, we can't possibly know that until we've
1911 seen the entire translation unit. */
1912 gcc_assert (at_eof);
1914 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1915 return;
1917 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1918 we will have CLASSTYPE_INTERFACE_ONLY set but not
1919 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1920 heuristic because someone will supply a #pragma implementation
1921 elsewhere, and deducing it here would produce a conflict. */
1922 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1923 return;
1925 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1926 import_export = -1;
1927 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1928 import_export = 1;
1929 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1930 && !flag_implicit_templates)
1931 /* For a template class, without -fimplicit-templates, check the
1932 repository. If the virtual table is assigned to this
1933 translation unit, then export the class; otherwise, import
1934 it. */
1935 import_export = repo_export_class_p (ctype) ? 1 : -1;
1936 else if (TYPE_POLYMORPHIC_P (ctype))
1938 /* The ABI specifies that the virtual table and associated
1939 information are emitted with the key method, if any. */
1940 tree method = CLASSTYPE_KEY_METHOD (ctype);
1941 /* If weak symbol support is not available, then we must be
1942 careful not to emit the vtable when the key function is
1943 inline. An inline function can be defined in multiple
1944 translation units. If we were to emit the vtable in each
1945 translation unit containing a definition, we would get
1946 multiple definition errors at link-time. */
1947 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1948 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1951 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1952 a definition anywhere else. */
1953 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1954 import_export = 0;
1956 /* Allow back ends the chance to overrule the decision. */
1957 if (targetm.cxx.import_export_class)
1958 import_export = targetm.cxx.import_export_class (ctype, import_export);
1960 if (import_export)
1962 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1963 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1967 /* Return true if VAR has already been provided to the back end; in that
1968 case VAR should not be modified further by the front end. */
1969 static bool
1970 var_finalized_p (tree var)
1972 return varpool_node::get_create (var)->definition;
1975 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1976 must be emitted in this translation unit. Mark it as such. */
1978 void
1979 mark_needed (tree decl)
1981 TREE_USED (decl) = 1;
1982 if (TREE_CODE (decl) == FUNCTION_DECL)
1984 /* Extern inline functions don't become needed when referenced.
1985 If we know a method will be emitted in other TU and no new
1986 functions can be marked reachable, just use the external
1987 definition. */
1988 struct cgraph_node *node = cgraph_node::get_create (decl);
1989 node->forced_by_abi = true;
1991 /* #pragma interface and -frepo code can call mark_needed for
1992 maybe-in-charge 'tors; mark the clones as well. */
1993 tree clone;
1994 FOR_EACH_CLONE (clone, decl)
1995 mark_needed (clone);
1997 else if (VAR_P (decl))
1999 varpool_node *node = varpool_node::get_create (decl);
2000 /* C++ frontend use mark_decl_references to force COMDAT variables
2001 to be output that might appear dead otherwise. */
2002 node->forced_by_abi = true;
2006 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2007 returns true if a definition of this entity should be provided in
2008 this object file. Callers use this function to determine whether
2009 or not to let the back end know that a definition of DECL is
2010 available in this translation unit. */
2012 bool
2013 decl_needed_p (tree decl)
2015 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2016 /* This function should only be called at the end of the translation
2017 unit. We cannot be sure of whether or not something will be
2018 COMDAT until that point. */
2019 gcc_assert (at_eof);
2021 /* All entities with external linkage that are not COMDAT/EXTERN should be
2022 emitted; they may be referred to from other object files. */
2023 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2024 return true;
2025 /* Functions marked "dllexport" must be emitted so that they are
2026 visible to other DLLs. */
2027 if (flag_keep_inline_dllexport
2028 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2029 return true;
2031 /* When not optimizing, do not bother to produce definitions for extern
2032 symbols. */
2033 if (DECL_REALLY_EXTERN (decl)
2034 && ((TREE_CODE (decl) != FUNCTION_DECL
2035 && !optimize)
2036 || (TREE_CODE (decl) == FUNCTION_DECL
2037 && !opt_for_fn (decl, optimize)))
2038 && !lookup_attribute ("always_inline", decl))
2039 return false;
2041 /* If this entity was used, let the back end see it; it will decide
2042 whether or not to emit it into the object file. */
2043 if (TREE_USED (decl))
2044 return true;
2045 /* Virtual functions might be needed for devirtualization. */
2046 if (flag_devirtualize
2047 && TREE_CODE (decl) == FUNCTION_DECL
2048 && DECL_VIRTUAL_P (decl))
2049 return true;
2050 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2051 reference to DECL might cause it to be emitted later. */
2052 return false;
2055 /* If necessary, write out the vtables for the dynamic class CTYPE.
2056 Returns true if any vtables were emitted. */
2058 static bool
2059 maybe_emit_vtables (tree ctype)
2061 tree vtbl;
2062 tree primary_vtbl;
2063 int needed = 0;
2064 varpool_node *current = NULL, *last = NULL;
2066 /* If the vtables for this class have already been emitted there is
2067 nothing more to do. */
2068 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2069 if (var_finalized_p (primary_vtbl))
2070 return false;
2071 /* Ignore dummy vtables made by get_vtable_decl. */
2072 if (TREE_TYPE (primary_vtbl) == void_type_node)
2073 return false;
2075 /* On some targets, we cannot determine the key method until the end
2076 of the translation unit -- which is when this function is
2077 called. */
2078 if (!targetm.cxx.key_method_may_be_inline ())
2079 determine_key_method (ctype);
2081 /* See if any of the vtables are needed. */
2082 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2084 import_export_decl (vtbl);
2085 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2086 needed = 1;
2088 if (!needed)
2090 /* If the references to this class' vtables are optimized away,
2091 still emit the appropriate debugging information. See
2092 dfs_debug_mark. */
2093 if (DECL_COMDAT (primary_vtbl)
2094 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2095 note_debug_info_needed (ctype);
2096 return false;
2099 /* The ABI requires that we emit all of the vtables if we emit any
2100 of them. */
2101 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2103 /* Mark entities references from the virtual table as used. */
2104 mark_vtable_entries (vtbl);
2106 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2108 vec<tree, va_gc> *cleanups = NULL;
2109 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2110 LOOKUP_NORMAL);
2112 /* It had better be all done at compile-time. */
2113 gcc_assert (!expr && !cleanups);
2116 /* Write it out. */
2117 DECL_EXTERNAL (vtbl) = 0;
2118 rest_of_decl_compilation (vtbl, 1, 1);
2120 /* Because we're only doing syntax-checking, we'll never end up
2121 actually marking the variable as written. */
2122 if (flag_syntax_only)
2123 TREE_ASM_WRITTEN (vtbl) = 1;
2124 else if (DECL_ONE_ONLY (vtbl))
2126 current = varpool_node::get_create (vtbl);
2127 if (last)
2128 current->add_to_same_comdat_group (last);
2129 last = current;
2133 /* Since we're writing out the vtable here, also write the debug
2134 info. */
2135 note_debug_info_needed (ctype);
2137 return true;
2140 /* A special return value from type_visibility meaning internal
2141 linkage. */
2143 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2145 /* walk_tree helper function for type_visibility. */
2147 static tree
2148 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2150 int *vis_p = (int *)data;
2151 if (! TYPE_P (*tp))
2153 *walk_subtrees = 0;
2155 else if (OVERLOAD_TYPE_P (*tp)
2156 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2158 *vis_p = VISIBILITY_ANON;
2159 return *tp;
2161 else if (CLASS_TYPE_P (*tp)
2162 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2163 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2164 return NULL;
2167 /* Returns the visibility of TYPE, which is the minimum visibility of its
2168 component types. */
2170 static int
2171 type_visibility (tree type)
2173 int vis = VISIBILITY_DEFAULT;
2174 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2175 return vis;
2178 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2179 specified (or if VISIBILITY is static). If TMPL is true, this
2180 constraint is for a template argument, and takes precedence
2181 over explicitly-specified visibility on the template. */
2183 static void
2184 constrain_visibility (tree decl, int visibility, bool tmpl)
2186 if (visibility == VISIBILITY_ANON)
2188 /* extern "C" declarations aren't affected by the anonymous
2189 namespace. */
2190 if (!DECL_EXTERN_C_P (decl))
2192 TREE_PUBLIC (decl) = 0;
2193 DECL_WEAK (decl) = 0;
2194 DECL_COMMON (decl) = 0;
2195 DECL_COMDAT (decl) = false;
2196 if (VAR_OR_FUNCTION_DECL_P (decl))
2198 struct symtab_node *snode = symtab_node::get (decl);
2200 if (snode)
2201 snode->set_comdat_group (NULL);
2203 DECL_INTERFACE_KNOWN (decl) = 1;
2204 if (DECL_LANG_SPECIFIC (decl))
2205 DECL_NOT_REALLY_EXTERN (decl) = 1;
2208 else if (visibility > DECL_VISIBILITY (decl)
2209 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2211 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2212 /* This visibility was not specified. */
2213 DECL_VISIBILITY_SPECIFIED (decl) = false;
2217 /* Constrain the visibility of DECL based on the visibility of its template
2218 arguments. */
2220 static void
2221 constrain_visibility_for_template (tree decl, tree targs)
2223 /* If this is a template instantiation, check the innermost
2224 template args for visibility constraints. The outer template
2225 args are covered by the class check. */
2226 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2227 int i;
2228 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2230 int vis = 0;
2232 tree arg = TREE_VEC_ELT (args, i-1);
2233 if (TYPE_P (arg))
2234 vis = type_visibility (arg);
2235 else
2237 if (REFERENCE_REF_P (arg))
2238 arg = TREE_OPERAND (arg, 0);
2239 if (TREE_TYPE (arg))
2240 STRIP_NOPS (arg);
2241 if (TREE_CODE (arg) == ADDR_EXPR)
2242 arg = TREE_OPERAND (arg, 0);
2243 if (VAR_OR_FUNCTION_DECL_P (arg))
2245 if (! TREE_PUBLIC (arg))
2246 vis = VISIBILITY_ANON;
2247 else
2248 vis = DECL_VISIBILITY (arg);
2251 if (vis)
2252 constrain_visibility (decl, vis, true);
2256 /* Like c_determine_visibility, but with additional C++-specific
2257 behavior.
2259 Function-scope entities can rely on the function's visibility because
2260 it is set in start_preparsed_function.
2262 Class-scope entities cannot rely on the class's visibility until the end
2263 of the enclosing class definition.
2265 Note that because namespaces have multiple independent definitions,
2266 namespace visibility is handled elsewhere using the #pragma visibility
2267 machinery rather than by decorating the namespace declaration.
2269 The goal is for constraints from the type to give a diagnostic, and
2270 other constraints to be applied silently. */
2272 void
2273 determine_visibility (tree decl)
2275 tree class_type = NULL_TREE;
2276 bool use_template;
2277 bool orig_visibility_specified;
2278 enum symbol_visibility orig_visibility;
2280 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2282 /* Only relevant for names with external linkage. */
2283 if (!TREE_PUBLIC (decl))
2284 return;
2286 /* Cloned constructors and destructors get the same visibility as
2287 the underlying function. That should be set up in
2288 maybe_clone_body. */
2289 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2291 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2292 orig_visibility = DECL_VISIBILITY (decl);
2294 if (TREE_CODE (decl) == TYPE_DECL)
2296 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2297 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2298 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2299 use_template = 1;
2300 else
2301 use_template = 0;
2303 else if (DECL_LANG_SPECIFIC (decl))
2304 use_template = DECL_USE_TEMPLATE (decl);
2305 else
2306 use_template = 0;
2308 /* If DECL is a member of a class, visibility specifiers on the
2309 class can influence the visibility of the DECL. */
2310 if (DECL_CLASS_SCOPE_P (decl))
2311 class_type = DECL_CONTEXT (decl);
2312 else
2314 /* Not a class member. */
2316 /* Virtual tables have DECL_CONTEXT set to their associated class,
2317 so they are automatically handled above. */
2318 gcc_assert (!VAR_P (decl)
2319 || !DECL_VTABLE_OR_VTT_P (decl));
2321 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2323 /* Local statics and classes get the visibility of their
2324 containing function by default, except that
2325 -fvisibility-inlines-hidden doesn't affect them. */
2326 tree fn = DECL_CONTEXT (decl);
2327 if (DECL_VISIBILITY_SPECIFIED (fn))
2329 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2330 DECL_VISIBILITY_SPECIFIED (decl) =
2331 DECL_VISIBILITY_SPECIFIED (fn);
2333 else
2335 if (DECL_CLASS_SCOPE_P (fn))
2336 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2337 else if (determine_hidden_inline (fn))
2339 DECL_VISIBILITY (decl) = default_visibility;
2340 DECL_VISIBILITY_SPECIFIED (decl) =
2341 visibility_options.inpragma;
2343 else
2345 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2346 DECL_VISIBILITY_SPECIFIED (decl) =
2347 DECL_VISIBILITY_SPECIFIED (fn);
2351 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2352 but have no TEMPLATE_INFO, so don't try to check it. */
2353 use_template = 0;
2355 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2356 && flag_visibility_ms_compat)
2358 /* Under -fvisibility-ms-compat, types are visible by default,
2359 even though their contents aren't. */
2360 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2361 int underlying_vis = type_visibility (underlying_type);
2362 if (underlying_vis == VISIBILITY_ANON
2363 || (CLASS_TYPE_P (underlying_type)
2364 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2365 constrain_visibility (decl, underlying_vis, false);
2366 else
2367 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2369 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2371 /* tinfo visibility is based on the type it's for. */
2372 constrain_visibility
2373 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2375 /* Give the target a chance to override the visibility associated
2376 with DECL. */
2377 if (TREE_PUBLIC (decl)
2378 && !DECL_REALLY_EXTERN (decl)
2379 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2380 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2381 targetm.cxx.determine_class_data_visibility (decl);
2383 else if (use_template)
2384 /* Template instantiations and specializations get visibility based
2385 on their template unless they override it with an attribute. */;
2386 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2388 if (determine_hidden_inline (decl))
2389 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2390 else
2392 /* Set default visibility to whatever the user supplied with
2393 #pragma GCC visibility or a namespace visibility attribute. */
2394 DECL_VISIBILITY (decl) = default_visibility;
2395 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2400 if (use_template)
2402 /* If the specialization doesn't specify visibility, use the
2403 visibility from the template. */
2404 tree tinfo = get_template_info (decl);
2405 tree args = TI_ARGS (tinfo);
2406 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2407 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2408 : DECL_ATTRIBUTES (decl));
2410 if (args != error_mark_node)
2412 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2414 if (!DECL_VISIBILITY_SPECIFIED (decl))
2416 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2417 && determine_hidden_inline (decl))
2418 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2419 else
2421 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2422 DECL_VISIBILITY_SPECIFIED (decl)
2423 = DECL_VISIBILITY_SPECIFIED (pattern);
2427 if (args
2428 /* Template argument visibility outweighs #pragma or namespace
2429 visibility, but not an explicit attribute. */
2430 && !lookup_attribute ("visibility", attribs))
2432 int depth = TMPL_ARGS_DEPTH (args);
2433 if (DECL_VISIBILITY_SPECIFIED (decl))
2435 /* A class template member with explicit visibility
2436 overrides the class visibility, so we need to apply
2437 all the levels of template args directly. */
2438 int i;
2439 for (i = 1; i <= depth; ++i)
2441 tree lev = TMPL_ARGS_LEVEL (args, i);
2442 constrain_visibility_for_template (decl, lev);
2445 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2446 /* Limit visibility based on its template arguments. */
2447 constrain_visibility_for_template (decl, args);
2452 if (class_type)
2453 determine_visibility_from_class (decl, class_type);
2455 if (decl_anon_ns_mem_p (decl))
2456 /* Names in an anonymous namespace get internal linkage.
2457 This might change once we implement export. */
2458 constrain_visibility (decl, VISIBILITY_ANON, false);
2459 else if (TREE_CODE (decl) != TYPE_DECL)
2461 /* Propagate anonymity from type to decl. */
2462 int tvis = type_visibility (TREE_TYPE (decl));
2463 if (tvis == VISIBILITY_ANON
2464 || ! DECL_VISIBILITY_SPECIFIED (decl))
2465 constrain_visibility (decl, tvis, false);
2467 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2468 /* DR 757: A type without linkage shall not be used as the type of a
2469 variable or function with linkage, unless
2470 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2471 o the variable or function is not used (3.2 [basic.def.odr]) or is
2472 defined in the same translation unit.
2474 Since non-extern "C" decls need to be defined in the same
2475 translation unit, we can make the type internal. */
2476 constrain_visibility (decl, VISIBILITY_ANON, false);
2478 /* If visibility changed and DECL already has DECL_RTL, ensure
2479 symbol flags are updated. */
2480 if ((DECL_VISIBILITY (decl) != orig_visibility
2481 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2482 && ((VAR_P (decl) && TREE_STATIC (decl))
2483 || TREE_CODE (decl) == FUNCTION_DECL)
2484 && DECL_RTL_SET_P (decl))
2485 make_decl_rtl (decl);
2488 /* By default, static data members and function members receive
2489 the visibility of their containing class. */
2491 static void
2492 determine_visibility_from_class (tree decl, tree class_type)
2494 if (DECL_VISIBILITY_SPECIFIED (decl))
2495 return;
2497 if (determine_hidden_inline (decl))
2498 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2499 else
2501 /* Default to the class visibility. */
2502 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2503 DECL_VISIBILITY_SPECIFIED (decl)
2504 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2507 /* Give the target a chance to override the visibility associated
2508 with DECL. */
2509 if (VAR_P (decl)
2510 && (DECL_TINFO_P (decl)
2511 || (DECL_VTABLE_OR_VTT_P (decl)
2512 /* Construction virtual tables are not exported because
2513 they cannot be referred to from other object files;
2514 their name is not standardized by the ABI. */
2515 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2516 && TREE_PUBLIC (decl)
2517 && !DECL_REALLY_EXTERN (decl)
2518 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2519 targetm.cxx.determine_class_data_visibility (decl);
2522 /* Returns true iff DECL is an inline that should get hidden visibility
2523 because of -fvisibility-inlines-hidden. */
2525 static bool
2526 determine_hidden_inline (tree decl)
2528 return (visibility_options.inlines_hidden
2529 /* Don't do this for inline templates; specializations might not be
2530 inline, and we don't want them to inherit the hidden
2531 visibility. We'll set it here for all inline instantiations. */
2532 && !processing_template_decl
2533 && TREE_CODE (decl) == FUNCTION_DECL
2534 && DECL_DECLARED_INLINE_P (decl)
2535 && (! DECL_LANG_SPECIFIC (decl)
2536 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2539 /* Constrain the visibility of a class TYPE based on the visibility of its
2540 field types. Warn if any fields require lesser visibility. */
2542 void
2543 constrain_class_visibility (tree type)
2545 tree binfo;
2546 tree t;
2547 int i;
2549 int vis = type_visibility (type);
2551 if (vis == VISIBILITY_ANON
2552 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2553 return;
2555 /* Don't warn about visibility if the class has explicit visibility. */
2556 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2557 vis = VISIBILITY_INTERNAL;
2559 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2560 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2562 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2563 int subvis = type_visibility (ftype);
2565 if (subvis == VISIBILITY_ANON)
2567 if (!in_main_input_context())
2569 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2570 if (nlt)
2572 if (same_type_p (TREE_TYPE (t), nlt))
2573 warning (OPT_Wsubobject_linkage, "\
2574 %qT has a field %qD whose type has no linkage",
2575 type, t);
2576 else
2577 warning (OPT_Wsubobject_linkage, "\
2578 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2579 type, t, nlt);
2581 else
2582 warning (OPT_Wsubobject_linkage, "\
2583 %qT has a field %qD whose type uses the anonymous namespace",
2584 type, t);
2587 else if (MAYBE_CLASS_TYPE_P (ftype)
2588 && vis < VISIBILITY_HIDDEN
2589 && subvis >= VISIBILITY_HIDDEN)
2590 warning (OPT_Wattributes, "\
2591 %qT declared with greater visibility than the type of its field %qD",
2592 type, t);
2595 binfo = TYPE_BINFO (type);
2596 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2598 int subvis = type_visibility (TREE_TYPE (t));
2600 if (subvis == VISIBILITY_ANON)
2602 if (!in_main_input_context())
2604 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2605 if (nlt)
2607 if (same_type_p (TREE_TYPE (t), nlt))
2608 warning (OPT_Wsubobject_linkage, "\
2609 %qT has a base %qT whose type has no linkage",
2610 type, TREE_TYPE (t));
2611 else
2612 warning (OPT_Wsubobject_linkage, "\
2613 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2614 type, TREE_TYPE (t), nlt);
2616 else
2617 warning (OPT_Wsubobject_linkage, "\
2618 %qT has a base %qT whose type uses the anonymous namespace",
2619 type, TREE_TYPE (t));
2622 else if (vis < VISIBILITY_HIDDEN
2623 && subvis >= VISIBILITY_HIDDEN)
2624 warning (OPT_Wattributes, "\
2625 %qT declared with greater visibility than its base %qT",
2626 type, TREE_TYPE (t));
2630 /* Functions for adjusting the visibility of a tagged type and its nested
2631 types and declarations when it gets a name for linkage purposes from a
2632 typedef. */
2634 static void bt_reset_linkage_1 (binding_entry, void *);
2635 static void bt_reset_linkage_2 (binding_entry, void *);
2637 /* First reset the visibility of all the types. */
2639 static void
2640 reset_type_linkage_1 (tree type)
2642 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2643 if (CLASS_TYPE_P (type))
2644 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2645 bt_reset_linkage_1, NULL);
2647 static void
2648 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2650 reset_type_linkage_1 (b->type);
2653 /* Then reset the visibility of any static data members or member
2654 functions that use those types. */
2656 static void
2657 reset_decl_linkage (tree decl)
2659 if (TREE_PUBLIC (decl))
2660 return;
2661 if (DECL_CLONED_FUNCTION_P (decl))
2662 return;
2663 TREE_PUBLIC (decl) = true;
2664 DECL_INTERFACE_KNOWN (decl) = false;
2665 determine_visibility (decl);
2666 tentative_decl_linkage (decl);
2668 static void
2669 reset_type_linkage_2 (tree type)
2671 if (CLASS_TYPE_P (type))
2673 if (tree vt = CLASSTYPE_VTABLES (type))
2675 tree name = mangle_vtbl_for_type (type);
2676 DECL_NAME (vt) = name;
2677 SET_DECL_ASSEMBLER_NAME (vt, name);
2678 reset_decl_linkage (vt);
2680 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2682 tree name = mangle_typeinfo_for_type (type);
2683 DECL_NAME (ti) = name;
2684 SET_DECL_ASSEMBLER_NAME (ti, name);
2685 TREE_TYPE (name) = type;
2686 reset_decl_linkage (ti);
2688 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2689 if (VAR_P (m))
2690 reset_decl_linkage (m);
2691 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2693 reset_decl_linkage (m);
2694 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (m))
2695 /* Also update its name, for cxx_dwarf_name. */
2696 DECL_NAME (m) = TYPE_IDENTIFIER (type);
2698 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2699 bt_reset_linkage_2, NULL);
2702 static void
2703 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2705 reset_type_linkage_2 (b->type);
2707 void
2708 reset_type_linkage (tree type)
2710 reset_type_linkage_1 (type);
2711 reset_type_linkage_2 (type);
2714 /* Set up our initial idea of what the linkage of DECL should be. */
2716 void
2717 tentative_decl_linkage (tree decl)
2719 if (DECL_INTERFACE_KNOWN (decl))
2720 /* We've already made a decision as to how this function will
2721 be handled. */;
2722 else if (vague_linkage_p (decl))
2724 if (TREE_CODE (decl) == FUNCTION_DECL
2725 && decl_defined_p (decl))
2727 DECL_EXTERNAL (decl) = 1;
2728 DECL_NOT_REALLY_EXTERN (decl) = 1;
2729 note_vague_linkage_fn (decl);
2730 /* A non-template inline function with external linkage will
2731 always be COMDAT. As we must eventually determine the
2732 linkage of all functions, and as that causes writes to
2733 the data mapped in from the PCH file, it's advantageous
2734 to mark the functions at this point. */
2735 if (DECL_DECLARED_INLINE_P (decl)
2736 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2737 || DECL_DEFAULTED_FN (decl)))
2739 /* This function must have external linkage, as
2740 otherwise DECL_INTERFACE_KNOWN would have been
2741 set. */
2742 gcc_assert (TREE_PUBLIC (decl));
2743 comdat_linkage (decl);
2744 DECL_INTERFACE_KNOWN (decl) = 1;
2747 else if (VAR_P (decl))
2748 maybe_commonize_var (decl);
2752 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2753 for DECL has not already been determined, do so now by setting
2754 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2755 function is called entities with vague linkage whose definitions
2756 are available must have TREE_PUBLIC set.
2758 If this function decides to place DECL in COMDAT, it will set
2759 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2760 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2761 callers defer that decision until it is clear that DECL is actually
2762 required. */
2764 void
2765 import_export_decl (tree decl)
2767 int emit_p;
2768 bool comdat_p;
2769 bool import_p;
2770 tree class_type = NULL_TREE;
2772 if (DECL_INTERFACE_KNOWN (decl))
2773 return;
2775 /* We cannot determine what linkage to give to an entity with vague
2776 linkage until the end of the file. For example, a virtual table
2777 for a class will be defined if and only if the key method is
2778 defined in this translation unit. As a further example, consider
2779 that when compiling a translation unit that uses PCH file with
2780 "-frepo" it would be incorrect to make decisions about what
2781 entities to emit when building the PCH; those decisions must be
2782 delayed until the repository information has been processed. */
2783 gcc_assert (at_eof);
2784 /* Object file linkage for explicit instantiations is handled in
2785 mark_decl_instantiated. For static variables in functions with
2786 vague linkage, maybe_commonize_var is used.
2788 Therefore, the only declarations that should be provided to this
2789 function are those with external linkage that are:
2791 * implicit instantiations of function templates
2793 * inline function
2795 * implicit instantiations of static data members of class
2796 templates
2798 * virtual tables
2800 * typeinfo objects
2802 Furthermore, all entities that reach this point must have a
2803 definition available in this translation unit.
2805 The following assertions check these conditions. */
2806 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2807 /* Any code that creates entities with TREE_PUBLIC cleared should
2808 also set DECL_INTERFACE_KNOWN. */
2809 gcc_assert (TREE_PUBLIC (decl));
2810 if (TREE_CODE (decl) == FUNCTION_DECL)
2811 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2812 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2813 || DECL_DECLARED_INLINE_P (decl));
2814 else
2815 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2816 || DECL_VTABLE_OR_VTT_P (decl)
2817 || DECL_TINFO_P (decl));
2818 /* Check that a definition of DECL is available in this translation
2819 unit. */
2820 gcc_assert (!DECL_REALLY_EXTERN (decl));
2822 /* Assume that DECL will not have COMDAT linkage. */
2823 comdat_p = false;
2824 /* Assume that DECL will not be imported into this translation
2825 unit. */
2826 import_p = false;
2828 /* See if the repository tells us whether or not to emit DECL in
2829 this translation unit. */
2830 emit_p = repo_emit_p (decl);
2831 if (emit_p == 0)
2832 import_p = true;
2833 else if (emit_p == 1)
2835 /* The repository indicates that this entity should be defined
2836 here. Make sure the back end honors that request. */
2837 mark_needed (decl);
2838 /* Output the definition as an ordinary strong definition. */
2839 DECL_EXTERNAL (decl) = 0;
2840 DECL_INTERFACE_KNOWN (decl) = 1;
2841 return;
2844 if (import_p)
2845 /* We have already decided what to do with this DECL; there is no
2846 need to check anything further. */
2848 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2850 class_type = DECL_CONTEXT (decl);
2851 import_export_class (class_type);
2852 if (TYPE_FOR_JAVA (class_type))
2853 import_p = true;
2854 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2855 && CLASSTYPE_INTERFACE_ONLY (class_type))
2856 import_p = true;
2857 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2858 && !CLASSTYPE_USE_TEMPLATE (class_type)
2859 && CLASSTYPE_KEY_METHOD (class_type)
2860 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2861 /* The ABI requires that all virtual tables be emitted with
2862 COMDAT linkage. However, on systems where COMDAT symbols
2863 don't show up in the table of contents for a static
2864 archive, or on systems without weak symbols (where we
2865 approximate COMDAT linkage by using internal linkage), the
2866 linker will report errors about undefined symbols because
2867 it will not see the virtual table definition. Therefore,
2868 in the case that we know that the virtual table will be
2869 emitted in only one translation unit, we make the virtual
2870 table an ordinary definition with external linkage. */
2871 DECL_EXTERNAL (decl) = 0;
2872 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2874 /* CLASS_TYPE is being exported from this translation unit,
2875 so DECL should be defined here. */
2876 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2877 /* If a class is declared in a header with the "extern
2878 template" extension, then it will not be instantiated,
2879 even in translation units that would normally require
2880 it. Often such classes are explicitly instantiated in
2881 one translation unit. Therefore, the explicit
2882 instantiation must be made visible to other translation
2883 units. */
2884 DECL_EXTERNAL (decl) = 0;
2885 else
2887 /* The generic C++ ABI says that class data is always
2888 COMDAT, even if there is a key function. Some
2889 variants (e.g., the ARM EABI) says that class data
2890 only has COMDAT linkage if the class data might be
2891 emitted in more than one translation unit. When the
2892 key method can be inline and is inline, we still have
2893 to arrange for comdat even though
2894 class_data_always_comdat is false. */
2895 if (!CLASSTYPE_KEY_METHOD (class_type)
2896 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2897 || targetm.cxx.class_data_always_comdat ())
2899 /* The ABI requires COMDAT linkage. Normally, we
2900 only emit COMDAT things when they are needed;
2901 make sure that we realize that this entity is
2902 indeed needed. */
2903 comdat_p = true;
2904 mark_needed (decl);
2908 else if (!flag_implicit_templates
2909 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2910 import_p = true;
2911 else
2912 comdat_p = true;
2914 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2916 tree type = TREE_TYPE (DECL_NAME (decl));
2917 if (CLASS_TYPE_P (type))
2919 class_type = type;
2920 import_export_class (type);
2921 if (CLASSTYPE_INTERFACE_KNOWN (type)
2922 && TYPE_POLYMORPHIC_P (type)
2923 && CLASSTYPE_INTERFACE_ONLY (type)
2924 /* If -fno-rtti was specified, then we cannot be sure
2925 that RTTI information will be emitted with the
2926 virtual table of the class, so we must emit it
2927 wherever it is used. */
2928 && flag_rtti)
2929 import_p = true;
2930 else
2932 if (CLASSTYPE_INTERFACE_KNOWN (type)
2933 && !CLASSTYPE_INTERFACE_ONLY (type))
2935 comdat_p = (targetm.cxx.class_data_always_comdat ()
2936 || (CLASSTYPE_KEY_METHOD (type)
2937 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2938 mark_needed (decl);
2939 if (!flag_weak)
2941 comdat_p = false;
2942 DECL_EXTERNAL (decl) = 0;
2945 else
2946 comdat_p = true;
2949 else
2950 comdat_p = true;
2952 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2954 /* DECL is an implicit instantiation of a function or static
2955 data member. */
2956 if ((flag_implicit_templates
2957 && !flag_use_repository)
2958 || (flag_implicit_inline_templates
2959 && TREE_CODE (decl) == FUNCTION_DECL
2960 && DECL_DECLARED_INLINE_P (decl)))
2961 comdat_p = true;
2962 else
2963 /* If we are not implicitly generating templates, then mark
2964 this entity as undefined in this translation unit. */
2965 import_p = true;
2967 else if (DECL_FUNCTION_MEMBER_P (decl))
2969 if (!DECL_DECLARED_INLINE_P (decl))
2971 tree ctype = DECL_CONTEXT (decl);
2972 import_export_class (ctype);
2973 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2975 DECL_NOT_REALLY_EXTERN (decl)
2976 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2977 || (DECL_DECLARED_INLINE_P (decl)
2978 && ! flag_implement_inlines
2979 && !DECL_VINDEX (decl)));
2981 if (!DECL_NOT_REALLY_EXTERN (decl))
2982 DECL_EXTERNAL (decl) = 1;
2984 /* Always make artificials weak. */
2985 if (DECL_ARTIFICIAL (decl) && flag_weak)
2986 comdat_p = true;
2987 else
2988 maybe_make_one_only (decl);
2991 else
2992 comdat_p = true;
2994 else
2995 comdat_p = true;
2997 if (import_p)
2999 /* If we are importing DECL into this translation unit, mark is
3000 an undefined here. */
3001 DECL_EXTERNAL (decl) = 1;
3002 DECL_NOT_REALLY_EXTERN (decl) = 0;
3004 else if (comdat_p)
3006 /* If we decided to put DECL in COMDAT, mark it accordingly at
3007 this point. */
3008 comdat_linkage (decl);
3011 DECL_INTERFACE_KNOWN (decl) = 1;
3014 /* Return an expression that performs the destruction of DECL, which
3015 must be a VAR_DECL whose type has a non-trivial destructor, or is
3016 an array whose (innermost) elements have a non-trivial destructor. */
3018 tree
3019 build_cleanup (tree decl)
3021 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3022 gcc_assert (clean != NULL_TREE);
3023 return clean;
3026 /* Returns the initialization guard variable for the variable DECL,
3027 which has static storage duration. */
3029 tree
3030 get_guard (tree decl)
3032 tree sname;
3033 tree guard;
3035 sname = mangle_guard_variable (decl);
3036 guard = IDENTIFIER_GLOBAL_VALUE (sname);
3037 if (! guard)
3039 tree guard_type;
3041 /* We use a type that is big enough to contain a mutex as well
3042 as an integer counter. */
3043 guard_type = targetm.cxx.guard_type ();
3044 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3045 VAR_DECL, sname, guard_type);
3047 /* The guard should have the same linkage as what it guards. */
3048 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3049 TREE_STATIC (guard) = TREE_STATIC (decl);
3050 DECL_COMMON (guard) = DECL_COMMON (decl);
3051 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3052 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3053 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3054 if (DECL_ONE_ONLY (decl))
3055 make_decl_one_only (guard, cxx_comdat_group (guard));
3056 if (TREE_PUBLIC (decl))
3057 DECL_WEAK (guard) = DECL_WEAK (decl);
3058 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3059 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3061 DECL_ARTIFICIAL (guard) = 1;
3062 DECL_IGNORED_P (guard) = 1;
3063 TREE_USED (guard) = 1;
3064 pushdecl_top_level_and_finish (guard, NULL_TREE);
3066 return guard;
3069 /* Return an atomic load of src with the appropriate memory model. */
3071 static tree
3072 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3074 tree ptr_type = build_pointer_type (char_type_node);
3075 tree mem_model = build_int_cst (integer_type_node, model);
3076 tree t, addr, val;
3077 unsigned int size;
3078 int fncode;
3080 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3082 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3083 t = builtin_decl_implicit ((enum built_in_function) fncode);
3085 addr = build1 (ADDR_EXPR, ptr_type, src);
3086 val = build_call_expr (t, 2, addr, mem_model);
3087 return val;
3090 /* Return those bits of the GUARD variable that should be set when the
3091 guarded entity is actually initialized. */
3093 static tree
3094 get_guard_bits (tree guard)
3096 if (!targetm.cxx.guard_mask_bit ())
3098 /* We only set the first byte of the guard, in order to leave room
3099 for a mutex in the high-order bits. */
3100 guard = build1 (ADDR_EXPR,
3101 build_pointer_type (TREE_TYPE (guard)),
3102 guard);
3103 guard = build1 (NOP_EXPR,
3104 build_pointer_type (char_type_node),
3105 guard);
3106 guard = build1 (INDIRECT_REF, char_type_node, guard);
3109 return guard;
3112 /* Return an expression which determines whether or not the GUARD
3113 variable has already been initialized. */
3115 tree
3116 get_guard_cond (tree guard, bool thread_safe)
3118 tree guard_value;
3120 if (!thread_safe)
3121 guard = get_guard_bits (guard);
3122 else
3123 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3125 /* Mask off all but the low bit. */
3126 if (targetm.cxx.guard_mask_bit ())
3128 guard_value = integer_one_node;
3129 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3130 guard_value = convert (TREE_TYPE (guard), guard_value);
3131 guard = cp_build_binary_op (input_location,
3132 BIT_AND_EXPR, guard, guard_value,
3133 tf_warning_or_error);
3136 guard_value = integer_zero_node;
3137 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3138 guard_value = convert (TREE_TYPE (guard), guard_value);
3139 return cp_build_binary_op (input_location,
3140 EQ_EXPR, guard, guard_value,
3141 tf_warning_or_error);
3144 /* Return an expression which sets the GUARD variable, indicating that
3145 the variable being guarded has been initialized. */
3147 tree
3148 set_guard (tree guard)
3150 tree guard_init;
3152 /* Set the GUARD to one. */
3153 guard = get_guard_bits (guard);
3154 guard_init = integer_one_node;
3155 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3156 guard_init = convert (TREE_TYPE (guard), guard_init);
3157 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
3158 tf_warning_or_error);
3161 /* Returns true iff we can tell that VAR does not have a dynamic
3162 initializer. */
3164 static bool
3165 var_defined_without_dynamic_init (tree var)
3167 /* If it's defined in another TU, we can't tell. */
3168 if (DECL_EXTERNAL (var))
3169 return false;
3170 /* If it has a non-trivial destructor, registering the destructor
3171 counts as dynamic initialization. */
3172 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3173 return false;
3174 /* If it's in this TU, its initializer has been processed, unless
3175 it's a case of self-initialization, then DECL_INITIALIZED_P is
3176 false while the initializer is handled by finish_id_expression. */
3177 if (!DECL_INITIALIZED_P (var))
3178 return false;
3179 /* If it has no initializer or a constant one, it's not dynamic. */
3180 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3181 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3184 /* Returns true iff VAR is a variable that needs uses to be
3185 wrapped for possible dynamic initialization. */
3187 static bool
3188 var_needs_tls_wrapper (tree var)
3190 return (!error_operand_p (var)
3191 && CP_DECL_THREAD_LOCAL_P (var)
3192 && !DECL_GNU_TLS_P (var)
3193 && !DECL_FUNCTION_SCOPE_P (var)
3194 && !var_defined_without_dynamic_init (var));
3197 /* Get the FUNCTION_DECL for the shared TLS init function for this
3198 translation unit. */
3200 static tree
3201 get_local_tls_init_fn (void)
3203 tree sname = get_identifier ("__tls_init");
3204 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3205 if (!fn)
3207 fn = build_lang_decl (FUNCTION_DECL, sname,
3208 build_function_type (void_type_node,
3209 void_list_node));
3210 SET_DECL_LANGUAGE (fn, lang_c);
3211 TREE_PUBLIC (fn) = false;
3212 DECL_ARTIFICIAL (fn) = true;
3213 mark_used (fn);
3214 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3216 return fn;
3219 /* Get a FUNCTION_DECL for the init function for the thread_local
3220 variable VAR. The init function will be an alias to the function
3221 that initializes all the non-local TLS variables in the translation
3222 unit. The init function is only used by the wrapper function. */
3224 static tree
3225 get_tls_init_fn (tree var)
3227 /* Only C++11 TLS vars need this init fn. */
3228 if (!var_needs_tls_wrapper (var))
3229 return NULL_TREE;
3231 /* If -fno-extern-tls-init, assume that we don't need to call
3232 a tls init function for a variable defined in another TU. */
3233 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3234 return NULL_TREE;
3236 #ifdef ASM_OUTPUT_DEF
3237 /* If the variable is internal, or if we can't generate aliases,
3238 call the local init function directly. */
3239 if (!TREE_PUBLIC (var))
3240 #endif
3241 return get_local_tls_init_fn ();
3243 tree sname = mangle_tls_init_fn (var);
3244 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3245 if (!fn)
3247 fn = build_lang_decl (FUNCTION_DECL, sname,
3248 build_function_type (void_type_node,
3249 void_list_node));
3250 SET_DECL_LANGUAGE (fn, lang_c);
3251 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3252 DECL_ARTIFICIAL (fn) = true;
3253 DECL_COMDAT (fn) = DECL_COMDAT (var);
3254 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3255 if (DECL_ONE_ONLY (var))
3256 make_decl_one_only (fn, cxx_comdat_group (fn));
3257 if (TREE_PUBLIC (var))
3259 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3260 /* If the variable is defined somewhere else and might have static
3261 initialization, make the init function a weak reference. */
3262 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3263 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3264 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3265 && DECL_EXTERNAL (var))
3266 declare_weak (fn);
3267 else
3268 DECL_WEAK (fn) = DECL_WEAK (var);
3270 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3271 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3272 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3273 DECL_IGNORED_P (fn) = 1;
3274 mark_used (fn);
3276 DECL_BEFRIENDING_CLASSES (fn) = var;
3278 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3280 return fn;
3283 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3284 variable VAR. The wrapper function calls the init function (if any) for
3285 VAR and then returns a reference to VAR. The wrapper function is used
3286 in place of VAR everywhere VAR is mentioned. */
3288 tree
3289 get_tls_wrapper_fn (tree var)
3291 /* Only C++11 TLS vars need this wrapper fn. */
3292 if (!var_needs_tls_wrapper (var))
3293 return NULL_TREE;
3295 tree sname = mangle_tls_wrapper_fn (var);
3296 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3297 if (!fn)
3299 /* A named rvalue reference is an lvalue, so the wrapper should
3300 always return an lvalue reference. */
3301 tree type = non_reference (TREE_TYPE (var));
3302 type = build_reference_type (type);
3303 tree fntype = build_function_type (type, void_list_node);
3304 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3305 SET_DECL_LANGUAGE (fn, lang_c);
3306 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3307 DECL_ARTIFICIAL (fn) = true;
3308 DECL_IGNORED_P (fn) = 1;
3309 /* The wrapper is inline and emitted everywhere var is used. */
3310 DECL_DECLARED_INLINE_P (fn) = true;
3311 if (TREE_PUBLIC (var))
3313 comdat_linkage (fn);
3314 #ifdef HAVE_GAS_HIDDEN
3315 /* Make the wrapper bind locally; there's no reason to share
3316 the wrapper between multiple shared objects. */
3317 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3318 DECL_VISIBILITY_SPECIFIED (fn) = true;
3319 #endif
3321 if (!TREE_PUBLIC (fn))
3322 DECL_INTERFACE_KNOWN (fn) = true;
3323 mark_used (fn);
3324 note_vague_linkage_fn (fn);
3326 #if 0
3327 /* We want CSE to commonize calls to the wrapper, but marking it as
3328 pure is unsafe since it has side-effects. I guess we need a new
3329 ECF flag even weaker than ECF_PURE. FIXME! */
3330 DECL_PURE_P (fn) = true;
3331 #endif
3333 DECL_BEFRIENDING_CLASSES (fn) = var;
3335 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3337 return fn;
3340 /* At EOF, generate the definition for the TLS wrapper function FN:
3342 T& var_wrapper() {
3343 if (init_fn) init_fn();
3344 return var;
3345 } */
3347 static void
3348 generate_tls_wrapper (tree fn)
3350 tree var = DECL_BEFRIENDING_CLASSES (fn);
3352 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3353 tree body = begin_function_body ();
3354 /* Only call the init fn if there might be one. */
3355 if (tree init_fn = get_tls_init_fn (var))
3357 tree if_stmt = NULL_TREE;
3358 /* If init_fn is a weakref, make sure it exists before calling. */
3359 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3361 if_stmt = begin_if_stmt ();
3362 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3363 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3364 NE_EXPR, addr, nullptr_node,
3365 tf_warning_or_error);
3366 finish_if_stmt_cond (cond, if_stmt);
3368 finish_expr_stmt (build_cxx_call
3369 (init_fn, 0, NULL, tf_warning_or_error));
3370 if (if_stmt)
3372 finish_then_clause (if_stmt);
3373 finish_if_stmt (if_stmt);
3376 else
3377 /* If there's no initialization, the wrapper is a constant function. */
3378 TREE_READONLY (fn) = true;
3379 finish_return_stmt (convert_from_reference (var));
3380 finish_function_body (body);
3381 expand_or_defer_fn (finish_function (0));
3384 /* Start the process of running a particular set of global constructors
3385 or destructors. Subroutine of do_[cd]tors. Also called from
3386 vtv_start_verification_constructor_init_function. */
3388 static tree
3389 start_objects (int method_type, int initp)
3391 tree body;
3392 tree fndecl;
3393 char type[14];
3395 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3397 if (initp != DEFAULT_INIT_PRIORITY)
3399 char joiner;
3401 #ifdef JOINER
3402 joiner = JOINER;
3403 #else
3404 joiner = '_';
3405 #endif
3407 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3409 else
3410 sprintf (type, "sub_%c", method_type);
3412 fndecl = build_lang_decl (FUNCTION_DECL,
3413 get_file_function_name (type),
3414 build_function_type_list (void_type_node,
3415 NULL_TREE));
3416 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3418 TREE_PUBLIC (current_function_decl) = 0;
3420 /* Mark as artificial because it's not explicitly in the user's
3421 source code. */
3422 DECL_ARTIFICIAL (current_function_decl) = 1;
3424 /* Mark this declaration as used to avoid spurious warnings. */
3425 TREE_USED (current_function_decl) = 1;
3427 /* Mark this function as a global constructor or destructor. */
3428 if (method_type == 'I')
3429 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3430 else
3431 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3433 body = begin_compound_stmt (BCS_FN_BODY);
3435 return body;
3438 /* Finish the process of running a particular set of global constructors
3439 or destructors. Subroutine of do_[cd]tors. */
3441 static void
3442 finish_objects (int method_type, int initp, tree body)
3444 tree fn;
3446 /* Finish up. */
3447 finish_compound_stmt (body);
3448 fn = finish_function (0);
3450 if (method_type == 'I')
3452 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3453 decl_init_priority_insert (fn, initp);
3455 else
3457 DECL_STATIC_DESTRUCTOR (fn) = 1;
3458 decl_fini_priority_insert (fn, initp);
3461 expand_or_defer_fn (fn);
3464 /* The names of the parameters to the function created to handle
3465 initializations and destructions for objects with static storage
3466 duration. */
3467 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3468 #define PRIORITY_IDENTIFIER "__priority"
3470 /* The name of the function we create to handle initializations and
3471 destructions for objects with static storage duration. */
3472 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3474 /* The declaration for the __INITIALIZE_P argument. */
3475 static GTY(()) tree initialize_p_decl;
3477 /* The declaration for the __PRIORITY argument. */
3478 static GTY(()) tree priority_decl;
3480 /* The declaration for the static storage duration function. */
3481 static GTY(()) tree ssdf_decl;
3483 /* All the static storage duration functions created in this
3484 translation unit. */
3485 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3487 /* A map from priority levels to information about that priority
3488 level. There may be many such levels, so efficient lookup is
3489 important. */
3490 static splay_tree priority_info_map;
3492 /* Begins the generation of the function that will handle all
3493 initialization and destruction of objects with static storage
3494 duration. The function generated takes two parameters of type
3495 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3496 nonzero, it performs initializations. Otherwise, it performs
3497 destructions. It only performs those initializations or
3498 destructions with the indicated __PRIORITY. The generated function
3499 returns no value.
3501 It is assumed that this function will only be called once per
3502 translation unit. */
3504 static tree
3505 start_static_storage_duration_function (unsigned count)
3507 tree type;
3508 tree body;
3509 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3511 /* Create the identifier for this function. It will be of the form
3512 SSDF_IDENTIFIER_<number>. */
3513 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3515 type = build_function_type_list (void_type_node,
3516 integer_type_node, integer_type_node,
3517 NULL_TREE);
3519 /* Create the FUNCTION_DECL itself. */
3520 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3521 get_identifier (id),
3522 type);
3523 TREE_PUBLIC (ssdf_decl) = 0;
3524 DECL_ARTIFICIAL (ssdf_decl) = 1;
3526 /* Put this function in the list of functions to be called from the
3527 static constructors and destructors. */
3528 if (!ssdf_decls)
3530 vec_alloc (ssdf_decls, 32);
3532 /* Take this opportunity to initialize the map from priority
3533 numbers to information about that priority level. */
3534 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3535 /*delete_key_fn=*/0,
3536 /*delete_value_fn=*/
3537 (splay_tree_delete_value_fn) &free);
3539 /* We always need to generate functions for the
3540 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3541 priorities later, we'll be sure to find the
3542 DEFAULT_INIT_PRIORITY. */
3543 get_priority_info (DEFAULT_INIT_PRIORITY);
3546 vec_safe_push (ssdf_decls, ssdf_decl);
3548 /* Create the argument list. */
3549 initialize_p_decl = cp_build_parm_decl
3550 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3551 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3552 TREE_USED (initialize_p_decl) = 1;
3553 priority_decl = cp_build_parm_decl
3554 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3555 DECL_CONTEXT (priority_decl) = ssdf_decl;
3556 TREE_USED (priority_decl) = 1;
3558 DECL_CHAIN (initialize_p_decl) = priority_decl;
3559 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3561 /* Put the function in the global scope. */
3562 pushdecl (ssdf_decl);
3564 /* Start the function itself. This is equivalent to declaring the
3565 function as:
3567 static void __ssdf (int __initialize_p, init __priority_p);
3569 It is static because we only need to call this function from the
3570 various constructor and destructor functions for this module. */
3571 start_preparsed_function (ssdf_decl,
3572 /*attrs=*/NULL_TREE,
3573 SF_PRE_PARSED);
3575 /* Set up the scope of the outermost block in the function. */
3576 body = begin_compound_stmt (BCS_FN_BODY);
3578 return body;
3581 /* Finish the generation of the function which performs initialization
3582 and destruction of objects with static storage duration. After
3583 this point, no more such objects can be created. */
3585 static void
3586 finish_static_storage_duration_function (tree body)
3588 /* Close out the function. */
3589 finish_compound_stmt (body);
3590 expand_or_defer_fn (finish_function (0));
3593 /* Return the information about the indicated PRIORITY level. If no
3594 code to handle this level has yet been generated, generate the
3595 appropriate prologue. */
3597 static priority_info
3598 get_priority_info (int priority)
3600 priority_info pi;
3601 splay_tree_node n;
3603 n = splay_tree_lookup (priority_info_map,
3604 (splay_tree_key) priority);
3605 if (!n)
3607 /* Create a new priority information structure, and insert it
3608 into the map. */
3609 pi = XNEW (struct priority_info_s);
3610 pi->initializations_p = 0;
3611 pi->destructions_p = 0;
3612 splay_tree_insert (priority_info_map,
3613 (splay_tree_key) priority,
3614 (splay_tree_value) pi);
3616 else
3617 pi = (priority_info) n->value;
3619 return pi;
3622 /* The effective initialization priority of a DECL. */
3624 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3625 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3626 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3628 /* Whether a DECL needs a guard to protect it against multiple
3629 initialization. */
3631 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3632 || DECL_ONE_ONLY (decl) \
3633 || DECL_WEAK (decl)))
3635 /* Called from one_static_initialization_or_destruction(),
3636 via walk_tree.
3637 Walks the initializer list of a global variable and looks for
3638 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3639 and that have their DECL_CONTEXT() == NULL.
3640 For each such temporary variable, set their DECL_CONTEXT() to
3641 the current function. This is necessary because otherwise
3642 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3643 when trying to refer to a temporary variable that does not have
3644 it's DECL_CONTECT() properly set. */
3645 static tree
3646 fix_temporary_vars_context_r (tree *node,
3647 int * /*unused*/,
3648 void * /*unused1*/)
3650 gcc_assert (current_function_decl);
3652 if (TREE_CODE (*node) == BIND_EXPR)
3654 tree var;
3656 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3657 if (VAR_P (var)
3658 && !DECL_NAME (var)
3659 && DECL_ARTIFICIAL (var)
3660 && !DECL_CONTEXT (var))
3661 DECL_CONTEXT (var) = current_function_decl;
3664 return NULL_TREE;
3667 /* Set up to handle the initialization or destruction of DECL. If
3668 INITP is nonzero, we are initializing the variable. Otherwise, we
3669 are destroying it. */
3671 static void
3672 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3674 tree guard_if_stmt = NULL_TREE;
3675 tree guard;
3677 /* If we are supposed to destruct and there's a trivial destructor,
3678 nothing has to be done. */
3679 if (!initp
3680 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3681 return;
3683 /* Trick the compiler into thinking we are at the file and line
3684 where DECL was declared so that error-messages make sense, and so
3685 that the debugger will show somewhat sensible file and line
3686 information. */
3687 input_location = DECL_SOURCE_LOCATION (decl);
3689 /* Make sure temporary variables in the initialiser all have
3690 their DECL_CONTEXT() set to a value different from NULL_TREE.
3691 This can happen when global variables initialisers are built.
3692 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3693 the temporary variables that might have been generated in the
3694 accompagning initialisers is NULL_TREE, meaning the variables have been
3695 declared in the global namespace.
3696 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3697 of the temporaries are set to the current function decl. */
3698 cp_walk_tree_without_duplicates (&init,
3699 fix_temporary_vars_context_r,
3700 NULL);
3702 /* Because of:
3704 [class.access.spec]
3706 Access control for implicit calls to the constructors,
3707 the conversion functions, or the destructor called to
3708 create and destroy a static data member is performed as
3709 if these calls appeared in the scope of the member's
3710 class.
3712 we pretend we are in a static member function of the class of
3713 which the DECL is a member. */
3714 if (member_p (decl))
3716 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3717 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3720 /* Assume we don't need a guard. */
3721 guard = NULL_TREE;
3722 /* We need a guard if this is an object with external linkage that
3723 might be initialized in more than one place. (For example, a
3724 static data member of a template, when the data member requires
3725 construction.) */
3726 if (NEEDS_GUARD_P (decl))
3728 tree guard_cond;
3730 guard = get_guard (decl);
3732 /* When using __cxa_atexit, we just check the GUARD as we would
3733 for a local static. */
3734 if (flag_use_cxa_atexit)
3736 /* When using __cxa_atexit, we never try to destroy
3737 anything from a static destructor. */
3738 gcc_assert (initp);
3739 guard_cond = get_guard_cond (guard, false);
3741 /* If we don't have __cxa_atexit, then we will be running
3742 destructors from .fini sections, or their equivalents. So,
3743 we need to know how many times we've tried to initialize this
3744 object. We do initializations only if the GUARD is zero,
3745 i.e., if we are the first to initialize the variable. We do
3746 destructions only if the GUARD is one, i.e., if we are the
3747 last to destroy the variable. */
3748 else if (initp)
3749 guard_cond
3750 = cp_build_binary_op (input_location,
3751 EQ_EXPR,
3752 cp_build_unary_op (PREINCREMENT_EXPR,
3753 guard,
3754 /*noconvert=*/1,
3755 tf_warning_or_error),
3756 integer_one_node,
3757 tf_warning_or_error);
3758 else
3759 guard_cond
3760 = cp_build_binary_op (input_location,
3761 EQ_EXPR,
3762 cp_build_unary_op (PREDECREMENT_EXPR,
3763 guard,
3764 /*noconvert=*/1,
3765 tf_warning_or_error),
3766 integer_zero_node,
3767 tf_warning_or_error);
3769 guard_if_stmt = begin_if_stmt ();
3770 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3774 /* If we're using __cxa_atexit, we have not already set the GUARD,
3775 so we must do so now. */
3776 if (guard && initp && flag_use_cxa_atexit)
3777 finish_expr_stmt (set_guard (guard));
3779 /* Perform the initialization or destruction. */
3780 if (initp)
3782 if (init)
3784 finish_expr_stmt (init);
3785 if (flag_sanitize & SANITIZE_ADDRESS)
3787 varpool_node *vnode = varpool_node::get (decl);
3788 if (vnode)
3789 vnode->dynamically_initialized = 1;
3793 /* If we're using __cxa_atexit, register a function that calls the
3794 destructor for the object. */
3795 if (flag_use_cxa_atexit)
3796 finish_expr_stmt (register_dtor_fn (decl));
3798 else
3799 finish_expr_stmt (build_cleanup (decl));
3801 /* Finish the guard if-stmt, if necessary. */
3802 if (guard)
3804 finish_then_clause (guard_if_stmt);
3805 finish_if_stmt (guard_if_stmt);
3808 /* Now that we're done with DECL we don't need to pretend to be a
3809 member of its class any longer. */
3810 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3811 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3814 /* Generate code to do the initialization or destruction of the decls in VARS,
3815 a TREE_LIST of VAR_DECL with static storage duration.
3816 Whether initialization or destruction is performed is specified by INITP. */
3818 static void
3819 do_static_initialization_or_destruction (tree vars, bool initp)
3821 tree node, init_if_stmt, cond;
3823 /* Build the outer if-stmt to check for initialization or destruction. */
3824 init_if_stmt = begin_if_stmt ();
3825 cond = initp ? integer_one_node : integer_zero_node;
3826 cond = cp_build_binary_op (input_location,
3827 EQ_EXPR,
3828 initialize_p_decl,
3829 cond,
3830 tf_warning_or_error);
3831 finish_if_stmt_cond (cond, init_if_stmt);
3833 /* To make sure dynamic construction doesn't access globals from other
3834 compilation units where they might not be yet constructed, for
3835 -fsanitize=address insert __asan_before_dynamic_init call that
3836 prevents access to either all global variables that need construction
3837 in other compilation units, or at least those that haven't been
3838 initialized yet. Variables that need dynamic construction in
3839 the current compilation unit are kept accessible. */
3840 if (flag_sanitize & SANITIZE_ADDRESS)
3841 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3843 node = vars;
3844 do {
3845 tree decl = TREE_VALUE (node);
3846 tree priority_if_stmt;
3847 int priority;
3848 priority_info pi;
3850 /* If we don't need a destructor, there's nothing to do. Avoid
3851 creating a possibly empty if-stmt. */
3852 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3854 node = TREE_CHAIN (node);
3855 continue;
3858 /* Remember that we had an initialization or finalization at this
3859 priority. */
3860 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3861 pi = get_priority_info (priority);
3862 if (initp)
3863 pi->initializations_p = 1;
3864 else
3865 pi->destructions_p = 1;
3867 /* Conditionalize this initialization on being in the right priority
3868 and being initializing/finalizing appropriately. */
3869 priority_if_stmt = begin_if_stmt ();
3870 cond = cp_build_binary_op (input_location,
3871 EQ_EXPR,
3872 priority_decl,
3873 build_int_cst (NULL_TREE, priority),
3874 tf_warning_or_error);
3875 finish_if_stmt_cond (cond, priority_if_stmt);
3877 /* Process initializers with same priority. */
3878 for (; node
3879 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3880 node = TREE_CHAIN (node))
3881 /* Do one initialization or destruction. */
3882 one_static_initialization_or_destruction (TREE_VALUE (node),
3883 TREE_PURPOSE (node), initp);
3885 /* Finish up the priority if-stmt body. */
3886 finish_then_clause (priority_if_stmt);
3887 finish_if_stmt (priority_if_stmt);
3889 } while (node);
3891 /* Revert what __asan_before_dynamic_init did by calling
3892 __asan_after_dynamic_init. */
3893 if (flag_sanitize & SANITIZE_ADDRESS)
3894 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3896 /* Finish up the init/destruct if-stmt body. */
3897 finish_then_clause (init_if_stmt);
3898 finish_if_stmt (init_if_stmt);
3901 /* VARS is a list of variables with static storage duration which may
3902 need initialization and/or finalization. Remove those variables
3903 that don't really need to be initialized or finalized, and return
3904 the resulting list. The order in which the variables appear in
3905 VARS is in reverse order of the order in which they should actually
3906 be initialized. The list we return is in the unreversed order;
3907 i.e., the first variable should be initialized first. */
3909 static tree
3910 prune_vars_needing_no_initialization (tree *vars)
3912 tree *var = vars;
3913 tree result = NULL_TREE;
3915 while (*var)
3917 tree t = *var;
3918 tree decl = TREE_VALUE (t);
3919 tree init = TREE_PURPOSE (t);
3921 /* Deal gracefully with error. */
3922 if (decl == error_mark_node)
3924 var = &TREE_CHAIN (t);
3925 continue;
3928 /* The only things that can be initialized are variables. */
3929 gcc_assert (VAR_P (decl));
3931 /* If this object is not defined, we don't need to do anything
3932 here. */
3933 if (DECL_EXTERNAL (decl))
3935 var = &TREE_CHAIN (t);
3936 continue;
3939 /* Also, if the initializer already contains errors, we can bail
3940 out now. */
3941 if (init && TREE_CODE (init) == TREE_LIST
3942 && value_member (error_mark_node, init))
3944 var = &TREE_CHAIN (t);
3945 continue;
3948 /* This variable is going to need initialization and/or
3949 finalization, so we add it to the list. */
3950 *var = TREE_CHAIN (t);
3951 TREE_CHAIN (t) = result;
3952 result = t;
3955 return result;
3958 /* Make sure we have told the back end about all the variables in
3959 VARS. */
3961 static void
3962 write_out_vars (tree vars)
3964 tree v;
3966 for (v = vars; v; v = TREE_CHAIN (v))
3968 tree var = TREE_VALUE (v);
3969 if (!var_finalized_p (var))
3971 import_export_decl (var);
3972 rest_of_decl_compilation (var, 1, 1);
3977 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3978 (otherwise) that will initialize all global objects with static
3979 storage duration having the indicated PRIORITY. */
3981 static void
3982 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3983 location_t *locus)
3985 char function_key;
3986 tree fndecl;
3987 tree body;
3988 size_t i;
3990 input_location = *locus;
3991 /* ??? */
3992 /* Was: locus->line++; */
3994 /* We use `I' to indicate initialization and `D' to indicate
3995 destruction. */
3996 function_key = constructor_p ? 'I' : 'D';
3998 /* We emit the function lazily, to avoid generating empty
3999 global constructors and destructors. */
4000 body = NULL_TREE;
4002 /* For Objective-C++, we may need to initialize metadata found in this module.
4003 This must be done _before_ any other static initializations. */
4004 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4005 && constructor_p && objc_static_init_needed_p ())
4007 body = start_objects (function_key, priority);
4008 objc_generate_static_init_call (NULL_TREE);
4011 /* Call the static storage duration function with appropriate
4012 arguments. */
4013 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4015 /* Calls to pure or const functions will expand to nothing. */
4016 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4018 tree call;
4020 if (! body)
4021 body = start_objects (function_key, priority);
4023 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4024 build_int_cst (NULL_TREE,
4025 constructor_p),
4026 build_int_cst (NULL_TREE,
4027 priority),
4028 NULL_TREE);
4029 finish_expr_stmt (call);
4033 /* Close out the function. */
4034 if (body)
4035 finish_objects (function_key, priority, body);
4038 /* Generate constructor and destructor functions for the priority
4039 indicated by N. */
4041 static int
4042 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4044 location_t *locus = (location_t *) data;
4045 int priority = (int) n->key;
4046 priority_info pi = (priority_info) n->value;
4048 /* Generate the functions themselves, but only if they are really
4049 needed. */
4050 if (pi->initializations_p)
4051 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4052 if (pi->destructions_p)
4053 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4055 /* Keep iterating. */
4056 return 0;
4059 /* Java requires that we be able to reference a local address for a
4060 method, and not be confused by PLT entries. If supported, create a
4061 hidden alias for all such methods. */
4063 static void
4064 build_java_method_aliases (void)
4066 #ifndef HAVE_GAS_HIDDEN
4067 return;
4068 #endif
4070 struct cgraph_node *node;
4071 FOR_EACH_FUNCTION (node)
4073 tree fndecl = node->decl;
4075 if (DECL_CLASS_SCOPE_P (fndecl)
4076 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4077 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4079 /* Mangle the name in a predictable way; we need to reference
4080 this from a java compiled object file. */
4081 tree oid = DECL_ASSEMBLER_NAME (fndecl);
4082 const char *oname = IDENTIFIER_POINTER (oid);
4083 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4084 char *nname = ACONCAT (("_ZGA", oname + 2, NULL));
4086 tree alias = make_alias_for (fndecl, get_identifier (nname));
4087 TREE_PUBLIC (alias) = 1;
4088 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4090 cgraph_node::create_same_body_alias (alias, fndecl);
4095 /* Return C++ property of T, based on given operation OP. */
4097 static int
4098 cpp_check (tree t, cpp_operation op)
4100 switch (op)
4102 case HAS_DEPENDENT_TEMPLATE_ARGS:
4104 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4105 if (!ti)
4106 return 0;
4107 ++processing_template_decl;
4108 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4109 --processing_template_decl;
4110 return dep;
4112 case IS_ABSTRACT:
4113 return DECL_PURE_VIRTUAL_P (t);
4114 case IS_CONSTRUCTOR:
4115 return DECL_CONSTRUCTOR_P (t);
4116 case IS_DESTRUCTOR:
4117 return DECL_DESTRUCTOR_P (t);
4118 case IS_COPY_CONSTRUCTOR:
4119 return DECL_COPY_CONSTRUCTOR_P (t);
4120 case IS_MOVE_CONSTRUCTOR:
4121 return DECL_MOVE_CONSTRUCTOR_P (t);
4122 case IS_TEMPLATE:
4123 return TREE_CODE (t) == TEMPLATE_DECL;
4124 case IS_TRIVIAL:
4125 return trivial_type_p (t);
4126 default:
4127 return 0;
4131 /* Collect source file references recursively, starting from NAMESPC. */
4133 static void
4134 collect_source_refs (tree namespc)
4136 tree t;
4138 if (!namespc)
4139 return;
4141 /* Iterate over names in this name space. */
4142 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4143 if (!DECL_IS_BUILTIN (t) )
4144 collect_source_ref (DECL_SOURCE_FILE (t));
4146 /* Dump siblings, if any */
4147 collect_source_refs (TREE_CHAIN (namespc));
4149 /* Dump children, if any */
4150 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4153 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4154 starting from NAMESPC. */
4156 static void
4157 collect_ada_namespace (tree namespc, const char *source_file)
4159 if (!namespc)
4160 return;
4162 /* Collect decls from this namespace */
4163 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4165 /* Collect siblings, if any */
4166 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4168 /* Collect children, if any */
4169 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4172 /* Returns true iff there is a definition available for variable or
4173 function DECL. */
4175 static bool
4176 decl_defined_p (tree decl)
4178 if (TREE_CODE (decl) == FUNCTION_DECL)
4179 return (DECL_INITIAL (decl) != NULL_TREE);
4180 else
4182 gcc_assert (VAR_P (decl));
4183 return !DECL_EXTERNAL (decl);
4187 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4189 [expr.const]
4191 An integral constant-expression can only involve ... const
4192 variables of integral or enumeration types initialized with
4193 constant expressions ...
4195 C++0x also allows constexpr variables and temporaries initialized
4196 with constant expressions. We handle the former here, but the latter
4197 are just folded away in cxx_eval_constant_expression.
4199 The standard does not require that the expression be non-volatile.
4200 G++ implements the proposed correction in DR 457. */
4202 bool
4203 decl_constant_var_p (tree decl)
4205 if (!decl_maybe_constant_var_p (decl))
4206 return false;
4208 /* We don't know if a template static data member is initialized with
4209 a constant expression until we instantiate its initializer. Even
4210 in the case of a constexpr variable, we can't treat it as a
4211 constant until its initializer is complete in case it's used in
4212 its own initializer. */
4213 mark_used (decl);
4214 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4217 /* Returns true if DECL could be a symbolic constant variable, depending on
4218 its initializer. */
4220 bool
4221 decl_maybe_constant_var_p (tree decl)
4223 tree type = TREE_TYPE (decl);
4224 if (!VAR_P (decl))
4225 return false;
4226 if (DECL_DECLARED_CONSTEXPR_P (decl))
4227 return true;
4228 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4229 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4232 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4233 called from grokfndecl and grokvardecl; in all modes it is called from
4234 cp_write_global_declarations. */
4236 void
4237 no_linkage_error (tree decl)
4239 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4240 /* In C++11 it's ok if the decl is defined. */
4241 return;
4242 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4243 if (t == NULL_TREE)
4244 /* The type that got us on no_linkage_decls must have gotten a name for
4245 linkage purposes. */;
4246 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4247 /* The type might end up having a typedef name for linkage purposes. */
4248 vec_safe_push (no_linkage_decls, decl);
4249 else if (TYPE_ANONYMOUS_P (t))
4251 bool d = false;
4252 if (cxx_dialect >= cxx11)
4253 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4254 "anonymous type, is used but never defined", decl);
4255 else if (DECL_EXTERN_C_P (decl))
4256 /* Allow this; it's pretty common in C. */;
4257 else if (VAR_P (decl))
4258 /* DRs 132, 319 and 389 seem to indicate types with
4259 no linkage can only be used to declare extern "C"
4260 entities. Since it's not always an error in the
4261 ISO C++ 90 Standard, we only issue a warning. */
4262 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4263 "with no linkage used to declare variable %q#D with "
4264 "linkage", decl);
4265 else
4266 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4267 "linkage used to declare function %q#D with linkage",
4268 decl);
4269 if (d && is_typedef_decl (TYPE_NAME (t)))
4270 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4271 "to the unqualified type, so it is not used for linkage",
4272 TYPE_NAME (t));
4274 else if (cxx_dialect >= cxx11)
4276 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4277 permerror (DECL_SOURCE_LOCATION (decl),
4278 "%q#D, declared using local type "
4279 "%qT, is used but never defined", decl, t);
4281 else if (VAR_P (decl))
4282 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4283 "used to declare variable %q#D with linkage", t, decl);
4284 else
4285 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4286 "to declare function %q#D with linkage", t, decl);
4289 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4291 static void
4292 collect_all_refs (const char *source_file)
4294 collect_ada_namespace (global_namespace, source_file);
4297 /* Clear DECL_EXTERNAL for NODE. */
4299 static bool
4300 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4302 DECL_EXTERNAL (node->decl) = 0;
4303 return false;
4306 /* Build up the function to run dynamic initializers for thread_local
4307 variables in this translation unit and alias the init functions for the
4308 individual variables to it. */
4310 static void
4311 handle_tls_init (void)
4313 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4314 if (vars == NULL_TREE)
4315 return;
4317 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4319 write_out_vars (vars);
4321 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4322 boolean_type_node);
4323 TREE_PUBLIC (guard) = false;
4324 TREE_STATIC (guard) = true;
4325 DECL_ARTIFICIAL (guard) = true;
4326 DECL_IGNORED_P (guard) = true;
4327 TREE_USED (guard) = true;
4328 CP_DECL_THREAD_LOCAL_P (guard) = true;
4329 set_decl_tls_model (guard, decl_default_tls_model (guard));
4330 pushdecl_top_level_and_finish (guard, NULL_TREE);
4332 tree fn = get_local_tls_init_fn ();
4333 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4334 tree body = begin_function_body ();
4335 tree if_stmt = begin_if_stmt ();
4336 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4337 tf_warning_or_error);
4338 finish_if_stmt_cond (cond, if_stmt);
4339 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4340 tf_warning_or_error));
4341 for (; vars; vars = TREE_CHAIN (vars))
4343 tree var = TREE_VALUE (vars);
4344 tree init = TREE_PURPOSE (vars);
4345 one_static_initialization_or_destruction (var, init, true);
4347 #ifdef ASM_OUTPUT_DEF
4348 /* Output init aliases even with -fno-extern-tls-init. */
4349 if (TREE_PUBLIC (var))
4351 tree single_init_fn = get_tls_init_fn (var);
4352 if (single_init_fn == NULL_TREE)
4353 continue;
4354 cgraph_node *alias
4355 = cgraph_node::get_create (fn)->create_same_body_alias
4356 (single_init_fn, fn);
4357 gcc_assert (alias != NULL);
4359 #endif
4362 finish_then_clause (if_stmt);
4363 finish_if_stmt (if_stmt);
4364 finish_function_body (body);
4365 expand_or_defer_fn (finish_function (0));
4368 /* We're at the end of compilation, so generate any mangling aliases that
4369 we've been saving up, if DECL is going to be output and ID2 isn't
4370 already taken by another declaration. */
4372 static void
4373 generate_mangling_alias (tree decl, tree id2)
4375 /* If there's a declaration already using this mangled name,
4376 don't create a compatibility alias that conflicts. */
4377 if (IDENTIFIER_GLOBAL_VALUE (id2))
4378 return;
4380 struct cgraph_node *n = NULL;
4381 if (TREE_CODE (decl) == FUNCTION_DECL
4382 && !(n = cgraph_node::get (decl)))
4383 /* Don't create an alias to an unreferenced function. */
4384 return;
4386 tree alias = make_alias_for (decl, id2);
4387 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4388 DECL_IGNORED_P (alias) = 1;
4389 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4390 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4391 if (vague_linkage_p (decl))
4392 DECL_WEAK (alias) = 1;
4393 if (TREE_CODE (decl) == FUNCTION_DECL)
4394 n->create_same_body_alias (alias, decl);
4395 else
4396 varpool_node::create_extra_name_alias (alias, decl);
4399 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4400 the end of translation, for compatibility across bugs in the mangling
4401 implementation. */
4403 void
4404 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4406 #ifdef ASM_OUTPUT_DEF
4407 if (at_eof)
4408 generate_mangling_alias (decl, id2);
4409 else
4411 vec_safe_push (mangling_aliases, decl);
4412 vec_safe_push (mangling_aliases, id2);
4414 #endif
4417 static void
4418 generate_mangling_aliases ()
4420 while (!vec_safe_is_empty (mangling_aliases))
4422 tree id2 = mangling_aliases->pop();
4423 tree decl = mangling_aliases->pop();
4424 generate_mangling_alias (decl, id2);
4428 /* The entire file is now complete. If requested, dump everything
4429 to a file. */
4431 static void
4432 dump_tu (void)
4434 int flags;
4435 FILE *stream = dump_begin (TDI_tu, &flags);
4437 if (stream)
4439 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4440 dump_end (TDI_tu, stream);
4444 static location_t locus_at_end_of_parsing;
4446 /* Check the deallocation functions for CODE to see if we want to warn that
4447 only one was defined. */
4449 static void
4450 maybe_warn_sized_delete (enum tree_code code)
4452 tree sized = NULL_TREE;
4453 tree unsized = NULL_TREE;
4455 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4456 ovl; ovl = OVL_NEXT (ovl))
4458 tree fn = OVL_CURRENT (ovl);
4459 /* We're only interested in usual deallocation functions. */
4460 if (!non_placement_deallocation_fn_p (fn))
4461 continue;
4462 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4463 unsized = fn;
4464 else
4465 sized = fn;
4467 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4468 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4469 "the program should also define %qD", sized);
4470 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4471 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4472 "the program should also define %qD", unsized);
4475 /* Check the global deallocation functions to see if we want to warn about
4476 defining unsized without sized (or vice versa). */
4478 static void
4479 maybe_warn_sized_delete ()
4481 if (!flag_sized_deallocation || !warn_sized_deallocation)
4482 return;
4483 maybe_warn_sized_delete (DELETE_EXPR);
4484 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4487 /* This routine is called at the end of compilation.
4488 Its job is to create all the code needed to initialize and
4489 destroy the global aggregates. We do the destruction
4490 first, since that way we only need to reverse the decls once. */
4492 void
4493 c_parse_final_cleanups (void)
4495 tree vars;
4496 bool reconsider;
4497 size_t i;
4498 unsigned ssdf_count = 0;
4499 int retries = 0;
4500 tree decl;
4502 locus_at_end_of_parsing = input_location;
4503 at_eof = 1;
4505 /* Bad parse errors. Just forget about it. */
4506 if (! global_bindings_p () || current_class_type
4507 || !vec_safe_is_empty (decl_namespace_list))
4508 return;
4510 /* This is the point to write out a PCH if we're doing that.
4511 In that case we do not want to do anything else. */
4512 if (pch_file)
4514 /* Mangle all symbols at PCH creation time. */
4515 symtab_node *node;
4516 FOR_EACH_SYMBOL (node)
4517 if (! is_a <varpool_node *> (node)
4518 || ! DECL_HARD_REGISTER (node->decl))
4519 DECL_ASSEMBLER_NAME (node->decl);
4520 c_common_write_pch ();
4521 dump_tu ();
4522 return;
4525 timevar_stop (TV_PHASE_PARSING);
4526 timevar_start (TV_PHASE_DEFERRED);
4528 symtab->process_same_body_aliases ();
4530 /* Handle -fdump-ada-spec[-slim] */
4531 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4533 if (flag_dump_ada_spec_slim)
4534 collect_source_ref (main_input_filename);
4535 else
4536 collect_source_refs (global_namespace);
4538 dump_ada_specs (collect_all_refs, cpp_check);
4541 /* FIXME - huh? was input_line -= 1;*/
4543 /* We now have to write out all the stuff we put off writing out.
4544 These include:
4546 o Template specializations that we have not yet instantiated,
4547 but which are needed.
4548 o Initialization and destruction for non-local objects with
4549 static storage duration. (Local objects with static storage
4550 duration are initialized when their scope is first entered,
4551 and are cleaned up via atexit.)
4552 o Virtual function tables.
4554 All of these may cause others to be needed. For example,
4555 instantiating one function may cause another to be needed, and
4556 generating the initializer for an object may cause templates to be
4557 instantiated, etc., etc. */
4559 emit_support_tinfos ();
4563 tree t;
4564 tree decl;
4566 reconsider = false;
4568 /* If there are templates that we've put off instantiating, do
4569 them now. */
4570 instantiate_pending_templates (retries);
4571 ggc_collect ();
4573 /* Write out virtual tables as required. Note that writing out
4574 the virtual table for a template class may cause the
4575 instantiation of members of that class. If we write out
4576 vtables then we remove the class from our list so we don't
4577 have to look at it again. */
4579 while (keyed_classes != NULL_TREE
4580 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4582 reconsider = true;
4583 keyed_classes = TREE_CHAIN (keyed_classes);
4586 t = keyed_classes;
4587 if (t != NULL_TREE)
4589 tree next = TREE_CHAIN (t);
4591 while (next)
4593 if (maybe_emit_vtables (TREE_VALUE (next)))
4595 reconsider = true;
4596 TREE_CHAIN (t) = TREE_CHAIN (next);
4598 else
4599 t = next;
4601 next = TREE_CHAIN (t);
4605 /* Write out needed type info variables. We have to be careful
4606 looping through unemitted decls, because emit_tinfo_decl may
4607 cause other variables to be needed. New elements will be
4608 appended, and we remove from the vector those that actually
4609 get emitted. */
4610 for (i = unemitted_tinfo_decls->length ();
4611 unemitted_tinfo_decls->iterate (--i, &t);)
4612 if (emit_tinfo_decl (t))
4614 reconsider = true;
4615 unemitted_tinfo_decls->unordered_remove (i);
4618 /* The list of objects with static storage duration is built up
4619 in reverse order. We clear STATIC_AGGREGATES so that any new
4620 aggregates added during the initialization of these will be
4621 initialized in the correct order when we next come around the
4622 loop. */
4623 vars = prune_vars_needing_no_initialization (&static_aggregates);
4625 if (vars)
4627 /* We need to start a new initialization function each time
4628 through the loop. That's because we need to know which
4629 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4630 isn't computed until a function is finished, and written
4631 out. That's a deficiency in the back end. When this is
4632 fixed, these initialization functions could all become
4633 inline, with resulting performance improvements. */
4634 tree ssdf_body;
4636 /* Set the line and file, so that it is obviously not from
4637 the source file. */
4638 input_location = locus_at_end_of_parsing;
4639 ssdf_body = start_static_storage_duration_function (ssdf_count);
4641 /* Make sure the back end knows about all the variables. */
4642 write_out_vars (vars);
4644 /* First generate code to do all the initializations. */
4645 if (vars)
4646 do_static_initialization_or_destruction (vars, /*initp=*/true);
4648 /* Then, generate code to do all the destructions. Do these
4649 in reverse order so that the most recently constructed
4650 variable is the first destroyed. If we're using
4651 __cxa_atexit, then we don't need to do this; functions
4652 were registered at initialization time to destroy the
4653 local statics. */
4654 if (!flag_use_cxa_atexit && vars)
4656 vars = nreverse (vars);
4657 do_static_initialization_or_destruction (vars, /*initp=*/false);
4659 else
4660 vars = NULL_TREE;
4662 /* Finish up the static storage duration function for this
4663 round. */
4664 input_location = locus_at_end_of_parsing;
4665 finish_static_storage_duration_function (ssdf_body);
4667 /* All those initializations and finalizations might cause
4668 us to need more inline functions, more template
4669 instantiations, etc. */
4670 reconsider = true;
4671 ssdf_count++;
4672 /* ??? was: locus_at_end_of_parsing.line++; */
4675 /* Now do the same for thread_local variables. */
4676 handle_tls_init ();
4678 /* Go through the set of inline functions whose bodies have not
4679 been emitted yet. If out-of-line copies of these functions
4680 are required, emit them. */
4681 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4683 /* Does it need synthesizing? */
4684 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4685 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4687 /* Even though we're already at the top-level, we push
4688 there again. That way, when we pop back a few lines
4689 hence, all of our state is restored. Otherwise,
4690 finish_function doesn't clean things up, and we end
4691 up with CURRENT_FUNCTION_DECL set. */
4692 push_to_top_level ();
4693 /* The decl's location will mark where it was first
4694 needed. Save that so synthesize method can indicate
4695 where it was needed from, in case of error */
4696 input_location = DECL_SOURCE_LOCATION (decl);
4697 synthesize_method (decl);
4698 pop_from_top_level ();
4699 reconsider = true;
4702 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4703 generate_tls_wrapper (decl);
4705 if (!DECL_SAVED_TREE (decl))
4706 continue;
4708 /* We lie to the back end, pretending that some functions
4709 are not defined when they really are. This keeps these
4710 functions from being put out unnecessarily. But, we must
4711 stop lying when the functions are referenced, or if they
4712 are not comdat since they need to be put out now. If
4713 DECL_INTERFACE_KNOWN, then we have already set
4714 DECL_EXTERNAL appropriately, so there's no need to check
4715 again, and we do not want to clear DECL_EXTERNAL if a
4716 previous call to import_export_decl set it.
4718 This is done in a separate for cycle, because if some
4719 deferred function is contained in another deferred
4720 function later in deferred_fns varray,
4721 rest_of_compilation would skip this function and we
4722 really cannot expand the same function twice. */
4723 import_export_decl (decl);
4724 if (DECL_NOT_REALLY_EXTERN (decl)
4725 && DECL_INITIAL (decl)
4726 && decl_needed_p (decl))
4728 struct cgraph_node *node, *next;
4730 node = cgraph_node::get (decl);
4731 if (node->cpp_implicit_alias)
4732 node = node->get_alias_target ();
4734 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4735 NULL, true);
4736 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4737 group, we need to mark all symbols in the same comdat group
4738 that way. */
4739 if (node->same_comdat_group)
4740 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4741 next != node;
4742 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4743 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4744 NULL, true);
4747 /* If we're going to need to write this function out, and
4748 there's already a body for it, create RTL for it now.
4749 (There might be no body if this is a method we haven't
4750 gotten around to synthesizing yet.) */
4751 if (!DECL_EXTERNAL (decl)
4752 && decl_needed_p (decl)
4753 && !TREE_ASM_WRITTEN (decl)
4754 && !cgraph_node::get (decl)->definition)
4756 /* We will output the function; no longer consider it in this
4757 loop. */
4758 DECL_DEFER_OUTPUT (decl) = 0;
4759 /* Generate RTL for this function now that we know we
4760 need it. */
4761 expand_or_defer_fn (decl);
4762 /* If we're compiling -fsyntax-only pretend that this
4763 function has been written out so that we don't try to
4764 expand it again. */
4765 if (flag_syntax_only)
4766 TREE_ASM_WRITTEN (decl) = 1;
4767 reconsider = true;
4771 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4772 reconsider = true;
4774 /* Static data members are just like namespace-scope globals. */
4775 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4777 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4778 /* Don't write it out if we haven't seen a definition. */
4779 || DECL_IN_AGGR_P (decl))
4780 continue;
4781 import_export_decl (decl);
4782 /* If this static data member is needed, provide it to the
4783 back end. */
4784 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4785 DECL_EXTERNAL (decl) = 0;
4787 if (vec_safe_length (pending_statics) != 0
4788 && wrapup_global_declarations (pending_statics->address (),
4789 pending_statics->length ()))
4790 reconsider = true;
4792 retries++;
4794 while (reconsider);
4796 generate_mangling_aliases ();
4798 /* All used inline functions must have a definition at this point. */
4799 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4801 if (/* Check online inline functions that were actually used. */
4802 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4803 /* If the definition actually was available here, then the
4804 fact that the function was not defined merely represents
4805 that for some reason (use of a template repository,
4806 #pragma interface, etc.) we decided not to emit the
4807 definition here. */
4808 && !DECL_INITIAL (decl)
4809 /* Don't complain if the template was defined. */
4810 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4811 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4812 (template_for_substitution (decl)))))
4814 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4815 "inline function %qD used but never defined", decl);
4816 /* Avoid a duplicate warning from check_global_declaration. */
4817 TREE_NO_WARNING (decl) = 1;
4821 /* So must decls that use a type with no linkage. */
4822 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4823 no_linkage_error (decl);
4825 maybe_warn_sized_delete ();
4827 /* Then, do the Objective-C stuff. This is where all the
4828 Objective-C module stuff gets generated (symtab,
4829 class/protocol/selector lists etc). This must be done after C++
4830 templates, destructors etc. so that selectors used in C++
4831 templates are properly allocated. */
4832 if (c_dialect_objc ())
4833 objc_write_global_declarations ();
4835 /* We give C linkage to static constructors and destructors. */
4836 push_lang_context (lang_name_c);
4838 /* Generate initialization and destruction functions for all
4839 priorities for which they are required. */
4840 if (priority_info_map)
4841 splay_tree_foreach (priority_info_map,
4842 generate_ctor_and_dtor_functions_for_priority,
4843 /*data=*/&locus_at_end_of_parsing);
4844 else if (c_dialect_objc () && objc_static_init_needed_p ())
4845 /* If this is obj-c++ and we need a static init, call
4846 generate_ctor_or_dtor_function. */
4847 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4848 DEFAULT_INIT_PRIORITY,
4849 &locus_at_end_of_parsing);
4851 /* We're done with the splay-tree now. */
4852 if (priority_info_map)
4853 splay_tree_delete (priority_info_map);
4855 /* Generate any missing aliases. */
4856 maybe_apply_pending_pragma_weaks ();
4858 /* We're done with static constructors, so we can go back to "C++"
4859 linkage now. */
4860 pop_lang_context ();
4862 /* Generate Java hidden aliases. */
4863 build_java_method_aliases ();
4865 if (flag_vtable_verify)
4867 vtv_recover_class_info ();
4868 vtv_compute_class_hierarchy_transitive_closure ();
4869 vtv_build_vtable_verify_fndecl ();
4872 perform_deferred_noexcept_checks ();
4874 finish_repo ();
4876 /* The entire file is now complete. If requested, dump everything
4877 to a file. */
4878 dump_tu ();
4880 if (flag_detailed_statistics)
4882 dump_tree_statistics ();
4883 dump_time_statistics ();
4886 timevar_stop (TV_PHASE_DEFERRED);
4887 timevar_start (TV_PHASE_PARSING);
4889 /* Indicate that we're done with front end processing. */
4890 at_eof = 2;
4893 /* Perform any post compilation-proper cleanups for the C++ front-end.
4894 This should really go away. No front-end should need to do
4895 anything past the compilation process. */
4897 void
4898 cxx_post_compilation_parsing_cleanups (void)
4900 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4902 if (flag_vtable_verify)
4904 /* Generate the special constructor initialization function that
4905 calls __VLTRegisterPairs, and give it a very high
4906 initialization priority. This must be done after
4907 finalize_compilation_unit so that we have accurate
4908 information about which vtable will actually be emitted. */
4909 vtv_generate_init_routine ();
4912 input_location = locus_at_end_of_parsing;
4914 #ifdef ENABLE_CHECKING
4915 validate_conversion_obstack ();
4916 #endif /* ENABLE_CHECKING */
4918 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4921 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4922 function to call in parse-tree form; it has not yet been
4923 semantically analyzed. ARGS are the arguments to the function.
4924 They have already been semantically analyzed. This may change
4925 ARGS. */
4927 tree
4928 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4929 tsubst_flags_t complain)
4931 tree orig_fn;
4932 vec<tree, va_gc> *orig_args = NULL;
4933 tree expr;
4934 tree object;
4936 orig_fn = fn;
4937 object = TREE_OPERAND (fn, 0);
4939 if (processing_template_decl)
4941 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4942 || TREE_CODE (fn) == MEMBER_REF);
4943 if (type_dependent_expression_p (fn)
4944 || any_type_dependent_arguments_p (*args))
4945 return build_nt_call_vec (fn, *args);
4947 orig_args = make_tree_vector_copy (*args);
4949 /* Transform the arguments and add the implicit "this"
4950 parameter. That must be done before the FN is transformed
4951 because we depend on the form of FN. */
4952 make_args_non_dependent (*args);
4953 object = build_non_dependent_expr (object);
4954 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4956 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4957 object = cp_build_addr_expr (object, complain);
4958 vec_safe_insert (*args, 0, object);
4960 /* Now that the arguments are done, transform FN. */
4961 fn = build_non_dependent_expr (fn);
4964 /* A qualified name corresponding to a bound pointer-to-member is
4965 represented as an OFFSET_REF:
4967 struct B { void g(); };
4968 void (B::*p)();
4969 void B::g() { (this->*p)(); } */
4970 if (TREE_CODE (fn) == OFFSET_REF)
4972 tree object_addr = cp_build_addr_expr (object, complain);
4973 fn = TREE_OPERAND (fn, 1);
4974 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4975 complain);
4976 vec_safe_insert (*args, 0, object_addr);
4979 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4980 expr = build_op_call (fn, args, complain);
4981 else
4982 expr = cp_build_function_call_vec (fn, args, complain);
4983 if (processing_template_decl && expr != error_mark_node)
4984 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4986 if (orig_args != NULL)
4987 release_tree_vector (orig_args);
4989 return expr;
4993 void
4994 check_default_args (tree x)
4996 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4997 bool saw_def = false;
4998 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4999 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5001 if (TREE_PURPOSE (arg))
5002 saw_def = true;
5003 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5005 error ("default argument missing for parameter %P of %q+#D", i, x);
5006 TREE_PURPOSE (arg) = error_mark_node;
5011 /* Return true if function DECL can be inlined. This is used to force
5012 instantiation of methods that might be interesting for inlining. */
5013 bool
5014 possibly_inlined_p (tree decl)
5016 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5017 if (DECL_UNINLINABLE (decl))
5018 return false;
5019 if (!optimize || pragma_java_exceptions)
5020 return DECL_DECLARED_INLINE_P (decl);
5021 /* When optimizing, we might inline everything when flatten
5022 attribute or heuristics inlining for size or autoinlining
5023 is used. */
5024 return true;
5027 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5028 If DECL is a specialization or implicitly declared class member,
5029 generate the actual definition. Return false if something goes
5030 wrong, true otherwise. */
5032 bool
5033 mark_used (tree decl, tsubst_flags_t complain)
5035 /* If DECL is a BASELINK for a single function, then treat it just
5036 like the DECL for the function. Otherwise, if the BASELINK is
5037 for an overloaded function, we don't know which function was
5038 actually used until after overload resolution. */
5039 if (BASELINK_P (decl))
5041 decl = BASELINK_FUNCTIONS (decl);
5042 if (really_overloaded_fn (decl))
5043 return true;
5044 decl = OVL_CURRENT (decl);
5047 /* Set TREE_USED for the benefit of -Wunused. */
5048 TREE_USED (decl) = 1;
5049 if (DECL_CLONED_FUNCTION_P (decl))
5050 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5052 /* Mark enumeration types as used. */
5053 if (TREE_CODE (decl) == CONST_DECL)
5054 used_types_insert (DECL_CONTEXT (decl));
5056 if (TREE_CODE (decl) == FUNCTION_DECL)
5057 maybe_instantiate_noexcept (decl);
5059 if (TREE_CODE (decl) == FUNCTION_DECL
5060 && DECL_DELETED_FN (decl))
5062 if (DECL_ARTIFICIAL (decl))
5064 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5065 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5067 /* We mark a lambda conversion op as deleted if we can't
5068 generate it properly; see maybe_add_lambda_conv_op. */
5069 sorry ("converting lambda which uses %<...%> to "
5070 "function pointer");
5071 return false;
5074 if (complain & tf_error)
5076 error ("use of deleted function %qD", decl);
5077 if (!maybe_explain_implicit_delete (decl))
5078 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5080 return false;
5083 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5084 && deprecated_state != DEPRECATED_SUPPRESS)
5085 warn_deprecated_use (decl, NULL_TREE);
5087 /* We can only check DECL_ODR_USED on variables or functions with
5088 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5089 might need special handling for. */
5090 if (!VAR_OR_FUNCTION_DECL_P (decl)
5091 || DECL_LANG_SPECIFIC (decl) == NULL
5092 || DECL_THUNK_P (decl))
5094 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
5096 if (complain & tf_error)
5097 error ("use of %qD before deduction of %<auto%>", decl);
5098 return false;
5100 return true;
5103 /* We only want to do this processing once. We don't need to keep trying
5104 to instantiate inline templates, because unit-at-a-time will make sure
5105 we get them compiled before functions that want to inline them. */
5106 if (DECL_ODR_USED (decl))
5107 return true;
5109 /* If within finish_function, defer the rest until that function
5110 finishes, otherwise it might recurse. */
5111 if (defer_mark_used_calls)
5113 vec_safe_push (deferred_mark_used_calls, decl);
5114 return true;
5117 /* Normally, we can wait until instantiation-time to synthesize DECL.
5118 However, if DECL is a static data member initialized with a constant
5119 or a constexpr function, we need it right now because a reference to
5120 such a data member or a call to such function is not value-dependent.
5121 For a function that uses auto in the return type, we need to instantiate
5122 it to find out its type. For OpenMP user defined reductions, we need
5123 them instantiated for reduction clauses which inline them by hand
5124 directly. */
5125 if (DECL_LANG_SPECIFIC (decl)
5126 && DECL_TEMPLATE_INFO (decl)
5127 && (decl_maybe_constant_var_p (decl)
5128 || (TREE_CODE (decl) == FUNCTION_DECL
5129 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5130 || undeduced_auto_decl (decl))
5131 && !DECL_DECLARED_CONCEPT_P (decl)
5132 && !uses_template_parms (DECL_TI_ARGS (decl)))
5134 /* Instantiating a function will result in garbage collection. We
5135 must treat this situation as if we were within the body of a
5136 function so as to avoid collecting live data only referenced from
5137 the stack (such as overload resolution candidates). */
5138 ++function_depth;
5139 instantiate_decl (decl, /*defer_ok=*/false,
5140 /*expl_inst_class_mem_p=*/false);
5141 --function_depth;
5144 if (processing_template_decl || in_template_function ())
5145 return true;
5147 /* Check this too in case we're within instantiate_non_dependent_expr. */
5148 if (DECL_TEMPLATE_INFO (decl)
5149 && uses_template_parms (DECL_TI_ARGS (decl)))
5150 return true;
5152 if (undeduced_auto_decl (decl))
5154 if (complain & tf_error)
5155 error ("use of %qD before deduction of %<auto%>", decl);
5156 return false;
5159 /* If we don't need a value, then we don't need to synthesize DECL. */
5160 if (cp_unevaluated_operand != 0)
5161 return true;
5163 DECL_ODR_USED (decl) = 1;
5164 if (DECL_CLONED_FUNCTION_P (decl))
5165 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5167 /* DR 757: A type without linkage shall not be used as the type of a
5168 variable or function with linkage, unless
5169 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5170 o the variable or function is not used (3.2 [basic.def.odr]) or is
5171 defined in the same translation unit. */
5172 if (cxx_dialect > cxx98
5173 && decl_linkage (decl) != lk_none
5174 && !DECL_EXTERN_C_P (decl)
5175 && !DECL_ARTIFICIAL (decl)
5176 && !decl_defined_p (decl)
5177 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5179 if (is_local_extern (decl))
5180 /* There's no way to define a local extern, and adding it to
5181 the vector interferes with GC, so give an error now. */
5182 no_linkage_error (decl);
5183 else
5184 vec_safe_push (no_linkage_decls, decl);
5187 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5188 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5189 /* Remember it, so we can check it was defined. */
5190 note_vague_linkage_fn (decl);
5192 /* Is it a synthesized method that needs to be synthesized? */
5193 if (TREE_CODE (decl) == FUNCTION_DECL
5194 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5195 && DECL_DEFAULTED_FN (decl)
5196 /* A function defaulted outside the class is synthesized either by
5197 cp_finish_decl or instantiate_decl. */
5198 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5199 && ! DECL_INITIAL (decl))
5201 /* Defer virtual destructors so that thunks get the right
5202 linkage. */
5203 if (DECL_VIRTUAL_P (decl) && !at_eof)
5205 note_vague_linkage_fn (decl);
5206 return true;
5209 /* Remember the current location for a function we will end up
5210 synthesizing. Then we can inform the user where it was
5211 required in the case of error. */
5212 DECL_SOURCE_LOCATION (decl) = input_location;
5214 /* Synthesizing an implicitly defined member function will result in
5215 garbage collection. We must treat this situation as if we were
5216 within the body of a function so as to avoid collecting live data
5217 on the stack (such as overload resolution candidates).
5219 We could just let cp_write_global_declarations handle synthesizing
5220 this function by adding it to deferred_fns, but doing
5221 it at the use site produces better error messages. */
5222 ++function_depth;
5223 synthesize_method (decl);
5224 --function_depth;
5225 /* If this is a synthesized method we don't need to
5226 do the instantiation test below. */
5228 else if (VAR_OR_FUNCTION_DECL_P (decl)
5229 && DECL_TEMPLATE_INFO (decl)
5230 && !DECL_DECLARED_CONCEPT_P (decl)
5231 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5232 || always_instantiate_p (decl)))
5233 /* If this is a function or variable that is an instance of some
5234 template, we now know that we will need to actually do the
5235 instantiation. We check that DECL is not an explicit
5236 instantiation because that is not checked in instantiate_decl.
5238 We put off instantiating functions in order to improve compile
5239 times. Maintaining a stack of active functions is expensive,
5240 and the inliner knows to instantiate any functions it might
5241 need. Therefore, we always try to defer instantiation. */
5243 ++function_depth;
5244 instantiate_decl (decl, /*defer_ok=*/true,
5245 /*expl_inst_class_mem_p=*/false);
5246 --function_depth;
5249 return true;
5252 bool
5253 mark_used (tree decl)
5255 return mark_used (decl, tf_warning_or_error);
5258 tree
5259 vtv_start_verification_constructor_init_function (void)
5261 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5264 tree
5265 vtv_finish_verification_constructor_init_function (tree function_body)
5267 tree fn;
5269 finish_compound_stmt (function_body);
5270 fn = finish_function (0);
5271 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5272 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5274 return fn;
5277 #include "gt-cp-decl2.h"