Change internals from Record to Struct

This commit is contained in:
Ginger Bill
2017-07-20 15:23:13 +01:00
parent 6d37ed12d2
commit 2da18b6d33
9 changed files with 293 additions and 297 deletions
+1 -1
View File
@@ -698,7 +698,7 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
String name = e->token.string;
Type *t = base_type(type_deref(e->type));
if (is_type_struct(t) || is_type_raw_union(t)) {
Scope *scope = scope_of_node(&c->info, t->Record.node);
Scope *scope = scope_of_node(&c->info, t->Struct.node);
GB_ASSERT(scope != nullptr);
for_array(i, scope->elements.entries) {
Entity *f = scope->elements.entries[i].value;
+83 -83
View File
@@ -125,8 +125,8 @@ bool check_is_assignable_to_using_subtype(Type *src, Type *dst) {
return false;
}
for (isize i = 0; i < src->Record.field_count; i++) {
Entity *f = src->Record.fields[i];
for (isize i = 0; i < src->Struct.field_count; i++) {
Entity *f = src->Struct.fields[i];
if (f->kind != Entity_Variable || (f->flags&EntityFlag_Using) == 0) {
continue;
}
@@ -732,9 +732,9 @@ void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *>
str = expr_to_string(node);
}
if (t->kind == Type_Record) {
for (isize i = 0; i < t->Record.field_count; i++) {
Entity *f = t->Record.fields[i];
if (t->kind == Type_Struct) {
for (isize i = 0; i < t->Struct.field_count; i++) {
Entity *f = t->Struct.fields[i];
GB_ASSERT(f->kind == Entity_Variable);
String name = f->token.string;
HashKey key = hash_string(name);
@@ -760,7 +760,7 @@ void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *>
}
void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields, Map<Entity *> *entity_map, AstNode *record_node, String context, bool allow_default_values) {
void check_struct_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields, Map<Entity *> *entity_map, AstNode *struct_node, String context, bool allow_default_values) {
GB_ASSERT(fields != nullptr);
if (decl->kind == AstNode_WhenStmt) {
ast_node(ws, WhenStmt, decl);
@@ -778,18 +778,18 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
operand.value.value_bool) {
for_array(i, ws->body->BlockStmt.stmts) {
AstNode *stmt = ws->body->BlockStmt.stmts[i];
check_record_field_decl(c, stmt, fields, entity_map, record_node, context, allow_default_values);
check_struct_field_decl(c, stmt, fields, entity_map, struct_node, context, allow_default_values);
}
} else if (ws->else_stmt) {
switch (ws->else_stmt->kind) {
case AstNode_BlockStmt:
for_array(i, ws->else_stmt->BlockStmt.stmts) {
AstNode *stmt = ws->else_stmt->BlockStmt.stmts[i];
check_record_field_decl(c, stmt, fields, entity_map, record_node, context, allow_default_values);
check_struct_field_decl(c, stmt, fields, entity_map, struct_node, context, allow_default_values);
}
break;
case AstNode_WhenStmt:
check_record_field_decl(c, ws->else_stmt, fields, entity_map, record_node, context, allow_default_values);
check_struct_field_decl(c, ws->else_stmt, fields, entity_map, struct_node, context, allow_default_values);
break;
default:
error(ws->else_stmt, "Invalid `else` statement in `when` statement");
@@ -959,7 +959,7 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
}
}
populate_using_entity_map(c, record_node, type, entity_map);
populate_using_entity_map(c, struct_node, type, entity_map);
}
}
@@ -997,7 +997,7 @@ Array<Entity *> check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
}
for_array(decl_index, decls) {
check_record_field_decl(c, decls[decl_index], &fields, &entity_map, node, context, context == "struct");
check_struct_field_decl(c, decls[decl_index], &fields, &entity_map, node, context, context == "struct");
}
@@ -1042,7 +1042,7 @@ GB_COMPARE_PROC(cmp_reorder_struct_fields) {
return diff < 0 ? -1 : diff > 0;
}
Entity *make_names_field_for_record(Checker *c, Scope *scope) {
Entity *make_names_field_for_struct(Checker *c, Scope *scope) {
Entity *e = make_entity_field(c->allocator, scope,
make_token_ident(str_lit("names")), t_string_slice, false, 0);
e->Variable.is_immutable = true;
@@ -1065,10 +1065,10 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Opera
case_end;
}
}
struct_type->Record.names = make_names_field_for_record(c, c->context.scope);
struct_type->Struct.names = make_names_field_for_struct(c, c->context.scope);
if (st->is_raw_union) {
struct_type->Record.is_raw_union = true;
struct_type->Struct.is_raw_union = true;
context = str_lit("struct #raw_union");
}
@@ -1120,7 +1120,7 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Opera
if (false && !is_type_polymorphic_struct(specialization)) {
gbString str = type_to_string(specialization);
defer (gb_string_free(str));
error(s, "Expected a polymorphic record, got %s", str);
error(s, "Expected a polymorphic struct, got %s", str);
specialization = nullptr;
}
}
@@ -1225,29 +1225,29 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Opera
fields = check_fields(c, node, st->fields, min_field_count, context);
}
struct_type->Record.scope = c->context.scope;
struct_type->Record.is_packed = st->is_packed;
struct_type->Record.is_ordered = st->is_ordered;
struct_type->Record.fields = fields.data;
struct_type->Record.fields_in_src_order = fields.data;
struct_type->Record.field_count = fields.count;
struct_type->Record.polymorphic_params = polymorphic_params;
struct_type->Record.is_polymorphic = is_polymorphic;
struct_type->Record.is_poly_specialized = is_poly_specialized;
struct_type->Struct.scope = c->context.scope;
struct_type->Struct.is_packed = st->is_packed;
struct_type->Struct.is_ordered = st->is_ordered;
struct_type->Struct.fields = fields.data;
struct_type->Struct.fields_in_src_order = fields.data;
struct_type->Struct.field_count = fields.count;
struct_type->Struct.polymorphic_params = polymorphic_params;
struct_type->Struct.is_polymorphic = is_polymorphic;
struct_type->Struct.is_poly_specialized = is_poly_specialized;
if (!struct_type->Record.is_raw_union) {
if (!struct_type->Struct.is_raw_union) {
type_set_offsets(c->allocator, struct_type);
if (!struct_type->failure && !st->is_packed && !st->is_ordered) {
struct_type->failure = false;
struct_type->Record.are_offsets_set = false;
struct_type->Record.offsets = nullptr;
struct_type->Struct.are_offsets_set = false;
struct_type->Struct.offsets = nullptr;
// NOTE(bill): Reorder fields for reduced size/performance
Entity **reordered_fields = gb_alloc_array(c->allocator, Entity *, fields.count);
for (isize i = 0; i < fields.count; i++) {
reordered_fields[i] = struct_type->Record.fields_in_src_order[i];
reordered_fields[i] = struct_type->Struct.fields_in_src_order[i];
}
// NOTE(bill): Hacky thing
@@ -1260,7 +1260,7 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Opera
reordered_fields[i]->Variable.field_index = i;
}
struct_type->Record.fields = reordered_fields;
struct_type->Struct.fields = reordered_fields;
}
type_set_offsets(c->allocator, struct_type);
@@ -1296,7 +1296,7 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Opera
if (custom_align < align) {
warning(st->align, "Custom alignment has been clamped to %lld from %lld", align, custom_align);
}
struct_type->Record.custom_align = custom_align;
struct_type->Struct.custom_align = custom_align;
return;
}
}
@@ -1370,13 +1370,13 @@ void check_union_type(Checker *c, Type *named_type, Type *union_type, AstNode *n
// }
// }
// union_type->Record.names = make_names_field_for_record(c, c->context.scope);
// union_type->Struct.names = make_names_field_for_struct(c, c->context.scope);
// auto fields = check_fields(c, node, ut->fields, min_field_count, str_lit("raw_union"));
// union_type->Record.scope = c->context.scope;
// union_type->Record.fields = fields.data;
// union_type->Record.field_count = fields.count;
// union_type->Struct.scope = c->context.scope;
// union_type->Struct.fields = fields.data;
// union_type->Struct.field_count = fields.count;
// }
@@ -1513,7 +1513,7 @@ void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *nod
enum_type->Enum.max_value = make_entity_constant(c->allocator, c->context.scope,
make_token_ident(str_lit("max_value")), constant_type, max_value);
enum_type->Enum.names = make_names_field_for_record(c, c->context.scope);
enum_type->Enum.names = make_names_field_for_struct(c, c->context.scope);
}
@@ -1639,17 +1639,17 @@ bool check_type_specialization_to(Checker *c, Type *specialization, Type *type,
return false;
}
// gb_printf_err("#1 %s %s\n", type_to_string(type), type_to_string(specialization));
if (t->kind == Type_Record) {
if (t->Record.polymorphic_parent == specialization) {
if (t->kind == Type_Struct) {
if (t->Struct.polymorphic_parent == specialization) {
return true;
}
if (t->Record.polymorphic_parent == s->Record.polymorphic_parent) {
GB_ASSERT(s->Record.polymorphic_params != nullptr);
GB_ASSERT(t->Record.polymorphic_params != nullptr);
if (t->Struct.polymorphic_parent == s->Struct.polymorphic_parent) {
GB_ASSERT(s->Struct.polymorphic_params != nullptr);
GB_ASSERT(t->Struct.polymorphic_params != nullptr);
TypeTuple *s_tuple = &s->Record.polymorphic_params->Tuple;
TypeTuple *t_tuple = &t->Record.polymorphic_params->Tuple;
TypeTuple *s_tuple = &s->Struct.polymorphic_params->Tuple;
TypeTuple *t_tuple = &t->Struct.polymorphic_params->Tuple;
GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
for_array(i, s_tuple->variables) {
Entity *s_e = s_tuple->variables[i];
@@ -1756,8 +1756,8 @@ bool is_polymorphic_type_assignable(Checker *c, Type *poly, Type *source, bool c
}
return false;
case Type_Record:
if (source->kind == Type_Record) {
case Type_Struct:
if (source->kind == Type_Struct) {
// return check_is_assignable_to(c, &o, poly);
}
return false;
@@ -2269,7 +2269,7 @@ Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type) {
case Type_Array:
case Type_Vector:
// Could be in C too
case Type_Record: {
case Type_Struct: {
i64 align = type_align_of(a, original_type);
i64 size = type_size_of(a, original_type);
switch (8*size) {
@@ -2308,7 +2308,7 @@ Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type) {
case Type_Array:
case Type_Vector:
// Could be in C too
case Type_Record: {
case Type_Struct: {
i64 align = type_align_of(a, original_type);
i64 size = type_size_of(a, original_type);
if (8*size > 16) {
@@ -2774,9 +2774,9 @@ void check_map_type(Checker *c, Type *type, AstNode *node) {
check_close_scope(c);
entry_type->Record.fields = fields;
entry_type->Record.fields_in_src_order = fields;
entry_type->Record.field_count = field_count;
entry_type->Struct.fields = fields;
entry_type->Struct.fields_in_src_order = fields;
entry_type->Struct.field_count = field_count;
type_set_offsets(a, entry_type);
type->Map.entry_type = entry_type;
@@ -2805,9 +2805,9 @@ void check_map_type(Checker *c, Type *type, AstNode *node) {
check_close_scope(c);
generated_struct_type->Record.fields = fields;
generated_struct_type->Record.fields_in_src_order = fields;
generated_struct_type->Record.field_count = field_count;
generated_struct_type->Struct.fields = fields;
generated_struct_type->Struct.fields_in_src_order = fields;
generated_struct_type->Struct.field_count = field_count;
type_set_offsets(a, generated_struct_type);
type->Map.generated_struct_type = generated_struct_type;
@@ -2869,7 +2869,7 @@ bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type)
specific = check_type(c, s);
if (false && !is_type_polymorphic_struct(specific)) {
gbString str = type_to_string(specific);
error(s, "Expected a polymorphic record, got %s", str);
error(s, "Expected a polymorphic struct, got %s", str);
gb_string_free(str);
specific = nullptr;
}
@@ -3005,7 +3005,7 @@ bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type)
check_open_scope(c, e);
check_struct_type(c, *type, e, nullptr);
check_close_scope(c);
(*type)->Record.node = e;
(*type)->Struct.node = e;
return true;
case_end;
@@ -3025,7 +3025,7 @@ bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type)
check_open_scope(c, e);
check_raw_union_type(c, *type, e);
check_close_scope(c);
(*type)->Record.node = e;
(*type)->Struct.node = e;
return true;
case_end;
*/
@@ -3687,8 +3687,8 @@ String check_down_cast_name(Type *dst_, Type *src_) {
Type *src = type_deref(src_);
Type *dst_s = base_type(dst);
GB_ASSERT(is_type_struct(dst_s) || is_type_raw_union(dst_s));
for (isize i = 0; i < dst_s->Record.field_count; i++) {
Entity *f = dst_s->Record.fields[i];
for (isize i = 0; i < dst_s->Struct.field_count; i++) {
Entity *f = dst_s->Struct.fields[i];
GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field);
if (f->flags & EntityFlag_Using) {
if (are_types_identical(f->type, src_)) {
@@ -4683,7 +4683,7 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node, Type *type_h
i64 max_count = 0;
switch (type->kind) {
case Type_Record: max_count = type->Record.field_count; break;
case Type_Struct: max_count = type->Struct.field_count; break;
case Type_Tuple: max_count = type->Tuple.variables.count; break;
}
@@ -5582,10 +5582,10 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
gbAllocator a = c->allocator;
Type *tuple = make_type_tuple(a);
i32 variable_count = type->Record.field_count;
i32 variable_count = type->Struct.field_count;
array_init_count(&tuple->Tuple.variables, a, variable_count);
// TODO(bill): Should I copy each of the entities or is this good enough?
gb_memcopy_array(tuple->Tuple.variables.data, type->Record.fields_in_src_order, variable_count);
gb_memcopy_array(tuple->Tuple.variables.data, type->Struct.fields_in_src_order, variable_count);
operand->type = tuple;
operand->mode = Addressing_Value;
@@ -6529,12 +6529,12 @@ CallArgumentData check_call_arguments(Checker *c, Operand *operand, Type *proc_t
Entity *find_using_index_expr(Type *t) {
t = base_type(t);
if (t->kind != Type_Record) {
if (t->kind != Type_Struct) {
return nullptr;
}
for (isize i = 0; i < t->Record.field_count; i++) {
Entity *f = t->Record.fields[i];
for (isize i = 0; i < t->Struct.field_count; i++) {
Entity *f = t->Struct.fields[i];
if (f->kind == Entity_Variable &&
(f->flags & EntityFlag_Field) != 0 &&
(f->flags & EntityFlag_Using) != 0) {
@@ -6550,7 +6550,7 @@ Entity *find_using_index_expr(Type *t) {
return nullptr;
}
isize lookup_polymorphic_struct_parameter(TypeRecord *st, String parameter_name) {
isize lookup_polymorphic_struct_parameter(TypeStruct *st, String parameter_name) {
if (!st->is_polymorphic) return -1;
TypeTuple *params = &st->polymorphic_params->Tuple;
@@ -6573,8 +6573,8 @@ CallArgumentError check_polymorphic_struct_type(Checker *c, Operand *operand, As
Type *original_type = operand->type;
Type *struct_type = base_type(operand->type);
GB_ASSERT(struct_type->kind == Type_Record);
TypeRecord *st = &struct_type->Record;
GB_ASSERT(struct_type->kind == Type_Struct);
TypeStruct *st = &struct_type->Struct;
GB_ASSERT(st->is_polymorphic);
bool show_error = true;
@@ -6736,7 +6736,7 @@ CallArgumentError check_polymorphic_struct_type(Checker *c, Operand *operand, As
for_array(i, *found_gen_types) {
Entity *e = (*found_gen_types)[i];
Type *t = base_type(e->type);
TypeTuple *tuple = &t->Record.polymorphic_params->Tuple;
TypeTuple *tuple = &t->Struct.polymorphic_params->Tuple;
bool ok = true;
GB_ASSERT(param_count == tuple->variables.count);
for (isize j = 0; j < param_count; j++) {
@@ -6778,8 +6778,8 @@ CallArgumentError check_polymorphic_struct_type(Checker *c, Operand *operand, As
check_open_scope(c, node);
check_struct_type(c, struct_type, node, &ordered_operands);
check_close_scope(c);
struct_type->Record.node = node;
struct_type->Record.polymorphic_parent = original_type;
struct_type->Struct.node = node;
struct_type->Struct.polymorphic_parent = original_type;
Entity *e = nullptr;
@@ -6799,7 +6799,7 @@ CallArgumentError check_polymorphic_struct_type(Checker *c, Operand *operand, As
named_type->Named.type_name = e;
if (!struct_type->Record.is_polymorphic) {
if (!struct_type->Struct.is_polymorphic) {
if (found_gen_types) {
array_add(found_gen_types, e);
} else {
@@ -7321,7 +7321,7 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t
switch (t->kind) {
case Type_Record: {
case Type_Struct: {
if (is_type_union(t)) {
is_constant = false;
}
@@ -7338,10 +7338,10 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t
}
{ // Checker values
isize field_count = t->Record.field_count;
isize min_field_count = t->Record.field_count;
isize field_count = t->Struct.field_count;
isize min_field_count = t->Struct.field_count;
for (isize i = min_field_count-1; i >= 0; i--) {
Entity *e = t->Record.fields_in_src_order[i];
Entity *e = t->Struct.fields_in_src_order[i];
GB_ASSERT(e->kind == Entity_Variable);
if (e->Variable.default_is_nil) {
min_field_count--;
@@ -7389,7 +7389,7 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t
continue;
}
Entity *field = t->Record.fields[sel.index[0]];
Entity *field = t->Struct.fields[sel.index[0]];
add_entity_use(c, fv->field, field);
if (fields_visited[sel.index[0]]) {
@@ -7412,8 +7412,8 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t
}
} else {
bool all_fields_are_blank = true;
for (isize i = 0; i < t->Record.field_count; i++) {
Entity *field = t->Record.fields_in_src_order[i];
for (isize i = 0; i < t->Struct.field_count; i++) {
Entity *field = t->Struct.fields_in_src_order[i];
if (!is_blank_ident(field->token)) {
all_fields_are_blank = false;
break;
@@ -7431,7 +7431,7 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t
break;
}
Entity *field = t->Record.fields_in_src_order[index];
Entity *field = t->Struct.fields_in_src_order[index];
if (!all_fields_are_blank && is_blank_ident(field->token)) {
// NOTE(bill): Ignore blank identifiers
continue;
@@ -7847,7 +7847,7 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t
valid = false;
}
if (!valid && t->kind == Type_Record) {
if (!valid && t->kind == Type_Struct) {
Entity *found = find_using_index_expr(t);
if (found != nullptr) {
valid = check_set_index_data(o, found->type, is_type_pointer(found->type), &max_count);
@@ -8133,7 +8133,7 @@ void check_expr_or_type(Checker *c, Operand *o, AstNode *e, Type *type_hint) {
gbString write_expr_to_string(gbString str, AstNode *node);
gbString write_record_fields_to_string(gbString str, Array<AstNode *> params) {
gbString write_struct_fields_to_string(gbString str, Array<AstNode *> params) {
for_array(i, params) {
if (i > 0) {
str = gb_string_appendc(str, ", ");
@@ -8469,21 +8469,21 @@ gbString write_expr_to_string(gbString str, AstNode *node) {
if (st->is_ordered) str = gb_string_appendc(str, "#ordered ");
if (st->is_raw_union) str = gb_string_appendc(str, "#raw_union ");
str = gb_string_appendc(str, "{");
str = write_record_fields_to_string(str, st->fields);
str = write_struct_fields_to_string(str, st->fields);
str = gb_string_appendc(str, "}");
case_end;
// case_ast_node(st, RawUnionType, node);
// str = gb_string_appendc(str, "raw_union ");
// str = gb_string_appendc(str, "{");
// str = write_record_fields_to_string(str, st->fields);
// str = write_struct_fields_to_string(str, st->fields);
// str = gb_string_appendc(str, "}");
// case_end;
case_ast_node(st, UnionType, node);
str = gb_string_appendc(str, "union ");
str = gb_string_appendc(str, "{");
str = write_record_fields_to_string(str, st->variants);
str = write_struct_fields_to_string(str, st->variants);
str = gb_string_appendc(str, "}");
case_end;
+6 -6
View File
@@ -315,7 +315,7 @@ Type *check_assignment_variable(Checker *c, Operand *rhs, AstNode *lhs_node) {
check_expr(c, &op_c, se->expr);
if (op_c.mode == Addressing_MapIndex) {
gbString str = expr_to_string(lhs.expr);
error(lhs.expr, "Cannot assign to record field `%s` in map", str);
error(lhs.expr, "Cannot assign to struct field `%s` in map", str);
gb_string_free(str);
return nullptr;
}
@@ -472,8 +472,8 @@ bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bo
switch (e->kind) {
case Entity_TypeName: {
Type *t = base_type(e->type);
if (t->kind == Type_Record) {
Scope *s = t->Record.scope;
if (t->kind == Type_Struct) {
Scope *s = t->Struct.scope;
if (s != nullptr) {
for_array(i, s->elements.entries) {
Entity *f = s->elements.entries[i].value;
@@ -502,7 +502,7 @@ bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bo
f->using_parent = e;
}
} else {
error(us->token, "`using` can be only applied to record type entities");
error(us->token, "`using` can be only applied to struct type entities");
}
} break;
@@ -531,7 +531,7 @@ bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bo
Type *t = base_type(type_deref(e->type));
if (is_type_struct(t) || is_type_raw_union(t) || is_type_union(t)) {
// TODO(bill): Make it work for unions too
Scope *found = scope_of_node(&c->info, t->Record.node);
Scope *found = scope_of_node(&c->info, t->Struct.node);
for_array(i, found->elements.entries) {
Entity *f = found->elements.entries[i].value;
if (f->kind == Entity_Variable) {
@@ -1803,7 +1803,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
Type *t = base_type(type_deref(e->type));
if (is_type_struct(t) || is_type_raw_union(t)) {
Scope *scope = scope_of_node(&c->info, t->Record.node);
Scope *scope = scope_of_node(&c->info, t->Struct.node);
for_array(i, scope->elements.entries) {
Entity *f = scope->elements.entries[i].value;
if (f->kind == Entity_Variable) {
+12 -12
View File
@@ -235,7 +235,7 @@ struct Scope {
bool is_global;
bool is_file;
bool is_init;
bool is_record;
bool is_struct;
bool has_been_imported; // This is only applicable to file scopes
AstFile * file;
};
@@ -472,7 +472,7 @@ void check_open_scope(Checker *c, AstNode *node) {
case AstNode_StructType:
case AstNode_EnumType:
case AstNode_UnionType:
scope->is_record = true;
scope->is_struct = true;
break;
}
c->context.scope = scope;
@@ -1174,9 +1174,9 @@ void add_type_info_type(Checker *c, Type *t) {
}
break;
case Type_Record: {
for (isize i = 0; i < bt->Record.field_count; i++) {
Entity *f = bt->Record.fields[i];
case Type_Struct: {
for (isize i = 0; i < bt->Struct.field_count; i++) {
Entity *f = bt->Struct.fields[i];
add_type_info_type(c, f->type);
}
} break;
@@ -1319,7 +1319,7 @@ Entity *find_core_entity(Checker *c, String name) {
return e;
}
Entity *find_sub_core_entity(TypeRecord *parent, String name) {
Entity *find_sub_core_entity(TypeStruct *parent, String name) {
GB_ASSERT(parent->scope->parent->is_global);
Entity *e = current_scope_lookup_entity(parent->scope, name);
if (e == nullptr) {
@@ -1339,16 +1339,16 @@ void init_preload(Checker *c) {
t_type_info = type_info_entity->type;
t_type_info_ptr = make_type_pointer(c->allocator, t_type_info);
GB_ASSERT(is_type_struct(type_info_entity->type));
TypeRecord *record = &base_type(type_info_entity->type)->Record;
TypeStruct *tis = &base_type(type_info_entity->type)->Struct;
Entity *type_info_enum_value = find_sub_core_entity(record, str_lit("EnumValue"));
Entity *type_info_enum_value = find_sub_core_entity(tis, str_lit("EnumValue"));
t_type_info_enum_value = type_info_enum_value->type;
t_type_info_enum_value_ptr = make_type_pointer(c->allocator, t_type_info_enum_value);
GB_ASSERT(record->field_count == 3);
GB_ASSERT(tis->field_count == 3);
Entity *type_info_variant = record->fields_in_src_order[2];
Entity *type_info_variant = tis->fields_in_src_order[2];
Type *tiv_type = type_info_variant->type;
GB_ASSERT(is_type_union(tiv_type));
TypeUnion *tiv = &tiv_type->Union;
@@ -1741,8 +1741,8 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_sco
d->type_expr = init;
d->init_expr = init;
} else if (init->kind == AstNode_ProcLit) {
if (c->context.scope->is_record) {
error(name, "Procedure declarations are not allowed within a record");
if (c->context.scope->is_struct) {
error(name, "Procedure declarations are not allowed within a struct");
continue;
}
ast_node(pl, ProcLit, init);
+59 -59
View File
@@ -661,10 +661,10 @@ bool ir_type_has_default_values(Type *t) {
case Type_Array:
return ir_type_has_default_values(t->Array.elem);
case Type_Record:
if (!t->Record.is_raw_union) {
for (isize i = 0; i < t->Record.field_count; i++) {
Entity *f = t->Record.fields_in_src_order[i];
case Type_Struct:
if (!t->Struct.is_raw_union) {
for (isize i = 0; i < t->Struct.field_count; i++) {
Entity *f = t->Struct.fields_in_src_order[i];
if (f->kind != Entity_Variable) continue;
if (f->Variable.default_is_nil) {
// NOTE(bill): This is technically zero
@@ -2375,9 +2375,9 @@ irValue *ir_emit_struct_ep(irProcedure *proc, irValue *s, i32 index) {
Type *result_type = nullptr;
if (is_type_struct(t)) {
GB_ASSERT(t->Record.field_count > 0);
GB_ASSERT_MSG(gb_is_between(index, 0, t->Record.field_count-1), "0..%d..%d", index, t->Record.field_count);
result_type = make_type_pointer(a, t->Record.fields[index]->type);
GB_ASSERT(t->Struct.field_count > 0);
GB_ASSERT_MSG(gb_is_between(index, 0, t->Struct.field_count-1), "0..%d..%d", index, t->Struct.field_count);
result_type = make_type_pointer(a, t->Struct.fields[index]->type);
} else if (is_type_union(t)) {
GB_ASSERT(index == -1);
return ir_emit_union_tag_ptr(proc, s);
@@ -2416,8 +2416,8 @@ irValue *ir_emit_struct_ep(irProcedure *proc, irValue *s, i32 index) {
} else if (is_type_dynamic_map(t)) {
Type *gst = t->Map.generated_struct_type;
switch (index) {
case 0: result_type = make_type_pointer(a, gst->Record.fields[0]->type); break;
case 1: result_type = make_type_pointer(a, gst->Record.fields[1]->type); break;
case 0: result_type = make_type_pointer(a, gst->Struct.fields[0]->type); break;
case 1: result_type = make_type_pointer(a, gst->Struct.fields[1]->type); break;
}
}else {
GB_PANIC("TODO(bill): struct_gep type: %s, %d", type_to_string(ir_type(s)), index);
@@ -2437,9 +2437,9 @@ irValue *ir_emit_struct_ev(irProcedure *proc, irValue *s, i32 index) {
Type *result_type = nullptr;
if (is_type_struct(t)) {
GB_ASSERT(t->Record.field_count > 0);
GB_ASSERT(gb_is_between(index, 0, t->Record.field_count-1));
result_type = t->Record.fields[index]->type;
GB_ASSERT(t->Struct.field_count > 0);
GB_ASSERT(gb_is_between(index, 0, t->Struct.field_count-1));
result_type = t->Struct.fields[index]->type;
} else if (is_type_union(t)) {
GB_ASSERT(index == -1);
return ir_emit_union_tag_value(proc, s);
@@ -2478,8 +2478,8 @@ irValue *ir_emit_struct_ev(irProcedure *proc, irValue *s, i32 index) {
} else if (is_type_dynamic_map(t)) {
Type *gst = t->Map.generated_struct_type;
switch (index) {
case 0: result_type = gst->Record.fields[0]->type; break;
case 1: result_type = gst->Record.fields[1]->type; break;
case 0: result_type = gst->Struct.fields[0]->type; break;
case 1: result_type = gst->Struct.fields[1]->type; break;
}
} else {
GB_PANIC("TODO(bill): struct_ev type: %s, %d", type_to_string(ir_type(s)), index);
@@ -2506,14 +2506,14 @@ irValue *ir_emit_deep_field_gep(irProcedure *proc, irValue *e, Selection sel) {
if (is_type_raw_union(type)) {
type = type->Record.fields[index]->type;
type = type->Struct.fields[index]->type;
e = ir_emit_conv(proc, e, make_type_pointer(proc->module->allocator, type));
} else if (type->kind == Type_Union) {
GB_ASSERT(index == -1);
type = t_type_info_ptr;
e = ir_emit_struct_ep(proc, e, index);
} else if (type->kind == Type_Record) {
type = type->Record.fields[index]->type;
} else if (type->kind == Type_Struct) {
type = type->Struct.fields[index]->type;
e = ir_emit_struct_ep(proc, e, index);
} else if (type->kind == Type_Tuple) {
type = type->Tuple.variables[index]->type;
@@ -2577,7 +2577,7 @@ irValue *ir_emit_deep_field_ev(irProcedure *proc, irValue *e, Selection sel) {
if (is_type_raw_union(type)) {
GB_PANIC("TODO(bill): IS THIS EVEN CORRECT?");
type = type->Record.fields[index]->type;
type = type->Struct.fields[index]->type;
e = ir_emit_conv(proc, e, type);
} else if (type->kind == Type_Map) {
e = ir_emit_struct_ev(proc, e, 1);
@@ -2755,8 +2755,8 @@ String ir_lookup_subtype_polymorphic_field(CheckerInfo *info, Type *dst, Type *s
// bool dst_is_ptr = dst != prev_dst;
GB_ASSERT(is_type_struct(src) || is_type_union(src));
for (isize i = 0; i < src->Record.field_count; i++) {
Entity *f = src->Record.fields[i];
for (isize i = 0; i < src->Struct.field_count; i++) {
Entity *f = src->Struct.fields[i];
if (f->kind == Entity_Variable && f->flags & EntityFlag_Using) {
if (are_types_identical(dst, f->type)) {
return f->token.string;
@@ -3141,7 +3141,7 @@ bool ir_is_type_aggregate(Type *t) {
case Type_Vector:
case Type_Array:
case Type_Slice:
case Type_Record:
case Type_Struct:
case Type_Tuple:
case Type_DynamicArray:
case Type_Map:
@@ -3226,8 +3226,8 @@ irValue *ir_emit_union_cast(irProcedure *proc, irValue *value, Type *type, Token
irValue *tag = ir_emit_load(proc, ir_emit_union_tag_ptr(proc, value));
irValue *dst_tag = nullptr;
for (isize i = 1; i < src->Record.variant_count; i++) {
Type *vt = src->Record.variants[i];
for (isize i = 1; i < src->Struct.variant_count; i++) {
Type *vt = src->Struct.variants[i];
if (are_types_identical(vt, dst)) {
dst_tag = ir_const_int(a, i);
break;
@@ -3261,8 +3261,8 @@ irValue *ir_emit_union_cast(irProcedure *proc, irValue *value, Type *type, Token
irValue *tag = ir_emit_load(proc, ir_emit_union_tag_ptr(proc, value_));
irValue *dst_tag = nullptr;
for (isize i = 1; i < src->Record.variant_count; i++) {
Type *vt = src->Record.variants[i];
for (isize i = 1; i < src->Struct.variant_count; i++) {
Type *vt = src->Struct.variants[i];
if (are_types_identical(vt, dst)) {
dst_tag = ir_const_int(a, i);
break;
@@ -3682,9 +3682,9 @@ void ir_gen_global_type_name(irModule *m, Entity *e, String name) {
bool is_poly = is_type_polymorphic(bt);
if (!is_poly) {
if (bt->kind == Type_Record &&
bt->Record.is_polymorphic &&
!bt->Record.is_poly_specialized) {
if (bt->kind == Type_Struct &&
bt->Struct.is_polymorphic &&
!bt->Struct.is_poly_specialized) {
is_poly = true;
}
}
@@ -3708,14 +3708,14 @@ void ir_gen_global_type_name(irModule *m, Entity *e, String name) {
if (is_type_union(e->type)) {
Type *bt = base_type(e->type);
// NOTE(bill): Zeroth entry is null (for `match type` stmts)
for (isize j = 1; j < bt->Record.variant_count; j++) {
ir_mangle_add_sub_type_name(m, bt->Record.variants[j], name);
for (isize j = 1; j < bt->Struct.variant_count; j++) {
ir_mangle_add_sub_type_name(m, bt->Struct.variants[j], name);
}
}
#endif
if (bt->kind == Type_Record) {
Scope *s = bt->Record.scope;
if (bt->kind == Type_Struct) {
Scope *s = bt->Struct.scope;
if (s != nullptr) {
for_array(i, s->elements.entries) {
Entity *e = s->elements.entries[i].value;
@@ -3939,9 +3939,9 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv
i32 variant_index = 0;
if (is_type_struct(type)) {
Type *st = base_type(type);
if (st->Record.variant_parent != nullptr) {
allocation_type = st->Record.variant_parent;
variant_index = st->Record.variant_index;
if (st->Struct.variant_parent != nullptr) {
allocation_type = st->Struct.variant_parent;
variant_index = st->Struct.variant_index;
GB_ASSERT(allocation_type != nullptr);
}
}
@@ -4433,12 +4433,12 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv
irValue *s = ir_build_expr(proc, ce->args[0]);
Type *t = base_type(ir_type(s));
GB_ASSERT(t->kind == Type_Record);
GB_ASSERT(t->kind == Type_Struct);
GB_ASSERT(is_type_tuple(tv.type));
irValue *tuple = ir_add_local_generated(proc, tv.type);
for (isize src_index = 0; src_index < t->Record.field_count; src_index++) {
Entity *field = t->Record.fields_in_src_order[src_index];
for (isize src_index = 0; src_index < t->Struct.field_count; src_index++) {
Entity *field = t->Struct.fields_in_src_order[src_index];
i32 field_index = field->Variable.field_index;
irValue *f = ir_emit_struct_ev(proc, s, field_index);
irValue *ep = ir_emit_struct_ep(proc, tuple, src_index);
@@ -5126,9 +5126,9 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) {
if (is_type_enum(type)) {
irValue *enum_info = ir_emit_conv(proc, ti_ptr, t_type_info_enum_ptr);
names_ptr = ir_emit_struct_ep(proc, enum_info, 3);
} else if (type->kind == Type_Record) {
irValue *record_info = ir_emit_conv(proc, ti_ptr, t_type_info_struct_ptr);
names_ptr = ir_emit_struct_ep(proc, record_info, 3);
} else if (type->kind == Type_Struct) {
irValue *struct_info = ir_emit_conv(proc, ti_ptr, t_type_info_struct_ptr);
names_ptr = ir_emit_struct_ep(proc, struct_info, 3);
}
return ir_addr(names_ptr);
} else {
@@ -5497,12 +5497,12 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) {
}
} break;
case Type_Record: {
case Type_Struct: {
// TODO(bill): "constant" unions are not initialized constantly at the moment.
// NOTE(bill): This is due to the layout of the unions when printed to LLVM-IR
bool is_union = is_type_union(bt);
GB_ASSERT(is_type_struct(bt) || is_type_union(bt));
TypeRecord *st = &bt->Record;
TypeStruct *st = &bt->Struct;
if (cl->elems.count > 0) {
ir_emit_store(proc, v, ir_add_module_constant(proc->module, type, exact_value_compound(expr)));
for_array(field_index, cl->elems) {
@@ -5822,8 +5822,8 @@ void ir_build_constant_value_decl(irProcedure *proc, AstNodeValueDecl *vd) {
bool polymorphic_struct = false;
if (e->type != nullptr && e->kind == Entity_TypeName) {
Type *bt = base_type(e->type);
if (bt->kind == Type_Record) {
polymorphic_struct = bt->Record.is_polymorphic;
if (bt->kind == Type_Struct) {
polymorphic_struct = bt->Struct.is_polymorphic;
}
}
@@ -6626,7 +6626,7 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) {
Type *enum_ptr = make_type_pointer(a, t);
t = base_type(t);
Type *core_elem = core_type(t);
i64 enum_count = t->Record.field_count;
i64 enum_count = t->Struct.field_count;
irValue *max_count = ir_const_int(a, enum_count);
irValue *eti = ir_emit_union_cast(proc, ir_type_info(proc, t), t_type_info_enum_ptr, pos);
@@ -7340,8 +7340,8 @@ void ir_init_module(irModule *m, Checker *c) {
case Type_Union:
count += t->Union.variants.count;
break;
case Type_Record:
count += t->Record.field_count;
case Type_Struct:
count += t->Struct.field_count;
break;
case Type_Tuple:
count += t->Tuple.variables.count;
@@ -7580,8 +7580,8 @@ void ir_gen_tree(irGen *s) {
bool polymorphic_struct = false;
if (e->type != nullptr && e->kind == Entity_TypeName) {
Type *bt = base_type(e->type);
if (bt->kind == Type_Record) {
polymorphic_struct = bt->Record.is_polymorphic;
if (bt->kind == Type_Struct) {
polymorphic_struct = bt->Struct.is_polymorphic;
}
}
@@ -8169,22 +8169,22 @@ void ir_gen_tree(irGen *s) {
} break;
case Type_Record: {
case Type_Struct: {
ir_emit_comment(proc, str_lit("TypeInfoStruct"));
tag = ir_emit_conv(proc, variant_ptr, t_type_info_struct_ptr);
{
irValue *is_packed = ir_const_bool(a, t->Record.is_packed);
irValue *is_ordered = ir_const_bool(a, t->Record.is_ordered);
irValue *is_raw_union = ir_const_bool(a, t->Record.is_raw_union);
irValue *is_custom_align = ir_const_bool(a, t->Record.custom_align != 0);
irValue *is_packed = ir_const_bool(a, t->Struct.is_packed);
irValue *is_ordered = ir_const_bool(a, t->Struct.is_ordered);
irValue *is_raw_union = ir_const_bool(a, t->Struct.is_raw_union);
irValue *is_custom_align = ir_const_bool(a, t->Struct.custom_align != 0);
ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 4), is_packed);
ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 5), is_ordered);
ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 6), is_raw_union);
ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 7), is_custom_align);
}
i32 count = t->Record.field_count;
i32 count = t->Struct.field_count;
irValue *memory_types = ir_type_info_member_types_offset (proc, count);
irValue *memory_names = ir_type_info_member_names_offset (proc, count);
@@ -8194,11 +8194,11 @@ void ir_gen_tree(irGen *s) {
type_set_offsets(a, t); // NOTE(bill): Just incase the offsets have not been set yet
for (isize source_index = 0; source_index < count; source_index++) {
// TODO(bill): Order fields in source order not layout order
Entity *f = t->Record.fields_in_src_order[source_index];
Entity *f = t->Struct.fields_in_src_order[source_index];
irValue *tip = ir_get_type_info_ptr(proc, f->type);
i64 foffset = 0;
if (!t->Record.is_raw_union) {
foffset = t->Record.offsets[f->Variable.field_index];
if (!t->Struct.is_raw_union) {
foffset = t->Struct.offsets[f->Variable.field_index];
}
GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field);
+26 -26
View File
@@ -299,8 +299,8 @@ void ir_print_type(irFileBuffer *f, irModule *m, Type *t) {
#endif
} return;
case Type_Record: {
if (t->Record.is_raw_union) {
case Type_Struct: {
if (t->Struct.is_raw_union) {
// NOTE(bill): The zero size array is used to fix the alignment used in a structure as
// LLVM takes the first element's alignment as the entire alignment (like C)
i64 size_of_union = type_size_of(heap_allocator(), t);
@@ -308,24 +308,24 @@ void ir_print_type(irFileBuffer *f, irModule *m, Type *t) {
ir_fprintf(f, "{[0 x <%lld x i8>], [%lld x i8]}", align_of_union, size_of_union);
return;
} else {
if (t->Record.is_packed) {
if (t->Struct.is_packed) {
ir_fprintf(f, "<");
}
ir_fprintf(f, "{");
if (t->Record.custom_align > 0) {
ir_fprintf(f, "[0 x <%lld x i8>]", t->Record.custom_align);
if (t->Record.field_count > 0) {
if (t->Struct.custom_align > 0) {
ir_fprintf(f, "[0 x <%lld x i8>]", t->Struct.custom_align);
if (t->Struct.field_count > 0) {
ir_fprintf(f, ", ");
}
}
for (isize i = 0; i < t->Record.field_count; i++) {
for (isize i = 0; i < t->Struct.field_count; i++) {
if (i > 0) {
ir_fprintf(f, ", ");
}
ir_print_type(f, m, t->Record.fields[i]->type);
ir_print_type(f, m, t->Struct.fields[i]->type);
}
ir_fprintf(f, "}");
if (t->Record.is_packed) {
if (t->Struct.is_packed) {
ir_fprintf(f, ">");
}
return;
@@ -585,7 +585,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
}
isize value_count = type->Record.field_count;
isize value_count = type->Struct.field_count;
ExactValue *values = gb_alloc_array(m->tmp_allocator, ExactValue, value_count);
bool *visited = gb_alloc_array(m->tmp_allocator, bool, value_count);
@@ -600,14 +600,14 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
GB_ASSERT(tav.mode != Addressing_Invalid);
Selection sel = lookup_field(m->allocator, type, name, false);
Entity *f = type->Record.fields[sel.index[0]];
Entity *f = type->Struct.fields[sel.index[0]];
values[f->Variable.field_index] = tav.value;
visited[f->Variable.field_index] = true;
}
} else {
for_array(i, cl->elems) {
Entity *f = type->Record.fields_in_src_order[i];
Entity *f = type->Struct.fields_in_src_order[i];
TypeAndValue tav = type_and_value_of_expr(m->info, cl->elems[i]);
ExactValue val = {};
if (tav.mode != Addressing_Invalid) {
@@ -621,7 +621,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
for (isize i = 0; i < value_count; i++) {
if (visited[i]) continue;
Entity *f = type->Record.fields[i];
Entity *f = type->Struct.fields[i];
ExactValue v = {};
if (!f->Variable.default_is_nil) {
v = f->Variable.default_value;
@@ -631,10 +631,10 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
if (type->Record.is_packed) ir_fprintf(f, "<");
if (type->Struct.is_packed) ir_fprintf(f, "<");
ir_fprintf(f, "{");
if (type->Record.custom_align > 0) {
ir_fprintf(f, "[0 x <%lld x i8>] zeroinitializer", cast(i64)type->Record.custom_align);
if (type->Struct.custom_align > 0) {
ir_fprintf(f, "[0 x <%lld x i8>] zeroinitializer", cast(i64)type->Struct.custom_align);
if (value_count > 0) {
ir_fprintf(f, ", ");
}
@@ -643,7 +643,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
for (isize i = 0; i < value_count; i++) {
if (i > 0) ir_fprintf(f, ", ");
Entity *e = type->Record.fields[i];
Entity *e = type->Struct.fields[i];
if (!visited[i] && e->Variable.default_is_undef) {
ir_print_type(f, m, e->type);
@@ -655,7 +655,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
ir_fprintf(f, "}");
if (type->Record.is_packed) ir_fprintf(f, ">");
if (type->Struct.is_packed) ir_fprintf(f, ">");
gb_temp_arena_memory_end(tmp);
} else {
@@ -670,11 +670,11 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
ir_fprintf(f, "zeroinitializer");
} else {
if (is_type_struct(type)) {
i32 value_count = type->Record.field_count;
if (type->Record.is_packed) ir_fprintf(f, "<");
i32 value_count = type->Struct.field_count;
if (type->Struct.is_packed) ir_fprintf(f, "<");
ir_fprintf(f, "{");
if (type->Record.custom_align > 0) {
ir_fprintf(f, "[0 x <%lld x i8>] zeroinitializer", cast(i64)type->Record.custom_align);
if (type->Struct.custom_align > 0) {
ir_fprintf(f, "[0 x <%lld x i8>] zeroinitializer", cast(i64)type->Struct.custom_align);
if (value_count > 0) {
ir_fprintf(f, ", ");
}
@@ -682,7 +682,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
for (isize i = 0; i < value_count; i++) {
if (i > 0) ir_fprintf(f, ", ");
Entity *e = type->Record.fields[i];
Entity *e = type->Struct.fields[i];
if (e->Variable.default_is_undef) {
ir_print_type(f, m, e->type);
ir_fprintf(f, " undef");
@@ -696,7 +696,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
}
ir_fprintf(f, "}");
if (type->Record.is_packed) ir_fprintf(f, ">");
if (type->Struct.is_packed) ir_fprintf(f, ">");
} else if (is_type_array(type)) {
i64 count = type->Array.count;
@@ -941,7 +941,7 @@ void ir_print_instr(irFileBuffer *f, irModule *m, irValue *value) {
i32 index = instr->StructElementPtr.elem_index;
Type *st = base_type(type_deref(et));
if (is_type_struct(st)) {
if (st->Record.custom_align > 0) {
if (st->Struct.custom_align > 0) {
index += 1;
}
} else if (is_type_union(st)) {
@@ -1011,7 +1011,7 @@ void ir_print_instr(irFileBuffer *f, irModule *m, irValue *value) {
i32 index = instr->StructExtractValue.index;
Type *st = base_type(et);
if (is_type_struct(st)) {
if (st->Record.custom_align > 0) {
if (st->Struct.custom_align > 0) {
index += 1;
}
} else if (is_type_union(st)) {
+5 -5
View File
@@ -2156,7 +2156,7 @@ Array<AstNode *> parse_rhs_expr_list (AstFile *f);
AstNode * parse_simple_stmt (AstFile *f, StmtAllowFlag flags);
AstNode * parse_type (AstFile *f);
AstNode * parse_call_expr (AstFile *f, AstNode *operand);
AstNode * parse_record_field_list(AstFile *f, isize *name_count_);
AstNode * parse_struct_field_list(AstFile *f, isize *name_count_);
AstNode *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, TokenKind follow, bool allow_default_parameters, bool allow_type_token);
@@ -2477,7 +2477,7 @@ AstNode *parse_operand(AstFile *f, bool lhs) {
Token open = expect_token_after(f, Token_OpenBrace, "struct");
isize name_count = 0;
AstNode *fields = parse_record_field_list(f, &name_count);
AstNode *fields = parse_struct_field_list(f, &name_count);
Token close = expect_token(f, Token_CloseBrace);
Array<AstNode *> decls = {};
@@ -3632,7 +3632,7 @@ bool parse_expect_struct_separator(AstFile *f, AstNode *param) {
}
AstNode *parse_record_field_list(AstFile *f, isize *name_count_) {
AstNode *parse_struct_field_list(AstFile *f, isize *name_count_) {
CommentGroup docs = f->lead_comment;
Token start_token = f->curr_token;
@@ -3949,7 +3949,7 @@ AstNode *parse_type_or_ident(AstFile *f) {
Token open = expect_token_after(f, Token_OpenBrace, "struct");
isize name_count = 0;
AstNode *fields = parse_record_field_list(f, &name_count);
AstNode *fields = parse_struct_field_list(f, &name_count);
Token close = expect_token(f, Token_CloseBrace);
Array<AstNode *> decls = {};
@@ -3992,7 +3992,7 @@ AstNode *parse_type_or_ident(AstFile *f) {
Token open = expect_token_after(f, Token_OpenBrace, "raw_union");
isize decl_count = 0;
AstNode *fields = parse_record_field_list(f, &decl_count);
AstNode *fields = parse_struct_field_list(f, &decl_count);
Token close = expect_token(f, Token_CloseBrace);
Array<AstNode *> decls = {};
+26 -26
View File
@@ -651,13 +651,13 @@ bool can_ssa_type(Type *t) {
case Type_Union:
return false;
case Type_Record:
if (!t->Record.is_raw_union) {
if (t->Record.field_count > SSA_MAX_STRUCT_FIELD_COUNT) {
case Type_Struct:
if (!t->Struct.is_raw_union) {
if (t->Struct.field_count > SSA_MAX_STRUCT_FIELD_COUNT) {
return false;
}
for (isize i = 0; i < t->Record.field_count; i++) {
if (!can_ssa_type(t->Record.fields[i]->type)) {
for (isize i = 0; i < t->Struct.field_count; i++) {
if (!can_ssa_type(t->Struct.fields[i]->type)) {
return false;
}
}
@@ -810,9 +810,9 @@ ssaValue *ssa_emit_ptr_index(ssaProc *p, ssaValue *s, i64 index) {
Type *result_type = nullptr;
if (is_type_struct(t)) {
GB_ASSERT(t->Record.field_count > 0);
GB_ASSERT(gb_is_between(index, 0, t->Record.field_count-1));
result_type = make_type_pointer(a, t->Record.fields[index]->type);
GB_ASSERT(t->Struct.field_count > 0);
GB_ASSERT(gb_is_between(index, 0, t->Struct.field_count-1));
result_type = make_type_pointer(a, t->Struct.fields[index]->type);
} else if (is_type_tuple(t)) {
GB_ASSERT(t->Tuple.variables.count > 0);
GB_ASSERT(gb_is_between(index, 0, t->Tuple.variables.count-1));
@@ -843,8 +843,8 @@ ssaValue *ssa_emit_ptr_index(ssaProc *p, ssaValue *s, i64 index) {
} else if (is_type_dynamic_map(t)) {
Type *gst = t->Map.generated_struct_type;
switch (index) {
case 0: result_type = make_type_pointer(a, gst->Record.fields[0]->type); break;
case 1: result_type = make_type_pointer(a, gst->Record.fields[1]->type); break;
case 0: result_type = make_type_pointer(a, gst->Struct.fields[0]->type); break;
case 1: result_type = make_type_pointer(a, gst->Struct.fields[1]->type); break;
}
}else {
GB_PANIC("TODO(bill): ssa_emit_ptr_index type: %s, %d", type_to_string(s->type), index);
@@ -868,14 +868,14 @@ ssaValue *ssa_emit_value_index(ssaProc *p, ssaValue *s, i64 index) {
Type *result_type = nullptr;
if (is_type_struct(t)) {
GB_ASSERT(t->Record.field_count > 0);
GB_ASSERT(gb_is_between(index, 0, t->Record.field_count-1));
result_type = t->Record.fields[index]->type;
GB_ASSERT(t->Struct.field_count > 0);
GB_ASSERT(gb_is_between(index, 0, t->Struct.field_count-1));
result_type = t->Struct.fields[index]->type;
} else if (is_type_union(t)) {
type_set_offsets(a, t);
GB_ASSERT(t->Record.field_count > 0);
GB_ASSERT(gb_is_between(index, 0, t->Record.field_count-1));
result_type = t->Record.fields[index]->type;
GB_ASSERT(t->Struct.field_count > 0);
GB_ASSERT(gb_is_between(index, 0, t->Struct.field_count-1));
result_type = t->Struct.fields[index]->type;
} else if (is_type_tuple(t)) {
GB_ASSERT(t->Tuple.variables.count > 0);
result_type = t->Tuple.variables[index]->type;
@@ -905,8 +905,8 @@ ssaValue *ssa_emit_value_index(ssaProc *p, ssaValue *s, i64 index) {
} else if (is_type_dynamic_map(t)) {
Type *gst = t->Map.generated_struct_type;
switch (index) {
case 0: result_type = gst->Record.fields[0]->type; break;
case 1: result_type = gst->Record.fields[1]->type; break;
case 0: result_type = gst->Struct.fields[0]->type; break;
case 1: result_type = gst->Struct.fields[1]->type; break;
}
} else {
GB_PANIC("TODO(bill): struct_ev type: %s, %d", type_to_string(s->type), index);
@@ -932,10 +932,10 @@ ssaValue *ssa_emit_deep_field_ptr_index(ssaProc *p, ssaValue *e, Selection sel)
if (is_type_raw_union(type)) {
type = type->Record.fields[index]->type;
type = type->Struct.fields[index]->type;
e = ssa_emit_conv(p, e, make_type_pointer(p->allocator, type));
} else if (type->kind == Type_Record) {
type = type->Record.fields[index]->type;
} else if (type->kind == Type_Struct) {
type = type->Struct.fields[index]->type;
e = ssa_emit_ptr_index(p, e, index);
} else if (type->kind == Type_Tuple) {
type = type->Tuple.variables[index]->type;
@@ -1003,7 +1003,7 @@ ssaValue *ssa_emit_deep_field_value_index(ssaProc *p, ssaValue *e, Selection sel
if (is_type_raw_union(type)) {
GB_PANIC("TODO(bill): IS THIS EVEN CORRECT?");
type = type->Record.fields[index]->type;
type = type->Struct.fields[index]->type;
e = ssa_emit_conv(p, e, type);
} else if (type->kind == Type_Map) {
e = ssa_emit_value_index(p, e, 1);
@@ -1072,9 +1072,9 @@ ssaAddr ssa_build_addr(ssaProc *p, AstNode *expr) {
// if (is_type_enum(type)) {
// ssaValue *enum_info = ssa_emit_conv(p, ti_ptr, t_type_info_enum_ptr);
// names_ptr = ssa_emit_ptr_index(p, enum_info, 1);
// } else if (type->kind == Type_Record) {
// ssaValue *record_info = ssa_emit_conv(p, ti_ptr, t_type_info_record_ptr);
// names_ptr = ssa_emit_ptr_index(p, record_info, 1);
// } else if (type->kind == Type_Struct) {
// ssaValue *struct_info = ssa_emit_conv(p, ti_ptr, t_type_info_struct_ptr);
// names_ptr = ssa_emit_ptr_index(p, struct_info, 1);
// }
// return ssa_addr(names_ptr);
// } else {
@@ -1658,7 +1658,7 @@ ssaValue *ssa_build_expr(ssaProc *p, AstNode *expr) {
default: GB_PANIC("Unknown float size");
}
}
// IMPORTANT TODO(bill): Do constant record/array literals correctly
// IMPORTANT TODO(bill): Do constant struct/array literals correctly
return ssa_const_nil(p, tv.type);
}
+75 -79
View File
@@ -68,11 +68,7 @@ struct BasicType {
String name;
};
struct TypeRecord {
// All record types
// Theses are arrays
// Entity_Variable - struct/raw_union (for common fields)
struct TypeStruct {
Entity **fields;
i32 field_count; // == struct_offsets count
Entity **fields_in_src_order; // Entity_Variable
@@ -106,7 +102,7 @@ struct TypeRecord {
TYPE_KIND(DynamicArray, struct { Type *elem; }) \
TYPE_KIND(Vector, struct { Type *elem; i64 count; }) \
TYPE_KIND(Slice, struct { Type *elem; }) \
TYPE_KIND(Record, TypeRecord) \
TYPE_KIND(Struct, TypeStruct) \
TYPE_KIND(Enum, struct { \
Entity **fields; \
i32 field_count; \
@@ -507,7 +503,7 @@ Type *make_type_slice(gbAllocator a, Type *elem) {
Type *make_type_struct(gbAllocator a) {
Type *t = alloc_type(a, Type_Record);
Type *t = alloc_type(a, Type_Struct);
return t;
}
@@ -825,7 +821,7 @@ Type *base_complex_elem_type(Type *t) {
bool is_type_struct(Type *t) {
t = base_type(t);
return (t->kind == Type_Record && !t->Record.is_raw_union);
return (t->kind == Type_Struct && !t->Struct.is_raw_union);
}
bool is_type_union(Type *t) {
t = base_type(t);
@@ -834,7 +830,7 @@ bool is_type_union(Type *t) {
bool is_type_raw_union(Type *t) {
t = base_type(t);
return (t->kind == Type_Record && t->Record.is_raw_union);
return (t->kind == Type_Struct && t->Struct.is_raw_union);
}
bool is_type_enum(Type *t) {
t = base_type(t);
@@ -910,16 +906,16 @@ bool is_type_indexable(Type *t) {
bool is_type_polymorphic_struct(Type *t) {
t = base_type(t);
if (t->kind == Type_Record) {
return t->Record.is_polymorphic;
if (t->kind == Type_Struct) {
return t->Struct.is_polymorphic;
}
return false;
}
bool is_type_polymorphic_struct_specialized(Type *t) {
t = base_type(t);
if (t->kind == Type_Record) {
return t->Record.is_polymorphic && t->Record.is_poly_specialized;
if (t->kind == Type_Struct) {
return t->Struct.is_polymorphic && t->Struct.is_poly_specialized;
}
return false;
}
@@ -983,12 +979,12 @@ bool is_type_polymorphic(Type *t) {
}
}
break;
case Type_Record:
if (t->Record.is_polymorphic) {
case Type_Struct:
if (t->Struct.is_polymorphic) {
return true;
}
for (isize i = 0; i < t->Record.field_count; i++) {
if (is_type_polymorphic(t->Record.fields[i]->type)) {
for (isize i = 0; i < t->Struct.field_count; i++) {
if (is_type_polymorphic(t->Struct.fields[i]->type)) {
return true;
}
}
@@ -1033,7 +1029,7 @@ bool type_has_nil(Type *t) {
return true;
case Type_Union:
return true;
case Type_Record:
case Type_Struct:
return false;
}
return false;
@@ -1132,17 +1128,17 @@ bool are_types_identical(Type *x, Type *y) {
}
break;
case Type_Record:
if (y->kind == Type_Record) {
if (x->Record.is_raw_union == y->Record.is_raw_union &&
x->Record.field_count == y->Record.field_count &&
x->Record.is_packed == y->Record.is_packed &&
x->Record.is_ordered == y->Record.is_ordered &&
x->Record.custom_align == y->Record.custom_align) {
case Type_Struct:
if (y->kind == Type_Struct) {
if (x->Struct.is_raw_union == y->Struct.is_raw_union &&
x->Struct.field_count == y->Struct.field_count &&
x->Struct.is_packed == y->Struct.is_packed &&
x->Struct.is_ordered == y->Struct.is_ordered &&
x->Struct.custom_align == y->Struct.custom_align) {
// TODO(bill); Fix the custom alignment rule
for (isize i = 0; i < x->Record.field_count; i++) {
Entity *xf = x->Record.fields[i];
Entity *yf = y->Record.fields[i];
for (isize i = 0; i < x->Struct.field_count; i++) {
Entity *xf = x->Struct.fields[i];
Entity *yf = y->Struct.fields[i];
if (!are_types_identical(xf->type, yf->type)) {
return false;
}
@@ -1279,12 +1275,12 @@ bool is_type_cte_safe(Type *type) {
case Type_Slice:
return false;
case Type_Record: {
if (type->Record.is_raw_union) {
case Type_Struct: {
if (type->Struct.is_raw_union) {
return false;
}
for (isize i = 0; i < type->Record.field_count; i++) {
Entity *v = type->Record.fields[i];
for (isize i = 0; i < type->Struct.field_count; i++) {
Entity *v = type->Struct.fields[i];
if (!is_type_cte_safe(v->type)) {
return false;
}
@@ -1405,7 +1401,7 @@ Selection lookup_field_from_index(gbAllocator a, Type *type, i64 index) {
i64 max_count = 0;
switch (type->kind) {
case Type_Record: max_count = type->Record.field_count; break;
case Type_Struct: max_count = type->Struct.field_count; break;
case Type_Tuple: max_count = type->Tuple.variables.count; break;
case Type_BitField: max_count = type->BitField.field_count; break;
}
@@ -1415,9 +1411,9 @@ Selection lookup_field_from_index(gbAllocator a, Type *type, i64 index) {
}
switch (type->kind) {
case Type_Record:
case Type_Struct:
for (isize i = 0; i < max_count; i++) {
Entity *f = type->Record.fields[i];
Entity *f = type->Struct.fields[i];
if (f->kind == Entity_Variable) {
if (f->Variable.field_src_index == index) {
Array<i32> sel_array = {0};
@@ -1527,10 +1523,10 @@ Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_n
if (is_type) {
switch (type->kind) {
case Type_Record:
if (type->Record.names != nullptr &&
case Type_Struct:
if (type->Struct.names != nullptr &&
field_name == "names") {
sel.entity = type->Record.names;
sel.entity = type->Struct.names;
return sel;
}
break;
@@ -1574,8 +1570,8 @@ Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_n
}
}
if (type->kind == Type_Record) {
Scope *s = type->Record.scope;
if (type->kind == Type_Struct) {
Scope *s = type->Struct.scope;
if (s != nullptr) {
Entity *found = current_scope_lookup_entity(s, field_name);
if (found != nullptr && found->kind != Entity_Variable) {
@@ -1604,9 +1600,9 @@ Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_n
return sel;
}
} else if (type->kind == Type_Record) {
for (isize i = 0; i < type->Record.field_count; i++) {
Entity *f = type->Record.fields[i];
} else if (type->kind == Type_Struct) {
for (isize i = 0; i < type->Struct.field_count; i++) {
Entity *f = type->Struct.fields[i];
if (f->kind != Entity_Variable || (f->flags & EntityFlag_Field) == 0) {
continue;
}
@@ -1843,11 +1839,11 @@ i64 type_align_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
return max;
} break;
case Type_Record: {
if (t->Record.is_raw_union) {
case Type_Struct: {
if (t->Struct.is_raw_union) {
i64 max = 1;
for (isize i = 0; i < t->Record.field_count; i++) {
Type *field_type = t->Record.fields[i]->type;
for (isize i = 0; i < t->Struct.field_count; i++) {
Type *field_type = t->Struct.fields[i]->type;
type_path_push(path, field_type);
if (path->failure) {
return FAILURE_ALIGNMENT;
@@ -1860,16 +1856,16 @@ i64 type_align_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
}
return max;
} else {
if (t->Record.custom_align > 0) {
return gb_clamp(t->Record.custom_align, 1, build_context.max_align);
if (t->Struct.custom_align > 0) {
return gb_clamp(t->Struct.custom_align, 1, build_context.max_align);
}
if (t->Record.field_count > 0) {
if (t->Struct.field_count > 0) {
i64 max = 1;
if (t->Record.is_packed) {
if (t->Struct.is_packed) {
max = build_context.word_size;
}
for (isize i = 0; i < t->Record.field_count; i++) {
Type *field_type = t->Record.fields[i]->type;
for (isize i = 0; i < t->Struct.field_count; i++) {
Type *field_type = t->Struct.fields[i]->type;
type_path_push(path, field_type);
if (path->failure) {
return FAILURE_ALIGNMENT;
@@ -1927,16 +1923,16 @@ i64 *type_set_offsets_of(gbAllocator allocator, Entity **fields, isize field_cou
bool type_set_offsets(gbAllocator allocator, Type *t) {
t = base_type(t);
if (t->kind == Type_Record) {
if (!t->Record.are_offsets_set) {
t->Record.are_offsets_being_processed = true;
t->Record.offsets = type_set_offsets_of(allocator, t->Record.fields, t->Record.field_count, t->Record.is_packed, t->Record.is_raw_union);
t->Record.are_offsets_set = true;
if (t->kind == Type_Struct) {
if (!t->Struct.are_offsets_set) {
t->Struct.are_offsets_being_processed = true;
t->Struct.offsets = type_set_offsets_of(allocator, t->Struct.fields, t->Struct.field_count, t->Struct.is_packed, t->Struct.is_raw_union);
t->Struct.are_offsets_set = true;
return true;
}
} else if (is_type_tuple(t)) {
if (!t->Tuple.are_offsets_set) {
t->Record.are_offsets_being_processed = true;
t->Struct.are_offsets_being_processed = true;
t->Tuple.offsets = type_set_offsets_of(allocator, t->Tuple.variables.data, t->Tuple.variables.count, false, false);
t->Tuple.are_offsets_set = true;
return true;
@@ -2088,16 +2084,16 @@ i64 type_size_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
} break;
case Type_Record: {
if (t->Record.is_raw_union) {
i64 count = t->Record.field_count;
case Type_Struct: {
if (t->Struct.is_raw_union) {
i64 count = t->Struct.field_count;
i64 align = type_align_of_internal(allocator, t, path);
if (path->failure) {
return FAILURE_SIZE;
}
i64 max = 0;
for (isize i = 0; i < count; i++) {
i64 size = type_size_of_internal(allocator, t->Record.fields[i]->type, path);
i64 size = type_size_of_internal(allocator, t->Struct.fields[i]->type, path);
if (max < size) {
max = size;
}
@@ -2105,7 +2101,7 @@ i64 type_size_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
// TODO(bill): Is this how it should work?
return align_formula(max, align);
} else {
i64 count = t->Record.field_count;
i64 count = t->Struct.field_count;
if (count == 0) {
return 0;
}
@@ -2113,12 +2109,12 @@ i64 type_size_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
if (path->failure) {
return FAILURE_SIZE;
}
if (t->Record.are_offsets_being_processed && t->Record.offsets == nullptr) {
if (t->Struct.are_offsets_being_processed && t->Struct.offsets == nullptr) {
type_path_print_illegal_cycle(path, path->path.count-1);
return FAILURE_SIZE;
}
type_set_offsets(allocator, t);
i64 size = t->Record.offsets[count-1] + type_size_of_internal(allocator, t->Record.fields[count-1]->type, path);
i64 size = t->Struct.offsets[count-1] + type_size_of_internal(allocator, t->Struct.fields[count-1]->type, path);
return align_formula(size, align);
}
} break;
@@ -2142,10 +2138,10 @@ i64 type_size_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
i64 type_offset_of(gbAllocator allocator, Type *t, i32 index) {
t = base_type(t);
if (t->kind == Type_Record && !t->Record.is_raw_union) {
if (t->kind == Type_Struct && !t->Struct.is_raw_union) {
type_set_offsets(allocator, t);
if (gb_is_between(index, 0, t->Record.field_count-1)) {
return t->Record.offsets[index];
if (gb_is_between(index, 0, t->Struct.field_count-1)) {
return t->Struct.offsets[index];
}
} else if (t->kind == Type_Tuple) {
type_set_offsets(allocator, t);
@@ -2191,8 +2187,8 @@ i64 type_offset_of_from_selection(gbAllocator allocator, Type *type, Selection s
isize index = sel.index[i];
t = base_type(t);
offset += type_offset_of(allocator, t, index);
if (t->kind == Type_Record && !t->Record.is_raw_union) {
t = t->Record.fields[index]->type;
if (t->kind == Type_Struct && !t->Struct.is_raw_union) {
t = t->Struct.fields[index]->type;
} else {
// NOTE(bill): No need to worry about custom types, just need the alignment
switch (t->kind) {
@@ -2308,11 +2304,11 @@ gbString write_type_to_string(gbString str, Type *type) {
str = gb_string_appendc(str, "}");
break;
case Type_Record: {
if (type->Record.is_raw_union) {
case Type_Struct: {
if (type->Struct.is_raw_union) {
str = gb_string_appendc(str, "raw_union{");
for (isize i = 0; i < type->Record.field_count; i++) {
Entity *f = type->Record.fields[i];
for (isize i = 0; i < type->Struct.field_count; i++) {
Entity *f = type->Struct.fields[i];
GB_ASSERT(f->kind == Entity_Variable);
if (i > 0) {
str = gb_string_appendc(str, ", ");
@@ -2324,15 +2320,15 @@ gbString write_type_to_string(gbString str, Type *type) {
str = gb_string_appendc(str, "}");
} else {
str = gb_string_appendc(str, "struct");
if (type->Record.is_packed) {
if (type->Struct.is_packed) {
str = gb_string_appendc(str, " #packed");
}
if (type->Record.is_ordered) {
if (type->Struct.is_ordered) {
str = gb_string_appendc(str, " #ordered");
}
str = gb_string_appendc(str, " {");
for (isize i = 0; i < type->Record.field_count; i++) {
Entity *f = type->Record.fields[i];
for (isize i = 0; i < type->Struct.field_count; i++) {
Entity *f = type->Struct.fields[i];
GB_ASSERT(f->kind == Entity_Variable);
if (i > 0) {
str = gb_string_appendc(str, ", ");