Tidy up structural types for rec, tup AST and typeck nodes.

This commit is contained in:
Graydon Hoare 2010-11-30 16:31:43 -08:00
parent 567a45cc70
commit 45043374ff
5 changed files with 117 additions and 117 deletions

View File

@ -109,11 +109,14 @@ tag decl_ {
type arm = rec(@pat pat, block block);
type elt = rec(mutability mut, @expr expr);
type field = rec(mutability mut, ident ident, @expr expr);
type expr = spanned[expr_];
tag expr_ {
expr_vec(vec[@expr], ann);
expr_tup(vec[tup(mutability, @expr)], ann);
expr_rec(vec[tup(ident,@expr)], ann);
expr_tup(vec[elt], ann);
expr_rec(vec[field], ann);
expr_call(@expr, vec[@expr], ann);
expr_binary(binop, @expr, @expr, ann);
expr_unary(unop, @expr, ann);
@ -143,6 +146,7 @@ tag lit_ {
// NB: If you change this, you'll probably want to change the corresponding
// type structure in middle/typeck.rs as well.
type ty_field = rec(ident ident, @ty ty);
type ty = spanned[ty_];
tag ty_ {
ty_nil;
@ -155,7 +159,7 @@ tag ty_ {
ty_box(@ty);
ty_vec(@ty);
ty_tup(vec[@ty]);
ty_rec(vec[tup(ident,@ty)]);
ty_rec(vec[ty_field]);
ty_fn(vec[rec(mode mode, @ty ty)], @ty); // TODO: effect
ty_path(path, option.t[def]);
ty_mutable(@ty);

View File

@ -189,17 +189,17 @@ impure fn parse_ty(parser p) -> @ast.ty {
case (token.REC) {
p.bump();
impure fn parse_field(parser p) -> tup(ast.ident, @ast.ty) {
impure fn parse_field(parser p) -> ast.ty_field {
auto ty = parse_ty(p);
auto id = parse_ident(p);
ret tup(id,ty);
ret rec(ident=id, ty=ty);
}
auto f = parse_field; // FIXME: trans_const_lval bug
auto elems =
parse_seq[tup(ast.ident, @ast.ty)](token.LPAREN,
token.RPAREN,
some(token.COMMA),
f, p);
parse_seq[ast.ty_field](token.LPAREN,
token.RPAREN,
some(token.COMMA),
f, p);
hi = p.get_span();
t = ast.ty_rec(elems.node);
}
@ -352,17 +352,12 @@ impure fn parse_name(parser p, ast.ident id) -> ast.name {
ret spanned(lo, tys.span, rec(ident=id, types=tys.node));
}
impure fn parse_possibly_mutable_expr(parser p)
-> tup(ast.mutability, @ast.expr) {
auto mut;
impure fn parse_mutabliity(parser p) -> ast.mutability {
if (p.peek() == token.MUTABLE) {
p.bump();
mut = ast.mut;
} else {
mut = ast.imm;
ret ast.mut;
}
ret tup(mut, parse_expr(p));
ret ast.imm;
}
impure fn parse_bottom_expr(parser p) -> @ast.expr {
@ -415,13 +410,17 @@ impure fn parse_bottom_expr(parser p) -> @ast.expr {
case (token.TUP) {
p.bump();
auto pf = parse_possibly_mutable_expr;
impure fn parse_elt(parser p) -> ast.elt {
auto m = parse_mutabliity(p);
auto e = parse_expr(p);
ret rec(mut=m, expr=e);
}
auto pf = parse_elt;
auto es =
parse_seq[tup(ast.mutability, @ast.expr)]
(token.LPAREN,
token.RPAREN,
some(token.COMMA),
pf, p);
parse_seq[ast.elt](token.LPAREN,
token.RPAREN,
some(token.COMMA),
pf, p);
hi = es.span;
ex = ast.expr_tup(es.node, ast.ann_none);
}
@ -439,21 +438,21 @@ impure fn parse_bottom_expr(parser p) -> @ast.expr {
case (token.REC) {
p.bump();
impure fn parse_entry(parser p) ->
tup(ast.ident, @ast.expr) {
impure fn parse_field(parser p) -> ast.field {
auto m = parse_mutabliity(p);
auto i = parse_ident(p);
expect(p, token.EQ);
auto e = parse_expr(p);
ret tup(i, e);
ret rec(mut=m, ident=i, expr=e);
}
auto pf = parse_entry;
auto es =
parse_seq[tup(ast.ident, @ast.expr)](token.LPAREN,
token.RPAREN,
some(token.COMMA),
pf, p);
hi = es.span;
ex = ast.expr_rec(es.node, ast.ann_none);
auto pf = parse_field;
auto fs =
parse_seq[ast.field](token.LPAREN,
token.RPAREN,
some(token.COMMA),
pf, p);
hi = fs.span;
ex = ast.expr_rec(fs.node, ast.ann_none);
}
case (_) {

View File

@ -49,7 +49,7 @@ type ast_fold[ENV] =
(fn(&ENV e, &span sp, vec[@ty] elts) -> @ty) fold_ty_tup,
(fn(&ENV e, &span sp,
vec[tup(ident,@ty)] elts) -> @ty) fold_ty_rec,
vec[ast.ty_field] elts) -> @ty) fold_ty_rec,
(fn(&ENV e, &span sp,
vec[rec(ast.mode mode, @ty ty)] inputs,
@ -65,12 +65,10 @@ type ast_fold[ENV] =
vec[@expr] es, ann a) -> @expr) fold_expr_vec,
(fn(&ENV e, &span sp,
vec[tup(mutability,@expr)] es,
ann a) -> @expr) fold_expr_tup,
vec[ast.elt] es, ann a) -> @expr) fold_expr_tup,
(fn(&ENV e, &span sp,
vec[tup(ident,@expr)] fields,
ann a) -> @expr) fold_expr_rec,
vec[ast.field] fields, ann a) -> @expr) fold_expr_rec,
(fn(&ENV e, &span sp,
@expr f, vec[@expr] args,
@ -252,16 +250,16 @@ fn fold_ty[ENV](&ENV env, ast_fold[ENV] fld, @ty t) -> @ty {
for (@ty elt in elts) {
append[@ty](elts_,fold_ty(env, fld, elt));
}
ret fld.fold_ty_tup(env_, t.span, elts);
ret fld.fold_ty_tup(env_, t.span, elts_);
}
case (ast.ty_rec(?elts)) {
let vec[tup(ident,@ty)] elts_ = vec();
for (tup(ident, @ty) elt in elts) {
append[tup(ident, @ty)]
(elts_, tup(elt._0, fold_ty(env, fld, elt._1)));
case (ast.ty_rec(?flds)) {
let vec[ast.ty_field] flds_ = vec();
for (ast.ty_field f in flds) {
append[ast.ty_field]
(flds_, rec(ty=fold_ty(env, fld, f.ty) with f));
}
ret fld.fold_ty_rec(env_, t.span, elts);
ret fld.fold_ty_rec(env_, t.span, flds_);
}
case (ast.ty_path(?pth, ?ref_opt)) {
@ -347,14 +345,13 @@ fn fold_exprs[ENV](&ENV env, ast_fold[ENV] fld, vec[@expr] es) -> vec[@expr] {
ret exprs;
}
fn fold_tup_entry[ENV](&ENV env, ast_fold[ENV] fld,
&tup(mutability,@expr) e) -> tup(mutability,@expr) {
ret tup(e._0, fold_expr(env, fld, e._1));
fn fold_tup_elt[ENV](&ENV env, ast_fold[ENV] fld, &ast.elt e) -> ast.elt {
ret rec(expr=fold_expr(env, fld, e.expr) with e);
}
fn fold_rec_entry[ENV](&ENV env, ast_fold[ENV] fld, &tup(ident,@expr) e)
-> tup(ident,@expr) {
ret tup(e._0, fold_expr(env, fld, e._1));
fn fold_rec_field[ENV](&ENV env, ast_fold[ENV] fld, &ast.field f)
-> ast.field {
ret rec(expr=fold_expr(env, fld, f.expr) with f);
}
fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr {
@ -372,19 +369,19 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr {
}
case (ast.expr_tup(?es, ?t)) {
let vec[tup(mutability,@expr)] entries = vec();
for (tup(mutability,@expr) entry in es) {
entries += fold_tup_entry[ENV](env, fld, entry);
let vec[ast.elt] elts = vec();
for (ast.elt e in es) {
elts += fold_tup_elt[ENV](env, fld, e);
}
ret fld.fold_expr_tup(env_, e.span, entries, t);
ret fld.fold_expr_tup(env_, e.span, elts, t);
}
case (ast.expr_rec(?es, ?t)) {
let vec[tup(ident,@expr)] entries = vec();
for (tup(ident,@expr) entry in es) {
entries += fold_rec_entry(env, fld, entry);
case (ast.expr_rec(?fs, ?t)) {
let vec[ast.field] fields = vec();
for (ast.field f in fs) {
fields += fold_rec_field(env, fld, f);
}
ret fld.fold_expr_rec(env_, e.span, entries, t);
ret fld.fold_expr_rec(env_, e.span, fields, t);
}
case (ast.expr_call(?f, ?args, ?t)) {
@ -680,7 +677,7 @@ fn identity_fold_ty_tup[ENV](&ENV env, &span sp,
}
fn identity_fold_ty_rec[ENV](&ENV env, &span sp,
vec[tup(ident,@ty)] elts) -> @ty {
vec[ast.ty_field] elts) -> @ty {
ret @respan(sp, ast.ty_rec(elts));
}
@ -708,13 +705,12 @@ fn identity_fold_expr_vec[ENV](&ENV env, &span sp, vec[@expr] es,
}
fn identity_fold_expr_tup[ENV](&ENV env, &span sp,
vec[tup(mutability, @expr)] es,
ann a) -> @expr {
vec[ast.elt] es, ann a) -> @expr {
ret @respan(sp, ast.expr_tup(es, a));
}
fn identity_fold_expr_rec[ENV](&ENV env, &span sp,
vec[tup(ident,@expr)] fields, ann a) -> @expr {
vec[ast.field] fields, ann a) -> @expr {
ret @respan(sp, ast.expr_rec(fields, a));
}

View File

@ -1197,15 +1197,15 @@ impure fn trans_call(@block_ctxt cx, @ast.expr f,
args_res._0.build.FastCall(f_res._0.val, args_res._1));
}
impure fn trans_tup(@block_ctxt cx, vec[tup(ast.mutability, @ast.expr)] args,
impure fn trans_tup(@block_ctxt cx, vec[ast.elt] elts,
&ast.ann ann) -> result {
auto ty = node_type(cx.fcx.ccx, ann);
auto tup_val = cx.build.Alloca(ty);
let int i = 0;
auto r = res(cx, C_nil());
for (tup(ast.mutability, @ast.expr) arg in args) {
auto t = typeck.expr_ty(arg._1);
auto src_res = trans_expr(r.bcx, arg._1);
for (ast.elt e in elts) {
auto t = typeck.expr_ty(e.expr);
auto src_res = trans_expr(r.bcx, e.expr);
auto dst_elt = r.bcx.build.GEP(tup_val, vec(C_int(0), C_int(i)));
// FIXME: calculate copy init-ness in typestate.
r = copy_ty(src_res.bcx, true, dst_elt, src_res.val, t);
@ -1214,21 +1214,21 @@ impure fn trans_tup(@block_ctxt cx, vec[tup(ast.mutability, @ast.expr)] args,
ret res(r.bcx, tup_val);
}
impure fn trans_rec(@block_ctxt cx, vec[tup(ast.ident, @ast.expr)] args,
impure fn trans_rec(@block_ctxt cx, vec[ast.field] fields,
&ast.ann ann) -> result {
auto ty = node_type(cx.fcx.ccx, ann);
auto tup_val = cx.build.Alloca(ty);
auto rec_val = cx.build.Alloca(ty);
let int i = 0;
auto r = res(cx, C_nil());
for (tup(ast.ident, @ast.expr) arg in args) {
auto t = typeck.expr_ty(arg._1);
auto src_res = trans_expr(r.bcx, arg._1);
auto dst_elt = r.bcx.build.GEP(tup_val, vec(C_int(0), C_int(i)));
for (ast.field f in fields) {
auto t = typeck.expr_ty(f.expr);
auto src_res = trans_expr(r.bcx, f.expr);
auto dst_elt = r.bcx.build.GEP(rec_val, vec(C_int(0), C_int(i)));
// FIXME: calculate copy init-ness in typestate.
r = copy_ty(src_res.bcx, true, dst_elt, src_res.val, t);
i += 1;
}
ret res(r.bcx, tup_val);
ret res(r.bcx, rec_val);
}

View File

@ -26,7 +26,7 @@ type fn_ctxt = rec(@ty ret_ty,
@crate_ctxt ccx);
type arg = rec(ast.mode mode, @ty ty);
type field = rec(ast.ident label, @ty ty);
type field = rec(ast.ident ident, @ty ty);
// NB: If you change this, you'll probably want to change the corresponding
// AST structure in front/ast.rs as well.
@ -83,8 +83,8 @@ fn ast_ty_to_str(&@ast.ty ty) -> str {
ret s + ast_ty_to_str(input.ty);
}
fn ast_field_to_str(&tup(ast.ident, @ast.ty) f) -> str {
ret ast_ty_to_str(f._1) + " " + f._0;
fn ast_ty_field_to_str(&ast.ty_field f) -> str {
ret ast_ty_to_str(f.ty) + " " + f.ident;
}
auto s;
@ -99,18 +99,17 @@ fn ast_ty_to_str(&@ast.ty ty) -> str {
case (ast.ty_box(?t)) { s = "@" + ast_ty_to_str(t); }
case (ast.ty_vec(?t)) { s = "vec[" + ast_ty_to_str(t) + "]"; }
case (ast.ty_tup(?elems)) {
case (ast.ty_tup(?elts)) {
auto f = ast_ty_to_str;
s = "tup(";
s += _str.connect(_vec.map[@ast.ty,str](f, elems), ",");
s += _str.connect(_vec.map[@ast.ty,str](f, elts), ",");
s += ")";
}
case (ast.ty_rec(?elems)) {
auto f = ast_field_to_str;
case (ast.ty_rec(?fields)) {
auto f = ast_ty_field_to_str;
s = "rec(";
s += _str.connect(_vec.map[tup(ast.ident, @ast.ty),str]
(f, elems), ",");
s += _str.connect(_vec.map[ast.ty_field,str](f, fields), ",");
s += ")";
}
@ -172,7 +171,7 @@ fn ty_to_str(&@ty typ) -> str {
}
fn field_to_str(&field f) -> str {
ret ty_to_str(f.ty) + " " + f.label;
ret ty_to_str(f.ty) + " " + f.ident;
}
auto s = "";
@ -251,9 +250,9 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty {
}
case (ast.ty_rec(?fields)) {
let vec[field] flds = vec();
for (tup(ast.ident, @ast.ty) f in fields) {
append[field](flds, rec(label=f._0,
ty=ast_ty_to_ty(getter, f._1)));
for (ast.ty_field f in fields) {
append[field](flds, rec(ident=f.ident,
ty=ast_ty_to_ty(getter, f.ty)));
}
sty = ty_rec(flds);
}
@ -478,7 +477,7 @@ fn field_idx(session.session sess, &span sp,
&ast.ident id, vec[field] fields) -> uint {
let uint i = 0u;
for (field f in fields) {
if (_str.eq(f.label, id)) {
if (_str.eq(f.ident, id)) {
ret i;
}
i += 1u;
@ -812,11 +811,11 @@ fn unify(&fn_ctxt fcx, @ty expected, @ty actual) -> unify_result {
ret ures_err(err, expected, actual);
}
if (!_str.eq(expected_field.label,
actual_field.label)) {
if (!_str.eq(expected_field.ident,
actual_field.ident)) {
auto err =
terr_record_fields(expected_field.label,
actual_field.label);
terr_record_fields(expected_field.ident,
actual_field.ident);
ret ures_err(err, expected, actual);
}
@ -1339,40 +1338,42 @@ fn check_expr(&fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
ast.ann_type(t_1)));
}
case (ast.expr_tup(?args, _)) {
let vec[tup(mutability, @ast.expr)] args_1 = vec();
let vec[@ty] args_t = vec();
case (ast.expr_tup(?elts, _)) {
let vec[ast.elt] elts_1 = vec();
let vec[@ty] elts_t = vec();
for (tup(mutability, @ast.expr) arg in args) {
auto expr_1 = check_expr(fcx, arg._1);
args_1 += tup(arg._0, expr_1);
if (arg._0 == ast.mut) {
append[@ty](args_t,@rec(mut=ast.mut
with *expr_ty(expr_1)));
} else {
append[@ty](args_t,expr_ty(expr_1));
for (ast.elt e in elts) {
auto expr_1 = check_expr(fcx, e.expr);
auto expr_t = expr_ty(expr_1);
if (e.mut == ast.mut) {
expr_t = @rec(mut=ast.mut with *expr_t);
}
append[ast.elt](elts_1, rec(expr=expr_1 with e));
append[@ty](elts_t, expr_t);
}
auto ann = ast.ann_type(plain_ty(ty_tup(args_t)));
auto ann = ast.ann_type(plain_ty(ty_tup(elts_t)));
ret @fold.respan[ast.expr_](expr.span,
ast.expr_tup(args_1, ann));
ast.expr_tup(elts_1, ann));
}
case (ast.expr_rec(?args, _)) {
let vec[tup(ast.ident, @ast.expr)] args_1 = vec();
let vec[field] args_t = vec();
case (ast.expr_rec(?fields, _)) {
let vec[ast.field] fields_1 = vec();
let vec[field] fields_t = vec();
for (tup(ast.ident, @ast.expr) arg in args) {
auto expr_1 = check_expr(fcx, arg._1);
args_1 += tup(arg._0, expr_1);
append[field](args_t,rec(label=arg._0,
ty=expr_ty(expr_1)));
for (ast.field f in fields) {
auto expr_1 = check_expr(fcx, f.expr);
auto expr_t = expr_ty(expr_1);
if (f.mut == ast.mut) {
expr_t = @rec(mut=ast.mut with *expr_t);
}
append[ast.field](fields_1, rec(expr=expr_1 with f));
append[field](fields_t, rec(ident=f.ident, ty=expr_t));
}
auto ann = ast.ann_type(plain_ty(ty_rec(args_t)));
auto ann = ast.ann_type(plain_ty(ty_rec(fields_t)));
ret @fold.respan[ast.expr_](expr.span,
ast.expr_rec(args_1, ann));
ast.expr_rec(fields_1, ann));
}
case (ast.expr_field(?base, ?field, _)) {