tvix_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. Information about the variables is emitted using
65 /// data-carrying opcodes (see [`crate::opcode::OpCode::DataStackIdx`]).
66 pub(crate) upvalue_count: usize,
67 pub(crate) formals: Option<Formals>,
68}
69
70impl Lambda {
71 pub fn chunk(&mut self) -> &mut Chunk {
72 &mut self.chunk
73 }
74}
75
76///
77/// In order to correctly reproduce cppnix's "pointer equality"
78/// semantics it is important that we never clone a Lambda --
79/// use `Rc<Lambda>::clone()` instead. This struct deliberately
80/// does not `derive(Clone)` in order to prevent this from being
81/// done accidentally.
82///
83#[derive(/* do not add Clone here */ Debug)]
84pub struct Closure {
85 pub lambda: Rc<Lambda>,
86 pub upvalues: Rc<Upvalues>,
87}
88
89impl Closure {
90 pub fn new(lambda: Rc<Lambda>) -> Self {
91 Self::new_with_upvalues(
92 Rc::new(Upvalues::with_capacity(lambda.upvalue_count)),
93 lambda,
94 )
95 }
96
97 pub fn new_with_upvalues(upvalues: Rc<Upvalues>, lambda: Rc<Lambda>) -> Self {
98 Closure { upvalues, lambda }
99 }
100
101 pub fn chunk(&self) -> &Chunk {
102 &self.lambda.chunk
103 }
104
105 pub fn lambda(&self) -> Rc<Lambda> {
106 self.lambda.clone()
107 }
108
109 pub fn upvalues(&self) -> Rc<Upvalues> {
110 self.upvalues.clone()
111 }
112}