2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::ptr::P;
|
|
|
|
use rustc_ast::token;
|
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2020-01-11 17:02:46 +01:00
|
|
|
use rustc_ast_pretty::pprust;
|
2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::base::{self, *};
|
2021-02-22 19:06:36 +03:00
|
|
|
use rustc_expand::module::DirOwnership;
|
2021-01-18 16:47:37 -05:00
|
|
|
use rustc_parse::parser::{ForceCollect, Parser};
|
|
|
|
use rustc_parse::{self, new_parser_from_file};
|
2020-01-05 10:47:20 +01:00
|
|
|
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::Symbol;
|
2022-04-25 18:02:43 -07:00
|
|
|
use rustc_span::{self, Pos, Span};
|
2019-02-07 02:33:01 +09:00
|
|
|
|
2018-08-30 11:42:16 +02:00
|
|
|
use smallvec::SmallVec;
|
2020-03-20 16:02:46 +01:00
|
|
|
use std::rc::Rc;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-02-26 10:15:29 -08:00
|
|
|
// These macros all relate to the file system; they either return
|
|
|
|
// the column/row/filename of the expression, or they include
|
|
|
|
// a given file into the current one.
|
2013-02-11 18:13:18 +02:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// line!(): expands to the current line number
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_line(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2012-12-12 17:08:09 -08:00
|
|
|
base::check_zero_tts(cx, sp, tts, "line!");
|
2013-02-11 18:13:18 +02:00
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2018-08-18 12:14:09 +02:00
|
|
|
let loc = cx.source_map().lookup_char_pos(topmost.lo());
|
2013-02-11 18:13:18 +02:00
|
|
|
|
2015-02-27 11:14:42 -08:00
|
|
|
base::MacEager::expr(cx.expr_u32(topmost, loc.line as u32))
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
|
|
|
|
2014-11-18 23:03:58 +11:00
|
|
|
/* column!(): expands to the current column number */
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_column(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2014-11-18 23:03:58 +11:00
|
|
|
base::check_zero_tts(cx, sp, tts, "column!");
|
2013-02-11 18:13:18 +02:00
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2018-08-18 12:14:09 +02:00
|
|
|
let loc = cx.source_map().lookup_char_pos(topmost.lo());
|
2015-02-21 06:56:46 -05:00
|
|
|
|
2017-12-24 02:20:06 +01:00
|
|
|
base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1))
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// file!(): expands to the current filename */
|
2018-08-18 12:13:56 +02:00
|
|
|
/// The source_file (`loc.file`) contains a bunch more information we could spit
|
2014-06-09 13:12:30 -07:00
|
|
|
/// out if we wanted.
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_file(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2012-12-12 17:08:09 -08:00
|
|
|
base::check_zero_tts(cx, sp, tts, "file!");
|
2013-02-11 18:13:18 +02:00
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2018-08-18 12:14:09 +02:00
|
|
|
let loc = cx.source_map().lookup_char_pos(topmost.lo());
|
2021-04-19 23:27:02 +01:00
|
|
|
base::MacEager::expr(
|
|
|
|
cx.expr_str(topmost, Symbol::intern(&loc.file.name.prefer_remapped().to_string_lossy())),
|
|
|
|
)
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
|
|
|
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_stringify(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2020-06-24 17:45:08 +03:00
|
|
|
let s = pprust::tts_to_string(&tts);
|
2016-11-16 10:52:37 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s)))
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
|
|
|
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_mod(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2012-12-12 17:08:09 -08:00
|
|
|
base::check_zero_tts(cx, sp, tts, "module_path!");
|
2016-09-07 23:21:59 +00:00
|
|
|
let mod_path = &cx.current_expansion.module.mod_path;
|
|
|
|
let string = mod_path.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("::");
|
2016-09-01 06:44:54 +00:00
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&string)))
|
2012-05-18 10:02:21 -07:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// include! : parse the given file as an expr
|
|
|
|
/// This is generally a bad idea because it's going to behave
|
|
|
|
/// unhygienically.
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_include<'cx>(
|
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'cx> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(file) = get_single_str_from_tts(cx, sp, tts, "include!") else {
|
|
|
|
return DummyResult::any(sp);
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2013-11-27 20:05:12 -07:00
|
|
|
// The file will be added to the code map by the parser
|
2022-04-25 18:02:43 -07:00
|
|
|
let file = match resolve_path(&cx.sess.parse_sess, file.as_str(), sp) {
|
2019-10-19 13:05:46 -04:00
|
|
|
Ok(f) => f,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
return DummyResult::any(sp);
|
|
|
|
}
|
|
|
|
};
|
2020-03-21 23:10:10 +01:00
|
|
|
let p = new_parser_from_file(cx.parse_sess(), &file, Some(sp));
|
2014-10-20 23:04:16 -07:00
|
|
|
|
2020-03-20 16:02:46 +01:00
|
|
|
// If in the included file we have e.g., `mod bar;`,
|
|
|
|
// then the path of `bar.rs` should be relative to the directory of `file`.
|
|
|
|
// See https://github.com/rust-lang/rust/pull/69838/files#r395217057 for a discussion.
|
|
|
|
// `MacroExpander::fully_expand_fragment` later restores, so "stack discipline" is maintained.
|
2021-02-21 19:15:43 +03:00
|
|
|
let dir_path = file.parent().unwrap_or(&file).to_owned();
|
|
|
|
cx.current_expansion.module = Rc::new(cx.current_expansion.module.with_dir_path(dir_path));
|
2021-02-22 19:06:36 +03:00
|
|
|
cx.current_expansion.dir_ownership = DirOwnership::Owned { relative: None };
|
2020-03-20 16:02:46 +01:00
|
|
|
|
2014-10-20 23:04:16 -07:00
|
|
|
struct ExpandResult<'a> {
|
2019-10-15 22:48:13 +02:00
|
|
|
p: Parser<'a>,
|
2020-06-09 20:59:43 +03:00
|
|
|
node_id: ast::NodeId,
|
2014-10-20 23:04:16 -07:00
|
|
|
}
|
|
|
|
impl<'a> base::MacResult for ExpandResult<'a> {
|
|
|
|
fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
|
2020-03-17 08:59:56 +01:00
|
|
|
let r = base::parse_expr(&mut self.p)?;
|
2019-09-08 10:00:26 -04:00
|
|
|
if self.p.token != token::Eof {
|
|
|
|
self.p.sess.buffer_lint(
|
2019-11-12 12:09:20 -05:00
|
|
|
&INCOMPLETE_INCLUDE,
|
2019-09-08 10:00:26 -04:00
|
|
|
self.p.token.span,
|
2020-06-09 20:59:43 +03:00
|
|
|
self.node_id,
|
2019-09-08 10:00:26 -04:00
|
|
|
"include macro expected single expression in source",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Some(r)
|
2014-10-20 23:04:16 -07:00
|
|
|
}
|
2018-08-30 11:42:16 +02:00
|
|
|
|
|
|
|
fn make_items(mut self: Box<ExpandResult<'a>>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
|
|
|
|
let mut ret = SmallVec::new();
|
2022-03-22 00:36:47 +01:00
|
|
|
loop {
|
2021-01-18 16:47:37 -05:00
|
|
|
match self.p.parse_item(ForceCollect::No) {
|
2020-03-17 08:59:56 +01:00
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(Some(item)) => ret.push(item),
|
|
|
|
Ok(None) => {
|
2022-03-22 00:36:47 +01:00
|
|
|
if self.p.token != token::Eof {
|
|
|
|
let token = pprust::token_to_string(&self.p.token);
|
|
|
|
let msg = format!("expected item, found `{}`", token);
|
|
|
|
self.p.struct_span_err(self.p.token.span, &msg).emit();
|
|
|
|
}
|
|
|
|
|
2020-03-17 08:59:56 +01:00
|
|
|
break;
|
2019-12-07 03:07:35 +01:00
|
|
|
}
|
2014-10-20 23:04:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 18:24:12 -05:00
|
|
|
Box::new(ExpandResult { p, node_id: cx.current_expansion.lint_node_id })
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
2012-05-18 10:00:49 -07:00
|
|
|
|
2013-02-26 10:15:29 -08:00
|
|
|
// include_str! : read the given file, insert it as a literal string expr
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_include_str(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(file) = get_single_str_from_tts(cx, sp, tts, "include_str!") else {
|
|
|
|
return DummyResult::any(sp);
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2022-04-25 18:02:43 -07:00
|
|
|
let file = match resolve_path(&cx.sess.parse_sess, file.as_str(), sp) {
|
2019-10-19 13:05:46 -04:00
|
|
|
Ok(f) => f,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
return DummyResult::any(sp);
|
|
|
|
}
|
|
|
|
};
|
2019-08-13 19:51:32 +03:00
|
|
|
match cx.source_map().load_binary_file(&file) {
|
|
|
|
Ok(bytes) => match std::str::from_utf8(&bytes) {
|
|
|
|
Ok(src) => {
|
|
|
|
let interned_src = Symbol::intern(&src);
|
|
|
|
base::MacEager::expr(cx.expr_str(sp, interned_src))
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
cx.span_err(sp, &format!("{} wasn't a utf-8 file", file.display()));
|
|
|
|
DummyResult::any(sp)
|
|
|
|
}
|
2018-11-16 16:22:06 -05:00
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
|
2019-08-13 20:51:54 +03:00
|
|
|
DummyResult::any(sp)
|
2013-10-13 18:48:47 -07:00
|
|
|
}
|
2012-05-18 10:02:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_include_bytes(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(file) = get_single_str_from_tts(cx, sp, tts, "include_bytes!") else {
|
|
|
|
return DummyResult::any(sp);
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2022-04-25 18:02:43 -07:00
|
|
|
let file = match resolve_path(&cx.sess.parse_sess, file.as_str(), sp) {
|
2019-10-19 13:05:46 -04:00
|
|
|
Ok(f) => f,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
return DummyResult::any(sp);
|
|
|
|
}
|
|
|
|
};
|
2019-08-13 19:51:32 +03:00
|
|
|
match cx.source_map().load_binary_file(&file) {
|
2020-09-16 19:41:22 -06:00
|
|
|
Ok(bytes) => base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(bytes.into()))),
|
2018-11-16 16:22:06 -05:00
|
|
|
Err(e) => {
|
|
|
|
cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
|
2019-08-13 20:51:54 +03:00
|
|
|
DummyResult::any(sp)
|
2013-10-14 08:24:17 -07:00
|
|
|
}
|
2012-05-18 10:03:27 -07:00
|
|
|
}
|
|
|
|
}
|