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