rils_builtins 0.3.0

Shared built-in API metadata for the Rils toolchain
Documentation
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Static descriptions of APIs shipped as part of Rils.
//!
//! Nothing in this crate performs I/O or executes script code. Tooling and
//! runtimes share these declarations while choosing their own implementation
//! strategy for runtime, intrinsic and host-backed items.

use core::fmt;

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
#[repr(u8)]
pub enum IntegerType {
    I8,
    I16,
    I32,
    I64,
    I128,
    Isize,
    U8,
    U16,
    U32,
    U64,
    U128,
    Usize,
}

impl IntegerType {
    pub const ALL: [Self; 12] = [
        Self::I8,
        Self::I16,
        Self::I32,
        Self::I64,
        Self::I128,
        Self::Isize,
        Self::U8,
        Self::U16,
        Self::U32,
        Self::U64,
        Self::U128,
        Self::Usize,
    ];
    pub const fn name(self) -> &'static str {
        match self {
            Self::I8 => "i8",
            Self::I16 => "i16",
            Self::I32 => "i32",
            Self::I64 => "i64",
            Self::I128 => "i128",
            Self::Isize => "isize",
            Self::U8 => "u8",
            Self::U16 => "u16",
            Self::U32 => "u32",
            Self::U64 => "u64",
            Self::U128 => "u128",
            Self::Usize => "usize",
        }
    }
    pub fn from_name(name: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|kind| kind.name() == name)
    }
    pub const fn is_signed(self) -> bool {
        matches!(
            self,
            Self::I8 | Self::I16 | Self::I32 | Self::I64 | Self::I128 | Self::Isize
        )
    }
    pub const fn bits(self) -> u32 {
        match self {
            Self::I8 | Self::U8 => 8,
            Self::I16 | Self::U16 => 16,
            Self::I32 | Self::U32 => 32,
            Self::I64 | Self::U64 => 64,
            Self::I128 | Self::U128 => 128,
            Self::Isize | Self::Usize => usize::BITS,
        }
    }
    pub const fn can_cast_losslessly_to(self, target: Self) -> bool {
        match (self.is_signed(), target.is_signed()) {
            (true, true) | (false, false) | (true, false) => target.bits() >= self.bits(),
            (false, true) => target.bits() > self.bits(),
        }
    }
    pub const fn can_represent_all(self, source: Self) -> bool {
        source.can_cast_losslessly_to(self)
    }
}

impl fmt::Display for IntegerType {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.name())
    }
}

/// A recursive type expression independent of the frontend's concrete `Type`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TypePattern {
    SelfType,
    Generic(&'static str),
    AnyInteger,
    Unknown,
    Unit,
    Bool,
    Char,
    String,
    F32,
    F64,
    U32,
    U8,
    Usize,
    Named {
        path: &'static str,
        arguments: &'static [TypePattern],
    },
    Option(&'static TypePattern),
    Result {
        ok: &'static TypePattern,
        error: &'static TypePattern,
    },
    Tuple(&'static [TypePattern]),
    Function {
        parameters: &'static [TypePattern],
        result: &'static TypePattern,
    },
    Reference {
        mutable: bool,
        inner: &'static TypePattern,
    },
}

#[derive(Clone, Copy, Debug)]
pub struct BuiltinSignature {
    pub parameters: &'static [TypePattern],
    pub result: TypePattern,
    pub variadic: bool,
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
#[repr(u16)]
pub enum IntrinsicId {
    IntegerTryFrom = 1,
    IntegerToF32 = 2,
    IntegerToF64 = 3,
    IntegerCheckedAdd = 16,
    IntegerCheckedSub = 17,
    IntegerCheckedMul = 18,
    IntegerCheckedDiv = 19,
    IntegerCheckedRem = 20,
    IntegerCheckedNeg = 21,
    IntegerCheckedAbs = 22,
    IntegerCheckedPow = 23,
    IntegerCheckedShl = 24,
    IntegerCheckedShr = 25,
    IntegerWrappingAdd = 32,
    IntegerWrappingSub = 33,
    IntegerWrappingMul = 34,
    IntegerWrappingNeg = 35,
    IntegerWrappingPow = 36,
    IntegerWrappingShl = 37,
    IntegerWrappingShr = 38,
    IntegerSaturatingAdd = 48,
    IntegerSaturatingSub = 49,
    IntegerSaturatingMul = 50,
    IntegerSaturatingNeg = 51,
    IntegerSaturatingAbs = 52,
    IntegerSaturatingPow = 53,
    IntegerOverflowingAdd = 64,
    IntegerOverflowingSub = 65,
    IntegerOverflowingMul = 66,
    IntegerOverflowingNeg = 67,
    IntegerOverflowingAbs = 68,
    IntegerOverflowingPow = 69,
    IntegerOverflowingShl = 70,
    IntegerOverflowingShr = 71,
    IntegerCountOnes = 80,
    IntegerCountZeros = 81,
    IntegerLeadingZeros = 82,
    IntegerTrailingZeros = 83,
    IntegerRotateLeft = 84,
    IntegerRotateRight = 85,
    IntegerPow = 86,
    IntegerDivEuclid = 87,
    IntegerRemEuclid = 88,
    IntegerAbs = 89,
    IntegerSwapBytes = 90,
    IntegerReverseBits = 91,
    FloatIsNan = 128,
    FloatIsInfinite = 129,
    FloatIsFinite = 130,
    FloatIsNormal = 131,
    FloatIsSignPositive = 132,
    FloatIsSignNegative = 133,
    FloatAbs = 134,
    FloatSignum = 135,
    FloatCopySign = 136,
    FloatFloor = 137,
    FloatCeil = 138,
    FloatRound = 139,
    FloatTrunc = 140,
    FloatFract = 141,
    FloatSqrt = 142,
    FloatRecip = 143,
    FloatMin = 144,
    FloatMax = 145,
    FloatClamp = 146,
    FloatMulAdd = 147,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IntrinsicKind {
    Method,
    AssociatedFunction,
}

#[derive(Clone, Copy, Debug)]
pub struct IntrinsicDeclaration {
    pub id: IntrinsicId,
    pub name: &'static str,
    pub kind: IntrinsicKind,
    pub signature: BuiltinSignature,
    pub documentation: &'static str,
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum IntegerConstantId {
    Min,
    Max,
    Bits,
}

#[derive(Clone, Copy, Debug)]
pub struct IntegerConstantDeclaration {
    pub id: IntegerConstantId,
    pub name: &'static str,
    pub value_type: TypePattern,
    pub documentation: &'static str,
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum FloatConstantId {
    Min,
    Max,
    Epsilon,
    MinPositive,
    Nan,
    Infinity,
    NegInfinity,
}

#[derive(Clone, Copy, Debug)]
pub struct FloatConstantDeclaration {
    pub id: FloatConstantId,
    pub name: &'static str,
    pub documentation: &'static str,
}

const SELF: TypePattern = TypePattern::SelfType;
const BOOL: TypePattern = TypePattern::Bool;
const U32: TypePattern = TypePattern::U32;
const STRING: TypePattern = TypePattern::String;
const OPTION_SELF: TypePattern = TypePattern::Option(&SELF);
const RESULT_SELF_STRING: TypePattern = TypePattern::Result {
    ok: &SELF,
    error: &STRING,
};
const SELF_BOOL: &[TypePattern] = &[SELF, BOOL];

macro_rules! intrinsic {
    ($id:ident, $name:literal, $kind:ident, [$($parameter:expr),* $(,)?] -> $result:expr, $documentation:literal) => {
        IntrinsicDeclaration {
            id: IntrinsicId::$id,
            name: $name,
            kind: IntrinsicKind::$kind,
            signature: BuiltinSignature {
                parameters: &[$($parameter),*], result: $result, variadic: false,
            },
            documentation: $documentation,
        }
    };
}

pub const INTEGER_INTRINSICS: &[IntrinsicDeclaration] = &[
    intrinsic!(IntegerTryFrom, "try_from", AssociatedFunction, [TypePattern::AnyInteger] -> RESULT_SELF_STRING, "Converts an integer when its value is representable by the target type."),
    intrinsic!(IntegerToF32, "to_f32", Method, [] -> TypePattern::F32, "Converts to f32, allowing IEEE-754 precision rounding."),
    intrinsic!(IntegerToF64, "to_f64", Method, [] -> TypePattern::F64, "Converts to f64, allowing IEEE-754 precision rounding."),
    intrinsic!(IntegerCheckedAdd, "checked_add", Method, [SELF] -> OPTION_SELF, "Returns None on overflow."),
    intrinsic!(IntegerCheckedSub, "checked_sub", Method, [SELF] -> OPTION_SELF, "Returns None on overflow."),
    intrinsic!(IntegerCheckedMul, "checked_mul", Method, [SELF] -> OPTION_SELF, "Returns None on overflow."),
    intrinsic!(IntegerCheckedDiv, "checked_div", Method, [SELF] -> OPTION_SELF, "Returns None on division failure or overflow."),
    intrinsic!(IntegerCheckedRem, "checked_rem", Method, [SELF] -> OPTION_SELF, "Returns None on remainder failure or overflow."),
    intrinsic!(IntegerCheckedNeg, "checked_neg", Method, [] -> OPTION_SELF, "Returns the negated value, or None when it cannot be represented."),
    intrinsic!(IntegerCheckedAbs, "checked_abs", Method, [] -> OPTION_SELF, "Returns the absolute value, or None on signed minimum overflow."),
    intrinsic!(IntegerCheckedPow, "checked_pow", Method, [U32] -> OPTION_SELF, "Raises to a power, returning None on overflow."),
    intrinsic!(IntegerCheckedShl, "checked_shl", Method, [U32] -> OPTION_SELF, "Shifts left, returning None when the shift is at least the bit width."),
    intrinsic!(IntegerCheckedShr, "checked_shr", Method, [U32] -> OPTION_SELF, "Shifts right, returning None when the shift is at least the bit width."),
    intrinsic!(IntegerWrappingAdd, "wrapping_add", Method, [SELF] -> SELF, "Adds with two's-complement wrapping."),
    intrinsic!(IntegerWrappingSub, "wrapping_sub", Method, [SELF] -> SELF, "Subtracts with two's-complement wrapping."),
    intrinsic!(IntegerWrappingMul, "wrapping_mul", Method, [SELF] -> SELF, "Multiplies with two's-complement wrapping."),
    intrinsic!(IntegerWrappingNeg, "wrapping_neg", Method, [] -> SELF, "Negates with two's-complement wrapping."),
    intrinsic!(IntegerWrappingPow, "wrapping_pow", Method, [U32] -> SELF, "Raises to a power with wrapping arithmetic."),
    intrinsic!(IntegerWrappingShl, "wrapping_shl", Method, [U32] -> SELF, "Shifts left after reducing the shift modulo the bit width."),
    intrinsic!(IntegerWrappingShr, "wrapping_shr", Method, [U32] -> SELF, "Shifts right after reducing the shift modulo the bit width."),
    intrinsic!(IntegerSaturatingAdd, "saturating_add", Method, [SELF] -> SELF, "Adds while saturating at the numeric bounds."),
    intrinsic!(IntegerSaturatingSub, "saturating_sub", Method, [SELF] -> SELF, "Subtracts while saturating at the numeric bounds."),
    intrinsic!(IntegerSaturatingMul, "saturating_mul", Method, [SELF] -> SELF, "Multiplies while saturating at the numeric bounds."),
    intrinsic!(IntegerSaturatingNeg, "saturating_neg", Method, [] -> SELF, "Negates while saturating at the numeric bounds."),
    intrinsic!(IntegerSaturatingAbs, "saturating_abs", Method, [] -> SELF, "Returns the absolute value, saturating signed minimum at MAX."),
    intrinsic!(IntegerSaturatingPow, "saturating_pow", Method, [U32] -> SELF, "Raises to a power while saturating at the numeric bounds."),
    intrinsic!(IntegerOverflowingAdd, "overflowing_add", Method, [SELF] -> TypePattern::Tuple(SELF_BOOL), "Returns the wrapped sum and whether overflow occurred."),
    intrinsic!(IntegerOverflowingSub, "overflowing_sub", Method, [SELF] -> TypePattern::Tuple(SELF_BOOL), "Returns the wrapped difference and whether overflow occurred."),
    intrinsic!(IntegerOverflowingMul, "overflowing_mul", Method, [SELF] -> TypePattern::Tuple(SELF_BOOL), "Returns the wrapped product and whether overflow occurred."),
    intrinsic!(IntegerOverflowingNeg, "overflowing_neg", Method, [] -> TypePattern::Tuple(SELF_BOOL), "Returns the wrapped negation and whether overflow occurred."),
    intrinsic!(IntegerOverflowingAbs, "overflowing_abs", Method, [] -> TypePattern::Tuple(SELF_BOOL), "Returns the wrapped absolute value and whether overflow occurred."),
    intrinsic!(IntegerOverflowingPow, "overflowing_pow", Method, [U32] -> TypePattern::Tuple(SELF_BOOL), "Returns the wrapped power and whether overflow occurred."),
    intrinsic!(IntegerOverflowingShl, "overflowing_shl", Method, [U32] -> TypePattern::Tuple(SELF_BOOL), "Returns the wrapped left shift and whether the shift exceeded the bit width."),
    intrinsic!(IntegerOverflowingShr, "overflowing_shr", Method, [U32] -> TypePattern::Tuple(SELF_BOOL), "Returns the wrapped right shift and whether the shift exceeded the bit width."),
    intrinsic!(IntegerCountOnes, "count_ones", Method, [] -> U32, "Returns the number of one bits."),
    intrinsic!(IntegerCountZeros, "count_zeros", Method, [] -> U32, "Returns the number of zero bits."),
    intrinsic!(IntegerLeadingZeros, "leading_zeros", Method, [] -> U32, "Returns the number of leading zero bits."),
    intrinsic!(IntegerTrailingZeros, "trailing_zeros", Method, [] -> U32, "Returns the number of trailing zero bits."),
    intrinsic!(IntegerRotateLeft, "rotate_left", Method, [U32] -> SELF, "Rotates bits to the left."),
    intrinsic!(IntegerRotateRight, "rotate_right", Method, [U32] -> SELF, "Rotates bits to the right."),
    intrinsic!(IntegerPow, "pow", Method, [U32] -> SELF, "Raises to a non-negative integer power, failing on overflow."),
    intrinsic!(IntegerDivEuclid, "div_euclid", Method, [SELF] -> SELF, "Computes Euclidean division, failing on zero or overflow."),
    intrinsic!(IntegerRemEuclid, "rem_euclid", Method, [SELF] -> SELF, "Computes the least non-negative remainder, failing on zero or overflow."),
    intrinsic!(IntegerAbs, "abs", Method, [] -> SELF, "Returns the absolute value, failing on signed minimum overflow."),
    intrinsic!(IntegerSwapBytes, "swap_bytes", Method, [] -> SELF, "Reverses the byte order."),
    intrinsic!(IntegerReverseBits, "reverse_bits", Method, [] -> SELF, "Reverses the order of bits."),
];

pub const FLOAT_INTRINSICS: &[IntrinsicDeclaration] = &[
    intrinsic!(FloatIsNan, "is_nan", Method, [] -> BOOL, "Returns whether the value is NaN."),
    intrinsic!(FloatIsInfinite, "is_infinite", Method, [] -> BOOL, "Returns whether the value is positive or negative infinity."),
    intrinsic!(FloatIsFinite, "is_finite", Method, [] -> BOOL, "Returns whether the value is neither infinite nor NaN."),
    intrinsic!(FloatIsNormal, "is_normal", Method, [] -> BOOL, "Returns whether the value is neither zero, subnormal, infinite nor NaN."),
    intrinsic!(FloatIsSignPositive, "is_sign_positive", Method, [] -> BOOL, "Returns whether the sign is positive, including positive zero and positive NaN."),
    intrinsic!(FloatIsSignNegative, "is_sign_negative", Method, [] -> BOOL, "Returns whether the sign is negative, including negative zero and negative NaN."),
    intrinsic!(FloatAbs, "abs", Method, [] -> SELF, "Returns the absolute value."),
    intrinsic!(FloatSignum, "signum", Method, [] -> SELF, "Returns 1 or -1 according to the sign, preserving NaN."),
    intrinsic!(FloatCopySign, "copysign", Method, [SELF] -> SELF, "Returns the magnitude of self with the sign of the argument."),
    intrinsic!(FloatFloor, "floor", Method, [] -> SELF, "Returns the greatest integer less than or equal to the value."),
    intrinsic!(FloatCeil, "ceil", Method, [] -> SELF, "Returns the smallest integer greater than or equal to the value."),
    intrinsic!(FloatRound, "round", Method, [] -> SELF, "Rounds to the nearest integer, with halfway cases away from zero."),
    intrinsic!(FloatTrunc, "trunc", Method, [] -> SELF, "Returns the integer part of the value."),
    intrinsic!(FloatFract, "fract", Method, [] -> SELF, "Returns the fractional part of the value."),
    intrinsic!(FloatSqrt, "sqrt", Method, [] -> SELF, "Returns the square root, or NaN for a negative value."),
    intrinsic!(FloatRecip, "recip", Method, [] -> SELF, "Returns the reciprocal."),
    intrinsic!(FloatMin, "min", Method, [SELF] -> SELF, "Returns the minimum, ignoring NaN when exactly one operand is NaN."),
    intrinsic!(FloatMax, "max", Method, [SELF] -> SELF, "Returns the maximum, ignoring NaN when exactly one operand is NaN."),
    intrinsic!(FloatClamp, "clamp", Method, [SELF, SELF] -> SELF, "Restricts the value to the inclusive interval, failing for invalid bounds."),
    intrinsic!(FloatMulAdd, "mul_add", Method, [SELF, SELF] -> SELF, "Computes self * a + b with one rounding operation."),
];

pub const INTEGER_CONSTANTS: &[IntegerConstantDeclaration] = &[
    IntegerConstantDeclaration {
        id: IntegerConstantId::Min,
        name: "MIN",
        value_type: TypePattern::SelfType,
        documentation: "The smallest value representable by this integer type.",
    },
    IntegerConstantDeclaration {
        id: IntegerConstantId::Max,
        name: "MAX",
        value_type: TypePattern::SelfType,
        documentation: "The largest value representable by this integer type.",
    },
    IntegerConstantDeclaration {
        id: IntegerConstantId::Bits,
        name: "BITS",
        value_type: TypePattern::U32,
        documentation: "The width of this integer type in bits.",
    },
];

pub const FLOAT_CONSTANTS: &[FloatConstantDeclaration] = &[
    FloatConstantDeclaration {
        id: FloatConstantId::Min,
        name: "MIN",
        documentation: "The smallest finite value.",
    },
    FloatConstantDeclaration {
        id: FloatConstantId::Max,
        name: "MAX",
        documentation: "The largest finite value.",
    },
    FloatConstantDeclaration {
        id: FloatConstantId::Epsilon,
        name: "EPSILON",
        documentation: "The difference between 1 and the next representable value.",
    },
    FloatConstantDeclaration {
        id: FloatConstantId::MinPositive,
        name: "MIN_POSITIVE",
        documentation: "The smallest positive normal value.",
    },
    FloatConstantDeclaration {
        id: FloatConstantId::Nan,
        name: "NAN",
        documentation: "A not-a-number value.",
    },
    FloatConstantDeclaration {
        id: FloatConstantId::Infinity,
        name: "INFINITY",
        documentation: "Positive infinity.",
    },
    FloatConstantDeclaration {
        id: FloatConstantId::NegInfinity,
        name: "NEG_INFINITY",
        documentation: "Negative infinity.",
    },
];

pub fn integer_constant(name: &str) -> Option<&'static IntegerConstantDeclaration> {
    INTEGER_CONSTANTS.iter().find(|item| item.name == name)
}

pub fn integer_method(name: &str) -> Option<&'static IntrinsicDeclaration> {
    INTEGER_INTRINSICS
        .iter()
        .find(|item| item.kind == IntrinsicKind::Method && item.name == name)
}
pub fn integer_associated_function(name: &str) -> Option<&'static IntrinsicDeclaration> {
    INTEGER_INTRINSICS
        .iter()
        .find(|item| item.kind == IntrinsicKind::AssociatedFunction && item.name == name)
}

pub fn float_method(name: &str) -> Option<&'static IntrinsicDeclaration> {
    FLOAT_INTRINSICS.iter().find(|item| item.name == name)
}

pub fn float_constant(name: &str) -> Option<&'static FloatConstantDeclaration> {
    FLOAT_CONSTANTS.iter().find(|item| item.name == name)
}

pub fn intrinsic(id: IntrinsicId) -> Option<&'static IntrinsicDeclaration> {
    INTEGER_INTRINSICS
        .iter()
        .chain(FLOAT_INTRINSICS)
        .find(|item| item.id == id)
}