fix test
[cparser] / type.c
diff --git a/type.c b/type.c
index e7ceb05..2ef7b85 100644 (file)
--- a/type.c
+++ b/type.c
@@ -1,16 +1,38 @@
+/*
+ * This file is part of cparser.
+ * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
 #include <config.h>
 
 #include <stdio.h>
 #include <assert.h>
+
 #include "type_t.h"
+#include "symbol_t.h"
 #include "type_hash.h"
 #include "adt/error.h"
+#include "lang_features.h"
 
 static struct obstack   _type_obst;
 static FILE            *out;
 struct obstack         *type_obst                 = &_type_obst;
 static int              type_visited              = 0;
-static bool             print_implicit_array_size = true;
+static bool             print_implicit_array_size = false;
 
 static void intern_print_type_pre(const type_t *type, bool top);
 static void intern_print_type_post(const type_t *type, bool top);
@@ -52,26 +74,41 @@ void print_atomic_type(const atomic_type_t *type)
 {
        print_type_qualifiers(type->type.qualifiers);
 
-       const char *s;
-       switch(type->akind) {
-       case ATOMIC_TYPE_INVALID:     s = "INVALIDATOMIC";      break;
-       case ATOMIC_TYPE_VOID:        s = "void";               break;
-       case ATOMIC_TYPE_BOOL:        s = "_Bool";              break;
-       case ATOMIC_TYPE_CHAR:        s = "char";               break;
-       case ATOMIC_TYPE_SCHAR:       s = "signed char";        break;
-       case ATOMIC_TYPE_UCHAR:       s = "unsigned char";      break;
-       case ATOMIC_TYPE_INT:         s = "int";                break;
-       case ATOMIC_TYPE_UINT:        s = "unsigned int";       break;
-       case ATOMIC_TYPE_SHORT:       s = "short";              break;
-       case ATOMIC_TYPE_USHORT:      s = "unsigned short";     break;
-       case ATOMIC_TYPE_LONG:        s = "long";               break;
-       case ATOMIC_TYPE_ULONG:       s = "unsigned long";      break;
-       case ATOMIC_TYPE_LONGLONG:    s = "long long";          break;
-       case ATOMIC_TYPE_ULONGLONG:   s = "unsigned long long"; break;
-       case ATOMIC_TYPE_LONG_DOUBLE: s = "long double";        break;
-       case ATOMIC_TYPE_FLOAT:       s = "float";              break;
-       case ATOMIC_TYPE_DOUBLE:      s = "double";             break;
-       default:                      s = "UNKNOWNATOMIC";      break;
+       const char *s = "INVALIDATOMIC";
+       switch((atomic_type_kind_t) type->akind) {
+       case ATOMIC_TYPE_INVALID:     break;
+       case ATOMIC_TYPE_VOID:                  s = "void";               break;
+       case ATOMIC_TYPE_BOOL:                  s = "_Bool";              break;
+       case ATOMIC_TYPE_CHAR:                  s = "char";               break;
+       case ATOMIC_TYPE_SCHAR:                 s = "signed char";        break;
+       case ATOMIC_TYPE_UCHAR:                 s = "unsigned char";      break;
+       case ATOMIC_TYPE_INT:                   s = "int";                break;
+       case ATOMIC_TYPE_UINT:                  s = "unsigned int";       break;
+       case ATOMIC_TYPE_SHORT:                 s = "short";              break;
+       case ATOMIC_TYPE_USHORT:                s = "unsigned short";     break;
+       case ATOMIC_TYPE_LONG:                  s = "long";               break;
+       case ATOMIC_TYPE_ULONG:                 s = "unsigned long";      break;
+       case ATOMIC_TYPE_LONGLONG:              s = "long long";          break;
+       case ATOMIC_TYPE_ULONGLONG:             s = "unsigned long long"; break;
+       case ATOMIC_TYPE_LONG_DOUBLE:           s = "long double";        break;
+       case ATOMIC_TYPE_FLOAT:                 s = "float";              break;
+       case ATOMIC_TYPE_DOUBLE:                s = "double";             break;
+       case ATOMIC_TYPE_INT8:                  s = "__int8";             break;
+       case ATOMIC_TYPE_INT16:                 s = "__int16";            break;
+       case ATOMIC_TYPE_INT32:                 s = "__int32";            break;
+       case ATOMIC_TYPE_INT64:                 s = "__int64";            break;
+       case ATOMIC_TYPE_INT128:                s = "__int128";           break;
+       case ATOMIC_TYPE_UINT8:                 s = "unsigned __int8";    break;
+       case ATOMIC_TYPE_UINT16:                s = "unsigned __int16";   break;
+       case ATOMIC_TYPE_UINT32:                s = "unsigned __int32";   break;
+       case ATOMIC_TYPE_UINT64:                s = "unsigned __int64";   break;
+       case ATOMIC_TYPE_UINT128:               s = "unsigned __int128";  break;
+       case ATOMIC_TYPE_FLOAT_COMPLEX:         s = "_Complex float";     break;
+       case ATOMIC_TYPE_DOUBLE_COMPLEX:        s = "_Complex float";     break;
+       case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:   s = "_Complex float";     break;
+       case ATOMIC_TYPE_FLOAT_IMAGINARY:       s = "_Imaginary float";   break;
+       case ATOMIC_TYPE_DOUBLE_IMAGINARY:      s = "_Imaginary float";   break;
+       case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY: s = "_Imaginary float";   break;
        }
        fputs(s, out);
 }
@@ -190,9 +227,9 @@ static void print_array_type_post(const array_type_t *type)
                fputs("static ", out);
        }
        print_type_qualifiers(type->type.qualifiers);
-       if(type->size != NULL
+       if(type->size_expression != NULL
                        && (print_implicit_array_size || !type->has_implicit_size)) {
-               print_expression(type->size);
+               print_expression(type->size_expression);
        }
        fputc(']', out);
        intern_print_type_post(type->element_type, false);
@@ -229,7 +266,13 @@ void print_enum_definition(const declaration_t *declaration)
                fprintf(out, "%s", entry->symbol->string);
                if(entry->init.initializer != NULL) {
                        fprintf(out, " = ");
-                       print_expression(entry->init.enum_value);
+
+                       /* skip the implicit cast */
+                       expression_t *expression = entry->init.enum_value;
+                       if(expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
+                               expression = expression->unary.value;
+                       }
+                       print_expression(expression);
                }
                fprintf(out, ",\n");
        }
@@ -538,7 +581,7 @@ bool is_type_integer(const type_t *type)
        if(type->kind != TYPE_ATOMIC)
                return false;
 
-       switch(type->atomic.akind) {
+       switch((atomic_type_kind_t) type->atomic.akind) {
        case ATOMIC_TYPE_BOOL:
        case ATOMIC_TYPE_CHAR:
        case ATOMIC_TYPE_SCHAR:
@@ -551,10 +594,33 @@ bool is_type_integer(const type_t *type)
        case ATOMIC_TYPE_ULONG:
        case ATOMIC_TYPE_LONGLONG:
        case ATOMIC_TYPE_ULONGLONG:
+       case ATOMIC_TYPE_INT8:
+       case ATOMIC_TYPE_INT16:
+       case ATOMIC_TYPE_INT32:
+       case ATOMIC_TYPE_INT64:
+       case ATOMIC_TYPE_INT128:
+       case ATOMIC_TYPE_UINT8:
+       case ATOMIC_TYPE_UINT16:
+       case ATOMIC_TYPE_UINT32:
+       case ATOMIC_TYPE_UINT64:
+       case ATOMIC_TYPE_UINT128:
                return true;
-       default:
+
+       case ATOMIC_TYPE_INVALID:
+       case ATOMIC_TYPE_VOID:
+       case ATOMIC_TYPE_FLOAT:
+       case ATOMIC_TYPE_DOUBLE:
+       case ATOMIC_TYPE_LONG_DOUBLE:
+       case ATOMIC_TYPE_FLOAT_COMPLEX:
+       case ATOMIC_TYPE_DOUBLE_COMPLEX:
+       case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
+       case ATOMIC_TYPE_FLOAT_IMAGINARY:
+       case ATOMIC_TYPE_DOUBLE_IMAGINARY:
+       case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
                return false;
        }
+
+       panic("unexpected atomic type kind");
 }
 
 /**
@@ -570,22 +636,46 @@ bool is_type_float(const type_t *type)
        if(type->kind != TYPE_ATOMIC)
                return false;
 
-       switch(type->atomic.akind) {
+       switch((atomic_type_kind_t) type->atomic.akind) {
        case ATOMIC_TYPE_FLOAT:
        case ATOMIC_TYPE_DOUBLE:
        case ATOMIC_TYPE_LONG_DOUBLE:
-#ifdef PROVIDE_COMPLEX
        case ATOMIC_TYPE_FLOAT_COMPLEX:
        case ATOMIC_TYPE_DOUBLE_COMPLEX:
        case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
        case ATOMIC_TYPE_FLOAT_IMAGINARY:
        case ATOMIC_TYPE_DOUBLE_IMAGINARY:
        case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
-#endif
                return true;
-       default:
+
+       case ATOMIC_TYPE_INVALID:
+       case ATOMIC_TYPE_VOID:
+       case ATOMIC_TYPE_BOOL:
+       case ATOMIC_TYPE_CHAR:
+       case ATOMIC_TYPE_SCHAR:
+       case ATOMIC_TYPE_UCHAR:
+       case ATOMIC_TYPE_SHORT:
+       case ATOMIC_TYPE_USHORT:
+       case ATOMIC_TYPE_INT:
+       case ATOMIC_TYPE_UINT:
+       case ATOMIC_TYPE_LONG:
+       case ATOMIC_TYPE_ULONG:
+       case ATOMIC_TYPE_LONGLONG:
+       case ATOMIC_TYPE_ULONGLONG:
+       case ATOMIC_TYPE_INT8:
+       case ATOMIC_TYPE_INT16:
+       case ATOMIC_TYPE_INT32:
+       case ATOMIC_TYPE_INT64:
+       case ATOMIC_TYPE_INT128:
+       case ATOMIC_TYPE_UINT8:
+       case ATOMIC_TYPE_UINT16:
+       case ATOMIC_TYPE_UINT32:
+       case ATOMIC_TYPE_UINT64:
+       case ATOMIC_TYPE_UINT128:
                return false;
        }
+
+       panic("unexpected atomic type kind");
 }
 
 /**
@@ -605,7 +695,7 @@ bool is_type_signed(const type_t *type)
        if(type->kind != TYPE_ATOMIC)
                return false;
 
-       switch(type->atomic.akind) {
+       switch((atomic_type_kind_t) type->atomic.akind) {
        case ATOMIC_TYPE_CHAR:
        case ATOMIC_TYPE_SCHAR:
        case ATOMIC_TYPE_SHORT:
@@ -615,14 +705,17 @@ bool is_type_signed(const type_t *type)
        case ATOMIC_TYPE_FLOAT:
        case ATOMIC_TYPE_DOUBLE:
        case ATOMIC_TYPE_LONG_DOUBLE:
-#ifdef PROVIDE_COMPLEX
+       case ATOMIC_TYPE_INT8:
+       case ATOMIC_TYPE_INT16:
+       case ATOMIC_TYPE_INT32:
+       case ATOMIC_TYPE_INT64:
+       case ATOMIC_TYPE_INT128:
        case ATOMIC_TYPE_FLOAT_COMPLEX:
        case ATOMIC_TYPE_DOUBLE_COMPLEX:
        case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
        case ATOMIC_TYPE_FLOAT_IMAGINARY:
        case ATOMIC_TYPE_DOUBLE_IMAGINARY:
        case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
-#endif
                return true;
 
        case ATOMIC_TYPE_BOOL:
@@ -631,11 +724,15 @@ bool is_type_signed(const type_t *type)
        case ATOMIC_TYPE_UINT:
        case ATOMIC_TYPE_ULONG:
        case ATOMIC_TYPE_ULONGLONG:
+       case ATOMIC_TYPE_UINT8:
+       case ATOMIC_TYPE_UINT16:
+       case ATOMIC_TYPE_UINT32:
+       case ATOMIC_TYPE_UINT64:
+       case ATOMIC_TYPE_UINT128:
                return false;
 
        case ATOMIC_TYPE_VOID:
        case ATOMIC_TYPE_INVALID:
-       case ATOMIC_TYPE_LAST:
                return false;
        }
 
@@ -708,7 +805,7 @@ bool is_type_incomplete(const type_t *type)
                return true;
 
        case TYPE_ARRAY:
-               return type->array.size == NULL;
+               return type->array.size_expression == NULL;
 
        case TYPE_ATOMIC:
                return type->atomic.akind == ATOMIC_TYPE_VOID;
@@ -780,12 +877,10 @@ static bool array_types_compatible(const array_type_t *array1,
        if(!types_compatible(element_type1, element_type2))
                return false;
 
-       if(array1->size != NULL && array2->size != NULL) {
-               /* TODO: check if size expression evaluate to the same value
-                * if they are constant */
-       }
+       if(!array1->size_constant || !array2->size_constant)
+               return true;
 
-       return true;
+       return array1->size == array2->size;
 }
 
 /**
@@ -910,6 +1005,77 @@ type_t *skip_typeref(type_t *type)
        return type;
 }
 
+unsigned get_atomic_type_size(atomic_type_kind_t kind)
+{
+       switch(kind) {
+       case ATOMIC_TYPE_CHAR:
+       case ATOMIC_TYPE_SCHAR:
+       case ATOMIC_TYPE_UCHAR:
+       case ATOMIC_TYPE_INT8:
+       case ATOMIC_TYPE_UINT8:
+               return 1;
+
+       case ATOMIC_TYPE_SHORT:
+       case ATOMIC_TYPE_USHORT:
+       case ATOMIC_TYPE_INT16:
+       case ATOMIC_TYPE_UINT16:
+               return 2;
+
+       case ATOMIC_TYPE_INT32:
+       case ATOMIC_TYPE_UINT32:
+               return 4;
+
+       case ATOMIC_TYPE_INT64:
+       case ATOMIC_TYPE_UINT64:
+               return 8;
+
+       case ATOMIC_TYPE_INT128:
+       case ATOMIC_TYPE_UINT128:
+               return 16;
+
+       case ATOMIC_TYPE_BOOL:
+       case ATOMIC_TYPE_INT:
+       case ATOMIC_TYPE_UINT:
+               return machine_size >> 3;
+
+       case ATOMIC_TYPE_LONG:
+       case ATOMIC_TYPE_ULONG:
+               return machine_size > 16 ? machine_size >> 3 : 4;
+
+       case ATOMIC_TYPE_LONGLONG:
+       case ATOMIC_TYPE_ULONGLONG:
+               return machine_size > 16 ? 8 : 4;
+
+       case ATOMIC_TYPE_FLOAT_IMAGINARY:
+       case ATOMIC_TYPE_FLOAT:
+               return 4;
+
+       case ATOMIC_TYPE_DOUBLE_IMAGINARY:
+       case ATOMIC_TYPE_DOUBLE:
+               return 8;
+
+       case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
+       case ATOMIC_TYPE_LONG_DOUBLE:
+               return 12;
+
+       case ATOMIC_TYPE_VOID:
+               return 1;
+
+       case ATOMIC_TYPE_FLOAT_COMPLEX:
+               return 2 * get_atomic_type_size(ATOMIC_TYPE_FLOAT);
+
+       case ATOMIC_TYPE_DOUBLE_COMPLEX:
+               return 2 * get_atomic_type_size(ATOMIC_TYPE_DOUBLE);
+
+       case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
+               return 2 * get_atomic_type_size(ATOMIC_TYPE_LONG_DOUBLE);
+
+       case ATOMIC_TYPE_INVALID:
+               break;
+       }
+       panic("Trying to determine size of invalid atomic type");
+}
+
 /**
  * Hash the given type and return the "singleton" version
  * of it.
@@ -936,8 +1102,11 @@ type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
 
        type->kind            = TYPE_ATOMIC;
        type->base.qualifiers = qualifiers;
+       type->base.alignment  = 0;
        type->atomic.akind    = atype;
 
+       /* TODO: set the aligmnent depending on the atype here */
+
        return identify_new_type(type);
 }
 
@@ -954,11 +1123,28 @@ type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
 
        type->kind              = TYPE_POINTER;
        type->base.qualifiers   = qualifiers;
+       type->base.alignment    = 0;
        type->pointer.points_to = points_to;
 
        return identify_new_type(type);
 }
 
+type_t *make_array_type(type_t *element_type, size_t size,
+                        type_qualifiers_t qualifiers)
+{
+       type_t *type = obstack_alloc(type_obst, sizeof(array_type_t));
+       memset(type, 0, sizeof(array_type_t));
+
+       type->kind                = TYPE_ARRAY;
+       type->base.qualifiers     = qualifiers;
+       type->base.alignment      = 0;
+       type->array.element_type  = element_type;
+       type->array.size          = size;
+       type->array.size_constant = true;
+
+       return identify_new_type(type);
+}
+
 /**
  * Debug helper. Prints the given type to stdout.
  */