]> git.sur5r.net Git - cc65/blobdiff - src/cc65/datatype.c
Squashed one more bug in the switch statement
[cc65] / src / cc65 / datatype.c
index 59a0adddd3b49da88e1e3d7b196eb99d0746c0c1..4da5470b98b8e84c20ad2860e49a6d5b0aa3b01c 100644 (file)
@@ -51,7 +51,7 @@
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                                  Data                                    */
 /*****************************************************************************/
 
 
@@ -63,6 +63,7 @@ type type_uint []     = { T_UINT,     T_END };
 type type_long []      = { T_LONG,     T_END };
 type type_ulong []     = { T_ULONG,    T_END };
 type type_void []      = { T_VOID,     T_END };
+type type_size_t []    = { T_UINT,     T_END };
 
 
 
@@ -111,7 +112,7 @@ type* TypeDup (const type* T)
 /* Create a copy of the given type on the heap */
 {
     unsigned Len = (TypeLen (T) + 1) * sizeof (type);
-    return memcpy (xmalloc (Len), T, Len);
+    return (type*) memcpy (xmalloc (Len), T, Len);
 }
 
 
@@ -121,7 +122,7 @@ type* TypeAlloc (unsigned Len)
  * trailing T_END.
  */
 {
-    return xmalloc (Len * sizeof (type));
+    return (type*) xmalloc (Len * sizeof (type));
 }
 
 
@@ -172,7 +173,7 @@ type* GetImplicitFuncType (void)
     type* T = TypeAlloc (1 + DECODE_SIZE + 2);
 
     /* Prepare the function descriptor */
-    F->Flags  = FD_IMPLICIT | FD_EMPTY | FD_ELLIPSIS;
+    F->Flags  = FD_IMPLICIT | FD_EMPTY | FD_VARIADIC;
     F->SymTab = &EmptySymTab;
     F->TagTab = &EmptySymTab;
 
@@ -190,13 +191,34 @@ type* GetImplicitFuncType (void)
 
 
 
+type* PointerTo (const type* T)
+/* Return a type string that is "pointer to T". The type string is allocated
+ * on the heap and may be freed after use.
+ */
+{
+    /* Get the size of the type string including the terminator */
+    unsigned Size = TypeLen (T) + 1;
+
+    /* Allocate the new type string */
+    type* P = TypeAlloc        (Size + 1);
+
+    /* Create the return type... */
+    P[0] = T_PTR;
+    memcpy (P+1, T, Size * sizeof (type));
+
+    /* ...and return it */
+    return P;
+}
+
+
+
 static type PrintTypeComp (FILE* F, type T, type Mask, const char* Name)
 /* Check for a specific component of the type. If it is there, print the
  * name and remove it. Return the type with the component removed.
  */
 {
     if ((T & Mask) == Mask) {
-       fprintf (F, "%s ", Name);
+       fprintf (F, "%s ", Name);
        T &= ~Mask;
     }
     return T;
@@ -208,7 +230,7 @@ void PrintType (FILE* F, const type* Type)
 /* Output translation of type array. */
 {
     type T;
-
+    unsigned long Size;
 
     /* Walk over the complete string */
     while ((T = *Type++) != T_END) {
@@ -217,57 +239,67 @@ void PrintType (FILE* F, const type* Type)
        T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
        T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
 
-       /* Signedness */
-       T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
+       /* Signedness. Omit the signedness specifier for long and int */
+       if ((T & T_MASK_TYPE) != T_TYPE_INT && (T & T_MASK_TYPE) != T_TYPE_LONG) {
+           T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
+       }
        T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
 
        /* Now check the real type */
        switch (T & T_MASK_TYPE) {
            case T_TYPE_CHAR:
-               fprintf (F, "char\n");
+               fprintf (F, "char");
                break;
            case T_TYPE_SHORT:
-               fprintf (F, "short\n");
+               fprintf (F, "short");
                break;
            case T_TYPE_INT:
-               fprintf (F, "int\n");
+               fprintf (F, "int");
                break;
            case T_TYPE_LONG:
-               fprintf (F, "long\n");
+               fprintf (F, "long");
                break;
            case T_TYPE_LONGLONG:
-               fprintf (F, "long long\n");
+               fprintf (F, "long long");
                break;
            case T_TYPE_FLOAT:
-               fprintf (F, "float\n");
+               fprintf (F, "float");
                break;
            case T_TYPE_DOUBLE:
-               fprintf (F, "double\n");
+               fprintf (F, "double");
                break;
            case T_TYPE_VOID:
-               fprintf (F, "void\n");
+               fprintf (F, "void");
                break;
            case T_TYPE_STRUCT:
-               fprintf (F, "struct %s\n", ((SymEntry*) DecodePtr (Type))->Name);
+               fprintf (F, "struct %s", ((SymEntry*) DecodePtr (Type))->Name);
                        Type += DECODE_SIZE;
                break;
            case T_TYPE_UNION:
-               fprintf (F, "union %s\n", ((SymEntry*) DecodePtr (Type))->Name);
+               fprintf (F, "union %s", ((SymEntry*) DecodePtr (Type))->Name);
                Type += DECODE_SIZE;
                break;
            case T_TYPE_ARRAY:
-                       fprintf (F, "array[%lu] of ", Decode (Type));
-               Type += DECODE_SIZE;
-               break;
+               /* Recursive call */
+               PrintType (F, Type + DECODE_SIZE);
+               Size = Decode (Type);
+               if (Size == 0) {
+                   fprintf (F, "[]");
+               } else {
+                   fprintf (F, "[%lu]", Size);
+               }
+               return;
            case T_TYPE_PTR:
-               fprintf (F, "pointer to ");
-               break;
+               /* Recursive call */
+               PrintType (F, Type);
+               fprintf (F, "*");
+               return;
            case T_TYPE_FUNC:
                fprintf (F, "function returning ");
                Type += DECODE_SIZE;
                break;
            default:
-               fprintf (F, "unknown type: %04X\n", T);
+               fprintf (F, "unknown type: %04X", T);
        }
 
     }
@@ -275,6 +307,40 @@ void PrintType (FILE* F, const type* Type)
 
 
 
+void PrintFuncSig (FILE* F, const char* Name, type* Type)
+/* Print a function signature. */
+{
+    /* Get the function descriptor */
+    const FuncDesc* D = GetFuncDesc (Type);
+
+    /* Print a comment with the function signature */
+    PrintType (F, GetFuncReturn (Type));
+    if (D->Flags & FD_FASTCALL) {
+       fprintf (F, " __fastcall__");
+    }
+    fprintf (F, " %s (", Name);
+
+    /* Parameters */
+    if (D->Flags & FD_VOID_PARAM) {
+       fprintf (F, "void");
+    } else {
+       unsigned I;
+       SymEntry* E = D->SymTab->SymHead;
+       for (I = 0; I < D->ParamCount; ++I) {
+           if (I > 0) {
+               fprintf (F, ", ");
+           }
+           PrintType (F, E->Type);
+           E = E->NextSym;
+       }
+    }
+
+    /* End of parameter list */
+    fprintf (F, ")");
+}
+
+
+
 void PrintRawType (FILE* F, const type* Type)
 /* Print a type string in raw format (for debugging) */
 {
@@ -360,8 +426,8 @@ unsigned SizeOf (const type* T)
     switch (UnqualifiedType (T[0])) {
 
        case T_VOID:
-           Error (ERR_ILLEGAL_SIZE);
-           return 0;
+           Error ("Variable has unknown size");
+           return 1;   /* Return something that makes sense */
 
        case T_SCHAR:
        case T_UCHAR:
@@ -391,7 +457,7 @@ unsigned SizeOf (const type* T)
 
        case T_STRUCT:
        case T_UNION:
-                   Entry = DecodePtr (T+1);
+                   Entry = (SymEntry*) DecodePtr (T+1);
                    return Entry->V.S.Size;
 
        case T_ARRAY:
@@ -453,8 +519,8 @@ unsigned TypeOf (const type* T)
                    return CF_LONG | CF_UNSIGNED;
 
         case T_FUNC:
-           F = DecodePtr (T+1);
-           return (F->Flags & FD_ELLIPSIS)? 0 : CF_FIXARGC;
+           F = (FuncDesc*) DecodePtr (T+1);
+           return (F->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
 
         case T_STRUCT:
         case T_UNION:
@@ -462,7 +528,7 @@ unsigned TypeOf (const type* T)
                    return CF_INT | CF_UNSIGNED;
 
                default:
-                   Error (ERR_ILLEGAL_TYPE);
+                   Error ("Illegal type");
                    return CF_INT;
     }
 }
@@ -620,12 +686,23 @@ int IsFastCallFunc (const type* T)
 {
     FuncDesc* F;
     CHECK (IsTypeFunc (T));
-    F = DecodePtr (T+1);
+    F = (FuncDesc*) DecodePtr (T+1);
     return (F->Flags & FD_FASTCALL) != 0;
 }
 
 
 
+int IsVariadicFunc (const type* T)
+/* Return true if this is a function type with variable parameter list */
+{
+    FuncDesc* F;
+    CHECK (IsTypeFunc (T));
+    F = (FuncDesc*) DecodePtr (T+1);
+    return (F->Flags & FD_VARIADIC) != 0;
+}
+
+
+
 int IsTypeFuncPtr (const type* T)
 /* Return true if this is a function pointer */
 {
@@ -684,7 +761,7 @@ type GetQualifier (const type* T)
 
 
 
-struct FuncDesc* GetFuncDesc (const type* T)
+FuncDesc* GetFuncDesc (const type* T)
 /* Get the FuncDesc pointer from a function or pointer-to-function type */
 {
     if (T[0] == T_PTR) {
@@ -696,9 +773,26 @@ struct FuncDesc* GetFuncDesc (const type* T)
     CHECK (T[0] == T_FUNC);
 
     /* Decode the function descriptor and return it */
-    return DecodePtr (T+1);
+    return (FuncDesc*) DecodePtr (T+1);
 }
 
 
 
+type* GetFuncReturn (type* T)
+/* Return a pointer to the return type of a function or pointer-to-function type */
+{
+    if (T[0] == T_PTR) {
+       /* Pointer to function */
+       ++T;
+    }
+
+    /* Be sure it's a function type */
+    CHECK (T[0] == T_FUNC);
+
+    /* Return a pointer to the return type */
+    return T + 1 + DECODE_SIZE;
+
+}
+
+