1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! The types a source declares, as the checker looks members up in them:
//! fields and properties by type, methods by signature, the components an
//! object has, an enum's cases.
use std::collections::HashMap;
use crate::ast::{self, Decl, DeclKind, Method};
/// A declared type's members, still as written (typed when looked up).
#[derive(Default)]
pub(crate) struct OwnType {
pub values: HashMap<String, ast::Type>,
pub methods: HashMap<String, Vec<Method>>,
/// The components an object of it has (`has Charge(…)`), by name.
pub components: Vec<String>,
pub cases: Vec<String>,
/// What it inherits and implements, by the names written (`: Machine, ITickable`).
pub bases: Vec<String>,
/// An `interface`: what classes implement, never one's base class.
pub interface: bool,
/// An `abstract class`: inherited, never made.
pub is_abstract: bool,
/// Its objects live on the heap (a class, a `record`, an interface):
/// never `Copy`.
pub heap: bool,
/// Its `readonly` methods (Rust's `&self`): the others change it.
pub readonly: Vec<String>,
/// A `readonly struct` or a record: every member is.
pub readonly_all: bool,
/// A record: set once, never changed; shared, so `Copy`.
pub record: bool,
/// Its primary constructor's parameters (`class Obsolete(string message)`).
pub ctor: Vec<ast::Param>,
/// Its type parameters' names (`T` of `Box<T>`).
pub generics: Vec<String>,
/// An enum's cases that carry data, their fields by case.
pub variants: HashMap<String, Vec<ast::Param>>,
/// Its indexers (`this[i32 i]`), still as written.
pub indexers: Vec<Indexer>,
}
/// An indexer as written: its getter (`get_Item`, what `x[i]` is typed as
/// a call of), and whether it's set too (`x[i] = v`).
#[derive(Clone)]
pub(crate) struct Indexer {
pub get: Method,
pub settable: bool,
}
/// Every type the source declares, by name; the units it declares.
#[derive(Default)]
pub(crate) struct OwnTypes {
pub types: HashMap<String, OwnType>,
pub units: Vec<String>,
/// The file's own methods (outside types), by name.
pub methods: HashMap<String, Vec<Method>>,
pub values: HashMap<String, ast::Type>,
/// What `extend` adds to the game's types, by their names.
pub extended: HashMap<String, OwnType>,
/// Each type's namespace (`namespace A.B;`), by its name.
pub namespaces: HashMap<String, Vec<String>>,
/// The namespace the declarations being collected are in.
current: Vec<String>,
}
impl OwnTypes {
/// The types of each of `sources` (the library's, then the file's).
pub fn collect(sources: &[&[Decl]]) -> Self {
let mut out = OwnTypes::default();
for decls in sources {
out.current.clear();
out.file(decls);
}
out
}
fn file(&mut self, decls: &[Decl]) {
for d in decls {
match &d.kind {
DeclKind::Method(m) => self
.methods
.entry(m.name.text.clone())
.or_default()
.push(m.clone()),
DeclKind::Field(f) => {
self.values.insert(f.name.text.clone(), f.ty.clone());
}
DeclKind::Property { ty, name, .. } => {
self.values.insert(name.text.clone(), ty.clone());
}
DeclKind::Unit { name, .. } => self.units.push(name.text.clone()),
DeclKind::Namespace { path, body } => {
let path: Vec<String> = path.iter().map(|n| n.text.clone()).collect();
match body {
None => self.current = path,
Some(body) => {
let inner = [self.current.clone(), path].concat();
let outer = std::mem::replace(&mut self.current, inner);
self.file(body);
self.current = outer;
}
}
}
_ => self.type_decl(d),
}
}
}
fn type_decl(&mut self, d: &Decl) {
let (name, body, has, ctor) = match &d.kind {
DeclKind::Type {
name,
header,
body,
word,
value,
} => {
let t = self.types.entry(name.text.clone()).or_default();
t.bases = header
.bases
.iter()
.filter_map(|b| super::written::simple_path(b).map(str::to_string))
.collect();
t.interface = word.text == crate::words::interface::WORD.text;
t.is_abstract = d.has(ast::Modifier::Abstract);
t.heap = !value;
t.record = word.text == crate::words::record::WORD.text;
t.readonly_all = d.has(ast::Modifier::Readonly) || t.record;
t.generics = type_params(&header.generics);
t.ctor = header.ctor.clone().unwrap_or_default();
// A record's constructor parameters are its properties.
let props = (word.text == crate::words::record::WORD.text)
.then_some(header.ctor.as_deref())
.flatten();
(name, body.as_slice(), header.has.as_slice(), props)
}
DeclKind::Content {
name,
header,
body,
kind,
} if kind.text != yog_ids::kind::RECIPE => {
(name, body.as_slice(), header.has.as_slice(), None)
}
DeclKind::Component { name, header, body }
| DeclKind::Screen {
name, header, body, ..
} => (name, body.as_slice(), header.has.as_slice(), None),
// The mod is an object too: its members, its constructor.
DeclKind::Mod { name, body } => (name, body.as_slice(), &[][..], None),
DeclKind::Enum {
name,
generics,
variants,
body,
..
} => {
self.namespaces
.insert(name.text.clone(), self.current.clone());
let t = self.types.entry(name.text.clone()).or_default();
t.cases = variants.iter().map(|v| v.name.text.clone()).collect();
t.generics = type_params(generics);
t.variants = variants
.iter()
.filter(|v| !v.fields.is_empty())
.map(|v| (v.name.text.clone(), v.fields.clone()))
.collect();
members(t, body);
return;
}
_ => return,
};
self.namespaces
.insert(name.text.clone(), self.current.clone());
let t = self.types.entry(name.text.clone()).or_default();
for p in ctor.into_iter().flatten() {
t.values.insert(p.name.text.clone(), p.ty.clone());
}
t.components = has.iter().filter_map(component_name).collect();
members(t, body);
}
/// `name` and every type it inherits or implements, nearest first,
/// each once (a cycle doesn't loop).
pub fn lineage(&self, name: &str) -> Vec<String> {
let mut out = vec![name.to_string()];
let mut i = 0;
while let Some(at) = out.get(i) {
let bases = self
.types
.get(at)
.map(|t| t.bases.clone())
.unwrap_or_default();
for b in bases {
if !out.contains(&b) {
out.push(b);
}
}
i += 1;
}
out
}
/// Whether own type `a` is `b`, inherits it or implements it.
pub fn is_a(&self, a: &str, b: &str) -> bool {
self.lineage(a).iter().any(|t| t == b)
}
/// The class `name` inherits (not an interface it implements).
pub fn base_class(&self, name: &str) -> Option<&str> {
let t = self.types.get(name)?;
t.bases
.iter()
.find(|b| self.types.get(*b).is_some_and(|o| !o.interface))
.map(String::as_str)
}
/// `extend T { … }`'s members and components: added to `T` when it's
/// the source's own, kept beside the game's members when it's the game's.
pub fn extensions(&mut self, decls: &[Decl]) {
for d in decls {
if let DeclKind::Extend { ty, header, body } = &d.kind
&& let Some(name) = super::written::simple_path(ty)
{
let t = match self.types.get_mut(name) {
Some(own) => own,
None => self.extended.entry(name.to_string()).or_default(),
};
t.components
.extend(header.has.iter().filter_map(component_name));
members(t, body);
}
}
}
}
/// The values and methods of a body.
fn members(t: &mut OwnType, body: &[Decl]) {
for d in body {
match &d.kind {
DeclKind::Field(f) => {
t.values.insert(f.name.text.clone(), f.ty.clone());
}
DeclKind::Property { ty, name, .. } => {
t.values.insert(name.text.clone(), ty.clone());
}
DeclKind::Event { ty, name } => {
t.values.insert(name.text.clone(), ty.clone());
}
DeclKind::Indexer(ix) => {
if let Some(get) = ix.getter() {
t.indexers.push(Indexer {
get,
settable: ix.setter().is_some(),
});
}
}
DeclKind::Method(m) => {
if d.has(ast::Modifier::Readonly) {
t.readonly.push(m.name.text.clone());
}
t.methods
.entry(m.name.text.clone())
.or_default()
.push(m.clone());
}
_ => {}
}
}
}
/// The names of a declaration's type parameters (lifetimes aside).
fn type_params(generics: &[ast::Generic]) -> Vec<String> {
generics
.iter()
.filter(|g| !g.lifetime)
.map(|g| g.name.text.clone())
.collect()
}
/// `Charge(1000)` or `Charge`: the component's name.
fn component_name(e: &crate::ast::Expr) -> Option<String> {
use crate::ast::ExprKind;
match &e.kind {
ExprKind::Ident(n) => Some(n.clone()),
ExprKind::Call { callee, .. } => component_name(callee),
ExprKind::Member { name, .. } => Some(name.text.clone()),
_ => None,
}
}