OSDN Git Service

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