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