+ return false;
+ }
+}
+
+/**
+ * Returns true if the given type is an integer or float type.
+ *
+ * @param type The type to check.
+ * @return True if type is an integer or float type.
+ */
+bool is_type_real(const type_t *type)
+{
+ /* 6.2.5.17 */
+ return is_type_integer(type)
+ || (type->kind == TYPE_ATOMIC && is_type_float(type));
+}
+
+/**
+ * Returns true if the given type represents a scalar type.
+ *
+ * @param type The type to check.
+ * @return True if type represents a scalar type.
+ */
+bool is_type_scalar(const type_t *type)
+{
+ assert(!is_typeref(type));
+
+ switch (type->kind) {
+ case TYPE_POINTER: return true;
+ case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
+ default: break;
+ }
+
+ return is_type_arithmetic(type);
+}
+
+/**
+ * Check if a given type is incomplete.
+ *
+ * @param type The type to check.
+ * @return True if the given type is incomplete (ie. just forward).
+ */
+bool is_type_incomplete(const type_t *type)
+{
+ assert(!is_typeref(type));
+
+ switch(type->kind) {
+ case TYPE_COMPOUND_STRUCT:
+ case TYPE_COMPOUND_UNION: {
+ const compound_type_t *compound_type = &type->compound;
+ declaration_t *declaration = compound_type->declaration;
+ return !declaration->init.complete;
+ }
+ case TYPE_ENUM: {
+ const enum_type_t *enum_type = &type->enumt;
+ declaration_t *declaration = enum_type->declaration;
+ return !declaration->init.complete;
+ }
+
+ case TYPE_ARRAY:
+ return type->array.size_expression == NULL
+ && !type->array.size_constant;
+
+ case TYPE_ATOMIC:
+ return type->atomic.akind == ATOMIC_TYPE_VOID;
+
+ case TYPE_COMPLEX:
+ return type->complex.akind == ATOMIC_TYPE_VOID;
+
+ case TYPE_IMAGINARY:
+ return type->imaginary.akind == ATOMIC_TYPE_VOID;
+
+ case TYPE_BITFIELD:
+ case TYPE_FUNCTION:
+ case TYPE_POINTER:
+ case TYPE_BUILTIN:
+ case TYPE_ERROR:
+ return false;
+
+ case TYPE_TYPEDEF:
+ case TYPE_TYPEOF:
+ panic("is_type_incomplete called without typerefs skipped");
+ case TYPE_INVALID:
+ break;
+ }
+
+ panic("invalid type found");
+}
+
+bool is_type_object(const type_t *type)
+{
+ return !is_type_function(type) && !is_type_incomplete(type);
+}
+
+/**
+ * Check if two function types are compatible.
+ */
+static bool function_types_compatible(const function_type_t *func1,
+ const function_type_t *func2)
+{
+ const type_t* const ret1 = skip_typeref(func1->return_type);
+ const type_t* const ret2 = skip_typeref(func2->return_type);
+ if (!types_compatible(ret1, ret2))
+ return false;
+
+ /* can parameters be compared? */
+ if (func1->unspecified_parameters || func2->unspecified_parameters)
+ return true;
+
+ if (func1->variadic != func2->variadic)
+ return false;
+
+ if (func1->calling_convention != func2->calling_convention)
+ return false;
+
+ /* TODO: handling of unspecified parameters not correct yet */
+
+ /* all argument types must be compatible */
+ function_parameter_t *parameter1 = func1->parameters;
+ function_parameter_t *parameter2 = func2->parameters;
+ for ( ; parameter1 != NULL && parameter2 != NULL;
+ parameter1 = parameter1->next, parameter2 = parameter2->next) {
+ type_t *parameter1_type = skip_typeref(parameter1->type);
+ type_t *parameter2_type = skip_typeref(parameter2->type);
+
+ parameter1_type = get_unqualified_type(parameter1_type);
+ parameter2_type = get_unqualified_type(parameter2_type);
+
+ if (!types_compatible(parameter1_type, parameter2_type))
+ return false;
+ }
+ /* same number of arguments? */
+ if (parameter1 != NULL || parameter2 != NULL)
+ return false;
+
+ return true;
+}
+
+/**
+ * Check if two array types are compatible.
+ */
+static bool array_types_compatible(const array_type_t *array1,
+ const array_type_t *array2)
+{
+ type_t *element_type1 = skip_typeref(array1->element_type);
+ type_t *element_type2 = skip_typeref(array2->element_type);
+ if (!types_compatible(element_type1, element_type2))
+ return false;
+
+ if (!array1->size_constant || !array2->size_constant)
+ return true;
+
+ return array1->size == array2->size;
+}
+
+/**
+ * Check if two types are compatible.
+ */
+bool types_compatible(const type_t *type1, const type_t *type2)
+{
+ assert(!is_typeref(type1));
+ assert(!is_typeref(type2));
+
+ /* shortcut: the same type is always compatible */
+ if (type1 == type2)
+ return true;
+
+ if (type1->base.qualifiers != type2->base.qualifiers)
+ return false;
+ if (type1->kind != type2->kind)
+ return false;
+
+ switch(type1->kind) {
+ case TYPE_FUNCTION:
+ return function_types_compatible(&type1->function, &type2->function);
+ case TYPE_ATOMIC:
+ return type1->atomic.akind == type2->atomic.akind;
+ case TYPE_COMPLEX:
+ return type1->complex.akind == type2->complex.akind;
+ case TYPE_IMAGINARY:
+ return type1->imaginary.akind == type2->imaginary.akind;
+ case TYPE_ARRAY:
+ return array_types_compatible(&type1->array, &type2->array);
+
+ case TYPE_POINTER: {
+ const type_t *const to1 = skip_typeref(type1->pointer.points_to);
+ const type_t *const to2 = skip_typeref(type2->pointer.points_to);
+ return types_compatible(to1, to2);
+ }
+
+ case TYPE_COMPOUND_STRUCT:
+ case TYPE_COMPOUND_UNION:
+ case TYPE_ENUM:
+ case TYPE_BUILTIN:
+ /* TODO: not implemented */
+ break;
+
+ case TYPE_BITFIELD:
+ /* not sure if this makes sense or is even needed, implement it if you
+ * really need it! */
+ panic("type compatibility check for bitfield type");
+
+ case TYPE_ERROR:
+ /* Hmm, the error type should be compatible to all other types */
+ return true;
+ case TYPE_INVALID:
+ panic("invalid type found in compatible types");
+ case TYPE_TYPEDEF:
+ case TYPE_TYPEOF:
+ panic("typerefs not skipped in compatible types?!?");
+ }
+
+ /* TODO: incomplete */
+ return false;
+}
+
+/**
+ * Skip all typerefs and return the underlying type.
+ */
+type_t *skip_typeref(type_t *type)
+{
+ type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
+ type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
+
+ while(true) {
+ switch(type->kind) {
+ case TYPE_ERROR:
+ return type;
+ case TYPE_TYPEDEF: {
+ qualifiers |= type->base.qualifiers;
+ modifiers |= type->base.modifiers;
+ const typedef_type_t *typedef_type = &type->typedeft;
+ if (typedef_type->resolved_type != NULL) {
+ type = typedef_type->resolved_type;
+ break;
+ }
+ type = typedef_type->declaration->type;
+ continue;
+ }
+ case TYPE_TYPEOF: {
+ const typeof_type_t *typeof_type = &type->typeoft;
+ if (typeof_type->typeof_type != NULL) {
+ type = typeof_type->typeof_type;
+ } else {
+ type = typeof_type->expression->base.type;
+ }
+ continue;
+ }
+ default:
+ break;
+ }
+ break;
+ }
+
+ if (qualifiers != TYPE_QUALIFIER_NONE || modifiers != TYPE_MODIFIER_NONE) {
+ type_t *const copy = duplicate_type(type);
+
+ /* for const with typedefed array type the element type has to be
+ * adjusted */
+ if (is_type_array(copy)) {
+ type_t *element_type = copy->array.element_type;
+ element_type = duplicate_type(element_type);
+ element_type->base.qualifiers |= qualifiers;
+ element_type->base.modifiers |= modifiers;
+ copy->array.element_type = element_type;
+ } else {
+ copy->base.qualifiers |= qualifiers;
+ copy->base.modifiers |= modifiers;
+ }
+
+ type = typehash_insert(copy);
+ if (type != copy) {
+ obstack_free(type_obst, copy);
+ }
+ }
+
+ return type;
+}
+
+unsigned get_atomic_type_size(atomic_type_kind_t kind)
+{
+ assert(kind <= ATOMIC_TYPE_LAST);
+ return atomic_type_properties[kind].size;
+}
+
+unsigned get_atomic_type_alignment(atomic_type_kind_t kind)
+{
+ assert(kind <= ATOMIC_TYPE_LAST);
+ return atomic_type_properties[kind].alignment;
+}
+
+unsigned get_atomic_type_flags(atomic_type_kind_t kind)
+{
+ assert(kind <= ATOMIC_TYPE_LAST);
+ return atomic_type_properties[kind].flags;
+}
+
+atomic_type_kind_t get_intptr_kind(void)
+{
+ if (machine_size <= 32)
+ return ATOMIC_TYPE_INT;
+ else if (machine_size <= 64)
+ return ATOMIC_TYPE_LONG;
+ else
+ return ATOMIC_TYPE_LONGLONG;
+}
+
+atomic_type_kind_t get_uintptr_kind(void)
+{
+ if (machine_size <= 32)
+ return ATOMIC_TYPE_UINT;
+ else if (machine_size <= 64)
+ return ATOMIC_TYPE_ULONG;
+ else
+ return ATOMIC_TYPE_ULONGLONG;
+}
+
+/**
+ * Find the atomic type kind representing a given size (signed).
+ */
+atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size) {
+ static atomic_type_kind_t kinds[32];
+
+ assert(size < 32);
+ atomic_type_kind_t kind = kinds[size];
+ if (kind == ATOMIC_TYPE_INVALID) {
+ static const atomic_type_kind_t possible_kinds[] = {
+ ATOMIC_TYPE_SCHAR,
+ ATOMIC_TYPE_SHORT,
+ ATOMIC_TYPE_INT,
+ ATOMIC_TYPE_LONG,
+ ATOMIC_TYPE_LONGLONG
+ };
+ for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
+ if (get_atomic_type_size(possible_kinds[i]) == size) {
+ kind = possible_kinds[i];
+ break;
+ }
+ }
+ kinds[size] = kind;
+ }
+ return kind;
+}
+
+/**
+ * Find the atomic type kind representing a given size (signed).
+ */
+atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size) {
+ static atomic_type_kind_t kinds[32];
+
+ assert(size < 32);
+ atomic_type_kind_t kind = kinds[size];
+ if (kind == ATOMIC_TYPE_INVALID) {
+ static const atomic_type_kind_t possible_kinds[] = {
+ ATOMIC_TYPE_UCHAR,
+ ATOMIC_TYPE_USHORT,
+ ATOMIC_TYPE_UINT,
+ ATOMIC_TYPE_ULONG,
+ ATOMIC_TYPE_ULONGLONG
+ };
+ for(unsigned i = 0; i < sizeof(possible_kinds)/sizeof(possible_kinds[0]); ++i) {
+ if (get_atomic_type_size(possible_kinds[i]) == size) {
+ kind = possible_kinds[i];
+ break;
+ }
+ }
+ kinds[size] = kind;