1use std::{fmt::Debug, rc::Rc};23use educe::Educe;4use jrsonnet_gcmodule::{Cc, Trace};5use jrsonnet_interner::IStr;6use jrsonnet_ir::{ArgsDesc, Destruct, Expr, ExprParams, Span};7pub use jrsonnet_macros::builtin;89use self::{10 builtin::{Builtin, StaticBuiltin},11 parse::{parse_builtin_call, parse_default_function_call, parse_function_call},12 prepared::{PreparedCall, parse_prepared_builtin_call, parse_prepared_function_call},13};14use crate::{15 Context, Result, Thunk, Val, bail, error::ErrorKind::*, evaluate, evaluate_trivial,16 function::builtin::BuiltinFunc,17};1819pub mod builtin;20mod native;21mod parse;22mod prepared;2324pub use jrsonnet_ir::function::*;25pub use native::NativeFn;26pub use prepared::PreparedFuncVal;27282930#[derive(Clone, Copy)]31pub struct CallLocation<'l>(pub Option<&'l Span>);32impl<'l> CallLocation<'l> {33 34 pub const fn new(loc: &'l Span) -> Self {35 Self(Some(loc))36 }37}38impl CallLocation<'static> {39 40 pub const fn native() -> Self {41 Self(None)42 }43}444546#[derive(Trace, Educe)]47#[educe(Debug, PartialEq)]48pub struct FuncDesc {49 50 51 52 53 54 55 56 57 58 pub name: IStr,59 60 61 62 63 64 65 66 67 68 pub ctx: Context,6970 71 pub params: ExprParams,72 73 pub body: Rc<Expr>,74}75impl FuncDesc {76 77 pub fn default_body_context(&self) -> Result<Context> {78 parse_default_function_call(self.ctx.clone(), &self.params)79 }8081 82 pub(crate) fn call_body_context(83 &self,84 call_ctx: Context,85 args: &ArgsDesc,86 tailstrict: bool,87 ) -> Result<Context> {88 parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)89 }9091 pub fn evaluate_trivial(&self) -> Option<Val> {92 evaluate_trivial(&self.body)93 }94}959697#[allow(clippy::module_name_repetitions)]98#[derive(Trace, Clone)]99pub enum FuncVal {100 101 Id,102 103 Normal(Cc<FuncDesc>),104 105 Thunk(Thunk<Val>),106 107 StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),108 109 Builtin(BuiltinFunc),110}111112impl Debug for FuncVal {113 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {114 match self {115 Self::Id => f.debug_tuple("Id").finish(),116 Self::Thunk(arg0) => f.debug_tuple("Thunk").field(arg0).finish(),117 Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),118 Self::StaticBuiltin(arg0) => {119 f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()120 }121 Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),122 }123 }124}125126#[allow(clippy::unnecessary_wraps)]127#[builtin]128const fn builtin_id(x: Val) -> Val {129 x130}131static ID: &builtin_id = &builtin_id {};132133impl FuncVal {134 pub fn builtin(builtin: impl Builtin) -> Self {135 Self::Builtin(BuiltinFunc::new(builtin))136 }137 pub fn static_builtin(static_builtin: &'static dyn StaticBuiltin) -> Self {138 Self::StaticBuiltin(static_builtin)139 }140141 pub fn params(&self) -> FunctionSignature {142 match self {143 Self::Id => ID.params(),144 Self::StaticBuiltin(i) => i.params(),145 Self::Builtin(i) => i.params(),146 Self::Normal(p) => p.params.signature.clone(),147 Self::Thunk(_) => FunctionSignature::empty(),148 }149 }150 151 pub fn params_len(&self) -> usize {152 self.params().iter().filter(|p| !p.has_default()).count()153 }154 155 pub fn name(&self) -> IStr {156 match self {157 Self::Id => "id".into(),158 Self::Normal(normal) => normal.name.clone(),159 Self::StaticBuiltin(builtin) => builtin.name().into(),160 Self::Builtin(builtin) => builtin.name().into(),161 Self::Thunk(_) => "thunk".into(),162 }163 }164 165 166 167 pub fn evaluate(168 &self,169 call_ctx: Context,170 loc: CallLocation<'_>,171 args: &ArgsDesc,172 tailstrict: bool,173 ) -> Result<Val> {174 match self {175 Self::Normal(func) => {176 let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;177 evaluate(body_ctx, &func.body)178 }179 Self::Thunk(thunk) => {180 if !args.named.is_empty() || !args.unnamed.is_empty() {181 bail!(TooManyArgsFunctionHas(0, FunctionSignature::empty()))182 }183 thunk.evaluate()184 }185 Self::Id => {186 let args = parse_builtin_call(call_ctx, ID.params(), args, tailstrict)?;187 ID.call(loc, &args)188 }189 Self::StaticBuiltin(b) => {190 let args = parse_builtin_call(call_ctx, b.params(), args, tailstrict)?;191 b.call(loc, &args)192 }193 Self::Builtin(b) => {194 let args = parse_builtin_call(call_ctx, b.params(), args, tailstrict)?;195 b.call(loc, &args)196 }197 }198 }199200 pub(crate) fn evaluate_prepared(201 &self,202 prepared: &PreparedCall,203 loc: CallLocation<'_>,204 unnamed: &[Thunk<Val>],205 named: &[Thunk<Val>],206 _tailstrict: bool,207 ) -> Result<Val> {208 match self {209 FuncVal::Normal(func) => {210 let body_ctx = parse_prepared_function_call(211 func.ctx.clone(),212 prepared,213 &func.params,214 unnamed,215 named,216 )?;217 evaluate(body_ctx, &func.body)218 }219 FuncVal::Thunk(t) => t.evaluate(),220 FuncVal::Id => {221 let args = parse_prepared_builtin_call(prepared, ID.params(), unnamed, named);222 ID.call(loc, &args)223 }224 FuncVal::StaticBuiltin(b) => {225 let args = parse_prepared_builtin_call(prepared, b.params(), unnamed, named);226 b.call(loc, &args)227 }228 FuncVal::Builtin(b) => {229 let args = parse_prepared_builtin_call(prepared, b.params(), unnamed, named);230 b.call(loc, &args)231 }232 }233 }234235 236 237 238 239 240 pub fn is_identity(&self) -> bool {241 match self {242 Self::Id => true,243 Self::Normal(desc) => {244 if desc.params.len() != 1 {245 return false;246 }247 let param = &desc.params.exprs[0];248 if param.default.is_some() {249 return false;250 }251252 #[allow(clippy::infallible_destructuring_match)]253 let id = match ¶m.destruct {254 Destruct::Full(id) => id,255 #[cfg(feature = "exp-destruct")]256 _ => return false,257 };258 matches!(&*desc.body, Expr::Var(v) if &**v == id)259 }260 _ => false,261 }262 }263 264 pub const fn identity() -> Self {265 Self::Id266 }267268 pub fn evaluate_trivial(&self) -> Option<Val> {269 match self {270 Self::Normal(n) => n.evaluate_trivial(),271 _ => None,272 }273 }274}275276impl<T> From<T> for FuncVal277where278 T: Builtin,279{280 fn from(value: T) -> Self {281 Self::builtin(value)282 }283}284impl From<&'static dyn StaticBuiltin> for FuncVal {285 fn from(value: &'static dyn StaticBuiltin) -> Self {286 Self::static_builtin(value)287 }288}