92cbcc34aa8e0139793d931131185d0463e1c116
[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 case TEMPLATE_TYPE_PARM:
251 error ("invalid use of template type parameter");
252 return;
253
254 default:
255 my_friendly_abort (108);
256 }
257
258 cp_error (errmsg, type);
259 }
260 }
261
262 /* Like error(), but don't call report_error_function(). */
263
264 static void
265 ack (s, v, v2)
266 char *s;
267 HOST_WIDE_INT v;
268 HOST_WIDE_INT v2;
269 {
270 extern char * progname;
271
272 if (input_filename)
273 fprintf (stderr, "%s:%d: ", input_filename, lineno);
274 else
275 fprintf (stderr, "%s: ", progname);
276
277 fprintf (stderr, s, v, v2);
278 fprintf (stderr, "\n");
279 }
280
281 /* There are times when the compiler can get very confused, confused
282 to the point of giving up by aborting, simply because of previous
283 input errors. It is much better to have the user go back and
284 correct those errors first, and see if it makes us happier, than it
285 is to abort on him. This is because when one has a 10,000 line
286 program, and the compiler comes back with ``core dump'', the user
287 is left not knowing even where to begin to fix things and no place
288 to even try and work around things.
289
290 The parameter is to uniquely identify the problem to the user, so
291 that they can say, I am having problem 59, and know that fix 7 will
292 probably solve their problem. Or, we can document what problem
293 59 is, so they can understand how to work around it, should they
294 ever run into it.
295
296 Note, there will be no more calls in the C++ front end to abort,
297 because the C++ front end is so unreliable still. The C front end
298 can get away with calling abort, because for most of the calls to
299 abort on most machines, it, I suspect, can be proven that it is
300 impossible to ever call abort. The same is not yet true for C++,
301 one day, maybe it will be.
302
303 We used to tell people to "fix the above error[s] and try recompiling
304 the program" via a call to fatal, but that message tended to look
305 silly. So instead, we just do the equivalent of a call to fatal in the
306 same situation (call exit). */
307
308 /* First used: 0 (reserved), Last used: 369. Free: */
309
310 static int abortcount = 0;
311
312 void
313 my_friendly_abort (i)
314 int i;
315 {
316 /* if the previous error came through here, i.e. report_error_function
317 ended up calling us again, don't just exit; we want a diagnostic of
318 some kind. */
319 if (abortcount == 1)
320 current_function_decl = NULL_TREE;
321 else if (errorcount > 0 || sorrycount > 0)
322 {
323 if (abortcount > 1)
324 {
325 if (i == 0)
326 ack ("Internal compiler error.");
327 else
328 ack ("Internal compiler error %d.", i);
329 ack ("Please submit a full bug report to `egcs-bugs@cygnus.com'.");
330 }
331 else
332 error ("confused by earlier errors, bailing out");
333
334 exit (34);
335 }
336 ++abortcount;
337
338 if (i == 0)
339 error ("Internal compiler error.");
340 else
341 error ("Internal compiler error %d.", i);
342
343 fatal ("Please submit a full bug report to `egcs-bugs@cygnus.com'.");
344 }
345
346 void
347 my_friendly_assert (cond, where)
348 int cond, where;
349 {
350 if (cond == 0)
351 my_friendly_abort (where);
352 }
353 \f
354 /* Return nonzero if VALUE is a valid constant-valued expression
355 for use in initializing a static variable; one that can be an
356 element of a "constant" initializer.
357
358 Return null_pointer_node if the value is absolute;
359 if it is relocatable, return the variable that determines the relocation.
360 We assume that VALUE has been folded as much as possible;
361 therefore, we do not need to check for such things as
362 arithmetic-combinations of integers. */
363
364 tree
365 initializer_constant_valid_p (value, endtype)
366 tree value;
367 tree endtype;
368 {
369 switch (TREE_CODE (value))
370 {
371 case CONSTRUCTOR:
372 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
373 && TREE_CONSTANT (value))
374 return
375 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
376 endtype);
377
378 return TREE_STATIC (value) ? null_pointer_node : 0;
379
380 case INTEGER_CST:
381 case REAL_CST:
382 case STRING_CST:
383 case COMPLEX_CST:
384 return null_pointer_node;
385
386 case ADDR_EXPR:
387 return TREE_OPERAND (value, 0);
388
389 case NON_LVALUE_EXPR:
390 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
391
392 case CONVERT_EXPR:
393 case NOP_EXPR:
394 /* Allow conversions between pointer types. */
395 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
396 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
397 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
398
399 /* Allow conversions between real types. */
400 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
401 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
402 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
403
404 /* Allow length-preserving conversions between integer types. */
405 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
406 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
407 && (TYPE_PRECISION (TREE_TYPE (value))
408 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
409 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
410
411 /* Allow conversions between other integer types only if
412 explicit value. */
413 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
414 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
415 {
416 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
417 endtype);
418 if (inner == null_pointer_node)
419 return null_pointer_node;
420 return 0;
421 }
422
423 /* Allow (int) &foo provided int is as wide as a pointer. */
424 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
425 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
426 && (TYPE_PRECISION (TREE_TYPE (value))
427 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
428 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
429 endtype);
430
431 /* Likewise conversions from int to pointers. */
432 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
433 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
434 && (TYPE_PRECISION (TREE_TYPE (value))
435 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
436 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
437 endtype);
438
439 /* Allow conversions to union types if the value inside is okay. */
440 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
441 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
442 endtype);
443 return 0;
444
445 case PLUS_EXPR:
446 if ((TREE_CODE (endtype) == INTEGER_TYPE)
447 && (TYPE_PRECISION (endtype) < POINTER_SIZE))
448 return 0;
449 {
450 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
451 endtype);
452 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
453 endtype);
454 /* If either term is absolute, use the other terms relocation. */
455 if (valid0 == null_pointer_node)
456 return valid1;
457 if (valid1 == null_pointer_node)
458 return valid0;
459 return 0;
460 }
461
462 case MINUS_EXPR:
463 if ((TREE_CODE (endtype) == INTEGER_TYPE)
464 && (TYPE_PRECISION (endtype) < POINTER_SIZE))
465 return 0;
466 {
467 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
468 endtype);
469 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
470 endtype);
471 /* Win if second argument is absolute. */
472 if (valid1 == null_pointer_node)
473 return valid0;
474 /* Win if both arguments have the same relocation.
475 Then the value is absolute. */
476 if (valid0 == valid1)
477 return null_pointer_node;
478 return 0;
479 }
480
481 default:
482 break;
483 }
484
485 return 0;
486 }
487 \f
488 /* Perform appropriate conversions on the initial value of a variable,
489 store it in the declaration DECL,
490 and print any error messages that are appropriate.
491 If the init is invalid, store an ERROR_MARK.
492
493 C++: Note that INIT might be a TREE_LIST, which would mean that it is
494 a base class initializer for some aggregate type, hopefully compatible
495 with DECL. If INIT is a single element, and DECL is an aggregate
496 type, we silently convert INIT into a TREE_LIST, allowing a constructor
497 to be called.
498
499 If INIT is a TREE_LIST and there is no constructor, turn INIT
500 into a CONSTRUCTOR and use standard initialization techniques.
501 Perhaps a warning should be generated?
502
503 Returns value of initializer if initialization could not be
504 performed for static variable. In that case, caller must do
505 the storing. */
506
507 tree
508 store_init_value (decl, init)
509 tree decl, init;
510 {
511 register tree value, type;
512
513 /* If variable's type was invalidly declared, just ignore it. */
514
515 type = TREE_TYPE (decl);
516 if (TREE_CODE (type) == ERROR_MARK)
517 return NULL_TREE;
518
519 #if 0
520 /* This breaks arrays, and should not have any effect for other decls. */
521 /* Take care of C++ business up here. */
522 type = TYPE_MAIN_VARIANT (type);
523 #endif
524
525 if (IS_AGGR_TYPE (type))
526 {
527 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
528 && TREE_CODE (init) != CONSTRUCTOR)
529 my_friendly_abort (109);
530
531 /* Although we are not allowed to declare variables of signature
532 type, we complain about a possible constructor call in such a
533 declaration as well. */
534 if (TREE_CODE (init) == TREE_LIST
535 && IS_SIGNATURE (type))
536 {
537 cp_error ("constructor syntax cannot be used with signature type `%T'",
538 type);
539 init = error_mark_node;
540 }
541 else if (TREE_CODE (init) == TREE_LIST)
542 {
543 cp_error ("constructor syntax used, but no constructor declared for type `%T'", type);
544 init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
545 }
546 #if 0
547 if (TREE_CODE (init) == CONSTRUCTOR)
548 {
549 tree field;
550
551 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */
552 if (CLASSTYPE_N_BASECLASSES (type))
553 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl);
554 if (CLASSTYPE_VTBL_PTR (type))
555 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl);
556 if (TYPE_NEEDS_CONSTRUCTING (type))
557 {
558 cp_error_at ("initializer list construction invalid for `%D'", decl);
559 error ("due to the presence of a constructor");
560 }
561 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
562 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
563 {
564 cp_error_at ("initializer list construction invalid for `%D'", decl);
565 cp_error_at ("due to non-public access of member `%D'", field);
566 }
567 for (field = TYPE_METHODS (type); field; field = TREE_CHAIN (field))
568 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
569 {
570 cp_error_at ("initializer list construction invalid for `%D'", decl);
571 cp_error_at ("due to non-public access of member `%D'", field);
572 }
573 }
574 #endif
575 }
576 else if (TREE_CODE (init) == TREE_LIST
577 && TREE_TYPE (init) != unknown_type_node)
578 {
579 if (TREE_CODE (decl) == RESULT_DECL)
580 {
581 if (TREE_CHAIN (init))
582 {
583 warning ("comma expression used to initialize return value");
584 init = build_compound_expr (init);
585 }
586 else
587 init = TREE_VALUE (init);
588 }
589 else if (TREE_TYPE (init) != 0
590 && TREE_CODE (TREE_TYPE (init)) == OFFSET_TYPE)
591 {
592 /* Use the type of our variable to instantiate
593 the type of our initializer. */
594 init = instantiate_type (type, init, 1);
595 }
596 else if (TREE_CODE (init) == TREE_LIST
597 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
598 {
599 error ("cannot initialize arrays using this syntax");
600 return NULL_TREE;
601 }
602 else
603 {
604 /* We get here with code like `int a (2);' */
605
606 if (TREE_CHAIN (init) != NULL_TREE)
607 {
608 pedwarn ("initializer list being treated as compound expression");
609 init = build_compound_expr (init);
610 }
611 else
612 init = TREE_VALUE (init);
613 }
614 }
615
616 if (TYPE_PTRMEMFUNC_P (type) && TREE_CODE (init) == CONSTRUCTOR
617 && TREE_TYPE (init) == NULL_TREE)
618 cp_pedwarn ("initializer list for `%T'", type);
619
620 /* End of special C++ code. */
621
622 /* Digest the specified initializer into an expression. */
623
624 value = digest_init (type, init, (tree *) 0);
625
626 /* Store the expression if valid; else report error. */
627
628 if (TREE_CODE (value) == ERROR_MARK)
629 ;
630 /* Other code expects that initializers for objects of types that need
631 constructing never make it into DECL_INITIAL, and passes 'init' to
632 expand_aggr_init without checking DECL_INITIAL. So just return. */
633 else if (TYPE_NEEDS_CONSTRUCTING (type))
634 return value;
635 else if (TREE_STATIC (decl)
636 && (! TREE_CONSTANT (value)
637 || ! initializer_constant_valid_p (value, TREE_TYPE (value))
638 #if 0
639 /* A STATIC PUBLIC int variable doesn't have to be
640 run time inited when doing pic. (mrs) */
641 /* Since ctors and dtors are the only things that can
642 reference vtables, and they are always written down
643 the the vtable definition, we can leave the
644 vtables in initialized data space.
645 However, other initialized data cannot be initialized
646 this way. Instead a global file-level initializer
647 must do the job. */
648 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
649 #endif
650 ))
651
652 return value;
653 #if 0 /* No, that's C. jason 9/19/94 */
654 else
655 {
656 if (pedantic && TREE_CODE (value) == CONSTRUCTOR
657 /* Don't complain about non-constant initializers of
658 signature tables and signature pointers/references. */
659 && ! (TYPE_LANG_SPECIFIC (type)
660 && (IS_SIGNATURE (type)
661 || IS_SIGNATURE_POINTER (type)
662 || IS_SIGNATURE_REFERENCE (type))))
663 {
664 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
665 pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
666 }
667 }
668 #endif
669 DECL_INITIAL (decl) = value;
670 return NULL_TREE;
671 }
672 \f
673 /* Digest the parser output INIT as an initializer for type TYPE.
674 Return a C expression of type TYPE to represent the initial value.
675
676 If TAIL is nonzero, it points to a variable holding a list of elements
677 of which INIT is the first. We update the list stored there by
678 removing from the head all the elements that we use.
679 Normally this is only one; we use more than one element only if
680 TYPE is an aggregate and INIT is not a constructor. */
681
682 tree
683 digest_init (type, init, tail)
684 tree type, init, *tail;
685 {
686 enum tree_code code = TREE_CODE (type);
687 tree element = NULL_TREE;
688 tree old_tail_contents;
689 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
690 tree node which has no TREE_TYPE. */
691 int raw_constructor;
692
693 /* By default, assume we use one element from a list.
694 We correct this later in the sole case where it is not true. */
695
696 if (tail)
697 {
698 old_tail_contents = *tail;
699 *tail = TREE_CHAIN (*tail);
700 }
701
702 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
703 && TREE_VALUE (init) == error_mark_node))
704 return error_mark_node;
705
706 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
707 if (TREE_CODE (init) == NON_LVALUE_EXPR)
708 init = TREE_OPERAND (init, 0);
709
710 if (init && TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (type))
711 init = default_conversion (init);
712
713 if (init && TYPE_PTRMEMFUNC_P (type)
714 && ((TREE_CODE (init) == ADDR_EXPR
715 && ((TREE_CODE (TREE_TYPE (init)) == POINTER_TYPE
716 && TREE_CODE (TREE_TYPE (TREE_TYPE (init))) == METHOD_TYPE)
717 || TREE_CODE (TREE_OPERAND (init, 0)) == TREE_LIST))
718 || TREE_CODE (init) == TREE_LIST
719 || integer_zerop (init)
720 || (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))))
721 {
722 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), init, 0);
723 }
724
725 raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
726
727 if (init && raw_constructor
728 && CONSTRUCTOR_ELTS (init) != 0
729 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
730 {
731 element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
732 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
733 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
734 element = TREE_OPERAND (element, 0);
735 if (element == error_mark_node)
736 return element;
737 }
738
739 /* Any type can be initialized from an expression of the same type,
740 optionally with braces. */
741
742 if (init && TREE_TYPE (init)
743 && (TYPE_MAIN_VARIANT (TREE_TYPE (init)) == type
744 || (code == ARRAY_TYPE && comptypes (TREE_TYPE (init), type, 1))))
745 {
746 if (pedantic && code == ARRAY_TYPE
747 && TREE_CODE (init) != STRING_CST)
748 pedwarn ("ANSI C++ forbids initializing array from array expression");
749 if (TREE_CODE (init) == CONST_DECL)
750 init = DECL_INITIAL (init);
751 else if (TREE_READONLY_DECL_P (init))
752 init = decl_constant_value (init);
753 else if (IS_AGGR_TYPE (type) && TYPE_NEEDS_CONSTRUCTING (type))
754 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
755 LOOKUP_NORMAL);
756 return init;
757 }
758
759 if (element && (TREE_TYPE (element) == type
760 || (code == ARRAY_TYPE && TREE_TYPE (element)
761 && comptypes (TREE_TYPE (element), type, 1))))
762 {
763 if (pedantic && code == ARRAY_TYPE)
764 pedwarn ("ANSI C++ forbids initializing array from array expression");
765 if (pedantic && (code == RECORD_TYPE || code == UNION_TYPE))
766 pedwarn ("ANSI C++ forbids single nonscalar initializer with braces");
767 if (TREE_CODE (element) == CONST_DECL)
768 element = DECL_INITIAL (element);
769 else if (TREE_READONLY_DECL_P (element))
770 element = decl_constant_value (element);
771 return element;
772 }
773
774 /* Initialization of an array of chars from a string constant
775 optionally enclosed in braces. */
776
777 if (code == ARRAY_TYPE)
778 {
779 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
780 if ((typ1 == char_type_node
781 || typ1 == signed_char_type_node
782 || typ1 == unsigned_char_type_node
783 || typ1 == unsigned_wchar_type_node
784 || typ1 == signed_wchar_type_node)
785 && ((init && TREE_CODE (init) == STRING_CST)
786 || (element && TREE_CODE (element) == STRING_CST)))
787 {
788 tree string = element ? element : init;
789
790 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
791 != char_type_node)
792 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
793 {
794 error ("char-array initialized from wide string");
795 return error_mark_node;
796 }
797 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
798 == char_type_node)
799 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
800 {
801 error ("int-array initialized from non-wide string");
802 return error_mark_node;
803 }
804
805 if (pedantic
806 && typ1 != char_type_node
807 && typ1 != signed_char_type_node
808 && typ1 != unsigned_char_type_node)
809 pedwarn ("ANSI C++ forbids string initializer except for `char' elements");
810 TREE_TYPE (string) = type;
811 if (TYPE_DOMAIN (type) != 0
812 && TREE_CONSTANT (TYPE_SIZE (type)))
813 {
814 register int size
815 = TREE_INT_CST_LOW (TYPE_SIZE (type));
816 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
817 /* In C it is ok to subtract 1 from the length of the string
818 because it's ok to ignore the terminating null char that is
819 counted in the length of the constant, but in C++ this would
820 be invalid. */
821 if (size < TREE_STRING_LENGTH (string))
822 pedwarn ("initializer-string for array of chars is too long");
823 }
824 return string;
825 }
826 }
827
828 /* Handle scalar types, including conversions,
829 and signature pointers and references. */
830
831 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
832 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
833 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
834 || (code == RECORD_TYPE && ! raw_constructor
835 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))))
836 {
837 if (raw_constructor)
838 {
839 if (element == 0)
840 {
841 error ("initializer for scalar variable requires one element");
842 return error_mark_node;
843 }
844 init = element;
845 }
846 while (TREE_CODE (init) == CONSTRUCTOR
847 && ! (TREE_TYPE (init)
848 && TYPE_PTRMEMFUNC_P (TREE_TYPE (init))))
849 {
850 cp_pedwarn ("braces around scalar initializer for `%T'", type);
851 init = CONSTRUCTOR_ELTS (init);
852 if (TREE_CHAIN (init))
853 cp_pedwarn ("ignoring extra initializers for `%T'", type);
854 init = TREE_VALUE (init);
855 }
856
857 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
858 "initialization", NULL_TREE, 0);
859 }
860
861 /* Come here only for records and arrays (and unions with constructors). */
862
863 if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
864 {
865 cp_error ("variable-sized object of type `%T' may not be initialized",
866 type);
867 return error_mark_node;
868 }
869
870 if (code == ARRAY_TYPE || code == RECORD_TYPE || code == UNION_TYPE)
871 {
872 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type))
873 {
874 cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
875 type, init);
876 return error_mark_node;
877 }
878 else if (raw_constructor)
879 return process_init_constructor (type, init, (tree *)0);
880 else if (TYPE_NON_AGGREGATE_CLASS (type))
881 {
882 int flags = LOOKUP_NORMAL;
883 /* Initialization from { } is copy-initialization. */
884 if (tail)
885 flags |= LOOKUP_ONLYCONVERTING;
886 return convert_for_initialization (0, type, init, flags,
887 "initialization", NULL_TREE, 0);
888 }
889 else if (tail != 0)
890 {
891 *tail = old_tail_contents;
892 return process_init_constructor (type, 0, tail);
893 }
894
895 if (code != ARRAY_TYPE)
896 return convert_for_initialization (NULL_TREE, type, init, LOOKUP_NORMAL,
897 "initialization", NULL_TREE, 0);
898 }
899
900 error ("invalid initializer");
901 return error_mark_node;
902 }
903 \f
904 /* Process a constructor for a variable of type TYPE.
905 The constructor elements may be specified either with INIT or with ELTS,
906 only one of which should be non-null.
907
908 If INIT is specified, it is a CONSTRUCTOR node which is specifically
909 and solely for initializing this datum.
910
911 If ELTS is specified, it is the address of a variable containing
912 a list of expressions. We take as many elements as we need
913 from the head of the list and update the list.
914
915 In the resulting constructor, TREE_CONSTANT is set if all elts are
916 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
917 constants that the assembler and linker can compute them. */
918
919 static tree
920 process_init_constructor (type, init, elts)
921 tree type, init, *elts;
922 {
923 register tree tail;
924 /* List of the elements of the result constructor,
925 in reverse order. */
926 register tree members = NULL;
927 tree result;
928 int allconstant = 1;
929 int allsimple = 1;
930 int erroneous = 0;
931
932 /* Make TAIL be the list of elements to use for the initialization,
933 no matter how the data was given to us. */
934
935 if (elts)
936 {
937 if (warn_missing_braces)
938 warning ("aggregate has a partly bracketed initializer");
939 tail = *elts;
940 }
941 else
942 tail = CONSTRUCTOR_ELTS (init);
943
944 /* Gobble as many elements as needed, and make a constructor or initial value
945 for each element of this aggregate. Chain them together in result.
946 If there are too few, use 0 for each scalar ultimate component. */
947
948 if (TREE_CODE (type) == ARRAY_TYPE)
949 {
950 tree domain = TYPE_DOMAIN (type);
951 register long len;
952 register int i;
953
954 if (domain)
955 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
956 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
957 + 1);
958 else
959 len = -1; /* Take as many as there are */
960
961 for (i = 0; (len < 0 || i < len) && tail != 0; i++)
962 {
963 register tree next1;
964
965 if (TREE_VALUE (tail) != 0)
966 {
967 tree tail1 = tail;
968 next1 = digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
969 TREE_VALUE (tail), &tail1);
970 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type))
971 && TYPE_MAIN_VARIANT (TREE_TYPE (type)) != TYPE_MAIN_VARIANT (TREE_TYPE (next1)))
972 {
973 /* The fact this needs to be done suggests this code needs
974 to be totally rewritten. */
975 next1 = convert_for_initialization (NULL_TREE, TREE_TYPE (type), next1, LOOKUP_NORMAL, "initialization", NULL_TREE, 0);
976 }
977 my_friendly_assert (tail1 == 0
978 || TREE_CODE (tail1) == TREE_LIST, 319);
979 if (tail == tail1 && len < 0)
980 {
981 error ("non-empty initializer for array of empty elements");
982 /* Just ignore what we were supposed to use. */
983 tail1 = NULL_TREE;
984 }
985 tail = tail1;
986 }
987 else
988 {
989 next1 = error_mark_node;
990 tail = TREE_CHAIN (tail);
991 }
992
993 if (next1 == error_mark_node)
994 erroneous = 1;
995 else if (!TREE_CONSTANT (next1))
996 allconstant = 0;
997 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
998 allsimple = 0;
999 members = expr_tree_cons (NULL_TREE, next1, members);
1000 }
1001 }
1002 if (TREE_CODE (type) == RECORD_TYPE)
1003 {
1004 register tree field;
1005
1006 if (tail)
1007 {
1008 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1009 {
1010 sorry ("initializer list for object of class with virtual baseclasses");
1011 return error_mark_node;
1012 }
1013
1014 if (TYPE_BINFO_BASETYPES (type))
1015 {
1016 sorry ("initializer list for object of class with baseclasses");
1017 return error_mark_node;
1018 }
1019
1020 if (TYPE_VIRTUAL_P (type))
1021 {
1022 sorry ("initializer list for object using virtual functions");
1023 return error_mark_node;
1024 }
1025 }
1026
1027 for (field = TYPE_FIELDS (type); field && tail;
1028 field = TREE_CHAIN (field))
1029 {
1030 register tree next1;
1031
1032 if (! DECL_NAME (field))
1033 {
1034 members = expr_tree_cons (field, integer_zero_node, members);
1035 continue;
1036 }
1037
1038 if (TREE_CODE (field) != FIELD_DECL)
1039 continue;
1040
1041 if (TREE_VALUE (tail) != 0)
1042 {
1043 tree tail1 = tail;
1044
1045 next1 = digest_init (TREE_TYPE (field),
1046 TREE_VALUE (tail), &tail1);
1047 my_friendly_assert (tail1 == 0
1048 || TREE_CODE (tail1) == TREE_LIST, 320);
1049 tail = tail1;
1050 }
1051 else
1052 {
1053 next1 = error_mark_node;
1054 tail = TREE_CHAIN (tail);
1055 }
1056
1057 if (next1 == error_mark_node)
1058 erroneous = 1;
1059 else if (!TREE_CONSTANT (next1))
1060 allconstant = 0;
1061 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1062 allsimple = 0;
1063 members = expr_tree_cons (field, next1, members);
1064 }
1065 for (; field; field = TREE_CHAIN (field))
1066 {
1067 if (TREE_CODE (field) != FIELD_DECL)
1068 continue;
1069
1070 /* Does this field have a default initialization? */
1071 if (DECL_INITIAL (field))
1072 {
1073 register tree next1 = DECL_INITIAL (field);
1074 if (TREE_CODE (next1) == ERROR_MARK)
1075 erroneous = 1;
1076 else if (!TREE_CONSTANT (next1))
1077 allconstant = 0;
1078 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1079 allsimple = 0;
1080 members = expr_tree_cons (field, next1, members);
1081 }
1082 else if (TREE_READONLY (field))
1083 error ("uninitialized const member `%s'",
1084 IDENTIFIER_POINTER (DECL_NAME (field)));
1085 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
1086 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1087 error ("member `%s' with uninitialized const fields",
1088 IDENTIFIER_POINTER (DECL_NAME (field)));
1089 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1090 error ("member `%s' is uninitialized reference",
1091 IDENTIFIER_POINTER (DECL_NAME (field)));
1092 }
1093 }
1094
1095 if (TREE_CODE (type) == UNION_TYPE)
1096 {
1097 register tree field = TYPE_FIELDS (type);
1098 register tree next1;
1099
1100 /* Find the first named field. ANSI decided in September 1990
1101 that only named fields count here. */
1102 while (field && (DECL_NAME (field) == 0
1103 || TREE_CODE (field) != FIELD_DECL))
1104 field = TREE_CHAIN (field);
1105
1106 /* If this element specifies a field, initialize via that field. */
1107 if (TREE_PURPOSE (tail) != NULL_TREE)
1108 {
1109 int win = 0;
1110
1111 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
1112 /* Handle the case of a call by build_c_cast. */
1113 field = TREE_PURPOSE (tail), win = 1;
1114 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
1115 error ("index value instead of field name in union initializer");
1116 else
1117 {
1118 tree temp;
1119 for (temp = TYPE_FIELDS (type);
1120 temp;
1121 temp = TREE_CHAIN (temp))
1122 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
1123 break;
1124 if (temp)
1125 field = temp, win = 1;
1126 else
1127 error ("no field `%s' in union being initialized",
1128 IDENTIFIER_POINTER (TREE_PURPOSE (tail)));
1129 }
1130 if (!win)
1131 TREE_VALUE (tail) = error_mark_node;
1132 }
1133 else if (field == 0)
1134 {
1135 cp_error ("union `%T' with no named members cannot be initialized",
1136 type);
1137 TREE_VALUE (tail) = error_mark_node;
1138 }
1139
1140 if (TREE_VALUE (tail) != 0)
1141 {
1142 tree tail1 = tail;
1143
1144 next1 = digest_init (TREE_TYPE (field),
1145 TREE_VALUE (tail), &tail1);
1146 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
1147 my_friendly_abort (357);
1148 tail = tail1;
1149 }
1150 else
1151 {
1152 next1 = error_mark_node;
1153 tail = TREE_CHAIN (tail);
1154 }
1155
1156 if (next1 == error_mark_node)
1157 erroneous = 1;
1158 else if (!TREE_CONSTANT (next1))
1159 allconstant = 0;
1160 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
1161 allsimple = 0;
1162 members = expr_tree_cons (field, next1, members);
1163 }
1164
1165 /* If arguments were specified as a list, just remove the ones we used. */
1166 if (elts)
1167 *elts = tail;
1168 /* If arguments were specified as a constructor,
1169 complain unless we used all the elements of the constructor. */
1170 else if (tail)
1171 pedwarn ("excess elements in aggregate initializer");
1172
1173 if (erroneous)
1174 return error_mark_node;
1175
1176 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1177 if (init)
1178 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1179 if (allconstant) TREE_CONSTANT (result) = 1;
1180 if (allconstant && allsimple) TREE_STATIC (result) = 1;
1181 return result;
1182 }
1183 \f
1184 /* Given a structure or union value DATUM, construct and return
1185 the structure or union component which results from narrowing
1186 that value by the type specified in BASETYPE. For example, given the
1187 hierarchy
1188
1189 class L { int ii; };
1190 class A : L { ... };
1191 class B : L { ... };
1192 class C : A, B { ... };
1193
1194 and the declaration
1195
1196 C x;
1197
1198 then the expression
1199
1200 x.A::ii refers to the ii member of the L part of
1201 of A part of the C object named by X. In this case,
1202 DATUM would be x, and BASETYPE would be A. */
1203
1204 tree
1205 build_scoped_ref (datum, basetype)
1206 tree datum;
1207 tree basetype;
1208 {
1209 tree ref;
1210 tree type = TREE_TYPE (datum);
1211
1212 if (datum == error_mark_node)
1213 return error_mark_node;
1214
1215 if (TREE_CODE (type) == REFERENCE_TYPE)
1216 type = TREE_TYPE (type);
1217
1218 type = TYPE_MAIN_VARIANT (type);
1219
1220 /* This is an easy conversion. */
1221 if (is_aggr_type (basetype, 1))
1222 {
1223 tree binfo = TYPE_BINFO (basetype);
1224 if (binfo != TYPE_BINFO (type))
1225 {
1226 binfo = get_binfo (binfo, type, 1);
1227 if (binfo == error_mark_node)
1228 return error_mark_node;
1229 if (binfo == 0)
1230 return error_not_base_type (basetype, type);
1231 }
1232
1233 switch (TREE_CODE (datum))
1234 {
1235 case NOP_EXPR:
1236 case CONVERT_EXPR:
1237 case FLOAT_EXPR:
1238 case FIX_TRUNC_EXPR:
1239 case FIX_FLOOR_EXPR:
1240 case FIX_ROUND_EXPR:
1241 case FIX_CEIL_EXPR:
1242 ref = convert_pointer_to (binfo,
1243 build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0));
1244 break;
1245 default:
1246 ref = convert_pointer_to (binfo,
1247 build_unary_op (ADDR_EXPR, datum, 0));
1248 }
1249 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1250 }
1251 return error_mark_node;
1252 }
1253
1254 /* Build a reference to an object specified by the C++ `->' operator.
1255 Usually this just involves dereferencing the object, but if the
1256 `->' operator is overloaded, then such overloads must be
1257 performed until an object which does not have the `->' operator
1258 overloaded is found. An error is reported when circular pointer
1259 delegation is detected. */
1260
1261 tree
1262 build_x_arrow (datum)
1263 tree datum;
1264 {
1265 tree types_memoized = NULL_TREE;
1266 register tree rval = datum;
1267 tree type = TREE_TYPE (rval);
1268 tree last_rval;
1269
1270 if (type == error_mark_node)
1271 return error_mark_node;
1272
1273 if (processing_template_decl)
1274 return build_min_nt (ARROW_EXPR, rval);
1275
1276 if (TREE_CODE (rval) == OFFSET_REF)
1277 {
1278 rval = resolve_offset_ref (datum);
1279 type = TREE_TYPE (rval);
1280 }
1281
1282 if (TREE_CODE (type) == REFERENCE_TYPE)
1283 {
1284 rval = convert_from_reference (rval);
1285 type = TREE_TYPE (rval);
1286 }
1287
1288 if (IS_AGGR_TYPE (type) && TYPE_OVERLOADS_ARROW (complete_type (type)))
1289 {
1290 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval, NULL_TREE, NULL_TREE)))
1291 {
1292 if (rval == error_mark_node)
1293 return error_mark_node;
1294
1295 if (value_member (TREE_TYPE (rval), types_memoized))
1296 {
1297 error ("circular pointer delegation detected");
1298 return error_mark_node;
1299 }
1300 else
1301 {
1302 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1303 types_memoized);
1304 }
1305 last_rval = rval;
1306 }
1307 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1308 last_rval = convert_from_reference (last_rval);
1309 }
1310 else
1311 last_rval = default_conversion (rval);
1312
1313 /* Signature pointers are not dereferenced. */
1314 if (TYPE_LANG_SPECIFIC (TREE_TYPE (last_rval))
1315 && IS_SIGNATURE_POINTER (TREE_TYPE (last_rval)))
1316 return last_rval;
1317
1318 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1319 return build_indirect_ref (last_rval, NULL_PTR);
1320
1321 if (types_memoized)
1322 error ("result of `operator->()' yields non-pointer result");
1323 else
1324 error ("base operand of `->' is not a pointer");
1325 return error_mark_node;
1326 }
1327
1328 /* Make an expression to refer to the COMPONENT field of
1329 structure or union value DATUM. COMPONENT is an arbitrary
1330 expression. DATUM has not already been checked out to be of
1331 aggregate type.
1332
1333 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1334 return an object of member type to a method of the current class,
1335 but there is not yet enough typing information to know which one.
1336 As a special case, if there is only one method by that name,
1337 it is returned. Otherwise we return an expression which other
1338 routines will have to know how to deal with later. */
1339
1340 tree
1341 build_m_component_ref (datum, component)
1342 tree datum, component;
1343 {
1344 tree type;
1345 tree objtype = TREE_TYPE (datum);
1346 tree rettype;
1347 tree binfo;
1348
1349 if (processing_template_decl)
1350 return build_min_nt (DOTSTAR_EXPR, datum, component);
1351
1352 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1353 {
1354 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1355 rettype = type;
1356 }
1357 else
1358 {
1359 component = build_indirect_ref (component, NULL_PTR);
1360 type = TREE_TYPE (component);
1361 rettype = TREE_TYPE (type);
1362 }
1363
1364 if (datum == error_mark_node || component == error_mark_node)
1365 return error_mark_node;
1366
1367 if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1368 {
1369 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1370 return error_mark_node;
1371 }
1372
1373 if (TREE_CODE (objtype) == REFERENCE_TYPE)
1374 objtype = TREE_TYPE (objtype);
1375 objtype = TYPE_MAIN_VARIANT (objtype);
1376
1377 if (! IS_AGGR_TYPE (objtype))
1378 {
1379 cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1380 cp_error ("which is of non-aggregate type `%T'", objtype);
1381 return error_mark_node;
1382 }
1383
1384 binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1385 if (binfo == NULL_TREE)
1386 {
1387 cp_error ("member type `%T::' incompatible with object type `%T'",
1388 TYPE_METHOD_BASETYPE (type), objtype);
1389 return error_mark_node;
1390 }
1391 else if (binfo == error_mark_node)
1392 return error_mark_node;
1393
1394 component = build (OFFSET_REF, rettype, datum, component);
1395 if (TREE_CODE (type) == OFFSET_TYPE)
1396 component = resolve_offset_ref (component);
1397 return component;
1398 }
1399
1400 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1401
1402 tree
1403 build_functional_cast (exp, parms)
1404 tree exp;
1405 tree parms;
1406 {
1407 /* This is either a call to a constructor,
1408 or a C cast in C++'s `functional' notation. */
1409 tree type;
1410
1411 if (exp == error_mark_node || parms == error_mark_node)
1412 return error_mark_node;
1413
1414 if (TREE_CODE (exp) == IDENTIFIER_NODE)
1415 {
1416 if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1417 /* Either an enum or an aggregate type. */
1418 type = IDENTIFIER_TYPE_VALUE (exp);
1419 else
1420 {
1421 type = lookup_name (exp, 1);
1422 if (!type || TREE_CODE (type) != TYPE_DECL)
1423 {
1424 cp_error ("`%T' fails to be a typedef or built-in type", exp);
1425 return error_mark_node;
1426 }
1427 type = TREE_TYPE (type);
1428 }
1429 }
1430 else if (TREE_CODE (exp) == TYPE_DECL)
1431 type = TREE_TYPE (exp);
1432 else
1433 type = exp;
1434
1435 if (processing_template_decl)
1436 return build_min (CAST_EXPR, type, parms);
1437
1438 if (IS_SIGNATURE (type))
1439 {
1440 error ("signature type not allowed in cast or constructor expression");
1441 return error_mark_node;
1442 }
1443
1444 if (! IS_AGGR_TYPE (type))
1445 {
1446 /* this must build a C cast */
1447 if (parms == NULL_TREE)
1448 parms = integer_zero_node;
1449 else
1450 {
1451 if (TREE_CHAIN (parms) != NULL_TREE)
1452 pedwarn ("initializer list being treated as compound expression");
1453 parms = build_compound_expr (parms);
1454 }
1455
1456 return build_c_cast (type, parms);
1457 }
1458
1459 /* Prepare to evaluate as a call to a constructor. If this expression
1460 is actually used, for example,
1461
1462 return X (arg1, arg2, ...);
1463
1464 then the slot being initialized will be filled in. */
1465
1466 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
1467 {
1468 cp_error ("type `%T' is not yet defined", type);
1469 return error_mark_node;
1470 }
1471
1472 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1473 return build_c_cast (type, TREE_VALUE (parms));
1474
1475 exp = build_method_call (NULL_TREE, ctor_identifier, parms,
1476 TYPE_BINFO (type), LOOKUP_NORMAL);
1477
1478 if (exp == error_mark_node)
1479 return error_mark_node;
1480
1481 return build_cplus_new (type, exp);
1482 }
1483 \f
1484 /* Return the character string for the name that encodes the
1485 enumeral value VALUE in the domain TYPE. */
1486
1487 char *
1488 enum_name_string (value, type)
1489 tree value;
1490 tree type;
1491 {
1492 register tree values = TYPE_VALUES (type);
1493 register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value);
1494
1495 my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1496 while (values
1497 && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval)
1498 values = TREE_CHAIN (values);
1499 if (values == NULL_TREE)
1500 {
1501 char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type));
1502
1503 /* Value must have been cast. */
1504 sprintf (buf, "(enum %s)%d",
1505 TYPE_NAME_STRING (type), intval);
1506 return buf;
1507 }
1508 return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1509 }
1510
1511 #if 0
1512 /* Print out a language-specific error message for
1513 (Pascal) case or (C) switch statements.
1514 CODE tells what sort of message to print.
1515 TYPE is the type of the switch index expression.
1516 NEW is the new value that we were trying to add.
1517 OLD is the old value that stopped us from adding it. */
1518
1519 void
1520 report_case_error (code, type, new_value, old_value)
1521 int code;
1522 tree type;
1523 tree new_value, old_value;
1524 {
1525 if (code == 1)
1526 {
1527 if (new_value)
1528 error ("case label not within a switch statement");
1529 else
1530 error ("default label not within a switch statement");
1531 }
1532 else if (code == 2)
1533 {
1534 if (new_value == 0)
1535 {
1536 error ("multiple default labels in one switch");
1537 return;
1538 }
1539 if (TREE_CODE (new_value) == RANGE_EXPR)
1540 if (TREE_CODE (old_value) == RANGE_EXPR)
1541 {
1542 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1543 if (TREE_CODE (type) == ENUMERAL_TYPE)
1544 sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1545 enum_name_string (TREE_OPERAND (new_value, 0), type),
1546 enum_name_string (TREE_OPERAND (new_value, 1), type),
1547 enum_name_string (TREE_OPERAND (old_value, 0), type),
1548 enum_name_string (TREE_OPERAND (old_value, 1), type));
1549 else
1550 sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1551 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1552 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1553 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1554 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1555 error (buf);
1556 }
1557 else
1558 {
1559 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1560 if (TREE_CODE (type) == ENUMERAL_TYPE)
1561 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1562 enum_name_string (TREE_OPERAND (new_value, 0), type),
1563 enum_name_string (TREE_OPERAND (new_value, 1), type),
1564 enum_name_string (old_value, type));
1565 else
1566 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1567 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1568 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1569 TREE_INT_CST_LOW (old_value));
1570 error (buf);
1571 }
1572 else if (TREE_CODE (old_value) == RANGE_EXPR)
1573 {
1574 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1575 if (TREE_CODE (type) == ENUMERAL_TYPE)
1576 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1577 enum_name_string (TREE_OPERAND (old_value, 0), type),
1578 enum_name_string (TREE_OPERAND (old_value, 1), type),
1579 enum_name_string (new_value, type));
1580 else
1581 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1582 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1583 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1584 TREE_INT_CST_LOW (new_value));
1585 error (buf);
1586 }
1587 else
1588 {
1589 if (TREE_CODE (type) == ENUMERAL_TYPE)
1590 error ("duplicate label `%s' in switch statement",
1591 enum_name_string (new_value, type));
1592 else
1593 error ("duplicate label (%d) in switch statement",
1594 TREE_INT_CST_LOW (new_value));
1595 }
1596 }
1597 else if (code == 3)
1598 {
1599 if (TREE_CODE (type) == ENUMERAL_TYPE)
1600 warning ("case value out of range for enum %s",
1601 TYPE_NAME_STRING (type));
1602 else
1603 warning ("case value out of range");
1604 }
1605 else if (code == 4)
1606 {
1607 if (TREE_CODE (type) == ENUMERAL_TYPE)
1608 error ("range values `%s' and `%s' reversed",
1609 enum_name_string (new_value, type),
1610 enum_name_string (old_value, type));
1611 else
1612 error ("range values reversed");
1613 }
1614 }
1615 #endif
1616
1617 void
1618 check_for_new_type (string, inptree)
1619 char *string;
1620 flagged_type_tree inptree;
1621 {
1622 if (pedantic && inptree.new_type_flag)
1623 pedwarn ("ANSI C++ forbids defining types within %s",string);
1624 }