Skip to content

Commit

Permalink
Add EnumVariant parsed declaration.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed May 27, 2024
1 parent 1a932be commit c0727bd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
19 changes: 17 additions & 2 deletions sway-core/src/decl_engine/parsed_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::{
concurrent_slab::ConcurrentSlab,
decl_engine::*,
language::parsed::{
AbiDeclaration, ConstantDeclaration, EnumDeclaration, FunctionDeclaration, ImplSelf,
ImplTrait, StorageDeclaration, StructDeclaration, TraitDeclaration, TraitFn,
AbiDeclaration, ConstantDeclaration, EnumDeclaration, EnumVariant, FunctionDeclaration,
ImplSelf, ImplTrait, StorageDeclaration, StructDeclaration, TraitDeclaration, TraitFn,
TraitTypeDeclaration, TypeAliasDeclaration, VariableDeclaration,
},
};
Expand All @@ -28,6 +28,7 @@ pub struct ParsedDeclEngine {
abi_slab: ConcurrentSlab<AbiDeclaration>,
constant_slab: ConcurrentSlab<ConstantDeclaration>,
enum_slab: ConcurrentSlab<EnumDeclaration>,
enum_variant_slab: ConcurrentSlab<EnumVariant>,
type_alias_slab: ConcurrentSlab<TypeAliasDeclaration>,
}

Expand Down Expand Up @@ -73,6 +74,7 @@ decl_engine_get!(storage_slab, StorageDeclaration);
decl_engine_get!(abi_slab, AbiDeclaration);
decl_engine_get!(constant_slab, ConstantDeclaration);
decl_engine_get!(enum_slab, EnumDeclaration);
decl_engine_get!(enum_variant_slab, EnumVariant);
decl_engine_get!(type_alias_slab, TypeAliasDeclaration);

macro_rules! decl_engine_insert {
Expand Down Expand Up @@ -102,6 +104,7 @@ decl_engine_insert!(storage_slab, StorageDeclaration);
decl_engine_insert!(abi_slab, AbiDeclaration);
decl_engine_insert!(constant_slab, ConstantDeclaration);
decl_engine_insert!(enum_slab, EnumDeclaration);
decl_engine_insert!(enum_variant_slab, EnumVariant);
decl_engine_insert!(type_alias_slab, TypeAliasDeclaration);

macro_rules! decl_engine_clear {
Expand Down Expand Up @@ -304,6 +307,18 @@ impl ParsedDeclEngine {
self.get(index)
}

/// Friendly helper method for calling the `get` method from the
/// implementation of [ParsedDeclEngineGet] for [ParsedDeclEngine]
///
/// Calling [ParsedDeclEngine][get] directly is equivalent to this method, but
/// this method adds additional syntax that some users may find helpful.
pub fn get_enum_variant<I>(&self, index: &I) -> Arc<EnumVariant>
where
ParsedDeclEngine: ParsedDeclEngineGet<I, EnumVariant>,
{
self.get(index)
}

/// Friendly helper method for calling the `get` method from the
/// implementation of [ParsedDeclEngineGet] for [ParsedDeclEngine]
///
Expand Down
12 changes: 11 additions & 1 deletion sway-core/src/language/parsed/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use r#enum::*;
pub use r#struct::*;
pub use r#trait::*;
pub use storage::*;
use sway_types::Spanned;
use sway_types::{Ident, Span, Spanned};
pub use type_alias::*;
pub use variable::*;

Expand All @@ -39,6 +39,7 @@ pub enum Declaration {
TraitDeclaration(ParsedDeclId<TraitDeclaration>),
StructDeclaration(ParsedDeclId<StructDeclaration>),
EnumDeclaration(ParsedDeclId<EnumDeclaration>),
EnumVariantDeclaration(EnumVariantDeclaration),
ImplTrait(ParsedDeclId<ImplTrait>),
ImplSelf(ParsedDeclId<ImplSelf>),
AbiDeclaration(ParsedDeclId<AbiDeclaration>),
Expand All @@ -48,6 +49,13 @@ pub enum Declaration {
TraitTypeDeclaration(ParsedDeclId<TraitTypeDeclaration>),
}

#[derive(Debug, Clone)]
pub struct EnumVariantDeclaration {
pub enum_ref: ParsedDeclId<EnumDeclaration>,
pub variant_name: Ident,
pub variant_decl_span: Span,
}

impl Declaration {
/// Checks if this `Declaration` is a test.
pub(crate) fn is_test(&self, engines: &Engines) -> bool {
Expand All @@ -71,6 +79,7 @@ impl Declaration {
TraitDeclaration(_) => "trait",
StructDeclaration(_) => "struct",
EnumDeclaration(_) => "enum",
EnumVariantDeclaration(_) => "enum variant",
ImplSelf(_) => "impl self",
ImplTrait(_) => "impl trait",
AbiDeclaration(_) => "abi",
Expand All @@ -89,6 +98,7 @@ impl Declaration {
TraitDeclaration(decl_id) => pe.get_trait(decl_id).span(),
StructDeclaration(decl_id) => pe.get_struct(decl_id).span(),
EnumDeclaration(decl_id) => pe.get_enum(decl_id).span(),
EnumVariantDeclaration(decl) => decl.variant_decl_span.clone(),
ImplTrait(decl_id) => pe.get_impl_trait(decl_id).span(),
ImplSelf(decl_id) => pe.get_impl_self(decl_id).span(),
AbiDeclaration(decl_id) => pe.get_abi(decl_id).span(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl TyDecl {
let enum_decl = engines.pe().get_enum(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, enum_decl.name.clone(), decl)?;
}
parsed::Declaration::EnumVariantDeclaration(_decl) => {}
parsed::Declaration::FunctionDeclaration(decl_id) => {
let fn_decl = engines.pe().get_function(decl_id);
let _ = ctx.insert_parsed_symbol(handler, engines, fn_decl.name.clone(), decl);
Expand Down Expand Up @@ -196,6 +197,10 @@ impl TyDecl {

decl
}
parsed::Declaration::EnumVariantDeclaration(_decl) => {
// Type-checked above as part of the containing enum.
unreachable!()
}
parsed::Declaration::FunctionDeclaration(decl_id) => {
let fn_decl = engines.pe().get_function(&decl_id);
let span = fn_decl.span.clone();
Expand Down
2 changes: 2 additions & 0 deletions sway-core/src/semantic_analysis/node_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ impl Dependencies {
})
.gather_from_type_parameters(type_parameters)
}
Declaration::EnumVariantDeclaration(_decl) => unreachable!(),
Declaration::TraitDeclaration(decl_id) => {
let trait_decl = engines.pe().get_trait(decl_id);
self.gather_from_iter(trait_decl.supertraits.iter(), |deps, sup| {
Expand Down Expand Up @@ -891,6 +892,7 @@ fn decl_name(engines: &Engines, decl: &Declaration) -> Option<DependentSymbol> {
let decl = engines.pe().get_enum(decl_id);
dep_sym(decl.name.clone())
}
Declaration::EnumVariantDeclaration(_decl) => None,
Declaration::TraitDeclaration(decl_id) => {
let decl = engines.pe().get_trait(decl_id);
dep_sym(decl.name.clone())
Expand Down
1 change: 1 addition & 0 deletions sway-lsp/src/traverse/parsed_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl Parse for Declaration {
Declaration::TraitDeclaration(decl_id) => decl_id.parse(ctx),
Declaration::StructDeclaration(decl_id) => decl_id.parse(ctx),
Declaration::EnumDeclaration(decl_id) => decl_id.parse(ctx),
Declaration::EnumVariantDeclaration(_decl) => unreachable!(),
Declaration::ImplTrait(decl_id) => decl_id.parse(ctx),
Declaration::ImplSelf(decl_id) => decl_id.parse(ctx),
Declaration::AbiDeclaration(decl_id) => decl_id.parse(ctx),
Expand Down

0 comments on commit c0727bd

Please sign in to comment.