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 extern char *index ();
37 extern char *rindex ();
39 int mark_addressable ();
40 static tree
convert_for_assignment ();
41 static void warn_for_assignment ();
42 static int function_types_compatible_p ();
43 static int type_lists_compatible_p ();
44 int self_promoting_args_p ();
45 static int self_promoting_type_p ();
46 static int comp_target_types ();
47 static tree
pointer_int_sum ();
48 static tree
pointer_diff ();
49 static tree
convert_sequence ();
50 static tree
unary_complex_lvalue ();
51 static tree
process_init_constructor ();
52 static tree
convert_arguments ();
53 static char *get_spelling ();
55 static void pedantic_lvalue_warning ();
56 tree
truthvalue_conversion ();
57 void incomplete_type_error ();
58 void readonly_warning ();
60 /* Do `exp = require_complete_type (exp);' to make sure exp
61 does not have an incomplete type. (That includes void types.) */
64 require_complete_type (value
)
67 tree type
= TREE_TYPE (value
);
69 /* First, detect a valid value with a complete type. */
70 if (TYPE_SIZE (type
) != 0
71 && type
!= void_type_node
)
74 incomplete_type_error (value
, type
);
75 return error_mark_node
;
78 /* Print an error message for invalid use of an incomplete type.
79 VALUE is the expression that was used (or 0 if that isn't known)
80 and TYPE is the type that was invalid. */
83 incomplete_type_error (value
, type
)
89 /* Avoid duplicate error message. */
90 if (TREE_CODE (type
) == ERROR_MARK
)
93 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
94 || TREE_CODE (value
) == PARM_DECL
))
95 error ("`%s' has an incomplete type",
96 IDENTIFIER_POINTER (DECL_NAME (value
)));
100 /* We must print an error message. Be clever about what it says. */
102 switch (TREE_CODE (type
))
105 errmsg
= "invalid use of undefined type `struct %s'";
109 errmsg
= "invalid use of undefined type `union %s'";
113 errmsg
= "invalid use of undefined type `enum %s'";
117 error ("invalid use of void expression");
121 if (TYPE_DOMAIN (type
))
123 type
= TREE_TYPE (type
);
126 error ("invalid use of array with unspecified bounds");
133 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
134 error (errmsg
, IDENTIFIER_POINTER (TYPE_NAME (type
)));
136 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
137 error ("invalid use of incomplete typedef `%s'",
138 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
))));
142 /* Return a variant of TYPE which has all the type qualifiers of LIKE
143 as well as those of TYPE. */
146 qualify_type (type
, like
)
149 int constflag
= TYPE_READONLY (type
) || TYPE_READONLY (like
);
150 int volflag
= TYPE_VOLATILE (type
) || TYPE_VOLATILE (like
);
151 return c_build_type_variant (type
, constflag
, volflag
);
154 /* Return the common type of two types.
155 We assume that comptypes has already been done and returned 1;
156 if that isn't so, this may crash. In particular, we assume that qualifiers
159 This is the type for the result of most arithmetic operations
160 if the operands have the given two types. */
166 register enum tree_code code1
;
167 register enum tree_code code2
;
169 /* Save time if the two types are the same. */
171 if (t1
== t2
) return t1
;
173 /* If one type is nonsense, use the other. */
174 if (t1
== error_mark_node
)
176 if (t2
== error_mark_node
)
179 /* Treat an enum type as the unsigned integer type of the same width. */
181 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
182 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
183 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
184 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
186 code1
= TREE_CODE (t1
);
187 code2
= TREE_CODE (t2
);
193 /* If only one is real, use it as the result. */
195 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
198 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
201 /* Both real or both integers; use the one with greater precision. */
203 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
205 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
208 /* Same precision. Prefer longs to ints even when same size. */
210 if (t1
== long_unsigned_type_node
211 || t2
== long_unsigned_type_node
)
212 return long_unsigned_type_node
;
214 if (t1
== long_integer_type_node
215 || t2
== long_integer_type_node
)
217 /* But preserve unsignedness from the other type,
218 since long cannot hold all the values of an unsigned int. */
219 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
220 return long_unsigned_type_node
;
221 return long_integer_type_node
;
224 /* Otherwise prefer the unsigned one. */
226 if (TREE_UNSIGNED (t1
))
232 /* For two pointers, do this recursively on the target type,
233 and combine the qualifiers of the two types' targets. */
235 tree target
= common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1
)),
236 TYPE_MAIN_VARIANT (TREE_TYPE (t2
)));
238 = TYPE_READONLY (TREE_TYPE (t1
)) || TYPE_READONLY (TREE_TYPE (t2
));
240 = TYPE_VOLATILE (TREE_TYPE (t1
)) || TYPE_VOLATILE (TREE_TYPE (t2
));
241 return build_pointer_type (c_build_type_variant (target
, constp
, volatilep
));
244 return build_pointer_type (common_type (TREE_TYPE (t1
), TREE_TYPE (t2
)));
248 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
249 /* Save space: see if the result is identical to one of the args. */
250 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
252 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
254 /* Merge the element types, and have a size if either arg has one. */
255 return build_array_type (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
259 /* Function types: prefer the one that specified arg types.
260 If both do, merge the arg types. Also merge the return types. */
262 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
263 tree p1
= TYPE_ARG_TYPES (t1
);
264 tree p2
= TYPE_ARG_TYPES (t2
);
269 /* Save space: see if the result is identical to one of the args. */
270 if (valtype
== TREE_TYPE (t1
) && ! TYPE_ARG_TYPES (t2
))
272 if (valtype
== TREE_TYPE (t2
) && ! TYPE_ARG_TYPES (t1
))
275 /* Simple way if one arg fails to specify argument types. */
276 if (TYPE_ARG_TYPES (t1
) == 0)
277 return build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
278 if (TYPE_ARG_TYPES (t2
) == 0)
279 return build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
281 /* If both args specify argument types, we must merge the two
282 lists, argument by argument. */
284 len
= list_length (p1
);
287 for (i
= 0; i
< len
; i
++)
288 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
293 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
295 /* A null type means arg type is not specified.
296 Take whatever the other function type has. */
297 if (TREE_VALUE (p1
) == 0)
299 TREE_VALUE (n
) = TREE_VALUE (p2
);
302 if (TREE_VALUE (p2
) == 0)
304 TREE_VALUE (n
) = TREE_VALUE (p1
);
308 /* Given wait (union {union wait *u; int *i} *)
309 and wait (union wait *),
310 prefer union wait * as type of parm. */
311 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
312 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
315 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
316 memb
; memb
= TREE_CHAIN (memb
))
317 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p2
)))
319 TREE_VALUE (n
) = TREE_VALUE (p2
);
321 pedwarn ("function types not truly compatible in ANSI C");
325 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
326 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
329 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
330 memb
; memb
= TREE_CHAIN (memb
))
331 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p1
)))
333 TREE_VALUE (n
) = TREE_VALUE (p1
);
335 pedwarn ("function types not truly compatible in ANSI C");
339 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
343 return build_function_type (valtype
, newargs
);
352 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
353 or various other operations. Return 2 if they are compatible
354 but a warning may be needed if you use them together. */
357 comptypes (type1
, type2
)
360 register tree t1
= type1
;
361 register tree t2
= type2
;
363 /* Suppress errors caused by previously reported errors. */
365 if (t1
== t2
|| TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
368 /* Treat an enum type as the unsigned integer type of the same width. */
370 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
371 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
372 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
373 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
378 /* Different classes of types can't be compatible. */
380 if (TREE_CODE (t1
) != TREE_CODE (t2
)) return 0;
382 /* Qualifiers must match. */
384 if (TYPE_READONLY (t1
) != TYPE_READONLY (t2
))
386 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
389 /* If generating auxiliary info, allow for two different type nodes which
390 have essentially the same definition. */
392 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
395 switch (TREE_CODE (t1
))
398 return (TREE_TYPE (t1
) == TREE_TYPE (t2
)
399 ? 1 : comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
)));
402 return function_types_compatible_p (t1
, t2
);
406 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
408 tree d1
= TYPE_DOMAIN (t1
);
409 tree d2
= TYPE_DOMAIN (t2
);
411 /* Target types must match incl. qualifiers. */
412 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
413 && 0 == (val
= comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))))
416 /* Sizes must match unless one is missing or variable. */
417 if (d1
== 0 || d2
== 0 || d1
== d2
418 || TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
419 || TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
420 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
421 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
)
424 return (((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1
))
425 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2
)))
426 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1
))
427 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2
)))
428 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1
))
429 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2
)))
430 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1
))
431 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2
))))
436 return maybe_objc_comptypes (t1
, t2
);
441 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
442 ignoring their qualifiers. */
445 comp_target_types (ttl
, ttr
)
448 int val
= comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl
)),
449 TYPE_MAIN_VARIANT (TREE_TYPE (ttr
)));
450 if (val
== 2 && pedantic
)
451 pedwarn ("types are not quite compatible");
455 /* Subroutines of `comptypes'. */
457 /* Return 1 if two function types F1 and F2 are compatible.
458 If either type specifies no argument types,
459 the other must specify a fixed number of self-promoting arg types.
460 Otherwise, if one type specifies only the number of arguments,
461 the other must specify that number of self-promoting arg types.
462 Otherwise, the argument types must match. */
465 function_types_compatible_p (f1
, f2
)
469 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
473 if (!(TREE_TYPE (f1
) == TREE_TYPE (f2
)
474 || (val
= comptypes (TREE_TYPE (f1
), TREE_TYPE (f2
)))))
477 args1
= TYPE_ARG_TYPES (f1
);
478 args2
= TYPE_ARG_TYPES (f2
);
480 /* An unspecified parmlist matches any specified parmlist
481 whose argument types don't need default promotions. */
485 if (!self_promoting_args_p (args2
))
487 /* If one of these types comes from a non-prototype fn definition,
488 compare that with the other type's arglist.
489 If they don't match, ask for a warning (but no error). */
490 if (TYPE_ACTUAL_ARG_TYPES (f1
)
491 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
)))
497 if (!self_promoting_args_p (args1
))
499 if (TYPE_ACTUAL_ARG_TYPES (f2
)
500 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
)))
505 /* Both types have argument lists: compare them and propagate results. */
506 val1
= type_lists_compatible_p (args1
, args2
);
507 return val1
!= 1 ? val1
: val
;
510 /* Check two lists of types for compatibility,
511 returning 0 for incompatible, 1 for compatible,
512 or 2 for compatible with warning. */
515 type_lists_compatible_p (args1
, args2
)
518 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
524 if (args1
== 0 && args2
== 0)
526 /* If one list is shorter than the other,
527 they fail to match. */
528 if (args1
== 0 || args2
== 0)
530 /* A null pointer instead of a type
531 means there is supposed to be an argument
532 but nothing is specified about what type it has.
533 So match anything that self-promotes. */
534 if (TREE_VALUE (args1
) == 0)
536 if (! self_promoting_type_p (TREE_VALUE (args2
)))
539 else if (TREE_VALUE (args2
) == 0)
541 if (! self_promoting_type_p (TREE_VALUE (args1
)))
544 else if (! (newval
= comptypes (TREE_VALUE (args1
), TREE_VALUE (args2
))))
546 /* Allow wait (union {union wait *u; int *i} *)
547 and wait (union wait *) to be compatible. */
548 if (TREE_CODE (TREE_VALUE (args1
)) == UNION_TYPE
549 && TYPE_NAME (TREE_VALUE (args1
)) == 0
550 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1
))) == INTEGER_CST
551 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1
)),
552 TYPE_SIZE (TREE_VALUE (args2
))))
555 for (memb
= TYPE_FIELDS (TREE_VALUE (args1
));
556 memb
; memb
= TREE_CHAIN (memb
))
557 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args2
)))
562 else if (TREE_CODE (TREE_VALUE (args2
)) == UNION_TYPE
563 && TYPE_NAME (TREE_VALUE (args2
)) == 0
564 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2
))) == INTEGER_CST
565 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2
)),
566 TYPE_SIZE (TREE_VALUE (args1
))))
569 for (memb
= TYPE_FIELDS (TREE_VALUE (args2
));
570 memb
; memb
= TREE_CHAIN (memb
))
571 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args1
)))
580 /* comptypes said ok, but record if it said to warn. */
584 args1
= TREE_CHAIN (args1
);
585 args2
= TREE_CHAIN (args2
);
589 /* Return 1 if PARMS specifies a fixed number of parameters
590 and none of their types is affected by default promotions. */
593 self_promoting_args_p (parms
)
597 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
599 register tree type
= TREE_VALUE (t
);
601 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
607 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
610 if (C_PROMOTING_INTEGER_TYPE_P (type
))
616 /* Return 1 if TYPE is not affected by default promotions. */
619 self_promoting_type_p (type
)
622 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
625 if (C_PROMOTING_INTEGER_TYPE_P (type
))
631 /* Return an unsigned type the same as TYPE in other respects. */
637 tree type1
= TYPE_MAIN_VARIANT (type
);
638 if (type1
== signed_char_type_node
|| type1
== char_type_node
)
639 return unsigned_char_type_node
;
640 if (type1
== integer_type_node
)
641 return unsigned_type_node
;
642 if (type1
== short_integer_type_node
)
643 return short_unsigned_type_node
;
644 if (type1
== long_integer_type_node
)
645 return long_unsigned_type_node
;
646 if (type1
== long_long_integer_type_node
)
647 return long_long_unsigned_type_node
;
651 /* Return a signed type the same as TYPE in other respects. */
657 tree type1
= TYPE_MAIN_VARIANT (type
);
658 if (type1
== unsigned_char_type_node
|| type1
== char_type_node
)
659 return signed_char_type_node
;
660 if (type1
== unsigned_type_node
)
661 return integer_type_node
;
662 if (type1
== short_unsigned_type_node
)
663 return short_integer_type_node
;
664 if (type1
== long_unsigned_type_node
)
665 return long_integer_type_node
;
666 if (type1
== long_long_unsigned_type_node
)
667 return long_long_integer_type_node
;
671 /* Return a type the same as TYPE except unsigned or
672 signed according to UNSIGNEDP. */
675 signed_or_unsigned_type (unsignedp
, type
)
679 if (TREE_CODE (type
) != INTEGER_TYPE
)
681 if (TYPE_PRECISION (type
) == TYPE_PRECISION (signed_char_type_node
))
682 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
683 if (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
684 return unsignedp
? unsigned_type_node
: integer_type_node
;
685 if (TYPE_PRECISION (type
) == TYPE_PRECISION (short_integer_type_node
))
686 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
687 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_integer_type_node
))
688 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
689 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_long_integer_type_node
))
690 return (unsignedp
? long_long_unsigned_type_node
691 : long_long_integer_type_node
);
695 /* Compute the value of the `sizeof' operator. */
701 enum tree_code code
= TREE_CODE (type
);
703 if (code
== FUNCTION_TYPE
)
705 if (pedantic
|| warn_pointer_arith
)
706 pedwarn ("sizeof applied to a function type");
709 if (code
== VOID_TYPE
)
711 if (pedantic
|| warn_pointer_arith
)
712 pedwarn ("sizeof applied to a void type");
715 if (code
== ERROR_MARK
)
717 if (TYPE_SIZE (type
) == 0)
719 error ("sizeof applied to an incomplete type");
723 /* Convert in case a char is more than one unit. */
724 return size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
725 size_int (TYPE_PRECISION (char_type_node
)));
729 c_sizeof_nowarn (type
)
732 enum tree_code code
= TREE_CODE (type
);
734 if (code
== FUNCTION_TYPE
736 || code
== ERROR_MARK
)
738 if (TYPE_SIZE (type
) == 0)
741 /* Convert in case a char is more than one unit. */
742 return size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
743 size_int (TYPE_PRECISION (char_type_node
)));
746 /* Compute the size to increment a pointer by. */
749 c_size_in_bytes (type
)
752 enum tree_code code
= TREE_CODE (type
);
754 if (code
== FUNCTION_TYPE
)
756 if (code
== VOID_TYPE
)
758 if (code
== ERROR_MARK
)
760 if (TYPE_SIZE (type
) == 0)
762 error ("arithmetic on pointer to an incomplete type");
766 /* Convert in case a char is more than one unit. */
767 return size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
768 size_int (BITS_PER_UNIT
));
771 /* Implement the __alignof keyword: Return the minimum required
772 alignment of TYPE, measured in bytes. */
778 enum tree_code code
= TREE_CODE (type
);
780 if (code
== FUNCTION_TYPE
)
781 return size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
783 if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
786 return size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
789 /* Print a warning if a constant expression had overflow in folding.
790 This doesn't really work--it is waiting for changes in fold. */
793 constant_expression_warning (value
)
796 if (TREE_CODE (value
) == NON_LVALUE_EXPR
&& TREE_CONSTANT_OVERFLOW (value
))
797 pedwarn ("overflow in constant expression");
800 /* Implement the __alignof keyword: Return the minimum required
801 alignment of EXPR, measured in bytes. For VAR_DECL's and
802 FIELD_DECL's return DECL_ALIGN (which can be set from an
803 "aligned" __attribute__ specification). */
805 c_alignof_expr (expr
)
808 if (TREE_CODE (expr
) == VAR_DECL
)
809 return size_int (DECL_ALIGN (expr
) / BITS_PER_UNIT
);
811 if (TREE_CODE (expr
) == COMPONENT_REF
812 && DECL_BIT_FIELD (TREE_OPERAND (expr
, 1)))
814 error ("`__alignof' applied to a bit-field");
817 else if (TREE_CODE (expr
) == COMPONENT_REF
818 && TREE_CODE (TREE_OPERAND (expr
, 1)) == FIELD_DECL
)
819 return size_int (DECL_ALIGN (TREE_OPERAND (expr
, 1)) / BITS_PER_UNIT
);
821 if (TREE_CODE (expr
) == INDIRECT_REF
)
823 tree t
= TREE_OPERAND (expr
, 0);
825 int bestalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
827 while (TREE_CODE (t
) == NOP_EXPR
828 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == POINTER_TYPE
)
832 t
= TREE_OPERAND (t
, 0);
833 thisalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
834 if (thisalign
> bestalign
)
835 best
= t
, bestalign
= thisalign
;
837 return c_alignof (TREE_TYPE (TREE_TYPE (best
)));
840 return c_alignof (TREE_TYPE (expr
));
842 /* Return either DECL or its known constant value (if it has one). */
845 decl_constant_value (decl
)
848 if (! TREE_PUBLIC (decl
)
849 /* Don't change a variable array bound or initial value to a constant
850 in a place where a variable is invalid. */
851 && current_function_decl
!= 0
853 && ! TREE_THIS_VOLATILE (decl
)
854 && DECL_INITIAL (decl
) != 0
855 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
856 /* This is invalid if initial value is not constant.
857 If it has either a function call, a memory reference,
858 or a variable, then re-evaluating it could give different results. */
859 && TREE_CONSTANT (DECL_INITIAL (decl
))
860 /* Check for cases where this is sub-optimal, even though valid. */
861 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
862 && DECL_MODE (decl
) != BLKmode
)
863 return DECL_INITIAL (decl
);
867 /* Perform default promotions for C data used in expressions.
868 Arrays and functions are converted to pointers;
869 enumeral types or short or char, to int.
870 In addition, manifest constants symbols are replaced by their values. */
873 default_conversion (exp
)
876 register tree type
= TREE_TYPE (exp
);
877 register enum tree_code code
= TREE_CODE (type
);
879 /* Constants can be used directly unless they're not loadable. */
880 if (TREE_CODE (exp
) == CONST_DECL
)
881 exp
= DECL_INITIAL (exp
);
882 /* Replace a nonvolatile const static variable with its value. */
884 && TREE_CODE (exp
) == VAR_DECL
885 && TREE_READONLY (exp
)
886 && DECL_MODE (exp
) != BLKmode
)
888 exp
= decl_constant_value (exp
);
889 type
= TREE_TYPE (exp
);
892 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
894 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
895 to integer and cause infinite recursion. */
896 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
897 || (TREE_CODE (exp
) == NOP_EXPR
898 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
899 exp
= TREE_OPERAND (exp
, 0);
901 /* Normally convert enums to int,
902 but convert wide enums to something wider. */
903 if (code
== ENUMERAL_TYPE
)
905 type
= type_for_size (MAX (TYPE_PRECISION (type
),
906 TYPE_PRECISION (integer_type_node
)),
907 (flag_traditional
&& TREE_UNSIGNED (type
)));
908 return convert (type
, exp
);
911 if (C_PROMOTING_INTEGER_TYPE_P (type
))
913 /* Traditionally, unsignedness is preserved in default promotions.
914 Also preserve unsignedness if not really getting any wider. */
915 if (TREE_UNSIGNED (type
)
917 || TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
918 return convert (unsigned_type_node
, exp
);
919 return convert (integer_type_node
, exp
);
921 if (flag_traditional
&& TYPE_MAIN_VARIANT (type
) == float_type_node
)
922 return convert (double_type_node
, exp
);
923 if (code
== VOID_TYPE
)
925 error ("void value not ignored as it ought to be");
926 return error_mark_node
;
928 if (code
== FUNCTION_TYPE
)
930 return build_unary_op (ADDR_EXPR
, exp
, 0);
932 if (code
== ARRAY_TYPE
)
935 tree restype
= TREE_TYPE (type
);
938 if (TREE_CODE (exp
) == INDIRECT_REF
)
939 return convert (TYPE_POINTER_TO (restype
),
940 TREE_OPERAND (exp
, 0));
942 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
944 tree op1
= default_conversion (TREE_OPERAND (exp
, 1));
945 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
946 TREE_OPERAND (exp
, 0), op1
);
950 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
952 error ("invalid use of non-lvalue array");
953 return error_mark_node
;
956 if (TYPE_READONLY (type
) || TYPE_VOLATILE (type
))
957 restype
= c_build_type_variant (restype
, TYPE_READONLY (type
),
958 TYPE_VOLATILE (type
));
960 ptrtype
= build_pointer_type (restype
);
962 if (TREE_CODE (exp
) == VAR_DECL
)
964 /* ??? This is not really quite correct
965 in that the type of the operand of ADDR_EXPR
966 is not the target type of the type of the ADDR_EXPR itself.
967 Question is, can this lossage be avoided? */
968 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
969 if (mark_addressable (exp
) == 0)
970 return error_mark_node
;
971 TREE_CONSTANT (adr
) = staticp (exp
);
972 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
975 /* This way is better for a COMPONENT_REF since it can
976 simplify the offset for a component. */
977 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
978 return convert (ptrtype
, adr
);
983 /* Make an expression to refer to the COMPONENT field of
984 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
987 build_component_ref (datum
, component
)
988 tree datum
, component
;
990 register tree type
= TREE_TYPE (datum
);
991 register enum tree_code code
= TREE_CODE (type
);
992 register tree field
= NULL
;
995 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
996 unless we are not to support things not strictly ANSI. */
997 switch (TREE_CODE (datum
))
1001 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
);
1002 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1003 TREE_OPERAND (datum
, 0), value
);
1006 return build_conditional_expr
1007 (TREE_OPERAND (datum
, 0),
1008 build_component_ref (TREE_OPERAND (datum
, 1), component
),
1009 build_component_ref (TREE_OPERAND (datum
, 2), component
));
1012 /* See if there is a field or component with name COMPONENT. */
1014 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1016 if (TYPE_SIZE (type
) == 0)
1018 incomplete_type_error (NULL_TREE
, type
);
1019 return error_mark_node
;
1022 /* Look up component name in the structure type definition.
1024 If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1025 to the field elements. Use a binary search on this array to quickly
1026 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1027 will always be set for structures which have many elements. */
1029 if (TYPE_LANG_SPECIFIC (type
))
1032 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->elts
[0];
1034 field
= TYPE_FIELDS (type
);
1036 top
= TYPE_LANG_SPECIFIC (type
)->len
;
1037 while (top
- bot
> 1)
1041 half
= (top
- bot
+ 1) >> 1;
1042 field
= field_array
[bot
+half
];
1043 cmp
= (long)DECL_NAME (field
) - (long)component
;
1052 if (DECL_NAME (field_array
[bot
]) == component
)
1053 field
= field_array
[bot
];
1054 else if (DECL_NAME (field
) != component
)
1059 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1061 if (DECL_NAME (field
) == component
)
1068 error (code
== RECORD_TYPE
1069 ? "structure has no member named `%s'"
1070 : "union has no member named `%s'",
1071 IDENTIFIER_POINTER (component
));
1072 return error_mark_node
;
1074 if (TREE_TYPE (field
) == error_mark_node
)
1075 return error_mark_node
;
1077 ref
= build (COMPONENT_REF
, TREE_TYPE (field
), datum
, field
);
1079 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
1080 TREE_READONLY (ref
) = 1;
1081 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
1082 TREE_THIS_VOLATILE (ref
) = 1;
1086 else if (code
!= ERROR_MARK
)
1087 error ("request for member `%s' in something not a structure or union",
1088 IDENTIFIER_POINTER (component
));
1090 return error_mark_node
;
1093 /* Given an expression PTR for a pointer, return an expression
1094 for the value pointed to.
1095 ERRORSTRING is the name of the operator to appear in error messages. */
1098 build_indirect_ref (ptr
, errorstring
)
1102 register tree pointer
= default_conversion (ptr
);
1103 register tree type
= TREE_TYPE (pointer
);
1105 if (TREE_CODE (type
) == POINTER_TYPE
)
1106 if (TREE_CODE (pointer
) == ADDR_EXPR
1107 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
1108 == TREE_TYPE (type
)))
1109 return TREE_OPERAND (pointer
, 0);
1112 tree t
= TREE_TYPE (type
);
1113 register tree ref
= build1 (INDIRECT_REF
,
1114 TYPE_MAIN_VARIANT (t
), pointer
);
1116 if (TREE_CODE (t
) == VOID_TYPE
1117 || (TYPE_SIZE (t
) == 0 && TREE_CODE (t
) != ARRAY_TYPE
))
1119 error ("dereferencing pointer to incomplete type");
1120 return error_mark_node
;
1123 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1124 so that we get the proper error message if the result is used
1125 to assign to. Also, &* is supposed to be a no-op.
1126 And ANSI C seems to specify that the type of the result
1127 should be the const type. */
1128 /* A de-reference of a pointer to const is not a const. It is valid
1129 to change it via some other pointer. */
1130 TREE_READONLY (ref
) = TYPE_READONLY (t
);
1131 TREE_SIDE_EFFECTS (ref
) = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
1132 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
1135 else if (TREE_CODE (pointer
) != ERROR_MARK
)
1136 error ("invalid type argument of `%s'", errorstring
);
1137 return error_mark_node
;
1140 /* This handles expressions of the form "a[i]", which denotes
1143 This is logically equivalent in C to *(a+i), but we may do it differently.
1144 If A is a variable or a member, we generate a primitive ARRAY_REF.
1145 This avoids forcing the array out of registers, and can work on
1146 arrays that are not lvalues (for example, members of structures returned
1150 build_array_ref (array
, index
)
1155 error ("subscript missing in array reference");
1156 return error_mark_node
;
1159 if (TREE_TYPE (array
) == error_mark_node
1160 || TREE_TYPE (index
) == error_mark_node
)
1161 return error_mark_node
;
1163 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
1164 && TREE_CODE (array
) != INDIRECT_REF
)
1168 /* Subscripting with type char is likely to lose
1169 on a machine where chars are signed.
1170 So warn on any machine, but optionally.
1171 Don't warn for unsigned char since that type is safe.
1172 Don't warn for signed char because anyone who uses that
1173 must have done so deliberately. */
1174 if (warn_char_subscripts
1175 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1176 warning ("array subscript has type `char'");
1178 /* Apply default promotions *after* noticing character types. */
1179 index
= default_conversion (index
);
1181 /* Require integer *after* promotion, for sake of enums. */
1182 if (TREE_CODE (TREE_TYPE (index
)) != INTEGER_TYPE
)
1184 error ("array subscript is not an integer");
1185 return error_mark_node
;
1188 /* An array that is indexed by a non-constant
1189 cannot be stored in a register; we must be able to do
1190 address arithmetic on its address.
1191 Likewise an array of elements of variable size. */
1192 if (TREE_CODE (index
) != INTEGER_CST
1193 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))) != 0
1194 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
1196 if (mark_addressable (array
) == 0)
1197 return error_mark_node
;
1200 if (pedantic
&& !lvalue_p (array
))
1202 if (DECL_REGISTER (array
))
1203 pedwarn ("ANSI C forbids subscripting `register' array");
1205 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1211 while (TREE_CODE (foo
) == COMPONENT_REF
)
1212 foo
= TREE_OPERAND (foo
, 0);
1213 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
1214 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1217 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
1218 rval
= build (ARRAY_REF
, type
, array
, index
);
1219 /* Array ref is const/volatile if the array elements are
1220 or if the array is. */
1221 TREE_READONLY (rval
)
1222 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
1223 | TREE_READONLY (array
));
1224 TREE_SIDE_EFFECTS (rval
)
1225 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1226 | TREE_SIDE_EFFECTS (array
));
1227 TREE_THIS_VOLATILE (rval
)
1228 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1229 /* This was added by rms on 16 Nov 91.
1230 It fixes vol struct foo *a; a->elts[1]
1231 in an inline function.
1232 Hope it doesn't break something else. */
1233 | TREE_THIS_VOLATILE (array
));
1234 return require_complete_type (fold (rval
));
1238 tree ar
= default_conversion (array
);
1239 tree ind
= default_conversion (index
);
1241 /* Put the integer in IND to simplify error checking. */
1242 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
1249 if (ar
== error_mark_node
)
1252 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
1254 error ("subscripted value is neither array nor pointer");
1255 return error_mark_node
;
1257 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
1259 error ("array subscript is not an integer");
1260 return error_mark_node
;
1263 return build_indirect_ref (build_binary_op (PLUS_EXPR
, ar
, ind
, 0),
1268 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against PARAMS. */
1270 #define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
1272 #define T_I &integer_type_node
1273 #define T_L &long_integer_type_node
1274 #define T_S &short_integer_type_node
1275 #define T_UI &unsigned_type_node
1276 #define T_UL &long_unsigned_type_node
1277 #define T_US &short_unsigned_type_node
1278 #define T_F &float_type_node
1279 #define T_D &double_type_node
1280 #define T_LD &long_double_type_node
1281 #define T_C &char_type_node
1282 #define T_V &void_type_node
1288 /* Type of argument if no length modifier is used. */
1290 /* Type of argument if length modifier for shortening is used.
1291 If NULL, then this modifier is not allowed. */
1293 /* Type of argument if length modifier `l' is used.
1294 If NULL, then this modifier is not allowed. */
1296 /* Type of argument if length modifier `L' is used.
1297 If NULL, then this modifier is not allowed. */
1299 /* List of other modifier characters allowed with these options. */
1303 static format_char_info print_table
[]
1305 { "di", 0, T_I
, T_I
, T_L
, NULL
, "-wp0 +" },
1306 { "oxX", 0, T_UI
, T_UI
, T_UL
, NULL
, "-wp0#" },
1307 { "u", 0, T_UI
, T_UI
, T_UL
, NULL
, "-wp0" },
1308 { "feEgG", 0, T_D
, NULL
, NULL
, T_LD
, "-wp0 +#" },
1309 { "c", 0, T_I
, NULL
, NULL
, NULL
, "-w" },
1310 { "s", 1, T_C
, NULL
, NULL
, NULL
, "-wp" },
1311 { "p", 1, T_V
, NULL
, NULL
, NULL
, "-" },
1312 { "n", 1, T_I
, T_S
, T_L
, NULL
, "" },
1316 static format_char_info scan_table
[]
1318 { "di", 1, T_I
, T_S
, T_L
, NULL
, "*" },
1319 { "ouxX", 1, T_UI
, T_US
, T_UL
, NULL
, "*" },
1320 { "efgEG", 1, T_F
, NULL
, T_D
, T_LD
, "*" },
1321 { "s[c", 1, T_C
, NULL
, NULL
, NULL
, "*" },
1322 { "p", 2, T_V
, NULL
, NULL
, NULL
, "*" },
1323 { "n", 1, T_I
, T_S
, T_L
, NULL
, "" },
1329 tree function_ident
; /* identifier such as "printf" */
1330 int is_scan
; /* TRUE if *scanf */
1331 int format_num
; /* number of format argument */
1332 int first_arg_num
; /* number of first arg (zero for varargs) */
1335 static unsigned int function_info_entries
= 0;
1336 static function_info
*function_info_table
= NULL
;
1338 /* Record information for argument format checking. FUNCTION_IDENT is
1339 the identifier node for the name of the function to check (its decl
1340 need not exist yet). IS_SCAN is true for scanf-type format checking;
1341 false indicates printf-style format checking. FORMAT_NUM is the number
1342 of the argument which is the format control string (starting from 1).
1343 FIRST_ARG_NUM is the number of the first actual argument to check
1344 against teh format string, or zero if no checking is not be done
1345 (e.g. for varargs such as vfprintf). */
1348 record_format_info (function_ident
, is_scan
, format_num
, first_arg_num
)
1349 tree function_ident
;
1354 function_info
*info
;
1356 function_info_entries
++;
1357 if (function_info_table
)
1359 = (function_info
*) xrealloc (function_info_table
,
1360 function_info_entries
* sizeof (function_info
));
1362 function_info_table
= (function_info
*) xmalloc (sizeof (function_info
));
1364 info
= &function_info_table
[function_info_entries
- 1];
1366 info
->function_ident
= function_ident
;
1367 info
->is_scan
= is_scan
;
1368 info
->format_num
= format_num
;
1369 info
->first_arg_num
= first_arg_num
;
1372 /* Initialize the table of functions to perform format checking on.
1373 The ANSI functions are always checked (whether <stdio.h> is
1374 included or not), since it is common to call printf without
1375 including <stdio.h>. There shouldn't be a problem with this,
1376 since ANSI reserves these function names whether you include the
1377 header file or not. In any case, the checking is harmless. */
1380 init_format_info_table ()
1382 record_format_info (get_identifier ("printf"), 0, 1, 2);
1383 record_format_info (get_identifier ("fprintf"), 0, 2, 3);
1384 record_format_info (get_identifier ("sprintf"), 0, 2, 3);
1385 record_format_info (get_identifier ("scanf"), 1, 1, 2);
1386 record_format_info (get_identifier ("fscanf"), 1, 2, 3);
1387 record_format_info (get_identifier ("sscanf"), 1, 2, 3);
1388 record_format_info (get_identifier ("vprintf"), 0, 1, 0);
1389 record_format_info (get_identifier ("vfprintf"), 0, 2, 0);
1390 record_format_info (get_identifier ("vsprintf"), 0, 2, 0);
1393 static char tfaff
[] = "too few arguments for format";
1395 /* Check the argument list of a call to printf, scanf, etc.
1396 INFO points to the element of function_info_table.
1397 PARAMS is the list of argument values. */
1400 check_format (info
, params
)
1401 function_info
*info
;
1406 int suppressed
, wide
, precise
;
1415 format_char_info
*fci
;
1416 static char message
[132];
1419 /* Skip to format argument. If the argument isn't available, there's
1420 no work for us to do; prototype checking will catch the problem. */
1421 for (arg_num
= 1; ; ++arg_num
)
1425 if (arg_num
== info
->format_num
)
1427 params
= TREE_CHAIN (params
);
1429 format_tree
= TREE_VALUE (params
);
1430 params
= TREE_CHAIN (params
);
1431 if (format_tree
== 0)
1433 /* We can only check the format if it's a string constant. */
1434 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1435 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
1436 if (format_tree
== null_pointer_node
)
1438 warning ("null format string");
1441 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1443 format_tree
= TREE_OPERAND (format_tree
, 0);
1444 if (TREE_CODE (format_tree
) != STRING_CST
)
1446 format_chars
= TREE_STRING_POINTER (format_tree
);
1447 format_length
= TREE_STRING_LENGTH (format_tree
);
1448 if (format_length
<= 1)
1449 warning ("zero-length format string");
1450 if (format_chars
[--format_length
] != 0)
1452 warning ("unterminated format string");
1455 /* Skip to first argument to check. */
1456 while (arg_num
+ 1 < info
->first_arg_num
)
1460 params
= TREE_CHAIN (params
);
1465 if (*format_chars
== 0)
1467 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
1468 warning ("embedded `\\0' in format");
1469 if (info
->first_arg_num
!= 0 && params
!= 0)
1470 warning ("too many arguments for format");
1473 if (*format_chars
++ != '%')
1475 if (*format_chars
== 0)
1477 warning ("spurious trailing `%%' in format");
1480 if (*format_chars
== '%')
1486 suppressed
= wide
= precise
= FALSE
;
1489 suppressed
= *format_chars
== '*';
1492 while (ISDIGIT (*format_chars
))
1497 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
1499 if (index (flag_chars
, *format_chars
) != 0)
1501 sprintf (message
, "repeated `%c' flag in format",
1505 i
= strlen (flag_chars
);
1506 flag_chars
[i
++] = *format_chars
++;
1509 /* "If the space and + flags both appear,
1510 the space flag will be ignored." */
1511 if (index (flag_chars
, ' ') != 0
1512 && index (flag_chars
, '+') != 0)
1513 warning ("use of both ` ' and `+' flags in format");
1514 /* "If the 0 and - flags both appear,
1515 the 0 flag will be ignored." */
1516 if (index (flag_chars
, '0') != 0
1517 && index (flag_chars
, '-') != 0)
1518 warning ("use of both `0' and `-' flags in format");
1519 if (*format_chars
== '*')
1522 /* "...a field width...may be indicated by an asterisk.
1523 In this case, an int argument supplies the field width..." */
1530 if (info
->first_arg_num
!= 0)
1532 cur_param
= TREE_VALUE (params
);
1533 params
= TREE_CHAIN (params
);
1535 /* size_t is generally not valid here.
1536 It will work on most machines, because size_t and int
1537 have the same mode. But might as well warn anyway,
1538 since it will fail on other machines. */
1539 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1540 != integer_type_node
)
1543 "field width is not type int (arg %d)",
1551 while (ISDIGIT (*format_chars
))
1557 if (*format_chars
== '.')
1560 /* "For d, i, o, u, x, and X conversions,
1561 if a precision is specified, the 0 flag will be ignored.
1562 For other conversions, the behavior is undefined." */
1563 if (index (flag_chars
, '0') != 0)
1564 warning ("precision and `0' flag both used in one %%-sequence");
1566 if (*format_chars
!= '*' && !ISDIGIT (*format_chars
))
1567 warning ("`.' not followed by `*' or digit in format");
1568 /* "...a...precision...may be indicated by an asterisk.
1569 In this case, an int argument supplies the...precision." */
1570 if (*format_chars
== '*')
1572 if (info
->first_arg_num
!= 0)
1580 cur_param
= TREE_VALUE (params
);
1581 params
= TREE_CHAIN (params
);
1583 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1584 != integer_type_node
)
1587 "field width is not type int (arg %d)",
1595 while (ISDIGIT (*format_chars
))
1600 if (*format_chars
== 'h' || *format_chars
== 'l' || *format_chars
== 'L')
1601 length_char
= *format_chars
++;
1604 if (suppressed
&& length_char
!= 0)
1607 "use of `*' and `%c' together in format",
1611 format_char
= *format_chars
;
1612 if (format_char
== 0)
1614 warning ("conversion lacks type at end of format");
1618 fci
= info
->is_scan
? scan_table
: print_table
;
1621 if (fci
->format_chars
== 0
1622 || index (fci
->format_chars
, format_char
) != 0)
1626 if (fci
->format_chars
== 0)
1628 if (format_char
>= 040 && format_char
< 0177)
1630 "unknown conversion type character `%c' in format",
1634 "unknown conversion type character 0x%x in format",
1639 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
1641 sprintf (message
, "width used with `%c' format",
1645 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
1647 sprintf (message
, "precision used with `%c' format",
1653 if (index (fci
->flag_chars
, '*') == 0)
1656 "suppression of `%c' conversion in format",
1662 for (i
= 0; flag_chars
[i
] != 0; ++i
)
1664 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
1666 sprintf (message
, "flag `%c' used with type `%c'",
1667 flag_chars
[i
], format_char
);
1671 switch (length_char
)
1673 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
1674 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
1675 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
1676 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
1678 if (wanted_type
== 0)
1681 "use of `%c' length character with `%c' type character",
1682 length_char
, format_char
);
1687 ** XXX -- should kvetch about stuff such as
1691 ** scanf ("%d", &i);
1695 /* Finally. . .check type of argument against desired type! */
1696 if (info
->first_arg_num
== 0)
1703 cur_param
= TREE_VALUE (params
);
1704 params
= TREE_CHAIN (params
);
1706 cur_type
= TREE_TYPE (cur_param
);
1708 /* Check the types of any additional pointer arguments
1709 that precede the "real" argument. */
1710 for (i
= 0; i
< fci
->pointer_count
; ++i
)
1712 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1714 cur_type
= TREE_TYPE (cur_type
);
1718 "format argument is not a %s (arg %d)",
1719 ((fci
->pointer_count
== 1) ? "pointer" : "pointer to a pointer"),
1725 /* Check the type of the "real" argument, if there's a type we want. */
1726 if (i
== fci
->pointer_count
&& wanted_type
!= 0
1727 && wanted_type
!= TYPE_MAIN_VARIANT (cur_type
)
1728 /* If we want `void *', allow any pointer type.
1729 (Anything else would already have got a warning.) */
1730 && ! (wanted_type
== void_type_node
1731 && fci
->pointer_count
> 0)
1732 /* Don't warn about differences merely in signedness. */
1733 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
1734 && TREE_CODE (cur_type
) == INTEGER_TYPE
1735 && TYPE_PRECISION (wanted_type
) == TYPE_PRECISION (cur_type
)))
1737 register char *this;
1738 register char *that
;
1740 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
1742 if (TYPE_NAME (cur_type
) != 0
1743 && TREE_CODE (cur_type
) != INTEGER_TYPE
1744 && !(TREE_CODE (cur_type
) == POINTER_TYPE
1745 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
)
1746 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
1747 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1749 /* A nameless type can't possibly match what the format wants.
1750 So there will be a warning for it.
1751 Make up a string to describe vaguely what it is. */
1754 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1757 that
= "different type";
1760 if (strcmp (this, that
) != 0)
1762 sprintf (message
, "%s format, %s arg (arg %d)",
1763 this, that
, arg_num
);
1770 /* Build a function call to function FUNCTION with parameters PARAMS.
1771 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1772 TREE_VALUE of each node is a parameter-expression.
1773 FUNCTION's data type may be a function type or a pointer-to-function. */
1776 build_function_call (function
, params
)
1777 tree function
, params
;
1779 register tree fntype
;
1780 register tree coerced_params
;
1781 tree name
= NULL_TREE
;
1783 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1784 STRIP_TYPE_NOPS (function
);
1786 /* Convert anything with function type to a pointer-to-function. */
1787 if (TREE_CODE (function
) == FUNCTION_DECL
)
1789 name
= DECL_NAME (function
);
1790 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1791 (because calling an inline function does not mean the function
1792 needs to be separately compiled). */
1793 fntype
= build_type_variant (TREE_TYPE (function
),
1794 TREE_READONLY (function
),
1795 TREE_THIS_VOLATILE (function
));
1796 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
1799 function
= default_conversion (function
);
1801 fntype
= TREE_TYPE (function
);
1803 if (TREE_CODE (fntype
) == ERROR_MARK
)
1804 return error_mark_node
;
1806 if (!(TREE_CODE (fntype
) == POINTER_TYPE
1807 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
1809 error ("called object is not a function");
1810 return error_mark_node
;
1813 /* fntype now gets the type of function pointed to. */
1814 fntype
= TREE_TYPE (fntype
);
1816 /* Convert the parameters to the types declared in the
1817 function prototype, or apply default promotions. */
1820 = convert_arguments (TYPE_ARG_TYPES (fntype
), params
, name
);
1822 /* Check for errors in format strings. */
1823 if (warn_format
&& name
!= 0)
1827 /* See if this function is a format function. */
1828 for (i
= 0; i
< function_info_entries
; i
++)
1829 if (function_info_table
[i
].function_ident
== name
)
1831 register char *message
;
1833 /* If so, check it. */
1834 check_format (&function_info_table
[i
], coerced_params
);
1839 /* Recognize certain built-in functions so we can make tree-codes
1840 other than CALL_EXPR. We do this when it enables fold-const.c
1841 to do something useful. */
1843 if (TREE_CODE (function
) == ADDR_EXPR
1844 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
1845 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
1846 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function
, 0)))
1851 if (coerced_params
== 0)
1852 return integer_zero_node
;
1853 return build_unary_op (ABS_EXPR
, TREE_VALUE (coerced_params
), 0);
1857 register tree result
1858 = build (CALL_EXPR
, TREE_TYPE (fntype
),
1859 function
, coerced_params
, NULL_TREE
);
1861 TREE_SIDE_EFFECTS (result
) = 1;
1862 if (TREE_TYPE (result
) == void_type_node
)
1864 return require_complete_type (result
);
1868 /* Convert the argument expressions in the list VALUES
1869 to the types in the list TYPELIST. The result is a list of converted
1870 argument expressions.
1872 If TYPELIST is exhausted, or when an element has NULL as its type,
1873 perform the default conversions.
1875 PARMLIST is the chain of parm decls for the function being called.
1876 It may be 0, if that info is not available.
1877 It is used only for generating error messages.
1879 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1881 This is also where warnings about wrong number of args are generated.
1883 Both VALUES and the returned value are chains of TREE_LIST nodes
1884 with the elements of the list in the TREE_VALUE slots of those nodes. */
1887 convert_arguments (typelist
, values
, name
)
1888 tree typelist
, values
, name
;
1890 register tree typetail
, valtail
;
1891 register tree result
= NULL
;
1894 /* Scan the given expressions and types, producing individual
1895 converted arguments and pushing them on RESULT in reverse order. */
1897 for (valtail
= values
, typetail
= typelist
, parmnum
= 0;
1899 valtail
= TREE_CHAIN (valtail
), parmnum
++)
1901 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
1902 register tree val
= TREE_VALUE (valtail
);
1904 if (type
== void_type_node
)
1907 error ("too many arguments to function `%s'",
1908 IDENTIFIER_POINTER (name
));
1910 error ("too many arguments to function");
1914 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1915 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1916 to convert automatically to a pointer. */
1917 if (TREE_CODE (val
) == NON_LVALUE_EXPR
)
1918 val
= TREE_OPERAND (val
, 0);
1920 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
1921 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
)
1922 val
= default_conversion (val
);
1924 val
= require_complete_type (val
);
1928 /* Formal parm type is specified by a function prototype. */
1931 if (TYPE_SIZE (type
) == 0)
1933 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
1939 #ifdef PROMOTE_PROTOTYPES
1940 /* Rather than truncating and then reextending,
1941 convert directly to int, if that's the type we will want. */
1942 if (! flag_traditional
1943 && TREE_CODE (type
) == INTEGER_TYPE
1944 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
1945 type
= integer_type_node
;
1948 #if 0 /* This turns out not to win--there's no way to write a prototype
1949 for a function whose arg type is a union with no tag. */
1950 /* Nameless union automatically casts the types it contains. */
1951 if (TREE_CODE (type
) == UNION_TYPE
&& TYPE_NAME (type
) == 0)
1955 for (field
= TYPE_FIELDS (type
); field
;
1956 field
= TREE_CHAIN (field
))
1957 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
1958 TYPE_MAIN_VARIANT (TREE_TYPE (val
))))
1962 val
= build1 (CONVERT_EXPR
, type
, val
);
1966 /* Optionally warn about conversions that
1967 differ from the default conversions. */
1968 if (warn_conversion
)
1970 int formal_prec
= TYPE_PRECISION (type
);
1972 if (TREE_CODE (type
) != REAL_TYPE
1973 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
1974 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
1975 else if (TREE_CODE (type
) == REAL_TYPE
1976 && TREE_CODE (TREE_TYPE (val
)) != REAL_TYPE
)
1977 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
1978 else if (TREE_CODE (type
) == REAL_TYPE
1979 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
1981 /* Warn if any argument is passed as `float',
1982 since without a prototype it would be `double'. */
1983 if (formal_prec
== TYPE_PRECISION (float_type_node
))
1984 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name
, parmnum
+ 1);
1986 /* Detect integer changing in width or signedness. */
1987 else if ((TREE_CODE (type
) == INTEGER_TYPE
1988 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1989 && (TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
1990 || TREE_CODE (TREE_TYPE (val
)) == ENUMERAL_TYPE
))
1992 tree would_have_been
= default_conversion (val
);
1993 tree type1
= TREE_TYPE (would_have_been
);
1995 if (TREE_CODE (type
) == ENUMERAL_TYPE
1996 && type
== TREE_TYPE (val
))
1997 /* No warning if function asks for enum
1998 and the actual arg is that enum type. */
2000 else if (formal_prec
!= TYPE_PRECISION (type1
))
2001 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name
, parmnum
+ 1);
2002 else if (TREE_UNSIGNED (type
) == TREE_UNSIGNED (type1
))
2004 /* Don't complain if the formal parameter type
2005 is an enum, because we can't tell now whether
2006 the value was an enum--even the same enum. */
2007 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
2009 else if (TREE_CODE (val
) == INTEGER_CST
2010 && int_fits_type_p (val
, type
))
2011 /* Change in signedness doesn't matter
2012 if a constant value is unaffected. */
2014 else if (TREE_CODE (TREE_TYPE (val
)) == ENUMERAL_TYPE
2015 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val
)), type
)
2016 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val
)), type
))
2017 /* Change in signedness doesn't matter
2018 if an enum value is unaffected. */
2020 else if (TREE_UNSIGNED (type
))
2021 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name
, parmnum
+ 1);
2023 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name
, parmnum
+ 1);
2027 parmval
= convert_for_assignment (type
, val
,
2028 (char *)0, /* arg passing */
2031 #ifdef PROMOTE_PROTOTYPES
2032 if (TREE_CODE (type
) == INTEGER_TYPE
2033 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2034 parmval
= default_conversion (parmval
);
2037 result
= tree_cons (NULL_TREE
, parmval
, result
);
2039 else if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
2040 && (TYPE_PRECISION (TREE_TYPE (val
))
2041 < TYPE_PRECISION (double_type_node
)))
2042 /* Convert `float' to `double'. */
2043 result
= tree_cons (NULL_TREE
, convert (double_type_node
, val
), result
);
2045 /* Convert `short' and `char' to full-size `int'. */
2046 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
2049 typetail
= TREE_CHAIN (typetail
);
2052 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
2055 error ("too few arguments to function `%s'",
2056 IDENTIFIER_POINTER (name
));
2058 error ("too few arguments to function");
2061 return nreverse (result
);
2064 /* This is the entry point used by the parser
2065 for binary operators in the input.
2066 In addition to constructing the expression,
2067 we check for operands that were written with other binary operators
2068 in a way that is likely to confuse the user. */
2071 parser_build_binary_op (code
, arg1
, arg2
)
2072 enum tree_code code
;
2075 tree result
= build_binary_op (code
, arg1
, arg2
, 1);
2078 char class1
= TREE_CODE_CLASS (TREE_CODE (arg1
));
2079 char class2
= TREE_CODE_CLASS (TREE_CODE (arg2
));
2080 enum tree_code code1
= ERROR_MARK
;
2081 enum tree_code code2
= ERROR_MARK
;
2083 if (class1
== 'e' || class1
== '1'
2084 || class1
== '2' || class1
== '<')
2085 code1
= C_EXP_ORIGINAL_CODE (arg1
);
2086 if (class2
== 'e' || class2
== '1'
2087 || class2
== '2' || class2
== '<')
2088 code2
= C_EXP_ORIGINAL_CODE (arg2
);
2090 /* Check for cases such as x+y<<z which users are likely
2091 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2092 is cleared to prevent these warnings. */
2093 if (warn_parentheses
)
2095 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
2097 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2098 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2099 warning ("suggest parentheses around + or - inside shift");
2102 if (code
== TRUTH_ORIF_EXPR
)
2104 if (code1
== TRUTH_ANDIF_EXPR
2105 || code2
== TRUTH_ANDIF_EXPR
)
2106 warning ("suggest parentheses around && within ||");
2109 if (code
== BIT_IOR_EXPR
)
2111 if (code1
== BIT_AND_EXPR
|| code1
== BIT_XOR_EXPR
2112 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2113 || code2
== BIT_AND_EXPR
|| code2
== BIT_XOR_EXPR
2114 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2115 warning ("suggest parentheses around arithmetic in operand of |");
2118 if (code
== BIT_XOR_EXPR
)
2120 if (code1
== BIT_AND_EXPR
2121 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2122 || code2
== BIT_AND_EXPR
2123 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2124 warning ("suggest parentheses around arithmetic in operand of ^");
2127 if (code
== BIT_AND_EXPR
)
2129 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2130 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2131 warning ("suggest parentheses around + or - in operand of &");
2135 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2136 if (TREE_CODE_CLASS (code
) == '<' && extra_warnings
2137 && (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<'))
2138 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2140 class = TREE_CODE_CLASS (TREE_CODE (result
));
2142 /* Record the code that was specified in the source,
2143 for the sake of warnings about confusing nesting. */
2144 if (class == 'e' || class == '1'
2145 || class == '2' || class == '<')
2146 C_SET_EXP_ORIGINAL_CODE (result
, code
);
2149 int flag
= TREE_CONSTANT (result
);
2150 result
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (result
), result
);
2151 C_SET_EXP_ORIGINAL_CODE (result
, code
);
2152 TREE_CONSTANT (result
) = flag
;
2158 /* Build a binary-operation expression without default conversions.
2159 CODE is the kind of expression to build.
2160 This function differs from `build' in several ways:
2161 the data type of the result is computed and recorded in it,
2162 warnings are generated if arg data types are invalid,
2163 special handling for addition and subtraction of pointers is known,
2164 and some optimization is done (operations on narrow ints
2165 are done in the narrower type when that gives the same result).
2166 Constant folding is also done before the result is returned.
2168 Note that the operands will never have enumeral types, or function
2169 or array types, because either they will have the default conversions
2170 performed or they have both just been converted to some other type in which
2171 the arithmetic is to be done. */
2174 build_binary_op (code
, orig_op0
, orig_op1
, convert_p
)
2175 enum tree_code code
;
2176 tree orig_op0
, orig_op1
;
2180 register enum tree_code code0
, code1
;
2183 /* Expression code to give to the expression when it is built.
2184 Normally this is CODE, which is what the caller asked for,
2185 but in some special cases we change it. */
2186 register enum tree_code resultcode
= code
;
2188 /* Data type in which the computation is to be performed.
2189 In the simplest cases this is the common type of the arguments. */
2190 register tree result_type
= NULL
;
2192 /* Nonzero means operands have already been type-converted
2193 in whatever way is necessary.
2194 Zero means they need to be converted to RESULT_TYPE. */
2197 /* Nonzero means after finally constructing the expression
2198 give it this type. Otherwise, give it type RESULT_TYPE. */
2199 tree final_type
= 0;
2201 /* Nonzero if this is an operation like MIN or MAX which can
2202 safely be computed in short if both args are promoted shorts.
2203 Also implies COMMON.
2204 -1 indicates a bitwise operation; this makes a difference
2205 in the exact conditions for when it is safe to do the operation
2206 in a narrower mode. */
2209 /* Nonzero if this is a comparison operation;
2210 if both args are promoted shorts, compare the original shorts.
2211 Also implies COMMON. */
2212 int short_compare
= 0;
2214 /* Nonzero if this is a right-shift operation, which can be computed on the
2215 original short and then promoted if the operand is a promoted short. */
2216 int short_shift
= 0;
2218 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2223 op0
= default_conversion (orig_op0
);
2224 op1
= default_conversion (orig_op1
);
2232 type0
= TREE_TYPE (op0
);
2233 type1
= TREE_TYPE (op1
);
2235 /* The expression codes of the data types of the arguments tell us
2236 whether the arguments are integers, floating, pointers, etc. */
2237 code0
= TREE_CODE (type0
);
2238 code1
= TREE_CODE (type1
);
2240 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2241 STRIP_TYPE_NOPS (op0
);
2242 STRIP_TYPE_NOPS (op1
);
2244 /* If an error was already reported for one of the arguments,
2245 avoid reporting another error. */
2247 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
2248 return error_mark_node
;
2253 /* Handle the pointer + int case. */
2254 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2255 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
2256 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
2257 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
2263 /* Subtraction of two similar pointers.
2264 We must subtract them as integers, then divide by object size. */
2265 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
2266 && comp_target_types (type0
, type1
))
2267 return pointer_diff (op0
, op1
);
2268 /* Handle pointer minus int. Just like pointer plus int. */
2269 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2270 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
2279 case TRUNC_DIV_EXPR
:
2281 case FLOOR_DIV_EXPR
:
2282 case ROUND_DIV_EXPR
:
2283 case EXACT_DIV_EXPR
:
2284 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
2285 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
2287 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
2288 resultcode
= RDIV_EXPR
;
2290 /* When dividing two signed integers, you have to promote to int.
2291 E.g. (short) -32868 / (short) -1 doesn't fit in a short. */
2292 shorten
= TREE_UNSIGNED (op0
);
2298 case BIT_ANDTC_EXPR
:
2301 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2303 /* If one operand is a constant, and the other is a short type
2304 that has been converted to an int,
2305 really do the work in the short type and then convert the
2306 result to int. If we are lucky, the constant will be 0 or 1
2307 in the short type, making the entire operation go away. */
2308 if (TREE_CODE (op0
) == INTEGER_CST
2309 && TREE_CODE (op1
) == NOP_EXPR
2310 && TYPE_PRECISION (type1
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))
2311 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
2313 final_type
= result_type
;
2314 op1
= TREE_OPERAND (op1
, 0);
2315 result_type
= TREE_TYPE (op1
);
2317 if (TREE_CODE (op1
) == INTEGER_CST
2318 && TREE_CODE (op0
) == NOP_EXPR
2319 && TYPE_PRECISION (type0
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))
2320 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
2322 final_type
= result_type
;
2323 op0
= TREE_OPERAND (op0
, 0);
2324 result_type
= TREE_TYPE (op0
);
2328 case TRUNC_MOD_EXPR
:
2329 case FLOOR_MOD_EXPR
:
2330 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2334 case TRUTH_ANDIF_EXPR
:
2335 case TRUTH_ORIF_EXPR
:
2336 case TRUTH_AND_EXPR
:
2338 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
|| code0
== REAL_TYPE
)
2339 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
|| code1
== REAL_TYPE
))
2341 /* Result of these operations is always an int,
2342 but that does not mean the operands should be
2343 converted to ints! */
2344 result_type
= integer_type_node
;
2345 op0
= truthvalue_conversion (op0
);
2346 op1
= truthvalue_conversion (op1
);
2351 /* Shift operations: result has same type as first operand;
2352 always convert second operand to int.
2353 Also set SHORT_SHIFT if shifting rightward. */
2356 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2358 if (TREE_CODE (op1
) == INTEGER_CST
)
2360 if (TREE_INT_CST_LOW (op1
) > 0)
2362 else if (TREE_INT_CST_LOW (op1
) < 0)
2363 warning ("shift count is negative");
2364 if (TREE_INT_CST_LOW (op1
) >= TYPE_PRECISION (type0
))
2365 warning ("shift count >= width of type");
2367 /* Use the type of the value to be shifted.
2368 This is what most traditional C compilers do. */
2369 result_type
= type0
;
2370 /* Unless traditional, convert the shift-count to an integer,
2371 regardless of size of value being shifted. */
2372 if (! flag_traditional
)
2374 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
2375 op1
= convert (integer_type_node
, op1
);
2376 /* Avoid converting op1 to result_type later. */
2383 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2385 if (TREE_CODE (op1
) == INTEGER_CST
2386 && TREE_INT_CST_LOW (op1
) < 0)
2387 warning ("shift count is negative");
2388 if (TREE_CODE (op1
) == INTEGER_CST
2389 && TREE_INT_CST_LOW (op1
) >= TYPE_PRECISION (type0
))
2390 warning ("shift count >= width of type");
2391 /* Use the type of the value to be shifted.
2392 This is what most traditional C compilers do. */
2393 result_type
= type0
;
2394 /* Unless traditional, convert the shift-count to an integer,
2395 regardless of size of value being shifted. */
2396 if (! flag_traditional
)
2398 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
2399 op1
= convert (integer_type_node
, op1
);
2400 /* Avoid converting op1 to result_type later. */
2408 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2410 if (TREE_CODE (op1
) == INTEGER_CST
2411 && TREE_INT_CST_LOW (op1
) < 0)
2412 warning ("shift count is negative");
2413 if (TREE_CODE (op1
) == INTEGER_CST
2414 && TREE_INT_CST_LOW (op1
) >= TYPE_PRECISION (type0
))
2415 warning ("shift count >= width of type");
2416 /* Use the type of the value to be shifted.
2417 This is what most traditional C compilers do. */
2418 result_type
= type0
;
2419 /* Unless traditional, convert the shift-count to an integer,
2420 regardless of size of value being shifted. */
2421 if (! flag_traditional
)
2423 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
2424 op1
= convert (integer_type_node
, op1
);
2425 /* Avoid converting op1 to result_type later. */
2433 /* Result of comparison is always int,
2434 but don't convert the args to int! */
2435 result_type
= integer_type_node
;
2437 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
2438 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
2440 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
2442 register tree tt0
= TREE_TYPE (type0
);
2443 register tree tt1
= TREE_TYPE (type1
);
2444 /* Anything compares with void *. void * compares with anything.
2445 Otherwise, the targets must be the same. */
2446 if (comp_target_types (type0
, type1
))
2448 else if (TYPE_MAIN_VARIANT (tt0
) == void_type_node
)
2450 if (pedantic
&& !integer_zerop (op0
)
2451 && TREE_CODE (tt1
) == FUNCTION_TYPE
)
2452 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2454 else if (TYPE_MAIN_VARIANT (tt1
) == void_type_node
)
2456 if (pedantic
&& !integer_zerop (op1
)
2457 && TREE_CODE (tt0
) == FUNCTION_TYPE
)
2458 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2461 pedwarn ("comparison of distinct pointer types lacks a cast");
2463 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
2464 && integer_zerop (op1
))
2465 op1
= null_pointer_node
;
2466 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
2467 && integer_zerop (op0
))
2468 op0
= null_pointer_node
;
2469 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2471 if (! flag_traditional
)
2472 pedwarn ("comparison between pointer and integer");
2473 op1
= convert (TREE_TYPE (op0
), op1
);
2475 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
2477 if (! flag_traditional
)
2478 pedwarn ("comparison between pointer and integer");
2479 op0
= convert (TREE_TYPE (op1
), op0
);
2482 /* If args are not valid, clear out RESULT_TYPE
2483 to cause an error message later. */
2489 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
2490 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
2492 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
2494 if (! comp_target_types (type0
, type1
))
2495 pedwarn ("comparison of distinct pointer types lacks a cast");
2497 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
2498 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2499 result_type
= common_type (type0
, type1
);
2507 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
2508 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
2510 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
2512 if (! comp_target_types (type0
, type1
))
2513 pedwarn ("comparison of distinct pointer types lacks a cast");
2515 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
2516 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2517 result_type
= integer_type_node
;
2519 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
2520 && integer_zerop (op1
))
2522 result_type
= integer_type_node
;
2523 op1
= null_pointer_node
;
2524 if (! flag_traditional
)
2525 pedwarn ("ordered comparison of pointer with integer zero");
2527 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
2528 && integer_zerop (op0
))
2530 result_type
= integer_type_node
;
2531 op0
= null_pointer_node
;
2533 pedwarn ("ordered comparison of pointer with integer zero");
2535 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2537 result_type
= integer_type_node
;
2538 if (! flag_traditional
)
2539 pedwarn ("comparison between pointer and integer");
2540 op1
= convert (TREE_TYPE (op0
), op1
);
2542 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
2544 result_type
= integer_type_node
;
2545 if (! flag_traditional
)
2546 pedwarn ("comparison between pointer and integer");
2547 op0
= convert (TREE_TYPE (op1
), op0
);
2553 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
2554 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
2556 if (shorten
|| common
|| short_compare
)
2557 result_type
= common_type (type0
, type1
);
2559 /* For certain operations (which identify themselves by shorten != 0)
2560 if both args were extended from the same smaller type,
2561 do the arithmetic in that type and then extend.
2563 shorten !=0 and !=1 indicates a bitwise operation.
2564 For them, this optimization is safe only if
2565 both args are zero-extended or both are sign-extended.
2566 Otherwise, we might change the result.
2567 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2568 but calculated in (unsigned short) it would be (unsigned short)-1. */
2572 int unsigned0
, unsigned1
;
2573 tree arg0
= get_narrower (op0
, &unsigned0
);
2574 tree arg1
= get_narrower (op1
, &unsigned1
);
2575 /* UNS is 1 if the operation to be done is an unsigned one. */
2576 int uns
= TREE_UNSIGNED (result_type
);
2579 final_type
= result_type
;
2581 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2582 but it *requires* conversion to FINAL_TYPE. */
2584 if ((TYPE_PRECISION (TREE_TYPE (op0
))
2585 == TYPE_PRECISION (TREE_TYPE (arg0
)))
2586 && TREE_TYPE (op0
) != final_type
)
2587 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
2588 if ((TYPE_PRECISION (TREE_TYPE (op1
))
2589 == TYPE_PRECISION (TREE_TYPE (arg1
)))
2590 && TREE_TYPE (op1
) != final_type
)
2591 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
2593 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2595 /* For bitwise operations, signedness of nominal type
2596 does not matter. Consider only how operands were extended. */
2600 /* Note that in all three cases below we refrain from optimizing
2601 an unsigned operation on sign-extended args.
2602 That would not be valid. */
2604 /* Both args variable: if both extended in same way
2605 from same width, do it in that width.
2606 Do it unsigned if args were zero-extended. */
2607 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
2608 < TYPE_PRECISION (result_type
))
2609 && (TYPE_PRECISION (TREE_TYPE (arg1
))
2610 == TYPE_PRECISION (TREE_TYPE (arg0
)))
2611 && unsigned0
== unsigned1
2612 && (unsigned0
|| !uns
))
2614 = signed_or_unsigned_type (unsigned0
,
2615 common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
2616 else if (TREE_CODE (arg0
) == INTEGER_CST
2617 && (unsigned1
|| !uns
)
2618 && (TYPE_PRECISION (TREE_TYPE (arg1
))
2619 < TYPE_PRECISION (result_type
))
2620 && (type
= signed_or_unsigned_type (unsigned1
,
2622 int_fits_type_p (arg0
, type
)))
2624 else if (TREE_CODE (arg1
) == INTEGER_CST
2625 && (unsigned0
|| !uns
)
2626 && (TYPE_PRECISION (TREE_TYPE (arg0
))
2627 < TYPE_PRECISION (result_type
))
2628 && (type
= signed_or_unsigned_type (unsigned0
,
2630 int_fits_type_p (arg1
, type
)))
2634 /* Shifts can be shortened if shifting right. */
2639 tree arg0
= get_narrower (op0
, &unsigned_arg
);
2641 final_type
= result_type
;
2643 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
2644 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
2646 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
2647 /* If arg is sign-extended and then unsigned-shifted,
2648 we can simulate this with a signed shift in arg's type
2649 only if the extended result is at least twice as wide
2650 as the arg. Otherwise, the shift could use up all the
2651 ones made by sign-extension and bring in zeros.
2652 We can't optimize that case at all, but in most machines
2653 it never happens because available widths are 2**N. */
2654 && (!TREE_UNSIGNED (final_type
)
2656 || 2 * TYPE_PRECISION (TREE_TYPE (arg0
)) <= TYPE_PRECISION (result_type
)))
2658 /* Do an unsigned shift if the operand was zero-extended. */
2660 = signed_or_unsigned_type (unsigned_arg
,
2662 /* Convert value-to-be-shifted to that type. */
2663 if (TREE_TYPE (op0
) != result_type
)
2664 op0
= convert (result_type
, op0
);
2669 /* Comparison operations are shortened too but differently.
2670 They identify themselves by setting short_compare = 1. */
2674 /* Don't write &op0, etc., because that would prevent op0
2675 from being kept in a register.
2676 Instead, make copies of the our local variables and
2677 pass the copies by reference, then copy them back afterward. */
2678 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
2679 enum tree_code xresultcode
= resultcode
;
2681 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
2684 op0
= xop0
, op1
= xop1
, result_type
= xresult_type
;
2685 resultcode
= xresultcode
;
2689 tree op0_type
= TREE_TYPE (orig_op0
);
2690 tree op1_type
= TREE_TYPE (orig_op1
);
2691 int op0_unsigned
= TREE_UNSIGNED (op0_type
);
2692 int op1_unsigned
= TREE_UNSIGNED (op1_type
);
2694 /* Give warnings for comparisons between signed and unsigned
2695 quantities that will fail. Do not warn if the signed quantity
2696 is an unsuffixed integer literal (or some static constant
2697 expression involving such literals) and it is positive.
2698 Do not warn if the width of the unsigned quantity is less
2699 than that of the signed quantity, since in this case all
2700 values of the unsigned quantity fit in the signed quantity.
2701 Do not warn if the signed type is the same size as the
2702 result_type since sign extension does not cause trouble in
2704 /* Do the checking based on the original operand trees, so that
2705 casts will be considered, but default promotions won't be. */
2706 if (op0_unsigned
!= op1_unsigned
2708 && TYPE_PRECISION (op0_type
) >= TYPE_PRECISION (op1_type
)
2709 && TYPE_PRECISION (op0_type
) < TYPE_PRECISION (result_type
)
2710 && (TREE_CODE (op1
) != INTEGER_CST
2711 || (TREE_CODE (op1
) == INTEGER_CST
2712 && INT_CST_LT (op1
, integer_zero_node
))))
2715 && TYPE_PRECISION (op1_type
) >= TYPE_PRECISION (op0_type
)
2716 && TYPE_PRECISION (op1_type
) < TYPE_PRECISION (result_type
)
2717 && (TREE_CODE (op0
) != INTEGER_CST
2718 || (TREE_CODE (op0
) == INTEGER_CST
2719 && INT_CST_LT (op0
, integer_zero_node
))))))
2720 warning ("comparison between signed and unsigned");
2725 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2726 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2727 Then the expression will be built.
2728 It will be given type FINAL_TYPE if that is nonzero;
2729 otherwise, it will be given type RESULT_TYPE. */
2733 binary_op_error (code
);
2734 return error_mark_node
;
2739 if (TREE_TYPE (op0
) != result_type
)
2740 op0
= convert (result_type
, op0
);
2741 if (TREE_TYPE (op1
) != result_type
)
2742 op1
= convert (result_type
, op1
);
2746 register tree result
= build (resultcode
, result_type
, op0
, op1
);
2747 register tree folded
;
2749 folded
= fold (result
);
2750 if (folded
== result
)
2751 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
2752 if (final_type
!= 0)
2753 return convert (final_type
, folded
);
2758 /* Return a tree for the sum or difference (RESULTCODE says which)
2759 of pointer PTROP and integer INTOP. */
2762 pointer_int_sum (resultcode
, ptrop
, intop
)
2763 enum tree_code resultcode
;
2764 register tree ptrop
, intop
;
2768 register tree result
;
2769 register tree folded
;
2771 /* The result is a pointer of the same type that is being added. */
2773 register tree result_type
= TREE_TYPE (ptrop
);
2775 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
2777 if (pedantic
|| warn_pointer_arith
)
2778 pedwarn ("pointer of type `void *' used in arithmetic");
2779 size_exp
= integer_one_node
;
2781 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
2783 if (pedantic
|| warn_pointer_arith
)
2784 pedwarn ("pointer to a function used in arithmetic");
2785 size_exp
= integer_one_node
;
2788 size_exp
= c_size_in_bytes (TREE_TYPE (result_type
));
2790 /* If what we are about to multiply by the size of the elements
2791 contains a constant term, apply distributive law
2792 and multiply that constant term separately.
2793 This helps produce common subexpressions. */
2795 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
2796 && ! TREE_CONSTANT (intop
)
2797 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
2798 && TREE_CONSTANT (size_exp
)
2799 /* If the constant comes from pointer subtraction,
2800 skip this optimization--it would cause an error. */
2801 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop
, 0))) == INTEGER_TYPE
)
2803 enum tree_code subcode
= resultcode
;
2804 tree int_type
= TREE_TYPE (intop
);
2805 if (TREE_CODE (intop
) == MINUS_EXPR
)
2806 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
2807 /* Convert both subexpression types to the type of intop,
2808 because weird cases involving pointer arithmetic
2809 can result in a sum or difference with different type args. */
2810 ptrop
= build_binary_op (subcode
, ptrop
,
2811 convert (int_type
, TREE_OPERAND (intop
, 1)), 1);
2812 intop
= convert (int_type
, TREE_OPERAND (intop
, 0));
2815 /* Convert the integer argument to a type the same size as a pointer
2816 so the multiply won't overflow spuriously. */
2818 if (TYPE_PRECISION (TREE_TYPE (intop
)) != POINTER_SIZE
)
2819 intop
= convert (type_for_size (POINTER_SIZE
, 0), intop
);
2821 /* Replace the integer argument
2822 with a suitable product by the object size. */
2824 intop
= build_binary_op (MULT_EXPR
, intop
, size_exp
, 1);
2826 /* Create the sum or difference. */
2828 result
= build (resultcode
, result_type
, ptrop
, intop
);
2830 folded
= fold (result
);
2831 if (folded
== result
)
2832 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
2836 /* Return a tree for the difference of pointers OP0 and OP1.
2837 The resulting tree has type int. */
2840 pointer_diff (op0
, op1
)
2841 register tree op0
, op1
;
2843 register tree result
, folded
;
2844 tree restype
= ptrdiff_type_node
;
2846 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
2848 if (pedantic
|| warn_pointer_arith
)
2850 if (TREE_CODE (target_type
) == VOID_TYPE
)
2851 pedwarn ("pointer of type `void *' used in subtraction");
2852 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
2853 pedwarn ("pointer to a function used in subtraction");
2856 /* First do the subtraction as integers;
2857 then drop through to build the divide operator. */
2859 op0
= build_binary_op (MINUS_EXPR
, convert (restype
, op0
),
2860 convert (restype
, op1
), 1);
2861 op1
= c_size_in_bytes (target_type
);
2863 /* Divide by the size, in easiest possible way. */
2865 result
= build (EXACT_DIV_EXPR
, restype
, op0
, op1
);
2867 folded
= fold (result
);
2868 if (folded
== result
)
2869 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
2873 /* Construct and perhaps optimize a tree representation
2874 for a unary operation. CODE, a tree_code, specifies the operation
2875 and XARG is the operand. NOCONVERT nonzero suppresses
2876 the default promotions (such as from short to int). */
2879 build_unary_op (code
, xarg
, noconvert
)
2880 enum tree_code code
;
2884 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2885 register tree arg
= xarg
;
2886 register tree argtype
= 0;
2887 register enum tree_code typecode
= TREE_CODE (TREE_TYPE (arg
));
2888 char *errstring
= NULL
;
2891 if (typecode
== ERROR_MARK
)
2892 return error_mark_node
;
2893 if (typecode
== ENUMERAL_TYPE
)
2894 typecode
= INTEGER_TYPE
;
2899 /* This is used for unary plus, because a CONVERT_EXPR
2900 is enough to prevent anybody from looking inside for
2901 associativity, but won't generate any code. */
2902 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
2903 errstring
= "wrong type argument to unary plus";
2904 else if (!noconvert
)
2905 arg
= default_conversion (arg
);
2909 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
2910 errstring
= "wrong type argument to unary minus";
2911 else if (!noconvert
)
2912 arg
= default_conversion (arg
);
2916 if (typecode
!= INTEGER_TYPE
)
2917 errstring
= "wrong type argument to bit-complement";
2918 else if (!noconvert
)
2919 arg
= default_conversion (arg
);
2923 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
2924 errstring
= "wrong type argument to abs";
2925 else if (!noconvert
)
2926 arg
= default_conversion (arg
);
2929 case TRUTH_NOT_EXPR
:
2930 if (typecode
!= INTEGER_TYPE
2931 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
2932 /* These will convert to a pointer. */
2933 && typecode
!= ARRAY_TYPE
&& typecode
!= FUNCTION_TYPE
)
2935 errstring
= "wrong type argument to unary exclamation mark";
2938 arg
= truthvalue_conversion (arg
);
2939 return invert_truthvalue (arg
);
2944 case PREINCREMENT_EXPR
:
2945 case POSTINCREMENT_EXPR
:
2946 case PREDECREMENT_EXPR
:
2947 case POSTDECREMENT_EXPR
:
2948 /* Handle complex lvalues (when permitted)
2949 by reduction to simpler cases. */
2951 val
= unary_complex_lvalue (code
, arg
);
2955 /* Report invalid types. */
2957 if (typecode
!= POINTER_TYPE
2958 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
)
2960 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2961 errstring
="wrong type argument to increment";
2963 errstring
="wrong type argument to decrement";
2969 tree result_type
= TREE_TYPE (arg
);
2971 arg
= get_unwidened (arg
, 0);
2972 argtype
= TREE_TYPE (arg
);
2974 /* Compute the increment. */
2976 if (typecode
== POINTER_TYPE
)
2978 if ((pedantic
|| warn_pointer_arith
)
2979 && (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
2980 || TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
))
2981 pedwarn ("wrong type argument to %s",
2982 ((code
== PREINCREMENT_EXPR
2983 || code
== POSTINCREMENT_EXPR
)
2984 ? "increment" : "decrement"));
2985 inc
= c_sizeof_nowarn (TREE_TYPE (result_type
));
2988 inc
= integer_one_node
;
2990 inc
= convert (argtype
, inc
);
2992 /* Handle incrementing a cast-expression. */
2995 switch (TREE_CODE (arg
))
3000 case FIX_TRUNC_EXPR
:
3001 case FIX_FLOOR_EXPR
:
3002 case FIX_ROUND_EXPR
:
3004 /* If the real type has the same machine representation
3005 as the type it is cast to, we can make better output
3006 by adding directly to the inside of the cast. */
3007 if ((TREE_CODE (TREE_TYPE (arg
))
3008 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))))
3009 && (TYPE_MODE (TREE_TYPE (arg
))
3010 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg
, 0)))))
3011 arg
= TREE_OPERAND (arg
, 0);
3014 tree incremented
, modify
, value
;
3015 pedantic_lvalue_warning (CONVERT_EXPR
);
3016 arg
= stabilize_reference (arg
);
3017 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
3020 value
= save_expr (arg
);
3021 incremented
= build (((code
== PREINCREMENT_EXPR
3022 || code
== POSTINCREMENT_EXPR
)
3023 ? PLUS_EXPR
: MINUS_EXPR
),
3024 argtype
, value
, inc
);
3025 TREE_SIDE_EFFECTS (incremented
) = 1;
3026 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
3027 value
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
3028 TREE_USED (value
) = 1;
3038 /* Complain about anything else that is not a true lvalue. */
3039 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
3040 || code
== POSTINCREMENT_EXPR
)
3041 ? "increment" : "decrement")))
3042 return error_mark_node
;
3044 /* Report a read-only lvalue. */
3045 if (TREE_READONLY (arg
))
3046 readonly_warning (arg
,
3047 ((code
== PREINCREMENT_EXPR
3048 || code
== POSTINCREMENT_EXPR
)
3049 ? "increment" : "decrement"));
3051 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
3052 TREE_SIDE_EFFECTS (val
) = 1;
3053 val
= convert (result_type
, val
);
3054 if (TREE_CODE (val
) != code
)
3055 TREE_NO_UNUSED_WARNING (val
) = 1;
3060 /* Note that this operation never does default_conversion
3061 regardless of NOCONVERT. */
3063 /* Let &* cancel out to simplify resulting code. */
3064 if (TREE_CODE (arg
) == INDIRECT_REF
)
3066 /* Don't let this be an lvalue. */
3067 if (lvalue_p (TREE_OPERAND (arg
, 0)))
3068 return non_lvalue (TREE_OPERAND (arg
, 0));
3069 return TREE_OPERAND (arg
, 0);
3072 /* For &x[y], return x+y */
3073 if (TREE_CODE (arg
) == ARRAY_REF
)
3075 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
3076 return error_mark_node
;
3077 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
3078 TREE_OPERAND (arg
, 1), 1);
3081 /* Handle complex lvalues (when permitted)
3082 by reduction to simpler cases. */
3083 val
= unary_complex_lvalue (code
, arg
);
3087 #if 0 /* Turned off because inconsistent;
3088 float f; *&(int)f = 3.4 stores in int format
3089 whereas (int)f = 3.4 stores in float format. */
3090 /* Address of a cast is just a cast of the address
3091 of the operand of the cast. */
3092 switch (TREE_CODE (arg
))
3097 case FIX_TRUNC_EXPR
:
3098 case FIX_FLOOR_EXPR
:
3099 case FIX_ROUND_EXPR
:
3102 pedwarn ("ANSI C forbids the address of a cast expression");
3103 return convert (build_pointer_type (TREE_TYPE (arg
)),
3104 build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0),
3109 /* Allow the address of a constructor if all the elements
3111 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_CONSTANT (arg
))
3113 /* Anything not already handled and not a true memory reference
3115 else if (typecode
!= FUNCTION_TYPE
&& !lvalue_or_else (arg
, "unary `&'"))
3116 return error_mark_node
;
3118 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3119 argtype
= TREE_TYPE (arg
);
3120 /* If the lvalue is const or volatile,
3121 merge that into the type that the address will point to. */
3122 if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'd'
3123 || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
3125 if (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
3126 argtype
= c_build_type_variant (argtype
,
3127 TREE_READONLY (arg
),
3128 TREE_THIS_VOLATILE (arg
));
3131 argtype
= build_pointer_type (argtype
);
3133 if (mark_addressable (arg
) == 0)
3134 return error_mark_node
;
3139 if (TREE_CODE (arg
) == COMPONENT_REF
)
3141 tree field
= TREE_OPERAND (arg
, 1);
3143 addr
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
3145 if (DECL_BIT_FIELD (field
))
3147 error ("attempt to take address of bit-field structure member `%s'",
3148 IDENTIFIER_POINTER (DECL_NAME (field
)));
3149 return error_mark_node
;
3152 addr
= convert (argtype
, addr
);
3154 if (! integer_zerop (DECL_FIELD_BITPOS (field
)))
3157 = size_binop (EASY_DIV_EXPR
, DECL_FIELD_BITPOS (field
),
3158 size_int (BITS_PER_UNIT
));
3159 int flag
= TREE_CONSTANT (addr
);
3160 addr
= fold (build (PLUS_EXPR
, argtype
,
3161 addr
, convert (argtype
, offset
)));
3162 TREE_CONSTANT (addr
) = flag
;
3166 addr
= build1 (code
, argtype
, arg
);
3168 /* Address of a static or external variable or
3169 function counts as a constant. */
3171 TREE_CONSTANT (addr
) = 1;
3179 argtype
= TREE_TYPE (arg
);
3180 return fold (build1 (code
, argtype
, arg
));
3184 return error_mark_node
;
3188 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3189 convert ARG with the same conversions in the same order
3190 and return the result. */
3193 convert_sequence (conversions
, arg
)
3197 switch (TREE_CODE (conversions
))
3202 case FIX_TRUNC_EXPR
:
3203 case FIX_FLOOR_EXPR
:
3204 case FIX_ROUND_EXPR
:
3206 return convert (TREE_TYPE (conversions
),
3207 convert_sequence (TREE_OPERAND (conversions
, 0),
3216 /* Return nonzero if REF is an lvalue valid for this language.
3217 Lvalues can be assigned, unless their type has TYPE_READONLY.
3218 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3224 register enum tree_code code
= TREE_CODE (ref
);
3229 return lvalue_p (TREE_OPERAND (ref
, 0));
3240 if (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
3241 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
)
3248 /* Return nonzero if REF is an lvalue valid for this language;
3249 otherwise, print an error message and return zero. */
3252 lvalue_or_else (ref
, string
)
3256 int win
= lvalue_p (ref
);
3258 error ("invalid lvalue in %s", string
);
3262 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3263 for certain kinds of expressions which are not really lvalues
3264 but which we can accept as lvalues.
3266 If ARG is not a kind of expression we can handle, return zero. */
3269 unary_complex_lvalue (code
, arg
)
3270 enum tree_code code
;
3273 /* Handle (a, b) used as an "lvalue". */
3274 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
3276 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
3277 pedantic_lvalue_warning (COMPOUND_EXPR
);
3278 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
3279 TREE_OPERAND (arg
, 0), real_result
);
3282 /* Handle (a ? b : c) used as an "lvalue". */
3283 if (TREE_CODE (arg
) == COND_EXPR
)
3285 pedantic_lvalue_warning (COND_EXPR
);
3286 return (build_conditional_expr
3287 (TREE_OPERAND (arg
, 0),
3288 build_unary_op (code
, TREE_OPERAND (arg
, 1), 0),
3289 build_unary_op (code
, TREE_OPERAND (arg
, 2), 0)));
3295 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3296 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3299 pedantic_lvalue_warning (code
)
3300 enum tree_code code
;
3303 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3304 code
== COND_EXPR
? "conditional"
3305 : code
== COMPOUND_EXPR
? "compound" : "cast");
3308 /* Warn about storing in something that is `const'. */
3311 readonly_warning (arg
, string
)
3316 strcpy (buf
, string
);
3318 if (TREE_CODE (arg
) == COMPONENT_REF
)
3320 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
3321 readonly_warning (TREE_OPERAND (arg
, 0), string
);
3324 strcat (buf
, " of read-only member `%s'");
3325 pedwarn (buf
, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg
, 1))));
3328 else if (TREE_CODE (arg
) == VAR_DECL
)
3330 strcat (buf
, " of read-only variable `%s'");
3331 pedwarn (buf
, IDENTIFIER_POINTER (DECL_NAME (arg
)));
3335 pedwarn ("%s of read-only location", buf
);
3339 /* Mark EXP saying that we need to be able to take the
3340 address of it; it should not be allocated in a register.
3341 Value is 1 if successful. */
3344 mark_addressable (exp
)
3347 register tree x
= exp
;
3349 switch (TREE_CODE (x
))
3354 x
= TREE_OPERAND (x
, 0);
3358 TREE_ADDRESSABLE (x
) = 1;
3365 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
3366 && DECL_NONLOCAL (x
))
3368 if (TREE_PUBLIC (x
))
3370 error ("global register variable `%s' used in nested function",
3371 IDENTIFIER_POINTER (DECL_NAME (x
)));
3374 pedwarn ("register variable `%s' used in nested function",
3375 IDENTIFIER_POINTER (DECL_NAME (x
)));
3377 else if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
))
3379 if (TREE_PUBLIC (x
))
3381 error ("address of global register variable `%s' requested",
3382 IDENTIFIER_POINTER (DECL_NAME (x
)));
3385 pedwarn ("address of register variable `%s' requested",
3386 IDENTIFIER_POINTER (DECL_NAME (x
)));
3388 put_var_into_stack (x
);
3392 TREE_ADDRESSABLE (x
) = 1;
3393 #if 0 /* poplevel deals with this now. */
3394 if (DECL_CONTEXT (x
) == 0)
3395 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
3403 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3406 build_conditional_expr (ifexp
, op1
, op2
)
3407 tree ifexp
, op1
, op2
;
3409 register tree type1
;
3410 register tree type2
;
3411 register enum tree_code code1
;
3412 register enum tree_code code2
;
3413 register tree result_type
= NULL
;
3415 /* If second operand is omitted, it is the same as the first one;
3416 make sure it is calculated only once. */
3420 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3421 ifexp
= op1
= save_expr (ifexp
);
3424 ifexp
= truthvalue_conversion (default_conversion (ifexp
));
3426 if (TREE_CODE (ifexp
) == ERROR_MARK
3427 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
3428 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
3429 return error_mark_node
;
3431 #if 0 /* Produces wrong result if within sizeof. */
3432 /* Don't promote the operands separately if they promote
3433 the same way. Return the unpromoted type and let the combined
3434 value get promoted if necessary. */
3436 if (TREE_TYPE (op1
) == TREE_TYPE (op2
)
3437 && TREE_CODE (TREE_TYPE (op1
)) != ARRAY_TYPE
3438 && TREE_CODE (TREE_TYPE (op1
)) != ENUMERAL_TYPE
3439 && TREE_CODE (TREE_TYPE (op1
)) != FUNCTION_TYPE
)
3441 if (TREE_CODE (ifexp
) == INTEGER_CST
)
3442 return (integer_zerop (ifexp
) ? op2
: op1
);
3444 return fold (build (COND_EXPR
, TREE_TYPE (op1
), ifexp
, op1
, op2
));
3448 /* They don't match; promote them both and then try to reconcile them. */
3450 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
3451 op1
= default_conversion (op1
);
3452 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
3453 op2
= default_conversion (op2
);
3455 type1
= TREE_TYPE (op1
);
3456 code1
= TREE_CODE (type1
);
3457 type2
= TREE_TYPE (op2
);
3458 code2
= TREE_CODE (type2
);
3460 /* Quickly detect the usual case where op1 and op2 have the same type
3462 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
3465 result_type
= type1
;
3467 result_type
= TYPE_MAIN_VARIANT (type1
);
3469 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
)
3470 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
))
3472 result_type
= common_type (type1
, type2
);
3474 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
3476 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
3477 pedwarn ("ANSI C forbids conditional expr with only one void side");
3478 result_type
= void_type_node
;
3480 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
3482 if (comp_target_types (type1
, type2
))
3483 result_type
= common_type (type1
, type2
);
3484 else if (integer_zerop (op1
) && TREE_TYPE (type1
) == void_type_node
)
3485 result_type
= qualify_type (type2
, type1
);
3486 else if (integer_zerop (op2
) && TREE_TYPE (type2
) == void_type_node
)
3487 result_type
= qualify_type (type1
, type2
);
3488 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1
)) == void_type_node
)
3490 if (pedantic
&& TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
3491 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3492 result_type
= qualify_type (type1
, type2
);
3494 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2
)) == void_type_node
)
3496 if (pedantic
&& TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
3497 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3498 result_type
= qualify_type (type2
, type1
);
3502 pedwarn ("pointer type mismatch in conditional expression");
3503 result_type
= build_pointer_type (void_type_node
);
3506 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
3508 if (! integer_zerop (op2
))
3509 pedwarn ("pointer/integer type mismatch in conditional expression");
3512 op2
= null_pointer_node
;
3513 #if 0 /* The spec seems to say this is permitted. */
3514 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
3515 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3518 result_type
= type1
;
3520 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3522 if (!integer_zerop (op1
))
3523 pedwarn ("pointer/integer type mismatch in conditional expression");
3526 op1
= null_pointer_node
;
3527 #if 0 /* The spec seems to say this is permitted. */
3528 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
3529 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3532 result_type
= type2
;
3537 if (flag_cond_mismatch
)
3538 result_type
= void_type_node
;
3541 error ("type mismatch in conditional expression");
3542 return error_mark_node
;
3546 /* Merge const and volatile flags of the incoming types. */
3548 = build_type_variant (result_type
,
3549 TREE_READONLY (op1
) || TREE_READONLY (op2
),
3550 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
3552 if (result_type
!= TREE_TYPE (op1
))
3553 op1
= convert (result_type
, op1
);
3554 if (result_type
!= TREE_TYPE (op2
))
3555 op2
= convert (result_type
, op2
);
3558 if (code1
== RECORD_TYPE
|| code1
== UNION_TYPE
)
3560 result_type
= TREE_TYPE (op1
);
3561 if (TREE_CONSTANT (ifexp
))
3562 return (integer_zerop (ifexp
) ? op2
: op1
);
3564 if (TYPE_MODE (result_type
) == BLKmode
)
3566 register tree tempvar
3567 = build_decl (VAR_DECL
, NULL_TREE
, result_type
);
3568 register tree xop1
= build_modify_expr (tempvar
, op1
);
3569 register tree xop2
= build_modify_expr (tempvar
, op2
);
3570 register tree result
= fold (build (COND_EXPR
, result_type
,
3571 ifexp
, xop1
, xop2
));
3573 layout_decl (tempvar
, TYPE_ALIGN (result_type
));
3574 /* No way to handle variable-sized objects here.
3575 I fear that the entire handling of BLKmode conditional exprs
3576 needs to be redone. */
3577 if (TREE_CODE (DECL_SIZE (tempvar
)) != INTEGER_CST
)
3580 = assign_stack_local (DECL_MODE (tempvar
),
3581 (TREE_INT_CST_LOW (DECL_SIZE (tempvar
))
3582 + BITS_PER_UNIT
- 1)
3586 TREE_SIDE_EFFECTS (result
)
3587 = TREE_SIDE_EFFECTS (ifexp
) | TREE_SIDE_EFFECTS (op1
)
3588 | TREE_SIDE_EFFECTS (op2
);
3589 return build (COMPOUND_EXPR
, result_type
, result
, tempvar
);
3594 if (TREE_CODE (ifexp
) == INTEGER_CST
)
3595 return (integer_zerop (ifexp
) ? op2
: op1
);
3596 return fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
));
3599 /* Given a list of expressions, return a compound expression
3600 that performs them all and returns the value of the last of them. */
3603 build_compound_expr (list
)
3608 if (TREE_CHAIN (list
) == 0)
3610 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3611 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3613 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3614 if (TREE_CODE (list
) == NON_LVALUE_EXPR
)
3615 list
= TREE_OPERAND (list
, 0);
3618 return TREE_VALUE (list
);
3621 if (TREE_CHAIN (list
) != 0 && TREE_CHAIN (TREE_CHAIN (list
)) == 0)
3623 /* Convert arrays to pointers when there really is a comma operator. */
3624 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list
)))) == ARRAY_TYPE
)
3625 TREE_VALUE (TREE_CHAIN (list
))
3626 = default_conversion (TREE_VALUE (TREE_CHAIN (list
)));
3629 rest
= build_compound_expr (TREE_CHAIN (list
));
3631 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
3634 return build (COMPOUND_EXPR
, TREE_TYPE (rest
), TREE_VALUE (list
), rest
);
3637 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3640 build_c_cast (type
, expr
)
3644 register tree value
= expr
;
3646 if (type
== error_mark_node
|| expr
== error_mark_node
)
3647 return error_mark_node
;
3648 type
= TYPE_MAIN_VARIANT (type
);
3651 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3652 if (TREE_CODE (value
) == NON_LVALUE_EXPR
)
3653 value
= TREE_OPERAND (value
, 0);
3656 if (TREE_CODE (type
) == ARRAY_TYPE
)
3658 error ("cast specifies array type");
3659 return error_mark_node
;
3662 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3664 error ("cast specifies function type");
3665 return error_mark_node
;
3668 if (type
== TREE_TYPE (value
))
3672 if (TREE_CODE (type
) == RECORD_TYPE
3673 || TREE_CODE (type
) == UNION_TYPE
)
3674 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3677 else if (TREE_CODE (type
) == UNION_TYPE
)
3680 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
3681 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
3682 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
3691 pedwarn ("ANSI C forbids casts to union type");
3692 if (TYPE_NAME (type
) != 0)
3694 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
3695 name
= IDENTIFIER_POINTER (TYPE_NAME (type
));
3697 name
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
)));
3701 return digest_init (type
, build_nt (CONSTRUCTOR
, NULL_TREE
,
3702 build_tree_list (field
, value
)),
3703 NULL_PTR
, 0, 0, name
);
3705 error ("cast to union type from type not present in union");
3706 return error_mark_node
;
3711 /* Convert functions and arrays to pointers,
3712 but don't convert any other types. */
3713 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
3714 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
)
3715 value
= default_conversion (value
);
3716 otype
= TREE_TYPE (value
);
3718 /* Optionally warn about potentially worrisome casts. */
3721 && TREE_CODE (type
) == POINTER_TYPE
3722 && TREE_CODE (otype
) == POINTER_TYPE
)
3724 if (TYPE_VOLATILE (TREE_TYPE (otype
))
3725 && ! TYPE_VOLATILE (TREE_TYPE (type
)))
3726 pedwarn ("cast discards `volatile' from pointer target type");
3727 if (TYPE_READONLY (TREE_TYPE (otype
))
3728 && ! TYPE_READONLY (TREE_TYPE (type
)))
3729 pedwarn ("cast discards `const' from pointer target type");
3732 /* Warn about possible alignment problems. */
3733 if (STRICT_ALIGNMENT
&& warn_cast_align
3734 && TREE_CODE (type
) == POINTER_TYPE
3735 && TREE_CODE (otype
) == POINTER_TYPE
3736 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
3737 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
3738 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
3739 warning ("cast increases required alignment of target type");
3741 if (TREE_CODE (type
) == INTEGER_TYPE
3742 && TREE_CODE (otype
) == POINTER_TYPE
3743 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
3744 warning ("cast from pointer to integer of different size");
3746 if (TREE_CODE (type
) == POINTER_TYPE
3747 && TREE_CODE (otype
) == INTEGER_TYPE
3748 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3749 /* Don't warn about converting 0 to pointer,
3750 provided the 0 was explicit--not cast or made by folding. */
3751 && !(TREE_CODE (value
) == INTEGER_CST
&& integer_zerop (value
)))
3752 warning ("cast to pointer from integer of different size");
3754 value
= convert (type
, value
);
3757 if (value
== expr
&& pedantic
)
3759 /* If pedantic, don't let a cast be an lvalue. */
3760 return non_lvalue (value
);
3765 /* Build an assignment expression of lvalue LHS from value RHS.
3766 MODIFYCODE is the code for a binary operator that we use
3767 to combine the old value of LHS with RHS to get the new value.
3768 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3771 build_modify_expr (lhs
, modifycode
, rhs
)
3773 enum tree_code modifycode
;
3775 register tree result
;
3777 tree lhstype
= TREE_TYPE (lhs
);
3778 tree olhstype
= lhstype
;
3780 /* Types that aren't fully specified cannot be used in assignments. */
3781 lhs
= require_complete_type (lhs
);
3783 /* Avoid duplicate error messages from operands that had errors. */
3784 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
3785 return error_mark_node
;
3787 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3788 /* Do not use STRIP_NOPS here. We do not want an enumerator
3789 whose value is 0 to count as a null pointer constant. */
3790 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3791 rhs
= TREE_OPERAND (rhs
, 0);
3795 /* Handle control structure constructs used as "lvalues". */
3797 switch (TREE_CODE (lhs
))
3799 /* Handle (a, b) used as an "lvalue". */
3801 pedantic_lvalue_warning (COMPOUND_EXPR
);
3802 return build (COMPOUND_EXPR
, lhstype
,
3803 TREE_OPERAND (lhs
, 0),
3804 build_modify_expr (TREE_OPERAND (lhs
, 1),
3807 /* Handle (a ? b : c) used as an "lvalue". */
3809 pedantic_lvalue_warning (COND_EXPR
);
3810 rhs
= save_expr (rhs
);
3812 /* Produce (a ? (b = rhs) : (c = rhs))
3813 except that the RHS goes through a save-expr
3814 so the code to compute it is only emitted once. */
3816 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
3817 build_modify_expr (TREE_OPERAND (lhs
, 1),
3819 build_modify_expr (TREE_OPERAND (lhs
, 2),
3821 /* Make sure the code to compute the rhs comes out
3822 before the split. */
3823 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
3824 /* But cast it to void to avoid an "unused" error. */
3825 convert (void_type_node
, rhs
), cond
);
3829 /* If a binary op has been requested, combine the old LHS value with the RHS
3830 producing the value we should actually store into the LHS. */
3832 if (modifycode
!= NOP_EXPR
)
3834 lhs
= stabilize_reference (lhs
);
3835 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
3838 /* Handle a cast used as an "lvalue".
3839 We have already performed any binary operator using the value as cast.
3840 Now convert the result to the cast type of the lhs,
3841 and then true type of the lhs and store it there;
3842 then convert result back to the cast type to be the value
3843 of the assignment. */
3845 switch (TREE_CODE (lhs
))
3850 case FIX_TRUNC_EXPR
:
3851 case FIX_FLOOR_EXPR
:
3852 case FIX_ROUND_EXPR
:
3854 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
3855 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
)
3856 newrhs
= default_conversion (newrhs
);
3858 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
3860 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
3861 convert (TREE_TYPE (inner_lhs
),
3862 convert (lhstype
, newrhs
)));
3863 pedantic_lvalue_warning (CONVERT_EXPR
);
3864 return convert (TREE_TYPE (lhs
), result
);
3868 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3869 Reject anything strange now. */
3871 if (!lvalue_or_else (lhs
, "assignment"))
3872 return error_mark_node
;
3874 /* Warn about storing in something that is `const'. */
3876 if (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
3877 || ((TREE_CODE (lhstype
) == RECORD_TYPE
3878 || TREE_CODE (lhstype
) == UNION_TYPE
)
3879 && C_TYPE_FIELDS_READONLY (lhstype
)))
3880 readonly_warning (lhs
, "assignment");
3882 /* If storing into a structure or union member,
3883 it has probably been given type `int'.
3884 Compute the type that would go with
3885 the actual amount of storage the member occupies. */
3887 if (TREE_CODE (lhs
) == COMPONENT_REF
3888 && (TREE_CODE (lhstype
) == INTEGER_TYPE
3889 || TREE_CODE (lhstype
) == REAL_TYPE
3890 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
3891 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
3893 /* If storing in a field that is in actuality a short or narrower than one,
3894 we must store in the field in its actual type. */
3896 if (lhstype
!= TREE_TYPE (lhs
))
3898 lhs
= copy_node (lhs
);
3899 TREE_TYPE (lhs
) = lhstype
;
3902 /* Convert new value to destination type. */
3904 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
3906 if (TREE_CODE (newrhs
) == ERROR_MARK
)
3907 return error_mark_node
;
3909 result
= build (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
3910 TREE_SIDE_EFFECTS (result
) = 1;
3912 /* If we got the LHS in a different type for storing in,
3913 convert the result back to the nominal type of LHS
3914 so that the value we return always has the same type
3915 as the LHS argument. */
3917 if (olhstype
== TREE_TYPE (result
))
3919 return convert_for_assignment (olhstype
, result
, "assignment", NULL_TREE
, 0);
3922 /* Convert value RHS to type TYPE as preparation for an assignment
3923 to an lvalue of type TYPE.
3924 The real work of conversion is done by `convert'.
3925 The purpose of this function is to generate error messages
3926 for assignments that are not allowed in C.
3927 ERRTYPE is a string to use in error messages:
3928 "assignment", "return", etc. If it is null, this is parameter passing
3929 for a function call (and different error messages are output). Otherwise,
3930 it may be a name stored in the spelling stack and interpreted by
3933 FUNNAME is the name of the function being called,
3934 as an IDENTIFIER_NODE, or null.
3935 PARMNUM is the number of the argument, for printing in error messages. */
3938 convert_for_assignment (type
, rhs
, errtype
, funname
, parmnum
)
3944 register enum tree_code codel
= TREE_CODE (type
);
3945 register tree rhstype
;
3946 register enum tree_code coder
;
3948 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3949 /* Do not use STRIP_NOPS here. We do not want an enumerator
3950 whose value is 0 to count as a null pointer constant. */
3951 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3952 rhs
= TREE_OPERAND (rhs
, 0);
3954 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
3955 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
)
3956 rhs
= default_conversion (rhs
);
3958 rhstype
= TREE_TYPE (rhs
);
3959 coder
= TREE_CODE (rhstype
);
3961 if (coder
== ERROR_MARK
)
3962 return error_mark_node
;
3964 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
3967 if (coder
== VOID_TYPE
)
3969 error ("void value not ignored as it ought to be");
3970 return error_mark_node
;
3972 /* Arithmetic types all interconvert, and enum is treated like int. */
3973 if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
|| codel
== ENUMERAL_TYPE
)
3975 (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
|| coder
== ENUMERAL_TYPE
))
3977 return convert (type
, rhs
);
3979 /* Conversions among pointers */
3980 else if (codel
== POINTER_TYPE
&& coder
== POINTER_TYPE
)
3982 register tree ttl
= TREE_TYPE (type
);
3983 register tree ttr
= TREE_TYPE (rhstype
);
3985 /* Any non-function converts to a [const][volatile] void *
3986 and vice versa; otherwise, targets must be the same.
3987 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3988 if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
3989 || TYPE_MAIN_VARIANT (ttr
) == void_type_node
3990 || comp_target_types (type
, rhstype
)
3991 || (!pedantic
/* Unless pedantic, mix signed and unsigned. */
3992 && TREE_CODE (ttl
) == INTEGER_TYPE
3993 && TREE_CODE (ttr
) == INTEGER_TYPE
3994 && TYPE_PRECISION (ttl
) == TYPE_PRECISION (ttr
)))
3997 && ((TYPE_MAIN_VARIANT (ttl
) == void_type_node
3998 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
4000 (TYPE_MAIN_VARIANT (ttr
) == void_type_node
4001 && !integer_zerop (rhs
)
4002 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
4003 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4004 get_spelling (errtype
), funname
, parmnum
);
4005 /* Const and volatile mean something different for function types,
4006 so the usual warnings are not appropriate. */
4007 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
4008 || TREE_CODE (ttl
) != FUNCTION_TYPE
)
4010 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
4011 warn_for_assignment ("%s discards `const' from pointer target type",
4012 get_spelling (errtype
), funname
, parmnum
);
4013 if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
4014 warn_for_assignment ("%s discards `volatile' from pointer target type",
4015 get_spelling (errtype
), funname
, parmnum
);
4019 /* Because const and volatile on functions are restrictions
4020 that say the function will not do certain things,
4021 it is okay to use a const or volatile function
4022 where an ordinary one is wanted, but not vice-versa. */
4023 if (TYPE_READONLY (ttl
) && ! TYPE_READONLY (ttr
))
4024 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4025 get_spelling (errtype
), funname
, parmnum
);
4026 if (TYPE_VOLATILE (ttl
) && ! TYPE_VOLATILE (ttr
))
4027 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4028 get_spelling (errtype
), funname
, parmnum
);
4031 else if (unsigned_type (TYPE_MAIN_VARIANT (ttl
))
4032 == unsigned_type (TYPE_MAIN_VARIANT (ttr
)))
4033 warn_for_assignment ("pointer targets in %s differ in signedness",
4034 get_spelling (errtype
), funname
, parmnum
);
4036 warn_for_assignment ("%s from incompatible pointer type",
4037 get_spelling (errtype
), funname
, parmnum
);
4038 return convert (type
, rhs
);
4040 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
4042 /* An explicit constant 0 can convert to a pointer,
4043 but not a 0 that results from casting or folding. */
4044 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
)))
4046 warn_for_assignment ("%s makes pointer from integer without a cast",
4047 get_spelling (errtype
), funname
, parmnum
);
4048 return convert (type
, rhs
);
4050 return null_pointer_node
;
4052 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
4054 warn_for_assignment ("%s makes integer from pointer without a cast",
4055 get_spelling (errtype
), funname
, parmnum
);
4056 return convert (type
, rhs
);
4062 error ("incompatible type for argument %d of `%s'",
4063 parmnum
, IDENTIFIER_POINTER (funname
));
4065 error ("incompatible type for argument %d of indirect function call",
4069 error ("incompatible types in %s", get_spelling (errtype
));
4071 return error_mark_node
;
4074 /* Print a warning using MSG.
4075 It gets OPNAME as its one parameter.
4076 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4077 FUNCTION and ARGNUM are handled specially if we are building an
4078 Objective-C selector. */
4081 warn_for_assignment (msg
, opname
, function
, argnum
)
4087 static char argstring
[] = "passing arg %d of `%s'";
4088 static char argnofun
[] = "passing arg %d";
4092 tree selector
= maybe_building_objc_message_expr ();
4094 if (selector
&& argnum
> 2)
4096 function
= selector
;
4101 /* Function name is known; supply it. */
4102 opname
= (char *) alloca (IDENTIFIER_LENGTH (function
)
4103 + sizeof (argstring
) + 25 /*%d*/ + 1);
4104 sprintf (opname
, argstring
, argnum
, IDENTIFIER_POINTER (function
));
4108 /* Function name unknown (call through ptr); just give arg number. */
4109 opname
= (char *) alloca (sizeof (argnofun
) + 25 /*%d*/ + 1);
4110 sprintf (opname
, argnofun
, argnum
);
4113 pedwarn (msg
, opname
);
4116 /* Return nonzero if VALUE is a valid constant-valued expression
4117 for use in initializing a static variable; one that can be an
4118 element of a "constant" initializer.
4120 Return null_pointer_node if the value is absolute;
4121 if it is relocatable, return the variable that determines the relocation.
4122 We assume that VALUE has been folded as much as possible;
4123 therefore, we do not need to check for such things as
4124 arithmetic-combinations of integers. */
4127 initializer_constant_valid_p (value
)
4130 switch (TREE_CODE (value
))
4133 return TREE_STATIC (value
) ? null_pointer_node
: 0;
4138 return null_pointer_node
;
4141 return TREE_OPERAND (value
, 0);
4143 case NON_LVALUE_EXPR
:
4144 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4148 /* Allow conversions between pointer types. */
4149 if (TREE_CODE (TREE_TYPE (value
)) == POINTER_TYPE
4150 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == POINTER_TYPE
)
4151 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4152 /* Allow conversions between real types. */
4153 if (TREE_CODE (TREE_TYPE (value
)) == REAL_TYPE
4154 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == REAL_TYPE
)
4155 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4156 /* Allow length-preserving conversions between integer types. */
4157 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
4158 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == INTEGER_TYPE
4159 && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value
)),
4160 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value
, 0)))))
4161 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4162 /* Allow conversions between integer types only if explicit value. */
4163 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
4164 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == INTEGER_TYPE
)
4166 tree inner
= initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4167 if (inner
== null_pointer_node
)
4168 return null_pointer_node
;
4171 /* Allow (int) &foo. */
4172 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
4173 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == POINTER_TYPE
4174 && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value
)),
4175 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value
, 0)))))
4176 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4177 /* Allow conversions to union types if the value inside is okay. */
4178 if (TREE_CODE (TREE_TYPE (value
)) == UNION_TYPE
)
4179 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4184 tree valid0
= initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4185 tree valid1
= initializer_constant_valid_p (TREE_OPERAND (value
, 1));
4186 /* If either term is absolute, use the other terms relocation. */
4187 if (valid0
== null_pointer_node
)
4189 if (valid1
== null_pointer_node
)
4196 tree valid0
= initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4197 tree valid1
= initializer_constant_valid_p (TREE_OPERAND (value
, 1));
4198 /* Win if second argument is absolute. */
4199 if (valid1
== null_pointer_node
)
4201 /* Win if both arguments have the same relocation.
4202 Then the value is absolute. */
4203 if (valid0
== valid1
)
4204 return null_pointer_node
;
4212 /* Perform appropriate conversions on the initial value of a variable,
4213 store it in the declaration DECL,
4214 and print any error messages that are appropriate.
4215 If the init is invalid, store an ERROR_MARK. */
4218 store_init_value (decl
, init
)
4221 register tree value
, type
;
4223 /* If variable's type was invalidly declared, just ignore it. */
4225 type
= TREE_TYPE (decl
);
4226 if (TREE_CODE (type
) == ERROR_MARK
)
4229 /* Digest the specified initializer into an expression. */
4231 value
= digest_init (type
, init
, NULL_PTR
, TREE_STATIC (decl
),
4232 TREE_STATIC (decl
) || pedantic
,
4233 IDENTIFIER_POINTER (DECL_NAME (decl
)));
4235 /* Store the expression if valid; else report error. */
4238 /* Note that this is the only place we can detect the error
4239 in a case such as struct foo bar = (struct foo) { x, y };
4240 where there is one initial value which is a constructor expression. */
4241 if (value
== error_mark_node
)
4243 else if (TREE_STATIC (decl
) && ! TREE_CONSTANT (value
))
4245 error ("initializer for static variable is not constant");
4246 value
= error_mark_node
;
4248 else if (TREE_STATIC (decl
)
4249 && initializer_constant_valid_p (value
) == 0)
4251 error ("initializer for static variable uses complicated arithmetic");
4252 value
= error_mark_node
;
4256 if (pedantic
&& TREE_CODE (value
) == CONSTRUCTOR
)
4258 if (! TREE_CONSTANT (value
))
4259 pedwarn ("aggregate initializer is not constant");
4260 else if (! TREE_STATIC (value
))
4261 pedwarn ("aggregate initializer uses complicated arithmetic");
4266 /* ANSI wants warnings about out-of-range constant initializers. */
4267 constant_expression_warning (value
);
4269 DECL_INITIAL (decl
) = value
;
4272 /* Methods for storing and printing names for error messages. */
4274 /* Implement a spelling stack that allows components of a name to be pushed
4275 and popped. Each element on the stack is this structure. */
4287 #define SPELLING_STRING 1
4288 #define SPELLING_MEMBER 2
4289 #define SPELLING_BOUNDS 3
4291 static struct spelling
*spelling
; /* Next stack element (unused). */
4292 static struct spelling
*spelling_base
; /* Spelling stack base. */
4293 static int spelling_size
; /* Size of the spelling stack. */
4295 /* Macros to save and restore the spelling stack around push_... functions.
4296 Alternative to SAVE_SPELLING_STACK. */
4298 #define SPELLING_DEPTH() (spelling - spelling_base)
4299 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4301 /* Save and restore the spelling stack around arbitrary C code. */
4303 #define SAVE_SPELLING_DEPTH(code) \
4305 int __depth = SPELLING_DEPTH (); \
4307 RESTORE_SPELLING_DEPTH (__depth); \
4310 /* Push an element on the spelling stack with type KIND and assign VALUE
4313 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4315 int depth = SPELLING_DEPTH (); \
4317 if (depth >= spelling_size) \
4319 spelling_size += 10; \
4320 if (spelling_base == 0) \
4322 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4325 = (struct spelling *) xrealloc (spelling_base, \
4326 spelling_size * sizeof (struct spelling)); \
4327 RESTORE_SPELLING_DEPTH (depth); \
4330 spelling->kind = (KIND); \
4331 spelling->MEMBER = (VALUE); \
4335 /* Push STRING on the stack. Printed literally. */
4338 push_string (string
)
4341 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
4344 /* Push a member name on the stack. Printed as '.' STRING. */
4347 push_member_name (string
)
4350 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
4353 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4356 push_array_bounds (bounds
)
4359 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
4362 /* Compute the maximum size in bytes of the printed spelling. */
4367 register int size
= 0;
4368 register struct spelling
*p
;
4370 for (p
= spelling_base
; p
< spelling
; p
++)
4372 if (p
->kind
== SPELLING_BOUNDS
)
4375 size
+= strlen (p
->u
.s
) + 1;
4381 /* Print the spelling to BUFFER and return it. */
4384 print_spelling (buffer
)
4385 register char *buffer
;
4387 register char *d
= buffer
;
4389 register struct spelling
*p
;
4391 for (p
= spelling_base
; p
< spelling
; p
++)
4392 if (p
->kind
== SPELLING_BOUNDS
)
4394 sprintf (d
, "[%d]", p
->u
.i
);
4399 if (p
->kind
== SPELLING_MEMBER
)
4401 for (s
= p
->u
.s
; *d
= *s
++; d
++)
4408 /* Provide a means to pass component names derived from the spelling stack. */
4410 char initialization_message
;
4412 /* Interpret the spelling of the given ERRTYPE message. */
4415 get_spelling (errtype
)
4418 static char *buffer
;
4419 static int size
= -1;
4421 if (errtype
== &initialization_message
)
4423 /* Avoid counting chars */
4424 static char message
[] = "initialization of `%s'";
4425 register int needed
= sizeof (message
) + spelling_length () + 1;
4429 buffer
= (char *) xmalloc (size
= needed
);
4431 buffer
= (char *) xrealloc (buffer
, size
= needed
);
4433 temp
= (char *) alloca (needed
);
4434 sprintf (buffer
, message
, print_spelling (temp
));
4441 /* Issue an error message for a bad initializer component.
4442 FORMAT describes the message. OFWHAT is the name for the component.
4443 LOCAL is a format string for formatting the insertion of the name
4446 If OFWHAT is null, the component name is stored on the spelling stack.
4447 If the component name is a null string, then LOCAL is omitted entirely. */
4450 error_init (format
, local
, ofwhat
)
4451 char *format
, *local
, *ofwhat
;
4456 ofwhat
= print_spelling (alloca (spelling_length () + 1));
4457 buffer
= (char *) alloca (strlen (local
) + strlen (ofwhat
) + 2);
4460 sprintf (buffer
, local
, ofwhat
);
4464 error (format
, buffer
);
4467 /* Issue a pedantic warning for a bad initializer component.
4468 FORMAT describes the message. OFWHAT is the name for the component.
4469 LOCAL is a format string for formatting the insertion of the name
4472 If OFWHAT is null, the component name is stored on the spelling stack.
4473 If the component name is a null string, then LOCAL is omitted entirely. */
4476 pedwarn_init (format
, local
, ofwhat
)
4477 char *format
, *local
, *ofwhat
;
4482 ofwhat
= print_spelling (alloca (spelling_length () + 1));
4483 buffer
= (char *) alloca (strlen (local
) + strlen (ofwhat
) + 2);
4486 sprintf (buffer
, local
, ofwhat
);
4490 pedwarn (format
, buffer
);
4493 /* Digest the parser output INIT as an initializer for type TYPE.
4494 Return a C expression of type TYPE to represent the initial value.
4496 If TAIL is nonzero, it points to a variable holding a list of elements
4497 of which INIT is the first. We update the list stored there by
4498 removing from the head all the elements that we use.
4499 Normally this is only one; we use more than one element only if
4500 TYPE is an aggregate and INIT is not a constructor.
4502 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4503 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4504 applies only to elements of constructors.
4506 If OFWHAT is nonnull, it specifies what we are initializing, for error
4507 messages. Examples: variable name, variable.member, array[44].
4508 If OFWHAT is null, the component name is stored on the spelling stack. */
4511 digest_init (type
, init
, tail
, require_constant
, constructor_constant
, ofwhat
)
4512 tree type
, init
, *tail
;
4513 int require_constant
, constructor_constant
;
4516 enum tree_code code
= TREE_CODE (type
);
4518 tree old_tail_contents
;
4519 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
4520 tree node which has no TREE_TYPE. */
4522 = TREE_CODE (init
) == CONSTRUCTOR
&& TREE_TYPE (init
) == 0;
4523 tree inside_init
= init
;
4525 /* By default, assume we use one element from a list.
4526 We correct this later in the sole case where it is not true. */
4530 old_tail_contents
= *tail
;
4531 *tail
= TREE_CHAIN (*tail
);
4534 if (init
== error_mark_node
)
4537 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4538 /* Do not use STRIP_NOPS here. We do not want an enumerator
4539 whose value is 0 to count as a null pointer constant. */
4540 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
4541 inside_init
= TREE_OPERAND (init
, 0);
4543 if (inside_init
&& raw_constructor
4544 && CONSTRUCTOR_ELTS (inside_init
) != 0
4545 && TREE_CHAIN (CONSTRUCTOR_ELTS (inside_init
)) == 0)
4547 element
= TREE_VALUE (CONSTRUCTOR_ELTS (inside_init
));
4548 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4549 if (element
&& TREE_CODE (element
) == NON_LVALUE_EXPR
)
4550 element
= TREE_OPERAND (element
, 0);
4553 /* Initialization of an array of chars from a string constant
4554 optionally enclosed in braces. */
4556 if (code
== ARRAY_TYPE
)
4558 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
4559 if ((typ1
== char_type_node
4560 || typ1
== signed_char_type_node
4561 || typ1
== unsigned_char_type_node
4562 || typ1
== unsigned_wchar_type_node
4563 || typ1
== signed_wchar_type_node
)
4564 && ((inside_init
&& TREE_CODE (inside_init
) == STRING_CST
)
4565 || (element
&& TREE_CODE (element
) == STRING_CST
)))
4567 tree string
= element
? element
: inside_init
;
4569 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string
)))
4571 && TYPE_PRECISION (typ1
) == TYPE_PRECISION (char_type_node
))
4573 error_init ("char-array%s initialized from wide string",
4575 return error_mark_node
;
4577 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string
)))
4579 && TYPE_PRECISION (typ1
) != TYPE_PRECISION (char_type_node
))
4581 error_init ("int-array%s initialized from non-wide string",
4583 return error_mark_node
;
4586 TREE_TYPE (string
) = type
;
4587 if (TYPE_DOMAIN (type
) != 0
4588 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
4590 register int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
4591 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
4592 /* Subtract 1 because it's ok to ignore the terminating null char
4593 that is counted in the length of the constant. */
4594 if (size
< TREE_STRING_LENGTH (string
) - 1)
4596 "initializer-string for array of chars%s is too long",
4603 /* Any type except an array can be initialized
4604 from an expression of the same type, optionally with braces.
4605 For an array, this is allowed only for a string constant. */
4607 if (inside_init
&& TREE_TYPE (inside_init
) != 0
4608 && ((TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
))
4609 == TYPE_MAIN_VARIANT (type
))
4610 || (code
== ARRAY_TYPE
4611 && comptypes (TREE_TYPE (inside_init
), type
))
4612 || (code
== POINTER_TYPE
4613 && (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
4614 || TREE_CODE (TREE_TYPE (inside_init
)) == FUNCTION_TYPE
)
4615 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
4616 TREE_TYPE (type
)))))
4618 if (code
== POINTER_TYPE
4619 && (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
4620 || TREE_CODE (TREE_TYPE (inside_init
)) == FUNCTION_TYPE
))
4621 inside_init
= default_conversion (inside_init
);
4622 else if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
)
4624 error_init ("array%s initialized from non-constant array expression",
4626 return error_mark_node
;
4629 if (optimize
&& TREE_READONLY (inside_init
)
4630 && TREE_CODE (inside_init
) == VAR_DECL
)
4631 inside_init
= decl_constant_value (inside_init
);
4633 if (require_constant
&& ! TREE_CONSTANT (inside_init
))
4635 error_init ("initializer element%s is not constant",
4636 " for `%s'", ofwhat
);
4637 inside_init
= error_mark_node
;
4639 else if (require_constant
&& initializer_constant_valid_p (inside_init
) == 0)
4641 error_init ("initializer element%s is not computable at load time",
4642 " for `%s'", ofwhat
);
4643 inside_init
= error_mark_node
;
4649 if (element
&& (TREE_TYPE (element
) == type
4650 || (code
== ARRAY_TYPE
&& TREE_TYPE (element
)
4651 && comptypes (TREE_TYPE (element
), type
))))
4653 if (code
== ARRAY_TYPE
)
4655 error_init ("array%s initialized from non-constant array expression",
4657 return error_mark_node
;
4659 if (pedantic
&& (code
== RECORD_TYPE
|| code
== UNION_TYPE
))
4660 pedwarn ("single-expression nonscalar initializer has braces");
4661 if (optimize
&& TREE_READONLY (element
) && TREE_CODE (element
) == VAR_DECL
)
4662 element
= decl_constant_value (element
);
4664 if (require_constant
&& ! TREE_CONSTANT (element
))
4666 error_init ("initializer element%s is not constant",
4667 " for `%s'", ofwhat
);
4668 element
= error_mark_node
;
4670 else if (require_constant
&& initializer_constant_valid_p (element
) == 0)
4672 error_init ("initializer element%s is not computable at load time",
4673 " for `%s'", ofwhat
);
4674 element
= error_mark_node
;
4680 /* Check for initializing a union by its first field.
4681 Such an initializer must use braces. */
4683 if (code
== UNION_TYPE
)
4686 tree field
= TYPE_FIELDS (type
);
4688 /* Find the first named field. ANSI decided in September 1990
4689 that only named fields count here. */
4690 while (field
&& DECL_NAME (field
) == 0)
4691 field
= TREE_CHAIN (field
);
4695 error_init ("union%s with no named members cannot be initialized",
4697 return error_mark_node
;
4700 if (raw_constructor
)
4701 result
= process_init_constructor (type
, inside_init
, NULL_PTR
,
4703 constructor_constant
, ofwhat
);
4706 *tail
= old_tail_contents
;
4707 result
= process_init_constructor (type
, NULL_TREE
, tail
,
4709 constructor_constant
, ofwhat
);
4718 /* Handle scalar types, including conversions. */
4720 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== POINTER_TYPE
4721 || code
== ENUMERAL_TYPE
)
4723 if (raw_constructor
)
4728 "initializer for scalar%s requires one element",
4730 return error_mark_node
;
4734 /* Deal with extra levels of {...}. */
4735 if (TREE_CODE (element
) == CONSTRUCTOR
4736 && TREE_TYPE (element
) == 0)
4739 "initializer for scalar%s requires one element",
4741 return error_mark_node
;
4743 inside_init
= element
;
4747 #if 0 /* A non-raw constructor is an actual expression. */
4748 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
4750 error_init ("initializer for scalar%s has extra braces",
4752 return error_mark_node
;
4759 push_string (ofwhat
);
4761 = convert_for_assignment (type
,
4762 default_conversion (raw_constructor
4765 &initialization_message
, NULL_TREE
, 0);
4768 if (require_constant
&& ! TREE_CONSTANT (inside_init
))
4770 error_init ("initializer element%s is not constant",
4771 " for `%s'", ofwhat
);
4772 inside_init
= error_mark_node
;
4774 else if (require_constant
&& initializer_constant_valid_p (inside_init
) == 0)
4776 error_init ("initializer element%s is not computable at load time",
4777 " for `%s'", ofwhat
);
4778 inside_init
= error_mark_node
;
4784 /* Come here only for records and arrays. */
4786 if (TYPE_SIZE (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
4788 error_init ("variable-sized object%s may not be initialized",
4790 return error_mark_node
;
4793 if (code
== ARRAY_TYPE
|| code
== RECORD_TYPE
)
4795 if (raw_constructor
)
4796 return process_init_constructor (type
, inside_init
,
4797 NULL_PTR
, constructor_constant
,
4798 constructor_constant
, ofwhat
);
4801 *tail
= old_tail_contents
;
4802 return process_init_constructor (type
, NULL_TREE
, tail
,
4803 constructor_constant
,
4804 constructor_constant
, ofwhat
);
4806 else if (flag_traditional
)
4807 /* Traditionally one can say `char x[100] = 0;'. */
4808 return process_init_constructor (type
,
4809 build_nt (CONSTRUCTOR
, NULL_TREE
,
4810 tree_cons (NULL_TREE
,
4813 NULL_PTR
, constructor_constant
,
4814 constructor_constant
, ofwhat
);
4817 error_init ("invalid initializer%s", " for `%s'", ofwhat
);
4818 return error_mark_node
;
4821 /* Process a constructor for a variable of type TYPE.
4822 The constructor elements may be specified either with INIT or with ELTS,
4823 only one of which should be non-null.
4825 If INIT is specified, it is a CONSTRUCTOR node which is specifically
4826 and solely for initializing this datum.
4828 If ELTS is specified, it is the address of a variable containing
4829 a list of expressions. We take as many elements as we need
4830 from the head of the list and update the list.
4832 In the resulting constructor, TREE_CONSTANT is set if all elts are
4833 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
4834 constants that the assembler and linker can compute them.
4836 The argument CONSTANT_VALUE says to print an error if either the
4837 value or any element is not a constant.
4839 The argument CONSTANT_ELEMENT says to print an error if an element
4840 of an aggregate is not constant. It does not apply to a value
4841 which is not a constructor.
4843 OFWHAT is a character string describing the object being initialized,
4844 for error messages. It might be "variable" or "variable.member"
4845 or "variable[17].member[5]". If OFWHAT is null, the description string
4846 is stored on the spelling stack. */
4849 process_init_constructor (type
, init
, elts
, constant_value
, constant_element
,
4851 tree type
, init
, *elts
;
4852 int constant_value
, constant_element
;
4856 /* List of the elements of the result constructor,
4857 in reverse order. */
4858 register tree members
= NULL
;
4860 int allconstant
= 1;
4863 int depth
= SPELLING_DEPTH ();
4866 push_string (ofwhat
);
4868 /* Make TAIL be the list of elements to use for the initialization,
4869 no matter how the data was given to us. */
4874 warning ("aggregate has a partly bracketed initializer");
4878 tail
= CONSTRUCTOR_ELTS (init
);
4880 /* Gobble as many elements as needed, and make a constructor or initial value
4881 for each element of this aggregate. Chain them together in result.
4882 If there are too few, use 0 for each scalar ultimate component. */
4884 if (TREE_CODE (type
) == ARRAY_TYPE
)
4886 tree min_index
, max_index
, current_index
, members_index
;
4889 /* These are non-zero only within a range initializer. */
4890 tree start_index
= 0, end_index
= 0;
4891 /* Within a range, this is the value for the elts in the range. */
4894 /* If we have array bounds, set our bounds from that. Otherwise,
4895 we have a lower bound of zero and an unknown upper bound. Also
4896 set the type of the bounds; use "int" as default. */
4897 if (TYPE_DOMAIN (type
))
4899 min_index
= members_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
4900 max_index
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
4901 bound_type
= TREE_TYPE (min_index
);
4905 min_index
= members_index
= integer_zero_node
;
4907 bound_type
= integer_type_node
;
4910 one
= convert (bound_type
, integer_one_node
);
4912 /* Don't leave the loop based on index if the next item has an explicit
4913 index value that will override it. */
4915 for (current_index
= min_index
; tail
!= 0 || end_index
;
4916 current_index
= fold (build (PLUS_EXPR
, bound_type
,
4917 current_index
, one
)))
4919 register tree next1
= 0;
4921 /* Handle the case where we are inside of a range.
4922 current_index increments through the range,
4923 so just keep reusing the same element of TAIL
4924 until the end of the range. */
4928 if (!tree_int_cst_lt (current_index
, end_index
))
4932 /* If this element specifies an index,
4933 move to that index before storing it in the new list. */
4934 else if (TREE_PURPOSE (tail
) != 0)
4937 tree index
= TREE_PURPOSE (tail
);
4939 if (index
&& TREE_CODE (index
) == NON_LVALUE_EXPR
)
4940 index
= TREE_OPERAND (index
, 0);
4942 /* Begin a range. */
4943 if (TREE_CODE (index
) == TREE_LIST
)
4945 start_index
= TREE_PURPOSE (index
);
4946 end_index
= TREE_PURPOSE (TREE_CHAIN (index
));
4948 /* Expose constants. */
4949 if (end_index
&& TREE_CODE (end_index
) == NON_LVALUE_EXPR
)
4950 end_index
= TREE_OPERAND (end_index
, 0);
4951 if (start_index
&& TREE_CODE (start_index
) == NON_LVALUE_EXPR
)
4952 start_index
= TREE_OPERAND (start_index
, 0);
4954 if ((TREE_CODE (start_index
) == IDENTIFIER_NODE
)
4955 || (TREE_CODE (end_index
) == IDENTIFIER_NODE
))
4956 error ("field name used as index in array initializer");
4957 else if ((TREE_CODE (start_index
) != INTEGER_CST
)
4958 || (TREE_CODE (end_index
) != INTEGER_CST
))
4959 error ("non-constant array index in initializer");
4960 else if (tree_int_cst_lt (start_index
, min_index
)
4961 || (max_index
&& tree_int_cst_lt (max_index
, start_index
))
4962 || tree_int_cst_lt (end_index
, min_index
)
4963 || (max_index
&& tree_int_cst_lt (max_index
, end_index
)))
4964 error ("array index out of range in initializer");
4965 else if (tree_int_cst_lt (end_index
, start_index
))
4967 /* If the range is empty, don't initialize any elements,
4968 but do reset current_index for the next initializer
4970 warning ("empty array initializer range");
4971 tail
= TREE_CHAIN (tail
);
4972 current_index
= end_index
;
4977 current_index
= start_index
;
4979 /* See if the first element is also the last. */
4980 if (!tree_int_cst_lt (current_index
, end_index
))
4984 else if (TREE_CODE (index
) == IDENTIFIER_NODE
)
4985 error ("field name used as index in array initializer");
4986 else if (TREE_CODE (index
) != INTEGER_CST
)
4987 error ("non-constant array index in initializer");
4988 else if (tree_int_cst_lt (index
, min_index
)
4989 || (max_index
&& tree_int_cst_lt (max_index
, index
)))
4990 error ("array index out of range in initializer");
4992 current_index
= index
, win
= 1;
4995 TREE_VALUE (tail
) = error_mark_node
;
4998 if (max_index
&& tree_int_cst_lt (max_index
, current_index
))
4999 break; /* Stop if we've indeed run out of elements. */
5001 /* Now digest the value specified. */
5004 else if (TREE_VALUE (tail
) != 0)
5008 /* Build the element of this array, with "[]" notation. For
5009 error messages, we assume that the index fits within a
5013 push_array_bounds (TREE_INT_CST_LOW (current_index
));
5014 next1
= digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
5015 TREE_VALUE (tail
), &tail1
,
5016 /* Both of these are the same because
5017 a value here is an elt overall. */
5018 constant_element
, constant_element
,
5022 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
5024 if (tail
== tail1
&& TYPE_DOMAIN (type
) == 0)
5027 "non-empty initializer for array%s of empty elements",
5029 /* Just ignore what we were supposed to use. */
5036 next1
= error_mark_node
;
5037 tail
= TREE_CHAIN (tail
);
5043 if (next1
== error_mark_node
)
5045 else if (!TREE_CONSTANT (next1
))
5047 else if (initializer_constant_valid_p (next1
) == 0)
5050 /* Now store NEXT1 in the list, I elements from the *end*.
5051 Make the list longer if necessary. */
5052 while (! tree_int_cst_lt (current_index
, members_index
))
5054 members
= tree_cons (NULL_TREE
, NULL_TREE
, members
);
5055 members_index
= fold (build (PLUS_EXPR
, bound_type
,
5056 members_index
, one
));
5064 for (idx
= fold (build (MINUS_EXPR
, bound_type
,
5065 members_index
, one
));
5066 tree_int_cst_lt (current_index
, idx
);
5067 idx
= fold (build (MINUS_EXPR
, bound_type
, idx
, one
)))
5068 temp
= TREE_CHAIN (temp
);
5069 TREE_VALUE (temp
) = next1
;
5073 if (TREE_CODE (type
) == RECORD_TYPE
)
5075 register tree field
;
5076 int members_length
= 0;
5079 /* Don't leave the loop based on field just yet; see if next item
5080 overrides the expected field first. */
5082 for (field
= TYPE_FIELDS (type
), i
= 0; tail
;
5083 field
= TREE_CHAIN (field
), i
++)
5085 register tree next1
;
5087 /* If this element specifies a field,
5088 move to that field before storing it in the new list. */
5089 if (TREE_PURPOSE (tail
) != 0)
5093 if (TREE_CODE (TREE_PURPOSE (tail
)) != IDENTIFIER_NODE
)
5094 error ("index value instead of field name in structure initializer");
5099 for (temp
= TYPE_FIELDS (type
), j
= 0;
5101 temp
= TREE_CHAIN (temp
), j
++)
5102 if (DECL_NAME (temp
) == TREE_PURPOSE (tail
))
5105 field
= temp
, i
= j
, win
= 1;
5107 error ("no field `%s' in structure being initialized",
5108 IDENTIFIER_POINTER (TREE_PURPOSE (tail
)));
5111 TREE_VALUE (tail
) = error_mark_node
;
5115 break; /* No more fields to init. */
5117 if (! DECL_NAME (field
))
5119 next1
= integer_zero_node
;
5121 else if (TREE_VALUE (tail
) != 0)
5125 /* Build the name of this member, with a "." for membership. */
5128 push_member_name (IDENTIFIER_POINTER (DECL_NAME (field
)));
5129 next1
= digest_init (TREE_TYPE (field
),
5130 TREE_VALUE (tail
), &tail1
,
5131 constant_element
, constant_element
,
5134 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
5140 next1
= error_mark_node
;
5141 tail
= TREE_CHAIN (tail
);
5144 if (next1
== error_mark_node
)
5146 else if (!TREE_CONSTANT (next1
))
5148 else if (initializer_constant_valid_p (next1
) == 0)
5151 /* Now store NEXT1 in the list, I elements from the *end*.
5152 Make the list longer if necessary. */
5153 while (i
>= members_length
)
5155 members
= tree_cons (NULL_TREE
, NULL_TREE
, members
);
5163 for (j
= members_length
- 1; j
> i
; j
--)
5164 temp
= TREE_CHAIN (temp
);
5165 TREE_VALUE (temp
) = next1
;
5166 TREE_PURPOSE (temp
) = field
;
5170 if (TREE_CODE (type
) == UNION_TYPE
)
5172 register tree field
= TYPE_FIELDS (type
);
5173 register tree next1
;
5175 /* Find the first named field. ANSI decided in September 1990
5176 that only named fields count here. */
5177 while (field
&& DECL_NAME (field
) == 0)
5178 field
= TREE_CHAIN (field
);
5180 /* For a union, get the initializer for 1 fld. */
5182 /* If this element specifies a field, initialize via that field. */
5183 if (TREE_PURPOSE (tail
) != 0)
5187 if (TREE_CODE (TREE_PURPOSE (tail
)) == FIELD_DECL
)
5188 /* Handle the case of a call by build_c_cast. */
5189 field
= TREE_PURPOSE (tail
), win
= 1;
5190 else if (TREE_CODE (TREE_PURPOSE (tail
)) != IDENTIFIER_NODE
)
5191 error ("index value instead of field name in union initializer");
5195 for (temp
= TYPE_FIELDS (type
);
5197 temp
= TREE_CHAIN (temp
))
5198 if (DECL_NAME (temp
) == TREE_PURPOSE (tail
))
5201 field
= temp
, win
= 1;
5203 error ("no field `%s' in union being initialized",
5204 IDENTIFIER_POINTER (TREE_PURPOSE (tail
)));
5207 TREE_VALUE (tail
) = error_mark_node
;
5210 if (TREE_VALUE (tail
) != 0)
5214 /* Build the name of this member, with a "." for membership. */
5217 push_member_name (IDENTIFIER_POINTER (DECL_NAME (field
)));
5218 next1
= digest_init (TREE_TYPE (field
),
5219 TREE_VALUE (tail
), &tail1
,
5220 constant_value
, constant_element
, NULL_PTR
);
5222 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
5228 next1
= error_mark_node
;
5229 tail
= TREE_CHAIN (tail
);
5232 if (next1
== error_mark_node
)
5234 else if (!TREE_CONSTANT (next1
))
5236 else if (initializer_constant_valid_p (next1
) == 0)
5238 members
= tree_cons (field
, next1
, members
);
5241 /* If arguments were specified as a list, just remove the ones we used. */
5244 /* If arguments were specified as a constructor,
5245 complain unless we used all the elements of the constructor. */
5248 if (TREE_CODE (type
) == UNION_TYPE
)
5250 pedwarn_init ("excess elements in union initializer%s",
5251 " after `%s'", NULL_PTR
);
5255 pedwarn_init ("excess elements in aggregate initializer%s",
5256 " after `%s'", NULL_PTR
);
5260 /* It might be possible to use SAVE_SPELLING_DEPTH, but I suspect that
5261 some preprocessor somewhere won't accept that much text as an argument.
5262 It's also likely to make debugging difficult. */
5264 RESTORE_SPELLING_DEPTH (depth
);
5267 return error_mark_node
;
5269 result
= build (CONSTRUCTOR
, type
, NULL_TREE
, nreverse (members
));
5270 if (allconstant
) TREE_CONSTANT (result
) = 1;
5271 if (allconstant
&& allsimple
) TREE_STATIC (result
) = 1;
5275 /* Expand an ASM statement with operands, handling output operands
5276 that are not variables or INDIRECT_REFS by transforming such
5277 cases into cases that expand_asm_operands can handle.
5279 Arguments are same as for expand_asm_operands. */
5282 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
5283 tree string
, outputs
, inputs
, clobbers
;
5288 int noutputs
= list_length (outputs
);
5290 /* o[I] is the place that output number I should be written. */
5291 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
5294 if (TREE_CODE (string
) == ADDR_EXPR
)
5295 string
= TREE_OPERAND (string
, 0);
5296 if (TREE_CODE (string
) != STRING_CST
)
5298 error ("asm template is not a string constant");
5302 /* Record the contents of OUTPUTS before it is modified. */
5303 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
5304 o
[i
] = TREE_VALUE (tail
);
5306 /* Perform default conversions on array and function inputs. */
5307 /* Don't do this for other types--
5308 it would screw up operands expected to be in memory. */
5309 for (i
= 0, tail
= inputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
5310 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail
))) == ARRAY_TYPE
5311 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail
))) == FUNCTION_TYPE
)
5312 TREE_VALUE (tail
) = default_conversion (TREE_VALUE (tail
));
5314 /* Generate the ASM_OPERANDS insn;
5315 store into the TREE_VALUEs of OUTPUTS some trees for
5316 where the values were actually stored. */
5317 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
5319 /* Copy all the intermediate outputs into the specified outputs. */
5320 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
5322 if (o
[i
] != TREE_VALUE (tail
))
5324 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
5328 /* Detect modification of read-only values.
5329 (Otherwise done by build_modify_expr.) */
5332 tree type
= TREE_TYPE (o
[i
]);
5333 if (TYPE_READONLY (type
)
5334 || ((TREE_CODE (type
) == RECORD_TYPE
5335 || TREE_CODE (type
) == UNION_TYPE
)
5336 && C_TYPE_FIELDS_READONLY (type
)))
5337 readonly_warning (o
[i
], "modification by `asm'");
5341 /* Those MODIFY_EXPRs could do autoincrements. */
5345 /* Expand a C `return' statement.
5346 RETVAL is the expression for what to return,
5347 or a null pointer for `return;' with no value. */
5350 c_expand_return (retval
)
5353 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
5355 if (TREE_THIS_VOLATILE (current_function_decl
))
5356 warning ("function declared `volatile' has a `return' statement");
5360 current_function_returns_null
= 1;
5361 if (warn_return_type
&& valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
5362 warning ("`return' with no value, in function returning non-void");
5363 expand_null_return ();
5365 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
5367 current_function_returns_null
= 1;
5368 if (pedantic
|| TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
5369 pedwarn ("`return' with a value, in function returning void");
5370 expand_return (retval
);
5374 tree t
= convert_for_assignment (valtype
, retval
, "return",
5376 tree res
= DECL_RESULT (current_function_decl
);
5377 t
= build (MODIFY_EXPR
, TREE_TYPE (res
),
5378 res
, convert (TREE_TYPE (res
), t
));
5380 current_function_returns_value
= 1;
5384 /* Start a C switch statement, testing expression EXP.
5385 Return EXP if it is valid, an error node otherwise. */
5388 c_expand_start_case (exp
)
5391 register enum tree_code code
= TREE_CODE (TREE_TYPE (exp
));
5392 tree type
= TREE_TYPE (exp
);
5394 if (code
!= INTEGER_TYPE
&& code
!= ENUMERAL_TYPE
&& code
!= ERROR_MARK
)
5396 error ("switch quantity not an integer");
5397 exp
= error_mark_node
;
5402 type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
5404 if (warn_traditional
5405 && (type
== long_integer_type_node
5406 || type
== long_unsigned_type_node
))
5407 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
5409 exp
= default_conversion (exp
);
5410 type
= TREE_TYPE (exp
);
5411 index
= get_unwidened (exp
, NULL_TREE
);
5412 /* We can't strip a conversion from a signed type to an unsigned,
5413 because if we did, int_fits_type_p would do the wrong thing
5414 when checking case values for being in range,
5415 and it's too hard to do the right thing. */
5416 if (TREE_UNSIGNED (TREE_TYPE (exp
))
5417 == TREE_UNSIGNED (TREE_TYPE (index
)))
5421 expand_start_case (1, exp
, type
, "switch statement");