2014-01-04 20:16:57 +00:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
2012-12-04 00:48:01 +00:00
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-02-25 19:11:21 +00:00
|
|
|
use ast;
|
2017-12-14 07:09:19 +00:00
|
|
|
use syntax_pos::{self, Pos, Span, FileName};
|
2012-12-23 22:41:37 +00:00
|
|
|
use ext::base::*;
|
|
|
|
use ext::base;
|
2013-05-17 14:19:28 +00:00
|
|
|
use ext::build::AstBuilder;
|
2016-11-05 04:16:26 +00:00
|
|
|
use parse::{token, DirectoryOwnership};
|
2014-10-21 06:04:16 +00:00
|
|
|
use parse;
|
2012-12-23 22:41:37 +00:00
|
|
|
use print::pprust;
|
2014-10-21 06:04:16 +00:00
|
|
|
use ptr::P;
|
2018-08-05 10:04:56 +00:00
|
|
|
use OneVector;
|
2016-11-16 10:52:37 +00:00
|
|
|
use symbol::Symbol;
|
2016-06-20 15:49:33 +00:00
|
|
|
use tokenstream;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
2017-12-14 07:09:19 +00:00
|
|
|
use std::path::PathBuf;
|
2018-02-27 16:11:14 +00:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2013-02-26 18:15:29 +00: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 16:13:18 +00:00
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// line!(): expands to the current line number
|
2016-06-20 15:49:33 +00:00
|
|
|
pub fn expand_line(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2012-12-13 01:08:09 +00:00
|
|
|
base::check_zero_tts(cx, sp, tts, "line!");
|
2013-02-11 16:13:18 +00:00
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2017-07-31 20:04:34 +00:00
|
|
|
let loc = cx.codemap().lookup_char_pos(topmost.lo());
|
2013-02-11 16:13:18 +00:00
|
|
|
|
2015-02-27 19:14:42 +00:00
|
|
|
base::MacEager::expr(cx.expr_u32(topmost, loc.line as u32))
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 12:03:58 +00:00
|
|
|
/* column!(): expands to the current column number */
|
2016-06-20 15:49:33 +00:00
|
|
|
pub fn expand_column(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2014-11-18 12:03:58 +00:00
|
|
|
base::check_zero_tts(cx, sp, tts, "column!");
|
2013-02-11 16:13:18 +00:00
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2017-07-31 20:04:34 +00:00
|
|
|
let loc = cx.codemap().lookup_char_pos(topmost.lo());
|
2015-02-21 11:56:46 +00:00
|
|
|
|
2017-12-24 01:20:06 +00:00
|
|
|
base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1))
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 23:42:36 +00:00
|
|
|
/* __rust_unstable_column!(): expands to the current column number */
|
|
|
|
pub fn expand_column_gated(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2017-08-09 23:42:36 +00:00
|
|
|
if sp.allows_unstable() {
|
|
|
|
expand_column(cx, sp, tts)
|
|
|
|
} else {
|
|
|
|
cx.span_fatal(sp, "the __rust_unstable_column macro is unstable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// file!(): expands to the current filename */
|
|
|
|
/// The filemap (`loc.file`) contains a bunch more information we could spit
|
|
|
|
/// out if we wanted.
|
2016-06-20 15:49:33 +00:00
|
|
|
pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2012-12-13 01:08:09 +00:00
|
|
|
base::check_zero_tts(cx, sp, tts, "file!");
|
2013-02-11 16:13:18 +00:00
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2017-07-31 20:04:34 +00:00
|
|
|
let loc = cx.codemap().lookup_char_pos(topmost.lo());
|
2017-12-14 07:09:19 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name.to_string())))
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 15:49:33 +00:00
|
|
|
pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2014-06-21 10:39:03 +00: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-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 15:49:33 +00:00
|
|
|
pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2012-12-13 01:08:09 +00: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 17:02:21 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// include! : parse the given file as an expr
|
|
|
|
/// This is generally a bad idea because it's going to behave
|
|
|
|
/// unhygienically.
|
2016-06-20 15:49:33 +00:00
|
|
|
pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'cx> {
|
2014-01-17 14:53:10 +00:00
|
|
|
let file = match get_single_str_from_tts(cx, sp, tts, "include!") {
|
|
|
|
Some(f) => f,
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return DummyResult::expr(sp),
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2013-11-28 03:05:12 +00:00
|
|
|
// The file will be added to the code map by the parser
|
2017-12-14 07:09:19 +00:00
|
|
|
let path = res_rel_file(cx, sp, file);
|
2017-11-28 02:14:24 +00:00
|
|
|
let directory_ownership = DirectoryOwnership::Owned { relative: None };
|
2016-11-05 04:16:26 +00:00
|
|
|
let p = parse::new_sub_parser_from_file(cx.parse_sess(), &path, directory_ownership, None, sp);
|
2014-10-21 06:04:16 +00:00
|
|
|
|
|
|
|
struct ExpandResult<'a> {
|
|
|
|
p: parse::parser::Parser<'a>,
|
|
|
|
}
|
|
|
|
impl<'a> base::MacResult for ExpandResult<'a> {
|
|
|
|
fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
|
2015-11-11 00:08:26 +00:00
|
|
|
Some(panictry!(self.p.parse_expr()))
|
2014-10-21 06:04:16 +00:00
|
|
|
}
|
|
|
|
fn make_items(mut self: Box<ExpandResult<'a>>)
|
2018-08-05 10:04:56 +00:00
|
|
|
-> Option<OneVector<P<ast::Item>>> {
|
|
|
|
let mut ret = OneVector::new();
|
2015-02-07 16:23:33 +00:00
|
|
|
while self.p.token != token::Eof {
|
2015-11-11 00:08:26 +00:00
|
|
|
match panictry!(self.p.parse_item()) {
|
2014-10-21 06:04:16 +00:00
|
|
|
Some(item) => ret.push(item),
|
2018-01-21 11:47:58 +00:00
|
|
|
None => self.p.diagnostic().span_fatal(self.p.span,
|
2015-12-20 21:00:43 +00:00
|
|
|
&format!("expected item, found `{}`",
|
2018-01-21 11:47:58 +00:00
|
|
|
self.p.this_token_to_string()))
|
|
|
|
.raise()
|
2014-10-21 06:04:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 03:56:16 +00:00
|
|
|
Box::new(ExpandResult { p: p })
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
2012-05-18 17:00:49 +00:00
|
|
|
|
2013-02-26 18:15:29 +00:00
|
|
|
// include_str! : read the given file, insert it as a literal string expr
|
2016-06-20 15:49:33 +00:00
|
|
|
pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2014-01-17 14:53:10 +00:00
|
|
|
let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
|
|
|
|
Some(f) => f,
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return DummyResult::expr(sp)
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2017-12-14 07:09:19 +00:00
|
|
|
let file = res_rel_file(cx, sp, file);
|
2015-02-27 05:00:43 +00:00
|
|
|
let mut bytes = Vec::new();
|
|
|
|
match File::open(&file).and_then(|mut f| f.read_to_end(&mut bytes)) {
|
|
|
|
Ok(..) => {}
|
2013-10-26 00:04:37 +00:00
|
|
|
Err(e) => {
|
2014-05-16 17:45:16 +00:00
|
|
|
cx.span_err(sp,
|
2015-01-25 06:43:11 +00:00
|
|
|
&format!("couldn't read {}: {}",
|
2014-05-16 17:45:16 +00:00
|
|
|
file.display(),
|
2015-02-18 23:58:07 +00:00
|
|
|
e));
|
2014-04-15 12:00:14 +00:00
|
|
|
return DummyResult::expr(sp);
|
2013-10-14 01:48:47 +00:00
|
|
|
}
|
2013-10-26 00:04:37 +00:00
|
|
|
};
|
2014-06-30 14:41:30 +00:00
|
|
|
match String::from_utf8(bytes) {
|
|
|
|
Ok(src) => {
|
2018-05-23 14:19:20 +00:00
|
|
|
let interned_src = Symbol::intern(&src);
|
|
|
|
|
2013-11-28 03:05:12 +00:00
|
|
|
// Add this input file to the code map to make it available as
|
|
|
|
// dependency information
|
2018-05-23 14:19:20 +00:00
|
|
|
cx.codemap().new_filemap(file.into(), src);
|
2014-01-08 08:59:11 +00:00
|
|
|
|
2018-05-23 14:19:20 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(sp, interned_src))
|
2013-11-28 03:05:12 +00:00
|
|
|
}
|
2014-06-30 14:41:30 +00:00
|
|
|
Err(_) => {
|
2014-05-16 17:45:16 +00:00
|
|
|
cx.span_err(sp,
|
2015-01-25 06:43:11 +00:00
|
|
|
&format!("{} wasn't a utf-8 file",
|
2015-02-18 23:58:07 +00:00
|
|
|
file.display()));
|
2017-05-12 18:05:39 +00:00
|
|
|
DummyResult::expr(sp)
|
2013-10-14 01:48:47 +00:00
|
|
|
}
|
2012-05-18 17:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 15:49:33 +00:00
|
|
|
pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2014-12-21 21:57:09 +00:00
|
|
|
let file = match get_single_str_from_tts(cx, sp, tts, "include_bytes!") {
|
2014-01-17 14:53:10 +00:00
|
|
|
Some(f) => f,
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return DummyResult::expr(sp)
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2017-12-14 07:09:19 +00:00
|
|
|
let file = res_rel_file(cx, sp, file);
|
2015-02-27 05:00:43 +00:00
|
|
|
let mut bytes = Vec::new();
|
|
|
|
match File::open(&file).and_then(|mut f| f.read_to_end(&mut bytes)) {
|
2013-10-26 00:04:37 +00:00
|
|
|
Err(e) => {
|
2014-05-16 17:45:16 +00:00
|
|
|
cx.span_err(sp,
|
2015-02-18 23:58:07 +00:00
|
|
|
&format!("couldn't read {}: {}", file.display(), e));
|
2017-05-12 18:05:39 +00:00
|
|
|
DummyResult::expr(sp)
|
2013-10-14 15:24:17 +00:00
|
|
|
}
|
2015-02-27 05:00:43 +00:00
|
|
|
Ok(..) => {
|
2015-04-14 17:53:23 +00:00
|
|
|
// Add this input file to the code map to make it available as
|
|
|
|
// dependency information, but don't enter it's contents
|
2018-05-23 14:19:20 +00:00
|
|
|
cx.codemap().new_filemap(file.into(), "".to_string());
|
2015-04-14 17:53:23 +00:00
|
|
|
|
2018-02-27 16:11:14 +00:00
|
|
|
base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::new(bytes))))
|
2013-10-14 15:24:17 +00:00
|
|
|
}
|
2012-05-18 17:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 18:15:29 +00:00
|
|
|
// resolve a file-system path to an absolute file-system path (if it
|
|
|
|
// isn't already)
|
2017-12-14 07:09:19 +00:00
|
|
|
fn res_rel_file(cx: &mut ExtCtxt, sp: syntax_pos::Span, arg: String) -> PathBuf {
|
|
|
|
let arg = PathBuf::from(arg);
|
2017-08-01 02:30:46 +00:00
|
|
|
// Relative paths are resolved relative to the file in which they are found
|
|
|
|
// after macro expansion (that is, they are unhygienic).
|
2013-09-27 00:21:59 +00:00
|
|
|
if !arg.is_absolute() {
|
2017-03-17 04:04:41 +00:00
|
|
|
let callsite = sp.source_callsite();
|
2017-12-14 07:09:19 +00:00
|
|
|
let mut path = match cx.codemap().span_to_unmapped_path(callsite) {
|
|
|
|
FileName::Real(path) => path,
|
|
|
|
other => panic!("cannot resolve relative path in non-file source `{}`", other),
|
|
|
|
};
|
2017-08-01 02:30:46 +00:00
|
|
|
path.pop();
|
|
|
|
path.push(arg);
|
|
|
|
path
|
2012-05-18 17:02:21 +00:00
|
|
|
} else {
|
2017-12-14 07:09:19 +00:00
|
|
|
arg
|
2012-05-18 17:02:21 +00:00
|
|
|
}
|
2012-05-18 17:00:49 +00:00
|
|
|
}
|