use std::collections::{BTreeMap, BTreeSet};
use std::str::FromStr;
use crate::ast::AstSyntheticLocalId;
use crate::hir::{HirProtoRef, LocalId, ParamId, TempId, UpvalueId};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum NamingMode {
DebugLike,
#[default]
Simple,
Heuristic,
}
impl NamingMode {
pub const fn as_str(self) -> &'static str {
match self {
Self::DebugLike => "debug-like",
Self::Simple => "simple",
Self::Heuristic => "heuristic",
}
}
}
impl FromStr for NamingMode {
type Err = ();
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"debug-like" => Ok(Self::DebugLike),
"simple" => Ok(Self::Simple),
"heuristic" => Ok(Self::Heuristic),
_ => Err(()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NamingOptions {
pub mode: NamingMode,
pub debug_like_include_function: bool,
}
impl Default for NamingOptions {
fn default() -> Self {
Self {
mode: NamingMode::Simple,
debug_like_include_function: true,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NameSource {
Debug,
CaptureProvenance,
SelfParam,
LoopRole,
FieldName,
TableShape,
BoolShape,
FunctionShape,
ResultShape,
Discard,
DebugLike,
Simple,
ConflictFallback,
}
impl NameSource {
pub const fn as_str(self) -> &'static str {
match self {
Self::Debug => "debug",
Self::CaptureProvenance => "capture-provenance",
Self::SelfParam => "self-param",
Self::LoopRole => "loop-role",
Self::FieldName => "field-name",
Self::TableShape => "table-shape",
Self::BoolShape => "bool-shape",
Self::FunctionShape => "function-shape",
Self::ResultShape => "result-shape",
Self::Discard => "discard",
Self::DebugLike => "debug-like",
Self::Simple => "simple",
Self::ConflictFallback => "conflict-fallback",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NameInfo {
pub text: String,
pub source: NameSource,
pub renamed: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct FunctionNameMap {
pub params: Vec<NameInfo>,
pub locals: Vec<NameInfo>,
pub synthetic_locals: BTreeMap<AstSyntheticLocalId, NameInfo>,
pub upvalues: Vec<NameInfo>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct NameMap {
pub entry_function: HirProtoRef,
pub mode: NamingMode,
pub functions: Vec<FunctionNameMap>,
}
impl NameMap {
pub fn function(&self, function: HirProtoRef) -> Option<&FunctionNameMap> {
self.functions.get(function.index())
}
}
#[derive(Debug, Clone, Default)]
pub struct NamingEvidence {
pub(super) functions: Vec<FunctionNamingEvidence>,
}
#[derive(Debug, Clone, Default)]
pub(super) struct FunctionNamingEvidence {
pub(super) param_debug_names: Vec<Option<String>>,
pub(super) local_debug_names: Vec<Option<String>>,
pub(super) upvalue_debug_names: Vec<Option<String>>,
pub(super) upvalue_capture_sources: Vec<Option<CapturedBinding>>,
pub(super) temp_debug_names: Vec<Option<String>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum CapturedBinding {
Param {
parent: HirProtoRef,
param: ParamId,
},
Local {
parent: HirProtoRef,
local: LocalId,
},
Temp {
parent: HirProtoRef,
temp: TempId,
},
Upvalue {
parent: HirProtoRef,
upvalue: UpvalueId,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ClosureCaptureEvidence {
pub(super) parent: HirProtoRef,
pub(super) captures: Vec<Option<CapturedBinding>>,
}
#[derive(Debug, Clone, Default)]
pub(super) struct FunctionHints {
pub(super) param_hints: BTreeMap<ParamId, CandidateHint>,
pub(super) local_hints: BTreeMap<LocalId, CandidateHint>,
pub(super) synthetic_locals: BTreeSet<AstSyntheticLocalId>,
pub(super) synthetic_local_hints: BTreeMap<AstSyntheticLocalId, CandidateHint>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct CandidateHint {
pub(super) text: String,
pub(super) source: NameSource,
}
#[derive(Debug, Clone, Copy, Default)]
pub(super) struct LoopContext {
pub(super) numeric_depth: usize,
}
#[derive(Debug, Default)]
pub(super) struct ModuleNameAllocator {
pub(super) function_shape_names: BTreeSet<String>,
pub(super) next_function_shape_suffix: BTreeMap<String, usize>,
}