Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ndc_analyser/src/name_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ pub(crate) struct NameResolver {
type_names: AHashMap<NodeId, SymbolId>,
/// What each struct declaration binds, keyed by the declaration's node.
structs: AHashMap<NodeId, StructSymbols>,
/// Each native's position in the function registry, which is also its
/// global slot in the VM. With `new(["len", "+"])`, `len` is 0 and `+` is 1.
#[cfg_attr(
not(test),
expect(
dead_code,
reason = "read by the layout pass, which is not written yet"
)
)]
native_slots: AHashMap<SymbolId, usize>,
/// Errors from statements that failed to resolve. Resolution carries on with
/// the next statement, so `a; b;` reports both unknown names.
errors: Vec<NameResolutionError>,
Expand Down Expand Up @@ -298,9 +308,11 @@ impl NameResolver {
let mut scopes = ScopeTree::new();
let mut symbols = SymbolTable::default();

for builtin in builtins {
let mut native_slots = AHashMap::default();
for (slot, builtin) in builtins.into_iter().enumerate() {
let symbol = symbols.declare(builtin.to_string(), BindingKind::Function);
scopes.bind(symbol);
native_slots.insert(symbol, slot);
}
for name in StaticType::BUILTIN_TYPE_NAMES {
let symbol = symbols.declare((*name).to_string(), BindingKind::Type);
Expand All @@ -321,6 +333,7 @@ impl NameResolver {
setters: Default::default(),
type_names: Default::default(),
structs: Default::default(),
native_slots,
errors: Default::default(),
}
}
Expand Down
18 changes: 18 additions & 0 deletions ndc_analyser/src/name_resolution/resolver_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,3 +1209,21 @@ fn statements_after_an_error_still_resolve() {
assert_eq!(uses(&resolver, x), 1);
assert!(resolver.take_errors().is_empty());
}

#[test]
fn natives_record_their_registry_slot() {
let mut resolver = NameResolver::new(["len", "+", "len"]);
resolve_source(&mut resolver, "let x = 1; len;").unwrap();

// Overloads of one name keep their own slots, in registration order.
let [newest_len, oldest_len] = only_reference(&resolver).0[0][..] else {
panic!("expected both `len` overloads");
};
let plus = builtin(&resolver, "+");
assert_eq!(resolver.native_slots[&oldest_len], 0);
assert_eq!(resolver.native_slots[&plus], 1);
assert_eq!(resolver.native_slots[&newest_len], 2);
// Program symbols aren't natives.
let [x] = declared(&resolver);
assert!(!resolver.native_slots.contains_key(&x));
}
Loading