marker_api 0.5.0

Marker's API, designed for stability and usability
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
use crate::{
    common::{ExprId, HasNodeId, SpanId},
    prelude::EmissionNode,
    private::Sealed,
    sem::TyKind,
    span::{HasSpan, Span},
    CtorBlocker,
};

use std::{fmt::Debug, marker::PhantomData};

mod block_expr;
mod call_exprs;
mod control_flow_expr;
mod ctor_expr;
mod lit_expr;
mod op_exprs;
mod path_expr;
mod place_expr;
mod unstable_expr;
pub use block_expr::*;
pub use call_exprs::*;
pub use control_flow_expr::*;
pub use ctor_expr::*;
pub use lit_expr::*;
pub use op_exprs::*;
pub use path_expr::*;
pub use place_expr::*;
pub use unstable_expr::*;

/// This trait combines methods, which are common between all expressions.
///
/// This trait is only meant to be implemented inside this crate. The `Sealed`
/// super trait prevents external implementations.
pub trait ExprData<'ast>: Debug + EmissionNode<'ast> + HasSpan<'ast> + HasNodeId + Sealed {
    /// Returns the [`ExprId`] of this expression.
    fn id(&self) -> ExprId;

    /// Returns the semantic type of this expression.
    fn ty(&self) -> TyKind<'ast>;

    /// Returns the [`ExprPrecedence`] of this expression.
    fn precedence(&self) -> ExprPrecedence;

    /// Returns this expression wrapped in it's [`ExprKind`] variant.
    ///
    /// In function parameters, it's recommended to use `Into<ExprKind<'ast>>`
    /// as a bound to support all expressions and `ExprKind<'ast>` as parameters.
    fn as_expr(&'ast self) -> ExprKind<'ast>;
}

#[repr(C)]
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
pub enum ExprKind<'ast> {
    IntLit(&'ast IntLitExpr<'ast>),
    FloatLit(&'ast FloatLitExpr<'ast>),
    StrLit(&'ast StrLitExpr<'ast>),
    CharLit(&'ast CharLitExpr<'ast>),
    BoolLit(&'ast BoolLitExpr<'ast>),
    Block(&'ast BlockExpr<'ast>),
    Closure(&'ast ClosureExpr<'ast>),
    UnaryOp(&'ast UnaryOpExpr<'ast>),
    Ref(&'ast RefExpr<'ast>),
    BinaryOp(&'ast BinaryOpExpr<'ast>),
    Try(&'ast TryExpr<'ast>),
    Assign(&'ast AssignExpr<'ast>),
    As(&'ast AsExpr<'ast>),
    Path(&'ast PathExpr<'ast>),
    Call(&'ast CallExpr<'ast>),
    Method(&'ast MethodExpr<'ast>),
    Array(&'ast ArrayExpr<'ast>),
    Tuple(&'ast TupleExpr<'ast>),
    Ctor(&'ast CtorExpr<'ast>),
    Range(&'ast RangeExpr<'ast>),
    Index(&'ast IndexExpr<'ast>),
    Field(&'ast FieldExpr<'ast>),
    If(&'ast IfExpr<'ast>),
    Let(&'ast LetExpr<'ast>),
    Match(&'ast MatchExpr<'ast>),
    Break(&'ast BreakExpr<'ast>),
    Return(&'ast ReturnExpr<'ast>),
    Continue(&'ast ContinueExpr<'ast>),
    For(&'ast ForExpr<'ast>),
    Loop(&'ast LoopExpr<'ast>),
    While(&'ast WhileExpr<'ast>),
    Await(&'ast AwaitExpr<'ast>),
    Unstable(&'ast UnstableExpr<'ast>),
}

impl<'ast> ExprKind<'ast> {
    impl_expr_kind_fn!(ExprKind: span() -> &Span<'ast>);
    impl_expr_kind_fn!(ExprKind: id() -> ExprId);
    impl_expr_kind_fn!(ExprKind: ty() -> TyKind<'ast>);
    impl_expr_kind_fn!(ExprKind: precedence() -> ExprPrecedence);
}

crate::span::impl_spanned_for!(ExprKind<'ast>);
crate::common::impl_identifiable_for!(ExprKind<'ast>);

#[repr(C)]
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
pub enum LitExprKind<'ast> {
    Int(&'ast IntLitExpr<'ast>),
    Float(&'ast FloatLitExpr<'ast>),
    Str(&'ast StrLitExpr<'ast>),
    Char(&'ast CharLitExpr<'ast>),
    Bool(&'ast BoolLitExpr<'ast>),
    /// Rust represents negative numbers as positive literals with a unary
    /// negation operator in front. This unary expression should therefore
    /// be seen as a part of a literal, that it wraps.
    UnaryOp(&'ast UnaryOpExpr<'ast>, CtorBlocker),
}

impl<'ast> LitExprKind<'ast> {
    impl_expr_kind_fn!(LitExprKind: span() -> &Span<'ast>);
    impl_expr_kind_fn!(LitExprKind: id() -> ExprId);
    impl_expr_kind_fn!(LitExprKind: ty() -> TyKind<'ast>);
    impl_expr_kind_fn!(LitExprKind: precedence() -> ExprPrecedence);
}

crate::span::impl_spanned_for!(LitExprKind<'ast>);
crate::common::impl_identifiable_for!(LitExprKind<'ast>);

impl<'ast> From<LitExprKind<'ast>> for ExprKind<'ast> {
    fn from(value: LitExprKind<'ast>) -> Self {
        match value {
            LitExprKind::Int(expr) => ExprKind::IntLit(expr),
            LitExprKind::Float(expr) => ExprKind::FloatLit(expr),
            LitExprKind::Str(expr) => ExprKind::StrLit(expr),
            LitExprKind::Char(expr) => ExprKind::CharLit(expr),
            LitExprKind::Bool(expr) => ExprKind::BoolLit(expr),
            LitExprKind::UnaryOp(expr, ..) => ExprKind::UnaryOp(expr),
        }
    }
}

impl<'ast> TryFrom<ExprKind<'ast>> for LitExprKind<'ast> {
    type Error = ();

    fn try_from(value: ExprKind<'ast>) -> Result<Self, Self::Error> {
        match value {
            ExprKind::IntLit(expr) => Ok(LitExprKind::Int(expr)),
            ExprKind::FloatLit(expr) => Ok(LitExprKind::Float(expr)),
            ExprKind::StrLit(expr) => Ok(LitExprKind::Str(expr)),
            ExprKind::CharLit(expr) => Ok(LitExprKind::Char(expr)),
            ExprKind::BoolLit(expr) => Ok(LitExprKind::Bool(expr)),
            ExprKind::UnaryOp(expr) => {
                // Only accept this conversion if this operation negates a literal.
                if expr.kind() == UnaryOpKind::Neg && TryInto::<LitExprKind<'_>>::try_into(expr.expr()).is_ok() {
                    Ok(LitExprKind::UnaryOp(expr, CtorBlocker::new()))
                } else {
                    Err(())
                }
            },
            _ => Err(()),
        }
    }
}

#[repr(u32)]
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
pub enum ExprPrecedence {
    Lit = 0x1400_0000,
    Block = 0x1400_0001,
    Ctor = 0x1400_0002,
    Assign = 0x1400_0003,
    For = 0x1400_0004,
    Loop = 0x1400_0005,
    While = 0x1400_0006,
    Await = 0x1400_0007,

    Path = 0x1300_0000,

    Method = 0x1200_0000,
    Call = 0x1200_0001,
    // These three are just a guess, as they're not listed in the precedence table
    If = 0x1200_0002,
    Let = 0x1200_0003,
    Match = 0x1200_0004,

    Field = 0x1100_0000,

    Fn = 0x1000_0000,
    Index = 0x1000_0001,

    Try = 0x0F00_0000,

    /// The unary `-` operator
    Neg = 0x0E00_0000,
    /// The `!` operator
    Not = 0x0E00_0001,
    /// The unary `*` operator
    Deref = 0x0E00_0002,
    /// The unary `&` operator
    Ref = 0x0E00_0003,

    As = 0x0D00_0000,

    /// The binary `*` operator
    Mul = 0x0C00_0000,
    /// The `/` operator
    Div = 0x0C00_0001,
    /// The `%` operator
    Rem = 0x0C00_0002,

    /// The `+` operator
    Add = 0x0B00_0000,
    /// The binary `-` operator
    Sub = 0x0B00_0001,

    /// The `>>` operator
    Shr = 0x0A00_0000,
    /// The `<<` operator
    Shl = 0x0A00_0001,

    /// The binary `&` operator
    BitAnd = 0x0900_0000,

    /// The `^` operator
    BitXor = 0x0800_0000,

    /// The `|` operator
    BitOr = 0x0700_0000,

    /// The `==`, `!=`, `<`, `<=`, `>`, `>=` operators
    Comparison = 0x0600_0000,

    /// The `&&` operator
    And = 0x0500_0000,

    /// The `||` operator
    Or = 0x0400_0000,

    /// Ranges `0..10`, `0..=8`
    Range = 0x0300_0000,

    /// This precedence level includes compound assignment operators, like:
    /// `+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `|=`, `^=`, `<<=`, `>>=`
    AssignOp = 0x0200_0000,

    Closure = 0x0100_0000,
    Break = 0x0100_0001,
    Return = 0x0100_0002,
    Continue = 0x0100_0003,
    /// The precedence originates from an unstable source. The stored value provides
    /// the current precedence of this expression. This might change in the future
    Unstable(i32),
}

macro_rules! impl_expr_kind_fn {
    (ExprKind: $method:ident () -> $return_ty:ty) => {
        impl_expr_kind_fn!((ExprKind) $method() -> $return_ty,
            IntLit, FloatLit, StrLit, CharLit, BoolLit,
            Block, Closure,
            UnaryOp, Ref, BinaryOp, Try, As, Assign,
            Path, Index, Field,
            Call, Method,
            Array, Tuple, Ctor, Range,
            If, Let, Match, Break, Return, Continue, For, Loop, While,
            Await,
            Unstable
        );
    };
    (LitExprKind: $method:ident () -> $return_ty:ty) => {
        impl_expr_kind_fn!((LitExprKind) $method() -> $return_ty,
            Int, Float, Str, Char, Bool, UnaryOp
        );
    };
    (($self:ident) $method:ident () -> $return_ty:ty $(, $kind:ident)+) => {
        pub fn $method(&self) -> $return_ty {
            match self {
                $($self::$kind(data, ..) => data.$method(),)*
            }
        }
    };
}

use impl_expr_kind_fn;

#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", visibility::make(pub))]
struct CommonExprData<'ast> {
    /// The lifetime is not needed right now, but it's safer to include it for
    /// future additions. Having it in this struct makes it easier for all
    /// expression structs, as they will have a valid use for `'ast` even if they
    /// don't need it. Otherwise, we might need to declare this field in each
    /// expression.
    _lifetime: PhantomData<&'ast ()>,
    id: ExprId,
    span: SpanId,
}

#[cfg(feature = "driver-api")]
impl<'ast> CommonExprData<'ast> {
    pub fn new(id: ExprId, span: SpanId) -> Self {
        Self {
            _lifetime: PhantomData,
            id,
            span,
        }
    }
}

macro_rules! impl_expr_data {
    ($self_ty:ty, $enum_name:ident) => {
        $crate::ast::expr::impl_expr_data!($self_ty, $enum_name,
            fn precedence(&self) -> $crate::ast::expr::ExprPrecedence {
                $crate::ast::expr::ExprPrecedence::$enum_name
            }
        );
    };
    ($self_ty:ty, $enum_name:ident, $precedence_fn:item) => {
        impl<'ast> super::ExprData<'ast> for $self_ty {
            fn id(&self) -> crate::common::ExprId {
                self.data.id
            }

            fn ty(&self) -> $crate::sem::TyKind<'ast> {
                $crate::context::with_cx(self, |cx| cx.expr_ty(self.data.id))
            }

            $precedence_fn

            fn as_expr(&'ast self) -> crate::ast::ExprKind<'ast> {
                $crate::ast::expr::ExprKind::$enum_name(self)
            }
        }

        $crate::span::impl_has_span_via_field!($self_ty, data.span);
        $crate::common::impl_identifiable_for!($self_ty, use $crate::ast::expr::ExprData);

        impl<'ast> $crate::private::Sealed for $self_ty {}

        impl<'ast> From<&'ast $self_ty> for $crate::ast::expr::ExprKind<'ast> {
            fn from(from: &'ast $self_ty) -> Self {
                $crate::ast::expr::ExprKind::$enum_name(from)
            }
        }
    };
}

use impl_expr_data;

/// An expression that is evaluated at compile time. These show up in array
/// indices and constant generics.
///
/// Marker currently doesn't provide a way to calculate the result of a constant
/// expression.
#[repr(C)]
#[derive(Debug)]
pub struct ConstExpr<'ast> {
    expr: ExprKind<'ast>,
}

impl<'ast> ConstExpr<'ast> {
    /// This returns the wrapped expression that will be evaluated at compile time.
    pub fn expr(&self) -> ExprKind<'ast> {
        self.expr
    }
}

#[cfg(feature = "driver-api")]
impl<'ast> ConstExpr<'ast> {
    pub fn new(expr: ExprKind<'ast>) -> Self {
        Self { expr }
    }
}

#[cfg(all(test, target_arch = "x86_64", target_pointer_width = "64"))]
mod test {
    use crate::test::assert_size_of;

    use super::*;
    use expect_test::expect;

    #[test]
    fn expr_struct_size() {
        // These sizes are allowed to change, this is just a check to have a
        // general overview and to prevent accidental changes
        assert_size_of::<IntLitExpr<'_>>(&expect!["40"]);
        assert_size_of::<FloatLitExpr<'_>>(&expect!["32"]);
        assert_size_of::<StrLitExpr<'_>>(&expect!["48"]);
        assert_size_of::<CharLitExpr<'_>>(&expect!["24"]);
        assert_size_of::<BoolLitExpr<'_>>(&expect!["24"]);
        assert_size_of::<BlockExpr<'_>>(&expect!["96"]);
        assert_size_of::<ClosureExpr<'_>>(&expect!["72"]);
        assert_size_of::<UnaryOpExpr<'_>>(&expect!["40"]);
        assert_size_of::<RefExpr<'_>>(&expect!["40"]);
        assert_size_of::<BinaryOpExpr<'_>>(&expect!["56"]);
        assert_size_of::<TryExpr<'_>>(&expect!["32"]);
        assert_size_of::<AssignExpr<'_>>(&expect!["56"]);
        assert_size_of::<AsExpr<'_>>(&expect!["48"]);
        assert_size_of::<PathExpr<'_>>(&expect!["96"]);
        assert_size_of::<CallExpr<'_>>(&expect!["48"]);
        assert_size_of::<MethodExpr<'_>>(&expect!["80"]);
        assert_size_of::<ArrayExpr<'_>>(&expect!["56"]);
        assert_size_of::<TupleExpr<'_>>(&expect!["32"]);
        assert_size_of::<CtorExpr<'_>>(&expect!["136"]);
        assert_size_of::<RangeExpr<'_>>(&expect!["72"]);
        assert_size_of::<IndexExpr<'_>>(&expect!["48"]);
        assert_size_of::<FieldExpr<'_>>(&expect!["48"]);
        assert_size_of::<IfExpr<'_>>(&expect!["72"]);
        assert_size_of::<LetExpr<'_>>(&expect!["48"]);
        assert_size_of::<MatchExpr<'_>>(&expect!["48"]);
        assert_size_of::<BreakExpr<'_>>(&expect!["72"]);
        assert_size_of::<ReturnExpr<'_>>(&expect!["40"]);
        assert_size_of::<ContinueExpr<'_>>(&expect!["48"]);
        assert_size_of::<ForExpr<'_>>(&expect!["88"]);
        assert_size_of::<LoopExpr<'_>>(&expect!["56"]);
        assert_size_of::<WhileExpr<'_>>(&expect!["72"]);
        assert_size_of::<UnstableExpr<'_>>(&expect!["24"]);
    }
}