2016-06-06 09:52:48 -05:00
|
|
|
// The compiler code necessary to support the env! extension. Eventually this
|
|
|
|
// should all get sucked into either the compiler syntax extension plugin
|
|
|
|
// interface.
|
|
|
|
//
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
use syntax::ast::{self, GenericArg, Ident};
|
2019-05-11 09:41:37 -05:00
|
|
|
use syntax::symbol::{kw, sym, Symbol};
|
2019-08-31 12:08:06 -05:00
|
|
|
use syntax::tokenstream::TokenStream;
|
2019-12-22 16:42:04 -06:00
|
|
|
use syntax_expand::base::{self, *};
|
|
|
|
use syntax_pos::Span;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2015-01-27 14:20:58 -06:00
|
|
|
use std::env;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
pub fn expand_option_env<'cx>(
|
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
|
|
|
) -> Box<dyn base::MacResult + 'cx> {
|
2014-01-17 08:53:10 -06:00
|
|
|
let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") {
|
2019-08-13 12:51:54 -05:00
|
|
|
None => return DummyResult::any(sp),
|
2016-06-06 09:52:48 -05:00
|
|
|
Some(v) => v,
|
2014-01-17 08:53:10 -06:00
|
|
|
};
|
2013-08-15 01:06:33 -05:00
|
|
|
|
2019-09-14 15:17:11 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2019-10-21 19:04:25 -05:00
|
|
|
let e = match env::var(&var.as_str()) {
|
2016-06-06 09:52:48 -05:00
|
|
|
Err(..) => {
|
2019-09-14 15:16:51 -05:00
|
|
|
let lt = cx.lifetime(sp, Ident::new(kw::StaticLifetime, sp));
|
2019-12-22 16:42:04 -06:00
|
|
|
cx.expr_path(cx.path_all(
|
|
|
|
sp,
|
|
|
|
true,
|
|
|
|
cx.std_path(&[sym::option, sym::Option, sym::None]),
|
|
|
|
vec![GenericArg::Type(cx.ty_rptr(
|
|
|
|
sp,
|
|
|
|
cx.ty_ident(sp, Ident::new(sym::str, sp)),
|
|
|
|
Some(lt),
|
|
|
|
ast::Mutability::Not,
|
|
|
|
))],
|
|
|
|
))
|
2016-06-06 09:52:48 -05:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
Ok(s) => cx.expr_call_global(
|
|
|
|
sp,
|
|
|
|
cx.std_path(&[sym::option, sym::Option, sym::Some]),
|
|
|
|
vec![cx.expr_str(sp, Symbol::intern(&s))],
|
|
|
|
),
|
2013-08-15 01:06:33 -05:00
|
|
|
};
|
2015-02-27 13:14:42 -06:00
|
|
|
MacEager::expr(e)
|
2013-08-15 01:06:33 -05:00
|
|
|
}
|
2012-05-14 17:32:32 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
pub fn expand_env<'cx>(
|
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
|
|
|
) -> Box<dyn base::MacResult + 'cx> {
|
2014-09-13 11:06:01 -05:00
|
|
|
let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
|
2015-03-24 18:53:34 -05:00
|
|
|
Some(ref exprs) if exprs.is_empty() => {
|
2014-01-17 08:53:10 -06:00
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
2019-08-13 12:51:54 -05:00
|
|
|
return DummyResult::any(sp);
|
2014-01-17 08:53:10 -06:00
|
|
|
}
|
2019-08-13 12:51:54 -05:00
|
|
|
None => return DummyResult::any(sp),
|
2016-06-06 09:52:48 -05:00
|
|
|
Some(exprs) => exprs.into_iter(),
|
2014-01-17 08:53:10 -06:00
|
|
|
};
|
2013-08-06 23:50:23 -05:00
|
|
|
|
2016-06-06 09:52:48 -05:00
|
|
|
let var = match expr_to_string(cx, exprs.next().unwrap(), "expected string literal") {
|
2019-08-13 12:51:54 -05:00
|
|
|
None => return DummyResult::any(sp),
|
2016-06-06 09:52:48 -05:00
|
|
|
Some((v, _style)) => v,
|
2014-01-17 08:53:10 -06:00
|
|
|
};
|
2014-09-13 11:06:01 -05:00
|
|
|
let msg = match exprs.next() {
|
2016-11-16 04:52:37 -06:00
|
|
|
None => Symbol::intern(&format!("environment variable `{}` not defined", var)),
|
2019-12-22 16:42:04 -06:00
|
|
|
Some(second) => match expr_to_string(cx, second, "expected string literal") {
|
|
|
|
None => return DummyResult::any(sp),
|
|
|
|
Some((s, _style)) => s,
|
|
|
|
},
|
2014-09-13 11:06:01 -05:00
|
|
|
};
|
|
|
|
|
2018-07-27 06:20:13 -05:00
|
|
|
if exprs.next().is_some() {
|
2016-07-03 16:38:37 -05:00
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
2019-08-13 12:51:54 -05:00
|
|
|
return DummyResult::any(sp);
|
2014-09-13 11:06:01 -05:00
|
|
|
}
|
2011-05-06 18:30:39 -05:00
|
|
|
|
2016-11-16 04:52:37 -06:00
|
|
|
let e = match env::var(&*var.as_str()) {
|
2015-02-04 18:03:12 -06:00
|
|
|
Err(_) => {
|
2016-11-16 04:52:37 -06:00
|
|
|
cx.span_err(sp, &msg.as_str());
|
2019-08-13 12:51:54 -05:00
|
|
|
return DummyResult::any(sp);
|
2014-01-17 08:53:10 -06:00
|
|
|
}
|
2016-11-16 04:52:37 -06:00
|
|
|
Ok(s) => cx.expr_str(sp, Symbol::intern(&s)),
|
2012-12-12 19:08:09 -06:00
|
|
|
};
|
2015-02-27 13:14:42 -06:00
|
|
|
MacEager::expr(e)
|
2011-05-04 21:05:16 -05:00
|
|
|
}
|