mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 22:58:46 +00:00
Untagged (unsafe) unions and unambiguous in|postfix notation.
This commit is contained in:
@@ -337,12 +337,12 @@ void init_universal_scope(void) {
|
||||
// Types
|
||||
for (isize i = 0; i < gb_count_of(basic_types); i++) {
|
||||
Token token = {Token_Identifier};
|
||||
token.string = basic_types[i].basic.name;
|
||||
token.string = basic_types[i].Basic.name;
|
||||
add_global_entity(make_entity_type_name(a, NULL, token, &basic_types[i]));
|
||||
}
|
||||
for (isize i = 0; i < gb_count_of(basic_type_aliases); i++) {
|
||||
Token token = {Token_Identifier};
|
||||
token.string = basic_type_aliases[i].basic.name;
|
||||
token.string = basic_type_aliases[i].Basic.name;
|
||||
add_global_entity(make_entity_type_name(a, NULL, token, &basic_type_aliases[i]));
|
||||
}
|
||||
|
||||
|
||||
+140
-77
@@ -16,7 +16,7 @@ void update_expr_type (Checker *c, AstNode *e, Type *type, b32
|
||||
|
||||
void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
|
||||
GB_ASSERT(node->kind == AstNode_StructType);
|
||||
GB_ASSERT(struct_type->kind == Type_Structure);
|
||||
GB_ASSERT(struct_type->kind == Type_Struct);
|
||||
ast_node(st, StructType, node);
|
||||
|
||||
Map<Entity *> entity_map = {};
|
||||
@@ -52,14 +52,57 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
|
||||
add_entity_use(&c->info, name, e);
|
||||
}
|
||||
}
|
||||
struct_type->structure.fields = fields;
|
||||
struct_type->structure.field_count = field_count;
|
||||
struct_type->structure.is_packed = st->is_packed;
|
||||
struct_type->Struct.fields = fields;
|
||||
struct_type->Struct.field_count = field_count;
|
||||
struct_type->Struct.is_packed = st->is_packed;
|
||||
}
|
||||
|
||||
void check_union_type(Checker *c, Type *union_type, AstNode *node) {
|
||||
GB_ASSERT(node->kind == AstNode_UnionType);
|
||||
GB_ASSERT(union_type->kind == Type_Union);
|
||||
ast_node(ut, UnionType, node);
|
||||
|
||||
Map<Entity *> entity_map = {};
|
||||
map_init(&entity_map, gb_heap_allocator());
|
||||
defer (map_destroy(&entity_map));
|
||||
|
||||
isize field_count = 0;
|
||||
for (AstNode *field = ut->field_list; field != NULL; field = field->next) {
|
||||
for (AstNode *name = field->Field.name_list; name != NULL; name = name->next) {
|
||||
GB_ASSERT(name->kind == AstNode_Ident);
|
||||
field_count++;
|
||||
}
|
||||
}
|
||||
|
||||
Entity **fields = gb_alloc_array(c->allocator, Entity *, ut->field_count);
|
||||
isize field_index = 0;
|
||||
for (AstNode *field = ut->field_list; field != NULL; field = field->next) {
|
||||
ast_node(f, Field, field);
|
||||
Type *type = check_type(c, f->type);
|
||||
for (AstNode *name = f->name_list; name != NULL; name = name->next) {
|
||||
ast_node(i, Ident, name);
|
||||
Token name_token = i->token;
|
||||
// TODO(bill): is the curr_scope correct?
|
||||
Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type);
|
||||
HashKey key = hash_string(name_token.string);
|
||||
if (map_get(&entity_map, key)) {
|
||||
// TODO(bill): Scope checking already checks the declaration
|
||||
error(&c->error_collector, name_token, "`%.*s` is already declared in this union", LIT(name_token.string));
|
||||
} else {
|
||||
map_set(&entity_map, key, e);
|
||||
fields[field_index++] = e;
|
||||
}
|
||||
add_entity_use(&c->info, name, e);
|
||||
}
|
||||
}
|
||||
union_type->Union.fields = fields;
|
||||
union_type->Union.field_count = field_count;
|
||||
}
|
||||
|
||||
|
||||
void check_enum_type(Checker *c, Type *enum_type, AstNode *node) {
|
||||
GB_ASSERT(node->kind == AstNode_EnumType);
|
||||
GB_ASSERT(enum_type->kind == Type_Enumeration);
|
||||
GB_ASSERT(enum_type->kind == Type_Enum);
|
||||
ast_node(et, EnumType, node);
|
||||
|
||||
Map<Entity *> entity_map = {};
|
||||
@@ -78,7 +121,7 @@ void check_enum_type(Checker *c, Type *enum_type, AstNode *node) {
|
||||
if (base_type == NULL) {
|
||||
base_type = t_int;
|
||||
}
|
||||
enum_type->enumeration.base = base_type;
|
||||
enum_type->Enum.base = base_type;
|
||||
|
||||
Entity **fields = gb_alloc_array(c->allocator, Entity *, et->field_count);
|
||||
isize field_index = 0;
|
||||
@@ -120,8 +163,8 @@ void check_enum_type(Checker *c, Type *enum_type, AstNode *node) {
|
||||
}
|
||||
add_entity_use(&c->info, f->field, e);
|
||||
}
|
||||
enum_type->enumeration.fields = fields;
|
||||
enum_type->enumeration.field_count = et->field_count;
|
||||
enum_type->Enum.fields = fields;
|
||||
enum_type->Enum.field_count = et->field_count;
|
||||
}
|
||||
|
||||
Type *check_get_params(Checker *c, Scope *scope, AstNode *field_list, isize field_count) {
|
||||
@@ -149,8 +192,8 @@ Type *check_get_params(Checker *c, Scope *scope, AstNode *field_list, isize fiel
|
||||
}
|
||||
}
|
||||
}
|
||||
tuple->tuple.variables = variables;
|
||||
tuple->tuple.variable_count = field_count;
|
||||
tuple->Tuple.variables = variables;
|
||||
tuple->Tuple.variable_count = field_count;
|
||||
|
||||
return tuple;
|
||||
}
|
||||
@@ -171,8 +214,8 @@ Type *check_get_results(Checker *c, Scope *scope, AstNode *list, isize list_coun
|
||||
// NOTE(bill): No need to record
|
||||
variables[variable_index++] = param;
|
||||
}
|
||||
tuple->tuple.variables = variables;
|
||||
tuple->tuple.variable_count = list_count;
|
||||
tuple->Tuple.variables = variables;
|
||||
tuple->Tuple.variable_count = list_count;
|
||||
|
||||
return tuple;
|
||||
}
|
||||
@@ -189,11 +232,11 @@ void check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node) {
|
||||
Type *params = check_get_params(c, c->context.scope, pt->param_list, param_count);
|
||||
Type *results = check_get_results(c, c->context.scope, pt->result_list, result_count);
|
||||
|
||||
type->proc.scope = c->context.scope;
|
||||
type->proc.params = params;
|
||||
type->proc.param_count = pt->param_count;
|
||||
type->proc.results = results;
|
||||
type->proc.result_count = pt->result_count;
|
||||
type->Proc.scope = c->context.scope;
|
||||
type->Proc.params = params;
|
||||
type->Proc.param_count = pt->param_count;
|
||||
type->Proc.results = results;
|
||||
type->Proc.result_count = pt->result_count;
|
||||
}
|
||||
|
||||
|
||||
@@ -354,7 +397,7 @@ Type *check_type_expr_extra(Checker *c, AstNode *e, Type *named_type) {
|
||||
case_end;
|
||||
|
||||
case_ast_node(st, StructType, e);
|
||||
Type *t = make_type_structure(c->allocator);
|
||||
Type *t = make_type_struct(c->allocator);
|
||||
set_base_type(named_type, t);
|
||||
check_struct_type(c, t, e);
|
||||
return t;
|
||||
@@ -460,14 +503,21 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type) {
|
||||
case_end;
|
||||
|
||||
case_ast_node(st, StructType, e);
|
||||
type = make_type_structure(c->allocator);
|
||||
type = make_type_struct(c->allocator);
|
||||
set_base_type(named_type, type);
|
||||
check_struct_type(c, type, e);
|
||||
goto end;
|
||||
case_end;
|
||||
|
||||
case_ast_node(st, UnionType, e);
|
||||
type = make_type_union(c->allocator);
|
||||
set_base_type(named_type, type);
|
||||
check_union_type(c, type, e);
|
||||
goto end;
|
||||
case_end;
|
||||
|
||||
case_ast_node(et, EnumType, e);
|
||||
type = make_type_enumeration(c->allocator);
|
||||
type = make_type_enum(c->allocator);
|
||||
set_base_type(named_type, type);
|
||||
check_enum_type(c, type, e);
|
||||
goto end;
|
||||
@@ -613,7 +663,7 @@ b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, Exac
|
||||
i64 imax = (1ll << (s-1ll));
|
||||
|
||||
|
||||
switch (type->basic.kind) {
|
||||
switch (type->Basic.kind) {
|
||||
case Basic_i8:
|
||||
case Basic_i16:
|
||||
case Basic_i32:
|
||||
@@ -638,7 +688,7 @@ b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, Exac
|
||||
if (v.kind != ExactValue_Float)
|
||||
return false;
|
||||
|
||||
switch (type->basic.kind) {
|
||||
switch (type->Basic.kind) {
|
||||
case Basic_f32:
|
||||
if (out_value) *out_value = v;
|
||||
return true;
|
||||
@@ -791,7 +841,7 @@ void check_comparison(Checker *c, Operand *x, Operand *y, Token op) {
|
||||
}
|
||||
|
||||
if (is_type_vector(get_base_type(y->type))) {
|
||||
x->type = make_type_vector(c->allocator, t_bool, get_base_type(y->type)->vector.count);
|
||||
x->type = make_type_vector(c->allocator, t_bool, get_base_type(y->type)->Vector.count);
|
||||
} else {
|
||||
x->type = t_untyped_bool;
|
||||
}
|
||||
@@ -1253,7 +1303,7 @@ void convert_to_typed(Checker *c, Operand *operand, Type *target_type) {
|
||||
update_expr_value(c, operand->expr, operand->value);
|
||||
} else {
|
||||
// TODO(bill): Is this really needed?
|
||||
switch (operand->type->basic.kind) {
|
||||
switch (operand->type->Basic.kind) {
|
||||
case Basic_UntypedBool:
|
||||
if (!is_type_boolean(target_type)) {
|
||||
convert_untyped_error(c, operand, target_type);
|
||||
@@ -1272,7 +1322,7 @@ void convert_to_typed(Checker *c, Operand *operand, Type *target_type) {
|
||||
}
|
||||
break;
|
||||
case Type_Pointer:
|
||||
switch (operand->type->basic.kind) {
|
||||
switch (operand->type->Basic.kind) {
|
||||
case Basic_UntypedPointer:
|
||||
target_type = t_untyped_pointer;
|
||||
break;
|
||||
@@ -1283,7 +1333,7 @@ void convert_to_typed(Checker *c, Operand *operand, Type *target_type) {
|
||||
break;
|
||||
|
||||
case Type_Proc:
|
||||
switch (operand->type->basic.kind) {
|
||||
switch (operand->type->Basic.kind) {
|
||||
case Basic_UntypedPointer:
|
||||
break;
|
||||
default:
|
||||
@@ -1359,14 +1409,14 @@ Entity *lookup_field(Type *type, AstNode *field_node, isize *index = NULL) {
|
||||
GB_ASSERT(field_node->kind == AstNode_Ident);
|
||||
type = get_base_type(type);
|
||||
if (type->kind == Type_Pointer)
|
||||
type = get_base_type(type->pointer.elem);
|
||||
type = get_base_type(type->Pointer.elem);
|
||||
|
||||
ast_node(i, Ident, field_node);
|
||||
String field_str = i->token.string;
|
||||
switch (type->kind) {
|
||||
case Type_Structure:
|
||||
for (isize i = 0; i < type->structure.field_count; i++) {
|
||||
Entity *f = type->structure.fields[i];
|
||||
case Type_Struct:
|
||||
for (isize i = 0; i < type->Struct.field_count; i++) {
|
||||
Entity *f = type->Struct.fields[i];
|
||||
GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field);
|
||||
String str = f->token.string;
|
||||
if (are_strings_equal(field_str, str)) {
|
||||
@@ -1375,9 +1425,22 @@ Entity *lookup_field(Type *type, AstNode *field_node, isize *index = NULL) {
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Type_Enumeration:
|
||||
for (isize i = 0; i < type->enumeration.field_count; i++) {
|
||||
Entity *f = type->enumeration.fields[i];
|
||||
|
||||
case Type_Union:
|
||||
for (isize i = 0; i < type->Union.field_count; i++) {
|
||||
Entity *f = type->Union.fields[i];
|
||||
GB_ASSERT(f->kind == Entity_Variable && f->Variable.is_field);
|
||||
String str = f->token.string;
|
||||
if (are_strings_equal(field_str, str)) {
|
||||
if (index) *index = i;
|
||||
return f;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Type_Enum:
|
||||
for (isize i = 0; i < type->Enum.field_count; i++) {
|
||||
Entity *f = type->Enum.fields[i];
|
||||
GB_ASSERT(f->kind == Entity_Constant);
|
||||
String str = f->token.string;
|
||||
if (are_strings_equal(field_str, str)) {
|
||||
@@ -1522,7 +1585,7 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
||||
Type *type = get_base_type(check_type(c, ce->arg_list));
|
||||
AstNode *field_arg = unparen_expr(ce->arg_list->next);
|
||||
if (type) {
|
||||
if (type->kind != Type_Structure) {
|
||||
if (type->kind != Type_Struct) {
|
||||
error(&c->error_collector, ast_node_token(ce->arg_list), "Expected a structure type for `offset_of`");
|
||||
return false;
|
||||
}
|
||||
@@ -1565,8 +1628,8 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
||||
Type *type = operand->type;
|
||||
if (get_base_type(type)->kind == Type_Pointer) {
|
||||
Type *p = get_base_type(type);
|
||||
if (get_base_type(p)->kind == Type_Structure)
|
||||
type = p->pointer.elem;
|
||||
if (get_base_type(p)->kind == Type_Struct)
|
||||
type = p->Pointer.elem;
|
||||
}
|
||||
|
||||
isize index = 0;
|
||||
@@ -1628,12 +1691,12 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
||||
|
||||
case Type_Array:
|
||||
mode = Addressing_Constant;
|
||||
value = make_exact_value_integer(t->array.count);
|
||||
value = make_exact_value_integer(t->Array.count);
|
||||
break;
|
||||
|
||||
case Type_Vector:
|
||||
mode = Addressing_Constant;
|
||||
value = make_exact_value_integer(t->vector.count);
|
||||
value = make_exact_value_integer(t->Vector.count);
|
||||
break;
|
||||
|
||||
case Type_Slice:
|
||||
@@ -1662,7 +1725,7 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
||||
|
||||
Type *d = get_base_type(operand->type);
|
||||
if (d->kind == Type_Slice)
|
||||
dest_type = d->slice.elem;
|
||||
dest_type = d->Slice.elem;
|
||||
|
||||
Operand op = {};
|
||||
check_expr(c, &op, ce->arg_list->next);
|
||||
@@ -1670,7 +1733,7 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
||||
return false;
|
||||
Type *s = get_base_type(op.type);
|
||||
if (s->kind == Type_Slice)
|
||||
src_type = s->slice.elem;
|
||||
src_type = s->Slice.elem;
|
||||
|
||||
if (dest_type == NULL || src_type == NULL) {
|
||||
error(&c->error_collector, ast_node_token(call), "`copy` only expects slices as arguments");
|
||||
@@ -1707,12 +1770,12 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
||||
return false;
|
||||
y_type = get_base_type(op.type);
|
||||
|
||||
if (!(is_type_pointer(x_type) && is_type_slice(x_type->pointer.elem))) {
|
||||
if (!(is_type_pointer(x_type) && is_type_slice(x_type->Pointer.elem))) {
|
||||
error(&c->error_collector, ast_node_token(call), "First argument to `append` must be a pointer to a slice");
|
||||
return false;
|
||||
}
|
||||
|
||||
Type *elem_type = x_type->pointer.elem->slice.elem;
|
||||
Type *elem_type = x_type->Pointer.elem->Slice.elem;
|
||||
if (!check_is_assignable_to(c, &op, elem_type)) {
|
||||
gbString d_arg = expr_to_string(ce->arg_list);
|
||||
gbString s_arg = expr_to_string(ce->arg_list->next);
|
||||
@@ -1744,7 +1807,7 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
||||
return false;
|
||||
}
|
||||
|
||||
isize max_count = vector_type->vector.count;
|
||||
isize max_count = vector_type->Vector.count;
|
||||
isize arg_count = 0;
|
||||
for (AstNode *arg = ce->arg_list->next; arg != NULL; arg = arg->next) {
|
||||
Operand op = {};
|
||||
@@ -1775,7 +1838,7 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
||||
return false;
|
||||
}
|
||||
|
||||
Type *elem_type = vector_type->vector.elem;
|
||||
Type *elem_type = vector_type->Vector.elem;
|
||||
operand->type = make_type_vector(c->allocator, elem_type, arg_count);
|
||||
operand->mode = Addressing_Value;
|
||||
} break;
|
||||
@@ -1793,8 +1856,8 @@ void check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode
|
||||
isize param_index = 0;
|
||||
isize param_count = 0;
|
||||
|
||||
if (proc_type->proc.params)
|
||||
param_count = proc_type->proc.params->tuple.variable_count;
|
||||
if (proc_type->Proc.params)
|
||||
param_count = proc_type->Proc.params->Tuple.variable_count;
|
||||
|
||||
if (ce->arg_list_count == 0 && param_count == 0)
|
||||
return;
|
||||
@@ -1802,7 +1865,7 @@ void check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode
|
||||
if (ce->arg_list_count > param_count) {
|
||||
error_code = +1;
|
||||
} else {
|
||||
Entity **sig_params = proc_type->proc.params->tuple.variables;
|
||||
Entity **sig_params = proc_type->Proc.params->Tuple.variables;
|
||||
AstNode *call_arg = ce->arg_list;
|
||||
for (; call_arg != NULL; call_arg = call_arg->next) {
|
||||
check_multi_expr(c, operand, call_arg);
|
||||
@@ -1813,7 +1876,7 @@ void check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode
|
||||
check_assignment(c, operand, sig_params[param_index]->type, make_string("argument"));
|
||||
param_index++;
|
||||
} else {
|
||||
auto *tuple = &operand->type->tuple;
|
||||
auto *tuple = &operand->type->Tuple;
|
||||
isize i = 0;
|
||||
for (;
|
||||
i < tuple->variable_count && param_index < param_count;
|
||||
@@ -1897,12 +1960,12 @@ ExpressionKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
|
||||
|
||||
check_call_arguments(c, operand, proc_type, call);
|
||||
|
||||
auto *proc = &proc_type->proc;
|
||||
auto *proc = &proc_type->Proc;
|
||||
if (proc->result_count == 0) {
|
||||
operand->mode = Addressing_NoValue;
|
||||
} else if (proc->result_count == 1) {
|
||||
operand->mode = Addressing_Value;
|
||||
operand->type = proc->results->tuple.variables[0]->type;
|
||||
operand->type = proc->results->Tuple.variables[0]->type;
|
||||
} else {
|
||||
operand->mode = Addressing_Value;
|
||||
operand->type = proc->results;
|
||||
@@ -2007,12 +2070,12 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
|
||||
Type *t = get_base_type(type);
|
||||
switch (t->kind) {
|
||||
case Type_Structure: {
|
||||
case Type_Struct: {
|
||||
if (cl->elem_count == 0)
|
||||
break; // NOTE(bill): No need to init
|
||||
{ // Checker values
|
||||
AstNode *elem = cl->elem_list;
|
||||
isize field_count = t->structure.field_count;
|
||||
isize field_count = t->Struct.field_count;
|
||||
if (elem->kind == AstNode_FieldValue) {
|
||||
b32 *fields_visited = gb_alloc_array(c->allocator, b32, field_count);
|
||||
|
||||
@@ -2041,7 +2104,7 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
"Unknown field `%.*s` in structure literal", LIT(name));
|
||||
continue;
|
||||
}
|
||||
Entity *field = t->structure.fields[index];
|
||||
Entity *field = t->Struct.fields[index];
|
||||
add_entity_use(&c->info, kv->field, field);
|
||||
|
||||
if (fields_visited[index]) {
|
||||
@@ -2064,7 +2127,7 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
"Mixture of `field = value` and value elements in a structure literal is not allowed");
|
||||
continue;
|
||||
}
|
||||
Entity *field = t->structure.fields[index];
|
||||
Entity *field = t->Struct.fields[index];
|
||||
|
||||
check_expr(c, o, elem);
|
||||
if (index >= field_count) {
|
||||
@@ -2088,13 +2151,13 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
Type *elem_type = NULL;
|
||||
String context_name = {};
|
||||
if (t->kind == Type_Slice) {
|
||||
elem_type = t->slice.elem;
|
||||
elem_type = t->Slice.elem;
|
||||
context_name = make_string("slice literal");
|
||||
} else if (t->kind == Type_Vector) {
|
||||
elem_type = t->vector.elem;
|
||||
elem_type = t->Vector.elem;
|
||||
context_name = make_string("vector literal");
|
||||
} else {
|
||||
elem_type = t->array.elem;
|
||||
elem_type = t->Array.elem;
|
||||
context_name = make_string("array literal");
|
||||
}
|
||||
|
||||
@@ -2111,14 +2174,14 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
|
||||
|
||||
if (t->kind == Type_Array &&
|
||||
t->array.count >= 0 &&
|
||||
index >= t->array.count) {
|
||||
error(&c->error_collector, ast_node_token(elem), "Index %lld is out of bounds (>= %lld) for array literal", index, t->array.count);
|
||||
t->Array.count >= 0 &&
|
||||
index >= t->Array.count) {
|
||||
error(&c->error_collector, ast_node_token(elem), "Index %lld is out of bounds (>= %lld) for array literal", index, t->Array.count);
|
||||
}
|
||||
if (t->kind == Type_Vector &&
|
||||
t->vector.count >= 0 &&
|
||||
index >= t->vector.count) {
|
||||
error(&c->error_collector, ast_node_token(elem), "Index %lld is out of bounds (>= %lld) for vector literal", index, t->vector.count);
|
||||
t->Vector.count >= 0 &&
|
||||
index >= t->Vector.count) {
|
||||
error(&c->error_collector, ast_node_token(elem), "Index %lld is out of bounds (>= %lld) for vector literal", index, t->Vector.count);
|
||||
}
|
||||
|
||||
Operand o = {};
|
||||
@@ -2129,14 +2192,14 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
max = index;
|
||||
|
||||
if (t->kind == Type_Vector) {
|
||||
if (t->vector.count > 1 && gb_is_between(index, 2, t->vector.count-1)) {
|
||||
if (t->Vector.count > 1 && gb_is_between(index, 2, t->Vector.count-1)) {
|
||||
error(&c->error_collector, ast_node_token(cl->elem_list),
|
||||
"Expected either 1 (broadcast) or %td elements in vector literal, got %td", t->vector.count, index);
|
||||
"Expected either 1 (broadcast) or %td elements in vector literal, got %td", t->Vector.count, index);
|
||||
}
|
||||
}
|
||||
|
||||
if (t->kind == Type_Array && ellipsis_array) {
|
||||
t->array.count = max;
|
||||
t->Array.count = max;
|
||||
}
|
||||
} break;
|
||||
|
||||
@@ -2213,31 +2276,31 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
|
||||
case Type_Array:
|
||||
valid = true;
|
||||
max_count = t->array.count;
|
||||
max_count = t->Array.count;
|
||||
if (o->mode != Addressing_Variable)
|
||||
o->mode = Addressing_Value;
|
||||
o->type = t->array.elem;
|
||||
o->type = t->Array.elem;
|
||||
break;
|
||||
|
||||
case Type_Vector:
|
||||
valid = true;
|
||||
max_count = t->vector.count;
|
||||
max_count = t->Vector.count;
|
||||
if (o->mode != Addressing_Variable)
|
||||
o->mode = Addressing_Value;
|
||||
o->type = t->vector.elem;
|
||||
o->type = t->Vector.elem;
|
||||
break;
|
||||
|
||||
|
||||
case Type_Slice:
|
||||
valid = true;
|
||||
o->type = t->slice.elem;
|
||||
o->type = t->Slice.elem;
|
||||
o->mode = Addressing_Variable;
|
||||
break;
|
||||
|
||||
case Type_Pointer:
|
||||
valid = true;
|
||||
o->mode = Addressing_Variable;
|
||||
o->type = get_base_type(t->pointer.elem);
|
||||
o->type = get_base_type(t->Pointer.elem);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -2282,14 +2345,14 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
|
||||
case Type_Array:
|
||||
valid = true;
|
||||
max_count = t->array.count;
|
||||
max_count = t->Array.count;
|
||||
if (o->mode != Addressing_Variable) {
|
||||
gbString str = expr_to_string(node);
|
||||
error(&c->error_collector, ast_node_token(node), "Cannot slice array `%s`, value is not addressable", str);
|
||||
gb_string_free(str);
|
||||
goto error;
|
||||
}
|
||||
o->type = make_type_slice(c->allocator, t->array.elem);
|
||||
o->type = make_type_slice(c->allocator, t->Array.elem);
|
||||
o->mode = Addressing_Value;
|
||||
break;
|
||||
|
||||
@@ -2300,7 +2363,7 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
|
||||
case Type_Pointer:
|
||||
valid = true;
|
||||
o->type = make_type_slice(c->allocator, get_base_type(t->pointer.elem));
|
||||
o->type = make_type_slice(c->allocator, get_base_type(t->Pointer.elem));
|
||||
o->mode = Addressing_Value;
|
||||
break;
|
||||
}
|
||||
@@ -2355,7 +2418,7 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
Type *t = get_base_type(o->type);
|
||||
if (t->kind == Type_Pointer) {
|
||||
o->mode = Addressing_Variable;
|
||||
o->type = t->pointer.elem;
|
||||
o->type = t->Pointer.elem;
|
||||
} else {
|
||||
gbString str = expr_to_string(o->expr);
|
||||
error(&c->error_collector, ast_node_token(o->expr), "Cannot dereference `%s`", str);
|
||||
@@ -2439,7 +2502,7 @@ void check_not_tuple(Checker *c, Operand *o) {
|
||||
if (o->mode == Addressing_Value) {
|
||||
// NOTE(bill): Tuples are not first class thus never named
|
||||
if (o->type->kind == Type_Tuple) {
|
||||
isize count = o->type->tuple.variable_count;
|
||||
isize count = o->type->Tuple.variable_count;
|
||||
GB_ASSERT(count != 1);
|
||||
error(&c->error_collector, ast_node_token(o->expr),
|
||||
"%td-valued tuple found where single value expected", count);
|
||||
|
||||
+13
-13
@@ -91,10 +91,10 @@ b32 check_is_assignable_to(Checker *c, Operand *operand, Type *type) {
|
||||
if (operand->mode == Addressing_Constant)
|
||||
return check_value_is_expressible(c, operand->value, tb, NULL);
|
||||
if (sb->kind == Type_Basic)
|
||||
return sb->basic.kind == Basic_UntypedBool && is_type_boolean(tb);
|
||||
return sb->Basic.kind == Basic_UntypedBool && is_type_boolean(tb);
|
||||
break;
|
||||
case Type_Pointer:
|
||||
return sb->basic.kind == Basic_UntypedPointer;
|
||||
return sb->Basic.kind == Basic_UntypedPointer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,13 +108,13 @@ b32 check_is_assignable_to(Checker *c, Operand *operand, Type *type) {
|
||||
return true;
|
||||
|
||||
if (sb->kind == Type_Array && tb->kind == Type_Array) {
|
||||
if (are_types_identical(sb->array.elem, tb->array.elem)) {
|
||||
return sb->array.count == tb->array.count;
|
||||
if (are_types_identical(sb->Array.elem, tb->Array.elem)) {
|
||||
return sb->Array.count == tb->Array.count;
|
||||
}
|
||||
}
|
||||
|
||||
if (sb->kind == Type_Slice && tb->kind == Type_Slice) {
|
||||
if (are_types_identical(sb->slice.elem, tb->slice.elem)) {
|
||||
if (are_types_identical(sb->Slice.elem, tb->Slice.elem)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -278,7 +278,7 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNode *in
|
||||
if (o.type->kind != Type_Tuple) {
|
||||
gb_array_append(operands, o);
|
||||
} else {
|
||||
auto *tuple = &o.type->tuple;
|
||||
auto *tuple = &o.type->Tuple;
|
||||
for (isize j = 0; j < tuple->variable_count; j++) {
|
||||
o.type = tuple->variables[j]->type;
|
||||
gb_array_append(operands, o);
|
||||
@@ -362,7 +362,7 @@ void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_e
|
||||
void check_type_decl(Checker *c, Entity *e, AstNode *type_expr, Type *named_type) {
|
||||
GB_ASSERT(e->type == NULL);
|
||||
Type *named = make_type_named(c->allocator, e->token.string, NULL, e);
|
||||
named->named.type_name = e;
|
||||
named->Named.type_name = e;
|
||||
set_base_type(named_type, named);
|
||||
e->type = named;
|
||||
|
||||
@@ -381,7 +381,7 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
|
||||
push_procedure(c, type);
|
||||
ast_node(bs, BlockStmt, body);
|
||||
check_stmt_list(c, bs->list, 0);
|
||||
if (type->proc.result_count > 0) {
|
||||
if (type->Proc.result_count > 0) {
|
||||
if (!check_is_terminating(c, body)) {
|
||||
error(&c->error_collector, bs->close, "Missing return statement at the end of the procedure");
|
||||
}
|
||||
@@ -414,7 +414,7 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d, b32 check_body_later) {
|
||||
if (d->scope == c->global_scope &&
|
||||
are_strings_equal(e->token.string, make_string("main"))) {
|
||||
if (proc_type != NULL) {
|
||||
auto *pt = &proc_type->proc;
|
||||
auto *pt = &proc_type->Proc;
|
||||
if (pt->param_count != 0 ||
|
||||
pt->result_count) {
|
||||
gbString str = type_to_string(proc_type);
|
||||
@@ -596,7 +596,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
||||
if (o.type->kind != Type_Tuple) {
|
||||
gb_array_append(operands, o);
|
||||
} else {
|
||||
auto *tuple = &o.type->tuple;
|
||||
auto *tuple = &o.type->Tuple;
|
||||
for (isize j = 0; j < tuple->variable_count; j++) {
|
||||
o.type = tuple->variables[j]->type;
|
||||
gb_array_append(operands, o);
|
||||
@@ -696,15 +696,15 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
||||
|
||||
Type *proc_type = c->proc_stack[gb_array_count(c->proc_stack)-1];
|
||||
isize result_count = 0;
|
||||
if (proc_type->proc.results)
|
||||
result_count = proc_type->proc.results->tuple.variable_count;
|
||||
if (proc_type->Proc.results)
|
||||
result_count = proc_type->Proc.results->Tuple.variable_count;
|
||||
if (result_count != rs->result_count) {
|
||||
error(&c->error_collector, rs->token, "Expected %td return %s, got %td",
|
||||
result_count,
|
||||
(result_count != 1 ? "values" : "value"),
|
||||
rs->result_count);
|
||||
} else if (result_count > 0) {
|
||||
auto *tuple = &proc_type->proc.results->tuple;
|
||||
auto *tuple = &proc_type->Proc.results->Tuple;
|
||||
check_init_variables(c, tuple->variables, tuple->variable_count,
|
||||
rs->result_list, rs->result_count, make_string("return statement"));
|
||||
}
|
||||
|
||||
+173
-128
@@ -61,8 +61,9 @@ struct BasicType {
|
||||
TYPE_KIND(Array), \
|
||||
TYPE_KIND(Vector), \
|
||||
TYPE_KIND(Slice), \
|
||||
TYPE_KIND(Structure), \
|
||||
TYPE_KIND(Enumeration), \
|
||||
TYPE_KIND(Struct), \
|
||||
TYPE_KIND(Union), \
|
||||
TYPE_KIND(Enum), \
|
||||
TYPE_KIND(Pointer), \
|
||||
TYPE_KIND(Named), \
|
||||
TYPE_KIND(Tuple), \
|
||||
@@ -90,18 +91,18 @@ struct Type {
|
||||
u32 flags;
|
||||
TypeKind kind;
|
||||
union {
|
||||
BasicType basic;
|
||||
BasicType Basic;
|
||||
struct {
|
||||
Type *elem;
|
||||
i64 count;
|
||||
} array;
|
||||
} Array;
|
||||
struct {
|
||||
Type *elem;
|
||||
i64 count;
|
||||
} vector;
|
||||
} Vector;
|
||||
struct {
|
||||
Type *elem;
|
||||
} slice;
|
||||
} Slice;
|
||||
struct {
|
||||
// Theses are arrays
|
||||
Entity **fields; // Entity_Variable
|
||||
@@ -109,42 +110,46 @@ struct Type {
|
||||
i64 * offsets;
|
||||
b32 are_offsets_set;
|
||||
b32 is_packed;
|
||||
} structure;
|
||||
struct { Type *elem; } pointer;
|
||||
} Struct;
|
||||
struct {
|
||||
Entity **fields; // Entity_Variable
|
||||
isize field_count;
|
||||
} Union;
|
||||
struct { Type *elem; } Pointer;
|
||||
struct {
|
||||
String name;
|
||||
Type * base;
|
||||
Entity *type_name; // Entity_TypeName
|
||||
} named;
|
||||
} Named;
|
||||
struct {
|
||||
Entity **variables; // Entity_Variable
|
||||
isize variable_count;
|
||||
} tuple;
|
||||
} Tuple;
|
||||
struct {
|
||||
Scope *scope;
|
||||
Type * params; // Type_Tuple
|
||||
Type * results; // Type_Tuple
|
||||
isize param_count;
|
||||
isize result_count;
|
||||
} proc;
|
||||
} Proc;
|
||||
struct {
|
||||
Type * base; // Default is `int`
|
||||
Entity **fields; // Entity_Constant
|
||||
isize field_count;
|
||||
} enumeration;
|
||||
} Enum;
|
||||
};
|
||||
};
|
||||
|
||||
Type *get_base_type(Type *t) {
|
||||
while (t->kind == Type_Named) {
|
||||
t = t->named.base;
|
||||
t = t->Named.base;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
void set_base_type(Type *t, Type *base) {
|
||||
if (t && t->kind == Type_Named) {
|
||||
t->named.base = base;
|
||||
t->Named.base = base;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,51 +163,56 @@ Type *alloc_type(gbAllocator a, TypeKind kind) {
|
||||
|
||||
Type *make_type_basic(gbAllocator a, BasicType basic) {
|
||||
Type *t = alloc_type(a, Type_Basic);
|
||||
t->basic = basic;
|
||||
t->Basic = basic;
|
||||
return t;
|
||||
}
|
||||
|
||||
Type *make_type_array(gbAllocator a, Type *elem, i64 count) {
|
||||
Type *t = alloc_type(a, Type_Array);
|
||||
t->array.elem = elem;
|
||||
t->array.count = count;
|
||||
t->Array.elem = elem;
|
||||
t->Array.count = count;
|
||||
return t;
|
||||
}
|
||||
|
||||
Type *make_type_vector(gbAllocator a, Type *elem, i64 count) {
|
||||
Type *t = alloc_type(a, Type_Vector);
|
||||
t->vector.elem = elem;
|
||||
t->vector.count = count;
|
||||
t->Vector.elem = elem;
|
||||
t->Vector.count = count;
|
||||
return t;
|
||||
}
|
||||
|
||||
Type *make_type_slice(gbAllocator a, Type *elem) {
|
||||
Type *t = alloc_type(a, Type_Slice);
|
||||
t->array.elem = elem;
|
||||
t->Array.elem = elem;
|
||||
return t;
|
||||
}
|
||||
|
||||
Type *make_type_structure(gbAllocator a) {
|
||||
Type *t = alloc_type(a, Type_Structure);
|
||||
Type *make_type_struct(gbAllocator a) {
|
||||
Type *t = alloc_type(a, Type_Struct);
|
||||
return t;
|
||||
}
|
||||
|
||||
Type *make_type_enumeration(gbAllocator a) {
|
||||
Type *t = alloc_type(a, Type_Enumeration);
|
||||
Type *make_type_union(gbAllocator a) {
|
||||
Type *t = alloc_type(a, Type_Union);
|
||||
return t;
|
||||
}
|
||||
|
||||
Type *make_type_enum(gbAllocator a) {
|
||||
Type *t = alloc_type(a, Type_Enum);
|
||||
return t;
|
||||
}
|
||||
|
||||
Type *make_type_pointer(gbAllocator a, Type *elem) {
|
||||
Type *t = alloc_type(a, Type_Pointer);
|
||||
t->pointer.elem = elem;
|
||||
t->Pointer.elem = elem;
|
||||
return t;
|
||||
}
|
||||
|
||||
Type *make_type_named(gbAllocator a, String name, Type *base, Entity *type_name) {
|
||||
Type *t = alloc_type(a, Type_Named);
|
||||
t->named.name = name;
|
||||
t->named.base = base;
|
||||
t->named.type_name = type_name;
|
||||
t->Named.name = name;
|
||||
t->Named.base = base;
|
||||
t->Named.type_name = type_name;
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -213,11 +223,11 @@ Type *make_type_tuple(gbAllocator a) {
|
||||
|
||||
Type *make_type_proc(gbAllocator a, Scope *scope, Type *params, isize param_count, Type *results, isize result_count) {
|
||||
Type *t = alloc_type(a, Type_Proc);
|
||||
t->proc.scope = scope;
|
||||
t->proc.params = params;
|
||||
t->proc.param_count = param_count;
|
||||
t->proc.results = results;
|
||||
t->proc.result_count = result_count;
|
||||
t->Proc.scope = scope;
|
||||
t->Proc.params = params;
|
||||
t->Proc.param_count = param_count;
|
||||
t->Proc.results = results;
|
||||
t->Proc.result_count = result_count;
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -226,7 +236,7 @@ Type *type_deref(Type *t) {
|
||||
if (t != NULL) {
|
||||
Type *bt = get_base_type(t);
|
||||
if (bt != NULL && bt->kind == Type_Pointer)
|
||||
return bt->pointer.elem;
|
||||
return bt->Pointer.elem;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
@@ -301,51 +311,51 @@ b32 is_type_named(Type *t) {
|
||||
b32 is_type_boolean(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_Boolean) != 0;
|
||||
return (t->Basic.flags & BasicFlag_Boolean) != 0;
|
||||
return false;
|
||||
}
|
||||
b32 is_type_integer(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_Integer) != 0;
|
||||
return (t->Basic.flags & BasicFlag_Integer) != 0;
|
||||
return false;
|
||||
}
|
||||
b32 is_type_unsigned(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_Unsigned) != 0;
|
||||
return (t->Basic.flags & BasicFlag_Unsigned) != 0;
|
||||
return false;
|
||||
}
|
||||
b32 is_type_numeric(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_Numeric) != 0;
|
||||
return (t->Basic.flags & BasicFlag_Numeric) != 0;
|
||||
if (t->kind == Type_Vector)
|
||||
return is_type_numeric(t->vector.elem);
|
||||
return is_type_numeric(t->Vector.elem);
|
||||
return false;
|
||||
}
|
||||
b32 is_type_string(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_String) != 0;
|
||||
return (t->Basic.flags & BasicFlag_String) != 0;
|
||||
return false;
|
||||
}
|
||||
b32 is_type_typed(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_Untyped) == 0;
|
||||
return (t->Basic.flags & BasicFlag_Untyped) == 0;
|
||||
return true;
|
||||
}
|
||||
b32 is_type_untyped(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_Untyped) != 0;
|
||||
return (t->Basic.flags & BasicFlag_Untyped) != 0;
|
||||
return false;
|
||||
}
|
||||
b32 is_type_ordered(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_Ordered) != 0;
|
||||
return (t->Basic.flags & BasicFlag_Ordered) != 0;
|
||||
if (t->kind == Type_Pointer)
|
||||
return true;
|
||||
return false;
|
||||
@@ -353,35 +363,35 @@ b32 is_type_ordered(Type *t) {
|
||||
b32 is_type_constant_type(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_ConstantType) != 0;
|
||||
return (t->Basic.flags & BasicFlag_ConstantType) != 0;
|
||||
return false;
|
||||
}
|
||||
b32 is_type_float(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_Float) != 0;
|
||||
return (t->Basic.flags & BasicFlag_Float) != 0;
|
||||
return false;
|
||||
}
|
||||
b32 is_type_pointer(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.flags & BasicFlag_Pointer) != 0;
|
||||
return (t->Basic.flags & BasicFlag_Pointer) != 0;
|
||||
return t->kind == Type_Pointer;
|
||||
}
|
||||
|
||||
b32 is_type_int_or_uint(Type *t) {
|
||||
if (t->kind == Type_Basic)
|
||||
return (t->basic.kind == Basic_int) || (t->basic.kind == Basic_uint);
|
||||
return (t->Basic.kind == Basic_int) || (t->Basic.kind == Basic_uint);
|
||||
return false;
|
||||
}
|
||||
b32 is_type_rawptr(Type *t) {
|
||||
if (t->kind == Type_Basic)
|
||||
return t->basic.kind == Basic_rawptr;
|
||||
return t->Basic.kind == Basic_rawptr;
|
||||
return false;
|
||||
}
|
||||
b32 is_type_u8(Type *t) {
|
||||
if (t->kind == Type_Basic)
|
||||
return t->basic.kind == Basic_u8;
|
||||
return t->Basic.kind == Basic_u8;
|
||||
return false;
|
||||
}
|
||||
b32 is_type_slice(Type *t) {
|
||||
@@ -391,7 +401,7 @@ b32 is_type_slice(Type *t) {
|
||||
b32 is_type_u8_slice(Type *t) {
|
||||
t = get_base_type(t);
|
||||
if (t->kind == Type_Slice)
|
||||
return is_type_u8(t->slice.elem);
|
||||
return is_type_u8(t->Slice.elem);
|
||||
return false;
|
||||
}
|
||||
b32 is_type_vector(Type *t) {
|
||||
@@ -403,18 +413,18 @@ b32 is_type_proc(Type *t) {
|
||||
}
|
||||
Type *base_vector_type(Type *t) {
|
||||
if (is_type_vector(t)) {
|
||||
return t->vector.elem;
|
||||
return t->Vector.elem;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
b32 is_type_enum(Type *t) {
|
||||
t = get_base_type(t);
|
||||
return t->kind == Type_Enumeration;
|
||||
return t->kind == Type_Enum;
|
||||
}
|
||||
Type *get_enum_base_type(Type *t) {
|
||||
Type *bt = get_base_type(t);
|
||||
if (is_type_enum(bt)) {
|
||||
return bt->enumeration.base;
|
||||
return bt->Enum.base;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
@@ -428,19 +438,19 @@ b32 is_type_comparable(Type *t) {
|
||||
return true;
|
||||
case Type_Pointer:
|
||||
return true;
|
||||
case Type_Structure: {
|
||||
for (isize i = 0; i < t->structure.field_count; i++) {
|
||||
if (!is_type_comparable(t->structure.fields[i]->type))
|
||||
case Type_Struct: {
|
||||
for (isize i = 0; i < t->Struct.field_count; i++) {
|
||||
if (!is_type_comparable(t->Struct.fields[i]->type))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} break;
|
||||
case Type_Array:
|
||||
return is_type_comparable(t->array.elem);
|
||||
return is_type_comparable(t->Array.elem);
|
||||
case Type_Vector:
|
||||
return is_type_comparable(t->vector.elem);
|
||||
case Type_Enumeration:
|
||||
return is_type_comparable(t->enumeration.base);
|
||||
return is_type_comparable(t->Vector.elem);
|
||||
case Type_Enum:
|
||||
return is_type_comparable(t->Enum.base);
|
||||
case Type_Proc:
|
||||
return true;
|
||||
}
|
||||
@@ -459,29 +469,29 @@ b32 are_types_identical(Type *x, Type *y) {
|
||||
switch (x->kind) {
|
||||
case Type_Basic:
|
||||
if (y->kind == Type_Basic)
|
||||
return x->basic.kind == y->basic.kind;
|
||||
return x->Basic.kind == y->Basic.kind;
|
||||
break;
|
||||
|
||||
case Type_Array:
|
||||
if (y->kind == Type_Array)
|
||||
return (x->array.count == y->array.count) && are_types_identical(x->array.elem, y->array.elem);
|
||||
return (x->Array.count == y->Array.count) && are_types_identical(x->Array.elem, y->Array.elem);
|
||||
break;
|
||||
|
||||
case Type_Vector:
|
||||
if (y->kind == Type_Vector)
|
||||
return (x->vector.count == y->vector.count) && are_types_identical(x->vector.elem, y->vector.elem);
|
||||
return (x->Vector.count == y->Vector.count) && are_types_identical(x->Vector.elem, y->Vector.elem);
|
||||
break;
|
||||
|
||||
case Type_Slice:
|
||||
if (y->kind == Type_Slice)
|
||||
return are_types_identical(x->slice.elem, y->slice.elem);
|
||||
return are_types_identical(x->Slice.elem, y->Slice.elem);
|
||||
break;
|
||||
|
||||
case Type_Structure:
|
||||
if (y->kind == Type_Structure) {
|
||||
if (x->structure.field_count == y->structure.field_count) {
|
||||
for (isize i = 0; i < x->structure.field_count; i++) {
|
||||
if (!are_types_identical(x->structure.fields[i]->type, y->structure.fields[i]->type)) {
|
||||
case Type_Struct:
|
||||
if (y->kind == Type_Struct) {
|
||||
if (x->Struct.field_count == y->Struct.field_count) {
|
||||
for (isize i = 0; i < x->Struct.field_count; i++) {
|
||||
if (!are_types_identical(x->Struct.fields[i]->type, y->Struct.fields[i]->type)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -493,19 +503,19 @@ b32 are_types_identical(Type *x, Type *y) {
|
||||
|
||||
case Type_Pointer:
|
||||
if (y->kind == Type_Pointer)
|
||||
return are_types_identical(x->pointer.elem, y->pointer.elem);
|
||||
return are_types_identical(x->Pointer.elem, y->Pointer.elem);
|
||||
break;
|
||||
|
||||
case Type_Named:
|
||||
if (y->kind == Type_Named)
|
||||
return x->named.base == y->named.base;
|
||||
return x->Named.base == y->Named.base;
|
||||
break;
|
||||
|
||||
case Type_Tuple:
|
||||
if (y->kind == Type_Tuple) {
|
||||
if (x->tuple.variable_count == y->tuple.variable_count) {
|
||||
for (isize i = 0; i < x->tuple.variable_count; i++) {
|
||||
if (!are_types_identical(x->tuple.variables[i]->type, y->tuple.variables[i]->type))
|
||||
if (x->Tuple.variable_count == y->Tuple.variable_count) {
|
||||
for (isize i = 0; i < x->Tuple.variable_count; i++) {
|
||||
if (!are_types_identical(x->Tuple.variables[i]->type, y->Tuple.variables[i]->type))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -515,8 +525,8 @@ b32 are_types_identical(Type *x, Type *y) {
|
||||
|
||||
case Type_Proc:
|
||||
if (y->kind == Type_Proc) {
|
||||
return are_types_identical(x->proc.params, y->proc.params) &&
|
||||
are_types_identical(x->proc.results, y->proc.results);
|
||||
return are_types_identical(x->Proc.params, y->Proc.params) &&
|
||||
are_types_identical(x->Proc.results, y->Proc.results);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -528,7 +538,7 @@ b32 are_types_identical(Type *x, Type *y) {
|
||||
|
||||
Type *default_type(Type *type) {
|
||||
if (type->kind == Type_Basic) {
|
||||
switch (type->basic.kind) {
|
||||
switch (type->Basic.kind) {
|
||||
case Basic_UntypedBool: return &basic_types[Basic_bool];
|
||||
case Basic_UntypedInteger: return &basic_types[Basic_int];
|
||||
case Basic_UntypedFloat: return &basic_types[Basic_f64];
|
||||
@@ -585,20 +595,20 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
|
||||
switch (t->kind) {
|
||||
case Type_Array:
|
||||
return type_align_of(s, allocator, t->array.elem);
|
||||
return type_align_of(s, allocator, t->Array.elem);
|
||||
case Type_Vector: {
|
||||
i64 size = type_size_of(s, allocator, t->vector.elem);
|
||||
size *= t->vector.count;
|
||||
i64 size = type_size_of(s, allocator, t->Vector.elem);
|
||||
size *= t->Vector.count;
|
||||
size = next_pow2(size);
|
||||
// TODO(bill): Type_Vector type_align_of
|
||||
return gb_clamp(size, s.max_align, 4*s.max_align);
|
||||
} break;
|
||||
|
||||
case Type_Structure: {
|
||||
if (!t->structure.is_packed) {
|
||||
case Type_Struct: {
|
||||
if (!t->Struct.is_packed) {
|
||||
i64 max = 1;
|
||||
for (isize i = 0; i < t->structure.field_count; i++) {
|
||||
i64 align = type_align_of(s, allocator, t->structure.fields[i]->type);
|
||||
for (isize i = 0; i < t->Struct.field_count; i++) {
|
||||
i64 align = type_align_of(s, allocator, t->Struct.fields[i]->type);
|
||||
if (max < align)
|
||||
max = align;
|
||||
}
|
||||
@@ -606,8 +616,18 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
}
|
||||
} break;
|
||||
|
||||
case Type_Enumeration:
|
||||
return type_align_of(s, allocator, t->enumeration.base);
|
||||
case Type_Union: {
|
||||
i64 max = 1;
|
||||
for (isize i = 0; i < t->Union.field_count; i++) {
|
||||
i64 align = type_align_of(s, allocator, t->Union.fields[i]->type);
|
||||
if (max < align)
|
||||
max = align;
|
||||
}
|
||||
return max;
|
||||
} break;
|
||||
|
||||
case Type_Enum:
|
||||
return type_align_of(s, allocator, t->Enum.base);
|
||||
}
|
||||
|
||||
return gb_clamp(next_pow2(type_size_of(s, allocator, t)), 1, s.max_align);
|
||||
@@ -635,10 +655,10 @@ i64 *type_set_offsets_of(BaseTypeSizes s, gbAllocator allocator, Entity **fields
|
||||
}
|
||||
|
||||
b32 type_set_offsets(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
GB_ASSERT(t->kind == Type_Structure);
|
||||
if (!t->structure.are_offsets_set) {
|
||||
t->structure.offsets = type_set_offsets_of(s, allocator, t->structure.fields, t->structure.field_count, t->structure.is_packed);
|
||||
t->structure.are_offsets_set = true;
|
||||
GB_ASSERT(t->kind == Type_Struct);
|
||||
if (!t->Struct.are_offsets_set) {
|
||||
t->Struct.offsets = type_set_offsets_of(s, allocator, t->Struct.fields, t->Struct.field_count, t->Struct.is_packed);
|
||||
t->Struct.are_offsets_set = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -649,7 +669,7 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
switch (t->kind) {
|
||||
case Type_Basic: {
|
||||
GB_ASSERT(is_type_typed(t));
|
||||
BasicKind kind = t->basic.kind;
|
||||
BasicKind kind = t->Basic.kind;
|
||||
if (kind < gb_count_of(basic_type_sizes)) {
|
||||
i64 size = basic_type_sizes[kind];
|
||||
if (size > 0)
|
||||
@@ -660,22 +680,22 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
} break;
|
||||
|
||||
case Type_Array: {
|
||||
i64 count = t->array.count;
|
||||
i64 count = t->Array.count;
|
||||
if (count == 0)
|
||||
return 0;
|
||||
i64 align = type_align_of(s, allocator, t->array.elem);
|
||||
i64 size = type_size_of(s, allocator, t->array.elem);
|
||||
i64 align = type_align_of(s, allocator, t->Array.elem);
|
||||
i64 size = type_size_of(s, allocator, t->Array.elem);
|
||||
i64 alignment = align_formula(size, align);
|
||||
return alignment*(count-1) + size;
|
||||
} break;
|
||||
|
||||
case Type_Vector: {
|
||||
i64 count = t->vector.count;
|
||||
i64 count = t->Vector.count;
|
||||
if (count == 0)
|
||||
return 0;
|
||||
// i64 align = type_align_of(s, allocator, t->vector.elem);
|
||||
i64 bit_size = 8*type_size_of(s, allocator, t->vector.elem);
|
||||
if (is_type_boolean(t->vector.elem)) {
|
||||
// i64 align = type_align_of(s, allocator, t->Vector.elem);
|
||||
i64 bit_size = 8*type_size_of(s, allocator, t->Vector.elem);
|
||||
if (is_type_boolean(t->Vector.elem)) {
|
||||
bit_size = 1; // NOTE(bill): LLVM can store booleans as 1 bit because a boolean _is_ an `i1`
|
||||
// Silly LLVM spec
|
||||
}
|
||||
@@ -691,16 +711,27 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
case Type_Slice: // ptr + len + cap
|
||||
return 3 * s.word_size;
|
||||
|
||||
case Type_Structure: {
|
||||
i64 count = t->structure.field_count;
|
||||
case Type_Struct: {
|
||||
i64 count = t->Struct.field_count;
|
||||
if (count == 0)
|
||||
return 0;
|
||||
type_set_offsets(s, allocator, t);
|
||||
return t->structure.offsets[count-1] + type_size_of(s, allocator, t->structure.fields[count-1]->type);
|
||||
return t->Struct.offsets[count-1] + type_size_of(s, allocator, t->Struct.fields[count-1]->type);
|
||||
} break;
|
||||
|
||||
case Type_Enumeration:
|
||||
return type_size_of(s, allocator, t->enumeration.base);
|
||||
case Type_Union: {
|
||||
i64 count = t->Union.field_count;
|
||||
i64 max = 0;
|
||||
for (isize i = 0; i < count; i++) {
|
||||
i64 size = type_size_of(s, allocator, t->Struct.fields[i]->type);
|
||||
if (max < size)
|
||||
max = size;
|
||||
}
|
||||
return max;
|
||||
} break;
|
||||
|
||||
case Type_Enum:
|
||||
return type_size_of(s, allocator, t->Enum.base);
|
||||
}
|
||||
|
||||
// Catch all
|
||||
@@ -708,10 +739,10 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
}
|
||||
|
||||
i64 type_offset_of(BaseTypeSizes s, gbAllocator allocator, Type *t, isize index) {
|
||||
if (t->kind == Type_Structure) {
|
||||
if (t->kind == Type_Struct) {
|
||||
type_set_offsets(s, allocator, t);
|
||||
if (gb_is_between(index, 0, t->structure.field_count-1)) {
|
||||
return t->structure.offsets[index];
|
||||
if (gb_is_between(index, 0, t->Struct.field_count-1)) {
|
||||
return t->Struct.offsets[index];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -725,31 +756,31 @@ gbString write_type_to_string(gbString str, Type *type) {
|
||||
|
||||
switch (type->kind) {
|
||||
case Type_Basic:
|
||||
str = gb_string_append_length(str, type->basic.name.text, type->basic.name.len);
|
||||
str = gb_string_append_length(str, type->Basic.name.text, type->Basic.name.len);
|
||||
break;
|
||||
|
||||
case Type_Array:
|
||||
str = gb_string_appendc(str, gb_bprintf("[%td]", type->array.count));
|
||||
str = write_type_to_string(str, type->array.elem);
|
||||
str = gb_string_appendc(str, gb_bprintf("[%td]", type->Array.count));
|
||||
str = write_type_to_string(str, type->Array.elem);
|
||||
break;
|
||||
|
||||
case Type_Vector:
|
||||
str = gb_string_appendc(str, gb_bprintf("{%td}", type->vector.count));
|
||||
str = write_type_to_string(str, type->vector.elem);
|
||||
str = gb_string_appendc(str, gb_bprintf("{%td}", type->Vector.count));
|
||||
str = write_type_to_string(str, type->Vector.elem);
|
||||
break;
|
||||
|
||||
case Type_Slice:
|
||||
str = gb_string_appendc(str, "[]");
|
||||
str = write_type_to_string(str, type->array.elem);
|
||||
str = write_type_to_string(str, type->Array.elem);
|
||||
break;
|
||||
|
||||
case Type_Structure: {
|
||||
case Type_Struct: {
|
||||
str = gb_string_appendc(str, "struct{");
|
||||
for (isize i = 0; i < type->structure.field_count; i++) {
|
||||
Entity *f = type->structure.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, "; ");
|
||||
str = gb_string_appendc(str, ", ");
|
||||
str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
|
||||
str = gb_string_appendc(str, ": ");
|
||||
str = write_type_to_string(str, f->type);
|
||||
@@ -757,19 +788,33 @@ gbString write_type_to_string(gbString str, Type *type) {
|
||||
str = gb_string_appendc(str, "}");
|
||||
} break;
|
||||
|
||||
case Type_Enumeration: {
|
||||
case Type_Union: {
|
||||
str = gb_string_appendc(str, "union{");
|
||||
for (isize i = 0; i < type->Union.field_count; i++) {
|
||||
Entity *f = type->Union.fields[i];
|
||||
GB_ASSERT(f->kind == Entity_Variable);
|
||||
if (i > 0)
|
||||
str = gb_string_appendc(str, ", ");
|
||||
str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
|
||||
str = gb_string_appendc(str, ": ");
|
||||
str = write_type_to_string(str, f->type);
|
||||
}
|
||||
str = gb_string_appendc(str, "}");
|
||||
} break;
|
||||
|
||||
case Type_Enum: {
|
||||
str = gb_string_appendc(str, "enum ");
|
||||
str = write_type_to_string(str, type->enumeration.base);
|
||||
str = write_type_to_string(str, type->Enum.base);
|
||||
} break;
|
||||
|
||||
case Type_Pointer:
|
||||
str = gb_string_appendc(str, "^");
|
||||
str = write_type_to_string(str, type->pointer.elem);
|
||||
str = write_type_to_string(str, type->Pointer.elem);
|
||||
break;
|
||||
|
||||
case Type_Named:
|
||||
if (type->named.type_name != NULL) {
|
||||
str = gb_string_append_length(str, type->named.name.text, type->named.name.len);
|
||||
if (type->Named.type_name != NULL) {
|
||||
str = gb_string_append_length(str, type->Named.name.text, type->Named.name.len);
|
||||
} else {
|
||||
// NOTE(bill): Just in case
|
||||
str = gb_string_appendc(str, "<named type>");
|
||||
@@ -777,9 +822,9 @@ gbString write_type_to_string(gbString str, Type *type) {
|
||||
break;
|
||||
|
||||
case Type_Tuple:
|
||||
if (type->tuple.variable_count > 0) {
|
||||
for (isize i = 0; i < type->tuple.variable_count; i++) {
|
||||
Entity *var = type->tuple.variables[i];
|
||||
if (type->Tuple.variable_count > 0) {
|
||||
for (isize i = 0; i < type->Tuple.variable_count; i++) {
|
||||
Entity *var = type->Tuple.variables[i];
|
||||
if (var != NULL) {
|
||||
GB_ASSERT(var->kind == Entity_Variable);
|
||||
if (i > 0)
|
||||
@@ -792,12 +837,12 @@ gbString write_type_to_string(gbString str, Type *type) {
|
||||
|
||||
case Type_Proc:
|
||||
str = gb_string_appendc(str, "proc(");
|
||||
if (type->proc.params)
|
||||
str = write_type_to_string(str, type->proc.params);
|
||||
if (type->Proc.params)
|
||||
str = write_type_to_string(str, type->Proc.params);
|
||||
str = gb_string_appendc(str, ")");
|
||||
if (type->proc.results) {
|
||||
if (type->Proc.results) {
|
||||
str = gb_string_appendc(str, " -> ");
|
||||
str = write_type_to_string(str, type->proc.results);
|
||||
str = write_type_to_string(str, type->Proc.results);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user