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