]> git.lizzy.rs Git - rust.git/blob - src/librustc_builtin_macros/env.rs
rustc: Allow cdylibs to link against dylibs
[rust.git] / src / librustc_builtin_macros / env.rs
1 // The compiler code necessary to support the env! extension.  Eventually this
2 // should all get sucked into either the compiler syntax extension plugin
3 // interface.
4 //
5
6 use rustc_expand::base::{self, *};
7 use rustc_span::symbol::{kw, sym, Symbol};
8 use rustc_span::Span;
9 use syntax::ast::{self, GenericArg, Ident};
10 use syntax::tokenstream::TokenStream;
11
12 use std::env;
13
14 pub fn expand_option_env<'cx>(
15     cx: &'cx mut ExtCtxt<'_>,
16     sp: Span,
17     tts: TokenStream,
18 ) -> Box<dyn base::MacResult + 'cx> {
19     let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") {
20         None => return DummyResult::any(sp),
21         Some(v) => v,
22     };
23
24     let sp = cx.with_def_site_ctxt(sp);
25     let e = match env::var(&var.as_str()) {
26         Err(..) => {
27             let lt = cx.lifetime(sp, Ident::new(kw::StaticLifetime, sp));
28             cx.expr_path(cx.path_all(
29                 sp,
30                 true,
31                 cx.std_path(&[sym::option, sym::Option, sym::None]),
32                 vec![GenericArg::Type(cx.ty_rptr(
33                     sp,
34                     cx.ty_ident(sp, Ident::new(sym::str, sp)),
35                     Some(lt),
36                     ast::Mutability::Not,
37                 ))],
38             ))
39         }
40         Ok(s) => cx.expr_call_global(
41             sp,
42             cx.std_path(&[sym::option, sym::Option, sym::Some]),
43             vec![cx.expr_str(sp, Symbol::intern(&s))],
44         ),
45     };
46     MacEager::expr(e)
47 }
48
49 pub fn expand_env<'cx>(
50     cx: &'cx mut ExtCtxt<'_>,
51     sp: Span,
52     tts: TokenStream,
53 ) -> Box<dyn base::MacResult + 'cx> {
54     let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
55         Some(ref exprs) if exprs.is_empty() => {
56             cx.span_err(sp, "env! takes 1 or 2 arguments");
57             return DummyResult::any(sp);
58         }
59         None => return DummyResult::any(sp),
60         Some(exprs) => exprs.into_iter(),
61     };
62
63     let var = match expr_to_string(cx, exprs.next().unwrap(), "expected string literal") {
64         None => return DummyResult::any(sp),
65         Some((v, _style)) => v,
66     };
67     let msg = match exprs.next() {
68         None => Symbol::intern(&format!("environment variable `{}` not defined", var)),
69         Some(second) => match expr_to_string(cx, second, "expected string literal") {
70             None => return DummyResult::any(sp),
71             Some((s, _style)) => s,
72         },
73     };
74
75     if exprs.next().is_some() {
76         cx.span_err(sp, "env! takes 1 or 2 arguments");
77         return DummyResult::any(sp);
78     }
79
80     let e = match env::var(&*var.as_str()) {
81         Err(_) => {
82             cx.span_err(sp, &msg.as_str());
83             return DummyResult::any(sp);
84         }
85         Ok(s) => cx.expr_str(sp, Symbol::intern(&s)),
86     };
87     MacEager::expr(e)
88 }