117dbd03c48e2888f89f6add1275b0a804f10e03
[dragonfly.git] / contrib / gcc-5.0 / gcc / cp / decl2.c
1 /* Process declarations and variables for C++ compiler.
2    Copyright (C) 1988-2015 Free Software Foundation, Inc.
3    Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
21
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.  */
25
26 /* ??? not all decl nodes are given the most useful possible
27    line numbers.  For example, the CONST_DECLs for enum values.  */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "stringpool.h"
44 #include "varasm.h"
45 #include "attribs.h"
46 #include "stor-layout.h"
47 #include "calls.h"
48 #include "flags.h"
49 #include "cp-tree.h"
50 #include "decl.h"
51 #include "toplev.h"
52 #include "timevar.h"
53 #include "cpplib.h"
54 #include "target.h"
55 #include "c-family/c-common.h"
56 #include "c-family/c-objc.h"
57 #include "hash-map.h"
58 #include "is-a.h"
59 #include "plugin-api.h"
60 #include "hard-reg-set.h"
61 #include "input.h"
62 #include "function.h"
63 #include "ipa-ref.h"
64 #include "cgraph.h"
65 #include "tree-inline.h"
66 #include "c-family/c-pragma.h"
67 #include "dumpfile.h"
68 #include "intl.h"
69 #include "splay-tree.h"
70 #include "langhooks.h"
71 #include "c-family/c-ada-spec.h"
72 #include "asan.h"
73
74 extern cpp_reader *parse_in;
75
76 /* This structure contains information about the initializations
77    and/or destructions required for a particular priority level.  */
78 typedef struct priority_info_s {
79   /* Nonzero if there have been any initializations at this priority
80      throughout the translation unit.  */
81   int initializations_p;
82   /* Nonzero if there have been any destructions at this priority
83      throughout the translation unit.  */
84   int destructions_p;
85 } *priority_info;
86
87 static void mark_vtable_entries (tree);
88 static bool maybe_emit_vtables (tree);
89 static bool acceptable_java_type (tree);
90 static tree start_objects (int, int);
91 static void finish_objects (int, int, tree);
92 static tree start_static_storage_duration_function (unsigned);
93 static void finish_static_storage_duration_function (tree);
94 static priority_info get_priority_info (int);
95 static void do_static_initialization_or_destruction (tree, bool);
96 static void one_static_initialization_or_destruction (tree, tree, bool);
97 static void generate_ctor_or_dtor_function (bool, int, location_t *);
98 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
99                                                           void *);
100 static tree prune_vars_needing_no_initialization (tree *);
101 static void write_out_vars (tree);
102 static void import_export_class (tree);
103 static tree get_guard_bits (tree);
104 static void determine_visibility_from_class (tree, tree);
105 static bool determine_hidden_inline (tree);
106 static bool decl_defined_p (tree);
107
108 /* A list of static class variables.  This is needed, because a
109    static class variable can be declared inside the class without
110    an initializer, and then initialized, statically, outside the class.  */
111 static GTY(()) vec<tree, va_gc> *pending_statics;
112
113 /* A list of functions which were declared inline, but which we
114    may need to emit outline anyway.  */
115 static GTY(()) vec<tree, va_gc> *deferred_fns;
116
117 /* A list of decls that use types with no linkage, which we need to make
118    sure are defined.  */
119 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
120
121 /* Nonzero if we're done parsing and into end-of-file activities.  */
122
123 int at_eof;
124 \f
125
126 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
127    FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
128    that apply to the function).  */
129
130 tree
131 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
132                   cp_ref_qualifier rqual)
133 {
134   tree raises;
135   tree attrs;
136   int type_quals;
137   bool late_return_type_p;
138
139   if (fntype == error_mark_node || ctype == error_mark_node)
140     return error_mark_node;
141
142   gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
143               || TREE_CODE (fntype) == METHOD_TYPE);
144
145   type_quals = quals & ~TYPE_QUAL_RESTRICT;
146   ctype = cp_build_qualified_type (ctype, type_quals);
147   raises = TYPE_RAISES_EXCEPTIONS (fntype);
148   attrs = TYPE_ATTRIBUTES (fntype);
149   late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
150   fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
151                                        (TREE_CODE (fntype) == METHOD_TYPE
152                                         ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
153                                         : TYPE_ARG_TYPES (fntype)));
154   if (attrs)
155     fntype = cp_build_type_attribute_variant (fntype, attrs);
156   if (rqual)
157     fntype = build_ref_qualified_type (fntype, rqual);
158   if (raises)
159     fntype = build_exception_variant (fntype, raises);
160   if (late_return_type_p)
161     TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
162
163   return fntype;
164 }
165
166 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
167    return type changed to NEW_RET.  */
168
169 tree
170 change_return_type (tree new_ret, tree fntype)
171 {
172   tree newtype;
173   tree args = TYPE_ARG_TYPES (fntype);
174   tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
175   tree attrs = TYPE_ATTRIBUTES (fntype);
176   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
177
178   if (new_ret == error_mark_node)
179     return fntype;
180
181   if (same_type_p (new_ret, TREE_TYPE (fntype)))
182     return fntype;
183
184   if (TREE_CODE (fntype) == FUNCTION_TYPE)
185     {
186       newtype = build_function_type (new_ret, args);
187       newtype = apply_memfn_quals (newtype,
188                                    type_memfn_quals (fntype),
189                                    type_memfn_rqual (fntype));
190     }
191   else
192     newtype = build_method_type_directly
193       (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
194   if (raises)
195     newtype = build_exception_variant (newtype, raises);
196   if (attrs)
197     newtype = cp_build_type_attribute_variant (newtype, attrs);
198   if (late_return_type_p)
199     TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
200
201   return newtype;
202 }
203
204 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
205    appropriately.  */
206
207 tree
208 cp_build_parm_decl (tree name, tree type)
209 {
210   tree parm = build_decl (input_location,
211                           PARM_DECL, name, type);
212   /* DECL_ARG_TYPE is only used by the back end and the back end never
213      sees templates.  */
214   if (!processing_template_decl)
215     DECL_ARG_TYPE (parm) = type_passed_as (type);
216
217   return parm;
218 }
219
220 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
221    indicated NAME.  */
222
223 tree
224 build_artificial_parm (tree name, tree type)
225 {
226   tree parm = cp_build_parm_decl (name, type);
227   DECL_ARTIFICIAL (parm) = 1;
228   /* All our artificial parms are implicitly `const'; they cannot be
229      assigned to.  */
230   TREE_READONLY (parm) = 1;
231   return parm;
232 }
233
234 /* Constructors for types with virtual baseclasses need an "in-charge" flag
235    saying whether this constructor is responsible for initialization of
236    virtual baseclasses or not.  All destructors also need this "in-charge"
237    flag, which additionally determines whether or not the destructor should
238    free the memory for the object.
239
240    This function adds the "in-charge" flag to member function FN if
241    appropriate.  It is called from grokclassfn and tsubst.
242    FN must be either a constructor or destructor.
243
244    The in-charge flag follows the 'this' parameter, and is followed by the
245    VTT parm (if any), then the user-written parms.  */
246
247 void
248 maybe_retrofit_in_chrg (tree fn)
249 {
250   tree basetype, arg_types, parms, parm, fntype;
251
252   /* If we've already add the in-charge parameter don't do it again.  */
253   if (DECL_HAS_IN_CHARGE_PARM_P (fn))
254     return;
255
256   /* When processing templates we can't know, in general, whether or
257      not we're going to have virtual baseclasses.  */
258   if (processing_template_decl)
259     return;
260
261   /* We don't need an in-charge parameter for constructors that don't
262      have virtual bases.  */
263   if (DECL_CONSTRUCTOR_P (fn)
264       && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
265     return;
266
267   arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
268   basetype = TREE_TYPE (TREE_VALUE (arg_types));
269   arg_types = TREE_CHAIN (arg_types);
270
271   parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
272
273   /* If this is a subobject constructor or destructor, our caller will
274      pass us a pointer to our VTT.  */
275   if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
276     {
277       parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
278
279       /* First add it to DECL_ARGUMENTS between 'this' and the real args...  */
280       DECL_CHAIN (parm) = parms;
281       parms = parm;
282
283       /* ...and then to TYPE_ARG_TYPES.  */
284       arg_types = hash_tree_chain (vtt_parm_type, arg_types);
285
286       DECL_HAS_VTT_PARM_P (fn) = 1;
287     }
288
289   /* Then add the in-charge parm (before the VTT parm).  */
290   parm = build_artificial_parm (in_charge_identifier, integer_type_node);
291   DECL_CHAIN (parm) = parms;
292   parms = parm;
293   arg_types = hash_tree_chain (integer_type_node, arg_types);
294
295   /* Insert our new parameter(s) into the list.  */
296   DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
297
298   /* And rebuild the function type.  */
299   fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
300                                        arg_types);
301   if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
302     fntype = build_exception_variant (fntype,
303                                       TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
304   if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
305     fntype = (cp_build_type_attribute_variant
306               (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
307   TREE_TYPE (fn) = fntype;
308
309   /* Now we've got the in-charge parameter.  */
310   DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
311 }
312
313 /* Classes overload their constituent function names automatically.
314    When a function name is declared in a record structure,
315    its name is changed to it overloaded name.  Since names for
316    constructors and destructors can conflict, we place a leading
317    '$' for destructors.
318
319    CNAME is the name of the class we are grokking for.
320
321    FUNCTION is a FUNCTION_DECL.  It was created by `grokdeclarator'.
322
323    FLAGS contains bits saying what's special about today's
324    arguments.  DTOR_FLAG == DESTRUCTOR.
325
326    If FUNCTION is a destructor, then we must add the `auto-delete' field
327    as a second parameter.  There is some hair associated with the fact
328    that we must "declare" this variable in the manner consistent with the
329    way the rest of the arguments were declared.
330
331    QUALS are the qualifiers for the this pointer.  */
332
333 void
334 grokclassfn (tree ctype, tree function, enum overload_flags flags)
335 {
336   tree fn_name = DECL_NAME (function);
337
338   /* Even within an `extern "C"' block, members get C++ linkage.  See
339      [dcl.link] for details.  */
340   SET_DECL_LANGUAGE (function, lang_cplusplus);
341
342   if (fn_name == NULL_TREE)
343     {
344       error ("name missing for member function");
345       fn_name = get_identifier ("<anonymous>");
346       DECL_NAME (function) = fn_name;
347     }
348
349   DECL_CONTEXT (function) = ctype;
350
351   if (flags == DTOR_FLAG)
352     DECL_DESTRUCTOR_P (function) = 1;
353
354   if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
355     maybe_retrofit_in_chrg (function);
356 }
357
358 /* Create an ARRAY_REF, checking for the user doing things backwards
359    along the way.  DECLTYPE_P is for N3276, as in the parser.  */
360
361 tree
362 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
363                  bool decltype_p)
364 {
365   tree type;
366   tree expr;
367   tree orig_array_expr = array_expr;
368   tree orig_index_exp = index_exp;
369
370   if (error_operand_p (array_expr) || error_operand_p (index_exp))
371     return error_mark_node;
372
373   if (processing_template_decl)
374     {
375       if (type_dependent_expression_p (array_expr)
376           || type_dependent_expression_p (index_exp))
377         return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
378                                  NULL_TREE, NULL_TREE);
379       array_expr = build_non_dependent_expr (array_expr);
380       index_exp = build_non_dependent_expr (index_exp);
381     }
382
383   type = TREE_TYPE (array_expr);
384   gcc_assert (type);
385   type = non_reference (type);
386
387   /* If they have an `operator[]', use that.  */
388   if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
389     {
390       tsubst_flags_t complain = tf_warning_or_error;
391       if (decltype_p)
392         complain |= tf_decltype;
393       expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
394                            index_exp, NULL_TREE, /*overload=*/NULL, complain);
395     }
396   else
397     {
398       tree p1, p2, i1, i2;
399
400       /* Otherwise, create an ARRAY_REF for a pointer or array type.
401          It is a little-known fact that, if `a' is an array and `i' is
402          an int, you can write `i[a]', which means the same thing as
403          `a[i]'.  */
404       if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
405         p1 = array_expr;
406       else
407         p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
408
409       if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
410         p2 = index_exp;
411       else
412         p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
413
414       i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
415                                        false);
416       i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
417                                        false);
418
419       if ((p1 && i2) && (i1 && p2))
420         error ("ambiguous conversion for array subscript");
421
422       if (p1 && i2)
423         array_expr = p1, index_exp = i2;
424       else if (i1 && p2)
425         array_expr = p2, index_exp = i1;
426       else
427         {
428           error ("invalid types %<%T[%T]%> for array subscript",
429                  type, TREE_TYPE (index_exp));
430           return error_mark_node;
431         }
432
433       if (array_expr == error_mark_node || index_exp == error_mark_node)
434         error ("ambiguous conversion for array subscript");
435
436       expr = build_array_ref (input_location, array_expr, index_exp);
437     }
438   if (processing_template_decl && expr != error_mark_node)
439     return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
440                               NULL_TREE, NULL_TREE);
441   return expr;
442 }
443
444 /* Given the cast expression EXP, checking out its validity.   Either return
445    an error_mark_node if there was an unavoidable error, return a cast to
446    void for trying to delete a pointer w/ the value 0, or return the
447    call to delete.  If DOING_VEC is true, we handle things differently
448    for doing an array delete.
449    Implements ARM $5.3.4.  This is called from the parser.  */
450
451 tree
452 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
453                tsubst_flags_t complain)
454 {
455   tree t, type;
456
457   if (exp == error_mark_node)
458     return exp;
459
460   if (processing_template_decl)
461     {
462       t = build_min (DELETE_EXPR, void_type_node, exp, size);
463       DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
464       DELETE_EXPR_USE_VEC (t) = doing_vec;
465       TREE_SIDE_EFFECTS (t) = 1;
466       return t;
467     }
468
469   /* An array can't have been allocated by new, so complain.  */
470   if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
471     warning (0, "deleting array %q#E", exp);
472
473   t = build_expr_type_conversion (WANT_POINTER, exp, true);
474
475   if (t == NULL_TREE || t == error_mark_node)
476     {
477       error ("type %q#T argument given to %<delete%>, expected pointer",
478              TREE_TYPE (exp));
479       return error_mark_node;
480     }
481
482   type = TREE_TYPE (t);
483
484   /* As of Valley Forge, you can delete a pointer to const.  */
485
486   /* You can't delete functions.  */
487   if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
488     {
489       error ("cannot delete a function.  Only pointer-to-objects are "
490              "valid arguments to %<delete%>");
491       return error_mark_node;
492     }
493
494   /* Deleting ptr to void is undefined behavior [expr.delete/3].  */
495   if (VOID_TYPE_P (TREE_TYPE (type)))
496     {
497       warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
498       doing_vec = 0;
499     }
500
501   /* Deleting a pointer with the value zero is valid and has no effect.  */
502   if (integer_zerop (t))
503     return build1 (NOP_EXPR, void_type_node, t);
504
505   if (doing_vec)
506     return build_vec_delete (t, /*maxindex=*/NULL_TREE,
507                              sfk_deleting_destructor,
508                              use_global_delete, complain);
509   else
510     return build_delete (type, t, sfk_deleting_destructor,
511                          LOOKUP_NORMAL, use_global_delete,
512                          complain);
513 }
514
515 /* Report an error if the indicated template declaration is not the
516    sort of thing that should be a member template.  */
517
518 void
519 check_member_template (tree tmpl)
520 {
521   tree decl;
522
523   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
524   decl = DECL_TEMPLATE_RESULT (tmpl);
525
526   if (TREE_CODE (decl) == FUNCTION_DECL
527       || DECL_ALIAS_TEMPLATE_P (tmpl)
528       || (TREE_CODE (decl) == TYPE_DECL
529           && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
530     {
531       /* The parser rejects template declarations in local classes
532          (with the exception of generic lambdas).  */
533       gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
534       /* The parser rejects any use of virtual in a function template.  */
535       gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
536                     && DECL_VIRTUAL_P (decl)));
537
538       /* The debug-information generating code doesn't know what to do
539          with member templates.  */
540       DECL_IGNORED_P (tmpl) = 1;
541     }
542   else if (variable_template_p (tmpl))
543     /* OK */;
544   else
545     error ("template declaration of %q#D", decl);
546 }
547
548 /* Return true iff TYPE is a valid Java parameter or return type.  */
549
550 static bool
551 acceptable_java_type (tree type)
552 {
553   if (type == error_mark_node)
554     return false;
555
556   if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
557     return true;
558   if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
559     {
560       type = TREE_TYPE (type);
561       if (TREE_CODE (type) == RECORD_TYPE)
562         {
563           tree args;  int i;
564           if (! TYPE_FOR_JAVA (type))
565             return false;
566           if (! CLASSTYPE_TEMPLATE_INFO (type))
567             return true;
568           args = CLASSTYPE_TI_ARGS (type);
569           i = TREE_VEC_LENGTH (args);
570           while (--i >= 0)
571             {
572               type = TREE_VEC_ELT (args, i);
573               if (TYPE_PTR_P (type))
574                 type = TREE_TYPE (type);
575               if (! TYPE_FOR_JAVA (type))
576                 return false;
577             }
578           return true;
579         }
580     }
581   return false;
582 }
583
584 /* For a METHOD in a Java class CTYPE, return true if
585    the parameter and return types are valid Java types.
586    Otherwise, print appropriate error messages, and return false.  */
587
588 bool
589 check_java_method (tree method)
590 {
591   bool jerr = false;
592   tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
593   tree ret_type = TREE_TYPE (TREE_TYPE (method));
594
595   if (!acceptable_java_type (ret_type))
596     {
597       error ("Java method %qD has non-Java return type %qT",
598              method, ret_type);
599       jerr = true;
600     }
601
602   arg_types = TREE_CHAIN (arg_types);
603   if (DECL_HAS_IN_CHARGE_PARM_P (method))
604     arg_types = TREE_CHAIN (arg_types);
605   if (DECL_HAS_VTT_PARM_P (method))
606     arg_types = TREE_CHAIN (arg_types);
607
608   for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
609     {
610       tree type = TREE_VALUE (arg_types);
611       if (!acceptable_java_type (type))
612         {
613           if (type != error_mark_node)
614             error ("Java method %qD has non-Java parameter type %qT",
615                    method, type);
616           jerr = true;
617         }
618     }
619   return !jerr;
620 }
621
622 /* Sanity check: report error if this function FUNCTION is not
623    really a member of the class (CTYPE) it is supposed to belong to.
624    TEMPLATE_PARMS is used to specify the template parameters of a member
625    template passed as FUNCTION_DECL. If the member template is passed as a
626    TEMPLATE_DECL, it can be NULL since the parameters can be extracted
627    from the declaration. If the function is not a function template, it
628    must be NULL.
629    It returns the original declaration for the function, NULL_TREE if
630    no declaration was found, error_mark_node if an error was emitted.  */
631
632 tree
633 check_classfn (tree ctype, tree function, tree template_parms)
634 {
635   int ix;
636   bool is_template;
637   tree pushed_scope;
638   
639   if (DECL_USE_TEMPLATE (function)
640       && !(TREE_CODE (function) == TEMPLATE_DECL
641            && DECL_TEMPLATE_SPECIALIZATION (function))
642       && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
643     /* Since this is a specialization of a member template,
644        we're not going to find the declaration in the class.
645        For example, in:
646
647          struct S { template <typename T> void f(T); };
648          template <> void S::f(int);
649
650        we're not going to find `S::f(int)', but there's no
651        reason we should, either.  We let our callers know we didn't
652        find the method, but we don't complain.  */
653     return NULL_TREE;
654
655   /* Basic sanity check: for a template function, the template parameters
656      either were not passed, or they are the same of DECL_TEMPLATE_PARMS.  */
657   if (TREE_CODE (function) == TEMPLATE_DECL)
658     {
659       if (template_parms
660           && !comp_template_parms (template_parms,
661                                    DECL_TEMPLATE_PARMS (function)))
662         {
663           error ("template parameter lists provided don%'t match the "
664                  "template parameters of %qD", function);
665           return error_mark_node;
666         }
667       template_parms = DECL_TEMPLATE_PARMS (function);
668     }
669
670   /* OK, is this a definition of a member template?  */
671   is_template = (template_parms != NULL_TREE);
672
673   /* [temp.mem]
674
675      A destructor shall not be a member template.  */
676   if (DECL_DESTRUCTOR_P (function) && is_template)
677     {
678       error ("destructor %qD declared as member template", function);
679       return error_mark_node;
680     }
681
682   /* We must enter the scope here, because conversion operators are
683      named by target type, and type equivalence relies on typenames
684      resolving within the scope of CTYPE.  */
685   pushed_scope = push_scope (ctype);
686   ix = class_method_index_for_fn (complete_type (ctype), function);
687   if (ix >= 0)
688     {
689       vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
690       tree fndecls, fndecl = 0;
691       bool is_conv_op;
692       const char *format = NULL;
693
694       for (fndecls = (*methods)[ix];
695            fndecls; fndecls = OVL_NEXT (fndecls))
696         {
697           tree p1, p2;
698
699           fndecl = OVL_CURRENT (fndecls);
700           p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
701           p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
702
703           /* We cannot simply call decls_match because this doesn't
704              work for static member functions that are pretending to
705              be methods, and because the name may have been changed by
706              asm("new_name").  */
707
708            /* Get rid of the this parameter on functions that become
709               static.  */
710           if (DECL_STATIC_FUNCTION_P (fndecl)
711               && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
712             p1 = TREE_CHAIN (p1);
713
714           /* A member template definition only matches a member template
715              declaration.  */
716           if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
717             continue;
718
719           /* ref-qualifier or absence of same must match.  */
720           if (type_memfn_rqual (TREE_TYPE (function))
721               != type_memfn_rqual (TREE_TYPE (fndecl)))
722             continue;
723
724           /* While finding a match, same types and params are not enough
725              if the function is versioned.  Also check version ("target")
726              attributes.  */
727           if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
728                            TREE_TYPE (TREE_TYPE (fndecl)))
729               && compparms (p1, p2)
730               && !targetm.target_option.function_versions (function, fndecl)
731               && (!is_template
732                   || comp_template_parms (template_parms,
733                                           DECL_TEMPLATE_PARMS (fndecl)))
734               && (DECL_TEMPLATE_SPECIALIZATION (function)
735                   == DECL_TEMPLATE_SPECIALIZATION (fndecl))
736               && (!DECL_TEMPLATE_SPECIALIZATION (function)
737                   || (DECL_TI_TEMPLATE (function)
738                       == DECL_TI_TEMPLATE (fndecl))))
739             break;
740         }
741       if (fndecls)
742         {
743           if (pushed_scope)
744             pop_scope (pushed_scope);
745           return OVL_CURRENT (fndecls);
746         }
747       
748       error_at (DECL_SOURCE_LOCATION (function),
749                 "prototype for %q#D does not match any in class %qT",
750                 function, ctype);
751       is_conv_op = DECL_CONV_FN_P (fndecl);
752
753       if (is_conv_op)
754         ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
755       fndecls = (*methods)[ix];
756       while (fndecls)
757         {
758           fndecl = OVL_CURRENT (fndecls);
759           fndecls = OVL_NEXT (fndecls);
760
761           if (!fndecls && is_conv_op)
762             {
763               if (methods->length () > (size_t) ++ix)
764                 {
765                   fndecls = (*methods)[ix];
766                   if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
767                     {
768                       fndecls = NULL_TREE;
769                       is_conv_op = false;
770                     }
771                 }
772               else
773                 is_conv_op = false;
774             }
775           if (format)
776             format = "                %+#D";
777           else if (fndecls)
778             format = N_("candidates are: %+#D");
779           else
780             format = N_("candidate is: %+#D");
781           error (format, fndecl);
782         }
783     }
784   else if (!COMPLETE_TYPE_P (ctype))
785     cxx_incomplete_type_error (function, ctype);
786   else
787     error ("no %q#D member function declared in class %qT",
788            function, ctype);
789
790   if (pushed_scope)
791     pop_scope (pushed_scope);
792   return error_mark_node;
793 }
794
795 /* DECL is a function with vague linkage.  Remember it so that at the
796    end of the translation unit we can decide whether or not to emit
797    it.  */
798
799 void
800 note_vague_linkage_fn (tree decl)
801 {
802   DECL_DEFER_OUTPUT (decl) = 1;
803   vec_safe_push (deferred_fns, decl);
804 }
805
806 /* As above, but for variable template instantiations.  */
807
808 void
809 note_variable_template_instantiation (tree decl)
810 {
811   vec_safe_push (pending_statics, decl);
812 }
813
814 /* We have just processed the DECL, which is a static data member.
815    The other parameters are as for cp_finish_decl.  */
816
817 void
818 finish_static_data_member_decl (tree decl,
819                                 tree init, bool init_const_expr_p,
820                                 tree asmspec_tree,
821                                 int flags)
822 {
823   DECL_CONTEXT (decl) = current_class_type;
824
825   /* We cannot call pushdecl here, because that would fill in the
826      TREE_CHAIN of our decl.  Instead, we modify cp_finish_decl to do
827      the right thing, namely, to put this decl out straight away.  */
828
829   if (! processing_template_decl)
830     vec_safe_push (pending_statics, decl);
831
832   if (LOCAL_CLASS_P (current_class_type)
833       /* We already complained about the template definition.  */
834       && !DECL_TEMPLATE_INSTANTIATION (decl))
835     permerror (input_location, "local class %q#T shall not have static data member %q#D",
836                current_class_type, decl);
837   else
838     for (tree t = current_class_type; TYPE_P (t);
839          t = CP_TYPE_CONTEXT (t))
840       if (TYPE_ANONYMOUS_P (t))
841         {
842           if (permerror (DECL_SOURCE_LOCATION (decl),
843                          "static data member %qD in unnamed class", decl))
844             inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
845                     "unnamed class defined here");
846           break;
847         }
848
849   DECL_IN_AGGR_P (decl) = 1;
850
851   if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
852       && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
853     SET_VAR_HAD_UNKNOWN_BOUND (decl);
854
855   cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
856 }
857
858 /* DECLARATOR and DECLSPECS correspond to a class member.  The other
859    parameters are as for cp_finish_decl.  Return the DECL for the
860    class member declared.  */
861
862 tree
863 grokfield (const cp_declarator *declarator,
864            cp_decl_specifier_seq *declspecs,
865            tree init, bool init_const_expr_p,
866            tree asmspec_tree,
867            tree attrlist)
868 {
869   tree value;
870   const char *asmspec = 0;
871   int flags;
872   tree name;
873
874   if (init
875       && TREE_CODE (init) == TREE_LIST
876       && TREE_VALUE (init) == error_mark_node
877       && TREE_CHAIN (init) == NULL_TREE)
878     init = NULL_TREE;
879
880   value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
881   if (! value || value == error_mark_node)
882     /* friend or constructor went bad.  */
883     return error_mark_node;
884   if (TREE_TYPE (value) == error_mark_node)
885     return value;
886
887   if (TREE_CODE (value) == TYPE_DECL && init)
888     {
889       error ("typedef %qD is initialized (use decltype instead)", value);
890       init = NULL_TREE;
891     }
892
893   /* Pass friendly classes back.  */
894   if (value == void_type_node)
895     return value;
896
897
898   name = DECL_NAME (value);
899
900   if (name != NULL_TREE)
901     {
902       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
903         {
904           error ("explicit template argument list not allowed");
905           return error_mark_node;
906         }
907
908       if (IDENTIFIER_POINTER (name)[0] == '_'
909           && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
910         error ("member %qD conflicts with virtual function table field name",
911                value);
912     }
913
914   /* Stash away type declarations.  */
915   if (TREE_CODE (value) == TYPE_DECL)
916     {
917       DECL_NONLOCAL (value) = 1;
918       DECL_CONTEXT (value) = current_class_type;
919
920       if (attrlist)
921         {
922           int attrflags = 0;
923
924           /* If this is a typedef that names the class for linkage purposes
925              (7.1.3p8), apply any attributes directly to the type.  */
926           if (OVERLOAD_TYPE_P (TREE_TYPE (value))
927               && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
928             attrflags = ATTR_FLAG_TYPE_IN_PLACE;
929
930           cplus_decl_attributes (&value, attrlist, attrflags);
931         }
932
933       if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
934           && TREE_TYPE (value) != error_mark_node
935           && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
936         set_underlying_type (value);
937
938       /* It's important that push_template_decl below follows
939          set_underlying_type above so that the created template
940          carries the properly set type of VALUE.  */
941       if (processing_template_decl)
942         value = push_template_decl (value);
943
944       record_locally_defined_typedef (value);
945       return value;
946     }
947
948   int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
949
950   if (!friendp && DECL_IN_AGGR_P (value))
951     {
952       error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
953       return void_type_node;
954     }
955
956   if (asmspec_tree && asmspec_tree != error_mark_node)
957     asmspec = TREE_STRING_POINTER (asmspec_tree);
958
959   if (init)
960     {
961       if (TREE_CODE (value) == FUNCTION_DECL)
962         {
963           if (init == ridpointers[(int)RID_DELETE])
964             {
965               DECL_DELETED_FN (value) = 1;
966               DECL_DECLARED_INLINE_P (value) = 1;
967               DECL_INITIAL (value) = error_mark_node;
968             }
969           else if (init == ridpointers[(int)RID_DEFAULT])
970             {
971               if (defaultable_fn_check (value))
972                 {
973                   DECL_DEFAULTED_FN (value) = 1;
974                   DECL_INITIALIZED_IN_CLASS_P (value) = 1;
975                   DECL_DECLARED_INLINE_P (value) = 1;
976                 }
977             }
978           else if (TREE_CODE (init) == DEFAULT_ARG)
979             error ("invalid initializer for member function %qD", value);
980           else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
981             {
982               if (integer_zerop (init))
983                 DECL_PURE_VIRTUAL_P (value) = 1;
984               else if (error_operand_p (init))
985                 ; /* An error has already been reported.  */
986               else
987                 error ("invalid initializer for member function %qD",
988                        value);
989             }
990           else
991             {
992               gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
993               if (friendp)
994                 error ("initializer specified for friend function %qD",
995                        value);
996               else
997                 error ("initializer specified for static member function %qD",
998                        value);
999             }
1000         }
1001       else if (TREE_CODE (value) == FIELD_DECL)
1002         /* C++11 NSDMI, keep going.  */;
1003       else if (!VAR_P (value))
1004         gcc_unreachable ();
1005     }
1006
1007   /* Pass friend decls back.  */
1008   if ((TREE_CODE (value) == FUNCTION_DECL
1009        || TREE_CODE (value) == TEMPLATE_DECL)
1010       && DECL_CONTEXT (value) != current_class_type)
1011     return value;
1012
1013   /* Need to set this before push_template_decl.  */
1014   if (TREE_CODE (value) == VAR_DECL)
1015     DECL_CONTEXT (value) = current_class_type;
1016
1017   if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
1018     {
1019       value = push_template_decl (value);
1020       if (error_operand_p (value))
1021         return error_mark_node;
1022     }
1023
1024   if (attrlist)
1025     cplus_decl_attributes (&value, attrlist, 0);
1026
1027   if (init && DIRECT_LIST_INIT_P (init))
1028     flags = LOOKUP_NORMAL;
1029   else
1030     flags = LOOKUP_IMPLICIT;
1031
1032   switch (TREE_CODE (value))
1033     {
1034     case VAR_DECL:
1035       finish_static_data_member_decl (value, init, init_const_expr_p,
1036                                       asmspec_tree, flags);
1037       return value;
1038
1039     case FIELD_DECL:
1040       if (asmspec)
1041         error ("%<asm%> specifiers are not permitted on non-static data members");
1042       if (DECL_INITIAL (value) == error_mark_node)
1043         init = error_mark_node;
1044       cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1045                       NULL_TREE, flags);
1046       DECL_IN_AGGR_P (value) = 1;
1047       return value;
1048
1049     case  FUNCTION_DECL:
1050       if (asmspec)
1051         set_user_assembler_name (value, asmspec);
1052
1053       cp_finish_decl (value,
1054                       /*init=*/NULL_TREE,
1055                       /*init_const_expr_p=*/false,
1056                       asmspec_tree, flags);
1057
1058       /* Pass friends back this way.  */
1059       if (DECL_FRIEND_P (value))
1060         return void_type_node;
1061
1062       DECL_IN_AGGR_P (value) = 1;
1063       return value;
1064
1065     default:
1066       gcc_unreachable ();
1067     }
1068   return NULL_TREE;
1069 }
1070
1071 /* Like `grokfield', but for bitfields.
1072    WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.  */
1073
1074 tree
1075 grokbitfield (const cp_declarator *declarator,
1076               cp_decl_specifier_seq *declspecs, tree width,
1077               tree attrlist)
1078 {
1079   tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1080
1081   if (value == error_mark_node)
1082     return NULL_TREE; /* friends went bad.  */
1083   if (TREE_TYPE (value) == error_mark_node)
1084     return value;
1085
1086   /* Pass friendly classes back.  */
1087   if (VOID_TYPE_P (value))
1088     return void_type_node;
1089
1090   if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1091       && (POINTER_TYPE_P (value)
1092           || !dependent_type_p (TREE_TYPE (value))))
1093     {
1094       error ("bit-field %qD with non-integral type", value);
1095       return error_mark_node;
1096     }
1097
1098   if (TREE_CODE (value) == TYPE_DECL)
1099     {
1100       error ("cannot declare %qD to be a bit-field type", value);
1101       return NULL_TREE;
1102     }
1103
1104   /* Usually, finish_struct_1 catches bitfields with invalid types.
1105      But, in the case of bitfields with function type, we confuse
1106      ourselves into thinking they are member functions, so we must
1107      check here.  */
1108   if (TREE_CODE (value) == FUNCTION_DECL)
1109     {
1110       error ("cannot declare bit-field %qD with function type",
1111              DECL_NAME (value));
1112       return NULL_TREE;
1113     }
1114
1115   if (DECL_IN_AGGR_P (value))
1116     {
1117       error ("%qD is already defined in the class %qT", value,
1118              DECL_CONTEXT (value));
1119       return void_type_node;
1120     }
1121
1122   if (TREE_STATIC (value))
1123     {
1124       error ("static member %qD cannot be a bit-field", value);
1125       return NULL_TREE;
1126     }
1127   cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1128
1129   if (width != error_mark_node)
1130     {
1131       /* The width must be an integer type.  */
1132       if (!type_dependent_expression_p (width)
1133           && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1134         error ("width of bit-field %qD has non-integral type %qT", value,
1135                TREE_TYPE (width));
1136       DECL_INITIAL (value) = width;
1137       SET_DECL_C_BIT_FIELD (value);
1138     }
1139
1140   DECL_IN_AGGR_P (value) = 1;
1141
1142   if (attrlist)
1143     cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1144
1145   return value;
1146 }
1147
1148 \f
1149 /* Returns true iff ATTR is an attribute which needs to be applied at
1150    instantiation time rather than template definition time.  */
1151
1152 static bool
1153 is_late_template_attribute (tree attr, tree decl)
1154 {
1155   tree name = get_attribute_name (attr);
1156   tree args = TREE_VALUE (attr);
1157   const struct attribute_spec *spec = lookup_attribute_spec (name);
1158   tree arg;
1159
1160   if (!spec)
1161     /* Unknown attribute.  */
1162     return false;
1163
1164   /* Attribute weak handling wants to write out assembly right away.  */
1165   if (is_attribute_p ("weak", name))
1166     return true;
1167
1168   /* Attribute unused is applied directly, as it appertains to
1169      decls. */
1170   if (is_attribute_p ("unused", name))
1171     return false;
1172
1173   /* #pragma omp declare simd attribute needs to be always deferred.  */
1174   if (flag_openmp
1175       && is_attribute_p ("omp declare simd", name))
1176     return true;
1177
1178   /* An attribute pack is clearly dependent.  */
1179   if (args && PACK_EXPANSION_P (args))
1180     return true;
1181
1182   /* If any of the arguments are dependent expressions, we can't evaluate
1183      the attribute until instantiation time.  */
1184   for (arg = args; arg; arg = TREE_CHAIN (arg))
1185     {
1186       tree t = TREE_VALUE (arg);
1187
1188       /* If the first attribute argument is an identifier, only consider
1189          second and following arguments.  Attributes like mode, format,
1190          cleanup and several target specific attributes aren't late
1191          just because they have an IDENTIFIER_NODE as first argument.  */
1192       if (arg == args && identifier_p (t))
1193         continue;
1194
1195       if (value_dependent_expression_p (t)
1196           || type_dependent_expression_p (t))
1197         return true;
1198     }
1199
1200   if (TREE_CODE (decl) == TYPE_DECL
1201       || TYPE_P (decl)
1202       || spec->type_required)
1203     {
1204       tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1205
1206       /* We can't apply any attributes to a completely unknown type until
1207          instantiation time.  */
1208       enum tree_code code = TREE_CODE (type);
1209       if (code == TEMPLATE_TYPE_PARM
1210           || code == BOUND_TEMPLATE_TEMPLATE_PARM
1211           || code == TYPENAME_TYPE)
1212         return true;
1213       /* Also defer most attributes on dependent types.  This is not
1214          necessary in all cases, but is the better default.  */
1215       else if (dependent_type_p (type)
1216                /* But some attributes specifically apply to templates.  */
1217                && !is_attribute_p ("abi_tag", name)
1218                && !is_attribute_p ("deprecated", name)
1219                && !is_attribute_p ("visibility", name))
1220         return true;
1221       else
1222         return false;
1223     }
1224   else
1225     return false;
1226 }
1227
1228 /* ATTR_P is a list of attributes.  Remove any attributes which need to be
1229    applied at instantiation time and return them.  If IS_DEPENDENT is true,
1230    the declaration itself is dependent, so all attributes should be applied
1231    at instantiation time.  */
1232
1233 static tree
1234 splice_template_attributes (tree *attr_p, tree decl)
1235 {
1236   tree *p = attr_p;
1237   tree late_attrs = NULL_TREE;
1238   tree *q = &late_attrs;
1239
1240   if (!p)
1241     return NULL_TREE;
1242
1243   for (; *p; )
1244     {
1245       if (is_late_template_attribute (*p, decl))
1246         {
1247           ATTR_IS_DEPENDENT (*p) = 1;
1248           *q = *p;
1249           *p = TREE_CHAIN (*p);
1250           q = &TREE_CHAIN (*q);
1251           *q = NULL_TREE;
1252         }
1253       else
1254         p = &TREE_CHAIN (*p);
1255     }
1256
1257   return late_attrs;
1258 }
1259
1260 /* Remove any late attributes from the list in ATTR_P and attach them to
1261    DECL_P.  */
1262
1263 static void
1264 save_template_attributes (tree *attr_p, tree *decl_p)
1265 {
1266   tree *q;
1267
1268   if (attr_p && *attr_p == error_mark_node)
1269     return;
1270
1271   tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1272   if (!late_attrs)
1273     return;
1274
1275   if (DECL_P (*decl_p))
1276     q = &DECL_ATTRIBUTES (*decl_p);
1277   else
1278     q = &TYPE_ATTRIBUTES (*decl_p);
1279
1280   tree old_attrs = *q;
1281
1282   /* Merge the late attributes at the beginning with the attribute
1283      list.  */
1284   late_attrs = merge_attributes (late_attrs, *q);
1285   *q = late_attrs;
1286
1287   if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1288     {
1289       /* We've added new attributes directly to the main variant, so
1290          now we need to update all of the other variants to include
1291          these new attributes.  */
1292       tree variant;
1293       for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1294            variant = TYPE_NEXT_VARIANT (variant))
1295         {
1296           gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1297           TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1298         }
1299     }
1300 }
1301
1302 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1303    to a typedef which gives a previously anonymous class or enum a name for
1304    linkage purposes.  */
1305
1306 bool
1307 attributes_naming_typedef_ok (tree attrs)
1308 {
1309   for (; attrs; attrs = TREE_CHAIN (attrs))
1310     {
1311       tree name = get_attribute_name (attrs);
1312       if (is_attribute_p ("vector_size", name))
1313         return false;
1314     }
1315   return true;
1316 }
1317
1318 /* Like reconstruct_complex_type, but handle also template trees.  */
1319
1320 tree
1321 cp_reconstruct_complex_type (tree type, tree bottom)
1322 {
1323   tree inner, outer;
1324   bool late_return_type_p = false;
1325
1326   if (TYPE_PTR_P (type))
1327     {
1328       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1329       outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1330                                            TYPE_REF_CAN_ALIAS_ALL (type));
1331     }
1332   else if (TREE_CODE (type) == REFERENCE_TYPE)
1333     {
1334       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1335       outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1336                                              TYPE_REF_CAN_ALIAS_ALL (type));
1337     }
1338   else if (TREE_CODE (type) == ARRAY_TYPE)
1339     {
1340       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1341       outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1342       /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1343          element type qualification will be handled by the recursive
1344          cp_reconstruct_complex_type call and cp_build_qualified_type
1345          for ARRAY_TYPEs changes the element type.  */
1346       return outer;
1347     }
1348   else if (TREE_CODE (type) == FUNCTION_TYPE)
1349     {
1350       late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1351       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1352       outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1353       outer = apply_memfn_quals (outer,
1354                                  type_memfn_quals (type),
1355                                  type_memfn_rqual (type));
1356     }
1357   else if (TREE_CODE (type) == METHOD_TYPE)
1358     {
1359       late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1360       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1361       /* The build_method_type_directly() routine prepends 'this' to argument list,
1362          so we must compensate by getting rid of it.  */
1363       outer
1364         = build_method_type_directly
1365             (class_of_this_parm (type), inner,
1366              TREE_CHAIN (TYPE_ARG_TYPES (type)));
1367     }
1368   else if (TREE_CODE (type) == OFFSET_TYPE)
1369     {
1370       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1371       outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1372     }
1373   else
1374     return bottom;
1375
1376   if (TYPE_ATTRIBUTES (type))
1377     outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1378   outer = cp_build_qualified_type (outer, cp_type_quals (type));
1379
1380   if (late_return_type_p)
1381     TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1382
1383   return outer;
1384 }
1385
1386 /* Replaces any constexpr expression that may be into the attributes
1387    arguments with their reduced value.  */
1388
1389 static void
1390 cp_check_const_attributes (tree attributes)
1391 {
1392   if (attributes == error_mark_node)
1393     return;
1394
1395   tree attr;
1396   for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1397     {
1398       tree arg;
1399       for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1400         {
1401           tree expr = TREE_VALUE (arg);
1402           if (EXPR_P (expr))
1403             TREE_VALUE (arg) = maybe_constant_value (expr);
1404         }
1405     }
1406 }
1407
1408 /* Return true if TYPE is an OpenMP mappable type.  */
1409 bool
1410 cp_omp_mappable_type (tree type)
1411 {
1412   /* Mappable type has to be complete.  */
1413   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1414     return false;
1415   /* Arrays have mappable type if the elements have mappable type.  */
1416   while (TREE_CODE (type) == ARRAY_TYPE)
1417     type = TREE_TYPE (type);
1418   /* A mappable type cannot contain virtual members.  */
1419   if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1420     return false;
1421   /* All data members must be non-static.  */
1422   if (CLASS_TYPE_P (type))
1423     {
1424       tree field;
1425       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1426         if (TREE_CODE (field) == VAR_DECL)
1427           return false;
1428         /* All fields must have mappable types.  */
1429         else if (TREE_CODE (field) == FIELD_DECL
1430                  && !cp_omp_mappable_type (TREE_TYPE (field)))
1431           return false;
1432     }
1433   return true;
1434 }
1435
1436 /* Like decl_attributes, but handle C++ complexity.  */
1437
1438 void
1439 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1440 {
1441   if (*decl == NULL_TREE || *decl == void_type_node
1442       || *decl == error_mark_node)
1443     return;
1444
1445   /* Add implicit "omp declare target" attribute if requested.  */
1446   if (scope_chain->omp_declare_target_attribute
1447       && ((TREE_CODE (*decl) == VAR_DECL
1448            && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1449           || TREE_CODE (*decl) == FUNCTION_DECL))
1450     {
1451       if (TREE_CODE (*decl) == VAR_DECL
1452           && DECL_CLASS_SCOPE_P (*decl))
1453         error ("%q+D static data member inside of declare target directive",
1454                *decl);
1455       else if (TREE_CODE (*decl) == VAR_DECL
1456                && (DECL_FUNCTION_SCOPE_P (*decl)
1457                    || (current_function_decl && !DECL_EXTERNAL (*decl))))
1458         error ("%q+D in block scope inside of declare target directive",
1459                *decl);
1460       else if (!processing_template_decl
1461                && TREE_CODE (*decl) == VAR_DECL
1462                && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1463         error ("%q+D in declare target directive does not have mappable type",
1464                *decl);
1465       else
1466         attributes = tree_cons (get_identifier ("omp declare target"),
1467                                 NULL_TREE, attributes);
1468     }
1469
1470   if (processing_template_decl)
1471     {
1472       if (check_for_bare_parameter_packs (attributes))
1473         return;
1474
1475       save_template_attributes (&attributes, decl);
1476     }
1477
1478   cp_check_const_attributes (attributes);
1479
1480   if (TREE_CODE (*decl) == TEMPLATE_DECL)
1481     decl = &DECL_TEMPLATE_RESULT (*decl);
1482
1483   if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1484     {
1485       attributes
1486         = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1487       decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1488                        attributes, flags);
1489     }
1490   else
1491     decl_attributes (decl, attributes, flags);
1492
1493   if (TREE_CODE (*decl) == TYPE_DECL)
1494     SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1495
1496   /* Propagate deprecation out to the template.  */
1497   if (TREE_DEPRECATED (*decl))
1498     if (tree ti = get_template_info (*decl))
1499       {
1500         tree tmpl = TI_TEMPLATE (ti);
1501         tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1502                         : DECL_TEMPLATE_RESULT (tmpl));
1503         if (*decl == pattern)
1504           TREE_DEPRECATED (tmpl) = true;
1505       }
1506 }
1507 \f
1508 /* Walks through the namespace- or function-scope anonymous union
1509    OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1510    Returns one of the fields for use in the mangled name.  */
1511
1512 static tree
1513 build_anon_union_vars (tree type, tree object)
1514 {
1515   tree main_decl = NULL_TREE;
1516   tree field;
1517
1518   /* Rather than write the code to handle the non-union case,
1519      just give an error.  */
1520   if (TREE_CODE (type) != UNION_TYPE)
1521     {
1522       error ("anonymous struct not inside named type");
1523       return error_mark_node;
1524     }
1525
1526   for (field = TYPE_FIELDS (type);
1527        field != NULL_TREE;
1528        field = DECL_CHAIN (field))
1529     {
1530       tree decl;
1531       tree ref;
1532
1533       if (DECL_ARTIFICIAL (field))
1534         continue;
1535       if (TREE_CODE (field) != FIELD_DECL)
1536         {
1537           permerror (input_location, "%q+#D invalid; an anonymous union can only "
1538                      "have non-static data members", field);
1539           continue;
1540         }
1541
1542       if (TREE_PRIVATE (field))
1543         permerror (input_location, "private member %q+#D in anonymous union", field);
1544       else if (TREE_PROTECTED (field))
1545         permerror (input_location, "protected member %q+#D in anonymous union", field);
1546
1547       if (processing_template_decl)
1548         ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1549                                 DECL_NAME (field), NULL_TREE);
1550       else
1551         ref = build_class_member_access_expr (object, field, NULL_TREE,
1552                                               false, tf_warning_or_error);
1553
1554       if (DECL_NAME (field))
1555         {
1556           tree base;
1557
1558           decl = build_decl (input_location,
1559                              VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1560           DECL_ANON_UNION_VAR_P (decl) = 1;
1561           DECL_ARTIFICIAL (decl) = 1;
1562
1563           base = get_base_address (object);
1564           TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1565           TREE_STATIC (decl) = TREE_STATIC (base);
1566           DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1567
1568           SET_DECL_VALUE_EXPR (decl, ref);
1569           DECL_HAS_VALUE_EXPR_P (decl) = 1;
1570
1571           decl = pushdecl (decl);
1572         }
1573       else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1574         decl = build_anon_union_vars (TREE_TYPE (field), ref);
1575       else
1576         decl = 0;
1577
1578       if (main_decl == NULL_TREE)
1579         main_decl = decl;
1580     }
1581
1582   return main_decl;
1583 }
1584
1585 /* Finish off the processing of a UNION_TYPE structure.  If the union is an
1586    anonymous union, then all members must be laid out together.  PUBLIC_P
1587    is nonzero if this union is not declared static.  */
1588
1589 void
1590 finish_anon_union (tree anon_union_decl)
1591 {
1592   tree type;
1593   tree main_decl;
1594   bool public_p;
1595
1596   if (anon_union_decl == error_mark_node)
1597     return;
1598
1599   type = TREE_TYPE (anon_union_decl);
1600   public_p = TREE_PUBLIC (anon_union_decl);
1601
1602   /* The VAR_DECL's context is the same as the TYPE's context.  */
1603   DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1604
1605   if (TYPE_FIELDS (type) == NULL_TREE)
1606     return;
1607
1608   if (public_p)
1609     {
1610       error ("namespace-scope anonymous aggregates must be static");
1611       return;
1612     }
1613
1614   main_decl = build_anon_union_vars (type, anon_union_decl);
1615   if (main_decl == error_mark_node)
1616     return;
1617   if (main_decl == NULL_TREE)
1618     {
1619       warning (0, "anonymous union with no members");
1620       return;
1621     }
1622
1623   if (!processing_template_decl)
1624     {
1625       /* Use main_decl to set the mangled name.  */
1626       DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1627       maybe_commonize_var (anon_union_decl);
1628       if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1629         mangle_decl (anon_union_decl);
1630       DECL_NAME (anon_union_decl) = NULL_TREE;
1631     }
1632
1633   pushdecl (anon_union_decl);
1634   cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1635 }
1636 \f
1637 /* Auxiliary functions to make type signatures for
1638    `operator new' and `operator delete' correspond to
1639    what compiler will be expecting.  */
1640
1641 tree
1642 coerce_new_type (tree type)
1643 {
1644   int e = 0;
1645   tree args = TYPE_ARG_TYPES (type);
1646
1647   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1648
1649   if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1650     {
1651       e = 1;
1652       error ("%<operator new%> must return type %qT", ptr_type_node);
1653     }
1654
1655   if (args && args != void_list_node)
1656     {
1657       if (TREE_PURPOSE (args))
1658         {
1659           /* [basic.stc.dynamic.allocation]
1660              
1661              The first parameter shall not have an associated default
1662              argument.  */
1663           error ("the first parameter of %<operator new%> cannot "
1664                  "have a default argument");
1665           /* Throw away the default argument.  */
1666           TREE_PURPOSE (args) = NULL_TREE;
1667         }
1668
1669       if (!same_type_p (TREE_VALUE (args), size_type_node))
1670         {
1671           e = 2;
1672           args = TREE_CHAIN (args);
1673         }
1674     }
1675   else
1676     e = 2;
1677
1678   if (e == 2)
1679     permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1680                "as first parameter", size_type_node);
1681
1682   switch (e)
1683   {
1684     case 2:
1685       args = tree_cons (NULL_TREE, size_type_node, args);
1686       /* Fall through.  */
1687     case 1:
1688       type = build_exception_variant
1689               (build_function_type (ptr_type_node, args),
1690                TYPE_RAISES_EXCEPTIONS (type));
1691       /* Fall through.  */
1692     default:;
1693   }
1694   return type;
1695 }
1696
1697 tree
1698 coerce_delete_type (tree type)
1699 {
1700   int e = 0;
1701   tree args = TYPE_ARG_TYPES (type);
1702
1703   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1704
1705   if (!same_type_p (TREE_TYPE (type), void_type_node))
1706     {
1707       e = 1;
1708       error ("%<operator delete%> must return type %qT", void_type_node);
1709     }
1710
1711   if (!args || args == void_list_node
1712       || !same_type_p (TREE_VALUE (args), ptr_type_node))
1713     {
1714       e = 2;
1715       if (args && args != void_list_node)
1716         args = TREE_CHAIN (args);
1717       error ("%<operator delete%> takes type %qT as first parameter",
1718              ptr_type_node);
1719     }
1720   switch (e)
1721   {
1722     case 2:
1723       args = tree_cons (NULL_TREE, ptr_type_node, args);
1724       /* Fall through.  */
1725     case 1:
1726       type = build_exception_variant
1727               (build_function_type (void_type_node, args),
1728                TYPE_RAISES_EXCEPTIONS (type));
1729       /* Fall through.  */
1730     default:;
1731   }
1732
1733   return type;
1734 }
1735 \f
1736 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1737    and mark them as needed.  */
1738
1739 static void
1740 mark_vtable_entries (tree decl)
1741 {
1742   tree fnaddr;
1743   unsigned HOST_WIDE_INT idx;
1744
1745   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1746                               idx, fnaddr)
1747     {
1748       tree fn;
1749
1750       STRIP_NOPS (fnaddr);
1751
1752       if (TREE_CODE (fnaddr) != ADDR_EXPR
1753           && TREE_CODE (fnaddr) != FDESC_EXPR)
1754         /* This entry is an offset: a virtual base class offset, a
1755            virtual call offset, an RTTI offset, etc.  */
1756         continue;
1757
1758       fn = TREE_OPERAND (fnaddr, 0);
1759       TREE_ADDRESSABLE (fn) = 1;
1760       /* When we don't have vcall offsets, we output thunks whenever
1761          we output the vtables that contain them.  With vcall offsets,
1762          we know all the thunks we'll need when we emit a virtual
1763          function, so we emit the thunks there instead.  */
1764       if (DECL_THUNK_P (fn))
1765         use_thunk (fn, /*emit_p=*/0);
1766       mark_used (fn);
1767     }
1768 }
1769
1770 /* Set DECL up to have the closest approximation of "initialized common"
1771    linkage available.  */
1772
1773 void
1774 comdat_linkage (tree decl)
1775 {
1776   if (flag_weak)
1777     make_decl_one_only (decl, cxx_comdat_group (decl));
1778   else if (TREE_CODE (decl) == FUNCTION_DECL
1779            || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1780     /* We can just emit function and compiler-generated variables
1781        statically; having multiple copies is (for the most part) only
1782        a waste of space.
1783
1784        There are two correctness issues, however: the address of a
1785        template instantiation with external linkage should be the
1786        same, independent of what translation unit asks for the
1787        address, and this will not hold when we emit multiple copies of
1788        the function.  However, there's little else we can do.
1789
1790        Also, by default, the typeinfo implementation assumes that
1791        there will be only one copy of the string used as the name for
1792        each type.  Therefore, if weak symbols are unavailable, the
1793        run-time library should perform a more conservative check; it
1794        should perform a string comparison, rather than an address
1795        comparison.  */
1796     TREE_PUBLIC (decl) = 0;
1797   else
1798     {
1799       /* Static data member template instantiations, however, cannot
1800          have multiple copies.  */
1801       if (DECL_INITIAL (decl) == 0
1802           || DECL_INITIAL (decl) == error_mark_node)
1803         DECL_COMMON (decl) = 1;
1804       else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1805         {
1806           DECL_COMMON (decl) = 1;
1807           DECL_INITIAL (decl) = error_mark_node;
1808         }
1809       else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1810         {
1811           /* We can't do anything useful; leave vars for explicit
1812              instantiation.  */
1813           DECL_EXTERNAL (decl) = 1;
1814           DECL_NOT_REALLY_EXTERN (decl) = 0;
1815         }
1816     }
1817
1818   DECL_COMDAT (decl) = 1;
1819 }
1820
1821 /* For win32 we also want to put explicit instantiations in
1822    linkonce sections, so that they will be merged with implicit
1823    instantiations; otherwise we get duplicate symbol errors.
1824    For Darwin we do not want explicit instantiations to be
1825    linkonce.  */
1826
1827 void
1828 maybe_make_one_only (tree decl)
1829 {
1830   /* We used to say that this was not necessary on targets that support weak
1831      symbols, because the implicit instantiations will defer to the explicit
1832      one.  However, that's not actually the case in SVR4; a strong definition
1833      after a weak one is an error.  Also, not making explicit
1834      instantiations one_only means that we can end up with two copies of
1835      some template instantiations.  */
1836   if (! flag_weak)
1837     return;
1838
1839   /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1840      we can get away with not emitting them if they aren't used.  We need
1841      to for variables so that cp_finish_decl will update their linkage,
1842      because their DECL_INITIAL may not have been set properly yet.  */
1843
1844   if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1845       || (! DECL_EXPLICIT_INSTANTIATION (decl)
1846           && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1847     {
1848       make_decl_one_only (decl, cxx_comdat_group (decl));
1849
1850       if (VAR_P (decl))
1851         {
1852           varpool_node *node = varpool_node::get_create (decl);
1853           DECL_COMDAT (decl) = 1;
1854           /* Mark it needed so we don't forget to emit it.  */
1855           node->forced_by_abi = true;
1856           TREE_USED (decl) = 1;
1857         }
1858     }
1859 }
1860
1861 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1862    This predicate will give the right answer during parsing of the
1863    function, which other tests may not.  */
1864
1865 bool
1866 vague_linkage_p (tree decl)
1867 {
1868   /* Unfortunately, import_export_decl has not always been called
1869      before the function is processed, so we cannot simply check
1870      DECL_COMDAT.  */
1871   if (DECL_COMDAT (decl)
1872       || (((TREE_CODE (decl) == FUNCTION_DECL
1873             && DECL_DECLARED_INLINE_P (decl))
1874            || (DECL_LANG_SPECIFIC (decl)
1875                && DECL_TEMPLATE_INSTANTIATION (decl)))
1876           && TREE_PUBLIC (decl)))
1877     return true;
1878   else if (DECL_FUNCTION_SCOPE_P (decl))
1879     /* A local static in an inline effectively has vague linkage.  */
1880     return (TREE_STATIC (decl)
1881             && vague_linkage_p (DECL_CONTEXT (decl)));
1882   else
1883     return false;
1884 }
1885
1886 /* Determine whether or not we want to specifically import or export CTYPE,
1887    using various heuristics.  */
1888
1889 static void
1890 import_export_class (tree ctype)
1891 {
1892   /* -1 for imported, 1 for exported.  */
1893   int import_export = 0;
1894
1895   /* It only makes sense to call this function at EOF.  The reason is
1896      that this function looks at whether or not the first non-inline
1897      non-abstract virtual member function has been defined in this
1898      translation unit.  But, we can't possibly know that until we've
1899      seen the entire translation unit.  */
1900   gcc_assert (at_eof);
1901
1902   if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1903     return;
1904
1905   /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1906      we will have CLASSTYPE_INTERFACE_ONLY set but not
1907      CLASSTYPE_INTERFACE_KNOWN.  In that case, we don't want to use this
1908      heuristic because someone will supply a #pragma implementation
1909      elsewhere, and deducing it here would produce a conflict.  */
1910   if (CLASSTYPE_INTERFACE_ONLY (ctype))
1911     return;
1912
1913   if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1914     import_export = -1;
1915   else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1916     import_export = 1;
1917   else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1918            && !flag_implicit_templates)
1919     /* For a template class, without -fimplicit-templates, check the
1920        repository.  If the virtual table is assigned to this
1921        translation unit, then export the class; otherwise, import
1922        it.  */
1923       import_export = repo_export_class_p (ctype) ? 1 : -1;
1924   else if (TYPE_POLYMORPHIC_P (ctype))
1925     {
1926       /* The ABI specifies that the virtual table and associated
1927          information are emitted with the key method, if any.  */
1928       tree method = CLASSTYPE_KEY_METHOD (ctype);
1929       /* If weak symbol support is not available, then we must be
1930          careful not to emit the vtable when the key function is
1931          inline.  An inline function can be defined in multiple
1932          translation units.  If we were to emit the vtable in each
1933          translation unit containing a definition, we would get
1934          multiple definition errors at link-time.  */
1935       if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1936         import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1937     }
1938
1939   /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1940      a definition anywhere else.  */
1941   if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1942     import_export = 0;
1943
1944   /* Allow back ends the chance to overrule the decision.  */
1945   if (targetm.cxx.import_export_class)
1946     import_export = targetm.cxx.import_export_class (ctype, import_export);
1947
1948   if (import_export)
1949     {
1950       SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1951       CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1952     }
1953 }
1954
1955 /* Return true if VAR has already been provided to the back end; in that
1956    case VAR should not be modified further by the front end.  */
1957 static bool
1958 var_finalized_p (tree var)
1959 {
1960   return varpool_node::get_create (var)->definition;
1961 }
1962
1963 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1964    must be emitted in this translation unit.  Mark it as such.  */
1965
1966 void
1967 mark_needed (tree decl)
1968 {
1969   TREE_USED (decl) = 1;
1970   if (TREE_CODE (decl) == FUNCTION_DECL)
1971     {
1972       /* Extern inline functions don't become needed when referenced.
1973          If we know a method will be emitted in other TU and no new
1974          functions can be marked reachable, just use the external
1975          definition.  */
1976       struct cgraph_node *node = cgraph_node::get_create (decl);
1977       node->forced_by_abi = true;
1978
1979       /* #pragma interface and -frepo code can call mark_needed for
1980           maybe-in-charge 'tors; mark the clones as well.  */
1981       tree clone;
1982       FOR_EACH_CLONE (clone, decl)
1983         mark_needed (clone);
1984     }
1985   else if (TREE_CODE (decl) == VAR_DECL)
1986     {
1987       varpool_node *node = varpool_node::get_create (decl);
1988       /* C++ frontend use mark_decl_references to force COMDAT variables
1989          to be output that might appear dead otherwise.  */
1990       node->forced_by_abi = true;
1991     }
1992 }
1993
1994 /* DECL is either a FUNCTION_DECL or a VAR_DECL.  This function
1995    returns true if a definition of this entity should be provided in
1996    this object file.  Callers use this function to determine whether
1997    or not to let the back end know that a definition of DECL is
1998    available in this translation unit.  */
1999
2000 bool
2001 decl_needed_p (tree decl)
2002 {
2003   gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2004   /* This function should only be called at the end of the translation
2005      unit.  We cannot be sure of whether or not something will be
2006      COMDAT until that point.  */
2007   gcc_assert (at_eof);
2008
2009   /* All entities with external linkage that are not COMDAT/EXTERN should be
2010      emitted; they may be referred to from other object files.  */
2011   if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2012     return true;
2013   /* Functions marked "dllexport" must be emitted so that they are
2014      visible to other DLLs.  */
2015   if (flag_keep_inline_dllexport
2016       && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2017     return true;
2018
2019   /* When not optimizing, do not bother to produce definitions for extern
2020      symbols.  */
2021   if (DECL_REALLY_EXTERN (decl)
2022       && ((TREE_CODE (decl) != FUNCTION_DECL
2023            && !optimize)
2024           || (TREE_CODE (decl) == FUNCTION_DECL
2025               && !opt_for_fn (decl, optimize)))
2026       && !lookup_attribute ("always_inline", decl))
2027     return false;
2028
2029   /* If this entity was used, let the back end see it; it will decide
2030      whether or not to emit it into the object file.  */
2031   if (TREE_USED (decl))
2032       return true;
2033   /* Virtual functions might be needed for devirtualization.  */
2034   if (flag_devirtualize
2035       && TREE_CODE (decl) == FUNCTION_DECL
2036       && DECL_VIRTUAL_P (decl))
2037     return true;
2038   /* Otherwise, DECL does not need to be emitted -- yet.  A subsequent
2039      reference to DECL might cause it to be emitted later.  */
2040   return false;
2041 }
2042
2043 /* If necessary, write out the vtables for the dynamic class CTYPE.
2044    Returns true if any vtables were emitted.  */
2045
2046 static bool
2047 maybe_emit_vtables (tree ctype)
2048 {
2049   tree vtbl;
2050   tree primary_vtbl;
2051   int needed = 0;
2052   varpool_node *current = NULL, *last = NULL;
2053
2054   /* If the vtables for this class have already been emitted there is
2055      nothing more to do.  */
2056   primary_vtbl = CLASSTYPE_VTABLES (ctype);
2057   if (var_finalized_p (primary_vtbl))
2058     return false;
2059   /* Ignore dummy vtables made by get_vtable_decl.  */
2060   if (TREE_TYPE (primary_vtbl) == void_type_node)
2061     return false;
2062
2063   /* On some targets, we cannot determine the key method until the end
2064      of the translation unit -- which is when this function is
2065      called.  */
2066   if (!targetm.cxx.key_method_may_be_inline ())
2067     determine_key_method (ctype);
2068
2069   /* See if any of the vtables are needed.  */
2070   for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2071     {
2072       import_export_decl (vtbl);
2073       if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2074         needed = 1;
2075     }
2076   if (!needed)
2077     {
2078       /* If the references to this class' vtables are optimized away,
2079          still emit the appropriate debugging information.  See
2080          dfs_debug_mark.  */
2081       if (DECL_COMDAT (primary_vtbl)
2082           && CLASSTYPE_DEBUG_REQUESTED (ctype))
2083         note_debug_info_needed (ctype);
2084       return false;
2085     }
2086
2087   /* The ABI requires that we emit all of the vtables if we emit any
2088      of them.  */
2089   for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2090     {
2091       /* Mark entities references from the virtual table as used.  */
2092       mark_vtable_entries (vtbl);
2093
2094       if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2095         {
2096           vec<tree, va_gc> *cleanups = NULL;
2097           tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2098                                         LOOKUP_NORMAL);
2099
2100           /* It had better be all done at compile-time.  */
2101           gcc_assert (!expr && !cleanups);
2102         }
2103
2104       /* Write it out.  */
2105       DECL_EXTERNAL (vtbl) = 0;
2106       rest_of_decl_compilation (vtbl, 1, 1);
2107
2108       /* Because we're only doing syntax-checking, we'll never end up
2109          actually marking the variable as written.  */
2110       if (flag_syntax_only)
2111         TREE_ASM_WRITTEN (vtbl) = 1;
2112       else if (DECL_ONE_ONLY (vtbl))
2113         {
2114           current = varpool_node::get_create (vtbl);
2115           if (last)
2116             current->add_to_same_comdat_group (last);
2117           last = current;
2118         }
2119     }
2120
2121   /* Since we're writing out the vtable here, also write the debug
2122      info.  */
2123   note_debug_info_needed (ctype);
2124
2125   return true;
2126 }
2127
2128 /* A special return value from type_visibility meaning internal
2129    linkage.  */
2130
2131 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2132
2133 /* walk_tree helper function for type_visibility.  */
2134
2135 static tree
2136 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2137 {
2138   int *vis_p = (int *)data;
2139   if (! TYPE_P (*tp))
2140     {
2141       *walk_subtrees = 0;
2142     }
2143   else if (OVERLOAD_TYPE_P (*tp)
2144            && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2145     {
2146       *vis_p = VISIBILITY_ANON;
2147       return *tp;
2148     }
2149   else if (CLASS_TYPE_P (*tp)
2150            && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2151     *vis_p = CLASSTYPE_VISIBILITY (*tp);
2152   return NULL;
2153 }
2154
2155 /* Returns the visibility of TYPE, which is the minimum visibility of its
2156    component types.  */
2157
2158 static int
2159 type_visibility (tree type)
2160 {
2161   int vis = VISIBILITY_DEFAULT;
2162   cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2163   return vis;
2164 }
2165
2166 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2167    specified (or if VISIBILITY is static).  If TMPL is true, this
2168    constraint is for a template argument, and takes precedence
2169    over explicitly-specified visibility on the template.  */
2170
2171 static void
2172 constrain_visibility (tree decl, int visibility, bool tmpl)
2173 {
2174   if (visibility == VISIBILITY_ANON)
2175     {
2176       /* extern "C" declarations aren't affected by the anonymous
2177          namespace.  */
2178       if (!DECL_EXTERN_C_P (decl))
2179         {
2180           TREE_PUBLIC (decl) = 0;
2181           DECL_WEAK (decl) = 0;
2182           DECL_COMMON (decl) = 0;
2183           DECL_COMDAT (decl) = false;
2184           if (TREE_CODE (decl) == FUNCTION_DECL
2185               || TREE_CODE (decl) == VAR_DECL)
2186             {
2187               struct symtab_node *snode = symtab_node::get (decl);
2188
2189               if (snode)
2190                 snode->set_comdat_group (NULL);
2191             }
2192           DECL_INTERFACE_KNOWN (decl) = 1;
2193           if (DECL_LANG_SPECIFIC (decl))
2194             DECL_NOT_REALLY_EXTERN (decl) = 1;
2195         }
2196     }
2197   else if (visibility > DECL_VISIBILITY (decl)
2198            && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2199     {
2200       DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2201       /* This visibility was not specified.  */
2202       DECL_VISIBILITY_SPECIFIED (decl) = false;
2203     }
2204 }
2205
2206 /* Constrain the visibility of DECL based on the visibility of its template
2207    arguments.  */
2208
2209 static void
2210 constrain_visibility_for_template (tree decl, tree targs)
2211 {
2212   /* If this is a template instantiation, check the innermost
2213      template args for visibility constraints.  The outer template
2214      args are covered by the class check.  */
2215   tree args = INNERMOST_TEMPLATE_ARGS (targs);
2216   int i;
2217   for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2218     {
2219       int vis = 0;
2220
2221       tree arg = TREE_VEC_ELT (args, i-1);
2222       if (TYPE_P (arg))
2223         vis = type_visibility (arg);
2224       else
2225         {
2226           if (REFERENCE_REF_P (arg))
2227             arg = TREE_OPERAND (arg, 0);
2228           if (TREE_TYPE (arg))
2229             STRIP_NOPS (arg);
2230           if (TREE_CODE (arg) == ADDR_EXPR)
2231             arg = TREE_OPERAND (arg, 0);
2232           if (VAR_OR_FUNCTION_DECL_P (arg))
2233             {
2234               if (! TREE_PUBLIC (arg))
2235                 vis = VISIBILITY_ANON;
2236               else
2237                 vis = DECL_VISIBILITY (arg);
2238             }
2239         }
2240       if (vis)
2241         constrain_visibility (decl, vis, true);
2242     }
2243 }
2244
2245 /* Like c_determine_visibility, but with additional C++-specific
2246    behavior.
2247
2248    Function-scope entities can rely on the function's visibility because
2249    it is set in start_preparsed_function.
2250
2251    Class-scope entities cannot rely on the class's visibility until the end
2252    of the enclosing class definition.
2253
2254    Note that because namespaces have multiple independent definitions,
2255    namespace visibility is handled elsewhere using the #pragma visibility
2256    machinery rather than by decorating the namespace declaration.
2257
2258    The goal is for constraints from the type to give a diagnostic, and
2259    other constraints to be applied silently.  */
2260
2261 void
2262 determine_visibility (tree decl)
2263 {
2264   tree class_type = NULL_TREE;
2265   bool use_template;
2266   bool orig_visibility_specified;
2267   enum symbol_visibility orig_visibility;
2268
2269   /* Remember that all decls get VISIBILITY_DEFAULT when built.  */
2270
2271   /* Only relevant for names with external linkage.  */
2272   if (!TREE_PUBLIC (decl))
2273     return;
2274
2275   /* Cloned constructors and destructors get the same visibility as
2276      the underlying function.  That should be set up in
2277      maybe_clone_body.  */
2278   gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2279
2280   orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2281   orig_visibility = DECL_VISIBILITY (decl);
2282
2283   if (TREE_CODE (decl) == TYPE_DECL)
2284     {
2285       if (CLASS_TYPE_P (TREE_TYPE (decl)))
2286         use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2287       else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2288         use_template = 1;
2289       else
2290         use_template = 0;
2291     }
2292   else if (DECL_LANG_SPECIFIC (decl))
2293     use_template = DECL_USE_TEMPLATE (decl);
2294   else
2295     use_template = 0;
2296
2297   /* If DECL is a member of a class, visibility specifiers on the
2298      class can influence the visibility of the DECL.  */
2299   if (DECL_CLASS_SCOPE_P (decl))
2300     class_type = DECL_CONTEXT (decl);
2301   else
2302     {
2303       /* Not a class member.  */
2304
2305       /* Virtual tables have DECL_CONTEXT set to their associated class,
2306          so they are automatically handled above.  */
2307       gcc_assert (!VAR_P (decl)
2308                   || !DECL_VTABLE_OR_VTT_P (decl));
2309
2310       if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2311         {
2312           /* Local statics and classes get the visibility of their
2313              containing function by default, except that
2314              -fvisibility-inlines-hidden doesn't affect them.  */
2315           tree fn = DECL_CONTEXT (decl);
2316           if (DECL_VISIBILITY_SPECIFIED (fn))
2317             {
2318               DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2319               DECL_VISIBILITY_SPECIFIED (decl) = 
2320                 DECL_VISIBILITY_SPECIFIED (fn);
2321             }
2322           else
2323             {
2324               if (DECL_CLASS_SCOPE_P (fn))
2325                 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2326               else if (determine_hidden_inline (fn))
2327                 {
2328                   DECL_VISIBILITY (decl) = default_visibility;
2329                   DECL_VISIBILITY_SPECIFIED (decl) =
2330                     visibility_options.inpragma;
2331                 }
2332               else
2333                 {
2334                   DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2335                   DECL_VISIBILITY_SPECIFIED (decl) =
2336                     DECL_VISIBILITY_SPECIFIED (fn);
2337                 }
2338             }
2339
2340           /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2341              but have no TEMPLATE_INFO, so don't try to check it.  */
2342           use_template = 0;
2343         }
2344       else if (VAR_P (decl) && DECL_TINFO_P (decl)
2345                && flag_visibility_ms_compat)
2346         {
2347           /* Under -fvisibility-ms-compat, types are visible by default,
2348              even though their contents aren't.  */
2349           tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2350           int underlying_vis = type_visibility (underlying_type);
2351           if (underlying_vis == VISIBILITY_ANON
2352               || (CLASS_TYPE_P (underlying_type)
2353                   && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2354             constrain_visibility (decl, underlying_vis, false);
2355           else
2356             DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2357         }
2358       else if (VAR_P (decl) && DECL_TINFO_P (decl))
2359         {
2360           /* tinfo visibility is based on the type it's for.  */
2361           constrain_visibility
2362             (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2363
2364           /* Give the target a chance to override the visibility associated
2365              with DECL.  */
2366           if (TREE_PUBLIC (decl)
2367               && !DECL_REALLY_EXTERN (decl)
2368               && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2369               && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2370             targetm.cxx.determine_class_data_visibility (decl);
2371         }
2372       else if (use_template)
2373         /* Template instantiations and specializations get visibility based
2374            on their template unless they override it with an attribute.  */;
2375       else if (! DECL_VISIBILITY_SPECIFIED (decl))
2376         {
2377           if (determine_hidden_inline (decl))
2378             DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2379           else
2380             {
2381               /* Set default visibility to whatever the user supplied with
2382                  #pragma GCC visibility or a namespace visibility attribute.  */
2383               DECL_VISIBILITY (decl) = default_visibility;
2384               DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2385             }
2386         }
2387     }
2388
2389   if (use_template)
2390     {
2391       /* If the specialization doesn't specify visibility, use the
2392          visibility from the template.  */
2393       tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2394                     ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2395                     : DECL_TEMPLATE_INFO (decl));
2396       tree args = TI_ARGS (tinfo);
2397       tree attribs = (TREE_CODE (decl) == TYPE_DECL
2398                       ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2399                       : DECL_ATTRIBUTES (decl));
2400       
2401       if (args != error_mark_node)
2402         {
2403           tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2404
2405           if (!DECL_VISIBILITY_SPECIFIED (decl))
2406             {
2407               if (!DECL_VISIBILITY_SPECIFIED (pattern)
2408                   && determine_hidden_inline (decl))
2409                 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2410               else
2411                 {
2412                   DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2413                   DECL_VISIBILITY_SPECIFIED (decl)
2414                     = DECL_VISIBILITY_SPECIFIED (pattern);
2415                 }
2416             }
2417
2418           if (args
2419               /* Template argument visibility outweighs #pragma or namespace
2420                  visibility, but not an explicit attribute.  */
2421               && !lookup_attribute ("visibility", attribs))
2422             {
2423               int depth = TMPL_ARGS_DEPTH (args);
2424               if (DECL_VISIBILITY_SPECIFIED (decl))
2425                 {
2426                   /* A class template member with explicit visibility
2427                      overrides the class visibility, so we need to apply
2428                      all the levels of template args directly.  */
2429                   int i;
2430                   for (i = 1; i <= depth; ++i)
2431                     {
2432                       tree lev = TMPL_ARGS_LEVEL (args, i);
2433                       constrain_visibility_for_template (decl, lev);
2434                     }
2435                 }
2436               else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2437                 /* Limit visibility based on its template arguments.  */
2438                 constrain_visibility_for_template (decl, args);
2439             }
2440         }
2441     }
2442
2443   if (class_type)
2444     determine_visibility_from_class (decl, class_type);
2445
2446   if (decl_anon_ns_mem_p (decl))
2447     /* Names in an anonymous namespace get internal linkage.
2448        This might change once we implement export.  */
2449     constrain_visibility (decl, VISIBILITY_ANON, false);
2450   else if (TREE_CODE (decl) != TYPE_DECL)
2451     {
2452       /* Propagate anonymity from type to decl.  */
2453       int tvis = type_visibility (TREE_TYPE (decl));
2454       if (tvis == VISIBILITY_ANON
2455           || ! DECL_VISIBILITY_SPECIFIED (decl))
2456         constrain_visibility (decl, tvis, false);
2457     }
2458   else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2459     /* DR 757: A type without linkage shall not be used as the type of a
2460        variable or function with linkage, unless
2461        o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2462        o the variable or function is not used (3.2 [basic.def.odr]) or is
2463        defined in the same translation unit.
2464
2465        Since non-extern "C" decls need to be defined in the same
2466        translation unit, we can make the type internal.  */
2467     constrain_visibility (decl, VISIBILITY_ANON, false);
2468
2469   /* If visibility changed and DECL already has DECL_RTL, ensure
2470      symbol flags are updated.  */
2471   if ((DECL_VISIBILITY (decl) != orig_visibility
2472        || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2473       && ((VAR_P (decl) && TREE_STATIC (decl))
2474           || TREE_CODE (decl) == FUNCTION_DECL)
2475       && DECL_RTL_SET_P (decl))
2476     make_decl_rtl (decl);
2477 }
2478
2479 /* By default, static data members and function members receive
2480    the visibility of their containing class.  */
2481
2482 static void
2483 determine_visibility_from_class (tree decl, tree class_type)
2484 {
2485   if (DECL_VISIBILITY_SPECIFIED (decl))
2486     return;
2487
2488   if (determine_hidden_inline (decl))
2489     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2490   else
2491     {
2492       /* Default to the class visibility.  */
2493       DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2494       DECL_VISIBILITY_SPECIFIED (decl)
2495         = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2496     }
2497
2498   /* Give the target a chance to override the visibility associated
2499      with DECL.  */
2500   if (VAR_P (decl)
2501       && (DECL_TINFO_P (decl)
2502           || (DECL_VTABLE_OR_VTT_P (decl)
2503               /* Construction virtual tables are not exported because
2504                  they cannot be referred to from other object files;
2505                  their name is not standardized by the ABI.  */
2506               && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2507       && TREE_PUBLIC (decl)
2508       && !DECL_REALLY_EXTERN (decl)
2509       && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2510     targetm.cxx.determine_class_data_visibility (decl);
2511 }
2512
2513 /* Returns true iff DECL is an inline that should get hidden visibility
2514    because of -fvisibility-inlines-hidden.  */
2515
2516 static bool
2517 determine_hidden_inline (tree decl)
2518 {
2519   return (visibility_options.inlines_hidden
2520           /* Don't do this for inline templates; specializations might not be
2521              inline, and we don't want them to inherit the hidden
2522              visibility.  We'll set it here for all inline instantiations.  */
2523           && !processing_template_decl
2524           && TREE_CODE (decl) == FUNCTION_DECL
2525           && DECL_DECLARED_INLINE_P (decl)
2526           && (! DECL_LANG_SPECIFIC (decl)
2527               || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2528 }
2529
2530 /* Constrain the visibility of a class TYPE based on the visibility of its
2531    field types.  Warn if any fields require lesser visibility.  */
2532
2533 void
2534 constrain_class_visibility (tree type)
2535 {
2536   tree binfo;
2537   tree t;
2538   int i;
2539
2540   int vis = type_visibility (type);
2541
2542   if (vis == VISIBILITY_ANON
2543       || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2544     return;
2545
2546   /* Don't warn about visibility if the class has explicit visibility.  */
2547   if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2548     vis = VISIBILITY_INTERNAL;
2549
2550   for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2551     if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2552       {
2553         tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2554         int subvis = type_visibility (ftype);
2555
2556         if (subvis == VISIBILITY_ANON)
2557           {
2558             if (!in_main_input_context ())
2559               warning (0, "\
2560 %qT has a field %qD whose type uses the anonymous namespace",
2561                        type, t);
2562           }
2563         else if (MAYBE_CLASS_TYPE_P (ftype)
2564                  && vis < VISIBILITY_HIDDEN
2565                  && subvis >= VISIBILITY_HIDDEN)
2566           warning (OPT_Wattributes, "\
2567 %qT declared with greater visibility than the type of its field %qD",
2568                    type, t);
2569       }
2570
2571   binfo = TYPE_BINFO (type);
2572   for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2573     {
2574       int subvis = type_visibility (TREE_TYPE (t));
2575
2576       if (subvis == VISIBILITY_ANON)
2577         {
2578           if (!in_main_input_context())
2579             warning (0, "\
2580 %qT has a base %qT whose type uses the anonymous namespace",
2581                      type, TREE_TYPE (t));
2582         }
2583       else if (vis < VISIBILITY_HIDDEN
2584                && subvis >= VISIBILITY_HIDDEN)
2585         warning (OPT_Wattributes, "\
2586 %qT declared with greater visibility than its base %qT",
2587                  type, TREE_TYPE (t));
2588     }
2589 }
2590
2591 /* Functions for adjusting the visibility of a tagged type and its nested
2592    types and declarations when it gets a name for linkage purposes from a
2593    typedef.  */
2594
2595 static void bt_reset_linkage_1 (binding_entry, void *);
2596 static void bt_reset_linkage_2 (binding_entry, void *);
2597
2598 /* First reset the visibility of all the types.  */
2599
2600 static void
2601 reset_type_linkage_1 (tree type)
2602 {
2603   set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2604   if (CLASS_TYPE_P (type))
2605     binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2606                            bt_reset_linkage_1, NULL);
2607 }
2608 static void
2609 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2610 {
2611   reset_type_linkage_1 (b->type);
2612 }
2613
2614 /* Then reset the visibility of any static data members or member
2615    functions that use those types.  */
2616
2617 static void
2618 reset_decl_linkage (tree decl)
2619 {
2620   if (TREE_PUBLIC (decl))
2621     return;
2622   if (DECL_CLONED_FUNCTION_P (decl))
2623     return;
2624   TREE_PUBLIC (decl) = true;
2625   DECL_INTERFACE_KNOWN (decl) = false;
2626   determine_visibility (decl);
2627   tentative_decl_linkage (decl);
2628 }
2629 static void
2630 reset_type_linkage_2 (tree type)
2631 {
2632   if (CLASS_TYPE_P (type))
2633     {
2634       if (tree vt = CLASSTYPE_VTABLES (type))
2635         {
2636           tree name = mangle_vtbl_for_type (type);
2637           DECL_NAME (vt) = name;
2638           SET_DECL_ASSEMBLER_NAME (vt, name);
2639           reset_decl_linkage (vt);
2640         }
2641       if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2642         {
2643           tree name = mangle_typeinfo_for_type (type);
2644           DECL_NAME (ti) = name;
2645           SET_DECL_ASSEMBLER_NAME (ti, name);
2646           TREE_TYPE (name) = type;
2647           reset_decl_linkage (ti);
2648         }
2649       for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2650         if (TREE_CODE (m) == VAR_DECL)
2651           reset_decl_linkage (m);
2652       for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2653         reset_decl_linkage (m);
2654       binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2655                              bt_reset_linkage_2, NULL);
2656     }
2657 }
2658 static void
2659 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2660 {
2661   reset_type_linkage_2 (b->type);
2662 }
2663 void
2664 reset_type_linkage (tree type)
2665 {
2666   reset_type_linkage_1 (type);
2667   reset_type_linkage_2 (type);
2668 }
2669
2670 /* Set up our initial idea of what the linkage of DECL should be.  */
2671
2672 void
2673 tentative_decl_linkage (tree decl)
2674 {
2675   if (DECL_INTERFACE_KNOWN (decl))
2676     /* We've already made a decision as to how this function will
2677        be handled.  */;
2678   else if (vague_linkage_p (decl))
2679     {
2680       if (TREE_CODE (decl) == FUNCTION_DECL
2681           && decl_defined_p (decl))
2682         {
2683           DECL_EXTERNAL (decl) = 1;
2684           DECL_NOT_REALLY_EXTERN (decl) = 1;
2685           note_vague_linkage_fn (decl);
2686           /* A non-template inline function with external linkage will
2687              always be COMDAT.  As we must eventually determine the
2688              linkage of all functions, and as that causes writes to
2689              the data mapped in from the PCH file, it's advantageous
2690              to mark the functions at this point.  */
2691           if (DECL_DECLARED_INLINE_P (decl)
2692               && (!DECL_IMPLICIT_INSTANTIATION (decl)
2693                   || DECL_DEFAULTED_FN (decl)))
2694             {
2695               /* This function must have external linkage, as
2696                  otherwise DECL_INTERFACE_KNOWN would have been
2697                  set.  */
2698               gcc_assert (TREE_PUBLIC (decl));
2699               comdat_linkage (decl);
2700               DECL_INTERFACE_KNOWN (decl) = 1;
2701             }
2702         }
2703       else if (TREE_CODE (decl) == VAR_DECL)
2704         maybe_commonize_var (decl);
2705     }
2706 }
2707
2708 /* DECL is a FUNCTION_DECL or VAR_DECL.  If the object file linkage
2709    for DECL has not already been determined, do so now by setting
2710    DECL_EXTERNAL, DECL_COMDAT and other related flags.  Until this
2711    function is called entities with vague linkage whose definitions
2712    are available must have TREE_PUBLIC set.
2713
2714    If this function decides to place DECL in COMDAT, it will set
2715    appropriate flags -- but will not clear DECL_EXTERNAL.  It is up to
2716    the caller to decide whether or not to clear DECL_EXTERNAL.  Some
2717    callers defer that decision until it is clear that DECL is actually
2718    required.  */
2719
2720 void
2721 import_export_decl (tree decl)
2722 {
2723   int emit_p;
2724   bool comdat_p;
2725   bool import_p;
2726   tree class_type = NULL_TREE;
2727
2728   if (DECL_INTERFACE_KNOWN (decl))
2729     return;
2730
2731   /* We cannot determine what linkage to give to an entity with vague
2732      linkage until the end of the file.  For example, a virtual table
2733      for a class will be defined if and only if the key method is
2734      defined in this translation unit.  As a further example, consider
2735      that when compiling a translation unit that uses PCH file with
2736      "-frepo" it would be incorrect to make decisions about what
2737      entities to emit when building the PCH; those decisions must be
2738      delayed until the repository information has been processed.  */
2739   gcc_assert (at_eof);
2740   /* Object file linkage for explicit instantiations is handled in
2741      mark_decl_instantiated.  For static variables in functions with
2742      vague linkage, maybe_commonize_var is used.
2743
2744      Therefore, the only declarations that should be provided to this
2745      function are those with external linkage that are:
2746
2747      * implicit instantiations of function templates
2748
2749      * inline function
2750
2751      * implicit instantiations of static data members of class
2752        templates
2753
2754      * virtual tables
2755
2756      * typeinfo objects
2757
2758      Furthermore, all entities that reach this point must have a
2759      definition available in this translation unit.
2760
2761      The following assertions check these conditions.  */
2762   gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2763   /* Any code that creates entities with TREE_PUBLIC cleared should
2764      also set DECL_INTERFACE_KNOWN.  */
2765   gcc_assert (TREE_PUBLIC (decl));
2766   if (TREE_CODE (decl) == FUNCTION_DECL)
2767     gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2768                 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2769                 || DECL_DECLARED_INLINE_P (decl));
2770   else
2771     gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2772                 || DECL_VTABLE_OR_VTT_P (decl)
2773                 || DECL_TINFO_P (decl));
2774   /* Check that a definition of DECL is available in this translation
2775      unit.  */
2776   gcc_assert (!DECL_REALLY_EXTERN (decl));
2777
2778   /* Assume that DECL will not have COMDAT linkage.  */
2779   comdat_p = false;
2780   /* Assume that DECL will not be imported into this translation
2781      unit.  */
2782   import_p = false;
2783
2784   /* See if the repository tells us whether or not to emit DECL in
2785      this translation unit.  */
2786   emit_p = repo_emit_p (decl);
2787   if (emit_p == 0)
2788     import_p = true;
2789   else if (emit_p == 1)
2790     {
2791       /* The repository indicates that this entity should be defined
2792          here.  Make sure the back end honors that request.  */
2793       mark_needed (decl);
2794       /* Output the definition as an ordinary strong definition.  */
2795       DECL_EXTERNAL (decl) = 0;
2796       DECL_INTERFACE_KNOWN (decl) = 1;
2797       return;
2798     }
2799
2800   if (import_p)
2801     /* We have already decided what to do with this DECL; there is no
2802        need to check anything further.  */
2803     ;
2804   else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2805     {
2806       class_type = DECL_CONTEXT (decl);
2807       import_export_class (class_type);
2808       if (TYPE_FOR_JAVA (class_type))
2809         import_p = true;
2810       else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2811                && CLASSTYPE_INTERFACE_ONLY (class_type))
2812         import_p = true;
2813       else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2814                && !CLASSTYPE_USE_TEMPLATE (class_type)
2815                && CLASSTYPE_KEY_METHOD (class_type)
2816                && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2817         /* The ABI requires that all virtual tables be emitted with
2818            COMDAT linkage.  However, on systems where COMDAT symbols
2819            don't show up in the table of contents for a static
2820            archive, or on systems without weak symbols (where we
2821            approximate COMDAT linkage by using internal linkage), the
2822            linker will report errors about undefined symbols because
2823            it will not see the virtual table definition.  Therefore,
2824            in the case that we know that the virtual table will be
2825            emitted in only one translation unit, we make the virtual
2826            table an ordinary definition with external linkage.  */
2827         DECL_EXTERNAL (decl) = 0;
2828       else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2829         {
2830           /* CLASS_TYPE is being exported from this translation unit,
2831              so DECL should be defined here.  */
2832           if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2833             /* If a class is declared in a header with the "extern
2834                template" extension, then it will not be instantiated,
2835                even in translation units that would normally require
2836                it.  Often such classes are explicitly instantiated in
2837                one translation unit.  Therefore, the explicit
2838                instantiation must be made visible to other translation
2839                units.  */
2840             DECL_EXTERNAL (decl) = 0;
2841           else
2842             {
2843               /* The generic C++ ABI says that class data is always
2844                  COMDAT, even if there is a key function.  Some
2845                  variants (e.g., the ARM EABI) says that class data
2846                  only has COMDAT linkage if the class data might be
2847                  emitted in more than one translation unit.  When the
2848                  key method can be inline and is inline, we still have
2849                  to arrange for comdat even though
2850                  class_data_always_comdat is false.  */
2851               if (!CLASSTYPE_KEY_METHOD (class_type)
2852                   || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2853                   || targetm.cxx.class_data_always_comdat ())
2854                 {
2855                   /* The ABI requires COMDAT linkage.  Normally, we
2856                      only emit COMDAT things when they are needed;
2857                      make sure that we realize that this entity is
2858                      indeed needed.  */
2859                   comdat_p = true;
2860                   mark_needed (decl);
2861                 }
2862             }
2863         }
2864       else if (!flag_implicit_templates
2865                && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2866         import_p = true;
2867       else
2868         comdat_p = true;
2869     }
2870   else if (VAR_P (decl) && DECL_TINFO_P (decl))
2871     {
2872       tree type = TREE_TYPE (DECL_NAME (decl));
2873       if (CLASS_TYPE_P (type))
2874         {
2875           class_type = type;
2876           import_export_class (type);
2877           if (CLASSTYPE_INTERFACE_KNOWN (type)
2878               && TYPE_POLYMORPHIC_P (type)
2879               && CLASSTYPE_INTERFACE_ONLY (type)
2880               /* If -fno-rtti was specified, then we cannot be sure
2881                  that RTTI information will be emitted with the
2882                  virtual table of the class, so we must emit it
2883                  wherever it is used.  */
2884               && flag_rtti)
2885             import_p = true;
2886           else
2887             {
2888               if (CLASSTYPE_INTERFACE_KNOWN (type)
2889                   && !CLASSTYPE_INTERFACE_ONLY (type))
2890                 {
2891                   comdat_p = (targetm.cxx.class_data_always_comdat ()
2892                               || (CLASSTYPE_KEY_METHOD (type)
2893                                   && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2894                   mark_needed (decl);
2895                   if (!flag_weak)
2896                     {
2897                       comdat_p = false;
2898                       DECL_EXTERNAL (decl) = 0;
2899                     }
2900                 }
2901               else
2902                 comdat_p = true;
2903             }
2904         }
2905       else
2906         comdat_p = true;
2907     }
2908   else if (DECL_TEMPLOID_INSTANTIATION (decl))
2909     {
2910       /* DECL is an implicit instantiation of a function or static
2911          data member.  */
2912       if ((flag_implicit_templates
2913            && !flag_use_repository)
2914           || (flag_implicit_inline_templates
2915               && TREE_CODE (decl) == FUNCTION_DECL
2916               && DECL_DECLARED_INLINE_P (decl)))
2917         comdat_p = true;
2918       else
2919         /* If we are not implicitly generating templates, then mark
2920            this entity as undefined in this translation unit.  */
2921         import_p = true;
2922     }
2923   else if (DECL_FUNCTION_MEMBER_P (decl))
2924     {
2925       if (!DECL_DECLARED_INLINE_P (decl))
2926         {
2927           tree ctype = DECL_CONTEXT (decl);
2928           import_export_class (ctype);
2929           if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2930             {
2931               DECL_NOT_REALLY_EXTERN (decl)
2932                 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2933                      || (DECL_DECLARED_INLINE_P (decl)
2934                          && ! flag_implement_inlines
2935                          && !DECL_VINDEX (decl)));
2936
2937               if (!DECL_NOT_REALLY_EXTERN (decl))
2938                 DECL_EXTERNAL (decl) = 1;
2939
2940               /* Always make artificials weak.  */
2941               if (DECL_ARTIFICIAL (decl) && flag_weak)
2942                 comdat_p = true;
2943               else
2944                 maybe_make_one_only (decl);
2945             }
2946         }
2947       else
2948         comdat_p = true;
2949     }
2950   else
2951     comdat_p = true;
2952
2953   if (import_p)
2954     {
2955       /* If we are importing DECL into this translation unit, mark is
2956          an undefined here.  */
2957       DECL_EXTERNAL (decl) = 1;
2958       DECL_NOT_REALLY_EXTERN (decl) = 0;
2959     }
2960   else if (comdat_p)
2961     {
2962       /* If we decided to put DECL in COMDAT, mark it accordingly at
2963          this point.  */
2964       comdat_linkage (decl);
2965     }
2966
2967   DECL_INTERFACE_KNOWN (decl) = 1;
2968 }
2969
2970 /* Return an expression that performs the destruction of DECL, which
2971    must be a VAR_DECL whose type has a non-trivial destructor, or is
2972    an array whose (innermost) elements have a non-trivial destructor.  */
2973
2974 tree
2975 build_cleanup (tree decl)
2976 {
2977   tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2978   gcc_assert (clean != NULL_TREE);
2979   return clean;
2980 }
2981
2982 /* Returns the initialization guard variable for the variable DECL,
2983    which has static storage duration.  */
2984
2985 tree
2986 get_guard (tree decl)
2987 {
2988   tree sname;
2989   tree guard;
2990
2991   sname = mangle_guard_variable (decl);
2992   guard = IDENTIFIER_GLOBAL_VALUE (sname);
2993   if (! guard)
2994     {
2995       tree guard_type;
2996
2997       /* We use a type that is big enough to contain a mutex as well
2998          as an integer counter.  */
2999       guard_type = targetm.cxx.guard_type ();
3000       guard = build_decl (DECL_SOURCE_LOCATION (decl),
3001                           VAR_DECL, sname, guard_type);
3002
3003       /* The guard should have the same linkage as what it guards.  */
3004       TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3005       TREE_STATIC (guard) = TREE_STATIC (decl);
3006       DECL_COMMON (guard) = DECL_COMMON (decl);
3007       DECL_COMDAT (guard) = DECL_COMDAT (decl);
3008       set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3009       if (DECL_ONE_ONLY (decl))
3010         make_decl_one_only (guard, cxx_comdat_group (guard));
3011       if (TREE_PUBLIC (decl))
3012         DECL_WEAK (guard) = DECL_WEAK (decl);
3013       DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3014       DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3015
3016       DECL_ARTIFICIAL (guard) = 1;
3017       DECL_IGNORED_P (guard) = 1;
3018       TREE_USED (guard) = 1;
3019       pushdecl_top_level_and_finish (guard, NULL_TREE);
3020     }
3021   return guard;
3022 }
3023
3024 /* Return those bits of the GUARD variable that should be set when the
3025    guarded entity is actually initialized.  */
3026
3027 static tree
3028 get_guard_bits (tree guard)
3029 {
3030   if (!targetm.cxx.guard_mask_bit ())
3031     {
3032       /* We only set the first byte of the guard, in order to leave room
3033          for a mutex in the high-order bits.  */
3034       guard = build1 (ADDR_EXPR,
3035                       build_pointer_type (TREE_TYPE (guard)),
3036                       guard);
3037       guard = build1 (NOP_EXPR,
3038                       build_pointer_type (char_type_node),
3039                       guard);
3040       guard = build1 (INDIRECT_REF, char_type_node, guard);
3041     }
3042
3043   return guard;
3044 }
3045
3046 /* Return an expression which determines whether or not the GUARD
3047    variable has already been initialized.  */
3048
3049 tree
3050 get_guard_cond (tree guard)
3051 {
3052   tree guard_value;
3053
3054   /* Check to see if the GUARD is zero.  */
3055   guard = get_guard_bits (guard);
3056
3057   /* Mask off all but the low bit.  */
3058   if (targetm.cxx.guard_mask_bit ())
3059     {
3060       guard_value = integer_one_node;
3061       if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3062         guard_value = convert (TREE_TYPE (guard), guard_value);
3063       guard = cp_build_binary_op (input_location,
3064                                   BIT_AND_EXPR, guard, guard_value,
3065                                   tf_warning_or_error);
3066     }
3067
3068   guard_value = integer_zero_node;
3069   if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3070     guard_value = convert (TREE_TYPE (guard), guard_value);
3071   return cp_build_binary_op (input_location,
3072                              EQ_EXPR, guard, guard_value,
3073                              tf_warning_or_error);
3074 }
3075
3076 /* Return an expression which sets the GUARD variable, indicating that
3077    the variable being guarded has been initialized.  */
3078
3079 tree
3080 set_guard (tree guard)
3081 {
3082   tree guard_init;
3083
3084   /* Set the GUARD to one.  */
3085   guard = get_guard_bits (guard);
3086   guard_init = integer_one_node;
3087   if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3088     guard_init = convert (TREE_TYPE (guard), guard_init);
3089   return cp_build_modify_expr (guard, NOP_EXPR, guard_init, 
3090                                tf_warning_or_error);
3091 }
3092
3093 /* Returns true iff we can tell that VAR does not have a dynamic
3094    initializer.  */
3095
3096 static bool
3097 var_defined_without_dynamic_init (tree var)
3098 {
3099   /* If it's defined in another TU, we can't tell.  */
3100   if (DECL_EXTERNAL (var))
3101     return false;
3102   /* If it has a non-trivial destructor, registering the destructor
3103      counts as dynamic initialization.  */
3104   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3105     return false;
3106   /* If it's in this TU, its initializer has been processed, unless
3107      it's a case of self-initialization, then DECL_INITIALIZED_P is
3108      false while the initializer is handled by finish_id_expression.  */
3109   if (!DECL_INITIALIZED_P (var))
3110     return false;
3111   /* If it has no initializer or a constant one, it's not dynamic.  */
3112   return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3113           || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3114 }
3115
3116 /* Returns true iff VAR is a variable that needs uses to be
3117    wrapped for possible dynamic initialization.  */
3118
3119 static bool
3120 var_needs_tls_wrapper (tree var)
3121 {
3122   return (!error_operand_p (var)
3123           && DECL_THREAD_LOCAL_P (var)
3124           && !DECL_GNU_TLS_P (var)
3125           && !DECL_FUNCTION_SCOPE_P (var)
3126           && !var_defined_without_dynamic_init (var));
3127 }
3128
3129 /* Get the FUNCTION_DECL for the shared TLS init function for this
3130    translation unit.  */
3131
3132 static tree
3133 get_local_tls_init_fn (void)
3134 {
3135   tree sname = get_identifier ("__tls_init");
3136   tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3137   if (!fn)
3138     {
3139       fn = build_lang_decl (FUNCTION_DECL, sname,
3140                              build_function_type (void_type_node,
3141                                                   void_list_node));
3142       SET_DECL_LANGUAGE (fn, lang_c);
3143       TREE_PUBLIC (fn) = false;
3144       DECL_ARTIFICIAL (fn) = true;
3145       mark_used (fn);
3146       SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3147     }
3148   return fn;
3149 }
3150
3151 /* Get a FUNCTION_DECL for the init function for the thread_local
3152    variable VAR.  The init function will be an alias to the function
3153    that initializes all the non-local TLS variables in the translation
3154    unit.  The init function is only used by the wrapper function.  */
3155
3156 static tree
3157 get_tls_init_fn (tree var)
3158 {
3159   /* Only C++11 TLS vars need this init fn.  */
3160   if (!var_needs_tls_wrapper (var))
3161     return NULL_TREE;
3162
3163   /* If -fno-extern-tls-init, assume that we don't need to call
3164      a tls init function for a variable defined in another TU.  */
3165   if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3166     return NULL_TREE;
3167
3168 #ifdef ASM_OUTPUT_DEF
3169   /* If the variable is internal, or if we can't generate aliases,
3170      call the local init function directly.  */
3171   if (!TREE_PUBLIC (var))
3172 #endif
3173     return get_local_tls_init_fn ();
3174
3175   tree sname = mangle_tls_init_fn (var);
3176   tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3177   if (!fn)
3178     {
3179       fn = build_lang_decl (FUNCTION_DECL, sname,
3180                             build_function_type (void_type_node,
3181                                                  void_list_node));
3182       SET_DECL_LANGUAGE (fn, lang_c);
3183       TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3184       DECL_ARTIFICIAL (fn) = true;
3185       DECL_COMDAT (fn) = DECL_COMDAT (var);
3186       DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3187       if (DECL_ONE_ONLY (var))
3188         make_decl_one_only (fn, cxx_comdat_group (fn));
3189       if (TREE_PUBLIC (var))
3190         {
3191           tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3192           /* If the variable is defined somewhere else and might have static
3193              initialization, make the init function a weak reference.  */
3194           if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3195                || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3196               && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3197               && DECL_EXTERNAL (var))
3198             declare_weak (fn);
3199           else
3200             DECL_WEAK (fn) = DECL_WEAK (var);
3201         }
3202       DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3203       DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3204       DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3205       DECL_IGNORED_P (fn) = 1;
3206       mark_used (fn);
3207
3208       DECL_BEFRIENDING_CLASSES (fn) = var;
3209
3210       SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3211     }
3212   return fn;
3213 }
3214
3215 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3216    variable VAR.  The wrapper function calls the init function (if any) for
3217    VAR and then returns a reference to VAR.  The wrapper function is used
3218    in place of VAR everywhere VAR is mentioned.  */
3219
3220 tree
3221 get_tls_wrapper_fn (tree var)
3222 {
3223   /* Only C++11 TLS vars need this wrapper fn.  */
3224   if (!var_needs_tls_wrapper (var))
3225     return NULL_TREE;
3226
3227   tree sname = mangle_tls_wrapper_fn (var);
3228   tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3229   if (!fn)
3230     {
3231       /* A named rvalue reference is an lvalue, so the wrapper should
3232          always return an lvalue reference.  */
3233       tree type = non_reference (TREE_TYPE (var));
3234       type = build_reference_type (type);
3235       tree fntype = build_function_type (type, void_list_node);
3236       fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3237       SET_DECL_LANGUAGE (fn, lang_c);
3238       TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3239       DECL_ARTIFICIAL (fn) = true;
3240       DECL_IGNORED_P (fn) = 1;
3241       /* The wrapper is inline and emitted everywhere var is used.  */
3242       DECL_DECLARED_INLINE_P (fn) = true;
3243       if (TREE_PUBLIC (var))
3244         {
3245           comdat_linkage (fn);
3246 #ifdef HAVE_GAS_HIDDEN
3247           /* Make the wrapper bind locally; there's no reason to share
3248              the wrapper between multiple shared objects.  */
3249           DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3250           DECL_VISIBILITY_SPECIFIED (fn) = true;
3251 #endif
3252         }
3253       if (!TREE_PUBLIC (fn))
3254         DECL_INTERFACE_KNOWN (fn) = true;
3255       mark_used (fn);
3256       note_vague_linkage_fn (fn);
3257
3258 #if 0
3259       /* We want CSE to commonize calls to the wrapper, but marking it as
3260          pure is unsafe since it has side-effects.  I guess we need a new
3261          ECF flag even weaker than ECF_PURE.  FIXME!  */
3262       DECL_PURE_P (fn) = true;
3263 #endif
3264
3265       DECL_BEFRIENDING_CLASSES (fn) = var;
3266
3267       SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3268     }
3269   return fn;
3270 }
3271
3272 /* At EOF, generate the definition for the TLS wrapper function FN:
3273
3274    T& var_wrapper() {
3275      if (init_fn) init_fn();
3276      return var;
3277    }  */
3278
3279 static void
3280 generate_tls_wrapper (tree fn)
3281 {
3282   tree var = DECL_BEFRIENDING_CLASSES (fn);
3283
3284   start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3285   tree body = begin_function_body ();
3286   /* Only call the init fn if there might be one.  */
3287   if (tree init_fn = get_tls_init_fn (var))
3288     {
3289       tree if_stmt = NULL_TREE;
3290       /* If init_fn is a weakref, make sure it exists before calling.  */
3291       if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3292         {
3293           if_stmt = begin_if_stmt ();
3294           tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3295           tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3296                                           NE_EXPR, addr, nullptr_node,
3297                                           tf_warning_or_error);
3298           finish_if_stmt_cond (cond, if_stmt);
3299         }
3300       finish_expr_stmt (build_cxx_call
3301                         (init_fn, 0, NULL, tf_warning_or_error));
3302       if (if_stmt)
3303         {
3304           finish_then_clause (if_stmt);
3305           finish_if_stmt (if_stmt);
3306         }
3307     }
3308   else
3309     /* If there's no initialization, the wrapper is a constant function.  */
3310     TREE_READONLY (fn) = true;
3311   finish_return_stmt (convert_from_reference (var));
3312   finish_function_body (body);
3313   expand_or_defer_fn (finish_function (0));
3314 }
3315
3316 /* Start the process of running a particular set of global constructors
3317    or destructors.  Subroutine of do_[cd]tors.  Also called from
3318    vtv_start_verification_constructor_init_function.  */
3319
3320 static tree
3321 start_objects (int method_type, int initp)
3322 {
3323   tree body;
3324   tree fndecl;
3325   char type[14];
3326
3327   /* Make ctor or dtor function.  METHOD_TYPE may be 'I' or 'D'.  */
3328
3329   if (initp != DEFAULT_INIT_PRIORITY)
3330     {
3331       char joiner;
3332
3333 #ifdef JOINER
3334       joiner = JOINER;
3335 #else
3336       joiner = '_';
3337 #endif
3338
3339       sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3340     }
3341   else
3342     sprintf (type, "sub_%c", method_type);
3343
3344   fndecl = build_lang_decl (FUNCTION_DECL,
3345                             get_file_function_name (type),
3346                             build_function_type_list (void_type_node,
3347                                                       NULL_TREE));
3348   start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3349
3350   TREE_PUBLIC (current_function_decl) = 0;
3351
3352   /* Mark as artificial because it's not explicitly in the user's
3353      source code.  */
3354   DECL_ARTIFICIAL (current_function_decl) = 1;
3355
3356   /* Mark this declaration as used to avoid spurious warnings.  */
3357   TREE_USED (current_function_decl) = 1;
3358
3359   /* Mark this function as a global constructor or destructor.  */
3360   if (method_type == 'I')
3361     DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3362   else
3363     DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3364
3365   body = begin_compound_stmt (BCS_FN_BODY);
3366
3367   return body;
3368 }
3369
3370 /* Finish the process of running a particular set of global constructors
3371    or destructors.  Subroutine of do_[cd]tors.  */
3372
3373 static void
3374 finish_objects (int method_type, int initp, tree body)
3375 {
3376   tree fn;
3377
3378   /* Finish up.  */
3379   finish_compound_stmt (body);
3380   fn = finish_function (0);
3381
3382   if (method_type == 'I')
3383     {
3384       DECL_STATIC_CONSTRUCTOR (fn) = 1;
3385       decl_init_priority_insert (fn, initp);
3386     }
3387   else
3388     {
3389       DECL_STATIC_DESTRUCTOR (fn) = 1;
3390       decl_fini_priority_insert (fn, initp);
3391     }
3392
3393   expand_or_defer_fn (fn);
3394 }
3395
3396 /* The names of the parameters to the function created to handle
3397    initializations and destructions for objects with static storage
3398    duration.  */
3399 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3400 #define PRIORITY_IDENTIFIER "__priority"
3401
3402 /* The name of the function we create to handle initializations and
3403    destructions for objects with static storage duration.  */
3404 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3405
3406 /* The declaration for the __INITIALIZE_P argument.  */
3407 static GTY(()) tree initialize_p_decl;
3408
3409 /* The declaration for the __PRIORITY argument.  */
3410 static GTY(()) tree priority_decl;
3411
3412 /* The declaration for the static storage duration function.  */
3413 static GTY(()) tree ssdf_decl;
3414
3415 /* All the static storage duration functions created in this
3416    translation unit.  */
3417 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3418
3419 /* A map from priority levels to information about that priority
3420    level.  There may be many such levels, so efficient lookup is
3421    important.  */
3422 static splay_tree priority_info_map;
3423
3424 /* Begins the generation of the function that will handle all
3425    initialization and destruction of objects with static storage
3426    duration.  The function generated takes two parameters of type
3427    `int': __INITIALIZE_P and __PRIORITY.  If __INITIALIZE_P is
3428    nonzero, it performs initializations.  Otherwise, it performs
3429    destructions.  It only performs those initializations or
3430    destructions with the indicated __PRIORITY.  The generated function
3431    returns no value.
3432
3433    It is assumed that this function will only be called once per
3434    translation unit.  */
3435
3436 static tree
3437 start_static_storage_duration_function (unsigned count)
3438 {
3439   tree type;
3440   tree body;
3441   char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3442
3443   /* Create the identifier for this function.  It will be of the form
3444      SSDF_IDENTIFIER_<number>.  */
3445   sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3446
3447   type = build_function_type_list (void_type_node,
3448                                    integer_type_node, integer_type_node,
3449                                    NULL_TREE);
3450
3451   /* Create the FUNCTION_DECL itself.  */
3452   ssdf_decl = build_lang_decl (FUNCTION_DECL,
3453                                get_identifier (id),
3454                                type);
3455   TREE_PUBLIC (ssdf_decl) = 0;
3456   DECL_ARTIFICIAL (ssdf_decl) = 1;
3457
3458   /* Put this function in the list of functions to be called from the
3459      static constructors and destructors.  */
3460   if (!ssdf_decls)
3461     {
3462       vec_alloc (ssdf_decls, 32);
3463
3464       /* Take this opportunity to initialize the map from priority
3465          numbers to information about that priority level.  */
3466       priority_info_map = splay_tree_new (splay_tree_compare_ints,
3467                                           /*delete_key_fn=*/0,
3468                                           /*delete_value_fn=*/
3469                                           (splay_tree_delete_value_fn) &free);
3470
3471       /* We always need to generate functions for the
3472          DEFAULT_INIT_PRIORITY so enter it now.  That way when we walk
3473          priorities later, we'll be sure to find the
3474          DEFAULT_INIT_PRIORITY.  */
3475       get_priority_info (DEFAULT_INIT_PRIORITY);
3476     }
3477
3478   vec_safe_push (ssdf_decls, ssdf_decl);
3479
3480   /* Create the argument list.  */
3481   initialize_p_decl = cp_build_parm_decl
3482     (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3483   DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3484   TREE_USED (initialize_p_decl) = 1;
3485   priority_decl = cp_build_parm_decl
3486     (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3487   DECL_CONTEXT (priority_decl) = ssdf_decl;
3488   TREE_USED (priority_decl) = 1;
3489
3490   DECL_CHAIN (initialize_p_decl) = priority_decl;
3491   DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3492
3493   /* Put the function in the global scope.  */
3494   pushdecl (ssdf_decl);
3495
3496   /* Start the function itself.  This is equivalent to declaring the
3497      function as:
3498
3499        static void __ssdf (int __initialize_p, init __priority_p);
3500
3501      It is static because we only need to call this function from the
3502      various constructor and destructor functions for this module.  */
3503   start_preparsed_function (ssdf_decl,
3504                             /*attrs=*/NULL_TREE,
3505                             SF_PRE_PARSED);
3506
3507   /* Set up the scope of the outermost block in the function.  */
3508   body = begin_compound_stmt (BCS_FN_BODY);
3509
3510   return body;
3511 }
3512
3513 /* Finish the generation of the function which performs initialization
3514    and destruction of objects with static storage duration.  After
3515    this point, no more such objects can be created.  */
3516
3517 static void
3518 finish_static_storage_duration_function (tree body)
3519 {
3520   /* Close out the function.  */
3521   finish_compound_stmt (body);
3522   expand_or_defer_fn (finish_function (0));
3523 }
3524
3525 /* Return the information about the indicated PRIORITY level.  If no
3526    code to handle this level has yet been generated, generate the
3527    appropriate prologue.  */
3528
3529 static priority_info
3530 get_priority_info (int priority)
3531 {
3532   priority_info pi;
3533   splay_tree_node n;
3534
3535   n = splay_tree_lookup (priority_info_map,
3536                          (splay_tree_key) priority);
3537   if (!n)
3538     {
3539       /* Create a new priority information structure, and insert it
3540          into the map.  */
3541       pi = XNEW (struct priority_info_s);
3542       pi->initializations_p = 0;
3543       pi->destructions_p = 0;
3544       splay_tree_insert (priority_info_map,
3545                          (splay_tree_key) priority,
3546                          (splay_tree_value) pi);
3547     }
3548   else
3549     pi = (priority_info) n->value;
3550
3551   return pi;
3552 }
3553
3554 /* The effective initialization priority of a DECL.  */
3555
3556 #define DECL_EFFECTIVE_INIT_PRIORITY(decl)                                    \
3557         ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3558          ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3559
3560 /* Whether a DECL needs a guard to protect it against multiple
3561    initialization.  */
3562
3563 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl)      \
3564                                                     || DECL_ONE_ONLY (decl) \
3565                                                     || DECL_WEAK (decl)))
3566
3567 /* Called from one_static_initialization_or_destruction(),
3568    via walk_tree.
3569    Walks the initializer list of a global variable and looks for
3570    temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3571    and that have their DECL_CONTEXT() == NULL.
3572    For each such temporary variable, set their DECL_CONTEXT() to
3573    the current function. This is necessary because otherwise
3574    some optimizers (enabled by -O2 -fprofile-arcs) might crash
3575    when trying to refer to a temporary variable that does not have
3576    it's DECL_CONTECT() properly set.  */
3577 static tree 
3578 fix_temporary_vars_context_r (tree *node,
3579                               int  * /*unused*/,
3580                               void * /*unused1*/)
3581 {
3582   gcc_assert (current_function_decl);
3583
3584   if (TREE_CODE (*node) == BIND_EXPR)
3585     {
3586       tree var;
3587
3588       for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3589         if (VAR_P (var)
3590           && !DECL_NAME (var)
3591           && DECL_ARTIFICIAL (var)
3592           && !DECL_CONTEXT (var))
3593           DECL_CONTEXT (var) = current_function_decl;
3594     }
3595
3596   return NULL_TREE;
3597 }
3598
3599 /* Set up to handle the initialization or destruction of DECL.  If
3600    INITP is nonzero, we are initializing the variable.  Otherwise, we
3601    are destroying it.  */
3602
3603 static void
3604 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3605 {
3606   tree guard_if_stmt = NULL_TREE;
3607   tree guard;
3608
3609   /* If we are supposed to destruct and there's a trivial destructor,
3610      nothing has to be done.  */
3611   if (!initp
3612       && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3613     return;
3614
3615   /* Trick the compiler into thinking we are at the file and line
3616      where DECL was declared so that error-messages make sense, and so
3617      that the debugger will show somewhat sensible file and line
3618      information.  */
3619   input_location = DECL_SOURCE_LOCATION (decl);
3620
3621   /* Make sure temporary variables in the initialiser all have
3622      their DECL_CONTEXT() set to a value different from NULL_TREE.
3623      This can happen when global variables initialisers are built.
3624      In that case, the DECL_CONTEXT() of the global variables _AND_ of all 
3625      the temporary variables that might have been generated in the
3626      accompagning initialisers is NULL_TREE, meaning the variables have been
3627      declared in the global namespace.
3628      What we want to do here is to fix that and make sure the DECL_CONTEXT()
3629      of the temporaries are set to the current function decl.  */
3630   cp_walk_tree_without_duplicates (&init,
3631                                    fix_temporary_vars_context_r,
3632                                    NULL);
3633
3634   /* Because of:
3635
3636        [class.access.spec]
3637
3638        Access control for implicit calls to the constructors,
3639        the conversion functions, or the destructor called to
3640        create and destroy a static data member is performed as
3641        if these calls appeared in the scope of the member's
3642        class.
3643
3644      we pretend we are in a static member function of the class of
3645      which the DECL is a member.  */
3646   if (member_p (decl))
3647     {
3648       DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3649       DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3650     }
3651
3652   /* Assume we don't need a guard.  */
3653   guard = NULL_TREE;
3654   /* We need a guard if this is an object with external linkage that
3655      might be initialized in more than one place.  (For example, a
3656      static data member of a template, when the data member requires
3657      construction.)  */
3658   if (NEEDS_GUARD_P (decl))
3659     {
3660       tree guard_cond;
3661
3662       guard = get_guard (decl);
3663
3664       /* When using __cxa_atexit, we just check the GUARD as we would
3665          for a local static.  */
3666       if (flag_use_cxa_atexit)
3667         {
3668           /* When using __cxa_atexit, we never try to destroy
3669              anything from a static destructor.  */
3670           gcc_assert (initp);
3671           guard_cond = get_guard_cond (guard);
3672         }
3673       /* If we don't have __cxa_atexit, then we will be running
3674          destructors from .fini sections, or their equivalents.  So,
3675          we need to know how many times we've tried to initialize this
3676          object.  We do initializations only if the GUARD is zero,
3677          i.e., if we are the first to initialize the variable.  We do
3678          destructions only if the GUARD is one, i.e., if we are the
3679          last to destroy the variable.  */
3680       else if (initp)
3681         guard_cond
3682           = cp_build_binary_op (input_location,
3683                                 EQ_EXPR,
3684                                 cp_build_unary_op (PREINCREMENT_EXPR,
3685                                                    guard,
3686                                                    /*noconvert=*/1,
3687                                                    tf_warning_or_error),
3688                                 integer_one_node,
3689                                 tf_warning_or_error);
3690       else
3691         guard_cond
3692           = cp_build_binary_op (input_location,
3693                                 EQ_EXPR,
3694                                 cp_build_unary_op (PREDECREMENT_EXPR,
3695                                                    guard,
3696                                                    /*noconvert=*/1,
3697                                                    tf_warning_or_error),
3698                                 integer_zero_node,
3699                                 tf_warning_or_error);
3700
3701       guard_if_stmt = begin_if_stmt ();
3702       finish_if_stmt_cond (guard_cond, guard_if_stmt);
3703     }
3704
3705
3706   /* If we're using __cxa_atexit, we have not already set the GUARD,
3707      so we must do so now.  */
3708   if (guard && initp && flag_use_cxa_atexit)
3709     finish_expr_stmt (set_guard (guard));
3710
3711   /* Perform the initialization or destruction.  */
3712   if (initp)
3713     {
3714       if (init)
3715         {
3716           finish_expr_stmt (init);
3717           if (flag_sanitize & SANITIZE_ADDRESS)
3718             {
3719               varpool_node *vnode = varpool_node::get (decl);
3720               if (vnode)
3721                 vnode->dynamically_initialized = 1;
3722             }
3723         }
3724
3725       /* If we're using __cxa_atexit, register a function that calls the
3726          destructor for the object.  */
3727       if (flag_use_cxa_atexit)
3728         finish_expr_stmt (register_dtor_fn (decl));
3729     }
3730   else
3731     finish_expr_stmt (build_cleanup (decl));
3732
3733   /* Finish the guard if-stmt, if necessary.  */
3734   if (guard)
3735     {
3736       finish_then_clause (guard_if_stmt);
3737       finish_if_stmt (guard_if_stmt);
3738     }
3739
3740   /* Now that we're done with DECL we don't need to pretend to be a
3741      member of its class any longer.  */
3742   DECL_CONTEXT (current_function_decl) = NULL_TREE;
3743   DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3744 }
3745
3746 /* Generate code to do the initialization or destruction of the decls in VARS,
3747    a TREE_LIST of VAR_DECL with static storage duration.
3748    Whether initialization or destruction is performed is specified by INITP.  */
3749
3750 static void
3751 do_static_initialization_or_destruction (tree vars, bool initp)
3752 {
3753   tree node, init_if_stmt, cond;
3754
3755   /* Build the outer if-stmt to check for initialization or destruction.  */
3756   init_if_stmt = begin_if_stmt ();
3757   cond = initp ? integer_one_node : integer_zero_node;
3758   cond = cp_build_binary_op (input_location,
3759                              EQ_EXPR,
3760                              initialize_p_decl,
3761                              cond,
3762                              tf_warning_or_error);
3763   finish_if_stmt_cond (cond, init_if_stmt);
3764
3765   /* To make sure dynamic construction doesn't access globals from other
3766      compilation units where they might not be yet constructed, for
3767      -fsanitize=address insert __asan_before_dynamic_init call that
3768      prevents access to either all global variables that need construction
3769      in other compilation units, or at least those that haven't been
3770      initialized yet.  Variables that need dynamic construction in
3771      the current compilation unit are kept accessible.  */
3772   if (flag_sanitize & SANITIZE_ADDRESS)
3773     finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3774
3775   node = vars;
3776   do {
3777     tree decl = TREE_VALUE (node);
3778     tree priority_if_stmt;
3779     int priority;
3780     priority_info pi;
3781
3782     /* If we don't need a destructor, there's nothing to do.  Avoid
3783        creating a possibly empty if-stmt.  */
3784     if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3785       {
3786         node = TREE_CHAIN (node);
3787         continue;
3788       }
3789
3790     /* Remember that we had an initialization or finalization at this
3791        priority.  */
3792     priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3793     pi = get_priority_info (priority);
3794     if (initp)
3795       pi->initializations_p = 1;
3796     else
3797       pi->destructions_p = 1;
3798
3799     /* Conditionalize this initialization on being in the right priority
3800        and being initializing/finalizing appropriately.  */
3801     priority_if_stmt = begin_if_stmt ();
3802     cond = cp_build_binary_op (input_location,
3803                                EQ_EXPR,
3804                                priority_decl,
3805                                build_int_cst (NULL_TREE, priority),
3806                                tf_warning_or_error);
3807     finish_if_stmt_cond (cond, priority_if_stmt);
3808
3809     /* Process initializers with same priority.  */
3810     for (; node
3811            && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3812          node = TREE_CHAIN (node))
3813       /* Do one initialization or destruction.  */
3814       one_static_initialization_or_destruction (TREE_VALUE (node),
3815                                                 TREE_PURPOSE (node), initp);
3816
3817     /* Finish up the priority if-stmt body.  */
3818     finish_then_clause (priority_if_stmt);
3819     finish_if_stmt (priority_if_stmt);
3820
3821   } while (node);
3822
3823   /* Revert what __asan_before_dynamic_init did by calling
3824      __asan_after_dynamic_init.  */
3825   if (flag_sanitize & SANITIZE_ADDRESS)
3826     finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3827
3828   /* Finish up the init/destruct if-stmt body.  */
3829   finish_then_clause (init_if_stmt);
3830   finish_if_stmt (init_if_stmt);
3831 }
3832
3833 /* VARS is a list of variables with static storage duration which may
3834    need initialization and/or finalization.  Remove those variables
3835    that don't really need to be initialized or finalized, and return
3836    the resulting list.  The order in which the variables appear in
3837    VARS is in reverse order of the order in which they should actually
3838    be initialized.  The list we return is in the unreversed order;
3839    i.e., the first variable should be initialized first.  */
3840
3841 static tree
3842 prune_vars_needing_no_initialization (tree *vars)
3843 {
3844   tree *var = vars;
3845   tree result = NULL_TREE;
3846
3847   while (*var)
3848     {
3849       tree t = *var;
3850       tree decl = TREE_VALUE (t);
3851       tree init = TREE_PURPOSE (t);
3852
3853       /* Deal gracefully with error.  */
3854       if (decl == error_mark_node)
3855         {
3856           var = &TREE_CHAIN (t);
3857           continue;
3858         }
3859
3860       /* The only things that can be initialized are variables.  */
3861       gcc_assert (VAR_P (decl));
3862
3863       /* If this object is not defined, we don't need to do anything
3864          here.  */
3865       if (DECL_EXTERNAL (decl))
3866         {
3867           var = &TREE_CHAIN (t);
3868           continue;
3869         }
3870
3871       /* Also, if the initializer already contains errors, we can bail
3872          out now.  */
3873       if (init && TREE_CODE (init) == TREE_LIST
3874           && value_member (error_mark_node, init))
3875         {
3876           var = &TREE_CHAIN (t);
3877           continue;
3878         }
3879
3880       /* This variable is going to need initialization and/or
3881          finalization, so we add it to the list.  */
3882       *var = TREE_CHAIN (t);
3883       TREE_CHAIN (t) = result;
3884       result = t;
3885     }
3886
3887   return result;
3888 }
3889
3890 /* Make sure we have told the back end about all the variables in
3891    VARS.  */
3892
3893 static void
3894 write_out_vars (tree vars)
3895 {
3896   tree v;
3897
3898   for (v = vars; v; v = TREE_CHAIN (v))
3899     {
3900       tree var = TREE_VALUE (v);
3901       if (!var_finalized_p (var))
3902         {
3903           import_export_decl (var);
3904           rest_of_decl_compilation (var, 1, 1);
3905         }
3906     }
3907 }
3908
3909 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3910    (otherwise) that will initialize all global objects with static
3911    storage duration having the indicated PRIORITY.  */
3912
3913 static void
3914 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3915                                 location_t *locus)
3916 {
3917   char function_key;
3918   tree fndecl;
3919   tree body;
3920   size_t i;
3921
3922   input_location = *locus;
3923   /* ??? */
3924   /* Was: locus->line++; */
3925
3926   /* We use `I' to indicate initialization and `D' to indicate
3927      destruction.  */
3928   function_key = constructor_p ? 'I' : 'D';
3929
3930   /* We emit the function lazily, to avoid generating empty
3931      global constructors and destructors.  */
3932   body = NULL_TREE;
3933
3934   /* For Objective-C++, we may need to initialize metadata found in this module.
3935      This must be done _before_ any other static initializations.  */
3936   if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3937       && constructor_p && objc_static_init_needed_p ())
3938     {
3939       body = start_objects (function_key, priority);
3940       objc_generate_static_init_call (NULL_TREE);
3941     }
3942
3943   /* Call the static storage duration function with appropriate
3944      arguments.  */
3945   FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3946     {
3947       /* Calls to pure or const functions will expand to nothing.  */
3948       if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3949         {
3950           tree call;
3951
3952           if (! body)
3953             body = start_objects (function_key, priority);
3954
3955           call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3956                                               build_int_cst (NULL_TREE,
3957                                                              constructor_p),
3958                                               build_int_cst (NULL_TREE,
3959                                                              priority),
3960                                               NULL_TREE);
3961           finish_expr_stmt (call);
3962         }
3963     }
3964
3965   /* Close out the function.  */
3966   if (body)
3967     finish_objects (function_key, priority, body);
3968 }
3969
3970 /* Generate constructor and destructor functions for the priority
3971    indicated by N.  */
3972
3973 static int
3974 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3975 {
3976   location_t *locus = (location_t *) data;
3977   int priority = (int) n->key;
3978   priority_info pi = (priority_info) n->value;
3979
3980   /* Generate the functions themselves, but only if they are really
3981      needed.  */
3982   if (pi->initializations_p)
3983     generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3984   if (pi->destructions_p)
3985     generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3986
3987   /* Keep iterating.  */
3988   return 0;
3989 }
3990
3991 /* Java requires that we be able to reference a local address for a
3992    method, and not be confused by PLT entries.  If hidden aliases are
3993    supported, collect and return all the functions for which we should
3994    emit a hidden alias.  */
3995
3996 static hash_set<tree> *
3997 collect_candidates_for_java_method_aliases (void)
3998 {
3999   struct cgraph_node *node;
4000   hash_set<tree> *candidates = NULL;
4001
4002 #ifndef HAVE_GAS_HIDDEN
4003   return candidates;
4004 #endif
4005
4006   FOR_EACH_FUNCTION (node)
4007     {
4008       tree fndecl = node->decl;
4009
4010       if (DECL_CLASS_SCOPE_P (fndecl)
4011           && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
4012           && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
4013         {
4014           if (candidates == NULL)
4015             candidates = new hash_set<tree>;
4016           candidates->add (fndecl);
4017         }
4018     }
4019
4020   return candidates;
4021 }
4022
4023
4024 /* Java requires that we be able to reference a local address for a
4025    method, and not be confused by PLT entries.  If hidden aliases are
4026    supported, emit one for each java function that we've emitted.
4027    CANDIDATES is the set of FUNCTION_DECLs that were gathered
4028    by collect_candidates_for_java_method_aliases.  */
4029
4030 static void
4031 build_java_method_aliases (hash_set<tree> *candidates)
4032 {
4033   struct cgraph_node *node;
4034
4035 #ifndef HAVE_GAS_HIDDEN
4036   return;
4037 #endif
4038
4039   FOR_EACH_FUNCTION (node)
4040     {
4041       tree fndecl = node->decl;
4042
4043       if (TREE_ASM_WRITTEN (fndecl)
4044           && candidates->contains (fndecl))
4045         {
4046           /* Mangle the name in a predictable way; we need to reference
4047              this from a java compiled object file.  */
4048           tree oid, nid, alias;
4049           const char *oname;
4050           char *nname;
4051
4052           oid = DECL_ASSEMBLER_NAME (fndecl);
4053           oname = IDENTIFIER_POINTER (oid);
4054           gcc_assert (oname[0] == '_' && oname[1] == 'Z');
4055           nname = ACONCAT (("_ZGA", oname+2, NULL));
4056           nid = get_identifier (nname);
4057
4058           alias = make_alias_for (fndecl, nid);
4059           TREE_PUBLIC (alias) = 1;
4060           DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
4061
4062           assemble_alias (alias, oid);
4063         }
4064     }
4065 }
4066
4067 /* Return C++ property of T, based on given operation OP.  */
4068
4069 static int
4070 cpp_check (tree t, cpp_operation op)
4071 {
4072   switch (op)
4073     {
4074       case IS_ABSTRACT:
4075         return DECL_PURE_VIRTUAL_P (t);
4076       case IS_CONSTRUCTOR:
4077         return DECL_CONSTRUCTOR_P (t);
4078       case IS_DESTRUCTOR:
4079         return DECL_DESTRUCTOR_P (t);
4080       case IS_COPY_CONSTRUCTOR:
4081         return DECL_COPY_CONSTRUCTOR_P (t);
4082       case IS_TEMPLATE:
4083         return TREE_CODE (t) == TEMPLATE_DECL;
4084       case IS_TRIVIAL:
4085         return trivial_type_p (t);
4086       default:
4087         return 0;
4088     }
4089 }
4090
4091 /* Collect source file references recursively, starting from NAMESPC.  */
4092
4093 static void 
4094 collect_source_refs (tree namespc) 
4095 {
4096   tree t;
4097
4098   if (!namespc) 
4099     return;
4100
4101   /* Iterate over names in this name space.  */
4102   for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4103     if (!DECL_IS_BUILTIN (t) )
4104       collect_source_ref (DECL_SOURCE_FILE (t));
4105   
4106   /* Dump siblings, if any */
4107   collect_source_refs (TREE_CHAIN (namespc));
4108
4109   /* Dump children, if any */
4110   collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4111 }
4112
4113 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4114    starting from NAMESPC.  */
4115
4116 static void
4117 collect_ada_namespace (tree namespc, const char *source_file)
4118 {
4119   if (!namespc)
4120     return;
4121
4122   /* Collect decls from this namespace */
4123   collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4124
4125   /* Collect siblings, if any */
4126   collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4127
4128   /* Collect children, if any */
4129   collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4130 }
4131
4132 /* Returns true iff there is a definition available for variable or
4133    function DECL.  */
4134
4135 static bool
4136 decl_defined_p (tree decl)
4137 {
4138   if (TREE_CODE (decl) == FUNCTION_DECL)
4139     return (DECL_INITIAL (decl) != NULL_TREE);
4140   else
4141     {
4142       gcc_assert (VAR_P (decl));
4143       return !DECL_EXTERNAL (decl);
4144     }
4145 }
4146
4147 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4148
4149       [expr.const]
4150
4151       An integral constant-expression can only involve ... const
4152       variables of integral or enumeration types initialized with
4153       constant expressions ...
4154
4155       C++0x also allows constexpr variables and temporaries initialized
4156       with constant expressions.  We handle the former here, but the latter
4157       are just folded away in cxx_eval_constant_expression.
4158
4159    The standard does not require that the expression be non-volatile.
4160    G++ implements the proposed correction in DR 457.  */
4161
4162 bool
4163 decl_constant_var_p (tree decl)
4164 {
4165   if (!decl_maybe_constant_var_p (decl))
4166     return false;
4167
4168   /* We don't know if a template static data member is initialized with
4169      a constant expression until we instantiate its initializer.  Even
4170      in the case of a constexpr variable, we can't treat it as a
4171      constant until its initializer is complete in case it's used in
4172      its own initializer.  */
4173   mark_used (decl);
4174   return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4175 }
4176
4177 /* Returns true if DECL could be a symbolic constant variable, depending on
4178    its initializer.  */
4179
4180 bool
4181 decl_maybe_constant_var_p (tree decl)
4182 {
4183   tree type = TREE_TYPE (decl);
4184   if (!VAR_P (decl))
4185     return false;
4186   if (DECL_DECLARED_CONSTEXPR_P (decl))
4187     return true;
4188   return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4189           && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4190 }
4191
4192 /* Complain that DECL uses a type with no linkage.  In C++98 mode this is
4193    called from grokfndecl and grokvardecl; in all modes it is called from
4194    cp_write_global_declarations.  */
4195
4196 void
4197 no_linkage_error (tree decl)
4198 {
4199   if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4200     /* In C++11 it's ok if the decl is defined.  */
4201     return;
4202   tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4203   if (t == NULL_TREE)
4204     /* The type that got us on no_linkage_decls must have gotten a name for
4205        linkage purposes.  */;
4206   else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4207     /* The type might end up having a typedef name for linkage purposes.  */
4208     vec_safe_push (no_linkage_decls, decl);
4209   else if (TYPE_ANONYMOUS_P (t))
4210     {
4211       bool d = false;
4212       if (cxx_dialect >= cxx11)
4213         d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4214                        "anonymous type, is used but never defined", decl);
4215       else if (DECL_EXTERN_C_P (decl))
4216         /* Allow this; it's pretty common in C.  */;
4217       else if (TREE_CODE (decl) == VAR_DECL)
4218         /* DRs 132, 319 and 389 seem to indicate types with
4219            no linkage can only be used to declare extern "C"
4220            entities.  Since it's not always an error in the
4221            ISO C++ 90 Standard, we only issue a warning.  */
4222         d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
4223                         "with no linkage used to declare variable %q#D with "
4224                         "linkage", decl);
4225       else
4226         d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
4227                        "linkage used to declare function %q#D with linkage",
4228                        decl);
4229       if (d && is_typedef_decl (TYPE_NAME (t)))
4230         inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4231                 "to the unqualified type, so it is not used for linkage",
4232                 TYPE_NAME (t));
4233     }
4234   else if (cxx_dialect >= cxx11)
4235     permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
4236                "%qT, is used but never defined", decl, t);
4237   else if (TREE_CODE (decl) == VAR_DECL)
4238     warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4239                 "used to declare variable %q#D with linkage", t, decl);
4240   else
4241     permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4242                "to declare function %q#D with linkage", t, decl);
4243 }
4244
4245 /* Collect declarations from all namespaces relevant to SOURCE_FILE.  */
4246
4247 static void
4248 collect_all_refs (const char *source_file)
4249 {
4250   collect_ada_namespace (global_namespace, source_file);
4251 }
4252
4253 /* Clear DECL_EXTERNAL for NODE.  */
4254
4255 static bool
4256 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4257 {
4258   DECL_EXTERNAL (node->decl) = 0;
4259   return false;
4260 }
4261
4262 /* Build up the function to run dynamic initializers for thread_local
4263    variables in this translation unit and alias the init functions for the
4264    individual variables to it.  */
4265
4266 static void
4267 handle_tls_init (void)
4268 {
4269   tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4270   if (vars == NULL_TREE)
4271     return;
4272
4273   location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4274
4275   write_out_vars (vars);
4276
4277   tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4278                            boolean_type_node);
4279   TREE_PUBLIC (guard) = false;
4280   TREE_STATIC (guard) = true;
4281   DECL_ARTIFICIAL (guard) = true;
4282   DECL_IGNORED_P (guard) = true;
4283   TREE_USED (guard) = true;
4284   set_decl_tls_model (guard, decl_default_tls_model (guard));
4285   pushdecl_top_level_and_finish (guard, NULL_TREE);
4286
4287   tree fn = get_local_tls_init_fn ();
4288   start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4289   tree body = begin_function_body ();
4290   tree if_stmt = begin_if_stmt ();
4291   tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4292                                  tf_warning_or_error);
4293   finish_if_stmt_cond (cond, if_stmt);
4294   finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
4295                                           tf_warning_or_error));
4296   for (; vars; vars = TREE_CHAIN (vars))
4297     {
4298       tree var = TREE_VALUE (vars);
4299       tree init = TREE_PURPOSE (vars);
4300       one_static_initialization_or_destruction (var, init, true);
4301
4302 #ifdef ASM_OUTPUT_DEF
4303       /* Output init aliases even with -fno-extern-tls-init.  */
4304       if (TREE_PUBLIC (var))
4305         {
4306           tree single_init_fn = get_tls_init_fn (var);
4307           if (single_init_fn == NULL_TREE)
4308             continue;
4309           cgraph_node *alias
4310             = cgraph_node::get_create (fn)->create_same_body_alias
4311                 (single_init_fn, fn);
4312           gcc_assert (alias != NULL);
4313         }
4314 #endif
4315     }
4316
4317   finish_then_clause (if_stmt);
4318   finish_if_stmt (if_stmt);
4319   finish_function_body (body);
4320   expand_or_defer_fn (finish_function (0));
4321 }
4322
4323 /* The entire file is now complete.  If requested, dump everything
4324    to a file.  */
4325
4326 static void
4327 dump_tu (void)
4328 {
4329   int flags;
4330   FILE *stream = dump_begin (TDI_tu, &flags);
4331
4332   if (stream)
4333     {
4334       dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4335       dump_end (TDI_tu, stream);
4336     }
4337 }
4338
4339 /* Check the deallocation functions for CODE to see if we want to warn that
4340    only one was defined.  */
4341
4342 static void
4343 maybe_warn_sized_delete (enum tree_code code)
4344 {
4345   tree sized = NULL_TREE;
4346   tree unsized = NULL_TREE;
4347
4348   for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
4349        ovl; ovl = OVL_NEXT (ovl))
4350     {
4351       tree fn = OVL_CURRENT (ovl);
4352       /* We're only interested in usual deallocation functions.  */
4353       if (!non_placement_deallocation_fn_p (fn))
4354         continue;
4355       if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4356         unsized = fn;
4357       else
4358         sized = fn;
4359     }
4360   if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4361     warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4362                 "the program should also define %qD", sized);
4363   else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4364     warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4365                 "the program should also define %qD", unsized);
4366 }
4367
4368 /* Check the global deallocation functions to see if we want to warn about
4369    defining unsized without sized (or vice versa).  */
4370
4371 static void
4372 maybe_warn_sized_delete ()
4373 {
4374   if (!flag_sized_deallocation || !warn_sized_deallocation)
4375     return;
4376   maybe_warn_sized_delete (DELETE_EXPR);
4377   maybe_warn_sized_delete (VEC_DELETE_EXPR);
4378 }
4379
4380 /* This routine is called at the end of compilation.
4381    Its job is to create all the code needed to initialize and
4382    destroy the global aggregates.  We do the destruction
4383    first, since that way we only need to reverse the decls once.  */
4384
4385 void
4386 cp_write_global_declarations (void)
4387 {
4388   tree vars;
4389   bool reconsider;
4390   size_t i;
4391   location_t locus;
4392   unsigned ssdf_count = 0;
4393   int retries = 0;
4394   tree decl;
4395   hash_set<tree> *candidates;
4396
4397   locus = input_location;
4398   at_eof = 1;
4399
4400   /* Bad parse errors.  Just forget about it.  */
4401   if (! global_bindings_p () || current_class_type
4402       || !vec_safe_is_empty (decl_namespace_list))
4403     return;
4404
4405   /* This is the point to write out a PCH if we're doing that.
4406      In that case we do not want to do anything else.  */
4407   if (pch_file)
4408     {
4409       c_common_write_pch ();
4410       dump_tu ();
4411       return;
4412     }
4413
4414   symtab->process_same_body_aliases ();
4415
4416   /* Handle -fdump-ada-spec[-slim] */
4417   if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4418     {
4419       if (flag_dump_ada_spec_slim)
4420         collect_source_ref (main_input_filename);
4421       else
4422         collect_source_refs (global_namespace);
4423
4424       dump_ada_specs (collect_all_refs, cpp_check);
4425     }
4426
4427   /* FIXME - huh?  was  input_line -= 1;*/
4428
4429   timevar_start (TV_PHASE_DEFERRED);
4430
4431   /* We now have to write out all the stuff we put off writing out.
4432      These include:
4433
4434        o Template specializations that we have not yet instantiated,
4435          but which are needed.
4436        o Initialization and destruction for non-local objects with
4437          static storage duration.  (Local objects with static storage
4438          duration are initialized when their scope is first entered,
4439          and are cleaned up via atexit.)
4440        o Virtual function tables.
4441
4442      All of these may cause others to be needed.  For example,
4443      instantiating one function may cause another to be needed, and
4444      generating the initializer for an object may cause templates to be
4445      instantiated, etc., etc.  */
4446
4447   emit_support_tinfos ();
4448
4449   do
4450     {
4451       tree t;
4452       tree decl;
4453
4454       reconsider = false;
4455
4456       /* If there are templates that we've put off instantiating, do
4457          them now.  */
4458       instantiate_pending_templates (retries);
4459       ggc_collect ();
4460
4461       /* Write out virtual tables as required.  Note that writing out
4462          the virtual table for a template class may cause the
4463          instantiation of members of that class.  If we write out
4464          vtables then we remove the class from our list so we don't
4465          have to look at it again.  */
4466
4467       while (keyed_classes != NULL_TREE
4468              && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4469         {
4470           reconsider = true;
4471           keyed_classes = TREE_CHAIN (keyed_classes);
4472         }
4473
4474       t = keyed_classes;
4475       if (t != NULL_TREE)
4476         {
4477           tree next = TREE_CHAIN (t);
4478
4479           while (next)
4480             {
4481               if (maybe_emit_vtables (TREE_VALUE (next)))
4482                 {
4483                   reconsider = true;
4484                   TREE_CHAIN (t) = TREE_CHAIN (next);
4485                 }
4486               else
4487                 t = next;
4488
4489               next = TREE_CHAIN (t);
4490             }
4491         }
4492
4493       /* Write out needed type info variables.  We have to be careful
4494          looping through unemitted decls, because emit_tinfo_decl may
4495          cause other variables to be needed. New elements will be
4496          appended, and we remove from the vector those that actually
4497          get emitted.  */
4498       for (i = unemitted_tinfo_decls->length ();
4499            unemitted_tinfo_decls->iterate (--i, &t);)
4500         if (emit_tinfo_decl (t))
4501           {
4502             reconsider = true;
4503             unemitted_tinfo_decls->unordered_remove (i);
4504           }
4505
4506       /* The list of objects with static storage duration is built up
4507          in reverse order.  We clear STATIC_AGGREGATES so that any new
4508          aggregates added during the initialization of these will be
4509          initialized in the correct order when we next come around the
4510          loop.  */
4511       vars = prune_vars_needing_no_initialization (&static_aggregates);
4512
4513       if (vars)
4514         {
4515           /* We need to start a new initialization function each time
4516              through the loop.  That's because we need to know which
4517              vtables have been referenced, and TREE_SYMBOL_REFERENCED
4518              isn't computed until a function is finished, and written
4519              out.  That's a deficiency in the back end.  When this is
4520              fixed, these initialization functions could all become
4521              inline, with resulting performance improvements.  */
4522           tree ssdf_body;
4523
4524           /* Set the line and file, so that it is obviously not from
4525              the source file.  */
4526           input_location = locus;
4527           ssdf_body = start_static_storage_duration_function (ssdf_count);
4528
4529           /* Make sure the back end knows about all the variables.  */
4530           write_out_vars (vars);
4531
4532           /* First generate code to do all the initializations.  */
4533           if (vars)
4534             do_static_initialization_or_destruction (vars, /*initp=*/true);
4535
4536           /* Then, generate code to do all the destructions.  Do these
4537              in reverse order so that the most recently constructed
4538              variable is the first destroyed.  If we're using
4539              __cxa_atexit, then we don't need to do this; functions
4540              were registered at initialization time to destroy the
4541              local statics.  */
4542           if (!flag_use_cxa_atexit && vars)
4543             {
4544               vars = nreverse (vars);
4545               do_static_initialization_or_destruction (vars, /*initp=*/false);
4546             }
4547           else
4548             vars = NULL_TREE;
4549
4550           /* Finish up the static storage duration function for this
4551              round.  */
4552           input_location = locus;
4553           finish_static_storage_duration_function (ssdf_body);
4554
4555           /* All those initializations and finalizations might cause
4556              us to need more inline functions, more template
4557              instantiations, etc.  */
4558           reconsider = true;
4559           ssdf_count++;
4560           /* ??? was:  locus.line++; */
4561         }
4562
4563       /* Now do the same for thread_local variables.  */
4564       handle_tls_init ();
4565
4566       /* Go through the set of inline functions whose bodies have not
4567          been emitted yet.  If out-of-line copies of these functions
4568          are required, emit them.  */
4569       FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4570         {
4571           /* Does it need synthesizing?  */
4572           if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4573               && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4574             {
4575               /* Even though we're already at the top-level, we push
4576                  there again.  That way, when we pop back a few lines
4577                  hence, all of our state is restored.  Otherwise,
4578                  finish_function doesn't clean things up, and we end
4579                  up with CURRENT_FUNCTION_DECL set.  */
4580               push_to_top_level ();
4581               /* The decl's location will mark where it was first
4582                  needed.  Save that so synthesize method can indicate
4583                  where it was needed from, in case of error  */
4584               input_location = DECL_SOURCE_LOCATION (decl);
4585               synthesize_method (decl);
4586               pop_from_top_level ();
4587               reconsider = true;
4588             }
4589
4590           if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4591             generate_tls_wrapper (decl);
4592
4593           if (!DECL_SAVED_TREE (decl))
4594             continue;
4595
4596           /* We lie to the back end, pretending that some functions
4597              are not defined when they really are.  This keeps these
4598              functions from being put out unnecessarily.  But, we must
4599              stop lying when the functions are referenced, or if they
4600              are not comdat since they need to be put out now.  If
4601              DECL_INTERFACE_KNOWN, then we have already set
4602              DECL_EXTERNAL appropriately, so there's no need to check
4603              again, and we do not want to clear DECL_EXTERNAL if a
4604              previous call to import_export_decl set it.
4605
4606              This is done in a separate for cycle, because if some
4607              deferred function is contained in another deferred
4608              function later in deferred_fns varray,
4609              rest_of_compilation would skip this function and we
4610              really cannot expand the same function twice.  */
4611           import_export_decl (decl);
4612           if (DECL_NOT_REALLY_EXTERN (decl)
4613               && DECL_INITIAL (decl)
4614               && decl_needed_p (decl))
4615             {
4616               struct cgraph_node *node, *next;
4617
4618               node = cgraph_node::get (decl);
4619               if (node->cpp_implicit_alias)
4620                 node = node->get_alias_target ();
4621
4622               node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4623                                                       NULL, true);
4624               /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4625                  group, we need to mark all symbols in the same comdat group
4626                  that way.  */
4627               if (node->same_comdat_group)
4628                 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4629                      next != node;
4630                      next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4631                   next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4632                                                           NULL, true);
4633             }
4634
4635           /* If we're going to need to write this function out, and
4636              there's already a body for it, create RTL for it now.
4637              (There might be no body if this is a method we haven't
4638              gotten around to synthesizing yet.)  */
4639           if (!DECL_EXTERNAL (decl)
4640               && decl_needed_p (decl)
4641               && !TREE_ASM_WRITTEN (decl)
4642               && !cgraph_node::get (decl)->definition)
4643             {
4644               /* We will output the function; no longer consider it in this
4645                  loop.  */
4646               DECL_DEFER_OUTPUT (decl) = 0;
4647               /* Generate RTL for this function now that we know we
4648                  need it.  */
4649               expand_or_defer_fn (decl);
4650               /* If we're compiling -fsyntax-only pretend that this
4651                  function has been written out so that we don't try to
4652                  expand it again.  */
4653               if (flag_syntax_only)
4654                 TREE_ASM_WRITTEN (decl) = 1;
4655               reconsider = true;
4656             }
4657         }
4658
4659       if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4660         reconsider = true;
4661
4662       /* Static data members are just like namespace-scope globals.  */
4663       FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4664         {
4665           if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4666               /* Don't write it out if we haven't seen a definition.  */
4667               || DECL_IN_AGGR_P (decl))
4668             continue;
4669           import_export_decl (decl);
4670           /* If this static data member is needed, provide it to the
4671              back end.  */
4672           if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4673             DECL_EXTERNAL (decl) = 0;
4674         }
4675       if (vec_safe_length (pending_statics) != 0
4676           && wrapup_global_declarations (pending_statics->address (),
4677                                          pending_statics->length ()))
4678         reconsider = true;
4679
4680       retries++;
4681     }
4682   while (reconsider);
4683
4684   /* All used inline functions must have a definition at this point.  */
4685   FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4686     {
4687       if (/* Check online inline functions that were actually used.  */
4688           DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4689           /* If the definition actually was available here, then the
4690              fact that the function was not defined merely represents
4691              that for some reason (use of a template repository,
4692              #pragma interface, etc.) we decided not to emit the
4693              definition here.  */
4694           && !DECL_INITIAL (decl)
4695           /* Don't complain if the template was defined.  */
4696           && !(DECL_TEMPLATE_INSTANTIATION (decl)
4697                && DECL_INITIAL (DECL_TEMPLATE_RESULT
4698                                 (template_for_substitution (decl)))))
4699         {
4700           warning (0, "inline function %q+D used but never defined", decl);
4701           /* Avoid a duplicate warning from check_global_declaration_1.  */
4702           TREE_NO_WARNING (decl) = 1;
4703         }
4704     }
4705
4706   /* So must decls that use a type with no linkage.  */
4707   FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4708     no_linkage_error (decl);
4709
4710   maybe_warn_sized_delete ();
4711
4712   /* Then, do the Objective-C stuff.  This is where all the
4713      Objective-C module stuff gets generated (symtab,
4714      class/protocol/selector lists etc).  This must be done after C++
4715      templates, destructors etc. so that selectors used in C++
4716      templates are properly allocated.  */
4717   if (c_dialect_objc ())
4718     objc_write_global_declarations ();
4719
4720   /* We give C linkage to static constructors and destructors.  */
4721   push_lang_context (lang_name_c);
4722
4723   /* Generate initialization and destruction functions for all
4724      priorities for which they are required.  */
4725   if (priority_info_map)
4726     splay_tree_foreach (priority_info_map,
4727                         generate_ctor_and_dtor_functions_for_priority,
4728                         /*data=*/&locus);
4729   else if (c_dialect_objc () && objc_static_init_needed_p ())
4730     /* If this is obj-c++ and we need a static init, call
4731        generate_ctor_or_dtor_function.  */
4732     generate_ctor_or_dtor_function (/*constructor_p=*/true,
4733                                     DEFAULT_INIT_PRIORITY, &locus);
4734
4735   /* We're done with the splay-tree now.  */
4736   if (priority_info_map)
4737     splay_tree_delete (priority_info_map);
4738
4739   /* Generate any missing aliases.  */
4740   maybe_apply_pending_pragma_weaks ();
4741
4742   /* We're done with static constructors, so we can go back to "C++"
4743      linkage now.  */
4744   pop_lang_context ();
4745
4746   /* Collect candidates for Java hidden aliases.  */
4747   candidates = collect_candidates_for_java_method_aliases ();
4748
4749   timevar_stop (TV_PHASE_DEFERRED);
4750   timevar_start (TV_PHASE_OPT_GEN);
4751
4752   if (flag_vtable_verify)
4753     {
4754       vtv_recover_class_info ();
4755       vtv_compute_class_hierarchy_transitive_closure ();
4756       vtv_build_vtable_verify_fndecl ();
4757     }
4758
4759   symtab->finalize_compilation_unit ();
4760
4761   if (flag_vtable_verify)
4762     {
4763       /* Generate the special constructor initialization function that
4764          calls __VLTRegisterPairs, and give it a very high
4765          initialization priority.  This must be done after
4766          finalize_compilation_unit so that we have accurate
4767          information about which vtable will actually be emitted.  */
4768       vtv_generate_init_routine ();
4769     }
4770
4771   timevar_stop (TV_PHASE_OPT_GEN);
4772   timevar_start (TV_PHASE_CHECK_DBGINFO);
4773
4774   /* Now, issue warnings about static, but not defined, functions,
4775      etc., and emit debugging information.  */
4776   walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4777   if (vec_safe_length (pending_statics) != 0)
4778     {
4779       check_global_declarations (pending_statics->address (),
4780                                  pending_statics->length ());
4781       emit_debug_global_declarations (pending_statics->address (),
4782                                       pending_statics->length ());
4783     }
4784
4785   perform_deferred_noexcept_checks ();
4786
4787   /* Generate hidden aliases for Java.  */
4788   if (candidates)
4789     {
4790       build_java_method_aliases (candidates);
4791       delete candidates;
4792     }
4793
4794   finish_repo ();
4795
4796   /* The entire file is now complete.  If requested, dump everything
4797      to a file.  */
4798   dump_tu ();
4799
4800   if (flag_detailed_statistics)
4801     {
4802       dump_tree_statistics ();
4803       dump_time_statistics ();
4804     }
4805   input_location = locus;
4806
4807 #ifdef ENABLE_CHECKING
4808   validate_conversion_obstack ();
4809 #endif /* ENABLE_CHECKING */
4810
4811   timevar_stop (TV_PHASE_CHECK_DBGINFO);
4812 }
4813
4814 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4815    function to call in parse-tree form; it has not yet been
4816    semantically analyzed.  ARGS are the arguments to the function.
4817    They have already been semantically analyzed.  This may change
4818    ARGS.  */
4819
4820 tree
4821 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4822                                  tsubst_flags_t complain)
4823 {
4824   tree orig_fn;
4825   vec<tree, va_gc> *orig_args = NULL;
4826   tree expr;
4827   tree object;
4828
4829   orig_fn = fn;
4830   object = TREE_OPERAND (fn, 0);
4831
4832   if (processing_template_decl)
4833     {
4834       gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4835                   || TREE_CODE (fn) == MEMBER_REF);
4836       if (type_dependent_expression_p (fn)
4837           || any_type_dependent_arguments_p (*args))
4838         return build_nt_call_vec (fn, *args);
4839
4840       orig_args = make_tree_vector_copy (*args);
4841
4842       /* Transform the arguments and add the implicit "this"
4843          parameter.  That must be done before the FN is transformed
4844          because we depend on the form of FN.  */
4845       make_args_non_dependent (*args);
4846       object = build_non_dependent_expr (object);
4847       if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4848         {
4849           if (TREE_CODE (fn) == DOTSTAR_EXPR)
4850             object = cp_build_addr_expr (object, complain);
4851           vec_safe_insert (*args, 0, object);
4852         }
4853       /* Now that the arguments are done, transform FN.  */
4854       fn = build_non_dependent_expr (fn);
4855     }
4856
4857   /* A qualified name corresponding to a bound pointer-to-member is
4858      represented as an OFFSET_REF:
4859
4860         struct B { void g(); };
4861         void (B::*p)();
4862         void B::g() { (this->*p)(); }  */
4863   if (TREE_CODE (fn) == OFFSET_REF)
4864     {
4865       tree object_addr = cp_build_addr_expr (object, complain);
4866       fn = TREE_OPERAND (fn, 1);
4867       fn = get_member_function_from_ptrfunc (&object_addr, fn,
4868                                              complain);
4869       vec_safe_insert (*args, 0, object_addr);
4870     }
4871
4872   if (CLASS_TYPE_P (TREE_TYPE (fn)))
4873     expr = build_op_call (fn, args, complain);
4874   else
4875     expr = cp_build_function_call_vec (fn, args, complain);
4876   if (processing_template_decl && expr != error_mark_node)
4877     expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4878
4879   if (orig_args != NULL)
4880     release_tree_vector (orig_args);
4881
4882   return expr;
4883 }
4884
4885
4886 void
4887 check_default_args (tree x)
4888 {
4889   tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4890   bool saw_def = false;
4891   int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4892   for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4893     {
4894       if (TREE_PURPOSE (arg))
4895         saw_def = true;
4896       else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4897         {
4898           error ("default argument missing for parameter %P of %q+#D", i, x);
4899           TREE_PURPOSE (arg) = error_mark_node;
4900         }
4901     }
4902 }
4903
4904 /* Return true if function DECL can be inlined.  This is used to force
4905    instantiation of methods that might be interesting for inlining.  */
4906 bool
4907 possibly_inlined_p (tree decl)
4908 {
4909   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4910   if (DECL_UNINLINABLE (decl))
4911     return false;
4912   if (!optimize || pragma_java_exceptions)
4913     return DECL_DECLARED_INLINE_P (decl);
4914   /* When optimizing, we might inline everything when flatten
4915      attribute or heuristics inlining for size or autoinlining
4916      is used.  */
4917   return true;
4918 }
4919
4920 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4921    If DECL is a specialization or implicitly declared class member,
4922    generate the actual definition.  Return false if something goes
4923    wrong, true otherwise.  */
4924
4925 bool
4926 mark_used (tree decl, tsubst_flags_t complain)
4927 {
4928   /* If DECL is a BASELINK for a single function, then treat it just
4929      like the DECL for the function.  Otherwise, if the BASELINK is
4930      for an overloaded function, we don't know which function was
4931      actually used until after overload resolution.  */
4932   if (BASELINK_P (decl))
4933     {
4934       decl = BASELINK_FUNCTIONS (decl);
4935       if (really_overloaded_fn (decl))
4936         return true;
4937       decl = OVL_CURRENT (decl);
4938     }
4939
4940   /* Set TREE_USED for the benefit of -Wunused.  */
4941   TREE_USED (decl) = 1;
4942   if (DECL_CLONED_FUNCTION_P (decl))
4943     TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4944
4945   /* Mark enumeration types as used.  */
4946   if (TREE_CODE (decl) == CONST_DECL)
4947     used_types_insert (DECL_CONTEXT (decl));
4948
4949   if (TREE_CODE (decl) == FUNCTION_DECL)
4950     maybe_instantiate_noexcept (decl);
4951
4952   if (TREE_CODE (decl) == FUNCTION_DECL
4953       && DECL_DELETED_FN (decl))
4954     {
4955       if (DECL_ARTIFICIAL (decl))
4956         {
4957           if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4958               && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4959             {
4960               /* We mark a lambda conversion op as deleted if we can't
4961                  generate it properly; see maybe_add_lambda_conv_op.  */
4962               sorry ("converting lambda which uses %<...%> to "
4963                      "function pointer");
4964               return false;
4965             }
4966         }
4967       if (complain & tf_error)
4968         {
4969           error ("use of deleted function %qD", decl);
4970           if (!maybe_explain_implicit_delete (decl))
4971             inform (DECL_SOURCE_LOCATION (decl), "declared here");
4972         }
4973       return false;
4974     }
4975
4976   if (TREE_DEPRECATED (decl) && (complain & tf_warning)
4977       && deprecated_state != DEPRECATED_SUPPRESS)
4978     warn_deprecated_use (decl, NULL_TREE);
4979
4980   /* We can only check DECL_ODR_USED on variables or functions with
4981      DECL_LANG_SPECIFIC set, and these are also the only decls that we
4982      might need special handling for.  */
4983   if (!VAR_OR_FUNCTION_DECL_P (decl)
4984       || DECL_LANG_SPECIFIC (decl) == NULL
4985       || DECL_THUNK_P (decl))
4986     {
4987       if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
4988         {
4989           if (complain & tf_error)
4990             error ("use of %qD before deduction of %<auto%>", decl);
4991           return false;
4992         }
4993       return true;
4994     }
4995
4996   /* We only want to do this processing once.  We don't need to keep trying
4997      to instantiate inline templates, because unit-at-a-time will make sure
4998      we get them compiled before functions that want to inline them.  */
4999   if (DECL_ODR_USED (decl))
5000     return true;
5001
5002   /* If within finish_function, defer the rest until that function
5003      finishes, otherwise it might recurse.  */
5004   if (defer_mark_used_calls)
5005     {
5006       vec_safe_push (deferred_mark_used_calls, decl);
5007       return true;
5008     }
5009
5010   /* Normally, we can wait until instantiation-time to synthesize DECL.
5011      However, if DECL is a static data member initialized with a constant
5012      or a constexpr function, we need it right now because a reference to
5013      such a data member or a call to such function is not value-dependent.
5014      For a function that uses auto in the return type, we need to instantiate
5015      it to find out its type.  For OpenMP user defined reductions, we need
5016      them instantiated for reduction clauses which inline them by hand
5017      directly.  */
5018   if (DECL_LANG_SPECIFIC (decl)
5019       && DECL_TEMPLATE_INFO (decl)
5020       && (decl_maybe_constant_var_p (decl)
5021           || (TREE_CODE (decl) == FUNCTION_DECL
5022               && (DECL_DECLARED_CONSTEXPR_P (decl)
5023                   || DECL_OMP_DECLARE_REDUCTION_P (decl)))
5024           || undeduced_auto_decl (decl))
5025       && !uses_template_parms (DECL_TI_ARGS (decl)))
5026     {
5027       /* Instantiating a function will result in garbage collection.  We
5028          must treat this situation as if we were within the body of a
5029          function so as to avoid collecting live data only referenced from
5030          the stack (such as overload resolution candidates).  */
5031       ++function_depth;
5032       instantiate_decl (decl, /*defer_ok=*/false,
5033                         /*expl_inst_class_mem_p=*/false);
5034       --function_depth;
5035     }
5036
5037   if (processing_template_decl || in_template_function ())
5038     return true;
5039
5040   /* Check this too in case we're within instantiate_non_dependent_expr.  */
5041   if (DECL_TEMPLATE_INFO (decl)
5042       && uses_template_parms (DECL_TI_ARGS (decl)))
5043     return true;
5044
5045   if (undeduced_auto_decl (decl))
5046     {
5047       if (complain & tf_error)
5048         error ("use of %qD before deduction of %<auto%>", decl);
5049       return false;
5050     }
5051
5052   /* If we don't need a value, then we don't need to synthesize DECL.  */
5053   if (cp_unevaluated_operand != 0)
5054     return true;
5055
5056   DECL_ODR_USED (decl) = 1;
5057   if (DECL_CLONED_FUNCTION_P (decl))
5058     DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5059
5060   /* DR 757: A type without linkage shall not be used as the type of a
5061      variable or function with linkage, unless
5062    o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5063    o the variable or function is not used (3.2 [basic.def.odr]) or is
5064    defined in the same translation unit.  */
5065   if (cxx_dialect > cxx98
5066       && decl_linkage (decl) != lk_none
5067       && !DECL_EXTERN_C_P (decl)
5068       && !DECL_ARTIFICIAL (decl)
5069       && !decl_defined_p (decl)
5070       && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5071     {
5072       if (is_local_extern (decl))
5073         /* There's no way to define a local extern, and adding it to
5074            the vector interferes with GC, so give an error now.  */
5075         no_linkage_error (decl);
5076       else
5077         vec_safe_push (no_linkage_decls, decl);
5078     }
5079
5080   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5081       && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5082     /* Remember it, so we can check it was defined.  */
5083     note_vague_linkage_fn (decl);
5084
5085   /* Is it a synthesized method that needs to be synthesized?  */
5086   if (TREE_CODE (decl) == FUNCTION_DECL
5087       && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5088       && DECL_DEFAULTED_FN (decl)
5089       /* A function defaulted outside the class is synthesized either by
5090          cp_finish_decl or instantiate_decl.  */
5091       && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5092       && ! DECL_INITIAL (decl))
5093     {
5094       /* Defer virtual destructors so that thunks get the right
5095          linkage.  */
5096       if (DECL_VIRTUAL_P (decl) && !at_eof)
5097         {
5098           note_vague_linkage_fn (decl);
5099           return true;
5100         }
5101
5102       /* Remember the current location for a function we will end up
5103          synthesizing.  Then we can inform the user where it was
5104          required in the case of error.  */
5105       DECL_SOURCE_LOCATION (decl) = input_location;
5106
5107       /* Synthesizing an implicitly defined member function will result in
5108          garbage collection.  We must treat this situation as if we were
5109          within the body of a function so as to avoid collecting live data
5110          on the stack (such as overload resolution candidates).
5111
5112          We could just let cp_write_global_declarations handle synthesizing
5113          this function by adding it to deferred_fns, but doing
5114          it at the use site produces better error messages.  */
5115       ++function_depth;
5116       synthesize_method (decl);
5117       --function_depth;
5118       /* If this is a synthesized method we don't need to
5119          do the instantiation test below.  */
5120     }
5121   else if (VAR_OR_FUNCTION_DECL_P (decl)
5122            && DECL_TEMPLATE_INFO (decl)
5123            && (!DECL_EXPLICIT_INSTANTIATION (decl)
5124                || always_instantiate_p (decl)))
5125     /* If this is a function or variable that is an instance of some
5126        template, we now know that we will need to actually do the
5127        instantiation. We check that DECL is not an explicit
5128        instantiation because that is not checked in instantiate_decl.
5129
5130        We put off instantiating functions in order to improve compile
5131        times.  Maintaining a stack of active functions is expensive,
5132        and the inliner knows to instantiate any functions it might
5133        need.  Therefore, we always try to defer instantiation.  */
5134     {
5135       ++function_depth;
5136       instantiate_decl (decl, /*defer_ok=*/true,
5137                         /*expl_inst_class_mem_p=*/false);
5138       --function_depth;
5139     }
5140
5141   return true;
5142 }
5143
5144 bool
5145 mark_used (tree decl)
5146 {
5147   return mark_used (decl, tf_warning_or_error);
5148 }
5149
5150 tree
5151 vtv_start_verification_constructor_init_function (void)
5152 {
5153   return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5154 }
5155
5156 tree
5157 vtv_finish_verification_constructor_init_function (tree function_body)
5158 {
5159   tree fn;
5160
5161   finish_compound_stmt (function_body);
5162   fn = finish_function (0);
5163   DECL_STATIC_CONSTRUCTOR (fn) = 1;
5164   decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5165
5166   return fn;
5167 }
5168
5169 #include "gt-cp-decl2.h"