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