1 use syntax_expand::panictry;
2 use syntax_expand::base::{self, *};
4 use syntax::parse::{self, token, DirectoryOwnership};
5 use syntax::print::pprust;
7 use syntax::symbol::Symbol;
8 use syntax::tokenstream::TokenStream;
9 use syntax::early_buffered_lints::BufferedEarlyLintId;
11 use smallvec::SmallVec;
12 use syntax_pos::{self, Pos, Span};
14 use rustc_data_structures::sync::Lrc;
16 // These macros all relate to the file system; they either return
17 // the column/row/filename of the expression, or they include
18 // a given file into the current one.
20 /// line!(): expands to the current line number
21 pub fn expand_line(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
22 -> Box<dyn base::MacResult+'static> {
23 base::check_zero_tts(cx, sp, tts, "line!");
25 let topmost = cx.expansion_cause().unwrap_or(sp);
26 let loc = cx.source_map().lookup_char_pos(topmost.lo());
28 base::MacEager::expr(cx.expr_u32(topmost, loc.line as u32))
31 /* column!(): expands to the current column number */
32 pub fn expand_column(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
33 -> Box<dyn base::MacResult+'static> {
34 base::check_zero_tts(cx, sp, tts, "column!");
36 let topmost = cx.expansion_cause().unwrap_or(sp);
37 let loc = cx.source_map().lookup_char_pos(topmost.lo());
39 base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1))
42 /// file!(): expands to the current filename */
43 /// The source_file (`loc.file`) contains a bunch more information we could spit
45 pub fn expand_file(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
46 -> Box<dyn base::MacResult+'static> {
47 base::check_zero_tts(cx, sp, tts, "file!");
49 let topmost = cx.expansion_cause().unwrap_or(sp);
50 let loc = cx.source_map().lookup_char_pos(topmost.lo());
51 base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name.to_string())))
54 pub fn expand_stringify(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
55 -> Box<dyn base::MacResult+'static> {
56 let s = pprust::tts_to_string(tts);
57 base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s)))
60 pub fn expand_mod(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
61 -> Box<dyn base::MacResult+'static> {
62 base::check_zero_tts(cx, sp, tts, "module_path!");
63 let mod_path = &cx.current_expansion.module.mod_path;
64 let string = mod_path.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("::");
66 base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&string)))
69 /// include! : parse the given file as an expr
70 /// This is generally a bad idea because it's going to behave
72 pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
73 -> Box<dyn base::MacResult+'cx> {
74 let file = match get_single_str_from_tts(cx, sp, tts, "include!") {
76 None => return DummyResult::any(sp),
78 // The file will be added to the code map by the parser
79 let file = cx.resolve_path(file, sp);
80 let directory_ownership = DirectoryOwnership::Owned { relative: None };
81 let p = parse::new_sub_parser_from_file(cx.parse_sess(), &file, directory_ownership, None, sp);
83 struct ExpandResult<'a> {
84 p: parse::parser::Parser<'a>,
86 impl<'a> base::MacResult for ExpandResult<'a> {
87 fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
88 let r = panictry!(self.p.parse_expr());
89 if self.p.token != token::Eof {
90 self.p.sess.buffer_lint(
91 BufferedEarlyLintId::IncompleteInclude,
94 "include macro expected single expression in source",
100 fn make_items(mut self: Box<ExpandResult<'a>>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
101 let mut ret = SmallVec::new();
102 while self.p.token != token::Eof {
103 match panictry!(self.p.parse_item()) {
104 Some(item) => ret.push(item),
105 None => self.p.sess.span_diagnostic.span_fatal(self.p.token.span,
106 &format!("expected item, found `{}`",
107 self.p.this_token_to_string()))
115 Box::new(ExpandResult { p })
118 // include_str! : read the given file, insert it as a literal string expr
119 pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
120 -> Box<dyn base::MacResult+'static> {
121 let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
123 None => return DummyResult::any(sp)
125 let file = cx.resolve_path(file, sp);
126 match cx.source_map().load_binary_file(&file) {
127 Ok(bytes) => match std::str::from_utf8(&bytes) {
129 let interned_src = Symbol::intern(&src);
130 base::MacEager::expr(cx.expr_str(sp, interned_src))
133 cx.span_err(sp, &format!("{} wasn't a utf-8 file", file.display()));
138 cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
144 pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
145 -> Box<dyn base::MacResult+'static> {
146 let file = match get_single_str_from_tts(cx, sp, tts, "include_bytes!") {
148 None => return DummyResult::any(sp)
150 let file = cx.resolve_path(file, sp);
151 match cx.source_map().load_binary_file(&file) {
153 base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::new(bytes))))
156 cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));