Skip to content

Commit fd4d5e8

Browse files
committed
feat: added HIR scope append functions
1 parent c61815f commit fd4d5e8

4 files changed

Lines changed: 106 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/astoir_hir/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ edition = "2024"
77
compiler_utils = { path = "../compiler_utils" }
88
diagnostics = { path = "../diagnostics" }
99
compiler_typing = { path = "../compiler_typing" }
10+
compiler_global_scope = { path = "../compiler_global_scope" }
1011
lexer = { path = "../lexer" }

compiler/astoir_hir/src/scope.rs

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! HIR version of the global scope in order to store descriptors and implementations
22
3-
use compiler_typing::TypedGlobalScope;
3+
use compiler_global_scope::key::EntryKey;
4+
use compiler_typing::{TypedGlobalScope, TypedGlobalScopeEntry, raw::RawType, tree::Type};
5+
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin};
46

57
use crate::{ctx::HIRFunction, nodes::HIRNode};
68

@@ -11,3 +13,102 @@ pub struct HIRGlobalScopeStorage {
1113
pub descriptors: Vec<HIRFunction>,
1214
pub implementations: Vec<Box<HIRNode>>,
1315
}
16+
17+
impl HIRGlobalScopeStorage {
18+
pub fn new() -> Self {
19+
HIRGlobalScopeStorage {
20+
scope: TypedGlobalScope::new(),
21+
descriptors: vec![],
22+
implementations: vec![],
23+
}
24+
}
25+
26+
/// This doesn't automatically handle descriptors and implementations
27+
pub fn append<K: DiagnosticSpanOrigin>(
28+
&mut self,
29+
name: EntryKey,
30+
entry: TypedGlobalScopeEntry,
31+
origin: &K,
32+
) -> DiagnosticResult<usize> {
33+
self.scope.append(name, entry, origin)
34+
}
35+
36+
pub fn append_func<K: DiagnosticSpanOrigin>(
37+
&mut self,
38+
name: EntryKey,
39+
descriptor: HIRFunction,
40+
implementation: Box<HIRNode>,
41+
origin: &K,
42+
) -> DiagnosticResult<usize> {
43+
self.descriptors.push(descriptor);
44+
self.implementations.push(implementation);
45+
46+
self.scope.append(
47+
name,
48+
TypedGlobalScopeEntry::Function {
49+
descriptor_ind: self.scope.descriptor_counter,
50+
impl_ind: self.scope.impl_counter,
51+
},
52+
origin,
53+
)
54+
}
55+
56+
pub fn append_implless_function<K: DiagnosticSpanOrigin>(
57+
&mut self,
58+
name: EntryKey,
59+
descriptor: HIRFunction,
60+
origin: &K,
61+
) -> DiagnosticResult<usize> {
62+
self.descriptors.push(descriptor);
63+
64+
self.scope.append(
65+
name,
66+
TypedGlobalScopeEntry::ImplLessFunction(self.scope.descriptor_counter),
67+
origin,
68+
)
69+
}
70+
71+
pub fn append_struct_function<K: DiagnosticSpanOrigin>(
72+
&mut self,
73+
name: EntryKey,
74+
descriptor: HIRFunction,
75+
implementation: Box<HIRNode>,
76+
struct_type: RawType,
77+
origin: &K,
78+
) -> DiagnosticResult<usize> {
79+
self.descriptors.push(descriptor);
80+
self.implementations.push(implementation);
81+
82+
let ind = self.scope.value_to_ind[&TypedGlobalScopeEntry::Type(struct_type)];
83+
84+
self.scope.append(
85+
name,
86+
TypedGlobalScopeEntry::StructFunction {
87+
descriptor_ind: self.scope.descriptor_counter,
88+
impl_ind: self.scope.impl_counter,
89+
struct_type: ind,
90+
},
91+
origin,
92+
)
93+
}
94+
95+
pub fn append_type<K: DiagnosticSpanOrigin>(
96+
&mut self,
97+
name: EntryKey,
98+
t: RawType,
99+
origin: &K,
100+
) -> DiagnosticResult<usize> {
101+
self.scope
102+
.append(name, TypedGlobalScopeEntry::Type(t), origin)
103+
}
104+
105+
pub fn append_type_binding<K: DiagnosticSpanOrigin>(
106+
&mut self,
107+
name: EntryKey,
108+
t: Type,
109+
origin: &K,
110+
) -> DiagnosticResult<usize> {
111+
self.scope
112+
.append(name, TypedGlobalScopeEntry::TypeAlias(t), origin)
113+
}
114+
}

compiler/compiler_typing/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::collections::HashMap;
44

5-
use compiler_global_scope::GlobalScopeStorage;
5+
use compiler_global_scope::{GlobalScopeStorage, entry::GlobalStorageEntryType};
66
use compiler_utils::hash::HashedString;
77
use diagnostics::DiagnosticResult;
88

@@ -18,6 +18,7 @@ pub mod tree;
1818
pub mod utils;
1919

2020
pub type TypedGlobalScope = GlobalScopeStorage<Type, RawType>;
21+
pub type TypedGlobalScopeEntry = GlobalStorageEntryType<Type, RawType>;
2122

2223
/// A function contained within a type.
2324
pub type TypedFunction = (Vec<(u64, TypeReference)>, Option<TypeReference>);

0 commit comments

Comments
 (0)