Add configuration for semi-hosted ARM.
[official-gcc.git] / gcc / cp / typeck2.c
blobe69ada49009a5d4b3c5c2dee741e9c932f4a7bca
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include <stdio.h>
34 #include "tree.h"
35 #include "cp-tree.h"
36 #include "flags.h"
38 static tree process_init_constructor ();
39 extern void pedwarn (), error ();
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. */
46 tree
47 error_not_base_type (basetype, type)
48 tree basetype, type;
50 if (TREE_CODE (basetype) == FUNCTION_DECL)
51 basetype = DECL_CLASS_CONTEXT (basetype);
52 cp_error ("type `%T' is not a base type for type `%T'", basetype, type);
53 return error_mark_node;
56 tree
57 binfo_or_else (parent_or_type, type)
58 tree parent_or_type, type;
60 tree binfo;
61 if (TYPE_MAIN_VARIANT (parent_or_type) == TYPE_MAIN_VARIANT (type))
62 return TYPE_BINFO (parent_or_type);
63 if ((binfo = get_binfo (parent_or_type, TYPE_MAIN_VARIANT (type), 0)))
65 if (binfo == error_mark_node)
66 return NULL_TREE;
67 return binfo;
69 error_not_base_type (parent_or_type, type);
70 return NULL_TREE;
73 /* Print an error message stemming from an invalid use of an
74 aggregate type.
76 TYPE is the type or binfo which draws the error.
77 MSG is the message to print.
78 ARG is an optional argument which may provide more information. */
79 void
80 error_with_aggr_type (type, msg, arg)
81 tree type;
82 char *msg;
83 HOST_WIDE_INT arg;
85 tree name;
87 if (TREE_CODE (type) == TREE_VEC)
88 type = BINFO_TYPE (type);
90 name = TYPE_NAME (type);
91 if (TREE_CODE (name) == TYPE_DECL)
92 name = DECL_NAME (name);
93 error (msg, IDENTIFIER_POINTER (name), arg);
96 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
97 value may not be changed thereafter. Thus, we emit hard errors for these,
98 rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For
99 example, conversions to references.) */
100 void
101 readonly_error (arg, string, soft)
102 tree arg;
103 char *string;
104 int soft;
106 char *fmt;
107 void (*fn)();
109 if (soft)
110 fn = pedwarn;
111 else
112 fn = error;
114 if (TREE_CODE (arg) == COMPONENT_REF)
116 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
117 fmt = "%s of member `%s' in read-only structure";
118 else
119 fmt = "%s of read-only member `%s'";
120 (*fn) (fmt, string, lang_printable_name (TREE_OPERAND (arg, 1)));
122 else if (TREE_CODE (arg) == VAR_DECL)
124 if (DECL_LANG_SPECIFIC (arg)
125 && DECL_IN_AGGR_P (arg)
126 && !TREE_STATIC (arg))
127 fmt = "%s of constant field `%s'";
128 else
129 fmt = "%s of read-only variable `%s'";
130 (*fn) (fmt, string, lang_printable_name (arg));
132 else if (TREE_CODE (arg) == PARM_DECL)
133 (*fn) ("%s of read-only parameter `%s'", string,
134 lang_printable_name (arg));
135 else if (TREE_CODE (arg) == INDIRECT_REF
136 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
137 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
138 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
139 (*fn) ("%s of read-only reference `%s'",
140 string, lang_printable_name (TREE_OPERAND (arg, 0)));
141 else if (TREE_CODE (arg) == RESULT_DECL)
142 (*fn) ("%s of read-only named return value `%s'",
143 string, lang_printable_name (arg));
144 else
145 (*fn) ("%s of read-only location", string);
148 /* Print an error message for invalid use of a type which declares
149 virtual functions which are not inheritable. */
150 void
151 abstract_virtuals_error (decl, type)
152 tree decl;
153 tree type;
155 tree u = CLASSTYPE_ABSTRACT_VIRTUALS (type);
157 if (decl)
159 if (TREE_CODE (decl) == RESULT_DECL)
160 return;
162 if (TREE_CODE (decl) == VAR_DECL)
163 cp_error ("cannot declare variable `%D' to be of type `%T'",
164 decl, type);
165 else if (TREE_CODE (decl) == PARM_DECL)
166 cp_error ("cannot declare parameter `%D' to be of type `%T'",
167 decl, type);
168 else if (TREE_CODE (decl) == FIELD_DECL)
169 cp_error ("cannot declare field `%D' to be of type `%T'",
170 decl, type);
171 else if (TREE_CODE (decl) == FUNCTION_DECL
172 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
173 cp_error ("invalid return type for method `%#D'", decl);
174 else if (TREE_CODE (decl) == FUNCTION_DECL)
175 cp_error ("invalid return type for function `%#D'", decl);
177 else cp_error ("cannot allocate an object of type `%T'", type);
178 /* Only go through this once. */
179 if (TREE_PURPOSE (u) == NULL_TREE)
181 error (" since the following virtual functions are abstract:");
182 TREE_PURPOSE (u) = error_mark_node;
183 while (u)
185 cp_error ("\t%#D", TREE_VALUE (u));
186 u = TREE_CHAIN (u);
189 else cp_error (" since type `%T' has abstract virtual functions", type);
192 /* Print an error message for invalid use of a signature type.
193 Signatures are treated similar to abstract classes here, they
194 cannot be instantiated. */
195 void
196 signature_error (decl, type)
197 tree decl;
198 tree type;
200 if (decl)
202 if (TREE_CODE (decl) == RESULT_DECL)
203 return;
205 if (TREE_CODE (decl) == VAR_DECL)
206 cp_error ("cannot declare variable `%D' to be of signature type `%T'",
207 decl, type);
208 else if (TREE_CODE (decl) == PARM_DECL)
209 cp_error ("cannot declare parameter `%D' to be of signature type `%T'",
210 decl, type);
211 else if (TREE_CODE (decl) == FIELD_DECL)
212 cp_error ("cannot declare field `%D' to be of signature type `%T'",
213 decl, type);
214 else if (TREE_CODE (decl) == FUNCTION_DECL
215 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
216 cp_error ("invalid return type for method `%#D'", decl);
217 else if (TREE_CODE (decl) == FUNCTION_DECL)
218 cp_error ("invalid return type for function `%#D'", decl);
220 else
221 cp_error ("cannot allocate an object of signature type `%T'", type);
224 /* Print an error message for invalid use of an incomplete type.
225 VALUE is the expression that was used (or 0 if that isn't known)
226 and TYPE is the type that was invalid. */
228 void
229 incomplete_type_error (value, type)
230 tree value;
231 tree type;
233 char *errmsg;
235 /* Avoid duplicate error message. */
236 if (TREE_CODE (type) == ERROR_MARK)
237 return;
239 if (value != 0 && (TREE_CODE (value) == VAR_DECL
240 || TREE_CODE (value) == PARM_DECL))
241 error ("`%s' has an incomplete type",
242 IDENTIFIER_POINTER (DECL_NAME (value)));
243 else
245 retry:
246 /* We must print an error message. Be clever about what it says. */
248 switch (TREE_CODE (type))
250 case RECORD_TYPE:
251 errmsg = "invalid use of undefined type `struct %s'";
252 break;
254 case UNION_TYPE:
255 errmsg = "invalid use of undefined type `union %s'";
256 break;
258 case ENUMERAL_TYPE:
259 errmsg = "invalid use of undefined type `enum %s'";
260 break;
262 case VOID_TYPE:
263 error ("invalid use of void expression");
264 return;
266 case ARRAY_TYPE:
267 if (TYPE_DOMAIN (type))
269 type = TREE_TYPE (type);
270 goto retry;
272 error ("invalid use of array with unspecified bounds");
273 return;
275 case OFFSET_TYPE:
276 error ("invalid use of member type (did you forget the `&' ?)");
277 return;
279 default:
280 my_friendly_abort (108);
283 error_with_aggr_type (type, errmsg);
287 /* Like error(), but don't call report_error_function(). */
288 static void
289 ack (s, v, v2)
290 char *s;
291 HOST_WIDE_INT v;
292 HOST_WIDE_INT v2;
294 extern char * progname;
296 if (input_filename)
297 fprintf (stderr, "%s:%d: ", input_filename, lineno);
298 else
299 fprintf (stderr, "%s: ", progname);
301 fprintf (stderr, s, v, v2);
302 fprintf (stderr, "\n");
305 /* There are times when the compiler can get very confused, confused
306 to the point of giving up by aborting, simply because of previous
307 input errors. It is much better to have the user go back and
308 correct those errors first, and see if it makes us happier, than it
309 is to abort on him. This is because when one has a 10,000 line
310 program, and the compiler comes back with ``core dump'', the user
311 is left not knowing even where to begin to fix things and no place
312 to even try and work around things.
314 The parameter is to uniquely identify the problem to the user, so
315 that they can say, I am having problem 59, and know that fix 7 will
316 probably solve their problem. Or, we can document what problem
317 59 is, so they can understand how to work around it, should they
318 ever run into it.
320 Note, there will be no more calls in the C++ front end to abort,
321 because the C++ front end is so unreliable still. The C front end
322 can get away with calling abort, because for most of the calls to
323 abort on most machines, it, I suspect, can be proven that it is
324 impossible to ever call abort. The same is not yet true for C++,
325 one day, maybe it will be.
327 We used to tell people to "fix the above error[s] and try recompiling
328 the program" via a call to fatal, but that message tended to look
329 silly. So instead, we just do the equivalent of a call to fatal in the
330 same situation (call exit). */
332 /* First used: 0 (reserved), Last used: 366. Free: */
334 static int abortcount = 0;
336 void
337 my_friendly_abort (i)
338 int i;
340 /* if the previous error came through here, i.e. report_error_function
341 ended up calling us again, don't just exit; we want a diagnostic of
342 some kind. */
343 if (abortcount == 1)
344 current_function_decl = NULL_TREE;
345 else if (errorcount > 0 || sorrycount > 0)
347 if (abortcount > 1)
349 if (i == 0)
350 ack ("Internal compiler error.");
351 else
352 ack ("Internal compiler error %d.", i);
353 ack ("Please submit a full bug report to `bug-g++@prep.ai.mit.edu'.");
355 else
356 error ("confused by earlier errors, bailing out");
358 exit (34);
360 ++abortcount;
362 if (i == 0)
363 error ("Internal compiler error.");
364 else
365 error ("Internal compiler error %d.", i);
367 fatal ("Please submit a full bug report to `bug-g++@prep.ai.mit.edu'.");
370 void
371 my_friendly_assert (cond, where)
372 int cond, where;
374 if (cond == 0)
375 my_friendly_abort (where);
378 /* Return nonzero if VALUE is a valid constant-valued expression
379 for use in initializing a static variable; one that can be an
380 element of a "constant" initializer.
382 Return null_pointer_node if the value is absolute;
383 if it is relocatable, return the variable that determines the relocation.
384 We assume that VALUE has been folded as much as possible;
385 therefore, we do not need to check for such things as
386 arithmetic-combinations of integers. */
388 tree
389 initializer_constant_valid_p (value, endtype)
390 tree value;
391 tree endtype;
393 switch (TREE_CODE (value))
395 case CONSTRUCTOR:
396 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
397 && TREE_CONSTANT (value))
398 return
399 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
400 endtype);
402 return TREE_STATIC (value) ? null_pointer_node : 0;
404 case INTEGER_CST:
405 case REAL_CST:
406 case STRING_CST:
407 case COMPLEX_CST:
408 return null_pointer_node;
410 case ADDR_EXPR:
411 return TREE_OPERAND (value, 0);
413 case NON_LVALUE_EXPR:
414 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
416 case CONVERT_EXPR:
417 case NOP_EXPR:
418 /* Allow conversions between pointer types. */
419 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
420 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
421 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
423 /* Allow conversions between real types. */
424 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
425 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
426 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
428 /* Allow length-preserving conversions between integer types. */
429 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
430 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
431 && (TYPE_PRECISION (TREE_TYPE (value))
432 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
433 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
435 /* Allow conversions between other integer types only if
436 explicit value. */
437 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
438 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
440 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
441 endtype);
442 if (inner == null_pointer_node)
443 return null_pointer_node;
444 return 0;
447 /* Allow (int) &foo provided int is as wide as a pointer. */
448 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
449 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
450 && (TYPE_PRECISION (TREE_TYPE (value))
451 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
452 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
453 endtype);
455 /* Likewise conversions from int to pointers. */
456 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
457 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
458 && (TYPE_PRECISION (TREE_TYPE (value))
459 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
460 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
461 endtype);
463 /* Allow conversions to union types if the value inside is okay. */
464 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
465 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
466 endtype);
467 return 0;
469 case PLUS_EXPR:
470 if (TREE_CODE (endtype) == INTEGER_TYPE
471 && TYPE_PRECISION (endtype) < POINTER_SIZE)
472 return 0;
474 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
475 endtype);
476 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
477 endtype);
478 /* If either term is absolute, use the other terms relocation. */
479 if (valid0 == null_pointer_node)
480 return valid1;
481 if (valid1 == null_pointer_node)
482 return valid0;
483 return 0;
486 case MINUS_EXPR:
487 if (TREE_CODE (endtype) == INTEGER_TYPE
488 && TYPE_PRECISION (endtype) < POINTER_SIZE)
489 return 0;
491 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
492 endtype);
493 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
494 endtype);
495 /* Win if second argument is absolute. */
496 if (valid1 == null_pointer_node)
497 return valid0;
498 /* Win if both arguments have the same relocation.
499 Then the value is absolute. */
500 if (valid0 == valid1)
501 return null_pointer_node;
502 return 0;
506 return 0;
509 /* Perform appropriate conversions on the initial value of a variable,
510 store it in the declaration DECL,
511 and print any error messages that are appropriate.
512 If the init is invalid, store an ERROR_MARK.
514 C++: Note that INIT might be a TREE_LIST, which would mean that it is
515 a base class initializer for some aggregate type, hopefully compatible
516 with DECL. If INIT is a single element, and DECL is an aggregate
517 type, we silently convert INIT into a TREE_LIST, allowing a constructor
518 to be called.
520 If INIT is a TREE_LIST and there is no constructor, turn INIT
521 into a CONSTRUCTOR and use standard initialization techniques.
522 Perhaps a warning should be generated?
524 Returns value of initializer if initialization could not be
525 performed for static variable. In that case, caller must do
526 the storing. */
528 tree
529 store_init_value (decl, init)
530 tree decl, init;
532 register tree value, type;
534 /* If variable's type was invalidly declared, just ignore it. */
536 type = TREE_TYPE (decl);
537 if (TREE_CODE (type) == ERROR_MARK)
538 return NULL_TREE;
540 /* Take care of C++ business up here. */
541 type = TYPE_MAIN_VARIANT (type);
543 if (IS_AGGR_TYPE (type))
545 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
546 && TREE_CODE (init) != CONSTRUCTOR)
547 my_friendly_abort (109);
549 /* Although we are not allowed to declare variables of signature
550 type, we complain about a possible constructor call in such a
551 declaration as well. */
552 if (TREE_CODE (init) == TREE_LIST
553 && IS_SIGNATURE (type))
555 cp_error ("constructor syntax cannot be used with signature type `%T'",
556 type);
557 init = error_mark_node;
559 else if (TREE_CODE (init) == TREE_LIST)
561 cp_error ("constructor syntax used, but no constructor declared for type `%T'", type);
562 init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
564 #if 0
565 if (TREE_CODE (init) == CONSTRUCTOR)
567 tree field;
568 tree funcs;
569 int func;
571 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */
572 if (CLASSTYPE_N_BASECLASSES (type))
573 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl);
574 if (CLASSTYPE_VTBL_PTR (type))
575 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl);
576 if (TYPE_NEEDS_CONSTRUCTING (type))
578 cp_error_at ("initializer list construction invalid for `%D'", decl);
579 error ("due to the presence of a constructor");
581 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
582 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
584 cp_error_at ("initializer list construction invalid for `%D'", decl);
585 cp_error_at ("due to non-public access of member `%D'", field);
587 funcs = TYPE_METHODS (type);
588 if (funcs)
589 for (func = 0; func < TREE_VEC_LENGTH (funcs); func++)
591 field = TREE_VEC_ELT (funcs, func);
592 if (field && (TREE_PRIVATE (field) || TREE_PROTECTED (field)))
594 cp_error_at ("initializer list construction invalid for `%D'", decl);
595 cp_error_at ("due to non-public access of member `%D'", field);
599 #endif
601 else if (TREE_CODE (init) == TREE_LIST
602 && TREE_TYPE (init) != unknown_type_node)
604 if (TREE_CODE (decl) == RESULT_DECL)
606 if (TREE_CHAIN (init))
608 warning ("comma expression used to initialize return value");
609 init = build_compound_expr (init);
611 else
612 init = TREE_VALUE (init);
614 else if (TREE_TYPE (init) != 0
615 && TREE_CODE (TREE_TYPE (init)) == OFFSET_TYPE)
617 /* Use the type of our variable to instantiate
618 the type of our initializer. */
619 init = instantiate_type (type, init, 1);
621 else if (TREE_CODE (init) == TREE_LIST
622 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
624 error ("cannot initialize arrays using this syntax");
625 return NULL_TREE;
627 else
629 /* We get here with code like `int a (2);' */
631 if (TREE_CHAIN (init) != NULL_TREE)
633 pedwarn ("initializer list being treated as compound expression");
634 init = build_compound_expr (init);
636 else
637 init = TREE_VALUE (init);
641 /* End of special C++ code. */
643 /* Digest the specified initializer into an expression. */
645 value = digest_init (type, init, (tree *) 0);
647 /* Store the expression if valid; else report error. */
649 if (TREE_CODE (value) == ERROR_MARK)
651 else if (TREE_STATIC (decl)
652 && (! TREE_CONSTANT (value)
653 || ! initializer_constant_valid_p (value, TREE_TYPE (value))
654 #if 0
655 /* A STATIC PUBLIC int variable doesn't have to be
656 run time inited when doing pic. (mrs) */
657 /* Since ctors and dtors are the only things that can
658 reference vtables, and they are always written down
659 the the vtable definition, we can leave the
660 vtables in initialized data space.
661 However, other initialized data cannot be initialized
662 this way. Instead a global file-level initializer
663 must do the job. */
664 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
665 #endif
668 return value;
669 #if 0 /* No, that's C. jason 9/19/94 */
670 else
672 if (pedantic && TREE_CODE (value) == CONSTRUCTOR
673 /* Don't complain about non-constant initializers of
674 signature tables and signature pointers/references. */
675 && ! (TYPE_LANG_SPECIFIC (type)
676 && (IS_SIGNATURE (type)
677 || IS_SIGNATURE_POINTER (type)
678 || IS_SIGNATURE_REFERENCE (type))))
680 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
681 pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
684 #endif
685 DECL_INITIAL (decl) = value;
686 return NULL_TREE;
689 /* Digest the parser output INIT as an initializer for type TYPE.
690 Return a C expression of type TYPE to represent the initial value.
692 If TAIL is nonzero, it points to a variable holding a list of elements
693 of which INIT is the first. We update the list stored there by
694 removing from the head all the elements that we use.
695 Normally this is only one; we use more than one element only if
696 TYPE is an aggregate and INIT is not a constructor. */
698 tree
699 digest_init (type, init, tail)
700 tree type, init, *tail;
702 enum tree_code code = TREE_CODE (type);
703 tree element = NULL_TREE;
704 tree old_tail_contents;
705 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
706 tree node which has no TREE_TYPE. */
707 int raw_constructor;
709 /* By default, assume we use one element from a list.
710 We correct this later in the sole case where it is not true. */
712 if (tail)
714 old_tail_contents = *tail;
715 *tail = TREE_CHAIN (*tail);
718 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
719 && TREE_VALUE (init) == error_mark_node))
720 return error_mark_node;
722 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
723 if (TREE_CODE (init) == NON_LVALUE_EXPR)
724 init = TREE_OPERAND (init, 0);
726 if (init && TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (type))
727 init = default_conversion (init);
729 if (init && TYPE_PTRMEMFUNC_P (type)
730 && ((TREE_CODE (init) == ADDR_EXPR
731 && ((TREE_CODE (TREE_TYPE (init)) == POINTER_TYPE
732 && TREE_CODE (TREE_TYPE (TREE_TYPE (init))) == METHOD_TYPE)
733 || TREE_CODE (TREE_OPERAND (init, 0)) == TREE_LIST))
734 || TREE_CODE (init) == TREE_LIST
735 || integer_zerop (init)
736 || (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))))
738 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), init, 0);
741 raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
743 if (init && raw_constructor
744 && CONSTRUCTOR_ELTS (init) != 0
745 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
747 element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
748 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
749 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
750 element = TREE_OPERAND (element, 0);
751 if (element == error_mark_node)
752 return element;
755 /* Any type can be initialized from an expression of the same type,
756 optionally with braces. */
758 if (init && TREE_TYPE (init)
759 && (TYPE_MAIN_VARIANT (TREE_TYPE (init)) == type
760 || (code == ARRAY_TYPE && comptypes (TREE_TYPE (init), type, 1))))
762 if (pedantic && code == ARRAY_TYPE
763 && TREE_CODE (init) != STRING_CST)
764 pedwarn ("ANSI C++ forbids initializing array from array expression");
765 if (TREE_CODE (init) == CONST_DECL)
766 init = DECL_INITIAL (init);
767 else if (TREE_READONLY_DECL_P (init))
768 init = decl_constant_value (init);
769 return init;
772 if (element && (TREE_TYPE (element) == type
773 || (code == ARRAY_TYPE && TREE_TYPE (element)
774 && comptypes (TREE_TYPE (element), type, 1))))
776 if (pedantic && code == ARRAY_TYPE)
777 pedwarn ("ANSI C++ forbids initializing array from array expression");
778 if (pedantic && (code == RECORD_TYPE || code == UNION_TYPE))
779 pedwarn ("ANSI C++ forbids single nonscalar initializer with braces");
780 if (TREE_CODE (element) == CONST_DECL)
781 element = DECL_INITIAL (element);
782 else if (TREE_READONLY_DECL_P (element))
783 element = decl_constant_value (element);
784 return element;
787 /* Initialization of an array of chars from a string constant
788 optionally enclosed in braces. */
790 if (code == ARRAY_TYPE)
792 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
793 if ((typ1 == char_type_node
794 || typ1 == signed_char_type_node
795 || typ1 == unsigned_char_type_node
796 || typ1 == unsigned_wchar_type_node
797 || typ1 == signed_wchar_type_node)
798 && ((init && TREE_CODE (init) == STRING_CST)
799 || (element && TREE_CODE (element) == STRING_CST)))
801 tree string = element ? element : init;
803 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
804 != char_type_node)
805 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
807 error ("char-array initialized from wide string");
808 return error_mark_node;
810 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
811 == char_type_node)
812 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
814 error ("int-array initialized from non-wide string");
815 return error_mark_node;
818 if (pedantic
819 && typ1 != char_type_node
820 && typ1 != signed_char_type_node
821 && typ1 != unsigned_char_type_node)
822 pedwarn ("ANSI C++ forbids string initializer except for `char' elements");
823 TREE_TYPE (string) = type;
824 if (TYPE_DOMAIN (type) != 0
825 && TREE_CONSTANT (TYPE_SIZE (type)))
827 register int size
828 = TREE_INT_CST_LOW (TYPE_SIZE (type));
829 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
830 /* In C it is ok to subtract 1 from the length of the string
831 because it's ok to ignore the terminating null char that is
832 counted in the length of the constant, but in C++ this would
833 be invalid. */
834 if (size < TREE_STRING_LENGTH (string))
835 pedwarn ("initializer-string for array of chars is too long");
837 return string;
841 /* Handle scalar types, including conversions,
842 and signature pointers and references. */
844 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
845 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
846 || code == BOOLEAN_TYPE
847 || (code == RECORD_TYPE && ! raw_constructor
848 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))))
850 if (raw_constructor)
852 if (element == 0)
854 error ("initializer for scalar variable requires one element");
855 return error_mark_node;
857 init = element;
859 while (TREE_CODE (init) == CONSTRUCTOR)
861 cp_pedwarn ("braces around scalar initializer for `%T'", type);
862 init = CONSTRUCTOR_ELTS (init);
863 if (TREE_CHAIN (init))
864 cp_pedwarn ("ignoring extra initializers for `%T'", type);
865 init = TREE_VALUE (init);
868 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
869 "initialization", NULL_TREE, 0);
872 /* Come here only for records and arrays (and unions with constructors). */
874 if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
876 cp_error ("variable-sized object of type `%T' may not be initialized",
877 type);
878 return error_mark_node;
881 if (code == ARRAY_TYPE || code == RECORD_TYPE || code == UNION_TYPE)
883 if (raw_constructor)
884 return process_init_constructor (type, init, (tree *)0);
885 else if (TYPE_NEEDS_CONSTRUCTING (type))
887 /* This can only be reached when caller is initializing
888 ARRAY_TYPE. In that case, we don't want to convert
889 INIT to TYPE. We will let `expand_vec_init' do it. */
890 return init;
892 else if (tail != 0)
894 *tail = old_tail_contents;
895 return process_init_constructor (type, 0, tail);
897 else if (flag_traditional)
898 /* Traditionally one can say `char x[100] = 0;'. */
899 return process_init_constructor (type,
900 build_nt (CONSTRUCTOR, 0,
901 tree_cons (0, init, 0)),
903 if (code != ARRAY_TYPE)
904 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
905 "initialization", NULL_TREE, 0);
908 error ("invalid initializer");
909 return error_mark_node;
912 /* Process a constructor for a variable of type TYPE.
913 The constructor elements may be specified either with INIT or with ELTS,
914 only one of which should be non-null.
916 If INIT is specified, it is a CONSTRUCTOR node which is specifically
917 and solely for initializing this datum.
919 If ELTS is specified, it is the address of a variable containing
920 a list of expressions. We take as many elements as we need
921 from the head of the list and update the list.
923 In the resulting constructor, TREE_CONSTANT is set if all elts are
924 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
925 constants that the assembler and linker can compute them. */
927 static tree
928 process_init_constructor (type, init, elts)
929 tree type, init, *elts;
931 register tree tail;
932 /* List of the elements of the result constructor,
933 in reverse order. */
934 register tree members = NULL;
935 tree result;
936 int allconstant = 1;
937 int allsimple = 1;
938 int erroneous = 0;
940 /* Make TAIL be the list of elements to use for the initialization,
941 no matter how the data was given to us. */
943 if (elts)
945 if (warn_missing_braces)
946 warning ("aggregate has a partly bracketed initializer");
947 tail = *elts;
949 else
950 tail = CONSTRUCTOR_ELTS (init);
952 /* Gobble as many elements as needed, and make a constructor or initial value
953 for each element of this aggregate. Chain them together in result.
954 If there are too few, use 0 for each scalar ultimate component. */
956 if (TREE_CODE (type) == ARRAY_TYPE)
958 tree domain = TYPE_DOMAIN (type);
959 register long len;
960 register int i;
962 if (domain)
963 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
964 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
965 + 1);
966 else
967 len = -1; /* Take as many as there are */
969 for (i = 0; (len < 0 || i < len) && tail != 0; i++)
971 register tree next1;
973 if (TREE_VALUE (tail) != 0)
975 tree tail1 = tail;
976 next1 = digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
977 TREE_VALUE (tail), &tail1);
978 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type))
979 && TYPE_MAIN_VARIANT (TREE_TYPE (type)) != TYPE_MAIN_VARIANT (TREE_TYPE (next1)))
981 /* The fact this needs to be done suggests this code needs
982 to be totally rewritten. */
983 next1 = convert_for_initialization (NULL_TREE, TREE_TYPE (type), next1, LOOKUP_NORMAL, "initialization", NULL_TREE, 0);
985 my_friendly_assert (tail1 == 0
986 || TREE_CODE (tail1) == TREE_LIST, 319);
987 if (tail == tail1 && len < 0)
989 error ("non-empty initializer for array of empty elements");
990 /* Just ignore what we were supposed to use. */
991 tail1 = NULL_TREE;
993 tail = tail1;
995 else
997 next1 = error_mark_node;
998 tail = TREE_CHAIN (tail);
1001 if (next1 == error_mark_node)
1002 erroneous = 1;
1003 else if (!TREE_CONSTANT (next1))
1004 allconstant = 0;
1005 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1006 allsimple = 0;
1007 members = tree_cons (NULL_TREE, next1, members);
1010 if (TREE_CODE (type) == RECORD_TYPE)
1012 register tree field;
1014 if (tail)
1016 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1018 sorry ("initializer list for object of class with virtual baseclasses");
1019 return error_mark_node;
1022 if (TYPE_BINFO_BASETYPES (type))
1024 sorry ("initializer list for object of class with baseclasses");
1025 return error_mark_node;
1028 if (TYPE_VIRTUAL_P (type))
1030 sorry ("initializer list for object using virtual functions");
1031 return error_mark_node;
1035 for (field = TYPE_FIELDS (type); field && tail;
1036 field = TREE_CHAIN (field))
1038 register tree next1;
1040 if (! DECL_NAME (field))
1042 members = tree_cons (field, integer_zero_node, members);
1043 continue;
1046 if (TREE_CODE (field) != FIELD_DECL)
1047 continue;
1049 if (TREE_VALUE (tail) != 0)
1051 tree tail1 = tail;
1053 next1 = digest_init (TREE_TYPE (field),
1054 TREE_VALUE (tail), &tail1);
1055 my_friendly_assert (tail1 == 0
1056 || TREE_CODE (tail1) == TREE_LIST, 320);
1057 tail = tail1;
1059 else
1061 next1 = error_mark_node;
1062 tail = TREE_CHAIN (tail);
1065 if (next1 == error_mark_node)
1066 erroneous = 1;
1067 else if (!TREE_CONSTANT (next1))
1068 allconstant = 0;
1069 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1070 allsimple = 0;
1071 members = tree_cons (field, next1, members);
1073 for (; field; field = TREE_CHAIN (field))
1075 if (TREE_CODE (field) != FIELD_DECL)
1076 continue;
1078 /* Does this field have a default initialization? */
1079 if (DECL_INITIAL (field))
1081 register tree next1 = DECL_INITIAL (field);
1082 if (TREE_CODE (next1) == ERROR_MARK)
1083 erroneous = 1;
1084 else if (!TREE_CONSTANT (next1))
1085 allconstant = 0;
1086 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1087 allsimple = 0;
1088 members = tree_cons (field, next1, members);
1090 else if (TREE_READONLY (field))
1091 error ("uninitialized const member `%s'",
1092 IDENTIFIER_POINTER (DECL_NAME (field)));
1093 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
1094 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1095 error ("member `%s' with uninitialized const fields",
1096 IDENTIFIER_POINTER (DECL_NAME (field)));
1097 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1098 error ("member `%s' is uninitialized reference",
1099 IDENTIFIER_POINTER (DECL_NAME (field)));
1103 if (TREE_CODE (type) == UNION_TYPE)
1105 register tree field = TYPE_FIELDS (type);
1106 register tree next1;
1108 /* Find the first named field. ANSI decided in September 1990
1109 that only named fields count here. */
1110 while (field && DECL_NAME (field) == 0)
1111 field = TREE_CHAIN (field);
1113 /* If this element specifies a field, initialize via that field. */
1114 if (TREE_PURPOSE (tail) != NULL_TREE)
1116 int win = 0;
1118 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
1119 /* Handle the case of a call by build_c_cast. */
1120 field = TREE_PURPOSE (tail), win = 1;
1121 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
1122 error ("index value instead of field name in union initializer");
1123 else
1125 tree temp;
1126 for (temp = TYPE_FIELDS (type);
1127 temp;
1128 temp = TREE_CHAIN (temp))
1129 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
1130 break;
1131 if (temp)
1132 field = temp, win = 1;
1133 else
1134 error ("no field `%s' in union being initialized",
1135 IDENTIFIER_POINTER (TREE_PURPOSE (tail)));
1137 if (!win)
1138 TREE_VALUE (tail) = error_mark_node;
1140 else if (field == 0)
1142 cp_error ("union `%T' with no named members cannot be initialized",
1143 type);
1144 TREE_VALUE (tail) = error_mark_node;
1147 if (TREE_VALUE (tail) != 0)
1149 tree tail1 = tail;
1151 next1 = digest_init (TREE_TYPE (field),
1152 TREE_VALUE (tail), &tail1);
1153 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
1154 my_friendly_abort (357);
1155 tail = tail1;
1157 else
1159 next1 = error_mark_node;
1160 tail = TREE_CHAIN (tail);
1163 if (next1 == error_mark_node)
1164 erroneous = 1;
1165 else if (!TREE_CONSTANT (next1))
1166 allconstant = 0;
1167 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
1168 allsimple = 0;
1169 members = tree_cons (field, next1, members);
1172 /* If arguments were specified as a list, just remove the ones we used. */
1173 if (elts)
1174 *elts = tail;
1175 /* If arguments were specified as a constructor,
1176 complain unless we used all the elements of the constructor. */
1177 else if (tail)
1178 pedwarn ("excess elements in aggregate initializer");
1180 if (erroneous)
1181 return error_mark_node;
1183 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1184 if (init)
1185 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1186 if (allconstant) TREE_CONSTANT (result) = 1;
1187 if (allconstant && allsimple) TREE_STATIC (result) = 1;
1188 return result;
1191 /* Given a structure or union value DATUM, construct and return
1192 the structure or union component which results from narrowing
1193 that value by the types specified in TYPES. For example, given the
1194 hierarchy
1196 class L { int ii; };
1197 class A : L { ... };
1198 class B : L { ... };
1199 class C : A, B { ... };
1201 and the declaration
1203 C x;
1205 then the expression
1207 x::C::A::L::ii refers to the ii member of the L part of
1208 of A part of the C object named by X. In this case,
1209 DATUM would be x, and TYPES would be a SCOPE_REF consisting of
1211 SCOPE_REF
1212 SCOPE_REF
1216 The last entry in the SCOPE_REF is always an IDENTIFIER_NODE.
1220 tree
1221 build_scoped_ref (datum, types)
1222 tree datum;
1223 tree types;
1225 tree ref;
1226 tree type = TREE_TYPE (datum);
1228 if (datum == error_mark_node)
1229 return error_mark_node;
1231 if (TREE_CODE (type) == REFERENCE_TYPE)
1232 type = TREE_TYPE (type);
1234 type = TYPE_MAIN_VARIANT (type);
1236 if (TREE_CODE (types) == SCOPE_REF)
1238 /* We have some work to do. */
1239 struct type_chain
1240 { tree type; struct type_chain *next; }
1241 *chain = NULL, *head = NULL, scratch;
1242 ref = build_unary_op (ADDR_EXPR, datum, 0);
1243 while (TREE_CODE (types) == SCOPE_REF)
1245 tree t = TREE_OPERAND (types, 1);
1246 if (is_aggr_typedef (t, 1))
1248 head = (struct type_chain *)alloca (sizeof (struct type_chain));
1249 head->type = IDENTIFIER_TYPE_VALUE (t);
1250 head->next = chain;
1251 chain = head;
1252 types = TREE_OPERAND (types, 0);
1254 else return error_mark_node;
1256 if (! is_aggr_typedef (types, 1))
1257 return error_mark_node;
1259 head = &scratch;
1260 head->type = IDENTIFIER_TYPE_VALUE (types);
1261 head->next = chain;
1262 chain = head;
1263 while (chain)
1265 tree binfo = chain->type;
1266 type = TREE_TYPE (TREE_TYPE (ref));
1267 if (binfo != TYPE_BINFO (type))
1269 binfo = get_binfo (binfo, type, 1);
1270 if (binfo == error_mark_node)
1271 return error_mark_node;
1272 if (binfo == 0)
1273 return error_not_base_type (chain->type, type);
1274 ref = convert_pointer_to (binfo, ref);
1276 chain = chain->next;
1278 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1281 /* This is an easy conversion. */
1282 if (is_aggr_typedef (types, 1))
1284 tree binfo = TYPE_BINFO (IDENTIFIER_TYPE_VALUE (types));
1285 if (binfo != TYPE_BINFO (type))
1287 binfo = get_binfo (binfo, type, 1);
1288 if (binfo == error_mark_node)
1289 return error_mark_node;
1290 if (binfo == 0)
1291 return error_not_base_type (IDENTIFIER_TYPE_VALUE (types), type);
1294 switch (TREE_CODE (datum))
1296 case NOP_EXPR:
1297 case CONVERT_EXPR:
1298 case FLOAT_EXPR:
1299 case FIX_TRUNC_EXPR:
1300 case FIX_FLOOR_EXPR:
1301 case FIX_ROUND_EXPR:
1302 case FIX_CEIL_EXPR:
1303 ref = convert_pointer_to (binfo,
1304 build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0));
1305 break;
1306 default:
1307 ref = convert_pointer_to (binfo,
1308 build_unary_op (ADDR_EXPR, datum, 0));
1310 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1312 return error_mark_node;
1315 /* Build a reference to an object specified by the C++ `->' operator.
1316 Usually this just involves dereferencing the object, but if the
1317 `->' operator is overloaded, then such overloads must be
1318 performed until an object which does not have the `->' operator
1319 overloaded is found. An error is reported when circular pointer
1320 delegation is detected. */
1321 tree
1322 build_x_arrow (datum)
1323 tree datum;
1325 tree types_memoized = NULL_TREE;
1326 register tree rval = datum;
1327 tree type = TREE_TYPE (rval);
1328 tree last_rval;
1330 if (type == error_mark_node)
1331 return error_mark_node;
1333 if (TREE_CODE (rval) == OFFSET_REF)
1335 rval = resolve_offset_ref (datum);
1336 type = TREE_TYPE (rval);
1339 if (TREE_CODE (type) == REFERENCE_TYPE)
1341 rval = convert_from_reference (rval);
1342 type = TREE_TYPE (rval);
1345 if (IS_AGGR_TYPE (type) && TYPE_OVERLOADS_ARROW (type))
1347 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval, NULL_TREE, NULL_TREE)))
1349 if (rval == error_mark_node)
1350 return error_mark_node;
1352 if (value_member (TREE_TYPE (rval), types_memoized))
1354 error ("circular pointer delegation detected");
1355 return error_mark_node;
1357 else
1359 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1360 types_memoized);
1362 last_rval = rval;
1364 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1365 last_rval = convert_from_reference (last_rval);
1367 else
1368 last_rval = default_conversion (rval);
1370 /* Signature pointers are not dereferenced. */
1371 if (TYPE_LANG_SPECIFIC (TREE_TYPE (last_rval))
1372 && IS_SIGNATURE_POINTER (TREE_TYPE (last_rval)))
1373 return last_rval;
1375 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1376 return build_indirect_ref (last_rval, NULL_PTR);
1378 if (types_memoized)
1379 error ("result of `operator->()' yields non-pointer result");
1380 else
1381 error ("base operand of `->' is not a pointer");
1382 return error_mark_node;
1385 /* Make an expression to refer to the COMPONENT field of
1386 structure or union value DATUM. COMPONENT is an arbitrary
1387 expression. DATUM has not already been checked out to be of
1388 aggregate type.
1390 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1391 return an object of member type to a method of the current class,
1392 but there is not yet enough typing information to know which one.
1393 As a special case, if there is only one method by that name,
1394 it is returned. Otherwise we return an expression which other
1395 routines will have to know how to deal with later. */
1396 tree
1397 build_m_component_ref (datum, component)
1398 tree datum, component;
1400 tree type;
1401 tree objtype = TREE_TYPE (datum);
1402 tree rettype;
1403 tree binfo;
1405 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1407 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1408 rettype = type;
1410 else
1412 component = build_indirect_ref (component, NULL_PTR);
1413 type = TREE_TYPE (component);
1414 rettype = TREE_TYPE (TREE_TYPE (component));
1417 if (datum == error_mark_node || component == error_mark_node)
1418 return error_mark_node;
1420 if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1422 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1423 return error_mark_node;
1426 if (TREE_CODE (objtype) == REFERENCE_TYPE)
1427 objtype = TREE_TYPE (objtype);
1428 objtype = TYPE_MAIN_VARIANT (objtype);
1430 if (! IS_AGGR_TYPE (objtype))
1432 cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1433 cp_error ("which is of non-aggregate type `%T'", objtype);
1434 return error_mark_node;
1437 binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1438 if (binfo == NULL_TREE)
1440 cp_error ("member type `%T::' incompatible with object type `%T'",
1441 TYPE_METHOD_BASETYPE (type), objtype);
1442 return error_mark_node;
1444 else if (binfo == error_mark_node)
1445 return error_mark_node;
1447 return build (OFFSET_REF, rettype, datum, component);
1450 /* Return a tree node for the expression TYPENAME '(' PARMS ')'.
1452 Because we cannot tell whether this construct is really a call to a
1453 constructor or a request for a type conversion, we try both, and
1454 report any ambiguities we find. */
1455 tree
1456 build_functional_cast (exp, parms)
1457 tree exp;
1458 tree parms;
1460 /* This is either a call to a constructor,
1461 or a C cast in C++'s `functional' notation. */
1462 tree type, name = NULL_TREE;
1463 tree expr_as_ctor = NULL_TREE;
1465 if (exp == error_mark_node || parms == error_mark_node)
1466 return error_mark_node;
1468 if (TREE_CODE (exp) == IDENTIFIER_NODE)
1470 name = exp;
1472 if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1473 /* Either an enum or an aggregate type. */
1474 type = IDENTIFIER_TYPE_VALUE (exp);
1475 else
1477 type = lookup_name (exp, 1);
1478 if (!type || TREE_CODE (type) != TYPE_DECL)
1480 cp_error ("`%T' fails to be a typedef or built-in type", name);
1481 return error_mark_node;
1483 type = TREE_TYPE (type);
1486 else
1487 type = exp;
1489 if (IS_SIGNATURE (type))
1491 error ("signature type not allowed in cast or constructor expression");
1492 return error_mark_node;
1495 /* Prepare to evaluate as a call to a constructor. If this expression
1496 is actually used, for example,
1498 return X (arg1, arg2, ...);
1500 then the slot being initialized will be filled in. */
1502 if (name == NULL_TREE)
1504 name = TYPE_NAME (type);
1505 if (TREE_CODE (name) == TYPE_DECL)
1506 name = DECL_NESTED_TYPENAME (name);
1509 if (! IS_AGGR_TYPE (type))
1511 /* this must build a C cast */
1512 if (parms == NULL_TREE)
1513 parms = integer_zero_node;
1514 else
1516 if (TREE_CHAIN (parms) != NULL_TREE)
1517 pedwarn ("initializer list being treated as compound expression");
1518 parms = build_compound_expr (parms);
1521 return build_c_cast (type, parms, 1);
1524 if (TYPE_SIZE (type) == NULL_TREE)
1526 cp_error ("type `%T' is not yet defined", type);
1527 return error_mark_node;
1530 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1531 return build_c_cast (type, parms, 1);
1533 expr_as_ctor = build_method_call (NULL_TREE, name, parms,
1534 NULL_TREE, LOOKUP_NORMAL);
1536 if (expr_as_ctor == error_mark_node)
1537 return error_mark_node;
1539 return build_cplus_new (type, expr_as_ctor, 1);
1542 /* Return the character string for the name that encodes the
1543 enumeral value VALUE in the domain TYPE. */
1544 char *
1545 enum_name_string (value, type)
1546 tree value;
1547 tree type;
1549 register tree values = TYPE_VALUES (type);
1550 register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value);
1552 my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1553 while (values
1554 && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval)
1555 values = TREE_CHAIN (values);
1556 if (values == NULL_TREE)
1558 char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type));
1560 /* Value must have been cast. */
1561 sprintf (buf, "(enum %s)%d",
1562 TYPE_NAME_STRING (type), intval);
1563 return buf;
1565 return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1568 #if 0
1569 /* Print out a language-specific error message for
1570 (Pascal) case or (C) switch statements.
1571 CODE tells what sort of message to print.
1572 TYPE is the type of the switch index expression.
1573 NEW is the new value that we were trying to add.
1574 OLD is the old value that stopped us from adding it. */
1575 void
1576 report_case_error (code, type, new_value, old_value)
1577 int code;
1578 tree type;
1579 tree new_value, old_value;
1581 if (code == 1)
1583 if (new_value)
1584 error ("case label not within a switch statement");
1585 else
1586 error ("default label not within a switch statement");
1588 else if (code == 2)
1590 if (new_value == 0)
1592 error ("multiple default labels in one switch");
1593 return;
1595 if (TREE_CODE (new_value) == RANGE_EXPR)
1596 if (TREE_CODE (old_value) == RANGE_EXPR)
1598 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1599 if (TREE_CODE (type) == ENUMERAL_TYPE)
1600 sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1601 enum_name_string (TREE_OPERAND (new_value, 0), type),
1602 enum_name_string (TREE_OPERAND (new_value, 1), type),
1603 enum_name_string (TREE_OPERAND (old_value, 0), type),
1604 enum_name_string (TREE_OPERAND (old_value, 1), type));
1605 else
1606 sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1607 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1608 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1609 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1610 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1611 error (buf);
1613 else
1615 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1616 if (TREE_CODE (type) == ENUMERAL_TYPE)
1617 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1618 enum_name_string (TREE_OPERAND (new_value, 0), type),
1619 enum_name_string (TREE_OPERAND (new_value, 1), type),
1620 enum_name_string (old_value, type));
1621 else
1622 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1623 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1624 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1625 TREE_INT_CST_LOW (old_value));
1626 error (buf);
1628 else if (TREE_CODE (old_value) == RANGE_EXPR)
1630 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1631 if (TREE_CODE (type) == ENUMERAL_TYPE)
1632 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1633 enum_name_string (TREE_OPERAND (old_value, 0), type),
1634 enum_name_string (TREE_OPERAND (old_value, 1), type),
1635 enum_name_string (new_value, type));
1636 else
1637 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1638 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1639 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1640 TREE_INT_CST_LOW (new_value));
1641 error (buf);
1643 else
1645 if (TREE_CODE (type) == ENUMERAL_TYPE)
1646 error ("duplicate label `%s' in switch statement",
1647 enum_name_string (new_value, type));
1648 else
1649 error ("duplicate label (%d) in switch statement",
1650 TREE_INT_CST_LOW (new_value));
1653 else if (code == 3)
1655 if (TREE_CODE (type) == ENUMERAL_TYPE)
1656 warning ("case value out of range for enum %s",
1657 TYPE_NAME_STRING (type));
1658 else
1659 warning ("case value out of range");
1661 else if (code == 4)
1663 if (TREE_CODE (type) == ENUMERAL_TYPE)
1664 error ("range values `%s' and `%s' reversed",
1665 enum_name_string (new_value, type),
1666 enum_name_string (old_value, type));
1667 else
1668 error ("range values reversed");
1671 #endif