OSDN Git Service

* cp-tree.h (char_type_p): New function.
[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 (char_type_p (typ1)
577           && ((init && TREE_CODE (init) == STRING_CST)
578               || (element && TREE_CODE (element) == STRING_CST)))
579         {
580           tree string = element ? element : init;
581
582           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
583                != char_type_node)
584               && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
585             {
586               error ("char-array initialized from wide string");
587               return error_mark_node;
588             }
589           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
590                == char_type_node)
591               && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
592             {
593               error ("int-array initialized from non-wide string");
594               return error_mark_node;
595             }
596
597           TREE_TYPE (string) = type;
598           if (TYPE_DOMAIN (type) != 0
599               && TREE_CONSTANT (TYPE_SIZE (type)))
600             {
601               register int size
602                 = TREE_INT_CST_LOW (TYPE_SIZE (type));
603               size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
604               /* In C it is ok to subtract 1 from the length of the string
605                  because it's ok to ignore the terminating null char that is
606                  counted in the length of the constant, but in C++ this would
607                  be invalid.  */
608               if (size < TREE_STRING_LENGTH (string))
609                 pedwarn ("initializer-string for array of chars is too long");
610             }
611           return string;
612         }
613     }
614
615   /* Handle scalar types, including conversions,
616      and signature pointers and references.  */
617
618   if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
619       || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
620       || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
621       || TYPE_PTRMEMFUNC_P (type))
622     {
623       if (raw_constructor)
624         {
625           if (element == 0)
626             {
627               error ("initializer for scalar variable requires one element");
628               return error_mark_node;
629             }
630           init = element;
631         }
632       while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
633         {
634           cp_pedwarn ("braces around scalar initializer for `%T'", type);
635           init = CONSTRUCTOR_ELTS (init);
636           if (TREE_CHAIN (init))
637             cp_pedwarn ("ignoring extra initializers for `%T'", type);
638           init = TREE_VALUE (init);
639         }
640
641       return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
642                                          "initialization", NULL_TREE, 0);
643     }
644
645   /* Come here only for records and arrays (and unions with constructors).  */
646
647   if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
648     {
649       cp_error ("variable-sized object of type `%T' may not be initialized",
650                 type);
651       return error_mark_node;
652     }
653
654   if (code == ARRAY_TYPE || IS_AGGR_TYPE_CODE (code))
655     {
656       if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type))
657         {
658           cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
659                     type, init);
660           return error_mark_node;
661         }
662       else if (raw_constructor)
663         return process_init_constructor (type, init, (tree *)0);
664       else if (can_convert_arg (type, TREE_TYPE (init), init)
665                || TYPE_NON_AGGREGATE_CLASS (type))
666         /* These are never initialized from multiple constructor elements.  */;
667       else if (tail != 0)
668         {
669           *tail = old_tail_contents;
670           return process_init_constructor (type, 0, tail);
671         }
672
673       if (code != ARRAY_TYPE)
674         {
675           int flags = LOOKUP_NORMAL;
676           /* Initialization from { } is copy-initialization.  */
677           if (tail)
678             flags |= LOOKUP_ONLYCONVERTING;
679
680           return convert_for_initialization (NULL_TREE, type, init, flags,
681                                              "initialization", NULL_TREE, 0);
682         }
683     }
684
685   error ("invalid initializer");
686   return error_mark_node;
687 }
688 \f
689 /* Process a constructor for a variable of type TYPE.
690    The constructor elements may be specified either with INIT or with ELTS,
691    only one of which should be non-null.
692
693    If INIT is specified, it is a CONSTRUCTOR node which is specifically
694    and solely for initializing this datum.
695
696    If ELTS is specified, it is the address of a variable containing
697    a list of expressions.  We take as many elements as we need
698    from the head of the list and update the list.
699
700    In the resulting constructor, TREE_CONSTANT is set if all elts are
701    constant, and TREE_STATIC is set if, in addition, all elts are simple enough
702    constants that the assembler and linker can compute them.  */
703
704 static tree
705 process_init_constructor (type, init, elts)
706      tree type, init, *elts;
707 {
708   register tree tail;
709   /* List of the elements of the result constructor,
710      in reverse order.  */
711   register tree members = NULL;
712   register tree next1;
713   tree result;
714   int allconstant = 1;
715   int allsimple = 1;
716   int erroneous = 0;
717
718   /* Make TAIL be the list of elements to use for the initialization,
719      no matter how the data was given to us.  */
720
721   if (elts)
722     {
723       if (warn_missing_braces)
724         warning ("aggregate has a partly bracketed initializer");
725       tail = *elts;
726     }
727   else
728     tail = CONSTRUCTOR_ELTS (init);
729
730   /* Gobble as many elements as needed, and make a constructor or initial value
731      for each element of this aggregate.  Chain them together in result.
732      If there are too few, use 0 for each scalar ultimate component.  */
733
734   if (TREE_CODE (type) == ARRAY_TYPE)
735     {
736       tree domain = TYPE_DOMAIN (type);
737       register long len;
738       register int i;
739
740       if (domain)
741         len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
742                - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
743                + 1);
744       else
745         len = -1;  /* Take as many as there are */
746
747       for (i = 0; len < 0 || i < len; i++)
748         {
749           if (tail)
750             {
751               if (TREE_PURPOSE (tail)
752                   && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
753                       || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
754                 sorry ("non-trivial labeled initializers");
755
756               if (TREE_VALUE (tail) != 0)
757                 {
758                   tree tail1 = tail;
759                   next1 = digest_init (TREE_TYPE (type),
760                                        TREE_VALUE (tail), &tail1);
761                   if (next1 == error_mark_node)
762                     return next1;
763                   my_friendly_assert
764                     (same_type_ignoring_top_level_qualifiers_p
765                      (TREE_TYPE (type), TREE_TYPE (next1)),
766                      981123);
767                   my_friendly_assert (tail1 == 0
768                                       || TREE_CODE (tail1) == TREE_LIST, 319);
769                   if (tail == tail1 && len < 0)
770                     {
771                       error ("non-empty initializer for array of empty elements");
772                       /* Just ignore what we were supposed to use.  */
773                       tail1 = NULL_TREE;
774                     }
775                   tail = tail1;
776                 }
777               else
778                 {
779                   next1 = error_mark_node;
780                   tail = TREE_CHAIN (tail);
781                 }
782             }
783           else if (len < 0)
784             /* We're done.  */
785             break;
786           else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
787             {
788               /* If this type needs constructors run for
789                  default-initialization, we can't rely on the backend to do it
790                  for us, so build up TARGET_EXPRs.  If the type in question is
791                  a class, just build one up; if it's an array, recurse.  */
792
793               if (IS_AGGR_TYPE (TREE_TYPE (type)))
794                 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
795               else
796                 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
797               next1 = digest_init (TREE_TYPE (type), next1, 0);
798             }
799           else
800             /* The default zero-initialization is fine for us; don't
801                add anything to the CONSTRUCTOR.  */
802             break;
803
804           if (next1 == error_mark_node)
805             erroneous = 1;
806           else if (!TREE_CONSTANT (next1))
807             allconstant = 0;
808           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
809             allsimple = 0;
810           members = tree_cons (size_int (i), next1, members);
811         }
812     }
813   else if (TREE_CODE (type) == RECORD_TYPE)
814     {
815       register tree field;
816
817       if (tail)
818         {
819           if (TYPE_USES_VIRTUAL_BASECLASSES (type))
820             {
821               sorry ("initializer list for object of class with virtual base classes");
822               return error_mark_node;
823             }
824
825           if (TYPE_BINFO_BASETYPES (type))
826             {
827               sorry ("initializer list for object of class with base classes");
828               return error_mark_node;
829             }
830
831           if (TYPE_POLYMORPHIC_P (type))
832             {
833               sorry ("initializer list for object using virtual functions");
834               return error_mark_node;
835             }
836         }
837
838       for (field = TYPE_FIELDS (type); field;
839            field = TREE_CHAIN (field))
840         {
841           if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
842             {
843               members = tree_cons (field, integer_zero_node, members);
844               continue;
845             }
846
847           if (TREE_CODE (field) != FIELD_DECL)
848             continue;
849
850           if (tail)
851             {
852               if (TREE_PURPOSE (tail)
853                   && TREE_PURPOSE (tail) != field
854                   && TREE_PURPOSE (tail) != DECL_NAME (field))
855                 sorry ("non-trivial labeled initializers");
856
857               if (TREE_VALUE (tail) != 0)
858                 {
859                   tree tail1 = tail;
860
861                   next1 = digest_init (TREE_TYPE (field),
862                                        TREE_VALUE (tail), &tail1);
863                   my_friendly_assert (tail1 == 0
864                                       || TREE_CODE (tail1) == TREE_LIST, 320);
865                   tail = tail1;
866                 }
867               else
868                 {
869                   next1 = error_mark_node;
870                   tail = TREE_CHAIN (tail);
871                 }
872             }
873           else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
874             {
875               /* If this type needs constructors run for
876                  default-initialization, we can't rely on the backend to do it
877                  for us, so build up TARGET_EXPRs.  If the type in question is
878                  a class, just build one up; if it's an array, recurse.  */
879
880               if (IS_AGGR_TYPE (TREE_TYPE (field)))
881                 next1 = build_functional_cast (TREE_TYPE (field),
882                                                NULL_TREE);
883               else
884                 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE,
885                                NULL_TREE);
886               next1 = digest_init (TREE_TYPE (field), next1, 0);
887
888               /* Warn when some struct elements are implicitly initialized.  */
889               if (extra_warnings)
890                 cp_warning ("missing initializer for member `%D'", field);
891             }
892           else
893             {
894               if (TREE_READONLY (field))
895                 cp_error ("uninitialized const member `%D'", field);
896               else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
897                        && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
898                 cp_error ("member `%D' with uninitialized const fields",
899                           field);
900               else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
901                 cp_error ("member `%D' is uninitialized reference", field);
902
903               /* Warn when some struct elements are implicitly initialized
904                  to zero.  */
905               if (extra_warnings)
906                 cp_warning ("missing initializer for member `%D'", field);
907
908               /* The default zero-initialization is fine for us; don't
909                  add anything to the CONSTRUCTOR.  */
910               continue;
911             }
912
913           if (next1 == error_mark_node)
914             erroneous = 1;
915           else if (!TREE_CONSTANT (next1))
916             allconstant = 0;
917           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
918             allsimple = 0;
919           members = tree_cons (field, next1, members);
920         }
921     }
922   else if (TREE_CODE (type) == UNION_TYPE
923            /* If the initializer was empty, use default zero initialization.  */
924            && tail)
925     {
926       register tree field = TYPE_FIELDS (type);
927
928       /* Find the first named field.  ANSI decided in September 1990
929          that only named fields count here.  */
930       while (field && (DECL_NAME (field) == 0
931                        || TREE_CODE (field) != FIELD_DECL))
932         field = TREE_CHAIN (field);
933
934       /* If this element specifies a field, initialize via that field.  */
935       if (TREE_PURPOSE (tail) != NULL_TREE)
936         {
937           int win = 0;
938
939           if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
940             /* Handle the case of a call by build_c_cast.  */
941             field = TREE_PURPOSE (tail), win = 1;
942           else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
943             error ("index value instead of field name in union initializer");
944           else
945             {
946               tree temp;
947               for (temp = TYPE_FIELDS (type);
948                    temp;
949                    temp = TREE_CHAIN (temp))
950                 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
951                   break;
952               if (temp)
953                 field = temp, win = 1;
954               else
955                 cp_error ("no field `%D' in union being initialized",
956                           TREE_PURPOSE (tail));
957             }
958           if (!win)
959             TREE_VALUE (tail) = error_mark_node;
960         }
961       else if (field == 0)
962         {
963           cp_error ("union `%T' with no named members cannot be initialized",
964                     type);
965           TREE_VALUE (tail) = error_mark_node;
966         }
967
968       if (TREE_VALUE (tail) != 0)
969         {
970           tree tail1 = tail;
971
972           next1 = digest_init (TREE_TYPE (field),
973                                TREE_VALUE (tail), &tail1);
974           if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
975             my_friendly_abort (357);
976           tail = tail1;
977         }
978       else
979         {
980           next1 = error_mark_node;
981           tail = TREE_CHAIN (tail);
982         }
983
984       if (next1 == error_mark_node)
985         erroneous = 1;
986       else if (!TREE_CONSTANT (next1))
987         allconstant = 0;
988       else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
989         allsimple = 0;
990       members = tree_cons (field, next1, members);
991     }
992
993   /* If arguments were specified as a list, just remove the ones we used.  */
994   if (elts)
995     *elts = tail;
996   /* If arguments were specified as a constructor,
997      complain unless we used all the elements of the constructor.  */
998   else if (tail)
999     pedwarn ("excess elements in aggregate initializer");
1000
1001   if (erroneous)
1002     return error_mark_node;
1003
1004   result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1005   if (init)
1006     TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1007   if (allconstant) TREE_CONSTANT (result) = 1;
1008   if (allconstant && allsimple) TREE_STATIC (result) = 1;
1009   return result;
1010 }
1011 \f
1012 /* Given a structure or union value DATUM, construct and return
1013    the structure or union component which results from narrowing
1014    that value by the type specified in BASETYPE.  For example, given the
1015    hierarchy
1016
1017    class L { int ii; };
1018    class A : L { ... };
1019    class B : L { ... };
1020    class C : A, B { ... };
1021
1022    and the declaration
1023
1024    C x;
1025
1026    then the expression
1027
1028    x.A::ii refers to the ii member of the L part of
1029    the A part of the C object named by X.  In this case,
1030    DATUM would be x, and BASETYPE would be A.
1031
1032    Note that this is nonconformant; the standard specifies that first
1033    we look up ii in A, then convert x to an L& and pull out the ii part.
1034    But narrowing seems to be standard practice, so let's do it anyway.  */
1035
1036 tree
1037 build_scoped_ref (datum, basetype)
1038      tree datum;
1039      tree basetype;
1040 {
1041   tree ref;
1042   tree type = TREE_TYPE (datum);
1043
1044   if (datum == error_mark_node)
1045     return error_mark_node;
1046
1047   /* Don't do this if it would cause an error or if we're being pedantic.  */
1048   if (! ACCESSIBLY_UNIQUELY_DERIVED_P (basetype, type)
1049       || pedantic)
1050     return datum;
1051
1052   ref = build_unary_op (ADDR_EXPR, datum, 0);
1053   ref = convert_pointer_to (basetype, ref);
1054
1055   return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1056 }
1057
1058 /* Build a reference to an object specified by the C++ `->' operator.
1059    Usually this just involves dereferencing the object, but if the
1060    `->' operator is overloaded, then such overloads must be
1061    performed until an object which does not have the `->' operator
1062    overloaded is found.  An error is reported when circular pointer
1063    delegation is detected.  */
1064
1065 tree
1066 build_x_arrow (datum)
1067      tree datum;
1068 {
1069   tree types_memoized = NULL_TREE;
1070   register tree rval = datum;
1071   tree type = TREE_TYPE (rval);
1072   tree last_rval = NULL_TREE;
1073
1074   if (type == error_mark_node)
1075     return error_mark_node;
1076
1077   if (processing_template_decl)
1078     return build_min_nt (ARROW_EXPR, rval);
1079
1080   if (TREE_CODE (rval) == OFFSET_REF)
1081     {
1082       rval = resolve_offset_ref (datum);
1083       type = TREE_TYPE (rval);
1084     }
1085
1086   if (TREE_CODE (type) == REFERENCE_TYPE)
1087     {
1088       rval = convert_from_reference (rval);
1089       type = TREE_TYPE (rval);
1090     }
1091
1092   if (IS_AGGR_TYPE (type))
1093     {
1094       while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval,
1095                                      NULL_TREE, NULL_TREE)))
1096         {
1097           if (rval == error_mark_node)
1098             return error_mark_node;
1099
1100           if (value_member (TREE_TYPE (rval), types_memoized))
1101             {
1102               error ("circular pointer delegation detected");
1103               return error_mark_node;
1104             }
1105           else
1106             {
1107               types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1108                                           types_memoized);
1109             }
1110           last_rval = rval;
1111         }     
1112
1113       if (last_rval == NULL_TREE)
1114         {
1115           cp_error ("base operand of `->' has non-pointer type `%T'", type);
1116           return error_mark_node;
1117         }
1118
1119       if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1120         last_rval = convert_from_reference (last_rval);
1121     }
1122   else
1123     last_rval = default_conversion (rval);
1124
1125   if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1126     return build_indirect_ref (last_rval, NULL_PTR);
1127
1128   if (types_memoized)
1129     error ("result of `operator->()' yields non-pointer result");
1130   else
1131     error ("base operand of `->' is not a pointer");
1132   return error_mark_node;
1133 }
1134
1135 /* Make an expression to refer to the COMPONENT field of
1136    structure or union value DATUM.  COMPONENT is an arbitrary
1137    expression.  DATUM has not already been checked out to be of
1138    aggregate type.
1139
1140    For C++, COMPONENT may be a TREE_LIST.  This happens when we must
1141    return an object of member type to a method of the current class,
1142    but there is not yet enough typing information to know which one.
1143    As a special case, if there is only one method by that name,
1144    it is returned.  Otherwise we return an expression which other
1145    routines will have to know how to deal with later.  */
1146
1147 tree
1148 build_m_component_ref (datum, component)
1149      tree datum, component;
1150 {
1151   tree type;
1152   tree objtype = TREE_TYPE (datum);
1153   tree rettype;
1154   tree binfo;
1155
1156   if (processing_template_decl)
1157     return build_min_nt (DOTSTAR_EXPR, datum, component);
1158
1159   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1160     {
1161       type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1162       rettype = type;
1163     }
1164   else
1165     {
1166       type = TREE_TYPE (TREE_TYPE (component));
1167       rettype = TREE_TYPE (type);
1168     }
1169
1170   if (datum == error_mark_node || component == error_mark_node)
1171     return error_mark_node;
1172
1173   if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1174     {
1175       cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1176       return error_mark_node;
1177     }
1178
1179   if (TREE_CODE (objtype) == REFERENCE_TYPE)
1180     objtype = TREE_TYPE (objtype);
1181   objtype = TYPE_MAIN_VARIANT (objtype);
1182
1183   if (! IS_AGGR_TYPE (objtype))
1184     {
1185       cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1186       cp_error ("which is of non-aggregate type `%T'", objtype);
1187       return error_mark_node;
1188     }
1189
1190   binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1191   if (binfo == NULL_TREE)
1192     {
1193       cp_error ("member type `%T::' incompatible with object type `%T'",
1194                 TYPE_METHOD_BASETYPE (type), objtype);
1195       return error_mark_node;
1196     }
1197   else if (binfo == error_mark_node)
1198     return error_mark_node;
1199
1200   component = build (OFFSET_REF, rettype, datum, component);
1201   if (TREE_CODE (type) == OFFSET_TYPE)
1202     component = resolve_offset_ref (component);
1203   return component;
1204 }
1205
1206 /* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
1207
1208 tree
1209 build_functional_cast (exp, parms)
1210      tree exp;
1211      tree parms;
1212 {
1213   /* This is either a call to a constructor,
1214      or a C cast in C++'s `functional' notation.  */
1215   tree type;
1216
1217   if (exp == error_mark_node || parms == error_mark_node)
1218     return error_mark_node;
1219
1220   if (TREE_CODE (exp) == IDENTIFIER_NODE)
1221     {
1222       if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1223         /* Either an enum or an aggregate type.  */
1224         type = IDENTIFIER_TYPE_VALUE (exp);
1225       else
1226         {
1227           type = lookup_name (exp, 1);
1228           if (!type || TREE_CODE (type) != TYPE_DECL)
1229             {
1230               cp_error ("`%T' fails to be a typedef or built-in type", exp);
1231               return error_mark_node;
1232             }
1233           type = TREE_TYPE (type);
1234         }
1235     }
1236   else if (TREE_CODE (exp) == TYPE_DECL)
1237     type = TREE_TYPE (exp);
1238   else
1239     type = exp;
1240
1241   if (processing_template_decl)
1242     return build_min (CAST_EXPR, type, parms);
1243
1244   if (! IS_AGGR_TYPE (type))
1245     {
1246       /* this must build a C cast */
1247       if (parms == NULL_TREE)
1248         parms = integer_zero_node;
1249       else
1250         {
1251           if (TREE_CHAIN (parms) != NULL_TREE)
1252             pedwarn ("initializer list being treated as compound expression");
1253           parms = build_compound_expr (parms);
1254         }
1255
1256       return build_c_cast (type, parms);
1257     }
1258
1259   /* Prepare to evaluate as a call to a constructor.  If this expression
1260      is actually used, for example,
1261          
1262      return X (arg1, arg2, ...);
1263          
1264      then the slot being initialized will be filled in.  */
1265
1266   if (!complete_type_or_else (type, NULL_TREE))
1267     return error_mark_node;
1268   if (abstract_virtuals_error (NULL_TREE, type))
1269     return error_mark_node;
1270
1271   if (parms && TREE_CHAIN (parms) == NULL_TREE)
1272     return build_c_cast (type, TREE_VALUE (parms));
1273
1274   /* We need to zero-initialize POD types.  Let's do that for everything
1275      that doesn't need a constructor.  */
1276   if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1277       && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1278     {
1279       exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
1280       return get_target_expr (exp);
1281     }
1282
1283   exp = build_method_call (NULL_TREE, complete_ctor_identifier, parms,
1284                            TYPE_BINFO (type), LOOKUP_NORMAL);
1285
1286   if (exp == error_mark_node)
1287     return error_mark_node;
1288
1289   return build_cplus_new (type, exp);
1290 }
1291 \f
1292 /* Return the character string for the name that encodes the
1293    enumeral value VALUE in the domain TYPE.  */
1294
1295 char *
1296 enum_name_string (value, type)
1297      tree value;
1298      tree type;
1299 {
1300   register tree values = TYPE_VALUES (type);
1301
1302   my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1303
1304   while (values && ! tree_int_cst_equal (TREE_VALUE (values), value))
1305     values = TREE_CHAIN (values);
1306
1307   if (values == NULL_TREE)
1308     {
1309       char *buf = (char *) oballoc (16 + TYPE_NAME_LENGTH (type));
1310
1311       /* Value must have been cast.  */
1312       sprintf (buf, "(enum %s)%ld",
1313                TYPE_NAME_STRING (type), (long) TREE_INT_CST_LOW (value));
1314       return buf;
1315     }
1316   return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1317 }
1318
1319 #if 0
1320 /* Print out a language-specific error message for
1321    (Pascal) case or (C) switch statements.
1322    CODE tells what sort of message to print. 
1323    TYPE is the type of the switch index expression.
1324    NEW is the new value that we were trying to add.
1325    OLD is the old value that stopped us from adding it.  */
1326
1327 void
1328 report_case_error (code, type, new_value, old_value)
1329      int code;
1330      tree type;
1331      tree new_value, old_value;
1332 {
1333   if (code == 1)
1334     {
1335       if (new_value)
1336         error ("case label not within a switch statement");
1337       else
1338         error ("default label not within a switch statement");
1339     }
1340   else if (code == 2)
1341     {
1342       if (new_value == 0)
1343         {
1344           error ("multiple default labels in one switch");
1345           return;
1346         }
1347       if (TREE_CODE (new_value) == RANGE_EXPR)
1348         if (TREE_CODE (old_value) == RANGE_EXPR)
1349           {
1350             char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1351             if (TREE_CODE (type) == ENUMERAL_TYPE)
1352               sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1353                        enum_name_string (TREE_OPERAND (new_value, 0), type),
1354                        enum_name_string (TREE_OPERAND (new_value, 1), type),
1355                        enum_name_string (TREE_OPERAND (old_value, 0), type),
1356                        enum_name_string (TREE_OPERAND (old_value, 1), type));
1357             else
1358               sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1359                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1360                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1361                        TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1362                        TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1363             error (buf);
1364           }
1365         else
1366           {
1367             char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1368             if (TREE_CODE (type) == ENUMERAL_TYPE)
1369               sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1370                        enum_name_string (TREE_OPERAND (new_value, 0), type),
1371                        enum_name_string (TREE_OPERAND (new_value, 1), type),
1372                        enum_name_string (old_value, type));
1373             else
1374               sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1375                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1376                        TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1377                        TREE_INT_CST_LOW (old_value));
1378             error (buf);
1379           }
1380       else if (TREE_CODE (old_value) == RANGE_EXPR)
1381         {
1382           char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1383           if (TREE_CODE (type) == ENUMERAL_TYPE)
1384             sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1385                      enum_name_string (TREE_OPERAND (old_value, 0), type),
1386                      enum_name_string (TREE_OPERAND (old_value, 1), type),
1387                      enum_name_string (new_value, type));
1388           else
1389             sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1390                      TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1391                      TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1392                      TREE_INT_CST_LOW (new_value));
1393           error (buf);
1394         }
1395       else
1396         {
1397           if (TREE_CODE (type) == ENUMERAL_TYPE)
1398             error ("duplicate label `%s' in switch statement",
1399                    enum_name_string (new_value, type));
1400           else
1401             error ("duplicate label (%d) in switch statement",
1402                    TREE_INT_CST_LOW (new_value));
1403         }
1404     }
1405   else if (code == 3)
1406     {
1407       if (TREE_CODE (type) == ENUMERAL_TYPE)
1408         warning ("case value out of range for enum %s",
1409                  TYPE_NAME_STRING (type));
1410       else
1411         warning ("case value out of range");
1412     }
1413   else if (code == 4)
1414     {
1415       if (TREE_CODE (type) == ENUMERAL_TYPE)
1416         error ("range values `%s' and `%s' reversed",
1417                enum_name_string (new_value, type),
1418                enum_name_string (old_value, type));
1419       else
1420         error ("range values reversed");
1421     }
1422 }
1423 #endif
1424
1425 /* Complain about defining new types in inappropriate places.  We give an
1426    exception for C-style casts, to accommodate GNU C stylings.  */
1427
1428 void
1429 check_for_new_type (string, inptree)
1430      const char *string;
1431      flagged_type_tree inptree;
1432 {
1433   if (inptree.new_type_flag
1434       && (pedantic || strcmp (string, "cast") != 0))
1435     pedwarn ("ISO C++ forbids defining types within %s", string);
1436 }
1437
1438 /* Add new exception specifier SPEC, to the LIST we currently have.
1439    If it's already in LIST then do nothing.
1440    Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1441    know what we're doing.  */
1442
1443 tree
1444 add_exception_specifier (list, spec, complain)
1445      tree list, spec;
1446      int complain;
1447 {
1448   int ok;
1449   tree core = spec;
1450   int is_ptr;
1451   
1452   if (spec == error_mark_node)
1453     return list;
1454   
1455   my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1456   
1457   /* [except.spec] 1, type in an exception specifier shall not be
1458      incomplete, or pointer or ref to incomplete other than pointer
1459      to cv void.  */
1460   is_ptr = TREE_CODE (core) == POINTER_TYPE;
1461   if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1462     core = TREE_TYPE (core);
1463   if (complain < 0)
1464     ok = 1;
1465   else if (VOID_TYPE_P (core))
1466     ok = is_ptr;
1467   else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1468     ok = 1;
1469   else
1470     ok = COMPLETE_TYPE_P (complete_type (core));
1471   
1472   if (ok)
1473     {
1474       tree probe;
1475       
1476       for (probe = list; probe; probe = TREE_CHAIN (probe))
1477         if (same_type_p (TREE_VALUE (probe), spec))
1478           break;
1479       if (!probe)
1480         {
1481           spec = build_decl_list (NULL_TREE, spec);
1482           TREE_CHAIN (spec) = list;
1483           list = spec;
1484         }
1485     }
1486   else if (complain)
1487     incomplete_type_error (NULL_TREE, core);
1488   return list;
1489 }