Skip to content

Commit

Permalink
Pass the Declaration Engine to IR gen (#2642)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyaherbert authored Aug 29, 2022
1 parent 38166d5 commit 4516bfc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
7 changes: 5 additions & 2 deletions sway-core/src/declaration_engine/declaration_engine.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use crate::{
concurrent_slab::ConcurrentSlab,
semantic_analysis::{
Expand All @@ -9,18 +11,19 @@ use crate::{
use super::{declaration_id::DeclarationId, declaration_wrapper::DeclarationWrapper};

/// Used inside of type inference to store declarations.
#[derive(Debug)]
pub struct DeclarationEngine {
slab: ConcurrentSlab<DeclarationId, DeclarationWrapper>,
// *declaration_id -> vec of monomorphized copies
// where the declaration_id is the original declaration
monomorphized_copies: im::HashMap<usize, Vec<DeclarationId>>,
monomorphized_copies: HashMap<usize, Vec<DeclarationId>>,
}

impl DeclarationEngine {
pub(crate) fn new() -> DeclarationEngine {
DeclarationEngine {
slab: ConcurrentSlab::default(),
monomorphized_copies: im::HashMap::new(),
monomorphized_copies: HashMap::new(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/declaration_engine/declaration_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{

/// The [DeclarationWrapper] type is used in the [DeclarationEngine]
/// as a means of placing all declaration types into the same type.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub(crate) enum DeclarationWrapper {
// no-op variant to fulfill the default trait
Default,
Expand Down
23 changes: 20 additions & 3 deletions sway-core/src/ir_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ use sway_types::span::Span;
pub(crate) use purity::PurityChecker;

pub fn compile_program(program: TypedProgram) -> Result<Context, CompileError> {
let TypedProgram { kind, root, .. } = program;
let TypedProgram {
kind,
root,
declaration_engine,
..
} = program;

let mut ctx = Context::default();
match kind {
Expand All @@ -31,11 +36,23 @@ pub fn compile_program(program: TypedProgram) -> Result<Context, CompileError> {
declarations,
// predicates and scripts have the same codegen, their only difference is static
// type-check time checks.
} => compile::compile_script(&mut ctx, main_function, &root.namespace, declarations),
} => compile::compile_script(
&mut ctx,
main_function,
&root.namespace,
&declaration_engine,
declarations,
),
TypedProgramKind::Contract {
abi_entries,
declarations,
} => compile::compile_contract(&mut ctx, abi_entries, &root.namespace, declarations),
} => compile::compile_contract(
&mut ctx,
abi_entries,
&root.namespace,
&declaration_engine,
declarations,
),
TypedProgramKind::Library { .. } => unimplemented!("compile library to ir"),
}?;
ctx.verify()
Expand Down
23 changes: 20 additions & 3 deletions sway-core/src/ir_generation/compile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
declaration_engine::declaration_engine,
error::CompileError,
metadata::MetadataManager,
parse_tree::Visibility,
Expand All @@ -19,13 +20,21 @@ pub(super) fn compile_script(
context: &mut Context,
main_function: TypedFunctionDeclaration,
namespace: &namespace::Module,
declaration_engine: &declaration_engine::DeclarationEngine,
declarations: Vec<TypedDeclaration>,
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Script);
let mut md_mgr = MetadataManager::default();

compile_constants(context, &mut md_mgr, module, namespace)?;
compile_declarations(context, &mut md_mgr, module, namespace, declarations)?;
compile_declarations(
context,
&mut md_mgr,
module,
namespace,
declaration_engine,
declarations,
)?;
compile_function(context, &mut md_mgr, module, main_function)?;

Ok(module)
Expand All @@ -35,13 +44,21 @@ pub(super) fn compile_contract(
context: &mut Context,
abi_entries: Vec<TypedFunctionDeclaration>,
namespace: &namespace::Module,
declaration_engine: &declaration_engine::DeclarationEngine,
declarations: Vec<TypedDeclaration>,
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Contract);
let mut md_mgr = MetadataManager::default();

compile_constants(context, &mut md_mgr, module, namespace)?;
compile_declarations(context, &mut md_mgr, module, namespace, declarations)?;
compile_declarations(
context,
&mut md_mgr,
module,
namespace,
declaration_engine,
declarations,
)?;
for decl in abi_entries {
compile_abi_method(context, &mut md_mgr, module, decl)?;
}
Expand Down Expand Up @@ -84,12 +101,12 @@ pub(crate) fn compile_constants(
// And for structs and enums in particular, we must ignore those with embedded generic types as
// they are monomorphised only at the instantation site. We must ignore the generic declarations
// altogether anyway.

fn compile_declarations(
context: &mut Context,
md_mgr: &mut MetadataManager,
module: Module,
namespace: &namespace::Module,
_declaration_engine: &declaration_engine::DeclarationEngine,
declarations: Vec<TypedDeclaration>,
) -> Result<(), CompileError> {
for declaration in declarations {
Expand Down
4 changes: 1 addition & 3 deletions sway-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,11 @@ pub fn parsed_to_ast(
let mut warnings = Vec::new();
let mut errors = Vec::new();

let mut declaration_engine = DeclarationEngine::new();

let CompileResult {
value: typed_program_result,
warnings: new_warnings,
errors: new_errors,
} = TypedProgram::type_check(parse_program, initial_namespace, &mut declaration_engine);
} = TypedProgram::type_check(parse_program, initial_namespace, DeclarationEngine::new());
warnings.extend(new_warnings);
errors.extend(new_errors);
let typed_program = match typed_program_result {
Expand Down
12 changes: 9 additions & 3 deletions sway-core/src/semantic_analysis/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ use fuel_tx::StorageSlot;
use sway_ir::{Context, Module};
use sway_types::{span::Span, Ident, JsonABI, JsonABIProgram, JsonTypeDeclaration, Spanned};

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct TypedProgram {
pub kind: TypedProgramKind,
pub root: TypedModule,
pub storage_slots: Vec<StorageSlot>,
pub declaration_engine: DeclarationEngine,
}

impl TypedProgram {
Expand All @@ -33,10 +34,10 @@ impl TypedProgram {
pub fn type_check(
parsed: &ParseProgram,
initial_namespace: namespace::Module,
declaration_engine: &mut DeclarationEngine,
mut declaration_engine: DeclarationEngine,
) -> CompileResult<Self> {
let mut namespace = Namespace::init_root(initial_namespace);
let ctx = TypeCheckContext::from_root(&mut namespace, declaration_engine);
let ctx = TypeCheckContext::from_root(&mut namespace, &mut declaration_engine);
let ParseProgram { root, kind } = parsed;
let mod_span = root.tree.span.clone();
let mod_res = TypedModule::type_check(ctx, root);
Expand All @@ -46,6 +47,7 @@ impl TypedProgram {
kind,
root,
storage_slots: vec![],
declaration_engine,
})
})
}
Expand Down Expand Up @@ -259,6 +261,7 @@ impl TypedProgram {
) -> CompileResult<Self> {
let mut warnings = vec![];
let mut errors = vec![];
let declaration_engine = DeclarationEngine::new();
match &self.kind {
TypedProgramKind::Contract { declarations, .. } => {
let storage_decl = declarations
Expand All @@ -282,6 +285,7 @@ impl TypedProgram {
kind: self.kind.clone(),
root: self.root.clone(),
storage_slots,
declaration_engine,
},
warnings,
errors,
Expand All @@ -292,6 +296,7 @@ impl TypedProgram {
kind: self.kind.clone(),
root: self.root.clone(),
storage_slots: vec![],
declaration_engine,
},
warnings,
errors,
Expand All @@ -303,6 +308,7 @@ impl TypedProgram {
kind: self.kind.clone(),
root: self.root.clone(),
storage_slots: vec![],
declaration_engine,
},
warnings,
errors,
Expand Down

0 comments on commit 4516bfc

Please sign in to comment.