OSDN Git Service

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