Skip to content

Commit 586eab1

Browse files
committed
fix: fixed HIR lowering layer from errors
1 parent 005a2fc commit 586eab1

7 files changed

Lines changed: 74 additions & 57 deletions

File tree

compiler/astoir_hir_lowering/src/func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub fn lower_ast_function_declaration(
145145
implementation.clone(),
146146
curr_ctx.clone(),
147147
&*node,
148-
);
148+
)?;
149149

150150
return Ok(implementation);
151151
}

compiler/astoir_hir_lowering/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,13 @@ pub fn lower_ast_toplevel(
123123
) -> DiagnosticResult<bool> {
124124
match node.kind {
125125
ASTTreeNodeKind::FunctionDeclaration { .. } => {
126-
let func_decl = lower_ast_function_declaration(context, node)?;
127-
128-
context.function_declarations.push(Some(func_decl));
126+
lower_ast_function_declaration(context, node)?;
129127

130128
return Ok(true);
131129
}
132130

133131
ASTTreeNodeKind::ShadowFunctionDeclaration { .. } => {
134-
let func_decl = lower_ast_shadow_function_declaration(context, node)?;
135-
136-
context.function_declarations.push(Some(func_decl));
132+
lower_ast_shadow_function_declaration(context, node)?;
137133

138134
return Ok(true);
139135
}

compiler/astoir_hir_lowering/src/structs.rs

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
use std::collections::HashMap;
1+
use std::{
2+
collections::HashMap,
3+
hash::{DefaultHasher, Hash, Hasher},
4+
};
25

36
use ast::tree::{ASTTreeNode, ASTTreeNodeKind};
47
use astoir_hir::{
58
ctx::{HIRBranchedContext, HIRContext},
69
nodes::{HIRNode, HIRNodeKind},
7-
structs::HIRStructContainer,
810
};
911
use compiler_global_scope::{entry::GlobalStorageEntryType, key::EntryKey};
1012
use compiler_typing::{raw::RawType, structs::RawStructTypeContainer};
11-
use compiler_utils::utils::indexed::IndexStorage;
13+
use compiler_utils::{hash::HashedString, utils::indexed::IndexStorage};
1214
use diagnostics::{DiagnosticResult, builders::make_already_in_scope};
1315

1416
use crate::{lower_ast_body, types::lower_ast_type_struct, values::lower_ast_value};
@@ -32,7 +34,8 @@ fn lower_ast_struct_function_decl(
3234
context: &mut HIRContext,
3335
node: Box<ASTTreeNode>,
3436
container: &mut RawStructTypeContainer,
35-
) -> DiagnosticResult<Box<HIRNode>> {
37+
ty: RawType,
38+
) -> DiagnosticResult<(Box<HIRNode>, usize)> {
3639
if let ASTTreeNodeKind::FunctionDeclaration {
3740
func_name,
3841
args,
@@ -66,18 +69,47 @@ fn lower_ast_struct_function_decl(
6669
.functions
6770
.append(func_name.hash, (arguments.clone(), ret_type.clone()));
6871

69-
return Ok(Box::new(HIRNode::new(
72+
let implementation = Box::new(HIRNode::new(
7073
HIRNodeKind::StructFunctionDeclaration {
7174
func_name: ind,
72-
arguments,
73-
return_type: ret_type,
75+
arguments: arguments.clone(),
76+
return_type: ret_type.clone(),
7477
body,
75-
ctx: curr_ctx,
78+
ctx: curr_ctx.clone(),
7679
requires_this,
7780
},
7881
&node.start,
7982
&node.end,
80-
)));
83+
));
84+
85+
let mut hasher = DefaultHasher::new();
86+
ty.hash(&mut hasher);
87+
88+
let fnname = format!("{}$${}", hasher.finish(), func_name.hash);
89+
90+
let ret_type2;
91+
let mut arguments2 = vec![];
92+
93+
if let Some(v) = ret_type {
94+
ret_type2 = Some(v.as_resolved()) // TODO: This unsupports generics, maybe fix later
95+
} else {
96+
ret_type2 = None;
97+
}
98+
99+
for arg in &arguments {
100+
arguments2.push((arg.0, arg.1.clone().as_resolved()));
101+
}
102+
103+
context.global_scope.append_struct_function(
104+
EntryKey {
105+
name_hash: HashedString::new(fnname.clone()).hash,
106+
},
107+
(ret_type2, arguments2, fnname),
108+
implementation,
109+
curr_ctx,
110+
ty,
111+
&*node,
112+
)?;
81113
}
82114

83115
panic!("Invalid node type")
@@ -98,10 +130,10 @@ pub fn lower_ast_struct_declaration(
98130
fields: IndexStorage::new(),
99131
functions: IndexStorage::new(),
100132
type_params,
133+
function_ids: vec![],
134+
self_ref: context.global_scope.scope.entries.len(),
101135
};
102136

103-
let mut func_impls = vec![];
104-
105137
let base = RawType::Struct(layout, container.clone());
106138

107139
let ind = match context.global_scope.append(
@@ -124,25 +156,23 @@ pub fn lower_ast_struct_declaration(
124156
GlobalStorageEntryType::Type(RawType::Struct(layout, container.clone()));
125157
}
126158
&ASTTreeNodeKind::FunctionDeclaration { .. } => {
127-
let body = lower_ast_struct_function_decl(context, member, &mut container)?;
159+
let body = lower_ast_struct_function_decl(
160+
context,
161+
member,
162+
&mut container,
163+
context.global_scope.scope.entries[ind].as_type_unsafe(),
164+
)?;
165+
166+
container.function_ids.push(body.1);
128167

129168
context.global_scope.scope.entries[ind].entry_type =
130169
GlobalStorageEntryType::Type(RawType::Struct(layout, container.clone()));
131-
132-
func_impls.push(body);
133170
}
134171

135172
_ => panic!("Invalid node type"),
136173
};
137174
}
138175

139-
context.struct_func_impls.insert(
140-
ind,
141-
HIRStructContainer {
142-
function_impls: func_impls,
143-
},
144-
);
145-
146176
return Ok(Box::new(HIRNode::new(
147177
HIRNodeKind::StructDeclaration {
148178
type_name: ind,

compiler/astoir_hir_lowering/src/uses.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use ast::{
66
types::ASTType,
77
};
88
use ast_parser::parse_ast_ctx;
9-
use astoir_hir::{
10-
ctx::HIRContext,
11-
nodes::{HIRNode, HIRNodeKind},
12-
};
9+
use astoir_hir::ctx::HIRContext;
1310
use compiler_global_scope::{entry::GlobalStorageEntryType, key::EntryKey};
1411
use compiler_typing::{raw::RawType, tree::Type};
1512
use compiler_utils::hash::HashedString;
@@ -60,24 +57,13 @@ pub fn handle_ast_use_statement_function_decl(
6057
arguments.push((arg.name.hash, t));
6158
}
6259

63-
let func_name = context.functions.append(
64-
func_name.hash,
65-
(ret_type.clone(), arguments.clone(), func_name.val.clone()),
66-
);
67-
context.function_contexts.push(None);
68-
69-
// Fabricate shadow func statement to satisfy functions_declarations
70-
71-
let node = HIRNode::new(
72-
HIRNodeKind::ShadowFunctionDeclaration {
73-
func_name,
74-
arguments,
75-
return_type: ret_type,
60+
let _ = context.global_scope.append_implless_function(
61+
EntryKey {
62+
name_hash: func_name.hash,
7663
},
77-
&node.start,
78-
&node.end,
79-
);
80-
context.function_declarations.push(Some(Box::new(node)));
64+
(ret_type.clone(), arguments.clone(), func_name.val.clone()),
65+
&*node,
66+
)?;
8167

8268
return Ok(());
8369
}

compiler/astoir_hir_lowering/src/values.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ use astoir_hir::{
44
nodes::{HIRNode, HIRNodeKind},
55
structs::StructLRUStep,
66
};
7+
use compiler_global_scope::key::EntryKey;
78
use compiler_typing::tree::Type;
89
use diagnostics::{
910
DiagnosticResult,
10-
builders::{
11-
make_cannot_find_func, make_invalid_pointing, make_struct_missing_field,
12-
make_struct_missing_func,
13-
},
11+
builders::{make_invalid_pointing, make_struct_missing_field, make_struct_missing_func},
1412
};
1513

1614
use crate::{
@@ -66,12 +64,12 @@ pub(crate) fn lower_ast_lru_base(
6664

6765
ind = res.0;
6866
} else {
69-
ind = match context.functions.get_index(func.hash) {
70-
Some(v) => v,
71-
None => return Err(make_cannot_find_func(&*node, &func.val).into()),
67+
let entry = EntryKey {
68+
name_hash: func.hash,
7269
};
7370

74-
func_type = context.functions.vals[ind].clone();
71+
ind = context.global_scope.get_ind(entry.clone(), &*node)?;
72+
func_type = context.global_scope.get_function_base(entry, &*node)?;
7573
}
7674

7775
let mut hir_args = vec![];

compiler/compiler_typing/src/raw.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ impl Hash for RawType {
349349
hasher.write_u8(*signed as u8);
350350
}
351351

352+
RawType::Struct(_, container) => {
353+
hasher.write_usize(9);
354+
hasher.write_usize(container.self_ref);
355+
}
356+
352357
_ => panic!("Unhashable type {:#?}", self),
353358
}
354359
}

compiler/compiler_typing/src/structs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub struct RawStructTypeContainer {
1919
pub fields: IndexStorage<TypeReference>,
2020
pub type_params: TypeParameterContainer,
2121
pub functions: IndexStorage<TypedFunction>,
22+
pub function_ids: Vec<usize>,
23+
pub self_ref: usize,
2224
}
2325

2426
#[derive(Debug, Clone, PartialEq, Eq)]

0 commit comments

Comments
 (0)