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