OSDN Git Service

90th 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              && ! (TREE_TYPE (init)
834                    && TYPE_PTRMEMFUNC_P (TREE_TYPE (init))))
835         {
836           cp_pedwarn ("braces around scalar initializer for `%T'", type);
837           init = CONSTRUCTOR_ELTS (init);
838           if (TREE_CHAIN (init))
839             cp_pedwarn ("ignoring extra initializers for `%T'", type);
840           init = TREE_VALUE (init);
841         }
842
843       return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
844                                          "initialization", NULL_TREE, 0);
845     }
846
847   /* Come here only for records and arrays (and unions with constructors).  */
848
849   if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
850     {
851       cp_error ("variable-sized object of type `%T' may not be initialized",
852                 type);
853       return error_mark_node;
854     }
855
856   if (code == ARRAY_TYPE || code == RECORD_TYPE || code == UNION_TYPE)
857     {
858       if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type))
859         {
860           cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
861                     type, init);
862           return error_mark_node;
863         }
864       else if (raw_constructor)
865         return process_init_constructor (type, init, (tree *)0);
866       else if (TYPE_NON_AGGREGATE_CLASS (type))
867         {
868           int flags = LOOKUP_NORMAL;
869           /* Initialization from { } is copy-initialization.  */
870           if (tail)
871             flags |= LOOKUP_ONLYCONVERTING;
872           return convert_for_initialization (0, type, init, flags,
873                                              "initialization", NULL_TREE, 0);
874         }
875       else if (tail != 0)
876         {
877           *tail = old_tail_contents;
878           return process_init_constructor (type, 0, tail);
879         }
880
881       if (code != ARRAY_TYPE)
882         return convert_for_initialization (NULL_TREE, type, init, LOOKUP_NORMAL,
883                                            "initialization", NULL_TREE, 0);
884     }
885
886   error ("invalid initializer");
887   return error_mark_node;
888 }
889 \f
890 /* Process a constructor for a variable of type TYPE.
891    The constructor elements may be specified either with INIT or with ELTS,
892    only one of which should be non-null.
893
894    If INIT is specified, it is a CONSTRUCTOR node which is specifically
895    and solely for initializing this datum.
896
897    If ELTS is specified, it is the address of a variable containing
898    a list of expressions.  We take as many elements as we need
899    from the head of the list and update the list.
900
901    In the resulting constructor, TREE_CONSTANT is set if all elts are
902    constant, and TREE_STATIC is set if, in addition, all elts are simple enough
903    constants that the assembler and linker can compute them.  */
904
905 static tree
906 process_init_constructor (type, init, elts)
907      tree type, init, *elts;
908 {
909   register tree tail;
910   /* List of the elements of the result constructor,
911      in reverse order.  */
912   register tree members = NULL;
913   tree result;
914   int allconstant = 1;
915   int allsimple = 1;
916   int erroneous = 0;
917
918   /* Make TAIL be the list of elements to use for the initialization,
919      no matter how the data was given to us.  */
920
921   if (elts)
922     {
923       if (warn_missing_braces)
924         warning ("aggregate has a partly bracketed initializer");
925       tail = *elts;
926     }
927   else
928     tail = CONSTRUCTOR_ELTS (init);
929
930   /* Gobble as many elements as needed, and make a constructor or initial value
931      for each element of this aggregate.  Chain them together in result.
932      If there are too few, use 0 for each scalar ultimate component.  */
933
934   if (TREE_CODE (type) == ARRAY_TYPE)
935     {
936       tree domain = TYPE_DOMAIN (type);
937       register long len;
938       register int i;
939
940       if (domain)
941         len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
942                - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
943                + 1);
944       else
945         len = -1;  /* Take as many as there are */
946
947       for (i = 0; (len < 0 || i < len) && tail != 0; i++)
948         {
949           register tree next1;
950
951           if (TREE_VALUE (tail) != 0)
952             {
953               tree tail1 = tail;
954               next1 = digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
955                                    TREE_VALUE (tail), &tail1);
956               if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type))
957                   && TYPE_MAIN_VARIANT (TREE_TYPE (type)) != TYPE_MAIN_VARIANT (TREE_TYPE (next1)))
958                 {
959                   /* The fact this needs to be done suggests this code needs
960                      to be totally rewritten.  */
961                   next1 = convert_for_initialization (NULL_TREE, TREE_TYPE (type), next1, LOOKUP_NORMAL, "initialization", NULL_TREE, 0);
962                 }
963               my_friendly_assert (tail1 == 0
964                                   || TREE_CODE (tail1) == TREE_LIST, 319);
965               if (tail == tail1 && len < 0)
966                 {
967                   error ("non-empty initializer for array of empty elements");
968                   /* Just ignore what we were supposed to use.  */
969                   tail1 = NULL_TREE;
970                 }
971               tail = tail1;
972             }
973           else
974             {
975               next1 = error_mark_node;
976               tail = TREE_CHAIN (tail);
977             }
978
979           if (next1 == error_mark_node)
980             erroneous = 1;
981           else if (!TREE_CONSTANT (next1))
982             allconstant = 0;
983           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
984             allsimple = 0;
985           members = tree_cons (NULL_TREE, next1, members);
986         }
987     }
988   if (TREE_CODE (type) == RECORD_TYPE)
989     {
990       register tree field;
991
992       if (tail)
993         {
994           if (TYPE_USES_VIRTUAL_BASECLASSES (type))
995             {
996               sorry ("initializer list for object of class with virtual baseclasses");
997               return error_mark_node;
998             }
999
1000           if (TYPE_BINFO_BASETYPES (type))
1001             {
1002               sorry ("initializer list for object of class with baseclasses");
1003               return error_mark_node;
1004             }
1005
1006           if (TYPE_VIRTUAL_P (type))
1007             {
1008               sorry ("initializer list for object using virtual functions");
1009               return error_mark_node;
1010             }
1011         }
1012
1013       for (field = TYPE_FIELDS (type); field && tail;
1014            field = TREE_CHAIN (field))
1015         {
1016           register tree next1;
1017
1018           if (! DECL_NAME (field))
1019             {
1020               members = tree_cons (field, integer_zero_node, members);
1021               continue;
1022             }
1023
1024           if (TREE_CODE (field) != FIELD_DECL)
1025             continue;
1026
1027           if (TREE_VALUE (tail) != 0)
1028             {
1029               tree tail1 = tail;
1030
1031               next1 = digest_init (TREE_TYPE (field),
1032                                    TREE_VALUE (tail), &tail1);
1033               my_friendly_assert (tail1 == 0
1034                                   || TREE_CODE (tail1) == TREE_LIST, 320);
1035               tail = tail1;
1036             }
1037           else
1038             {
1039               next1 = error_mark_node;
1040               tail = TREE_CHAIN (tail);
1041             }
1042
1043           if (next1 == error_mark_node)
1044             erroneous = 1;
1045           else if (!TREE_CONSTANT (next1))
1046             allconstant = 0;
1047           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1048             allsimple = 0;
1049           members = tree_cons (field, next1, members);
1050         }
1051       for (; field; field = TREE_CHAIN (field))
1052         {
1053           if (TREE_CODE (field) != FIELD_DECL)
1054             continue;
1055
1056           /* Does this field have a default initialization?  */
1057           if (DECL_INITIAL (field))
1058             {
1059               register tree next1 = DECL_INITIAL (field);
1060               if (TREE_CODE (next1) == ERROR_MARK)
1061                 erroneous = 1;
1062               else if (!TREE_CONSTANT (next1))
1063                 allconstant = 0;
1064               else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1065                 allsimple = 0;
1066               members = tree_cons (field, next1, members);
1067             }
1068           else if (TREE_READONLY (field))
1069             error ("uninitialized const member `%s'",
1070                    IDENTIFIER_POINTER (DECL_NAME (field)));
1071           else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
1072                    && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1073             error ("member `%s' with uninitialized const fields",
1074                    IDENTIFIER_POINTER (DECL_NAME (field)));
1075           else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1076             error ("member `%s' is uninitialized reference",
1077                    IDENTIFIER_POINTER (DECL_NAME (field)));
1078         }
1079     }
1080
1081   if (TREE_CODE (type) == UNION_TYPE)
1082     {
1083       register tree field = TYPE_FIELDS (type);
1084       register tree next1;
1085
1086       /* Find the first named field.  ANSI decided in September 1990
1087          that only named fields count here.  */
1088       while (field && (DECL_NAME (field) == 0
1089                        || TREE_CODE (field) != FIELD_DECL))
1090         field = TREE_CHAIN (field);
1091
1092       /* If this element specifies a field, initialize via that field.  */
1093       if (TREE_PURPOSE (tail) != NULL_TREE)
1094         {
1095           int win = 0;
1096
1097           if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
1098             /* Handle the case of a call by build_c_cast.  */
1099             field = TREE_PURPOSE (tail), win = 1;
1100           else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
1101             error ("index value instead of field name in union initializer");
1102           else
1103             {
1104               tree temp;
1105               for (temp = TYPE_FIELDS (type);
1106                    temp;
1107                    temp = TREE_CHAIN (temp))
1108                 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
1109                   break;
1110               if (temp)
1111                 field = temp, win = 1;
1112               else
1113                 error ("no field `%s' in union being initialized",
1114                        IDENTIFIER_POINTER (TREE_PURPOSE (tail)));
1115             }
1116           if (!win)
1117             TREE_VALUE (tail) = error_mark_node;
1118         }
1119       else if (field == 0)
1120         {
1121           cp_error ("union `%T' with no named members cannot be initialized",
1122                     type);
1123           TREE_VALUE (tail) = error_mark_node;
1124         }
1125
1126       if (TREE_VALUE (tail) != 0)
1127         {
1128           tree tail1 = tail;
1129
1130           next1 = digest_init (TREE_TYPE (field),
1131                                TREE_VALUE (tail), &tail1);
1132           if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
1133             my_friendly_abort (357);
1134           tail = tail1;
1135         }
1136       else
1137         {
1138           next1 = error_mark_node;
1139           tail = TREE_CHAIN (tail);
1140         }
1141
1142       if (next1 == error_mark_node)
1143         erroneous = 1;
1144       else if (!TREE_CONSTANT (next1))
1145         allconstant = 0;
1146       else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
1147         allsimple = 0;
1148       members = tree_cons (field, next1, members);
1149     }
1150
1151   /* If arguments were specified as a list, just remove the ones we used.  */
1152   if (elts)
1153     *elts = tail;
1154   /* If arguments were specified as a constructor,
1155      complain unless we used all the elements of the constructor.  */
1156   else if (tail)
1157     pedwarn ("excess elements in aggregate initializer");
1158
1159   if (erroneous)
1160     return error_mark_node;
1161
1162   result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1163   if (init)
1164     TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1165   if (allconstant) TREE_CONSTANT (result) = 1;
1166   if (allconstant && allsimple) TREE_STATIC (result) = 1;
1167   return result;
1168 }
1169 \f
1170 /* Given a structure or union value DATUM, construct and return
1171    the structure or union component which results from narrowing
1172    that value by the type specified in BASETYPE.  For example, given the
1173    hierarchy
1174
1175    class L { int ii; };
1176    class A : L { ... };
1177    class B : L { ... };
1178    class C : A, B { ... };
1179
1180    and the declaration
1181
1182    C x;
1183
1184    then the expression
1185
1186    x.A::ii refers to the ii member of the L part of
1187    of A part of the C object named by X.  In this case,
1188    DATUM would be x, and BASETYPE would be A.  */
1189
1190 tree
1191 build_scoped_ref (datum, basetype)
1192      tree datum;
1193      tree basetype;
1194 {
1195   tree ref;
1196   tree type = TREE_TYPE (datum);
1197
1198   if (datum == error_mark_node)
1199     return error_mark_node;
1200
1201   if (TREE_CODE (type) == REFERENCE_TYPE)
1202     type = TREE_TYPE (type);
1203
1204   type = TYPE_MAIN_VARIANT (type);
1205
1206   /* This is an easy conversion.  */
1207   if (is_aggr_type (basetype, 1))
1208     {
1209       tree binfo = TYPE_BINFO (basetype);
1210       if (binfo != TYPE_BINFO (type))
1211         {
1212           binfo = get_binfo (binfo, type, 1);
1213           if (binfo == error_mark_node)
1214             return error_mark_node;
1215           if (binfo == 0)
1216             return error_not_base_type (basetype, type);
1217         }
1218
1219       switch (TREE_CODE (datum))
1220         {
1221         case NOP_EXPR:
1222         case CONVERT_EXPR:
1223         case FLOAT_EXPR:
1224         case FIX_TRUNC_EXPR:
1225         case FIX_FLOOR_EXPR:
1226         case FIX_ROUND_EXPR:
1227         case FIX_CEIL_EXPR:
1228           ref = convert_pointer_to (binfo,
1229                                     build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0));
1230           break;
1231         default:
1232           ref = convert_pointer_to (binfo,
1233                                     build_unary_op (ADDR_EXPR, datum, 0));
1234         }
1235       return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1236     }
1237   return error_mark_node;
1238 }
1239
1240 /* Build a reference to an object specified by the C++ `->' operator.
1241    Usually this just involves dereferencing the object, but if the
1242    `->' operator is overloaded, then such overloads must be
1243    performed until an object which does not have the `->' operator
1244    overloaded is found.  An error is reported when circular pointer
1245    delegation is detected.  */
1246
1247 tree
1248 build_x_arrow (datum)
1249      tree datum;
1250 {
1251   tree types_memoized = NULL_TREE;
1252   register tree rval = datum;
1253   tree type = TREE_TYPE (rval);
1254   tree last_rval;
1255
1256   if (type == error_mark_node)
1257     return error_mark_node;
1258
1259   if (processing_template_decl)
1260     return build_min_nt (ARROW_EXPR, rval);
1261
1262   if (TREE_CODE (rval) == OFFSET_REF)
1263     {
1264       rval = resolve_offset_ref (datum);
1265       type = TREE_TYPE (rval);
1266     }
1267
1268   if (TREE_CODE (type) == REFERENCE_TYPE)
1269     {
1270       rval = convert_from_reference (rval);
1271       type = TREE_TYPE (rval);
1272     }
1273
1274   if (IS_AGGR_TYPE (type) && TYPE_OVERLOADS_ARROW (complete_type (type)))
1275     {
1276       while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval, NULL_TREE, NULL_TREE)))
1277         {
1278           if (rval == error_mark_node)
1279             return error_mark_node;
1280
1281           if (value_member (TREE_TYPE (rval), types_memoized))
1282             {
1283               error ("circular pointer delegation detected");
1284               return error_mark_node;
1285             }
1286           else
1287             {
1288               types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1289                                           types_memoized);
1290             }
1291           last_rval = rval;
1292         }     
1293       if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1294         last_rval = convert_from_reference (last_rval);
1295     }
1296   else
1297     last_rval = default_conversion (rval);
1298
1299   /* Signature pointers are not dereferenced.  */
1300   if (TYPE_LANG_SPECIFIC (TREE_TYPE (last_rval))
1301       && IS_SIGNATURE_POINTER (TREE_TYPE (last_rval)))
1302     return last_rval;
1303
1304   if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1305     return build_indirect_ref (last_rval, NULL_PTR);
1306
1307   if (types_memoized)
1308     error ("result of `operator->()' yields non-pointer result");
1309   else
1310     error ("base operand of `->' is not a pointer");
1311   return error_mark_node;
1312 }
1313
1314 /* Make an expression to refer to the COMPONENT field of
1315    structure or union value DATUM.  COMPONENT is an arbitrary
1316    expression.  DATUM has not already been checked out to be of
1317    aggregate type.
1318
1319    For C++, COMPONENT may be a TREE_LIST.  This happens when we must
1320    return an object of member type to a method of the current class,
1321    but there is not yet enough typing information to know which one.
1322    As a special case, if there is only one method by that name,
1323    it is returned.  Otherwise we return an expression which other
1324    routines will have to know how to deal with later.  */
1325
1326 tree
1327 build_m_component_ref (datum, component)
1328      tree datum, component;
1329 {
1330   tree type;
1331   tree objtype = TREE_TYPE (datum);
1332   tree rettype;
1333   tree binfo;
1334
1335   if (processing_template_decl)
1336     return build_min_nt (DOTSTAR_EXPR, datum, component);
1337
1338   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1339     {
1340       type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1341       rettype = type;
1342     }
1343   else
1344     {
1345       component = build_indirect_ref (component, NULL_PTR);
1346       type = TREE_TYPE (component);
1347       rettype = TREE_TYPE (type);
1348     }
1349
1350   if (datum == error_mark_node || component == error_mark_node)
1351     return error_mark_node;
1352
1353   if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1354     {
1355       cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1356       return error_mark_node;
1357     }
1358
1359   if (TREE_CODE (objtype) == REFERENCE_TYPE)
1360     objtype = TREE_TYPE (objtype);
1361   objtype = TYPE_MAIN_VARIANT (objtype);
1362
1363   if (! IS_AGGR_TYPE (objtype))
1364     {
1365       cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1366       cp_error ("which is of non-aggregate type `%T'", objtype);
1367       return error_mark_node;
1368     }
1369
1370   binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1371   if (binfo == NULL_TREE)
1372     {
1373       cp_error ("member type `%T::' incompatible with object type `%T'",
1374                 TYPE_METHOD_BASETYPE (type), objtype);
1375       return error_mark_node;
1376     }
1377   else if (binfo == error_mark_node)
1378     return error_mark_node;
1379
1380   return build (OFFSET_REF, rettype, datum, component);
1381 }
1382
1383 /* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
1384
1385 tree
1386 build_functional_cast (exp, parms)
1387      tree exp;
1388      tree parms;
1389 {
1390   tree binfo;
1391
1392   /* This is either a call to a constructor,
1393      or a C cast in C++'s `functional' notation.  */
1394   tree type;
1395
1396   if (exp == error_mark_node || parms == error_mark_node)
1397     return error_mark_node;
1398
1399   if (TREE_CODE (exp) == IDENTIFIER_NODE)
1400     {
1401       if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1402         /* Either an enum or an aggregate type.  */
1403         type = IDENTIFIER_TYPE_VALUE (exp);
1404       else
1405         {
1406           type = lookup_name (exp, 1);
1407           if (!type || TREE_CODE (type) != TYPE_DECL)
1408             {
1409               cp_error ("`%T' fails to be a typedef or built-in type", exp);
1410               return error_mark_node;
1411             }
1412           type = TREE_TYPE (type);
1413         }
1414     }
1415   else if (TREE_CODE (exp) == TYPE_DECL)
1416     type = TREE_TYPE (exp);
1417   else
1418     type = exp;
1419
1420   if (processing_template_decl)
1421     return build_min (CAST_EXPR, type, parms);
1422
1423   if (IS_SIGNATURE (type))
1424     {
1425       error ("signature type not allowed in cast or constructor expression");
1426       return error_mark_node;
1427     }
1428
1429   if (! IS_AGGR_TYPE (type))
1430     {
1431       /* this must build a C cast */
1432       if (parms == NULL_TREE)
1433         parms = integer_zero_node;
1434       else
1435         {
1436           if (TREE_CHAIN (parms) != NULL_TREE)
1437             pedwarn ("initializer list being treated as compound expression");
1438           parms = build_compound_expr (parms);
1439         }
1440
1441       return build_c_cast (type, parms);
1442     }
1443
1444   /* Prepare to evaluate as a call to a constructor.  If this expression
1445      is actually used, for example,
1446          
1447      return X (arg1, arg2, ...);
1448          
1449      then the slot being initialized will be filled in.  */
1450
1451   if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
1452     {
1453       cp_error ("type `%T' is not yet defined", type);
1454       return error_mark_node;
1455     }
1456
1457   if (parms && TREE_CHAIN (parms) == NULL_TREE)
1458     return build_c_cast (type, TREE_VALUE (parms));
1459
1460   exp = build_method_call (NULL_TREE, ctor_identifier, parms,
1461                            TYPE_BINFO (type), LOOKUP_NORMAL);
1462
1463   if (exp == error_mark_node)
1464     return error_mark_node;
1465
1466   return build_cplus_new (type, exp);
1467 }
1468 \f
1469 /* Return the character string for the name that encodes the
1470    enumeral value VALUE in the domain TYPE.  */
1471
1472 char *
1473 enum_name_string (value, type)
1474      tree value;
1475      tree type;
1476 {
1477   register tree values = TYPE_VALUES (type);
1478   register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value);
1479
1480   my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1481   while (values
1482          && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval)
1483     values = TREE_CHAIN (values);
1484   if (values == NULL_TREE)
1485     {
1486       char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type));
1487
1488       /* Value must have been cast.  */
1489       sprintf (buf, "(enum %s)%d",
1490                TYPE_NAME_STRING (type), intval);
1491       return buf;
1492     }
1493   return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1494 }
1495
1496 #if 0
1497 /* Print out a language-specific error message for
1498    (Pascal) case or (C) switch statements.
1499    CODE tells what sort of message to print. 
1500    TYPE is the type of the switch index expression.
1501    NEW is the new value that we were trying to add.
1502    OLD is the old value that stopped us from adding it.  */
1503
1504 void
1505 report_case_error (code, type, new_value, old_value)
1506      int code;
1507      tree type;
1508      tree new_value, old_value;
1509 {
1510   if (code == 1)
1511     {
1512       if (new_value)
1513         error ("case label not within a switch statement");
1514       else
1515         error ("default label not within a switch statement");
1516     }
1517   else if (code == 2)
1518     {
1519       if (new_value == 0)
1520         {
1521           error ("multiple default labels in one switch");
1522           return;
1523         }
1524       if (TREE_CODE (new_value) == RANGE_EXPR)
1525         if (TREE_CODE (old_value) == RANGE_EXPR)
1526           {
1527             char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1528             if (TREE_CODE (type) == ENUMERAL_TYPE)
1529               sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1530                        enum_name_string (TREE_OPERAND (new_value, 0), type),
1531                        enum_name_string (TREE_OPERAND (new_value, 1), type),
1532                        enum_name_string (TREE_OPERAND (old_value, 0), type),
1533                        enum_name_string (TREE_OPERAND (old_value, 1), type));
1534             else
1535               sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1536                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1537                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1538                        TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1539                        TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1540             error (buf);
1541           }
1542         else
1543           {
1544             char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1545             if (TREE_CODE (type) == ENUMERAL_TYPE)
1546               sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1547                        enum_name_string (TREE_OPERAND (new_value, 0), type),
1548                        enum_name_string (TREE_OPERAND (new_value, 1), type),
1549                        enum_name_string (old_value, type));
1550             else
1551               sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1552                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1553                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1554                        TREE_INT_CST_LOW (old_value));
1555             error (buf);
1556           }
1557       else if (TREE_CODE (old_value) == RANGE_EXPR)
1558         {
1559           char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1560           if (TREE_CODE (type) == ENUMERAL_TYPE)
1561             sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1562                      enum_name_string (TREE_OPERAND (old_value, 0), type),
1563                      enum_name_string (TREE_OPERAND (old_value, 1), type),
1564                      enum_name_string (new_value, type));
1565           else
1566             sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1567                      TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1568                      TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1569                      TREE_INT_CST_LOW (new_value));
1570           error (buf);
1571         }
1572       else
1573         {
1574           if (TREE_CODE (type) == ENUMERAL_TYPE)
1575             error ("duplicate label `%s' in switch statement",
1576                    enum_name_string (new_value, type));
1577           else
1578             error ("duplicate label (%d) in switch statement",
1579                    TREE_INT_CST_LOW (new_value));
1580         }
1581     }
1582   else if (code == 3)
1583     {
1584       if (TREE_CODE (type) == ENUMERAL_TYPE)
1585         warning ("case value out of range for enum %s",
1586                  TYPE_NAME_STRING (type));
1587       else
1588         warning ("case value out of range");
1589     }
1590   else if (code == 4)
1591     {
1592       if (TREE_CODE (type) == ENUMERAL_TYPE)
1593         error ("range values `%s' and `%s' reversed",
1594                enum_name_string (new_value, type),
1595                enum_name_string (old_value, type));
1596       else
1597         error ("range values reversed");
1598     }
1599 }
1600 #endif
1601
1602 void
1603 check_for_new_type (string, inptree)
1604      char *string;
1605      flagged_type_tree inptree;
1606 {
1607   if (pedantic && inptree.new_type_flag)
1608     pedwarn ("ANSI C++ forbids defining types within %s",string);
1609 }