Skip to content

Commit

Permalink
Add constants to the DeclarationEngine (FuelLabs#2714)
Browse files Browse the repository at this point in the history
* Make the DE a lazy static as a semi-temporary bandaid.

* more public methods.

* update

* Add Span to the DeclarationId type so that it can derive Spanned.

* Add new macro to use with DE.

* Add the CopyTypes trait to DeclarationId

* fmt

* Add traits to the DeclarationEngine.

* Remove macro requirement.

* Fix bad merge.

* Add constants to DE.

* Reviewer feedback.

Co-authored-by: Toby Hutton <[email protected]>
  • Loading branch information
emilyaherbert and otrho authored Sep 8, 2022
1 parent 4b75c1d commit d1700ad
Show file tree
Hide file tree
Showing 20 changed files with 697 additions and 356 deletions.
29 changes: 16 additions & 13 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ impl ControlFlowGraph {
ControlFlowGraphNode::ProgramNode(TypedAstNode {
content:
TypedAstNodeContent::Declaration(TypedDeclaration::ConstantDeclaration(
TypedConstantDeclaration {
visibility: Visibility::Public,
..
},
decl_id,
)),
..
}) => true,
}) => {
let decl = de_get_constant(decl_id.clone(), &decl_id.span())?;
decl.visibility.is_public()
}
_ => false,
};
if count_it {
Expand Down Expand Up @@ -318,12 +318,13 @@ fn connect_declaration(
) -> Result<Vec<NodeIndex>, CompileError> {
use TypedDeclaration::*;
match decl {
VariableDeclaration(TypedVariableDeclaration {
name,
body,
mutability: is_mutable,
..
}) => {
VariableDeclaration(var_decl) => {
let TypedVariableDeclaration {
name,
body,
mutability: is_mutable,
..
} = &**var_decl;
if matches!(is_mutable, VariableMutability::ExportedConst) {
graph.namespace.insert_constant(name.clone(), entry_node);
Ok(leaves.to_vec())
Expand All @@ -339,8 +340,10 @@ fn connect_declaration(
)
}
}
ConstantDeclaration(TypedConstantDeclaration { name, value, .. }) => {
graph.namespace.insert_constant(name.clone(), entry_node);
ConstantDeclaration(decl_id) => {
let TypedConstantDeclaration { name, value, .. } =
de_get_constant(decl_id.clone(), &span)?;
graph.namespace.insert_constant(name, entry_node);
connect_expression(
&value.expression,
graph,
Expand Down
32 changes: 30 additions & 2 deletions sway-core/src/declaration_engine/declaration_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use sway_types::{Span, Spanned};
use crate::{
concurrent_slab::ConcurrentSlab,
semantic_analysis::{
TypedAbiDeclaration, TypedImplTrait, TypedStorageDeclaration, TypedStructDeclaration,
TypedTraitDeclaration, TypedTraitFn,
TypedAbiDeclaration, TypedConstantDeclaration, TypedImplTrait, TypedStorageDeclaration,
TypedStructDeclaration, TypedTraitDeclaration, TypedTraitFn,
},
CompileError, TypedFunctionDeclaration,
};
Expand Down Expand Up @@ -205,6 +205,23 @@ impl DeclarationEngine {
) -> Result<TypedAbiDeclaration, CompileError> {
self.slab.get(*index).expect_abi(span)
}

fn insert_constant(&self, constant: TypedConstantDeclaration) -> DeclarationId {
let span = constant.name.span();
DeclarationId::new(
self.slab
.insert(DeclarationWrapper::Constant(Box::new(constant))),
span,
)
}

fn get_constant(
&self,
index: DeclarationId,
span: &Span,
) -> Result<TypedConstantDeclaration, CompileError> {
self.slab.get(*index).expect_constant(span)
}
}

pub(crate) fn de_clear() {
Expand Down Expand Up @@ -316,3 +333,14 @@ pub(crate) fn de_insert_abi(abi: TypedAbiDeclaration) -> DeclarationId {
pub fn de_get_abi(index: DeclarationId, span: &Span) -> Result<TypedAbiDeclaration, CompileError> {
DECLARATION_ENGINE.get_abi(index, span)
}

pub(crate) fn de_insert_constant(constant: TypedConstantDeclaration) -> DeclarationId {
DECLARATION_ENGINE.insert_constant(constant)
}

pub fn de_get_constant(
index: DeclarationId,
span: &Span,
) -> Result<TypedConstantDeclaration, CompileError> {
DECLARATION_ENGINE.get_constant(index, span)
}
25 changes: 23 additions & 2 deletions sway-core/src/declaration_engine/declaration_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use sway_types::Span;

use crate::{
semantic_analysis::{
TypedAbiDeclaration, TypedImplTrait, TypedStorageDeclaration, TypedStructDeclaration,
TypedTraitDeclaration, TypedTraitFn,
TypedAbiDeclaration, TypedConstantDeclaration, TypedImplTrait, TypedStorageDeclaration,
TypedStructDeclaration, TypedTraitDeclaration, TypedTraitFn,
},
type_system::{CopyTypes, TypeMapping},
CompileError, TypedFunctionDeclaration,
Expand All @@ -24,6 +24,7 @@ pub(crate) enum DeclarationWrapper {
Struct(TypedStructDeclaration),
Storage(TypedStorageDeclaration),
Abi(TypedAbiDeclaration),
Constant(Box<TypedConstantDeclaration>),
}

impl Default for DeclarationWrapper {
Expand All @@ -46,6 +47,7 @@ impl PartialEq for DeclarationWrapper {
(DeclarationWrapper::Struct(l), DeclarationWrapper::Struct(r)) => l == r,
(DeclarationWrapper::Storage(l), DeclarationWrapper::Storage(r)) => l == r,
(DeclarationWrapper::Abi(l), DeclarationWrapper::Abi(r)) => l == r,
(DeclarationWrapper::Constant(l), DeclarationWrapper::Constant(r)) => l == r,
_ => false,
}
}
Expand All @@ -68,6 +70,7 @@ impl CopyTypes for DeclarationWrapper {
DeclarationWrapper::Struct(decl) => decl.copy_types(type_mapping),
DeclarationWrapper::Storage(_) => {}
DeclarationWrapper::Abi(_) => {}
DeclarationWrapper::Constant(_) => {}
}
}
}
Expand All @@ -84,6 +87,7 @@ impl DeclarationWrapper {
DeclarationWrapper::TraitFn(_) => "trait function",
DeclarationWrapper::Storage(_) => "storage",
DeclarationWrapper::Abi(_) => "abi",
DeclarationWrapper::Constant(_) => "constant",
}
}

Expand Down Expand Up @@ -190,4 +194,21 @@ impl DeclarationWrapper {
)),
}
}

pub(super) fn expect_constant(
self,
span: &Span,
) -> Result<TypedConstantDeclaration, CompileError> {
match self {
DeclarationWrapper::Constant(decl) => Ok(*decl),
DeclarationWrapper::Unknown => Err(CompileError::Internal(
"did not expect to find unknown declaration",
span.clone(),
)),
_ => Err(CompileError::Internal(
"expected to find constant definition",
span.clone(),
)),
}
}
}
4 changes: 3 additions & 1 deletion 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::de_get_constant,
error::CompileError,
metadata::MetadataManager,
parse_tree::Visibility,
Expand Down Expand Up @@ -93,7 +94,8 @@ fn compile_declarations(
) -> Result<(), CompileError> {
for declaration in declarations {
match declaration {
TypedDeclaration::ConstantDeclaration(decl) => {
TypedDeclaration::ConstantDeclaration(ref decl_id) => {
let decl = de_get_constant(decl_id.clone(), &declaration.span())?;
compile_const_decl(
&mut LookupEnv {
context,
Expand Down
13 changes: 7 additions & 6 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
declaration_engine::declaration_engine::de_get_constant,
error::CompileError,
metadata::MetadataManager,
semantic_analysis::{
Expand Down Expand Up @@ -43,11 +44,11 @@ pub(crate) fn compile_const_decl(
// See if we it's a global const and whether we can compile it *now*.
let decl = module_ns.check_symbol(name)?;
let decl_name_value = match decl {
TypedDeclaration::ConstantDeclaration(TypedConstantDeclaration {
name,
value,
..
}) => Some((name, value)),
TypedDeclaration::ConstantDeclaration(decl_id) => {
let TypedConstantDeclaration { name, value, .. } =
de_get_constant(decl_id.clone(), &name.span())?;
Some((name, value))
}
_otherwise => None,
};
if let Some((name, value)) = decl_name_value {
Expand All @@ -56,7 +57,7 @@ pub(crate) fn compile_const_decl(
env.md_mgr,
env.module,
env.module_ns,
value,
&value,
)?;
env.module
.add_global_constant(env.context, name.as_str().to_owned(), const_val);
Expand Down
6 changes: 4 additions & 2 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::{
use crate::{
asm_generation::from_ir::ir_type_size_in_bytes,
constants,
declaration_engine::declaration_engine::de_get_constant,
error::{CompileError, Hint},
ir_generation::const_eval::{
compile_constant_expression, compile_constant_expression_to_constant,
Expand Down Expand Up @@ -121,9 +122,10 @@ impl FnCompiler {
}
TypedAstNodeContent::Declaration(td) => match td {
TypedDeclaration::VariableDeclaration(tvd) => {
self.compile_var_decl(context, md_mgr, tvd, span_md_idx)
self.compile_var_decl(context, md_mgr, *tvd, span_md_idx)
}
TypedDeclaration::ConstantDeclaration(tcd) => {
TypedDeclaration::ConstantDeclaration(decl_id) => {
let tcd = de_get_constant(decl_id, &ast_node.span)?;
self.compile_const_decl(context, md_mgr, tcd, span_md_idx)
}
TypedDeclaration::FunctionDeclaration(_) => {
Expand Down
Loading

0 comments on commit d1700ad

Please sign in to comment.