Skip to main content

code_moniker_core/lang/sdk/
model.rs

1use std::collections::BTreeMap;
2
3use crate::core::code_graph::Position;
4use crate::core::moniker::Moniker;
5
6use super::scope::{Namespace, ScopeId, ScopeTree};
7
8#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
9pub struct DefNameKey {
10	pub namespace: Namespace,
11	pub name: Vec<u8>,
12}
13
14impl DefNameKey {
15	pub fn new(namespace: Namespace, name: impl Into<Vec<u8>>) -> Self {
16		Self {
17			namespace,
18			name: name.into(),
19		}
20	}
21}
22
23#[derive(Clone, Debug, Eq, PartialEq)]
24pub struct DiscoveredDef {
25	pub moniker: Moniker,
26	pub parent: Moniker,
27	pub namespace: Namespace,
28	pub name: Vec<u8>,
29	pub kind: &'static [u8],
30	pub visibility: &'static [u8],
31	pub signature: Vec<u8>,
32	pub position: Option<Position>,
33	pub call_name: Vec<u8>,
34	pub call_arity: Option<usize>,
35}
36
37impl DiscoveredDef {
38	pub fn key(&self) -> DefNameKey {
39		DefNameKey::new(self.namespace, self.name.clone())
40	}
41}
42
43#[derive(Clone, Debug, Default, Eq, PartialEq)]
44pub struct DefIndex {
45	by_moniker: BTreeMap<Moniker, DiscoveredDef>,
46	by_name: BTreeMap<DefNameKey, Vec<Moniker>>,
47}
48
49impl DefIndex {
50	pub fn from_defs(defs: &[DiscoveredDef]) -> Self {
51		let mut index = Self::default();
52		for def in defs {
53			index.insert(def.clone());
54		}
55		index
56	}
57
58	pub fn insert(&mut self, def: DiscoveredDef) {
59		self.by_name
60			.entry(def.key())
61			.or_default()
62			.push(def.moniker.clone());
63		self.by_moniker.insert(def.moniker.clone(), def);
64	}
65
66	pub fn contains(&self, moniker: &Moniker) -> bool {
67		self.by_moniker.contains_key(moniker)
68	}
69
70	pub fn get(&self, moniker: &Moniker) -> Option<&DiscoveredDef> {
71		self.by_moniker.get(moniker)
72	}
73
74	pub fn by_name(&self, namespace: Namespace, name: &[u8]) -> &[Moniker] {
75		self.by_name
76			.get(&DefNameKey::new(namespace, name.to_vec()))
77			.map(Vec::as_slice)
78			.unwrap_or_default()
79	}
80}
81
82#[derive(Clone, Debug, Eq, PartialEq)]
83pub enum ImportKind {
84	Symbol,
85	Module,
86	Wildcard,
87	Alias,
88	Reexport,
89}
90
91#[derive(Clone, Debug, Eq, PartialEq)]
92pub struct ImportTarget {
93	pub kind: ImportKind,
94	pub namespace: Namespace,
95	pub alias: Vec<u8>,
96	pub target: Moniker,
97	pub confidence: &'static [u8],
98}
99
100#[derive(Clone, Debug, Default, Eq, PartialEq)]
101pub struct ImportTable {
102	by_scope: BTreeMap<ScopeId, Vec<ImportTarget>>,
103}
104
105impl ImportTable {
106	pub fn insert(&mut self, scope: ScopeId, target: ImportTarget) {
107		self.by_scope.entry(scope).or_default().push(target);
108	}
109
110	pub fn scoped(&self, scope: ScopeId) -> &[ImportTarget] {
111		self.by_scope
112			.get(&scope)
113			.map(Vec::as_slice)
114			.unwrap_or_default()
115	}
116}
117
118#[derive(Clone, Debug, Eq, PartialEq)]
119pub struct DiscoveredFile {
120	pub root: Moniker,
121	pub root_kind: &'static [u8],
122	pub defs: Vec<DiscoveredDef>,
123	pub def_index: DefIndex,
124	pub scopes: ScopeTree,
125	pub imports: ImportTable,
126}
127
128impl DiscoveredFile {
129	pub fn new(
130		root: Moniker,
131		root_kind: &'static [u8],
132		defs: Vec<DiscoveredDef>,
133		scopes: ScopeTree,
134		imports: ImportTable,
135	) -> Self {
136		let def_index = DefIndex::from_defs(&defs);
137		Self {
138			root,
139			root_kind,
140			defs,
141			def_index,
142			scopes,
143			imports,
144		}
145	}
146}
147
148#[derive(Clone, Debug, Eq, PartialEq)]
149pub enum TargetExpr {
150	Bare(Vec<u8>),
151	Path(Vec<Vec<u8>>),
152	Receiver {
153		receiver: Box<TargetExpr>,
154		name: Vec<u8>,
155	},
156	SelfType(Vec<u8>),
157	External {
158		package: Vec<u8>,
159		path: Vec<Vec<u8>>,
160	},
161}
162
163#[derive(Clone, Debug, Default, Eq, PartialEq)]
164pub struct RefHints {
165	pub receiver_hint: Vec<u8>,
166	pub alias: Vec<u8>,
167	pub namespace: Option<Namespace>,
168	pub call_name: Vec<u8>,
169	pub call_arity: Option<usize>,
170}
171
172#[derive(Clone, Debug, Eq, PartialEq)]
173pub struct UnresolvedRef {
174	pub source: Moniker,
175	pub kind: &'static [u8],
176	pub source_scope: ScopeId,
177	pub position: Option<Position>,
178	pub target: TargetExpr,
179	pub hints: RefHints,
180}
181
182#[derive(Clone, Debug, Eq, PartialEq)]
183pub struct ResolvedRef {
184	pub source: Moniker,
185	pub target: Moniker,
186	pub kind: &'static [u8],
187	pub position: Option<Position>,
188	pub confidence: &'static [u8],
189	pub hints: RefHints,
190}