Remove assert in get_def_bb_for_const
[official-gcc.git] / gcc / cp / decl2.c
blob22f9eded29185a5b932b1d9f8c2415582df68c85
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2016 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 "target.h"
33 #include "cp-tree.h"
34 #include "c-family/c-common.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "varasm.h"
39 #include "attribs.h"
40 #include "stor-layout.h"
41 #include "calls.h"
42 #include "decl.h"
43 #include "toplev.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-pragma.h"
46 #include "dumpfile.h"
47 #include "intl.h"
48 #include "c-family/c-ada-spec.h"
49 #include "asan.h"
51 extern cpp_reader *parse_in;
53 /* This structure contains information about the initializations
54 and/or destructions required for a particular priority level. */
55 typedef struct priority_info_s {
56 /* Nonzero if there have been any initializations at this priority
57 throughout the translation unit. */
58 int initializations_p;
59 /* Nonzero if there have been any destructions at this priority
60 throughout the translation unit. */
61 int destructions_p;
62 } *priority_info;
64 static void mark_vtable_entries (tree);
65 static bool maybe_emit_vtables (tree);
66 static bool acceptable_java_type (tree);
67 static tree start_objects (int, int);
68 static void finish_objects (int, int, tree);
69 static tree start_static_storage_duration_function (unsigned);
70 static void finish_static_storage_duration_function (tree);
71 static priority_info get_priority_info (int);
72 static void do_static_initialization_or_destruction (tree, bool);
73 static void one_static_initialization_or_destruction (tree, tree, bool);
74 static void generate_ctor_or_dtor_function (bool, int, location_t *);
75 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
76 void *);
77 static tree prune_vars_needing_no_initialization (tree *);
78 static void write_out_vars (tree);
79 static void import_export_class (tree);
80 static tree get_guard_bits (tree);
81 static void determine_visibility_from_class (tree, tree);
82 static bool determine_hidden_inline (tree);
83 static bool decl_defined_p (tree);
85 /* A list of static class variables. This is needed, because a
86 static class variable can be declared inside the class without
87 an initializer, and then initialized, statically, outside the class. */
88 static GTY(()) vec<tree, va_gc> *pending_statics;
90 /* A list of functions which were declared inline, but which we
91 may need to emit outline anyway. */
92 static GTY(()) vec<tree, va_gc> *deferred_fns;
94 /* A list of decls that use types with no linkage, which we need to make
95 sure are defined. */
96 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
98 /* A vector of alternating decls and identifiers, where the latter
99 is to be an alias for the former if the former is defined. */
100 static GTY(()) vec<tree, va_gc> *mangling_aliases;
102 /* Nonzero if we're done parsing and into end-of-file activities. */
104 int at_eof;
106 /* True if note_mangling_alias should enqueue mangling aliases for
107 later generation, rather than emitting them right away. */
109 bool defer_mangling_aliases = true;
112 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
113 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
114 that apply to the function). */
116 tree
117 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
118 cp_ref_qualifier rqual)
120 tree raises;
121 tree attrs;
122 int type_quals;
123 bool late_return_type_p;
125 if (fntype == error_mark_node || ctype == error_mark_node)
126 return error_mark_node;
128 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
129 || TREE_CODE (fntype) == METHOD_TYPE);
131 type_quals = quals & ~TYPE_QUAL_RESTRICT;
132 ctype = cp_build_qualified_type (ctype, type_quals);
133 raises = TYPE_RAISES_EXCEPTIONS (fntype);
134 attrs = TYPE_ATTRIBUTES (fntype);
135 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
136 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
137 (TREE_CODE (fntype) == METHOD_TYPE
138 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
139 : TYPE_ARG_TYPES (fntype)));
140 if (attrs)
141 fntype = cp_build_type_attribute_variant (fntype, attrs);
142 if (rqual)
143 fntype = build_ref_qualified_type (fntype, rqual);
144 if (raises)
145 fntype = build_exception_variant (fntype, raises);
146 if (late_return_type_p)
147 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
149 return fntype;
152 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
153 return type changed to NEW_RET. */
155 tree
156 change_return_type (tree new_ret, tree fntype)
158 tree newtype;
159 tree args = TYPE_ARG_TYPES (fntype);
160 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
161 tree attrs = TYPE_ATTRIBUTES (fntype);
162 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
164 if (new_ret == error_mark_node)
165 return fntype;
167 if (same_type_p (new_ret, TREE_TYPE (fntype)))
168 return fntype;
170 if (TREE_CODE (fntype) == FUNCTION_TYPE)
172 newtype = build_function_type (new_ret, args);
173 newtype = apply_memfn_quals (newtype,
174 type_memfn_quals (fntype),
175 type_memfn_rqual (fntype));
177 else
178 newtype = build_method_type_directly
179 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
180 if (FUNCTION_REF_QUALIFIED (fntype))
181 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
182 if (raises)
183 newtype = build_exception_variant (newtype, raises);
184 if (attrs)
185 newtype = cp_build_type_attribute_variant (newtype, attrs);
186 if (late_return_type_p)
187 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
189 return newtype;
192 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
193 appropriately. */
195 tree
196 cp_build_parm_decl (tree name, tree type)
198 tree parm = build_decl (input_location,
199 PARM_DECL, name, type);
200 /* DECL_ARG_TYPE is only used by the back end and the back end never
201 sees templates. */
202 if (!processing_template_decl)
203 DECL_ARG_TYPE (parm) = type_passed_as (type);
205 return parm;
208 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
209 indicated NAME. */
211 tree
212 build_artificial_parm (tree name, tree type)
214 tree parm = cp_build_parm_decl (name, type);
215 DECL_ARTIFICIAL (parm) = 1;
216 /* All our artificial parms are implicitly `const'; they cannot be
217 assigned to. */
218 TREE_READONLY (parm) = 1;
219 return parm;
222 /* Constructors for types with virtual baseclasses need an "in-charge" flag
223 saying whether this constructor is responsible for initialization of
224 virtual baseclasses or not. All destructors also need this "in-charge"
225 flag, which additionally determines whether or not the destructor should
226 free the memory for the object.
228 This function adds the "in-charge" flag to member function FN if
229 appropriate. It is called from grokclassfn and tsubst.
230 FN must be either a constructor or destructor.
232 The in-charge flag follows the 'this' parameter, and is followed by the
233 VTT parm (if any), then the user-written parms. */
235 void
236 maybe_retrofit_in_chrg (tree fn)
238 tree basetype, arg_types, parms, parm, fntype;
240 /* If we've already add the in-charge parameter don't do it again. */
241 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
242 return;
244 /* When processing templates we can't know, in general, whether or
245 not we're going to have virtual baseclasses. */
246 if (processing_template_decl)
247 return;
249 /* We don't need an in-charge parameter for constructors that don't
250 have virtual bases. */
251 if (DECL_CONSTRUCTOR_P (fn)
252 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
253 return;
255 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
256 basetype = TREE_TYPE (TREE_VALUE (arg_types));
257 arg_types = TREE_CHAIN (arg_types);
259 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
261 /* If this is a subobject constructor or destructor, our caller will
262 pass us a pointer to our VTT. */
263 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
265 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
267 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
268 DECL_CHAIN (parm) = parms;
269 parms = parm;
271 /* ...and then to TYPE_ARG_TYPES. */
272 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
274 DECL_HAS_VTT_PARM_P (fn) = 1;
277 /* Then add the in-charge parm (before the VTT parm). */
278 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
279 DECL_CHAIN (parm) = parms;
280 parms = parm;
281 arg_types = hash_tree_chain (integer_type_node, arg_types);
283 /* Insert our new parameter(s) into the list. */
284 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
286 /* And rebuild the function type. */
287 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
288 arg_types);
289 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
290 fntype = build_exception_variant (fntype,
291 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
292 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
293 fntype = (cp_build_type_attribute_variant
294 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
295 TREE_TYPE (fn) = fntype;
297 /* Now we've got the in-charge parameter. */
298 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
301 /* Classes overload their constituent function names automatically.
302 When a function name is declared in a record structure,
303 its name is changed to it overloaded name. Since names for
304 constructors and destructors can conflict, we place a leading
305 '$' for destructors.
307 CNAME is the name of the class we are grokking for.
309 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
311 FLAGS contains bits saying what's special about today's
312 arguments. DTOR_FLAG == DESTRUCTOR.
314 If FUNCTION is a destructor, then we must add the `auto-delete' field
315 as a second parameter. There is some hair associated with the fact
316 that we must "declare" this variable in the manner consistent with the
317 way the rest of the arguments were declared.
319 QUALS are the qualifiers for the this pointer. */
321 void
322 grokclassfn (tree ctype, tree function, enum overload_flags flags)
324 tree fn_name = DECL_NAME (function);
326 /* Even within an `extern "C"' block, members get C++ linkage. See
327 [dcl.link] for details. */
328 SET_DECL_LANGUAGE (function, lang_cplusplus);
330 if (fn_name == NULL_TREE)
332 error ("name missing for member function");
333 fn_name = get_identifier ("<anonymous>");
334 DECL_NAME (function) = fn_name;
337 DECL_CONTEXT (function) = ctype;
339 if (flags == DTOR_FLAG)
340 DECL_DESTRUCTOR_P (function) = 1;
342 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
343 maybe_retrofit_in_chrg (function);
346 /* Create an ARRAY_REF, checking for the user doing things backwards
347 along the way. DECLTYPE_P is for N3276, as in the parser. */
349 tree
350 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
351 bool decltype_p)
353 tree type;
354 tree expr;
355 tree orig_array_expr = array_expr;
356 tree orig_index_exp = index_exp;
357 tree overload = NULL_TREE;
359 if (error_operand_p (array_expr) || error_operand_p (index_exp))
360 return error_mark_node;
362 if (processing_template_decl)
364 if (type_dependent_expression_p (array_expr)
365 || type_dependent_expression_p (index_exp))
366 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
367 NULL_TREE, NULL_TREE);
368 array_expr = build_non_dependent_expr (array_expr);
369 index_exp = build_non_dependent_expr (index_exp);
372 type = TREE_TYPE (array_expr);
373 gcc_assert (type);
374 type = non_reference (type);
376 /* If they have an `operator[]', use that. */
377 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
379 tsubst_flags_t complain = tf_warning_or_error;
380 if (decltype_p)
381 complain |= tf_decltype;
382 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
383 index_exp, NULL_TREE, &overload, complain);
385 else
387 tree p1, p2, i1, i2;
389 /* Otherwise, create an ARRAY_REF for a pointer or array type.
390 It is a little-known fact that, if `a' is an array and `i' is
391 an int, you can write `i[a]', which means the same thing as
392 `a[i]'. */
393 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
394 p1 = array_expr;
395 else
396 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
398 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
399 p2 = index_exp;
400 else
401 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
403 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
404 false);
405 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
406 false);
408 if ((p1 && i2) && (i1 && p2))
409 error ("ambiguous conversion for array subscript");
411 if (p1 && i2)
412 array_expr = p1, index_exp = i2;
413 else if (i1 && p2)
414 array_expr = p2, index_exp = i1;
415 else
417 error ("invalid types %<%T[%T]%> for array subscript",
418 type, TREE_TYPE (index_exp));
419 return error_mark_node;
422 if (array_expr == error_mark_node || index_exp == error_mark_node)
423 error ("ambiguous conversion for array subscript");
425 expr = build_array_ref (input_location, array_expr, index_exp);
427 if (processing_template_decl && expr != error_mark_node)
429 if (overload != NULL_TREE)
430 return (build_min_non_dep_op_overload
431 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
433 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
434 NULL_TREE, NULL_TREE);
436 return expr;
439 /* Given the cast expression EXP, checking out its validity. Either return
440 an error_mark_node if there was an unavoidable error, return a cast to
441 void for trying to delete a pointer w/ the value 0, or return the
442 call to delete. If DOING_VEC is true, we handle things differently
443 for doing an array delete.
444 Implements ARM $5.3.4. This is called from the parser. */
446 tree
447 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
448 tsubst_flags_t complain)
450 tree t, type;
452 if (exp == error_mark_node)
453 return exp;
455 if (processing_template_decl)
457 t = build_min (DELETE_EXPR, void_type_node, exp, size);
458 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
459 DELETE_EXPR_USE_VEC (t) = doing_vec;
460 TREE_SIDE_EFFECTS (t) = 1;
461 return t;
464 /* An array can't have been allocated by new, so complain. */
465 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
466 warning (0, "deleting array %q#E", exp);
468 t = build_expr_type_conversion (WANT_POINTER, exp, true);
470 if (t == NULL_TREE || t == error_mark_node)
472 error ("type %q#T argument given to %<delete%>, expected pointer",
473 TREE_TYPE (exp));
474 return error_mark_node;
477 type = TREE_TYPE (t);
479 /* As of Valley Forge, you can delete a pointer to const. */
481 /* You can't delete functions. */
482 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
484 error ("cannot delete a function. Only pointer-to-objects are "
485 "valid arguments to %<delete%>");
486 return error_mark_node;
489 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
490 if (VOID_TYPE_P (TREE_TYPE (type)))
492 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
493 doing_vec = 0;
496 /* Deleting a pointer with the value zero is valid and has no effect. */
497 if (integer_zerop (t))
498 return build1 (NOP_EXPR, void_type_node, t);
500 if (doing_vec)
501 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
502 sfk_deleting_destructor,
503 use_global_delete, complain);
504 else
505 return build_delete (type, t, sfk_deleting_destructor,
506 LOOKUP_NORMAL, use_global_delete,
507 complain);
510 /* Report an error if the indicated template declaration is not the
511 sort of thing that should be a member template. */
513 void
514 check_member_template (tree tmpl)
516 tree decl;
518 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
519 decl = DECL_TEMPLATE_RESULT (tmpl);
521 if (TREE_CODE (decl) == FUNCTION_DECL
522 || DECL_ALIAS_TEMPLATE_P (tmpl)
523 || (TREE_CODE (decl) == TYPE_DECL
524 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
526 /* The parser rejects template declarations in local classes
527 (with the exception of generic lambdas). */
528 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
529 /* The parser rejects any use of virtual in a function template. */
530 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
531 && DECL_VIRTUAL_P (decl)));
533 /* The debug-information generating code doesn't know what to do
534 with member templates. */
535 DECL_IGNORED_P (tmpl) = 1;
537 else if (variable_template_p (tmpl))
538 /* OK */;
539 else
540 error ("template declaration of %q#D", decl);
543 /* Return true iff TYPE is a valid Java parameter or return type. */
545 static bool
546 acceptable_java_type (tree type)
548 if (type == error_mark_node)
549 return false;
551 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
552 return true;
553 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
555 type = TREE_TYPE (type);
556 if (TREE_CODE (type) == RECORD_TYPE)
558 tree args; int i;
559 if (! TYPE_FOR_JAVA (type))
560 return false;
561 if (! CLASSTYPE_TEMPLATE_INFO (type))
562 return true;
563 args = CLASSTYPE_TI_ARGS (type);
564 i = TREE_VEC_LENGTH (args);
565 while (--i >= 0)
567 type = TREE_VEC_ELT (args, i);
568 if (TYPE_PTR_P (type))
569 type = TREE_TYPE (type);
570 if (! TYPE_FOR_JAVA (type))
571 return false;
573 return true;
576 return false;
579 /* For a METHOD in a Java class CTYPE, return true if
580 the parameter and return types are valid Java types.
581 Otherwise, print appropriate error messages, and return false. */
583 bool
584 check_java_method (tree method)
586 bool jerr = false;
587 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
588 tree ret_type = TREE_TYPE (TREE_TYPE (method));
590 if (!acceptable_java_type (ret_type))
592 error ("Java method %qD has non-Java return type %qT",
593 method, ret_type);
594 jerr = true;
597 arg_types = TREE_CHAIN (arg_types);
598 if (DECL_HAS_IN_CHARGE_PARM_P (method))
599 arg_types = TREE_CHAIN (arg_types);
600 if (DECL_HAS_VTT_PARM_P (method))
601 arg_types = TREE_CHAIN (arg_types);
603 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
605 tree type = TREE_VALUE (arg_types);
606 if (!acceptable_java_type (type))
608 if (type != error_mark_node)
609 error ("Java method %qD has non-Java parameter type %qT",
610 method, type);
611 jerr = true;
614 return !jerr;
617 /* Sanity check: report error if this function FUNCTION is not
618 really a member of the class (CTYPE) it is supposed to belong to.
619 TEMPLATE_PARMS is used to specify the template parameters of a member
620 template passed as FUNCTION_DECL. If the member template is passed as a
621 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
622 from the declaration. If the function is not a function template, it
623 must be NULL.
624 It returns the original declaration for the function, NULL_TREE if
625 no declaration was found, error_mark_node if an error was emitted. */
627 tree
628 check_classfn (tree ctype, tree function, tree template_parms)
630 int ix;
631 bool is_template;
632 tree pushed_scope;
634 if (DECL_USE_TEMPLATE (function)
635 && !(TREE_CODE (function) == TEMPLATE_DECL
636 && DECL_TEMPLATE_SPECIALIZATION (function))
637 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
638 /* Since this is a specialization of a member template,
639 we're not going to find the declaration in the class.
640 For example, in:
642 struct S { template <typename T> void f(T); };
643 template <> void S::f(int);
645 we're not going to find `S::f(int)', but there's no
646 reason we should, either. We let our callers know we didn't
647 find the method, but we don't complain. */
648 return NULL_TREE;
650 /* Basic sanity check: for a template function, the template parameters
651 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
652 if (TREE_CODE (function) == TEMPLATE_DECL)
654 if (template_parms
655 && !comp_template_parms (template_parms,
656 DECL_TEMPLATE_PARMS (function)))
658 error ("template parameter lists provided don%'t match the "
659 "template parameters of %qD", function);
660 return error_mark_node;
662 template_parms = DECL_TEMPLATE_PARMS (function);
665 /* OK, is this a definition of a member template? */
666 is_template = (template_parms != NULL_TREE);
668 /* [temp.mem]
670 A destructor shall not be a member template. */
671 if (DECL_DESTRUCTOR_P (function) && is_template)
673 error ("destructor %qD declared as member template", function);
674 return error_mark_node;
677 /* We must enter the scope here, because conversion operators are
678 named by target type, and type equivalence relies on typenames
679 resolving within the scope of CTYPE. */
680 pushed_scope = push_scope (ctype);
681 ix = class_method_index_for_fn (complete_type (ctype), function);
682 if (ix >= 0)
684 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
685 tree fndecls, fndecl = 0;
686 bool is_conv_op;
687 const char *format = NULL;
689 for (fndecls = (*methods)[ix];
690 fndecls; fndecls = OVL_NEXT (fndecls))
692 tree p1, p2;
694 fndecl = OVL_CURRENT (fndecls);
695 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
696 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
698 /* We cannot simply call decls_match because this doesn't
699 work for static member functions that are pretending to
700 be methods, and because the name may have been changed by
701 asm("new_name"). */
703 /* Get rid of the this parameter on functions that become
704 static. */
705 if (DECL_STATIC_FUNCTION_P (fndecl)
706 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
707 p1 = TREE_CHAIN (p1);
709 /* A member template definition only matches a member template
710 declaration. */
711 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
712 continue;
714 /* ref-qualifier or absence of same must match. */
715 if (type_memfn_rqual (TREE_TYPE (function))
716 != type_memfn_rqual (TREE_TYPE (fndecl)))
717 continue;
719 // Include constraints in the match.
720 tree c1 = get_constraints (function);
721 tree c2 = get_constraints (fndecl);
723 /* While finding a match, same types and params are not enough
724 if the function is versioned. Also check version ("target")
725 attributes. */
726 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
727 TREE_TYPE (TREE_TYPE (fndecl)))
728 && compparms (p1, p2)
729 && !targetm.target_option.function_versions (function, fndecl)
730 && (!is_template
731 || comp_template_parms (template_parms,
732 DECL_TEMPLATE_PARMS (fndecl)))
733 && equivalent_constraints (c1, c2)
734 && (DECL_TEMPLATE_SPECIALIZATION (function)
735 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
736 && (!DECL_TEMPLATE_SPECIALIZATION (function)
737 || (DECL_TI_TEMPLATE (function)
738 == DECL_TI_TEMPLATE (fndecl))))
739 break;
741 if (fndecls)
743 if (pushed_scope)
744 pop_scope (pushed_scope);
745 return OVL_CURRENT (fndecls);
748 error_at (DECL_SOURCE_LOCATION (function),
749 "prototype for %q#D does not match any in class %qT",
750 function, ctype);
751 is_conv_op = DECL_CONV_FN_P (fndecl);
753 if (is_conv_op)
754 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
755 fndecls = (*methods)[ix];
756 while (fndecls)
758 fndecl = OVL_CURRENT (fndecls);
759 fndecls = OVL_NEXT (fndecls);
761 if (!fndecls && is_conv_op)
763 if (methods->length () > (size_t) ++ix)
765 fndecls = (*methods)[ix];
766 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
768 fndecls = NULL_TREE;
769 is_conv_op = false;
772 else
773 is_conv_op = false;
775 if (format)
776 format = " %+#D";
777 else if (fndecls)
778 format = N_("candidates are: %+#D");
779 else
780 format = N_("candidate is: %+#D");
781 error (format, fndecl);
784 else if (!COMPLETE_TYPE_P (ctype))
785 cxx_incomplete_type_error (function, ctype);
786 else
787 error ("no %q#D member function declared in class %qT",
788 function, ctype);
790 if (pushed_scope)
791 pop_scope (pushed_scope);
792 return error_mark_node;
795 /* DECL is a function with vague linkage. Remember it so that at the
796 end of the translation unit we can decide whether or not to emit
797 it. */
799 void
800 note_vague_linkage_fn (tree decl)
802 DECL_DEFER_OUTPUT (decl) = 1;
803 vec_safe_push (deferred_fns, decl);
806 /* As above, but for variable template instantiations. */
808 void
809 note_variable_template_instantiation (tree decl)
811 vec_safe_push (pending_statics, decl);
814 /* We have just processed the DECL, which is a static data member.
815 The other parameters are as for cp_finish_decl. */
817 void
818 finish_static_data_member_decl (tree decl,
819 tree init, bool init_const_expr_p,
820 tree asmspec_tree,
821 int flags)
823 DECL_CONTEXT (decl) = current_class_type;
825 /* We cannot call pushdecl here, because that would fill in the
826 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
827 the right thing, namely, to put this decl out straight away. */
829 if (! processing_template_decl)
830 vec_safe_push (pending_statics, decl);
832 if (LOCAL_CLASS_P (current_class_type)
833 /* We already complained about the template definition. */
834 && !DECL_TEMPLATE_INSTANTIATION (decl))
835 permerror (input_location, "local class %q#T shall not have static data member %q#D",
836 current_class_type, decl);
837 else
838 for (tree t = current_class_type; TYPE_P (t);
839 t = CP_TYPE_CONTEXT (t))
840 if (TYPE_ANONYMOUS_P (t))
842 if (permerror (DECL_SOURCE_LOCATION (decl),
843 "static data member %qD in unnamed class", decl))
844 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
845 "unnamed class defined here");
846 break;
849 DECL_IN_AGGR_P (decl) = 1;
851 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
852 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
853 SET_VAR_HAD_UNKNOWN_BOUND (decl);
855 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
858 /* DECLARATOR and DECLSPECS correspond to a class member. The other
859 parameters are as for cp_finish_decl. Return the DECL for the
860 class member declared. */
862 tree
863 grokfield (const cp_declarator *declarator,
864 cp_decl_specifier_seq *declspecs,
865 tree init, bool init_const_expr_p,
866 tree asmspec_tree,
867 tree attrlist)
869 tree value;
870 const char *asmspec = 0;
871 int flags;
872 tree name;
874 if (init
875 && TREE_CODE (init) == TREE_LIST
876 && TREE_VALUE (init) == error_mark_node
877 && TREE_CHAIN (init) == NULL_TREE)
878 init = NULL_TREE;
880 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
881 if (! value || value == error_mark_node)
882 /* friend or constructor went bad. */
883 return error_mark_node;
884 if (TREE_TYPE (value) == error_mark_node)
885 return value;
887 if (TREE_CODE (value) == TYPE_DECL && init)
889 error ("typedef %qD is initialized (use decltype instead)", value);
890 init = NULL_TREE;
893 /* Pass friendly classes back. */
894 if (value == void_type_node)
895 return value;
898 name = DECL_NAME (value);
900 if (name != NULL_TREE)
902 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
904 error ("explicit template argument list not allowed");
905 return error_mark_node;
908 if (IDENTIFIER_POINTER (name)[0] == '_'
909 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
910 error ("member %qD conflicts with virtual function table field name",
911 value);
914 /* Stash away type declarations. */
915 if (TREE_CODE (value) == TYPE_DECL)
917 DECL_NONLOCAL (value) = 1;
918 DECL_CONTEXT (value) = current_class_type;
920 if (attrlist)
922 int attrflags = 0;
924 /* If this is a typedef that names the class for linkage purposes
925 (7.1.3p8), apply any attributes directly to the type. */
926 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
927 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
928 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
930 cplus_decl_attributes (&value, attrlist, attrflags);
933 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
934 && TREE_TYPE (value) != error_mark_node
935 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
936 set_underlying_type (value);
938 /* It's important that push_template_decl below follows
939 set_underlying_type above so that the created template
940 carries the properly set type of VALUE. */
941 if (processing_template_decl)
942 value = push_template_decl (value);
944 record_locally_defined_typedef (value);
945 return value;
948 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
950 if (!friendp && DECL_IN_AGGR_P (value))
952 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
953 return void_type_node;
956 if (asmspec_tree && asmspec_tree != error_mark_node)
957 asmspec = TREE_STRING_POINTER (asmspec_tree);
959 if (init)
961 if (TREE_CODE (value) == FUNCTION_DECL)
963 if (init == ridpointers[(int)RID_DELETE])
965 DECL_DELETED_FN (value) = 1;
966 DECL_DECLARED_INLINE_P (value) = 1;
967 DECL_INITIAL (value) = error_mark_node;
969 else if (init == ridpointers[(int)RID_DEFAULT])
971 if (defaultable_fn_check (value))
973 DECL_DEFAULTED_FN (value) = 1;
974 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
975 DECL_DECLARED_INLINE_P (value) = 1;
978 else if (TREE_CODE (init) == DEFAULT_ARG)
979 error ("invalid initializer for member function %qD", value);
980 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
982 if (integer_zerop (init))
983 DECL_PURE_VIRTUAL_P (value) = 1;
984 else if (error_operand_p (init))
985 ; /* An error has already been reported. */
986 else
987 error ("invalid initializer for member function %qD",
988 value);
990 else
992 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
993 if (friendp)
994 error ("initializer specified for friend function %qD",
995 value);
996 else
997 error ("initializer specified for static member function %qD",
998 value);
1001 else if (TREE_CODE (value) == FIELD_DECL)
1002 /* C++11 NSDMI, keep going. */;
1003 else if (!VAR_P (value))
1004 gcc_unreachable ();
1007 /* Pass friend decls back. */
1008 if ((TREE_CODE (value) == FUNCTION_DECL
1009 || TREE_CODE (value) == TEMPLATE_DECL)
1010 && DECL_CONTEXT (value) != current_class_type)
1011 return value;
1013 /* Need to set this before push_template_decl. */
1014 if (VAR_P (value))
1015 DECL_CONTEXT (value) = current_class_type;
1017 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1019 value = push_template_decl (value);
1020 if (error_operand_p (value))
1021 return error_mark_node;
1024 if (attrlist)
1025 cplus_decl_attributes (&value, attrlist, 0);
1027 if (init && DIRECT_LIST_INIT_P (init))
1028 flags = LOOKUP_NORMAL;
1029 else
1030 flags = LOOKUP_IMPLICIT;
1032 switch (TREE_CODE (value))
1034 case VAR_DECL:
1035 finish_static_data_member_decl (value, init, init_const_expr_p,
1036 asmspec_tree, flags);
1037 return value;
1039 case FIELD_DECL:
1040 if (asmspec)
1041 error ("%<asm%> specifiers are not permitted on non-static data members");
1042 if (DECL_INITIAL (value) == error_mark_node)
1043 init = error_mark_node;
1044 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1045 NULL_TREE, flags);
1046 DECL_IN_AGGR_P (value) = 1;
1047 return value;
1049 case FUNCTION_DECL:
1050 if (asmspec)
1051 set_user_assembler_name (value, asmspec);
1053 cp_finish_decl (value,
1054 /*init=*/NULL_TREE,
1055 /*init_const_expr_p=*/false,
1056 asmspec_tree, flags);
1058 /* Pass friends back this way. */
1059 if (DECL_FRIEND_P (value))
1060 return void_type_node;
1062 DECL_IN_AGGR_P (value) = 1;
1063 return value;
1065 default:
1066 gcc_unreachable ();
1068 return NULL_TREE;
1071 /* Like `grokfield', but for bitfields.
1072 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1074 tree
1075 grokbitfield (const cp_declarator *declarator,
1076 cp_decl_specifier_seq *declspecs, tree width,
1077 tree attrlist)
1079 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1081 if (value == error_mark_node)
1082 return NULL_TREE; /* friends went bad. */
1083 if (TREE_TYPE (value) == error_mark_node)
1084 return value;
1086 /* Pass friendly classes back. */
1087 if (VOID_TYPE_P (value))
1088 return void_type_node;
1090 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1091 && (POINTER_TYPE_P (value)
1092 || !dependent_type_p (TREE_TYPE (value))))
1094 error ("bit-field %qD with non-integral type", value);
1095 return error_mark_node;
1098 if (TREE_CODE (value) == TYPE_DECL)
1100 error ("cannot declare %qD to be a bit-field type", value);
1101 return NULL_TREE;
1104 /* Usually, finish_struct_1 catches bitfields with invalid types.
1105 But, in the case of bitfields with function type, we confuse
1106 ourselves into thinking they are member functions, so we must
1107 check here. */
1108 if (TREE_CODE (value) == FUNCTION_DECL)
1110 error ("cannot declare bit-field %qD with function type",
1111 DECL_NAME (value));
1112 return NULL_TREE;
1115 if (DECL_IN_AGGR_P (value))
1117 error ("%qD is already defined in the class %qT", value,
1118 DECL_CONTEXT (value));
1119 return void_type_node;
1122 if (TREE_STATIC (value))
1124 error ("static member %qD cannot be a bit-field", value);
1125 return NULL_TREE;
1127 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1129 if (width != error_mark_node)
1131 /* The width must be an integer type. */
1132 if (!type_dependent_expression_p (width)
1133 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1134 error ("width of bit-field %qD has non-integral type %qT", value,
1135 TREE_TYPE (width));
1136 DECL_INITIAL (value) = width;
1137 SET_DECL_C_BIT_FIELD (value);
1140 DECL_IN_AGGR_P (value) = 1;
1142 if (attrlist)
1143 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1145 return value;
1149 /* Returns true iff ATTR is an attribute which needs to be applied at
1150 instantiation time rather than template definition time. */
1152 static bool
1153 is_late_template_attribute (tree attr, tree decl)
1155 tree name = get_attribute_name (attr);
1156 tree args = TREE_VALUE (attr);
1157 const struct attribute_spec *spec = lookup_attribute_spec (name);
1158 tree arg;
1160 if (!spec)
1161 /* Unknown attribute. */
1162 return false;
1164 /* Attribute weak handling wants to write out assembly right away. */
1165 if (is_attribute_p ("weak", name))
1166 return true;
1168 /* Attribute unused is applied directly, as it appertains to
1169 decls. */
1170 if (is_attribute_p ("unused", name))
1171 return false;
1173 /* Attribute tls_model wants to modify the symtab. */
1174 if (is_attribute_p ("tls_model", name))
1175 return true;
1177 /* #pragma omp declare simd attribute needs to be always deferred. */
1178 if (flag_openmp
1179 && is_attribute_p ("omp declare simd", name))
1180 return true;
1182 /* An attribute pack is clearly dependent. */
1183 if (args && PACK_EXPANSION_P (args))
1184 return true;
1186 /* If any of the arguments are dependent expressions, we can't evaluate
1187 the attribute until instantiation time. */
1188 for (arg = args; arg; arg = TREE_CHAIN (arg))
1190 tree t = TREE_VALUE (arg);
1192 /* If the first attribute argument is an identifier, only consider
1193 second and following arguments. Attributes like mode, format,
1194 cleanup and several target specific attributes aren't late
1195 just because they have an IDENTIFIER_NODE as first argument. */
1196 if (arg == args && attribute_takes_identifier_p (name)
1197 && identifier_p (t))
1198 continue;
1200 if (value_dependent_expression_p (t)
1201 || type_dependent_expression_p (t))
1202 return true;
1205 if (TREE_CODE (decl) == TYPE_DECL
1206 || TYPE_P (decl)
1207 || spec->type_required)
1209 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1211 /* We can't apply any attributes to a completely unknown type until
1212 instantiation time. */
1213 enum tree_code code = TREE_CODE (type);
1214 if (code == TEMPLATE_TYPE_PARM
1215 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1216 || code == TYPENAME_TYPE)
1217 return true;
1218 /* Also defer most attributes on dependent types. This is not
1219 necessary in all cases, but is the better default. */
1220 else if (dependent_type_p (type)
1221 /* But some attributes specifically apply to templates. */
1222 && !is_attribute_p ("abi_tag", name)
1223 && !is_attribute_p ("deprecated", name)
1224 && !is_attribute_p ("visibility", name))
1225 return true;
1226 else
1227 return false;
1229 else
1230 return false;
1233 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1234 applied at instantiation time and return them. If IS_DEPENDENT is true,
1235 the declaration itself is dependent, so all attributes should be applied
1236 at instantiation time. */
1238 static tree
1239 splice_template_attributes (tree *attr_p, tree decl)
1241 tree *p = attr_p;
1242 tree late_attrs = NULL_TREE;
1243 tree *q = &late_attrs;
1245 if (!p)
1246 return NULL_TREE;
1248 for (; *p; )
1250 if (is_late_template_attribute (*p, decl))
1252 ATTR_IS_DEPENDENT (*p) = 1;
1253 *q = *p;
1254 *p = TREE_CHAIN (*p);
1255 q = &TREE_CHAIN (*q);
1256 *q = NULL_TREE;
1258 else
1259 p = &TREE_CHAIN (*p);
1262 return late_attrs;
1265 /* Remove any late attributes from the list in ATTR_P and attach them to
1266 DECL_P. */
1268 static void
1269 save_template_attributes (tree *attr_p, tree *decl_p)
1271 tree *q;
1273 if (attr_p && *attr_p == error_mark_node)
1274 return;
1276 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1277 if (!late_attrs)
1278 return;
1280 if (DECL_P (*decl_p))
1281 q = &DECL_ATTRIBUTES (*decl_p);
1282 else
1283 q = &TYPE_ATTRIBUTES (*decl_p);
1285 tree old_attrs = *q;
1287 /* Merge the late attributes at the beginning with the attribute
1288 list. */
1289 late_attrs = merge_attributes (late_attrs, *q);
1290 *q = late_attrs;
1292 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1294 /* We've added new attributes directly to the main variant, so
1295 now we need to update all of the other variants to include
1296 these new attributes. */
1297 tree variant;
1298 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1299 variant = TYPE_NEXT_VARIANT (variant))
1301 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1302 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1307 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1308 to a typedef which gives a previously anonymous class or enum a name for
1309 linkage purposes. */
1311 bool
1312 attributes_naming_typedef_ok (tree attrs)
1314 for (; attrs; attrs = TREE_CHAIN (attrs))
1316 tree name = get_attribute_name (attrs);
1317 if (is_attribute_p ("vector_size", name))
1318 return false;
1320 return true;
1323 /* Like reconstruct_complex_type, but handle also template trees. */
1325 tree
1326 cp_reconstruct_complex_type (tree type, tree bottom)
1328 tree inner, outer;
1329 bool late_return_type_p = false;
1331 if (TYPE_PTR_P (type))
1333 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1334 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1335 TYPE_REF_CAN_ALIAS_ALL (type));
1337 else if (TREE_CODE (type) == REFERENCE_TYPE)
1339 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1340 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1341 TYPE_REF_CAN_ALIAS_ALL (type));
1343 else if (TREE_CODE (type) == ARRAY_TYPE)
1345 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1346 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1347 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1348 element type qualification will be handled by the recursive
1349 cp_reconstruct_complex_type call and cp_build_qualified_type
1350 for ARRAY_TYPEs changes the element type. */
1351 return outer;
1353 else if (TREE_CODE (type) == FUNCTION_TYPE)
1355 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1356 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1357 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1358 outer = apply_memfn_quals (outer,
1359 type_memfn_quals (type),
1360 type_memfn_rqual (type));
1362 else if (TREE_CODE (type) == METHOD_TYPE)
1364 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1365 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1366 /* The build_method_type_directly() routine prepends 'this' to argument list,
1367 so we must compensate by getting rid of it. */
1368 outer
1369 = build_method_type_directly
1370 (class_of_this_parm (type), inner,
1371 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1373 else if (TREE_CODE (type) == OFFSET_TYPE)
1375 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1376 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1378 else
1379 return bottom;
1381 if (TYPE_ATTRIBUTES (type))
1382 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1383 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1385 if (late_return_type_p)
1386 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1388 return outer;
1391 /* Replaces any constexpr expression that may be into the attributes
1392 arguments with their reduced value. */
1394 static void
1395 cp_check_const_attributes (tree attributes)
1397 if (attributes == error_mark_node)
1398 return;
1400 tree attr;
1401 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1403 tree arg;
1404 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1406 tree expr = TREE_VALUE (arg);
1407 if (EXPR_P (expr))
1408 TREE_VALUE (arg) = maybe_constant_value (expr);
1413 /* Return true if TYPE is an OpenMP mappable type. */
1414 bool
1415 cp_omp_mappable_type (tree type)
1417 /* Mappable type has to be complete. */
1418 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1419 return false;
1420 /* Arrays have mappable type if the elements have mappable type. */
1421 while (TREE_CODE (type) == ARRAY_TYPE)
1422 type = TREE_TYPE (type);
1423 /* A mappable type cannot contain virtual members. */
1424 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1425 return false;
1426 /* All data members must be non-static. */
1427 if (CLASS_TYPE_P (type))
1429 tree field;
1430 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1431 if (VAR_P (field))
1432 return false;
1433 /* All fields must have mappable types. */
1434 else if (TREE_CODE (field) == FIELD_DECL
1435 && !cp_omp_mappable_type (TREE_TYPE (field)))
1436 return false;
1438 return true;
1441 /* Like decl_attributes, but handle C++ complexity. */
1443 void
1444 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1446 if (*decl == NULL_TREE || *decl == void_type_node
1447 || *decl == error_mark_node)
1448 return;
1450 /* Add implicit "omp declare target" attribute if requested. */
1451 if (scope_chain->omp_declare_target_attribute
1452 && ((VAR_P (*decl)
1453 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1454 || TREE_CODE (*decl) == FUNCTION_DECL))
1456 if (VAR_P (*decl)
1457 && DECL_CLASS_SCOPE_P (*decl))
1458 error ("%q+D static data member inside of declare target directive",
1459 *decl);
1460 else if (!processing_template_decl
1461 && VAR_P (*decl)
1462 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1463 error ("%q+D in declare target directive does not have mappable type",
1464 *decl);
1465 else
1466 attributes = tree_cons (get_identifier ("omp declare target"),
1467 NULL_TREE, attributes);
1470 if (processing_template_decl)
1472 if (check_for_bare_parameter_packs (attributes))
1473 return;
1475 save_template_attributes (&attributes, decl);
1478 cp_check_const_attributes (attributes);
1480 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1481 decl = &DECL_TEMPLATE_RESULT (*decl);
1483 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1485 attributes
1486 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1487 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1488 attributes, flags);
1490 else
1491 decl_attributes (decl, attributes, flags);
1493 if (TREE_CODE (*decl) == TYPE_DECL)
1494 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1496 /* Propagate deprecation out to the template. */
1497 if (TREE_DEPRECATED (*decl))
1498 if (tree ti = get_template_info (*decl))
1500 tree tmpl = TI_TEMPLATE (ti);
1501 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1502 : DECL_TEMPLATE_RESULT (tmpl));
1503 if (*decl == pattern)
1504 TREE_DEPRECATED (tmpl) = true;
1508 /* Walks through the namespace- or function-scope anonymous union
1509 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1510 Returns one of the fields for use in the mangled name. */
1512 static tree
1513 build_anon_union_vars (tree type, tree object)
1515 tree main_decl = NULL_TREE;
1516 tree field;
1518 /* Rather than write the code to handle the non-union case,
1519 just give an error. */
1520 if (TREE_CODE (type) != UNION_TYPE)
1522 error ("anonymous struct not inside named type");
1523 return error_mark_node;
1526 for (field = TYPE_FIELDS (type);
1527 field != NULL_TREE;
1528 field = DECL_CHAIN (field))
1530 tree decl;
1531 tree ref;
1533 if (DECL_ARTIFICIAL (field))
1534 continue;
1535 if (TREE_CODE (field) != FIELD_DECL)
1537 permerror (DECL_SOURCE_LOCATION (field),
1538 "%q#D invalid; an anonymous union can only "
1539 "have non-static data members", field);
1540 continue;
1543 if (TREE_PRIVATE (field))
1544 permerror (DECL_SOURCE_LOCATION (field),
1545 "private member %q#D in anonymous union", field);
1546 else if (TREE_PROTECTED (field))
1547 permerror (DECL_SOURCE_LOCATION (field),
1548 "protected member %q#D in anonymous union", field);
1550 if (processing_template_decl)
1551 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1552 DECL_NAME (field), NULL_TREE);
1553 else
1554 ref = build_class_member_access_expr (object, field, NULL_TREE,
1555 false, tf_warning_or_error);
1557 if (DECL_NAME (field))
1559 tree base;
1561 decl = build_decl (input_location,
1562 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1563 DECL_ANON_UNION_VAR_P (decl) = 1;
1564 DECL_ARTIFICIAL (decl) = 1;
1566 base = get_base_address (object);
1567 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1568 TREE_STATIC (decl) = TREE_STATIC (base);
1569 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1571 SET_DECL_VALUE_EXPR (decl, ref);
1572 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1574 decl = pushdecl (decl);
1576 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1577 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1578 else
1579 decl = 0;
1581 if (main_decl == NULL_TREE)
1582 main_decl = decl;
1585 return main_decl;
1588 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1589 anonymous union, then all members must be laid out together. PUBLIC_P
1590 is nonzero if this union is not declared static. */
1592 void
1593 finish_anon_union (tree anon_union_decl)
1595 tree type;
1596 tree main_decl;
1597 bool public_p;
1599 if (anon_union_decl == error_mark_node)
1600 return;
1602 type = TREE_TYPE (anon_union_decl);
1603 public_p = TREE_PUBLIC (anon_union_decl);
1605 /* The VAR_DECL's context is the same as the TYPE's context. */
1606 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1608 if (TYPE_FIELDS (type) == NULL_TREE)
1609 return;
1611 if (public_p)
1613 error ("namespace-scope anonymous aggregates must be static");
1614 return;
1617 main_decl = build_anon_union_vars (type, anon_union_decl);
1618 if (main_decl == error_mark_node)
1619 return;
1620 if (main_decl == NULL_TREE)
1622 warning (0, "anonymous union with no members");
1623 return;
1626 if (!processing_template_decl)
1628 /* Use main_decl to set the mangled name. */
1629 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1630 maybe_commonize_var (anon_union_decl);
1631 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1632 mangle_decl (anon_union_decl);
1633 DECL_NAME (anon_union_decl) = NULL_TREE;
1636 pushdecl (anon_union_decl);
1637 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1640 /* Auxiliary functions to make type signatures for
1641 `operator new' and `operator delete' correspond to
1642 what compiler will be expecting. */
1644 tree
1645 coerce_new_type (tree type)
1647 int e = 0;
1648 tree args = TYPE_ARG_TYPES (type);
1650 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1652 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1654 e = 1;
1655 error ("%<operator new%> must return type %qT", ptr_type_node);
1658 if (args && args != void_list_node)
1660 if (TREE_PURPOSE (args))
1662 /* [basic.stc.dynamic.allocation]
1664 The first parameter shall not have an associated default
1665 argument. */
1666 error ("the first parameter of %<operator new%> cannot "
1667 "have a default argument");
1668 /* Throw away the default argument. */
1669 TREE_PURPOSE (args) = NULL_TREE;
1672 if (!same_type_p (TREE_VALUE (args), size_type_node))
1674 e = 2;
1675 args = TREE_CHAIN (args);
1678 else
1679 e = 2;
1681 if (e == 2)
1682 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1683 "as first parameter", size_type_node);
1685 switch (e)
1687 case 2:
1688 args = tree_cons (NULL_TREE, size_type_node, args);
1689 /* Fall through. */
1690 case 1:
1691 type = build_exception_variant
1692 (build_function_type (ptr_type_node, args),
1693 TYPE_RAISES_EXCEPTIONS (type));
1694 /* Fall through. */
1695 default:;
1697 return type;
1700 tree
1701 coerce_delete_type (tree type)
1703 int e = 0;
1704 tree args = TYPE_ARG_TYPES (type);
1706 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1708 if (!same_type_p (TREE_TYPE (type), void_type_node))
1710 e = 1;
1711 error ("%<operator delete%> must return type %qT", void_type_node);
1714 if (!args || args == void_list_node
1715 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1717 e = 2;
1718 if (args && args != void_list_node)
1719 args = TREE_CHAIN (args);
1720 error ("%<operator delete%> takes type %qT as first parameter",
1721 ptr_type_node);
1723 switch (e)
1725 case 2:
1726 args = tree_cons (NULL_TREE, ptr_type_node, args);
1727 /* Fall through. */
1728 case 1:
1729 type = build_exception_variant
1730 (build_function_type (void_type_node, args),
1731 TYPE_RAISES_EXCEPTIONS (type));
1732 /* Fall through. */
1733 default:;
1736 return type;
1739 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1740 and mark them as needed. */
1742 static void
1743 mark_vtable_entries (tree decl)
1745 tree fnaddr;
1746 unsigned HOST_WIDE_INT idx;
1748 /* It's OK for the vtable to refer to deprecated virtual functions. */
1749 warning_sentinel w(warn_deprecated_decl);
1751 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1752 idx, fnaddr)
1754 tree fn;
1756 STRIP_NOPS (fnaddr);
1758 if (TREE_CODE (fnaddr) != ADDR_EXPR
1759 && TREE_CODE (fnaddr) != FDESC_EXPR)
1760 /* This entry is an offset: a virtual base class offset, a
1761 virtual call offset, an RTTI offset, etc. */
1762 continue;
1764 fn = TREE_OPERAND (fnaddr, 0);
1765 TREE_ADDRESSABLE (fn) = 1;
1766 /* When we don't have vcall offsets, we output thunks whenever
1767 we output the vtables that contain them. With vcall offsets,
1768 we know all the thunks we'll need when we emit a virtual
1769 function, so we emit the thunks there instead. */
1770 if (DECL_THUNK_P (fn))
1771 use_thunk (fn, /*emit_p=*/0);
1772 mark_used (fn);
1776 /* Set DECL up to have the closest approximation of "initialized common"
1777 linkage available. */
1779 void
1780 comdat_linkage (tree decl)
1782 if (flag_weak)
1783 make_decl_one_only (decl, cxx_comdat_group (decl));
1784 else if (TREE_CODE (decl) == FUNCTION_DECL
1785 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1786 /* We can just emit function and compiler-generated variables
1787 statically; having multiple copies is (for the most part) only
1788 a waste of space.
1790 There are two correctness issues, however: the address of a
1791 template instantiation with external linkage should be the
1792 same, independent of what translation unit asks for the
1793 address, and this will not hold when we emit multiple copies of
1794 the function. However, there's little else we can do.
1796 Also, by default, the typeinfo implementation assumes that
1797 there will be only one copy of the string used as the name for
1798 each type. Therefore, if weak symbols are unavailable, the
1799 run-time library should perform a more conservative check; it
1800 should perform a string comparison, rather than an address
1801 comparison. */
1802 TREE_PUBLIC (decl) = 0;
1803 else
1805 /* Static data member template instantiations, however, cannot
1806 have multiple copies. */
1807 if (DECL_INITIAL (decl) == 0
1808 || DECL_INITIAL (decl) == error_mark_node)
1809 DECL_COMMON (decl) = 1;
1810 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1812 DECL_COMMON (decl) = 1;
1813 DECL_INITIAL (decl) = error_mark_node;
1815 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1817 /* We can't do anything useful; leave vars for explicit
1818 instantiation. */
1819 DECL_EXTERNAL (decl) = 1;
1820 DECL_NOT_REALLY_EXTERN (decl) = 0;
1824 if (TREE_PUBLIC (decl))
1825 DECL_COMDAT (decl) = 1;
1828 /* For win32 we also want to put explicit instantiations in
1829 linkonce sections, so that they will be merged with implicit
1830 instantiations; otherwise we get duplicate symbol errors.
1831 For Darwin we do not want explicit instantiations to be
1832 linkonce. */
1834 void
1835 maybe_make_one_only (tree decl)
1837 /* We used to say that this was not necessary on targets that support weak
1838 symbols, because the implicit instantiations will defer to the explicit
1839 one. However, that's not actually the case in SVR4; a strong definition
1840 after a weak one is an error. Also, not making explicit
1841 instantiations one_only means that we can end up with two copies of
1842 some template instantiations. */
1843 if (! flag_weak)
1844 return;
1846 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1847 we can get away with not emitting them if they aren't used. We need
1848 to for variables so that cp_finish_decl will update their linkage,
1849 because their DECL_INITIAL may not have been set properly yet. */
1851 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1852 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1853 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1855 make_decl_one_only (decl, cxx_comdat_group (decl));
1857 if (VAR_P (decl))
1859 varpool_node *node = varpool_node::get_create (decl);
1860 DECL_COMDAT (decl) = 1;
1861 /* Mark it needed so we don't forget to emit it. */
1862 node->forced_by_abi = true;
1863 TREE_USED (decl) = 1;
1868 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1869 This predicate will give the right answer during parsing of the
1870 function, which other tests may not. */
1872 bool
1873 vague_linkage_p (tree decl)
1875 if (!TREE_PUBLIC (decl))
1877 gcc_checking_assert (!DECL_COMDAT (decl));
1878 return false;
1880 /* Unfortunately, import_export_decl has not always been called
1881 before the function is processed, so we cannot simply check
1882 DECL_COMDAT. */
1883 if (DECL_COMDAT (decl)
1884 || (TREE_CODE (decl) == FUNCTION_DECL
1885 && DECL_DECLARED_INLINE_P (decl))
1886 || (DECL_LANG_SPECIFIC (decl)
1887 && DECL_TEMPLATE_INSTANTIATION (decl)))
1888 return true;
1889 else if (DECL_FUNCTION_SCOPE_P (decl))
1890 /* A local static in an inline effectively has vague linkage. */
1891 return (TREE_STATIC (decl)
1892 && vague_linkage_p (DECL_CONTEXT (decl)));
1893 else
1894 return false;
1897 /* Determine whether or not we want to specifically import or export CTYPE,
1898 using various heuristics. */
1900 static void
1901 import_export_class (tree ctype)
1903 /* -1 for imported, 1 for exported. */
1904 int import_export = 0;
1906 /* It only makes sense to call this function at EOF. The reason is
1907 that this function looks at whether or not the first non-inline
1908 non-abstract virtual member function has been defined in this
1909 translation unit. But, we can't possibly know that until we've
1910 seen the entire translation unit. */
1911 gcc_assert (at_eof);
1913 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1914 return;
1916 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1917 we will have CLASSTYPE_INTERFACE_ONLY set but not
1918 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1919 heuristic because someone will supply a #pragma implementation
1920 elsewhere, and deducing it here would produce a conflict. */
1921 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1922 return;
1924 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1925 import_export = -1;
1926 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1927 import_export = 1;
1928 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1929 && !flag_implicit_templates)
1930 /* For a template class, without -fimplicit-templates, check the
1931 repository. If the virtual table is assigned to this
1932 translation unit, then export the class; otherwise, import
1933 it. */
1934 import_export = repo_export_class_p (ctype) ? 1 : -1;
1935 else if (TYPE_POLYMORPHIC_P (ctype))
1937 /* The ABI specifies that the virtual table and associated
1938 information are emitted with the key method, if any. */
1939 tree method = CLASSTYPE_KEY_METHOD (ctype);
1940 /* If weak symbol support is not available, then we must be
1941 careful not to emit the vtable when the key function is
1942 inline. An inline function can be defined in multiple
1943 translation units. If we were to emit the vtable in each
1944 translation unit containing a definition, we would get
1945 multiple definition errors at link-time. */
1946 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1947 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1950 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1951 a definition anywhere else. */
1952 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1953 import_export = 0;
1955 /* Allow back ends the chance to overrule the decision. */
1956 if (targetm.cxx.import_export_class)
1957 import_export = targetm.cxx.import_export_class (ctype, import_export);
1959 if (import_export)
1961 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1962 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1966 /* Return true if VAR has already been provided to the back end; in that
1967 case VAR should not be modified further by the front end. */
1968 static bool
1969 var_finalized_p (tree var)
1971 return varpool_node::get_create (var)->definition;
1974 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1975 must be emitted in this translation unit. Mark it as such. */
1977 void
1978 mark_needed (tree decl)
1980 TREE_USED (decl) = 1;
1981 if (TREE_CODE (decl) == FUNCTION_DECL)
1983 /* Extern inline functions don't become needed when referenced.
1984 If we know a method will be emitted in other TU and no new
1985 functions can be marked reachable, just use the external
1986 definition. */
1987 struct cgraph_node *node = cgraph_node::get_create (decl);
1988 node->forced_by_abi = true;
1990 /* #pragma interface and -frepo code can call mark_needed for
1991 maybe-in-charge 'tors; mark the clones as well. */
1992 tree clone;
1993 FOR_EACH_CLONE (clone, decl)
1994 mark_needed (clone);
1996 else if (VAR_P (decl))
1998 varpool_node *node = varpool_node::get_create (decl);
1999 /* C++ frontend use mark_decl_references to force COMDAT variables
2000 to be output that might appear dead otherwise. */
2001 node->forced_by_abi = true;
2005 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2006 returns true if a definition of this entity should be provided in
2007 this object file. Callers use this function to determine whether
2008 or not to let the back end know that a definition of DECL is
2009 available in this translation unit. */
2011 bool
2012 decl_needed_p (tree decl)
2014 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2015 /* This function should only be called at the end of the translation
2016 unit. We cannot be sure of whether or not something will be
2017 COMDAT until that point. */
2018 gcc_assert (at_eof);
2020 /* All entities with external linkage that are not COMDAT/EXTERN should be
2021 emitted; they may be referred to from other object files. */
2022 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2023 return true;
2024 /* Functions marked "dllexport" must be emitted so that they are
2025 visible to other DLLs. */
2026 if (flag_keep_inline_dllexport
2027 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2028 return true;
2030 /* When not optimizing, do not bother to produce definitions for extern
2031 symbols. */
2032 if (DECL_REALLY_EXTERN (decl)
2033 && ((TREE_CODE (decl) != FUNCTION_DECL
2034 && !optimize)
2035 || (TREE_CODE (decl) == FUNCTION_DECL
2036 && !opt_for_fn (decl, optimize)))
2037 && !lookup_attribute ("always_inline", decl))
2038 return false;
2040 /* If this entity was used, let the back end see it; it will decide
2041 whether or not to emit it into the object file. */
2042 if (TREE_USED (decl))
2043 return true;
2044 /* Virtual functions might be needed for devirtualization. */
2045 if (flag_devirtualize
2046 && TREE_CODE (decl) == FUNCTION_DECL
2047 && DECL_VIRTUAL_P (decl))
2048 return true;
2049 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2050 reference to DECL might cause it to be emitted later. */
2051 return false;
2054 /* If necessary, write out the vtables for the dynamic class CTYPE.
2055 Returns true if any vtables were emitted. */
2057 static bool
2058 maybe_emit_vtables (tree ctype)
2060 tree vtbl;
2061 tree primary_vtbl;
2062 int needed = 0;
2063 varpool_node *current = NULL, *last = NULL;
2065 /* If the vtables for this class have already been emitted there is
2066 nothing more to do. */
2067 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2068 if (var_finalized_p (primary_vtbl))
2069 return false;
2070 /* Ignore dummy vtables made by get_vtable_decl. */
2071 if (TREE_TYPE (primary_vtbl) == void_type_node)
2072 return false;
2074 /* On some targets, we cannot determine the key method until the end
2075 of the translation unit -- which is when this function is
2076 called. */
2077 if (!targetm.cxx.key_method_may_be_inline ())
2078 determine_key_method (ctype);
2080 /* See if any of the vtables are needed. */
2081 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2083 import_export_decl (vtbl);
2084 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2085 needed = 1;
2087 if (!needed)
2089 /* If the references to this class' vtables are optimized away,
2090 still emit the appropriate debugging information. See
2091 dfs_debug_mark. */
2092 if (DECL_COMDAT (primary_vtbl)
2093 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2094 note_debug_info_needed (ctype);
2095 return false;
2098 /* The ABI requires that we emit all of the vtables if we emit any
2099 of them. */
2100 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2102 /* Mark entities references from the virtual table as used. */
2103 mark_vtable_entries (vtbl);
2105 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2107 vec<tree, va_gc> *cleanups = NULL;
2108 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2109 LOOKUP_NORMAL);
2111 /* It had better be all done at compile-time. */
2112 gcc_assert (!expr && !cleanups);
2115 /* Write it out. */
2116 DECL_EXTERNAL (vtbl) = 0;
2117 rest_of_decl_compilation (vtbl, 1, 1);
2119 /* Because we're only doing syntax-checking, we'll never end up
2120 actually marking the variable as written. */
2121 if (flag_syntax_only)
2122 TREE_ASM_WRITTEN (vtbl) = 1;
2123 else if (DECL_ONE_ONLY (vtbl))
2125 current = varpool_node::get_create (vtbl);
2126 if (last)
2127 current->add_to_same_comdat_group (last);
2128 last = current;
2132 /* Since we're writing out the vtable here, also write the debug
2133 info. */
2134 note_debug_info_needed (ctype);
2136 return true;
2139 /* A special return value from type_visibility meaning internal
2140 linkage. */
2142 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2144 /* walk_tree helper function for type_visibility. */
2146 static tree
2147 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2149 int *vis_p = (int *)data;
2150 if (! TYPE_P (*tp))
2152 *walk_subtrees = 0;
2154 else if (OVERLOAD_TYPE_P (*tp)
2155 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2157 *vis_p = VISIBILITY_ANON;
2158 return *tp;
2160 else if (CLASS_TYPE_P (*tp)
2161 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2162 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2163 return NULL;
2166 /* Returns the visibility of TYPE, which is the minimum visibility of its
2167 component types. */
2169 static int
2170 type_visibility (tree type)
2172 int vis = VISIBILITY_DEFAULT;
2173 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2174 return vis;
2177 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2178 specified (or if VISIBILITY is static). If TMPL is true, this
2179 constraint is for a template argument, and takes precedence
2180 over explicitly-specified visibility on the template. */
2182 static void
2183 constrain_visibility (tree decl, int visibility, bool tmpl)
2185 if (visibility == VISIBILITY_ANON)
2187 /* extern "C" declarations aren't affected by the anonymous
2188 namespace. */
2189 if (!DECL_EXTERN_C_P (decl))
2191 TREE_PUBLIC (decl) = 0;
2192 DECL_WEAK (decl) = 0;
2193 DECL_COMMON (decl) = 0;
2194 DECL_COMDAT (decl) = false;
2195 if (VAR_OR_FUNCTION_DECL_P (decl))
2197 struct symtab_node *snode = symtab_node::get (decl);
2199 if (snode)
2200 snode->set_comdat_group (NULL);
2202 DECL_INTERFACE_KNOWN (decl) = 1;
2203 if (DECL_LANG_SPECIFIC (decl))
2204 DECL_NOT_REALLY_EXTERN (decl) = 1;
2207 else if (visibility > DECL_VISIBILITY (decl)
2208 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2210 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2211 /* This visibility was not specified. */
2212 DECL_VISIBILITY_SPECIFIED (decl) = false;
2216 /* Constrain the visibility of DECL based on the visibility of its template
2217 arguments. */
2219 static void
2220 constrain_visibility_for_template (tree decl, tree targs)
2222 /* If this is a template instantiation, check the innermost
2223 template args for visibility constraints. The outer template
2224 args are covered by the class check. */
2225 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2226 int i;
2227 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2229 int vis = 0;
2231 tree arg = TREE_VEC_ELT (args, i-1);
2232 if (TYPE_P (arg))
2233 vis = type_visibility (arg);
2234 else
2236 if (REFERENCE_REF_P (arg))
2237 arg = TREE_OPERAND (arg, 0);
2238 if (TREE_TYPE (arg))
2239 STRIP_NOPS (arg);
2240 if (TREE_CODE (arg) == ADDR_EXPR)
2241 arg = TREE_OPERAND (arg, 0);
2242 if (VAR_OR_FUNCTION_DECL_P (arg))
2244 if (! TREE_PUBLIC (arg))
2245 vis = VISIBILITY_ANON;
2246 else
2247 vis = DECL_VISIBILITY (arg);
2250 if (vis)
2251 constrain_visibility (decl, vis, true);
2255 /* Like c_determine_visibility, but with additional C++-specific
2256 behavior.
2258 Function-scope entities can rely on the function's visibility because
2259 it is set in start_preparsed_function.
2261 Class-scope entities cannot rely on the class's visibility until the end
2262 of the enclosing class definition.
2264 Note that because namespaces have multiple independent definitions,
2265 namespace visibility is handled elsewhere using the #pragma visibility
2266 machinery rather than by decorating the namespace declaration.
2268 The goal is for constraints from the type to give a diagnostic, and
2269 other constraints to be applied silently. */
2271 void
2272 determine_visibility (tree decl)
2274 tree class_type = NULL_TREE;
2275 bool use_template;
2276 bool orig_visibility_specified;
2277 enum symbol_visibility orig_visibility;
2279 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2281 /* Only relevant for names with external linkage. */
2282 if (!TREE_PUBLIC (decl))
2283 return;
2285 /* Cloned constructors and destructors get the same visibility as
2286 the underlying function. That should be set up in
2287 maybe_clone_body. */
2288 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2290 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2291 orig_visibility = DECL_VISIBILITY (decl);
2293 if (TREE_CODE (decl) == TYPE_DECL)
2295 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2296 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2297 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2298 use_template = 1;
2299 else
2300 use_template = 0;
2302 else if (DECL_LANG_SPECIFIC (decl))
2303 use_template = DECL_USE_TEMPLATE (decl);
2304 else
2305 use_template = 0;
2307 /* If DECL is a member of a class, visibility specifiers on the
2308 class can influence the visibility of the DECL. */
2309 if (DECL_CLASS_SCOPE_P (decl))
2310 class_type = DECL_CONTEXT (decl);
2311 else
2313 /* Not a class member. */
2315 /* Virtual tables have DECL_CONTEXT set to their associated class,
2316 so they are automatically handled above. */
2317 gcc_assert (!VAR_P (decl)
2318 || !DECL_VTABLE_OR_VTT_P (decl));
2320 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2322 /* Local statics and classes get the visibility of their
2323 containing function by default, except that
2324 -fvisibility-inlines-hidden doesn't affect them. */
2325 tree fn = DECL_CONTEXT (decl);
2326 if (DECL_VISIBILITY_SPECIFIED (fn))
2328 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2329 DECL_VISIBILITY_SPECIFIED (decl) =
2330 DECL_VISIBILITY_SPECIFIED (fn);
2332 else
2334 if (DECL_CLASS_SCOPE_P (fn))
2335 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2336 else if (determine_hidden_inline (fn))
2338 DECL_VISIBILITY (decl) = default_visibility;
2339 DECL_VISIBILITY_SPECIFIED (decl) =
2340 visibility_options.inpragma;
2342 else
2344 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2345 DECL_VISIBILITY_SPECIFIED (decl) =
2346 DECL_VISIBILITY_SPECIFIED (fn);
2350 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2351 but have no TEMPLATE_INFO, so don't try to check it. */
2352 use_template = 0;
2354 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2355 && flag_visibility_ms_compat)
2357 /* Under -fvisibility-ms-compat, types are visible by default,
2358 even though their contents aren't. */
2359 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2360 int underlying_vis = type_visibility (underlying_type);
2361 if (underlying_vis == VISIBILITY_ANON
2362 || (CLASS_TYPE_P (underlying_type)
2363 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2364 constrain_visibility (decl, underlying_vis, false);
2365 else
2366 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2368 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2370 /* tinfo visibility is based on the type it's for. */
2371 constrain_visibility
2372 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2374 /* Give the target a chance to override the visibility associated
2375 with DECL. */
2376 if (TREE_PUBLIC (decl)
2377 && !DECL_REALLY_EXTERN (decl)
2378 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2379 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2380 targetm.cxx.determine_class_data_visibility (decl);
2382 else if (use_template)
2383 /* Template instantiations and specializations get visibility based
2384 on their template unless they override it with an attribute. */;
2385 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2387 if (determine_hidden_inline (decl))
2388 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2389 else
2391 /* Set default visibility to whatever the user supplied with
2392 #pragma GCC visibility or a namespace visibility attribute. */
2393 DECL_VISIBILITY (decl) = default_visibility;
2394 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2399 if (use_template)
2401 /* If the specialization doesn't specify visibility, use the
2402 visibility from the template. */
2403 tree tinfo = get_template_info (decl);
2404 tree args = TI_ARGS (tinfo);
2405 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2406 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2407 : DECL_ATTRIBUTES (decl));
2409 if (args != error_mark_node)
2411 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2413 if (!DECL_VISIBILITY_SPECIFIED (decl))
2415 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2416 && determine_hidden_inline (decl))
2417 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2418 else
2420 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2421 DECL_VISIBILITY_SPECIFIED (decl)
2422 = DECL_VISIBILITY_SPECIFIED (pattern);
2426 if (args
2427 /* Template argument visibility outweighs #pragma or namespace
2428 visibility, but not an explicit attribute. */
2429 && !lookup_attribute ("visibility", attribs))
2431 int depth = TMPL_ARGS_DEPTH (args);
2432 if (DECL_VISIBILITY_SPECIFIED (decl))
2434 /* A class template member with explicit visibility
2435 overrides the class visibility, so we need to apply
2436 all the levels of template args directly. */
2437 int i;
2438 for (i = 1; i <= depth; ++i)
2440 tree lev = TMPL_ARGS_LEVEL (args, i);
2441 constrain_visibility_for_template (decl, lev);
2444 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2445 /* Limit visibility based on its template arguments. */
2446 constrain_visibility_for_template (decl, args);
2451 if (class_type)
2452 determine_visibility_from_class (decl, class_type);
2454 if (decl_anon_ns_mem_p (decl))
2455 /* Names in an anonymous namespace get internal linkage.
2456 This might change once we implement export. */
2457 constrain_visibility (decl, VISIBILITY_ANON, false);
2458 else if (TREE_CODE (decl) != TYPE_DECL)
2460 /* Propagate anonymity from type to decl. */
2461 int tvis = type_visibility (TREE_TYPE (decl));
2462 if (tvis == VISIBILITY_ANON
2463 || ! DECL_VISIBILITY_SPECIFIED (decl))
2464 constrain_visibility (decl, tvis, false);
2466 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2467 /* DR 757: A type without linkage shall not be used as the type of a
2468 variable or function with linkage, unless
2469 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2470 o the variable or function is not used (3.2 [basic.def.odr]) or is
2471 defined in the same translation unit.
2473 Since non-extern "C" decls need to be defined in the same
2474 translation unit, we can make the type internal. */
2475 constrain_visibility (decl, VISIBILITY_ANON, false);
2477 /* If visibility changed and DECL already has DECL_RTL, ensure
2478 symbol flags are updated. */
2479 if ((DECL_VISIBILITY (decl) != orig_visibility
2480 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2481 && ((VAR_P (decl) && TREE_STATIC (decl))
2482 || TREE_CODE (decl) == FUNCTION_DECL)
2483 && DECL_RTL_SET_P (decl))
2484 make_decl_rtl (decl);
2487 /* By default, static data members and function members receive
2488 the visibility of their containing class. */
2490 static void
2491 determine_visibility_from_class (tree decl, tree class_type)
2493 if (DECL_VISIBILITY_SPECIFIED (decl))
2494 return;
2496 if (determine_hidden_inline (decl))
2497 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2498 else
2500 /* Default to the class visibility. */
2501 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2502 DECL_VISIBILITY_SPECIFIED (decl)
2503 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2506 /* Give the target a chance to override the visibility associated
2507 with DECL. */
2508 if (VAR_P (decl)
2509 && (DECL_TINFO_P (decl)
2510 || (DECL_VTABLE_OR_VTT_P (decl)
2511 /* Construction virtual tables are not exported because
2512 they cannot be referred to from other object files;
2513 their name is not standardized by the ABI. */
2514 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2515 && TREE_PUBLIC (decl)
2516 && !DECL_REALLY_EXTERN (decl)
2517 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2518 targetm.cxx.determine_class_data_visibility (decl);
2521 /* Returns true iff DECL is an inline that should get hidden visibility
2522 because of -fvisibility-inlines-hidden. */
2524 static bool
2525 determine_hidden_inline (tree decl)
2527 return (visibility_options.inlines_hidden
2528 /* Don't do this for inline templates; specializations might not be
2529 inline, and we don't want them to inherit the hidden
2530 visibility. We'll set it here for all inline instantiations. */
2531 && !processing_template_decl
2532 && TREE_CODE (decl) == FUNCTION_DECL
2533 && DECL_DECLARED_INLINE_P (decl)
2534 && (! DECL_LANG_SPECIFIC (decl)
2535 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2538 /* Constrain the visibility of a class TYPE based on the visibility of its
2539 field types. Warn if any fields require lesser visibility. */
2541 void
2542 constrain_class_visibility (tree type)
2544 tree binfo;
2545 tree t;
2546 int i;
2548 int vis = type_visibility (type);
2550 if (vis == VISIBILITY_ANON
2551 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2552 return;
2554 /* Don't warn about visibility if the class has explicit visibility. */
2555 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2556 vis = VISIBILITY_INTERNAL;
2558 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2559 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2561 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2562 int subvis = type_visibility (ftype);
2564 if (subvis == VISIBILITY_ANON)
2566 if (!in_main_input_context())
2568 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2569 if (nlt)
2571 if (same_type_p (TREE_TYPE (t), nlt))
2572 warning (OPT_Wsubobject_linkage, "\
2573 %qT has a field %qD whose type has no linkage",
2574 type, t);
2575 else
2576 warning (OPT_Wsubobject_linkage, "\
2577 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2578 type, t, nlt);
2580 else
2581 warning (OPT_Wsubobject_linkage, "\
2582 %qT has a field %qD whose type uses the anonymous namespace",
2583 type, t);
2586 else if (MAYBE_CLASS_TYPE_P (ftype)
2587 && vis < VISIBILITY_HIDDEN
2588 && subvis >= VISIBILITY_HIDDEN)
2589 warning (OPT_Wattributes, "\
2590 %qT declared with greater visibility than the type of its field %qD",
2591 type, t);
2594 binfo = TYPE_BINFO (type);
2595 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2597 int subvis = type_visibility (TREE_TYPE (t));
2599 if (subvis == VISIBILITY_ANON)
2601 if (!in_main_input_context())
2603 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2604 if (nlt)
2606 if (same_type_p (TREE_TYPE (t), nlt))
2607 warning (OPT_Wsubobject_linkage, "\
2608 %qT has a base %qT whose type has no linkage",
2609 type, TREE_TYPE (t));
2610 else
2611 warning (OPT_Wsubobject_linkage, "\
2612 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2613 type, TREE_TYPE (t), nlt);
2615 else
2616 warning (OPT_Wsubobject_linkage, "\
2617 %qT has a base %qT whose type uses the anonymous namespace",
2618 type, TREE_TYPE (t));
2621 else if (vis < VISIBILITY_HIDDEN
2622 && subvis >= VISIBILITY_HIDDEN)
2623 warning (OPT_Wattributes, "\
2624 %qT declared with greater visibility than its base %qT",
2625 type, TREE_TYPE (t));
2629 /* Functions for adjusting the visibility of a tagged type and its nested
2630 types and declarations when it gets a name for linkage purposes from a
2631 typedef. */
2633 static void bt_reset_linkage_1 (binding_entry, void *);
2634 static void bt_reset_linkage_2 (binding_entry, void *);
2636 /* First reset the visibility of all the types. */
2638 static void
2639 reset_type_linkage_1 (tree type)
2641 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2642 if (CLASS_TYPE_P (type))
2643 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2644 bt_reset_linkage_1, NULL);
2646 static void
2647 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2649 reset_type_linkage_1 (b->type);
2652 /* Then reset the visibility of any static data members or member
2653 functions that use those types. */
2655 static void
2656 reset_decl_linkage (tree decl)
2658 if (TREE_PUBLIC (decl))
2659 return;
2660 if (DECL_CLONED_FUNCTION_P (decl))
2661 return;
2662 TREE_PUBLIC (decl) = true;
2663 DECL_INTERFACE_KNOWN (decl) = false;
2664 determine_visibility (decl);
2665 tentative_decl_linkage (decl);
2667 static void
2668 reset_type_linkage_2 (tree type)
2670 if (CLASS_TYPE_P (type))
2672 if (tree vt = CLASSTYPE_VTABLES (type))
2674 tree name = mangle_vtbl_for_type (type);
2675 DECL_NAME (vt) = name;
2676 SET_DECL_ASSEMBLER_NAME (vt, name);
2677 reset_decl_linkage (vt);
2679 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2681 tree name = mangle_typeinfo_for_type (type);
2682 DECL_NAME (ti) = name;
2683 SET_DECL_ASSEMBLER_NAME (ti, name);
2684 TREE_TYPE (name) = type;
2685 reset_decl_linkage (ti);
2687 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2689 tree mem = STRIP_TEMPLATE (m);
2690 if (VAR_P (mem))
2691 reset_decl_linkage (mem);
2693 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2695 tree mem = STRIP_TEMPLATE (m);
2696 reset_decl_linkage (mem);
2697 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (mem))
2699 /* Also update its name, for cxx_dwarf_name. */
2700 DECL_NAME (mem) = TYPE_IDENTIFIER (type);
2701 if (m != mem)
2702 DECL_NAME (m) = TYPE_IDENTIFIER (type);
2705 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2706 bt_reset_linkage_2, NULL);
2709 static void
2710 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2712 reset_type_linkage_2 (b->type);
2714 void
2715 reset_type_linkage (tree type)
2717 reset_type_linkage_1 (type);
2718 reset_type_linkage_2 (type);
2721 /* Set up our initial idea of what the linkage of DECL should be. */
2723 void
2724 tentative_decl_linkage (tree decl)
2726 if (DECL_INTERFACE_KNOWN (decl))
2727 /* We've already made a decision as to how this function will
2728 be handled. */;
2729 else if (vague_linkage_p (decl))
2731 if (TREE_CODE (decl) == FUNCTION_DECL
2732 && decl_defined_p (decl))
2734 DECL_EXTERNAL (decl) = 1;
2735 DECL_NOT_REALLY_EXTERN (decl) = 1;
2736 note_vague_linkage_fn (decl);
2737 /* A non-template inline function with external linkage will
2738 always be COMDAT. As we must eventually determine the
2739 linkage of all functions, and as that causes writes to
2740 the data mapped in from the PCH file, it's advantageous
2741 to mark the functions at this point. */
2742 if (DECL_DECLARED_INLINE_P (decl)
2743 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2744 || DECL_DEFAULTED_FN (decl)))
2746 /* This function must have external linkage, as
2747 otherwise DECL_INTERFACE_KNOWN would have been
2748 set. */
2749 gcc_assert (TREE_PUBLIC (decl));
2750 comdat_linkage (decl);
2751 DECL_INTERFACE_KNOWN (decl) = 1;
2754 else if (VAR_P (decl))
2755 maybe_commonize_var (decl);
2759 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2760 for DECL has not already been determined, do so now by setting
2761 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2762 function is called entities with vague linkage whose definitions
2763 are available must have TREE_PUBLIC set.
2765 If this function decides to place DECL in COMDAT, it will set
2766 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2767 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2768 callers defer that decision until it is clear that DECL is actually
2769 required. */
2771 void
2772 import_export_decl (tree decl)
2774 int emit_p;
2775 bool comdat_p;
2776 bool import_p;
2777 tree class_type = NULL_TREE;
2779 if (DECL_INTERFACE_KNOWN (decl))
2780 return;
2782 /* We cannot determine what linkage to give to an entity with vague
2783 linkage until the end of the file. For example, a virtual table
2784 for a class will be defined if and only if the key method is
2785 defined in this translation unit. As a further example, consider
2786 that when compiling a translation unit that uses PCH file with
2787 "-frepo" it would be incorrect to make decisions about what
2788 entities to emit when building the PCH; those decisions must be
2789 delayed until the repository information has been processed. */
2790 gcc_assert (at_eof);
2791 /* Object file linkage for explicit instantiations is handled in
2792 mark_decl_instantiated. For static variables in functions with
2793 vague linkage, maybe_commonize_var is used.
2795 Therefore, the only declarations that should be provided to this
2796 function are those with external linkage that are:
2798 * implicit instantiations of function templates
2800 * inline function
2802 * implicit instantiations of static data members of class
2803 templates
2805 * virtual tables
2807 * typeinfo objects
2809 Furthermore, all entities that reach this point must have a
2810 definition available in this translation unit.
2812 The following assertions check these conditions. */
2813 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2814 /* Any code that creates entities with TREE_PUBLIC cleared should
2815 also set DECL_INTERFACE_KNOWN. */
2816 gcc_assert (TREE_PUBLIC (decl));
2817 if (TREE_CODE (decl) == FUNCTION_DECL)
2818 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2819 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2820 || DECL_DECLARED_INLINE_P (decl));
2821 else
2822 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2823 || DECL_VTABLE_OR_VTT_P (decl)
2824 || DECL_TINFO_P (decl));
2825 /* Check that a definition of DECL is available in this translation
2826 unit. */
2827 gcc_assert (!DECL_REALLY_EXTERN (decl));
2829 /* Assume that DECL will not have COMDAT linkage. */
2830 comdat_p = false;
2831 /* Assume that DECL will not be imported into this translation
2832 unit. */
2833 import_p = false;
2835 /* See if the repository tells us whether or not to emit DECL in
2836 this translation unit. */
2837 emit_p = repo_emit_p (decl);
2838 if (emit_p == 0)
2839 import_p = true;
2840 else if (emit_p == 1)
2842 /* The repository indicates that this entity should be defined
2843 here. Make sure the back end honors that request. */
2844 mark_needed (decl);
2845 /* Output the definition as an ordinary strong definition. */
2846 DECL_EXTERNAL (decl) = 0;
2847 DECL_INTERFACE_KNOWN (decl) = 1;
2848 return;
2851 if (import_p)
2852 /* We have already decided what to do with this DECL; there is no
2853 need to check anything further. */
2855 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2857 class_type = DECL_CONTEXT (decl);
2858 import_export_class (class_type);
2859 if (TYPE_FOR_JAVA (class_type))
2860 import_p = true;
2861 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2862 && CLASSTYPE_INTERFACE_ONLY (class_type))
2863 import_p = true;
2864 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2865 && !CLASSTYPE_USE_TEMPLATE (class_type)
2866 && CLASSTYPE_KEY_METHOD (class_type)
2867 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2868 /* The ABI requires that all virtual tables be emitted with
2869 COMDAT linkage. However, on systems where COMDAT symbols
2870 don't show up in the table of contents for a static
2871 archive, or on systems without weak symbols (where we
2872 approximate COMDAT linkage by using internal linkage), the
2873 linker will report errors about undefined symbols because
2874 it will not see the virtual table definition. Therefore,
2875 in the case that we know that the virtual table will be
2876 emitted in only one translation unit, we make the virtual
2877 table an ordinary definition with external linkage. */
2878 DECL_EXTERNAL (decl) = 0;
2879 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2881 /* CLASS_TYPE is being exported from this translation unit,
2882 so DECL should be defined here. */
2883 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2884 /* If a class is declared in a header with the "extern
2885 template" extension, then it will not be instantiated,
2886 even in translation units that would normally require
2887 it. Often such classes are explicitly instantiated in
2888 one translation unit. Therefore, the explicit
2889 instantiation must be made visible to other translation
2890 units. */
2891 DECL_EXTERNAL (decl) = 0;
2892 else
2894 /* The generic C++ ABI says that class data is always
2895 COMDAT, even if there is a key function. Some
2896 variants (e.g., the ARM EABI) says that class data
2897 only has COMDAT linkage if the class data might be
2898 emitted in more than one translation unit. When the
2899 key method can be inline and is inline, we still have
2900 to arrange for comdat even though
2901 class_data_always_comdat is false. */
2902 if (!CLASSTYPE_KEY_METHOD (class_type)
2903 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2904 || targetm.cxx.class_data_always_comdat ())
2906 /* The ABI requires COMDAT linkage. Normally, we
2907 only emit COMDAT things when they are needed;
2908 make sure that we realize that this entity is
2909 indeed needed. */
2910 comdat_p = true;
2911 mark_needed (decl);
2915 else if (!flag_implicit_templates
2916 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2917 import_p = true;
2918 else
2919 comdat_p = true;
2921 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2923 tree type = TREE_TYPE (DECL_NAME (decl));
2924 if (CLASS_TYPE_P (type))
2926 class_type = type;
2927 import_export_class (type);
2928 if (CLASSTYPE_INTERFACE_KNOWN (type)
2929 && TYPE_POLYMORPHIC_P (type)
2930 && CLASSTYPE_INTERFACE_ONLY (type)
2931 /* If -fno-rtti was specified, then we cannot be sure
2932 that RTTI information will be emitted with the
2933 virtual table of the class, so we must emit it
2934 wherever it is used. */
2935 && flag_rtti)
2936 import_p = true;
2937 else
2939 if (CLASSTYPE_INTERFACE_KNOWN (type)
2940 && !CLASSTYPE_INTERFACE_ONLY (type))
2942 comdat_p = (targetm.cxx.class_data_always_comdat ()
2943 || (CLASSTYPE_KEY_METHOD (type)
2944 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2945 mark_needed (decl);
2946 if (!flag_weak)
2948 comdat_p = false;
2949 DECL_EXTERNAL (decl) = 0;
2952 else
2953 comdat_p = true;
2956 else
2957 comdat_p = true;
2959 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2961 /* DECL is an implicit instantiation of a function or static
2962 data member. */
2963 if ((flag_implicit_templates
2964 && !flag_use_repository)
2965 || (flag_implicit_inline_templates
2966 && TREE_CODE (decl) == FUNCTION_DECL
2967 && DECL_DECLARED_INLINE_P (decl)))
2968 comdat_p = true;
2969 else
2970 /* If we are not implicitly generating templates, then mark
2971 this entity as undefined in this translation unit. */
2972 import_p = true;
2974 else if (DECL_FUNCTION_MEMBER_P (decl))
2976 if (!DECL_DECLARED_INLINE_P (decl))
2978 tree ctype = DECL_CONTEXT (decl);
2979 import_export_class (ctype);
2980 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2982 DECL_NOT_REALLY_EXTERN (decl)
2983 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2984 || (DECL_DECLARED_INLINE_P (decl)
2985 && ! flag_implement_inlines
2986 && !DECL_VINDEX (decl)));
2988 if (!DECL_NOT_REALLY_EXTERN (decl))
2989 DECL_EXTERNAL (decl) = 1;
2991 /* Always make artificials weak. */
2992 if (DECL_ARTIFICIAL (decl) && flag_weak)
2993 comdat_p = true;
2994 else
2995 maybe_make_one_only (decl);
2998 else
2999 comdat_p = true;
3001 else
3002 comdat_p = true;
3004 if (import_p)
3006 /* If we are importing DECL into this translation unit, mark is
3007 an undefined here. */
3008 DECL_EXTERNAL (decl) = 1;
3009 DECL_NOT_REALLY_EXTERN (decl) = 0;
3011 else if (comdat_p)
3013 /* If we decided to put DECL in COMDAT, mark it accordingly at
3014 this point. */
3015 comdat_linkage (decl);
3018 DECL_INTERFACE_KNOWN (decl) = 1;
3021 /* Return an expression that performs the destruction of DECL, which
3022 must be a VAR_DECL whose type has a non-trivial destructor, or is
3023 an array whose (innermost) elements have a non-trivial destructor. */
3025 tree
3026 build_cleanup (tree decl)
3028 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3029 gcc_assert (clean != NULL_TREE);
3030 return clean;
3033 /* Returns the initialization guard variable for the variable DECL,
3034 which has static storage duration. */
3036 tree
3037 get_guard (tree decl)
3039 tree sname;
3040 tree guard;
3042 sname = mangle_guard_variable (decl);
3043 guard = IDENTIFIER_GLOBAL_VALUE (sname);
3044 if (! guard)
3046 tree guard_type;
3048 /* We use a type that is big enough to contain a mutex as well
3049 as an integer counter. */
3050 guard_type = targetm.cxx.guard_type ();
3051 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3052 VAR_DECL, sname, guard_type);
3054 /* The guard should have the same linkage as what it guards. */
3055 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3056 TREE_STATIC (guard) = TREE_STATIC (decl);
3057 DECL_COMMON (guard) = DECL_COMMON (decl);
3058 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3059 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3060 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3061 if (DECL_ONE_ONLY (decl))
3062 make_decl_one_only (guard, cxx_comdat_group (guard));
3063 if (TREE_PUBLIC (decl))
3064 DECL_WEAK (guard) = DECL_WEAK (decl);
3065 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3066 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3068 DECL_ARTIFICIAL (guard) = 1;
3069 DECL_IGNORED_P (guard) = 1;
3070 TREE_USED (guard) = 1;
3071 pushdecl_top_level_and_finish (guard, NULL_TREE);
3073 return guard;
3076 /* Return an atomic load of src with the appropriate memory model. */
3078 static tree
3079 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3081 tree ptr_type = build_pointer_type (char_type_node);
3082 tree mem_model = build_int_cst (integer_type_node, model);
3083 tree t, addr, val;
3084 unsigned int size;
3085 int fncode;
3087 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3089 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3090 t = builtin_decl_implicit ((enum built_in_function) fncode);
3092 addr = build1 (ADDR_EXPR, ptr_type, src);
3093 val = build_call_expr (t, 2, addr, mem_model);
3094 return val;
3097 /* Return those bits of the GUARD variable that should be set when the
3098 guarded entity is actually initialized. */
3100 static tree
3101 get_guard_bits (tree guard)
3103 if (!targetm.cxx.guard_mask_bit ())
3105 /* We only set the first byte of the guard, in order to leave room
3106 for a mutex in the high-order bits. */
3107 guard = build1 (ADDR_EXPR,
3108 build_pointer_type (TREE_TYPE (guard)),
3109 guard);
3110 guard = build1 (NOP_EXPR,
3111 build_pointer_type (char_type_node),
3112 guard);
3113 guard = build1 (INDIRECT_REF, char_type_node, guard);
3116 return guard;
3119 /* Return an expression which determines whether or not the GUARD
3120 variable has already been initialized. */
3122 tree
3123 get_guard_cond (tree guard, bool thread_safe)
3125 tree guard_value;
3127 if (!thread_safe)
3128 guard = get_guard_bits (guard);
3129 else
3130 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3132 /* Mask off all but the low bit. */
3133 if (targetm.cxx.guard_mask_bit ())
3135 guard_value = integer_one_node;
3136 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3137 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3138 guard = cp_build_binary_op (input_location,
3139 BIT_AND_EXPR, guard, guard_value,
3140 tf_warning_or_error);
3143 guard_value = integer_zero_node;
3144 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3145 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3146 return cp_build_binary_op (input_location,
3147 EQ_EXPR, guard, guard_value,
3148 tf_warning_or_error);
3151 /* Return an expression which sets the GUARD variable, indicating that
3152 the variable being guarded has been initialized. */
3154 tree
3155 set_guard (tree guard)
3157 tree guard_init;
3159 /* Set the GUARD to one. */
3160 guard = get_guard_bits (guard);
3161 guard_init = integer_one_node;
3162 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3163 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3164 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3165 tf_warning_or_error);
3168 /* Returns true iff we can tell that VAR does not have a dynamic
3169 initializer. */
3171 static bool
3172 var_defined_without_dynamic_init (tree var)
3174 /* If it's defined in another TU, we can't tell. */
3175 if (DECL_EXTERNAL (var))
3176 return false;
3177 /* If it has a non-trivial destructor, registering the destructor
3178 counts as dynamic initialization. */
3179 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3180 return false;
3181 /* If it's in this TU, its initializer has been processed, unless
3182 it's a case of self-initialization, then DECL_INITIALIZED_P is
3183 false while the initializer is handled by finish_id_expression. */
3184 if (!DECL_INITIALIZED_P (var))
3185 return false;
3186 /* If it has no initializer or a constant one, it's not dynamic. */
3187 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3188 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3191 /* Returns true iff VAR is a variable that needs uses to be
3192 wrapped for possible dynamic initialization. */
3194 static bool
3195 var_needs_tls_wrapper (tree var)
3197 return (!error_operand_p (var)
3198 && CP_DECL_THREAD_LOCAL_P (var)
3199 && !DECL_GNU_TLS_P (var)
3200 && !DECL_FUNCTION_SCOPE_P (var)
3201 && !var_defined_without_dynamic_init (var));
3204 /* Get the FUNCTION_DECL for the shared TLS init function for this
3205 translation unit. */
3207 static tree
3208 get_local_tls_init_fn (void)
3210 tree sname = get_identifier ("__tls_init");
3211 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3212 if (!fn)
3214 fn = build_lang_decl (FUNCTION_DECL, sname,
3215 build_function_type (void_type_node,
3216 void_list_node));
3217 SET_DECL_LANGUAGE (fn, lang_c);
3218 TREE_PUBLIC (fn) = false;
3219 DECL_ARTIFICIAL (fn) = true;
3220 mark_used (fn);
3221 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3223 return fn;
3226 /* Get a FUNCTION_DECL for the init function for the thread_local
3227 variable VAR. The init function will be an alias to the function
3228 that initializes all the non-local TLS variables in the translation
3229 unit. The init function is only used by the wrapper function. */
3231 static tree
3232 get_tls_init_fn (tree var)
3234 /* Only C++11 TLS vars need this init fn. */
3235 if (!var_needs_tls_wrapper (var))
3236 return NULL_TREE;
3238 /* If -fno-extern-tls-init, assume that we don't need to call
3239 a tls init function for a variable defined in another TU. */
3240 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3241 return NULL_TREE;
3243 #ifdef ASM_OUTPUT_DEF
3244 /* If the variable is internal, or if we can't generate aliases,
3245 call the local init function directly. */
3246 if (!TREE_PUBLIC (var))
3247 #endif
3248 return get_local_tls_init_fn ();
3250 tree sname = mangle_tls_init_fn (var);
3251 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3252 if (!fn)
3254 fn = build_lang_decl (FUNCTION_DECL, sname,
3255 build_function_type (void_type_node,
3256 void_list_node));
3257 SET_DECL_LANGUAGE (fn, lang_c);
3258 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3259 DECL_ARTIFICIAL (fn) = true;
3260 DECL_COMDAT (fn) = DECL_COMDAT (var);
3261 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3262 if (DECL_ONE_ONLY (var))
3263 make_decl_one_only (fn, cxx_comdat_group (fn));
3264 if (TREE_PUBLIC (var))
3266 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3267 /* If the variable is defined somewhere else and might have static
3268 initialization, make the init function a weak reference. */
3269 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3270 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3271 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3272 && DECL_EXTERNAL (var))
3273 declare_weak (fn);
3274 else
3275 DECL_WEAK (fn) = DECL_WEAK (var);
3277 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3278 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3279 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3280 DECL_IGNORED_P (fn) = 1;
3281 mark_used (fn);
3283 DECL_BEFRIENDING_CLASSES (fn) = var;
3285 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3287 return fn;
3290 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3291 variable VAR. The wrapper function calls the init function (if any) for
3292 VAR and then returns a reference to VAR. The wrapper function is used
3293 in place of VAR everywhere VAR is mentioned. */
3295 tree
3296 get_tls_wrapper_fn (tree var)
3298 /* Only C++11 TLS vars need this wrapper fn. */
3299 if (!var_needs_tls_wrapper (var))
3300 return NULL_TREE;
3302 tree sname = mangle_tls_wrapper_fn (var);
3303 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3304 if (!fn)
3306 /* A named rvalue reference is an lvalue, so the wrapper should
3307 always return an lvalue reference. */
3308 tree type = non_reference (TREE_TYPE (var));
3309 type = build_reference_type (type);
3310 tree fntype = build_function_type (type, void_list_node);
3311 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3312 SET_DECL_LANGUAGE (fn, lang_c);
3313 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3314 DECL_ARTIFICIAL (fn) = true;
3315 DECL_IGNORED_P (fn) = 1;
3316 /* The wrapper is inline and emitted everywhere var is used. */
3317 DECL_DECLARED_INLINE_P (fn) = true;
3318 if (TREE_PUBLIC (var))
3320 comdat_linkage (fn);
3321 #ifdef HAVE_GAS_HIDDEN
3322 /* Make the wrapper bind locally; there's no reason to share
3323 the wrapper between multiple shared objects. */
3324 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3325 DECL_VISIBILITY_SPECIFIED (fn) = true;
3326 #endif
3328 if (!TREE_PUBLIC (fn))
3329 DECL_INTERFACE_KNOWN (fn) = true;
3330 mark_used (fn);
3331 note_vague_linkage_fn (fn);
3333 #if 0
3334 /* We want CSE to commonize calls to the wrapper, but marking it as
3335 pure is unsafe since it has side-effects. I guess we need a new
3336 ECF flag even weaker than ECF_PURE. FIXME! */
3337 DECL_PURE_P (fn) = true;
3338 #endif
3340 DECL_BEFRIENDING_CLASSES (fn) = var;
3342 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3344 return fn;
3347 /* At EOF, generate the definition for the TLS wrapper function FN:
3349 T& var_wrapper() {
3350 if (init_fn) init_fn();
3351 return var;
3352 } */
3354 static void
3355 generate_tls_wrapper (tree fn)
3357 tree var = DECL_BEFRIENDING_CLASSES (fn);
3359 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3360 tree body = begin_function_body ();
3361 /* Only call the init fn if there might be one. */
3362 if (tree init_fn = get_tls_init_fn (var))
3364 tree if_stmt = NULL_TREE;
3365 /* If init_fn is a weakref, make sure it exists before calling. */
3366 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3368 if_stmt = begin_if_stmt ();
3369 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3370 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3371 NE_EXPR, addr, nullptr_node,
3372 tf_warning_or_error);
3373 finish_if_stmt_cond (cond, if_stmt);
3375 finish_expr_stmt (build_cxx_call
3376 (init_fn, 0, NULL, tf_warning_or_error));
3377 if (if_stmt)
3379 finish_then_clause (if_stmt);
3380 finish_if_stmt (if_stmt);
3383 else
3384 /* If there's no initialization, the wrapper is a constant function. */
3385 TREE_READONLY (fn) = true;
3386 finish_return_stmt (convert_from_reference (var));
3387 finish_function_body (body);
3388 expand_or_defer_fn (finish_function (0));
3391 /* Start the process of running a particular set of global constructors
3392 or destructors. Subroutine of do_[cd]tors. Also called from
3393 vtv_start_verification_constructor_init_function. */
3395 static tree
3396 start_objects (int method_type, int initp)
3398 tree body;
3399 tree fndecl;
3400 char type[14];
3402 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3404 if (initp != DEFAULT_INIT_PRIORITY)
3406 char joiner;
3408 #ifdef JOINER
3409 joiner = JOINER;
3410 #else
3411 joiner = '_';
3412 #endif
3414 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3416 else
3417 sprintf (type, "sub_%c", method_type);
3419 fndecl = build_lang_decl (FUNCTION_DECL,
3420 get_file_function_name (type),
3421 build_function_type_list (void_type_node,
3422 NULL_TREE));
3423 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3425 TREE_PUBLIC (current_function_decl) = 0;
3427 /* Mark as artificial because it's not explicitly in the user's
3428 source code. */
3429 DECL_ARTIFICIAL (current_function_decl) = 1;
3431 /* Mark this declaration as used to avoid spurious warnings. */
3432 TREE_USED (current_function_decl) = 1;
3434 /* Mark this function as a global constructor or destructor. */
3435 if (method_type == 'I')
3436 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3437 else
3438 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3440 body = begin_compound_stmt (BCS_FN_BODY);
3442 return body;
3445 /* Finish the process of running a particular set of global constructors
3446 or destructors. Subroutine of do_[cd]tors. */
3448 static void
3449 finish_objects (int method_type, int initp, tree body)
3451 tree fn;
3453 /* Finish up. */
3454 finish_compound_stmt (body);
3455 fn = finish_function (0);
3457 if (method_type == 'I')
3459 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3460 decl_init_priority_insert (fn, initp);
3462 else
3464 DECL_STATIC_DESTRUCTOR (fn) = 1;
3465 decl_fini_priority_insert (fn, initp);
3468 expand_or_defer_fn (fn);
3471 /* The names of the parameters to the function created to handle
3472 initializations and destructions for objects with static storage
3473 duration. */
3474 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3475 #define PRIORITY_IDENTIFIER "__priority"
3477 /* The name of the function we create to handle initializations and
3478 destructions for objects with static storage duration. */
3479 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3481 /* The declaration for the __INITIALIZE_P argument. */
3482 static GTY(()) tree initialize_p_decl;
3484 /* The declaration for the __PRIORITY argument. */
3485 static GTY(()) tree priority_decl;
3487 /* The declaration for the static storage duration function. */
3488 static GTY(()) tree ssdf_decl;
3490 /* All the static storage duration functions created in this
3491 translation unit. */
3492 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3494 /* A map from priority levels to information about that priority
3495 level. There may be many such levels, so efficient lookup is
3496 important. */
3497 static splay_tree priority_info_map;
3499 /* Begins the generation of the function that will handle all
3500 initialization and destruction of objects with static storage
3501 duration. The function generated takes two parameters of type
3502 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3503 nonzero, it performs initializations. Otherwise, it performs
3504 destructions. It only performs those initializations or
3505 destructions with the indicated __PRIORITY. The generated function
3506 returns no value.
3508 It is assumed that this function will only be called once per
3509 translation unit. */
3511 static tree
3512 start_static_storage_duration_function (unsigned count)
3514 tree type;
3515 tree body;
3516 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3518 /* Create the identifier for this function. It will be of the form
3519 SSDF_IDENTIFIER_<number>. */
3520 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3522 type = build_function_type_list (void_type_node,
3523 integer_type_node, integer_type_node,
3524 NULL_TREE);
3526 /* Create the FUNCTION_DECL itself. */
3527 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3528 get_identifier (id),
3529 type);
3530 TREE_PUBLIC (ssdf_decl) = 0;
3531 DECL_ARTIFICIAL (ssdf_decl) = 1;
3533 /* Put this function in the list of functions to be called from the
3534 static constructors and destructors. */
3535 if (!ssdf_decls)
3537 vec_alloc (ssdf_decls, 32);
3539 /* Take this opportunity to initialize the map from priority
3540 numbers to information about that priority level. */
3541 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3542 /*delete_key_fn=*/0,
3543 /*delete_value_fn=*/
3544 (splay_tree_delete_value_fn) &free);
3546 /* We always need to generate functions for the
3547 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3548 priorities later, we'll be sure to find the
3549 DEFAULT_INIT_PRIORITY. */
3550 get_priority_info (DEFAULT_INIT_PRIORITY);
3553 vec_safe_push (ssdf_decls, ssdf_decl);
3555 /* Create the argument list. */
3556 initialize_p_decl = cp_build_parm_decl
3557 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3558 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3559 TREE_USED (initialize_p_decl) = 1;
3560 priority_decl = cp_build_parm_decl
3561 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3562 DECL_CONTEXT (priority_decl) = ssdf_decl;
3563 TREE_USED (priority_decl) = 1;
3565 DECL_CHAIN (initialize_p_decl) = priority_decl;
3566 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3568 /* Put the function in the global scope. */
3569 pushdecl (ssdf_decl);
3571 /* Start the function itself. This is equivalent to declaring the
3572 function as:
3574 static void __ssdf (int __initialize_p, init __priority_p);
3576 It is static because we only need to call this function from the
3577 various constructor and destructor functions for this module. */
3578 start_preparsed_function (ssdf_decl,
3579 /*attrs=*/NULL_TREE,
3580 SF_PRE_PARSED);
3582 /* Set up the scope of the outermost block in the function. */
3583 body = begin_compound_stmt (BCS_FN_BODY);
3585 return body;
3588 /* Finish the generation of the function which performs initialization
3589 and destruction of objects with static storage duration. After
3590 this point, no more such objects can be created. */
3592 static void
3593 finish_static_storage_duration_function (tree body)
3595 /* Close out the function. */
3596 finish_compound_stmt (body);
3597 expand_or_defer_fn (finish_function (0));
3600 /* Return the information about the indicated PRIORITY level. If no
3601 code to handle this level has yet been generated, generate the
3602 appropriate prologue. */
3604 static priority_info
3605 get_priority_info (int priority)
3607 priority_info pi;
3608 splay_tree_node n;
3610 n = splay_tree_lookup (priority_info_map,
3611 (splay_tree_key) priority);
3612 if (!n)
3614 /* Create a new priority information structure, and insert it
3615 into the map. */
3616 pi = XNEW (struct priority_info_s);
3617 pi->initializations_p = 0;
3618 pi->destructions_p = 0;
3619 splay_tree_insert (priority_info_map,
3620 (splay_tree_key) priority,
3621 (splay_tree_value) pi);
3623 else
3624 pi = (priority_info) n->value;
3626 return pi;
3629 /* The effective initialization priority of a DECL. */
3631 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3632 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3633 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3635 /* Whether a DECL needs a guard to protect it against multiple
3636 initialization. */
3638 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3639 || DECL_ONE_ONLY (decl) \
3640 || DECL_WEAK (decl)))
3642 /* Called from one_static_initialization_or_destruction(),
3643 via walk_tree.
3644 Walks the initializer list of a global variable and looks for
3645 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3646 and that have their DECL_CONTEXT() == NULL.
3647 For each such temporary variable, set their DECL_CONTEXT() to
3648 the current function. This is necessary because otherwise
3649 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3650 when trying to refer to a temporary variable that does not have
3651 it's DECL_CONTECT() properly set. */
3652 static tree
3653 fix_temporary_vars_context_r (tree *node,
3654 int * /*unused*/,
3655 void * /*unused1*/)
3657 gcc_assert (current_function_decl);
3659 if (TREE_CODE (*node) == BIND_EXPR)
3661 tree var;
3663 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3664 if (VAR_P (var)
3665 && !DECL_NAME (var)
3666 && DECL_ARTIFICIAL (var)
3667 && !DECL_CONTEXT (var))
3668 DECL_CONTEXT (var) = current_function_decl;
3671 return NULL_TREE;
3674 /* Set up to handle the initialization or destruction of DECL. If
3675 INITP is nonzero, we are initializing the variable. Otherwise, we
3676 are destroying it. */
3678 static void
3679 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3681 tree guard_if_stmt = NULL_TREE;
3682 tree guard;
3684 /* If we are supposed to destruct and there's a trivial destructor,
3685 nothing has to be done. */
3686 if (!initp
3687 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3688 return;
3690 /* Trick the compiler into thinking we are at the file and line
3691 where DECL was declared so that error-messages make sense, and so
3692 that the debugger will show somewhat sensible file and line
3693 information. */
3694 input_location = DECL_SOURCE_LOCATION (decl);
3696 /* Make sure temporary variables in the initialiser all have
3697 their DECL_CONTEXT() set to a value different from NULL_TREE.
3698 This can happen when global variables initialisers are built.
3699 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3700 the temporary variables that might have been generated in the
3701 accompagning initialisers is NULL_TREE, meaning the variables have been
3702 declared in the global namespace.
3703 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3704 of the temporaries are set to the current function decl. */
3705 cp_walk_tree_without_duplicates (&init,
3706 fix_temporary_vars_context_r,
3707 NULL);
3709 /* Because of:
3711 [class.access.spec]
3713 Access control for implicit calls to the constructors,
3714 the conversion functions, or the destructor called to
3715 create and destroy a static data member is performed as
3716 if these calls appeared in the scope of the member's
3717 class.
3719 we pretend we are in a static member function of the class of
3720 which the DECL is a member. */
3721 if (member_p (decl))
3723 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3724 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3727 /* Assume we don't need a guard. */
3728 guard = NULL_TREE;
3729 /* We need a guard if this is an object with external linkage that
3730 might be initialized in more than one place. (For example, a
3731 static data member of a template, when the data member requires
3732 construction.) */
3733 if (NEEDS_GUARD_P (decl))
3735 tree guard_cond;
3737 guard = get_guard (decl);
3739 /* When using __cxa_atexit, we just check the GUARD as we would
3740 for a local static. */
3741 if (flag_use_cxa_atexit)
3743 /* When using __cxa_atexit, we never try to destroy
3744 anything from a static destructor. */
3745 gcc_assert (initp);
3746 guard_cond = get_guard_cond (guard, false);
3748 /* If we don't have __cxa_atexit, then we will be running
3749 destructors from .fini sections, or their equivalents. So,
3750 we need to know how many times we've tried to initialize this
3751 object. We do initializations only if the GUARD is zero,
3752 i.e., if we are the first to initialize the variable. We do
3753 destructions only if the GUARD is one, i.e., if we are the
3754 last to destroy the variable. */
3755 else if (initp)
3756 guard_cond
3757 = cp_build_binary_op (input_location,
3758 EQ_EXPR,
3759 cp_build_unary_op (PREINCREMENT_EXPR,
3760 guard,
3761 /*noconvert=*/1,
3762 tf_warning_or_error),
3763 integer_one_node,
3764 tf_warning_or_error);
3765 else
3766 guard_cond
3767 = cp_build_binary_op (input_location,
3768 EQ_EXPR,
3769 cp_build_unary_op (PREDECREMENT_EXPR,
3770 guard,
3771 /*noconvert=*/1,
3772 tf_warning_or_error),
3773 integer_zero_node,
3774 tf_warning_or_error);
3776 guard_if_stmt = begin_if_stmt ();
3777 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3781 /* If we're using __cxa_atexit, we have not already set the GUARD,
3782 so we must do so now. */
3783 if (guard && initp && flag_use_cxa_atexit)
3784 finish_expr_stmt (set_guard (guard));
3786 /* Perform the initialization or destruction. */
3787 if (initp)
3789 if (init)
3791 finish_expr_stmt (init);
3792 if (flag_sanitize & SANITIZE_ADDRESS)
3794 varpool_node *vnode = varpool_node::get (decl);
3795 if (vnode)
3796 vnode->dynamically_initialized = 1;
3800 /* If we're using __cxa_atexit, register a function that calls the
3801 destructor for the object. */
3802 if (flag_use_cxa_atexit)
3803 finish_expr_stmt (register_dtor_fn (decl));
3805 else
3806 finish_expr_stmt (build_cleanup (decl));
3808 /* Finish the guard if-stmt, if necessary. */
3809 if (guard)
3811 finish_then_clause (guard_if_stmt);
3812 finish_if_stmt (guard_if_stmt);
3815 /* Now that we're done with DECL we don't need to pretend to be a
3816 member of its class any longer. */
3817 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3818 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3821 /* Generate code to do the initialization or destruction of the decls in VARS,
3822 a TREE_LIST of VAR_DECL with static storage duration.
3823 Whether initialization or destruction is performed is specified by INITP. */
3825 static void
3826 do_static_initialization_or_destruction (tree vars, bool initp)
3828 tree node, init_if_stmt, cond;
3830 /* Build the outer if-stmt to check for initialization or destruction. */
3831 init_if_stmt = begin_if_stmt ();
3832 cond = initp ? integer_one_node : integer_zero_node;
3833 cond = cp_build_binary_op (input_location,
3834 EQ_EXPR,
3835 initialize_p_decl,
3836 cond,
3837 tf_warning_or_error);
3838 finish_if_stmt_cond (cond, init_if_stmt);
3840 /* To make sure dynamic construction doesn't access globals from other
3841 compilation units where they might not be yet constructed, for
3842 -fsanitize=address insert __asan_before_dynamic_init call that
3843 prevents access to either all global variables that need construction
3844 in other compilation units, or at least those that haven't been
3845 initialized yet. Variables that need dynamic construction in
3846 the current compilation unit are kept accessible. */
3847 if (flag_sanitize & SANITIZE_ADDRESS)
3848 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3850 node = vars;
3851 do {
3852 tree decl = TREE_VALUE (node);
3853 tree priority_if_stmt;
3854 int priority;
3855 priority_info pi;
3857 /* If we don't need a destructor, there's nothing to do. Avoid
3858 creating a possibly empty if-stmt. */
3859 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3861 node = TREE_CHAIN (node);
3862 continue;
3865 /* Remember that we had an initialization or finalization at this
3866 priority. */
3867 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3868 pi = get_priority_info (priority);
3869 if (initp)
3870 pi->initializations_p = 1;
3871 else
3872 pi->destructions_p = 1;
3874 /* Conditionalize this initialization on being in the right priority
3875 and being initializing/finalizing appropriately. */
3876 priority_if_stmt = begin_if_stmt ();
3877 cond = cp_build_binary_op (input_location,
3878 EQ_EXPR,
3879 priority_decl,
3880 build_int_cst (NULL_TREE, priority),
3881 tf_warning_or_error);
3882 finish_if_stmt_cond (cond, priority_if_stmt);
3884 /* Process initializers with same priority. */
3885 for (; node
3886 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3887 node = TREE_CHAIN (node))
3888 /* Do one initialization or destruction. */
3889 one_static_initialization_or_destruction (TREE_VALUE (node),
3890 TREE_PURPOSE (node), initp);
3892 /* Finish up the priority if-stmt body. */
3893 finish_then_clause (priority_if_stmt);
3894 finish_if_stmt (priority_if_stmt);
3896 } while (node);
3898 /* Revert what __asan_before_dynamic_init did by calling
3899 __asan_after_dynamic_init. */
3900 if (flag_sanitize & SANITIZE_ADDRESS)
3901 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3903 /* Finish up the init/destruct if-stmt body. */
3904 finish_then_clause (init_if_stmt);
3905 finish_if_stmt (init_if_stmt);
3908 /* VARS is a list of variables with static storage duration which may
3909 need initialization and/or finalization. Remove those variables
3910 that don't really need to be initialized or finalized, and return
3911 the resulting list. The order in which the variables appear in
3912 VARS is in reverse order of the order in which they should actually
3913 be initialized. The list we return is in the unreversed order;
3914 i.e., the first variable should be initialized first. */
3916 static tree
3917 prune_vars_needing_no_initialization (tree *vars)
3919 tree *var = vars;
3920 tree result = NULL_TREE;
3922 while (*var)
3924 tree t = *var;
3925 tree decl = TREE_VALUE (t);
3926 tree init = TREE_PURPOSE (t);
3928 /* Deal gracefully with error. */
3929 if (decl == error_mark_node)
3931 var = &TREE_CHAIN (t);
3932 continue;
3935 /* The only things that can be initialized are variables. */
3936 gcc_assert (VAR_P (decl));
3938 /* If this object is not defined, we don't need to do anything
3939 here. */
3940 if (DECL_EXTERNAL (decl))
3942 var = &TREE_CHAIN (t);
3943 continue;
3946 /* Also, if the initializer already contains errors, we can bail
3947 out now. */
3948 if (init && TREE_CODE (init) == TREE_LIST
3949 && value_member (error_mark_node, init))
3951 var = &TREE_CHAIN (t);
3952 continue;
3955 /* This variable is going to need initialization and/or
3956 finalization, so we add it to the list. */
3957 *var = TREE_CHAIN (t);
3958 TREE_CHAIN (t) = result;
3959 result = t;
3962 return result;
3965 /* Make sure we have told the back end about all the variables in
3966 VARS. */
3968 static void
3969 write_out_vars (tree vars)
3971 tree v;
3973 for (v = vars; v; v = TREE_CHAIN (v))
3975 tree var = TREE_VALUE (v);
3976 if (!var_finalized_p (var))
3978 import_export_decl (var);
3979 rest_of_decl_compilation (var, 1, 1);
3984 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3985 (otherwise) that will initialize all global objects with static
3986 storage duration having the indicated PRIORITY. */
3988 static void
3989 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3990 location_t *locus)
3992 char function_key;
3993 tree fndecl;
3994 tree body;
3995 size_t i;
3997 input_location = *locus;
3998 /* ??? */
3999 /* Was: locus->line++; */
4001 /* We use `I' to indicate initialization and `D' to indicate
4002 destruction. */
4003 function_key = constructor_p ? 'I' : 'D';
4005 /* We emit the function lazily, to avoid generating empty
4006 global constructors and destructors. */
4007 body = NULL_TREE;
4009 /* For Objective-C++, we may need to initialize metadata found in this module.
4010 This must be done _before_ any other static initializations. */
4011 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4012 && constructor_p && objc_static_init_needed_p ())
4014 body = start_objects (function_key, priority);
4015 objc_generate_static_init_call (NULL_TREE);
4018 /* Call the static storage duration function with appropriate
4019 arguments. */
4020 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4022 /* Calls to pure or const functions will expand to nothing. */
4023 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4025 tree call;
4027 if (! body)
4028 body = start_objects (function_key, priority);
4030 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4031 build_int_cst (NULL_TREE,
4032 constructor_p),
4033 build_int_cst (NULL_TREE,
4034 priority),
4035 NULL_TREE);
4036 finish_expr_stmt (call);
4040 /* Close out the function. */
4041 if (body)
4042 finish_objects (function_key, priority, body);
4045 /* Generate constructor and destructor functions for the priority
4046 indicated by N. */
4048 static int
4049 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4051 location_t *locus = (location_t *) data;
4052 int priority = (int) n->key;
4053 priority_info pi = (priority_info) n->value;
4055 /* Generate the functions themselves, but only if they are really
4056 needed. */
4057 if (pi->initializations_p)
4058 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4059 if (pi->destructions_p)
4060 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4062 /* Keep iterating. */
4063 return 0;
4066 /* Java requires that we be able to reference a local address for a
4067 method, and not be confused by PLT entries. If supported, create a
4068 hidden alias for all such methods. */
4070 static void
4071 build_java_method_aliases (void)
4073 #ifndef HAVE_GAS_HIDDEN
4074 return;
4075 #endif
4077 struct cgraph_node *node;
4078 FOR_EACH_FUNCTION (node)
4080 tree fndecl = node->decl;
4082 if (DECL_CLASS_SCOPE_P (fndecl)
4083 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4084 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4086 /* Mangle the name in a predictable way; we need to reference
4087 this from a java compiled object file. */
4088 tree oid = DECL_ASSEMBLER_NAME (fndecl);
4089 const char *oname = IDENTIFIER_POINTER (oid);
4090 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4091 char *nname = ACONCAT (("_ZGA", oname + 2, NULL));
4093 tree alias = make_alias_for (fndecl, get_identifier (nname));
4094 TREE_PUBLIC (alias) = 1;
4095 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4097 cgraph_node::create_same_body_alias (alias, fndecl);
4102 /* Return C++ property of T, based on given operation OP. */
4104 static int
4105 cpp_check (tree t, cpp_operation op)
4107 switch (op)
4109 case HAS_DEPENDENT_TEMPLATE_ARGS:
4111 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4112 if (!ti)
4113 return 0;
4114 ++processing_template_decl;
4115 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4116 --processing_template_decl;
4117 return dep;
4119 case IS_ABSTRACT:
4120 return DECL_PURE_VIRTUAL_P (t);
4121 case IS_CONSTRUCTOR:
4122 return DECL_CONSTRUCTOR_P (t);
4123 case IS_DESTRUCTOR:
4124 return DECL_DESTRUCTOR_P (t);
4125 case IS_COPY_CONSTRUCTOR:
4126 return DECL_COPY_CONSTRUCTOR_P (t);
4127 case IS_MOVE_CONSTRUCTOR:
4128 return DECL_MOVE_CONSTRUCTOR_P (t);
4129 case IS_TEMPLATE:
4130 return TREE_CODE (t) == TEMPLATE_DECL;
4131 case IS_TRIVIAL:
4132 return trivial_type_p (t);
4133 default:
4134 return 0;
4138 /* Collect source file references recursively, starting from NAMESPC. */
4140 static void
4141 collect_source_refs (tree namespc)
4143 tree t;
4145 if (!namespc)
4146 return;
4148 /* Iterate over names in this name space. */
4149 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4150 if (!DECL_IS_BUILTIN (t) )
4151 collect_source_ref (DECL_SOURCE_FILE (t));
4153 /* Dump siblings, if any */
4154 collect_source_refs (TREE_CHAIN (namespc));
4156 /* Dump children, if any */
4157 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4160 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4161 starting from NAMESPC. */
4163 static void
4164 collect_ada_namespace (tree namespc, const char *source_file)
4166 if (!namespc)
4167 return;
4169 /* Collect decls from this namespace */
4170 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4172 /* Collect siblings, if any */
4173 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4175 /* Collect children, if any */
4176 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4179 /* Returns true iff there is a definition available for variable or
4180 function DECL. */
4182 static bool
4183 decl_defined_p (tree decl)
4185 if (TREE_CODE (decl) == FUNCTION_DECL)
4186 return (DECL_INITIAL (decl) != NULL_TREE);
4187 else
4189 gcc_assert (VAR_P (decl));
4190 return !DECL_EXTERNAL (decl);
4194 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4196 [expr.const]
4198 An integral constant-expression can only involve ... const
4199 variables of integral or enumeration types initialized with
4200 constant expressions ...
4202 C++0x also allows constexpr variables and temporaries initialized
4203 with constant expressions. We handle the former here, but the latter
4204 are just folded away in cxx_eval_constant_expression.
4206 The standard does not require that the expression be non-volatile.
4207 G++ implements the proposed correction in DR 457. */
4209 bool
4210 decl_constant_var_p (tree decl)
4212 if (!decl_maybe_constant_var_p (decl))
4213 return false;
4215 /* We don't know if a template static data member is initialized with
4216 a constant expression until we instantiate its initializer. Even
4217 in the case of a constexpr variable, we can't treat it as a
4218 constant until its initializer is complete in case it's used in
4219 its own initializer. */
4220 mark_used (decl);
4221 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4224 /* Returns true if DECL could be a symbolic constant variable, depending on
4225 its initializer. */
4227 bool
4228 decl_maybe_constant_var_p (tree decl)
4230 tree type = TREE_TYPE (decl);
4231 if (!VAR_P (decl))
4232 return false;
4233 if (DECL_DECLARED_CONSTEXPR_P (decl))
4234 return true;
4235 if (DECL_HAS_VALUE_EXPR_P (decl))
4236 /* A proxy isn't constant. */
4237 return false;
4238 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4239 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4242 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4243 called from grokfndecl and grokvardecl; in all modes it is called from
4244 cp_write_global_declarations. */
4246 void
4247 no_linkage_error (tree decl)
4249 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4250 /* In C++11 it's ok if the decl is defined. */
4251 return;
4252 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4253 if (t == NULL_TREE)
4254 /* The type that got us on no_linkage_decls must have gotten a name for
4255 linkage purposes. */;
4256 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4257 /* The type might end up having a typedef name for linkage purposes. */
4258 vec_safe_push (no_linkage_decls, decl);
4259 else if (TYPE_ANONYMOUS_P (t))
4261 bool d = false;
4262 if (cxx_dialect >= cxx11)
4263 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4264 "anonymous type, is used but never defined", decl);
4265 else if (DECL_EXTERN_C_P (decl))
4266 /* Allow this; it's pretty common in C. */;
4267 else if (VAR_P (decl))
4268 /* DRs 132, 319 and 389 seem to indicate types with
4269 no linkage can only be used to declare extern "C"
4270 entities. Since it's not always an error in the
4271 ISO C++ 90 Standard, we only issue a warning. */
4272 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4273 "with no linkage used to declare variable %q#D with "
4274 "linkage", decl);
4275 else
4276 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4277 "linkage used to declare function %q#D with linkage",
4278 decl);
4279 if (d && is_typedef_decl (TYPE_NAME (t)))
4280 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4281 "to the unqualified type, so it is not used for linkage",
4282 TYPE_NAME (t));
4284 else if (cxx_dialect >= cxx11)
4286 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4287 permerror (DECL_SOURCE_LOCATION (decl),
4288 "%q#D, declared using local type "
4289 "%qT, is used but never defined", decl, t);
4291 else if (VAR_P (decl))
4292 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4293 "used to declare variable %q#D with linkage", t, decl);
4294 else
4295 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4296 "to declare function %q#D with linkage", t, decl);
4299 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4301 static void
4302 collect_all_refs (const char *source_file)
4304 collect_ada_namespace (global_namespace, source_file);
4307 /* Clear DECL_EXTERNAL for NODE. */
4309 static bool
4310 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4312 DECL_EXTERNAL (node->decl) = 0;
4313 return false;
4316 /* Build up the function to run dynamic initializers for thread_local
4317 variables in this translation unit and alias the init functions for the
4318 individual variables to it. */
4320 static void
4321 handle_tls_init (void)
4323 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4324 if (vars == NULL_TREE)
4325 return;
4327 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4329 write_out_vars (vars);
4331 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4332 boolean_type_node);
4333 TREE_PUBLIC (guard) = false;
4334 TREE_STATIC (guard) = true;
4335 DECL_ARTIFICIAL (guard) = true;
4336 DECL_IGNORED_P (guard) = true;
4337 TREE_USED (guard) = true;
4338 CP_DECL_THREAD_LOCAL_P (guard) = true;
4339 set_decl_tls_model (guard, decl_default_tls_model (guard));
4340 pushdecl_top_level_and_finish (guard, NULL_TREE);
4342 tree fn = get_local_tls_init_fn ();
4343 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4344 tree body = begin_function_body ();
4345 tree if_stmt = begin_if_stmt ();
4346 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4347 tf_warning_or_error);
4348 finish_if_stmt_cond (cond, if_stmt);
4349 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4350 boolean_true_node,
4351 tf_warning_or_error));
4352 for (; vars; vars = TREE_CHAIN (vars))
4354 tree var = TREE_VALUE (vars);
4355 tree init = TREE_PURPOSE (vars);
4356 one_static_initialization_or_destruction (var, init, true);
4358 #ifdef ASM_OUTPUT_DEF
4359 /* Output init aliases even with -fno-extern-tls-init. */
4360 if (TREE_PUBLIC (var))
4362 tree single_init_fn = get_tls_init_fn (var);
4363 if (single_init_fn == NULL_TREE)
4364 continue;
4365 cgraph_node *alias
4366 = cgraph_node::get_create (fn)->create_same_body_alias
4367 (single_init_fn, fn);
4368 gcc_assert (alias != NULL);
4370 #endif
4373 finish_then_clause (if_stmt);
4374 finish_if_stmt (if_stmt);
4375 finish_function_body (body);
4376 expand_or_defer_fn (finish_function (0));
4379 /* We're at the end of compilation, so generate any mangling aliases that
4380 we've been saving up, if DECL is going to be output and ID2 isn't
4381 already taken by another declaration. */
4383 static void
4384 generate_mangling_alias (tree decl, tree id2)
4386 /* If there's a declaration already using this mangled name,
4387 don't create a compatibility alias that conflicts. */
4388 if (IDENTIFIER_GLOBAL_VALUE (id2))
4389 return;
4391 struct cgraph_node *n = NULL;
4392 if (TREE_CODE (decl) == FUNCTION_DECL
4393 && !(n = cgraph_node::get (decl)))
4394 /* Don't create an alias to an unreferenced function. */
4395 return;
4397 tree alias = make_alias_for (decl, id2);
4398 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4399 DECL_IGNORED_P (alias) = 1;
4400 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4401 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4402 if (vague_linkage_p (decl))
4403 DECL_WEAK (alias) = 1;
4404 if (TREE_CODE (decl) == FUNCTION_DECL)
4405 n->create_same_body_alias (alias, decl);
4406 else
4407 varpool_node::create_extra_name_alias (alias, decl);
4410 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4411 the end of translation, for compatibility across bugs in the mangling
4412 implementation. */
4414 void
4415 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4417 #ifdef ASM_OUTPUT_DEF
4418 if (!defer_mangling_aliases)
4419 generate_mangling_alias (decl, id2);
4420 else
4422 vec_safe_push (mangling_aliases, decl);
4423 vec_safe_push (mangling_aliases, id2);
4425 #endif
4428 /* Emit all mangling aliases that were deferred up to this point. */
4430 void
4431 generate_mangling_aliases ()
4433 while (!vec_safe_is_empty (mangling_aliases))
4435 tree id2 = mangling_aliases->pop();
4436 tree decl = mangling_aliases->pop();
4437 generate_mangling_alias (decl, id2);
4439 defer_mangling_aliases = false;
4442 /* The entire file is now complete. If requested, dump everything
4443 to a file. */
4445 static void
4446 dump_tu (void)
4448 int flags;
4449 FILE *stream = dump_begin (TDI_tu, &flags);
4451 if (stream)
4453 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4454 dump_end (TDI_tu, stream);
4458 static location_t locus_at_end_of_parsing;
4460 /* Check the deallocation functions for CODE to see if we want to warn that
4461 only one was defined. */
4463 static void
4464 maybe_warn_sized_delete (enum tree_code code)
4466 tree sized = NULL_TREE;
4467 tree unsized = NULL_TREE;
4469 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4470 ovl; ovl = OVL_NEXT (ovl))
4472 tree fn = OVL_CURRENT (ovl);
4473 /* We're only interested in usual deallocation functions. */
4474 if (!non_placement_deallocation_fn_p (fn))
4475 continue;
4476 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4477 unsized = fn;
4478 else
4479 sized = fn;
4481 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4482 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4483 "the program should also define %qD", sized);
4484 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4485 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4486 "the program should also define %qD", unsized);
4489 /* Check the global deallocation functions to see if we want to warn about
4490 defining unsized without sized (or vice versa). */
4492 static void
4493 maybe_warn_sized_delete ()
4495 if (!flag_sized_deallocation || !warn_sized_deallocation)
4496 return;
4497 maybe_warn_sized_delete (DELETE_EXPR);
4498 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4501 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4502 look them up when evaluating non-type template parameters. Now we need to
4503 lower them to something the back end can understand. */
4505 static void
4506 lower_var_init ()
4508 varpool_node *node;
4509 FOR_EACH_VARIABLE (node)
4511 tree d = node->decl;
4512 if (tree init = DECL_INITIAL (d))
4513 DECL_INITIAL (d) = cplus_expand_constant (init);
4517 /* This routine is called at the end of compilation.
4518 Its job is to create all the code needed to initialize and
4519 destroy the global aggregates. We do the destruction
4520 first, since that way we only need to reverse the decls once. */
4522 void
4523 c_parse_final_cleanups (void)
4525 tree vars;
4526 bool reconsider;
4527 size_t i;
4528 unsigned ssdf_count = 0;
4529 int retries = 0;
4530 tree decl;
4532 locus_at_end_of_parsing = input_location;
4533 at_eof = 1;
4535 /* Bad parse errors. Just forget about it. */
4536 if (! global_bindings_p () || current_class_type
4537 || !vec_safe_is_empty (decl_namespace_list))
4538 return;
4540 /* This is the point to write out a PCH if we're doing that.
4541 In that case we do not want to do anything else. */
4542 if (pch_file)
4544 /* Mangle all symbols at PCH creation time. */
4545 symtab_node *node;
4546 FOR_EACH_SYMBOL (node)
4547 if (! is_a <varpool_node *> (node)
4548 || ! DECL_HARD_REGISTER (node->decl))
4549 DECL_ASSEMBLER_NAME (node->decl);
4550 c_common_write_pch ();
4551 dump_tu ();
4552 return;
4555 timevar_stop (TV_PHASE_PARSING);
4556 timevar_start (TV_PHASE_DEFERRED);
4558 symtab->process_same_body_aliases ();
4560 /* Handle -fdump-ada-spec[-slim] */
4561 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4563 if (flag_dump_ada_spec_slim)
4564 collect_source_ref (main_input_filename);
4565 else
4566 collect_source_refs (global_namespace);
4568 dump_ada_specs (collect_all_refs, cpp_check);
4571 /* FIXME - huh? was input_line -= 1;*/
4573 /* We now have to write out all the stuff we put off writing out.
4574 These include:
4576 o Template specializations that we have not yet instantiated,
4577 but which are needed.
4578 o Initialization and destruction for non-local objects with
4579 static storage duration. (Local objects with static storage
4580 duration are initialized when their scope is first entered,
4581 and are cleaned up via atexit.)
4582 o Virtual function tables.
4584 All of these may cause others to be needed. For example,
4585 instantiating one function may cause another to be needed, and
4586 generating the initializer for an object may cause templates to be
4587 instantiated, etc., etc. */
4589 emit_support_tinfos ();
4593 tree t;
4594 tree decl;
4596 reconsider = false;
4598 /* If there are templates that we've put off instantiating, do
4599 them now. */
4600 instantiate_pending_templates (retries);
4601 ggc_collect ();
4603 /* Write out virtual tables as required. Note that writing out
4604 the virtual table for a template class may cause the
4605 instantiation of members of that class. If we write out
4606 vtables then we remove the class from our list so we don't
4607 have to look at it again. */
4609 while (keyed_classes != NULL_TREE
4610 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4612 reconsider = true;
4613 keyed_classes = TREE_CHAIN (keyed_classes);
4616 t = keyed_classes;
4617 if (t != NULL_TREE)
4619 tree next = TREE_CHAIN (t);
4621 while (next)
4623 if (maybe_emit_vtables (TREE_VALUE (next)))
4625 reconsider = true;
4626 TREE_CHAIN (t) = TREE_CHAIN (next);
4628 else
4629 t = next;
4631 next = TREE_CHAIN (t);
4635 /* Write out needed type info variables. We have to be careful
4636 looping through unemitted decls, because emit_tinfo_decl may
4637 cause other variables to be needed. New elements will be
4638 appended, and we remove from the vector those that actually
4639 get emitted. */
4640 for (i = unemitted_tinfo_decls->length ();
4641 unemitted_tinfo_decls->iterate (--i, &t);)
4642 if (emit_tinfo_decl (t))
4644 reconsider = true;
4645 unemitted_tinfo_decls->unordered_remove (i);
4648 /* The list of objects with static storage duration is built up
4649 in reverse order. We clear STATIC_AGGREGATES so that any new
4650 aggregates added during the initialization of these will be
4651 initialized in the correct order when we next come around the
4652 loop. */
4653 vars = prune_vars_needing_no_initialization (&static_aggregates);
4655 if (vars)
4657 /* We need to start a new initialization function each time
4658 through the loop. That's because we need to know which
4659 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4660 isn't computed until a function is finished, and written
4661 out. That's a deficiency in the back end. When this is
4662 fixed, these initialization functions could all become
4663 inline, with resulting performance improvements. */
4664 tree ssdf_body;
4666 /* Set the line and file, so that it is obviously not from
4667 the source file. */
4668 input_location = locus_at_end_of_parsing;
4669 ssdf_body = start_static_storage_duration_function (ssdf_count);
4671 /* Make sure the back end knows about all the variables. */
4672 write_out_vars (vars);
4674 /* First generate code to do all the initializations. */
4675 if (vars)
4676 do_static_initialization_or_destruction (vars, /*initp=*/true);
4678 /* Then, generate code to do all the destructions. Do these
4679 in reverse order so that the most recently constructed
4680 variable is the first destroyed. If we're using
4681 __cxa_atexit, then we don't need to do this; functions
4682 were registered at initialization time to destroy the
4683 local statics. */
4684 if (!flag_use_cxa_atexit && vars)
4686 vars = nreverse (vars);
4687 do_static_initialization_or_destruction (vars, /*initp=*/false);
4689 else
4690 vars = NULL_TREE;
4692 /* Finish up the static storage duration function for this
4693 round. */
4694 input_location = locus_at_end_of_parsing;
4695 finish_static_storage_duration_function (ssdf_body);
4697 /* All those initializations and finalizations might cause
4698 us to need more inline functions, more template
4699 instantiations, etc. */
4700 reconsider = true;
4701 ssdf_count++;
4702 /* ??? was: locus_at_end_of_parsing.line++; */
4705 /* Now do the same for thread_local variables. */
4706 handle_tls_init ();
4708 /* Go through the set of inline functions whose bodies have not
4709 been emitted yet. If out-of-line copies of these functions
4710 are required, emit them. */
4711 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4713 /* Does it need synthesizing? */
4714 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4715 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4717 /* Even though we're already at the top-level, we push
4718 there again. That way, when we pop back a few lines
4719 hence, all of our state is restored. Otherwise,
4720 finish_function doesn't clean things up, and we end
4721 up with CURRENT_FUNCTION_DECL set. */
4722 push_to_top_level ();
4723 /* The decl's location will mark where it was first
4724 needed. Save that so synthesize method can indicate
4725 where it was needed from, in case of error */
4726 input_location = DECL_SOURCE_LOCATION (decl);
4727 synthesize_method (decl);
4728 pop_from_top_level ();
4729 reconsider = true;
4732 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4733 generate_tls_wrapper (decl);
4735 if (!DECL_SAVED_TREE (decl))
4736 continue;
4738 /* We lie to the back end, pretending that some functions
4739 are not defined when they really are. This keeps these
4740 functions from being put out unnecessarily. But, we must
4741 stop lying when the functions are referenced, or if they
4742 are not comdat since they need to be put out now. If
4743 DECL_INTERFACE_KNOWN, then we have already set
4744 DECL_EXTERNAL appropriately, so there's no need to check
4745 again, and we do not want to clear DECL_EXTERNAL if a
4746 previous call to import_export_decl set it.
4748 This is done in a separate for cycle, because if some
4749 deferred function is contained in another deferred
4750 function later in deferred_fns varray,
4751 rest_of_compilation would skip this function and we
4752 really cannot expand the same function twice. */
4753 import_export_decl (decl);
4754 if (DECL_NOT_REALLY_EXTERN (decl)
4755 && DECL_INITIAL (decl)
4756 && decl_needed_p (decl))
4758 struct cgraph_node *node, *next;
4760 node = cgraph_node::get (decl);
4761 if (node->cpp_implicit_alias)
4762 node = node->get_alias_target ();
4764 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4765 NULL, true);
4766 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4767 group, we need to mark all symbols in the same comdat group
4768 that way. */
4769 if (node->same_comdat_group)
4770 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4771 next != node;
4772 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4773 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4774 NULL, true);
4777 /* If we're going to need to write this function out, and
4778 there's already a body for it, create RTL for it now.
4779 (There might be no body if this is a method we haven't
4780 gotten around to synthesizing yet.) */
4781 if (!DECL_EXTERNAL (decl)
4782 && decl_needed_p (decl)
4783 && !TREE_ASM_WRITTEN (decl)
4784 && !cgraph_node::get (decl)->definition)
4786 /* We will output the function; no longer consider it in this
4787 loop. */
4788 DECL_DEFER_OUTPUT (decl) = 0;
4789 /* Generate RTL for this function now that we know we
4790 need it. */
4791 expand_or_defer_fn (decl);
4792 /* If we're compiling -fsyntax-only pretend that this
4793 function has been written out so that we don't try to
4794 expand it again. */
4795 if (flag_syntax_only)
4796 TREE_ASM_WRITTEN (decl) = 1;
4797 reconsider = true;
4801 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4802 reconsider = true;
4804 /* Static data members are just like namespace-scope globals. */
4805 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4807 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4808 /* Don't write it out if we haven't seen a definition. */
4809 || DECL_IN_AGGR_P (decl))
4810 continue;
4811 import_export_decl (decl);
4812 /* If this static data member is needed, provide it to the
4813 back end. */
4814 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4815 DECL_EXTERNAL (decl) = 0;
4817 if (vec_safe_length (pending_statics) != 0
4818 && wrapup_global_declarations (pending_statics->address (),
4819 pending_statics->length ()))
4820 reconsider = true;
4822 retries++;
4824 while (reconsider);
4826 lower_var_init ();
4828 generate_mangling_aliases ();
4830 /* All used inline functions must have a definition at this point. */
4831 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4833 if (/* Check online inline functions that were actually used. */
4834 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4835 /* If the definition actually was available here, then the
4836 fact that the function was not defined merely represents
4837 that for some reason (use of a template repository,
4838 #pragma interface, etc.) we decided not to emit the
4839 definition here. */
4840 && !DECL_INITIAL (decl)
4841 /* Don't complain if the template was defined. */
4842 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4843 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4844 (template_for_substitution (decl)))))
4846 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4847 "inline function %qD used but never defined", decl);
4848 /* Avoid a duplicate warning from check_global_declaration. */
4849 TREE_NO_WARNING (decl) = 1;
4853 /* So must decls that use a type with no linkage. */
4854 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4855 no_linkage_error (decl);
4857 maybe_warn_sized_delete ();
4859 /* Then, do the Objective-C stuff. This is where all the
4860 Objective-C module stuff gets generated (symtab,
4861 class/protocol/selector lists etc). This must be done after C++
4862 templates, destructors etc. so that selectors used in C++
4863 templates are properly allocated. */
4864 if (c_dialect_objc ())
4865 objc_write_global_declarations ();
4867 /* We give C linkage to static constructors and destructors. */
4868 push_lang_context (lang_name_c);
4870 /* Generate initialization and destruction functions for all
4871 priorities for which they are required. */
4872 if (priority_info_map)
4873 splay_tree_foreach (priority_info_map,
4874 generate_ctor_and_dtor_functions_for_priority,
4875 /*data=*/&locus_at_end_of_parsing);
4876 else if (c_dialect_objc () && objc_static_init_needed_p ())
4877 /* If this is obj-c++ and we need a static init, call
4878 generate_ctor_or_dtor_function. */
4879 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4880 DEFAULT_INIT_PRIORITY,
4881 &locus_at_end_of_parsing);
4883 /* We're done with the splay-tree now. */
4884 if (priority_info_map)
4885 splay_tree_delete (priority_info_map);
4887 /* Generate any missing aliases. */
4888 maybe_apply_pending_pragma_weaks ();
4890 /* We're done with static constructors, so we can go back to "C++"
4891 linkage now. */
4892 pop_lang_context ();
4894 /* Generate Java hidden aliases. */
4895 build_java_method_aliases ();
4897 if (flag_vtable_verify)
4899 vtv_recover_class_info ();
4900 vtv_compute_class_hierarchy_transitive_closure ();
4901 vtv_build_vtable_verify_fndecl ();
4904 perform_deferred_noexcept_checks ();
4906 finish_repo ();
4908 fini_constexpr ();
4910 /* The entire file is now complete. If requested, dump everything
4911 to a file. */
4912 dump_tu ();
4914 if (flag_detailed_statistics)
4916 dump_tree_statistics ();
4917 dump_time_statistics ();
4920 timevar_stop (TV_PHASE_DEFERRED);
4921 timevar_start (TV_PHASE_PARSING);
4923 /* Indicate that we're done with front end processing. */
4924 at_eof = 2;
4927 /* Perform any post compilation-proper cleanups for the C++ front-end.
4928 This should really go away. No front-end should need to do
4929 anything past the compilation process. */
4931 void
4932 cxx_post_compilation_parsing_cleanups (void)
4934 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4936 if (flag_vtable_verify)
4938 /* Generate the special constructor initialization function that
4939 calls __VLTRegisterPairs, and give it a very high
4940 initialization priority. This must be done after
4941 finalize_compilation_unit so that we have accurate
4942 information about which vtable will actually be emitted. */
4943 vtv_generate_init_routine ();
4946 input_location = locus_at_end_of_parsing;
4948 if (flag_checking)
4949 validate_conversion_obstack ();
4951 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4954 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4955 function to call in parse-tree form; it has not yet been
4956 semantically analyzed. ARGS are the arguments to the function.
4957 They have already been semantically analyzed. This may change
4958 ARGS. */
4960 tree
4961 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4962 tsubst_flags_t complain)
4964 tree orig_fn;
4965 vec<tree, va_gc> *orig_args = NULL;
4966 tree expr;
4967 tree object;
4969 orig_fn = fn;
4970 object = TREE_OPERAND (fn, 0);
4972 if (processing_template_decl)
4974 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4975 || TREE_CODE (fn) == MEMBER_REF);
4976 if (type_dependent_expression_p (fn)
4977 || any_type_dependent_arguments_p (*args))
4978 return build_nt_call_vec (fn, *args);
4980 orig_args = make_tree_vector_copy (*args);
4982 /* Transform the arguments and add the implicit "this"
4983 parameter. That must be done before the FN is transformed
4984 because we depend on the form of FN. */
4985 make_args_non_dependent (*args);
4986 object = build_non_dependent_expr (object);
4987 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4989 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4990 object = cp_build_addr_expr (object, complain);
4991 vec_safe_insert (*args, 0, object);
4993 /* Now that the arguments are done, transform FN. */
4994 fn = build_non_dependent_expr (fn);
4997 /* A qualified name corresponding to a bound pointer-to-member is
4998 represented as an OFFSET_REF:
5000 struct B { void g(); };
5001 void (B::*p)();
5002 void B::g() { (this->*p)(); } */
5003 if (TREE_CODE (fn) == OFFSET_REF)
5005 tree object_addr = cp_build_addr_expr (object, complain);
5006 fn = TREE_OPERAND (fn, 1);
5007 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5008 complain);
5009 vec_safe_insert (*args, 0, object_addr);
5012 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5013 expr = build_op_call (fn, args, complain);
5014 else
5015 expr = cp_build_function_call_vec (fn, args, complain);
5016 if (processing_template_decl && expr != error_mark_node)
5017 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5019 if (orig_args != NULL)
5020 release_tree_vector (orig_args);
5022 return expr;
5026 void
5027 check_default_args (tree x)
5029 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5030 bool saw_def = false;
5031 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5032 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5034 if (TREE_PURPOSE (arg))
5035 saw_def = true;
5036 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5038 error ("default argument missing for parameter %P of %q+#D", i, x);
5039 TREE_PURPOSE (arg) = error_mark_node;
5044 /* Return true if function DECL can be inlined. This is used to force
5045 instantiation of methods that might be interesting for inlining. */
5046 bool
5047 possibly_inlined_p (tree decl)
5049 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5050 if (DECL_UNINLINABLE (decl))
5051 return false;
5052 if (!optimize || pragma_java_exceptions)
5053 return DECL_DECLARED_INLINE_P (decl);
5054 /* When optimizing, we might inline everything when flatten
5055 attribute or heuristics inlining for size or autoinlining
5056 is used. */
5057 return true;
5060 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5061 If DECL is a specialization or implicitly declared class member,
5062 generate the actual definition. Return false if something goes
5063 wrong, true otherwise. */
5065 bool
5066 mark_used (tree decl, tsubst_flags_t complain)
5068 /* If DECL is a BASELINK for a single function, then treat it just
5069 like the DECL for the function. Otherwise, if the BASELINK is
5070 for an overloaded function, we don't know which function was
5071 actually used until after overload resolution. */
5072 if (BASELINK_P (decl))
5074 decl = BASELINK_FUNCTIONS (decl);
5075 if (really_overloaded_fn (decl))
5076 return true;
5077 decl = OVL_CURRENT (decl);
5080 /* Set TREE_USED for the benefit of -Wunused. */
5081 TREE_USED (decl) = 1;
5083 if (TREE_CODE (decl) == TEMPLATE_DECL)
5084 return true;
5086 if (DECL_CLONED_FUNCTION_P (decl))
5087 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5089 /* Mark enumeration types as used. */
5090 if (TREE_CODE (decl) == CONST_DECL)
5091 used_types_insert (DECL_CONTEXT (decl));
5093 if (TREE_CODE (decl) == FUNCTION_DECL)
5094 maybe_instantiate_noexcept (decl);
5096 if (TREE_CODE (decl) == FUNCTION_DECL
5097 && DECL_DELETED_FN (decl))
5099 if (DECL_ARTIFICIAL (decl))
5101 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5102 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5104 /* We mark a lambda conversion op as deleted if we can't
5105 generate it properly; see maybe_add_lambda_conv_op. */
5106 sorry ("converting lambda which uses %<...%> to "
5107 "function pointer");
5108 return false;
5111 if (complain & tf_error)
5113 error ("use of deleted function %qD", decl);
5114 if (!maybe_explain_implicit_delete (decl))
5115 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5117 return false;
5120 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5121 && deprecated_state != DEPRECATED_SUPPRESS)
5122 warn_deprecated_use (decl, NULL_TREE);
5124 /* We can only check DECL_ODR_USED on variables or functions with
5125 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5126 might need special handling for. */
5127 if (!VAR_OR_FUNCTION_DECL_P (decl)
5128 || DECL_LANG_SPECIFIC (decl) == NULL
5129 || DECL_THUNK_P (decl))
5131 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
5133 if (complain & tf_error)
5134 error ("use of %qD before deduction of %<auto%>", decl);
5135 return false;
5137 return true;
5140 /* We only want to do this processing once. We don't need to keep trying
5141 to instantiate inline templates, because unit-at-a-time will make sure
5142 we get them compiled before functions that want to inline them. */
5143 if (DECL_ODR_USED (decl))
5144 return true;
5146 /* Normally, we can wait until instantiation-time to synthesize DECL.
5147 However, if DECL is a static data member initialized with a constant
5148 or a constexpr function, we need it right now because a reference to
5149 such a data member or a call to such function is not value-dependent.
5150 For a function that uses auto in the return type, we need to instantiate
5151 it to find out its type. For OpenMP user defined reductions, we need
5152 them instantiated for reduction clauses which inline them by hand
5153 directly. */
5154 if (DECL_LANG_SPECIFIC (decl)
5155 && DECL_TEMPLATE_INFO (decl)
5156 && (decl_maybe_constant_var_p (decl)
5157 || (TREE_CODE (decl) == FUNCTION_DECL
5158 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5159 || undeduced_auto_decl (decl))
5160 && !DECL_DECLARED_CONCEPT_P (decl)
5161 && !uses_template_parms (DECL_TI_ARGS (decl)))
5163 /* Instantiating a function will result in garbage collection. We
5164 must treat this situation as if we were within the body of a
5165 function so as to avoid collecting live data only referenced from
5166 the stack (such as overload resolution candidates). */
5167 ++function_depth;
5168 instantiate_decl (decl, /*defer_ok=*/false,
5169 /*expl_inst_class_mem_p=*/false);
5170 --function_depth;
5173 if (processing_template_decl || in_template_function ())
5174 return true;
5176 /* Check this too in case we're within instantiate_non_dependent_expr. */
5177 if (DECL_TEMPLATE_INFO (decl)
5178 && uses_template_parms (DECL_TI_ARGS (decl)))
5179 return true;
5181 if (undeduced_auto_decl (decl))
5183 if (complain & tf_error)
5184 error ("use of %qD before deduction of %<auto%>", decl);
5185 return false;
5188 /* If we don't need a value, then we don't need to synthesize DECL. */
5189 if (cp_unevaluated_operand != 0)
5190 return true;
5192 DECL_ODR_USED (decl) = 1;
5193 if (DECL_CLONED_FUNCTION_P (decl))
5194 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5196 /* DR 757: A type without linkage shall not be used as the type of a
5197 variable or function with linkage, unless
5198 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5199 o the variable or function is not used (3.2 [basic.def.odr]) or is
5200 defined in the same translation unit. */
5201 if (cxx_dialect > cxx98
5202 && decl_linkage (decl) != lk_none
5203 && !DECL_EXTERN_C_P (decl)
5204 && !DECL_ARTIFICIAL (decl)
5205 && !decl_defined_p (decl)
5206 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5208 if (is_local_extern (decl))
5209 /* There's no way to define a local extern, and adding it to
5210 the vector interferes with GC, so give an error now. */
5211 no_linkage_error (decl);
5212 else
5213 vec_safe_push (no_linkage_decls, decl);
5216 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5217 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5218 /* Remember it, so we can check it was defined. */
5219 note_vague_linkage_fn (decl);
5221 /* Is it a synthesized method that needs to be synthesized? */
5222 if (TREE_CODE (decl) == FUNCTION_DECL
5223 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5224 && DECL_DEFAULTED_FN (decl)
5225 /* A function defaulted outside the class is synthesized either by
5226 cp_finish_decl or instantiate_decl. */
5227 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5228 && ! DECL_INITIAL (decl))
5230 /* Defer virtual destructors so that thunks get the right
5231 linkage. */
5232 if (DECL_VIRTUAL_P (decl) && !at_eof)
5234 note_vague_linkage_fn (decl);
5235 return true;
5238 /* Remember the current location for a function we will end up
5239 synthesizing. Then we can inform the user where it was
5240 required in the case of error. */
5241 DECL_SOURCE_LOCATION (decl) = input_location;
5243 /* Synthesizing an implicitly defined member function will result in
5244 garbage collection. We must treat this situation as if we were
5245 within the body of a function so as to avoid collecting live data
5246 on the stack (such as overload resolution candidates).
5248 We could just let cp_write_global_declarations handle synthesizing
5249 this function by adding it to deferred_fns, but doing
5250 it at the use site produces better error messages. */
5251 ++function_depth;
5252 synthesize_method (decl);
5253 --function_depth;
5254 /* If this is a synthesized method we don't need to
5255 do the instantiation test below. */
5257 else if (VAR_OR_FUNCTION_DECL_P (decl)
5258 && DECL_TEMPLATE_INFO (decl)
5259 && !DECL_DECLARED_CONCEPT_P (decl)
5260 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5261 || always_instantiate_p (decl)))
5262 /* If this is a function or variable that is an instance of some
5263 template, we now know that we will need to actually do the
5264 instantiation. We check that DECL is not an explicit
5265 instantiation because that is not checked in instantiate_decl.
5267 We put off instantiating functions in order to improve compile
5268 times. Maintaining a stack of active functions is expensive,
5269 and the inliner knows to instantiate any functions it might
5270 need. Therefore, we always try to defer instantiation. */
5272 ++function_depth;
5273 instantiate_decl (decl, /*defer_ok=*/true,
5274 /*expl_inst_class_mem_p=*/false);
5275 --function_depth;
5278 return true;
5281 bool
5282 mark_used (tree decl)
5284 return mark_used (decl, tf_warning_or_error);
5287 tree
5288 vtv_start_verification_constructor_init_function (void)
5290 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5293 tree
5294 vtv_finish_verification_constructor_init_function (tree function_body)
5296 tree fn;
5298 finish_compound_stmt (function_body);
5299 fn = finish_function (0);
5300 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5301 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5303 return fn;
5306 #include "gt-cp-decl2.h"