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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use crate::{
    ast::pat::PatKind,
    common::{ExprId, SpanId},
    context::with_cx,
    ffi::{FfiOption, FfiSlice},
    span::{Ident, Span},
};

use super::{CommonExprData, ExprKind};

/// An if expression. If let expressions are expressed as an [`IfExpr`] with an
/// [`LetExpr`] as the conditional expression.
///
/// ```
/// # let cond = true;
/// // vvvv the condition
/// if cond {
///     // then expression
/// } else {
///     // else expression
/// }
///
/// # let slice: &[i32] = &[1, 2];
/// if let [x] = slice {
///     // then expression
/// } // No else expression
///
/// # let num = 5;
/// if num == 1 {
///     // then expression
/// } else /* `IfExpr` as an else expression */ if num == 2 {
///     // then expression of the else expression
/// } else {
///     // else expression of the else expression
/// }
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct IfExpr<'ast> {
    data: CommonExprData<'ast>,
    condition: ExprKind<'ast>,
    then: ExprKind<'ast>,
    els: FfiOption<ExprKind<'ast>>,
}

impl<'ast> IfExpr<'ast> {
    pub fn condition(&self) -> ExprKind<'ast> {
        self.condition
    }

    pub fn then(&self) -> ExprKind<'ast> {
        self.then
    }

    /// This returns the `else` expression of this `if` expression, this will
    /// either be a [`BlockExpr`](super::BlockExpr) or [`IfExpr`].
    ///
    /// `els` is an abbreviation for `else`, which is a reserved keyword in Rust.
    pub fn els(&self) -> Option<ExprKind<'ast>> {
        self.els.copy()
    }
}

super::impl_expr_data!(IfExpr<'ast>, If);

#[cfg(feature = "driver-api")]
impl<'ast> IfExpr<'ast> {
    pub fn new(
        data: CommonExprData<'ast>,
        condition: ExprKind<'ast>,
        then: ExprKind<'ast>,
        els: Option<ExprKind<'ast>>,
    ) -> Self {
        Self {
            data,
            condition,
            then,
            els: els.into(),
        }
    }
}

/// A `let` expression used in conditional statements, to check if the value
/// of the scrutinee matches the pattern.
///
/// ```
/// # let slice: &[i32] = &[1, 2];
/// //     vvv The pattern
/// if let [x] = slice {
/// //           ^^^^^ The scrutinee
///     // ...
/// }
///
/// # let mut opt = Some(1);
/// //        vvvvvvv The pattern
/// while let Some(_) = opt {
/// //                  ^^^ The scrutinee
///     // ...
///     # opt = None;
/// }
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct LetExpr<'ast> {
    data: CommonExprData<'ast>,
    pat: PatKind<'ast>,
    scrutinee: ExprKind<'ast>,
}

impl<'ast> LetExpr<'ast> {
    pub fn pat(&self) -> PatKind<'ast> {
        self.pat
    }

    pub fn scrutinee(&self) -> ExprKind<'ast> {
        self.scrutinee
    }
}

super::impl_expr_data!(LetExpr<'ast>, Let);

#[cfg(feature = "driver-api")]
impl<'ast> LetExpr<'ast> {
    pub fn new(data: CommonExprData<'ast>, pat: PatKind<'ast>, scrutinee: ExprKind<'ast>) -> Self {
        Self { data, pat, scrutinee }
    }
}

/// A match expression with a scrutinee and [`MatchArm`]s
///
/// ```
/// # let scrutinee: &[i32] = &[1, 2];
/// //    vvvvvvvvv The scrutinee of the expression
/// match scrutinee {
///     // v Arm 0
///     [] => println!("Such much empty"),
///     // v Arm 1
///     [x] if *x == 1 => println!("found a one"),
///     // v Arm 2
///     _ => {
///        // A block as the arm expression
///        println!("default branch");
///     },
/// }
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct MatchExpr<'ast> {
    data: CommonExprData<'ast>,
    scrutinee: ExprKind<'ast>,
    arms: FfiSlice<'ast, MatchArm<'ast>>,
}

impl<'ast> MatchExpr<'ast> {
    pub fn scrutinee(&self) -> ExprKind<'ast> {
        self.scrutinee
    }

    pub fn arms(&self) -> &[MatchArm<'ast>] {
        self.arms.get()
    }
}

super::impl_expr_data!(MatchExpr<'ast>, Match);

#[cfg(feature = "driver-api")]
impl<'ast> MatchExpr<'ast> {
    pub fn new(data: CommonExprData<'ast>, scrutinee: ExprKind<'ast>, arms: &'ast [MatchArm<'ast>]) -> Self {
        Self {
            data,
            scrutinee,
            arms: arms.into(),
        }
    }
}

/// An arm inside a [`MatchExpr`] with an optional guard.
///
/// ```
/// # let scrutinee: &[i32] = &[1, 2];
/// match scrutinee {
/// //  vvvvv A branch with a pattern
///     [] => println!("Such much empty"),
///
/// //  vvv The pattern of the arm
///     [x] if *x == 1 => println!("found a one"),
/// //         ^^^^^^^ The guard expression of the arm
///
/// //   v A wildcard pattern used as a default branch
///      _ => {
///         // A block as the arm expression
///         println!("default branch");
///      },
/// }
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct MatchArm<'ast> {
    span: SpanId,
    pat: PatKind<'ast>,
    guard: FfiOption<ExprKind<'ast>>,
    expr: ExprKind<'ast>,
}

impl<'ast> MatchArm<'ast> {
    pub fn span(&self) -> &Span<'ast> {
        with_cx(self, |cx| cx.span(self.span))
    }

    pub fn pat(&self) -> PatKind<'ast> {
        self.pat
    }

    pub fn guard(&self) -> Option<ExprKind<'ast>> {
        self.guard.copy()
    }

    pub fn expr(&self) -> ExprKind<'ast> {
        self.expr
    }

    // FIXME(xFrednet): Add `fn attrs() -> ??? {}`, see rust-marker/marker#51
}

#[cfg(feature = "driver-api")]
impl<'ast> MatchArm<'ast> {
    pub fn new(span: SpanId, pat: PatKind<'ast>, guard: Option<ExprKind<'ast>>, expr: ExprKind<'ast>) -> Self {
        Self {
            span,
            pat,
            guard: guard.into(),
            expr,
        }
    }
}

/// A return expression with an optional value.
///
/// ```
/// pub fn foo(a: bool) {
///     if a {
///         return;
///     //  ^^^^^^ A return expression without a value
///     }
///     // ...
/// }
///
/// pub fn bar(b: bool) -> i32 {
///     if b {
///     //  vvvvvvvvvvvvv A return expression with a value
///         return 0xcafe;
///     //         ^^^^^^ The value of the return expression
///     }
///
///     0xbeef
/// //  ^^^^^^ This is the value of the function body and
/// //         not a return expression
/// }
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct ReturnExpr<'ast> {
    data: CommonExprData<'ast>,
    expr: FfiOption<ExprKind<'ast>>,
}

impl<'ast> ReturnExpr<'ast> {
    pub fn expr(&self) -> Option<ExprKind<'ast>> {
        self.expr.copy()
    }
}

super::impl_expr_data!(ReturnExpr<'ast>, Return);

#[cfg(feature = "driver-api")]
impl<'ast> ReturnExpr<'ast> {
    pub fn new(data: CommonExprData<'ast>, expr: Option<ExprKind<'ast>>) -> Self {
        Self {
            data,
            expr: expr.into(),
        }
    }
}

/// A break expression with an optional label and an optional value.
///
/// ```
/// for i in 0..10 {
///     if i == 2 {
///         break;
///     //  ^^^^^ A break expression targeting the for loop
///     }
/// }
///
/// let _ = 'label: {
/// //  vvvvvvvvvvvvvv A break expression with a label and a value
///     break 'label 4;
/// //        ^^^^^^ ^ An integer literal being returned as a value of the target
/// //           |
/// //           An optional label, specifying which labeled expression is the target
/// };
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct BreakExpr<'ast> {
    data: CommonExprData<'ast>,
    label: FfiOption<Ident<'ast>>,
    target_id: ExprId,
    expr: FfiOption<ExprKind<'ast>>,
}

impl<'ast> BreakExpr<'ast> {
    pub fn label(&self) -> Option<&Ident<'ast>> {
        self.label.get()
    }

    pub fn target_id(&self) -> ExprId {
        self.target_id
    }

    pub fn expr(&self) -> Option<ExprKind<'ast>> {
        self.expr.copy()
    }
}

super::impl_expr_data!(BreakExpr<'ast>, Break);

#[cfg(feature = "driver-api")]
impl<'ast> BreakExpr<'ast> {
    pub fn new(
        data: CommonExprData<'ast>,
        label: Option<Ident<'ast>>,
        target_id: ExprId,
        expr: Option<ExprKind<'ast>>,
    ) -> Self {
        Self {
            data,
            label: label.into(),
            target_id,
            expr: expr.into(),
        }
    }
}

/// A continue expression with an optional label.
///
/// ```
/// for i in 0..10 {
///     if i == 2 {
///         continue;
///     //  ^^^^^^^^ A continue expression targeting the for loop
///     }
/// }
///
/// 'label: for a in 0..100 {
///     for b in 0..a {
///         if b == 2 {
///         //  vvvvvvvvvvvvvvv The continue expression targeting the outer loop
///             continue 'label;
///         //           ^^^^^^ The label identifying the target loop
///         }
///         // ...
///     }
/// }
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct ContinueExpr<'ast> {
    data: CommonExprData<'ast>,
    label: FfiOption<Ident<'ast>>,
    target_id: ExprId,
}

impl<'ast> ContinueExpr<'ast> {
    pub fn label(&self) -> Option<&Ident<'ast>> {
        self.label.get()
    }

    pub fn target_id(&self) -> ExprId {
        self.target_id
    }
}

super::impl_expr_data!(ContinueExpr<'ast>, Continue);

#[cfg(feature = "driver-api")]
impl<'ast> ContinueExpr<'ast> {
    pub fn new(data: CommonExprData<'ast>, label: Option<Ident<'ast>>, target_id: ExprId) -> Self {
        Self {
            data,
            label: label.into(),
            target_id,
        }
    }
}

/// An unconditional loop expression
///
/// ```
/// # // The `if false` is needed as `cargo test --doc` would not terminate otherwise
/// # if false {
///     //      vvvvvv A loop expression
///     let _ = loop {
///         break 3;
///     //  ^^^^^^^ A break expression targeting the loop and returning a value
///     };
///
/// //  vvvvvvvvvvvvvvvvvv An infinite loop
///     'infinite: loop {};
/// //  ^^^^^^^^^       ^^ A block expression as the loop body expression
/// //      |
/// //      An optional label to be targeted by break and continue expressions
/// # }
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct LoopExpr<'ast> {
    data: CommonExprData<'ast>,
    label: FfiOption<Ident<'ast>>,
    block: ExprKind<'ast>,
}

impl<'ast> LoopExpr<'ast> {
    pub fn label(&self) -> Option<&Ident<'ast>> {
        self.label.get()
    }

    pub fn block(&self) -> ExprKind<'ast> {
        self.block
    }
}

super::impl_expr_data!(LoopExpr<'ast>, Loop);

#[cfg(feature = "driver-api")]
impl<'ast> LoopExpr<'ast> {
    pub fn new(data: CommonExprData<'ast>, label: Option<Ident<'ast>>, block: ExprKind<'ast>) -> Self {
        Self {
            data,
            label: label.into(),
            block,
        }
    }
}

/// A `while` loop expression
///
/// ```
///     # let run = false;
/// //  vvvvvvvvvvvv The while loop expression
///     while run {}
/// //        ^^^ ^^ The loop body
/// //         |
/// //         The loop condition
///
///     # let maybe: Option<i32> = None;
/// //  vvvvvv An optional label to be targeted by break and continue expressions
///     'label: while let Some(_) = maybe {}
/// //                ^^^^^^^^^^^^^^^^^^^ A condition using pattern matching
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct WhileExpr<'ast> {
    data: CommonExprData<'ast>,
    label: FfiOption<Ident<'ast>>,
    condition: ExprKind<'ast>,
    block: ExprKind<'ast>,
}

impl<'ast> WhileExpr<'ast> {
    pub fn label(&self) -> Option<&Ident<'ast>> {
        self.label.get()
    }

    pub fn condition(&self) -> ExprKind {
        self.condition
    }

    pub fn block(&self) -> ExprKind<'ast> {
        self.block
    }
}

super::impl_expr_data!(WhileExpr<'ast>, While);

#[cfg(feature = "driver-api")]
impl<'ast> WhileExpr<'ast> {
    pub fn new(
        data: CommonExprData<'ast>,
        label: Option<Ident<'ast>>,
        condition: ExprKind<'ast>,
        block: ExprKind<'ast>,
    ) -> Self {
        Self {
            data,
            condition,
            label: label.into(),
            block,
        }
    }
}

/// A `for` loop iterating over values.
///
/// ```
/// //  vvvvvvvvvvvvvvvvv The for loop expression
///     for i in 0..16 {}
/// //      ^    ^^^^^ A range as the iterable
/// //      |
/// //      A pattern introducing `i` as the iter variable
///
///     # let tuple_iter = [(1, 2)];
/// //  vvvvvv An optional label to be targeted by break and continue expressions
///     'label: for (a, b) in tuple_iter {}
/// //              ^^^^^^ A pattern matching the values of the iterable
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct ForExpr<'ast> {
    data: CommonExprData<'ast>,
    label: FfiOption<Ident<'ast>>,
    pat: PatKind<'ast>,
    iterable: ExprKind<'ast>,
    block: ExprKind<'ast>,
}

impl<'ast> ForExpr<'ast> {
    pub fn label(&self) -> Option<&Ident<'ast>> {
        self.label.get()
    }

    pub fn pat(&self) -> PatKind<'ast> {
        self.pat
    }

    pub fn iterable(&self) -> ExprKind {
        self.iterable
    }

    pub fn block(&self) -> ExprKind<'ast> {
        self.block
    }
}

super::impl_expr_data!(ForExpr<'ast>, For);

#[cfg(feature = "driver-api")]
impl<'ast> ForExpr<'ast> {
    pub fn new(
        data: CommonExprData<'ast>,
        label: Option<Ident<'ast>>,
        pat: PatKind<'ast>,
        iterable: ExprKind<'ast>,
        block: ExprKind<'ast>,
    ) -> Self {
        Self {
            data,
            label: label.into(),
            pat,
            iterable,
            block,
        }
    }
}