1
Fork 0

libsyntax: add explicit modes where required to copy strs/vecs

This commit is contained in:
Erick Tryzelaar 2013-02-17 08:28:11 -08:00
parent 5b9e110eab
commit 44f5537abf
6 changed files with 105 additions and 75 deletions

View file

@ -209,7 +209,7 @@ fn eq(a: @ast::meta_item, b: @ast::meta_item) -> bool {
pub fn contains_name(metas: &[@ast::meta_item], name: &str) -> bool { pub fn contains_name(metas: &[@ast::meta_item], name: &str) -> bool {
let matches = find_meta_items_by_name(metas, name); let matches = find_meta_items_by_name(metas, name);
return vec::len(matches) > 0u; matches.len() > 0u
} }
pub fn attrs_contains_name(attrs: &[ast::attribute], name: &str) -> bool { pub fn attrs_contains_name(attrs: &[ast::attribute], name: &str) -> bool {
@ -227,14 +227,14 @@ pub fn first_attr_value_str_by_name(attrs: ~[ast::attribute], name: &str)
} }
} }
fn last_meta_item_by_name(items: ~[@ast::meta_item], name: &str) fn last_meta_item_by_name(items: &[@ast::meta_item], name: &str)
-> Option<@ast::meta_item> { -> Option<@ast::meta_item> {
let items = attr::find_meta_items_by_name(items, name); let items = attr::find_meta_items_by_name(items, name);
vec::last_opt(items) vec::last_opt(items)
} }
pub fn last_meta_item_value_str_by_name(items: ~[@ast::meta_item], name: &str) pub fn last_meta_item_value_str_by_name(items: &[@ast::meta_item], name: &str)
-> Option<@~str> { -> Option<@~str> {
match last_meta_item_by_name(items, name) { match last_meta_item_by_name(items, name) {

View file

@ -120,7 +120,7 @@ pub fn expand_auto_encode(
fn filter_attrs(item: @ast::item) -> @ast::item { fn filter_attrs(item: @ast::item) -> @ast::item {
@ast::item { @ast::item {
attrs: item.attrs.filtered(|a| !is_auto_encode(a)), attrs: item.attrs.filtered(|a| !is_auto_encode(a)),
.. *item .. copy *item
} }
} }
@ -175,7 +175,7 @@ pub fn expand_auto_decode(
fn filter_attrs(item: @ast::item) -> @ast::item { fn filter_attrs(item: @ast::item) -> @ast::item {
@ast::item { @ast::item {
attrs: item.attrs.filtered(|a| !is_auto_decode(a)), attrs: item.attrs.filtered(|a| !is_auto_decode(a)),
.. *item .. copy *item
} }
} }
@ -237,7 +237,7 @@ priv impl ext_ctxt {
} }
} }
fn expr(span: span, node: ast::expr_) -> @ast::expr { fn expr(span: span, +node: ast::expr_) -> @ast::expr {
@ast::expr { @ast::expr {
id: self.next_id(), id: self.next_id(),
callee_id: self.next_id(), callee_id: self.next_id(),
@ -246,7 +246,7 @@ priv impl ext_ctxt {
} }
} }
fn path(span: span, strs: ~[ast::ident]) -> @ast::path { fn path(span: span, +strs: ~[ast::ident]) -> @ast::path {
@ast::path { @ast::path {
span: span, span: span,
global: false, global: false,
@ -256,7 +256,7 @@ priv impl ext_ctxt {
} }
} }
fn path_global(span: span, strs: ~[ast::ident]) -> @ast::path { fn path_global(span: span, +strs: ~[ast::ident]) -> @ast::path {
@ast::path { @ast::path {
span: span, span: span,
global: true, global: true,
@ -266,8 +266,11 @@ priv impl ext_ctxt {
} }
} }
fn path_tps(span: span, strs: ~[ast::ident], fn path_tps(
tps: ~[@ast::Ty]) -> @ast::path { span: span,
+strs: ~[ast::ident],
+tps: ~[@ast::Ty]
) -> @ast::path {
@ast::path { @ast::path {
span: span, span: span,
global: false, global: false,
@ -277,8 +280,11 @@ priv impl ext_ctxt {
} }
} }
fn path_tps_global(span: span, strs: ~[ast::ident], fn path_tps_global(
tps: ~[@ast::Ty]) -> @ast::path { span: span,
+strs: ~[ast::ident],
+tps: ~[@ast::Ty]
) -> @ast::path {
@ast::path { @ast::path {
span: span, span: span,
global: true, global: true,
@ -288,8 +294,11 @@ priv impl ext_ctxt {
} }
} }
fn ty_path(span: span, strs: ~[ast::ident], fn ty_path(
tps: ~[@ast::Ty]) -> @ast::Ty { span: span,
+strs: ~[ast::ident],
+tps: ~[@ast::Ty]
) -> @ast::Ty {
@ast::Ty { @ast::Ty {
id: self.next_id(), id: self.next_id(),
node: ast::ty_path( node: ast::ty_path(
@ -335,13 +344,13 @@ priv impl ext_ctxt {
span: span})) span: span}))
} }
fn lambda(blk: ast::blk) -> @ast::expr { fn lambda(+blk: ast::blk) -> @ast::expr {
let ext_cx = self; let ext_cx = self;
let blk_e = self.expr(blk.span, ast::expr_block(blk)); let blk_e = self.expr(blk.span, ast::expr_block(blk));
quote_expr!( || $blk_e ) quote_expr!( || $blk_e )
} }
fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk { fn blk(span: span, +stmts: ~[@ast::stmt]) -> ast::blk {
codemap::spanned { codemap::spanned {
node: ast::blk_ { node: ast::blk_ {
view_items: ~[], view_items: ~[],
@ -367,15 +376,15 @@ priv impl ext_ctxt {
} }
} }
fn expr_path(span: span, strs: ~[ast::ident]) -> @ast::expr { fn expr_path(span: span, +strs: ~[ast::ident]) -> @ast::expr {
self.expr(span, ast::expr_path(self.path(span, strs))) self.expr(span, ast::expr_path(self.path(span, strs)))
} }
fn expr_path_global(span: span, strs: ~[ast::ident]) -> @ast::expr { fn expr_path_global(span: span, +strs: ~[ast::ident]) -> @ast::expr {
self.expr(span, ast::expr_path(self.path_global(span, strs))) self.expr(span, ast::expr_path(self.path_global(span, strs)))
} }
fn expr_var(span: span, var: ~str) -> @ast::expr { fn expr_var(span: span, +var: ~str) -> @ast::expr {
self.expr_path(span, ~[self.ident_of(var)]) self.expr_path(span, ~[self.ident_of(var)])
} }
@ -390,7 +399,7 @@ priv impl ext_ctxt {
fn expr_call( fn expr_call(
span: span, span: span,
expr: @ast::expr, expr: @ast::expr,
args: ~[@ast::expr] +args: ~[@ast::expr]
) -> @ast::expr { ) -> @ast::expr {
self.expr(span, ast::expr_call(expr, args, ast::NoSugar)) self.expr(span, ast::expr_call(expr, args, ast::NoSugar))
} }
@ -399,7 +408,7 @@ priv impl ext_ctxt {
self.lambda(self.expr_blk(expr)) self.lambda(self.expr_blk(expr))
} }
fn lambda_stmts(span: span, stmts: ~[@ast::stmt]) -> @ast::expr { fn lambda_stmts(span: span, +stmts: ~[@ast::stmt]) -> @ast::expr {
self.lambda(self.blk(span, stmts)) self.lambda(self.blk(span, stmts))
} }
} }
@ -545,7 +554,7 @@ fn mk_deser_impl(
fn mk_ser_method( fn mk_ser_method(
cx: ext_ctxt, cx: ext_ctxt,
span: span, span: span,
ser_body: ast::blk +ser_body: ast::blk
) -> @ast::method { ) -> @ast::method {
let ty_s = @ast::Ty { let ty_s = @ast::Ty {
id: cx.next_id(), id: cx.next_id(),
@ -609,7 +618,7 @@ fn mk_deser_method(
cx: ext_ctxt, cx: ext_ctxt,
span: span, span: span,
ty: @ast::Ty, ty: @ast::Ty,
deser_body: ast::blk +deser_body: ast::blk
) -> @ast::method { ) -> @ast::method {
let ty_d = @ast::Ty { let ty_d = @ast::Ty {
id: cx.next_id(), id: cx.next_id(),
@ -947,7 +956,7 @@ fn mk_enum_ser_body(
cx: ext_ctxt, cx: ext_ctxt,
span: span, span: span,
name: ast::ident, name: ast::ident,
variants: ~[ast::variant] +variants: ~[ast::variant]
) -> @ast::expr { ) -> @ast::expr {
let arms = do variants.mapi |v_idx, variant| { let arms = do variants.mapi |v_idx, variant| {
match variant.node.kind { match variant.node.kind {

View file

@ -33,7 +33,7 @@ mod syntax {
pub use parse; pub use parse;
} }
pub fn path(ids: ~[ident], span: span) -> @ast::path { pub fn path(+ids: ~[ident], span: span) -> @ast::path {
@ast::path { span: span, @ast::path { span: span,
global: false, global: false,
idents: ids, idents: ids,
@ -41,7 +41,7 @@ pub fn path(ids: ~[ident], span: span) -> @ast::path {
types: ~[] } types: ~[] }
} }
pub fn path_global(ids: ~[ident], span: span) -> @ast::path { pub fn path_global(+ids: ~[ident], span: span) -> @ast::path {
@ast::path { span: span, @ast::path { span: span,
global: true, global: true,
idents: ids, idents: ids,
@ -50,19 +50,23 @@ pub fn path_global(ids: ~[ident], span: span) -> @ast::path {
} }
pub trait append_types { pub trait append_types {
fn add_ty(ty: @ast::Ty) -> @ast::path; fn add_ty(&self, ty: @ast::Ty) -> @ast::path;
fn add_tys(+tys: ~[@ast::Ty]) -> @ast::path; fn add_tys(&self, +tys: ~[@ast::Ty]) -> @ast::path;
} }
pub impl append_types for @ast::path { pub impl append_types for @ast::path {
fn add_ty(ty: @ast::Ty) -> @ast::path { fn add_ty(&self, ty: @ast::Ty) -> @ast::path {
@ast::path { types: vec::append_one(self.types, ty), @ast::path {
.. *self} types: vec::append_one(copy self.types, ty),
.. **self
}
} }
fn add_tys(+tys: ~[@ast::Ty]) -> @ast::path { fn add_tys(&self, +tys: ~[@ast::Ty]) -> @ast::path {
@ast::path { types: vec::append(self.types, tys), @ast::path {
.. *self} types: vec::append(copy self.types, tys),
.. **self
}
} }
} }
@ -73,24 +77,28 @@ pub trait ext_ctxt_ast_builder {
fn expr_block(&self, e: @ast::expr) -> ast::blk; fn expr_block(&self, e: @ast::expr) -> ast::blk;
fn fn_decl(&self, +inputs: ~[ast::arg], output: @ast::Ty) -> ast::fn_decl; fn fn_decl(&self, +inputs: ~[ast::arg], output: @ast::Ty) -> ast::fn_decl;
fn item(&self, name: ident, span: span, +node: ast::item_) -> @ast::item; fn item(&self, name: ident, span: span, +node: ast::item_) -> @ast::item;
fn item_fn_poly(&self, name: ident, fn item_fn_poly(&self,
+inputs: ~[ast::arg], name: ident,
output: @ast::Ty, +inputs: ~[ast::arg],
+ty_params: ~[ast::ty_param], output: @ast::Ty,
+body: ast::blk) -> @ast::item; +ty_params: ~[ast::ty_param],
fn item_fn(&self, name: ident, +body: ast::blk) -> @ast::item;
+inputs: ~[ast::arg], fn item_fn(&self,
output: @ast::Ty, name: ident,
+body: ast::blk) -> @ast::item; +inputs: ~[ast::arg],
fn item_enum_poly(&self, name: ident, output: @ast::Ty,
span: span, +body: ast::blk) -> @ast::item;
+enum_definition: ast::enum_def, fn item_enum_poly(&self,
+ty_params: ~[ast::ty_param]) -> @ast::item; name: ident,
span: span,
+enum_definition: ast::enum_def,
+ty_params: ~[ast::ty_param]) -> @ast::item;
fn item_enum(&self, name: ident, span: span, fn item_enum(&self, name: ident, span: span,
+enum_definition: ast::enum_def) -> @ast::item; +enum_definition: ast::enum_def) -> @ast::item;
fn item_struct_poly(&self, name: ident, span: span, fn item_struct_poly(&self,
struct_def: ast::struct_def, name: ident, span: span,
ty_params: ~[ast::ty_param]) -> @ast::item; struct_def: ast::struct_def,
+ty_params: ~[ast::ty_param]) -> @ast::item;
fn item_struct(&self, name: ident, span: span, fn item_struct(&self, name: ident, span: span,
struct_def: ast::struct_def) -> @ast::item; struct_def: ast::struct_def) -> @ast::item;
fn struct_expr(&self, path: @ast::path, fn struct_expr(&self, path: @ast::path,
@ -105,14 +113,14 @@ pub trait ext_ctxt_ast_builder {
ty: @ast::Ty, ty: @ast::Ty,
+params: ~[ast::ty_param]) -> @ast::item; +params: ~[ast::ty_param]) -> @ast::item;
fn item_ty(&self, name: ident, span: span, ty: @ast::Ty) -> @ast::item; fn item_ty(&self, name: ident, span: span, ty: @ast::Ty) -> @ast::item;
fn ty_vars(&self, +ty_params: ~[ast::ty_param]) -> ~[@ast::Ty]; fn ty_vars(&self, ty_params: &[ast::ty_param]) -> ~[@ast::Ty];
fn ty_vars_global(&self, +ty_params: ~[ast::ty_param]) -> ~[@ast::Ty]; fn ty_vars_global(&self, ty_params: &[ast::ty_param]) -> ~[@ast::Ty];
fn ty_field_imm(&self, name: ident, ty: @ast::Ty) -> ast::ty_field; fn ty_field_imm(&self, name: ident, ty: @ast::Ty) -> ast::ty_field;
fn field_imm(&self, name: ident, e: @ast::expr) -> ast::field; fn field_imm(&self, name: ident, e: @ast::expr) -> ast::field;
fn block(&self, +stmts: ~[@ast::stmt], e: @ast::expr) -> ast::blk; fn block(&self, +stmts: ~[@ast::stmt], e: @ast::expr) -> ast::blk;
fn stmt_let(&self, ident: ident, e: @ast::expr) -> @ast::stmt; fn stmt_let(&self, ident: ident, e: @ast::expr) -> @ast::stmt;
fn stmt_expr(&self, e: @ast::expr) -> @ast::stmt; fn stmt_expr(&self, e: @ast::expr) -> @ast::stmt;
fn block_expr(&self, b: ast::blk) -> @ast::expr; fn block_expr(&self, +b: ast::blk) -> @ast::expr;
fn ty_option(&self, ty: @ast::Ty) -> @ast::Ty; fn ty_option(&self, ty: @ast::Ty) -> @ast::Ty;
fn ty_infer(&self) -> @ast::Ty; fn ty_infer(&self) -> @ast::Ty;
fn ty_nil_ast_builder(&self) -> @ast::Ty; fn ty_nil_ast_builder(&self) -> @ast::Ty;
@ -128,7 +136,7 @@ pub impl ext_ctxt_ast_builder for ext_ctxt {
], dummy_sp()).add_ty(ty)) ], dummy_sp()).add_ty(ty))
} }
fn block_expr(&self, b: ast::blk) -> @ast::expr { fn block_expr(&self, +b: ast::blk) -> @ast::expr {
@expr { @expr {
id: self.next_id(), id: self.next_id(),
callee_id: self.next_id(), callee_id: self.next_id(),
@ -282,7 +290,7 @@ pub impl ext_ctxt_ast_builder for ext_ctxt {
fn item_struct_poly(&self, name: ident, span: span, fn item_struct_poly(&self, name: ident, span: span,
struct_def: ast::struct_def, struct_def: ast::struct_def,
ty_params: ~[ast::ty_param]) -> @ast::item { +ty_params: ~[ast::ty_param]) -> @ast::item {
self.item(name, span, ast::item_struct(@struct_def, ty_params)) self.item(name, span, ast::item_struct(@struct_def, ty_params))
} }
@ -386,12 +394,12 @@ pub impl ext_ctxt_ast_builder for ext_ctxt {
self.item_ty_poly(name, span, ty, ~[]) self.item_ty_poly(name, span, ty, ~[])
} }
fn ty_vars(&self, +ty_params: ~[ast::ty_param]) -> ~[@ast::Ty] { fn ty_vars(&self, +ty_params: &[ast::ty_param]) -> ~[@ast::Ty] {
ty_params.map(|p| self.ty_path_ast_builder( ty_params.map(|p| self.ty_path_ast_builder(
path(~[p.ident], dummy_sp()))) path(~[p.ident], dummy_sp())))
} }
fn ty_vars_global(&self, +ty_params: ~[ast::ty_param]) -> ~[@ast::Ty] { fn ty_vars_global(&self, ty_params: &[ast::ty_param]) -> ~[@ast::Ty] {
ty_params.map(|p| self.ty_path_ast_builder( ty_params.map(|p| self.ty_path_ast_builder(
path(~[p.ident], dummy_sp()))) path(~[p.ident], dummy_sp())))
} }

View file

@ -58,8 +58,7 @@ pub impl gen_send for message {
let next = this.proto.get_state(next_state.state); let next = this.proto.get_state(next_state.state);
assert next_state.tys.len() == next.ty_params.len(); assert next_state.tys.len() == next.ty_params.len();
let arg_names = tys.mapi(|i, _ty| cx.ident_of(~"x_"+i.to_str())); let arg_names = tys.mapi(|i, _ty| cx.ident_of(~"x_"+i.to_str()));
let args_ast = vec::map2(arg_names, *tys, |n, t| cx.arg(*n, *t));
let args_ast = (arg_names, *tys).map(|n, t| cx.arg(*n, *t));
let pipe_ty = cx.ty_path_ast_builder( let pipe_ty = cx.ty_path_ast_builder(
path(~[this.data_name()], span) path(~[this.data_name()], span)
@ -137,7 +136,7 @@ pub impl gen_send for message {
debug!("pipec: no next state"); debug!("pipec: no next state");
let arg_names = tys.mapi(|i, _ty| (~"x_" + i.to_str())); let arg_names = tys.mapi(|i, _ty| (~"x_" + i.to_str()));
let args_ast = do (arg_names, *tys).map |n, t| { let args_ast = do vec::map2(arg_names, *tys) |n, t| {
cx.arg(cx.ident_of(*n), *t) cx.arg(cx.ident_of(*n), *t)
}; };

View file

@ -120,11 +120,11 @@ pub impl state_ {
pub type protocol = @mut protocol_; pub type protocol = @mut protocol_;
pub fn protocol(name: ~str, +span: span) -> protocol { pub fn protocol(+name: ~str, +span: span) -> protocol {
@mut protocol_(name, span) @mut protocol_(name, span)
} }
pub fn protocol_(name: ~str, span: span) -> protocol_ { pub fn protocol_(+name: ~str, span: span) -> protocol_ {
protocol_ { protocol_ {
name: name, name: name,
span: span, span: span,
@ -174,7 +174,7 @@ pub impl protocol_ {
} }
pub impl protocol { pub impl protocol {
fn add_state_poly(&self, name: ~str, ident: ast::ident, dir: direction, fn add_state_poly(&self, +name: ~str, ident: ast::ident, dir: direction,
+ty_params: ~[ast::ty_param]) -> state { +ty_params: ~[ast::ty_param]) -> state {
let messages = @mut ~[]; let messages = @mut ~[];

View file

@ -23,19 +23,33 @@ use core::str;
use core::vec; use core::vec;
fn topmost_expn_info(expn_info: @codemap::ExpnInfo) -> @codemap::ExpnInfo { fn topmost_expn_info(expn_info: @codemap::ExpnInfo) -> @codemap::ExpnInfo {
let ExpandedFrom(CallInfo { call_site, _ }) = *expn_info; // FIXME(#3874): this would be better written as:
match call_site.expn_info { // let @ExpandedFrom(CallInfo {
Some(next_expn_info) => { // call_site: ref call_site,
let ExpandedFrom(CallInfo { // _
callee: NameAndSpan {name, _}, // }) = expn_info;
_ match *expn_info {
}) = *next_expn_info; ExpandedFrom(CallInfo { call_site: ref call_site, _}) => {
// Don't recurse into file using "include!" match call_site.expn_info {
if name == ~"include" { return expn_info; } Some(next_expn_info) => {
// Don't recurse into file using "include!"
match *next_expn_info {
ExpandedFrom(
CallInfo { callee: NameAndSpan {
name: ref name,
_
},
_
}) => {
if *name == ~"include" { return expn_info; }
}
}
topmost_expn_info(next_expn_info) topmost_expn_info(next_expn_info)
}, },
None => expn_info None => expn_info
}
}
} }
} }