1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization.
26 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
27 and to process initializations in declarations (since they work
28 like a strange sort of assignment). */
36 /* Nonzero if we've already printed a "partly bracketed initializer"
37 message within this initializer. */
38 static int partial_bracket_mentioned
= 0;
40 extern char *index ();
41 extern char *rindex ();
43 int mark_addressable ();
44 static tree
convert_for_assignment ();
45 static void warn_for_assignment ();
46 static int function_types_compatible_p ();
47 static int type_lists_compatible_p ();
48 int self_promoting_args_p ();
49 static int self_promoting_type_p ();
50 static int comp_target_types ();
51 static tree
pointer_int_sum ();
52 static tree
pointer_diff ();
53 static tree
convert_sequence ();
54 static tree
unary_complex_lvalue ();
55 static tree
process_init_constructor ();
56 static tree
convert_arguments ();
57 static char *get_spelling ();
59 static void pedantic_lvalue_warning ();
60 tree
truthvalue_conversion ();
61 void incomplete_type_error ();
62 void readonly_warning ();
63 static tree
internal_build_compound_expr ();
66 /* Do `exp = require_complete_type (exp);' to make sure exp
67 does not have an incomplete type. (That includes void types.) */
70 require_complete_type (value
)
73 tree type
= TREE_TYPE (value
);
75 /* First, detect a valid value with a complete type. */
76 if (TYPE_SIZE (type
) != 0
77 && type
!= void_type_node
)
80 incomplete_type_error (value
, type
);
81 return error_mark_node
;
84 /* Print an error message for invalid use of an incomplete type.
85 VALUE is the expression that was used (or 0 if that isn't known)
86 and TYPE is the type that was invalid. */
89 incomplete_type_error (value
, type
)
95 /* Avoid duplicate error message. */
96 if (TREE_CODE (type
) == ERROR_MARK
)
99 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
100 || TREE_CODE (value
) == PARM_DECL
))
101 error ("`%s' has an incomplete type",
102 IDENTIFIER_POINTER (DECL_NAME (value
)));
106 /* We must print an error message. Be clever about what it says. */
108 switch (TREE_CODE (type
))
111 errmsg
= "invalid use of undefined type `struct %s'";
115 errmsg
= "invalid use of undefined type `union %s'";
119 errmsg
= "invalid use of undefined type `enum %s'";
123 error ("invalid use of void expression");
127 if (TYPE_DOMAIN (type
))
129 type
= TREE_TYPE (type
);
132 error ("invalid use of array with unspecified bounds");
139 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
140 error (errmsg
, IDENTIFIER_POINTER (TYPE_NAME (type
)));
142 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
143 error ("invalid use of incomplete typedef `%s'",
144 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
))));
148 /* Return a variant of TYPE which has all the type qualifiers of LIKE
149 as well as those of TYPE. */
152 qualify_type (type
, like
)
155 int constflag
= TYPE_READONLY (type
) || TYPE_READONLY (like
);
156 int volflag
= TYPE_VOLATILE (type
) || TYPE_VOLATILE (like
);
157 return c_build_type_variant (type
, constflag
, volflag
);
160 /* Return the common type of two types.
161 We assume that comptypes has already been done and returned 1;
162 if that isn't so, this may crash. In particular, we assume that qualifiers
165 This is the type for the result of most arithmetic operations
166 if the operands have the given two types. */
172 register enum tree_code code1
;
173 register enum tree_code code2
;
175 /* Save time if the two types are the same. */
177 if (t1
== t2
) return t1
;
179 /* If one type is nonsense, use the other. */
180 if (t1
== error_mark_node
)
182 if (t2
== error_mark_node
)
185 /* Treat an enum type as the unsigned integer type of the same width. */
187 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
188 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
189 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
190 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
192 code1
= TREE_CODE (t1
);
193 code2
= TREE_CODE (t2
);
195 /* If one type is complex, form the common type
196 of the non-complex components,
197 then make that complex. */
198 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
200 tree subtype1
, subtype2
, subtype
;
201 if (code1
== COMPLEX_TYPE
)
202 subtype1
= TREE_TYPE (t1
);
205 if (code2
== COMPLEX_TYPE
)
206 subtype2
= TREE_TYPE (t2
);
209 subtype
= common_type (subtype1
, subtype2
);
210 return build_complex_type (subtype
);
217 /* If only one is real, use it as the result. */
219 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
222 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
225 /* Both real or both integers; use the one with greater precision. */
227 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
229 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
232 /* Same precision. Prefer longs to ints even when same size. */
234 if (t1
== long_unsigned_type_node
235 || t2
== long_unsigned_type_node
)
236 return long_unsigned_type_node
;
238 if (t1
== long_integer_type_node
239 || t2
== long_integer_type_node
)
241 /* But preserve unsignedness from the other type,
242 since long cannot hold all the values of an unsigned int. */
243 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
244 return long_unsigned_type_node
;
245 return long_integer_type_node
;
248 /* Otherwise prefer the unsigned one. */
250 if (TREE_UNSIGNED (t1
))
255 /* For two pointers, do this recursively on the target type,
256 and combine the qualifiers of the two types' targets. */
257 /* This code was turned off; I don't know why.
258 But ANSI C specifies doing this with the qualifiers.
259 So I turned it on again. */
261 tree target
= common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1
)),
262 TYPE_MAIN_VARIANT (TREE_TYPE (t2
)));
264 = TYPE_READONLY (TREE_TYPE (t1
)) || TYPE_READONLY (TREE_TYPE (t2
));
266 = TYPE_VOLATILE (TREE_TYPE (t1
)) || TYPE_VOLATILE (TREE_TYPE (t2
));
267 return build_pointer_type (c_build_type_variant (target
, constp
, volatilep
));
270 return build_pointer_type (common_type (TREE_TYPE (t1
), TREE_TYPE (t2
)));
275 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
276 /* Save space: see if the result is identical to one of the args. */
277 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
279 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
281 /* Merge the element types, and have a size if either arg has one. */
282 return build_array_type (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
286 /* Function types: prefer the one that specified arg types.
287 If both do, merge the arg types. Also merge the return types. */
289 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
290 tree p1
= TYPE_ARG_TYPES (t1
);
291 tree p2
= TYPE_ARG_TYPES (t2
);
296 /* Save space: see if the result is identical to one of the args. */
297 if (valtype
== TREE_TYPE (t1
) && ! TYPE_ARG_TYPES (t2
))
299 if (valtype
== TREE_TYPE (t2
) && ! TYPE_ARG_TYPES (t1
))
302 /* Simple way if one arg fails to specify argument types. */
303 if (TYPE_ARG_TYPES (t1
) == 0)
304 return build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
305 if (TYPE_ARG_TYPES (t2
) == 0)
306 return build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
308 /* If both args specify argument types, we must merge the two
309 lists, argument by argument. */
311 len
= list_length (p1
);
314 for (i
= 0; i
< len
; i
++)
315 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
320 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
322 /* A null type means arg type is not specified.
323 Take whatever the other function type has. */
324 if (TREE_VALUE (p1
) == 0)
326 TREE_VALUE (n
) = TREE_VALUE (p2
);
329 if (TREE_VALUE (p2
) == 0)
331 TREE_VALUE (n
) = TREE_VALUE (p1
);
335 /* Given wait (union {union wait *u; int *i} *)
336 and wait (union wait *),
337 prefer union wait * as type of parm. */
338 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
339 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
342 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
343 memb
; memb
= TREE_CHAIN (memb
))
344 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p2
)))
346 TREE_VALUE (n
) = TREE_VALUE (p2
);
348 pedwarn ("function types not truly compatible in ANSI C");
352 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
353 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
356 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
357 memb
; memb
= TREE_CHAIN (memb
))
358 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p1
)))
360 TREE_VALUE (n
) = TREE_VALUE (p1
);
362 pedwarn ("function types not truly compatible in ANSI C");
366 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
370 return build_function_type (valtype
, newargs
);
379 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
380 or various other operations. Return 2 if they are compatible
381 but a warning may be needed if you use them together. */
384 comptypes (type1
, type2
)
387 register tree t1
= type1
;
388 register tree t2
= type2
;
390 /* Suppress errors caused by previously reported errors. */
392 if (t1
== t2
|| TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
395 /* Treat an enum type as the unsigned integer type of the same width. */
397 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
398 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
399 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
400 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
405 /* Different classes of types can't be compatible. */
407 if (TREE_CODE (t1
) != TREE_CODE (t2
)) return 0;
409 /* Qualifiers must match. */
411 if (TYPE_READONLY (t1
) != TYPE_READONLY (t2
))
413 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
416 /* Allow for two different type nodes which have essentially the same
417 definition. Note that we already checked for equality of the type
418 type qualifiers (just above). */
420 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
423 switch (TREE_CODE (t1
))
426 return (TREE_TYPE (t1
) == TREE_TYPE (t2
)
427 ? 1 : comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
)));
430 return function_types_compatible_p (t1
, t2
);
434 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
436 tree d1
= TYPE_DOMAIN (t1
);
437 tree d2
= TYPE_DOMAIN (t2
);
439 /* Target types must match incl. qualifiers. */
440 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
441 && 0 == (val
= comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))))
444 /* Sizes must match unless one is missing or variable. */
445 if (d1
== 0 || d2
== 0 || d1
== d2
446 || TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
447 || TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
448 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
449 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
)
452 return (((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1
))
453 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2
)))
454 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1
))
455 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2
)))
456 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1
))
457 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2
)))
458 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1
))
459 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2
))))
464 if (maybe_objc_comptypes (t1
, t2
, 0) == 1)
470 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
471 ignoring their qualifiers. */
474 comp_target_types (ttl
, ttr
)
479 /* Give maybe_objc_comptypes a crack at letting these types through. */
480 if (val
= maybe_objc_comptypes (ttl
, ttr
, 1) >= 0)
483 val
= comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl
)),
484 TYPE_MAIN_VARIANT (TREE_TYPE (ttr
)));
486 if (val
== 2 && pedantic
)
487 pedwarn ("types are not quite compatible");
491 /* Subroutines of `comptypes'. */
493 /* Return 1 if two function types F1 and F2 are compatible.
494 If either type specifies no argument types,
495 the other must specify a fixed number of self-promoting arg types.
496 Otherwise, if one type specifies only the number of arguments,
497 the other must specify that number of self-promoting arg types.
498 Otherwise, the argument types must match. */
501 function_types_compatible_p (f1
, f2
)
505 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
509 if (!(TREE_TYPE (f1
) == TREE_TYPE (f2
)
510 || (val
= comptypes (TREE_TYPE (f1
), TREE_TYPE (f2
)))))
513 args1
= TYPE_ARG_TYPES (f1
);
514 args2
= TYPE_ARG_TYPES (f2
);
516 /* An unspecified parmlist matches any specified parmlist
517 whose argument types don't need default promotions. */
521 if (!self_promoting_args_p (args2
))
523 /* If one of these types comes from a non-prototype fn definition,
524 compare that with the other type's arglist.
525 If they don't match, ask for a warning (but no error). */
526 if (TYPE_ACTUAL_ARG_TYPES (f1
)
527 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
)))
533 if (!self_promoting_args_p (args1
))
535 if (TYPE_ACTUAL_ARG_TYPES (f2
)
536 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
)))
541 /* Both types have argument lists: compare them and propagate results. */
542 val1
= type_lists_compatible_p (args1
, args2
);
543 return val1
!= 1 ? val1
: val
;
546 /* Check two lists of types for compatibility,
547 returning 0 for incompatible, 1 for compatible,
548 or 2 for compatible with warning. */
551 type_lists_compatible_p (args1
, args2
)
554 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
560 if (args1
== 0 && args2
== 0)
562 /* If one list is shorter than the other,
563 they fail to match. */
564 if (args1
== 0 || args2
== 0)
566 /* A null pointer instead of a type
567 means there is supposed to be an argument
568 but nothing is specified about what type it has.
569 So match anything that self-promotes. */
570 if (TREE_VALUE (args1
) == 0)
572 if (! self_promoting_type_p (TREE_VALUE (args2
)))
575 else if (TREE_VALUE (args2
) == 0)
577 if (! self_promoting_type_p (TREE_VALUE (args1
)))
580 else if (! (newval
= comptypes (TREE_VALUE (args1
), TREE_VALUE (args2
))))
582 /* Allow wait (union {union wait *u; int *i} *)
583 and wait (union wait *) to be compatible. */
584 if (TREE_CODE (TREE_VALUE (args1
)) == UNION_TYPE
585 && TYPE_NAME (TREE_VALUE (args1
)) == 0
586 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1
))) == INTEGER_CST
587 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1
)),
588 TYPE_SIZE (TREE_VALUE (args2
))))
591 for (memb
= TYPE_FIELDS (TREE_VALUE (args1
));
592 memb
; memb
= TREE_CHAIN (memb
))
593 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args2
)))
598 else if (TREE_CODE (TREE_VALUE (args2
)) == UNION_TYPE
599 && TYPE_NAME (TREE_VALUE (args2
)) == 0
600 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2
))) == INTEGER_CST
601 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2
)),
602 TYPE_SIZE (TREE_VALUE (args1
))))
605 for (memb
= TYPE_FIELDS (TREE_VALUE (args2
));
606 memb
; memb
= TREE_CHAIN (memb
))
607 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args1
)))
616 /* comptypes said ok, but record if it said to warn. */
620 args1
= TREE_CHAIN (args1
);
621 args2
= TREE_CHAIN (args2
);
625 /* Return 1 if PARMS specifies a fixed number of parameters
626 and none of their types is affected by default promotions. */
629 self_promoting_args_p (parms
)
633 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
635 register tree type
= TREE_VALUE (t
);
637 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
643 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
646 if (C_PROMOTING_INTEGER_TYPE_P (type
))
652 /* Return 1 if TYPE is not affected by default promotions. */
655 self_promoting_type_p (type
)
658 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
661 if (C_PROMOTING_INTEGER_TYPE_P (type
))
667 /* Return an unsigned type the same as TYPE in other respects. */
673 tree type1
= TYPE_MAIN_VARIANT (type
);
674 if (type1
== signed_char_type_node
|| type1
== char_type_node
)
675 return unsigned_char_type_node
;
676 if (type1
== integer_type_node
)
677 return unsigned_type_node
;
678 if (type1
== short_integer_type_node
)
679 return short_unsigned_type_node
;
680 if (type1
== long_integer_type_node
)
681 return long_unsigned_type_node
;
682 if (type1
== long_long_integer_type_node
)
683 return long_long_unsigned_type_node
;
687 /* Return a signed type the same as TYPE in other respects. */
693 tree type1
= TYPE_MAIN_VARIANT (type
);
694 if (type1
== unsigned_char_type_node
|| type1
== char_type_node
)
695 return signed_char_type_node
;
696 if (type1
== unsigned_type_node
)
697 return integer_type_node
;
698 if (type1
== short_unsigned_type_node
)
699 return short_integer_type_node
;
700 if (type1
== long_unsigned_type_node
)
701 return long_integer_type_node
;
702 if (type1
== long_long_unsigned_type_node
)
703 return long_long_integer_type_node
;
707 /* Return a type the same as TYPE except unsigned or
708 signed according to UNSIGNEDP. */
711 signed_or_unsigned_type (unsignedp
, type
)
715 if (TREE_CODE (type
) != INTEGER_TYPE
)
717 if (TYPE_PRECISION (type
) == TYPE_PRECISION (signed_char_type_node
))
718 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
719 if (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
720 return unsignedp
? unsigned_type_node
: integer_type_node
;
721 if (TYPE_PRECISION (type
) == TYPE_PRECISION (short_integer_type_node
))
722 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
723 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_integer_type_node
))
724 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
725 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_long_integer_type_node
))
726 return (unsignedp
? long_long_unsigned_type_node
727 : long_long_integer_type_node
);
731 /* Compute the value of the `sizeof' operator. */
737 enum tree_code code
= TREE_CODE (type
);
740 if (code
== FUNCTION_TYPE
)
742 if (pedantic
|| warn_pointer_arith
)
743 pedwarn ("sizeof applied to a function type");
746 if (code
== VOID_TYPE
)
748 if (pedantic
|| warn_pointer_arith
)
749 pedwarn ("sizeof applied to a void type");
752 if (code
== ERROR_MARK
)
754 if (TYPE_SIZE (type
) == 0)
756 error ("sizeof applied to an incomplete type");
760 /* Convert in case a char is more than one unit. */
761 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
762 size_int (TYPE_PRECISION (char_type_node
)));
763 /* size_binop does not put the constant in range, so do it now. */
764 if (TREE_CODE (t
) == INTEGER_CST
)
765 TREE_CONSTANT_OVERFLOW (t
) |= force_fit_type (t
, 0);
770 c_sizeof_nowarn (type
)
773 enum tree_code code
= TREE_CODE (type
);
776 if (code
== FUNCTION_TYPE
778 || code
== ERROR_MARK
)
780 if (TYPE_SIZE (type
) == 0)
783 /* Convert in case a char is more than one unit. */
784 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
785 size_int (TYPE_PRECISION (char_type_node
)));
786 force_fit_type (t
, 0);
790 /* Compute the size to increment a pointer by. */
793 c_size_in_bytes (type
)
796 enum tree_code code
= TREE_CODE (type
);
799 if (code
== FUNCTION_TYPE
)
801 if (code
== VOID_TYPE
)
803 if (code
== ERROR_MARK
)
805 if (TYPE_SIZE (type
) == 0)
807 error ("arithmetic on pointer to an incomplete type");
811 /* Convert in case a char is more than one unit. */
812 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
813 size_int (BITS_PER_UNIT
));
814 force_fit_type (t
, 0);
818 /* Implement the __alignof keyword: Return the minimum required
819 alignment of TYPE, measured in bytes. */
825 enum tree_code code
= TREE_CODE (type
);
827 if (code
== FUNCTION_TYPE
)
828 return size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
830 if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
833 return size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
836 /* Implement the __alignof keyword: Return the minimum required
837 alignment of EXPR, measured in bytes. For VAR_DECL's and
838 FIELD_DECL's return DECL_ALIGN (which can be set from an
839 "aligned" __attribute__ specification). */
842 c_alignof_expr (expr
)
845 if (TREE_CODE (expr
) == VAR_DECL
)
846 return size_int (DECL_ALIGN (expr
) / BITS_PER_UNIT
);
848 if (TREE_CODE (expr
) == COMPONENT_REF
849 && DECL_BIT_FIELD (TREE_OPERAND (expr
, 1)))
851 error ("`__alignof' applied to a bit-field");
854 else if (TREE_CODE (expr
) == COMPONENT_REF
855 && TREE_CODE (TREE_OPERAND (expr
, 1)) == FIELD_DECL
)
856 return size_int (DECL_ALIGN (TREE_OPERAND (expr
, 1)) / BITS_PER_UNIT
);
858 if (TREE_CODE (expr
) == INDIRECT_REF
)
860 tree t
= TREE_OPERAND (expr
, 0);
862 int bestalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
864 while (TREE_CODE (t
) == NOP_EXPR
865 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == POINTER_TYPE
)
869 t
= TREE_OPERAND (t
, 0);
870 thisalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
871 if (thisalign
> bestalign
)
872 best
= t
, bestalign
= thisalign
;
874 return c_alignof (TREE_TYPE (TREE_TYPE (best
)));
877 return c_alignof (TREE_TYPE (expr
));
879 /* Return either DECL or its known constant value (if it has one). */
882 decl_constant_value (decl
)
885 if (! TREE_PUBLIC (decl
)
886 /* Don't change a variable array bound or initial value to a constant
887 in a place where a variable is invalid. */
888 && current_function_decl
!= 0
890 && ! TREE_THIS_VOLATILE (decl
)
891 && DECL_INITIAL (decl
) != 0
892 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
893 /* This is invalid if initial value is not constant.
894 If it has either a function call, a memory reference,
895 or a variable, then re-evaluating it could give different results. */
896 && TREE_CONSTANT (DECL_INITIAL (decl
))
897 /* Check for cases where this is sub-optimal, even though valid. */
898 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
899 && DECL_MODE (decl
) != BLKmode
)
900 return DECL_INITIAL (decl
);
904 /* Perform default promotions for C data used in expressions.
905 Arrays and functions are converted to pointers;
906 enumeral types or short or char, to int.
907 In addition, manifest constants symbols are replaced by their values. */
910 default_conversion (exp
)
913 register tree type
= TREE_TYPE (exp
);
914 register enum tree_code code
= TREE_CODE (type
);
916 /* Constants can be used directly unless they're not loadable. */
917 if (TREE_CODE (exp
) == CONST_DECL
)
918 exp
= DECL_INITIAL (exp
);
919 /* Replace a nonvolatile const static variable with its value. */
921 && TREE_CODE (exp
) == VAR_DECL
922 && TREE_READONLY (exp
)
923 /* But not for iterators! */
925 && DECL_MODE (exp
) != BLKmode
)
927 exp
= decl_constant_value (exp
);
928 type
= TREE_TYPE (exp
);
931 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
933 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
934 to integer and cause infinite recursion. */
935 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
936 || (TREE_CODE (exp
) == NOP_EXPR
937 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
938 exp
= TREE_OPERAND (exp
, 0);
940 /* Normally convert enums to int,
941 but convert wide enums to something wider. */
942 if (code
== ENUMERAL_TYPE
)
944 type
= type_for_size (MAX (TYPE_PRECISION (type
),
945 TYPE_PRECISION (integer_type_node
)),
946 (flag_traditional
&& TREE_UNSIGNED (type
)));
947 return convert (type
, exp
);
950 if (C_PROMOTING_INTEGER_TYPE_P (type
))
952 /* Traditionally, unsignedness is preserved in default promotions.
953 Also preserve unsignedness if not really getting any wider. */
954 if (TREE_UNSIGNED (type
)
956 || TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
957 return convert (unsigned_type_node
, exp
);
958 return convert (integer_type_node
, exp
);
960 if (flag_traditional
&& TYPE_MAIN_VARIANT (type
) == float_type_node
)
961 return convert (double_type_node
, exp
);
962 if (code
== VOID_TYPE
)
964 error ("void value not ignored as it ought to be");
965 return error_mark_node
;
967 if (code
== FUNCTION_TYPE
)
969 return build_unary_op (ADDR_EXPR
, exp
, 0);
971 if (code
== ARRAY_TYPE
)
974 tree restype
= TREE_TYPE (type
);
977 if (TREE_CODE (exp
) == INDIRECT_REF
)
978 return convert (TYPE_POINTER_TO (restype
),
979 TREE_OPERAND (exp
, 0));
981 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
983 tree op1
= default_conversion (TREE_OPERAND (exp
, 1));
984 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
985 TREE_OPERAND (exp
, 0), op1
);
989 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
991 error ("invalid use of non-lvalue array");
992 return error_mark_node
;
995 if (TYPE_READONLY (type
) || TYPE_VOLATILE (type
))
996 restype
= c_build_type_variant (restype
, TYPE_READONLY (type
),
997 TYPE_VOLATILE (type
));
999 ptrtype
= build_pointer_type (restype
);
1001 if (TREE_CODE (exp
) == VAR_DECL
)
1003 /* ??? This is not really quite correct
1004 in that the type of the operand of ADDR_EXPR
1005 is not the target type of the type of the ADDR_EXPR itself.
1006 Question is, can this lossage be avoided? */
1007 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1008 if (mark_addressable (exp
) == 0)
1009 return error_mark_node
;
1010 TREE_CONSTANT (adr
) = staticp (exp
);
1011 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1014 /* This way is better for a COMPONENT_REF since it can
1015 simplify the offset for a component. */
1016 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1017 return convert (ptrtype
, adr
);
1022 /* Make an expression to refer to the COMPONENT field of
1023 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1026 build_component_ref (datum
, component
)
1027 tree datum
, component
;
1029 register tree type
= TREE_TYPE (datum
);
1030 register enum tree_code code
= TREE_CODE (type
);
1031 register tree field
= NULL
;
1034 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1035 unless we are not to support things not strictly ANSI. */
1036 switch (TREE_CODE (datum
))
1040 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
);
1041 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1042 TREE_OPERAND (datum
, 0), value
);
1045 return build_conditional_expr
1046 (TREE_OPERAND (datum
, 0),
1047 build_component_ref (TREE_OPERAND (datum
, 1), component
),
1048 build_component_ref (TREE_OPERAND (datum
, 2), component
));
1051 /* See if there is a field or component with name COMPONENT. */
1053 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1055 if (TYPE_SIZE (type
) == 0)
1057 incomplete_type_error (NULL_TREE
, type
);
1058 return error_mark_node
;
1061 /* Look up component name in the structure type definition.
1063 If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1064 to the field elements. Use a binary search on this array to quickly
1065 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1066 will always be set for structures which have many elements. */
1068 if (TYPE_LANG_SPECIFIC (type
))
1071 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->elts
[0];
1073 field
= TYPE_FIELDS (type
);
1075 top
= TYPE_LANG_SPECIFIC (type
)->len
;
1076 while (top
- bot
> 1)
1080 half
= (top
- bot
+ 1) >> 1;
1081 field
= field_array
[bot
+half
];
1082 cmp
= (long)DECL_NAME (field
) - (long)component
;
1091 if (DECL_NAME (field_array
[bot
]) == component
)
1092 field
= field_array
[bot
];
1093 else if (DECL_NAME (field
) != component
)
1098 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1100 if (DECL_NAME (field
) == component
)
1107 error (code
== RECORD_TYPE
1108 ? "structure has no member named `%s'"
1109 : "union has no member named `%s'",
1110 IDENTIFIER_POINTER (component
));
1111 return error_mark_node
;
1113 if (TREE_TYPE (field
) == error_mark_node
)
1114 return error_mark_node
;
1116 ref
= build (COMPONENT_REF
, TREE_TYPE (field
), datum
, field
);
1118 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
1119 TREE_READONLY (ref
) = 1;
1120 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
1121 TREE_THIS_VOLATILE (ref
) = 1;
1125 else if (code
!= ERROR_MARK
)
1126 error ("request for member `%s' in something not a structure or union",
1127 IDENTIFIER_POINTER (component
));
1129 return error_mark_node
;
1132 /* Given an expression PTR for a pointer, return an expression
1133 for the value pointed to.
1134 ERRORSTRING is the name of the operator to appear in error messages. */
1137 build_indirect_ref (ptr
, errorstring
)
1141 register tree pointer
= default_conversion (ptr
);
1142 register tree type
= TREE_TYPE (pointer
);
1144 if (TREE_CODE (type
) == POINTER_TYPE
)
1146 if (TREE_CODE (pointer
) == ADDR_EXPR
1148 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
1149 == TREE_TYPE (type
)))
1150 return TREE_OPERAND (pointer
, 0);
1153 tree t
= TREE_TYPE (type
);
1154 register tree ref
= build1 (INDIRECT_REF
,
1155 TYPE_MAIN_VARIANT (t
), pointer
);
1157 if (TYPE_SIZE (t
) == 0 && TREE_CODE (t
) != ARRAY_TYPE
)
1159 error ("dereferencing pointer to incomplete type");
1160 return error_mark_node
;
1162 if (TREE_CODE (t
) == VOID_TYPE
)
1163 warning ("dereferencing `void *' pointer");
1165 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1166 so that we get the proper error message if the result is used
1167 to assign to. Also, &* is supposed to be a no-op.
1168 And ANSI C seems to specify that the type of the result
1169 should be the const type. */
1170 /* A de-reference of a pointer to const is not a const. It is valid
1171 to change it via some other pointer. */
1172 TREE_READONLY (ref
) = TYPE_READONLY (t
);
1173 TREE_SIDE_EFFECTS (ref
)
1174 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
) || flag_volatile
;
1175 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
) || flag_volatile
;
1179 else if (TREE_CODE (pointer
) != ERROR_MARK
)
1180 error ("invalid type argument of `%s'", errorstring
);
1181 return error_mark_node
;
1184 /* This handles expressions of the form "a[i]", which denotes
1187 This is logically equivalent in C to *(a+i), but we may do it differently.
1188 If A is a variable or a member, we generate a primitive ARRAY_REF.
1189 This avoids forcing the array out of registers, and can work on
1190 arrays that are not lvalues (for example, members of structures returned
1194 build_array_ref (array
, index
)
1199 error ("subscript missing in array reference");
1200 return error_mark_node
;
1203 if (TREE_TYPE (array
) == error_mark_node
1204 || TREE_TYPE (index
) == error_mark_node
)
1205 return error_mark_node
;
1207 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
1208 && TREE_CODE (array
) != INDIRECT_REF
)
1212 /* Subscripting with type char is likely to lose
1213 on a machine where chars are signed.
1214 So warn on any machine, but optionally.
1215 Don't warn for unsigned char since that type is safe.
1216 Don't warn for signed char because anyone who uses that
1217 must have done so deliberately. */
1218 if (warn_char_subscripts
1219 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1220 warning ("array subscript has type `char'");
1222 /* Apply default promotions *after* noticing character types. */
1223 index
= default_conversion (index
);
1225 /* Require integer *after* promotion, for sake of enums. */
1226 if (TREE_CODE (TREE_TYPE (index
)) != INTEGER_TYPE
)
1228 error ("array subscript is not an integer");
1229 return error_mark_node
;
1232 /* An array that is indexed by a non-constant
1233 cannot be stored in a register; we must be able to do
1234 address arithmetic on its address.
1235 Likewise an array of elements of variable size. */
1236 if (TREE_CODE (index
) != INTEGER_CST
1237 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))) != 0
1238 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
1240 if (mark_addressable (array
) == 0)
1241 return error_mark_node
;
1244 if (pedantic
&& !lvalue_p (array
))
1246 if (DECL_REGISTER (array
))
1247 pedwarn ("ANSI C forbids subscripting `register' array");
1249 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1255 while (TREE_CODE (foo
) == COMPONENT_REF
)
1256 foo
= TREE_OPERAND (foo
, 0);
1257 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
1258 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1261 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
1262 rval
= build (ARRAY_REF
, type
, array
, index
);
1263 /* Array ref is const/volatile if the array elements are
1264 or if the array is. */
1265 TREE_READONLY (rval
)
1266 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
1267 | TREE_READONLY (array
));
1268 TREE_SIDE_EFFECTS (rval
)
1269 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1270 | TREE_SIDE_EFFECTS (array
));
1271 TREE_THIS_VOLATILE (rval
)
1272 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1273 /* This was added by rms on 16 Nov 91.
1274 It fixes vol struct foo *a; a->elts[1]
1275 in an inline function.
1276 Hope it doesn't break something else. */
1277 | TREE_THIS_VOLATILE (array
));
1278 return require_complete_type (fold (rval
));
1282 tree ar
= default_conversion (array
);
1283 tree ind
= default_conversion (index
);
1285 /* Put the integer in IND to simplify error checking. */
1286 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
1293 if (ar
== error_mark_node
)
1296 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
1298 error ("subscripted value is neither array nor pointer");
1299 return error_mark_node
;
1301 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
1303 error ("array subscript is not an integer");
1304 return error_mark_node
;
1307 return build_indirect_ref (build_binary_op (PLUS_EXPR
, ar
, ind
, 0),
1312 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against PARAMS. */
1314 #define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
1316 #define T_I &integer_type_node
1317 #define T_L &long_integer_type_node
1318 #define T_S &short_integer_type_node
1319 #define T_UI &unsigned_type_node
1320 #define T_UL &long_unsigned_type_node
1321 #define T_US &short_unsigned_type_node
1322 #define T_F &float_type_node
1323 #define T_D &double_type_node
1324 #define T_LD &long_double_type_node
1325 #define T_C &char_type_node
1326 #define T_V &void_type_node
1327 #define T_W &wchar_type_node
1333 /* Type of argument if no length modifier is used. */
1335 /* Type of argument if length modifier for shortening is used.
1336 If NULL, then this modifier is not allowed. */
1338 /* Type of argument if length modifier `l' is used.
1339 If NULL, then this modifier is not allowed. */
1341 /* Type of argument if length modifier `L' is used.
1342 If NULL, then this modifier is not allowed. */
1344 /* List of other modifier characters allowed with these options. */
1348 static format_char_info print_table
[]
1350 { "di", 0, T_I
, T_I
, T_L
, NULL
, "-wp0 +" },
1351 { "oxX", 0, T_UI
, T_UI
, T_UL
, NULL
, "-wp0#" },
1352 { "u", 0, T_UI
, T_UI
, T_UL
, NULL
, "-wp0" },
1353 { "feEgG", 0, T_D
, NULL
, NULL
, T_LD
, "-wp0 +#" },
1354 { "c", 0, T_I
, NULL
, T_W
, NULL
, "-w" },
1355 { "C", 0, T_W
, NULL
, NULL
, NULL
, "-w" },
1356 { "s", 1, T_C
, NULL
, T_W
, NULL
, "-wp" },
1357 { "S", 1, T_W
, NULL
, NULL
, NULL
, "-wp" },
1358 { "p", 1, T_V
, NULL
, NULL
, NULL
, "-" },
1359 { "n", 1, T_I
, T_S
, T_L
, NULL
, "" },
1363 static format_char_info scan_table
[]
1365 { "di", 1, T_I
, T_S
, T_L
, NULL
, "*" },
1366 { "ouxX", 1, T_UI
, T_US
, T_UL
, NULL
, "*" },
1367 { "efgEG", 1, T_F
, NULL
, T_D
, T_LD
, "*" },
1368 { "sc", 1, T_C
, NULL
, T_W
, NULL
, "*" },
1369 { "[", 1, T_C
, NULL
, NULL
, NULL
, "*" },
1370 { "C", 1, T_W
, NULL
, NULL
, NULL
, "*" },
1371 { "S", 1, T_W
, NULL
, NULL
, NULL
, "*" },
1372 { "p", 2, T_V
, NULL
, NULL
, NULL
, "*" },
1373 { "n", 1, T_I
, T_S
, T_L
, NULL
, "" },
1379 tree function_ident
; /* identifier such as "printf" */
1380 int is_scan
; /* TRUE if *scanf */
1381 int format_num
; /* number of format argument */
1382 int first_arg_num
; /* number of first arg (zero for varargs) */
1385 static unsigned int function_info_entries
= 0;
1386 static function_info
*function_info_table
= NULL
;
1388 /* Record information for argument format checking. FUNCTION_IDENT is
1389 the identifier node for the name of the function to check (its decl
1390 need not exist yet). IS_SCAN is true for scanf-type format checking;
1391 false indicates printf-style format checking. FORMAT_NUM is the number
1392 of the argument which is the format control string (starting from 1).
1393 FIRST_ARG_NUM is the number of the first actual argument to check
1394 against teh format string, or zero if no checking is not be done
1395 (e.g. for varargs such as vfprintf). */
1398 record_format_info (function_ident
, is_scan
, format_num
, first_arg_num
)
1399 tree function_ident
;
1404 function_info
*info
;
1406 function_info_entries
++;
1407 if (function_info_table
)
1409 = (function_info
*) xrealloc (function_info_table
,
1410 function_info_entries
* sizeof (function_info
));
1412 function_info_table
= (function_info
*) xmalloc (sizeof (function_info
));
1414 info
= &function_info_table
[function_info_entries
- 1];
1416 info
->function_ident
= function_ident
;
1417 info
->is_scan
= is_scan
;
1418 info
->format_num
= format_num
;
1419 info
->first_arg_num
= first_arg_num
;
1422 /* Initialize the table of functions to perform format checking on.
1423 The ANSI functions are always checked (whether <stdio.h> is
1424 included or not), since it is common to call printf without
1425 including <stdio.h>. There shouldn't be a problem with this,
1426 since ANSI reserves these function names whether you include the
1427 header file or not. In any case, the checking is harmless. */
1430 init_format_info_table ()
1432 record_format_info (get_identifier ("printf"), 0, 1, 2);
1433 record_format_info (get_identifier ("fprintf"), 0, 2, 3);
1434 record_format_info (get_identifier ("sprintf"), 0, 2, 3);
1435 record_format_info (get_identifier ("scanf"), 1, 1, 2);
1436 record_format_info (get_identifier ("fscanf"), 1, 2, 3);
1437 record_format_info (get_identifier ("sscanf"), 1, 2, 3);
1438 record_format_info (get_identifier ("vprintf"), 0, 1, 0);
1439 record_format_info (get_identifier ("vfprintf"), 0, 2, 0);
1440 record_format_info (get_identifier ("vsprintf"), 0, 2, 0);
1443 static char tfaff
[] = "too few arguments for format";
1445 /* Check the argument list of a call to printf, scanf, etc.
1446 INFO points to the element of function_info_table.
1447 PARAMS is the list of argument values. */
1450 check_format (info
, params
)
1451 function_info
*info
;
1456 int suppressed
, wide
, precise
;
1465 format_char_info
*fci
;
1466 static char message
[132];
1469 /* Skip to format argument. If the argument isn't available, there's
1470 no work for us to do; prototype checking will catch the problem. */
1471 for (arg_num
= 1; ; ++arg_num
)
1475 if (arg_num
== info
->format_num
)
1477 params
= TREE_CHAIN (params
);
1479 format_tree
= TREE_VALUE (params
);
1480 params
= TREE_CHAIN (params
);
1481 if (format_tree
== 0)
1483 /* We can only check the format if it's a string constant. */
1484 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1485 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
1486 if (format_tree
== null_pointer_node
)
1488 warning ("null format string");
1491 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1493 format_tree
= TREE_OPERAND (format_tree
, 0);
1494 if (TREE_CODE (format_tree
) != STRING_CST
)
1496 format_chars
= TREE_STRING_POINTER (format_tree
);
1497 format_length
= TREE_STRING_LENGTH (format_tree
);
1498 if (format_length
<= 1)
1499 warning ("zero-length format string");
1500 if (format_chars
[--format_length
] != 0)
1502 warning ("unterminated format string");
1505 /* Skip to first argument to check. */
1506 while (arg_num
+ 1 < info
->first_arg_num
)
1510 params
= TREE_CHAIN (params
);
1515 if (*format_chars
== 0)
1517 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
1518 warning ("embedded `\\0' in format");
1519 if (info
->first_arg_num
!= 0 && params
!= 0)
1520 warning ("too many arguments for format");
1523 if (*format_chars
++ != '%')
1525 if (*format_chars
== 0)
1527 warning ("spurious trailing `%%' in format");
1530 if (*format_chars
== '%')
1536 suppressed
= wide
= precise
= FALSE
;
1539 suppressed
= *format_chars
== '*';
1542 while (ISDIGIT (*format_chars
))
1547 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
1549 if (index (flag_chars
, *format_chars
) != 0)
1551 sprintf (message
, "repeated `%c' flag in format",
1555 i
= strlen (flag_chars
);
1556 flag_chars
[i
++] = *format_chars
++;
1559 /* "If the space and + flags both appear,
1560 the space flag will be ignored." */
1561 if (index (flag_chars
, ' ') != 0
1562 && index (flag_chars
, '+') != 0)
1563 warning ("use of both ` ' and `+' flags in format");
1564 /* "If the 0 and - flags both appear,
1565 the 0 flag will be ignored." */
1566 if (index (flag_chars
, '0') != 0
1567 && index (flag_chars
, '-') != 0)
1568 warning ("use of both `0' and `-' flags in format");
1569 if (*format_chars
== '*')
1572 /* "...a field width...may be indicated by an asterisk.
1573 In this case, an int argument supplies the field width..." */
1580 if (info
->first_arg_num
!= 0)
1582 cur_param
= TREE_VALUE (params
);
1583 params
= TREE_CHAIN (params
);
1585 /* size_t is generally not valid here.
1586 It will work on most machines, because size_t and int
1587 have the same mode. But might as well warn anyway,
1588 since it will fail on other machines. */
1589 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1590 != integer_type_node
)
1593 "field width is not type int (arg %d)",
1601 while (ISDIGIT (*format_chars
))
1607 if (*format_chars
== '.')
1611 if (*format_chars
!= '*' && !ISDIGIT (*format_chars
))
1612 warning ("`.' not followed by `*' or digit in format");
1613 /* "...a...precision...may be indicated by an asterisk.
1614 In this case, an int argument supplies the...precision." */
1615 if (*format_chars
== '*')
1617 if (info
->first_arg_num
!= 0)
1625 cur_param
= TREE_VALUE (params
);
1626 params
= TREE_CHAIN (params
);
1628 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1629 != integer_type_node
)
1632 "field width is not type int (arg %d)",
1640 while (ISDIGIT (*format_chars
))
1645 if (*format_chars
== 'h' || *format_chars
== 'l' || *format_chars
== 'L')
1646 length_char
= *format_chars
++;
1649 if (suppressed
&& length_char
!= 0)
1652 "use of `*' and `%c' together in format",
1656 format_char
= *format_chars
;
1657 if (format_char
== 0)
1659 warning ("conversion lacks type at end of format");
1663 fci
= info
->is_scan
? scan_table
: print_table
;
1666 if (fci
->format_chars
== 0
1667 || index (fci
->format_chars
, format_char
) != 0)
1671 if (fci
->format_chars
== 0)
1673 if (format_char
>= 040 && format_char
< 0177)
1675 "unknown conversion type character `%c' in format",
1679 "unknown conversion type character 0x%x in format",
1684 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
1686 sprintf (message
, "width used with `%c' format",
1690 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
1692 sprintf (message
, "precision used with `%c' format",
1698 if (index (fci
->flag_chars
, '*') == 0)
1701 "suppression of `%c' conversion in format",
1707 for (i
= 0; flag_chars
[i
] != 0; ++i
)
1709 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
1711 sprintf (message
, "flag `%c' used with type `%c'",
1712 flag_chars
[i
], format_char
);
1716 if (precise
&& index (flag_chars
, '0') != 0
1717 && (format_char
== 'd' || format_char
== 'i'
1718 || format_char
== 'o' || format_char
== 'u'
1719 || format_char
== 'x' || format_char
== 'x'))
1722 "precision and `0' flag not both allowed with `%c' format",
1726 switch (length_char
)
1728 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
1729 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
1730 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
1731 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
1733 if (wanted_type
== 0)
1736 "use of `%c' length character with `%c' type character",
1737 length_char
, format_char
);
1742 ** XXX -- should kvetch about stuff such as
1746 ** scanf ("%d", &i);
1750 /* Finally. . .check type of argument against desired type! */
1751 if (info
->first_arg_num
== 0)
1758 cur_param
= TREE_VALUE (params
);
1759 params
= TREE_CHAIN (params
);
1761 cur_type
= TREE_TYPE (cur_param
);
1763 /* Check the types of any additional pointer arguments
1764 that precede the "real" argument. */
1765 for (i
= 0; i
< fci
->pointer_count
; ++i
)
1767 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1769 cur_type
= TREE_TYPE (cur_type
);
1773 "format argument is not a %s (arg %d)",
1774 ((fci
->pointer_count
== 1) ? "pointer" : "pointer to a pointer"),
1780 /* Check the type of the "real" argument, if there's a type we want. */
1781 if (i
== fci
->pointer_count
&& wanted_type
!= 0
1782 && wanted_type
!= TYPE_MAIN_VARIANT (cur_type
)
1783 /* If we want `void *', allow any pointer type.
1784 (Anything else would already have got a warning.) */
1785 && ! (wanted_type
== void_type_node
1786 && fci
->pointer_count
> 0)
1787 /* Don't warn about differences merely in signedness. */
1788 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
1789 && TREE_CODE (cur_type
) == INTEGER_TYPE
1790 && TYPE_PRECISION (wanted_type
) == TYPE_PRECISION (cur_type
)))
1792 register char *this;
1793 register char *that
;
1795 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
1797 if (TYPE_NAME (cur_type
) != 0
1798 && TREE_CODE (cur_type
) != INTEGER_TYPE
1799 && !(TREE_CODE (cur_type
) == POINTER_TYPE
1800 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
)
1801 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
1802 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1804 /* A nameless type can't possibly match what the format wants.
1805 So there will be a warning for it.
1806 Make up a string to describe vaguely what it is. */
1809 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1812 that
= "different type";
1815 if (strcmp (this, that
) != 0)
1817 sprintf (message
, "%s format, %s arg (arg %d)",
1818 this, that
, arg_num
);
1825 /* Build a function call to function FUNCTION with parameters PARAMS.
1826 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1827 TREE_VALUE of each node is a parameter-expression.
1828 FUNCTION's data type may be a function type or a pointer-to-function. */
1831 build_function_call (function
, params
)
1832 tree function
, params
;
1834 register tree fntype
, fundecl
;
1835 register tree coerced_params
;
1836 tree name
= NULL_TREE
;
1838 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1839 STRIP_TYPE_NOPS (function
);
1841 /* Convert anything with function type to a pointer-to-function. */
1842 if (TREE_CODE (function
) == FUNCTION_DECL
)
1844 name
= DECL_NAME (function
);
1845 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1846 (because calling an inline function does not mean the function
1847 needs to be separately compiled). */
1848 fntype
= build_type_variant (TREE_TYPE (function
),
1849 TREE_READONLY (function
),
1850 TREE_THIS_VOLATILE (function
));
1852 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
1855 function
= default_conversion (function
);
1857 fntype
= TREE_TYPE (function
);
1859 if (TREE_CODE (fntype
) == ERROR_MARK
)
1860 return error_mark_node
;
1862 if (!(TREE_CODE (fntype
) == POINTER_TYPE
1863 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
1865 error ("called object is not a function");
1866 return error_mark_node
;
1869 /* fntype now gets the type of function pointed to. */
1870 fntype
= TREE_TYPE (fntype
);
1872 /* Convert the parameters to the types declared in the
1873 function prototype, or apply default promotions. */
1876 = convert_arguments (TYPE_ARG_TYPES (fntype
), params
, name
, fundecl
);
1878 /* Check for errors in format strings. */
1879 if (warn_format
&& name
!= 0)
1883 /* See if this function is a format function. */
1884 for (i
= 0; i
< function_info_entries
; i
++)
1885 if (function_info_table
[i
].function_ident
== name
)
1887 register char *message
;
1889 /* If so, check it. */
1890 check_format (&function_info_table
[i
], coerced_params
);
1895 /* Recognize certain built-in functions so we can make tree-codes
1896 other than CALL_EXPR. We do this when it enables fold-const.c
1897 to do something useful. */
1899 if (TREE_CODE (function
) == ADDR_EXPR
1900 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
1901 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
1902 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function
, 0)))
1907 if (coerced_params
== 0)
1908 return integer_zero_node
;
1909 return build_unary_op (ABS_EXPR
, TREE_VALUE (coerced_params
), 0);
1913 register tree result
1914 = build (CALL_EXPR
, TREE_TYPE (fntype
),
1915 function
, coerced_params
, NULL_TREE
);
1917 TREE_SIDE_EFFECTS (result
) = 1;
1918 if (TREE_TYPE (result
) == void_type_node
)
1920 return require_complete_type (result
);
1924 /* Convert the argument expressions in the list VALUES
1925 to the types in the list TYPELIST. The result is a list of converted
1926 argument expressions.
1928 If TYPELIST is exhausted, or when an element has NULL as its type,
1929 perform the default conversions.
1931 PARMLIST is the chain of parm decls for the function being called.
1932 It may be 0, if that info is not available.
1933 It is used only for generating error messages.
1935 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1937 This is also where warnings about wrong number of args are generated.
1939 Both VALUES and the returned value are chains of TREE_LIST nodes
1940 with the elements of the list in the TREE_VALUE slots of those nodes. */
1943 convert_arguments (typelist
, values
, name
, fundecl
)
1944 tree typelist
, values
, name
, fundecl
;
1946 register tree typetail
, valtail
;
1947 register tree result
= NULL
;
1950 /* Scan the given expressions and types, producing individual
1951 converted arguments and pushing them on RESULT in reverse order. */
1953 for (valtail
= values
, typetail
= typelist
, parmnum
= 0;
1955 valtail
= TREE_CHAIN (valtail
), parmnum
++)
1957 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
1958 register tree val
= TREE_VALUE (valtail
);
1960 if (type
== void_type_node
)
1963 error ("too many arguments to function `%s'",
1964 IDENTIFIER_POINTER (name
));
1966 error ("too many arguments to function");
1970 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1971 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1972 to convert automatically to a pointer. */
1973 if (TREE_CODE (val
) == NON_LVALUE_EXPR
)
1974 val
= TREE_OPERAND (val
, 0);
1976 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
1977 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
)
1978 val
= default_conversion (val
);
1980 val
= require_complete_type (val
);
1984 /* Formal parm type is specified by a function prototype. */
1987 if (TYPE_SIZE (type
) == 0)
1989 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
1995 #ifdef PROMOTE_PROTOTYPES
1996 /* Rather than truncating and then reextending,
1997 convert directly to int, if that's the type we will want. */
1998 if (! flag_traditional
1999 && TREE_CODE (type
) == INTEGER_TYPE
2000 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2001 type
= integer_type_node
;
2004 #if 0 /* This turns out not to win--there's no way to write a prototype
2005 for a function whose arg type is a union with no tag. */
2006 /* Nameless union automatically casts the types it contains. */
2007 if (TREE_CODE (type
) == UNION_TYPE
&& TYPE_NAME (type
) == 0)
2011 for (field
= TYPE_FIELDS (type
); field
;
2012 field
= TREE_CHAIN (field
))
2013 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
2014 TYPE_MAIN_VARIANT (TREE_TYPE (val
))))
2018 val
= build1 (CONVERT_EXPR
, type
, val
);
2022 /* Optionally warn about conversions that
2023 differ from the default conversions. */
2024 if (warn_conversion
)
2026 int formal_prec
= TYPE_PRECISION (type
);
2028 if (TREE_CODE (type
) != REAL_TYPE
2029 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
2030 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
2031 else if (TREE_CODE (type
) == REAL_TYPE
2032 && TREE_CODE (TREE_TYPE (val
)) != REAL_TYPE
)
2033 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
2034 else if (TREE_CODE (type
) == REAL_TYPE
2035 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
2037 /* Warn if any argument is passed as `float',
2038 since without a prototype it would be `double'. */
2039 if (formal_prec
== TYPE_PRECISION (float_type_node
))
2040 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name
, parmnum
+ 1);
2042 /* Detect integer changing in width or signedness. */
2043 else if ((TREE_CODE (type
) == INTEGER_TYPE
2044 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2045 && (TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
2046 || TREE_CODE (TREE_TYPE (val
)) == ENUMERAL_TYPE
))
2048 tree would_have_been
= default_conversion (val
);
2049 tree type1
= TREE_TYPE (would_have_been
);
2051 if (TREE_CODE (type
) == ENUMERAL_TYPE
2052 && type
== TREE_TYPE (val
))
2053 /* No warning if function asks for enum
2054 and the actual arg is that enum type. */
2056 else if (formal_prec
!= TYPE_PRECISION (type1
))
2057 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name
, parmnum
+ 1);
2058 else if (TREE_UNSIGNED (type
) == TREE_UNSIGNED (type1
))
2060 /* Don't complain if the formal parameter type
2061 is an enum, because we can't tell now whether
2062 the value was an enum--even the same enum. */
2063 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
2065 else if (TREE_CODE (val
) == INTEGER_CST
2066 && int_fits_type_p (val
, type
))
2067 /* Change in signedness doesn't matter
2068 if a constant value is unaffected. */
2070 else if (TREE_CODE (TREE_TYPE (val
)) == ENUMERAL_TYPE
2071 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val
)), type
)
2072 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val
)), type
))
2073 /* Change in signedness doesn't matter
2074 if an enum value is unaffected. */
2076 else if (TREE_UNSIGNED (type
))
2077 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name
, parmnum
+ 1);
2079 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name
, parmnum
+ 1);
2083 parmval
= convert_for_assignment (type
, val
,
2084 (char *)0, /* arg passing */
2085 fundecl
, name
, parmnum
+ 1);
2087 #ifdef PROMOTE_PROTOTYPES
2088 if (TREE_CODE (type
) == INTEGER_TYPE
2089 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2090 parmval
= default_conversion (parmval
);
2093 result
= tree_cons (NULL_TREE
, parmval
, result
);
2095 else if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
2096 && (TYPE_PRECISION (TREE_TYPE (val
))
2097 < TYPE_PRECISION (double_type_node
)))
2098 /* Convert `float' to `double'. */
2099 result
= tree_cons (NULL_TREE
, convert (double_type_node
, val
), result
);
2101 /* Convert `short' and `char' to full-size `int'. */
2102 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
2105 typetail
= TREE_CHAIN (typetail
);
2108 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
2111 error ("too few arguments to function `%s'",
2112 IDENTIFIER_POINTER (name
));
2114 error ("too few arguments to function");
2117 return nreverse (result
);
2120 /* This is the entry point used by the parser
2121 for binary operators in the input.
2122 In addition to constructing the expression,
2123 we check for operands that were written with other binary operators
2124 in a way that is likely to confuse the user. */
2127 parser_build_binary_op (code
, arg1
, arg2
)
2128 enum tree_code code
;
2131 tree result
= build_binary_op (code
, arg1
, arg2
, 1);
2134 char class1
= TREE_CODE_CLASS (TREE_CODE (arg1
));
2135 char class2
= TREE_CODE_CLASS (TREE_CODE (arg2
));
2136 enum tree_code code1
= ERROR_MARK
;
2137 enum tree_code code2
= ERROR_MARK
;
2139 if (class1
== 'e' || class1
== '1'
2140 || class1
== '2' || class1
== '<')
2141 code1
= C_EXP_ORIGINAL_CODE (arg1
);
2142 if (class2
== 'e' || class2
== '1'
2143 || class2
== '2' || class2
== '<')
2144 code2
= C_EXP_ORIGINAL_CODE (arg2
);
2146 /* Check for cases such as x+y<<z which users are likely
2147 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2148 is cleared to prevent these warnings. */
2149 if (warn_parentheses
)
2151 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
2153 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2154 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2155 warning ("suggest parentheses around + or - inside shift");
2158 if (code
== TRUTH_ORIF_EXPR
)
2160 if (code1
== TRUTH_ANDIF_EXPR
2161 || code2
== TRUTH_ANDIF_EXPR
)
2162 warning ("suggest parentheses around && within ||");
2165 if (code
== BIT_IOR_EXPR
)
2167 if (code1
== BIT_AND_EXPR
|| code1
== BIT_XOR_EXPR
2168 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2169 || code2
== BIT_AND_EXPR
|| code2
== BIT_XOR_EXPR
2170 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2171 warning ("suggest parentheses around arithmetic in operand of |");
2174 if (code
== BIT_XOR_EXPR
)
2176 if (code1
== BIT_AND_EXPR
2177 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2178 || code2
== BIT_AND_EXPR
2179 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2180 warning ("suggest parentheses around arithmetic in operand of ^");
2183 if (code
== BIT_AND_EXPR
)
2185 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2186 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2187 warning ("suggest parentheses around + or - in operand of &");
2191 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2192 if (TREE_CODE_CLASS (code
) == '<' && extra_warnings
2193 && (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<'))
2194 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2196 unsigned_conversion_warning (result
, arg1
);
2197 unsigned_conversion_warning (result
, arg2
);
2198 overflow_warning (result
);
2200 class = TREE_CODE_CLASS (TREE_CODE (result
));
2202 /* Record the code that was specified in the source,
2203 for the sake of warnings about confusing nesting. */
2204 if (class == 'e' || class == '1'
2205 || class == '2' || class == '<')
2206 C_SET_EXP_ORIGINAL_CODE (result
, code
);
2209 int flag
= TREE_CONSTANT (result
);
2210 /* We use NOP_EXPR rather than NON_LVALUE_EXPR
2211 so that convert_for_assignment won't strip it.
2212 That way, we get warnings for things like p = (1 - 1). */
2213 result
= build1 (NOP_EXPR
, TREE_TYPE (result
), result
);
2214 C_SET_EXP_ORIGINAL_CODE (result
, code
);
2215 TREE_CONSTANT (result
) = flag
;
2221 /* Build a binary-operation expression without default conversions.
2222 CODE is the kind of expression to build.
2223 This function differs from `build' in several ways:
2224 the data type of the result is computed and recorded in it,
2225 warnings are generated if arg data types are invalid,
2226 special handling for addition and subtraction of pointers is known,
2227 and some optimization is done (operations on narrow ints
2228 are done in the narrower type when that gives the same result).
2229 Constant folding is also done before the result is returned.
2231 Note that the operands will never have enumeral types, or function
2232 or array types, because either they will have the default conversions
2233 performed or they have both just been converted to some other type in which
2234 the arithmetic is to be done. */
2237 build_binary_op (code
, orig_op0
, orig_op1
, convert_p
)
2238 enum tree_code code
;
2239 tree orig_op0
, orig_op1
;
2243 register enum tree_code code0
, code1
;
2246 /* Expression code to give to the expression when it is built.
2247 Normally this is CODE, which is what the caller asked for,
2248 but in some special cases we change it. */
2249 register enum tree_code resultcode
= code
;
2251 /* Data type in which the computation is to be performed.
2252 In the simplest cases this is the common type of the arguments. */
2253 register tree result_type
= NULL
;
2255 /* Nonzero means operands have already been type-converted
2256 in whatever way is necessary.
2257 Zero means they need to be converted to RESULT_TYPE. */
2260 /* Nonzero means after finally constructing the expression
2261 give it this type. Otherwise, give it type RESULT_TYPE. */
2262 tree final_type
= 0;
2264 /* Nonzero if this is an operation like MIN or MAX which can
2265 safely be computed in short if both args are promoted shorts.
2266 Also implies COMMON.
2267 -1 indicates a bitwise operation; this makes a difference
2268 in the exact conditions for when it is safe to do the operation
2269 in a narrower mode. */
2272 /* Nonzero if this is a comparison operation;
2273 if both args are promoted shorts, compare the original shorts.
2274 Also implies COMMON. */
2275 int short_compare
= 0;
2277 /* Nonzero if this is a right-shift operation, which can be computed on the
2278 original short and then promoted if the operand is a promoted short. */
2279 int short_shift
= 0;
2281 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2286 op0
= default_conversion (orig_op0
);
2287 op1
= default_conversion (orig_op1
);
2295 type0
= TREE_TYPE (op0
);
2296 type1
= TREE_TYPE (op1
);
2298 /* The expression codes of the data types of the arguments tell us
2299 whether the arguments are integers, floating, pointers, etc. */
2300 code0
= TREE_CODE (type0
);
2301 code1
= TREE_CODE (type1
);
2303 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2304 STRIP_TYPE_NOPS (op0
);
2305 STRIP_TYPE_NOPS (op1
);
2307 /* If an error was already reported for one of the arguments,
2308 avoid reporting another error. */
2310 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
2311 return error_mark_node
;
2316 /* Handle the pointer + int case. */
2317 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2318 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
2319 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
2320 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
2326 /* Subtraction of two similar pointers.
2327 We must subtract them as integers, then divide by object size. */
2328 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
2329 && comp_target_types (type0
, type1
))
2330 return pointer_diff (op0
, op1
);
2331 /* Handle pointer minus int. Just like pointer plus int. */
2332 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2333 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
2342 case TRUNC_DIV_EXPR
:
2344 case FLOOR_DIV_EXPR
:
2345 case ROUND_DIV_EXPR
:
2346 case EXACT_DIV_EXPR
:
2347 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
2348 || code0
== COMPLEX_TYPE
)
2349 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
2350 || code1
== COMPLEX_TYPE
))
2352 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
2353 resultcode
= RDIV_EXPR
;
2355 /* When dividing two signed integers, you have to promote to int.
2356 E.g. (short) -32868 / (short) -1 doesn't fit in a short. */
2357 shorten
= TREE_UNSIGNED (op0
);
2363 case BIT_ANDTC_EXPR
:
2366 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2368 /* If one operand is a constant, and the other is a short type
2369 that has been converted to an int,
2370 really do the work in the short type and then convert the
2371 result to int. If we are lucky, the constant will be 0 or 1
2372 in the short type, making the entire operation go away. */
2373 if (TREE_CODE (op0
) == INTEGER_CST
2374 && TREE_CODE (op1
) == NOP_EXPR
2375 && TYPE_PRECISION (type1
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))
2376 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
2378 final_type
= result_type
;
2379 op1
= TREE_OPERAND (op1
, 0);
2380 result_type
= TREE_TYPE (op1
);
2382 if (TREE_CODE (op1
) == INTEGER_CST
2383 && TREE_CODE (op0
) == NOP_EXPR
2384 && TYPE_PRECISION (type0
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))
2385 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
2387 final_type
= result_type
;
2388 op0
= TREE_OPERAND (op0
, 0);
2389 result_type
= TREE_TYPE (op0
);
2393 case TRUNC_MOD_EXPR
:
2394 case FLOOR_MOD_EXPR
:
2395 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2399 case TRUTH_ANDIF_EXPR
:
2400 case TRUTH_ORIF_EXPR
:
2401 case TRUTH_AND_EXPR
:
2403 case TRUTH_XOR_EXPR
:
2404 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
2405 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
2406 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
2407 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
2409 /* Result of these operations is always an int,
2410 but that does not mean the operands should be
2411 converted to ints! */
2412 result_type
= integer_type_node
;
2413 op0
= truthvalue_conversion (op0
);
2414 op1
= truthvalue_conversion (op1
);
2419 /* Shift operations: result has same type as first operand;
2420 always convert second operand to int.
2421 Also set SHORT_SHIFT if shifting rightward. */
2424 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2426 if (TREE_CODE (op1
) == INTEGER_CST
)
2428 if (tree_int_cst_lt (op1
, integer_zero_node
))
2429 warning ("right shift count is negative");
2432 if (TREE_INT_CST_LOW (op1
) | TREE_INT_CST_HIGH (op1
))
2434 if (TREE_INT_CST_HIGH (op1
) != 0
2435 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
2436 >= TYPE_PRECISION (type0
)))
2437 warning ("right shift count >= width of type");
2440 /* Use the type of the value to be shifted.
2441 This is what most traditional C compilers do. */
2442 result_type
= type0
;
2443 /* Unless traditional, convert the shift-count to an integer,
2444 regardless of size of value being shifted. */
2445 if (! flag_traditional
)
2447 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
2448 op1
= convert (integer_type_node
, op1
);
2449 /* Avoid converting op1 to result_type later. */
2456 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2458 if (TREE_CODE (op1
) == INTEGER_CST
)
2460 if (tree_int_cst_lt (op1
, integer_zero_node
))
2461 warning ("left shift count is negative");
2462 else if (TREE_INT_CST_HIGH (op1
) != 0
2463 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
2464 >= TYPE_PRECISION (type0
)))
2465 warning ("left shift count >= width of type");
2467 /* Use the type of the value to be shifted.
2468 This is what most traditional C compilers do. */
2469 result_type
= type0
;
2470 /* Unless traditional, convert the shift-count to an integer,
2471 regardless of size of value being shifted. */
2472 if (! flag_traditional
)
2474 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
2475 op1
= convert (integer_type_node
, op1
);
2476 /* Avoid converting op1 to result_type later. */
2484 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2486 if (TREE_CODE (op1
) == INTEGER_CST
)
2488 if (tree_int_cst_lt (op1
, integer_zero_node
))
2489 warning ("shift count is negative");
2490 else if (TREE_INT_CST_HIGH (op1
) != 0
2491 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
2492 >= TYPE_PRECISION (type0
)))
2493 warning ("shift count >= width of type");
2495 /* Use the type of the value to be shifted.
2496 This is what most traditional C compilers do. */
2497 result_type
= type0
;
2498 /* Unless traditional, convert the shift-count to an integer,
2499 regardless of size of value being shifted. */
2500 if (! flag_traditional
)
2502 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
2503 op1
= convert (integer_type_node
, op1
);
2504 /* Avoid converting op1 to result_type later. */
2512 /* Result of comparison is always int,
2513 but don't convert the args to int! */
2514 result_type
= integer_type_node
;
2516 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
2517 || code0
== COMPLEX_TYPE
)
2518 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
2519 || code1
== COMPLEX_TYPE
))
2521 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
2523 register tree tt0
= TREE_TYPE (type0
);
2524 register tree tt1
= TREE_TYPE (type1
);
2525 /* Anything compares with void *. void * compares with anything.
2526 Otherwise, the targets must be the same. */
2527 if (comp_target_types (type0
, type1
))
2529 else if (TYPE_MAIN_VARIANT (tt0
) == void_type_node
)
2531 if (pedantic
&& !integer_zerop (op0
)
2532 && TREE_CODE (tt1
) == FUNCTION_TYPE
)
2533 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2535 else if (TYPE_MAIN_VARIANT (tt1
) == void_type_node
)
2537 if (pedantic
&& !integer_zerop (op1
)
2538 && TREE_CODE (tt0
) == FUNCTION_TYPE
)
2539 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2542 pedwarn ("comparison of distinct pointer types lacks a cast");
2544 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
2545 && integer_zerop (op1
))
2546 op1
= null_pointer_node
;
2547 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
2548 && integer_zerop (op0
))
2549 op0
= null_pointer_node
;
2550 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2552 if (! flag_traditional
)
2553 pedwarn ("comparison between pointer and integer");
2554 op1
= convert (TREE_TYPE (op0
), op1
);
2556 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
2558 if (! flag_traditional
)
2559 pedwarn ("comparison between pointer and integer");
2560 op0
= convert (TREE_TYPE (op1
), op0
);
2563 /* If args are not valid, clear out RESULT_TYPE
2564 to cause an error message later. */
2570 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
2571 || code0
== COMPLEX_TYPE
)
2572 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
2573 || code1
== COMPLEX_TYPE
))
2575 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
2577 if (! comp_target_types (type0
, type1
))
2578 pedwarn ("comparison of distinct pointer types lacks a cast");
2580 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
2581 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2582 result_type
= common_type (type0
, type1
);
2590 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
2591 || code0
== COMPLEX_TYPE
)
2592 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
2593 || code1
== COMPLEX_TYPE
))
2595 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
2597 if (! comp_target_types (type0
, type1
))
2598 pedwarn ("comparison of distinct pointer types lacks a cast");
2600 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
2601 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2602 result_type
= integer_type_node
;
2604 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
2605 && integer_zerop (op1
))
2607 result_type
= integer_type_node
;
2608 op1
= null_pointer_node
;
2609 if (! flag_traditional
)
2610 pedwarn ("ordered comparison of pointer with integer zero");
2612 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
2613 && integer_zerop (op0
))
2615 result_type
= integer_type_node
;
2616 op0
= null_pointer_node
;
2618 pedwarn ("ordered comparison of pointer with integer zero");
2620 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2622 result_type
= integer_type_node
;
2623 if (! flag_traditional
)
2624 pedwarn ("comparison between pointer and integer");
2625 op1
= convert (TREE_TYPE (op0
), op1
);
2627 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
2629 result_type
= integer_type_node
;
2630 if (! flag_traditional
)
2631 pedwarn ("comparison between pointer and integer");
2632 op0
= convert (TREE_TYPE (op1
), op0
);
2638 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
2640 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
2642 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
2644 if (shorten
|| common
|| short_compare
)
2645 result_type
= common_type (type0
, type1
);
2647 /* For certain operations (which identify themselves by shorten != 0)
2648 if both args were extended from the same smaller type,
2649 do the arithmetic in that type and then extend.
2651 shorten !=0 and !=1 indicates a bitwise operation.
2652 For them, this optimization is safe only if
2653 both args are zero-extended or both are sign-extended.
2654 Otherwise, we might change the result.
2655 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2656 but calculated in (unsigned short) it would be (unsigned short)-1. */
2658 if (shorten
&& none_complex
)
2660 int unsigned0
, unsigned1
;
2661 tree arg0
= get_narrower (op0
, &unsigned0
);
2662 tree arg1
= get_narrower (op1
, &unsigned1
);
2663 /* UNS is 1 if the operation to be done is an unsigned one. */
2664 int uns
= TREE_UNSIGNED (result_type
);
2667 final_type
= result_type
;
2669 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2670 but it *requires* conversion to FINAL_TYPE. */
2672 if ((TYPE_PRECISION (TREE_TYPE (op0
))
2673 == TYPE_PRECISION (TREE_TYPE (arg0
)))
2674 && TREE_TYPE (op0
) != final_type
)
2675 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
2676 if ((TYPE_PRECISION (TREE_TYPE (op1
))
2677 == TYPE_PRECISION (TREE_TYPE (arg1
)))
2678 && TREE_TYPE (op1
) != final_type
)
2679 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
2681 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2683 /* For bitwise operations, signedness of nominal type
2684 does not matter. Consider only how operands were extended. */
2688 /* Note that in all three cases below we refrain from optimizing
2689 an unsigned operation on sign-extended args.
2690 That would not be valid. */
2692 /* Both args variable: if both extended in same way
2693 from same width, do it in that width.
2694 Do it unsigned if args were zero-extended. */
2695 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
2696 < TYPE_PRECISION (result_type
))
2697 && (TYPE_PRECISION (TREE_TYPE (arg1
))
2698 == TYPE_PRECISION (TREE_TYPE (arg0
)))
2699 && unsigned0
== unsigned1
2700 && (unsigned0
|| !uns
))
2702 = signed_or_unsigned_type (unsigned0
,
2703 common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
2704 else if (TREE_CODE (arg0
) == INTEGER_CST
2705 && (unsigned1
|| !uns
)
2706 && (TYPE_PRECISION (TREE_TYPE (arg1
))
2707 < TYPE_PRECISION (result_type
))
2708 && (type
= signed_or_unsigned_type (unsigned1
,
2710 int_fits_type_p (arg0
, type
)))
2712 else if (TREE_CODE (arg1
) == INTEGER_CST
2713 && (unsigned0
|| !uns
)
2714 && (TYPE_PRECISION (TREE_TYPE (arg0
))
2715 < TYPE_PRECISION (result_type
))
2716 && (type
= signed_or_unsigned_type (unsigned0
,
2718 int_fits_type_p (arg1
, type
)))
2722 /* Shifts can be shortened if shifting right. */
2727 tree arg0
= get_narrower (op0
, &unsigned_arg
);
2729 final_type
= result_type
;
2731 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
2732 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
2734 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
2735 /* If arg is sign-extended and then unsigned-shifted,
2736 we can simulate this with a signed shift in arg's type
2737 only if the extended result is at least twice as wide
2738 as the arg. Otherwise, the shift could use up all the
2739 ones made by sign-extension and bring in zeros.
2740 We can't optimize that case at all, but in most machines
2741 it never happens because available widths are 2**N. */
2742 && (!TREE_UNSIGNED (final_type
)
2744 || 2 * TYPE_PRECISION (TREE_TYPE (arg0
)) <= TYPE_PRECISION (result_type
)))
2746 /* Do an unsigned shift if the operand was zero-extended. */
2748 = signed_or_unsigned_type (unsigned_arg
,
2750 /* Convert value-to-be-shifted to that type. */
2751 if (TREE_TYPE (op0
) != result_type
)
2752 op0
= convert (result_type
, op0
);
2757 /* Comparison operations are shortened too but differently.
2758 They identify themselves by setting short_compare = 1. */
2760 if (short_compare
&& none_complex
)
2762 /* Don't write &op0, etc., because that would prevent op0
2763 from being kept in a register.
2764 Instead, make copies of the our local variables and
2765 pass the copies by reference, then copy them back afterward. */
2766 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
2767 enum tree_code xresultcode
= resultcode
;
2769 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
2772 op0
= xop0
, op1
= xop1
, result_type
= xresult_type
;
2773 resultcode
= xresultcode
;
2777 tree op0_type
= TREE_TYPE (orig_op0
);
2778 tree op1_type
= TREE_TYPE (orig_op1
);
2779 int op0_unsigned
= TREE_UNSIGNED (op0_type
);
2780 int op1_unsigned
= TREE_UNSIGNED (op1_type
);
2782 /* Give warnings for comparisons between signed and unsigned
2783 quantities that will fail. Do not warn if the signed quantity
2784 is an unsuffixed integer literal (or some static constant
2785 expression involving such literals) and it is positive.
2786 Do not warn if the width of the unsigned quantity is less
2787 than that of the signed quantity, since in this case all
2788 values of the unsigned quantity fit in the signed quantity.
2789 Do not warn if the signed type is the same size as the
2790 result_type since sign extension does not cause trouble in
2792 /* Do the checking based on the original operand trees, so that
2793 casts will be considered, but default promotions won't be. */
2794 if (op0_unsigned
!= op1_unsigned
2796 && TYPE_PRECISION (op0_type
) >= TYPE_PRECISION (op1_type
)
2797 && TYPE_PRECISION (op0_type
) < TYPE_PRECISION (result_type
)
2798 && (TREE_CODE (op1
) != INTEGER_CST
2799 || (TREE_CODE (op1
) == INTEGER_CST
2800 && INT_CST_LT (op1
, integer_zero_node
))))
2803 && TYPE_PRECISION (op1_type
) >= TYPE_PRECISION (op0_type
)
2804 && TYPE_PRECISION (op1_type
) < TYPE_PRECISION (result_type
)
2805 && (TREE_CODE (op0
) != INTEGER_CST
2806 || (TREE_CODE (op0
) == INTEGER_CST
2807 && INT_CST_LT (op0
, integer_zero_node
))))))
2808 warning ("comparison between signed and unsigned");
2813 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2814 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2815 Then the expression will be built.
2816 It will be given type FINAL_TYPE if that is nonzero;
2817 otherwise, it will be given type RESULT_TYPE. */
2821 binary_op_error (code
);
2822 return error_mark_node
;
2827 if (TREE_TYPE (op0
) != result_type
)
2828 op0
= convert (result_type
, op0
);
2829 if (TREE_TYPE (op1
) != result_type
)
2830 op1
= convert (result_type
, op1
);
2834 register tree result
= build (resultcode
, result_type
, op0
, op1
);
2835 register tree folded
;
2837 folded
= fold (result
);
2838 if (folded
== result
)
2839 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
2840 if (final_type
!= 0)
2841 return convert (final_type
, folded
);
2846 /* Return a tree for the sum or difference (RESULTCODE says which)
2847 of pointer PTROP and integer INTOP. */
2850 pointer_int_sum (resultcode
, ptrop
, intop
)
2851 enum tree_code resultcode
;
2852 register tree ptrop
, intop
;
2856 register tree result
;
2857 register tree folded
;
2859 /* The result is a pointer of the same type that is being added. */
2861 register tree result_type
= TREE_TYPE (ptrop
);
2863 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
2865 if (pedantic
|| warn_pointer_arith
)
2866 pedwarn ("pointer of type `void *' used in arithmetic");
2867 size_exp
= integer_one_node
;
2869 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
2871 if (pedantic
|| warn_pointer_arith
)
2872 pedwarn ("pointer to a function used in arithmetic");
2873 size_exp
= integer_one_node
;
2876 size_exp
= c_size_in_bytes (TREE_TYPE (result_type
));
2878 /* If what we are about to multiply by the size of the elements
2879 contains a constant term, apply distributive law
2880 and multiply that constant term separately.
2881 This helps produce common subexpressions. */
2883 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
2884 && ! TREE_CONSTANT (intop
)
2885 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
2886 && TREE_CONSTANT (size_exp
)
2887 /* If the constant comes from pointer subtraction,
2888 skip this optimization--it would cause an error. */
2889 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop
, 0))) == INTEGER_TYPE
)
2891 enum tree_code subcode
= resultcode
;
2892 tree int_type
= TREE_TYPE (intop
);
2893 if (TREE_CODE (intop
) == MINUS_EXPR
)
2894 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
2895 /* Convert both subexpression types to the type of intop,
2896 because weird cases involving pointer arithmetic
2897 can result in a sum or difference with different type args. */
2898 ptrop
= build_binary_op (subcode
, ptrop
,
2899 convert (int_type
, TREE_OPERAND (intop
, 1)), 1);
2900 intop
= convert (int_type
, TREE_OPERAND (intop
, 0));
2903 /* Convert the integer argument to a type the same size as a pointer
2904 so the multiply won't overflow spuriously. */
2906 if (TYPE_PRECISION (TREE_TYPE (intop
)) != POINTER_SIZE
)
2907 intop
= convert (type_for_size (POINTER_SIZE
, 0), intop
);
2909 /* Replace the integer argument
2910 with a suitable product by the object size. */
2912 intop
= build_binary_op (MULT_EXPR
, intop
, size_exp
, 1);
2914 /* Create the sum or difference. */
2916 result
= build (resultcode
, result_type
, ptrop
, intop
);
2918 folded
= fold (result
);
2919 if (folded
== result
)
2920 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
2924 /* Return a tree for the difference of pointers OP0 and OP1.
2925 The resulting tree has type int. */
2928 pointer_diff (op0
, op1
)
2929 register tree op0
, op1
;
2931 register tree result
, folded
;
2932 tree restype
= ptrdiff_type_node
;
2934 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
2936 if (pedantic
|| warn_pointer_arith
)
2938 if (TREE_CODE (target_type
) == VOID_TYPE
)
2939 pedwarn ("pointer of type `void *' used in subtraction");
2940 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
2941 pedwarn ("pointer to a function used in subtraction");
2944 /* First do the subtraction as integers;
2945 then drop through to build the divide operator. */
2947 op0
= build_binary_op (MINUS_EXPR
, convert (restype
, op0
),
2948 convert (restype
, op1
), 1);
2949 /* This generates an error if op1 is pointer to incomplete type. */
2950 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1
))) == 0)
2951 error ("arithmetic on pointer to an incomplete type");
2952 /* This generates an error if op0 is pointer to incomplete type. */
2953 op1
= c_size_in_bytes (target_type
);
2955 /* Divide by the size, in easiest possible way. */
2957 result
= build (EXACT_DIV_EXPR
, restype
, op0
, op1
);
2959 folded
= fold (result
);
2960 if (folded
== result
)
2961 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
2965 /* Construct and perhaps optimize a tree representation
2966 for a unary operation. CODE, a tree_code, specifies the operation
2967 and XARG is the operand. NOCONVERT nonzero suppresses
2968 the default promotions (such as from short to int). */
2971 build_unary_op (code
, xarg
, noconvert
)
2972 enum tree_code code
;
2976 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2977 register tree arg
= xarg
;
2978 register tree argtype
= 0;
2979 register enum tree_code typecode
= TREE_CODE (TREE_TYPE (arg
));
2980 char *errstring
= NULL
;
2983 if (typecode
== ERROR_MARK
)
2984 return error_mark_node
;
2985 if (typecode
== ENUMERAL_TYPE
)
2986 typecode
= INTEGER_TYPE
;
2991 /* This is used for unary plus, because a CONVERT_EXPR
2992 is enough to prevent anybody from looking inside for
2993 associativity, but won't generate any code. */
2994 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2995 || typecode
== COMPLEX_TYPE
))
2996 errstring
= "wrong type argument to unary plus";
2997 else if (!noconvert
)
2998 arg
= default_conversion (arg
);
3002 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3003 || typecode
== COMPLEX_TYPE
))
3004 errstring
= "wrong type argument to unary minus";
3005 else if (!noconvert
)
3006 arg
= default_conversion (arg
);
3010 if (typecode
!= INTEGER_TYPE
)
3011 errstring
= "wrong type argument to bit-complement";
3012 else if (!noconvert
)
3013 arg
= default_conversion (arg
);
3017 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3018 || typecode
== COMPLEX_TYPE
))
3019 errstring
= "wrong type argument to abs";
3020 else if (!noconvert
)
3021 arg
= default_conversion (arg
);
3024 case TRUTH_NOT_EXPR
:
3025 if (typecode
!= INTEGER_TYPE
3026 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3027 && typecode
!= COMPLEX_TYPE
3028 /* These will convert to a pointer. */
3029 && typecode
!= ARRAY_TYPE
&& typecode
!= FUNCTION_TYPE
)
3031 errstring
= "wrong type argument to unary exclamation mark";
3034 arg
= truthvalue_conversion (arg
);
3035 return invert_truthvalue (arg
);
3041 if (TREE_CODE (arg
) == COMPLEX_CST
)
3042 return TREE_REALPART (arg
);
3043 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
3044 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
3049 if (TREE_CODE (arg
) == COMPLEX_CST
)
3050 return TREE_IMAGPART (arg
);
3051 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
3052 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
3054 return convert (TREE_TYPE (arg
), integer_zero_node
);
3056 case PREINCREMENT_EXPR
:
3057 case POSTINCREMENT_EXPR
:
3058 case PREDECREMENT_EXPR
:
3059 case POSTDECREMENT_EXPR
:
3060 /* Handle complex lvalues (when permitted)
3061 by reduction to simpler cases. */
3063 val
= unary_complex_lvalue (code
, arg
);
3067 /* Increment or decrement the real part of the value,
3068 and don't change the imaginary part. */
3069 if (typecode
== COMPLEX_TYPE
)
3073 arg
= stabilize_reference (arg
);
3074 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
3075 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
3076 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
3077 build_unary_op (code
, real
, 1), imag
);
3080 /* Report invalid types. */
3082 if (typecode
!= POINTER_TYPE
3083 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
)
3085 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3086 errstring
="wrong type argument to increment";
3088 errstring
="wrong type argument to decrement";
3094 tree result_type
= TREE_TYPE (arg
);
3096 arg
= get_unwidened (arg
, 0);
3097 argtype
= TREE_TYPE (arg
);
3099 /* Compute the increment. */
3101 if (typecode
== POINTER_TYPE
)
3103 /* If pointer target is an undefined struct,
3104 we just cannot know how to do the arithmetic. */
3105 if (TYPE_SIZE (TREE_TYPE (result_type
)) == 0)
3106 error ("%s of pointer to unknown structure",
3107 ((code
== PREINCREMENT_EXPR
3108 || code
== POSTINCREMENT_EXPR
)
3109 ? "increment" : "decrement"));
3110 else if ((pedantic
|| warn_pointer_arith
)
3111 && (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
3112 || TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
))
3113 pedwarn ("wrong type argument to %s",
3114 ((code
== PREINCREMENT_EXPR
3115 || code
== POSTINCREMENT_EXPR
)
3116 ? "increment" : "decrement"));
3117 inc
= c_sizeof_nowarn (TREE_TYPE (result_type
));
3120 inc
= integer_one_node
;
3122 inc
= convert (argtype
, inc
);
3124 /* Handle incrementing a cast-expression. */
3127 switch (TREE_CODE (arg
))
3132 case FIX_TRUNC_EXPR
:
3133 case FIX_FLOOR_EXPR
:
3134 case FIX_ROUND_EXPR
:
3136 pedantic_lvalue_warning (CONVERT_EXPR
);
3137 /* If the real type has the same machine representation
3138 as the type it is cast to, we can make better output
3139 by adding directly to the inside of the cast. */
3140 if ((TREE_CODE (TREE_TYPE (arg
))
3141 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))))
3142 && (TYPE_MODE (TREE_TYPE (arg
))
3143 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg
, 0)))))
3144 arg
= TREE_OPERAND (arg
, 0);
3147 tree incremented
, modify
, value
;
3148 arg
= stabilize_reference (arg
);
3149 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
3152 value
= save_expr (arg
);
3153 incremented
= build (((code
== PREINCREMENT_EXPR
3154 || code
== POSTINCREMENT_EXPR
)
3155 ? PLUS_EXPR
: MINUS_EXPR
),
3156 argtype
, value
, inc
);
3157 TREE_SIDE_EFFECTS (incremented
) = 1;
3158 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
3159 value
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
3160 TREE_USED (value
) = 1;
3170 /* Complain about anything else that is not a true lvalue. */
3171 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
3172 || code
== POSTINCREMENT_EXPR
)
3173 ? "increment" : "decrement")))
3174 return error_mark_node
;
3176 /* Report a read-only lvalue. */
3177 if (TREE_READONLY (arg
))
3178 readonly_warning (arg
,
3179 ((code
== PREINCREMENT_EXPR
3180 || code
== POSTINCREMENT_EXPR
)
3181 ? "increment" : "decrement"));
3183 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
3184 TREE_SIDE_EFFECTS (val
) = 1;
3185 val
= convert (result_type
, val
);
3186 if (TREE_CODE (val
) != code
)
3187 TREE_NO_UNUSED_WARNING (val
) = 1;
3192 /* Note that this operation never does default_conversion
3193 regardless of NOCONVERT. */
3195 /* Let &* cancel out to simplify resulting code. */
3196 if (TREE_CODE (arg
) == INDIRECT_REF
)
3198 /* Don't let this be an lvalue. */
3199 if (lvalue_p (TREE_OPERAND (arg
, 0)))
3200 return non_lvalue (TREE_OPERAND (arg
, 0));
3201 return TREE_OPERAND (arg
, 0);
3204 /* For &x[y], return x+y */
3205 if (TREE_CODE (arg
) == ARRAY_REF
)
3207 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
3208 return error_mark_node
;
3209 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
3210 TREE_OPERAND (arg
, 1), 1);
3213 /* Handle complex lvalues (when permitted)
3214 by reduction to simpler cases. */
3215 val
= unary_complex_lvalue (code
, arg
);
3219 #if 0 /* Turned off because inconsistent;
3220 float f; *&(int)f = 3.4 stores in int format
3221 whereas (int)f = 3.4 stores in float format. */
3222 /* Address of a cast is just a cast of the address
3223 of the operand of the cast. */
3224 switch (TREE_CODE (arg
))
3229 case FIX_TRUNC_EXPR
:
3230 case FIX_FLOOR_EXPR
:
3231 case FIX_ROUND_EXPR
:
3234 pedwarn ("ANSI C forbids the address of a cast expression");
3235 return convert (build_pointer_type (TREE_TYPE (arg
)),
3236 build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0),
3241 /* Allow the address of a constructor if all the elements
3243 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_CONSTANT (arg
))
3245 /* Anything not already handled and not a true memory reference
3247 else if (typecode
!= FUNCTION_TYPE
&& !lvalue_or_else (arg
, "unary `&'"))
3248 return error_mark_node
;
3250 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3251 argtype
= TREE_TYPE (arg
);
3252 /* If the lvalue is const or volatile,
3253 merge that into the type that the address will point to. */
3254 if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'd'
3255 || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
3257 if (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
3258 argtype
= c_build_type_variant (argtype
,
3259 TREE_READONLY (arg
),
3260 TREE_THIS_VOLATILE (arg
));
3263 argtype
= build_pointer_type (argtype
);
3265 if (mark_addressable (arg
) == 0)
3266 return error_mark_node
;
3271 if (TREE_CODE (arg
) == COMPONENT_REF
)
3273 tree field
= TREE_OPERAND (arg
, 1);
3275 addr
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
3277 if (DECL_BIT_FIELD (field
))
3279 error ("attempt to take address of bit-field structure member `%s'",
3280 IDENTIFIER_POINTER (DECL_NAME (field
)));
3281 return error_mark_node
;
3284 addr
= convert (argtype
, addr
);
3286 if (! integer_zerop (DECL_FIELD_BITPOS (field
)))
3289 = size_binop (EASY_DIV_EXPR
, DECL_FIELD_BITPOS (field
),
3290 size_int (BITS_PER_UNIT
));
3291 int flag
= TREE_CONSTANT (addr
);
3292 addr
= fold (build (PLUS_EXPR
, argtype
,
3293 addr
, convert (argtype
, offset
)));
3294 TREE_CONSTANT (addr
) = flag
;
3298 addr
= build1 (code
, argtype
, arg
);
3300 /* Address of a static or external variable or
3301 file-scope function counts as a constant. */
3303 && ! (TREE_CODE (arg
) == FUNCTION_DECL
3304 && DECL_CONTEXT (arg
) != 0))
3305 TREE_CONSTANT (addr
) = 1;
3313 argtype
= TREE_TYPE (arg
);
3314 return fold (build1 (code
, argtype
, arg
));
3318 return error_mark_node
;
3322 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3323 convert ARG with the same conversions in the same order
3324 and return the result. */
3327 convert_sequence (conversions
, arg
)
3331 switch (TREE_CODE (conversions
))
3336 case FIX_TRUNC_EXPR
:
3337 case FIX_FLOOR_EXPR
:
3338 case FIX_ROUND_EXPR
:
3340 return convert (TREE_TYPE (conversions
),
3341 convert_sequence (TREE_OPERAND (conversions
, 0),
3350 /* Return nonzero if REF is an lvalue valid for this language.
3351 Lvalues can be assigned, unless their type has TYPE_READONLY.
3352 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3358 register enum tree_code code
= TREE_CODE (ref
);
3365 return lvalue_p (TREE_OPERAND (ref
, 0));
3376 if (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
3377 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
)
3384 /* Return nonzero if REF is an lvalue valid for this language;
3385 otherwise, print an error message and return zero. */
3388 lvalue_or_else (ref
, string
)
3392 int win
= lvalue_p (ref
);
3394 error ("invalid lvalue in %s", string
);
3398 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3399 for certain kinds of expressions which are not really lvalues
3400 but which we can accept as lvalues.
3402 If ARG is not a kind of expression we can handle, return zero. */
3405 unary_complex_lvalue (code
, arg
)
3406 enum tree_code code
;
3409 /* Handle (a, b) used as an "lvalue". */
3410 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
3412 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
3413 pedantic_lvalue_warning (COMPOUND_EXPR
);
3414 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
3415 TREE_OPERAND (arg
, 0), real_result
);
3418 /* Handle (a ? b : c) used as an "lvalue". */
3419 if (TREE_CODE (arg
) == COND_EXPR
)
3421 pedantic_lvalue_warning (COND_EXPR
);
3422 return (build_conditional_expr
3423 (TREE_OPERAND (arg
, 0),
3424 build_unary_op (code
, TREE_OPERAND (arg
, 1), 0),
3425 build_unary_op (code
, TREE_OPERAND (arg
, 2), 0)));
3431 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3432 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3435 pedantic_lvalue_warning (code
)
3436 enum tree_code code
;
3439 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3440 code
== COND_EXPR
? "conditional"
3441 : code
== COMPOUND_EXPR
? "compound" : "cast");
3444 /* Warn about storing in something that is `const'. */
3447 readonly_warning (arg
, string
)
3452 strcpy (buf
, string
);
3454 /* Forbid assignments to iterators. */
3455 if (TREE_CODE (arg
) == VAR_DECL
&& ITERATOR_P (arg
))
3457 strcat (buf
, " of iterator `%s'");
3458 pedwarn (buf
, IDENTIFIER_POINTER (DECL_NAME (arg
)));
3461 if (TREE_CODE (arg
) == COMPONENT_REF
)
3463 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
3464 readonly_warning (TREE_OPERAND (arg
, 0), string
);
3467 strcat (buf
, " of read-only member `%s'");
3468 pedwarn (buf
, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg
, 1))));
3471 else if (TREE_CODE (arg
) == VAR_DECL
)
3473 strcat (buf
, " of read-only variable `%s'");
3474 pedwarn (buf
, IDENTIFIER_POINTER (DECL_NAME (arg
)));
3478 pedwarn ("%s of read-only location", buf
);
3482 /* Mark EXP saying that we need to be able to take the
3483 address of it; it should not be allocated in a register.
3484 Value is 1 if successful. */
3487 mark_addressable (exp
)
3490 register tree x
= exp
;
3492 switch (TREE_CODE (x
))
3497 x
= TREE_OPERAND (x
, 0);
3501 TREE_ADDRESSABLE (x
) = 1;
3508 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
3509 && DECL_NONLOCAL (x
))
3511 if (TREE_PUBLIC (x
))
3513 error ("global register variable `%s' used in nested function",
3514 IDENTIFIER_POINTER (DECL_NAME (x
)));
3517 pedwarn ("register variable `%s' used in nested function",
3518 IDENTIFIER_POINTER (DECL_NAME (x
)));
3520 else if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
))
3522 if (TREE_PUBLIC (x
))
3524 error ("address of global register variable `%s' requested",
3525 IDENTIFIER_POINTER (DECL_NAME (x
)));
3528 pedwarn ("address of register variable `%s' requested",
3529 IDENTIFIER_POINTER (DECL_NAME (x
)));
3531 put_var_into_stack (x
);
3535 TREE_ADDRESSABLE (x
) = 1;
3536 #if 0 /* poplevel deals with this now. */
3537 if (DECL_CONTEXT (x
) == 0)
3538 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
3546 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3549 build_conditional_expr (ifexp
, op1
, op2
)
3550 tree ifexp
, op1
, op2
;
3552 register tree type1
;
3553 register tree type2
;
3554 register enum tree_code code1
;
3555 register enum tree_code code2
;
3556 register tree result_type
= NULL
;
3558 /* If second operand is omitted, it is the same as the first one;
3559 make sure it is calculated only once. */
3563 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3564 ifexp
= op1
= save_expr (ifexp
);
3567 ifexp
= truthvalue_conversion (default_conversion (ifexp
));
3569 #if 0 /* Produces wrong result if within sizeof. */
3570 /* Don't promote the operands separately if they promote
3571 the same way. Return the unpromoted type and let the combined
3572 value get promoted if necessary. */
3574 if (TREE_TYPE (op1
) == TREE_TYPE (op2
)
3575 && TREE_CODE (TREE_TYPE (op1
)) != ARRAY_TYPE
3576 && TREE_CODE (TREE_TYPE (op1
)) != ENUMERAL_TYPE
3577 && TREE_CODE (TREE_TYPE (op1
)) != FUNCTION_TYPE
)
3579 if (TREE_CODE (ifexp
) == INTEGER_CST
)
3580 return (integer_zerop (ifexp
) ? op2
: op1
);
3582 return fold (build (COND_EXPR
, TREE_TYPE (op1
), ifexp
, op1
, op2
));
3586 /* Promote both alternatives. */
3588 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
3589 op1
= default_conversion (op1
);
3590 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
3591 op2
= default_conversion (op2
);
3593 if (TREE_CODE (ifexp
) == ERROR_MARK
3594 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
3595 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
3596 return error_mark_node
;
3598 type1
= TREE_TYPE (op1
);
3599 code1
= TREE_CODE (type1
);
3600 type2
= TREE_TYPE (op2
);
3601 code2
= TREE_CODE (type2
);
3603 /* Quickly detect the usual case where op1 and op2 have the same type
3605 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
3608 result_type
= type1
;
3610 result_type
= TYPE_MAIN_VARIANT (type1
);
3612 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
)
3613 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
))
3615 result_type
= common_type (type1
, type2
);
3617 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
3619 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
3620 pedwarn ("ANSI C forbids conditional expr with only one void side");
3621 result_type
= void_type_node
;
3623 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
3625 if (comp_target_types (type1
, type2
))
3626 result_type
= common_type (type1
, type2
);
3627 else if (integer_zerop (op1
) && TREE_TYPE (type1
) == void_type_node
)
3628 result_type
= qualify_type (type2
, type1
);
3629 else if (integer_zerop (op2
) && TREE_TYPE (type2
) == void_type_node
)
3630 result_type
= qualify_type (type1
, type2
);
3631 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1
)) == void_type_node
)
3633 if (pedantic
&& TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
3634 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3635 result_type
= qualify_type (type1
, type2
);
3637 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2
)) == void_type_node
)
3639 if (pedantic
&& TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
3640 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3641 result_type
= qualify_type (type2
, type1
);
3645 pedwarn ("pointer type mismatch in conditional expression");
3646 result_type
= build_pointer_type (void_type_node
);
3649 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
3651 if (! integer_zerop (op2
))
3652 pedwarn ("pointer/integer type mismatch in conditional expression");
3655 op2
= null_pointer_node
;
3656 #if 0 /* The spec seems to say this is permitted. */
3657 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
3658 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3661 result_type
= type1
;
3663 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3665 if (!integer_zerop (op1
))
3666 pedwarn ("pointer/integer type mismatch in conditional expression");
3669 op1
= null_pointer_node
;
3670 #if 0 /* The spec seems to say this is permitted. */
3671 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
3672 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3675 result_type
= type2
;
3680 if (flag_cond_mismatch
)
3681 result_type
= void_type_node
;
3684 error ("type mismatch in conditional expression");
3685 return error_mark_node
;
3689 /* Merge const and volatile flags of the incoming types. */
3691 = build_type_variant (result_type
,
3692 TREE_READONLY (op1
) || TREE_READONLY (op2
),
3693 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
3695 if (result_type
!= TREE_TYPE (op1
))
3696 op1
= convert_and_check (result_type
, op1
);
3697 if (result_type
!= TREE_TYPE (op2
))
3698 op2
= convert_and_check (result_type
, op2
);
3701 if (code1
== RECORD_TYPE
|| code1
== UNION_TYPE
)
3703 result_type
= TREE_TYPE (op1
);
3704 if (TREE_CONSTANT (ifexp
))
3705 return (integer_zerop (ifexp
) ? op2
: op1
);
3707 if (TYPE_MODE (result_type
) == BLKmode
)
3709 register tree tempvar
3710 = build_decl (VAR_DECL
, NULL_TREE
, result_type
);
3711 register tree xop1
= build_modify_expr (tempvar
, op1
);
3712 register tree xop2
= build_modify_expr (tempvar
, op2
);
3713 register tree result
= fold (build (COND_EXPR
, result_type
,
3714 ifexp
, xop1
, xop2
));
3716 layout_decl (tempvar
, TYPE_ALIGN (result_type
));
3717 /* No way to handle variable-sized objects here.
3718 I fear that the entire handling of BLKmode conditional exprs
3719 needs to be redone. */
3720 if (TREE_CODE (DECL_SIZE (tempvar
)) != INTEGER_CST
)
3723 = assign_stack_local (DECL_MODE (tempvar
),
3724 (TREE_INT_CST_LOW (DECL_SIZE (tempvar
))
3725 + BITS_PER_UNIT
- 1)
3729 TREE_SIDE_EFFECTS (result
)
3730 = TREE_SIDE_EFFECTS (ifexp
) | TREE_SIDE_EFFECTS (op1
)
3731 | TREE_SIDE_EFFECTS (op2
);
3732 return build (COMPOUND_EXPR
, result_type
, result
, tempvar
);
3737 if (TREE_CODE (ifexp
) == INTEGER_CST
)
3738 return integer_zerop (ifexp
) ? op2
: op1
;
3740 return fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
));
3743 /* Given a list of expressions, return a compound expression
3744 that performs them all and returns the value of the last of them. */
3747 build_compound_expr (list
)
3750 return internal_build_compound_expr (list
, TRUE
);
3754 internal_build_compound_expr (list
, first_p
)
3760 if (TREE_CHAIN (list
) == 0)
3762 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3763 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3765 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3766 if (TREE_CODE (list
) == NON_LVALUE_EXPR
)
3767 list
= TREE_OPERAND (list
, 0);
3770 /* Don't let (0, 0) be null pointer constant. */
3771 if (!first_p
&& integer_zerop (TREE_VALUE (list
)))
3772 return non_lvalue (TREE_VALUE (list
));
3773 return TREE_VALUE (list
);
3776 if (TREE_CHAIN (list
) != 0 && TREE_CHAIN (TREE_CHAIN (list
)) == 0)
3778 /* Convert arrays to pointers when there really is a comma operator. */
3779 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list
)))) == ARRAY_TYPE
)
3780 TREE_VALUE (TREE_CHAIN (list
))
3781 = default_conversion (TREE_VALUE (TREE_CHAIN (list
)));
3784 rest
= internal_build_compound_expr (TREE_CHAIN (list
), FALSE
);
3786 /* When pedantic, a compound expression can be neither an lvalue
3787 nor an integer constant expression. */
3788 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)) && ! pedantic
)
3791 return build (COMPOUND_EXPR
, TREE_TYPE (rest
), TREE_VALUE (list
), rest
);
3794 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3797 build_c_cast (type
, expr
)
3801 register tree value
= expr
;
3803 if (type
== error_mark_node
|| expr
== error_mark_node
)
3804 return error_mark_node
;
3805 type
= TYPE_MAIN_VARIANT (type
);
3808 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3809 if (TREE_CODE (value
) == NON_LVALUE_EXPR
)
3810 value
= TREE_OPERAND (value
, 0);
3813 if (TREE_CODE (type
) == ARRAY_TYPE
)
3815 error ("cast specifies array type");
3816 return error_mark_node
;
3819 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3821 error ("cast specifies function type");
3822 return error_mark_node
;
3825 if (type
== TREE_TYPE (value
))
3829 if (TREE_CODE (type
) == RECORD_TYPE
3830 || TREE_CODE (type
) == UNION_TYPE
)
3831 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3834 else if (TREE_CODE (type
) == UNION_TYPE
)
3837 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
3838 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
3839 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
3848 pedwarn ("ANSI C forbids casts to union type");
3849 if (TYPE_NAME (type
) != 0)
3851 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
3852 name
= IDENTIFIER_POINTER (TYPE_NAME (type
));
3854 name
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
)));
3858 return digest_init (type
, build_nt (CONSTRUCTOR
, NULL_TREE
,
3859 build_tree_list (field
, value
)),
3860 NULL_PTR
, 0, 0, name
);
3862 error ("cast to union type from type not present in union");
3863 return error_mark_node
;
3869 /* If casting to void, avoid the error that would come
3870 from default_conversion in the case of a non-lvalue array. */
3871 if (type
== void_type_node
)
3872 return build1 (CONVERT_EXPR
, type
, value
);
3874 /* Convert functions and arrays to pointers,
3875 but don't convert any other types. */
3876 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
3877 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
)
3878 value
= default_conversion (value
);
3879 otype
= TREE_TYPE (value
);
3881 /* Optionally warn about potentially worrisome casts. */
3884 && TREE_CODE (type
) == POINTER_TYPE
3885 && TREE_CODE (otype
) == POINTER_TYPE
)
3887 if (TYPE_VOLATILE (TREE_TYPE (otype
))
3888 && ! TYPE_VOLATILE (TREE_TYPE (type
)))
3889 pedwarn ("cast discards `volatile' from pointer target type");
3890 if (TYPE_READONLY (TREE_TYPE (otype
))
3891 && ! TYPE_READONLY (TREE_TYPE (type
)))
3892 pedwarn ("cast discards `const' from pointer target type");
3895 /* Warn about possible alignment problems. */
3896 if (STRICT_ALIGNMENT
&& warn_cast_align
3897 && TREE_CODE (type
) == POINTER_TYPE
3898 && TREE_CODE (otype
) == POINTER_TYPE
3899 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
3900 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
3901 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
3902 warning ("cast increases required alignment of target type");
3904 if (TREE_CODE (type
) == INTEGER_TYPE
3905 && TREE_CODE (otype
) == POINTER_TYPE
3906 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3907 && !TREE_CONSTANT (value
))
3908 warning ("cast from pointer to integer of different size");
3910 if (TREE_CODE (type
) == POINTER_TYPE
3911 && TREE_CODE (otype
) == INTEGER_TYPE
3912 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3914 /* Don't warn about converting 0 to pointer,
3915 provided the 0 was explicit--not cast or made by folding. */
3916 && !(TREE_CODE (value
) == INTEGER_CST
&& integer_zerop (value
))
3918 /* Don't warn about converting any constant. */
3919 && !TREE_CONSTANT (value
))
3920 warning ("cast to pointer from integer of different size");
3922 value
= convert (type
, value
);
3924 /* Ignore any integer overflow caused by the cast. */
3925 if (TREE_CODE (value
) == INTEGER_CST
)
3926 TREE_CONSTANT_OVERFLOW (value
) = 0;
3929 if (value
== expr
&& pedantic
)
3931 /* If pedantic, don't let a cast be an lvalue. */
3932 return non_lvalue (value
);
3937 /* Build an assignment expression of lvalue LHS from value RHS.
3938 MODIFYCODE is the code for a binary operator that we use
3939 to combine the old value of LHS with RHS to get the new value.
3940 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3943 build_modify_expr (lhs
, modifycode
, rhs
)
3945 enum tree_code modifycode
;
3947 register tree result
;
3949 tree lhstype
= TREE_TYPE (lhs
);
3950 tree olhstype
= lhstype
;
3952 /* Types that aren't fully specified cannot be used in assignments. */
3953 lhs
= require_complete_type (lhs
);
3955 /* Avoid duplicate error messages from operands that had errors. */
3956 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
3957 return error_mark_node
;
3959 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3960 /* Do not use STRIP_NOPS here. We do not want an enumerator
3961 whose value is 0 to count as a null pointer constant. */
3962 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3963 rhs
= TREE_OPERAND (rhs
, 0);
3967 /* Handle control structure constructs used as "lvalues". */
3969 switch (TREE_CODE (lhs
))
3971 /* Handle (a, b) used as an "lvalue". */
3973 pedantic_lvalue_warning (COMPOUND_EXPR
);
3974 return build (COMPOUND_EXPR
, lhstype
,
3975 TREE_OPERAND (lhs
, 0),
3976 build_modify_expr (TREE_OPERAND (lhs
, 1),
3979 /* Handle (a ? b : c) used as an "lvalue". */
3981 pedantic_lvalue_warning (COND_EXPR
);
3982 rhs
= save_expr (rhs
);
3984 /* Produce (a ? (b = rhs) : (c = rhs))
3985 except that the RHS goes through a save-expr
3986 so the code to compute it is only emitted once. */
3988 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
3989 build_modify_expr (TREE_OPERAND (lhs
, 1),
3991 build_modify_expr (TREE_OPERAND (lhs
, 2),
3993 /* Make sure the code to compute the rhs comes out
3994 before the split. */
3995 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
3996 /* But cast it to void to avoid an "unused" error. */
3997 convert (void_type_node
, rhs
), cond
);
4001 /* If a binary op has been requested, combine the old LHS value with the RHS
4002 producing the value we should actually store into the LHS. */
4004 if (modifycode
!= NOP_EXPR
)
4006 lhs
= stabilize_reference (lhs
);
4007 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
4010 /* Handle a cast used as an "lvalue".
4011 We have already performed any binary operator using the value as cast.
4012 Now convert the result to the cast type of the lhs,
4013 and then true type of the lhs and store it there;
4014 then convert result back to the cast type to be the value
4015 of the assignment. */
4017 switch (TREE_CODE (lhs
))
4022 case FIX_TRUNC_EXPR
:
4023 case FIX_FLOOR_EXPR
:
4024 case FIX_ROUND_EXPR
:
4026 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
4027 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
)
4028 newrhs
= default_conversion (newrhs
);
4030 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
4032 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
4033 convert (TREE_TYPE (inner_lhs
),
4034 convert (lhstype
, newrhs
)));
4035 pedantic_lvalue_warning (CONVERT_EXPR
);
4036 return convert (TREE_TYPE (lhs
), result
);
4040 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
4041 Reject anything strange now. */
4043 if (!lvalue_or_else (lhs
, "assignment"))
4044 return error_mark_node
;
4046 /* Warn about storing in something that is `const'. */
4048 if (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
4049 || ((TREE_CODE (lhstype
) == RECORD_TYPE
4050 || TREE_CODE (lhstype
) == UNION_TYPE
)
4051 && C_TYPE_FIELDS_READONLY (lhstype
)))
4052 readonly_warning (lhs
, "assignment");
4054 /* If storing into a structure or union member,
4055 it has probably been given type `int'.
4056 Compute the type that would go with
4057 the actual amount of storage the member occupies. */
4059 if (TREE_CODE (lhs
) == COMPONENT_REF
4060 && (TREE_CODE (lhstype
) == INTEGER_TYPE
4061 || TREE_CODE (lhstype
) == REAL_TYPE
4062 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
4063 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
4065 /* If storing in a field that is in actuality a short or narrower than one,
4066 we must store in the field in its actual type. */
4068 if (lhstype
!= TREE_TYPE (lhs
))
4070 lhs
= copy_node (lhs
);
4071 TREE_TYPE (lhs
) = lhstype
;
4074 /* Convert new value to destination type. */
4076 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
4077 NULL_TREE
, NULL_TREE
, 0);
4078 if (TREE_CODE (newrhs
) == ERROR_MARK
)
4079 return error_mark_node
;
4081 result
= build (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
4082 TREE_SIDE_EFFECTS (result
) = 1;
4084 /* If we got the LHS in a different type for storing in,
4085 convert the result back to the nominal type of LHS
4086 so that the value we return always has the same type
4087 as the LHS argument. */
4089 if (olhstype
== TREE_TYPE (result
))
4091 return convert_for_assignment (olhstype
, result
, "assignment",
4092 NULL_TREE
, NULL_TREE
, 0);
4095 /* Convert value RHS to type TYPE as preparation for an assignment
4096 to an lvalue of type TYPE.
4097 The real work of conversion is done by `convert'.
4098 The purpose of this function is to generate error messages
4099 for assignments that are not allowed in C.
4100 ERRTYPE is a string to use in error messages:
4101 "assignment", "return", etc. If it is null, this is parameter passing
4102 for a function call (and different error messages are output). Otherwise,
4103 it may be a name stored in the spelling stack and interpreted by
4106 FUNNAME is the name of the function being called,
4107 as an IDENTIFIER_NODE, or null.
4108 PARMNUM is the number of the argument, for printing in error messages. */
4111 convert_for_assignment (type
, rhs
, errtype
, fundecl
, funname
, parmnum
)
4114 tree fundecl
, funname
;
4117 register enum tree_code codel
= TREE_CODE (type
);
4118 register tree rhstype
;
4119 register enum tree_code coder
;
4121 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4122 /* Do not use STRIP_NOPS here. We do not want an enumerator
4123 whose value is 0 to count as a null pointer constant. */
4124 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
4125 rhs
= TREE_OPERAND (rhs
, 0);
4127 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
4128 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
)
4129 rhs
= default_conversion (rhs
);
4131 rhstype
= TREE_TYPE (rhs
);
4132 coder
= TREE_CODE (rhstype
);
4134 if (coder
== ERROR_MARK
)
4135 return error_mark_node
;
4137 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
4139 overflow_warning (rhs
);
4140 /* Check for Objective-C protocols. This will issue a warning if
4141 there are protocol violations. No need to use the return value. */
4142 maybe_objc_comptypes (type
, rhstype
, 0);
4146 if (coder
== VOID_TYPE
)
4148 error ("void value not ignored as it ought to be");
4149 return error_mark_node
;
4151 /* Arithmetic types all interconvert, and enum is treated like int. */
4152 if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
|| codel
== ENUMERAL_TYPE
4153 || codel
== COMPLEX_TYPE
)
4155 (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
|| coder
== ENUMERAL_TYPE
4156 || codel
== COMPLEX_TYPE
))
4157 /* Don't use convert_and_check here. If the input has type int
4158 and did not overflow, and we are converting it here to a short,
4159 we don't want an error. A warning would be okay, but it's too risky now
4160 to add an option to convert_and_check to get just warnings. */
4161 return convert (type
, rhs
);
4162 /* Conversion to a union from its member types. */
4163 else if (codel
== UNION_TYPE
)
4166 for (memb_types
= TYPE_FIELDS (type
); memb_types
;
4167 memb_types
= TREE_CHAIN (memb_types
))
4169 if (comptypes (TREE_TYPE (memb_types
), TREE_TYPE (rhs
)))
4172 && !(fundecl
!= 0 && DECL_IN_SYSTEM_HEADER (fundecl
)))
4173 pedwarn ("ANSI C prohibits argument conversion to union type");
4174 return build1 (NOP_EXPR
, type
, rhs
);
4178 /* Conversions among pointers */
4179 else if (codel
== POINTER_TYPE
&& coder
== POINTER_TYPE
)
4181 register tree ttl
= TREE_TYPE (type
);
4182 register tree ttr
= TREE_TYPE (rhstype
);
4184 /* Any non-function converts to a [const][volatile] void *
4185 and vice versa; otherwise, targets must be the same.
4186 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4187 if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
4188 || TYPE_MAIN_VARIANT (ttr
) == void_type_node
4189 || comp_target_types (type
, rhstype
)
4190 || (!pedantic
/* Unless pedantic, mix signed and unsigned. */
4191 && TREE_CODE (ttl
) == INTEGER_TYPE
4192 && TREE_CODE (ttr
) == INTEGER_TYPE
4193 && TYPE_PRECISION (ttl
) == TYPE_PRECISION (ttr
)))
4196 && ((TYPE_MAIN_VARIANT (ttl
) == void_type_node
4197 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
4199 (TYPE_MAIN_VARIANT (ttr
) == void_type_node
4200 && !integer_zerop (rhs
)
4201 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
4202 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4203 get_spelling (errtype
), funname
, parmnum
);
4204 /* Const and volatile mean something different for function types,
4205 so the usual warnings are not appropriate. */
4206 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
4207 || TREE_CODE (ttl
) != FUNCTION_TYPE
)
4209 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
4210 warn_for_assignment ("%s discards `const' from pointer target type",
4211 get_spelling (errtype
), funname
, parmnum
);
4212 if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
4213 warn_for_assignment ("%s discards `volatile' from pointer target type",
4214 get_spelling (errtype
), funname
, parmnum
);
4218 /* Because const and volatile on functions are restrictions
4219 that say the function will not do certain things,
4220 it is okay to use a const or volatile function
4221 where an ordinary one is wanted, but not vice-versa. */
4222 if (TYPE_READONLY (ttl
) && ! TYPE_READONLY (ttr
))
4223 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4224 get_spelling (errtype
), funname
, parmnum
);
4225 if (TYPE_VOLATILE (ttl
) && ! TYPE_VOLATILE (ttr
))
4226 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4227 get_spelling (errtype
), funname
, parmnum
);
4230 else if (unsigned_type (TYPE_MAIN_VARIANT (ttl
))
4231 == unsigned_type (TYPE_MAIN_VARIANT (ttr
)))
4232 warn_for_assignment ("pointer targets in %s differ in signedness",
4233 get_spelling (errtype
), funname
, parmnum
);
4235 warn_for_assignment ("%s from incompatible pointer type",
4236 get_spelling (errtype
), funname
, parmnum
);
4237 return convert (type
, rhs
);
4239 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
4241 /* An explicit constant 0 can convert to a pointer,
4242 but not a 0 that results from casting or folding. */
4243 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
)))
4245 warn_for_assignment ("%s makes pointer from integer without a cast",
4246 get_spelling (errtype
), funname
, parmnum
);
4247 return convert (type
, rhs
);
4249 return null_pointer_node
;
4251 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
4253 warn_for_assignment ("%s makes integer from pointer without a cast",
4254 get_spelling (errtype
), funname
, parmnum
);
4255 return convert (type
, rhs
);
4262 tree selector
= maybe_building_objc_message_expr ();
4264 if (selector
&& parmnum
> 2)
4265 error ("incompatible type for argument %d of `%s'",
4266 parmnum
- 2, IDENTIFIER_POINTER (selector
));
4268 error ("incompatible type for argument %d of `%s'",
4269 parmnum
, IDENTIFIER_POINTER (funname
));
4272 error ("incompatible type for argument %d of indirect function call",
4276 error ("incompatible types in %s", get_spelling (errtype
));
4278 return error_mark_node
;
4281 /* Print a warning using MSG.
4282 It gets OPNAME as its one parameter.
4283 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4284 FUNCTION and ARGNUM are handled specially if we are building an
4285 Objective-C selector. */
4288 warn_for_assignment (msg
, opname
, function
, argnum
)
4294 static char argstring
[] = "passing arg %d of `%s'";
4295 static char argnofun
[] = "passing arg %d";
4299 tree selector
= maybe_building_objc_message_expr ();
4301 if (selector
&& argnum
> 2)
4303 function
= selector
;
4308 /* Function name is known; supply it. */
4309 opname
= (char *) alloca (IDENTIFIER_LENGTH (function
)
4310 + sizeof (argstring
) + 25 /*%d*/ + 1);
4311 sprintf (opname
, argstring
, argnum
, IDENTIFIER_POINTER (function
));
4315 /* Function name unknown (call through ptr); just give arg number. */
4316 opname
= (char *) alloca (sizeof (argnofun
) + 25 /*%d*/ + 1);
4317 sprintf (opname
, argnofun
, argnum
);
4320 pedwarn (msg
, opname
);
4323 /* Return nonzero if VALUE is a valid constant-valued expression
4324 for use in initializing a static variable; one that can be an
4325 element of a "constant" initializer.
4327 Return null_pointer_node if the value is absolute;
4328 if it is relocatable, return the variable that determines the relocation.
4329 We assume that VALUE has been folded as much as possible;
4330 therefore, we do not need to check for such things as
4331 arithmetic-combinations of integers. */
4334 initializer_constant_valid_p (value
, endtype
)
4338 switch (TREE_CODE (value
))
4341 return TREE_STATIC (value
) ? null_pointer_node
: 0;
4347 return null_pointer_node
;
4350 return TREE_OPERAND (value
, 0);
4352 case NON_LVALUE_EXPR
:
4353 return initializer_constant_valid_p (TREE_OPERAND (value
, 0), endtype
);
4357 /* Allow conversions between pointer types. */
4358 if (TREE_CODE (TREE_TYPE (value
)) == POINTER_TYPE
4359 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == POINTER_TYPE
)
4360 return initializer_constant_valid_p (TREE_OPERAND (value
, 0), endtype
);
4361 /* Allow conversions between real types. */
4362 if (TREE_CODE (TREE_TYPE (value
)) == REAL_TYPE
4363 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == REAL_TYPE
)
4364 return initializer_constant_valid_p (TREE_OPERAND (value
, 0), endtype
);
4365 /* Allow length-preserving conversions between integer types. */
4366 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
4367 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == INTEGER_TYPE
4368 && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value
)),
4369 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value
, 0)))))
4370 return initializer_constant_valid_p (TREE_OPERAND (value
, 0), endtype
);
4371 /* Allow conversions between integer types only if explicit value. */
4372 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
4373 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == INTEGER_TYPE
)
4375 tree inner
= initializer_constant_valid_p (TREE_OPERAND (value
, 0),
4377 if (inner
== null_pointer_node
)
4378 return null_pointer_node
;
4381 /* Allow (int) &foo provided int is as wide as a pointer. */
4382 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
4383 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == POINTER_TYPE
4384 && ! tree_int_cst_lt (TYPE_SIZE (TREE_TYPE (value
)),
4385 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value
, 0)))))
4386 return initializer_constant_valid_p (TREE_OPERAND (value
, 0),
4388 /* Allow conversions to union types if the value inside is okay. */
4389 if (TREE_CODE (TREE_TYPE (value
)) == UNION_TYPE
)
4390 return initializer_constant_valid_p (TREE_OPERAND (value
, 0),
4395 if (TREE_CODE (endtype
) == INTEGER_TYPE
4396 && TYPE_PRECISION (endtype
) < POINTER_SIZE
)
4399 tree valid0
= initializer_constant_valid_p (TREE_OPERAND (value
, 0),
4401 tree valid1
= initializer_constant_valid_p (TREE_OPERAND (value
, 1),
4403 /* If either term is absolute, use the other terms relocation. */
4404 if (valid0
== null_pointer_node
)
4406 if (valid1
== null_pointer_node
)
4412 if (TREE_CODE (endtype
) == INTEGER_TYPE
4413 && TYPE_PRECISION (endtype
) < POINTER_SIZE
)
4416 tree valid0
= initializer_constant_valid_p (TREE_OPERAND (value
, 0),
4418 tree valid1
= initializer_constant_valid_p (TREE_OPERAND (value
, 1),
4420 /* Win if second argument is absolute. */
4421 if (valid1
== null_pointer_node
)
4423 /* Win if both arguments have the same relocation.
4424 Then the value is absolute. */
4425 if (valid0
== valid1
)
4426 return null_pointer_node
;
4434 /* Perform appropriate conversions on the initial value of a variable,
4435 store it in the declaration DECL,
4436 and print any error messages that are appropriate.
4437 If the init is invalid, store an ERROR_MARK. */
4440 store_init_value (decl
, init
)
4443 register tree value
, type
;
4445 /* If variable's type was invalidly declared, just ignore it. */
4447 type
= TREE_TYPE (decl
);
4448 if (TREE_CODE (type
) == ERROR_MARK
)
4451 /* Digest the specified initializer into an expression. */
4453 value
= digest_init (type
, init
, NULL_PTR
, TREE_STATIC (decl
),
4454 TREE_STATIC (decl
) || pedantic
,
4455 IDENTIFIER_POINTER (DECL_NAME (decl
)));
4457 /* Store the expression if valid; else report error. */
4460 /* Note that this is the only place we can detect the error
4461 in a case such as struct foo bar = (struct foo) { x, y };
4462 where there is one initial value which is a constructor expression. */
4463 if (value
== error_mark_node
)
4465 else if (TREE_STATIC (decl
) && ! TREE_CONSTANT (value
))
4467 error ("initializer for static variable is not constant");
4468 value
= error_mark_node
;
4470 else if (TREE_STATIC (decl
)
4471 && initializer_constant_valid_p (value
, TREE_TYPE (value
)) == 0)
4473 error ("initializer for static variable uses complicated arithmetic");
4474 value
= error_mark_node
;
4478 if (pedantic
&& TREE_CODE (value
) == CONSTRUCTOR
)
4480 if (! TREE_CONSTANT (value
))
4481 pedwarn ("aggregate initializer is not constant");
4482 else if (! TREE_STATIC (value
))
4483 pedwarn ("aggregate initializer uses complicated arithmetic");
4488 /* ANSI wants warnings about out-of-range constant initializers. */
4489 constant_expression_warning (value
);
4491 DECL_INITIAL (decl
) = value
;
4494 /* Methods for storing and printing names for error messages. */
4496 /* Implement a spelling stack that allows components of a name to be pushed
4497 and popped. Each element on the stack is this structure. */
4509 #define SPELLING_STRING 1
4510 #define SPELLING_MEMBER 2
4511 #define SPELLING_BOUNDS 3
4513 static struct spelling
*spelling
; /* Next stack element (unused). */
4514 static struct spelling
*spelling_base
; /* Spelling stack base. */
4515 static int spelling_size
; /* Size of the spelling stack. */
4517 /* Macros to save and restore the spelling stack around push_... functions.
4518 Alternative to SAVE_SPELLING_STACK. */
4520 #define SPELLING_DEPTH() (spelling - spelling_base)
4521 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4523 /* Save and restore the spelling stack around arbitrary C code. */
4525 #define SAVE_SPELLING_DEPTH(code) \
4527 int __depth = SPELLING_DEPTH (); \
4529 RESTORE_SPELLING_DEPTH (__depth); \
4532 /* Push an element on the spelling stack with type KIND and assign VALUE
4535 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4537 int depth = SPELLING_DEPTH (); \
4539 if (depth >= spelling_size) \
4541 spelling_size += 10; \
4542 if (spelling_base == 0) \
4544 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4547 = (struct spelling *) xrealloc (spelling_base, \
4548 spelling_size * sizeof (struct spelling)); \
4549 RESTORE_SPELLING_DEPTH (depth); \
4552 spelling->kind = (KIND); \
4553 spelling->MEMBER = (VALUE); \
4557 /* Push STRING on the stack. Printed literally. */
4560 push_string (string
)
4563 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
4566 /* Push a member name on the stack. Printed as '.' STRING. */
4569 push_member_name (string
)
4572 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
4575 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4578 push_array_bounds (bounds
)
4581 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
4584 /* Compute the maximum size in bytes of the printed spelling. */
4589 register int size
= 0;
4590 register struct spelling
*p
;
4592 for (p
= spelling_base
; p
< spelling
; p
++)
4594 if (p
->kind
== SPELLING_BOUNDS
)
4597 size
+= strlen (p
->u
.s
) + 1;
4603 /* Print the spelling to BUFFER and return it. */
4606 print_spelling (buffer
)
4607 register char *buffer
;
4609 register char *d
= buffer
;
4611 register struct spelling
*p
;
4613 for (p
= spelling_base
; p
< spelling
; p
++)
4614 if (p
->kind
== SPELLING_BOUNDS
)
4616 sprintf (d
, "[%d]", p
->u
.i
);
4621 if (p
->kind
== SPELLING_MEMBER
)
4623 for (s
= p
->u
.s
; *d
= *s
++; d
++)
4630 /* Provide a means to pass component names derived from the spelling stack. */
4632 char initialization_message
;
4634 /* Interpret the spelling of the given ERRTYPE message. */
4637 get_spelling (errtype
)
4640 static char *buffer
;
4641 static int size
= -1;
4643 if (errtype
== &initialization_message
)
4645 /* Avoid counting chars */
4646 static char message
[] = "initialization of `%s'";
4647 register int needed
= sizeof (message
) + spelling_length () + 1;
4651 buffer
= (char *) xmalloc (size
= needed
);
4653 buffer
= (char *) xrealloc (buffer
, size
= needed
);
4655 temp
= (char *) alloca (needed
);
4656 sprintf (buffer
, message
, print_spelling (temp
));
4663 /* Issue an error message for a bad initializer component.
4664 FORMAT describes the message. OFWHAT is the name for the component.
4665 LOCAL is a format string for formatting the insertion of the name
4668 If OFWHAT is null, the component name is stored on the spelling stack.
4669 If the component name is a null string, then LOCAL is omitted entirely. */
4672 error_init (format
, local
, ofwhat
)
4673 char *format
, *local
, *ofwhat
;
4678 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
4679 buffer
= (char *) alloca (strlen (local
) + strlen (ofwhat
) + 2);
4682 sprintf (buffer
, local
, ofwhat
);
4686 error (format
, buffer
);
4689 /* Issue a pedantic warning for a bad initializer component.
4690 FORMAT describes the message. OFWHAT is the name for the component.
4691 LOCAL is a format string for formatting the insertion of the name
4694 If OFWHAT is null, the component name is stored on the spelling stack.
4695 If the component name is a null string, then LOCAL is omitted entirely. */
4698 pedwarn_init (format
, local
, ofwhat
)
4699 char *format
, *local
, *ofwhat
;
4704 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
4705 buffer
= (char *) alloca (strlen (local
) + strlen (ofwhat
) + 2);
4708 sprintf (buffer
, local
, ofwhat
);
4712 pedwarn (format
, buffer
);
4715 /* Keep a pointer to the last free TREE_LIST node as we digest an initializer,
4716 so that we can reuse it. This is set in digest_init, and used in
4717 process_init_constructor.
4719 We will never keep more than one free TREE_LIST node here. This is for
4720 two main reasons. First, we take elements off the old list and add them
4721 to the new list one at a time, thus there should never be more than
4722 one free TREE_LIST at a time, and thus even if there is, we will never
4723 need more than one. Secondly, to avoid dangling pointers to freed obstacks,
4724 we want to always ensure that we have either a pointer to a valid TREE_LIST
4725 within the current initializer, or else a pointer to null. */
4727 static tree free_tree_list
= NULL_TREE
;
4729 /* Digest the parser output INIT as an initializer for type TYPE.
4730 Return a C expression of type TYPE to represent the initial value.
4732 If TAIL is nonzero, it points to a variable holding a list of elements
4733 of which INIT is the first. We update the list stored there by
4734 removing from the head all the elements that we use.
4735 Normally this is only one; we use more than one element only if
4736 TYPE is an aggregate and INIT is not a constructor.
4738 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4739 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4740 applies only to elements of constructors.
4742 If OFWHAT is nonnull, it specifies what we are initializing, for error
4743 messages. Examples: variable name, variable.member, array[44].
4744 If OFWHAT is null, the component name is stored on the spelling stack.
4745 (That is true for all nested calls to digest_init.) */
4748 digest_init (type
, init
, tail
, require_constant
, constructor_constant
, ofwhat
)
4749 tree type
, init
, *tail
;
4750 int require_constant
, constructor_constant
;
4753 enum tree_code code
= TREE_CODE (type
);
4755 tree old_tail_contents
;
4756 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
4757 tree node which has no TREE_TYPE. */
4759 = TREE_CODE (init
) == CONSTRUCTOR
&& TREE_TYPE (init
) == 0;
4760 tree inside_init
= init
;
4762 /* Make sure there is just one "partially bracketed" message
4763 per top-level initializer or constructor. */
4765 partial_bracket_mentioned
= 0;
4767 /* By default, assume we use one element from a list.
4768 We correct this later in the cases where it is not true.
4770 Thus, we update TAIL now to point to the next element, and save the
4771 old value in OLD_TAIL_CONTENTS. If we didn't actually use the first
4772 element, then we will reset TAIL before proceeding. FREE_TREE_LIST
4773 is handled similarly. */
4777 old_tail_contents
= *tail
;
4778 *tail
= TREE_CHAIN (*tail
);
4779 free_tree_list
= old_tail_contents
;
4784 if (init
== error_mark_node
)
4787 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4788 /* Do not use STRIP_NOPS here. We do not want an enumerator
4789 whose value is 0 to count as a null pointer constant. */
4790 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
4791 inside_init
= TREE_OPERAND (init
, 0);
4793 if (inside_init
&& raw_constructor
4794 && CONSTRUCTOR_ELTS (inside_init
) != 0
4795 && TREE_CHAIN (CONSTRUCTOR_ELTS (inside_init
)) == 0)
4797 element
= TREE_VALUE (CONSTRUCTOR_ELTS (inside_init
));
4798 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4799 if (element
&& TREE_CODE (element
) == NON_LVALUE_EXPR
)
4800 element
= TREE_OPERAND (element
, 0);
4803 /* Initialization of an array of chars from a string constant
4804 optionally enclosed in braces. */
4806 if (code
== ARRAY_TYPE
)
4808 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
4809 if ((typ1
== char_type_node
4810 || typ1
== signed_char_type_node
4811 || typ1
== unsigned_char_type_node
4812 || typ1
== unsigned_wchar_type_node
4813 || typ1
== signed_wchar_type_node
)
4814 && ((inside_init
&& TREE_CODE (inside_init
) == STRING_CST
)
4815 || (element
&& TREE_CODE (element
) == STRING_CST
)))
4817 tree string
= element
? element
: inside_init
;
4819 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string
)))
4821 && TYPE_PRECISION (typ1
) == TYPE_PRECISION (char_type_node
))
4823 error_init ("char-array%s initialized from wide string",
4825 return error_mark_node
;
4827 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string
)))
4829 && TYPE_PRECISION (typ1
) != TYPE_PRECISION (char_type_node
))
4831 error_init ("int-array%s initialized from non-wide string",
4833 return error_mark_node
;
4836 TREE_TYPE (string
) = type
;
4837 if (TYPE_DOMAIN (type
) != 0
4838 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
4840 register int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
4841 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
4842 /* Subtract 1 (or sizeof (wchar_t))
4843 because it's ok to ignore the terminating null char
4844 that is counted in the length of the constant. */
4845 if (size
< TREE_STRING_LENGTH (string
)
4846 - (TYPE_PRECISION (typ1
) != TYPE_PRECISION (char_type_node
)
4847 ? TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
4850 "initializer-string for array of chars%s is too long",
4857 /* Any type except an array can be initialized
4858 from an expression of the same type, optionally with braces.
4859 For an array, this is allowed only for a string constant. */
4861 if (inside_init
&& TREE_TYPE (inside_init
) != 0
4862 && ((TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
))
4863 == TYPE_MAIN_VARIANT (type
))
4864 || (code
== ARRAY_TYPE
4865 && comptypes (TREE_TYPE (inside_init
), type
))
4866 || (code
== POINTER_TYPE
4867 && (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
4868 || TREE_CODE (TREE_TYPE (inside_init
)) == FUNCTION_TYPE
)
4869 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
4870 TREE_TYPE (type
)))))
4872 if (code
== POINTER_TYPE
4873 && (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
4874 || TREE_CODE (TREE_TYPE (inside_init
)) == FUNCTION_TYPE
))
4875 inside_init
= default_conversion (inside_init
);
4876 else if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
)
4878 error_init ("array%s initialized from non-constant array expression",
4880 return error_mark_node
;
4883 if (optimize
&& TREE_READONLY (inside_init
)
4884 && TREE_CODE (inside_init
) == VAR_DECL
)
4885 inside_init
= decl_constant_value (inside_init
);
4887 if (require_constant
&& ! TREE_CONSTANT (inside_init
))
4889 error_init ("initializer element%s is not constant",
4890 " for `%s'", ofwhat
);
4891 inside_init
= error_mark_node
;
4893 else if (require_constant
4894 && initializer_constant_valid_p (inside_init
, TREE_TYPE (inside_init
)) == 0)
4896 error_init ("initializer element%s is not computable at load time",
4897 " for `%s'", ofwhat
);
4898 inside_init
= error_mark_node
;
4904 if (element
&& (TREE_TYPE (element
) == type
4905 || (code
== ARRAY_TYPE
&& TREE_TYPE (element
)
4906 && comptypes (TREE_TYPE (element
), type
))))
4908 if (code
== ARRAY_TYPE
)
4910 error_init ("array%s initialized from non-constant array expression",
4912 return error_mark_node
;
4914 if (pedantic
&& (code
== RECORD_TYPE
|| code
== UNION_TYPE
))
4915 pedwarn ("single-expression nonscalar initializer has braces");
4916 if (optimize
&& TREE_READONLY (element
) && TREE_CODE (element
) == VAR_DECL
)
4917 element
= decl_constant_value (element
);
4919 if (require_constant
&& ! TREE_CONSTANT (element
))
4921 error_init ("initializer element%s is not constant",
4922 " for `%s'", ofwhat
);
4923 element
= error_mark_node
;
4925 else if (require_constant
4926 && initializer_constant_valid_p (element
, TREE_TYPE (element
)) == 0)
4928 error_init ("initializer element%s is not computable at load time",
4929 " for `%s'", ofwhat
);
4930 element
= error_mark_node
;
4936 /* Check for initializing a union by its first field.
4937 Such an initializer must use braces. */
4939 if (code
== UNION_TYPE
)
4942 tree field
= TYPE_FIELDS (type
);
4944 /* Find the first named field. ANSI decided in September 1990
4945 that only named fields count here. */
4946 while (field
&& DECL_NAME (field
) == 0)
4947 field
= TREE_CHAIN (field
);
4951 error_init ("union%s with no named members cannot be initialized",
4953 return error_mark_node
;
4956 if (raw_constructor
)
4957 result
= process_init_constructor (type
, inside_init
, NULL_PTR
,
4959 constructor_constant
, ofwhat
);
4962 *tail
= old_tail_contents
;
4963 free_tree_list
= NULL_TREE
;
4964 result
= process_init_constructor (type
, NULL_TREE
, tail
,
4966 constructor_constant
, ofwhat
);
4975 /* Handle scalar types, including conversions. */
4977 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== POINTER_TYPE
4978 || code
== ENUMERAL_TYPE
|| code
== COMPLEX_TYPE
)
4980 if (raw_constructor
)
4985 "initializer for scalar%s requires one element",
4987 return error_mark_node
;
4991 /* Deal with extra levels of {...}. */
4992 if (TREE_CODE (element
) == CONSTRUCTOR
4993 && TREE_TYPE (element
) == 0)
4996 "initializer for scalar%s requires one element",
4998 return error_mark_node
;
5000 inside_init
= element
;
5004 #if 0 /* A non-raw constructor is an actual expression. */
5005 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
5007 error_init ("initializer for scalar%s has extra braces",
5009 return error_mark_node
;
5016 push_string (ofwhat
);
5017 if (!raw_constructor
)
5019 /* Note that convert_for_assignment calls default_conversion
5020 for arrays and functions. We must not call it in the
5021 case where inside_init is a null pointer constant. */
5023 = convert_for_assignment (type
, inside_init
,
5024 &initialization_message
,
5025 NULL_TREE
, NULL_TREE
, 0);
5028 if (require_constant
&& ! TREE_CONSTANT (inside_init
))
5030 error_init ("initializer element%s is not constant",
5031 " for `%s'", ofwhat
);
5032 inside_init
= error_mark_node
;
5034 else if (require_constant
5035 && initializer_constant_valid_p (inside_init
, TREE_TYPE (inside_init
)) == 0)
5037 error_init ("initializer element%s is not computable at load time",
5038 " for `%s'", ofwhat
);
5039 inside_init
= error_mark_node
;
5045 /* Come here only for records and arrays. */
5047 if (TYPE_SIZE (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
5049 error_init ("variable-sized object%s may not be initialized",
5051 return error_mark_node
;
5054 if (code
== ARRAY_TYPE
|| code
== RECORD_TYPE
)
5056 if (raw_constructor
)
5057 return process_init_constructor (type
, inside_init
,
5058 NULL_PTR
, constructor_constant
,
5059 constructor_constant
, ofwhat
);
5062 *tail
= old_tail_contents
;
5063 free_tree_list
= NULL_TREE
;
5064 return process_init_constructor (type
, NULL_TREE
, tail
,
5065 constructor_constant
,
5066 constructor_constant
, ofwhat
);
5068 else if (flag_traditional
)
5069 /* Traditionally one can say `char x[100] = 0;'. */
5070 return process_init_constructor (type
,
5071 build_nt (CONSTRUCTOR
, NULL_TREE
,
5072 tree_cons (NULL_TREE
,
5075 NULL_PTR
, constructor_constant
,
5076 constructor_constant
, ofwhat
);
5079 error_init ("invalid initializer%s", " for `%s'", ofwhat
);
5080 return error_mark_node
;
5083 /* Process a constructor for a variable of type TYPE.
5084 The constructor elements may be specified either with INIT or with ELTS,
5085 only one of which should be non-null.
5087 If INIT is specified, it is a CONSTRUCTOR node which is specifically
5088 and solely for initializing this datum.
5090 If ELTS is specified, it is the address of a variable containing
5091 a list of expressions. We take as many elements as we need
5092 from the head of the list and update the list.
5094 In the resulting constructor, TREE_CONSTANT is set if all elts are
5095 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
5096 constants that the assembler and linker can compute them.
5098 The argument CONSTANT_VALUE says to print an error if either the
5099 value or any element is not a constant.
5101 The argument CONSTANT_ELEMENT says to print an error if an element
5102 of an aggregate is not constant. It does not apply to a value
5103 which is not a constructor.
5105 OFWHAT is a character string describing the object being initialized,
5106 for error messages. It might be "variable" or "variable.member"
5107 or "variable[17].member[5]".
5109 If OFWHAT is null, the description string is stored on the spelling
5110 stack. That is always true for recursive calls. */
5113 process_init_constructor (type
, init
, elts
, constant_value
, constant_element
,
5115 tree type
, init
, *elts
;
5116 int constant_value
, constant_element
;
5120 /* List of the elements of the result constructor,
5121 in reverse order. */
5122 register tree members
= NULL
;
5124 int allconstant
= 1;
5127 int depth
= SPELLING_DEPTH ();
5130 push_string (ofwhat
);
5132 /* Make TAIL be the list of elements to use for the initialization,
5133 no matter how the data was given to us. */
5137 if (warn_missing_braces
)
5139 if (! partial_bracket_mentioned
)
5140 warning ("aggregate has a partly bracketed initializer");
5141 partial_bracket_mentioned
= 1;
5146 tail
= CONSTRUCTOR_ELTS (init
);
5148 /* Gobble as many elements as needed, and make a constructor or initial value
5149 for each element of this aggregate. Chain them together in result.
5150 If there are too few, use 0 for each scalar ultimate component. */
5152 if (TREE_CODE (type
) == ARRAY_TYPE
)
5154 tree min_index
, max_index
;
5155 /* These are non-zero only within a range initializer. */
5156 tree start_index
= 0, end_index
= 0;
5157 /* Within a range, this is the value for the elts in the range. */
5159 /* Do arithmetic using double integers, but don't use fold/build,
5160 because these allocate a new tree object everytime they are called,
5161 thus resulting in gcc using too much memory for large
5163 union tree_node current_index_node
, members_index_node
;
5164 tree current_index
= ¤t_index_node
;
5165 tree members_index
= &members_index_node
;
5166 TREE_TYPE (current_index
) = integer_type_node
;
5167 TREE_TYPE (members_index
) = integer_type_node
;
5169 /* If we have array bounds, set our bounds from that. Otherwise,
5170 we have a lower bound of zero and an unknown upper bound. */
5171 if (TYPE_DOMAIN (type
))
5173 min_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
5174 max_index
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
5178 min_index
= integer_zero_node
;
5182 TREE_INT_CST_LOW (members_index
) = TREE_INT_CST_LOW (min_index
);
5183 TREE_INT_CST_HIGH (members_index
) = TREE_INT_CST_HIGH (min_index
);
5185 /* Don't leave the loop based on index if the next item has an explicit
5186 index value that will override it. */
5188 for (TREE_INT_CST_LOW (current_index
) = TREE_INT_CST_LOW (min_index
),
5189 TREE_INT_CST_HIGH (current_index
) = TREE_INT_CST_HIGH (min_index
);
5190 tail
!= 0 || end_index
;
5191 add_double (TREE_INT_CST_LOW (current_index
),
5192 TREE_INT_CST_HIGH (current_index
), 1, 0,
5193 &TREE_INT_CST_LOW (current_index
),
5194 &TREE_INT_CST_HIGH (current_index
)))
5196 register tree next1
= 0;
5198 /* Handle the case where we are inside of a range.
5199 current_index increments through the range,
5200 so just keep reusing the same element of TAIL
5201 until the end of the range. */
5205 if (!tree_int_cst_lt (current_index
, end_index
))
5209 /* If this element specifies an index,
5210 move to that index before storing it in the new list. */
5211 else if (TREE_PURPOSE (tail
) != 0)
5214 tree index
= TREE_PURPOSE (tail
);
5216 if (index
&& (TREE_CODE (index
) == NON_LVALUE_EXPR
5217 || TREE_CODE (index
) == NOP_EXPR
))
5218 index
= TREE_OPERAND (index
, 0);
5220 /* Begin a range. */
5221 if (TREE_CODE (index
) == TREE_LIST
)
5223 start_index
= TREE_PURPOSE (index
);
5224 end_index
= TREE_PURPOSE (TREE_CHAIN (index
));
5226 /* Expose constants. It Doesn't matter if we change
5229 && (TREE_CODE (end_index
) == NON_LVALUE_EXPR
5230 || TREE_CODE (end_index
) == NOP_EXPR
))
5231 end_index
= TREE_OPERAND (end_index
, 0);
5233 && (TREE_CODE (start_index
) == NON_LVALUE_EXPR
5234 || TREE_CODE (start_index
) == NOP_EXPR
))
5235 start_index
= TREE_OPERAND (start_index
, 0);
5237 constant_expression_warning (start_index
);
5238 constant_expression_warning (end_index
);
5240 if ((TREE_CODE (start_index
) == IDENTIFIER_NODE
)
5241 || (TREE_CODE (end_index
) == IDENTIFIER_NODE
))
5242 error ("field name used as index in array initializer");
5243 else if ((TREE_CODE (start_index
) != INTEGER_CST
)
5244 || (TREE_CODE (end_index
) != INTEGER_CST
))
5245 error ("non-constant or non-integer array index in initializer");
5246 else if (tree_int_cst_lt (start_index
, min_index
)
5247 || (max_index
&& tree_int_cst_lt (max_index
, start_index
))
5248 || tree_int_cst_lt (end_index
, min_index
)
5249 || (max_index
&& tree_int_cst_lt (max_index
, end_index
)))
5250 error ("array index out of range in initializer");
5251 else if (tree_int_cst_lt (end_index
, start_index
))
5253 /* If the range is empty, don't initialize any elements,
5254 but do reset current_index for the next initializer
5256 warning ("empty array initializer range");
5257 tail
= TREE_CHAIN (tail
);
5258 current_index
= end_index
;
5263 current_index
= start_index
;
5265 /* See if the first element is also the last. */
5266 if (!tree_int_cst_lt (current_index
, end_index
))
5270 else if (TREE_CODE (index
) == IDENTIFIER_NODE
)
5271 error ("field name used as index in array initializer");
5272 else if (TREE_CODE (index
) != INTEGER_CST
)
5273 error ("non-constant array index in initializer");
5274 else if (tree_int_cst_lt (index
, min_index
)
5275 || (max_index
&& tree_int_cst_lt (max_index
, index
)))
5276 error ("array index out of range in initializer");
5279 constant_expression_warning (index
);
5280 current_index
= index
, win
= 1;
5285 /* If there was an error, end the current range. */
5287 TREE_VALUE (tail
) = error_mark_node
;
5291 if (max_index
&& tree_int_cst_lt (max_index
, current_index
))
5292 break; /* Stop if we've indeed run out of elements. */
5294 /* Now digest the value specified. */
5297 else if (TREE_VALUE (tail
) != 0)
5301 /* Build the element of this array, with "[]" notation. For
5302 error messages, we assume that the index fits within a
5306 push_array_bounds (TREE_INT_CST_LOW (current_index
));
5307 next1
= digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
5308 TREE_VALUE (tail
), &tail1
,
5309 /* Both of these are the same because
5310 a value here is an elt overall. */
5311 constant_element
, constant_element
,
5315 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
5317 if (tail
== tail1
&& TYPE_DOMAIN (type
) == 0)
5320 "non-empty initializer for array%s of empty elements",
5322 /* Just ignore what we were supposed to use. */
5329 next1
= error_mark_node
;
5330 tail
= TREE_CHAIN (tail
);
5336 if (next1
== error_mark_node
)
5338 else if (!TREE_CONSTANT (next1
))
5340 else if (initializer_constant_valid_p (next1
, TREE_TYPE (next1
)) == 0)
5343 /* Now store NEXT1 in the list, I elements from the *end*.
5344 Make the list longer if necessary. */
5345 while (! tree_int_cst_lt (current_index
, members_index
))
5349 TREE_CHAIN (free_tree_list
) = members
;
5350 TREE_PURPOSE (free_tree_list
) = NULL_TREE
;
5351 TREE_VALUE (free_tree_list
) = NULL_TREE
;
5352 members
= free_tree_list
;
5353 free_tree_list
= NULL_TREE
;
5356 members
= tree_cons (NULL_TREE
, NULL_TREE
, members
);
5357 add_double (TREE_INT_CST_LOW (members_index
),
5358 TREE_INT_CST_HIGH (members_index
), 1, 0,
5359 &TREE_INT_CST_LOW (members_index
),
5360 &TREE_INT_CST_HIGH (members_index
));
5365 union tree_node idx_node
;
5366 tree idx
= &idx_node
;
5367 TREE_TYPE (idx
) = integer_type_node
;
5370 for (add_double (TREE_INT_CST_LOW (members_index
),
5371 TREE_INT_CST_HIGH (members_index
), -1, -1,
5372 &TREE_INT_CST_LOW (idx
),
5373 &TREE_INT_CST_HIGH (idx
));
5374 tree_int_cst_lt (current_index
, idx
);
5375 add_double (TREE_INT_CST_LOW (idx
),
5376 TREE_INT_CST_HIGH (idx
), -1, -1,
5377 &TREE_INT_CST_LOW (idx
),
5378 &TREE_INT_CST_HIGH (idx
)))
5379 temp
= TREE_CHAIN (temp
);
5380 TREE_VALUE (temp
) = next1
;
5384 if (TREE_CODE (type
) == RECORD_TYPE
)
5386 register tree field
;
5387 int members_length
= 0;
5390 /* Don't leave the loop based on field just yet; see if next item
5391 overrides the expected field first. */
5393 for (field
= TYPE_FIELDS (type
), i
= 0; tail
;
5394 field
= TREE_CHAIN (field
), i
++)
5396 register tree next1
;
5398 /* If this element specifies a field,
5399 move to that field before storing it in the new list. */
5400 if (TREE_PURPOSE (tail
) != 0)
5404 if (TREE_CODE (TREE_PURPOSE (tail
)) != IDENTIFIER_NODE
)
5405 error ("index value instead of field name in structure initializer");
5410 for (temp
= TYPE_FIELDS (type
), j
= 0;
5412 temp
= TREE_CHAIN (temp
), j
++)
5413 if (DECL_NAME (temp
) == TREE_PURPOSE (tail
))
5416 field
= temp
, i
= j
, win
= 1;
5418 error ("no field `%s' in structure being initialized",
5419 IDENTIFIER_POINTER (TREE_PURPOSE (tail
)));
5422 TREE_VALUE (tail
) = error_mark_node
;
5426 break; /* No more fields to init. */
5428 if (! DECL_NAME (field
))
5430 next1
= integer_zero_node
;
5432 else if (TREE_VALUE (tail
) != 0)
5436 /* Build the name of this member, with a "." for membership. */
5439 push_member_name (IDENTIFIER_POINTER (DECL_NAME (field
)));
5440 next1
= digest_init (TREE_TYPE (field
),
5441 TREE_VALUE (tail
), &tail1
,
5442 constant_element
, constant_element
,
5445 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
5451 next1
= error_mark_node
;
5452 tail
= TREE_CHAIN (tail
);
5455 if (next1
== error_mark_node
)
5457 else if (!TREE_CONSTANT (next1
))
5459 else if (initializer_constant_valid_p (next1
, TREE_TYPE (next1
)) == 0)
5462 /* Now store NEXT1 in the list, I elements from the *end*.
5463 Make the list longer if necessary. */
5464 while (i
>= members_length
)
5468 TREE_CHAIN (free_tree_list
) = members
;
5469 TREE_PURPOSE (free_tree_list
) = NULL_TREE
;
5470 TREE_VALUE (free_tree_list
) = NULL_TREE
;
5471 members
= free_tree_list
;
5472 free_tree_list
= NULL_TREE
;
5475 members
= tree_cons (NULL_TREE
, NULL_TREE
, members
);
5483 for (j
= members_length
- 1; j
> i
; j
--)
5484 temp
= TREE_CHAIN (temp
);
5485 TREE_VALUE (temp
) = next1
;
5486 TREE_PURPOSE (temp
) = field
;
5490 if (TREE_CODE (type
) == UNION_TYPE
)
5492 register tree field
= TYPE_FIELDS (type
);
5493 register tree next1
;
5495 /* Find the first named field. ANSI decided in September 1990
5496 that only named fields count here. */
5497 while (field
&& DECL_NAME (field
) == 0)
5498 field
= TREE_CHAIN (field
);
5500 /* For a union, get the initializer for 1 fld. */
5504 error ("empty initializer for union");
5505 tail
= build_tree_list (0, 0);
5508 /* If this element specifies a field, initialize via that field. */
5509 if (TREE_PURPOSE (tail
) != 0)
5513 if (TREE_CODE (TREE_PURPOSE (tail
)) == FIELD_DECL
)
5514 /* Handle the case of a call by build_c_cast. */
5515 field
= TREE_PURPOSE (tail
), win
= 1;
5516 else if (TREE_CODE (TREE_PURPOSE (tail
)) != IDENTIFIER_NODE
)
5517 error ("index value instead of field name in union initializer");
5521 for (temp
= TYPE_FIELDS (type
);
5523 temp
= TREE_CHAIN (temp
))
5524 if (DECL_NAME (temp
) == TREE_PURPOSE (tail
))
5527 field
= temp
, win
= 1;
5529 error ("no field `%s' in union being initialized",
5530 IDENTIFIER_POINTER (TREE_PURPOSE (tail
)));
5533 TREE_VALUE (tail
) = error_mark_node
;
5536 if (TREE_VALUE (tail
) != 0)
5540 /* Build the name of this member, with a "." for membership. */
5543 push_member_name (IDENTIFIER_POINTER (DECL_NAME (field
)));
5544 next1
= digest_init (TREE_TYPE (field
),
5545 TREE_VALUE (tail
), &tail1
,
5546 constant_value
, constant_element
, NULL_PTR
);
5548 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
5554 next1
= error_mark_node
;
5555 tail
= TREE_CHAIN (tail
);
5558 if (next1
== error_mark_node
)
5560 else if (!TREE_CONSTANT (next1
))
5562 else if (initializer_constant_valid_p (next1
, TREE_TYPE (next1
)) == 0)
5566 TREE_CHAIN (free_tree_list
) = members
;
5567 TREE_PURPOSE (free_tree_list
) = field
;
5568 TREE_VALUE (free_tree_list
) = next1
;
5569 members
= free_tree_list
;
5570 free_tree_list
= NULL_TREE
;
5573 members
= tree_cons (field
, next1
, members
);
5576 /* If arguments were specified as a list, just remove the ones we used. */
5579 /* If arguments were specified as a constructor,
5580 complain unless we used all the elements of the constructor. */
5583 if (TREE_CODE (type
) == UNION_TYPE
)
5585 pedwarn_init ("excess elements in union initializer%s",
5586 " after `%s'", NULL_PTR
);
5590 pedwarn_init ("excess elements in aggregate initializer%s",
5591 " after `%s'", NULL_PTR
);
5595 /* It might be possible to use SAVE_SPELLING_DEPTH, but I suspect that
5596 some preprocessor somewhere won't accept that much text as an argument.
5597 It's also likely to make debugging difficult. */
5599 RESTORE_SPELLING_DEPTH (depth
);
5602 return error_mark_node
;
5605 result
= build (CONSTRUCTOR
, type
, NULL_TREE
, nreverse (members
));
5609 CONSTRUCTOR_ELTS (result
) = nreverse (members
);
5610 TREE_TYPE (result
) = type
;
5611 TREE_CONSTANT (result
) = 0;
5612 TREE_STATIC (result
) = 0;
5614 if (allconstant
) TREE_CONSTANT (result
) = 1;
5615 if (allconstant
&& allsimple
) TREE_STATIC (result
) = 1;
5619 /* Expand an ASM statement with operands, handling output operands
5620 that are not variables or INDIRECT_REFS by transforming such
5621 cases into cases that expand_asm_operands can handle.
5623 Arguments are same as for expand_asm_operands. */
5626 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
5627 tree string
, outputs
, inputs
, clobbers
;
5632 int noutputs
= list_length (outputs
);
5634 /* o[I] is the place that output number I should be written. */
5635 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
5638 if (TREE_CODE (string
) == ADDR_EXPR
)
5639 string
= TREE_OPERAND (string
, 0);
5640 if (TREE_CODE (string
) != STRING_CST
)
5642 error ("asm template is not a string constant");
5646 /* Record the contents of OUTPUTS before it is modified. */
5647 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
5648 o
[i
] = TREE_VALUE (tail
);
5650 /* Perform default conversions on array and function inputs. */
5651 /* Don't do this for other types--
5652 it would screw up operands expected to be in memory. */
5653 for (i
= 0, tail
= inputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
5654 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail
))) == ARRAY_TYPE
5655 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail
))) == FUNCTION_TYPE
)
5656 TREE_VALUE (tail
) = default_conversion (TREE_VALUE (tail
));
5658 /* Generate the ASM_OPERANDS insn;
5659 store into the TREE_VALUEs of OUTPUTS some trees for
5660 where the values were actually stored. */
5661 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
5663 /* Copy all the intermediate outputs into the specified outputs. */
5664 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
5666 if (o
[i
] != TREE_VALUE (tail
))
5668 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
5672 /* Detect modification of read-only values.
5673 (Otherwise done by build_modify_expr.) */
5676 tree type
= TREE_TYPE (o
[i
]);
5677 if (TYPE_READONLY (type
)
5678 || ((TREE_CODE (type
) == RECORD_TYPE
5679 || TREE_CODE (type
) == UNION_TYPE
)
5680 && C_TYPE_FIELDS_READONLY (type
)))
5681 readonly_warning (o
[i
], "modification by `asm'");
5685 /* Those MODIFY_EXPRs could do autoincrements. */
5689 /* Expand a C `return' statement.
5690 RETVAL is the expression for what to return,
5691 or a null pointer for `return;' with no value. */
5694 c_expand_return (retval
)
5697 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
5699 if (TREE_THIS_VOLATILE (current_function_decl
))
5700 warning ("function declared `volatile' has a `return' statement");
5704 current_function_returns_null
= 1;
5705 if (warn_return_type
&& valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
5706 warning ("`return' with no value, in function returning non-void");
5707 expand_null_return ();
5709 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
5711 current_function_returns_null
= 1;
5712 if (pedantic
|| TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
5713 pedwarn ("`return' with a value, in function returning void");
5714 expand_return (retval
);
5718 tree t
= convert_for_assignment (valtype
, retval
, "return",
5719 NULL_TREE
, NULL_TREE
, 0);
5720 tree res
= DECL_RESULT (current_function_decl
);
5721 t
= build (MODIFY_EXPR
, TREE_TYPE (res
),
5722 res
, convert (TREE_TYPE (res
), t
));
5724 current_function_returns_value
= 1;
5728 /* Start a C switch statement, testing expression EXP.
5729 Return EXP if it is valid, an error node otherwise. */
5732 c_expand_start_case (exp
)
5735 register enum tree_code code
= TREE_CODE (TREE_TYPE (exp
));
5736 tree type
= TREE_TYPE (exp
);
5738 if (code
!= INTEGER_TYPE
&& code
!= ENUMERAL_TYPE
&& code
!= ERROR_MARK
)
5740 error ("switch quantity not an integer");
5741 exp
= error_mark_node
;
5746 type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
5748 if (warn_traditional
5749 && (type
== long_integer_type_node
5750 || type
== long_unsigned_type_node
))
5751 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
5753 exp
= default_conversion (exp
);
5754 type
= TREE_TYPE (exp
);
5755 index
= get_unwidened (exp
, NULL_TREE
);
5756 /* We can't strip a conversion from a signed type to an unsigned,
5757 because if we did, int_fits_type_p would do the wrong thing
5758 when checking case values for being in range,
5759 and it's too hard to do the right thing. */
5760 if (TREE_UNSIGNED (TREE_TYPE (exp
))
5761 == TREE_UNSIGNED (TREE_TYPE (index
)))
5765 expand_start_case (1, exp
, type
, "switch statement");