//! all main IR Upper data structures stored in UProgram use super::*; use crate::{ common::FileSpan, ir::{Len, ID}, }; use std::{ collections::HashMap, fmt::{Debug, Display}, }; pub type NamePath = Vec; pub type FnID = ID; pub type VarID = ID; pub type IdentID = ID; pub type TypeID = ID; pub type GenericID = ID; pub type StructID = ID; pub type DataID = ID; pub type ModID = ID; pub type InstrID = ID; pub type VarRes = URes; pub type TypeRes = URes; pub struct UFunc { pub name: String, pub origin: Origin, pub args: Vec, pub gargs: Vec, pub ret: TypeRes, pub instructions: Vec, } pub struct StructField { pub ty: TypeRes, pub origin: Origin, // pub vis: Visibility } pub struct UStruct { pub name: String, pub origin: Origin, pub fields: HashMap, pub gargs: Vec, } pub struct UGeneric { pub name: String, pub origin: Origin, } pub struct UVar { pub name: String, pub origin: Origin, pub ty: TypeRes, pub parent: Option, pub children: HashMap, } pub enum VarTy { Ident(IdentID), Res(TypeID), } #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] pub struct VarOffset { pub id: VarID, pub offset: Len, } #[derive(Clone)] pub struct UData { pub name: String, pub ty: TypeID, pub content: Vec, } #[derive(Clone)] pub struct UModule { pub name: String, pub members: HashMap, pub parent: Option, pub func: FnID, } #[derive(Clone)] pub struct Member { pub id: MemberID, // pub visibility: Visibility } #[derive(Clone)] pub enum MemberID { Fn(FnID), Struct(StructID), Var(VarID), Module(ModID), Type(TypeDef), } #[derive(Clone)] pub struct TypeDef { pub gargs: Vec, pub ty: TypeID, } impl MemberID { pub fn kind(&self) -> KindTy { match self { MemberID::Fn(_) => KindTy::Fn, MemberID::Struct(_) => KindTy::Struct, MemberID::Var(_) => KindTy::Var, MemberID::Module(_) => KindTy::Module, MemberID::Type(_) => KindTy::Type, } } pub fn display_str(&self, p: &UProgram) -> String { let name = match self { MemberID::Var(id) => &p.vars[id].name, MemberID::Fn(id) => &p.fns[id].name, MemberID::Struct(id) => &p.structs[id].name, MemberID::Module(id) => &p.modules[id].name, MemberID::Type(def) => &p.type_name(def.ty), }; format!("{} '{}'", self.kind(), name) } } pub enum URes { Res(T), Unres(IdentID), } pub type Origin = FileSpan; // "effective" (externally visible) kinds #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum KindTy { Type, Var, Struct, Fn, Module, Generic, } impl Display for KindTy { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { KindTy::Type => "type", KindTy::Var => "variable", KindTy::Fn => "function", KindTy::Struct => "struct", KindTy::Module => "module", KindTy::Generic => "generic", }) } }