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
use crate::ast::{Ast, Expr, NodeId};
use crate::core::{Func, Type};
use proc_macro2::{Ident, Span};
use std::fmt::Display;
use std::rc::Rc;
/// Function call in form of `upper(arg)` or `lower(arg)`, etc.
#[derive(Debug, Clone)]
pub struct Call {
id: NodeId,
/// Unprocessed raw arguments generating by the parser.
raw_args: Vec<Rc<Expr>>,
/// Unprocessed raw arguments generating by the parser as a token-sequence.
raw_tokens: Option<Rc<Expr>>,
name: Ident,
span: Span,
}
impl Call {
/// Creates a new [`Call`] with the given name and arguments.
pub fn new(
id: NodeId,
name: Ident,
raw_args: Vec<Rc<Expr>>,
raw_tokens: Option<Rc<Expr>>,
span: Span,
) -> Self {
Self {
id,
name,
raw_args,
raw_tokens,
span,
}
}
/// The function's name.
pub fn name(&self) -> &Ident {
&self.name
}
/// The function call arguments.
pub fn raw_args(&self) -> &[Rc<Expr>] {
self.raw_args.as_ref()
}
/// The function call arguments as raw tokens.
pub fn raw_tokens(&self) -> Option<Rc<Expr>> {
self.raw_tokens.as_ref().map(|tokens| tokens.clone())
}
}
impl Ast for Call {
fn id(&self) -> NodeId {
self.id
}
fn span(&self) -> Span {
self.span
}
}
impl Display for Call {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}({})",
self.name(),
match (self.raw_args(), self.raw_tokens()) {
(args, Some(tokens)) if args.len() == 1 => tokens.to_string(),
(args, _) => args
.iter()
.map(|arg| arg.to_string())
.collect::<Vec<_>>()
.join(", "),
},
)
}
}
/// Metadata for storing resolved function call information
#[derive(Debug, Clone)]
pub struct CallMetadata {
/// Resolved arguments - ready to be used in [`Func::call`].
pub args: Vec<Rc<Expr>>,
/// The resolved function type.
pub func: Rc<Func>,
/// The target coercion type.
pub target_type: Type,
/// The cost of coercing to the target type.
pub coercion_cost: u32,
}