1use std::{2 any::Any,3 fmt::{self, Debug, Display},4 hash::{Hash, Hasher},5 path::{Path, PathBuf},6 rc::Rc,7};89use jrsonnet_gcmodule::{Trace, Tracer};10use jrsonnet_interner::{IBytes, IStr};11#[cfg(feature = "serde")]12use serde::{Deserialize, Serialize};13#[cfg(feature = "structdump")]14use structdump::Codegen;1516use crate::location::{location_to_offset, offset_to_location, CodeLocation};1718macro_rules! any_ext_methods {19 ($T:ident) => {20 fn as_any(&self) -> &dyn Any;21 fn dyn_hash(&self, hasher: &mut dyn Hasher);22 fn dyn_eq(&self, other: &dyn $T) -> bool;23 fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;24 };25}26macro_rules! any_ext_impl {27 ($T:ident) => {28 fn as_any(&self) -> &dyn Any {29 self30 }31 fn dyn_hash(&self, mut hasher: &mut dyn Hasher) {32 self.hash(&mut hasher)33 }34 fn dyn_eq(&self, other: &dyn $T) -> bool {35 let Some(other) = other.as_any().downcast_ref::<Self>() else {36 return false;37 };38 let this = <Self as $T>::as_any(self)39 .downcast_ref::<Self>()40 .expect("restricted by impl");41 this == other42 }43 fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {44 <Self as std::fmt::Debug>::fmt(self, fmt)45 }46 };47}48macro_rules! any_ext {49 ($T:ident) => {50 impl Hash for dyn $T {51 fn hash<H: Hasher>(&self, state: &mut H) {52 self.dyn_hash(state)53 }54 }55 impl PartialEq for dyn $T {56 fn eq(&self, other: &Self) -> bool {57 self.dyn_eq(other)58 }59 }60 impl Eq for dyn $T {}61 };62}63pub trait SourcePathT: Trace + Debug + Display {64 65 66 fn is_default(&self) -> bool;67 fn path(&self) -> Option<&Path>;68 any_ext_methods!(SourcePathT);69}70any_ext!(SourcePathT);71727374757677787980818283848586#[derive(Eq, Debug, Clone)]87pub struct SourcePath(Rc<dyn SourcePathT>);88impl SourcePath {89 pub fn new(inner: impl SourcePathT) -> Self {90 Self(Rc::new(inner))91 }92 pub fn downcast_ref<T: SourcePathT>(&self) -> Option<&T> {93 self.0.as_any().downcast_ref()94 }95 pub fn is_default(&self) -> bool {96 self.0.is_default()97 }98 pub fn path(&self) -> Option<&Path> {99 self.0.path()100 }101}102impl Hash for SourcePath {103 fn hash<H: Hasher>(&self, state: &mut H) {104 self.0.hash(state);105 }106}107impl PartialEq for SourcePath {108 #[allow(clippy::op_ref)]109 fn eq(&self, other: &Self) -> bool {110 &*self.0 == &*other.0111 }112}113impl Trace for SourcePath {114 fn trace(&self, tracer: &mut Tracer) {115 (*self.0).trace(tracer)116 }117118 fn is_type_tracked() -> bool119 where120 Self: Sized,121 {122 true123 }124}125impl Display for SourcePath {126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {127 write!(f, "{}", self.0)128 }129}130impl Default for SourcePath {131 fn default() -> Self {132 Self(Rc::new(SourceDefault))133 }134}135136#[cfg(feature = "structdump")]137impl Codegen for SourcePath {138 fn gen_code(139 &self,140 res: &mut structdump::CodegenResult,141 unique: bool,142 ) -> structdump::TokenStream {143 let source_virtual = self144 .0145 .as_any()146 .downcast_ref::<SourceVirtual>()147 .expect("can only codegen for virtual source paths!")148 .0149 .clone();150 let val = res.add_value(source_virtual, false);151 res.add_code(152 structdump::quote! {153 structdump_import::SourcePath::new(structdump_import::SourceVirtual(#val))154 },155 Some(structdump::quote!(SourcePath)),156 unique,157 )158 }159}160161#[derive(Trace, Hash, PartialEq, Eq, Debug)]162struct SourceDefault;163impl Display for SourceDefault {164 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {165 write!(f, "<default>")166 }167}168impl SourcePathT for SourceDefault {169 fn is_default(&self) -> bool {170 true171 }172 fn path(&self) -> Option<&Path> {173 None174 }175 any_ext_impl!(SourcePathT);176}177178179180181182183#[derive(Trace, Hash, PartialEq, Eq, Debug)]184pub struct SourceFile(PathBuf);185impl SourceFile {186 pub fn new(path: PathBuf) -> Self {187 Self(path)188 }189 pub fn path(&self) -> &Path {190 &self.0191 }192}193impl Display for SourceFile {194 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {195 write!(f, "{}", self.0.display())196 }197}198impl SourcePathT for SourceFile {199 fn is_default(&self) -> bool {200 false201 }202 fn path(&self) -> Option<&Path> {203 Some(&self.0)204 }205 any_ext_impl!(SourcePathT);206}207208209210211#[derive(Trace, Hash, PartialEq, Eq, Debug)]212pub struct SourceDirectory(PathBuf);213impl SourceDirectory {214 pub fn new(path: PathBuf) -> Self {215 Self(path)216 }217 pub fn path(&self) -> &Path {218 &self.0219 }220}221impl Display for SourceDirectory {222 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {223 write!(f, "{}", self.0.display())224 }225}226impl SourcePathT for SourceDirectory {227 fn is_default(&self) -> bool {228 false229 }230 fn path(&self) -> Option<&Path> {231 Some(&self.0)232 }233 any_ext_impl!(SourcePathT);234}235236237238239240#[cfg_attr(feature = "structdump", derive(Codegen))]241#[derive(Trace, Hash, PartialEq, Eq, Debug, Clone)]242pub struct SourceVirtual(pub IStr);243impl Display for SourceVirtual {244 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {245 write!(f, "{}", self.0)246 }247}248impl SourcePathT for SourceVirtual {249 fn is_default(&self) -> bool {250 true251 }252 fn path(&self) -> Option<&Path> {253 None254 }255 any_ext_impl!(SourcePathT);256}257258259260261262263#[allow(clippy::derived_hash_with_manual_eq)]264#[derive(Trace, Debug, Hash)]265pub struct SourceFifo(pub String, pub IBytes);266impl PartialEq for SourceFifo {267 fn eq(&self, other: &Self) -> bool {268 std::ptr::eq(self, other)269 }270}271impl fmt::Display for SourceFifo {272 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {273 write!(f, "fifo({:?})", self.0)274 }275}276impl SourcePathT for SourceFifo {277 fn is_default(&self) -> bool {278 279 true280 }281282 fn path(&self) -> Option<&Path> {283 None284 }285286 any_ext_impl!(SourcePathT);287}288289290291#[cfg_attr(feature = "structdump", derive(Codegen))]292#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]293#[derive(Clone, PartialEq, Eq, Debug)]294pub struct Source(pub Rc<(SourcePath, IStr)>);295296impl Trace for Source {297 fn trace(&self, _tracer: &mut Tracer) {}298299 fn is_type_tracked() -> bool {300 false301 }302}303304impl Source {305 pub fn new(path: SourcePath, code: IStr) -> Self {306 Self(Rc::new((path, code)))307 }308309 pub fn new_virtual(name: IStr, code: IStr) -> Self {310 Self::new(SourcePath::new(SourceVirtual(name)), code)311 }312313 pub fn code(&self) -> &str {314 &self.0 .1315 }316317 pub fn source_path(&self) -> &SourcePath {318 &self.0 .0319 }320321 pub fn map_source_locations<const S: usize>(&self, locs: &[u32; S]) -> [CodeLocation; S] {322 offset_to_location(&self.0 .1, locs)323 }324 pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {325 location_to_offset(&self.0 .1, line, column)326 }327}