Merge branches/gcc-4_9-branch rev 225109.
[official-gcc.git] / gcc-4_9-branch / gcc / cp / decl2.c
blobf37fea3aedb282d5b7bb8784f22f9052c3f512c9
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "stringpool.h"
35 #include "varasm.h"
36 #include "attribs.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "pointer-set.h"
40 #include "flags.h"
41 #include "cp-tree.h"
42 #include "decl.h"
43 #include "toplev.h"
44 #include "timevar.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "c-family/c-common.h"
48 #include "c-family/c-objc.h"
49 #include "cgraph.h"
50 #include "tree-inline.h"
51 #include "c-family/c-pragma.h"
52 #include "dumpfile.h"
53 #include "intl.h"
54 #include "splay-tree.h"
55 #include "langhooks.h"
56 #include "c-family/c-ada-spec.h"
57 #include "asan.h"
59 extern cpp_reader *parse_in;
61 /* This structure contains information about the initializations
62 and/or destructions required for a particular priority level. */
63 typedef struct priority_info_s {
64 /* Nonzero if there have been any initializations at this priority
65 throughout the translation unit. */
66 int initializations_p;
67 /* Nonzero if there have been any destructions at this priority
68 throughout the translation unit. */
69 int destructions_p;
70 } *priority_info;
72 static void mark_vtable_entries (tree);
73 static bool maybe_emit_vtables (tree);
74 static bool acceptable_java_type (tree);
75 static tree start_objects (int, int);
76 static void finish_objects (int, int, tree);
77 static tree start_static_storage_duration_function (unsigned);
78 static void finish_static_storage_duration_function (tree);
79 static priority_info get_priority_info (int);
80 static void do_static_initialization_or_destruction (tree, bool);
81 static void one_static_initialization_or_destruction (tree, tree, bool);
82 static void generate_ctor_or_dtor_function (bool, int, location_t *);
83 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
84 void *);
85 static tree prune_vars_needing_no_initialization (tree *);
86 static void write_out_vars (tree);
87 static void import_export_class (tree);
88 static tree get_guard_bits (tree);
89 static void determine_visibility_from_class (tree, tree);
90 static bool determine_hidden_inline (tree);
91 static bool decl_defined_p (tree);
93 /* A list of static class variables. This is needed, because a
94 static class variable can be declared inside the class without
95 an initializer, and then initialized, statically, outside the class. */
96 static GTY(()) vec<tree, va_gc> *pending_statics;
98 /* A list of functions which were declared inline, but which we
99 may need to emit outline anyway. */
100 static GTY(()) vec<tree, va_gc> *deferred_fns;
102 /* A list of functions which we might want to set DECL_COMDAT on at EOF. */
104 static GTY(()) vec<tree, va_gc> *maybe_comdat_fns;
106 /* A list of decls that use types with no linkage, which we need to make
107 sure are defined. */
108 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
110 /* Nonzero if we're done parsing and into end-of-file activities. */
112 int at_eof;
115 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
116 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
117 that apply to the function). */
119 tree
120 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
121 cp_ref_qualifier rqual)
123 tree raises;
124 tree attrs;
125 int type_quals;
127 if (fntype == error_mark_node || ctype == error_mark_node)
128 return error_mark_node;
130 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
131 || TREE_CODE (fntype) == METHOD_TYPE);
133 type_quals = quals & ~TYPE_QUAL_RESTRICT;
134 ctype = cp_build_qualified_type (ctype, type_quals);
135 raises = TYPE_RAISES_EXCEPTIONS (fntype);
136 attrs = TYPE_ATTRIBUTES (fntype);
137 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
138 (TREE_CODE (fntype) == METHOD_TYPE
139 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
140 : TYPE_ARG_TYPES (fntype)));
141 if (attrs)
142 fntype = cp_build_type_attribute_variant (fntype, attrs);
143 if (rqual)
144 fntype = build_ref_qualified_type (fntype, rqual);
145 if (raises)
146 fntype = build_exception_variant (fntype, raises);
148 return fntype;
151 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
152 return type changed to NEW_RET. */
154 tree
155 change_return_type (tree new_ret, tree fntype)
157 tree newtype;
158 tree args = TYPE_ARG_TYPES (fntype);
159 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
160 tree attrs = TYPE_ATTRIBUTES (fntype);
162 if (new_ret == error_mark_node)
163 return fntype;
165 if (same_type_p (new_ret, TREE_TYPE (fntype)))
166 return fntype;
168 if (TREE_CODE (fntype) == FUNCTION_TYPE)
170 newtype = build_function_type (new_ret, args);
171 newtype = apply_memfn_quals (newtype,
172 type_memfn_quals (fntype),
173 type_memfn_rqual (fntype));
175 else
176 newtype = build_method_type_directly
177 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
178 if (raises)
179 newtype = build_exception_variant (newtype, raises);
180 if (attrs)
181 newtype = cp_build_type_attribute_variant (newtype, attrs);
183 return newtype;
186 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
187 appropriately. */
189 tree
190 cp_build_parm_decl (tree name, tree type)
192 tree parm = build_decl (input_location,
193 PARM_DECL, name, type);
194 /* DECL_ARG_TYPE is only used by the back end and the back end never
195 sees templates. */
196 if (!processing_template_decl)
197 DECL_ARG_TYPE (parm) = type_passed_as (type);
199 return parm;
202 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
203 indicated NAME. */
205 tree
206 build_artificial_parm (tree name, tree type)
208 tree parm = cp_build_parm_decl (name, type);
209 DECL_ARTIFICIAL (parm) = 1;
210 /* All our artificial parms are implicitly `const'; they cannot be
211 assigned to. */
212 TREE_READONLY (parm) = 1;
213 return parm;
216 /* Constructors for types with virtual baseclasses need an "in-charge" flag
217 saying whether this constructor is responsible for initialization of
218 virtual baseclasses or not. All destructors also need this "in-charge"
219 flag, which additionally determines whether or not the destructor should
220 free the memory for the object.
222 This function adds the "in-charge" flag to member function FN if
223 appropriate. It is called from grokclassfn and tsubst.
224 FN must be either a constructor or destructor.
226 The in-charge flag follows the 'this' parameter, and is followed by the
227 VTT parm (if any), then the user-written parms. */
229 void
230 maybe_retrofit_in_chrg (tree fn)
232 tree basetype, arg_types, parms, parm, fntype;
234 /* If we've already add the in-charge parameter don't do it again. */
235 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
236 return;
238 /* When processing templates we can't know, in general, whether or
239 not we're going to have virtual baseclasses. */
240 if (processing_template_decl)
241 return;
243 /* We don't need an in-charge parameter for constructors that don't
244 have virtual bases. */
245 if (DECL_CONSTRUCTOR_P (fn)
246 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
247 return;
249 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
250 basetype = TREE_TYPE (TREE_VALUE (arg_types));
251 arg_types = TREE_CHAIN (arg_types);
253 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
255 /* If this is a subobject constructor or destructor, our caller will
256 pass us a pointer to our VTT. */
257 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
259 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
261 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
262 DECL_CHAIN (parm) = parms;
263 parms = parm;
265 /* ...and then to TYPE_ARG_TYPES. */
266 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
268 DECL_HAS_VTT_PARM_P (fn) = 1;
271 /* Then add the in-charge parm (before the VTT parm). */
272 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
273 DECL_CHAIN (parm) = parms;
274 parms = parm;
275 arg_types = hash_tree_chain (integer_type_node, arg_types);
277 /* Insert our new parameter(s) into the list. */
278 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
280 /* And rebuild the function type. */
281 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
282 arg_types);
283 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
284 fntype = build_exception_variant (fntype,
285 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
286 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
287 fntype = (cp_build_type_attribute_variant
288 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
289 TREE_TYPE (fn) = fntype;
291 /* Now we've got the in-charge parameter. */
292 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
295 /* Classes overload their constituent function names automatically.
296 When a function name is declared in a record structure,
297 its name is changed to it overloaded name. Since names for
298 constructors and destructors can conflict, we place a leading
299 '$' for destructors.
301 CNAME is the name of the class we are grokking for.
303 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
305 FLAGS contains bits saying what's special about today's
306 arguments. DTOR_FLAG == DESTRUCTOR.
308 If FUNCTION is a destructor, then we must add the `auto-delete' field
309 as a second parameter. There is some hair associated with the fact
310 that we must "declare" this variable in the manner consistent with the
311 way the rest of the arguments were declared.
313 QUALS are the qualifiers for the this pointer. */
315 void
316 grokclassfn (tree ctype, tree function, enum overload_flags flags)
318 tree fn_name = DECL_NAME (function);
320 /* Even within an `extern "C"' block, members get C++ linkage. See
321 [dcl.link] for details. */
322 SET_DECL_LANGUAGE (function, lang_cplusplus);
324 if (fn_name == NULL_TREE)
326 error ("name missing for member function");
327 fn_name = get_identifier ("<anonymous>");
328 DECL_NAME (function) = fn_name;
331 DECL_CONTEXT (function) = ctype;
333 if (flags == DTOR_FLAG)
334 DECL_DESTRUCTOR_P (function) = 1;
336 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
337 maybe_retrofit_in_chrg (function);
340 /* Create an ARRAY_REF, checking for the user doing things backwards
341 along the way. DECLTYPE_P is for N3276, as in the parser. */
343 tree
344 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
345 bool decltype_p)
347 tree type;
348 tree expr;
349 tree orig_array_expr = array_expr;
350 tree orig_index_exp = index_exp;
352 if (error_operand_p (array_expr) || error_operand_p (index_exp))
353 return error_mark_node;
355 if (processing_template_decl)
357 if (type_dependent_expression_p (array_expr)
358 || type_dependent_expression_p (index_exp))
359 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
360 NULL_TREE, NULL_TREE);
361 array_expr = build_non_dependent_expr (array_expr);
362 index_exp = build_non_dependent_expr (index_exp);
365 type = TREE_TYPE (array_expr);
366 gcc_assert (type);
367 type = non_reference (type);
369 /* If they have an `operator[]', use that. */
370 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
372 tsubst_flags_t complain = tf_warning_or_error;
373 if (decltype_p)
374 complain |= tf_decltype;
375 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
376 index_exp, NULL_TREE, /*overload=*/NULL, complain);
378 else
380 tree p1, p2, i1, i2;
382 /* Otherwise, create an ARRAY_REF for a pointer or array type.
383 It is a little-known fact that, if `a' is an array and `i' is
384 an int, you can write `i[a]', which means the same thing as
385 `a[i]'. */
386 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
387 p1 = array_expr;
388 else
389 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
391 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
392 p2 = index_exp;
393 else
394 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
396 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
397 false);
398 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
399 false);
401 if ((p1 && i2) && (i1 && p2))
402 error ("ambiguous conversion for array subscript");
404 if (p1 && i2)
405 array_expr = p1, index_exp = i2;
406 else if (i1 && p2)
407 array_expr = p2, index_exp = i1;
408 else
410 error ("invalid types %<%T[%T]%> for array subscript",
411 type, TREE_TYPE (index_exp));
412 return error_mark_node;
415 if (array_expr == error_mark_node || index_exp == error_mark_node)
416 error ("ambiguous conversion for array subscript");
418 expr = build_array_ref (input_location, array_expr, index_exp);
420 if (processing_template_decl && expr != error_mark_node)
421 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
422 NULL_TREE, NULL_TREE);
423 return expr;
426 /* Given the cast expression EXP, checking out its validity. Either return
427 an error_mark_node if there was an unavoidable error, return a cast to
428 void for trying to delete a pointer w/ the value 0, or return the
429 call to delete. If DOING_VEC is true, we handle things differently
430 for doing an array delete.
431 Implements ARM $5.3.4. This is called from the parser. */
433 tree
434 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
435 tsubst_flags_t complain)
437 tree t, type;
439 if (exp == error_mark_node)
440 return exp;
442 if (processing_template_decl)
444 t = build_min (DELETE_EXPR, void_type_node, exp, size);
445 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
446 DELETE_EXPR_USE_VEC (t) = doing_vec;
447 TREE_SIDE_EFFECTS (t) = 1;
448 return t;
451 /* An array can't have been allocated by new, so complain. */
452 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
453 warning (0, "deleting array %q#E", exp);
455 t = build_expr_type_conversion (WANT_POINTER, exp, true);
457 if (t == NULL_TREE || t == error_mark_node)
459 error ("type %q#T argument given to %<delete%>, expected pointer",
460 TREE_TYPE (exp));
461 return error_mark_node;
464 type = TREE_TYPE (t);
466 /* As of Valley Forge, you can delete a pointer to const. */
468 /* You can't delete functions. */
469 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
471 error ("cannot delete a function. Only pointer-to-objects are "
472 "valid arguments to %<delete%>");
473 return error_mark_node;
476 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
477 if (VOID_TYPE_P (TREE_TYPE (type)))
479 warning (0, "deleting %qT is undefined", type);
480 doing_vec = 0;
483 /* Deleting a pointer with the value zero is valid and has no effect. */
484 if (integer_zerop (t))
485 return build1 (NOP_EXPR, void_type_node, t);
487 if (doing_vec)
488 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
489 sfk_deleting_destructor,
490 use_global_delete, complain);
491 else
492 return build_delete (type, t, sfk_deleting_destructor,
493 LOOKUP_NORMAL, use_global_delete,
494 complain);
497 /* Report an error if the indicated template declaration is not the
498 sort of thing that should be a member template. */
500 void
501 check_member_template (tree tmpl)
503 tree decl;
505 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
506 decl = DECL_TEMPLATE_RESULT (tmpl);
508 if (TREE_CODE (decl) == FUNCTION_DECL
509 || DECL_ALIAS_TEMPLATE_P (tmpl)
510 || (TREE_CODE (decl) == TYPE_DECL
511 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
513 /* The parser rejects template declarations in local classes
514 (with the exception of generic lambdas). */
515 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
516 /* The parser rejects any use of virtual in a function template. */
517 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
518 && DECL_VIRTUAL_P (decl)));
520 /* The debug-information generating code doesn't know what to do
521 with member templates. */
522 DECL_IGNORED_P (tmpl) = 1;
524 else
525 error ("template declaration of %q#D", decl);
528 /* Return true iff TYPE is a valid Java parameter or return type. */
530 static bool
531 acceptable_java_type (tree type)
533 if (type == error_mark_node)
534 return false;
536 if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
537 return true;
538 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
540 type = TREE_TYPE (type);
541 if (TREE_CODE (type) == RECORD_TYPE)
543 tree args; int i;
544 if (! TYPE_FOR_JAVA (type))
545 return false;
546 if (! CLASSTYPE_TEMPLATE_INFO (type))
547 return true;
548 args = CLASSTYPE_TI_ARGS (type);
549 i = TREE_VEC_LENGTH (args);
550 while (--i >= 0)
552 type = TREE_VEC_ELT (args, i);
553 if (TYPE_PTR_P (type))
554 type = TREE_TYPE (type);
555 if (! TYPE_FOR_JAVA (type))
556 return false;
558 return true;
561 return false;
564 /* For a METHOD in a Java class CTYPE, return true if
565 the parameter and return types are valid Java types.
566 Otherwise, print appropriate error messages, and return false. */
568 bool
569 check_java_method (tree method)
571 bool jerr = false;
572 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
573 tree ret_type = TREE_TYPE (TREE_TYPE (method));
575 if (!acceptable_java_type (ret_type))
577 error ("Java method %qD has non-Java return type %qT",
578 method, ret_type);
579 jerr = true;
582 arg_types = TREE_CHAIN (arg_types);
583 if (DECL_HAS_IN_CHARGE_PARM_P (method))
584 arg_types = TREE_CHAIN (arg_types);
585 if (DECL_HAS_VTT_PARM_P (method))
586 arg_types = TREE_CHAIN (arg_types);
588 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
590 tree type = TREE_VALUE (arg_types);
591 if (!acceptable_java_type (type))
593 if (type != error_mark_node)
594 error ("Java method %qD has non-Java parameter type %qT",
595 method, type);
596 jerr = true;
599 return !jerr;
602 /* Sanity check: report error if this function FUNCTION is not
603 really a member of the class (CTYPE) it is supposed to belong to.
604 TEMPLATE_PARMS is used to specify the template parameters of a member
605 template passed as FUNCTION_DECL. If the member template is passed as a
606 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
607 from the declaration. If the function is not a function template, it
608 must be NULL.
609 It returns the original declaration for the function, NULL_TREE if
610 no declaration was found, error_mark_node if an error was emitted. */
612 tree
613 check_classfn (tree ctype, tree function, tree template_parms)
615 int ix;
616 bool is_template;
617 tree pushed_scope;
619 if (DECL_USE_TEMPLATE (function)
620 && !(TREE_CODE (function) == TEMPLATE_DECL
621 && DECL_TEMPLATE_SPECIALIZATION (function))
622 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
623 /* Since this is a specialization of a member template,
624 we're not going to find the declaration in the class.
625 For example, in:
627 struct S { template <typename T> void f(T); };
628 template <> void S::f(int);
630 we're not going to find `S::f(int)', but there's no
631 reason we should, either. We let our callers know we didn't
632 find the method, but we don't complain. */
633 return NULL_TREE;
635 /* Basic sanity check: for a template function, the template parameters
636 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
637 if (TREE_CODE (function) == TEMPLATE_DECL)
639 if (template_parms
640 && !comp_template_parms (template_parms,
641 DECL_TEMPLATE_PARMS (function)))
643 error ("template parameter lists provided don%'t match the "
644 "template parameters of %qD", function);
645 return error_mark_node;
647 template_parms = DECL_TEMPLATE_PARMS (function);
650 /* OK, is this a definition of a member template? */
651 is_template = (template_parms != NULL_TREE);
653 /* [temp.mem]
655 A destructor shall not be a member template. */
656 if (DECL_DESTRUCTOR_P (function) && is_template)
658 error ("destructor %qD declared as member template", function);
659 return error_mark_node;
662 /* We must enter the scope here, because conversion operators are
663 named by target type, and type equivalence relies on typenames
664 resolving within the scope of CTYPE. */
665 pushed_scope = push_scope (ctype);
666 ix = class_method_index_for_fn (complete_type (ctype), function);
667 if (ix >= 0)
669 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
670 tree fndecls, fndecl = 0;
671 bool is_conv_op;
672 const char *format = NULL;
674 for (fndecls = (*methods)[ix];
675 fndecls; fndecls = OVL_NEXT (fndecls))
677 tree p1, p2;
679 fndecl = OVL_CURRENT (fndecls);
680 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
681 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
683 /* We cannot simply call decls_match because this doesn't
684 work for static member functions that are pretending to
685 be methods, and because the name may have been changed by
686 asm("new_name"). */
688 /* Get rid of the this parameter on functions that become
689 static. */
690 if (DECL_STATIC_FUNCTION_P (fndecl)
691 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
692 p1 = TREE_CHAIN (p1);
694 /* A member template definition only matches a member template
695 declaration. */
696 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
697 continue;
699 /* ref-qualifier or absence of same must match. */
700 if (type_memfn_rqual (TREE_TYPE (function))
701 != type_memfn_rqual (TREE_TYPE (fndecl)))
702 continue;
704 /* While finding a match, same types and params are not enough
705 if the function is versioned. Also check version ("target")
706 attributes. */
707 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
708 TREE_TYPE (TREE_TYPE (fndecl)))
709 && compparms (p1, p2)
710 && !targetm.target_option.function_versions (function, fndecl)
711 && (!is_template
712 || comp_template_parms (template_parms,
713 DECL_TEMPLATE_PARMS (fndecl)))
714 && (DECL_TEMPLATE_SPECIALIZATION (function)
715 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
716 && (!DECL_TEMPLATE_SPECIALIZATION (function)
717 || (DECL_TI_TEMPLATE (function)
718 == DECL_TI_TEMPLATE (fndecl))))
719 break;
721 if (fndecls)
723 if (pushed_scope)
724 pop_scope (pushed_scope);
725 return OVL_CURRENT (fndecls);
728 error_at (DECL_SOURCE_LOCATION (function),
729 "prototype for %q#D does not match any in class %qT",
730 function, ctype);
731 is_conv_op = DECL_CONV_FN_P (fndecl);
733 if (is_conv_op)
734 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
735 fndecls = (*methods)[ix];
736 while (fndecls)
738 fndecl = OVL_CURRENT (fndecls);
739 fndecls = OVL_NEXT (fndecls);
741 if (!fndecls && is_conv_op)
743 if (methods->length () > (size_t) ++ix)
745 fndecls = (*methods)[ix];
746 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
748 fndecls = NULL_TREE;
749 is_conv_op = false;
752 else
753 is_conv_op = false;
755 if (format)
756 format = " %+#D";
757 else if (fndecls)
758 format = N_("candidates are: %+#D");
759 else
760 format = N_("candidate is: %+#D");
761 error (format, fndecl);
764 else if (!COMPLETE_TYPE_P (ctype))
765 cxx_incomplete_type_error (function, ctype);
766 else
767 error ("no %q#D member function declared in class %qT",
768 function, ctype);
770 if (pushed_scope)
771 pop_scope (pushed_scope);
772 return error_mark_node;
775 /* DECL is a function with vague linkage. Remember it so that at the
776 end of the translation unit we can decide whether or not to emit
777 it. */
779 void
780 note_vague_linkage_fn (tree decl)
782 DECL_DEFER_OUTPUT (decl) = 1;
783 vec_safe_push (deferred_fns, decl);
786 /* We have just processed the DECL, which is a static data member.
787 The other parameters are as for cp_finish_decl. */
789 void
790 finish_static_data_member_decl (tree decl,
791 tree init, bool init_const_expr_p,
792 tree asmspec_tree,
793 int flags)
795 DECL_CONTEXT (decl) = current_class_type;
797 /* We cannot call pushdecl here, because that would fill in the
798 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
799 the right thing, namely, to put this decl out straight away. */
801 if (! processing_template_decl)
802 vec_safe_push (pending_statics, decl);
804 if (LOCAL_CLASS_P (current_class_type)
805 /* We already complained about the template definition. */
806 && !DECL_TEMPLATE_INSTANTIATION (decl))
807 permerror (input_location, "local class %q#T shall not have static data member %q#D",
808 current_class_type, decl);
809 else
810 for (tree t = current_class_type; TYPE_P (t);
811 t = CP_TYPE_CONTEXT (t))
812 if (TYPE_ANONYMOUS_P (t))
814 if (permerror (DECL_SOURCE_LOCATION (decl),
815 "static data member %qD in unnamed class", decl))
816 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
817 "unnamed class defined here");
818 break;
821 DECL_IN_AGGR_P (decl) = 1;
823 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
824 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
825 SET_VAR_HAD_UNKNOWN_BOUND (decl);
827 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
830 /* DECLARATOR and DECLSPECS correspond to a class member. The other
831 parameters are as for cp_finish_decl. Return the DECL for the
832 class member declared. */
834 tree
835 grokfield (const cp_declarator *declarator,
836 cp_decl_specifier_seq *declspecs,
837 tree init, bool init_const_expr_p,
838 tree asmspec_tree,
839 tree attrlist)
841 tree value;
842 const char *asmspec = 0;
843 int flags;
844 tree name;
846 if (init
847 && TREE_CODE (init) == TREE_LIST
848 && TREE_VALUE (init) == error_mark_node
849 && TREE_CHAIN (init) == NULL_TREE)
850 init = NULL_TREE;
852 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
853 if (! value || value == error_mark_node)
854 /* friend or constructor went bad. */
855 return error_mark_node;
856 if (TREE_TYPE (value) == error_mark_node)
857 return value;
859 if (TREE_CODE (value) == TYPE_DECL && init)
861 error ("typedef %qD is initialized (use decltype instead)", value);
862 init = NULL_TREE;
865 /* Pass friendly classes back. */
866 if (value == void_type_node)
867 return value;
869 /* Pass friend decls back. */
870 if ((TREE_CODE (value) == FUNCTION_DECL
871 || TREE_CODE (value) == TEMPLATE_DECL)
872 && DECL_CONTEXT (value) != current_class_type)
873 return value;
875 name = DECL_NAME (value);
877 if (name != NULL_TREE)
879 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
881 error ("explicit template argument list not allowed");
882 return error_mark_node;
885 if (IDENTIFIER_POINTER (name)[0] == '_'
886 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
887 error ("member %qD conflicts with virtual function table field name",
888 value);
891 /* Stash away type declarations. */
892 if (TREE_CODE (value) == TYPE_DECL)
894 DECL_NONLOCAL (value) = 1;
895 DECL_CONTEXT (value) = current_class_type;
897 if (attrlist)
899 int attrflags = 0;
901 /* If this is a typedef that names the class for linkage purposes
902 (7.1.3p8), apply any attributes directly to the type. */
903 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
904 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
905 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
907 cplus_decl_attributes (&value, attrlist, attrflags);
910 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
911 && TREE_TYPE (value) != error_mark_node
912 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
913 set_underlying_type (value);
915 /* It's important that push_template_decl below follows
916 set_underlying_type above so that the created template
917 carries the properly set type of VALUE. */
918 if (processing_template_decl)
919 value = push_template_decl (value);
921 record_locally_defined_typedef (value);
922 return value;
925 if (DECL_IN_AGGR_P (value))
927 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
928 return void_type_node;
931 if (asmspec_tree && asmspec_tree != error_mark_node)
932 asmspec = TREE_STRING_POINTER (asmspec_tree);
934 if (init)
936 if (TREE_CODE (value) == FUNCTION_DECL)
938 /* Initializers for functions are rejected early in the parser.
939 If we get here, it must be a pure specifier for a method. */
940 if (init == ridpointers[(int)RID_DELETE])
942 DECL_DELETED_FN (value) = 1;
943 DECL_DECLARED_INLINE_P (value) = 1;
944 DECL_INITIAL (value) = error_mark_node;
946 else if (init == ridpointers[(int)RID_DEFAULT])
948 if (defaultable_fn_check (value))
950 DECL_DEFAULTED_FN (value) = 1;
951 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
952 DECL_DECLARED_INLINE_P (value) = 1;
955 else if (TREE_CODE (init) == DEFAULT_ARG)
956 error ("invalid initializer for member function %qD", value);
957 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
959 if (integer_zerop (init))
960 DECL_PURE_VIRTUAL_P (value) = 1;
961 else if (error_operand_p (init))
962 ; /* An error has already been reported. */
963 else
964 error ("invalid initializer for member function %qD",
965 value);
967 else
969 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
970 error ("initializer specified for static member function %qD",
971 value);
974 else if (TREE_CODE (value) == FIELD_DECL)
975 /* C++11 NSDMI, keep going. */;
976 else if (!VAR_P (value))
977 gcc_unreachable ();
980 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
982 value = push_template_decl (value);
983 if (error_operand_p (value))
984 return error_mark_node;
987 if (attrlist)
988 cplus_decl_attributes (&value, attrlist, 0);
990 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
991 && CONSTRUCTOR_IS_DIRECT_INIT (init))
992 flags = LOOKUP_NORMAL;
993 else
994 flags = LOOKUP_IMPLICIT;
996 switch (TREE_CODE (value))
998 case VAR_DECL:
999 finish_static_data_member_decl (value, init, init_const_expr_p,
1000 asmspec_tree, flags);
1001 return value;
1003 case FIELD_DECL:
1004 if (asmspec)
1005 error ("%<asm%> specifiers are not permitted on non-static data members");
1006 if (DECL_INITIAL (value) == error_mark_node)
1007 init = error_mark_node;
1008 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1009 NULL_TREE, flags);
1010 DECL_IN_AGGR_P (value) = 1;
1011 return value;
1013 case FUNCTION_DECL:
1014 if (asmspec)
1015 set_user_assembler_name (value, asmspec);
1017 cp_finish_decl (value,
1018 /*init=*/NULL_TREE,
1019 /*init_const_expr_p=*/false,
1020 asmspec_tree, flags);
1022 /* Pass friends back this way. */
1023 if (DECL_FRIEND_P (value))
1024 return void_type_node;
1026 DECL_IN_AGGR_P (value) = 1;
1027 return value;
1029 default:
1030 gcc_unreachable ();
1032 return NULL_TREE;
1035 /* Like `grokfield', but for bitfields.
1036 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1038 tree
1039 grokbitfield (const cp_declarator *declarator,
1040 cp_decl_specifier_seq *declspecs, tree width,
1041 tree attrlist)
1043 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1045 if (value == error_mark_node)
1046 return NULL_TREE; /* friends went bad. */
1047 if (TREE_TYPE (value) == error_mark_node)
1048 return value;
1050 /* Pass friendly classes back. */
1051 if (VOID_TYPE_P (value))
1052 return void_type_node;
1054 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1055 && (POINTER_TYPE_P (value)
1056 || !dependent_type_p (TREE_TYPE (value))))
1058 error ("bit-field %qD with non-integral type", value);
1059 return error_mark_node;
1062 if (TREE_CODE (value) == TYPE_DECL)
1064 error ("cannot declare %qD to be a bit-field type", value);
1065 return NULL_TREE;
1068 /* Usually, finish_struct_1 catches bitfields with invalid types.
1069 But, in the case of bitfields with function type, we confuse
1070 ourselves into thinking they are member functions, so we must
1071 check here. */
1072 if (TREE_CODE (value) == FUNCTION_DECL)
1074 error ("cannot declare bit-field %qD with function type",
1075 DECL_NAME (value));
1076 return NULL_TREE;
1079 if (DECL_IN_AGGR_P (value))
1081 error ("%qD is already defined in the class %qT", value,
1082 DECL_CONTEXT (value));
1083 return void_type_node;
1086 if (TREE_STATIC (value))
1088 error ("static member %qD cannot be a bit-field", value);
1089 return NULL_TREE;
1091 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1093 if (width != error_mark_node)
1095 /* The width must be an integer type. */
1096 if (!type_dependent_expression_p (width)
1097 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1098 error ("width of bit-field %qD has non-integral type %qT", value,
1099 TREE_TYPE (width));
1100 DECL_INITIAL (value) = width;
1101 SET_DECL_C_BIT_FIELD (value);
1104 DECL_IN_AGGR_P (value) = 1;
1106 if (attrlist)
1107 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1109 return value;
1113 /* Returns true iff ATTR is an attribute which needs to be applied at
1114 instantiation time rather than template definition time. */
1116 static bool
1117 is_late_template_attribute (tree attr, tree decl)
1119 tree name = get_attribute_name (attr);
1120 tree args = TREE_VALUE (attr);
1121 const struct attribute_spec *spec = lookup_attribute_spec (name);
1122 tree arg;
1124 if (!spec)
1125 /* Unknown attribute. */
1126 return false;
1128 /* Attribute weak handling wants to write out assembly right away. */
1129 if (is_attribute_p ("weak", name))
1130 return true;
1132 /* Attribute unused is applied directly, as it appertains to
1133 decls. */
1134 if (is_attribute_p ("unused", name))
1135 return false;
1137 /* #pragma omp declare simd attribute needs to be always deferred. */
1138 if (flag_openmp
1139 && is_attribute_p ("omp declare simd", name))
1140 return true;
1142 /* If any of the arguments are dependent expressions, we can't evaluate
1143 the attribute until instantiation time. */
1144 for (arg = args; arg; arg = TREE_CHAIN (arg))
1146 tree t = TREE_VALUE (arg);
1148 /* If the first attribute argument is an identifier, only consider
1149 second and following arguments. Attributes like mode, format,
1150 cleanup and several target specific attributes aren't late
1151 just because they have an IDENTIFIER_NODE as first argument. */
1152 if (arg == args && identifier_p (t))
1153 continue;
1155 if (value_dependent_expression_p (t)
1156 || type_dependent_expression_p (t))
1157 return true;
1160 if (TREE_CODE (decl) == TYPE_DECL
1161 || TYPE_P (decl)
1162 || spec->type_required)
1164 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1166 /* We can't apply any attributes to a completely unknown type until
1167 instantiation time. */
1168 enum tree_code code = TREE_CODE (type);
1169 if (code == TEMPLATE_TYPE_PARM
1170 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1171 || code == TYPENAME_TYPE)
1172 return true;
1173 /* Also defer most attributes on dependent types. This is not
1174 necessary in all cases, but is the better default. */
1175 else if (dependent_type_p (type)
1176 /* But attributes abi_tag and visibility specifically apply
1177 to templates. */
1178 && !is_attribute_p ("abi_tag", name)
1179 && !is_attribute_p ("visibility", name))
1180 return true;
1181 else
1182 return false;
1184 else
1185 return false;
1188 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1189 applied at instantiation time and return them. If IS_DEPENDENT is true,
1190 the declaration itself is dependent, so all attributes should be applied
1191 at instantiation time. */
1193 static tree
1194 splice_template_attributes (tree *attr_p, tree decl)
1196 tree *p = attr_p;
1197 tree late_attrs = NULL_TREE;
1198 tree *q = &late_attrs;
1200 if (!p)
1201 return NULL_TREE;
1203 for (; *p; )
1205 if (is_late_template_attribute (*p, decl))
1207 ATTR_IS_DEPENDENT (*p) = 1;
1208 *q = *p;
1209 *p = TREE_CHAIN (*p);
1210 q = &TREE_CHAIN (*q);
1211 *q = NULL_TREE;
1213 else
1214 p = &TREE_CHAIN (*p);
1217 return late_attrs;
1220 /* Remove any late attributes from the list in ATTR_P and attach them to
1221 DECL_P. */
1223 static void
1224 save_template_attributes (tree *attr_p, tree *decl_p)
1226 tree *q;
1228 if (attr_p && *attr_p == error_mark_node)
1229 return;
1231 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1232 if (!late_attrs)
1233 return;
1235 if (DECL_P (*decl_p))
1236 q = &DECL_ATTRIBUTES (*decl_p);
1237 else
1238 q = &TYPE_ATTRIBUTES (*decl_p);
1240 tree old_attrs = *q;
1242 /* Merge the late attributes at the beginning with the attribute
1243 list. */
1244 late_attrs = merge_attributes (late_attrs, *q);
1245 *q = late_attrs;
1247 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1249 /* We've added new attributes directly to the main variant, so
1250 now we need to update all of the other variants to include
1251 these new attributes. */
1252 tree variant;
1253 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1254 variant = TYPE_NEXT_VARIANT (variant))
1256 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1257 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1262 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1263 to a typedef which gives a previously anonymous class or enum a name for
1264 linkage purposes. */
1266 bool
1267 attributes_naming_typedef_ok (tree attrs)
1269 for (; attrs; attrs = TREE_CHAIN (attrs))
1271 tree name = get_attribute_name (attrs);
1272 if (is_attribute_p ("vector_size", name))
1273 return false;
1275 return true;
1278 /* Like reconstruct_complex_type, but handle also template trees. */
1280 tree
1281 cp_reconstruct_complex_type (tree type, tree bottom)
1283 tree inner, outer;
1285 if (TYPE_PTR_P (type))
1287 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1288 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1289 TYPE_REF_CAN_ALIAS_ALL (type));
1291 else if (TREE_CODE (type) == REFERENCE_TYPE)
1293 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1294 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1295 TYPE_REF_CAN_ALIAS_ALL (type));
1297 else if (TREE_CODE (type) == ARRAY_TYPE)
1299 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1300 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1301 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1302 element type qualification will be handled by the recursive
1303 cp_reconstruct_complex_type call and cp_build_qualified_type
1304 for ARRAY_TYPEs changes the element type. */
1305 return outer;
1307 else if (TREE_CODE (type) == FUNCTION_TYPE)
1309 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1310 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1311 outer = apply_memfn_quals (outer,
1312 type_memfn_quals (type),
1313 type_memfn_rqual (type));
1315 else if (TREE_CODE (type) == METHOD_TYPE)
1317 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1318 /* The build_method_type_directly() routine prepends 'this' to argument list,
1319 so we must compensate by getting rid of it. */
1320 outer
1321 = build_method_type_directly
1322 (class_of_this_parm (type), inner,
1323 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1325 else if (TREE_CODE (type) == OFFSET_TYPE)
1327 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1328 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1330 else
1331 return bottom;
1333 if (TYPE_ATTRIBUTES (type))
1334 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1335 return cp_build_qualified_type (outer, cp_type_quals (type));
1338 /* Replaces any constexpr expression that may be into the attributes
1339 arguments with their reduced value. */
1341 static void
1342 cp_check_const_attributes (tree attributes)
1344 if (attributes == error_mark_node)
1345 return;
1347 tree attr;
1348 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1350 tree arg;
1351 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1353 tree expr = TREE_VALUE (arg);
1354 if (EXPR_P (expr))
1355 TREE_VALUE (arg) = maybe_constant_value (expr);
1360 /* Return true if TYPE is an OpenMP mappable type. */
1361 bool
1362 cp_omp_mappable_type (tree type)
1364 /* Mappable type has to be complete. */
1365 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1366 return false;
1367 /* Arrays have mappable type if the elements have mappable type. */
1368 while (TREE_CODE (type) == ARRAY_TYPE)
1369 type = TREE_TYPE (type);
1370 /* A mappable type cannot contain virtual members. */
1371 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1372 return false;
1373 /* All data members must be non-static. */
1374 if (CLASS_TYPE_P (type))
1376 tree field;
1377 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1378 if (TREE_CODE (field) == VAR_DECL)
1379 return false;
1380 /* All fields must have mappable types. */
1381 else if (TREE_CODE (field) == FIELD_DECL
1382 && !cp_omp_mappable_type (TREE_TYPE (field)))
1383 return false;
1385 return true;
1388 /* Like decl_attributes, but handle C++ complexity. */
1390 void
1391 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1393 if (*decl == NULL_TREE || *decl == void_type_node
1394 || *decl == error_mark_node)
1395 return;
1397 /* Add implicit "omp declare target" attribute if requested. */
1398 if (scope_chain->omp_declare_target_attribute
1399 && ((TREE_CODE (*decl) == VAR_DECL
1400 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1401 || TREE_CODE (*decl) == FUNCTION_DECL))
1403 if (TREE_CODE (*decl) == VAR_DECL
1404 && DECL_CLASS_SCOPE_P (*decl))
1405 error ("%q+D static data member inside of declare target directive",
1406 *decl);
1407 else if (TREE_CODE (*decl) == VAR_DECL
1408 && (DECL_FUNCTION_SCOPE_P (*decl)
1409 || (current_function_decl && !DECL_EXTERNAL (*decl))))
1410 error ("%q+D in block scope inside of declare target directive",
1411 *decl);
1412 else if (!processing_template_decl
1413 && TREE_CODE (*decl) == VAR_DECL
1414 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1415 error ("%q+D in declare target directive does not have mappable type",
1416 *decl);
1417 else
1418 attributes = tree_cons (get_identifier ("omp declare target"),
1419 NULL_TREE, attributes);
1422 if (processing_template_decl)
1424 if (check_for_bare_parameter_packs (attributes))
1425 return;
1427 save_template_attributes (&attributes, decl);
1430 cp_check_const_attributes (attributes);
1432 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1433 decl = &DECL_TEMPLATE_RESULT (*decl);
1435 decl_attributes (decl, attributes, flags);
1437 if (TREE_CODE (*decl) == TYPE_DECL)
1438 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1441 /* Walks through the namespace- or function-scope anonymous union
1442 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1443 Returns one of the fields for use in the mangled name. */
1445 static tree
1446 build_anon_union_vars (tree type, tree object)
1448 tree main_decl = NULL_TREE;
1449 tree field;
1451 /* Rather than write the code to handle the non-union case,
1452 just give an error. */
1453 if (TREE_CODE (type) != UNION_TYPE)
1455 error ("anonymous struct not inside named type");
1456 return error_mark_node;
1459 for (field = TYPE_FIELDS (type);
1460 field != NULL_TREE;
1461 field = DECL_CHAIN (field))
1463 tree decl;
1464 tree ref;
1466 if (DECL_ARTIFICIAL (field))
1467 continue;
1468 if (TREE_CODE (field) != FIELD_DECL)
1470 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1471 "have non-static data members", field);
1472 continue;
1475 if (TREE_PRIVATE (field))
1476 permerror (input_location, "private member %q+#D in anonymous union", field);
1477 else if (TREE_PROTECTED (field))
1478 permerror (input_location, "protected member %q+#D in anonymous union", field);
1480 if (processing_template_decl)
1481 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1482 DECL_NAME (field), NULL_TREE);
1483 else
1484 ref = build_class_member_access_expr (object, field, NULL_TREE,
1485 false, tf_warning_or_error);
1487 if (DECL_NAME (field))
1489 tree base;
1491 decl = build_decl (input_location,
1492 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1493 DECL_ANON_UNION_VAR_P (decl) = 1;
1494 DECL_ARTIFICIAL (decl) = 1;
1496 base = get_base_address (object);
1497 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1498 TREE_STATIC (decl) = TREE_STATIC (base);
1499 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1501 SET_DECL_VALUE_EXPR (decl, ref);
1502 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1504 decl = pushdecl (decl);
1506 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1507 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1508 else
1509 decl = 0;
1511 if (main_decl == NULL_TREE)
1512 main_decl = decl;
1515 return main_decl;
1518 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1519 anonymous union, then all members must be laid out together. PUBLIC_P
1520 is nonzero if this union is not declared static. */
1522 void
1523 finish_anon_union (tree anon_union_decl)
1525 tree type;
1526 tree main_decl;
1527 bool public_p;
1529 if (anon_union_decl == error_mark_node)
1530 return;
1532 type = TREE_TYPE (anon_union_decl);
1533 public_p = TREE_PUBLIC (anon_union_decl);
1535 /* The VAR_DECL's context is the same as the TYPE's context. */
1536 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1538 if (TYPE_FIELDS (type) == NULL_TREE)
1539 return;
1541 if (public_p)
1543 error ("namespace-scope anonymous aggregates must be static");
1544 return;
1547 main_decl = build_anon_union_vars (type, anon_union_decl);
1548 if (main_decl == error_mark_node)
1549 return;
1550 if (main_decl == NULL_TREE)
1552 warning (0, "anonymous union with no members");
1553 return;
1556 if (!processing_template_decl)
1558 /* Use main_decl to set the mangled name. */
1559 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1560 maybe_commonize_var (anon_union_decl);
1561 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1562 mangle_decl (anon_union_decl);
1563 DECL_NAME (anon_union_decl) = NULL_TREE;
1566 pushdecl (anon_union_decl);
1567 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1570 /* Auxiliary functions to make type signatures for
1571 `operator new' and `operator delete' correspond to
1572 what compiler will be expecting. */
1574 tree
1575 coerce_new_type (tree type)
1577 int e = 0;
1578 tree args = TYPE_ARG_TYPES (type);
1580 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1582 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1584 e = 1;
1585 error ("%<operator new%> must return type %qT", ptr_type_node);
1588 if (args && args != void_list_node)
1590 if (TREE_PURPOSE (args))
1592 /* [basic.stc.dynamic.allocation]
1594 The first parameter shall not have an associated default
1595 argument. */
1596 error ("the first parameter of %<operator new%> cannot "
1597 "have a default argument");
1598 /* Throw away the default argument. */
1599 TREE_PURPOSE (args) = NULL_TREE;
1602 if (!same_type_p (TREE_VALUE (args), size_type_node))
1604 e = 2;
1605 args = TREE_CHAIN (args);
1608 else
1609 e = 2;
1611 if (e == 2)
1612 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1613 "as first parameter", size_type_node);
1615 switch (e)
1617 case 2:
1618 args = tree_cons (NULL_TREE, size_type_node, args);
1619 /* Fall through. */
1620 case 1:
1621 type = build_exception_variant
1622 (build_function_type (ptr_type_node, args),
1623 TYPE_RAISES_EXCEPTIONS (type));
1624 /* Fall through. */
1625 default:;
1627 return type;
1630 tree
1631 coerce_delete_type (tree type)
1633 int e = 0;
1634 tree args = TYPE_ARG_TYPES (type);
1636 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1638 if (!same_type_p (TREE_TYPE (type), void_type_node))
1640 e = 1;
1641 error ("%<operator delete%> must return type %qT", void_type_node);
1644 if (!args || args == void_list_node
1645 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1647 e = 2;
1648 if (args && args != void_list_node)
1649 args = TREE_CHAIN (args);
1650 error ("%<operator delete%> takes type %qT as first parameter",
1651 ptr_type_node);
1653 switch (e)
1655 case 2:
1656 args = tree_cons (NULL_TREE, ptr_type_node, args);
1657 /* Fall through. */
1658 case 1:
1659 type = build_exception_variant
1660 (build_function_type (void_type_node, args),
1661 TYPE_RAISES_EXCEPTIONS (type));
1662 /* Fall through. */
1663 default:;
1666 return type;
1669 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1670 and mark them as needed. */
1672 static void
1673 mark_vtable_entries (tree decl)
1675 tree fnaddr;
1676 unsigned HOST_WIDE_INT idx;
1678 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1679 idx, fnaddr)
1681 tree fn;
1683 STRIP_NOPS (fnaddr);
1685 if (TREE_CODE (fnaddr) != ADDR_EXPR
1686 && TREE_CODE (fnaddr) != FDESC_EXPR)
1687 /* This entry is an offset: a virtual base class offset, a
1688 virtual call offset, an RTTI offset, etc. */
1689 continue;
1691 fn = TREE_OPERAND (fnaddr, 0);
1692 TREE_ADDRESSABLE (fn) = 1;
1693 /* When we don't have vcall offsets, we output thunks whenever
1694 we output the vtables that contain them. With vcall offsets,
1695 we know all the thunks we'll need when we emit a virtual
1696 function, so we emit the thunks there instead. */
1697 if (DECL_THUNK_P (fn))
1698 use_thunk (fn, /*emit_p=*/0);
1699 mark_used (fn);
1703 /* Set DECL up to have the closest approximation of "initialized common"
1704 linkage available. */
1706 void
1707 comdat_linkage (tree decl)
1709 if (flag_weak)
1710 make_decl_one_only (decl, cxx_comdat_group (decl));
1711 else if (TREE_CODE (decl) == FUNCTION_DECL
1712 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1713 /* We can just emit function and compiler-generated variables
1714 statically; having multiple copies is (for the most part) only
1715 a waste of space.
1717 There are two correctness issues, however: the address of a
1718 template instantiation with external linkage should be the
1719 same, independent of what translation unit asks for the
1720 address, and this will not hold when we emit multiple copies of
1721 the function. However, there's little else we can do.
1723 Also, by default, the typeinfo implementation assumes that
1724 there will be only one copy of the string used as the name for
1725 each type. Therefore, if weak symbols are unavailable, the
1726 run-time library should perform a more conservative check; it
1727 should perform a string comparison, rather than an address
1728 comparison. */
1729 TREE_PUBLIC (decl) = 0;
1730 else
1732 /* Static data member template instantiations, however, cannot
1733 have multiple copies. */
1734 if (DECL_INITIAL (decl) == 0
1735 || DECL_INITIAL (decl) == error_mark_node)
1736 DECL_COMMON (decl) = 1;
1737 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1739 DECL_COMMON (decl) = 1;
1740 DECL_INITIAL (decl) = error_mark_node;
1742 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1744 /* We can't do anything useful; leave vars for explicit
1745 instantiation. */
1746 DECL_EXTERNAL (decl) = 1;
1747 DECL_NOT_REALLY_EXTERN (decl) = 0;
1751 DECL_COMDAT (decl) = 1;
1754 /* For win32 we also want to put explicit instantiations in
1755 linkonce sections, so that they will be merged with implicit
1756 instantiations; otherwise we get duplicate symbol errors.
1757 For Darwin we do not want explicit instantiations to be
1758 linkonce. */
1760 void
1761 maybe_make_one_only (tree decl)
1763 /* We used to say that this was not necessary on targets that support weak
1764 symbols, because the implicit instantiations will defer to the explicit
1765 one. However, that's not actually the case in SVR4; a strong definition
1766 after a weak one is an error. Also, not making explicit
1767 instantiations one_only means that we can end up with two copies of
1768 some template instantiations. */
1769 if (! flag_weak)
1770 return;
1772 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1773 we can get away with not emitting them if they aren't used. We need
1774 to for variables so that cp_finish_decl will update their linkage,
1775 because their DECL_INITIAL may not have been set properly yet. */
1777 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1778 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1779 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1781 make_decl_one_only (decl, cxx_comdat_group (decl));
1783 if (VAR_P (decl))
1785 varpool_node *node = varpool_node_for_decl (decl);
1786 DECL_COMDAT (decl) = 1;
1787 /* Mark it needed so we don't forget to emit it. */
1788 node->forced_by_abi = true;
1789 TREE_USED (decl) = 1;
1794 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1795 This predicate will give the right answer during parsing of the
1796 function, which other tests may not. */
1798 bool
1799 vague_linkage_p (tree decl)
1801 /* Unfortunately, import_export_decl has not always been called
1802 before the function is processed, so we cannot simply check
1803 DECL_COMDAT. */
1804 return (DECL_COMDAT (decl)
1805 || (((TREE_CODE (decl) == FUNCTION_DECL
1806 && DECL_DECLARED_INLINE_P (decl))
1807 || (DECL_LANG_SPECIFIC (decl)
1808 && DECL_TEMPLATE_INSTANTIATION (decl)))
1809 && TREE_PUBLIC (decl)));
1812 /* Determine whether or not we want to specifically import or export CTYPE,
1813 using various heuristics. */
1815 static void
1816 import_export_class (tree ctype)
1818 /* -1 for imported, 1 for exported. */
1819 int import_export = 0;
1821 /* It only makes sense to call this function at EOF. The reason is
1822 that this function looks at whether or not the first non-inline
1823 non-abstract virtual member function has been defined in this
1824 translation unit. But, we can't possibly know that until we've
1825 seen the entire translation unit. */
1826 gcc_assert (at_eof);
1828 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1829 return;
1831 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1832 we will have CLASSTYPE_INTERFACE_ONLY set but not
1833 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1834 heuristic because someone will supply a #pragma implementation
1835 elsewhere, and deducing it here would produce a conflict. */
1836 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1837 return;
1839 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1840 import_export = -1;
1841 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1842 import_export = 1;
1843 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1844 && !flag_implicit_templates)
1845 /* For a template class, without -fimplicit-templates, check the
1846 repository. If the virtual table is assigned to this
1847 translation unit, then export the class; otherwise, import
1848 it. */
1849 import_export = repo_export_class_p (ctype) ? 1 : -1;
1850 else if (TYPE_POLYMORPHIC_P (ctype))
1852 /* The ABI specifies that the virtual table and associated
1853 information are emitted with the key method, if any. */
1854 tree method = CLASSTYPE_KEY_METHOD (ctype);
1855 /* If weak symbol support is not available, then we must be
1856 careful not to emit the vtable when the key function is
1857 inline. An inline function can be defined in multiple
1858 translation units. If we were to emit the vtable in each
1859 translation unit containing a definition, we would get
1860 multiple definition errors at link-time. */
1861 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1862 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1865 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1866 a definition anywhere else. */
1867 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1868 import_export = 0;
1870 /* Allow back ends the chance to overrule the decision. */
1871 if (targetm.cxx.import_export_class)
1872 import_export = targetm.cxx.import_export_class (ctype, import_export);
1874 if (import_export)
1876 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1877 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1881 /* Return true if VAR has already been provided to the back end; in that
1882 case VAR should not be modified further by the front end. */
1883 static bool
1884 var_finalized_p (tree var)
1886 return varpool_node_for_decl (var)->definition;
1889 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1890 must be emitted in this translation unit. Mark it as such. */
1892 void
1893 mark_needed (tree decl)
1895 TREE_USED (decl) = 1;
1896 if (TREE_CODE (decl) == FUNCTION_DECL)
1898 /* Extern inline functions don't become needed when referenced.
1899 If we know a method will be emitted in other TU and no new
1900 functions can be marked reachable, just use the external
1901 definition. */
1902 struct cgraph_node *node = cgraph_get_create_node (decl);
1903 node->forced_by_abi = true;
1905 /* #pragma interface and -frepo code can call mark_needed for
1906 maybe-in-charge 'tors; mark the clones as well. */
1907 tree clone;
1908 FOR_EACH_CLONE (clone, decl)
1909 mark_needed (clone);
1911 else if (TREE_CODE (decl) == VAR_DECL)
1913 varpool_node *node = varpool_node_for_decl (decl);
1914 /* C++ frontend use mark_decl_references to force COMDAT variables
1915 to be output that might appear dead otherwise. */
1916 node->forced_by_abi = true;
1920 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1921 returns true if a definition of this entity should be provided in
1922 this object file. Callers use this function to determine whether
1923 or not to let the back end know that a definition of DECL is
1924 available in this translation unit. */
1926 bool
1927 decl_needed_p (tree decl)
1929 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1930 /* This function should only be called at the end of the translation
1931 unit. We cannot be sure of whether or not something will be
1932 COMDAT until that point. */
1933 gcc_assert (at_eof);
1935 /* All entities with external linkage that are not COMDAT should be
1936 emitted; they may be referred to from other object files. */
1937 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1938 return true;
1939 /* If this entity was used, let the back end see it; it will decide
1940 whether or not to emit it into the object file. */
1941 if (TREE_USED (decl))
1942 return true;
1943 /* Functions marked "dllexport" must be emitted so that they are
1944 visible to other DLLs. */
1945 if (flag_keep_inline_dllexport
1946 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1947 return true;
1948 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1949 reference to DECL might cause it to be emitted later. */
1950 return false;
1953 /* If necessary, write out the vtables for the dynamic class CTYPE.
1954 Returns true if any vtables were emitted. */
1956 static bool
1957 maybe_emit_vtables (tree ctype)
1959 tree vtbl;
1960 tree primary_vtbl;
1961 int needed = 0;
1962 varpool_node *current = NULL, *last = NULL;
1964 /* If the vtables for this class have already been emitted there is
1965 nothing more to do. */
1966 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1967 if (var_finalized_p (primary_vtbl))
1968 return false;
1969 /* Ignore dummy vtables made by get_vtable_decl. */
1970 if (TREE_TYPE (primary_vtbl) == void_type_node)
1971 return false;
1973 /* On some targets, we cannot determine the key method until the end
1974 of the translation unit -- which is when this function is
1975 called. */
1976 if (!targetm.cxx.key_method_may_be_inline ())
1977 determine_key_method (ctype);
1979 /* See if any of the vtables are needed. */
1980 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1982 import_export_decl (vtbl);
1983 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1984 needed = 1;
1986 if (!needed)
1988 /* If the references to this class' vtables are optimized away,
1989 still emit the appropriate debugging information. See
1990 dfs_debug_mark. */
1991 if (DECL_COMDAT (primary_vtbl)
1992 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1993 note_debug_info_needed (ctype);
1994 return false;
1997 /* The ABI requires that we emit all of the vtables if we emit any
1998 of them. */
1999 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2001 /* Mark entities references from the virtual table as used. */
2002 mark_vtable_entries (vtbl);
2004 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2006 vec<tree, va_gc> *cleanups = NULL;
2007 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2008 LOOKUP_NORMAL);
2010 /* It had better be all done at compile-time. */
2011 gcc_assert (!expr && !cleanups);
2014 /* Write it out. */
2015 DECL_EXTERNAL (vtbl) = 0;
2016 rest_of_decl_compilation (vtbl, 1, 1);
2018 /* Because we're only doing syntax-checking, we'll never end up
2019 actually marking the variable as written. */
2020 if (flag_syntax_only)
2021 TREE_ASM_WRITTEN (vtbl) = 1;
2022 else if (DECL_ONE_ONLY (vtbl))
2024 current = varpool_node_for_decl (vtbl);
2025 if (last)
2026 symtab_add_to_same_comdat_group (current, last);
2027 last = current;
2031 /* Since we're writing out the vtable here, also write the debug
2032 info. */
2033 note_debug_info_needed (ctype);
2035 return true;
2038 /* A special return value from type_visibility meaning internal
2039 linkage. */
2041 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2043 /* walk_tree helper function for type_visibility. */
2045 static tree
2046 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2048 int *vis_p = (int *)data;
2049 if (! TYPE_P (*tp))
2051 *walk_subtrees = 0;
2053 else if (OVERLOAD_TYPE_P (*tp)
2054 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2056 *vis_p = VISIBILITY_ANON;
2057 return *tp;
2059 else if (CLASS_TYPE_P (*tp)
2060 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2061 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2062 return NULL;
2065 /* Returns the visibility of TYPE, which is the minimum visibility of its
2066 component types. */
2068 static int
2069 type_visibility (tree type)
2071 int vis = VISIBILITY_DEFAULT;
2072 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2073 return vis;
2076 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2077 specified (or if VISIBILITY is static). If TMPL is true, this
2078 constraint is for a template argument, and takes precedence
2079 over explicitly-specified visibility on the template. */
2081 static void
2082 constrain_visibility (tree decl, int visibility, bool tmpl)
2084 if (visibility == VISIBILITY_ANON)
2086 /* extern "C" declarations aren't affected by the anonymous
2087 namespace. */
2088 if (!DECL_EXTERN_C_P (decl))
2090 TREE_PUBLIC (decl) = 0;
2091 DECL_WEAK (decl) = 0;
2092 DECL_COMMON (decl) = 0;
2093 DECL_COMDAT_GROUP (decl) = NULL_TREE;
2094 DECL_INTERFACE_KNOWN (decl) = 1;
2095 if (DECL_LANG_SPECIFIC (decl))
2096 DECL_NOT_REALLY_EXTERN (decl) = 1;
2099 else if (visibility > DECL_VISIBILITY (decl)
2100 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2102 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2103 /* This visibility was not specified. */
2104 DECL_VISIBILITY_SPECIFIED (decl) = false;
2108 /* Constrain the visibility of DECL based on the visibility of its template
2109 arguments. */
2111 static void
2112 constrain_visibility_for_template (tree decl, tree targs)
2114 /* If this is a template instantiation, check the innermost
2115 template args for visibility constraints. The outer template
2116 args are covered by the class check. */
2117 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2118 int i;
2119 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2121 int vis = 0;
2123 tree arg = TREE_VEC_ELT (args, i-1);
2124 if (TYPE_P (arg))
2125 vis = type_visibility (arg);
2126 else
2128 if (REFERENCE_REF_P (arg))
2129 arg = TREE_OPERAND (arg, 0);
2130 if (TREE_TYPE (arg))
2131 STRIP_NOPS (arg);
2132 if (TREE_CODE (arg) == ADDR_EXPR)
2133 arg = TREE_OPERAND (arg, 0);
2134 if (VAR_OR_FUNCTION_DECL_P (arg))
2136 if (! TREE_PUBLIC (arg))
2137 vis = VISIBILITY_ANON;
2138 else
2139 vis = DECL_VISIBILITY (arg);
2142 if (vis)
2143 constrain_visibility (decl, vis, true);
2147 /* Like c_determine_visibility, but with additional C++-specific
2148 behavior.
2150 Function-scope entities can rely on the function's visibility because
2151 it is set in start_preparsed_function.
2153 Class-scope entities cannot rely on the class's visibility until the end
2154 of the enclosing class definition.
2156 Note that because namespaces have multiple independent definitions,
2157 namespace visibility is handled elsewhere using the #pragma visibility
2158 machinery rather than by decorating the namespace declaration.
2160 The goal is for constraints from the type to give a diagnostic, and
2161 other constraints to be applied silently. */
2163 void
2164 determine_visibility (tree decl)
2166 tree class_type = NULL_TREE;
2167 bool use_template;
2168 bool orig_visibility_specified;
2169 enum symbol_visibility orig_visibility;
2171 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2173 /* Only relevant for names with external linkage. */
2174 if (!TREE_PUBLIC (decl))
2175 return;
2177 /* Cloned constructors and destructors get the same visibility as
2178 the underlying function. That should be set up in
2179 maybe_clone_body. */
2180 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2182 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2183 orig_visibility = DECL_VISIBILITY (decl);
2185 if (TREE_CODE (decl) == TYPE_DECL)
2187 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2188 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2189 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2190 use_template = 1;
2191 else
2192 use_template = 0;
2194 else if (DECL_LANG_SPECIFIC (decl))
2195 use_template = DECL_USE_TEMPLATE (decl);
2196 else
2197 use_template = 0;
2199 /* If DECL is a member of a class, visibility specifiers on the
2200 class can influence the visibility of the DECL. */
2201 if (DECL_CLASS_SCOPE_P (decl))
2202 class_type = DECL_CONTEXT (decl);
2203 else
2205 /* Not a class member. */
2207 /* Virtual tables have DECL_CONTEXT set to their associated class,
2208 so they are automatically handled above. */
2209 gcc_assert (!VAR_P (decl)
2210 || !DECL_VTABLE_OR_VTT_P (decl));
2212 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2214 /* Local statics and classes get the visibility of their
2215 containing function by default, except that
2216 -fvisibility-inlines-hidden doesn't affect them. */
2217 tree fn = DECL_CONTEXT (decl);
2218 if (DECL_VISIBILITY_SPECIFIED (fn))
2220 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2221 DECL_VISIBILITY_SPECIFIED (decl) =
2222 DECL_VISIBILITY_SPECIFIED (fn);
2224 else
2226 if (DECL_CLASS_SCOPE_P (fn))
2227 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2228 else if (determine_hidden_inline (fn))
2230 DECL_VISIBILITY (decl) = default_visibility;
2231 DECL_VISIBILITY_SPECIFIED (decl) =
2232 visibility_options.inpragma;
2234 else
2236 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2237 DECL_VISIBILITY_SPECIFIED (decl) =
2238 DECL_VISIBILITY_SPECIFIED (fn);
2242 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2243 but have no TEMPLATE_INFO, so don't try to check it. */
2244 use_template = 0;
2246 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2247 && flag_visibility_ms_compat)
2249 /* Under -fvisibility-ms-compat, types are visible by default,
2250 even though their contents aren't. */
2251 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2252 int underlying_vis = type_visibility (underlying_type);
2253 if (underlying_vis == VISIBILITY_ANON
2254 || (CLASS_TYPE_P (underlying_type)
2255 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2256 constrain_visibility (decl, underlying_vis, false);
2257 else
2258 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2260 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2262 /* tinfo visibility is based on the type it's for. */
2263 constrain_visibility
2264 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2266 /* Give the target a chance to override the visibility associated
2267 with DECL. */
2268 if (TREE_PUBLIC (decl)
2269 && !DECL_REALLY_EXTERN (decl)
2270 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2271 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2272 targetm.cxx.determine_class_data_visibility (decl);
2274 else if (use_template)
2275 /* Template instantiations and specializations get visibility based
2276 on their template unless they override it with an attribute. */;
2277 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2279 if (determine_hidden_inline (decl))
2280 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2281 else
2283 /* Set default visibility to whatever the user supplied with
2284 #pragma GCC visibility or a namespace visibility attribute. */
2285 DECL_VISIBILITY (decl) = default_visibility;
2286 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2291 if (use_template)
2293 /* If the specialization doesn't specify visibility, use the
2294 visibility from the template. */
2295 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2296 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2297 : DECL_TEMPLATE_INFO (decl));
2298 tree args = TI_ARGS (tinfo);
2299 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2300 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2301 : DECL_ATTRIBUTES (decl));
2303 if (args != error_mark_node)
2305 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2307 if (!DECL_VISIBILITY_SPECIFIED (decl))
2309 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2310 && determine_hidden_inline (decl))
2311 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2312 else
2314 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2315 DECL_VISIBILITY_SPECIFIED (decl)
2316 = DECL_VISIBILITY_SPECIFIED (pattern);
2320 if (args
2321 /* Template argument visibility outweighs #pragma or namespace
2322 visibility, but not an explicit attribute. */
2323 && !lookup_attribute ("visibility", attribs))
2325 int depth = TMPL_ARGS_DEPTH (args);
2326 if (DECL_VISIBILITY_SPECIFIED (decl))
2328 /* A class template member with explicit visibility
2329 overrides the class visibility, so we need to apply
2330 all the levels of template args directly. */
2331 int i;
2332 for (i = 1; i <= depth; ++i)
2334 tree lev = TMPL_ARGS_LEVEL (args, i);
2335 constrain_visibility_for_template (decl, lev);
2338 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2339 /* Limit visibility based on its template arguments. */
2340 constrain_visibility_for_template (decl, args);
2345 if (class_type)
2346 determine_visibility_from_class (decl, class_type);
2348 if (decl_anon_ns_mem_p (decl))
2349 /* Names in an anonymous namespace get internal linkage.
2350 This might change once we implement export. */
2351 constrain_visibility (decl, VISIBILITY_ANON, false);
2352 else if (TREE_CODE (decl) != TYPE_DECL)
2354 /* Propagate anonymity from type to decl. */
2355 int tvis = type_visibility (TREE_TYPE (decl));
2356 if (tvis == VISIBILITY_ANON
2357 || ! DECL_VISIBILITY_SPECIFIED (decl))
2358 constrain_visibility (decl, tvis, false);
2360 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2361 /* DR 757: A type without linkage shall not be used as the type of a
2362 variable or function with linkage, unless
2363 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2364 o the variable or function is not used (3.2 [basic.def.odr]) or is
2365 defined in the same translation unit.
2367 Since non-extern "C" decls need to be defined in the same
2368 translation unit, we can make the type internal. */
2369 constrain_visibility (decl, VISIBILITY_ANON, false);
2371 /* If visibility changed and DECL already has DECL_RTL, ensure
2372 symbol flags are updated. */
2373 if ((DECL_VISIBILITY (decl) != orig_visibility
2374 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2375 && ((VAR_P (decl) && TREE_STATIC (decl))
2376 || TREE_CODE (decl) == FUNCTION_DECL)
2377 && DECL_RTL_SET_P (decl))
2378 make_decl_rtl (decl);
2381 /* By default, static data members and function members receive
2382 the visibility of their containing class. */
2384 static void
2385 determine_visibility_from_class (tree decl, tree class_type)
2387 if (DECL_VISIBILITY_SPECIFIED (decl))
2388 return;
2390 if (determine_hidden_inline (decl))
2391 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2392 else
2394 /* Default to the class visibility. */
2395 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2396 DECL_VISIBILITY_SPECIFIED (decl)
2397 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2400 /* Give the target a chance to override the visibility associated
2401 with DECL. */
2402 if (VAR_P (decl)
2403 && (DECL_TINFO_P (decl)
2404 || (DECL_VTABLE_OR_VTT_P (decl)
2405 /* Construction virtual tables are not exported because
2406 they cannot be referred to from other object files;
2407 their name is not standardized by the ABI. */
2408 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2409 && TREE_PUBLIC (decl)
2410 && !DECL_REALLY_EXTERN (decl)
2411 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2412 targetm.cxx.determine_class_data_visibility (decl);
2415 /* Returns true iff DECL is an inline that should get hidden visibility
2416 because of -fvisibility-inlines-hidden. */
2418 static bool
2419 determine_hidden_inline (tree decl)
2421 return (visibility_options.inlines_hidden
2422 /* Don't do this for inline templates; specializations might not be
2423 inline, and we don't want them to inherit the hidden
2424 visibility. We'll set it here for all inline instantiations. */
2425 && !processing_template_decl
2426 && TREE_CODE (decl) == FUNCTION_DECL
2427 && DECL_DECLARED_INLINE_P (decl)
2428 && (! DECL_LANG_SPECIFIC (decl)
2429 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2432 /* Constrain the visibility of a class TYPE based on the visibility of its
2433 field types. Warn if any fields require lesser visibility. */
2435 void
2436 constrain_class_visibility (tree type)
2438 tree binfo;
2439 tree t;
2440 int i;
2442 int vis = type_visibility (type);
2444 if (vis == VISIBILITY_ANON
2445 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2446 return;
2448 /* Don't warn about visibility if the class has explicit visibility. */
2449 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2450 vis = VISIBILITY_INTERNAL;
2452 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2453 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2455 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2456 int subvis = type_visibility (ftype);
2458 if (subvis == VISIBILITY_ANON)
2460 if (!in_main_input_context ())
2461 warning (0, "\
2462 %qT has a field %qD whose type uses the anonymous namespace",
2463 type, t);
2465 else if (MAYBE_CLASS_TYPE_P (ftype)
2466 && vis < VISIBILITY_HIDDEN
2467 && subvis >= VISIBILITY_HIDDEN)
2468 warning (OPT_Wattributes, "\
2469 %qT declared with greater visibility than the type of its field %qD",
2470 type, t);
2473 binfo = TYPE_BINFO (type);
2474 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2476 int subvis = type_visibility (TREE_TYPE (t));
2478 if (subvis == VISIBILITY_ANON)
2480 if (!in_main_input_context())
2481 warning (0, "\
2482 %qT has a base %qT whose type uses the anonymous namespace",
2483 type, TREE_TYPE (t));
2485 else if (vis < VISIBILITY_HIDDEN
2486 && subvis >= VISIBILITY_HIDDEN)
2487 warning (OPT_Wattributes, "\
2488 %qT declared with greater visibility than its base %qT",
2489 type, TREE_TYPE (t));
2493 /* Functions for adjusting the visibility of a tagged type and its nested
2494 types and declarations when it gets a name for linkage purposes from a
2495 typedef. */
2497 static void bt_reset_linkage_1 (binding_entry, void *);
2498 static void bt_reset_linkage_2 (binding_entry, void *);
2500 /* First reset the visibility of all the types. */
2502 static void
2503 reset_type_linkage_1 (tree type)
2505 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2506 if (CLASS_TYPE_P (type))
2507 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2508 bt_reset_linkage_1, NULL);
2510 static void
2511 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2513 reset_type_linkage_1 (b->type);
2516 /* Then reset the visibility of any static data members or member
2517 functions that use those types. */
2519 static void
2520 reset_decl_linkage (tree decl)
2522 if (TREE_PUBLIC (decl))
2523 return;
2524 if (DECL_CLONED_FUNCTION_P (decl))
2525 return;
2526 TREE_PUBLIC (decl) = true;
2527 DECL_INTERFACE_KNOWN (decl) = false;
2528 determine_visibility (decl);
2529 tentative_decl_linkage (decl);
2531 static void
2532 reset_type_linkage_2 (tree type)
2534 if (CLASS_TYPE_P (type))
2536 if (tree vt = CLASSTYPE_VTABLES (type))
2538 tree name = mangle_vtbl_for_type (type);
2539 DECL_NAME (vt) = name;
2540 SET_DECL_ASSEMBLER_NAME (vt, name);
2541 reset_decl_linkage (vt);
2543 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2545 tree name = mangle_typeinfo_for_type (type);
2546 DECL_NAME (ti) = name;
2547 SET_DECL_ASSEMBLER_NAME (ti, name);
2548 TREE_TYPE (name) = type;
2549 reset_decl_linkage (ti);
2551 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2552 if (TREE_CODE (m) == VAR_DECL)
2553 reset_decl_linkage (m);
2554 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2555 reset_decl_linkage (m);
2556 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2557 bt_reset_linkage_2, NULL);
2560 static void
2561 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2563 reset_type_linkage_2 (b->type);
2565 void
2566 reset_type_linkage (tree type)
2568 reset_type_linkage_1 (type);
2569 reset_type_linkage_2 (type);
2572 /* Set up our initial idea of what the linkage of DECL should be. */
2574 void
2575 tentative_decl_linkage (tree decl)
2577 if (DECL_INTERFACE_KNOWN (decl))
2578 /* We've already made a decision as to how this function will
2579 be handled. */;
2580 else if (vague_linkage_p (decl))
2582 if (TREE_CODE (decl) == FUNCTION_DECL
2583 && decl_defined_p (decl))
2585 DECL_EXTERNAL (decl) = 1;
2586 DECL_NOT_REALLY_EXTERN (decl) = 1;
2587 note_vague_linkage_fn (decl);
2588 /* A non-template inline function with external linkage will
2589 always be COMDAT. As we must eventually determine the
2590 linkage of all functions, and as that causes writes to
2591 the data mapped in from the PCH file, it's advantageous
2592 to mark the functions at this point. */
2593 if (DECL_DECLARED_INLINE_P (decl)
2594 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2595 || DECL_DEFAULTED_FN (decl)))
2597 /* This function must have external linkage, as
2598 otherwise DECL_INTERFACE_KNOWN would have been
2599 set. */
2600 gcc_assert (TREE_PUBLIC (decl));
2601 comdat_linkage (decl);
2602 DECL_INTERFACE_KNOWN (decl) = 1;
2605 else if (TREE_CODE (decl) == VAR_DECL)
2606 maybe_commonize_var (decl);
2610 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2611 for DECL has not already been determined, do so now by setting
2612 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2613 function is called entities with vague linkage whose definitions
2614 are available must have TREE_PUBLIC set.
2616 If this function decides to place DECL in COMDAT, it will set
2617 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2618 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2619 callers defer that decision until it is clear that DECL is actually
2620 required. */
2622 void
2623 import_export_decl (tree decl)
2625 int emit_p;
2626 bool comdat_p;
2627 bool import_p;
2628 tree class_type = NULL_TREE;
2630 if (DECL_INTERFACE_KNOWN (decl))
2631 return;
2633 /* We cannot determine what linkage to give to an entity with vague
2634 linkage until the end of the file. For example, a virtual table
2635 for a class will be defined if and only if the key method is
2636 defined in this translation unit. As a further example, consider
2637 that when compiling a translation unit that uses PCH file with
2638 "-frepo" it would be incorrect to make decisions about what
2639 entities to emit when building the PCH; those decisions must be
2640 delayed until the repository information has been processed. */
2641 gcc_assert (at_eof);
2642 /* Object file linkage for explicit instantiations is handled in
2643 mark_decl_instantiated. For static variables in functions with
2644 vague linkage, maybe_commonize_var is used.
2646 Therefore, the only declarations that should be provided to this
2647 function are those with external linkage that are:
2649 * implicit instantiations of function templates
2651 * inline function
2653 * implicit instantiations of static data members of class
2654 templates
2656 * virtual tables
2658 * typeinfo objects
2660 Furthermore, all entities that reach this point must have a
2661 definition available in this translation unit.
2663 The following assertions check these conditions. */
2664 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2665 /* Any code that creates entities with TREE_PUBLIC cleared should
2666 also set DECL_INTERFACE_KNOWN. */
2667 gcc_assert (TREE_PUBLIC (decl));
2668 if (TREE_CODE (decl) == FUNCTION_DECL)
2669 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2670 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2671 || DECL_DECLARED_INLINE_P (decl));
2672 else
2673 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2674 || DECL_VTABLE_OR_VTT_P (decl)
2675 || DECL_TINFO_P (decl));
2676 /* Check that a definition of DECL is available in this translation
2677 unit. */
2678 gcc_assert (!DECL_REALLY_EXTERN (decl));
2680 /* Assume that DECL will not have COMDAT linkage. */
2681 comdat_p = false;
2682 /* Assume that DECL will not be imported into this translation
2683 unit. */
2684 import_p = false;
2686 /* See if the repository tells us whether or not to emit DECL in
2687 this translation unit. */
2688 emit_p = repo_emit_p (decl);
2689 if (emit_p == 0)
2690 import_p = true;
2691 else if (emit_p == 1)
2693 /* The repository indicates that this entity should be defined
2694 here. Make sure the back end honors that request. */
2695 mark_needed (decl);
2696 /* Output the definition as an ordinary strong definition. */
2697 DECL_EXTERNAL (decl) = 0;
2698 DECL_INTERFACE_KNOWN (decl) = 1;
2699 return;
2702 if (import_p)
2703 /* We have already decided what to do with this DECL; there is no
2704 need to check anything further. */
2706 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2708 class_type = DECL_CONTEXT (decl);
2709 import_export_class (class_type);
2710 if (TYPE_FOR_JAVA (class_type))
2711 import_p = true;
2712 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2713 && CLASSTYPE_INTERFACE_ONLY (class_type))
2714 import_p = true;
2715 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2716 && !CLASSTYPE_USE_TEMPLATE (class_type)
2717 && CLASSTYPE_KEY_METHOD (class_type)
2718 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2719 /* The ABI requires that all virtual tables be emitted with
2720 COMDAT linkage. However, on systems where COMDAT symbols
2721 don't show up in the table of contents for a static
2722 archive, or on systems without weak symbols (where we
2723 approximate COMDAT linkage by using internal linkage), the
2724 linker will report errors about undefined symbols because
2725 it will not see the virtual table definition. Therefore,
2726 in the case that we know that the virtual table will be
2727 emitted in only one translation unit, we make the virtual
2728 table an ordinary definition with external linkage. */
2729 DECL_EXTERNAL (decl) = 0;
2730 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2732 /* CLASS_TYPE is being exported from this translation unit,
2733 so DECL should be defined here. */
2734 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2735 /* If a class is declared in a header with the "extern
2736 template" extension, then it will not be instantiated,
2737 even in translation units that would normally require
2738 it. Often such classes are explicitly instantiated in
2739 one translation unit. Therefore, the explicit
2740 instantiation must be made visible to other translation
2741 units. */
2742 DECL_EXTERNAL (decl) = 0;
2743 else
2745 /* The generic C++ ABI says that class data is always
2746 COMDAT, even if there is a key function. Some
2747 variants (e.g., the ARM EABI) says that class data
2748 only has COMDAT linkage if the class data might be
2749 emitted in more than one translation unit. When the
2750 key method can be inline and is inline, we still have
2751 to arrange for comdat even though
2752 class_data_always_comdat is false. */
2753 if (!CLASSTYPE_KEY_METHOD (class_type)
2754 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2755 || targetm.cxx.class_data_always_comdat ())
2757 /* The ABI requires COMDAT linkage. Normally, we
2758 only emit COMDAT things when they are needed;
2759 make sure that we realize that this entity is
2760 indeed needed. */
2761 comdat_p = true;
2762 mark_needed (decl);
2766 else if (!flag_implicit_templates
2767 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2768 import_p = true;
2769 else
2770 comdat_p = true;
2772 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2774 tree type = TREE_TYPE (DECL_NAME (decl));
2775 if (CLASS_TYPE_P (type))
2777 class_type = type;
2778 import_export_class (type);
2779 if (CLASSTYPE_INTERFACE_KNOWN (type)
2780 && TYPE_POLYMORPHIC_P (type)
2781 && CLASSTYPE_INTERFACE_ONLY (type)
2782 /* If -fno-rtti was specified, then we cannot be sure
2783 that RTTI information will be emitted with the
2784 virtual table of the class, so we must emit it
2785 wherever it is used. */
2786 && flag_rtti)
2787 import_p = true;
2788 else
2790 if (CLASSTYPE_INTERFACE_KNOWN (type)
2791 && !CLASSTYPE_INTERFACE_ONLY (type))
2793 comdat_p = (targetm.cxx.class_data_always_comdat ()
2794 || (CLASSTYPE_KEY_METHOD (type)
2795 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2796 mark_needed (decl);
2797 if (!flag_weak)
2799 comdat_p = false;
2800 DECL_EXTERNAL (decl) = 0;
2803 else
2804 comdat_p = true;
2807 else
2808 comdat_p = true;
2810 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2812 /* DECL is an implicit instantiation of a function or static
2813 data member. */
2814 if ((flag_implicit_templates
2815 && !flag_use_repository)
2816 || (flag_implicit_inline_templates
2817 && TREE_CODE (decl) == FUNCTION_DECL
2818 && DECL_DECLARED_INLINE_P (decl)))
2819 comdat_p = true;
2820 else
2821 /* If we are not implicitly generating templates, then mark
2822 this entity as undefined in this translation unit. */
2823 import_p = true;
2825 else if (DECL_FUNCTION_MEMBER_P (decl))
2827 if (!DECL_DECLARED_INLINE_P (decl))
2829 tree ctype = DECL_CONTEXT (decl);
2830 import_export_class (ctype);
2831 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2833 DECL_NOT_REALLY_EXTERN (decl)
2834 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2835 || (DECL_DECLARED_INLINE_P (decl)
2836 && ! flag_implement_inlines
2837 && !DECL_VINDEX (decl)));
2839 if (!DECL_NOT_REALLY_EXTERN (decl))
2840 DECL_EXTERNAL (decl) = 1;
2842 /* Always make artificials weak. */
2843 if (DECL_ARTIFICIAL (decl) && flag_weak)
2844 comdat_p = true;
2845 else
2846 maybe_make_one_only (decl);
2849 else
2850 comdat_p = true;
2852 else
2853 comdat_p = true;
2855 if (import_p)
2857 /* If we are importing DECL into this translation unit, mark is
2858 an undefined here. */
2859 DECL_EXTERNAL (decl) = 1;
2860 DECL_NOT_REALLY_EXTERN (decl) = 0;
2862 else if (comdat_p)
2864 /* If we decided to put DECL in COMDAT, mark it accordingly at
2865 this point. */
2866 comdat_linkage (decl);
2869 DECL_INTERFACE_KNOWN (decl) = 1;
2872 /* Return an expression that performs the destruction of DECL, which
2873 must be a VAR_DECL whose type has a non-trivial destructor, or is
2874 an array whose (innermost) elements have a non-trivial destructor. */
2876 tree
2877 build_cleanup (tree decl)
2879 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2880 gcc_assert (clean != NULL_TREE);
2881 return clean;
2884 /* Returns the initialization guard variable for the variable DECL,
2885 which has static storage duration. */
2887 tree
2888 get_guard (tree decl)
2890 tree sname;
2891 tree guard;
2893 sname = mangle_guard_variable (decl);
2894 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2895 if (! guard)
2897 tree guard_type;
2899 /* We use a type that is big enough to contain a mutex as well
2900 as an integer counter. */
2901 guard_type = targetm.cxx.guard_type ();
2902 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2903 VAR_DECL, sname, guard_type);
2905 /* The guard should have the same linkage as what it guards. */
2906 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2907 TREE_STATIC (guard) = TREE_STATIC (decl);
2908 DECL_COMMON (guard) = DECL_COMMON (decl);
2909 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2910 DECL_TLS_MODEL (guard) = DECL_TLS_MODEL (decl);
2911 if (DECL_ONE_ONLY (decl))
2912 make_decl_one_only (guard, cxx_comdat_group (guard));
2913 if (TREE_PUBLIC (decl))
2914 DECL_WEAK (guard) = DECL_WEAK (decl);
2915 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2916 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2918 DECL_ARTIFICIAL (guard) = 1;
2919 DECL_IGNORED_P (guard) = 1;
2920 TREE_USED (guard) = 1;
2921 pushdecl_top_level_and_finish (guard, NULL_TREE);
2923 return guard;
2926 /* Return those bits of the GUARD variable that should be set when the
2927 guarded entity is actually initialized. */
2929 static tree
2930 get_guard_bits (tree guard)
2932 if (!targetm.cxx.guard_mask_bit ())
2934 /* We only set the first byte of the guard, in order to leave room
2935 for a mutex in the high-order bits. */
2936 guard = build1 (ADDR_EXPR,
2937 build_pointer_type (TREE_TYPE (guard)),
2938 guard);
2939 guard = build1 (NOP_EXPR,
2940 build_pointer_type (char_type_node),
2941 guard);
2942 guard = build1 (INDIRECT_REF, char_type_node, guard);
2945 return guard;
2948 /* Return an expression which determines whether or not the GUARD
2949 variable has already been initialized. */
2951 tree
2952 get_guard_cond (tree guard)
2954 tree guard_value;
2956 /* Check to see if the GUARD is zero. */
2957 guard = get_guard_bits (guard);
2959 /* Mask off all but the low bit. */
2960 if (targetm.cxx.guard_mask_bit ())
2962 guard_value = integer_one_node;
2963 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2964 guard_value = convert (TREE_TYPE (guard), guard_value);
2965 guard = cp_build_binary_op (input_location,
2966 BIT_AND_EXPR, guard, guard_value,
2967 tf_warning_or_error);
2970 guard_value = integer_zero_node;
2971 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2972 guard_value = convert (TREE_TYPE (guard), guard_value);
2973 return cp_build_binary_op (input_location,
2974 EQ_EXPR, guard, guard_value,
2975 tf_warning_or_error);
2978 /* Return an expression which sets the GUARD variable, indicating that
2979 the variable being guarded has been initialized. */
2981 tree
2982 set_guard (tree guard)
2984 tree guard_init;
2986 /* Set the GUARD to one. */
2987 guard = get_guard_bits (guard);
2988 guard_init = integer_one_node;
2989 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2990 guard_init = convert (TREE_TYPE (guard), guard_init);
2991 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2992 tf_warning_or_error);
2995 /* Returns true iff we can tell that VAR does not have a dynamic
2996 initializer. */
2998 static bool
2999 var_defined_without_dynamic_init (tree var)
3001 /* If it's defined in another TU, we can't tell. */
3002 if (DECL_EXTERNAL (var))
3003 return false;
3004 /* If it has a non-trivial destructor, registering the destructor
3005 counts as dynamic initialization. */
3006 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3007 return false;
3008 /* If it's in this TU, its initializer has been processed. */
3009 gcc_assert (DECL_INITIALIZED_P (var));
3010 /* If it has no initializer or a constant one, it's not dynamic. */
3011 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3012 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3015 /* Returns true iff VAR is a variable that needs uses to be
3016 wrapped for possible dynamic initialization. */
3018 static bool
3019 var_needs_tls_wrapper (tree var)
3021 return (!error_operand_p (var)
3022 && DECL_THREAD_LOCAL_P (var)
3023 && !DECL_GNU_TLS_P (var)
3024 && !DECL_FUNCTION_SCOPE_P (var)
3025 && !var_defined_without_dynamic_init (var));
3028 /* Get the FUNCTION_DECL for the shared TLS init function for this
3029 translation unit. */
3031 static tree
3032 get_local_tls_init_fn (void)
3034 tree sname = get_identifier ("__tls_init");
3035 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3036 if (!fn)
3038 fn = build_lang_decl (FUNCTION_DECL, sname,
3039 build_function_type (void_type_node,
3040 void_list_node));
3041 SET_DECL_LANGUAGE (fn, lang_c);
3042 TREE_PUBLIC (fn) = false;
3043 DECL_ARTIFICIAL (fn) = true;
3044 mark_used (fn);
3045 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3047 return fn;
3050 /* Get a FUNCTION_DECL for the init function for the thread_local
3051 variable VAR. The init function will be an alias to the function
3052 that initializes all the non-local TLS variables in the translation
3053 unit. The init function is only used by the wrapper function. */
3055 static tree
3056 get_tls_init_fn (tree var)
3058 /* Only C++11 TLS vars need this init fn. */
3059 if (!var_needs_tls_wrapper (var))
3060 return NULL_TREE;
3062 /* If -fno-extern-tls-init, assume that we don't need to call
3063 a tls init function for a variable defined in another TU. */
3064 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3065 return NULL_TREE;
3067 #ifdef ASM_OUTPUT_DEF
3068 /* If the variable is internal, or if we can't generate aliases,
3069 call the local init function directly. */
3070 if (!TREE_PUBLIC (var))
3071 #endif
3072 return get_local_tls_init_fn ();
3074 tree sname = mangle_tls_init_fn (var);
3075 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3076 if (!fn)
3078 fn = build_lang_decl (FUNCTION_DECL, sname,
3079 build_function_type (void_type_node,
3080 void_list_node));
3081 SET_DECL_LANGUAGE (fn, lang_c);
3082 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3083 DECL_ARTIFICIAL (fn) = true;
3084 DECL_COMDAT (fn) = DECL_COMDAT (var);
3085 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3086 if (DECL_ONE_ONLY (var))
3087 make_decl_one_only (fn, cxx_comdat_group (fn));
3088 if (TREE_PUBLIC (var))
3090 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3091 /* If the variable is defined somewhere else and might have static
3092 initialization, make the init function a weak reference. */
3093 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3094 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3095 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3096 && DECL_EXTERNAL (var))
3097 declare_weak (fn);
3098 else
3099 DECL_WEAK (fn) = DECL_WEAK (var);
3101 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3102 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3103 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3104 DECL_IGNORED_P (fn) = 1;
3105 mark_used (fn);
3107 DECL_BEFRIENDING_CLASSES (fn) = var;
3109 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3111 return fn;
3114 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3115 variable VAR. The wrapper function calls the init function (if any) for
3116 VAR and then returns a reference to VAR. The wrapper function is used
3117 in place of VAR everywhere VAR is mentioned. */
3119 tree
3120 get_tls_wrapper_fn (tree var)
3122 /* Only C++11 TLS vars need this wrapper fn. */
3123 if (!var_needs_tls_wrapper (var))
3124 return NULL_TREE;
3126 tree sname = mangle_tls_wrapper_fn (var);
3127 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3128 if (!fn)
3130 /* A named rvalue reference is an lvalue, so the wrapper should
3131 always return an lvalue reference. */
3132 tree type = non_reference (TREE_TYPE (var));
3133 type = build_reference_type (type);
3134 tree fntype = build_function_type (type, void_list_node);
3135 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3136 SET_DECL_LANGUAGE (fn, lang_c);
3137 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3138 DECL_ARTIFICIAL (fn) = true;
3139 DECL_IGNORED_P (fn) = 1;
3140 /* The wrapper is inline and emitted everywhere var is used. */
3141 DECL_DECLARED_INLINE_P (fn) = true;
3142 if (TREE_PUBLIC (var))
3144 comdat_linkage (fn);
3145 #ifdef HAVE_GAS_HIDDEN
3146 /* Make the wrapper bind locally; there's no reason to share
3147 the wrapper between multiple shared objects. */
3148 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3149 DECL_VISIBILITY_SPECIFIED (fn) = true;
3150 #endif
3152 if (!TREE_PUBLIC (fn))
3153 DECL_INTERFACE_KNOWN (fn) = true;
3154 mark_used (fn);
3155 note_vague_linkage_fn (fn);
3157 #if 0
3158 /* We want CSE to commonize calls to the wrapper, but marking it as
3159 pure is unsafe since it has side-effects. I guess we need a new
3160 ECF flag even weaker than ECF_PURE. FIXME! */
3161 DECL_PURE_P (fn) = true;
3162 #endif
3164 DECL_BEFRIENDING_CLASSES (fn) = var;
3166 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3168 return fn;
3171 /* At EOF, generate the definition for the TLS wrapper function FN:
3173 T& var_wrapper() {
3174 if (init_fn) init_fn();
3175 return var;
3176 } */
3178 static void
3179 generate_tls_wrapper (tree fn)
3181 tree var = DECL_BEFRIENDING_CLASSES (fn);
3183 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3184 tree body = begin_function_body ();
3185 /* Only call the init fn if there might be one. */
3186 if (tree init_fn = get_tls_init_fn (var))
3188 tree if_stmt = NULL_TREE;
3189 /* If init_fn is a weakref, make sure it exists before calling. */
3190 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3192 if_stmt = begin_if_stmt ();
3193 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3194 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3195 NE_EXPR, addr, nullptr_node,
3196 tf_warning_or_error);
3197 finish_if_stmt_cond (cond, if_stmt);
3199 finish_expr_stmt (build_cxx_call
3200 (init_fn, 0, NULL, tf_warning_or_error));
3201 if (if_stmt)
3203 finish_then_clause (if_stmt);
3204 finish_if_stmt (if_stmt);
3207 else
3208 /* If there's no initialization, the wrapper is a constant function. */
3209 TREE_READONLY (fn) = true;
3210 finish_return_stmt (convert_from_reference (var));
3211 finish_function_body (body);
3212 expand_or_defer_fn (finish_function (0));
3215 /* Start the process of running a particular set of global constructors
3216 or destructors. Subroutine of do_[cd]tors. Also called from
3217 vtv_start_verification_constructor_init_function. */
3219 static tree
3220 start_objects (int method_type, int initp)
3222 tree body;
3223 tree fndecl;
3224 char type[14];
3226 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3228 if (initp != DEFAULT_INIT_PRIORITY)
3230 char joiner;
3232 #ifdef JOINER
3233 joiner = JOINER;
3234 #else
3235 joiner = '_';
3236 #endif
3238 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3240 else
3241 sprintf (type, "sub_%c", method_type);
3243 fndecl = build_lang_decl (FUNCTION_DECL,
3244 get_file_function_name (type),
3245 build_function_type_list (void_type_node,
3246 NULL_TREE));
3247 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3249 TREE_PUBLIC (current_function_decl) = 0;
3251 /* Mark as artificial because it's not explicitly in the user's
3252 source code. */
3253 DECL_ARTIFICIAL (current_function_decl) = 1;
3255 /* Mark this declaration as used to avoid spurious warnings. */
3256 TREE_USED (current_function_decl) = 1;
3258 /* Mark this function as a global constructor or destructor. */
3259 if (method_type == 'I')
3260 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3261 else
3262 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3264 body = begin_compound_stmt (BCS_FN_BODY);
3266 return body;
3269 /* Finish the process of running a particular set of global constructors
3270 or destructors. Subroutine of do_[cd]tors. */
3272 static void
3273 finish_objects (int method_type, int initp, tree body)
3275 tree fn;
3277 /* Finish up. */
3278 finish_compound_stmt (body);
3279 fn = finish_function (0);
3281 if (method_type == 'I')
3283 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3284 decl_init_priority_insert (fn, initp);
3286 else
3288 DECL_STATIC_DESTRUCTOR (fn) = 1;
3289 decl_fini_priority_insert (fn, initp);
3292 expand_or_defer_fn (fn);
3295 /* The names of the parameters to the function created to handle
3296 initializations and destructions for objects with static storage
3297 duration. */
3298 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3299 #define PRIORITY_IDENTIFIER "__priority"
3301 /* The name of the function we create to handle initializations and
3302 destructions for objects with static storage duration. */
3303 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3305 /* The declaration for the __INITIALIZE_P argument. */
3306 static GTY(()) tree initialize_p_decl;
3308 /* The declaration for the __PRIORITY argument. */
3309 static GTY(()) tree priority_decl;
3311 /* The declaration for the static storage duration function. */
3312 static GTY(()) tree ssdf_decl;
3314 /* All the static storage duration functions created in this
3315 translation unit. */
3316 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3318 /* A map from priority levels to information about that priority
3319 level. There may be many such levels, so efficient lookup is
3320 important. */
3321 static splay_tree priority_info_map;
3323 /* Begins the generation of the function that will handle all
3324 initialization and destruction of objects with static storage
3325 duration. The function generated takes two parameters of type
3326 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3327 nonzero, it performs initializations. Otherwise, it performs
3328 destructions. It only performs those initializations or
3329 destructions with the indicated __PRIORITY. The generated function
3330 returns no value.
3332 It is assumed that this function will only be called once per
3333 translation unit. */
3335 static tree
3336 start_static_storage_duration_function (unsigned count)
3338 tree type;
3339 tree body;
3340 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3342 /* Create the identifier for this function. It will be of the form
3343 SSDF_IDENTIFIER_<number>. */
3344 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3346 type = build_function_type_list (void_type_node,
3347 integer_type_node, integer_type_node,
3348 NULL_TREE);
3350 /* Create the FUNCTION_DECL itself. */
3351 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3352 get_identifier (id),
3353 type);
3354 TREE_PUBLIC (ssdf_decl) = 0;
3355 DECL_ARTIFICIAL (ssdf_decl) = 1;
3357 /* Put this function in the list of functions to be called from the
3358 static constructors and destructors. */
3359 if (!ssdf_decls)
3361 vec_alloc (ssdf_decls, 32);
3363 /* Take this opportunity to initialize the map from priority
3364 numbers to information about that priority level. */
3365 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3366 /*delete_key_fn=*/0,
3367 /*delete_value_fn=*/
3368 (splay_tree_delete_value_fn) &free);
3370 /* We always need to generate functions for the
3371 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3372 priorities later, we'll be sure to find the
3373 DEFAULT_INIT_PRIORITY. */
3374 get_priority_info (DEFAULT_INIT_PRIORITY);
3377 vec_safe_push (ssdf_decls, ssdf_decl);
3379 /* Create the argument list. */
3380 initialize_p_decl = cp_build_parm_decl
3381 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3382 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3383 TREE_USED (initialize_p_decl) = 1;
3384 priority_decl = cp_build_parm_decl
3385 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3386 DECL_CONTEXT (priority_decl) = ssdf_decl;
3387 TREE_USED (priority_decl) = 1;
3389 DECL_CHAIN (initialize_p_decl) = priority_decl;
3390 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3392 /* Put the function in the global scope. */
3393 pushdecl (ssdf_decl);
3395 /* Start the function itself. This is equivalent to declaring the
3396 function as:
3398 static void __ssdf (int __initialize_p, init __priority_p);
3400 It is static because we only need to call this function from the
3401 various constructor and destructor functions for this module. */
3402 start_preparsed_function (ssdf_decl,
3403 /*attrs=*/NULL_TREE,
3404 SF_PRE_PARSED);
3406 /* Set up the scope of the outermost block in the function. */
3407 body = begin_compound_stmt (BCS_FN_BODY);
3409 return body;
3412 /* Finish the generation of the function which performs initialization
3413 and destruction of objects with static storage duration. After
3414 this point, no more such objects can be created. */
3416 static void
3417 finish_static_storage_duration_function (tree body)
3419 /* Close out the function. */
3420 finish_compound_stmt (body);
3421 expand_or_defer_fn (finish_function (0));
3424 /* Return the information about the indicated PRIORITY level. If no
3425 code to handle this level has yet been generated, generate the
3426 appropriate prologue. */
3428 static priority_info
3429 get_priority_info (int priority)
3431 priority_info pi;
3432 splay_tree_node n;
3434 n = splay_tree_lookup (priority_info_map,
3435 (splay_tree_key) priority);
3436 if (!n)
3438 /* Create a new priority information structure, and insert it
3439 into the map. */
3440 pi = XNEW (struct priority_info_s);
3441 pi->initializations_p = 0;
3442 pi->destructions_p = 0;
3443 splay_tree_insert (priority_info_map,
3444 (splay_tree_key) priority,
3445 (splay_tree_value) pi);
3447 else
3448 pi = (priority_info) n->value;
3450 return pi;
3453 /* The effective initialization priority of a DECL. */
3455 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3456 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3457 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3459 /* Whether a DECL needs a guard to protect it against multiple
3460 initialization. */
3462 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3463 || DECL_ONE_ONLY (decl) \
3464 || DECL_WEAK (decl)))
3466 /* Called from one_static_initialization_or_destruction(),
3467 via walk_tree.
3468 Walks the initializer list of a global variable and looks for
3469 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3470 and that have their DECL_CONTEXT() == NULL.
3471 For each such temporary variable, set their DECL_CONTEXT() to
3472 the current function. This is necessary because otherwise
3473 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3474 when trying to refer to a temporary variable that does not have
3475 it's DECL_CONTECT() properly set. */
3476 static tree
3477 fix_temporary_vars_context_r (tree *node,
3478 int * /*unused*/,
3479 void * /*unused1*/)
3481 gcc_assert (current_function_decl);
3483 if (TREE_CODE (*node) == BIND_EXPR)
3485 tree var;
3487 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3488 if (VAR_P (var)
3489 && !DECL_NAME (var)
3490 && DECL_ARTIFICIAL (var)
3491 && !DECL_CONTEXT (var))
3492 DECL_CONTEXT (var) = current_function_decl;
3495 return NULL_TREE;
3498 /* Set up to handle the initialization or destruction of DECL. If
3499 INITP is nonzero, we are initializing the variable. Otherwise, we
3500 are destroying it. */
3502 static void
3503 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3505 tree guard_if_stmt = NULL_TREE;
3506 tree guard;
3508 /* If we are supposed to destruct and there's a trivial destructor,
3509 nothing has to be done. */
3510 if (!initp
3511 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3512 return;
3514 /* Trick the compiler into thinking we are at the file and line
3515 where DECL was declared so that error-messages make sense, and so
3516 that the debugger will show somewhat sensible file and line
3517 information. */
3518 input_location = DECL_SOURCE_LOCATION (decl);
3520 /* Make sure temporary variables in the initialiser all have
3521 their DECL_CONTEXT() set to a value different from NULL_TREE.
3522 This can happen when global variables initialisers are built.
3523 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3524 the temporary variables that might have been generated in the
3525 accompagning initialisers is NULL_TREE, meaning the variables have been
3526 declared in the global namespace.
3527 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3528 of the temporaries are set to the current function decl. */
3529 cp_walk_tree_without_duplicates (&init,
3530 fix_temporary_vars_context_r,
3531 NULL);
3533 /* Because of:
3535 [class.access.spec]
3537 Access control for implicit calls to the constructors,
3538 the conversion functions, or the destructor called to
3539 create and destroy a static data member is performed as
3540 if these calls appeared in the scope of the member's
3541 class.
3543 we pretend we are in a static member function of the class of
3544 which the DECL is a member. */
3545 if (member_p (decl))
3547 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3548 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3551 /* Assume we don't need a guard. */
3552 guard = NULL_TREE;
3553 /* We need a guard if this is an object with external linkage that
3554 might be initialized in more than one place. (For example, a
3555 static data member of a template, when the data member requires
3556 construction.) */
3557 if (NEEDS_GUARD_P (decl))
3559 tree guard_cond;
3561 guard = get_guard (decl);
3563 /* When using __cxa_atexit, we just check the GUARD as we would
3564 for a local static. */
3565 if (flag_use_cxa_atexit)
3567 /* When using __cxa_atexit, we never try to destroy
3568 anything from a static destructor. */
3569 gcc_assert (initp);
3570 guard_cond = get_guard_cond (guard);
3572 /* If we don't have __cxa_atexit, then we will be running
3573 destructors from .fini sections, or their equivalents. So,
3574 we need to know how many times we've tried to initialize this
3575 object. We do initializations only if the GUARD is zero,
3576 i.e., if we are the first to initialize the variable. We do
3577 destructions only if the GUARD is one, i.e., if we are the
3578 last to destroy the variable. */
3579 else if (initp)
3580 guard_cond
3581 = cp_build_binary_op (input_location,
3582 EQ_EXPR,
3583 cp_build_unary_op (PREINCREMENT_EXPR,
3584 guard,
3585 /*noconvert=*/1,
3586 tf_warning_or_error),
3587 integer_one_node,
3588 tf_warning_or_error);
3589 else
3590 guard_cond
3591 = cp_build_binary_op (input_location,
3592 EQ_EXPR,
3593 cp_build_unary_op (PREDECREMENT_EXPR,
3594 guard,
3595 /*noconvert=*/1,
3596 tf_warning_or_error),
3597 integer_zero_node,
3598 tf_warning_or_error);
3600 guard_if_stmt = begin_if_stmt ();
3601 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3605 /* If we're using __cxa_atexit, we have not already set the GUARD,
3606 so we must do so now. */
3607 if (guard && initp && flag_use_cxa_atexit)
3608 finish_expr_stmt (set_guard (guard));
3610 /* Perform the initialization or destruction. */
3611 if (initp)
3613 if (init)
3615 finish_expr_stmt (init);
3616 if (flag_sanitize & SANITIZE_ADDRESS)
3618 varpool_node *vnode = varpool_get_node (decl);
3619 if (vnode)
3620 vnode->dynamically_initialized = 1;
3624 /* If we're using __cxa_atexit, register a function that calls the
3625 destructor for the object. */
3626 if (flag_use_cxa_atexit)
3627 finish_expr_stmt (register_dtor_fn (decl));
3629 else
3630 finish_expr_stmt (build_cleanup (decl));
3632 /* Finish the guard if-stmt, if necessary. */
3633 if (guard)
3635 finish_then_clause (guard_if_stmt);
3636 finish_if_stmt (guard_if_stmt);
3639 /* Now that we're done with DECL we don't need to pretend to be a
3640 member of its class any longer. */
3641 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3642 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3645 /* Generate code to do the initialization or destruction of the decls in VARS,
3646 a TREE_LIST of VAR_DECL with static storage duration.
3647 Whether initialization or destruction is performed is specified by INITP. */
3649 static void
3650 do_static_initialization_or_destruction (tree vars, bool initp)
3652 tree node, init_if_stmt, cond;
3654 /* Build the outer if-stmt to check for initialization or destruction. */
3655 init_if_stmt = begin_if_stmt ();
3656 cond = initp ? integer_one_node : integer_zero_node;
3657 cond = cp_build_binary_op (input_location,
3658 EQ_EXPR,
3659 initialize_p_decl,
3660 cond,
3661 tf_warning_or_error);
3662 finish_if_stmt_cond (cond, init_if_stmt);
3664 /* To make sure dynamic construction doesn't access globals from other
3665 compilation units where they might not be yet constructed, for
3666 -fsanitize=address insert __asan_before_dynamic_init call that
3667 prevents access to either all global variables that need construction
3668 in other compilation units, or at least those that haven't been
3669 initialized yet. Variables that need dynamic construction in
3670 the current compilation unit are kept accessible. */
3671 if (flag_sanitize & SANITIZE_ADDRESS)
3672 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3674 node = vars;
3675 do {
3676 tree decl = TREE_VALUE (node);
3677 tree priority_if_stmt;
3678 int priority;
3679 priority_info pi;
3681 /* If we don't need a destructor, there's nothing to do. Avoid
3682 creating a possibly empty if-stmt. */
3683 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3685 node = TREE_CHAIN (node);
3686 continue;
3689 /* Remember that we had an initialization or finalization at this
3690 priority. */
3691 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3692 pi = get_priority_info (priority);
3693 if (initp)
3694 pi->initializations_p = 1;
3695 else
3696 pi->destructions_p = 1;
3698 /* Conditionalize this initialization on being in the right priority
3699 and being initializing/finalizing appropriately. */
3700 priority_if_stmt = begin_if_stmt ();
3701 cond = cp_build_binary_op (input_location,
3702 EQ_EXPR,
3703 priority_decl,
3704 build_int_cst (NULL_TREE, priority),
3705 tf_warning_or_error);
3706 finish_if_stmt_cond (cond, priority_if_stmt);
3708 /* Process initializers with same priority. */
3709 for (; node
3710 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3711 node = TREE_CHAIN (node))
3712 /* Do one initialization or destruction. */
3713 one_static_initialization_or_destruction (TREE_VALUE (node),
3714 TREE_PURPOSE (node), initp);
3716 /* Finish up the priority if-stmt body. */
3717 finish_then_clause (priority_if_stmt);
3718 finish_if_stmt (priority_if_stmt);
3720 } while (node);
3722 /* Revert what __asan_before_dynamic_init did by calling
3723 __asan_after_dynamic_init. */
3724 if (flag_sanitize & SANITIZE_ADDRESS)
3725 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3727 /* Finish up the init/destruct if-stmt body. */
3728 finish_then_clause (init_if_stmt);
3729 finish_if_stmt (init_if_stmt);
3732 /* VARS is a list of variables with static storage duration which may
3733 need initialization and/or finalization. Remove those variables
3734 that don't really need to be initialized or finalized, and return
3735 the resulting list. The order in which the variables appear in
3736 VARS is in reverse order of the order in which they should actually
3737 be initialized. The list we return is in the unreversed order;
3738 i.e., the first variable should be initialized first. */
3740 static tree
3741 prune_vars_needing_no_initialization (tree *vars)
3743 tree *var = vars;
3744 tree result = NULL_TREE;
3746 while (*var)
3748 tree t = *var;
3749 tree decl = TREE_VALUE (t);
3750 tree init = TREE_PURPOSE (t);
3752 /* Deal gracefully with error. */
3753 if (decl == error_mark_node)
3755 var = &TREE_CHAIN (t);
3756 continue;
3759 /* The only things that can be initialized are variables. */
3760 gcc_assert (VAR_P (decl));
3762 /* If this object is not defined, we don't need to do anything
3763 here. */
3764 if (DECL_EXTERNAL (decl))
3766 var = &TREE_CHAIN (t);
3767 continue;
3770 /* Also, if the initializer already contains errors, we can bail
3771 out now. */
3772 if (init && TREE_CODE (init) == TREE_LIST
3773 && value_member (error_mark_node, init))
3775 var = &TREE_CHAIN (t);
3776 continue;
3779 /* This variable is going to need initialization and/or
3780 finalization, so we add it to the list. */
3781 *var = TREE_CHAIN (t);
3782 TREE_CHAIN (t) = result;
3783 result = t;
3786 return result;
3789 /* Make sure we have told the back end about all the variables in
3790 VARS. */
3792 static void
3793 write_out_vars (tree vars)
3795 tree v;
3797 for (v = vars; v; v = TREE_CHAIN (v))
3799 tree var = TREE_VALUE (v);
3800 if (!var_finalized_p (var))
3802 import_export_decl (var);
3803 rest_of_decl_compilation (var, 1, 1);
3808 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3809 (otherwise) that will initialize all global objects with static
3810 storage duration having the indicated PRIORITY. */
3812 static void
3813 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3814 location_t *locus)
3816 char function_key;
3817 tree fndecl;
3818 tree body;
3819 size_t i;
3821 input_location = *locus;
3822 /* ??? */
3823 /* Was: locus->line++; */
3825 /* We use `I' to indicate initialization and `D' to indicate
3826 destruction. */
3827 function_key = constructor_p ? 'I' : 'D';
3829 /* We emit the function lazily, to avoid generating empty
3830 global constructors and destructors. */
3831 body = NULL_TREE;
3833 /* For Objective-C++, we may need to initialize metadata found in this module.
3834 This must be done _before_ any other static initializations. */
3835 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3836 && constructor_p && objc_static_init_needed_p ())
3838 body = start_objects (function_key, priority);
3839 objc_generate_static_init_call (NULL_TREE);
3842 /* Call the static storage duration function with appropriate
3843 arguments. */
3844 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3846 /* Calls to pure or const functions will expand to nothing. */
3847 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3849 tree call;
3851 if (! body)
3852 body = start_objects (function_key, priority);
3854 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3855 build_int_cst (NULL_TREE,
3856 constructor_p),
3857 build_int_cst (NULL_TREE,
3858 priority),
3859 NULL_TREE);
3860 finish_expr_stmt (call);
3864 /* Close out the function. */
3865 if (body)
3866 finish_objects (function_key, priority, body);
3869 /* Generate constructor and destructor functions for the priority
3870 indicated by N. */
3872 static int
3873 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3875 location_t *locus = (location_t *) data;
3876 int priority = (int) n->key;
3877 priority_info pi = (priority_info) n->value;
3879 /* Generate the functions themselves, but only if they are really
3880 needed. */
3881 if (pi->initializations_p)
3882 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3883 if (pi->destructions_p)
3884 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3886 /* Keep iterating. */
3887 return 0;
3890 /* Java requires that we be able to reference a local address for a
3891 method, and not be confused by PLT entries. If hidden aliases are
3892 supported, collect and return all the functions for which we should
3893 emit a hidden alias. */
3895 static struct pointer_set_t *
3896 collect_candidates_for_java_method_aliases (void)
3898 struct cgraph_node *node;
3899 struct pointer_set_t *candidates = NULL;
3901 #ifndef HAVE_GAS_HIDDEN
3902 return candidates;
3903 #endif
3905 FOR_EACH_FUNCTION (node)
3907 tree fndecl = node->decl;
3909 if (DECL_CLASS_SCOPE_P (fndecl)
3910 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3911 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3913 if (candidates == NULL)
3914 candidates = pointer_set_create ();
3915 pointer_set_insert (candidates, fndecl);
3919 return candidates;
3923 /* Java requires that we be able to reference a local address for a
3924 method, and not be confused by PLT entries. If hidden aliases are
3925 supported, emit one for each java function that we've emitted.
3926 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3927 by collect_candidates_for_java_method_aliases. */
3929 static void
3930 build_java_method_aliases (struct pointer_set_t *candidates)
3932 struct cgraph_node *node;
3934 #ifndef HAVE_GAS_HIDDEN
3935 return;
3936 #endif
3938 FOR_EACH_FUNCTION (node)
3940 tree fndecl = node->decl;
3942 if (TREE_ASM_WRITTEN (fndecl)
3943 && pointer_set_contains (candidates, fndecl))
3945 /* Mangle the name in a predictable way; we need to reference
3946 this from a java compiled object file. */
3947 tree oid, nid, alias;
3948 const char *oname;
3949 char *nname;
3951 oid = DECL_ASSEMBLER_NAME (fndecl);
3952 oname = IDENTIFIER_POINTER (oid);
3953 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3954 nname = ACONCAT (("_ZGA", oname+2, NULL));
3955 nid = get_identifier (nname);
3957 alias = make_alias_for (fndecl, nid);
3958 TREE_PUBLIC (alias) = 1;
3959 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3961 assemble_alias (alias, oid);
3966 /* Return C++ property of T, based on given operation OP. */
3968 static int
3969 cpp_check (tree t, cpp_operation op)
3971 switch (op)
3973 case IS_ABSTRACT:
3974 return DECL_PURE_VIRTUAL_P (t);
3975 case IS_CONSTRUCTOR:
3976 return DECL_CONSTRUCTOR_P (t);
3977 case IS_DESTRUCTOR:
3978 return DECL_DESTRUCTOR_P (t);
3979 case IS_COPY_CONSTRUCTOR:
3980 return DECL_COPY_CONSTRUCTOR_P (t);
3981 case IS_TEMPLATE:
3982 return TREE_CODE (t) == TEMPLATE_DECL;
3983 case IS_TRIVIAL:
3984 return trivial_type_p (t);
3985 default:
3986 return 0;
3990 /* Collect source file references recursively, starting from NAMESPC. */
3992 static void
3993 collect_source_refs (tree namespc)
3995 tree t;
3997 if (!namespc)
3998 return;
4000 /* Iterate over names in this name space. */
4001 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4002 if (!DECL_IS_BUILTIN (t) )
4003 collect_source_ref (DECL_SOURCE_FILE (t));
4005 /* Dump siblings, if any */
4006 collect_source_refs (TREE_CHAIN (namespc));
4008 /* Dump children, if any */
4009 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4012 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4013 starting from NAMESPC. */
4015 static void
4016 collect_ada_namespace (tree namespc, const char *source_file)
4018 if (!namespc)
4019 return;
4021 /* Collect decls from this namespace */
4022 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4024 /* Collect siblings, if any */
4025 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4027 /* Collect children, if any */
4028 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4031 /* Returns true iff there is a definition available for variable or
4032 function DECL. */
4034 static bool
4035 decl_defined_p (tree decl)
4037 if (TREE_CODE (decl) == FUNCTION_DECL)
4038 return (DECL_INITIAL (decl) != NULL_TREE);
4039 else
4041 gcc_assert (VAR_P (decl));
4042 return !DECL_EXTERNAL (decl);
4046 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4048 [expr.const]
4050 An integral constant-expression can only involve ... const
4051 variables of integral or enumeration types initialized with
4052 constant expressions ...
4054 C++0x also allows constexpr variables and temporaries initialized
4055 with constant expressions. We handle the former here, but the latter
4056 are just folded away in cxx_eval_constant_expression.
4058 The standard does not require that the expression be non-volatile.
4059 G++ implements the proposed correction in DR 457. */
4061 bool
4062 decl_constant_var_p (tree decl)
4064 if (!decl_maybe_constant_var_p (decl))
4065 return false;
4067 /* We don't know if a template static data member is initialized with
4068 a constant expression until we instantiate its initializer. Even
4069 in the case of a constexpr variable, we can't treat it as a
4070 constant until its initializer is complete in case it's used in
4071 its own initializer. */
4072 mark_used (decl);
4073 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4076 /* Returns true if DECL could be a symbolic constant variable, depending on
4077 its initializer. */
4079 bool
4080 decl_maybe_constant_var_p (tree decl)
4082 tree type = TREE_TYPE (decl);
4083 if (!VAR_P (decl))
4084 return false;
4085 if (DECL_DECLARED_CONSTEXPR_P (decl))
4086 return true;
4087 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4088 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4091 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4092 called from grokfndecl and grokvardecl; in all modes it is called from
4093 cp_write_global_declarations. */
4095 void
4096 no_linkage_error (tree decl)
4098 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4099 /* In C++11 it's ok if the decl is defined. */
4100 return;
4101 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4102 if (t == NULL_TREE)
4103 /* The type that got us on no_linkage_decls must have gotten a name for
4104 linkage purposes. */;
4105 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4106 /* The type might end up having a typedef name for linkage purposes. */
4107 vec_safe_push (no_linkage_decls, decl);
4108 else if (TYPE_ANONYMOUS_P (t))
4110 bool d = false;
4111 if (cxx_dialect >= cxx11)
4112 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4113 "anonymous type, is used but never defined", decl);
4114 else if (DECL_EXTERN_C_P (decl))
4115 /* Allow this; it's pretty common in C. */;
4116 else if (TREE_CODE (decl) == VAR_DECL)
4117 /* DRs 132, 319 and 389 seem to indicate types with
4118 no linkage can only be used to declare extern "C"
4119 entities. Since it's not always an error in the
4120 ISO C++ 90 Standard, we only issue a warning. */
4121 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4122 "with no linkage used to declare variable %q#D with "
4123 "linkage", decl);
4124 else
4125 d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4126 "linkage used to declare function %q#D with linkage",
4127 decl);
4128 if (d && is_typedef_decl (TYPE_NAME (t)))
4129 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4130 "to the unqualified type, so it is not used for linkage",
4131 TYPE_NAME (t));
4133 else if (cxx_dialect >= cxx11)
4134 permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4135 "%qT, is used but never defined", decl, t);
4136 else if (TREE_CODE (decl) == VAR_DECL)
4137 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4138 "used to declare variable %q#D with linkage", t, decl);
4139 else
4140 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4141 "to declare function %q#D with linkage", t, decl);
4144 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4146 static void
4147 collect_all_refs (const char *source_file)
4149 collect_ada_namespace (global_namespace, source_file);
4152 /* Clear DECL_EXTERNAL for NODE. */
4154 static bool
4155 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4157 DECL_EXTERNAL (node->decl) = 0;
4158 return false;
4161 /* Build up the function to run dynamic initializers for thread_local
4162 variables in this translation unit and alias the init functions for the
4163 individual variables to it. */
4165 static void
4166 handle_tls_init (void)
4168 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4169 if (vars == NULL_TREE)
4170 return;
4172 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4174 write_out_vars (vars);
4176 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4177 boolean_type_node);
4178 TREE_PUBLIC (guard) = false;
4179 TREE_STATIC (guard) = true;
4180 DECL_ARTIFICIAL (guard) = true;
4181 DECL_IGNORED_P (guard) = true;
4182 TREE_USED (guard) = true;
4183 DECL_TLS_MODEL (guard) = decl_default_tls_model (guard);
4184 pushdecl_top_level_and_finish (guard, NULL_TREE);
4186 tree fn = get_local_tls_init_fn ();
4187 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4188 tree body = begin_function_body ();
4189 tree if_stmt = begin_if_stmt ();
4190 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4191 tf_warning_or_error);
4192 finish_if_stmt_cond (cond, if_stmt);
4193 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4194 tf_warning_or_error));
4195 for (; vars; vars = TREE_CHAIN (vars))
4197 tree var = TREE_VALUE (vars);
4198 tree init = TREE_PURPOSE (vars);
4199 one_static_initialization_or_destruction (var, init, true);
4201 #ifdef ASM_OUTPUT_DEF
4202 /* Output init aliases even with -fno-extern-tls-init. */
4203 if (TREE_PUBLIC (var))
4205 tree single_init_fn = get_tls_init_fn (var);
4206 if (single_init_fn == NULL_TREE)
4207 continue;
4208 cgraph_node *alias
4209 = cgraph_same_body_alias (cgraph_get_create_node (fn),
4210 single_init_fn, fn);
4211 gcc_assert (alias != NULL);
4213 #endif
4216 finish_then_clause (if_stmt);
4217 finish_if_stmt (if_stmt);
4218 finish_function_body (body);
4219 expand_or_defer_fn (finish_function (0));
4222 /* The entire file is now complete. If requested, dump everything
4223 to a file. */
4225 static void
4226 dump_tu (void)
4228 int flags;
4229 FILE *stream = dump_begin (TDI_tu, &flags);
4231 if (stream)
4233 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4234 dump_end (TDI_tu, stream);
4238 /* Much like the above, but not necessarily defined. 4.9 hack for setting
4239 DECL_COMDAT on DECL_EXTERNAL functions, along with set_comdat. */
4241 void
4242 note_comdat_fn (tree decl)
4244 vec_safe_push (maybe_comdat_fns, decl);
4247 /* DECL is a function with vague linkage that was not
4248 instantiated/synthesized in this translation unit. Set DECL_COMDAT for
4249 the benefit of can_refer_decl_in_current_unit_p. */
4251 static void
4252 set_comdat (tree decl)
4254 DECL_COMDAT (decl) = true;
4256 tree clone;
4257 FOR_EACH_CLONE (clone, decl)
4258 set_comdat (clone);
4260 if (DECL_VIRTUAL_P (decl))
4261 for (tree thunk = DECL_THUNKS (decl); thunk;
4262 thunk = DECL_CHAIN (thunk))
4263 DECL_COMDAT (thunk) = true;
4266 /* This routine is called at the end of compilation.
4267 Its job is to create all the code needed to initialize and
4268 destroy the global aggregates. We do the destruction
4269 first, since that way we only need to reverse the decls once. */
4271 void
4272 cp_write_global_declarations (void)
4274 tree vars;
4275 bool reconsider;
4276 size_t i;
4277 location_t locus;
4278 unsigned ssdf_count = 0;
4279 int retries = 0;
4280 tree decl;
4281 struct pointer_set_t *candidates;
4283 locus = input_location;
4284 at_eof = 1;
4286 /* Bad parse errors. Just forget about it. */
4287 if (! global_bindings_p () || current_class_type
4288 || !vec_safe_is_empty (decl_namespace_list))
4289 return;
4291 /* This is the point to write out a PCH if we're doing that.
4292 In that case we do not want to do anything else. */
4293 if (pch_file)
4295 c_common_write_pch ();
4296 dump_tu ();
4297 return;
4300 cgraph_process_same_body_aliases ();
4302 /* Handle -fdump-ada-spec[-slim] */
4303 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4305 if (flag_dump_ada_spec_slim)
4306 collect_source_ref (main_input_filename);
4307 else
4308 collect_source_refs (global_namespace);
4310 dump_ada_specs (collect_all_refs, cpp_check);
4313 /* FIXME - huh? was input_line -= 1;*/
4315 timevar_start (TV_PHASE_DEFERRED);
4317 /* We now have to write out all the stuff we put off writing out.
4318 These include:
4320 o Template specializations that we have not yet instantiated,
4321 but which are needed.
4322 o Initialization and destruction for non-local objects with
4323 static storage duration. (Local objects with static storage
4324 duration are initialized when their scope is first entered,
4325 and are cleaned up via atexit.)
4326 o Virtual function tables.
4328 All of these may cause others to be needed. For example,
4329 instantiating one function may cause another to be needed, and
4330 generating the initializer for an object may cause templates to be
4331 instantiated, etc., etc. */
4333 emit_support_tinfos ();
4337 tree t;
4338 tree decl;
4340 reconsider = false;
4342 /* If there are templates that we've put off instantiating, do
4343 them now. */
4344 instantiate_pending_templates (retries);
4345 ggc_collect ();
4347 /* Write out virtual tables as required. Note that writing out
4348 the virtual table for a template class may cause the
4349 instantiation of members of that class. If we write out
4350 vtables then we remove the class from our list so we don't
4351 have to look at it again. */
4353 while (keyed_classes != NULL_TREE
4354 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4356 reconsider = true;
4357 keyed_classes = TREE_CHAIN (keyed_classes);
4360 t = keyed_classes;
4361 if (t != NULL_TREE)
4363 tree next = TREE_CHAIN (t);
4365 while (next)
4367 if (maybe_emit_vtables (TREE_VALUE (next)))
4369 reconsider = true;
4370 TREE_CHAIN (t) = TREE_CHAIN (next);
4372 else
4373 t = next;
4375 next = TREE_CHAIN (t);
4379 /* Write out needed type info variables. We have to be careful
4380 looping through unemitted decls, because emit_tinfo_decl may
4381 cause other variables to be needed. New elements will be
4382 appended, and we remove from the vector those that actually
4383 get emitted. */
4384 for (i = unemitted_tinfo_decls->length ();
4385 unemitted_tinfo_decls->iterate (--i, &t);)
4386 if (emit_tinfo_decl (t))
4388 reconsider = true;
4389 unemitted_tinfo_decls->unordered_remove (i);
4392 /* The list of objects with static storage duration is built up
4393 in reverse order. We clear STATIC_AGGREGATES so that any new
4394 aggregates added during the initialization of these will be
4395 initialized in the correct order when we next come around the
4396 loop. */
4397 vars = prune_vars_needing_no_initialization (&static_aggregates);
4399 if (vars)
4401 /* We need to start a new initialization function each time
4402 through the loop. That's because we need to know which
4403 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4404 isn't computed until a function is finished, and written
4405 out. That's a deficiency in the back end. When this is
4406 fixed, these initialization functions could all become
4407 inline, with resulting performance improvements. */
4408 tree ssdf_body;
4410 /* Set the line and file, so that it is obviously not from
4411 the source file. */
4412 input_location = locus;
4413 ssdf_body = start_static_storage_duration_function (ssdf_count);
4415 /* Make sure the back end knows about all the variables. */
4416 write_out_vars (vars);
4418 /* First generate code to do all the initializations. */
4419 if (vars)
4420 do_static_initialization_or_destruction (vars, /*initp=*/true);
4422 /* Then, generate code to do all the destructions. Do these
4423 in reverse order so that the most recently constructed
4424 variable is the first destroyed. If we're using
4425 __cxa_atexit, then we don't need to do this; functions
4426 were registered at initialization time to destroy the
4427 local statics. */
4428 if (!flag_use_cxa_atexit && vars)
4430 vars = nreverse (vars);
4431 do_static_initialization_or_destruction (vars, /*initp=*/false);
4433 else
4434 vars = NULL_TREE;
4436 /* Finish up the static storage duration function for this
4437 round. */
4438 input_location = locus;
4439 finish_static_storage_duration_function (ssdf_body);
4441 /* All those initializations and finalizations might cause
4442 us to need more inline functions, more template
4443 instantiations, etc. */
4444 reconsider = true;
4445 ssdf_count++;
4446 /* ??? was: locus.line++; */
4449 /* Now do the same for thread_local variables. */
4450 handle_tls_init ();
4452 /* Go through the set of inline functions whose bodies have not
4453 been emitted yet. If out-of-line copies of these functions
4454 are required, emit them. */
4455 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4457 /* Does it need synthesizing? */
4458 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4459 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4461 /* Even though we're already at the top-level, we push
4462 there again. That way, when we pop back a few lines
4463 hence, all of our state is restored. Otherwise,
4464 finish_function doesn't clean things up, and we end
4465 up with CURRENT_FUNCTION_DECL set. */
4466 push_to_top_level ();
4467 /* The decl's location will mark where it was first
4468 needed. Save that so synthesize method can indicate
4469 where it was needed from, in case of error */
4470 input_location = DECL_SOURCE_LOCATION (decl);
4471 synthesize_method (decl);
4472 pop_from_top_level ();
4473 reconsider = true;
4476 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4477 generate_tls_wrapper (decl);
4479 if (!DECL_SAVED_TREE (decl))
4480 continue;
4482 /* We lie to the back end, pretending that some functions
4483 are not defined when they really are. This keeps these
4484 functions from being put out unnecessarily. But, we must
4485 stop lying when the functions are referenced, or if they
4486 are not comdat since they need to be put out now. If
4487 DECL_INTERFACE_KNOWN, then we have already set
4488 DECL_EXTERNAL appropriately, so there's no need to check
4489 again, and we do not want to clear DECL_EXTERNAL if a
4490 previous call to import_export_decl set it.
4492 This is done in a separate for cycle, because if some
4493 deferred function is contained in another deferred
4494 function later in deferred_fns varray,
4495 rest_of_compilation would skip this function and we
4496 really cannot expand the same function twice. */
4497 import_export_decl (decl);
4498 if (DECL_NOT_REALLY_EXTERN (decl)
4499 && DECL_INITIAL (decl)
4500 && decl_needed_p (decl))
4502 struct cgraph_node *node, *next;
4504 node = cgraph_get_node (decl);
4505 if (node->cpp_implicit_alias)
4506 node = cgraph_alias_target (node);
4508 cgraph_for_node_and_aliases (node, clear_decl_external,
4509 NULL, true);
4510 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4511 group, we need to mark all symbols in the same comdat group
4512 that way. */
4513 if (node->same_comdat_group)
4514 for (next = cgraph (node->same_comdat_group);
4515 next != node;
4516 next = cgraph (next->same_comdat_group))
4517 cgraph_for_node_and_aliases (next, clear_decl_external,
4518 NULL, true);
4521 /* If we're going to need to write this function out, and
4522 there's already a body for it, create RTL for it now.
4523 (There might be no body if this is a method we haven't
4524 gotten around to synthesizing yet.) */
4525 if (!DECL_EXTERNAL (decl)
4526 && decl_needed_p (decl)
4527 && !TREE_ASM_WRITTEN (decl)
4528 && !cgraph_get_node (decl)->definition)
4530 /* We will output the function; no longer consider it in this
4531 loop. */
4532 DECL_DEFER_OUTPUT (decl) = 0;
4533 /* Generate RTL for this function now that we know we
4534 need it. */
4535 expand_or_defer_fn (decl);
4536 /* If we're compiling -fsyntax-only pretend that this
4537 function has been written out so that we don't try to
4538 expand it again. */
4539 if (flag_syntax_only)
4540 TREE_ASM_WRITTEN (decl) = 1;
4541 reconsider = true;
4545 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4546 reconsider = true;
4548 /* Static data members are just like namespace-scope globals. */
4549 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4551 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4552 /* Don't write it out if we haven't seen a definition. */
4553 || DECL_IN_AGGR_P (decl))
4554 continue;
4555 import_export_decl (decl);
4556 /* If this static data member is needed, provide it to the
4557 back end. */
4558 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4559 DECL_EXTERNAL (decl) = 0;
4561 if (vec_safe_length (pending_statics) != 0
4562 && wrapup_global_declarations (pending_statics->address (),
4563 pending_statics->length ()))
4564 reconsider = true;
4566 retries++;
4568 while (reconsider);
4570 /* All used inline functions must have a definition at this point. */
4571 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4573 if (/* Check online inline functions that were actually used. */
4574 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4575 /* If the definition actually was available here, then the
4576 fact that the function was not defined merely represents
4577 that for some reason (use of a template repository,
4578 #pragma interface, etc.) we decided not to emit the
4579 definition here. */
4580 && !DECL_INITIAL (decl)
4581 /* Don't complain if the template was defined. */
4582 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4583 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4584 (template_for_substitution (decl)))))
4586 warning (0, "inline function %q+D used but never defined", decl);
4587 /* Avoid a duplicate warning from check_global_declaration_1. */
4588 TREE_NO_WARNING (decl) = 1;
4592 /* So must decls that use a type with no linkage. */
4593 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4594 no_linkage_error (decl);
4596 /* Then, do the Objective-C stuff. This is where all the
4597 Objective-C module stuff gets generated (symtab,
4598 class/protocol/selector lists etc). This must be done after C++
4599 templates, destructors etc. so that selectors used in C++
4600 templates are properly allocated. */
4601 if (c_dialect_objc ())
4602 objc_write_global_declarations ();
4604 /* We give C linkage to static constructors and destructors. */
4605 push_lang_context (lang_name_c);
4607 /* Generate initialization and destruction functions for all
4608 priorities for which they are required. */
4609 if (priority_info_map)
4610 splay_tree_foreach (priority_info_map,
4611 generate_ctor_and_dtor_functions_for_priority,
4612 /*data=*/&locus);
4613 else if (c_dialect_objc () && objc_static_init_needed_p ())
4614 /* If this is obj-c++ and we need a static init, call
4615 generate_ctor_or_dtor_function. */
4616 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4617 DEFAULT_INIT_PRIORITY, &locus);
4619 /* We're done with the splay-tree now. */
4620 if (priority_info_map)
4621 splay_tree_delete (priority_info_map);
4623 /* Generate any missing aliases. */
4624 maybe_apply_pending_pragma_weaks ();
4626 /* We're done with static constructors, so we can go back to "C++"
4627 linkage now. */
4628 pop_lang_context ();
4630 /* Collect candidates for Java hidden aliases. */
4631 candidates = collect_candidates_for_java_method_aliases ();
4633 timevar_stop (TV_PHASE_DEFERRED);
4634 timevar_start (TV_PHASE_OPT_GEN);
4636 if (flag_vtable_verify)
4638 vtv_recover_class_info ();
4639 vtv_compute_class_hierarchy_transitive_closure ();
4640 vtv_build_vtable_verify_fndecl ();
4643 FOR_EACH_VEC_SAFE_ELT (maybe_comdat_fns, i, decl)
4644 if (!DECL_COMDAT (decl) && vague_linkage_p (decl))
4645 set_comdat (decl);
4647 finalize_compilation_unit ();
4649 if (flag_vtable_verify)
4651 /* Generate the special constructor initialization function that
4652 calls __VLTRegisterPairs, and give it a very high
4653 initialization priority. This must be done after
4654 finalize_compilation_unit so that we have accurate
4655 information about which vtable will actually be emitted. */
4656 vtv_generate_init_routine ();
4659 timevar_stop (TV_PHASE_OPT_GEN);
4660 timevar_start (TV_PHASE_CHECK_DBGINFO);
4662 /* Now, issue warnings about static, but not defined, functions,
4663 etc., and emit debugging information. */
4664 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4665 if (vec_safe_length (pending_statics) != 0)
4667 check_global_declarations (pending_statics->address (),
4668 pending_statics->length ());
4669 emit_debug_global_declarations (pending_statics->address (),
4670 pending_statics->length ());
4673 perform_deferred_noexcept_checks ();
4675 /* Generate hidden aliases for Java. */
4676 if (candidates)
4678 build_java_method_aliases (candidates);
4679 pointer_set_destroy (candidates);
4682 finish_repo ();
4684 /* The entire file is now complete. If requested, dump everything
4685 to a file. */
4686 dump_tu ();
4688 if (flag_detailed_statistics)
4690 dump_tree_statistics ();
4691 dump_time_statistics ();
4693 input_location = locus;
4695 #ifdef ENABLE_CHECKING
4696 validate_conversion_obstack ();
4697 #endif /* ENABLE_CHECKING */
4699 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4702 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4703 function to call in parse-tree form; it has not yet been
4704 semantically analyzed. ARGS are the arguments to the function.
4705 They have already been semantically analyzed. This may change
4706 ARGS. */
4708 tree
4709 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4710 tsubst_flags_t complain)
4712 tree orig_fn;
4713 vec<tree, va_gc> *orig_args = NULL;
4714 tree expr;
4715 tree object;
4717 orig_fn = fn;
4718 object = TREE_OPERAND (fn, 0);
4720 if (processing_template_decl)
4722 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4723 || TREE_CODE (fn) == MEMBER_REF);
4724 if (type_dependent_expression_p (fn)
4725 || any_type_dependent_arguments_p (*args))
4726 return build_nt_call_vec (fn, *args);
4728 orig_args = make_tree_vector_copy (*args);
4730 /* Transform the arguments and add the implicit "this"
4731 parameter. That must be done before the FN is transformed
4732 because we depend on the form of FN. */
4733 make_args_non_dependent (*args);
4734 object = build_non_dependent_expr (object);
4735 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4737 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4738 object = cp_build_addr_expr (object, complain);
4739 vec_safe_insert (*args, 0, object);
4741 /* Now that the arguments are done, transform FN. */
4742 fn = build_non_dependent_expr (fn);
4745 /* A qualified name corresponding to a bound pointer-to-member is
4746 represented as an OFFSET_REF:
4748 struct B { void g(); };
4749 void (B::*p)();
4750 void B::g() { (this->*p)(); } */
4751 if (TREE_CODE (fn) == OFFSET_REF)
4753 tree object_addr = cp_build_addr_expr (object, complain);
4754 fn = TREE_OPERAND (fn, 1);
4755 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4756 complain);
4757 vec_safe_insert (*args, 0, object_addr);
4760 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4761 expr = build_op_call (fn, args, complain);
4762 else
4763 expr = cp_build_function_call_vec (fn, args, complain);
4764 if (processing_template_decl && expr != error_mark_node)
4765 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4767 if (orig_args != NULL)
4768 release_tree_vector (orig_args);
4770 return expr;
4774 void
4775 check_default_args (tree x)
4777 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4778 bool saw_def = false;
4779 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4780 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4782 if (TREE_PURPOSE (arg))
4783 saw_def = true;
4784 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4786 error ("default argument missing for parameter %P of %q+#D", i, x);
4787 TREE_PURPOSE (arg) = error_mark_node;
4792 /* Return true if function DECL can be inlined. This is used to force
4793 instantiation of methods that might be interesting for inlining. */
4794 bool
4795 possibly_inlined_p (tree decl)
4797 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4798 if (DECL_UNINLINABLE (decl))
4799 return false;
4800 if (!optimize || pragma_java_exceptions)
4801 return DECL_DECLARED_INLINE_P (decl);
4802 /* When optimizing, we might inline everything when flatten
4803 attribute or heuristics inlining for size or autoinlining
4804 is used. */
4805 return true;
4808 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4809 If DECL is a specialization or implicitly declared class member,
4810 generate the actual definition. Return false if something goes
4811 wrong, true otherwise. */
4813 bool
4814 mark_used (tree decl, tsubst_flags_t complain)
4816 /* If DECL is a BASELINK for a single function, then treat it just
4817 like the DECL for the function. Otherwise, if the BASELINK is
4818 for an overloaded function, we don't know which function was
4819 actually used until after overload resolution. */
4820 if (BASELINK_P (decl))
4822 decl = BASELINK_FUNCTIONS (decl);
4823 if (really_overloaded_fn (decl))
4824 return true;
4825 decl = OVL_CURRENT (decl);
4828 /* Set TREE_USED for the benefit of -Wunused. */
4829 TREE_USED (decl) = 1;
4830 if (DECL_CLONED_FUNCTION_P (decl))
4831 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4833 /* Mark enumeration types as used. */
4834 if (TREE_CODE (decl) == CONST_DECL)
4835 used_types_insert (DECL_CONTEXT (decl));
4837 if (TREE_CODE (decl) == FUNCTION_DECL
4838 && DECL_DELETED_FN (decl))
4840 if (DECL_ARTIFICIAL (decl))
4842 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4843 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4845 /* We mark a lambda conversion op as deleted if we can't
4846 generate it properly; see maybe_add_lambda_conv_op. */
4847 sorry ("converting lambda which uses %<...%> to "
4848 "function pointer");
4849 return false;
4852 if (complain & tf_error)
4854 error ("use of deleted function %qD", decl);
4855 if (!maybe_explain_implicit_delete (decl))
4856 inform (DECL_SOURCE_LOCATION (decl), "declared here");
4858 return false;
4861 /* We can only check DECL_ODR_USED on variables or functions with
4862 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4863 might need special handling for. */
4864 if (!VAR_OR_FUNCTION_DECL_P (decl)
4865 || DECL_LANG_SPECIFIC (decl) == NULL
4866 || DECL_THUNK_P (decl))
4868 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4870 if (complain & tf_error)
4871 error ("use of %qD before deduction of %<auto%>", decl);
4872 return false;
4874 return true;
4877 /* We only want to do this processing once. We don't need to keep trying
4878 to instantiate inline templates, because unit-at-a-time will make sure
4879 we get them compiled before functions that want to inline them. */
4880 if (DECL_ODR_USED (decl))
4881 return true;
4883 /* If within finish_function, defer the rest until that function
4884 finishes, otherwise it might recurse. */
4885 if (defer_mark_used_calls)
4887 vec_safe_push (deferred_mark_used_calls, decl);
4888 return true;
4891 if (TREE_CODE (decl) == FUNCTION_DECL)
4892 maybe_instantiate_noexcept (decl);
4894 /* Normally, we can wait until instantiation-time to synthesize DECL.
4895 However, if DECL is a static data member initialized with a constant
4896 or a constexpr function, we need it right now because a reference to
4897 such a data member or a call to such function is not value-dependent.
4898 For a function that uses auto in the return type, we need to instantiate
4899 it to find out its type. For OpenMP user defined reductions, we need
4900 them instantiated for reduction clauses which inline them by hand
4901 directly. */
4902 if (DECL_LANG_SPECIFIC (decl)
4903 && DECL_TEMPLATE_INFO (decl)
4904 && (decl_maybe_constant_var_p (decl)
4905 || (TREE_CODE (decl) == FUNCTION_DECL
4906 && (DECL_DECLARED_CONSTEXPR_P (decl)
4907 || DECL_OMP_DECLARE_REDUCTION_P (decl)))
4908 || undeduced_auto_decl (decl))
4909 && !uses_template_parms (DECL_TI_ARGS (decl)))
4911 /* Instantiating a function will result in garbage collection. We
4912 must treat this situation as if we were within the body of a
4913 function so as to avoid collecting live data only referenced from
4914 the stack (such as overload resolution candidates). */
4915 ++function_depth;
4916 instantiate_decl (decl, /*defer_ok=*/false,
4917 /*expl_inst_class_mem_p=*/false);
4918 --function_depth;
4921 if (processing_template_decl || in_template_function ())
4922 return true;
4924 /* Check this too in case we're within fold_non_dependent_expr. */
4925 if (DECL_TEMPLATE_INFO (decl)
4926 && uses_template_parms (DECL_TI_ARGS (decl)))
4927 return true;
4929 require_deduced_type (decl);
4931 /* If we don't need a value, then we don't need to synthesize DECL. */
4932 if (cp_unevaluated_operand != 0)
4933 return true;
4935 DECL_ODR_USED (decl) = 1;
4936 if (DECL_CLONED_FUNCTION_P (decl))
4937 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4939 /* DR 757: A type without linkage shall not be used as the type of a
4940 variable or function with linkage, unless
4941 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4942 o the variable or function is not used (3.2 [basic.def.odr]) or is
4943 defined in the same translation unit. */
4944 if (cxx_dialect > cxx98
4945 && decl_linkage (decl) != lk_none
4946 && !DECL_EXTERN_C_P (decl)
4947 && !DECL_ARTIFICIAL (decl)
4948 && !decl_defined_p (decl)
4949 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4951 if (is_local_extern (decl))
4952 /* There's no way to define a local extern, and adding it to
4953 the vector interferes with GC, so give an error now. */
4954 no_linkage_error (decl);
4955 else
4956 vec_safe_push (no_linkage_decls, decl);
4959 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4960 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4961 /* Remember it, so we can check it was defined. */
4962 note_vague_linkage_fn (decl);
4964 /* Is it a synthesized method that needs to be synthesized? */
4965 if (TREE_CODE (decl) == FUNCTION_DECL
4966 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4967 && DECL_DEFAULTED_FN (decl)
4968 /* A function defaulted outside the class is synthesized either by
4969 cp_finish_decl or instantiate_decl. */
4970 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
4971 && ! DECL_INITIAL (decl))
4973 /* Defer virtual destructors so that thunks get the right
4974 linkage. */
4975 if (DECL_VIRTUAL_P (decl) && !at_eof)
4977 note_vague_linkage_fn (decl);
4978 return true;
4981 /* Remember the current location for a function we will end up
4982 synthesizing. Then we can inform the user where it was
4983 required in the case of error. */
4984 DECL_SOURCE_LOCATION (decl) = input_location;
4986 /* Synthesizing an implicitly defined member function will result in
4987 garbage collection. We must treat this situation as if we were
4988 within the body of a function so as to avoid collecting live data
4989 on the stack (such as overload resolution candidates).
4991 We could just let cp_write_global_declarations handle synthesizing
4992 this function by adding it to deferred_fns, but doing
4993 it at the use site produces better error messages. */
4994 ++function_depth;
4995 synthesize_method (decl);
4996 --function_depth;
4997 /* If this is a synthesized method we don't need to
4998 do the instantiation test below. */
5000 else if (VAR_OR_FUNCTION_DECL_P (decl)
5001 && DECL_TEMPLATE_INFO (decl)
5002 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5003 || always_instantiate_p (decl)))
5004 /* If this is a function or variable that is an instance of some
5005 template, we now know that we will need to actually do the
5006 instantiation. We check that DECL is not an explicit
5007 instantiation because that is not checked in instantiate_decl.
5009 We put off instantiating functions in order to improve compile
5010 times. Maintaining a stack of active functions is expensive,
5011 and the inliner knows to instantiate any functions it might
5012 need. Therefore, we always try to defer instantiation. */
5014 ++function_depth;
5015 instantiate_decl (decl, /*defer_ok=*/true,
5016 /*expl_inst_class_mem_p=*/false);
5017 --function_depth;
5020 return true;
5023 bool
5024 mark_used (tree decl)
5026 return mark_used (decl, tf_warning_or_error);
5029 tree
5030 vtv_start_verification_constructor_init_function (void)
5032 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5035 tree
5036 vtv_finish_verification_constructor_init_function (tree function_body)
5038 tree fn;
5040 finish_compound_stmt (function_body);
5041 fn = finish_function (0);
5042 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5043 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5045 return fn;
5048 #include "gt-cp-decl2.h"