OSDN Git Service

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