2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
|
|
|
use codemap;
|
2012-09-04 13:37:29 -05:00
|
|
|
use codemap::span;
|
2013-05-08 14:26:34 -05:00
|
|
|
use fold;
|
2012-12-13 15:05:22 -06:00
|
|
|
use ext::base::ext_ctxt;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ext::build;
|
|
|
|
|
2013-02-14 23:50:03 -06:00
|
|
|
use opt_vec::OptVec;
|
|
|
|
|
2013-02-21 02:16:31 -06:00
|
|
|
pub struct Field {
|
|
|
|
ident: ast::ident,
|
|
|
|
ex: @ast::expr
|
|
|
|
}
|
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_expr(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
sp: codemap::span,
|
2013-04-17 11:15:08 -05:00
|
|
|
expr: ast::expr_)
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::expr {
|
2012-06-29 16:38:33 -05:00
|
|
|
@ast::expr {
|
2013-01-15 15:51:43 -06:00
|
|
|
id: cx.next_id(),
|
|
|
|
callee_id: cx.next_id(),
|
|
|
|
node: expr,
|
|
|
|
span: sp,
|
|
|
|
}
|
2012-06-29 16:38:33 -05:00
|
|
|
}
|
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_lit(cx: @ext_ctxt, sp: span, lit: ast::lit_) -> @ast::expr {
|
2013-01-30 11:56:33 -06:00
|
|
|
let sp_lit = @codemap::spanned { node: lit, span: sp };
|
2012-07-11 16:31:35 -05:00
|
|
|
mk_expr(cx, sp, ast::expr_lit(sp_lit))
|
2012-01-25 15:47:56 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_int(cx: @ext_ctxt, sp: span, i: int) -> @ast::expr {
|
2012-01-25 15:47:56 -06:00
|
|
|
let lit = ast::lit_int(i as i64, ast::ty_i);
|
2012-08-01 19:30:05 -05:00
|
|
|
return mk_lit(cx, sp, lit);
|
2012-01-25 15:47:56 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_uint(cx: @ext_ctxt, sp: span, u: uint) -> @ast::expr {
|
2012-01-25 15:47:56 -06:00
|
|
|
let lit = ast::lit_uint(u as u64, ast::ty_u);
|
2012-08-01 19:30:05 -05:00
|
|
|
return mk_lit(cx, sp, lit);
|
2012-01-25 15:47:56 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_u8(cx: @ext_ctxt, sp: span, u: u8) -> @ast::expr {
|
2012-07-13 17:54:39 -05:00
|
|
|
let lit = ast::lit_uint(u as u64, ast::ty_u8);
|
2012-08-01 19:30:05 -05:00
|
|
|
return mk_lit(cx, sp, lit);
|
2012-07-13 17:54:39 -05:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_binary(cx: @ext_ctxt, sp: span, op: ast::binop,
|
2013-01-29 16:41:40 -06:00
|
|
|
lhs: @ast::expr, rhs: @ast::expr) -> @ast::expr {
|
2012-06-27 18:06:58 -05:00
|
|
|
cx.next_id(); // see ast_util::op_expr_callee_id
|
2012-06-29 16:38:33 -05:00
|
|
|
mk_expr(cx, sp, ast::expr_binary(op, lhs, rhs))
|
2012-01-25 15:47:56 -06:00
|
|
|
}
|
2013-05-06 10:23:51 -05:00
|
|
|
|
|
|
|
pub fn mk_deref(cx: @ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
|
|
|
|
mk_unary(cx, sp, ast::deref, e)
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_unary(cx: @ext_ctxt, sp: span, op: ast::unop, e: @ast::expr)
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::expr {
|
2012-06-27 18:06:58 -05:00
|
|
|
cx.next_id(); // see ast_util::op_expr_callee_id
|
2012-06-29 16:38:33 -05:00
|
|
|
mk_expr(cx, sp, ast::expr_unary(op, e))
|
2012-01-26 17:34:05 -06:00
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_raw_path(sp: span, idents: ~[ast::ident]) -> @ast::Path {
|
2013-05-06 10:23:51 -05:00
|
|
|
mk_raw_path_(sp, idents, None, ~[])
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
2013-01-29 16:41:40 -06:00
|
|
|
pub fn mk_raw_path_(sp: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
idents: ~[ast::ident],
|
2013-05-06 10:23:51 -05:00
|
|
|
rp: Option<@ast::Lifetime>,
|
2013-04-17 11:15:08 -05:00
|
|
|
types: ~[@ast::Ty])
|
2013-03-26 19:00:35 -05:00
|
|
|
-> @ast::Path {
|
|
|
|
@ast::Path { span: sp,
|
2013-01-13 12:48:09 -06:00
|
|
|
global: false,
|
|
|
|
idents: idents,
|
2013-05-06 10:23:51 -05:00
|
|
|
rp: rp,
|
2013-02-15 03:15:53 -06:00
|
|
|
types: types }
|
2012-11-20 21:20:55 -06:00
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_raw_path_global(sp: span, idents: ~[ast::ident]) -> @ast::Path {
|
2013-05-06 10:23:51 -05:00
|
|
|
mk_raw_path_global_(sp, idents, None, ~[])
|
2013-04-10 18:31:51 -05:00
|
|
|
}
|
|
|
|
pub fn mk_raw_path_global_(sp: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
idents: ~[ast::ident],
|
2013-05-06 10:23:51 -05:00
|
|
|
rp: Option<@ast::Lifetime>,
|
2013-04-17 11:15:08 -05:00
|
|
|
types: ~[@ast::Ty]) -> @ast::Path {
|
2013-03-26 19:00:35 -05:00
|
|
|
@ast::Path { span: sp,
|
2013-01-13 12:48:09 -06:00
|
|
|
global: true,
|
|
|
|
idents: idents,
|
2013-05-06 10:23:51 -05:00
|
|
|
rp: rp,
|
2013-04-10 18:31:51 -05:00
|
|
|
types: types }
|
2012-12-23 16:41:37 -06:00
|
|
|
}
|
2013-05-06 10:23:51 -05:00
|
|
|
pub fn mk_path_raw(cx: @ext_ctxt, sp: span, path: @ast::Path)-> @ast::expr {
|
|
|
|
mk_expr(cx, sp, ast::expr_path(path))
|
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_path(cx: @ext_ctxt, sp: span, idents: ~[ast::ident])
|
2013-03-12 15:00:50 -05:00
|
|
|
-> @ast::expr {
|
2013-05-06 10:23:51 -05:00
|
|
|
mk_path_raw(cx, sp, mk_raw_path(sp, idents))
|
2012-01-25 15:47:56 -06:00
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_path_global(cx: @ext_ctxt, sp: span, idents: ~[ast::ident])
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::expr {
|
2013-05-06 10:23:51 -05:00
|
|
|
mk_path_raw(cx, sp, mk_raw_path_global(sp, idents))
|
2012-12-23 16:41:37 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_access_(cx: @ext_ctxt, sp: span, p: @ast::expr, m: ast::ident)
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::expr {
|
2012-06-29 18:26:56 -05:00
|
|
|
mk_expr(cx, sp, ast::expr_field(p, m, ~[]))
|
2012-01-26 17:34:05 -06:00
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_access(cx: @ext_ctxt, sp: span, p: ~[ast::ident], m: ast::ident)
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::expr {
|
2012-01-26 17:34:05 -06:00
|
|
|
let pathexpr = mk_path(cx, sp, p);
|
2012-08-01 19:30:05 -05:00
|
|
|
return mk_access_(cx, sp, pathexpr, m);
|
2012-01-26 17:34:05 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_addr_of(cx: @ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
|
2012-09-28 14:22:33 -05:00
|
|
|
return mk_expr(cx, sp, ast::expr_addr_of(ast::m_imm, e));
|
|
|
|
}
|
2013-03-20 12:01:58 -05:00
|
|
|
pub fn mk_mut_addr_of(cx: @ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
|
|
|
|
return mk_expr(cx, sp, ast::expr_addr_of(ast::m_mutbl, e));
|
|
|
|
}
|
2013-03-20 20:28:30 -05:00
|
|
|
pub fn mk_method_call(cx: @ext_ctxt,
|
|
|
|
sp: span,
|
|
|
|
rcvr_expr: @ast::expr,
|
|
|
|
method_ident: ast::ident,
|
2013-04-17 11:15:08 -05:00
|
|
|
args: ~[@ast::expr]) -> @ast::expr {
|
2013-03-20 20:28:30 -05:00
|
|
|
mk_expr(cx, sp, ast::expr_method_call(rcvr_expr, method_ident, ~[], args, ast::NoSugar))
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_call_(cx: @ext_ctxt, sp: span, fn_expr: @ast::expr,
|
2013-04-17 11:15:08 -05:00
|
|
|
args: ~[@ast::expr]) -> @ast::expr {
|
2013-01-31 19:12:29 -06:00
|
|
|
mk_expr(cx, sp, ast::expr_call(fn_expr, args, ast::NoSugar))
|
2012-01-26 17:34:05 -06:00
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_call(cx: @ext_ctxt, sp: span, fn_path: ~[ast::ident],
|
|
|
|
args: ~[@ast::expr]) -> @ast::expr {
|
2012-01-26 17:34:05 -06:00
|
|
|
let pathexpr = mk_path(cx, sp, fn_path);
|
2012-08-01 19:30:05 -05:00
|
|
|
return mk_call_(cx, sp, pathexpr, args);
|
2012-01-26 17:34:05 -06:00
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_call_global(cx: @ext_ctxt, sp: span, fn_path: ~[ast::ident],
|
|
|
|
args: ~[@ast::expr]) -> @ast::expr {
|
2012-12-23 16:41:37 -06:00
|
|
|
let pathexpr = mk_path_global(cx, sp, fn_path);
|
|
|
|
return mk_call_(cx, sp, pathexpr, args);
|
|
|
|
}
|
2012-01-26 17:34:05 -06:00
|
|
|
// e = expr, t = type
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_base_vec_e(cx: @ext_ctxt, sp: span, exprs: ~[@ast::expr])
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::expr {
|
2012-02-15 13:25:39 -06:00
|
|
|
let vecexpr = ast::expr_vec(exprs, ast::m_imm);
|
2012-07-11 16:31:35 -05:00
|
|
|
mk_expr(cx, sp, vecexpr)
|
2012-01-25 15:47:56 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_vstore_e(cx: @ext_ctxt, sp: span, expr: @ast::expr,
|
2013-01-29 16:41:40 -06:00
|
|
|
vst: ast::expr_vstore) ->
|
2012-06-25 22:00:39 -05:00
|
|
|
@ast::expr {
|
2012-06-29 16:38:33 -05:00
|
|
|
mk_expr(cx, sp, ast::expr_vstore(expr, vst))
|
2012-06-25 22:00:39 -05:00
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_uniq_vec_e(cx: @ext_ctxt, sp: span, exprs: ~[@ast::expr])
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::expr {
|
2012-09-11 23:25:01 -05:00
|
|
|
mk_vstore_e(cx, sp, mk_base_vec_e(cx, sp, exprs), ast::expr_vstore_uniq)
|
2012-06-25 22:00:39 -05:00
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_slice_vec_e(cx: @ext_ctxt, sp: span, exprs: ~[@ast::expr])
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::expr {
|
2012-11-20 18:07:57 -06:00
|
|
|
mk_vstore_e(cx, sp, mk_base_vec_e(cx, sp, exprs),
|
|
|
|
ast::expr_vstore_slice)
|
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_base_str(cx: @ext_ctxt, sp: span, s: ~str) -> @ast::expr {
|
2012-07-13 17:54:39 -05:00
|
|
|
let lit = ast::lit_str(@s);
|
2012-08-01 19:30:05 -05:00
|
|
|
return mk_lit(cx, sp, lit);
|
2012-07-13 17:54:39 -05:00
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_uniq_str(cx: @ext_ctxt, sp: span, s: ~str) -> @ast::expr {
|
2012-09-11 23:25:01 -05:00
|
|
|
mk_vstore_e(cx, sp, mk_base_str(cx, sp, s), ast::expr_vstore_uniq)
|
2012-07-13 17:54:39 -05:00
|
|
|
}
|
2013-02-21 02:16:31 -06:00
|
|
|
pub fn mk_field(sp: span, f: &Field) -> ast::field {
|
2013-01-30 11:56:33 -06:00
|
|
|
codemap::spanned {
|
2013-01-14 23:36:27 -06:00
|
|
|
node: ast::field_ { mutbl: ast::m_imm, ident: f.ident, expr: f.ex },
|
|
|
|
span: sp,
|
|
|
|
}
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
2013-02-21 02:16:31 -06:00
|
|
|
pub fn mk_fields(sp: span, fields: ~[Field]) -> ~[ast::field] {
|
2013-02-15 03:15:53 -06:00
|
|
|
fields.map(|f| mk_field(sp, f))
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_struct_e(cx: @ext_ctxt,
|
2013-02-24 23:27:51 -06:00
|
|
|
sp: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
ctor_path: ~[ast::ident],
|
|
|
|
fields: ~[Field])
|
2013-02-24 23:27:51 -06:00
|
|
|
-> @ast::expr {
|
2012-11-08 19:00:55 -06:00
|
|
|
mk_expr(cx, sp,
|
|
|
|
ast::expr_struct(mk_raw_path(sp, ctor_path),
|
|
|
|
mk_fields(sp, fields),
|
|
|
|
option::None::<@ast::expr>))
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_global_struct_e(cx: @ext_ctxt,
|
2013-02-24 23:27:51 -06:00
|
|
|
sp: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
ctor_path: ~[ast::ident],
|
|
|
|
fields: ~[Field])
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::expr {
|
2013-01-24 12:33:20 -06:00
|
|
|
mk_expr(cx, sp,
|
|
|
|
ast::expr_struct(mk_raw_path_global(sp, ctor_path),
|
|
|
|
mk_fields(sp, fields),
|
|
|
|
option::None::<@ast::expr>))
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_glob_use(cx: @ext_ctxt,
|
2013-02-24 23:27:51 -06:00
|
|
|
sp: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
path: ~[ast::ident]) -> @ast::view_item {
|
2013-01-30 11:56:33 -06:00
|
|
|
let glob = @codemap::spanned {
|
2012-12-27 13:36:00 -06:00
|
|
|
node: ast::view_path_glob(mk_raw_path(sp, path), cx.next_id()),
|
|
|
|
span: sp,
|
|
|
|
};
|
2013-02-17 20:45:00 -06:00
|
|
|
@ast::view_item { node: ast::view_item_use(~[glob]),
|
2013-01-13 18:51:48 -06:00
|
|
|
attrs: ~[],
|
|
|
|
vis: ast::private,
|
|
|
|
span: sp }
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_local(cx: @ext_ctxt, sp: span, mutbl: bool,
|
2013-01-29 16:41:40 -06:00
|
|
|
ident: ast::ident, ex: @ast::expr) -> @ast::stmt {
|
2012-12-06 13:01:58 -06:00
|
|
|
|
2013-01-14 22:52:28 -06:00
|
|
|
let pat = @ast::pat {
|
|
|
|
id: cx.next_id(),
|
|
|
|
node: ast::pat_ident(
|
2013-01-10 12:59:58 -06:00
|
|
|
ast::bind_by_copy,
|
2013-01-14 22:52:28 -06:00
|
|
|
mk_raw_path(sp, ~[ident]),
|
|
|
|
None),
|
|
|
|
span: sp,
|
|
|
|
};
|
2013-01-15 16:59:39 -06:00
|
|
|
let ty = @ast::Ty { id: cx.next_id(), node: ast::ty_infer, span: sp };
|
2013-01-30 11:56:33 -06:00
|
|
|
let local = @codemap::spanned {
|
2013-01-14 22:52:28 -06:00
|
|
|
node: ast::local_ {
|
|
|
|
is_mutbl: mutbl,
|
|
|
|
ty: ty,
|
|
|
|
pat: pat,
|
|
|
|
init: Some(ex),
|
|
|
|
id: cx.next_id(),
|
|
|
|
},
|
|
|
|
span: sp,
|
|
|
|
};
|
2013-01-30 11:56:33 -06:00
|
|
|
let decl = codemap::spanned {node: ast::decl_local(~[local]), span: sp};
|
|
|
|
@codemap::spanned { node: ast::stmt_decl(@decl, cx.next_id()), span: sp }
|
2012-12-06 13:01:58 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_block(cx: @ext_ctxt, span: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
view_items: ~[@ast::view_item],
|
|
|
|
stmts: ~[@ast::stmt],
|
2013-01-29 16:41:40 -06:00
|
|
|
expr: Option<@ast::expr>) -> @ast::expr {
|
2013-01-30 11:56:33 -06:00
|
|
|
let blk = codemap::spanned {
|
2013-01-14 21:35:08 -06:00
|
|
|
node: ast::blk_ {
|
|
|
|
view_items: view_items,
|
|
|
|
stmts: stmts,
|
|
|
|
expr: expr,
|
|
|
|
id: cx.next_id(),
|
|
|
|
rules: ast::default_blk,
|
|
|
|
},
|
|
|
|
span: span,
|
|
|
|
};
|
|
|
|
mk_expr(cx, span, ast::expr_block(blk))
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_block_(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
span: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
stmts: ~[@ast::stmt])
|
2013-01-29 16:41:40 -06:00
|
|
|
-> ast::blk {
|
2013-01-30 11:56:33 -06:00
|
|
|
codemap::spanned {
|
2013-01-14 21:35:08 -06:00
|
|
|
node: ast::blk_ {
|
2012-11-20 20:27:13 -06:00
|
|
|
view_items: ~[],
|
2013-01-14 21:35:08 -06:00
|
|
|
stmts: stmts,
|
2012-11-20 20:27:13 -06:00
|
|
|
expr: None,
|
|
|
|
id: cx.next_id(),
|
2013-01-14 21:35:08 -06:00
|
|
|
rules: ast::default_blk,
|
2012-11-20 20:27:13 -06:00
|
|
|
},
|
2013-01-14 21:35:08 -06:00
|
|
|
span: span,
|
2012-11-20 20:27:13 -06:00
|
|
|
}
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_simple_block(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
span: span,
|
|
|
|
expr: @ast::expr)
|
|
|
|
-> ast::blk {
|
2013-01-30 11:56:33 -06:00
|
|
|
codemap::spanned {
|
2013-01-14 21:35:08 -06:00
|
|
|
node: ast::blk_ {
|
|
|
|
view_items: ~[],
|
|
|
|
stmts: ~[],
|
|
|
|
expr: Some(expr),
|
|
|
|
id: cx.next_id(),
|
|
|
|
rules: ast::default_blk,
|
|
|
|
},
|
|
|
|
span: span,
|
|
|
|
}
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
2013-04-10 18:31:51 -05:00
|
|
|
pub fn mk_lambda_(cx: @ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
fn_decl: ast::fn_decl,
|
|
|
|
blk: ast::blk)
|
|
|
|
-> @ast::expr {
|
|
|
|
mk_expr(cx, span, ast::expr_fn_block(fn_decl, blk))
|
|
|
|
}
|
|
|
|
pub fn mk_lambda(cx: @ext_ctxt,
|
2013-04-08 20:53:39 -05:00
|
|
|
span: span,
|
|
|
|
fn_decl: ast::fn_decl,
|
|
|
|
expr: @ast::expr)
|
|
|
|
-> @ast::expr {
|
2013-04-10 18:31:51 -05:00
|
|
|
let blk = mk_simple_block(cx, span, expr);
|
|
|
|
mk_lambda_(cx, span, fn_decl, blk)
|
|
|
|
}
|
|
|
|
pub fn mk_lambda_stmts(cx: @ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
fn_decl: ast::fn_decl,
|
|
|
|
stmts: ~[@ast::stmt])
|
|
|
|
-> @ast::expr {
|
|
|
|
let blk = mk_block(cx, span, ~[], stmts, None);
|
|
|
|
mk_lambda(cx, span, fn_decl, blk)
|
|
|
|
}
|
2013-04-08 20:53:39 -05:00
|
|
|
pub fn mk_lambda_no_args(cx: @ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
expr: @ast::expr)
|
|
|
|
-> @ast::expr {
|
|
|
|
let fn_decl = mk_fn_decl(~[], mk_ty_infer(cx, span));
|
|
|
|
mk_lambda(cx, span, fn_decl, expr)
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_copy(cx: @ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
|
2012-11-08 19:00:55 -06:00
|
|
|
mk_expr(cx, sp, ast::expr_copy(e))
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_managed(cx: @ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
|
2012-11-08 19:00:55 -06:00
|
|
|
mk_expr(cx, sp, ast::expr_unary(ast::box(ast::m_imm), e))
|
2012-01-25 15:47:56 -06:00
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_pat(cx: @ext_ctxt, span: span, pat: ast::pat_) -> @ast::pat {
|
2013-01-14 22:52:28 -06:00
|
|
|
@ast::pat { id: cx.next_id(), node: pat, span: span }
|
2012-11-20 14:59:37 -06:00
|
|
|
}
|
2013-04-08 20:53:39 -05:00
|
|
|
pub fn mk_pat_wild(cx: @ext_ctxt, span: span) -> @ast::pat {
|
|
|
|
mk_pat(cx, span, ast::pat_wild)
|
|
|
|
}
|
|
|
|
pub fn mk_pat_lit(cx: @ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
expr: @ast::expr) -> @ast::pat {
|
|
|
|
mk_pat(cx, span, ast::pat_lit(expr))
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_pat_ident(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
span: span,
|
2013-01-10 12:59:58 -06:00
|
|
|
ident: ast::ident) -> @ast::pat {
|
|
|
|
mk_pat_ident_with_binding_mode(cx, span, ident, ast::bind_by_copy)
|
2013-01-24 18:24:45 -06:00
|
|
|
}
|
2013-04-08 20:53:39 -05:00
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_pat_ident_with_binding_mode(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
span: span,
|
|
|
|
ident: ast::ident,
|
|
|
|
bm: ast::binding_mode) -> @ast::pat {
|
2012-11-20 14:59:37 -06:00
|
|
|
let path = mk_raw_path(span, ~[ ident ]);
|
2013-01-24 18:24:45 -06:00
|
|
|
let pat = ast::pat_ident(bm, path, None);
|
2013-02-15 03:15:53 -06:00
|
|
|
mk_pat(cx, span, pat)
|
2012-11-20 14:59:37 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_pat_enum(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
span: span,
|
2013-03-26 19:00:35 -05:00
|
|
|
path: @ast::Path,
|
2013-04-17 11:15:08 -05:00
|
|
|
subpats: ~[@ast::pat])
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::pat {
|
2013-02-15 03:15:53 -06:00
|
|
|
let pat = ast::pat_enum(path, Some(subpats));
|
|
|
|
mk_pat(cx, span, pat)
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_pat_struct(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
span: span,
|
2013-03-26 19:00:35 -05:00
|
|
|
path: @ast::Path,
|
2013-04-17 11:15:08 -05:00
|
|
|
field_pats: ~[ast::field_pat])
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::pat {
|
2013-02-15 03:15:53 -06:00
|
|
|
let pat = ast::pat_struct(path, field_pats, false);
|
|
|
|
mk_pat(cx, span, pat)
|
2012-12-10 15:04:04 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_bool(cx: @ext_ctxt, span: span, value: bool) -> @ast::expr {
|
2013-01-30 11:56:33 -06:00
|
|
|
let lit_expr = ast::expr_lit(@codemap::spanned {
|
|
|
|
node: ast::lit_bool(value),
|
|
|
|
span: span });
|
2013-02-15 03:15:53 -06:00
|
|
|
build::mk_expr(cx, span, lit_expr)
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_stmt(cx: @ext_ctxt, span: span, expr: @ast::expr) -> @ast::stmt {
|
2012-11-20 20:27:13 -06:00
|
|
|
let stmt_ = ast::stmt_semi(expr, cx.next_id());
|
2013-02-15 03:15:53 -06:00
|
|
|
@codemap::spanned { node: stmt_, span: span }
|
2012-11-20 20:27:13 -06:00
|
|
|
}
|
2013-05-06 10:23:51 -05:00
|
|
|
|
|
|
|
pub fn mk_ty_mt(ty: @ast::Ty, mutbl: ast::mutability) -> ast::mt {
|
|
|
|
ast::mt {
|
|
|
|
ty: ty,
|
|
|
|
mutbl: mutbl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_ty(cx: @ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
ty: ast::ty_) -> @ast::Ty {
|
|
|
|
@ast::Ty {
|
|
|
|
id: cx.next_id(),
|
|
|
|
span: span,
|
|
|
|
node: ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_ty_path(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
span: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
idents: ~[ ast::ident ])
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::Ty {
|
2012-11-20 20:27:13 -06:00
|
|
|
let ty = build::mk_raw_path(span, idents);
|
2013-05-06 10:23:51 -05:00
|
|
|
mk_ty_path_path(cx, span, ty)
|
2012-11-20 20:27:13 -06:00
|
|
|
}
|
2013-05-06 10:23:51 -05:00
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_ty_path_global(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
span: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
idents: ~[ ast::ident ])
|
2013-01-29 16:41:40 -06:00
|
|
|
-> @ast::Ty {
|
2012-12-23 16:41:37 -06:00
|
|
|
let ty = build::mk_raw_path_global(span, idents);
|
2013-05-06 10:23:51 -05:00
|
|
|
mk_ty_path_path(cx, span, ty)
|
2012-12-23 16:41:37 -06:00
|
|
|
}
|
2013-05-06 10:23:51 -05:00
|
|
|
|
|
|
|
pub fn mk_ty_path_path(cx: @ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
path: @ast::Path)
|
|
|
|
-> @ast::Ty {
|
|
|
|
let ty = ast::ty_path(path, cx.next_id());
|
|
|
|
mk_ty(cx, span, ty)
|
|
|
|
}
|
|
|
|
|
2013-04-10 18:31:51 -05:00
|
|
|
pub fn mk_ty_rptr(cx: @ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
ty: @ast::Ty,
|
2013-05-06 10:23:51 -05:00
|
|
|
lifetime: Option<@ast::Lifetime>,
|
2013-04-10 18:31:51 -05:00
|
|
|
mutbl: ast::mutability)
|
|
|
|
-> @ast::Ty {
|
2013-05-06 10:23:51 -05:00
|
|
|
mk_ty(cx, span,
|
|
|
|
ast::ty_rptr(lifetime, mk_ty_mt(ty, mutbl)))
|
|
|
|
}
|
|
|
|
pub fn mk_ty_uniq(cx: @ext_ctxt, span: span, ty: @ast::Ty) -> @ast::Ty {
|
|
|
|
mk_ty(cx, span, ast::ty_uniq(mk_ty_mt(ty, ast::m_imm)))
|
2013-04-10 18:31:51 -05:00
|
|
|
}
|
2013-05-06 10:23:51 -05:00
|
|
|
pub fn mk_ty_box(cx: @ext_ctxt, span: span,
|
|
|
|
ty: @ast::Ty, mutbl: ast::mutability) -> @ast::Ty {
|
|
|
|
mk_ty(cx, span, ast::ty_box(mk_ty_mt(ty, mutbl)))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-10 18:31:51 -05:00
|
|
|
pub fn mk_ty_infer(cx: @ext_ctxt, span: span) -> @ast::Ty {
|
2013-05-06 10:23:51 -05:00
|
|
|
mk_ty(cx, span, ast::ty_infer)
|
2013-04-10 18:31:51 -05:00
|
|
|
}
|
2013-03-27 05:16:28 -05:00
|
|
|
pub fn mk_trait_ref_global(cx: @ext_ctxt,
|
|
|
|
span: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
idents: ~[ ast::ident ])
|
2013-03-27 05:16:28 -05:00
|
|
|
-> @ast::trait_ref
|
|
|
|
{
|
2013-04-10 18:31:51 -05:00
|
|
|
mk_trait_ref_(cx, build::mk_raw_path_global(span, idents))
|
|
|
|
}
|
|
|
|
pub fn mk_trait_ref_(cx: @ext_ctxt, path: @ast::Path) -> @ast::trait_ref {
|
2013-03-27 05:16:28 -05:00
|
|
|
@ast::trait_ref {
|
|
|
|
path: path,
|
|
|
|
ref_id: cx.next_id()
|
|
|
|
}
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_simple_ty_path(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
span: span,
|
|
|
|
ident: ast::ident)
|
|
|
|
-> @ast::Ty {
|
2012-11-20 20:27:13 -06:00
|
|
|
mk_ty_path(cx, span, ~[ ident ])
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_arg(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
span: span,
|
|
|
|
ident: ast::ident,
|
|
|
|
ty: @ast::Ty)
|
|
|
|
-> ast::arg {
|
2012-11-20 20:27:13 -06:00
|
|
|
let arg_pat = mk_pat_ident(cx, span, ident);
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::arg {
|
2013-01-03 10:45:07 -06:00
|
|
|
is_mutbl: false,
|
2012-11-20 20:27:13 -06:00
|
|
|
ty: ty,
|
|
|
|
pat: arg_pat,
|
|
|
|
id: cx.next_id()
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn mk_fn_decl(inputs: ~[ast::arg], output: @ast::Ty) -> ast::fn_decl {
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::fn_decl { inputs: inputs, output: output, cf: ast::return_val }
|
2012-11-20 20:27:13 -06:00
|
|
|
}
|
2013-04-10 18:31:51 -05:00
|
|
|
pub fn mk_trait_ty_param_bound_global(cx: @ext_ctxt,
|
|
|
|
span: span,
|
2013-04-17 11:15:08 -05:00
|
|
|
idents: ~[ast::ident])
|
2013-04-10 18:31:51 -05:00
|
|
|
-> ast::TyParamBound {
|
|
|
|
ast::TraitTyParamBound(mk_trait_ref_global(cx, span, idents))
|
|
|
|
}
|
|
|
|
pub fn mk_trait_ty_param_bound_(cx: @ext_ctxt,
|
|
|
|
path: @ast::Path) -> ast::TyParamBound {
|
|
|
|
ast::TraitTyParamBound(mk_trait_ref_(cx, path))
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_ty_param(cx: @ext_ctxt,
|
2013-01-29 16:41:40 -06:00
|
|
|
ident: ast::ident,
|
2013-02-14 23:50:03 -06:00
|
|
|
bounds: @OptVec<ast::TyParamBound>)
|
|
|
|
-> ast::TyParam {
|
|
|
|
ast::TyParam { ident: ident, id: cx.next_id(), bounds: bounds }
|
|
|
|
}
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn mk_lifetime(cx: @ext_ctxt,
|
2013-02-14 23:50:03 -06:00
|
|
|
span: span,
|
2013-04-08 20:53:39 -05:00
|
|
|
ident: ast::ident)
|
|
|
|
-> ast::Lifetime {
|
2013-02-14 23:50:03 -06:00
|
|
|
ast::Lifetime { id: cx.next_id(), span: span, ident: ident }
|
2012-11-20 21:20:55 -06:00
|
|
|
}
|
2013-04-08 20:53:39 -05:00
|
|
|
pub fn mk_arm(cx: @ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
pats: ~[@ast::pat],
|
|
|
|
expr: @ast::expr)
|
|
|
|
-> ast::arm {
|
|
|
|
ast::arm {
|
|
|
|
pats: pats,
|
|
|
|
guard: None,
|
|
|
|
body: mk_simple_block(cx, span, expr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn mk_unreachable(cx: @ext_ctxt, span: span) -> @ast::expr {
|
|
|
|
let loc = cx.codemap().lookup_char_pos(span.lo);
|
|
|
|
mk_call_global(
|
|
|
|
cx,
|
|
|
|
span,
|
|
|
|
~[
|
|
|
|
cx.ident_of(~"core"),
|
|
|
|
cx.ident_of(~"sys"),
|
2013-04-23 15:30:58 -05:00
|
|
|
cx.ident_of(~"FailWithCause"),
|
|
|
|
cx.ident_of(~"fail_with"),
|
2013-04-08 20:53:39 -05:00
|
|
|
],
|
|
|
|
~[
|
2013-04-23 15:30:58 -05:00
|
|
|
mk_base_str(cx, span, ~"internal error: entered unreachable code"),
|
|
|
|
mk_base_str(cx, span, loc.file.name),
|
2013-04-08 20:53:39 -05:00
|
|
|
mk_uint(cx, span, loc.line),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
pub fn mk_unreachable_arm(cx: @ext_ctxt, span: span) -> ast::arm {
|
|
|
|
mk_arm(cx, span, ~[mk_pat_wild(cx, span)], mk_unreachable(cx, span))
|
|
|
|
}
|
2013-05-08 14:26:34 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// Duplication functions
|
|
|
|
//
|
|
|
|
// These functions just duplicate AST nodes.
|
|
|
|
//
|
|
|
|
|
|
|
|
pub fn duplicate_expr(cx: @ext_ctxt, expr: @ast::expr) -> @ast::expr {
|
|
|
|
let folder = fold::default_ast_fold();
|
|
|
|
let folder = @fold::AstFoldFns {
|
|
|
|
new_id: |_| cx.next_id(),
|
|
|
|
..*folder
|
|
|
|
};
|
|
|
|
let folder = fold::make_fold(folder);
|
|
|
|
folder.fold_expr(expr)
|
|
|
|
}
|
|
|
|
|