aboutsummaryrefslogtreecommitdiff
path: root/src/comp/front
diff options
context:
space:
mode:
authorPatrick Walton <[email protected]>2011-03-17 17:39:47 -0700
committerPatrick Walton <[email protected]>2011-03-17 17:39:47 -0700
commit5eca7129e3857e3506d12de7591238f8a7d55da1 (patch)
tree54701099b4b723539074b662daaf3290e63d51e0 /src/comp/front
parentrustc: Typo: mutabliity -> mutability (diff)
downloadrust-5eca7129e3857e3506d12de7591238f8a7d55da1.tar.xz
rust-5eca7129e3857e3506d12de7591238f8a7d55da1.zip
rustc: Switch mutability from being a type constructor to a field annotation
Diffstat (limited to 'src/comp/front')
-rw-r--r--src/comp/front/ast.rs14
-rw-r--r--src/comp/front/parser.rs94
2 files changed, 60 insertions, 48 deletions
diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs
index 1970f788..e519d03b 100644
--- a/src/comp/front/ast.rs
+++ b/src/comp/front/ast.rs
@@ -168,7 +168,6 @@ tag unop {
bitnot;
not;
neg;
- _mutable;
}
fn unop_to_str(unop op) -> str {
@@ -178,7 +177,6 @@ fn unop_to_str(unop op) -> str {
case (bitnot) {ret "~";}
case (not) {ret "!";}
case (neg) {ret "-";}
- case (_mutable) {ret "mutable";}
}
}
@@ -215,7 +213,7 @@ type field = rec(mutability mut, ident ident, @expr expr);
type expr = spanned[expr_];
tag expr_ {
- expr_vec(vec[@expr], ann);
+ expr_vec(vec[@expr], mutability, ann);
expr_tup(vec[elt], ann);
expr_rec(vec[field], option.t[@expr], ann);
expr_call(@expr, vec[@expr], ann);
@@ -263,7 +261,8 @@ tag lit_ {
// NB: If you change this, you'll probably want to change the corresponding
// type structure in middle/ty.rs as well.
-type ty_field = rec(ident ident, @ty ty);
+type mt = rec(@ty ty, mutability mut);
+type ty_field = rec(ident ident, mt mt);
type ty_arg = rec(mode mode, @ty ty);
// TODO: effect
type ty_method = rec(proto proto, ident ident,
@@ -277,16 +276,15 @@ tag ty_ {
ty_machine(util.common.ty_mach);
ty_char;
ty_str;
- ty_box(@ty);
- ty_vec(@ty);
+ ty_box(mt);
+ ty_vec(mt);
ty_port(@ty);
ty_chan(@ty);
- ty_tup(vec[@ty]);
+ ty_tup(vec[mt]);
ty_rec(vec[ty_field]);
ty_fn(proto, vec[ty_arg], @ty); // TODO: effect
ty_obj(vec[ty_method]);
ty_path(path, option.t[def]);
- ty_mutable(@ty);
ty_type;
ty_constr(@ty, vec[@constr]);
}
diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs
index db09301c..71f0f2d8 100644
--- a/src/comp/front/parser.rs
+++ b/src/comp/front/parser.rs
@@ -189,6 +189,11 @@ impure fn parse_ty_fn(ast.proto proto, parser p,
if (p.peek() == token.BINOP(token.AND)) {
p.bump();
mode = ast.alias;
+
+ if (p.peek() == token.MUTABLE) {
+ p.bump();
+ // TODO: handle mutable alias args
+ }
} else {
mode = ast.val;
}
@@ -262,10 +267,16 @@ impure fn parse_ty_obj(parser p, &mutable ast.span hi) -> ast.ty_ {
ret ast.ty_obj(meths.node);
}
+impure fn parse_mt(parser p) -> ast.mt {
+ auto mut = parse_mutability(p);
+ auto t = parse_ty(p);
+ ret rec(ty=t, mut=mut);
+}
+
impure fn parse_ty_field(parser p) -> ast.ty_field {
- auto ty = parse_ty(p);
+ auto mt = parse_mt(p);
auto id = parse_ident(p);
- ret rec(ident=id, ty=ty);
+ ret rec(ident=id, mt=mt);
}
impure fn parse_constr_arg(parser p) -> @ast.constr_arg {
@@ -360,25 +371,25 @@ impure fn parse_ty(parser p) -> @ast.ty {
case (token.AT) {
p.bump();
- auto t0 = parse_ty(p);
- hi = t0.span;
- t = ast.ty_box(t0);
+ auto mt = parse_mt(p);
+ hi = mt.ty.span;
+ t = ast.ty_box(mt);
}
case (token.VEC) {
p.bump();
expect(p, token.LBRACKET);
- t = ast.ty_vec(parse_ty(p));
+ t = ast.ty_vec(parse_mt(p));
hi = p.get_span();
expect(p, token.RBRACKET);
}
case (token.TUP) {
p.bump();
- auto f = parse_ty; // FIXME: trans_const_lval bug
- auto elems = parse_seq[@ast.ty] (token.LPAREN,
- token.RPAREN,
- some(token.COMMA), f, p);
+ auto f = parse_mt; // FIXME: trans_const_lval bug
+ auto elems = parse_seq[ast.mt] (token.LPAREN,
+ token.RPAREN,
+ some(token.COMMA), f, p);
hi = elems.span;
t = ast.ty_tup(elems.node);
}
@@ -395,13 +406,6 @@ impure fn parse_ty(parser p) -> @ast.ty {
t = ast.ty_rec(elems.node);
}
- case (token.MUTABLE) {
- p.bump();
- auto t0 = parse_ty(p);
- hi = t0.span;
- t = ast.ty_mutable(t0);
- }
-
case (token.FN) {
auto flo = p.get_span();
p.bump();
@@ -463,20 +467,22 @@ impure fn parse_arg(parser p) -> ast.arg {
if (p.peek() == token.BINOP(token.AND)) {
m = ast.alias;
p.bump();
+
+ if (p.peek() == token.MUTABLE) {
+ // TODO: handle mutable alias args
+ p.bump();
+ }
}
let @ast.ty t = parse_ty(p);
let ast.ident i = parse_ident(p);
ret rec(mode=m, ty=t, ident=i, id=p.next_def_id());
}
-impure fn parse_seq[T](token.token bra,
- token.token ket,
- option.t[token.token] sep,
- (impure fn(parser) -> T) f,
- parser p) -> util.common.spanned[vec[T]] {
+impure fn parse_seq_to_end[T](token.token ket,
+ option.t[token.token] sep,
+ (impure fn(parser) -> T) f,
+ parser p) -> vec[T] {
let bool first = true;
- auto lo = p.get_span();
- expect(p, bra);
let vec[T] v = vec();
while (p.peek() != ket) {
alt(sep) {
@@ -494,9 +500,20 @@ impure fn parse_seq[T](token.token bra,
let T t = f(p);
v += vec(t);
}
- auto hi = p.get_span();
expect(p, ket);
- ret spanned(lo, hi, v);
+ ret v;
+}
+
+impure fn parse_seq[T](token.token bra,
+ token.token ket,
+ option.t[token.token] sep,
+ (impure fn(parser) -> T) f,
+ parser p) -> util.common.spanned[vec[T]] {
+ auto lo = p.get_span();
+ expect(p, bra);
+ auto result = parse_seq_to_end[T](ket, sep, f, p);
+ auto hi = p.get_span();
+ ret spanned(lo, hi, result);
}
impure fn parse_lit(parser p) -> ast.lit {
@@ -667,12 +684,15 @@ impure fn parse_bottom_expr(parser p) -> @ast.expr {
case (token.VEC) {
p.bump();
auto pf = parse_expr;
- auto es = parse_seq[@ast.expr](token.LPAREN,
- token.RPAREN,
- some(token.COMMA),
- pf, p);
- hi = es.span;
- ex = ast.expr_vec(es.node, ast.ann_none);
+
+ expect(p, token.LPAREN);
+ auto mut = parse_mutability(p);
+
+ auto es = parse_seq_to_end[@ast.expr](token.RPAREN,
+ some(token.COMMA),
+ pf, p);
+ hi = p.get_span();
+ ex = ast.expr_vec(es, mut, ast.ann_none);
}
case (token.REC) {
@@ -1004,13 +1024,6 @@ impure fn parse_prefix_expr(parser p) -> @ast.expr {
ex = ast.expr_unary(ast.box, e, ast.ann_none);
}
- case (token.MUTABLE) {
- p.bump();
- auto e = parse_prefix_expr(p);
- hi = e.span;
- ex = ast.expr_unary(ast._mutable, e, ast.ann_none);
- }
-
case (_) {
ret parse_dot_or_call_expr(p);
}
@@ -1558,7 +1571,7 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool {
}
case (ast.stmt_expr(?e)) {
alt (e.node) {
- case (ast.expr_vec(_,_)) { ret true; }
+ case (ast.expr_vec(_,_,_)) { ret true; }
case (ast.expr_tup(_,_)) { ret true; }
case (ast.expr_rec(_,_,_)) { ret true; }
case (ast.expr_call(_,_,_)) { ret true; }
@@ -1722,6 +1735,7 @@ impure fn parse_item_fn_or_iter(parser p, ast.effect eff) -> @ast.item {
impure fn parse_obj_field(parser p) -> ast.obj_field {
+ auto mut = parse_mutability(p); // TODO: store this, use it in typeck
auto ty = parse_ty(p);
auto ident = parse_ident(p);
ret rec(ty=ty, ident=ident, id=p.next_def_id(), ann=ast.ann_none);