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