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