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