OSDN Git Service

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