Skip to content

Commit 352d73b

Browse files
committed
Implement mir function body lowering
1 parent f7e0410 commit 352d73b

22 files changed

Lines changed: 1297 additions & 74 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.

crates/analyzer/src/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,11 @@ pub struct FunctionBody {
355355
pub expressions: IndexMap<NodeId, ExpressionAttributes>,
356356
pub emits: IndexMap<NodeId, EventId>,
357357
pub string_literals: IndexSet<SmolStr>, // for yulgen
358+
// Map lhs of variable declaration to type.
359+
pub var_types: IndexMap<NodeId, Type>,
358360

359361
// This is the id of the VarDecl TypeDesc node
362+
// TODO: Do we really need this?
360363
pub var_decl_types: IndexMap<NodeId, FixedSize>,
361364
pub calls: IndexMap<NodeId, CallType>,
362365
pub spans: HashMap<NodeId, Span>,

crates/analyzer/src/namespace/items.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::{builtins, errors::ConstEvalError};
1010
use fe_common::diagnostics::Diagnostic;
1111
use fe_common::files::{common_prefix, Utf8Path};
1212
use fe_common::{impl_intern_key, FileKind, SourceFileId};
13-
use fe_parser::ast;
1413
use fe_parser::node::{Node, Span};
14+
use fe_parser::{ast, node::NodeId};
1515
use indexmap::{indexmap, IndexMap, IndexSet};
1616
use smol_str::SmolStr;
1717
use std::ops::Deref;
@@ -779,6 +779,14 @@ impl ModuleConstantId {
779779
Item::Module(self.data(db).module)
780780
}
781781

782+
pub fn module(&self, db: &dyn AnalyzerDb) -> ModuleId {
783+
self.data(db).module
784+
}
785+
786+
pub fn node_id(&self, db: &dyn AnalyzerDb) -> NodeId {
787+
self.data(db).ast.id
788+
}
789+
782790
pub fn sink_diagnostics(&self, db: &dyn AnalyzerDb, sink: &mut impl DiagnosticSink) {
783791
db.module_constant_type(*self)
784792
.diagnostics

crates/analyzer/src/namespace/scopes.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ impl<'a> AnalyzerContext for ItemScope<'a> {
6464
_expr: &Node<ast::Expr>,
6565
_value: crate::context::Constant,
6666
) {
67-
// We use salsa query to get constant. So no need to add constant explicitly.
67+
// We use salsa query to get constant. So no need to add constant
68+
// explicitly.
6869
}
6970

7071
fn constant_value_by_name(
@@ -174,6 +175,15 @@ impl<'a> FunctionScope<'a> {
174175
.expect_none("declaration attributes already exist");
175176
}
176177

178+
pub fn map_variable_type<T>(&self, node: &Node<T>, typ: Type) {
179+
self.add_node(node);
180+
self.body
181+
.borrow_mut()
182+
.var_types
183+
.insert(node.id, typ)
184+
.expect_none("variable has already registered")
185+
}
186+
177187
fn add_node<T>(&self, node: &Node<T>) {
178188
self.body.borrow_mut().spans.insert(node.id, node.span);
179189
}

crates/analyzer/src/traversal/declarations.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ pub fn const_decl(scope: &mut BlockScope, stmt: &Node<fe::FuncStmt>) -> Result<(
7676
let const_value = const_expr::eval_expr(scope, value)?;
7777

7878
scope.root.add_declaration(typ, declared_type.clone());
79+
scope
80+
.root
81+
.map_variable_type(name, declared_type.clone().into());
7982
// this logs a message on err, so it's safe to ignore here.
8083
let _ = scope.add_var(name.kind.as_str(), declared_type, true, name.span);
8184
scope.add_constant(name, value, const_value);
@@ -94,6 +97,7 @@ fn add_var(
9497
match &target.kind {
9598
fe::VarDeclTarget::Name(name) => {
9699
// this logs a message on err, so it's safe to ignore here.
100+
scope.root.map_variable_type(target, typ.clone().into());
97101
let _ = scope.add_var(name, typ, false, target.span);
98102
Ok(())
99103
}

crates/common/src/files.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ impl SourceFileId {
110110
// Used by lowering::ZeroSpanNode, unit tests, and benchmarks
111111
Self(u32::MAX)
112112
}
113+
pub fn is_dummy(self) -> bool {
114+
self == Self::dummy_file()
115+
}
113116
}
114117

115118
#[test]

crates/common/src/span.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ impl Span {
3232
}
3333
}
3434

35+
pub fn dummy() -> Self {
36+
Self {
37+
file_id: SourceFileId::dummy_file(),
38+
start: usize::MAX,
39+
end: usize::MAX,
40+
}
41+
}
42+
43+
pub fn is_dummy(&self) -> bool {
44+
self == &Self::dummy()
45+
}
46+
3547
pub fn from_pair<S, E>(start_elem: S, end_elem: E) -> Self
3648
where
3749
S: Into<Span>,

crates/mir/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ fe-analyzer = { path = "../analyzer", version = "^0.13.0-alpha" }
1313
salsa = "0.16.1"
1414
smol_str = "0.1.21"
1515
num-bigint = "0.4.3"
16+
num-traits = "0.2.14"
1617
id-arena = "2.2.1"
1718
fxhash = "0.2.1"

crates/mir/src/db.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use fe_analyzer::{
66
};
77
use fe_common::db::Upcast;
88

9-
use crate::ir::{self, TypeId};
9+
use crate::ir::{self, ConstantId, TypeId};
1010

1111
mod queries;
1212

@@ -24,9 +24,14 @@ pub trait MirDb: AnalyzerDb + Upcast<dyn AnalyzerDb> {
2424
#[salsa::invoke(queries::types::mir_lowered_event_type)]
2525
fn mir_lowered_event_type(&self, analyzer_type: analyzer_items::EventId) -> TypeId;
2626

27+
#[salsa::invoke(queries::constant::mir_lowered_constant)]
28+
fn mir_lowered_constant(&self, analyzer_const: analyzer_items::ModuleConstantId) -> ConstantId;
29+
2730
#[salsa::invoke(queries::function::mir_lowered_func_signature)]
2831
fn mir_lowered_func_signature(
2932
&self,
3033
analyzer_func: analyzer_items::FunctionId,
3134
) -> ir::FunctionId;
35+
#[salsa::invoke(queries::function::mir_lowered_func_body)]
36+
fn mir_lowered_func_body(&self, func: ir::FunctionId) -> Rc<ir::FunctionBody>;
3237
}

crates/mir/src/db/queries.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod constant;
12
pub mod function;
23
pub mod types;

0 commit comments

Comments
 (0)