+
+ /* TODO: incomplete */
+ return false;
+}
+
+/**
+ * Check if two pointer types are compatible.
+ */
+bool pointers_compatible(const type_t *type1, const type_t *type2)
+{
+ assert(!is_typeref(type1));
+ assert(!is_typeref(type2));
+
+ assert(type1->kind == TYPE_POINTER);
+ assert(type2->kind == TYPE_POINTER);
+ (void) type1;
+ (void) type2;
+ /* TODO */
+ return true;
+}
+
+/**
+ * Skip all typerefs and return the underlying type.
+ */
+type_t *skip_typeref(type_t *type)
+{
+ unsigned qualifiers = TYPE_QUALIFIER_NONE;
+
+ while(true) {
+ switch(type->kind) {
+ case TYPE_ERROR:
+ return type;
+ case TYPE_TYPEDEF: {
+ qualifiers |= type->base.qualifiers;
+ 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) {
+ type_t *const copy = duplicate_type(type);
+ copy->base.qualifiers |= qualifiers;
+
+ 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;
+ }
+ return kind;
+}
+
+/**
+ * Hash the given type and return the "singleton" version
+ * of it.
+ */
+static type_t *identify_new_type(type_t *type)
+{
+ type_t *result = typehash_insert(type);
+ if(result != type) {
+ obstack_free(type_obst, type);
+ }
+ return result;
+}
+
+/**
+ * Creates a new atomic type.
+ *
+ * @param akind The kind of the atomic type.
+ * @param qualifiers Type qualifiers for the new type.
+ */
+type_t *make_atomic_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
+{
+ type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
+ memset(type, 0, sizeof(atomic_type_t));
+
+ type->kind = TYPE_ATOMIC;
+ type->base.qualifiers = qualifiers;
+ type->base.alignment = get_atomic_type_alignment(akind);
+ type->atomic.akind = akind;
+
+ return identify_new_type(type);
+}
+
+/**
+ * Creates a new complex type.
+ *
+ * @param akind The kind of the atomic type.
+ * @param qualifiers Type qualifiers for the new type.
+ */
+type_t *make_complex_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
+{
+ type_t *type = obstack_alloc(type_obst, sizeof(complex_type_t));
+ memset(type, 0, sizeof(complex_type_t));
+
+ type->kind = TYPE_COMPLEX;
+ type->base.qualifiers = qualifiers;
+ type->base.alignment = get_atomic_type_alignment(akind);
+ type->complex.akind = akind;
+
+ return identify_new_type(type);
+}
+
+/**
+ * Creates a new imaginary type.
+ *
+ * @param akind The kind of the atomic type.
+ * @param qualifiers Type qualifiers for the new type.
+ */
+type_t *make_imaginary_type(atomic_type_kind_t akind, type_qualifiers_t qualifiers)
+{
+ type_t *type = obstack_alloc(type_obst, sizeof(imaginary_type_t));
+ memset(type, 0, sizeof(imaginary_type_t));
+
+ type->kind = TYPE_IMAGINARY;
+ type->base.qualifiers = qualifiers;
+ type->base.alignment = get_atomic_type_alignment(akind);
+ type->imaginary.akind = akind;
+
+ return identify_new_type(type);
+}
+
+/**
+ * Creates a new pointer type.
+ *
+ * @param points_to The points-to type for the new type.
+ * @param qualifiers Type qualifiers for the new type.
+ */
+type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
+{
+ type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
+ memset(type, 0, sizeof(pointer_type_t));
+
+ 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);