OSDN Git Service

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