use std::collections::{BTreeMap, BTreeSet};
use crate::ast::AstSyntheticLocalId;
use crate::hir::{HirProtoRef, LocalId, ParamId, TempId, UpvalueId};
use strum_macros::{Display, EnumString, IntoStaticStr};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Display, EnumString, IntoStaticStr)]
#[strum(serialize_all = "kebab-case")]
pub enum NamingMode {
DebugLike,
#[default]
Simple,
Heuristic,
}
#[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, Display, IntoStaticStr)]
#[strum(serialize_all = "kebab-case")]
pub enum NameSource {
Debug,
CaptureProvenance,
SelfParam,
LoopRole,
FieldName,
TableShape,
BoolShape,
FunctionShape,
ResultShape,
Discard,
DebugLike,
Simple,
ConflictFallback,
}
#[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>,
}