functions.c
// Recurse through a type and make list of all scalars + their offsets
Recurse through a type and make list of all scalars + their offsets
Recurse through a type and make list of all scalars + their offsets
static void flatten_type(Type *type, StructOrUnionScalars *scalars, int offset) {
    if (type->type == TYPE_STRUCT_OR_UNION) {
        StructOrUnion *s = type->struct_or_union_desc;
        for (StructOrUnionMember **pmember = s->members; *pmember; pmember++) {
            StructOrUnionMember *member = *pmember;
            flatten_type(member->type, scalars, offset + member->offset);
        }
    }
    else if (type->type == TYPE_ARRAY) {
        int element_size = get_type_size(type->target);
        for (int i = 0; i < type->array_size; i++)
            flatten_type(type->target, scalars, offset + element_size * i);
    }
    else {
        StructOrUnionScalar *scalar = malloc(sizeof(StructOrUnionScalar));
        if (scalars->count == MAX_STRUCT_OR_UNION_SCALARS) panic("Exceeded max number of scalars in a struct/union");
        scalars->scalars[scalars->count++] = scalar;
        scalar->type = type;
        scalar->offset = offset;
    }
}
static void flatten_type ( Type * type , StructOrUnionScalars * scalars , int offset ) { if ( type -> type == TYPE_STRUCT_OR_UNION ) { StructOrUnion * s = type -> struct_or_union_desc ; for ( StructOrUnionMember * * pmember = s -> members ; * pmember ; pmember ++ ) { StructOrUnionMember * member = * pmember ; flatten_type ( member -> type , scalars , offset + member -> offset ) ; } } else if ( type -> type == TYPE_ARRAY ) { int element_size = get_type_size ( type -> target ) ; for ( int i = 0 ; i < type -> array_size ; i ++ ) flatten_type ( type -> target , scalars , offset + element_size * i ) ; } else { StructOrUnionScalar * scalar = malloc ( sizeof ( StructOrUnionScalar ) ) ; if ( scalars -> count == MAX_STRUCT_OR_UNION_SCALARS ) panic ( " " ) ; scalars -> scalars [ scalars -> count ++ ] = scalar ; scalar -> type = type ; scalar -> offset = offset ; } }
Recurse through a type and make list of all scalars + their offsets
Recurse through a type and make list of all scalars + their offsets