more wrestling
This commit is contained in:
+2
-158
@@ -1,6 +1,6 @@
|
||||
//! all main IR Upper data structures stored in UProgram
|
||||
|
||||
use super::{FnInst, ResErr, StructInst, Type, UInstrInst, UInstruction, UProgram};
|
||||
use super::*;
|
||||
use crate::{
|
||||
common::FileSpan,
|
||||
ir::{Len, ID},
|
||||
@@ -10,29 +10,6 @@ use std::{
|
||||
fmt::{Debug, Display},
|
||||
};
|
||||
|
||||
pub trait ResStage {
|
||||
type Var;
|
||||
type Func;
|
||||
type Struct;
|
||||
type Type;
|
||||
}
|
||||
|
||||
pub struct Unresolved;
|
||||
impl ResStage for Unresolved {
|
||||
type Var = IdentID;
|
||||
type Func = IdentID;
|
||||
type Struct = IdentID;
|
||||
type Type = IdentID;
|
||||
}
|
||||
|
||||
pub struct Resolved;
|
||||
impl ResStage for Resolved {
|
||||
type Var = VarID;
|
||||
type Func = FnInst;
|
||||
type Struct = StructInst;
|
||||
type Type = TypeID;
|
||||
}
|
||||
|
||||
pub type NamePath = Vec<String>;
|
||||
|
||||
pub type FnID = ID<UFunc>;
|
||||
@@ -79,55 +56,6 @@ pub struct UVar {
|
||||
pub children: HashMap<String, VarID>,
|
||||
}
|
||||
|
||||
/// a generic identifier for all (identifiable) kinds
|
||||
/// eg. a::b::c.d.e
|
||||
/// or a::Result<T,_>
|
||||
pub struct UIdent {
|
||||
pub status: IdentStatus,
|
||||
pub origin: Origin,
|
||||
}
|
||||
|
||||
pub enum IdentStatus {
|
||||
Res(Res),
|
||||
Unres {
|
||||
base: ResBase,
|
||||
path: Vec<MemberIdent>,
|
||||
},
|
||||
Failed(Option<ResErr>),
|
||||
Cooked,
|
||||
}
|
||||
|
||||
pub struct MemberIdent {
|
||||
pub ty: MemberTy,
|
||||
pub name: String,
|
||||
pub gargs: Vec<TypeID>,
|
||||
pub origin: Origin,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum MemberTy {
|
||||
Member,
|
||||
Field,
|
||||
}
|
||||
|
||||
impl MemberTy {
|
||||
pub fn sep(&self) -> &'static str {
|
||||
match self {
|
||||
MemberTy::Member => "::",
|
||||
MemberTy::Field => ".",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for MemberTy {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(match self {
|
||||
MemberTy::Member => "member",
|
||||
MemberTy::Field => "field",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
||||
pub struct VarOffset {
|
||||
pub id: VarID,
|
||||
@@ -186,7 +114,7 @@ impl MemberID {
|
||||
MemberID::Fn(id) => &p.fns[id].name,
|
||||
MemberID::Struct(id) => &p.structs[id].name,
|
||||
MemberID::Module(id) => &p.modules[id].name,
|
||||
MemberID::Type(id) => &p.type_name(id),
|
||||
MemberID::Type(def) => &p.type_name(def.ty),
|
||||
};
|
||||
format!("{} '{}'", self.kind(), name)
|
||||
}
|
||||
@@ -218,90 +146,6 @@ impl Display for KindTy {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Res {
|
||||
Var(VarID),
|
||||
Fn(FnInst),
|
||||
Struct(StructInst),
|
||||
Type(TypeID),
|
||||
Generic(GenericID),
|
||||
Module(ModID),
|
||||
}
|
||||
|
||||
impl Res {
|
||||
pub fn kind(&self) -> KindTy {
|
||||
match self {
|
||||
Res::Var(..) => KindTy::Var,
|
||||
Res::Fn(..) => KindTy::Fn,
|
||||
Res::Struct(..) => KindTy::Struct,
|
||||
Res::Type(..) => KindTy::Type,
|
||||
Res::Module(..) => KindTy::Module,
|
||||
Res::Generic(..) => KindTy::Generic,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn display_str(&self, p: &UProgram) -> String {
|
||||
let name = match self {
|
||||
Res::Var(id) => &p.vars[id].name,
|
||||
Res::Fn(fi) => &p.fns[fi.id].name,
|
||||
Res::Struct(si) => &p.structs[si.id].name,
|
||||
Res::Type(id) => &p.type_name(id),
|
||||
Res::Generic(id) => &p.generics[id].name,
|
||||
Res::Module(id) => &p.modules[id].name,
|
||||
};
|
||||
format!("{} '{}'", self.kind(), name)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum ResBase {
|
||||
Unvalidated(MemRes),
|
||||
Validated(Res),
|
||||
}
|
||||
|
||||
impl ResBase {
|
||||
pub fn display_str(&self, p: &UProgram) -> String {
|
||||
match self {
|
||||
ResBase::Unvalidated(uv) => uv.display_str(p),
|
||||
ResBase::Validated(res) => res.display_str(p),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MemRes {
|
||||
pub mem: Member,
|
||||
pub origin: Origin,
|
||||
pub gargs: Vec<TypeID>,
|
||||
}
|
||||
|
||||
impl MemRes {
|
||||
pub fn display_str(&self, p: &UProgram) -> String {
|
||||
self.mem.id.display_str(p)
|
||||
}
|
||||
}
|
||||
|
||||
impl IdentID {
|
||||
pub fn var(&self, p: &UProgram) -> Option<VarID> {
|
||||
match p.idents[self].status {
|
||||
IdentStatus::Res(Res::Var(id)) => Some(id),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn fun<'a>(&self, p: &'a UProgram) -> Option<&'a FnInst> {
|
||||
match &p.idents[self].status {
|
||||
IdentStatus::Res(Res::Fn(i)) => Some(&i),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn struc<'a>(&self, p: &'a UProgram) -> Option<&'a StructInst> {
|
||||
match &p.idents[self].status {
|
||||
IdentStatus::Res(Res::Struct(i)) => Some(&i),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UFunc {
|
||||
pub fn flat_iter(&self) -> impl Iterator<Item = &UInstrInst> {
|
||||
InstrIter::new(self.instructions.iter())
|
||||
|
||||
Reference in New Issue
Block a user