diff --git a/sway-core/src/decl_engine/engine.rs b/sway-core/src/decl_engine/engine.rs index 8f992f119bd..6dca672b734 100644 --- a/sway-core/src/decl_engine/engine.rs +++ b/sway-core/src/decl_engine/engine.rs @@ -13,9 +13,10 @@ use crate::{ engine_threading::*, language::{ parsed::{ - AbiDeclaration, ConfigurableDeclaration, ConstantDeclaration, EnumDeclaration, - FunctionDeclaration, ImplSelfOrTrait, StorageDeclaration, StructDeclaration, - TraitDeclaration, TraitFn, TraitTypeDeclaration, TypeAliasDeclaration, + AbiDeclaration, ConfigurableDeclaration, ConstantDeclaration, Declaration, + EnumDeclaration, FunctionDeclaration, ImplSelfOrTrait, StorageDeclaration, + StructDeclaration, TraitDeclaration, TraitFn, TraitTypeDeclaration, + TypeAliasDeclaration, }, ty::{ self, TyAbiDecl, TyConfigurableDecl, TyConstantDecl, TyDeclParsedType, TyEnumDecl, @@ -122,6 +123,13 @@ where fn get_parsed_decl_id(&self, decl_id: &DeclId) -> Option>; } +pub trait DeclEngineGetParsedDecl +where + T: TyDeclParsedType, +{ + fn get_parsed_decl(&self, decl_id: &DeclId) -> Option; +} + pub trait DeclEngineInsert where T: Named + Spanned + TyDeclParsedType, @@ -288,6 +296,82 @@ decl_engine_parsed_decl_id!(configurable_parsed_decl_id_map, ty::TyConfigurableD decl_engine_parsed_decl_id!(enum_parsed_decl_id_map, ty::TyEnumDecl); decl_engine_parsed_decl_id!(type_alias_parsed_decl_id_map, ty::TyTypeAliasDecl); +macro_rules! decl_engine_parsed_decl { + ($slab:ident, $decl:ty, $ctor:expr) => { + impl DeclEngineGetParsedDecl<$decl> for DeclEngine { + fn get_parsed_decl(&self, decl_id: &DeclId<$decl>) -> Option { + let parsed_decl_id_map = self.$slab.read(); + if let Some(parsed_decl_id) = parsed_decl_id_map.get(&decl_id) { + return Some($ctor(parsed_decl_id.clone())); + } else { + None + } + } + } + }; +} + +decl_engine_parsed_decl!( + function_parsed_decl_id_map, + ty::TyFunctionDecl, + Declaration::FunctionDeclaration +); +decl_engine_parsed_decl!( + trait_parsed_decl_id_map, + ty::TyTraitDecl, + Declaration::TraitDeclaration +); +decl_engine_parsed_decl!( + trait_fn_parsed_decl_id_map, + ty::TyTraitFn, + Declaration::TraitFnDeclaration +); +decl_engine_parsed_decl!( + trait_type_parsed_decl_id_map, + ty::TyTraitType, + Declaration::TraitTypeDeclaration +); +decl_engine_parsed_decl!( + impl_self_or_trait_parsed_decl_id_map, + ty::TyImplSelfOrTrait, + Declaration::ImplSelfOrTrait +); +decl_engine_parsed_decl!( + struct_parsed_decl_id_map, + ty::TyStructDecl, + Declaration::StructDeclaration +); +decl_engine_parsed_decl!( + storage_parsed_decl_id_map, + ty::TyStorageDecl, + Declaration::StorageDeclaration +); +decl_engine_parsed_decl!( + abi_parsed_decl_id_map, + ty::TyAbiDecl, + Declaration::AbiDeclaration +); +decl_engine_parsed_decl!( + constant_parsed_decl_id_map, + ty::TyConstantDecl, + Declaration::ConstantDeclaration +); +decl_engine_parsed_decl!( + configurable_parsed_decl_id_map, + ty::TyConfigurableDecl, + Declaration::ConfigurableDeclaration +); +decl_engine_parsed_decl!( + enum_parsed_decl_id_map, + ty::TyEnumDecl, + Declaration::EnumDeclaration +); +decl_engine_parsed_decl!( + type_alias_parsed_decl_id_map, + ty::TyTypeAliasDecl, + Declaration::TypeAliasDeclaration +); + macro_rules! decl_engine_replace { ($slab:ident, $decl:ty) => { impl DeclEngineReplace<$decl> for DeclEngine { diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index e3adb11ce4e..7cb16bebd5b 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -12,7 +12,7 @@ use sway_types::{Ident, Named, Span, Spanned}; use crate::{ decl_engine::*, engine_threading::*, - language::{ty::*, Visibility}, + language::{parsed::Declaration, ty::*, Visibility}, type_system::*, types::*, }; @@ -496,6 +496,26 @@ impl GetDeclIdent for TyDecl { } impl TyDecl { + pub(crate) fn get_parsed_decl(&self, decl_engine: &DeclEngine) -> Option { + match self { + TyDecl::VariableDecl(_decl) => None, + TyDecl::ConstantDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + TyDecl::ConfigurableDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + TyDecl::TraitTypeDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + TyDecl::FunctionDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + TyDecl::TraitDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + TyDecl::StructDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + TyDecl::EnumDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + TyDecl::EnumVariantDecl(decl) => decl_engine.get_parsed_decl(decl.enum_ref.id()), + TyDecl::ImplSelfOrTrait(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + TyDecl::AbiDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + TyDecl::GenericTypeForFunctionScope(_data) => None, + TyDecl::ErrorRecovery(_, _) => None, + TyDecl::StorageDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + TyDecl::TypeAliasDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id), + } + } + /// Retrieves the declaration as a `DeclId`. /// /// Returns an error if `self` is not the [TyDecl][EnumDecl] variant.