sway_core/semantic_analysis/
program.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::{
    language::{
        parsed::ParseProgram,
        ty::{self, TyProgram},
    },
    metadata::MetadataManager,
    semantic_analysis::{
        namespace::{self},
        TypeCheckContext,
    },
    BuildConfig, Engines,
};
use sway_error::handler::{ErrorEmitted, Handler};
use sway_features::ExperimentalFeatures;
use sway_ir::{Context, Module};

use super::{
    symbol_collection_context::SymbolCollectionContext, TypeCheckAnalysis,
    TypeCheckAnalysisContext, TypeCheckFinalization, TypeCheckFinalizationContext,
};

impl TyProgram {
    /// Collects the given parsed program to produce a symbol maps.
    ///
    /// The given `initial_namespace` acts as an initial state for each module within this program.
    /// It should contain a submodule for each library package dependency.
    pub fn collect(
        handler: &Handler,
        engines: &Engines,
        parsed: &ParseProgram,
        namespace: namespace::Namespace,
    ) -> Result<SymbolCollectionContext, ErrorEmitted> {
        let mut ctx = SymbolCollectionContext::new(namespace);
        let ParseProgram { root, kind: _ } = parsed;

        ty::TyModule::collect(handler, engines, &mut ctx, root)?;
        Ok(ctx)
    }

    /// Type-check the given parsed program to produce a typed program.
    ///
    /// The given `namespace` acts as an initial state for each module within this program.
    /// It should contain a submodule for each library package dependency.
    #[allow(clippy::too_many_arguments)]
    pub fn type_check(
        handler: &Handler,
        engines: &Engines,
        parsed: &ParseProgram,
        collection_ctx: &mut SymbolCollectionContext,
        mut namespace: namespace::Namespace,
        package_name: &str,
        build_config: Option<&BuildConfig>,
        experimental: ExperimentalFeatures,
    ) -> Result<Self, ErrorEmitted> {
        let mut ctx =
            TypeCheckContext::from_root(&mut namespace, collection_ctx, engines, experimental)
                .with_kind(parsed.kind);

        let ParseProgram { root, kind } = parsed;

        let root = ty::TyModule::type_check(
            handler,
            ctx.by_ref(),
            engines,
            parsed.kind,
            root,
            build_config,
        )?;

        let (kind, declarations, configurables) = Self::validate_root(
            handler,
            engines,
            &root,
            *kind,
            package_name,
            ctx.experimental,
        )?;

        let program = TyProgram {
            kind,
            root: (*root).clone(),
            declarations,
            configurables,
            storage_slots: vec![],
            logged_types: vec![],
            messages_types: vec![],
        };

        Ok(program)
    }

    pub(crate) fn get_typed_program_with_initialized_storage_slots(
        self,
        handler: &Handler,
        engines: &Engines,
        context: &mut Context,
        md_mgr: &mut MetadataManager,
        module: Module,
    ) -> Result<Self, ErrorEmitted> {
        let decl_engine = engines.de();
        match &self.kind {
            ty::TyProgramKind::Contract { .. } => {
                let storage_decl = self
                    .declarations
                    .iter()
                    .find(|decl| matches!(decl, ty::TyDecl::StorageDecl { .. }));

                // Expecting at most a single storage declaration
                match storage_decl {
                    Some(ty::TyDecl::StorageDecl(ty::StorageDecl { decl_id, .. })) => {
                        let decl = decl_engine.get_storage(decl_id);
                        let mut storage_slots = decl.get_initialized_storage_slots(
                            handler, engines, context, md_mgr, module,
                        )?;
                        // Sort the slots to standardize the output. Not strictly required by the
                        // spec.
                        storage_slots.sort();
                        Ok(Self {
                            storage_slots,
                            ..self
                        })
                    }
                    _ => Ok(Self {
                        storage_slots: vec![],
                        ..self
                    }),
                }
            }
            _ => Ok(Self {
                storage_slots: vec![],
                ..self
            }),
        }
    }
}

impl TypeCheckAnalysis for TyProgram {
    fn type_check_analyze(
        &self,
        handler: &Handler,
        ctx: &mut TypeCheckAnalysisContext,
    ) -> Result<(), ErrorEmitted> {
        for node in self.root.all_nodes.iter() {
            node.type_check_analyze(handler, ctx)?;
        }
        Ok(())
    }
}

impl TypeCheckFinalization for TyProgram {
    fn type_check_finalize(
        &mut self,
        handler: &Handler,
        ctx: &mut TypeCheckFinalizationContext,
    ) -> Result<(), ErrorEmitted> {
        handler.scope(|handler| {
            for node in self.root.all_nodes.iter_mut() {
                let _ = node.type_check_finalize(handler, ctx);
            }
            Ok(())
        })
    }
}