OSDN Git Service

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