* cp-tree.def (SCOPE_STMT): Take one operand.
[official-gcc.git] / gcc / cp / typeck2.c
blob64671dbae2f1a84a68de5985bd8d78701a581d2f
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
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.
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). */
33 #include "config.h"
34 #include "system.h"
35 #include "tree.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "toplev.h"
39 #include "output.h"
41 static tree process_init_constructor PROTO((tree, tree, tree *));
42 static void ack PVPROTO ((const char *, ...)) ATTRIBUTE_PRINTF_1;
44 /* Print an error message stemming from an attempt to use
45 BASETYPE as a base class for TYPE. */
47 tree
48 error_not_base_type (basetype, type)
49 tree basetype, type;
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;
57 tree
58 binfo_or_else (parent_or_type, type)
59 tree parent_or_type, type;
61 tree binfo;
62 if (TYPE_MAIN_VARIANT (parent_or_type) == TYPE_MAIN_VARIANT (type))
63 return TYPE_BINFO (parent_or_type);
64 if ((binfo = get_binfo (parent_or_type, TYPE_MAIN_VARIANT (type), 0)))
66 if (binfo == error_mark_node)
67 return NULL_TREE;
68 return binfo;
70 error_not_base_type (parent_or_type, type);
71 return NULL_TREE;
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.) */
79 void
80 readonly_error (arg, string, soft)
81 tree arg;
82 const char *string;
83 int soft;
85 const char *fmt;
86 void (*fn) PVPROTO ((const char *, ...));
88 if (soft)
89 fn = cp_pedwarn;
90 else
91 fn = cp_error;
93 if (TREE_CODE (arg) == COMPONENT_REF)
95 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
96 fmt = "%s of member `%D' in read-only structure";
97 else
98 fmt = "%s of read-only member `%D'";
99 (*fn) (fmt, string, TREE_OPERAND (arg, 1));
101 else if (TREE_CODE (arg) == VAR_DECL)
103 if (DECL_LANG_SPECIFIC (arg)
104 && DECL_IN_AGGR_P (arg)
105 && !TREE_STATIC (arg))
106 fmt = "%s of constant field `%D'";
107 else
108 fmt = "%s of read-only variable `%D'";
109 (*fn) (fmt, string, arg);
111 else if (TREE_CODE (arg) == PARM_DECL)
112 (*fn) ("%s of read-only parameter `%D'", string, arg);
113 else if (TREE_CODE (arg) == INDIRECT_REF
114 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
115 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
116 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
117 (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0));
118 else if (TREE_CODE (arg) == RESULT_DECL)
119 (*fn) ("%s of read-only named return value `%D'", string, arg);
120 else if (TREE_CODE (arg) == FUNCTION_DECL)
121 (*fn) ("%s of function `%D'", string, arg);
122 else
123 (*fn) ("%s of read-only location", string);
126 /* If TYPE has abstract virtual functions, issue an error about trying
127 to create an object of that type. DECL is the object declared, or
128 NULL_TREE if the declaration is unavailable. Returns 1 if an error
129 occurred; zero if all was well. */
132 abstract_virtuals_error (decl, type)
133 tree decl;
134 tree type;
136 tree u;
137 tree tu;
139 if (!CLASS_TYPE_P (type) || !CLASSTYPE_ABSTRACT_VIRTUALS (type))
140 return 0;
142 u = CLASSTYPE_ABSTRACT_VIRTUALS (type);
143 if (decl)
145 if (TREE_CODE (decl) == RESULT_DECL)
146 return 0;
148 if (TREE_CODE (decl) == VAR_DECL)
149 cp_error ("cannot declare variable `%D' to be of type `%T'",
150 decl, type);
151 else if (TREE_CODE (decl) == PARM_DECL)
152 cp_error ("cannot declare parameter `%D' to be of type `%T'",
153 decl, type);
154 else if (TREE_CODE (decl) == FIELD_DECL)
155 cp_error ("cannot declare field `%D' to be of type `%T'",
156 decl, type);
157 else if (TREE_CODE (decl) == FUNCTION_DECL
158 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
159 cp_error ("invalid return type for method `%#D'", decl);
160 else if (TREE_CODE (decl) == FUNCTION_DECL)
161 cp_error ("invalid return type for function `%#D'", decl);
163 else
164 cp_error ("cannot allocate an object of type `%T'", type);
166 /* Only go through this once. */
167 if (TREE_PURPOSE (u) == NULL_TREE)
169 TREE_PURPOSE (u) = error_mark_node;
171 error (" since the following virtual functions are abstract:");
172 for (tu = u; tu; tu = TREE_CHAIN (tu))
173 cp_error_at ("\t%#D", TREE_VALUE (tu));
175 else
176 cp_error (" since type `%T' has abstract virtual functions", type);
178 return 1;
181 /* Print an error message for invalid use of an incomplete type.
182 VALUE is the expression that was used (or 0 if that isn't known)
183 and TYPE is the type that was invalid. */
185 void
186 incomplete_type_error (value, type)
187 tree value;
188 tree type;
190 /* Avoid duplicate error message. */
191 if (TREE_CODE (type) == ERROR_MARK)
192 return;
194 retry:
195 /* We must print an error message. Be clever about what it says. */
197 switch (TREE_CODE (type))
199 case RECORD_TYPE:
200 case UNION_TYPE:
201 case ENUMERAL_TYPE:
202 cp_error ("invalid use of undefined type `%#T'", type);
203 cp_error_at ("forward declaration of `%#T'", type);
204 break;
206 case VOID_TYPE:
207 cp_error ("invalid use of void expression");
208 break;
210 case ARRAY_TYPE:
211 if (TYPE_DOMAIN (type))
213 type = TREE_TYPE (type);
214 goto retry;
216 cp_error ("invalid use of array with unspecified bounds");
217 break;
219 case OFFSET_TYPE:
220 bad_member:
221 cp_error ("invalid use of member (did you forget the `&' ?)");
222 break;
224 case TEMPLATE_TYPE_PARM:
225 cp_error ("invalid use of template type parameter");
226 break;
228 case UNKNOWN_TYPE:
229 if (value && TREE_CODE (value) == COMPONENT_REF)
230 goto bad_member;
231 else if (value && TREE_CODE (value) == ADDR_EXPR)
232 cp_error ("address of overloaded function with no contextual type information");
233 else if (value && TREE_CODE (value) == OVERLOAD)
234 cp_error ("overloaded function with no contextual type information");
235 else
236 cp_error ("insufficient contextual information to determine type");
237 break;
239 default:
240 my_friendly_abort (108);
243 if (value != 0 && (TREE_CODE (value) == VAR_DECL
244 || TREE_CODE (value) == PARM_DECL))
245 cp_error_at ("incomplete `%D' defined here", value);
248 /* Like error(), but don't call report_error_function(). */
250 static void
251 ack VPROTO ((const char *msg, ...))
253 #ifndef ANSI_PROTOTYPES
254 const char *msg;
255 #endif
256 va_list ap;
258 VA_START (ap, msg);
260 #ifndef ANSI_PROTOTYPES
261 msg = va_arg (ap, const char *);
262 #endif
264 if (input_filename)
265 fprintf (stderr, "%s:%d: ", input_filename, lineno);
266 else
267 fprintf (stderr, "%s: ", progname);
269 vfprintf (stderr, msg, ap);
270 va_end (ap);
272 fprintf (stderr, "\n");
275 /* There are times when the compiler can get very confused, confused
276 to the point of giving up by aborting, simply because of previous
277 input errors. It is much better to have the user go back and
278 correct those errors first, and see if it makes us happier, than it
279 is to abort on him. This is because when one has a 10,000 line
280 program, and the compiler comes back with ``core dump'', the user
281 is left not knowing even where to begin to fix things and no place
282 to even try and work around things.
284 The parameter is to uniquely identify the problem to the user, so
285 that they can say, I am having problem 59, and know that fix 7 will
286 probably solve their problem. Or, we can document what problem
287 59 is, so they can understand how to work around it, should they
288 ever run into it.
290 We used to tell people to "fix the above error[s] and try recompiling
291 the program" via a call to fatal, but that message tended to look
292 silly. So instead, we just do the equivalent of a call to fatal in the
293 same situation (call exit).
295 We used to assign sequential numbers for the aborts; now we use an
296 encoding of the date the abort was added, since that has more meaning
297 when we only see the error message. */
299 static int abortcount = 0;
301 void
302 my_friendly_abort (i)
303 int i;
305 /* if the previous error came through here, i.e. report_error_function
306 ended up calling us again, don't just exit; we want a diagnostic of
307 some kind. */
308 if (abortcount == 1)
309 current_function_decl = NULL_TREE;
310 else if (errorcount > 0 || sorrycount > 0)
312 if (abortcount > 1)
314 if (i == 0)
315 ack ("Internal compiler error.");
316 else
317 ack ("Internal compiler error %d.", i);
318 ack ("Please submit a full bug report.");
319 ack ("See <URL:http://www.gnu.org/software/gcc/faq.html#bugreport> for instructions.");
321 else
322 error ("confused by earlier errors, bailing out");
324 exit (34);
326 ++abortcount;
328 if (i == 0)
329 error ("Internal compiler error.");
330 else
331 error ("Internal compiler error %d.", i);
333 error ("Please submit a full bug report.");
334 fatal ("See <URL:http://www.gnu.org/software/gcc/faq.html#bugreport> for instructions.");
337 void
338 my_friendly_assert (cond, where)
339 int cond, where;
341 if (cond == 0)
342 my_friendly_abort (where);
345 /* Perform appropriate conversions on the initial value of a variable,
346 store it in the declaration DECL,
347 and print any error messages that are appropriate.
348 If the init is invalid, store an ERROR_MARK.
350 C++: Note that INIT might be a TREE_LIST, which would mean that it is
351 a base class initializer for some aggregate type, hopefully compatible
352 with DECL. If INIT is a single element, and DECL is an aggregate
353 type, we silently convert INIT into a TREE_LIST, allowing a constructor
354 to be called.
356 If INIT is a TREE_LIST and there is no constructor, turn INIT
357 into a CONSTRUCTOR and use standard initialization techniques.
358 Perhaps a warning should be generated?
360 Returns value of initializer if initialization could not be
361 performed for static variable. In that case, caller must do
362 the storing. */
364 tree
365 store_init_value (decl, init)
366 tree decl, init;
368 register tree value, type;
370 /* If variable's type was invalidly declared, just ignore it. */
372 type = TREE_TYPE (decl);
373 if (TREE_CODE (type) == ERROR_MARK)
374 return NULL_TREE;
376 #if 0
377 /* This breaks arrays, and should not have any effect for other decls. */
378 /* Take care of C++ business up here. */
379 type = TYPE_MAIN_VARIANT (type);
380 #endif
382 if (IS_AGGR_TYPE (type))
384 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
385 && TREE_CODE (init) != CONSTRUCTOR)
386 my_friendly_abort (109);
388 if (TREE_CODE (init) == TREE_LIST)
390 cp_error ("constructor syntax used, but no constructor declared for type `%T'", type);
391 init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
393 #if 0
394 if (TREE_CODE (init) == CONSTRUCTOR)
396 tree field;
398 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */
399 if (CLASSTYPE_N_BASECLASSES (type))
400 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl);
401 if (CLASSTYPE_VTBL_PTR (type))
402 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl);
403 if (TYPE_NEEDS_CONSTRUCTING (type))
405 cp_error_at ("initializer list construction invalid for `%D'", decl);
406 error ("due to the presence of a constructor");
408 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
409 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
411 cp_error_at ("initializer list construction invalid for `%D'", decl);
412 cp_error_at ("due to non-public access of member `%D'", field);
414 for (field = TYPE_METHODS (type); field; field = TREE_CHAIN (field))
415 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
417 cp_error_at ("initializer list construction invalid for `%D'", decl);
418 cp_error_at ("due to non-public access of member `%D'", field);
421 #endif
423 else if (TREE_CODE (init) == TREE_LIST
424 && TREE_TYPE (init) != unknown_type_node)
426 if (TREE_CODE (decl) == RESULT_DECL)
428 if (TREE_CHAIN (init))
430 warning ("comma expression used to initialize return value");
431 init = build_compound_expr (init);
433 else
434 init = TREE_VALUE (init);
436 else if (TREE_CODE (init) == TREE_LIST
437 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
439 error ("cannot initialize arrays using this syntax");
440 return NULL_TREE;
442 else
444 /* We get here with code like `int a (2);' */
446 if (TREE_CHAIN (init) != NULL_TREE)
448 pedwarn ("initializer list being treated as compound expression");
449 init = build_compound_expr (init);
451 else
452 init = TREE_VALUE (init);
456 /* End of special C++ code. */
458 /* Digest the specified initializer into an expression. */
460 value = digest_init (type, init, (tree *) 0);
462 /* Store the expression if valid; else report error. */
464 if (TREE_CODE (value) == ERROR_MARK)
466 /* Other code expects that initializers for objects of types that need
467 constructing never make it into DECL_INITIAL, and passes 'init' to
468 build_aggr_init without checking DECL_INITIAL. So just return. */
469 else if (TYPE_NEEDS_CONSTRUCTING (type))
470 return value;
471 else if (TREE_STATIC (decl)
472 && (! TREE_CONSTANT (value)
473 || ! initializer_constant_valid_p (value, TREE_TYPE (value))
474 #if 0
475 /* A STATIC PUBLIC int variable doesn't have to be
476 run time inited when doing pic. (mrs) */
477 /* Since ctors and dtors are the only things that can
478 reference vtables, and they are always written down
479 the vtable definition, we can leave the
480 vtables in initialized data space.
481 However, other initialized data cannot be initialized
482 this way. Instead a global file-level initializer
483 must do the job. */
484 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
485 #endif
488 return value;
489 #if 0 /* No, that's C. jason 9/19/94 */
490 else
492 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
494 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
495 pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
498 #endif
500 /* Store the VALUE in DECL_INITIAL. If we're building a
501 statement-tree we will actually expand the initialization later
502 when we output this function. */
503 DECL_INITIAL (decl) = value;
504 return NULL_TREE;
507 /* Digest the parser output INIT as an initializer for type TYPE.
508 Return a C expression of type TYPE to represent the initial value.
510 If TAIL is nonzero, it points to a variable holding a list of elements
511 of which INIT is the first. We update the list stored there by
512 removing from the head all the elements that we use.
513 Normally this is only one; we use more than one element only if
514 TYPE is an aggregate and INIT is not a constructor. */
516 tree
517 digest_init (type, init, tail)
518 tree type, init, *tail;
520 enum tree_code code = TREE_CODE (type);
521 tree element = NULL_TREE;
522 tree old_tail_contents = NULL_TREE;
523 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
524 tree node which has no TREE_TYPE. */
525 int raw_constructor;
527 /* By default, assume we use one element from a list.
528 We correct this later in the sole case where it is not true. */
530 if (tail)
532 old_tail_contents = *tail;
533 *tail = TREE_CHAIN (*tail);
536 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
537 && TREE_VALUE (init) == error_mark_node))
538 return error_mark_node;
540 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
541 if (TREE_CODE (init) == NON_LVALUE_EXPR)
542 init = TREE_OPERAND (init, 0);
544 if (TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == type)
545 return init;
547 raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
549 if (raw_constructor
550 && CONSTRUCTOR_ELTS (init) != 0
551 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
553 element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
554 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
555 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
556 element = TREE_OPERAND (element, 0);
557 if (element == error_mark_node)
558 return element;
561 /* Initialization of an array of chars from a string constant
562 optionally enclosed in braces. */
564 if (code == ARRAY_TYPE)
566 tree typ1;
568 if (TREE_CODE (init) == TREE_LIST)
570 error ("initializing array with parameter list");
571 return error_mark_node;
574 typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
575 if ((typ1 == char_type_node
576 || typ1 == signed_char_type_node
577 || typ1 == unsigned_char_type_node
578 || typ1 == unsigned_wchar_type_node
579 || typ1 == signed_wchar_type_node)
580 && ((init && TREE_CODE (init) == STRING_CST)
581 || (element && TREE_CODE (element) == STRING_CST)))
583 tree string = element ? element : init;
585 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
586 != char_type_node)
587 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
589 error ("char-array initialized from wide string");
590 return error_mark_node;
592 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
593 == char_type_node)
594 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
596 error ("int-array initialized from non-wide string");
597 return error_mark_node;
600 TREE_TYPE (string) = type;
601 if (TYPE_DOMAIN (type) != 0
602 && TREE_CONSTANT (TYPE_SIZE (type)))
604 register int size
605 = TREE_INT_CST_LOW (TYPE_SIZE (type));
606 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
607 /* In C it is ok to subtract 1 from the length of the string
608 because it's ok to ignore the terminating null char that is
609 counted in the length of the constant, but in C++ this would
610 be invalid. */
611 if (size < TREE_STRING_LENGTH (string))
612 pedwarn ("initializer-string for array of chars is too long");
614 return string;
618 /* Handle scalar types, including conversions,
619 and signature pointers and references. */
621 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
622 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
623 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
624 || TYPE_PTRMEMFUNC_P (type))
626 if (raw_constructor)
628 if (element == 0)
630 error ("initializer for scalar variable requires one element");
631 return error_mark_node;
633 init = element;
635 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
637 cp_pedwarn ("braces around scalar initializer for `%T'", type);
638 init = CONSTRUCTOR_ELTS (init);
639 if (TREE_CHAIN (init))
640 cp_pedwarn ("ignoring extra initializers for `%T'", type);
641 init = TREE_VALUE (init);
644 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
645 "initialization", NULL_TREE, 0);
648 /* Come here only for records and arrays (and unions with constructors). */
650 if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
652 cp_error ("variable-sized object of type `%T' may not be initialized",
653 type);
654 return error_mark_node;
657 if (code == ARRAY_TYPE || IS_AGGR_TYPE_CODE (code))
659 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type))
661 cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
662 type, init);
663 return error_mark_node;
665 else if (raw_constructor)
666 return process_init_constructor (type, init, (tree *)0);
667 else if (can_convert_arg (type, TREE_TYPE (init), init)
668 || TYPE_NON_AGGREGATE_CLASS (type))
669 /* These are never initialized from multiple constructor elements. */;
670 else if (tail != 0)
672 *tail = old_tail_contents;
673 return process_init_constructor (type, 0, tail);
676 if (code != ARRAY_TYPE)
678 int flags = LOOKUP_NORMAL;
679 /* Initialization from { } is copy-initialization. */
680 if (tail)
681 flags |= LOOKUP_ONLYCONVERTING;
683 return convert_for_initialization (NULL_TREE, type, init, flags,
684 "initialization", NULL_TREE, 0);
688 error ("invalid initializer");
689 return error_mark_node;
692 /* Process a constructor for a variable of type TYPE.
693 The constructor elements may be specified either with INIT or with ELTS,
694 only one of which should be non-null.
696 If INIT is specified, it is a CONSTRUCTOR node which is specifically
697 and solely for initializing this datum.
699 If ELTS is specified, it is the address of a variable containing
700 a list of expressions. We take as many elements as we need
701 from the head of the list and update the list.
703 In the resulting constructor, TREE_CONSTANT is set if all elts are
704 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
705 constants that the assembler and linker can compute them. */
707 static tree
708 process_init_constructor (type, init, elts)
709 tree type, init, *elts;
711 register tree tail;
712 /* List of the elements of the result constructor,
713 in reverse order. */
714 register tree members = NULL;
715 register tree next1;
716 tree result;
717 int allconstant = 1;
718 int allsimple = 1;
719 int erroneous = 0;
721 /* Make TAIL be the list of elements to use for the initialization,
722 no matter how the data was given to us. */
724 if (elts)
726 if (warn_missing_braces)
727 warning ("aggregate has a partly bracketed initializer");
728 tail = *elts;
730 else
731 tail = CONSTRUCTOR_ELTS (init);
733 /* Gobble as many elements as needed, and make a constructor or initial value
734 for each element of this aggregate. Chain them together in result.
735 If there are too few, use 0 for each scalar ultimate component. */
737 if (TREE_CODE (type) == ARRAY_TYPE)
739 tree domain = TYPE_DOMAIN (type);
740 register long len;
741 register int i;
743 if (domain)
744 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
745 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
746 + 1);
747 else
748 len = -1; /* Take as many as there are */
750 for (i = 0; len < 0 || i < len; i++)
752 if (tail)
754 if (TREE_PURPOSE (tail)
755 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
756 || TREE_INT_CST_LOW (TREE_PURPOSE (tail)) != i))
757 sorry ("non-trivial labeled initializers");
759 if (TREE_VALUE (tail) != 0)
761 tree tail1 = tail;
762 next1 = digest_init (TREE_TYPE (type),
763 TREE_VALUE (tail), &tail1);
764 if (next1 == error_mark_node)
765 return next1;
766 my_friendly_assert
767 (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
768 TYPE_MAIN_VARIANT (TREE_TYPE (next1))),
769 981123);
770 my_friendly_assert (tail1 == 0
771 || TREE_CODE (tail1) == TREE_LIST, 319);
772 if (tail == tail1 && len < 0)
774 error ("non-empty initializer for array of empty elements");
775 /* Just ignore what we were supposed to use. */
776 tail1 = NULL_TREE;
778 tail = tail1;
780 else
782 next1 = error_mark_node;
783 tail = TREE_CHAIN (tail);
786 else if (len < 0)
787 /* We're done. */
788 break;
789 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
791 /* If this type needs constructors run for
792 default-initialization, we can't rely on the backend to do it
793 for us, so build up TARGET_EXPRs. If the type in question is
794 a class, just build one up; if it's an array, recurse. */
796 if (IS_AGGR_TYPE (TREE_TYPE (type)))
797 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
798 else
799 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
800 next1 = digest_init (TREE_TYPE (type), next1, 0);
802 else
803 /* The default zero-initialization is fine for us; don't
804 add anything to the CONSTRUCTOR. */
805 break;
807 if (next1 == error_mark_node)
808 erroneous = 1;
809 else if (!TREE_CONSTANT (next1))
810 allconstant = 0;
811 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
812 allsimple = 0;
813 members = expr_tree_cons (NULL_TREE, next1, members);
816 else if (TREE_CODE (type) == RECORD_TYPE)
818 register tree field;
820 if (tail)
822 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
824 sorry ("initializer list for object of class with virtual baseclasses");
825 return error_mark_node;
828 if (TYPE_BINFO_BASETYPES (type))
830 sorry ("initializer list for object of class with baseclasses");
831 return error_mark_node;
834 if (TYPE_VIRTUAL_P (type))
836 sorry ("initializer list for object using virtual functions");
837 return error_mark_node;
841 for (field = TYPE_FIELDS (type); field;
842 field = TREE_CHAIN (field))
844 if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
846 members = expr_tree_cons (field, integer_zero_node, members);
847 continue;
850 if (TREE_CODE (field) != FIELD_DECL)
851 continue;
853 if (tail)
855 if (TREE_PURPOSE (tail)
856 && TREE_PURPOSE (tail) != field
857 && TREE_PURPOSE (tail) != DECL_NAME (field))
858 sorry ("non-trivial labeled initializers");
860 if (TREE_VALUE (tail) != 0)
862 tree tail1 = tail;
864 next1 = digest_init (TREE_TYPE (field),
865 TREE_VALUE (tail), &tail1);
866 my_friendly_assert (tail1 == 0
867 || TREE_CODE (tail1) == TREE_LIST, 320);
868 tail = tail1;
870 else
872 next1 = error_mark_node;
873 tail = TREE_CHAIN (tail);
876 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
878 /* If this type needs constructors run for
879 default-initialization, we can't rely on the backend to do it
880 for us, so build up TARGET_EXPRs. If the type in question is
881 a class, just build one up; if it's an array, recurse. */
883 if (IS_AGGR_TYPE (TREE_TYPE (field)))
884 next1 = build_functional_cast (TREE_TYPE (field),
885 NULL_TREE);
886 else
887 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE,
888 NULL_TREE);
889 next1 = digest_init (TREE_TYPE (field), next1, 0);
891 /* Warn when some struct elements are implicitly initialized. */
892 if (extra_warnings)
893 cp_warning ("missing initializer for member `%D'", field);
895 else
897 if (TREE_READONLY (field))
898 cp_error ("uninitialized const member `%D'", field);
899 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
900 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
901 cp_error ("member `%D' with uninitialized const fields",
902 field);
903 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
904 cp_error ("member `%D' is uninitialized reference", field);
906 /* Warn when some struct elements are implicitly initialized
907 to zero. */
908 if (extra_warnings)
909 cp_warning ("missing initializer for member `%D'", field);
911 /* The default zero-initialization is fine for us; don't
912 add anything to the CONSTRUCTOR. */
913 continue;
916 if (next1 == error_mark_node)
917 erroneous = 1;
918 else if (!TREE_CONSTANT (next1))
919 allconstant = 0;
920 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
921 allsimple = 0;
922 members = expr_tree_cons (field, next1, members);
925 else if (TREE_CODE (type) == UNION_TYPE
926 /* If the initializer was empty, use default zero initialization. */
927 && tail)
929 register tree field = TYPE_FIELDS (type);
931 /* Find the first named field. ANSI decided in September 1990
932 that only named fields count here. */
933 while (field && (DECL_NAME (field) == 0
934 || TREE_CODE (field) != FIELD_DECL))
935 field = TREE_CHAIN (field);
937 /* If this element specifies a field, initialize via that field. */
938 if (TREE_PURPOSE (tail) != NULL_TREE)
940 int win = 0;
942 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
943 /* Handle the case of a call by build_c_cast. */
944 field = TREE_PURPOSE (tail), win = 1;
945 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
946 error ("index value instead of field name in union initializer");
947 else
949 tree temp;
950 for (temp = TYPE_FIELDS (type);
951 temp;
952 temp = TREE_CHAIN (temp))
953 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
954 break;
955 if (temp)
956 field = temp, win = 1;
957 else
958 cp_error ("no field `%D' in union being initialized",
959 TREE_PURPOSE (tail));
961 if (!win)
962 TREE_VALUE (tail) = error_mark_node;
964 else if (field == 0)
966 cp_error ("union `%T' with no named members cannot be initialized",
967 type);
968 TREE_VALUE (tail) = error_mark_node;
971 if (TREE_VALUE (tail) != 0)
973 tree tail1 = tail;
975 next1 = digest_init (TREE_TYPE (field),
976 TREE_VALUE (tail), &tail1);
977 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
978 my_friendly_abort (357);
979 tail = tail1;
981 else
983 next1 = error_mark_node;
984 tail = TREE_CHAIN (tail);
987 if (next1 == error_mark_node)
988 erroneous = 1;
989 else if (!TREE_CONSTANT (next1))
990 allconstant = 0;
991 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
992 allsimple = 0;
993 members = expr_tree_cons (field, next1, members);
996 /* If arguments were specified as a list, just remove the ones we used. */
997 if (elts)
998 *elts = tail;
999 /* If arguments were specified as a constructor,
1000 complain unless we used all the elements of the constructor. */
1001 else if (tail)
1002 pedwarn ("excess elements in aggregate initializer");
1004 if (erroneous)
1005 return error_mark_node;
1007 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1008 if (init)
1009 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1010 if (allconstant) TREE_CONSTANT (result) = 1;
1011 if (allconstant && allsimple) TREE_STATIC (result) = 1;
1012 return result;
1015 /* Given a structure or union value DATUM, construct and return
1016 the structure or union component which results from narrowing
1017 that value by the type specified in BASETYPE. For example, given the
1018 hierarchy
1020 class L { int ii; };
1021 class A : L { ... };
1022 class B : L { ... };
1023 class C : A, B { ... };
1025 and the declaration
1027 C x;
1029 then the expression
1031 x.A::ii refers to the ii member of the L part of
1032 the A part of the C object named by X. In this case,
1033 DATUM would be x, and BASETYPE would be A. */
1035 tree
1036 build_scoped_ref (datum, basetype)
1037 tree datum;
1038 tree basetype;
1040 tree ref;
1041 tree type = TREE_TYPE (datum);
1043 if (datum == error_mark_node)
1044 return error_mark_node;
1046 if (TREE_CODE (type) == REFERENCE_TYPE)
1047 type = TREE_TYPE (type);
1049 type = TYPE_MAIN_VARIANT (type);
1051 /* This is an easy conversion. */
1052 if (is_aggr_type (basetype, 1))
1054 tree binfo = TYPE_BINFO (basetype);
1055 if (binfo != TYPE_BINFO (type))
1057 binfo = get_binfo (binfo, type, 1);
1058 if (binfo == error_mark_node)
1059 return error_mark_node;
1060 if (binfo == 0)
1061 return error_not_base_type (basetype, type);
1064 switch (TREE_CODE (datum))
1066 case NOP_EXPR:
1067 case CONVERT_EXPR:
1068 case FLOAT_EXPR:
1069 case FIX_TRUNC_EXPR:
1070 case FIX_FLOOR_EXPR:
1071 case FIX_ROUND_EXPR:
1072 case FIX_CEIL_EXPR:
1073 ref = convert_pointer_to (binfo,
1074 build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0));
1075 break;
1076 default:
1077 ref = convert_pointer_to (binfo,
1078 build_unary_op (ADDR_EXPR, datum, 0));
1080 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1082 return error_mark_node;
1085 /* Build a reference to an object specified by the C++ `->' operator.
1086 Usually this just involves dereferencing the object, but if the
1087 `->' operator is overloaded, then such overloads must be
1088 performed until an object which does not have the `->' operator
1089 overloaded is found. An error is reported when circular pointer
1090 delegation is detected. */
1092 tree
1093 build_x_arrow (datum)
1094 tree datum;
1096 tree types_memoized = NULL_TREE;
1097 register tree rval = datum;
1098 tree type = TREE_TYPE (rval);
1099 tree last_rval = NULL_TREE;
1101 if (type == error_mark_node)
1102 return error_mark_node;
1104 if (processing_template_decl)
1105 return build_min_nt (ARROW_EXPR, rval);
1107 if (TREE_CODE (rval) == OFFSET_REF)
1109 rval = resolve_offset_ref (datum);
1110 type = TREE_TYPE (rval);
1113 if (TREE_CODE (type) == REFERENCE_TYPE)
1115 rval = convert_from_reference (rval);
1116 type = TREE_TYPE (rval);
1119 if (IS_AGGR_TYPE (type))
1121 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval,
1122 NULL_TREE, NULL_TREE)))
1124 if (rval == error_mark_node)
1125 return error_mark_node;
1127 if (value_member (TREE_TYPE (rval), types_memoized))
1129 error ("circular pointer delegation detected");
1130 return error_mark_node;
1132 else
1134 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1135 types_memoized);
1137 last_rval = rval;
1140 if (last_rval == NULL_TREE)
1142 cp_error ("base operand of `->' has non-pointer type `%T'", type);
1143 return error_mark_node;
1146 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1147 last_rval = convert_from_reference (last_rval);
1149 else
1150 last_rval = default_conversion (rval);
1152 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1153 return build_indirect_ref (last_rval, NULL_PTR);
1155 if (types_memoized)
1156 error ("result of `operator->()' yields non-pointer result");
1157 else
1158 error ("base operand of `->' is not a pointer");
1159 return error_mark_node;
1162 /* Make an expression to refer to the COMPONENT field of
1163 structure or union value DATUM. COMPONENT is an arbitrary
1164 expression. DATUM has not already been checked out to be of
1165 aggregate type.
1167 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1168 return an object of member type to a method of the current class,
1169 but there is not yet enough typing information to know which one.
1170 As a special case, if there is only one method by that name,
1171 it is returned. Otherwise we return an expression which other
1172 routines will have to know how to deal with later. */
1174 tree
1175 build_m_component_ref (datum, component)
1176 tree datum, component;
1178 tree type;
1179 tree objtype = TREE_TYPE (datum);
1180 tree rettype;
1181 tree binfo;
1183 if (processing_template_decl)
1184 return build_min_nt (DOTSTAR_EXPR, datum, component);
1186 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1188 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1189 rettype = type;
1191 else
1193 type = TREE_TYPE (TREE_TYPE (component));
1194 rettype = TREE_TYPE (type);
1197 if (datum == error_mark_node || component == error_mark_node)
1198 return error_mark_node;
1200 if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1202 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1203 return error_mark_node;
1206 if (TREE_CODE (objtype) == REFERENCE_TYPE)
1207 objtype = TREE_TYPE (objtype);
1208 objtype = TYPE_MAIN_VARIANT (objtype);
1210 if (! IS_AGGR_TYPE (objtype))
1212 cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1213 cp_error ("which is of non-aggregate type `%T'", objtype);
1214 return error_mark_node;
1217 binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1218 if (binfo == NULL_TREE)
1220 cp_error ("member type `%T::' incompatible with object type `%T'",
1221 TYPE_METHOD_BASETYPE (type), objtype);
1222 return error_mark_node;
1224 else if (binfo == error_mark_node)
1225 return error_mark_node;
1227 component = build (OFFSET_REF, rettype, datum, component);
1228 if (TREE_CODE (type) == OFFSET_TYPE)
1229 component = resolve_offset_ref (component);
1230 return component;
1233 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1235 tree
1236 build_functional_cast (exp, parms)
1237 tree exp;
1238 tree parms;
1240 /* This is either a call to a constructor,
1241 or a C cast in C++'s `functional' notation. */
1242 tree type;
1244 if (exp == error_mark_node || parms == error_mark_node)
1245 return error_mark_node;
1247 if (TREE_CODE (exp) == IDENTIFIER_NODE)
1249 if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1250 /* Either an enum or an aggregate type. */
1251 type = IDENTIFIER_TYPE_VALUE (exp);
1252 else
1254 type = lookup_name (exp, 1);
1255 if (!type || TREE_CODE (type) != TYPE_DECL)
1257 cp_error ("`%T' fails to be a typedef or built-in type", exp);
1258 return error_mark_node;
1260 type = TREE_TYPE (type);
1263 else if (TREE_CODE (exp) == TYPE_DECL)
1264 type = TREE_TYPE (exp);
1265 else
1266 type = exp;
1268 if (processing_template_decl)
1269 return build_min (CAST_EXPR, type, parms);
1271 if (! IS_AGGR_TYPE (type))
1273 /* this must build a C cast */
1274 if (parms == NULL_TREE)
1275 parms = integer_zero_node;
1276 else
1278 if (TREE_CHAIN (parms) != NULL_TREE)
1279 pedwarn ("initializer list being treated as compound expression");
1280 parms = build_compound_expr (parms);
1283 return build_c_cast (type, parms);
1286 /* Prepare to evaluate as a call to a constructor. If this expression
1287 is actually used, for example,
1289 return X (arg1, arg2, ...);
1291 then the slot being initialized will be filled in. */
1293 if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
1295 cp_error ("type `%T' is not yet defined", type);
1296 return error_mark_node;
1298 if (abstract_virtuals_error (NULL_TREE, type))
1299 return error_mark_node;
1301 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1302 return build_c_cast (type, TREE_VALUE (parms));
1304 /* We need to zero-initialize POD types. Let's do that for everything
1305 that doesn't need a constructor. */
1306 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1307 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1309 exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
1310 return get_target_expr (exp);
1313 exp = build_method_call (NULL_TREE, ctor_identifier, parms,
1314 TYPE_BINFO (type), LOOKUP_NORMAL);
1316 if (exp == error_mark_node)
1317 return error_mark_node;
1319 return build_cplus_new (type, exp);
1322 /* Return the character string for the name that encodes the
1323 enumeral value VALUE in the domain TYPE. */
1325 char *
1326 enum_name_string (value, type)
1327 tree value;
1328 tree type;
1330 register tree values = TYPE_VALUES (type);
1331 register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value);
1333 my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1334 while (values
1335 && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval)
1336 values = TREE_CHAIN (values);
1337 if (values == NULL_TREE)
1339 char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type));
1341 /* Value must have been cast. */
1342 sprintf (buf, "(enum %s)%ld",
1343 TYPE_NAME_STRING (type), (long) intval);
1344 return buf;
1346 return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1349 #if 0
1350 /* Print out a language-specific error message for
1351 (Pascal) case or (C) switch statements.
1352 CODE tells what sort of message to print.
1353 TYPE is the type of the switch index expression.
1354 NEW is the new value that we were trying to add.
1355 OLD is the old value that stopped us from adding it. */
1357 void
1358 report_case_error (code, type, new_value, old_value)
1359 int code;
1360 tree type;
1361 tree new_value, old_value;
1363 if (code == 1)
1365 if (new_value)
1366 error ("case label not within a switch statement");
1367 else
1368 error ("default label not within a switch statement");
1370 else if (code == 2)
1372 if (new_value == 0)
1374 error ("multiple default labels in one switch");
1375 return;
1377 if (TREE_CODE (new_value) == RANGE_EXPR)
1378 if (TREE_CODE (old_value) == RANGE_EXPR)
1380 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1381 if (TREE_CODE (type) == ENUMERAL_TYPE)
1382 sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1383 enum_name_string (TREE_OPERAND (new_value, 0), type),
1384 enum_name_string (TREE_OPERAND (new_value, 1), type),
1385 enum_name_string (TREE_OPERAND (old_value, 0), type),
1386 enum_name_string (TREE_OPERAND (old_value, 1), type));
1387 else
1388 sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1389 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1390 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1391 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1392 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1393 error (buf);
1395 else
1397 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1398 if (TREE_CODE (type) == ENUMERAL_TYPE)
1399 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1400 enum_name_string (TREE_OPERAND (new_value, 0), type),
1401 enum_name_string (TREE_OPERAND (new_value, 1), type),
1402 enum_name_string (old_value, type));
1403 else
1404 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1405 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1406 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1407 TREE_INT_CST_LOW (old_value));
1408 error (buf);
1410 else if (TREE_CODE (old_value) == RANGE_EXPR)
1412 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1413 if (TREE_CODE (type) == ENUMERAL_TYPE)
1414 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1415 enum_name_string (TREE_OPERAND (old_value, 0), type),
1416 enum_name_string (TREE_OPERAND (old_value, 1), type),
1417 enum_name_string (new_value, type));
1418 else
1419 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1420 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1421 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1422 TREE_INT_CST_LOW (new_value));
1423 error (buf);
1425 else
1427 if (TREE_CODE (type) == ENUMERAL_TYPE)
1428 error ("duplicate label `%s' in switch statement",
1429 enum_name_string (new_value, type));
1430 else
1431 error ("duplicate label (%d) in switch statement",
1432 TREE_INT_CST_LOW (new_value));
1435 else if (code == 3)
1437 if (TREE_CODE (type) == ENUMERAL_TYPE)
1438 warning ("case value out of range for enum %s",
1439 TYPE_NAME_STRING (type));
1440 else
1441 warning ("case value out of range");
1443 else if (code == 4)
1445 if (TREE_CODE (type) == ENUMERAL_TYPE)
1446 error ("range values `%s' and `%s' reversed",
1447 enum_name_string (new_value, type),
1448 enum_name_string (old_value, type));
1449 else
1450 error ("range values reversed");
1453 #endif
1455 /* Complain about defining new types in inappropriate places. We give an
1456 exception for C-style casts, to accommodate GNU C stylings. */
1458 void
1459 check_for_new_type (string, inptree)
1460 const char *string;
1461 flagged_type_tree inptree;
1463 if (inptree.new_type_flag
1464 && (pedantic || strcmp (string, "cast") != 0))
1465 pedwarn ("ANSI C++ forbids defining types within %s",string);
1468 /* Add new exception specifier SPEC, to the LIST we currently have.
1469 If it's already in LIST then do nothing.
1470 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1471 know what we're doing. */
1473 tree
1474 add_exception_specifier (list, spec, complain)
1475 tree list, spec;
1476 int complain;
1478 int ok;
1479 tree core = spec;
1480 int is_ptr;
1482 if (spec == error_mark_node)
1483 return list;
1485 my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1487 /* [except.spec] 1, type in an exception specifier shall not be
1488 incomplete, or pointer or ref to incomplete other than pointer
1489 to cv void. */
1490 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1491 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1492 core = TREE_TYPE (core);
1493 if (complain < 0)
1494 ok = 1;
1495 else if (TYPE_MAIN_VARIANT (core) == void_type_node)
1496 ok = is_ptr;
1497 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1498 ok = 1;
1499 else
1500 ok = TYPE_SIZE (complete_type (core)) != NULL_TREE;
1502 if (ok)
1504 tree probe;
1506 for (probe = list; probe; probe = TREE_CHAIN (probe))
1507 if (same_type_p (TREE_VALUE (probe), spec))
1508 break;
1509 if (!probe)
1511 spec = build_decl_list (NULL_TREE, spec);
1512 TREE_CHAIN (spec) = list;
1513 list = spec;
1516 else if (complain)
1517 incomplete_type_error (NULL_TREE, core);
1518 return list;