]> git.sur5r.net Git - cc65/blobdiff - src/cc65/datatype.c
New function ReplaceType
[cc65] / src / cc65 / datatype.c
index 1bc0c74af7de80ddd15afa65243616ec823c039b..3996e04a52d324d10588ea2306dd14bf90f07c73 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2000 Ullrich von Bassewitz                                       */
+/* (C) 1998-2002 Ullrich von Bassewitz                                       */
 /*               Wacholderweg 14                                             */
 /*               D-70597 Stuttgart                                           */
 /* EMail:        uz@musoftware.de                                            */
@@ -135,6 +135,18 @@ void TypeFree (type* T)
 
 
 
+int SignExtendChar (int C)
+/* Do correct sign extension of a character */
+{
+    if (SignedChars && (C & 0x80) != 0) {
+               return C | ~0xFF;
+    } else {
+               return C & 0xFF;
+    }
+}
+
+
+
 type GetDefaultChar (void)
 /* Return the default char type (signed/unsigned) depending on the settings */
 {
@@ -410,14 +422,6 @@ void CopyEncode (const type* Source, type* Target)
 
 
 
-type UnqualifiedType (type T)
-/* Return the unqalified type */
-{
-    return (T & ~T_MASK_QUAL);
-}
-
-
-
 unsigned SizeOf (const type* T)
 /* Compute size of object represented by type array. */
 {
@@ -426,34 +430,40 @@ unsigned SizeOf (const type* T)
     switch (UnqualifiedType (T[0])) {
 
        case T_VOID:
-           Error ("Variable has unknown size");
-           return 1;   /* Return something that makes sense */
+                   return 0;   /* Assume voids have size zero */
 
        case T_SCHAR:
        case T_UCHAR:
-           return 1;
+           return SIZEOF_CHAR;
 
                case T_SHORT:
        case T_USHORT:
+            return SIZEOF_SHORT;
+
        case T_INT:
        case T_UINT:
+            return SIZEOF_INT;
+
        case T_PTR:
-           return 2;
+       case T_FUNC:    /* Maybe pointer to function */
+           return SIZEOF_PTR;
 
         case T_LONG:
        case T_ULONG:
-           return 4;
+           return SIZEOF_LONG;
 
        case T_LONGLONG:
        case T_ULONGLONG:
-           return 8;
+           return SIZEOF_LONGLONG;
 
         case T_ENUM:
-           return 2;
+           return SIZEOF_INT;
 
        case T_FLOAT:
+            return SIZEOF_FLOAT;
+
        case T_DOUBLE:
-           return 4;
+           return SIZEOF_DOUBLE;
 
        case T_STRUCT:
        case T_UNION:
@@ -476,7 +486,7 @@ unsigned PSizeOf (const type* T)
 /* Compute size of pointer object. */
 {
     /* We are expecting a pointer expression */
-    CHECK ((*T & T_CLASS_PTR) != 0);
+    CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
 
     /* Skip the pointer or array token itself */
     if (IsTypeArray (T)) {
@@ -488,6 +498,38 @@ unsigned PSizeOf (const type* T)
 
 
 
+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 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).
+ */
+{
+    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)
 /* Get the code generator base type of the object */
 {
@@ -541,7 +583,7 @@ type* Indirect (type* T)
  */
 {
     /* We are expecting a pointer expression */
-    CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
+    CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
 
     /* Skip the pointer or array token itself */
     if (IsTypeArray (T)) {
@@ -553,78 +595,6 @@ type* Indirect (type* T)
 
 
 
-int IsTypeChar (const type* T)
-/* Return true if this is a character type */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
-}
-
-
-
-int IsTypeInt (const type* T)
-/* Return true if this is an int type (signed or unsigned) */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
-}
-
-
-
-int IsTypeLong (const type* T)
-/* Return true if this is a long type (signed or unsigned) */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
-}
-
-
-
-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 */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_DOUBLE;
-}
-
-
-
-int IsTypePtr (const type* T)
-/* Return true if this is a pointer type */
-{
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
-}
-
-
-
-int IsTypeArray (const type* T)
-/* Return true if this is an array type */
-{
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
-}
-
-
-
-int IsTypeVoid (const type* T)
-/* Return true if this is a void type */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
-}
-
-
-
-int IsTypeFunc (const type* T)
-/* Return true if this is a function class */
-{
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
-}
-
-
-
 int IsClassInt (const type* T)
 /* Return true if this is an integer type */
 {
@@ -703,50 +673,6 @@ int IsVariadicFunc (const type* 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);
-}
-
-
-
-type GetType (const type* T)
-/* Get the raw type */
-{
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_TYPE);
-}
-
-
-
-type GetClass (const type* T)
-/* Get the class of a type string */
-{
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_CLASS);
-}
-
-
-
-type GetSignedness (const type* T)
-/* Get the sign of a type */
-{
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_SIGN);
-}
-
-
-
-type GetSizeModifier (const type* T)
-/* Get the size modifier of a type */
-{
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_SIZE);
-}
-
-
-
 type GetQualifier (const type* T)
 /* Get the qualifier from the given type string */
 {
@@ -764,7 +690,7 @@ type GetQualifier (const type* T)
 FuncDesc* GetFuncDesc (const type* T)
 /* Get the FuncDesc pointer from a function or pointer-to-function type */
 {
-    if (UnqualifiedType (T[0]) == T_PTR) {      
+    if (UnqualifiedType (T[0]) == T_PTR) {
        /* Pointer to function */
        ++T;
     }