Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abi declarations to the DeclarationEngine #2710

Merged
merged 40 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7a8bb5a
Make the DE a lazy static as a semi-temporary bandaid.
Aug 31, 2022
0a0915d
more public methods.
Aug 31, 2022
69d7215
update
Aug 31, 2022
c12c353
Add Span to the DeclarationId type so that it can derive Spanned.
Aug 31, 2022
34e0ea2
Add new macro to use with DE.
Aug 31, 2022
f0e5e3c
Add the CopyTypes trait to DeclarationId
Aug 31, 2022
9f70f4a
fmt
Aug 31, 2022
404af6e
Merge branch 'emilyaherbert/new-macro' into emilyaherbert-2633/traits…
Aug 31, 2022
d7376a3
Add traits to the DeclarationEngine.
Sep 1, 2022
e7007db
Merge branch 'master' into emilyaherbert/de-as-lazy-static
otrho Sep 1, 2022
a5a2795
Merge branch 'emilyaherbert/de-as-lazy-static' into emilyaherbert/add…
emilyaherbert Sep 1, 2022
e5d4813
Merge branch 'emilyaherbert/add-copytypes-trait-to-declaration-id' in…
emilyaherbert Sep 1, 2022
b2f65fa
Merge remote-tracking branch 'origin' into emilyaherbert/add-copytype…
Sep 1, 2022
38f533c
Merge branch 'emilyaherbert/add-copytypes-trait-to-declaration-id' in…
emilyaherbert Sep 1, 2022
3a45dce
Remove macro requirement.
Sep 1, 2022
2f0c106
Merge branch 'emilyaherbert-2633/traits-to-de' of github.com:FuelLabs…
Sep 1, 2022
97bfa08
Merge branch 'master' into emilyaherbert/add-copytypes-trait-to-decla…
emilyaherbert Sep 1, 2022
8e5b57e
Merge branch 'master' into emilyaherbert/add-copytypes-trait-to-decla…
emilyaherbert Sep 1, 2022
6136d59
Merge branch 'master' into emilyaherbert/add-copytypes-trait-to-decla…
emilyaherbert Sep 1, 2022
ea5c576
Fix bad merge.
Sep 1, 2022
90972b4
Merge branch 'emilyaherbert/add-copytypes-trait-to-declaration-id' in…
Sep 1, 2022
a6b2e1c
Merge branch 'master' into emilyaherbert-2633/traits-to-de
emilyaherbert Sep 1, 2022
db88486
Merge branch 'master' into emilyaherbert-2633/traits-to-de
emilyaherbert Sep 2, 2022
7606901
Merge branch 'master' into emilyaherbert-2633/traits-to-de
emilyaherbert Sep 2, 2022
0b2e348
Merge branch 'master' into emilyaherbert-2633/traits-to-de
emilyaherbert Sep 2, 2022
318b2b5
Merge branch 'master' into emilyaherbert-2633/traits-to-de
emilyaherbert Sep 2, 2022
f7b277e
Add abi declarations to the declaration engine.
Sep 2, 2022
c698a9d
Merge branch 'master' into emilyaherbert-2633/traits-to-de
emilyaherbert Sep 2, 2022
dda11c5
Merge branch 'emilyaherbert-2633/traits-to-de' into emilyaherbert-268…
emilyaherbert Sep 2, 2022
9873e99
Merge branch 'master' into emilyaherbert-2633/traits-to-de
emilyaherbert Sep 3, 2022
ab9efc1
Merge branch 'master' into emilyaherbert-2633/traits-to-de
emilyaherbert Sep 4, 2022
ac1fd9b
Merge remote-tracking branch 'origin/master' into emilyaherbert-2633/…
Sep 5, 2022
9cdd694
Merge branch 'emilyaherbert-2633/traits-to-de' of github.com:FuelLabs…
Sep 5, 2022
7c85966
Merge branch 'emilyaherbert-2633/traits-to-de' into emilyaherbert-268…
emilyaherbert Sep 5, 2022
eec9ec1
Merge remote-tracking branch 'origin/master' into emilyaherbert-2680/…
Sep 6, 2022
4e3e98e
Merge branch 'master' into emilyaherbert-2680/abi-decl-to-de
emilyaherbert Sep 6, 2022
f130c0d
Merge branch 'master' into emilyaherbert-2680/abi-decl-to-de
emilyaherbert Sep 7, 2022
5a3434f
Merge branch 'master' into emilyaherbert-2680/abi-decl-to-de
emilyaherbert Sep 7, 2022
f629165
Merge branch 'master' into emilyaherbert-2680/abi-decl-to-de
emilyaherbert Sep 7, 2022
cb4a7d2
Merge branch 'master' into emilyaherbert-2680/abi-decl-to-de
otrho Sep 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;
use crate::{
declaration_engine::declaration_engine::de_get_storage,
declaration_engine::declaration_engine::de_get_trait,
declaration_engine::declaration_engine::*,
parse_tree::{CallPath, Visibility},
semantic_analysis::{
ast_node::{
Expand Down Expand Up @@ -361,8 +360,9 @@ fn connect_declaration(
connect_trait_declaration(&trait_decl, graph, entry_node);
Ok(leaves.to_vec())
}
AbiDeclaration(abi_decl) => {
connect_abi_declaration(abi_decl, graph, entry_node);
AbiDeclaration(decl_id) => {
let abi_decl = de_get_abi(decl_id.clone(), &span)?;
connect_abi_declaration(&abi_decl, graph, entry_node);
Ok(leaves.to_vec())
}
StructDeclaration(struct_decl) => {
Expand Down
101 changes: 61 additions & 40 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::{
TypedImplTrait, TypedStorageDeclaration, TypedStructDeclaration, TypedTraitDeclaration,
TypedTraitFn,
TypedAbiDeclaration, TypedImplTrait, TypedStorageDeclaration, TypedStructDeclaration,
TypedTraitDeclaration, TypedTraitFn,
},
CompileError, TypedFunctionDeclaration,
};
Expand All @@ -33,11 +33,11 @@ impl DeclarationEngine {
monomorphized_copies.clear();
}

fn de_look_up_decl_id(&self, index: DeclarationId) -> DeclarationWrapper {
fn look_up_decl_id(&self, index: DeclarationId) -> DeclarationWrapper {
self.slab.get(*index)
}

fn de_add_monomorphized_copy(&self, original_id: DeclarationId, new_id: DeclarationId) {
fn add_monomorphized_copy(&self, original_id: DeclarationId, new_id: DeclarationId) {
let mut monomorphized_copies = self.monomorphized_copies.write().unwrap();
match monomorphized_copies.get_mut(&*original_id) {
Some(prev) => {
Expand All @@ -49,7 +49,7 @@ impl DeclarationEngine {
}
}

fn de_get_monomorphized_copies(&self, original_id: DeclarationId) -> Vec<DeclarationWrapper> {
fn get_monomorphized_copies(&self, original_id: DeclarationId) -> Vec<DeclarationWrapper> {
let monomorphized_copies = self.monomorphized_copies.write().unwrap();
match monomorphized_copies.get(&*original_id).cloned() {
Some(copies) => copies
Expand All @@ -60,23 +60,23 @@ impl DeclarationEngine {
}
}

fn de_insert_function(&self, function: TypedFunctionDeclaration) -> DeclarationId {
fn insert_function(&self, function: TypedFunctionDeclaration) -> DeclarationId {
let span = function.span();
DeclarationId::new(
self.slab.insert(DeclarationWrapper::Function(function)),
span,
)
}

fn de_get_function(
fn get_function(
&self,
index: DeclarationId,
span: &Span,
) -> Result<TypedFunctionDeclaration, CompileError> {
self.slab.get(*index).expect_function(span)
}

fn de_add_monomorphized_function_copy(
fn add_monomorphized_function_copy(
&self,
original_id: DeclarationId,
new_copy: TypedFunctionDeclaration,
Expand All @@ -86,42 +86,42 @@ impl DeclarationEngine {
self.slab.insert(DeclarationWrapper::Function(new_copy)),
span,
);
self.de_add_monomorphized_copy(original_id, new_id)
self.add_monomorphized_copy(original_id, new_id)
}

fn de_get_monomorphized_function_copies(
fn get_monomorphized_function_copies(
&self,
original_id: DeclarationId,
span: &Span,
) -> Result<Vec<TypedFunctionDeclaration>, CompileError> {
self.de_get_monomorphized_copies(original_id)
self.get_monomorphized_copies(original_id)
.into_iter()
.map(|x| x.expect_function(span))
.collect::<Result<_, _>>()
}

fn de_insert_trait(&self, r#trait: TypedTraitDeclaration) -> DeclarationId {
fn insert_trait(&self, r#trait: TypedTraitDeclaration) -> DeclarationId {
let span = r#trait.name.span();
DeclarationId::new(self.slab.insert(DeclarationWrapper::Trait(r#trait)), span)
}

fn de_get_trait(
fn get_trait(
&self,
index: DeclarationId,
span: &Span,
) -> Result<TypedTraitDeclaration, CompileError> {
self.slab.get(*index).expect_trait(span)
}

fn de_insert_trait_fn(&self, trait_fn: TypedTraitFn) -> DeclarationId {
fn insert_trait_fn(&self, trait_fn: TypedTraitFn) -> DeclarationId {
let span = trait_fn.name.span();
DeclarationId::new(
self.slab.insert(DeclarationWrapper::TraitFn(trait_fn)),
span,
)
}

fn de_get_trait_fn(
fn get_trait_fn(
&self,
index: DeclarationId,
span: &Span,
Expand All @@ -137,116 +137,129 @@ impl DeclarationEngine {
)
}

fn de_get_trait_impl(
fn get_trait_impl(
&self,
index: DeclarationId,
span: &Span,
) -> Result<TypedImplTrait, CompileError> {
self.slab.get(*index).expect_trait_impl(span)
}

fn de_insert_struct(&self, r#struct: TypedStructDeclaration) -> DeclarationId {
fn insert_struct(&self, r#struct: TypedStructDeclaration) -> DeclarationId {
let span = r#struct.span();
DeclarationId::new(self.slab.insert(DeclarationWrapper::Struct(r#struct)), span)
}

fn de_get_struct(
fn get_struct(
&self,
index: DeclarationId,
span: &Span,
) -> Result<TypedStructDeclaration, CompileError> {
self.slab.get(*index).expect_struct(span)
}

fn de_add_monomorphized_struct_copy(
fn add_monomorphized_struct_copy(
&self,
original_id: DeclarationId,
new_copy: TypedStructDeclaration,
) {
let span = new_copy.span();
let new_id =
DeclarationId::new(self.slab.insert(DeclarationWrapper::Struct(new_copy)), span);
self.de_add_monomorphized_copy(original_id, new_id)
self.add_monomorphized_copy(original_id, new_id)
}

fn de_get_monomorphized_struct_copies(
fn get_monomorphized_struct_copies(
&self,
original_id: DeclarationId,
span: &Span,
) -> Result<Vec<TypedStructDeclaration>, CompileError> {
self.de_get_monomorphized_copies(original_id)
self.get_monomorphized_copies(original_id)
.into_iter()
.map(|x| x.expect_struct(span))
.collect::<Result<_, _>>()
}

fn de_insert_storage(&self, storage: TypedStorageDeclaration) -> DeclarationId {
fn insert_storage(&self, storage: TypedStorageDeclaration) -> DeclarationId {
let span = storage.span();
DeclarationId::new(self.slab.insert(DeclarationWrapper::Storage(storage)), span)
}

fn de_get_storage(
fn get_storage(
&self,
index: DeclarationId,
span: &Span,
) -> Result<TypedStorageDeclaration, CompileError> {
self.slab.get(*index).expect_storage(span)
}

fn insert_abi(&self, abi: TypedAbiDeclaration) -> DeclarationId {
let span = abi.span.clone();
DeclarationId::new(self.slab.insert(DeclarationWrapper::Abi(abi)), span)
}

fn get_abi(
&self,
index: DeclarationId,
span: &Span,
) -> Result<TypedAbiDeclaration, CompileError> {
self.slab.get(*index).expect_abi(span)
}
}

pub(crate) fn de_clear() {
DECLARATION_ENGINE.clear()
}

pub(crate) fn de_look_up_decl_id(index: DeclarationId) -> DeclarationWrapper {
DECLARATION_ENGINE.de_look_up_decl_id(index)
DECLARATION_ENGINE.look_up_decl_id(index)
}

pub(crate) fn de_insert_function(function: TypedFunctionDeclaration) -> DeclarationId {
DECLARATION_ENGINE.de_insert_function(function)
DECLARATION_ENGINE.insert_function(function)
}

pub(crate) fn de_get_function(
index: DeclarationId,
span: &Span,
) -> Result<TypedFunctionDeclaration, CompileError> {
DECLARATION_ENGINE.de_get_function(index, span)
DECLARATION_ENGINE.get_function(index, span)
}

pub(crate) fn de_add_monomorphized_function_copy(
original_id: DeclarationId,
new_copy: TypedFunctionDeclaration,
) {
DECLARATION_ENGINE.de_add_monomorphized_function_copy(original_id, new_copy);
DECLARATION_ENGINE.add_monomorphized_function_copy(original_id, new_copy);
}

pub(crate) fn de_get_monomorphized_function_copies(
original_id: DeclarationId,
span: &Span,
) -> Result<Vec<TypedFunctionDeclaration>, CompileError> {
DECLARATION_ENGINE.de_get_monomorphized_function_copies(original_id, span)
DECLARATION_ENGINE.get_monomorphized_function_copies(original_id, span)
}

pub(crate) fn de_insert_trait(r#trait: TypedTraitDeclaration) -> DeclarationId {
DECLARATION_ENGINE.de_insert_trait(r#trait)
DECLARATION_ENGINE.insert_trait(r#trait)
}

pub fn de_get_trait(
index: DeclarationId,
span: &Span,
) -> Result<TypedTraitDeclaration, CompileError> {
DECLARATION_ENGINE.de_get_trait(index, span)
DECLARATION_ENGINE.get_trait(index, span)
}

pub(crate) fn de_insert_trait_fn(trait_fn: TypedTraitFn) -> DeclarationId {
DECLARATION_ENGINE.de_insert_trait_fn(trait_fn)
DECLARATION_ENGINE.insert_trait_fn(trait_fn)
}

pub(crate) fn de_get_trait_fn(
index: DeclarationId,
span: &Span,
) -> Result<TypedTraitFn, CompileError> {
DECLARATION_ENGINE.de_get_trait_fn(index, span)
DECLARATION_ENGINE.get_trait_fn(index, span)
}

pub(crate) fn insert_trait_impl(trait_impl: TypedImplTrait) -> DeclarationId {
Expand All @@ -257,41 +270,49 @@ pub(crate) fn de_get_trait_impl(
index: DeclarationId,
span: &Span,
) -> Result<TypedImplTrait, CompileError> {
DECLARATION_ENGINE.de_get_trait_impl(index, span)
DECLARATION_ENGINE.get_trait_impl(index, span)
}

pub(crate) fn de_insert_struct(r#struct: TypedStructDeclaration) -> DeclarationId {
DECLARATION_ENGINE.de_insert_struct(r#struct)
DECLARATION_ENGINE.insert_struct(r#struct)
}

pub(crate) fn de_get_struct(
index: DeclarationId,
span: &Span,
) -> Result<TypedStructDeclaration, CompileError> {
DECLARATION_ENGINE.de_get_struct(index, span)
DECLARATION_ENGINE.get_struct(index, span)
}

pub(crate) fn de_add_monomorphized_struct_copy(
original_id: DeclarationId,
new_copy: TypedStructDeclaration,
) {
DECLARATION_ENGINE.de_add_monomorphized_struct_copy(original_id, new_copy);
DECLARATION_ENGINE.add_monomorphized_struct_copy(original_id, new_copy);
}

pub(crate) fn de_get_monomorphized_struct_copies(
original_id: DeclarationId,
span: &Span,
) -> Result<Vec<TypedStructDeclaration>, CompileError> {
DECLARATION_ENGINE.de_get_monomorphized_struct_copies(original_id, span)
DECLARATION_ENGINE.get_monomorphized_struct_copies(original_id, span)
}

pub(crate) fn de_insert_storage(storage: TypedStorageDeclaration) -> DeclarationId {
DECLARATION_ENGINE.de_insert_storage(storage)
DECLARATION_ENGINE.insert_storage(storage)
}

pub fn de_get_storage(
index: DeclarationId,
span: &Span,
) -> Result<TypedStorageDeclaration, CompileError> {
DECLARATION_ENGINE.de_get_storage(index, span)
DECLARATION_ENGINE.get_storage(index, span)
}

pub(crate) fn de_insert_abi(abi: TypedAbiDeclaration) -> DeclarationId {
DECLARATION_ENGINE.insert_abi(abi)
}

pub fn de_get_abi(index: DeclarationId, span: &Span) -> Result<TypedAbiDeclaration, CompileError> {
DECLARATION_ENGINE.get_abi(index, span)
}
22 changes: 20 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::{
TypedImplTrait, TypedStorageDeclaration, TypedStructDeclaration, TypedTraitDeclaration,
TypedTraitFn,
TypedAbiDeclaration, TypedImplTrait, TypedStorageDeclaration, TypedStructDeclaration,
TypedTraitDeclaration, TypedTraitFn,
},
type_system::{CopyTypes, TypeMapping},
CompileError, TypedFunctionDeclaration,
Expand All @@ -23,6 +23,7 @@ pub(crate) enum DeclarationWrapper {
TraitImpl(TypedImplTrait),
Struct(TypedStructDeclaration),
Storage(TypedStorageDeclaration),
Abi(TypedAbiDeclaration),
}

impl Default for DeclarationWrapper {
Expand All @@ -44,6 +45,7 @@ impl PartialEq for DeclarationWrapper {
(DeclarationWrapper::TraitImpl(l), DeclarationWrapper::TraitImpl(r)) => l == r,
(DeclarationWrapper::Struct(l), DeclarationWrapper::Struct(r)) => l == r,
(DeclarationWrapper::Storage(l), DeclarationWrapper::Storage(r)) => l == r,
(DeclarationWrapper::Abi(l), DeclarationWrapper::Abi(r)) => l == r,
_ => false,
}
}
Expand All @@ -65,6 +67,7 @@ impl CopyTypes for DeclarationWrapper {
DeclarationWrapper::TraitImpl(decl) => decl.copy_types(type_mapping),
DeclarationWrapper::Struct(decl) => decl.copy_types(type_mapping),
DeclarationWrapper::Storage(_) => {}
DeclarationWrapper::Abi(_) => {}
}
}
}
Expand All @@ -80,6 +83,7 @@ impl DeclarationWrapper {
DeclarationWrapper::TraitImpl(_) => "impl trait",
DeclarationWrapper::TraitFn(_) => "trait function",
DeclarationWrapper::Storage(_) => "storage",
DeclarationWrapper::Abi(_) => "abi",
}
}

Expand Down Expand Up @@ -172,4 +176,18 @@ impl DeclarationWrapper {
}),
}
}

pub(super) fn expect_abi(self, span: &Span) -> Result<TypedAbiDeclaration, CompileError> {
match self {
DeclarationWrapper::Abi(decl) => Ok(decl),
DeclarationWrapper::Unknown => Err(CompileError::Internal(
"did not expect to find unknown declaration",
span.clone(),
)),
_ => Err(CompileError::Internal(
"expected ABI definition",
span.clone(),
)),
}
}
}
Loading