summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.html2
-rw-r--r--README.md2
-rw-r--r--cgen.c16
-rw-r--r--decls_cgen.c22
-rw-r--r--parse.c4
-rw-r--r--test.toc4
-rw-r--r--typedefs_cgen.c33
-rw-r--r--types.c9
-rw-r--r--types.h16
9 files changed, 54 insertions, 54 deletions
diff --git a/README.html b/README.html
index 32673e4..2bf99bb 100644
--- a/README.html
+++ b/README.html
@@ -92,8 +92,6 @@ it is nearly as fast in theory.</p>
<tr><th>Version</th><th>Description</th><th>Date</th></tr>
<tr><td>0.0</td><td>Initial version.</td><td>2019 Dec 6</td></tr>
<tr><td>0.1</td><td>Constant parameter inference.</td><td>2019 Dec 15</td></tr>
-<tr><td>0.1.1</td><td>Better constant parameter inference.</td><td>2019 Dec 16</td></tr>
-<tr><td>0.1.2</td><td>Minor bug fixes</td><td>2020 Jan 2</td></tr>
</table>
diff --git a/README.md b/README.md
index 0a62770..22ca0cf 100644
--- a/README.md
+++ b/README.md
@@ -80,8 +80,6 @@ Here are the major versions of `toc`.
<tr><th>Version</th><th>Description</th><th>Date</th></tr>
<tr><td>0.0</td><td>Initial version.</td><td>2019 Dec 6</td></tr>
<tr><td>0.1</td><td>Constant parameter inference.</td><td>2019 Dec 15</td></tr>
-<tr><td>0.1.1</td><td>Better constant parameter inference.</td><td>2019 Dec 16</td></tr>
-<tr><td>0.1.2</td><td>Minor bug fixes</td><td>2020 Jan 2</td></tr>
</table>
---
diff --git a/cgen.c b/cgen.c
index e2fc85c..941f988 100644
--- a/cgen.c
+++ b/cgen.c
@@ -153,32 +153,32 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d);
}
-#define cgen_recurse_subtypes(f, g, type, extra) \
+#define cgen_recurse_subtypes(f, g, type) \
switch (type->kind) { \
case TYPE_STRUCT: \
/* don't descend into fields */ \
break; \
case TYPE_FN: \
arr_foreach(type->fn.types, Type, sub) { \
- if (!f(g, sub, extra)) \
+ if (!f(g, sub)) \
return false; \
} \
break; \
case TYPE_TUPLE: \
arr_foreach(type->tuple, Type, sub) \
- if (!f(g, sub, extra)) \
+ if (!f(g, sub)) \
return false; \
break; \
case TYPE_ARR: \
- if (!f(g, type->arr.of, extra)) \
+ if (!f(g, type->arr.of)) \
return false; \
break; \
case TYPE_SLICE: \
- if (!f(g, type->slice, extra)) \
+ if (!f(g, type->slice)) \
return false; \
break; \
case TYPE_PTR: \
- if (!f(g, type->ptr, extra)) \
+ if (!f(g, type->ptr)) \
return false; \
break; \
case TYPE_VOID: \
@@ -356,8 +356,8 @@ static bool cgen_type_pre(CGenerator *g, Type *t, Location where) {
return false;
case TYPE_STRUCT:
cgen_write(g, "struct ");
- if (t->struc->c.name) {
- cgen_ident(g, t->struc->c.name);
+ if (t->struc->name) {
+ cgen_ident(g, t->struc->name);
} else if (t->struc->c.id) {
cgen_ident_id(g, t->struc->c.id);
} else {
diff --git a/decls_cgen.c b/decls_cgen.c
index bc6df42..179d55f 100644
--- a/decls_cgen.c
+++ b/decls_cgen.c
@@ -7,16 +7,16 @@ static bool cgen_decls_stmt(CGenerator *g, Statement *s);
static bool cgen_decls_block(CGenerator *g, Block *b);
static bool cgen_decls_decl(CGenerator *g, Declaration *d);
-static bool cgen_decls_type(CGenerator *g, Type *type, Location where) {
+static bool cgen_decls_type(CGenerator *g, Type *type) {
if (!(type->flags & TYPE_IS_RESOLVED)) /* non-instance constant fn parameter type */
return true;
if (type->kind == TYPE_STRUCT) {
StructDef *sdef = type->struc;
- if (!(sdef->flags & STRUCT_DEF_CGENERATED)) {
+ if (!(sdef->flags & STRUCT_DEF_CGEN_DEFINED)) {
/* generate struct definition */
cgen_write(g, "struct ");
- if (sdef->c.name) {
- cgen_ident(g, sdef->c.name);
+ if (sdef->name) {
+ cgen_ident(g, sdef->name);
} else {
assert(sdef->c.id);
cgen_ident_id(g, sdef->c.id);
@@ -25,20 +25,20 @@ static bool cgen_decls_type(CGenerator *g, Type *type, Location where) {
cgen_nl(g);
++g->indent_lvl;
arr_foreach(sdef->fields, Field, f) {
- if (!cgen_type_pre(g, &f->type, where)) return false;
+ if (!cgen_type_pre(g, &f->type, sdef->where)) return false;
cgen_write(g, " ");
cgen_ident(g, f->name);
- if (!cgen_type_post(g, &f->type, where)) return false;
+ if (!cgen_type_post(g, &f->type, sdef->where)) return false;
cgen_write(g, ";");
cgen_nl(g);
}
--g->indent_lvl;
cgen_write(g, "};");
cgen_nl(g);
- sdef->flags |= STRUCT_DEF_CGENERATED;
+ sdef->flags |= STRUCT_DEF_CGEN_DEFINED;
}
}
- cgen_recurse_subtypes(cgen_decls_type, g, type, where);
+ cgen_recurse_subtypes(cgen_decls_type, g, type);
return true;
}
@@ -104,11 +104,11 @@ static bool cgen_decls_expr(CGenerator *g, Expression *e) {
} break;
case EXPR_TYPE: {
Type *type = &e->typeval;
- if (!cgen_decls_type(g, type, e->where))
+ if (!cgen_decls_type(g, type))
return false;
} break;
case EXPR_CAST:
- if (!cgen_decls_type(g, &e->cast.type, e->where))
+ if (!cgen_decls_type(g, &e->cast.type))
return false;
default:
break;
@@ -131,7 +131,7 @@ static bool cgen_decls_block(CGenerator *g, Block *b) {
}
static bool cgen_decls_decl(CGenerator *g, Declaration *d) {
- if (!cgen_decls_type(g, &d->type, d->where))
+ if (!cgen_decls_type(g, &d->type))
return false;
if (cgen_fn_is_direct(g, d)) {
d->expr.fn->c.name = d->idents[0];
diff --git a/parse.c b/parse.c
index dcc5531..787c091 100644
--- a/parse.c
+++ b/parse.c
@@ -570,8 +570,8 @@ static bool parse_type(Parser *p, Type *type) {
type->kind = TYPE_STRUCT;
StructDef *struc = type->struc = parser_malloc(p, sizeof *type->struc);
struc->flags = 0;
+ struc->name = NULL;
/* help cgen out */
- struc->c.name = NULL;
struc->c.id = 0;
struc->fields = NULL;
struc->export.id = 0;
@@ -1879,7 +1879,7 @@ static bool parse_decl(Parser *p, Declaration *d, DeclEndKind ends_with, U16 fla
tokr_err(t, "You must have an expression at the end of this constant declaration.");
goto ret_false;
}
-
+
d->where.end = t->token;
switch (ends_with) {
case DECL_END_RPAREN_COMMA:
diff --git a/test.toc b/test.toc
index cd0eb78..6cc33f4 100644
--- a/test.toc
+++ b/test.toc
@@ -9,8 +9,8 @@ putf ::= fn(x: float) {
-point ::= pkg "point";
-
+// point ::= pkg "point";
+Foo ::= struct { x, y : int; };
main ::= fn() {
diff --git a/typedefs_cgen.c b/typedefs_cgen.c
index 3f7d8f1..814eb4a 100644
--- a/typedefs_cgen.c
+++ b/typedefs_cgen.c
@@ -9,20 +9,19 @@ static bool typedefs_expr(CGenerator *g, Expression *e);
/* i is the name for this type, NULL if not available */
/* ALWAYS RETURNS TRUE. it just returns a bool for cgen_recurse_into_type to work */
-static bool typedefs_type(CGenerator *g, Type *type, Identifier i) {
+static bool typedefs_type(CGenerator *g, Type *type) {
if (!(type->flags & TYPE_IS_RESOLVED)) /* non-instance constant fn parameter type */
return true;
if (type->kind == TYPE_STRUCT) {
StructDef *sdef = type->struc;
/* we'll actually define the struct later; here we can just declare it */
- if (sdef->c.id || sdef->c.name) {
+ if (sdef->flags & STRUCT_DEF_CGEN_DECLARED) {
/* we've already done this */
} else {
cgen_write(g, "struct ");
- if (i) {
- cgen_ident(g, i);
- sdef->c.name = i;
+ if (sdef->name) {
+ cgen_ident(g, sdef->name);
} else {
IdentID id = ++g->ident_counter;
cgen_ident_id(g, id);
@@ -30,9 +29,10 @@ static bool typedefs_type(CGenerator *g, Type *type, Identifier i) {
}
cgen_write(g, ";");
cgen_nl(g);
+ sdef->flags |= STRUCT_DEF_CGEN_DECLARED;
}
}
- cgen_recurse_subtypes(typedefs_type, g, type, NULL);
+ cgen_recurse_subtypes(typedefs_type, g, type);
return true;
}
@@ -52,30 +52,22 @@ static bool typedefs_block(CGenerator *g, Block *b) {
static bool typedefs_expr(CGenerator *g, Expression *e) {
cgen_recurse_subexprs(g, e, typedefs_expr, typedefs_block, typedefs_decl);
if (e->kind == EXPR_CAST) {
- typedefs_type(g, &e->cast.type, NULL);
+ typedefs_type(g, &e->cast.type);
}
if (e->kind == EXPR_FN) {
/* needs to go before decls_cgen.c... */
e->fn->c.id = ++g->ident_counter;
}
- if (e->kind == EXPR_TYPE && e->typeval.kind == TYPE_STRUCT) {
- StructDef *sdef = e->typeval.struc;
- if (sdef->c.id || sdef->c.name) {
- /* we've already done this */
- } else {
- cgen_write(g, "struct ");
- IdentID id = ++g->ident_counter;
- cgen_ident_id(g, id);
- sdef->c.id = id;
- cgen_write(g, ";");
- }
+ if (e->kind == EXPR_TYPE) {
+ if (!typedefs_type(g, &e->typeval))
+ return false;
}
return true;
}
static bool typedefs_decl(CGenerator *g, Declaration *d) {
- typedefs_type(g, &d->type, NULL);
+ typedefs_type(g, &d->type);
if (cgen_fn_is_direct(g, d)) {
d->expr.fn->c.name = d->idents[0];
}
@@ -85,7 +77,8 @@ static bool typedefs_decl(CGenerator *g, Declaration *d) {
Value *val = decl_val_at_index(d, idx);
if (type_is_builtin(type, BUILTIN_TYPE)) {
/* generate typedef */
- typedefs_type(g, val->type, i);
+ if (!typedefs_type(g, val->type))
+ return false;
if (val->type->kind != TYPE_STRUCT) {
IdentID id = 0;
diff --git a/types.c b/types.c
index 48be1d7..591cc92 100644
--- a/types.c
+++ b/types.c
@@ -2098,6 +2098,13 @@ static bool types_block(Typer *tr, Block *b) {
static bool types_decl(Typer *tr, Declaration *d) {
bool success = true;
if (d->flags & DECL_FOUND_TYPE) return true;
+
+ if ((d->flags & DECL_HAS_EXPR)
+ && d->expr.kind == EXPR_TYPE
+ && d->expr.typeval.kind == TYPE_STRUCT) {
+ d->expr.typeval.struc->name = d->idents[0];
+ }
+
if (d->flags & DECL_INFER) {
d->type.kind = TYPE_UNKNOWN;
d->type.flags = 0;
@@ -2191,6 +2198,8 @@ static bool types_decl(Typer *tr, Declaration *d) {
}
}
}
+
+
ret:
/* pretend we found the type even if we didn't to prevent too many errors */
d->flags |= DECL_FOUND_TYPE;
diff --git a/types.h b/types.h
index 8050c3a..8fd9af1 100644
--- a/types.h
+++ b/types.h
@@ -390,11 +390,6 @@ typedef struct FnType {
} FnType;
enum {
- STRUCT_DEF_FOUND_OFFSETS = 0x01,
- STRUCT_DEF_CGENERATED = 0x02,
-};
-
-enum {
TYPE_IS_FLEXIBLE = 0x01,
TYPE_IS_RESOLVED = 0x02,
};
@@ -430,14 +425,21 @@ typedef struct Field {
size_t offset; /* offset during compile time */
} Field;
+enum {
+ STRUCT_DEF_FOUND_OFFSETS = 0x01,
+ STRUCT_DEF_CGEN_DECLARED = 0x02,
+ STRUCT_DEF_CGEN_DEFINED = 0x04
+};
+
typedef struct StructDef {
Field *fields;
Location where;
- U16 flags;
+ U8 flags;
size_t size; /* size of this struct during compile time */
size_t align;
+ Identifier name;
struct {
- Identifier name;
+ /* if name is NULL, use this */
IdentID id;
} c;
struct {