Skip to content

Commit d4a54a0

Browse files
committed
feat: added getters for function ctx
1 parent 1a77fbc commit d4a54a0

6 files changed

Lines changed: 80 additions & 9 deletions

File tree

compiler/astoir_hir/src/ctx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use diagnostics::{
1313
use crate::{nodes::HIRNode, scope::HIRGlobalScopeStorage};
1414

1515
pub type HIRFunction = (Option<Type>, Vec<(u64, Type)>, String);
16-
pub type HIRFunctionImpl = (HIRBranchedContext, Box<HIRNode>);
16+
pub type HIRFunctionImpl = Box<HIRNode>;
1717

1818
/// The function HIR context. Contains a mapping from element name hash to element index and other variable information.
1919
/// Uses a branch based system to contain variables.

compiler/astoir_hir/src/scope.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct HIRGlobalScopeStorage {
1616
pub scope: TypedGlobalScope,
1717
pub descriptors: Vec<HIRFunction>,
1818
pub implementations: Vec<HIRFunctionImpl>,
19+
pub contexts: Vec<HIRBranchedContext>,
1920
}
2021

2122
impl HIRGlobalScopeStorage {
@@ -24,6 +25,7 @@ impl HIRGlobalScopeStorage {
2425
scope: TypedGlobalScope::new(),
2526
descriptors: vec![],
2627
implementations: vec![],
28+
contexts: vec![],
2729
}
2830
}
2931

@@ -46,7 +48,8 @@ impl HIRGlobalScopeStorage {
4648
origin: &K,
4749
) -> DiagnosticResult<usize> {
4850
self.descriptors.push(descriptor);
49-
self.implementations.push((brctx, implementation));
51+
self.implementations.push(implementation);
52+
self.contexts.push(brctx);
5053

5154
self.scope.append(
5255
name,
@@ -83,7 +86,8 @@ impl HIRGlobalScopeStorage {
8386
origin: &K,
8487
) -> DiagnosticResult<usize> {
8588
self.descriptors.push(descriptor);
86-
self.implementations.push((brctx, implementation));
89+
self.implementations.push(implementation);
90+
self.contexts.push(brctx);
8791

8892
let ind = self.scope.value_to_ind[&TypedGlobalScopeEntry::Type(struct_type)];
8993

@@ -164,14 +168,27 @@ impl HIRGlobalScopeStorage {
164168
return Ok(self.descriptors[ind].clone());
165169
}
166170

171+
pub fn get_function_ctx<K: DiagnosticSpanOrigin>(
172+
&self,
173+
name: EntryKey,
174+
origin: &K,
175+
) -> DiagnosticResult<HIRBranchedContext> {
176+
let ind = self.scope.get_function_ctx(name, origin)?;
177+
178+
return Ok(self.contexts[ind].clone());
179+
}
180+
167181
pub fn get_function_impl<K: DiagnosticSpanOrigin>(
168182
&self,
169183
name: EntryKey,
170184
origin: &K,
171-
) -> DiagnosticResult<HIRFunctionImpl> {
185+
) -> DiagnosticResult<(HIRFunctionImpl, HIRBranchedContext)> {
172186
let ind = self.scope.get_function_impl(name, origin)?;
173187

174-
return Ok(self.implementations[ind].clone());
188+
return Ok((
189+
self.implementations[ind].clone(),
190+
self.contexts[ind].clone(),
191+
));
175192
}
176193

177194
pub fn get_implless_function<K: DiagnosticSpanOrigin>(
@@ -201,12 +218,13 @@ impl HIRGlobalScopeStorage {
201218
&self,
202219
name: EntryKey,
203220
origin: &K,
204-
) -> DiagnosticResult<(HIRFunction, HIRFunctionImpl, RawType)> {
221+
) -> DiagnosticResult<(HIRFunction, HIRFunctionImpl, HIRBranchedContext, RawType)> {
205222
let res = self.scope.get_exact_struct_function(name, origin)?;
206223

207224
return Ok((
208225
self.descriptors[res.0].clone(),
209226
self.implementations[res.1].clone(),
227+
self.contexts[res.1].clone(),
210228
res.2,
211229
));
212230
}

compiler/astoir_mir_lowering/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use astoir_hir::{
66
};
77
use astoir_mir::{ctx::MIRContext, funcs::MIRFunction};
88
use compiler_typing::{
9-
SizedType, TypedGlobalScopeEntry, raw::RawType, structs::LoweredStructTypeContainer, tree::Type,
9+
SizedType, TypedGlobalScope, TypedGlobalScopeEntry, raw::RawType,
10+
structs::LoweredStructTypeContainer, tree::Type,
1011
};
1112
use compiler_utils::utils::indexed::IndexStorage;
1213
use diagnostics::{DiagnosticResult, unsure_panic};
@@ -68,6 +69,9 @@ pub fn lower_hir(ctx: HIRContext) -> DiagnosticResult<MIRContext> {
6869
lower_hir_top_level(node, &mut lowering_ctx)?;
6970
}
7071

72+
TypedGlobalScopeEntry::TypeAlias(_) => continue,
73+
TypedGlobalScopeEntry::Type(_) => continue,
74+
7175
TypedGlobalScopeEntry::ImplLessFunction(descriptor_ind) => {
7276
let descriptor =
7377
lowering_ctx.hir_ctx.global_scope.descriptors[descriptor_ind].clone();
@@ -96,7 +100,7 @@ pub fn lower_hir(ctx: HIRContext) -> DiagnosticResult<MIRContext> {
96100
lowering_ctx.mir_ctx.append_function(func);
97101
}
98102

99-
_ => todo!("Add support for remaining nodes"),
103+
_ => todo!("Add support for remaining nodes {:#?}", entry),
100104
};
101105
}
102106

compiler/astoir_mir_lowering/src/vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn lower_hir_variable_declaration(
4646
}
4747
};
4848

49-
let local_ctx = ctx.hir_ctx.global_scope.implementations[*fns_ind].0.clone();
49+
let local_ctx = ctx.hir_ctx.global_scope.contexts[*fns_ind].0.clone();
5050

5151
if local_ctx.is_eligible_for_ssa(variable) {
5252
if default_val.is_some() {

compiler/compiler_global_scope/src/entry.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum GlobalStorageEntryType<T: Hash, R: Hash> {
1313
impl_ind: usize,
1414
},
1515
ImplLessFunction(usize),
16+
HalfImplFunction { descriptor_ind: usize, branch_ctx: usize }
1617
StructFunction {
1718
descriptor_ind: usize,
1819
impl_ind: usize,

compiler/compiler_global_scope/src/lib.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ impl<T: Clone + Hash + Eq, R: Clone + Hash + Eq> GlobalScopeStorage<T, R> {
5757
return Err(make_already_in_scope(origin, &name.name_hash).into());
5858
}
5959

60+
if let GlobalStorageEntryType::Function { .. } = entry {
61+
self.descriptor_counter += 1;
62+
self.impl_counter += 1;
63+
}
64+
65+
if let GlobalStorageEntryType::ImplLessFunction(_) = entry {
66+
self.descriptor_counter += 1;
67+
}
68+
69+
if let GlobalStorageEntryType::StructFunction { .. } = entry {
70+
self.descriptor_counter += 1;
71+
self.impl_counter += 1;
72+
}
73+
6074
let parent_index = self.entries.len();
6175

6276
self.value_to_ind.insert(entry.clone(), parent_index);
@@ -125,6 +139,10 @@ impl<T: Clone + Hash + Eq, R: Clone + Hash + Eq> GlobalScopeStorage<T, R> {
125139
impl_ind: _,
126140
} => Ok(descriptor_ind),
127141
GlobalStorageEntryType::ImplLessFunction(descriptor_ind) => Ok(descriptor_ind),
142+
GlobalStorageEntryType::HalfImplFunction {
143+
descriptor_ind,
144+
branch_ctx: _,
145+
} => Ok(descriptor_ind),
128146
GlobalStorageEntryType::StructFunction {
129147
descriptor_ind,
130148
impl_ind: _,
@@ -135,6 +153,36 @@ impl<T: Clone + Hash + Eq, R: Clone + Hash + Eq> GlobalScopeStorage<T, R> {
135153
};
136154
}
137155

156+
pub fn get_function_ctx<K: DiagnosticSpanOrigin>(
157+
&self,
158+
name: EntryKey,
159+
origin: &K,
160+
) -> DiagnosticResult<usize> {
161+
let base = self.get_base(name, origin)?;
162+
163+
return match base {
164+
GlobalStorageEntryType::Function {
165+
descriptor_ind: _,
166+
impl_ind,
167+
} => Ok(impl_ind),
168+
169+
GlobalStorageEntryType::StructFunction {
170+
descriptor_ind: _,
171+
impl_ind,
172+
struct_type: _,
173+
} => Ok(impl_ind),
174+
175+
GlobalStorageEntryType::HalfImplFunction {
176+
descriptor_ind: _,
177+
branch_ctx,
178+
} => Ok(branch_ctx),
179+
180+
_ => Err(
181+
make_expected_simple_error(origin, &"function with implementation", &base).into(),
182+
),
183+
};
184+
}
185+
138186
pub fn get_function_impl<K: DiagnosticSpanOrigin>(
139187
&self,
140188
name: EntryKey,

0 commit comments

Comments
 (0)