1 /*****************************************************************************/
5 /* Handle type conversions */
9 /* (C) 2002-2008 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
58 static void DoConversion (ExprDesc* Expr, const Type* NewType)
59 /* Emit code to convert the given expression to a new type. */
66 /* Remember the old type */
69 /* If we're converting to void, we're done. Note: This does also cover a
70 * conversion void -> void.
72 if (IsTypeVoid (NewType)) {
73 ED_MakeRVal (Expr); /* Never an lvalue */
77 /* Don't allow casts from void to something else. */
78 if (IsTypeVoid (OldType)) {
79 Error ("Cannot convert from `void' to something else");
83 /* Get the sizes of the types. Since we've excluded void types, checking
84 * for known sizes makes sense here.
86 OldSize = CheckedSizeOf (OldType);
87 NewSize = CheckedSizeOf (NewType);
90 if (ED_IsLVal (Expr)) {
92 /* We have an lvalue. If the new size is smaller than the new one,
93 * we don't need to do anything. The compiler will generate code
94 * to load only the portion of the value that is actually needed.
95 * This works only on a little endian architecture, but that's
97 * If both sizes are equal, do also leave the value alone.
98 * If the new size is larger, we must convert the value.
100 if (NewSize > OldSize) {
101 /* Load the value into the primary */
102 LoadExpr (CF_NONE, Expr);
104 /* Emit typecast code */
105 g_typecast (TypeOf (NewType), TypeOf (OldType));
107 /* Value is now in primary and an rvalue */
108 ED_MakeRValExpr (Expr);
111 } else if (ED_IsLocAbs (Expr)) {
113 /* A cast of a constant numeric value to another type. Be sure
114 * to handle sign extension correctly.
117 /* Get the current and new size of the value */
118 unsigned OldBits = OldSize * 8;
119 unsigned NewBits = NewSize * 8;
121 /* Check if the new datatype will have a smaller range. If it
122 * has a larger range, things are ok, since the value is
123 * internally already represented by a long.
125 if (NewBits <= OldBits) {
127 /* Cut the value to the new size */
128 Expr->IVal &= (0xFFFFFFFFUL >> (32 - NewBits));
130 /* If the new type is signed, sign extend the value */
131 if (!IsSignUnsigned (NewType)) {
132 if (Expr->IVal & (0x01UL << (NewBits-1))) {
133 /* Beware: Use the safe shift routine here. */
134 Expr->IVal |= shl_l (~0UL, NewBits);
141 /* The value is not a constant. If the sizes of the types are
142 * not equal, add conversion code. Be sure to convert chars
145 if (OldSize != NewSize) {
147 /* Load the value into the primary */
148 LoadExpr (CF_NONE, Expr);
150 /* Emit typecast code. */
151 g_typecast (TypeOf (NewType) | CF_FORCECHAR, TypeOf (OldType));
153 /* Value is now a rvalue in the primary */
154 ED_MakeRValExpr (Expr);
159 /* The expression has always the new type */
160 ReplaceType (Expr, NewType);
165 void TypeConversion (ExprDesc* Expr, Type* NewType)
166 /* Do an automatic conversion of the given expression to the new type. Output
167 * warnings or errors where this automatic conversion is suspicious or
171 /* First, do some type checking */
172 if (IsTypeVoid (NewType) || IsTypeVoid (Expr->Type)) {
173 /* If one of the sides are of type void, output a more apropriate
176 Error ("Illegal type");
179 /* If both types are equal, no conversion is needed */
180 if (TypeCmp (Expr->Type, NewType) >= TC_EQUAL) {
181 /* We're already done */
185 /* Check for conversion problems */
186 if (IsClassInt (NewType)) {
188 /* Handle conversions to int type */
189 if (IsClassPtr (Expr->Type)) {
190 /* Pointer -> int conversion. Convert array to pointer */
191 if (IsTypeArray (Expr->Type)) {
192 Expr->Type = ArrayToPtr (Expr->Type);
194 Warning ("Converting pointer to integer without a cast");
195 } else if (!IsClassInt (Expr->Type) && !IsClassFloat (Expr->Type)) {
196 Error ("Incompatible types");
199 } else if (IsClassFloat (NewType)) {
201 if (!IsClassFloat (Expr->Type) && !IsClassInt (Expr->Type)) {
202 Error ("Incompatible types");
205 } else if (IsClassPtr (NewType)) {
207 /* Handle conversions to pointer type */
208 if (IsClassPtr (Expr->Type)) {
210 /* Convert array to pointer */
211 if (IsTypeArray (Expr->Type)) {
212 Expr->Type = ArrayToPtr (Expr->Type);
215 /* Pointer to pointer assignment is valid, if:
216 * - both point to the same types, or
217 * - the rhs pointer is a void pointer, or
218 * - the lhs pointer is a void pointer.
220 if (!IsTypeVoid (Indirect (NewType)) && !IsTypeVoid (Indirect (Expr->Type))) {
221 /* Compare the types */
222 switch (TypeCmp (NewType, Expr->Type)) {
224 case TC_INCOMPATIBLE:
225 Error ("Incompatible pointer types");
229 Error ("Pointer types differ in type qualifiers");
238 } else if (IsClassInt (Expr->Type)) {
239 /* Int to pointer assignment is valid only for constant zero */
240 if (!ED_IsConstAbsInt (Expr) || Expr->IVal != 0) {
241 Warning ("Converting integer to pointer without a cast");
243 } else if (IsTypeFuncPtr (NewType) && IsTypeFunc(Expr->Type)) {
244 /* Function -> Function pointer. First convert rhs to pointer */
245 Expr->Type = PointerTo (Expr->Type);
247 /* Assignment of function to function pointer is allowed, provided
248 * that both functions have the same parameter list.
250 if (TypeCmp (NewType, Expr->Type) < TC_EQUAL) {
251 Error ("Incompatible types");
254 Error ("Incompatible types");
259 /* Invalid automatic conversion */
260 Error ("Incompatible types");
264 /* Do the actual conversion */
265 DoConversion (Expr, NewType);
270 void TypeCast (ExprDesc* Expr)
271 /* Handle an explicit cast. */
273 Type NewType[MAXTYPELEN];
275 /* Skip the left paren */
284 /* Read the expression we have to cast */
287 /* Convert functions and arrays to "pointer to" object */
288 Expr->Type = PtrConversion (Expr->Type);
290 /* Convert the value. */
291 DoConversion (Expr, NewType);