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