1
Fork 0

libsyntax: Remove last use of structural records in pipes compiler.

This commit is contained in:
Luqman Aden 2013-02-17 00:55:36 -05:00
parent a6945f2a45
commit 0126af3144
3 changed files with 69 additions and 56 deletions

View file

@ -88,6 +88,12 @@ pub trait ext_ctxt_ast_builder {
+ty_params: ~[ast::ty_param]) -> @ast::item; +ty_params: ~[ast::ty_param]) -> @ast::item;
fn item_enum(name: ident, span: span, fn item_enum(name: ident, span: span,
+enum_definition: ast::enum_def) -> @ast::item; +enum_definition: ast::enum_def) -> @ast::item;
fn item_struct_poly(name: ident, span: span,
struct_def: ast::struct_def,
ty_params: ~[ast::ty_param]) -> @ast::item;
fn item_struct(name: ident, span: span,
struct_def: ast::struct_def) -> @ast::item;
fn struct_expr(path: @ast::path, fields: ~[ast::field]) -> @ast::expr;
fn variant(name: ident, span: span, +tys: ~[@ast::Ty]) -> ast::variant; fn variant(name: ident, span: span, +tys: ~[@ast::Ty]) -> ast::variant;
fn item_mod(name: ident, span: span, +items: ~[@ast::item]) -> @ast::item; fn item_mod(name: ident, span: span, +items: ~[@ast::item]) -> @ast::item;
fn ty_path_ast_builder(path: @ast::path) -> @ast::Ty; fn ty_path_ast_builder(path: @ast::path) -> @ast::Ty;
@ -99,9 +105,7 @@ pub trait ext_ctxt_ast_builder {
fn ty_vars(+ty_params: ~[ast::ty_param]) -> ~[@ast::Ty]; fn ty_vars(+ty_params: ~[ast::ty_param]) -> ~[@ast::Ty];
fn ty_vars_global(+ty_params: ~[ast::ty_param]) -> ~[@ast::Ty]; fn ty_vars_global(+ty_params: ~[ast::ty_param]) -> ~[@ast::Ty];
fn ty_field_imm(name: ident, ty: @ast::Ty) -> ast::ty_field; fn ty_field_imm(name: ident, ty: @ast::Ty) -> ast::ty_field;
fn ty_rec(+v: ~[ast::ty_field]) -> @ast::Ty;
fn field_imm(name: ident, e: @ast::expr) -> ast::field; fn field_imm(name: ident, e: @ast::expr) -> ast::field;
fn rec(+v: ~[ast::field]) -> @ast::expr;
fn block(+stmts: ~[@ast::stmt], e: @ast::expr) -> ast::blk; fn block(+stmts: ~[@ast::stmt], e: @ast::expr) -> ast::blk;
fn stmt_let(ident: ident, e: @ast::expr) -> @ast::stmt; fn stmt_let(ident: ident, e: @ast::expr) -> @ast::stmt;
fn stmt_expr(e: @ast::expr) -> @ast::stmt; fn stmt_expr(e: @ast::expr) -> @ast::stmt;
@ -147,15 +151,6 @@ pub impl ext_ctxt_ast_builder for ext_ctxt {
} }
} }
fn rec(+fields: ~[ast::field]) -> @ast::expr {
@expr {
id: self.next_id(),
callee_id: self.next_id(),
node: ast::expr_rec(fields, None),
span: dummy_sp(),
}
}
fn ty_field_imm(name: ident, ty: @ast::Ty) -> ast::ty_field { fn ty_field_imm(name: ident, ty: @ast::Ty) -> ast::ty_field {
spanned { spanned {
node: ast::ty_field_ { node: ast::ty_field_ {
@ -166,14 +161,6 @@ pub impl ext_ctxt_ast_builder for ext_ctxt {
} }
} }
fn ty_rec(+fields: ~[ast::ty_field]) -> @ast::Ty {
@ast::Ty {
id: self.next_id(),
node: ast::ty_rec(fields),
span: dummy_sp(),
}
}
fn ty_infer() -> @ast::Ty { fn ty_infer() -> @ast::Ty {
@ast::Ty { @ast::Ty {
id: self.next_id(), id: self.next_id(),
@ -286,6 +273,26 @@ pub impl ext_ctxt_ast_builder for ext_ctxt {
self.item_enum_poly(name, span, enum_definition, ~[]) self.item_enum_poly(name, span, enum_definition, ~[])
} }
fn item_struct(name: ident, span: span,
struct_def: ast::struct_def) -> @ast::item {
self.item_struct_poly(name, span, struct_def, ~[])
}
fn item_struct_poly(name: ident, span: span,
struct_def: ast::struct_def,
ty_params: ~[ast::ty_param]) -> @ast::item {
self.item(name, span, ast::item_struct(@struct_def, ty_params))
}
fn struct_expr(path: @ast::path, fields: ~[ast::field]) -> @ast::expr {
@ast::expr {
id: self.next_id(),
callee_id: self.next_id(),
node: ast::expr_struct(path, fields, None),
span: dummy_sp()
}
}
fn variant(name: ident, fn variant(name: ident,
span: span, span: span,
+tys: ~[@ast::Ty]) -> ast::variant { +tys: ~[@ast::Ty]) -> ast::variant {
@ -300,7 +307,8 @@ pub impl ext_ctxt_ast_builder for ext_ctxt {
kind: ast::tuple_variant_kind(args), kind: ast::tuple_variant_kind(args),
id: self.next_id(), id: self.next_id(),
disr_expr: None, disr_expr: None,
vis: ast::public}, vis: ast::public
},
span: span, span: span,
} }
} }

View file

@ -345,7 +345,9 @@ pub impl gen_init for protocol {
} }
fn gen_buffer_init(ext_cx: ext_ctxt) -> @ast::expr { fn gen_buffer_init(ext_cx: ext_ctxt) -> @ast::expr {
ext_cx.rec(self.states.map_to_vec(|s| { ext_cx.struct_expr(path(~[ext_cx.ident_of(~"__Buffer")],
dummy_sp()),
self.states.map_to_vec(|s| {
let fty = s.to_ty(ext_cx); let fty = s.to_ty(ext_cx);
ext_cx.field_imm(ext_cx.ident_of(s.name), ext_cx.field_imm(ext_cx.ident_of(s.name),
quote_expr!( quote_expr!(
@ -409,13 +411,27 @@ pub impl gen_init for protocol {
let ty = s.to_ty(cx); let ty = s.to_ty(cx);
let fty = quote_ty!( ::pipes::Packet<$ty> ); let fty = quote_ty!( ::pipes::Packet<$ty> );
cx.ty_field_imm(cx.ident_of(s.name), fty) @spanned {
node: ast::struct_field_ {
kind: ast::named_field(
cx.ident_of(s.name),
ast::struct_immutable,
ast::inherited),
id: cx.next_id(),
ty: fty
},
span: dummy_sp()
}
}; };
cx.item_ty_poly( cx.item_struct_poly(
cx.ident_of(~"__Buffer"), cx.ident_of(~"__Buffer"),
dummy_sp(), dummy_sp(),
cx.ty_rec(fields), ast::struct_def {
fields: fields,
dtor: None,
ctor_id: None
},
cx.strip_bounds(params)) cx.strip_bounds(params))
} }

View file

@ -19,20 +19,9 @@ use core::cmp;
use core::dvec::DVec; use core::dvec::DVec;
use core::to_str::ToStr; use core::to_str::ToStr;
#[deriving_eq]
pub enum direction { send, recv } pub enum direction { send, recv }
pub impl cmp::Eq for direction {
pure fn eq(&self, other: &direction) -> bool {
match ((*self), (*other)) {
(send, send) => true,
(recv, recv) => true,
(send, _) => false,
(recv, _) => false,
}
}
pure fn ne(&self, other: &direction) -> bool { !(*self).eq(other) }
}
pub impl ToStr for direction { pub impl ToStr for direction {
pure fn to_str(&self) -> ~str { pure fn to_str(&self) -> ~str {
match *self { match *self {
@ -82,36 +71,36 @@ pub impl message {
} }
} }
pub enum state { pub type state = @state_;
state_(@{
id: uint, pub struct state_ {
name: ~str, id: uint,
ident: ast::ident, name: ~str,
span: span, ident: ast::ident,
dir: direction, span: span,
ty_params: ~[ast::ty_param], dir: direction,
messages: DVec<message>, ty_params: ~[ast::ty_param],
proto: protocol, messages: DVec<message>,
}), proto: protocol
} }
pub impl state { pub impl state_ {
fn add_message(name: ~str, span: span, fn add_message(@self, name: ~str, span: span,
+data: ~[@ast::Ty], next: Option<next_state>) { +data: ~[@ast::Ty], next: Option<next_state>) {
self.messages.push(message(name, span, data, self, self.messages.push(message(name, span, data, self,
next)); next));
} }
fn filename() -> ~str { fn filename(&self) -> ~str {
(*self).proto.filename() self.proto.filename()
} }
fn data_name() -> ast::ident { fn data_name(&self) -> ast::ident {
self.ident self.ident
} }
/// Returns the type that is used for the messages. /// Returns the type that is used for the messages.
fn to_ty(cx: ext_ctxt) -> @ast::Ty { fn to_ty(&self, cx: ext_ctxt) -> @ast::Ty {
cx.ty_path_ast_builder cx.ty_path_ast_builder
(path(~[cx.ident_of(self.name)],self.span).add_tys( (path(~[cx.ident_of(self.name)],self.span).add_tys(
cx.ty_vars(self.ty_params))) cx.ty_vars(self.ty_params)))
@ -119,7 +108,7 @@ pub impl state {
/// Iterate over the states that can be reached in one message /// Iterate over the states that can be reached in one message
/// from this state. /// from this state.
fn reachable(f: fn(state) -> bool) { fn reachable(&self, f: fn(state) -> bool) {
for self.messages.each |m| { for self.messages.each |m| {
match *m { match *m {
message(_, _, _, _, Some(next_state { state: ref id, _ })) => { message(_, _, _, _, Some(next_state { state: ref id, _ })) => {
@ -199,7 +188,7 @@ pub impl protocol {
+ty_params: ~[ast::ty_param]) -> state { +ty_params: ~[ast::ty_param]) -> state {
let messages = DVec(); let messages = DVec();
let state = state_(@{ let state = @state_ {
id: self.states.len(), id: self.states.len(),
name: name, name: name,
ident: ident, ident: ident,
@ -208,7 +197,7 @@ pub impl protocol {
ty_params: ty_params, ty_params: ty_params,
messages: messages, messages: messages,
proto: self proto: self
}); };
self.states.push(state); self.states.push(state);
state state