-
Notifications
You must be signed in to change notification settings - Fork 182
/
ast.rs
571 lines (505 loc) · 12 KB
/
ast.rs
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
565
566
567
568
569
570
571
use std::fmt;
use string_cache::DefaultAtom as Atom;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Span {
pub lo: usize,
pub hi: usize,
}
impl Span {
pub fn new(lo: usize, hi: usize) -> Self {
Span { lo, hi }
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Program {
pub items: Vec<Item>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Item {
AdtDefn(AdtDefn),
FnDefn(FnDefn),
ClosureDefn(ClosureDefn),
TraitDefn(TraitDefn),
OpaqueTyDefn(OpaqueTyDefn),
CoroutineDefn(CoroutineDefn),
Impl(Impl),
Clause(Clause),
Foreign(ForeignDefn),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ForeignDefn(pub Identifier);
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AdtDefn {
pub name: Identifier,
pub variable_kinds: Vec<VariableKind>,
pub where_clauses: Vec<QuantifiedWhereClause>,
pub variants: Vec<Variant>,
pub flags: AdtFlags,
pub repr: AdtRepr,
pub variances: Option<Vec<Variance>>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Variant {
pub name: Identifier,
pub fields: Vec<Field>,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Movability {
Static,
Movable,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CoroutineDefn {
pub name: Identifier,
pub movability: Movability,
pub variable_kinds: Vec<VariableKind>,
pub upvars: Vec<Ty>,
pub resume_ty: Ty,
pub yield_ty: Ty,
pub return_ty: Ty,
pub witness_types: Vec<Ty>,
pub witness_lifetimes: Vec<Identifier>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AdtFlags {
pub upstream: bool,
pub fundamental: bool,
pub phantom_data: bool,
pub one_zst: bool,
pub kind: AdtKind,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum AdtKind {
Struct,
Enum,
Union,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum AdtReprAttr {
C,
Packed,
Int(Ty),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AdtRepr {
pub c: bool,
pub packed: bool,
pub int: Option<Ty>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FnSig {
pub abi: FnAbi,
pub safety: Safety,
pub variadic: bool,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FnDefn {
pub name: Identifier,
pub variable_kinds: Vec<VariableKind>,
pub where_clauses: Vec<QuantifiedWhereClause>,
pub argument_types: Vec<Ty>,
pub return_type: Ty,
pub sig: FnSig,
pub variances: Option<Vec<Variance>>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ClosureDefn {
pub name: Identifier,
pub kind: ClosureKind,
pub variable_kinds: Vec<VariableKind>,
pub argument_types: Vec<Ty>,
pub return_type: Ty,
pub upvars: Vec<Ty>,
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct FnAbi(pub Atom);
impl Default for FnAbi {
fn default() -> Self {
FnAbi(Atom::from("Rust"))
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TraitDefn {
pub name: Identifier,
pub variable_kinds: Vec<VariableKind>,
pub where_clauses: Vec<QuantifiedWhereClause>,
pub assoc_ty_defns: Vec<AssocTyDefn>,
pub flags: TraitFlags,
pub well_known: Option<WellKnownTrait>,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum WellKnownTrait {
Sized,
Copy,
Clone,
Drop,
FnOnce,
FnMut,
Fn,
Unsize,
Unpin,
CoerceUnsized,
DiscriminantKind,
Coroutine,
DispatchFromDyn,
Tuple,
Pointee,
FnPtr,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TraitFlags {
pub auto: bool,
pub marker: bool,
pub upstream: bool,
pub fundamental: bool,
pub non_enumerable: bool,
pub coinductive: bool,
pub object_safe: bool,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AssocTyDefn {
pub name: Identifier,
pub variable_kinds: Vec<VariableKind>,
pub bounds: Vec<QuantifiedInlineBound>,
pub where_clauses: Vec<QuantifiedWhereClause>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct OpaqueTyDefn {
pub ty: Ty,
pub variable_kinds: Vec<VariableKind>,
pub name: Identifier,
pub bounds: Vec<QuantifiedInlineBound>,
pub where_clauses: Vec<QuantifiedWhereClause>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum VariableKind {
Ty(Identifier),
IntegerTy(Identifier),
FloatTy(Identifier),
Lifetime(Identifier),
Const(Identifier),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum GenericArg {
Ty(Ty),
Lifetime(Lifetime),
Id(Identifier),
Const(Const),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Const {
Id(Identifier),
Value(u32),
}
#[derive(Clone, PartialEq, Eq, Debug)]
/// An inline bound, e.g. `: Foo<K>` in `impl<K, T: Foo<K>> SomeType<T>`.
pub enum InlineBound {
TraitBound(TraitBound),
AliasEqBound(AliasEqBound),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct QuantifiedInlineBound {
pub variable_kinds: Vec<VariableKind>,
pub bound: InlineBound,
}
#[derive(Clone, PartialEq, Eq, Debug)]
/// Represents a trait bound on e.g. a type or type parameter.
/// Does not know anything about what it's binding.
pub struct TraitBound {
pub trait_name: Identifier,
pub args_no_self: Vec<GenericArg>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
/// Represents an alias equality bound on e.g. a type or type parameter.
/// Does not know anything about what it's binding.
pub struct AliasEqBound {
pub trait_bound: TraitBound,
pub name: Identifier,
pub args: Vec<GenericArg>,
pub value: Ty,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Kind {
Ty,
Lifetime,
Const,
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
Kind::Ty => "type",
Kind::Lifetime => "lifetime",
Kind::Const => "const",
})
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Impl {
pub variable_kinds: Vec<VariableKind>,
pub trait_ref: TraitRef,
pub polarity: Polarity,
pub where_clauses: Vec<QuantifiedWhereClause>,
pub assoc_ty_values: Vec<AssocTyValue>,
pub impl_type: ImplType,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ImplType {
Local,
External,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AssocTyValue {
pub name: Identifier,
pub variable_kinds: Vec<VariableKind>,
pub value: Ty,
pub default: bool,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Ty {
Id {
name: Identifier,
},
Dyn {
bounds: Vec<QuantifiedInlineBound>,
lifetime: Lifetime,
},
Apply {
name: Identifier,
args: Vec<GenericArg>,
},
Projection {
proj: ProjectionTy,
},
ForAll {
lifetime_names: Vec<Identifier>,
types: Vec<Box<Ty>>,
sig: FnSig,
},
Tuple {
types: Vec<Box<Ty>>,
},
Scalar {
ty: ScalarType,
},
Slice {
ty: Box<Ty>,
},
Array {
ty: Box<Ty>,
len: Const,
},
Raw {
mutability: Mutability,
ty: Box<Ty>,
},
Ref {
mutability: Mutability,
lifetime: Lifetime,
ty: Box<Ty>,
},
Str,
Never,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum IntTy {
Isize,
I8,
I16,
I32,
I64,
I128,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum UintTy {
Usize,
U8,
U16,
U32,
U64,
U128,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum FloatTy {
F16,
F32,
F64,
F128,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ScalarType {
Bool,
Char,
Int(IntTy),
Uint(UintTy),
Float(FloatTy),
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Mutability {
Mut,
Not,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Safety {
Safe,
Unsafe,
}
impl Default for Safety {
fn default() -> Self {
Self::Safe
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Lifetime {
Id { name: Identifier },
Static,
Erased,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ProjectionTy {
pub trait_ref: TraitRef,
pub name: Identifier,
pub args: Vec<GenericArg>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TraitRef {
pub trait_name: Identifier,
pub args: Vec<GenericArg>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Polarity {
/// `impl Foo for Bar`
Positive,
/// `impl !Foo for Bar`
Negative,
}
impl Polarity {
pub fn from_bool(polarity: bool) -> Polarity {
if polarity {
Polarity::Positive
} else {
Polarity::Negative
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Identifier {
pub str: Atom,
pub span: Span,
}
impl fmt::Display for Identifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.str)
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum WhereClause {
Implemented { trait_ref: TraitRef },
ProjectionEq { projection: ProjectionTy, ty: Ty },
LifetimeOutlives { a: Lifetime, b: Lifetime },
TypeOutlives { ty: Ty, lifetime: Lifetime },
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum DomainGoal {
Holds { where_clause: WhereClause },
Normalize { projection: ProjectionTy, ty: Ty },
TraitRefWellFormed { trait_ref: TraitRef },
TyWellFormed { ty: Ty },
TyFromEnv { ty: Ty },
TraitRefFromEnv { trait_ref: TraitRef },
IsLocal { ty: Ty },
IsUpstream { ty: Ty },
IsFullyVisible { ty: Ty },
LocalImplAllowed { trait_ref: TraitRef },
Compatible,
DownstreamType { ty: Ty },
Reveal,
ObjectSafe { id: Identifier },
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum LeafGoal {
DomainGoal { goal: DomainGoal },
UnifyGenericArgs { a: GenericArg, b: GenericArg },
SubtypeGenericArgs { a: Ty, b: Ty },
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct QuantifiedWhereClause {
pub variable_kinds: Vec<VariableKind>,
pub where_clause: WhereClause,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Field {
pub name: Identifier,
pub ty: Ty,
}
#[derive(Clone, PartialEq, Eq, Debug)]
/// This allows users to add arbitrary `A :- B` clauses into the
/// logic; it has no equivalent in Rust, but it's useful for testing.
pub struct Clause {
pub variable_kinds: Vec<VariableKind>,
pub consequence: DomainGoal,
pub conditions: Vec<Box<Goal>>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Goal {
ForAll(Vec<VariableKind>, Box<Goal>),
Exists(Vec<VariableKind>, Box<Goal>),
Implies(Vec<Clause>, Box<Goal>),
And(Box<Goal>, Vec<Box<Goal>>),
Not(Box<Goal>),
/// The `compatible { G }` syntax
Compatible(Box<Goal>),
// Additional kinds of goals:
Leaf(LeafGoal),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ClosureKind {
Fn,
FnMut,
FnOnce,
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum FnArg {
NonVariadic(Ty),
Variadic,
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum FnArgs {
NonVariadic(Vec<Ty>),
Variadic(Vec<Ty>),
}
impl FnArgs {
pub fn is_variadic(&self) -> bool {
matches!(self, Self::Variadic(..))
}
pub fn to_tys(self) -> Vec<Ty> {
match self {
Self::NonVariadic(tys) | Self::Variadic(tys) => tys,
}
}
pub fn from_vec(mut args: Vec<FnArg>) -> Result<Self, &'static str> {
let mut tys = Vec::with_capacity(args.len());
let last = args.pop();
for arg in args {
match arg {
FnArg::NonVariadic(ty) => tys.push(ty),
FnArg::Variadic => {
return Err("a variadic argument must be the last parameter in a function");
}
}
}
Ok(match last {
Some(FnArg::NonVariadic(ty)) => {
tys.push(ty);
FnArgs::NonVariadic(tys)
}
Some(FnArg::Variadic) => FnArgs::Variadic(tys),
None => FnArgs::NonVariadic(tys),
})
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Variance {
Invariant,
Covariant,
Contravariant,
}