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