snix_eval_builtin_macros/lib.rs
1extern crate proc_macro;
2
3use proc_macro::TokenStream;
4use proc_macro2::Span;
5use quote::{ToTokens, quote, quote_spanned};
6use syn::parse::Parse;
7use syn::spanned::Spanned;
8use syn::{
9 Attribute, FnArg, Ident, Item, ItemMod, LitStr, Meta, Pat, PatIdent, PatType, Token, Type,
10 parse_macro_input, parse_quote, parse_quote_spanned, parse2,
11};
12
13/// Description of a single argument passed to a builtin
14struct BuiltinArgument {
15 /// The name of the argument, to be used in docstrings and error messages
16 name: Ident,
17
18 /// Type of the argument.
19 ty: Box<Type>,
20
21 /// Whether the argument should be forced before the underlying builtin
22 /// function is called.
23 strict: bool,
24
25 /// Propagate catchable values as values to the function, rather than short-circuit returning
26 /// them if encountered
27 catch: bool,
28
29 /// Span at which the argument was defined.
30 span: Span,
31}
32
33fn extract_docstring(attrs: &[Attribute]) -> Option<String> {
34 // Rust docstrings are transparently written pre-macro expansion into an attribute that looks
35 // like:
36 //
37 // #[doc = "docstring here"]
38 //
39 // Multi-line docstrings yield multiple attributes in order, which we assemble into a single
40 // string below.
41
42 #[allow(dead_code)]
43 #[derive(Debug)]
44 struct Docstring {
45 eq: Token![=],
46 doc: LitStr,
47 }
48
49 impl Parse for Docstring {
50 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
51 Ok(Self {
52 eq: input.parse()?,
53 doc: input.parse()?,
54 })
55 }
56 }
57
58 attrs
59 .iter()
60 .filter(|attr| attr.path.get_ident().into_iter().any(|id| id == "doc"))
61 .filter_map(|attr| parse2::<Docstring>(attr.tokens.clone()).ok())
62 .map(|docstring| docstring.doc.value())
63 .reduce(|mut fst, snd| {
64 if snd.is_empty() {
65 // An empty string represents a spacing newline that was added in the
66 // original doc comment.
67 fst.push_str("\n\n");
68 } else {
69 fst.push_str(&snd);
70 }
71
72 fst
73 })
74}
75
76/// Parse arguments to the `builtins` macro itself, such as `#[builtins(state = Rc<State>)]`.
77fn parse_module_args(args: TokenStream) -> Option<Type> {
78 if args.is_empty() {
79 return None;
80 }
81
82 let meta: Meta = syn::parse(args).expect("could not parse arguments to `builtins`-attribute");
83 let name_value = match meta {
84 Meta::NameValue(nv) => nv,
85 _ => panic!("arguments to `builtins`-attribute must be of the form `name = value`"),
86 };
87
88 if *name_value.path.get_ident().unwrap() != "state" {
89 return None;
90 }
91
92 if let syn::Lit::Str(type_name) = name_value.lit {
93 let state_type: Type =
94 syn::parse_str(&type_name.value()).expect("failed to parse builtins state type");
95 return Some(state_type);
96 }
97
98 panic!("state attribute must be a quoted Rust type");
99}
100
101/// Mark the annotated module as a module for defining Nix builtins.
102///
103/// An optional type definition may be specified as an argument (e.g. `#[builtins(Rc<State>)]`),
104/// which will add a parameter to the `builtins` function of that type which is passed to each
105/// builtin upon instantiation. Using this, builtins that close over some external state can be
106/// written.
107///
108/// The type of each function is rewritten to receive a `Vec<Value>`, containing each `Value`
109/// argument that the function receives. The body of functions is accordingly rewritten to "unwrap"
110/// values from this vector and bind them to the correct names, so unless a static error occurs this
111/// transformation is mostly invisible to users of the macro.
112///
113/// A function `fn builtins() -> Vec<Builtin>` will be defined within the annotated module,
114/// returning a list of `snix_eval::Builtin` for each function annotated with the `#[builtin]`
115/// attribute within the module. If a `state` type is specified, the `builtins` function will take a
116/// value of that type.
117///
118/// Each invocation of the `#[builtin]` annotation within the module should be passed a string
119/// literal for the name of the builtin.
120///
121/// # Examples
122/// ```ignore
123/// # use snix_eval;
124/// # use snix_eval_builtin_macros::builtins;
125///
126/// #[builtins]
127/// mod builtins {
128/// use snix_eval::{GenCo, ErrorKind, Value};
129///
130/// #[builtin("identity")]
131/// pub async fn builtin_identity(co: GenCo, x: Value) -> Result<Value, ErrorKind> {
132/// Ok(x)
133/// }
134///
135/// // Builtins can request their argument not be forced before being called by annotating the
136/// // argument with the `#[lazy]` attribute
137///
138/// #[builtin("tryEval")]
139/// pub async fn builtin_try_eval(co: GenCo, #[lazy] x: Value) -> Result<Value, ErrorKind> {
140/// todo!()
141/// }
142/// }
143/// ```
144#[proc_macro_attribute]
145pub fn builtins(args: TokenStream, item: TokenStream) -> TokenStream {
146 let mut module = parse_macro_input!(item as ItemMod);
147
148 // parse the optional state type, which users might want to pass to builtins
149 let state_type = parse_module_args(args);
150
151 let (_, items) = match &mut module.content {
152 Some(content) => content,
153 None => {
154 return (quote_spanned!(module.span() =>
155 compile_error!("Builtin modules must be defined in-line")
156 ))
157 .into();
158 }
159 };
160
161 let mut builtins = vec![];
162 for item in items.iter_mut() {
163 if let Item::Fn(f) = item
164 && let Some(builtin_attr_pos) = f
165 .attrs
166 .iter()
167 .position(|attr| attr.path.get_ident().iter().any(|id| *id == "builtin"))
168 {
169 let builtin_attr = f.attrs.remove(builtin_attr_pos);
170 let name: LitStr = match builtin_attr.parse_args() {
171 Ok(args) => args,
172 Err(err) => return err.into_compile_error().into(),
173 };
174
175 if f.sig.inputs.len() <= 1 {
176 return (quote_spanned!(
177 f.sig.inputs.span() =>
178 compile_error!("Builtin functions must take at least two arguments")
179 ))
180 .into();
181 }
182
183 // Inspect the first argument to determine if this function is
184 // taking the state parameter.
185 // TODO(tazjin): add a test in //snix/eval that covers this
186 let mut captures_state = false;
187 if let FnArg::Typed(PatType { pat, .. }) = &f.sig.inputs[0]
188 && let Pat::Ident(PatIdent { ident, .. }) = pat.as_ref()
189 && *ident == "state"
190 {
191 if state_type.is_none() {
192 panic!("builtin captures a `state` argument, but no state type was defined");
193 }
194
195 captures_state = true;
196 }
197
198 let mut rewritten_args = std::mem::take(&mut f.sig.inputs)
199 .into_iter()
200 .collect::<Vec<_>>();
201
202 // Split out the value arguments from the static arguments.
203 let split_idx = if captures_state { 2 } else { 1 };
204 let value_args = rewritten_args.split_off(split_idx);
205
206 let builtin_arguments = value_args
207 .into_iter()
208 .map(|arg| {
209 let span = arg.span();
210 let mut strict = true;
211 let mut catch = false;
212 let (name, ty) = match arg {
213 FnArg::Receiver(_) => {
214 return Err(quote_spanned!(span => {
215 compile_error!("unexpected receiver argument in builtin")
216 }))
217 }
218 FnArg::Typed(PatType {
219 mut attrs, pat, ty, ..
220 }) => {
221 attrs.retain(|attr| {
222 attr.path.get_ident().into_iter().any(|id| {
223 if id == "lazy" {
224 strict = false;
225 false
226 } else if id == "catch" {
227 catch = true;
228 false
229 } else {
230 true
231 }
232 })
233 });
234 match pat.as_ref() {
235 Pat::Ident(PatIdent { ident, .. }) => {
236 (ident.clone(), ty.clone())
237 }
238 _ => panic!("ignored value parameters must be named, e.g. `_x` and not just `_`"),
239 }
240 }
241 };
242
243 if catch && !strict {
244 return Err(quote_spanned!(span => {
245 compile_error!("Cannot mix both lazy and catch on the same argument")
246 }));
247 }
248
249 Ok(BuiltinArgument {
250 strict,
251 catch,
252 span,
253 name,
254 ty,
255 })
256 })
257 .collect::<Result<Vec<BuiltinArgument>, _>>();
258
259 let builtin_arguments = match builtin_arguments {
260 Err(err) => return err.into(),
261
262 // reverse argument order, as they are popped from the stack
263 // slice in opposite order
264 Ok(args) => args,
265 };
266
267 // Rewrite the argument to the actual function to take a
268 // `Vec<Value>`, which is then destructured into the
269 // user-defined values in the function header.
270 let sig_span = f.sig.span();
271 rewritten_args.push(parse_quote_spanned!(sig_span=> mut values: Vec<Value>));
272 f.sig.inputs = rewritten_args.into_iter().collect();
273
274 // Rewrite the body of the function to do said argument forcing.
275 //
276 // This is done by creating a new block for each of the
277 // arguments that evaluates it, and wraps the inner block.
278 for arg in &builtin_arguments {
279 let block = &f.block;
280 let ty = &arg.ty;
281 let ident = &arg.name;
282
283 f.block = Box::new(match arg {
284 BuiltinArgument {
285 strict: true,
286 catch: true,
287 ..
288 } => parse_quote_spanned! {
289 arg.span => {
290 let #ident: #ty = snix_eval::generators::request_force(
291 &co, values.pop().expect("Snix bug: builtin called with incorrect number of arguments")
292 ).await;
293 #block
294 }
295 },
296 BuiltinArgument {
297 strict: true,
298 catch: false,
299 ..
300 } => parse_quote_spanned! {
301 arg.span => {
302 let #ident: #ty = snix_eval::generators::request_force(
303 &co, values.pop().expect("Snix bug: builtin called with incorrect number of arguments")
304 ).await;
305 if #ident.is_catchable() {
306 return Ok(#ident);
307 }
308 #block
309 }
310 },
311 BuiltinArgument {
312 strict: false,
313 catch: _,
314 ..
315 } => parse_quote_spanned! {
316 arg.span => {
317 let #ident: #ty = values.pop().expect("Snix bug: builtin called with incorrect number of arguments");
318 #block
319 }
320 },
321 });
322 }
323
324 let fn_name = f.sig.ident.clone();
325 let arg_count = builtin_arguments.len();
326 let docstring = match extract_docstring(&f.attrs) {
327 Some(docs) => quote!(Some(#docs)),
328 None => quote!(None),
329 };
330
331 if captures_state {
332 builtins.push(quote_spanned! { builtin_attr.span() => {
333 let inner_state = state.clone();
334 snix_eval::Builtin::new(
335 #name,
336 #docstring,
337 #arg_count,
338 move |values| Gen::new(|co| snix_eval::generators::pin_generator(#fn_name(inner_state.clone(), co, values))),
339 )
340 }});
341 } else {
342 builtins.push(quote_spanned! { builtin_attr.span() => {
343 snix_eval::Builtin::new(
344 #name,
345 #docstring,
346 #arg_count,
347 |values| Gen::new(|co| snix_eval::generators::pin_generator(#fn_name(co, values))),
348 )
349 }});
350 }
351 }
352 }
353
354 if let Some(state_type) = state_type {
355 items.push(parse_quote! {
356 pub fn builtins(state: #state_type) -> Vec<(&'static str, Value)> {
357 vec![#(#builtins),*].into_iter().map(|b| (b.name(), Value::Builtin(b))).collect()
358 }
359 });
360 } else {
361 items.push(parse_quote! {
362 pub fn builtins() -> Vec<(&'static str, Value)> {
363 vec![#(#builtins),*].into_iter().map(|b| (b.name(), Value::Builtin(b))).collect()
364 }
365 });
366 }
367
368 module.into_token_stream().into()
369}