]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/typeck2.c
call.c, [...]: Add include of toplev.h.
[thirdparty/gcc.git] / gcc / cp / typeck2.c
CommitLineData
8d08fdba
MS
1/* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
c0cc28e1 3 Copyright (C) 1987, 88, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
8d08fdba
MS
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
6bc06b8f
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
8d08fdba
MS
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"
8d052bc7 34#include "system.h"
8d08fdba
MS
35#include "tree.h"
36#include "cp-tree.h"
37#include "flags.h"
12027a89 38#include "toplev.h"
8d08fdba 39
49c249e1 40static tree process_init_constructor PROTO((tree, tree, tree *));
8d08fdba
MS
41
42extern int errorcount;
43extern int sorrycount;
44
45/* Print an error message stemming from an attempt to use
46 BASETYPE as a base class for TYPE. */
e92cc029 47
8d08fdba
MS
48tree
49error_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
58tree
59binfo_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))
a4443a08 64 return TYPE_BINFO (parent_or_type);
8926095f 65 if ((binfo = get_binfo (parent_or_type, TYPE_MAIN_VARIANT (type), 0)))
8d08fdba
MS
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
8d08fdba
MS
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.) */
e92cc029 79
8d08fdba
MS
80void
81readonly_error (arg, string, soft)
82 tree arg;
83 char *string;
84 int soft;
85{
86 char *fmt;
87 void (*fn)();
88
89 if (soft)
fc378698 90 fn = cp_pedwarn;
8d08fdba 91 else
fc378698 92 fn = cp_error;
8d08fdba
MS
93
94 if (TREE_CODE (arg) == COMPONENT_REF)
95 {
96 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
fc378698 97 fmt = "%s of member `%D' in read-only structure";
8d08fdba 98 else
fc378698
MS
99 fmt = "%s of read-only member `%D'";
100 (*fn) (fmt, string, TREE_OPERAND (arg, 1));
8d08fdba
MS
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))
fc378698 107 fmt = "%s of constant field `%D'";
8d08fdba 108 else
fc378698
MS
109 fmt = "%s of read-only variable `%D'";
110 (*fn) (fmt, string, arg);
8d08fdba
MS
111 }
112 else if (TREE_CODE (arg) == PARM_DECL)
fc378698 113 (*fn) ("%s of read-only parameter `%D'", string, arg);
8d08fdba
MS
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))
fc378698 118 (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0));
8d08fdba 119 else if (TREE_CODE (arg) == RESULT_DECL)
fc378698 120 (*fn) ("%s of read-only named return value `%D'", string, arg);
8d08fdba
MS
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. */
e92cc029 127
8d08fdba
MS
128void
129abstract_virtuals_error (decl, type)
130 tree decl;
131 tree type;
132{
133 tree u = CLASSTYPE_ABSTRACT_VIRTUALS (type);
4a67c9e9
MH
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 }
8d08fdba
MS
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 }
4a67c9e9
MH
178 else
179 cp_error ("cannot allocate an object of type `%T'", type);
180
8d08fdba
MS
181 /* Only go through this once. */
182 if (TREE_PURPOSE (u) == NULL_TREE)
183 {
8d08fdba 184 TREE_PURPOSE (u) = error_mark_node;
4a67c9e9
MH
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)
8d08fdba 206 {
4a67c9e9
MH
207 if (DECL_NEEDS_FINAL_OVERRIDER_P (TREE_VALUE (tu)))
208 cp_error ("\t%#D", TREE_VALUE (tu));
209 tu = TREE_CHAIN (tu);
8d08fdba
MS
210 }
211 }
4a67c9e9
MH
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 }
8d08fdba
MS
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. */
e92cc029 229
8d08fdba
MS
230void
231signature_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
39211cd5 256 cp_error ("cannot allocate an object of signature type `%T'", type);
8d08fdba
MS
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
263void
264incomplete_type_error (value, type)
265 tree value;
266 tree type;
267{
a703fb38 268 char *errmsg = 0;
8d08fdba
MS
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))
fc378698 276 cp_error ("`%D' has incomplete type", value);
8d08fdba
MS
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:
8d08fdba 285 case UNION_TYPE:
8d08fdba 286 case ENUMERAL_TYPE:
fc378698 287 errmsg = "invalid use of undefined type `%#T'";
8d08fdba
MS
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
75650646
MM
307 case TEMPLATE_TYPE_PARM:
308 error ("invalid use of template type parameter");
309 return;
310
8d08fdba
MS
311 default:
312 my_friendly_abort (108);
313 }
314
fc378698 315 cp_error (errmsg, type);
8d08fdba
MS
316 }
317}
318
319/* Like error(), but don't call report_error_function(). */
e92cc029 320
8d08fdba
MS
321static void
322ack (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
2636fde4 365/* First used: 0 (reserved), Last used: 369. Free: */
8d08fdba
MS
366
367static int abortcount = 0;
368
369void
370my_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);
f017f649 386 ack ("Please submit a full bug report to `egcs-bugs@cygnus.com'.");
8d08fdba
MS
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
f017f649 400 fatal ("Please submit a full bug report to `egcs-bugs@cygnus.com'.");
8d08fdba
MS
401}
402
403void
404my_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
8ccc31eb
MS
415 Return null_pointer_node if the value is absolute;
416 if it is relocatable, return the variable that determines the relocation.
8d08fdba
MS
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
8ccc31eb
MS
421tree
422initializer_constant_valid_p (value, endtype)
8d08fdba 423 tree value;
8ccc31eb 424 tree endtype;
8d08fdba
MS
425{
426 switch (TREE_CODE (value))
427 {
428 case CONSTRUCTOR:
8ccc31eb
MS
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;
8d08fdba
MS
436
437 case INTEGER_CST:
438 case REAL_CST:
439 case STRING_CST:
8ccc31eb
MS
440 case COMPLEX_CST:
441 return null_pointer_node;
8d08fdba
MS
442
443 case ADDR_EXPR:
8ccc31eb
MS
444 return TREE_OPERAND (value, 0);
445
446 case NON_LVALUE_EXPR:
447 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
8d08fdba
MS
448
449 case CONVERT_EXPR:
450 case NOP_EXPR:
8ccc31eb 451 /* Allow conversions between pointer types. */
31bcaa20
JM
452 if (POINTER_TYPE_P (TREE_TYPE (value))
453 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
8ccc31eb
MS
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
8d08fdba
MS
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
8ccc31eb
MS
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);
8d08fdba
MS
500 return 0;
501
502 case PLUS_EXPR:
5566b478
MS
503 if ((TREE_CODE (endtype) == INTEGER_TYPE)
504 && (TYPE_PRECISION (endtype) < POINTER_SIZE))
8ccc31eb 505 return 0;
8d08fdba 506 {
8ccc31eb
MS
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;
8d08fdba
MS
516 return 0;
517 }
518
519 case MINUS_EXPR:
5566b478
MS
520 if ((TREE_CODE (endtype) == INTEGER_TYPE)
521 && (TYPE_PRECISION (endtype) < POINTER_SIZE))
8ccc31eb 522 return 0;
8d08fdba 523 {
8ccc31eb
MS
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;
8d08fdba
MS
535 return 0;
536 }
7f85441b
KG
537
538 default:
539 break;
8926095f 540 }
8ccc31eb
MS
541
542 return 0;
8d08fdba
MS
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
564tree
565store_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
878cd289
MS
576#if 0
577 /* This breaks arrays, and should not have any effect for other decls. */
8d08fdba
MS
578 /* Take care of C++ business up here. */
579 type = TYPE_MAIN_VARIANT (type);
878cd289 580#endif
8d08fdba 581
e8abc66f 582 if (IS_AGGR_TYPE (type))
8d08fdba 583 {
e8abc66f
MS
584 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
585 && TREE_CODE (init) != CONSTRUCTOR)
586 my_friendly_abort (109);
587
8d08fdba
MS
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;
8d08fdba
MS
607
608 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */
609 if (CLASSTYPE_N_BASECLASSES (type))
c0cc28e1 610 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl);
8d08fdba 611 if (CLASSTYPE_VTBL_PTR (type))
c0cc28e1 612 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl);
8d08fdba
MS
613 if (TYPE_NEEDS_CONSTRUCTING (type))
614 {
c0cc28e1 615 cp_error_at ("initializer list construction invalid for `%D'", decl);
8d08fdba
MS
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 {
c0cc28e1 621 cp_error_at ("initializer list construction invalid for `%D'", decl);
8d08fdba
MS
622 cp_error_at ("due to non-public access of member `%D'", field);
623 }
72b7eeff
MS
624 for (field = TYPE_METHODS (type); field; field = TREE_CHAIN (field))
625 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
8d08fdba 626 {
72b7eeff
MS
627 cp_error_at ("initializer list construction invalid for `%D'", decl);
628 cp_error_at ("due to non-public access of member `%D'", field);
8d08fdba
MS
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 ;
7834ab39
MS
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;
8d08fdba
MS
688 else if (TREE_STATIC (decl)
689 && (! TREE_CONSTANT (value)
8ccc31eb 690 || ! initializer_constant_valid_p (value, TREE_TYPE (value))
8926095f
MS
691#if 0
692 /* A STATIC PUBLIC int variable doesn't have to be
693 run time inited when doing pic. (mrs) */
8d08fdba
MS
694 /* Since ctors and dtors are the only things that can
695 reference vtables, and they are always written down
696 the 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. */
8926095f
MS
701 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
702#endif
703 ))
704
8d08fdba 705 return value;
f376e137 706#if 0 /* No, that's C. jason 9/19/94 */
8d08fdba
MS
707 else
708 {
63718c49
GB
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))))
8d08fdba
MS
716 {
717 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
718 pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
719 }
720 }
f376e137 721#endif
8d08fdba
MS
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
735tree
736digest_init (type, init, tail)
737 tree type, init, *tail;
738{
739 enum tree_code code = TREE_CODE (type);
00595019 740 tree element = NULL_TREE;
a703fb38 741 tree old_tail_contents = NULL_TREE;
8d08fdba
MS
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
8d08fdba
MS
763 raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
764
dc26f471 765 if (raw_constructor
8d08fdba
MS
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
8d08fdba
MS
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 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
783 if ((typ1 == char_type_node
784 || typ1 == signed_char_type_node
785 || typ1 == unsigned_char_type_node
786 || typ1 == unsigned_wchar_type_node
787 || typ1 == signed_wchar_type_node)
788 && ((init && TREE_CODE (init) == STRING_CST)
789 || (element && TREE_CODE (element) == STRING_CST)))
790 {
791 tree string = element ? element : init;
792
793 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
794 != char_type_node)
795 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
796 {
797 error ("char-array initialized from wide string");
798 return error_mark_node;
799 }
800 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
801 == char_type_node)
802 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
803 {
804 error ("int-array initialized from non-wide string");
805 return error_mark_node;
806 }
807
8d08fdba
MS
808 TREE_TYPE (string) = type;
809 if (TYPE_DOMAIN (type) != 0
810 && TREE_CONSTANT (TYPE_SIZE (type)))
811 {
812 register int size
813 = TREE_INT_CST_LOW (TYPE_SIZE (type));
814 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
815 /* In C it is ok to subtract 1 from the length of the string
816 because it's ok to ignore the terminating null char that is
817 counted in the length of the constant, but in C++ this would
818 be invalid. */
819 if (size < TREE_STRING_LENGTH (string))
a4443a08 820 pedwarn ("initializer-string for array of chars is too long");
8d08fdba
MS
821 }
822 return string;
823 }
824 }
825
826 /* Handle scalar types, including conversions,
827 and signature pointers and references. */
828
829 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
830 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
37c46b43 831 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
dc26f471 832 || TYPE_PTRMEMFUNC_P (type)
8d08fdba
MS
833 || (code == RECORD_TYPE && ! raw_constructor
834 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))))
835 {
836 if (raw_constructor)
837 {
838 if (element == 0)
839 {
840 error ("initializer for scalar variable requires one element");
841 return error_mark_node;
842 }
843 init = element;
844 }
dc26f471 845 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
b7484fbe
MS
846 {
847 cp_pedwarn ("braces around scalar initializer for `%T'", type);
848 init = CONSTRUCTOR_ELTS (init);
849 if (TREE_CHAIN (init))
850 cp_pedwarn ("ignoring extra initializers for `%T'", type);
851 init = TREE_VALUE (init);
852 }
8d08fdba
MS
853
854 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
855 "initialization", NULL_TREE, 0);
856 }
857
858 /* Come here only for records and arrays (and unions with constructors). */
859
860 if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
861 {
7177d104
MS
862 cp_error ("variable-sized object of type `%T' may not be initialized",
863 type);
8d08fdba
MS
864 return error_mark_node;
865 }
866
867 if (code == ARRAY_TYPE || code == RECORD_TYPE || code == UNION_TYPE)
868 {
f30432d7
MS
869 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type))
870 {
871 cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
872 type, init);
873 return error_mark_node;
874 }
875 else if (raw_constructor)
8d08fdba 876 return process_init_constructor (type, init, (tree *)0);
dc26f471
JM
877 else if (can_convert_arg (type, TREE_TYPE (init), init)
878 || TYPE_NON_AGGREGATE_CLASS (type))
879 /* These are never initialized from multiple constructor elements. */;
8d08fdba
MS
880 else if (tail != 0)
881 {
882 *tail = old_tail_contents;
883 return process_init_constructor (type, 0, tail);
884 }
d22c8596 885
8d08fdba 886 if (code != ARRAY_TYPE)
dc26f471
JM
887 {
888 int flags = LOOKUP_NORMAL;
889 /* Initialization from { } is copy-initialization. */
890 if (tail)
891 flags |= LOOKUP_ONLYCONVERTING;
892
893 return convert_for_initialization (NULL_TREE, type, init, flags,
894 "initialization", NULL_TREE, 0);
895 }
8d08fdba
MS
896 }
897
898 error ("invalid initializer");
899 return error_mark_node;
900}
901\f
902/* Process a constructor for a variable of type TYPE.
903 The constructor elements may be specified either with INIT or with ELTS,
904 only one of which should be non-null.
905
906 If INIT is specified, it is a CONSTRUCTOR node which is specifically
907 and solely for initializing this datum.
908
909 If ELTS is specified, it is the address of a variable containing
910 a list of expressions. We take as many elements as we need
911 from the head of the list and update the list.
912
913 In the resulting constructor, TREE_CONSTANT is set if all elts are
914 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
915 constants that the assembler and linker can compute them. */
916
917static tree
918process_init_constructor (type, init, elts)
919 tree type, init, *elts;
920{
8d08fdba
MS
921 register tree tail;
922 /* List of the elements of the result constructor,
923 in reverse order. */
924 register tree members = NULL;
925 tree result;
926 int allconstant = 1;
927 int allsimple = 1;
928 int erroneous = 0;
929
930 /* Make TAIL be the list of elements to use for the initialization,
931 no matter how the data was given to us. */
932
933 if (elts)
934 {
935 if (warn_missing_braces)
936 warning ("aggregate has a partly bracketed initializer");
937 tail = *elts;
938 }
939 else
940 tail = CONSTRUCTOR_ELTS (init);
941
942 /* Gobble as many elements as needed, and make a constructor or initial value
943 for each element of this aggregate. Chain them together in result.
944 If there are too few, use 0 for each scalar ultimate component. */
945
946 if (TREE_CODE (type) == ARRAY_TYPE)
947 {
948 tree domain = TYPE_DOMAIN (type);
949 register long len;
950 register int i;
951
952 if (domain)
953 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
954 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
955 + 1);
956 else
957 len = -1; /* Take as many as there are */
958
959 for (i = 0; (len < 0 || i < len) && tail != 0; i++)
960 {
961 register tree next1;
962
f02d609c
JM
963 if (TREE_PURPOSE (tail)
964 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
965 || TREE_INT_CST_LOW (TREE_PURPOSE (tail)) != i))
c8fcb331
JM
966 sorry ("non-trivial labeled initializers");
967
8d08fdba
MS
968 if (TREE_VALUE (tail) != 0)
969 {
970 tree tail1 = tail;
2b6815ea 971 next1 = digest_init (TREE_TYPE (type),
8d08fdba 972 TREE_VALUE (tail), &tail1);
a5894242
MS
973 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type))
974 && TYPE_MAIN_VARIANT (TREE_TYPE (type)) != TYPE_MAIN_VARIANT (TREE_TYPE (next1)))
975 {
976 /* The fact this needs to be done suggests this code needs
977 to be totally rewritten. */
978 next1 = convert_for_initialization (NULL_TREE, TREE_TYPE (type), next1, LOOKUP_NORMAL, "initialization", NULL_TREE, 0);
979 }
8d08fdba
MS
980 my_friendly_assert (tail1 == 0
981 || TREE_CODE (tail1) == TREE_LIST, 319);
982 if (tail == tail1 && len < 0)
983 {
984 error ("non-empty initializer for array of empty elements");
985 /* Just ignore what we were supposed to use. */
eac293a1 986 tail1 = NULL_TREE;
8d08fdba
MS
987 }
988 tail = tail1;
989 }
990 else
991 {
992 next1 = error_mark_node;
993 tail = TREE_CHAIN (tail);
994 }
995
996 if (next1 == error_mark_node)
997 erroneous = 1;
998 else if (!TREE_CONSTANT (next1))
999 allconstant = 0;
8ccc31eb 1000 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
8d08fdba 1001 allsimple = 0;
e66d884e 1002 members = expr_tree_cons (NULL_TREE, next1, members);
8d08fdba
MS
1003 }
1004 }
a3203465 1005 if (TREE_CODE (type) == RECORD_TYPE)
8d08fdba
MS
1006 {
1007 register tree field;
1008
1009 if (tail)
1010 {
1011 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1012 {
1013 sorry ("initializer list for object of class with virtual baseclasses");
1014 return error_mark_node;
1015 }
1016
1017 if (TYPE_BINFO_BASETYPES (type))
1018 {
1019 sorry ("initializer list for object of class with baseclasses");
1020 return error_mark_node;
1021 }
1022
1023 if (TYPE_VIRTUAL_P (type))
1024 {
1025 sorry ("initializer list for object using virtual functions");
1026 return error_mark_node;
1027 }
1028 }
1029
1030 for (field = TYPE_FIELDS (type); field && tail;
1031 field = TREE_CHAIN (field))
1032 {
1033 register tree next1;
1034
1035 if (! DECL_NAME (field))
1036 {
e66d884e 1037 members = expr_tree_cons (field, integer_zero_node, members);
8d08fdba
MS
1038 continue;
1039 }
1040
2986ae00 1041 if (TREE_CODE (field) != FIELD_DECL)
8d08fdba
MS
1042 continue;
1043
c8fcb331 1044 if (TREE_PURPOSE (tail)
6c3e25eb 1045 && TREE_PURPOSE (tail) != field
c8fcb331
JM
1046 && TREE_PURPOSE (tail) != DECL_NAME (field))
1047 sorry ("non-trivial labeled initializers");
1048
8d08fdba
MS
1049 if (TREE_VALUE (tail) != 0)
1050 {
1051 tree tail1 = tail;
1052
51c184be
MS
1053 next1 = digest_init (TREE_TYPE (field),
1054 TREE_VALUE (tail), &tail1);
8d08fdba
MS
1055 my_friendly_assert (tail1 == 0
1056 || TREE_CODE (tail1) == TREE_LIST, 320);
1057 tail = tail1;
1058 }
1059 else
1060 {
1061 next1 = error_mark_node;
1062 tail = TREE_CHAIN (tail);
1063 }
1064
1065 if (next1 == error_mark_node)
1066 erroneous = 1;
1067 else if (!TREE_CONSTANT (next1))
1068 allconstant = 0;
8ccc31eb 1069 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
8d08fdba 1070 allsimple = 0;
e66d884e 1071 members = expr_tree_cons (field, next1, members);
8d08fdba
MS
1072 }
1073 for (; field; field = TREE_CHAIN (field))
1074 {
1075 if (TREE_CODE (field) != FIELD_DECL)
1076 continue;
1077
1078 /* Does this field have a default initialization? */
1079 if (DECL_INITIAL (field))
1080 {
1081 register tree next1 = DECL_INITIAL (field);
1082 if (TREE_CODE (next1) == ERROR_MARK)
1083 erroneous = 1;
1084 else if (!TREE_CONSTANT (next1))
1085 allconstant = 0;
8ccc31eb 1086 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
8d08fdba 1087 allsimple = 0;
e66d884e 1088 members = expr_tree_cons (field, next1, members);
8d08fdba
MS
1089 }
1090 else if (TREE_READONLY (field))
1091 error ("uninitialized const member `%s'",
1092 IDENTIFIER_POINTER (DECL_NAME (field)));
1093 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
1094 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1095 error ("member `%s' with uninitialized const fields",
1096 IDENTIFIER_POINTER (DECL_NAME (field)));
1097 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1098 error ("member `%s' is uninitialized reference",
1099 IDENTIFIER_POINTER (DECL_NAME (field)));
1100 }
1101 }
1102
a3203465 1103 if (TREE_CODE (type) == UNION_TYPE)
8d08fdba
MS
1104 {
1105 register tree field = TYPE_FIELDS (type);
1106 register tree next1;
1107
1108 /* Find the first named field. ANSI decided in September 1990
1109 that only named fields count here. */
6467930b
MS
1110 while (field && (DECL_NAME (field) == 0
1111 || TREE_CODE (field) != FIELD_DECL))
8d08fdba
MS
1112 field = TREE_CHAIN (field);
1113
1114 /* If this element specifies a field, initialize via that field. */
1115 if (TREE_PURPOSE (tail) != NULL_TREE)
1116 {
1117 int win = 0;
1118
1119 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
1120 /* Handle the case of a call by build_c_cast. */
1121 field = TREE_PURPOSE (tail), win = 1;
1122 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
1123 error ("index value instead of field name in union initializer");
1124 else
1125 {
1126 tree temp;
1127 for (temp = TYPE_FIELDS (type);
1128 temp;
1129 temp = TREE_CHAIN (temp))
1130 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
1131 break;
1132 if (temp)
1133 field = temp, win = 1;
1134 else
1135 error ("no field `%s' in union being initialized",
1136 IDENTIFIER_POINTER (TREE_PURPOSE (tail)));
1137 }
1138 if (!win)
1139 TREE_VALUE (tail) = error_mark_node;
1140 }
7177d104
MS
1141 else if (field == 0)
1142 {
1143 cp_error ("union `%T' with no named members cannot be initialized",
1144 type);
1145 TREE_VALUE (tail) = error_mark_node;
1146 }
8d08fdba
MS
1147
1148 if (TREE_VALUE (tail) != 0)
1149 {
1150 tree tail1 = tail;
1151
1152 next1 = digest_init (TREE_TYPE (field),
1153 TREE_VALUE (tail), &tail1);
1154 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
7177d104 1155 my_friendly_abort (357);
8d08fdba
MS
1156 tail = tail1;
1157 }
1158 else
1159 {
1160 next1 = error_mark_node;
1161 tail = TREE_CHAIN (tail);
1162 }
1163
1164 if (next1 == error_mark_node)
1165 erroneous = 1;
1166 else if (!TREE_CONSTANT (next1))
1167 allconstant = 0;
8ccc31eb 1168 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
8d08fdba 1169 allsimple = 0;
e66d884e 1170 members = expr_tree_cons (field, next1, members);
8d08fdba
MS
1171 }
1172
1173 /* If arguments were specified as a list, just remove the ones we used. */
1174 if (elts)
1175 *elts = tail;
1176 /* If arguments were specified as a constructor,
1177 complain unless we used all the elements of the constructor. */
1178 else if (tail)
7177d104 1179 pedwarn ("excess elements in aggregate initializer");
8d08fdba
MS
1180
1181 if (erroneous)
1182 return error_mark_node;
1183
1184 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1185 if (init)
1186 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1187 if (allconstant) TREE_CONSTANT (result) = 1;
1188 if (allconstant && allsimple) TREE_STATIC (result) = 1;
1189 return result;
1190}
1191\f
1192/* Given a structure or union value DATUM, construct and return
1193 the structure or union component which results from narrowing
be99da77 1194 that value by the type specified in BASETYPE. For example, given the
8d08fdba
MS
1195 hierarchy
1196
1197 class L { int ii; };
1198 class A : L { ... };
1199 class B : L { ... };
1200 class C : A, B { ... };
1201
1202 and the declaration
1203
1204 C x;
1205
1206 then the expression
1207
be99da77 1208 x.A::ii refers to the ii member of the L part of
8d08fdba 1209 of A part of the C object named by X. In this case,
e92cc029 1210 DATUM would be x, and BASETYPE would be A. */
8d08fdba
MS
1211
1212tree
be99da77 1213build_scoped_ref (datum, basetype)
8d08fdba 1214 tree datum;
be99da77 1215 tree basetype;
8d08fdba
MS
1216{
1217 tree ref;
1218 tree type = TREE_TYPE (datum);
1219
1220 if (datum == error_mark_node)
1221 return error_mark_node;
1222
1223 if (TREE_CODE (type) == REFERENCE_TYPE)
1224 type = TREE_TYPE (type);
1225
1226 type = TYPE_MAIN_VARIANT (type);
1227
8d08fdba 1228 /* This is an easy conversion. */
be99da77 1229 if (is_aggr_type (basetype, 1))
8d08fdba 1230 {
be99da77 1231 tree binfo = TYPE_BINFO (basetype);
8d08fdba
MS
1232 if (binfo != TYPE_BINFO (type))
1233 {
1234 binfo = get_binfo (binfo, type, 1);
1235 if (binfo == error_mark_node)
1236 return error_mark_node;
1237 if (binfo == 0)
be99da77 1238 return error_not_base_type (basetype, type);
8d08fdba
MS
1239 }
1240
1241 switch (TREE_CODE (datum))
1242 {
1243 case NOP_EXPR:
1244 case CONVERT_EXPR:
1245 case FLOAT_EXPR:
1246 case FIX_TRUNC_EXPR:
1247 case FIX_FLOOR_EXPR:
1248 case FIX_ROUND_EXPR:
1249 case FIX_CEIL_EXPR:
1250 ref = convert_pointer_to (binfo,
1251 build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0));
1252 break;
1253 default:
1254 ref = convert_pointer_to (binfo,
1255 build_unary_op (ADDR_EXPR, datum, 0));
1256 }
1257 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1258 }
1259 return error_mark_node;
1260}
1261
1262/* Build a reference to an object specified by the C++ `->' operator.
1263 Usually this just involves dereferencing the object, but if the
1264 `->' operator is overloaded, then such overloads must be
1265 performed until an object which does not have the `->' operator
1266 overloaded is found. An error is reported when circular pointer
1267 delegation is detected. */
e92cc029 1268
8d08fdba
MS
1269tree
1270build_x_arrow (datum)
1271 tree datum;
1272{
1273 tree types_memoized = NULL_TREE;
1274 register tree rval = datum;
1275 tree type = TREE_TYPE (rval);
a703fb38 1276 tree last_rval = NULL_TREE;
8d08fdba
MS
1277
1278 if (type == error_mark_node)
1279 return error_mark_node;
1280
5156628f 1281 if (processing_template_decl)
5566b478
MS
1282 return build_min_nt (ARROW_EXPR, rval);
1283
a0a33927
MS
1284 if (TREE_CODE (rval) == OFFSET_REF)
1285 {
1286 rval = resolve_offset_ref (datum);
1287 type = TREE_TYPE (rval);
1288 }
1289
8d08fdba
MS
1290 if (TREE_CODE (type) == REFERENCE_TYPE)
1291 {
1292 rval = convert_from_reference (rval);
1293 type = TREE_TYPE (rval);
1294 }
1295
3c215895 1296 if (IS_AGGR_TYPE (type))
8d08fdba 1297 {
3c215895
JM
1298 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval,
1299 NULL_TREE, NULL_TREE)))
8d08fdba
MS
1300 {
1301 if (rval == error_mark_node)
1302 return error_mark_node;
1303
1304 if (value_member (TREE_TYPE (rval), types_memoized))
1305 {
1306 error ("circular pointer delegation detected");
1307 return error_mark_node;
1308 }
1309 else
1310 {
1311 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1312 types_memoized);
1313 }
1314 last_rval = rval;
1315 }
297dcfb3
MM
1316
1317 if (last_rval == NULL_TREE)
1318 {
1319 cp_error ("base operand of `->' has non-pointer type `%T'", type);
1320 return error_mark_node;
1321 }
1322
8d08fdba
MS
1323 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1324 last_rval = convert_from_reference (last_rval);
1325 }
1326 else
1327 last_rval = default_conversion (rval);
1328
8d08fdba
MS
1329 /* Signature pointers are not dereferenced. */
1330 if (TYPE_LANG_SPECIFIC (TREE_TYPE (last_rval))
1331 && IS_SIGNATURE_POINTER (TREE_TYPE (last_rval)))
1332 return last_rval;
1333
1334 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1335 return build_indirect_ref (last_rval, NULL_PTR);
1336
8d08fdba
MS
1337 if (types_memoized)
1338 error ("result of `operator->()' yields non-pointer result");
1339 else
1340 error ("base operand of `->' is not a pointer");
1341 return error_mark_node;
1342}
1343
1344/* Make an expression to refer to the COMPONENT field of
1345 structure or union value DATUM. COMPONENT is an arbitrary
1346 expression. DATUM has not already been checked out to be of
1347 aggregate type.
1348
1349 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1350 return an object of member type to a method of the current class,
1351 but there is not yet enough typing information to know which one.
1352 As a special case, if there is only one method by that name,
1353 it is returned. Otherwise we return an expression which other
1354 routines will have to know how to deal with later. */
e92cc029 1355
8d08fdba
MS
1356tree
1357build_m_component_ref (datum, component)
1358 tree datum, component;
1359{
1360 tree type;
1361 tree objtype = TREE_TYPE (datum);
1362 tree rettype;
71851aaa 1363 tree binfo;
8d08fdba 1364
5156628f 1365 if (processing_template_decl)
5566b478
MS
1366 return build_min_nt (DOTSTAR_EXPR, datum, component);
1367
8d08fdba
MS
1368 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1369 {
1370 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1371 rettype = type;
1372 }
1373 else
1374 {
1375 component = build_indirect_ref (component, NULL_PTR);
1376 type = TREE_TYPE (component);
4ac14744 1377 rettype = TREE_TYPE (type);
8d08fdba
MS
1378 }
1379
1380 if (datum == error_mark_node || component == error_mark_node)
1381 return error_mark_node;
1382
8d08fdba
MS
1383 if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1384 {
51c184be 1385 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
8d08fdba
MS
1386 return error_mark_node;
1387 }
1388
1389 if (TREE_CODE (objtype) == REFERENCE_TYPE)
1390 objtype = TREE_TYPE (objtype);
db5ae43f 1391 objtype = TYPE_MAIN_VARIANT (objtype);
8d08fdba 1392
51c184be
MS
1393 if (! IS_AGGR_TYPE (objtype))
1394 {
1395 cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1396 cp_error ("which is of non-aggregate type `%T'", objtype);
1397 return error_mark_node;
1398 }
71851aaa
MS
1399
1400 binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1401 if (binfo == NULL_TREE)
8d08fdba
MS
1402 {
1403 cp_error ("member type `%T::' incompatible with object type `%T'",
1404 TYPE_METHOD_BASETYPE (type), objtype);
1405 return error_mark_node;
1406 }
71851aaa
MS
1407 else if (binfo == error_mark_node)
1408 return error_mark_node;
8d08fdba 1409
d2e5ee5c
MS
1410 component = build (OFFSET_REF, rettype, datum, component);
1411 if (TREE_CODE (type) == OFFSET_TYPE)
1412 component = resolve_offset_ref (component);
1413 return component;
8d08fdba
MS
1414}
1415
fc378698 1416/* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
e92cc029 1417
8d08fdba
MS
1418tree
1419build_functional_cast (exp, parms)
1420 tree exp;
1421 tree parms;
1422{
1423 /* This is either a call to a constructor,
1424 or a C cast in C++'s `functional' notation. */
fc378698 1425 tree type;
8d08fdba
MS
1426
1427 if (exp == error_mark_node || parms == error_mark_node)
1428 return error_mark_node;
1429
1430 if (TREE_CODE (exp) == IDENTIFIER_NODE)
1431 {
8d08fdba
MS
1432 if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1433 /* Either an enum or an aggregate type. */
1434 type = IDENTIFIER_TYPE_VALUE (exp);
1435 else
1436 {
1437 type = lookup_name (exp, 1);
1438 if (!type || TREE_CODE (type) != TYPE_DECL)
1439 {
fc378698 1440 cp_error ("`%T' fails to be a typedef or built-in type", exp);
8d08fdba
MS
1441 return error_mark_node;
1442 }
1443 type = TREE_TYPE (type);
1444 }
1445 }
45537677
MS
1446 else if (TREE_CODE (exp) == TYPE_DECL)
1447 type = TREE_TYPE (exp);
8d08fdba
MS
1448 else
1449 type = exp;
1450
5156628f 1451 if (processing_template_decl)
5566b478
MS
1452 return build_min (CAST_EXPR, type, parms);
1453
8d08fdba
MS
1454 if (IS_SIGNATURE (type))
1455 {
1456 error ("signature type not allowed in cast or constructor expression");
1457 return error_mark_node;
1458 }
1459
8d08fdba
MS
1460 if (! IS_AGGR_TYPE (type))
1461 {
1462 /* this must build a C cast */
1463 if (parms == NULL_TREE)
d18c083e 1464 parms = integer_zero_node;
8ccc31eb 1465 else
a0a33927 1466 {
8ccc31eb
MS
1467 if (TREE_CHAIN (parms) != NULL_TREE)
1468 pedwarn ("initializer list being treated as compound expression");
a0a33927
MS
1469 parms = build_compound_expr (parms);
1470 }
8ccc31eb 1471
faf5394a 1472 return build_c_cast (type, parms);
8d08fdba
MS
1473 }
1474
45537677
MS
1475 /* Prepare to evaluate as a call to a constructor. If this expression
1476 is actually used, for example,
1477
1478 return X (arg1, arg2, ...);
1479
1480 then the slot being initialized will be filled in. */
1481
5566b478 1482 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
8d08fdba 1483 {
8d08fdba
MS
1484 cp_error ("type `%T' is not yet defined", type);
1485 return error_mark_node;
1486 }
1487
1488 if (parms && TREE_CHAIN (parms) == NULL_TREE)
faf5394a 1489 return build_c_cast (type, TREE_VALUE (parms));
8d08fdba 1490
fc378698
MS
1491 exp = build_method_call (NULL_TREE, ctor_identifier, parms,
1492 TYPE_BINFO (type), LOOKUP_NORMAL);
8d08fdba 1493
fc378698 1494 if (exp == error_mark_node)
a0a33927 1495 return error_mark_node;
8d08fdba 1496
fc378698 1497 return build_cplus_new (type, exp);
8d08fdba
MS
1498}
1499\f
1500/* Return the character string for the name that encodes the
1501 enumeral value VALUE in the domain TYPE. */
e92cc029 1502
8d08fdba
MS
1503char *
1504enum_name_string (value, type)
1505 tree value;
1506 tree type;
1507{
1508 register tree values = TYPE_VALUES (type);
1509 register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value);
1510
1511 my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1512 while (values
1513 && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval)
1514 values = TREE_CHAIN (values);
1515 if (values == NULL_TREE)
1516 {
1517 char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type));
1518
1519 /* Value must have been cast. */
1520 sprintf (buf, "(enum %s)%d",
1521 TYPE_NAME_STRING (type), intval);
1522 return buf;
1523 }
1524 return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1525}
1526
f0e01782 1527#if 0
8d08fdba
MS
1528/* Print out a language-specific error message for
1529 (Pascal) case or (C) switch statements.
1530 CODE tells what sort of message to print.
1531 TYPE is the type of the switch index expression.
1532 NEW is the new value that we were trying to add.
1533 OLD is the old value that stopped us from adding it. */
e92cc029 1534
8d08fdba
MS
1535void
1536report_case_error (code, type, new_value, old_value)
1537 int code;
1538 tree type;
1539 tree new_value, old_value;
1540{
1541 if (code == 1)
1542 {
1543 if (new_value)
1544 error ("case label not within a switch statement");
1545 else
1546 error ("default label not within a switch statement");
1547 }
1548 else if (code == 2)
1549 {
1550 if (new_value == 0)
1551 {
1552 error ("multiple default labels in one switch");
1553 return;
1554 }
1555 if (TREE_CODE (new_value) == RANGE_EXPR)
1556 if (TREE_CODE (old_value) == RANGE_EXPR)
1557 {
1558 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1559 if (TREE_CODE (type) == ENUMERAL_TYPE)
1560 sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1561 enum_name_string (TREE_OPERAND (new_value, 0), type),
1562 enum_name_string (TREE_OPERAND (new_value, 1), type),
1563 enum_name_string (TREE_OPERAND (old_value, 0), type),
1564 enum_name_string (TREE_OPERAND (old_value, 1), type));
1565 else
1566 sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1567 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1568 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1569 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1570 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1571 error (buf);
1572 }
1573 else
1574 {
1575 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1576 if (TREE_CODE (type) == ENUMERAL_TYPE)
1577 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1578 enum_name_string (TREE_OPERAND (new_value, 0), type),
1579 enum_name_string (TREE_OPERAND (new_value, 1), type),
1580 enum_name_string (old_value, type));
1581 else
1582 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1583 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1584 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1585 TREE_INT_CST_LOW (old_value));
1586 error (buf);
1587 }
1588 else if (TREE_CODE (old_value) == RANGE_EXPR)
1589 {
1590 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1591 if (TREE_CODE (type) == ENUMERAL_TYPE)
1592 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1593 enum_name_string (TREE_OPERAND (old_value, 0), type),
1594 enum_name_string (TREE_OPERAND (old_value, 1), type),
1595 enum_name_string (new_value, type));
1596 else
1597 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1598 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1599 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1600 TREE_INT_CST_LOW (new_value));
1601 error (buf);
1602 }
1603 else
1604 {
1605 if (TREE_CODE (type) == ENUMERAL_TYPE)
1606 error ("duplicate label `%s' in switch statement",
1607 enum_name_string (new_value, type));
1608 else
1609 error ("duplicate label (%d) in switch statement",
1610 TREE_INT_CST_LOW (new_value));
1611 }
1612 }
1613 else if (code == 3)
1614 {
1615 if (TREE_CODE (type) == ENUMERAL_TYPE)
1616 warning ("case value out of range for enum %s",
1617 TYPE_NAME_STRING (type));
1618 else
1619 warning ("case value out of range");
1620 }
1621 else if (code == 4)
1622 {
1623 if (TREE_CODE (type) == ENUMERAL_TYPE)
1624 error ("range values `%s' and `%s' reversed",
1625 enum_name_string (new_value, type),
1626 enum_name_string (old_value, type));
1627 else
1628 error ("range values reversed");
1629 }
1630}
f0e01782 1631#endif
46b02c6d
MS
1632
1633void
1634check_for_new_type (string, inptree)
1635 char *string;
1636 flagged_type_tree inptree;
1637{
1638 if (pedantic && inptree.new_type_flag)
1639 pedwarn ("ANSI C++ forbids defining types within %s",string);
1640}