rustc: Change the address-of operator to an explicit production so that we can parse the mutability

This commit is contained in:
Patrick Walton 2012-03-08 16:34:36 -08:00
parent 0722786664
commit ddeaea2287
10 changed files with 97 additions and 55 deletions

View file

@ -1393,7 +1393,6 @@ fn serialize_81<S: std::serialization::serializer>(s: S,
/*syntax::ast::mutability*/ /*syntax::ast::mutability*/
{|| {||
alt v { alt v {
syntax::ast::box(v0) { syntax::ast::box(v0) {
@ -1430,10 +1429,6 @@ fn serialize_81<S: std::serialization::serializer>(s: S,
syntax::ast::neg { syntax::ast::neg {
s.emit_enum_variant("syntax::ast::neg", 4u, 0u, {|| }) s.emit_enum_variant("syntax::ast::neg", 4u, 0u, {|| })
} }
syntax::ast::addr_of {
s.emit_enum_variant("syntax::ast::addr_of", 5u, 0u,
{|| })
}
} }
}); });
} }
@ -2426,6 +2421,7 @@ fn serialize_73<S: std::serialization::serializer>(s: S,
/*[@syntax::ast::ty]*/ /*[@syntax::ast::ty]*/
/*@syntax::ast::expr*//*@syntax::ast::expr*/ /*@syntax::ast::expr*//*@syntax::ast::expr*/
/*@syntax::ast::path*/ /*@syntax::ast::path*/
/*syntax::ast::mutability*//*@syntax::ast::expr*/
/*core::option::t<@syntax::ast::expr>*/ /*core::option::t<@syntax::ast::expr>*/
@ -2893,8 +2889,26 @@ fn serialize_73<S: std::serialization::serializer>(s: S,
} }
}) })
} }
syntax::ast::expr_addr_of(v0, v1) {
s.emit_enum_variant("syntax::ast::expr_addr_of", 25u,
2u,
{||
{
s.emit_enum_variant_arg(0u,
{||
serialize_33(s,
v0)
});
s.emit_enum_variant_arg(1u,
{||
serialize_71(s,
v1)
})
}
})
}
syntax::ast::expr_fail(v0) { syntax::ast::expr_fail(v0) {
s.emit_enum_variant("syntax::ast::expr_fail", 25u, 1u, s.emit_enum_variant("syntax::ast::expr_fail", 26u, 1u,
{|| {||
{ {
s.emit_enum_variant_arg(0u, s.emit_enum_variant_arg(0u,
@ -2906,15 +2920,15 @@ fn serialize_73<S: std::serialization::serializer>(s: S,
}) })
} }
syntax::ast::expr_break { syntax::ast::expr_break {
s.emit_enum_variant("syntax::ast::expr_break", 26u, s.emit_enum_variant("syntax::ast::expr_break", 27u,
0u, {|| }) 0u, {|| })
} }
syntax::ast::expr_cont { syntax::ast::expr_cont {
s.emit_enum_variant("syntax::ast::expr_cont", 27u, 0u, s.emit_enum_variant("syntax::ast::expr_cont", 28u, 0u,
{|| }) {|| })
} }
syntax::ast::expr_ret(v0) { syntax::ast::expr_ret(v0) {
s.emit_enum_variant("syntax::ast::expr_ret", 28u, 1u, s.emit_enum_variant("syntax::ast::expr_ret", 29u, 1u,
{|| {||
{ {
s.emit_enum_variant_arg(0u, s.emit_enum_variant_arg(0u,
@ -2926,7 +2940,7 @@ fn serialize_73<S: std::serialization::serializer>(s: S,
}) })
} }
syntax::ast::expr_be(v0) { syntax::ast::expr_be(v0) {
s.emit_enum_variant("syntax::ast::expr_be", 29u, 1u, s.emit_enum_variant("syntax::ast::expr_be", 30u, 1u,
{|| {||
{ {
s.emit_enum_variant_arg(0u, s.emit_enum_variant_arg(0u,
@ -2938,7 +2952,7 @@ fn serialize_73<S: std::serialization::serializer>(s: S,
}) })
} }
syntax::ast::expr_log(v0, v1, v2) { syntax::ast::expr_log(v0, v1, v2) {
s.emit_enum_variant("syntax::ast::expr_log", 30u, 3u, s.emit_enum_variant("syntax::ast::expr_log", 31u, 3u,
{|| {||
{ {
s.emit_enum_variant_arg(0u, s.emit_enum_variant_arg(0u,
@ -2960,7 +2974,7 @@ fn serialize_73<S: std::serialization::serializer>(s: S,
}) })
} }
syntax::ast::expr_assert(v0) { syntax::ast::expr_assert(v0) {
s.emit_enum_variant("syntax::ast::expr_assert", 31u, s.emit_enum_variant("syntax::ast::expr_assert", 32u,
1u, 1u,
{|| {||
{ {
@ -2973,7 +2987,7 @@ fn serialize_73<S: std::serialization::serializer>(s: S,
}) })
} }
syntax::ast::expr_check(v0, v1) { syntax::ast::expr_check(v0, v1) {
s.emit_enum_variant("syntax::ast::expr_check", 32u, s.emit_enum_variant("syntax::ast::expr_check", 33u,
2u, 2u,
{|| {||
{ {
@ -2991,7 +3005,7 @@ fn serialize_73<S: std::serialization::serializer>(s: S,
}) })
} }
syntax::ast::expr_if_check(v0, v1, v2) { syntax::ast::expr_if_check(v0, v1, v2) {
s.emit_enum_variant("syntax::ast::expr_if_check", 33u, s.emit_enum_variant("syntax::ast::expr_if_check", 34u,
3u, 3u,
{|| {||
{ {
@ -3014,7 +3028,7 @@ fn serialize_73<S: std::serialization::serializer>(s: S,
}) })
} }
syntax::ast::expr_mac(v0) { syntax::ast::expr_mac(v0) {
s.emit_enum_variant("syntax::ast::expr_mac", 34u, 1u, s.emit_enum_variant("syntax::ast::expr_mac", 35u, 1u,
{|| {||
{ {
s.emit_enum_variant_arg(0u, s.emit_enum_variant_arg(0u,
@ -5550,8 +5564,6 @@ fn deserialize_81<S: std::serialization::deserializer>(s: S) ->
{|| {||
s.read_enum_variant({|v_id| s.read_enum_variant({|v_id|
alt check v_id { alt check v_id {
@ -5570,7 +5582,6 @@ fn deserialize_81<S: std::serialization::deserializer>(s: S) ->
2u { syntax::ast::deref } 2u { syntax::ast::deref }
3u { syntax::ast::not } 3u { syntax::ast::not }
4u { syntax::ast::neg } 4u { syntax::ast::neg }
5u { syntax::ast::addr_of }
} }
}) })
}) })
@ -6580,6 +6591,8 @@ fn deserialize_73<S: std::serialization::deserializer>(s: S) ->
/*@syntax::ast::path*/ /*@syntax::ast::path*/
/*syntax::ast::mutability*//*@syntax::ast::expr*/
/*core::option::t<@syntax::ast::expr>*/ /*core::option::t<@syntax::ast::expr>*/
@ -6870,26 +6883,36 @@ fn deserialize_73<S: std::serialization::deserializer>(s: S) ->
})) }))
} }
25u { 25u {
syntax::ast::expr_addr_of(s.read_enum_variant_arg(0u,
{||
deserialize_33(s)
}),
s.read_enum_variant_arg(1u,
{||
deserialize_71(s)
}))
}
26u {
syntax::ast::expr_fail(s.read_enum_variant_arg(0u, syntax::ast::expr_fail(s.read_enum_variant_arg(0u,
{|| {||
deserialize_78(s) deserialize_78(s)
})) }))
} }
26u { syntax::ast::expr_break } 27u { syntax::ast::expr_break }
27u { syntax::ast::expr_cont } 28u { syntax::ast::expr_cont }
28u { 29u {
syntax::ast::expr_ret(s.read_enum_variant_arg(0u, syntax::ast::expr_ret(s.read_enum_variant_arg(0u,
{|| {||
deserialize_78(s) deserialize_78(s)
})) }))
} }
29u { 30u {
syntax::ast::expr_be(s.read_enum_variant_arg(0u, syntax::ast::expr_be(s.read_enum_variant_arg(0u,
{|| {||
deserialize_71(s) deserialize_71(s)
})) }))
} }
30u { 31u {
syntax::ast::expr_log(s.read_enum_variant_arg(0u, syntax::ast::expr_log(s.read_enum_variant_arg(0u,
{|| {||
deserialize_128(s) deserialize_128(s)
@ -6903,13 +6926,13 @@ fn deserialize_73<S: std::serialization::deserializer>(s: S) ->
deserialize_71(s) deserialize_71(s)
})) }))
} }
31u { 32u {
syntax::ast::expr_assert(s.read_enum_variant_arg(0u, syntax::ast::expr_assert(s.read_enum_variant_arg(0u,
{|| {||
deserialize_71(s) deserialize_71(s)
})) }))
} }
32u { 33u {
syntax::ast::expr_check(s.read_enum_variant_arg(0u, syntax::ast::expr_check(s.read_enum_variant_arg(0u,
{|| {||
deserialize_129(s) deserialize_129(s)
@ -6919,7 +6942,7 @@ fn deserialize_73<S: std::serialization::deserializer>(s: S) ->
deserialize_71(s) deserialize_71(s)
})) }))
} }
33u { 34u {
syntax::ast::expr_if_check(s.read_enum_variant_arg(0u, syntax::ast::expr_if_check(s.read_enum_variant_arg(0u,
{|| {||
deserialize_71(s) deserialize_71(s)
@ -6933,7 +6956,7 @@ fn deserialize_73<S: std::serialization::deserializer>(s: S) ->
deserialize_78(s) deserialize_78(s)
})) }))
} }
34u { 35u {
syntax::ast::expr_mac(s.read_enum_variant_arg(0u, syntax::ast::expr_mac(s.read_enum_variant_arg(0u,
{|| {||
deserialize_68(s) deserialize_68(s)
@ -8171,7 +8194,7 @@ fn serialize_164<S: std::serialization::serializer>(s: S,
s.emit_enum("syntax::ast::def", s.emit_enum("syntax::ast::def",
/*syntax::ast::def_id*//*syntax::ast::purity*/ /*syntax::ast::def_id*//*syntax::ast::purity*/
/*syntax::ast::def_id*/ /*syntax::ast::node_id*/
/*syntax::ast::def_id*/ /*syntax::ast::def_id*/
/*syntax::ast::def_id*/ /*syntax::ast::def_id*/
/*syntax::ast::def_id*/ /*syntax::ast::def_id*/
@ -8534,7 +8557,7 @@ fn deserialize_164<S: std::serialization::deserializer>(s: S) ->
s.read_enum("syntax::ast::def", s.read_enum("syntax::ast::def",
/*syntax::ast::def_id*//*syntax::ast::purity*/ /*syntax::ast::def_id*//*syntax::ast::purity*/
/*syntax::ast::def_id*/ /*syntax::ast::node_id*/
/*syntax::ast::def_id*/ /*syntax::ast::def_id*/
@ -8985,7 +9008,7 @@ fn deserialize_syntax_ast_inlined_item<S: std::serialization::deserializer>(s:
deserialize_171(s) deserialize_171(s)
} }
/*[syntax::ast::node_id]*/ /*[syntax::ast::node_id]*/
fn serialize_172<S: std::serialization::serializer>(s: S, fn serialize_173<S: std::serialization::serializer>(s: S,
v: v:
[syntax::ast::node_id]) { [syntax::ast::node_id]) {
s.emit_vec(vec::len(v), /*syntax::ast::node_id*/ s.emit_vec(vec::len(v), /*syntax::ast::node_id*/
@ -8997,7 +9020,7 @@ fn serialize_172<S: std::serialization::serializer>(s: S,
}); });
} }
/*middle::last_use::is_last_use*/ /*middle::last_use::is_last_use*/
fn serialize_173<S: std::serialization::serializer>(s: S, fn serialize_172<S: std::serialization::serializer>(s: S,
v: v:
middle::last_use::is_last_use) { middle::last_use::is_last_use) {
s.emit_enum("middle::last_use::is_last_use", s.emit_enum("middle::last_use::is_last_use",
@ -9021,7 +9044,7 @@ fn serialize_173<S: std::serialization::serializer>(s: S,
{ {
s.emit_enum_variant_arg(0u, s.emit_enum_variant_arg(0u,
{|| {||
serialize_172(s, serialize_173(s,
v0) v0)
}) })
} }
@ -9034,10 +9057,10 @@ fn serialize_middle_last_use_is_last_use<S: std::serialization::serializer>(s:
S, S,
v: v:
middle::last_use::is_last_use) { middle::last_use::is_last_use) {
serialize_173(s, v); serialize_172(s, v);
} }
/*[syntax::ast::node_id]*/ /*[syntax::ast::node_id]*/
fn deserialize_172<S: std::serialization::deserializer>(s: S) -> fn deserialize_173<S: std::serialization::deserializer>(s: S) ->
[syntax::ast::node_id] { [syntax::ast::node_id] {
s.read_vec( s.read_vec(
@ -9050,7 +9073,7 @@ fn deserialize_172<S: std::serialization::deserializer>(s: S) ->
}) })
} }
/*middle::last_use::is_last_use*/ /*middle::last_use::is_last_use*/
fn deserialize_173<S: std::serialization::deserializer>(s: S) -> fn deserialize_172<S: std::serialization::deserializer>(s: S) ->
middle::last_use::is_last_use { middle::last_use::is_last_use {
s.read_enum("middle::last_use::is_last_use", s.read_enum("middle::last_use::is_last_use",
@ -9070,7 +9093,7 @@ fn deserialize_173<S: std::serialization::deserializer>(s: S) ->
2u { 2u {
middle::last_use::closes_over(s.read_enum_variant_arg(0u, middle::last_use::closes_over(s.read_enum_variant_arg(0u,
{|| {||
deserialize_172(s) deserialize_173(s)
})) }))
} }
} }
@ -9080,5 +9103,5 @@ fn deserialize_173<S: std::serialization::deserializer>(s: S) ->
fn deserialize_middle_last_use_is_last_use<S: std::serialization::deserializer>(s: fn deserialize_middle_last_use_is_last_use<S: std::serialization::deserializer>(s:
S) S)
-> middle::last_use::is_last_use { -> middle::last_use::is_last_use {
deserialize_173(s) deserialize_172(s)
} }

View file

@ -1645,16 +1645,17 @@ fn trans_unary(bcx: block, op: ast::unop, e: @ast::expr,
translated using trans_lval(), not \ translated using trans_lval(), not \
trans_unary()"); trans_unary()");
} }
ast::addr_of { }
}
fn trans_addr_of(cx: block, e: @ast::expr, dest: dest) -> block {
// FIXME: This is wrong. // FIXME: This is wrong.
let {bcx, val, kind} = trans_temp_lval(bcx, e); let {bcx, val, kind} = trans_temp_lval(cx, e);
if kind != owned { if kind != owned {
bcx.sess().span_bug(e.span, bcx.sess().span_bug(e.span,
"can't take the address of an rvalue"); "can't take the address of an rvalue");
} }
ret store_in_dest(bcx, val, dest); ret store_in_dest(bcx, val, dest);
}
}
} }
fn trans_compare(cx: block, op: ast::binop, lhs: ValueRef, fn trans_compare(cx: block, op: ast::binop, lhs: ValueRef,
@ -3151,6 +3152,7 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
assert op != ast::deref; // lvals are handled above assert op != ast::deref; // lvals are handled above
ret trans_unary(bcx, op, x, e, dest); ret trans_unary(bcx, op, x, e, dest);
} }
ast::expr_addr_of(_, x) { ret trans_addr_of(bcx, x, dest); }
ast::expr_fn(proto, decl, body, cap_clause) { ast::expr_fn(proto, decl, body, cap_clause) {
ret closure::trans_expr_fn( ret closure::trans_expr_fn(
bcx, proto, decl, body, e.span, e.id, *cap_clause, dest); bcx, proto, decl, body, e.span, e.id, *cap_clause, dest);
@ -4307,8 +4309,7 @@ fn trans_const_expr(cx: crate_ctxt, e: @ast::expr) -> ValueRef {
ret alt u { ret alt u {
ast::box(_) | ast::box(_) |
ast::uniq(_) | ast::uniq(_) |
ast::deref | ast::deref { cx.sess.span_bug(e.span,
ast::addr_of { cx.sess.span_bug(e.span,
"bad unop type in trans_const_expr"); } "bad unop type in trans_const_expr"); }
ast::not { llvm::LLVMConstNot(te) } ast::not { llvm::LLVMConstNot(te) }
ast::neg { ast::neg {

View file

@ -409,6 +409,10 @@ fn find_pre_post_expr(fcx: fn_ctxt, e: @expr) {
find_pre_post_expr(fcx, operand); find_pre_post_expr(fcx, operand);
copy_pre_post(fcx.ccx, e.id, operand); copy_pre_post(fcx.ccx, e.id, operand);
} }
expr_addr_of(_, operand) {
find_pre_post_expr(fcx, operand);
copy_pre_post(fcx.ccx, e.id, operand);
}
expr_cast(operand, _) { expr_cast(operand, _) {
find_pre_post_expr(fcx, operand); find_pre_post_expr(fcx, operand);
copy_pre_post(fcx.ccx, e.id, operand); copy_pre_post(fcx.ccx, e.id, operand);

View file

@ -587,6 +587,9 @@ fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool {
expr_unary(_, operand) { expr_unary(_, operand) {
ret find_pre_post_state_sub(fcx, pres, operand, e.id, none); ret find_pre_post_state_sub(fcx, pres, operand, e.id, none);
} }
expr_addr_of(_, operand) {
ret find_pre_post_state_sub(fcx, pres, operand, e.id, none);
}
expr_cast(operand, _) { expr_cast(operand, _) {
ret find_pre_post_state_sub(fcx, pres, operand, e.id, none); ret find_pre_post_state_sub(fcx, pres, operand, e.id, none);
} }

View file

@ -2198,14 +2198,17 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
oper_t = check_user_unop(fcx, "-", "unary-", expr, oper_t); oper_t = check_user_unop(fcx, "-", "unary-", expr, oper_t);
} }
} }
ast::addr_of {
// FIXME: This is incorrect. Infer the proper region.
let tm = { ty: oper_t, mutbl: ast::m_imm };
oper_t = ty::mk_rptr(tcx, ty::re_block(0), tm);
}
} }
write_ty(tcx, id, oper_t); write_ty(tcx, id, oper_t);
} }
ast::expr_addr_of(mutbl, oper) {
bot = check_expr(fcx, oper);
let oper_t = expr_ty(tcx, oper);
// FIXME: This is incorrect. Infer the proper region.
let tm = { ty: oper_t, mutbl: mutbl };
oper_t = ty::mk_rptr(tcx, ty::re_block(0), tm);
}
ast::expr_path(pth) { ast::expr_path(pth) {
let defn = lookup_def(fcx, pth.span, id); let defn = lookup_def(fcx, pth.span, id);

View file

@ -159,7 +159,7 @@ enum binop {
enum unop { enum unop {
box(mutability), box(mutability),
uniq(mutability), uniq(mutability),
deref, not, neg, addr_of deref, not, neg
} }
// Generally, after typeck you can get the inferred value // Generally, after typeck you can get the inferred value
@ -245,6 +245,7 @@ enum expr_ {
expr_field(@expr, ident, [@ty]), expr_field(@expr, ident, [@ty]),
expr_index(@expr, @expr), expr_index(@expr, @expr),
expr_path(@path), expr_path(@path),
expr_addr_of(mutability, @expr),
expr_fail(option<@expr>), expr_fail(option<@expr>),
expr_break, expr_break,
expr_cont, expr_cont,

View file

@ -402,6 +402,7 @@ fn noop_fold_expr(e: expr_, fld: ast_fold) -> expr_ {
expr_unary(binop, ohs) { expr_unary(binop, fld.fold_expr(ohs)) } expr_unary(binop, ohs) { expr_unary(binop, fld.fold_expr(ohs)) }
expr_lit(_) { e } expr_lit(_) { e }
expr_cast(expr, ty) { expr_cast(fld.fold_expr(expr), ty) } expr_cast(expr, ty) { expr_cast(fld.fold_expr(expr), ty) }
expr_addr_of(m, ohs) { expr_addr_of(m, fld.fold_expr(ohs)) }
expr_if(cond, tr, fl) { expr_if(cond, tr, fl) {
expr_if(fld.fold_expr(cond), fld.fold_block(tr), expr_if(fld.fold_expr(cond), fld.fold_block(tr),
option::map(fl, fld.fold_expr)) option::map(fl, fld.fold_expr))

View file

@ -1129,9 +1129,10 @@ fn parse_prefix_expr(p: parser) -> pexpr {
} }
token::AND { token::AND {
p.bump(); p.bump();
let m = parse_mutability(p);
let e = to_expr(parse_prefix_expr(p)); let e = to_expr(parse_prefix_expr(p));
hi = e.span.hi; hi = e.span.hi;
ex = ast::expr_unary(ast::addr_of, e); ex = ast::expr_addr_of(m, e);
} }
_ { ret parse_dot_or_call_expr(p); } _ { ret parse_dot_or_call_expr(p); }
} }

View file

@ -877,6 +877,10 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
word(s.s, ast_util::unop_to_str(op)); word(s.s, ast_util::unop_to_str(op));
print_op_maybe_parens(s, expr, parse::parser::unop_prec); print_op_maybe_parens(s, expr, parse::parser::unop_prec);
} }
ast::expr_addr_of(m, expr) {
print_mutability(s, m);
print_expr(s, expr);
}
ast::expr_lit(lit) { print_literal(s, lit); } ast::expr_lit(lit) { print_literal(s, lit); }
ast::expr_cast(expr, ty) { ast::expr_cast(expr, ty) {
print_op_maybe_parens(s, expr, parse::parser::as_prec); print_op_maybe_parens(s, expr, parse::parser::as_prec);

View file

@ -331,6 +331,7 @@ fn visit_expr<E>(ex: @expr, e: E, v: vt<E>) {
expr_unary(_, a) { v.visit_expr(a, e, v); } expr_unary(_, a) { v.visit_expr(a, e, v); }
expr_lit(_) { } expr_lit(_) { }
expr_cast(x, t) { v.visit_expr(x, e, v); v.visit_ty(t, e, v); } expr_cast(x, t) { v.visit_expr(x, e, v); v.visit_ty(t, e, v); }
expr_addr_of(_, x) { v.visit_expr(x, e, v); }
expr_if(x, b, eo) { expr_if(x, b, eo) {
v.visit_expr(x, e, v); v.visit_expr(x, e, v);
v.visit_block(b, e, v); v.visit_block(b, e, v);