1 use rustc_expand::base::{self, *};
2 use rustc_expand::panictry;
3 use rustc_parse::{self, new_sub_parser_from_file, parser::Parser, DirectoryOwnership};
4 use rustc_span::symbol::Symbol;
6 use syntax::early_buffered_lints::INCOMPLETE_INCLUDE;
7 use syntax::print::pprust;
10 use syntax::tokenstream::TokenStream;
12 use rustc_span::{self, Pos, Span};
13 use smallvec::SmallVec;
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
26 ) -> Box<dyn base::MacResult + 'static> {
27 let sp = cx.with_def_site_ctxt(sp);
28 base::check_zero_tts(cx, sp, tts, "line!");
30 let topmost = cx.expansion_cause().unwrap_or(sp);
31 let loc = cx.source_map().lookup_char_pos(topmost.lo());
33 base::MacEager::expr(cx.expr_u32(topmost, loc.line as u32))
36 /* column!(): expands to the current column number */
41 ) -> Box<dyn base::MacResult + 'static> {
42 let sp = cx.with_def_site_ctxt(sp);
43 base::check_zero_tts(cx, sp, tts, "column!");
45 let topmost = cx.expansion_cause().unwrap_or(sp);
46 let loc = cx.source_map().lookup_char_pos(topmost.lo());
48 base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1))
51 /// file!(): expands to the current filename */
52 /// The source_file (`loc.file`) contains a bunch more information we could spit
58 ) -> Box<dyn base::MacResult + 'static> {
59 let sp = cx.with_def_site_ctxt(sp);
60 base::check_zero_tts(cx, sp, tts, "file!");
62 let topmost = cx.expansion_cause().unwrap_or(sp);
63 let loc = cx.source_map().lookup_char_pos(topmost.lo());
64 base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name.to_string())))
67 pub fn expand_stringify(
71 ) -> Box<dyn base::MacResult + 'static> {
72 let sp = cx.with_def_site_ctxt(sp);
73 let s = pprust::tts_to_string(tts);
74 base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s)))
81 ) -> Box<dyn base::MacResult + 'static> {
82 let sp = cx.with_def_site_ctxt(sp);
83 base::check_zero_tts(cx, sp, tts, "module_path!");
84 let mod_path = &cx.current_expansion.module.mod_path;
85 let string = mod_path.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("::");
87 base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&string)))
90 /// include! : parse the given file as an expr
91 /// This is generally a bad idea because it's going to behave
93 pub fn expand_include<'cx>(
94 cx: &'cx mut ExtCtxt<'_>,
97 ) -> Box<dyn base::MacResult + 'cx> {
98 let sp = cx.with_def_site_ctxt(sp);
99 let file = match get_single_str_from_tts(cx, sp, tts, "include!") {
101 None => return DummyResult::any(sp),
103 // The file will be added to the code map by the parser
104 let file = match cx.resolve_path(file, sp) {
108 return DummyResult::any(sp);
111 let directory_ownership = DirectoryOwnership::Owned { relative: None };
112 let p = new_sub_parser_from_file(cx.parse_sess(), &file, directory_ownership, None, sp);
114 struct ExpandResult<'a> {
117 impl<'a> base::MacResult for ExpandResult<'a> {
118 fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
119 let r = panictry!(self.p.parse_expr());
120 if self.p.token != token::Eof {
121 self.p.sess.buffer_lint(
125 "include macro expected single expression in source",
131 fn make_items(mut self: Box<ExpandResult<'a>>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
132 let mut ret = SmallVec::new();
133 while self.p.token != token::Eof {
134 match panictry!(self.p.parse_item()) {
135 Some(item) => ret.push(item),
137 let token = pprust::token_to_string(&self.p.token);
143 &format!("expected item, found `{}`", token),
153 Box::new(ExpandResult { p })
156 // include_str! : read the given file, insert it as a literal string expr
157 pub fn expand_include_str(
158 cx: &mut ExtCtxt<'_>,
161 ) -> Box<dyn base::MacResult + 'static> {
162 let sp = cx.with_def_site_ctxt(sp);
163 let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
165 None => return DummyResult::any(sp),
167 let file = match cx.resolve_path(file, sp) {
171 return DummyResult::any(sp);
174 match cx.source_map().load_binary_file(&file) {
175 Ok(bytes) => match std::str::from_utf8(&bytes) {
177 let interned_src = Symbol::intern(&src);
178 base::MacEager::expr(cx.expr_str(sp, interned_src))
181 cx.span_err(sp, &format!("{} wasn't a utf-8 file", file.display()));
186 cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
192 pub fn expand_include_bytes(
193 cx: &mut ExtCtxt<'_>,
196 ) -> Box<dyn base::MacResult + 'static> {
197 let sp = cx.with_def_site_ctxt(sp);
198 let file = match get_single_str_from_tts(cx, sp, tts, "include_bytes!") {
200 None => return DummyResult::any(sp),
202 let file = match cx.resolve_path(file, sp) {
206 return DummyResult::any(sp);
209 match cx.source_map().load_binary_file(&file) {
210 Ok(bytes) => base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::new(bytes)))),
212 cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));