+ case TYPE_TYPEDEF: {
+ decl_modifiers_t modifiers = type->typedeft.typedefe->modifiers;
+ modifiers |= get_type_modifiers(type->typedeft.typedefe->type);
+ return modifiers;
+ }
+ case TYPE_TYPEOF:
+ return get_type_modifiers(type->typeoft.typeof_type);
+ }
+ panic("invalid type found in get_type_modifiers");
+}
+
+type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type)
+{
+ type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
+
+ while (true) {
+ switch (type->base.kind) {
+ case TYPE_ERROR:
+ return TYPE_QUALIFIER_NONE;
+ 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;
+ else
+ type = typedef_type->typedefe->type;
+ continue;
+ case TYPE_TYPEOF:
+ type = type->typeoft.typeof_type;
+ continue;
+ case TYPE_ARRAY:
+ if (skip_array_type) {
+ type = type->array.element_type;
+ continue;
+ }
+ break;
+ default:
+ break;
+ }
+ break;
+ }
+ return type->base.qualifiers | qualifiers;
+}
+
+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;
+}
+
+/**
+ * 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 (size_t i = 0; i < lengthof(possible_kinds); ++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 (size_t i = 0; i < lengthof(possible_kinds); ++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.
+ */
+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 *const type = allocate_type_zero(TYPE_ATOMIC);
+ type->base.qualifiers = qualifiers;
+ 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 *const type = allocate_type_zero(TYPE_COMPLEX);
+ type->base.qualifiers = qualifiers;
+ type->atomic.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 *const type = allocate_type_zero(TYPE_IMAGINARY);
+ type->base.qualifiers = qualifiers;
+ type->atomic.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 *const type = allocate_type_zero(TYPE_POINTER);
+ type->base.qualifiers = qualifiers;
+ type->pointer.points_to = points_to;
+ type->pointer.base_variable = NULL;
+
+ return identify_new_type(type);
+}
+
+/**
+ * Creates a new reference type.
+ *
+ * @param refers_to The referred-to type for the new type.
+ */
+type_t *make_reference_type(type_t *refers_to)
+{
+ type_t *const type = allocate_type_zero(TYPE_REFERENCE);
+ type->base.qualifiers = TYPE_QUALIFIER_NONE;
+ type->reference.refers_to = refers_to;
+
+ return identify_new_type(type);
+}
+
+/**
+ * Creates a new based pointer type.
+ *
+ * @param points_to The points-to type for the new type.
+ * @param qualifiers Type qualifiers for the new type.
+ * @param variable The based variable
+ */
+type_t *make_based_pointer_type(type_t *points_to,
+ type_qualifiers_t qualifiers, variable_t *variable)
+{
+ type_t *const type = allocate_type_zero(TYPE_POINTER);
+ type->base.qualifiers = qualifiers;
+ type->pointer.points_to = points_to;
+ type->pointer.base_variable = variable;
+
+ return identify_new_type(type);
+}
+
+
+type_t *make_array_type(type_t *element_type, size_t size,
+ type_qualifiers_t qualifiers)
+{
+ type_t *const type = allocate_type_zero(TYPE_ARRAY);
+ type->base.qualifiers = qualifiers;
+ type->array.element_type = element_type;
+ type->array.size = size;
+ type->array.size_constant = true;
+
+ return identify_new_type(type);
+}
+
+static entity_t *pack_bitfield_members(il_size_t *struct_offset,
+ il_alignment_t *struct_alignment,
+ bool packed, entity_t *first)
+{
+ il_size_t offset = *struct_offset;
+ il_alignment_t alignment = *struct_alignment;
+ size_t bit_offset = 0;
+
+ entity_t *member;
+ for (member = first; member != NULL; member = member->base.next) {
+ if (member->kind != ENTITY_COMPOUND_MEMBER)
+ continue;
+ if (!member->compound_member.bitfield)
+ break;
+
+ type_t *const base_type = skip_typeref(member->declaration.type);
+ il_alignment_t base_alignment = get_type_alignment_compound(base_type);
+ il_alignment_t alignment_mask = base_alignment-1;
+ if (base_alignment > alignment)
+ alignment = base_alignment;
+
+ size_t bit_size = member->compound_member.bit_size;
+ if (!packed) {
+ bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
+ offset &= ~alignment_mask;
+ size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
+
+ if (bit_offset + bit_size > base_size || bit_size == 0) {
+ offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
+ offset = (offset + base_alignment-1) & ~alignment_mask;
+ bit_offset = 0;
+ }
+ }
+
+ if (byte_order_big_endian) {
+ size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
+ member->compound_member.offset = offset & ~alignment_mask;
+ member->compound_member.bit_offset = base_size - bit_offset - bit_size;
+ } else {
+ member->compound_member.offset = offset;
+ member->compound_member.bit_offset = bit_offset;
+ }
+
+ bit_offset += bit_size;
+ offset += bit_offset / BITS_PER_BYTE;
+ bit_offset %= BITS_PER_BYTE;
+ }
+
+ if (bit_offset > 0)
+ offset += 1;
+
+ *struct_offset = offset;
+ *struct_alignment = alignment;
+ return member;
+}
+
+void layout_struct_type(compound_type_t *type)
+{
+ assert(type->compound != NULL);
+
+ compound_t *compound = type->compound;
+ if (!compound->complete)
+ return;
+ if (type->compound->layouted)
+ return;
+
+ il_size_t offset = 0;
+ il_alignment_t alignment = compound->alignment;
+ bool need_pad = false;
+
+ entity_t *entry = compound->members.entities;
+ while (entry != NULL) {
+ if (entry->kind != ENTITY_COMPOUND_MEMBER) {
+ entry = entry->base.next;
+ continue;
+ }
+
+ type_t *const m_type = skip_typeref(entry->declaration.type);
+ if (!is_type_valid(m_type)) {
+ entry = entry->base.next;
+ continue;
+ }
+
+ if (entry->compound_member.bitfield) {
+ entry = pack_bitfield_members(&offset, &alignment,
+ compound->packed, entry);
+ continue;
+ }
+
+ il_alignment_t m_alignment = get_type_alignment_compound(m_type);
+ if (m_alignment > alignment)
+ alignment = m_alignment;
+
+ if (!compound->packed) {
+ il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
+
+ if (new_offset > offset) {
+ need_pad = true;
+ offset = new_offset;
+ }
+ }
+
+ entry->compound_member.offset = offset;
+ offset += get_type_size(m_type);
+
+ entry = entry->base.next;
+ }
+
+ if (!compound->packed) {
+ il_size_t new_offset = (offset + alignment-1) & -alignment;
+ if (new_offset > offset) {
+ need_pad = true;
+ offset = new_offset;
+ }
+ }
+
+ source_position_t const *const pos = &compound->base.source_position;
+ if (need_pad) {
+ warningf(WARN_PADDED, pos, "'%T' needs padding", type);
+ } else if (compound->packed) {
+ warningf(WARN_PACKED, pos, "superfluous packed attribute on '%T'", type);
+ }
+
+ compound->size = offset;
+ compound->alignment = alignment;
+ compound->layouted = true;
+}
+
+void layout_union_type(compound_type_t *type)
+{
+ assert(type->compound != NULL);
+
+ compound_t *compound = type->compound;
+ if (! compound->complete)
+ return;
+
+ il_size_t size = 0;
+ il_alignment_t alignment = compound->alignment;
+
+ entity_t *entry = compound->members.entities;
+ for (; entry != NULL; entry = entry->base.next) {
+ if (entry->kind != ENTITY_COMPOUND_MEMBER)
+ continue;
+
+ type_t *m_type = skip_typeref(entry->declaration.type);
+ if (! is_type_valid(skip_typeref(m_type)))
+ continue;
+
+ entry->compound_member.offset = 0;
+ il_size_t m_size = get_type_size(m_type);
+ if (m_size > size)
+ size = m_size;
+ il_alignment_t m_alignment = get_type_alignment_compound(m_type);
+ if (m_alignment > alignment)
+ alignment = m_alignment;
+ }
+ size = (size + alignment - 1) & -alignment;
+
+ compound->size = size;
+ compound->alignment = alignment;
+}
+
+function_parameter_t *allocate_parameter(type_t *const type)
+{
+ function_parameter_t *const param = obstack_alloc(&type_obst, sizeof(*param));
+ memset(param, 0, sizeof(*param));
+ param->type = type;
+ return param;
+}
+
+type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
+ type_t *argument_type2, decl_modifiers_t modifiers)
+{
+ function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
+ function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
+ parameter1->next = parameter2;
+
+ type_t *type = allocate_type_zero(TYPE_FUNCTION);
+ type->function.return_type = return_type;
+ type->function.parameters = parameter1;
+ type->function.modifiers |= modifiers;
+ type->function.linkage = LINKAGE_C;
+
+ return identify_new_type(type);
+}
+
+type_t *make_function_1_type(type_t *return_type, type_t *argument_type,
+ decl_modifiers_t modifiers)
+{
+ function_parameter_t *const parameter = allocate_parameter(argument_type);
+
+ type_t *type = allocate_type_zero(TYPE_FUNCTION);
+ type->function.return_type = return_type;
+ type->function.parameters = parameter;
+ type->function.modifiers |= modifiers;
+ type->function.linkage = LINKAGE_C;
+
+ return identify_new_type(type);
+}
+
+type_t *make_function_1_type_variadic(type_t *return_type,
+ type_t *argument_type,
+ decl_modifiers_t modifiers)
+{
+ function_parameter_t *const parameter = allocate_parameter(argument_type);
+
+ type_t *type = allocate_type_zero(TYPE_FUNCTION);
+ type->function.return_type = return_type;
+ type->function.parameters = parameter;
+ type->function.variadic = true;
+ type->function.modifiers |= modifiers;
+ type->function.linkage = LINKAGE_C;
+
+ return identify_new_type(type);
+}
+
+type_t *make_function_0_type(type_t *return_type, decl_modifiers_t modifiers)
+{
+ type_t *type = allocate_type_zero(TYPE_FUNCTION);
+ type->function.return_type = return_type;
+ type->function.parameters = NULL;
+ type->function.modifiers |= modifiers;
+ type->function.linkage = LINKAGE_C;
+
+ return identify_new_type(type);
+}
+
+type_t *make_function_type(type_t *return_type, int n_types,
+ type_t *const *argument_types,
+ decl_modifiers_t modifiers)
+{
+ type_t *type = allocate_type_zero(TYPE_FUNCTION);
+ type->function.return_type = return_type;
+ type->function.modifiers |= modifiers;
+ type->function.linkage = LINKAGE_C;
+
+ function_parameter_t **anchor = &type->function.parameters;
+ for (int i = 0; i < n_types; ++i) {
+ function_parameter_t *parameter = allocate_parameter(argument_types[i]);
+ *anchor = parameter;
+ anchor = ¶meter->next;