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
pub(crate) mod compile;
pub mod const_eval;
mod convert;
mod function;
mod lexical_map;
mod purity;
pub mod storage;
mod types;

use sway_error::error::CompileError;
use sway_ir::Context;
use sway_types::span::Span;

pub(crate) use purity::{check_function_purity, PurityEnv};

use crate::language::ty;

pub fn compile_program(program: ty::TyProgram) -> Result<Context, CompileError> {
    let ty::TyProgram {
        kind,
        root,
        logged_types,
        ..
    } = program;

    let mut ctx = Context::default();
    match kind {
        ty::TyProgramKind::Script {
            main_function,
            declarations,
        }
        | ty::TyProgramKind::Predicate {
            main_function,
            declarations,
            // predicates and scripts have the same codegen, their only difference is static
            // type-check time checks.
            // Predicates are not allowed to use logs so no need to pass in `logged_types` here
        } => compile::compile_script(
            &mut ctx,
            main_function,
            &root.namespace,
            declarations,
            &logged_types
                .into_iter()
                .map(|(log_id, type_id)| (type_id, log_id))
                .collect(),
        ),
        ty::TyProgramKind::Contract {
            abi_entries,
            declarations,
        } => compile::compile_contract(
            &mut ctx,
            abi_entries,
            &root.namespace,
            declarations,
            &logged_types
                .into_iter()
                .map(|(log_id, type_id)| (type_id, log_id))
                .collect(),
        ),
        ty::TyProgramKind::Library { .. } => unimplemented!("compile library to ir"),
    }?;
    ctx.verify()
        .map_err(|ir_error| CompileError::InternalOwned(ir_error.to_string(), Span::dummy()))
}