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