1use std::fmt::Debug;23pub use arglike::{ArgLike, ArgsLike, TlaArg};4use jrsonnet_gcmodule::{Cc, Trace};5use jrsonnet_interner::IStr;6pub use jrsonnet_macros::builtin;7use jrsonnet_parser::{Destruct, Expr, ExprLocation, LocExpr, ParamsDesc};89use self::{10 arglike::OptionalContext,11 builtin::{Builtin, StaticBuiltin},12 native::NativeDesc,13 parse::{parse_default_function_call, parse_function_call},14};15use crate::{evaluate, gc::TraceBox, typed::Any, Context, ContextBuilder, Result, Val};1617pub mod arglike;18pub mod builtin;19pub mod native;20pub mod parse;21222324#[derive(Clone, Copy)]25pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);26impl<'l> CallLocation<'l> {27 28 pub const fn new(loc: &'l ExprLocation) -> Self {29 Self(Some(loc))30 }31}32impl CallLocation<'static> {33 34 pub const fn native() -> Self {35 Self(None)36 }37}383940#[derive(Debug, PartialEq, Trace)]41pub struct FuncDesc {42 43 44 45 46 47 48 49 50 51 pub name: IStr,52 53 54 55 56 57 58 59 60 61 pub ctx: Context,6263 64 pub params: ParamsDesc,65 66 pub body: LocExpr,67}68impl FuncDesc {69 70 pub fn default_body_context(&self) -> Result<Context> {71 parse_default_function_call(self.ctx.clone(), &self.params)72 }7374 75 pub fn call_body_context(76 &self,77 call_ctx: Context,78 args: &dyn ArgsLike,79 tailstrict: bool,80 ) -> Result<Context> {81 parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)82 }83}848586#[allow(clippy::module_name_repetitions)]87#[derive(Trace, Clone)]88pub enum FuncVal {89 90 Id,91 92 Normal(Cc<FuncDesc>),93 94 StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),95 96 Builtin(Cc<TraceBox<dyn Builtin>>),97}9899impl Debug for FuncVal {100 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {101 match self {102 Self::Id => f.debug_tuple("Id").finish(),103 Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),104 Self::StaticBuiltin(arg0) => {105 f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()106 }107 Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),108 }109 }110}111112impl FuncVal {113 114 pub fn params_len(&self) -> usize {115 match self {116 Self::Id => 1,117 Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),118 Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),119 Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),120 }121 }122 123 pub fn name(&self) -> IStr {124 match self {125 Self::Id => "id".into(),126 Self::Normal(normal) => normal.name.clone(),127 Self::StaticBuiltin(builtin) => builtin.name().into(),128 Self::Builtin(builtin) => builtin.name().into(),129 }130 }131 132 133 134 pub fn evaluate(135 &self,136 call_ctx: Context,137 loc: CallLocation<'_>,138 args: &dyn ArgsLike,139 tailstrict: bool,140 ) -> Result<Val> {141 match self {142 Self::Id => {143 #[allow(clippy::unnecessary_wraps)]144 #[builtin]145 const fn builtin_id(v: Any) -> Result<Any> {146 Ok(v)147 }148 static ID: &builtin_id = &builtin_id {};149150 ID.call(call_ctx, loc, args)151 }152 Self::Normal(func) => {153 let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;154 evaluate(body_ctx, &func.body)155 }156 Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),157 Self::Builtin(b) => b.call(call_ctx, loc, args),158 }159 }160 pub fn evaluate_simple<A: ArgsLike + OptionalContext>(&self, args: &A) -> Result<Val> {161 self.evaluate(162 ContextBuilder::dangerous_empty_state().build(),163 CallLocation::native(),164 args,165 true,166 )167 }168 169 pub fn into_native<D: NativeDesc>(self) -> D::Value {170 D::into_native(self)171 }172173 174 175 176 177 178 pub fn is_identity(&self) -> bool {179 match self {180 Self::Id => true,181 Self::Normal(desc) => {182 if desc.params.len() != 1 {183 return false;184 }185 let param = &desc.params[0];186 if param.1.is_some() {187 return false;188 }189 #[allow(clippy::infallible_destructuring_match)]190 let id = match ¶m.0 {191 Destruct::Full(id) => id,192 #[cfg(feature = "exp-destruct")]193 _ => return false,194 };195 &desc.body.0 as &Expr == &Expr::Var(id.clone())196 }197 _ => false,198 }199 }200 201 pub const fn identity() -> Self {202 Self::Id203 }204}