Skip to main content

sway_core/language/ty/
code_block.rs

1use crate::{
2    decl_engine::*, engine_threading::*, language::ty::*, semantic_analysis::TypeCheckContext,
3    transform::AllowDeprecatedState, type_system::*, HasChanges,
4};
5use serde::{Deserialize, Serialize};
6use std::hash::Hasher;
7use sway_error::handler::{ErrorEmitted, Handler};
8use sway_types::Span;
9
10#[derive(Clone, Debug, Serialize, Deserialize)]
11pub struct TyCodeBlock {
12    pub contents: Vec<TyAstNode>,
13    pub(crate) whole_block_span: Span,
14}
15
16impl TyCodeBlock {
17    pub(crate) fn check_deprecated(
18        &self,
19        engines: &Engines,
20        handler: &Handler,
21        allow_deprecated: &mut AllowDeprecatedState,
22    ) {
23        for n in self.contents.iter() {
24            n.check_deprecated(engines, handler, allow_deprecated);
25        }
26    }
27}
28
29impl Default for TyCodeBlock {
30    fn default() -> Self {
31        Self {
32            contents: Default::default(),
33            whole_block_span: Span::dummy(),
34        }
35    }
36}
37
38impl EqWithEngines for TyCodeBlock {}
39impl PartialEqWithEngines for TyCodeBlock {
40    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
41        self.contents.eq(&other.contents, ctx)
42    }
43}
44
45impl HashWithEngines for TyCodeBlock {
46    fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
47        let TyCodeBlock { contents, .. } = self;
48        contents.hash(state, engines);
49    }
50}
51
52impl SubstTypes for TyCodeBlock {
53    fn subst_inner(&mut self, ctx: &SubstTypesContext) -> HasChanges {
54        self.contents.subst(ctx)
55    }
56}
57
58impl ReplaceDecls for TyCodeBlock {
59    fn replace_decls_inner(
60        &mut self,
61        decl_mapping: &DeclMapping,
62        handler: &Handler,
63        ctx: &mut TypeCheckContext,
64    ) -> Result<HasChanges, ErrorEmitted> {
65        handler.scope(|handler| {
66            let mut has_changes = HasChanges::No;
67            for node in self.contents.iter_mut() {
68                if let Ok(r) = node.replace_decls(decl_mapping, handler, ctx) {
69                    has_changes |= r;
70                }
71            }
72            Ok(has_changes)
73        })
74    }
75}
76
77impl UpdateConstantExpression for TyCodeBlock {
78    fn update_constant_expression(
79        &mut self,
80        engines: &Engines,
81        implementing_type: &TyDecl,
82    ) -> HasChanges {
83        self.contents.iter_mut().fold(HasChanges::No, |acc, x| {
84            acc | x.update_constant_expression(engines, implementing_type)
85        })
86    }
87}
88
89impl MaterializeConstGenerics for TyCodeBlock {
90    fn materialize_const_generics(
91        &mut self,
92        engines: &Engines,
93        handler: &Handler,
94        name: &str,
95        value: &TyExpression,
96    ) -> Result<HasChanges, ErrorEmitted> {
97        let mut has_changes = HasChanges::No;
98        for x in self.contents.iter_mut() {
99            has_changes |= x.materialize_const_generics(engines, handler, name, value)?;
100        }
101        Ok(has_changes)
102    }
103}