snix_eval/value/function.rs
1//! This module implements the runtime representation of functions.
2use std::{collections::BTreeMap, hash::Hash, rc::Rc};
3
4use codemap::Span;
5use smol_str::SmolStr;
6
7use crate::{chunk::Chunk, upvalues::Upvalues};
8
9use super::NixString;
10
11#[derive(Clone, Debug, PartialEq)]
12pub(crate) struct Formals {
13 /// Map from argument name, to whether that argument is required
14 pub(crate) arguments: BTreeMap<NixString, bool>,
15
16 /// Do the formals of this function accept extra arguments
17 pub(crate) ellipsis: bool,
18
19 /// The span of the formals themselves, to use to emit errors
20 pub(crate) span: Span,
21
22 /// Optionally tracks a name for all function arguments (args@ style).
23 /// Used by toXML.
24 pub(crate) name: Option<String>,
25}
26
27impl Formals {
28 /// Returns true if the given arg is a valid argument to these formals.
29 ///
30 /// This is true if it is either listed in the list of arguments, or the formals have an
31 /// ellipsis
32 pub(crate) fn contains<Q>(&self, arg: &Q) -> bool
33 where
34 Q: ?Sized + Hash + Ord + Eq,
35 NixString: std::borrow::Borrow<Q>,
36 {
37 self.ellipsis || self.arguments.contains_key(arg)
38 }
39}
40
41/// The opcodes for a thunk or closure, plus the number of
42/// non-executable opcodes which are allowed after an OpThunkClosure or
43/// OpThunkSuspended referencing it. At runtime `Lambda` is usually wrapped
44/// in `Rc` to avoid copying the `Chunk` it holds (which can be
45/// quite large).
46///
47/// In order to correctly reproduce cppnix's "pointer equality"
48/// semantics it is important that we never clone a Lambda --
49/// use `Rc<Lambda>::clone()` instead. This struct deliberately
50/// does not `derive(Clone)` in order to prevent this from being
51/// done accidentally.
52///
53#[derive(/* do not add Clone here */ Debug, Default)]
54pub struct Lambda {
55 pub(crate) chunk: Chunk,
56
57 /// Name of the function (equivalent to the name of the
58 /// identifier (e.g. a value in a let-expression or an attribute
59 /// set entry) it is located in).
60 pub(crate) name: Option<SmolStr>,
61
62 /// Number of upvalues which the code in this Lambda closes
63 /// over, and which need to be initialised at
64 /// runtime.
65 pub(crate) upvalue_count: usize,
66 pub(crate) formals: Option<Formals>,
67}
68
69impl Lambda {
70 pub fn chunk(&mut self) -> &mut Chunk {
71 &mut self.chunk
72 }
73}
74
75///
76/// In order to correctly reproduce cppnix's "pointer equality"
77/// semantics it is important that we never clone a Lambda --
78/// use `Rc<Lambda>::clone()` instead. This struct deliberately
79/// does not `derive(Clone)` in order to prevent this from being
80/// done accidentally.
81///
82#[derive(/* do not add Clone here */ Debug)]
83pub struct Closure {
84 pub lambda: Rc<Lambda>,
85 pub upvalues: Rc<Upvalues>,
86}
87
88impl Closure {
89 pub fn new(lambda: Rc<Lambda>) -> Self {
90 Self::new_with_upvalues(
91 Rc::new(Upvalues::with_capacity(lambda.upvalue_count)),
92 lambda,
93 )
94 }
95
96 pub fn new_with_upvalues(upvalues: Rc<Upvalues>, lambda: Rc<Lambda>) -> Self {
97 Closure { upvalues, lambda }
98 }
99
100 pub fn chunk(&self) -> &Chunk {
101 &self.lambda.chunk
102 }
103
104 pub fn lambda(&self) -> Rc<Lambda> {
105 self.lambda.clone()
106 }
107
108 pub fn upvalues(&self) -> Rc<Upvalues> {
109 self.upvalues.clone()
110 }
111}