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;
26 // These macros all relate to the file system; they either return
27 // the column/row/filename of the expression, or they include
28 // a given file into the current one.
30 /// line!(): expands to the current line number
31 pub fn expand_line(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
32 -> Box<base::MacResult> {
33 base::check_zero_tts(cx, sp, tts, "line!");
35 let topmost = topmost_expn_info(cx.backtrace().unwrap());
36 let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo);
38 base::MacExpr::new(cx.expr_uint(topmost.call_site, loc.line))
41 /* col!(): expands to the current column number */
42 pub fn expand_col(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
43 -> Box<base::MacResult> {
44 base::check_zero_tts(cx, sp, tts, "col!");
46 let topmost = topmost_expn_info(cx.backtrace().unwrap());
47 let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo);
48 base::MacExpr::new(cx.expr_uint(topmost.call_site, loc.col.to_uint()))
51 /// file!(): expands to the current filename */
52 /// The filemap (`loc.file`) contains a bunch more information we could spit
54 pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
55 -> Box<base::MacResult> {
56 base::check_zero_tts(cx, sp, tts, "file!");
58 let topmost = topmost_expn_info(cx.backtrace().unwrap());
59 let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo);
60 let filename = token::intern_and_get_ident(loc.file.name.as_slice());
61 base::MacExpr::new(cx.expr_str(topmost.call_site, filename))
64 pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
65 -> Box<base::MacResult> {
66 let s = pprust::tts_to_string(tts);
67 base::MacExpr::new(cx.expr_str(sp,
68 token::intern_and_get_ident(s.as_slice())))
71 pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
72 -> Box<base::MacResult> {
73 base::check_zero_tts(cx, sp, tts, "module_path!");
74 let string = cx.mod_path()
76 .map(|x| token::get_ident(*x).get().to_string())
77 .collect::<Vec<String>>()
79 base::MacExpr::new(cx.expr_str(
81 token::intern_and_get_ident(string.as_slice())))
84 /// include! : parse the given file as an expr
85 /// This is generally a bad idea because it's going to behave
87 pub fn expand_include(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
88 -> Box<base::MacResult> {
89 let file = match get_single_str_from_tts(cx, sp, tts, "include!") {
91 None => return DummyResult::expr(sp),
93 // The file will be added to the code map by the parser
95 parse::new_sub_parser_from_file(cx.parse_sess(),
103 base::MacExpr::new(p.parse_expr())
106 // include_str! : read the given file, insert it as a literal string expr
107 pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
108 -> Box<base::MacResult> {
109 let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
111 None => return DummyResult::expr(sp)
113 let file = res_rel_file(cx, sp, &Path::new(file));
114 let bytes = match File::open(&file).read_to_end() {
117 format!("couldn't read {}: {}",
120 return DummyResult::expr(sp);
124 match String::from_utf8(bytes) {
126 // Add this input file to the code map to make it available as
127 // dependency information
128 let filename = file.display().to_string();
129 let interned = token::intern_and_get_ident(src.as_slice());
130 cx.codemap().new_filemap(filename, src);
132 base::MacExpr::new(cx.expr_str(sp, interned))
136 format!("{} wasn't a utf-8 file",
137 file.display()).as_slice());
138 return DummyResult::expr(sp);
143 pub fn expand_include_bin(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
144 -> Box<base::MacResult> {
145 let file = match get_single_str_from_tts(cx, sp, tts, "include_bin!") {
147 None => return DummyResult::expr(sp)
149 let file = res_rel_file(cx, sp, &Path::new(file));
150 match File::open(&file).read_to_end() {
153 format!("couldn't read {}: {}",
156 return DummyResult::expr(sp);
159 let bytes = bytes.iter().map(|x| *x).collect();
160 base::MacExpr::new(cx.expr_lit(sp, ast::LitBinary(Rc::new(bytes))))
165 // recur along an ExpnInfo chain to find the original expression
166 fn topmost_expn_info(expn_info: Gc<codemap::ExpnInfo>) -> Gc<codemap::ExpnInfo> {
168 ExpnInfo { call_site: ref call_site, .. } => {
169 match call_site.expn_info {
170 Some(next_expn_info) => {
171 match *next_expn_info {
173 callee: NameAndSpan { name: ref name, .. },
176 // Don't recurse into file using "include!"
177 if "include" == name.as_slice() {
180 topmost_expn_info(next_expn_info)
191 // resolve a file-system path to an absolute file-system path (if it
193 fn res_rel_file(cx: &mut ExtCtxt, sp: codemap::Span, arg: &Path) -> Path {
194 // NB: relative paths are resolved relative to the compilation unit
195 if !arg.is_absolute() {
196 let mut cu = Path::new(cx.codemap().span_to_filename(sp));