sway_core/language/parsed/declaration/
impl_trait.rs1use super::{ConstantDeclaration, FunctionDeclaration, TraitTypeDeclaration};
2use crate::{
3 decl_engine::{parsed_id::ParsedDeclId, ParsedInterfaceDeclId},
4 engine_threading::{
5 DebugWithEngines, EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext,
6 },
7 language::CallPath,
8 type_system::GenericArgument,
9 Engines, GenericTypeArgument, TypeParameter,
10};
11
12use sway_types::{span::Span, Named, Spanned};
13
14#[derive(Debug, Clone)]
15pub enum ImplItem {
16 Fn(ParsedDeclId<FunctionDeclaration>),
17 Constant(ParsedDeclId<ConstantDeclaration>),
18 Type(ParsedDeclId<TraitTypeDeclaration>),
19}
20
21impl ImplItem {
22 pub fn span(&self, engines: &Engines) -> Span {
23 match self {
24 ImplItem::Fn(id) => engines.pe().get_function(id).span(),
25 ImplItem::Constant(id) => engines.pe().get_constant(id).span(),
26 ImplItem::Type(id) => engines.pe().get_trait_type(id).span(),
27 }
28 }
29}
30
31impl EqWithEngines for ImplItem {}
32impl PartialEqWithEngines for ImplItem {
33 fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
34 match (self, other) {
35 (ImplItem::Fn(lhs), ImplItem::Fn(rhs)) => PartialEqWithEngines::eq(lhs, rhs, ctx),
36 (ImplItem::Constant(lhs), ImplItem::Constant(rhs)) => {
37 PartialEqWithEngines::eq(lhs, rhs, ctx)
38 }
39 (ImplItem::Type(lhs), ImplItem::Type(rhs)) => PartialEqWithEngines::eq(lhs, rhs, ctx),
40 _ => false,
41 }
42 }
43}
44
45impl DebugWithEngines for ImplItem {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>, engines: &Engines) -> std::fmt::Result {
47 match self {
48 ImplItem::Fn(decl_id) => {
49 let decl = engines.pe().get_function(decl_id);
50 f.write_fmt(format_args!("{:?}", engines.help_out(decl)))
51 }
52 ImplItem::Constant(decl_id) => {
53 let decl = engines.pe().get_constant(decl_id);
54 f.write_fmt(format_args!("{:?}", engines.help_out(decl)))
55 }
56 ImplItem::Type(decl_id) => {
57 let decl = engines.pe().get_trait_type(decl_id);
58 f.write_fmt(format_args!("{:?}", engines.help_out(decl)))
59 }
60 }
61 }
62}
63
64#[derive(Debug, Clone)]
70pub struct ImplSelfOrTrait {
71 pub is_self: bool,
72 pub impl_type_parameters: Vec<TypeParameter>,
73 pub trait_name: CallPath,
74 pub trait_type_arguments: Vec<GenericArgument>,
75 pub trait_decl_ref: Option<ParsedInterfaceDeclId>,
76 pub implementing_for: GenericTypeArgument,
77 pub items: Vec<ImplItem>,
78 pub(crate) block_span: Span,
80}
81
82impl EqWithEngines for ImplSelfOrTrait {}
83impl PartialEqWithEngines for ImplSelfOrTrait {
84 fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
85 self.impl_type_parameters
86 .eq(&other.impl_type_parameters, ctx)
87 && self.trait_name == other.trait_name
88 && self
89 .trait_type_arguments
90 .eq(&other.trait_type_arguments, ctx)
91 && self.implementing_for.eq(&other.implementing_for, ctx)
92 && self.items.eq(&other.items, ctx)
93 && self.block_span == other.block_span
94 }
95}
96
97impl Named for ImplSelfOrTrait {
98 fn name(&self) -> &sway_types::BaseIdent {
99 &self.trait_name.suffix
100 }
101}
102
103impl Spanned for ImplSelfOrTrait {
104 fn span(&self) -> sway_types::Span {
105 self.block_span.clone()
106 }
107}
108
109impl DebugWithEngines for ImplSelfOrTrait {
110 fn fmt(&self, f: &mut std::fmt::Formatter<'_>, engines: &Engines) -> std::fmt::Result {
111 if self.is_self {
112 f.write_fmt(format_args!(
113 "impl {}",
114 engines.help_out(self.implementing_for.clone())
115 ))
116 } else {
117 f.write_fmt(format_args!(
118 "impl {} for {:?}",
119 self.trait_name,
120 engines.help_out(self.implementing_for.clone())
121 ))
122 }
123 }
124}