summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--copy.c6
-rw-r--r--eval.c1
-rw-r--r--parse.c1
-rw-r--r--test.toc6
-rw-r--r--types.c62
5 files changed, 37 insertions, 39 deletions
diff --git a/copy.c b/copy.c
index ccbb4cb..e90eb55 100644
--- a/copy.c
+++ b/copy.c
@@ -85,6 +85,7 @@ static void copy_struct(Copier *c, StructDef *out, StructDef *in) {
idents_create(&out->scope.idents, c->allocr, &out->scope);
Block *prev = c->block;
+ copy_block(c, &out->scope, &in->scope, 0);
c->block = &out->scope;
arr_set_lena(&out->fields, nfields, c->allocr);
@@ -106,7 +107,6 @@ static void copy_struct(Copier *c, StructDef *out, StructDef *in) {
for (size_t i = 0; i < nconstants; ++i) {
copy_decl(c, &out->constants[i], &in->constants[i]);
}
-
c->block = prev;
}
@@ -207,8 +207,8 @@ static void copy_fn_expr(Copier *c, FnExpr *fout, FnExpr *fin, bool copy_body) {
}
copy_type(c, &fout->ret_type, &fin->ret_type);
if (copy_body) {
- copy_block(c, &fout->body, &fin->body, COPY_BLOCK_DONT_CREATE_IDENTS);
c->block = prev;
+ copy_block(c, &fout->body, &fin->body, copy_body ? COPY_BLOCK_DONT_CREATE_IDENTS : 0);
}
}
}
@@ -385,6 +385,7 @@ static void copy_decl(Copier *c, Declaration *out, Declaration *in) {
out->idents[i] = in->idents[i];
assert(c->block);
copier_ident_translate(c, &out->idents[i]);
+
out->idents[i]->decl_kind = IDECL_DECL;
out->idents[i]->decl = out;
}
@@ -418,6 +419,7 @@ static void copy_stmt(Copier *c, Statement *out, Statement *in) {
}
}
+/* COPY_BLOCK_DONT_CREATE_IDENTS is for copy_fn_expr */
static void copy_block(Copier *c, Block *out, Block *in, U8 flags) {
assert(!(in->flags & BLOCK_FINDING_TYPES));
Identifiers out_idents = out->idents;
diff --git a/eval.c b/eval.c
index f156bb3..31c0472 100644
--- a/eval.c
+++ b/eval.c
@@ -645,7 +645,6 @@ static Value *ident_val(Identifier i) {
return valp;
} else {
/* struct parameter */
- printf("--%p\n",decl);
assert(decl->flags & DECL_FOUND_VAL);
if (arr_len(decl->idents) > 1)
return &decl->val.tuple[idx];
diff --git a/parse.c b/parse.c
index d9af50e..92d106a 100644
--- a/parse.c
+++ b/parse.c
@@ -680,6 +680,7 @@ static Status parse_type(Parser *p, Type *type, Location *where) {
++t->token;
struc->where.end = t->token;
}
+ struc->scope.where = struc->where;
p->block = prev_block;
break;
diff --git a/test.toc b/test.toc
index 2eb2814..076d0a7 100644
--- a/test.toc
+++ b/test.toc
@@ -24,11 +24,11 @@ main ::= fn() {
*/
foo ::= fn(x::int) Type {
- struct {
- a: [x]int;
+ struct (t :: Type) {
+ a: [x]t;
}
};
main ::= fn() {
- f: foo(5);
+ f: foo(5)(int);
}; \ No newline at end of file
diff --git a/types.c b/types.c
index 0fd160a..128a36f 100644
--- a/types.c
+++ b/types.c
@@ -566,36 +566,34 @@ static Status type_of_ident(Typer *tr, Location where, Identifier *ident, Type *
assert(i->idents->scope == tr->block);
#endif
#endif
- if (i->decl_kind == IDECL_NONE) {
- Block *b = tr->block;
- bool undeclared = false;
- while (1) {
- /* OPTIM: only hash once */
- Identifier translated = ident_translate(i, b ? &b->idents : tr->globals);
- if (translated && translated->decl_kind != IDECL_NONE) {
- /* printf("translated %s from\n", ident_to_str(i)); */
- /* print_block_location(i->idents->scope); */
- /* printf(" to \n"); */
- /* print_block_location(translated->idents->scope); */
-
- i = *ident = translated;
- break;
- }
- if (b) {
- b = b->parent;
- } else {
- undeclared = true;
- break;
- }
+ Block *b = tr->block;
+ bool undeclared = false;
+ while (1) {
+ /* OPTIM: only hash once */
+ Identifier translated = ident_translate(i, b ? &b->idents : tr->globals);
+ if (translated && translated->decl_kind != IDECL_NONE) {
+#if 0
+ printf("translated %s from\n", ident_to_str(i));
+ print_block_location(i->idents->scope);
+ printf(" to \n");
+ print_block_location(translated->idents->scope);
+#endif
+ i = *ident = translated;
+ break;
}
- if (undeclared) {
- char *s = ident_to_str(i);
- err_print(where, "Undeclared identifier: %s", s);
- free(s);
- return false;
+ if (b) {
+ b = b->parent;
+ } else {
+ undeclared = true;
+ break;
}
}
-
+ if (undeclared) {
+ char *s = ident_to_str(i);
+ err_print(where, "Undeclared identifier: %s", s);
+ free(s);
+ return false;
+ }
switch (i->decl_kind) {
case IDECL_DECL: {
Declaration *d = i->decl;
@@ -661,7 +659,7 @@ static Status type_of_ident(Typer *tr, Location where, Identifier *ident, Type *
} else {
/* let's type the declaration, and redo this (for evaling future functions) */
if (!types_decl(tr, d)) return false;
- return type_of_ident(tr, where, ident, t);
+ return type_of_ident(tr, where, ident, t);
}
return false;
}
@@ -1736,7 +1734,6 @@ static Status types_expr(Typer *tr, Expression *e) {
CallExpr *c = &e->call;
c->instance = NULL;
Expression *f = c->fn;
- Copier cop = {0};
FnExpr *fn_decl = NULL;
if (!types_expr(tr, f)) return false;
arr_foreach(c->args, Argument, arg) {
@@ -1771,7 +1768,7 @@ static Status types_expr(Typer *tr, Expression *e) {
info_print(base->struc->where, "struct was declared here.");
return false;
}
- cop = copier_create(tr->allocr, tr->block);
+ Copier cop = copier_create(tr->allocr, base->struc->scope.parent);
HashTable *table = &base->struc->instances;
StructDef struc;
copy_struct(&cop, &struc, base->struc);
@@ -1941,7 +1938,6 @@ static Status types_expr(Typer *tr, Expression *e) {
Type table_index_type = {0};
Value table_index = {0};
FnExpr *fn_copy = NULL;
- cop = copier_create(tr->allocr, tr->block);
if (fn_type->constness) {
/* evaluate compile-time arguments + add an instance */
@@ -1955,6 +1951,7 @@ static Status types_expr(Typer *tr, Expression *e) {
/* fn is the instance, original_fn is not */
original_fn = fn;
fn_copy = typer_malloc(tr, sizeof *fn_copy);
+ Copier cop = copier_create(tr->allocr, fn->body.parent);
copy_fn_expr(&cop, fn_copy, fn, true);
fn = fn_copy;
/* keep track of the declaration */
@@ -2070,7 +2067,6 @@ static Status types_expr(Typer *tr, Expression *e) {
arg_exprs[i].flags = EXPR_FOUND_TYPE;
arg_exprs[i].val = val_copy;
param_decl->flags |= DECL_FOUND_VAL;
- printf("%p\n",param_decl);
copy_val(tr->allocr, &param_decl->val, &val_copy, type);
if (!(param_decl->flags & DECL_ANNOTATES_TYPE)) {
param_decl->type = *type;
@@ -2099,7 +2095,7 @@ static Status types_expr(Typer *tr, Expression *e) {
}
}
/* type params, return declarations, etc */
- if (!type_of_fn(tr, fn_copy, &f->type, TYPE_OF_FN_IS_INSTANCE))
+ if (!type_of_fn(tr, fn, &f->type, TYPE_OF_FN_IS_INSTANCE))
return false;
/* deal with default arguments */