From c0727bd07390abc009c24e1c0f02a33cfb0d0b94 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Mon, 27 May 2024 15:31:42 +0100 Subject: [PATCH] Add `EnumVariant` parsed declaration. --- sway-core/src/decl_engine/parsed_engine.rs | 19 +++++++++++++++++-- sway-core/src/language/parsed/declaration.rs | 12 +++++++++++- .../ast_node/declaration/declaration.rs | 5 +++++ .../semantic_analysis/node_dependencies.rs | 2 ++ sway-lsp/src/traverse/parsed_tree.rs | 1 + 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/sway-core/src/decl_engine/parsed_engine.rs b/sway-core/src/decl_engine/parsed_engine.rs index a529739b5fa..7357d4c3158 100644 --- a/sway-core/src/decl_engine/parsed_engine.rs +++ b/sway-core/src/decl_engine/parsed_engine.rs @@ -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, }, }; @@ -28,6 +28,7 @@ pub struct ParsedDeclEngine { abi_slab: ConcurrentSlab, constant_slab: ConcurrentSlab, enum_slab: ConcurrentSlab, + enum_variant_slab: ConcurrentSlab, type_alias_slab: ConcurrentSlab, } @@ -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 { @@ -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 { @@ -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(&self, index: &I) -> Arc + where + ParsedDeclEngine: ParsedDeclEngineGet, + { + self.get(index) + } + /// Friendly helper method for calling the `get` method from the /// implementation of [ParsedDeclEngineGet] for [ParsedDeclEngine] /// diff --git a/sway-core/src/language/parsed/declaration.rs b/sway-core/src/language/parsed/declaration.rs index a90dbfa481d..bfe8257c2d3 100644 --- a/sway-core/src/language/parsed/declaration.rs +++ b/sway-core/src/language/parsed/declaration.rs @@ -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::*; @@ -39,6 +39,7 @@ pub enum Declaration { TraitDeclaration(ParsedDeclId), StructDeclaration(ParsedDeclId), EnumDeclaration(ParsedDeclId), + EnumVariantDeclaration(EnumVariantDeclaration), ImplTrait(ParsedDeclId), ImplSelf(ParsedDeclId), AbiDeclaration(ParsedDeclId), @@ -48,6 +49,13 @@ pub enum Declaration { TraitTypeDeclaration(ParsedDeclId), } +#[derive(Debug, Clone)] +pub struct EnumVariantDeclaration { + pub enum_ref: ParsedDeclId, + 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 { @@ -71,6 +79,7 @@ impl Declaration { TraitDeclaration(_) => "trait", StructDeclaration(_) => "struct", EnumDeclaration(_) => "enum", + EnumVariantDeclaration(_) => "enum variant", ImplSelf(_) => "impl self", ImplTrait(_) => "impl trait", AbiDeclaration(_) => "abi", @@ -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(), diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs index 4ca4a44f19c..8d146683dd2 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -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); @@ -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(); diff --git a/sway-core/src/semantic_analysis/node_dependencies.rs b/sway-core/src/semantic_analysis/node_dependencies.rs index 2112b57c1a9..735b625d7bd 100644 --- a/sway-core/src/semantic_analysis/node_dependencies.rs +++ b/sway-core/src/semantic_analysis/node_dependencies.rs @@ -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| { @@ -891,6 +892,7 @@ fn decl_name(engines: &Engines, decl: &Declaration) -> Option { 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()) diff --git a/sway-lsp/src/traverse/parsed_tree.rs b/sway-lsp/src/traverse/parsed_tree.rs index b6a173afbff..21cf83c954d 100644 --- a/sway-lsp/src/traverse/parsed_tree.rs +++ b/sway-lsp/src/traverse/parsed_tree.rs @@ -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),