118334Speter/* Report error messages, build initializers, and perform
218334Speter   some front-end optimizations for C++ compiler.
390075Sobrien   Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4169689Skan   1999, 2000, 2001, 2002, 2004, 2005, 2006
5169689Skan   Free Software Foundation, Inc.
618334Speter   Hacked by Michael Tiemann (tiemann@cygnus.com)
718334Speter
8132718SkanThis file is part of GCC.
918334Speter
10132718SkanGCC is free software; you can redistribute it and/or modify
1118334Speterit under the terms of the GNU General Public License as published by
1218334Speterthe Free Software Foundation; either version 2, or (at your option)
1318334Speterany later version.
1418334Speter
15132718SkanGCC is distributed in the hope that it will be useful,
1618334Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of
1718334SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1818334SpeterGNU General Public License for more details.
1918334Speter
2018334SpeterYou should have received a copy of the GNU General Public License
21132718Skanalong with GCC; see the file COPYING.  If not, write to
22169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
23169689SkanBoston, MA 02110-1301, USA.  */
2418334Speter
2518334Speter
2618334Speter/* This file is part of the C++ front end.
2718334Speter   It contains routines to build C++ expressions given their operands,
2818334Speter   including computing the types of the result, C and C++ specific error
29169689Skan   checks, and some optimization.  */
3018334Speter
3118334Speter#include "config.h"
3250397Sobrien#include "system.h"
33132718Skan#include "coretypes.h"
34132718Skan#include "tm.h"
3518334Speter#include "tree.h"
3618334Speter#include "cp-tree.h"
3718334Speter#include "flags.h"
3850397Sobrien#include "toplev.h"
3990075Sobrien#include "output.h"
4090075Sobrien#include "diagnostic.h"
4118334Speter
42169689Skanstatic tree
43169689Skanprocess_init_constructor (tree type, tree init);
4418334Speter
45169689Skan
4618334Speter/* Print an error message stemming from an attempt to use
4718334Speter   BASETYPE as a base class for TYPE.  */
4850397Sobrien
4918334Spetertree
50132718Skanerror_not_base_type (tree basetype, tree type)
5118334Speter{
5218334Speter  if (TREE_CODE (basetype) == FUNCTION_DECL)
5390075Sobrien    basetype = DECL_CONTEXT (basetype);
54169689Skan  error ("type %qT is not a base type for type %qT", basetype, type);
5518334Speter  return error_mark_node;
5618334Speter}
5718334Speter
5818334Spetertree
59132718Skanbinfo_or_else (tree base, tree type)
6018334Speter{
61169689Skan  tree binfo = lookup_base (type, base, ba_unique, NULL);
6290075Sobrien
6390075Sobrien  if (binfo == error_mark_node)
6490075Sobrien    return NULL_TREE;
6590075Sobrien  else if (!binfo)
6690075Sobrien    error_not_base_type (base, type);
6790075Sobrien  return binfo;
6818334Speter}
6918334Speter
7018334Speter/* According to ARM $7.1.6, "A `const' object may be initialized, but its
7118334Speter   value may not be changed thereafter.  Thus, we emit hard errors for these,
7218334Speter   rather than just pedwarns.  If `SOFT' is 1, then we just pedwarn.  (For
7318334Speter   example, conversions to references.)  */
7450397Sobrien
7518334Spetervoid
76132718Skanreadonly_error (tree arg, const char* string, int soft)
7718334Speter{
7852284Sobrien  const char *fmt;
79169689Skan  void (*fn) (const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
8018334Speter
8118334Speter  if (soft)
8290075Sobrien    fn = pedwarn;
8318334Speter  else
8490075Sobrien    fn = error;
8518334Speter
8618334Speter  if (TREE_CODE (arg) == COMPONENT_REF)
8718334Speter    {
8818334Speter      if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
89169689Skan	fmt = "%s of data-member %qD in read-only structure";
9018334Speter      else
91169689Skan	fmt = "%s of read-only data-member %qD";
9250397Sobrien      (*fn) (fmt, string, TREE_OPERAND (arg, 1));
9318334Speter    }
9418334Speter  else if (TREE_CODE (arg) == VAR_DECL)
9518334Speter    {
9618334Speter      if (DECL_LANG_SPECIFIC (arg)
9718334Speter	  && DECL_IN_AGGR_P (arg)
9818334Speter	  && !TREE_STATIC (arg))
99169689Skan	fmt = "%s of constant field %qD";
10018334Speter      else
101169689Skan	fmt = "%s of read-only variable %qD";
10250397Sobrien      (*fn) (fmt, string, arg);
10318334Speter    }
10418334Speter  else if (TREE_CODE (arg) == PARM_DECL)
105169689Skan    (*fn) ("%s of read-only parameter %qD", string, arg);
10618334Speter  else if (TREE_CODE (arg) == INDIRECT_REF
107169689Skan	   && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
108169689Skan	   && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
109169689Skan	       || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
110169689Skan    (*fn) ("%s of read-only reference %qD", string, TREE_OPERAND (arg, 0));
11118334Speter  else if (TREE_CODE (arg) == RESULT_DECL)
112169689Skan    (*fn) ("%s of read-only named return value %qD", string, arg);
11352284Sobrien  else if (TREE_CODE (arg) == FUNCTION_DECL)
114169689Skan    (*fn) ("%s of function %qD", string, arg);
11552284Sobrien  else
11618334Speter    (*fn) ("%s of read-only location", string);
11718334Speter}
11818334Speter
119169689Skan
120169689Skan/* Structure that holds information about declarations whose type was
121169689Skan   incomplete and we could not check whether it was abstract or not.  */
122169689Skan
123169689Skanstruct pending_abstract_type GTY((chain_next ("%h.next")))
124169689Skan{
125169689Skan  /* Declaration which we are checking for abstractness. It is either
126169689Skan     a DECL node, or an IDENTIFIER_NODE if we do not have a full
127169689Skan     declaration available.  */
128169689Skan  tree decl;
129169689Skan
130169689Skan  /* Type which will be checked for abstractness.  */
131169689Skan  tree type;
132169689Skan
133169689Skan  /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
134169689Skan     because DECLs already carry locus information.  */
135169689Skan  location_t locus;
136169689Skan
137169689Skan  /* Link to the next element in list.  */
138169689Skan  struct pending_abstract_type* next;
139169689Skan};
140169689Skan
141169689Skan
142169689Skan/* Compute the hash value of the node VAL. This function is used by the
143169689Skan   hash table abstract_pending_vars.  */
144169689Skan
145169689Skanstatic hashval_t
146169689Skanpat_calc_hash (const void* val)
147169689Skan{
148169689Skan  const struct pending_abstract_type *pat =
149169689Skan     (const struct pending_abstract_type *) val;
150169689Skan  return (hashval_t) TYPE_UID (pat->type);
151169689Skan}
152169689Skan
153169689Skan
154169689Skan/* Compare node VAL1 with the type VAL2. This function is used by the
155169689Skan   hash table abstract_pending_vars.  */
156169689Skan
157169689Skanstatic int
158169689Skanpat_compare (const void* val1, const void* val2)
159169689Skan{
160169689Skan  const struct pending_abstract_type *pat1 =
161169689Skan     (const struct pending_abstract_type *) val1;
162169689Skan  tree type2 = (tree)val2;
163169689Skan
164169689Skan  return (pat1->type == type2);
165169689Skan}
166169689Skan
167169689Skan/* Hash table that maintains pending_abstract_type nodes, for which we still
168169689Skan   need to check for type abstractness.  The key of the table is the type
169169689Skan   of the declaration.  */
170169689Skanstatic GTY ((param_is (struct pending_abstract_type)))
171169689Skanhtab_t abstract_pending_vars = NULL;
172169689Skan
173169689Skan
174169689Skan/* This function is called after TYPE is completed, and will check if there
175169689Skan   are pending declarations for which we still need to verify the abstractness
176169689Skan   of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
177169689Skan   turned out to be incomplete.  */
178169689Skan
179169689Skanvoid
180169689Skancomplete_type_check_abstract (tree type)
181169689Skan{
182169689Skan  void **slot;
183169689Skan  struct pending_abstract_type *pat;
184169689Skan  location_t cur_loc = input_location;
185169689Skan
186169689Skan  gcc_assert (COMPLETE_TYPE_P (type));
187169689Skan
188169689Skan  if (!abstract_pending_vars)
189169689Skan    return;
190169689Skan
191169689Skan  /* Retrieve the list of pending declarations for this type.  */
192169689Skan  slot = htab_find_slot_with_hash (abstract_pending_vars, type,
193169689Skan				   (hashval_t)TYPE_UID (type), NO_INSERT);
194169689Skan  if (!slot)
195169689Skan    return;
196169689Skan  pat = (struct pending_abstract_type*)*slot;
197169689Skan  gcc_assert (pat);
198169689Skan
199169689Skan  /* If the type is not abstract, do not do anything.  */
200169689Skan  if (CLASSTYPE_PURE_VIRTUALS (type))
201169689Skan    {
202169689Skan      struct pending_abstract_type *prev = 0, *next;
203169689Skan
204169689Skan      /* Reverse the list to emit the errors in top-down order.  */
205169689Skan      for (; pat; pat = next)
206169689Skan	{
207169689Skan	  next = pat->next;
208169689Skan	  pat->next = prev;
209169689Skan	  prev = pat;
210169689Skan	}
211169689Skan      pat = prev;
212169689Skan
213169689Skan      /* Go through the list, and call abstract_virtuals_error for each
214169689Skan	element: it will issue a diagnostic if the type is abstract.  */
215169689Skan      while (pat)
216169689Skan	{
217169689Skan	  gcc_assert (type == pat->type);
218169689Skan
219169689Skan	  /* Tweak input_location so that the diagnostic appears at the correct
220169689Skan	    location. Notice that this is only needed if the decl is an
221169689Skan	    IDENTIFIER_NODE.  */
222169689Skan	  input_location = pat->locus;
223169689Skan	  abstract_virtuals_error (pat->decl, pat->type);
224169689Skan	  pat = pat->next;
225169689Skan	}
226169689Skan    }
227169689Skan
228169689Skan  htab_clear_slot (abstract_pending_vars, slot);
229169689Skan
230169689Skan  input_location = cur_loc;
231169689Skan}
232169689Skan
233169689Skan
23490075Sobrien/* If TYPE has abstract virtual functions, issue an error about trying
23590075Sobrien   to create an object of that type.  DECL is the object declared, or
23690075Sobrien   NULL_TREE if the declaration is unavailable.  Returns 1 if an error
23790075Sobrien   occurred; zero if all was well.  */
23850397Sobrien
23990075Sobrienint
240132718Skanabstract_virtuals_error (tree decl, tree type)
24118334Speter{
242169689Skan  VEC(tree,gc) *pure;
24318334Speter
244169689Skan  /* This function applies only to classes. Any other entity can never
245169689Skan     be abstract.  */
246169689Skan  if (!CLASS_TYPE_P (type))
24790075Sobrien    return 0;
24890075Sobrien
249169689Skan  /* If the type is incomplete, we register it within a hash table,
250169689Skan     so that we can check again once it is completed. This makes sense
251169689Skan     only for objects for which we have a declaration or at least a
252169689Skan     name.  */
253169689Skan  if (!COMPLETE_TYPE_P (type))
254169689Skan    {
255169689Skan      void **slot;
256169689Skan      struct pending_abstract_type *pat;
257169689Skan
258169689Skan      gcc_assert (!decl || DECL_P (decl)
259169689Skan		  || TREE_CODE (decl) == IDENTIFIER_NODE);
260169689Skan
261169689Skan      if (!abstract_pending_vars)
262169689Skan	abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash,
263169689Skan						&pat_compare, NULL);
264169689Skan
265169689Skan      slot = htab_find_slot_with_hash (abstract_pending_vars, type,
266169689Skan				      (hashval_t)TYPE_UID (type), INSERT);
267169689Skan
268169689Skan      pat = GGC_NEW (struct pending_abstract_type);
269169689Skan      pat->type = type;
270169689Skan      pat->decl = decl;
271169689Skan      pat->locus = ((decl && DECL_P (decl))
272169689Skan		    ? DECL_SOURCE_LOCATION (decl)
273169689Skan		    : input_location);
274169689Skan
275169689Skan      pat->next = (struct pending_abstract_type *) *slot;
276169689Skan      *slot = pat;
277169689Skan
278169689Skan      return 0;
279169689Skan    }
280169689Skan
28190075Sobrien  if (!TYPE_SIZE (type))
28290075Sobrien    /* TYPE is being defined, and during that time
28390075Sobrien       CLASSTYPE_PURE_VIRTUALS holds the inline friends.  */
28490075Sobrien    return 0;
28590075Sobrien
286169689Skan  pure = CLASSTYPE_PURE_VIRTUALS (type);
287169689Skan  if (!pure)
288132718Skan    return 0;
289132718Skan
29018334Speter  if (decl)
29118334Speter    {
29218334Speter      if (TREE_CODE (decl) == RESULT_DECL)
29390075Sobrien	return 0;
29418334Speter
29518334Speter      if (TREE_CODE (decl) == VAR_DECL)
296169689Skan	error ("cannot declare variable %q+D to be of abstract "
297169689Skan	       "type %qT", decl, type);
29818334Speter      else if (TREE_CODE (decl) == PARM_DECL)
299169689Skan	error ("cannot declare parameter %q+D to be of abstract type %qT",
300169689Skan	       decl, type);
30118334Speter      else if (TREE_CODE (decl) == FIELD_DECL)
302169689Skan	error ("cannot declare field %q+D to be of abstract type %qT",
303169689Skan	       decl, type);
30418334Speter      else if (TREE_CODE (decl) == FUNCTION_DECL
30518334Speter	       && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
306169689Skan	error ("invalid abstract return type for member function %q+#D", decl);
30718334Speter      else if (TREE_CODE (decl) == FUNCTION_DECL)
308169689Skan	error ("invalid abstract return type for function %q+#D", decl);
309169689Skan      else if (TREE_CODE (decl) == IDENTIFIER_NODE)
310169689Skan	/* Here we do not have location information.  */
311169689Skan	error ("invalid abstract type %qT for %qE", type, decl);
312169689Skan      else
313169689Skan	error ("invalid abstract type for %q+D", decl);
31418334Speter    }
31550397Sobrien  else
316169689Skan    error ("cannot allocate an object of abstract type %qT", type);
31750397Sobrien
31818334Speter  /* Only go through this once.  */
319169689Skan  if (VEC_length (tree, pure))
32018334Speter    {
321169689Skan      unsigned ix;
322169689Skan      tree fn;
32350397Sobrien
324169689Skan      inform ("%J  because the following virtual functions are pure "
325169689Skan	      "within %qT:", TYPE_MAIN_DECL (type), type);
326169689Skan
327169689Skan      for (ix = 0; VEC_iterate (tree, pure, ix, fn); ix++)
328169689Skan	inform ("\t%+#D", fn);
329169689Skan      /* Now truncate the vector.  This leaves it non-null, so we know
330169689Skan	 there are pure virtuals, but empty so we don't list them out
331169689Skan	 again.  */
332169689Skan      VEC_truncate (tree, pure, 0);
33318334Speter    }
33450397Sobrien  else
335169689Skan    inform ("%J  since type %qT has pure virtual functions",
336169689Skan	    TYPE_MAIN_DECL (type), type);
33718334Speter
33890075Sobrien  return 1;
33918334Speter}
34018334Speter
34118334Speter/* Print an error message for invalid use of an incomplete type.
34218334Speter   VALUE is the expression that was used (or 0 if that isn't known)
343117395Skan   and TYPE is the type that was invalid.  DIAG_TYPE indicates the
344117395Skan   type of diagnostic:  0 for an error, 1 for a warning, 2 for a
345117395Skan   pedwarn.  */
34618334Speter
34718334Spetervoid
348132718Skancxx_incomplete_type_diagnostic (tree value, tree type, int diag_type)
34918334Speter{
35090075Sobrien  int decl = 0;
351169689Skan  void (*p_msg) (const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
352117395Skan
353117395Skan  if (diag_type == 1)
354169689Skan    p_msg = warning0;
355117395Skan  else if (diag_type == 2)
356169689Skan    p_msg = pedwarn;
357117395Skan  else
358169689Skan    p_msg = error;
359169689Skan
36018334Speter  /* Avoid duplicate error message.  */
36118334Speter  if (TREE_CODE (type) == ERROR_MARK)
36218334Speter    return;
36318334Speter
36490075Sobrien  if (value != 0 && (TREE_CODE (value) == VAR_DECL
365102780Skan		     || TREE_CODE (value) == PARM_DECL
366102780Skan		     || TREE_CODE (value) == FIELD_DECL))
36790075Sobrien    {
368169689Skan      p_msg ("%q+D has incomplete type", value);
36990075Sobrien      decl = 1;
37090075Sobrien    }
371169689Skan retry:
37252284Sobrien  /* We must print an error message.  Be clever about what it says.  */
37352284Sobrien
37452284Sobrien  switch (TREE_CODE (type))
37518334Speter    {
37652284Sobrien    case RECORD_TYPE:
37752284Sobrien    case UNION_TYPE:
37852284Sobrien    case ENUMERAL_TYPE:
37990075Sobrien      if (!decl)
380169689Skan	p_msg ("invalid use of incomplete type %q#T", type);
381102780Skan      if (!TYPE_TEMPLATE_INFO (type))
382169689Skan	p_msg ("forward declaration of %q+#T", type);
383102780Skan      else
384169689Skan	p_msg ("declaration of %q+#T", type);
38552284Sobrien      break;
38618334Speter
38752284Sobrien    case VOID_TYPE:
388169689Skan      p_msg ("invalid use of %qT", type);
38952284Sobrien      break;
39018334Speter
39152284Sobrien    case ARRAY_TYPE:
39252284Sobrien      if (TYPE_DOMAIN (type))
393169689Skan	{
394169689Skan	  type = TREE_TYPE (type);
395169689Skan	  goto retry;
396169689Skan	}
397169689Skan      p_msg ("invalid use of array with unspecified bounds");
39852284Sobrien      break;
39918334Speter
40052284Sobrien    case OFFSET_TYPE:
40152284Sobrien    bad_member:
402169689Skan      p_msg ("invalid use of member (did you forget the %<&%> ?)");
40352284Sobrien      break;
40418334Speter
40552284Sobrien    case TEMPLATE_TYPE_PARM:
406169689Skan      p_msg ("invalid use of template type parameter %qT", type);
40752284Sobrien      break;
40818334Speter
409169689Skan    case BOUND_TEMPLATE_TEMPLATE_PARM:
410169689Skan      p_msg ("invalid use of template template parameter %qT",
411169689Skan            TYPE_NAME (type));
412169689Skan      break;
413169689Skan
414169689Skan    case TYPENAME_TYPE:
415169689Skan      p_msg ("invalid use of dependent type %qT", type);
416169689Skan      break;
417169689Skan
41852284Sobrien    case UNKNOWN_TYPE:
41952284Sobrien      if (value && TREE_CODE (value) == COMPONENT_REF)
420169689Skan	goto bad_member;
42152284Sobrien      else if (value && TREE_CODE (value) == ADDR_EXPR)
422169689Skan	p_msg ("address of overloaded function with no contextual "
423169689Skan	       "type information");
42452284Sobrien      else if (value && TREE_CODE (value) == OVERLOAD)
425169689Skan	p_msg ("overloaded function with no contextual type information");
42652284Sobrien      else
427169689Skan	p_msg ("insufficient contextual information to determine type");
42852284Sobrien      break;
429169689Skan
43052284Sobrien    default:
431169689Skan      gcc_unreachable ();
43252284Sobrien    }
43318334Speter}
43418334Speter
435117395Skan/* Backward-compatibility interface to incomplete_type_diagnostic;
436117395Skan   required by ../tree.c.  */
437117395Skan#undef cxx_incomplete_type_error
438117395Skanvoid
439132718Skancxx_incomplete_type_error (tree value, tree type)
440117395Skan{
441117395Skan  cxx_incomplete_type_diagnostic (value, type, 0);
442117395Skan}
443117395Skan
44418334Speter
445132718Skan/* The recursive part of split_nonconstant_init.  DEST is an lvalue
446169689Skan   expression to which INIT should be assigned.  INIT is a CONSTRUCTOR.  */
447132718Skan
448169689Skanstatic void
449169689Skansplit_nonconstant_init_1 (tree dest, tree init)
450132718Skan{
451169689Skan  unsigned HOST_WIDE_INT idx;
452169689Skan  tree field_index, value;
453169689Skan  tree type = TREE_TYPE (dest);
454169689Skan  tree inner_type = NULL;
455132718Skan  bool array_type_p = false;
456132718Skan
457132718Skan  switch (TREE_CODE (type))
458132718Skan    {
459132718Skan    case ARRAY_TYPE:
460132718Skan      inner_type = TREE_TYPE (type);
461132718Skan      array_type_p = true;
462132718Skan      /* FALLTHRU */
463132718Skan
464132718Skan    case RECORD_TYPE:
465132718Skan    case UNION_TYPE:
466132718Skan    case QUAL_UNION_TYPE:
467169689Skan      FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
468169689Skan				field_index, value)
469132718Skan	{
470169689Skan	  /* The current implementation of this algorithm assumes that
471169689Skan	     the field was set for all the elements. This is usually done
472169689Skan	     by process_init_constructor.  */
473169689Skan	  gcc_assert (field_index);
474132718Skan
475132718Skan	  if (!array_type_p)
476132718Skan	    inner_type = TREE_TYPE (field_index);
477132718Skan
478132718Skan	  if (TREE_CODE (value) == CONSTRUCTOR)
479132718Skan	    {
480169689Skan	      tree sub;
481169689Skan
482132718Skan	      if (array_type_p)
483169689Skan		sub = build4 (ARRAY_REF, inner_type, dest, field_index,
484169689Skan			      NULL_TREE, NULL_TREE);
485132718Skan	      else
486169689Skan		sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
487169689Skan			      NULL_TREE);
488132718Skan
489169689Skan	      split_nonconstant_init_1 (sub, value);
490132718Skan	    }
491132718Skan	  else if (!initializer_constant_valid_p (value, inner_type))
492132718Skan	    {
493169689Skan	      tree code;
494169689Skan	      tree sub;
495132718Skan
496169689Skan	      /* FIXME: Ordered removal is O(1) so the whole function is
497169689Skan		 worst-case quadratic. This could be fixed using an aside
498169689Skan		 bitmap to record which elements must be removed and remove
499169689Skan		 them all at the same time. Or by merging
500169689Skan		 split_non_constant_init into process_init_constructor_array,
501169689Skan		 that is separating constants from non-constants while building
502169689Skan		 the vector.  */
503169689Skan	      VEC_ordered_remove (constructor_elt, CONSTRUCTOR_ELTS (init),
504169689Skan				  idx);
505169689Skan	      --idx;
506169689Skan
507132718Skan	      if (array_type_p)
508169689Skan		sub = build4 (ARRAY_REF, inner_type, dest, field_index,
509169689Skan			      NULL_TREE, NULL_TREE);
510132718Skan	      else
511169689Skan		sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
512169689Skan			      NULL_TREE);
513132718Skan
514169689Skan	      code = build2 (INIT_EXPR, inner_type, sub, value);
515132718Skan	      code = build_stmt (EXPR_STMT, code);
516169689Skan	      add_stmt (code);
517132718Skan	      continue;
518132718Skan	    }
519132718Skan	}
520132718Skan      break;
521132718Skan
522132718Skan    case VECTOR_TYPE:
523132718Skan      if (!initializer_constant_valid_p (init, type))
524132718Skan	{
525169689Skan	  tree code;
526146895Skan	  tree cons = copy_node (init);
527132718Skan	  CONSTRUCTOR_ELTS (init) = NULL;
528169689Skan	  code = build2 (MODIFY_EXPR, type, dest, cons);
529132718Skan	  code = build_stmt (EXPR_STMT, code);
530169689Skan	  add_stmt (code);
531132718Skan	}
532132718Skan      break;
533132718Skan
534132718Skan    default:
535169689Skan      gcc_unreachable ();
536132718Skan    }
537132718Skan
538169689Skan  /* The rest of the initializer is now a constant. */
539169689Skan  TREE_CONSTANT (init) = 1;
540132718Skan}
541132718Skan
542169689Skan/* A subroutine of store_init_value.  Splits non-constant static
543132718Skan   initializer INIT into a constant part and generates code to
544132718Skan   perform the non-constant part of the initialization to DEST.
545132718Skan   Returns the code for the runtime init.  */
546132718Skan
547132718Skanstatic tree
548132718Skansplit_nonconstant_init (tree dest, tree init)
549132718Skan{
550132718Skan  tree code;
551132718Skan
552132718Skan  if (TREE_CODE (init) == CONSTRUCTOR)
553132718Skan    {
554169689Skan      code = push_stmt_list ();
555169689Skan      split_nonconstant_init_1 (dest, init);
556169689Skan      code = pop_stmt_list (code);
557132718Skan      DECL_INITIAL (dest) = init;
558132718Skan      TREE_READONLY (dest) = 0;
559132718Skan    }
560132718Skan  else
561169689Skan    code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
562132718Skan
563132718Skan  return code;
564132718Skan}
565132718Skan
56618334Speter/* Perform appropriate conversions on the initial value of a variable,
56718334Speter   store it in the declaration DECL,
56818334Speter   and print any error messages that are appropriate.
56918334Speter   If the init is invalid, store an ERROR_MARK.
57018334Speter
57118334Speter   C++: Note that INIT might be a TREE_LIST, which would mean that it is
57218334Speter   a base class initializer for some aggregate type, hopefully compatible
57318334Speter   with DECL.  If INIT is a single element, and DECL is an aggregate
57418334Speter   type, we silently convert INIT into a TREE_LIST, allowing a constructor
57518334Speter   to be called.
57618334Speter
57718334Speter   If INIT is a TREE_LIST and there is no constructor, turn INIT
57818334Speter   into a CONSTRUCTOR and use standard initialization techniques.
57918334Speter   Perhaps a warning should be generated?
58018334Speter
581132718Skan   Returns code to be executed if initialization could not be performed
582132718Skan   for static variable.  In that case, caller must emit the code.  */
58318334Speter
58418334Spetertree
585132718Skanstore_init_value (tree decl, tree init)
58618334Speter{
587132718Skan  tree value, type;
58818334Speter
58918334Speter  /* If variable's type was invalidly declared, just ignore it.  */
59018334Speter
59118334Speter  type = TREE_TYPE (decl);
59218334Speter  if (TREE_CODE (type) == ERROR_MARK)
59318334Speter    return NULL_TREE;
59418334Speter
59518334Speter  if (IS_AGGR_TYPE (type))
59618334Speter    {
597169689Skan      gcc_assert (TYPE_HAS_TRIVIAL_INIT_REF (type)
598169689Skan		  || TREE_CODE (init) == CONSTRUCTOR);
59918334Speter
60090075Sobrien      if (TREE_CODE (init) == TREE_LIST)
60118334Speter	{
602169689Skan	  error ("constructor syntax used, but no constructor declared "
603169689Skan		 "for type %qT", type);
604169689Skan	  init = build_constructor_from_list (NULL_TREE, nreverse (init));
60518334Speter	}
60618334Speter    }
60718334Speter  else if (TREE_CODE (init) == TREE_LIST
60818334Speter	   && TREE_TYPE (init) != unknown_type_node)
60918334Speter    {
61018334Speter      if (TREE_CODE (decl) == RESULT_DECL)
611132718Skan	init = build_x_compound_expr_from_list (init,
612132718Skan						"return value initializer");
61318334Speter      else if (TREE_CODE (init) == TREE_LIST
61418334Speter	       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
61518334Speter	{
61618334Speter	  error ("cannot initialize arrays using this syntax");
61718334Speter	  return NULL_TREE;
61818334Speter	}
61918334Speter      else
620132718Skan	/* We get here with code like `int a (2);' */
621132718Skan	init = build_x_compound_expr_from_list (init, "initializer");
62218334Speter    }
62318334Speter
624169689Skan  /* End of special C++ code.  */
625169689Skan
626117395Skan  /* Digest the specified initializer into an expression.  */
627169689Skan  value = digest_init (type, init);
628146895Skan  /* If the initializer is not a constant, fill in DECL_INITIAL with
629146895Skan     the bits that are constant, and then return an expression that
630146895Skan     will perform the dynamic initialization.  */
631146895Skan  if (value != error_mark_node
632169689Skan      && (TREE_SIDE_EFFECTS (value)
633169689Skan	   || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
634132718Skan    return split_nonconstant_init (decl, value);
635146895Skan  /* If the value is a constant, just put it in DECL_INITIAL.  If DECL
636146895Skan     is an automatic variable, the middle end will turn this into a
637146895Skan     dynamic initialization later.  */
63818334Speter  DECL_INITIAL (decl) = value;
63918334Speter  return NULL_TREE;
64018334Speter}
641102780Skan
64218334Speter
643169689Skan/* Process the initializer INIT for a variable of type TYPE, emitting
644169689Skan   diagnostics for invalid initializers and converting the initializer as
645169689Skan   appropriate.
64618334Speter
647169689Skan   For aggregate types, it assumes that reshape_init has already run, thus the
648169689Skan   initializer will have the right shape (brace elision has been undone).  */
64918334Speter
65018334Spetertree
651169689Skandigest_init (tree type, tree init)
65218334Speter{
65318334Speter  enum tree_code code = TREE_CODE (type);
65418334Speter
655169689Skan  if (init == error_mark_node)
65618334Speter    return error_mark_node;
65718334Speter
658169689Skan  gcc_assert (init);
65990075Sobrien
66090075Sobrien  /* We must strip the outermost array type when completing the type,
66190075Sobrien     because the its bounds might be incomplete at the moment.  */
66290075Sobrien  if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
66390075Sobrien			      ? TREE_TYPE (type) : type, NULL_TREE))
66490075Sobrien    return error_mark_node;
665169689Skan
666169689Skan  /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
667169689Skan     (g++.old-deja/g++.law/casts2.C).  */
66818334Speter  if (TREE_CODE (init) == NON_LVALUE_EXPR)
66918334Speter    init = TREE_OPERAND (init, 0);
67018334Speter
671169689Skan  /* Initialization of an array of chars from a string constant. The initializer
672169689Skan     can be optionally enclosed in braces, but reshape_init has already removed
673169689Skan     them if they were present.  */
67418334Speter  if (code == ARRAY_TYPE)
67518334Speter    {
676169689Skan      tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
67790075Sobrien      if (char_type_p (typ1)
678169689Skan	  /*&& init */
679169689Skan	  && TREE_CODE (init) == STRING_CST)
68018334Speter	{
681169689Skan	  tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
68218334Speter
683169689Skan	  if (char_type != char_type_node
68418334Speter	      && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
68518334Speter	    {
68618334Speter	      error ("char-array initialized from wide string");
68718334Speter	      return error_mark_node;
68818334Speter	    }
689169689Skan	  if (char_type == char_type_node
69018334Speter	      && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
69118334Speter	    {
69218334Speter	      error ("int-array initialized from non-wide string");
69318334Speter	      return error_mark_node;
69418334Speter	    }
69518334Speter
696169689Skan	  TREE_TYPE (init) = type;
697169689Skan	  if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
69818334Speter	    {
699132718Skan	      int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
70018334Speter	      size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
70118334Speter	      /* In C it is ok to subtract 1 from the length of the string
70218334Speter		 because it's ok to ignore the terminating null char that is
70318334Speter		 counted in the length of the constant, but in C++ this would
70418334Speter		 be invalid.  */
705169689Skan	      if (size < TREE_STRING_LENGTH (init))
70618334Speter		pedwarn ("initializer-string for array of chars is too long");
70718334Speter	    }
708169689Skan	  return init;
70918334Speter	}
71018334Speter    }
71118334Speter
712169689Skan  /* Handle scalar types (including conversions) and references.  */
713171825Skan  if (TREE_CODE (type) != COMPLEX_TYPE
714171825Skan      && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
715169689Skan    return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
716169689Skan				       "initialization", NULL_TREE, 0);
71718334Speter
718169689Skan  /* Come here only for aggregates: records, arrays, unions, complex numbers
719169689Skan     and vectors.  */
720169689Skan  gcc_assert (TREE_CODE (type) == ARRAY_TYPE
721169689Skan	      || TREE_CODE (type) == VECTOR_TYPE
722169689Skan	      || TREE_CODE (type) == RECORD_TYPE
723169689Skan	      || TREE_CODE (type) == UNION_TYPE
724169689Skan	      || TREE_CODE (type) == COMPLEX_TYPE);
725169689Skan
726169689Skan  if (BRACE_ENCLOSED_INITIALIZER_P (init))
727169689Skan      return process_init_constructor (type, init);
728169689Skan  else
72918334Speter    {
730169689Skan      if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
73118334Speter	{
732169689Skan	  error ("cannot initialize aggregate of type %qT with "
733169689Skan		 "a compound literal", type);
734169689Skan
735169689Skan	  return error_mark_node;
73618334Speter	}
737169689Skan
738169689Skan      if (TREE_CODE (type) == ARRAY_TYPE
739169689Skan	  && TREE_CODE (init) != CONSTRUCTOR)
74018334Speter	{
741169689Skan	  error ("array must be initialized with a brace-enclosed"
742169689Skan		 " initializer");
743169689Skan	  return error_mark_node;
74418334Speter	}
74518334Speter
746169689Skan      return convert_for_initialization (NULL_TREE, type, init,
747169689Skan					 LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING,
74818334Speter					 "initialization", NULL_TREE, 0);
74918334Speter    }
750169689Skan}
75118334Speter
752169689Skan
753169689Skan/* Set of flags used within process_init_constructor to describe the
754169689Skan   initializers.  */
755169689Skan#define PICFLAG_ERRONEOUS 1
756169689Skan#define PICFLAG_NOT_ALL_CONSTANT 2
757169689Skan#define PICFLAG_NOT_ALL_SIMPLE 4
75818334Speter
759169689Skan/* Given an initializer INIT, return the flag (PICFLAG_*) which better
760169689Skan   describe it.  */
761169689Skan
762169689Skanstatic int
763169689Skanpicflag_from_initializer (tree init)
764169689Skan{
765169689Skan  if (init == error_mark_node)
766169689Skan    return PICFLAG_ERRONEOUS;
767169689Skan  else if (!TREE_CONSTANT (init))
768169689Skan    return PICFLAG_NOT_ALL_CONSTANT;
769169689Skan  else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
770169689Skan    return PICFLAG_NOT_ALL_SIMPLE;
771169689Skan  return 0;
772169689Skan}
773169689Skan
774169689Skan/* Subroutine of process_init_constructor, which will process an initializer
775169689Skan   INIT for a array or vector of type TYPE. Returns the flags (PICFLAG_*) which
776169689Skan   describe the initializers.  */
777169689Skan
778169689Skanstatic int
779169689Skanprocess_init_constructor_array (tree type, tree init)
780169689Skan{
781169689Skan  unsigned HOST_WIDE_INT i, len = 0;
782169689Skan  int flags = 0;
783169689Skan  bool unbounded = false;
784169689Skan  constructor_elt *ce;
785169689Skan  VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (init);
786169689Skan
787169689Skan  gcc_assert (TREE_CODE (type) == ARRAY_TYPE
788169689Skan	      || TREE_CODE (type) == VECTOR_TYPE);
789169689Skan
790169689Skan  if (TREE_CODE (type) == ARRAY_TYPE)
79118334Speter    {
792169689Skan      tree domain = TYPE_DOMAIN (type);
793169689Skan      if (domain)
794169689Skan	len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
795169689Skan	      - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
796169689Skan	      + 1);
797169689Skan      else
798169689Skan	unbounded = true;  /* Take as many as there are.  */
79918334Speter    }
800169689Skan  else
801169689Skan    /* Vectors are like simple fixed-size arrays.  */
802169689Skan    len = TYPE_VECTOR_SUBPARTS (type);
80318334Speter
804169689Skan  /* There cannot be more initializers than needed as otherwise
805169689Skan     reshape_init would have already rejected the initializer.  */
806169689Skan  if (!unbounded)
807169689Skan    gcc_assert (VEC_length (constructor_elt, v) <= len);
808169689Skan
809169689Skan  for (i = 0; VEC_iterate (constructor_elt, v, i, ce); ++i)
81018334Speter    {
811169689Skan      if (ce->index)
81218334Speter	{
813169689Skan	  gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
814169689Skan	  if (compare_tree_int (ce->index, i) != 0)
815169689Skan	    {
816169689Skan	      ce->value = error_mark_node;
817169689Skan	      sorry ("non-trivial designated initializers not supported");
818169689Skan	    }
81918334Speter	}
820169689Skan      else
821169689Skan	ce->index = size_int (i);
822169689Skan      gcc_assert (ce->value);
823169689Skan      ce->value = digest_init (TREE_TYPE (type), ce->value);
82450397Sobrien
825169689Skan      if (ce->value != error_mark_node)
826169689Skan	gcc_assert (same_type_ignoring_top_level_qualifiers_p
827169689Skan		      (TREE_TYPE (type), TREE_TYPE (ce->value)));
82850397Sobrien
829169689Skan      flags |= picflag_from_initializer (ce->value);
83018334Speter    }
83118334Speter
832169689Skan  /* No more initializers. If the array is unbounded, we are done. Otherwise,
833169689Skan     we must add initializers ourselves.  */
834169689Skan  if (!unbounded)
835169689Skan    for (; i < len; ++i)
836169689Skan      {
837169689Skan	tree next;
83818334Speter
839169689Skan	if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
840169689Skan	  {
841169689Skan	    /* If this type needs constructors run for default-initialization,
842169689Skan	      we can't rely on the backend to do it for us, so build up
843169689Skan	      TARGET_EXPRs.  If the type in question is a class, just build
844169689Skan	      one up; if it's an array, recurse.  */
845169689Skan	    if (IS_AGGR_TYPE (TREE_TYPE (type)))
846169689Skan		next = build_functional_cast (TREE_TYPE (type), NULL_TREE);
847169689Skan	    else
848169689Skan		next = build_constructor (NULL_TREE, NULL);
849169689Skan	    next = digest_init (TREE_TYPE (type), next);
850169689Skan	  }
851169689Skan	else if (!zero_init_p (TREE_TYPE (type)))
852169689Skan	  next = build_zero_init (TREE_TYPE (type),
853169689Skan				  /*nelts=*/NULL_TREE,
854169689Skan				  /*static_storage_p=*/false);
855169689Skan	else
856169689Skan	  /* The default zero-initialization is fine for us; don't
857169689Skan	     add anything to the CONSTRUCTOR.  */
858169689Skan	  break;
85918334Speter
860169689Skan	flags |= picflag_from_initializer (next);
861169689Skan	CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
862169689Skan      }
86318334Speter
864169689Skan  CONSTRUCTOR_ELTS (init) = v;
865169689Skan  return flags;
866169689Skan}
86718334Speter
868169689Skan/* Subroutine of process_init_constructor, which will process an initializer
869169689Skan   INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
870169689Skan   the initializers.  */
871169689Skan
872169689Skanstatic int
873169689Skanprocess_init_constructor_record (tree type, tree init)
87418334Speter{
875169689Skan  VEC(constructor_elt,gc) *v = NULL;
876169689Skan  int flags = 0;
877169689Skan  tree field;
878169689Skan  unsigned HOST_WIDE_INT idx = 0;
87918334Speter
880169689Skan  gcc_assert (TREE_CODE (type) == RECORD_TYPE);
881169689Skan  gcc_assert (!CLASSTYPE_VBASECLASSES (type));
882169689Skan  gcc_assert (!TYPE_BINFO (type)
883169689Skan	      || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
884169689Skan  gcc_assert (!TYPE_POLYMORPHIC_P (type));
88518334Speter
886169689Skan  /* Generally, we will always have an index for each initializer (which is
887169689Skan     a FIELD_DECL, put by reshape_init), but compound literals don't go trough
888169689Skan     reshape_init. So we need to handle both cases.  */
889169689Skan  for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
89018334Speter    {
891169689Skan      tree next;
89218334Speter
893169689Skan      if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
89496263Sobrien	{
895169689Skan	  flags |= picflag_from_initializer (integer_zero_node);
896169689Skan	  CONSTRUCTOR_APPEND_ELT (v, field, integer_zero_node);
897169689Skan	  continue;
89896263Sobrien	}
89918334Speter
900169689Skan      if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
901169689Skan	continue;
902169689Skan
903169689Skan      if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)))
90418334Speter	{
905169689Skan	  constructor_elt *ce = VEC_index (constructor_elt,
906169689Skan					   CONSTRUCTOR_ELTS (init), idx);
907169689Skan	  if (ce->index)
90852284Sobrien	    {
909169689Skan	      /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
910169689Skan		 latter case can happen in templates where lookup has to be
911169689Skan		 deferred.  */
912169689Skan	      gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
913169689Skan			  || TREE_CODE (ce->index) == IDENTIFIER_NODE);
914169689Skan	      if (ce->index != field
915169689Skan		  && ce->index != DECL_NAME (field))
91618334Speter		{
917169689Skan		  ce->value = error_mark_node;
918169689Skan		  sorry ("non-trivial designated initializers not supported");
91918334Speter		}
92018334Speter	    }
92152284Sobrien
922169689Skan	  gcc_assert (ce->value);
923169689Skan	  next = digest_init (TREE_TYPE (field), ce->value);
924169689Skan	  ++idx;
925169689Skan	}
926169689Skan      else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
927169689Skan	{
928169689Skan	  /* If this type needs constructors run for
929169689Skan	     default-initialization, we can't rely on the backend to do it
930169689Skan	     for us, so build up TARGET_EXPRs.  If the type in question is
931169689Skan	     a class, just build one up; if it's an array, recurse.  */
932169689Skan	  if (IS_AGGR_TYPE (TREE_TYPE (field)))
933169689Skan	    next = build_functional_cast (TREE_TYPE (field), NULL_TREE);
93452284Sobrien	  else
935169689Skan	    next = build_constructor (NULL_TREE, NULL);
93618334Speter
937169689Skan	  next = digest_init (TREE_TYPE (field), next);
938169689Skan
939169689Skan	  /* Warn when some struct elements are implicitly initialized.  */
940169689Skan	  warning (OPT_Wmissing_field_initializers,
941169689Skan		   "missing initializer for member %qD", field);
94218334Speter	}
943169689Skan      else
94418334Speter	{
945169689Skan	  if (TREE_READONLY (field))
946169689Skan	    error ("uninitialized const member %qD", field);
947169689Skan	  else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
948169689Skan	    error ("member %qD with uninitialized const fields", field);
949169689Skan	  else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
950169689Skan	    error ("member %qD is uninitialized reference", field);
95118334Speter
952169689Skan	  /* Warn when some struct elements are implicitly initialized
953169689Skan	     to zero.  */
954169689Skan	  warning (OPT_Wmissing_field_initializers,
955169689Skan		   "missing initializer for member %qD", field);
95618334Speter
957169689Skan	  if (!zero_init_p (TREE_TYPE (field)))
958169689Skan	    next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
959169689Skan				    /*static_storage_p=*/false);
960169689Skan	  else
961169689Skan	    /* The default zero-initialization is fine for us; don't
962169689Skan	    add anything to the CONSTRUCTOR.  */
963169689Skan	    continue;
96418334Speter	}
96518334Speter
966169689Skan      flags |= picflag_from_initializer (next);
967169689Skan      CONSTRUCTOR_APPEND_ELT (v, field, next);
968169689Skan    }
96918334Speter
970169689Skan  CONSTRUCTOR_ELTS (init) = v;
971169689Skan  return flags;
972169689Skan}
97318334Speter
974169689Skan/* Subroutine of process_init_constructor, which will process a single
975169689Skan   initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
976169689Skan   which describe the initializer.  */
97750397Sobrien
978169689Skanstatic int
979169689Skanprocess_init_constructor_union (tree type, tree init)
980169689Skan{
981169689Skan  constructor_elt *ce;
98252284Sobrien
983169689Skan  /* If the initializer was empty, use default zero initialization.  */
984169689Skan  if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
985169689Skan    return 0;
98618334Speter
987169689Skan  gcc_assert (VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) == 1);
988169689Skan  ce = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (init), 0);
98952284Sobrien
990169689Skan  /* If this element specifies a field, initialize via that field.  */
991169689Skan  if (ce->index)
992169689Skan    {
993169689Skan      if (TREE_CODE (ce->index) == FIELD_DECL)
994169689Skan	;
995169689Skan      else if (TREE_CODE (ce->index) == IDENTIFIER_NODE)
996169689Skan	{
997169689Skan	  /* This can happen within a cast, see g++.dg/opt/cse2.C.  */
998169689Skan	  tree name = ce->index;
999169689Skan	  tree field;
1000169689Skan	  for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1001169689Skan	    if (DECL_NAME (field) == name)
1002169689Skan	      break;
1003169689Skan	  if (!field)
100418334Speter	    {
1005169689Skan	      error ("no field %qD found in union being initialized", field);
1006169689Skan	      ce->value = error_mark_node;
100718334Speter	    }
1008169689Skan	  ce->index = field;
100918334Speter	}
1010169689Skan      else
1011169689Skan	{
1012169689Skan	  gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1013169689Skan		      || TREE_CODE (ce->index) == RANGE_EXPR);
1014169689Skan	  error ("index value instead of field name in union initializer");
1015169689Skan	  ce->value = error_mark_node;
1016169689Skan	}
101718334Speter    }
1018169689Skan  else
101918334Speter    {
102018334Speter      /* Find the first named field.  ANSI decided in September 1990
102118334Speter	 that only named fields count here.  */
1022169689Skan      tree field = TYPE_FIELDS (type);
1023117395Skan      while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
102418334Speter	field = TREE_CHAIN (field);
1025169689Skan      gcc_assert (field);
1026169689Skan      ce->index = field;
1027169689Skan    }
102818334Speter
1029169689Skan  if (ce->value && ce->value != error_mark_node)
1030169689Skan    ce->value = digest_init (TREE_TYPE (ce->index), ce->value);
103118334Speter
1032169689Skan  return picflag_from_initializer (ce->value);
1033169689Skan}
103418334Speter
1035169689Skan/* Process INIT, a constructor for a variable of aggregate type TYPE. The
1036169689Skan   constructor is a brace-enclosed initializer, and will be modified in-place.
103718334Speter
1038169689Skan   Each element is converted to the right type through digest_init, and
1039169689Skan   missing initializers are added following the language rules (zero-padding,
1040169689Skan   etc.).
104118334Speter
1042169689Skan   After the execution, the initializer will have TREE_CONSTANT if all elts are
1043169689Skan   constant, and TREE_STATIC set if, in addition, all elts are simple enough
1044169689Skan   constants that the assembler and linker can compute them.
104518334Speter
1046169689Skan   The function returns the initializer itself, or error_mark_node in case
1047169689Skan   of error.  */
104818334Speter
1049169689Skanstatic tree
1050169689Skanprocess_init_constructor (tree type, tree init)
1051169689Skan{
1052169689Skan  int flags;
1053169689Skan
1054169689Skan  gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1055169689Skan
1056169689Skan  if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1057169689Skan    flags = process_init_constructor_array (type, init);
1058169689Skan  else if (TREE_CODE (type) == RECORD_TYPE)
1059169689Skan    flags = process_init_constructor_record (type, init);
1060169689Skan  else if (TREE_CODE (type) == UNION_TYPE)
1061169689Skan    flags = process_init_constructor_union (type, init);
1062169689Skan  else
1063169689Skan    gcc_unreachable ();
1064169689Skan
1065169689Skan  if (flags & PICFLAG_ERRONEOUS)
106618334Speter    return error_mark_node;
106718334Speter
1068169689Skan  TREE_TYPE (init) = type;
1069107590Sobrien  if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1070169689Skan    cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1071169689Skan  if (!(flags & PICFLAG_NOT_ALL_CONSTANT))
1072169689Skan    {
1073169689Skan      TREE_CONSTANT (init) = 1;
1074169689Skan      TREE_INVARIANT (init) = 1;
1075169689Skan      if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1076169689Skan	TREE_STATIC (init) = 1;
1077169689Skan    }
1078169689Skan  return init;
107918334Speter}
108018334Speter
108118334Speter/* Given a structure or union value DATUM, construct and return
108218334Speter   the structure or union component which results from narrowing
1083117395Skan   that value to the base specified in BASETYPE.  For example, given the
108418334Speter   hierarchy
108518334Speter
108618334Speter   class L { int ii; };
108718334Speter   class A : L { ... };
108818334Speter   class B : L { ... };
108918334Speter   class C : A, B { ... };
109018334Speter
109118334Speter   and the declaration
109218334Speter
109318334Speter   C x;
109418334Speter
109518334Speter   then the expression
109618334Speter
109750397Sobrien   x.A::ii refers to the ii member of the L part of
109850397Sobrien   the A part of the C object named by X.  In this case,
109990075Sobrien   DATUM would be x, and BASETYPE would be A.
110018334Speter
110190075Sobrien   I used to think that this was nonconformant, that the standard specified
110290075Sobrien   that first we look up ii in A, then convert x to an L& and pull out the
110390075Sobrien   ii part.  But in fact, it does say that we convert x to an A&; A here
1104117395Skan   is known as the "naming class".  (jason 2000-12-19)
110590075Sobrien
1106117395Skan   BINFO_P points to a variable initialized either to NULL_TREE or to the
1107117395Skan   binfo for the specific base subobject we want to convert to.  */
1108117395Skan
110918334Spetertree
1110132718Skanbuild_scoped_ref (tree datum, tree basetype, tree* binfo_p)
111118334Speter{
111290075Sobrien  tree binfo;
111318334Speter
111418334Speter  if (datum == error_mark_node)
111518334Speter    return error_mark_node;
1116117395Skan  if (*binfo_p)
1117117395Skan    binfo = *binfo_p;
1118117395Skan  else
1119117395Skan    binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
112018334Speter
1121117395Skan  if (!binfo || binfo == error_mark_node)
1122117395Skan    {
1123117395Skan      *binfo_p = NULL_TREE;
1124117395Skan      if (!binfo)
1125117395Skan	error_not_base_type (basetype, TREE_TYPE (datum));
1126117395Skan      return error_mark_node;
1127117395Skan    }
112818334Speter
1129117395Skan  *binfo_p = binfo;
1130117395Skan  return build_base_path (PLUS_EXPR, datum, binfo, 1);
113118334Speter}
113218334Speter
113318334Speter/* Build a reference to an object specified by the C++ `->' operator.
113418334Speter   Usually this just involves dereferencing the object, but if the
113518334Speter   `->' operator is overloaded, then such overloads must be
113618334Speter   performed until an object which does not have the `->' operator
113718334Speter   overloaded is found.  An error is reported when circular pointer
113818334Speter   delegation is detected.  */
113950397Sobrien
114018334Spetertree
1141132718Skanbuild_x_arrow (tree expr)
114218334Speter{
1143132718Skan  tree orig_expr = expr;
114418334Speter  tree types_memoized = NULL_TREE;
1145132718Skan  tree type = TREE_TYPE (expr);
114650397Sobrien  tree last_rval = NULL_TREE;
114718334Speter
114818334Speter  if (type == error_mark_node)
114918334Speter    return error_mark_node;
115018334Speter
115150397Sobrien  if (processing_template_decl)
115218334Speter    {
1153132718Skan      if (type_dependent_expression_p (expr))
1154132718Skan	return build_min_nt (ARROW_EXPR, expr);
1155132718Skan      expr = build_non_dependent_expr (expr);
115618334Speter    }
115718334Speter
115850397Sobrien  if (IS_AGGR_TYPE (type))
115918334Speter    {
1160132718Skan      while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr,
1161132718Skan				   NULL_TREE, NULL_TREE,
1162132718Skan				   /*overloaded_p=*/NULL)))
116318334Speter	{
1164132718Skan	  if (expr == error_mark_node)
116518334Speter	    return error_mark_node;
116618334Speter
1167132718Skan	  if (value_member (TREE_TYPE (expr), types_memoized))
116818334Speter	    {
116918334Speter	      error ("circular pointer delegation detected");
117018334Speter	      return error_mark_node;
117118334Speter	    }
117218334Speter	  else
117318334Speter	    {
1174132718Skan	      types_memoized = tree_cons (NULL_TREE, TREE_TYPE (expr),
117518334Speter					  types_memoized);
117618334Speter	    }
1177132718Skan	  last_rval = expr;
1178169689Skan	}
117950397Sobrien
118050397Sobrien      if (last_rval == NULL_TREE)
118150397Sobrien	{
1182169689Skan	  error ("base operand of %<->%> has non-pointer type %qT", type);
118350397Sobrien	  return error_mark_node;
118450397Sobrien	}
118550397Sobrien
118618334Speter      if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
118718334Speter	last_rval = convert_from_reference (last_rval);
118818334Speter    }
118918334Speter  else
1190132718Skan    last_rval = decay_conversion (expr);
119118334Speter
119218334Speter  if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1193132718Skan    {
1194132718Skan      if (processing_template_decl)
1195132718Skan	{
1196132718Skan	  expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr);
1197132718Skan	  /* It will be dereferenced.  */
1198132718Skan	  TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval));
1199132718Skan	  return expr;
1200132718Skan	}
120118334Speter
1202132718Skan      return build_indirect_ref (last_rval, NULL);
1203132718Skan    }
1204132718Skan
120518334Speter  if (types_memoized)
1206169689Skan    error ("result of %<operator->()%> yields non-pointer result");
120718334Speter  else
1208169689Skan    error ("base operand of %<->%> is not a pointer");
120918334Speter  return error_mark_node;
121018334Speter}
121118334Speter
1212132718Skan/* Return an expression for "DATUM .* COMPONENT".  DATUM has not
1213132718Skan   already been checked out to be of aggregate type.  */
121418334Speter
121518334Spetertree
1216132718Skanbuild_m_component_ref (tree datum, tree component)
121718334Speter{
1218132718Skan  tree ptrmem_type;
1219132718Skan  tree objtype;
122018334Speter  tree type;
122118334Speter  tree binfo;
1222169689Skan  tree ctype;
122318334Speter
1224169689Skan  if (error_operand_p (datum) || error_operand_p (component))
122590075Sobrien    return error_mark_node;
122690075Sobrien
1227132718Skan  ptrmem_type = TREE_TYPE (component);
1228132718Skan  if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type))
122918334Speter    {
1230169689Skan      error ("%qE cannot be used as a member pointer, since it is of "
1231169689Skan	     "type %qT",
1232132718Skan	     component, ptrmem_type);
123318334Speter      return error_mark_node;
123418334Speter    }
1235169689Skan
1236169689Skan  objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
123718334Speter  if (! IS_AGGR_TYPE (objtype))
123818334Speter    {
1239169689Skan      error ("cannot apply member pointer %qE to %qE, which is of "
1240169689Skan	     "non-class type %qT",
1241169689Skan	     component, datum, objtype);
124218334Speter      return error_mark_node;
124318334Speter    }
124418334Speter
1245132718Skan  type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1246169689Skan  ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1247169689Skan
1248169689Skan  if (!COMPLETE_TYPE_P (ctype))
124918334Speter    {
1250169689Skan      if (!same_type_p (ctype, objtype))
1251169689Skan	goto mismatch;
1252169689Skan      binfo = NULL;
125318334Speter    }
1254169689Skan  else
1255169689Skan    {
1256169689Skan      binfo = lookup_base (objtype, ctype, ba_check, NULL);
125718334Speter
1258169689Skan      if (!binfo)
1259169689Skan	{
1260169689Skan	mismatch:
1261169689Skan	  error ("pointer to member type %qT incompatible with object "
1262169689Skan		 "type %qT",
1263169689Skan		 type, objtype);
1264169689Skan	  return error_mark_node;
1265169689Skan	}
1266169689Skan      else if (binfo == error_mark_node)
1267169689Skan	return error_mark_node;
1268169689Skan    }
1269169689Skan
1270132718Skan  if (TYPE_PTRMEM_P (ptrmem_type))
1271132718Skan    {
1272132718Skan      /* Compute the type of the field, as described in [expr.ref].
1273132718Skan	 There's no such thing as a mutable pointer-to-member, so
1274132718Skan	 things are not as complex as they are for references to
1275132718Skan	 non-static data members.  */
1276132718Skan      type = cp_build_qualified_type (type,
1277169689Skan				      (cp_type_quals (type)
1278132718Skan				       | cp_type_quals (TREE_TYPE (datum))));
1279169689Skan
1280169689Skan      datum = build_address (datum);
1281169689Skan
1282169689Skan      /* Convert object to the correct base.  */
1283169689Skan      if (binfo)
1284169689Skan	datum = build_base_path (PLUS_EXPR, datum, binfo, 1);
1285169689Skan
1286132718Skan      /* Build an expression for "object + offset" where offset is the
1287132718Skan	 value stored in the pointer-to-data-member.  */
1288169689Skan      datum = build2 (PLUS_EXPR, build_pointer_type (type),
1289169689Skan		      datum, build_nop (ptrdiff_type_node, component));
1290132718Skan      return build_indirect_ref (datum, 0);
1291132718Skan    }
1292132718Skan  else
1293169689Skan    return build2 (OFFSET_REF, type, datum, component);
129418334Speter}
129518334Speter
129650397Sobrien/* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
129718334Speter
129818334Spetertree
1299132718Skanbuild_functional_cast (tree exp, tree parms)
130018334Speter{
130118334Speter  /* This is either a call to a constructor,
130218334Speter     or a C cast in C++'s `functional' notation.  */
130350397Sobrien  tree type;
130418334Speter
130518334Speter  if (exp == error_mark_node || parms == error_mark_node)
130618334Speter    return error_mark_node;
130718334Speter
1308132718Skan  if (TREE_CODE (exp) == TYPE_DECL)
130950397Sobrien    type = TREE_TYPE (exp);
131018334Speter  else
131118334Speter    type = exp;
131218334Speter
131350397Sobrien  if (processing_template_decl)
1314132718Skan    {
1315132718Skan      tree t = build_min (CAST_EXPR, type, parms);
1316132718Skan      /* We don't know if it will or will not have side effects.  */
1317132718Skan      TREE_SIDE_EFFECTS (t) = 1;
1318132718Skan      return t;
1319132718Skan    }
132050397Sobrien
132118334Speter  if (! IS_AGGR_TYPE (type))
132218334Speter    {
132318334Speter      if (parms == NULL_TREE)
1324169689Skan	return cp_convert (type, integer_zero_node);
132518334Speter
1326169689Skan      /* This must build a C cast.  */
1327169689Skan      parms = build_x_compound_expr_from_list (parms, "functional cast");
132850397Sobrien      return build_c_cast (type, parms);
132918334Speter    }
133018334Speter
133150397Sobrien  /* Prepare to evaluate as a call to a constructor.  If this expression
133250397Sobrien     is actually used, for example,
1333169689Skan
133450397Sobrien     return X (arg1, arg2, ...);
1335169689Skan
133650397Sobrien     then the slot being initialized will be filled in.  */
133750397Sobrien
133890075Sobrien  if (!complete_type_or_else (type, NULL_TREE))
133990075Sobrien    return error_mark_node;
134090075Sobrien  if (abstract_virtuals_error (NULL_TREE, type))
134190075Sobrien    return error_mark_node;
134218334Speter
134318334Speter  if (parms && TREE_CHAIN (parms) == NULL_TREE)
134450397Sobrien    return build_c_cast (type, TREE_VALUE (parms));
134518334Speter
1346169689Skan  /* We need to zero-initialize POD types.  */
1347169689Skan  if (parms == NULL_TREE
1348169689Skan      && !CLASSTYPE_NON_POD_P (type)
134950397Sobrien      && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
135050397Sobrien    {
1351171825Skan      exp = build_zero_init (type,
1352171825Skan			     /*nelts=*/NULL_TREE,
1353171825Skan			     /*static_storage_p=*/false);
135450397Sobrien      return get_target_expr (exp);
135550397Sobrien    }
135618334Speter
1357117395Skan  exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms,
1358169689Skan				   type, LOOKUP_NORMAL);
135950397Sobrien
136050397Sobrien  if (exp == error_mark_node)
136118334Speter    return error_mark_node;
136218334Speter
136350397Sobrien  return build_cplus_new (type, exp);
136418334Speter}
136518334Speter
136650397Sobrien
136790075Sobrien/* Add new exception specifier SPEC, to the LIST we currently have.
136890075Sobrien   If it's already in LIST then do nothing.
136990075Sobrien   Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
137090075Sobrien   know what we're doing.  */
137150397Sobrien
137290075Sobrientree
1373132718Skanadd_exception_specifier (tree list, tree spec, int complain)
137418334Speter{
1375132718Skan  bool ok;
137690075Sobrien  tree core = spec;
1377132718Skan  bool is_ptr;
1378117395Skan  int diag_type = -1; /* none */
1379169689Skan
138090075Sobrien  if (spec == error_mark_node)
138190075Sobrien    return list;
1382169689Skan
1383169689Skan  gcc_assert (spec && (!list || TREE_VALUE (list)));
1384169689Skan
138590075Sobrien  /* [except.spec] 1, type in an exception specifier shall not be
138690075Sobrien     incomplete, or pointer or ref to incomplete other than pointer
138790075Sobrien     to cv void.  */
138890075Sobrien  is_ptr = TREE_CODE (core) == POINTER_TYPE;
138990075Sobrien  if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
139090075Sobrien    core = TREE_TYPE (core);
139190075Sobrien  if (complain < 0)
1392132718Skan    ok = true;
139390075Sobrien  else if (VOID_TYPE_P (core))
139490075Sobrien    ok = is_ptr;
139590075Sobrien  else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1396132718Skan    ok = true;
139790075Sobrien  else if (processing_template_decl)
1398132718Skan    ok = true;
139990075Sobrien  else
1400117395Skan    {
1401132718Skan      ok = true;
1402117395Skan      /* 15.4/1 says that types in an exception specifier must be complete,
1403169689Skan	 but it seems more reasonable to only require this on definitions
1404169689Skan	 and calls.  So just give a pedwarn at this point; we will give an
1405169689Skan	 error later if we hit one of those two cases.  */
1406117395Skan      if (!COMPLETE_TYPE_P (complete_type (core)))
1407117395Skan	diag_type = 2; /* pedwarn */
1408117395Skan    }
140990075Sobrien
141090075Sobrien  if (ok)
141118334Speter    {
141290075Sobrien      tree probe;
1413169689Skan
141490075Sobrien      for (probe = list; probe; probe = TREE_CHAIN (probe))
1415169689Skan	if (same_type_p (TREE_VALUE (probe), spec))
1416169689Skan	  break;
141790075Sobrien      if (!probe)
1418117395Skan	list = tree_cons (NULL_TREE, spec, list);
141918334Speter    }
1420117395Skan  else
1421117395Skan    diag_type = 0; /* error */
1422169689Skan
1423117395Skan  if (diag_type >= 0 && complain)
1424117395Skan    cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1425117395Skan
142690075Sobrien  return list;
142718334Speter}
142850397Sobrien
142990075Sobrien/* Combine the two exceptions specifier lists LIST and ADD, and return
1430117395Skan   their union.  */
143152284Sobrien
143290075Sobrientree
1433132718Skanmerge_exception_specifiers (tree list, tree add)
143450397Sobrien{
143590075Sobrien  if (!list || !add)
143690075Sobrien    return NULL_TREE;
143790075Sobrien  else if (!TREE_VALUE (list))
143890075Sobrien    return add;
143990075Sobrien  else if (!TREE_VALUE (add))
144090075Sobrien    return list;
144190075Sobrien  else
144290075Sobrien    {
144390075Sobrien      tree orig_list = list;
1444169689Skan
144590075Sobrien      for (; add; add = TREE_CHAIN (add))
1446169689Skan	{
1447169689Skan	  tree spec = TREE_VALUE (add);
1448169689Skan	  tree probe;
1449169689Skan
1450169689Skan	  for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1451169689Skan	    if (same_type_p (TREE_VALUE (probe), spec))
1452169689Skan	      break;
1453169689Skan	  if (!probe)
1454169689Skan	    {
1455169689Skan	      spec = build_tree_list (NULL_TREE, spec);
1456169689Skan	      TREE_CHAIN (spec) = list;
1457169689Skan	      list = spec;
1458169689Skan	    }
1459169689Skan	}
146090075Sobrien    }
146190075Sobrien  return list;
146250397Sobrien}
1463117395Skan
1464117395Skan/* Subroutine of build_call.  Ensure that each of the types in the
1465117395Skan   exception specification is complete.  Technically, 15.4/1 says that
1466117395Skan   they need to be complete when we see a declaration of the function,
1467117395Skan   but we should be able to get away with only requiring this when the
1468117395Skan   function is defined or called.  See also add_exception_specifier.  */
1469117395Skan
1470117395Skanvoid
1471132718Skanrequire_complete_eh_spec_types (tree fntype, tree decl)
1472117395Skan{
1473117395Skan  tree raises;
1474117395Skan  /* Don't complain about calls to op new.  */
1475117395Skan  if (decl && DECL_ARTIFICIAL (decl))
1476117395Skan    return;
1477117395Skan  for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1478117395Skan       raises = TREE_CHAIN (raises))
1479117395Skan    {
1480117395Skan      tree type = TREE_VALUE (raises);
1481117395Skan      if (type && !COMPLETE_TYPE_P (type))
1482117395Skan	{
1483117395Skan	  if (decl)
1484117395Skan	    error
1485169689Skan	      ("call to function %qD which throws incomplete type %q#T",
1486117395Skan	       decl, type);
1487117395Skan	  else
1488169689Skan	    error ("call to function which throws incomplete type %q#T",
1489117395Skan		   decl);
1490117395Skan	}
1491117395Skan    }
1492117395Skan}
1493169689Skan
1494169689Skan
1495169689Skan#include "gt-cp-typeck2.h"
1496