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])
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::MRExpr(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])
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::MRExpr(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
53 * out if we wanted. */
54 pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
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);
61 base::MRExpr(cx.expr_str(topmost.call_site, filename))
64 pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
66 let s = pprust::tts_to_str(tts);
67 base::MRExpr(cx.expr_str(sp, token::intern_and_get_ident(s)))
70 pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
72 base::check_zero_tts(cx, sp, tts, "module_path!");
73 let string = cx.mod_path()
74 .map(|x| token::get_ident(*x).get().to_str())
76 base::MRExpr(cx.expr_str(sp, token::intern_and_get_ident(string)))
79 // include! : parse the given file as an expr
80 // This is generally a bad idea because it's going to behave
82 pub fn expand_include(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
84 let file = match get_single_str_from_tts(cx, sp, tts, "include!") {
86 None => return MacResult::dummy_expr(sp),
88 // The file will be added to the code map by the parser
90 parse::new_sub_parser_from_file(cx.parse_sess(),
96 base::MRExpr(p.parse_expr())
99 // include_str! : read the given file, insert it as a literal string expr
100 pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
102 let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
104 None => return MacResult::dummy_expr(sp)
106 let file = res_rel_file(cx, sp, &Path::new(file));
107 let bytes = match File::open(&file).read_to_end() {
109 cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e));
110 return MacResult::dummy_expr(sp);
114 match str::from_utf8_owned(bytes) {
116 // Add this input file to the code map to make it available as
117 // dependency information
118 let filename = file.display().to_str();
119 let interned = token::intern_and_get_ident(src);
120 cx.parse_sess.cm.new_filemap(filename, src);
122 base::MRExpr(cx.expr_str(sp, interned))
125 cx.span_err(sp, format!("{} wasn't a utf-8 file", file.display()));
126 return MacResult::dummy_expr(sp);
131 pub fn expand_include_bin(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
134 let file = match get_single_str_from_tts(cx, sp, tts, "include_bin!") {
136 None => return MacResult::dummy_expr(sp)
138 let file = res_rel_file(cx, sp, &Path::new(file));
139 match File::open(&file).read_to_end() {
141 cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e));
142 return MacResult::dummy_expr(sp);
145 let bytes = bytes.iter().map(|x| *x).collect();
146 base::MRExpr(cx.expr_lit(sp, ast::LitBinary(Rc::new(bytes))))
151 // recur along an ExpnInfo chain to find the original expression
152 fn topmost_expn_info(expn_info: @codemap::ExpnInfo) -> @codemap::ExpnInfo {
154 ExpnInfo { call_site: ref call_site, .. } => {
155 match call_site.expn_info {
156 Some(next_expn_info) => {
157 match *next_expn_info {
159 callee: NameAndSpan { name: ref name, .. },
162 // Don't recurse into file using "include!"
163 if "include" == *name {
166 topmost_expn_info(next_expn_info)
177 // resolve a file-system path to an absolute file-system path (if it
179 fn res_rel_file(cx: &mut ExtCtxt, sp: codemap::Span, arg: &Path) -> Path {
180 // NB: relative paths are resolved relative to the compilation unit
181 if !arg.is_absolute() {
182 let mut cu = Path::new(cx.codemap().span_to_filename(sp));