sway_core/language/ty/declaration/
const_generic.rs1use crate::{
2 decl_engine::MaterializeConstGenerics,
4 language::{parsed::ConstGenericDeclaration, ty::TyExpression, CallPath},
5 semantic_analysis::{TypeCheckAnalysis, TypeCheckAnalysisContext},
6 SubstTypes,
7 TypeId,
8};
9use serde::{Deserialize, Serialize};
10use sway_error::handler::{ErrorEmitted, Handler};
11use sway_types::{BaseIdent, Ident, Named, Span, Spanned};
12
13use super::TyDeclParsedType;
14
15#[derive(Clone, Debug, Serialize, Deserialize)]
16pub struct TyConstGenericDecl {
17 pub call_path: CallPath,
18 pub return_type: TypeId,
19 pub span: Span,
20 pub value: Option<TyExpression>,
21}
22
23impl SubstTypes for TyConstGenericDecl {
24 fn subst_inner(&mut self, ctx: &crate::SubstTypesContext) -> crate::HasChanges {
25 self.return_type.subst(ctx)
26 }
27}
28
29impl MaterializeConstGenerics for TyConstGenericDecl {
30 fn materialize_const_generics(
31 &mut self,
32 _engines: &crate::Engines,
33 _handler: &Handler,
34 name: &str,
35 value: &TyExpression,
36 ) -> Result<(), ErrorEmitted> {
37 if self.call_path.suffix.as_str() == name {
38 assert!(self.value.is_none());
39 self.value = Some(value.clone());
40 }
41 Ok(())
42 }
43}
44
45impl TypeCheckAnalysis for TyConstGenericDecl {
46 fn type_check_analyze(
47 &self,
48 _handler: &Handler,
49 _ctx: &mut TypeCheckAnalysisContext,
50 ) -> Result<(), ErrorEmitted> {
51 Ok(())
52 }
53}
54
55impl TyConstGenericDecl {
56 pub fn name(&self) -> &BaseIdent {
57 &self.call_path.suffix
58 }
59}
60
61impl Named for TyConstGenericDecl {
62 fn name(&self) -> &Ident {
63 &self.call_path.suffix
64 }
65}
66
67impl Spanned for TyConstGenericDecl {
68 fn span(&self) -> Span {
69 self.span.clone()
70 }
71}
72
73impl TyDeclParsedType for TyConstGenericDecl {
74 type ParsedType = ConstGenericDeclaration;
75}