diff options
-rw-r--r-- | cgen.c | 12 | ||||
-rw-r--r-- | copy.c | 6 | ||||
-rw-r--r-- | eval.c | 8 | ||||
-rw-r--r-- | package.c | 13 | ||||
-rw-r--r-- | parse.c | 26 | ||||
-rw-r--r-- | test.toc | 18 | ||||
-rw-r--r-- | types.c | 42 | ||||
-rw-r--r-- | types.h | 12 |
8 files changed, 65 insertions, 72 deletions
@@ -55,8 +55,6 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d); case EXPR_TYPE: \ case EXPR_VAL: \ case EXPR_C: \ - case EXPR_DSIZEOF: \ - case EXPR_DALIGNOF: \ case EXPR_IDENT: \ case EXPR_LITERAL_BOOL: \ case EXPR_LITERAL_INT: \ @@ -737,8 +735,6 @@ static bool cgen_set_tuple(CGenerator *g, Expression *exprs, Identifier *idents, case EXPR_CAST: case EXPR_NEW: case EXPR_C: - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: case EXPR_TYPE: case EXPR_PKG: assert(0); @@ -1148,8 +1144,6 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) { case EXPR_IDENT: case EXPR_FN: case EXPR_C: - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: case EXPR_TYPE: case EXPR_PKG: break; @@ -1340,6 +1334,10 @@ static bool cgen_expr(CGenerator *g, Expression *e) { } handled = true; } break; + case UNARY_DSIZEOF: + case UNARY_DALIGNOF: + assert(0); + return false; } if (handled) break; cgen_write(g, "("); @@ -1443,8 +1441,6 @@ static bool cgen_expr(CGenerator *g, Expression *e) { cgen_write(g, ")"); } } break; - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: /* handled by types.c */ case EXPR_TUPLE: /* the only time this should happen is if you're stating a tuple, e.g. 3, 5;, but we've errored about that before @@ -247,12 +247,6 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) { case EXPR_C: copy_expr(c, out->c.code = allocr_malloc(a, sizeof *out->c.code), in->c.code); break; - case EXPR_DSIZEOF: - copy_expr(c, out->dsizeof.of = allocr_malloc(a, sizeof *out->dsizeof.of), in->dsizeof.of); - break; - case EXPR_DALIGNOF: - copy_expr(c, out->dalignof.of = allocr_malloc(a, sizeof *out->dalignof.of), in->dalignof.of); - break; case EXPR_SLICE: { SliceExpr *sin = &in->slice; SliceExpr *sout = &out->slice; @@ -1074,6 +1074,10 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { default: assert(0); break; } break; + case UNARY_DSIZEOF: + case UNARY_DALIGNOF: + assert(0); + return false; } } break; case EXPR_BINARY_OP: { @@ -1522,10 +1526,6 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { case EXPR_PKG: v->pkg = e->pkg.name_ident->pkg; break; - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: - assert(0); - return false; } return true; } @@ -317,7 +317,7 @@ static bool export_type(Exporter *ex, Type *type, Location where) { struc->export.id = (U32)nexported_structs; } - export_len(ex, (size_t)struc->export.id); + export_vlq(ex, (U64)struc->export.id); } break; case TYPE_EXPR: if (!export_expr(ex, type->expr)) @@ -378,6 +378,13 @@ static void import_type(Importer *im, Type *type) { type->fn.constness[i] = import_u8(im); } else type->fn.constness = NULL; } break; + case TYPE_STRUCT: { + U64 struct_id = import_vlq(im); + type->struc = &im->structs[struct_id]; + } break; + case TYPE_EXPR: + import_expr(im, type->expr = imptr_new_expr(im)); + break; } } @@ -655,10 +662,6 @@ static bool export_expr(Exporter *ex, Expression *e) { if (!export_block(ex, &ea->body)) return false; } break; - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: - assert(0); - break; } return true; } @@ -36,8 +36,6 @@ static const char *expr_kind_to_str(ExprKind k) { case EXPR_EACH: return "each expression"; case EXPR_CALL: return "function call"; case EXPR_C: return "c code"; - case EXPR_DSIZEOF: return "#sizeof"; - case EXPR_DALIGNOF: return "#alignof"; case EXPR_NEW: return "new expression"; case EXPR_CAST: return "cast expression"; case EXPR_UNARY_OP: return "unary operator"; @@ -63,6 +61,8 @@ static const char *unary_op_to_str(UnaryOp u) { case UNARY_NOT: return "!"; case UNARY_DEL: return "del"; case UNARY_LEN: return "len"; + case UNARY_DSIZEOF: return "#sizeof"; + case UNARY_DALIGNOF: return "#alignof"; } assert(0); return ""; @@ -1667,12 +1667,14 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { single_arg = e->c.code = parser_new_expr(p); break; case DIRECT_SIZEOF: - e->kind = EXPR_DSIZEOF; - single_arg = e->dsizeof.of = parser_new_expr(p); + e->kind = EXPR_UNARY_OP; + e->unary.op = UNARY_DSIZEOF; + single_arg = e->unary.of = parser_new_expr(p); break; case DIRECT_ALIGNOF: - e->kind = EXPR_DALIGNOF; - single_arg = e->dalignof.of = parser_new_expr(p); + e->kind = EXPR_UNARY_OP; + e->unary.op = UNARY_DALIGNOF; + single_arg = e->unary.of = parser_new_expr(p); break; case DIRECT_EXPORT: tokr_err(t, "Unrecognized expression."); @@ -2222,16 +2224,6 @@ static void fprint_expr(FILE *out, Expression *e) { fprint_expr(out, e->c.code); fprintf(out, ")"); break; - case EXPR_DSIZEOF: - fprintf(out, "#sizeof("); - fprint_expr(out, e->dsizeof.of); - fprintf(out, ")"); - break; - case EXPR_DALIGNOF: - fprintf(out, "#alignof("); - fprint_expr(out, e->dalignof.of); - fprintf(out, ")"); - break; case EXPR_SLICE: { SliceExpr *s = &e->slice; fprint_expr(out, s->of); @@ -2349,8 +2341,6 @@ static bool expr_is_definitely_const(Expression *e) { case EXPR_LITERAL_CHAR: case EXPR_LITERAL_STR: case EXPR_LITERAL_BOOL: - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: case EXPR_TYPE: case EXPR_VAL: case EXPR_PKG: @@ -9,6 +9,20 @@ putf ::= fn(x: float) { -point ::= pkg "point"; +// point ::= pkg "point"; + + main ::= fn() { -};
\ No newline at end of file + s ::= struct { x,y,z:int; f:f32; }; + puti(#alignof(s)); + puti(#sizeof(s)); + +}; +/* +something's wrong (should be too few opening parentheses!) +main ::= fn() { + puti(#alignof(struct { x,y,z:int; f:f64; }); + puti(#sizeof(struct { x,y,z:int; f:f64; }); + +}; +*/
\ No newline at end of file @@ -168,8 +168,6 @@ static bool expr_must_lval(Expression *e) { case EXPR_EACH: case EXPR_CALL: case EXPR_C: - case EXPR_DALIGNOF: - case EXPR_DSIZEOF: case EXPR_BLOCK: case EXPR_SLICE: case EXPR_TYPE: @@ -1645,23 +1643,6 @@ static bool types_expr(Typer *tr, Expression *e) { code->kind = EXPR_VAL; t->kind = TYPE_UNKNOWN; } break; - case EXPR_DSIZEOF: - case EXPR_DALIGNOF: { - Expression *of = e->kind == EXPR_DSIZEOF ? e->dsizeof.of : e->dalignof.of; - if (!types_expr(tr, of)) - return false; - if (type_is_builtin(&of->type, BUILTIN_TYPE)) { - Value val; - if (!eval_expr(tr->evalr, of, &val)) - return false; - e->val.i64 = (I64)(e->kind == EXPR_DSIZEOF ? compiler_sizeof : compiler_alignof)(val.type); - } else { - e->val.i64 = (I64)(e->kind == EXPR_DSIZEOF ? compiler_sizeof : compiler_alignof)(&of->type); - } - e->kind = EXPR_VAL; - t->kind = TYPE_BUILTIN; - t->builtin = BUILTIN_I64; - } break; case EXPR_UNARY_OP: { Expression *of = e->unary.of; Type *of_type = &of->type; @@ -1741,7 +1722,28 @@ static bool types_expr(Typer *tr, Expression *e) { return false; } break; - } + case UNARY_DSIZEOF: + case UNARY_DALIGNOF: { + if (!types_expr(tr, of)) + return false; + Type *queried_type; + if (type_is_builtin(&of->type, BUILTIN_TYPE)) { + Value val; + if (!eval_expr(tr->evalr, of, &val)) + return false; + queried_type = val.type; + } else { + queried_type = &of->type; + } + if (e->unary.op == UNARY_DSIZEOF) + e->val.i64 = (I64)compiler_sizeof(queried_type); + else + e->val.i64 = (I64)compiler_alignof(queried_type); + e->kind = EXPR_VAL; + t->kind = TYPE_BUILTIN; + t->builtin = BUILTIN_I64; + } break; + } } break; case EXPR_BINARY_OP: { Expression *lhs = e->binary.lhs; @@ -476,8 +476,6 @@ typedef enum { EXPR_BLOCK, EXPR_TUPLE, EXPR_C, - EXPR_DSIZEOF, - EXPR_DALIGNOF, EXPR_SLICE, EXPR_TYPE, EXPR_PKG, @@ -495,7 +493,9 @@ typedef enum { UNARY_DEREF, /* *x */ UNARY_NOT, /* !x */ UNARY_DEL, /* del x */ - UNARY_LEN /* x.len ; replaces BINARY_DOT len when typing */ + UNARY_LEN, /* x.len ; replaces BINARY_DOT len when typing */ + UNARY_DSIZEOF, + UNARY_DALIGNOF } UnaryOp; typedef enum { @@ -655,12 +655,6 @@ typedef struct Expression { struct { struct Expression *code; } c; - struct { - struct Expression *of; - } dsizeof; /* #sizeof directive */ - struct { - struct Expression *of; - } dalignof; /* #alignof directive */ Identifier ident; NewExpr new; struct { |