OSDN Git Service

* (REG_CLASS_FROM_CONSTRAINT): Only define if not already defined.
[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, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4    1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5    Hacked by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24
25 /* This file is part of the C++ front end.
26    It contains routines to build C++ expressions given their operands,
27    including computing the types of the result, C and C++ specific error
28    checks, and some optimization.
29
30    There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
31    and to process initializations in declarations (since they work
32    like a strange sort of assignment).  */
33
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "tree.h"
39 #include "cp-tree.h"
40 #include "flags.h"
41 #include "toplev.h"
42 #include "output.h"
43 #include "diagnostic.h"
44
45 static tree process_init_constructor (tree, tree, tree *);
46
47 /* Print an error message stemming from an attempt to use
48    BASETYPE as a base class for TYPE.  */
49
50 tree
51 error_not_base_type (tree basetype, tree type)
52 {
53   if (TREE_CODE (basetype) == FUNCTION_DECL)
54     basetype = DECL_CONTEXT (basetype);
55   error ("type `%T' is not a base type for type `%T'", basetype, type);
56   return error_mark_node;
57 }
58
59 tree
60 binfo_or_else (tree base, tree type)
61 {
62   tree binfo = lookup_base (type, base, ba_ignore, NULL);
63
64   if (binfo == error_mark_node)
65     return NULL_TREE;
66   else if (!binfo)
67     error_not_base_type (base, type);
68   return binfo;
69 }
70
71 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
72    value may not be changed thereafter.  Thus, we emit hard errors for these,
73    rather than just pedwarns.  If `SOFT' is 1, then we just pedwarn.  (For
74    example, conversions to references.)  */
75
76 void
77 readonly_error (tree arg, const char* string, int soft)
78 {
79   const char *fmt;
80   void (*fn) (const char *, ...);
81
82   if (soft)
83     fn = pedwarn;
84   else
85     fn = error;
86
87   if (TREE_CODE (arg) == COMPONENT_REF)
88     {
89       if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
90         fmt = "%s of data-member `%D' in read-only structure";
91       else
92         fmt = "%s of read-only data-member `%D'";
93       (*fn) (fmt, string, TREE_OPERAND (arg, 1));
94     }
95   else if (TREE_CODE (arg) == VAR_DECL)
96     {
97       if (DECL_LANG_SPECIFIC (arg)
98           && DECL_IN_AGGR_P (arg)
99           && !TREE_STATIC (arg))
100         fmt = "%s of constant field `%D'";
101       else
102         fmt = "%s of read-only variable `%D'";
103       (*fn) (fmt, string, arg);
104     }
105   else if (TREE_CODE (arg) == PARM_DECL)
106     (*fn) ("%s of read-only parameter `%D'", string, arg);
107   else if (TREE_CODE (arg) == INDIRECT_REF
108            && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
109            && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
110                || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
111     (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0));
112   else if (TREE_CODE (arg) == RESULT_DECL)
113     (*fn) ("%s of read-only named return value `%D'", string, arg);
114   else if (TREE_CODE (arg) == FUNCTION_DECL)
115     (*fn) ("%s of function `%D'", string, arg);
116   else
117     (*fn) ("%s of read-only location", string);
118 }
119
120 /* If TYPE has abstract virtual functions, issue an error about trying
121    to create an object of that type.  DECL is the object declared, or
122    NULL_TREE if the declaration is unavailable.  Returns 1 if an error
123    occurred; zero if all was well.  */
124
125 int
126 abstract_virtuals_error (tree decl, tree type)
127 {
128   tree u;
129   tree tu;
130
131   if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type))
132     return 0;
133
134   if (!TYPE_SIZE (type))
135     /* TYPE is being defined, and during that time
136        CLASSTYPE_PURE_VIRTUALS holds the inline friends.  */
137     return 0;
138
139   if (dependent_type_p (type))
140     /* For a dependent type, we do not yet know which functions are pure
141        virtuals.  */
142     return 0;
143
144   u = CLASSTYPE_PURE_VIRTUALS (type);
145   if (decl)
146     {
147       if (TREE_CODE (decl) == RESULT_DECL)
148         return 0;
149
150       if (TREE_CODE (decl) == VAR_DECL)
151         error ("cannot declare variable `%D' to be of type `%T'",
152                     decl, type);
153       else if (TREE_CODE (decl) == PARM_DECL)
154         error ("cannot declare parameter `%D' to be of type `%T'",
155                     decl, type);
156       else if (TREE_CODE (decl) == FIELD_DECL)
157         error ("cannot declare field `%D' to be of type `%T'",
158                     decl, type);
159       else if (TREE_CODE (decl) == FUNCTION_DECL
160                && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
161         error ("invalid return type for member function `%#D'", decl);
162       else if (TREE_CODE (decl) == FUNCTION_DECL)
163         error ("invalid return type for function `%#D'", decl);
164     }
165   else
166     error ("cannot allocate an object of type `%T'", type);
167
168   /* Only go through this once.  */
169   if (TREE_PURPOSE (u) == NULL_TREE)
170     {
171       TREE_PURPOSE (u) = error_mark_node;
172
173       error ("  because the following virtual functions are abstract:");
174       for (tu = u; tu; tu = TREE_CHAIN (tu))
175         cp_error_at ("\t%#D", TREE_VALUE (tu));
176     }
177   else
178     error ("  since type `%T' has abstract virtual functions", type);
179
180   return 1;
181 }
182
183 /* Print an error message for invalid use of an incomplete type.
184    VALUE is the expression that was used (or 0 if that isn't known)
185    and TYPE is the type that was invalid.  DIAG_TYPE indicates the
186    type of diagnostic:  0 for an error, 1 for a warning, 2 for a
187    pedwarn.  */
188
189 void
190 cxx_incomplete_type_diagnostic (tree value, tree type, int diag_type)
191 {
192   int decl = 0;
193   void (*p_msg) (const char *, ...);
194   void (*p_msg_at) (const char *, ...);
195
196   if (diag_type == 1)
197     {
198       p_msg = warning;
199       p_msg_at = cp_warning_at;
200     }
201   else if (diag_type == 2)
202     {
203       p_msg = pedwarn;
204       p_msg_at = cp_pedwarn_at;
205     }
206   else
207     {
208       p_msg = error;
209       p_msg_at = cp_error_at;
210     }
211   
212   /* Avoid duplicate error message.  */
213   if (TREE_CODE (type) == ERROR_MARK)
214     return;
215
216   if (value != 0 && (TREE_CODE (value) == VAR_DECL
217                      || TREE_CODE (value) == PARM_DECL
218                      || TREE_CODE (value) == FIELD_DECL))
219     {
220       (*p_msg_at) ("`%D' has incomplete type", value);
221       decl = 1;
222     }
223 retry:
224   /* We must print an error message.  Be clever about what it says.  */
225
226   switch (TREE_CODE (type))
227     {
228     case RECORD_TYPE:
229     case UNION_TYPE:
230     case ENUMERAL_TYPE:
231       if (!decl)
232         (*p_msg) ("invalid use of undefined type `%#T'", type);
233       if (!TYPE_TEMPLATE_INFO (type))
234         (*p_msg_at) ("forward declaration of `%#T'", type);
235       else
236         (*p_msg_at) ("declaration of `%#T'", type);
237       break;
238
239     case VOID_TYPE:
240       (*p_msg) ("invalid use of `%T'", type);
241       break;
242
243     case ARRAY_TYPE:
244       if (TYPE_DOMAIN (type))
245         {
246           type = TREE_TYPE (type);
247           goto retry;
248         }
249       (*p_msg) ("invalid use of array with unspecified bounds");
250       break;
251
252     case OFFSET_TYPE:
253     bad_member:
254       (*p_msg) ("invalid use of member (did you forget the `&' ?)");
255       break;
256
257     case TEMPLATE_TYPE_PARM:
258       (*p_msg) ("invalid use of template type parameter");
259       break;
260
261     case UNKNOWN_TYPE:
262       if (value && TREE_CODE (value) == COMPONENT_REF)
263         goto bad_member;
264       else if (value && TREE_CODE (value) == ADDR_EXPR)
265         (*p_msg) ("address of overloaded function with no contextual type information");
266       else if (value && TREE_CODE (value) == OVERLOAD)
267         (*p_msg) ("overloaded function with no contextual type information");
268       else
269         (*p_msg) ("insufficient contextual information to determine type");
270       break;
271     
272     default:
273       abort ();
274     }
275 }
276
277 /* Backward-compatibility interface to incomplete_type_diagnostic;
278    required by ../tree.c.  */
279 #undef cxx_incomplete_type_error
280 void
281 cxx_incomplete_type_error (tree value, tree type)
282 {
283   cxx_incomplete_type_diagnostic (value, type, 0);
284 }
285
286 \f
287 /* Perform appropriate conversions on the initial value of a variable,
288    store it in the declaration DECL,
289    and print any error messages that are appropriate.
290    If the init is invalid, store an ERROR_MARK.
291
292    C++: Note that INIT might be a TREE_LIST, which would mean that it is
293    a base class initializer for some aggregate type, hopefully compatible
294    with DECL.  If INIT is a single element, and DECL is an aggregate
295    type, we silently convert INIT into a TREE_LIST, allowing a constructor
296    to be called.
297
298    If INIT is a TREE_LIST and there is no constructor, turn INIT
299    into a CONSTRUCTOR and use standard initialization techniques.
300    Perhaps a warning should be generated?
301
302    Returns value of initializer if initialization could not be
303    performed for static variable.  In that case, caller must do
304    the storing.  */
305
306 tree
307 store_init_value (tree decl, tree init)
308 {
309   register tree value, type;
310
311   /* If variable's type was invalidly declared, just ignore it.  */
312
313   type = TREE_TYPE (decl);
314   if (TREE_CODE (type) == ERROR_MARK)
315     return NULL_TREE;
316
317   if (IS_AGGR_TYPE (type))
318     {
319       if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
320           && TREE_CODE (init) != CONSTRUCTOR)
321         abort ();
322
323       if (TREE_CODE (init) == TREE_LIST)
324         {
325           error ("constructor syntax used, but no constructor declared for type `%T'", type);
326           init = build_constructor (NULL_TREE, nreverse (init));
327         }
328     }
329   else if (TREE_CODE (init) == TREE_LIST
330            && TREE_TYPE (init) != unknown_type_node)
331     {
332       if (TREE_CODE (decl) == RESULT_DECL)
333         {
334           if (TREE_CHAIN (init))
335             {
336               warning ("comma expression used to initialize return value");
337               init = build_compound_expr (init);
338             }
339           else
340             init = TREE_VALUE (init);
341         }
342       else if (TREE_CODE (init) == TREE_LIST
343                && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
344         {
345           error ("cannot initialize arrays using this syntax");
346           return NULL_TREE;
347         }
348       else
349         {
350           /* We get here with code like `int a (2);' */
351              
352           if (TREE_CHAIN (init) != NULL_TREE)
353             {
354               pedwarn ("initializer list being treated as compound expression");
355               init = build_compound_expr (init);
356             }
357           else
358             init = TREE_VALUE (init);
359         }
360     }
361
362   /* End of special C++ code.  */
363
364   /* Digest the specified initializer into an expression.  */
365   value = digest_init (type, init, (tree *) 0);
366
367   /* Store the expression if valid; else report error.  */
368
369   if (TREE_CODE (value) == ERROR_MARK)
370     ;
371   /* Other code expects that initializers for objects of types that need
372      constructing never make it into DECL_INITIAL, and passes 'init' to
373      build_aggr_init without checking DECL_INITIAL.  So just return.  */
374   else if (TYPE_NEEDS_CONSTRUCTING (type))
375     return value;
376   else if (TREE_STATIC (decl)
377            && (! TREE_CONSTANT (value)
378                || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
379     return value;
380   
381   /* Store the VALUE in DECL_INITIAL.  If we're building a
382      statement-tree we will actually expand the initialization later
383      when we output this function.  */
384   DECL_INITIAL (decl) = value;
385   return NULL_TREE;
386 }
387
388 \f
389 /* Digest the parser output INIT as an initializer for type TYPE.
390    Return a C expression of type TYPE to represent the initial value.
391
392    If TAIL is nonzero, it points to a variable holding a list of elements
393    of which INIT is the first.  We update the list stored there by
394    removing from the head all the elements that we use.
395    Normally this is only one; we use more than one element only if
396    TYPE is an aggregate and INIT is not a constructor.  */
397
398 tree
399 digest_init (tree type, tree init, tree* tail)
400 {
401   enum tree_code code = TREE_CODE (type);
402   tree element = NULL_TREE;
403   tree old_tail_contents = NULL_TREE;
404   /* Nonzero if INIT is a braced grouping.  */
405   int raw_constructor;
406
407   /* By default, assume we use one element from a list.
408      We correct this later in the sole case where it is not true.  */
409
410   if (tail)
411     {
412       old_tail_contents = *tail;
413       *tail = TREE_CHAIN (*tail);
414     }
415
416   if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
417                                   && TREE_VALUE (init) == error_mark_node))
418     return error_mark_node;
419
420   if (TREE_CODE (init) == ERROR_MARK)
421     /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside
422        a template function. This gets substituted during instantiation.  */
423     return init;
424
425   /* We must strip the outermost array type when completing the type,
426      because the its bounds might be incomplete at the moment.  */
427   if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
428                               ? TREE_TYPE (type) : type, NULL_TREE))
429     return error_mark_node;
430   
431   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
432   if (TREE_CODE (init) == NON_LVALUE_EXPR)
433     init = TREE_OPERAND (init, 0);
434
435   raw_constructor = (TREE_CODE (init) == CONSTRUCTOR 
436                      && TREE_HAS_CONSTRUCTOR (init));
437
438   if (raw_constructor
439       && CONSTRUCTOR_ELTS (init) != 0
440       && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
441     {
442       element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
443       /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
444       if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
445         element = TREE_OPERAND (element, 0);
446       if (element == error_mark_node)
447         return element;
448     }
449
450   /* Initialization of an array of chars from a string constant
451      optionally enclosed in braces.  */
452
453   if (code == ARRAY_TYPE)
454     {
455       tree typ1;
456
457       if (TREE_CODE (init) == TREE_LIST)
458         {
459           error ("initializing array with parameter list");
460           return error_mark_node;
461         }
462
463       typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
464       if (char_type_p (typ1)
465           && ((init && TREE_CODE (init) == STRING_CST)
466               || (element && TREE_CODE (element) == STRING_CST)))
467         {
468           tree string = element ? element : init;
469
470           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
471                != char_type_node)
472               && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
473             {
474               error ("char-array initialized from wide string");
475               return error_mark_node;
476             }
477           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
478                == char_type_node)
479               && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
480             {
481               error ("int-array initialized from non-wide string");
482               return error_mark_node;
483             }
484
485           TREE_TYPE (string) = type;
486           if (TYPE_DOMAIN (type) != 0
487               && TREE_CONSTANT (TYPE_SIZE (type)))
488             {
489               register int size
490                 = TREE_INT_CST_LOW (TYPE_SIZE (type));
491               size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
492               /* In C it is ok to subtract 1 from the length of the string
493                  because it's ok to ignore the terminating null char that is
494                  counted in the length of the constant, but in C++ this would
495                  be invalid.  */
496               if (size < TREE_STRING_LENGTH (string))
497                 pedwarn ("initializer-string for array of chars is too long");
498             }
499           return string;
500         }
501     }
502
503   /* Handle scalar types, including conversions,
504      and signature pointers and references.  */
505
506   if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
507       || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
508       || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
509       || TYPE_PTRMEMFUNC_P (type))
510     {
511       if (raw_constructor)
512         {
513           if (element == 0)
514             {
515               error ("initializer for scalar variable requires one element");
516               return error_mark_node;
517             }
518           init = element;
519         }
520       while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
521         {
522           pedwarn ("braces around scalar initializer for `%T'", type);
523           init = CONSTRUCTOR_ELTS (init);
524           if (TREE_CHAIN (init))
525             pedwarn ("ignoring extra initializers for `%T'", type);
526           init = TREE_VALUE (init);
527         }
528
529       return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
530                                          "initialization", NULL_TREE, 0);
531     }
532
533   /* Come here only for records and arrays (and unions with constructors).  */
534
535   if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
536     {
537       error ("variable-sized object of type `%T' may not be initialized",
538                 type);
539       return error_mark_node;
540     }
541
542   if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code))
543     {
544       if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type)
545           && TREE_HAS_CONSTRUCTOR (init))
546         {
547           error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
548                     type, init);
549           return error_mark_node;
550         }
551       else if (raw_constructor)
552         return process_init_constructor (type, init, (tree *)0);
553       else if (can_convert_arg (type, TREE_TYPE (init), init)
554                || TYPE_NON_AGGREGATE_CLASS (type))
555         /* These are never initialized from multiple constructor elements.  */;
556       else if (tail != 0)
557         {
558           *tail = old_tail_contents;
559           return process_init_constructor (type, 0, tail);
560         }
561
562       if (code != ARRAY_TYPE)
563         {
564           int flags = LOOKUP_NORMAL;
565           /* Initialization from { } is copy-initialization.  */
566           if (tail)
567             flags |= LOOKUP_ONLYCONVERTING;
568
569           return convert_for_initialization (NULL_TREE, type, init, flags,
570                                              "initialization", NULL_TREE, 0);
571         }
572     }
573
574   error ("invalid initializer");
575   return error_mark_node;
576 }
577 \f
578 /* Process a constructor for a variable of type TYPE.
579    The constructor elements may be specified either with INIT or with ELTS,
580    only one of which should be non-null.
581
582    If INIT is specified, it is a CONSTRUCTOR node which is specifically
583    and solely for initializing this datum.
584
585    If ELTS is specified, it is the address of a variable containing
586    a list of expressions.  We take as many elements as we need
587    from the head of the list and update the list.
588
589    In the resulting constructor, TREE_CONSTANT is set if all elts are
590    constant, and TREE_STATIC is set if, in addition, all elts are simple enough
591    constants that the assembler and linker can compute them.  */
592
593 static tree
594 process_init_constructor (tree type, tree init, tree* elts)
595 {
596   register tree tail;
597   /* List of the elements of the result constructor,
598      in reverse order.  */
599   register tree members = NULL;
600   register tree next1;
601   tree result;
602   int allconstant = 1;
603   int allsimple = 1;
604   int erroneous = 0;
605
606   /* Make TAIL be the list of elements to use for the initialization,
607      no matter how the data was given to us.  */
608
609   if (elts)
610     {
611       if (warn_missing_braces)
612         warning ("aggregate has a partly bracketed initializer");
613       tail = *elts;
614     }
615   else
616     tail = CONSTRUCTOR_ELTS (init);
617
618   /* Gobble as many elements as needed, and make a constructor or initial value
619      for each element of this aggregate.  Chain them together in result.
620      If there are too few, use 0 for each scalar ultimate component.  */
621
622   if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
623     {
624       register long len;
625       register int i;
626
627       if (TREE_CODE (type) == ARRAY_TYPE)
628         {
629           tree domain = TYPE_DOMAIN (type);
630           if (domain)
631             len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
632                    - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
633                    + 1);
634           else
635             len = -1;  /* Take as many as there are */
636         }
637       else
638         {
639           /* Vectors are like simple fixed-size arrays.  */
640           len = TYPE_VECTOR_SUBPARTS (type);
641         }
642
643       for (i = 0; len < 0 || i < len; i++)
644         {
645           if (tail)
646             {
647               if (TREE_PURPOSE (tail)
648                   && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
649                       || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
650                 sorry ("non-trivial labeled initializers");
651
652               if (TREE_VALUE (tail) != 0)
653                 {
654                   tree tail1 = tail;
655                   next1 = digest_init (TREE_TYPE (type),
656                                        TREE_VALUE (tail), &tail1);
657                   if (next1 == error_mark_node)
658                     return next1;
659                   my_friendly_assert
660                     (same_type_ignoring_top_level_qualifiers_p
661                      (TREE_TYPE (type), TREE_TYPE (next1)),
662                      981123);
663                   my_friendly_assert (tail1 == 0
664                                       || TREE_CODE (tail1) == TREE_LIST, 319);
665                   if (tail == tail1 && len < 0)
666                     {
667                       error ("non-empty initializer for array of empty elements");
668                       /* Just ignore what we were supposed to use.  */
669                       tail1 = NULL_TREE;
670                     }
671                   tail = tail1;
672                 }
673               else
674                 {
675                   next1 = error_mark_node;
676                   tail = TREE_CHAIN (tail);
677                 }
678             }
679           else if (len < 0)
680             /* We're done.  */
681             break;
682           else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
683             {
684               /* If this type needs constructors run for
685                  default-initialization, we can't rely on the backend to do it
686                  for us, so build up TARGET_EXPRs.  If the type in question is
687                  a class, just build one up; if it's an array, recurse.  */
688
689               if (IS_AGGR_TYPE (TREE_TYPE (type)))
690                 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
691               else
692                 next1 = build_constructor (NULL_TREE, NULL_TREE);
693               next1 = digest_init (TREE_TYPE (type), next1, 0);
694             }
695           else if (! zero_init_p (TREE_TYPE (type)))
696             next1 = build_zero_init (TREE_TYPE (type),
697                                      /*nelts=*/NULL_TREE,
698                                      /*static_storage_p=*/false);
699           else
700             /* The default zero-initialization is fine for us; don't
701                add anything to the CONSTRUCTOR.  */
702             break;
703
704           if (next1 == error_mark_node)
705             erroneous = 1;
706           else if (!TREE_CONSTANT (next1))
707             allconstant = 0;
708           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
709             allsimple = 0;
710           members = tree_cons (size_int (i), next1, members);
711         }
712     }
713   else if (TREE_CODE (type) == RECORD_TYPE)
714     {
715       register tree field;
716
717       if (tail)
718         {
719           if (TYPE_USES_VIRTUAL_BASECLASSES (type))
720             {
721               sorry ("initializer list for object of class with virtual base classes");
722               return error_mark_node;
723             }
724
725           if (TYPE_BINFO_BASETYPES (type))
726             {
727               sorry ("initializer list for object of class with base classes");
728               return error_mark_node;
729             }
730
731           if (TYPE_POLYMORPHIC_P (type))
732             {
733               sorry ("initializer list for object using virtual functions");
734               return error_mark_node;
735             }
736         }
737
738       for (field = TYPE_FIELDS (type); field;
739            field = TREE_CHAIN (field))
740         {
741           if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
742             {
743               members = tree_cons (field, integer_zero_node, members);
744               continue;
745             }
746
747           if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
748             continue;
749
750           if (tail)
751             {
752               if (TREE_PURPOSE (tail)
753                   && TREE_PURPOSE (tail) != field
754                   && TREE_PURPOSE (tail) != DECL_NAME (field))
755                 sorry ("non-trivial labeled initializers");
756
757               if (TREE_VALUE (tail) != 0)
758                 {
759                   tree tail1 = tail;
760
761                   next1 = digest_init (TREE_TYPE (field),
762                                        TREE_VALUE (tail), &tail1);
763                   my_friendly_assert (tail1 == 0
764                                       || TREE_CODE (tail1) == TREE_LIST, 320);
765                   tail = tail1;
766                 }
767               else
768                 {
769                   next1 = error_mark_node;
770                   tail = TREE_CHAIN (tail);
771                 }
772             }
773           else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
774             {
775               /* If this type needs constructors run for
776                  default-initialization, we can't rely on the backend to do it
777                  for us, so build up TARGET_EXPRs.  If the type in question is
778                  a class, just build one up; if it's an array, recurse.  */
779
780               if (IS_AGGR_TYPE (TREE_TYPE (field)))
781                 next1 = build_functional_cast (TREE_TYPE (field),
782                                                NULL_TREE);
783               else
784                 {
785                   next1 = build_constructor (NULL_TREE, NULL_TREE);
786                   if (init)
787                     TREE_HAS_CONSTRUCTOR (next1)
788                        = TREE_HAS_CONSTRUCTOR (init);
789                 }
790               next1 = digest_init (TREE_TYPE (field), next1, 0);
791
792               /* Warn when some struct elements are implicitly initialized.  */
793               if (extra_warnings
794                   && (!init || TREE_HAS_CONSTRUCTOR (init)))
795                 warning ("missing initializer for member `%D'", field);
796             }
797           else
798             {
799               if (TREE_READONLY (field))
800                 error ("uninitialized const member `%D'", field);
801               else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
802                 error ("member `%D' with uninitialized const fields",
803                           field);
804               else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
805                 error ("member `%D' is uninitialized reference", field);
806
807               /* Warn when some struct elements are implicitly initialized
808                  to zero.  */
809               if (extra_warnings
810                   && (!init || TREE_HAS_CONSTRUCTOR (init)))
811                 warning ("missing initializer for member `%D'", field);
812
813               if (! zero_init_p (TREE_TYPE (field)))
814                 next1 = build_zero_init (TREE_TYPE (field),
815                                          /*nelts=*/NULL_TREE,
816                                          /*static_storage_p=*/false);
817               else
818                 /* The default zero-initialization is fine for us; don't
819                    add anything to the CONSTRUCTOR.  */
820                 continue;
821             }
822
823           if (next1 == error_mark_node)
824             erroneous = 1;
825           else if (!TREE_CONSTANT (next1))
826             allconstant = 0;
827           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
828             allsimple = 0;
829           members = tree_cons (field, next1, members);
830         }
831     }
832   else if (TREE_CODE (type) == UNION_TYPE
833            /* If the initializer was empty, use default zero initialization.  */
834            && tail)
835     {
836       register tree field = TYPE_FIELDS (type);
837
838       /* Find the first named field.  ANSI decided in September 1990
839          that only named fields count here.  */
840       while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
841         field = TREE_CHAIN (field);
842
843       /* If this element specifies a field, initialize via that field.  */
844       if (TREE_PURPOSE (tail) != NULL_TREE)
845         {
846           int win = 0;
847
848           if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
849             /* Handle the case of a call by build_c_cast.  */
850             field = TREE_PURPOSE (tail), win = 1;
851           else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
852             error ("index value instead of field name in union initializer");
853           else
854             {
855               tree temp;
856               for (temp = TYPE_FIELDS (type);
857                    temp;
858                    temp = TREE_CHAIN (temp))
859                 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
860                   break;
861               if (temp)
862                 field = temp, win = 1;
863               else
864                 error ("no field `%D' in union being initialized",
865                           TREE_PURPOSE (tail));
866             }
867           if (!win)
868             TREE_VALUE (tail) = error_mark_node;
869         }
870       else if (field == 0)
871         {
872           error ("union `%T' with no named members cannot be initialized",
873                     type);
874           TREE_VALUE (tail) = error_mark_node;
875         }
876
877       if (TREE_VALUE (tail) != 0)
878         {
879           tree tail1 = tail;
880
881           next1 = digest_init (TREE_TYPE (field),
882                                TREE_VALUE (tail), &tail1);
883           if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
884             abort ();
885           tail = tail1;
886         }
887       else
888         {
889           next1 = error_mark_node;
890           tail = TREE_CHAIN (tail);
891         }
892
893       if (next1 == error_mark_node)
894         erroneous = 1;
895       else if (!TREE_CONSTANT (next1))
896         allconstant = 0;
897       else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
898         allsimple = 0;
899       members = tree_cons (field, next1, members);
900     }
901
902   /* If arguments were specified as a list, just remove the ones we used.  */
903   if (elts)
904     *elts = tail;
905   /* If arguments were specified as a constructor,
906      complain unless we used all the elements of the constructor.  */
907   else if (tail)
908     pedwarn ("excess elements in aggregate initializer");
909
910   if (erroneous)
911     return error_mark_node;
912
913   result = build_constructor (type, nreverse (members));
914   if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
915     complete_array_type (type, result, /*do_default=*/0);
916   if (init)
917     TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
918   if (allconstant) TREE_CONSTANT (result) = 1;
919   if (allconstant && allsimple) TREE_STATIC (result) = 1;
920   return result;
921 }
922 \f
923 /* Given a structure or union value DATUM, construct and return
924    the structure or union component which results from narrowing
925    that value to the base specified in BASETYPE.  For example, given the
926    hierarchy
927
928    class L { int ii; };
929    class A : L { ... };
930    class B : L { ... };
931    class C : A, B { ... };
932
933    and the declaration
934
935    C x;
936
937    then the expression
938
939    x.A::ii refers to the ii member of the L part of
940    the A part of the C object named by X.  In this case,
941    DATUM would be x, and BASETYPE would be A.
942
943    I used to think that this was nonconformant, that the standard specified
944    that first we look up ii in A, then convert x to an L& and pull out the
945    ii part.  But in fact, it does say that we convert x to an A&; A here
946    is known as the "naming class".  (jason 2000-12-19)
947
948    BINFO_P points to a variable initialized either to NULL_TREE or to the
949    binfo for the specific base subobject we want to convert to.  */
950
951 tree
952 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
953 {
954   tree binfo;
955
956   if (datum == error_mark_node)
957     return error_mark_node;
958   if (*binfo_p)
959     binfo = *binfo_p;
960   else
961     binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
962
963   if (!binfo || binfo == error_mark_node)
964     {
965       *binfo_p = NULL_TREE;
966       if (!binfo)
967         error_not_base_type (basetype, TREE_TYPE (datum));
968       return error_mark_node;
969     }
970
971   *binfo_p = binfo;
972   return build_base_path (PLUS_EXPR, datum, binfo, 1);
973 }
974
975 /* Build a reference to an object specified by the C++ `->' operator.
976    Usually this just involves dereferencing the object, but if the
977    `->' operator is overloaded, then such overloads must be
978    performed until an object which does not have the `->' operator
979    overloaded is found.  An error is reported when circular pointer
980    delegation is detected.  */
981
982 tree
983 build_x_arrow (tree datum)
984 {
985   tree types_memoized = NULL_TREE;
986   register tree rval = datum;
987   tree type = TREE_TYPE (rval);
988   tree last_rval = NULL_TREE;
989
990   if (type == error_mark_node)
991     return error_mark_node;
992
993   if (processing_template_decl)
994     return build_min_nt (ARROW_EXPR, rval);
995
996   if (TREE_CODE (rval) == OFFSET_REF)
997     {
998       rval = resolve_offset_ref (datum);
999       type = TREE_TYPE (rval);
1000     }
1001
1002   if (TREE_CODE (type) == REFERENCE_TYPE)
1003     {
1004       rval = convert_from_reference (rval);
1005       type = TREE_TYPE (rval);
1006     }
1007
1008   if (IS_AGGR_TYPE (type))
1009     {
1010       while ((rval = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, rval,
1011                                    NULL_TREE, NULL_TREE)))
1012         {
1013           if (rval == error_mark_node)
1014             return error_mark_node;
1015
1016           if (value_member (TREE_TYPE (rval), types_memoized))
1017             {
1018               error ("circular pointer delegation detected");
1019               return error_mark_node;
1020             }
1021           else
1022             {
1023               types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1024                                           types_memoized);
1025             }
1026           last_rval = rval;
1027         }     
1028
1029       if (last_rval == NULL_TREE)
1030         {
1031           error ("base operand of `->' has non-pointer type `%T'", type);
1032           return error_mark_node;
1033         }
1034
1035       if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1036         last_rval = convert_from_reference (last_rval);
1037     }
1038   else
1039     last_rval = default_conversion (rval);
1040
1041   if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1042     return build_indirect_ref (last_rval, NULL);
1043
1044   if (types_memoized)
1045     error ("result of `operator->()' yields non-pointer result");
1046   else
1047     error ("base operand of `->' is not a pointer");
1048   return error_mark_node;
1049 }
1050
1051 /* Make an expression to refer to the COMPONENT field of
1052    structure or union value DATUM.  COMPONENT is an arbitrary
1053    expression.  DATUM has not already been checked out to be of
1054    aggregate type.
1055
1056    For C++, COMPONENT may be a TREE_LIST.  This happens when we must
1057    return an object of member type to a method of the current class,
1058    but there is not yet enough typing information to know which one.
1059    As a special case, if there is only one method by that name,
1060    it is returned.  Otherwise we return an expression which other
1061    routines will have to know how to deal with later.  */
1062
1063 tree
1064 build_m_component_ref (tree datum, tree component)
1065 {
1066   tree type;
1067   tree objtype;
1068   tree field_type;
1069   int type_quals;
1070   tree binfo;
1071
1072   if (processing_template_decl)
1073     return build_min_nt (DOTSTAR_EXPR, datum, component);
1074
1075   datum = decay_conversion (datum);
1076
1077   if (datum == error_mark_node || component == error_mark_node)
1078     return error_mark_node;
1079
1080   objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));  
1081
1082   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1083     {
1084       type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1085       field_type = type;
1086     }
1087   else if (TYPE_PTRMEM_P (TREE_TYPE (component)))
1088     {
1089       type = TREE_TYPE (TREE_TYPE (component));
1090       field_type = TREE_TYPE (type);
1091       
1092       /* Compute the type of the field, as described in [expr.ref].  */
1093       type_quals = TYPE_UNQUALIFIED;
1094       if (TREE_CODE (field_type) == REFERENCE_TYPE)
1095         /* The standard says that the type of the result should be the
1096            type referred to by the reference.  But for now, at least,
1097            we do the conversion from reference type later.  */
1098         ;
1099       else
1100         {
1101           type_quals = (cp_type_quals (field_type)  
1102                         | cp_type_quals (TREE_TYPE (datum)));
1103
1104           /* There's no such thing as a mutable pointer-to-member, so
1105              things are not as complex as they are for references to
1106              non-static data members.  */
1107           field_type = cp_build_qualified_type (field_type, type_quals);
1108         }
1109     }
1110   else
1111     {
1112       error ("`%E' cannot be used as a member pointer, since it is of type `%T'", 
1113                 component, TREE_TYPE (component));
1114       return error_mark_node;
1115     }
1116
1117   if (! IS_AGGR_TYPE (objtype))
1118     {
1119       error ("cannot apply member pointer `%E' to `%E', which is of non-aggregate type `%T'",
1120                 component, datum, objtype);
1121       return error_mark_node;
1122     }
1123
1124   binfo = lookup_base (objtype, TYPE_METHOD_BASETYPE (type),
1125                        ba_check, NULL);
1126   if (!binfo)
1127     {
1128       error ("member type `%T::' incompatible with object type `%T'",
1129                 TYPE_METHOD_BASETYPE (type), objtype);
1130       return error_mark_node;
1131     }
1132   else if (binfo == error_mark_node)
1133     return error_mark_node;
1134
1135   component = build (OFFSET_REF, field_type, datum, component);
1136   if (TREE_CODE (type) == OFFSET_TYPE)
1137     component = resolve_offset_ref (component);
1138   return component;
1139 }
1140
1141 /* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
1142
1143 tree
1144 build_functional_cast (tree exp, tree parms)
1145 {
1146   /* This is either a call to a constructor,
1147      or a C cast in C++'s `functional' notation.  */
1148   tree type;
1149
1150   if (exp == error_mark_node || parms == error_mark_node)
1151     return error_mark_node;
1152
1153   if (TREE_CODE (exp) == IDENTIFIER_NODE)
1154     {
1155       if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1156         /* Either an enum or an aggregate type.  */
1157         type = IDENTIFIER_TYPE_VALUE (exp);
1158       else
1159         {
1160           type = lookup_name (exp, 1);
1161           if (!type || TREE_CODE (type) != TYPE_DECL)
1162             {
1163               error ("`%T' fails to be a typedef or built-in type", exp);
1164               return error_mark_node;
1165             }
1166           type = TREE_TYPE (type);
1167         }
1168     }
1169   else if (TREE_CODE (exp) == TYPE_DECL)
1170     type = TREE_TYPE (exp);
1171   else
1172     type = exp;
1173
1174   if (processing_template_decl)
1175     return build_min (CAST_EXPR, type, parms);
1176
1177   if (! IS_AGGR_TYPE (type))
1178     {
1179       /* this must build a C cast */
1180       if (parms == NULL_TREE)
1181         parms = integer_zero_node;
1182       else
1183         {
1184           if (TREE_CHAIN (parms) != NULL_TREE)
1185             pedwarn ("initializer list being treated as compound expression");
1186           parms = build_compound_expr (parms);
1187         }
1188
1189       return build_c_cast (type, parms);
1190     }
1191
1192   /* Prepare to evaluate as a call to a constructor.  If this expression
1193      is actually used, for example,
1194          
1195      return X (arg1, arg2, ...);
1196          
1197      then the slot being initialized will be filled in.  */
1198
1199   if (!complete_type_or_else (type, NULL_TREE))
1200     return error_mark_node;
1201   if (abstract_virtuals_error (NULL_TREE, type))
1202     return error_mark_node;
1203
1204   if (parms && TREE_CHAIN (parms) == NULL_TREE)
1205     return build_c_cast (type, TREE_VALUE (parms));
1206
1207   /* We need to zero-initialize POD types.  Let's do that for everything
1208      that doesn't need a constructor.  */
1209   if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1210       && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1211     {
1212       exp = build_constructor (type, NULL_TREE);
1213       return get_target_expr (exp);
1214     }
1215
1216   exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms,
1217                                    TYPE_BINFO (type), LOOKUP_NORMAL);
1218
1219   if (exp == error_mark_node)
1220     return error_mark_node;
1221
1222   return build_cplus_new (type, exp);
1223 }
1224 \f
1225
1226 /* Add new exception specifier SPEC, to the LIST we currently have.
1227    If it's already in LIST then do nothing.
1228    Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1229    know what we're doing.  */
1230
1231 tree
1232 add_exception_specifier (tree list, tree spec, int complain)
1233 {
1234   int ok;
1235   tree core = spec;
1236   int is_ptr;
1237   int diag_type = -1; /* none */
1238   
1239   if (spec == error_mark_node)
1240     return list;
1241   
1242   my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1243   
1244   /* [except.spec] 1, type in an exception specifier shall not be
1245      incomplete, or pointer or ref to incomplete other than pointer
1246      to cv void.  */
1247   is_ptr = TREE_CODE (core) == POINTER_TYPE;
1248   if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1249     core = TREE_TYPE (core);
1250   if (complain < 0)
1251     ok = 1;
1252   else if (VOID_TYPE_P (core))
1253     ok = is_ptr;
1254   else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1255     ok = 1;
1256   else if (processing_template_decl)
1257     ok = 1;
1258   else
1259     {
1260       ok = 1;
1261       /* 15.4/1 says that types in an exception specifier must be complete,
1262          but it seems more reasonable to only require this on definitions
1263          and calls.  So just give a pedwarn at this point; we will give an
1264          error later if we hit one of those two cases.  */
1265       if (!COMPLETE_TYPE_P (complete_type (core)))
1266         diag_type = 2; /* pedwarn */
1267     }
1268
1269   if (ok)
1270     {
1271       tree probe;
1272       
1273       for (probe = list; probe; probe = TREE_CHAIN (probe))
1274         if (same_type_p (TREE_VALUE (probe), spec))
1275           break;
1276       if (!probe)
1277         list = tree_cons (NULL_TREE, spec, list);
1278     }
1279   else
1280     diag_type = 0; /* error */
1281     
1282   if (diag_type >= 0 && complain)
1283     cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1284
1285   return list;
1286 }
1287
1288 /* Combine the two exceptions specifier lists LIST and ADD, and return
1289    their union.  */
1290
1291 tree
1292 merge_exception_specifiers (tree list, tree add)
1293 {
1294   if (!list || !add)
1295     return NULL_TREE;
1296   else if (!TREE_VALUE (list))
1297     return add;
1298   else if (!TREE_VALUE (add))
1299     return list;
1300   else
1301     {
1302       tree orig_list = list;
1303       
1304       for (; add; add = TREE_CHAIN (add))
1305         {
1306           tree spec = TREE_VALUE (add);
1307           tree probe;
1308           
1309           for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1310             if (same_type_p (TREE_VALUE (probe), spec))
1311               break;
1312           if (!probe)
1313             {
1314               spec = build_tree_list (NULL_TREE, spec);
1315               TREE_CHAIN (spec) = list;
1316               list = spec;
1317             }
1318         }
1319     }
1320   return list;
1321 }
1322
1323 /* Subroutine of build_call.  Ensure that each of the types in the
1324    exception specification is complete.  Technically, 15.4/1 says that
1325    they need to be complete when we see a declaration of the function,
1326    but we should be able to get away with only requiring this when the
1327    function is defined or called.  See also add_exception_specifier.  */
1328
1329 void
1330 require_complete_eh_spec_types (tree fntype, tree decl)
1331 {
1332   tree raises;
1333   /* Don't complain about calls to op new.  */
1334   if (decl && DECL_ARTIFICIAL (decl))
1335     return;
1336   for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1337        raises = TREE_CHAIN (raises))
1338     {
1339       tree type = TREE_VALUE (raises);
1340       if (type && !COMPLETE_TYPE_P (type))
1341         {
1342           if (decl)
1343             error
1344               ("call to function `%D' which throws incomplete type `%#T'",
1345                decl, type);
1346           else
1347             error ("call to function which throws incomplete type `%#T'",
1348                    decl);
1349         }
1350     }
1351 }