Skip to content

Commit

Permalink
Implement Declaration::TraitFn.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Aug 4, 2024
1 parent 24e7ebf commit 8da3223
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sway-core/src/language/parsed/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub enum Declaration {
StorageDeclaration(ParsedDeclId<StorageDeclaration>),
TypeAliasDeclaration(ParsedDeclId<TypeAliasDeclaration>),
TraitTypeDeclaration(ParsedDeclId<TraitTypeDeclaration>),
TraitFnDeclaration(ParsedDeclId<TraitFn>),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -84,6 +85,7 @@ impl Declaration {
TraitTypeDeclaration(_) => "type",
FunctionDeclaration(_) => "function",
TraitDeclaration(_) => "trait",
TraitFnDeclaration(_) => "trait fn",
StructDeclaration(_) => "struct",
EnumDeclaration(_) => "enum",
EnumVariantDeclaration(_) => "enum variant",
Expand Down Expand Up @@ -111,6 +113,7 @@ impl Declaration {
StorageDeclaration(decl_id) => pe.get_storage(decl_id).span(),
TypeAliasDeclaration(decl_id) => pe.get_type_alias(decl_id).span(),
TraitTypeDeclaration(decl_id) => pe.get_trait_type(decl_id).span(),
TraitFnDeclaration(decl_id) => pe.get_trait_fn(decl_id).span(),
}
}

Expand Down Expand Up @@ -138,7 +141,8 @@ impl Declaration {
Declaration::ImplSelfOrTrait(_)
| Declaration::StorageDeclaration(_)
| Declaration::AbiDeclaration(_)
| Declaration::TraitTypeDeclaration(_) => Visibility::Public,
| Declaration::TraitTypeDeclaration(_)
| Declaration::TraitFnDeclaration(_) => Visibility::Public,
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions sway-core/src/language/parsed/declaration/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ pub struct TraitFn {
pub return_type: TypeArgument,
}

impl Spanned for TraitFn {
fn span(&self) -> sway_types::Span {
self.span.clone()
}
}

#[derive(Debug, Clone)]
pub struct TraitTypeDeclaration {
pub name: Ident,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl TyDecl {
let trait_type_decl = engines.pe().get_trait_type(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, trait_type_decl.name.clone(), decl)?;
}
parsed::Declaration::TraitFnDeclaration(decl_id) => {
let trait_fn_decl = engines.pe().get_trait_fn(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, trait_fn_decl.name.clone(), decl)?;
}
parsed::Declaration::EnumDeclaration(decl_id) => {
let enum_decl = engines.pe().get_enum(decl_id).as_ref().clone();
ctx.insert_parsed_symbol(handler, engines, enum_decl.name.clone(), decl)?;
Expand Down Expand Up @@ -609,6 +613,9 @@ impl TyDecl {
ctx.insert_symbol(handler, name, decl.clone())?;
decl
}
parsed::Declaration::TraitFnDeclaration(_decl_id) => {
unreachable!();
}
};

Ok(decl)
Expand Down
20 changes: 20 additions & 0 deletions sway-core/src/semantic_analysis/node_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ impl Dependencies {
let decl = engines.pe().get_trait_type(decl_id);
self.gather_from_type_decl(engines, &decl)
}
Declaration::TraitFnDeclaration(decl_id) => {
let decl = engines.pe().get_trait_fn(decl_id);
self.gather_from_trait_fn_decl(engines, &decl)
}
Declaration::FunctionDeclaration(decl_id) => {
let fn_decl = engines.pe().get_function(decl_id);
self.gather_from_fn_decl(engines, &fn_decl)
Expand Down Expand Up @@ -505,6 +509,18 @@ impl Dependencies {
}
}

fn gather_from_trait_fn_decl(self, engines: &Engines, fn_decl: &TraitFn) -> Self {
let TraitFn {
parameters,
return_type,
..
} = fn_decl;
self.gather_from_iter(parameters.iter(), |deps, param| {
deps.gather_from_type_argument(engines, &param.type_argument)
})
.gather_from_type_argument(engines, return_type)
}

fn gather_from_fn_decl(self, engines: &Engines, fn_decl: &FunctionDeclaration) -> Self {
let FunctionDeclaration {
parameters,
Expand Down Expand Up @@ -894,6 +910,10 @@ fn decl_name(engines: &Engines, decl: &Declaration) -> Option<DependentSymbol> {
let decl = engines.pe().get_trait_type(decl_id);
dep_sym(decl.name.clone())
}
Declaration::TraitFnDeclaration(decl_id) => {
let decl = engines.pe().get_trait_fn(decl_id);
dep_sym(decl.name.clone())
}
Declaration::StructDeclaration(decl_id) => {
let decl = engines.pe().get_struct(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 @@ -133,6 +133,7 @@ impl Parse for Declaration {
Declaration::StorageDeclaration(decl_id) => decl_id.parse(ctx),
Declaration::TypeAliasDeclaration(decl_id) => decl_id.parse(ctx),
Declaration::TraitTypeDeclaration(decl_id) => decl_id.parse(ctx),
Declaration::TraitFnDeclaration(decl_id) => decl_id.parse(ctx),
}
}
}
Expand Down

0 comments on commit 8da3223

Please sign in to comment.