1 // Copyright 2012-2013 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.
13 use codemap::{Pos, Span};
14 use codemap::{ExpnInfo, NameAndSpan};
17 use ext::build::AstBuilder;
19 use parse::token::get_ident_interner;
27 // These macros all relate to the file system; they either return
28 // the column/row/filename of the expression, or they include
29 // a given file into the current one.
31 /* line!(): expands to the current line number */
32 pub fn expand_line(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
34 base::check_zero_tts(cx, sp, tts, "line!");
36 let topmost = topmost_expn_info(cx.backtrace().unwrap());
37 let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo);
39 base::MRExpr(cx.expr_uint(topmost.call_site, loc.line))
42 /* col!(): expands to the current column number */
43 pub fn expand_col(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
45 base::check_zero_tts(cx, sp, tts, "col!");
47 let topmost = topmost_expn_info(cx.backtrace().unwrap());
48 let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo);
49 base::MRExpr(cx.expr_uint(topmost.call_site, loc.col.to_uint()))
52 /* file!(): expands to the current filename */
53 /* The filemap (`loc.file`) contains a bunch more information we could spit
54 * out if we wanted. */
55 pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
57 base::check_zero_tts(cx, sp, tts, "file!");
59 let topmost = topmost_expn_info(cx.backtrace().unwrap());
60 let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo);
61 let filename = token::intern_and_get_ident(loc.file.name);
62 base::MRExpr(cx.expr_str(topmost.call_site, filename))
65 pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
67 let s = pprust::tts_to_str(tts, get_ident_interner());
68 base::MRExpr(cx.expr_str(sp, token::intern_and_get_ident(s)))
71 pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
73 base::check_zero_tts(cx, sp, tts, "module_path!");
74 let string = cx.mod_path()
76 let interned_str = token::get_ident(x.name);
77 interned_str.get().to_str()
80 base::MRExpr(cx.expr_str(sp, token::intern_and_get_ident(string)))
83 // include! : parse the given file as an expr
84 // This is generally a bad idea because it's going to behave
86 pub fn expand_include(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
88 let file = match get_single_str_from_tts(cx, sp, tts, "include!") {
90 None => return MacResult::dummy_expr(),
92 // The file will be added to the code map by the parser
94 parse::new_sub_parser_from_file(cx.parse_sess(),
100 base::MRExpr(p.parse_expr())
103 // include_str! : read the given file, insert it as a literal string expr
104 pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
106 let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
108 None => return MacResult::dummy_expr()
110 let file = res_rel_file(cx, sp, &Path::new(file));
111 let bytes = match io::result(|| File::open(&file).read_to_end()) {
113 cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e.desc));
114 return MacResult::dummy_expr();
118 match str::from_utf8_owned(bytes) {
120 // Add this input file to the code map to make it available as
121 // dependency information
122 let filename = file.display().to_str();
123 let interned = token::intern_and_get_ident(src);
124 cx.parse_sess.cm.new_filemap(filename, src);
126 base::MRExpr(cx.expr_str(sp, interned))
129 cx.span_err(sp, format!("{} wasn't a utf-8 file", file.display()));
130 return MacResult::dummy_expr();
135 pub fn expand_include_bin(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
140 let file = match get_single_str_from_tts(cx, sp, tts, "include_bin!") {
142 None => return MacResult::dummy_expr()
144 let file = res_rel_file(cx, sp, &Path::new(file));
145 match io::result(|| File::open(&file).read_to_end()) {
147 cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e.desc));
148 return MacResult::dummy_expr();
151 let bytes = at_vec::to_managed_move(bytes);
152 base::MRExpr(cx.expr_lit(sp, ast::LitBinary(bytes)))
157 // recur along an ExpnInfo chain to find the original expression
158 fn topmost_expn_info(expn_info: @codemap::ExpnInfo) -> @codemap::ExpnInfo {
160 ExpnInfo { call_site: ref call_site, .. } => {
161 match call_site.expn_info {
162 Some(next_expn_info) => {
163 match *next_expn_info {
165 callee: NameAndSpan { name: ref name, .. },
168 // Don't recurse into file using "include!"
169 if "include" == *name {
172 topmost_expn_info(next_expn_info)
183 // resolve a file-system path to an absolute file-system path (if it
185 fn res_rel_file(cx: &mut ExtCtxt, sp: codemap::Span, arg: &Path) -> Path {
186 // NB: relative paths are resolved relative to the compilation unit
187 if !arg.is_absolute() {
188 let mut cu = Path::new(cx.codemap().span_to_filename(sp));