summaryrefslogtreecommitdiff
path: root/types.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-03-27 21:24:32 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2020-03-27 21:24:32 -0400
commit0848ff3bb41572d2e850360f220369ccd62c715b (patch)
treef94fc3f9a549fce6ce226729d47baf0e5285dd01 /types.c
parent7cf5a8735ecbd37faf5e9f740d5f1a49939eea07 (diff)
removed StructDef.constants
Diffstat (limited to 'types.c')
-rw-r--r--types.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/types.c b/types.c
index 3ecb90b..a9e3ef6 100644
--- a/types.c
+++ b/types.c
@@ -780,7 +780,6 @@ static Status add_block_to_struct(Typer *tr, Block *b, StructDef *s, Statement *
err_print(d->where, "struct members can't be inferred.");
return false;
}
- *(Declaration **)typer_arr_add(tr, &s->constants) = d;
*(Statement *)typer_arr_add(tr, new_stmts) = *stmt;
} else {
if (flags & DECL_SEMI_CONST) {
@@ -887,7 +886,6 @@ static Status type_resolve(Typer *tr, Type *t, Location where) {
if (!types_block(tr, &s->body))
return false;
s->fields = NULL;
- s->constants = NULL;
Statement *new_stmts = NULL;
if (!add_block_to_struct(tr, &s->body, s, &new_stmts))
return false;
@@ -2889,9 +2887,6 @@ static Status types_expr(Typer *tr, Expression *e) {
return false;
}
Value field_name;
-
- /* replace with BINARY_DOT */
- e->binary.op = BINARY_DOT;
if (!eval_expr(tr->evalr, rhs, &field_name)) return false;
/* get the field, if it exists */
@@ -2899,8 +2894,21 @@ static Status types_expr(Typer *tr, Expression *e) {
field_name.slice.data, (size_t)field_name.slice.n);
if (ident_is_declared(ident)) {
assert(ident->decl_kind == IDECL_DECL);
- Field *f = ident->decl->field + ident_index_in_decl(ident, ident->decl);
- e->binary.dot.field = f;
+ Declaration *decl = ident->decl;
+ if (decl->flags & DECL_IS_CONST) {
+ /* replace with value of struct constant */
+ e->kind = EXPR_VAL;
+ assert(decl->flags & DECL_FOUND_VAL);
+ int idx = ident_index_in_decl(ident, decl);
+ *t = *decl_type_at_index(decl, idx);
+ copy_val(tr->allocr, &e->val, *decl_val_at_index(decl, idx), t);
+ } else {
+ /* replace with BINARY_DOT */
+ e->binary.op = BINARY_DOT;
+ Field *f = decl->field + ident_index_in_decl(ident, decl);
+ e->binary.dot.field = f;
+ *t = *f->type;
+ }
} else {
char *fstr = err_malloc((size_t)(field_name.slice.n + 1));
memcpy(fstr, field_name.slice.data, (size_t)field_name.slice.n);