Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / cp / typeck2.c
blobfae966874a12f0d59b053a662c6cb683f7d3073d
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987, 88, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
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.
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.
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. */
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.
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). */
33 #include "config.h"
34 #include <stdio.h>
35 #include "tree.h"
36 #include "cp-tree.h"
37 #include "flags.h"
39 static tree process_init_constructor PROTO((tree, tree, tree *));
41 extern int errorcount;
42 extern int sorrycount;
44 /* Print an error message stemming from an attempt to use
45 BASETYPE as a base class for TYPE. */
47 tree
48 error_not_base_type (basetype, type)
49 tree basetype, type;
51 if (TREE_CODE (basetype) == FUNCTION_DECL)
52 basetype = DECL_CLASS_CONTEXT (basetype);
53 cp_error ("type `%T' is not a base type for type `%T'", basetype, type);
54 return error_mark_node;
57 tree
58 binfo_or_else (parent_or_type, type)
59 tree parent_or_type, type;
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)))
66 if (binfo == error_mark_node)
67 return NULL_TREE;
68 return binfo;
70 error_not_base_type (parent_or_type, type);
71 return NULL_TREE;
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.) */
79 void
80 readonly_error (arg, string, soft)
81 tree arg;
82 char *string;
83 int soft;
85 char *fmt;
86 void (*fn)();
88 if (soft)
89 fn = cp_pedwarn;
90 else
91 fn = cp_error;
93 if (TREE_CODE (arg) == COMPONENT_REF)
95 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
96 fmt = "%s of member `%D' in read-only structure";
97 else
98 fmt = "%s of read-only member `%D'";
99 (*fn) (fmt, string, TREE_OPERAND (arg, 1));
101 else if (TREE_CODE (arg) == VAR_DECL)
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);
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
121 (*fn) ("%s of read-only location", string);
124 /* Print an error message for invalid use of a type which declares
125 virtual functions which are not inheritable. */
127 void
128 abstract_virtuals_error (decl, type)
129 tree decl;
130 tree type;
132 tree u = CLASSTYPE_ABSTRACT_VIRTUALS (type);
134 if (decl)
136 if (TREE_CODE (decl) == RESULT_DECL)
137 return;
139 if (TREE_CODE (decl) == VAR_DECL)
140 cp_error ("cannot declare variable `%D' to be of type `%T'",
141 decl, type);
142 else if (TREE_CODE (decl) == PARM_DECL)
143 cp_error ("cannot declare parameter `%D' to be of type `%T'",
144 decl, type);
145 else if (TREE_CODE (decl) == FIELD_DECL)
146 cp_error ("cannot declare field `%D' to be of type `%T'",
147 decl, type);
148 else if (TREE_CODE (decl) == FUNCTION_DECL
149 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
150 cp_error ("invalid return type for method `%#D'", decl);
151 else if (TREE_CODE (decl) == FUNCTION_DECL)
152 cp_error ("invalid return type for function `%#D'", decl);
154 else cp_error ("cannot allocate an object of type `%T'", type);
155 /* Only go through this once. */
156 if (TREE_PURPOSE (u) == NULL_TREE)
158 error (" since the following virtual functions are abstract:");
159 TREE_PURPOSE (u) = error_mark_node;
160 while (u)
162 cp_error ("\t%#D", TREE_VALUE (u));
163 u = TREE_CHAIN (u);
166 else cp_error (" since type `%T' has abstract virtual functions", type);
169 /* Print an error message for invalid use of a signature type.
170 Signatures are treated similar to abstract classes here, they
171 cannot be instantiated. */
173 void
174 signature_error (decl, type)
175 tree decl;
176 tree type;
178 if (decl)
180 if (TREE_CODE (decl) == RESULT_DECL)
181 return;
183 if (TREE_CODE (decl) == VAR_DECL)
184 cp_error ("cannot declare variable `%D' to be of signature type `%T'",
185 decl, type);
186 else if (TREE_CODE (decl) == PARM_DECL)
187 cp_error ("cannot declare parameter `%D' to be of signature type `%T'",
188 decl, type);
189 else if (TREE_CODE (decl) == FIELD_DECL)
190 cp_error ("cannot declare field `%D' to be of signature type `%T'",
191 decl, type);
192 else if (TREE_CODE (decl) == FUNCTION_DECL
193 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
194 cp_error ("invalid return type for method `%#D'", decl);
195 else if (TREE_CODE (decl) == FUNCTION_DECL)
196 cp_error ("invalid return type for function `%#D'", decl);
198 else
199 cp_error ("cannot allocate an object of signature type `%T'", type);
202 /* Print an error message for invalid use of an incomplete type.
203 VALUE is the expression that was used (or 0 if that isn't known)
204 and TYPE is the type that was invalid. */
206 void
207 incomplete_type_error (value, type)
208 tree value;
209 tree type;
211 char *errmsg;
213 /* Avoid duplicate error message. */
214 if (TREE_CODE (type) == ERROR_MARK)
215 return;
217 if (value != 0 && (TREE_CODE (value) == VAR_DECL
218 || TREE_CODE (value) == PARM_DECL))
219 cp_error ("`%D' has incomplete type", value);
220 else
222 retry:
223 /* We must print an error message. Be clever about what it says. */
225 switch (TREE_CODE (type))
227 case RECORD_TYPE:
228 case UNION_TYPE:
229 case ENUMERAL_TYPE:
230 errmsg = "invalid use of undefined type `%#T'";
231 break;
233 case VOID_TYPE:
234 error ("invalid use of void expression");
235 return;
237 case ARRAY_TYPE:
238 if (TYPE_DOMAIN (type))
240 type = TREE_TYPE (type);
241 goto retry;
243 error ("invalid use of array with unspecified bounds");
244 return;
246 case OFFSET_TYPE:
247 error ("invalid use of member type (did you forget the `&' ?)");
248 return;
250 default:
251 my_friendly_abort (108);
254 cp_error (errmsg, type);
258 /* Like error(), but don't call report_error_function(). */
260 static void
261 ack (s, v, v2)
262 char *s;
263 HOST_WIDE_INT v;
264 HOST_WIDE_INT v2;
266 extern char * progname;
268 if (input_filename)
269 fprintf (stderr, "%s:%d: ", input_filename, lineno);
270 else
271 fprintf (stderr, "%s: ", progname);
273 fprintf (stderr, s, v, v2);
274 fprintf (stderr, "\n");
277 /* There are times when the compiler can get very confused, confused
278 to the point of giving up by aborting, simply because of previous
279 input errors. It is much better to have the user go back and
280 correct those errors first, and see if it makes us happier, than it
281 is to abort on him. This is because when one has a 10,000 line
282 program, and the compiler comes back with ``core dump'', the user
283 is left not knowing even where to begin to fix things and no place
284 to even try and work around things.
286 The parameter is to uniquely identify the problem to the user, so
287 that they can say, I am having problem 59, and know that fix 7 will
288 probably solve their problem. Or, we can document what problem
289 59 is, so they can understand how to work around it, should they
290 ever run into it.
292 Note, there will be no more calls in the C++ front end to abort,
293 because the C++ front end is so unreliable still. The C front end
294 can get away with calling abort, because for most of the calls to
295 abort on most machines, it, I suspect, can be proven that it is
296 impossible to ever call abort. The same is not yet true for C++,
297 one day, maybe it will be.
299 We used to tell people to "fix the above error[s] and try recompiling
300 the program" via a call to fatal, but that message tended to look
301 silly. So instead, we just do the equivalent of a call to fatal in the
302 same situation (call exit). */
304 /* First used: 0 (reserved), Last used: 369. Free: */
306 static int abortcount = 0;
308 void
309 my_friendly_abort (i)
310 int i;
312 /* if the previous error came through here, i.e. report_error_function
313 ended up calling us again, don't just exit; we want a diagnostic of
314 some kind. */
315 if (abortcount == 1)
316 current_function_decl = NULL_TREE;
317 else if (errorcount > 0 || sorrycount > 0)
319 if (abortcount > 1)
321 if (i == 0)
322 ack ("Internal compiler error.");
323 else
324 ack ("Internal compiler error %d.", i);
325 ack ("Please submit a full bug report to `bug-g++@prep.ai.mit.edu'.");
327 else
328 error ("confused by earlier errors, bailing out");
330 exit (34);
332 ++abortcount;
334 if (i == 0)
335 error ("Internal compiler error.");
336 else
337 error ("Internal compiler error %d.", i);
339 fatal ("Please submit a full bug report to `bug-g++@prep.ai.mit.edu'.");
342 void
343 my_friendly_assert (cond, where)
344 int cond, where;
346 if (cond == 0)
347 my_friendly_abort (where);
350 /* Return nonzero if VALUE is a valid constant-valued expression
351 for use in initializing a static variable; one that can be an
352 element of a "constant" initializer.
354 Return null_pointer_node if the value is absolute;
355 if it is relocatable, return the variable that determines the relocation.
356 We assume that VALUE has been folded as much as possible;
357 therefore, we do not need to check for such things as
358 arithmetic-combinations of integers. */
360 tree
361 initializer_constant_valid_p (value, endtype)
362 tree value;
363 tree endtype;
365 switch (TREE_CODE (value))
367 case CONSTRUCTOR:
368 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
369 && TREE_CONSTANT (value))
370 return
371 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
372 endtype);
374 return TREE_STATIC (value) ? null_pointer_node : 0;
376 case INTEGER_CST:
377 case REAL_CST:
378 case STRING_CST:
379 case COMPLEX_CST:
380 return null_pointer_node;
382 case ADDR_EXPR:
383 return TREE_OPERAND (value, 0);
385 case NON_LVALUE_EXPR:
386 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
388 case CONVERT_EXPR:
389 case NOP_EXPR:
390 /* Allow conversions between pointer types. */
391 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
392 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
393 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
395 /* Allow conversions between real types. */
396 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
397 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
398 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
400 /* Allow length-preserving conversions between integer types. */
401 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
402 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
403 && (TYPE_PRECISION (TREE_TYPE (value))
404 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
405 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
407 /* Allow conversions between other integer types only if
408 explicit value. */
409 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
410 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
412 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
413 endtype);
414 if (inner == null_pointer_node)
415 return null_pointer_node;
416 return 0;
419 /* Allow (int) &foo provided int is as wide as a pointer. */
420 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
421 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
422 && (TYPE_PRECISION (TREE_TYPE (value))
423 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
424 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
425 endtype);
427 /* Likewise conversions from int to pointers. */
428 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
429 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
430 && (TYPE_PRECISION (TREE_TYPE (value))
431 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
432 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
433 endtype);
435 /* Allow conversions to union types if the value inside is okay. */
436 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
437 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
438 endtype);
439 return 0;
441 case PLUS_EXPR:
442 if ((TREE_CODE (endtype) == INTEGER_TYPE)
443 && (TYPE_PRECISION (endtype) < POINTER_SIZE))
444 return 0;
446 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
447 endtype);
448 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
449 endtype);
450 /* If either term is absolute, use the other terms relocation. */
451 if (valid0 == null_pointer_node)
452 return valid1;
453 if (valid1 == null_pointer_node)
454 return valid0;
455 return 0;
458 case MINUS_EXPR:
459 if ((TREE_CODE (endtype) == INTEGER_TYPE)
460 && (TYPE_PRECISION (endtype) < POINTER_SIZE))
461 return 0;
463 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
464 endtype);
465 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
466 endtype);
467 /* Win if second argument is absolute. */
468 if (valid1 == null_pointer_node)
469 return valid0;
470 /* Win if both arguments have the same relocation.
471 Then the value is absolute. */
472 if (valid0 == valid1)
473 return null_pointer_node;
474 return 0;
478 return 0;
481 /* Perform appropriate conversions on the initial value of a variable,
482 store it in the declaration DECL,
483 and print any error messages that are appropriate.
484 If the init is invalid, store an ERROR_MARK.
486 C++: Note that INIT might be a TREE_LIST, which would mean that it is
487 a base class initializer for some aggregate type, hopefully compatible
488 with DECL. If INIT is a single element, and DECL is an aggregate
489 type, we silently convert INIT into a TREE_LIST, allowing a constructor
490 to be called.
492 If INIT is a TREE_LIST and there is no constructor, turn INIT
493 into a CONSTRUCTOR and use standard initialization techniques.
494 Perhaps a warning should be generated?
496 Returns value of initializer if initialization could not be
497 performed for static variable. In that case, caller must do
498 the storing. */
500 tree
501 store_init_value (decl, init)
502 tree decl, init;
504 register tree value, type;
506 /* If variable's type was invalidly declared, just ignore it. */
508 type = TREE_TYPE (decl);
509 if (TREE_CODE (type) == ERROR_MARK)
510 return NULL_TREE;
512 #if 0
513 /* This breaks arrays, and should not have any effect for other decls. */
514 /* Take care of C++ business up here. */
515 type = TYPE_MAIN_VARIANT (type);
516 #endif
518 if (IS_AGGR_TYPE (type))
520 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
521 && TREE_CODE (init) != CONSTRUCTOR)
522 my_friendly_abort (109);
524 /* Although we are not allowed to declare variables of signature
525 type, we complain about a possible constructor call in such a
526 declaration as well. */
527 if (TREE_CODE (init) == TREE_LIST
528 && IS_SIGNATURE (type))
530 cp_error ("constructor syntax cannot be used with signature type `%T'",
531 type);
532 init = error_mark_node;
534 else if (TREE_CODE (init) == TREE_LIST)
536 cp_error ("constructor syntax used, but no constructor declared for type `%T'", type);
537 init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
539 #if 0
540 if (TREE_CODE (init) == CONSTRUCTOR)
542 tree field;
544 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */
545 if (CLASSTYPE_N_BASECLASSES (type))
546 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl);
547 if (CLASSTYPE_VTBL_PTR (type))
548 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl);
549 if (TYPE_NEEDS_CONSTRUCTING (type))
551 cp_error_at ("initializer list construction invalid for `%D'", decl);
552 error ("due to the presence of a constructor");
554 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
555 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
557 cp_error_at ("initializer list construction invalid for `%D'", decl);
558 cp_error_at ("due to non-public access of member `%D'", field);
560 for (field = TYPE_METHODS (type); field; field = TREE_CHAIN (field))
561 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
563 cp_error_at ("initializer list construction invalid for `%D'", decl);
564 cp_error_at ("due to non-public access of member `%D'", field);
567 #endif
569 else if (TREE_CODE (init) == TREE_LIST
570 && TREE_TYPE (init) != unknown_type_node)
572 if (TREE_CODE (decl) == RESULT_DECL)
574 if (TREE_CHAIN (init))
576 warning ("comma expression used to initialize return value");
577 init = build_compound_expr (init);
579 else
580 init = TREE_VALUE (init);
582 else if (TREE_TYPE (init) != 0
583 && TREE_CODE (TREE_TYPE (init)) == OFFSET_TYPE)
585 /* Use the type of our variable to instantiate
586 the type of our initializer. */
587 init = instantiate_type (type, init, 1);
589 else if (TREE_CODE (init) == TREE_LIST
590 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
592 error ("cannot initialize arrays using this syntax");
593 return NULL_TREE;
595 else
597 /* We get here with code like `int a (2);' */
599 if (TREE_CHAIN (init) != NULL_TREE)
601 pedwarn ("initializer list being treated as compound expression");
602 init = build_compound_expr (init);
604 else
605 init = TREE_VALUE (init);
609 if (TYPE_PTRMEMFUNC_P (type) && TREE_CODE (init) == CONSTRUCTOR
610 && TREE_TYPE (init) == NULL_TREE)
611 cp_pedwarn ("initializer list for `%T'", type);
613 /* End of special C++ code. */
615 /* Digest the specified initializer into an expression. */
617 value = digest_init (type, init, (tree *) 0);
619 /* Store the expression if valid; else report error. */
621 if (TREE_CODE (value) == ERROR_MARK)
623 /* Other code expects that initializers for objects of types that need
624 constructing never make it into DECL_INITIAL, and passes 'init' to
625 expand_aggr_init without checking DECL_INITIAL. So just return. */
626 else if (TYPE_NEEDS_CONSTRUCTING (type))
627 return value;
628 else if (TREE_STATIC (decl)
629 && (! TREE_CONSTANT (value)
630 || ! initializer_constant_valid_p (value, TREE_TYPE (value))
631 #if 0
632 /* A STATIC PUBLIC int variable doesn't have to be
633 run time inited when doing pic. (mrs) */
634 /* Since ctors and dtors are the only things that can
635 reference vtables, and they are always written down
636 the the vtable definition, we can leave the
637 vtables in initialized data space.
638 However, other initialized data cannot be initialized
639 this way. Instead a global file-level initializer
640 must do the job. */
641 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
642 #endif
645 return value;
646 #if 0 /* No, that's C. jason 9/19/94 */
647 else
649 if (pedantic && TREE_CODE (value) == CONSTRUCTOR
650 /* Don't complain about non-constant initializers of
651 signature tables and signature pointers/references. */
652 && ! (TYPE_LANG_SPECIFIC (type)
653 && (IS_SIGNATURE (type)
654 || IS_SIGNATURE_POINTER (type)
655 || IS_SIGNATURE_REFERENCE (type))))
657 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
658 pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
661 #endif
662 DECL_INITIAL (decl) = value;
663 return NULL_TREE;
666 /* Digest the parser output INIT as an initializer for type TYPE.
667 Return a C expression of type TYPE to represent the initial value.
669 If TAIL is nonzero, it points to a variable holding a list of elements
670 of which INIT is the first. We update the list stored there by
671 removing from the head all the elements that we use.
672 Normally this is only one; we use more than one element only if
673 TYPE is an aggregate and INIT is not a constructor. */
675 tree
676 digest_init (type, init, tail)
677 tree type, init, *tail;
679 enum tree_code code = TREE_CODE (type);
680 tree element = NULL_TREE;
681 tree old_tail_contents;
682 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
683 tree node which has no TREE_TYPE. */
684 int raw_constructor;
686 /* By default, assume we use one element from a list.
687 We correct this later in the sole case where it is not true. */
689 if (tail)
691 old_tail_contents = *tail;
692 *tail = TREE_CHAIN (*tail);
695 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
696 && TREE_VALUE (init) == error_mark_node))
697 return error_mark_node;
699 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
700 if (TREE_CODE (init) == NON_LVALUE_EXPR)
701 init = TREE_OPERAND (init, 0);
703 if (init && TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (type))
704 init = default_conversion (init);
706 if (init && TYPE_PTRMEMFUNC_P (type)
707 && ((TREE_CODE (init) == ADDR_EXPR
708 && ((TREE_CODE (TREE_TYPE (init)) == POINTER_TYPE
709 && TREE_CODE (TREE_TYPE (TREE_TYPE (init))) == METHOD_TYPE)
710 || TREE_CODE (TREE_OPERAND (init, 0)) == TREE_LIST))
711 || TREE_CODE (init) == TREE_LIST
712 || integer_zerop (init)
713 || (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))))
715 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), init, 0);
718 raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
720 if (init && raw_constructor
721 && CONSTRUCTOR_ELTS (init) != 0
722 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
724 element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
725 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
726 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
727 element = TREE_OPERAND (element, 0);
728 if (element == error_mark_node)
729 return element;
732 /* Any type can be initialized from an expression of the same type,
733 optionally with braces. */
735 if (init && TREE_TYPE (init)
736 && (TYPE_MAIN_VARIANT (TREE_TYPE (init)) == type
737 || (code == ARRAY_TYPE && comptypes (TREE_TYPE (init), type, 1))))
739 if (pedantic && code == ARRAY_TYPE
740 && TREE_CODE (init) != STRING_CST)
741 pedwarn ("ANSI C++ forbids initializing array from array expression");
742 if (TREE_CODE (init) == CONST_DECL)
743 init = DECL_INITIAL (init);
744 else if (TREE_READONLY_DECL_P (init))
745 init = decl_constant_value (init);
746 else if (IS_AGGR_TYPE (type) && TYPE_NEEDS_CONSTRUCTING (type))
747 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
748 LOOKUP_NORMAL);
749 return init;
752 if (element && (TREE_TYPE (element) == type
753 || (code == ARRAY_TYPE && TREE_TYPE (element)
754 && comptypes (TREE_TYPE (element), type, 1))))
756 if (pedantic && code == ARRAY_TYPE)
757 pedwarn ("ANSI C++ forbids initializing array from array expression");
758 if (pedantic && (code == RECORD_TYPE || code == UNION_TYPE))
759 pedwarn ("ANSI C++ forbids single nonscalar initializer with braces");
760 if (TREE_CODE (element) == CONST_DECL)
761 element = DECL_INITIAL (element);
762 else if (TREE_READONLY_DECL_P (element))
763 element = decl_constant_value (element);
764 return element;
767 /* Initialization of an array of chars from a string constant
768 optionally enclosed in braces. */
770 if (code == ARRAY_TYPE)
772 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
773 if ((typ1 == char_type_node
774 || typ1 == signed_char_type_node
775 || typ1 == unsigned_char_type_node
776 || typ1 == unsigned_wchar_type_node
777 || typ1 == signed_wchar_type_node)
778 && ((init && TREE_CODE (init) == STRING_CST)
779 || (element && TREE_CODE (element) == STRING_CST)))
781 tree string = element ? element : init;
783 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
784 != char_type_node)
785 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
787 error ("char-array initialized from wide string");
788 return error_mark_node;
790 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
791 == char_type_node)
792 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
794 error ("int-array initialized from non-wide string");
795 return error_mark_node;
798 if (pedantic
799 && typ1 != char_type_node
800 && typ1 != signed_char_type_node
801 && typ1 != unsigned_char_type_node)
802 pedwarn ("ANSI C++ forbids string initializer except for `char' elements");
803 TREE_TYPE (string) = type;
804 if (TYPE_DOMAIN (type) != 0
805 && TREE_CONSTANT (TYPE_SIZE (type)))
807 register int size
808 = TREE_INT_CST_LOW (TYPE_SIZE (type));
809 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
810 /* In C it is ok to subtract 1 from the length of the string
811 because it's ok to ignore the terminating null char that is
812 counted in the length of the constant, but in C++ this would
813 be invalid. */
814 if (size < TREE_STRING_LENGTH (string))
815 pedwarn ("initializer-string for array of chars is too long");
817 return string;
821 /* Handle scalar types, including conversions,
822 and signature pointers and references. */
824 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
825 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
826 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
827 || (code == RECORD_TYPE && ! raw_constructor
828 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))))
830 if (raw_constructor)
832 if (element == 0)
834 error ("initializer for scalar variable requires one element");
835 return error_mark_node;
837 init = element;
839 while (TREE_CODE (init) == CONSTRUCTOR
840 && ! (TREE_TYPE (init)
841 && TYPE_PTRMEMFUNC_P (TREE_TYPE (init))))
843 cp_pedwarn ("braces around scalar initializer for `%T'", type);
844 init = CONSTRUCTOR_ELTS (init);
845 if (TREE_CHAIN (init))
846 cp_pedwarn ("ignoring extra initializers for `%T'", type);
847 init = TREE_VALUE (init);
850 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
851 "initialization", NULL_TREE, 0);
854 /* Come here only for records and arrays (and unions with constructors). */
856 if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
858 cp_error ("variable-sized object of type `%T' may not be initialized",
859 type);
860 return error_mark_node;
863 if (code == ARRAY_TYPE || code == RECORD_TYPE || code == UNION_TYPE)
865 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type))
867 cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
868 type, init);
869 return error_mark_node;
871 else if (raw_constructor)
872 return process_init_constructor (type, init, (tree *)0);
873 else if (TYPE_NON_AGGREGATE_CLASS (type))
875 int flags = LOOKUP_NORMAL;
876 /* Initialization from { } is copy-initialization. */
877 if (tail)
878 flags |= LOOKUP_ONLYCONVERTING;
879 return convert_for_initialization (0, type, init, flags,
880 "initialization", NULL_TREE, 0);
882 else if (tail != 0)
884 *tail = old_tail_contents;
885 return process_init_constructor (type, 0, tail);
888 if (code != ARRAY_TYPE)
889 return convert_for_initialization (NULL_TREE, type, init, LOOKUP_NORMAL,
890 "initialization", NULL_TREE, 0);
893 error ("invalid initializer");
894 return error_mark_node;
897 /* Process a constructor for a variable of type TYPE.
898 The constructor elements may be specified either with INIT or with ELTS,
899 only one of which should be non-null.
901 If INIT is specified, it is a CONSTRUCTOR node which is specifically
902 and solely for initializing this datum.
904 If ELTS is specified, it is the address of a variable containing
905 a list of expressions. We take as many elements as we need
906 from the head of the list and update the list.
908 In the resulting constructor, TREE_CONSTANT is set if all elts are
909 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
910 constants that the assembler and linker can compute them. */
912 static tree
913 process_init_constructor (type, init, elts)
914 tree type, init, *elts;
916 register tree tail;
917 /* List of the elements of the result constructor,
918 in reverse order. */
919 register tree members = NULL;
920 tree result;
921 int allconstant = 1;
922 int allsimple = 1;
923 int erroneous = 0;
925 /* Make TAIL be the list of elements to use for the initialization,
926 no matter how the data was given to us. */
928 if (elts)
930 if (warn_missing_braces)
931 warning ("aggregate has a partly bracketed initializer");
932 tail = *elts;
934 else
935 tail = CONSTRUCTOR_ELTS (init);
937 /* Gobble as many elements as needed, and make a constructor or initial value
938 for each element of this aggregate. Chain them together in result.
939 If there are too few, use 0 for each scalar ultimate component. */
941 if (TREE_CODE (type) == ARRAY_TYPE)
943 tree domain = TYPE_DOMAIN (type);
944 register long len;
945 register int i;
947 if (domain)
948 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
949 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
950 + 1);
951 else
952 len = -1; /* Take as many as there are */
954 for (i = 0; (len < 0 || i < len) && tail != 0; i++)
956 register tree next1;
958 if (TREE_VALUE (tail) != 0)
960 tree tail1 = tail;
961 next1 = digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
962 TREE_VALUE (tail), &tail1);
963 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type))
964 && TYPE_MAIN_VARIANT (TREE_TYPE (type)) != TYPE_MAIN_VARIANT (TREE_TYPE (next1)))
966 /* The fact this needs to be done suggests this code needs
967 to be totally rewritten. */
968 next1 = convert_for_initialization (NULL_TREE, TREE_TYPE (type), next1, LOOKUP_NORMAL, "initialization", NULL_TREE, 0);
970 my_friendly_assert (tail1 == 0
971 || TREE_CODE (tail1) == TREE_LIST, 319);
972 if (tail == tail1 && len < 0)
974 error ("non-empty initializer for array of empty elements");
975 /* Just ignore what we were supposed to use. */
976 tail1 = NULL_TREE;
978 tail = tail1;
980 else
982 next1 = error_mark_node;
983 tail = TREE_CHAIN (tail);
986 if (next1 == error_mark_node)
987 erroneous = 1;
988 else if (!TREE_CONSTANT (next1))
989 allconstant = 0;
990 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
991 allsimple = 0;
992 members = expr_tree_cons (NULL_TREE, next1, members);
995 if (TREE_CODE (type) == RECORD_TYPE)
997 register tree field;
999 if (tail)
1001 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1003 sorry ("initializer list for object of class with virtual baseclasses");
1004 return error_mark_node;
1007 if (TYPE_BINFO_BASETYPES (type))
1009 sorry ("initializer list for object of class with baseclasses");
1010 return error_mark_node;
1013 if (TYPE_VIRTUAL_P (type))
1015 sorry ("initializer list for object using virtual functions");
1016 return error_mark_node;
1020 for (field = TYPE_FIELDS (type); field && tail;
1021 field = TREE_CHAIN (field))
1023 register tree next1;
1025 if (! DECL_NAME (field))
1027 members = expr_tree_cons (field, integer_zero_node, members);
1028 continue;
1031 if (TREE_CODE (field) != FIELD_DECL)
1032 continue;
1034 if (TREE_VALUE (tail) != 0)
1036 tree tail1 = tail;
1038 next1 = digest_init (TREE_TYPE (field),
1039 TREE_VALUE (tail), &tail1);
1040 my_friendly_assert (tail1 == 0
1041 || TREE_CODE (tail1) == TREE_LIST, 320);
1042 tail = tail1;
1044 else
1046 next1 = error_mark_node;
1047 tail = TREE_CHAIN (tail);
1050 if (next1 == error_mark_node)
1051 erroneous = 1;
1052 else if (!TREE_CONSTANT (next1))
1053 allconstant = 0;
1054 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1055 allsimple = 0;
1056 members = expr_tree_cons (field, next1, members);
1058 for (; field; field = TREE_CHAIN (field))
1060 if (TREE_CODE (field) != FIELD_DECL)
1061 continue;
1063 /* Does this field have a default initialization? */
1064 if (DECL_INITIAL (field))
1066 register tree next1 = DECL_INITIAL (field);
1067 if (TREE_CODE (next1) == ERROR_MARK)
1068 erroneous = 1;
1069 else if (!TREE_CONSTANT (next1))
1070 allconstant = 0;
1071 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1072 allsimple = 0;
1073 members = expr_tree_cons (field, next1, members);
1075 else if (TREE_READONLY (field))
1076 error ("uninitialized const member `%s'",
1077 IDENTIFIER_POINTER (DECL_NAME (field)));
1078 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
1079 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1080 error ("member `%s' with uninitialized const fields",
1081 IDENTIFIER_POINTER (DECL_NAME (field)));
1082 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1083 error ("member `%s' is uninitialized reference",
1084 IDENTIFIER_POINTER (DECL_NAME (field)));
1088 if (TREE_CODE (type) == UNION_TYPE)
1090 register tree field = TYPE_FIELDS (type);
1091 register tree next1;
1093 /* Find the first named field. ANSI decided in September 1990
1094 that only named fields count here. */
1095 while (field && (DECL_NAME (field) == 0
1096 || TREE_CODE (field) != FIELD_DECL))
1097 field = TREE_CHAIN (field);
1099 /* If this element specifies a field, initialize via that field. */
1100 if (TREE_PURPOSE (tail) != NULL_TREE)
1102 int win = 0;
1104 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
1105 /* Handle the case of a call by build_c_cast. */
1106 field = TREE_PURPOSE (tail), win = 1;
1107 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
1108 error ("index value instead of field name in union initializer");
1109 else
1111 tree temp;
1112 for (temp = TYPE_FIELDS (type);
1113 temp;
1114 temp = TREE_CHAIN (temp))
1115 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
1116 break;
1117 if (temp)
1118 field = temp, win = 1;
1119 else
1120 error ("no field `%s' in union being initialized",
1121 IDENTIFIER_POINTER (TREE_PURPOSE (tail)));
1123 if (!win)
1124 TREE_VALUE (tail) = error_mark_node;
1126 else if (field == 0)
1128 cp_error ("union `%T' with no named members cannot be initialized",
1129 type);
1130 TREE_VALUE (tail) = error_mark_node;
1133 if (TREE_VALUE (tail) != 0)
1135 tree tail1 = tail;
1137 next1 = digest_init (TREE_TYPE (field),
1138 TREE_VALUE (tail), &tail1);
1139 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
1140 my_friendly_abort (357);
1141 tail = tail1;
1143 else
1145 next1 = error_mark_node;
1146 tail = TREE_CHAIN (tail);
1149 if (next1 == error_mark_node)
1150 erroneous = 1;
1151 else if (!TREE_CONSTANT (next1))
1152 allconstant = 0;
1153 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
1154 allsimple = 0;
1155 members = expr_tree_cons (field, next1, members);
1158 /* If arguments were specified as a list, just remove the ones we used. */
1159 if (elts)
1160 *elts = tail;
1161 /* If arguments were specified as a constructor,
1162 complain unless we used all the elements of the constructor. */
1163 else if (tail)
1164 pedwarn ("excess elements in aggregate initializer");
1166 if (erroneous)
1167 return error_mark_node;
1169 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1170 if (init)
1171 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1172 if (allconstant) TREE_CONSTANT (result) = 1;
1173 if (allconstant && allsimple) TREE_STATIC (result) = 1;
1174 return result;
1177 /* Given a structure or union value DATUM, construct and return
1178 the structure or union component which results from narrowing
1179 that value by the type specified in BASETYPE. For example, given the
1180 hierarchy
1182 class L { int ii; };
1183 class A : L { ... };
1184 class B : L { ... };
1185 class C : A, B { ... };
1187 and the declaration
1189 C x;
1191 then the expression
1193 x.A::ii refers to the ii member of the L part of
1194 of A part of the C object named by X. In this case,
1195 DATUM would be x, and BASETYPE would be A. */
1197 tree
1198 build_scoped_ref (datum, basetype)
1199 tree datum;
1200 tree basetype;
1202 tree ref;
1203 tree type = TREE_TYPE (datum);
1205 if (datum == error_mark_node)
1206 return error_mark_node;
1208 if (TREE_CODE (type) == REFERENCE_TYPE)
1209 type = TREE_TYPE (type);
1211 type = TYPE_MAIN_VARIANT (type);
1213 /* This is an easy conversion. */
1214 if (is_aggr_type (basetype, 1))
1216 tree binfo = TYPE_BINFO (basetype);
1217 if (binfo != TYPE_BINFO (type))
1219 binfo = get_binfo (binfo, type, 1);
1220 if (binfo == error_mark_node)
1221 return error_mark_node;
1222 if (binfo == 0)
1223 return error_not_base_type (basetype, type);
1226 switch (TREE_CODE (datum))
1228 case NOP_EXPR:
1229 case CONVERT_EXPR:
1230 case FLOAT_EXPR:
1231 case FIX_TRUNC_EXPR:
1232 case FIX_FLOOR_EXPR:
1233 case FIX_ROUND_EXPR:
1234 case FIX_CEIL_EXPR:
1235 ref = convert_pointer_to (binfo,
1236 build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0));
1237 break;
1238 default:
1239 ref = convert_pointer_to (binfo,
1240 build_unary_op (ADDR_EXPR, datum, 0));
1242 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1244 return error_mark_node;
1247 /* Build a reference to an object specified by the C++ `->' operator.
1248 Usually this just involves dereferencing the object, but if the
1249 `->' operator is overloaded, then such overloads must be
1250 performed until an object which does not have the `->' operator
1251 overloaded is found. An error is reported when circular pointer
1252 delegation is detected. */
1254 tree
1255 build_x_arrow (datum)
1256 tree datum;
1258 tree types_memoized = NULL_TREE;
1259 register tree rval = datum;
1260 tree type = TREE_TYPE (rval);
1261 tree last_rval;
1263 if (type == error_mark_node)
1264 return error_mark_node;
1266 if (processing_template_decl)
1267 return build_min_nt (ARROW_EXPR, rval);
1269 if (TREE_CODE (rval) == OFFSET_REF)
1271 rval = resolve_offset_ref (datum);
1272 type = TREE_TYPE (rval);
1275 if (TREE_CODE (type) == REFERENCE_TYPE)
1277 rval = convert_from_reference (rval);
1278 type = TREE_TYPE (rval);
1281 if (IS_AGGR_TYPE (type) && TYPE_OVERLOADS_ARROW (complete_type (type)))
1283 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval, NULL_TREE, NULL_TREE)))
1285 if (rval == error_mark_node)
1286 return error_mark_node;
1288 if (value_member (TREE_TYPE (rval), types_memoized))
1290 error ("circular pointer delegation detected");
1291 return error_mark_node;
1293 else
1295 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1296 types_memoized);
1298 last_rval = rval;
1300 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1301 last_rval = convert_from_reference (last_rval);
1303 else
1304 last_rval = default_conversion (rval);
1306 /* Signature pointers are not dereferenced. */
1307 if (TYPE_LANG_SPECIFIC (TREE_TYPE (last_rval))
1308 && IS_SIGNATURE_POINTER (TREE_TYPE (last_rval)))
1309 return last_rval;
1311 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1312 return build_indirect_ref (last_rval, NULL_PTR);
1314 if (types_memoized)
1315 error ("result of `operator->()' yields non-pointer result");
1316 else
1317 error ("base operand of `->' is not a pointer");
1318 return error_mark_node;
1321 /* Make an expression to refer to the COMPONENT field of
1322 structure or union value DATUM. COMPONENT is an arbitrary
1323 expression. DATUM has not already been checked out to be of
1324 aggregate type.
1326 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1327 return an object of member type to a method of the current class,
1328 but there is not yet enough typing information to know which one.
1329 As a special case, if there is only one method by that name,
1330 it is returned. Otherwise we return an expression which other
1331 routines will have to know how to deal with later. */
1333 tree
1334 build_m_component_ref (datum, component)
1335 tree datum, component;
1337 tree type;
1338 tree objtype = TREE_TYPE (datum);
1339 tree rettype;
1340 tree binfo;
1342 if (processing_template_decl)
1343 return build_min_nt (DOTSTAR_EXPR, datum, component);
1345 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1347 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1348 rettype = type;
1350 else
1352 component = build_indirect_ref (component, NULL_PTR);
1353 type = TREE_TYPE (component);
1354 rettype = TREE_TYPE (type);
1357 if (datum == error_mark_node || component == error_mark_node)
1358 return error_mark_node;
1360 if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1362 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1363 return error_mark_node;
1366 if (TREE_CODE (objtype) == REFERENCE_TYPE)
1367 objtype = TREE_TYPE (objtype);
1368 objtype = TYPE_MAIN_VARIANT (objtype);
1370 if (! IS_AGGR_TYPE (objtype))
1372 cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1373 cp_error ("which is of non-aggregate type `%T'", objtype);
1374 return error_mark_node;
1377 binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1378 if (binfo == NULL_TREE)
1380 cp_error ("member type `%T::' incompatible with object type `%T'",
1381 TYPE_METHOD_BASETYPE (type), objtype);
1382 return error_mark_node;
1384 else if (binfo == error_mark_node)
1385 return error_mark_node;
1387 component = build (OFFSET_REF, rettype, datum, component);
1388 if (TREE_CODE (type) == OFFSET_TYPE)
1389 component = resolve_offset_ref (component);
1390 return component;
1393 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1395 tree
1396 build_functional_cast (exp, parms)
1397 tree exp;
1398 tree parms;
1400 /* This is either a call to a constructor,
1401 or a C cast in C++'s `functional' notation. */
1402 tree type;
1404 if (exp == error_mark_node || parms == error_mark_node)
1405 return error_mark_node;
1407 if (TREE_CODE (exp) == IDENTIFIER_NODE)
1409 if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1410 /* Either an enum or an aggregate type. */
1411 type = IDENTIFIER_TYPE_VALUE (exp);
1412 else
1414 type = lookup_name (exp, 1);
1415 if (!type || TREE_CODE (type) != TYPE_DECL)
1417 cp_error ("`%T' fails to be a typedef or built-in type", exp);
1418 return error_mark_node;
1420 type = TREE_TYPE (type);
1423 else if (TREE_CODE (exp) == TYPE_DECL)
1424 type = TREE_TYPE (exp);
1425 else
1426 type = exp;
1428 if (processing_template_decl)
1429 return build_min (CAST_EXPR, type, parms);
1431 if (IS_SIGNATURE (type))
1433 error ("signature type not allowed in cast or constructor expression");
1434 return error_mark_node;
1437 if (! IS_AGGR_TYPE (type))
1439 /* this must build a C cast */
1440 if (parms == NULL_TREE)
1441 parms = integer_zero_node;
1442 else
1444 if (TREE_CHAIN (parms) != NULL_TREE)
1445 pedwarn ("initializer list being treated as compound expression");
1446 parms = build_compound_expr (parms);
1449 return build_c_cast (type, parms);
1452 /* Prepare to evaluate as a call to a constructor. If this expression
1453 is actually used, for example,
1455 return X (arg1, arg2, ...);
1457 then the slot being initialized will be filled in. */
1459 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
1461 cp_error ("type `%T' is not yet defined", type);
1462 return error_mark_node;
1465 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1466 return build_c_cast (type, TREE_VALUE (parms));
1468 exp = build_method_call (NULL_TREE, ctor_identifier, parms,
1469 TYPE_BINFO (type), LOOKUP_NORMAL);
1471 if (exp == error_mark_node)
1472 return error_mark_node;
1474 return build_cplus_new (type, exp);
1477 /* Return the character string for the name that encodes the
1478 enumeral value VALUE in the domain TYPE. */
1480 char *
1481 enum_name_string (value, type)
1482 tree value;
1483 tree type;
1485 register tree values = TYPE_VALUES (type);
1486 register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value);
1488 my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1489 while (values
1490 && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval)
1491 values = TREE_CHAIN (values);
1492 if (values == NULL_TREE)
1494 char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type));
1496 /* Value must have been cast. */
1497 sprintf (buf, "(enum %s)%d",
1498 TYPE_NAME_STRING (type), intval);
1499 return buf;
1501 return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1504 #if 0
1505 /* Print out a language-specific error message for
1506 (Pascal) case or (C) switch statements.
1507 CODE tells what sort of message to print.
1508 TYPE is the type of the switch index expression.
1509 NEW is the new value that we were trying to add.
1510 OLD is the old value that stopped us from adding it. */
1512 void
1513 report_case_error (code, type, new_value, old_value)
1514 int code;
1515 tree type;
1516 tree new_value, old_value;
1518 if (code == 1)
1520 if (new_value)
1521 error ("case label not within a switch statement");
1522 else
1523 error ("default label not within a switch statement");
1525 else if (code == 2)
1527 if (new_value == 0)
1529 error ("multiple default labels in one switch");
1530 return;
1532 if (TREE_CODE (new_value) == RANGE_EXPR)
1533 if (TREE_CODE (old_value) == RANGE_EXPR)
1535 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1536 if (TREE_CODE (type) == ENUMERAL_TYPE)
1537 sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1538 enum_name_string (TREE_OPERAND (new_value, 0), type),
1539 enum_name_string (TREE_OPERAND (new_value, 1), type),
1540 enum_name_string (TREE_OPERAND (old_value, 0), type),
1541 enum_name_string (TREE_OPERAND (old_value, 1), type));
1542 else
1543 sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1544 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1545 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1546 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1547 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1548 error (buf);
1550 else
1552 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1553 if (TREE_CODE (type) == ENUMERAL_TYPE)
1554 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1555 enum_name_string (TREE_OPERAND (new_value, 0), type),
1556 enum_name_string (TREE_OPERAND (new_value, 1), type),
1557 enum_name_string (old_value, type));
1558 else
1559 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1560 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1561 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1562 TREE_INT_CST_LOW (old_value));
1563 error (buf);
1565 else if (TREE_CODE (old_value) == RANGE_EXPR)
1567 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1568 if (TREE_CODE (type) == ENUMERAL_TYPE)
1569 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1570 enum_name_string (TREE_OPERAND (old_value, 0), type),
1571 enum_name_string (TREE_OPERAND (old_value, 1), type),
1572 enum_name_string (new_value, type));
1573 else
1574 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1575 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1576 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1577 TREE_INT_CST_LOW (new_value));
1578 error (buf);
1580 else
1582 if (TREE_CODE (type) == ENUMERAL_TYPE)
1583 error ("duplicate label `%s' in switch statement",
1584 enum_name_string (new_value, type));
1585 else
1586 error ("duplicate label (%d) in switch statement",
1587 TREE_INT_CST_LOW (new_value));
1590 else if (code == 3)
1592 if (TREE_CODE (type) == ENUMERAL_TYPE)
1593 warning ("case value out of range for enum %s",
1594 TYPE_NAME_STRING (type));
1595 else
1596 warning ("case value out of range");
1598 else if (code == 4)
1600 if (TREE_CODE (type) == ENUMERAL_TYPE)
1601 error ("range values `%s' and `%s' reversed",
1602 enum_name_string (new_value, type),
1603 enum_name_string (old_value, type));
1604 else
1605 error ("range values reversed");
1608 #endif
1610 void
1611 check_for_new_type (string, inptree)
1612 char *string;
1613 flagged_type_tree inptree;
1615 if (pedantic && inptree.new_type_flag)
1616 pedwarn ("ANSI C++ forbids defining types within %s",string);