1 /*****************************************************************************/
5 /* Handle type conversions */
9 /* (C) 2002-2004 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 static void DoPtrConversions (ExprDesc* Expr)
58 /* If the expression is a function, convert it to pointer to function.
59 * If the expression is an array, convert it to pointer to first element.
62 if (IsTypeFunc (Expr->Type)) {
63 Expr->Type = PointerTo (Expr->Type);
64 } else if (IsTypeArray (Expr->Type)) {
65 Expr->Type = ArrayToPtr (Expr->Type);
71 static void DoConversion (ExprDesc* Expr, const type* NewType)
72 /* Emit code to convert the given expression to a new type. */
79 /* Remember the old type */
82 /* If we're converting to void, we're done. Note: This does also cover a
83 * conversion void -> void.
85 if (IsTypeVoid (NewType)) {
86 ED_MakeRVal (Expr); /* Never an lvalue */
90 /* Don't allow casts from void to something else. */
91 if (IsTypeVoid (OldType)) {
92 Error ("Cannot convert from `void' to something else");
96 /* Get the sizes of the types. Since we've excluded void types, checking
97 * for known sizes makes sense here.
99 OldSize = CheckedSizeOf (OldType);
100 NewSize = CheckedSizeOf (NewType);
103 if (ED_IsLVal (Expr)) {
105 /* We have an lvalue. If the new size is smaller than the new one,
106 * we don't need to do anything. The compiler will generate code
107 * to load only the portion of the value that is actually needed.
108 * This works only on a little endian architecture, but that's
110 * If both sizes are equal, do also leave the value alone.
111 * If the new size is larger, we must convert the value.
113 if (NewSize > OldSize) {
114 /* Load the value into the primary */
115 ExprLoad (CF_NONE, Expr);
117 /* Emit typecast code */
118 g_typecast (TypeOf (NewType), TypeOf (OldType));
120 /* Value is now in primary and an rvalue */
121 ED_MakeRValExpr (Expr);
124 } else if (ED_IsLocAbs (Expr)) {
126 /* A cast of a constant numeric value to another type. Be sure
127 * to handle sign extension correctly.
130 /* Get the current and new size of the value */
131 unsigned OldBits = OldSize * 8;
132 unsigned NewBits = NewSize * 8;
134 /* Check if the new datatype will have a smaller range. If it
135 * has a larger range, things are ok, since the value is
136 * internally already represented by a long.
138 if (NewBits <= OldBits) {
140 /* Cut the value to the new size */
141 Expr->IVal &= (0xFFFFFFFFUL >> (32 - NewBits));
143 /* If the new type is signed, sign extend the value */
144 if (!IsSignUnsigned (NewType)) {
145 if (Expr->IVal & (0x01UL << (NewBits-1))) {
146 /* Beware: Use the safe shift routine here. */
147 Expr->IVal |= shl_l (~0UL, NewBits);
154 /* The value is not a constant. If the sizes of the types are
155 * not equal, add conversion code. Be sure to convert chars
158 if (OldSize != NewSize) {
160 /* Load the value into the primary */
161 ExprLoad (CF_NONE, Expr);
163 /* Emit typecast code. */
164 g_typecast (TypeOf (NewType) | CF_FORCECHAR, TypeOf (OldType));
166 /* Value is now a rvalue in the primary */
167 ED_MakeRValExpr (Expr);
172 /* The expression has always the new type */
173 ReplaceType (Expr, NewType);
178 void TypeConversion (ExprDesc* Expr, type* NewType)
179 /* Do an automatic conversion of the given expression to the new type. Output
180 * warnings or errors where this automatic conversion is suspicious or
184 /* Get the type of the right hand side. Treat function types as
185 * pointer-to-function
187 DoPtrConversions (Expr);
189 /* First, do some type checking */
190 if (IsTypeVoid (NewType) || IsTypeVoid (Expr->Type)) {
191 /* If one of the sides are of type void, output a more apropriate
194 Error ("Illegal type");
197 /* Check for conversion problems */
198 if (IsClassInt (NewType)) {
200 /* Handle conversions to int type */
201 if (IsClassPtr (Expr->Type)) {
202 /* Pointer -> int conversion */
203 Warning ("Converting pointer to integer without a cast");
204 } else if (!IsClassInt (Expr->Type)) {
205 Error ("Incompatible types");
208 } else if (IsClassPtr (NewType)) {
210 /* Handle conversions to pointer type */
211 if (IsClassPtr (Expr->Type)) {
212 /* Pointer to pointer assignment is valid, if:
213 * - both point to the same types, or
214 * - the rhs pointer is a void pointer, or
215 * - the lhs pointer is a void pointer.
217 if (!IsTypeVoid (Indirect (NewType)) && !IsTypeVoid (Indirect (Expr->Type))) {
218 /* Compare the types */
219 switch (TypeCmp (NewType, Expr->Type)) {
221 case TC_INCOMPATIBLE:
222 Error ("Incompatible pointer types");
226 Error ("Pointer types differ in type qualifiers");
234 } else if (IsClassInt (Expr->Type)) {
235 /* Int to pointer assignment is valid only for constant zero */
236 if (!ED_IsConstAbsInt (Expr) || Expr->IVal != 0) {
237 Warning ("Converting integer to pointer without a cast");
239 } else if (IsTypeFuncPtr (NewType) && IsTypeFunc(Expr->Type)) {
240 /* Assignment of function to function pointer is allowed, provided
241 * that both functions have the same parameter list.
243 if (TypeCmp (Indirect (NewType), Expr->Type) < TC_EQUAL) {
244 Error ("Incompatible types");
247 Error ("Incompatible types");
252 /* Invalid automatic conversion */
253 Error ("Incompatible types");
257 /* Do the actual conversion */
258 DoConversion (Expr, NewType);
263 void TypeCast (ExprDesc* Expr)
264 /* Handle an explicit cast. The function returns true if the resulting
265 * expression is an lvalue and false if not.
268 type NewType[MAXTYPELEN];
270 /* Skip the left paren */
279 /* Read the expression we have to cast */
282 /* Convert functions and arrays to "pointer to" object */
283 DoPtrConversions (Expr);
285 /* Convert the value. */
286 DoConversion (Expr, NewType);