2016-06-06 20:22:48 +05:30
|
|
|
// 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 17:41:37 -05:00
|
|
|
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::{self as ast, GenericArg};
|
2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::base::{self, *};
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
use std::env;
|
2013-05-24 19:35:29 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn expand_option_env<'cx>(
|
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
|
|
|
) -> Box<dyn base::MacResult + 'cx> {
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(var) = get_single_str_from_tts(cx, sp, tts, "option_env!") else {
|
|
|
|
return DummyResult::any(sp);
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2013-08-15 02:06:33 -04:00
|
|
|
|
2019-09-14 21:17:11 +01:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2020-05-03 20:47:51 +03:00
|
|
|
let value = env::var(&var.as_str()).ok().as_deref().map(Symbol::intern);
|
2020-07-30 11:27:50 +10:00
|
|
|
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((Symbol::intern(&var), value));
|
2020-05-03 20:47:51 +03:00
|
|
|
let e = match value {
|
|
|
|
None => {
|
2019-09-14 21:16:51 +01:00
|
|
|
let lt = cx.lifetime(sp, Ident::new(kw::StaticLifetime, sp));
|
2019-12-22 17:42:04 -05: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 20:22:48 +05:30
|
|
|
}
|
2020-05-03 20:47:51 +03:00
|
|
|
Some(value) => cx.expr_call_global(
|
2019-12-22 17:42:04 -05:00
|
|
|
sp,
|
|
|
|
cx.std_path(&[sym::option, sym::Option, sym::Some]),
|
2020-05-03 20:47:51 +03:00
|
|
|
vec![cx.expr_str(sp, value)],
|
2019-12-22 17:42:04 -05:00
|
|
|
),
|
2013-08-15 02:06:33 -04:00
|
|
|
};
|
2015-02-27 11:14:42 -08:00
|
|
|
MacEager::expr(e)
|
2013-08-15 02:06:33 -04:00
|
|
|
}
|
2012-05-14 15:32:32 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn expand_env<'cx>(
|
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
|
|
|
) -> Box<dyn base::MacResult + 'cx> {
|
2014-09-13 19:06:01 +03:00
|
|
|
let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
|
2015-03-24 16:53:34 -07:00
|
|
|
Some(ref exprs) if exprs.is_empty() => {
|
2014-01-18 01:53:10 +11:00
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
2019-08-13 20:51:54 +03:00
|
|
|
return DummyResult::any(sp);
|
2014-01-18 01:53:10 +11:00
|
|
|
}
|
2019-08-13 20:51:54 +03:00
|
|
|
None => return DummyResult::any(sp),
|
2016-06-06 20:22:48 +05:30
|
|
|
Some(exprs) => exprs.into_iter(),
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2013-08-07 00:50:23 -04:00
|
|
|
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some((var, _style)) = expr_to_string(cx, exprs.next().unwrap(), "expected string literal") else {
|
|
|
|
return DummyResult::any(sp);
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2014-09-13 19:06:01 +03:00
|
|
|
let msg = match exprs.next() {
|
2016-11-16 10:52:37 +00:00
|
|
|
None => Symbol::intern(&format!("environment variable `{}` not defined", var)),
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(second) => match expr_to_string(cx, second, "expected string literal") {
|
|
|
|
None => return DummyResult::any(sp),
|
|
|
|
Some((s, _style)) => s,
|
|
|
|
},
|
2014-09-13 19:06:01 +03:00
|
|
|
};
|
|
|
|
|
2018-07-27 13:20:13 +02:00
|
|
|
if exprs.next().is_some() {
|
2016-07-03 14:38:37 -07:00
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
2019-08-13 20:51:54 +03:00
|
|
|
return DummyResult::any(sp);
|
2014-09-13 19:06:01 +03:00
|
|
|
}
|
2011-05-06 16:30:39 -07:00
|
|
|
|
2020-05-26 19:45:30 -04:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2021-12-15 14:39:23 +11:00
|
|
|
let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern);
|
2020-07-30 11:27:50 +10:00
|
|
|
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
|
2020-05-03 20:47:51 +03:00
|
|
|
let e = match value {
|
|
|
|
None => {
|
2021-12-15 14:39:23 +11:00
|
|
|
cx.span_err(sp, msg.as_str());
|
2019-08-13 20:51:54 +03:00
|
|
|
return DummyResult::any(sp);
|
2014-01-18 01:53:10 +11:00
|
|
|
}
|
2020-05-03 20:47:51 +03:00
|
|
|
Some(value) => cx.expr_str(sp, value),
|
2012-12-12 17:08:09 -08:00
|
|
|
};
|
2015-02-27 11:14:42 -08:00
|
|
|
MacEager::expr(e)
|
2011-05-04 19:05:16 -07:00
|
|
|
}
|