1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
22
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
47
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
56 };
57
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
60
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
63
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
66
67 struct c_label_context_se *label_context_stack_se;
68 struct c_label_context_vm *label_context_stack_vm;
69
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
73
74 static int require_constant_value;
75 static int require_constant_elements;
76
77 static tree qualify_type (tree, tree);
78 static int tagged_types_tu_compatible_p (tree, tree);
79 static int comp_target_types (tree, tree);
80 static int function_types_compatible_p (tree, tree);
81 static int type_lists_compatible_p (tree, tree);
82 static tree decl_constant_value_for_broken_optimization (tree);
83 static tree lookup_field (tree, tree);
84 static tree convert_arguments (tree, tree, tree, tree);
85 static tree pointer_diff (tree, tree);
86 static tree unary_complex_lvalue (enum tree_code, tree, int);
87 static void pedantic_lvalue_warning (enum tree_code);
88 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
89 int);
90 static tree valid_compound_expr_initializer (tree, tree);
91 static void push_string (const char *);
92 static void push_member_name (tree);
93 static int spelling_length (void);
94 static char *print_spelling (char *);
95 static void warning_init (const char *);
96 static tree digest_init (tree, tree, bool, int);
97 static void output_init_element (tree, bool, tree, tree, int);
98 static void output_pending_init_elements (int);
99 static int set_designator (int);
100 static void push_range_stack (tree);
101 static void add_pending_init (tree, tree);
102 static void set_nonincremental_init (void);
103 static void set_nonincremental_init_from_string (tree);
104 static tree find_init_member (tree);
105 static void readonly_error (tree, enum lvalue_use);
106 static int lvalue_or_else (tree, enum lvalue_use);
107 static int lvalue_p (tree);
108 static void record_maybe_used_decl (tree);
109 static int comptypes_internal (tree, tree);
110 /* This is a cache to hold if two types are compatible or not. */
111
112 struct tagged_tu_seen_cache {
113 const struct tagged_tu_seen_cache * next;
114 tree t1;
115 tree t2;
116 /* The return value of tagged_types_tu_compatible_p if we had seen
117 these two types already. */
118 int val;
119 };
120
121 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
122 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
123
124 /* Do `exp = require_complete_type (exp);' to make sure exp
125 does not have an incomplete type. (That includes void types.) */
126
127 tree
require_complete_type(tree value)128 require_complete_type (tree value)
129 {
130 tree type = TREE_TYPE (value);
131
132 if (value == error_mark_node || type == error_mark_node)
133 return error_mark_node;
134
135 /* First, detect a valid value with a complete type. */
136 if (COMPLETE_TYPE_P (type))
137 return value;
138
139 c_incomplete_type_error (value, type);
140 return error_mark_node;
141 }
142
143 /* Print an error message for invalid use of an incomplete type.
144 VALUE is the expression that was used (or 0 if that isn't known)
145 and TYPE is the type that was invalid. */
146
147 void
c_incomplete_type_error(tree value,tree type)148 c_incomplete_type_error (tree value, tree type)
149 {
150 const char *type_code_string;
151
152 /* Avoid duplicate error message. */
153 if (TREE_CODE (type) == ERROR_MARK)
154 return;
155
156 if (value != 0 && (TREE_CODE (value) == VAR_DECL
157 || TREE_CODE (value) == PARM_DECL))
158 error ("%qD has an incomplete type", value);
159 else
160 {
161 retry:
162 /* We must print an error message. Be clever about what it says. */
163
164 switch (TREE_CODE (type))
165 {
166 case RECORD_TYPE:
167 type_code_string = "struct";
168 break;
169
170 case UNION_TYPE:
171 type_code_string = "union";
172 break;
173
174 case ENUMERAL_TYPE:
175 type_code_string = "enum";
176 break;
177
178 case VOID_TYPE:
179 error ("invalid use of void expression");
180 return;
181
182 case ARRAY_TYPE:
183 if (TYPE_DOMAIN (type))
184 {
185 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
186 {
187 error ("invalid use of flexible array member");
188 return;
189 }
190 type = TREE_TYPE (type);
191 goto retry;
192 }
193 error ("invalid use of array with unspecified bounds");
194 return;
195
196 default:
197 gcc_unreachable ();
198 }
199
200 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
201 error ("invalid use of undefined type %<%s %E%>",
202 type_code_string, TYPE_NAME (type));
203 else
204 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
205 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
206 }
207 }
208
209 /* Given a type, apply default promotions wrt unnamed function
210 arguments and return the new type. */
211
212 tree
c_type_promotes_to(tree type)213 c_type_promotes_to (tree type)
214 {
215 if (TYPE_MAIN_VARIANT (type) == float_type_node)
216 return double_type_node;
217
218 if (c_promoting_integer_type_p (type))
219 {
220 /* Preserve unsignedness if not really getting any wider. */
221 if (TYPE_UNSIGNED (type)
222 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
223 return unsigned_type_node;
224 return integer_type_node;
225 }
226
227 return type;
228 }
229
230 /* Return a variant of TYPE which has all the type qualifiers of LIKE
231 as well as those of TYPE. */
232
233 static tree
qualify_type(tree type,tree like)234 qualify_type (tree type, tree like)
235 {
236 return c_build_qualified_type (type,
237 TYPE_QUALS (type) | TYPE_QUALS (like));
238 }
239
240 /* Return the composite type of two compatible types.
241
242 We assume that comptypes has already been done and returned
243 nonzero; if that isn't so, this may crash. In particular, we
244 assume that qualifiers match. */
245
246 tree
composite_type(tree t1,tree t2)247 composite_type (tree t1, tree t2)
248 {
249 enum tree_code code1;
250 enum tree_code code2;
251 tree attributes;
252
253 /* Save time if the two types are the same. */
254
255 if (t1 == t2) return t1;
256
257 /* If one type is nonsense, use the other. */
258 if (t1 == error_mark_node)
259 return t2;
260 if (t2 == error_mark_node)
261 return t1;
262
263 code1 = TREE_CODE (t1);
264 code2 = TREE_CODE (t2);
265
266 /* Merge the attributes. */
267 attributes = targetm.merge_type_attributes (t1, t2);
268
269 /* If one is an enumerated type and the other is the compatible
270 integer type, the composite type might be either of the two
271 (DR#013 question 3). For consistency, use the enumerated type as
272 the composite type. */
273
274 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
275 return t1;
276 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
277 return t2;
278
279 gcc_assert (code1 == code2);
280
281 switch (code1)
282 {
283 case POINTER_TYPE:
284 /* For two pointers, do this recursively on the target type. */
285 {
286 tree pointed_to_1 = TREE_TYPE (t1);
287 tree pointed_to_2 = TREE_TYPE (t2);
288 tree target = composite_type (pointed_to_1, pointed_to_2);
289 t1 = build_pointer_type (target);
290 t1 = build_type_attribute_variant (t1, attributes);
291 return qualify_type (t1, t2);
292 }
293
294 case ARRAY_TYPE:
295 {
296 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
297 int quals;
298 tree unqual_elt;
299 tree d1 = TYPE_DOMAIN (t1);
300 tree d2 = TYPE_DOMAIN (t2);
301 bool d1_variable, d2_variable;
302 bool d1_zero, d2_zero;
303
304 /* We should not have any type quals on arrays at all. */
305 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
306
307 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
308 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
309
310 d1_variable = (!d1_zero
311 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
312 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
313 d2_variable = (!d2_zero
314 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
315 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
316
317 /* Save space: see if the result is identical to one of the args. */
318 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
319 && (d2_variable || d2_zero || !d1_variable))
320 return build_type_attribute_variant (t1, attributes);
321 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
322 && (d1_variable || d1_zero || !d2_variable))
323 return build_type_attribute_variant (t2, attributes);
324
325 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
326 return build_type_attribute_variant (t1, attributes);
327 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
328 return build_type_attribute_variant (t2, attributes);
329
330 /* Merge the element types, and have a size if either arg has
331 one. We may have qualifiers on the element types. To set
332 up TYPE_MAIN_VARIANT correctly, we need to form the
333 composite of the unqualified types and add the qualifiers
334 back at the end. */
335 quals = TYPE_QUALS (strip_array_types (elt));
336 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
337 t1 = build_array_type (unqual_elt,
338 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
339 && (d2_variable
340 || d2_zero
341 || !d1_variable))
342 ? t1
343 : t2));
344 t1 = c_build_qualified_type (t1, quals);
345 return build_type_attribute_variant (t1, attributes);
346 }
347
348 case FUNCTION_TYPE:
349 /* Function types: prefer the one that specified arg types.
350 If both do, merge the arg types. Also merge the return types. */
351 {
352 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
353 tree p1 = TYPE_ARG_TYPES (t1);
354 tree p2 = TYPE_ARG_TYPES (t2);
355 int len;
356 tree newargs, n;
357 int i;
358
359 /* Save space: see if the result is identical to one of the args. */
360 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
361 return build_type_attribute_variant (t1, attributes);
362 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
363 return build_type_attribute_variant (t2, attributes);
364
365 /* Simple way if one arg fails to specify argument types. */
366 if (TYPE_ARG_TYPES (t1) == 0)
367 {
368 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
369 t1 = build_type_attribute_variant (t1, attributes);
370 return qualify_type (t1, t2);
371 }
372 if (TYPE_ARG_TYPES (t2) == 0)
373 {
374 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
375 t1 = build_type_attribute_variant (t1, attributes);
376 return qualify_type (t1, t2);
377 }
378
379 /* If both args specify argument types, we must merge the two
380 lists, argument by argument. */
381 /* Tell global_bindings_p to return false so that variable_size
382 doesn't die on VLAs in parameter types. */
383 c_override_global_bindings_to_false = true;
384
385 len = list_length (p1);
386 newargs = 0;
387
388 for (i = 0; i < len; i++)
389 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
390
391 n = newargs;
392
393 for (; p1;
394 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
395 {
396 /* A null type means arg type is not specified.
397 Take whatever the other function type has. */
398 if (TREE_VALUE (p1) == 0)
399 {
400 TREE_VALUE (n) = TREE_VALUE (p2);
401 goto parm_done;
402 }
403 if (TREE_VALUE (p2) == 0)
404 {
405 TREE_VALUE (n) = TREE_VALUE (p1);
406 goto parm_done;
407 }
408
409 /* Given wait (union {union wait *u; int *i} *)
410 and wait (union wait *),
411 prefer union wait * as type of parm. */
412 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
413 && TREE_VALUE (p1) != TREE_VALUE (p2))
414 {
415 tree memb;
416 tree mv2 = TREE_VALUE (p2);
417 if (mv2 && mv2 != error_mark_node
418 && TREE_CODE (mv2) != ARRAY_TYPE)
419 mv2 = TYPE_MAIN_VARIANT (mv2);
420 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
421 memb; memb = TREE_CHAIN (memb))
422 {
423 tree mv3 = TREE_TYPE (memb);
424 if (mv3 && mv3 != error_mark_node
425 && TREE_CODE (mv3) != ARRAY_TYPE)
426 mv3 = TYPE_MAIN_VARIANT (mv3);
427 if (comptypes (mv3, mv2))
428 {
429 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
430 TREE_VALUE (p2));
431 if (pedantic)
432 pedwarn ("function types not truly compatible in ISO C");
433 goto parm_done;
434 }
435 }
436 }
437 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
438 && TREE_VALUE (p2) != TREE_VALUE (p1))
439 {
440 tree memb;
441 tree mv1 = TREE_VALUE (p1);
442 if (mv1 && mv1 != error_mark_node
443 && TREE_CODE (mv1) != ARRAY_TYPE)
444 mv1 = TYPE_MAIN_VARIANT (mv1);
445 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
446 memb; memb = TREE_CHAIN (memb))
447 {
448 tree mv3 = TREE_TYPE (memb);
449 if (mv3 && mv3 != error_mark_node
450 && TREE_CODE (mv3) != ARRAY_TYPE)
451 mv3 = TYPE_MAIN_VARIANT (mv3);
452 if (comptypes (mv3, mv1))
453 {
454 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
455 TREE_VALUE (p1));
456 if (pedantic)
457 pedwarn ("function types not truly compatible in ISO C");
458 goto parm_done;
459 }
460 }
461 }
462 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
463 parm_done: ;
464 }
465
466 c_override_global_bindings_to_false = false;
467 t1 = build_function_type (valtype, newargs);
468 t1 = qualify_type (t1, t2);
469 /* ... falls through ... */
470 }
471
472 default:
473 return build_type_attribute_variant (t1, attributes);
474 }
475
476 }
477
478 /* Return the type of a conditional expression between pointers to
479 possibly differently qualified versions of compatible types.
480
481 We assume that comp_target_types has already been done and returned
482 nonzero; if that isn't so, this may crash. */
483
484 static tree
common_pointer_type(tree t1,tree t2)485 common_pointer_type (tree t1, tree t2)
486 {
487 tree attributes;
488 tree pointed_to_1, mv1;
489 tree pointed_to_2, mv2;
490 tree target;
491
492 /* Save time if the two types are the same. */
493
494 if (t1 == t2) return t1;
495
496 /* If one type is nonsense, use the other. */
497 if (t1 == error_mark_node)
498 return t2;
499 if (t2 == error_mark_node)
500 return t1;
501
502 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
503 && TREE_CODE (t2) == POINTER_TYPE);
504
505 /* Merge the attributes. */
506 attributes = targetm.merge_type_attributes (t1, t2);
507
508 /* Find the composite type of the target types, and combine the
509 qualifiers of the two types' targets. Do not lose qualifiers on
510 array element types by taking the TYPE_MAIN_VARIANT. */
511 mv1 = pointed_to_1 = TREE_TYPE (t1);
512 mv2 = pointed_to_2 = TREE_TYPE (t2);
513 if (TREE_CODE (mv1) != ARRAY_TYPE)
514 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
515 if (TREE_CODE (mv2) != ARRAY_TYPE)
516 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
517 target = composite_type (mv1, mv2);
518 t1 = build_pointer_type (c_build_qualified_type
519 (target,
520 TYPE_QUALS (pointed_to_1) |
521 TYPE_QUALS (pointed_to_2)));
522 return build_type_attribute_variant (t1, attributes);
523 }
524
525 /* Return the common type for two arithmetic types under the usual
526 arithmetic conversions. The default conversions have already been
527 applied, and enumerated types converted to their compatible integer
528 types. The resulting type is unqualified and has no attributes.
529
530 This is the type for the result of most arithmetic operations
531 if the operands have the given two types. */
532
533 static tree
c_common_type(tree t1,tree t2)534 c_common_type (tree t1, tree t2)
535 {
536 enum tree_code code1;
537 enum tree_code code2;
538
539 /* If one type is nonsense, use the other. */
540 if (t1 == error_mark_node)
541 return t2;
542 if (t2 == error_mark_node)
543 return t1;
544
545 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
546 t1 = TYPE_MAIN_VARIANT (t1);
547
548 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
549 t2 = TYPE_MAIN_VARIANT (t2);
550
551 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
552 t1 = build_type_attribute_variant (t1, NULL_TREE);
553
554 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
555 t2 = build_type_attribute_variant (t2, NULL_TREE);
556
557 /* Save time if the two types are the same. */
558
559 if (t1 == t2) return t1;
560
561 code1 = TREE_CODE (t1);
562 code2 = TREE_CODE (t2);
563
564 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
565 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
566 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
567 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
568
569 /* If one type is a vector type, return that type. (How the usual
570 arithmetic conversions apply to the vector types extension is not
571 precisely specified.) */
572 if (code1 == VECTOR_TYPE)
573 return t1;
574
575 if (code2 == VECTOR_TYPE)
576 return t2;
577
578 /* If one type is complex, form the common type of the non-complex
579 components, then make that complex. Use T1 or T2 if it is the
580 required type. */
581 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
582 {
583 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
584 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
585 tree subtype = c_common_type (subtype1, subtype2);
586
587 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
588 return t1;
589 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
590 return t2;
591 else
592 return build_complex_type (subtype);
593 }
594
595 /* If only one is real, use it as the result. */
596
597 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
598 return t1;
599
600 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
601 return t2;
602
603 /* Both real or both integers; use the one with greater precision. */
604
605 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
606 return t1;
607 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
608 return t2;
609
610 /* Same precision. Prefer long longs to longs to ints when the
611 same precision, following the C99 rules on integer type rank
612 (which are equivalent to the C90 rules for C90 types). */
613
614 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
615 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
616 return long_long_unsigned_type_node;
617
618 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
619 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
620 {
621 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
622 return long_long_unsigned_type_node;
623 else
624 return long_long_integer_type_node;
625 }
626
627 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
628 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
629 return long_unsigned_type_node;
630
631 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
632 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
633 {
634 /* But preserve unsignedness from the other type,
635 since long cannot hold all the values of an unsigned int. */
636 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
637 return long_unsigned_type_node;
638 else
639 return long_integer_type_node;
640 }
641
642 /* Likewise, prefer long double to double even if same size. */
643 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
644 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
645 return long_double_type_node;
646
647 /* Otherwise prefer the unsigned one. */
648
649 if (TYPE_UNSIGNED (t1))
650 return t1;
651 else
652 return t2;
653 }
654
655 /* Wrapper around c_common_type that is used by c-common.c and other
656 front end optimizations that remove promotions. ENUMERAL_TYPEs
657 are allowed here and are converted to their compatible integer types.
658 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
659 preferably a non-Boolean type as the common type. */
660 tree
common_type(tree t1,tree t2)661 common_type (tree t1, tree t2)
662 {
663 if (TREE_CODE (t1) == ENUMERAL_TYPE)
664 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
665 if (TREE_CODE (t2) == ENUMERAL_TYPE)
666 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
667
668 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
669 if (TREE_CODE (t1) == BOOLEAN_TYPE
670 && TREE_CODE (t2) == BOOLEAN_TYPE)
671 return boolean_type_node;
672
673 /* If either type is BOOLEAN_TYPE, then return the other. */
674 if (TREE_CODE (t1) == BOOLEAN_TYPE)
675 return t2;
676 if (TREE_CODE (t2) == BOOLEAN_TYPE)
677 return t1;
678
679 return c_common_type (t1, t2);
680 }
681
682 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
683 or various other operations. Return 2 if they are compatible
684 but a warning may be needed if you use them together. */
685
686 int
comptypes(tree type1,tree type2)687 comptypes (tree type1, tree type2)
688 {
689 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
690 int val;
691
692 val = comptypes_internal (type1, type2);
693 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
694
695 return val;
696 }
697 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
698 or various other operations. Return 2 if they are compatible
699 but a warning may be needed if you use them together. This
700 differs from comptypes, in that we don't free the seen types. */
701
702 static int
comptypes_internal(tree type1,tree type2)703 comptypes_internal (tree type1, tree type2)
704 {
705 tree t1 = type1;
706 tree t2 = type2;
707 int attrval, val;
708
709 /* Suppress errors caused by previously reported errors. */
710
711 if (t1 == t2 || !t1 || !t2
712 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
713 return 1;
714
715 /* If either type is the internal version of sizetype, return the
716 language version. */
717 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
718 && TYPE_ORIG_SIZE_TYPE (t1))
719 t1 = TYPE_ORIG_SIZE_TYPE (t1);
720
721 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
722 && TYPE_ORIG_SIZE_TYPE (t2))
723 t2 = TYPE_ORIG_SIZE_TYPE (t2);
724
725
726 /* Enumerated types are compatible with integer types, but this is
727 not transitive: two enumerated types in the same translation unit
728 are compatible with each other only if they are the same type. */
729
730 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
731 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
732 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
733 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
734
735 if (t1 == t2)
736 return 1;
737
738 /* Different classes of types can't be compatible. */
739
740 if (TREE_CODE (t1) != TREE_CODE (t2))
741 return 0;
742
743 /* Qualifiers must match. C99 6.7.3p9 */
744
745 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
746 return 0;
747
748 /* Allow for two different type nodes which have essentially the same
749 definition. Note that we already checked for equality of the type
750 qualifiers (just above). */
751
752 if (TREE_CODE (t1) != ARRAY_TYPE
753 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
754 return 1;
755
756 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
757 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
758 return 0;
759
760 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
761 val = 0;
762
763 switch (TREE_CODE (t1))
764 {
765 case POINTER_TYPE:
766 /* Do not remove mode or aliasing information. */
767 if (TYPE_MODE (t1) != TYPE_MODE (t2)
768 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
769 break;
770 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
771 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
772 break;
773
774 case FUNCTION_TYPE:
775 val = function_types_compatible_p (t1, t2);
776 break;
777
778 case ARRAY_TYPE:
779 {
780 tree d1 = TYPE_DOMAIN (t1);
781 tree d2 = TYPE_DOMAIN (t2);
782 bool d1_variable, d2_variable;
783 bool d1_zero, d2_zero;
784 val = 1;
785
786 /* Target types must match incl. qualifiers. */
787 if (TREE_TYPE (t1) != TREE_TYPE (t2)
788 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
789 return 0;
790
791 /* Sizes must match unless one is missing or variable. */
792 if (d1 == 0 || d2 == 0 || d1 == d2)
793 break;
794
795 d1_zero = !TYPE_MAX_VALUE (d1);
796 d2_zero = !TYPE_MAX_VALUE (d2);
797
798 d1_variable = (!d1_zero
799 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
800 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
801 d2_variable = (!d2_zero
802 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
803 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
804
805 if (d1_variable || d2_variable)
806 break;
807 if (d1_zero && d2_zero)
808 break;
809 if (d1_zero || d2_zero
810 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
811 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
812 val = 0;
813
814 break;
815 }
816
817 case ENUMERAL_TYPE:
818 case RECORD_TYPE:
819 case UNION_TYPE:
820 if (val != 1 && !same_translation_unit_p (t1, t2))
821 {
822 if (attrval != 2)
823 return tagged_types_tu_compatible_p (t1, t2);
824 val = tagged_types_tu_compatible_p (t1, t2);
825 }
826 break;
827
828 case VECTOR_TYPE:
829 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
830 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
831 break;
832
833 default:
834 break;
835 }
836 return attrval == 2 && val == 1 ? 2 : val;
837 }
838
839 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
840 ignoring their qualifiers. */
841
842 static int
comp_target_types(tree ttl,tree ttr)843 comp_target_types (tree ttl, tree ttr)
844 {
845 int val;
846 tree mvl, mvr;
847
848 /* Do not lose qualifiers on element types of array types that are
849 pointer targets by taking their TYPE_MAIN_VARIANT. */
850 mvl = TREE_TYPE (ttl);
851 mvr = TREE_TYPE (ttr);
852 if (TREE_CODE (mvl) != ARRAY_TYPE)
853 mvl = TYPE_MAIN_VARIANT (mvl);
854 if (TREE_CODE (mvr) != ARRAY_TYPE)
855 mvr = TYPE_MAIN_VARIANT (mvr);
856 val = comptypes (mvl, mvr);
857
858 if (val == 2 && pedantic)
859 pedwarn ("types are not quite compatible");
860 return val;
861 }
862
863 /* Subroutines of `comptypes'. */
864
865 /* Determine whether two trees derive from the same translation unit.
866 If the CONTEXT chain ends in a null, that tree's context is still
867 being parsed, so if two trees have context chains ending in null,
868 they're in the same translation unit. */
869 int
same_translation_unit_p(tree t1,tree t2)870 same_translation_unit_p (tree t1, tree t2)
871 {
872 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
873 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
874 {
875 case tcc_declaration:
876 t1 = DECL_CONTEXT (t1); break;
877 case tcc_type:
878 t1 = TYPE_CONTEXT (t1); break;
879 case tcc_exceptional:
880 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
881 default: gcc_unreachable ();
882 }
883
884 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
885 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
886 {
887 case tcc_declaration:
888 t2 = DECL_CONTEXT (t2); break;
889 case tcc_type:
890 t2 = TYPE_CONTEXT (t2); break;
891 case tcc_exceptional:
892 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
893 default: gcc_unreachable ();
894 }
895
896 return t1 == t2;
897 }
898
899 /* Allocate the seen two types, assuming that they are compatible. */
900
901 static struct tagged_tu_seen_cache *
alloc_tagged_tu_seen_cache(tree t1,tree t2)902 alloc_tagged_tu_seen_cache (tree t1, tree t2)
903 {
904 struct tagged_tu_seen_cache *tu = xmalloc (sizeof (struct tagged_tu_seen_cache));
905 tu->next = tagged_tu_seen_base;
906 tu->t1 = t1;
907 tu->t2 = t2;
908
909 tagged_tu_seen_base = tu;
910
911 /* The C standard says that two structures in different translation
912 units are compatible with each other only if the types of their
913 fields are compatible (among other things). We assume that they
914 are compatible until proven otherwise when building the cache.
915 An example where this can occur is:
916 struct a
917 {
918 struct a *next;
919 };
920 If we are comparing this against a similar struct in another TU,
921 and did not assume they were compatible, we end up with an infinite
922 loop. */
923 tu->val = 1;
924 return tu;
925 }
926
927 /* Free the seen types until we get to TU_TIL. */
928
929 static void
free_all_tagged_tu_seen_up_to(const struct tagged_tu_seen_cache * tu_til)930 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
931 {
932 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
933 while (tu != tu_til)
934 {
935 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
936 tu = tu1->next;
937 free (tu1);
938 }
939 tagged_tu_seen_base = tu_til;
940 }
941
942 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
943 compatible. If the two types are not the same (which has been
944 checked earlier), this can only happen when multiple translation
945 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
946 rules. */
947
948 static int
tagged_types_tu_compatible_p(tree t1,tree t2)949 tagged_types_tu_compatible_p (tree t1, tree t2)
950 {
951 tree s1, s2;
952 bool needs_warning = false;
953
954 /* We have to verify that the tags of the types are the same. This
955 is harder than it looks because this may be a typedef, so we have
956 to go look at the original type. It may even be a typedef of a
957 typedef...
958 In the case of compiler-created builtin structs the TYPE_DECL
959 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
960 while (TYPE_NAME (t1)
961 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
962 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
963 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
964
965 while (TYPE_NAME (t2)
966 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
967 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
968 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
969
970 /* C90 didn't have the requirement that the two tags be the same. */
971 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
972 return 0;
973
974 /* C90 didn't say what happened if one or both of the types were
975 incomplete; we choose to follow C99 rules here, which is that they
976 are compatible. */
977 if (TYPE_SIZE (t1) == NULL
978 || TYPE_SIZE (t2) == NULL)
979 return 1;
980
981 {
982 const struct tagged_tu_seen_cache * tts_i;
983 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
984 if (tts_i->t1 == t1 && tts_i->t2 == t2)
985 return tts_i->val;
986 }
987
988 switch (TREE_CODE (t1))
989 {
990 case ENUMERAL_TYPE:
991 {
992 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
993 /* Speed up the case where the type values are in the same order. */
994 tree tv1 = TYPE_VALUES (t1);
995 tree tv2 = TYPE_VALUES (t2);
996
997 if (tv1 == tv2)
998 {
999 return 1;
1000 }
1001
1002 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1003 {
1004 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1005 break;
1006 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1007 {
1008 tu->val = 0;
1009 return 0;
1010 }
1011 }
1012
1013 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1014 {
1015 return 1;
1016 }
1017 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1018 {
1019 tu->val = 0;
1020 return 0;
1021 }
1022
1023 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1024 {
1025 tu->val = 0;
1026 return 0;
1027 }
1028
1029 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1030 {
1031 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1032 if (s2 == NULL
1033 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1034 {
1035 tu->val = 0;
1036 return 0;
1037 }
1038 }
1039 return 1;
1040 }
1041
1042 case UNION_TYPE:
1043 {
1044 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1045 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1046 {
1047 tu->val = 0;
1048 return 0;
1049 }
1050
1051 /* Speed up the common case where the fields are in the same order. */
1052 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1053 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1054 {
1055 int result;
1056
1057
1058 if (DECL_NAME (s1) == NULL
1059 || DECL_NAME (s1) != DECL_NAME (s2))
1060 break;
1061 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1062 if (result == 0)
1063 {
1064 tu->val = 0;
1065 return 0;
1066 }
1067 if (result == 2)
1068 needs_warning = true;
1069
1070 if (TREE_CODE (s1) == FIELD_DECL
1071 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1072 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1073 {
1074 tu->val = 0;
1075 return 0;
1076 }
1077 }
1078 if (!s1 && !s2)
1079 {
1080 tu->val = needs_warning ? 2 : 1;
1081 return tu->val;
1082 }
1083
1084 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1085 {
1086 bool ok = false;
1087
1088 if (DECL_NAME (s1) != NULL)
1089 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1090 if (DECL_NAME (s1) == DECL_NAME (s2))
1091 {
1092 int result;
1093 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1094 if (result == 0)
1095 {
1096 tu->val = 0;
1097 return 0;
1098 }
1099 if (result == 2)
1100 needs_warning = true;
1101
1102 if (TREE_CODE (s1) == FIELD_DECL
1103 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1104 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1105 break;
1106
1107 ok = true;
1108 break;
1109 }
1110 if (!ok)
1111 {
1112 tu->val = 0;
1113 return 0;
1114 }
1115 }
1116 tu->val = needs_warning ? 2 : 10;
1117 return tu->val;
1118 }
1119
1120 case RECORD_TYPE:
1121 {
1122 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1123
1124 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1125 s1 && s2;
1126 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1127 {
1128 int result;
1129 if (TREE_CODE (s1) != TREE_CODE (s2)
1130 || DECL_NAME (s1) != DECL_NAME (s2))
1131 break;
1132 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1133 if (result == 0)
1134 break;
1135 if (result == 2)
1136 needs_warning = true;
1137
1138 if (TREE_CODE (s1) == FIELD_DECL
1139 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1140 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1141 break;
1142 }
1143 if (s1 && s2)
1144 tu->val = 0;
1145 else
1146 tu->val = needs_warning ? 2 : 1;
1147 return tu->val;
1148 }
1149
1150 default:
1151 gcc_unreachable ();
1152 }
1153 }
1154
1155 /* Return 1 if two function types F1 and F2 are compatible.
1156 If either type specifies no argument types,
1157 the other must specify a fixed number of self-promoting arg types.
1158 Otherwise, if one type specifies only the number of arguments,
1159 the other must specify that number of self-promoting arg types.
1160 Otherwise, the argument types must match. */
1161
1162 static int
function_types_compatible_p(tree f1,tree f2)1163 function_types_compatible_p (tree f1, tree f2)
1164 {
1165 tree args1, args2;
1166 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1167 int val = 1;
1168 int val1;
1169 tree ret1, ret2;
1170
1171 ret1 = TREE_TYPE (f1);
1172 ret2 = TREE_TYPE (f2);
1173
1174 /* 'volatile' qualifiers on a function's return type used to mean
1175 the function is noreturn. */
1176 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1177 pedwarn ("function return types not compatible due to %<volatile%>");
1178 if (TYPE_VOLATILE (ret1))
1179 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1180 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1181 if (TYPE_VOLATILE (ret2))
1182 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1183 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1184 val = comptypes_internal (ret1, ret2);
1185 if (val == 0)
1186 return 0;
1187
1188 args1 = TYPE_ARG_TYPES (f1);
1189 args2 = TYPE_ARG_TYPES (f2);
1190
1191 /* An unspecified parmlist matches any specified parmlist
1192 whose argument types don't need default promotions. */
1193
1194 if (args1 == 0)
1195 {
1196 if (!self_promoting_args_p (args2))
1197 return 0;
1198 /* If one of these types comes from a non-prototype fn definition,
1199 compare that with the other type's arglist.
1200 If they don't match, ask for a warning (but no error). */
1201 if (TYPE_ACTUAL_ARG_TYPES (f1)
1202 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1203 val = 2;
1204 return val;
1205 }
1206 if (args2 == 0)
1207 {
1208 if (!self_promoting_args_p (args1))
1209 return 0;
1210 if (TYPE_ACTUAL_ARG_TYPES (f2)
1211 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1212 val = 2;
1213 return val;
1214 }
1215
1216 /* Both types have argument lists: compare them and propagate results. */
1217 val1 = type_lists_compatible_p (args1, args2);
1218 return val1 != 1 ? val1 : val;
1219 }
1220
1221 /* Check two lists of types for compatibility,
1222 returning 0 for incompatible, 1 for compatible,
1223 or 2 for compatible with warning. */
1224
1225 static int
type_lists_compatible_p(tree args1,tree args2)1226 type_lists_compatible_p (tree args1, tree args2)
1227 {
1228 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1229 int val = 1;
1230 int newval = 0;
1231
1232 while (1)
1233 {
1234 tree a1, mv1, a2, mv2;
1235 if (args1 == 0 && args2 == 0)
1236 return val;
1237 /* If one list is shorter than the other,
1238 they fail to match. */
1239 if (args1 == 0 || args2 == 0)
1240 return 0;
1241 mv1 = a1 = TREE_VALUE (args1);
1242 mv2 = a2 = TREE_VALUE (args2);
1243 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1244 mv1 = TYPE_MAIN_VARIANT (mv1);
1245 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1246 mv2 = TYPE_MAIN_VARIANT (mv2);
1247 /* A null pointer instead of a type
1248 means there is supposed to be an argument
1249 but nothing is specified about what type it has.
1250 So match anything that self-promotes. */
1251 if (a1 == 0)
1252 {
1253 if (c_type_promotes_to (a2) != a2)
1254 return 0;
1255 }
1256 else if (a2 == 0)
1257 {
1258 if (c_type_promotes_to (a1) != a1)
1259 return 0;
1260 }
1261 /* If one of the lists has an error marker, ignore this arg. */
1262 else if (TREE_CODE (a1) == ERROR_MARK
1263 || TREE_CODE (a2) == ERROR_MARK)
1264 ;
1265 else if (!(newval = comptypes_internal (mv1, mv2)))
1266 {
1267 /* Allow wait (union {union wait *u; int *i} *)
1268 and wait (union wait *) to be compatible. */
1269 if (TREE_CODE (a1) == UNION_TYPE
1270 && (TYPE_NAME (a1) == 0
1271 || TYPE_TRANSPARENT_UNION (a1))
1272 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1273 && tree_int_cst_equal (TYPE_SIZE (a1),
1274 TYPE_SIZE (a2)))
1275 {
1276 tree memb;
1277 for (memb = TYPE_FIELDS (a1);
1278 memb; memb = TREE_CHAIN (memb))
1279 {
1280 tree mv3 = TREE_TYPE (memb);
1281 if (mv3 && mv3 != error_mark_node
1282 && TREE_CODE (mv3) != ARRAY_TYPE)
1283 mv3 = TYPE_MAIN_VARIANT (mv3);
1284 if (comptypes_internal (mv3, mv2))
1285 break;
1286 }
1287 if (memb == 0)
1288 return 0;
1289 }
1290 else if (TREE_CODE (a2) == UNION_TYPE
1291 && (TYPE_NAME (a2) == 0
1292 || TYPE_TRANSPARENT_UNION (a2))
1293 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1294 && tree_int_cst_equal (TYPE_SIZE (a2),
1295 TYPE_SIZE (a1)))
1296 {
1297 tree memb;
1298 for (memb = TYPE_FIELDS (a2);
1299 memb; memb = TREE_CHAIN (memb))
1300 {
1301 tree mv3 = TREE_TYPE (memb);
1302 if (mv3 && mv3 != error_mark_node
1303 && TREE_CODE (mv3) != ARRAY_TYPE)
1304 mv3 = TYPE_MAIN_VARIANT (mv3);
1305 if (comptypes_internal (mv3, mv1))
1306 break;
1307 }
1308 if (memb == 0)
1309 return 0;
1310 }
1311 else
1312 return 0;
1313 }
1314
1315 /* comptypes said ok, but record if it said to warn. */
1316 if (newval > val)
1317 val = newval;
1318
1319 args1 = TREE_CHAIN (args1);
1320 args2 = TREE_CHAIN (args2);
1321 }
1322 }
1323
1324 /* Compute the size to increment a pointer by. */
1325
1326 static tree
c_size_in_bytes(tree type)1327 c_size_in_bytes (tree type)
1328 {
1329 enum tree_code code = TREE_CODE (type);
1330
1331 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1332 return size_one_node;
1333
1334 if (!COMPLETE_OR_VOID_TYPE_P (type))
1335 {
1336 error ("arithmetic on pointer to an incomplete type");
1337 return size_one_node;
1338 }
1339
1340 /* Convert in case a char is more than one unit. */
1341 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1342 size_int (TYPE_PRECISION (char_type_node)
1343 / BITS_PER_UNIT));
1344 }
1345
1346 /* Return either DECL or its known constant value (if it has one). */
1347
1348 tree
decl_constant_value(tree decl)1349 decl_constant_value (tree decl)
1350 {
1351 if (/* Don't change a variable array bound or initial value to a constant
1352 in a place where a variable is invalid. Note that DECL_INITIAL
1353 isn't valid for a PARM_DECL. */
1354 current_function_decl != 0
1355 && TREE_CODE (decl) != PARM_DECL
1356 && !TREE_THIS_VOLATILE (decl)
1357 && TREE_READONLY (decl)
1358 && DECL_INITIAL (decl) != 0
1359 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1360 /* This is invalid if initial value is not constant.
1361 If it has either a function call, a memory reference,
1362 or a variable, then re-evaluating it could give different results. */
1363 && TREE_CONSTANT (DECL_INITIAL (decl))
1364 /* Check for cases where this is sub-optimal, even though valid. */
1365 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1366 return DECL_INITIAL (decl);
1367 return decl;
1368 }
1369
1370 /* Return either DECL or its known constant value (if it has one), but
1371 return DECL if pedantic or DECL has mode BLKmode. This is for
1372 bug-compatibility with the old behavior of decl_constant_value
1373 (before GCC 3.0); every use of this function is a bug and it should
1374 be removed before GCC 3.1. It is not appropriate to use pedantic
1375 in a way that affects optimization, and BLKmode is probably not the
1376 right test for avoiding misoptimizations either. */
1377
1378 static tree
decl_constant_value_for_broken_optimization(tree decl)1379 decl_constant_value_for_broken_optimization (tree decl)
1380 {
1381 tree ret;
1382
1383 if (pedantic || DECL_MODE (decl) == BLKmode)
1384 return decl;
1385
1386 ret = decl_constant_value (decl);
1387 /* Avoid unwanted tree sharing between the initializer and current
1388 function's body where the tree can be modified e.g. by the
1389 gimplifier. */
1390 if (ret != decl && TREE_STATIC (decl))
1391 ret = unshare_expr (ret);
1392 return ret;
1393 }
1394
1395 /* Convert the array expression EXP to a pointer. */
1396 static tree
array_to_pointer_conversion(tree exp)1397 array_to_pointer_conversion (tree exp)
1398 {
1399 tree orig_exp = exp;
1400 tree type = TREE_TYPE (exp);
1401 tree adr;
1402 tree restype = TREE_TYPE (type);
1403 tree ptrtype;
1404
1405 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1406
1407 STRIP_TYPE_NOPS (exp);
1408
1409 if (TREE_NO_WARNING (orig_exp))
1410 TREE_NO_WARNING (exp) = 1;
1411
1412 ptrtype = build_pointer_type (restype);
1413
1414 if (TREE_CODE (exp) == INDIRECT_REF)
1415 return convert (ptrtype, TREE_OPERAND (exp, 0));
1416
1417 if (TREE_CODE (exp) == VAR_DECL)
1418 {
1419 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1420 ADDR_EXPR because it's the best way of representing what
1421 happens in C when we take the address of an array and place
1422 it in a pointer to the element type. */
1423 adr = build1 (ADDR_EXPR, ptrtype, exp);
1424 if (!c_mark_addressable (exp))
1425 return error_mark_node;
1426 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1427 return adr;
1428 }
1429
1430 /* This way is better for a COMPONENT_REF since it can
1431 simplify the offset for a component. */
1432 adr = build_unary_op (ADDR_EXPR, exp, 1);
1433 return convert (ptrtype, adr);
1434 }
1435
1436 /* Convert the function expression EXP to a pointer. */
1437 static tree
function_to_pointer_conversion(tree exp)1438 function_to_pointer_conversion (tree exp)
1439 {
1440 tree orig_exp = exp;
1441
1442 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1443
1444 STRIP_TYPE_NOPS (exp);
1445
1446 if (TREE_NO_WARNING (orig_exp))
1447 TREE_NO_WARNING (exp) = 1;
1448
1449 return build_unary_op (ADDR_EXPR, exp, 0);
1450 }
1451
1452 /* Perform the default conversion of arrays and functions to pointers.
1453 Return the result of converting EXP. For any other expression, just
1454 return EXP after removing NOPs. */
1455
1456 struct c_expr
default_function_array_conversion(struct c_expr exp)1457 default_function_array_conversion (struct c_expr exp)
1458 {
1459 tree orig_exp = exp.value;
1460 tree type = TREE_TYPE (exp.value);
1461 enum tree_code code = TREE_CODE (type);
1462
1463 switch (code)
1464 {
1465 case ARRAY_TYPE:
1466 {
1467 bool not_lvalue = false;
1468 bool lvalue_array_p;
1469
1470 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1471 || TREE_CODE (exp.value) == NOP_EXPR)
1472 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1473 {
1474 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1475 not_lvalue = true;
1476 exp.value = TREE_OPERAND (exp.value, 0);
1477 }
1478
1479 if (TREE_NO_WARNING (orig_exp))
1480 TREE_NO_WARNING (exp.value) = 1;
1481
1482 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1483 if (!flag_isoc99 && !lvalue_array_p)
1484 {
1485 /* Before C99, non-lvalue arrays do not decay to pointers.
1486 Normally, using such an array would be invalid; but it can
1487 be used correctly inside sizeof or as a statement expression.
1488 Thus, do not give an error here; an error will result later. */
1489 return exp;
1490 }
1491
1492 exp.value = array_to_pointer_conversion (exp.value);
1493 }
1494 break;
1495 case FUNCTION_TYPE:
1496 exp.value = function_to_pointer_conversion (exp.value);
1497 break;
1498 default:
1499 STRIP_TYPE_NOPS (exp.value);
1500 if (TREE_NO_WARNING (orig_exp))
1501 TREE_NO_WARNING (exp.value) = 1;
1502 break;
1503 }
1504
1505 return exp;
1506 }
1507
1508
1509 /* EXP is an expression of integer type. Apply the integer promotions
1510 to it and return the promoted value. */
1511
1512 tree
perform_integral_promotions(tree exp)1513 perform_integral_promotions (tree exp)
1514 {
1515 tree type = TREE_TYPE (exp);
1516 enum tree_code code = TREE_CODE (type);
1517
1518 gcc_assert (INTEGRAL_TYPE_P (type));
1519
1520 /* Normally convert enums to int,
1521 but convert wide enums to something wider. */
1522 if (code == ENUMERAL_TYPE)
1523 {
1524 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1525 TYPE_PRECISION (integer_type_node)),
1526 ((TYPE_PRECISION (type)
1527 >= TYPE_PRECISION (integer_type_node))
1528 && TYPE_UNSIGNED (type)));
1529
1530 return convert (type, exp);
1531 }
1532
1533 /* ??? This should no longer be needed now bit-fields have their
1534 proper types. */
1535 if (TREE_CODE (exp) == COMPONENT_REF
1536 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1537 /* If it's thinner than an int, promote it like a
1538 c_promoting_integer_type_p, otherwise leave it alone. */
1539 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1540 TYPE_PRECISION (integer_type_node)))
1541 return convert (integer_type_node, exp);
1542
1543 if (c_promoting_integer_type_p (type))
1544 {
1545 /* Preserve unsignedness if not really getting any wider. */
1546 if (TYPE_UNSIGNED (type)
1547 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1548 return convert (unsigned_type_node, exp);
1549
1550 return convert (integer_type_node, exp);
1551 }
1552
1553 return exp;
1554 }
1555
1556
1557 /* Perform default promotions for C data used in expressions.
1558 Enumeral types or short or char are converted to int.
1559 In addition, manifest constants symbols are replaced by their values. */
1560
1561 tree
default_conversion(tree exp)1562 default_conversion (tree exp)
1563 {
1564 tree orig_exp;
1565 tree type = TREE_TYPE (exp);
1566 enum tree_code code = TREE_CODE (type);
1567
1568 /* Functions and arrays have been converted during parsing. */
1569 gcc_assert (code != FUNCTION_TYPE);
1570 if (code == ARRAY_TYPE)
1571 return exp;
1572
1573 /* Constants can be used directly unless they're not loadable. */
1574 if (TREE_CODE (exp) == CONST_DECL)
1575 exp = DECL_INITIAL (exp);
1576
1577 /* Replace a nonvolatile const static variable with its value unless
1578 it is an array, in which case we must be sure that taking the
1579 address of the array produces consistent results. */
1580 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1581 {
1582 exp = decl_constant_value_for_broken_optimization (exp);
1583 type = TREE_TYPE (exp);
1584 }
1585
1586 /* Strip no-op conversions. */
1587 orig_exp = exp;
1588 STRIP_TYPE_NOPS (exp);
1589
1590 if (TREE_NO_WARNING (orig_exp))
1591 TREE_NO_WARNING (exp) = 1;
1592
1593 if (INTEGRAL_TYPE_P (type))
1594 return perform_integral_promotions (exp);
1595
1596 if (code == VOID_TYPE)
1597 {
1598 error ("void value not ignored as it ought to be");
1599 return error_mark_node;
1600 }
1601 return exp;
1602 }
1603
1604 /* Look up COMPONENT in a structure or union DECL.
1605
1606 If the component name is not found, returns NULL_TREE. Otherwise,
1607 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1608 stepping down the chain to the component, which is in the last
1609 TREE_VALUE of the list. Normally the list is of length one, but if
1610 the component is embedded within (nested) anonymous structures or
1611 unions, the list steps down the chain to the component. */
1612
1613 static tree
lookup_field(tree decl,tree component)1614 lookup_field (tree decl, tree component)
1615 {
1616 tree type = TREE_TYPE (decl);
1617 tree field;
1618
1619 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1620 to the field elements. Use a binary search on this array to quickly
1621 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1622 will always be set for structures which have many elements. */
1623
1624 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1625 {
1626 int bot, top, half;
1627 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1628
1629 field = TYPE_FIELDS (type);
1630 bot = 0;
1631 top = TYPE_LANG_SPECIFIC (type)->s->len;
1632 while (top - bot > 1)
1633 {
1634 half = (top - bot + 1) >> 1;
1635 field = field_array[bot+half];
1636
1637 if (DECL_NAME (field) == NULL_TREE)
1638 {
1639 /* Step through all anon unions in linear fashion. */
1640 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1641 {
1642 field = field_array[bot++];
1643 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1644 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1645 {
1646 tree anon = lookup_field (field, component);
1647
1648 if (anon)
1649 return tree_cons (NULL_TREE, field, anon);
1650 }
1651 }
1652
1653 /* Entire record is only anon unions. */
1654 if (bot > top)
1655 return NULL_TREE;
1656
1657 /* Restart the binary search, with new lower bound. */
1658 continue;
1659 }
1660
1661 if (DECL_NAME (field) == component)
1662 break;
1663 if (DECL_NAME (field) < component)
1664 bot += half;
1665 else
1666 top = bot + half;
1667 }
1668
1669 if (DECL_NAME (field_array[bot]) == component)
1670 field = field_array[bot];
1671 else if (DECL_NAME (field) != component)
1672 return NULL_TREE;
1673 }
1674 else
1675 {
1676 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1677 {
1678 if (DECL_NAME (field) == NULL_TREE
1679 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1680 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1681 {
1682 tree anon = lookup_field (field, component);
1683
1684 if (anon)
1685 return tree_cons (NULL_TREE, field, anon);
1686 }
1687
1688 if (DECL_NAME (field) == component)
1689 break;
1690 }
1691
1692 if (field == NULL_TREE)
1693 return NULL_TREE;
1694 }
1695
1696 return tree_cons (NULL_TREE, field, NULL_TREE);
1697 }
1698
1699 /* Make an expression to refer to the COMPONENT field of
1700 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1701
1702 tree
build_component_ref(tree datum,tree component)1703 build_component_ref (tree datum, tree component)
1704 {
1705 tree type = TREE_TYPE (datum);
1706 enum tree_code code = TREE_CODE (type);
1707 tree field = NULL;
1708 tree ref;
1709
1710 if (!objc_is_public (datum, component))
1711 return error_mark_node;
1712
1713 /* See if there is a field or component with name COMPONENT. */
1714
1715 if (code == RECORD_TYPE || code == UNION_TYPE)
1716 {
1717 if (!COMPLETE_TYPE_P (type))
1718 {
1719 c_incomplete_type_error (NULL_TREE, type);
1720 return error_mark_node;
1721 }
1722
1723 field = lookup_field (datum, component);
1724
1725 if (!field)
1726 {
1727 error ("%qT has no member named %qE", type, component);
1728 return error_mark_node;
1729 }
1730
1731 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1732 This might be better solved in future the way the C++ front
1733 end does it - by giving the anonymous entities each a
1734 separate name and type, and then have build_component_ref
1735 recursively call itself. We can't do that here. */
1736 do
1737 {
1738 tree subdatum = TREE_VALUE (field);
1739
1740 if (TREE_TYPE (subdatum) == error_mark_node)
1741 return error_mark_node;
1742
1743 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1744 NULL_TREE);
1745 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1746 TREE_READONLY (ref) = 1;
1747 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1748 TREE_THIS_VOLATILE (ref) = 1;
1749
1750 if (TREE_DEPRECATED (subdatum))
1751 warn_deprecated_use (subdatum);
1752
1753 datum = ref;
1754
1755 field = TREE_CHAIN (field);
1756 }
1757 while (field);
1758
1759 return ref;
1760 }
1761 else if (code != ERROR_MARK)
1762 error ("request for member %qE in something not a structure or union",
1763 component);
1764
1765 return error_mark_node;
1766 }
1767
1768 /* Given an expression PTR for a pointer, return an expression
1769 for the value pointed to.
1770 ERRORSTRING is the name of the operator to appear in error messages. */
1771
1772 tree
build_indirect_ref(tree ptr,const char * errorstring)1773 build_indirect_ref (tree ptr, const char *errorstring)
1774 {
1775 tree pointer = default_conversion (ptr);
1776 tree type = TREE_TYPE (pointer);
1777
1778 if (TREE_CODE (type) == POINTER_TYPE)
1779 {
1780 if (TREE_CODE (pointer) == ADDR_EXPR
1781 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1782 == TREE_TYPE (type)))
1783 return TREE_OPERAND (pointer, 0);
1784 else
1785 {
1786 tree t = TREE_TYPE (type);
1787 tree ref;
1788
1789 ref = build1 (INDIRECT_REF, t, pointer);
1790
1791 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1792 {
1793 error ("dereferencing pointer to incomplete type");
1794 return error_mark_node;
1795 }
1796 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1797 warning (0, "dereferencing %<void *%> pointer");
1798
1799 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1800 so that we get the proper error message if the result is used
1801 to assign to. Also, &* is supposed to be a no-op.
1802 And ANSI C seems to specify that the type of the result
1803 should be the const type. */
1804 /* A de-reference of a pointer to const is not a const. It is valid
1805 to change it via some other pointer. */
1806 TREE_READONLY (ref) = TYPE_READONLY (t);
1807 TREE_SIDE_EFFECTS (ref)
1808 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1809 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1810 return ref;
1811 }
1812 }
1813 else if (TREE_CODE (pointer) != ERROR_MARK)
1814 error ("invalid type argument of %qs", errorstring);
1815 return error_mark_node;
1816 }
1817
1818 /* This handles expressions of the form "a[i]", which denotes
1819 an array reference.
1820
1821 This is logically equivalent in C to *(a+i), but we may do it differently.
1822 If A is a variable or a member, we generate a primitive ARRAY_REF.
1823 This avoids forcing the array out of registers, and can work on
1824 arrays that are not lvalues (for example, members of structures returned
1825 by functions). */
1826
1827 tree
build_array_ref(tree array,tree index)1828 build_array_ref (tree array, tree index)
1829 {
1830 bool swapped = false;
1831 if (TREE_TYPE (array) == error_mark_node
1832 || TREE_TYPE (index) == error_mark_node)
1833 return error_mark_node;
1834
1835 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1836 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1837 {
1838 tree temp;
1839 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1840 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1841 {
1842 error ("subscripted value is neither array nor pointer");
1843 return error_mark_node;
1844 }
1845 temp = array;
1846 array = index;
1847 index = temp;
1848 swapped = true;
1849 }
1850
1851 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1852 {
1853 error ("array subscript is not an integer");
1854 return error_mark_node;
1855 }
1856
1857 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1858 {
1859 error ("subscripted value is pointer to function");
1860 return error_mark_node;
1861 }
1862
1863 /* Subscripting with type char is likely to lose on a machine where
1864 chars are signed. So warn on any machine, but optionally. Don't
1865 warn for unsigned char since that type is safe. Don't warn for
1866 signed char because anyone who uses that must have done so
1867 deliberately. ??? Existing practice has also been to warn only
1868 when the char index is syntactically the index, not for
1869 char[array]. */
1870 if (!swapped
1871 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1872 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
1873
1874 /* Apply default promotions *after* noticing character types. */
1875 index = default_conversion (index);
1876
1877 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1878
1879 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1880 {
1881 tree rval, type;
1882
1883 /* An array that is indexed by a non-constant
1884 cannot be stored in a register; we must be able to do
1885 address arithmetic on its address.
1886 Likewise an array of elements of variable size. */
1887 if (TREE_CODE (index) != INTEGER_CST
1888 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1889 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1890 {
1891 if (!c_mark_addressable (array))
1892 return error_mark_node;
1893 }
1894 /* An array that is indexed by a constant value which is not within
1895 the array bounds cannot be stored in a register either; because we
1896 would get a crash in store_bit_field/extract_bit_field when trying
1897 to access a non-existent part of the register. */
1898 if (TREE_CODE (index) == INTEGER_CST
1899 && TYPE_DOMAIN (TREE_TYPE (array))
1900 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1901 {
1902 if (!c_mark_addressable (array))
1903 return error_mark_node;
1904 }
1905
1906 if (pedantic)
1907 {
1908 tree foo = array;
1909 while (TREE_CODE (foo) == COMPONENT_REF)
1910 foo = TREE_OPERAND (foo, 0);
1911 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1912 pedwarn ("ISO C forbids subscripting %<register%> array");
1913 else if (!flag_isoc99 && !lvalue_p (foo))
1914 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1915 }
1916
1917 type = TREE_TYPE (TREE_TYPE (array));
1918 if (TREE_CODE (type) != ARRAY_TYPE)
1919 type = TYPE_MAIN_VARIANT (type);
1920 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1921 /* Array ref is const/volatile if the array elements are
1922 or if the array is. */
1923 TREE_READONLY (rval)
1924 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1925 | TREE_READONLY (array));
1926 TREE_SIDE_EFFECTS (rval)
1927 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1928 | TREE_SIDE_EFFECTS (array));
1929 TREE_THIS_VOLATILE (rval)
1930 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1931 /* This was added by rms on 16 Nov 91.
1932 It fixes vol struct foo *a; a->elts[1]
1933 in an inline function.
1934 Hope it doesn't break something else. */
1935 | TREE_THIS_VOLATILE (array));
1936 return require_complete_type (fold (rval));
1937 }
1938 else
1939 {
1940 tree ar = default_conversion (array);
1941
1942 if (ar == error_mark_node)
1943 return ar;
1944
1945 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1946 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1947
1948 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1949 "array indexing");
1950 }
1951 }
1952
1953 /* Build an external reference to identifier ID. FUN indicates
1954 whether this will be used for a function call. LOC is the source
1955 location of the identifier. */
1956 tree
build_external_ref(tree id,int fun,location_t loc)1957 build_external_ref (tree id, int fun, location_t loc)
1958 {
1959 tree ref;
1960 tree decl = lookup_name (id);
1961
1962 /* In Objective-C, an instance variable (ivar) may be preferred to
1963 whatever lookup_name() found. */
1964 decl = objc_lookup_ivar (decl, id);
1965
1966 if (decl && decl != error_mark_node)
1967 ref = decl;
1968 else if (fun)
1969 /* Implicit function declaration. */
1970 ref = implicitly_declare (id);
1971 else if (decl == error_mark_node)
1972 /* Don't complain about something that's already been
1973 complained about. */
1974 return error_mark_node;
1975 else
1976 {
1977 undeclared_variable (id, loc);
1978 return error_mark_node;
1979 }
1980
1981 if (TREE_TYPE (ref) == error_mark_node)
1982 return error_mark_node;
1983
1984 if (TREE_DEPRECATED (ref))
1985 warn_deprecated_use (ref);
1986
1987 if (!skip_evaluation)
1988 assemble_external (ref);
1989 TREE_USED (ref) = 1;
1990
1991 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1992 {
1993 if (!in_sizeof && !in_typeof)
1994 C_DECL_USED (ref) = 1;
1995 else if (DECL_INITIAL (ref) == 0
1996 && DECL_EXTERNAL (ref)
1997 && !TREE_PUBLIC (ref))
1998 record_maybe_used_decl (ref);
1999 }
2000
2001 if (TREE_CODE (ref) == CONST_DECL)
2002 {
2003 ref = DECL_INITIAL (ref);
2004 TREE_CONSTANT (ref) = 1;
2005 TREE_INVARIANT (ref) = 1;
2006 }
2007 else if (current_function_decl != 0
2008 && !DECL_FILE_SCOPE_P (current_function_decl)
2009 && (TREE_CODE (ref) == VAR_DECL
2010 || TREE_CODE (ref) == PARM_DECL
2011 || TREE_CODE (ref) == FUNCTION_DECL))
2012 {
2013 tree context = decl_function_context (ref);
2014
2015 if (context != 0 && context != current_function_decl)
2016 DECL_NONLOCAL (ref) = 1;
2017 }
2018
2019 return ref;
2020 }
2021
2022 /* Record details of decls possibly used inside sizeof or typeof. */
2023 struct maybe_used_decl
2024 {
2025 /* The decl. */
2026 tree decl;
2027 /* The level seen at (in_sizeof + in_typeof). */
2028 int level;
2029 /* The next one at this level or above, or NULL. */
2030 struct maybe_used_decl *next;
2031 };
2032
2033 static struct maybe_used_decl *maybe_used_decls;
2034
2035 /* Record that DECL, an undefined static function reference seen
2036 inside sizeof or typeof, might be used if the operand of sizeof is
2037 a VLA type or the operand of typeof is a variably modified
2038 type. */
2039
2040 static void
record_maybe_used_decl(tree decl)2041 record_maybe_used_decl (tree decl)
2042 {
2043 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2044 t->decl = decl;
2045 t->level = in_sizeof + in_typeof;
2046 t->next = maybe_used_decls;
2047 maybe_used_decls = t;
2048 }
2049
2050 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2051 USED is false, just discard them. If it is true, mark them used
2052 (if no longer inside sizeof or typeof) or move them to the next
2053 level up (if still inside sizeof or typeof). */
2054
2055 void
pop_maybe_used(bool used)2056 pop_maybe_used (bool used)
2057 {
2058 struct maybe_used_decl *p = maybe_used_decls;
2059 int cur_level = in_sizeof + in_typeof;
2060 while (p && p->level > cur_level)
2061 {
2062 if (used)
2063 {
2064 if (cur_level == 0)
2065 C_DECL_USED (p->decl) = 1;
2066 else
2067 p->level = cur_level;
2068 }
2069 p = p->next;
2070 }
2071 if (!used || cur_level == 0)
2072 maybe_used_decls = p;
2073 }
2074
2075 /* Return the result of sizeof applied to EXPR. */
2076
2077 struct c_expr
c_expr_sizeof_expr(struct c_expr expr)2078 c_expr_sizeof_expr (struct c_expr expr)
2079 {
2080 struct c_expr ret;
2081 if (expr.value == error_mark_node)
2082 {
2083 ret.value = error_mark_node;
2084 ret.original_code = ERROR_MARK;
2085 pop_maybe_used (false);
2086 }
2087 else
2088 {
2089 ret.value = c_sizeof (TREE_TYPE (expr.value));
2090 ret.original_code = ERROR_MARK;
2091 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2092 }
2093 return ret;
2094 }
2095
2096 /* Return the result of sizeof applied to T, a structure for the type
2097 name passed to sizeof (rather than the type itself). */
2098
2099 struct c_expr
c_expr_sizeof_type(struct c_type_name * t)2100 c_expr_sizeof_type (struct c_type_name *t)
2101 {
2102 tree type;
2103 struct c_expr ret;
2104 type = groktypename (t);
2105 ret.value = c_sizeof (type);
2106 ret.original_code = ERROR_MARK;
2107 pop_maybe_used (type != error_mark_node
2108 ? C_TYPE_VARIABLE_SIZE (type) : false);
2109 return ret;
2110 }
2111
2112 /* Build a function call to function FUNCTION with parameters PARAMS.
2113 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2114 TREE_VALUE of each node is a parameter-expression.
2115 FUNCTION's data type may be a function type or a pointer-to-function. */
2116
2117 tree
build_function_call(tree function,tree params)2118 build_function_call (tree function, tree params)
2119 {
2120 tree fntype, fundecl = 0;
2121 tree coerced_params;
2122 tree name = NULL_TREE, result;
2123 tree tem;
2124
2125 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2126 STRIP_TYPE_NOPS (function);
2127
2128 /* Convert anything with function type to a pointer-to-function. */
2129 if (TREE_CODE (function) == FUNCTION_DECL)
2130 {
2131 /* Implement type-directed function overloading for builtins.
2132 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2133 handle all the type checking. The result is a complete expression
2134 that implements this function call. */
2135 tem = resolve_overloaded_builtin (function, params);
2136 if (tem)
2137 return tem;
2138
2139 name = DECL_NAME (function);
2140 fundecl = function;
2141 }
2142 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2143 function = function_to_pointer_conversion (function);
2144
2145 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2146 expressions, like those used for ObjC messenger dispatches. */
2147 function = objc_rewrite_function_call (function, params);
2148
2149 fntype = TREE_TYPE (function);
2150
2151 if (TREE_CODE (fntype) == ERROR_MARK)
2152 return error_mark_node;
2153
2154 if (!(TREE_CODE (fntype) == POINTER_TYPE
2155 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2156 {
2157 error ("called object %qE is not a function", function);
2158 return error_mark_node;
2159 }
2160
2161 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2162 current_function_returns_abnormally = 1;
2163
2164 /* fntype now gets the type of function pointed to. */
2165 fntype = TREE_TYPE (fntype);
2166
2167 #if 0
2168 /* Check that the function is called through a compatible prototype.
2169 If it is not, replace the call by a trap, wrapped up in a compound
2170 expression if necessary. This has the nice side-effect to prevent
2171 the tree-inliner from generating invalid assignment trees which may
2172 blow up in the RTL expander later. */
2173 if (TREE_CODE (function) == NOP_EXPR
2174 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2175 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2176 && !comptypes (fntype, TREE_TYPE (tem)))
2177 {
2178 tree return_type = TREE_TYPE (fntype);
2179 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2180 NULL_TREE);
2181
2182 /* This situation leads to run-time undefined behavior. We can't,
2183 therefore, simply error unless we can prove that all possible
2184 executions of the program must execute the code. */
2185 warning (0, "function called through a non-compatible type");
2186
2187 /* We can, however, treat "undefined" any way we please.
2188 Call abort to encourage the user to fix the program. */
2189 inform ("if this code is reached, the program will abort");
2190
2191 if (VOID_TYPE_P (return_type))
2192 return trap;
2193 else
2194 {
2195 tree rhs;
2196
2197 if (AGGREGATE_TYPE_P (return_type))
2198 rhs = build_compound_literal (return_type,
2199 build_constructor (return_type, 0));
2200 else
2201 rhs = fold_build1 (NOP_EXPR, return_type, integer_zero_node);
2202
2203 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2204 }
2205 }
2206 #endif /* 0 */
2207
2208 /* Convert the parameters to the types declared in the
2209 function prototype, or apply default promotions. */
2210
2211 coerced_params
2212 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2213
2214 if (coerced_params == error_mark_node)
2215 return error_mark_node;
2216
2217 /* Check that the arguments to the function are valid. */
2218
2219 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2220 TYPE_ARG_TYPES (fntype));
2221
2222 if (require_constant_value)
2223 {
2224 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2225 function, coerced_params, NULL_TREE);
2226
2227 if (TREE_CONSTANT (result)
2228 && (name == NULL_TREE
2229 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2230 pedwarn_init ("initializer element is not constant");
2231 }
2232 else
2233 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2234 function, coerced_params, NULL_TREE);
2235
2236 if (VOID_TYPE_P (TREE_TYPE (result)))
2237 return result;
2238 return require_complete_type (result);
2239 }
2240
2241 /* Convert the argument expressions in the list VALUES
2242 to the types in the list TYPELIST. The result is a list of converted
2243 argument expressions, unless there are too few arguments in which
2244 case it is error_mark_node.
2245
2246 If TYPELIST is exhausted, or when an element has NULL as its type,
2247 perform the default conversions.
2248
2249 PARMLIST is the chain of parm decls for the function being called.
2250 It may be 0, if that info is not available.
2251 It is used only for generating error messages.
2252
2253 FUNCTION is a tree for the called function. It is used only for
2254 error messages, where it is formatted with %qE.
2255
2256 This is also where warnings about wrong number of args are generated.
2257
2258 Both VALUES and the returned value are chains of TREE_LIST nodes
2259 with the elements of the list in the TREE_VALUE slots of those nodes. */
2260
2261 static tree
convert_arguments(tree typelist,tree values,tree function,tree fundecl)2262 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2263 {
2264 tree typetail, valtail;
2265 tree result = NULL;
2266 int parmnum;
2267 tree selector;
2268
2269 /* Change pointer to function to the function itself for
2270 diagnostics. */
2271 if (TREE_CODE (function) == ADDR_EXPR
2272 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2273 function = TREE_OPERAND (function, 0);
2274
2275 /* Handle an ObjC selector specially for diagnostics. */
2276 selector = objc_message_selector ();
2277
2278 /* Scan the given expressions and types, producing individual
2279 converted arguments and pushing them on RESULT in reverse order. */
2280
2281 for (valtail = values, typetail = typelist, parmnum = 0;
2282 valtail;
2283 valtail = TREE_CHAIN (valtail), parmnum++)
2284 {
2285 tree type = typetail ? TREE_VALUE (typetail) : 0;
2286 tree val = TREE_VALUE (valtail);
2287 tree rname = function;
2288 int argnum = parmnum + 1;
2289 const char *invalid_func_diag;
2290
2291 if (type == void_type_node)
2292 {
2293 error ("too many arguments to function %qE", function);
2294 break;
2295 }
2296
2297 if (selector && argnum > 2)
2298 {
2299 rname = selector;
2300 argnum -= 2;
2301 }
2302
2303 STRIP_TYPE_NOPS (val);
2304
2305 val = require_complete_type (val);
2306
2307 if (type != 0)
2308 {
2309 /* Formal parm type is specified by a function prototype. */
2310 tree parmval;
2311
2312 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2313 {
2314 error ("type of formal parameter %d is incomplete", parmnum + 1);
2315 parmval = val;
2316 }
2317 else
2318 {
2319 /* Optionally warn about conversions that
2320 differ from the default conversions. */
2321 if (warn_conversion || warn_traditional)
2322 {
2323 unsigned int formal_prec = TYPE_PRECISION (type);
2324
2325 if (INTEGRAL_TYPE_P (type)
2326 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2327 warning (0, "passing argument %d of %qE as integer "
2328 "rather than floating due to prototype",
2329 argnum, rname);
2330 if (INTEGRAL_TYPE_P (type)
2331 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2332 warning (0, "passing argument %d of %qE as integer "
2333 "rather than complex due to prototype",
2334 argnum, rname);
2335 else if (TREE_CODE (type) == COMPLEX_TYPE
2336 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2337 warning (0, "passing argument %d of %qE as complex "
2338 "rather than floating due to prototype",
2339 argnum, rname);
2340 else if (TREE_CODE (type) == REAL_TYPE
2341 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2342 warning (0, "passing argument %d of %qE as floating "
2343 "rather than integer due to prototype",
2344 argnum, rname);
2345 else if (TREE_CODE (type) == COMPLEX_TYPE
2346 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2347 warning (0, "passing argument %d of %qE as complex "
2348 "rather than integer due to prototype",
2349 argnum, rname);
2350 else if (TREE_CODE (type) == REAL_TYPE
2351 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2352 warning (0, "passing argument %d of %qE as floating "
2353 "rather than complex due to prototype",
2354 argnum, rname);
2355 /* ??? At some point, messages should be written about
2356 conversions between complex types, but that's too messy
2357 to do now. */
2358 else if (TREE_CODE (type) == REAL_TYPE
2359 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2360 {
2361 /* Warn if any argument is passed as `float',
2362 since without a prototype it would be `double'. */
2363 if (formal_prec == TYPE_PRECISION (float_type_node))
2364 warning (0, "passing argument %d of %qE as %<float%> "
2365 "rather than %<double%> due to prototype",
2366 argnum, rname);
2367 }
2368 /* Detect integer changing in width or signedness.
2369 These warnings are only activated with
2370 -Wconversion, not with -Wtraditional. */
2371 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2372 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2373 {
2374 tree would_have_been = default_conversion (val);
2375 tree type1 = TREE_TYPE (would_have_been);
2376
2377 if (TREE_CODE (type) == ENUMERAL_TYPE
2378 && (TYPE_MAIN_VARIANT (type)
2379 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2380 /* No warning if function asks for enum
2381 and the actual arg is that enum type. */
2382 ;
2383 else if (formal_prec != TYPE_PRECISION (type1))
2384 warning (OPT_Wconversion, "passing argument %d of %qE "
2385 "with different width due to prototype",
2386 argnum, rname);
2387 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2388 ;
2389 /* Don't complain if the formal parameter type
2390 is an enum, because we can't tell now whether
2391 the value was an enum--even the same enum. */
2392 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2393 ;
2394 else if (TREE_CODE (val) == INTEGER_CST
2395 && int_fits_type_p (val, type))
2396 /* Change in signedness doesn't matter
2397 if a constant value is unaffected. */
2398 ;
2399 /* If the value is extended from a narrower
2400 unsigned type, it doesn't matter whether we
2401 pass it as signed or unsigned; the value
2402 certainly is the same either way. */
2403 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2404 && TYPE_UNSIGNED (TREE_TYPE (val)))
2405 ;
2406 else if (TYPE_UNSIGNED (type))
2407 warning (OPT_Wconversion, "passing argument %d of %qE "
2408 "as unsigned due to prototype",
2409 argnum, rname);
2410 else
2411 warning (OPT_Wconversion, "passing argument %d of %qE "
2412 "as signed due to prototype", argnum, rname);
2413 }
2414 }
2415
2416 parmval = convert_for_assignment (type, val, ic_argpass,
2417 fundecl, function,
2418 parmnum + 1);
2419
2420 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2421 && INTEGRAL_TYPE_P (type)
2422 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2423 parmval = default_conversion (parmval);
2424 }
2425 result = tree_cons (NULL_TREE, parmval, result);
2426 }
2427 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2428 && (TYPE_PRECISION (TREE_TYPE (val))
2429 < TYPE_PRECISION (double_type_node)))
2430 /* Convert `float' to `double'. */
2431 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2432 else if ((invalid_func_diag =
2433 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2434 {
2435 error (invalid_func_diag);
2436 return error_mark_node;
2437 }
2438 else
2439 /* Convert `short' and `char' to full-size `int'. */
2440 result = tree_cons (NULL_TREE, default_conversion (val), result);
2441
2442 if (typetail)
2443 typetail = TREE_CHAIN (typetail);
2444 }
2445
2446 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2447 {
2448 error ("too few arguments to function %qE", function);
2449 return error_mark_node;
2450 }
2451
2452 return nreverse (result);
2453 }
2454
2455 /* This is the entry point used by the parser to build unary operators
2456 in the input. CODE, a tree_code, specifies the unary operator, and
2457 ARG is the operand. For unary plus, the C parser currently uses
2458 CONVERT_EXPR for code. */
2459
2460 struct c_expr
parser_build_unary_op(enum tree_code code,struct c_expr arg)2461 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2462 {
2463 struct c_expr result;
2464
2465 result.original_code = ERROR_MARK;
2466 result.value = build_unary_op (code, arg.value, 0);
2467 overflow_warning (result.value);
2468 return result;
2469 }
2470
2471 /* This is the entry point used by the parser to build binary operators
2472 in the input. CODE, a tree_code, specifies the binary operator, and
2473 ARG1 and ARG2 are the operands. In addition to constructing the
2474 expression, we check for operands that were written with other binary
2475 operators in a way that is likely to confuse the user. */
2476
2477 struct c_expr
parser_build_binary_op(enum tree_code code,struct c_expr arg1,struct c_expr arg2)2478 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2479 struct c_expr arg2)
2480 {
2481 struct c_expr result;
2482
2483 enum tree_code code1 = arg1.original_code;
2484 enum tree_code code2 = arg2.original_code;
2485
2486 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2487 result.original_code = code;
2488
2489 if (TREE_CODE (result.value) == ERROR_MARK)
2490 return result;
2491
2492 /* Check for cases such as x+y<<z which users are likely
2493 to misinterpret. */
2494 if (warn_parentheses)
2495 {
2496 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2497 {
2498 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2499 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2500 warning (OPT_Wparentheses,
2501 "suggest parentheses around + or - inside shift");
2502 }
2503
2504 if (code == TRUTH_ORIF_EXPR)
2505 {
2506 if (code1 == TRUTH_ANDIF_EXPR
2507 || code2 == TRUTH_ANDIF_EXPR)
2508 warning (OPT_Wparentheses,
2509 "suggest parentheses around && within ||");
2510 }
2511
2512 if (code == BIT_IOR_EXPR)
2513 {
2514 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2515 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2516 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2517 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2518 warning (OPT_Wparentheses,
2519 "suggest parentheses around arithmetic in operand of |");
2520 /* Check cases like x|y==z */
2521 if (TREE_CODE_CLASS (code1) == tcc_comparison
2522 || TREE_CODE_CLASS (code2) == tcc_comparison)
2523 warning (OPT_Wparentheses,
2524 "suggest parentheses around comparison in operand of |");
2525 }
2526
2527 if (code == BIT_XOR_EXPR)
2528 {
2529 if (code1 == BIT_AND_EXPR
2530 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2531 || code2 == BIT_AND_EXPR
2532 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2533 warning (OPT_Wparentheses,
2534 "suggest parentheses around arithmetic in operand of ^");
2535 /* Check cases like x^y==z */
2536 if (TREE_CODE_CLASS (code1) == tcc_comparison
2537 || TREE_CODE_CLASS (code2) == tcc_comparison)
2538 warning (OPT_Wparentheses,
2539 "suggest parentheses around comparison in operand of ^");
2540 }
2541
2542 if (code == BIT_AND_EXPR)
2543 {
2544 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2545 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2546 warning (OPT_Wparentheses,
2547 "suggest parentheses around + or - in operand of &");
2548 /* Check cases like x&y==z */
2549 if (TREE_CODE_CLASS (code1) == tcc_comparison
2550 || TREE_CODE_CLASS (code2) == tcc_comparison)
2551 warning (OPT_Wparentheses,
2552 "suggest parentheses around comparison in operand of &");
2553 }
2554 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2555 if (TREE_CODE_CLASS (code) == tcc_comparison
2556 && (TREE_CODE_CLASS (code1) == tcc_comparison
2557 || TREE_CODE_CLASS (code2) == tcc_comparison))
2558 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2559 "have their mathematical meaning");
2560
2561 }
2562
2563 unsigned_conversion_warning (result.value, arg1.value);
2564 unsigned_conversion_warning (result.value, arg2.value);
2565 overflow_warning (result.value);
2566
2567 return result;
2568 }
2569
2570 /* Return a tree for the difference of pointers OP0 and OP1.
2571 The resulting tree has type int. */
2572
2573 static tree
pointer_diff(tree op0,tree op1)2574 pointer_diff (tree op0, tree op1)
2575 {
2576 tree restype = ptrdiff_type_node;
2577
2578 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2579 tree con0, con1, lit0, lit1;
2580 tree orig_op1 = op1;
2581
2582 if (pedantic || warn_pointer_arith)
2583 {
2584 if (TREE_CODE (target_type) == VOID_TYPE)
2585 pedwarn ("pointer of type %<void *%> used in subtraction");
2586 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2587 pedwarn ("pointer to a function used in subtraction");
2588 }
2589
2590 /* If the conversion to ptrdiff_type does anything like widening or
2591 converting a partial to an integral mode, we get a convert_expression
2592 that is in the way to do any simplifications.
2593 (fold-const.c doesn't know that the extra bits won't be needed.
2594 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2595 different mode in place.)
2596 So first try to find a common term here 'by hand'; we want to cover
2597 at least the cases that occur in legal static initializers. */
2598 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2599 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2600
2601 if (TREE_CODE (con0) == PLUS_EXPR)
2602 {
2603 lit0 = TREE_OPERAND (con0, 1);
2604 con0 = TREE_OPERAND (con0, 0);
2605 }
2606 else
2607 lit0 = integer_zero_node;
2608
2609 if (TREE_CODE (con1) == PLUS_EXPR)
2610 {
2611 lit1 = TREE_OPERAND (con1, 1);
2612 con1 = TREE_OPERAND (con1, 0);
2613 }
2614 else
2615 lit1 = integer_zero_node;
2616
2617 if (operand_equal_p (con0, con1, 0))
2618 {
2619 op0 = lit0;
2620 op1 = lit1;
2621 }
2622
2623
2624 /* First do the subtraction as integers;
2625 then drop through to build the divide operator.
2626 Do not do default conversions on the minus operator
2627 in case restype is a short type. */
2628
2629 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2630 convert (restype, op1), 0);
2631 /* This generates an error if op1 is pointer to incomplete type. */
2632 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2633 error ("arithmetic on pointer to an incomplete type");
2634
2635 /* This generates an error if op0 is pointer to incomplete type. */
2636 op1 = c_size_in_bytes (target_type);
2637
2638 /* Divide by the size, in easiest possible way. */
2639 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2640 }
2641
2642 /* Construct and perhaps optimize a tree representation
2643 for a unary operation. CODE, a tree_code, specifies the operation
2644 and XARG is the operand.
2645 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2646 the default promotions (such as from short to int).
2647 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2648 allows non-lvalues; this is only used to handle conversion of non-lvalue
2649 arrays to pointers in C99. */
2650
2651 tree
build_unary_op(enum tree_code code,tree xarg,int flag)2652 build_unary_op (enum tree_code code, tree xarg, int flag)
2653 {
2654 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2655 tree arg = xarg;
2656 tree argtype = 0;
2657 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2658 tree val;
2659 int noconvert = flag;
2660 const char *invalid_op_diag;
2661
2662 if (typecode == ERROR_MARK)
2663 return error_mark_node;
2664 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2665 typecode = INTEGER_TYPE;
2666
2667 if ((invalid_op_diag
2668 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2669 {
2670 error (invalid_op_diag);
2671 return error_mark_node;
2672 }
2673
2674 switch (code)
2675 {
2676 case CONVERT_EXPR:
2677 /* This is used for unary plus, because a CONVERT_EXPR
2678 is enough to prevent anybody from looking inside for
2679 associativity, but won't generate any code. */
2680 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2681 || typecode == COMPLEX_TYPE
2682 || typecode == VECTOR_TYPE))
2683 {
2684 error ("wrong type argument to unary plus");
2685 return error_mark_node;
2686 }
2687 else if (!noconvert)
2688 arg = default_conversion (arg);
2689 arg = non_lvalue (arg);
2690 break;
2691
2692 case NEGATE_EXPR:
2693 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2694 || typecode == COMPLEX_TYPE
2695 || typecode == VECTOR_TYPE))
2696 {
2697 error ("wrong type argument to unary minus");
2698 return error_mark_node;
2699 }
2700 else if (!noconvert)
2701 arg = default_conversion (arg);
2702 break;
2703
2704 case BIT_NOT_EXPR:
2705 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2706 {
2707 if (!noconvert)
2708 arg = default_conversion (arg);
2709 }
2710 else if (typecode == COMPLEX_TYPE)
2711 {
2712 code = CONJ_EXPR;
2713 if (pedantic)
2714 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2715 if (!noconvert)
2716 arg = default_conversion (arg);
2717 }
2718 else
2719 {
2720 error ("wrong type argument to bit-complement");
2721 return error_mark_node;
2722 }
2723 break;
2724
2725 case ABS_EXPR:
2726 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2727 {
2728 error ("wrong type argument to abs");
2729 return error_mark_node;
2730 }
2731 else if (!noconvert)
2732 arg = default_conversion (arg);
2733 break;
2734
2735 case CONJ_EXPR:
2736 /* Conjugating a real value is a no-op, but allow it anyway. */
2737 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2738 || typecode == COMPLEX_TYPE))
2739 {
2740 error ("wrong type argument to conjugation");
2741 return error_mark_node;
2742 }
2743 else if (!noconvert)
2744 arg = default_conversion (arg);
2745 break;
2746
2747 case TRUTH_NOT_EXPR:
2748 if (typecode != INTEGER_TYPE
2749 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2750 && typecode != COMPLEX_TYPE)
2751 {
2752 error ("wrong type argument to unary exclamation mark");
2753 return error_mark_node;
2754 }
2755 arg = c_objc_common_truthvalue_conversion (arg);
2756 return invert_truthvalue (arg);
2757
2758 case NOP_EXPR:
2759 break;
2760
2761 case REALPART_EXPR:
2762 if (TREE_CODE (arg) == COMPLEX_CST)
2763 return TREE_REALPART (arg);
2764 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2765 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2766 else
2767 return arg;
2768
2769 case IMAGPART_EXPR:
2770 if (TREE_CODE (arg) == COMPLEX_CST)
2771 return TREE_IMAGPART (arg);
2772 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2773 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2774 else
2775 return convert (TREE_TYPE (arg), integer_zero_node);
2776
2777 case PREINCREMENT_EXPR:
2778 case POSTINCREMENT_EXPR:
2779 case PREDECREMENT_EXPR:
2780 case POSTDECREMENT_EXPR:
2781 /* Handle complex lvalues (when permitted)
2782 by reduction to simpler cases. */
2783
2784 val = unary_complex_lvalue (code, arg, 0);
2785 if (val != 0)
2786 return val;
2787
2788 /* Increment or decrement the real part of the value,
2789 and don't change the imaginary part. */
2790 if (typecode == COMPLEX_TYPE)
2791 {
2792 tree real, imag;
2793
2794 if (pedantic)
2795 pedwarn ("ISO C does not support %<++%> and %<--%>"
2796 " on complex types");
2797
2798 arg = stabilize_reference (arg);
2799 real = build_unary_op (REALPART_EXPR, arg, 1);
2800 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2801 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2802 build_unary_op (code, real, 1), imag);
2803 }
2804
2805 /* Report invalid types. */
2806
2807 if (typecode != POINTER_TYPE
2808 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2809 {
2810 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2811 error ("wrong type argument to increment");
2812 else
2813 error ("wrong type argument to decrement");
2814
2815 return error_mark_node;
2816 }
2817
2818 {
2819 tree inc;
2820 tree result_type = TREE_TYPE (arg);
2821
2822 arg = get_unwidened (arg, 0);
2823 argtype = TREE_TYPE (arg);
2824
2825 /* Compute the increment. */
2826
2827 if (typecode == POINTER_TYPE)
2828 {
2829 /* If pointer target is an undefined struct,
2830 we just cannot know how to do the arithmetic. */
2831 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2832 {
2833 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2834 error ("increment of pointer to unknown structure");
2835 else
2836 error ("decrement of pointer to unknown structure");
2837 }
2838 else if ((pedantic || warn_pointer_arith)
2839 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2840 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2841 {
2842 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2843 pedwarn ("wrong type argument to increment");
2844 else
2845 pedwarn ("wrong type argument to decrement");
2846 }
2847
2848 inc = c_size_in_bytes (TREE_TYPE (result_type));
2849 }
2850 else
2851 inc = integer_one_node;
2852
2853 inc = convert (argtype, inc);
2854
2855 /* Handle incrementing a cast-expression. */
2856
2857 while (1)
2858 switch (TREE_CODE (arg))
2859 {
2860 case NOP_EXPR:
2861 case CONVERT_EXPR:
2862 case FLOAT_EXPR:
2863 case FIX_TRUNC_EXPR:
2864 case FIX_FLOOR_EXPR:
2865 case FIX_ROUND_EXPR:
2866 case FIX_CEIL_EXPR:
2867 pedantic_lvalue_warning (CONVERT_EXPR);
2868 /* If the real type has the same machine representation
2869 as the type it is cast to, we can make better output
2870 by adding directly to the inside of the cast. */
2871 if ((TREE_CODE (TREE_TYPE (arg))
2872 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2873 && (TYPE_MODE (TREE_TYPE (arg))
2874 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2875 arg = TREE_OPERAND (arg, 0);
2876 else
2877 {
2878 tree incremented, modify, value;
2879 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2880 value = boolean_increment (code, arg);
2881 else
2882 {
2883 arg = stabilize_reference (arg);
2884 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2885 value = arg;
2886 else
2887 value = save_expr (arg);
2888 incremented = build (((code == PREINCREMENT_EXPR
2889 || code == POSTINCREMENT_EXPR)
2890 ? PLUS_EXPR : MINUS_EXPR),
2891 argtype, value, inc);
2892 TREE_SIDE_EFFECTS (incremented) = 1;
2893 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2894 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2895 }
2896 TREE_USED (value) = 1;
2897 return value;
2898 }
2899 break;
2900
2901 default:
2902 goto give_up;
2903 }
2904 give_up:
2905
2906 /* Complain about anything else that is not a true lvalue. */
2907 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2908 || code == POSTINCREMENT_EXPR)
2909 ? lv_increment
2910 : lv_decrement)))
2911 return error_mark_node;
2912
2913 /* Report a read-only lvalue. */
2914 if (TREE_READONLY (arg))
2915 readonly_error (arg,
2916 ((code == PREINCREMENT_EXPR
2917 || code == POSTINCREMENT_EXPR)
2918 ? lv_increment : lv_decrement));
2919
2920 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2921 val = boolean_increment (code, arg);
2922 else
2923 val = build2 (code, TREE_TYPE (arg), arg, inc);
2924 TREE_SIDE_EFFECTS (val) = 1;
2925 val = convert (result_type, val);
2926 if (TREE_CODE (val) != code)
2927 TREE_NO_WARNING (val) = 1;
2928 return val;
2929 }
2930
2931 case ADDR_EXPR:
2932 /* Note that this operation never does default_conversion. */
2933
2934 /* Let &* cancel out to simplify resulting code. */
2935 if (TREE_CODE (arg) == INDIRECT_REF)
2936 {
2937 /* Don't let this be an lvalue. */
2938 if (lvalue_p (TREE_OPERAND (arg, 0)))
2939 return non_lvalue (TREE_OPERAND (arg, 0));
2940 return TREE_OPERAND (arg, 0);
2941 }
2942
2943 /* For &x[y], return x+y */
2944 if (TREE_CODE (arg) == ARRAY_REF)
2945 {
2946 tree op0 = TREE_OPERAND (arg, 0);
2947 if (!c_mark_addressable (op0))
2948 return error_mark_node;
2949 return build_binary_op (PLUS_EXPR,
2950 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
2951 ? array_to_pointer_conversion (op0)
2952 : op0),
2953 TREE_OPERAND (arg, 1), 1);
2954 }
2955
2956 /* Handle complex lvalues (when permitted)
2957 by reduction to simpler cases. */
2958 val = unary_complex_lvalue (code, arg, flag);
2959 if (val != 0)
2960 return val;
2961
2962 /* Anything not already handled and not a true memory reference
2963 or a non-lvalue array is an error. */
2964 else if (typecode != FUNCTION_TYPE && !flag
2965 && !lvalue_or_else (arg, lv_addressof))
2966 return error_mark_node;
2967
2968 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2969 argtype = TREE_TYPE (arg);
2970
2971 /* If the lvalue is const or volatile, merge that into the type
2972 to which the address will point. Note that you can't get a
2973 restricted pointer by taking the address of something, so we
2974 only have to deal with `const' and `volatile' here. */
2975 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2976 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2977 argtype = c_build_type_variant (argtype,
2978 TREE_READONLY (arg),
2979 TREE_THIS_VOLATILE (arg));
2980
2981 if (!c_mark_addressable (arg))
2982 return error_mark_node;
2983
2984 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2985 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2986
2987 argtype = build_pointer_type (argtype);
2988
2989 /* ??? Cope with user tricks that amount to offsetof. Delete this
2990 when we have proper support for integer constant expressions. */
2991 val = get_base_address (arg);
2992 if (val && TREE_CODE (val) == INDIRECT_REF
2993 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
2994 {
2995 tree op0 = fold_convert (argtype, fold_offsetof (arg)), op1;
2996
2997 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
2998 return fold_build2 (PLUS_EXPR, argtype, op0, op1);
2999 }
3000
3001 val = build1 (ADDR_EXPR, argtype, arg);
3002
3003 return val;
3004
3005 default:
3006 break;
3007 }
3008
3009 if (argtype == 0)
3010 argtype = TREE_TYPE (arg);
3011 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
3012 : fold_build1 (code, argtype, arg);
3013 }
3014
3015 /* Return nonzero if REF is an lvalue valid for this language.
3016 Lvalues can be assigned, unless their type has TYPE_READONLY.
3017 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3018
3019 static int
lvalue_p(tree ref)3020 lvalue_p (tree ref)
3021 {
3022 enum tree_code code = TREE_CODE (ref);
3023
3024 switch (code)
3025 {
3026 case REALPART_EXPR:
3027 case IMAGPART_EXPR:
3028 case COMPONENT_REF:
3029 return lvalue_p (TREE_OPERAND (ref, 0));
3030
3031 case COMPOUND_LITERAL_EXPR:
3032 case STRING_CST:
3033 return 1;
3034
3035 case INDIRECT_REF:
3036 case ARRAY_REF:
3037 case VAR_DECL:
3038 case PARM_DECL:
3039 case RESULT_DECL:
3040 case ERROR_MARK:
3041 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3042 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3043
3044 case BIND_EXPR:
3045 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3046
3047 default:
3048 return 0;
3049 }
3050 }
3051
3052 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3053 for certain kinds of expressions which are not really lvalues
3054 but which we can accept as lvalues. If FLAG is nonzero, then
3055 non-lvalues are OK since we may be converting a non-lvalue array to
3056 a pointer in C99.
3057
3058 If ARG is not a kind of expression we can handle, return zero. */
3059
3060 static tree
unary_complex_lvalue(enum tree_code code,tree arg,int flag)3061 unary_complex_lvalue (enum tree_code code, tree arg, int flag)
3062 {
3063 /* Handle (a, b) used as an "lvalue". */
3064 if (TREE_CODE (arg) == COMPOUND_EXPR)
3065 {
3066 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3067
3068 /* If this returns a function type, it isn't really being used as
3069 an lvalue, so don't issue a warning about it. */
3070 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3071 pedantic_lvalue_warning (COMPOUND_EXPR);
3072
3073 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3074 TREE_OPERAND (arg, 0), real_result);
3075 }
3076
3077 /* Handle (a ? b : c) used as an "lvalue". */
3078 if (TREE_CODE (arg) == COND_EXPR)
3079 {
3080 if (!flag)
3081 pedantic_lvalue_warning (COND_EXPR);
3082 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3083 pedantic_lvalue_warning (COMPOUND_EXPR);
3084
3085 return (build_conditional_expr
3086 (TREE_OPERAND (arg, 0),
3087 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3088 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
3089 }
3090
3091 return 0;
3092 }
3093
3094 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3095 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3096
3097 static void
pedantic_lvalue_warning(enum tree_code code)3098 pedantic_lvalue_warning (enum tree_code code)
3099 {
3100 if (pedantic)
3101 switch (code)
3102 {
3103 case COND_EXPR:
3104 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3105 break;
3106 case COMPOUND_EXPR:
3107 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3108 break;
3109 default:
3110 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3111 break;
3112 }
3113 }
3114
3115 /* Give an error for storing in something that is 'const'. */
3116
3117 static void
readonly_error(tree arg,enum lvalue_use use)3118 readonly_error (tree arg, enum lvalue_use use)
3119 {
3120 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3121 || use == lv_asm);
3122 /* Using this macro rather than (for example) arrays of messages
3123 ensures that all the format strings are checked at compile
3124 time. */
3125 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3126 : (use == lv_increment ? (I) \
3127 : (use == lv_decrement ? (D) : (AS))))
3128 if (TREE_CODE (arg) == COMPONENT_REF)
3129 {
3130 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3131 readonly_error (TREE_OPERAND (arg, 0), use);
3132 else
3133 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3134 G_("increment of read-only member %qD"),
3135 G_("decrement of read-only member %qD"),
3136 G_("read-only member %qD used as %<asm%> output")),
3137 TREE_OPERAND (arg, 1));
3138 }
3139 else if (TREE_CODE (arg) == VAR_DECL)
3140 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3141 G_("increment of read-only variable %qD"),
3142 G_("decrement of read-only variable %qD"),
3143 G_("read-only variable %qD used as %<asm%> output")),
3144 arg);
3145 else
3146 error (READONLY_MSG (G_("assignment of read-only location"),
3147 G_("increment of read-only location"),
3148 G_("decrement of read-only location"),
3149 G_("read-only location used as %<asm%> output")));
3150 }
3151
3152
3153 /* Return nonzero if REF is an lvalue valid for this language;
3154 otherwise, print an error message and return zero. USE says
3155 how the lvalue is being used and so selects the error message. */
3156
3157 static int
lvalue_or_else(tree ref,enum lvalue_use use)3158 lvalue_or_else (tree ref, enum lvalue_use use)
3159 {
3160 int win = lvalue_p (ref);
3161
3162 if (!win)
3163 lvalue_error (use);
3164
3165 return win;
3166 }
3167
3168 /* Mark EXP saying that we need to be able to take the
3169 address of it; it should not be allocated in a register.
3170 Returns true if successful. */
3171
3172 bool
c_mark_addressable(tree exp)3173 c_mark_addressable (tree exp)
3174 {
3175 tree x = exp;
3176
3177 while (1)
3178 switch (TREE_CODE (x))
3179 {
3180 case COMPONENT_REF:
3181 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3182 {
3183 error
3184 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3185 return false;
3186 }
3187
3188 /* ... fall through ... */
3189
3190 case ADDR_EXPR:
3191 case ARRAY_REF:
3192 case REALPART_EXPR:
3193 case IMAGPART_EXPR:
3194 x = TREE_OPERAND (x, 0);
3195 break;
3196
3197 case COMPOUND_LITERAL_EXPR:
3198 case CONSTRUCTOR:
3199 TREE_ADDRESSABLE (x) = 1;
3200 return true;
3201
3202 case VAR_DECL:
3203 case CONST_DECL:
3204 case PARM_DECL:
3205 case RESULT_DECL:
3206 if (C_DECL_REGISTER (x)
3207 && DECL_NONLOCAL (x))
3208 {
3209 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3210 {
3211 error
3212 ("global register variable %qD used in nested function", x);
3213 return false;
3214 }
3215 pedwarn ("register variable %qD used in nested function", x);
3216 }
3217 else if (C_DECL_REGISTER (x))
3218 {
3219 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3220 error ("address of global register variable %qD requested", x);
3221 else
3222 error ("address of register variable %qD requested", x);
3223 return false;
3224 }
3225
3226 /* drops in */
3227 case FUNCTION_DECL:
3228 TREE_ADDRESSABLE (x) = 1;
3229 /* drops out */
3230 default:
3231 return true;
3232 }
3233 }
3234
3235 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3236
3237 tree
build_conditional_expr(tree ifexp,tree op1,tree op2)3238 build_conditional_expr (tree ifexp, tree op1, tree op2)
3239 {
3240 tree type1;
3241 tree type2;
3242 enum tree_code code1;
3243 enum tree_code code2;
3244 tree result_type = NULL;
3245 tree orig_op1 = op1, orig_op2 = op2;
3246
3247 /* Promote both alternatives. */
3248
3249 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3250 op1 = default_conversion (op1);
3251 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3252 op2 = default_conversion (op2);
3253
3254 if (TREE_CODE (ifexp) == ERROR_MARK
3255 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3256 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3257 return error_mark_node;
3258
3259 type1 = TREE_TYPE (op1);
3260 code1 = TREE_CODE (type1);
3261 type2 = TREE_TYPE (op2);
3262 code2 = TREE_CODE (type2);
3263
3264 /* C90 does not permit non-lvalue arrays in conditional expressions.
3265 In C99 they will be pointers by now. */
3266 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3267 {
3268 error ("non-lvalue array in conditional expression");
3269 return error_mark_node;
3270 }
3271
3272 /* Quickly detect the usual case where op1 and op2 have the same type
3273 after promotion. */
3274 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3275 {
3276 if (type1 == type2)
3277 result_type = type1;
3278 else
3279 result_type = TYPE_MAIN_VARIANT (type1);
3280 }
3281 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3282 || code1 == COMPLEX_TYPE)
3283 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3284 || code2 == COMPLEX_TYPE))
3285 {
3286 result_type = c_common_type (type1, type2);
3287
3288 /* If -Wsign-compare, warn here if type1 and type2 have
3289 different signedness. We'll promote the signed to unsigned
3290 and later code won't know it used to be different.
3291 Do this check on the original types, so that explicit casts
3292 will be considered, but default promotions won't. */
3293 if (warn_sign_compare && !skip_evaluation)
3294 {
3295 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3296 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3297
3298 if (unsigned_op1 ^ unsigned_op2)
3299 {
3300 /* Do not warn if the result type is signed, since the
3301 signed type will only be chosen if it can represent
3302 all the values of the unsigned type. */
3303 if (!TYPE_UNSIGNED (result_type))
3304 /* OK */;
3305 /* Do not warn if the signed quantity is an unsuffixed
3306 integer literal (or some static constant expression
3307 involving such literals) and it is non-negative. */
3308 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3309 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3310 /* OK */;
3311 else
3312 warning (0, "signed and unsigned type in conditional expression");
3313 }
3314 }
3315 }
3316 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3317 {
3318 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3319 pedwarn ("ISO C forbids conditional expr with only one void side");
3320 result_type = void_type_node;
3321 }
3322 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3323 {
3324 if (comp_target_types (type1, type2))
3325 result_type = common_pointer_type (type1, type2);
3326 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3327 && TREE_CODE (orig_op1) != NOP_EXPR)
3328 result_type = qualify_type (type2, type1);
3329 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3330 && TREE_CODE (orig_op2) != NOP_EXPR)
3331 result_type = qualify_type (type1, type2);
3332 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3333 {
3334 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3335 pedwarn ("ISO C forbids conditional expr between "
3336 "%<void *%> and function pointer");
3337 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3338 TREE_TYPE (type2)));
3339 }
3340 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3341 {
3342 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3343 pedwarn ("ISO C forbids conditional expr between "
3344 "%<void *%> and function pointer");
3345 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3346 TREE_TYPE (type1)));
3347 }
3348 else
3349 {
3350 pedwarn ("pointer type mismatch in conditional expression");
3351 result_type = build_pointer_type (void_type_node);
3352 }
3353 }
3354 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3355 {
3356 if (!integer_zerop (op2))
3357 pedwarn ("pointer/integer type mismatch in conditional expression");
3358 else
3359 {
3360 op2 = null_pointer_node;
3361 }
3362 result_type = type1;
3363 }
3364 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3365 {
3366 if (!integer_zerop (op1))
3367 pedwarn ("pointer/integer type mismatch in conditional expression");
3368 else
3369 {
3370 op1 = null_pointer_node;
3371 }
3372 result_type = type2;
3373 }
3374
3375 if (!result_type)
3376 {
3377 if (flag_cond_mismatch)
3378 result_type = void_type_node;
3379 else
3380 {
3381 error ("type mismatch in conditional expression");
3382 return error_mark_node;
3383 }
3384 }
3385
3386 /* Merge const and volatile flags of the incoming types. */
3387 result_type
3388 = build_type_variant (result_type,
3389 TREE_READONLY (op1) || TREE_READONLY (op2),
3390 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3391
3392 if (result_type != TREE_TYPE (op1))
3393 op1 = convert_and_check (result_type, op1);
3394 if (result_type != TREE_TYPE (op2))
3395 op2 = convert_and_check (result_type, op2);
3396
3397 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3398 }
3399
3400 /* Return a compound expression that performs two expressions and
3401 returns the value of the second of them. */
3402
3403 tree
build_compound_expr(tree expr1,tree expr2)3404 build_compound_expr (tree expr1, tree expr2)
3405 {
3406 if (!TREE_SIDE_EFFECTS (expr1))
3407 {
3408 /* The left-hand operand of a comma expression is like an expression
3409 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3410 any side-effects, unless it was explicitly cast to (void). */
3411 if (warn_unused_value)
3412 {
3413 if (VOID_TYPE_P (TREE_TYPE (expr1))
3414 && TREE_CODE (expr1) == CONVERT_EXPR)
3415 ; /* (void) a, b */
3416 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3417 && TREE_CODE (expr1) == COMPOUND_EXPR
3418 && TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR)
3419 ; /* (void) a, (void) b, c */
3420 else
3421 warning (0, "left-hand operand of comma expression has no effect");
3422 }
3423 }
3424
3425 /* With -Wunused, we should also warn if the left-hand operand does have
3426 side-effects, but computes a value which is not used. For example, in
3427 `foo() + bar(), baz()' the result of the `+' operator is not used,
3428 so we should issue a warning. */
3429 else if (warn_unused_value)
3430 warn_if_unused_value (expr1, input_location);
3431
3432 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3433 }
3434
3435 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3436
3437 tree
build_c_cast(tree type,tree expr)3438 build_c_cast (tree type, tree expr)
3439 {
3440 tree value = expr;
3441
3442 if (type == error_mark_node || expr == error_mark_node)
3443 return error_mark_node;
3444
3445 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3446 only in <protocol> qualifications. But when constructing cast expressions,
3447 the protocols do matter and must be kept around. */
3448 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3449 return build1 (NOP_EXPR, type, expr);
3450
3451 type = TYPE_MAIN_VARIANT (type);
3452
3453 if (TREE_CODE (type) == ARRAY_TYPE)
3454 {
3455 error ("cast specifies array type");
3456 return error_mark_node;
3457 }
3458
3459 if (TREE_CODE (type) == FUNCTION_TYPE)
3460 {
3461 error ("cast specifies function type");
3462 return error_mark_node;
3463 }
3464
3465 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3466 {
3467 if (pedantic)
3468 {
3469 if (TREE_CODE (type) == RECORD_TYPE
3470 || TREE_CODE (type) == UNION_TYPE)
3471 pedwarn ("ISO C forbids casting nonscalar to the same type");
3472 }
3473 }
3474 else if (TREE_CODE (type) == UNION_TYPE)
3475 {
3476 tree field;
3477
3478 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3479 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3480 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3481 break;
3482
3483 if (field)
3484 {
3485 tree t;
3486
3487 if (pedantic)
3488 pedwarn ("ISO C forbids casts to union type");
3489 t = digest_init (type,
3490 build_constructor_single (type, field, value),
3491 true, 0);
3492 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3493 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3494 return t;
3495 }
3496 error ("cast to union type from type not present in union");
3497 return error_mark_node;
3498 }
3499 else
3500 {
3501 tree otype, ovalue;
3502
3503 if (type == void_type_node)
3504 return build1 (CONVERT_EXPR, type, value);
3505
3506 otype = TREE_TYPE (value);
3507
3508 /* Optionally warn about potentially worrisome casts. */
3509
3510 if (warn_cast_qual
3511 && TREE_CODE (type) == POINTER_TYPE
3512 && TREE_CODE (otype) == POINTER_TYPE)
3513 {
3514 tree in_type = type;
3515 tree in_otype = otype;
3516 int added = 0;
3517 int discarded = 0;
3518
3519 /* Check that the qualifiers on IN_TYPE are a superset of
3520 the qualifiers of IN_OTYPE. The outermost level of
3521 POINTER_TYPE nodes is uninteresting and we stop as soon
3522 as we hit a non-POINTER_TYPE node on either type. */
3523 do
3524 {
3525 in_otype = TREE_TYPE (in_otype);
3526 in_type = TREE_TYPE (in_type);
3527
3528 /* GNU C allows cv-qualified function types. 'const'
3529 means the function is very pure, 'volatile' means it
3530 can't return. We need to warn when such qualifiers
3531 are added, not when they're taken away. */
3532 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3533 && TREE_CODE (in_type) == FUNCTION_TYPE)
3534 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3535 else
3536 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3537 }
3538 while (TREE_CODE (in_type) == POINTER_TYPE
3539 && TREE_CODE (in_otype) == POINTER_TYPE);
3540
3541 if (added)
3542 warning (0, "cast adds new qualifiers to function type");
3543
3544 if (discarded)
3545 /* There are qualifiers present in IN_OTYPE that are not
3546 present in IN_TYPE. */
3547 warning (0, "cast discards qualifiers from pointer target type");
3548 }
3549
3550 /* Warn about possible alignment problems. */
3551 if (STRICT_ALIGNMENT
3552 && TREE_CODE (type) == POINTER_TYPE
3553 && TREE_CODE (otype) == POINTER_TYPE
3554 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3555 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3556 /* Don't warn about opaque types, where the actual alignment
3557 restriction is unknown. */
3558 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3559 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3560 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3561 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3562 warning (OPT_Wcast_align,
3563 "cast increases required alignment of target type");
3564
3565 if (TREE_CODE (type) == INTEGER_TYPE
3566 && TREE_CODE (otype) == POINTER_TYPE
3567 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3568 && !TREE_CONSTANT (value))
3569 warning (OPT_Wpointer_to_int_cast,
3570 "cast from pointer to integer of different size");
3571
3572 if (TREE_CODE (value) == CALL_EXPR
3573 && TREE_CODE (type) != TREE_CODE (otype))
3574 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3575 "to non-matching type %qT", otype, type);
3576
3577 if (TREE_CODE (type) == POINTER_TYPE
3578 && TREE_CODE (otype) == INTEGER_TYPE
3579 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3580 /* Don't warn about converting any constant. */
3581 && !TREE_CONSTANT (value))
3582 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3583 "of different size");
3584
3585 strict_aliasing_warning (otype, type, expr);
3586
3587 /* If pedantic, warn for conversions between function and object
3588 pointer types, except for converting a null pointer constant
3589 to function pointer type. */
3590 if (pedantic
3591 && TREE_CODE (type) == POINTER_TYPE
3592 && TREE_CODE (otype) == POINTER_TYPE
3593 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3594 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3595 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3596
3597 if (pedantic
3598 && TREE_CODE (type) == POINTER_TYPE
3599 && TREE_CODE (otype) == POINTER_TYPE
3600 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3601 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3602 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3603 && TREE_CODE (expr) != NOP_EXPR))
3604 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3605
3606 ovalue = value;
3607 value = convert (type, value);
3608
3609 /* Ignore any integer overflow caused by the cast. */
3610 if (TREE_CODE (value) == INTEGER_CST)
3611 {
3612 if (CONSTANT_CLASS_P (ovalue)
3613 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3614 {
3615 /* Avoid clobbering a shared constant. */
3616 value = copy_node (value);
3617 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3618 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3619 }
3620 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3621 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3622 value = build_int_cst_wide (TREE_TYPE (value),
3623 TREE_INT_CST_LOW (value),
3624 TREE_INT_CST_HIGH (value));
3625 }
3626 }
3627
3628 /* If pedantic, don't let a cast be an lvalue. */
3629 if (value == expr && pedantic)
3630 value = non_lvalue (value);
3631
3632 return value;
3633 }
3634
3635 /* Interpret a cast of expression EXPR to type TYPE. */
3636 tree
c_cast_expr(struct c_type_name * type_name,tree expr)3637 c_cast_expr (struct c_type_name *type_name, tree expr)
3638 {
3639 tree type;
3640 int saved_wsp = warn_strict_prototypes;
3641
3642 /* This avoids warnings about unprototyped casts on
3643 integers. E.g. "#define SIG_DFL (void(*)())0". */
3644 if (TREE_CODE (expr) == INTEGER_CST)
3645 warn_strict_prototypes = 0;
3646 type = groktypename (type_name);
3647 warn_strict_prototypes = saved_wsp;
3648
3649 return build_c_cast (type, expr);
3650 }
3651
3652
3653 /* Build an assignment expression of lvalue LHS from value RHS.
3654 MODIFYCODE is the code for a binary operator that we use
3655 to combine the old value of LHS with RHS to get the new value.
3656 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3657
3658 tree
build_modify_expr(tree lhs,enum tree_code modifycode,tree rhs)3659 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3660 {
3661 tree result;
3662 tree newrhs;
3663 tree lhstype = TREE_TYPE (lhs);
3664 tree olhstype = lhstype;
3665
3666 /* Types that aren't fully specified cannot be used in assignments. */
3667 lhs = require_complete_type (lhs);
3668
3669 /* Avoid duplicate error messages from operands that had errors. */
3670 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3671 return error_mark_node;
3672
3673 STRIP_TYPE_NOPS (rhs);
3674
3675 newrhs = rhs;
3676
3677 /* Handle control structure constructs used as "lvalues". */
3678
3679 switch (TREE_CODE (lhs))
3680 {
3681 /* Handle (a, b) used as an "lvalue". */
3682 case COMPOUND_EXPR:
3683 pedantic_lvalue_warning (COMPOUND_EXPR);
3684 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3685 if (TREE_CODE (newrhs) == ERROR_MARK)
3686 return error_mark_node;
3687 return build (COMPOUND_EXPR, lhstype,
3688 TREE_OPERAND (lhs, 0), newrhs);
3689
3690 /* Handle (a ? b : c) used as an "lvalue". */
3691 case COND_EXPR:
3692 pedantic_lvalue_warning (COND_EXPR);
3693 rhs = save_expr (rhs);
3694 {
3695 /* Produce (a ? (b = rhs) : (c = rhs))
3696 except that the RHS goes through a save-expr
3697 so the code to compute it is only emitted once. */
3698 tree cond
3699 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3700 build_modify_expr (TREE_OPERAND (lhs, 1),
3701 modifycode, rhs),
3702 build_modify_expr (TREE_OPERAND (lhs, 2),
3703 modifycode, rhs));
3704 if (TREE_CODE (cond) == ERROR_MARK)
3705 return cond;
3706 /* Make sure the code to compute the rhs comes out
3707 before the split. */
3708 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3709 /* But cast it to void to avoid an "unused" error. */
3710 convert (void_type_node, rhs), cond);
3711 }
3712 default:
3713 break;
3714 }
3715
3716 /* If a binary op has been requested, combine the old LHS value with the RHS
3717 producing the value we should actually store into the LHS. */
3718
3719 if (modifycode != NOP_EXPR)
3720 {
3721 lhs = stabilize_reference (lhs);
3722 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3723 }
3724
3725 /* Handle a cast used as an "lvalue".
3726 We have already performed any binary operator using the value as cast.
3727 Now convert the result to the cast type of the lhs,
3728 and then true type of the lhs and store it there;
3729 then convert result back to the cast type to be the value
3730 of the assignment. */
3731
3732 switch (TREE_CODE (lhs))
3733 {
3734 case NOP_EXPR:
3735 case CONVERT_EXPR:
3736 case FLOAT_EXPR:
3737 case FIX_TRUNC_EXPR:
3738 case FIX_FLOOR_EXPR:
3739 case FIX_ROUND_EXPR:
3740 case FIX_CEIL_EXPR:
3741 {
3742 struct c_expr newrhs_expr;
3743 tree inner_lhs = TREE_OPERAND (lhs, 0);
3744 tree result;
3745 newrhs_expr.value = newrhs;
3746 newrhs_expr = default_function_array_conversion (newrhs_expr);
3747 newrhs = newrhs_expr.value;
3748 result = build_modify_expr (inner_lhs, NOP_EXPR,
3749 convert (TREE_TYPE (inner_lhs),
3750 convert (lhstype, newrhs)));
3751 if (TREE_CODE (result) == ERROR_MARK)
3752 return result;
3753 pedantic_lvalue_warning (CONVERT_EXPR);
3754 return convert (TREE_TYPE (lhs), result);
3755 }
3756
3757 default:
3758 break;
3759 }
3760
3761 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3762 Reject anything strange now. */
3763
3764 if (!lvalue_or_else (lhs, lv_assign))
3765 return error_mark_node;
3766
3767 /* Give an error for storing in something that is 'const'. */
3768
3769 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3770 || ((TREE_CODE (lhstype) == RECORD_TYPE
3771 || TREE_CODE (lhstype) == UNION_TYPE)
3772 && C_TYPE_FIELDS_READONLY (lhstype)))
3773 readonly_error (lhs, lv_assign);
3774
3775 /* If storing into a structure or union member,
3776 it has probably been given type `int'.
3777 Compute the type that would go with
3778 the actual amount of storage the member occupies. */
3779
3780 if (TREE_CODE (lhs) == COMPONENT_REF
3781 && (TREE_CODE (lhstype) == INTEGER_TYPE
3782 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3783 || TREE_CODE (lhstype) == REAL_TYPE
3784 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3785 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3786
3787 /* If storing in a field that is in actuality a short or narrower than one,
3788 we must store in the field in its actual type. */
3789
3790 if (lhstype != TREE_TYPE (lhs))
3791 {
3792 lhs = copy_node (lhs);
3793 TREE_TYPE (lhs) = lhstype;
3794 }
3795
3796 /* Convert new value to destination type. */
3797
3798 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3799 NULL_TREE, NULL_TREE, 0);
3800 if (TREE_CODE (newrhs) == ERROR_MARK)
3801 return error_mark_node;
3802
3803 /* Emit ObjC write barrier, if necessary. */
3804 if (c_dialect_objc () && flag_objc_gc)
3805 {
3806 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3807 if (result)
3808 return result;
3809 }
3810
3811 /* Scan operands. */
3812
3813 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3814 TREE_SIDE_EFFECTS (result) = 1;
3815
3816 /* If we got the LHS in a different type for storing in,
3817 convert the result back to the nominal type of LHS
3818 so that the value we return always has the same type
3819 as the LHS argument. */
3820
3821 if (olhstype == TREE_TYPE (result))
3822 return result;
3823 return convert_for_assignment (olhstype, result, ic_assign,
3824 NULL_TREE, NULL_TREE, 0);
3825 }
3826
3827 /* Convert value RHS to type TYPE as preparation for an assignment
3828 to an lvalue of type TYPE.
3829 The real work of conversion is done by `convert'.
3830 The purpose of this function is to generate error messages
3831 for assignments that are not allowed in C.
3832 ERRTYPE says whether it is argument passing, assignment,
3833 initialization or return.
3834
3835 FUNCTION is a tree for the function being called.
3836 PARMNUM is the number of the argument, for printing in error messages. */
3837
3838 static tree
convert_for_assignment(tree type,tree rhs,enum impl_conv errtype,tree fundecl,tree function,int parmnum)3839 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3840 tree fundecl, tree function, int parmnum)
3841 {
3842 enum tree_code codel = TREE_CODE (type);
3843 tree rhstype;
3844 enum tree_code coder;
3845 tree rname = NULL_TREE;
3846 bool objc_ok = false;
3847
3848 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3849 {
3850 tree selector;
3851 /* Change pointer to function to the function itself for
3852 diagnostics. */
3853 if (TREE_CODE (function) == ADDR_EXPR
3854 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3855 function = TREE_OPERAND (function, 0);
3856
3857 /* Handle an ObjC selector specially for diagnostics. */
3858 selector = objc_message_selector ();
3859 rname = function;
3860 if (selector && parmnum > 2)
3861 {
3862 rname = selector;
3863 parmnum -= 2;
3864 }
3865 }
3866
3867 /* This macro is used to emit diagnostics to ensure that all format
3868 strings are complete sentences, visible to gettext and checked at
3869 compile time. */
3870 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3871 do { \
3872 switch (errtype) \
3873 { \
3874 case ic_argpass: \
3875 pedwarn (AR, parmnum, rname); \
3876 break; \
3877 case ic_argpass_nonproto: \
3878 warning (0, AR, parmnum, rname); \
3879 break; \
3880 case ic_assign: \
3881 pedwarn (AS); \
3882 break; \
3883 case ic_init: \
3884 pedwarn (IN); \
3885 break; \
3886 case ic_return: \
3887 pedwarn (RE); \
3888 break; \
3889 default: \
3890 gcc_unreachable (); \
3891 } \
3892 } while (0)
3893
3894 STRIP_TYPE_NOPS (rhs);
3895
3896 if (optimize && TREE_CODE (rhs) == VAR_DECL
3897 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3898 rhs = decl_constant_value_for_broken_optimization (rhs);
3899
3900 rhstype = TREE_TYPE (rhs);
3901 coder = TREE_CODE (rhstype);
3902
3903 if (coder == ERROR_MARK)
3904 return error_mark_node;
3905
3906 if (c_dialect_objc ())
3907 {
3908 int parmno;
3909
3910 switch (errtype)
3911 {
3912 case ic_return:
3913 parmno = 0;
3914 break;
3915
3916 case ic_assign:
3917 parmno = -1;
3918 break;
3919
3920 case ic_init:
3921 parmno = -2;
3922 break;
3923
3924 default:
3925 parmno = parmnum;
3926 break;
3927 }
3928
3929 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3930 }
3931
3932 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3933 {
3934 overflow_warning (rhs);
3935 return rhs;
3936 }
3937
3938 if (coder == VOID_TYPE)
3939 {
3940 /* Except for passing an argument to an unprototyped function,
3941 this is a constraint violation. When passing an argument to
3942 an unprototyped function, it is compile-time undefined;
3943 making it a constraint in that case was rejected in
3944 DR#252. */
3945 error ("void value not ignored as it ought to be");
3946 return error_mark_node;
3947 }
3948 /* A type converts to a reference to it.
3949 This code doesn't fully support references, it's just for the
3950 special case of va_start and va_copy. */
3951 if (codel == REFERENCE_TYPE
3952 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3953 {
3954 if (!lvalue_p (rhs))
3955 {
3956 error ("cannot pass rvalue to reference parameter");
3957 return error_mark_node;
3958 }
3959 if (!c_mark_addressable (rhs))
3960 return error_mark_node;
3961 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3962
3963 /* We already know that these two types are compatible, but they
3964 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3965 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3966 likely to be va_list, a typedef to __builtin_va_list, which
3967 is different enough that it will cause problems later. */
3968 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3969 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3970
3971 rhs = build1 (NOP_EXPR, type, rhs);
3972 return rhs;
3973 }
3974 /* Some types can interconvert without explicit casts. */
3975 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3976 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3977 return convert (type, rhs);
3978 /* Arithmetic types all interconvert, and enum is treated like int. */
3979 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3980 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3981 || codel == BOOLEAN_TYPE)
3982 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3983 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3984 || coder == BOOLEAN_TYPE))
3985 return convert_and_check (type, rhs);
3986
3987 /* Conversion to a transparent union from its member types.
3988 This applies only to function arguments. */
3989 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3990 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3991 {
3992 tree memb, marginal_memb = NULL_TREE;
3993
3994 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
3995 {
3996 tree memb_type = TREE_TYPE (memb);
3997
3998 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3999 TYPE_MAIN_VARIANT (rhstype)))
4000 break;
4001
4002 if (TREE_CODE (memb_type) != POINTER_TYPE)
4003 continue;
4004
4005 if (coder == POINTER_TYPE)
4006 {
4007 tree ttl = TREE_TYPE (memb_type);
4008 tree ttr = TREE_TYPE (rhstype);
4009
4010 /* Any non-function converts to a [const][volatile] void *
4011 and vice versa; otherwise, targets must be the same.
4012 Meanwhile, the lhs target must have all the qualifiers of
4013 the rhs. */
4014 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4015 || comp_target_types (memb_type, rhstype))
4016 {
4017 /* If this type won't generate any warnings, use it. */
4018 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4019 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4020 && TREE_CODE (ttl) == FUNCTION_TYPE)
4021 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4022 == TYPE_QUALS (ttr))
4023 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4024 == TYPE_QUALS (ttl))))
4025 break;
4026
4027 /* Keep looking for a better type, but remember this one. */
4028 if (!marginal_memb)
4029 marginal_memb = memb;
4030 }
4031 }
4032
4033 /* Can convert integer zero to any pointer type. */
4034 if (integer_zerop (rhs)
4035 || (TREE_CODE (rhs) == NOP_EXPR
4036 && integer_zerop (TREE_OPERAND (rhs, 0))))
4037 {
4038 rhs = null_pointer_node;
4039 break;
4040 }
4041 }
4042
4043 if (memb || marginal_memb)
4044 {
4045 if (!memb)
4046 {
4047 /* We have only a marginally acceptable member type;
4048 it needs a warning. */
4049 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
4050 tree ttr = TREE_TYPE (rhstype);
4051
4052 /* Const and volatile mean something different for function
4053 types, so the usual warnings are not appropriate. */
4054 if (TREE_CODE (ttr) == FUNCTION_TYPE
4055 && TREE_CODE (ttl) == FUNCTION_TYPE)
4056 {
4057 /* Because const and volatile on functions are
4058 restrictions that say the function will not do
4059 certain things, it is okay to use a const or volatile
4060 function where an ordinary one is wanted, but not
4061 vice-versa. */
4062 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4063 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
4064 "makes qualified function "
4065 "pointer from unqualified"),
4066 G_("assignment makes qualified "
4067 "function pointer from "
4068 "unqualified"),
4069 G_("initialization makes qualified "
4070 "function pointer from "
4071 "unqualified"),
4072 G_("return makes qualified function "
4073 "pointer from unqualified"));
4074 }
4075 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4076 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4077 "qualifiers from pointer target type"),
4078 G_("assignment discards qualifiers "
4079 "from pointer target type"),
4080 G_("initialization discards qualifiers "
4081 "from pointer target type"),
4082 G_("return discards qualifiers from "
4083 "pointer target type"));
4084
4085 memb = marginal_memb;
4086 }
4087
4088 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
4089 pedwarn ("ISO C prohibits argument conversion to union type");
4090
4091 return build_constructor_single (type, memb, rhs);
4092 }
4093 }
4094
4095 /* Conversions among pointers */
4096 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4097 && (coder == codel))
4098 {
4099 tree ttl = TREE_TYPE (type);
4100 tree ttr = TREE_TYPE (rhstype);
4101 tree mvl = ttl;
4102 tree mvr = ttr;
4103 bool is_opaque_pointer;
4104 int target_cmp = 0; /* Cache comp_target_types () result. */
4105
4106 if (TREE_CODE (mvl) != ARRAY_TYPE)
4107 mvl = TYPE_MAIN_VARIANT (mvl);
4108 if (TREE_CODE (mvr) != ARRAY_TYPE)
4109 mvr = TYPE_MAIN_VARIANT (mvr);
4110 /* Opaque pointers are treated like void pointers. */
4111 is_opaque_pointer = (targetm.vector_opaque_p (type)
4112 || targetm.vector_opaque_p (rhstype))
4113 && TREE_CODE (ttl) == VECTOR_TYPE
4114 && TREE_CODE (ttr) == VECTOR_TYPE;
4115
4116 /* C++ does not allow the implicit conversion void* -> T*. However,
4117 for the purpose of reducing the number of false positives, we
4118 tolerate the special case of
4119
4120 int *p = NULL;
4121
4122 where NULL is typically defined in C to be '(void *) 0'. */
4123 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
4124 warning (OPT_Wc___compat, "request for implicit conversion from "
4125 "%qT to %qT not permitted in C++", rhstype, type);
4126
4127 /* Check if the right-hand side has a format attribute but the
4128 left-hand side doesn't. */
4129 if (warn_missing_format_attribute
4130 && check_missing_format_attribute (type, rhstype))
4131 {
4132 switch (errtype)
4133 {
4134 case ic_argpass:
4135 case ic_argpass_nonproto:
4136 warning (OPT_Wmissing_format_attribute,
4137 "argument %d of %qE might be "
4138 "a candidate for a format attribute",
4139 parmnum, rname);
4140 break;
4141 case ic_assign:
4142 warning (OPT_Wmissing_format_attribute,
4143 "assignment left-hand side might be "
4144 "a candidate for a format attribute");
4145 break;
4146 case ic_init:
4147 warning (OPT_Wmissing_format_attribute,
4148 "initialization left-hand side might be "
4149 "a candidate for a format attribute");
4150 break;
4151 case ic_return:
4152 warning (OPT_Wmissing_format_attribute,
4153 "return type might be "
4154 "a candidate for a format attribute");
4155 break;
4156 default:
4157 gcc_unreachable ();
4158 }
4159 }
4160
4161 /* Any non-function converts to a [const][volatile] void *
4162 and vice versa; otherwise, targets must be the same.
4163 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4164 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4165 || (target_cmp = comp_target_types (type, rhstype))
4166 || is_opaque_pointer
4167 || (c_common_unsigned_type (mvl)
4168 == c_common_unsigned_type (mvr)))
4169 {
4170 if (pedantic
4171 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4172 ||
4173 (VOID_TYPE_P (ttr)
4174 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4175 which are not ANSI null ptr constants. */
4176 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4177 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4178 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
4179 "%qE between function pointer "
4180 "and %<void *%>"),
4181 G_("ISO C forbids assignment between "
4182 "function pointer and %<void *%>"),
4183 G_("ISO C forbids initialization between "
4184 "function pointer and %<void *%>"),
4185 G_("ISO C forbids return between function "
4186 "pointer and %<void *%>"));
4187 /* Const and volatile mean something different for function types,
4188 so the usual warnings are not appropriate. */
4189 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4190 && TREE_CODE (ttl) != FUNCTION_TYPE)
4191 {
4192 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4193 {
4194 /* Types differing only by the presence of the 'volatile'
4195 qualifier are acceptable if the 'volatile' has been added
4196 in by the Objective-C EH machinery. */
4197 if (!objc_type_quals_match (ttl, ttr))
4198 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4199 "qualifiers from pointer target type"),
4200 G_("assignment discards qualifiers "
4201 "from pointer target type"),
4202 G_("initialization discards qualifiers "
4203 "from pointer target type"),
4204 G_("return discards qualifiers from "
4205 "pointer target type"));
4206 }
4207 /* If this is not a case of ignoring a mismatch in signedness,
4208 no warning. */
4209 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4210 || target_cmp)
4211 ;
4212 /* If there is a mismatch, do warn. */
4213 else if (warn_pointer_sign)
4214 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
4215 "%d of %qE differ in signedness"),
4216 G_("pointer targets in assignment "
4217 "differ in signedness"),
4218 G_("pointer targets in initialization "
4219 "differ in signedness"),
4220 G_("pointer targets in return differ "
4221 "in signedness"));
4222 }
4223 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4224 && TREE_CODE (ttr) == FUNCTION_TYPE)
4225 {
4226 /* Because const and volatile on functions are restrictions
4227 that say the function will not do certain things,
4228 it is okay to use a const or volatile function
4229 where an ordinary one is wanted, but not vice-versa. */
4230 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4231 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4232 "qualified function pointer "
4233 "from unqualified"),
4234 G_("assignment makes qualified function "
4235 "pointer from unqualified"),
4236 G_("initialization makes qualified "
4237 "function pointer from unqualified"),
4238 G_("return makes qualified function "
4239 "pointer from unqualified"));
4240 }
4241 }
4242 else
4243 /* Avoid warning about the volatile ObjC EH puts on decls. */
4244 if (!objc_ok)
4245 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
4246 "incompatible pointer type"),
4247 G_("assignment from incompatible pointer type"),
4248 G_("initialization from incompatible "
4249 "pointer type"),
4250 G_("return from incompatible pointer type"));
4251
4252 return convert (type, rhs);
4253 }
4254 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4255 {
4256 /* ??? This should not be an error when inlining calls to
4257 unprototyped functions. */
4258 error ("invalid use of non-lvalue array");
4259 return error_mark_node;
4260 }
4261 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4262 {
4263 /* An explicit constant 0 can convert to a pointer,
4264 or one that results from arithmetic, even including
4265 a cast to integer type. */
4266 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4267 &&
4268 !(TREE_CODE (rhs) == NOP_EXPR
4269 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4270 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4271 && integer_zerop (TREE_OPERAND (rhs, 0))))
4272 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4273 "pointer from integer without a cast"),
4274 G_("assignment makes pointer from integer "
4275 "without a cast"),
4276 G_("initialization makes pointer from "
4277 "integer without a cast"),
4278 G_("return makes pointer from integer "
4279 "without a cast"));
4280
4281 return convert (type, rhs);
4282 }
4283 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4284 {
4285 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
4286 "from pointer without a cast"),
4287 G_("assignment makes integer from pointer "
4288 "without a cast"),
4289 G_("initialization makes integer from pointer "
4290 "without a cast"),
4291 G_("return makes integer from pointer "
4292 "without a cast"));
4293 return convert (type, rhs);
4294 }
4295 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4296 return convert (type, rhs);
4297
4298 switch (errtype)
4299 {
4300 case ic_argpass:
4301 case ic_argpass_nonproto:
4302 /* ??? This should not be an error when inlining calls to
4303 unprototyped functions. */
4304 error ("incompatible type for argument %d of %qE", parmnum, rname);
4305 break;
4306 case ic_assign:
4307 error ("incompatible types in assignment");
4308 break;
4309 case ic_init:
4310 error ("incompatible types in initialization");
4311 break;
4312 case ic_return:
4313 error ("incompatible types in return");
4314 break;
4315 default:
4316 gcc_unreachable ();
4317 }
4318
4319 return error_mark_node;
4320 }
4321
4322 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
4323 is used for error and waring reporting and indicates which argument
4324 is being processed. */
4325
4326 tree
c_convert_parm_for_inlining(tree parm,tree value,tree fn,int argnum)4327 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
4328 {
4329 tree ret, type;
4330
4331 /* If FN was prototyped, the value has been converted already
4332 in convert_arguments. */
4333 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4334 return value;
4335
4336 type = TREE_TYPE (parm);
4337 ret = convert_for_assignment (type, value,
4338 ic_argpass_nonproto, fn,
4339 fn, argnum);
4340 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
4341 && INTEGRAL_TYPE_P (type)
4342 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4343 ret = default_conversion (ret);
4344 return ret;
4345 }
4346
4347 /* If VALUE is a compound expr all of whose expressions are constant, then
4348 return its value. Otherwise, return error_mark_node.
4349
4350 This is for handling COMPOUND_EXPRs as initializer elements
4351 which is allowed with a warning when -pedantic is specified. */
4352
4353 static tree
valid_compound_expr_initializer(tree value,tree endtype)4354 valid_compound_expr_initializer (tree value, tree endtype)
4355 {
4356 if (TREE_CODE (value) == COMPOUND_EXPR)
4357 {
4358 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4359 == error_mark_node)
4360 return error_mark_node;
4361 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4362 endtype);
4363 }
4364 else if (!initializer_constant_valid_p (value, endtype))
4365 return error_mark_node;
4366 else
4367 return value;
4368 }
4369
4370 /* Perform appropriate conversions on the initial value of a variable,
4371 store it in the declaration DECL,
4372 and print any error messages that are appropriate.
4373 If the init is invalid, store an ERROR_MARK. */
4374
4375 void
store_init_value(tree decl,tree init)4376 store_init_value (tree decl, tree init)
4377 {
4378 tree value, type;
4379
4380 /* If variable's type was invalidly declared, just ignore it. */
4381
4382 type = TREE_TYPE (decl);
4383 if (TREE_CODE (type) == ERROR_MARK)
4384 return;
4385
4386 /* Digest the specified initializer into an expression. */
4387
4388 value = digest_init (type, init, true, TREE_STATIC (decl));
4389
4390 /* Store the expression if valid; else report error. */
4391
4392 if (!in_system_header
4393 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4394 warning (OPT_Wtraditional, "traditional C rejects automatic "
4395 "aggregate initialization");
4396
4397 DECL_INITIAL (decl) = value;
4398
4399 /* ANSI wants warnings about out-of-range constant initializers. */
4400 STRIP_TYPE_NOPS (value);
4401 constant_expression_warning (value);
4402
4403 /* Check if we need to set array size from compound literal size. */
4404 if (TREE_CODE (type) == ARRAY_TYPE
4405 && TYPE_DOMAIN (type) == 0
4406 && value != error_mark_node)
4407 {
4408 tree inside_init = init;
4409
4410 STRIP_TYPE_NOPS (inside_init);
4411 inside_init = fold (inside_init);
4412
4413 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4414 {
4415 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4416
4417 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4418 {
4419 /* For int foo[] = (int [3]){1}; we need to set array size
4420 now since later on array initializer will be just the
4421 brace enclosed list of the compound literal. */
4422 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4423 layout_type (type);
4424 layout_decl (decl, 0);
4425 }
4426 }
4427 }
4428 }
4429
4430 /* Methods for storing and printing names for error messages. */
4431
4432 /* Implement a spelling stack that allows components of a name to be pushed
4433 and popped. Each element on the stack is this structure. */
4434
4435 struct spelling
4436 {
4437 int kind;
4438 union
4439 {
4440 unsigned HOST_WIDE_INT i;
4441 const char *s;
4442 } u;
4443 };
4444
4445 #define SPELLING_STRING 1
4446 #define SPELLING_MEMBER 2
4447 #define SPELLING_BOUNDS 3
4448
4449 static struct spelling *spelling; /* Next stack element (unused). */
4450 static struct spelling *spelling_base; /* Spelling stack base. */
4451 static int spelling_size; /* Size of the spelling stack. */
4452
4453 /* Macros to save and restore the spelling stack around push_... functions.
4454 Alternative to SAVE_SPELLING_STACK. */
4455
4456 #define SPELLING_DEPTH() (spelling - spelling_base)
4457 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4458
4459 /* Push an element on the spelling stack with type KIND and assign VALUE
4460 to MEMBER. */
4461
4462 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4463 { \
4464 int depth = SPELLING_DEPTH (); \
4465 \
4466 if (depth >= spelling_size) \
4467 { \
4468 spelling_size += 10; \
4469 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4470 spelling_size); \
4471 RESTORE_SPELLING_DEPTH (depth); \
4472 } \
4473 \
4474 spelling->kind = (KIND); \
4475 spelling->MEMBER = (VALUE); \
4476 spelling++; \
4477 }
4478
4479 /* Push STRING on the stack. Printed literally. */
4480
4481 static void
push_string(const char * string)4482 push_string (const char *string)
4483 {
4484 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4485 }
4486
4487 /* Push a member name on the stack. Printed as '.' STRING. */
4488
4489 static void
push_member_name(tree decl)4490 push_member_name (tree decl)
4491 {
4492 const char *const string
4493 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4494 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4495 }
4496
4497 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4498
4499 static void
push_array_bounds(unsigned HOST_WIDE_INT bounds)4500 push_array_bounds (unsigned HOST_WIDE_INT bounds)
4501 {
4502 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4503 }
4504
4505 /* Compute the maximum size in bytes of the printed spelling. */
4506
4507 static int
spelling_length(void)4508 spelling_length (void)
4509 {
4510 int size = 0;
4511 struct spelling *p;
4512
4513 for (p = spelling_base; p < spelling; p++)
4514 {
4515 if (p->kind == SPELLING_BOUNDS)
4516 size += 25;
4517 else
4518 size += strlen (p->u.s) + 1;
4519 }
4520
4521 return size;
4522 }
4523
4524 /* Print the spelling to BUFFER and return it. */
4525
4526 static char *
print_spelling(char * buffer)4527 print_spelling (char *buffer)
4528 {
4529 char *d = buffer;
4530 struct spelling *p;
4531
4532 for (p = spelling_base; p < spelling; p++)
4533 if (p->kind == SPELLING_BOUNDS)
4534 {
4535 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
4536 d += strlen (d);
4537 }
4538 else
4539 {
4540 const char *s;
4541 if (p->kind == SPELLING_MEMBER)
4542 *d++ = '.';
4543 for (s = p->u.s; (*d = *s++); d++)
4544 ;
4545 }
4546 *d++ = '\0';
4547 return buffer;
4548 }
4549
4550 /* Issue an error message for a bad initializer component.
4551 MSGID identifies the message.
4552 The component name is taken from the spelling stack. */
4553
4554 void
error_init(const char * msgid)4555 error_init (const char *msgid)
4556 {
4557 char *ofwhat;
4558
4559 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4560 if (*ofwhat)
4561 error ("%s (near initialization for %qs)", _(msgid), ofwhat);
4562 else
4563 error ("%s", _(msgid));
4564 }
4565
4566 /* Issue a pedantic warning for a bad initializer component.
4567 MSGID identifies the message.
4568 The component name is taken from the spelling stack. */
4569
4570 void
pedwarn_init(const char * msgid)4571 pedwarn_init (const char *msgid)
4572 {
4573 char *ofwhat;
4574
4575 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4576 if (*ofwhat)
4577 pedwarn ("%s (near initialization for %qs)", _(msgid), ofwhat);
4578 else
4579 pedwarn ("%s", _(msgid));
4580 }
4581
4582 /* Issue a warning for a bad initializer component.
4583 MSGID identifies the message.
4584 The component name is taken from the spelling stack. */
4585
4586 static void
warning_init(const char * msgid)4587 warning_init (const char *msgid)
4588 {
4589 char *ofwhat;
4590
4591 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4592 if (*ofwhat)
4593 warning (0, "%s (near initialization for %qs)", _(msgid), ofwhat);
4594 else
4595 warning (0, "%s", _(msgid));
4596 }
4597
4598 /* If TYPE is an array type and EXPR is a parenthesized string
4599 constant, warn if pedantic that EXPR is being used to initialize an
4600 object of type TYPE. */
4601
4602 void
maybe_warn_string_init(tree type,struct c_expr expr)4603 maybe_warn_string_init (tree type, struct c_expr expr)
4604 {
4605 if (pedantic
4606 && TREE_CODE (type) == ARRAY_TYPE
4607 && TREE_CODE (expr.value) == STRING_CST
4608 && expr.original_code != STRING_CST)
4609 pedwarn_init ("array initialized from parenthesized string constant");
4610 }
4611
4612 /* Digest the parser output INIT as an initializer for type TYPE.
4613 Return a C expression of type TYPE to represent the initial value.
4614
4615 If INIT is a string constant, STRICT_STRING is true if it is
4616 unparenthesized or we should not warn here for it being parenthesized.
4617 For other types of INIT, STRICT_STRING is not used.
4618
4619 REQUIRE_CONSTANT requests an error if non-constant initializers or
4620 elements are seen. */
4621
4622 static tree
digest_init(tree type,tree init,bool strict_string,int require_constant)4623 digest_init (tree type, tree init, bool strict_string, int require_constant)
4624 {
4625 enum tree_code code = TREE_CODE (type);
4626 tree inside_init = init;
4627
4628 if (type == error_mark_node
4629 || !init
4630 || init == error_mark_node
4631 || TREE_TYPE (init) == error_mark_node)
4632 return error_mark_node;
4633
4634 STRIP_TYPE_NOPS (inside_init);
4635
4636 inside_init = fold (inside_init);
4637
4638 /* Initialization of an array of chars from a string constant
4639 optionally enclosed in braces. */
4640
4641 if (code == ARRAY_TYPE && inside_init
4642 && TREE_CODE (inside_init) == STRING_CST)
4643 {
4644 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4645 /* Note that an array could be both an array of character type
4646 and an array of wchar_t if wchar_t is signed char or unsigned
4647 char. */
4648 bool char_array = (typ1 == char_type_node
4649 || typ1 == signed_char_type_node
4650 || typ1 == unsigned_char_type_node);
4651 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4652 if (char_array || wchar_array)
4653 {
4654 struct c_expr expr;
4655 bool char_string;
4656 expr.value = inside_init;
4657 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4658 maybe_warn_string_init (type, expr);
4659
4660 char_string
4661 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4662 == char_type_node);
4663
4664 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4665 TYPE_MAIN_VARIANT (type)))
4666 return inside_init;
4667
4668 if (!wchar_array && !char_string)
4669 {
4670 error_init ("char-array initialized from wide string");
4671 return error_mark_node;
4672 }
4673 if (char_string && !char_array)
4674 {
4675 error_init ("wchar_t-array initialized from non-wide string");
4676 return error_mark_node;
4677 }
4678
4679 TREE_TYPE (inside_init) = type;
4680 if (TYPE_DOMAIN (type) != 0
4681 && TYPE_SIZE (type) != 0
4682 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4683 /* Subtract 1 (or sizeof (wchar_t))
4684 because it's ok to ignore the terminating null char
4685 that is counted in the length of the constant. */
4686 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4687 TREE_STRING_LENGTH (inside_init)
4688 - ((TYPE_PRECISION (typ1)
4689 != TYPE_PRECISION (char_type_node))
4690 ? (TYPE_PRECISION (wchar_type_node)
4691 / BITS_PER_UNIT)
4692 : 1)))
4693 pedwarn_init ("initializer-string for array of chars is too long");
4694
4695 return inside_init;
4696 }
4697 else if (INTEGRAL_TYPE_P (typ1))
4698 {
4699 error_init ("array of inappropriate type initialized "
4700 "from string constant");
4701 return error_mark_node;
4702 }
4703 }
4704
4705 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4706 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4707 below and handle as a constructor. */
4708 if (code == VECTOR_TYPE
4709 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4710 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4711 && TREE_CONSTANT (inside_init))
4712 {
4713 if (TREE_CODE (inside_init) == VECTOR_CST
4714 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4715 TYPE_MAIN_VARIANT (type)))
4716 return inside_init;
4717
4718 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4719 {
4720 unsigned HOST_WIDE_INT ix;
4721 tree value;
4722 bool constant_p = true;
4723
4724 /* Iterate through elements and check if all constructor
4725 elements are *_CSTs. */
4726 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4727 if (!CONSTANT_CLASS_P (value))
4728 {
4729 constant_p = false;
4730 break;
4731 }
4732
4733 if (constant_p)
4734 return build_vector_from_ctor (type,
4735 CONSTRUCTOR_ELTS (inside_init));
4736 }
4737 }
4738
4739 /* Any type can be initialized
4740 from an expression of the same type, optionally with braces. */
4741
4742 if (inside_init && TREE_TYPE (inside_init) != 0
4743 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4744 TYPE_MAIN_VARIANT (type))
4745 || (code == ARRAY_TYPE
4746 && comptypes (TREE_TYPE (inside_init), type))
4747 || (code == VECTOR_TYPE
4748 && comptypes (TREE_TYPE (inside_init), type))
4749 || (code == POINTER_TYPE
4750 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4751 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4752 TREE_TYPE (type)))))
4753 {
4754 if (code == POINTER_TYPE)
4755 {
4756 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4757 {
4758 if (TREE_CODE (inside_init) == STRING_CST
4759 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4760 inside_init = array_to_pointer_conversion (inside_init);
4761 else
4762 {
4763 error_init ("invalid use of non-lvalue array");
4764 return error_mark_node;
4765 }
4766 }
4767 }
4768
4769 if (code == VECTOR_TYPE)
4770 /* Although the types are compatible, we may require a
4771 conversion. */
4772 inside_init = convert (type, inside_init);
4773
4774 if (require_constant && !flag_isoc99
4775 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4776 {
4777 /* As an extension, allow initializing objects with static storage
4778 duration with compound literals (which are then treated just as
4779 the brace enclosed list they contain). */
4780 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4781 inside_init = DECL_INITIAL (decl);
4782 }
4783
4784 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4785 && TREE_CODE (inside_init) != CONSTRUCTOR)
4786 {
4787 error_init ("array initialized from non-constant array expression");
4788 return error_mark_node;
4789 }
4790
4791 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4792 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4793
4794 /* Compound expressions can only occur here if -pedantic or
4795 -pedantic-errors is specified. In the later case, we always want
4796 an error. In the former case, we simply want a warning. */
4797 if (require_constant && pedantic
4798 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4799 {
4800 inside_init
4801 = valid_compound_expr_initializer (inside_init,
4802 TREE_TYPE (inside_init));
4803 if (inside_init == error_mark_node)
4804 error_init ("initializer element is not constant");
4805 else
4806 pedwarn_init ("initializer element is not constant");
4807 if (flag_pedantic_errors)
4808 inside_init = error_mark_node;
4809 }
4810 else if (require_constant
4811 && !initializer_constant_valid_p (inside_init,
4812 TREE_TYPE (inside_init)))
4813 {
4814 error_init ("initializer element is not constant");
4815 inside_init = error_mark_node;
4816 }
4817
4818 /* Added to enable additional -Wmissing-format-attribute warnings. */
4819 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4820 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4821 NULL_TREE, 0);
4822 return inside_init;
4823 }
4824
4825 /* Handle scalar types, including conversions. */
4826
4827 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4828 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4829 || code == VECTOR_TYPE)
4830 {
4831 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4832 && (TREE_CODE (init) == STRING_CST
4833 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4834 init = array_to_pointer_conversion (init);
4835 inside_init
4836 = convert_for_assignment (type, init, ic_init,
4837 NULL_TREE, NULL_TREE, 0);
4838
4839 /* Check to see if we have already given an error message. */
4840 if (inside_init == error_mark_node)
4841 ;
4842 else if (require_constant && !TREE_CONSTANT (inside_init))
4843 {
4844 error_init ("initializer element is not constant");
4845 inside_init = error_mark_node;
4846 }
4847 else if (require_constant
4848 && !initializer_constant_valid_p (inside_init,
4849 TREE_TYPE (inside_init)))
4850 {
4851 error_init ("initializer element is not computable at load time");
4852 inside_init = error_mark_node;
4853 }
4854
4855 return inside_init;
4856 }
4857
4858 /* Come here only for records and arrays. */
4859
4860 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4861 {
4862 error_init ("variable-sized object may not be initialized");
4863 return error_mark_node;
4864 }
4865
4866 error_init ("invalid initializer");
4867 return error_mark_node;
4868 }
4869
4870 /* Handle initializers that use braces. */
4871
4872 /* Type of object we are accumulating a constructor for.
4873 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4874 static tree constructor_type;
4875
4876 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4877 left to fill. */
4878 static tree constructor_fields;
4879
4880 /* For an ARRAY_TYPE, this is the specified index
4881 at which to store the next element we get. */
4882 static tree constructor_index;
4883
4884 /* For an ARRAY_TYPE, this is the maximum index. */
4885 static tree constructor_max_index;
4886
4887 /* For a RECORD_TYPE, this is the first field not yet written out. */
4888 static tree constructor_unfilled_fields;
4889
4890 /* For an ARRAY_TYPE, this is the index of the first element
4891 not yet written out. */
4892 static tree constructor_unfilled_index;
4893
4894 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4895 This is so we can generate gaps between fields, when appropriate. */
4896 static tree constructor_bit_index;
4897
4898 /* If we are saving up the elements rather than allocating them,
4899 this is the list of elements so far (in reverse order,
4900 most recent first). */
4901 static VEC(constructor_elt,gc) *constructor_elements;
4902
4903 /* 1 if constructor should be incrementally stored into a constructor chain,
4904 0 if all the elements should be kept in AVL tree. */
4905 static int constructor_incremental;
4906
4907 /* 1 if so far this constructor's elements are all compile-time constants. */
4908 static int constructor_constant;
4909
4910 /* 1 if so far this constructor's elements are all valid address constants. */
4911 static int constructor_simple;
4912
4913 /* 1 if this constructor is erroneous so far. */
4914 static int constructor_erroneous;
4915
4916 /* Structure for managing pending initializer elements, organized as an
4917 AVL tree. */
4918
4919 struct init_node
4920 {
4921 struct init_node *left, *right;
4922 struct init_node *parent;
4923 int balance;
4924 tree purpose;
4925 tree value;
4926 };
4927
4928 /* Tree of pending elements at this constructor level.
4929 These are elements encountered out of order
4930 which belong at places we haven't reached yet in actually
4931 writing the output.
4932 Will never hold tree nodes across GC runs. */
4933 static struct init_node *constructor_pending_elts;
4934
4935 /* The SPELLING_DEPTH of this constructor. */
4936 static int constructor_depth;
4937
4938 /* DECL node for which an initializer is being read.
4939 0 means we are reading a constructor expression
4940 such as (struct foo) {...}. */
4941 static tree constructor_decl;
4942
4943 /* Nonzero if this is an initializer for a top-level decl. */
4944 static int constructor_top_level;
4945
4946 /* Nonzero if there were any member designators in this initializer. */
4947 static int constructor_designated;
4948
4949 /* Nesting depth of designator list. */
4950 static int designator_depth;
4951
4952 /* Nonzero if there were diagnosed errors in this designator list. */
4953 static int designator_erroneous;
4954
4955
4956 /* This stack has a level for each implicit or explicit level of
4957 structuring in the initializer, including the outermost one. It
4958 saves the values of most of the variables above. */
4959
4960 struct constructor_range_stack;
4961
4962 struct constructor_stack
4963 {
4964 struct constructor_stack *next;
4965 tree type;
4966 tree fields;
4967 tree index;
4968 tree max_index;
4969 tree unfilled_index;
4970 tree unfilled_fields;
4971 tree bit_index;
4972 VEC(constructor_elt,gc) *elements;
4973 struct init_node *pending_elts;
4974 int offset;
4975 int depth;
4976 /* If value nonzero, this value should replace the entire
4977 constructor at this level. */
4978 struct c_expr replacement_value;
4979 struct constructor_range_stack *range_stack;
4980 char constant;
4981 char simple;
4982 char implicit;
4983 char erroneous;
4984 char outer;
4985 char incremental;
4986 char designated;
4987 };
4988
4989 static struct constructor_stack *constructor_stack;
4990
4991 /* This stack represents designators from some range designator up to
4992 the last designator in the list. */
4993
4994 struct constructor_range_stack
4995 {
4996 struct constructor_range_stack *next, *prev;
4997 struct constructor_stack *stack;
4998 tree range_start;
4999 tree index;
5000 tree range_end;
5001 tree fields;
5002 };
5003
5004 static struct constructor_range_stack *constructor_range_stack;
5005
5006 /* This stack records separate initializers that are nested.
5007 Nested initializers can't happen in ANSI C, but GNU C allows them
5008 in cases like { ... (struct foo) { ... } ... }. */
5009
5010 struct initializer_stack
5011 {
5012 struct initializer_stack *next;
5013 tree decl;
5014 struct constructor_stack *constructor_stack;
5015 struct constructor_range_stack *constructor_range_stack;
5016 VEC(constructor_elt,gc) *elements;
5017 struct spelling *spelling;
5018 struct spelling *spelling_base;
5019 int spelling_size;
5020 char top_level;
5021 char require_constant_value;
5022 char require_constant_elements;
5023 };
5024
5025 static struct initializer_stack *initializer_stack;
5026
5027 /* Prepare to parse and output the initializer for variable DECL. */
5028
5029 void
start_init(tree decl,tree asmspec_tree ATTRIBUTE_UNUSED,int top_level)5030 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
5031 {
5032 const char *locus;
5033 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
5034
5035 p->decl = constructor_decl;
5036 p->require_constant_value = require_constant_value;
5037 p->require_constant_elements = require_constant_elements;
5038 p->constructor_stack = constructor_stack;
5039 p->constructor_range_stack = constructor_range_stack;
5040 p->elements = constructor_elements;
5041 p->spelling = spelling;
5042 p->spelling_base = spelling_base;
5043 p->spelling_size = spelling_size;
5044 p->top_level = constructor_top_level;
5045 p->next = initializer_stack;
5046 initializer_stack = p;
5047
5048 constructor_decl = decl;
5049 constructor_designated = 0;
5050 constructor_top_level = top_level;
5051
5052 if (decl != 0 && decl != error_mark_node)
5053 {
5054 require_constant_value = TREE_STATIC (decl);
5055 require_constant_elements
5056 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5057 /* For a scalar, you can always use any value to initialize,
5058 even within braces. */
5059 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5060 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5061 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5062 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5063 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5064 }
5065 else
5066 {
5067 require_constant_value = 0;
5068 require_constant_elements = 0;
5069 locus = "(anonymous)";
5070 }
5071
5072 constructor_stack = 0;
5073 constructor_range_stack = 0;
5074
5075 missing_braces_mentioned = 0;
5076
5077 spelling_base = 0;
5078 spelling_size = 0;
5079 RESTORE_SPELLING_DEPTH (0);
5080
5081 if (locus)
5082 push_string (locus);
5083 }
5084
5085 void
finish_init(void)5086 finish_init (void)
5087 {
5088 struct initializer_stack *p = initializer_stack;
5089
5090 /* Free the whole constructor stack of this initializer. */
5091 while (constructor_stack)
5092 {
5093 struct constructor_stack *q = constructor_stack;
5094 constructor_stack = q->next;
5095 free (q);
5096 }
5097
5098 gcc_assert (!constructor_range_stack);
5099
5100 /* Pop back to the data of the outer initializer (if any). */
5101 free (spelling_base);
5102
5103 constructor_decl = p->decl;
5104 require_constant_value = p->require_constant_value;
5105 require_constant_elements = p->require_constant_elements;
5106 constructor_stack = p->constructor_stack;
5107 constructor_range_stack = p->constructor_range_stack;
5108 constructor_elements = p->elements;
5109 spelling = p->spelling;
5110 spelling_base = p->spelling_base;
5111 spelling_size = p->spelling_size;
5112 constructor_top_level = p->top_level;
5113 initializer_stack = p->next;
5114 free (p);
5115 }
5116
5117 /* Call here when we see the initializer is surrounded by braces.
5118 This is instead of a call to push_init_level;
5119 it is matched by a call to pop_init_level.
5120
5121 TYPE is the type to initialize, for a constructor expression.
5122 For an initializer for a decl, TYPE is zero. */
5123
5124 void
really_start_incremental_init(tree type)5125 really_start_incremental_init (tree type)
5126 {
5127 struct constructor_stack *p = XNEW (struct constructor_stack);
5128
5129 if (type == 0)
5130 type = TREE_TYPE (constructor_decl);
5131
5132 if (targetm.vector_opaque_p (type))
5133 error ("opaque vector types cannot be initialized");
5134
5135 p->type = constructor_type;
5136 p->fields = constructor_fields;
5137 p->index = constructor_index;
5138 p->max_index = constructor_max_index;
5139 p->unfilled_index = constructor_unfilled_index;
5140 p->unfilled_fields = constructor_unfilled_fields;
5141 p->bit_index = constructor_bit_index;
5142 p->elements = constructor_elements;
5143 p->constant = constructor_constant;
5144 p->simple = constructor_simple;
5145 p->erroneous = constructor_erroneous;
5146 p->pending_elts = constructor_pending_elts;
5147 p->depth = constructor_depth;
5148 p->replacement_value.value = 0;
5149 p->replacement_value.original_code = ERROR_MARK;
5150 p->implicit = 0;
5151 p->range_stack = 0;
5152 p->outer = 0;
5153 p->incremental = constructor_incremental;
5154 p->designated = constructor_designated;
5155 p->next = 0;
5156 constructor_stack = p;
5157
5158 constructor_constant = 1;
5159 constructor_simple = 1;
5160 constructor_depth = SPELLING_DEPTH ();
5161 constructor_elements = 0;
5162 constructor_pending_elts = 0;
5163 constructor_type = type;
5164 constructor_incremental = 1;
5165 constructor_designated = 0;
5166 designator_depth = 0;
5167 designator_erroneous = 0;
5168
5169 if (TREE_CODE (constructor_type) == RECORD_TYPE
5170 || TREE_CODE (constructor_type) == UNION_TYPE)
5171 {
5172 constructor_fields = TYPE_FIELDS (constructor_type);
5173 /* Skip any nameless bit fields at the beginning. */
5174 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5175 && DECL_NAME (constructor_fields) == 0)
5176 constructor_fields = TREE_CHAIN (constructor_fields);
5177
5178 constructor_unfilled_fields = constructor_fields;
5179 constructor_bit_index = bitsize_zero_node;
5180 }
5181 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5182 {
5183 if (TYPE_DOMAIN (constructor_type))
5184 {
5185 constructor_max_index
5186 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5187
5188 /* Detect non-empty initializations of zero-length arrays. */
5189 if (constructor_max_index == NULL_TREE
5190 && TYPE_SIZE (constructor_type))
5191 constructor_max_index = build_int_cst (NULL_TREE, -1);
5192
5193 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5194 to initialize VLAs will cause a proper error; avoid tree
5195 checking errors as well by setting a safe value. */
5196 if (constructor_max_index
5197 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5198 constructor_max_index = build_int_cst (NULL_TREE, -1);
5199
5200 constructor_index
5201 = convert (bitsizetype,
5202 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5203 }
5204 else
5205 {
5206 constructor_index = bitsize_zero_node;
5207 constructor_max_index = NULL_TREE;
5208 }
5209
5210 constructor_unfilled_index = constructor_index;
5211 }
5212 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5213 {
5214 /* Vectors are like simple fixed-size arrays. */
5215 constructor_max_index =
5216 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5217 constructor_index = bitsize_zero_node;
5218 constructor_unfilled_index = constructor_index;
5219 }
5220 else
5221 {
5222 /* Handle the case of int x = {5}; */
5223 constructor_fields = constructor_type;
5224 constructor_unfilled_fields = constructor_type;
5225 }
5226 }
5227
5228 /* Push down into a subobject, for initialization.
5229 If this is for an explicit set of braces, IMPLICIT is 0.
5230 If it is because the next element belongs at a lower level,
5231 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5232
5233 void
push_init_level(int implicit)5234 push_init_level (int implicit)
5235 {
5236 struct constructor_stack *p;
5237 tree value = NULL_TREE;
5238
5239 /* If we've exhausted any levels that didn't have braces,
5240 pop them now. If implicit == 1, this will have been done in
5241 process_init_element; do not repeat it here because in the case
5242 of excess initializers for an empty aggregate this leads to an
5243 infinite cycle of popping a level and immediately recreating
5244 it. */
5245 if (implicit != 1)
5246 {
5247 while (constructor_stack->implicit)
5248 {
5249 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5250 || TREE_CODE (constructor_type) == UNION_TYPE)
5251 && constructor_fields == 0)
5252 process_init_element (pop_init_level (1));
5253 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5254 && constructor_max_index
5255 && tree_int_cst_lt (constructor_max_index,
5256 constructor_index))
5257 process_init_element (pop_init_level (1));
5258 else
5259 break;
5260 }
5261 }
5262
5263 /* Unless this is an explicit brace, we need to preserve previous
5264 content if any. */
5265 if (implicit)
5266 {
5267 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5268 || TREE_CODE (constructor_type) == UNION_TYPE)
5269 && constructor_fields)
5270 value = find_init_member (constructor_fields);
5271 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5272 value = find_init_member (constructor_index);
5273 }
5274
5275 p = XNEW (struct constructor_stack);
5276 p->type = constructor_type;
5277 p->fields = constructor_fields;
5278 p->index = constructor_index;
5279 p->max_index = constructor_max_index;
5280 p->unfilled_index = constructor_unfilled_index;
5281 p->unfilled_fields = constructor_unfilled_fields;
5282 p->bit_index = constructor_bit_index;
5283 p->elements = constructor_elements;
5284 p->constant = constructor_constant;
5285 p->simple = constructor_simple;
5286 p->erroneous = constructor_erroneous;
5287 p->pending_elts = constructor_pending_elts;
5288 p->depth = constructor_depth;
5289 p->replacement_value.value = 0;
5290 p->replacement_value.original_code = ERROR_MARK;
5291 p->implicit = implicit;
5292 p->outer = 0;
5293 p->incremental = constructor_incremental;
5294 p->designated = constructor_designated;
5295 p->next = constructor_stack;
5296 p->range_stack = 0;
5297 constructor_stack = p;
5298
5299 constructor_constant = 1;
5300 constructor_simple = 1;
5301 constructor_depth = SPELLING_DEPTH ();
5302 constructor_elements = 0;
5303 constructor_incremental = 1;
5304 constructor_designated = 0;
5305 constructor_pending_elts = 0;
5306 if (!implicit)
5307 {
5308 p->range_stack = constructor_range_stack;
5309 constructor_range_stack = 0;
5310 designator_depth = 0;
5311 designator_erroneous = 0;
5312 }
5313
5314 /* Don't die if an entire brace-pair level is superfluous
5315 in the containing level. */
5316 if (constructor_type == 0)
5317 ;
5318 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5319 || TREE_CODE (constructor_type) == UNION_TYPE)
5320 {
5321 /* Don't die if there are extra init elts at the end. */
5322 if (constructor_fields == 0)
5323 constructor_type = 0;
5324 else
5325 {
5326 constructor_type = TREE_TYPE (constructor_fields);
5327 push_member_name (constructor_fields);
5328 constructor_depth++;
5329 }
5330 }
5331 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5332 {
5333 constructor_type = TREE_TYPE (constructor_type);
5334 push_array_bounds (tree_low_cst (constructor_index, 1));
5335 constructor_depth++;
5336 }
5337
5338 if (constructor_type == 0)
5339 {
5340 error_init ("extra brace group at end of initializer");
5341 constructor_fields = 0;
5342 constructor_unfilled_fields = 0;
5343 return;
5344 }
5345
5346 if (value && TREE_CODE (value) == CONSTRUCTOR)
5347 {
5348 constructor_constant = TREE_CONSTANT (value);
5349 constructor_simple = TREE_STATIC (value);
5350 constructor_elements = CONSTRUCTOR_ELTS (value);
5351 if (!VEC_empty (constructor_elt, constructor_elements)
5352 && (TREE_CODE (constructor_type) == RECORD_TYPE
5353 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5354 set_nonincremental_init ();
5355 }
5356
5357 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5358 {
5359 missing_braces_mentioned = 1;
5360 warning_init ("missing braces around initializer");
5361 }
5362
5363 if (TREE_CODE (constructor_type) == RECORD_TYPE
5364 || TREE_CODE (constructor_type) == UNION_TYPE)
5365 {
5366 constructor_fields = TYPE_FIELDS (constructor_type);
5367 /* Skip any nameless bit fields at the beginning. */
5368 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5369 && DECL_NAME (constructor_fields) == 0)
5370 constructor_fields = TREE_CHAIN (constructor_fields);
5371
5372 constructor_unfilled_fields = constructor_fields;
5373 constructor_bit_index = bitsize_zero_node;
5374 }
5375 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5376 {
5377 /* Vectors are like simple fixed-size arrays. */
5378 constructor_max_index =
5379 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5380 constructor_index = convert (bitsizetype, integer_zero_node);
5381 constructor_unfilled_index = constructor_index;
5382 }
5383 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5384 {
5385 if (TYPE_DOMAIN (constructor_type))
5386 {
5387 constructor_max_index
5388 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5389
5390 /* Detect non-empty initializations of zero-length arrays. */
5391 if (constructor_max_index == NULL_TREE
5392 && TYPE_SIZE (constructor_type))
5393 constructor_max_index = build_int_cst (NULL_TREE, -1);
5394
5395 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5396 to initialize VLAs will cause a proper error; avoid tree
5397 checking errors as well by setting a safe value. */
5398 if (constructor_max_index
5399 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5400 constructor_max_index = build_int_cst (NULL_TREE, -1);
5401
5402 constructor_index
5403 = convert (bitsizetype,
5404 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5405 }
5406 else
5407 constructor_index = bitsize_zero_node;
5408
5409 constructor_unfilled_index = constructor_index;
5410 if (value && TREE_CODE (value) == STRING_CST)
5411 {
5412 /* We need to split the char/wchar array into individual
5413 characters, so that we don't have to special case it
5414 everywhere. */
5415 set_nonincremental_init_from_string (value);
5416 }
5417 }
5418 else
5419 {
5420 if (constructor_type != error_mark_node)
5421 warning_init ("braces around scalar initializer");
5422 constructor_fields = constructor_type;
5423 constructor_unfilled_fields = constructor_type;
5424 }
5425 }
5426
5427 /* At the end of an implicit or explicit brace level,
5428 finish up that level of constructor. If a single expression
5429 with redundant braces initialized that level, return the
5430 c_expr structure for that expression. Otherwise, the original_code
5431 element is set to ERROR_MARK.
5432 If we were outputting the elements as they are read, return 0 as the value
5433 from inner levels (process_init_element ignores that),
5434 but return error_mark_node as the value from the outermost level
5435 (that's what we want to put in DECL_INITIAL).
5436 Otherwise, return a CONSTRUCTOR expression as the value. */
5437
5438 struct c_expr
pop_init_level(int implicit)5439 pop_init_level (int implicit)
5440 {
5441 struct constructor_stack *p;
5442 struct c_expr ret;
5443 ret.value = 0;
5444 ret.original_code = ERROR_MARK;
5445
5446 if (implicit == 0)
5447 {
5448 /* When we come to an explicit close brace,
5449 pop any inner levels that didn't have explicit braces. */
5450 while (constructor_stack->implicit)
5451 process_init_element (pop_init_level (1));
5452
5453 gcc_assert (!constructor_range_stack);
5454 }
5455
5456 /* Now output all pending elements. */
5457 constructor_incremental = 1;
5458 output_pending_init_elements (1);
5459
5460 p = constructor_stack;
5461
5462 /* Error for initializing a flexible array member, or a zero-length
5463 array member in an inappropriate context. */
5464 if (constructor_type && constructor_fields
5465 && TREE_CODE (constructor_type) == ARRAY_TYPE
5466 && TYPE_DOMAIN (constructor_type)
5467 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5468 {
5469 /* Silently discard empty initializations. The parser will
5470 already have pedwarned for empty brackets. */
5471 if (integer_zerop (constructor_unfilled_index))
5472 constructor_type = NULL_TREE;
5473 else
5474 {
5475 gcc_assert (!TYPE_SIZE (constructor_type));
5476
5477 if (constructor_depth > 2)
5478 error_init ("initialization of flexible array member in a nested context");
5479 else if (pedantic)
5480 pedwarn_init ("initialization of a flexible array member");
5481
5482 /* We have already issued an error message for the existence
5483 of a flexible array member not at the end of the structure.
5484 Discard the initializer so that we do not die later. */
5485 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5486 constructor_type = NULL_TREE;
5487 }
5488 }
5489
5490 /* Warn when some struct elements are implicitly initialized to zero. */
5491 if (warn_missing_field_initializers
5492 && constructor_type
5493 && TREE_CODE (constructor_type) == RECORD_TYPE
5494 && constructor_unfilled_fields)
5495 {
5496 /* Do not warn for flexible array members or zero-length arrays. */
5497 while (constructor_unfilled_fields
5498 && (!DECL_SIZE (constructor_unfilled_fields)
5499 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5500 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5501
5502 /* Do not warn if this level of the initializer uses member
5503 designators; it is likely to be deliberate. */
5504 if (constructor_unfilled_fields && !constructor_designated)
5505 {
5506 push_member_name (constructor_unfilled_fields);
5507 warning_init ("missing initializer");
5508 RESTORE_SPELLING_DEPTH (constructor_depth);
5509 }
5510 }
5511
5512 /* Pad out the end of the structure. */
5513 if (p->replacement_value.value)
5514 /* If this closes a superfluous brace pair,
5515 just pass out the element between them. */
5516 ret = p->replacement_value;
5517 else if (constructor_type == 0)
5518 ;
5519 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5520 && TREE_CODE (constructor_type) != UNION_TYPE
5521 && TREE_CODE (constructor_type) != ARRAY_TYPE
5522 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5523 {
5524 /* A nonincremental scalar initializer--just return
5525 the element, after verifying there is just one. */
5526 if (VEC_empty (constructor_elt,constructor_elements))
5527 {
5528 if (!constructor_erroneous)
5529 error_init ("empty scalar initializer");
5530 ret.value = error_mark_node;
5531 }
5532 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5533 {
5534 error_init ("extra elements in scalar initializer");
5535 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5536 }
5537 else
5538 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5539 }
5540 else
5541 {
5542 if (constructor_erroneous)
5543 ret.value = error_mark_node;
5544 else
5545 {
5546 ret.value = build_constructor (constructor_type,
5547 constructor_elements);
5548 if (constructor_constant)
5549 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5550 if (constructor_constant && constructor_simple)
5551 TREE_STATIC (ret.value) = 1;
5552 }
5553 }
5554
5555 constructor_type = p->type;
5556 constructor_fields = p->fields;
5557 constructor_index = p->index;
5558 constructor_max_index = p->max_index;
5559 constructor_unfilled_index = p->unfilled_index;
5560 constructor_unfilled_fields = p->unfilled_fields;
5561 constructor_bit_index = p->bit_index;
5562 constructor_elements = p->elements;
5563 constructor_constant = p->constant;
5564 constructor_simple = p->simple;
5565 constructor_erroneous = p->erroneous;
5566 constructor_incremental = p->incremental;
5567 constructor_designated = p->designated;
5568 constructor_pending_elts = p->pending_elts;
5569 constructor_depth = p->depth;
5570 if (!p->implicit)
5571 constructor_range_stack = p->range_stack;
5572 RESTORE_SPELLING_DEPTH (constructor_depth);
5573
5574 constructor_stack = p->next;
5575 free (p);
5576
5577 if (ret.value == 0)
5578 {
5579 if (constructor_stack == 0)
5580 {
5581 ret.value = error_mark_node;
5582 return ret;
5583 }
5584 return ret;
5585 }
5586 return ret;
5587 }
5588
5589 /* Common handling for both array range and field name designators.
5590 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5591
5592 static int
set_designator(int array)5593 set_designator (int array)
5594 {
5595 tree subtype;
5596 enum tree_code subcode;
5597
5598 /* Don't die if an entire brace-pair level is superfluous
5599 in the containing level. */
5600 if (constructor_type == 0)
5601 return 1;
5602
5603 /* If there were errors in this designator list already, bail out
5604 silently. */
5605 if (designator_erroneous)
5606 return 1;
5607
5608 if (!designator_depth)
5609 {
5610 gcc_assert (!constructor_range_stack);
5611
5612 /* Designator list starts at the level of closest explicit
5613 braces. */
5614 while (constructor_stack->implicit)
5615 process_init_element (pop_init_level (1));
5616 constructor_designated = 1;
5617 return 0;
5618 }
5619
5620 switch (TREE_CODE (constructor_type))
5621 {
5622 case RECORD_TYPE:
5623 case UNION_TYPE:
5624 subtype = TREE_TYPE (constructor_fields);
5625 if (subtype != error_mark_node)
5626 subtype = TYPE_MAIN_VARIANT (subtype);
5627 break;
5628 case ARRAY_TYPE:
5629 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5630 break;
5631 default:
5632 gcc_unreachable ();
5633 }
5634
5635 subcode = TREE_CODE (subtype);
5636 if (array && subcode != ARRAY_TYPE)
5637 {
5638 error_init ("array index in non-array initializer");
5639 return 1;
5640 }
5641 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5642 {
5643 error_init ("field name not in record or union initializer");
5644 return 1;
5645 }
5646
5647 constructor_designated = 1;
5648 push_init_level (2);
5649 return 0;
5650 }
5651
5652 /* If there are range designators in designator list, push a new designator
5653 to constructor_range_stack. RANGE_END is end of such stack range or
5654 NULL_TREE if there is no range designator at this level. */
5655
5656 static void
push_range_stack(tree range_end)5657 push_range_stack (tree range_end)
5658 {
5659 struct constructor_range_stack *p;
5660
5661 p = GGC_NEW (struct constructor_range_stack);
5662 p->prev = constructor_range_stack;
5663 p->next = 0;
5664 p->fields = constructor_fields;
5665 p->range_start = constructor_index;
5666 p->index = constructor_index;
5667 p->stack = constructor_stack;
5668 p->range_end = range_end;
5669 if (constructor_range_stack)
5670 constructor_range_stack->next = p;
5671 constructor_range_stack = p;
5672 }
5673
5674 /* Within an array initializer, specify the next index to be initialized.
5675 FIRST is that index. If LAST is nonzero, then initialize a range
5676 of indices, running from FIRST through LAST. */
5677
5678 void
set_init_index(tree first,tree last)5679 set_init_index (tree first, tree last)
5680 {
5681 if (set_designator (1))
5682 return;
5683
5684 designator_erroneous = 1;
5685
5686 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5687 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5688 {
5689 error_init ("array index in initializer not of integer type");
5690 return;
5691 }
5692
5693 if (TREE_CODE (first) != INTEGER_CST)
5694 error_init ("nonconstant array index in initializer");
5695 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5696 error_init ("nonconstant array index in initializer");
5697 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5698 error_init ("array index in non-array initializer");
5699 else if (tree_int_cst_sgn (first) == -1)
5700 error_init ("array index in initializer exceeds array bounds");
5701 else if (constructor_max_index
5702 && tree_int_cst_lt (constructor_max_index, first))
5703 error_init ("array index in initializer exceeds array bounds");
5704 else
5705 {
5706 constructor_index = convert (bitsizetype, first);
5707
5708 if (last)
5709 {
5710 if (tree_int_cst_equal (first, last))
5711 last = 0;
5712 else if (tree_int_cst_lt (last, first))
5713 {
5714 error_init ("empty index range in initializer");
5715 last = 0;
5716 }
5717 else
5718 {
5719 last = convert (bitsizetype, last);
5720 if (constructor_max_index != 0
5721 && tree_int_cst_lt (constructor_max_index, last))
5722 {
5723 error_init ("array index range in initializer exceeds array bounds");
5724 last = 0;
5725 }
5726 }
5727 }
5728
5729 designator_depth++;
5730 designator_erroneous = 0;
5731 if (constructor_range_stack || last)
5732 push_range_stack (last);
5733 }
5734 }
5735
5736 /* Within a struct initializer, specify the next field to be initialized. */
5737
5738 void
set_init_label(tree fieldname)5739 set_init_label (tree fieldname)
5740 {
5741 tree tail;
5742
5743 if (set_designator (0))
5744 return;
5745
5746 designator_erroneous = 1;
5747
5748 if (TREE_CODE (constructor_type) != RECORD_TYPE
5749 && TREE_CODE (constructor_type) != UNION_TYPE)
5750 {
5751 error_init ("field name not in record or union initializer");
5752 return;
5753 }
5754
5755 for (tail = TYPE_FIELDS (constructor_type); tail;
5756 tail = TREE_CHAIN (tail))
5757 {
5758 if (DECL_NAME (tail) == fieldname)
5759 break;
5760 }
5761
5762 if (tail == 0)
5763 error ("unknown field %qE specified in initializer", fieldname);
5764 else
5765 {
5766 constructor_fields = tail;
5767 designator_depth++;
5768 designator_erroneous = 0;
5769 if (constructor_range_stack)
5770 push_range_stack (NULL_TREE);
5771 }
5772 }
5773
5774 /* Add a new initializer to the tree of pending initializers. PURPOSE
5775 identifies the initializer, either array index or field in a structure.
5776 VALUE is the value of that index or field. */
5777
5778 static void
add_pending_init(tree purpose,tree value)5779 add_pending_init (tree purpose, tree value)
5780 {
5781 struct init_node *p, **q, *r;
5782
5783 q = &constructor_pending_elts;
5784 p = 0;
5785
5786 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5787 {
5788 while (*q != 0)
5789 {
5790 p = *q;
5791 if (tree_int_cst_lt (purpose, p->purpose))
5792 q = &p->left;
5793 else if (tree_int_cst_lt (p->purpose, purpose))
5794 q = &p->right;
5795 else
5796 {
5797 if (TREE_SIDE_EFFECTS (p->value))
5798 warning_init ("initialized field with side-effects overwritten");
5799 p->value = value;
5800 return;
5801 }
5802 }
5803 }
5804 else
5805 {
5806 tree bitpos;
5807
5808 bitpos = bit_position (purpose);
5809 while (*q != NULL)
5810 {
5811 p = *q;
5812 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5813 q = &p->left;
5814 else if (p->purpose != purpose)
5815 q = &p->right;
5816 else
5817 {
5818 if (TREE_SIDE_EFFECTS (p->value))
5819 warning_init ("initialized field with side-effects overwritten");
5820 p->value = value;
5821 return;
5822 }
5823 }
5824 }
5825
5826 r = GGC_NEW (struct init_node);
5827 r->purpose = purpose;
5828 r->value = value;
5829
5830 *q = r;
5831 r->parent = p;
5832 r->left = 0;
5833 r->right = 0;
5834 r->balance = 0;
5835
5836 while (p)
5837 {
5838 struct init_node *s;
5839
5840 if (r == p->left)
5841 {
5842 if (p->balance == 0)
5843 p->balance = -1;
5844 else if (p->balance < 0)
5845 {
5846 if (r->balance < 0)
5847 {
5848 /* L rotation. */
5849 p->left = r->right;
5850 if (p->left)
5851 p->left->parent = p;
5852 r->right = p;
5853
5854 p->balance = 0;
5855 r->balance = 0;
5856
5857 s = p->parent;
5858 p->parent = r;
5859 r->parent = s;
5860 if (s)
5861 {
5862 if (s->left == p)
5863 s->left = r;
5864 else
5865 s->right = r;
5866 }
5867 else
5868 constructor_pending_elts = r;
5869 }
5870 else
5871 {
5872 /* LR rotation. */
5873 struct init_node *t = r->right;
5874
5875 r->right = t->left;
5876 if (r->right)
5877 r->right->parent = r;
5878 t->left = r;
5879
5880 p->left = t->right;
5881 if (p->left)
5882 p->left->parent = p;
5883 t->right = p;
5884
5885 p->balance = t->balance < 0;
5886 r->balance = -(t->balance > 0);
5887 t->balance = 0;
5888
5889 s = p->parent;
5890 p->parent = t;
5891 r->parent = t;
5892 t->parent = s;
5893 if (s)
5894 {
5895 if (s->left == p)
5896 s->left = t;
5897 else
5898 s->right = t;
5899 }
5900 else
5901 constructor_pending_elts = t;
5902 }
5903 break;
5904 }
5905 else
5906 {
5907 /* p->balance == +1; growth of left side balances the node. */
5908 p->balance = 0;
5909 break;
5910 }
5911 }
5912 else /* r == p->right */
5913 {
5914 if (p->balance == 0)
5915 /* Growth propagation from right side. */
5916 p->balance++;
5917 else if (p->balance > 0)
5918 {
5919 if (r->balance > 0)
5920 {
5921 /* R rotation. */
5922 p->right = r->left;
5923 if (p->right)
5924 p->right->parent = p;
5925 r->left = p;
5926
5927 p->balance = 0;
5928 r->balance = 0;
5929
5930 s = p->parent;
5931 p->parent = r;
5932 r->parent = s;
5933 if (s)
5934 {
5935 if (s->left == p)
5936 s->left = r;
5937 else
5938 s->right = r;
5939 }
5940 else
5941 constructor_pending_elts = r;
5942 }
5943 else /* r->balance == -1 */
5944 {
5945 /* RL rotation */
5946 struct init_node *t = r->left;
5947
5948 r->left = t->right;
5949 if (r->left)
5950 r->left->parent = r;
5951 t->right = r;
5952
5953 p->right = t->left;
5954 if (p->right)
5955 p->right->parent = p;
5956 t->left = p;
5957
5958 r->balance = (t->balance < 0);
5959 p->balance = -(t->balance > 0);
5960 t->balance = 0;
5961
5962 s = p->parent;
5963 p->parent = t;
5964 r->parent = t;
5965 t->parent = s;
5966 if (s)
5967 {
5968 if (s->left == p)
5969 s->left = t;
5970 else
5971 s->right = t;
5972 }
5973 else
5974 constructor_pending_elts = t;
5975 }
5976 break;
5977 }
5978 else
5979 {
5980 /* p->balance == -1; growth of right side balances the node. */
5981 p->balance = 0;
5982 break;
5983 }
5984 }
5985
5986 r = p;
5987 p = p->parent;
5988 }
5989 }
5990
5991 /* Build AVL tree from a sorted chain. */
5992
5993 static void
set_nonincremental_init(void)5994 set_nonincremental_init (void)
5995 {
5996 unsigned HOST_WIDE_INT ix;
5997 tree index, value;
5998
5999 if (TREE_CODE (constructor_type) != RECORD_TYPE
6000 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6001 return;
6002
6003 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
6004 add_pending_init (index, value);
6005 constructor_elements = 0;
6006 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6007 {
6008 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6009 /* Skip any nameless bit fields at the beginning. */
6010 while (constructor_unfilled_fields != 0
6011 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6012 && DECL_NAME (constructor_unfilled_fields) == 0)
6013 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6014
6015 }
6016 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6017 {
6018 if (TYPE_DOMAIN (constructor_type))
6019 constructor_unfilled_index
6020 = convert (bitsizetype,
6021 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6022 else
6023 constructor_unfilled_index = bitsize_zero_node;
6024 }
6025 constructor_incremental = 0;
6026 }
6027
6028 /* Build AVL tree from a string constant. */
6029
6030 static void
set_nonincremental_init_from_string(tree str)6031 set_nonincremental_init_from_string (tree str)
6032 {
6033 tree value, purpose, type;
6034 HOST_WIDE_INT val[2];
6035 const char *p, *end;
6036 int byte, wchar_bytes, charwidth, bitpos;
6037
6038 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
6039
6040 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6041 == TYPE_PRECISION (char_type_node))
6042 wchar_bytes = 1;
6043 else
6044 {
6045 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6046 == TYPE_PRECISION (wchar_type_node));
6047 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6048 }
6049 charwidth = TYPE_PRECISION (char_type_node);
6050 type = TREE_TYPE (constructor_type);
6051 p = TREE_STRING_POINTER (str);
6052 end = p + TREE_STRING_LENGTH (str);
6053
6054 for (purpose = bitsize_zero_node;
6055 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6056 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6057 {
6058 if (wchar_bytes == 1)
6059 {
6060 val[1] = (unsigned char) *p++;
6061 val[0] = 0;
6062 }
6063 else
6064 {
6065 val[0] = 0;
6066 val[1] = 0;
6067 for (byte = 0; byte < wchar_bytes; byte++)
6068 {
6069 if (BYTES_BIG_ENDIAN)
6070 bitpos = (wchar_bytes - byte - 1) * charwidth;
6071 else
6072 bitpos = byte * charwidth;
6073 val[bitpos < HOST_BITS_PER_WIDE_INT]
6074 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6075 << (bitpos % HOST_BITS_PER_WIDE_INT);
6076 }
6077 }
6078
6079 if (!TYPE_UNSIGNED (type))
6080 {
6081 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6082 if (bitpos < HOST_BITS_PER_WIDE_INT)
6083 {
6084 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6085 {
6086 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6087 val[0] = -1;
6088 }
6089 }
6090 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6091 {
6092 if (val[1] < 0)
6093 val[0] = -1;
6094 }
6095 else if (val[0] & (((HOST_WIDE_INT) 1)
6096 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6097 val[0] |= ((HOST_WIDE_INT) -1)
6098 << (bitpos - HOST_BITS_PER_WIDE_INT);
6099 }
6100
6101 value = build_int_cst_wide (type, val[1], val[0]);
6102 add_pending_init (purpose, value);
6103 }
6104
6105 constructor_incremental = 0;
6106 }
6107
6108 /* Return value of FIELD in pending initializer or zero if the field was
6109 not initialized yet. */
6110
6111 static tree
find_init_member(tree field)6112 find_init_member (tree field)
6113 {
6114 struct init_node *p;
6115
6116 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6117 {
6118 if (constructor_incremental
6119 && tree_int_cst_lt (field, constructor_unfilled_index))
6120 set_nonincremental_init ();
6121
6122 p = constructor_pending_elts;
6123 while (p)
6124 {
6125 if (tree_int_cst_lt (field, p->purpose))
6126 p = p->left;
6127 else if (tree_int_cst_lt (p->purpose, field))
6128 p = p->right;
6129 else
6130 return p->value;
6131 }
6132 }
6133 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6134 {
6135 tree bitpos = bit_position (field);
6136
6137 if (constructor_incremental
6138 && (!constructor_unfilled_fields
6139 || tree_int_cst_lt (bitpos,
6140 bit_position (constructor_unfilled_fields))))
6141 set_nonincremental_init ();
6142
6143 p = constructor_pending_elts;
6144 while (p)
6145 {
6146 if (field == p->purpose)
6147 return p->value;
6148 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6149 p = p->left;
6150 else
6151 p = p->right;
6152 }
6153 }
6154 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6155 {
6156 if (!VEC_empty (constructor_elt, constructor_elements)
6157 && (VEC_last (constructor_elt, constructor_elements)->index
6158 == field))
6159 return VEC_last (constructor_elt, constructor_elements)->value;
6160 }
6161 return 0;
6162 }
6163
6164 /* "Output" the next constructor element.
6165 At top level, really output it to assembler code now.
6166 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6167 TYPE is the data type that the containing data type wants here.
6168 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6169 If VALUE is a string constant, STRICT_STRING is true if it is
6170 unparenthesized or we should not warn here for it being parenthesized.
6171 For other types of VALUE, STRICT_STRING is not used.
6172
6173 PENDING if non-nil means output pending elements that belong
6174 right after this element. (PENDING is normally 1;
6175 it is 0 while outputting pending elements, to avoid recursion.) */
6176
6177 static void
output_init_element(tree value,bool strict_string,tree type,tree field,int pending)6178 output_init_element (tree value, bool strict_string, tree type, tree field,
6179 int pending)
6180 {
6181 constructor_elt *celt;
6182
6183 if (type == error_mark_node || value == error_mark_node)
6184 {
6185 constructor_erroneous = 1;
6186 return;
6187 }
6188 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6189 && (TREE_CODE (value) == STRING_CST
6190 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6191 && !(TREE_CODE (value) == STRING_CST
6192 && TREE_CODE (type) == ARRAY_TYPE
6193 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
6194 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6195 TYPE_MAIN_VARIANT (type)))
6196 value = array_to_pointer_conversion (value);
6197
6198 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6199 && require_constant_value && !flag_isoc99 && pending)
6200 {
6201 /* As an extension, allow initializing objects with static storage
6202 duration with compound literals (which are then treated just as
6203 the brace enclosed list they contain). */
6204 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6205 value = DECL_INITIAL (decl);
6206 }
6207
6208 if (value == error_mark_node)
6209 constructor_erroneous = 1;
6210 else if (!TREE_CONSTANT (value))
6211 constructor_constant = 0;
6212 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6213 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6214 || TREE_CODE (constructor_type) == UNION_TYPE)
6215 && DECL_C_BIT_FIELD (field)
6216 && TREE_CODE (value) != INTEGER_CST))
6217 constructor_simple = 0;
6218
6219 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6220 {
6221 if (require_constant_value)
6222 {
6223 error_init ("initializer element is not constant");
6224 value = error_mark_node;
6225 }
6226 else if (require_constant_elements)
6227 pedwarn ("initializer element is not computable at load time");
6228 }
6229
6230 /* If this field is empty (and not at the end of structure),
6231 don't do anything other than checking the initializer. */
6232 if (field
6233 && (TREE_TYPE (field) == error_mark_node
6234 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6235 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6236 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6237 || TREE_CHAIN (field)))))
6238 return;
6239
6240 value = digest_init (type, value, strict_string, require_constant_value);
6241 if (value == error_mark_node)
6242 {
6243 constructor_erroneous = 1;
6244 return;
6245 }
6246
6247 /* If this element doesn't come next in sequence,
6248 put it on constructor_pending_elts. */
6249 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6250 && (!constructor_incremental
6251 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6252 {
6253 if (constructor_incremental
6254 && tree_int_cst_lt (field, constructor_unfilled_index))
6255 set_nonincremental_init ();
6256
6257 add_pending_init (field, value);
6258 return;
6259 }
6260 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6261 && (!constructor_incremental
6262 || field != constructor_unfilled_fields))
6263 {
6264 /* We do this for records but not for unions. In a union,
6265 no matter which field is specified, it can be initialized
6266 right away since it starts at the beginning of the union. */
6267 if (constructor_incremental)
6268 {
6269 if (!constructor_unfilled_fields)
6270 set_nonincremental_init ();
6271 else
6272 {
6273 tree bitpos, unfillpos;
6274
6275 bitpos = bit_position (field);
6276 unfillpos = bit_position (constructor_unfilled_fields);
6277
6278 if (tree_int_cst_lt (bitpos, unfillpos))
6279 set_nonincremental_init ();
6280 }
6281 }
6282
6283 add_pending_init (field, value);
6284 return;
6285 }
6286 else if (TREE_CODE (constructor_type) == UNION_TYPE
6287 && !VEC_empty (constructor_elt, constructor_elements))
6288 {
6289 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6290 constructor_elements)->value))
6291 warning_init ("initialized field with side-effects overwritten");
6292
6293 /* We can have just one union field set. */
6294 constructor_elements = 0;
6295 }
6296
6297 /* Otherwise, output this element either to
6298 constructor_elements or to the assembler file. */
6299
6300 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6301 celt->index = field;
6302 celt->value = value;
6303
6304 /* Advance the variable that indicates sequential elements output. */
6305 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6306 constructor_unfilled_index
6307 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6308 bitsize_one_node);
6309 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6310 {
6311 constructor_unfilled_fields
6312 = TREE_CHAIN (constructor_unfilled_fields);
6313
6314 /* Skip any nameless bit fields. */
6315 while (constructor_unfilled_fields != 0
6316 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6317 && DECL_NAME (constructor_unfilled_fields) == 0)
6318 constructor_unfilled_fields =
6319 TREE_CHAIN (constructor_unfilled_fields);
6320 }
6321 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6322 constructor_unfilled_fields = 0;
6323
6324 /* Now output any pending elements which have become next. */
6325 if (pending)
6326 output_pending_init_elements (0);
6327 }
6328
6329 /* Output any pending elements which have become next.
6330 As we output elements, constructor_unfilled_{fields,index}
6331 advances, which may cause other elements to become next;
6332 if so, they too are output.
6333
6334 If ALL is 0, we return when there are
6335 no more pending elements to output now.
6336
6337 If ALL is 1, we output space as necessary so that
6338 we can output all the pending elements. */
6339
6340 static void
output_pending_init_elements(int all)6341 output_pending_init_elements (int all)
6342 {
6343 struct init_node *elt = constructor_pending_elts;
6344 tree next;
6345
6346 retry:
6347
6348 /* Look through the whole pending tree.
6349 If we find an element that should be output now,
6350 output it. Otherwise, set NEXT to the element
6351 that comes first among those still pending. */
6352
6353 next = 0;
6354 while (elt)
6355 {
6356 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6357 {
6358 if (tree_int_cst_equal (elt->purpose,
6359 constructor_unfilled_index))
6360 output_init_element (elt->value, true,
6361 TREE_TYPE (constructor_type),
6362 constructor_unfilled_index, 0);
6363 else if (tree_int_cst_lt (constructor_unfilled_index,
6364 elt->purpose))
6365 {
6366 /* Advance to the next smaller node. */
6367 if (elt->left)
6368 elt = elt->left;
6369 else
6370 {
6371 /* We have reached the smallest node bigger than the
6372 current unfilled index. Fill the space first. */
6373 next = elt->purpose;
6374 break;
6375 }
6376 }
6377 else
6378 {
6379 /* Advance to the next bigger node. */
6380 if (elt->right)
6381 elt = elt->right;
6382 else
6383 {
6384 /* We have reached the biggest node in a subtree. Find
6385 the parent of it, which is the next bigger node. */
6386 while (elt->parent && elt->parent->right == elt)
6387 elt = elt->parent;
6388 elt = elt->parent;
6389 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6390 elt->purpose))
6391 {
6392 next = elt->purpose;
6393 break;
6394 }
6395 }
6396 }
6397 }
6398 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6399 || TREE_CODE (constructor_type) == UNION_TYPE)
6400 {
6401 tree ctor_unfilled_bitpos, elt_bitpos;
6402
6403 /* If the current record is complete we are done. */
6404 if (constructor_unfilled_fields == 0)
6405 break;
6406
6407 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6408 elt_bitpos = bit_position (elt->purpose);
6409 /* We can't compare fields here because there might be empty
6410 fields in between. */
6411 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6412 {
6413 constructor_unfilled_fields = elt->purpose;
6414 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6415 elt->purpose, 0);
6416 }
6417 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6418 {
6419 /* Advance to the next smaller node. */
6420 if (elt->left)
6421 elt = elt->left;
6422 else
6423 {
6424 /* We have reached the smallest node bigger than the
6425 current unfilled field. Fill the space first. */
6426 next = elt->purpose;
6427 break;
6428 }
6429 }
6430 else
6431 {
6432 /* Advance to the next bigger node. */
6433 if (elt->right)
6434 elt = elt->right;
6435 else
6436 {
6437 /* We have reached the biggest node in a subtree. Find
6438 the parent of it, which is the next bigger node. */
6439 while (elt->parent && elt->parent->right == elt)
6440 elt = elt->parent;
6441 elt = elt->parent;
6442 if (elt
6443 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6444 bit_position (elt->purpose))))
6445 {
6446 next = elt->purpose;
6447 break;
6448 }
6449 }
6450 }
6451 }
6452 }
6453
6454 /* Ordinarily return, but not if we want to output all
6455 and there are elements left. */
6456 if (!(all && next != 0))
6457 return;
6458
6459 /* If it's not incremental, just skip over the gap, so that after
6460 jumping to retry we will output the next successive element. */
6461 if (TREE_CODE (constructor_type) == RECORD_TYPE
6462 || TREE_CODE (constructor_type) == UNION_TYPE)
6463 constructor_unfilled_fields = next;
6464 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6465 constructor_unfilled_index = next;
6466
6467 /* ELT now points to the node in the pending tree with the next
6468 initializer to output. */
6469 goto retry;
6470 }
6471
6472 /* Add one non-braced element to the current constructor level.
6473 This adjusts the current position within the constructor's type.
6474 This may also start or terminate implicit levels
6475 to handle a partly-braced initializer.
6476
6477 Once this has found the correct level for the new element,
6478 it calls output_init_element. */
6479
6480 void
process_init_element(struct c_expr value)6481 process_init_element (struct c_expr value)
6482 {
6483 tree orig_value = value.value;
6484 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6485 bool strict_string = value.original_code == STRING_CST;
6486
6487 designator_depth = 0;
6488 designator_erroneous = 0;
6489
6490 /* Handle superfluous braces around string cst as in
6491 char x[] = {"foo"}; */
6492 if (string_flag
6493 && constructor_type
6494 && TREE_CODE (constructor_type) == ARRAY_TYPE
6495 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6496 && integer_zerop (constructor_unfilled_index))
6497 {
6498 if (constructor_stack->replacement_value.value)
6499 error_init ("excess elements in char array initializer");
6500 constructor_stack->replacement_value = value;
6501 return;
6502 }
6503
6504 if (constructor_stack->replacement_value.value != 0)
6505 {
6506 error_init ("excess elements in struct initializer");
6507 return;
6508 }
6509
6510 /* Ignore elements of a brace group if it is entirely superfluous
6511 and has already been diagnosed. */
6512 if (constructor_type == 0)
6513 return;
6514
6515 /* If we've exhausted any levels that didn't have braces,
6516 pop them now. */
6517 while (constructor_stack->implicit)
6518 {
6519 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6520 || TREE_CODE (constructor_type) == UNION_TYPE)
6521 && constructor_fields == 0)
6522 process_init_element (pop_init_level (1));
6523 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6524 && (constructor_max_index == 0
6525 || tree_int_cst_lt (constructor_max_index,
6526 constructor_index)))
6527 process_init_element (pop_init_level (1));
6528 else
6529 break;
6530 }
6531
6532 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6533 if (constructor_range_stack)
6534 {
6535 /* If value is a compound literal and we'll be just using its
6536 content, don't put it into a SAVE_EXPR. */
6537 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6538 || !require_constant_value
6539 || flag_isoc99)
6540 value.value = save_expr (value.value);
6541 }
6542
6543 while (1)
6544 {
6545 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6546 {
6547 tree fieldtype;
6548 enum tree_code fieldcode;
6549
6550 if (constructor_fields == 0)
6551 {
6552 pedwarn_init ("excess elements in struct initializer");
6553 break;
6554 }
6555
6556 fieldtype = TREE_TYPE (constructor_fields);
6557 if (fieldtype != error_mark_node)
6558 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6559 fieldcode = TREE_CODE (fieldtype);
6560
6561 /* Error for non-static initialization of a flexible array member. */
6562 if (fieldcode == ARRAY_TYPE
6563 && !require_constant_value
6564 && TYPE_SIZE (fieldtype) == NULL_TREE
6565 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6566 {
6567 error_init ("non-static initialization of a flexible array member");
6568 break;
6569 }
6570
6571 /* Accept a string constant to initialize a subarray. */
6572 if (value.value != 0
6573 && fieldcode == ARRAY_TYPE
6574 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6575 && string_flag)
6576 value.value = orig_value;
6577 /* Otherwise, if we have come to a subaggregate,
6578 and we don't have an element of its type, push into it. */
6579 else if (value.value != 0
6580 && value.value != error_mark_node
6581 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6582 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6583 || fieldcode == UNION_TYPE))
6584 {
6585 push_init_level (1);
6586 continue;
6587 }
6588
6589 if (value.value)
6590 {
6591 push_member_name (constructor_fields);
6592 output_init_element (value.value, strict_string,
6593 fieldtype, constructor_fields, 1);
6594 RESTORE_SPELLING_DEPTH (constructor_depth);
6595 }
6596 else
6597 /* Do the bookkeeping for an element that was
6598 directly output as a constructor. */
6599 {
6600 /* For a record, keep track of end position of last field. */
6601 if (DECL_SIZE (constructor_fields))
6602 constructor_bit_index
6603 = size_binop (PLUS_EXPR,
6604 bit_position (constructor_fields),
6605 DECL_SIZE (constructor_fields));
6606
6607 /* If the current field was the first one not yet written out,
6608 it isn't now, so update. */
6609 if (constructor_unfilled_fields == constructor_fields)
6610 {
6611 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6612 /* Skip any nameless bit fields. */
6613 while (constructor_unfilled_fields != 0
6614 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6615 && DECL_NAME (constructor_unfilled_fields) == 0)
6616 constructor_unfilled_fields =
6617 TREE_CHAIN (constructor_unfilled_fields);
6618 }
6619 }
6620
6621 constructor_fields = TREE_CHAIN (constructor_fields);
6622 /* Skip any nameless bit fields at the beginning. */
6623 while (constructor_fields != 0
6624 && DECL_C_BIT_FIELD (constructor_fields)
6625 && DECL_NAME (constructor_fields) == 0)
6626 constructor_fields = TREE_CHAIN (constructor_fields);
6627 }
6628 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6629 {
6630 tree fieldtype;
6631 enum tree_code fieldcode;
6632
6633 if (constructor_fields == 0)
6634 {
6635 pedwarn_init ("excess elements in union initializer");
6636 break;
6637 }
6638
6639 fieldtype = TREE_TYPE (constructor_fields);
6640 if (fieldtype != error_mark_node)
6641 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6642 fieldcode = TREE_CODE (fieldtype);
6643
6644 /* Warn that traditional C rejects initialization of unions.
6645 We skip the warning if the value is zero. This is done
6646 under the assumption that the zero initializer in user
6647 code appears conditioned on e.g. __STDC__ to avoid
6648 "missing initializer" warnings and relies on default
6649 initialization to zero in the traditional C case.
6650 We also skip the warning if the initializer is designated,
6651 again on the assumption that this must be conditional on
6652 __STDC__ anyway (and we've already complained about the
6653 member-designator already). */
6654 if (!in_system_header && !constructor_designated
6655 && !(value.value && (integer_zerop (value.value)
6656 || real_zerop (value.value))))
6657 warning (OPT_Wtraditional, "traditional C rejects initialization "
6658 "of unions");
6659
6660 /* Accept a string constant to initialize a subarray. */
6661 if (value.value != 0
6662 && fieldcode == ARRAY_TYPE
6663 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6664 && string_flag)
6665 value.value = orig_value;
6666 /* Otherwise, if we have come to a subaggregate,
6667 and we don't have an element of its type, push into it. */
6668 else if (value.value != 0
6669 && value.value != error_mark_node
6670 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6671 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6672 || fieldcode == UNION_TYPE))
6673 {
6674 push_init_level (1);
6675 continue;
6676 }
6677
6678 if (value.value)
6679 {
6680 push_member_name (constructor_fields);
6681 output_init_element (value.value, strict_string,
6682 fieldtype, constructor_fields, 1);
6683 RESTORE_SPELLING_DEPTH (constructor_depth);
6684 }
6685 else
6686 /* Do the bookkeeping for an element that was
6687 directly output as a constructor. */
6688 {
6689 constructor_bit_index = DECL_SIZE (constructor_fields);
6690 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6691 }
6692
6693 constructor_fields = 0;
6694 }
6695 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6696 {
6697 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6698 enum tree_code eltcode = TREE_CODE (elttype);
6699
6700 /* Accept a string constant to initialize a subarray. */
6701 if (value.value != 0
6702 && eltcode == ARRAY_TYPE
6703 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6704 && string_flag)
6705 value.value = orig_value;
6706 /* Otherwise, if we have come to a subaggregate,
6707 and we don't have an element of its type, push into it. */
6708 else if (value.value != 0
6709 && value.value != error_mark_node
6710 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6711 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6712 || eltcode == UNION_TYPE))
6713 {
6714 push_init_level (1);
6715 continue;
6716 }
6717
6718 if (constructor_max_index != 0
6719 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6720 || integer_all_onesp (constructor_max_index)))
6721 {
6722 pedwarn_init ("excess elements in array initializer");
6723 break;
6724 }
6725
6726 /* Now output the actual element. */
6727 if (value.value)
6728 {
6729 push_array_bounds (tree_low_cst (constructor_index, 1));
6730 output_init_element (value.value, strict_string,
6731 elttype, constructor_index, 1);
6732 RESTORE_SPELLING_DEPTH (constructor_depth);
6733 }
6734
6735 constructor_index
6736 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6737
6738 if (!value.value)
6739 /* If we are doing the bookkeeping for an element that was
6740 directly output as a constructor, we must update
6741 constructor_unfilled_index. */
6742 constructor_unfilled_index = constructor_index;
6743 }
6744 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6745 {
6746 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6747
6748 /* Do a basic check of initializer size. Note that vectors
6749 always have a fixed size derived from their type. */
6750 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6751 {
6752 pedwarn_init ("excess elements in vector initializer");
6753 break;
6754 }
6755
6756 /* Now output the actual element. */
6757 if (value.value)
6758 output_init_element (value.value, strict_string,
6759 elttype, constructor_index, 1);
6760
6761 constructor_index
6762 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6763
6764 if (!value.value)
6765 /* If we are doing the bookkeeping for an element that was
6766 directly output as a constructor, we must update
6767 constructor_unfilled_index. */
6768 constructor_unfilled_index = constructor_index;
6769 }
6770
6771 /* Handle the sole element allowed in a braced initializer
6772 for a scalar variable. */
6773 else if (constructor_type != error_mark_node
6774 && constructor_fields == 0)
6775 {
6776 pedwarn_init ("excess elements in scalar initializer");
6777 break;
6778 }
6779 else
6780 {
6781 if (value.value)
6782 output_init_element (value.value, strict_string,
6783 constructor_type, NULL_TREE, 1);
6784 constructor_fields = 0;
6785 }
6786
6787 /* Handle range initializers either at this level or anywhere higher
6788 in the designator stack. */
6789 if (constructor_range_stack)
6790 {
6791 struct constructor_range_stack *p, *range_stack;
6792 int finish = 0;
6793
6794 range_stack = constructor_range_stack;
6795 constructor_range_stack = 0;
6796 while (constructor_stack != range_stack->stack)
6797 {
6798 gcc_assert (constructor_stack->implicit);
6799 process_init_element (pop_init_level (1));
6800 }
6801 for (p = range_stack;
6802 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6803 p = p->prev)
6804 {
6805 gcc_assert (constructor_stack->implicit);
6806 process_init_element (pop_init_level (1));
6807 }
6808
6809 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6810 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6811 finish = 1;
6812
6813 while (1)
6814 {
6815 constructor_index = p->index;
6816 constructor_fields = p->fields;
6817 if (finish && p->range_end && p->index == p->range_start)
6818 {
6819 finish = 0;
6820 p->prev = 0;
6821 }
6822 p = p->next;
6823 if (!p)
6824 break;
6825 push_init_level (2);
6826 p->stack = constructor_stack;
6827 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6828 p->index = p->range_start;
6829 }
6830
6831 if (!finish)
6832 constructor_range_stack = range_stack;
6833 continue;
6834 }
6835
6836 break;
6837 }
6838
6839 constructor_range_stack = 0;
6840 }
6841
6842 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6843 (guaranteed to be 'volatile' or null) and ARGS (represented using
6844 an ASM_EXPR node). */
6845 tree
build_asm_stmt(tree cv_qualifier,tree args)6846 build_asm_stmt (tree cv_qualifier, tree args)
6847 {
6848 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6849 ASM_VOLATILE_P (args) = 1;
6850 return add_stmt (args);
6851 }
6852
6853 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6854 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6855 SIMPLE indicates whether there was anything at all after the
6856 string in the asm expression -- asm("blah") and asm("blah" : )
6857 are subtly different. We use a ASM_EXPR node to represent this. */
6858 tree
build_asm_expr(tree string,tree outputs,tree inputs,tree clobbers,bool simple)6859 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6860 bool simple)
6861 {
6862 tree tail;
6863 tree args;
6864 int i;
6865 const char *constraint;
6866 const char **oconstraints;
6867 bool allows_mem, allows_reg, is_inout;
6868 int ninputs, noutputs;
6869
6870 ninputs = list_length (inputs);
6871 noutputs = list_length (outputs);
6872 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6873
6874 string = resolve_asm_operand_names (string, outputs, inputs);
6875
6876 /* Remove output conversions that change the type but not the mode. */
6877 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6878 {
6879 tree output = TREE_VALUE (tail);
6880
6881 /* ??? Really, this should not be here. Users should be using a
6882 proper lvalue, dammit. But there's a long history of using casts
6883 in the output operands. In cases like longlong.h, this becomes a
6884 primitive form of typechecking -- if the cast can be removed, then
6885 the output operand had a type of the proper width; otherwise we'll
6886 get an error. Gross, but ... */
6887 STRIP_NOPS (output);
6888
6889 if (!lvalue_or_else (output, lv_asm))
6890 output = error_mark_node;
6891
6892 if (output != error_mark_node
6893 && (TREE_READONLY (output)
6894 || TYPE_READONLY (TREE_TYPE (output))
6895 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6896 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6897 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6898 readonly_error (output, lv_asm);
6899
6900 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6901 oconstraints[i] = constraint;
6902
6903 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6904 &allows_mem, &allows_reg, &is_inout))
6905 {
6906 /* If the operand is going to end up in memory,
6907 mark it addressable. */
6908 if (!allows_reg && !c_mark_addressable (output))
6909 output = error_mark_node;
6910 }
6911 else
6912 output = error_mark_node;
6913
6914 TREE_VALUE (tail) = output;
6915 }
6916
6917 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6918 {
6919 tree input;
6920
6921 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6922 input = TREE_VALUE (tail);
6923
6924 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6925 oconstraints, &allows_mem, &allows_reg))
6926 {
6927 /* If the operand is going to end up in memory,
6928 mark it addressable. */
6929 if (!allows_reg && allows_mem)
6930 {
6931 /* Strip the nops as we allow this case. FIXME, this really
6932 should be rejected or made deprecated. */
6933 STRIP_NOPS (input);
6934 if (!c_mark_addressable (input))
6935 input = error_mark_node;
6936 }
6937 }
6938 else
6939 input = error_mark_node;
6940
6941 TREE_VALUE (tail) = input;
6942 }
6943
6944 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6945
6946 /* asm statements without outputs, including simple ones, are treated
6947 as volatile. */
6948 ASM_INPUT_P (args) = simple;
6949 ASM_VOLATILE_P (args) = (noutputs == 0);
6950
6951 return args;
6952 }
6953
6954 /* Generate a goto statement to LABEL. */
6955
6956 tree
c_finish_goto_label(tree label)6957 c_finish_goto_label (tree label)
6958 {
6959 tree decl = lookup_label (label);
6960 if (!decl)
6961 return NULL_TREE;
6962
6963 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6964 {
6965 error ("jump into statement expression");
6966 return NULL_TREE;
6967 }
6968
6969 if (C_DECL_UNJUMPABLE_VM (decl))
6970 {
6971 error ("jump into scope of identifier with variably modified type");
6972 return NULL_TREE;
6973 }
6974
6975 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
6976 {
6977 /* No jump from outside this statement expression context, so
6978 record that there is a jump from within this context. */
6979 struct c_label_list *nlist;
6980 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6981 nlist->next = label_context_stack_se->labels_used;
6982 nlist->label = decl;
6983 label_context_stack_se->labels_used = nlist;
6984 }
6985
6986 if (!C_DECL_UNDEFINABLE_VM (decl))
6987 {
6988 /* No jump from outside this context context of identifiers with
6989 variably modified type, so record that there is a jump from
6990 within this context. */
6991 struct c_label_list *nlist;
6992 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6993 nlist->next = label_context_stack_vm->labels_used;
6994 nlist->label = decl;
6995 label_context_stack_vm->labels_used = nlist;
6996 }
6997
6998 TREE_USED (decl) = 1;
6999 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
7000 }
7001
7002 /* Generate a computed goto statement to EXPR. */
7003
7004 tree
c_finish_goto_ptr(tree expr)7005 c_finish_goto_ptr (tree expr)
7006 {
7007 if (pedantic)
7008 pedwarn ("ISO C forbids %<goto *expr;%>");
7009 expr = convert (ptr_type_node, expr);
7010 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
7011 }
7012
7013 /* Generate a C `return' statement. RETVAL is the expression for what
7014 to return, or a null pointer for `return;' with no value. */
7015
7016 tree
c_finish_return(tree retval)7017 c_finish_return (tree retval)
7018 {
7019 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
7020 bool no_warning = false;
7021
7022 if (TREE_THIS_VOLATILE (current_function_decl))
7023 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7024
7025 if (!retval)
7026 {
7027 current_function_returns_null = 1;
7028 if ((warn_return_type || flag_isoc99)
7029 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7030 {
7031 pedwarn_c99 ("%<return%> with no value, in "
7032 "function returning non-void");
7033 no_warning = true;
7034 }
7035 }
7036 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7037 {
7038 current_function_returns_null = 1;
7039 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7040 pedwarn ("%<return%> with a value, in function returning void");
7041 }
7042 else
7043 {
7044 tree t = convert_for_assignment (valtype, retval, ic_return,
7045 NULL_TREE, NULL_TREE, 0);
7046 tree res = DECL_RESULT (current_function_decl);
7047 tree inner;
7048
7049 current_function_returns_value = 1;
7050 if (t == error_mark_node)
7051 return NULL_TREE;
7052
7053 inner = t = convert (TREE_TYPE (res), t);
7054
7055 /* Strip any conversions, additions, and subtractions, and see if
7056 we are returning the address of a local variable. Warn if so. */
7057 while (1)
7058 {
7059 switch (TREE_CODE (inner))
7060 {
7061 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7062 case PLUS_EXPR:
7063 inner = TREE_OPERAND (inner, 0);
7064 continue;
7065
7066 case MINUS_EXPR:
7067 /* If the second operand of the MINUS_EXPR has a pointer
7068 type (or is converted from it), this may be valid, so
7069 don't give a warning. */
7070 {
7071 tree op1 = TREE_OPERAND (inner, 1);
7072
7073 while (!POINTER_TYPE_P (TREE_TYPE (op1))
7074 && (TREE_CODE (op1) == NOP_EXPR
7075 || TREE_CODE (op1) == NON_LVALUE_EXPR
7076 || TREE_CODE (op1) == CONVERT_EXPR))
7077 op1 = TREE_OPERAND (op1, 0);
7078
7079 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7080 break;
7081
7082 inner = TREE_OPERAND (inner, 0);
7083 continue;
7084 }
7085
7086 case ADDR_EXPR:
7087 inner = TREE_OPERAND (inner, 0);
7088
7089 while (REFERENCE_CLASS_P (inner)
7090 && TREE_CODE (inner) != INDIRECT_REF)
7091 inner = TREE_OPERAND (inner, 0);
7092
7093 if (DECL_P (inner)
7094 && !DECL_EXTERNAL (inner)
7095 && !TREE_STATIC (inner)
7096 && DECL_CONTEXT (inner) == current_function_decl)
7097 warning (0, "function returns address of local variable");
7098 break;
7099
7100 default:
7101 break;
7102 }
7103
7104 break;
7105 }
7106
7107 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
7108 }
7109
7110 ret_stmt = build_stmt (RETURN_EXPR, retval);
7111 TREE_NO_WARNING (ret_stmt) |= no_warning;
7112 return add_stmt (ret_stmt);
7113 }
7114
7115 struct c_switch {
7116 /* The SWITCH_EXPR being built. */
7117 tree switch_expr;
7118
7119 /* The original type of the testing expression, i.e. before the
7120 default conversion is applied. */
7121 tree orig_type;
7122
7123 /* A splay-tree mapping the low element of a case range to the high
7124 element, or NULL_TREE if there is no high element. Used to
7125 determine whether or not a new case label duplicates an old case
7126 label. We need a tree, rather than simply a hash table, because
7127 of the GNU case range extension. */
7128 splay_tree cases;
7129
7130 /* Number of nested statement expressions within this switch
7131 statement; if nonzero, case and default labels may not
7132 appear. */
7133 unsigned int blocked_stmt_expr;
7134
7135 /* Scope of outermost declarations of identifiers with variably
7136 modified type within this switch statement; if nonzero, case and
7137 default labels may not appear. */
7138 unsigned int blocked_vm;
7139
7140 /* The next node on the stack. */
7141 struct c_switch *next;
7142 };
7143
7144 /* A stack of the currently active switch statements. The innermost
7145 switch statement is on the top of the stack. There is no need to
7146 mark the stack for garbage collection because it is only active
7147 during the processing of the body of a function, and we never
7148 collect at that point. */
7149
7150 struct c_switch *c_switch_stack;
7151
7152 /* Start a C switch statement, testing expression EXP. Return the new
7153 SWITCH_EXPR. */
7154
7155 tree
c_start_case(tree exp)7156 c_start_case (tree exp)
7157 {
7158 enum tree_code code;
7159 tree type, orig_type = error_mark_node;
7160 struct c_switch *cs;
7161
7162 if (exp != error_mark_node)
7163 {
7164 code = TREE_CODE (TREE_TYPE (exp));
7165 orig_type = TREE_TYPE (exp);
7166
7167 if (!INTEGRAL_TYPE_P (orig_type)
7168 && code != ERROR_MARK)
7169 {
7170 error ("switch quantity not an integer");
7171 exp = integer_zero_node;
7172 orig_type = error_mark_node;
7173 }
7174 else
7175 {
7176 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7177
7178 if (!in_system_header
7179 && (type == long_integer_type_node
7180 || type == long_unsigned_type_node))
7181 warning (OPT_Wtraditional, "%<long%> switch expression not "
7182 "converted to %<int%> in ISO C");
7183
7184 exp = default_conversion (exp);
7185 type = TREE_TYPE (exp);
7186 }
7187 }
7188
7189 /* Add this new SWITCH_EXPR to the stack. */
7190 cs = XNEW (struct c_switch);
7191 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
7192 cs->orig_type = orig_type;
7193 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7194 cs->blocked_stmt_expr = 0;
7195 cs->blocked_vm = 0;
7196 cs->next = c_switch_stack;
7197 c_switch_stack = cs;
7198
7199 return add_stmt (cs->switch_expr);
7200 }
7201
7202 /* Process a case label. */
7203
7204 tree
do_case(tree low_value,tree high_value)7205 do_case (tree low_value, tree high_value)
7206 {
7207 tree label = NULL_TREE;
7208
7209 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7210 && !c_switch_stack->blocked_vm)
7211 {
7212 label = c_add_case_label (c_switch_stack->cases,
7213 SWITCH_COND (c_switch_stack->switch_expr),
7214 c_switch_stack->orig_type,
7215 low_value, high_value);
7216 if (label == error_mark_node)
7217 label = NULL_TREE;
7218 }
7219 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7220 {
7221 if (low_value)
7222 error ("case label in statement expression not containing "
7223 "enclosing switch statement");
7224 else
7225 error ("%<default%> label in statement expression not containing "
7226 "enclosing switch statement");
7227 }
7228 else if (c_switch_stack && c_switch_stack->blocked_vm)
7229 {
7230 if (low_value)
7231 error ("case label in scope of identifier with variably modified "
7232 "type not containing enclosing switch statement");
7233 else
7234 error ("%<default%> label in scope of identifier with variably "
7235 "modified type not containing enclosing switch statement");
7236 }
7237 else if (low_value)
7238 error ("case label not within a switch statement");
7239 else
7240 error ("%<default%> label not within a switch statement");
7241
7242 return label;
7243 }
7244
7245 /* Finish the switch statement. */
7246
7247 void
c_finish_case(tree body)7248 c_finish_case (tree body)
7249 {
7250 struct c_switch *cs = c_switch_stack;
7251 location_t switch_location;
7252
7253 SWITCH_BODY (cs->switch_expr) = body;
7254
7255 /* We must not be within a statement expression nested in the switch
7256 at this point; we might, however, be within the scope of an
7257 identifier with variably modified type nested in the switch. */
7258 gcc_assert (!cs->blocked_stmt_expr);
7259
7260 /* Emit warnings as needed. */
7261 if (EXPR_HAS_LOCATION (cs->switch_expr))
7262 switch_location = EXPR_LOCATION (cs->switch_expr);
7263 else
7264 switch_location = input_location;
7265 c_do_switch_warnings (cs->cases, switch_location,
7266 TREE_TYPE (cs->switch_expr),
7267 SWITCH_COND (cs->switch_expr));
7268
7269 /* Pop the stack. */
7270 c_switch_stack = cs->next;
7271 splay_tree_delete (cs->cases);
7272 XDELETE (cs);
7273 }
7274
7275 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7276 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7277 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7278 statement, and was not surrounded with parenthesis. */
7279
7280 void
c_finish_if_stmt(location_t if_locus,tree cond,tree then_block,tree else_block,bool nested_if)7281 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7282 tree else_block, bool nested_if)
7283 {
7284 tree stmt;
7285
7286 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7287 if (warn_parentheses && nested_if && else_block == NULL)
7288 {
7289 tree inner_if = then_block;
7290
7291 /* We know from the grammar productions that there is an IF nested
7292 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7293 it might not be exactly THEN_BLOCK, but should be the last
7294 non-container statement within. */
7295 while (1)
7296 switch (TREE_CODE (inner_if))
7297 {
7298 case COND_EXPR:
7299 goto found;
7300 case BIND_EXPR:
7301 inner_if = BIND_EXPR_BODY (inner_if);
7302 break;
7303 case STATEMENT_LIST:
7304 inner_if = expr_last (then_block);
7305 break;
7306 case TRY_FINALLY_EXPR:
7307 case TRY_CATCH_EXPR:
7308 inner_if = TREE_OPERAND (inner_if, 0);
7309 break;
7310 default:
7311 gcc_unreachable ();
7312 }
7313 found:
7314
7315 if (COND_EXPR_ELSE (inner_if))
7316 warning (OPT_Wparentheses,
7317 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7318 &if_locus);
7319 }
7320
7321 /* Diagnose ";" via the special empty statement node that we create. */
7322 if (extra_warnings)
7323 {
7324 tree *inner_then = &then_block, *inner_else = &else_block;
7325
7326 if (TREE_CODE (*inner_then) == STATEMENT_LIST
7327 && STATEMENT_LIST_TAIL (*inner_then))
7328 inner_then = &STATEMENT_LIST_TAIL (*inner_then)->stmt;
7329 if (*inner_else && TREE_CODE (*inner_else) == STATEMENT_LIST
7330 && STATEMENT_LIST_TAIL (*inner_else))
7331 inner_else = &STATEMENT_LIST_TAIL (*inner_else)->stmt;
7332
7333 if (TREE_CODE (*inner_then) == NOP_EXPR && !TREE_TYPE (*inner_then))
7334 {
7335 if (!*inner_else)
7336 warning (0, "%Hempty body in an if-statement",
7337 EXPR_LOCUS (*inner_then));
7338
7339 *inner_then = alloc_stmt_list ();
7340 }
7341 if (*inner_else
7342 && TREE_CODE (*inner_else) == NOP_EXPR
7343 && !TREE_TYPE (*inner_else))
7344 {
7345 warning (0, "%Hempty body in an else-statement",
7346 EXPR_LOCUS (*inner_else));
7347
7348 *inner_else = alloc_stmt_list ();
7349 }
7350 }
7351
7352 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7353 SET_EXPR_LOCATION (stmt, if_locus);
7354 add_stmt (stmt);
7355 }
7356
7357 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7358 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7359 is false for DO loops. INCR is the FOR increment expression. BODY is
7360 the statement controlled by the loop. BLAB is the break label. CLAB is
7361 the continue label. Everything is allowed to be NULL. */
7362
7363 void
c_finish_loop(location_t start_locus,tree cond,tree incr,tree body,tree blab,tree clab,bool cond_is_first)7364 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7365 tree blab, tree clab, bool cond_is_first)
7366 {
7367 tree entry = NULL, exit = NULL, t;
7368
7369 /* If the condition is zero don't generate a loop construct. */
7370 if (cond && integer_zerop (cond))
7371 {
7372 if (cond_is_first)
7373 {
7374 t = build_and_jump (&blab);
7375 SET_EXPR_LOCATION (t, start_locus);
7376 add_stmt (t);
7377 }
7378 }
7379 else
7380 {
7381 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7382
7383 /* If we have an exit condition, then we build an IF with gotos either
7384 out of the loop, or to the top of it. If there's no exit condition,
7385 then we just build a jump back to the top. */
7386 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7387
7388 if (cond && !integer_nonzerop (cond))
7389 {
7390 /* Canonicalize the loop condition to the end. This means
7391 generating a branch to the loop condition. Reuse the
7392 continue label, if possible. */
7393 if (cond_is_first)
7394 {
7395 if (incr || !clab)
7396 {
7397 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7398 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7399 }
7400 else
7401 t = build1 (GOTO_EXPR, void_type_node, clab);
7402 SET_EXPR_LOCATION (t, start_locus);
7403 add_stmt (t);
7404 }
7405
7406 t = build_and_jump (&blab);
7407 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7408 if (cond_is_first)
7409 SET_EXPR_LOCATION (exit, start_locus);
7410 else
7411 SET_EXPR_LOCATION (exit, input_location);
7412 }
7413
7414 add_stmt (top);
7415 }
7416
7417 if (body)
7418 add_stmt (body);
7419 if (clab)
7420 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7421 if (incr)
7422 add_stmt (incr);
7423 if (entry)
7424 add_stmt (entry);
7425 if (exit)
7426 add_stmt (exit);
7427 if (blab)
7428 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7429 }
7430
7431 tree
c_finish_bc_stmt(tree * label_p,bool is_break)7432 c_finish_bc_stmt (tree *label_p, bool is_break)
7433 {
7434 bool skip;
7435 tree label = *label_p;
7436
7437 /* In switch statements break is sometimes stylistically used after
7438 a return statement. This can lead to spurious warnings about
7439 control reaching the end of a non-void function when it is
7440 inlined. Note that we are calling block_may_fallthru with
7441 language specific tree nodes; this works because
7442 block_may_fallthru returns true when given something it does not
7443 understand. */
7444 skip = !block_may_fallthru (cur_stmt_list);
7445
7446 if (!label)
7447 {
7448 if (!skip)
7449 *label_p = label = create_artificial_label ();
7450 }
7451 else if (TREE_CODE (label) != LABEL_DECL)
7452 {
7453 if (is_break)
7454 error ("break statement not within loop or switch");
7455 else
7456 error ("continue statement not within a loop");
7457 return NULL_TREE;
7458 }
7459
7460 if (skip)
7461 return NULL_TREE;
7462
7463 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7464 }
7465
7466 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7467
7468 static void
emit_side_effect_warnings(tree expr)7469 emit_side_effect_warnings (tree expr)
7470 {
7471 if (expr == error_mark_node)
7472 ;
7473 else if (!TREE_SIDE_EFFECTS (expr))
7474 {
7475 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7476 warning (0, "%Hstatement with no effect",
7477 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7478 }
7479 else if (warn_unused_value)
7480 warn_if_unused_value (expr, input_location);
7481 }
7482
7483 /* Process an expression as if it were a complete statement. Emit
7484 diagnostics, but do not call ADD_STMT. */
7485
7486 tree
c_process_expr_stmt(tree expr)7487 c_process_expr_stmt (tree expr)
7488 {
7489 if (!expr)
7490 return NULL_TREE;
7491
7492 if (warn_sequence_point)
7493 verify_sequence_points (expr);
7494
7495 if (TREE_TYPE (expr) != error_mark_node
7496 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7497 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7498 error ("expression statement has incomplete type");
7499
7500 /* If we're not processing a statement expression, warn about unused values.
7501 Warnings for statement expressions will be emitted later, once we figure
7502 out which is the result. */
7503 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7504 && (extra_warnings || warn_unused_value))
7505 emit_side_effect_warnings (expr);
7506
7507 /* If the expression is not of a type to which we cannot assign a line
7508 number, wrap the thing in a no-op NOP_EXPR. */
7509 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7510 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7511
7512 if (EXPR_P (expr))
7513 SET_EXPR_LOCATION (expr, input_location);
7514
7515 return expr;
7516 }
7517
7518 /* Emit an expression as a statement. */
7519
7520 tree
c_finish_expr_stmt(tree expr)7521 c_finish_expr_stmt (tree expr)
7522 {
7523 if (expr)
7524 return add_stmt (c_process_expr_stmt (expr));
7525 else
7526 return NULL;
7527 }
7528
7529 /* Do the opposite and emit a statement as an expression. To begin,
7530 create a new binding level and return it. */
7531
7532 tree
c_begin_stmt_expr(void)7533 c_begin_stmt_expr (void)
7534 {
7535 tree ret;
7536 struct c_label_context_se *nstack;
7537 struct c_label_list *glist;
7538
7539 /* We must force a BLOCK for this level so that, if it is not expanded
7540 later, there is a way to turn off the entire subtree of blocks that
7541 are contained in it. */
7542 keep_next_level ();
7543 ret = c_begin_compound_stmt (true);
7544 if (c_switch_stack)
7545 {
7546 c_switch_stack->blocked_stmt_expr++;
7547 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7548 }
7549 for (glist = label_context_stack_se->labels_used;
7550 glist != NULL;
7551 glist = glist->next)
7552 {
7553 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7554 }
7555 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7556 nstack->labels_def = NULL;
7557 nstack->labels_used = NULL;
7558 nstack->next = label_context_stack_se;
7559 label_context_stack_se = nstack;
7560
7561 /* Mark the current statement list as belonging to a statement list. */
7562 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7563
7564 return ret;
7565 }
7566
7567 tree
c_finish_stmt_expr(tree body)7568 c_finish_stmt_expr (tree body)
7569 {
7570 tree last, type, tmp, val;
7571 tree *last_p;
7572 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7573
7574 body = c_end_compound_stmt (body, true);
7575 if (c_switch_stack)
7576 {
7577 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7578 c_switch_stack->blocked_stmt_expr--;
7579 }
7580 /* It is no longer possible to jump to labels defined within this
7581 statement expression. */
7582 for (dlist = label_context_stack_se->labels_def;
7583 dlist != NULL;
7584 dlist = dlist->next)
7585 {
7586 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7587 }
7588 /* It is again possible to define labels with a goto just outside
7589 this statement expression. */
7590 for (glist = label_context_stack_se->next->labels_used;
7591 glist != NULL;
7592 glist = glist->next)
7593 {
7594 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7595 glist_prev = glist;
7596 }
7597 if (glist_prev != NULL)
7598 glist_prev->next = label_context_stack_se->labels_used;
7599 else
7600 label_context_stack_se->next->labels_used
7601 = label_context_stack_se->labels_used;
7602 label_context_stack_se = label_context_stack_se->next;
7603
7604 /* Locate the last statement in BODY. See c_end_compound_stmt
7605 about always returning a BIND_EXPR. */
7606 last_p = &BIND_EXPR_BODY (body);
7607 last = BIND_EXPR_BODY (body);
7608
7609 continue_searching:
7610 if (TREE_CODE (last) == STATEMENT_LIST)
7611 {
7612 tree_stmt_iterator i;
7613
7614 /* This can happen with degenerate cases like ({ }). No value. */
7615 if (!TREE_SIDE_EFFECTS (last))
7616 return body;
7617
7618 /* If we're supposed to generate side effects warnings, process
7619 all of the statements except the last. */
7620 if (extra_warnings || warn_unused_value)
7621 {
7622 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7623 emit_side_effect_warnings (tsi_stmt (i));
7624 }
7625 else
7626 i = tsi_last (last);
7627 last_p = tsi_stmt_ptr (i);
7628 last = *last_p;
7629 }
7630
7631 /* If the end of the list is exception related, then the list was split
7632 by a call to push_cleanup. Continue searching. */
7633 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7634 || TREE_CODE (last) == TRY_CATCH_EXPR)
7635 {
7636 last_p = &TREE_OPERAND (last, 0);
7637 last = *last_p;
7638 goto continue_searching;
7639 }
7640
7641 /* In the case that the BIND_EXPR is not necessary, return the
7642 expression out from inside it. */
7643 if (last == error_mark_node
7644 || (last == BIND_EXPR_BODY (body)
7645 && BIND_EXPR_VARS (body) == NULL))
7646 {
7647 /* Do not warn if the return value of a statement expression is
7648 unused. */
7649 if (EXPR_P (last))
7650 TREE_NO_WARNING (last) = 1;
7651 return last;
7652 }
7653
7654 /* Extract the type of said expression. */
7655 type = TREE_TYPE (last);
7656
7657 /* If we're not returning a value at all, then the BIND_EXPR that
7658 we already have is a fine expression to return. */
7659 if (!type || VOID_TYPE_P (type))
7660 return body;
7661
7662 /* Now that we've located the expression containing the value, it seems
7663 silly to make voidify_wrapper_expr repeat the process. Create a
7664 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7665 tmp = create_tmp_var_raw (type, NULL);
7666
7667 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7668 tree_expr_nonnegative_p giving up immediately. */
7669 val = last;
7670 if (TREE_CODE (val) == NOP_EXPR
7671 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7672 val = TREE_OPERAND (val, 0);
7673
7674 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7675 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7676
7677 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7678 }
7679
7680 /* Begin the scope of an identifier of variably modified type, scope
7681 number SCOPE. Jumping from outside this scope to inside it is not
7682 permitted. */
7683
7684 void
c_begin_vm_scope(unsigned int scope)7685 c_begin_vm_scope (unsigned int scope)
7686 {
7687 struct c_label_context_vm *nstack;
7688 struct c_label_list *glist;
7689
7690 gcc_assert (scope > 0);
7691 if (c_switch_stack && !c_switch_stack->blocked_vm)
7692 c_switch_stack->blocked_vm = scope;
7693 for (glist = label_context_stack_vm->labels_used;
7694 glist != NULL;
7695 glist = glist->next)
7696 {
7697 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7698 }
7699 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7700 nstack->labels_def = NULL;
7701 nstack->labels_used = NULL;
7702 nstack->scope = scope;
7703 nstack->next = label_context_stack_vm;
7704 label_context_stack_vm = nstack;
7705 }
7706
7707 /* End a scope which may contain identifiers of variably modified
7708 type, scope number SCOPE. */
7709
7710 void
c_end_vm_scope(unsigned int scope)7711 c_end_vm_scope (unsigned int scope)
7712 {
7713 if (label_context_stack_vm == NULL)
7714 return;
7715 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7716 c_switch_stack->blocked_vm = 0;
7717 /* We may have a number of nested scopes of identifiers with
7718 variably modified type, all at this depth. Pop each in turn. */
7719 while (label_context_stack_vm->scope == scope)
7720 {
7721 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7722
7723 /* It is no longer possible to jump to labels defined within this
7724 scope. */
7725 for (dlist = label_context_stack_vm->labels_def;
7726 dlist != NULL;
7727 dlist = dlist->next)
7728 {
7729 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7730 }
7731 /* It is again possible to define labels with a goto just outside
7732 this scope. */
7733 for (glist = label_context_stack_vm->next->labels_used;
7734 glist != NULL;
7735 glist = glist->next)
7736 {
7737 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7738 glist_prev = glist;
7739 }
7740 if (glist_prev != NULL)
7741 glist_prev->next = label_context_stack_vm->labels_used;
7742 else
7743 label_context_stack_vm->next->labels_used
7744 = label_context_stack_vm->labels_used;
7745 label_context_stack_vm = label_context_stack_vm->next;
7746 }
7747 }
7748
7749 /* Begin and end compound statements. This is as simple as pushing
7750 and popping new statement lists from the tree. */
7751
7752 tree
c_begin_compound_stmt(bool do_scope)7753 c_begin_compound_stmt (bool do_scope)
7754 {
7755 tree stmt = push_stmt_list ();
7756 if (do_scope)
7757 push_scope ();
7758 return stmt;
7759 }
7760
7761 tree
c_end_compound_stmt(tree stmt,bool do_scope)7762 c_end_compound_stmt (tree stmt, bool do_scope)
7763 {
7764 tree block = NULL;
7765
7766 if (do_scope)
7767 {
7768 if (c_dialect_objc ())
7769 objc_clear_super_receiver ();
7770 block = pop_scope ();
7771 }
7772
7773 stmt = pop_stmt_list (stmt);
7774 stmt = c_build_bind_expr (block, stmt);
7775
7776 /* If this compound statement is nested immediately inside a statement
7777 expression, then force a BIND_EXPR to be created. Otherwise we'll
7778 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7779 STATEMENT_LISTs merge, and thus we can lose track of what statement
7780 was really last. */
7781 if (cur_stmt_list
7782 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7783 && TREE_CODE (stmt) != BIND_EXPR)
7784 {
7785 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7786 TREE_SIDE_EFFECTS (stmt) = 1;
7787 }
7788
7789 return stmt;
7790 }
7791
7792 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7793 when the current scope is exited. EH_ONLY is true when this is not
7794 meant to apply to normal control flow transfer. */
7795
7796 void
push_cleanup(tree ARG_UNUSED (decl),tree cleanup,bool eh_only)7797 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7798 {
7799 enum tree_code code;
7800 tree stmt, list;
7801 bool stmt_expr;
7802
7803 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7804 stmt = build_stmt (code, NULL, cleanup);
7805 add_stmt (stmt);
7806 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7807 list = push_stmt_list ();
7808 TREE_OPERAND (stmt, 0) = list;
7809 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7810 }
7811
7812 /* Build a binary-operation expression without default conversions.
7813 CODE is the kind of expression to build.
7814 This function differs from `build' in several ways:
7815 the data type of the result is computed and recorded in it,
7816 warnings are generated if arg data types are invalid,
7817 special handling for addition and subtraction of pointers is known,
7818 and some optimization is done (operations on narrow ints
7819 are done in the narrower type when that gives the same result).
7820 Constant folding is also done before the result is returned.
7821
7822 Note that the operands will never have enumeral types, or function
7823 or array types, because either they will have the default conversions
7824 performed or they have both just been converted to some other type in which
7825 the arithmetic is to be done. */
7826
7827 tree
build_binary_op(enum tree_code code,tree orig_op0,tree orig_op1,int convert_p)7828 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7829 int convert_p)
7830 {
7831 tree type0, type1;
7832 enum tree_code code0, code1;
7833 tree op0, op1;
7834 const char *invalid_op_diag;
7835
7836 /* Expression code to give to the expression when it is built.
7837 Normally this is CODE, which is what the caller asked for,
7838 but in some special cases we change it. */
7839 enum tree_code resultcode = code;
7840
7841 /* Data type in which the computation is to be performed.
7842 In the simplest cases this is the common type of the arguments. */
7843 tree result_type = NULL;
7844
7845 /* Nonzero means operands have already been type-converted
7846 in whatever way is necessary.
7847 Zero means they need to be converted to RESULT_TYPE. */
7848 int converted = 0;
7849
7850 /* Nonzero means create the expression with this type, rather than
7851 RESULT_TYPE. */
7852 tree build_type = 0;
7853
7854 /* Nonzero means after finally constructing the expression
7855 convert it to this type. */
7856 tree final_type = 0;
7857
7858 /* Nonzero if this is an operation like MIN or MAX which can
7859 safely be computed in short if both args are promoted shorts.
7860 Also implies COMMON.
7861 -1 indicates a bitwise operation; this makes a difference
7862 in the exact conditions for when it is safe to do the operation
7863 in a narrower mode. */
7864 int shorten = 0;
7865
7866 /* Nonzero if this is a comparison operation;
7867 if both args are promoted shorts, compare the original shorts.
7868 Also implies COMMON. */
7869 int short_compare = 0;
7870
7871 /* Nonzero if this is a right-shift operation, which can be computed on the
7872 original short and then promoted if the operand is a promoted short. */
7873 int short_shift = 0;
7874
7875 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7876 int common = 0;
7877
7878 /* True means types are compatible as far as ObjC is concerned. */
7879 bool objc_ok;
7880
7881 if (convert_p)
7882 {
7883 op0 = default_conversion (orig_op0);
7884 op1 = default_conversion (orig_op1);
7885 }
7886 else
7887 {
7888 op0 = orig_op0;
7889 op1 = orig_op1;
7890 }
7891
7892 type0 = TREE_TYPE (op0);
7893 type1 = TREE_TYPE (op1);
7894
7895 /* The expression codes of the data types of the arguments tell us
7896 whether the arguments are integers, floating, pointers, etc. */
7897 code0 = TREE_CODE (type0);
7898 code1 = TREE_CODE (type1);
7899
7900 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7901 STRIP_TYPE_NOPS (op0);
7902 STRIP_TYPE_NOPS (op1);
7903
7904 /* If an error was already reported for one of the arguments,
7905 avoid reporting another error. */
7906
7907 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7908 return error_mark_node;
7909
7910 if ((invalid_op_diag
7911 = targetm.invalid_binary_op (code, type0, type1)))
7912 {
7913 error (invalid_op_diag);
7914 return error_mark_node;
7915 }
7916
7917 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
7918
7919 switch (code)
7920 {
7921 case PLUS_EXPR:
7922 /* Handle the pointer + int case. */
7923 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7924 return pointer_int_sum (PLUS_EXPR, op0, op1);
7925 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7926 return pointer_int_sum (PLUS_EXPR, op1, op0);
7927 else
7928 common = 1;
7929 break;
7930
7931 case MINUS_EXPR:
7932 /* Subtraction of two similar pointers.
7933 We must subtract them as integers, then divide by object size. */
7934 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7935 && comp_target_types (type0, type1))
7936 return pointer_diff (op0, op1);
7937 /* Handle pointer minus int. Just like pointer plus int. */
7938 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7939 return pointer_int_sum (MINUS_EXPR, op0, op1);
7940 else
7941 common = 1;
7942 break;
7943
7944 case MULT_EXPR:
7945 common = 1;
7946 break;
7947
7948 case TRUNC_DIV_EXPR:
7949 case CEIL_DIV_EXPR:
7950 case FLOOR_DIV_EXPR:
7951 case ROUND_DIV_EXPR:
7952 case EXACT_DIV_EXPR:
7953 /* Floating point division by zero is a legitimate way to obtain
7954 infinities and NaNs. */
7955 if (skip_evaluation == 0 && integer_zerop (op1))
7956 warning (OPT_Wdiv_by_zero, "division by zero");
7957
7958 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7959 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7960 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7961 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7962 {
7963 enum tree_code tcode0 = code0, tcode1 = code1;
7964
7965 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7966 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7967 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7968 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7969
7970 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
7971 resultcode = RDIV_EXPR;
7972 else
7973 /* Although it would be tempting to shorten always here, that
7974 loses on some targets, since the modulo instruction is
7975 undefined if the quotient can't be represented in the
7976 computation mode. We shorten only if unsigned or if
7977 dividing by something we know != -1. */
7978 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7979 || (TREE_CODE (op1) == INTEGER_CST
7980 && !integer_all_onesp (op1)));
7981 common = 1;
7982 }
7983 break;
7984
7985 case BIT_AND_EXPR:
7986 case BIT_IOR_EXPR:
7987 case BIT_XOR_EXPR:
7988 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7989 shorten = -1;
7990 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7991 common = 1;
7992 break;
7993
7994 case TRUNC_MOD_EXPR:
7995 case FLOOR_MOD_EXPR:
7996 if (skip_evaluation == 0 && integer_zerop (op1))
7997 warning (OPT_Wdiv_by_zero, "division by zero");
7998
7999 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8000 {
8001 /* Although it would be tempting to shorten always here, that loses
8002 on some targets, since the modulo instruction is undefined if the
8003 quotient can't be represented in the computation mode. We shorten
8004 only if unsigned or if dividing by something we know != -1. */
8005 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
8006 || (TREE_CODE (op1) == INTEGER_CST
8007 && !integer_all_onesp (op1)));
8008 common = 1;
8009 }
8010 break;
8011
8012 case TRUTH_ANDIF_EXPR:
8013 case TRUTH_ORIF_EXPR:
8014 case TRUTH_AND_EXPR:
8015 case TRUTH_OR_EXPR:
8016 case TRUTH_XOR_EXPR:
8017 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
8018 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
8019 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
8020 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
8021 {
8022 /* Result of these operations is always an int,
8023 but that does not mean the operands should be
8024 converted to ints! */
8025 result_type = integer_type_node;
8026 op0 = c_common_truthvalue_conversion (op0);
8027 op1 = c_common_truthvalue_conversion (op1);
8028 converted = 1;
8029 }
8030 break;
8031
8032 /* Shift operations: result has same type as first operand;
8033 always convert second operand to int.
8034 Also set SHORT_SHIFT if shifting rightward. */
8035
8036 case RSHIFT_EXPR:
8037 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8038 {
8039 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8040 {
8041 if (tree_int_cst_sgn (op1) < 0)
8042 warning (0, "right shift count is negative");
8043 else
8044 {
8045 if (!integer_zerop (op1))
8046 short_shift = 1;
8047
8048 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8049 warning (0, "right shift count >= width of type");
8050 }
8051 }
8052
8053 /* Use the type of the value to be shifted. */
8054 result_type = type0;
8055 /* Convert the shift-count to an integer, regardless of size
8056 of value being shifted. */
8057 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8058 op1 = convert (integer_type_node, op1);
8059 /* Avoid converting op1 to result_type later. */
8060 converted = 1;
8061 }
8062 break;
8063
8064 case LSHIFT_EXPR:
8065 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8066 {
8067 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
8068 {
8069 if (tree_int_cst_sgn (op1) < 0)
8070 warning (0, "left shift count is negative");
8071
8072 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
8073 warning (0, "left shift count >= width of type");
8074 }
8075
8076 /* Use the type of the value to be shifted. */
8077 result_type = type0;
8078 /* Convert the shift-count to an integer, regardless of size
8079 of value being shifted. */
8080 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
8081 op1 = convert (integer_type_node, op1);
8082 /* Avoid converting op1 to result_type later. */
8083 converted = 1;
8084 }
8085 break;
8086
8087 case EQ_EXPR:
8088 case NE_EXPR:
8089 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
8090 warning (OPT_Wfloat_equal,
8091 "comparing floating point with == or != is unsafe");
8092 /* Result of comparison is always int,
8093 but don't convert the args to int! */
8094 build_type = integer_type_node;
8095 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
8096 || code0 == COMPLEX_TYPE)
8097 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
8098 || code1 == COMPLEX_TYPE))
8099 short_compare = 1;
8100 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8101 {
8102 tree tt0 = TREE_TYPE (type0);
8103 tree tt1 = TREE_TYPE (type1);
8104 /* Anything compares with void *. void * compares with anything.
8105 Otherwise, the targets must be compatible
8106 and both must be object or both incomplete. */
8107 if (comp_target_types (type0, type1))
8108 result_type = common_pointer_type (type0, type1);
8109 else if (VOID_TYPE_P (tt0))
8110 {
8111 /* op0 != orig_op0 detects the case of something
8112 whose value is 0 but which isn't a valid null ptr const. */
8113 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
8114 && TREE_CODE (tt1) == FUNCTION_TYPE)
8115 pedwarn ("ISO C forbids comparison of %<void *%>"
8116 " with function pointer");
8117 }
8118 else if (VOID_TYPE_P (tt1))
8119 {
8120 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
8121 && TREE_CODE (tt0) == FUNCTION_TYPE)
8122 pedwarn ("ISO C forbids comparison of %<void *%>"
8123 " with function pointer");
8124 }
8125 else
8126 /* Avoid warning about the volatile ObjC EH puts on decls. */
8127 if (!objc_ok)
8128 pedwarn ("comparison of distinct pointer types lacks a cast");
8129
8130 if (result_type == NULL_TREE)
8131 result_type = ptr_type_node;
8132 }
8133 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
8134 && integer_zerop (op1))
8135 result_type = type0;
8136 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
8137 && integer_zerop (op0))
8138 result_type = type1;
8139 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8140 {
8141 result_type = type0;
8142 pedwarn ("comparison between pointer and integer");
8143 }
8144 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8145 {
8146 result_type = type1;
8147 pedwarn ("comparison between pointer and integer");
8148 }
8149 break;
8150
8151 case LE_EXPR:
8152 case GE_EXPR:
8153 case LT_EXPR:
8154 case GT_EXPR:
8155 build_type = integer_type_node;
8156 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
8157 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
8158 short_compare = 1;
8159 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
8160 {
8161 if (comp_target_types (type0, type1))
8162 {
8163 result_type = common_pointer_type (type0, type1);
8164 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
8165 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8166 pedwarn ("comparison of complete and incomplete pointers");
8167 else if (pedantic
8168 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
8169 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
8170 }
8171 else
8172 {
8173 result_type = ptr_type_node;
8174 pedwarn ("comparison of distinct pointer types lacks a cast");
8175 }
8176 }
8177 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
8178 && integer_zerop (op1))
8179 {
8180 result_type = type0;
8181 if (pedantic || extra_warnings)
8182 pedwarn ("ordered comparison of pointer with integer zero");
8183 }
8184 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
8185 && integer_zerop (op0))
8186 {
8187 result_type = type1;
8188 if (pedantic)
8189 pedwarn ("ordered comparison of pointer with integer zero");
8190 }
8191 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
8192 {
8193 result_type = type0;
8194 pedwarn ("comparison between pointer and integer");
8195 }
8196 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8197 {
8198 result_type = type1;
8199 pedwarn ("comparison between pointer and integer");
8200 }
8201 break;
8202
8203 default:
8204 gcc_unreachable ();
8205 }
8206
8207 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8208 return error_mark_node;
8209
8210 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8211 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8212 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8213 TREE_TYPE (type1))))
8214 {
8215 binary_op_error (code);
8216 return error_mark_node;
8217 }
8218
8219 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8220 || code0 == VECTOR_TYPE)
8221 &&
8222 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8223 || code1 == VECTOR_TYPE))
8224 {
8225 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8226
8227 if (shorten || common || short_compare)
8228 result_type = c_common_type (type0, type1);
8229
8230 /* For certain operations (which identify themselves by shorten != 0)
8231 if both args were extended from the same smaller type,
8232 do the arithmetic in that type and then extend.
8233
8234 shorten !=0 and !=1 indicates a bitwise operation.
8235 For them, this optimization is safe only if
8236 both args are zero-extended or both are sign-extended.
8237 Otherwise, we might change the result.
8238 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8239 but calculated in (unsigned short) it would be (unsigned short)-1. */
8240
8241 if (shorten && none_complex)
8242 {
8243 int unsigned0, unsigned1;
8244 tree arg0 = get_narrower (op0, &unsigned0);
8245 tree arg1 = get_narrower (op1, &unsigned1);
8246 /* UNS is 1 if the operation to be done is an unsigned one. */
8247 int uns = TYPE_UNSIGNED (result_type);
8248 tree type;
8249
8250 final_type = result_type;
8251
8252 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8253 but it *requires* conversion to FINAL_TYPE. */
8254
8255 if ((TYPE_PRECISION (TREE_TYPE (op0))
8256 == TYPE_PRECISION (TREE_TYPE (arg0)))
8257 && TREE_TYPE (op0) != final_type)
8258 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
8259 if ((TYPE_PRECISION (TREE_TYPE (op1))
8260 == TYPE_PRECISION (TREE_TYPE (arg1)))
8261 && TREE_TYPE (op1) != final_type)
8262 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
8263
8264 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
8265
8266 /* For bitwise operations, signedness of nominal type
8267 does not matter. Consider only how operands were extended. */
8268 if (shorten == -1)
8269 uns = unsigned0;
8270
8271 /* Note that in all three cases below we refrain from optimizing
8272 an unsigned operation on sign-extended args.
8273 That would not be valid. */
8274
8275 /* Both args variable: if both extended in same way
8276 from same width, do it in that width.
8277 Do it unsigned if args were zero-extended. */
8278 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8279 < TYPE_PRECISION (result_type))
8280 && (TYPE_PRECISION (TREE_TYPE (arg1))
8281 == TYPE_PRECISION (TREE_TYPE (arg0)))
8282 && unsigned0 == unsigned1
8283 && (unsigned0 || !uns))
8284 result_type
8285 = c_common_signed_or_unsigned_type
8286 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
8287 else if (TREE_CODE (arg0) == INTEGER_CST
8288 && (unsigned1 || !uns)
8289 && (TYPE_PRECISION (TREE_TYPE (arg1))
8290 < TYPE_PRECISION (result_type))
8291 && (type
8292 = c_common_signed_or_unsigned_type (unsigned1,
8293 TREE_TYPE (arg1)),
8294 int_fits_type_p (arg0, type)))
8295 result_type = type;
8296 else if (TREE_CODE (arg1) == INTEGER_CST
8297 && (unsigned0 || !uns)
8298 && (TYPE_PRECISION (TREE_TYPE (arg0))
8299 < TYPE_PRECISION (result_type))
8300 && (type
8301 = c_common_signed_or_unsigned_type (unsigned0,
8302 TREE_TYPE (arg0)),
8303 int_fits_type_p (arg1, type)))
8304 result_type = type;
8305 }
8306
8307 /* Shifts can be shortened if shifting right. */
8308
8309 if (short_shift)
8310 {
8311 int unsigned_arg;
8312 tree arg0 = get_narrower (op0, &unsigned_arg);
8313
8314 final_type = result_type;
8315
8316 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8317 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8318
8319 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8320 /* We can shorten only if the shift count is less than the
8321 number of bits in the smaller type size. */
8322 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8323 /* We cannot drop an unsigned shift after sign-extension. */
8324 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8325 {
8326 /* Do an unsigned shift if the operand was zero-extended. */
8327 result_type
8328 = c_common_signed_or_unsigned_type (unsigned_arg,
8329 TREE_TYPE (arg0));
8330 /* Convert value-to-be-shifted to that type. */
8331 if (TREE_TYPE (op0) != result_type)
8332 op0 = convert (result_type, op0);
8333 converted = 1;
8334 }
8335 }
8336
8337 /* Comparison operations are shortened too but differently.
8338 They identify themselves by setting short_compare = 1. */
8339
8340 if (short_compare)
8341 {
8342 /* Don't write &op0, etc., because that would prevent op0
8343 from being kept in a register.
8344 Instead, make copies of the our local variables and
8345 pass the copies by reference, then copy them back afterward. */
8346 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8347 enum tree_code xresultcode = resultcode;
8348 tree val
8349 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8350
8351 if (val != 0)
8352 return val;
8353
8354 op0 = xop0, op1 = xop1;
8355 converted = 1;
8356 resultcode = xresultcode;
8357
8358 if (warn_sign_compare && skip_evaluation == 0)
8359 {
8360 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8361 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8362 int unsignedp0, unsignedp1;
8363 tree primop0 = get_narrower (op0, &unsignedp0);
8364 tree primop1 = get_narrower (op1, &unsignedp1);
8365
8366 xop0 = orig_op0;
8367 xop1 = orig_op1;
8368 STRIP_TYPE_NOPS (xop0);
8369 STRIP_TYPE_NOPS (xop1);
8370
8371 /* Give warnings for comparisons between signed and unsigned
8372 quantities that may fail.
8373
8374 Do the checking based on the original operand trees, so that
8375 casts will be considered, but default promotions won't be.
8376
8377 Do not warn if the comparison is being done in a signed type,
8378 since the signed type will only be chosen if it can represent
8379 all the values of the unsigned type. */
8380 if (!TYPE_UNSIGNED (result_type))
8381 /* OK */;
8382 /* Do not warn if both operands are the same signedness. */
8383 else if (op0_signed == op1_signed)
8384 /* OK */;
8385 else
8386 {
8387 tree sop, uop;
8388
8389 if (op0_signed)
8390 sop = xop0, uop = xop1;
8391 else
8392 sop = xop1, uop = xop0;
8393
8394 /* Do not warn if the signed quantity is an
8395 unsuffixed integer literal (or some static
8396 constant expression involving such literals or a
8397 conditional expression involving such literals)
8398 and it is non-negative. */
8399 if (tree_expr_nonnegative_p (sop))
8400 /* OK */;
8401 /* Do not warn if the comparison is an equality operation,
8402 the unsigned quantity is an integral constant, and it
8403 would fit in the result if the result were signed. */
8404 else if (TREE_CODE (uop) == INTEGER_CST
8405 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8406 && int_fits_type_p
8407 (uop, c_common_signed_type (result_type)))
8408 /* OK */;
8409 /* Do not warn if the unsigned quantity is an enumeration
8410 constant and its maximum value would fit in the result
8411 if the result were signed. */
8412 else if (TREE_CODE (uop) == INTEGER_CST
8413 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8414 && int_fits_type_p
8415 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8416 c_common_signed_type (result_type)))
8417 /* OK */;
8418 else
8419 warning (0, "comparison between signed and unsigned");
8420 }
8421
8422 /* Warn if two unsigned values are being compared in a size
8423 larger than their original size, and one (and only one) is the
8424 result of a `~' operator. This comparison will always fail.
8425
8426 Also warn if one operand is a constant, and the constant
8427 does not have all bits set that are set in the ~ operand
8428 when it is extended. */
8429
8430 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8431 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8432 {
8433 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8434 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8435 &unsignedp0);
8436 else
8437 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8438 &unsignedp1);
8439
8440 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8441 {
8442 tree primop;
8443 HOST_WIDE_INT constant, mask;
8444 int unsignedp, bits;
8445
8446 if (host_integerp (primop0, 0))
8447 {
8448 primop = primop1;
8449 unsignedp = unsignedp1;
8450 constant = tree_low_cst (primop0, 0);
8451 }
8452 else
8453 {
8454 primop = primop0;
8455 unsignedp = unsignedp0;
8456 constant = tree_low_cst (primop1, 0);
8457 }
8458
8459 bits = TYPE_PRECISION (TREE_TYPE (primop));
8460 if (bits < TYPE_PRECISION (result_type)
8461 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8462 {
8463 mask = (~(HOST_WIDE_INT) 0) << bits;
8464 if ((mask & constant) != mask)
8465 warning (0, "comparison of promoted ~unsigned with constant");
8466 }
8467 }
8468 else if (unsignedp0 && unsignedp1
8469 && (TYPE_PRECISION (TREE_TYPE (primop0))
8470 < TYPE_PRECISION (result_type))
8471 && (TYPE_PRECISION (TREE_TYPE (primop1))
8472 < TYPE_PRECISION (result_type)))
8473 warning (0, "comparison of promoted ~unsigned with unsigned");
8474 }
8475 }
8476 }
8477 }
8478
8479 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8480 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8481 Then the expression will be built.
8482 It will be given type FINAL_TYPE if that is nonzero;
8483 otherwise, it will be given type RESULT_TYPE. */
8484
8485 if (!result_type)
8486 {
8487 binary_op_error (code);
8488 return error_mark_node;
8489 }
8490
8491 if (!converted)
8492 {
8493 if (TREE_TYPE (op0) != result_type)
8494 op0 = convert (result_type, op0);
8495 if (TREE_TYPE (op1) != result_type)
8496 op1 = convert (result_type, op1);
8497
8498 /* This can happen if one operand has a vector type, and the other
8499 has a different type. */
8500 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8501 return error_mark_node;
8502 }
8503
8504 if (build_type == NULL_TREE)
8505 build_type = result_type;
8506
8507 {
8508 /* Treat expressions in initializers specially as they can't trap. */
8509 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8510 build_type,
8511 op0, op1)
8512 : fold_build2 (resultcode, build_type,
8513 op0, op1);
8514
8515 if (final_type != 0)
8516 result = convert (final_type, result);
8517 return result;
8518 }
8519 }
8520
8521
8522 /* Convert EXPR to be a truth-value, validating its type for this
8523 purpose. */
8524
8525 tree
c_objc_common_truthvalue_conversion(tree expr)8526 c_objc_common_truthvalue_conversion (tree expr)
8527 {
8528 switch (TREE_CODE (TREE_TYPE (expr)))
8529 {
8530 case ARRAY_TYPE:
8531 error ("used array that cannot be converted to pointer where scalar is required");
8532 return error_mark_node;
8533
8534 case RECORD_TYPE:
8535 error ("used struct type value where scalar is required");
8536 return error_mark_node;
8537
8538 case UNION_TYPE:
8539 error ("used union type value where scalar is required");
8540 return error_mark_node;
8541
8542 case FUNCTION_TYPE:
8543 gcc_unreachable ();
8544
8545 default:
8546 break;
8547 }
8548
8549 /* ??? Should we also give an error for void and vectors rather than
8550 leaving those to give errors later? */
8551 return c_common_truthvalue_conversion (expr);
8552 }
8553
8554
8555 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8556 required. */
8557
8558 tree
c_expr_to_decl(tree expr,bool * tc ATTRIBUTE_UNUSED,bool * ti ATTRIBUTE_UNUSED,bool * se)8559 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8560 bool *ti ATTRIBUTE_UNUSED, bool *se)
8561 {
8562 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8563 {
8564 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8565 /* Executing a compound literal inside a function reinitializes
8566 it. */
8567 if (!TREE_STATIC (decl))
8568 *se = true;
8569 return decl;
8570 }
8571 else
8572 return expr;
8573 }
8574