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};1112use crate::location::{location_to_offset, offset_to_location, CodeLocation};1314macro_rules! any_ext_methods {15 ($T:ident) => {16 fn as_any(&self) -> &dyn Any;17 fn dyn_hash(&self, hasher: &mut dyn Hasher);18 fn dyn_eq(&self, other: &dyn $T) -> bool;19 fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;20 };21}22macro_rules! any_ext_impl {23 ($T:ident) => {24 fn as_any(&self) -> &dyn Any {25 self26 }27 fn dyn_hash(&self, mut hasher: &mut dyn Hasher) {28 self.hash(&mut hasher)29 }30 fn dyn_eq(&self, other: &dyn $T) -> bool {31 let Some(other) = other.as_any().downcast_ref::<Self>() else {32 return false;33 };34 let this = <Self as $T>::as_any(self)35 .downcast_ref::<Self>()36 .expect("restricted by impl");37 this == other38 }39 fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {40 <Self as std::fmt::Debug>::fmt(self, fmt)41 }42 };43}44macro_rules! any_ext {45 ($T:ident) => {46 impl Hash for dyn $T {47 fn hash<H: Hasher>(&self, state: &mut H) {48 self.dyn_hash(state)49 }50 }51 impl PartialEq for dyn $T {52 fn eq(&self, other: &Self) -> bool {53 self.dyn_eq(other)54 }55 }56 impl Eq for dyn $T {}57 };58}59pub trait SourcePathT: Trace + Debug + Display {60 61 62 fn is_default(&self) -> bool;63 fn path(&self) -> Option<&Path>;64 any_ext_methods!(SourcePathT);65}66any_ext!(SourcePathT);67686970717273747576777879808182#[derive(Eq, Debug, Clone)]83pub struct SourcePath(Rc<dyn SourcePathT>);84impl SourcePath {85 pub fn new(inner: impl SourcePathT) -> Self {86 Self(Rc::new(inner))87 }88 pub fn downcast_ref<T: SourcePathT>(&self) -> Option<&T> {89 self.0.as_any().downcast_ref()90 }91 pub fn is_default(&self) -> bool {92 self.0.is_default()93 }94 pub fn path(&self) -> Option<&Path> {95 self.0.path()96 }97}98impl Hash for SourcePath {99 fn hash<H: Hasher>(&self, state: &mut H) {100 self.0.hash(state);101 }102}103impl PartialEq for SourcePath {104 #[allow(clippy::op_ref)]105 fn eq(&self, other: &Self) -> bool {106 &*self.0 == &*other.0107 }108}109impl Trace for SourcePath {110 fn trace(&self, tracer: &mut Tracer) {111 (*self.0).trace(tracer)112 }113114 fn is_type_tracked() -> bool115 where116 Self: Sized,117 {118 true119 }120}121impl Display for SourcePath {122 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {123 write!(f, "{}", self.0)124 }125}126impl Default for SourcePath {127 fn default() -> Self {128 Self(Rc::new(SourceDefault))129 }130}131132#[derive(Trace, Hash, PartialEq, Eq, Debug)]133struct SourceDefault;134impl Display for SourceDefault {135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {136 write!(f, "<default>")137 }138}139impl SourcePathT for SourceDefault {140 fn is_default(&self) -> bool {141 true142 }143 fn path(&self) -> Option<&Path> {144 None145 }146 any_ext_impl!(SourcePathT);147}148149150151152153154#[derive(Trace, Hash, PartialEq, Eq, Debug)]155pub struct SourceFile(PathBuf);156impl SourceFile {157 pub fn new(path: PathBuf) -> Self {158 Self(path)159 }160 pub fn path(&self) -> &Path {161 &self.0162 }163}164impl Display for SourceFile {165 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {166 write!(f, "{}", self.0.display())167 }168}169impl SourcePathT for SourceFile {170 fn is_default(&self) -> bool {171 false172 }173 fn path(&self) -> Option<&Path> {174 Some(&self.0)175 }176 any_ext_impl!(SourcePathT);177}178179180181182#[derive(Trace, Hash, PartialEq, Eq, Debug)]183pub struct SourceDirectory(PathBuf);184impl SourceDirectory {185 pub fn new(path: PathBuf) -> Self {186 Self(path)187 }188 pub fn path(&self) -> &Path {189 &self.0190 }191}192impl Display for SourceDirectory {193 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {194 write!(f, "{}", self.0.display())195 }196}197impl SourcePathT for SourceDirectory {198 fn is_default(&self) -> bool {199 false200 }201 fn path(&self) -> Option<&Path> {202 Some(&self.0)203 }204 any_ext_impl!(SourcePathT);205}206207208209210211#[derive(Trace, Hash, PartialEq, Eq, Debug, Clone)]212pub struct SourceVirtual(pub IStr);213impl Display for SourceVirtual {214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {215 write!(f, "{}", self.0)216 }217}218impl SourcePathT for SourceVirtual {219 fn is_default(&self) -> bool {220 true221 }222 fn path(&self) -> Option<&Path> {223 None224 }225 any_ext_impl!(SourcePathT);226}227228229230231232233#[allow(clippy::derived_hash_with_manual_eq)]234#[derive(Trace, Debug, Hash)]235pub struct SourceFifo(pub String, pub IBytes);236impl PartialEq for SourceFifo {237 fn eq(&self, other: &Self) -> bool {238 std::ptr::eq(self, other)239 }240}241impl fmt::Display for SourceFifo {242 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {243 write!(f, "fifo({:?})", self.0)244 }245}246impl SourcePathT for SourceFifo {247 fn is_default(&self) -> bool {248 249 true250 }251252 fn path(&self) -> Option<&Path> {253 None254 }255256 any_ext_impl!(SourcePathT);257}258259260261#[derive(Clone, PartialEq, Eq, Debug)]262pub struct Source(pub Rc<(SourcePath, IStr)>);263264impl Trace for Source {265 fn trace(&self, _tracer: &mut Tracer) {}266267 fn is_type_tracked() -> bool {268 false269 }270}271272impl Source {273 pub fn new(path: SourcePath, code: IStr) -> Self {274 Self(Rc::new((path, code)))275 }276277 pub fn new_virtual(name: IStr, code: IStr) -> Self {278 Self::new(SourcePath::new(SourceVirtual(name)), code)279 }280281 pub fn code(&self) -> &str {282 &self.0 .1283 }284285 pub fn source_path(&self) -> &SourcePath {286 &self.0 .0287 }288289 pub fn map_source_locations<const S: usize>(&self, locs: &[u32; S]) -> [CodeLocation; S] {290 offset_to_location(&self.0 .1, locs)291 }292 pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {293 location_to_offset(&self.0 .1, line, column)294 }295}