1 // Copyright 2012-2013 The Rust Project Developers. See the
2 // COPYRIGHT 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::{FileMap, Loc, Pos, ExpandedFrom, span};
14 use codemap::{CallInfo, NameAndSpan};
17 use ext::build::{mk_base_vec_e, mk_uint, mk_u8, mk_base_str};
21 // These macros all relate to the file system; they either return
22 // the column/row/filename of the expression, or they include
23 // a given file into the current one.
25 /* line!(): expands to the current line number */
26 pub fn expand_line(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
28 base::check_zero_tts(cx, sp, tts, "line!");
30 let topmost = topmost_expn_info(cx.backtrace().get());
31 let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo);
33 base::MRExpr(mk_uint(cx, topmost.call_site, loc.line))
36 /* col!(): expands to the current column number */
37 pub fn expand_col(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
39 base::check_zero_tts(cx, sp, tts, "col!");
41 let topmost = topmost_expn_info(cx.backtrace().get());
42 let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo);
43 base::MRExpr(mk_uint(cx, topmost.call_site, loc.col.to_uint()))
46 /* file!(): expands to the current filename */
47 /* The filemap (`loc.file`) contains a bunch more information we could spit
48 * out if we wanted. */
49 pub fn expand_file(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
51 base::check_zero_tts(cx, sp, tts, "file!");
53 let topmost = topmost_expn_info(cx.backtrace().get());
54 let Loc { file: @FileMap { name: filename, _ }, _ } =
55 cx.codemap().lookup_char_pos(topmost.call_site.lo);
56 base::MRExpr(mk_base_str(cx, topmost.call_site, filename))
59 pub fn expand_stringify(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
61 let s = pprust::tts_to_str(tts, cx.parse_sess().interner);
62 base::MRExpr(mk_base_str(cx, sp, s))
65 pub fn expand_mod(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
67 base::check_zero_tts(cx, sp, tts, "module_path!");
68 base::MRExpr(mk_base_str(cx, sp,
69 str::connect(cx.mod_path().map(
70 |x| cx.str_of(*x)), ~"::")))
73 // include! : parse the given file as an expr
74 // This is generally a bad idea because it's going to behave
76 pub fn expand_include(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
78 let file = get_single_str_from_tts(cx, sp, tts, "include!");
79 let p = parse::new_sub_parser_from_file(
80 cx.parse_sess(), cx.cfg(),
81 &res_rel_file(cx, sp, &Path(file)), sp);
82 base::MRExpr(p.parse_expr())
85 // include_str! : read the given file, insert it as a literal string expr
86 pub fn expand_include_str(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
88 let file = get_single_str_from_tts(cx, sp, tts, "include_str!");
89 let res = io::read_whole_file_str(&res_rel_file(cx, sp, &Path(file)));
91 result::Ok(_) => { /* Continue. */ }
92 result::Err(ref e) => {
93 cx.parse_sess().span_diagnostic.handler().fatal((*e));
97 base::MRExpr(mk_base_str(cx, sp, result::unwrap(res)))
100 pub fn expand_include_bin(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
102 let file = get_single_str_from_tts(cx, sp, tts, "include_bin!");
103 match io::read_whole_file(&res_rel_file(cx, sp, &Path(file))) {
105 let u8_exprs = vec::map(src, |char| {
108 base::MRExpr(mk_base_vec_e(cx, sp, u8_exprs))
110 result::Err(ref e) => {
111 cx.parse_sess().span_diagnostic.handler().fatal((*e))
116 // recur along an ExpnInfo chain to find the original expression
117 fn topmost_expn_info(expn_info: @codemap::ExpnInfo) -> @codemap::ExpnInfo {
119 ExpandedFrom(CallInfo { call_site: ref call_site, _ }) => {
120 match call_site.expn_info {
121 Some(next_expn_info) => {
122 match *next_expn_info {
123 ExpandedFrom(CallInfo {
124 callee: NameAndSpan { name: ref name, _ },
127 // Don't recurse into file using "include!"
128 if *name == ~"include" {
131 topmost_expn_info(next_expn_info)
142 // resolve a file-system path to an absolute file-system path (if it
144 fn res_rel_file(cx: @ext_ctxt, sp: codemap::span, arg: &Path) -> Path {
145 // NB: relative paths are resolved relative to the compilation unit
146 if !arg.is_absolute {
147 let cu = Path(cx.codemap().span_to_filename(sp));
148 cu.dir_path().push_many(arg.components)