]> git.sur5r.net Git - cc65/blobdiff - src/cc65/datatype.c
Made C's sizeof operator work with initialized void variables.
[cc65] / src / cc65 / datatype.c
index 053810b50c0b1356825700dc8bf216abd6fa92d6..2d54316cd9cbba88b28f97773107e48c8e132cac 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2012, Ullrich von Bassewitz                                      */
+/* (C) 1998-2015, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
@@ -293,15 +293,15 @@ void PrintType (FILE* F, const Type* T)
                 /* Recursive call */
                 PrintType (F, T + 1);
                 if (T->A.L == UNSPECIFIED) {
-                    fprintf (F, "[]");
+                    fprintf (F, " []");
                 } else {
-                    fprintf (F, "[%ld]", T->A.L);
+                    fprintf (F, " [%ld]", T->A.L);
                 }
                 return;
             case T_TYPE_PTR:
                 /* Recursive call */
                 PrintType (F, T + 1);
-                fprintf (F, "*");
+                fprintf (F, " *");
                 return;
             case T_TYPE_FUNC:
                 fprintf (F, "function returning ");
@@ -389,7 +389,16 @@ unsigned SizeOf (const Type* T)
     switch (UnqualifiedType (T->C)) {
 
         case T_VOID:
-            return 0;   /* Assume voids have size zero */
+            /* A void variable is a cc65 extension.
+            ** Get its size (in bytes).
+            */
+            return T->A.U;
+
+        /* Beware: There's a chance that this triggers problems in other parts
+           of the compiler. The solution is to fix the callers, because calling
+           SizeOf() with a function type as argument is bad. */
+        case T_FUNC:
+            return 0;   /* Size of function is unknown */
 
         case T_SCHAR:
         case T_UCHAR:
@@ -404,7 +413,6 @@ unsigned SizeOf (const Type* T)
             return SIZEOF_INT;
 
         case T_PTR:
-        case T_FUNC:    /* Maybe pointer to function */
             return SIZEOF_PTR;
 
         case T_LONG:
@@ -433,7 +441,7 @@ unsigned SizeOf (const Type* T)
                 /* Array with unspecified size */
                 return 0;
             } else {
-                return T->A.L * SizeOf (T + 1);
+                return T->A.U * SizeOf (T + 1);
             }
 
         default:
@@ -659,7 +667,7 @@ Type* GetBaseElementType (Type* T)
 ** will return. Otherwise it will return the base element type, which means
 ** the element type that is not an array.
 */
-{     
+{
     while (IsTypeArray (T)) {
         ++T;
     }