]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-typeck.c
* regclass.c (record_reg_classes): Correctly handle 'p' constraint.
[thirdparty/gcc.git] / gcc / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
e5e809f4 2 Copyright (C) 1987, 88, 91-97, 1998 Free Software Foundation, Inc.
400fbf9f
JW
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
940d9d63
RK
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
400fbf9f
JW
20
21
22/* This file is part of the C front end.
23 It contains routines to build C expressions given their operands,
24 including computing the types of the result, C-specific error checks,
25 and some optimization.
26
27 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
28 and to process initializations in declarations (since they work
29 like a strange sort of assignment). */
30
31#include "config.h"
670ee920 32#include "system.h"
400fbf9f
JW
33#include "tree.h"
34#include "c-tree.h"
35#include "flags.h"
e14417fa 36#include "output.h"
234042f4
JL
37#include "rtl.h"
38#include "expr.h"
5f6da302 39#include "toplev.h"
ab87f8c8 40#include "intl.h"
400fbf9f 41
b71c7f8a 42/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 43 message within this initializer. */
b71c7f8a 44static int missing_braces_mentioned;
103b7b17 45
9f6de2b9 46static tree qualify_type PROTO((tree, tree));
75ddf8b0
RK
47static int comp_target_types PROTO((tree, tree));
48static int function_types_compatible_p PROTO((tree, tree));
49static int type_lists_compatible_p PROTO((tree, tree));
50static int self_promoting_type_p PROTO((tree));
51static tree decl_constant_value PROTO((tree));
52static tree lookup_field PROTO((tree, tree, tree *));
53static tree convert_arguments PROTO((tree, tree, tree, tree));
54static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
55static tree pointer_diff PROTO((tree, tree));
56static tree unary_complex_lvalue PROTO((enum tree_code, tree));
57static void pedantic_lvalue_warning PROTO((enum tree_code));
58static tree internal_build_compound_expr PROTO((tree, int));
59static tree convert_for_assignment PROTO((tree, tree, char *, tree,
60 tree, int));
61static void warn_for_assignment PROTO((char *, char *, tree, int));
62static tree valid_compound_expr_initializer PROTO((tree, tree));
63static void push_string PROTO((char *));
64static void push_member_name PROTO((tree));
65static void push_array_bounds PROTO((int));
66static int spelling_length PROTO((void));
67static char *print_spelling PROTO((char *));
ab87f8c8 68static void warning_init PROTO((char *));
75ddf8b0
RK
69static tree digest_init PROTO((tree, tree, int, int));
70static void check_init_type_bitfields PROTO((tree));
71static void output_init_element PROTO((tree, tree, tree, int));
72static void output_pending_init_elements PROTO((int));
e5e809f4
JL
73static void add_pending_init PROTO((tree, tree));
74static int pending_init_member PROTO((tree));
400fbf9f
JW
75\f
76/* Do `exp = require_complete_type (exp);' to make sure exp
77 does not have an incomplete type. (That includes void types.) */
78
79tree
80require_complete_type (value)
81 tree value;
82{
83 tree type = TREE_TYPE (value);
84
85 /* First, detect a valid value with a complete type. */
86 if (TYPE_SIZE (type) != 0
87 && type != void_type_node)
88 return value;
89
90 incomplete_type_error (value, type);
91 return error_mark_node;
92}
93
94/* Print an error message for invalid use of an incomplete type.
95 VALUE is the expression that was used (or 0 if that isn't known)
96 and TYPE is the type that was invalid. */
97
98void
99incomplete_type_error (value, type)
100 tree value;
101 tree type;
102{
ab87f8c8 103 char *type_code_string;
400fbf9f
JW
104
105 /* Avoid duplicate error message. */
106 if (TREE_CODE (type) == ERROR_MARK)
107 return;
108
109 if (value != 0 && (TREE_CODE (value) == VAR_DECL
110 || TREE_CODE (value) == PARM_DECL))
111 error ("`%s' has an incomplete type",
112 IDENTIFIER_POINTER (DECL_NAME (value)));
113 else
114 {
115 retry:
116 /* We must print an error message. Be clever about what it says. */
117
118 switch (TREE_CODE (type))
119 {
120 case RECORD_TYPE:
ab87f8c8 121 type_code_string = "struct";
400fbf9f
JW
122 break;
123
124 case UNION_TYPE:
ab87f8c8 125 type_code_string = "union";
400fbf9f
JW
126 break;
127
128 case ENUMERAL_TYPE:
ab87f8c8 129 type_code_string = "enum";
400fbf9f
JW
130 break;
131
132 case VOID_TYPE:
133 error ("invalid use of void expression");
134 return;
135
136 case ARRAY_TYPE:
137 if (TYPE_DOMAIN (type))
138 {
139 type = TREE_TYPE (type);
140 goto retry;
141 }
142 error ("invalid use of array with unspecified bounds");
143 return;
144
145 default:
146 abort ();
147 }
148
149 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ab87f8c8
JL
150 error ("invalid use of undefined type `%s %s'",
151 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
400fbf9f
JW
152 else
153 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
154 error ("invalid use of incomplete typedef `%s'",
155 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
156 }
157}
158
159/* Return a variant of TYPE which has all the type qualifiers of LIKE
160 as well as those of TYPE. */
161
162static tree
163qualify_type (type, like)
164 tree type, like;
165{
3932261a 166 return c_build_qualified_type (type, TYPE_QUALS (like));
400fbf9f
JW
167}
168\f
169/* Return the common type of two types.
170 We assume that comptypes has already been done and returned 1;
6cb72a7d
RS
171 if that isn't so, this may crash. In particular, we assume that qualifiers
172 match.
400fbf9f
JW
173
174 This is the type for the result of most arithmetic operations
6cb72a7d 175 if the operands have the given two types. */
400fbf9f
JW
176
177tree
178common_type (t1, t2)
179 tree t1, t2;
180{
181 register enum tree_code code1;
182 register enum tree_code code2;
4b027d16 183 tree attributes;
400fbf9f
JW
184
185 /* Save time if the two types are the same. */
186
187 if (t1 == t2) return t1;
188
189 /* If one type is nonsense, use the other. */
190 if (t1 == error_mark_node)
191 return t2;
192 if (t2 == error_mark_node)
193 return t1;
194
d9525bec
BK
195 /* Merge the attributes. */
196 attributes = merge_machine_type_attributes (t1, t2);
4b027d16 197
400fbf9f
JW
198 /* Treat an enum type as the unsigned integer type of the same width. */
199
200 if (TREE_CODE (t1) == ENUMERAL_TYPE)
201 t1 = type_for_size (TYPE_PRECISION (t1), 1);
202 if (TREE_CODE (t2) == ENUMERAL_TYPE)
203 t2 = type_for_size (TYPE_PRECISION (t2), 1);
204
205 code1 = TREE_CODE (t1);
206 code2 = TREE_CODE (t2);
207
75326e8c
RK
208 /* If one type is complex, form the common type of the non-complex
209 components, then make that complex. Use T1 or T2 if it is the
210 required type. */
b6a10c9f
RS
211 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
212 {
75326e8c
RK
213 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
214 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
215 tree subtype = common_type (subtype1, subtype2);
216
217 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
4b027d16 218 return build_type_attribute_variant (t1, attributes);
75326e8c 219 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
4b027d16 220 return build_type_attribute_variant (t2, attributes);
b6a10c9f 221 else
4b027d16
RK
222 return build_type_attribute_variant (build_complex_type (subtype),
223 attributes);
b6a10c9f
RS
224 }
225
400fbf9f
JW
226 switch (code1)
227 {
228 case INTEGER_TYPE:
229 case REAL_TYPE:
230 /* If only one is real, use it as the result. */
231
232 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
4b027d16 233 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
234
235 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
4b027d16 236 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
237
238 /* Both real or both integers; use the one with greater precision. */
239
240 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
4b027d16 241 return build_type_attribute_variant (t1, attributes);
400fbf9f 242 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
4b027d16 243 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
244
245 /* Same precision. Prefer longs to ints even when same size. */
246
36618528
RS
247 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
248 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
4b027d16
RK
249 return build_type_attribute_variant (long_unsigned_type_node,
250 attributes);
400fbf9f 251
36618528
RS
252 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
253 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
400fbf9f
JW
254 {
255 /* But preserve unsignedness from the other type,
256 since long cannot hold all the values of an unsigned int. */
257 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
4b027d16
RK
258 t1 = long_unsigned_type_node;
259 else
260 t1 = long_integer_type_node;
261 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
262 }
263
e9a25f70
JL
264 /* Likewise, prefer long double to double even if same size. */
265 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
266 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
267 return build_type_attribute_variant (long_double_type_node,
268 attributes);
269
400fbf9f
JW
270 /* Otherwise prefer the unsigned one. */
271
272 if (TREE_UNSIGNED (t1))
4b027d16
RK
273 return build_type_attribute_variant (t1, attributes);
274 else
275 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
276
277 case POINTER_TYPE:
400fbf9f
JW
278 /* For two pointers, do this recursively on the target type,
279 and combine the qualifiers of the two types' targets. */
8706edbc
RS
280 /* This code was turned off; I don't know why.
281 But ANSI C specifies doing this with the qualifiers.
282 So I turned it on again. */
400fbf9f 283 {
3932261a
MM
284 tree pointed_to_1 = TREE_TYPE (t1);
285 tree pointed_to_2 = TREE_TYPE (t2);
286 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
287 TYPE_MAIN_VARIANT (pointed_to_2));
288 t1 = build_pointer_type (c_build_qualified_type
289 (target,
290 TYPE_QUALS (pointed_to_1) |
291 TYPE_QUALS (pointed_to_2)));
4b027d16 292 return build_type_attribute_variant (t1, attributes);
400fbf9f 293 }
8706edbc 294#if 0
4b027d16
RK
295 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
296 return build_type_attribute_variant (t1, attributes);
8706edbc 297#endif
400fbf9f
JW
298
299 case ARRAY_TYPE:
300 {
301 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
302 /* Save space: see if the result is identical to one of the args. */
303 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
4b027d16 304 return build_type_attribute_variant (t1, attributes);
400fbf9f 305 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
4b027d16 306 return build_type_attribute_variant (t2, attributes);
400fbf9f 307 /* Merge the element types, and have a size if either arg has one. */
4b027d16
RK
308 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
309 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
310 }
311
312 case FUNCTION_TYPE:
313 /* Function types: prefer the one that specified arg types.
314 If both do, merge the arg types. Also merge the return types. */
315 {
316 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
317 tree p1 = TYPE_ARG_TYPES (t1);
318 tree p2 = TYPE_ARG_TYPES (t2);
319 int len;
320 tree newargs, n;
321 int i;
322
323 /* Save space: see if the result is identical to one of the args. */
324 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
4b027d16 325 return build_type_attribute_variant (t1, attributes);
400fbf9f 326 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
4b027d16 327 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
328
329 /* Simple way if one arg fails to specify argument types. */
330 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16
RK
331 {
332 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
333 return build_type_attribute_variant (t1, attributes);
334 }
400fbf9f 335 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
336 {
337 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
338 return build_type_attribute_variant (t1, attributes);
339 }
400fbf9f
JW
340
341 /* If both args specify argument types, we must merge the two
342 lists, argument by argument. */
343
344 len = list_length (p1);
345 newargs = 0;
346
347 for (i = 0; i < len; i++)
8d9bfdc5 348 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
349
350 n = newargs;
351
352 for (; p1;
353 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
354 {
355 /* A null type means arg type is not specified.
356 Take whatever the other function type has. */
357 if (TREE_VALUE (p1) == 0)
358 {
359 TREE_VALUE (n) = TREE_VALUE (p2);
360 goto parm_done;
361 }
362 if (TREE_VALUE (p2) == 0)
363 {
364 TREE_VALUE (n) = TREE_VALUE (p1);
365 goto parm_done;
366 }
367
368 /* Given wait (union {union wait *u; int *i} *)
369 and wait (union wait *),
370 prefer union wait * as type of parm. */
371 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
372 && TREE_VALUE (p1) != TREE_VALUE (p2))
373 {
374 tree memb;
375 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
376 memb; memb = TREE_CHAIN (memb))
377 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
378 {
379 TREE_VALUE (n) = TREE_VALUE (p2);
380 if (pedantic)
381 pedwarn ("function types not truly compatible in ANSI C");
382 goto parm_done;
383 }
384 }
385 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
386 && TREE_VALUE (p2) != TREE_VALUE (p1))
387 {
388 tree memb;
389 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
390 memb; memb = TREE_CHAIN (memb))
391 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
392 {
393 TREE_VALUE (n) = TREE_VALUE (p1);
394 if (pedantic)
395 pedwarn ("function types not truly compatible in ANSI C");
396 goto parm_done;
397 }
398 }
399 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
400 parm_done: ;
401 }
402
4b027d16 403 t1 = build_function_type (valtype, newargs);
0f41302f 404 /* ... falls through ... */
400fbf9f
JW
405 }
406
407 default:
4b027d16 408 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
409 }
410
411}
412\f
413/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
414 or various other operations. Return 2 if they are compatible
415 but a warning may be needed if you use them together. */
416
417int
418comptypes (type1, type2)
419 tree type1, type2;
420{
421 register tree t1 = type1;
422 register tree t2 = type2;
4b027d16 423 int attrval, val;
400fbf9f
JW
424
425 /* Suppress errors caused by previously reported errors. */
426
8d47dfc5
RH
427 if (t1 == t2 || !t1 || !t2
428 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
429 return 1;
430
b8c21346
RK
431 /* Treat an enum type as the integer type of the same width and
432 signedness. */
400fbf9f
JW
433
434 if (TREE_CODE (t1) == ENUMERAL_TYPE)
b8c21346 435 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
400fbf9f 436 if (TREE_CODE (t2) == ENUMERAL_TYPE)
b8c21346 437 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
400fbf9f
JW
438
439 if (t1 == t2)
440 return 1;
441
442 /* Different classes of types can't be compatible. */
443
444 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
445
446 /* Qualifiers must match. */
447
3932261a 448 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
449 return 0;
450
08632da2
RS
451 /* Allow for two different type nodes which have essentially the same
452 definition. Note that we already checked for equality of the type
38e01259 453 qualifiers (just above). */
400fbf9f
JW
454
455 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
456 return 1;
457
4b027d16
RK
458#ifndef COMP_TYPE_ATTRIBUTES
459#define COMP_TYPE_ATTRIBUTES(t1,t2) 1
460#endif
461
462 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
463 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
464 return 0;
465
466 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
467 val = 0;
468
400fbf9f
JW
469 switch (TREE_CODE (t1))
470 {
471 case POINTER_TYPE:
4b027d16 472 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
400fbf9f 473 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
4b027d16 474 break;
400fbf9f
JW
475
476 case FUNCTION_TYPE:
4b027d16
RK
477 val = function_types_compatible_p (t1, t2);
478 break;
400fbf9f
JW
479
480 case ARRAY_TYPE:
481 {
400fbf9f
JW
482 tree d1 = TYPE_DOMAIN (t1);
483 tree d2 = TYPE_DOMAIN (t2);
4b027d16 484 val = 1;
400fbf9f
JW
485
486 /* Target types must match incl. qualifiers. */
487 if (TREE_TYPE (t1) != TREE_TYPE (t2)
488 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
489 return 0;
490
491 /* Sizes must match unless one is missing or variable. */
492 if (d1 == 0 || d2 == 0 || d1 == d2
493 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
494 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
495 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
496 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
4b027d16 497 break;
400fbf9f 498
4b027d16 499 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
400fbf9f
JW
500 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
501 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
502 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
503 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
504 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
505 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
4b027d16
RK
506 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
507 val = 0;
508 break;
400fbf9f
JW
509 }
510
511 case RECORD_TYPE:
392202b0 512 if (maybe_objc_comptypes (t1, t2, 0) == 1)
4b027d16
RK
513 val = 1;
514 break;
e9a25f70
JL
515
516 default:
517 break;
400fbf9f 518 }
4b027d16 519 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
520}
521
522/* Return 1 if TTL and TTR are pointers to types that are equivalent,
523 ignoring their qualifiers. */
524
525static int
526comp_target_types (ttl, ttr)
527 tree ttl, ttr;
528{
392202b0 529 int val;
8b40563c 530
392202b0 531 /* Give maybe_objc_comptypes a crack at letting these types through. */
1d300e19 532 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
392202b0 533 return val;
8b40563c 534
392202b0
TW
535 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
536 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
8b40563c 537
400fbf9f
JW
538 if (val == 2 && pedantic)
539 pedwarn ("types are not quite compatible");
540 return val;
541}
542\f
543/* Subroutines of `comptypes'. */
544
545/* Return 1 if two function types F1 and F2 are compatible.
546 If either type specifies no argument types,
547 the other must specify a fixed number of self-promoting arg types.
548 Otherwise, if one type specifies only the number of arguments,
549 the other must specify that number of self-promoting arg types.
550 Otherwise, the argument types must match. */
551
552static int
553function_types_compatible_p (f1, f2)
554 tree f1, f2;
555{
556 tree args1, args2;
557 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
558 int val = 1;
559 int val1;
560
561 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
562 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
563 return 0;
564
565 args1 = TYPE_ARG_TYPES (f1);
566 args2 = TYPE_ARG_TYPES (f2);
567
568 /* An unspecified parmlist matches any specified parmlist
569 whose argument types don't need default promotions. */
570
571 if (args1 == 0)
572 {
573 if (!self_promoting_args_p (args2))
574 return 0;
575 /* If one of these types comes from a non-prototype fn definition,
576 compare that with the other type's arglist.
577 If they don't match, ask for a warning (but no error). */
578 if (TYPE_ACTUAL_ARG_TYPES (f1)
579 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
580 val = 2;
581 return val;
582 }
583 if (args2 == 0)
584 {
585 if (!self_promoting_args_p (args1))
586 return 0;
587 if (TYPE_ACTUAL_ARG_TYPES (f2)
588 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
589 val = 2;
590 return val;
591 }
592
593 /* Both types have argument lists: compare them and propagate results. */
594 val1 = type_lists_compatible_p (args1, args2);
595 return val1 != 1 ? val1 : val;
596}
597
598/* Check two lists of types for compatibility,
599 returning 0 for incompatible, 1 for compatible,
600 or 2 for compatible with warning. */
601
602static int
603type_lists_compatible_p (args1, args2)
604 tree args1, args2;
605{
606 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
607 int val = 1;
9d5f3e49 608 int newval = 0;
400fbf9f
JW
609
610 while (1)
611 {
612 if (args1 == 0 && args2 == 0)
613 return val;
614 /* If one list is shorter than the other,
615 they fail to match. */
616 if (args1 == 0 || args2 == 0)
617 return 0;
618 /* A null pointer instead of a type
619 means there is supposed to be an argument
620 but nothing is specified about what type it has.
621 So match anything that self-promotes. */
622 if (TREE_VALUE (args1) == 0)
623 {
624 if (! self_promoting_type_p (TREE_VALUE (args2)))
625 return 0;
626 }
627 else if (TREE_VALUE (args2) == 0)
628 {
629 if (! self_promoting_type_p (TREE_VALUE (args1)))
630 return 0;
631 }
632 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
633 {
634 /* Allow wait (union {union wait *u; int *i} *)
635 and wait (union wait *) to be compatible. */
636 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
ea3373cd
RK
637 && (TYPE_NAME (TREE_VALUE (args1)) == 0
638 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
400fbf9f
JW
639 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
640 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
641 TYPE_SIZE (TREE_VALUE (args2))))
642 {
643 tree memb;
644 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
645 memb; memb = TREE_CHAIN (memb))
646 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
647 break;
648 if (memb == 0)
649 return 0;
650 }
651 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
ea3373cd
RK
652 && (TYPE_NAME (TREE_VALUE (args2)) == 0
653 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
400fbf9f
JW
654 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
655 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
656 TYPE_SIZE (TREE_VALUE (args1))))
657 {
658 tree memb;
659 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
660 memb; memb = TREE_CHAIN (memb))
661 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
662 break;
663 if (memb == 0)
664 return 0;
665 }
666 else
667 return 0;
668 }
669
670 /* comptypes said ok, but record if it said to warn. */
671 if (newval > val)
672 val = newval;
673
674 args1 = TREE_CHAIN (args1);
675 args2 = TREE_CHAIN (args2);
676 }
677}
678
679/* Return 1 if PARMS specifies a fixed number of parameters
680 and none of their types is affected by default promotions. */
681
805f961c 682int
400fbf9f
JW
683self_promoting_args_p (parms)
684 tree parms;
685{
686 register tree t;
687 for (t = parms; t; t = TREE_CHAIN (t))
688 {
689 register tree type = TREE_VALUE (t);
690
691 if (TREE_CHAIN (t) == 0 && type != void_type_node)
692 return 0;
693
d627ed1b
RS
694 if (type == 0)
695 return 0;
696
6cb72a7d 697 if (TYPE_MAIN_VARIANT (type) == float_type_node)
400fbf9f
JW
698 return 0;
699
d627ed1b 700 if (C_PROMOTING_INTEGER_TYPE_P (type))
400fbf9f
JW
701 return 0;
702 }
703 return 1;
704}
705
706/* Return 1 if TYPE is not affected by default promotions. */
707
708static int
709self_promoting_type_p (type)
710 tree type;
711{
6cb72a7d 712 if (TYPE_MAIN_VARIANT (type) == float_type_node)
400fbf9f
JW
713 return 0;
714
d627ed1b 715 if (C_PROMOTING_INTEGER_TYPE_P (type))
400fbf9f
JW
716 return 0;
717
718 return 1;
719}
720\f
721/* Return an unsigned type the same as TYPE in other respects. */
722
723tree
724unsigned_type (type)
725 tree type;
726{
6cb72a7d
RS
727 tree type1 = TYPE_MAIN_VARIANT (type);
728 if (type1 == signed_char_type_node || type1 == char_type_node)
400fbf9f 729 return unsigned_char_type_node;
6cb72a7d 730 if (type1 == integer_type_node)
400fbf9f 731 return unsigned_type_node;
6cb72a7d 732 if (type1 == short_integer_type_node)
400fbf9f 733 return short_unsigned_type_node;
6cb72a7d 734 if (type1 == long_integer_type_node)
400fbf9f 735 return long_unsigned_type_node;
6cb72a7d 736 if (type1 == long_long_integer_type_node)
400fbf9f 737 return long_long_unsigned_type_node;
fb695d4a
RK
738 if (type1 == intDI_type_node)
739 return unsigned_intDI_type_node;
740 if (type1 == intSI_type_node)
741 return unsigned_intSI_type_node;
742 if (type1 == intHI_type_node)
743 return unsigned_intHI_type_node;
744 if (type1 == intQI_type_node)
745 return unsigned_intQI_type_node;
f63f0752
RK
746
747 return signed_or_unsigned_type (1, type);
400fbf9f
JW
748}
749
750/* Return a signed type the same as TYPE in other respects. */
751
752tree
753signed_type (type)
754 tree type;
755{
6cb72a7d
RS
756 tree type1 = TYPE_MAIN_VARIANT (type);
757 if (type1 == unsigned_char_type_node || type1 == char_type_node)
400fbf9f 758 return signed_char_type_node;
6cb72a7d 759 if (type1 == unsigned_type_node)
400fbf9f 760 return integer_type_node;
6cb72a7d 761 if (type1 == short_unsigned_type_node)
400fbf9f 762 return short_integer_type_node;
6cb72a7d 763 if (type1 == long_unsigned_type_node)
400fbf9f 764 return long_integer_type_node;
6cb72a7d 765 if (type1 == long_long_unsigned_type_node)
400fbf9f 766 return long_long_integer_type_node;
fb695d4a
RK
767 if (type1 == unsigned_intDI_type_node)
768 return intDI_type_node;
769 if (type1 == unsigned_intSI_type_node)
770 return intSI_type_node;
771 if (type1 == unsigned_intHI_type_node)
772 return intHI_type_node;
773 if (type1 == unsigned_intQI_type_node)
774 return intQI_type_node;
f63f0752
RK
775
776 return signed_or_unsigned_type (0, type);
400fbf9f
JW
777}
778
779/* Return a type the same as TYPE except unsigned or
780 signed according to UNSIGNEDP. */
781
782tree
783signed_or_unsigned_type (unsignedp, type)
784 int unsignedp;
785 tree type;
786{
e5e809f4 787 if ((! INTEGRAL_TYPE_P (type) && ! POINTER_TYPE_P (type))
3c967567 788 || TREE_UNSIGNED (type) == unsignedp)
400fbf9f
JW
789 return type;
790 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
791 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
792 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
793 return unsignedp ? unsigned_type_node : integer_type_node;
794 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
795 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
796 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
797 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
798 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
799 return (unsignedp ? long_long_unsigned_type_node
800 : long_long_integer_type_node);
801 return type;
802}
803
804/* Compute the value of the `sizeof' operator. */
805
806tree
807c_sizeof (type)
808 tree type;
809{
810 enum tree_code code = TREE_CODE (type);
f7c8fb3f 811 tree t;
400fbf9f
JW
812
813 if (code == FUNCTION_TYPE)
814 {
815 if (pedantic || warn_pointer_arith)
816 pedwarn ("sizeof applied to a function type");
817 return size_int (1);
818 }
819 if (code == VOID_TYPE)
820 {
821 if (pedantic || warn_pointer_arith)
822 pedwarn ("sizeof applied to a void type");
823 return size_int (1);
824 }
825 if (code == ERROR_MARK)
826 return size_int (1);
827 if (TYPE_SIZE (type) == 0)
828 {
829 error ("sizeof applied to an incomplete type");
830 return size_int (0);
831 }
832
833 /* Convert in case a char is more than one unit. */
f7c8fb3f
RS
834 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
835 size_int (TYPE_PRECISION (char_type_node)));
f8dac6eb 836 t = convert (sizetype, t);
fa427131 837 /* size_binop does not put the constant in range, so do it now. */
10d5caec
PE
838 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
839 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
f7c8fb3f 840 return t;
400fbf9f
JW
841}
842
843tree
844c_sizeof_nowarn (type)
845 tree type;
846{
847 enum tree_code code = TREE_CODE (type);
f7c8fb3f 848 tree t;
400fbf9f
JW
849
850 if (code == FUNCTION_TYPE
851 || code == VOID_TYPE
852 || code == ERROR_MARK)
853 return size_int (1);
854 if (TYPE_SIZE (type) == 0)
855 return size_int (0);
856
857 /* Convert in case a char is more than one unit. */
f7c8fb3f
RS
858 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
859 size_int (TYPE_PRECISION (char_type_node)));
f8dac6eb 860 t = convert (sizetype, t);
e58cd767 861 force_fit_type (t, 0);
f7c8fb3f 862 return t;
400fbf9f
JW
863}
864
865/* Compute the size to increment a pointer by. */
866
867tree
868c_size_in_bytes (type)
869 tree type;
870{
871 enum tree_code code = TREE_CODE (type);
f7c8fb3f 872 tree t;
400fbf9f
JW
873
874 if (code == FUNCTION_TYPE)
875 return size_int (1);
876 if (code == VOID_TYPE)
877 return size_int (1);
878 if (code == ERROR_MARK)
879 return size_int (1);
880 if (TYPE_SIZE (type) == 0)
881 {
882 error ("arithmetic on pointer to an incomplete type");
883 return size_int (1);
884 }
885
886 /* Convert in case a char is more than one unit. */
f7c8fb3f 887 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
400fbf9f 888 size_int (BITS_PER_UNIT));
f8dac6eb 889 t = convert (sizetype, t);
e58cd767 890 force_fit_type (t, 0);
f7c8fb3f 891 return t;
400fbf9f
JW
892}
893
894/* Implement the __alignof keyword: Return the minimum required
895 alignment of TYPE, measured in bytes. */
896
897tree
898c_alignof (type)
899 tree type;
900{
901 enum tree_code code = TREE_CODE (type);
902
903 if (code == FUNCTION_TYPE)
904 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
905
906 if (code == VOID_TYPE || code == ERROR_MARK)
907 return size_int (1);
908
909 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
910}
911\f
912/* Implement the __alignof keyword: Return the minimum required
913 alignment of EXPR, measured in bytes. For VAR_DECL's and
914 FIELD_DECL's return DECL_ALIGN (which can be set from an
915 "aligned" __attribute__ specification). */
9e9bd45d 916
400fbf9f
JW
917tree
918c_alignof_expr (expr)
919 tree expr;
920{
921 if (TREE_CODE (expr) == VAR_DECL)
922 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
923
924 if (TREE_CODE (expr) == COMPONENT_REF
ef86d2a6 925 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
400fbf9f
JW
926 {
927 error ("`__alignof' applied to a bit-field");
928 return size_int (1);
929 }
930 else if (TREE_CODE (expr) == COMPONENT_REF
931 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
932 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
933
934 if (TREE_CODE (expr) == INDIRECT_REF)
935 {
936 tree t = TREE_OPERAND (expr, 0);
937 tree best = t;
938 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
939
940 while (TREE_CODE (t) == NOP_EXPR
941 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
942 {
943 int thisalign;
944
945 t = TREE_OPERAND (t, 0);
946 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
947 if (thisalign > bestalign)
948 best = t, bestalign = thisalign;
949 }
950 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
951 }
952 else
953 return c_alignof (TREE_TYPE (expr));
954}
a7c1916a 955
400fbf9f
JW
956/* Return either DECL or its known constant value (if it has one). */
957
958static tree
959decl_constant_value (decl)
960 tree decl;
961{
a7c1916a 962 if (/* Don't change a variable array bound or initial value to a constant
400fbf9f 963 in a place where a variable is invalid. */
a7c1916a 964 current_function_decl != 0
400fbf9f
JW
965 && ! pedantic
966 && ! TREE_THIS_VOLATILE (decl)
8c3a6477 967 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
400fbf9f
JW
968 && DECL_INITIAL (decl) != 0
969 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
970 /* This is invalid if initial value is not constant.
971 If it has either a function call, a memory reference,
972 or a variable, then re-evaluating it could give different results. */
973 && TREE_CONSTANT (DECL_INITIAL (decl))
974 /* Check for cases where this is sub-optimal, even though valid. */
975 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
976 && DECL_MODE (decl) != BLKmode)
977 return DECL_INITIAL (decl);
978 return decl;
979}
980
981/* Perform default promotions for C data used in expressions.
982 Arrays and functions are converted to pointers;
983 enumeral types or short or char, to int.
984 In addition, manifest constants symbols are replaced by their values. */
985
986tree
987default_conversion (exp)
988 tree exp;
989{
990 register tree type = TREE_TYPE (exp);
991 register enum tree_code code = TREE_CODE (type);
992
993 /* Constants can be used directly unless they're not loadable. */
994 if (TREE_CODE (exp) == CONST_DECL)
995 exp = DECL_INITIAL (exp);
d4424a75
RK
996
997 /* Replace a nonvolatile const static variable with its value unless
998 it is an array, in which case we must be sure that taking the
999 address of the array produces consistent results. */
1000 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
400fbf9f
JW
1001 {
1002 exp = decl_constant_value (exp);
1003 type = TREE_TYPE (exp);
1004 }
1005
a7d53fce
RS
1006 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1007 an lvalue. */
1008 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
1009 to integer and cause infinite recursion. */
1010 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1011 || (TREE_CODE (exp) == NOP_EXPR
1012 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1013 exp = TREE_OPERAND (exp, 0);
400fbf9f
JW
1014
1015 /* Normally convert enums to int,
1016 but convert wide enums to something wider. */
1017 if (code == ENUMERAL_TYPE)
1018 {
1019 type = type_for_size (MAX (TYPE_PRECISION (type),
1020 TYPE_PRECISION (integer_type_node)),
86463d5d 1021 ((flag_traditional
e9a25f70
JL
1022 || (TYPE_PRECISION (type)
1023 >= TYPE_PRECISION (integer_type_node)))
86463d5d 1024 && TREE_UNSIGNED (type)));
400fbf9f
JW
1025 return convert (type, exp);
1026 }
1027
9753f113 1028 if (TREE_CODE (exp) == COMPONENT_REF
e9a25f70 1029 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
9753f113 1030 {
cff9c407
RK
1031 tree width = DECL_SIZE (TREE_OPERAND (exp, 1));
1032 HOST_WIDE_INT low = TREE_INT_CST_LOW (width);
9753f113 1033
cff9c407
RK
1034 /* If it's thinner than an int, promote it like a
1035 C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */
9753f113 1036
cff9c407
RK
1037 if (low < TYPE_PRECISION (integer_type_node))
1038 {
1039 if (flag_traditional && TREE_UNSIGNED (type))
1040 return convert (unsigned_type_node, exp);
1041 else
1042 return convert (integer_type_node, exp);
1043 }
9753f113
RK
1044 }
1045
d627ed1b 1046 if (C_PROMOTING_INTEGER_TYPE_P (type))
400fbf9f 1047 {
e83d45c4
RS
1048 /* Traditionally, unsignedness is preserved in default promotions.
1049 Also preserve unsignedness if not really getting any wider. */
1050 if (TREE_UNSIGNED (type)
1051 && (flag_traditional
1052 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
400fbf9f
JW
1053 return convert (unsigned_type_node, exp);
1054 return convert (integer_type_node, exp);
1055 }
19d76e60
RK
1056 if (flag_traditional && !flag_allow_single_precision
1057 && TYPE_MAIN_VARIANT (type) == float_type_node)
400fbf9f
JW
1058 return convert (double_type_node, exp);
1059 if (code == VOID_TYPE)
1060 {
1061 error ("void value not ignored as it ought to be");
1062 return error_mark_node;
1063 }
1064 if (code == FUNCTION_TYPE)
1065 {
1066 return build_unary_op (ADDR_EXPR, exp, 0);
1067 }
1068 if (code == ARRAY_TYPE)
1069 {
1070 register tree adr;
1071 tree restype = TREE_TYPE (type);
1072 tree ptrtype;
d11fdb45
RS
1073 int constp = 0;
1074 int volatilep = 0;
1075
1076 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1077 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1078 {
1079 constp = TREE_READONLY (exp);
1080 volatilep = TREE_THIS_VOLATILE (exp);
1081 }
1082
3932261a
MM
1083 if (TYPE_QUALS (type) || constp || volatilep)
1084 restype
1085 = c_build_qualified_type (restype,
1086 TYPE_QUALS (type)
1087 | (constp * TYPE_QUAL_CONST)
1088 | (volatilep * TYPE_QUAL_VOLATILE));
400fbf9f
JW
1089
1090 if (TREE_CODE (exp) == INDIRECT_REF)
1091 return convert (TYPE_POINTER_TO (restype),
1092 TREE_OPERAND (exp, 0));
1093
1094 if (TREE_CODE (exp) == COMPOUND_EXPR)
1095 {
1096 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1097 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1098 TREE_OPERAND (exp, 0), op1);
1099 }
1100
cff9c407 1101 if (! lvalue_p (exp)
400fbf9f
JW
1102 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1103 {
8efabd13
RS
1104 error ("invalid use of non-lvalue array");
1105 return error_mark_node;
400fbf9f
JW
1106 }
1107
400fbf9f
JW
1108 ptrtype = build_pointer_type (restype);
1109
1110 if (TREE_CODE (exp) == VAR_DECL)
1111 {
1112 /* ??? This is not really quite correct
1113 in that the type of the operand of ADDR_EXPR
1114 is not the target type of the type of the ADDR_EXPR itself.
1115 Question is, can this lossage be avoided? */
1116 adr = build1 (ADDR_EXPR, ptrtype, exp);
1117 if (mark_addressable (exp) == 0)
1118 return error_mark_node;
1119 TREE_CONSTANT (adr) = staticp (exp);
1120 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1121 return adr;
1122 }
1123 /* This way is better for a COMPONENT_REF since it can
1124 simplify the offset for a component. */
1125 adr = build_unary_op (ADDR_EXPR, exp, 1);
1126 return convert (ptrtype, adr);
1127 }
1128 return exp;
1129}
1130\f
19d76e60
RK
1131/* Look up component name in the structure type definition.
1132
1133 If this component name is found indirectly within an anonymous union,
1134 store in *INDIRECT the component which directly contains
1135 that anonymous union. Otherwise, set *INDIRECT to 0. */
2f2d13da
DE
1136
1137static tree
19d76e60 1138lookup_field (type, component, indirect)
2f2d13da 1139 tree type, component;
19d76e60 1140 tree *indirect;
2f2d13da
DE
1141{
1142 tree field;
1143
1144 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1145 to the field elements. Use a binary search on this array to quickly
1146 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1147 will always be set for structures which have many elements. */
1148
1149 if (TYPE_LANG_SPECIFIC (type))
1150 {
1151 int bot, top, half;
1152 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1153
1154 field = TYPE_FIELDS (type);
1155 bot = 0;
1156 top = TYPE_LANG_SPECIFIC (type)->len;
1157 while (top - bot > 1)
1158 {
2f2d13da
DE
1159 half = (top - bot + 1) >> 1;
1160 field = field_array[bot+half];
1161
1162 if (DECL_NAME (field) == NULL_TREE)
1163 {
1164 /* Step through all anon unions in linear fashion. */
1165 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1166 {
a68b98cf 1167 tree anon = 0, junk;
19d76e60 1168
2f2d13da 1169 field = field_array[bot++];
a68b98cf
RK
1170 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1171 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1172 anon = lookup_field (TREE_TYPE (field), component, &junk);
1173
2f2d13da 1174 if (anon != NULL_TREE)
19d76e60
RK
1175 {
1176 *indirect = field;
1177 return anon;
1178 }
2f2d13da
DE
1179 }
1180
1181 /* Entire record is only anon unions. */
1182 if (bot > top)
1183 return NULL_TREE;
1184
1185 /* Restart the binary search, with new lower bound. */
1186 continue;
1187 }
1188
e8b87aac 1189 if (DECL_NAME (field) == component)
2f2d13da 1190 break;
e8b87aac 1191 if (DECL_NAME (field) < component)
2f2d13da
DE
1192 bot += half;
1193 else
1194 top = bot + half;
1195 }
1196
1197 if (DECL_NAME (field_array[bot]) == component)
1198 field = field_array[bot];
1199 else if (DECL_NAME (field) != component)
1200 field = 0;
1201 }
1202 else
1203 {
1204 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1205 {
1206 if (DECL_NAME (field) == NULL_TREE)
1207 {
19d76e60 1208 tree junk;
a68b98cf
RK
1209 tree anon = 0;
1210
1211 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1212 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1213 anon = lookup_field (TREE_TYPE (field), component, &junk);
1214
2f2d13da 1215 if (anon != NULL_TREE)
19d76e60
RK
1216 {
1217 *indirect = field;
1218 return anon;
1219 }
2f2d13da
DE
1220 }
1221
1222 if (DECL_NAME (field) == component)
1223 break;
1224 }
1225 }
1226
19d76e60 1227 *indirect = NULL_TREE;
2f2d13da
DE
1228 return field;
1229}
1230
400fbf9f
JW
1231/* Make an expression to refer to the COMPONENT field of
1232 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1233
1234tree
1235build_component_ref (datum, component)
1236 tree datum, component;
1237{
1238 register tree type = TREE_TYPE (datum);
1239 register enum tree_code code = TREE_CODE (type);
1240 register tree field = NULL;
1241 register tree ref;
1242
1243 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1244 unless we are not to support things not strictly ANSI. */
1245 switch (TREE_CODE (datum))
1246 {
1247 case COMPOUND_EXPR:
1248 {
1249 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
400fbf9f
JW
1250 return build (COMPOUND_EXPR, TREE_TYPE (value),
1251 TREE_OPERAND (datum, 0), value);
1252 }
1253 case COND_EXPR:
400fbf9f
JW
1254 return build_conditional_expr
1255 (TREE_OPERAND (datum, 0),
1256 build_component_ref (TREE_OPERAND (datum, 1), component),
1257 build_component_ref (TREE_OPERAND (datum, 2), component));
e9a25f70
JL
1258
1259 default:
1260 break;
400fbf9f
JW
1261 }
1262
1263 /* See if there is a field or component with name COMPONENT. */
1264
1265 if (code == RECORD_TYPE || code == UNION_TYPE)
1266 {
19d76e60
RK
1267 tree indirect = 0;
1268
400fbf9f
JW
1269 if (TYPE_SIZE (type) == 0)
1270 {
8d9bfdc5 1271 incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
1272 return error_mark_node;
1273 }
1274
19d76e60 1275 field = lookup_field (type, component, &indirect);
400fbf9f
JW
1276
1277 if (!field)
1278 {
1279 error (code == RECORD_TYPE
1280 ? "structure has no member named `%s'"
1281 : "union has no member named `%s'",
1282 IDENTIFIER_POINTER (component));
1283 return error_mark_node;
1284 }
1285 if (TREE_TYPE (field) == error_mark_node)
1286 return error_mark_node;
1287
19d76e60
RK
1288 /* If FIELD was found buried within an anonymous union,
1289 make one COMPONENT_REF to get that anonymous union,
1290 then fall thru to make a second COMPONENT_REF to get FIELD. */
1291 if (indirect != 0)
1292 {
1293 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1294 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1295 TREE_READONLY (ref) = 1;
1296 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1297 TREE_THIS_VOLATILE (ref) = 1;
1298 datum = ref;
1299 }
1300
400fbf9f
JW
1301 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1302
1303 if (TREE_READONLY (datum) || TREE_READONLY (field))
1304 TREE_READONLY (ref) = 1;
1305 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1306 TREE_THIS_VOLATILE (ref) = 1;
1307
1308 return ref;
1309 }
1310 else if (code != ERROR_MARK)
1311 error ("request for member `%s' in something not a structure or union",
1312 IDENTIFIER_POINTER (component));
1313
1314 return error_mark_node;
1315}
1316\f
1317/* Given an expression PTR for a pointer, return an expression
1318 for the value pointed to.
1319 ERRORSTRING is the name of the operator to appear in error messages. */
1320
1321tree
1322build_indirect_ref (ptr, errorstring)
1323 tree ptr;
1324 char *errorstring;
1325{
1326 register tree pointer = default_conversion (ptr);
1327 register tree type = TREE_TYPE (pointer);
1328
1329 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b
RS
1330 {
1331 if (TREE_CODE (pointer) == ADDR_EXPR
1332 && !flag_volatile
1333 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1334 == TREE_TYPE (type)))
1335 return TREE_OPERAND (pointer, 0);
1336 else
1337 {
1338 tree t = TREE_TYPE (type);
1339 register tree ref = build1 (INDIRECT_REF,
1340 TYPE_MAIN_VARIANT (t), pointer);
400fbf9f 1341
870cc33b
RS
1342 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1343 {
1344 error ("dereferencing pointer to incomplete type");
1345 return error_mark_node;
1346 }
bd5b5c85 1347 if (TREE_CODE (t) == VOID_TYPE && skip_evaluation == 0)
870cc33b
RS
1348 warning ("dereferencing `void *' pointer");
1349
1350 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1351 so that we get the proper error message if the result is used
1352 to assign to. Also, &* is supposed to be a no-op.
1353 And ANSI C seems to specify that the type of the result
1354 should be the const type. */
1355 /* A de-reference of a pointer to const is not a const. It is valid
1356 to change it via some other pointer. */
1357 TREE_READONLY (ref) = TYPE_READONLY (t);
1358 TREE_SIDE_EFFECTS (ref)
1359 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
493692cd 1360 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
870cc33b
RS
1361 return ref;
1362 }
1363 }
400fbf9f
JW
1364 else if (TREE_CODE (pointer) != ERROR_MARK)
1365 error ("invalid type argument of `%s'", errorstring);
1366 return error_mark_node;
1367}
1368
1369/* This handles expressions of the form "a[i]", which denotes
1370 an array reference.
1371
1372 This is logically equivalent in C to *(a+i), but we may do it differently.
1373 If A is a variable or a member, we generate a primitive ARRAY_REF.
1374 This avoids forcing the array out of registers, and can work on
1375 arrays that are not lvalues (for example, members of structures returned
1376 by functions). */
1377
1378tree
1379build_array_ref (array, index)
1380 tree array, index;
1381{
1382 if (index == 0)
1383 {
1384 error ("subscript missing in array reference");
1385 return error_mark_node;
1386 }
1387
1388 if (TREE_TYPE (array) == error_mark_node
1389 || TREE_TYPE (index) == error_mark_node)
1390 return error_mark_node;
1391
1392 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1393 && TREE_CODE (array) != INDIRECT_REF)
1394 {
1395 tree rval, type;
1396
400fbf9f
JW
1397 /* Subscripting with type char is likely to lose
1398 on a machine where chars are signed.
1399 So warn on any machine, but optionally.
1400 Don't warn for unsigned char since that type is safe.
1401 Don't warn for signed char because anyone who uses that
1402 must have done so deliberately. */
1403 if (warn_char_subscripts
1404 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1405 warning ("array subscript has type `char'");
1406
0e51ef9b
RS
1407 /* Apply default promotions *after* noticing character types. */
1408 index = default_conversion (index);
1409
fdeefd49
RS
1410 /* Require integer *after* promotion, for sake of enums. */
1411 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1412 {
1413 error ("array subscript is not an integer");
1414 return error_mark_node;
1415 }
1416
400fbf9f
JW
1417 /* An array that is indexed by a non-constant
1418 cannot be stored in a register; we must be able to do
1419 address arithmetic on its address.
1420 Likewise an array of elements of variable size. */
1421 if (TREE_CODE (index) != INTEGER_CST
1422 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1423 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1424 {
1425 if (mark_addressable (array) == 0)
1426 return error_mark_node;
1427 }
e6d52559
JW
1428 /* An array that is indexed by a constant value which is not within
1429 the array bounds cannot be stored in a register either; because we
1430 would get a crash in store_bit_field/extract_bit_field when trying
1431 to access a non-existent part of the register. */
1432 if (TREE_CODE (index) == INTEGER_CST
1433 && TYPE_VALUES (TREE_TYPE (array))
1434 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1435 {
1436 if (mark_addressable (array) == 0)
1437 return error_mark_node;
1438 }
400fbf9f
JW
1439
1440 if (pedantic && !lvalue_p (array))
1441 {
1394aabd 1442 if (DECL_REGISTER (array))
400fbf9f
JW
1443 pedwarn ("ANSI C forbids subscripting `register' array");
1444 else
1445 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1446 }
1447
1448 if (pedantic)
1449 {
1450 tree foo = array;
1451 while (TREE_CODE (foo) == COMPONENT_REF)
1452 foo = TREE_OPERAND (foo, 0);
1394aabd 1453 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
400fbf9f
JW
1454 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1455 }
1456
1457 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1458 rval = build (ARRAY_REF, type, array, index);
1459 /* Array ref is const/volatile if the array elements are
1460 or if the array is. */
1461 TREE_READONLY (rval)
1462 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1463 | TREE_READONLY (array));
1464 TREE_SIDE_EFFECTS (rval)
1465 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1466 | TREE_SIDE_EFFECTS (array));
1467 TREE_THIS_VOLATILE (rval)
1468 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1469 /* This was added by rms on 16 Nov 91.
1470 It fixes vol struct foo *a; a->elts[1]
1471 in an inline function.
1472 Hope it doesn't break something else. */
1473 | TREE_THIS_VOLATILE (array));
1474 return require_complete_type (fold (rval));
1475 }
1476
1477 {
1478 tree ar = default_conversion (array);
1479 tree ind = default_conversion (index);
1480
aed11452
RK
1481 /* Do the same warning check as above, but only on the part that's
1482 syntactically the index and only if it is also semantically
1483 the index. */
1484 if (warn_char_subscripts
1485 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1486 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1487 warning ("subscript has type `char'");
1488
400fbf9f
JW
1489 /* Put the integer in IND to simplify error checking. */
1490 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1491 {
1492 tree temp = ar;
1493 ar = ind;
1494 ind = temp;
1495 }
1496
1497 if (ar == error_mark_node)
1498 return ar;
1499
004252d7
RK
1500 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1501 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
400fbf9f
JW
1502 {
1503 error ("subscripted value is neither array nor pointer");
1504 return error_mark_node;
1505 }
1506 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1507 {
1508 error ("array subscript is not an integer");
1509 return error_mark_node;
1510 }
1511
1512 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1513 "array indexing");
1514 }
1515}
1516\f
400fbf9f
JW
1517/* Build a function call to function FUNCTION with parameters PARAMS.
1518 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1519 TREE_VALUE of each node is a parameter-expression.
1520 FUNCTION's data type may be a function type or a pointer-to-function. */
1521
1522tree
1523build_function_call (function, params)
1524 tree function, params;
1525{
346d29dc 1526 register tree fntype, fundecl = 0;
400fbf9f 1527 register tree coerced_params;
19d76e60 1528 tree name = NULL_TREE, assembler_name = NULL_TREE;
400fbf9f 1529
fc76e425 1530 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 1531 STRIP_TYPE_NOPS (function);
400fbf9f
JW
1532
1533 /* Convert anything with function type to a pointer-to-function. */
1534 if (TREE_CODE (function) == FUNCTION_DECL)
1535 {
1536 name = DECL_NAME (function);
19d76e60
RK
1537 assembler_name = DECL_ASSEMBLER_NAME (function);
1538
400fbf9f
JW
1539 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1540 (because calling an inline function does not mean the function
1541 needs to be separately compiled). */
1542 fntype = build_type_variant (TREE_TYPE (function),
1543 TREE_READONLY (function),
1544 TREE_THIS_VOLATILE (function));
9b7267b8 1545 fundecl = function;
400fbf9f
JW
1546 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1547 }
1548 else
1549 function = default_conversion (function);
1550
1551 fntype = TREE_TYPE (function);
1552
1553 if (TREE_CODE (fntype) == ERROR_MARK)
1554 return error_mark_node;
1555
1556 if (!(TREE_CODE (fntype) == POINTER_TYPE
1557 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1558 {
1559 error ("called object is not a function");
1560 return error_mark_node;
1561 }
1562
1563 /* fntype now gets the type of function pointed to. */
1564 fntype = TREE_TYPE (fntype);
1565
1566 /* Convert the parameters to the types declared in the
1567 function prototype, or apply default promotions. */
1568
1569 coerced_params
9b7267b8 1570 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
400fbf9f
JW
1571
1572 /* Check for errors in format strings. */
400fbf9f 1573
19d76e60
RK
1574 if (warn_format && (name || assembler_name))
1575 check_function_format (name, assembler_name, coerced_params);
400fbf9f
JW
1576
1577 /* Recognize certain built-in functions so we can make tree-codes
1578 other than CALL_EXPR. We do this when it enables fold-const.c
1579 to do something useful. */
1580
1581 if (TREE_CODE (function) == ADDR_EXPR
1582 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1583 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1584 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1585 {
1586 case BUILT_IN_ABS:
1587 case BUILT_IN_LABS:
1588 case BUILT_IN_FABS:
1589 if (coerced_params == 0)
1590 return integer_zero_node;
1591 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
e9a25f70
JL
1592 default:
1593 break;
400fbf9f
JW
1594 }
1595
1596 {
1597 register tree result
1598 = build (CALL_EXPR, TREE_TYPE (fntype),
1599 function, coerced_params, NULL_TREE);
1600
1601 TREE_SIDE_EFFECTS (result) = 1;
1602 if (TREE_TYPE (result) == void_type_node)
1603 return result;
1604 return require_complete_type (result);
1605 }
1606}
1607\f
1608/* Convert the argument expressions in the list VALUES
1609 to the types in the list TYPELIST. The result is a list of converted
1610 argument expressions.
1611
1612 If TYPELIST is exhausted, or when an element has NULL as its type,
1613 perform the default conversions.
1614
1615 PARMLIST is the chain of parm decls for the function being called.
1616 It may be 0, if that info is not available.
1617 It is used only for generating error messages.
1618
1619 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1620
1621 This is also where warnings about wrong number of args are generated.
1622
1623 Both VALUES and the returned value are chains of TREE_LIST nodes
1624 with the elements of the list in the TREE_VALUE slots of those nodes. */
1625
1626static tree
9b7267b8
RS
1627convert_arguments (typelist, values, name, fundecl)
1628 tree typelist, values, name, fundecl;
400fbf9f
JW
1629{
1630 register tree typetail, valtail;
1631 register tree result = NULL;
1632 int parmnum;
1633
1634 /* Scan the given expressions and types, producing individual
1635 converted arguments and pushing them on RESULT in reverse order. */
1636
1637 for (valtail = values, typetail = typelist, parmnum = 0;
1638 valtail;
1639 valtail = TREE_CHAIN (valtail), parmnum++)
1640 {
1641 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1642 register tree val = TREE_VALUE (valtail);
1643
1644 if (type == void_type_node)
1645 {
1646 if (name)
1647 error ("too many arguments to function `%s'",
1648 IDENTIFIER_POINTER (name));
1649 else
1650 error ("too many arguments to function");
1651 break;
1652 }
1653
1654 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
1655 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1656 to convert automatically to a pointer. */
400fbf9f
JW
1657 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1658 val = TREE_OPERAND (val, 0);
1659
1660 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1661 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1662 val = default_conversion (val);
1663
1664 val = require_complete_type (val);
1665
1666 if (type != 0)
1667 {
1668 /* Formal parm type is specified by a function prototype. */
1669 tree parmval;
1670
1671 if (TYPE_SIZE (type) == 0)
1672 {
1673 error ("type of formal parameter %d is incomplete", parmnum + 1);
1674 parmval = val;
1675 }
1676 else
1677 {
d45cf215
RS
1678 /* Optionally warn about conversions that
1679 differ from the default conversions. */
400fbf9f
JW
1680 if (warn_conversion)
1681 {
1682 int formal_prec = TYPE_PRECISION (type);
400fbf9f 1683
aae43c5f 1684 if (INTEGRAL_TYPE_P (type)
400fbf9f 1685 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
754a4d82 1686 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
aae43c5f
RK
1687 else if (TREE_CODE (type) == COMPLEX_TYPE
1688 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1689 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
400fbf9f 1690 else if (TREE_CODE (type) == REAL_TYPE
aae43c5f 1691 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
754a4d82 1692 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
aae43c5f
RK
1693 else if (TREE_CODE (type) == REAL_TYPE
1694 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1695 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1696 /* ??? At some point, messages should be written about
1697 conversions between complex types, but that's too messy
1698 to do now. */
d45cf215
RS
1699 else if (TREE_CODE (type) == REAL_TYPE
1700 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1701 {
1702 /* Warn if any argument is passed as `float',
047de90b 1703 since without a prototype it would be `double'. */
d45cf215 1704 if (formal_prec == TYPE_PRECISION (float_type_node))
754a4d82 1705 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
d45cf215 1706 }
400fbf9f 1707 /* Detect integer changing in width or signedness. */
aae43c5f
RK
1708 else if (INTEGRAL_TYPE_P (type)
1709 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
400fbf9f 1710 {
d45cf215
RS
1711 tree would_have_been = default_conversion (val);
1712 tree type1 = TREE_TYPE (would_have_been);
1713
754a4d82
RS
1714 if (TREE_CODE (type) == ENUMERAL_TYPE
1715 && type == TREE_TYPE (val))
1716 /* No warning if function asks for enum
1717 and the actual arg is that enum type. */
1718 ;
1719 else if (formal_prec != TYPE_PRECISION (type1))
1720 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
d45cf215
RS
1721 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1722 ;
800cd3b9
RS
1723 /* Don't complain if the formal parameter type
1724 is an enum, because we can't tell now whether
1725 the value was an enum--even the same enum. */
1726 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1727 ;
400fbf9f
JW
1728 else if (TREE_CODE (val) == INTEGER_CST
1729 && int_fits_type_p (val, type))
1730 /* Change in signedness doesn't matter
1731 if a constant value is unaffected. */
1732 ;
4bbbc5d9
RS
1733 /* Likewise for a constant in a NOP_EXPR. */
1734 else if (TREE_CODE (val) == NOP_EXPR
1735 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1736 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1737 ;
1738#if 0 /* We never get such tree structure here. */
047de90b
RS
1739 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1740 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1741 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1742 /* Change in signedness doesn't matter
1743 if an enum value is unaffected. */
1744 ;
4bbbc5d9 1745#endif
ce9895ae
RS
1746 /* If the value is extended from a narrower
1747 unsigned type, it doesn't matter whether we
1748 pass it as signed or unsigned; the value
1749 certainly is the same either way. */
1750 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1751 && TREE_UNSIGNED (TREE_TYPE (val)))
1752 ;
400fbf9f 1753 else if (TREE_UNSIGNED (type))
754a4d82 1754 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
400fbf9f 1755 else
754a4d82 1756 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
400fbf9f
JW
1757 }
1758 }
1759
1760 parmval = convert_for_assignment (type, val,
0f41302f 1761 (char *) 0, /* arg passing */
9b7267b8 1762 fundecl, name, parmnum + 1);
400fbf9f
JW
1763
1764#ifdef PROMOTE_PROTOTYPES
7fb90b98
RK
1765 if ((TREE_CODE (type) == INTEGER_TYPE
1766 || TREE_CODE (type) == ENUMERAL_TYPE)
400fbf9f
JW
1767 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1768 parmval = default_conversion (parmval);
1769#endif
1770 }
8d9bfdc5 1771 result = tree_cons (NULL_TREE, parmval, result);
400fbf9f
JW
1772 }
1773 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1774 && (TYPE_PRECISION (TREE_TYPE (val))
1775 < TYPE_PRECISION (double_type_node)))
1776 /* Convert `float' to `double'. */
1777 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1778 else
1779 /* Convert `short' and `char' to full-size `int'. */
1780 result = tree_cons (NULL_TREE, default_conversion (val), result);
1781
1782 if (typetail)
1783 typetail = TREE_CHAIN (typetail);
1784 }
1785
1786 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1787 {
1788 if (name)
1789 error ("too few arguments to function `%s'",
1790 IDENTIFIER_POINTER (name));
1791 else
1792 error ("too few arguments to function");
1793 }
1794
1795 return nreverse (result);
1796}
1797\f
1798/* This is the entry point used by the parser
1799 for binary operators in the input.
1800 In addition to constructing the expression,
1801 we check for operands that were written with other binary operators
1802 in a way that is likely to confuse the user. */
edc7c4ec 1803
400fbf9f
JW
1804tree
1805parser_build_binary_op (code, arg1, arg2)
1806 enum tree_code code;
1807 tree arg1, arg2;
1808{
1809 tree result = build_binary_op (code, arg1, arg2, 1);
1810
1811 char class;
1812 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1813 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1814 enum tree_code code1 = ERROR_MARK;
1815 enum tree_code code2 = ERROR_MARK;
1816
1817 if (class1 == 'e' || class1 == '1'
1818 || class1 == '2' || class1 == '<')
1819 code1 = C_EXP_ORIGINAL_CODE (arg1);
1820 if (class2 == 'e' || class2 == '1'
1821 || class2 == '2' || class2 == '<')
1822 code2 = C_EXP_ORIGINAL_CODE (arg2);
1823
1824 /* Check for cases such as x+y<<z which users are likely
1825 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1826 is cleared to prevent these warnings. */
1827 if (warn_parentheses)
1828 {
1829 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1830 {
1831 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1832 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1833 warning ("suggest parentheses around + or - inside shift");
1834 }
1835
1836 if (code == TRUTH_ORIF_EXPR)
1837 {
1838 if (code1 == TRUTH_ANDIF_EXPR
1839 || code2 == TRUTH_ANDIF_EXPR)
1840 warning ("suggest parentheses around && within ||");
1841 }
1842
1843 if (code == BIT_IOR_EXPR)
1844 {
1845 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1846 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1847 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1848 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1849 warning ("suggest parentheses around arithmetic in operand of |");
7e9d002a
RK
1850 /* Check cases like x|y==z */
1851 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1852 warning ("suggest parentheses around comparison in operand of |");
400fbf9f
JW
1853 }
1854
1855 if (code == BIT_XOR_EXPR)
1856 {
1857 if (code1 == BIT_AND_EXPR
1858 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1859 || code2 == BIT_AND_EXPR
1860 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1861 warning ("suggest parentheses around arithmetic in operand of ^");
7e9d002a
RK
1862 /* Check cases like x^y==z */
1863 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1864 warning ("suggest parentheses around comparison in operand of ^");
400fbf9f
JW
1865 }
1866
1867 if (code == BIT_AND_EXPR)
1868 {
1869 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1870 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1871 warning ("suggest parentheses around + or - in operand of &");
7e9d002a
RK
1872 /* Check cases like x&y==z */
1873 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1874 warning ("suggest parentheses around comparison in operand of &");
400fbf9f
JW
1875 }
1876 }
1877
001af587 1878 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
edc7c4ec 1879 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
001af587
RS
1880 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1881 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1882
e58cd767
RS
1883 unsigned_conversion_warning (result, arg1);
1884 unsigned_conversion_warning (result, arg2);
1885 overflow_warning (result);
1886
edc7c4ec
RS
1887 class = TREE_CODE_CLASS (TREE_CODE (result));
1888
400fbf9f
JW
1889 /* Record the code that was specified in the source,
1890 for the sake of warnings about confusing nesting. */
1891 if (class == 'e' || class == '1'
1892 || class == '2' || class == '<')
1893 C_SET_EXP_ORIGINAL_CODE (result, code);
1894 else
1895 {
1896 int flag = TREE_CONSTANT (result);
d11fdb45
RS
1897 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1898 so that convert_for_assignment wouldn't strip it.
1899 That way, we got warnings for things like p = (1 - 1).
1900 But it turns out we should not get those warnings. */
1901 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
400fbf9f
JW
1902 C_SET_EXP_ORIGINAL_CODE (result, code);
1903 TREE_CONSTANT (result) = flag;
1904 }
1905
1906 return result;
1907}
1908
1909/* Build a binary-operation expression without default conversions.
1910 CODE is the kind of expression to build.
1911 This function differs from `build' in several ways:
1912 the data type of the result is computed and recorded in it,
1913 warnings are generated if arg data types are invalid,
1914 special handling for addition and subtraction of pointers is known,
1915 and some optimization is done (operations on narrow ints
1916 are done in the narrower type when that gives the same result).
1917 Constant folding is also done before the result is returned.
1918
1919 Note that the operands will never have enumeral types, or function
1920 or array types, because either they will have the default conversions
1921 performed or they have both just been converted to some other type in which
1922 the arithmetic is to be done. */
1923
1924tree
1925build_binary_op (code, orig_op0, orig_op1, convert_p)
1926 enum tree_code code;
1927 tree orig_op0, orig_op1;
1928 int convert_p;
1929{
1930 tree type0, type1;
1931 register enum tree_code code0, code1;
1932 tree op0, op1;
1933
1934 /* Expression code to give to the expression when it is built.
1935 Normally this is CODE, which is what the caller asked for,
1936 but in some special cases we change it. */
1937 register enum tree_code resultcode = code;
1938
1939 /* Data type in which the computation is to be performed.
1940 In the simplest cases this is the common type of the arguments. */
1941 register tree result_type = NULL;
1942
1943 /* Nonzero means operands have already been type-converted
1944 in whatever way is necessary.
1945 Zero means they need to be converted to RESULT_TYPE. */
1946 int converted = 0;
1947
293c9fdd
JM
1948 /* Nonzero means create the expression with this type, rather than
1949 RESULT_TYPE. */
1950 tree build_type = 0;
1951
400fbf9f 1952 /* Nonzero means after finally constructing the expression
293c9fdd 1953 convert it to this type. */
400fbf9f
JW
1954 tree final_type = 0;
1955
1956 /* Nonzero if this is an operation like MIN or MAX which can
1957 safely be computed in short if both args are promoted shorts.
1958 Also implies COMMON.
1959 -1 indicates a bitwise operation; this makes a difference
1960 in the exact conditions for when it is safe to do the operation
1961 in a narrower mode. */
1962 int shorten = 0;
1963
1964 /* Nonzero if this is a comparison operation;
1965 if both args are promoted shorts, compare the original shorts.
1966 Also implies COMMON. */
1967 int short_compare = 0;
1968
1969 /* Nonzero if this is a right-shift operation, which can be computed on the
1970 original short and then promoted if the operand is a promoted short. */
1971 int short_shift = 0;
1972
1973 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1974 int common = 0;
1975
1976 if (convert_p)
1977 {
1978 op0 = default_conversion (orig_op0);
1979 op1 = default_conversion (orig_op1);
1980 }
1981 else
1982 {
1983 op0 = orig_op0;
1984 op1 = orig_op1;
1985 }
1986
1987 type0 = TREE_TYPE (op0);
1988 type1 = TREE_TYPE (op1);
1989
1990 /* The expression codes of the data types of the arguments tell us
1991 whether the arguments are integers, floating, pointers, etc. */
1992 code0 = TREE_CODE (type0);
1993 code1 = TREE_CODE (type1);
1994
fc76e425 1995 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce
RS
1996 STRIP_TYPE_NOPS (op0);
1997 STRIP_TYPE_NOPS (op1);
400fbf9f
JW
1998
1999 /* If an error was already reported for one of the arguments,
2000 avoid reporting another error. */
2001
2002 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2003 return error_mark_node;
2004
2005 switch (code)
2006 {
2007 case PLUS_EXPR:
2008 /* Handle the pointer + int case. */
2009 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2010 return pointer_int_sum (PLUS_EXPR, op0, op1);
2011 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2012 return pointer_int_sum (PLUS_EXPR, op1, op0);
2013 else
2014 common = 1;
2015 break;
2016
2017 case MINUS_EXPR:
2018 /* Subtraction of two similar pointers.
2019 We must subtract them as integers, then divide by object size. */
2020 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2021 && comp_target_types (type0, type1))
2022 return pointer_diff (op0, op1);
2023 /* Handle pointer minus int. Just like pointer plus int. */
2024 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2025 return pointer_int_sum (MINUS_EXPR, op0, op1);
2026 else
2027 common = 1;
2028 break;
2029
2030 case MULT_EXPR:
2031 common = 1;
2032 break;
2033
2034 case TRUNC_DIV_EXPR:
2035 case CEIL_DIV_EXPR:
2036 case FLOOR_DIV_EXPR:
2037 case ROUND_DIV_EXPR:
2038 case EXACT_DIV_EXPR:
b6a10c9f
RS
2039 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2040 || code0 == COMPLEX_TYPE)
2041 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2042 || code1 == COMPLEX_TYPE))
400fbf9f
JW
2043 {
2044 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2045 resultcode = RDIV_EXPR;
2046 else
8b39ed65
TG
2047 {
2048 /* Although it would be tempting to shorten always here, that
2049 loses on some targets, since the modulo instruction is
2050 undefined if the quotient can't be represented in the
2051 computation mode. We shorten only if unsigned or if
2052 dividing by something we know != -1. */
96d8f1d8 2053 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
8b39ed65
TG
2054 || (TREE_CODE (op1) == INTEGER_CST
2055 && (TREE_INT_CST_LOW (op1) != -1
2056 || TREE_INT_CST_HIGH (op1) != -1)));
2057 }
400fbf9f
JW
2058 common = 1;
2059 }
2060 break;
2061
2062 case BIT_AND_EXPR:
2063 case BIT_ANDTC_EXPR:
2064 case BIT_IOR_EXPR:
2065 case BIT_XOR_EXPR:
2066 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2067 shorten = -1;
2068 /* If one operand is a constant, and the other is a short type
2069 that has been converted to an int,
2070 really do the work in the short type and then convert the
2071 result to int. If we are lucky, the constant will be 0 or 1
2072 in the short type, making the entire operation go away. */
2073 if (TREE_CODE (op0) == INTEGER_CST
2074 && TREE_CODE (op1) == NOP_EXPR
2075 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2076 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2077 {
2078 final_type = result_type;
2079 op1 = TREE_OPERAND (op1, 0);
2080 result_type = TREE_TYPE (op1);
2081 }
2082 if (TREE_CODE (op1) == INTEGER_CST
2083 && TREE_CODE (op0) == NOP_EXPR
2084 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2085 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2086 {
2087 final_type = result_type;
2088 op0 = TREE_OPERAND (op0, 0);
2089 result_type = TREE_TYPE (op0);
2090 }
2091 break;
2092
2093 case TRUNC_MOD_EXPR:
047de90b 2094 case FLOOR_MOD_EXPR:
400fbf9f 2095 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
03d5b1f5
RS
2096 {
2097 /* Although it would be tempting to shorten always here, that loses
2098 on some targets, since the modulo instruction is undefined if the
2099 quotient can't be represented in the computation mode. We shorten
2100 only if unsigned or if dividing by something we know != -1. */
96d8f1d8 2101 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
03d5b1f5
RS
2102 || (TREE_CODE (op1) == INTEGER_CST
2103 && (TREE_INT_CST_LOW (op1) != -1
2104 || TREE_INT_CST_HIGH (op1) != -1)));
2105 common = 1;
2106 }
400fbf9f
JW
2107 break;
2108
2109 case TRUTH_ANDIF_EXPR:
2110 case TRUTH_ORIF_EXPR:
2111 case TRUTH_AND_EXPR:
2112 case TRUTH_OR_EXPR:
1eca8b1e 2113 case TRUTH_XOR_EXPR:
b6a10c9f
RS
2114 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2115 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2116 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2117 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
400fbf9f
JW
2118 {
2119 /* Result of these operations is always an int,
2120 but that does not mean the operands should be
2121 converted to ints! */
2122 result_type = integer_type_node;
2123 op0 = truthvalue_conversion (op0);
2124 op1 = truthvalue_conversion (op1);
2125 converted = 1;
2126 }
2127 break;
2128
2129 /* Shift operations: result has same type as first operand;
2130 always convert second operand to int.
2131 Also set SHORT_SHIFT if shifting rightward. */
2132
2133 case RSHIFT_EXPR:
2134 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2135 {
47ee6837 2136 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
400fbf9f 2137 {
ff3225e7 2138 if (tree_int_cst_sgn (op1) < 0)
315da535 2139 warning ("right shift count is negative");
17651386
RS
2140 else
2141 {
2142 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2143 short_shift = 1;
2144 if (TREE_INT_CST_HIGH (op1) != 0
2145 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2146 >= TYPE_PRECISION (type0)))
315da535 2147 warning ("right shift count >= width of type");
17651386 2148 }
400fbf9f 2149 }
d45cf215
RS
2150 /* Use the type of the value to be shifted.
2151 This is what most traditional C compilers do. */
2152 result_type = type0;
400fbf9f
JW
2153 /* Unless traditional, convert the shift-count to an integer,
2154 regardless of size of value being shifted. */
2155 if (! flag_traditional)
2156 {
6cb72a7d 2157 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
400fbf9f
JW
2158 op1 = convert (integer_type_node, op1);
2159 /* Avoid converting op1 to result_type later. */
2160 converted = 1;
2161 }
400fbf9f
JW
2162 }
2163 break;
2164
2165 case LSHIFT_EXPR:
2166 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2167 {
47ee6837 2168 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
17651386 2169 {
ff3225e7 2170 if (tree_int_cst_sgn (op1) < 0)
315da535 2171 warning ("left shift count is negative");
17651386
RS
2172 else if (TREE_INT_CST_HIGH (op1) != 0
2173 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2174 >= TYPE_PRECISION (type0)))
315da535 2175 warning ("left shift count >= width of type");
17651386 2176 }
d45cf215
RS
2177 /* Use the type of the value to be shifted.
2178 This is what most traditional C compilers do. */
2179 result_type = type0;
400fbf9f
JW
2180 /* Unless traditional, convert the shift-count to an integer,
2181 regardless of size of value being shifted. */
2182 if (! flag_traditional)
2183 {
6cb72a7d 2184 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
400fbf9f
JW
2185 op1 = convert (integer_type_node, op1);
2186 /* Avoid converting op1 to result_type later. */
2187 converted = 1;
2188 }
400fbf9f
JW
2189 }
2190 break;
2191
2192 case RROTATE_EXPR:
2193 case LROTATE_EXPR:
2194 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2195 {
47ee6837 2196 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
17651386 2197 {
ff3225e7 2198 if (tree_int_cst_sgn (op1) < 0)
17651386
RS
2199 warning ("shift count is negative");
2200 else if (TREE_INT_CST_HIGH (op1) != 0
2201 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2202 >= TYPE_PRECISION (type0)))
2203 warning ("shift count >= width of type");
2204 }
d45cf215
RS
2205 /* Use the type of the value to be shifted.
2206 This is what most traditional C compilers do. */
2207 result_type = type0;
400fbf9f
JW
2208 /* Unless traditional, convert the shift-count to an integer,
2209 regardless of size of value being shifted. */
2210 if (! flag_traditional)
2211 {
6cb72a7d 2212 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
400fbf9f
JW
2213 op1 = convert (integer_type_node, op1);
2214 /* Avoid converting op1 to result_type later. */
2215 converted = 1;
2216 }
400fbf9f
JW
2217 }
2218 break;
2219
2220 case EQ_EXPR:
2221 case NE_EXPR:
2222 /* Result of comparison is always int,
2223 but don't convert the args to int! */
293c9fdd 2224 build_type = integer_type_node;
b6a10c9f
RS
2225 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2226 || code0 == COMPLEX_TYPE)
2227 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2228 || code1 == COMPLEX_TYPE))
400fbf9f
JW
2229 short_compare = 1;
2230 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2231 {
2232 register tree tt0 = TREE_TYPE (type0);
2233 register tree tt1 = TREE_TYPE (type1);
2234 /* Anything compares with void *. void * compares with anything.
d11fdb45
RS
2235 Otherwise, the targets must be compatible
2236 and both must be object or both incomplete. */
400fbf9f 2237 if (comp_target_types (type0, type1))
605a99f6 2238 result_type = common_type (type0, type1);
400fbf9f
JW
2239 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2240 {
fd5d5b94
RS
2241 /* op0 != orig_op0 detects the case of something
2242 whose value is 0 but which isn't a valid null ptr const. */
2243 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
400fbf9f
JW
2244 && TREE_CODE (tt1) == FUNCTION_TYPE)
2245 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2246 }
2247 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2248 {
fd5d5b94 2249 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
400fbf9f
JW
2250 && TREE_CODE (tt0) == FUNCTION_TYPE)
2251 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2252 }
2253 else
2254 pedwarn ("comparison of distinct pointer types lacks a cast");
605a99f6
JM
2255
2256 if (result_type == NULL_TREE)
2257 result_type = ptr_type_node;
400fbf9f
JW
2258 }
2259 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2260 && integer_zerop (op1))
293c9fdd 2261 result_type = type0;
400fbf9f
JW
2262 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2263 && integer_zerop (op0))
293c9fdd 2264 result_type = type1;
400fbf9f
JW
2265 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2266 {
293c9fdd 2267 result_type = type0;
400fbf9f
JW
2268 if (! flag_traditional)
2269 pedwarn ("comparison between pointer and integer");
400fbf9f
JW
2270 }
2271 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2272 {
293c9fdd 2273 result_type = type1;
400fbf9f
JW
2274 if (! flag_traditional)
2275 pedwarn ("comparison between pointer and integer");
400fbf9f 2276 }
400fbf9f
JW
2277 break;
2278
2279 case MAX_EXPR:
2280 case MIN_EXPR:
9db931af
RS
2281 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2282 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
400fbf9f
JW
2283 shorten = 1;
2284 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2285 {
605a99f6
JM
2286 if (comp_target_types (type0, type1))
2287 {
2288 result_type = common_type (type0, type1);
2289 if (pedantic
2290 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2291 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2292 }
2293 else
2294 {
2295 result_type = ptr_type_node;
2296 pedwarn ("comparison of distinct pointer types lacks a cast");
2297 }
400fbf9f
JW
2298 }
2299 break;
2300
2301 case LE_EXPR:
2302 case GE_EXPR:
2303 case LT_EXPR:
2304 case GT_EXPR:
293c9fdd 2305 build_type = integer_type_node;
9db931af
RS
2306 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2307 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
400fbf9f
JW
2308 short_compare = 1;
2309 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2310 {
605a99f6
JM
2311 if (comp_target_types (type0, type1))
2312 {
2313 result_type = common_type (type0, type1);
2314 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2315 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2316 pedwarn ("comparison of complete and incomplete pointers");
2317 else if (pedantic
2318 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2319 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2320 }
2321 else
2322 {
2323 result_type = ptr_type_node;
2324 pedwarn ("comparison of distinct pointer types lacks a cast");
2325 }
400fbf9f
JW
2326 }
2327 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2328 && integer_zerop (op1))
2329 {
293c9fdd 2330 result_type = type0;
ddcf4abc 2331 if (pedantic || extra_warnings)
400fbf9f
JW
2332 pedwarn ("ordered comparison of pointer with integer zero");
2333 }
2334 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2335 && integer_zerop (op0))
2336 {
293c9fdd 2337 result_type = type1;
400fbf9f
JW
2338 if (pedantic)
2339 pedwarn ("ordered comparison of pointer with integer zero");
2340 }
2341 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2342 {
293c9fdd 2343 result_type = type0;
400fbf9f
JW
2344 if (! flag_traditional)
2345 pedwarn ("comparison between pointer and integer");
400fbf9f
JW
2346 }
2347 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2348 {
293c9fdd 2349 result_type = type1;
400fbf9f
JW
2350 if (! flag_traditional)
2351 pedwarn ("comparison between pointer and integer");
400fbf9f 2352 }
400fbf9f 2353 break;
e9a25f70
JL
2354
2355 default:
2356 break;
400fbf9f
JW
2357 }
2358
b6a10c9f
RS
2359 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2360 &&
2361 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
400fbf9f 2362 {
b6a10c9f
RS
2363 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2364
400fbf9f
JW
2365 if (shorten || common || short_compare)
2366 result_type = common_type (type0, type1);
2367
2368 /* For certain operations (which identify themselves by shorten != 0)
2369 if both args were extended from the same smaller type,
2370 do the arithmetic in that type and then extend.
2371
2372 shorten !=0 and !=1 indicates a bitwise operation.
2373 For them, this optimization is safe only if
2374 both args are zero-extended or both are sign-extended.
2375 Otherwise, we might change the result.
2376 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2377 but calculated in (unsigned short) it would be (unsigned short)-1. */
2378
b6a10c9f 2379 if (shorten && none_complex)
400fbf9f
JW
2380 {
2381 int unsigned0, unsigned1;
2382 tree arg0 = get_narrower (op0, &unsigned0);
2383 tree arg1 = get_narrower (op1, &unsigned1);
2384 /* UNS is 1 if the operation to be done is an unsigned one. */
2385 int uns = TREE_UNSIGNED (result_type);
2386 tree type;
2387
2388 final_type = result_type;
2389
e7951b3f 2390 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
400fbf9f
JW
2391 but it *requires* conversion to FINAL_TYPE. */
2392
e7951b3f
RS
2393 if ((TYPE_PRECISION (TREE_TYPE (op0))
2394 == TYPE_PRECISION (TREE_TYPE (arg0)))
2395 && TREE_TYPE (op0) != final_type)
400fbf9f 2396 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
e7951b3f
RS
2397 if ((TYPE_PRECISION (TREE_TYPE (op1))
2398 == TYPE_PRECISION (TREE_TYPE (arg1)))
2399 && TREE_TYPE (op1) != final_type)
400fbf9f
JW
2400 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2401
2402 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2403
2404 /* For bitwise operations, signedness of nominal type
2405 does not matter. Consider only how operands were extended. */
2406 if (shorten == -1)
2407 uns = unsigned0;
2408
2409 /* Note that in all three cases below we refrain from optimizing
2410 an unsigned operation on sign-extended args.
2411 That would not be valid. */
2412
2413 /* Both args variable: if both extended in same way
2414 from same width, do it in that width.
2415 Do it unsigned if args were zero-extended. */
2416 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2417 < TYPE_PRECISION (result_type))
2418 && (TYPE_PRECISION (TREE_TYPE (arg1))
2419 == TYPE_PRECISION (TREE_TYPE (arg0)))
2420 && unsigned0 == unsigned1
2421 && (unsigned0 || !uns))
2422 result_type
2423 = signed_or_unsigned_type (unsigned0,
2424 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2425 else if (TREE_CODE (arg0) == INTEGER_CST
2426 && (unsigned1 || !uns)
2427 && (TYPE_PRECISION (TREE_TYPE (arg1))
2428 < TYPE_PRECISION (result_type))
2429 && (type = signed_or_unsigned_type (unsigned1,
2430 TREE_TYPE (arg1)),
2431 int_fits_type_p (arg0, type)))
2432 result_type = type;
2433 else if (TREE_CODE (arg1) == INTEGER_CST
2434 && (unsigned0 || !uns)
2435 && (TYPE_PRECISION (TREE_TYPE (arg0))
2436 < TYPE_PRECISION (result_type))
2437 && (type = signed_or_unsigned_type (unsigned0,
2438 TREE_TYPE (arg0)),
2439 int_fits_type_p (arg1, type)))
2440 result_type = type;
2441 }
2442
2443 /* Shifts can be shortened if shifting right. */
2444
2445 if (short_shift)
2446 {
2447 int unsigned_arg;
2448 tree arg0 = get_narrower (op0, &unsigned_arg);
2449
2450 final_type = result_type;
2451
2452 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2453 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2454
2455 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6cb70f0c
JW
2456 /* We can shorten only if the shift count is less than the
2457 number of bits in the smaller type size. */
2458 && TREE_INT_CST_HIGH (op1) == 0
2459 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
400fbf9f
JW
2460 /* If arg is sign-extended and then unsigned-shifted,
2461 we can simulate this with a signed shift in arg's type
2462 only if the extended result is at least twice as wide
2463 as the arg. Otherwise, the shift could use up all the
2464 ones made by sign-extension and bring in zeros.
2465 We can't optimize that case at all, but in most machines
2466 it never happens because available widths are 2**N. */
2467 && (!TREE_UNSIGNED (final_type)
2468 || unsigned_arg
2469 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2470 {
2471 /* Do an unsigned shift if the operand was zero-extended. */
2472 result_type
2473 = signed_or_unsigned_type (unsigned_arg,
2474 TREE_TYPE (arg0));
2475 /* Convert value-to-be-shifted to that type. */
2476 if (TREE_TYPE (op0) != result_type)
2477 op0 = convert (result_type, op0);
2478 converted = 1;
2479 }
2480 }
2481
2482 /* Comparison operations are shortened too but differently.
2483 They identify themselves by setting short_compare = 1. */
2484
75326e8c 2485 if (short_compare)
400fbf9f
JW
2486 {
2487 /* Don't write &op0, etc., because that would prevent op0
2488 from being kept in a register.
2489 Instead, make copies of the our local variables and
2490 pass the copies by reference, then copy them back afterward. */
2491 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2492 enum tree_code xresultcode = resultcode;
2493 tree val
2494 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2495 if (val != 0)
2496 return val;
293c9fdd
JM
2497 op0 = xop0, op1 = xop1;
2498 converted = 1;
400fbf9f
JW
2499 resultcode = xresultcode;
2500
407cb092
PE
2501 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2502 && skip_evaluation == 0)
400fbf9f 2503 {
d2d7ed3e
JM
2504 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2505 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2506
64c01f80
DE
2507 int unsignedp0, unsignedp1;
2508 tree primop0 = get_narrower (op0, &unsignedp0);
2509 tree primop1 = get_narrower (op1, &unsignedp1);
2510
912b4fc3
JM
2511 /* Avoid spurious warnings for comparison with enumerators. */
2512
2513 xop0 = orig_op0;
2514 xop1 = orig_op1;
2515 STRIP_TYPE_NOPS (xop0);
2516 STRIP_TYPE_NOPS (xop1);
2517
400fbf9f 2518 /* Give warnings for comparisons between signed and unsigned
293c9fdd 2519 quantities that may fail. */
400fbf9f
JW
2520 /* Do the checking based on the original operand trees, so that
2521 casts will be considered, but default promotions won't be. */
293c9fdd
JM
2522
2523 /* Do not warn if the comparison is being done in a signed type,
2524 since the signed type will only be chosen if it can represent
2525 all the values of the unsigned type. */
2526 if (! TREE_UNSIGNED (result_type))
2527 /* OK */;
2e14370e
JM
2528 /* Do not warn if both operands are unsigned. */
2529 else if (op0_signed == op1_signed)
2530 /* OK */;
293c9fdd
JM
2531 /* Do not warn if the signed quantity is an unsuffixed
2532 integer literal (or some static constant expression
2533 involving such literals) and it is non-negative. */
2534 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2535 && tree_int_cst_sgn (xop0) >= 0)
2536 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2537 && tree_int_cst_sgn (xop1) >= 0))
2538 /* OK */;
2539 /* Do not warn if the comparison is an equality operation,
2540 the unsigned quantity is an integral constant and it does
2541 not use the most significant bit of result_type. */
2542 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2543 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
9f6de2b9 2544 && int_fits_type_p (xop1, signed_type (result_type)))
293c9fdd 2545 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
9f6de2b9 2546 && int_fits_type_p (xop0, signed_type (result_type)))))
293c9fdd
JM
2547 /* OK */;
2548 else
400fbf9f 2549 warning ("comparison between signed and unsigned");
64c01f80
DE
2550
2551 /* Warn if two unsigned values are being compared in a size
2552 larger than their original size, and one (and only one) is the
2553 result of a `~' operator. This comparison will always fail.
2554
2555 Also warn if one operand is a constant, and the constant
2556 does not have all bits set that are set in the ~ operand
2557 when it is extended. */
2558
2559 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2560 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2561 {
2562 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2563 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2564 &unsignedp0);
2565 else
2566 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2567 &unsignedp1);
2568
2569 if (TREE_CODE (primop0) == INTEGER_CST
2570 || TREE_CODE (primop1) == INTEGER_CST)
2571 {
2572 tree primop;
2573 long constant, mask;
2574 int unsignedp, bits;
2575
2576 if (TREE_CODE (primop0) == INTEGER_CST)
2577 {
2578 primop = primop1;
2579 unsignedp = unsignedp1;
2580 constant = TREE_INT_CST_LOW (primop0);
2581 }
2582 else
2583 {
2584 primop = primop0;
2585 unsignedp = unsignedp0;
2586 constant = TREE_INT_CST_LOW (primop1);
2587 }
2588
2589 bits = TYPE_PRECISION (TREE_TYPE (primop));
2590 if (bits < TYPE_PRECISION (result_type)
2591 && bits < HOST_BITS_PER_LONG && unsignedp)
2592 {
2593 mask = (~0L) << bits;
2594 if ((mask & constant) != mask)
2595 warning ("comparison of promoted ~unsigned with constant");
2596 }
2597 }
2598 else if (unsignedp0 && unsignedp1
2599 && (TYPE_PRECISION (TREE_TYPE (primop0))
2600 < TYPE_PRECISION (result_type))
2601 && (TYPE_PRECISION (TREE_TYPE (primop1))
2602 < TYPE_PRECISION (result_type)))
2603 warning ("comparison of promoted ~unsigned with unsigned");
2604 }
400fbf9f
JW
2605 }
2606 }
2607 }
2608
2609 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2610 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2611 Then the expression will be built.
2612 It will be given type FINAL_TYPE if that is nonzero;
2613 otherwise, it will be given type RESULT_TYPE. */
2614
2615 if (!result_type)
2616 {
2617 binary_op_error (code);
2618 return error_mark_node;
2619 }
2620
2621 if (! converted)
2622 {
2623 if (TREE_TYPE (op0) != result_type)
2624 op0 = convert (result_type, op0);
2625 if (TREE_TYPE (op1) != result_type)
2626 op1 = convert (result_type, op1);
2627 }
2628
293c9fdd
JM
2629 if (build_type == NULL_TREE)
2630 build_type = result_type;
2631
400fbf9f 2632 {
293c9fdd 2633 register tree result = build (resultcode, build_type, op0, op1);
400fbf9f
JW
2634 register tree folded;
2635
2636 folded = fold (result);
2637 if (folded == result)
2638 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2639 if (final_type != 0)
2640 return convert (final_type, folded);
2641 return folded;
2642 }
2643}
2644\f
2645/* Return a tree for the sum or difference (RESULTCODE says which)
2646 of pointer PTROP and integer INTOP. */
2647
2648static tree
2649pointer_int_sum (resultcode, ptrop, intop)
2650 enum tree_code resultcode;
2651 register tree ptrop, intop;
2652{
2653 tree size_exp;
2654
2655 register tree result;
2656 register tree folded;
2657
2658 /* The result is a pointer of the same type that is being added. */
2659
2660 register tree result_type = TREE_TYPE (ptrop);
2661
2662 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2663 {
2664 if (pedantic || warn_pointer_arith)
2665 pedwarn ("pointer of type `void *' used in arithmetic");
2666 size_exp = integer_one_node;
2667 }
2668 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2669 {
2670 if (pedantic || warn_pointer_arith)
2671 pedwarn ("pointer to a function used in arithmetic");
2672 size_exp = integer_one_node;
2673 }
2674 else
2675 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2676
2677 /* If what we are about to multiply by the size of the elements
2678 contains a constant term, apply distributive law
2679 and multiply that constant term separately.
2680 This helps produce common subexpressions. */
2681
2682 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2683 && ! TREE_CONSTANT (intop)
2684 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2685 && TREE_CONSTANT (size_exp)
2686 /* If the constant comes from pointer subtraction,
2687 skip this optimization--it would cause an error. */
ba11c179
RK
2688 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2689 /* If the constant is unsigned, and smaller than the pointer size,
2690 then we must skip this optimization. This is because it could cause
2691 an overflow error if the constant is negative but INTOP is not. */
2692 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2693 || (TYPE_PRECISION (TREE_TYPE (intop))
2694 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
400fbf9f
JW
2695 {
2696 enum tree_code subcode = resultcode;
d45cf215 2697 tree int_type = TREE_TYPE (intop);
400fbf9f
JW
2698 if (TREE_CODE (intop) == MINUS_EXPR)
2699 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
d45cf215
RS
2700 /* Convert both subexpression types to the type of intop,
2701 because weird cases involving pointer arithmetic
2702 can result in a sum or difference with different type args. */
2703 ptrop = build_binary_op (subcode, ptrop,
2704 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2705 intop = convert (int_type, TREE_OPERAND (intop, 0));
400fbf9f
JW
2706 }
2707
b200d1aa 2708 /* Convert the integer argument to a type the same size as sizetype
400fbf9f
JW
2709 so the multiply won't overflow spuriously. */
2710
489af5d1
RK
2711 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2712 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2713 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2714 TREE_UNSIGNED (sizetype)), intop);
400fbf9f 2715
6946afd3
RK
2716 /* Replace the integer argument with a suitable product by the object size.
2717 Do this multiplication as signed, then convert to the appropriate
2718 pointer type (actually unsigned integral). */
400fbf9f 2719
6946afd3
RK
2720 intop = convert (result_type,
2721 build_binary_op (MULT_EXPR, intop,
2722 convert (TREE_TYPE (intop), size_exp), 1));
400fbf9f
JW
2723
2724 /* Create the sum or difference. */
2725
2726 result = build (resultcode, result_type, ptrop, intop);
2727
2728 folded = fold (result);
2729 if (folded == result)
2730 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2731 return folded;
2732}
2733
2734/* Return a tree for the difference of pointers OP0 and OP1.
2735 The resulting tree has type int. */
2736
2737static tree
2738pointer_diff (op0, op1)
2739 register tree op0, op1;
2740{
2741 register tree result, folded;
2742 tree restype = ptrdiff_type_node;
2743
2744 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2745
2746 if (pedantic || warn_pointer_arith)
2747 {
2748 if (TREE_CODE (target_type) == VOID_TYPE)
2749 pedwarn ("pointer of type `void *' used in subtraction");
2750 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2751 pedwarn ("pointer to a function used in subtraction");
2752 }
2753
2754 /* First do the subtraction as integers;
04044297
MM
2755 then drop through to build the divide operator.
2756 Do not do default conversions on the minus operator
2757 in case restype is a short type. */
400fbf9f
JW
2758
2759 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
04044297 2760 convert (restype, op1), 0);
ea8dd784
JW
2761 /* This generates an error if op1 is pointer to incomplete type. */
2762 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2763 error ("arithmetic on pointer to an incomplete type");
6946afd3 2764
ea8dd784 2765 /* This generates an error if op0 is pointer to incomplete type. */
400fbf9f
JW
2766 op1 = c_size_in_bytes (target_type);
2767
2768 /* Divide by the size, in easiest possible way. */
2769
6946afd3 2770 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
400fbf9f
JW
2771
2772 folded = fold (result);
2773 if (folded == result)
2774 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2775 return folded;
2776}
2777\f
2778/* Construct and perhaps optimize a tree representation
2779 for a unary operation. CODE, a tree_code, specifies the operation
2780 and XARG is the operand. NOCONVERT nonzero suppresses
2781 the default promotions (such as from short to int). */
2782
2783tree
2784build_unary_op (code, xarg, noconvert)
2785 enum tree_code code;
2786 tree xarg;
2787 int noconvert;
2788{
2789 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2790 register tree arg = xarg;
2791 register tree argtype = 0;
2792 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
400fbf9f
JW
2793 tree val;
2794
2795 if (typecode == ERROR_MARK)
2796 return error_mark_node;
2797 if (typecode == ENUMERAL_TYPE)
2798 typecode = INTEGER_TYPE;
2799
2800 switch (code)
2801 {
2802 case CONVERT_EXPR:
2803 /* This is used for unary plus, because a CONVERT_EXPR
2804 is enough to prevent anybody from looking inside for
2805 associativity, but won't generate any code. */
b6a10c9f
RS
2806 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2807 || typecode == COMPLEX_TYPE))
ab87f8c8
JL
2808 {
2809 error ("wrong type argument to unary plus");
2810 return error_mark_node;
2811 }
400fbf9f
JW
2812 else if (!noconvert)
2813 arg = default_conversion (arg);
2814 break;
2815
2816 case NEGATE_EXPR:
b6a10c9f
RS
2817 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2818 || typecode == COMPLEX_TYPE))
ab87f8c8
JL
2819 {
2820 error ("wrong type argument to unary minus");
2821 return error_mark_node;
2822 }
400fbf9f
JW
2823 else if (!noconvert)
2824 arg = default_conversion (arg);
2825 break;
2826
2827 case BIT_NOT_EXPR:
1c2a9b35
RS
2828 if (typecode == COMPLEX_TYPE)
2829 {
2830 code = CONJ_EXPR;
2831 if (!noconvert)
2832 arg = default_conversion (arg);
2833 }
2834 else if (typecode != INTEGER_TYPE)
ab87f8c8
JL
2835 {
2836 error ("wrong type argument to bit-complement");
2837 return error_mark_node;
2838 }
400fbf9f
JW
2839 else if (!noconvert)
2840 arg = default_conversion (arg);
2841 break;
2842
2843 case ABS_EXPR:
b6a10c9f
RS
2844 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2845 || typecode == COMPLEX_TYPE))
ab87f8c8
JL
2846 {
2847 error ("wrong type argument to abs");
2848 return error_mark_node;
2849 }
400fbf9f
JW
2850 else if (!noconvert)
2851 arg = default_conversion (arg);
2852 break;
2853
1c2a9b35
RS
2854 case CONJ_EXPR:
2855 /* Conjugating a real value is a no-op, but allow it anyway. */
2856 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2857 || typecode == COMPLEX_TYPE))
ab87f8c8
JL
2858 {
2859 error ("wrong type argument to conjugation");
2860 return error_mark_node;
2861 }
1c2a9b35
RS
2862 else if (!noconvert)
2863 arg = default_conversion (arg);
2864 break;
2865
400fbf9f
JW
2866 case TRUTH_NOT_EXPR:
2867 if (typecode != INTEGER_TYPE
2868 && typecode != REAL_TYPE && typecode != POINTER_TYPE
b6a10c9f 2869 && typecode != COMPLEX_TYPE
400fbf9f
JW
2870 /* These will convert to a pointer. */
2871 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2872 {
ab87f8c8
JL
2873 error ("wrong type argument to unary exclamation mark");
2874 return error_mark_node;
400fbf9f
JW
2875 }
2876 arg = truthvalue_conversion (arg);
2877 return invert_truthvalue (arg);
2878
2879 case NOP_EXPR:
2880 break;
b6a10c9f
RS
2881
2882 case REALPART_EXPR:
2883 if (TREE_CODE (arg) == COMPLEX_CST)
2884 return TREE_REALPART (arg);
2885 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2886 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2887 else
2888 return arg;
2889
2890 case IMAGPART_EXPR:
2891 if (TREE_CODE (arg) == COMPLEX_CST)
2892 return TREE_IMAGPART (arg);
2893 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2894 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2895 else
2896 return convert (TREE_TYPE (arg), integer_zero_node);
400fbf9f
JW
2897
2898 case PREINCREMENT_EXPR:
2899 case POSTINCREMENT_EXPR:
2900 case PREDECREMENT_EXPR:
2901 case POSTDECREMENT_EXPR:
2902 /* Handle complex lvalues (when permitted)
2903 by reduction to simpler cases. */
2904
2905 val = unary_complex_lvalue (code, arg);
2906 if (val != 0)
2907 return val;
2908
b6a10c9f
RS
2909 /* Increment or decrement the real part of the value,
2910 and don't change the imaginary part. */
2911 if (typecode == COMPLEX_TYPE)
2912 {
2913 tree real, imag;
2914
2915 arg = stabilize_reference (arg);
2916 real = build_unary_op (REALPART_EXPR, arg, 1);
2917 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2918 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2919 build_unary_op (code, real, 1), imag);
2920 }
2921
400fbf9f
JW
2922 /* Report invalid types. */
2923
2924 if (typecode != POINTER_TYPE
2925 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2926 {
ab87f8c8
JL
2927 error (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2928 ? "wrong type argument to increment"
2929 : "wrong type argument to decrement");
2930 return error_mark_node;
400fbf9f
JW
2931 }
2932
2933 {
2934 register tree inc;
2935 tree result_type = TREE_TYPE (arg);
2936
2937 arg = get_unwidened (arg, 0);
2938 argtype = TREE_TYPE (arg);
2939
2940 /* Compute the increment. */
2941
2942 if (typecode == POINTER_TYPE)
2943 {
6bc4e3d0
RS
2944 /* If pointer target is an undefined struct,
2945 we just cannot know how to do the arithmetic. */
2946 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
ab87f8c8
JL
2947 error (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2948 ? "increment of pointer to unknown structure"
2949 : "decrement of pointer to unknown structure");
6bc4e3d0
RS
2950 else if ((pedantic || warn_pointer_arith)
2951 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2952 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
ab87f8c8
JL
2953 pedwarn (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2954 ? "wrong type argument to increment"
2955 : "wrong type argument to decrement");
0e9cff7f 2956 inc = c_size_in_bytes (TREE_TYPE (result_type));
400fbf9f
JW
2957 }
2958 else
2959 inc = integer_one_node;
2960
2961 inc = convert (argtype, inc);
2962
2963 /* Handle incrementing a cast-expression. */
2964
2965 while (1)
2966 switch (TREE_CODE (arg))
2967 {
2968 case NOP_EXPR:
2969 case CONVERT_EXPR:
2970 case FLOAT_EXPR:
2971 case FIX_TRUNC_EXPR:
2972 case FIX_FLOOR_EXPR:
2973 case FIX_ROUND_EXPR:
2974 case FIX_CEIL_EXPR:
ee71df46 2975 pedantic_lvalue_warning (CONVERT_EXPR);
400fbf9f
JW
2976 /* If the real type has the same machine representation
2977 as the type it is cast to, we can make better output
2978 by adding directly to the inside of the cast. */
2979 if ((TREE_CODE (TREE_TYPE (arg))
2980 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2981 && (TYPE_MODE (TREE_TYPE (arg))
2982 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2983 arg = TREE_OPERAND (arg, 0);
2984 else
2985 {
2986 tree incremented, modify, value;
400fbf9f
JW
2987 arg = stabilize_reference (arg);
2988 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2989 value = arg;
2990 else
2991 value = save_expr (arg);
2992 incremented = build (((code == PREINCREMENT_EXPR
2993 || code == POSTINCREMENT_EXPR)
2994 ? PLUS_EXPR : MINUS_EXPR),
2995 argtype, value, inc);
2996 TREE_SIDE_EFFECTS (incremented) = 1;
2997 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2998 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2999 TREE_USED (value) = 1;
3000 return value;
3001 }
3002 break;
3003
3004 default:
3005 goto give_up;
3006 }
3007 give_up:
3008
3009 /* Complain about anything else that is not a true lvalue. */
3010 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3011 || code == POSTINCREMENT_EXPR)
ab87f8c8
JL
3012 ? "invalid lvalue in increment"
3013 : "invalid lvalue in decrement")))
400fbf9f
JW
3014 return error_mark_node;
3015
3016 /* Report a read-only lvalue. */
26b3c423 3017 if (TREE_READONLY (arg))
400fbf9f
JW
3018 readonly_warning (arg,
3019 ((code == PREINCREMENT_EXPR
3020 || code == POSTINCREMENT_EXPR)
3021 ? "increment" : "decrement"));
3022
3023 val = build (code, TREE_TYPE (arg), arg, inc);
3024 TREE_SIDE_EFFECTS (val) = 1;
3025 val = convert (result_type, val);
3026 if (TREE_CODE (val) != code)
3027 TREE_NO_UNUSED_WARNING (val) = 1;
3028 return val;
3029 }
3030
3031 case ADDR_EXPR:
3032 /* Note that this operation never does default_conversion
3033 regardless of NOCONVERT. */
3034
3035 /* Let &* cancel out to simplify resulting code. */
3036 if (TREE_CODE (arg) == INDIRECT_REF)
3037 {
3038 /* Don't let this be an lvalue. */
3039 if (lvalue_p (TREE_OPERAND (arg, 0)))
3040 return non_lvalue (TREE_OPERAND (arg, 0));
3041 return TREE_OPERAND (arg, 0);
3042 }
3043
3044 /* For &x[y], return x+y */
3045 if (TREE_CODE (arg) == ARRAY_REF)
3046 {
3047 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3048 return error_mark_node;
3049 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3050 TREE_OPERAND (arg, 1), 1);
3051 }
3052
3053 /* Handle complex lvalues (when permitted)
3054 by reduction to simpler cases. */
3055 val = unary_complex_lvalue (code, arg);
3056 if (val != 0)
3057 return val;
3058
3059#if 0 /* Turned off because inconsistent;
3060 float f; *&(int)f = 3.4 stores in int format
3061 whereas (int)f = 3.4 stores in float format. */
3062 /* Address of a cast is just a cast of the address
3063 of the operand of the cast. */
3064 switch (TREE_CODE (arg))
3065 {
3066 case NOP_EXPR:
3067 case CONVERT_EXPR:
3068 case FLOAT_EXPR:
3069 case FIX_TRUNC_EXPR:
3070 case FIX_FLOOR_EXPR:
3071 case FIX_ROUND_EXPR:
3072 case FIX_CEIL_EXPR:
3073 if (pedantic)
3074 pedwarn ("ANSI C forbids the address of a cast expression");
3075 return convert (build_pointer_type (TREE_TYPE (arg)),
3076 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3077 0));
3078 }
3079#endif
3080
3081 /* Allow the address of a constructor if all the elements
3082 are constant. */
3083 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3084 ;
3085 /* Anything not already handled and not a true memory reference
3086 is an error. */
ab87f8c8
JL
3087 else if (typecode != FUNCTION_TYPE
3088 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
400fbf9f
JW
3089 return error_mark_node;
3090
3091 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3092 argtype = TREE_TYPE (arg);
3932261a
MM
3093 /* If the lvalue is const or volatile, merge that into the type
3094 to which the address will point. Note that you can't get a
3095 restricted pointer by taking the address of something, so we
3096 only have to deal with `const' and `volatile' here. */
400fbf9f
JW
3097 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3098 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3099 {
3100 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3101 argtype = c_build_type_variant (argtype,
3102 TREE_READONLY (arg),
3103 TREE_THIS_VOLATILE (arg));
3104 }
3105
3106 argtype = build_pointer_type (argtype);
3107
3108 if (mark_addressable (arg) == 0)
3109 return error_mark_node;
3110
3111 {
3112 tree addr;
3113
3114 if (TREE_CODE (arg) == COMPONENT_REF)
3115 {
3116 tree field = TREE_OPERAND (arg, 1);
3117
3118 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3119
ef86d2a6 3120 if (DECL_C_BIT_FIELD (field))
400fbf9f
JW
3121 {
3122 error ("attempt to take address of bit-field structure member `%s'",
3123 IDENTIFIER_POINTER (DECL_NAME (field)));
3124 return error_mark_node;
3125 }
3126
3127 addr = convert (argtype, addr);
3128
3129 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3130 {
3131 tree offset
3132 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3133 size_int (BITS_PER_UNIT));
3134 int flag = TREE_CONSTANT (addr);
3135 addr = fold (build (PLUS_EXPR, argtype,
3136 addr, convert (argtype, offset)));
3137 TREE_CONSTANT (addr) = flag;
3138 }
3139 }
3140 else
3141 addr = build1 (code, argtype, arg);
3142
3143 /* Address of a static or external variable or
8706edbc
RS
3144 file-scope function counts as a constant. */
3145 if (staticp (arg)
3146 && ! (TREE_CODE (arg) == FUNCTION_DECL
3147 && DECL_CONTEXT (arg) != 0))
7d2d49af 3148 TREE_CONSTANT (addr) = 1;
400fbf9f
JW
3149 return addr;
3150 }
e9a25f70
JL
3151
3152 default:
3153 break;
400fbf9f
JW
3154 }
3155
ab87f8c8
JL
3156 if (argtype == 0)
3157 argtype = TREE_TYPE (arg);
3158 return fold (build1 (code, argtype, arg));
400fbf9f
JW
3159}
3160
3161#if 0
3162/* If CONVERSIONS is a conversion expression or a nested sequence of such,
3163 convert ARG with the same conversions in the same order
3164 and return the result. */
3165
3166static tree
3167convert_sequence (conversions, arg)
3168 tree conversions;
3169 tree arg;
3170{
3171 switch (TREE_CODE (conversions))
3172 {
3173 case NOP_EXPR:
3174 case CONVERT_EXPR:
3175 case FLOAT_EXPR:
3176 case FIX_TRUNC_EXPR:
3177 case FIX_FLOOR_EXPR:
3178 case FIX_ROUND_EXPR:
3179 case FIX_CEIL_EXPR:
3180 return convert (TREE_TYPE (conversions),
3181 convert_sequence (TREE_OPERAND (conversions, 0),
3182 arg));
3183
3184 default:
3185 return arg;
3186 }
3187}
3188#endif /* 0 */
3189
3190/* Return nonzero if REF is an lvalue valid for this language.
3191 Lvalues can be assigned, unless their type has TYPE_READONLY.
1394aabd 3192 Lvalues can have their address taken, unless they have DECL_REGISTER. */
400fbf9f
JW
3193
3194int
3195lvalue_p (ref)
3196 tree ref;
3197{
3198 register enum tree_code code = TREE_CODE (ref);
3199
3200 switch (code)
3201 {
b6a10c9f
RS
3202 case REALPART_EXPR:
3203 case IMAGPART_EXPR:
400fbf9f
JW
3204 case COMPONENT_REF:
3205 return lvalue_p (TREE_OPERAND (ref, 0));
3206
3207 case STRING_CST:
3208 return 1;
3209
3210 case INDIRECT_REF:
3211 case ARRAY_REF:
3212 case VAR_DECL:
3213 case PARM_DECL:
3214 case RESULT_DECL:
3215 case ERROR_MARK:
e9a25f70
JL
3216 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3217 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
cff9c407
RK
3218
3219 case BIND_EXPR:
3220 case RTL_EXPR:
e9a25f70
JL
3221 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3222
3223 default:
3224 return 0;
400fbf9f 3225 }
400fbf9f
JW
3226}
3227
3228/* Return nonzero if REF is an lvalue valid for this language;
3229 otherwise, print an error message and return zero. */
3230
3231int
ab87f8c8 3232lvalue_or_else (ref, msgid)
400fbf9f 3233 tree ref;
ab87f8c8 3234 char *msgid;
400fbf9f
JW
3235{
3236 int win = lvalue_p (ref);
3237 if (! win)
ab87f8c8 3238 error (msgid);
400fbf9f
JW
3239 return win;
3240}
3241
3242/* Apply unary lvalue-demanding operator CODE to the expression ARG
3243 for certain kinds of expressions which are not really lvalues
3244 but which we can accept as lvalues.
3245
3246 If ARG is not a kind of expression we can handle, return zero. */
3247
3248static tree
3249unary_complex_lvalue (code, arg)
3250 enum tree_code code;
3251 tree arg;
3252{
3253 /* Handle (a, b) used as an "lvalue". */
3254 if (TREE_CODE (arg) == COMPOUND_EXPR)
3255 {
3256 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
e9a25f70
JL
3257
3258 /* If this returns a function type, it isn't really being used as
3259 an lvalue, so don't issue a warning about it. */
3260 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3261 pedantic_lvalue_warning (COMPOUND_EXPR);
3262
400fbf9f
JW
3263 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3264 TREE_OPERAND (arg, 0), real_result);
3265 }
3266
3267 /* Handle (a ? b : c) used as an "lvalue". */
3268 if (TREE_CODE (arg) == COND_EXPR)
3269 {
3270 pedantic_lvalue_warning (COND_EXPR);
e9a25f70
JL
3271 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3272 pedantic_lvalue_warning (COMPOUND_EXPR);
3273
400fbf9f
JW
3274 return (build_conditional_expr
3275 (TREE_OPERAND (arg, 0),
3276 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3277 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3278 }
3279
3280 return 0;
3281}
3282
3283/* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3284 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3285
3286static void
3287pedantic_lvalue_warning (code)
3288 enum tree_code code;
3289{
3290 if (pedantic)
ab87f8c8
JL
3291 pedwarn (code == COND_EXPR
3292 ? "ANSI C forbids use of conditional expressions as lvalues"
3293 : code == COMPOUND_EXPR
3294 ? "ANSI C forbids use of compound expressions as lvalues"
3295 : "ANSI C forbids use of cast expressions as lvalues");
400fbf9f
JW
3296}
3297\f
3298/* Warn about storing in something that is `const'. */
3299
3300void
ab87f8c8 3301readonly_warning (arg, msgid)
400fbf9f 3302 tree arg;
ab87f8c8 3303 char *msgid;
400fbf9f 3304{
3791970d 3305 /* Forbid assignments to iterators. */
550707f7 3306 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
ab87f8c8
JL
3307 pedwarn ("%s of iterator `%s'", _(msgid),
3308 IDENTIFIER_POINTER (DECL_NAME (arg)));
3791970d 3309
400fbf9f
JW
3310 if (TREE_CODE (arg) == COMPONENT_REF)
3311 {
3312 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
ab87f8c8 3313 readonly_warning (TREE_OPERAND (arg, 0), msgid);
400fbf9f 3314 else
ab87f8c8
JL
3315 pedwarn ("%s of read-only member `%s'", _(msgid),
3316 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
400fbf9f
JW
3317 }
3318 else if (TREE_CODE (arg) == VAR_DECL)
ab87f8c8
JL
3319 pedwarn ("%s of read-only variable `%s'", _(msgid),
3320 IDENTIFIER_POINTER (DECL_NAME (arg)));
400fbf9f 3321 else
ab87f8c8 3322 pedwarn ("%s of read-only location", _(msgid));
400fbf9f
JW
3323}
3324\f
3325/* Mark EXP saying that we need to be able to take the
3326 address of it; it should not be allocated in a register.
3327 Value is 1 if successful. */
3328
3329int
3330mark_addressable (exp)
3331 tree exp;
3332{
3333 register tree x = exp;
3334 while (1)
3335 switch (TREE_CODE (x))
3336 {
400fbf9f 3337 case COMPONENT_REF:
1598f4da 3338 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
36c336d1
RK
3339 {
3340 error ("cannot take address of bitfield `%s'",
3341 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3342 return 0;
3343 }
1598f4da 3344
0f41302f 3345 /* ... fall through ... */
1598f4da
RK
3346
3347 case ADDR_EXPR:
400fbf9f 3348 case ARRAY_REF:
ce95080d
RS
3349 case REALPART_EXPR:
3350 case IMAGPART_EXPR:
400fbf9f
JW
3351 x = TREE_OPERAND (x, 0);
3352 break;
3353
3354 case CONSTRUCTOR:
3355 TREE_ADDRESSABLE (x) = 1;
3356 return 1;
3357
3358 case VAR_DECL:
3359 case CONST_DECL:
3360 case PARM_DECL:
3361 case RESULT_DECL:
1394aabd
RS
3362 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3363 && DECL_NONLOCAL (x))
4bb6d2f8
RS
3364 {
3365 if (TREE_PUBLIC (x))
3366 {
3367 error ("global register variable `%s' used in nested function",
3368 IDENTIFIER_POINTER (DECL_NAME (x)));
3369 return 0;
3370 }
3371 pedwarn ("register variable `%s' used in nested function",
3372 IDENTIFIER_POINTER (DECL_NAME (x)));
3373 }
1394aabd 3374 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
400fbf9f
JW
3375 {
3376 if (TREE_PUBLIC (x))
3377 {
3378 error ("address of global register variable `%s' requested",
3379 IDENTIFIER_POINTER (DECL_NAME (x)));
3380 return 0;
3381 }
bbbd6700
RK
3382
3383 /* If we are making this addressable due to its having
3384 volatile components, give a different error message. Also
3385 handle the case of an unnamed parameter by not trying
3386 to give the name. */
3387
3388 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3389 {
3390 error ("cannot put object with volatile field into register");
3391 return 0;
3392 }
3393
400fbf9f
JW
3394 pedwarn ("address of register variable `%s' requested",
3395 IDENTIFIER_POINTER (DECL_NAME (x)));
3396 }
3397 put_var_into_stack (x);
3398
3399 /* drops in */
3400 case FUNCTION_DECL:
3401 TREE_ADDRESSABLE (x) = 1;
3402#if 0 /* poplevel deals with this now. */
3403 if (DECL_CONTEXT (x) == 0)
3404 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3405#endif
3406
3407 default:
3408 return 1;
3409 }
3410}
3411\f
3412/* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3413
3414tree
3415build_conditional_expr (ifexp, op1, op2)
3416 tree ifexp, op1, op2;
3417{
3418 register tree type1;
3419 register tree type2;
3420 register enum tree_code code1;
3421 register enum tree_code code2;
3422 register tree result_type = NULL;
fd5d5b94 3423 tree orig_op1 = op1, orig_op2 = op2;
400fbf9f 3424
400fbf9f
JW
3425 ifexp = truthvalue_conversion (default_conversion (ifexp));
3426
400fbf9f
JW
3427#if 0 /* Produces wrong result if within sizeof. */
3428 /* Don't promote the operands separately if they promote
3429 the same way. Return the unpromoted type and let the combined
3430 value get promoted if necessary. */
3431
3432 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3433 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3434 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3435 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3436 {
3437 if (TREE_CODE (ifexp) == INTEGER_CST)
a29f2ec1 3438 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
400fbf9f
JW
3439
3440 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3441 }
3442#endif
3443
e855c5ce 3444 /* Promote both alternatives. */
400fbf9f
JW
3445
3446 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3447 op1 = default_conversion (op1);
3448 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3449 op2 = default_conversion (op2);
3450
e855c5ce
RS
3451 if (TREE_CODE (ifexp) == ERROR_MARK
3452 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3453 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3454 return error_mark_node;
3455
400fbf9f
JW
3456 type1 = TREE_TYPE (op1);
3457 code1 = TREE_CODE (type1);
3458 type2 = TREE_TYPE (op2);
3459 code2 = TREE_CODE (type2);
3460
3461 /* Quickly detect the usual case where op1 and op2 have the same type
3462 after promotion. */
1ad409d2
RS
3463 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3464 {
3465 if (type1 == type2)
3466 result_type = type1;
3467 else
3468 result_type = TYPE_MAIN_VARIANT (type1);
3469 }
400fbf9f
JW
3470 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3471 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3472 {
3473 result_type = common_type (type1, type2);
3474 }
3475 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3476 {
3477 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3478 pedwarn ("ANSI C forbids conditional expr with only one void side");
3479 result_type = void_type_node;
3480 }
3481 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3482 {
3483 if (comp_target_types (type1, type2))
3484 result_type = common_type (type1, type2);
fd5d5b94
RS
3485 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3486 && TREE_CODE (orig_op1) != NOP_EXPR)
400fbf9f 3487 result_type = qualify_type (type2, type1);
fd5d5b94
RS
3488 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3489 && TREE_CODE (orig_op2) != NOP_EXPR)
400fbf9f
JW
3490 result_type = qualify_type (type1, type2);
3491 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3492 {
3493 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3494 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3495 result_type = qualify_type (type1, type2);
3496 }
3497 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3498 {
3499 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3500 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3501 result_type = qualify_type (type2, type1);
3502 }
3503 else
3504 {
3505 pedwarn ("pointer type mismatch in conditional expression");
3506 result_type = build_pointer_type (void_type_node);
3507 }
3508 }
3509 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3510 {
3511 if (! integer_zerop (op2))
3512 pedwarn ("pointer/integer type mismatch in conditional expression");
3513 else
3514 {
3515 op2 = null_pointer_node;
3516#if 0 /* The spec seems to say this is permitted. */
3517 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3518 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3519#endif
3520 }
3521 result_type = type1;
3522 }
3523 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3524 {
3525 if (!integer_zerop (op1))
3526 pedwarn ("pointer/integer type mismatch in conditional expression");
3527 else
3528 {
3529 op1 = null_pointer_node;
3530#if 0 /* The spec seems to say this is permitted. */
3531 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3532 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3533#endif
3534 }
3535 result_type = type2;
3536 }
3537
3538 if (!result_type)
3539 {
3540 if (flag_cond_mismatch)
3541 result_type = void_type_node;
3542 else
3543 {
3544 error ("type mismatch in conditional expression");
3545 return error_mark_node;
3546 }
3547 }
3548
1dfdf85d
RS
3549 /* Merge const and volatile flags of the incoming types. */
3550 result_type
3551 = build_type_variant (result_type,
48c73063
RS
3552 TREE_READONLY (op1) || TREE_READONLY (op2),
3553 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
e58cd767 3554
400fbf9f 3555 if (result_type != TREE_TYPE (op1))
e58cd767 3556 op1 = convert_and_check (result_type, op1);
400fbf9f 3557 if (result_type != TREE_TYPE (op2))
e58cd767 3558 op2 = convert_and_check (result_type, op2);
400fbf9f
JW
3559
3560#if 0
3561 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3562 {
3563 result_type = TREE_TYPE (op1);
3564 if (TREE_CONSTANT (ifexp))
a29f2ec1 3565 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
400fbf9f
JW
3566
3567 if (TYPE_MODE (result_type) == BLKmode)
3568 {
3569 register tree tempvar
3570 = build_decl (VAR_DECL, NULL_TREE, result_type);
3571 register tree xop1 = build_modify_expr (tempvar, op1);
3572 register tree xop2 = build_modify_expr (tempvar, op2);
3573 register tree result = fold (build (COND_EXPR, result_type,
3574 ifexp, xop1, xop2));
3575
3576 layout_decl (tempvar, TYPE_ALIGN (result_type));
3577 /* No way to handle variable-sized objects here.
3578 I fear that the entire handling of BLKmode conditional exprs
3579 needs to be redone. */
3580 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3581 abort ();
3582 DECL_RTL (tempvar)
3583 = assign_stack_local (DECL_MODE (tempvar),
3584 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3585 + BITS_PER_UNIT - 1)
3586 / BITS_PER_UNIT,
3587 0);
3588
3589 TREE_SIDE_EFFECTS (result)
3590 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3591 | TREE_SIDE_EFFECTS (op2);
3592 return build (COMPOUND_EXPR, result_type, result, tempvar);
3593 }
3594 }
3595#endif /* 0 */
5abb45f2
RS
3596
3597 if (TREE_CODE (ifexp) == INTEGER_CST)
a29f2ec1 3598 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
400fbf9f 3599
400fbf9f
JW
3600 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3601}
3602\f
3603/* Given a list of expressions, return a compound expression
3604 that performs them all and returns the value of the last of them. */
3605
3606tree
3607build_compound_expr (list)
3608 tree list;
82bde854 3609{
43a5a542 3610 return internal_build_compound_expr (list, TRUE);
82bde854
MM
3611}
3612
3613static tree
3614internal_build_compound_expr (list, first_p)
3615 tree list;
3616 int first_p;
400fbf9f
JW
3617{
3618 register tree rest;
3619
3620 if (TREE_CHAIN (list) == 0)
3621 {
6dc42e49 3622#if 0 /* If something inside inhibited lvalueness, we should not override. */
400fbf9f
JW
3623 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3624
3625 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3626 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3627 list = TREE_OPERAND (list, 0);
3628#endif
3629
439f6027 3630 /* Don't let (0, 0) be null pointer constant. */
82bde854 3631 if (!first_p && integer_zerop (TREE_VALUE (list)))
439f6027
RS
3632 return non_lvalue (TREE_VALUE (list));
3633 return TREE_VALUE (list);
400fbf9f
JW
3634 }
3635
3636 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3637 {
3638 /* Convert arrays to pointers when there really is a comma operator. */
3639 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3640 TREE_VALUE (TREE_CHAIN (list))
3641 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3642 }
3643
82bde854 3644 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
400fbf9f 3645
0e7c47fa
RK
3646 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3647 {
3648 /* The left-hand operand of a comma expression is like an expression
3649 statement: with -W or -Wunused, we should warn if it doesn't have
3650 any side-effects, unless it was explicitly cast to (void). */
3651 if ((extra_warnings || warn_unused)
3652 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3653 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3654 warning ("left-hand operand of comma expression has no effect");
3655
3656 /* When pedantic, a compound expression can be neither an lvalue
3657 nor an integer constant expression. */
3658 if (! pedantic)
3659 return rest;
3660 }
3661
3662 /* With -Wunused, we should also warn if the left-hand operand does have
3663 side-effects, but computes a value which is not used. For example, in
3664 `foo() + bar(), baz()' the result of the `+' operator is not used,
3665 so we should issue a warning. */
3666 else if (warn_unused)
3667 warn_if_unused_value (TREE_VALUE (list));
400fbf9f
JW
3668
3669 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3670}
3671
3672/* Build an expression representing a cast to type TYPE of expression EXPR. */
3673
3674tree
3675build_c_cast (type, expr)
3676 register tree type;
3677 tree expr;
3678{
3679 register tree value = expr;
3680
3681 if (type == error_mark_node || expr == error_mark_node)
3682 return error_mark_node;
3683 type = TYPE_MAIN_VARIANT (type);
3684
3685#if 0
3686 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3687 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3688 value = TREE_OPERAND (value, 0);
3689#endif
3690
3691 if (TREE_CODE (type) == ARRAY_TYPE)
3692 {
3693 error ("cast specifies array type");
3694 return error_mark_node;
3695 }
3696
3697 if (TREE_CODE (type) == FUNCTION_TYPE)
3698 {
3699 error ("cast specifies function type");
3700 return error_mark_node;
3701 }
3702
3703 if (type == TREE_TYPE (value))
3704 {
3705 if (pedantic)
3706 {
3707 if (TREE_CODE (type) == RECORD_TYPE
3708 || TREE_CODE (type) == UNION_TYPE)
3709 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3710 }
3711 }
3712 else if (TREE_CODE (type) == UNION_TYPE)
3713 {
3714 tree field;
0c16ddf7
RS
3715 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3716 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3717 value = default_conversion (value);
3718
400fbf9f
JW
3719 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3720 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3721 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3722 break;
3723
3724 if (field)
3725 {
805f961c 3726 char *name;
281ec92f 3727 tree t;
805f961c 3728
400fbf9f
JW
3729 if (pedantic)
3730 pedwarn ("ANSI C forbids casts to union type");
805f961c
RS
3731 if (TYPE_NAME (type) != 0)
3732 {
3733 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3734 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3735 else
3736 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3737 }
3738 else
3739 name = "";
281ec92f
RS
3740 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3741 build_tree_list (field, value)),
3742 0, 0);
3743 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3744 return t;
400fbf9f
JW
3745 }
3746 error ("cast to union type from type not present in union");
3747 return error_mark_node;
3748 }
3749 else
3750 {
10d5caec 3751 tree otype, ovalue;
53b01f59
RS
3752
3753 /* If casting to void, avoid the error that would come
3754 from default_conversion in the case of a non-lvalue array. */
3755 if (type == void_type_node)
3756 return build1 (CONVERT_EXPR, type, value);
3757
400fbf9f
JW
3758 /* Convert functions and arrays to pointers,
3759 but don't convert any other types. */
3760 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3761 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3762 value = default_conversion (value);
3763 otype = TREE_TYPE (value);
3764
d45cf215 3765 /* Optionally warn about potentially worrisome casts. */
400fbf9f
JW
3766
3767 if (warn_cast_qual
3768 && TREE_CODE (type) == POINTER_TYPE
3769 && TREE_CODE (otype) == POINTER_TYPE)
3770 {
f5963e61
JL
3771 /* Go to the innermost object being pointed to. */
3772 tree in_type = type;
3773 tree in_otype = otype;
3774
3775 while (TREE_CODE (in_type) == POINTER_TYPE)
3776 in_type = TREE_TYPE (in_type);
3777 while (TREE_CODE (in_otype) == POINTER_TYPE)
3778 in_otype = TREE_TYPE (in_otype);
3932261a
MM
3779
3780 if (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type))
3781 /* There are qualifiers present in IN_OTYPE that are not
3782 present in IN_TYPE. */
3783 pedwarn ("cast discards qualifiers from pointer target type");
400fbf9f
JW
3784 }
3785
3786 /* Warn about possible alignment problems. */
d45cf215 3787 if (STRICT_ALIGNMENT && warn_cast_align
400fbf9f
JW
3788 && TREE_CODE (type) == POINTER_TYPE
3789 && TREE_CODE (otype) == POINTER_TYPE
3790 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3791 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
ec9aa895
RK
3792 /* Don't warn about opaque types, where the actual alignment
3793 restriction is unknown. */
3794 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3795 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3796 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
400fbf9f
JW
3797 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3798 warning ("cast increases required alignment of target type");
400fbf9f
JW
3799
3800 if (TREE_CODE (type) == INTEGER_TYPE
3801 && TREE_CODE (otype) == POINTER_TYPE
c9b7f31c
RS
3802 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3803 && !TREE_CONSTANT (value))
400fbf9f
JW
3804 warning ("cast from pointer to integer of different size");
3805
796bb373
RK
3806 if (warn_bad_function_cast
3807 && TREE_CODE (value) == CALL_EXPR
3808 && TREE_CODE (type) != TREE_CODE (otype))
3809 warning ("cast does not match function type");
3810
400fbf9f
JW
3811 if (TREE_CODE (type) == POINTER_TYPE
3812 && TREE_CODE (otype) == INTEGER_TYPE
2918ed3c 3813 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
c9b7f31c 3814#if 0
2918ed3c
RS
3815 /* Don't warn about converting 0 to pointer,
3816 provided the 0 was explicit--not cast or made by folding. */
c9b7f31c
RS
3817 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3818#endif
3819 /* Don't warn about converting any constant. */
3820 && !TREE_CONSTANT (value))
400fbf9f
JW
3821 warning ("cast to pointer from integer of different size");
3822
10d5caec 3823 ovalue = value;
400fbf9f 3824 value = convert (type, value);
e58cd767
RS
3825
3826 /* Ignore any integer overflow caused by the cast. */
3827 if (TREE_CODE (value) == INTEGER_CST)
10d5caec
PE
3828 {
3829 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3830 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3831 }
400fbf9f
JW
3832 }
3833
fd5d5b94
RS
3834 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3835 if (pedantic && TREE_CODE (value) == INTEGER_CST
3836 && TREE_CODE (expr) == INTEGER_CST
3837 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3838 value = non_lvalue (value);
3839
3840 /* If pedantic, don't let a cast be an lvalue. */
400fbf9f 3841 if (value == expr && pedantic)
fd5d5b94
RS
3842 value = non_lvalue (value);
3843
400fbf9f
JW
3844 return value;
3845}
3846\f
3847/* Build an assignment expression of lvalue LHS from value RHS.
3848 MODIFYCODE is the code for a binary operator that we use
3849 to combine the old value of LHS with RHS to get the new value.
3850 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3851
3852tree
3853build_modify_expr (lhs, modifycode, rhs)
3854 tree lhs, rhs;
3855 enum tree_code modifycode;
3856{
3857 register tree result;
3858 tree newrhs;
3859 tree lhstype = TREE_TYPE (lhs);
3860 tree olhstype = lhstype;
3861
3862 /* Types that aren't fully specified cannot be used in assignments. */
3863 lhs = require_complete_type (lhs);
3864
3865 /* Avoid duplicate error messages from operands that had errors. */
3866 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3867 return error_mark_node;
3868
3869 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
3870 /* Do not use STRIP_NOPS here. We do not want an enumerator
3871 whose value is 0 to count as a null pointer constant. */
400fbf9f
JW
3872 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3873 rhs = TREE_OPERAND (rhs, 0);
3874
3875 newrhs = rhs;
3876
3877 /* Handle control structure constructs used as "lvalues". */
3878
3879 switch (TREE_CODE (lhs))
3880 {
3881 /* Handle (a, b) used as an "lvalue". */
3882 case COMPOUND_EXPR:
3883 pedantic_lvalue_warning (COMPOUND_EXPR);
19d76e60
RK
3884 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3885 modifycode, rhs);
3886 if (TREE_CODE (newrhs) == ERROR_MARK)
3887 return error_mark_node;
400fbf9f 3888 return build (COMPOUND_EXPR, lhstype,
19d76e60
RK
3889 TREE_OPERAND (lhs, 0), newrhs);
3890
400fbf9f
JW
3891 /* Handle (a ? b : c) used as an "lvalue". */
3892 case COND_EXPR:
3893 pedantic_lvalue_warning (COND_EXPR);
3894 rhs = save_expr (rhs);
3895 {
3896 /* Produce (a ? (b = rhs) : (c = rhs))
3897 except that the RHS goes through a save-expr
3898 so the code to compute it is only emitted once. */
3899 tree cond
3900 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3901 build_modify_expr (TREE_OPERAND (lhs, 1),
3902 modifycode, rhs),
3903 build_modify_expr (TREE_OPERAND (lhs, 2),
3904 modifycode, rhs));
19d76e60
RK
3905 if (TREE_CODE (cond) == ERROR_MARK)
3906 return cond;
400fbf9f
JW
3907 /* Make sure the code to compute the rhs comes out
3908 before the split. */
3909 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3910 /* But cast it to void to avoid an "unused" error. */
3911 convert (void_type_node, rhs), cond);
3912 }
e9a25f70
JL
3913 default:
3914 break;
400fbf9f
JW
3915 }
3916
3917 /* If a binary op has been requested, combine the old LHS value with the RHS
3918 producing the value we should actually store into the LHS. */
3919
3920 if (modifycode != NOP_EXPR)
3921 {
3922 lhs = stabilize_reference (lhs);
3923 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3924 }
3925
3926 /* Handle a cast used as an "lvalue".
3927 We have already performed any binary operator using the value as cast.
3928 Now convert the result to the cast type of the lhs,
3929 and then true type of the lhs and store it there;
3930 then convert result back to the cast type to be the value
3931 of the assignment. */
3932
3933 switch (TREE_CODE (lhs))
3934 {
3935 case NOP_EXPR:
3936 case CONVERT_EXPR:
3937 case FLOAT_EXPR:
3938 case FIX_TRUNC_EXPR:
3939 case FIX_FLOOR_EXPR:
3940 case FIX_ROUND_EXPR:
3941 case FIX_CEIL_EXPR:
3942 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3943 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3944 newrhs = default_conversion (newrhs);
3945 {
3946 tree inner_lhs = TREE_OPERAND (lhs, 0);
3947 tree result;
3948 result = build_modify_expr (inner_lhs, NOP_EXPR,
3949 convert (TREE_TYPE (inner_lhs),
3950 convert (lhstype, newrhs)));
19d76e60
RK
3951 if (TREE_CODE (result) == ERROR_MARK)
3952 return result;
400fbf9f
JW
3953 pedantic_lvalue_warning (CONVERT_EXPR);
3954 return convert (TREE_TYPE (lhs), result);
3955 }
e9a25f70
JL
3956
3957 default:
3958 break;
400fbf9f
JW
3959 }
3960
3961 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3962 Reject anything strange now. */
3963
ab87f8c8 3964 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
400fbf9f
JW
3965 return error_mark_node;
3966
3967 /* Warn about storing in something that is `const'. */
3968
3969 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3970 || ((TREE_CODE (lhstype) == RECORD_TYPE
3971 || TREE_CODE (lhstype) == UNION_TYPE)
3972 && C_TYPE_FIELDS_READONLY (lhstype)))
3973 readonly_warning (lhs, "assignment");
3974
3975 /* If storing into a structure or union member,
3976 it has probably been given type `int'.
3977 Compute the type that would go with
3978 the actual amount of storage the member occupies. */
3979
3980 if (TREE_CODE (lhs) == COMPONENT_REF
3981 && (TREE_CODE (lhstype) == INTEGER_TYPE
3982 || TREE_CODE (lhstype) == REAL_TYPE
3983 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3984 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3985
3986 /* If storing in a field that is in actuality a short or narrower than one,
3987 we must store in the field in its actual type. */
3988
3989 if (lhstype != TREE_TYPE (lhs))
3990 {
3991 lhs = copy_node (lhs);
3992 TREE_TYPE (lhs) = lhstype;
3993 }
3994
3995 /* Convert new value to destination type. */
3996
ab87f8c8 3997 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
9b7267b8 3998 NULL_TREE, NULL_TREE, 0);
400fbf9f
JW
3999 if (TREE_CODE (newrhs) == ERROR_MARK)
4000 return error_mark_node;
4001
4002 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4003 TREE_SIDE_EFFECTS (result) = 1;
4004
4005 /* If we got the LHS in a different type for storing in,
4006 convert the result back to the nominal type of LHS
4007 so that the value we return always has the same type
4008 as the LHS argument. */
4009
4010 if (olhstype == TREE_TYPE (result))
4011 return result;
ab87f8c8 4012 return convert_for_assignment (olhstype, result, _("assignment"),
9b7267b8 4013 NULL_TREE, NULL_TREE, 0);
400fbf9f
JW
4014}
4015\f
4016/* Convert value RHS to type TYPE as preparation for an assignment
4017 to an lvalue of type TYPE.
4018 The real work of conversion is done by `convert'.
4019 The purpose of this function is to generate error messages
4020 for assignments that are not allowed in C.
4021 ERRTYPE is a string to use in error messages:
4022 "assignment", "return", etc. If it is null, this is parameter passing
ab87f8c8 4023 for a function call (and different error messages are output).
400fbf9f
JW
4024
4025 FUNNAME is the name of the function being called,
4026 as an IDENTIFIER_NODE, or null.
4027 PARMNUM is the number of the argument, for printing in error messages. */
4028
4029static tree
9b7267b8 4030convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
400fbf9f
JW
4031 tree type, rhs;
4032 char *errtype;
9b7267b8 4033 tree fundecl, funname;
400fbf9f
JW
4034 int parmnum;
4035{
4036 register enum tree_code codel = TREE_CODE (type);
4037 register tree rhstype;
4038 register enum tree_code coder;
4039
4040 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
4041 /* Do not use STRIP_NOPS here. We do not want an enumerator
4042 whose value is 0 to count as a null pointer constant. */
400fbf9f
JW
4043 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4044 rhs = TREE_OPERAND (rhs, 0);
4045
4046 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4047 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4048 rhs = default_conversion (rhs);
8c3a6477
RK
4049 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4050 rhs = decl_constant_value (rhs);
400fbf9f
JW
4051
4052 rhstype = TREE_TYPE (rhs);
4053 coder = TREE_CODE (rhstype);
4054
4055 if (coder == ERROR_MARK)
4056 return error_mark_node;
4057
4058 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
e58cd767
RS
4059 {
4060 overflow_warning (rhs);
8b40563c
TW
4061 /* Check for Objective-C protocols. This will issue a warning if
4062 there are protocol violations. No need to use the return value. */
4063 maybe_objc_comptypes (type, rhstype, 0);
e58cd767
RS
4064 return rhs;
4065 }
400fbf9f
JW
4066
4067 if (coder == VOID_TYPE)
4068 {
4069 error ("void value not ignored as it ought to be");
4070 return error_mark_node;
4071 }
4072 /* Arithmetic types all interconvert, and enum is treated like int. */
b6a10c9f
RS
4073 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
4074 || codel == COMPLEX_TYPE)
61179109
RK
4075 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
4076 || coder == COMPLEX_TYPE))
da3c6115 4077 return convert_and_check (type, rhs);
61179109 4078
7e842ef8
PE
4079 /* Conversion to a transparent union from its member types.
4080 This applies only to function arguments. */
4081 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4082 {
4083 tree memb_types;
4084 tree marginal_memb_type = 0;
4085
4086 for (memb_types = TYPE_FIELDS (type); memb_types;
4087 memb_types = TREE_CHAIN (memb_types))
4088 {
4089 tree memb_type = TREE_TYPE (memb_types);
4090
4091 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4092 TYPE_MAIN_VARIANT (rhstype)))
4093 break;
4094
4095 if (TREE_CODE (memb_type) != POINTER_TYPE)
4096 continue;
4097
4098 if (coder == POINTER_TYPE)
4099 {
4100 register tree ttl = TREE_TYPE (memb_type);
4101 register tree ttr = TREE_TYPE (rhstype);
4102
4103 /* Any non-function converts to a [const][volatile] void *
4104 and vice versa; otherwise, targets must be the same.
4105 Meanwhile, the lhs target must have all the qualifiers of
4106 the rhs. */
4107 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4108 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4109 || comp_target_types (memb_type, rhstype))
4110 {
4111 /* If this type won't generate any warnings, use it. */
3932261a
MM
4112 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4113 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4114 && TREE_CODE (ttl) == FUNCTION_TYPE)
4115 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4116 == TYPE_QUALS (ttr))
b58c9a79 4117 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3932261a 4118 == TYPE_QUALS (ttl))))
7e842ef8
PE
4119 break;
4120
4121 /* Keep looking for a better type, but remember this one. */
4122 if (! marginal_memb_type)
4123 marginal_memb_type = memb_type;
4124 }
4125 }
4126
4127 /* Can convert integer zero to any pointer type. */
4128 if (integer_zerop (rhs)
4129 || (TREE_CODE (rhs) == NOP_EXPR
4130 && integer_zerop (TREE_OPERAND (rhs, 0))))
4131 {
4132 rhs = null_pointer_node;
4133 break;
4134 }
4135 }
4136
4137 if (memb_types || marginal_memb_type)
4138 {
4139 if (! memb_types)
4140 {
4141 /* We have only a marginally acceptable member type;
0f41302f 4142 it needs a warning. */
7e842ef8
PE
4143 register tree ttl = TREE_TYPE (marginal_memb_type);
4144 register tree ttr = TREE_TYPE (rhstype);
4145
4146 /* Const and volatile mean something different for function
4147 types, so the usual warnings are not appropriate. */
4148 if (TREE_CODE (ttr) == FUNCTION_TYPE
4149 && TREE_CODE (ttl) == FUNCTION_TYPE)
4150 {
4151 /* Because const and volatile on functions are
4152 restrictions that say the function will not do
4153 certain things, it is okay to use a const or volatile
4154 function where an ordinary one is wanted, but not
4155 vice-versa. */
3932261a
MM
4156 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4157 warn_for_assignment ("%s makes qualified function pointer from unqualified",
ab87f8c8 4158 errtype, funname, parmnum);
7e842ef8 4159 }
3932261a
MM
4160 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4161 warn_for_assignment ("%s discards qualifiers from pointer target type",
ab87f8c8 4162 errtype, funname,
3932261a 4163 parmnum);
7e842ef8
PE
4164 }
4165
4166 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4167 pedwarn ("ANSI C prohibits argument conversion to union type");
4168
4169 return build1 (NOP_EXPR, type, rhs);
4170 }
4171 }
4172
400fbf9f
JW
4173 /* Conversions among pointers */
4174 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4175 {
4176 register tree ttl = TREE_TYPE (type);
4177 register tree ttr = TREE_TYPE (rhstype);
4178
4179 /* Any non-function converts to a [const][volatile] void *
4180 and vice versa; otherwise, targets must be the same.
4181 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4182 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4183 || TYPE_MAIN_VARIANT (ttr) == void_type_node
790e9490
RS
4184 || comp_target_types (type, rhstype)
4185 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4186 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
400fbf9f
JW
4187 {
4188 if (pedantic
4189 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4190 && TREE_CODE (ttr) == FUNCTION_TYPE)
4191 ||
4192 (TYPE_MAIN_VARIANT (ttr) == void_type_node
fd5d5b94
RS
4193 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4194 which are not ANSI null ptr constants. */
4195 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
400fbf9f
JW
4196 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4197 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
ab87f8c8 4198 errtype, funname, parmnum);
400fbf9f
JW
4199 /* Const and volatile mean something different for function types,
4200 so the usual warnings are not appropriate. */
4201 else if (TREE_CODE (ttr) != FUNCTION_TYPE
caf2e8e4 4202 && TREE_CODE (ttl) != FUNCTION_TYPE)
400fbf9f 4203 {
3932261a
MM
4204 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4205 warn_for_assignment ("%s discards qualifiers from pointer target type",
ab87f8c8 4206 errtype, funname, parmnum);
790e9490
RS
4207 /* If this is not a case of ignoring a mismatch in signedness,
4208 no warning. */
4209 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4210 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4211 || comp_target_types (type, rhstype))
4212 ;
4213 /* If there is a mismatch, do warn. */
4214 else if (pedantic)
4215 warn_for_assignment ("pointer targets in %s differ in signedness",
ab87f8c8 4216 errtype, funname, parmnum);
400fbf9f 4217 }
d949d5df
RK
4218 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4219 && TREE_CODE (ttr) == FUNCTION_TYPE)
400fbf9f
JW
4220 {
4221 /* Because const and volatile on functions are restrictions
4222 that say the function will not do certain things,
4223 it is okay to use a const or volatile function
4224 where an ordinary one is wanted, but not vice-versa. */
3932261a
MM
4225 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4226 warn_for_assignment ("%s makes qualified function pointer from unqualified",
ab87f8c8 4227 errtype, funname, parmnum);
400fbf9f
JW
4228 }
4229 }
400fbf9f
JW
4230 else
4231 warn_for_assignment ("%s from incompatible pointer type",
ab87f8c8 4232 errtype, funname, parmnum);
400fbf9f
JW
4233 return convert (type, rhs);
4234 }
4235 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4236 {
2918ed3c 4237 /* An explicit constant 0 can convert to a pointer,
f1a2b955
RS
4238 or one that results from arithmetic, even including
4239 a cast to integer type. */
4240 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4241 &&
4242 ! (TREE_CODE (rhs) == NOP_EXPR
4243 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4244 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4245 && integer_zerop (TREE_OPERAND (rhs, 0))))
400fbf9f
JW
4246 {
4247 warn_for_assignment ("%s makes pointer from integer without a cast",
ab87f8c8 4248 errtype, funname, parmnum);
400fbf9f
JW
4249 return convert (type, rhs);
4250 }
4251 return null_pointer_node;
4252 }
4253 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4254 {
4255 warn_for_assignment ("%s makes integer from pointer without a cast",
ab87f8c8 4256 errtype, funname, parmnum);
400fbf9f
JW
4257 return convert (type, rhs);
4258 }
4259
4260 if (!errtype)
4261 {
4262 if (funname)
8b40563c
TW
4263 {
4264 tree selector = maybe_building_objc_message_expr ();
4265
4266 if (selector && parmnum > 2)
4267 error ("incompatible type for argument %d of `%s'",
4268 parmnum - 2, IDENTIFIER_POINTER (selector));
4269 else
4270 error ("incompatible type for argument %d of `%s'",
4271 parmnum, IDENTIFIER_POINTER (funname));
4272 }
400fbf9f
JW
4273 else
4274 error ("incompatible type for argument %d of indirect function call",
4275 parmnum);
4276 }
4277 else
ab87f8c8 4278 error ("incompatible types in %s", errtype);
400fbf9f
JW
4279
4280 return error_mark_node;
4281}
4282
ab87f8c8 4283/* Print a warning using MSGID.
400fbf9f
JW
4284 It gets OPNAME as its one parameter.
4285 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4286 FUNCTION and ARGNUM are handled specially if we are building an
4287 Objective-C selector. */
4288
4289static void
ab87f8c8
JL
4290warn_for_assignment (msgid, opname, function, argnum)
4291 char *msgid;
400fbf9f
JW
4292 char *opname;
4293 tree function;
4294 int argnum;
4295{
400fbf9f
JW
4296 if (opname == 0)
4297 {
4298 tree selector = maybe_building_objc_message_expr ();
4299
4300 if (selector && argnum > 2)
4301 {
4302 function = selector;
4303 argnum -= 2;
4304 }
4305 if (function)
4306 {
4307 /* Function name is known; supply it. */
ab87f8c8 4308 char *argstring = _("passing arg %d of `%s'");
400fbf9f 4309 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
ab87f8c8 4310 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
400fbf9f
JW
4311 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4312 }
4313 else
4314 {
4315 /* Function name unknown (call through ptr); just give arg number. */
ab87f8c8
JL
4316 char *argnofun = _("passing arg %d of pointer to function");
4317 opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
400fbf9f
JW
4318 sprintf (opname, argnofun, argnum);
4319 }
4320 }
ab87f8c8 4321 pedwarn (msgid, opname);
400fbf9f
JW
4322}
4323\f
4324/* Return nonzero if VALUE is a valid constant-valued expression
4325 for use in initializing a static variable; one that can be an
4326 element of a "constant" initializer.
4327
4328 Return null_pointer_node if the value is absolute;
4329 if it is relocatable, return the variable that determines the relocation.
4330 We assume that VALUE has been folded as much as possible;
4331 therefore, we do not need to check for such things as
4332 arithmetic-combinations of integers. */
4333
ca1f6b57 4334tree
f0c70ef0 4335initializer_constant_valid_p (value, endtype)
400fbf9f 4336 tree value;
f0c70ef0 4337 tree endtype;
400fbf9f
JW
4338{
4339 switch (TREE_CODE (value))
4340 {
4341 case CONSTRUCTOR:
977e6fb5
MM
4342 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4343 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
d6d22108
JL
4344 && TREE_CONSTANT (value)
4345 && CONSTRUCTOR_ELTS (value))
75ddf8b0
RK
4346 return
4347 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4348 endtype);
281ec92f 4349
400fbf9f
JW
4350 return TREE_STATIC (value) ? null_pointer_node : 0;
4351
4352 case INTEGER_CST:
4353 case REAL_CST:
4354 case STRING_CST:
466e9220 4355 case COMPLEX_CST:
400fbf9f
JW
4356 return null_pointer_node;
4357
4358 case ADDR_EXPR:
4359 return TREE_OPERAND (value, 0);
4360
4361 case NON_LVALUE_EXPR:
f0c70ef0 4362 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
400fbf9f
JW
4363
4364 case CONVERT_EXPR:
4365 case NOP_EXPR:
4366 /* Allow conversions between pointer types. */
4367 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4368 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
f0c70ef0 4369 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
9c4dcbc7 4370
400fbf9f
JW
4371 /* Allow conversions between real types. */
4372 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4373 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
f0c70ef0 4374 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
9c4dcbc7 4375
400fbf9f
JW
4376 /* Allow length-preserving conversions between integer types. */
4377 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4378 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
9c4dcbc7
RK
4379 && (TYPE_PRECISION (TREE_TYPE (value))
4380 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
f0c70ef0 4381 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
9c4dcbc7
RK
4382
4383 /* Allow conversions between other integer types only if
4384 explicit value. */
400fbf9f
JW
4385 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4386 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4387 {
f0c70ef0
RS
4388 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4389 endtype);
400fbf9f
JW
4390 if (inner == null_pointer_node)
4391 return null_pointer_node;
4392 return 0;
4393 }
9c4dcbc7 4394
9b7267b8 4395 /* Allow (int) &foo provided int is as wide as a pointer. */
400fbf9f
JW
4396 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4397 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
9c4dcbc7
RK
4398 && (TYPE_PRECISION (TREE_TYPE (value))
4399 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4400 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4401 endtype);
4402
0d906a5f
JM
4403 /* Likewise conversions from int to pointers, but also allow
4404 conversions from 0. */
9c4dcbc7 4405 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
0d906a5f
JM
4406 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4407 {
4408 if (integer_zerop (TREE_OPERAND (value, 0)))
4409 return null_pointer_node;
4410 else if (TYPE_PRECISION (TREE_TYPE (value))
4411 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))
4412 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4413 endtype);
4414 }
9c4dcbc7 4415
805f961c
RS
4416 /* Allow conversions to union types if the value inside is okay. */
4417 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
f0c70ef0
RS
4418 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4419 endtype);
400fbf9f
JW
4420 return 0;
4421
4422 case PLUS_EXPR:
1bbe9280
RS
4423 if (TREE_CODE (endtype) == INTEGER_TYPE
4424 && TYPE_PRECISION (endtype) < POINTER_SIZE)
f0c70ef0 4425 return 0;
400fbf9f 4426 {
f0c70ef0
RS
4427 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4428 endtype);
4429 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4430 endtype);
400fbf9f
JW
4431 /* If either term is absolute, use the other terms relocation. */
4432 if (valid0 == null_pointer_node)
4433 return valid1;
4434 if (valid1 == null_pointer_node)
4435 return valid0;
4436 return 0;
4437 }
4438
4439 case MINUS_EXPR:
1bbe9280
RS
4440 if (TREE_CODE (endtype) == INTEGER_TYPE
4441 && TYPE_PRECISION (endtype) < POINTER_SIZE)
f0c70ef0 4442 return 0;
400fbf9f 4443 {
f0c70ef0
RS
4444 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4445 endtype);
4446 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4447 endtype);
400fbf9f
JW
4448 /* Win if second argument is absolute. */
4449 if (valid1 == null_pointer_node)
4450 return valid0;
4451 /* Win if both arguments have the same relocation.
4452 Then the value is absolute. */
4453 if (valid0 == valid1)
4454 return null_pointer_node;
4455 return 0;
4456 }
400fbf9f 4457
e9a25f70
JL
4458 default:
4459 return 0;
4460 }
400fbf9f 4461}
d9fc6069
JW
4462
4463/* If VALUE is a compound expr all of whose expressions are constant, then
4464 return its value. Otherwise, return error_mark_node.
4465
4466 This is for handling COMPOUND_EXPRs as initializer elements
4467 which is allowed with a warning when -pedantic is specified. */
4468
4469static tree
4470valid_compound_expr_initializer (value, endtype)
4471 tree value;
4472 tree endtype;
4473{
4474 if (TREE_CODE (value) == COMPOUND_EXPR)
4475 {
4476 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4477 == error_mark_node)
4478 return error_mark_node;
4479 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4480 endtype);
4481 }
4482 else if (! TREE_CONSTANT (value)
4483 && ! initializer_constant_valid_p (value, endtype))
4484 return error_mark_node;
4485 else
4486 return value;
4487}
400fbf9f
JW
4488\f
4489/* Perform appropriate conversions on the initial value of a variable,
4490 store it in the declaration DECL,
4491 and print any error messages that are appropriate.
4492 If the init is invalid, store an ERROR_MARK. */
4493
4494void
4495store_init_value (decl, init)
4496 tree decl, init;
4497{
4498 register tree value, type;
4499
4500 /* If variable's type was invalidly declared, just ignore it. */
4501
4502 type = TREE_TYPE (decl);
4503 if (TREE_CODE (type) == ERROR_MARK)
4504 return;
4505
4506 /* Digest the specified initializer into an expression. */
4507
790e9490
RS
4508 value = digest_init (type, init, TREE_STATIC (decl),
4509 TREE_STATIC (decl) || pedantic);
400fbf9f
JW
4510
4511 /* Store the expression if valid; else report error. */
4512
4513#if 0
4514 /* Note that this is the only place we can detect the error
4515 in a case such as struct foo bar = (struct foo) { x, y };
d45cf215 4516 where there is one initial value which is a constructor expression. */
400fbf9f
JW
4517 if (value == error_mark_node)
4518 ;
4519 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4520 {
4521 error ("initializer for static variable is not constant");
4522 value = error_mark_node;
4523 }
4524 else if (TREE_STATIC (decl)
f0c70ef0 4525 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
400fbf9f
JW
4526 {
4527 error ("initializer for static variable uses complicated arithmetic");
4528 value = error_mark_node;
4529 }
4530 else
4531 {
4532 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4533 {
4534 if (! TREE_CONSTANT (value))
4535 pedwarn ("aggregate initializer is not constant");
4536 else if (! TREE_STATIC (value))
4537 pedwarn ("aggregate initializer uses complicated arithmetic");
4538 }
4539 }
4540#endif
4541
10d5caec
PE
4542 DECL_INITIAL (decl) = value;
4543
26b3c423 4544 /* ANSI wants warnings about out-of-range constant initializers. */
10d5caec 4545 STRIP_TYPE_NOPS (value);
26b3c423 4546 constant_expression_warning (value);
400fbf9f
JW
4547}
4548\f
075fc632 4549/* Methods for storing and printing names for error messages. */
d45cf215
RS
4550
4551/* Implement a spelling stack that allows components of a name to be pushed
4552 and popped. Each element on the stack is this structure. */
4553
4554struct spelling
4555{
4556 int kind;
4557 union
4558 {
4559 int i;
4560 char *s;
4561 } u;
4562};
4563
4564#define SPELLING_STRING 1
4565#define SPELLING_MEMBER 2
4566#define SPELLING_BOUNDS 3
4567
4568static struct spelling *spelling; /* Next stack element (unused). */
4569static struct spelling *spelling_base; /* Spelling stack base. */
4570static int spelling_size; /* Size of the spelling stack. */
4571
4572/* Macros to save and restore the spelling stack around push_... functions.
4573 Alternative to SAVE_SPELLING_STACK. */
4574
4575#define SPELLING_DEPTH() (spelling - spelling_base)
4576#define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4577
4578/* Save and restore the spelling stack around arbitrary C code. */
4579
4580#define SAVE_SPELLING_DEPTH(code) \
4581{ \
4582 int __depth = SPELLING_DEPTH (); \
4583 code; \
4584 RESTORE_SPELLING_DEPTH (__depth); \
4585}
4586
4587/* Push an element on the spelling stack with type KIND and assign VALUE
4588 to MEMBER. */
4589
4590#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4591{ \
4592 int depth = SPELLING_DEPTH (); \
4593 \
4594 if (depth >= spelling_size) \
4595 { \
4596 spelling_size += 10; \
4597 if (spelling_base == 0) \
4598 spelling_base \
4599 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4600 else \
4601 spelling_base \
4602 = (struct spelling *) xrealloc (spelling_base, \
4603 spelling_size * sizeof (struct spelling)); \
4604 RESTORE_SPELLING_DEPTH (depth); \
4605 } \
4606 \
4607 spelling->kind = (KIND); \
4608 spelling->MEMBER = (VALUE); \
4609 spelling++; \
4610}
4611
4612/* Push STRING on the stack. Printed literally. */
4613
4614static void
4615push_string (string)
4616 char *string;
4617{
4618 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4619}
4620
4621/* Push a member name on the stack. Printed as '.' STRING. */
4622
4623static void
19d76e60
RK
4624push_member_name (decl)
4625 tree decl;
4626
d45cf215 4627{
19d76e60
RK
4628 char *string
4629 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
d45cf215
RS
4630 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4631}
4632
4633/* Push an array bounds on the stack. Printed as [BOUNDS]. */
4634
4635static void
4636push_array_bounds (bounds)
4637 int bounds;
4638{
4639 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4640}
4641
4642/* Compute the maximum size in bytes of the printed spelling. */
4643
4644static int
4645spelling_length ()
4646{
4647 register int size = 0;
4648 register struct spelling *p;
4649
4650 for (p = spelling_base; p < spelling; p++)
4651 {
4652 if (p->kind == SPELLING_BOUNDS)
4653 size += 25;
4654 else
4655 size += strlen (p->u.s) + 1;
4656 }
4657
4658 return size;
4659}
4660
4661/* Print the spelling to BUFFER and return it. */
4662
4663static char *
4664print_spelling (buffer)
4665 register char *buffer;
4666{
4667 register char *d = buffer;
4668 register char *s;
4669 register struct spelling *p;
4670
4671 for (p = spelling_base; p < spelling; p++)
4672 if (p->kind == SPELLING_BOUNDS)
4673 {
4674 sprintf (d, "[%d]", p->u.i);
4675 d += strlen (d);
4676 }
4677 else
4678 {
4679 if (p->kind == SPELLING_MEMBER)
4680 *d++ = '.';
1d300e19 4681 for (s = p->u.s; (*d = *s++); d++)
d45cf215
RS
4682 ;
4683 }
4684 *d++ = '\0';
4685 return buffer;
4686}
4687
400fbf9f 4688/* Issue an error message for a bad initializer component.
ab87f8c8
JL
4689 MSGID identifies the message.
4690 The component name is taken from the spelling stack. */
400fbf9f
JW
4691
4692void
ab87f8c8
JL
4693error_init (msgid)
4694 char *msgid;
400fbf9f 4695{
ab87f8c8 4696 char *ofwhat;
400fbf9f 4697
ab87f8c8
JL
4698 error (msgid);
4699 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
400fbf9f 4700 if (*ofwhat)
ab87f8c8 4701 error ("(near initialization for `%s')", ofwhat);
400fbf9f
JW
4702}
4703
4704/* Issue a pedantic warning for a bad initializer component.
ab87f8c8
JL
4705 MSGID identifies the message.
4706 The component name is taken from the spelling stack. */
400fbf9f
JW
4707
4708void
ab87f8c8
JL
4709pedwarn_init (msgid)
4710 char *msgid;
400fbf9f 4711{
ab87f8c8 4712 char *ofwhat;
400fbf9f 4713
ab87f8c8
JL
4714 pedwarn (msgid);
4715 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
400fbf9f 4716 if (*ofwhat)
ab87f8c8 4717 pedwarn ("(near initialization for `%s')", ofwhat);
400fbf9f 4718}
b71c7f8a
RK
4719
4720/* Issue a warning for a bad initializer component.
ab87f8c8
JL
4721 MSGID identifies the message.
4722 The component name is taken from the spelling stack. */
b71c7f8a
RK
4723
4724static void
ab87f8c8
JL
4725warning_init (msgid)
4726 char *msgid;
b71c7f8a 4727{
ab87f8c8 4728 char *ofwhat;
b71c7f8a 4729
ab87f8c8
JL
4730 warning (msgid);
4731 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
b71c7f8a 4732 if (*ofwhat)
ab87f8c8 4733 warning ("(near initialization for `%s')", ofwhat);
b71c7f8a 4734}
400fbf9f
JW
4735\f
4736/* Digest the parser output INIT as an initializer for type TYPE.
4737 Return a C expression of type TYPE to represent the initial value.
4738
400fbf9f
JW
4739 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4740 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
59b22f64 4741 applies only to elements of constructors. */
400fbf9f 4742
b62acd60 4743static tree
790e9490
RS
4744digest_init (type, init, require_constant, constructor_constant)
4745 tree type, init;
400fbf9f 4746 int require_constant, constructor_constant;
400fbf9f
JW
4747{
4748 enum tree_code code = TREE_CODE (type);
047de90b 4749 tree inside_init = init;
400fbf9f 4750
400fbf9f
JW
4751 if (init == error_mark_node)
4752 return init;
4753
4754 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
4755 /* Do not use STRIP_NOPS here. We do not want an enumerator
4756 whose value is 0 to count as a null pointer constant. */
400fbf9f 4757 if (TREE_CODE (init) == NON_LVALUE_EXPR)
047de90b 4758 inside_init = TREE_OPERAND (init, 0);
400fbf9f 4759
400fbf9f
JW
4760 /* Initialization of an array of chars from a string constant
4761 optionally enclosed in braces. */
4762
4763 if (code == ARRAY_TYPE)
4764 {
4765 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4766 if ((typ1 == char_type_node
4767 || typ1 == signed_char_type_node
4768 || typ1 == unsigned_char_type_node
4769 || typ1 == unsigned_wchar_type_node
4770 || typ1 == signed_wchar_type_node)
fd5d5b94 4771 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
400fbf9f 4772 {
4d65300e
RS
4773 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4774 TYPE_MAIN_VARIANT (type)))
fd5d5b94 4775 return inside_init;
d11fdb45 4776
fd5d5b94 4777 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
400fbf9f
JW
4778 != char_type_node)
4779 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4780 {
ab87f8c8 4781 error_init ("char-array initialized from wide string");
400fbf9f
JW
4782 return error_mark_node;
4783 }
fd5d5b94 4784 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
400fbf9f
JW
4785 == char_type_node)
4786 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4787 {
ab87f8c8 4788 error_init ("int-array initialized from non-wide string");
400fbf9f
JW
4789 return error_mark_node;
4790 }
4791
fd5d5b94 4792 TREE_TYPE (inside_init) = type;
400fbf9f
JW
4793 if (TYPE_DOMAIN (type) != 0
4794 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4795 {
4796 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4797 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
fe9ef5d7
RS
4798 /* Subtract 1 (or sizeof (wchar_t))
4799 because it's ok to ignore the terminating null char
400fbf9f 4800 that is counted in the length of the constant. */
fd5d5b94 4801 if (size < TREE_STRING_LENGTH (inside_init)
fe9ef5d7
RS
4802 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4803 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4804 : 1))
ab87f8c8 4805 pedwarn_init ("initializer-string for array of chars is too long");
400fbf9f 4806 }
fd5d5b94 4807 return inside_init;
400fbf9f
JW
4808 }
4809 }
4810
de520661
RS
4811 /* Any type can be initialized
4812 from an expression of the same type, optionally with braces. */
400fbf9f 4813
2726966d 4814 if (inside_init && TREE_TYPE (inside_init) != 0
5522c047
PB
4815 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4816 TYPE_MAIN_VARIANT (type))
2726966d 4817 || (code == ARRAY_TYPE
3c3fa147
RS
4818 && comptypes (TREE_TYPE (inside_init), type))
4819 || (code == POINTER_TYPE
3c3fa147
RS
4820 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4821 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4822 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4823 TREE_TYPE (type)))))
400fbf9f
JW
4824 {
4825 if (code == POINTER_TYPE
047de90b
RS
4826 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4827 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4828 inside_init = default_conversion (inside_init);
de520661
RS
4829 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4830 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 4831 {
ab87f8c8 4832 error_init ("array initialized from non-constant array expression");
400fbf9f
JW
4833 return error_mark_node;
4834 }
4835
8c3a6477 4836 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
047de90b 4837 inside_init = decl_constant_value (inside_init);
400fbf9f 4838
d9fc6069
JW
4839 /* Compound expressions can only occur here if -pedantic or
4840 -pedantic-errors is specified. In the later case, we always want
4841 an error. In the former case, we simply want a warning. */
4842 if (require_constant && pedantic
4843 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4844 {
4845 inside_init
4846 = valid_compound_expr_initializer (inside_init,
4847 TREE_TYPE (inside_init));
4848 if (inside_init == error_mark_node)
ab87f8c8 4849 error_init ("initializer element is not constant");
d9fc6069 4850 else
ab87f8c8 4851 pedwarn_init ("initializer element is not constant");
d9fc6069
JW
4852 if (flag_pedantic_errors)
4853 inside_init = error_mark_node;
4854 }
4855 else if (require_constant && ! TREE_CONSTANT (inside_init))
400fbf9f 4856 {
ab87f8c8 4857 error_init ("initializer element is not constant");
047de90b 4858 inside_init = error_mark_node;
400fbf9f 4859 }
f0c70ef0
RS
4860 else if (require_constant
4861 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
400fbf9f 4862 {
ab87f8c8 4863 error_init ("initializer element is not computable at load time");
047de90b 4864 inside_init = error_mark_node;
400fbf9f
JW
4865 }
4866
047de90b 4867 return inside_init;
400fbf9f
JW
4868 }
4869
400fbf9f
JW
4870 /* Handle scalar types, including conversions. */
4871
4872 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
337633f9 4873 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
400fbf9f 4874 {
e3a12f0c
RS
4875 /* Note that convert_for_assignment calls default_conversion
4876 for arrays and functions. We must not call it in the
4877 case where inside_init is a null pointer constant. */
4878 inside_init
ab87f8c8 4879 = convert_for_assignment (type, init, _("initialization"),
e3a12f0c 4880 NULL_TREE, NULL_TREE, 0);
400fbf9f 4881
047de90b 4882 if (require_constant && ! TREE_CONSTANT (inside_init))
400fbf9f 4883 {
ab87f8c8 4884 error_init ("initializer element is not constant");
047de90b 4885 inside_init = error_mark_node;
400fbf9f 4886 }
f0c70ef0
RS
4887 else if (require_constant
4888 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
400fbf9f 4889 {
ab87f8c8 4890 error_init ("initializer element is not computable at load time");
047de90b 4891 inside_init = error_mark_node;
400fbf9f
JW
4892 }
4893
047de90b 4894 return inside_init;
400fbf9f
JW
4895 }
4896
4897 /* Come here only for records and arrays. */
4898
4899 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4900 {
ab87f8c8 4901 error_init ("variable-sized object may not be initialized");
400fbf9f
JW
4902 return error_mark_node;
4903 }
4904
81a55c6c
RS
4905 /* Traditionally, you can write struct foo x = 0;
4906 and it initializes the first element of x to 0. */
4907 if (flag_traditional)
4908 {
6c99c37b 4909 tree top = 0, prev = 0, otype = type;
81a55c6c
RS
4910 while (TREE_CODE (type) == RECORD_TYPE
4911 || TREE_CODE (type) == ARRAY_TYPE
4912 || TREE_CODE (type) == QUAL_UNION_TYPE
4913 || TREE_CODE (type) == UNION_TYPE)
4914 {
4915 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4916 if (prev == 0)
4917 top = temp;
4918 else
4919 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4920 prev = temp;
4921 if (TREE_CODE (type) == ARRAY_TYPE)
4922 type = TREE_TYPE (type);
4923 else if (TYPE_FIELDS (type))
4924 type = TREE_TYPE (TYPE_FIELDS (type));
4925 else
4926 {
ab87f8c8 4927 error_init ("invalid initializer");
81a55c6c
RS
4928 return error_mark_node;
4929 }
4930 }
6c99c37b
RK
4931
4932 if (otype != type)
4933 {
4934 TREE_OPERAND (prev, 1)
4935 = build_tree_list (NULL_TREE,
4936 digest_init (type, init, require_constant,
4937 constructor_constant));
4938 return top;
4939 }
4940 else
4941 return error_mark_node;
81a55c6c 4942 }
ab87f8c8 4943 error_init ("invalid initializer");
400fbf9f
JW
4944 return error_mark_node;
4945}
4946\f
de520661 4947/* Handle initializers that use braces. */
400fbf9f 4948
de520661
RS
4949/* Type of object we are accumulating a constructor for.
4950 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4951static tree constructor_type;
400fbf9f 4952
de520661
RS
4953/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4954 left to fill. */
4955static tree constructor_fields;
400fbf9f 4956
de520661
RS
4957/* For an ARRAY_TYPE, this is the specified index
4958 at which to store the next element we get.
4959 This is a special INTEGER_CST node that we modify in place. */
4960static tree constructor_index;
400fbf9f 4961
de520661 4962/* For an ARRAY_TYPE, this is the end index of the range
ddd5a7c1 4963 to initialize with the next element, or NULL in the ordinary case
de520661
RS
4964 where the element is used just once. */
4965static tree constructor_range_end;
400fbf9f 4966
de520661
RS
4967/* For an ARRAY_TYPE, this is the maximum index. */
4968static tree constructor_max_index;
103b7b17 4969
de520661
RS
4970/* For a RECORD_TYPE, this is the first field not yet written out. */
4971static tree constructor_unfilled_fields;
400fbf9f 4972
de520661
RS
4973/* For an ARRAY_TYPE, this is the index of the first element
4974 not yet written out.
4975 This is a special INTEGER_CST node that we modify in place. */
4976static tree constructor_unfilled_index;
4977
b62acd60
RS
4978/* In a RECORD_TYPE, the byte index of the next consecutive field.
4979 This is so we can generate gaps between fields, when appropriate.
4980 This is a special INTEGER_CST node that we modify in place. */
4981static tree constructor_bit_index;
4982
de520661
RS
4983/* If we are saving up the elements rather than allocating them,
4984 this is the list of elements so far (in reverse order,
4985 most recent first). */
4986static tree constructor_elements;
4987
4988/* 1 if so far this constructor's elements are all compile-time constants. */
4989static int constructor_constant;
4990
4991/* 1 if so far this constructor's elements are all valid address constants. */
4992static int constructor_simple;
4993
4994/* 1 if this constructor is erroneous so far. */
4995static int constructor_erroneous;
4996
4997/* 1 if have called defer_addressed_constants. */
4998static int constructor_subconstants_deferred;
4999
e5e809f4
JL
5000/* Structure for managing pending initializer elements, organized as an
5001 AVL tree. */
5002
5003struct init_node
5004{
5005 struct init_node *left, *right;
5006 struct init_node *parent;
5007 int balance;
5008 tree purpose;
5009 tree value;
5010};
5011
5012/* Tree of pending elements at this constructor level.
de520661
RS
5013 These are elements encountered out of order
5014 which belong at places we haven't reached yet in actually
5015 writing the output. */
e5e809f4 5016static struct init_node *constructor_pending_elts;
de520661
RS
5017
5018/* The SPELLING_DEPTH of this constructor. */
5019static int constructor_depth;
5020
cc77d4d5 5021/* 0 if implicitly pushing constructor levels is allowed. */
0f41302f 5022int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
cc77d4d5 5023
de520661
RS
5024static int require_constant_value;
5025static int require_constant_elements;
5026
5027/* 1 if it is ok to output this constructor as we read it.
5028 0 means must accumulate a CONSTRUCTOR expression. */
5029static int constructor_incremental;
5030
5031/* DECL node for which an initializer is being read.
5032 0 means we are reading a constructor expression
5033 such as (struct foo) {...}. */
5034static tree constructor_decl;
5035
5036/* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
5037static char *constructor_asmspec;
5038
5039/* Nonzero if this is an initializer for a top-level decl. */
5040static int constructor_top_level;
5041
b62acd60
RS
5042\f
5043/* This stack has a level for each implicit or explicit level of
5044 structuring in the initializer, including the outermost one. It
5045 saves the values of most of the variables above. */
de520661
RS
5046
5047struct constructor_stack
400fbf9f 5048{
de520661
RS
5049 struct constructor_stack *next;
5050 tree type;
5051 tree fields;
5052 tree index;
5053 tree range_end;
5054 tree max_index;
5055 tree unfilled_index;
5056 tree unfilled_fields;
b62acd60 5057 tree bit_index;
de520661
RS
5058 tree elements;
5059 int offset;
e5e809f4 5060 struct init_node *pending_elts;
de520661 5061 int depth;
790e9490
RS
5062 /* If nonzero, this value should replace the entire
5063 constructor at this level. */
5064 tree replacement_value;
de520661
RS
5065 char constant;
5066 char simple;
5067 char implicit;
5068 char incremental;
5069 char erroneous;
5070 char outer;
5071};
d45cf215 5072
de520661 5073struct constructor_stack *constructor_stack;
400fbf9f 5074
de520661
RS
5075/* This stack records separate initializers that are nested.
5076 Nested initializers can't happen in ANSI C, but GNU C allows them
5077 in cases like { ... (struct foo) { ... } ... }. */
400fbf9f 5078
de520661
RS
5079struct initializer_stack
5080{
5081 struct initializer_stack *next;
5082 tree decl;
5083 char *asmspec;
5084 struct constructor_stack *constructor_stack;
dea273df 5085 tree elements;
de520661
RS
5086 struct spelling *spelling;
5087 struct spelling *spelling_base;
5088 int spelling_size;
5089 char top_level;
5090 char incremental;
5091 char require_constant_value;
5092 char require_constant_elements;
5093 char deferred;
5094};
5095
5096struct initializer_stack *initializer_stack;
5097\f
5098/* Prepare to parse and output the initializer for variable DECL. */
5099
5100void
e28cae4f 5101start_init (decl, asmspec_tree, top_level)
de520661 5102 tree decl;
e28cae4f 5103 tree asmspec_tree;
de520661
RS
5104 int top_level;
5105{
5106 char *locus;
5107 struct initializer_stack *p
5108 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
e28cae4f
RS
5109 char *asmspec = 0;
5110
5111 if (asmspec_tree)
5112 asmspec = TREE_STRING_POINTER (asmspec_tree);
de520661
RS
5113
5114 p->decl = constructor_decl;
5115 p->asmspec = constructor_asmspec;
5116 p->incremental = constructor_incremental;
5117 p->require_constant_value = require_constant_value;
5118 p->require_constant_elements = require_constant_elements;
5119 p->constructor_stack = constructor_stack;
dea273df 5120 p->elements = constructor_elements;
de520661
RS
5121 p->spelling = spelling;
5122 p->spelling_base = spelling_base;
5123 p->spelling_size = spelling_size;
5124 p->deferred = constructor_subconstants_deferred;
5125 p->top_level = constructor_top_level;
b62acd60 5126 p->next = initializer_stack;
de520661
RS
5127 initializer_stack = p;
5128
5129 constructor_decl = decl;
5130 constructor_incremental = top_level;
5131 constructor_asmspec = asmspec;
5132 constructor_subconstants_deferred = 0;
5133 constructor_top_level = top_level;
5134
5135 if (decl != 0)
3c3fa147 5136 {
de520661 5137 require_constant_value = TREE_STATIC (decl);
f1a2b955
RS
5138 require_constant_elements
5139 = ((TREE_STATIC (decl) || pedantic)
5140 /* For a scalar, you can always use any value to initialize,
5141 even within braces. */
5142 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5143 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5144 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5145 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
de520661
RS
5146 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5147 constructor_incremental |= TREE_STATIC (decl);
3c3fa147 5148 }
400fbf9f 5149 else
de520661
RS
5150 {
5151 require_constant_value = 0;
5152 require_constant_elements = 0;
5153 locus = "(anonymous)";
5154 }
400fbf9f 5155
de520661 5156 constructor_stack = 0;
400fbf9f 5157
b71c7f8a
RK
5158 missing_braces_mentioned = 0;
5159
de520661
RS
5160 spelling_base = 0;
5161 spelling_size = 0;
5162 RESTORE_SPELLING_DEPTH (0);
d45cf215 5163
de520661
RS
5164 if (locus)
5165 push_string (locus);
5166}
400fbf9f 5167
de520661
RS
5168void
5169finish_init ()
5170{
5171 struct initializer_stack *p = initializer_stack;
400fbf9f 5172
de520661
RS
5173 /* Output subconstants (string constants, usually)
5174 that were referenced within this initializer and saved up.
5175 Must do this if and only if we called defer_addressed_constants. */
5176 if (constructor_subconstants_deferred)
5177 output_deferred_addressed_constants ();
4f77a31b 5178
de520661
RS
5179 /* Free the whole constructor stack of this initializer. */
5180 while (constructor_stack)
5181 {
5182 struct constructor_stack *q = constructor_stack;
5183 constructor_stack = q->next;
5184 free (q);
5185 }
400fbf9f 5186
de520661
RS
5187 /* Pop back to the data of the outer initializer (if any). */
5188 constructor_decl = p->decl;
5189 constructor_asmspec = p->asmspec;
5190 constructor_incremental = p->incremental;
5191 require_constant_value = p->require_constant_value;
5192 require_constant_elements = p->require_constant_elements;
5193 constructor_stack = p->constructor_stack;
dea273df 5194 constructor_elements = p->elements;
de520661
RS
5195 spelling = p->spelling;
5196 spelling_base = p->spelling_base;
5197 spelling_size = p->spelling_size;
5198 constructor_subconstants_deferred = p->deferred;
5199 constructor_top_level = p->top_level;
5200 initializer_stack = p->next;
5201 free (p);
5202}
5203\f
5204/* Call here when we see the initializer is surrounded by braces.
5205 This is instead of a call to push_init_level;
5206 it is matched by a call to pop_init_level.
400fbf9f 5207
de520661
RS
5208 TYPE is the type to initialize, for a constructor expression.
5209 For an initializer for a decl, TYPE is zero. */
5a7ec9d9 5210
de520661
RS
5211void
5212really_start_incremental_init (type)
5213 tree type;
5214{
5215 struct constructor_stack *p
5216 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5217
5218 if (type == 0)
5219 type = TREE_TYPE (constructor_decl);
5220
5221 /* Turn off constructor_incremental if type is a struct with bitfields.
5222 Do this before the first push, so that the corrected value
5223 is available in finish_init. */
5224 check_init_type_bitfields (type);
5225
5226 p->type = constructor_type;
5227 p->fields = constructor_fields;
5228 p->index = constructor_index;
5229 p->range_end = constructor_range_end;
5230 p->max_index = constructor_max_index;
5231 p->unfilled_index = constructor_unfilled_index;
5232 p->unfilled_fields = constructor_unfilled_fields;
b62acd60 5233 p->bit_index = constructor_bit_index;
5cb7368c 5234 p->elements = constructor_elements;
de520661
RS
5235 p->constant = constructor_constant;
5236 p->simple = constructor_simple;
5237 p->erroneous = constructor_erroneous;
5238 p->pending_elts = constructor_pending_elts;
5239 p->depth = constructor_depth;
790e9490 5240 p->replacement_value = 0;
de520661
RS
5241 p->implicit = 0;
5242 p->incremental = constructor_incremental;
5243 p->outer = 0;
5244 p->next = 0;
5245 constructor_stack = p;
5246
5247 constructor_constant = 1;
5248 constructor_simple = 1;
5249 constructor_depth = SPELLING_DEPTH ();
5250 constructor_elements = 0;
5251 constructor_pending_elts = 0;
5252 constructor_type = type;
5253
5254 if (TREE_CODE (constructor_type) == RECORD_TYPE
5255 || TREE_CODE (constructor_type) == UNION_TYPE)
5256 {
5257 constructor_fields = TYPE_FIELDS (constructor_type);
abc95ed3 5258 /* Skip any nameless bit fields at the beginning. */
ef86d2a6 5259 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
fc623854
RS
5260 && DECL_NAME (constructor_fields) == 0)
5261 constructor_fields = TREE_CHAIN (constructor_fields);
de520661 5262 constructor_unfilled_fields = constructor_fields;
b62acd60 5263 constructor_bit_index = copy_node (integer_zero_node);
f8dac6eb 5264 TREE_TYPE (constructor_bit_index) = sbitsizetype;
de520661
RS
5265 }
5266 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5267 {
de520661 5268 constructor_range_end = 0;
de520661 5269 if (TYPE_DOMAIN (constructor_type))
2bede729
PB
5270 {
5271 constructor_max_index
5272 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5273 constructor_index
5274 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5275 }
5276 else
5277 constructor_index = copy_node (integer_zero_node);
5278 constructor_unfilled_index = copy_node (constructor_index);
de520661
RS
5279 }
5280 else
5281 {
5282 /* Handle the case of int x = {5}; */
5283 constructor_fields = constructor_type;
5284 constructor_unfilled_fields = constructor_type;
5285 }
400fbf9f 5286
de520661
RS
5287 if (constructor_incremental)
5288 {
5289 int momentary = suspend_momentary ();
5290 push_obstacks_nochange ();
5291 if (TREE_PERMANENT (constructor_decl))
5292 end_temporary_allocation ();
5293 make_decl_rtl (constructor_decl, constructor_asmspec,
5294 constructor_top_level);
5295 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5296 pop_obstacks ();
5297 resume_momentary (momentary);
5298 }
400fbf9f 5299
de520661
RS
5300 if (constructor_incremental)
5301 {
5302 defer_addressed_constants ();
5303 constructor_subconstants_deferred = 1;
5304 }
5305}
5306\f
5307/* Push down into a subobject, for initialization.
5308 If this is for an explicit set of braces, IMPLICIT is 0.
5309 If it is because the next element belongs at a lower level,
5310 IMPLICIT is 1. */
400fbf9f 5311
de520661
RS
5312void
5313push_init_level (implicit)
5314 int implicit;
5315{
94ba5069
RS
5316 struct constructor_stack *p;
5317
5318 /* If we've exhausted any levels that didn't have braces,
5319 pop them now. */
5320 while (constructor_stack->implicit)
5321 {
5322 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5323 || TREE_CODE (constructor_type) == UNION_TYPE)
5324 && constructor_fields == 0)
5325 process_init_element (pop_init_level (1));
5326 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5327 && tree_int_cst_lt (constructor_max_index, constructor_index))
5328 process_init_element (pop_init_level (1));
5329 else
5330 break;
5331 }
5332
bdc49177
JW
5333 /* Structure elements may require alignment. Do this now if necessary
5334 for the subaggregate, and if it comes next in sequence. Don't do
5335 this for subaggregates that will go on the pending list. */
7eec3328 5336 if (constructor_incremental && constructor_type != 0
bdc49177
JW
5337 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5338 && constructor_fields == constructor_unfilled_fields)
e700c8ec
RS
5339 {
5340 /* Advance to offset of this element. */
5341 if (! tree_int_cst_equal (constructor_bit_index,
5342 DECL_FIELD_BITPOS (constructor_fields)))
5343 {
f8dac6eb
R
5344 /* By using unsigned arithmetic, the result will be correct even
5345 in case of overflows, if BITS_PER_UNIT is a power of two. */
5346 unsigned next = (TREE_INT_CST_LOW
5347 (DECL_FIELD_BITPOS (constructor_fields))
5348 / (unsigned)BITS_PER_UNIT);
5349 unsigned here = (TREE_INT_CST_LOW (constructor_bit_index)
5350 / (unsigned)BITS_PER_UNIT);
5351
5352 assemble_zeros ((next - here)
5353 * (unsigned)BITS_PER_UNIT
5354 / (unsigned)BITS_PER_UNIT);
e700c8ec 5355 }
24c032e9
JW
5356 /* Indicate that we have now filled the structure up to the current
5357 field. */
5358 constructor_unfilled_fields = constructor_fields;
e700c8ec
RS
5359 }
5360
94ba5069 5361 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
de520661
RS
5362 p->type = constructor_type;
5363 p->fields = constructor_fields;
5364 p->index = constructor_index;
5365 p->range_end = constructor_range_end;
5366 p->max_index = constructor_max_index;
5367 p->unfilled_index = constructor_unfilled_index;
5368 p->unfilled_fields = constructor_unfilled_fields;
b62acd60 5369 p->bit_index = constructor_bit_index;
de520661
RS
5370 p->elements = constructor_elements;
5371 p->constant = constructor_constant;
5372 p->simple = constructor_simple;
5373 p->erroneous = constructor_erroneous;
5374 p->pending_elts = constructor_pending_elts;
5375 p->depth = constructor_depth;
790e9490 5376 p->replacement_value = 0;
de520661
RS
5377 p->implicit = implicit;
5378 p->incremental = constructor_incremental;
5379 p->outer = 0;
5380 p->next = constructor_stack;
5381 constructor_stack = p;
5382
5383 constructor_constant = 1;
5384 constructor_simple = 1;
5385 constructor_depth = SPELLING_DEPTH ();
5386 constructor_elements = 0;
5387 constructor_pending_elts = 0;
5388
94ba5069
RS
5389 /* Don't die if an entire brace-pair level is superfluous
5390 in the containing level. */
5391 if (constructor_type == 0)
5392 ;
5393 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5394 || TREE_CODE (constructor_type) == UNION_TYPE)
de520661 5395 {
91fa3c30
RS
5396 /* Don't die if there are extra init elts at the end. */
5397 if (constructor_fields == 0)
5398 constructor_type = 0;
5399 else
5400 {
5401 constructor_type = TREE_TYPE (constructor_fields);
19d76e60 5402 push_member_name (constructor_fields);
e4376e63 5403 constructor_depth++;
81f415f0
RK
5404 if (constructor_fields != constructor_unfilled_fields)
5405 constructor_incremental = 0;
91fa3c30 5406 }
de520661
RS
5407 }
5408 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5409 {
5410 constructor_type = TREE_TYPE (constructor_type);
5411 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
e4376e63 5412 constructor_depth++;
20e5a991
RK
5413 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5414 || constructor_range_end != 0)
81f415f0 5415 constructor_incremental = 0;
de520661 5416 }
400fbf9f 5417
91fa3c30
RS
5418 if (constructor_type == 0)
5419 {
ab87f8c8 5420 error_init ("extra brace group at end of initializer");
91fa3c30
RS
5421 constructor_fields = 0;
5422 constructor_unfilled_fields = 0;
b71c7f8a 5423 return;
91fa3c30 5424 }
b71c7f8a
RK
5425
5426 /* Turn off constructor_incremental if type is a struct with bitfields. */
5427 check_init_type_bitfields (constructor_type);
5428
5429 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5430 {
5431 missing_braces_mentioned = 1;
ab87f8c8 5432 warning_init ("missing braces around initializer");
b71c7f8a
RK
5433 }
5434
5435 if (TREE_CODE (constructor_type) == RECORD_TYPE
91fa3c30 5436 || TREE_CODE (constructor_type) == UNION_TYPE)
de520661
RS
5437 {
5438 constructor_fields = TYPE_FIELDS (constructor_type);
abc95ed3 5439 /* Skip any nameless bit fields at the beginning. */
ef86d2a6 5440 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
fc623854
RS
5441 && DECL_NAME (constructor_fields) == 0)
5442 constructor_fields = TREE_CHAIN (constructor_fields);
de520661 5443 constructor_unfilled_fields = constructor_fields;
b62acd60 5444 constructor_bit_index = copy_node (integer_zero_node);
f8dac6eb 5445 TREE_TYPE (constructor_bit_index) = sbitsizetype;
de520661
RS
5446 }
5447 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5448 {
de520661 5449 constructor_range_end = 0;
de520661 5450 if (TYPE_DOMAIN (constructor_type))
2bede729
PB
5451 {
5452 constructor_max_index
5453 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5454 constructor_index
5455 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5456 }
5457 else
5458 constructor_index = copy_node (integer_zero_node);
5459 constructor_unfilled_index = copy_node (constructor_index);
de520661
RS
5460 }
5461 else
5462 {
ab87f8c8 5463 warning_init ("braces around scalar initializer");
de520661
RS
5464 constructor_fields = constructor_type;
5465 constructor_unfilled_fields = constructor_type;
5466 }
5467}
400fbf9f 5468
de520661
RS
5469/* Don't read a struct incrementally if it has any bitfields,
5470 because the incremental reading code doesn't know how to
5471 handle bitfields yet. */
d45cf215 5472
de520661
RS
5473static void
5474check_init_type_bitfields (type)
5475 tree type;
5476{
5477 if (TREE_CODE (type) == RECORD_TYPE)
5478 {
5479 tree tail;
5480 for (tail = TYPE_FIELDS (type); tail;
5481 tail = TREE_CHAIN (tail))
3c9d8baf 5482 {
43f7bed5 5483 if (DECL_C_BIT_FIELD (tail))
3c9d8baf
RK
5484 {
5485 constructor_incremental = 0;
5486 break;
5487 }
5488
5489 check_init_type_bitfields (TREE_TYPE (tail));
5490 }
400fbf9f 5491 }
3c9d8baf 5492
43f7bed5
VM
5493 else if (TREE_CODE (type) == UNION_TYPE)
5494 {
5495 tree tail = TYPE_FIELDS (type);
5496 if (tail && DECL_C_BIT_FIELD (tail))
5497 /* We also use the nonincremental algorithm for initiliazation
5498 of unions whose first member is a bitfield, becuase the
5499 incremental algorithm has no code for dealing with
5500 bitfields. */
5501 constructor_incremental = 0;
5502 }
5503
3c9d8baf
RK
5504 else if (TREE_CODE (type) == ARRAY_TYPE)
5505 check_init_type_bitfields (TREE_TYPE (type));
de520661
RS
5506}
5507
5508/* At the end of an implicit or explicit brace level,
5509 finish up that level of constructor.
5510 If we were outputting the elements as they are read, return 0
5511 from inner levels (process_init_element ignores that),
5512 but return error_mark_node from the outermost level
5513 (that's what we want to put in DECL_INITIAL).
5514 Otherwise, return a CONSTRUCTOR expression. */
5515
5516tree
5517pop_init_level (implicit)
5518 int implicit;
5519{
5520 struct constructor_stack *p;
9d5f3e49 5521 int size = 0;
de520661
RS
5522 tree constructor = 0;
5523
5524 if (implicit == 0)
400fbf9f 5525 {
de520661
RS
5526 /* When we come to an explicit close brace,
5527 pop any inner levels that didn't have explicit braces. */
5528 while (constructor_stack->implicit)
5529 process_init_element (pop_init_level (1));
5530 }
400fbf9f 5531
de520661 5532 p = constructor_stack;
91fa3c30
RS
5533
5534 if (constructor_type != 0)
5535 size = int_size_in_bytes (constructor_type);
400fbf9f 5536
9dfcc8db
BH
5537 /* Warn when some struct elements are implicitly initialized to zero. */
5538 if (extra_warnings
5539 && constructor_type
5540 && TREE_CODE (constructor_type) == RECORD_TYPE
5541 && constructor_unfilled_fields)
5542 {
5543 push_member_name (constructor_unfilled_fields);
ab87f8c8 5544 warning_init ("missing initializer");
9dfcc8db
BH
5545 RESTORE_SPELLING_DEPTH (constructor_depth);
5546 }
5547
de520661
RS
5548 /* Now output all pending elements. */
5549 output_pending_init_elements (1);
5550
b62acd60
RS
5551#if 0 /* c-parse.in warns about {}. */
5552 /* In ANSI, each brace level must have at least one element. */
5553 if (! implicit && pedantic
5554 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5555 ? integer_zerop (constructor_unfilled_index)
5556 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
ab87f8c8 5557 pedwarn_init ("empty braces in initializer");
b62acd60
RS
5558#endif
5559
de520661
RS
5560 /* Pad out the end of the structure. */
5561
790e9490
RS
5562 if (p->replacement_value)
5563 {
5564 /* If this closes a superfluous brace pair,
5565 just pass out the element between them. */
5566 constructor = p->replacement_value;
5567 /* If this is the top level thing within the initializer,
d11fdb45 5568 and it's for a variable, then since we already called
790e9490
RS
5569 assemble_variable, we must output the value now. */
5570 if (p->next == 0 && constructor_decl != 0
5571 && constructor_incremental)
5572 {
5573 constructor = digest_init (constructor_type, constructor,
48dd3a7c
RK
5574 require_constant_value,
5575 require_constant_elements);
790e9490
RS
5576
5577 /* If initializing an array of unknown size,
5578 determine the size now. */
5579 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5580 && TYPE_DOMAIN (constructor_type) == 0)
5581 {
5582 int failure;
5ded5b76 5583 int momentary_p;
790e9490
RS
5584
5585 push_obstacks_nochange ();
5586 if (TREE_PERMANENT (constructor_type))
5587 end_temporary_allocation ();
5588
5ded5b76
RK
5589 momentary_p = suspend_momentary ();
5590
790e9490
RS
5591 /* We shouldn't have an incomplete array type within
5592 some other type. */
5593 if (constructor_stack->next)
5594 abort ();
5595
5596 failure
5597 = complete_array_type (constructor_type,
5598 constructor, 0);
5599 if (failure)
5600 abort ();
5601
5602 size = int_size_in_bytes (constructor_type);
5ded5b76 5603 resume_momentary (momentary_p);
790e9490
RS
5604 pop_obstacks ();
5605 }
5606
5607 output_constant (constructor, size);
5608 }
5609 }
91fa3c30
RS
5610 else if (constructor_type == 0)
5611 ;
19d76e60
RK
5612 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5613 && TREE_CODE (constructor_type) != UNION_TYPE
5614 && TREE_CODE (constructor_type) != ARRAY_TYPE
5615 && ! constructor_incremental)
5616 {
5617 /* A nonincremental scalar initializer--just return
5618 the element, after verifying there is just one. */
5619 if (constructor_elements == 0)
5620 {
ab87f8c8 5621 error_init ("empty scalar initializer");
19d76e60
RK
5622 constructor = error_mark_node;
5623 }
5624 else if (TREE_CHAIN (constructor_elements) != 0)
5625 {
ab87f8c8 5626 error_init ("extra elements in scalar initializer");
19d76e60
RK
5627 constructor = TREE_VALUE (constructor_elements);
5628 }
5629 else
5630 constructor = TREE_VALUE (constructor_elements);
5631 }
790e9490 5632 else if (! constructor_incremental)
de520661
RS
5633 {
5634 if (constructor_erroneous)
5635 constructor = error_mark_node;
5636 else
400fbf9f 5637 {
de520661
RS
5638 int momentary = suspend_momentary ();
5639
5640 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5641 nreverse (constructor_elements));
5642 if (constructor_constant)
5643 TREE_CONSTANT (constructor) = 1;
5644 if (constructor_constant && constructor_simple)
5645 TREE_STATIC (constructor) = 1;
19d76e60 5646
de520661
RS
5647 resume_momentary (momentary);
5648 }
5649 }
5650 else
5651 {
5652 tree filled;
5653 int momentary = suspend_momentary ();
400fbf9f 5654
de520661
RS
5655 if (TREE_CODE (constructor_type) == RECORD_TYPE
5656 || TREE_CODE (constructor_type) == UNION_TYPE)
5657 {
de520661
RS
5658 /* Find the offset of the end of that field. */
5659 filled = size_binop (CEIL_DIV_EXPR,
b62acd60 5660 constructor_bit_index,
de520661
RS
5661 size_int (BITS_PER_UNIT));
5662 }
5663 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5664 {
5665 /* If initializing an array of unknown size,
5666 determine the size now. */
5667 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5668 && TYPE_DOMAIN (constructor_type) == 0)
400fbf9f 5669 {
de520661
RS
5670 tree maxindex
5671 = size_binop (MINUS_EXPR,
5672 constructor_unfilled_index,
5673 integer_one_node);
5674
5675 push_obstacks_nochange ();
5676 if (TREE_PERMANENT (constructor_type))
5677 end_temporary_allocation ();
5678 maxindex = copy_node (maxindex);
5679 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5680 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5681
45ce961e
JW
5682 /* TYPE_MAX_VALUE is always one less than the number of elements
5683 in the array, because we start counting at zero. Therefore,
5684 warn only if the value is less than zero. */
de520661 5685 if (pedantic
ff3225e7 5686 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
45ce961e 5687 < 0))
ff3225e7
RK
5688 error_with_decl (constructor_decl,
5689 "zero or negative array size `%s'");
de520661
RS
5690 layout_type (constructor_type);
5691 size = int_size_in_bytes (constructor_type);
5692 pop_obstacks ();
400fbf9f
JW
5693 }
5694
de520661
RS
5695 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5696 size_in_bytes (TREE_TYPE (constructor_type)));
5697 }
5698 else
5699 filled = 0;
400fbf9f 5700
de520661
RS
5701 if (filled != 0)
5702 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5703
5704 resume_momentary (momentary);
5705 }
5706
5707
5708 constructor_type = p->type;
5709 constructor_fields = p->fields;
5710 constructor_index = p->index;
5711 constructor_range_end = p->range_end;
5712 constructor_max_index = p->max_index;
5713 constructor_unfilled_index = p->unfilled_index;
5714 constructor_unfilled_fields = p->unfilled_fields;
b62acd60 5715 constructor_bit_index = p->bit_index;
de520661
RS
5716 constructor_elements = p->elements;
5717 constructor_constant = p->constant;
5718 constructor_simple = p->simple;
5719 constructor_erroneous = p->erroneous;
5720 constructor_pending_elts = p->pending_elts;
5721 constructor_depth = p->depth;
5722 constructor_incremental = p->incremental;
5723 RESTORE_SPELLING_DEPTH (constructor_depth);
5724
5725 constructor_stack = p->next;
5726 free (p);
5727
5728 if (constructor == 0)
5729 {
5730 if (constructor_stack == 0)
5731 return error_mark_node;
5732 return NULL_TREE;
5733 }
5734 return constructor;
5735}
5736
5737/* Within an array initializer, specify the next index to be initialized.
5738 FIRST is that index. If LAST is nonzero, then initialize a range
5739 of indices, running from FIRST through LAST. */
5740
5741void
5742set_init_index (first, last)
5743 tree first, last;
5744{
19d76e60
RK
5745 while ((TREE_CODE (first) == NOP_EXPR
5746 || TREE_CODE (first) == CONVERT_EXPR
5747 || TREE_CODE (first) == NON_LVALUE_EXPR)
5748 && (TYPE_MODE (TREE_TYPE (first))
5749 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5750 (first) = TREE_OPERAND (first, 0);
5751 if (last)
5752 while ((TREE_CODE (last) == NOP_EXPR
5753 || TREE_CODE (last) == CONVERT_EXPR
5754 || TREE_CODE (last) == NON_LVALUE_EXPR)
5755 && (TYPE_MODE (TREE_TYPE (last))
5756 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5757 (last) = TREE_OPERAND (last, 0);
5758
94ba5069 5759 if (TREE_CODE (first) != INTEGER_CST)
ab87f8c8 5760 error_init ("nonconstant array index in initializer");
94ba5069 5761 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ab87f8c8 5762 error_init ("nonconstant array index in initializer");
7b1d6e6e 5763 else if (! constructor_unfilled_index)
ab87f8c8 5764 error_init ("array index in non-array initializer");
94ba5069 5765 else if (tree_int_cst_lt (first, constructor_unfilled_index))
ab87f8c8 5766 error_init ("duplicate array index in initializer");
de520661
RS
5767 else
5768 {
ee2990e7
RK
5769 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first);
5770 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first);
de520661
RS
5771
5772 if (last != 0 && tree_int_cst_lt (last, first))
ab87f8c8 5773 error_init ("empty index range in initializer");
de520661 5774 else
b62acd60
RS
5775 {
5776 if (pedantic)
5777 pedwarn ("ANSI C forbids specifying element to initialize");
5778 constructor_range_end = last;
5779 }
de520661
RS
5780 }
5781}
5782
5783/* Within a struct initializer, specify the next field to be initialized. */
5784
94ba5069 5785void
de520661
RS
5786set_init_label (fieldname)
5787 tree fieldname;
5788{
5789 tree tail;
5790 int passed = 0;
5791
e5cfb88f
RK
5792 /* Don't die if an entire brace-pair level is superfluous
5793 in the containing level. */
5794 if (constructor_type == 0)
5795 return;
5796
de520661
RS
5797 for (tail = TYPE_FIELDS (constructor_type); tail;
5798 tail = TREE_CHAIN (tail))
5799 {
5800 if (tail == constructor_unfilled_fields)
5801 passed = 1;
5802 if (DECL_NAME (tail) == fieldname)
5803 break;
5804 }
5805
5806 if (tail == 0)
5807 error ("unknown field `%s' specified in initializer",
5808 IDENTIFIER_POINTER (fieldname));
5809 else if (!passed)
5810 error ("field `%s' already initialized",
5811 IDENTIFIER_POINTER (fieldname));
5812 else
b62acd60
RS
5813 {
5814 constructor_fields = tail;
5815 if (pedantic)
5816 pedwarn ("ANSI C forbids specifying structure member to initialize");
5817 }
de520661
RS
5818}
5819\f
e5e809f4
JL
5820/* Add a new initializer to the tree of pending initializers. PURPOSE
5821 indentifies the initializer, either array index or field in a structure.
5822 VALUE is the value of that index or field. */
5823
5824static void
5825add_pending_init (purpose, value)
5826 tree purpose, value;
5827{
5828 struct init_node *p, **q, *r;
5829
5830 q = &constructor_pending_elts;
5831 p = 0;
5832
5833 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5834 {
5835 while (*q != 0)
5836 {
5837 p = *q;
5838 if (tree_int_cst_lt (purpose, p->purpose))
5839 q = &p->left;
5840 else if (tree_int_cst_lt (p->purpose, purpose))
5841 q = &p->right;
5842 else
5843 abort ();
5844 }
5845 }
5846 else
5847 {
5848 while (*q != NULL)
5849 {
5850 p = *q;
5851 if (tree_int_cst_lt (DECL_FIELD_BITPOS (purpose),
5852 DECL_FIELD_BITPOS (p->purpose)))
5853 q = &p->left;
5854 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (p->purpose),
5855 DECL_FIELD_BITPOS (purpose)))
5856 q = &p->right;
5857 else
5858 abort ();
5859 }
5860 }
5861
5862 r = (struct init_node *) oballoc (sizeof (struct init_node));
5863 r->purpose = purpose;
5864 r->value = value;
5865
5866 *q = r;
5867 r->parent = p;
5868 r->left = 0;
5869 r->right = 0;
5870 r->balance = 0;
5871
5872 while (p)
5873 {
5874 struct init_node *s;
5875
5876 if (r == p->left)
5877 {
5878 if (p->balance == 0)
5879 p->balance = -1;
5880 else if (p->balance < 0)
5881 {
5882 if (r->balance < 0)
5883 {
5884 /* L rotation. */
5885 p->left = r->right;
5886 if (p->left)
5887 p->left->parent = p;
5888 r->right = p;
5889
5890 p->balance = 0;
5891 r->balance = 0;
5892
5893 s = p->parent;
5894 p->parent = r;
5895 r->parent = s;
5896 if (s)
5897 {
5898 if (s->left == p)
5899 s->left = r;
5900 else
5901 s->right = r;
5902 }
5903 else
5904 constructor_pending_elts = r;
5905 }
5906 else
5907 {
5908 /* LR rotation. */
5909 struct init_node *t = r->right;
5910
5911 r->right = t->left;
5912 if (r->right)
5913 r->right->parent = r;
5914 t->left = r;
5915
5916 p->left = t->right;
5917 if (p->left)
5918 p->left->parent = p;
5919 t->right = p;
5920
5921 p->balance = t->balance < 0;
5922 r->balance = -(t->balance > 0);
5923 t->balance = 0;
5924
5925 s = p->parent;
5926 p->parent = t;
5927 r->parent = t;
5928 t->parent = s;
5929 if (s)
5930 {
5931 if (s->left == p)
5932 s->left = t;
5933 else
5934 s->right = t;
5935 }
5936 else
5937 constructor_pending_elts = t;
5938 }
5939 break;
5940 }
5941 else
5942 {
5943 /* p->balance == +1; growth of left side balances the node. */
5944 p->balance = 0;
5945 break;
5946 }
5947 }
5948 else /* r == p->right */
5949 {
5950 if (p->balance == 0)
5951 /* Growth propagation from right side. */
5952 p->balance++;
5953 else if (p->balance > 0)
5954 {
5955 if (r->balance > 0)
5956 {
5957 /* R rotation. */
5958 p->right = r->left;
5959 if (p->right)
5960 p->right->parent = p;
5961 r->left = p;
5962
5963 p->balance = 0;
5964 r->balance = 0;
5965
5966 s = p->parent;
5967 p->parent = r;
5968 r->parent = s;
5969 if (s)
5970 {
5971 if (s->left == p)
5972 s->left = r;
5973 else
5974 s->right = r;
5975 }
5976 else
5977 constructor_pending_elts = r;
5978 }
5979 else /* r->balance == -1 */
5980 {
5981 /* RL rotation */
5982 struct init_node *t = r->left;
5983
5984 r->left = t->right;
5985 if (r->left)
5986 r->left->parent = r;
5987 t->right = r;
5988
5989 p->right = t->left;
5990 if (p->right)
5991 p->right->parent = p;
5992 t->left = p;
5993
5994 r->balance = (t->balance < 0);
5995 p->balance = -(t->balance > 0);
5996 t->balance = 0;
5997
5998 s = p->parent;
5999 p->parent = t;
6000 r->parent = t;
6001 t->parent = s;
6002 if (s)
6003 {
6004 if (s->left == p)
6005 s->left = t;
6006 else
6007 s->right = t;
6008 }
6009 else
6010 constructor_pending_elts = t;
6011 }
6012 break;
6013 }
6014 else
6015 {
6016 /* p->balance == -1; growth of right side balances the node. */
6017 p->balance = 0;
6018 break;
6019 }
6020 }
6021
6022 r = p;
6023 p = p->parent;
6024 }
6025}
6026
6027/* Return nonzero if FIELD is equal to the index of a pending initializer. */
6028
6029static int
6030pending_init_member (field)
6031 tree field;
6032{
6033 struct init_node *p;
6034
6035 p = constructor_pending_elts;
6036 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6037 {
6038 while (p)
6039 {
6040 if (tree_int_cst_equal (field, p->purpose))
6041 return 1;
6042 else if (tree_int_cst_lt (field, p->purpose))
6043 p = p->left;
6044 else
6045 p = p->right;
6046 }
6047 }
6048 else
6049 {
6050 while (p)
6051 {
6052 if (field == p->purpose)
6053 return 1;
6054 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (field),
6055 DECL_FIELD_BITPOS (p->purpose)))
6056 p = p->left;
6057 else
6058 p = p->right;
6059 }
6060 }
6061
6062 return 0;
6063}
6064
de520661
RS
6065/* "Output" the next constructor element.
6066 At top level, really output it to assembler code now.
6067 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6068 TYPE is the data type that the containing data type wants here.
6069 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6070
6071 PENDING if non-nil means output pending elements that belong
6072 right after this element. (PENDING is normally 1;
6073 it is 0 while outputting pending elements, to avoid recursion.) */
6074
34403047 6075static void
de520661
RS
6076output_init_element (value, type, field, pending)
6077 tree value, type, field;
6078 int pending;
6079{
6080 int duplicate = 0;
6081
d3ab9753
RS
6082 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6083 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
fd5d5b94
RS
6084 && !(TREE_CODE (value) == STRING_CST
6085 && TREE_CODE (type) == ARRAY_TYPE
6086 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
1e40eab8
RS
6087 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6088 TYPE_MAIN_VARIANT (type))))
d3ab9753
RS
6089 value = default_conversion (value);
6090
6091 if (value == error_mark_node)
6092 constructor_erroneous = 1;
6093 else if (!TREE_CONSTANT (value))
6094 constructor_constant = 0;
4160009f
RK
6095 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6096 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6097 || TREE_CODE (constructor_type) == UNION_TYPE)
ef86d2a6
RK
6098 && DECL_C_BIT_FIELD (field)
6099 && TREE_CODE (value) != INTEGER_CST))
d3ab9753
RS
6100 constructor_simple = 0;
6101
de520661
RS
6102 if (require_constant_value && ! TREE_CONSTANT (value))
6103 {
ab87f8c8 6104 error_init ("initializer element is not constant");
de520661
RS
6105 value = error_mark_node;
6106 }
6107 else if (require_constant_elements
6108 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6109 {
ab87f8c8 6110 error_init ("initializer element is not computable at load time");
de520661
RS
6111 value = error_mark_node;
6112 }
6113
6114 /* If this element duplicates one on constructor_pending_elts,
6115 print a message and ignore it. Don't do this when we're
6116 processing elements taken off constructor_pending_elts,
6117 because we'd always get spurious errors. */
6118 if (pending)
6119 {
6120 if (TREE_CODE (constructor_type) == RECORD_TYPE
e5e809f4
JL
6121 || TREE_CODE (constructor_type) == UNION_TYPE
6122 || TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 6123 {
e5e809f4 6124 if (pending_init_member (field))
400fbf9f 6125 {
ab87f8c8 6126 error_init ("duplicate initializer");
de520661 6127 duplicate = 1;
400fbf9f 6128 }
400fbf9f
JW
6129 }
6130 }
400fbf9f 6131
de520661
RS
6132 /* If this element doesn't come next in sequence,
6133 put it on constructor_pending_elts. */
6134 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6135 && !tree_int_cst_equal (field, constructor_unfilled_index))
6136 {
6137 if (! duplicate)
8348547a
RS
6138 /* The copy_node is needed in case field is actually
6139 constructor_index, which is modified in place. */
e5e809f4
JL
6140 add_pending_init (copy_node (field),
6141 digest_init (type, value, require_constant_value,
6142 require_constant_elements));
de520661 6143 }
76aaaae2 6144 else if (TREE_CODE (constructor_type) == RECORD_TYPE
de520661
RS
6145 && field != constructor_unfilled_fields)
6146 {
76aaaae2
RS
6147 /* We do this for records but not for unions. In a union,
6148 no matter which field is specified, it can be initialized
6149 right away since it starts at the beginning of the union. */
de520661 6150 if (!duplicate)
e5e809f4
JL
6151 add_pending_init (field,
6152 digest_init (type, value, require_constant_value,
6153 require_constant_elements));
de520661
RS
6154 }
6155 else
6156 {
6157 /* Otherwise, output this element either to
6158 constructor_elements or to the assembler file. */
400fbf9f 6159
de520661 6160 if (!duplicate)
c2f4acb7 6161 {
de520661 6162 if (! constructor_incremental)
94ba5069 6163 {
19d76e60 6164 if (field && TREE_CODE (field) == INTEGER_CST)
94ba5069
RS
6165 field = copy_node (field);
6166 constructor_elements
48dd3a7c
RK
6167 = tree_cons (field, digest_init (type, value,
6168 require_constant_value,
6169 require_constant_elements),
94ba5069
RS
6170 constructor_elements);
6171 }
de520661 6172 else
b62acd60
RS
6173 {
6174 /* Structure elements may require alignment.
6175 Do this, if necessary. */
6176 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6177 {
6178 /* Advance to offset of this element. */
6179 if (! tree_int_cst_equal (constructor_bit_index,
b5ff0f70 6180 DECL_FIELD_BITPOS (field)))
b62acd60 6181 {
f8dac6eb
R
6182 /* By using unsigned arithmetic, the result will be
6183 correct even in case of overflows, if BITS_PER_UNIT
6184 is a power of two. */
6185 unsigned next = (TREE_INT_CST_LOW
6186 (DECL_FIELD_BITPOS (field))
6187 / (unsigned)BITS_PER_UNIT);
6188 unsigned here = (TREE_INT_CST_LOW
6189 (constructor_bit_index)
6190 / (unsigned)BITS_PER_UNIT);
6191
6192 assemble_zeros ((next - here)
6193 * (unsigned)BITS_PER_UNIT
6194 / (unsigned)BITS_PER_UNIT);
b62acd60
RS
6195 }
6196 }
48dd3a7c
RK
6197 output_constant (digest_init (type, value,
6198 require_constant_value,
6199 require_constant_elements),
d11fdb45 6200 int_size_in_bytes (type));
b62acd60 6201
925d5bbf
RS
6202 /* For a record or union,
6203 keep track of end position of last field. */
6204 if (TREE_CODE (constructor_type) == RECORD_TYPE
6205 || TREE_CODE (constructor_type) == UNION_TYPE)
b62acd60 6206 {
b5ff0f70
RK
6207 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
6208 DECL_SIZE (field));
b62acd60
RS
6209 TREE_INT_CST_LOW (constructor_bit_index)
6210 = TREE_INT_CST_LOW (temp);
6211 TREE_INT_CST_HIGH (constructor_bit_index)
6212 = TREE_INT_CST_HIGH (temp);
6213 }
6214 }
c2f4acb7
RS
6215 }
6216
de520661
RS
6217 /* Advance the variable that indicates sequential elements output. */
6218 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
400fbf9f 6219 {
de520661
RS
6220 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
6221 integer_one_node);
6222 TREE_INT_CST_LOW (constructor_unfilled_index)
6223 = TREE_INT_CST_LOW (tem);
6224 TREE_INT_CST_HIGH (constructor_unfilled_index)
6225 = TREE_INT_CST_HIGH (tem);
6226 }
6227 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6228 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6229 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6230 constructor_unfilled_fields = 0;
6231
6232 /* Now output any pending elements which have become next. */
6233 if (pending)
6234 output_pending_init_elements (0);
6235 }
6236}
400fbf9f 6237
de520661
RS
6238/* Output any pending elements which have become next.
6239 As we output elements, constructor_unfilled_{fields,index}
6240 advances, which may cause other elements to become next;
6241 if so, they too are output.
6242
6243 If ALL is 0, we return when there are
6244 no more pending elements to output now.
6245
6246 If ALL is 1, we output space as necessary so that
6247 we can output all the pending elements. */
6248
6249static void
6250output_pending_init_elements (all)
6251 int all;
6252{
e5e809f4 6253 struct init_node *elt = constructor_pending_elts;
de520661
RS
6254 tree next;
6255
6256 retry:
6257
e5e809f4 6258 /* Look thru the whole pending tree.
de520661
RS
6259 If we find an element that should be output now,
6260 output it. Otherwise, set NEXT to the element
6261 that comes first among those still pending. */
6262
6263 next = 0;
e5e809f4 6264 while (elt)
de520661
RS
6265 {
6266 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6267 {
e5e809f4 6268 if (tree_int_cst_equal (elt->purpose,
de520661 6269 constructor_unfilled_index))
e5e809f4
JL
6270 output_init_element (elt->value,
6271 TREE_TYPE (constructor_type),
6272 constructor_unfilled_index, 0);
6273 else if (tree_int_cst_lt (constructor_unfilled_index,
6274 elt->purpose))
400fbf9f 6275 {
e5e809f4
JL
6276 /* Advance to the next smaller node. */
6277 if (elt->left)
6278 elt = elt->left;
6279 else
6280 {
6281 /* We have reached the smallest node bigger than the
6282 current unfilled index. Fill the space first. */
6283 next = elt->purpose;
6284 break;
6285 }
6286 }
6287 else
6288 {
6289 /* Advance to the next bigger node. */
6290 if (elt->right)
6291 elt = elt->right;
6292 else
6293 {
6294 /* We have reached the biggest node in a subtree. Find
6295 the parent of it, which is the next bigger node. */
6296 while (elt->parent && elt->parent->right == elt)
6297 elt = elt->parent;
6298 elt = elt->parent;
6299 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6300 elt->purpose))
6301 {
6302 next = elt->purpose;
6303 break;
6304 }
6305 }
de520661 6306 }
de520661
RS
6307 }
6308 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6309 || TREE_CODE (constructor_type) == UNION_TYPE)
6310 {
e5e809f4
JL
6311 /* If the current record is complete we are done. */
6312 if (constructor_unfilled_fields == 0)
6313 break;
6314 if (elt->purpose == constructor_unfilled_fields)
de520661 6315 {
e5e809f4 6316 output_init_element (elt->value,
de520661
RS
6317 TREE_TYPE (constructor_unfilled_fields),
6318 constructor_unfilled_fields,
6319 0);
400fbf9f 6320 }
e5e809f4
JL
6321 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6322 DECL_FIELD_BITPOS (elt->purpose)))
6323 {
6324 /* Advance to the next smaller node. */
6325 if (elt->left)
6326 elt = elt->left;
6327 else
6328 {
6329 /* We have reached the smallest node bigger than the
6330 current unfilled field. Fill the space first. */
6331 next = elt->purpose;
6332 break;
6333 }
6334 }
6335 else
6336 {
6337 /* Advance to the next bigger node. */
6338 if (elt->right)
6339 elt = elt->right;
6340 else
6341 {
6342 /* We have reached the biggest node in a subtree. Find
6343 the parent of it, which is the next bigger node. */
6344 while (elt->parent && elt->parent->right == elt)
6345 elt = elt->parent;
6346 elt = elt->parent;
6347 if (elt
6348 && tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6349 DECL_FIELD_BITPOS (elt->purpose)))
6350 {
6351 next = elt->purpose;
6352 break;
6353 }
6354 }
6355 }
400fbf9f 6356 }
de520661
RS
6357 }
6358
6359 /* Ordinarily return, but not if we want to output all
6360 and there are elements left. */
6361 if (! (all && next != 0))
6362 return;
6363
6364 /* Generate space up to the position of NEXT. */
6365 if (constructor_incremental)
6366 {
6367 tree filled;
9d5f3e49 6368 tree nextpos_tree = size_int (0);
400fbf9f 6369
de520661
RS
6370 if (TREE_CODE (constructor_type) == RECORD_TYPE
6371 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 6372 {
e5e809f4 6373 tree tail;
b5ff0f70 6374 /* Find the last field written out, if any. */
de520661
RS
6375 for (tail = TYPE_FIELDS (constructor_type); tail;
6376 tail = TREE_CHAIN (tail))
6377 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6378 break;
b5ff0f70
RK
6379
6380 if (tail)
6381 /* Find the offset of the end of that field. */
6382 filled = size_binop (CEIL_DIV_EXPR,
6383 size_binop (PLUS_EXPR,
6384 DECL_FIELD_BITPOS (tail),
6385 DECL_SIZE (tail)),
6386 size_int (BITS_PER_UNIT));
6387 else
6388 filled = size_int (0);
6389
de520661
RS
6390 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6391 DECL_FIELD_BITPOS (next),
6392 size_int (BITS_PER_UNIT));
b5ff0f70
RK
6393
6394 TREE_INT_CST_HIGH (constructor_bit_index)
6395 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6396 TREE_INT_CST_LOW (constructor_bit_index)
6397 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
de520661 6398 constructor_unfilled_fields = next;
400fbf9f 6399 }
de520661 6400 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
400fbf9f 6401 {
de520661
RS
6402 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6403 size_in_bytes (TREE_TYPE (constructor_type)));
6404 nextpos_tree
6405 = size_binop (MULT_EXPR, next,
6406 size_in_bytes (TREE_TYPE (constructor_type)));
6407 TREE_INT_CST_LOW (constructor_unfilled_index)
6408 = TREE_INT_CST_LOW (next);
6409 TREE_INT_CST_HIGH (constructor_unfilled_index)
6410 = TREE_INT_CST_HIGH (next);
400fbf9f 6411 }
de520661
RS
6412 else
6413 filled = 0;
400fbf9f 6414
de520661 6415 if (filled)
fe67cf58 6416 {
de520661
RS
6417 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6418
6419 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
fe67cf58 6420 }
de520661 6421 }
94ba5069
RS
6422 else
6423 {
6424 /* If it's not incremental, just skip over the gap,
6425 so that after jumping to retry we will output the next
6426 successive element. */
6427 if (TREE_CODE (constructor_type) == RECORD_TYPE
6428 || TREE_CODE (constructor_type) == UNION_TYPE)
6429 constructor_unfilled_fields = next;
6430 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6431 {
6432 TREE_INT_CST_LOW (constructor_unfilled_index)
6433 = TREE_INT_CST_LOW (next);
6434 TREE_INT_CST_HIGH (constructor_unfilled_index)
6435 = TREE_INT_CST_HIGH (next);
6436 }
6437 }
de520661 6438
e5e809f4
JL
6439 /* ELT now points to the node in the pending tree with the next
6440 initializer to output. */
de520661
RS
6441 goto retry;
6442}
6443\f
6444/* Add one non-braced element to the current constructor level.
6445 This adjusts the current position within the constructor's type.
6446 This may also start or terminate implicit levels
6447 to handle a partly-braced initializer.
6448
6449 Once this has found the correct level for the new element,
6450 it calls output_init_element.
6451
6452 Note: if we are incrementally outputting this constructor,
6453 this function may be called with a null argument
6454 representing a sub-constructor that was already incrementally output.
6455 When that happens, we output nothing, but we do the bookkeeping
6456 to skip past that element of the current constructor. */
6457
6458void
6459process_init_element (value)
6460 tree value;
6461{
b62acd60
RS
6462 tree orig_value = value;
6463 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6464
790e9490
RS
6465 /* Handle superfluous braces around string cst as in
6466 char x[] = {"foo"}; */
6467 if (string_flag
d27c148b 6468 && constructor_type
790e9490 6469 && TREE_CODE (constructor_type) == ARRAY_TYPE
61e215dd 6470 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
790e9490
RS
6471 && integer_zerop (constructor_unfilled_index))
6472 {
6473 constructor_stack->replacement_value = value;
6474 return;
6475 }
6476
790e9490
RS
6477 if (constructor_stack->replacement_value != 0)
6478 {
ab87f8c8 6479 error_init ("excess elements in struct initializer");
790e9490
RS
6480 return;
6481 }
6482
91fa3c30
RS
6483 /* Ignore elements of a brace group if it is entirely superfluous
6484 and has already been diagnosed. */
6485 if (constructor_type == 0)
6486 return;
6487
de520661
RS
6488 /* If we've exhausted any levels that didn't have braces,
6489 pop them now. */
6490 while (constructor_stack->implicit)
6491 {
6492 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6493 || TREE_CODE (constructor_type) == UNION_TYPE)
6494 && constructor_fields == 0)
6495 process_init_element (pop_init_level (1));
6496 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
ec0bc8b6
RK
6497 && (constructor_max_index == 0
6498 || tree_int_cst_lt (constructor_max_index,
6499 constructor_index)))
de520661 6500 process_init_element (pop_init_level (1));
fe67cf58 6501 else
de520661 6502 break;
400fbf9f
JW
6503 }
6504
de520661 6505 while (1)
400fbf9f 6506 {
de520661 6507 if (TREE_CODE (constructor_type) == RECORD_TYPE)
400fbf9f 6508 {
de520661
RS
6509 tree fieldtype;
6510 enum tree_code fieldcode;
6511
6512 if (constructor_fields == 0)
6513 {
ab87f8c8 6514 pedwarn_init ("excess elements in struct initializer");
de520661
RS
6515 break;
6516 }
6517
1d33b2a9
JW
6518 fieldtype = TREE_TYPE (constructor_fields);
6519 if (fieldtype != error_mark_node)
6520 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
de520661
RS
6521 fieldcode = TREE_CODE (fieldtype);
6522
b62acd60
RS
6523 /* Accept a string constant to initialize a subarray. */
6524 if (value != 0
6525 && fieldcode == ARRAY_TYPE
6526 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6527 && string_flag)
6528 value = orig_value;
6529 /* Otherwise, if we have come to a subaggregate,
6530 and we don't have an element of its type, push into it. */
cc77d4d5 6531 else if (value != 0 && !constructor_no_implicit
ee7204ee 6532 && value != error_mark_node
b62acd60
RS
6533 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6534 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6535 || fieldcode == UNION_TYPE))
de520661
RS
6536 {
6537 push_init_level (1);
6538 continue;
6539 }
6540
6541 if (value)
6542 {
19d76e60 6543 push_member_name (constructor_fields);
de520661
RS
6544 output_init_element (value, fieldtype, constructor_fields, 1);
6545 RESTORE_SPELLING_DEPTH (constructor_depth);
6546 }
6547 else
b62acd60
RS
6548 /* Do the bookkeeping for an element that was
6549 directly output as a constructor. */
6550 {
6551 /* For a record, keep track of end position of last field. */
6552 tree temp = size_binop (PLUS_EXPR,
6553 DECL_FIELD_BITPOS (constructor_fields),
6554 DECL_SIZE (constructor_fields));
6555 TREE_INT_CST_LOW (constructor_bit_index)
6556 = TREE_INT_CST_LOW (temp);
6557 TREE_INT_CST_HIGH (constructor_bit_index)
6558 = TREE_INT_CST_HIGH (temp);
6559
6560 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6561 }
de520661
RS
6562
6563 constructor_fields = TREE_CHAIN (constructor_fields);
abc95ed3 6564 /* Skip any nameless bit fields at the beginning. */
ef86d2a6
RK
6565 while (constructor_fields != 0
6566 && DECL_C_BIT_FIELD (constructor_fields)
fc623854
RS
6567 && DECL_NAME (constructor_fields) == 0)
6568 constructor_fields = TREE_CHAIN (constructor_fields);
de520661 6569 break;
400fbf9f 6570 }
de520661 6571 if (TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 6572 {
de520661
RS
6573 tree fieldtype;
6574 enum tree_code fieldcode;
6575
6576 if (constructor_fields == 0)
6577 {
ab87f8c8 6578 pedwarn_init ("excess elements in union initializer");
de520661
RS
6579 break;
6580 }
6581
1d33b2a9
JW
6582 fieldtype = TREE_TYPE (constructor_fields);
6583 if (fieldtype != error_mark_node)
6584 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
de520661
RS
6585 fieldcode = TREE_CODE (fieldtype);
6586
b62acd60
RS
6587 /* Accept a string constant to initialize a subarray. */
6588 if (value != 0
6589 && fieldcode == ARRAY_TYPE
6590 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6591 && string_flag)
6592 value = orig_value;
6593 /* Otherwise, if we have come to a subaggregate,
6594 and we don't have an element of its type, push into it. */
cc77d4d5 6595 else if (value != 0 && !constructor_no_implicit
ee7204ee 6596 && value != error_mark_node
b62acd60
RS
6597 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6598 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6599 || fieldcode == UNION_TYPE))
de520661
RS
6600 {
6601 push_init_level (1);
6602 continue;
6603 }
6604
6605 if (value)
6606 {
19d76e60 6607 push_member_name (constructor_fields);
de520661
RS
6608 output_init_element (value, fieldtype, constructor_fields, 1);
6609 RESTORE_SPELLING_DEPTH (constructor_depth);
6610 }
6611 else
94ba5069
RS
6612 /* Do the bookkeeping for an element that was
6613 directly output as a constructor. */
6614 {
6615 TREE_INT_CST_LOW (constructor_bit_index)
6616 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6617 TREE_INT_CST_HIGH (constructor_bit_index)
6618 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6619
6620 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6621 }
de520661
RS
6622
6623 constructor_fields = 0;
6624 break;
400fbf9f 6625 }
de520661
RS
6626 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6627 {
6628 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6629 enum tree_code eltcode = TREE_CODE (elttype);
6630
b62acd60
RS
6631 /* Accept a string constant to initialize a subarray. */
6632 if (value != 0
6633 && eltcode == ARRAY_TYPE
6634 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6635 && string_flag)
6636 value = orig_value;
6637 /* Otherwise, if we have come to a subaggregate,
6638 and we don't have an element of its type, push into it. */
cc77d4d5 6639 else if (value != 0 && !constructor_no_implicit
ee7204ee 6640 && value != error_mark_node
b62acd60
RS
6641 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6642 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6643 || eltcode == UNION_TYPE))
de520661
RS
6644 {
6645 push_init_level (1);
6646 continue;
6647 }
6648
6649 if (constructor_max_index != 0
6650 && tree_int_cst_lt (constructor_max_index, constructor_index))
6651 {
ab87f8c8 6652 pedwarn_init ("excess elements in array initializer");
de520661
RS
6653 break;
6654 }
400fbf9f 6655
0f41302f 6656 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
333a5dae 6657 if (constructor_range_end)
ee2990e7
RK
6658 {
6659 if (constructor_max_index != 0
6660 && tree_int_cst_lt (constructor_max_index,
6661 constructor_range_end))
6662 {
ab87f8c8 6663 pedwarn_init ("excess elements in array initializer");
ee2990e7
RK
6664 TREE_INT_CST_HIGH (constructor_range_end)
6665 = TREE_INT_CST_HIGH (constructor_max_index);
6666 TREE_INT_CST_LOW (constructor_range_end)
6667 = TREE_INT_CST_LOW (constructor_max_index);
6668 }
6669
6670 value = save_expr (value);
6671 }
333a5dae 6672
de520661
RS
6673 /* Now output the actual element.
6674 Ordinarily, output once.
6675 If there is a range, repeat it till we advance past the range. */
6676 do
6677 {
6678 tree tem;
d45cf215 6679
de520661
RS
6680 if (value)
6681 {
6682 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6683 output_init_element (value, elttype, constructor_index, 1);
6684 RESTORE_SPELLING_DEPTH (constructor_depth);
6685 }
d45cf215 6686
de520661
RS
6687 tem = size_binop (PLUS_EXPR, constructor_index,
6688 integer_one_node);
ee2990e7
RK
6689 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem);
6690 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem);
de520661
RS
6691
6692 if (!value)
6693 /* If we are doing the bookkeeping for an element that was
6694 directly output as a constructor,
6695 we must update constructor_unfilled_index. */
6696 {
6697 TREE_INT_CST_LOW (constructor_unfilled_index)
6698 = TREE_INT_CST_LOW (constructor_index);
6699 TREE_INT_CST_HIGH (constructor_unfilled_index)
6700 = TREE_INT_CST_HIGH (constructor_index);
6701 }
6702 }
6703 while (! (constructor_range_end == 0
6704 || tree_int_cst_lt (constructor_range_end,
6705 constructor_index)));
400fbf9f 6706
de520661
RS
6707 break;
6708 }
6709
6710 /* Handle the sole element allowed in a braced initializer
6711 for a scalar variable. */
6712 if (constructor_fields == 0)
6713 {
ab87f8c8 6714 pedwarn_init ("excess elements in scalar initializer");
de520661
RS
6715 break;
6716 }
6717
6718 if (value)
6719 output_init_element (value, constructor_type, NULL_TREE, 1);
6720 constructor_fields = 0;
6721 break;
fe67cf58 6722 }
de520661
RS
6723
6724 /* If the (lexically) previous elments are not now saved,
6725 we can discard the storage for them. */
8d75e509
RK
6726 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6727 && constructor_stack == 0)
de520661 6728 clear_momentary ();
400fbf9f
JW
6729}
6730\f
6731/* Expand an ASM statement with operands, handling output operands
6732 that are not variables or INDIRECT_REFS by transforming such
6733 cases into cases that expand_asm_operands can handle.
6734
6735 Arguments are same as for expand_asm_operands. */
6736
6737void
6738c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6739 tree string, outputs, inputs, clobbers;
6740 int vol;
6741 char *filename;
6742 int line;
6743{
6744 int noutputs = list_length (outputs);
6745 register int i;
6746 /* o[I] is the place that output number I should be written. */
6747 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6748 register tree tail;
6749
6750 if (TREE_CODE (string) == ADDR_EXPR)
6751 string = TREE_OPERAND (string, 0);
6752 if (TREE_CODE (string) != STRING_CST)
6753 {
6754 error ("asm template is not a string constant");
6755 return;
6756 }
6757
7b6327ae 6758 /* Record the contents of OUTPUTS before it is modified. */
400fbf9f
JW
6759 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6760 o[i] = TREE_VALUE (tail);
6761
6762 /* Perform default conversions on array and function inputs. */
6763 /* Don't do this for other types--
6764 it would screw up operands expected to be in memory. */
6765 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6766 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6767 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6768 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6769
6770 /* Generate the ASM_OPERANDS insn;
6771 store into the TREE_VALUEs of OUTPUTS some trees for
6772 where the values were actually stored. */
6773 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6774
6775 /* Copy all the intermediate outputs into the specified outputs. */
6776 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6777 {
6778 if (o[i] != TREE_VALUE (tail))
6779 {
6780 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
f5a8bfff 6781 NULL_RTX, VOIDmode, EXPAND_NORMAL);
400fbf9f
JW
6782 free_temp_slots ();
6783 }
6784 /* Detect modification of read-only values.
6785 (Otherwise done by build_modify_expr.) */
6786 else
6787 {
6788 tree type = TREE_TYPE (o[i]);
a43ea319
RK
6789 if (TREE_READONLY (o[i])
6790 || TYPE_READONLY (type)
400fbf9f
JW
6791 || ((TREE_CODE (type) == RECORD_TYPE
6792 || TREE_CODE (type) == UNION_TYPE)
6793 && C_TYPE_FIELDS_READONLY (type)))
6794 readonly_warning (o[i], "modification by `asm'");
6795 }
6796 }
6797
6798 /* Those MODIFY_EXPRs could do autoincrements. */
6799 emit_queue ();
6800}
6801\f
6802/* Expand a C `return' statement.
6803 RETVAL is the expression for what to return,
6804 or a null pointer for `return;' with no value. */
6805
6806void
6807c_expand_return (retval)
6808 tree retval;
6809{
6810 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6811
6812 if (TREE_THIS_VOLATILE (current_function_decl))
08bf538e 6813 warning ("function declared `noreturn' has a `return' statement");
400fbf9f
JW
6814
6815 if (!retval)
6816 {
6817 current_function_returns_null = 1;
6818 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6819 warning ("`return' with no value, in function returning non-void");
6820 expand_null_return ();
6821 }
6822 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6823 {
6824 current_function_returns_null = 1;
6825 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6826 pedwarn ("`return' with a value, in function returning void");
6827 expand_return (retval);
6828 }
6829 else
6830 {
ab87f8c8 6831 tree t = convert_for_assignment (valtype, retval, _("return"),
9b7267b8 6832 NULL_TREE, NULL_TREE, 0);
400fbf9f 6833 tree res = DECL_RESULT (current_function_decl);
88a3dbc1 6834 tree inner;
70768eda
RK
6835
6836 if (t == error_mark_node)
6837 return;
6838
88a3dbc1
RK
6839 inner = t = convert (TREE_TYPE (res), t);
6840
6841 /* Strip any conversions, additions, and subtractions, and see if
6842 we are returning the address of a local variable. Warn if so. */
abe80e6d 6843 while (1)
88a3dbc1 6844 {
abe80e6d
RK
6845 switch (TREE_CODE (inner))
6846 {
6847 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6848 case PLUS_EXPR:
6849 inner = TREE_OPERAND (inner, 0);
6850 continue;
6851
6852 case MINUS_EXPR:
6853 /* If the second operand of the MINUS_EXPR has a pointer
6854 type (or is converted from it), this may be valid, so
6855 don't give a warning. */
6856 {
6857 tree op1 = TREE_OPERAND (inner, 1);
6858
6859 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6860 && (TREE_CODE (op1) == NOP_EXPR
6861 || TREE_CODE (op1) == NON_LVALUE_EXPR
6862 || TREE_CODE (op1) == CONVERT_EXPR))
6863 op1 = TREE_OPERAND (op1, 0);
6864
6865 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6866 break;
88a3dbc1 6867
abe80e6d
RK
6868 inner = TREE_OPERAND (inner, 0);
6869 continue;
6870 }
6871
6872 case ADDR_EXPR:
6873 inner = TREE_OPERAND (inner, 0);
88a3dbc1 6874
abe80e6d
RK
6875 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6876 inner = TREE_OPERAND (inner, 0);
6877
6878 if (TREE_CODE (inner) == VAR_DECL
6879 && ! DECL_EXTERNAL (inner)
6880 && ! TREE_STATIC (inner)
6881 && DECL_CONTEXT (inner) == current_function_decl)
6882 warning ("function returns address of local variable");
6883 break;
e9a25f70
JL
6884
6885 default:
6886 break;
abe80e6d
RK
6887 }
6888
6889 break;
88a3dbc1
RK
6890 }
6891
6892 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
1c2a9b35 6893 TREE_SIDE_EFFECTS (t) = 1;
400fbf9f
JW
6894 expand_return (t);
6895 current_function_returns_value = 1;
6896 }
6897}
6898\f
6899/* Start a C switch statement, testing expression EXP.
6900 Return EXP if it is valid, an error node otherwise. */
6901
6902tree
6903c_expand_start_case (exp)
6904 tree exp;
6905{
6906 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6907 tree type = TREE_TYPE (exp);
6908
6909 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6910 {
6911 error ("switch quantity not an integer");
6912 exp = error_mark_node;
6913 }
6914 else
6915 {
6916 tree index;
6cb72a7d 6917 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
400fbf9f
JW
6918
6919 if (warn_traditional
6cb72a7d
RS
6920 && (type == long_integer_type_node
6921 || type == long_unsigned_type_node))
400fbf9f
JW
6922 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6923
6924 exp = default_conversion (exp);
6925 type = TREE_TYPE (exp);
8d9bfdc5 6926 index = get_unwidened (exp, NULL_TREE);
400fbf9f
JW
6927 /* We can't strip a conversion from a signed type to an unsigned,
6928 because if we did, int_fits_type_p would do the wrong thing
6929 when checking case values for being in range,
6930 and it's too hard to do the right thing. */
6931 if (TREE_UNSIGNED (TREE_TYPE (exp))
6932 == TREE_UNSIGNED (TREE_TYPE (index)))
6933 exp = index;
6934 }
6935
6936 expand_start_case (1, exp, type, "switch statement");
6937
6938 return exp;
6939}