OSDN Git Service

58th Cygnus<->FSF merge
[pf3gnuchains/gcc-fork.git] / gcc / cp / typeck2.c
1 /* Report error messages, build initializers, and perform
2    some front-end optimizations for C++ compiler.
3    Copyright (C) 1987, 88, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
4    Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 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 TYPE_BINFO (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: 366.  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 null_pointer_node if the value is absolute;
383    if it is relocatable, return the variable that determines the relocation.
384    We assume that VALUE has been folded as much as possible;
385    therefore, we do not need to check for such things as
386    arithmetic-combinations of integers.  */
387
388 tree
389 initializer_constant_valid_p (value, endtype)
390      tree value;
391      tree endtype;
392 {
393   switch (TREE_CODE (value))
394     {
395     case CONSTRUCTOR:
396       if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
397           && TREE_CONSTANT (value))
398         return
399           initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
400                                         endtype);
401         
402       return TREE_STATIC (value) ? null_pointer_node : 0;
403
404     case INTEGER_CST:
405     case REAL_CST:
406     case STRING_CST:
407     case COMPLEX_CST:
408       return null_pointer_node;
409
410     case ADDR_EXPR:
411       return TREE_OPERAND (value, 0);
412
413     case NON_LVALUE_EXPR:
414       return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
415
416     case CONVERT_EXPR:
417     case NOP_EXPR:
418       /* Allow conversions between pointer types.  */
419       if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
420           && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
421         return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
422
423       /* Allow conversions between real types.  */
424       if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
425           && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
426         return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
427
428       /* Allow length-preserving conversions between integer types.  */
429       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_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), endtype);
434
435       /* Allow conversions between other integer types only if
436          explicit value.  */
437       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
438           && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
439         {
440           tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
441                                                      endtype);
442           if (inner == null_pointer_node)
443             return null_pointer_node;
444           return 0;
445         }
446
447       /* Allow (int) &foo provided int is as wide as a pointer.  */
448       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
449           && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
450           && (TYPE_PRECISION (TREE_TYPE (value))
451               >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
452         return initializer_constant_valid_p (TREE_OPERAND (value, 0),
453                                              endtype);
454
455       /* Likewise conversions from int to pointers.  */
456       if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
457           && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
458           && (TYPE_PRECISION (TREE_TYPE (value))
459               <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
460         return initializer_constant_valid_p (TREE_OPERAND (value, 0),
461                                              endtype);
462
463       /* Allow conversions to union types if the value inside is okay.  */
464       if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
465         return initializer_constant_valid_p (TREE_OPERAND (value, 0),
466                                              endtype);
467       return 0;
468
469     case PLUS_EXPR:
470       if (TREE_CODE (endtype) == INTEGER_TYPE
471           && TYPE_PRECISION (endtype) < POINTER_SIZE)
472         return 0;
473       {
474         tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
475                                                     endtype);
476         tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
477                                                     endtype);
478         /* If either term is absolute, use the other terms relocation.  */
479         if (valid0 == null_pointer_node)
480           return valid1;
481         if (valid1 == null_pointer_node)
482           return valid0;
483         return 0;
484       }
485
486     case MINUS_EXPR:
487       if (TREE_CODE (endtype) == INTEGER_TYPE
488           && TYPE_PRECISION (endtype) < POINTER_SIZE)
489         return 0;
490       {
491         tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
492                                                     endtype);
493         tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
494                                                     endtype);
495         /* Win if second argument is absolute.  */
496         if (valid1 == null_pointer_node)
497           return valid0;
498         /* Win if both arguments have the same relocation.
499            Then the value is absolute.  */
500         if (valid0 == valid1)
501           return null_pointer_node;
502         return 0;
503       }
504     }
505
506   return 0;
507 }
508 \f
509 /* Perform appropriate conversions on the initial value of a variable,
510    store it in the declaration DECL,
511    and print any error messages that are appropriate.
512    If the init is invalid, store an ERROR_MARK.
513
514    C++: Note that INIT might be a TREE_LIST, which would mean that it is
515    a base class initializer for some aggregate type, hopefully compatible
516    with DECL.  If INIT is a single element, and DECL is an aggregate
517    type, we silently convert INIT into a TREE_LIST, allowing a constructor
518    to be called.
519
520    If INIT is a TREE_LIST and there is no constructor, turn INIT
521    into a CONSTRUCTOR and use standard initialization techniques.
522    Perhaps a warning should be generated?
523
524    Returns value of initializer if initialization could not be
525    performed for static variable.  In that case, caller must do
526    the storing.  */
527
528 tree
529 store_init_value (decl, init)
530      tree decl, init;
531 {
532   register tree value, type;
533
534   /* If variable's type was invalidly declared, just ignore it.  */
535
536   type = TREE_TYPE (decl);
537   if (TREE_CODE (type) == ERROR_MARK)
538     return NULL_TREE;
539
540   /* Take care of C++ business up here.  */
541   type = TYPE_MAIN_VARIANT (type);
542
543   /* implicitly tests if IS_AGGR_TYPE.  */
544   if (TYPE_NEEDS_CONSTRUCTING (type) && TREE_CODE (init) != CONSTRUCTOR)
545     my_friendly_abort (109);
546   else if (IS_AGGR_TYPE (type))
547     {
548       /* Although we are not allowed to declare variables of signature
549          type, we complain about a possible constructor call in such a
550          declaration as well.  */
551       if (TREE_CODE (init) == TREE_LIST
552           && IS_SIGNATURE (type))
553         {
554           cp_error ("constructor syntax cannot be used with signature type `%T'",
555                     type);
556           init = error_mark_node;
557         }
558       else if (TREE_CODE (init) == TREE_LIST)
559         {
560           cp_error ("constructor syntax used, but no constructor declared for type `%T'", type);
561           init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
562         }
563 #if 0
564       if (TREE_CODE (init) == CONSTRUCTOR)
565         {
566           tree field;
567           tree funcs;
568           int func;
569
570           /* Check that we're really an aggregate as ARM 8.4.1 defines it.  */
571           if (CLASSTYPE_N_BASECLASSES (type))
572             cp_error_at ("initializer list construction invalid for derived class object `%D'", decl);
573           if (CLASSTYPE_VTBL_PTR (type))
574             cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl);
575           if (TYPE_NEEDS_CONSTRUCTING (type))
576             {
577               cp_error_at ("initializer list construction invalid for `%D'", decl);
578               error ("due to the presence of a constructor");
579             }
580           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
581             if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
582               {
583                 cp_error_at ("initializer list construction invalid for `%D'", decl);
584                 cp_error_at ("due to non-public access of member `%D'", field);
585               }
586           funcs = TYPE_METHODS (type);
587           if (funcs)
588             for (func = 0; func < TREE_VEC_LENGTH (funcs); func++)
589               {
590                 field = TREE_VEC_ELT (funcs, func);
591                 if (field && (TREE_PRIVATE (field) || TREE_PROTECTED (field)))
592                   {
593                     cp_error_at ("initializer list construction invalid for `%D'", decl);
594                     cp_error_at ("due to non-public access of member `%D'", field);
595                   }
596               }
597         }
598 #endif
599     }
600   else if (TREE_CODE (init) == TREE_LIST
601            && TREE_TYPE (init) != unknown_type_node)
602     {
603       if (TREE_CODE (decl) == RESULT_DECL)
604         {
605           if (TREE_CHAIN (init))
606             {
607               warning ("comma expression used to initialize return value");
608               init = build_compound_expr (init);
609             }
610           else
611             init = TREE_VALUE (init);
612         }
613       else if (TREE_TYPE (init) != 0
614                && TREE_CODE (TREE_TYPE (init)) == OFFSET_TYPE)
615         {
616           /* Use the type of our variable to instantiate
617              the type of our initializer.  */
618           init = instantiate_type (type, init, 1);
619         }
620       else if (TREE_CODE (init) == TREE_LIST
621                && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
622         {
623           error ("cannot initialize arrays using this syntax");
624           return NULL_TREE;
625         }
626       else
627         {
628           /* We get here with code like `int a (2);' */
629              
630           if (TREE_CHAIN (init) != NULL_TREE)
631             {
632               pedwarn ("initializer list being treated as compound expression");
633               init = build_compound_expr (init);
634             }
635           else
636             init = TREE_VALUE (init);
637         }
638     }
639
640   /* End of special C++ code.  */
641
642   /* Digest the specified initializer into an expression.  */
643
644   value = digest_init (type, init, (tree *) 0);
645
646   /* Store the expression if valid; else report error.  */
647
648   if (TREE_CODE (value) == ERROR_MARK)
649     ;
650   else if (TREE_STATIC (decl)
651            && (! TREE_CONSTANT (value)
652                || ! initializer_constant_valid_p (value, TREE_TYPE (value))
653 #if 0
654                /* A STATIC PUBLIC int variable doesn't have to be
655                   run time inited when doing pic.  (mrs) */
656                /* Since ctors and dtors are the only things that can
657                   reference vtables, and they are always written down
658                   the the vtable definition, we can leave the
659                   vtables in initialized data space.
660                   However, other initialized data cannot be initialized
661                   this way.  Instead a global file-level initializer
662                   must do the job.  */
663                || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
664 #endif
665                ))
666
667     return value;
668 #if 0 /* No, that's C.  jason 9/19/94 */
669   else
670     {
671       if (pedantic && TREE_CODE (value) == CONSTRUCTOR
672           /* Don't complain about non-constant initializers of
673              signature tables and signature pointers/references.  */
674           && ! (TYPE_LANG_SPECIFIC (type)
675                 && (IS_SIGNATURE (type)
676                     || IS_SIGNATURE_POINTER (type)
677                     || IS_SIGNATURE_REFERENCE (type))))
678         {
679           if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
680             pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
681         }
682     }
683 #endif
684   DECL_INITIAL (decl) = value;
685   return NULL_TREE;
686 }
687 \f
688 /* Digest the parser output INIT as an initializer for type TYPE.
689    Return a C expression of type TYPE to represent the initial value.
690
691    If TAIL is nonzero, it points to a variable holding a list of elements
692    of which INIT is the first.  We update the list stored there by
693    removing from the head all the elements that we use.
694    Normally this is only one; we use more than one element only if
695    TYPE is an aggregate and INIT is not a constructor.  */
696
697 tree
698 digest_init (type, init, tail)
699      tree type, init, *tail;
700 {
701   enum tree_code code = TREE_CODE (type);
702   tree element = NULL_TREE;
703   tree old_tail_contents;
704   /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
705      tree node which has no TREE_TYPE.  */
706   int raw_constructor;
707
708   /* By default, assume we use one element from a list.
709      We correct this later in the sole case where it is not true.  */
710
711   if (tail)
712     {
713       old_tail_contents = *tail;
714       *tail = TREE_CHAIN (*tail);
715     }
716
717   if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
718                                   && TREE_VALUE (init) == error_mark_node))
719     return error_mark_node;
720
721   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
722   if (TREE_CODE (init) == NON_LVALUE_EXPR)
723     init = TREE_OPERAND (init, 0);
724
725   if (init && TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (type))
726     init = default_conversion (init);
727
728   if (init && TYPE_PTRMEMFUNC_P (type)
729       && ((TREE_CODE (init) == ADDR_EXPR
730            && ((TREE_CODE (TREE_TYPE (init)) == POINTER_TYPE
731                 && TREE_CODE (TREE_TYPE (TREE_TYPE (init))) == METHOD_TYPE)
732                || TREE_CODE (TREE_OPERAND (init, 0)) == TREE_LIST))
733           || TREE_CODE (init) == TREE_LIST
734           || integer_zerop (init)
735           || (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))))
736     {
737       return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), init, 0);
738     }
739
740   raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
741
742   if (init && raw_constructor
743       && CONSTRUCTOR_ELTS (init) != 0
744       && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
745     {
746       element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
747       /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
748       if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
749         element = TREE_OPERAND (element, 0);
750       if (element == error_mark_node)
751         return element;
752     }
753
754   /* Any type can be initialized from an expression of the same type,
755      optionally with braces.  */
756
757   if (init && TREE_TYPE (init)
758       && (TYPE_MAIN_VARIANT (TREE_TYPE (init)) == type
759           || (code == ARRAY_TYPE && comptypes (TREE_TYPE (init), type, 1))))
760     {
761       if (pedantic && code == ARRAY_TYPE
762           && TREE_CODE (init) != STRING_CST)
763         pedwarn ("ANSI C++ forbids initializing array from array expression");
764       if (TREE_CODE (init) == CONST_DECL)
765         init = DECL_INITIAL (init);
766       else if (TREE_READONLY_DECL_P (init))
767         init = decl_constant_value (init);
768       return init;
769     }
770
771   if (element && (TREE_TYPE (element) == type
772                   || (code == ARRAY_TYPE && TREE_TYPE (element)
773                       && comptypes (TREE_TYPE (element), type, 1))))
774     {
775       if (pedantic && code == ARRAY_TYPE)
776         pedwarn ("ANSI C++ forbids initializing array from array expression");
777       if (pedantic && (code == RECORD_TYPE || code == UNION_TYPE))
778         pedwarn ("ANSI C++ forbids single nonscalar initializer with braces");
779       if (TREE_CODE (element) == CONST_DECL)
780         element = DECL_INITIAL (element);
781       else if (TREE_READONLY_DECL_P (element))
782         element = decl_constant_value (element);
783       return element;
784     }
785
786   /* Initialization of an array of chars from a string constant
787      optionally enclosed in braces.  */
788
789   if (code == ARRAY_TYPE)
790     {
791       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
792       if ((typ1 == char_type_node
793            || typ1 == signed_char_type_node
794            || typ1 == unsigned_char_type_node
795            || typ1 == unsigned_wchar_type_node
796            || typ1 == signed_wchar_type_node)
797           && ((init && TREE_CODE (init) == STRING_CST)
798               || (element && TREE_CODE (element) == STRING_CST)))
799         {
800           tree string = element ? element : init;
801
802           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
803                != char_type_node)
804               && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
805             {
806               error ("char-array initialized from wide string");
807               return error_mark_node;
808             }
809           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
810                == char_type_node)
811               && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
812             {
813               error ("int-array initialized from non-wide string");
814               return error_mark_node;
815             }
816
817           if (pedantic
818               && typ1 != char_type_node
819               && typ1 != signed_char_type_node
820               && typ1 != unsigned_char_type_node)
821             pedwarn ("ANSI C++ forbids string initializer except for `char' elements");
822           TREE_TYPE (string) = type;
823           if (TYPE_DOMAIN (type) != 0
824               && TREE_CONSTANT (TYPE_SIZE (type)))
825             {
826               register int size
827                 = TREE_INT_CST_LOW (TYPE_SIZE (type));
828               size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
829               /* In C it is ok to subtract 1 from the length of the string
830                  because it's ok to ignore the terminating null char that is
831                  counted in the length of the constant, but in C++ this would
832                  be invalid.  */
833               if (size < TREE_STRING_LENGTH (string))
834                 pedwarn ("initializer-string for array of chars is too long");
835             }
836           return string;
837         }
838     }
839
840   /* Handle scalar types, including conversions,
841      and signature pointers and references.  */
842
843   if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
844       || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
845       || code == BOOLEAN_TYPE
846       || (code == RECORD_TYPE && ! raw_constructor
847           && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))))
848     {
849       if (raw_constructor)
850         {
851           if (element == 0)
852             {
853               error ("initializer for scalar variable requires one element");
854               return error_mark_node;
855             }
856           init = element;
857         }
858       while (TREE_CODE (init) == CONSTRUCTOR)
859         {
860           cp_pedwarn ("braces around scalar initializer for `%T'", type);
861           init = CONSTRUCTOR_ELTS (init);
862           if (TREE_CHAIN (init))
863             cp_pedwarn ("ignoring extra initializers for `%T'", type);
864           init = TREE_VALUE (init);
865         }
866
867       return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
868                                          "initialization", NULL_TREE, 0);
869     }
870
871   /* Come here only for records and arrays (and unions with constructors).  */
872
873   if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
874     {
875       cp_error ("variable-sized object of type `%T' may not be initialized",
876                 type);
877       return error_mark_node;
878     }
879
880   if (code == ARRAY_TYPE || code == RECORD_TYPE || code == UNION_TYPE)
881     {
882       if (raw_constructor)
883         return process_init_constructor (type, init, (tree *)0);
884       else if (TYPE_NEEDS_CONSTRUCTING (type))
885         {
886           /* This can only be reached when caller is initializing
887              ARRAY_TYPE.  In that case, we don't want to convert
888              INIT to TYPE.  We will let `expand_vec_init' do it.  */
889           return init;
890         }
891       else if (tail != 0)
892         {
893           *tail = old_tail_contents;
894           return process_init_constructor (type, 0, tail);
895         }
896       else if (flag_traditional)
897         /* Traditionally one can say `char x[100] = 0;'.  */
898         return process_init_constructor (type,
899                                          build_nt (CONSTRUCTOR, 0,
900                                                    tree_cons (0, init, 0)),
901                                          0);
902       if (code != ARRAY_TYPE)
903         return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
904                                            "initialization", NULL_TREE, 0);
905     }
906
907   error ("invalid initializer");
908   return error_mark_node;
909 }
910 \f
911 /* Process a constructor for a variable of type TYPE.
912    The constructor elements may be specified either with INIT or with ELTS,
913    only one of which should be non-null.
914
915    If INIT is specified, it is a CONSTRUCTOR node which is specifically
916    and solely for initializing this datum.
917
918    If ELTS is specified, it is the address of a variable containing
919    a list of expressions.  We take as many elements as we need
920    from the head of the list and update the list.
921
922    In the resulting constructor, TREE_CONSTANT is set if all elts are
923    constant, and TREE_STATIC is set if, in addition, all elts are simple enough
924    constants that the assembler and linker can compute them.  */
925
926 static tree
927 process_init_constructor (type, init, elts)
928      tree type, init, *elts;
929 {
930   register tree tail;
931   /* List of the elements of the result constructor,
932      in reverse order.  */
933   register tree members = NULL;
934   tree result;
935   int allconstant = 1;
936   int allsimple = 1;
937   int erroneous = 0;
938
939   /* Make TAIL be the list of elements to use for the initialization,
940      no matter how the data was given to us.  */
941
942   if (elts)
943     {
944       if (warn_missing_braces)
945         warning ("aggregate has a partly bracketed initializer");
946       tail = *elts;
947     }
948   else
949     tail = CONSTRUCTOR_ELTS (init);
950
951   /* Gobble as many elements as needed, and make a constructor or initial value
952      for each element of this aggregate.  Chain them together in result.
953      If there are too few, use 0 for each scalar ultimate component.  */
954
955   if (TREE_CODE (type) == ARRAY_TYPE)
956     {
957       tree domain = TYPE_DOMAIN (type);
958       register long len;
959       register int i;
960
961       if (domain)
962         len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
963                - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
964                + 1);
965       else
966         len = -1;  /* Take as many as there are */
967
968       for (i = 0; (len < 0 || i < len) && tail != 0; i++)
969         {
970           register tree next1;
971
972           if (TREE_VALUE (tail) != 0)
973             {
974               tree tail1 = tail;
975               next1 = digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
976                                    TREE_VALUE (tail), &tail1);
977               my_friendly_assert (tail1 == 0
978                                   || TREE_CODE (tail1) == TREE_LIST, 319);
979               if (tail == tail1 && len < 0)
980                 {
981                   error ("non-empty initializer for array of empty elements");
982                   /* Just ignore what we were supposed to use.  */
983                   tail1 = NULL_TREE;
984                 }
985               tail = tail1;
986             }
987           else
988             {
989               next1 = error_mark_node;
990               tail = TREE_CHAIN (tail);
991             }
992
993           if (next1 == error_mark_node)
994             erroneous = 1;
995           else if (!TREE_CONSTANT (next1))
996             allconstant = 0;
997           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
998             allsimple = 0;
999           members = tree_cons (NULL_TREE, next1, members);
1000         }
1001     }
1002   if (TREE_CODE (type) == RECORD_TYPE)
1003     {
1004       register tree field;
1005
1006       if (tail)
1007         {
1008           if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1009             {
1010               sorry ("initializer list for object of class with virtual baseclasses");
1011               return error_mark_node;
1012             }
1013
1014           if (TYPE_BINFO_BASETYPES (type))
1015             {
1016               sorry ("initializer list for object of class with baseclasses");
1017               return error_mark_node;
1018             }
1019
1020           if (TYPE_VIRTUAL_P (type))
1021             {
1022               sorry ("initializer list for object using virtual functions");
1023               return error_mark_node;
1024             }
1025         }
1026
1027       for (field = TYPE_FIELDS (type); field && tail;
1028            field = TREE_CHAIN (field))
1029         {
1030           register tree next1;
1031
1032           if (! DECL_NAME (field))
1033             {
1034               members = tree_cons (field, integer_zero_node, members);
1035               continue;
1036             }
1037
1038           if (TREE_CODE (field) != FIELD_DECL)
1039             continue;
1040
1041           if (TREE_VALUE (tail) != 0)
1042             {
1043               tree tail1 = tail;
1044
1045               next1 = digest_init (TREE_TYPE (field),
1046                                    TREE_VALUE (tail), &tail1);
1047               my_friendly_assert (tail1 == 0
1048                                   || TREE_CODE (tail1) == TREE_LIST, 320);
1049               tail = tail1;
1050             }
1051           else
1052             {
1053               next1 = error_mark_node;
1054               tail = TREE_CHAIN (tail);
1055             }
1056
1057           if (next1 == error_mark_node)
1058             erroneous = 1;
1059           else if (!TREE_CONSTANT (next1))
1060             allconstant = 0;
1061           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1062             allsimple = 0;
1063           members = tree_cons (field, next1, members);
1064         }
1065       for (; field; field = TREE_CHAIN (field))
1066         {
1067           if (TREE_CODE (field) != FIELD_DECL)
1068             continue;
1069
1070           /* Does this field have a default initialization?  */
1071           if (DECL_INITIAL (field))
1072             {
1073               register tree next1 = DECL_INITIAL (field);
1074               if (TREE_CODE (next1) == ERROR_MARK)
1075                 erroneous = 1;
1076               else if (!TREE_CONSTANT (next1))
1077                 allconstant = 0;
1078               else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1079                 allsimple = 0;
1080               members = tree_cons (field, next1, members);
1081             }
1082           else if (TREE_READONLY (field))
1083             error ("uninitialized const member `%s'",
1084                    IDENTIFIER_POINTER (DECL_NAME (field)));
1085           else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
1086                    && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1087             error ("member `%s' with uninitialized const fields",
1088                    IDENTIFIER_POINTER (DECL_NAME (field)));
1089           else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1090             error ("member `%s' is uninitialized reference",
1091                    IDENTIFIER_POINTER (DECL_NAME (field)));
1092         }
1093     }
1094
1095   if (TREE_CODE (type) == UNION_TYPE)
1096     {
1097       register tree field = TYPE_FIELDS (type);
1098       register tree next1;
1099
1100       /* Find the first named field.  ANSI decided in September 1990
1101          that only named fields count here.  */
1102       while (field && DECL_NAME (field) == 0)
1103         field = TREE_CHAIN (field);
1104
1105       /* If this element specifies a field, initialize via that field.  */
1106       if (TREE_PURPOSE (tail) != NULL_TREE)
1107         {
1108           int win = 0;
1109
1110           if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
1111             /* Handle the case of a call by build_c_cast.  */
1112             field = TREE_PURPOSE (tail), win = 1;
1113           else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
1114             error ("index value instead of field name in union initializer");
1115           else
1116             {
1117               tree temp;
1118               for (temp = TYPE_FIELDS (type);
1119                    temp;
1120                    temp = TREE_CHAIN (temp))
1121                 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
1122                   break;
1123               if (temp)
1124                 field = temp, win = 1;
1125               else
1126                 error ("no field `%s' in union being initialized",
1127                        IDENTIFIER_POINTER (TREE_PURPOSE (tail)));
1128             }
1129           if (!win)
1130             TREE_VALUE (tail) = error_mark_node;
1131         }
1132       else if (field == 0)
1133         {
1134           cp_error ("union `%T' with no named members cannot be initialized",
1135                     type);
1136           TREE_VALUE (tail) = error_mark_node;
1137         }
1138
1139       if (TREE_VALUE (tail) != 0)
1140         {
1141           tree tail1 = tail;
1142
1143           next1 = digest_init (TREE_TYPE (field),
1144                                TREE_VALUE (tail), &tail1);
1145           if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
1146             my_friendly_abort (357);
1147           tail = tail1;
1148         }
1149       else
1150         {
1151           next1 = error_mark_node;
1152           tail = TREE_CHAIN (tail);
1153         }
1154
1155       if (next1 == error_mark_node)
1156         erroneous = 1;
1157       else if (!TREE_CONSTANT (next1))
1158         allconstant = 0;
1159       else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
1160         allsimple = 0;
1161       members = tree_cons (field, next1, members);
1162     }
1163
1164   /* If arguments were specified as a list, just remove the ones we used.  */
1165   if (elts)
1166     *elts = tail;
1167   /* If arguments were specified as a constructor,
1168      complain unless we used all the elements of the constructor.  */
1169   else if (tail)
1170     pedwarn ("excess elements in aggregate initializer");
1171
1172   if (erroneous)
1173     return error_mark_node;
1174
1175   result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1176   if (init)
1177     TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1178   if (allconstant) TREE_CONSTANT (result) = 1;
1179   if (allconstant && allsimple) TREE_STATIC (result) = 1;
1180   return result;
1181 }
1182 \f
1183 /* Given a structure or union value DATUM, construct and return
1184    the structure or union component which results from narrowing
1185    that value by the types specified in TYPES.  For example, given the
1186    hierarchy
1187
1188    class L { int ii; };
1189    class A : L { ... };
1190    class B : L { ... };
1191    class C : A, B { ... };
1192
1193    and the declaration
1194
1195    C x;
1196
1197    then the expression
1198
1199    x::C::A::L::ii refers to the ii member of the L part of
1200    of A part of the C object named by X.  In this case,
1201    DATUM would be x, and TYPES would be a SCOPE_REF consisting of
1202
1203         SCOPE_REF
1204                 SCOPE_REF
1205                         C       A
1206                 L
1207
1208    The last entry in the SCOPE_REF is always an IDENTIFIER_NODE.
1209
1210 */
1211
1212 tree
1213 build_scoped_ref (datum, types)
1214      tree datum;
1215      tree types;
1216 {
1217   tree ref;
1218   tree type = TREE_TYPE (datum);
1219
1220   if (datum == error_mark_node)
1221     return error_mark_node;
1222
1223   if (TREE_CODE (type) == REFERENCE_TYPE)
1224     type = TREE_TYPE (type);
1225
1226   type = TYPE_MAIN_VARIANT (type);
1227
1228   if (TREE_CODE (types) == SCOPE_REF)
1229     {
1230       /* We have some work to do.  */
1231       struct type_chain
1232         { tree type; struct type_chain *next; }
1233       *chain = NULL, *head = NULL, scratch;
1234       ref = build_unary_op (ADDR_EXPR, datum, 0);
1235       while (TREE_CODE (types) == SCOPE_REF)
1236         {
1237           tree t = TREE_OPERAND (types, 1);
1238           if (is_aggr_typedef (t, 1))
1239             {
1240               head = (struct type_chain *)alloca (sizeof (struct type_chain));
1241               head->type = IDENTIFIER_TYPE_VALUE (t);
1242               head->next = chain;
1243               chain = head;
1244               types = TREE_OPERAND (types, 0);
1245             }
1246           else return error_mark_node;
1247         }
1248       if (! is_aggr_typedef (types, 1))
1249         return error_mark_node;
1250
1251       head = &scratch;
1252       head->type = IDENTIFIER_TYPE_VALUE (types);
1253       head->next = chain;
1254       chain = head;
1255       while (chain)
1256         {
1257           tree binfo = chain->type;
1258           type = TREE_TYPE (TREE_TYPE (ref));
1259           if (binfo != TYPE_BINFO (type))
1260             {
1261               binfo = get_binfo (binfo, type, 1);
1262               if (binfo == error_mark_node)
1263                 return error_mark_node;
1264               if (binfo == 0)
1265                 return error_not_base_type (chain->type, type);
1266               ref = convert_pointer_to (binfo, ref);
1267             }
1268           chain = chain->next;
1269         }
1270       return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1271     }
1272
1273   /* This is an easy conversion.  */
1274   if (is_aggr_typedef (types, 1))
1275     {
1276       tree binfo = TYPE_BINFO (IDENTIFIER_TYPE_VALUE (types));
1277       if (binfo != TYPE_BINFO (type))
1278         {
1279           binfo = get_binfo (binfo, type, 1);
1280           if (binfo == error_mark_node)
1281             return error_mark_node;
1282           if (binfo == 0)
1283             return error_not_base_type (IDENTIFIER_TYPE_VALUE (types), type);
1284         }
1285
1286       switch (TREE_CODE (datum))
1287         {
1288         case NOP_EXPR:
1289         case CONVERT_EXPR:
1290         case FLOAT_EXPR:
1291         case FIX_TRUNC_EXPR:
1292         case FIX_FLOOR_EXPR:
1293         case FIX_ROUND_EXPR:
1294         case FIX_CEIL_EXPR:
1295           ref = convert_pointer_to (binfo,
1296                                     build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0));
1297           break;
1298         default:
1299           ref = convert_pointer_to (binfo,
1300                                     build_unary_op (ADDR_EXPR, datum, 0));
1301         }
1302       return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1303     }
1304   return error_mark_node;
1305 }
1306
1307 /* Build a reference to an object specified by the C++ `->' operator.
1308    Usually this just involves dereferencing the object, but if the
1309    `->' operator is overloaded, then such overloads must be
1310    performed until an object which does not have the `->' operator
1311    overloaded is found.  An error is reported when circular pointer
1312    delegation is detected.  */
1313 tree
1314 build_x_arrow (datum)
1315      tree datum;
1316 {
1317   tree types_memoized = NULL_TREE;
1318   register tree rval = datum;
1319   tree type = TREE_TYPE (rval);
1320   tree last_rval;
1321
1322   if (type == error_mark_node)
1323     return error_mark_node;
1324
1325   if (TREE_CODE (rval) == OFFSET_REF)
1326     {
1327       rval = resolve_offset_ref (datum);
1328       type = TREE_TYPE (rval);
1329     }
1330
1331   if (TREE_CODE (type) == REFERENCE_TYPE)
1332     {
1333       rval = convert_from_reference (rval);
1334       type = TREE_TYPE (rval);
1335     }
1336
1337   if (IS_AGGR_TYPE (type) && TYPE_OVERLOADS_ARROW (type))
1338     {
1339       while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval, NULL_TREE, NULL_TREE)))
1340         {
1341           if (rval == error_mark_node)
1342             return error_mark_node;
1343
1344           if (value_member (TREE_TYPE (rval), types_memoized))
1345             {
1346               error ("circular pointer delegation detected");
1347               return error_mark_node;
1348             }
1349           else
1350             {
1351               types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1352                                           types_memoized);
1353             }
1354           last_rval = rval;
1355         }     
1356       if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1357         last_rval = convert_from_reference (last_rval);
1358     }
1359   else
1360     last_rval = default_conversion (rval);
1361
1362   /* Signature pointers are not dereferenced.  */
1363   if (TYPE_LANG_SPECIFIC (TREE_TYPE (last_rval))
1364       && IS_SIGNATURE_POINTER (TREE_TYPE (last_rval)))
1365     return last_rval;
1366
1367   if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1368     return build_indirect_ref (last_rval, NULL_PTR);
1369
1370   if (types_memoized)
1371     error ("result of `operator->()' yields non-pointer result");
1372   else
1373     error ("base operand of `->' is not a pointer");
1374   return error_mark_node;
1375 }
1376
1377 /* Make an expression to refer to the COMPONENT field of
1378    structure or union value DATUM.  COMPONENT is an arbitrary
1379    expression.  DATUM has not already been checked out to be of
1380    aggregate type.
1381
1382    For C++, COMPONENT may be a TREE_LIST.  This happens when we must
1383    return an object of member type to a method of the current class,
1384    but there is not yet enough typing information to know which one.
1385    As a special case, if there is only one method by that name,
1386    it is returned.  Otherwise we return an expression which other
1387    routines will have to know how to deal with later.  */
1388 tree
1389 build_m_component_ref (datum, component)
1390      tree datum, component;
1391 {
1392   tree type;
1393   tree objtype = TREE_TYPE (datum);
1394   tree rettype;
1395
1396   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1397     {
1398       type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1399       rettype = type;
1400     }
1401   else
1402     {
1403       component = build_indirect_ref (component, NULL_PTR);
1404       type = TREE_TYPE (component);
1405       rettype = TREE_TYPE (TREE_TYPE (component));
1406     }
1407
1408   if (datum == error_mark_node || component == error_mark_node)
1409     return error_mark_node;
1410
1411   if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1412     {
1413       cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1414       return error_mark_node;
1415     }
1416
1417   if (TREE_CODE (objtype) == REFERENCE_TYPE)
1418     objtype = TREE_TYPE (objtype);
1419   objtype = TYPE_MAIN_VARIANT (objtype);
1420
1421   if (! IS_AGGR_TYPE (objtype))
1422     {
1423       cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1424       cp_error ("which is of non-aggregate type `%T'", objtype);
1425       return error_mark_node;
1426     }
1427   
1428   if (! comptypes (TYPE_METHOD_BASETYPE (type), objtype, 0))
1429     {
1430       cp_error ("member type `%T::' incompatible with object type `%T'",
1431                 TYPE_METHOD_BASETYPE (type), objtype);
1432       return error_mark_node;
1433     }
1434
1435   return build (OFFSET_REF, rettype, datum, component);
1436 }
1437
1438 /* Return a tree node for the expression TYPENAME '(' PARMS ')'.
1439
1440    Because we cannot tell whether this construct is really a call to a
1441    constructor or a request for a type conversion, we try both, and
1442    report any ambiguities we find.  */
1443 tree
1444 build_functional_cast (exp, parms)
1445      tree exp;
1446      tree parms;
1447 {
1448   /* This is either a call to a constructor,
1449      or a C cast in C++'s `functional' notation.  */
1450   tree type, name = NULL_TREE;
1451   tree expr_as_ctor = NULL_TREE;
1452
1453   if (exp == error_mark_node || parms == error_mark_node)
1454     return error_mark_node;
1455
1456   if (TREE_CODE (exp) == IDENTIFIER_NODE)
1457     {
1458       name = exp;
1459
1460       if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1461         /* Either an enum or an aggregate type.  */
1462         type = IDENTIFIER_TYPE_VALUE (exp);
1463       else
1464         {
1465           type = lookup_name (exp, 1);
1466           if (!type || TREE_CODE (type) != TYPE_DECL)
1467             {
1468               cp_error ("`%T' fails to be a typedef or built-in type", name);
1469               return error_mark_node;
1470             }
1471           type = TREE_TYPE (type);
1472         }
1473     }
1474   else
1475     type = exp;
1476
1477   if (IS_SIGNATURE (type))
1478     {
1479       error ("signature type not allowed in cast or constructor expression");
1480       return error_mark_node;
1481     }
1482
1483   /* Prepare to evaluate as a call to a constructor.  If this expression
1484      is actually used, for example,
1485          
1486      return X (arg1, arg2, ...);
1487          
1488      then the slot being initialized will be filled in.  */
1489
1490   if (name == NULL_TREE)
1491     {
1492       name = TYPE_NAME (type);
1493       if (TREE_CODE (name) == TYPE_DECL)
1494         name = DECL_NAME (name);
1495     }
1496
1497   if (! IS_AGGR_TYPE (type))
1498     {
1499       /* this must build a C cast */
1500       if (parms == NULL_TREE)
1501         parms = integer_zero_node;
1502       else
1503         {
1504           if (TREE_CHAIN (parms) != NULL_TREE)
1505             pedwarn ("initializer list being treated as compound expression");
1506           parms = build_compound_expr (parms);
1507         }
1508
1509       return build_c_cast (type, parms, 1);
1510     }
1511
1512   if (TYPE_SIZE (type) == NULL_TREE)
1513     {
1514       cp_error ("type `%T' is not yet defined", type);
1515       return error_mark_node;
1516     }
1517
1518   if (parms && TREE_CHAIN (parms) == NULL_TREE)
1519     return build_c_cast (type, parms, 1);
1520
1521   expr_as_ctor = build_method_call (NULL_TREE, name, parms,
1522                                     NULL_TREE, LOOKUP_NORMAL);
1523
1524   if (expr_as_ctor == error_mark_node)
1525     return error_mark_node;
1526
1527   if (current_function_decl)
1528     return build_cplus_new (type, expr_as_ctor, 1);
1529
1530   {
1531     register tree parm = TREE_OPERAND (expr_as_ctor, 1);
1532
1533     /* Initializers for static variables and parameters have
1534        to handle doing the initialization and cleanup themselves.  */
1535     my_friendly_assert (TREE_CODE (expr_as_ctor) == CALL_EXPR, 322);
1536 #if 0
1537     /* The following assertion fails in cases where we are initializing
1538        a static member variable of a particular instance of a template
1539        class with a call to a constructor of the given instance, as in:
1540        
1541        TMPL<int> object = TMPL<int>();
1542        
1543        Curiously, the assertion does not fail if we do the same thing
1544        for a static member of a non-template class, as in:
1545        
1546        T object = T();
1547        
1548        I can't see why we should care here whether or not the initializer
1549        expression involves a call to `new', so for the time being, it
1550        seems best to just avoid doing this assertion.  */
1551     my_friendly_assert (TREE_CALLS_NEW (TREE_VALUE (parm)), 323);
1552 #endif
1553     TREE_VALUE (parm) = NULL_TREE;
1554     expr_as_ctor = build_indirect_ref (expr_as_ctor, NULL_PTR);
1555     TREE_HAS_CONSTRUCTOR (expr_as_ctor) = 1;
1556   }
1557   return expr_as_ctor;
1558 }
1559 \f
1560 /* Return the character string for the name that encodes the
1561    enumeral value VALUE in the domain TYPE.  */
1562 char *
1563 enum_name_string (value, type)
1564      tree value;
1565      tree type;
1566 {
1567   register tree values = TYPE_VALUES (type);
1568   register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value);
1569
1570   my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1571   while (values
1572          && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval)
1573     values = TREE_CHAIN (values);
1574   if (values == NULL_TREE)
1575     {
1576       char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type));
1577
1578       /* Value must have been cast.  */
1579       sprintf (buf, "(enum %s)%d",
1580                TYPE_NAME_STRING (type), intval);
1581       return buf;
1582     }
1583   return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1584 }
1585
1586 #if 0
1587 /* Print out a language-specific error message for
1588    (Pascal) case or (C) switch statements.
1589    CODE tells what sort of message to print. 
1590    TYPE is the type of the switch index expression.
1591    NEW is the new value that we were trying to add.
1592    OLD is the old value that stopped us from adding it.  */
1593 void
1594 report_case_error (code, type, new_value, old_value)
1595      int code;
1596      tree type;
1597      tree new_value, old_value;
1598 {
1599   if (code == 1)
1600     {
1601       if (new_value)
1602         error ("case label not within a switch statement");
1603       else
1604         error ("default label not within a switch statement");
1605     }
1606   else if (code == 2)
1607     {
1608       if (new_value == 0)
1609         {
1610           error ("multiple default labels in one switch");
1611           return;
1612         }
1613       if (TREE_CODE (new_value) == RANGE_EXPR)
1614         if (TREE_CODE (old_value) == RANGE_EXPR)
1615           {
1616             char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1617             if (TREE_CODE (type) == ENUMERAL_TYPE)
1618               sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1619                        enum_name_string (TREE_OPERAND (new_value, 0), type),
1620                        enum_name_string (TREE_OPERAND (new_value, 1), type),
1621                        enum_name_string (TREE_OPERAND (old_value, 0), type),
1622                        enum_name_string (TREE_OPERAND (old_value, 1), type));
1623             else
1624               sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1625                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1626                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1627                        TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1628                        TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1629             error (buf);
1630           }
1631         else
1632           {
1633             char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1634             if (TREE_CODE (type) == ENUMERAL_TYPE)
1635               sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1636                        enum_name_string (TREE_OPERAND (new_value, 0), type),
1637                        enum_name_string (TREE_OPERAND (new_value, 1), type),
1638                        enum_name_string (old_value, type));
1639             else
1640               sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1641                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1642                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1643                        TREE_INT_CST_LOW (old_value));
1644             error (buf);
1645           }
1646       else if (TREE_CODE (old_value) == RANGE_EXPR)
1647         {
1648           char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1649           if (TREE_CODE (type) == ENUMERAL_TYPE)
1650             sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1651                      enum_name_string (TREE_OPERAND (old_value, 0), type),
1652                      enum_name_string (TREE_OPERAND (old_value, 1), type),
1653                      enum_name_string (new_value, type));
1654           else
1655             sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1656                      TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1657                      TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1658                      TREE_INT_CST_LOW (new_value));
1659           error (buf);
1660         }
1661       else
1662         {
1663           if (TREE_CODE (type) == ENUMERAL_TYPE)
1664             error ("duplicate label `%s' in switch statement",
1665                    enum_name_string (new_value, type));
1666           else
1667             error ("duplicate label (%d) in switch statement",
1668                    TREE_INT_CST_LOW (new_value));
1669         }
1670     }
1671   else if (code == 3)
1672     {
1673       if (TREE_CODE (type) == ENUMERAL_TYPE)
1674         warning ("case value out of range for enum %s",
1675                  TYPE_NAME_STRING (type));
1676       else
1677         warning ("case value out of range");
1678     }
1679   else if (code == 4)
1680     {
1681       if (TREE_CODE (type) == ENUMERAL_TYPE)
1682         error ("range values `%s' and `%s' reversed",
1683                enum_name_string (new_value, type),
1684                enum_name_string (old_value, type));
1685       else
1686         error ("range values reversed");
1687     }
1688 }
1689 #endif