-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathfunction_calls.rs
More file actions
262 lines (241 loc) · 9.35 KB
/
Copy pathfunction_calls.rs
File metadata and controls
262 lines (241 loc) · 9.35 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#![allow(
clippy::arithmetic_side_effects,
clippy::indexing_slicing,
clippy::unseparated_literal_suffix,
clippy::as_conversions,
clippy::unused_trait_names,
clippy::pattern_type_mismatch
)]
use super::{Compiler, CompilerError, Register, Result};
use crate::ast::ExprRef;
use crate::builtins;
use crate::compiler::destructuring_planner::plans::BindingPlan;
use crate::lexer::Span;
use crate::rvm::instructions::{BuiltinCallParams, FunctionCallParams};
use crate::rvm::Instruction;
use crate::utils::get_path_string;
use crate::value::Value;
use alloc::{format, string::ToString, vec::Vec};
enum CallTarget {
User {
rule_index: u16,
expected_args: Option<usize>,
},
Builtin {
builtin_index: u16,
expected_args: Option<usize>,
},
HostAwait {
expected_args: Option<usize>,
},
}
impl<'a> Compiler<'a> {
pub(super) fn compile_function_call(
&mut self,
fcn: &ExprRef,
params: &[ExprRef],
span: Span,
) -> Result<Register> {
let fcn_path = get_path_string(fcn, None)
.map_err(|_| CompilerError::InvalidFunctionExpression.at(&span))?;
let original_fcn_path = fcn_path.clone();
let full_fcn_path = if self.policy.inner.rules.contains_key(&fcn_path) {
fcn_path
} else {
get_path_string(fcn, Some(&self.current_package))
.map_err(|_| CompilerError::InvalidFunctionExpressionWithPackage.at(&span))?
};
let mut out_param_plan: Option<(BindingPlan, Span)> = None;
let mut params_to_compile = params.len();
let call_target = self.determine_call_target(&original_fcn_path, &full_fcn_path, &span)?;
let expected_args = match &call_target {
CallTarget::User { expected_args, .. } => *expected_args,
CallTarget::Builtin { expected_args, .. } => *expected_args,
CallTarget::HostAwait { expected_args } => *expected_args,
};
if let Some(expected) = expected_args {
if params.len() == expected + 1 {
if let Some(last_param) = params.last() {
let plan = self.expect_binding_plan_for_expr(
last_param,
&format!("extra argument for function '{}'", original_fcn_path),
)?;
match plan {
BindingPlan::Parameter { .. } => {
out_param_plan = Some((plan, last_param.span().clone()));
params_to_compile -= 1;
}
other => {
return Err(CompilerError::UnexpectedBindingPlan {
context: "function extra argument".to_string(),
found: format!("{other:?}"),
}
.at(last_param.span()));
}
}
}
}
}
let mut arg_regs = Vec::new();
for param in params.iter().take(params_to_compile) {
let param_reg = self.compile_rego_expr_with_span(param, param.span(), false)?;
arg_regs.push(param_reg);
}
let dest = self.alloc_register();
match call_target {
CallTarget::User { rule_index, .. } => {
let mut args_array = [0u8; 8];
let num_args = arg_regs.len().min(8) as u8;
for (i, ®) in arg_regs.iter().take(8).enumerate() {
args_array[i] = reg;
}
let params_index = self.program.add_function_call_params(FunctionCallParams {
func_rule_index: rule_index,
dest,
num_args,
args: args_array,
});
self.emit_instruction(Instruction::FunctionCall { params_index }, &span);
}
CallTarget::Builtin { builtin_index, .. } => {
let mut args_array = [0u8; 8];
let num_args = arg_regs.len().min(8) as u8;
for (i, ®) in arg_regs.iter().take(8).enumerate() {
args_array[i] = reg;
}
let params_index = self.program.add_builtin_call_params(BuiltinCallParams {
dest,
builtin_index,
num_args,
args: args_array,
});
self.emit_instruction(Instruction::BuiltinCall { params_index }, &span);
}
CallTarget::HostAwait { .. } => {
let (arg_reg, id_reg) = if original_fcn_path == "__builtin_host_await" {
// Explicit __builtin_host_await(arg, id) — 2 arguments
if arg_regs.len() != 2 {
return Err(CompilerError::General {
message: format!(
"__builtin_host_await expects 2 arguments, got {}",
arg_regs.len()
),
}
.at(&span));
}
(arg_regs[0], arg_regs[1])
} else {
// Registered host-awaitable builtin — identifier is the function name
if arg_regs.is_empty() {
return Err(CompilerError::General {
message: format!(
"host-awaitable builtin '{}' expects at least 1 argument, got 0",
original_fcn_path
),
}
.at(&span));
}
let id_reg = self.alloc_register();
let literal_idx = self.add_literal(Value::String(original_fcn_path.into()));
self.emit_instruction(
Instruction::Load {
dest: id_reg,
literal_idx,
},
&span,
);
// HostAwait carries a single arg register; registered builtins
// are restricted to arg_count == 1 at registration time, so
// arg_regs[0] is the only argument.
(arg_regs[0], id_reg)
};
self.emit_instruction(
Instruction::HostAwait {
dest,
arg: arg_reg,
id: id_reg,
},
&span,
);
}
}
if let Some((plan, plan_span)) = &out_param_plan {
let plan_result = self
.apply_binding_plan(plan, dest, plan_span)
.map_err(|err| CompilerError::from(err).at(plan_span))?;
if let Some(result_reg) = plan_result {
self.emit_instruction(
Instruction::Move {
dest,
src: result_reg,
},
&span,
);
} else {
self.emit_instruction(Instruction::LoadBool { dest, value: true }, &span);
}
}
Ok(dest)
}
fn lookup_builtin_arity(&self, name: &str) -> Option<usize> {
if name == "print" {
Some(2)
} else {
builtins::BUILTINS
.get(name)
.map(|(_, arity)| *arity as usize)
}
}
}
impl<'a> Compiler<'a> {
fn determine_call_target(
&mut self,
original_fcn_path: &str,
full_fcn_path: &str,
span: &Span,
) -> Result<CallTarget> {
if original_fcn_path == "__builtin_host_await" {
return Ok(CallTarget::HostAwait {
expected_args: Some(2),
});
}
// Check registered host-awaitable builtins
if let Some(&arg_count) = self.host_await_builtins.get(original_fcn_path) {
return Ok(CallTarget::HostAwait {
expected_args: Some(arg_count),
});
}
if self.is_user_defined_function(full_fcn_path) {
let rule_index = self.get_or_assign_rule_index(full_fcn_path)?;
let expected_args = self
.policy
.inner
.functions
.get(full_fcn_path)
.map(|(_, arity, _)| *arity as usize)
.or_else(|| {
self.rule_function_param_count
.get(rule_index as usize)
.and_then(|count| *count)
});
Ok(CallTarget::User {
rule_index,
expected_args,
})
} else if self.is_builtin(original_fcn_path) {
let builtin_index = self.get_builtin_index(original_fcn_path)?;
let expected_args = self.lookup_builtin_arity(original_fcn_path);
Ok(CallTarget::Builtin {
builtin_index,
expected_args,
})
} else {
Err(CompilerError::UnknownFunction {
name: original_fcn_path.to_string(),
}
.at(span))
}
}
}