typeck2.c revision 117395
1/* Report error messages, build initializers, and perform
2   some front-end optimizations for C++ compiler.
3   Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4   1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5   Hacked by Michael Tiemann (tiemann@cygnus.com)
6
7This file is part of GNU CC.
8
9GNU CC is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2, or (at your option)
12any later version.
13
14GNU CC is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GNU CC; see the file COPYING.  If not, write to
21the Free Software Foundation, 59 Temple Place - Suite 330,
22Boston, MA 02111-1307, USA.  */
23
24
25/* This file is part of the C++ front end.
26   It contains routines to build C++ expressions given their operands,
27   including computing the types of the result, C and C++ specific error
28   checks, and some optimization.
29
30   There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
31   and to process initializations in declarations (since they work
32   like a strange sort of assignment).  */
33
34#include "config.h"
35#include "system.h"
36#include "tree.h"
37#include "cp-tree.h"
38#include "flags.h"
39#include "toplev.h"
40#include "output.h"
41#include "diagnostic.h"
42
43static tree process_init_constructor PARAMS ((tree, tree, tree *));
44
45/* Print an error message stemming from an attempt to use
46   BASETYPE as a base class for TYPE.  */
47
48tree
49error_not_base_type (basetype, type)
50     tree basetype, type;
51{
52  if (TREE_CODE (basetype) == FUNCTION_DECL)
53    basetype = DECL_CONTEXT (basetype);
54  error ("type `%T' is not a base type for type `%T'", basetype, type);
55  return error_mark_node;
56}
57
58tree
59binfo_or_else (base, type)
60     tree base, type;
61{
62  tree binfo = lookup_base (type, base, ba_ignore, NULL);
63
64  if (binfo == error_mark_node)
65    return NULL_TREE;
66  else if (!binfo)
67    error_not_base_type (base, type);
68  return binfo;
69}
70
71/* According to ARM $7.1.6, "A `const' object may be initialized, but its
72   value may not be changed thereafter.  Thus, we emit hard errors for these,
73   rather than just pedwarns.  If `SOFT' is 1, then we just pedwarn.  (For
74   example, conversions to references.)  */
75
76void
77readonly_error (arg, string, soft)
78     tree arg;
79     const char *string;
80     int soft;
81{
82  const char *fmt;
83  void (*fn) PARAMS ((const char *, ...));
84
85  if (soft)
86    fn = pedwarn;
87  else
88    fn = error;
89
90  if (TREE_CODE (arg) == COMPONENT_REF)
91    {
92      if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
93        fmt = "%s of data-member `%D' in read-only structure";
94      else
95        fmt = "%s of read-only data-member `%D'";
96      (*fn) (fmt, string, TREE_OPERAND (arg, 1));
97    }
98  else if (TREE_CODE (arg) == VAR_DECL)
99    {
100      if (DECL_LANG_SPECIFIC (arg)
101	  && DECL_IN_AGGR_P (arg)
102	  && !TREE_STATIC (arg))
103	fmt = "%s of constant field `%D'";
104      else
105	fmt = "%s of read-only variable `%D'";
106      (*fn) (fmt, string, arg);
107    }
108  else if (TREE_CODE (arg) == PARM_DECL)
109    (*fn) ("%s of read-only parameter `%D'", string, arg);
110  else if (TREE_CODE (arg) == INDIRECT_REF
111           && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
112           && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
113               || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
114    (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0));
115  else if (TREE_CODE (arg) == RESULT_DECL)
116    (*fn) ("%s of read-only named return value `%D'", string, arg);
117  else if (TREE_CODE (arg) == FUNCTION_DECL)
118    (*fn) ("%s of function `%D'", string, arg);
119  else
120    (*fn) ("%s of read-only location", string);
121}
122
123/* If TYPE has abstract virtual functions, issue an error about trying
124   to create an object of that type.  DECL is the object declared, or
125   NULL_TREE if the declaration is unavailable.  Returns 1 if an error
126   occurred; zero if all was well.  */
127
128int
129abstract_virtuals_error (decl, type)
130     tree decl;
131     tree type;
132{
133  tree u;
134  tree tu;
135
136  if (processing_template_decl)
137    /* If we are processing a template, TYPE may be a template
138       class where CLASSTYPE_PURE_VIRTUALS always contains
139       inline friends.  */
140    return 0;
141
142  if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type))
143    return 0;
144
145  if (!TYPE_SIZE (type))
146    /* TYPE is being defined, and during that time
147       CLASSTYPE_PURE_VIRTUALS holds the inline friends.  */
148    return 0;
149
150  u = CLASSTYPE_PURE_VIRTUALS (type);
151  if (decl)
152    {
153      if (TREE_CODE (decl) == RESULT_DECL)
154	return 0;
155
156      if (TREE_CODE (decl) == VAR_DECL)
157	error ("cannot declare variable `%D' to be of type `%T'",
158		    decl, type);
159      else if (TREE_CODE (decl) == PARM_DECL)
160	error ("cannot declare parameter `%D' to be of type `%T'",
161		    decl, type);
162      else if (TREE_CODE (decl) == FIELD_DECL)
163	error ("cannot declare field `%D' to be of type `%T'",
164		    decl, type);
165      else if (TREE_CODE (decl) == FUNCTION_DECL
166	       && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
167	error ("invalid return type for member function `%#D'", decl);
168      else if (TREE_CODE (decl) == FUNCTION_DECL)
169	error ("invalid return type for function `%#D'", decl);
170    }
171  else
172    error ("cannot allocate an object of type `%T'", type);
173
174  /* Only go through this once.  */
175  if (TREE_PURPOSE (u) == NULL_TREE)
176    {
177      TREE_PURPOSE (u) = error_mark_node;
178
179      error ("  because the following virtual functions are abstract:");
180      for (tu = u; tu; tu = TREE_CHAIN (tu))
181	cp_error_at ("\t%#D", TREE_VALUE (tu));
182    }
183  else
184    error ("  since type `%T' has abstract virtual functions", type);
185
186  return 1;
187}
188
189/* Print an error message for invalid use of an incomplete type.
190   VALUE is the expression that was used (or 0 if that isn't known)
191   and TYPE is the type that was invalid.  DIAG_TYPE indicates the
192   type of diagnostic:  0 for an error, 1 for a warning, 2 for a
193   pedwarn.  */
194
195void
196cxx_incomplete_type_diagnostic (value, type, diag_type)
197     tree value;
198     tree type;
199     int diag_type;
200{
201  int decl = 0;
202  void (*p_msg) PARAMS ((const char *, ...));
203  void (*p_msg_at) PARAMS ((const char *, ...));
204
205  if (diag_type == 1)
206    {
207      p_msg = warning;
208      p_msg_at = cp_warning_at;
209    }
210  else if (diag_type == 2)
211    {
212      p_msg = pedwarn;
213      p_msg_at = cp_pedwarn_at;
214    }
215  else
216    {
217      p_msg = error;
218      p_msg_at = cp_error_at;
219    }
220
221  /* Avoid duplicate error message.  */
222  if (TREE_CODE (type) == ERROR_MARK)
223    return;
224
225  if (value != 0 && (TREE_CODE (value) == VAR_DECL
226		     || TREE_CODE (value) == PARM_DECL
227		     || TREE_CODE (value) == FIELD_DECL))
228    {
229      (*p_msg_at) ("`%D' has incomplete type", value);
230      decl = 1;
231    }
232retry:
233  /* We must print an error message.  Be clever about what it says.  */
234
235  switch (TREE_CODE (type))
236    {
237    case RECORD_TYPE:
238    case UNION_TYPE:
239    case ENUMERAL_TYPE:
240      if (!decl)
241        (*p_msg) ("invalid use of undefined type `%#T'", type);
242      if (!TYPE_TEMPLATE_INFO (type))
243	(*p_msg_at) ("forward declaration of `%#T'", type);
244      else
245	(*p_msg_at) ("declaration of `%#T'", type);
246      break;
247
248    case VOID_TYPE:
249      (*p_msg) ("invalid use of `%T'", type);
250      break;
251
252    case ARRAY_TYPE:
253      if (TYPE_DOMAIN (type))
254        {
255          type = TREE_TYPE (type);
256          goto retry;
257        }
258      (*p_msg) ("invalid use of array with unspecified bounds");
259      break;
260
261    case OFFSET_TYPE:
262    bad_member:
263      (*p_msg) ("invalid use of member (did you forget the `&' ?)");
264      break;
265
266    case TEMPLATE_TYPE_PARM:
267      (*p_msg) ("invalid use of template type parameter");
268      break;
269
270    case UNKNOWN_TYPE:
271      if (value && TREE_CODE (value) == COMPONENT_REF)
272        goto bad_member;
273      else if (value && TREE_CODE (value) == ADDR_EXPR)
274        (*p_msg) ("address of overloaded function with no contextual type information");
275      else if (value && TREE_CODE (value) == OVERLOAD)
276        (*p_msg) ("overloaded function with no contextual type information");
277      else
278        (*p_msg) ("insufficient contextual information to determine type");
279      break;
280
281    default:
282      abort ();
283    }
284}
285
286/* Backward-compatibility interface to incomplete_type_diagnostic;
287   required by ../tree.c.  */
288#undef cxx_incomplete_type_error
289void
290cxx_incomplete_type_error (value, type)
291     tree value;
292     tree type;
293{
294  cxx_incomplete_type_diagnostic (value, type, 0);
295}
296
297
298/* Perform appropriate conversions on the initial value of a variable,
299   store it in the declaration DECL,
300   and print any error messages that are appropriate.
301   If the init is invalid, store an ERROR_MARK.
302
303   C++: Note that INIT might be a TREE_LIST, which would mean that it is
304   a base class initializer for some aggregate type, hopefully compatible
305   with DECL.  If INIT is a single element, and DECL is an aggregate
306   type, we silently convert INIT into a TREE_LIST, allowing a constructor
307   to be called.
308
309   If INIT is a TREE_LIST and there is no constructor, turn INIT
310   into a CONSTRUCTOR and use standard initialization techniques.
311   Perhaps a warning should be generated?
312
313   Returns value of initializer if initialization could not be
314   performed for static variable.  In that case, caller must do
315   the storing.  */
316
317tree
318store_init_value (decl, init)
319     tree decl, init;
320{
321  register tree value, type;
322
323  /* If variable's type was invalidly declared, just ignore it.  */
324
325  type = TREE_TYPE (decl);
326  if (TREE_CODE (type) == ERROR_MARK)
327    return NULL_TREE;
328
329  if (IS_AGGR_TYPE (type))
330    {
331      if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
332	  && TREE_CODE (init) != CONSTRUCTOR)
333	abort ();
334
335      if (TREE_CODE (init) == TREE_LIST)
336	{
337	  error ("constructor syntax used, but no constructor declared for type `%T'", type);
338	  init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
339	}
340    }
341  else if (TREE_CODE (init) == TREE_LIST
342	   && TREE_TYPE (init) != unknown_type_node)
343    {
344      if (TREE_CODE (decl) == RESULT_DECL)
345	{
346	  if (TREE_CHAIN (init))
347	    {
348	      warning ("comma expression used to initialize return value");
349	      init = build_compound_expr (init);
350	    }
351	  else
352	    init = TREE_VALUE (init);
353	}
354      else if (TREE_CODE (init) == TREE_LIST
355	       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
356	{
357	  error ("cannot initialize arrays using this syntax");
358	  return NULL_TREE;
359	}
360      else
361	{
362	  /* We get here with code like `int a (2);' */
363
364	  if (TREE_CHAIN (init) != NULL_TREE)
365	    {
366	      pedwarn ("initializer list being treated as compound expression");
367	      init = build_compound_expr (init);
368	    }
369	  else
370	    init = TREE_VALUE (init);
371	}
372    }
373
374  /* End of special C++ code.  */
375
376  /* Digest the specified initializer into an expression.  */
377  value = digest_init (type, init, (tree *) 0);
378
379  /* Store the expression if valid; else report error.  */
380
381  if (TREE_CODE (value) == ERROR_MARK)
382    ;
383  /* Other code expects that initializers for objects of types that need
384     constructing never make it into DECL_INITIAL, and passes 'init' to
385     build_aggr_init without checking DECL_INITIAL.  So just return.  */
386  else if (TYPE_NEEDS_CONSTRUCTING (type))
387    return value;
388  else if (TREE_STATIC (decl)
389	   && (! TREE_CONSTANT (value)
390	       || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
391    return value;
392
393  /* Store the VALUE in DECL_INITIAL.  If we're building a
394     statement-tree we will actually expand the initialization later
395     when we output this function.  */
396  DECL_INITIAL (decl) = value;
397  return NULL_TREE;
398}
399
400
401/* Digest the parser output INIT as an initializer for type TYPE.
402   Return a C expression of type TYPE to represent the initial value.
403
404   If TAIL is nonzero, it points to a variable holding a list of elements
405   of which INIT is the first.  We update the list stored there by
406   removing from the head all the elements that we use.
407   Normally this is only one; we use more than one element only if
408   TYPE is an aggregate and INIT is not a constructor.  */
409
410tree
411digest_init (type, init, tail)
412     tree type, init, *tail;
413{
414  enum tree_code code = TREE_CODE (type);
415  tree element = NULL_TREE;
416  tree old_tail_contents = NULL_TREE;
417  /* Nonzero if INIT is a braced grouping.  */
418  int raw_constructor;
419
420  /* By default, assume we use one element from a list.
421     We correct this later in the sole case where it is not true.  */
422
423  if (tail)
424    {
425      old_tail_contents = *tail;
426      *tail = TREE_CHAIN (*tail);
427    }
428
429  if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
430				  && TREE_VALUE (init) == error_mark_node))
431    return error_mark_node;
432
433  if (TREE_CODE (init) == ERROR_MARK)
434    /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside
435       a template function. This gets substituted during instantiation.  */
436    return init;
437
438  /* We must strip the outermost array type when completing the type,
439     because the its bounds might be incomplete at the moment.  */
440  if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
441			      ? TREE_TYPE (type) : type, NULL_TREE))
442    return error_mark_node;
443
444  /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
445  if (TREE_CODE (init) == NON_LVALUE_EXPR)
446    init = TREE_OPERAND (init, 0);
447
448  raw_constructor = (TREE_CODE (init) == CONSTRUCTOR
449		     && TREE_HAS_CONSTRUCTOR (init));
450
451  if (raw_constructor
452      && CONSTRUCTOR_ELTS (init) != 0
453      && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
454    {
455      element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
456      /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
457      if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
458	element = TREE_OPERAND (element, 0);
459      if (element == error_mark_node)
460	return element;
461    }
462
463  /* Initialization of an array of chars from a string constant
464     optionally enclosed in braces.  */
465
466  if (code == ARRAY_TYPE)
467    {
468      tree typ1;
469
470      if (TREE_CODE (init) == TREE_LIST)
471	{
472	  error ("initializing array with parameter list");
473	  return error_mark_node;
474	}
475
476      typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
477      if (char_type_p (typ1)
478	  && ((init && TREE_CODE (init) == STRING_CST)
479	      || (element && TREE_CODE (element) == STRING_CST)))
480	{
481	  tree string = element ? element : init;
482
483	  if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
484	       != char_type_node)
485	      && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
486	    {
487	      error ("char-array initialized from wide string");
488	      return error_mark_node;
489	    }
490	  if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
491	       == char_type_node)
492	      && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
493	    {
494	      error ("int-array initialized from non-wide string");
495	      return error_mark_node;
496	    }
497
498	  TREE_TYPE (string) = type;
499	  if (TYPE_DOMAIN (type) != 0
500	      && TREE_CONSTANT (TYPE_SIZE (type)))
501	    {
502	      register int size
503		= TREE_INT_CST_LOW (TYPE_SIZE (type));
504	      size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
505	      /* In C it is ok to subtract 1 from the length of the string
506		 because it's ok to ignore the terminating null char that is
507		 counted in the length of the constant, but in C++ this would
508		 be invalid.  */
509	      if (size < TREE_STRING_LENGTH (string))
510		pedwarn ("initializer-string for array of chars is too long");
511	    }
512	  return string;
513	}
514    }
515
516  /* Handle scalar types, including conversions,
517     and signature pointers and references.  */
518
519  if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
520      || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
521      || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
522      || TYPE_PTRMEMFUNC_P (type))
523    {
524      if (raw_constructor)
525	{
526	  if (element == 0)
527	    {
528	      error ("initializer for scalar variable requires one element");
529	      return error_mark_node;
530	    }
531	  init = element;
532	}
533      while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
534	{
535	  pedwarn ("braces around scalar initializer for `%T'", type);
536	  init = CONSTRUCTOR_ELTS (init);
537	  if (TREE_CHAIN (init))
538	    pedwarn ("ignoring extra initializers for `%T'", type);
539	  init = TREE_VALUE (init);
540	}
541
542      return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
543					 "initialization", NULL_TREE, 0);
544    }
545
546  /* Come here only for records and arrays (and unions with constructors).  */
547
548  if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
549    {
550      error ("variable-sized object of type `%T' may not be initialized",
551		type);
552      return error_mark_node;
553    }
554
555  if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code))
556    {
557      if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type)
558	  && TREE_HAS_CONSTRUCTOR (init))
559	{
560	  error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
561		    type, init);
562	  return error_mark_node;
563	}
564      else if (raw_constructor)
565	return process_init_constructor (type, init, (tree *)0);
566      else if (can_convert_arg (type, TREE_TYPE (init), init)
567	       || TYPE_NON_AGGREGATE_CLASS (type))
568	/* These are never initialized from multiple constructor elements.  */;
569      else if (tail != 0)
570	{
571	  *tail = old_tail_contents;
572	  return process_init_constructor (type, 0, tail);
573	}
574
575      if (code != ARRAY_TYPE)
576	{
577	  int flags = LOOKUP_NORMAL;
578	  /* Initialization from { } is copy-initialization.  */
579	  if (tail)
580	    flags |= LOOKUP_ONLYCONVERTING;
581
582	  return convert_for_initialization (NULL_TREE, type, init, flags,
583					     "initialization", NULL_TREE, 0);
584	}
585    }
586
587  error ("invalid initializer");
588  return error_mark_node;
589}
590
591/* Process a constructor for a variable of type TYPE.
592   The constructor elements may be specified either with INIT or with ELTS,
593   only one of which should be non-null.
594
595   If INIT is specified, it is a CONSTRUCTOR node which is specifically
596   and solely for initializing this datum.
597
598   If ELTS is specified, it is the address of a variable containing
599   a list of expressions.  We take as many elements as we need
600   from the head of the list and update the list.
601
602   In the resulting constructor, TREE_CONSTANT is set if all elts are
603   constant, and TREE_STATIC is set if, in addition, all elts are simple enough
604   constants that the assembler and linker can compute them.  */
605
606static tree
607process_init_constructor (type, init, elts)
608     tree type, init, *elts;
609{
610  register tree tail;
611  /* List of the elements of the result constructor,
612     in reverse order.  */
613  register tree members = NULL;
614  register tree next1;
615  tree result;
616  int allconstant = 1;
617  int allsimple = 1;
618  int erroneous = 0;
619
620  /* Make TAIL be the list of elements to use for the initialization,
621     no matter how the data was given to us.  */
622
623  if (elts)
624    {
625      if (warn_missing_braces)
626	warning ("aggregate has a partly bracketed initializer");
627      tail = *elts;
628    }
629  else
630    tail = CONSTRUCTOR_ELTS (init);
631
632  /* Gobble as many elements as needed, and make a constructor or initial value
633     for each element of this aggregate.  Chain them together in result.
634     If there are too few, use 0 for each scalar ultimate component.  */
635
636  if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
637    {
638      register long len;
639      register int i;
640
641      if (TREE_CODE (type) == ARRAY_TYPE)
642	{
643	  tree domain = TYPE_DOMAIN (type);
644	  if (domain)
645	    len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
646		   - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
647		   + 1);
648	  else
649	    len = -1;  /* Take as many as there are */
650	}
651      else
652	{
653	  /* Vectors are like simple fixed-size arrays.  */
654	  len = TYPE_VECTOR_SUBPARTS (type);
655	}
656
657      for (i = 0; len < 0 || i < len; i++)
658	{
659	  if (tail)
660	    {
661	      if (TREE_PURPOSE (tail)
662		  && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
663		      || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
664		sorry ("non-trivial labeled initializers");
665
666	      if (TREE_VALUE (tail) != 0)
667		{
668		  tree tail1 = tail;
669		  next1 = digest_init (TREE_TYPE (type),
670				       TREE_VALUE (tail), &tail1);
671		  if (next1 == error_mark_node)
672		    return next1;
673		  my_friendly_assert
674		    (same_type_ignoring_top_level_qualifiers_p
675		     (TREE_TYPE (type), TREE_TYPE (next1)),
676		     981123);
677		  my_friendly_assert (tail1 == 0
678				      || TREE_CODE (tail1) == TREE_LIST, 319);
679		  if (tail == tail1 && len < 0)
680		    {
681		      error ("non-empty initializer for array of empty elements");
682		      /* Just ignore what we were supposed to use.  */
683		      tail1 = NULL_TREE;
684		    }
685		  tail = tail1;
686		}
687	      else
688		{
689		  next1 = error_mark_node;
690		  tail = TREE_CHAIN (tail);
691		}
692	    }
693	  else if (len < 0)
694	    /* We're done.  */
695	    break;
696	  else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
697	    {
698	      /* If this type needs constructors run for
699		 default-initialization, we can't rely on the backend to do it
700		 for us, so build up TARGET_EXPRs.  If the type in question is
701		 a class, just build one up; if it's an array, recurse.  */
702
703	      if (IS_AGGR_TYPE (TREE_TYPE (type)))
704		next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
705	      else
706		next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
707	      next1 = digest_init (TREE_TYPE (type), next1, 0);
708	    }
709	  else if (! zero_init_p (TREE_TYPE (type)))
710	    next1 = build_zero_init (TREE_TYPE (type),
711				     /*nelts=*/NULL_TREE,
712				     /*static_storage_p=*/false);
713	  else
714	    /* The default zero-initialization is fine for us; don't
715	       add anything to the CONSTRUCTOR.  */
716	    break;
717
718	  if (next1 == error_mark_node)
719	    erroneous = 1;
720	  else if (!TREE_CONSTANT (next1))
721	    allconstant = 0;
722	  else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
723	    allsimple = 0;
724	  members = tree_cons (size_int (i), next1, members);
725	}
726    }
727  else if (TREE_CODE (type) == RECORD_TYPE)
728    {
729      register tree field;
730
731      if (tail)
732	{
733	  if (TYPE_USES_VIRTUAL_BASECLASSES (type))
734	    {
735	      sorry ("initializer list for object of class with virtual base classes");
736	      return error_mark_node;
737	    }
738
739	  if (TYPE_BINFO_BASETYPES (type))
740	    {
741	      sorry ("initializer list for object of class with base classes");
742	      return error_mark_node;
743	    }
744
745	  if (TYPE_POLYMORPHIC_P (type))
746	    {
747	      sorry ("initializer list for object using virtual functions");
748	      return error_mark_node;
749	    }
750	}
751
752      for (field = TYPE_FIELDS (type); field;
753	   field = TREE_CHAIN (field))
754	{
755	  if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
756	    {
757	      members = tree_cons (field, integer_zero_node, members);
758	      continue;
759	    }
760
761	  if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
762	    continue;
763
764	  if (tail)
765	    {
766	      if (TREE_PURPOSE (tail)
767		  && TREE_PURPOSE (tail) != field
768		  && TREE_PURPOSE (tail) != DECL_NAME (field))
769		sorry ("non-trivial labeled initializers");
770
771	      if (TREE_VALUE (tail) != 0)
772		{
773		  tree tail1 = tail;
774
775		  next1 = digest_init (TREE_TYPE (field),
776				       TREE_VALUE (tail), &tail1);
777		  my_friendly_assert (tail1 == 0
778				      || TREE_CODE (tail1) == TREE_LIST, 320);
779		  tail = tail1;
780		}
781	      else
782		{
783		  next1 = error_mark_node;
784		  tail = TREE_CHAIN (tail);
785		}
786	    }
787	  else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
788	    {
789	      /* If this type needs constructors run for
790		 default-initialization, we can't rely on the backend to do it
791		 for us, so build up TARGET_EXPRs.  If the type in question is
792		 a class, just build one up; if it's an array, recurse.  */
793
794	      if (IS_AGGR_TYPE (TREE_TYPE (field)))
795		next1 = build_functional_cast (TREE_TYPE (field),
796					       NULL_TREE);
797	      else
798	        {
799		  next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE,
800			         NULL_TREE);
801                  if (init)
802                    TREE_HAS_CONSTRUCTOR (next1)
803                       = TREE_HAS_CONSTRUCTOR (init);
804                }
805	      next1 = digest_init (TREE_TYPE (field), next1, 0);
806
807	      /* Warn when some struct elements are implicitly initialized.  */
808	      if (extra_warnings
809	          && (!init || TREE_HAS_CONSTRUCTOR (init)))
810		warning ("missing initializer for member `%D'", field);
811	    }
812	  else
813	    {
814	      if (TREE_READONLY (field))
815		error ("uninitialized const member `%D'", field);
816	      else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
817		error ("member `%D' with uninitialized const fields",
818			  field);
819	      else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
820		error ("member `%D' is uninitialized reference", field);
821
822	      /* Warn when some struct elements are implicitly initialized
823		 to zero.  */
824	      if (extra_warnings
825	          && (!init || TREE_HAS_CONSTRUCTOR (init)))
826		warning ("missing initializer for member `%D'", field);
827
828	      if (! zero_init_p (TREE_TYPE (field)))
829		next1 = build_zero_init (TREE_TYPE (field),
830					 /*nelts=*/NULL_TREE,
831					 /*static_storage_p=*/false);
832	      else
833		/* The default zero-initialization is fine for us; don't
834		   add anything to the CONSTRUCTOR.  */
835		continue;
836	    }
837
838	  if (next1 == error_mark_node)
839	    erroneous = 1;
840	  else if (!TREE_CONSTANT (next1))
841	    allconstant = 0;
842	  else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
843	    allsimple = 0;
844	  members = tree_cons (field, next1, members);
845	}
846    }
847  else if (TREE_CODE (type) == UNION_TYPE
848	   /* If the initializer was empty, use default zero initialization.  */
849	   && tail)
850    {
851      register tree field = TYPE_FIELDS (type);
852
853      /* Find the first named field.  ANSI decided in September 1990
854	 that only named fields count here.  */
855      while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
856	field = TREE_CHAIN (field);
857
858      /* If this element specifies a field, initialize via that field.  */
859      if (TREE_PURPOSE (tail) != NULL_TREE)
860	{
861	  int win = 0;
862
863	  if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
864	    /* Handle the case of a call by build_c_cast.  */
865	    field = TREE_PURPOSE (tail), win = 1;
866	  else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
867	    error ("index value instead of field name in union initializer");
868	  else
869	    {
870	      tree temp;
871	      for (temp = TYPE_FIELDS (type);
872		   temp;
873		   temp = TREE_CHAIN (temp))
874		if (DECL_NAME (temp) == TREE_PURPOSE (tail))
875		  break;
876	      if (temp)
877		field = temp, win = 1;
878	      else
879		error ("no field `%D' in union being initialized",
880			  TREE_PURPOSE (tail));
881	    }
882	  if (!win)
883	    TREE_VALUE (tail) = error_mark_node;
884	}
885      else if (field == 0)
886	{
887	  error ("union `%T' with no named members cannot be initialized",
888		    type);
889	  TREE_VALUE (tail) = error_mark_node;
890	}
891
892      if (TREE_VALUE (tail) != 0)
893	{
894	  tree tail1 = tail;
895
896	  next1 = digest_init (TREE_TYPE (field),
897			       TREE_VALUE (tail), &tail1);
898	  if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
899	    abort ();
900	  tail = tail1;
901	}
902      else
903	{
904	  next1 = error_mark_node;
905	  tail = TREE_CHAIN (tail);
906	}
907
908      if (next1 == error_mark_node)
909	erroneous = 1;
910      else if (!TREE_CONSTANT (next1))
911	allconstant = 0;
912      else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
913	allsimple = 0;
914      members = tree_cons (field, next1, members);
915    }
916
917  /* If arguments were specified as a list, just remove the ones we used.  */
918  if (elts)
919    *elts = tail;
920  /* If arguments were specified as a constructor,
921     complain unless we used all the elements of the constructor.  */
922  else if (tail)
923    pedwarn ("excess elements in aggregate initializer");
924
925  if (erroneous)
926    return error_mark_node;
927
928  result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
929  if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
930    complete_array_type (type, result, /*do_default=*/0);
931  if (init)
932    TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
933  if (allconstant) TREE_CONSTANT (result) = 1;
934  if (allconstant && allsimple) TREE_STATIC (result) = 1;
935  return result;
936}
937
938/* Given a structure or union value DATUM, construct and return
939   the structure or union component which results from narrowing
940   that value to the base specified in BASETYPE.  For example, given the
941   hierarchy
942
943   class L { int ii; };
944   class A : L { ... };
945   class B : L { ... };
946   class C : A, B { ... };
947
948   and the declaration
949
950   C x;
951
952   then the expression
953
954   x.A::ii refers to the ii member of the L part of
955   the A part of the C object named by X.  In this case,
956   DATUM would be x, and BASETYPE would be A.
957
958   I used to think that this was nonconformant, that the standard specified
959   that first we look up ii in A, then convert x to an L& and pull out the
960   ii part.  But in fact, it does say that we convert x to an A&; A here
961   is known as the "naming class".  (jason 2000-12-19)
962
963   BINFO_P points to a variable initialized either to NULL_TREE or to the
964   binfo for the specific base subobject we want to convert to.  */
965
966tree
967build_scoped_ref (datum, basetype, binfo_p)
968     tree datum;
969     tree basetype;
970     tree *binfo_p;
971{
972  tree binfo;
973
974  if (datum == error_mark_node)
975    return error_mark_node;
976  if (*binfo_p)
977    binfo = *binfo_p;
978  else
979    binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
980
981  if (!binfo || binfo == error_mark_node)
982    {
983      *binfo_p = NULL_TREE;
984      if (!binfo)
985	error_not_base_type (basetype, TREE_TYPE (datum));
986      return error_mark_node;
987    }
988
989  *binfo_p = binfo;
990  return build_base_path (PLUS_EXPR, datum, binfo, 1);
991}
992
993/* Build a reference to an object specified by the C++ `->' operator.
994   Usually this just involves dereferencing the object, but if the
995   `->' operator is overloaded, then such overloads must be
996   performed until an object which does not have the `->' operator
997   overloaded is found.  An error is reported when circular pointer
998   delegation is detected.  */
999
1000tree
1001build_x_arrow (datum)
1002     tree datum;
1003{
1004  tree types_memoized = NULL_TREE;
1005  register tree rval = datum;
1006  tree type = TREE_TYPE (rval);
1007  tree last_rval = NULL_TREE;
1008
1009  if (type == error_mark_node)
1010    return error_mark_node;
1011
1012  if (processing_template_decl)
1013    return build_min_nt (ARROW_EXPR, rval);
1014
1015  if (TREE_CODE (rval) == OFFSET_REF)
1016    {
1017      rval = resolve_offset_ref (datum);
1018      type = TREE_TYPE (rval);
1019    }
1020
1021  if (TREE_CODE (type) == REFERENCE_TYPE)
1022    {
1023      rval = convert_from_reference (rval);
1024      type = TREE_TYPE (rval);
1025    }
1026
1027  if (IS_AGGR_TYPE (type))
1028    {
1029      while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval,
1030				     NULL_TREE, NULL_TREE)))
1031	{
1032	  if (rval == error_mark_node)
1033	    return error_mark_node;
1034
1035	  if (value_member (TREE_TYPE (rval), types_memoized))
1036	    {
1037	      error ("circular pointer delegation detected");
1038	      return error_mark_node;
1039	    }
1040	  else
1041	    {
1042	      types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1043					  types_memoized);
1044	    }
1045	  last_rval = rval;
1046	}
1047
1048      if (last_rval == NULL_TREE)
1049	{
1050	  error ("base operand of `->' has non-pointer type `%T'", type);
1051	  return error_mark_node;
1052	}
1053
1054      if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1055	last_rval = convert_from_reference (last_rval);
1056    }
1057  else
1058    last_rval = default_conversion (rval);
1059
1060  if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1061    return build_indirect_ref (last_rval, NULL);
1062
1063  if (types_memoized)
1064    error ("result of `operator->()' yields non-pointer result");
1065  else
1066    error ("base operand of `->' is not a pointer");
1067  return error_mark_node;
1068}
1069
1070/* Make an expression to refer to the COMPONENT field of
1071   structure or union value DATUM.  COMPONENT is an arbitrary
1072   expression.  DATUM has not already been checked out to be of
1073   aggregate type.
1074
1075   For C++, COMPONENT may be a TREE_LIST.  This happens when we must
1076   return an object of member type to a method of the current class,
1077   but there is not yet enough typing information to know which one.
1078   As a special case, if there is only one method by that name,
1079   it is returned.  Otherwise we return an expression which other
1080   routines will have to know how to deal with later.  */
1081
1082tree
1083build_m_component_ref (datum, component)
1084     tree datum, component;
1085{
1086  tree type;
1087  tree objtype;
1088  tree field_type;
1089  int type_quals;
1090  tree binfo;
1091
1092  if (processing_template_decl)
1093    return build_min_nt (DOTSTAR_EXPR, datum, component);
1094
1095  datum = decay_conversion (datum);
1096
1097  if (datum == error_mark_node || component == error_mark_node)
1098    return error_mark_node;
1099
1100  objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1101
1102  if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1103    {
1104      type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1105      field_type = type;
1106    }
1107  else if (TYPE_PTRMEM_P (TREE_TYPE (component)))
1108    {
1109      type = TREE_TYPE (TREE_TYPE (component));
1110      field_type = TREE_TYPE (type);
1111
1112      /* Compute the type of the field, as described in [expr.ref].  */
1113      type_quals = TYPE_UNQUALIFIED;
1114      if (TREE_CODE (field_type) == REFERENCE_TYPE)
1115	/* The standard says that the type of the result should be the
1116       	   type referred to by the reference.  But for now, at least,
1117       	   we do the conversion from reference type later.  */
1118	;
1119      else
1120	{
1121	  type_quals = (cp_type_quals (field_type)
1122			| cp_type_quals (TREE_TYPE (datum)));
1123
1124	  /* There's no such thing as a mutable pointer-to-member, so
1125	     things are not as complex as they are for references to
1126	     non-static data members.  */
1127	  field_type = cp_build_qualified_type (field_type, type_quals);
1128	}
1129    }
1130  else
1131    {
1132      error ("`%E' cannot be used as a member pointer, since it is of type `%T'",
1133		component, TREE_TYPE (component));
1134      return error_mark_node;
1135    }
1136
1137  if (! IS_AGGR_TYPE (objtype))
1138    {
1139      error ("cannot apply member pointer `%E' to `%E', which is of non-aggregate type `%T'",
1140		component, datum, objtype);
1141      return error_mark_node;
1142    }
1143
1144  binfo = lookup_base (objtype, TYPE_METHOD_BASETYPE (type),
1145		       ba_check, NULL);
1146  if (!binfo)
1147    {
1148      error ("member type `%T::' incompatible with object type `%T'",
1149		TYPE_METHOD_BASETYPE (type), objtype);
1150      return error_mark_node;
1151    }
1152  else if (binfo == error_mark_node)
1153    return error_mark_node;
1154
1155  component = build (OFFSET_REF, field_type, datum, component);
1156  if (TREE_CODE (type) == OFFSET_TYPE)
1157    component = resolve_offset_ref (component);
1158  return component;
1159}
1160
1161/* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
1162
1163tree
1164build_functional_cast (exp, parms)
1165     tree exp;
1166     tree parms;
1167{
1168  /* This is either a call to a constructor,
1169     or a C cast in C++'s `functional' notation.  */
1170  tree type;
1171
1172  if (exp == error_mark_node || parms == error_mark_node)
1173    return error_mark_node;
1174
1175  if (TREE_CODE (exp) == IDENTIFIER_NODE)
1176    {
1177      if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1178	/* Either an enum or an aggregate type.  */
1179	type = IDENTIFIER_TYPE_VALUE (exp);
1180      else
1181	{
1182	  type = lookup_name (exp, 1);
1183	  if (!type || TREE_CODE (type) != TYPE_DECL)
1184	    {
1185	      error ("`%T' fails to be a typedef or built-in type", exp);
1186	      return error_mark_node;
1187	    }
1188	  type = TREE_TYPE (type);
1189	}
1190    }
1191  else if (TREE_CODE (exp) == TYPE_DECL)
1192    type = TREE_TYPE (exp);
1193  else
1194    type = exp;
1195
1196  if (processing_template_decl)
1197    return build_min (CAST_EXPR, type, parms);
1198
1199  if (! IS_AGGR_TYPE (type))
1200    {
1201      /* this must build a C cast */
1202      if (parms == NULL_TREE)
1203	parms = integer_zero_node;
1204      else
1205	{
1206	  if (TREE_CHAIN (parms) != NULL_TREE)
1207	    pedwarn ("initializer list being treated as compound expression");
1208	  parms = build_compound_expr (parms);
1209	}
1210
1211      return build_c_cast (type, parms);
1212    }
1213
1214  /* Prepare to evaluate as a call to a constructor.  If this expression
1215     is actually used, for example,
1216
1217     return X (arg1, arg2, ...);
1218
1219     then the slot being initialized will be filled in.  */
1220
1221  if (!complete_type_or_else (type, NULL_TREE))
1222    return error_mark_node;
1223  if (abstract_virtuals_error (NULL_TREE, type))
1224    return error_mark_node;
1225
1226  if (parms && TREE_CHAIN (parms) == NULL_TREE)
1227    return build_c_cast (type, TREE_VALUE (parms));
1228
1229  /* We need to zero-initialize POD types.  Let's do that for everything
1230     that doesn't need a constructor.  */
1231  if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1232      && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1233    {
1234      exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
1235      return get_target_expr (exp);
1236    }
1237
1238  exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms,
1239				   TYPE_BINFO (type), LOOKUP_NORMAL);
1240
1241  if (exp == error_mark_node)
1242    return error_mark_node;
1243
1244  return build_cplus_new (type, exp);
1245}
1246
1247
1248/* Complain about defining new types in inappropriate places.  We give an
1249   exception for C-style casts, to accommodate GNU C stylings.  */
1250
1251void
1252check_for_new_type (string, inptree)
1253     const char *string;
1254     flagged_type_tree inptree;
1255{
1256  if (inptree.new_type_flag
1257      && (pedantic || strcmp (string, "cast") != 0))
1258    pedwarn ("ISO C++ forbids defining types within %s", string);
1259}
1260
1261/* Add new exception specifier SPEC, to the LIST we currently have.
1262   If it's already in LIST then do nothing.
1263   Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1264   know what we're doing.  */
1265
1266tree
1267add_exception_specifier (list, spec, complain)
1268     tree list, spec;
1269     int complain;
1270{
1271  int ok;
1272  tree core = spec;
1273  int is_ptr;
1274  int diag_type = -1; /* none */
1275
1276  if (spec == error_mark_node)
1277    return list;
1278
1279  my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1280
1281  /* [except.spec] 1, type in an exception specifier shall not be
1282     incomplete, or pointer or ref to incomplete other than pointer
1283     to cv void.  */
1284  is_ptr = TREE_CODE (core) == POINTER_TYPE;
1285  if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1286    core = TREE_TYPE (core);
1287  if (complain < 0)
1288    ok = 1;
1289  else if (VOID_TYPE_P (core))
1290    ok = is_ptr;
1291  else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1292    ok = 1;
1293  else if (processing_template_decl)
1294    ok = 1;
1295  else
1296    {
1297      ok = 1;
1298      /* 15.4/1 says that types in an exception specifier must be complete,
1299         but it seems more reasonable to only require this on definitions
1300         and calls.  So just give a pedwarn at this point; we will give an
1301         error later if we hit one of those two cases.  */
1302      if (!COMPLETE_TYPE_P (complete_type (core)))
1303	diag_type = 2; /* pedwarn */
1304    }
1305
1306  if (ok)
1307    {
1308      tree probe;
1309
1310      for (probe = list; probe; probe = TREE_CHAIN (probe))
1311        if (same_type_p (TREE_VALUE (probe), spec))
1312          break;
1313      if (!probe)
1314	list = tree_cons (NULL_TREE, spec, list);
1315    }
1316  else
1317    diag_type = 0; /* error */
1318
1319  if (diag_type >= 0 && complain)
1320    cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1321
1322  return list;
1323}
1324
1325/* Combine the two exceptions specifier lists LIST and ADD, and return
1326   their union.  */
1327
1328tree
1329merge_exception_specifiers (list, add)
1330     tree list, add;
1331{
1332  if (!list || !add)
1333    return NULL_TREE;
1334  else if (!TREE_VALUE (list))
1335    return add;
1336  else if (!TREE_VALUE (add))
1337    return list;
1338  else
1339    {
1340      tree orig_list = list;
1341
1342      for (; add; add = TREE_CHAIN (add))
1343        {
1344          tree spec = TREE_VALUE (add);
1345          tree probe;
1346
1347          for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1348            if (same_type_p (TREE_VALUE (probe), spec))
1349              break;
1350          if (!probe)
1351            {
1352              spec = build_tree_list (NULL_TREE, spec);
1353              TREE_CHAIN (spec) = list;
1354              list = spec;
1355            }
1356        }
1357    }
1358  return list;
1359}
1360
1361/* Subroutine of build_call.  Ensure that each of the types in the
1362   exception specification is complete.  Technically, 15.4/1 says that
1363   they need to be complete when we see a declaration of the function,
1364   but we should be able to get away with only requiring this when the
1365   function is defined or called.  See also add_exception_specifier.  */
1366
1367void
1368require_complete_eh_spec_types (fntype, decl)
1369     tree fntype, decl;
1370{
1371  tree raises;
1372  /* Don't complain about calls to op new.  */
1373  if (decl && DECL_ARTIFICIAL (decl))
1374    return;
1375  for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1376       raises = TREE_CHAIN (raises))
1377    {
1378      tree type = TREE_VALUE (raises);
1379      if (type && !COMPLETE_TYPE_P (type))
1380	{
1381	  if (decl)
1382	    error
1383	      ("call to function `%D' which throws incomplete type `%#T'",
1384	       decl, type);
1385	  else
1386	    error ("call to function which throws incomplete type `%#T'",
1387		   decl);
1388	}
1389    }
1390}
1391