Skip to main content

rib/type_inference/
inferred_expr.rs

1use crate::call_type::CallType;
2use crate::rib_type_error::RibTypeErrorInternal;
3use crate::{
4    visit_post_order_rev_mut, ComponentDependency, CustomInstanceSpec, DynamicParsedFunctionName,
5    Expr, FunctionName, GlobalVariableTypeSpec,
6};
7use std::collections::HashSet;
8
9#[derive(Debug, Clone)]
10pub struct InferredExpr(Expr);
11
12impl InferredExpr {
13    pub fn get_expr(&self) -> &Expr {
14        &self.0
15    }
16
17    pub fn from_expr(
18        expr: Expr,
19        component_dependency: &ComponentDependency,
20        global_variable_type_spec: &Vec<GlobalVariableTypeSpec>,
21        custom_instance_spec: &[CustomInstanceSpec],
22    ) -> Result<InferredExpr, RibTypeErrorInternal> {
23        let mut mutable_expr = expr;
24
25        mutable_expr.infer_types(
26            component_dependency,
27            global_variable_type_spec,
28            custom_instance_spec,
29        )?;
30
31        Ok(InferredExpr(mutable_expr))
32    }
33
34    // Only a fully inferred Rib can reliably tell us what are the exact
35    // function calls.
36    pub fn worker_invoke_calls(&self) -> Vec<DynamicParsedFunctionName> {
37        let mut expr = self.0.clone();
38        let mut worker_calls = vec![];
39        visit_post_order_rev_mut(&mut expr, &mut |expr| {
40            if let Expr::Call {
41                call_type: CallType::Function { function_name, .. },
42                ..
43            } = expr
44            {
45                worker_calls.push(function_name.clone());
46            }
47        });
48
49        worker_calls
50    }
51
52    pub fn worker_invoke_registry_keys(&self) -> HashSet<FunctionName> {
53        let worker_calls = self.worker_invoke_calls();
54
55        let mut registry_keys = HashSet::new();
56
57        for call in worker_calls {
58            let keys = FunctionName::from_dynamic_parsed_function_name(&call);
59            registry_keys.insert(keys);
60        }
61
62        registry_keys
63    }
64}