mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-31 11:50:07 +00:00
Begin work on const llvm aggregate literals
This commit is contained in:
@@ -619,9 +619,11 @@ void add_type_and_value(CheckerInfo *i, AstNode *expression, AddressingMode mode
|
||||
return;
|
||||
|
||||
if (mode == Addressing_Constant) {
|
||||
GB_ASSERT(value.kind != ExactValue_Invalid);
|
||||
if (!(type != t_invalid || is_type_constant_type(type))) {
|
||||
compiler_error("add_type_and_value - invalid type: %s", type_to_string(type));
|
||||
if (is_type_constant_type(type)) {
|
||||
GB_ASSERT(value.kind != ExactValue_Invalid);
|
||||
if (!(type != t_invalid || is_type_constant_type(type))) {
|
||||
compiler_error("add_type_and_value - invalid type: %s", type_to_string(type));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,8 @@ struct Entity {
|
||||
b8 anonymous; // Variable is an anonymous
|
||||
b8 is_using; // `using` variable
|
||||
|
||||
i32 field_index; // Order in source
|
||||
i32 field_index;
|
||||
i32 field_src_index;
|
||||
b8 is_field; // Is struct field
|
||||
} Variable;
|
||||
struct {
|
||||
@@ -124,9 +125,10 @@ Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type,
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type, b32 is_anonymous, i32 field_index) {
|
||||
Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type, b32 is_anonymous, i32 field_src_index) {
|
||||
Entity *entity = make_entity_variable(a, scope, token, type);
|
||||
entity->Variable.field_index = field_index;
|
||||
entity->Variable.field_src_index = field_src_index;
|
||||
entity->Variable.field_index = field_src_index;
|
||||
entity->Variable.is_field = true;
|
||||
entity->Variable.anonymous = cast(b8)is_anonymous;
|
||||
entity->Variable.is_using = cast(b8)is_anonymous;
|
||||
|
||||
+35
-12
@@ -501,13 +501,16 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecke
|
||||
}
|
||||
|
||||
// NOTE(bill): Hacky thing
|
||||
// TODO(bill): Probably make an inline sorting procedure rather than use global variables
|
||||
__checker_sizes = c->sizes;
|
||||
__checker_allocator = c->allocator;
|
||||
// TODO(bill): Figure out rules more
|
||||
// sorting rules
|
||||
// compound literal order must match source not layout
|
||||
// NOTE(bill): compound literal order must match source not layout
|
||||
gb_sort_array(reordered_fields, field_count, cmp_struct_entity_size);
|
||||
|
||||
for (isize i = 0; i < field_count; i++) {
|
||||
reordered_fields[i]->Variable.field_index = i;
|
||||
}
|
||||
|
||||
struct_type->Record.fields = reordered_fields;
|
||||
}
|
||||
|
||||
@@ -3256,6 +3259,7 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
|
||||
case_ast_node(cl, CompoundLit, node);
|
||||
Type *type = type_hint;
|
||||
b32 ellipsis_array = false;
|
||||
b32 is_constant = true;
|
||||
if (cl->type != NULL) {
|
||||
type = NULL;
|
||||
|
||||
@@ -3297,15 +3301,15 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
|
||||
"Mixture of `field = value` and value elements in a structure literal is not allowed");
|
||||
continue;
|
||||
}
|
||||
ast_node(kv, FieldValue, elem);
|
||||
if (kv->field->kind != AstNode_Ident) {
|
||||
gbString expr_str = expr_to_string(kv->field);
|
||||
ast_node(fv, FieldValue, elem);
|
||||
if (fv->field->kind != AstNode_Ident) {
|
||||
gbString expr_str = expr_to_string(fv->field);
|
||||
defer (gb_string_free(expr_str));
|
||||
error(ast_node_token(elem),
|
||||
"Invalid field name `%s` in structure literal", expr_str);
|
||||
continue;
|
||||
}
|
||||
String name = kv->field->Ident.string;
|
||||
String name = fv->field->Ident.string;
|
||||
|
||||
Selection sel = lookup_field(c->allocator, type, name, o->mode == Addressing_Type);
|
||||
if (sel.entity == NULL) {
|
||||
@@ -3321,7 +3325,7 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
|
||||
}
|
||||
|
||||
Entity *field = t->Record.fields[sel.index[0]];
|
||||
add_entity_use(c, kv->field, field);
|
||||
add_entity_use(c, fv->field, field);
|
||||
|
||||
if (fields_visited[sel.index[0]]) {
|
||||
error(ast_node_token(elem),
|
||||
@@ -3330,7 +3334,12 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
|
||||
}
|
||||
|
||||
fields_visited[sel.index[0]] = true;
|
||||
check_expr(c, o, kv->value);
|
||||
check_expr(c, o, fv->value);
|
||||
|
||||
if (is_constant) {
|
||||
is_constant = o->mode == Addressing_Constant;
|
||||
}
|
||||
|
||||
check_assignment(c, o, field->type, make_string("structure literal"));
|
||||
}
|
||||
} else {
|
||||
@@ -3348,6 +3357,11 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
|
||||
error(ast_node_token(o->expr), "Too many values in structure literal, expected %td", field_count);
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_constant) {
|
||||
is_constant = o->mode == Addressing_Constant;
|
||||
}
|
||||
|
||||
check_assignment(c, o, field->type, make_string("structure literal"));
|
||||
}
|
||||
if (gb_array_count(cl->elems) < field_count) {
|
||||
@@ -3358,7 +3372,7 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
|
||||
|
||||
} break;
|
||||
|
||||
case Type_Slice:
|
||||
// case Type_Slice:
|
||||
case Type_Array:
|
||||
case Type_Vector:
|
||||
{
|
||||
@@ -3401,6 +3415,10 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
|
||||
Operand o = {};
|
||||
check_expr_with_type_hint(c, &o, e, elem_type);
|
||||
check_assignment(c, &o, elem_type, context_name);
|
||||
|
||||
if (is_constant) {
|
||||
is_constant = o.mode == Addressing_Constant;
|
||||
}
|
||||
}
|
||||
if (max < index)
|
||||
max = index;
|
||||
@@ -3419,13 +3437,18 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
|
||||
|
||||
default: {
|
||||
gbString str = type_to_string(type);
|
||||
error(ast_node_token(node), "Invalid compound literal type `%s`", str);
|
||||
error(ast_node_token(node), "Invalid or unyet supported compound literal type `%s`", str);
|
||||
gb_string_free(str);
|
||||
goto error;
|
||||
} break;
|
||||
}
|
||||
|
||||
o->mode = Addressing_Value;
|
||||
if (is_constant) {
|
||||
o->mode = Addressing_Constant;
|
||||
o->value = make_exact_value_compound(node);
|
||||
} else {
|
||||
o->mode = Addressing_Value;
|
||||
}
|
||||
o->type = type;
|
||||
case_end;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user