1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
16 use ext::base::ext_ctxt;
22 pub fn mk_expr(cx: ext_ctxt,
28 callee_id: cx.next_id(),
34 pub fn mk_lit(cx: ext_ctxt, sp: span, lit: ast::lit_) -> @ast::expr {
35 let sp_lit = @codemap::spanned { node: lit, span: sp };
36 mk_expr(cx, sp, ast::expr_lit(sp_lit))
38 pub fn mk_int(cx: ext_ctxt, sp: span, i: int) -> @ast::expr {
39 let lit = ast::lit_int(i as i64, ast::ty_i);
40 return mk_lit(cx, sp, lit);
42 pub fn mk_uint(cx: ext_ctxt, sp: span, u: uint) -> @ast::expr {
43 let lit = ast::lit_uint(u as u64, ast::ty_u);
44 return mk_lit(cx, sp, lit);
46 pub fn mk_u8(cx: ext_ctxt, sp: span, u: u8) -> @ast::expr {
47 let lit = ast::lit_uint(u as u64, ast::ty_u8);
48 return mk_lit(cx, sp, lit);
50 pub fn mk_binary(cx: ext_ctxt, sp: span, op: ast::binop,
51 lhs: @ast::expr, rhs: @ast::expr) -> @ast::expr {
52 cx.next_id(); // see ast_util::op_expr_callee_id
53 mk_expr(cx, sp, ast::expr_binary(op, lhs, rhs))
55 pub fn mk_unary(cx: ext_ctxt, sp: span, op: ast::unop, e: @ast::expr)
57 cx.next_id(); // see ast_util::op_expr_callee_id
58 mk_expr(cx, sp, ast::expr_unary(op, e))
60 pub fn mk_raw_path(sp: span, idents: ~[ast::ident]) -> @ast::path {
61 let p = @ast::path { span: sp,
68 pub fn mk_raw_path_(sp: span,
69 idents: ~[ast::ident],
72 @ast::path { span: sp,
78 pub fn mk_raw_path_global(sp: span, idents: ~[ast::ident]) -> @ast::path {
79 @ast::path { span: sp,
85 pub fn mk_path(cx: ext_ctxt, sp: span, idents: ~[ast::ident]) -> @ast::expr {
86 mk_expr(cx, sp, ast::expr_path(mk_raw_path(sp, idents)))
88 pub fn mk_path_global(cx: ext_ctxt, sp: span, idents: ~[ast::ident])
90 mk_expr(cx, sp, ast::expr_path(mk_raw_path_global(sp, idents)))
92 pub fn mk_access_(cx: ext_ctxt, sp: span, p: @ast::expr, m: ast::ident)
94 mk_expr(cx, sp, ast::expr_field(p, m, ~[]))
96 pub fn mk_access(cx: ext_ctxt, sp: span, p: ~[ast::ident], m: ast::ident)
98 let pathexpr = mk_path(cx, sp, p);
99 return mk_access_(cx, sp, pathexpr, m);
101 pub fn mk_addr_of(cx: ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
102 return mk_expr(cx, sp, ast::expr_addr_of(ast::m_imm, e));
104 pub fn mk_call_(cx: ext_ctxt, sp: span, fn_expr: @ast::expr,
105 args: ~[@ast::expr]) -> @ast::expr {
106 mk_expr(cx, sp, ast::expr_call(fn_expr, args, ast::NoSugar))
108 pub fn mk_call(cx: ext_ctxt, sp: span, fn_path: ~[ast::ident],
109 args: ~[@ast::expr]) -> @ast::expr {
110 let pathexpr = mk_path(cx, sp, fn_path);
111 return mk_call_(cx, sp, pathexpr, args);
113 pub fn mk_call_global(cx: ext_ctxt, sp: span, fn_path: ~[ast::ident],
114 args: ~[@ast::expr]) -> @ast::expr {
115 let pathexpr = mk_path_global(cx, sp, fn_path);
116 return mk_call_(cx, sp, pathexpr, args);
118 // e = expr, t = type
119 pub fn mk_base_vec_e(cx: ext_ctxt, sp: span, exprs: ~[@ast::expr])
121 let vecexpr = ast::expr_vec(exprs, ast::m_imm);
122 mk_expr(cx, sp, vecexpr)
124 pub fn mk_vstore_e(cx: ext_ctxt, sp: span, expr: @ast::expr,
125 vst: ast::expr_vstore) ->
127 mk_expr(cx, sp, ast::expr_vstore(expr, vst))
129 pub fn mk_uniq_vec_e(cx: ext_ctxt, sp: span, exprs: ~[@ast::expr])
131 mk_vstore_e(cx, sp, mk_base_vec_e(cx, sp, exprs), ast::expr_vstore_uniq)
133 pub fn mk_slice_vec_e(cx: ext_ctxt, sp: span, exprs: ~[@ast::expr])
135 mk_vstore_e(cx, sp, mk_base_vec_e(cx, sp, exprs),
136 ast::expr_vstore_slice)
138 pub fn mk_fixed_vec_e(cx: ext_ctxt, sp: span, exprs: ~[@ast::expr])
140 mk_vstore_e(cx, sp, mk_base_vec_e(cx, sp, exprs),
141 ast::expr_vstore_fixed(None))
143 pub fn mk_base_str(cx: ext_ctxt, sp: span, s: ~str) -> @ast::expr {
144 let lit = ast::lit_str(@s);
145 return mk_lit(cx, sp, lit);
147 pub fn mk_uniq_str(cx: ext_ctxt, sp: span, s: ~str) -> @ast::expr {
148 mk_vstore_e(cx, sp, mk_base_str(cx, sp, s), ast::expr_vstore_uniq)
150 pub fn mk_field(sp: span, f: &{ident: ast::ident, ex: @ast::expr})
153 node: ast::field_ { mutbl: ast::m_imm, ident: f.ident, expr: f.ex },
157 pub fn mk_fields(sp: span, fields: ~[{ident: ast::ident, ex: @ast::expr}])
159 fields.map(|f| mk_field(sp, f))
161 pub fn mk_rec_e(cx: ext_ctxt,
163 fields: ~[{ident: ast::ident, ex: @ast::expr}])
165 mk_expr(cx, sp, ast::expr_rec(mk_fields(sp, fields),
166 option::None::<@ast::expr>))
168 pub fn mk_struct_e(cx: ext_ctxt,
170 ctor_path: ~[ast::ident],
171 fields: ~[{ident: ast::ident, ex: @ast::expr}])
174 ast::expr_struct(mk_raw_path(sp, ctor_path),
175 mk_fields(sp, fields),
176 option::None::<@ast::expr>))
178 pub fn mk_global_struct_e(cx: ext_ctxt,
180 ctor_path: ~[ast::ident],
181 fields: ~[{ident: ast::ident, ex: @ast::expr}])
184 ast::expr_struct(mk_raw_path_global(sp, ctor_path),
185 mk_fields(sp, fields),
186 option::None::<@ast::expr>))
188 pub fn mk_glob_use(cx: ext_ctxt,
190 path: ~[ast::ident]) -> @ast::view_item {
191 let glob = @codemap::spanned {
192 node: ast::view_path_glob(mk_raw_path(sp, path), cx.next_id()),
195 @ast::view_item { node: ast::view_item_use(~[glob]),
200 pub fn mk_local(cx: ext_ctxt, sp: span, mutbl: bool,
201 ident: ast::ident, ex: @ast::expr) -> @ast::stmt {
203 let pat = @ast::pat {
205 node: ast::pat_ident(
207 mk_raw_path(sp, ~[ident]),
211 let ty = @ast::Ty { id: cx.next_id(), node: ast::ty_infer, span: sp };
212 let local = @codemap::spanned {
222 let decl = codemap::spanned {node: ast::decl_local(~[local]), span: sp};
223 @codemap::spanned { node: ast::stmt_decl(@decl, cx.next_id()), span: sp }
225 pub fn mk_block(cx: ext_ctxt, span: span,
226 view_items: ~[@ast::view_item],
227 stmts: ~[@ast::stmt],
228 expr: Option<@ast::expr>) -> @ast::expr {
229 let blk = codemap::spanned {
231 view_items: view_items,
235 rules: ast::default_blk,
239 mk_expr(cx, span, ast::expr_block(blk))
241 pub fn mk_block_(cx: ext_ctxt,
243 +stmts: ~[@ast::stmt])
251 rules: ast::default_blk,
256 pub fn mk_simple_block(cx: ext_ctxt,
266 rules: ast::default_blk,
271 pub fn mk_copy(cx: ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
272 mk_expr(cx, sp, ast::expr_copy(e))
274 pub fn mk_managed(cx: ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
275 mk_expr(cx, sp, ast::expr_unary(ast::box(ast::m_imm), e))
277 pub fn mk_pat(cx: ext_ctxt, span: span, +pat: ast::pat_) -> @ast::pat {
278 @ast::pat { id: cx.next_id(), node: pat, span: span }
280 pub fn mk_pat_ident(cx: ext_ctxt,
282 ident: ast::ident) -> @ast::pat {
283 mk_pat_ident_with_binding_mode(cx, span, ident, ast::bind_by_copy)
285 pub fn mk_pat_ident_with_binding_mode(cx: ext_ctxt,
288 bm: ast::binding_mode) -> @ast::pat {
289 let path = mk_raw_path(span, ~[ ident ]);
290 let pat = ast::pat_ident(bm, path, None);
291 mk_pat(cx, span, pat)
293 pub fn mk_pat_enum(cx: ext_ctxt,
296 +subpats: ~[@ast::pat])
298 let pat = ast::pat_enum(path, Some(subpats));
299 mk_pat(cx, span, pat)
301 pub fn mk_pat_struct(cx: ext_ctxt,
304 +field_pats: ~[ast::field_pat])
306 let pat = ast::pat_struct(path, field_pats, false);
307 mk_pat(cx, span, pat)
309 pub fn mk_bool(cx: ext_ctxt, span: span, value: bool) -> @ast::expr {
310 let lit_expr = ast::expr_lit(@codemap::spanned {
311 node: ast::lit_bool(value),
313 build::mk_expr(cx, span, lit_expr)
315 pub fn mk_stmt(cx: ext_ctxt, span: span, expr: @ast::expr) -> @ast::stmt {
316 let stmt_ = ast::stmt_semi(expr, cx.next_id());
317 @codemap::spanned { node: stmt_, span: span }
319 pub fn mk_ty_path(cx: ext_ctxt,
321 idents: ~[ ast::ident ])
323 let ty = build::mk_raw_path(span, idents);
324 let ty = ast::ty_path(ty, cx.next_id());
325 let ty = @ast::Ty { id: cx.next_id(), node: ty, span: span };
328 pub fn mk_ty_path_global(cx: ext_ctxt,
330 idents: ~[ ast::ident ])
332 let ty = build::mk_raw_path_global(span, idents);
333 let ty = ast::ty_path(ty, cx.next_id());
334 let ty = @ast::Ty { id: cx.next_id(), node: ty, span: span };
337 pub fn mk_simple_ty_path(cx: ext_ctxt,
341 mk_ty_path(cx, span, ~[ ident ])
343 pub fn mk_arg(cx: ext_ctxt,
348 let arg_pat = mk_pat_ident(cx, span, ident);
350 mode: ast::infer(cx.next_id()),
357 pub fn mk_fn_decl(+inputs: ~[ast::arg], output: @ast::Ty) -> ast::fn_decl {
358 ast::fn_decl { inputs: inputs, output: output, cf: ast::return_val }
360 pub fn mk_ty_param(cx: ext_ctxt,
362 bounds: @~[ast::ty_param_bound])
364 ast::ty_param { ident: ident, id: cx.next_id(), bounds: bounds }