cairo_lang_sierra/extensions/modules/
function_call.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
use itertools::chain;

use super::coupon::coupon_ty;
use crate::extensions::lib_func::{
    LibfuncSignature, OutputVarInfo, SignatureBasedConcreteLibfunc, SignatureSpecializationContext,
    SpecializationContext,
};
use crate::extensions::{
    NamedLibfunc, OutputVarReferenceInfo, SpecializationError, args_as_single_user_func,
};
use crate::program::{Function, FunctionSignature, GenericArg};

/// Returns the [OutputVarInfo] instances for the return types of the given function signature.
fn get_output_var_infos(
    context: &dyn SignatureSpecializationContext,
    signature: FunctionSignature,
) -> Result<Vec<OutputVarInfo>, SpecializationError> {
    let mut curr_stack_idx = 0;
    let mut get_stack_idx = || {
        let idx = curr_stack_idx;
        curr_stack_idx += 1;
        idx
    };

    signature
        .ret_types
        .iter()
        .map(|ty| {
            Ok(OutputVarInfo {
                ty: ty.clone(),
                ref_info: if context.get_type_info(ty.clone())?.zero_sized {
                    OutputVarReferenceInfo::ZeroSized
                } else {
                    OutputVarReferenceInfo::NewTempVar { idx: get_stack_idx() }
                },
            })
        })
        .collect::<Result<Vec<_>, _>>()
}

/// Libfunc used to call user functions.
#[derive(Default)]
pub struct FunctionCallLibfunc {}
impl NamedLibfunc for FunctionCallLibfunc {
    type Concrete = SignatureAndFunctionConcreteLibfunc;
    const STR_ID: &'static str = "function_call";

    fn specialize_signature(
        &self,
        context: &dyn SignatureSpecializationContext,
        args: &[GenericArg],
    ) -> Result<LibfuncSignature, SpecializationError> {
        let function_id = args_as_single_user_func(args)?;

        let signature = context.get_function_signature(&function_id)?;
        let ap_change = context.get_function_ap_change(&function_id)?;
        Ok(LibfuncSignature::new_non_branch(
            signature.param_types.clone(),
            get_output_var_infos(context, signature)?,
            ap_change,
        ))
    }

    fn specialize(
        &self,
        context: &dyn SpecializationContext,
        args: &[GenericArg],
    ) -> Result<Self::Concrete, SpecializationError> {
        let function_id = args_as_single_user_func(args)?;

        Ok(Self::Concrete {
            function: context.get_function(&function_id)?,
            signature: self.specialize_signature(context.upcast(), args)?,
        })
    }
}

pub struct SignatureAndFunctionConcreteLibfunc {
    pub function: Function,
    pub signature: LibfuncSignature,
}
impl SignatureBasedConcreteLibfunc for SignatureAndFunctionConcreteLibfunc {
    fn signature(&self) -> &LibfuncSignature {
        &self.signature
    }
}

/// Libfunc used to call user functions.
#[derive(Default)]
pub struct CouponCallLibfunc {}
impl NamedLibfunc for CouponCallLibfunc {
    type Concrete = SignatureAndFunctionConcreteLibfunc;
    const STR_ID: &'static str = "coupon_call";

    fn specialize_signature(
        &self,
        context: &dyn SignatureSpecializationContext,
        args: &[GenericArg],
    ) -> Result<LibfuncSignature, SpecializationError> {
        let function_id = args_as_single_user_func(args)?;

        let signature = context.get_function_signature(&function_id)?;
        let ap_change = context.get_function_ap_change(&function_id)?;

        let coupon_ty = coupon_ty(context, function_id.clone())?;
        Ok(LibfuncSignature::new_non_branch(
            chain!(signature.param_types.iter().cloned(), [coupon_ty]).collect(),
            get_output_var_infos(context, signature)?,
            ap_change,
        ))
    }

    fn specialize(
        &self,
        context: &dyn SpecializationContext,
        args: &[GenericArg],
    ) -> Result<Self::Concrete, SpecializationError> {
        let function_id = args_as_single_user_func(args)?;

        Ok(Self::Concrete {
            function: context.get_function(&function_id)?,
            signature: self.specialize_signature(context.upcast(), args)?,
        })
    }
}