Record system include paths in module info; handle -fsigned-char properly
[official-gcc.git] / gcc-4_8 / gcc / cp / decl2.c
blob94989cb0c691a8a72bcdd6f5ed33b61c52e082f8
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2013 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 "flags.h"
35 #include "cp-tree.h"
36 #include "decl.h"
37 #include "toplev.h"
38 #include "timevar.h"
39 #include "cpplib.h"
40 #include "target.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
43 #include "cgraph.h"
44 #include "tree-inline.h"
45 #include "c-family/c-pragma.h"
46 #include "dumpfile.h"
47 #include "intl.h"
48 #include "gimple.h"
49 #include "pointer-set.h"
50 #include "splay-tree.h"
51 #include "langhooks.h"
52 #include "c-family/c-ada-spec.h"
54 extern cpp_reader *parse_in;
56 /* This structure contains information about the initializations
57 and/or destructions required for a particular priority level. */
58 typedef struct priority_info_s {
59 /* Nonzero if there have been any initializations at this priority
60 throughout the translation unit. */
61 int initializations_p;
62 /* Nonzero if there have been any destructions at this priority
63 throughout the translation unit. */
64 int destructions_p;
65 } *priority_info;
67 static void mark_vtable_entries (tree);
68 static bool maybe_emit_vtables (tree);
69 static bool acceptable_java_type (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static bool decl_defined_p (tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec<tree, va_gc> *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec<tree, va_gc> *deferred_fns;
97 /* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
103 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
104 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
105 that apply to the function). */
107 tree
108 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
109 cp_ref_qualifier rqual)
111 tree raises;
112 tree attrs;
113 int type_quals;
115 if (fntype == error_mark_node || ctype == error_mark_node)
116 return error_mark_node;
118 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
119 || TREE_CODE (fntype) == METHOD_TYPE);
121 type_quals = quals & ~TYPE_QUAL_RESTRICT;
122 ctype = cp_build_qualified_type (ctype, type_quals);
123 raises = TYPE_RAISES_EXCEPTIONS (fntype);
124 attrs = TYPE_ATTRIBUTES (fntype);
125 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
126 (TREE_CODE (fntype) == METHOD_TYPE
127 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
128 : TYPE_ARG_TYPES (fntype)));
129 if (attrs)
130 fntype = cp_build_type_attribute_variant (fntype, attrs);
131 if (rqual)
132 fntype = build_ref_qualified_type (fntype, rqual);
133 if (raises)
134 fntype = build_exception_variant (fntype, raises);
136 return fntype;
139 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
140 return type changed to NEW_RET. */
142 tree
143 change_return_type (tree new_ret, tree fntype)
145 tree newtype;
146 tree args = TYPE_ARG_TYPES (fntype);
147 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
148 tree attrs = TYPE_ATTRIBUTES (fntype);
150 if (new_ret == error_mark_node)
151 return fntype;
153 if (same_type_p (new_ret, TREE_TYPE (fntype)))
154 return fntype;
156 if (TREE_CODE (fntype) == FUNCTION_TYPE)
158 newtype = build_function_type (new_ret, args);
159 newtype = apply_memfn_quals (newtype,
160 type_memfn_quals (fntype),
161 type_memfn_rqual (fntype));
163 else
164 newtype = build_method_type_directly
165 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
166 if (raises)
167 newtype = build_exception_variant (newtype, raises);
168 if (attrs)
169 newtype = cp_build_type_attribute_variant (newtype, attrs);
171 return newtype;
174 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
175 appropriately. */
177 tree
178 cp_build_parm_decl (tree name, tree type)
180 tree parm = build_decl (input_location,
181 PARM_DECL, name, type);
182 /* DECL_ARG_TYPE is only used by the back end and the back end never
183 sees templates. */
184 if (!processing_template_decl)
185 DECL_ARG_TYPE (parm) = type_passed_as (type);
187 /* If the type is a pack expansion, then we have a function
188 parameter pack. */
189 if (type && TREE_CODE (type) == TYPE_PACK_EXPANSION)
190 FUNCTION_PARAMETER_PACK_P (parm) = 1;
192 return parm;
195 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
196 indicated NAME. */
198 tree
199 build_artificial_parm (tree name, tree type)
201 tree parm = cp_build_parm_decl (name, type);
202 DECL_ARTIFICIAL (parm) = 1;
203 /* All our artificial parms are implicitly `const'; they cannot be
204 assigned to. */
205 TREE_READONLY (parm) = 1;
206 return parm;
209 /* Constructors for types with virtual baseclasses need an "in-charge" flag
210 saying whether this constructor is responsible for initialization of
211 virtual baseclasses or not. All destructors also need this "in-charge"
212 flag, which additionally determines whether or not the destructor should
213 free the memory for the object.
215 This function adds the "in-charge" flag to member function FN if
216 appropriate. It is called from grokclassfn and tsubst.
217 FN must be either a constructor or destructor.
219 The in-charge flag follows the 'this' parameter, and is followed by the
220 VTT parm (if any), then the user-written parms. */
222 void
223 maybe_retrofit_in_chrg (tree fn)
225 tree basetype, arg_types, parms, parm, fntype;
227 /* If we've already add the in-charge parameter don't do it again. */
228 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
229 return;
231 /* When processing templates we can't know, in general, whether or
232 not we're going to have virtual baseclasses. */
233 if (processing_template_decl)
234 return;
236 /* We don't need an in-charge parameter for constructors that don't
237 have virtual bases. */
238 if (DECL_CONSTRUCTOR_P (fn)
239 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
240 return;
242 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
243 basetype = TREE_TYPE (TREE_VALUE (arg_types));
244 arg_types = TREE_CHAIN (arg_types);
246 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
248 /* If this is a subobject constructor or destructor, our caller will
249 pass us a pointer to our VTT. */
250 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
252 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
254 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
255 DECL_CHAIN (parm) = parms;
256 parms = parm;
258 /* ...and then to TYPE_ARG_TYPES. */
259 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
261 DECL_HAS_VTT_PARM_P (fn) = 1;
264 /* Then add the in-charge parm (before the VTT parm). */
265 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
266 DECL_CHAIN (parm) = parms;
267 parms = parm;
268 arg_types = hash_tree_chain (integer_type_node, arg_types);
270 /* Insert our new parameter(s) into the list. */
271 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
273 /* And rebuild the function type. */
274 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
275 arg_types);
276 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
277 fntype = build_exception_variant (fntype,
278 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
279 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
280 fntype = (cp_build_type_attribute_variant
281 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
282 TREE_TYPE (fn) = fntype;
284 /* Now we've got the in-charge parameter. */
285 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
288 /* Classes overload their constituent function names automatically.
289 When a function name is declared in a record structure,
290 its name is changed to it overloaded name. Since names for
291 constructors and destructors can conflict, we place a leading
292 '$' for destructors.
294 CNAME is the name of the class we are grokking for.
296 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
298 FLAGS contains bits saying what's special about today's
299 arguments. DTOR_FLAG == DESTRUCTOR.
301 If FUNCTION is a destructor, then we must add the `auto-delete' field
302 as a second parameter. There is some hair associated with the fact
303 that we must "declare" this variable in the manner consistent with the
304 way the rest of the arguments were declared.
306 QUALS are the qualifiers for the this pointer. */
308 void
309 grokclassfn (tree ctype, tree function, enum overload_flags flags)
311 tree fn_name = DECL_NAME (function);
313 /* Even within an `extern "C"' block, members get C++ linkage. See
314 [dcl.link] for details. */
315 SET_DECL_LANGUAGE (function, lang_cplusplus);
317 if (fn_name == NULL_TREE)
319 error ("name missing for member function");
320 fn_name = get_identifier ("<anonymous>");
321 DECL_NAME (function) = fn_name;
324 DECL_CONTEXT (function) = ctype;
326 if (flags == DTOR_FLAG)
327 DECL_DESTRUCTOR_P (function) = 1;
329 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
330 maybe_retrofit_in_chrg (function);
333 /* Create an ARRAY_REF, checking for the user doing things backwards
334 along the way. DECLTYPE_P is for N3276, as in the parser. */
336 tree
337 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
338 bool decltype_p)
340 tree type;
341 tree expr;
342 tree orig_array_expr = array_expr;
343 tree orig_index_exp = index_exp;
345 if (error_operand_p (array_expr) || error_operand_p (index_exp))
346 return error_mark_node;
348 if (processing_template_decl)
350 if (type_dependent_expression_p (array_expr)
351 || type_dependent_expression_p (index_exp))
352 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
353 NULL_TREE, NULL_TREE);
354 array_expr = build_non_dependent_expr (array_expr);
355 index_exp = build_non_dependent_expr (index_exp);
358 type = TREE_TYPE (array_expr);
359 gcc_assert (type);
360 type = non_reference (type);
362 /* If they have an `operator[]', use that. */
363 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
365 tsubst_flags_t complain = tf_warning_or_error;
366 if (decltype_p)
367 complain |= tf_decltype;
368 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
369 index_exp, NULL_TREE, /*overload=*/NULL, complain);
371 else
373 tree p1, p2, i1, i2;
375 /* Otherwise, create an ARRAY_REF for a pointer or array type.
376 It is a little-known fact that, if `a' is an array and `i' is
377 an int, you can write `i[a]', which means the same thing as
378 `a[i]'. */
379 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
380 p1 = array_expr;
381 else
382 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
384 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
385 p2 = index_exp;
386 else
387 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
389 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
390 false);
391 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
392 false);
394 if ((p1 && i2) && (i1 && p2))
395 error ("ambiguous conversion for array subscript");
397 if (p1 && i2)
398 array_expr = p1, index_exp = i2;
399 else if (i1 && p2)
400 array_expr = p2, index_exp = i1;
401 else
403 error ("invalid types %<%T[%T]%> for array subscript",
404 type, TREE_TYPE (index_exp));
405 return error_mark_node;
408 if (array_expr == error_mark_node || index_exp == error_mark_node)
409 error ("ambiguous conversion for array subscript");
411 expr = build_array_ref (input_location, array_expr, index_exp);
413 if (processing_template_decl && expr != error_mark_node)
414 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
415 NULL_TREE, NULL_TREE);
416 return expr;
419 /* Given the cast expression EXP, checking out its validity. Either return
420 an error_mark_node if there was an unavoidable error, return a cast to
421 void for trying to delete a pointer w/ the value 0, or return the
422 call to delete. If DOING_VEC is true, we handle things differently
423 for doing an array delete.
424 Implements ARM $5.3.4. This is called from the parser. */
426 tree
427 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
428 tsubst_flags_t complain)
430 tree t, type;
432 if (exp == error_mark_node)
433 return exp;
435 if (processing_template_decl)
437 t = build_min (DELETE_EXPR, void_type_node, exp, size);
438 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
439 DELETE_EXPR_USE_VEC (t) = doing_vec;
440 TREE_SIDE_EFFECTS (t) = 1;
441 return t;
444 /* An array can't have been allocated by new, so complain. */
445 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
446 warning (0, "deleting array %q#E", exp);
448 t = build_expr_type_conversion (WANT_POINTER, exp, true);
450 if (t == NULL_TREE || t == error_mark_node)
452 error ("type %q#T argument given to %<delete%>, expected pointer",
453 TREE_TYPE (exp));
454 return error_mark_node;
457 type = TREE_TYPE (t);
459 /* As of Valley Forge, you can delete a pointer to const. */
461 /* You can't delete functions. */
462 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
464 error ("cannot delete a function. Only pointer-to-objects are "
465 "valid arguments to %<delete%>");
466 return error_mark_node;
469 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
470 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
472 warning (0, "deleting %qT is undefined", type);
473 doing_vec = 0;
476 /* Deleting a pointer with the value zero is valid and has no effect. */
477 if (integer_zerop (t))
478 return build1 (NOP_EXPR, void_type_node, t);
480 if (doing_vec)
481 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
482 sfk_deleting_destructor,
483 use_global_delete, complain);
484 else
485 return build_delete (type, t, sfk_deleting_destructor,
486 LOOKUP_NORMAL, use_global_delete,
487 complain);
490 /* Report an error if the indicated template declaration is not the
491 sort of thing that should be a member template. */
493 void
494 check_member_template (tree tmpl)
496 tree decl;
498 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
499 decl = DECL_TEMPLATE_RESULT (tmpl);
501 if (TREE_CODE (decl) == FUNCTION_DECL
502 || DECL_ALIAS_TEMPLATE_P (tmpl)
503 || (TREE_CODE (decl) == TYPE_DECL
504 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
506 /* The parser rejects template declarations in local classes. */
507 gcc_assert (!current_function_decl);
508 /* The parser rejects any use of virtual in a function template. */
509 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
510 && DECL_VIRTUAL_P (decl)));
512 /* The debug-information generating code doesn't know what to do
513 with member templates. */
514 DECL_IGNORED_P (tmpl) = 1;
516 else
517 error ("template declaration of %q#D", decl);
520 /* Return true iff TYPE is a valid Java parameter or return type. */
522 static bool
523 acceptable_java_type (tree type)
525 if (type == error_mark_node)
526 return false;
528 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
529 return true;
530 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
532 type = TREE_TYPE (type);
533 if (TREE_CODE (type) == RECORD_TYPE)
535 tree args; int i;
536 if (! TYPE_FOR_JAVA (type))
537 return false;
538 if (! CLASSTYPE_TEMPLATE_INFO (type))
539 return true;
540 args = CLASSTYPE_TI_ARGS (type);
541 i = TREE_VEC_LENGTH (args);
542 while (--i >= 0)
544 type = TREE_VEC_ELT (args, i);
545 if (TREE_CODE (type) == POINTER_TYPE)
546 type = TREE_TYPE (type);
547 if (! TYPE_FOR_JAVA (type))
548 return false;
550 return true;
553 return false;
556 /* For a METHOD in a Java class CTYPE, return true if
557 the parameter and return types are valid Java types.
558 Otherwise, print appropriate error messages, and return false. */
560 bool
561 check_java_method (tree method)
563 bool jerr = false;
564 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
565 tree ret_type = TREE_TYPE (TREE_TYPE (method));
567 if (!acceptable_java_type (ret_type))
569 error ("Java method %qD has non-Java return type %qT",
570 method, ret_type);
571 jerr = true;
574 arg_types = TREE_CHAIN (arg_types);
575 if (DECL_HAS_IN_CHARGE_PARM_P (method))
576 arg_types = TREE_CHAIN (arg_types);
577 if (DECL_HAS_VTT_PARM_P (method))
578 arg_types = TREE_CHAIN (arg_types);
580 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
582 tree type = TREE_VALUE (arg_types);
583 if (!acceptable_java_type (type))
585 if (type != error_mark_node)
586 error ("Java method %qD has non-Java parameter type %qT",
587 method, type);
588 jerr = true;
591 return !jerr;
594 /* Sanity check: report error if this function FUNCTION is not
595 really a member of the class (CTYPE) it is supposed to belong to.
596 TEMPLATE_PARMS is used to specify the template parameters of a member
597 template passed as FUNCTION_DECL. If the member template is passed as a
598 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
599 from the declaration. If the function is not a function template, it
600 must be NULL.
601 It returns the original declaration for the function, NULL_TREE if
602 no declaration was found, error_mark_node if an error was emitted. */
604 tree
605 check_classfn (tree ctype, tree function, tree template_parms)
607 int ix;
608 bool is_template;
609 tree pushed_scope;
611 if (DECL_USE_TEMPLATE (function)
612 && !(TREE_CODE (function) == TEMPLATE_DECL
613 && DECL_TEMPLATE_SPECIALIZATION (function))
614 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
615 /* Since this is a specialization of a member template,
616 we're not going to find the declaration in the class.
617 For example, in:
619 struct S { template <typename T> void f(T); };
620 template <> void S::f(int);
622 we're not going to find `S::f(int)', but there's no
623 reason we should, either. We let our callers know we didn't
624 find the method, but we don't complain. */
625 return NULL_TREE;
627 /* Basic sanity check: for a template function, the template parameters
628 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
629 if (TREE_CODE (function) == TEMPLATE_DECL)
631 if (template_parms
632 && !comp_template_parms (template_parms,
633 DECL_TEMPLATE_PARMS (function)))
635 error ("template parameter lists provided don%'t match the "
636 "template parameters of %qD", function);
637 return error_mark_node;
639 template_parms = DECL_TEMPLATE_PARMS (function);
642 /* OK, is this a definition of a member template? */
643 is_template = (template_parms != NULL_TREE);
645 /* We must enter the scope here, because conversion operators are
646 named by target type, and type equivalence relies on typenames
647 resolving within the scope of CTYPE. */
648 pushed_scope = push_scope (ctype);
649 ix = class_method_index_for_fn (complete_type (ctype), function);
650 if (ix >= 0)
652 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
653 tree fndecls, fndecl = 0;
654 bool is_conv_op;
655 const char *format = NULL;
657 for (fndecls = (*methods)[ix];
658 fndecls; fndecls = OVL_NEXT (fndecls))
660 tree p1, p2;
662 fndecl = OVL_CURRENT (fndecls);
663 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
664 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
666 /* We cannot simply call decls_match because this doesn't
667 work for static member functions that are pretending to
668 be methods, and because the name may have been changed by
669 asm("new_name"). */
671 /* Get rid of the this parameter on functions that become
672 static. */
673 if (DECL_STATIC_FUNCTION_P (fndecl)
674 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
675 p1 = TREE_CHAIN (p1);
677 /* A member template definition only matches a member template
678 declaration. */
679 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
680 continue;
682 /* ref-qualifier or absence of same must match. */
683 if (type_memfn_rqual (TREE_TYPE (function))
684 != type_memfn_rqual (TREE_TYPE (fndecl)))
685 continue;
687 /* While finding a match, same types and params are not enough
688 if the function is versioned. Also check version ("target")
689 attributes. */
690 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
691 TREE_TYPE (TREE_TYPE (fndecl)))
692 && compparms (p1, p2)
693 && !targetm.target_option.function_versions (function, fndecl)
694 && (!is_template
695 || comp_template_parms (template_parms,
696 DECL_TEMPLATE_PARMS (fndecl)))
697 && (DECL_TEMPLATE_SPECIALIZATION (function)
698 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
699 && (!DECL_TEMPLATE_SPECIALIZATION (function)
700 || (DECL_TI_TEMPLATE (function)
701 == DECL_TI_TEMPLATE (fndecl))))
702 break;
704 if (fndecls)
706 if (pushed_scope)
707 pop_scope (pushed_scope);
708 return OVL_CURRENT (fndecls);
711 error_at (DECL_SOURCE_LOCATION (function),
712 "prototype for %q#D does not match any in class %qT",
713 function, ctype);
714 is_conv_op = DECL_CONV_FN_P (fndecl);
716 if (is_conv_op)
717 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
718 fndecls = (*methods)[ix];
719 while (fndecls)
721 fndecl = OVL_CURRENT (fndecls);
722 fndecls = OVL_NEXT (fndecls);
724 if (!fndecls && is_conv_op)
726 if (methods->length () > (size_t) ++ix)
728 fndecls = (*methods)[ix];
729 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
731 fndecls = NULL_TREE;
732 is_conv_op = false;
735 else
736 is_conv_op = false;
738 if (format)
739 format = " %+#D";
740 else if (fndecls)
741 format = N_("candidates are: %+#D");
742 else
743 format = N_("candidate is: %+#D");
744 error (format, fndecl);
747 else if (!COMPLETE_TYPE_P (ctype))
748 cxx_incomplete_type_error (function, ctype);
749 else
750 error ("no %q#D member function declared in class %qT",
751 function, ctype);
753 if (pushed_scope)
754 pop_scope (pushed_scope);
755 return error_mark_node;
758 /* DECL is a function with vague linkage. Remember it so that at the
759 end of the translation unit we can decide whether or not to emit
760 it. */
762 void
763 note_vague_linkage_fn (tree decl)
765 DECL_DEFER_OUTPUT (decl) = 1;
766 vec_safe_push (deferred_fns, decl);
769 /* We have just processed the DECL, which is a static data member.
770 The other parameters are as for cp_finish_decl. */
772 void
773 finish_static_data_member_decl (tree decl,
774 tree init, bool init_const_expr_p,
775 tree asmspec_tree,
776 int flags)
778 DECL_CONTEXT (decl) = current_class_type;
780 /* We cannot call pushdecl here, because that would fill in the
781 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
782 the right thing, namely, to put this decl out straight away. */
784 if (! processing_template_decl)
785 vec_safe_push (pending_statics, decl);
787 if (LOCAL_CLASS_P (current_class_type)
788 /* We already complained about the template definition. */
789 && !DECL_TEMPLATE_INSTANTIATION (decl))
790 permerror (input_location, "local class %q#T shall not have static data member %q#D",
791 current_class_type, decl);
793 DECL_IN_AGGR_P (decl) = 1;
795 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
796 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
797 SET_VAR_HAD_UNKNOWN_BOUND (decl);
799 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
802 /* DECLARATOR and DECLSPECS correspond to a class member. The other
803 parameters are as for cp_finish_decl. Return the DECL for the
804 class member declared. */
806 tree
807 grokfield (const cp_declarator *declarator,
808 cp_decl_specifier_seq *declspecs,
809 tree init, bool init_const_expr_p,
810 tree asmspec_tree,
811 tree attrlist)
813 tree value;
814 const char *asmspec = 0;
815 int flags;
816 tree name;
818 if (init
819 && TREE_CODE (init) == TREE_LIST
820 && TREE_VALUE (init) == error_mark_node
821 && TREE_CHAIN (init) == NULL_TREE)
822 init = NULL_TREE;
824 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
825 if (! value || error_operand_p (value))
826 /* friend or constructor went bad. */
827 return error_mark_node;
829 if (TREE_CODE (value) == TYPE_DECL && init)
831 error ("typedef %qD is initialized (use decltype instead)", value);
832 init = NULL_TREE;
835 /* Pass friendly classes back. */
836 if (value == void_type_node)
837 return value;
839 /* Pass friend decls back. */
840 if ((TREE_CODE (value) == FUNCTION_DECL
841 || TREE_CODE (value) == TEMPLATE_DECL)
842 && DECL_CONTEXT (value) != current_class_type)
843 return value;
845 name = DECL_NAME (value);
847 if (name != NULL_TREE)
849 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
851 error ("explicit template argument list not allowed");
852 return error_mark_node;
855 if (IDENTIFIER_POINTER (name)[0] == '_'
856 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
857 error ("member %qD conflicts with virtual function table field name",
858 value);
861 /* Stash away type declarations. */
862 if (TREE_CODE (value) == TYPE_DECL)
864 DECL_NONLOCAL (value) = 1;
865 DECL_CONTEXT (value) = current_class_type;
867 if (attrlist)
869 int attrflags = 0;
871 /* If this is a typedef that names the class for linkage purposes
872 (7.1.3p8), apply any attributes directly to the type. */
873 if (TAGGED_TYPE_P (TREE_TYPE (value))
874 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
875 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
877 cplus_decl_attributes (&value, attrlist, attrflags);
880 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
881 && TREE_TYPE (value) != error_mark_node
882 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
883 set_underlying_type (value);
885 /* It's important that push_template_decl below follows
886 set_underlying_type above so that the created template
887 carries the properly set type of VALUE. */
888 if (processing_template_decl)
889 value = push_template_decl (value);
891 record_locally_defined_typedef (value);
892 return value;
895 if (DECL_IN_AGGR_P (value))
897 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
898 return void_type_node;
901 if (asmspec_tree && asmspec_tree != error_mark_node)
902 asmspec = TREE_STRING_POINTER (asmspec_tree);
904 if (init)
906 if (TREE_CODE (value) == FUNCTION_DECL)
908 /* Initializers for functions are rejected early in the parser.
909 If we get here, it must be a pure specifier for a method. */
910 if (init == ridpointers[(int)RID_DELETE])
912 DECL_DELETED_FN (value) = 1;
913 DECL_DECLARED_INLINE_P (value) = 1;
914 DECL_INITIAL (value) = error_mark_node;
916 else if (init == ridpointers[(int)RID_DEFAULT])
918 if (defaultable_fn_check (value))
920 DECL_DEFAULTED_FN (value) = 1;
921 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
922 DECL_DECLARED_INLINE_P (value) = 1;
925 else if (TREE_CODE (init) == DEFAULT_ARG)
926 error ("invalid initializer for member function %qD", value);
927 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
929 if (integer_zerop (init))
930 DECL_PURE_VIRTUAL_P (value) = 1;
931 else if (error_operand_p (init))
932 ; /* An error has already been reported. */
933 else
934 error ("invalid initializer for member function %qD",
935 value);
937 else
939 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
940 error ("initializer specified for static member function %qD",
941 value);
944 else if (TREE_CODE (value) == FIELD_DECL)
945 /* C++11 NSDMI, keep going. */;
946 else if (TREE_CODE (value) != VAR_DECL)
947 gcc_unreachable ();
948 else if (!processing_template_decl)
950 if (TREE_CODE (init) == CONSTRUCTOR)
951 init = digest_init (TREE_TYPE (value), init, tf_warning_or_error);
952 init = maybe_constant_init (init);
954 if (init != error_mark_node && !TREE_CONSTANT (init))
956 /* We can allow references to things that are effectively
957 static, since references are initialized with the
958 address. */
959 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
960 || (TREE_STATIC (init) == 0
961 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
963 error ("field initializer is not constant");
964 init = error_mark_node;
970 if (processing_template_decl
971 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
973 value = push_template_decl (value);
974 if (error_operand_p (value))
975 return error_mark_node;
978 if (attrlist)
979 cplus_decl_attributes (&value, attrlist, 0);
981 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
982 && CONSTRUCTOR_IS_DIRECT_INIT (init))
983 flags = LOOKUP_NORMAL;
984 else
985 flags = LOOKUP_IMPLICIT;
987 switch (TREE_CODE (value))
989 case VAR_DECL:
990 finish_static_data_member_decl (value, init, init_const_expr_p,
991 asmspec_tree, flags);
992 return value;
994 case FIELD_DECL:
995 if (asmspec)
996 error ("%<asm%> specifiers are not permitted on non-static data members");
997 if (DECL_INITIAL (value) == error_mark_node)
998 init = error_mark_node;
999 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1000 NULL_TREE, flags);
1001 DECL_IN_AGGR_P (value) = 1;
1002 return value;
1004 case FUNCTION_DECL:
1005 if (asmspec)
1006 set_user_assembler_name (value, asmspec);
1008 cp_finish_decl (value,
1009 /*init=*/NULL_TREE,
1010 /*init_const_expr_p=*/false,
1011 asmspec_tree, flags);
1013 /* Pass friends back this way. */
1014 if (DECL_FRIEND_P (value))
1015 return void_type_node;
1017 DECL_IN_AGGR_P (value) = 1;
1018 return value;
1020 default:
1021 gcc_unreachable ();
1023 return NULL_TREE;
1026 /* Like `grokfield', but for bitfields.
1027 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1029 tree
1030 grokbitfield (const cp_declarator *declarator,
1031 cp_decl_specifier_seq *declspecs, tree width,
1032 tree attrlist)
1034 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1036 if (value == error_mark_node)
1037 return NULL_TREE; /* friends went bad. */
1039 /* Pass friendly classes back. */
1040 if (TREE_CODE (value) == VOID_TYPE)
1041 return void_type_node;
1043 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1044 && (POINTER_TYPE_P (value)
1045 || !dependent_type_p (TREE_TYPE (value))))
1047 error ("bit-field %qD with non-integral type", value);
1048 return error_mark_node;
1051 if (TREE_CODE (value) == TYPE_DECL)
1053 error ("cannot declare %qD to be a bit-field type", value);
1054 return NULL_TREE;
1057 /* Usually, finish_struct_1 catches bitfields with invalid types.
1058 But, in the case of bitfields with function type, we confuse
1059 ourselves into thinking they are member functions, so we must
1060 check here. */
1061 if (TREE_CODE (value) == FUNCTION_DECL)
1063 error ("cannot declare bit-field %qD with function type",
1064 DECL_NAME (value));
1065 return NULL_TREE;
1068 if (DECL_IN_AGGR_P (value))
1070 error ("%qD is already defined in the class %qT", value,
1071 DECL_CONTEXT (value));
1072 return void_type_node;
1075 if (TREE_STATIC (value))
1077 error ("static member %qD cannot be a bit-field", value);
1078 return NULL_TREE;
1080 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1082 if (width != error_mark_node)
1084 /* The width must be an integer type. */
1085 if (!type_dependent_expression_p (width)
1086 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1087 error ("width of bit-field %qD has non-integral type %qT", value,
1088 TREE_TYPE (width));
1089 DECL_INITIAL (value) = width;
1090 SET_DECL_C_BIT_FIELD (value);
1093 DECL_IN_AGGR_P (value) = 1;
1095 if (attrlist)
1096 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1098 return value;
1102 /* Returns true iff ATTR is an attribute which needs to be applied at
1103 instantiation time rather than template definition time. */
1105 static bool
1106 is_late_template_attribute (tree attr, tree decl)
1108 tree name = get_attribute_name (attr);
1109 tree args = TREE_VALUE (attr);
1110 const struct attribute_spec *spec = lookup_attribute_spec (name);
1111 tree arg;
1113 if (!spec)
1114 /* Unknown attribute. */
1115 return false;
1117 /* Attribute weak handling wants to write out assembly right away. */
1118 if (is_attribute_p ("weak", name))
1119 return true;
1121 /* Attribute unused is applied directly, as it appertains to
1122 decls. */
1123 if (is_attribute_p ("unused", name))
1124 return false;
1126 /* If any of the arguments are dependent expressions, we can't evaluate
1127 the attribute until instantiation time. */
1128 for (arg = args; arg; arg = TREE_CHAIN (arg))
1130 tree t = TREE_VALUE (arg);
1132 /* If the first attribute argument is an identifier, only consider
1133 second and following arguments. Attributes like mode, format,
1134 cleanup and several target specific attributes aren't late
1135 just because they have an IDENTIFIER_NODE as first argument. */
1136 if (arg == args && TREE_CODE (t) == IDENTIFIER_NODE)
1137 continue;
1139 if (value_dependent_expression_p (t)
1140 || type_dependent_expression_p (t))
1141 return true;
1144 if (TREE_CODE (decl) == TYPE_DECL
1145 || TYPE_P (decl)
1146 || spec->type_required)
1148 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1150 /* We can't apply any attributes to a completely unknown type until
1151 instantiation time. */
1152 enum tree_code code = TREE_CODE (type);
1153 if (code == TEMPLATE_TYPE_PARM
1154 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1155 || code == TYPENAME_TYPE)
1156 return true;
1157 /* Also defer most attributes on dependent types. This is not
1158 necessary in all cases, but is the better default. */
1159 else if (dependent_type_p (type)
1160 /* But attribute visibility specifically works on
1161 templates. */
1162 && !is_attribute_p ("visibility", name))
1163 return true;
1164 else
1165 return false;
1167 else
1168 return false;
1171 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1172 applied at instantiation time and return them. If IS_DEPENDENT is true,
1173 the declaration itself is dependent, so all attributes should be applied
1174 at instantiation time. */
1176 static tree
1177 splice_template_attributes (tree *attr_p, tree decl)
1179 tree *p = attr_p;
1180 tree late_attrs = NULL_TREE;
1181 tree *q = &late_attrs;
1183 if (!p)
1184 return NULL_TREE;
1186 for (; *p; )
1188 if (is_late_template_attribute (*p, decl))
1190 ATTR_IS_DEPENDENT (*p) = 1;
1191 *q = *p;
1192 *p = TREE_CHAIN (*p);
1193 q = &TREE_CHAIN (*q);
1194 *q = NULL_TREE;
1196 else
1197 p = &TREE_CHAIN (*p);
1200 return late_attrs;
1203 /* Remove any late attributes from the list in ATTR_P and attach them to
1204 DECL_P. */
1206 static void
1207 save_template_attributes (tree *attr_p, tree *decl_p)
1209 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1210 tree *q;
1211 tree old_attrs = NULL_TREE;
1213 if (!late_attrs)
1214 return;
1216 if (DECL_P (*decl_p))
1217 q = &DECL_ATTRIBUTES (*decl_p);
1218 else
1219 q = &TYPE_ATTRIBUTES (*decl_p);
1221 old_attrs = *q;
1223 /* Merge the late attributes at the beginning with the attribute
1224 list. */
1225 late_attrs = merge_attributes (late_attrs, *q);
1226 *q = late_attrs;
1228 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1230 /* We've added new attributes directly to the main variant, so
1231 now we need to update all of the other variants to include
1232 these new attributes. */
1233 tree variant;
1234 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1235 variant = TYPE_NEXT_VARIANT (variant))
1237 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1238 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1243 /* Like reconstruct_complex_type, but handle also template trees. */
1245 tree
1246 cp_reconstruct_complex_type (tree type, tree bottom)
1248 tree inner, outer;
1250 if (TREE_CODE (type) == POINTER_TYPE)
1252 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1253 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1254 TYPE_REF_CAN_ALIAS_ALL (type));
1256 else if (TREE_CODE (type) == REFERENCE_TYPE)
1258 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1259 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1260 TYPE_REF_CAN_ALIAS_ALL (type));
1262 else if (TREE_CODE (type) == ARRAY_TYPE)
1264 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1265 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1266 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1267 element type qualification will be handled by the recursive
1268 cp_reconstruct_complex_type call and cp_build_qualified_type
1269 for ARRAY_TYPEs changes the element type. */
1270 return outer;
1272 else if (TREE_CODE (type) == FUNCTION_TYPE)
1274 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1275 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1276 outer = apply_memfn_quals (outer,
1277 type_memfn_quals (type),
1278 type_memfn_rqual (type));
1280 else if (TREE_CODE (type) == METHOD_TYPE)
1282 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1283 /* The build_method_type_directly() routine prepends 'this' to argument list,
1284 so we must compensate by getting rid of it. */
1285 outer
1286 = build_method_type_directly
1287 (class_of_this_parm (type), inner,
1288 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1290 else if (TREE_CODE (type) == OFFSET_TYPE)
1292 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1293 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1295 else
1296 return bottom;
1298 if (TYPE_ATTRIBUTES (type))
1299 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1300 return cp_build_qualified_type (outer, cp_type_quals (type));
1303 /* Replaces any constexpr expression that may be into the attributes
1304 arguments with their reduced value. */
1306 static void
1307 cp_check_const_attributes (tree attributes)
1309 tree attr;
1310 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1312 tree arg;
1313 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1315 tree expr = TREE_VALUE (arg);
1316 if (EXPR_P (expr))
1317 TREE_VALUE (arg) = maybe_constant_value (expr);
1322 /* Like decl_attributes, but handle C++ complexity. */
1324 void
1325 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1327 if (*decl == NULL_TREE || *decl == void_type_node
1328 || *decl == error_mark_node)
1329 return;
1331 if (processing_template_decl)
1333 if (check_for_bare_parameter_packs (attributes))
1334 return;
1336 save_template_attributes (&attributes, decl);
1339 cp_check_const_attributes (attributes);
1341 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1342 decl = &DECL_TEMPLATE_RESULT (*decl);
1344 decl_attributes (decl, attributes, flags);
1346 if (TREE_CODE (*decl) == TYPE_DECL)
1347 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1350 /* Walks through the namespace- or function-scope anonymous union
1351 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1352 Returns one of the fields for use in the mangled name. */
1354 static tree
1355 build_anon_union_vars (tree type, tree object)
1357 tree main_decl = NULL_TREE;
1358 tree field;
1360 /* Rather than write the code to handle the non-union case,
1361 just give an error. */
1362 if (TREE_CODE (type) != UNION_TYPE)
1364 error ("anonymous struct not inside named type");
1365 return error_mark_node;
1368 for (field = TYPE_FIELDS (type);
1369 field != NULL_TREE;
1370 field = DECL_CHAIN (field))
1372 tree decl;
1373 tree ref;
1375 if (DECL_ARTIFICIAL (field))
1376 continue;
1377 if (TREE_CODE (field) != FIELD_DECL)
1379 permerror (input_location, "%q+#D invalid; an anonymous union can only "
1380 "have non-static data members", field);
1381 continue;
1384 if (TREE_PRIVATE (field))
1385 permerror (input_location, "private member %q+#D in anonymous union", field);
1386 else if (TREE_PROTECTED (field))
1387 permerror (input_location, "protected member %q+#D in anonymous union", field);
1389 if (processing_template_decl)
1390 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1391 DECL_NAME (field), NULL_TREE);
1392 else
1393 ref = build_class_member_access_expr (object, field, NULL_TREE,
1394 false, tf_warning_or_error);
1396 if (DECL_NAME (field))
1398 tree base;
1400 decl = build_decl (input_location,
1401 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1402 DECL_ANON_UNION_VAR_P (decl) = 1;
1403 DECL_ARTIFICIAL (decl) = 1;
1405 base = get_base_address (object);
1406 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1407 TREE_STATIC (decl) = TREE_STATIC (base);
1408 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1410 SET_DECL_VALUE_EXPR (decl, ref);
1411 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1413 decl = pushdecl (decl);
1415 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1416 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1417 else
1418 decl = 0;
1420 if (main_decl == NULL_TREE)
1421 main_decl = decl;
1424 return main_decl;
1427 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1428 anonymous union, then all members must be laid out together. PUBLIC_P
1429 is nonzero if this union is not declared static. */
1431 void
1432 finish_anon_union (tree anon_union_decl)
1434 tree type;
1435 tree main_decl;
1436 bool public_p;
1438 if (anon_union_decl == error_mark_node)
1439 return;
1441 type = TREE_TYPE (anon_union_decl);
1442 public_p = TREE_PUBLIC (anon_union_decl);
1444 /* The VAR_DECL's context is the same as the TYPE's context. */
1445 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1447 if (TYPE_FIELDS (type) == NULL_TREE)
1448 return;
1450 if (public_p)
1452 error ("namespace-scope anonymous aggregates must be static");
1453 return;
1456 main_decl = build_anon_union_vars (type, anon_union_decl);
1457 if (main_decl == error_mark_node)
1458 return;
1459 if (main_decl == NULL_TREE)
1461 warning (0, "anonymous union with no members");
1462 return;
1465 if (!processing_template_decl)
1467 /* Use main_decl to set the mangled name. */
1468 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1469 maybe_commonize_var (anon_union_decl);
1470 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1471 mangle_decl (anon_union_decl);
1472 DECL_NAME (anon_union_decl) = NULL_TREE;
1475 pushdecl (anon_union_decl);
1476 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1479 /* Auxiliary functions to make type signatures for
1480 `operator new' and `operator delete' correspond to
1481 what compiler will be expecting. */
1483 tree
1484 coerce_new_type (tree type)
1486 int e = 0;
1487 tree args = TYPE_ARG_TYPES (type);
1489 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1491 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1493 e = 1;
1494 error ("%<operator new%> must return type %qT", ptr_type_node);
1497 if (args && args != void_list_node)
1499 if (TREE_PURPOSE (args))
1501 /* [basic.stc.dynamic.allocation]
1503 The first parameter shall not have an associated default
1504 argument. */
1505 error ("the first parameter of %<operator new%> cannot "
1506 "have a default argument");
1507 /* Throw away the default argument. */
1508 TREE_PURPOSE (args) = NULL_TREE;
1511 if (!same_type_p (TREE_VALUE (args), size_type_node))
1513 e = 2;
1514 args = TREE_CHAIN (args);
1517 else
1518 e = 2;
1520 if (e == 2)
1521 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1522 "as first parameter", size_type_node);
1524 switch (e)
1526 case 2:
1527 args = tree_cons (NULL_TREE, size_type_node, args);
1528 /* Fall through. */
1529 case 1:
1530 type = build_exception_variant
1531 (build_function_type (ptr_type_node, args),
1532 TYPE_RAISES_EXCEPTIONS (type));
1533 /* Fall through. */
1534 default:;
1536 return type;
1539 tree
1540 coerce_delete_type (tree type)
1542 int e = 0;
1543 tree args = TYPE_ARG_TYPES (type);
1545 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1547 if (!same_type_p (TREE_TYPE (type), void_type_node))
1549 e = 1;
1550 error ("%<operator delete%> must return type %qT", void_type_node);
1553 if (!args || args == void_list_node
1554 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1556 e = 2;
1557 if (args && args != void_list_node)
1558 args = TREE_CHAIN (args);
1559 error ("%<operator delete%> takes type %qT as first parameter",
1560 ptr_type_node);
1562 switch (e)
1564 case 2:
1565 args = tree_cons (NULL_TREE, ptr_type_node, args);
1566 /* Fall through. */
1567 case 1:
1568 type = build_exception_variant
1569 (build_function_type (void_type_node, args),
1570 TYPE_RAISES_EXCEPTIONS (type));
1571 /* Fall through. */
1572 default:;
1575 return type;
1578 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1579 and mark them as needed. */
1581 static void
1582 mark_vtable_entries (tree decl)
1584 tree fnaddr;
1585 unsigned HOST_WIDE_INT idx;
1587 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1588 idx, fnaddr)
1590 tree fn;
1592 STRIP_NOPS (fnaddr);
1594 if (TREE_CODE (fnaddr) != ADDR_EXPR
1595 && TREE_CODE (fnaddr) != FDESC_EXPR)
1596 /* This entry is an offset: a virtual base class offset, a
1597 virtual call offset, an RTTI offset, etc. */
1598 continue;
1600 fn = TREE_OPERAND (fnaddr, 0);
1601 TREE_ADDRESSABLE (fn) = 1;
1602 /* When we don't have vcall offsets, we output thunks whenever
1603 we output the vtables that contain them. With vcall offsets,
1604 we know all the thunks we'll need when we emit a virtual
1605 function, so we emit the thunks there instead. */
1606 if (DECL_THUNK_P (fn))
1607 use_thunk (fn, /*emit_p=*/0);
1608 mark_used (fn);
1612 /* Set DECL up to have the closest approximation of "initialized common"
1613 linkage available. */
1615 void
1616 comdat_linkage (tree decl)
1618 if (flag_weak)
1619 make_decl_one_only (decl, cxx_comdat_group (decl));
1620 else if (TREE_CODE (decl) == FUNCTION_DECL
1621 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1622 /* We can just emit function and compiler-generated variables
1623 statically; having multiple copies is (for the most part) only
1624 a waste of space.
1626 There are two correctness issues, however: the address of a
1627 template instantiation with external linkage should be the
1628 same, independent of what translation unit asks for the
1629 address, and this will not hold when we emit multiple copies of
1630 the function. However, there's little else we can do.
1632 Also, by default, the typeinfo implementation assumes that
1633 there will be only one copy of the string used as the name for
1634 each type. Therefore, if weak symbols are unavailable, the
1635 run-time library should perform a more conservative check; it
1636 should perform a string comparison, rather than an address
1637 comparison. */
1638 TREE_PUBLIC (decl) = 0;
1639 else
1641 /* Static data member template instantiations, however, cannot
1642 have multiple copies. */
1643 if (DECL_INITIAL (decl) == 0
1644 || DECL_INITIAL (decl) == error_mark_node)
1645 DECL_COMMON (decl) = 1;
1646 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1648 DECL_COMMON (decl) = 1;
1649 DECL_INITIAL (decl) = error_mark_node;
1651 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1653 /* We can't do anything useful; leave vars for explicit
1654 instantiation. */
1655 DECL_EXTERNAL (decl) = 1;
1656 DECL_NOT_REALLY_EXTERN (decl) = 0;
1660 DECL_COMDAT (decl) = 1;
1663 /* For win32 we also want to put explicit instantiations in
1664 linkonce sections, so that they will be merged with implicit
1665 instantiations; otherwise we get duplicate symbol errors.
1666 For Darwin we do not want explicit instantiations to be
1667 linkonce. */
1669 void
1670 maybe_make_one_only (tree decl)
1672 /* We used to say that this was not necessary on targets that support weak
1673 symbols, because the implicit instantiations will defer to the explicit
1674 one. However, that's not actually the case in SVR4; a strong definition
1675 after a weak one is an error. Also, not making explicit
1676 instantiations one_only means that we can end up with two copies of
1677 some template instantiations. */
1678 if (! flag_weak)
1679 return;
1681 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1682 we can get away with not emitting them if they aren't used. We need
1683 to for variables so that cp_finish_decl will update their linkage,
1684 because their DECL_INITIAL may not have been set properly yet. */
1686 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1687 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1688 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1690 make_decl_one_only (decl, cxx_comdat_group (decl));
1692 if (TREE_CODE (decl) == VAR_DECL)
1694 DECL_COMDAT (decl) = 1;
1695 /* Mark it needed so we don't forget to emit it. */
1696 mark_decl_referenced (decl);
1697 TREE_USED (decl) = 1;
1702 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1703 This predicate will give the right answer during parsing of the
1704 function, which other tests may not. */
1706 bool
1707 vague_linkage_p (tree decl)
1709 /* Unfortunately, import_export_decl has not always been called
1710 before the function is processed, so we cannot simply check
1711 DECL_COMDAT. */
1712 return (DECL_COMDAT (decl)
1713 || (((TREE_CODE (decl) == FUNCTION_DECL
1714 && DECL_DECLARED_INLINE_P (decl))
1715 || (DECL_LANG_SPECIFIC (decl)
1716 && DECL_TEMPLATE_INSTANTIATION (decl)))
1717 && TREE_PUBLIC (decl)));
1720 /* Determine whether or not we want to specifically import or export CTYPE,
1721 using various heuristics. */
1723 static void
1724 import_export_class (tree ctype)
1726 /* -1 for imported, 1 for exported. */
1727 int import_export = 0;
1729 /* It only makes sense to call this function at EOF. The reason is
1730 that this function looks at whether or not the first non-inline
1731 non-abstract virtual member function has been defined in this
1732 translation unit. But, we can't possibly know that until we've
1733 seen the entire translation unit. */
1734 gcc_assert (at_eof);
1736 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1737 return;
1739 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1740 we will have CLASSTYPE_INTERFACE_ONLY set but not
1741 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1742 heuristic because someone will supply a #pragma implementation
1743 elsewhere, and deducing it here would produce a conflict. */
1744 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1745 return;
1747 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1748 import_export = -1;
1749 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1750 import_export = 1;
1751 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1752 && !flag_implicit_templates)
1753 /* For a template class, without -fimplicit-templates, check the
1754 repository. If the virtual table is assigned to this
1755 translation unit, then export the class; otherwise, import
1756 it. */
1757 import_export = repo_export_class_p (ctype) ? 1 : -1;
1758 else if (TYPE_POLYMORPHIC_P (ctype))
1760 /* The ABI specifies that the virtual table and associated
1761 information are emitted with the key method, if any. */
1762 tree method = CLASSTYPE_KEY_METHOD (ctype);
1763 /* If weak symbol support is not available, then we must be
1764 careful not to emit the vtable when the key function is
1765 inline. An inline function can be defined in multiple
1766 translation units. If we were to emit the vtable in each
1767 translation unit containing a definition, we would get
1768 multiple definition errors at link-time. */
1769 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1770 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1773 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1774 a definition anywhere else. */
1775 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1776 import_export = 0;
1778 /* Allow back ends the chance to overrule the decision. */
1779 if (targetm.cxx.import_export_class)
1780 import_export = targetm.cxx.import_export_class (ctype, import_export);
1782 if (import_export)
1784 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1785 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1789 /* Return true if VAR has already been provided to the back end; in that
1790 case VAR should not be modified further by the front end. */
1791 static bool
1792 var_finalized_p (tree var)
1794 return varpool_node_for_decl (var)->finalized;
1797 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1798 must be emitted in this translation unit. Mark it as such. */
1800 void
1801 mark_needed (tree decl)
1803 TREE_USED (decl) = 1;
1804 mark_decl_referenced (decl);
1807 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1808 returns true if a definition of this entity should be provided in
1809 this object file. Callers use this function to determine whether
1810 or not to let the back end know that a definition of DECL is
1811 available in this translation unit. */
1813 bool
1814 decl_needed_p (tree decl)
1816 gcc_assert (TREE_CODE (decl) == VAR_DECL
1817 || TREE_CODE (decl) == FUNCTION_DECL);
1818 /* This function should only be called at the end of the translation
1819 unit. We cannot be sure of whether or not something will be
1820 COMDAT until that point. */
1821 gcc_assert (at_eof);
1823 /* All entities with external linkage that are not COMDAT should be
1824 emitted; they may be referred to from other object files. */
1825 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1826 return true;
1827 /* If this entity was used, let the back end see it; it will decide
1828 whether or not to emit it into the object file. */
1829 if (TREE_USED (decl))
1830 return true;
1831 /* Functions marked "dllexport" must be emitted so that they are
1832 visible to other DLLs. */
1833 if (flag_keep_inline_dllexport
1834 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1835 return true;
1836 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1837 reference to DECL might cause it to be emitted later. */
1838 return false;
1841 /* If necessary, write out the vtables for the dynamic class CTYPE.
1842 Returns true if any vtables were emitted. */
1844 static bool
1845 maybe_emit_vtables (tree ctype)
1847 tree vtbl;
1848 tree primary_vtbl;
1849 int needed = 0;
1850 struct varpool_node *current = NULL, *last = NULL;
1852 /* If the vtables for this class have already been emitted there is
1853 nothing more to do. */
1854 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1855 if (var_finalized_p (primary_vtbl))
1856 return false;
1857 /* Ignore dummy vtables made by get_vtable_decl. */
1858 if (TREE_TYPE (primary_vtbl) == void_type_node)
1859 return false;
1861 /* On some targets, we cannot determine the key method until the end
1862 of the translation unit -- which is when this function is
1863 called. */
1864 if (!targetm.cxx.key_method_may_be_inline ())
1865 determine_key_method (ctype);
1867 /* See if any of the vtables are needed. */
1868 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1870 import_export_decl (vtbl);
1871 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1872 needed = 1;
1874 if (!needed)
1876 /* If the references to this class' vtables are optimized away,
1877 still emit the appropriate debugging information. See
1878 dfs_debug_mark. */
1879 if (DECL_COMDAT (primary_vtbl)
1880 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1881 note_debug_info_needed (ctype);
1882 return false;
1885 /* The ABI requires that we emit all of the vtables if we emit any
1886 of them. */
1887 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1889 /* Mark entities references from the virtual table as used. */
1890 mark_vtable_entries (vtbl);
1892 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1894 vec<tree, va_gc> *cleanups = NULL;
1895 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
1896 LOOKUP_NORMAL);
1898 /* It had better be all done at compile-time. */
1899 gcc_assert (!expr && !cleanups);
1902 /* Write it out. */
1903 DECL_EXTERNAL (vtbl) = 0;
1904 rest_of_decl_compilation (vtbl, 1, 1);
1906 /* Because we're only doing syntax-checking, we'll never end up
1907 actually marking the variable as written. */
1908 if (flag_syntax_only)
1909 TREE_ASM_WRITTEN (vtbl) = 1;
1910 else if (DECL_ONE_ONLY (vtbl))
1912 current = varpool_node_for_decl (vtbl);
1913 if (last)
1914 symtab_add_to_same_comdat_group ((symtab_node) current, (symtab_node) last);
1915 last = current;
1919 /* Since we're writing out the vtable here, also write the debug
1920 info. */
1921 note_debug_info_needed (ctype);
1923 return true;
1926 /* A special return value from type_visibility meaning internal
1927 linkage. */
1929 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
1931 /* walk_tree helper function for type_visibility. */
1933 static tree
1934 min_vis_r (tree *tp, int *walk_subtrees, void *data)
1936 int *vis_p = (int *)data;
1937 if (! TYPE_P (*tp))
1939 *walk_subtrees = 0;
1941 else if (TAGGED_TYPE_P (*tp)
1942 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
1944 *vis_p = VISIBILITY_ANON;
1945 return *tp;
1947 else if (CLASS_TYPE_P (*tp)
1948 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
1949 *vis_p = CLASSTYPE_VISIBILITY (*tp);
1950 return NULL;
1953 /* Returns the visibility of TYPE, which is the minimum visibility of its
1954 component types. */
1956 static int
1957 type_visibility (tree type)
1959 int vis = VISIBILITY_DEFAULT;
1960 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
1961 return vis;
1964 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
1965 specified (or if VISIBILITY is static). If TMPL is true, this
1966 constraint is for a template argument, and takes precedence
1967 over explicitly-specified visibility on the template. */
1969 static void
1970 constrain_visibility (tree decl, int visibility, bool tmpl)
1972 if (visibility == VISIBILITY_ANON)
1974 /* extern "C" declarations aren't affected by the anonymous
1975 namespace. */
1976 if (!DECL_EXTERN_C_P (decl))
1978 TREE_PUBLIC (decl) = 0;
1979 DECL_WEAK (decl) = 0;
1980 DECL_COMMON (decl) = 0;
1981 DECL_COMDAT_GROUP (decl) = NULL_TREE;
1982 DECL_INTERFACE_KNOWN (decl) = 1;
1983 if (DECL_LANG_SPECIFIC (decl))
1984 DECL_NOT_REALLY_EXTERN (decl) = 1;
1987 else if (visibility > DECL_VISIBILITY (decl)
1988 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
1990 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
1991 /* This visibility was not specified. */
1992 DECL_VISIBILITY_SPECIFIED (decl) = false;
1996 /* Constrain the visibility of DECL based on the visibility of its template
1997 arguments. */
1999 static void
2000 constrain_visibility_for_template (tree decl, tree targs)
2002 /* If this is a template instantiation, check the innermost
2003 template args for visibility constraints. The outer template
2004 args are covered by the class check. */
2005 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2006 int i;
2007 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2009 int vis = 0;
2011 tree arg = TREE_VEC_ELT (args, i-1);
2012 if (TYPE_P (arg))
2013 vis = type_visibility (arg);
2014 else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2016 STRIP_NOPS (arg);
2017 if (TREE_CODE (arg) == ADDR_EXPR)
2018 arg = TREE_OPERAND (arg, 0);
2019 if (TREE_CODE (arg) == VAR_DECL
2020 || TREE_CODE (arg) == FUNCTION_DECL)
2022 if (! TREE_PUBLIC (arg))
2023 vis = VISIBILITY_ANON;
2024 else
2025 vis = DECL_VISIBILITY (arg);
2028 if (vis)
2029 constrain_visibility (decl, vis, true);
2033 /* Like c_determine_visibility, but with additional C++-specific
2034 behavior.
2036 Function-scope entities can rely on the function's visibility because
2037 it is set in start_preparsed_function.
2039 Class-scope entities cannot rely on the class's visibility until the end
2040 of the enclosing class definition.
2042 Note that because namespaces have multiple independent definitions,
2043 namespace visibility is handled elsewhere using the #pragma visibility
2044 machinery rather than by decorating the namespace declaration.
2046 The goal is for constraints from the type to give a diagnostic, and
2047 other constraints to be applied silently. */
2049 void
2050 determine_visibility (tree decl)
2052 tree class_type = NULL_TREE;
2053 bool use_template;
2054 bool orig_visibility_specified;
2055 enum symbol_visibility orig_visibility;
2057 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2059 /* Only relevant for names with external linkage. */
2060 if (!TREE_PUBLIC (decl))
2061 return;
2063 /* Cloned constructors and destructors get the same visibility as
2064 the underlying function. That should be set up in
2065 maybe_clone_body. */
2066 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2068 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2069 orig_visibility = DECL_VISIBILITY (decl);
2071 if (TREE_CODE (decl) == TYPE_DECL)
2073 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2074 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2075 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2076 use_template = 1;
2077 else
2078 use_template = 0;
2080 else if (DECL_LANG_SPECIFIC (decl))
2081 use_template = DECL_USE_TEMPLATE (decl);
2082 else
2083 use_template = 0;
2085 /* If DECL is a member of a class, visibility specifiers on the
2086 class can influence the visibility of the DECL. */
2087 if (DECL_CLASS_SCOPE_P (decl))
2088 class_type = DECL_CONTEXT (decl);
2089 else
2091 /* Not a class member. */
2093 /* Virtual tables have DECL_CONTEXT set to their associated class,
2094 so they are automatically handled above. */
2095 gcc_assert (TREE_CODE (decl) != VAR_DECL
2096 || !DECL_VTABLE_OR_VTT_P (decl));
2098 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2100 /* Local statics and classes get the visibility of their
2101 containing function by default, except that
2102 -fvisibility-inlines-hidden doesn't affect them. */
2103 tree fn = DECL_CONTEXT (decl);
2104 if (DECL_VISIBILITY_SPECIFIED (fn))
2106 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2107 DECL_VISIBILITY_SPECIFIED (decl) =
2108 DECL_VISIBILITY_SPECIFIED (fn);
2110 else
2112 if (DECL_CLASS_SCOPE_P (fn))
2113 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2114 else if (determine_hidden_inline (fn))
2116 DECL_VISIBILITY (decl) = default_visibility;
2117 DECL_VISIBILITY_SPECIFIED (decl) =
2118 visibility_options.inpragma;
2120 else
2122 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2123 DECL_VISIBILITY_SPECIFIED (decl) =
2124 DECL_VISIBILITY_SPECIFIED (fn);
2128 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2129 but have no TEMPLATE_INFO, so don't try to check it. */
2130 use_template = 0;
2132 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl)
2133 && flag_visibility_ms_compat)
2135 /* Under -fvisibility-ms-compat, types are visible by default,
2136 even though their contents aren't. */
2137 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2138 int underlying_vis = type_visibility (underlying_type);
2139 if (underlying_vis == VISIBILITY_ANON
2140 || (CLASS_TYPE_P (underlying_type)
2141 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2142 constrain_visibility (decl, underlying_vis, false);
2143 else
2144 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2146 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2148 /* tinfo visibility is based on the type it's for. */
2149 constrain_visibility
2150 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2152 /* Give the target a chance to override the visibility associated
2153 with DECL. */
2154 if (TREE_PUBLIC (decl)
2155 && !DECL_REALLY_EXTERN (decl)
2156 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2157 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2158 targetm.cxx.determine_class_data_visibility (decl);
2160 else if (use_template)
2161 /* Template instantiations and specializations get visibility based
2162 on their template unless they override it with an attribute. */;
2163 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2165 if (determine_hidden_inline (decl))
2166 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2167 else
2169 /* Set default visibility to whatever the user supplied with
2170 #pragma GCC visibility or a namespace visibility attribute. */
2171 DECL_VISIBILITY (decl) = default_visibility;
2172 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2177 if (use_template)
2179 /* If the specialization doesn't specify visibility, use the
2180 visibility from the template. */
2181 tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2182 ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2183 : DECL_TEMPLATE_INFO (decl));
2184 tree args = TI_ARGS (tinfo);
2185 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2186 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2187 : DECL_ATTRIBUTES (decl));
2189 if (args != error_mark_node)
2191 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2193 if (!DECL_VISIBILITY_SPECIFIED (decl))
2195 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2196 && determine_hidden_inline (decl))
2197 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2198 else
2200 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2201 DECL_VISIBILITY_SPECIFIED (decl)
2202 = DECL_VISIBILITY_SPECIFIED (pattern);
2206 if (args
2207 /* Template argument visibility outweighs #pragma or namespace
2208 visibility, but not an explicit attribute. */
2209 && !lookup_attribute ("visibility", attribs))
2211 int depth = TMPL_ARGS_DEPTH (args);
2212 if (DECL_VISIBILITY_SPECIFIED (decl))
2214 /* A class template member with explicit visibility
2215 overrides the class visibility, so we need to apply
2216 all the levels of template args directly. */
2217 int i;
2218 for (i = 1; i <= depth; ++i)
2220 tree lev = TMPL_ARGS_LEVEL (args, i);
2221 constrain_visibility_for_template (decl, lev);
2224 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2225 /* Limit visibility based on its template arguments. */
2226 constrain_visibility_for_template (decl, args);
2231 if (class_type)
2232 determine_visibility_from_class (decl, class_type);
2234 if (decl_anon_ns_mem_p (decl))
2235 /* Names in an anonymous namespace get internal linkage.
2236 This might change once we implement export. */
2237 constrain_visibility (decl, VISIBILITY_ANON, false);
2238 else if (TREE_CODE (decl) != TYPE_DECL)
2240 /* Propagate anonymity from type to decl. */
2241 int tvis = type_visibility (TREE_TYPE (decl));
2242 if (tvis == VISIBILITY_ANON
2243 || ! DECL_VISIBILITY_SPECIFIED (decl))
2244 constrain_visibility (decl, tvis, false);
2246 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2247 /* DR 757: A type without linkage shall not be used as the type of a
2248 variable or function with linkage, unless
2249 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2250 o the variable or function is not used (3.2 [basic.def.odr]) or is
2251 defined in the same translation unit.
2253 Since non-extern "C" decls need to be defined in the same
2254 translation unit, we can make the type internal. */
2255 constrain_visibility (decl, VISIBILITY_ANON, false);
2257 /* If visibility changed and DECL already has DECL_RTL, ensure
2258 symbol flags are updated. */
2259 if ((DECL_VISIBILITY (decl) != orig_visibility
2260 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2261 && ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2262 || TREE_CODE (decl) == FUNCTION_DECL)
2263 && DECL_RTL_SET_P (decl))
2264 make_decl_rtl (decl);
2267 /* By default, static data members and function members receive
2268 the visibility of their containing class. */
2270 static void
2271 determine_visibility_from_class (tree decl, tree class_type)
2273 if (DECL_VISIBILITY_SPECIFIED (decl))
2274 return;
2276 if (determine_hidden_inline (decl))
2277 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2278 else
2280 /* Default to the class visibility. */
2281 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2282 DECL_VISIBILITY_SPECIFIED (decl)
2283 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2286 /* Give the target a chance to override the visibility associated
2287 with DECL. */
2288 if (TREE_CODE (decl) == VAR_DECL
2289 && (DECL_TINFO_P (decl)
2290 || (DECL_VTABLE_OR_VTT_P (decl)
2291 /* Construction virtual tables are not exported because
2292 they cannot be referred to from other object files;
2293 their name is not standardized by the ABI. */
2294 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2295 && TREE_PUBLIC (decl)
2296 && !DECL_REALLY_EXTERN (decl)
2297 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2298 targetm.cxx.determine_class_data_visibility (decl);
2301 /* Returns true iff DECL is an inline that should get hidden visibility
2302 because of -fvisibility-inlines-hidden. */
2304 static bool
2305 determine_hidden_inline (tree decl)
2307 return (visibility_options.inlines_hidden
2308 /* Don't do this for inline templates; specializations might not be
2309 inline, and we don't want them to inherit the hidden
2310 visibility. We'll set it here for all inline instantiations. */
2311 && !processing_template_decl
2312 && TREE_CODE (decl) == FUNCTION_DECL
2313 && DECL_DECLARED_INLINE_P (decl)
2314 && (! DECL_LANG_SPECIFIC (decl)
2315 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2318 /* Constrain the visibility of a class TYPE based on the visibility of its
2319 field types. Warn if any fields require lesser visibility. */
2321 void
2322 constrain_class_visibility (tree type)
2324 tree binfo;
2325 tree t;
2326 int i;
2328 int vis = type_visibility (type);
2330 if (vis == VISIBILITY_ANON
2331 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2332 return;
2334 /* Don't warn about visibility if the class has explicit visibility. */
2335 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2336 vis = VISIBILITY_INTERNAL;
2338 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2339 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2341 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2342 int subvis = type_visibility (ftype);
2344 if (subvis == VISIBILITY_ANON)
2346 if (!in_main_input_context ())
2347 warning (0, "\
2348 %qT has a field %qD whose type uses the anonymous namespace",
2349 type, t);
2351 else if (MAYBE_CLASS_TYPE_P (ftype)
2352 && vis < VISIBILITY_HIDDEN
2353 && subvis >= VISIBILITY_HIDDEN)
2354 warning (OPT_Wattributes, "\
2355 %qT declared with greater visibility than the type of its field %qD",
2356 type, t);
2359 binfo = TYPE_BINFO (type);
2360 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2362 int subvis = type_visibility (TREE_TYPE (t));
2364 if (subvis == VISIBILITY_ANON)
2366 if (!in_main_input_context())
2367 warning (0, "\
2368 %qT has a base %qT whose type uses the anonymous namespace",
2369 type, TREE_TYPE (t));
2371 else if (vis < VISIBILITY_HIDDEN
2372 && subvis >= VISIBILITY_HIDDEN)
2373 warning (OPT_Wattributes, "\
2374 %qT declared with greater visibility than its base %qT",
2375 type, TREE_TYPE (t));
2379 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2380 for DECL has not already been determined, do so now by setting
2381 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2382 function is called entities with vague linkage whose definitions
2383 are available must have TREE_PUBLIC set.
2385 If this function decides to place DECL in COMDAT, it will set
2386 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2387 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2388 callers defer that decision until it is clear that DECL is actually
2389 required. */
2391 void
2392 import_export_decl (tree decl)
2394 int emit_p;
2395 bool comdat_p;
2396 bool import_p;
2397 tree class_type = NULL_TREE;
2399 if (DECL_INTERFACE_KNOWN (decl))
2400 return;
2402 /* We cannot determine what linkage to give to an entity with vague
2403 linkage until the end of the file. For example, a virtual table
2404 for a class will be defined if and only if the key method is
2405 defined in this translation unit. As a further example, consider
2406 that when compiling a translation unit that uses PCH file with
2407 "-frepo" it would be incorrect to make decisions about what
2408 entities to emit when building the PCH; those decisions must be
2409 delayed until the repository information has been processed. */
2410 gcc_assert (at_eof);
2411 /* Object file linkage for explicit instantiations is handled in
2412 mark_decl_instantiated. For static variables in functions with
2413 vague linkage, maybe_commonize_var is used.
2415 Therefore, the only declarations that should be provided to this
2416 function are those with external linkage that are:
2418 * implicit instantiations of function templates
2420 * inline function
2422 * implicit instantiations of static data members of class
2423 templates
2425 * virtual tables
2427 * typeinfo objects
2429 Furthermore, all entities that reach this point must have a
2430 definition available in this translation unit.
2432 The following assertions check these conditions. */
2433 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
2434 || TREE_CODE (decl) == VAR_DECL);
2435 /* Any code that creates entities with TREE_PUBLIC cleared should
2436 also set DECL_INTERFACE_KNOWN. */
2437 gcc_assert (TREE_PUBLIC (decl));
2438 if (TREE_CODE (decl) == FUNCTION_DECL)
2439 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2440 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2441 || DECL_DECLARED_INLINE_P (decl));
2442 else
2443 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2444 || DECL_VTABLE_OR_VTT_P (decl)
2445 || DECL_TINFO_P (decl));
2446 /* Check that a definition of DECL is available in this translation
2447 unit. */
2448 gcc_assert (!DECL_REALLY_EXTERN (decl));
2450 /* Assume that DECL will not have COMDAT linkage. */
2451 comdat_p = false;
2452 /* Assume that DECL will not be imported into this translation
2453 unit. */
2454 import_p = false;
2456 /* See if the repository tells us whether or not to emit DECL in
2457 this translation unit. */
2458 emit_p = repo_emit_p (decl);
2459 if (emit_p == 0)
2460 import_p = true;
2461 else if (emit_p == 1)
2463 /* The repository indicates that this entity should be defined
2464 here. Make sure the back end honors that request. */
2465 if (TREE_CODE (decl) == VAR_DECL)
2466 mark_needed (decl);
2467 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2468 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2470 tree clone;
2471 FOR_EACH_CLONE (clone, decl)
2472 mark_needed (clone);
2474 else
2475 mark_needed (decl);
2476 /* Output the definition as an ordinary strong definition. */
2477 DECL_EXTERNAL (decl) = 0;
2478 DECL_INTERFACE_KNOWN (decl) = 1;
2479 return;
2482 if (import_p)
2483 /* We have already decided what to do with this DECL; there is no
2484 need to check anything further. */
2486 else if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
2488 class_type = DECL_CONTEXT (decl);
2489 import_export_class (class_type);
2490 if (TYPE_FOR_JAVA (class_type))
2491 import_p = true;
2492 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2493 && CLASSTYPE_INTERFACE_ONLY (class_type))
2494 import_p = true;
2495 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2496 && !CLASSTYPE_USE_TEMPLATE (class_type)
2497 && CLASSTYPE_KEY_METHOD (class_type)
2498 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2499 /* The ABI requires that all virtual tables be emitted with
2500 COMDAT linkage. However, on systems where COMDAT symbols
2501 don't show up in the table of contents for a static
2502 archive, or on systems without weak symbols (where we
2503 approximate COMDAT linkage by using internal linkage), the
2504 linker will report errors about undefined symbols because
2505 it will not see the virtual table definition. Therefore,
2506 in the case that we know that the virtual table will be
2507 emitted in only one translation unit, we make the virtual
2508 table an ordinary definition with external linkage. */
2509 DECL_EXTERNAL (decl) = 0;
2510 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2512 /* CLASS_TYPE is being exported from this translation unit,
2513 so DECL should be defined here. */
2514 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2515 /* If a class is declared in a header with the "extern
2516 template" extension, then it will not be instantiated,
2517 even in translation units that would normally require
2518 it. Often such classes are explicitly instantiated in
2519 one translation unit. Therefore, the explicit
2520 instantiation must be made visible to other translation
2521 units. */
2522 DECL_EXTERNAL (decl) = 0;
2523 else
2525 /* The generic C++ ABI says that class data is always
2526 COMDAT, even if there is a key function. Some
2527 variants (e.g., the ARM EABI) says that class data
2528 only has COMDAT linkage if the class data might be
2529 emitted in more than one translation unit. When the
2530 key method can be inline and is inline, we still have
2531 to arrange for comdat even though
2532 class_data_always_comdat is false. */
2533 if (!CLASSTYPE_KEY_METHOD (class_type)
2534 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2535 || targetm.cxx.class_data_always_comdat ())
2537 /* The ABI requires COMDAT linkage. Normally, we
2538 only emit COMDAT things when they are needed;
2539 make sure that we realize that this entity is
2540 indeed needed. */
2541 comdat_p = true;
2542 mark_needed (decl);
2546 else if (!flag_implicit_templates
2547 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2548 import_p = true;
2549 else
2550 comdat_p = true;
2552 else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2554 tree type = TREE_TYPE (DECL_NAME (decl));
2555 if (CLASS_TYPE_P (type))
2557 class_type = type;
2558 import_export_class (type);
2559 if (CLASSTYPE_INTERFACE_KNOWN (type)
2560 && TYPE_POLYMORPHIC_P (type)
2561 && CLASSTYPE_INTERFACE_ONLY (type)
2562 /* If -fno-rtti was specified, then we cannot be sure
2563 that RTTI information will be emitted with the
2564 virtual table of the class, so we must emit it
2565 wherever it is used. */
2566 && flag_rtti)
2567 import_p = true;
2568 else
2570 if (CLASSTYPE_INTERFACE_KNOWN (type)
2571 && !CLASSTYPE_INTERFACE_ONLY (type))
2573 comdat_p = (targetm.cxx.class_data_always_comdat ()
2574 || (CLASSTYPE_KEY_METHOD (type)
2575 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2576 mark_needed (decl);
2577 if (!flag_weak)
2579 comdat_p = false;
2580 DECL_EXTERNAL (decl) = 0;
2583 else
2584 comdat_p = true;
2587 else
2588 comdat_p = true;
2590 else if (DECL_TEMPLATE_INSTANTIATION (decl)
2591 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2593 /* DECL is an implicit instantiation of a function or static
2594 data member. */
2595 if ((flag_implicit_templates
2596 && !flag_use_repository)
2597 || (flag_implicit_inline_templates
2598 && TREE_CODE (decl) == FUNCTION_DECL
2599 && DECL_DECLARED_INLINE_P (decl)))
2600 comdat_p = true;
2601 else
2602 /* If we are not implicitly generating templates, then mark
2603 this entity as undefined in this translation unit. */
2604 import_p = true;
2606 else if (DECL_FUNCTION_MEMBER_P (decl))
2608 if (!DECL_DECLARED_INLINE_P (decl))
2610 tree ctype = DECL_CONTEXT (decl);
2611 import_export_class (ctype);
2612 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2614 DECL_NOT_REALLY_EXTERN (decl)
2615 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2616 || (DECL_DECLARED_INLINE_P (decl)
2617 && ! flag_implement_inlines
2618 && !DECL_VINDEX (decl)));
2620 if (!DECL_NOT_REALLY_EXTERN (decl))
2621 DECL_EXTERNAL (decl) = 1;
2623 /* Always make artificials weak. */
2624 if (DECL_ARTIFICIAL (decl) && flag_weak)
2625 comdat_p = true;
2626 else
2627 maybe_make_one_only (decl);
2630 else
2631 comdat_p = true;
2633 else
2634 comdat_p = true;
2636 if (import_p)
2638 /* If we are importing DECL into this translation unit, mark is
2639 an undefined here. */
2640 DECL_EXTERNAL (decl) = 1;
2641 DECL_NOT_REALLY_EXTERN (decl) = 0;
2643 else if (comdat_p)
2645 /* If we decided to put DECL in COMDAT, mark it accordingly at
2646 this point. */
2647 comdat_linkage (decl);
2650 DECL_INTERFACE_KNOWN (decl) = 1;
2653 /* Return an expression that performs the destruction of DECL, which
2654 must be a VAR_DECL whose type has a non-trivial destructor, or is
2655 an array whose (innermost) elements have a non-trivial destructor. */
2657 tree
2658 build_cleanup (tree decl)
2660 tree temp;
2661 tree type = TREE_TYPE (decl);
2663 /* This function should only be called for declarations that really
2664 require cleanups. */
2665 gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type));
2667 /* Treat all objects with destructors as used; the destructor may do
2668 something substantive. */
2669 mark_used (decl);
2671 if (TREE_CODE (type) == ARRAY_TYPE)
2672 temp = decl;
2673 else
2674 temp = build_address (decl);
2675 temp = build_delete (TREE_TYPE (temp), temp,
2676 sfk_complete_destructor,
2677 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
2678 tf_warning_or_error);
2679 return temp;
2682 /* Returns the initialization guard variable for the variable DECL,
2683 which has static storage duration. */
2685 tree
2686 get_guard (tree decl)
2688 tree sname;
2689 tree guard;
2691 sname = mangle_guard_variable (decl);
2692 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2693 if (! guard)
2695 tree guard_type;
2697 /* We use a type that is big enough to contain a mutex as well
2698 as an integer counter. */
2699 guard_type = targetm.cxx.guard_type ();
2700 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2701 VAR_DECL, sname, guard_type);
2703 /* The guard should have the same linkage as what it guards. */
2704 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2705 TREE_STATIC (guard) = TREE_STATIC (decl);
2706 DECL_COMMON (guard) = DECL_COMMON (decl);
2707 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2708 DECL_TLS_MODEL (guard) = DECL_TLS_MODEL (decl);
2709 if (DECL_ONE_ONLY (decl))
2710 make_decl_one_only (guard, cxx_comdat_group (guard));
2711 if (TREE_PUBLIC (decl))
2712 DECL_WEAK (guard) = DECL_WEAK (decl);
2713 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2714 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2716 DECL_ARTIFICIAL (guard) = 1;
2717 DECL_IGNORED_P (guard) = 1;
2718 TREE_USED (guard) = 1;
2719 pushdecl_top_level_and_finish (guard, NULL_TREE);
2721 return guard;
2724 /* Return those bits of the GUARD variable that should be set when the
2725 guarded entity is actually initialized. */
2727 static tree
2728 get_guard_bits (tree guard)
2730 if (!targetm.cxx.guard_mask_bit ())
2732 /* We only set the first byte of the guard, in order to leave room
2733 for a mutex in the high-order bits. */
2734 guard = build1 (ADDR_EXPR,
2735 build_pointer_type (TREE_TYPE (guard)),
2736 guard);
2737 guard = build1 (NOP_EXPR,
2738 build_pointer_type (char_type_node),
2739 guard);
2740 guard = build1 (INDIRECT_REF, char_type_node, guard);
2743 return guard;
2746 /* Return an expression which determines whether or not the GUARD
2747 variable has already been initialized. */
2749 tree
2750 get_guard_cond (tree guard)
2752 tree guard_value;
2754 /* Check to see if the GUARD is zero. */
2755 guard = get_guard_bits (guard);
2757 /* Mask off all but the low bit. */
2758 if (targetm.cxx.guard_mask_bit ())
2760 guard_value = integer_one_node;
2761 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2762 guard_value = convert (TREE_TYPE (guard), guard_value);
2763 guard = cp_build_binary_op (input_location,
2764 BIT_AND_EXPR, guard, guard_value,
2765 tf_warning_or_error);
2768 guard_value = integer_zero_node;
2769 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2770 guard_value = convert (TREE_TYPE (guard), guard_value);
2771 return cp_build_binary_op (input_location,
2772 EQ_EXPR, guard, guard_value,
2773 tf_warning_or_error);
2776 /* Return an expression which sets the GUARD variable, indicating that
2777 the variable being guarded has been initialized. */
2779 tree
2780 set_guard (tree guard)
2782 tree guard_init;
2784 /* Set the GUARD to one. */
2785 guard = get_guard_bits (guard);
2786 guard_init = integer_one_node;
2787 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2788 guard_init = convert (TREE_TYPE (guard), guard_init);
2789 return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2790 tf_warning_or_error);
2793 /* Returns true iff we can tell that VAR does not have a dynamic
2794 initializer. */
2796 static bool
2797 var_defined_without_dynamic_init (tree var)
2799 /* If it's defined in another TU, we can't tell. */
2800 if (DECL_EXTERNAL (var))
2801 return false;
2802 /* If it has a non-trivial destructor, registering the destructor
2803 counts as dynamic initialization. */
2804 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
2805 return false;
2806 /* If it's in this TU, its initializer has been processed. */
2807 gcc_assert (DECL_INITIALIZED_P (var));
2808 /* If it has no initializer or a constant one, it's not dynamic. */
2809 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
2810 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
2813 /* Returns true iff VAR is a variable that needs uses to be
2814 wrapped for possible dynamic initialization. */
2816 static bool
2817 var_needs_tls_wrapper (tree var)
2819 return (!error_operand_p (var)
2820 && DECL_THREAD_LOCAL_P (var)
2821 && !DECL_GNU_TLS_P (var)
2822 && !DECL_FUNCTION_SCOPE_P (var)
2823 && !var_defined_without_dynamic_init (var));
2826 /* Get the FUNCTION_DECL for the shared TLS init function for this
2827 translation unit. */
2829 static tree
2830 get_local_tls_init_fn (void)
2832 tree sname = get_identifier ("__tls_init");
2833 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
2834 if (!fn)
2836 fn = build_lang_decl (FUNCTION_DECL, sname,
2837 build_function_type (void_type_node,
2838 void_list_node));
2839 SET_DECL_LANGUAGE (fn, lang_c);
2840 TREE_PUBLIC (fn) = false;
2841 DECL_ARTIFICIAL (fn) = true;
2842 mark_used (fn);
2843 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
2845 return fn;
2848 /* Get a FUNCTION_DECL for the init function for the thread_local
2849 variable VAR. The init function will be an alias to the function
2850 that initializes all the non-local TLS variables in the translation
2851 unit. The init function is only used by the wrapper function. */
2853 static tree
2854 get_tls_init_fn (tree var)
2856 /* Only C++11 TLS vars need this init fn. */
2857 if (!var_needs_tls_wrapper (var))
2858 return NULL_TREE;
2860 /* If -fno-extern-tls-init, assume that we don't need to call
2861 a tls init function for a variable defined in another TU. */
2862 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
2863 return NULL_TREE;
2865 #ifdef ASM_OUTPUT_DEF
2866 /* If the variable is internal, or if we can't generate aliases,
2867 call the local init function directly. */
2868 if (!TREE_PUBLIC (var))
2869 #endif
2870 return get_local_tls_init_fn ();
2872 tree sname = mangle_tls_init_fn (var);
2873 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
2874 if (!fn)
2876 fn = build_lang_decl (FUNCTION_DECL, sname,
2877 build_function_type (void_type_node,
2878 void_list_node));
2879 SET_DECL_LANGUAGE (fn, lang_c);
2880 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
2881 DECL_ARTIFICIAL (fn) = true;
2882 DECL_COMDAT (fn) = DECL_COMDAT (var);
2883 DECL_EXTERNAL (fn) = true;
2884 if (DECL_ONE_ONLY (var))
2885 make_decl_one_only (fn, cxx_comdat_group (fn));
2886 if (TREE_PUBLIC (var))
2888 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
2889 /* If the variable is defined somewhere else and might have static
2890 initialization, make the init function a weak reference. */
2891 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
2892 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
2893 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
2894 && DECL_EXTERNAL (var))
2895 declare_weak (fn);
2896 else
2897 DECL_WEAK (fn) = DECL_WEAK (var);
2899 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
2900 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
2901 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
2902 DECL_IGNORED_P (fn) = 1;
2903 mark_used (fn);
2905 DECL_BEFRIENDING_CLASSES (fn) = var;
2907 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
2909 return fn;
2912 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
2913 variable VAR. The wrapper function calls the init function (if any) for
2914 VAR and then returns a reference to VAR. The wrapper function is used
2915 in place of VAR everywhere VAR is mentioned. */
2917 tree
2918 get_tls_wrapper_fn (tree var)
2920 /* Only C++11 TLS vars need this wrapper fn. */
2921 if (!var_needs_tls_wrapper (var))
2922 return NULL_TREE;
2924 tree sname = mangle_tls_wrapper_fn (var);
2925 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
2926 if (!fn)
2928 /* A named rvalue reference is an lvalue, so the wrapper should
2929 always return an lvalue reference. */
2930 tree type = non_reference (TREE_TYPE (var));
2931 type = build_reference_type (type);
2932 tree fntype = build_function_type (type, void_list_node);
2933 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
2934 SET_DECL_LANGUAGE (fn, lang_c);
2935 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
2936 DECL_ARTIFICIAL (fn) = true;
2937 DECL_IGNORED_P (fn) = 1;
2938 /* The wrapper is inline and emitted everywhere var is used. */
2939 DECL_DECLARED_INLINE_P (fn) = true;
2940 if (TREE_PUBLIC (var))
2942 comdat_linkage (fn);
2943 #ifdef HAVE_GAS_HIDDEN
2944 /* Make the wrapper bind locally; there's no reason to share
2945 the wrapper between multiple shared objects. */
2946 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
2947 DECL_VISIBILITY_SPECIFIED (fn) = true;
2948 #endif
2950 if (!TREE_PUBLIC (fn))
2951 DECL_INTERFACE_KNOWN (fn) = true;
2952 mark_used (fn);
2953 note_vague_linkage_fn (fn);
2955 #if 0
2956 /* We want CSE to commonize calls to the wrapper, but marking it as
2957 pure is unsafe since it has side-effects. I guess we need a new
2958 ECF flag even weaker than ECF_PURE. FIXME! */
2959 DECL_PURE_P (fn) = true;
2960 #endif
2962 DECL_BEFRIENDING_CLASSES (fn) = var;
2964 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
2966 return fn;
2969 /* At EOF, generate the definition for the TLS wrapper function FN:
2971 T& var_wrapper() {
2972 if (init_fn) init_fn();
2973 return var;
2974 } */
2976 static void
2977 generate_tls_wrapper (tree fn)
2979 tree var = DECL_BEFRIENDING_CLASSES (fn);
2981 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
2982 tree body = begin_function_body ();
2983 /* Only call the init fn if there might be one. */
2984 if (tree init_fn = get_tls_init_fn (var))
2986 tree if_stmt = NULL_TREE;
2987 /* If init_fn is a weakref, make sure it exists before calling. */
2988 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
2990 if_stmt = begin_if_stmt ();
2991 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
2992 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
2993 NE_EXPR, addr, nullptr_node,
2994 tf_warning_or_error);
2995 finish_if_stmt_cond (cond, if_stmt);
2997 finish_expr_stmt (build_cxx_call
2998 (init_fn, 0, NULL, tf_warning_or_error));
2999 if (if_stmt)
3001 finish_then_clause (if_stmt);
3002 finish_if_stmt (if_stmt);
3005 else
3006 /* If there's no initialization, the wrapper is a constant function. */
3007 TREE_READONLY (fn) = true;
3008 finish_return_stmt (convert_from_reference (var));
3009 finish_function_body (body);
3010 expand_or_defer_fn (finish_function (0));
3013 /* Start the process of running a particular set of global constructors
3014 or destructors. Subroutine of do_[cd]tors. */
3016 static tree
3017 start_objects (int method_type, int initp)
3019 tree body;
3020 tree fndecl;
3021 char type[14];
3023 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3025 if (initp != DEFAULT_INIT_PRIORITY)
3027 char joiner;
3029 #ifdef JOINER
3030 joiner = JOINER;
3031 #else
3032 joiner = '_';
3033 #endif
3035 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3037 else
3038 sprintf (type, "sub_%c", method_type);
3040 fndecl = build_lang_decl (FUNCTION_DECL,
3041 get_file_function_name (type),
3042 build_function_type_list (void_type_node,
3043 NULL_TREE));
3044 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3046 TREE_PUBLIC (current_function_decl) = 0;
3048 /* Mark as artificial because it's not explicitly in the user's
3049 source code. */
3050 DECL_ARTIFICIAL (current_function_decl) = 1;
3052 /* Mark this declaration as used to avoid spurious warnings. */
3053 TREE_USED (current_function_decl) = 1;
3055 /* Mark this function as a global constructor or destructor. */
3056 if (method_type == 'I')
3057 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3058 else
3059 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3061 body = begin_compound_stmt (BCS_FN_BODY);
3063 return body;
3066 /* Finish the process of running a particular set of global constructors
3067 or destructors. Subroutine of do_[cd]tors. */
3069 static void
3070 finish_objects (int method_type, int initp, tree body)
3072 tree fn;
3074 /* Finish up. */
3075 finish_compound_stmt (body);
3076 fn = finish_function (0);
3078 if (method_type == 'I')
3080 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3081 decl_init_priority_insert (fn, initp);
3083 else
3085 DECL_STATIC_DESTRUCTOR (fn) = 1;
3086 decl_fini_priority_insert (fn, initp);
3089 expand_or_defer_fn (fn);
3092 /* The names of the parameters to the function created to handle
3093 initializations and destructions for objects with static storage
3094 duration. */
3095 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3096 #define PRIORITY_IDENTIFIER "__priority"
3098 /* The name of the function we create to handle initializations and
3099 destructions for objects with static storage duration. */
3100 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3102 /* The declaration for the __INITIALIZE_P argument. */
3103 static GTY(()) tree initialize_p_decl;
3105 /* The declaration for the __PRIORITY argument. */
3106 static GTY(()) tree priority_decl;
3108 /* The declaration for the static storage duration function. */
3109 static GTY(()) tree ssdf_decl;
3111 /* All the static storage duration functions created in this
3112 translation unit. */
3113 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3115 /* A map from priority levels to information about that priority
3116 level. There may be many such levels, so efficient lookup is
3117 important. */
3118 static splay_tree priority_info_map;
3120 /* Begins the generation of the function that will handle all
3121 initialization and destruction of objects with static storage
3122 duration. The function generated takes two parameters of type
3123 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3124 nonzero, it performs initializations. Otherwise, it performs
3125 destructions. It only performs those initializations or
3126 destructions with the indicated __PRIORITY. The generated function
3127 returns no value.
3129 It is assumed that this function will only be called once per
3130 translation unit. */
3132 static tree
3133 start_static_storage_duration_function (unsigned count)
3135 tree type;
3136 tree body;
3137 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 64];
3139 /* Create the identifier for this function. It will be of the form
3140 SSDF_IDENTIFIER_<number>. */
3141 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3142 if (L_IPO_IS_AUXILIARY_MODULE)
3143 sprintf (id, "%s.cmo.%u", id, current_module_id);
3145 type = build_function_type_list (void_type_node,
3146 integer_type_node, integer_type_node,
3147 NULL_TREE);
3149 /* Create the FUNCTION_DECL itself. */
3150 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3151 get_identifier (id),
3152 type);
3153 TREE_PUBLIC (ssdf_decl) = 0;
3154 DECL_ARTIFICIAL (ssdf_decl) = 1;
3156 /* Put this function in the list of functions to be called from the
3157 static constructors and destructors. */
3158 if (!ssdf_decls)
3160 vec_alloc (ssdf_decls, 32);
3162 /* Take this opportunity to initialize the map from priority
3163 numbers to information about that priority level. */
3164 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3165 /*delete_key_fn=*/0,
3166 /*delete_value_fn=*/
3167 (splay_tree_delete_value_fn) &free);
3169 /* We always need to generate functions for the
3170 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3171 priorities later, we'll be sure to find the
3172 DEFAULT_INIT_PRIORITY. */
3173 get_priority_info (DEFAULT_INIT_PRIORITY);
3176 vec_safe_push (ssdf_decls, ssdf_decl);
3178 /* Create the argument list. */
3179 initialize_p_decl = cp_build_parm_decl
3180 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3181 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3182 TREE_USED (initialize_p_decl) = 1;
3183 priority_decl = cp_build_parm_decl
3184 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3185 DECL_CONTEXT (priority_decl) = ssdf_decl;
3186 TREE_USED (priority_decl) = 1;
3188 DECL_CHAIN (initialize_p_decl) = priority_decl;
3189 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3191 /* Put the function in the global scope. */
3192 pushdecl (ssdf_decl);
3194 /* Start the function itself. This is equivalent to declaring the
3195 function as:
3197 static void __ssdf (int __initialize_p, init __priority_p);
3199 It is static because we only need to call this function from the
3200 various constructor and destructor functions for this module. */
3201 start_preparsed_function (ssdf_decl,
3202 /*attrs=*/NULL_TREE,
3203 SF_PRE_PARSED);
3205 /* Set up the scope of the outermost block in the function. */
3206 body = begin_compound_stmt (BCS_FN_BODY);
3208 return body;
3211 /* Finish the generation of the function which performs initialization
3212 and destruction of objects with static storage duration. After
3213 this point, no more such objects can be created. */
3215 static void
3216 finish_static_storage_duration_function (tree body)
3218 /* Close out the function. */
3219 finish_compound_stmt (body);
3220 expand_or_defer_fn (finish_function (0));
3223 /* Return the information about the indicated PRIORITY level. If no
3224 code to handle this level has yet been generated, generate the
3225 appropriate prologue. */
3227 static priority_info
3228 get_priority_info (int priority)
3230 priority_info pi;
3231 splay_tree_node n;
3233 n = splay_tree_lookup (priority_info_map,
3234 (splay_tree_key) priority);
3235 if (!n)
3237 /* Create a new priority information structure, and insert it
3238 into the map. */
3239 pi = XNEW (struct priority_info_s);
3240 pi->initializations_p = 0;
3241 pi->destructions_p = 0;
3242 splay_tree_insert (priority_info_map,
3243 (splay_tree_key) priority,
3244 (splay_tree_value) pi);
3246 else
3247 pi = (priority_info) n->value;
3249 return pi;
3252 /* The effective initialization priority of a DECL. */
3254 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3255 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3256 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3258 /* Whether a DECL needs a guard to protect it against multiple
3259 initialization. */
3261 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3262 || DECL_ONE_ONLY (decl) \
3263 || DECL_WEAK (decl)))
3265 /* Called from one_static_initialization_or_destruction(),
3266 via walk_tree.
3267 Walks the initializer list of a global variable and looks for
3268 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3269 and that have their DECL_CONTEXT() == NULL.
3270 For each such temporary variable, set their DECL_CONTEXT() to
3271 the current function. This is necessary because otherwise
3272 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3273 when trying to refer to a temporary variable that does not have
3274 it's DECL_CONTECT() properly set. */
3275 static tree
3276 fix_temporary_vars_context_r (tree *node,
3277 int * /*unused*/,
3278 void * /*unused1*/)
3280 gcc_assert (current_function_decl);
3282 if (TREE_CODE (*node) == BIND_EXPR)
3284 tree var;
3286 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3287 if (TREE_CODE (var) == VAR_DECL
3288 && !DECL_NAME (var)
3289 && DECL_ARTIFICIAL (var)
3290 && !DECL_CONTEXT (var))
3291 DECL_CONTEXT (var) = current_function_decl;
3294 return NULL_TREE;
3297 /* Set up to handle the initialization or destruction of DECL. If
3298 INITP is nonzero, we are initializing the variable. Otherwise, we
3299 are destroying it. */
3301 static void
3302 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3304 tree guard_if_stmt = NULL_TREE;
3305 tree guard;
3307 /* If we are supposed to destruct and there's a trivial destructor,
3308 nothing has to be done. */
3309 if (!initp
3310 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3311 return;
3313 /* Trick the compiler into thinking we are at the file and line
3314 where DECL was declared so that error-messages make sense, and so
3315 that the debugger will show somewhat sensible file and line
3316 information. */
3317 input_location = DECL_SOURCE_LOCATION (decl);
3319 /* Make sure temporary variables in the initialiser all have
3320 their DECL_CONTEXT() set to a value different from NULL_TREE.
3321 This can happen when global variables initialisers are built.
3322 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3323 the temporary variables that might have been generated in the
3324 accompagning initialisers is NULL_TREE, meaning the variables have been
3325 declared in the global namespace.
3326 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3327 of the temporaries are set to the current function decl. */
3328 cp_walk_tree_without_duplicates (&init,
3329 fix_temporary_vars_context_r,
3330 NULL);
3332 /* Because of:
3334 [class.access.spec]
3336 Access control for implicit calls to the constructors,
3337 the conversion functions, or the destructor called to
3338 create and destroy a static data member is performed as
3339 if these calls appeared in the scope of the member's
3340 class.
3342 we pretend we are in a static member function of the class of
3343 which the DECL is a member. */
3344 if (member_p (decl))
3346 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3347 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3350 /* Assume we don't need a guard. */
3351 guard = NULL_TREE;
3352 /* We need a guard if this is an object with external linkage that
3353 might be initialized in more than one place. (For example, a
3354 static data member of a template, when the data member requires
3355 construction.) */
3356 if (NEEDS_GUARD_P (decl))
3358 tree guard_cond;
3360 guard = get_guard (decl);
3362 /* When using __cxa_atexit, we just check the GUARD as we would
3363 for a local static. */
3364 if (flag_use_cxa_atexit)
3366 /* When using __cxa_atexit, we never try to destroy
3367 anything from a static destructor. */
3368 gcc_assert (initp);
3369 guard_cond = get_guard_cond (guard);
3371 /* If we don't have __cxa_atexit, then we will be running
3372 destructors from .fini sections, or their equivalents. So,
3373 we need to know how many times we've tried to initialize this
3374 object. We do initializations only if the GUARD is zero,
3375 i.e., if we are the first to initialize the variable. We do
3376 destructions only if the GUARD is one, i.e., if we are the
3377 last to destroy the variable. */
3378 else if (initp)
3379 guard_cond
3380 = cp_build_binary_op (input_location,
3381 EQ_EXPR,
3382 cp_build_unary_op (PREINCREMENT_EXPR,
3383 guard,
3384 /*noconvert=*/1,
3385 tf_warning_or_error),
3386 integer_one_node,
3387 tf_warning_or_error);
3388 else
3389 guard_cond
3390 = cp_build_binary_op (input_location,
3391 EQ_EXPR,
3392 cp_build_unary_op (PREDECREMENT_EXPR,
3393 guard,
3394 /*noconvert=*/1,
3395 tf_warning_or_error),
3396 integer_zero_node,
3397 tf_warning_or_error);
3399 guard_if_stmt = begin_if_stmt ();
3400 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3404 /* If we're using __cxa_atexit, we have not already set the GUARD,
3405 so we must do so now. */
3406 if (guard && initp && flag_use_cxa_atexit)
3407 finish_expr_stmt (set_guard (guard));
3409 /* Perform the initialization or destruction. */
3410 if (initp)
3412 if (init)
3413 finish_expr_stmt (init);
3415 /* If we're using __cxa_atexit, register a function that calls the
3416 destructor for the object. */
3417 if (flag_use_cxa_atexit)
3418 finish_expr_stmt (register_dtor_fn (decl));
3420 else
3421 finish_expr_stmt (build_cleanup (decl));
3423 /* Finish the guard if-stmt, if necessary. */
3424 if (guard)
3426 finish_then_clause (guard_if_stmt);
3427 finish_if_stmt (guard_if_stmt);
3430 /* Now that we're done with DECL we don't need to pretend to be a
3431 member of its class any longer. */
3432 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3433 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3436 /* Generate code to do the initialization or destruction of the decls in VARS,
3437 a TREE_LIST of VAR_DECL with static storage duration.
3438 Whether initialization or destruction is performed is specified by INITP. */
3440 static void
3441 do_static_initialization_or_destruction (tree vars, bool initp)
3443 tree node, init_if_stmt, cond;
3445 /* Build the outer if-stmt to check for initialization or destruction. */
3446 init_if_stmt = begin_if_stmt ();
3447 cond = initp ? integer_one_node : integer_zero_node;
3448 cond = cp_build_binary_op (input_location,
3449 EQ_EXPR,
3450 initialize_p_decl,
3451 cond,
3452 tf_warning_or_error);
3453 finish_if_stmt_cond (cond, init_if_stmt);
3455 node = vars;
3456 do {
3457 tree decl = TREE_VALUE (node);
3458 tree priority_if_stmt;
3459 int priority;
3460 priority_info pi;
3462 /* If we don't need a destructor, there's nothing to do. Avoid
3463 creating a possibly empty if-stmt. */
3464 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3466 node = TREE_CHAIN (node);
3467 continue;
3470 /* Remember that we had an initialization or finalization at this
3471 priority. */
3472 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3473 pi = get_priority_info (priority);
3474 if (initp)
3475 pi->initializations_p = 1;
3476 else
3477 pi->destructions_p = 1;
3479 /* Conditionalize this initialization on being in the right priority
3480 and being initializing/finalizing appropriately. */
3481 priority_if_stmt = begin_if_stmt ();
3482 cond = cp_build_binary_op (input_location,
3483 EQ_EXPR,
3484 priority_decl,
3485 build_int_cst (NULL_TREE, priority),
3486 tf_warning_or_error);
3487 finish_if_stmt_cond (cond, priority_if_stmt);
3489 /* Process initializers with same priority. */
3490 for (; node
3491 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3492 node = TREE_CHAIN (node))
3493 /* Do one initialization or destruction. */
3494 one_static_initialization_or_destruction (TREE_VALUE (node),
3495 TREE_PURPOSE (node), initp);
3497 /* Finish up the priority if-stmt body. */
3498 finish_then_clause (priority_if_stmt);
3499 finish_if_stmt (priority_if_stmt);
3501 } while (node);
3503 /* Finish up the init/destruct if-stmt body. */
3504 finish_then_clause (init_if_stmt);
3505 finish_if_stmt (init_if_stmt);
3508 /* VARS is a list of variables with static storage duration which may
3509 need initialization and/or finalization. Remove those variables
3510 that don't really need to be initialized or finalized, and return
3511 the resulting list. The order in which the variables appear in
3512 VARS is in reverse order of the order in which they should actually
3513 be initialized. The list we return is in the unreversed order;
3514 i.e., the first variable should be initialized first. */
3516 static tree
3517 prune_vars_needing_no_initialization (tree *vars)
3519 tree *var = vars;
3520 tree result = NULL_TREE;
3522 while (*var)
3524 tree t = *var;
3525 tree decl = TREE_VALUE (t);
3526 tree init = TREE_PURPOSE (t);
3528 /* Deal gracefully with error. */
3529 if (decl == error_mark_node)
3531 var = &TREE_CHAIN (t);
3532 continue;
3535 /* The only things that can be initialized are variables. */
3536 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3538 /* If this object is not defined, we don't need to do anything
3539 here. */
3540 if (DECL_EXTERNAL (decl))
3542 var = &TREE_CHAIN (t);
3543 continue;
3546 /* Also, if the initializer already contains errors, we can bail
3547 out now. */
3548 if (init && TREE_CODE (init) == TREE_LIST
3549 && value_member (error_mark_node, init))
3551 var = &TREE_CHAIN (t);
3552 continue;
3555 gcc_assert (!L_IPO_IS_AUXILIARY_MODULE
3556 || varpool_is_auxiliary (varpool_node_for_decl (decl)));
3558 /* This variable is going to need initialization and/or
3559 finalization, so we add it to the list. */
3560 *var = TREE_CHAIN (t);
3561 TREE_CHAIN (t) = result;
3562 result = t;
3565 return result;
3568 /* Make sure we have told the back end about all the variables in
3569 VARS. */
3571 static void
3572 write_out_vars (tree vars)
3574 tree v;
3576 for (v = vars; v; v = TREE_CHAIN (v))
3578 tree var = TREE_VALUE (v);
3579 if (!var_finalized_p (var))
3581 import_export_decl (var);
3582 rest_of_decl_compilation (var, 1, 1);
3587 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3588 (otherwise) that will initialize all global objects with static
3589 storage duration having the indicated PRIORITY. */
3591 static void
3592 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3593 location_t *locus)
3595 char function_key;
3596 tree fndecl;
3597 tree body;
3598 size_t i;
3600 input_location = *locus;
3601 /* ??? */
3602 /* Was: locus->line++; */
3604 /* We use `I' to indicate initialization and `D' to indicate
3605 destruction. */
3606 function_key = constructor_p ? 'I' : 'D';
3608 /* We emit the function lazily, to avoid generating empty
3609 global constructors and destructors. */
3610 body = NULL_TREE;
3612 /* For Objective-C++, we may need to initialize metadata found in this module.
3613 This must be done _before_ any other static initializations. */
3614 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3615 && constructor_p && objc_static_init_needed_p ())
3617 body = start_objects (function_key, priority);
3618 objc_generate_static_init_call (NULL_TREE);
3621 /* Call the static storage duration function with appropriate
3622 arguments. */
3623 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3625 /* Calls to pure or const functions will expand to nothing. */
3626 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3628 tree call;
3630 if (! body)
3631 body = start_objects (function_key, priority);
3633 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3634 build_int_cst (NULL_TREE,
3635 constructor_p),
3636 build_int_cst (NULL_TREE,
3637 priority),
3638 NULL_TREE);
3639 finish_expr_stmt (call);
3643 /* Close out the function. */
3644 if (body)
3645 finish_objects (function_key, priority, body);
3648 /* Generate constructor and destructor functions for the priority
3649 indicated by N. */
3651 static int
3652 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3654 location_t *locus = (location_t *) data;
3655 int priority = (int) n->key;
3656 priority_info pi = (priority_info) n->value;
3658 /* Generate the functions themselves, but only if they are really
3659 needed. */
3660 if (pi->initializations_p)
3661 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3662 if (pi->destructions_p)
3663 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3665 /* Keep iterating. */
3666 return 0;
3669 /* Java requires that we be able to reference a local address for a
3670 method, and not be confused by PLT entries. If hidden aliases are
3671 supported, collect and return all the functions for which we should
3672 emit a hidden alias. */
3674 static struct pointer_set_t *
3675 collect_candidates_for_java_method_aliases (void)
3677 struct cgraph_node *node;
3678 struct pointer_set_t *candidates = NULL;
3680 #ifndef HAVE_GAS_HIDDEN
3681 return candidates;
3682 #endif
3684 FOR_EACH_FUNCTION (node)
3686 tree fndecl = node->symbol.decl;
3688 if (DECL_CONTEXT (fndecl)
3689 && TYPE_P (DECL_CONTEXT (fndecl))
3690 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3691 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3693 if (candidates == NULL)
3694 candidates = pointer_set_create ();
3695 pointer_set_insert (candidates, fndecl);
3699 return candidates;
3703 /* Java requires that we be able to reference a local address for a
3704 method, and not be confused by PLT entries. If hidden aliases are
3705 supported, emit one for each java function that we've emitted.
3706 CANDIDATES is the set of FUNCTION_DECLs that were gathered
3707 by collect_candidates_for_java_method_aliases. */
3709 static void
3710 build_java_method_aliases (struct pointer_set_t *candidates)
3712 struct cgraph_node *node;
3714 #ifndef HAVE_GAS_HIDDEN
3715 return;
3716 #endif
3718 FOR_EACH_FUNCTION (node)
3720 tree fndecl = node->symbol.decl;
3722 if (TREE_ASM_WRITTEN (fndecl)
3723 && pointer_set_contains (candidates, fndecl))
3725 /* Mangle the name in a predictable way; we need to reference
3726 this from a java compiled object file. */
3727 tree oid, nid, alias;
3728 const char *oname;
3729 char *nname;
3731 oid = DECL_ASSEMBLER_NAME (fndecl);
3732 oname = IDENTIFIER_POINTER (oid);
3733 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3734 nname = ACONCAT (("_ZGA", oname+2, NULL));
3735 nid = get_identifier (nname);
3737 alias = make_alias_for (fndecl, nid);
3738 TREE_PUBLIC (alias) = 1;
3739 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3741 assemble_alias (alias, oid);
3746 /* Return C++ property of T, based on given operation OP. */
3748 static int
3749 cpp_check (tree t, cpp_operation op)
3751 switch (op)
3753 case IS_ABSTRACT:
3754 return DECL_PURE_VIRTUAL_P (t);
3755 case IS_CONSTRUCTOR:
3756 return DECL_CONSTRUCTOR_P (t);
3757 case IS_DESTRUCTOR:
3758 return DECL_DESTRUCTOR_P (t);
3759 case IS_COPY_CONSTRUCTOR:
3760 return DECL_COPY_CONSTRUCTOR_P (t);
3761 case IS_TEMPLATE:
3762 return TREE_CODE (t) == TEMPLATE_DECL;
3763 default:
3764 return 0;
3768 /* Collect source file references recursively, starting from NAMESPC. */
3770 static void
3771 collect_source_refs (tree namespc)
3773 tree t;
3775 if (!namespc)
3776 return;
3778 /* Iterate over names in this name space. */
3779 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
3780 if (!DECL_IS_BUILTIN (t) )
3781 collect_source_ref (DECL_SOURCE_FILE (t));
3783 /* Dump siblings, if any */
3784 collect_source_refs (TREE_CHAIN (namespc));
3786 /* Dump children, if any */
3787 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
3790 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
3791 starting from NAMESPC. */
3793 static void
3794 collect_ada_namespace (tree namespc, const char *source_file)
3796 if (!namespc)
3797 return;
3799 /* Collect decls from this namespace */
3800 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
3802 /* Collect siblings, if any */
3803 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
3805 /* Collect children, if any */
3806 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
3809 /* Returns true iff there is a definition available for variable or
3810 function DECL. */
3812 static bool
3813 decl_defined_p (tree decl)
3815 if (TREE_CODE (decl) == FUNCTION_DECL)
3816 return (DECL_INITIAL (decl) != NULL_TREE);
3817 else
3819 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3820 return !DECL_EXTERNAL (decl);
3824 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
3826 [expr.const]
3828 An integral constant-expression can only involve ... const
3829 variables of integral or enumeration types initialized with
3830 constant expressions ...
3832 C++0x also allows constexpr variables and temporaries initialized
3833 with constant expressions. We handle the former here, but the latter
3834 are just folded away in cxx_eval_constant_expression.
3836 The standard does not require that the expression be non-volatile.
3837 G++ implements the proposed correction in DR 457. */
3839 bool
3840 decl_constant_var_p (tree decl)
3842 if (!decl_maybe_constant_var_p (decl))
3843 return false;
3845 /* We don't know if a template static data member is initialized with
3846 a constant expression until we instantiate its initializer. Even
3847 in the case of a constexpr variable, we can't treat it as a
3848 constant until its initializer is complete in case it's used in
3849 its own initializer. */
3850 mark_used (decl);
3851 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
3854 /* Returns true if DECL could be a symbolic constant variable, depending on
3855 its initializer. */
3857 bool
3858 decl_maybe_constant_var_p (tree decl)
3860 tree type = TREE_TYPE (decl);
3861 if (TREE_CODE (decl) != VAR_DECL)
3862 return false;
3863 if (DECL_DECLARED_CONSTEXPR_P (decl))
3864 return true;
3865 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
3866 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3869 /* Complain that DECL uses a type with no linkage but is never defined. */
3871 static void
3872 no_linkage_error (tree decl)
3874 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
3875 if (TYPE_ANONYMOUS_P (t))
3877 permerror (0, "%q+#D, declared using anonymous type, "
3878 "is used but never defined", decl);
3879 if (is_typedef_decl (TYPE_NAME (t)))
3880 permerror (0, "%q+#D does not refer to the unqualified type, "
3881 "so it is not used for linkage", TYPE_NAME (t));
3883 else
3884 permerror (0, "%q+#D, declared using local type %qT, "
3885 "is used but never defined", decl, t);
3888 /* Clear the list of deferred functions. */
3889 void
3890 cp_clear_deferred_fns (void)
3892 vec_free (deferred_fns);
3893 deferred_fns = NULL;
3894 keyed_classes = NULL;
3895 vec_free (no_linkage_decls);
3896 no_linkage_decls = NULL;
3897 cp_clear_constexpr_hashtable ();
3898 clear_pending_templates ();
3901 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
3903 static void
3904 collect_all_refs (const char *source_file)
3906 collect_ada_namespace (global_namespace, source_file);
3909 /* Clear DECL_EXTERNAL for NODE. */
3911 static bool
3912 clear_decl_external (struct cgraph_node *node, void * /*data*/)
3914 DECL_EXTERNAL (node->symbol.decl) = 0;
3915 return false;
3918 /* Build up the function to run dynamic initializers for thread_local
3919 variables in this translation unit and alias the init functions for the
3920 individual variables to it. */
3922 static void
3923 handle_tls_init (void)
3925 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
3926 if (vars == NULL_TREE)
3927 return;
3929 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
3931 write_out_vars (vars);
3933 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
3934 boolean_type_node);
3935 TREE_PUBLIC (guard) = false;
3936 TREE_STATIC (guard) = true;
3937 DECL_ARTIFICIAL (guard) = true;
3938 DECL_IGNORED_P (guard) = true;
3939 TREE_USED (guard) = true;
3940 DECL_TLS_MODEL (guard) = decl_default_tls_model (guard);
3941 pushdecl_top_level_and_finish (guard, NULL_TREE);
3943 tree fn = get_local_tls_init_fn ();
3944 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
3945 tree body = begin_function_body ();
3946 tree if_stmt = begin_if_stmt ();
3947 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
3948 tf_warning_or_error);
3949 finish_if_stmt_cond (cond, if_stmt);
3950 finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
3951 tf_warning_or_error));
3952 for (; vars; vars = TREE_CHAIN (vars))
3954 tree var = TREE_VALUE (vars);
3955 tree init = TREE_PURPOSE (vars);
3956 one_static_initialization_or_destruction (var, init, true);
3958 #ifdef ASM_OUTPUT_DEF
3959 /* Output init aliases even with -fno-extern-tls-init. */
3960 if (TREE_PUBLIC (var))
3962 tree single_init_fn = get_tls_init_fn (var);
3963 cgraph_node *alias
3964 = cgraph_same_body_alias (cgraph_get_create_node (fn),
3965 single_init_fn, fn);
3966 gcc_assert (alias != NULL);
3968 #endif
3971 finish_then_clause (if_stmt);
3972 finish_if_stmt (if_stmt);
3973 finish_function_body (body);
3974 expand_or_defer_fn (finish_function (0));
3977 /* This routine is called at the end of compilation.
3978 Its job is to create all the code needed to initialize and
3979 destroy the global aggregates. We do the destruction
3980 first, since that way we only need to reverse the decls once. */
3982 void
3983 cp_process_pending_declarations (location_t locus)
3985 tree vars, decl;
3986 bool reconsider;
3987 size_t i;
3988 unsigned ssdf_count = 0;
3989 int retries = 0;
3991 timevar_start (TV_PHASE_DEFERRED);
3995 tree t;
3996 tree decl;
3998 reconsider = false;
4000 /* If there are templates that we've put off instantiating, do
4001 them now. */
4002 instantiate_pending_templates (retries);
4003 ggc_collect ();
4005 /* Write out virtual tables as required. Note that writing out
4006 the virtual table for a template class may cause the
4007 instantiation of members of that class. If we write out
4008 vtables then we remove the class from our list so we don't
4009 have to look at it again. */
4011 while (keyed_classes != NULL_TREE
4012 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4014 reconsider = true;
4015 keyed_classes = TREE_CHAIN (keyed_classes);
4018 t = keyed_classes;
4019 if (t != NULL_TREE)
4021 tree next = TREE_CHAIN (t);
4023 while (next)
4025 if (maybe_emit_vtables (TREE_VALUE (next)))
4027 reconsider = true;
4028 TREE_CHAIN (t) = TREE_CHAIN (next);
4030 else
4031 t = next;
4033 next = TREE_CHAIN (t);
4037 /* Write out needed type info variables. We have to be careful
4038 looping through unemitted decls, because emit_tinfo_decl may
4039 cause other variables to be needed. New elements will be
4040 appended, and we remove from the vector those that actually
4041 get emitted. */
4042 for (i = unemitted_tinfo_decls->length ();
4043 unemitted_tinfo_decls->iterate (--i, &t);)
4044 if (emit_tinfo_decl (t))
4046 reconsider = true;
4047 unemitted_tinfo_decls->unordered_remove (i);
4050 /* The list of objects with static storage duration is built up
4051 in reverse order. We clear STATIC_AGGREGATES so that any new
4052 aggregates added during the initialization of these will be
4053 initialized in the correct order when we next come around the
4054 loop. */
4055 vars = prune_vars_needing_no_initialization (&static_aggregates);
4057 if (vars)
4059 /* We need to start a new initialization function each time
4060 through the loop. That's because we need to know which
4061 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4062 isn't computed until a function is finished, and written
4063 out. That's a deficiency in the back end. When this is
4064 fixed, these initialization functions could all become
4065 inline, with resulting performance improvements. */
4066 tree ssdf_body;
4068 /* Set the line and file, so that it is obviously not from
4069 the source file. */
4070 input_location = locus;
4071 ssdf_body = start_static_storage_duration_function (ssdf_count);
4073 /* Make sure the back end knows about all the variables. */
4074 write_out_vars (vars);
4076 /* First generate code to do all the initializations. */
4077 if (vars)
4078 do_static_initialization_or_destruction (vars, /*initp=*/true);
4080 /* Then, generate code to do all the destructions. Do these
4081 in reverse order so that the most recently constructed
4082 variable is the first destroyed. If we're using
4083 __cxa_atexit, then we don't need to do this; functions
4084 were registered at initialization time to destroy the
4085 local statics. */
4086 if (!flag_use_cxa_atexit && vars)
4088 vars = nreverse (vars);
4089 do_static_initialization_or_destruction (vars, /*initp=*/false);
4091 else
4092 vars = NULL_TREE;
4094 /* Finish up the static storage duration function for this
4095 round. */
4096 input_location = locus;
4097 finish_static_storage_duration_function (ssdf_body);
4099 /* All those initializations and finalizations might cause
4100 us to need more inline functions, more template
4101 instantiations, etc. */
4102 reconsider = true;
4103 ssdf_count++;
4104 /* ??? was: locus.line++; */
4107 /* Now do the same for thread_local variables. */
4108 handle_tls_init ();
4110 /* Go through the set of inline functions whose bodies have not
4111 been emitted yet. If out-of-line copies of these functions
4112 are required, emit them. */
4113 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4115 /* Does it need synthesizing? */
4116 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4117 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4119 /* Even though we're already at the top-level, we push
4120 there again. That way, when we pop back a few lines
4121 hence, all of our state is restored. Otherwise,
4122 finish_function doesn't clean things up, and we end
4123 up with CURRENT_FUNCTION_DECL set. */
4124 push_to_top_level ();
4125 /* The decl's location will mark where it was first
4126 needed. Save that so synthesize method can indicate
4127 where it was needed from, in case of error */
4128 input_location = DECL_SOURCE_LOCATION (decl);
4129 synthesize_method (decl);
4130 pop_from_top_level ();
4131 reconsider = true;
4134 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4135 generate_tls_wrapper (decl);
4137 if (!DECL_SAVED_TREE (decl))
4138 continue;
4140 /* We lie to the back end, pretending that some functions
4141 are not defined when they really are. This keeps these
4142 functions from being put out unnecessarily. But, we must
4143 stop lying when the functions are referenced, or if they
4144 are not comdat since they need to be put out now. If
4145 DECL_INTERFACE_KNOWN, then we have already set
4146 DECL_EXTERNAL appropriately, so there's no need to check
4147 again, and we do not want to clear DECL_EXTERNAL if a
4148 previous call to import_export_decl set it.
4150 This is done in a separate for cycle, because if some
4151 deferred function is contained in another deferred
4152 function later in deferred_fns varray,
4153 rest_of_compilation would skip this function and we
4154 really cannot expand the same function twice. */
4155 import_export_decl (decl);
4156 if (DECL_NOT_REALLY_EXTERN (decl)
4157 && DECL_INITIAL (decl)
4158 && decl_needed_p (decl))
4160 struct cgraph_node *node, *next;
4162 node = cgraph_get_node (decl);
4163 if (node->same_body_alias)
4164 node = cgraph_alias_aliased_node (node);
4166 cgraph_for_node_and_aliases (node, clear_decl_external,
4167 NULL, true);
4168 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4169 group, we need to mark all symbols in the same comdat group
4170 that way. */
4171 if (node->symbol.same_comdat_group)
4172 for (next = cgraph (node->symbol.same_comdat_group);
4173 next != node;
4174 next = cgraph (next->symbol.same_comdat_group))
4175 cgraph_for_node_and_aliases (next, clear_decl_external,
4176 NULL, true);
4179 /* If we're going to need to write this function out, and
4180 there's already a body for it, create RTL for it now.
4181 (There might be no body if this is a method we haven't
4182 gotten around to synthesizing yet.) */
4183 if (!DECL_EXTERNAL (decl)
4184 && decl_needed_p (decl)
4185 && !TREE_ASM_WRITTEN (decl)
4186 && !cgraph_get_create_node (decl)->local.finalized)
4188 /* We will output the function; no longer consider it in this
4189 loop. */
4190 DECL_DEFER_OUTPUT (decl) = 0;
4191 /* Generate RTL for this function now that we know we
4192 need it. */
4193 expand_or_defer_fn (decl);
4194 /* If we're compiling -fsyntax-only pretend that this
4195 function has been written out so that we don't try to
4196 expand it again. */
4197 if (flag_syntax_only)
4198 TREE_ASM_WRITTEN (decl) = 1;
4199 reconsider = true;
4203 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4204 reconsider = true;
4206 /* Static data members are just like namespace-scope globals. */
4207 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4209 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4210 /* Don't write it out if we haven't seen a definition. */
4211 || DECL_IN_AGGR_P (decl))
4212 continue;
4213 import_export_decl (decl);
4214 /* If this static data member is needed, provide it to the
4215 back end. */
4216 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4217 DECL_EXTERNAL (decl) = 0;
4219 if (vec_safe_length (pending_statics) != 0
4220 && wrapup_global_declarations (pending_statics->address (),
4221 pending_statics->length ()))
4222 reconsider = true;
4224 retries++;
4226 while (reconsider);
4228 if (L_IPO_IS_AUXILIARY_MODULE)
4230 tree fndecl;
4231 int i;
4233 gcc_assert (flag_dyn_ipa && L_IPO_COMP_MODE);
4235 /* Do some cleanup -- we do not really need static init function
4236 to be created for auxiliary modules -- they are created to keep
4237 funcdef_no consistent between profile use and profile gen. */
4238 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4239 /* Such ssdf_decls are not called from GLOBAL ctor/dtor, mark
4240 them reachable to avoid being eliminated too early before
4241 gimplication. */
4242 cgraph_enqueue_node (cgraph_get_create_node (fndecl));
4243 ssdf_decls = NULL;
4244 timevar_stop (TV_PHASE_DEFERRED);
4245 return;
4248 /* All used inline functions must have a definition at this point. */
4249 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4251 if (/* Check online inline functions that were actually used. */
4252 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4253 /* If the definition actually was available here, then the
4254 fact that the function was not defined merely represents
4255 that for some reason (use of a template repository,
4256 #pragma interface, etc.) we decided not to emit the
4257 definition here. */
4258 && !DECL_INITIAL (decl)
4259 /* Don't complain if the template was defined. */
4260 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4261 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4262 (template_for_substitution (decl)))))
4264 warning (0, "inline function %q+D used but never defined", decl);
4265 /* Avoid a duplicate warning from check_global_declaration_1. */
4266 TREE_NO_WARNING (decl) = 1;
4270 /* So must decls that use a type with no linkage. */
4271 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4272 if (!decl_defined_p (decl))
4273 no_linkage_error (decl);
4275 /* Then, do the Objective-C stuff. This is where all the
4276 Objective-C module stuff gets generated (symtab,
4277 class/protocol/selector lists etc). This must be done after C++
4278 templates, destructors etc. so that selectors used in C++
4279 templates are properly allocated. */
4280 if (c_dialect_objc ())
4281 objc_write_global_declarations ();
4283 /* We give C linkage to static constructors and destructors. */
4284 push_lang_context (lang_name_c);
4286 /* Generate initialization and destruction functions for all
4287 priorities for which they are required. */
4288 if (priority_info_map)
4289 splay_tree_foreach (priority_info_map,
4290 generate_ctor_and_dtor_functions_for_priority,
4291 /*data=*/&locus);
4292 else if (c_dialect_objc () && objc_static_init_needed_p ())
4293 /* If this is obj-c++ and we need a static init, call
4294 generate_ctor_or_dtor_function. */
4295 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4296 DEFAULT_INIT_PRIORITY, &locus);
4298 /* We're done with the splay-tree now. */
4299 if (priority_info_map)
4301 splay_tree_delete (priority_info_map);
4302 priority_info_map = NULL;
4305 /* Generate any missing aliases. */
4306 maybe_apply_pending_pragma_weaks ();
4308 /* We're done with static constructors, so we can go back to "C++"
4309 linkage now. */
4310 pop_lang_context ();
4312 ssdf_decls = NULL;
4313 timevar_stop (TV_PHASE_DEFERRED);
4316 /* This routine is called at the end of compilation.
4317 Its job is to create all the code needed to initialize and
4318 destroy the global aggregates. We do the destruction
4319 first, since that way we only need to reverse the decls once. */
4321 void
4322 cp_write_global_declarations (void)
4324 bool reconsider = false;
4325 location_t locus;
4326 struct pointer_set_t *candidates;
4328 locus = input_location;
4329 at_eof = 1;
4331 /* Bad parse errors. Just forget about it. */
4332 if (! global_bindings_p () || current_class_type
4333 || !vec_safe_is_empty (decl_namespace_list))
4334 return;
4336 if (pch_file)
4338 c_common_write_pch ();
4339 return;
4342 cgraph_process_same_body_aliases ();
4344 /* Handle -fdump-ada-spec[-slim] */
4345 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4347 if (flag_dump_ada_spec_slim)
4348 collect_source_ref (main_input_filename);
4349 else
4350 collect_source_refs (global_namespace);
4352 dump_ada_specs (collect_all_refs, cpp_check);
4355 /* FIXME - huh? was input_line -= 1;*/
4357 /* We now have to write out all the stuff we put off writing out.
4358 These include:
4360 o Template specializations that we have not yet instantiated,
4361 but which are needed.
4362 o Initialization and destruction for non-local objects with
4363 static storage duration. (Local objects with static storage
4364 duration are initialized when their scope is first entered,
4365 and are cleaned up via atexit.)
4366 o Virtual function tables.
4368 All of these may cause others to be needed. For example,
4369 instantiating one function may cause another to be needed, and
4370 generating the initializer for an object may cause templates to be
4371 instantiated, etc., etc. */
4373 timevar_push (TV_VARCONST);
4375 emit_support_tinfos ();
4377 if (!L_IPO_COMP_MODE)
4378 cp_process_pending_declarations (locus);
4380 /* Collect candidates for Java hidden aliases. */
4381 candidates = collect_candidates_for_java_method_aliases ();
4384 timevar_start (TV_PHASE_OPT_GEN);
4386 finalize_compilation_unit ();
4388 timevar_stop (TV_PHASE_OPT_GEN);
4389 timevar_start (TV_PHASE_CHECK_DBGINFO);
4391 /* Now, issue warnings about static, but not defined, functions,
4392 etc., and emit debugging information. */
4393 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4394 if (vec_safe_length (pending_statics) != 0)
4396 check_global_declarations (pending_statics->address (),
4397 pending_statics->length ());
4398 emit_debug_global_declarations (pending_statics->address (),
4399 pending_statics->length ());
4402 perform_deferred_noexcept_checks ();
4404 /* Generate hidden aliases for Java. */
4405 if (candidates)
4407 build_java_method_aliases (candidates);
4408 pointer_set_destroy (candidates);
4411 finish_repo ();
4413 /* The entire file is now complete. If requested, dump everything
4414 to a file. */
4416 int flags;
4417 FILE *stream = dump_begin (TDI_tu, &flags);
4419 if (stream)
4421 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4422 dump_end (TDI_tu, stream);
4426 if (flag_detailed_statistics)
4428 dump_tree_statistics ();
4429 dump_time_statistics ();
4431 input_location = locus;
4433 #ifdef ENABLE_CHECKING
4434 validate_conversion_obstack ();
4435 #endif /* ENABLE_CHECKING */
4437 timevar_stop (TV_PHASE_CHECK_DBGINFO);
4440 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4441 function to call in parse-tree form; it has not yet been
4442 semantically analyzed. ARGS are the arguments to the function.
4443 They have already been semantically analyzed. This may change
4444 ARGS. */
4446 tree
4447 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4448 tsubst_flags_t complain)
4450 tree orig_fn;
4451 vec<tree, va_gc> *orig_args = NULL;
4452 tree expr;
4453 tree object;
4455 orig_fn = fn;
4456 object = TREE_OPERAND (fn, 0);
4458 if (processing_template_decl)
4460 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4461 || TREE_CODE (fn) == MEMBER_REF);
4462 if (type_dependent_expression_p (fn)
4463 || any_type_dependent_arguments_p (*args))
4464 return build_nt_call_vec (fn, *args);
4466 orig_args = make_tree_vector_copy (*args);
4468 /* Transform the arguments and add the implicit "this"
4469 parameter. That must be done before the FN is transformed
4470 because we depend on the form of FN. */
4471 make_args_non_dependent (*args);
4472 object = build_non_dependent_expr (object);
4473 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4475 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4476 object = cp_build_addr_expr (object, complain);
4477 vec_safe_insert (*args, 0, object);
4479 /* Now that the arguments are done, transform FN. */
4480 fn = build_non_dependent_expr (fn);
4483 /* A qualified name corresponding to a bound pointer-to-member is
4484 represented as an OFFSET_REF:
4486 struct B { void g(); };
4487 void (B::*p)();
4488 void B::g() { (this->*p)(); } */
4489 if (TREE_CODE (fn) == OFFSET_REF)
4491 tree object_addr = cp_build_addr_expr (object, complain);
4492 fn = TREE_OPERAND (fn, 1);
4493 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4494 complain);
4495 vec_safe_insert (*args, 0, object_addr);
4498 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4499 expr = build_op_call (fn, args, complain);
4500 else
4501 expr = cp_build_function_call_vec (fn, args, complain);
4502 if (processing_template_decl && expr != error_mark_node)
4503 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4505 if (orig_args != NULL)
4506 release_tree_vector (orig_args);
4508 return expr;
4512 void
4513 check_default_args (tree x)
4515 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4516 bool saw_def = false;
4517 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4518 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4520 if (TREE_PURPOSE (arg))
4521 saw_def = true;
4522 else if (saw_def)
4524 error ("default argument missing for parameter %P of %q+#D", i, x);
4525 TREE_PURPOSE (arg) = error_mark_node;
4530 /* Return true if function DECL can be inlined. This is used to force
4531 instantiation of methods that might be interesting for inlining. */
4532 bool
4533 possibly_inlined_p (tree decl)
4535 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4536 if (DECL_UNINLINABLE (decl))
4537 return false;
4538 if (!optimize || pragma_java_exceptions)
4539 return DECL_DECLARED_INLINE_P (decl);
4540 /* When optimizing, we might inline everything when flatten
4541 attribute or heuristics inlining for size or autoinlining
4542 is used. */
4543 return true;
4546 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4547 If DECL is a specialization or implicitly declared class member,
4548 generate the actual definition. Return false if something goes
4549 wrong, true otherwise. */
4551 bool
4552 mark_used (tree decl)
4554 /* If DECL is a BASELINK for a single function, then treat it just
4555 like the DECL for the function. Otherwise, if the BASELINK is
4556 for an overloaded function, we don't know which function was
4557 actually used until after overload resolution. */
4558 if (BASELINK_P (decl))
4560 decl = BASELINK_FUNCTIONS (decl);
4561 if (really_overloaded_fn (decl))
4562 return true;
4563 decl = OVL_CURRENT (decl);
4566 /* Set TREE_USED for the benefit of -Wunused. */
4567 TREE_USED (decl) = 1;
4568 if (DECL_CLONED_FUNCTION_P (decl))
4569 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4571 /* Mark enumeration types as used. */
4572 if (TREE_CODE (decl) == CONST_DECL)
4573 used_types_insert (DECL_CONTEXT (decl));
4575 if (TREE_CODE (decl) == FUNCTION_DECL
4576 && DECL_DELETED_FN (decl))
4578 if (DECL_ARTIFICIAL (decl))
4580 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4581 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4583 /* We mark a lambda conversion op as deleted if we can't
4584 generate it properly; see maybe_add_lambda_conv_op. */
4585 sorry ("converting lambda which uses %<...%> to "
4586 "function pointer");
4587 return false;
4590 error ("use of deleted function %qD", decl);
4591 if (!maybe_explain_implicit_delete (decl))
4592 error_at (DECL_SOURCE_LOCATION (decl), "declared here");
4593 return false;
4596 /* We can only check DECL_ODR_USED on variables or functions with
4597 DECL_LANG_SPECIFIC set, and these are also the only decls that we
4598 might need special handling for. */
4599 if ((TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
4600 || DECL_LANG_SPECIFIC (decl) == NULL
4601 || DECL_THUNK_P (decl))
4603 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4605 error ("use of %qD before deduction of %<auto%>", decl);
4606 return false;
4608 return true;
4611 /* We only want to do this processing once. We don't need to keep trying
4612 to instantiate inline templates, because unit-at-a-time will make sure
4613 we get them compiled before functions that want to inline them. */
4614 if (DECL_ODR_USED (decl))
4615 return true;
4617 /* If within finish_function, defer the rest until that function
4618 finishes, otherwise it might recurse. */
4619 if (defer_mark_used_calls)
4621 vec_safe_push (deferred_mark_used_calls, decl);
4622 return true;
4625 if (TREE_CODE (decl) == FUNCTION_DECL)
4626 maybe_instantiate_noexcept (decl);
4628 /* Normally, we can wait until instantiation-time to synthesize DECL.
4629 However, if DECL is a static data member initialized with a constant
4630 or a constexpr function, we need it right now because a reference to
4631 such a data member or a call to such function is not value-dependent.
4632 For a function that uses auto in the return type, we need to instantiate
4633 it to find out its type. */
4634 if ((decl_maybe_constant_var_p (decl)
4635 || (TREE_CODE (decl) == FUNCTION_DECL
4636 && DECL_DECLARED_CONSTEXPR_P (decl))
4637 || type_uses_auto (TREE_TYPE (decl)))
4638 && DECL_LANG_SPECIFIC (decl)
4639 && DECL_TEMPLATE_INFO (decl)
4640 && !uses_template_parms (DECL_TI_ARGS (decl)))
4642 /* Instantiating a function will result in garbage collection. We
4643 must treat this situation as if we were within the body of a
4644 function so as to avoid collecting live data only referenced from
4645 the stack (such as overload resolution candidates). */
4646 ++function_depth;
4647 instantiate_decl (decl, /*defer_ok=*/false,
4648 /*expl_inst_class_mem_p=*/false);
4649 --function_depth;
4652 if (processing_template_decl)
4653 return true;
4655 /* Check this too in case we're within fold_non_dependent_expr. */
4656 if (DECL_TEMPLATE_INFO (decl)
4657 && uses_template_parms (DECL_TI_ARGS (decl)))
4658 return true;
4660 if (type_uses_auto (TREE_TYPE (decl)))
4662 error ("use of %qD before deduction of %<auto%>", decl);
4663 return false;
4666 /* If we don't need a value, then we don't need to synthesize DECL. */
4667 if (cp_unevaluated_operand != 0)
4668 return true;
4670 DECL_ODR_USED (decl) = 1;
4671 if (DECL_CLONED_FUNCTION_P (decl))
4672 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4674 /* DR 757: A type without linkage shall not be used as the type of a
4675 variable or function with linkage, unless
4676 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4677 o the variable or function is not used (3.2 [basic.def.odr]) or is
4678 defined in the same translation unit. */
4679 if (cxx_dialect > cxx98
4680 && decl_linkage (decl) != lk_none
4681 && !DECL_EXTERN_C_P (decl)
4682 && !DECL_ARTIFICIAL (decl)
4683 && !decl_defined_p (decl)
4684 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4686 if (is_local_extern (decl))
4687 /* There's no way to define a local extern, and adding it to
4688 the vector interferes with GC, so give an error now. */
4689 no_linkage_error (decl);
4690 else
4691 vec_safe_push (no_linkage_decls, decl);
4694 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4695 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4696 /* Remember it, so we can check it was defined. */
4697 note_vague_linkage_fn (decl);
4699 /* Is it a synthesized method that needs to be synthesized? */
4700 if (TREE_CODE (decl) == FUNCTION_DECL
4701 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4702 && DECL_DEFAULTED_FN (decl)
4703 /* A function defaulted outside the class is synthesized either by
4704 cp_finish_decl or instantiate_decl. */
4705 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
4706 && ! DECL_INITIAL (decl))
4708 /* Defer virtual destructors so that thunks get the right
4709 linkage. */
4710 if (DECL_VIRTUAL_P (decl) && !at_eof)
4712 note_vague_linkage_fn (decl);
4713 return true;
4716 /* Remember the current location for a function we will end up
4717 synthesizing. Then we can inform the user where it was
4718 required in the case of error. */
4719 DECL_SOURCE_LOCATION (decl) = input_location;
4721 /* Synthesizing an implicitly defined member function will result in
4722 garbage collection. We must treat this situation as if we were
4723 within the body of a function so as to avoid collecting live data
4724 on the stack (such as overload resolution candidates).
4726 We could just let cp_write_global_declarations handle synthesizing
4727 this function by adding it to deferred_fns, but doing
4728 it at the use site produces better error messages. */
4729 ++function_depth;
4730 synthesize_method (decl);
4731 --function_depth;
4732 /* If this is a synthesized method we don't need to
4733 do the instantiation test below. */
4735 else if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4736 && DECL_TEMPLATE_INFO (decl)
4737 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4738 || always_instantiate_p (decl)))
4739 /* If this is a function or variable that is an instance of some
4740 template, we now know that we will need to actually do the
4741 instantiation. We check that DECL is not an explicit
4742 instantiation because that is not checked in instantiate_decl.
4744 We put off instantiating functions in order to improve compile
4745 times. Maintaining a stack of active functions is expensive,
4746 and the inliner knows to instantiate any functions it might
4747 need. Therefore, we always try to defer instantiation. */
4749 ++function_depth;
4750 instantiate_decl (decl, /*defer_ok=*/true,
4751 /*expl_inst_class_mem_p=*/false);
4752 --function_depth;
4755 return true;
4758 #include "gt-cp-decl2.h"