1/* Report error messages, build initializers, and perform
2   some front-end optimizations for C++ compiler.
3   Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
4   Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING.  If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA.  */
22
23
24/* This file is part of the C++ front end.
25   It contains routines to build C++ expressions given their operands,
26   including computing the types of the result, C and C++ specific error
27   checks, and some optimization.
28
29   There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30   and to process initializations in declarations (since they work
31   like a strange sort of assignment).  */
32
33#include "config.h"
34#include "system.h"
35#include "tree.h"
36#include "cp-tree.h"
37#include "flags.h"
38#include "toplev.h"
39
40static tree process_init_constructor PROTO((tree, tree, tree *));
41static void ack PVPROTO ((const char *, ...)) ATTRIBUTE_PRINTF_1;
42
43extern int errorcount;
44extern int sorrycount;
45
46/* Print an error message stemming from an attempt to use
47   BASETYPE as a base class for TYPE.  */
48
49tree
50error_not_base_type (basetype, type)
51     tree basetype, type;
52{
53  if (TREE_CODE (basetype) == FUNCTION_DECL)
54    basetype = DECL_CLASS_CONTEXT (basetype);
55  cp_error ("type `%T' is not a base type for type `%T'", basetype, type);
56  return error_mark_node;
57}
58
59tree
60binfo_or_else (parent_or_type, type)
61     tree parent_or_type, type;
62{
63  tree binfo;
64  if (TYPE_MAIN_VARIANT (parent_or_type) == TYPE_MAIN_VARIANT (type))
65    return TYPE_BINFO (parent_or_type);
66  if ((binfo = get_binfo (parent_or_type, TYPE_MAIN_VARIANT (type), 0)))
67    {
68      if (binfo == error_mark_node)
69	return NULL_TREE;
70      return binfo;
71    }
72  error_not_base_type (parent_or_type, type);
73  return NULL_TREE;
74}
75
76/* According to ARM $7.1.6, "A `const' object may be initialized, but its
77   value may not be changed thereafter.  Thus, we emit hard errors for these,
78   rather than just pedwarns.  If `SOFT' is 1, then we just pedwarn.  (For
79   example, conversions to references.)  */
80
81void
82readonly_error (arg, string, soft)
83     tree arg;
84     const char *string;
85     int soft;
86{
87  const char *fmt;
88  void (*fn) PVPROTO ((const char *, ...));
89
90  if (soft)
91    fn = cp_pedwarn;
92  else
93    fn = cp_error;
94
95  if (TREE_CODE (arg) == COMPONENT_REF)
96    {
97      if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
98        fmt = "%s of member `%D' in read-only structure";
99      else
100        fmt = "%s of read-only member `%D'";
101      (*fn) (fmt, string, TREE_OPERAND (arg, 1));
102    }
103  else if (TREE_CODE (arg) == VAR_DECL)
104    {
105      if (DECL_LANG_SPECIFIC (arg)
106	  && DECL_IN_AGGR_P (arg)
107	  && !TREE_STATIC (arg))
108	fmt = "%s of constant field `%D'";
109      else
110	fmt = "%s of read-only variable `%D'";
111      (*fn) (fmt, string, arg);
112    }
113  else if (TREE_CODE (arg) == PARM_DECL)
114    (*fn) ("%s of read-only parameter `%D'", string, arg);
115  else if (TREE_CODE (arg) == INDIRECT_REF
116           && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
117           && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
118               || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
119    (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0));
120  else if (TREE_CODE (arg) == RESULT_DECL)
121    (*fn) ("%s of read-only named return value `%D'", string, arg);
122  else if (TREE_CODE (arg) == FUNCTION_DECL)
123    (*fn) ("%s of function `%D'", string, arg);
124  else
125    (*fn) ("%s of read-only location", string);
126}
127
128/* Print an error message for invalid use of a type which declares
129   virtual functions which are not inheritable.  */
130
131void
132abstract_virtuals_error (decl, type)
133     tree decl;
134     tree type;
135{
136  tree u = CLASSTYPE_ABSTRACT_VIRTUALS (type);
137  tree tu;
138
139  if (decl)
140    {
141      if (TREE_CODE (decl) == RESULT_DECL)
142	return;
143
144      if (TREE_CODE (decl) == VAR_DECL)
145	cp_error ("cannot declare variable `%D' to be of type `%T'",
146		    decl, type);
147      else if (TREE_CODE (decl) == PARM_DECL)
148	cp_error ("cannot declare parameter `%D' to be of type `%T'",
149		    decl, type);
150      else if (TREE_CODE (decl) == FIELD_DECL)
151	cp_error ("cannot declare field `%D' to be of type `%T'",
152		    decl, type);
153      else if (TREE_CODE (decl) == FUNCTION_DECL
154	       && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
155	cp_error ("invalid return type for method `%#D'", decl);
156      else if (TREE_CODE (decl) == FUNCTION_DECL)
157	cp_error ("invalid return type for function `%#D'", decl);
158    }
159  else
160    cp_error ("cannot allocate an object of type `%T'", type);
161
162  /* Only go through this once.  */
163  if (TREE_PURPOSE (u) == NULL_TREE)
164    {
165      TREE_PURPOSE (u) = error_mark_node;
166
167      error ("  since the following virtual functions are abstract:");
168      for (tu = u; tu; tu = TREE_CHAIN (tu))
169	cp_error_at ("\t%#D", TREE_VALUE (tu));
170    }
171  else
172    cp_error ("  since type `%T' has abstract virtual functions", type);
173}
174
175/* Print an error message for invalid use of a signature type.
176   Signatures are treated similar to abstract classes here, they
177   cannot be instantiated.  */
178
179void
180signature_error (decl, type)
181     tree decl;
182     tree type;
183{
184  if (decl)
185    {
186      if (TREE_CODE (decl) == RESULT_DECL)
187	return;
188
189      if (TREE_CODE (decl) == VAR_DECL)
190	cp_error ("cannot declare variable `%D' to be of signature type `%T'",
191		  decl, type);
192      else if (TREE_CODE (decl) == PARM_DECL)
193	cp_error ("cannot declare parameter `%D' to be of signature type `%T'",
194		  decl, type);
195      else if (TREE_CODE (decl) == FIELD_DECL)
196	cp_error ("cannot declare field `%D' to be of signature type `%T'",
197		  decl, type);
198      else if (TREE_CODE (decl) == FUNCTION_DECL
199	       && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
200	cp_error ("invalid return type for method `%#D'", decl);
201      else if (TREE_CODE (decl) == FUNCTION_DECL)
202	cp_error ("invalid return type for function `%#D'", decl);
203    }
204  else
205    cp_error ("cannot allocate an object of signature type `%T'", type);
206}
207
208/* Print an error message for invalid use of an incomplete type.
209   VALUE is the expression that was used (or 0 if that isn't known)
210   and TYPE is the type that was invalid.  */
211
212void
213incomplete_type_error (value, type)
214     tree value;
215     tree type;
216{
217  /* Avoid duplicate error message.  */
218  if (TREE_CODE (type) == ERROR_MARK)
219    return;
220
221retry:
222  /* We must print an error message.  Be clever about what it says.  */
223
224  switch (TREE_CODE (type))
225    {
226    case RECORD_TYPE:
227    case UNION_TYPE:
228    case ENUMERAL_TYPE:
229      cp_error ("invalid use of undefined type `%#T'", type);
230      cp_error_at ("forward declaration of `%#T'", type);
231      break;
232
233    case VOID_TYPE:
234      cp_error ("invalid use of void expression");
235      break;
236
237    case ARRAY_TYPE:
238      if (TYPE_DOMAIN (type))
239        {
240          type = TREE_TYPE (type);
241          goto retry;
242        }
243      cp_error ("invalid use of array with unspecified bounds");
244      break;
245
246    case OFFSET_TYPE:
247    bad_member:
248      cp_error ("invalid use of member (did you forget the `&' ?)");
249      break;
250
251    case TEMPLATE_TYPE_PARM:
252      cp_error ("invalid use of template type parameter");
253      break;
254
255    case UNKNOWN_TYPE:
256      if (value && TREE_CODE (value) == COMPONENT_REF)
257        goto bad_member;
258      else if (value && TREE_CODE (value) == ADDR_EXPR)
259        cp_error ("address of overloaded function with no contextual type information");
260      else if (value && TREE_CODE (value) == OVERLOAD)
261        cp_error ("overloaded function with no contextual type information");
262      else
263        cp_error ("insufficient contextual information to determine type");
264      break;
265
266    default:
267      my_friendly_abort (108);
268    }
269
270  if (value != 0 && (TREE_CODE (value) == VAR_DECL
271		     || TREE_CODE (value) == PARM_DECL))
272    cp_error_at ("incomplete `%D' defined here", value);
273}
274
275/* Like error(), but don't call report_error_function().  */
276
277static void
278ack VPROTO ((const char *msg, ...))
279{
280#ifndef ANSI_PROTOTYPES
281  const char *msg;
282#endif
283  va_list ap;
284  extern char * progname;
285
286  VA_START (ap, msg);
287
288#ifndef ANSI_PROTOTYPES
289  msg = va_arg (ap, const char *);
290#endif
291
292  if (input_filename)
293    fprintf (stderr, "%s:%d: ", input_filename, lineno);
294  else
295    fprintf (stderr, "%s: ", progname);
296
297  vfprintf (stderr, msg, ap);
298  va_end (ap);
299
300  fprintf (stderr, "\n");
301}
302
303/* There are times when the compiler can get very confused, confused
304   to the point of giving up by aborting, simply because of previous
305   input errors.  It is much better to have the user go back and
306   correct those errors first, and see if it makes us happier, than it
307   is to abort on him.  This is because when one has a 10,000 line
308   program, and the compiler comes back with ``core dump'', the user
309   is left not knowing even where to begin to fix things and no place
310   to even try and work around things.
311
312   The parameter is to uniquely identify the problem to the user, so
313   that they can say, I am having problem 59, and know that fix 7 will
314   probably solve their problem.  Or, we can document what problem
315   59 is, so they can understand how to work around it, should they
316   ever run into it.
317
318   We used to tell people to "fix the above error[s] and try recompiling
319   the program" via a call to fatal, but that message tended to look
320   silly.  So instead, we just do the equivalent of a call to fatal in the
321   same situation (call exit).
322
323   We used to assign sequential numbers for the aborts; now we use an
324   encoding of the date the abort was added, since that has more meaning
325   when we only see the error message.  */
326
327static int abortcount = 0;
328
329void
330my_friendly_abort (i)
331     int i;
332{
333  /* if the previous error came through here, i.e. report_error_function
334     ended up calling us again, don't just exit; we want a diagnostic of
335     some kind.  */
336  if (abortcount == 1)
337    current_function_decl = NULL_TREE;
338  else if (errorcount > 0 || sorrycount > 0)
339    {
340      if (abortcount > 1)
341	{
342	  if (i == 0)
343	    ack ("Internal compiler error.");
344	  else
345	    ack ("Internal compiler error %d.", i);
346	  ack ("Please submit info to Oliver Tappe <gcc@hirschkaefer.de>.");
347	}
348      else
349	error ("confused by earlier errors, bailing out");
350
351      exit (34);
352    }
353  ++abortcount;
354
355  if (i == 0)
356    error ("Internal compiler error.");
357  else
358    error ("Internal compiler error %d.", i);
359
360  fatal ("Please submit info to Oliver Tappe <gcc@hirschkaefer.de>.");
361}
362
363void
364my_friendly_assert (cond, where)
365     int cond, where;
366{
367  if (cond == 0)
368    my_friendly_abort (where);
369}
370
371/* Return nonzero if VALUE is a valid constant-valued expression
372   for use in initializing a static variable; one that can be an
373   element of a "constant" initializer.
374
375   Return null_pointer_node if the value is absolute;
376   if it is relocatable, return the variable that determines the relocation.
377   We assume that VALUE has been folded as much as possible;
378   therefore, we do not need to check for such things as
379   arithmetic-combinations of integers.  */
380
381tree
382initializer_constant_valid_p (value, endtype)
383     tree value;
384     tree endtype;
385{
386  switch (TREE_CODE (value))
387    {
388    case CONSTRUCTOR:
389      if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
390	  && TREE_CONSTANT (value))
391	return
392	  initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
393					endtype);
394
395      return TREE_STATIC (value) ? null_pointer_node : 0;
396
397    case INTEGER_CST:
398    case REAL_CST:
399    case STRING_CST:
400    case COMPLEX_CST:
401    case PTRMEM_CST:
402      return null_pointer_node;
403
404    case ADDR_EXPR:
405      return TREE_OPERAND (value, 0);
406
407    case NON_LVALUE_EXPR:
408      return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
409
410    case CONVERT_EXPR:
411    case NOP_EXPR:
412      /* Allow conversions between pointer types.  */
413      if (POINTER_TYPE_P (TREE_TYPE (value))
414	  && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
415	return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
416
417      /* Allow conversions between real types.  */
418      if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
419	  && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
420	return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
421
422      /* Allow length-preserving conversions between integer types.  */
423      if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
424	  && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
425	  && (TYPE_PRECISION (TREE_TYPE (value))
426	      == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
427	return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
428
429      /* Allow conversions between other integer types only if
430	 explicit value.  */
431      if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
432	  && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
433	{
434	  tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
435						     endtype);
436	  if (inner == null_pointer_node)
437	    return null_pointer_node;
438	  return 0;
439	}
440
441      /* Allow (int) &foo provided int is as wide as a pointer.  */
442      if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
443	  && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
444	  && (TYPE_PRECISION (TREE_TYPE (value))
445	      >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
446	return initializer_constant_valid_p (TREE_OPERAND (value, 0),
447					     endtype);
448
449      /* Likewise conversions from int to pointers, but also allow
450	 conversions from 0.  */
451      if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
452	  && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
453	{
454	  if (integer_zerop (TREE_OPERAND (value, 0)))
455	    return null_pointer_node;
456	  else if (TYPE_PRECISION (TREE_TYPE (value))
457		   <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))
458	    return initializer_constant_valid_p (TREE_OPERAND (value, 0),
459						 endtype);
460	}
461
462      /* Allow conversions to union types if the value inside is okay.  */
463      if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
464	return initializer_constant_valid_p (TREE_OPERAND (value, 0),
465					     endtype);
466      return 0;
467
468    case PLUS_EXPR:
469      if ((TREE_CODE (endtype) == INTEGER_TYPE)
470	  && (TYPE_PRECISION (endtype) < POINTER_SIZE))
471	return 0;
472      {
473	tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
474						    endtype);
475	tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
476						    endtype);
477	/* If either term is absolute, use the other terms relocation.  */
478	if (valid0 == null_pointer_node)
479	  return valid1;
480	if (valid1 == null_pointer_node)
481	  return valid0;
482	return 0;
483      }
484
485    case MINUS_EXPR:
486      if ((TREE_CODE (endtype) == INTEGER_TYPE)
487	  && (TYPE_PRECISION (endtype) < POINTER_SIZE))
488	return 0;
489      {
490	tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
491						    endtype);
492	tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
493						    endtype);
494	/* Win if second argument is absolute.  */
495	if (valid1 == null_pointer_node)
496	  return valid0;
497	/* Win if both arguments have the same relocation.
498	   Then the value is absolute.  */
499	if (valid0 == valid1)
500	  return null_pointer_node;
501	return 0;
502      }
503
504    default:
505      break;
506    }
507
508  return 0;
509}
510
511/* Perform appropriate conversions on the initial value of a variable,
512   store it in the declaration DECL,
513   and print any error messages that are appropriate.
514   If the init is invalid, store an ERROR_MARK.
515
516   C++: Note that INIT might be a TREE_LIST, which would mean that it is
517   a base class initializer for some aggregate type, hopefully compatible
518   with DECL.  If INIT is a single element, and DECL is an aggregate
519   type, we silently convert INIT into a TREE_LIST, allowing a constructor
520   to be called.
521
522   If INIT is a TREE_LIST and there is no constructor, turn INIT
523   into a CONSTRUCTOR and use standard initialization techniques.
524   Perhaps a warning should be generated?
525
526   Returns value of initializer if initialization could not be
527   performed for static variable.  In that case, caller must do
528   the storing.  */
529
530tree
531store_init_value (decl, init)
532     tree decl, init;
533{
534  register tree value, type;
535
536  /* If variable's type was invalidly declared, just ignore it.  */
537
538  type = TREE_TYPE (decl);
539  if (TREE_CODE (type) == ERROR_MARK)
540    return NULL_TREE;
541
542#if 0
543  /* This breaks arrays, and should not have any effect for other decls.  */
544  /* Take care of C++ business up here.  */
545  type = TYPE_MAIN_VARIANT (type);
546#endif
547
548  if (IS_AGGR_TYPE (type))
549    {
550      if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
551	  && TREE_CODE (init) != CONSTRUCTOR)
552	my_friendly_abort (109);
553
554      /* Although we are not allowed to declare variables of signature
555	 type, we complain about a possible constructor call in such a
556	 declaration as well.  */
557      if (TREE_CODE (init) == TREE_LIST
558	  && IS_SIGNATURE (type))
559	{
560	  cp_error ("constructor syntax cannot be used with signature type `%T'",
561		    type);
562	  init = error_mark_node;
563	}
564      else if (TREE_CODE (init) == TREE_LIST)
565	{
566	  cp_error ("constructor syntax used, but no constructor declared for type `%T'", type);
567	  init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
568	}
569#if 0
570      if (TREE_CODE (init) == CONSTRUCTOR)
571	{
572	  tree field;
573
574	  /* Check that we're really an aggregate as ARM 8.4.1 defines it.  */
575	  if (CLASSTYPE_N_BASECLASSES (type))
576	    cp_error_at ("initializer list construction invalid for derived class object `%D'", decl);
577	  if (CLASSTYPE_VTBL_PTR (type))
578	    cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl);
579	  if (TYPE_NEEDS_CONSTRUCTING (type))
580	    {
581	      cp_error_at ("initializer list construction invalid for `%D'", decl);
582	      error ("due to the presence of a constructor");
583	    }
584	  for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
585	    if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
586	      {
587		cp_error_at ("initializer list construction invalid for `%D'", decl);
588		cp_error_at ("due to non-public access of member `%D'", field);
589	      }
590	  for (field = TYPE_METHODS (type); field; field = TREE_CHAIN (field))
591	    if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
592	      {
593		cp_error_at ("initializer list construction invalid for `%D'", decl);
594		cp_error_at ("due to non-public access of member `%D'", field);
595	      }
596	}
597#endif
598    }
599  else if (TREE_CODE (init) == TREE_LIST
600	   && TREE_TYPE (init) != unknown_type_node)
601    {
602      if (TREE_CODE (decl) == RESULT_DECL)
603	{
604	  if (TREE_CHAIN (init))
605	    {
606	      warning ("comma expression used to initialize return value");
607	      init = build_compound_expr (init);
608	    }
609	  else
610	    init = TREE_VALUE (init);
611	}
612      else if (TREE_CODE (init) == TREE_LIST
613	       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
614	{
615	  error ("cannot initialize arrays using this syntax");
616	  return NULL_TREE;
617	}
618      else
619	{
620	  /* We get here with code like `int a (2);' */
621
622	  if (TREE_CHAIN (init) != NULL_TREE)
623	    {
624	      pedwarn ("initializer list being treated as compound expression");
625	      init = build_compound_expr (init);
626	    }
627	  else
628	    init = TREE_VALUE (init);
629	}
630    }
631
632  /* End of special C++ code.  */
633
634  /* Digest the specified initializer into an expression.  */
635
636  value = digest_init (type, init, (tree *) 0);
637
638  /* Store the expression if valid; else report error.  */
639
640  if (TREE_CODE (value) == ERROR_MARK)
641    ;
642  /* Other code expects that initializers for objects of types that need
643     constructing never make it into DECL_INITIAL, and passes 'init' to
644     expand_aggr_init without checking DECL_INITIAL.  So just return.  */
645  else if (TYPE_NEEDS_CONSTRUCTING (type))
646    return value;
647  else if (TREE_STATIC (decl)
648	   && (! TREE_CONSTANT (value)
649	       || ! initializer_constant_valid_p (value, TREE_TYPE (value))
650#if 0
651	       /* A STATIC PUBLIC int variable doesn't have to be
652		  run time inited when doing pic.  (mrs) */
653	       /* Since ctors and dtors are the only things that can
654		  reference vtables, and they are always written down
655		  the vtable definition, we can leave the
656		  vtables in initialized data space.
657		  However, other initialized data cannot be initialized
658		  this way.  Instead a global file-level initializer
659		  must do the job.  */
660	       || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
661#endif
662	       ))
663
664    return value;
665#if 0 /* No, that's C.  jason 9/19/94 */
666  else
667    {
668      if (pedantic && TREE_CODE (value) == CONSTRUCTOR
669	  /* Don't complain about non-constant initializers of
670	     signature tables and signature pointers/references.  */
671	  && ! (TYPE_LANG_SPECIFIC (type)
672		&& (IS_SIGNATURE (type)
673		    || IS_SIGNATURE_POINTER (type)
674		    || IS_SIGNATURE_REFERENCE (type))))
675	{
676	  if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
677	    pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
678	}
679    }
680#endif
681  DECL_INITIAL (decl) = value;
682  return NULL_TREE;
683}
684
685/* Digest the parser output INIT as an initializer for type TYPE.
686   Return a C expression of type TYPE to represent the initial value.
687
688   If TAIL is nonzero, it points to a variable holding a list of elements
689   of which INIT is the first.  We update the list stored there by
690   removing from the head all the elements that we use.
691   Normally this is only one; we use more than one element only if
692   TYPE is an aggregate and INIT is not a constructor.  */
693
694tree
695digest_init (type, init, tail)
696     tree type, init, *tail;
697{
698  enum tree_code code = TREE_CODE (type);
699  tree element = NULL_TREE;
700  tree old_tail_contents = NULL_TREE;
701  /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
702     tree node which has no TREE_TYPE.  */
703  int raw_constructor;
704
705  /* By default, assume we use one element from a list.
706     We correct this later in the sole case where it is not true.  */
707
708  if (tail)
709    {
710      old_tail_contents = *tail;
711      *tail = TREE_CHAIN (*tail);
712    }
713
714  if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
715				  && TREE_VALUE (init) == error_mark_node))
716    return error_mark_node;
717
718  /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
719  if (TREE_CODE (init) == NON_LVALUE_EXPR)
720    init = TREE_OPERAND (init, 0);
721
722  if (TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == type)
723    return init;
724
725  raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
726
727  if (raw_constructor
728      && CONSTRUCTOR_ELTS (init) != 0
729      && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
730    {
731      element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
732      /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
733      if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
734	element = TREE_OPERAND (element, 0);
735      if (element == error_mark_node)
736	return element;
737    }
738
739  /* Initialization of an array of chars from a string constant
740     optionally enclosed in braces.  */
741
742  if (code == ARRAY_TYPE)
743    {
744      tree typ1;
745
746      if (TREE_CODE (init) == TREE_LIST)
747	{
748	  error ("initializing array with parameter list");
749	  return error_mark_node;
750	}
751
752      typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
753      if ((typ1 == char_type_node
754	   || typ1 == signed_char_type_node
755	   || typ1 == unsigned_char_type_node
756	   || typ1 == unsigned_wchar_type_node
757	   || typ1 == signed_wchar_type_node)
758	  && ((init && TREE_CODE (init) == STRING_CST)
759	      || (element && TREE_CODE (element) == STRING_CST)))
760	{
761	  tree string = element ? element : init;
762
763	  if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
764	       != char_type_node)
765	      && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
766	    {
767	      error ("char-array initialized from wide string");
768	      return error_mark_node;
769	    }
770	  if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
771	       == char_type_node)
772	      && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
773	    {
774	      error ("int-array initialized from non-wide string");
775	      return error_mark_node;
776	    }
777
778	  TREE_TYPE (string) = type;
779	  if (TYPE_DOMAIN (type) != 0
780	      && TREE_CONSTANT (TYPE_SIZE (type)))
781	    {
782	      register int size
783		= TREE_INT_CST_LOW (TYPE_SIZE (type));
784	      size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
785	      /* In C it is ok to subtract 1 from the length of the string
786		 because it's ok to ignore the terminating null char that is
787		 counted in the length of the constant, but in C++ this would
788		 be invalid.  */
789	      if (size < TREE_STRING_LENGTH (string))
790		pedwarn ("initializer-string for array of chars is too long");
791	    }
792	  return string;
793	}
794    }
795
796  /* Handle scalar types, including conversions,
797     and signature pointers and references.  */
798
799  if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
800      || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
801      || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
802      || TYPE_PTRMEMFUNC_P (type)
803      || (code == RECORD_TYPE && ! raw_constructor
804	  && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))))
805    {
806      if (raw_constructor)
807	{
808	  if (element == 0)
809	    {
810	      error ("initializer for scalar variable requires one element");
811	      return error_mark_node;
812	    }
813	  init = element;
814	}
815      while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
816	{
817	  cp_pedwarn ("braces around scalar initializer for `%T'", type);
818	  init = CONSTRUCTOR_ELTS (init);
819	  if (TREE_CHAIN (init))
820	    cp_pedwarn ("ignoring extra initializers for `%T'", type);
821	  init = TREE_VALUE (init);
822	}
823
824      return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
825					 "initialization", NULL_TREE, 0);
826    }
827
828  /* Come here only for records and arrays (and unions with constructors).  */
829
830  if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
831    {
832      cp_error ("variable-sized object of type `%T' may not be initialized",
833		type);
834      return error_mark_node;
835    }
836
837  if (code == ARRAY_TYPE || IS_AGGR_TYPE_CODE (code))
838    {
839      if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type))
840	{
841	  cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
842		    type, init);
843	  return error_mark_node;
844	}
845      else if (raw_constructor)
846	return process_init_constructor (type, init, (tree *)0);
847      else if (can_convert_arg (type, TREE_TYPE (init), init)
848	       || TYPE_NON_AGGREGATE_CLASS (type))
849	/* These are never initialized from multiple constructor elements.  */;
850      else if (tail != 0)
851	{
852	  *tail = old_tail_contents;
853	  return process_init_constructor (type, 0, tail);
854	}
855
856      if (code != ARRAY_TYPE)
857	{
858	  int flags = LOOKUP_NORMAL;
859	  /* Initialization from { } is copy-initialization.  */
860	  if (tail)
861	    flags |= LOOKUP_ONLYCONVERTING;
862
863	  return convert_for_initialization (NULL_TREE, type, init, flags,
864					     "initialization", NULL_TREE, 0);
865	}
866    }
867
868  error ("invalid initializer");
869  return error_mark_node;
870}
871
872/* Process a constructor for a variable of type TYPE.
873   The constructor elements may be specified either with INIT or with ELTS,
874   only one of which should be non-null.
875
876   If INIT is specified, it is a CONSTRUCTOR node which is specifically
877   and solely for initializing this datum.
878
879   If ELTS is specified, it is the address of a variable containing
880   a list of expressions.  We take as many elements as we need
881   from the head of the list and update the list.
882
883   In the resulting constructor, TREE_CONSTANT is set if all elts are
884   constant, and TREE_STATIC is set if, in addition, all elts are simple enough
885   constants that the assembler and linker can compute them.  */
886
887static tree
888process_init_constructor (type, init, elts)
889     tree type, init, *elts;
890{
891  register tree tail;
892  /* List of the elements of the result constructor,
893     in reverse order.  */
894  register tree members = NULL;
895  register tree next1;
896  tree result;
897  int allconstant = 1;
898  int allsimple = 1;
899  int erroneous = 0;
900
901  /* Make TAIL be the list of elements to use for the initialization,
902     no matter how the data was given to us.  */
903
904  if (elts)
905    {
906      if (warn_missing_braces)
907	warning ("aggregate has a partly bracketed initializer");
908      tail = *elts;
909    }
910  else
911    tail = CONSTRUCTOR_ELTS (init);
912
913  /* Gobble as many elements as needed, and make a constructor or initial value
914     for each element of this aggregate.  Chain them together in result.
915     If there are too few, use 0 for each scalar ultimate component.  */
916
917  if (TREE_CODE (type) == ARRAY_TYPE)
918    {
919      tree domain = TYPE_DOMAIN (type);
920      register long len;
921      register int i;
922
923      if (domain)
924	len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
925	       - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
926	       + 1);
927      else
928	len = -1;  /* Take as many as there are */
929
930      for (i = 0; len < 0 || i < len; i++)
931	{
932	  if (tail)
933	    {
934	      if (TREE_PURPOSE (tail)
935		  && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
936		      || TREE_INT_CST_LOW (TREE_PURPOSE (tail)) != i))
937		sorry ("non-trivial labeled initializers");
938
939	      if (TREE_VALUE (tail) != 0)
940		{
941		  tree tail1 = tail;
942		  next1 = digest_init (TREE_TYPE (type),
943				       TREE_VALUE (tail), &tail1);
944		  if (next1 == error_mark_node)
945		    return next1;
946		  my_friendly_assert
947		    (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
948				  TYPE_MAIN_VARIANT (TREE_TYPE (next1))),
949		     981123);
950		  my_friendly_assert (tail1 == 0
951				      || TREE_CODE (tail1) == TREE_LIST, 319);
952		  if (tail == tail1 && len < 0)
953		    {
954		      error ("non-empty initializer for array of empty elements");
955		      /* Just ignore what we were supposed to use.  */
956		      tail1 = NULL_TREE;
957		    }
958		  tail = tail1;
959		}
960	      else
961		{
962		  next1 = error_mark_node;
963		  tail = TREE_CHAIN (tail);
964		}
965	    }
966	  else if (len < 0)
967	    /* We're done.  */
968	    break;
969	  else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
970	    {
971	      /* If this type needs constructors run for
972		 default-initialization, we can't rely on the backend to do it
973		 for us, so build up TARGET_EXPRs.  If the type in question is
974		 a class, just build one up; if it's an array, recurse.  */
975
976	      if (IS_AGGR_TYPE (TREE_TYPE (type)))
977		next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
978	      else
979		next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
980	      next1 = digest_init (TREE_TYPE (type), next1, 0);
981	    }
982	  else
983	    /* The default zero-initialization is fine for us; don't
984	       add anything to the CONSTRUCTOR.  */
985	    break;
986
987	  if (next1 == error_mark_node)
988	    erroneous = 1;
989	  else if (!TREE_CONSTANT (next1))
990	    allconstant = 0;
991	  else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
992	    allsimple = 0;
993	  members = expr_tree_cons (NULL_TREE, next1, members);
994	}
995    }
996  else if (TREE_CODE (type) == RECORD_TYPE)
997    {
998      register tree field;
999
1000      if (tail)
1001	{
1002	  if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1003	    {
1004	      sorry ("initializer list for object of class with virtual baseclasses");
1005	      return error_mark_node;
1006	    }
1007
1008	  if (TYPE_BINFO_BASETYPES (type))
1009	    {
1010	      sorry ("initializer list for object of class with baseclasses");
1011	      return error_mark_node;
1012	    }
1013
1014	  if (TYPE_VIRTUAL_P (type))
1015	    {
1016	      sorry ("initializer list for object using virtual functions");
1017	      return error_mark_node;
1018	    }
1019	}
1020
1021      for (field = TYPE_FIELDS (type); field;
1022	   field = TREE_CHAIN (field))
1023	{
1024	  if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1025	    {
1026	      members = expr_tree_cons (field, integer_zero_node, members);
1027	      continue;
1028	    }
1029
1030	  if (TREE_CODE (field) != FIELD_DECL)
1031	    continue;
1032
1033	  if (tail)
1034	    {
1035	      if (TREE_PURPOSE (tail)
1036		  && TREE_PURPOSE (tail) != field
1037		  && TREE_PURPOSE (tail) != DECL_NAME (field))
1038		sorry ("non-trivial labeled initializers");
1039
1040	      if (TREE_VALUE (tail) != 0)
1041		{
1042		  tree tail1 = tail;
1043
1044		  next1 = digest_init (TREE_TYPE (field),
1045				       TREE_VALUE (tail), &tail1);
1046		  my_friendly_assert (tail1 == 0
1047				      || TREE_CODE (tail1) == TREE_LIST, 320);
1048		  tail = tail1;
1049		}
1050	      else
1051		{
1052		  next1 = error_mark_node;
1053		  tail = TREE_CHAIN (tail);
1054		}
1055	    }
1056	  else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1057	    {
1058	      /* If this type needs constructors run for
1059		 default-initialization, we can't rely on the backend to do it
1060		 for us, so build up TARGET_EXPRs.  If the type in question is
1061		 a class, just build one up; if it's an array, recurse.  */
1062
1063	      if (IS_AGGR_TYPE (TREE_TYPE (field)))
1064		next1 = build_functional_cast (TREE_TYPE (field),
1065					       NULL_TREE);
1066	      else
1067		next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE,
1068			       NULL_TREE);
1069	      next1 = digest_init (TREE_TYPE (field), next1, 0);
1070
1071	      /* Warn when some struct elements are implicitly initialized.  */
1072	      if (extra_warnings)
1073		cp_warning ("missing initializer for member `%D'", field);
1074	    }
1075	  else
1076	    {
1077	      if (TREE_READONLY (field))
1078		cp_error ("uninitialized const member `%D'", field);
1079	      else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
1080		       && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1081		cp_error ("member `%D' with uninitialized const fields",
1082			  field);
1083	      else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1084		cp_error ("member `%D' is uninitialized reference", field);
1085
1086	      /* Warn when some struct elements are implicitly initialized
1087		 to zero.  */
1088	      if (extra_warnings)
1089		cp_warning ("missing initializer for member `%D'", field);
1090
1091	      /* The default zero-initialization is fine for us; don't
1092		 add anything to the CONSTRUCTOR.  */
1093	      continue;
1094	    }
1095
1096	  if (next1 == error_mark_node)
1097	    erroneous = 1;
1098	  else if (!TREE_CONSTANT (next1))
1099	    allconstant = 0;
1100	  else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1101	    allsimple = 0;
1102	  members = expr_tree_cons (field, next1, members);
1103	}
1104    }
1105  else if (TREE_CODE (type) == UNION_TYPE)
1106    {
1107      register tree field = TYPE_FIELDS (type);
1108
1109      /* Find the first named field.  ANSI decided in September 1990
1110	 that only named fields count here.  */
1111      while (field && (DECL_NAME (field) == 0
1112		       || TREE_CODE (field) != FIELD_DECL))
1113	field = TREE_CHAIN (field);
1114
1115      /* If this element specifies a field, initialize via that field.  */
1116      if (TREE_PURPOSE (tail) != NULL_TREE)
1117	{
1118	  int win = 0;
1119
1120	  if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
1121	    /* Handle the case of a call by build_c_cast.  */
1122	    field = TREE_PURPOSE (tail), win = 1;
1123	  else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
1124	    error ("index value instead of field name in union initializer");
1125	  else
1126	    {
1127	      tree temp;
1128	      for (temp = TYPE_FIELDS (type);
1129		   temp;
1130		   temp = TREE_CHAIN (temp))
1131		if (DECL_NAME (temp) == TREE_PURPOSE (tail))
1132		  break;
1133	      if (temp)
1134		field = temp, win = 1;
1135	      else
1136		cp_error ("no field `%D' in union being initialized",
1137			  TREE_PURPOSE (tail));
1138	    }
1139	  if (!win)
1140	    TREE_VALUE (tail) = error_mark_node;
1141	}
1142      else if (field == 0)
1143	{
1144	  cp_error ("union `%T' with no named members cannot be initialized",
1145		    type);
1146	  TREE_VALUE (tail) = error_mark_node;
1147	}
1148
1149      if (TREE_VALUE (tail) != 0)
1150	{
1151	  tree tail1 = tail;
1152
1153	  next1 = digest_init (TREE_TYPE (field),
1154			       TREE_VALUE (tail), &tail1);
1155	  if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
1156	    my_friendly_abort (357);
1157	  tail = tail1;
1158	}
1159      else
1160	{
1161	  next1 = error_mark_node;
1162	  tail = TREE_CHAIN (tail);
1163	}
1164
1165      if (next1 == error_mark_node)
1166	erroneous = 1;
1167      else if (!TREE_CONSTANT (next1))
1168	allconstant = 0;
1169      else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
1170	allsimple = 0;
1171      members = expr_tree_cons (field, next1, members);
1172    }
1173
1174  /* If arguments were specified as a list, just remove the ones we used.  */
1175  if (elts)
1176    *elts = tail;
1177  /* If arguments were specified as a constructor,
1178     complain unless we used all the elements of the constructor.  */
1179  else if (tail)
1180    pedwarn ("excess elements in aggregate initializer");
1181
1182  if (erroneous)
1183    return error_mark_node;
1184
1185  result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1186  if (init)
1187    TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1188  if (allconstant) TREE_CONSTANT (result) = 1;
1189  if (allconstant && allsimple) TREE_STATIC (result) = 1;
1190  return result;
1191}
1192
1193/* Given a structure or union value DATUM, construct and return
1194   the structure or union component which results from narrowing
1195   that value by the type specified in BASETYPE.  For example, given the
1196   hierarchy
1197
1198   class L { int ii; };
1199   class A : L { ... };
1200   class B : L { ... };
1201   class C : A, B { ... };
1202
1203   and the declaration
1204
1205   C x;
1206
1207   then the expression
1208
1209   x.A::ii refers to the ii member of the L part of
1210   the A part of the C object named by X.  In this case,
1211   DATUM would be x, and BASETYPE would be A.  */
1212
1213tree
1214build_scoped_ref (datum, basetype)
1215     tree datum;
1216     tree basetype;
1217{
1218  tree ref;
1219  tree type = TREE_TYPE (datum);
1220
1221  if (datum == error_mark_node)
1222    return error_mark_node;
1223
1224  if (TREE_CODE (type) == REFERENCE_TYPE)
1225    type = TREE_TYPE (type);
1226
1227  type = TYPE_MAIN_VARIANT (type);
1228
1229  /* This is an easy conversion.  */
1230  if (is_aggr_type (basetype, 1))
1231    {
1232      tree binfo = TYPE_BINFO (basetype);
1233      if (binfo != TYPE_BINFO (type))
1234	{
1235	  binfo = get_binfo (binfo, type, 1);
1236	  if (binfo == error_mark_node)
1237	    return error_mark_node;
1238	  if (binfo == 0)
1239	    return error_not_base_type (basetype, type);
1240	}
1241
1242      switch (TREE_CODE (datum))
1243	{
1244	case NOP_EXPR:
1245	case CONVERT_EXPR:
1246	case FLOAT_EXPR:
1247	case FIX_TRUNC_EXPR:
1248	case FIX_FLOOR_EXPR:
1249	case FIX_ROUND_EXPR:
1250	case FIX_CEIL_EXPR:
1251	  ref = convert_pointer_to (binfo,
1252				    build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0));
1253	  break;
1254	default:
1255	  ref = convert_pointer_to (binfo,
1256				    build_unary_op (ADDR_EXPR, datum, 0));
1257	}
1258      return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1259    }
1260  return error_mark_node;
1261}
1262
1263/* Build a reference to an object specified by the C++ `->' operator.
1264   Usually this just involves dereferencing the object, but if the
1265   `->' operator is overloaded, then such overloads must be
1266   performed until an object which does not have the `->' operator
1267   overloaded is found.  An error is reported when circular pointer
1268   delegation is detected.  */
1269
1270tree
1271build_x_arrow (datum)
1272     tree datum;
1273{
1274  tree types_memoized = NULL_TREE;
1275  register tree rval = datum;
1276  tree type = TREE_TYPE (rval);
1277  tree last_rval = NULL_TREE;
1278
1279  if (type == error_mark_node)
1280    return error_mark_node;
1281
1282  if (processing_template_decl)
1283    return build_min_nt (ARROW_EXPR, rval);
1284
1285  if (TREE_CODE (rval) == OFFSET_REF)
1286    {
1287      rval = resolve_offset_ref (datum);
1288      type = TREE_TYPE (rval);
1289    }
1290
1291  if (TREE_CODE (type) == REFERENCE_TYPE)
1292    {
1293      rval = convert_from_reference (rval);
1294      type = TREE_TYPE (rval);
1295    }
1296
1297  if (IS_AGGR_TYPE (type))
1298    {
1299      while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval,
1300				     NULL_TREE, NULL_TREE)))
1301	{
1302	  if (rval == error_mark_node)
1303	    return error_mark_node;
1304
1305	  if (value_member (TREE_TYPE (rval), types_memoized))
1306	    {
1307	      error ("circular pointer delegation detected");
1308	      return error_mark_node;
1309	    }
1310	  else
1311	    {
1312	      types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1313					  types_memoized);
1314	    }
1315	  last_rval = rval;
1316	}
1317
1318      if (last_rval == NULL_TREE)
1319	{
1320	  cp_error ("base operand of `->' has non-pointer type `%T'", type);
1321	  return error_mark_node;
1322	}
1323
1324      if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1325	last_rval = convert_from_reference (last_rval);
1326    }
1327  else
1328    last_rval = default_conversion (rval);
1329
1330  /* Signature pointers are not dereferenced.  */
1331  if (TYPE_LANG_SPECIFIC (TREE_TYPE (last_rval))
1332      && IS_SIGNATURE_POINTER (TREE_TYPE (last_rval)))
1333    return last_rval;
1334
1335  if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1336    return build_indirect_ref (last_rval, NULL_PTR);
1337
1338  if (types_memoized)
1339    error ("result of `operator->()' yields non-pointer result");
1340  else
1341    error ("base operand of `->' is not a pointer");
1342  return error_mark_node;
1343}
1344
1345/* Make an expression to refer to the COMPONENT field of
1346   structure or union value DATUM.  COMPONENT is an arbitrary
1347   expression.  DATUM has not already been checked out to be of
1348   aggregate type.
1349
1350   For C++, COMPONENT may be a TREE_LIST.  This happens when we must
1351   return an object of member type to a method of the current class,
1352   but there is not yet enough typing information to know which one.
1353   As a special case, if there is only one method by that name,
1354   it is returned.  Otherwise we return an expression which other
1355   routines will have to know how to deal with later.  */
1356
1357tree
1358build_m_component_ref (datum, component)
1359     tree datum, component;
1360{
1361  tree type;
1362  tree objtype = TREE_TYPE (datum);
1363  tree rettype;
1364  tree binfo;
1365
1366  if (processing_template_decl)
1367    return build_min_nt (DOTSTAR_EXPR, datum, component);
1368
1369  if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1370    {
1371      type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1372      rettype = type;
1373    }
1374  else
1375    {
1376      type = TREE_TYPE (TREE_TYPE (component));
1377      rettype = TREE_TYPE (type);
1378    }
1379
1380  if (datum == error_mark_node || component == error_mark_node)
1381    return error_mark_node;
1382
1383  if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1384    {
1385      cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1386      return error_mark_node;
1387    }
1388
1389  if (TREE_CODE (objtype) == REFERENCE_TYPE)
1390    objtype = TREE_TYPE (objtype);
1391  objtype = TYPE_MAIN_VARIANT (objtype);
1392
1393  if (! IS_AGGR_TYPE (objtype))
1394    {
1395      cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1396      cp_error ("which is of non-aggregate type `%T'", objtype);
1397      return error_mark_node;
1398    }
1399
1400  binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1401  if (binfo == NULL_TREE)
1402    {
1403      cp_error ("member type `%T::' incompatible with object type `%T'",
1404		TYPE_METHOD_BASETYPE (type), objtype);
1405      return error_mark_node;
1406    }
1407  else if (binfo == error_mark_node)
1408    return error_mark_node;
1409
1410  component = build (OFFSET_REF, rettype, datum, component);
1411  if (TREE_CODE (type) == OFFSET_TYPE)
1412    component = resolve_offset_ref (component);
1413  return component;
1414}
1415
1416/* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
1417
1418tree
1419build_functional_cast (exp, parms)
1420     tree exp;
1421     tree parms;
1422{
1423  /* This is either a call to a constructor,
1424     or a C cast in C++'s `functional' notation.  */
1425  tree type;
1426
1427  if (exp == error_mark_node || parms == error_mark_node)
1428    return error_mark_node;
1429
1430  if (TREE_CODE (exp) == IDENTIFIER_NODE)
1431    {
1432      if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1433	/* Either an enum or an aggregate type.  */
1434	type = IDENTIFIER_TYPE_VALUE (exp);
1435      else
1436	{
1437	  type = lookup_name (exp, 1);
1438	  if (!type || TREE_CODE (type) != TYPE_DECL)
1439	    {
1440	      cp_error ("`%T' fails to be a typedef or built-in type", exp);
1441	      return error_mark_node;
1442	    }
1443	  type = TREE_TYPE (type);
1444	}
1445    }
1446  else if (TREE_CODE (exp) == TYPE_DECL)
1447    type = TREE_TYPE (exp);
1448  else
1449    type = exp;
1450
1451  if (processing_template_decl)
1452    return build_min (CAST_EXPR, type, parms);
1453
1454  if (IS_SIGNATURE (type))
1455    {
1456      error ("signature type not allowed in cast or constructor expression");
1457      return error_mark_node;
1458    }
1459
1460  if (! IS_AGGR_TYPE (type))
1461    {
1462      /* this must build a C cast */
1463      if (parms == NULL_TREE)
1464	parms = integer_zero_node;
1465      else
1466	{
1467	  if (TREE_CHAIN (parms) != NULL_TREE)
1468	    pedwarn ("initializer list being treated as compound expression");
1469	  parms = build_compound_expr (parms);
1470	}
1471
1472      return build_c_cast (type, parms);
1473    }
1474
1475  /* Prepare to evaluate as a call to a constructor.  If this expression
1476     is actually used, for example,
1477
1478     return X (arg1, arg2, ...);
1479
1480     then the slot being initialized will be filled in.  */
1481
1482  if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
1483    {
1484      cp_error ("type `%T' is not yet defined", type);
1485      return error_mark_node;
1486    }
1487  if (IS_AGGR_TYPE (type) && CLASSTYPE_ABSTRACT_VIRTUALS (type))
1488    {
1489      abstract_virtuals_error (NULL_TREE, type);
1490      return error_mark_node;
1491    }
1492
1493  if (parms && TREE_CHAIN (parms) == NULL_TREE)
1494    return build_c_cast (type, TREE_VALUE (parms));
1495
1496  /* We need to zero-initialize POD types.  Let's do that for everything
1497     that doesn't need a constructor.  */
1498  if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1499      && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1500    {
1501      exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
1502      return get_target_expr (exp);
1503    }
1504
1505  exp = build_method_call (NULL_TREE, ctor_identifier, parms,
1506			   TYPE_BINFO (type), LOOKUP_NORMAL);
1507
1508  if (exp == error_mark_node)
1509    return error_mark_node;
1510
1511  return build_cplus_new (type, exp);
1512}
1513
1514/* Return the character string for the name that encodes the
1515   enumeral value VALUE in the domain TYPE.  */
1516
1517char *
1518enum_name_string (value, type)
1519     tree value;
1520     tree type;
1521{
1522  register tree values = TYPE_VALUES (type);
1523  register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value);
1524
1525  my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1526  while (values
1527	 && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval)
1528    values = TREE_CHAIN (values);
1529  if (values == NULL_TREE)
1530    {
1531      char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type));
1532
1533      /* Value must have been cast.  */
1534      sprintf (buf, "(enum %s)%ld",
1535	       TYPE_NAME_STRING (type), (long) intval);
1536      return buf;
1537    }
1538  return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1539}
1540
1541#if 0
1542/* Print out a language-specific error message for
1543   (Pascal) case or (C) switch statements.
1544   CODE tells what sort of message to print.
1545   TYPE is the type of the switch index expression.
1546   NEW is the new value that we were trying to add.
1547   OLD is the old value that stopped us from adding it.  */
1548
1549void
1550report_case_error (code, type, new_value, old_value)
1551     int code;
1552     tree type;
1553     tree new_value, old_value;
1554{
1555  if (code == 1)
1556    {
1557      if (new_value)
1558	error ("case label not within a switch statement");
1559      else
1560	error ("default label not within a switch statement");
1561    }
1562  else if (code == 2)
1563    {
1564      if (new_value == 0)
1565	{
1566	  error ("multiple default labels in one switch");
1567	  return;
1568	}
1569      if (TREE_CODE (new_value) == RANGE_EXPR)
1570	if (TREE_CODE (old_value) == RANGE_EXPR)
1571	  {
1572	    char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1573	    if (TREE_CODE (type) == ENUMERAL_TYPE)
1574	      sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1575		       enum_name_string (TREE_OPERAND (new_value, 0), type),
1576		       enum_name_string (TREE_OPERAND (new_value, 1), type),
1577		       enum_name_string (TREE_OPERAND (old_value, 0), type),
1578		       enum_name_string (TREE_OPERAND (old_value, 1), type));
1579	    else
1580	      sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1581		       TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1582		       TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1583		       TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1584		       TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1585	    error (buf);
1586	  }
1587	else
1588	  {
1589	    char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1590	    if (TREE_CODE (type) == ENUMERAL_TYPE)
1591	      sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1592		       enum_name_string (TREE_OPERAND (new_value, 0), type),
1593		       enum_name_string (TREE_OPERAND (new_value, 1), type),
1594		       enum_name_string (old_value, type));
1595	    else
1596	      sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1597		       TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1598		       TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1599		       TREE_INT_CST_LOW (old_value));
1600	    error (buf);
1601	  }
1602      else if (TREE_CODE (old_value) == RANGE_EXPR)
1603	{
1604	  char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1605	  if (TREE_CODE (type) == ENUMERAL_TYPE)
1606	    sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1607		     enum_name_string (TREE_OPERAND (old_value, 0), type),
1608		     enum_name_string (TREE_OPERAND (old_value, 1), type),
1609		     enum_name_string (new_value, type));
1610	  else
1611	    sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1612		     TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1613		     TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1614		     TREE_INT_CST_LOW (new_value));
1615	  error (buf);
1616	}
1617      else
1618	{
1619	  if (TREE_CODE (type) == ENUMERAL_TYPE)
1620	    error ("duplicate label `%s' in switch statement",
1621		   enum_name_string (new_value, type));
1622	  else
1623	    error ("duplicate label (%d) in switch statement",
1624		   TREE_INT_CST_LOW (new_value));
1625	}
1626    }
1627  else if (code == 3)
1628    {
1629      if (TREE_CODE (type) == ENUMERAL_TYPE)
1630	warning ("case value out of range for enum %s",
1631		 TYPE_NAME_STRING (type));
1632      else
1633	warning ("case value out of range");
1634    }
1635  else if (code == 4)
1636    {
1637      if (TREE_CODE (type) == ENUMERAL_TYPE)
1638	error ("range values `%s' and `%s' reversed",
1639	       enum_name_string (new_value, type),
1640	       enum_name_string (old_value, type));
1641      else
1642	error ("range values reversed");
1643    }
1644}
1645#endif
1646
1647/* Complain about defining new types in inappropriate places.  We give an
1648   exception for C-style casts, to accommodate GNU C stylings.  */
1649
1650void
1651check_for_new_type (string, inptree)
1652     const char *string;
1653     flagged_type_tree inptree;
1654{
1655  if (inptree.new_type_flag
1656      && (pedantic || strcmp (string, "cast") != 0))
1657    pedwarn ("ANSI C++ forbids defining types within %s",string);
1658}
1659