1132718Skan/* Process declarations and variables for C++ compiler.
290075Sobrien   Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3169689Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
418334Speter   Hacked by Michael Tiemann (tiemann@cygnus.com)
518334Speter
6132718SkanThis file is part of GCC.
718334Speter
8132718SkanGCC is free software; you can redistribute it and/or modify
918334Speterit under the terms of the GNU General Public License as published by
1018334Speterthe Free Software Foundation; either version 2, or (at your option)
1118334Speterany later version.
1218334Speter
13132718SkanGCC is distributed in the hope that it will be useful,
1418334Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of
1518334SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1618334SpeterGNU General Public License for more details.
1718334Speter
1818334SpeterYou should have received a copy of the GNU General Public License
19132718Skanalong with GCC; see the file COPYING.  If not, write to
20169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
21169689SkanBoston, MA 02110-1301, USA.  */
2218334Speter
2318334Speter
24132718Skan/* Process declarations and symbol lookup for C++ front end.
2518334Speter   Also constructs types; the standard scalar types at initialization,
2618334Speter   and structure, union, array and enum types when they are declared.  */
2718334Speter
2818334Speter/* ??? not all decl nodes are given the most useful possible
2918334Speter   line numbers.  For example, the CONST_DECLs for enum values.  */
3018334Speter
3118334Speter#include "config.h"
3250397Sobrien#include "system.h"
33132718Skan#include "coretypes.h"
34132718Skan#include "tm.h"
3518334Speter#include "tree.h"
3618334Speter#include "rtl.h"
3790075Sobrien#include "expr.h"
3818334Speter#include "flags.h"
3918334Speter#include "cp-tree.h"
4018334Speter#include "decl.h"
4118334Speter#include "output.h"
4250397Sobrien#include "except.h"
4350397Sobrien#include "toplev.h"
4490075Sobrien#include "timevar.h"
4550397Sobrien#include "cpplib.h"
4690075Sobrien#include "target.h"
47117395Skan#include "c-common.h"
48169689Skan#include "tree-mudflap.h"
49132718Skan#include "cgraph.h"
50132718Skan#include "tree-inline.h"
51169689Skan#include "c-pragma.h"
52169689Skan#include "tree-dump.h"
53169689Skan#include "intl.h"
54169689Skan
5590075Sobrienextern cpp_reader *parse_in;
5618334Speter
5752284Sobrien/* This structure contains information about the initializations
5852284Sobrien   and/or destructions required for a particular priority level.  */
5952284Sobrientypedef struct priority_info_s {
60117395Skan  /* Nonzero if there have been any initializations at this priority
6152284Sobrien     throughout the translation unit.  */
6252284Sobrien  int initializations_p;
63117395Skan  /* Nonzero if there have been any destructions at this priority
6452284Sobrien     throughout the translation unit.  */
6552284Sobrien  int destructions_p;
6652284Sobrien} *priority_info;
6752284Sobrien
68132718Skanstatic void mark_vtable_entries (tree);
69132718Skanstatic bool maybe_emit_vtables (tree);
70132718Skanstatic bool acceptable_java_type (tree);
71132718Skanstatic tree start_objects (int, int);
72132718Skanstatic void finish_objects (int, int, tree);
73132718Skanstatic tree start_static_storage_duration_function (unsigned);
74132718Skanstatic void finish_static_storage_duration_function (tree);
75132718Skanstatic priority_info get_priority_info (int);
76169689Skanstatic void do_static_initialization_or_destruction (tree, bool);
77169689Skanstatic void one_static_initialization_or_destruction (tree, tree, bool);
78132718Skanstatic void generate_ctor_or_dtor_function (bool, int, location_t *);
79132718Skanstatic int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
80169689Skan							  void *);
81132718Skanstatic tree prune_vars_needing_no_initialization (tree *);
82132718Skanstatic void write_out_vars (tree);
83132718Skanstatic void import_export_class (tree);
84132718Skanstatic tree get_guard_bits (tree);
85169689Skanstatic void determine_visibility_from_class (tree, tree);
8650397Sobrien
8718334Speter/* A list of static class variables.  This is needed, because a
8818334Speter   static class variable can be declared inside the class without
8990075Sobrien   an initializer, and then initialized, statically, outside the class.  */
90169689Skanstatic GTY(()) VEC(tree,gc) *pending_statics;
9118334Speter
9218334Speter/* A list of functions which were declared inline, but which we
9350397Sobrien   may need to emit outline anyway.  */
94169689Skanstatic GTY(()) VEC(tree,gc) *deferred_fns;
9518334Speter
9650397Sobrien/* Nonzero if we're done parsing and into end-of-file activities.  */
9750397Sobrien
9850397Sobrienint at_eof;
9950397Sobrien
10018334Speter/* Functions called along with real static constructors and destructors.  */
10118334Speter
10290075Sobrientree static_ctors;
10390075Sobrientree static_dtors;
10450397Sobrien
10518334Speter
10650397Sobrien
107169689Skan/* Return a member function type (a METHOD_TYPE), given FNTYPE (a
108169689Skan   FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
109169689Skan   that apply to the function).  */
110169689Skan
111169689Skantree
112169689Skanbuild_memfn_type (tree fntype, tree ctype, cp_cv_quals quals)
11318334Speter{
114169689Skan  tree raises;
115169689Skan  int type_quals;
11618334Speter
117169689Skan  if (fntype == error_mark_node || ctype == error_mark_node)
118169689Skan    return error_mark_node;
11952284Sobrien
120169689Skan  type_quals = quals & ~TYPE_QUAL_RESTRICT;
12152284Sobrien  ctype = cp_build_qualified_type (ctype, type_quals);
122132718Skan  fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
123132718Skan				       (TREE_CODE (fntype) == METHOD_TYPE
124132718Skan					? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
125132718Skan					: TYPE_ARG_TYPES (fntype)));
126169689Skan  raises = TYPE_RAISES_EXCEPTIONS (fntype);
12718334Speter  if (raises)
12818334Speter    fntype = build_exception_variant (fntype, raises);
12918334Speter
130169689Skan  return fntype;
13118334Speter}
13218334Speter
133117395Skan/* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
134117395Skan   appropriately.  */
135117395Skan
136117395Skantree
137132718Skancp_build_parm_decl (tree name, tree type)
138117395Skan{
139117395Skan  tree parm = build_decl (PARM_DECL, name, type);
140132718Skan  /* DECL_ARG_TYPE is only used by the back end and the back end never
141132718Skan     sees templates.  */
142132718Skan  if (!processing_template_decl)
143132718Skan    DECL_ARG_TYPE (parm) = type_passed_as (type);
144117395Skan  return parm;
145117395Skan}
146117395Skan
14790075Sobrien/* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
14890075Sobrien   indicated NAME.  */
14990075Sobrien
15090075Sobrientree
151132718Skanbuild_artificial_parm (tree name, tree type)
15290075Sobrien{
153117395Skan  tree parm = cp_build_parm_decl (name, type);
15490075Sobrien  DECL_ARTIFICIAL (parm) = 1;
15590075Sobrien  /* All our artificial parms are implicitly `const'; they cannot be
15690075Sobrien     assigned to.  */
15790075Sobrien  TREE_READONLY (parm) = 1;
15890075Sobrien  return parm;
15918334Speter}
16018334Speter
16150397Sobrien/* Constructors for types with virtual baseclasses need an "in-charge" flag
16250397Sobrien   saying whether this constructor is responsible for initialization of
16350397Sobrien   virtual baseclasses or not.  All destructors also need this "in-charge"
16450397Sobrien   flag, which additionally determines whether or not the destructor should
16550397Sobrien   free the memory for the object.
16650397Sobrien
16750397Sobrien   This function adds the "in-charge" flag to member function FN if
16850397Sobrien   appropriate.  It is called from grokclassfn and tsubst.
16960967Sobrien   FN must be either a constructor or destructor.
17050397Sobrien
17190075Sobrien   The in-charge flag follows the 'this' parameter, and is followed by the
17290075Sobrien   VTT parm (if any), then the user-written parms.  */
17360967Sobrien
17450397Sobrienvoid
175132718Skanmaybe_retrofit_in_chrg (tree fn)
17650397Sobrien{
17750397Sobrien  tree basetype, arg_types, parms, parm, fntype;
17850397Sobrien
17990075Sobrien  /* If we've already add the in-charge parameter don't do it again.  */
18090075Sobrien  if (DECL_HAS_IN_CHARGE_PARM_P (fn))
18160967Sobrien    return;
18260967Sobrien
18390075Sobrien  /* When processing templates we can't know, in general, whether or
18490075Sobrien     not we're going to have virtual baseclasses.  */
185132718Skan  if (processing_template_decl)
18690075Sobrien    return;
18790075Sobrien
18890075Sobrien  /* We don't need an in-charge parameter for constructors that don't
18990075Sobrien     have virtual bases.  */
19050397Sobrien  if (DECL_CONSTRUCTOR_P (fn)
191169689Skan      && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
19250397Sobrien    return;
19350397Sobrien
19460967Sobrien  arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
19560967Sobrien  basetype = TREE_TYPE (TREE_VALUE (arg_types));
19660967Sobrien  arg_types = TREE_CHAIN (arg_types);
19760967Sobrien
19890075Sobrien  parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
19990075Sobrien
20090075Sobrien  /* If this is a subobject constructor or destructor, our caller will
20190075Sobrien     pass us a pointer to our VTT.  */
202169689Skan  if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
20360967Sobrien    {
20490075Sobrien      parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
20560967Sobrien
20690075Sobrien      /* First add it to DECL_ARGUMENTS between 'this' and the real args...  */
20790075Sobrien      TREE_CHAIN (parm) = parms;
20890075Sobrien      parms = parm;
20990075Sobrien
21090075Sobrien      /* ...and then to TYPE_ARG_TYPES.  */
21190075Sobrien      arg_types = hash_tree_chain (vtt_parm_type, arg_types);
21290075Sobrien
21390075Sobrien      DECL_HAS_VTT_PARM_P (fn) = 1;
21460967Sobrien    }
21560967Sobrien
21690075Sobrien  /* Then add the in-charge parm (before the VTT parm).  */
21790075Sobrien  parm = build_artificial_parm (in_charge_identifier, integer_type_node);
21890075Sobrien  TREE_CHAIN (parm) = parms;
21990075Sobrien  parms = parm;
22090075Sobrien  arg_types = hash_tree_chain (integer_type_node, arg_types);
22150397Sobrien
22290075Sobrien  /* Insert our new parameter(s) into the list.  */
22390075Sobrien  TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
22490075Sobrien
22590075Sobrien  /* And rebuild the function type.  */
226132718Skan  fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
227132718Skan				       arg_types);
22850397Sobrien  if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
22950397Sobrien    fntype = build_exception_variant (fntype,
23050397Sobrien				      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
23150397Sobrien  TREE_TYPE (fn) = fntype;
23290075Sobrien
23390075Sobrien  /* Now we've got the in-charge parameter.  */
23490075Sobrien  DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
23550397Sobrien}
23650397Sobrien
23718334Speter/* Classes overload their constituent function names automatically.
23818334Speter   When a function name is declared in a record structure,
23918334Speter   its name is changed to it overloaded name.  Since names for
24018334Speter   constructors and destructors can conflict, we place a leading
24118334Speter   '$' for destructors.
24218334Speter
24318334Speter   CNAME is the name of the class we are grokking for.
24418334Speter
24518334Speter   FUNCTION is a FUNCTION_DECL.  It was created by `grokdeclarator'.
24618334Speter
24718334Speter   FLAGS contains bits saying what's special about today's
24818334Speter   arguments.  1 == DESTRUCTOR.  2 == OPERATOR.
24918334Speter
25018334Speter   If FUNCTION is a destructor, then we must add the `auto-delete' field
25118334Speter   as a second parameter.  There is some hair associated with the fact
25218334Speter   that we must "declare" this variable in the manner consistent with the
25318334Speter   way the rest of the arguments were declared.
25418334Speter
25518334Speter   QUALS are the qualifiers for the this pointer.  */
25618334Speter
25718334Spetervoid
258169689Skangrokclassfn (tree ctype, tree function, enum overload_flags flags)
25918334Speter{
26018334Speter  tree fn_name = DECL_NAME (function);
26118334Speter
26290075Sobrien  /* Even within an `extern "C"' block, members get C++ linkage.  See
26390075Sobrien     [dcl.link] for details.  */
26490075Sobrien  SET_DECL_LANGUAGE (function, lang_cplusplus);
26590075Sobrien
26618334Speter  if (fn_name == NULL_TREE)
26718334Speter    {
26818334Speter      error ("name missing for member function");
26918334Speter      fn_name = get_identifier ("<anonymous>");
27018334Speter      DECL_NAME (function) = fn_name;
27118334Speter    }
27218334Speter
27350397Sobrien  DECL_CONTEXT (function) = ctype;
27450397Sobrien
27590075Sobrien  if (flags == DTOR_FLAG)
27690075Sobrien    DECL_DESTRUCTOR_P (function) = 1;
27790075Sobrien
27850397Sobrien  if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
27990075Sobrien    maybe_retrofit_in_chrg (function);
28018334Speter}
28118334Speter
28218334Speter/* Create an ARRAY_REF, checking for the user doing things backwards
28318334Speter   along the way.  */
28450397Sobrien
28518334Spetertree
286132718Skangrok_array_decl (tree array_expr, tree index_exp)
28718334Speter{
288132718Skan  tree type;
289132718Skan  tree expr;
290132718Skan  tree orig_array_expr = array_expr;
291132718Skan  tree orig_index_exp = index_exp;
29218334Speter
293132718Skan  if (error_operand_p (array_expr) || error_operand_p (index_exp))
29418334Speter    return error_mark_node;
295132718Skan
29650397Sobrien  if (processing_template_decl)
29718334Speter    {
298132718Skan      if (type_dependent_expression_p (array_expr)
299132718Skan	  || type_dependent_expression_p (index_exp))
300169689Skan	return build_min_nt (ARRAY_REF, array_expr, index_exp,
301169689Skan			     NULL_TREE, NULL_TREE);
302132718Skan      array_expr = build_non_dependent_expr (array_expr);
303132718Skan      index_exp = build_non_dependent_expr (index_exp);
30418334Speter    }
30518334Speter
306132718Skan  type = TREE_TYPE (array_expr);
307169689Skan  gcc_assert (type);
308132718Skan  type = non_reference (type);
30918334Speter
31018334Speter  /* If they have an `operator[]', use that.  */
31150397Sobrien  if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
312132718Skan    expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL,
313132718Skan			 array_expr, index_exp, NULL_TREE,
314132718Skan			 /*overloaded_p=*/NULL);
315132718Skan  else
316132718Skan    {
317132718Skan      tree p1, p2, i1, i2;
31818334Speter
319132718Skan      /* Otherwise, create an ARRAY_REF for a pointer or array type.
320132718Skan	 It is a little-known fact that, if `a' is an array and `i' is
321132718Skan	 an int, you can write `i[a]', which means the same thing as
322132718Skan	 `a[i]'.  */
323132718Skan      if (TREE_CODE (type) == ARRAY_TYPE)
324132718Skan	p1 = array_expr;
325132718Skan      else
326132718Skan	p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
32718334Speter
328132718Skan      if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
329132718Skan	p2 = index_exp;
330132718Skan      else
331132718Skan	p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
33218334Speter
333169689Skan      i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
334132718Skan				       false);
335169689Skan      i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
336132718Skan				       false);
33718334Speter
338132718Skan      if ((p1 && i2) && (i1 && p2))
339132718Skan	error ("ambiguous conversion for array subscript");
34018334Speter
341132718Skan      if (p1 && i2)
342132718Skan	array_expr = p1, index_exp = i2;
343132718Skan      else if (i1 && p2)
344132718Skan	array_expr = p2, index_exp = i1;
345132718Skan      else
346132718Skan	{
347169689Skan	  error ("invalid types %<%T[%T]%> for array subscript",
348169689Skan		 type, TREE_TYPE (index_exp));
349132718Skan	  return error_mark_node;
350132718Skan	}
35118334Speter
352132718Skan      if (array_expr == error_mark_node || index_exp == error_mark_node)
353132718Skan	error ("ambiguous conversion for array subscript");
354132718Skan
355132718Skan      expr = build_array_ref (array_expr, index_exp);
35618334Speter    }
357132718Skan  if (processing_template_decl && expr != error_mark_node)
358169689Skan    return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
359169689Skan			      NULL_TREE, NULL_TREE);
360132718Skan  return expr;
36118334Speter}
36218334Speter
36318334Speter/* Given the cast expression EXP, checking out its validity.   Either return
36418334Speter   an error_mark_node if there was an unavoidable error, return a cast to
36518334Speter   void for trying to delete a pointer w/ the value 0, or return the
366132718Skan   call to delete.  If DOING_VEC is true, we handle things differently
367132718Skan   for doing an array delete.
36818334Speter   Implements ARM $5.3.4.  This is called from the parser.  */
36950397Sobrien
37018334Spetertree
371132718Skandelete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete)
37218334Speter{
37350397Sobrien  tree t, type;
37418334Speter
37550397Sobrien  if (exp == error_mark_node)
37650397Sobrien    return exp;
37750397Sobrien
37850397Sobrien  if (processing_template_decl)
37918334Speter    {
38050397Sobrien      t = build_min (DELETE_EXPR, void_type_node, exp, size);
38150397Sobrien      DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
38250397Sobrien      DELETE_EXPR_USE_VEC (t) = doing_vec;
383132718Skan      TREE_SIDE_EFFECTS (t) = 1;
38450397Sobrien      return t;
38550397Sobrien    }
38618334Speter
387132718Skan  /* An array can't have been allocated by new, so complain.  */
388132718Skan  if (TREE_CODE (exp) == VAR_DECL
389132718Skan      && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
390169689Skan    warning (0, "deleting array %q#D", exp);
391132718Skan
392132718Skan  t = build_expr_type_conversion (WANT_POINTER, exp, true);
393132718Skan
39450397Sobrien  if (t == NULL_TREE || t == error_mark_node)
39550397Sobrien    {
396169689Skan      error ("type %q#T argument given to %<delete%>, expected pointer",
397169689Skan	     TREE_TYPE (exp));
39850397Sobrien      return error_mark_node;
39918334Speter    }
40018334Speter
40150397Sobrien  type = TREE_TYPE (t);
40250397Sobrien
40350397Sobrien  /* As of Valley Forge, you can delete a pointer to const.  */
40450397Sobrien
40550397Sobrien  /* You can't delete functions.  */
40650397Sobrien  if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
40718334Speter    {
408169689Skan      error ("cannot delete a function.  Only pointer-to-objects are "
409169689Skan	     "valid arguments to %<delete%>");
41050397Sobrien      return error_mark_node;
41118334Speter    }
41218334Speter
413117395Skan  /* Deleting ptr to void is undefined behavior [expr.delete/3].  */
41452284Sobrien  if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
41590075Sobrien    {
416169689Skan      warning (0, "deleting %qT is undefined", type);
41790075Sobrien      doing_vec = 0;
41890075Sobrien    }
41990075Sobrien
42050397Sobrien  /* Deleting a pointer with the value zero is valid and has no effect.  */
42150397Sobrien  if (integer_zerop (t))
42250397Sobrien    return build1 (NOP_EXPR, void_type_node, t);
42350397Sobrien
42418334Speter  if (doing_vec)
425169689Skan    return build_vec_delete (t, /*maxindex=*/NULL_TREE,
426132718Skan			     sfk_deleting_destructor,
42790075Sobrien			     use_global_delete);
42818334Speter  else
42996263Sobrien    return build_delete (type, t, sfk_deleting_destructor,
43096263Sobrien			 LOOKUP_NORMAL, use_global_delete);
43118334Speter}
43218334Speter
43350397Sobrien/* Report an error if the indicated template declaration is not the
43450397Sobrien   sort of thing that should be a member template.  */
43550397Sobrien
43650397Sobrienvoid
437132718Skancheck_member_template (tree tmpl)
43850397Sobrien{
43950397Sobrien  tree decl;
44050397Sobrien
441169689Skan  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
44250397Sobrien  decl = DECL_TEMPLATE_RESULT (tmpl);
44350397Sobrien
44450397Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL
44550397Sobrien      || (TREE_CODE (decl) == TYPE_DECL
44650397Sobrien	  && IS_AGGR_TYPE (TREE_TYPE (decl))))
44750397Sobrien    {
448169689Skan      /* The parser rejects template declarations in local classes.  */
449169689Skan      gcc_assert (!current_function_decl);
450169689Skan      /* The parser rejects any use of virtual in a function template.  */
451169689Skan      gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
452169689Skan		    && DECL_VIRTUAL_P (decl)));
45350397Sobrien
45450397Sobrien      /* The debug-information generating code doesn't know what to do
455169689Skan	 with member templates.  */
45650397Sobrien      DECL_IGNORED_P (tmpl) = 1;
457169689Skan    }
45850397Sobrien  else
459169689Skan    error ("template declaration of %q#D", decl);
46050397Sobrien}
46150397Sobrien
462117395Skan/* Return true iff TYPE is a valid Java parameter or return type.  */
46350397Sobrien
464132718Skanstatic bool
465132718Skanacceptable_java_type (tree type)
46650397Sobrien{
467169689Skan  if (type == error_mark_node)
468169689Skan    return false;
469169689Skan
47050397Sobrien  if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
471169689Skan    return true;
47290075Sobrien  if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
47350397Sobrien    {
47450397Sobrien      type = TREE_TYPE (type);
47550397Sobrien      if (TREE_CODE (type) == RECORD_TYPE)
47650397Sobrien	{
47752284Sobrien	  tree args;  int i;
47852284Sobrien	  if (! TYPE_FOR_JAVA (type))
479132718Skan	    return false;
48052284Sobrien	  if (! CLASSTYPE_TEMPLATE_INFO (type))
481132718Skan	    return true;
48252284Sobrien	  args = CLASSTYPE_TI_ARGS (type);
48352284Sobrien	  i = TREE_VEC_LENGTH (args);
48452284Sobrien	  while (--i >= 0)
48552284Sobrien	    {
48652284Sobrien	      type = TREE_VEC_ELT (args, i);
48752284Sobrien	      if (TREE_CODE (type) == POINTER_TYPE)
48852284Sobrien		type = TREE_TYPE (type);
48952284Sobrien	      if (! TYPE_FOR_JAVA (type))
490132718Skan		return false;
49152284Sobrien	    }
492132718Skan	  return true;
49350397Sobrien	}
49450397Sobrien    }
495132718Skan  return false;
49650397Sobrien}
49750397Sobrien
498132718Skan/* For a METHOD in a Java class CTYPE, return true if
49950397Sobrien   the parameter and return types are valid Java types.
500132718Skan   Otherwise, print appropriate error messages, and return false.  */
50150397Sobrien
502132718Skanbool
503132718Skancheck_java_method (tree method)
50450397Sobrien{
505132718Skan  bool jerr = false;
50650397Sobrien  tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
50750397Sobrien  tree ret_type = TREE_TYPE (TREE_TYPE (method));
508132718Skan
509132718Skan  if (!acceptable_java_type (ret_type))
51050397Sobrien    {
511169689Skan      error ("Java method %qD has non-Java return type %qT",
512169689Skan	     method, ret_type);
513132718Skan      jerr = true;
51450397Sobrien    }
515132718Skan
516132718Skan  arg_types = TREE_CHAIN (arg_types);
517132718Skan  if (DECL_HAS_IN_CHARGE_PARM_P (method))
518132718Skan    arg_types = TREE_CHAIN (arg_types);
519132718Skan  if (DECL_HAS_VTT_PARM_P (method))
520132718Skan    arg_types = TREE_CHAIN (arg_types);
521169689Skan
52250397Sobrien  for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
52350397Sobrien    {
52450397Sobrien      tree type = TREE_VALUE (arg_types);
525132718Skan      if (!acceptable_java_type (type))
52650397Sobrien	{
527169689Skan          if (type != error_mark_node)
528169689Skan	    error ("Java method %qD has non-Java parameter type %qT",
529169689Skan		   method, type);
530132718Skan	  jerr = true;
53150397Sobrien	}
53250397Sobrien    }
533132718Skan  return !jerr;
53450397Sobrien}
53550397Sobrien
53618334Speter/* Sanity check: report error if this function FUNCTION is not
53718334Speter   really a member of the class (CTYPE) it is supposed to belong to.
538169689Skan   TEMPLATE_PARMS is used to specify the template parameters of a member
539169689Skan   template passed as FUNCTION_DECL. If the member template is passed as a
540169689Skan   TEMPLATE_DECL, it can be NULL since the parameters can be extracted
541169689Skan   from the declaration. If the function is not a function template, it
542169689Skan   must be NULL.
543169689Skan   It returns the original declaration for the function, or NULL_TREE
544169689Skan   if no declaration was found (and an error was emitted).  */
54518334Speter
54618334Spetertree
547169689Skancheck_classfn (tree ctype, tree function, tree template_parms)
54818334Speter{
549117395Skan  int ix;
550169689Skan  bool is_template;
551169689Skan  tree pushed_scope;
55252284Sobrien
55352284Sobrien  if (DECL_USE_TEMPLATE (function)
55490075Sobrien      && !(TREE_CODE (function) == TEMPLATE_DECL
55590075Sobrien	   && DECL_TEMPLATE_SPECIALIZATION (function))
556169689Skan      && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
55752284Sobrien    /* Since this is a specialization of a member template,
55852284Sobrien       we're not going to find the declaration in the class.
55952284Sobrien       For example, in:
560169689Skan
561169689Skan	 struct S { template <typename T> void f(T); };
562169689Skan	 template <> void S::f(int);
563169689Skan
56452284Sobrien       we're not going to find `S::f(int)', but there's no
56552284Sobrien       reason we should, either.  We let our callers know we didn't
56652284Sobrien       find the method, but we don't complain.  */
56752284Sobrien    return NULL_TREE;
568117395Skan
569169689Skan  /* Basic sanity check: for a template function, the template parameters
570169689Skan     either were not passed, or they are the same of DECL_TEMPLATE_PARMS.  */
571169689Skan  if (TREE_CODE (function) == TEMPLATE_DECL)
572169689Skan    {
573169689Skan      gcc_assert (!template_parms
574169689Skan		  || comp_template_parms (template_parms,
575169689Skan					  DECL_TEMPLATE_PARMS (function)));
576169689Skan      template_parms = DECL_TEMPLATE_PARMS (function);
577169689Skan    }
578169689Skan
579117395Skan  /* OK, is this a definition of a member template?  */
580169689Skan  is_template = (template_parms != NULL_TREE);
581117395Skan
582169689Skan  /* We must enter the scope here, because conversion operators are
583169689Skan     named by target type, and type equivalence relies on typenames
584169689Skan     resolving within the scope of CTYPE.  */
585169689Skan  pushed_scope = push_scope (ctype);
586169689Skan  ix = class_method_index_for_fn (complete_type (ctype), function);
587117395Skan  if (ix >= 0)
588117395Skan    {
589169689Skan      VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (ctype);
590117395Skan      tree fndecls, fndecl = 0;
591117395Skan      bool is_conv_op;
592117395Skan      const char *format = NULL;
593169689Skan
594169689Skan      for (fndecls = VEC_index (tree, methods, ix);
595117395Skan	   fndecls; fndecls = OVL_NEXT (fndecls))
596117395Skan	{
597117395Skan	  tree p1, p2;
598169689Skan
599117395Skan	  fndecl = OVL_CURRENT (fndecls);
600117395Skan	  p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
601117395Skan	  p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
60218334Speter
603117395Skan	  /* We cannot simply call decls_match because this doesn't
604117395Skan	     work for static member functions that are pretending to
605117395Skan	     be methods, and because the name may have been changed by
606169689Skan	     asm("new_name").  */
607169689Skan
608117395Skan	   /* Get rid of the this parameter on functions that become
609117395Skan	      static.  */
610117395Skan	  if (DECL_STATIC_FUNCTION_P (fndecl)
611117395Skan	      && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
612117395Skan	    p1 = TREE_CHAIN (p1);
61318334Speter
614117395Skan	  /* A member template definition only matches a member template
615117395Skan	     declaration.  */
616117395Skan	  if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
617117395Skan	    continue;
618169689Skan
619117395Skan	  if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
620117395Skan			   TREE_TYPE (TREE_TYPE (fndecl)))
621117395Skan	      && compparms (p1, p2)
622169689Skan	      && (!is_template
623169689Skan		  || comp_template_parms (template_parms,
624169689Skan					  DECL_TEMPLATE_PARMS (fndecl)))
625117395Skan	      && (DECL_TEMPLATE_SPECIALIZATION (function)
626117395Skan		  == DECL_TEMPLATE_SPECIALIZATION (fndecl))
627117395Skan	      && (!DECL_TEMPLATE_SPECIALIZATION (function)
628169689Skan		  || (DECL_TI_TEMPLATE (function)
629117395Skan		      == DECL_TI_TEMPLATE (fndecl))))
630132718Skan	    break;
631117395Skan	}
632132718Skan      if (fndecls)
633169689Skan	{
634169689Skan	  if (pushed_scope)
635169689Skan	    pop_scope (pushed_scope);
636169689Skan	  return OVL_CURRENT (fndecls);
637169689Skan	}
638169689Skan
639169689Skan      error ("prototype for %q#D does not match any in class %qT",
640117395Skan	     function, ctype);
641117395Skan      is_conv_op = DECL_CONV_FN_P (fndecl);
642117395Skan
643117395Skan      if (is_conv_op)
644117395Skan	ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
645169689Skan      fndecls = VEC_index (tree, methods, ix);
646117395Skan      while (fndecls)
64718334Speter	{
648117395Skan	  fndecl = OVL_CURRENT (fndecls);
649117395Skan	  fndecls = OVL_NEXT (fndecls);
650117395Skan
651117395Skan	  if (!fndecls && is_conv_op)
65218334Speter	    {
653169689Skan	      if (VEC_length (tree, methods) > (size_t) ++ix)
65418334Speter		{
655169689Skan		  fndecls = VEC_index (tree, methods, ix);
656117395Skan		  if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
65718334Speter		    {
658117395Skan		      fndecls = NULL_TREE;
659117395Skan		      is_conv_op = false;
66018334Speter		    }
66118334Speter		}
662117395Skan	      else
663117395Skan		is_conv_op = false;
66418334Speter	    }
665117395Skan	  if (format)
666169689Skan	    format = "                %+#D";
667117395Skan	  else if (fndecls)
668169689Skan	    format = N_("candidates are: %+#D");
669117395Skan	  else
670169689Skan	    format = N_("candidate is: %+#D");
671169689Skan	  error (format, fndecl);
67218334Speter	}
67318334Speter    }
674117395Skan  else if (!COMPLETE_TYPE_P (ctype))
675117395Skan    cxx_incomplete_type_error (function, ctype);
67618334Speter  else
677169689Skan    error ("no %q#D member function declared in class %qT",
678117395Skan	   function, ctype);
67918334Speter
68050397Sobrien  /* If we did not find the method in the class, add it to avoid
68152284Sobrien     spurious errors (unless the CTYPE is not yet defined, in which
68252284Sobrien     case we'll only confuse ourselves when the function is declared
68352284Sobrien     properly within the class.  */
68490075Sobrien  if (COMPLETE_TYPE_P (ctype))
685169689Skan    add_method (ctype, function, NULL_TREE);
686169689Skan
687169689Skan  if (pushed_scope)
688169689Skan    pop_scope (pushed_scope);
68918334Speter  return NULL_TREE;
69018334Speter}
69118334Speter
692169689Skan/* DECL is a function with vague linkage.  Remember it so that at the
693169689Skan   end of the translation unit we can decide whether or not to emit
694169689Skan   it.  */
695169689Skan
696169689Skanvoid
697169689Skannote_vague_linkage_fn (tree decl)
698169689Skan{
699169689Skan  if (!DECL_DEFERRED_FN (decl))
700169689Skan    {
701169689Skan      DECL_DEFERRED_FN (decl) = 1;
702169689Skan      DECL_DEFER_OUTPUT (decl) = 1;
703169689Skan      VEC_safe_push (tree, gc, deferred_fns, decl);
704169689Skan    }
705169689Skan}
706169689Skan
70752284Sobrien/* We have just processed the DECL, which is a static data member.
708169689Skan   The other parameters are as for cp_finish_decl.  */
70952284Sobrien
71052284Sobrienvoid
711169689Skanfinish_static_data_member_decl (tree decl,
712169689Skan				tree init, bool init_const_expr_p,
713169689Skan				tree asmspec_tree,
714169689Skan				int flags)
71552284Sobrien{
71690075Sobrien  DECL_CONTEXT (decl) = current_class_type;
71752284Sobrien
71852284Sobrien  /* We cannot call pushdecl here, because that would fill in the
71990075Sobrien     TREE_CHAIN of our decl.  Instead, we modify cp_finish_decl to do
72052284Sobrien     the right thing, namely, to put this decl out straight away.  */
72190075Sobrien
72252284Sobrien  if (! processing_template_decl)
723169689Skan    VEC_safe_push (tree, gc, pending_statics, decl);
72452284Sobrien
72590075Sobrien  if (LOCAL_CLASS_P (current_class_type))
726169689Skan    pedwarn ("local class %q#T shall not have static data member %q#D",
72790075Sobrien	     current_class_type, decl);
72890075Sobrien
72952284Sobrien  /* Static consts need not be initialized in the class definition.  */
73052284Sobrien  if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
73152284Sobrien    {
73290075Sobrien      static int explained = 0;
733169689Skan
73452284Sobrien      error ("initializer invalid for static member with constructor");
73590075Sobrien      if (!explained)
736169689Skan	{
73790075Sobrien	  error ("(an out of class initialization is required)");
73890075Sobrien	  explained = 1;
73990075Sobrien	}
74090075Sobrien      init = NULL_TREE;
74152284Sobrien    }
74252284Sobrien  /* Force the compiler to know when an uninitialized static const
74352284Sobrien     member is being used.  */
74452284Sobrien  if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
74552284Sobrien    TREE_USED (decl) = 1;
74652284Sobrien  DECL_INITIAL (decl) = init;
74752284Sobrien  DECL_IN_AGGR_P (decl) = 1;
74852284Sobrien
749169689Skan  cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
75052284Sobrien}
75152284Sobrien
752169689Skan/* DECLARATOR and DECLSPECS correspond to a class member.  The other
753169689Skan   parameters are as for cp_finish_decl.  Return the DECL for the
754169689Skan   class member declared.  */
75518334Speter
75618334Spetertree
757169689Skangrokfield (const cp_declarator *declarator,
758169689Skan	   cp_decl_specifier_seq *declspecs,
759169689Skan	   tree init, bool init_const_expr_p,
760169689Skan	   tree asmspec_tree,
761169689Skan	   tree attrlist)
76218334Speter{
76390075Sobrien  tree value;
76490075Sobrien  const char *asmspec = 0;
76518334Speter  int flags = LOOKUP_ONLYCONVERTING;
76618334Speter
76718334Speter  if (init
76818334Speter      && TREE_CODE (init) == TREE_LIST
76918334Speter      && TREE_VALUE (init) == error_mark_node
77018334Speter      && TREE_CHAIN (init) == NULL_TREE)
77150397Sobrien    init = NULL_TREE;
77218334Speter
77390075Sobrien  value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
774132718Skan  if (! value || error_operand_p (value))
77552284Sobrien    /* friend or constructor went bad.  */
776107590Sobrien    return error_mark_node;
77718334Speter
778107590Sobrien  if (TREE_CODE (value) == TYPE_DECL && init)
779107590Sobrien    {
780169689Skan      error ("typedef %qD is initialized (use __typeof__ instead)", value);
781107590Sobrien      init = NULL_TREE;
782107590Sobrien    }
783107590Sobrien
78418334Speter  /* Pass friendly classes back.  */
785132718Skan  if (value == void_type_node)
786132718Skan    return value;
78718334Speter
788132718Skan  /* Pass friend decls back.  */
789132718Skan  if ((TREE_CODE (value) == FUNCTION_DECL
790132718Skan       || TREE_CODE (value) == TEMPLATE_DECL)
791132718Skan      && DECL_CONTEXT (value) != current_class_type)
792132718Skan    return value;
793132718Skan
79418334Speter  if (DECL_NAME (value) != NULL_TREE
79518334Speter      && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
79618334Speter      && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
797169689Skan    error ("member %qD conflicts with virtual function table field name",
798169689Skan	   value);
79918334Speter
80018334Speter  /* Stash away type declarations.  */
80118334Speter  if (TREE_CODE (value) == TYPE_DECL)
80218334Speter    {
80318334Speter      DECL_NONLOCAL (value) = 1;
80418334Speter      DECL_CONTEXT (value) = current_class_type;
80518334Speter
80690075Sobrien      if (processing_template_decl)
80790075Sobrien	value = push_template_decl (value);
80818334Speter
809169689Skan      if (attrlist)
810169689Skan	{
811169689Skan	  /* Avoid storing attributes in template parameters:
812169689Skan	     tsubst is not ready to handle them.  */
813169689Skan	  tree type = TREE_TYPE (value);
814169689Skan	  if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
815169689Skan	      || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
816169689Skan	    sorry ("applying attributes to template parameters is not implemented");
817169689Skan	  else
818169689Skan	    cplus_decl_attributes (&value, attrlist, 0);
819169689Skan	}
820169689Skan
82118334Speter      return value;
82218334Speter    }
82318334Speter
82418334Speter  if (DECL_IN_AGGR_P (value))
82518334Speter    {
826169689Skan      error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
82718334Speter      return void_type_node;
82818334Speter    }
82918334Speter
830169689Skan  if (asmspec_tree && asmspec_tree != error_mark_node)
83118334Speter    asmspec = TREE_STRING_POINTER (asmspec_tree);
83218334Speter
83318334Speter  if (init)
83418334Speter    {
83590075Sobrien      if (TREE_CODE (value) == FUNCTION_DECL)
83618334Speter	{
837169689Skan	  /* Initializers for functions are rejected early in the parser.
838169689Skan	     If we get here, it must be a pure specifier for a method.  */
839169689Skan	  if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
840169689Skan	    {
841169689Skan	      gcc_assert (error_operand_p (init) || integer_zerop (init));
842169689Skan	      DECL_PURE_VIRTUAL_P (value) = 1;
843169689Skan	    }
844169689Skan	  else
845169689Skan	    {
846169689Skan	      gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
847169689Skan	      error ("initializer specified for static member function %qD",
848169689Skan		     value);
849169689Skan	    }
85018334Speter	}
85118334Speter      else if (pedantic && TREE_CODE (value) != VAR_DECL)
85218334Speter	/* Already complained in grokdeclarator.  */
85318334Speter	init = NULL_TREE;
854169689Skan      else if (!processing_template_decl)
85518334Speter	{
856169689Skan	  if (TREE_CODE (init) == CONSTRUCTOR)
857169689Skan	    init = digest_init (TREE_TYPE (value), init);
858169689Skan	  else
859169689Skan	    init = integral_constant_value (init);
860110611Skan
861169689Skan	  if (init != error_mark_node && !TREE_CONSTANT (init))
86218334Speter	    {
863169689Skan	      /* We can allow references to things that are effectively
864169689Skan		 static, since references are initialized with the
865169689Skan		 address.  */
866169689Skan	      if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
867169689Skan		  || (TREE_STATIC (init) == 0
868169689Skan		      && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
86918334Speter		{
870169689Skan		  error ("field initializer is not constant");
871169689Skan		  init = error_mark_node;
87218334Speter		}
87318334Speter	    }
87418334Speter	}
87518334Speter    }
87618334Speter
877117395Skan  if (processing_template_decl
87850397Sobrien      && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
879132718Skan    {
880132718Skan      value = push_template_decl (value);
881132718Skan      if (error_operand_p (value))
882132718Skan	return error_mark_node;
883132718Skan    }
88450397Sobrien
88518336Speter  if (attrlist)
88690075Sobrien    cplus_decl_attributes (&value, attrlist, 0);
88718336Speter
888169689Skan  switch (TREE_CODE (value))
88918334Speter    {
890169689Skan    case VAR_DECL:
891169689Skan      finish_static_data_member_decl (value, init, init_const_expr_p,
892169689Skan				      asmspec_tree, flags);
89318334Speter      return value;
894169689Skan
895169689Skan    case FIELD_DECL:
89618334Speter      if (asmspec)
897169689Skan	error ("%<asm%> specifiers are not permitted on non-static data members");
89818334Speter      if (DECL_INITIAL (value) == error_mark_node)
89918334Speter	init = error_mark_node;
900169689Skan      cp_finish_decl (value, init, /*init_const_expr_p=*/false,
901169689Skan		      NULL_TREE, flags);
90218334Speter      DECL_INITIAL (value) = init;
90318334Speter      DECL_IN_AGGR_P (value) = 1;
90418334Speter      return value;
905169689Skan
906169689Skan    case  FUNCTION_DECL:
90718334Speter      if (asmspec)
908169689Skan	set_user_assembler_name (value, asmspec);
90918334Speter
910169689Skan      cp_finish_decl (value,
911169689Skan		      /*init=*/NULL_TREE,
912169689Skan		      /*init_const_expr_p=*/false,
913169689Skan		      asmspec_tree, flags);
914169689Skan
91518334Speter      /* Pass friends back this way.  */
91618334Speter      if (DECL_FRIEND_P (value))
91718334Speter	return void_type_node;
91818334Speter
91918334Speter      DECL_IN_AGGR_P (value) = 1;
92018334Speter      return value;
921169689Skan
922169689Skan    default:
923169689Skan      gcc_unreachable ();
92418334Speter    }
92518334Speter  return NULL_TREE;
92618334Speter}
92718334Speter
92818334Speter/* Like `grokfield', but for bitfields.
92918334Speter   WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.  */
93018334Speter
93118334Spetertree
932169689Skangrokbitfield (const cp_declarator *declarator,
933169689Skan	      cp_decl_specifier_seq *declspecs, tree width)
93418334Speter{
935132718Skan  tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, NULL);
93618334Speter
937169689Skan  if (value == error_mark_node)
938169689Skan    return NULL_TREE; /* friends went bad.  */
93918334Speter
94018334Speter  /* Pass friendly classes back.  */
94118334Speter  if (TREE_CODE (value) == VOID_TYPE)
94218334Speter    return void_type_node;
94318334Speter
944169689Skan  if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
945169689Skan      && (POINTER_TYPE_P (value)
946169689Skan          || !dependent_type_p (TREE_TYPE (value))))
947169689Skan    {
948169689Skan      error ("bit-field %qD with non-integral type", value);
949169689Skan      return error_mark_node;
950169689Skan    }
951169689Skan
95218334Speter  if (TREE_CODE (value) == TYPE_DECL)
95318334Speter    {
954169689Skan      error ("cannot declare %qD to be a bit-field type", value);
95518334Speter      return NULL_TREE;
95618334Speter    }
95718334Speter
95890075Sobrien  /* Usually, finish_struct_1 catches bitfields with invalid types.
95952284Sobrien     But, in the case of bitfields with function type, we confuse
96052284Sobrien     ourselves into thinking they are member functions, so we must
96152284Sobrien     check here.  */
96252284Sobrien  if (TREE_CODE (value) == FUNCTION_DECL)
96352284Sobrien    {
964169689Skan      error ("cannot declare bit-field %qD with function type",
96590075Sobrien	     DECL_NAME (value));
96652284Sobrien      return NULL_TREE;
96752284Sobrien    }
96852284Sobrien
96918334Speter  if (DECL_IN_AGGR_P (value))
97018334Speter    {
971169689Skan      error ("%qD is already defined in the class %qT", value,
972169689Skan	     DECL_CONTEXT (value));
97318334Speter      return void_type_node;
97418334Speter    }
97518334Speter
97618334Speter  if (TREE_STATIC (value))
97718334Speter    {
978169689Skan      error ("static member %qD cannot be a bit-field", value);
97918334Speter      return NULL_TREE;
98018334Speter    }
981169689Skan  finish_decl (value, NULL_TREE, NULL_TREE);
98218334Speter
98318334Speter  if (width != error_mark_node)
98418334Speter    {
98550397Sobrien      constant_expression_warning (width);
98650397Sobrien      DECL_INITIAL (value) = width;
98752284Sobrien      SET_DECL_C_BIT_FIELD (value);
98818334Speter    }
98918334Speter
99018334Speter  DECL_IN_AGGR_P (value) = 1;
99118334Speter  return value;
99218334Speter}
99318334Speter
99418334Speter
99518334Spetervoid
996132718Skancplus_decl_attributes (tree *decl, tree attributes, int flags)
99718334Speter{
998169689Skan  if (*decl == NULL_TREE || *decl == void_type_node
999169689Skan      || *decl == error_mark_node)
100018334Speter    return;
100118334Speter
100290075Sobrien  if (TREE_CODE (*decl) == TEMPLATE_DECL)
100390075Sobrien    decl = &DECL_TEMPLATE_RESULT (*decl);
100418334Speter
100590075Sobrien  decl_attributes (decl, attributes, flags);
100618334Speter
100790075Sobrien  if (TREE_CODE (*decl) == TYPE_DECL)
100890075Sobrien    SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
100918334Speter}
101018334Speter
1011161651Skan/* Walks through the namespace- or function-scope anonymous union
1012169689Skan   OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1013161651Skan   Returns one of the fields for use in the mangled name.  */
101450397Sobrien
1015132718Skanstatic tree
1016161651Skanbuild_anon_union_vars (tree type, tree object)
101718334Speter{
101850397Sobrien  tree main_decl = NULL_TREE;
101950397Sobrien  tree field;
102050397Sobrien
102190075Sobrien  /* Rather than write the code to handle the non-union case,
102290075Sobrien     just give an error.  */
102390075Sobrien  if (TREE_CODE (type) != UNION_TYPE)
102490075Sobrien    error ("anonymous struct not inside named type");
102590075Sobrien
1026169689Skan  for (field = TYPE_FIELDS (type);
1027169689Skan       field != NULL_TREE;
102850397Sobrien       field = TREE_CHAIN (field))
102950397Sobrien    {
103050397Sobrien      tree decl;
1031132718Skan      tree ref;
103252284Sobrien
103352284Sobrien      if (DECL_ARTIFICIAL (field))
103452284Sobrien	continue;
103550397Sobrien      if (TREE_CODE (field) != FIELD_DECL)
103652284Sobrien	{
1037169689Skan	  pedwarn ("%q+#D invalid; an anonymous union can only "
1038169689Skan		   "have non-static data members", field);
103952284Sobrien	  continue;
104052284Sobrien	}
104150397Sobrien
104250397Sobrien      if (TREE_PRIVATE (field))
1043169689Skan	pedwarn ("private member %q+#D in anonymous union", field);
104450397Sobrien      else if (TREE_PROTECTED (field))
1045169689Skan	pedwarn ("protected member %q+#D in anonymous union", field);
104650397Sobrien
1047132718Skan      if (processing_template_decl)
1048169689Skan	ref = build_min_nt (COMPONENT_REF, object,
1049169689Skan			    DECL_NAME (field), NULL_TREE);
105050397Sobrien      else
1051132718Skan	ref = build_class_member_access_expr (object, field, NULL_TREE,
1052132718Skan					      false);
1053132718Skan
1054132718Skan      if (DECL_NAME (field))
105550397Sobrien	{
1056169689Skan	  tree base;
1057169689Skan
1058169689Skan	  decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1059171825Skan	  DECL_ANON_UNION_VAR_P (decl) = 1;
1060169689Skan
1061169689Skan	  base = get_base_address (object);
1062169689Skan	  TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1063169689Skan	  TREE_STATIC (decl) = TREE_STATIC (base);
1064169689Skan	  DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1065169689Skan
1066169689Skan	  SET_DECL_VALUE_EXPR (decl, ref);
1067169689Skan	  DECL_HAS_VALUE_EXPR_P (decl) = 1;
1068169689Skan
106950397Sobrien	  decl = pushdecl (decl);
107050397Sobrien	}
1071132718Skan      else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1072161651Skan	decl = build_anon_union_vars (TREE_TYPE (field), ref);
1073132718Skan      else
1074132718Skan	decl = 0;
107550397Sobrien
1076132718Skan      if (main_decl == NULL_TREE)
1077132718Skan	main_decl = decl;
1078132718Skan    }
107950397Sobrien
108050397Sobrien  return main_decl;
108118334Speter}
108218334Speter
108390075Sobrien/* Finish off the processing of a UNION_TYPE structure.  If the union is an
108490075Sobrien   anonymous union, then all members must be laid out together.  PUBLIC_P
108590075Sobrien   is nonzero if this union is not declared static.  */
108650397Sobrien
108718334Spetervoid
1088132718Skanfinish_anon_union (tree anon_union_decl)
108918334Speter{
1090146895Skan  tree type;
109150397Sobrien  tree main_decl;
1092146895Skan  bool public_p;
109318334Speter
1094146895Skan  if (anon_union_decl == error_mark_node)
1095146895Skan    return;
1096146895Skan
1097146895Skan  type = TREE_TYPE (anon_union_decl);
1098146895Skan  public_p = TREE_PUBLIC (anon_union_decl);
1099146895Skan
1100117395Skan  /* The VAR_DECL's context is the same as the TYPE's context.  */
110190075Sobrien  DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1102169689Skan
110350397Sobrien  if (TYPE_FIELDS (type) == NULL_TREE)
110418334Speter    return;
110518334Speter
110618334Speter  if (public_p)
110718334Speter    {
110890075Sobrien      error ("namespace-scope anonymous aggregates must be static");
110918334Speter      return;
111018334Speter    }
111118334Speter
1112161651Skan  main_decl = build_anon_union_vars (type, anon_union_decl);
1113169689Skan  if (main_decl == error_mark_node)
1114169689Skan    return;
1115132718Skan  if (main_decl == NULL_TREE)
1116104752Skan    {
1117169689Skan      warning (0, "anonymous union with no members");
1118132718Skan      return;
111952284Sobrien    }
112052284Sobrien
1121132718Skan  if (!processing_template_decl)
112218334Speter    {
1123132718Skan      /* Use main_decl to set the mangled name.  */
1124132718Skan      DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1125132718Skan      mangle_decl (anon_union_decl);
1126132718Skan      DECL_NAME (anon_union_decl) = NULL_TREE;
112718334Speter    }
1128132718Skan
1129132718Skan  pushdecl (anon_union_decl);
1130132718Skan  if (building_stmt_tree ()
1131132718Skan      && at_function_scope_p ())
1132169689Skan    add_decl_expr (anon_union_decl);
1133132718Skan  else if (!processing_template_decl)
1134169689Skan    rest_of_decl_compilation (anon_union_decl,
1135132718Skan			      toplevel_bindings_p (), at_eof);
113618334Speter}
113718334Speter
113818334Speter/* Auxiliary functions to make type signatures for
113918334Speter   `operator new' and `operator delete' correspond to
114018334Speter   what compiler will be expecting.  */
114118334Speter
114218334Spetertree
1143132718Skancoerce_new_type (tree type)
114418334Speter{
114590075Sobrien  int e = 0;
114690075Sobrien  tree args = TYPE_ARG_TYPES (type);
114718334Speter
1148169689Skan  gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1149169689Skan
115090075Sobrien  if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1151169689Skan    {
1152169689Skan      e = 1;
1153169689Skan      error ("%<operator new%> must return type %qT", ptr_type_node);
1154169689Skan    }
115518334Speter
115690075Sobrien  if (!args || args == void_list_node
1157110611Skan      || !same_type_p (TREE_VALUE (args), size_type_node))
115890075Sobrien    {
115990075Sobrien      e = 2;
116090075Sobrien      if (args && args != void_list_node)
1161169689Skan	args = TREE_CHAIN (args);
1162169689Skan      pedwarn ("%<operator new%> takes type %<size_t%> (%qT) "
1163169689Skan	       "as first parameter", size_type_node);
116490075Sobrien    }
116590075Sobrien  switch (e)
116690075Sobrien  {
116790075Sobrien    case 2:
1168110611Skan      args = tree_cons (NULL_TREE, size_type_node, args);
1169132718Skan      /* Fall through.  */
117090075Sobrien    case 1:
117190075Sobrien      type = build_exception_variant
1172169689Skan	      (build_function_type (ptr_type_node, args),
1173169689Skan	       TYPE_RAISES_EXCEPTIONS (type));
1174132718Skan      /* Fall through.  */
117590075Sobrien    default:;
117690075Sobrien  }
117718334Speter  return type;
117818334Speter}
117918334Speter
118018334Spetertree
1181132718Skancoerce_delete_type (tree type)
118218334Speter{
118390075Sobrien  int e = 0;
118490075Sobrien  tree args = TYPE_ARG_TYPES (type);
118518334Speter
1186169689Skan  gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1187169689Skan
118890075Sobrien  if (!same_type_p (TREE_TYPE (type), void_type_node))
1189169689Skan    {
1190169689Skan      e = 1;
1191169689Skan      error ("%<operator delete%> must return type %qT", void_type_node);
1192169689Skan    }
119350397Sobrien
119490075Sobrien  if (!args || args == void_list_node
119590075Sobrien      || !same_type_p (TREE_VALUE (args), ptr_type_node))
119618334Speter    {
119790075Sobrien      e = 2;
119890075Sobrien      if (args && args != void_list_node)
1199169689Skan	args = TREE_CHAIN (args);
1200169689Skan      error ("%<operator delete%> takes type %qT as first parameter",
1201169689Skan	     ptr_type_node);
120218334Speter    }
120390075Sobrien  switch (e)
120490075Sobrien  {
120590075Sobrien    case 2:
120690075Sobrien      args = tree_cons (NULL_TREE, ptr_type_node, args);
1207132718Skan      /* Fall through.  */
120890075Sobrien    case 1:
120990075Sobrien      type = build_exception_variant
1210169689Skan	      (build_function_type (void_type_node, args),
1211169689Skan	       TYPE_RAISES_EXCEPTIONS (type));
1212132718Skan      /* Fall through.  */
121390075Sobrien    default:;
121490075Sobrien  }
121550397Sobrien
121618334Speter  return type;
121718334Speter}
121818334Speter
121918334Speterstatic void
1220132718Skanmark_vtable_entries (tree decl)
122118334Speter{
1222169689Skan  tree fnaddr;
1223169689Skan  unsigned HOST_WIDE_INT idx;
122418334Speter
1225169689Skan  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1226169689Skan			      idx, fnaddr)
122750397Sobrien    {
122852284Sobrien      tree fn;
1229132718Skan
1230132718Skan      STRIP_NOPS (fnaddr);
1231132718Skan
123290075Sobrien      if (TREE_CODE (fnaddr) != ADDR_EXPR
123390075Sobrien	  && TREE_CODE (fnaddr) != FDESC_EXPR)
123490075Sobrien	/* This entry is an offset: a virtual base class offset, a
123590075Sobrien	   virtual call offset, an RTTI offset, etc.  */
123652284Sobrien	continue;
123752284Sobrien
123852284Sobrien      fn = TREE_OPERAND (fnaddr, 0);
123918334Speter      TREE_ADDRESSABLE (fn) = 1;
124090075Sobrien      /* When we don't have vcall offsets, we output thunks whenever
124190075Sobrien	 we output the vtables that contain them.  With vcall offsets,
124290075Sobrien	 we know all the thunks we'll need when we emit a virtual
124390075Sobrien	 function, so we emit the thunks there instead.  */
1244169689Skan      if (DECL_THUNK_P (fn))
124590075Sobrien	use_thunk (fn, /*emit_p=*/0);
124650397Sobrien      mark_used (fn);
124718334Speter    }
124818334Speter}
124918334Speter
125050397Sobrien/* Set DECL up to have the closest approximation of "initialized common"
125150397Sobrien   linkage available.  */
125250397Sobrien
125350397Sobrienvoid
1254132718Skancomdat_linkage (tree decl)
125550397Sobrien{
125650397Sobrien  if (flag_weak)
125750397Sobrien    make_decl_one_only (decl);
1258169689Skan  else if (TREE_CODE (decl) == FUNCTION_DECL
125990075Sobrien	   || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
126090075Sobrien    /* We can just emit function and compiler-generated variables
126190075Sobrien       statically; having multiple copies is (for the most part) only
1262169689Skan       a waste of space.
126390075Sobrien
126490075Sobrien       There are two correctness issues, however: the address of a
126590075Sobrien       template instantiation with external linkage should be the
126690075Sobrien       same, independent of what translation unit asks for the
126790075Sobrien       address, and this will not hold when we emit multiple copies of
1268169689Skan       the function.  However, there's little else we can do.
126990075Sobrien
127090075Sobrien       Also, by default, the typeinfo implementation assumes that
127190075Sobrien       there will be only one copy of the string used as the name for
127290075Sobrien       each type.  Therefore, if weak symbols are unavailable, the
127390075Sobrien       run-time library should perform a more conservative check; it
127490075Sobrien       should perform a string comparison, rather than an address
127590075Sobrien       comparison.  */
127652284Sobrien    TREE_PUBLIC (decl) = 0;
127750397Sobrien  else
127852284Sobrien    {
127952284Sobrien      /* Static data member template instantiations, however, cannot
128052284Sobrien	 have multiple copies.  */
128152284Sobrien      if (DECL_INITIAL (decl) == 0
128252284Sobrien	  || DECL_INITIAL (decl) == error_mark_node)
128352284Sobrien	DECL_COMMON (decl) = 1;
128452284Sobrien      else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
128552284Sobrien	{
128652284Sobrien	  DECL_COMMON (decl) = 1;
128752284Sobrien	  DECL_INITIAL (decl) = error_mark_node;
128852284Sobrien	}
1289117395Skan      else if (!DECL_EXPLICIT_INSTANTIATION (decl))
129052284Sobrien	{
129152284Sobrien	  /* We can't do anything useful; leave vars for explicit
129252284Sobrien	     instantiation.  */
129352284Sobrien	  DECL_EXTERNAL (decl) = 1;
129452284Sobrien	  DECL_NOT_REALLY_EXTERN (decl) = 0;
129552284Sobrien	}
129652284Sobrien    }
129750397Sobrien
129850397Sobrien  if (DECL_LANG_SPECIFIC (decl))
129950397Sobrien    DECL_COMDAT (decl) = 1;
130050397Sobrien}
130150397Sobrien
130250397Sobrien/* For win32 we also want to put explicit instantiations in
130350397Sobrien   linkonce sections, so that they will be merged with implicit
1304169689Skan   instantiations; otherwise we get duplicate symbol errors.
1305169689Skan   For Darwin we do not want explicit instantiations to be
1306169689Skan   linkonce.  */
130750397Sobrien
130850397Sobrienvoid
1309132718Skanmaybe_make_one_only (tree decl)
131050397Sobrien{
131156385Sobrien  /* We used to say that this was not necessary on targets that support weak
131256385Sobrien     symbols, because the implicit instantiations will defer to the explicit
131356385Sobrien     one.  However, that's not actually the case in SVR4; a strong definition
131456385Sobrien     after a weak one is an error.  Also, not making explicit
131556385Sobrien     instantiations one_only means that we can end up with two copies of
1316117395Skan     some template instantiations.  */
131790075Sobrien  if (! flag_weak)
131850397Sobrien    return;
131950397Sobrien
1320169689Skan  /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
132152284Sobrien     we can get away with not emitting them if they aren't used.  We need
132252284Sobrien     to for variables so that cp_finish_decl will update their linkage,
132352284Sobrien     because their DECL_INITIAL may not have been set properly yet.  */
132450397Sobrien
1325169689Skan  if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1326169689Skan      || (! DECL_EXPLICIT_INSTANTIATION (decl)
1327169689Skan	  && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
132896263Sobrien    {
1329169689Skan      make_decl_one_only (decl);
133050397Sobrien
1331169689Skan      if (TREE_CODE (decl) == VAR_DECL)
133218334Speter	{
1333169689Skan	  DECL_COMDAT (decl) = 1;
1334169689Skan	  /* Mark it needed so we don't forget to emit it.  */
1335169689Skan	  mark_decl_referenced (decl);
133618334Speter	}
133718334Speter    }
133818334Speter}
133918334Speter
134050397Sobrien/* Determine whether or not we want to specifically import or export CTYPE,
134150397Sobrien   using various heuristics.  */
134250397Sobrien
134390075Sobrienstatic void
1344132718Skanimport_export_class (tree ctype)
134518334Speter{
134650397Sobrien  /* -1 for imported, 1 for exported.  */
134750397Sobrien  int import_export = 0;
134818334Speter
134990075Sobrien  /* It only makes sense to call this function at EOF.  The reason is
135090075Sobrien     that this function looks at whether or not the first non-inline
135190075Sobrien     non-abstract virtual member function has been defined in this
135290075Sobrien     translation unit.  But, we can't possibly know that until we've
135390075Sobrien     seen the entire translation unit.  */
1354169689Skan  gcc_assert (at_eof);
135590075Sobrien
135650397Sobrien  if (CLASSTYPE_INTERFACE_KNOWN (ctype))
135750397Sobrien    return;
135850397Sobrien
1359169689Skan  /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
136052284Sobrien     we will have CLASSTYPE_INTERFACE_ONLY set but not
136152284Sobrien     CLASSTYPE_INTERFACE_KNOWN.  In that case, we don't want to use this
136252284Sobrien     heuristic because someone will supply a #pragma implementation
136352284Sobrien     elsewhere, and deducing it here would produce a conflict.  */
136452284Sobrien  if (CLASSTYPE_INTERFACE_ONLY (ctype))
136552284Sobrien    return;
136652284Sobrien
136750397Sobrien  if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
136850397Sobrien    import_export = -1;
136952284Sobrien  else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
137052284Sobrien    import_export = 1;
1371169689Skan  else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1372169689Skan	   && !flag_implicit_templates)
1373169689Skan    /* For a template class, without -fimplicit-templates, check the
1374169689Skan       repository.  If the virtual table is assigned to this
1375169689Skan       translation unit, then export the class; otherwise, import
1376169689Skan       it.  */
1377169689Skan      import_export = repo_export_class_p (ctype) ? 1 : -1;
1378169689Skan  else if (TYPE_POLYMORPHIC_P (ctype))
137918334Speter    {
1380169689Skan      /* The ABI specifies that the virtual table and associated
1381169689Skan	 information are emitted with the key method, if any.  */
1382117395Skan      tree method = CLASSTYPE_KEY_METHOD (ctype);
1383169689Skan      /* If weak symbol support is not available, then we must be
1384169689Skan	 careful not to emit the vtable when the key function is
1385169689Skan	 inline.  An inline function can be defined in multiple
1386169689Skan	 translation units.  If we were to emit the vtable in each
1387169689Skan	 translation unit containing a definition, we would get
1388169689Skan	 multiple definition errors at link-time.  */
1389169689Skan      if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
139090075Sobrien	import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
139118334Speter    }
139252284Sobrien
1393169689Skan  /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1394169689Skan     a definition anywhere else.  */
1395169689Skan  if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
139652284Sobrien    import_export = 0;
139718334Speter
1398169689Skan  /* Allow backends the chance to overrule the decision.  */
1399169689Skan  if (targetm.cxx.import_export_class)
1400169689Skan    import_export = targetm.cxx.import_export_class (ctype, import_export);
1401169689Skan
140250397Sobrien  if (import_export)
140318334Speter    {
140450397Sobrien      SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
140550397Sobrien      CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
140618334Speter    }
140718334Speter}
140852284Sobrien
1409132718Skan/* Return true if VAR has already been provided to the back end; in that
1410132718Skan   case VAR should not be modified further by the front end.  */
1411132718Skanstatic bool
1412132718Skanvar_finalized_p (tree var)
141352284Sobrien{
1414169689Skan  return cgraph_varpool_node (var)->finalized;
141552284Sobrien}
141652284Sobrien
1417169689Skan/* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1418169689Skan   must be emitted in this translation unit.  Mark it as such.  */
1419169689Skan
1420169689Skanvoid
1421169689Skanmark_needed (tree decl)
1422169689Skan{
1423169689Skan  /* It's possible that we no longer need to set
1424169689Skan     TREE_SYMBOL_REFERENCED here directly, but doing so is
1425169689Skan     harmless.  */
1426169689Skan  TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1427169689Skan  mark_decl_referenced (decl);
1428169689Skan}
1429169689Skan
1430169689Skan/* DECL is either a FUNCTION_DECL or a VAR_DECL.  This function
1431169689Skan   returns true if a definition of this entity should be provided in
1432169689Skan   this object file.  Callers use this function to determine whether
1433169689Skan   or not to let the back end know that a definition of DECL is
1434169689Skan   available in this translation unit.  */
1435169689Skan
1436169689Skanbool
1437169689Skandecl_needed_p (tree decl)
1438169689Skan{
1439169689Skan  gcc_assert (TREE_CODE (decl) == VAR_DECL
1440169689Skan	      || TREE_CODE (decl) == FUNCTION_DECL);
1441169689Skan  /* This function should only be called at the end of the translation
1442169689Skan     unit.  We cannot be sure of whether or not something will be
1443169689Skan     COMDAT until that point.  */
1444169689Skan  gcc_assert (at_eof);
1445169689Skan
1446169689Skan  /* All entities with external linkage that are not COMDAT should be
1447169689Skan     emitted; they may be referred to from other object files.  */
1448169689Skan  if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1449169689Skan    return true;
1450169689Skan  /* If this entity was used, let the back-end see it; it will decide
1451169689Skan     whether or not to emit it into the object file.  */
1452169689Skan  if (TREE_USED (decl)
1453169689Skan      || (DECL_ASSEMBLER_NAME_SET_P (decl)
1454169689Skan	  && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
1455169689Skan      return true;
1456169689Skan  /* Otherwise, DECL does not need to be emitted -- yet.  A subsequent
1457169689Skan     reference to DECL might cause it to be emitted later.  */
1458169689Skan  return false;
1459169689Skan}
1460169689Skan
1461117395Skan/* If necessary, write out the vtables for the dynamic class CTYPE.
1462132718Skan   Returns true if any vtables were emitted.  */
1463117395Skan
1464132718Skanstatic bool
1465117395Skanmaybe_emit_vtables (tree ctype)
146618334Speter{
1467117395Skan  tree vtbl;
1468117395Skan  tree primary_vtbl;
1469169689Skan  int needed = 0;
1470117395Skan
1471117395Skan  /* If the vtables for this class have already been emitted there is
1472117395Skan     nothing more to do.  */
1473117395Skan  primary_vtbl = CLASSTYPE_VTABLES (ctype);
1474132718Skan  if (var_finalized_p (primary_vtbl))
1475132718Skan    return false;
1476117395Skan  /* Ignore dummy vtables made by get_vtable_decl.  */
1477117395Skan  if (TREE_TYPE (primary_vtbl) == void_type_node)
1478132718Skan    return false;
1479117395Skan
1480169689Skan  /* On some targets, we cannot determine the key method until the end
1481169689Skan     of the translation unit -- which is when this function is
1482169689Skan     called.  */
1483169689Skan  if (!targetm.cxx.key_method_may_be_inline ())
1484169689Skan    determine_key_method (ctype);
148550397Sobrien
1486117395Skan  /* See if any of the vtables are needed.  */
1487117395Skan  for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1488132718Skan    {
1489169689Skan      import_export_decl (vtbl);
1490169689Skan      if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1491169689Skan	needed = 1;
1492132718Skan    }
1493169689Skan  if (!needed)
149418334Speter    {
1495117395Skan      /* If the references to this class' vtables are optimized away,
1496117395Skan	 still emit the appropriate debugging information.  See
1497117395Skan	 dfs_debug_mark.  */
1498169689Skan      if (DECL_COMDAT (primary_vtbl)
1499117395Skan	  && CLASSTYPE_DEBUG_REQUESTED (ctype))
1500117395Skan	note_debug_info_needed (ctype);
1501132718Skan      return false;
1502117395Skan    }
1503117395Skan
1504117395Skan  /* The ABI requires that we emit all of the vtables if we emit any
1505117395Skan     of them.  */
1506117395Skan  for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1507117395Skan    {
1508169689Skan      /* Mark entities references from the virtual table as used.  */
1509117395Skan      mark_vtable_entries (vtbl);
1510132718Skan
1511117395Skan      if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1512132718Skan	{
1513169689Skan	  tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl));
1514169689Skan
1515132718Skan	  /* It had better be all done at compile-time.  */
1516169689Skan	  gcc_assert (!expr);
1517132718Skan	}
151818334Speter
1519169689Skan      /* Write it out.  */
1520169689Skan      DECL_EXTERNAL (vtbl) = 0;
1521169689Skan      rest_of_decl_compilation (vtbl, 1, 1);
152218334Speter
152390075Sobrien      /* Because we're only doing syntax-checking, we'll never end up
152490075Sobrien	 actually marking the variable as written.  */
152590075Sobrien      if (flag_syntax_only)
1526117395Skan	TREE_ASM_WRITTEN (vtbl) = 1;
152718334Speter    }
152818334Speter
1529117395Skan  /* Since we're writing out the vtable here, also write the debug
1530117395Skan     info.  */
1531117395Skan  note_debug_info_needed (ctype);
153290075Sobrien
1533132718Skan  return true;
153418334Speter}
153518334Speter
1536169689Skan/* A special return value from type_visibility meaning internal
1537169689Skan   linkage.  */
153818334Speter
1539169689Skanenum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
1540169689Skan
1541169689Skan/* walk_tree helper function for type_visibility.  */
1542169689Skan
1543169689Skanstatic tree
1544169689Skanmin_vis_r (tree *tp, int *walk_subtrees, void *data)
1545169689Skan{
1546169689Skan  int *vis_p = (int *)data;
1547169689Skan  if (! TYPE_P (*tp))
1548169689Skan    {
1549169689Skan      *walk_subtrees = 0;
1550169689Skan    }
1551169689Skan  else if (CLASS_TYPE_P (*tp))
1552169689Skan    {
1553169689Skan      if (!TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
1554169689Skan	{
1555169689Skan	  *vis_p = VISIBILITY_ANON;
1556169689Skan	  return *tp;
1557169689Skan	}
1558169689Skan      else if (CLASSTYPE_VISIBILITY (*tp) > *vis_p)
1559169689Skan	*vis_p = CLASSTYPE_VISIBILITY (*tp);
1560169689Skan    }
1561169689Skan  return NULL;
1562169689Skan}
1563169689Skan
1564169689Skan/* Returns the visibility of TYPE, which is the minimum visibility of its
1565169689Skan   component types.  */
1566169689Skan
1567169689Skanstatic int
1568169689Skantype_visibility (tree type)
1569169689Skan{
1570169689Skan  int vis = VISIBILITY_DEFAULT;
1571169689Skan  walk_tree_without_duplicates (&type, min_vis_r, &vis);
1572169689Skan  return vis;
1573169689Skan}
1574169689Skan
1575169689Skan/* Limit the visibility of DECL to VISIBILITY, if not explicitly
1576169689Skan   specified (or if VISIBILITY is static).  */
1577169689Skan
1578169689Skanstatic bool
1579169689Skanconstrain_visibility (tree decl, int visibility)
1580169689Skan{
1581169689Skan  if (visibility == VISIBILITY_ANON)
1582169689Skan    {
1583169689Skan      /* extern "C" declarations aren't affected by the anonymous
1584169689Skan	 namespace.  */
1585169689Skan      if (!DECL_EXTERN_C_P (decl))
1586169689Skan	{
1587169689Skan	  TREE_PUBLIC (decl) = 0;
1588169689Skan	  DECL_INTERFACE_KNOWN (decl) = 1;
1589169689Skan	  if (DECL_LANG_SPECIFIC (decl))
1590169689Skan	    DECL_NOT_REALLY_EXTERN (decl) = 1;
1591169689Skan	}
1592169689Skan    }
1593169689Skan  else if (visibility > DECL_VISIBILITY (decl)
1594169689Skan	   && !DECL_VISIBILITY_SPECIFIED (decl))
1595169689Skan    {
1596169689Skan      DECL_VISIBILITY (decl) = visibility;
1597169689Skan      return true;
1598169689Skan    }
1599169689Skan  return false;
1600169689Skan}
1601169689Skan
1602169689Skan/* Constrain the visibility of DECL based on the visibility of its template
1603169689Skan   arguments.  */
1604169689Skan
1605169689Skanstatic void
1606169689Skanconstrain_visibility_for_template (tree decl, tree targs)
1607169689Skan{
1608169689Skan  /* If this is a template instantiation, check the innermost
1609169689Skan     template args for visibility constraints.  The outer template
1610169689Skan     args are covered by the class check.  */
1611169689Skan  tree args = INNERMOST_TEMPLATE_ARGS (targs);
1612169689Skan  int i;
1613169689Skan  for (i = TREE_VEC_LENGTH (args); i > 0; --i)
1614169689Skan    {
1615169689Skan      int vis = 0;
1616169689Skan
1617169689Skan      tree arg = TREE_VEC_ELT (args, i-1);
1618169689Skan      if (TYPE_P (arg))
1619169689Skan	vis = type_visibility (arg);
1620169689Skan      else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
1621169689Skan	{
1622169689Skan	  STRIP_NOPS (arg);
1623169689Skan	  if (TREE_CODE (arg) == ADDR_EXPR)
1624169689Skan	    arg = TREE_OPERAND (arg, 0);
1625169689Skan	  if (TREE_CODE (arg) == VAR_DECL
1626169689Skan	      || TREE_CODE (arg) == FUNCTION_DECL)
1627169689Skan	    {
1628169689Skan	      if (! TREE_PUBLIC (arg))
1629169689Skan		vis = VISIBILITY_ANON;
1630169689Skan	      else
1631169689Skan		vis = DECL_VISIBILITY (arg);
1632169689Skan	    }
1633169689Skan	}
1634169689Skan      if (vis)
1635169689Skan	constrain_visibility (decl, vis);
1636169689Skan    }
1637169689Skan}
1638169689Skan
1639169689Skan/* Like c_determine_visibility, but with additional C++-specific
1640169689Skan   behavior.
1641169689Skan
1642169689Skan   Function-scope entities can rely on the function's visibility because
1643169689Skan   it is set in start_preparsed_function.
1644169689Skan
1645169689Skan   Class-scope entities cannot rely on the class's visibility until the end
1646169689Skan   of the enclosing class definition.
1647169689Skan
1648169689Skan   Note that because namespaces have multiple independent definitions,
1649169689Skan   namespace visibility is handled elsewhere using the #pragma visibility
1650169689Skan   machinery rather than by decorating the namespace declaration.
1651169689Skan
1652169689Skan   The goal is for constraints from the type to give a diagnostic, and
1653169689Skan   other constraints to be applied silently.  */
1654169689Skan
165518334Spetervoid
1656169689Skandetermine_visibility (tree decl)
1657169689Skan{
1658169689Skan  tree class_type = NULL_TREE;
1659169689Skan  bool use_template;
1660169689Skan
1661169689Skan  /* Remember that all decls get VISIBILITY_DEFAULT when built.  */
1662169689Skan
1663169689Skan  /* Only relevant for names with external linkage.  */
1664169689Skan  if (!TREE_PUBLIC (decl))
1665169689Skan    return;
1666169689Skan
1667169689Skan  /* Cloned constructors and destructors get the same visibility as
1668169689Skan     the underlying function.  That should be set up in
1669169689Skan     maybe_clone_body.  */
1670169689Skan  gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
1671169689Skan
1672169689Skan  if (TREE_CODE (decl) == TYPE_DECL)
1673169689Skan    {
1674169689Skan      if (CLASS_TYPE_P (TREE_TYPE (decl)))
1675169689Skan	use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
1676169689Skan      else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
1677169689Skan	use_template = 1;
1678169689Skan      else
1679169689Skan	use_template = 0;
1680169689Skan    }
1681169689Skan  else if (DECL_LANG_SPECIFIC (decl))
1682169689Skan    use_template = DECL_USE_TEMPLATE (decl);
1683169689Skan  else
1684169689Skan    use_template = 0;
1685169689Skan
1686169689Skan  /* Anything that is exported must have default visibility.  */
1687169689Skan  if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1688169689Skan      && lookup_attribute ("dllexport",
1689169689Skan			   TREE_CODE (decl) == TYPE_DECL
1690169689Skan			   ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1691169689Skan			   : DECL_ATTRIBUTES (decl)))
1692169689Skan    {
1693169689Skan      DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1694169689Skan      DECL_VISIBILITY_SPECIFIED (decl) = 1;
1695169689Skan    }
1696169689Skan
1697169689Skan  /* If DECL is a member of a class, visibility specifiers on the
1698169689Skan     class can influence the visibility of the DECL.  */
1699169689Skan  if (DECL_CLASS_SCOPE_P (decl))
1700169689Skan    class_type = DECL_CONTEXT (decl);
1701169689Skan  else if (TREE_CODE (decl) == VAR_DECL
1702169689Skan	   && DECL_TINFO_P (decl)
1703169689Skan	   && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl))))
1704169689Skan    class_type = TREE_TYPE (DECL_NAME (decl));
1705169689Skan  else
1706169689Skan    {
1707169689Skan      /* Not a class member.  */
1708169689Skan
1709169689Skan      /* Virtual tables have DECL_CONTEXT set to their associated class,
1710169689Skan	 so they are automatically handled above.  */
1711169689Skan      gcc_assert (TREE_CODE (decl) != VAR_DECL
1712169689Skan		  || !DECL_VTABLE_OR_VTT_P (decl));
1713169689Skan
1714169689Skan      if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
1715169689Skan	{
1716169689Skan	  /* Local statics and classes get the visibility of their
1717169689Skan	     containing function by default, except that
1718169689Skan	     -fvisibility-inlines-hidden doesn't affect them.  */
1719169689Skan	  tree fn = DECL_CONTEXT (decl);
1720169689Skan	  if (DECL_VISIBILITY_SPECIFIED (fn) || ! DECL_CLASS_SCOPE_P (fn))
1721169689Skan	    {
1722169689Skan	      DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
1723169689Skan	      DECL_VISIBILITY_SPECIFIED (decl) =
1724169689Skan		DECL_VISIBILITY_SPECIFIED (fn);
1725169689Skan	    }
1726169689Skan	  else
1727169689Skan	    determine_visibility_from_class (decl, DECL_CONTEXT (fn));
1728169689Skan
1729169689Skan	  /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
1730169689Skan	     but have no TEMPLATE_INFO, so don't try to check it.  */
1731169689Skan	  use_template = 0;
1732169689Skan	}
1733260075Spfg      else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl)
1734260075Spfg	       && flag_visibility_ms_compat)
1735260075Spfg	{
1736260075Spfg	  /* Under -fvisibility-ms-compat, types are visible by default,
1737260075Spfg	     even though their contents aren't.  */
1738260075Spfg	  tree underlying_type = TREE_TYPE (DECL_NAME (decl));
1739260075Spfg	  int underlying_vis = type_visibility (underlying_type);
1740260075Spfg	  if (underlying_vis == VISIBILITY_ANON
1741260075Spfg	      || CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type))
1742260075Spfg	    constrain_visibility (decl, underlying_vis);
1743260075Spfg	  else
1744260075Spfg	    DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1745260075Spfg	}
1746169689Skan      else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
1747169689Skan	{
1748169689Skan	  /* tinfo visibility is based on the type it's for.  */
1749169689Skan	  constrain_visibility
1750169689Skan	    (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))));
1751169689Skan	}
1752169689Skan      else if (use_template)
1753169689Skan	/* Template instantiations and specializations get visibility based
1754169689Skan	   on their template unless they override it with an attribute.  */;
1755169689Skan      else if (! DECL_VISIBILITY_SPECIFIED (decl))
1756169689Skan	{
1757169689Skan	  /* Set default visibility to whatever the user supplied with
1758169689Skan	     #pragma GCC visibility or a namespace visibility attribute.  */
1759169689Skan	  DECL_VISIBILITY (decl) = default_visibility;
1760169689Skan	  DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
1761169689Skan	}
1762169689Skan    }
1763169689Skan
1764169689Skan  if (use_template)
1765169689Skan    {
1766169689Skan      /* If the specialization doesn't specify visibility, use the
1767169689Skan	 visibility from the template.  */
1768169689Skan      tree tinfo = (TREE_CODE (decl) == TYPE_DECL
1769169689Skan		    ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
1770169689Skan		    : DECL_TEMPLATE_INFO (decl));
1771169689Skan      tree args = TI_ARGS (tinfo);
1772169689Skan
1773169689Skan      if (args != error_mark_node)
1774169689Skan	{
1775169689Skan	  int depth = TMPL_ARGS_DEPTH (args);
1776169689Skan	  tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
1777169689Skan
1778169689Skan	  if (!DECL_VISIBILITY_SPECIFIED (decl))
1779169689Skan	    {
1780169689Skan	      DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
1781169689Skan	      DECL_VISIBILITY_SPECIFIED (decl)
1782169689Skan		= DECL_VISIBILITY_SPECIFIED (pattern);
1783169689Skan	    }
1784169689Skan
1785169689Skan	  /* FIXME should TMPL_ARGS_DEPTH really return 1 for null input? */
1786169689Skan	  if (args && depth > template_class_depth (class_type))
1787169689Skan	    /* Limit visibility based on its template arguments.  */
1788169689Skan	    constrain_visibility_for_template (decl, args);
1789169689Skan	}
1790169689Skan    }
1791169689Skan
1792169689Skan  if (class_type)
1793169689Skan    determine_visibility_from_class (decl, class_type);
1794169689Skan
1795169689Skan  if (decl_anon_ns_mem_p (decl))
1796169689Skan    /* Names in an anonymous namespace get internal linkage.
1797169689Skan       This might change once we implement export.  */
1798169689Skan    constrain_visibility (decl, VISIBILITY_ANON);
1799169689Skan  else if (TREE_CODE (decl) != TYPE_DECL)
1800169689Skan    {
1801169689Skan      /* Propagate anonymity from type to decl.  */
1802169689Skan      int tvis = type_visibility (TREE_TYPE (decl));
1803169689Skan      if (tvis == VISIBILITY_ANON)
1804169689Skan	constrain_visibility (decl, tvis);
1805169689Skan    }
1806169689Skan}
1807169689Skan
1808169689Skan/* By default, static data members and function members receive
1809169689Skan   the visibility of their containing class.  */
1810169689Skan
1811169689Skanstatic void
1812169689Skandetermine_visibility_from_class (tree decl, tree class_type)
1813169689Skan{
1814169689Skan  if (visibility_options.inlines_hidden
1815169689Skan      /* Don't do this for inline templates; specializations might not be
1816169689Skan	 inline, and we don't want them to inherit the hidden
1817169689Skan	 visibility.  We'll set it here for all inline instantiations.  */
1818169689Skan      && !processing_template_decl
1819169689Skan      && ! DECL_VISIBILITY_SPECIFIED (decl)
1820169689Skan      && TREE_CODE (decl) == FUNCTION_DECL
1821169689Skan      && DECL_DECLARED_INLINE_P (decl)
1822169689Skan      && (! DECL_LANG_SPECIFIC (decl)
1823169689Skan	  || ! DECL_EXPLICIT_INSTANTIATION (decl)))
1824169689Skan    DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1825169689Skan  else if (!DECL_VISIBILITY_SPECIFIED (decl))
1826169689Skan    {
1827169689Skan      /* Default to the class visibility.  */
1828169689Skan      DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
1829169689Skan      DECL_VISIBILITY_SPECIFIED (decl)
1830169689Skan	= CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
1831169689Skan    }
1832169689Skan
1833169689Skan  /* Give the target a chance to override the visibility associated
1834169689Skan     with DECL.  */
1835169689Skan  if (TREE_CODE (decl) == VAR_DECL
1836169689Skan      && (DECL_TINFO_P (decl)
1837169689Skan	  || (DECL_VTABLE_OR_VTT_P (decl)
1838169689Skan	      /* Construction virtual tables are not exported because
1839169689Skan		 they cannot be referred to from other object files;
1840169689Skan		 their name is not standardized by the ABI.  */
1841169689Skan	      && !DECL_CONSTRUCTION_VTABLE_P (decl)))
1842169689Skan      && TREE_PUBLIC (decl)
1843169689Skan      && !DECL_REALLY_EXTERN (decl)
1844169689Skan      && !DECL_VISIBILITY_SPECIFIED (decl)
1845169689Skan      && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
1846169689Skan    targetm.cxx.determine_class_data_visibility (decl);
1847169689Skan}
1848169689Skan
1849169689Skan/* Constrain the visibility of a class TYPE based on the visibility of its
1850169689Skan   field types.  Warn if any fields require lesser visibility.  */
1851169689Skan
1852169689Skanvoid
1853169689Skanconstrain_class_visibility (tree type)
1854169689Skan{
1855169689Skan  tree binfo;
1856169689Skan  tree t;
1857169689Skan  int i;
1858169689Skan
1859169689Skan  int vis = type_visibility (type);
1860169689Skan
1861169689Skan  if (vis == VISIBILITY_ANON
1862169689Skan      || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
1863169689Skan    return;
1864169689Skan
1865169689Skan  /* Don't warn about visibility if the class has explicit visibility.  */
1866169689Skan  if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
1867169689Skan    vis = VISIBILITY_INTERNAL;
1868169689Skan
1869169689Skan  for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
1870169689Skan    if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
1871169689Skan      {
1872169689Skan	tree ftype = strip_array_types (TREE_TYPE (t));
1873169689Skan	int subvis = type_visibility (ftype);
1874169689Skan
1875169689Skan	if (subvis == VISIBILITY_ANON)
1876220150Smm	  {
1877220150Smm	    if (!in_main_input_context ())
1878220150Smm	      warning (0, "\
1879169689Skan%qT has a field %qD whose type uses the anonymous namespace",
1880169689Skan		   type, t);
1881220150Smm	  }
1882169689Skan	else if (IS_AGGR_TYPE (ftype)
1883169689Skan		 && vis < VISIBILITY_HIDDEN
1884169689Skan		 && subvis >= VISIBILITY_HIDDEN)
1885169689Skan	  warning (OPT_Wattributes, "\
1886169689Skan%qT declared with greater visibility than the type of its field %qD",
1887169689Skan		   type, t);
1888169689Skan      }
1889169689Skan
1890169689Skan  binfo = TYPE_BINFO (type);
1891169689Skan  for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
1892169689Skan    {
1893169689Skan      int subvis = type_visibility (TREE_TYPE (t));
1894169689Skan
1895169689Skan      if (subvis == VISIBILITY_ANON)
1896220150Smm        {
1897220150Smm	  if (!in_main_input_context())
1898220150Smm	    warning (0, "\
1899169689Skan%qT has a base %qT whose type uses the anonymous namespace",
1900169689Skan		 type, TREE_TYPE (t));
1901220150Smm	}
1902169689Skan      else if (vis < VISIBILITY_HIDDEN
1903169689Skan	       && subvis >= VISIBILITY_HIDDEN)
1904169689Skan	warning (OPT_Wattributes, "\
1905169689Skan%qT declared with greater visibility than its base %qT",
1906169689Skan		 type, TREE_TYPE (t));
1907169689Skan    }
1908169689Skan}
1909169689Skan
1910169689Skan/* DECL is a FUNCTION_DECL or VAR_DECL.  If the object file linkage
1911169689Skan   for DECL has not already been determined, do so now by setting
1912169689Skan   DECL_EXTERNAL, DECL_COMDAT and other related flags.  Until this
1913169689Skan   function is called entities with vague linkage whose definitions
1914169689Skan   are available must have TREE_PUBLIC set.
1915169689Skan
1916169689Skan   If this function decides to place DECL in COMDAT, it will set
1917169689Skan   appropriate flags -- but will not clear DECL_EXTERNAL.  It is up to
1918169689Skan   the caller to decide whether or not to clear DECL_EXTERNAL.  Some
1919169689Skan   callers defer that decision until it is clear that DECL is actually
1920169689Skan   required.  */
1921169689Skan
1922169689Skanvoid
1923132718Skanimport_export_decl (tree decl)
192418334Speter{
1925169689Skan  int emit_p;
1926169689Skan  bool comdat_p;
1927169689Skan  bool import_p;
1928169689Skan  tree class_type = NULL_TREE;
1929169689Skan
193018334Speter  if (DECL_INTERFACE_KNOWN (decl))
193118334Speter    return;
193218334Speter
1933169689Skan  /* We cannot determine what linkage to give to an entity with vague
1934169689Skan     linkage until the end of the file.  For example, a virtual table
1935169689Skan     for a class will be defined if and only if the key method is
1936169689Skan     defined in this translation unit.  As a further example, consider
1937169689Skan     that when compiling a translation unit that uses PCH file with
1938169689Skan     "-frepo" it would be incorrect to make decisions about what
1939169689Skan     entities to emit when building the PCH; those decisions must be
1940169689Skan     delayed until the repository information has been processed.  */
1941169689Skan  gcc_assert (at_eof);
1942169689Skan  /* Object file linkage for explicit instantiations is handled in
1943169689Skan     mark_decl_instantiated.  For static variables in functions with
1944169689Skan     vague linkage, maybe_commonize_var is used.
1945169689Skan
1946169689Skan     Therefore, the only declarations that should be provided to this
1947169689Skan     function are those with external linkage that are:
1948169689Skan
1949169689Skan     * implicit instantiations of function templates
1950169689Skan
1951169689Skan     * inline function
1952169689Skan
1953169689Skan     * implicit instantiations of static data members of class
1954169689Skan       templates
1955169689Skan
1956169689Skan     * virtual tables
1957169689Skan
1958169689Skan     * typeinfo objects
1959169689Skan
1960169689Skan     Furthermore, all entities that reach this point must have a
1961169689Skan     definition available in this translation unit.
1962169689Skan
1963169689Skan     The following assertions check these conditions.  */
1964169689Skan  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1965169689Skan	      || TREE_CODE (decl) == VAR_DECL);
1966169689Skan  /* Any code that creates entities with TREE_PUBLIC cleared should
1967169689Skan     also set DECL_INTERFACE_KNOWN.  */
1968169689Skan  gcc_assert (TREE_PUBLIC (decl));
1969169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
1970169689Skan    gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
1971169689Skan		|| DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
1972169689Skan		|| DECL_DECLARED_INLINE_P (decl));
1973169689Skan  else
1974169689Skan    gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
1975169689Skan		|| DECL_VTABLE_OR_VTT_P (decl)
1976169689Skan		|| DECL_TINFO_P (decl));
1977169689Skan  /* Check that a definition of DECL is available in this translation
1978169689Skan     unit.  */
1979169689Skan  gcc_assert (!DECL_REALLY_EXTERN (decl));
1980169689Skan
1981169689Skan  /* Assume that DECL will not have COMDAT linkage.  */
1982169689Skan  comdat_p = false;
1983169689Skan  /* Assume that DECL will not be imported into this translation
1984169689Skan     unit.  */
1985169689Skan  import_p = false;
1986169689Skan
1987169689Skan  /* See if the repository tells us whether or not to emit DECL in
1988169689Skan     this translation unit.  */
1989169689Skan  emit_p = repo_emit_p (decl);
1990169689Skan  if (emit_p == 0)
1991169689Skan    import_p = true;
1992169689Skan  else if (emit_p == 1)
199318334Speter    {
1994169689Skan      /* The repository indicates that this entity should be defined
1995169689Skan	 here.  Make sure the back end honors that request.  */
1996169689Skan      if (TREE_CODE (decl) == VAR_DECL)
1997169689Skan	mark_needed (decl);
1998169689Skan      else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
1999169689Skan	       || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
200018334Speter	{
2001169689Skan	  tree clone;
2002169689Skan	  FOR_EACH_CLONE (clone, decl)
2003169689Skan	    mark_needed (clone);
2004169689Skan	}
2005169689Skan      else
2006169689Skan	mark_needed (decl);
2007169689Skan      /* Output the definition as an ordinary strong definition.  */
2008169689Skan      DECL_EXTERNAL (decl) = 0;
2009169689Skan      DECL_INTERFACE_KNOWN (decl) = 1;
2010169689Skan      return;
2011169689Skan    }
2012169689Skan
2013169689Skan  if (import_p)
2014169689Skan    /* We have already decided what to do with this DECL; there is no
2015169689Skan       need to check anything further.  */
2016169689Skan    ;
2017169689Skan  else if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
2018169689Skan    {
2019169689Skan      class_type = DECL_CONTEXT (decl);
2020169689Skan      import_export_class (class_type);
2021169689Skan      if (TYPE_FOR_JAVA (class_type))
2022169689Skan	import_p = true;
2023169689Skan      else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2024169689Skan	       && CLASSTYPE_INTERFACE_ONLY (class_type))
2025169689Skan	import_p = true;
2026169689Skan      else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2027169689Skan	       && !CLASSTYPE_USE_TEMPLATE (class_type)
2028169689Skan	       && CLASSTYPE_KEY_METHOD (class_type)
2029169689Skan	       && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2030169689Skan	/* The ABI requires that all virtual tables be emitted with
2031169689Skan	   COMDAT linkage.  However, on systems where COMDAT symbols
2032169689Skan	   don't show up in the table of contents for a static
2033169689Skan	   archive, or on systems without weak symbols (where we
2034169689Skan	   approximate COMDAT linkage by using internal linkage), the
2035169689Skan	   linker will report errors about undefined symbols because
2036169689Skan	   it will not see the virtual table definition.  Therefore,
2037169689Skan	   in the case that we know that the virtual table will be
2038169689Skan	   emitted in only one translation unit, we make the virtual
2039169689Skan	   table an ordinary definition with external linkage.  */
2040169689Skan	DECL_EXTERNAL (decl) = 0;
2041169689Skan      else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2042169689Skan	{
2043169689Skan	  /* CLASS_TYPE is being exported from this translation unit,
2044169689Skan	     so DECL should be defined here.  */
2045169689Skan	  if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2046169689Skan	    /* If a class is declared in a header with the "extern
2047169689Skan	       template" extension, then it will not be instantiated,
2048169689Skan	       even in translation units that would normally require
2049169689Skan	       it.  Often such classes are explicitly instantiated in
2050169689Skan	       one translation unit.  Therefore, the explicit
2051169689Skan	       instantiation must be made visible to other translation
2052169689Skan	       units.  */
2053169689Skan	    DECL_EXTERNAL (decl) = 0;
205452284Sobrien	  else
2055169689Skan	    {
2056169689Skan	      /* The generic C++ ABI says that class data is always
2057169689Skan		 COMDAT, even if there is a key function.  Some
2058169689Skan		 variants (e.g., the ARM EABI) says that class data
2059169689Skan		 only has COMDAT linkage if the class data might be
2060169689Skan		 emitted in more than one translation unit.  When the
2061169689Skan		 key method can be inline and is inline, we still have
2062169689Skan		 to arrange for comdat even though
2063169689Skan		 class_data_always_comdat is false.  */
2064169689Skan	      if (!CLASSTYPE_KEY_METHOD (class_type)
2065169689Skan		  || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2066169689Skan		  || targetm.cxx.class_data_always_comdat ())
2067169689Skan		{
2068169689Skan		  /* The ABI requires COMDAT linkage.  Normally, we
2069169689Skan		     only emit COMDAT things when they are needed;
2070169689Skan		     make sure that we realize that this entity is
2071169689Skan		     indeed needed.  */
2072169689Skan		  comdat_p = true;
2073169689Skan		  mark_needed (decl);
2074169689Skan		}
2075169689Skan	    }
207618334Speter	}
2077169689Skan      else if (!flag_implicit_templates
2078169689Skan	       && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2079169689Skan	import_p = true;
208018334Speter      else
2081169689Skan	comdat_p = true;
2082169689Skan    }
2083169689Skan  else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2084169689Skan    {
2085169689Skan      tree type = TREE_TYPE (DECL_NAME (decl));
2086169689Skan      if (CLASS_TYPE_P (type))
2087102780Skan	{
2088169689Skan	  class_type = type;
2089169689Skan	  import_export_class (type);
2090169689Skan	  if (CLASSTYPE_INTERFACE_KNOWN (type)
2091169689Skan	      && TYPE_POLYMORPHIC_P (type)
2092169689Skan	      && CLASSTYPE_INTERFACE_ONLY (type)
2093169689Skan	      /* If -fno-rtti was specified, then we cannot be sure
2094169689Skan		 that RTTI information will be emitted with the
2095169689Skan		 virtual table of the class, so we must emit it
2096169689Skan		 wherever it is used.  */
2097169689Skan	      && flag_rtti)
2098169689Skan	    import_p = true;
2099169689Skan	  else
2100169689Skan	    {
2101169689Skan	      if (CLASSTYPE_INTERFACE_KNOWN (type)
2102169689Skan		  && !CLASSTYPE_INTERFACE_ONLY (type))
2103169689Skan		{
2104169689Skan		  comdat_p = (targetm.cxx.class_data_always_comdat ()
2105169689Skan			      || (CLASSTYPE_KEY_METHOD (type)
2106169689Skan				  && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2107169689Skan		  mark_needed (decl);
2108169689Skan		  if (!flag_weak)
2109169689Skan		    {
2110169689Skan		      comdat_p = false;
2111169689Skan		      DECL_EXTERNAL (decl) = 0;
2112169689Skan		    }
2113169689Skan		}
2114169689Skan	      else
2115169689Skan		comdat_p = true;
2116169689Skan	    }
2117102780Skan	}
2118169689Skan      else
2119169689Skan	comdat_p = true;
212018334Speter    }
2121169689Skan  else if (DECL_TEMPLATE_INSTANTIATION (decl)
2122169689Skan	   || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2123169689Skan    {
2124169689Skan      /* DECL is an implicit instantiation of a function or static
2125169689Skan	 data member.  */
2126169689Skan      if (flag_implicit_templates
2127169689Skan	  || (flag_implicit_inline_templates
2128169689Skan	      && TREE_CODE (decl) == FUNCTION_DECL
2129169689Skan	      && DECL_DECLARED_INLINE_P (decl)))
2130169689Skan	comdat_p = true;
2131169689Skan      else
2132169689Skan	/* If we are not implicitly generating templates, then mark
2133169689Skan	   this entity as undefined in this translation unit.  */
2134169689Skan	import_p = true;
2135169689Skan    }
213618334Speter  else if (DECL_FUNCTION_MEMBER_P (decl))
213718334Speter    {
213890075Sobrien      if (!DECL_DECLARED_INLINE_P (decl))
213918334Speter	{
214090075Sobrien	  tree ctype = DECL_CONTEXT (decl);
214190075Sobrien	  import_export_class (ctype);
214290075Sobrien	  if (CLASSTYPE_INTERFACE_KNOWN (ctype))
214390075Sobrien	    {
214490075Sobrien	      DECL_NOT_REALLY_EXTERN (decl)
214590075Sobrien		= ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2146169689Skan		     || (DECL_DECLARED_INLINE_P (decl)
214790075Sobrien			 && ! flag_implement_inlines
214890075Sobrien			 && !DECL_VINDEX (decl)));
214952284Sobrien
2150102780Skan	      if (!DECL_NOT_REALLY_EXTERN (decl))
2151102780Skan		DECL_EXTERNAL (decl) = 1;
2152102780Skan
215390075Sobrien	      /* Always make artificials weak.  */
215490075Sobrien	      if (DECL_ARTIFICIAL (decl) && flag_weak)
2155169689Skan		comdat_p = true;
215690075Sobrien	      else
215790075Sobrien		maybe_make_one_only (decl);
215890075Sobrien	    }
215918334Speter	}
216018334Speter      else
2161169689Skan	comdat_p = true;
216218334Speter    }
2163102780Skan  else
2164169689Skan    comdat_p = true;
216550397Sobrien
2166169689Skan  if (import_p)
2167102780Skan    {
2168169689Skan      /* If we are importing DECL into this translation unit, mark is
2169169689Skan	 an undefined here.  */
2170169689Skan      DECL_EXTERNAL (decl) = 1;
2171169689Skan      DECL_NOT_REALLY_EXTERN (decl) = 0;
2172102780Skan    }
2173169689Skan  else if (comdat_p)
2174102780Skan    {
2175169689Skan      /* If we decided to put DECL in COMDAT, mark it accordingly at
2176169689Skan	 this point.  */
2177169689Skan      comdat_linkage (decl);
2178102780Skan    }
217918334Speter
218018334Speter  DECL_INTERFACE_KNOWN (decl) = 1;
218118334Speter}
218218334Speter
2183117395Skan/* Return an expression that performs the destruction of DECL, which
2184117395Skan   must be a VAR_DECL whose type has a non-trivial destructor, or is
2185117395Skan   an array whose (innermost) elements have a non-trivial destructor.  */
2186117395Skan
218750397Sobrientree
2188132718Skanbuild_cleanup (tree decl)
218950397Sobrien{
219050397Sobrien  tree temp;
219150397Sobrien  tree type = TREE_TYPE (decl);
219250397Sobrien
2193117395Skan  /* This function should only be called for declarations that really
2194117395Skan     require cleanups.  */
2195169689Skan  gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type));
2196117395Skan
2197117395Skan  /* Treat all objects with destructors as used; the destructor may do
2198117395Skan     something substantive.  */
2199117395Skan  mark_used (decl);
2200117395Skan
220150397Sobrien  if (TREE_CODE (type) == ARRAY_TYPE)
220250397Sobrien    temp = decl;
220350397Sobrien  else
220450397Sobrien    {
2205117395Skan      cxx_mark_addressable (decl);
220650397Sobrien      temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
220750397Sobrien    }
220850397Sobrien  temp = build_delete (TREE_TYPE (temp), temp,
220990075Sobrien		       sfk_complete_destructor,
221050397Sobrien		       LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
221150397Sobrien  return temp;
221250397Sobrien}
221350397Sobrien
221490075Sobrien/* Returns the initialization guard variable for the variable DECL,
221590075Sobrien   which has static storage duration.  */
221618334Speter
221790075Sobrientree
2218132718Skanget_guard (tree decl)
221950397Sobrien{
222090075Sobrien  tree sname;
222190075Sobrien  tree guard;
222290075Sobrien
222390075Sobrien  sname = mangle_guard_variable (decl);
222490075Sobrien  guard = IDENTIFIER_GLOBAL_VALUE (sname);
222590075Sobrien  if (! guard)
222650397Sobrien    {
222790075Sobrien      tree guard_type;
222890075Sobrien
222990075Sobrien      /* We use a type that is big enough to contain a mutex as well
223090075Sobrien	 as an integer counter.  */
2231169689Skan      guard_type = targetm.cxx.guard_type ();
223290075Sobrien      guard = build_decl (VAR_DECL, sname, guard_type);
2233169689Skan
2234117395Skan      /* The guard should have the same linkage as what it guards.  */
223590075Sobrien      TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
223690075Sobrien      TREE_STATIC (guard) = TREE_STATIC (decl);
223790075Sobrien      DECL_COMMON (guard) = DECL_COMMON (decl);
223890075Sobrien      DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
223990075Sobrien      if (TREE_PUBLIC (decl))
2240169689Skan	DECL_WEAK (guard) = DECL_WEAK (decl);
2241169689Skan
224290075Sobrien      DECL_ARTIFICIAL (guard) = 1;
2243169689Skan      DECL_IGNORED_P (guard) = 1;
224490075Sobrien      TREE_USED (guard) = 1;
2245117395Skan      pushdecl_top_level_and_finish (guard, NULL_TREE);
224650397Sobrien    }
224790075Sobrien  return guard;
224850397Sobrien}
224918334Speter
225090075Sobrien/* Return those bits of the GUARD variable that should be set when the
225190075Sobrien   guarded entity is actually initialized.  */
225290075Sobrien
225390075Sobrienstatic tree
2254132718Skanget_guard_bits (tree guard)
225590075Sobrien{
2256169689Skan  if (!targetm.cxx.guard_mask_bit ())
2257169689Skan    {
2258169689Skan      /* We only set the first byte of the guard, in order to leave room
2259169689Skan	 for a mutex in the high-order bits.  */
2260169689Skan      guard = build1 (ADDR_EXPR,
2261169689Skan		      build_pointer_type (TREE_TYPE (guard)),
2262169689Skan		      guard);
2263169689Skan      guard = build1 (NOP_EXPR,
2264169689Skan		      build_pointer_type (char_type_node),
2265169689Skan		      guard);
2266169689Skan      guard = build1 (INDIRECT_REF, char_type_node, guard);
2267169689Skan    }
226890075Sobrien
226990075Sobrien  return guard;
227090075Sobrien}
227190075Sobrien
227290075Sobrien/* Return an expression which determines whether or not the GUARD
227390075Sobrien   variable has already been initialized.  */
227490075Sobrien
227590075Sobrientree
2276132718Skanget_guard_cond (tree guard)
227790075Sobrien{
227890075Sobrien  tree guard_value;
227990075Sobrien
228090075Sobrien  /* Check to see if the GUARD is zero.  */
228190075Sobrien  guard = get_guard_bits (guard);
2282169689Skan
2283169689Skan  /* Mask off all but the low bit.  */
2284169689Skan  if (targetm.cxx.guard_mask_bit ())
2285169689Skan    {
2286169689Skan      guard_value = integer_one_node;
2287169689Skan      if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2288169689Skan	guard_value = convert (TREE_TYPE (guard), guard_value);
2289169689Skan	guard = cp_build_binary_op (BIT_AND_EXPR, guard, guard_value);
2290169689Skan    }
2291169689Skan
229290075Sobrien  guard_value = integer_zero_node;
229390075Sobrien  if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
229490075Sobrien    guard_value = convert (TREE_TYPE (guard), guard_value);
229590075Sobrien  return cp_build_binary_op (EQ_EXPR, guard, guard_value);
229690075Sobrien}
229790075Sobrien
229890075Sobrien/* Return an expression which sets the GUARD variable, indicating that
229990075Sobrien   the variable being guarded has been initialized.  */
230090075Sobrien
230190075Sobrientree
2302132718Skanset_guard (tree guard)
230390075Sobrien{
230490075Sobrien  tree guard_init;
230590075Sobrien
230690075Sobrien  /* Set the GUARD to one.  */
230790075Sobrien  guard = get_guard_bits (guard);
230890075Sobrien  guard_init = integer_one_node;
230990075Sobrien  if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
231090075Sobrien    guard_init = convert (TREE_TYPE (guard), guard_init);
231190075Sobrien  return build_modify_expr (guard, NOP_EXPR, guard_init);
231290075Sobrien}
231390075Sobrien
231450397Sobrien/* Start the process of running a particular set of global constructors
231550397Sobrien   or destructors.  Subroutine of do_[cd]tors.  */
231650397Sobrien
231790075Sobrienstatic tree
2318132718Skanstart_objects (int method_type, int initp)
231950397Sobrien{
232090075Sobrien  tree body;
2321169689Skan  tree fndecl;
232252284Sobrien  char type[10];
232350397Sobrien
232450397Sobrien  /* Make ctor or dtor function.  METHOD_TYPE may be 'I' or 'D'.  */
232550397Sobrien
232652284Sobrien  if (initp != DEFAULT_INIT_PRIORITY)
232752284Sobrien    {
232852284Sobrien      char joiner;
232950397Sobrien
233052284Sobrien#ifdef JOINER
233152284Sobrien      joiner = JOINER;
233252284Sobrien#else
233352284Sobrien      joiner = '_';
233452284Sobrien#endif
233552284Sobrien
233652284Sobrien      sprintf (type, "%c%c%.5u", method_type, joiner, initp);
233752284Sobrien    }
233852284Sobrien  else
233952284Sobrien    sprintf (type, "%c", method_type);
234052284Sobrien
2341169689Skan  fndecl = build_lang_decl (FUNCTION_DECL,
2342169689Skan			    get_file_function_name_long (type),
2343169689Skan			    build_function_type (void_type_node,
2344169689Skan						 void_list_node));
2345169689Skan  start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
234652284Sobrien
234750397Sobrien  /* It can be a static function as long as collect2 does not have
234850397Sobrien     to scan the object file to find its ctor/dtor routine.  */
234990075Sobrien  TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
235050397Sobrien
235190075Sobrien  /* Mark this declaration as used to avoid spurious warnings.  */
235290075Sobrien  TREE_USED (current_function_decl) = 1;
235352284Sobrien
235490075Sobrien  /* Mark this function as a global constructor or destructor.  */
235590075Sobrien  if (method_type == 'I')
235690075Sobrien    DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
235790075Sobrien  else
235890075Sobrien    DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2359117395Skan  DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
236090075Sobrien
2361169689Skan  body = begin_compound_stmt (BCS_FN_BODY);
236290075Sobrien
236352284Sobrien  /* We cannot allow these functions to be elided, even if they do not
236452284Sobrien     have external linkage.  And, there's no point in deferring
2365169689Skan     compilation of these functions; they're all going to have to be
236652284Sobrien     out anyhow.  */
2367169689Skan  DECL_INLINE (current_function_decl) = 0;
2368169689Skan  DECL_UNINLINABLE (current_function_decl) = 1;
236990075Sobrien
237090075Sobrien  return body;
237150397Sobrien}
237250397Sobrien
237350397Sobrien/* Finish the process of running a particular set of global constructors
237450397Sobrien   or destructors.  Subroutine of do_[cd]tors.  */
237550397Sobrien
237650397Sobrienstatic void
2377132718Skanfinish_objects (int method_type, int initp, tree body)
237850397Sobrien{
237990075Sobrien  tree fn;
238050397Sobrien
238190075Sobrien  /* Finish up.  */
2382132718Skan  finish_compound_stmt (body);
238390075Sobrien  fn = finish_function (0);
2384132718Skan  expand_or_defer_fn (fn);
238550397Sobrien
238690075Sobrien  /* When only doing semantic analysis, and no RTL generation, we
238790075Sobrien     can't call functions that directly emit assembly code; there is
238890075Sobrien     no assembly file in which to put the code.  */
238990075Sobrien  if (flag_syntax_only)
239090075Sobrien    return;
239190075Sobrien
239290075Sobrien  if (targetm.have_ctors_dtors)
239352284Sobrien    {
239490075Sobrien      rtx fnsym = XEXP (DECL_RTL (fn), 0);
2395169689Skan      cgraph_mark_needed_node (cgraph_node (fn));
239652284Sobrien      if (method_type == 'I')
239790075Sobrien	(* targetm.asm_out.constructor) (fnsym, initp);
239852284Sobrien      else
239990075Sobrien	(* targetm.asm_out.destructor) (fnsym, initp);
240052284Sobrien    }
240150397Sobrien}
240250397Sobrien
240352284Sobrien/* The names of the parameters to the function created to handle
240452284Sobrien   initializations and destructions for objects with static storage
240552284Sobrien   duration.  */
240652284Sobrien#define INITIALIZE_P_IDENTIFIER "__initialize_p"
240752284Sobrien#define PRIORITY_IDENTIFIER "__priority"
240850397Sobrien
240952284Sobrien/* The name of the function we create to handle initializations and
241052284Sobrien   destructions for objects with static storage duration.  */
241152284Sobrien#define SSDF_IDENTIFIER "__static_initialization_and_destruction"
241252284Sobrien
241352284Sobrien/* The declaration for the __INITIALIZE_P argument.  */
2414117395Skanstatic GTY(()) tree initialize_p_decl;
241552284Sobrien
241652284Sobrien/* The declaration for the __PRIORITY argument.  */
2417117395Skanstatic GTY(()) tree priority_decl;
241852284Sobrien
241952284Sobrien/* The declaration for the static storage duration function.  */
2420117395Skanstatic GTY(()) tree ssdf_decl;
242152284Sobrien
242252284Sobrien/* All the static storage duration functions created in this
242352284Sobrien   translation unit.  */
2424169689Skanstatic GTY(()) VEC(tree,gc) *ssdf_decls;
242552284Sobrien
242652284Sobrien/* A map from priority levels to information about that priority
242752284Sobrien   level.  There may be many such levels, so efficient lookup is
242852284Sobrien   important.  */
242952284Sobrienstatic splay_tree priority_info_map;
243052284Sobrien
243152284Sobrien/* Begins the generation of the function that will handle all
243252284Sobrien   initialization and destruction of objects with static storage
243352284Sobrien   duration.  The function generated takes two parameters of type
243452284Sobrien   `int': __INITIALIZE_P and __PRIORITY.  If __INITIALIZE_P is
2435117395Skan   nonzero, it performs initializations.  Otherwise, it performs
243652284Sobrien   destructions.  It only performs those initializations or
243752284Sobrien   destructions with the indicated __PRIORITY.  The generated function
2438169689Skan   returns no value.
243952284Sobrien
244052284Sobrien   It is assumed that this function will only be called once per
244152284Sobrien   translation unit.  */
244252284Sobrien
244390075Sobrienstatic tree
2444132718Skanstart_static_storage_duration_function (unsigned count)
244550397Sobrien{
244652284Sobrien  tree parm_types;
244752284Sobrien  tree type;
244890075Sobrien  tree body;
244952284Sobrien  char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
245052284Sobrien
245152284Sobrien  /* Create the identifier for this function.  It will be of the form
245252284Sobrien     SSDF_IDENTIFIER_<number>.  */
2453132718Skan  sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
245450397Sobrien
245552284Sobrien  /* Create the parameters.  */
245652284Sobrien  parm_types = void_list_node;
245790075Sobrien  parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
245890075Sobrien  parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
245952284Sobrien  type = build_function_type (void_type_node, parm_types);
246050397Sobrien
246152284Sobrien  /* Create the FUNCTION_DECL itself.  */
2462169689Skan  ssdf_decl = build_lang_decl (FUNCTION_DECL,
246352284Sobrien			       get_identifier (id),
246452284Sobrien			       type);
246552284Sobrien  TREE_PUBLIC (ssdf_decl) = 0;
246652284Sobrien  DECL_ARTIFICIAL (ssdf_decl) = 1;
246750397Sobrien
246852284Sobrien  /* Put this function in the list of functions to be called from the
246952284Sobrien     static constructors and destructors.  */
247052284Sobrien  if (!ssdf_decls)
247152284Sobrien    {
2472169689Skan      ssdf_decls = VEC_alloc (tree, gc, 32);
247350397Sobrien
247452284Sobrien      /* Take this opportunity to initialize the map from priority
2475117395Skan	 numbers to information about that priority level.  */
247652284Sobrien      priority_info_map = splay_tree_new (splay_tree_compare_ints,
247752284Sobrien					  /*delete_key_fn=*/0,
247852284Sobrien					  /*delete_value_fn=*/
247952284Sobrien					  (splay_tree_delete_value_fn) &free);
248050397Sobrien
248152284Sobrien      /* We always need to generate functions for the
248252284Sobrien	 DEFAULT_INIT_PRIORITY so enter it now.  That way when we walk
248352284Sobrien	 priorities later, we'll be sure to find the
248452284Sobrien	 DEFAULT_INIT_PRIORITY.  */
248552284Sobrien      get_priority_info (DEFAULT_INIT_PRIORITY);
248650397Sobrien    }
248750397Sobrien
2488169689Skan  VEC_safe_push (tree, gc, ssdf_decls, ssdf_decl);
248952284Sobrien
249052284Sobrien  /* Create the argument list.  */
2491117395Skan  initialize_p_decl = cp_build_parm_decl
2492117395Skan    (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
249352284Sobrien  DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
249452284Sobrien  TREE_USED (initialize_p_decl) = 1;
2495117395Skan  priority_decl = cp_build_parm_decl
2496117395Skan    (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
249752284Sobrien  DECL_CONTEXT (priority_decl) = ssdf_decl;
249852284Sobrien  TREE_USED (priority_decl) = 1;
249952284Sobrien
250052284Sobrien  TREE_CHAIN (initialize_p_decl) = priority_decl;
250152284Sobrien  DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
250252284Sobrien
250390075Sobrien  /* Put the function in the global scope.  */
250490075Sobrien  pushdecl (ssdf_decl);
250590075Sobrien
2506132718Skan  /* Start the function itself.  This is equivalent to declaring the
250752284Sobrien     function as:
250852284Sobrien
250952284Sobrien       static void __ssdf (int __initialize_p, init __priority_p);
2510169689Skan
251152284Sobrien     It is static because we only need to call this function from the
251252284Sobrien     various constructor and destructor functions for this module.  */
2513169689Skan  start_preparsed_function (ssdf_decl,
2514169689Skan			    /*attrs=*/NULL_TREE,
2515169689Skan			    SF_PRE_PARSED);
251652284Sobrien
251752284Sobrien  /* Set up the scope of the outermost block in the function.  */
2518169689Skan  body = begin_compound_stmt (BCS_FN_BODY);
251952284Sobrien
252052284Sobrien  /* This function must not be deferred because we are depending on
252152284Sobrien     its compilation to tell us what is TREE_SYMBOL_REFERENCED.  */
2522169689Skan  DECL_INLINE (ssdf_decl) = 0;
2523169689Skan  DECL_UNINLINABLE (ssdf_decl) = 1;
252450397Sobrien
252590075Sobrien  return body;
252652284Sobrien}
252750397Sobrien
252852284Sobrien/* Finish the generation of the function which performs initialization
252952284Sobrien   and destruction of objects with static storage duration.  After
253052284Sobrien   this point, no more such objects can be created.  */
253150397Sobrien
253252284Sobrienstatic void
2533132718Skanfinish_static_storage_duration_function (tree body)
253452284Sobrien{
253552284Sobrien  /* Close out the function.  */
2536132718Skan  finish_compound_stmt (body);
2537132718Skan  expand_or_defer_fn (finish_function (0));
253852284Sobrien}
253950397Sobrien
254052284Sobrien/* Return the information about the indicated PRIORITY level.  If no
254152284Sobrien   code to handle this level has yet been generated, generate the
254252284Sobrien   appropriate prologue.  */
254350397Sobrien
254452284Sobrienstatic priority_info
2545132718Skanget_priority_info (int priority)
254652284Sobrien{
254752284Sobrien  priority_info pi;
254852284Sobrien  splay_tree_node n;
254952284Sobrien
2550169689Skan  n = splay_tree_lookup (priority_info_map,
255152284Sobrien			 (splay_tree_key) priority);
255252284Sobrien  if (!n)
255352284Sobrien    {
255452284Sobrien      /* Create a new priority information structure, and insert it
255552284Sobrien	 into the map.  */
2556169689Skan      pi = XNEW (struct priority_info_s);
255752284Sobrien      pi->initializations_p = 0;
255852284Sobrien      pi->destructions_p = 0;
255952284Sobrien      splay_tree_insert (priority_info_map,
256052284Sobrien			 (splay_tree_key) priority,
256152284Sobrien			 (splay_tree_value) pi);
256250397Sobrien    }
256352284Sobrien  else
256452284Sobrien    pi = (priority_info) n->value;
256550397Sobrien
256652284Sobrien  return pi;
256750397Sobrien}
256850397Sobrien
2569169689Skan/* The effective initialization priority of a DECL.  */
2570169689Skan
2571169689Skan#define DECL_EFFECTIVE_INIT_PRIORITY(decl)				      \
2572169689Skan	((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
2573169689Skan	 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
2574169689Skan
2575169689Skan/* Whether a DECL needs a guard to protect it against multiple
2576169689Skan   initialization.  */
2577169689Skan
2578169689Skan#define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl)      \
2579169689Skan						    || DECL_ONE_ONLY (decl) \
2580169689Skan						    || DECL_WEAK (decl)))
2581169689Skan
258290075Sobrien/* Set up to handle the initialization or destruction of DECL.  If
2583117395Skan   INITP is nonzero, we are initializing the variable.  Otherwise, we
258490075Sobrien   are destroying it.  */
258518334Speter
2586169689Skanstatic void
2587169689Skanone_static_initialization_or_destruction (tree decl, tree init, bool initp)
258818334Speter{
258990075Sobrien  tree guard_if_stmt = NULL_TREE;
259090075Sobrien  tree guard;
259118334Speter
2592169689Skan  /* If we are supposed to destruct and there's a trivial destructor,
2593169689Skan     nothing has to be done.  */
2594169689Skan  if (!initp
2595169689Skan      && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2596169689Skan    return;
259790075Sobrien
259890075Sobrien  /* Trick the compiler into thinking we are at the file and line
259990075Sobrien     where DECL was declared so that error-messages make sense, and so
260090075Sobrien     that the debugger will show somewhat sensible file and line
260190075Sobrien     information.  */
2602132718Skan  input_location = DECL_SOURCE_LOCATION (decl);
260318334Speter
260490075Sobrien  /* Because of:
260552284Sobrien
260690075Sobrien       [class.access.spec]
260790075Sobrien
260890075Sobrien       Access control for implicit calls to the constructors,
260990075Sobrien       the conversion functions, or the destructor called to
261090075Sobrien       create and destroy a static data member is performed as
261190075Sobrien       if these calls appeared in the scope of the member's
2612169689Skan       class.
261390075Sobrien
261490075Sobrien     we pretend we are in a static member function of the class of
261590075Sobrien     which the DECL is a member.  */
261690075Sobrien  if (member_p (decl))
261790075Sobrien    {
261890075Sobrien      DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
261990075Sobrien      DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
262090075Sobrien    }
262152284Sobrien
262290075Sobrien  /* Assume we don't need a guard.  */
262390075Sobrien  guard = NULL_TREE;
262490075Sobrien  /* We need a guard if this is an object with external linkage that
262590075Sobrien     might be initialized in more than one place.  (For example, a
262690075Sobrien     static data member of a template, when the data member requires
262790075Sobrien     construction.)  */
2628169689Skan  if (NEEDS_GUARD_P (decl))
262990075Sobrien    {
263090075Sobrien      tree guard_cond;
263152284Sobrien
263290075Sobrien      guard = get_guard (decl);
263352284Sobrien
263490075Sobrien      /* When using __cxa_atexit, we just check the GUARD as we would
263590075Sobrien	 for a local static.  */
263690075Sobrien      if (flag_use_cxa_atexit)
263790075Sobrien	{
263890075Sobrien	  /* When using __cxa_atexit, we never try to destroy
263990075Sobrien	     anything from a static destructor.  */
2640169689Skan	  gcc_assert (initp);
264190075Sobrien	  guard_cond = get_guard_cond (guard);
264290075Sobrien	}
264390075Sobrien      /* If we don't have __cxa_atexit, then we will be running
264490075Sobrien	 destructors from .fini sections, or their equivalents.  So,
264590075Sobrien	 we need to know how many times we've tried to initialize this
264690075Sobrien	 object.  We do initializations only if the GUARD is zero,
264790075Sobrien	 i.e., if we are the first to initialize the variable.  We do
264890075Sobrien	 destructions only if the GUARD is one, i.e., if we are the
264990075Sobrien	 last to destroy the variable.  */
265090075Sobrien      else if (initp)
2651169689Skan	guard_cond
265290075Sobrien	  = cp_build_binary_op (EQ_EXPR,
265390075Sobrien				build_unary_op (PREINCREMENT_EXPR,
265490075Sobrien						guard,
265590075Sobrien						/*noconvert=*/1),
265690075Sobrien				integer_one_node);
265790075Sobrien      else
2658169689Skan	guard_cond
265990075Sobrien	  = cp_build_binary_op (EQ_EXPR,
266090075Sobrien				build_unary_op (PREDECREMENT_EXPR,
266190075Sobrien						guard,
266290075Sobrien						/*noconvert=*/1),
266390075Sobrien				integer_zero_node);
266452284Sobrien
2665169689Skan      guard_if_stmt = begin_if_stmt ();
2666169689Skan      finish_if_stmt_cond (guard_cond, guard_if_stmt);
266790075Sobrien    }
266890075Sobrien
266990075Sobrien
267090075Sobrien  /* If we're using __cxa_atexit, we have not already set the GUARD,
267190075Sobrien     so we must do so now.  */
267290075Sobrien  if (guard && initp && flag_use_cxa_atexit)
267390075Sobrien    finish_expr_stmt (set_guard (guard));
267490075Sobrien
2675169689Skan  /* Perform the initialization or destruction.  */
2676169689Skan  if (initp)
2677169689Skan    {
2678169689Skan      if (init)
2679169689Skan	finish_expr_stmt (init);
268052284Sobrien
2681169689Skan      /* If we're using __cxa_atexit, register a function that calls the
2682169689Skan	 destructor for the object.  */
2683169689Skan      if (flag_use_cxa_atexit)
2684169689Skan	finish_expr_stmt (register_dtor_fn (decl));
2685169689Skan    }
2686169689Skan  else
2687169689Skan    finish_expr_stmt (build_cleanup (decl));
268852284Sobrien
2689169689Skan  /* Finish the guard if-stmt, if necessary.  */
2690169689Skan  if (guard)
2691169689Skan    {
2692169689Skan      finish_then_clause (guard_if_stmt);
2693169689Skan      finish_if_stmt (guard_if_stmt);
2694169689Skan    }
269552284Sobrien
269690075Sobrien  /* Now that we're done with DECL we don't need to pretend to be a
269790075Sobrien     member of its class any longer.  */
269890075Sobrien  DECL_CONTEXT (current_function_decl) = NULL_TREE;
269990075Sobrien  DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
270090075Sobrien}
270118334Speter
2702169689Skan/* Generate code to do the initialization or destruction of the decls in VARS,
2703169689Skan   a TREE_LIST of VAR_DECL with static storage duration.
2704169689Skan   Whether initialization or destruction is performed is specified by INITP.  */
270550397Sobrien
270690075Sobrienstatic void
2707169689Skando_static_initialization_or_destruction (tree vars, bool initp)
270890075Sobrien{
2709169689Skan  tree node, init_if_stmt, cond;
271090075Sobrien
2711169689Skan  /* Build the outer if-stmt to check for initialization or destruction.  */
2712169689Skan  init_if_stmt = begin_if_stmt ();
2713169689Skan  cond = initp ? integer_one_node : integer_zero_node;
2714169689Skan  cond = cp_build_binary_op (EQ_EXPR,
2715169689Skan				  initialize_p_decl,
2716169689Skan				  cond);
2717169689Skan  finish_if_stmt_cond (cond, init_if_stmt);
271818334Speter
2719169689Skan  node = vars;
2720169689Skan  do {
2721169689Skan    tree decl = TREE_VALUE (node);
2722169689Skan    tree priority_if_stmt;
2723169689Skan    int priority;
2724169689Skan    priority_info pi;
2725117395Skan
2726169689Skan    /* If we don't need a destructor, there's nothing to do.  Avoid
2727169689Skan       creating a possibly empty if-stmt.  */
2728169689Skan    if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2729169689Skan      {
2730169689Skan	node = TREE_CHAIN (node);
2731169689Skan	continue;
2732169689Skan      }
273318334Speter
2734169689Skan    /* Remember that we had an initialization or finalization at this
2735169689Skan       priority.  */
2736169689Skan    priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
2737169689Skan    pi = get_priority_info (priority);
2738169689Skan    if (initp)
2739169689Skan      pi->initializations_p = 1;
2740169689Skan    else
2741169689Skan      pi->destructions_p = 1;
274250397Sobrien
2743169689Skan    /* Conditionalize this initialization on being in the right priority
2744169689Skan       and being initializing/finalizing appropriately.  */
2745169689Skan    priority_if_stmt = begin_if_stmt ();
2746169689Skan    cond = cp_build_binary_op (EQ_EXPR,
2747169689Skan			       priority_decl,
2748169689Skan			       build_int_cst (NULL_TREE, priority));
2749169689Skan    finish_if_stmt_cond (cond, priority_if_stmt);
275050397Sobrien
2751169689Skan    /* Process initializers with same priority.  */
2752169689Skan    for (; node
2753169689Skan	   && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
2754169689Skan	 node = TREE_CHAIN (node))
2755169689Skan      /* Do one initialization or destruction.  */
2756169689Skan      one_static_initialization_or_destruction (TREE_VALUE (node),
2757169689Skan						TREE_PURPOSE (node), initp);
275850397Sobrien
2759169689Skan    /* Finish up the priority if-stmt body.  */
2760169689Skan    finish_then_clause (priority_if_stmt);
2761169689Skan    finish_if_stmt (priority_if_stmt);
276290075Sobrien
2763169689Skan  } while (node);
276450397Sobrien
2765169689Skan  /* Finish up the init/destruct if-stmt body.  */
2766169689Skan  finish_then_clause (init_if_stmt);
2767169689Skan  finish_if_stmt (init_if_stmt);
276890075Sobrien}
276952284Sobrien
277090075Sobrien/* VARS is a list of variables with static storage duration which may
277190075Sobrien   need initialization and/or finalization.  Remove those variables
277290075Sobrien   that don't really need to be initialized or finalized, and return
277390075Sobrien   the resulting list.  The order in which the variables appear in
277490075Sobrien   VARS is in reverse order of the order in which they should actually
277590075Sobrien   be initialized.  The list we return is in the unreversed order;
277690075Sobrien   i.e., the first variable should be initialized first.  */
277752284Sobrien
277890075Sobrienstatic tree
2779132718Skanprune_vars_needing_no_initialization (tree *vars)
278090075Sobrien{
2781117395Skan  tree *var = vars;
2782117395Skan  tree result = NULL_TREE;
278352284Sobrien
2784117395Skan  while (*var)
278590075Sobrien    {
2786117395Skan      tree t = *var;
2787117395Skan      tree decl = TREE_VALUE (t);
2788117395Skan      tree init = TREE_PURPOSE (t);
278952284Sobrien
279090075Sobrien      /* Deal gracefully with error.  */
279190075Sobrien      if (decl == error_mark_node)
2792117395Skan	{
2793117395Skan	  var = &TREE_CHAIN (t);
2794117395Skan	  continue;
2795117395Skan	}
279652284Sobrien
279790075Sobrien      /* The only things that can be initialized are variables.  */
2798169689Skan      gcc_assert (TREE_CODE (decl) == VAR_DECL);
279952284Sobrien
280090075Sobrien      /* If this object is not defined, we don't need to do anything
280190075Sobrien	 here.  */
280290075Sobrien      if (DECL_EXTERNAL (decl))
2803117395Skan	{
2804117395Skan	  var = &TREE_CHAIN (t);
2805117395Skan	  continue;
2806117395Skan	}
280752284Sobrien
280890075Sobrien      /* Also, if the initializer already contains errors, we can bail
280990075Sobrien	 out now.  */
2810169689Skan      if (init && TREE_CODE (init) == TREE_LIST
281190075Sobrien	  && value_member (error_mark_node, init))
2812117395Skan	{
2813117395Skan	  var = &TREE_CHAIN (t);
2814117395Skan	  continue;
2815117395Skan	}
281690075Sobrien
281790075Sobrien      /* This variable is going to need initialization and/or
281890075Sobrien	 finalization, so we add it to the list.  */
2819117395Skan      *var = TREE_CHAIN (t);
2820117395Skan      TREE_CHAIN (t) = result;
2821117395Skan      result = t;
282250397Sobrien    }
282350397Sobrien
282490075Sobrien  return result;
282590075Sobrien}
282650397Sobrien
282790075Sobrien/* Make sure we have told the back end about all the variables in
282890075Sobrien   VARS.  */
282990075Sobrien
283090075Sobrienstatic void
2831132718Skanwrite_out_vars (tree vars)
283290075Sobrien{
283390075Sobrien  tree v;
283490075Sobrien
283590075Sobrien  for (v = vars; v; v = TREE_CHAIN (v))
2836169689Skan    {
2837169689Skan      tree var = TREE_VALUE (v);
2838169689Skan      if (!var_finalized_p (var))
2839169689Skan	{
2840169689Skan	  import_export_decl (var);
2841169689Skan	  rest_of_decl_compilation (var, 1, 1);
2842169689Skan	}
2843169689Skan    }
284452284Sobrien}
284550397Sobrien
284652284Sobrien/* Generate a static constructor (if CONSTRUCTOR_P) or destructor
284752284Sobrien   (otherwise) that will initialize all gobal objects with static
284852284Sobrien   storage duration having the indicated PRIORITY.  */
284918334Speter
285052284Sobrienstatic void
2851132718Skangenerate_ctor_or_dtor_function (bool constructor_p, int priority,
2852132718Skan				location_t *locus)
285352284Sobrien{
285452284Sobrien  char function_key;
285552284Sobrien  tree arguments;
2856132718Skan  tree fndecl;
285790075Sobrien  tree body;
285852284Sobrien  size_t i;
285918334Speter
2860132718Skan  input_location = *locus;
2861169689Skan#ifdef USE_MAPPED_LOCATION
2862169689Skan  /* ??? */
2863169689Skan#else
2864132718Skan  locus->line++;
2865169689Skan#endif
2866169689Skan
286752284Sobrien  /* We use `I' to indicate initialization and `D' to indicate
286852284Sobrien     destruction.  */
2869132718Skan  function_key = constructor_p ? 'I' : 'D';
287018334Speter
2871132718Skan  /* We emit the function lazily, to avoid generating empty
2872132718Skan     global constructors and destructors.  */
2873132718Skan  body = NULL_TREE;
287418334Speter
2875169689Skan  /* For Objective-C++, we may need to initialize metadata found in this module.
2876169689Skan     This must be done _before_ any other static initializations.  */
2877169689Skan  if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
2878169689Skan      && constructor_p && objc_static_init_needed_p ())
2879169689Skan    {
2880169689Skan      body = start_objects (function_key, priority);
2881169689Skan      static_ctors = objc_generate_static_init_call (static_ctors);
2882169689Skan    }
2883169689Skan
288452284Sobrien  /* Call the static storage duration function with appropriate
288552284Sobrien     arguments.  */
2886169689Skan  for (i = 0; VEC_iterate (tree, ssdf_decls, i, fndecl); ++i)
2887169689Skan    {
2888169689Skan      /* Calls to pure or const functions will expand to nothing.  */
2889169689Skan      if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2890169689Skan	{
2891169689Skan	  if (! body)
2892169689Skan	    body = start_objects (function_key, priority);
2893132718Skan
2894169689Skan	  arguments = tree_cons (NULL_TREE,
2895169689Skan				 build_int_cst (NULL_TREE, priority),
2896169689Skan				 NULL_TREE);
2897169689Skan	  arguments = tree_cons (NULL_TREE,
2898169689Skan				 build_int_cst (NULL_TREE, constructor_p),
2899169689Skan				 arguments);
2900169689Skan	  finish_expr_stmt (build_function_call (fndecl, arguments));
2901169689Skan	}
2902169689Skan    }
2903132718Skan
290452284Sobrien  /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
290552284Sobrien     calls to any functions marked with attributes indicating that
290652284Sobrien     they should be called at initialization- or destruction-time.  */
290752284Sobrien  if (priority == DEFAULT_INIT_PRIORITY)
290818334Speter    {
290952284Sobrien      tree fns;
2910117395Skan
2911169689Skan      for (fns = constructor_p ? static_ctors : static_dtors;
291252284Sobrien	   fns;
291352284Sobrien	   fns = TREE_CHAIN (fns))
2914132718Skan	{
2915132718Skan	  fndecl = TREE_VALUE (fns);
2916132718Skan
2917132718Skan	  /* Calls to pure/const functions will expand to nothing.  */
2918132718Skan	  if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2919132718Skan	    {
2920132718Skan	      if (! body)
2921132718Skan		body = start_objects (function_key, priority);
2922132718Skan	      finish_expr_stmt (build_function_call (fndecl, NULL_TREE));
2923132718Skan	    }
2924132718Skan	}
292518334Speter    }
292618334Speter
292752284Sobrien  /* Close out the function.  */
2928132718Skan  if (body)
2929132718Skan    finish_objects (function_key, priority, body);
293052284Sobrien}
293118334Speter
293252284Sobrien/* Generate constructor and destructor functions for the priority
293352284Sobrien   indicated by N.  */
293418334Speter
293552284Sobrienstatic int
2936132718Skangenerate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
293752284Sobrien{
2938169689Skan  location_t *locus = (location_t *) data;
293952284Sobrien  int priority = (int) n->key;
294052284Sobrien  priority_info pi = (priority_info) n->value;
294150397Sobrien
294252284Sobrien  /* Generate the functions themselves, but only if they are really
294352284Sobrien     needed.  */
294452284Sobrien  if (pi->initializations_p
294552284Sobrien      || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
2946132718Skan    generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
294752284Sobrien  if (pi->destructions_p
294852284Sobrien      || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
2949132718Skan    generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
295050397Sobrien
295152284Sobrien  /* Keep iterating.  */
295252284Sobrien  return 0;
295352284Sobrien}
295450397Sobrien
2955132718Skan/* Called via LANGHOOK_CALLGRAPH_ANALYZE_EXPR.  It is supposed to mark
2956132718Skan   decls referenced from frontend specific constructs; it will be called
2957132718Skan   only for language-specific tree nodes.
2958132718Skan
2959132718Skan   Here we must deal with member pointers.  */
2960132718Skan
2961132718Skantree
2962132718Skancxx_callgraph_analyze_expr (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2963132718Skan			    tree from ATTRIBUTE_UNUSED)
2964132718Skan{
2965132718Skan  tree t = *tp;
2966132718Skan
2967169689Skan  switch (TREE_CODE (t))
2968169689Skan    {
2969169689Skan    case PTRMEM_CST:
2970169689Skan      if (TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2971169689Skan	cgraph_mark_needed_node (cgraph_node (PTRMEM_CST_MEMBER (t)));
2972169689Skan      break;
2973169689Skan    case BASELINK:
2974169689Skan      if (TREE_CODE (BASELINK_FUNCTIONS (t)) == FUNCTION_DECL)
2975169689Skan	cgraph_mark_needed_node (cgraph_node (BASELINK_FUNCTIONS (t)));
2976169689Skan      break;
2977169689Skan    case VAR_DECL:
2978169689Skan      if (DECL_VTABLE_OR_VTT_P (t))
2979169689Skan	{
2980169689Skan	  /* The ABI requires that all virtual tables be emitted
2981169689Skan	     whenever one of them is.  */
2982169689Skan	  tree vtbl;
2983169689Skan	  for (vtbl = CLASSTYPE_VTABLES (DECL_CONTEXT (t));
2984169689Skan	       vtbl;
2985169689Skan	       vtbl = TREE_CHAIN (vtbl))
2986169689Skan	    mark_decl_referenced (vtbl);
2987169689Skan	}
2988169689Skan      else if (DECL_CONTEXT (t)
2989169689Skan	       && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
2990169689Skan	/* If we need a static variable in a function, then we
2991169689Skan	   need the containing function.  */
2992169689Skan	mark_decl_referenced (DECL_CONTEXT (t));
2993169689Skan      break;
2994169689Skan    default:
2995169689Skan      break;
2996169689Skan    }
2997132718Skan
2998132718Skan  return NULL;
2999132718Skan}
3000132718Skan
3001169689Skan/* Java requires that we be able to reference a local address for a
3002169689Skan   method, and not be confused by PLT entries.  If hidden aliases are
3003169689Skan   supported, emit one for each java function that we've emitted.  */
3004169689Skan
3005169689Skanstatic void
3006169689Skanbuild_java_method_aliases (void)
3007169689Skan{
3008169689Skan  struct cgraph_node *node;
3009169689Skan
3010169689Skan#ifndef HAVE_GAS_HIDDEN
3011169689Skan  return;
3012169689Skan#endif
3013169689Skan
3014169689Skan  for (node = cgraph_nodes; node ; node = node->next)
3015169689Skan    {
3016169689Skan      tree fndecl = node->decl;
3017169689Skan
3018169689Skan      if (TREE_ASM_WRITTEN (fndecl)
3019169689Skan	  && DECL_CONTEXT (fndecl)
3020169689Skan	  && TYPE_P (DECL_CONTEXT (fndecl))
3021169689Skan	  && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3022169689Skan	  && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3023169689Skan	{
3024169689Skan	  /* Mangle the name in a predictable way; we need to reference
3025169689Skan	     this from a java compiled object file.  */
3026169689Skan	  tree oid, nid, alias;
3027169689Skan	  const char *oname;
3028169689Skan	  char *nname;
3029169689Skan
3030169689Skan	  oid = DECL_ASSEMBLER_NAME (fndecl);
3031169689Skan	  oname = IDENTIFIER_POINTER (oid);
3032169689Skan	  gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3033169689Skan	  nname = ACONCAT (("_ZGA", oname+2, NULL));
3034169689Skan	  nid = get_identifier (nname);
3035169689Skan
3036169689Skan	  alias = make_alias_for (fndecl, nid);
3037169689Skan	  TREE_PUBLIC (alias) = 1;
3038169689Skan	  DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3039169689Skan
3040169689Skan	  assemble_alias (alias, oid);
3041169689Skan	}
3042169689Skan    }
3043169689Skan}
3044169689Skan
304552284Sobrien/* This routine is called from the last rule in yyparse ().
304652284Sobrien   Its job is to create all the code needed to initialize and
304752284Sobrien   destroy the global aggregates.  We do the destruction
304852284Sobrien   first, since that way we only need to reverse the decls once.  */
304918334Speter
305052284Sobrienvoid
3051169689Skancp_finish_file (void)
305252284Sobrien{
305352284Sobrien  tree vars;
3054132718Skan  bool reconsider;
305552284Sobrien  size_t i;
3056132718Skan  location_t locus;
3057132718Skan  unsigned ssdf_count = 0;
3058169689Skan  int retries = 0;
3059169689Skan  tree decl;
306052284Sobrien
3061132718Skan  locus = input_location;
306252284Sobrien  at_eof = 1;
306352284Sobrien
306452284Sobrien  /* Bad parse errors.  Just forget about it.  */
306552284Sobrien  if (! global_bindings_p () || current_class_type || decl_namespace_list)
306652284Sobrien    return;
306752284Sobrien
3068132718Skan  if (pch_file)
3069132718Skan    c_common_write_pch ();
3070132718Skan
3071169689Skan#ifdef USE_MAPPED_LOCATION
3072169689Skan  /* FIXME - huh? */
3073169689Skan#else
307452284Sobrien  /* Otherwise, GDB can get confused, because in only knows
307552284Sobrien     about source for LINENO-1 lines.  */
3076132718Skan  input_line -= 1;
3077169689Skan#endif
307852284Sobrien
307952284Sobrien  /* We now have to write out all the stuff we put off writing out.
308052284Sobrien     These include:
308152284Sobrien
308252284Sobrien       o Template specializations that we have not yet instantiated,
3083169689Skan	 but which are needed.
308452284Sobrien       o Initialization and destruction for non-local objects with
3085169689Skan	 static storage duration.  (Local objects with static storage
308652284Sobrien	 duration are initialized when their scope is first entered,
308752284Sobrien	 and are cleaned up via atexit.)
3088169689Skan       o Virtual function tables.
308952284Sobrien
309052284Sobrien     All of these may cause others to be needed.  For example,
309152284Sobrien     instantiating one function may cause another to be needed, and
3092117395Skan     generating the initializer for an object may cause templates to be
309352284Sobrien     instantiated, etc., etc.  */
309452284Sobrien
309590075Sobrien  timevar_push (TV_VARCONST);
309618334Speter
309790075Sobrien  emit_support_tinfos ();
3098169689Skan
3099169689Skan  do
310018334Speter    {
3101117395Skan      tree t;
3102169689Skan      tree decl;
3103117395Skan
3104132718Skan      reconsider = false;
310550397Sobrien
310652284Sobrien      /* If there are templates that we've put off instantiating, do
310752284Sobrien	 them now.  */
3108169689Skan      instantiate_pending_templates (retries);
3109132718Skan      ggc_collect ();
311018334Speter
311190075Sobrien      /* Write out virtual tables as required.  Note that writing out
3112169689Skan	 the virtual table for a template class may cause the
3113169689Skan	 instantiation of members of that class.  If we write out
3114169689Skan	 vtables then we remove the class from our list so we don't
3115169689Skan	 have to look at it again.  */
3116169689Skan
3117117395Skan      while (keyed_classes != NULL_TREE
3118169689Skan	     && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
3119169689Skan	{
3120169689Skan	  reconsider = true;
3121169689Skan	  keyed_classes = TREE_CHAIN (keyed_classes);
3122169689Skan	}
3123169689Skan
3124117395Skan      t = keyed_classes;
3125117395Skan      if (t != NULL_TREE)
3126169689Skan	{
3127169689Skan	  tree next = TREE_CHAIN (t);
3128169689Skan
3129169689Skan	  while (next)
3130169689Skan	    {
3131169689Skan	      if (maybe_emit_vtables (TREE_VALUE (next)))
3132169689Skan		{
3133169689Skan		  reconsider = true;
3134169689Skan		  TREE_CHAIN (t) = TREE_CHAIN (next);
3135169689Skan		}
3136169689Skan	      else
3137169689Skan		t = next;
3138169689Skan
3139169689Skan	      next = TREE_CHAIN (t);
3140169689Skan	    }
3141169689Skan	}
3142169689Skan
3143132718Skan      /* Write out needed type info variables.  We have to be careful
3144169689Skan	 looping through unemitted decls, because emit_tinfo_decl may
3145169689Skan	 cause other variables to be needed. New elements will be
3146169689Skan	 appended, and we remove from the vector those that actually
3147169689Skan	 get emitted.  */
3148169689Skan      for (i = VEC_length (tree, unemitted_tinfo_decls);
3149169689Skan	   VEC_iterate (tree, unemitted_tinfo_decls, --i, t);)
3150169689Skan	if (emit_tinfo_decl (t))
3151169689Skan	  {
3152169689Skan	    reconsider = true;
3153169689Skan	    VEC_unordered_remove (tree, unemitted_tinfo_decls, i);
3154169689Skan	  }
315570635Sobrien
315652284Sobrien      /* The list of objects with static storage duration is built up
315790075Sobrien	 in reverse order.  We clear STATIC_AGGREGATES so that any new
315890075Sobrien	 aggregates added during the initialization of these will be
315990075Sobrien	 initialized in the correct order when we next come around the
316090075Sobrien	 loop.  */
3161117395Skan      vars = prune_vars_needing_no_initialization (&static_aggregates);
316290075Sobrien
316390075Sobrien      if (vars)
316452284Sobrien	{
316590075Sobrien	  /* We need to start a new initialization function each time
316690075Sobrien	     through the loop.  That's because we need to know which
316790075Sobrien	     vtables have been referenced, and TREE_SYMBOL_REFERENCED
316890075Sobrien	     isn't computed until a function is finished, and written
316990075Sobrien	     out.  That's a deficiency in the back-end.  When this is
317090075Sobrien	     fixed, these initialization functions could all become
317190075Sobrien	     inline, with resulting performance improvements.  */
3172132718Skan	  tree ssdf_body;
317390075Sobrien
3174132718Skan	  /* Set the line and file, so that it is obviously not from
3175132718Skan	     the source file.  */
3176132718Skan	  input_location = locus;
3177132718Skan	  ssdf_body = start_static_storage_duration_function (ssdf_count);
3178132718Skan
317990075Sobrien	  /* Make sure the back end knows about all the variables.  */
318090075Sobrien	  write_out_vars (vars);
318190075Sobrien
318290075Sobrien	  /* First generate code to do all the initializations.  */
3183169689Skan	  if (vars)
3184169689Skan	    do_static_initialization_or_destruction (vars, /*initp=*/true);
318590075Sobrien
318690075Sobrien	  /* Then, generate code to do all the destructions.  Do these
318790075Sobrien	     in reverse order so that the most recently constructed
318890075Sobrien	     variable is the first destroyed.  If we're using
318990075Sobrien	     __cxa_atexit, then we don't need to do this; functions
319090075Sobrien	     were registered at initialization time to destroy the
319190075Sobrien	     local statics.  */
3192169689Skan	  if (!flag_use_cxa_atexit && vars)
319352284Sobrien	    {
319490075Sobrien	      vars = nreverse (vars);
3195169689Skan	      do_static_initialization_or_destruction (vars, /*initp=*/false);
319652284Sobrien	    }
319790075Sobrien	  else
319890075Sobrien	    vars = NULL_TREE;
319918334Speter
320090075Sobrien	  /* Finish up the static storage duration function for this
320190075Sobrien	     round.  */
3202132718Skan	  input_location = locus;
320390075Sobrien	  finish_static_storage_duration_function (ssdf_body);
320490075Sobrien
320590075Sobrien	  /* All those initializations and finalizations might cause
320690075Sobrien	     us to need more inline functions, more template
320790075Sobrien	     instantiations, etc.  */
3208132718Skan	  reconsider = true;
3209132718Skan	  ssdf_count++;
3210169689Skan#ifdef USE_MAPPED_LOCATION
3211169689Skan	  /* ??? */
3212169689Skan#else
3213132718Skan	  locus.line++;
3214169689Skan#endif
321552284Sobrien	}
3216169689Skan
3217169689Skan      /* Go through the set of inline functions whose bodies have not
3218169689Skan	 been emitted yet.  If out-of-line copies of these functions
3219169689Skan	 are required, emit them.  */
3220169689Skan      for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
322152284Sobrien	{
3222117395Skan	  /* Does it need synthesizing?  */
322352284Sobrien	  if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
322452284Sobrien	      && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
322552284Sobrien	    {
322652284Sobrien	      /* Even though we're already at the top-level, we push
322752284Sobrien		 there again.  That way, when we pop back a few lines
322852284Sobrien		 hence, all of our state is restored.  Otherwise,
322952284Sobrien		 finish_function doesn't clean things up, and we end
323052284Sobrien		 up with CURRENT_FUNCTION_DECL set.  */
323152284Sobrien	      push_to_top_level ();
3232169689Skan	      /* The decl's location will mark where it was first
3233169689Skan		 needed.  Save that so synthesize method can indicate
3234169689Skan		 where it was needed from, in case of error  */
3235169689Skan	      input_location = DECL_SOURCE_LOCATION (decl);
323690075Sobrien	      synthesize_method (decl);
323752284Sobrien	      pop_from_top_level ();
3238132718Skan	      reconsider = true;
323952284Sobrien	    }
324050397Sobrien
3241117395Skan	  if (!DECL_SAVED_TREE (decl))
3242117395Skan	    continue;
3243117395Skan
3244117395Skan	  /* We lie to the back-end, pretending that some functions
3245117395Skan	     are not defined when they really are.  This keeps these
3246117395Skan	     functions from being put out unnecessarily.  But, we must
3247117395Skan	     stop lying when the functions are referenced, or if they
3248169689Skan	     are not comdat since they need to be put out now.  If
3249169689Skan	     DECL_INTERFACE_KNOWN, then we have already set
3250169689Skan	     DECL_EXTERNAL appropriately, so there's no need to check
3251169689Skan	     again, and we do not want to clear DECL_EXTERNAL if a
3252169689Skan	     previous call to import_export_decl set it.
3253169689Skan
3254169689Skan	     This is done in a separate for cycle, because if some
3255169689Skan	     deferred function is contained in another deferred
3256169689Skan	     function later in deferred_fns varray,
3257169689Skan	     rest_of_compilation would skip this function and we
3258169689Skan	     really cannot expand the same function twice.  */
3259169689Skan	  import_export_decl (decl);
326052284Sobrien	  if (DECL_NOT_REALLY_EXTERN (decl)
326152284Sobrien	      && DECL_INITIAL (decl)
3262169689Skan	      && decl_needed_p (decl))
326352284Sobrien	    DECL_EXTERNAL (decl) = 0;
326490075Sobrien
326590075Sobrien	  /* If we're going to need to write this function out, and
326690075Sobrien	     there's already a body for it, create RTL for it now.
326790075Sobrien	     (There might be no body if this is a method we haven't
326890075Sobrien	     gotten around to synthesizing yet.)  */
326990075Sobrien	  if (!DECL_EXTERNAL (decl)
3270169689Skan	      && decl_needed_p (decl)
3271132718Skan	      && !TREE_ASM_WRITTEN (decl)
3272169689Skan	      && !cgraph_node (decl)->local.finalized)
327390075Sobrien	    {
3274132718Skan	      /* We will output the function; no longer consider it in this
3275132718Skan		 loop.  */
3276132718Skan	      DECL_DEFER_OUTPUT (decl) = 0;
327790075Sobrien	      /* Generate RTL for this function now that we know we
327890075Sobrien		 need it.  */
3279132718Skan	      expand_or_defer_fn (decl);
328090075Sobrien	      /* If we're compiling -fsyntax-only pretend that this
328190075Sobrien		 function has been written out so that we don't try to
328290075Sobrien		 expand it again.  */
328390075Sobrien	      if (flag_syntax_only)
328490075Sobrien		TREE_ASM_WRITTEN (decl) = 1;
3285132718Skan	      reconsider = true;
328690075Sobrien	    }
328752284Sobrien	}
328818334Speter
328952284Sobrien      if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3290132718Skan	reconsider = true;
329118334Speter
329252284Sobrien      /* Static data members are just like namespace-scope globals.  */
3293169689Skan      for (i = 0; VEC_iterate (tree, pending_statics, i, decl); ++i)
329452284Sobrien	{
3295169689Skan	  if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl))
329652284Sobrien	    continue;
329752284Sobrien	  import_export_decl (decl);
3298169689Skan	  /* If this static data member is needed, provide it to the
3299169689Skan	     back end.  */
3300169689Skan	  if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
330152284Sobrien	    DECL_EXTERNAL (decl) = 0;
330252284Sobrien	}
3303169689Skan      if (VEC_length (tree, pending_statics) != 0
3304169689Skan	  && wrapup_global_declarations (VEC_address (tree, pending_statics),
3305169689Skan					 VEC_length (tree, pending_statics)))
3306132718Skan	reconsider = true;
3307132718Skan
3308169689Skan      retries++;
3309169689Skan    }
331052284Sobrien  while (reconsider);
331118334Speter
3312132718Skan  /* All used inline functions must have a definition at this point.  */
3313169689Skan  for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3314117395Skan    {
3315169689Skan      if (/* Check online inline functions that were actually used.  */
3316169689Skan	  TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
3317169689Skan	  /* If the definition actually was available here, then the
3318169689Skan	     fact that the function was not defined merely represents
3319169689Skan	     that for some reason (use of a template repository,
3320169689Skan	     #pragma interface, etc.) we decided not to emit the
3321169689Skan	     definition here.  */
3322169689Skan	  && !DECL_INITIAL (decl)
3323169689Skan	  /* An explicit instantiation can be used to specify
3324169689Skan	     that the body is in another unit. It will have
3325169689Skan	     already verified there was a definition.  */
3326169689Skan	  && !DECL_EXPLICIT_INSTANTIATION (decl))
3327132718Skan	{
3328169689Skan	  warning (0, "inline function %q+D used but never defined", decl);
3329169689Skan	  /* Avoid a duplicate warning from check_global_declaration_1.  */
3330169689Skan	  TREE_NO_WARNING (decl) = 1;
3331132718Skan	}
3332117395Skan    }
3333169689Skan
333452284Sobrien  /* We give C linkage to static constructors and destructors.  */
333552284Sobrien  push_lang_context (lang_name_c);
333650397Sobrien
333752284Sobrien  /* Generate initialization and destruction functions for all
333852284Sobrien     priorities for which they are required.  */
333952284Sobrien  if (priority_info_map)
3340169689Skan    splay_tree_foreach (priority_info_map,
334152284Sobrien			generate_ctor_and_dtor_functions_for_priority,
3342132718Skan			/*data=*/&locus);
3343117395Skan  else
3344117395Skan    {
3345169689Skan      /* If we have a ctor or this is obj-c++ and we need a static init,
3346169689Skan	 call generate_ctor_or_dtor_function.  */
3347169689Skan      if (static_ctors || (c_dialect_objc () && objc_static_init_needed_p ()))
3348117395Skan	generate_ctor_or_dtor_function (/*constructor_p=*/true,
3349132718Skan					DEFAULT_INIT_PRIORITY, &locus);
3350117395Skan      if (static_dtors)
3351117395Skan	generate_ctor_or_dtor_function (/*constructor_p=*/false,
3352132718Skan					DEFAULT_INIT_PRIORITY, &locus);
3353117395Skan    }
335450397Sobrien
335552284Sobrien  /* We're done with the splay-tree now.  */
335652284Sobrien  if (priority_info_map)
335752284Sobrien    splay_tree_delete (priority_info_map);
335850397Sobrien
3359169689Skan  /* Generate any missing aliases.  */
3360169689Skan  maybe_apply_pending_pragma_weaks ();
3361169689Skan
336252284Sobrien  /* We're done with static constructors, so we can go back to "C++"
336352284Sobrien     linkage now.  */
336452284Sobrien  pop_lang_context ();
336518334Speter
3366169689Skan  cgraph_finalize_compilation_unit ();
3367169689Skan  cgraph_optimize ();
3368132718Skan
336952284Sobrien  /* Now, issue warnings about static, but not defined, functions,
337090075Sobrien     etc., and emit debugging information.  */
337152284Sobrien  walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3372169689Skan  if (VEC_length (tree, pending_statics) != 0)
3373169689Skan    {
3374169689Skan      check_global_declarations (VEC_address (tree, pending_statics),
3375169689Skan				 VEC_length (tree, pending_statics));
3376169689Skan      emit_debug_global_declarations (VEC_address (tree, pending_statics),
3377169689Skan				      VEC_length (tree, pending_statics));
3378169689Skan    }
337918334Speter
3380169689Skan  /* Generate hidden aliases for Java.  */
3381169689Skan  build_java_method_aliases ();
3382169689Skan
338318334Speter  finish_repo ();
338418334Speter
338590075Sobrien  /* The entire file is now complete.  If requested, dump everything
3386117395Skan     to a file.  */
338790075Sobrien  {
338890075Sobrien    int flags;
3389169689Skan    FILE *stream = dump_begin (TDI_tu, &flags);
339018334Speter
339190075Sobrien    if (stream)
339290075Sobrien      {
339390075Sobrien	dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3394169689Skan	dump_end (TDI_tu, stream);
339590075Sobrien      }
339690075Sobrien  }
3397169689Skan
339890075Sobrien  timevar_pop (TV_VARCONST);
339990075Sobrien
340018334Speter  if (flag_detailed_statistics)
340150397Sobrien    {
340250397Sobrien      dump_tree_statistics ();
340350397Sobrien      dump_time_statistics ();
340450397Sobrien    }
3405132718Skan  input_location = locus;
3406169689Skan
3407169689Skan#ifdef ENABLE_CHECKING
3408169689Skan  validate_conversion_obstack ();
3409169689Skan#endif /* ENABLE_CHECKING */
341018334Speter}
341118334Speter
3412132718Skan/* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
3413132718Skan   function to call in parse-tree form; it has not yet been
3414132718Skan   semantically analyzed.  ARGS are the arguments to the function.
3415132718Skan   They have already been semantically analyzed.  */
341618334Speter
341718334Spetertree
3418132718Skanbuild_offset_ref_call_from_tree (tree fn, tree args)
341918334Speter{
3420132718Skan  tree orig_fn;
3421132718Skan  tree orig_args;
3422132718Skan  tree expr;
3423132718Skan  tree object;
342418334Speter
3425132718Skan  orig_fn = fn;
3426132718Skan  orig_args = args;
3427132718Skan  object = TREE_OPERAND (fn, 0);
342818334Speter
3429132718Skan  if (processing_template_decl)
343018334Speter    {
3431169689Skan      gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
3432169689Skan		  || TREE_CODE (fn) == MEMBER_REF);
3433132718Skan      if (type_dependent_expression_p (fn)
3434132718Skan	  || any_type_dependent_arguments_p (args))
3435169689Skan	return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
343618334Speter
3437132718Skan      /* Transform the arguments and add the implicit "this"
3438132718Skan	 parameter.  That must be done before the FN is transformed
3439132718Skan	 because we depend on the form of FN.  */
3440132718Skan      args = build_non_dependent_args (args);
3441132718Skan      if (TREE_CODE (fn) == DOTSTAR_EXPR)
3442132718Skan	object = build_unary_op (ADDR_EXPR, object, 0);
3443132718Skan      object = build_non_dependent_expr (object);
3444132718Skan      args = tree_cons (NULL_TREE, object, args);
3445132718Skan      /* Now that the arguments are done, transform FN.  */
3446132718Skan      fn = build_non_dependent_expr (fn);
344718334Speter    }
344818334Speter
3449132718Skan  /* A qualified name corresponding to a bound pointer-to-member is
3450132718Skan     represented as an OFFSET_REF:
345118334Speter
3452117395Skan	struct B { void g(); };
3453117395Skan	void (B::*p)();
3454117395Skan	void B::g() { (this->*p)(); }  */
3455132718Skan  if (TREE_CODE (fn) == OFFSET_REF)
3456117395Skan    {
3457132718Skan      tree object_addr = build_unary_op (ADDR_EXPR, object, 0);
3458117395Skan      fn = TREE_OPERAND (fn, 1);
3459117395Skan      fn = get_member_function_from_ptrfunc (&object_addr, fn);
3460117395Skan      args = tree_cons (NULL_TREE, object_addr, args);
3461117395Skan    }
3462117395Skan
3463132718Skan  expr = build_function_call (fn, args);
3464132718Skan  if (processing_template_decl && expr != error_mark_node)
3465169689Skan    return build_min_non_dep (CALL_EXPR, expr, orig_fn, orig_args, NULL_TREE);
3466132718Skan  return expr;
3467117395Skan}
3468117395Skan
3469169689Skan
347018334Spetervoid
3471132718Skancheck_default_args (tree x)
347250397Sobrien{
347318334Speter  tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
3474132718Skan  bool saw_def = false;
3475132718Skan  int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
347618334Speter  for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
347718334Speter    {
347818334Speter      if (TREE_PURPOSE (arg))
3479132718Skan	saw_def = true;
348018334Speter      else if (saw_def)
348118334Speter	{
3482169689Skan	  error ("default argument missing for parameter %P of %q+#D", i, x);
3483161651Skan	  TREE_PURPOSE (arg) = error_mark_node;
348418334Speter	}
348518334Speter    }
348618334Speter}
348750397Sobrien
3488169689Skan/* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
3489169689Skan   If DECL is a specialization or implicitly declared class member,
3490169689Skan   generate the actual definition.  */
3491169689Skan
349250397Sobrienvoid
3493132718Skanmark_used (tree decl)
349450397Sobrien{
3495169689Skan  HOST_WIDE_INT saved_processing_template_decl = 0;
3496169689Skan
3497161651Skan  /* If DECL is a BASELINK for a single function, then treat it just
3498161651Skan     like the DECL for the function.  Otherwise, if the BASELINK is
3499161651Skan     for an overloaded function, we don't know which function was
3500161651Skan     actually used until after overload resolution.  */
3501161651Skan  if (TREE_CODE (decl) == BASELINK)
3502161651Skan    {
3503161651Skan      decl = BASELINK_FUNCTIONS (decl);
3504161651Skan      if (really_overloaded_fn (decl))
3505161651Skan	return;
3506161651Skan      decl = OVL_CURRENT (decl);
3507161651Skan    }
3508161651Skan
350950397Sobrien  TREE_USED (decl) = 1;
3510169689Skan  if (DECL_CLONED_FUNCTION_P (decl))
3511169689Skan    TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
3512169689Skan  /* If we don't need a value, then we don't need to synthesize DECL.  */
3513169689Skan  if (skip_evaluation)
351450397Sobrien    return;
3515169689Skan  /* Normally, we can wait until instantiation-time to synthesize
3516169689Skan     DECL.  However, if DECL is a static data member initialized with
3517169689Skan     a constant, we need the value right now because a reference to
3518169689Skan     such a data member is not value-dependent.  */
3519169689Skan  if (TREE_CODE (decl) == VAR_DECL
3520169689Skan      && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
3521169689Skan      && DECL_CLASS_SCOPE_P (decl))
3522169689Skan    {
3523169689Skan      /* Don't try to instantiate members of dependent types.  We
3524169689Skan	 cannot just use dependent_type_p here because this function
3525169689Skan	 may be called from fold_non_dependent_expr, and then we may
3526169689Skan	 see dependent types, even though processing_template_decl
3527169689Skan	 will not be set.  */
3528169689Skan      if (CLASSTYPE_TEMPLATE_INFO ((DECL_CONTEXT (decl)))
3529169689Skan	  && uses_template_parms (CLASSTYPE_TI_ARGS (DECL_CONTEXT (decl))))
3530169689Skan	return;
3531169689Skan      /* Pretend that we are not in a template, even if we are, so
3532169689Skan	 that the static data member initializer will be processed.  */
3533169689Skan      saved_processing_template_decl = processing_template_decl;
3534169689Skan      processing_template_decl = 0;
3535169689Skan    }
353652284Sobrien
3537169689Skan  if (processing_template_decl)
3538169689Skan    return;
3539169689Skan
3540117395Skan  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
3541117395Skan      && !TREE_ASM_WRITTEN (decl))
3542117395Skan    /* Remember it, so we can check it was defined.  */
3543169689Skan    {
3544169689Skan      if (DECL_DEFERRED_FN (decl))
3545169689Skan	return;
3546117395Skan
3547169689Skan      /* Remember the current location for a function we will end up
3548169689Skan	 synthesizing.  Then we can inform the user where it was
3549169689Skan	 required in the case of error.  */
3550169689Skan      if (DECL_ARTIFICIAL (decl) && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
3551169689Skan	  && !DECL_THUNK_P (decl))
3552169689Skan	DECL_SOURCE_LOCATION (decl) = input_location;
3553169689Skan
3554169689Skan      note_vague_linkage_fn (decl);
3555169689Skan    }
3556169689Skan
3557117395Skan  assemble_external (decl);
3558117395Skan
355950397Sobrien  /* Is it a synthesized method that needs to be synthesized?  */
356090075Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL
356190075Sobrien      && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
3562169689Skan      && DECL_ARTIFICIAL (decl)
3563132718Skan      && !DECL_THUNK_P (decl)
356490075Sobrien      && ! DECL_INITIAL (decl)
3565169689Skan      /* Kludge: don't synthesize for default args.  Unfortunately this
3566169689Skan	 rules out initializers of namespace-scoped objects too, but
3567169689Skan	 it's sort-of ok if the implicit ctor or dtor decl keeps
3568169689Skan	 pointing to the class location.  */
356950397Sobrien      && current_function_decl)
357090075Sobrien    {
357190075Sobrien      synthesize_method (decl);
357290075Sobrien      /* If we've already synthesized the method we don't need to
3573169689Skan	 do the instantiation test below.  */
357490075Sobrien    }
3575169689Skan  else if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
3576169689Skan	   && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3577169689Skan	   && (!DECL_EXPLICIT_INSTANTIATION (decl)
3578169689Skan	       || (TREE_CODE (decl) == FUNCTION_DECL
3579169689Skan		   && DECL_INLINE (DECL_TEMPLATE_RESULT
3580169689Skan				   (template_for_substitution (decl))))
3581169689Skan	       /* We need to instantiate static data members so that there
3582169689Skan		  initializers are available in integral constant
3583169689Skan		  expressions.  */
3584169689Skan	       || (TREE_CODE (decl) == VAR_DECL
3585169689Skan		   && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))))
3586169689Skan    /* If this is a function or variable that is an instance of some
3587169689Skan       template, we now know that we will need to actually do the
3588169689Skan       instantiation. We check that DECL is not an explicit
3589169689Skan       instantiation because that is not checked in instantiate_decl.
359052284Sobrien
3591169689Skan       We put off instantiating functions in order to improve compile
3592169689Skan       times.  Maintaining a stack of active functions is expensive,
3593169689Skan       and the inliner knows to instantiate any functions it might
3594169689Skan       need.  Therefore, we always try to defer instantiation.  */
3595169689Skan    instantiate_decl (decl, /*defer_ok=*/true,
3596169689Skan		      /*expl_inst_class_mem_p=*/false);
3597117395Skan
3598169689Skan  processing_template_decl = saved_processing_template_decl;
359950397Sobrien}
360050397Sobrien
3601117395Skan#include "gt-cp-decl2.h"
3602