Skip to content

Commit

Permalink
Refactor existing code to use engines and add a new query engine. (Fu…
Browse files Browse the repository at this point in the history
…elLabs#4575)

## Description

As title says, this refactor existing code to pass the engines type to
more places, and adds a new query engine while at it.

The query engine in this form still does not do anything at all, but
will be populated next with the necessary bits for its purpose.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
tritao authored May 23, 2023
1 parent e5d3185 commit dc67807
Show file tree
Hide file tree
Showing 66 changed files with 574 additions and 568 deletions.
4 changes: 3 additions & 1 deletion forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use sway_core::{
parsed::{ParseProgram, TreeType},
ty, Visibility,
},
query_engine::QueryEngine,
semantic_analysis::namespace,
source_map::SourceMap,
transform::AttributeKind,
Expand Down Expand Up @@ -2231,7 +2232,8 @@ pub fn build(

let type_engine = TypeEngine::default();
let decl_engine = DeclEngine::default();
let engines = Engines::new(&type_engine, &decl_engine);
let query_engine = QueryEngine::default();
let engines = Engines::new(&type_engine, &decl_engine, &query_engine);
let include_tests = profile.include_tests;

// This is the Contract ID of the current contract being compiled.
Expand Down
7 changes: 5 additions & 2 deletions forc-plugins/forc-doc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use std::{
sync::Arc,
{fs, path::PathBuf},
};
use sway_core::{decl_engine::DeclEngine, BuildTarget, Engines, TypeEngine};
use sway_core::{
decl_engine::DeclEngine, query_engine::QueryEngine, BuildTarget, Engines, TypeEngine,
};

mod cli;
mod doc;
Expand Down Expand Up @@ -150,7 +152,8 @@ fn build_docs(
pkg::BuildPlan::from_lock_and_manifests(&lock_path, &member_manifests, locked, offline)?;
let type_engine = TypeEngine::default();
let decl_engine = DeclEngine::default();
let engines = Engines::new(&type_engine, &decl_engine);
let query_engine = QueryEngine::default();
let engines = Engines::new(&type_engine, &decl_engine, &query_engine);
let tests_enabled = true;
let typed_program = match pkg::check(
&plan,
Expand Down
7 changes: 5 additions & 2 deletions forc/src/cli/commands/check.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::ops::forc_check;
use clap::Parser;
use forc_util::{forc_result_bail, ForcResult};
use sway_core::{decl_engine::DeclEngine, BuildTarget, Engines, TypeEngine};
use sway_core::{
decl_engine::DeclEngine, query_engine::QueryEngine, BuildTarget, Engines, TypeEngine,
};

/// Check the current or target project and all of its dependencies for errors.
///
Expand Down Expand Up @@ -37,7 +39,8 @@ pub struct Command {
pub(crate) fn exec(command: Command) -> ForcResult<()> {
let type_engine = TypeEngine::default();
let decl_engine = DeclEngine::default();
let engines = Engines::new(&type_engine, &decl_engine);
let query_engine = QueryEngine::default();
let engines = Engines::new(&type_engine, &decl_engine, &query_engine);
let res = forc_check::check(command, engines)?;
if !res.is_ok() {
forc_result_bail!("unable to type check");
Expand Down
14 changes: 10 additions & 4 deletions sway-core/src/engine_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ use std::{
hash::{BuildHasher, Hash, Hasher},
};

use crate::{decl_engine::DeclEngine, type_system::TypeEngine};
use crate::{decl_engine::DeclEngine, query_engine::QueryEngine, type_system::TypeEngine};

#[derive(Debug, Clone, Copy)]
pub struct Engines<'a> {
type_engine: &'a TypeEngine,
decl_engine: &'a DeclEngine,
query_engine: &'a QueryEngine,
}

impl<'a> Engines<'a> {
pub fn new(type_engine: &'a TypeEngine, decl_engine: &'a DeclEngine) -> Engines<'a> {
pub fn new(
type_engine: &'a TypeEngine,
decl_engine: &'a DeclEngine,
query_engine: &'a QueryEngine,
) -> Engines<'a> {
Engines {
type_engine,
decl_engine,
query_engine,
}
}

Expand All @@ -28,8 +34,8 @@ impl<'a> Engines<'a> {
self.decl_engine
}

pub(crate) fn unwrap(self) -> (&'a TypeEngine, &'a DeclEngine) {
(self.type_engine, self.decl_engine)
pub(crate) fn unwrap(self) -> (&'a TypeEngine, &'a DeclEngine, &'a QueryEngine) {
(self.type_engine, self.decl_engine, self.query_engine)
}

/// Helps out some `thing: T` by adding `self` as context.
Expand Down
11 changes: 5 additions & 6 deletions sway-core/src/ir_generation/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub(crate) fn compile_constants(
module: Module,
module_ns: &namespace::Module,
) -> Result<(), CompileError> {
let (type_engine, decl_engine) = engines.unwrap();
let (type_engine, decl_engine, query_engine) = engines.unwrap();
for decl_name in module_ns.get_all_declared_symbols() {
if let Some(ty::TyDecl::ConstantDecl(ty::ConstantDecl { decl_id, .. })) =
module_ns.symbols.get(decl_name)
Expand All @@ -212,6 +212,7 @@ pub(crate) fn compile_constants(
&mut LookupEnv {
type_engine,
decl_engine,
query_engine,
context,
md_mgr,
module,
Expand Down Expand Up @@ -249,7 +250,7 @@ fn compile_declarations(
namespace: &namespace::Module,
declarations: &[ty::TyDecl],
) -> Result<(), CompileError> {
let (type_engine, decl_engine) = engines.unwrap();
let (type_engine, decl_engine, query_engine) = engines.unwrap();
for declaration in declarations {
match declaration {
ty::TyDecl::ConstantDecl(ty::ConstantDecl { decl_id, .. }) => {
Expand All @@ -259,6 +260,7 @@ fn compile_declarations(
&mut LookupEnv {
type_engine,
decl_engine,
query_engine,
context,
md_mgr,
module,
Expand Down Expand Up @@ -534,11 +536,8 @@ fn compile_abi_method(
messages_types_map: &HashMap<TypeId, MessageId>,
engines: Engines<'_>,
) -> Result<Function, CompileError> {
let type_engine = engines.te();
let decl_engine = engines.de();

// Use the error from .to_fn_selector_value() if possible, else make an CompileError::Internal.
let get_selector_result = ast_fn_decl.to_fn_selector_value(type_engine, decl_engine);
let get_selector_result = ast_fn_decl.to_fn_selector_value(engines);
let mut warnings = Vec::new();
let mut errors = Vec::new();
let selector = match get_selector_result.ok(&mut warnings, &mut errors) {
Expand Down
9 changes: 6 additions & 3 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
CallPath,
},
metadata::MetadataManager,
query_engine::QueryEngine,
semantic_analysis::*,
TypeEngine,
};
Expand All @@ -35,6 +36,7 @@ use sway_utils::mapped_stack::MappedStack;
pub(crate) struct LookupEnv<'a> {
pub(crate) type_engine: &'a TypeEngine,
pub(crate) decl_engine: &'a DeclEngine,
pub(crate) query_engine: &'a QueryEngine,
pub(crate) context: &'a mut Context,
pub(crate) md_mgr: &'a mut MetadataManager,
pub(crate) module: Module,
Expand Down Expand Up @@ -137,7 +139,7 @@ pub(crate) fn compile_const_decl(
}

let const_val = compile_constant_expression(
Engines::new(env.type_engine, env.decl_engine),
Engines::new(env.type_engine, env.decl_engine, env.query_engine),
env.context,
env.md_mgr,
env.module,
Expand Down Expand Up @@ -212,10 +214,11 @@ pub(crate) fn compile_constant_expression_to_constant(
function_compiler: Option<&FnCompiler>,
const_expr: &ty::TyExpression,
) -> Result<Constant, CompileError> {
let (type_engine, decl_engine) = engines.unwrap();
let (type_engine, decl_engine, query_engine) = engines.unwrap();
let lookup = &mut LookupEnv {
type_engine,
decl_engine,
query_engine,
context,
md_mgr,
module,
Expand Down Expand Up @@ -396,7 +399,7 @@ fn const_eval_typed_expr(
if !element_typs.iter().all(|tid| {
lookup.type_engine.get(*tid).eq(
&elem_type_info,
Engines::new(lookup.type_engine, lookup.decl_engine),
Engines::new(lookup.type_engine, lookup.decl_engine, lookup.query_engine),
)
}) {
// This shouldn't happen if the type checker did its job.
Expand Down
13 changes: 8 additions & 5 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
*,
},
metadata::MetadataManager,
query_engine::QueryEngine,
type_system::*,
types::*,
};
Expand Down Expand Up @@ -60,6 +61,7 @@ use std::collections::HashMap;
pub(crate) struct FnCompiler<'eng> {
type_engine: &'eng TypeEngine,
decl_engine: &'eng DeclEngine,
query_engine: &'eng QueryEngine,
module: Module,
pub(super) function: Function,
pub(super) current_block: Block,
Expand All @@ -84,7 +86,7 @@ impl<'eng> FnCompiler<'eng> {
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
) -> Self {
let (type_engine, decl_engine) = engines.unwrap();
let (type_engine, decl_engine, query_engine) = engines.unwrap();
let lexical_map = LexicalMap::from_iter(
function
.args_iter(context)
Expand All @@ -93,6 +95,7 @@ impl<'eng> FnCompiler<'eng> {
FnCompiler {
type_engine,
decl_engine,
query_engine,
module,
function,
current_block: function.get_entry_block(context),
Expand Down Expand Up @@ -494,7 +497,7 @@ impl<'eng> FnCompiler<'eng> {
Ok(key_val)
}

let engines = Engines::new(self.type_engine, self.decl_engine);
let engines = Engines::new(self.type_engine, self.decl_engine, self.query_engine);

// We safely index into arguments and type_arguments arrays below
// because the type-checker ensures that the arguments are all there.
Expand Down Expand Up @@ -1358,7 +1361,7 @@ impl<'eng> FnCompiler<'eng> {
};
let is_entry = false;
let new_func = compile_function(
Engines::new(self.type_engine, self.decl_engine),
Engines::new(self.type_engine, self.decl_engine, self.query_engine),
context,
md_mgr,
self.module,
Expand Down Expand Up @@ -1772,7 +1775,7 @@ impl<'eng> FnCompiler<'eng> {
} = ast_const_decl;
if let Some(value) = value {
let const_expr_val = compile_constant_expression(
Engines::new(self.type_engine, self.decl_engine),
Engines::new(self.type_engine, self.decl_engine, self.query_engine),
context,
md_mgr,
self.module,
Expand Down Expand Up @@ -2037,7 +2040,7 @@ impl<'eng> FnCompiler<'eng> {
value: ConstantValue::Uint(constant_value),
..
}) = compile_constant_expression_to_constant(
Engines::new(self.type_engine, self.decl_engine),
Engines::new(self.type_engine, self.decl_engine, self.query_engine),
context,
md_mgr,
self.module,
Expand Down
3 changes: 1 addition & 2 deletions sway-core/src/language/ty/declaration/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ impl HashWithEngines for TyAbiDecl {
impl CreateTypeId for TyAbiDecl {
fn create_type_id(&self, engines: Engines<'_>) -> TypeId {
let type_engine = engines.te();
let decl_engine = engines.de();
let ty = TypeInfo::ContractCaller {
abi_name: AbiName::Known(self.name.clone().into()),
address: None,
};
type_engine.insert(decl_engine, ty)
type_engine.insert(engines, ty)
}
}

Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/language/ty/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ impl TyDecl {
subst_list: _,
decl_span,
}) => type_engine.insert(
decl_engine,
engines,
TypeInfo::Struct(DeclRef::new(name.clone(), *decl_id, decl_span.clone())),
),
TyDecl::EnumDecl(EnumDecl {
Expand All @@ -828,13 +828,13 @@ impl TyDecl {
subst_list: _,
decl_span,
}) => type_engine.insert(
decl_engine,
engines,
TypeInfo::Enum(DeclRef::new(name.clone(), *decl_id, decl_span.clone())),
),
TyDecl::StorageDecl(StorageDecl { decl_id, .. }) => {
let storage_decl = decl_engine.get_storage(decl_id);
type_engine.insert(
decl_engine,
engines,
TypeInfo::Storage {
fields: storage_decl.fields_as_typed_struct_fields(),
},
Expand Down
27 changes: 8 additions & 19 deletions sway-core/src/language/ty/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,12 @@ impl TyFunctionDecl {
}
}

pub fn to_fn_selector_value_untruncated(
&self,
type_engine: &TypeEngine,
decl_engine: &DeclEngine,
) -> CompileResult<Vec<u8>> {
pub fn to_fn_selector_value_untruncated(&self, engines: Engines<'_>) -> CompileResult<Vec<u8>> {
let mut errors = vec![];
let mut warnings = vec![];
let mut hasher = Sha256::new();
let data = check!(
self.to_selector_name(type_engine, decl_engine),
self.to_selector_name(engines),
return err(warnings, errors),
warnings,
errors
Expand All @@ -279,15 +275,11 @@ impl TyFunctionDecl {
/// Converts a [TyFunctionDecl] into a value that is to be used in contract function
/// selectors.
/// Hashes the name and parameters using SHA256, and then truncates to four bytes.
pub fn to_fn_selector_value(
&self,
type_engine: &TypeEngine,
decl_engine: &DeclEngine,
) -> CompileResult<[u8; 4]> {
pub fn to_fn_selector_value(&self, engines: Engines<'_>) -> CompileResult<[u8; 4]> {
let mut errors = vec![];
let mut warnings = vec![];
let hash = check!(
self.to_fn_selector_value_untruncated(type_engine, decl_engine),
self.to_fn_selector_value_untruncated(engines),
return err(warnings, errors),
warnings,
errors
Expand All @@ -298,21 +290,18 @@ impl TyFunctionDecl {
ok(buf, warnings, errors)
}

pub fn to_selector_name(
&self,
type_engine: &TypeEngine,
decl_engine: &DeclEngine,
) -> CompileResult<String> {
pub fn to_selector_name(&self, engines: Engines<'_>) -> CompileResult<String> {
let mut errors = vec![];
let mut warnings = vec![];
let named_params = self
.parameters
.iter()
.map(|TyFunctionParameter { type_argument, .. }| {
type_engine
engines
.te()
.to_typeinfo(type_argument.type_id, &type_argument.span)
.expect("unreachable I think?")
.to_selector_name(type_engine, decl_engine, &type_argument.span)
.to_selector_name(engines, &type_argument.span)
})
.filter_map(|name| name.ok(&mut warnings, &mut errors))
.collect::<Vec<String>>();
Expand Down
3 changes: 1 addition & 2 deletions sway-core/src/language/ty/declaration/type_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ impl ReplaceSelfType for TyTypeAliasDecl {
impl CreateTypeId for TyTypeAliasDecl {
fn create_type_id(&self, engines: Engines<'_>) -> TypeId {
let type_engine = engines.te();
let decl_engine = engines.de();
type_engine.insert(
decl_engine,
engines,
TypeInfo::Alias {
name: self.name.clone(),
ty: self.ty.clone(),
Expand Down
3 changes: 1 addition & 2 deletions sway-core/src/language/ty/expression/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,9 @@ impl DeterministicallyAborts for TyExpression {
impl TyExpression {
pub(crate) fn error(span: Span, engines: Engines<'_>) -> TyExpression {
let type_engine = engines.te();
let decl_engine = engines.de();
TyExpression {
expression: TyExpressionVariant::Tuple { fields: vec![] },
return_type: type_engine.insert(decl_engine, TypeInfo::ErrorRecovery),
return_type: type_engine.insert(engines, TypeInfo::ErrorRecovery),
span,
}
}
Expand Down
Loading

0 comments on commit dc67807

Please sign in to comment.