]> 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 df9c7d13e3b404074d6453a28f1d2d2112f8c6a2..2d54316cd9cbba88b28f97773107e48c8e132cac 100644 (file)
@@ -1,15 +1,15 @@
 /*****************************************************************************/
 /*                                                                           */
-/*                               datatype.c                                 */
+/*                                datatype.c                                 */
 /*                                                                           */
-/*              Type string handling for the cc65 C compiler                */
+/*               Type string handling for the cc65 C compiler                */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2000 Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
-/* EMail:        uz@musoftware.de                                            */
+/* (C) 1998-2015, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 #include <string.h>
 
 /* common */
+#include "addrsize.h"
 #include "check.h"
+#include "mmodel.h"
 #include "xmalloc.h"
 
 /* cc65 */
 #include "codegen.h"
 #include "datatype.h"
 #include "error.h"
+#include "fp.h"
 #include "funcdesc.h"
 #include "global.h"
-#include "util.h"
 #include "symtab.h"
 
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                                   Data                                    */
 /*****************************************************************************/
 
 
 
 /* Predefined type strings */
-type type_uchar []             = { T_UCHAR,    T_END };
-type type_int []       = { T_INT,      T_END };
-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 };
+Type type_schar[]       = { TYPE(T_SCHAR),  TYPE(T_END) };
+Type type_uchar[]       = { TYPE(T_UCHAR),  TYPE(T_END) };
+Type type_int[]         = { TYPE(T_INT),    TYPE(T_END) };
+Type type_uint[]        = { TYPE(T_UINT),   TYPE(T_END) };
+Type type_long[]        = { TYPE(T_LONG),   TYPE(T_END) };
+Type type_ulong[]       = { TYPE(T_ULONG),  TYPE(T_END) };
+Type type_void[]        = { TYPE(T_VOID),   TYPE(T_END) };
+Type type_size_t[]      = { TYPE(T_SIZE_T), TYPE(T_END) };
+Type type_float[]       = { TYPE(T_FLOAT),  TYPE(T_END) };
+Type type_double[]      = { TYPE(T_DOUBLE), TYPE(T_END) };
 
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
 
-unsigned TypeLen (const type* T)
+unsigned TypeLen (const Type* T)
 /* Return the length of the type string */
 {
-    const type* Start = T;
-    while (*T != T_END) {
-       ++T;
+    const Type* Start = T;
+    while (T->C != T_END) {
+        ++T;
     }
     return T - Start;
 }
 
 
 
-type* TypeCpy (type* Dest, const type* Src)
+Type* TypeCopy (Type* Dest, const Type* Src)
 /* Copy a type string */
 {
-    type T;
-    type* Orig = Dest;
-    do {
-       T = *Src++;
-       *Dest++ = T;
-    } while (T);
+    Type* Orig = Dest;
+    while (1) {
+        *Dest = *Src;
+        if (Src->C == T_END) {
+            break;
+        }
+        Src++;
+        Dest++;
+    }
     return Orig;
 }
 
 
 
-type* TypeCat (type* Dest, const type* Src)
-/* Append Src */
-{
-    TypeCpy (Dest + TypeLen (Dest), Src);
-    return Dest;
-}
-
-
-
-type* TypeDup (const type* T)
+Type* TypeDup (const Type* T)
 /* Create a copy of the given type on the heap */
 {
-    unsigned Len = (TypeLen (T) + 1) * sizeof (type);
+    unsigned Len = (TypeLen (T) + 1) * sizeof (Type);
     return memcpy (xmalloc (Len), T, Len);
 }
 
 
 
-type* TypeAlloc (unsigned Len)
+Type* TypeAlloc (unsigned Len)
 /* Allocate memory for a type string of length Len. Len *must* include the
- * trailing T_END.
- */
+** trailing T_END.
+*/
 {
-    return xmalloc (Len * sizeof (type));
+    return xmalloc (Len * sizeof (Type));
 }
 
 
 
-void TypeFree (type* T)
+void TypeFree (Type* T)
 /* Free a type string */
 {
     xfree (T);
@@ -135,27 +134,37 @@ void TypeFree (type* T)
 
 
 
-type GetDefaultChar (void)
+int SignExtendChar (int C)
+/* Do correct sign extension of a character */
+{
+    if (IS_Get (&SignedChars) && (C & 0x80) != 0) {
+        return C | ~0xFF;
+    } else {
+        return C & 0xFF;
+    }
+}
+
+
+
+TypeCode GetDefaultChar (void)
 /* Return the default char type (signed/unsigned) depending on the settings */
 {
-    return SignedChars? T_SCHAR : T_UCHAR;
+    return IS_Get (&SignedChars)? T_SCHAR : T_UCHAR;
 }
 
 
 
-type* GetCharArrayType (unsigned Len)
+Type* GetCharArrayType (unsigned Len)
 /* Return the type for a char array of the given length */
 {
     /* Allocate memory for the type string */
-    type* T = TypeAlloc (1 + DECODE_SIZE + 2);
+    Type* T = TypeAlloc (3);    /* array/char/terminator */
 
     /* Fill the type string */
-    T [0]            = T_ARRAY;
-    T [DECODE_SIZE+1] = GetDefaultChar();
-    T [DECODE_SIZE+2] = T_END;
-
-    /* Encode the length in the type string */
-    Encode (T+1, Len);
+    T[0].C   = T_ARRAY;
+    T[0].A.L = Len;             /* Array length is in the L attribute */
+    T[1].C   = GetDefaultChar ();
+    T[2].C   = T_END;
 
     /* Return the new type */
     return T;
@@ -163,27 +172,25 @@ type* GetCharArrayType (unsigned Len)
 
 
 
-type* GetImplicitFuncType (void)
+Type* GetImplicitFuncType (void)
 /* Return a type string for an inplicitly declared function */
 {
     /* Get a new function descriptor */
     FuncDesc* F = NewFuncDesc ();
 
     /* Allocate memory for the type string */
-    type* T = TypeAlloc (1 + DECODE_SIZE + 2);
+    Type* T = TypeAlloc (3);    /* func/returns int/terminator */
 
     /* Prepare the function descriptor */
-    F->Flags  = FD_IMPLICIT | FD_EMPTY | FD_ELLIPSIS;
+    F->Flags  = FD_EMPTY | FD_VARIADIC;
     F->SymTab = &EmptySymTab;
     F->TagTab = &EmptySymTab;
 
     /* Fill the type string */
-    T [0]            = T_FUNC;
-    T [DECODE_SIZE+1] = T_INT;
-    T [DECODE_SIZE+2] = T_END;
-
-    /* Encode the function descriptor into the type string */
-    EncodePtr (T+1, F);
+    T[0].C   = T_FUNC | CodeAddrSizeQualifier ();
+    T[0].A.P = F;
+    T[1].C   = T_INT;
+    T[2].C   = T_END;
 
     /* Return the new type */
     return T;
@@ -191,20 +198,20 @@ type* GetImplicitFuncType (void)
 
 
 
-type* PointerTo (const type* T)
+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.
- */
+** 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);
+    Type* P = TypeAlloc (Size + 1);
 
     /* Create the return type... */
-    P[0] = T_PTR;
-    memcpy (P+1, T, Size * sizeof (type));
+    P[0].C = T_PTR | (T[0].C & T_QUAL_ADDRSIZE);
+    memcpy (P+1, T, Size * sizeof (Type));
 
     /* ...and return it */
     return P;
@@ -212,515 +219,538 @@ type* PointerTo (const type* T)
 
 
 
-static type PrintTypeComp (FILE* F, type T, type Mask, const char* Name)
+static TypeCode PrintTypeComp (FILE* F, TypeCode C, TypeCode 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.
- */
+** name and remove it. Return the type with the component removed.
+*/
 {
-    if ((T & Mask) == Mask) {
-       fprintf (F, "%s ", Name);
-       T &= ~Mask;
+    if ((C & Mask) == Mask) {
+        fprintf (F, "%s ", Name);
+        C &= ~Mask;
     }
-    return T;
+    return C;
 }
 
 
 
-void PrintType (FILE* F, const type* Type)
+void PrintType (FILE* F, const Type* T)
 /* Output translation of type array. */
 {
-    type T;
-
-
-    /* Walk over the complete string */
-    while ((T = *Type++) != T_END) {
-
-       /* Print any qualifiers */
-       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");
-       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");
-               break;
-           case T_TYPE_SHORT:
-               fprintf (F, "short\n");
-               break;
-           case T_TYPE_INT:
-               fprintf (F, "int\n");
-               break;
-           case T_TYPE_LONG:
-               fprintf (F, "long\n");
-               break;
-           case T_TYPE_LONGLONG:
-               fprintf (F, "long long\n");
-               break;
-           case T_TYPE_FLOAT:
-               fprintf (F, "float\n");
-               break;
-           case T_TYPE_DOUBLE:
-               fprintf (F, "double\n");
-               break;
-           case T_TYPE_VOID:
-               fprintf (F, "void\n");
-               break;
-           case T_TYPE_STRUCT:
-               fprintf (F, "struct %s\n", ((SymEntry*) DecodePtr (Type))->Name);
-                       Type += DECODE_SIZE;
-               break;
-           case T_TYPE_UNION:
-               fprintf (F, "union %s\n", ((SymEntry*) DecodePtr (Type))->Name);
-               Type += DECODE_SIZE;
-               break;
-           case T_TYPE_ARRAY:
-                       fprintf (F, "array[%lu] of ", Decode (Type));
-               Type += DECODE_SIZE;
-               break;
-           case T_TYPE_PTR:
-               fprintf (F, "pointer to ");
-               break;
-           case T_TYPE_FUNC:
-               fprintf (F, "function returning ");
-               Type += DECODE_SIZE;
-               break;
-           default:
-               fprintf (F, "unknown type: %04X\n", T);
-       }
-
+    /* Walk over the type string */
+    while (T->C != T_END) {
+
+        /* Get the type code */
+        TypeCode C = T->C;
+
+        /* Print any qualifiers */
+        C = PrintTypeComp (F, C, T_QUAL_CONST, "const");
+        C = PrintTypeComp (F, C, T_QUAL_VOLATILE, "volatile");
+        C = PrintTypeComp (F, C, T_QUAL_RESTRICT, "restrict");
+        C = PrintTypeComp (F, C, T_QUAL_NEAR, "__near__");
+        C = PrintTypeComp (F, C, T_QUAL_FAR, "__far__");
+        C = PrintTypeComp (F, C, T_QUAL_FASTCALL, "__fastcall__");
+        C = PrintTypeComp (F, C, T_QUAL_CDECL, "__cdecl__");
+
+        /* Signedness. Omit the signedness specifier for long and int */
+        if ((C & T_MASK_TYPE) != T_TYPE_INT && (C & T_MASK_TYPE) != T_TYPE_LONG) {
+            C = PrintTypeComp (F, C, T_SIGN_SIGNED, "signed");
+        }
+        C = PrintTypeComp (F, C, T_SIGN_UNSIGNED, "unsigned");
+
+        /* Now check the real type */
+        switch (C & T_MASK_TYPE) {
+            case T_TYPE_CHAR:
+                fprintf (F, "char");
+                break;
+            case T_TYPE_SHORT:
+                fprintf (F, "short");
+                break;
+            case T_TYPE_INT:
+                fprintf (F, "int");
+                break;
+            case T_TYPE_LONG:
+                fprintf (F, "long");
+                break;
+            case T_TYPE_LONGLONG:
+                fprintf (F, "long long");
+                break;
+            case T_TYPE_FLOAT:
+                fprintf (F, "float");
+                break;
+            case T_TYPE_DOUBLE:
+                fprintf (F, "double");
+                break;
+            case T_TYPE_VOID:
+                fprintf (F, "void");
+                break;
+            case T_TYPE_STRUCT:
+                fprintf (F, "struct %s", ((SymEntry*) T->A.P)->Name);
+                break;
+            case T_TYPE_UNION:
+                fprintf (F, "union %s", ((SymEntry*) T->A.P)->Name);
+                break;
+            case T_TYPE_ARRAY:
+                /* Recursive call */
+                PrintType (F, T + 1);
+                if (T->A.L == UNSPECIFIED) {
+                    fprintf (F, " []");
+                } else {
+                    fprintf (F, " [%ld]", T->A.L);
+                }
+                return;
+            case T_TYPE_PTR:
+                /* Recursive call */
+                PrintType (F, T + 1);
+                fprintf (F, " *");
+                return;
+            case T_TYPE_FUNC:
+                fprintf (F, "function returning ");
+                break;
+            default:
+                fprintf (F, "unknown type: %04lX", T->C);
+        }
+
+        /* Next element */
+        ++T;
     }
 }
 
 
 
-void PrintRawType (FILE* F, const type* Type)
-/* Print a type string in raw format (for debugging) */
+void PrintFuncSig (FILE* F, const char* Name, Type* T)
+/* Print a function signature. */
 {
-    while (*Type != T_END) {
-               fprintf (F, "%04X ", *Type++);
-    }
-    fprintf (F, "\n");
-}
-
+    /* Get the function descriptor */
+    const FuncDesc* D = GetFuncDesc (T);
 
-
-void Encode (type* Type, unsigned long Val)
-/* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
-{
-    int I;
-    for (I = 0; I < DECODE_SIZE; ++I) {
-       *Type++ = ((type) Val) | 0x8000;
-       Val >>= 15;
+    /* Print a comment with the function signature */
+    PrintType (F, GetFuncReturn (T));
+    if (IsQualNear (T)) {
+        fprintf (F, " __near__");
     }
-}
-
+    if (IsQualFar (T)) {
+        fprintf (F, " __far__");
+    }
+    if (IsQualFastcall (T)) {
+        fprintf (F, " __fastcall__");
+    }
+    if (IsQualCDecl (T)) {
+        fprintf (F, " __cdecl__");
+    }
+    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, ", ");
+            }
+            if (SymIsRegVar (E)) {
+                fprintf (F, "register ");
+            }
+            PrintType (F, E->Type);
+            E = E->NextSym;
+        }
+    }
 
-void EncodePtr (type* Type, void* P)
-/* Encode a pointer into a type array */
-{
-    Encode (Type, (unsigned long) P);
+    /* End of parameter list */
+    fprintf (F, ")");
 }
 
 
 
-unsigned long Decode (const type* Type)
-/* Decode */
+void PrintRawType (FILE* F, const Type* T)
+/* Print a type string in raw format (for debugging) */
 {
-    int I;
-    unsigned long Val = 0;
-    for (I = DECODE_SIZE-1; I >= 0; I--) {
-       Val <<= 15;
-       Val |= (Type[I] & 0x7FFF);
+    while (T->C != T_END) {
+        fprintf (F, "%04lX ", T->C);
+        ++T;
     }
-    return Val;
+    fprintf (F, "\n");
 }
 
 
 
-void* DecodePtr (const type* Type)
-/* Decode a pointer from a type array */
+int TypeHasAttr (const Type* T)
+/* Return true if the given type has attribute data */
 {
-    return (void*) Decode (Type);
+    return IsClassStruct (T) || IsTypeArray (T) || IsClassFunc (T);
 }
 
 
 
-int HasEncode (const type* Type)
-/* Return true if the given type has encoded data */
+unsigned SizeOf (const Type* T)
+/* Compute size of object represented by type array. */
 {
-    return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
-}
+    switch (UnqualifiedType (T->C)) {
 
+        case T_VOID:
+            /* 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 */
 
-void CopyEncode (const type* Source, type* Target)
-/* Copy encoded data from Source to Target */
-{
-    memcpy (Target, Source, DECODE_SIZE * sizeof (type));
-}
+        case T_SCHAR:
+        case T_UCHAR:
+            return SIZEOF_CHAR;
 
+        case T_SHORT:
+        case T_USHORT:
+            return SIZEOF_SHORT;
 
+        case T_INT:
+        case T_UINT:
+            return SIZEOF_INT;
 
-type UnqualifiedType (type T)
-/* Return the unqalified type */
-{
-    return (T & ~T_MASK_QUAL);
-}
+        case T_PTR:
+            return SIZEOF_PTR;
 
+        case T_LONG:
+        case T_ULONG:
+            return SIZEOF_LONG;
 
+        case T_LONGLONG:
+        case T_ULONGLONG:
+            return SIZEOF_LONGLONG;
 
-unsigned SizeOf (const type* T)
-/* Compute size of object represented by type array. */
-{
-    SymEntry* Entry;
+        case T_ENUM:
+            return SIZEOF_INT;
 
-    switch (UnqualifiedType (T[0])) {
+        case T_FLOAT:
+            return SIZEOF_FLOAT;
 
-       case T_VOID:
-           Error ("Variable has unknown size");
-           return 1;   /* Return something that makes sense */
+        case T_DOUBLE:
+            return SIZEOF_DOUBLE;
 
-       case T_SCHAR:
-       case T_UCHAR:
-           return 1;
+        case T_STRUCT:
+        case T_UNION:
+            return ((SymEntry*) T->A.P)->V.S.Size;
 
-               case T_SHORT:
-       case T_USHORT:
-       case T_INT:
-       case T_UINT:
-       case T_PTR:
-           return 2;
+        case T_ARRAY:
+            if (T->A.L == UNSPECIFIED) {
+                /* Array with unspecified size */
+                return 0;
+            } else {
+                return T->A.U * SizeOf (T + 1);
+            }
 
-        case T_LONG:
-       case T_ULONG:
-           return 4;
+        default:
+            Internal ("Unknown type in SizeOf: %04lX", T->C);
+            return 0;
 
-       case T_LONGLONG:
-       case T_ULONGLONG:
-           return 8;
+    }
+}
 
-        case T_ENUM:
-           return 2;
 
-       case T_FLOAT:
-       case T_DOUBLE:
-           return 4;
 
-       case T_STRUCT:
-       case T_UNION:
-                   Entry = DecodePtr (T+1);
-                   return Entry->V.S.Size;
+unsigned PSizeOf (const Type* T)
+/* Compute size of pointer object. */
+{
+    /* We are expecting a pointer expression */
+    CHECK (IsClassPtr (T));
 
-       case T_ARRAY:
-           return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
+    /* Skip the pointer or array token itself */
+    return SizeOf (T + 1);
+}
 
-       default:
-           Internal ("Unknown type in SizeOf: %04X", *T);
-           return 0;
 
+
+unsigned CheckedSizeOf (const Type* T)
+/* Return the size of a data type. If the size is zero, emit an error and
+** return some valid size instead (so the rest of the compiler doesn't have
+** to work with invalid sizes).
+*/
+{
+    unsigned Size = SizeOf (T);
+    if (Size == 0) {
+        Error ("Size of data type is unknown");
+        Size = SIZEOF_CHAR;     /* Don't return zero */
     }
+    return Size;
 }
 
 
 
-unsigned PSizeOf (const type* T)
-/* Compute size of pointer object. */
+unsigned CheckedPSizeOf (const Type* T)
+/* Return the size of a data type that is pointed to by a pointer. If the
+** size is zero, emit an error and return some valid size instead (so the
+** rest of the compiler doesn't have to work with invalid sizes).
+*/
 {
-    /* We are expecting a pointer expression */
-    CHECK ((*T & T_CLASS_PTR) != 0);
-
-    /* Skip the pointer or array token itself */
-    if (IsTypeArray (T)) {
-               return SizeOf (T + DECODE_SIZE + 1);
-    } else {
-       return SizeOf (T + 1);
+    unsigned Size = PSizeOf (T);
+    if (Size == 0) {
+        Error ("Size of data type is unknown");
+        Size = SIZEOF_CHAR;     /* Don't return zero */
     }
+    return Size;
 }
 
 
 
-unsigned TypeOf (const type* T)
+unsigned TypeOf (const Type* T)
 /* Get the code generator base type of the object */
 {
-    FuncDesc* F;
+    switch (UnqualifiedType (T->C)) {
 
-    switch (UnqualifiedType (T[0])) {
-           
-       case T_SCHAR:
-           return CF_CHAR;
+        case T_SCHAR:
+            return CF_CHAR;
 
-       case T_UCHAR:
-           return CF_CHAR | CF_UNSIGNED;
+        case T_UCHAR:
+            return CF_CHAR | CF_UNSIGNED;
 
-       case T_SHORT:
-       case T_INT:
+        case T_SHORT:
+        case T_INT:
         case T_ENUM:
-           return CF_INT;
+            return CF_INT;
 
-       case T_USHORT:
-       case T_UINT:
-       case T_PTR:
-       case T_ARRAY:
-           return CF_INT | CF_UNSIGNED;
+        case T_USHORT:
+        case T_UINT:
+        case T_PTR:
+        case T_ARRAY:
+            return CF_INT | CF_UNSIGNED;
 
         case T_LONG:
-           return CF_LONG;
+            return CF_LONG;
 
-               case T_ULONG:
-                   return CF_LONG | CF_UNSIGNED;
+        case T_ULONG:
+            return CF_LONG | CF_UNSIGNED;
+
+        case T_FLOAT:
+        case T_DOUBLE:
+            /* These two are identical in the backend */
+            return CF_FLOAT;
 
         case T_FUNC:
-           F = DecodePtr (T+1);
-           return (F->Flags & FD_ELLIPSIS)? 0 : CF_FIXARGC;
+            return (((FuncDesc*) T->A.P)->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
 
         case T_STRUCT:
         case T_UNION:
-                   /* Address of ... */
-                   return CF_INT | CF_UNSIGNED;
+            /* Address of ... */
+            return CF_INT | CF_UNSIGNED;
 
-               default:
-                   Error ("Illegal type");
-                   return CF_INT;
+        default:
+            Error ("Illegal type %04lX", T->C);
+            return CF_INT;
     }
 }
 
 
 
-type* Indirect (type* T)
+Type* Indirect (Type* T)
 /* Do one indirection for the given type, that is, return the type where the
- * given type points to.
- */
+** given type points to.
+*/
 {
     /* We are expecting a pointer expression */
-    CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
+    CHECK (IsClassPtr (T));
 
     /* Skip the pointer or array token itself */
-    if (IsTypeArray (T)) {
-               return T + DECODE_SIZE + 1;
-    } else {
-       return T + 1;
-    }
-}
-
-
-
-int IsTypeChar (const type* T)
-/* Return true if this is a character type */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
+    return T + 1;
 }
 
 
 
-int IsTypeInt (const type* T)
-/* Return true if this is an int type (signed or unsigned) */
+Type* ArrayToPtr (Type* T)
+/* Convert an array to a pointer to it's first element */
 {
-    return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
+    /* Return pointer to first element */
+    return PointerTo (GetElementType (T));
 }
 
 
 
-int IsTypeLong (const type* T)
-/* Return true if this is a long type (signed or unsigned) */
+int IsVariadicFunc (const Type* T)
+/* Return true if this is a function type or pointer to function type with
+** variable parameter list
+*/
 {
-    return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
+    FuncDesc* F = GetFuncDesc (T);
+    return (F->Flags & FD_VARIADIC) != 0;
 }
 
 
 
-int IsTypeFloat (const type* T)
-/* Return true if this is a float type */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_FLOAT;
-}
-
-
-
-int IsTypeDouble (const type* T)
-/* Return true if this is a double type */
+FuncDesc* GetFuncDesc (const Type* T)
+/* Get the FuncDesc pointer from a function or pointer-to-function type */
 {
-    return (T[0] & T_MASK_TYPE) == T_TYPE_DOUBLE;
-}
-
+    if (UnqualifiedType (T->C) == T_PTR) {
+        /* Pointer to function */
+        ++T;
+    }
 
+    /* Be sure it's a function type */
+    CHECK (IsClassFunc (T));
 
-int IsTypePtr (const type* T)
-/* Return true if this is a pointer type */
-{
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
+    /* Get the function descriptor from the type attributes */
+    return T->A.P;
 }
 
 
 
-int IsTypeArray (const type* T)
-/* Return true if this is an array type */
+void SetFuncDesc (Type* T, FuncDesc* F)
+/* Set the FuncDesc pointer in a function or pointer-to-function type */
 {
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
-}
-
+    if (UnqualifiedType (T->C) == T_PTR) {
+        /* Pointer to function */
+        ++T;
+    }
 
+    /* Be sure it's a function type */
+    CHECK (IsClassFunc (T));
 
-int IsTypeVoid (const type* T)
-/* Return true if this is a void type */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
+    /* Set the function descriptor */
+    T->A.P = F;
 }
 
 
 
-int IsTypeFunc (const type* T)
-/* Return true if this is a function class */
+Type* GetFuncReturn (Type* T)
+/* Return a pointer to the return type of a function or pointer-to-function type */
 {
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
-}
-
+    if (UnqualifiedType (T->C) == T_PTR) {
+        /* Pointer to function */
+        ++T;
+    }
 
+    /* Be sure it's a function type */
+    CHECK (IsClassFunc (T));
 
-int IsClassInt (const type* T)
-/* Return true if this is an integer type */
-{
-    return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
+    /* Return a pointer to the return type */
+    return T + 1;
 }
 
 
 
-int IsClassFloat (const type* T)
-/* Return true if this is a float type */
+long GetElementCount (const Type* T)
+/* Get the element count of the array specified in T (which must be of
+** array type).
+*/
 {
-    return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
+    CHECK (IsTypeArray (T));
+    return T->A.L;
 }
 
 
 
-int IsClassPtr (const type* T)
-/* Return true if this is a pointer type */
+void SetElementCount (Type* T, long Count)
+/* Set the element count of the array specified in T (which must be of
+** array type).
+*/
 {
-    return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
+    CHECK (IsTypeArray (T));
+    T->A.L = Count;
 }
 
 
 
-int IsClassStruct (const type* T)
-/* Return true if this is a struct type */
+Type* GetElementType (Type* T)
+/* Return the element type of the given array type. */
 {
-    return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
+    CHECK (IsTypeArray (T));
+    return T + 1;
 }
 
 
 
-int IsSignUnsigned (const type* T)
-/* Return true if this is an unsigned type */
+Type* GetBaseElementType (Type* T)
+/* Return the base element type of a given type. If T is not an array, this
+** will return. Otherwise it will return the base element type, which means
+** the element type that is not an array.
+*/
 {
-    return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
+    while (IsTypeArray (T)) {
+        ++T;
+    }
+    return T;
 }
 
 
 
-int IsQualConst (const type* T)
-/* Return true if the given type has a const memory image */
+SymEntry* GetSymEntry (const Type* T)
+/* Return a SymEntry pointer from a type */
 {
-    return (GetQualifier (T) & T_QUAL_CONST) != 0;
-}
-
+    /* Only structs or unions have a SymEntry attribute */
+    CHECK (IsClassStruct (T));
 
-
-int IsQualVolatile (const type* T)
-/* Return true if the given type has a volatile type qualifier */
-{
-    return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
+    /* Return the attribute */
+    return T->A.P;
 }
 
 
 
-int IsFastCallFunc (const type* T)
-/* Return true if this is a function type with __fastcall__ calling conventions */
+void SetSymEntry (Type* T, SymEntry* S)
+/* Set the SymEntry pointer for a type */
 {
-    FuncDesc* F;
-    CHECK (IsTypeFunc (T));
-    F = DecodePtr (T+1);
-    return (F->Flags & FD_FASTCALL) != 0;
-}
-
+    /* Only structs or unions have a SymEntry attribute */
+    CHECK (IsClassStruct (T));
 
-
-int IsTypeFuncPtr (const type* T)
-/* Return true if this is a function pointer */
-{
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
+    /* Set the attribute */
+    T->A.P = S;
 }
 
 
 
-type GetType (const type* T)
-/* Get the raw type */
+Type* IntPromotion (Type* T)
+/* Apply the integer promotions to T and return the result. The returned type
+** string may be T if there is no need to change it.
+*/
 {
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_TYPE);
-}
-
+    /* We must have an int to apply int promotions */
+    PRECONDITION (IsClassInt (T));
 
-
-type GetClass (const type* T)
-/* Get the class of a type string */
-{
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_CLASS);
+    /* An integer can represent all values from either signed or unsigned char,
+    ** so convert chars to int and leave all other types alone.
+    */
+    if (IsTypeChar (T)) {
+        return type_int;
+    } else {
+        return T;
+    }
 }
 
 
 
-type GetSignedness (const type* T)
-/* Get the sign of a type */
+Type* PtrConversion (Type* T)
+/* If the type is a function, convert it to pointer to function. If the
+** expression is an array, convert it to pointer to first element. Otherwise
+** return T.
+*/
 {
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_SIGN);
+    if (IsTypeFunc (T)) {
+        return PointerTo (T);
+    } else if (IsTypeArray (T)) {
+        return ArrayToPtr (T);
+    } else {
+        return T;
+    }
 }
 
 
 
-type GetSizeModifier (const type* T)
-/* Get the size modifier of a type */
+TypeCode AddrSizeQualifier (unsigned AddrSize)
+/* Return T_QUAL_NEAR or T_QUAL_FAR depending on the address size */
 {
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_SIZE);
-}
-
+    switch (AddrSize) {
 
+        case ADDR_SIZE_ABS:
+            return T_QUAL_NEAR;
 
-type GetQualifier (const type* T)
-/* Get the qualifier from the given type string */
-{
-    /* If this is an array, look at the element type, otherwise look at the
-     * type itself.
-     */
-    if (IsTypeArray (T)) {
-       T += DECODE_SIZE + 1;
-    }
-    return (T[0] & T_QUAL_CONST);
-}
-
+        case ADDR_SIZE_FAR:
+            return T_QUAL_FAR;
 
+        default:
+            Error ("Invalid address size");
+            return T_QUAL_NEAR;
 
-struct FuncDesc* GetFuncDesc (const type* T)
-/* Get the FuncDesc pointer from 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);
-
-    /* Decode the function descriptor and return it */
-    return DecodePtr (T+1);
 }
-
-
-
-