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, BuiltinParam, ParamName, StaticBuiltin},12 native::NativeDesc,13 parse::{parse_default_function_call, parse_function_call},14};15use crate::{evaluate, evaluate_trivial, gc::TraceBox, tb, 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 }8384 pub fn evaluate_trivial(&self) -> Option<Val> {85 evaluate_trivial(&self.body)86 }87}888990#[allow(clippy::module_name_repetitions)]91#[derive(Trace, Clone)]92pub enum FuncVal {93 94 Id,95 96 Normal(Cc<FuncDesc>),97 98 StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),99 100 Builtin(Cc<TraceBox<dyn Builtin>>),101}102103impl Debug for FuncVal {104 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {105 match self {106 Self::Id => f.debug_tuple("Id").finish(),107 Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),108 Self::StaticBuiltin(arg0) => {109 f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()110 }111 Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),112 }113 }114}115116#[allow(clippy::unnecessary_wraps)]117#[builtin]118const fn builtin_id(x: Val) -> Val {119 x120}121static ID: &builtin_id = &builtin_id {};122123impl FuncVal {124 pub fn builtin(builtin: impl Builtin) -> Self {125 Self::Builtin(Cc::new(tb!(builtin)))126 }127128 pub fn params(&self) -> Vec<BuiltinParam> {129 match self {130 Self::Id => ID.params().to_vec(),131 Self::StaticBuiltin(i) => i.params().to_vec(),132 Self::Builtin(i) => i.params().to_vec(),133 Self::Normal(p) => p134 .params135 .iter()136 .map(|p| {137 BuiltinParam::new(138 p.0.name()139 .as_ref()140 .map(IStr::to_string)141 .map_or(ParamName::ANONYMOUS, ParamName::new_dynamic),142 p.1.is_some(),143 )144 })145 .collect(),146 }147 }148 149 pub fn params_len(&self) -> usize {150 match self {151 Self::Id => 1,152 Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),153 Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default()).count(),154 Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default()).count(),155 }156 }157 158 pub fn name(&self) -> IStr {159 match self {160 Self::Id => "id".into(),161 Self::Normal(normal) => normal.name.clone(),162 Self::StaticBuiltin(builtin) => builtin.name().into(),163 Self::Builtin(builtin) => builtin.name().into(),164 }165 }166 167 168 169 pub fn evaluate(170 &self,171 call_ctx: Context,172 loc: CallLocation<'_>,173 args: &dyn ArgsLike,174 tailstrict: bool,175 ) -> Result<Val> {176 match self {177 Self::Id => ID.call(call_ctx, loc, args),178 Self::Normal(func) => {179 let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;180 evaluate(body_ctx, &func.body)181 }182 Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),183 Self::Builtin(b) => b.call(call_ctx, loc, args),184 }185 }186 pub fn evaluate_simple<A: ArgsLike + OptionalContext>(187 &self,188 args: &A,189 tailstrict: bool,190 ) -> Result<Val> {191 self.evaluate(192 ContextBuilder::dangerous_empty_state().build(),193 CallLocation::native(),194 args,195 tailstrict,196 )197 }198 199 pub fn into_native<D: NativeDesc>(self) -> D::Value {200 D::into_native(self)201 }202203 204 205 206 207 208 pub fn is_identity(&self) -> bool {209 match self {210 Self::Id => true,211 Self::Normal(desc) => {212 if desc.params.len() != 1 {213 return false;214 }215 let param = &desc.params[0];216 if param.1.is_some() {217 return false;218 }219 #[allow(clippy::infallible_destructuring_match)]220 let id = match ¶m.0 {221 Destruct::Full(id) => id,222 #[cfg(feature = "exp-destruct")]223 _ => return false,224 };225 &desc.body.0 as &Expr == &Expr::Var(id.clone())226 }227 _ => false,228 }229 }230 231 pub const fn identity() -> Self {232 Self::Id233 }234235 pub fn evaluate_trivial(&self) -> Option<Val> {236 match self {237 FuncVal::Normal(n) => n.evaluate_trivial(),238 _ => None,239 }240 }241}