use smallvec::SmallVec;
use solar_data_structures::map::FxHashSet;
use solar_interface::{Span, Symbol};
pub type DebugSpans = SmallVec<[Span; 2]>;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DebugFunction {
pub identifier: Symbol,
pub declaration: Span,
pub dispatched: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DebugFunctionExit {
Return,
Revert,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum DebugLocKind {
#[default]
Normal,
Dropped,
CompilerGenerated,
Unknown,
Temporary,
}
impl DebugLocKind {
#[must_use]
pub const fn keyword(self) -> Option<&'static str> {
match self {
Self::Normal => None,
Self::Dropped => Some("dropped"),
Self::CompilerGenerated => Some("compiler_generated"),
Self::Unknown => Some("unknown"),
Self::Temporary => Some("temporary"),
}
}
#[must_use]
pub const fn merge(self, other: Self) -> Self {
match (self, other) {
(Self::Normal, _) | (_, Self::Normal) => Self::Normal,
(a, b) if a as u8 == b as u8 => a,
_ => Self::Unknown,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct DebugLoc {
span: Span,
additional_spans: Option<Box<AdditionalSpans>>,
modifier_depth: u32,
kind: DebugLocKind,
all_spans: bool,
}
impl DebugLoc {
pub const EMPTY: Self = Self {
span: Span::DUMMY,
additional_spans: None,
modifier_depth: 0,
kind: DebugLocKind::Normal,
all_spans: false,
};
#[must_use]
pub fn span(&self) -> Option<Span> {
(!self.span.is_dummy()).then_some(self.span)
}
#[must_use]
pub fn only_span(&self) -> Option<Span> {
if self.additional_spans.is_some() { None } else { self.span() }
}
pub fn spans(&self) -> impl Iterator<Item = Span> + Clone + '_ {
self.span()
.into_iter()
.chain(self.additional_spans.iter().flat_map(|spans| spans.spans.iter().copied()))
}
#[must_use]
pub fn has_spans(&self) -> bool {
!self.span.is_dummy()
}
#[must_use]
pub fn all_spans(&self) -> bool {
self.all_spans
}
pub fn set_all_spans(&mut self, all_spans: bool) {
self.all_spans = all_spans && self.additional_spans.is_some();
}
pub fn set_span(&mut self, span: Option<Span>) {
self.span = span.filter(|span| !span.is_dummy()).unwrap_or(Span::DUMMY);
self.additional_spans = None;
self.all_spans = false;
self.kind = DebugLocKind::Normal;
}
pub fn set_spans(&mut self, spans: impl IntoIterator<Item = Span>) {
self.set_span(None);
self.insert_spans(spans);
}
#[must_use]
pub const fn modifier_depth(&self) -> u32 {
self.modifier_depth
}
pub fn set_modifier_depth(&mut self, depth: u32) {
self.modifier_depth = depth;
}
#[must_use]
pub const fn kind(&self) -> DebugLocKind {
self.kind
}
pub fn set_kind(&mut self, kind: DebugLocKind) {
self.kind = kind;
}
#[must_use]
pub fn is_gap(&self) -> bool {
self.kind == DebugLocKind::Normal && !self.has_spans()
}
pub fn preserve(&mut self, from: &Self) {
self.clone_from(from);
}
pub fn merge<'a>(&mut self, others: impl IntoIterator<Item = &'a Self>) {
self.all_spans = false;
let mut located = self.has_spans();
let mut unlocated_kind = (!located).then_some(self.kind);
for other in others {
self.vote_depth(other);
if other.has_spans() {
located = true;
if unlocated_kind.is_none() {
self.insert_spans(other.spans());
}
} else {
let kind = unlocated_kind.map_or(other.kind, |kind| kind.merge(other.kind));
unlocated_kind = Some(kind);
}
}
if let Some(kind) = unlocated_kind {
self.set_span(None);
self.kind = if located { DebugLocKind::Unknown } else { kind };
}
}
pub fn combine<'a>(&mut self, others: impl IntoIterator<Item = &'a Self>) {
let mut exact = !self.has_alternatives();
for other in others {
exact &= !other.has_alternatives();
self.vote_depth(other);
self.insert_spans(other.spans());
self.kind =
if self.has_spans() { DebugLocKind::Normal } else { self.kind.merge(other.kind) };
}
self.all_spans = exact && self.additional_spans.is_some();
}
pub fn drop(&mut self, kind: DebugLocKind) {
self.set_span(None);
self.kind = kind;
}
fn has_alternatives(&self) -> bool {
self.additional_spans.is_some() && !self.all_spans
}
fn vote_depth(&mut self, other: &Self) {
if self.modifier_depth != other.modifier_depth {
self.modifier_depth = 0;
}
}
fn insert_spans(&mut self, spans: impl IntoIterator<Item = Span>) {
for span in spans {
if span.is_dummy() || span == self.span {
continue;
}
if self.span.is_dummy() {
self.span = span;
} else {
self.additional_spans.get_or_insert_default().insert(span);
}
}
}
}
#[derive(Clone, Debug, Default)]
struct AdditionalSpans {
spans: DebugSpans,
seen: Option<FxHashSet<Span>>,
}
impl AdditionalSpans {
const SCAN_LIMIT: usize = 16;
fn insert(&mut self, span: Span) {
let is_new = match &mut self.seen {
Some(seen) => seen.insert(span),
None => !self.spans.contains(&span),
};
if !is_new {
return;
}
self.spans.push(span);
if self.seen.is_none() && self.spans.len() > Self::SCAN_LIMIT {
self.seen = Some(self.spans.iter().copied().collect());
}
}
}
impl PartialEq for AdditionalSpans {
fn eq(&self, other: &Self) -> bool {
self.spans == other.spans
}
}
impl Eq for AdditionalSpans {}
impl std::hash::Hash for AdditionalSpans {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.spans.hash(state);
}
}
#[cfg(test)]
mod tests {
use super::*;
use solar_interface::BytePos;
fn span(lo: u32) -> Span {
Span::new(BytePos(lo), BytePos(lo + 1))
}
fn loc(spans: impl IntoIterator<Item = Span>, depth: u32) -> DebugLoc {
let mut loc = DebugLoc::default();
loc.set_spans(spans);
loc.set_modifier_depth(depth);
loc
}
#[test]
fn preserve_copies_everything() {
let from = loc([span(1), span(2)], 3);
let mut to = loc([span(9)], 1);
to.preserve(&from);
assert_eq!(to, from);
}
#[test]
fn merge_keeps_agreeing_depth_and_zeroes_conflicts() {
let mut merged = loc([span(1)], 2);
merged.merge([&loc([span(2)], 2), &loc([span(3)], 2)]);
assert_eq!(merged.modifier_depth(), 2);
merged.merge([&loc([span(4)], 1)]);
assert_eq!(merged.modifier_depth(), 0);
merged.set_modifier_depth(2);
merged.merge([&loc([span(5)], 1), &loc([span(6)], 2)]);
assert_eq!(merged.modifier_depth(), 0);
}
#[test]
fn merge_votes_depth_of_inputs_without_spans() {
let mut merged = loc([], 2);
merged.merge([&loc([], 2)]);
assert_eq!(merged.modifier_depth(), 2);
merged.merge([&DebugLoc::EMPTY]);
assert_eq!(merged.modifier_depth(), 0);
}
#[test]
fn merge_with_an_unlocated_input_has_no_spans() {
let mut merged = loc([span(1), span(2)], 0);
merged.merge([&DebugLoc::EMPTY, &loc([span(3)], 0)]);
assert!(!merged.has_spans());
assert_eq!(merged.kind(), DebugLocKind::Unknown);
let mut unlocated = DebugLoc::EMPTY;
unlocated.merge([&loc([span(1)], 0)]);
assert!(!unlocated.has_spans());
assert_eq!(unlocated.kind(), DebugLocKind::Unknown);
}
#[test]
fn merge_kind_does_not_depend_on_input_order() {
let mut dropped = DebugLoc::EMPTY;
dropped.drop(DebugLocKind::Dropped);
let located = loc([span(1)], 0);
for inputs in [[&dropped, &DebugLoc::EMPTY], [&DebugLoc::EMPTY, &dropped]] {
let mut merged = located.clone();
merged.merge(inputs);
assert_eq!(merged.kind(), DebugLocKind::Unknown);
}
}
#[test]
fn merge_unions_the_spans_of_located_inputs() {
let mut merged = loc([span(1)], 0);
merged.merge([&loc([span(2), span(1)], 0)]);
assert_eq!(merged.spans().collect::<Vec<_>>(), [span(1), span(2)]);
assert_eq!(merged.kind(), DebugLocKind::Normal);
}
#[test]
fn combine_marks_several_spans_as_all_holding() {
let mut combined = loc([span(1)], 0);
combined.combine([&loc([span(1)], 0)]);
assert!(!combined.all_spans());
combined.combine([&loc([span(2)], 0)]);
assert!(combined.all_spans());
assert_eq!(combined.spans().collect::<Vec<_>>(), [span(1), span(2)]);
let mut preserved = DebugLoc::EMPTY;
preserved.preserve(&combined);
assert!(preserved.all_spans());
let mut replaced = combined.clone();
replaced.set_span(Some(span(3)));
assert!(!replaced.all_spans());
}
#[test]
fn combine_over_alternatives_stays_alternatives() {
let alternatives = loc([span(1), span(2)], 0);
let mut combined = loc([span(3)], 0);
combined.combine([&alternatives]);
assert!(!combined.all_spans());
assert_eq!(combined.spans().count(), 3);
}
#[test]
fn merge_clears_the_all_flag_and_keeps_the_union() {
let mut all = loc([span(1)], 0);
all.combine([&loc([span(2)], 0)]);
assert!(all.all_spans());
let mut merged = loc([span(3)], 0);
merged.merge([&all]);
assert!(!merged.all_spans());
assert_eq!(merged.spans().collect::<Vec<_>>(), [span(3), span(1), span(2)]);
all.merge([&loc([span(4)], 0)]);
assert!(!all.all_spans());
}
#[test]
fn combine_unions_spans_and_ignores_unlocated_inputs() {
let mut combined = loc([span(1)], 2);
combined.combine([&DebugLoc::EMPTY.clone(), &loc([span(2)], 2)]);
assert_eq!(combined.spans().collect::<Vec<_>>(), [span(1), span(2)]);
assert_eq!(combined.kind(), DebugLocKind::Normal);
assert_eq!(combined.modifier_depth(), 0);
}
#[test]
fn drop_keeps_depth() {
let mut dropped = loc([span(1), span(2)], 2);
dropped.drop(DebugLocKind::CompilerGenerated);
assert!(!dropped.has_spans());
assert_eq!(dropped.modifier_depth(), 2);
assert_eq!(dropped.kind(), DebugLocKind::CompilerGenerated);
}
#[test]
fn merge_without_spans_merges_kinds() {
let mut merged = DebugLoc::EMPTY;
merged.drop(DebugLocKind::Dropped);
let mut other = DebugLoc::EMPTY;
other.drop(DebugLocKind::CompilerGenerated);
merged.merge([&other]);
assert_eq!(merged.kind(), DebugLocKind::Unknown);
merged.combine([&loc([span(1)], 0)]);
assert_eq!(merged.kind(), DebugLocKind::Normal);
}
}