2022-08-23 19:37:00 +02:00
|
|
|
//! This module implements the runtime representation of functions.
|
2024-01-23 13:54:27 +01:00
|
|
|
use std::{collections::BTreeMap, hash::Hash, rc::Rc};
|
2022-08-23 19:37:00 +02:00
|
|
|
|
2022-10-13 05:53:03 +02:00
|
|
|
use codemap::Span;
|
2022-10-22 22:55:21 +02:00
|
|
|
use smol_str::SmolStr;
|
2022-10-13 05:53:03 +02:00
|
|
|
|
2022-10-16 01:10:10 +02:00
|
|
|
use crate::{chunk::Chunk, upvalues::Upvalues};
|
2022-08-23 19:37:00 +02:00
|
|
|
|
2022-10-13 05:27:09 +02:00
|
|
|
use super::NixString;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub(crate) struct Formals {
|
|
|
|
/// Map from argument name, to whether that argument is required
|
2024-01-23 13:54:27 +01:00
|
|
|
pub(crate) arguments: BTreeMap<NixString, bool>,
|
2022-10-13 05:27:09 +02:00
|
|
|
|
|
|
|
/// Do the formals of this function accept extra arguments
|
|
|
|
pub(crate) ellipsis: bool,
|
2022-10-13 05:53:03 +02:00
|
|
|
|
|
|
|
/// The span of the formals themselves, to use to emit errors
|
|
|
|
pub(crate) span: Span,
|
2024-01-23 14:34:38 +01:00
|
|
|
|
|
|
|
/// Optionally tracks a name for all function arguments (args@ style).
|
|
|
|
/// Used by toXML.
|
|
|
|
pub(crate) name: Option<String>,
|
2022-10-13 05:53:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Formals {
|
|
|
|
/// Returns true if the given arg is a valid argument to these formals.
|
|
|
|
///
|
|
|
|
/// This is true if it is either listed in the list of arguments, or the formals have an
|
|
|
|
/// ellipsis
|
|
|
|
pub(crate) fn contains<Q>(&self, arg: &Q) -> bool
|
|
|
|
where
|
2024-01-23 13:54:27 +01:00
|
|
|
Q: ?Sized + Hash + Ord + Eq,
|
2022-10-13 05:53:03 +02:00
|
|
|
NixString: std::borrow::Borrow<Q>,
|
|
|
|
{
|
2022-12-20 15:22:56 +01:00
|
|
|
self.ellipsis || self.arguments.contains_key(arg)
|
2022-10-13 05:53:03 +02:00
|
|
|
}
|
2022-10-13 05:27:09 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 02:06:08 +02:00
|
|
|
/// The opcodes for a thunk or closure, plus the number of
|
2022-10-16 01:10:10 +02:00
|
|
|
/// non-executable opcodes which are allowed after an OpThunkClosure or
|
|
|
|
/// OpThunkSuspended referencing it. At runtime `Lambda` is usually wrapped
|
2022-10-16 02:06:08 +02:00
|
|
|
/// in `Rc` to avoid copying the `Chunk` it holds (which can be
|
|
|
|
/// quite large).
|
2022-11-28 09:18:04 +01:00
|
|
|
///
|
|
|
|
/// In order to correctly reproduce cppnix's "pointer equality"
|
|
|
|
/// semantics it is important that we never clone a Lambda --
|
2023-09-21 23:57:20 +02:00
|
|
|
/// use `Rc<Lambda>::clone()` instead. This struct deliberately
|
2022-11-28 09:18:04 +01:00
|
|
|
/// does not `derive(Clone)` in order to prevent this from being
|
|
|
|
/// done accidentally.
|
|
|
|
///
|
|
|
|
#[derive(/* do not add Clone here */ Debug, Default)]
|
2022-08-23 19:37:00 +02:00
|
|
|
pub struct Lambda {
|
2022-08-27 19:41:10 +02:00
|
|
|
pub(crate) chunk: Chunk,
|
2022-10-16 02:06:08 +02:00
|
|
|
|
2022-10-22 22:55:21 +02:00
|
|
|
/// Name of the function (equivalent to the name of the
|
|
|
|
/// identifier (e.g. a value in a let-expression or an attribute
|
|
|
|
/// set entry) it is located in).
|
|
|
|
pub(crate) name: Option<SmolStr>,
|
|
|
|
|
2022-10-16 02:06:08 +02:00
|
|
|
/// Number of upvalues which the code in this Lambda closes
|
|
|
|
/// over, and which need to be initialised at
|
|
|
|
/// runtime. Information about the variables is emitted using
|
2023-09-21 23:57:20 +02:00
|
|
|
/// data-carrying opcodes (see [`crate::opcode::OpCode::DataStackIdx`]).
|
2022-08-26 20:48:51 +02:00
|
|
|
pub(crate) upvalue_count: usize,
|
2022-10-13 05:27:09 +02:00
|
|
|
pub(crate) formals: Option<Formals>,
|
2022-08-23 21:54:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Lambda {
|
2022-08-27 19:41:10 +02:00
|
|
|
pub fn chunk(&mut self) -> &mut Chunk {
|
2022-08-23 21:54:25 +02:00
|
|
|
&mut self.chunk
|
|
|
|
}
|
2022-08-23 19:37:00 +02:00
|
|
|
}
|
2022-08-24 21:08:26 +02:00
|
|
|
|
2022-11-28 09:18:04 +01:00
|
|
|
///
|
|
|
|
/// In order to correctly reproduce cppnix's "pointer equality"
|
|
|
|
/// semantics it is important that we never clone a Lambda --
|
2023-09-21 23:57:20 +02:00
|
|
|
/// use `Rc<Lambda>::clone()` instead. This struct deliberately
|
2022-11-28 09:18:04 +01:00
|
|
|
/// does not `derive(Clone)` in order to prevent this from being
|
|
|
|
/// done accidentally.
|
|
|
|
///
|
|
|
|
#[derive(/* do not add Clone here */ Debug)]
|
2022-10-16 01:10:10 +02:00
|
|
|
pub struct Closure {
|
2022-08-28 05:07:20 +02:00
|
|
|
pub lambda: Rc<Lambda>,
|
2022-11-24 09:35:33 +01:00
|
|
|
pub upvalues: Rc<Upvalues>,
|
2022-08-26 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Closure {
|
2022-08-28 05:07:20 +02:00
|
|
|
pub fn new(lambda: Rc<Lambda>) -> Self {
|
2022-11-24 09:35:33 +01:00
|
|
|
Self::new_with_upvalues(
|
|
|
|
Rc::new(Upvalues::with_capacity(lambda.upvalue_count)),
|
|
|
|
lambda,
|
|
|
|
)
|
2022-08-27 19:41:10 +02:00
|
|
|
}
|
|
|
|
|
2022-11-24 09:35:33 +01:00
|
|
|
pub fn new_with_upvalues(upvalues: Rc<Upvalues>, lambda: Rc<Lambda>) -> Self {
|
2022-11-27 09:54:39 +01:00
|
|
|
Closure { upvalues, lambda }
|
2022-08-27 19:41:10 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 01:10:10 +02:00
|
|
|
pub fn chunk(&self) -> &Chunk {
|
|
|
|
&self.lambda.chunk
|
2022-08-27 19:41:10 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 01:10:10 +02:00
|
|
|
pub fn lambda(&self) -> Rc<Lambda> {
|
|
|
|
self.lambda.clone()
|
2022-08-26 23:21:08 +02:00
|
|
|
}
|
2022-08-28 16:28:20 +02:00
|
|
|
|
2022-11-28 09:18:04 +01:00
|
|
|
pub fn upvalues(&self) -> Rc<Upvalues> {
|
|
|
|
self.upvalues.clone()
|
2022-08-28 16:28:20 +02:00
|
|
|
}
|
2022-08-24 21:08:26 +02:00
|
|
|
}
|