]> git.sur5r.net Git - cc65/blobdiff - src/cc65/typeconv.c
Merge remote-tracking branch 'upstream/master' into a5200
[cc65] / src / cc65 / typeconv.c
index e9f72ee11347737e9141c4d5f485fa64027eb54d..9a6f3ab23f10bd4f9ebb5539459dcb92e14dc1c8 100644 (file)
@@ -50,7 +50,7 @@
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
@@ -128,7 +128,7 @@ static void DoConversion (ExprDesc* Expr, const Type* NewType)
             Expr->IVal &= (0xFFFFFFFFUL >> (32 - NewBits));
 
             /* If the new type is signed, sign extend the value */
-            if (!IsSignUnsigned (NewType)) {
+            if (IsSignSigned (NewType)) {
                 if (Expr->IVal & (0x01UL << (NewBits-1))) {
                     /* Beware: Use the safe shift routine here. */
                     Expr->IVal |= shl_l (~0UL, NewBits);
@@ -180,10 +180,15 @@ void TypeConversion (ExprDesc* Expr, Type* NewType)
 
     /* First, do some type checking */
     if (IsTypeVoid (NewType) || IsTypeVoid (Expr->Type)) {
-       /* If one of the sides are of type void, output a more apropriate
-        * error message.
-        */
-               Error ("Illegal type");
+        /* If one of the sides are of type void, output a more apropriate
+         * error message.
+         */
+        Error ("Illegal type");
+    }
+
+    /* If Expr is a function, convert it to pointer to function */
+    if (IsTypeFunc(Expr->Type)) {
+        Expr->Type = PointerTo (Expr->Type);
     }
 
     /* If both types are equal, no conversion is needed */
@@ -196,15 +201,15 @@ void TypeConversion (ExprDesc* Expr, Type* NewType)
     if (IsClassInt (NewType)) {
 
         /* Handle conversions to int type */
-               if (IsClassPtr (Expr->Type)) {
-           /* Pointer -> int conversion. Convert array to pointer */
+        if (IsClassPtr (Expr->Type)) {
+            /* Pointer -> int conversion. Convert array to pointer */
             if (IsTypeArray (Expr->Type)) {
                 Expr->Type = ArrayToPtr (Expr->Type);
             }
-           Warning ("Converting pointer to integer without a cast");
-               } else if (!IsClassInt (Expr->Type) && !IsClassFloat (Expr->Type)) {
-           Error ("Incompatible types");
-               }
+            Warning ("Converting pointer to integer without a cast");
+        } else if (!IsClassInt (Expr->Type) && !IsClassFloat (Expr->Type)) {
+            Error ("Incompatible types");
+        }
 
     } else if (IsClassFloat (NewType)) {
 
@@ -215,54 +220,44 @@ void TypeConversion (ExprDesc* Expr, Type* NewType)
     } else if (IsClassPtr (NewType)) {
 
         /* Handle conversions to pointer type */
-               if (IsClassPtr (Expr->Type)) {
+        if (IsClassPtr (Expr->Type)) {
 
             /* Convert array to pointer */
             if (IsTypeArray (Expr->Type)) {
                 Expr->Type = ArrayToPtr (Expr->Type);
             }
 
-           /* Pointer to pointer assignment is valid, if:
-            *   - both point to the same types, or
-            *   - the rhs pointer is a void pointer, or
-            *   - the lhs pointer is a void pointer.
-            */
-           if (!IsTypeVoid (Indirect (NewType)) && !IsTypeVoid (Indirect (Expr->Type))) {
-               /* Compare the types */
-               switch (TypeCmp (NewType, Expr->Type)) {
-
-                   case TC_INCOMPATIBLE:
-                       Error ("Incompatible pointer types");
-                       break;
-
-                   case TC_QUAL_DIFF:
-                       Error ("Pointer types differ in type qualifiers");
-                       break;
-
-                   default:
-                       /* Ok */
-                       break;
-               }
-           }
-
-       } else if (IsClassInt (Expr->Type)) {
-           /* Int to pointer assignment is valid only for constant zero */
-           if (!ED_IsConstAbsInt (Expr) || Expr->IVal != 0) {
-               Warning ("Converting integer to pointer without a cast");
-           }
-       } else if (IsTypeFuncPtr (NewType) && IsTypeFunc(Expr->Type)) {
-            /* Function -> Function pointer. First convert rhs to pointer */
-            Expr->Type = PointerTo (Expr->Type);
-
-           /* Assignment of function to function pointer is allowed, provided
-            * that both functions have the same parameter list.
-            */
-                   if (TypeCmp (NewType, Expr->Type) < TC_COMPATIBLE) {
-                Error ("Incompatible types");
-           }
-       } else {
-           Error ("Incompatible types");
-       }
+            /* Pointer to pointer assignment is valid, if:
+             *   - both point to the same types, or
+             *   - the rhs pointer is a void pointer, or
+             *   - the lhs pointer is a void pointer.
+             */
+            if (!IsTypeVoid (Indirect (NewType)) && !IsTypeVoid (Indirect (Expr->Type))) {
+                /* Compare the types */
+                switch (TypeCmp (NewType, Expr->Type)) {
+
+                    case TC_INCOMPATIBLE:
+                        Error ("Incompatible pointer types");
+                        break;
+
+                    case TC_QUAL_DIFF:
+                        Error ("Pointer types differ in type qualifiers");
+                        break;
+
+                    default:
+                        /* Ok */
+                        break;
+                }
+            }
+
+        } else if (IsClassInt (Expr->Type)) {
+            /* Int to pointer assignment is valid only for constant zero */
+            if (!ED_IsConstAbsInt (Expr) || Expr->IVal != 0) {
+                Warning ("Converting integer to pointer without a cast");
+            }
+        } else {
+            Error ("Incompatible types");
+        }
 
     } else {
 
@@ -300,8 +295,3 @@ void TypeCast (ExprDesc* Expr)
     /* Convert the value. */
     DoConversion (Expr, NewType);
 }
-
-
-
-
-