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