1 use rustc_parse::{self, DirectoryOwnership, new_sub_parser_from_file, parser::Parser};
3 use syntax::print::pprust;
5 use syntax::symbol::Symbol;
7 use syntax::tokenstream::TokenStream;
8 use syntax::early_buffered_lints::BufferedEarlyLintId;
9 use syntax_expand::panictry;
10 use syntax_expand::base::{self, *};
12 use smallvec::SmallVec;
13 use syntax_pos::{self, Pos, Span};
15 use rustc_data_structures::sync::Lrc;
17 // These macros all relate to the file system; they either return
18 // the column/row/filename of the expression, or they include
19 // a given file into the current one.
21 /// line!(): expands to the current line number
22 pub fn expand_line(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
23 -> Box<dyn base::MacResult+'static> {
24 let sp = cx.with_def_site_ctxt(sp);
25 base::check_zero_tts(cx, sp, tts, "line!");
27 let topmost = cx.expansion_cause().unwrap_or(sp);
28 let loc = cx.source_map().lookup_char_pos(topmost.lo());
30 base::MacEager::expr(cx.expr_u32(topmost, loc.line as u32))
33 /* column!(): expands to the current column number */
34 pub fn expand_column(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
35 -> Box<dyn base::MacResult+'static> {
36 let sp = cx.with_def_site_ctxt(sp);
37 base::check_zero_tts(cx, sp, tts, "column!");
39 let topmost = cx.expansion_cause().unwrap_or(sp);
40 let loc = cx.source_map().lookup_char_pos(topmost.lo());
42 base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1))
45 /// file!(): expands to the current filename */
46 /// The source_file (`loc.file`) contains a bunch more information we could spit
48 pub fn expand_file(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
49 -> Box<dyn base::MacResult+'static> {
50 let sp = cx.with_def_site_ctxt(sp);
51 base::check_zero_tts(cx, sp, tts, "file!");
53 let topmost = cx.expansion_cause().unwrap_or(sp);
54 let loc = cx.source_map().lookup_char_pos(topmost.lo());
55 base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name.to_string())))
58 pub fn expand_stringify(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
59 -> Box<dyn base::MacResult+'static> {
60 let sp = cx.with_def_site_ctxt(sp);
61 let s = pprust::tts_to_string(tts);
62 base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s)))
65 pub fn expand_mod(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
66 -> Box<dyn base::MacResult+'static> {
67 let sp = cx.with_def_site_ctxt(sp);
68 base::check_zero_tts(cx, sp, tts, "module_path!");
69 let mod_path = &cx.current_expansion.module.mod_path;
70 let string = mod_path.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("::");
72 base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&string)))
75 /// include! : parse the given file as an expr
76 /// This is generally a bad idea because it's going to behave
78 pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
79 -> Box<dyn base::MacResult+'cx> {
80 let sp = cx.with_def_site_ctxt(sp);
81 let file = match get_single_str_from_tts(cx, sp, tts, "include!") {
83 None => return DummyResult::any(sp),
85 // The file will be added to the code map by the parser
86 let file = match cx.resolve_path(file, sp) {
90 return DummyResult::any(sp);
93 let directory_ownership = DirectoryOwnership::Owned { relative: None };
94 let p = new_sub_parser_from_file(cx.parse_sess(), &file, directory_ownership, None, sp);
96 struct ExpandResult<'a> {
99 impl<'a> base::MacResult for ExpandResult<'a> {
100 fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
101 let r = panictry!(self.p.parse_expr());
102 if self.p.token != token::Eof {
103 self.p.sess.buffer_lint(
104 BufferedEarlyLintId::IncompleteInclude,
107 "include macro expected single expression in source",
113 fn make_items(mut self: Box<ExpandResult<'a>>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
114 let mut ret = SmallVec::new();
115 while self.p.token != token::Eof {
116 match panictry!(self.p.parse_item()) {
117 Some(item) => ret.push(item),
118 None => self.p.sess.span_diagnostic.span_fatal(self.p.token.span,
119 &format!("expected item, found `{}`",
120 self.p.this_token_to_string()))
128 Box::new(ExpandResult { p })
131 // include_str! : read the given file, insert it as a literal string expr
132 pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
133 -> Box<dyn base::MacResult+'static> {
134 let sp = cx.with_def_site_ctxt(sp);
135 let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
137 None => return DummyResult::any(sp)
139 let file = match cx.resolve_path(file, sp) {
143 return DummyResult::any(sp);
146 match cx.source_map().load_binary_file(&file) {
147 Ok(bytes) => match std::str::from_utf8(&bytes) {
149 let interned_src = Symbol::intern(&src);
150 base::MacEager::expr(cx.expr_str(sp, interned_src))
153 cx.span_err(sp, &format!("{} wasn't a utf-8 file", file.display()));
158 cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
164 pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
165 -> Box<dyn base::MacResult+'static> {
166 let sp = cx.with_def_site_ctxt(sp);
167 let file = match get_single_str_from_tts(cx, sp, tts, "include_bytes!") {
169 None => return DummyResult::any(sp)
171 let file = match cx.resolve_path(file, sp) {
175 return DummyResult::any(sp);
178 match cx.source_map().load_binary_file(&file) {
180 base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::new(bytes))))
183 cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));