1pub use rils_syntax::IntegerType;
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum TypePattern {
12 SelfType,
13 Generic(&'static str),
14 BoundGeneric {
15 name: &'static str,
16 bounds: &'static [TypePattern],
17 },
18 AnyInteger,
19 Unknown,
20 Unit,
21 Bool,
22 Char,
23 String,
24 F32,
25 F64,
26 U32,
27 U8,
28 Usize,
29 Named {
30 path: &'static str,
31 arguments: &'static [TypePattern],
32 },
33 Option(&'static TypePattern),
34 Result {
35 ok: &'static TypePattern,
36 error: &'static TypePattern,
37 },
38 Tuple(&'static [TypePattern]),
39 Array {
40 element: &'static TypePattern,
41 length: usize,
42 },
43 ArrayParameter {
44 element: &'static TypePattern,
45 length: &'static str,
46 },
47 Slice(&'static TypePattern),
48 Function {
49 parameters: &'static [TypePattern],
50 result: &'static TypePattern,
51 },
52 Reference {
53 mutable: bool,
54 inner: &'static TypePattern,
55 },
56 Associated {
57 base: &'static TypePattern,
58 trait_name: Option<&'static str>,
59 trait_arguments: &'static [TypePattern],
60 name: &'static str,
61 arguments: &'static [TypePattern],
62 },
63}
64
65#[derive(Clone, Copy, Debug)]
66pub struct BuiltinSignature {
67 pub parameter_names: &'static [&'static str],
69 pub parameters: &'static [TypePattern],
70 pub parameter_capabilities: &'static [(usize, crate::CallableTraitKind)],
73 pub result: TypePattern,
74 pub variadic: bool,
75}
76
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub enum IntrinsicKind {
79 Method,
80 AssociatedFunction,
81}
82
83#[derive(Clone, Copy, Debug)]
84pub struct IntrinsicDeclaration {
85 pub symbol: &'static str,
86 pub name: &'static str,
87 pub kind: IntrinsicKind,
88 pub signature: BuiltinSignature,
89 pub documentation: &'static str,
90}
91
92pub fn integer_method(name: &str) -> Option<&'static IntrinsicDeclaration> {
93 crate::INTEGER_INTRINSICS
94 .iter()
95 .find(|item| item.kind == IntrinsicKind::Method && item.name == name)
96}
97
98pub fn integer_associated_function(name: &str) -> Option<&'static IntrinsicDeclaration> {
99 crate::INTEGER_INTRINSICS
100 .iter()
101 .find(|item| item.kind == IntrinsicKind::AssociatedFunction && item.name == name)
102}
103
104pub fn float_method(name: &str) -> Option<&'static IntrinsicDeclaration> {
105 crate::FLOAT_INTRINSICS
106 .iter()
107 .find(|item| item.name == name)
108}
109
110pub fn intrinsic(symbol: &str) -> Option<&'static IntrinsicDeclaration> {
111 crate::INTEGER_INTRINSICS
112 .iter()
113 .chain(crate::FLOAT_INTRINSICS)
114 .find(|item| item.symbol == symbol)
115}
116
117pub fn intrinsic_by_symbol(
120 symbol: &str,
121) -> Option<(&'static IntrinsicDeclaration, Option<IntegerType>)> {
122 if let Some(rest) = symbol.strip_prefix("core::integer::")
123 && let Some((target, method)) = rest.split_once("::")
124 && let Some(target) = IntegerType::from_name(target)
125 {
126 let declaration = integer_associated_function(method)?;
127 return Some((declaration, Some(target)));
128 }
129 crate::INTEGER_INTRINSICS
130 .iter()
131 .chain(crate::FLOAT_INTRINSICS)
132 .find(|declaration| declaration.symbol == symbol)
133 .map(|declaration| (declaration, None))
134}