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