Skip to main content

rib/
call_type.rs

1// Copyright 2024-2025 Golem Cloud
2//
3// Licensed under the Golem Source License v1.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://license.golem.cloud/LICENSE
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{ComponentDependencyKey, DynamicParsedFunctionName, Expr};
16use crate::{FullyQualifiedResourceConstructor, VariableId};
17use std::fmt::Display;
18
19#[derive(Debug, Hash, PartialEq, Eq, Clone, Ord, PartialOrd)]
20pub enum CallType {
21    Function {
22        component_info: Option<ComponentDependencyKey>,
23        // as compilation progress the function call is expected to a instance_identifier
24        // and will be always `Some`.
25        instance_identifier: Option<Box<InstanceIdentifier>>,
26        // TODO; a dynamic-parsed-function-name can be replaced by ParsedFunctionName
27        // after the introduction of non-lazy resource constructor.
28        function_name: DynamicParsedFunctionName,
29    },
30    VariantConstructor(String),
31    EnumConstructor(String),
32    InstanceCreation(InstanceCreationType),
33}
34
35// InstanceIdentifier holds the variables that are used to identify a worker or resource instance.
36// Unlike InstanceCreationType, this type can be formed only after the instance is inferred
37#[derive(Debug, Hash, PartialEq, Eq, Clone, Ord, PartialOrd)]
38pub enum InstanceIdentifier {
39    WitWorker {
40        variable_id: Option<VariableId>,
41        worker_name: Option<Box<Expr>>,
42    },
43
44    WitResource {
45        variable_id: Option<VariableId>,
46        worker_name: Option<Box<Expr>>,
47        resource_name: String,
48    },
49}
50
51impl InstanceIdentifier {
52    pub fn worker_name_mut(&mut self) -> Option<&mut Box<Expr>> {
53        match self {
54            InstanceIdentifier::WitWorker { worker_name, .. } => worker_name.as_mut(),
55            InstanceIdentifier::WitResource { worker_name, .. } => worker_name.as_mut(),
56        }
57    }
58    pub fn worker_name(&self) -> Option<&Expr> {
59        match self {
60            InstanceIdentifier::WitWorker { worker_name, .. } => worker_name.as_deref(),
61            InstanceIdentifier::WitResource { worker_name, .. } => worker_name.as_deref(),
62        }
63    }
64}
65
66#[derive(Debug, Hash, PartialEq, Eq, Clone, Ord, PartialOrd)]
67pub enum InstanceCreationType {
68    // A wit worker instance can be created without another module
69    WitWorker {
70        component_info: Option<ComponentDependencyKey>,
71        worker_name: Option<Box<Expr>>,
72    },
73    // an instance type of the type wit-resource can only be part of
74    // another instance (we call it module), which can be theoretically only be
75    // a worker, but we don't restrict this in types, such that it will easily
76    // handle nested wit resources
77    WitResource {
78        component_info: Option<ComponentDependencyKey>,
79        // this module identifier during resource creation will be always a worker module, but we don't necessarily restrict
80        // i.e, we do allow nested resource construction
81        module: Option<InstanceIdentifier>,
82        resource_name: FullyQualifiedResourceConstructor,
83    },
84}
85
86impl InstanceCreationType {
87    pub fn worker_name(&self) -> Option<Expr> {
88        match self {
89            InstanceCreationType::WitWorker { worker_name, .. } => worker_name.as_deref().cloned(),
90            InstanceCreationType::WitResource { module, .. } => {
91                let r = module.as_ref().and_then(|m| m.worker_name());
92                r.cloned()
93            }
94        }
95    }
96}
97
98impl CallType {
99    pub fn function_name(&self) -> Option<DynamicParsedFunctionName> {
100        match self {
101            CallType::Function { function_name, .. } => Some(function_name.clone()),
102            _ => None,
103        }
104    }
105    pub fn worker_expr(&self) -> Option<&Expr> {
106        match self {
107            CallType::Function {
108                instance_identifier,
109                ..
110            } => {
111                let module = instance_identifier.as_ref()?;
112                module.worker_name()
113            }
114            _ => None,
115        }
116    }
117
118    pub fn function_call(
119        function: DynamicParsedFunctionName,
120        component_info: Option<ComponentDependencyKey>,
121    ) -> CallType {
122        CallType::Function {
123            instance_identifier: None,
124            function_name: function,
125            component_info,
126        }
127    }
128
129    pub fn function_call_with_worker(
130        module: InstanceIdentifier,
131        function: DynamicParsedFunctionName,
132        component_info: Option<ComponentDependencyKey>,
133    ) -> CallType {
134        CallType::Function {
135            instance_identifier: Some(Box::new(module)),
136            function_name: function,
137            component_info,
138        }
139    }
140
141    pub fn is_resource_method(&self) -> bool {
142        match self {
143            CallType::Function { function_name, .. } => function_name
144                .to_parsed_function_name()
145                .function
146                .resource_method_name()
147                .is_some(),
148            _ => false,
149        }
150    }
151}
152
153impl Display for CallType {
154    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
155        match self {
156            CallType::Function { function_name, .. } => write!(f, "{function_name}"),
157            CallType::VariantConstructor(name) => write!(f, "{name}"),
158            CallType::EnumConstructor(name) => write!(f, "{name}"),
159            CallType::InstanceCreation(instance_creation_type) => match instance_creation_type {
160                InstanceCreationType::WitWorker { .. } => {
161                    write!(f, "instance")
162                }
163                InstanceCreationType::WitResource { resource_name, .. } => {
164                    write!(f, "{}", resource_name.resource_name)
165                }
166            },
167        }
168    }
169}