forked from rust-lang/chalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.rs
382 lines (336 loc) · 8.09 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
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 {
StructDefn(StructDefn),
FnDefn(FnDefn),
TraitDefn(TraitDefn),
OpaqueTyDefn(OpaqueTyDefn),
Impl(Impl),
Clause(Clause),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct StructDefn {
pub name: Identifier,
pub variable_kinds: Vec<VariableKind>,
pub where_clauses: Vec<QuantifiedWhereClause>,
pub fields: Vec<Field>,
pub flags: StructFlags,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct StructFlags {
pub upstream: bool,
pub fundamental: 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,
}
#[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 {
SizedTrait,
CopyTrait,
CloneTrait,
DropTrait,
}
#[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 identifier: Identifier,
pub bounds: Vec<QuantifiedInlineBound>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum VariableKind {
Ty(Identifier),
Lifetime(Identifier),
Const(Identifier),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum GenericArg {
Ty(Ty),
Lifetime(Lifetime),
Id(Identifier),
ConstValue(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>,
},
Apply {
name: Identifier,
args: Vec<GenericArg>,
},
Projection {
proj: ProjectionTy,
},
ForAll {
lifetime_names: Vec<Identifier>,
ty: Box<Ty>,
},
Tuple {
types: Vec<Box<Ty>>,
},
Scalar {
ty: ScalarType,
},
Slice {
ty: Box<Ty>,
},
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 {
F32,
F64,
}
#[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(Clone, PartialEq, Eq, Debug)]
pub enum Lifetime {
Id { name: Identifier },
}
#[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 },
}
#[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 },
}
#[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),
}