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 /*****************************************************************************/
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 /* Get the type of the right hand side. Treat function types as
172 * pointer-to-function
174 Expr->Type = PtrConversion (Expr->Type);
176 /* First, do some type checking */
177 if (IsTypeVoid (NewType) || IsTypeVoid (Expr->Type)) {
178 /* If one of the sides are of type void, output a more apropriate
181 Error ("Illegal type");
184 /* If both types are equal, no conversion is needed */
185 if (TypeCmp (Expr->Type, NewType) >= TC_EQUAL) {
186 /* We're already done */
190 /* Check for conversion problems */
191 if (IsClassInt (NewType)) {
193 /* Handle conversions to int type */
194 if (IsClassPtr (Expr->Type)) {
195 /* Pointer -> int conversion */
196 Warning ("Converting pointer to integer without a cast");
197 } else if (!IsClassInt (Expr->Type) && !IsClassFloat (Expr->Type)) {
198 Error ("Incompatible types");
201 } else if (IsClassFloat (NewType)) {
203 if (!IsClassFloat (Expr->Type) && !IsClassInt (Expr->Type)) {
204 Error ("Incompatible types");
207 } else if (IsClassPtr (NewType)) {
209 /* Handle conversions to pointer type */
210 if (IsClassPtr (Expr->Type)) {
211 /* Pointer to pointer assignment is valid, if:
212 * - both point to the same types, or
213 * - the rhs pointer is a void pointer, or
214 * - the lhs pointer is a void pointer.
216 if (!IsTypeVoid (Indirect (NewType)) && !IsTypeVoid (Indirect (Expr->Type))) {
217 /* Compare the types */
218 switch (TypeCmp (NewType, Expr->Type)) {
220 case TC_INCOMPATIBLE:
221 Error ("Incompatible pointer types");
225 Error ("Pointer types differ in type qualifiers");
233 } else if (IsClassInt (Expr->Type)) {
234 /* Int to pointer assignment is valid only for constant zero */
235 if (!ED_IsConstAbsInt (Expr) || Expr->IVal != 0) {
236 Warning ("Converting integer to pointer without a cast");
238 } else if (IsTypeFuncPtr (NewType) && IsTypeFunc(Expr->Type)) {
239 /* Assignment of function to function pointer is allowed, provided
240 * that both functions have the same parameter list.
242 if (TypeCmp (Indirect (NewType), Expr->Type) < TC_EQUAL) {
243 Error ("Incompatible types");
246 Error ("Incompatible types");
251 /* Invalid automatic conversion */
252 Error ("Incompatible types");
256 /* Do the actual conversion */
257 DoConversion (Expr, NewType);
262 void TypeCast (ExprDesc* Expr)
263 /* Handle an explicit cast. The function returns true if the resulting
264 * expression is an lvalue and false if not.
267 type NewType[MAXTYPELEN];
269 /* Skip the left paren */
278 /* Read the expression we have to cast */
281 /* Convert functions and arrays to "pointer to" object */
282 Expr->Type = PtrConversion (Expr->Type);
284 /* Convert the value. */
285 DoConversion (Expr, NewType);