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