aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/ast
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-14 10:38:20 +0100
committerAleksey Kladov <[email protected]>2018-08-14 10:38:20 +0100
commit5953a348bd6102a868f303d3f732a6ec7d465833 (patch)
tree8f4ca16180f850465d871d9b2959d4ae065dcfeb /crates/libsyntax2/src/ast
parentde02d2891ec7f7b295e33887d49de954f677387a (diff)
Less hackish impl structure
Diffstat (limited to 'crates/libsyntax2/src/ast')
-rw-r--r--crates/libsyntax2/src/ast/generated.rs292
-rw-r--r--crates/libsyntax2/src/ast/generated.rs.tera28
-rw-r--r--crates/libsyntax2/src/ast/mod.rs23
3 files changed, 342 insertions, 1 deletions
diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs
index 13668b803..3e6c673ab 100644
--- a/crates/libsyntax2/src/ast/generated.rs
+++ b/crates/libsyntax2/src/ast/generated.rs
@@ -5,6 +5,24 @@ use {
5 SyntaxKind::*, 5 SyntaxKind::*,
6}; 6};
7 7
8// ArrayType
9#[derive(Debug, Clone, Copy)]
10pub struct ArrayType<R: TreeRoot = Arc<SyntaxRoot>> {
11 syntax: SyntaxNode<R>,
12}
13
14impl<R: TreeRoot> AstNode<R> for ArrayType<R> {
15 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
16 match syntax.kind() {
17 ARRAY_TYPE => Some(ArrayType { syntax }),
18 _ => None,
19 }
20 }
21 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
22}
23
24impl<R: TreeRoot> ArrayType<R> {}
25
8// ConstDef 26// ConstDef
9#[derive(Debug, Clone, Copy)] 27#[derive(Debug, Clone, Copy)]
10pub struct ConstDef<R: TreeRoot = Arc<SyntaxRoot>> { 28pub struct ConstDef<R: TreeRoot = Arc<SyntaxRoot>> {
@@ -24,6 +42,24 @@ impl<R: TreeRoot> AstNode<R> for ConstDef<R> {
24impl<R: TreeRoot> ast::NameOwner<R> for ConstDef<R> {} 42impl<R: TreeRoot> ast::NameOwner<R> for ConstDef<R> {}
25impl<R: TreeRoot> ConstDef<R> {} 43impl<R: TreeRoot> ConstDef<R> {}
26 44
45// DynTraitType
46#[derive(Debug, Clone, Copy)]
47pub struct DynTraitType<R: TreeRoot = Arc<SyntaxRoot>> {
48 syntax: SyntaxNode<R>,
49}
50
51impl<R: TreeRoot> AstNode<R> for DynTraitType<R> {
52 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
53 match syntax.kind() {
54 DYN_TRAIT_TYPE => Some(DynTraitType { syntax }),
55 _ => None,
56 }
57 }
58 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
59}
60
61impl<R: TreeRoot> DynTraitType<R> {}
62
27// EnumDef 63// EnumDef
28#[derive(Debug, Clone, Copy)] 64#[derive(Debug, Clone, Copy)]
29pub struct EnumDef<R: TreeRoot = Arc<SyntaxRoot>> { 65pub struct EnumDef<R: TreeRoot = Arc<SyntaxRoot>> {
@@ -86,6 +122,42 @@ impl<R: TreeRoot> AstNode<R> for FnDef<R> {
86impl<R: TreeRoot> ast::NameOwner<R> for FnDef<R> {} 122impl<R: TreeRoot> ast::NameOwner<R> for FnDef<R> {}
87impl<R: TreeRoot> FnDef<R> {} 123impl<R: TreeRoot> FnDef<R> {}
88 124
125// FnPointerType
126#[derive(Debug, Clone, Copy)]
127pub struct FnPointerType<R: TreeRoot = Arc<SyntaxRoot>> {
128 syntax: SyntaxNode<R>,
129}
130
131impl<R: TreeRoot> AstNode<R> for FnPointerType<R> {
132 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
133 match syntax.kind() {
134 FN_POINTER_TYPE => Some(FnPointerType { syntax }),
135 _ => None,
136 }
137 }
138 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
139}
140
141impl<R: TreeRoot> FnPointerType<R> {}
142
143// ForType
144#[derive(Debug, Clone, Copy)]
145pub struct ForType<R: TreeRoot = Arc<SyntaxRoot>> {
146 syntax: SyntaxNode<R>,
147}
148
149impl<R: TreeRoot> AstNode<R> for ForType<R> {
150 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
151 match syntax.kind() {
152 FOR_TYPE => Some(ForType { syntax }),
153 _ => None,
154 }
155 }
156 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
157}
158
159impl<R: TreeRoot> ForType<R> {}
160
89// ImplItem 161// ImplItem
90#[derive(Debug, Clone, Copy)] 162#[derive(Debug, Clone, Copy)]
91pub struct ImplItem<R: TreeRoot = Arc<SyntaxRoot>> { 163pub struct ImplItem<R: TreeRoot = Arc<SyntaxRoot>> {
@@ -104,6 +176,24 @@ impl<R: TreeRoot> AstNode<R> for ImplItem<R> {
104 176
105impl<R: TreeRoot> ImplItem<R> {} 177impl<R: TreeRoot> ImplItem<R> {}
106 178
179// ImplTraitType
180#[derive(Debug, Clone, Copy)]
181pub struct ImplTraitType<R: TreeRoot = Arc<SyntaxRoot>> {
182 syntax: SyntaxNode<R>,
183}
184
185impl<R: TreeRoot> AstNode<R> for ImplTraitType<R> {
186 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
187 match syntax.kind() {
188 IMPL_TRAIT_TYPE => Some(ImplTraitType { syntax }),
189 _ => None,
190 }
191 }
192 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
193}
194
195impl<R: TreeRoot> ImplTraitType<R> {}
196
107// Module 197// Module
108#[derive(Debug, Clone, Copy)] 198#[derive(Debug, Clone, Copy)]
109pub struct Module<R: TreeRoot = Arc<SyntaxRoot>> { 199pub struct Module<R: TreeRoot = Arc<SyntaxRoot>> {
@@ -159,6 +249,132 @@ impl<R: TreeRoot> AstNode<R> for NameRef<R> {
159 249
160impl<R: TreeRoot> NameRef<R> {} 250impl<R: TreeRoot> NameRef<R> {}
161 251
252// NeverType
253#[derive(Debug, Clone, Copy)]
254pub struct NeverType<R: TreeRoot = Arc<SyntaxRoot>> {
255 syntax: SyntaxNode<R>,
256}
257
258impl<R: TreeRoot> AstNode<R> for NeverType<R> {
259 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
260 match syntax.kind() {
261 NEVER_TYPE => Some(NeverType { syntax }),
262 _ => None,
263 }
264 }
265 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
266}
267
268impl<R: TreeRoot> NeverType<R> {}
269
270// ParenType
271#[derive(Debug, Clone, Copy)]
272pub struct ParenType<R: TreeRoot = Arc<SyntaxRoot>> {
273 syntax: SyntaxNode<R>,
274}
275
276impl<R: TreeRoot> AstNode<R> for ParenType<R> {
277 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
278 match syntax.kind() {
279 PAREN_TYPE => Some(ParenType { syntax }),
280 _ => None,
281 }
282 }
283 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
284}
285
286impl<R: TreeRoot> ParenType<R> {}
287
288// PathType
289#[derive(Debug, Clone, Copy)]
290pub struct PathType<R: TreeRoot = Arc<SyntaxRoot>> {
291 syntax: SyntaxNode<R>,
292}
293
294impl<R: TreeRoot> AstNode<R> for PathType<R> {
295 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
296 match syntax.kind() {
297 PATH_TYPE => Some(PathType { syntax }),
298 _ => None,
299 }
300 }
301 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
302}
303
304impl<R: TreeRoot> PathType<R> {}
305
306// PlaceholderType
307#[derive(Debug, Clone, Copy)]
308pub struct PlaceholderType<R: TreeRoot = Arc<SyntaxRoot>> {
309 syntax: SyntaxNode<R>,
310}
311
312impl<R: TreeRoot> AstNode<R> for PlaceholderType<R> {
313 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
314 match syntax.kind() {
315 PLACEHOLDER_TYPE => Some(PlaceholderType { syntax }),
316 _ => None,
317 }
318 }
319 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
320}
321
322impl<R: TreeRoot> PlaceholderType<R> {}
323
324// PointerType
325#[derive(Debug, Clone, Copy)]
326pub struct PointerType<R: TreeRoot = Arc<SyntaxRoot>> {
327 syntax: SyntaxNode<R>,
328}
329
330impl<R: TreeRoot> AstNode<R> for PointerType<R> {
331 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
332 match syntax.kind() {
333 POINTER_TYPE => Some(PointerType { syntax }),
334 _ => None,
335 }
336 }
337 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
338}
339
340impl<R: TreeRoot> PointerType<R> {}
341
342// ReferenceType
343#[derive(Debug, Clone, Copy)]
344pub struct ReferenceType<R: TreeRoot = Arc<SyntaxRoot>> {
345 syntax: SyntaxNode<R>,
346}
347
348impl<R: TreeRoot> AstNode<R> for ReferenceType<R> {
349 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
350 match syntax.kind() {
351 REFERENCE_TYPE => Some(ReferenceType { syntax }),
352 _ => None,
353 }
354 }
355 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
356}
357
358impl<R: TreeRoot> ReferenceType<R> {}
359
360// SliceType
361#[derive(Debug, Clone, Copy)]
362pub struct SliceType<R: TreeRoot = Arc<SyntaxRoot>> {
363 syntax: SyntaxNode<R>,
364}
365
366impl<R: TreeRoot> AstNode<R> for SliceType<R> {
367 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
368 match syntax.kind() {
369 SLICE_TYPE => Some(SliceType { syntax }),
370 _ => None,
371 }
372 }
373 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
374}
375
376impl<R: TreeRoot> SliceType<R> {}
377
162// StaticDef 378// StaticDef
163#[derive(Debug, Clone, Copy)] 379#[derive(Debug, Clone, Copy)]
164pub struct StaticDef<R: TreeRoot = Arc<SyntaxRoot>> { 380pub struct StaticDef<R: TreeRoot = Arc<SyntaxRoot>> {
@@ -216,6 +432,24 @@ impl<R: TreeRoot> AstNode<R> for TraitDef<R> {
216impl<R: TreeRoot> ast::NameOwner<R> for TraitDef<R> {} 432impl<R: TreeRoot> ast::NameOwner<R> for TraitDef<R> {}
217impl<R: TreeRoot> TraitDef<R> {} 433impl<R: TreeRoot> TraitDef<R> {}
218 434
435// TupleType
436#[derive(Debug, Clone, Copy)]
437pub struct TupleType<R: TreeRoot = Arc<SyntaxRoot>> {
438 syntax: SyntaxNode<R>,
439}
440
441impl<R: TreeRoot> AstNode<R> for TupleType<R> {
442 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
443 match syntax.kind() {
444 TUPLE_TYPE => Some(TupleType { syntax }),
445 _ => None,
446 }
447 }
448 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
449}
450
451impl<R: TreeRoot> TupleType<R> {}
452
219// TypeDef 453// TypeDef
220#[derive(Debug, Clone, Copy)] 454#[derive(Debug, Clone, Copy)]
221pub struct TypeDef<R: TreeRoot = Arc<SyntaxRoot>> { 455pub struct TypeDef<R: TreeRoot = Arc<SyntaxRoot>> {
@@ -235,3 +469,61 @@ impl<R: TreeRoot> AstNode<R> for TypeDef<R> {
235impl<R: TreeRoot> ast::NameOwner<R> for TypeDef<R> {} 469impl<R: TreeRoot> ast::NameOwner<R> for TypeDef<R> {}
236impl<R: TreeRoot> TypeDef<R> {} 470impl<R: TreeRoot> TypeDef<R> {}
237 471
472// TypeRef
473#[derive(Debug, Clone, Copy)]
474pub enum TypeRef<R: TreeRoot = Arc<SyntaxRoot>> {
475 ParenType(ParenType<R>),
476 TupleType(TupleType<R>),
477 NeverType(NeverType<R>),
478 PathType(PathType<R>),
479 PointerType(PointerType<R>),
480 ArrayType(ArrayType<R>),
481 SliceType(SliceType<R>),
482 ReferenceType(ReferenceType<R>),
483 PlaceholderType(PlaceholderType<R>),
484 FnPointerType(FnPointerType<R>),
485 ForType(ForType<R>),
486 ImplTraitType(ImplTraitType<R>),
487 DynTraitType(DynTraitType<R>),
488}
489
490impl<R: TreeRoot> AstNode<R> for TypeRef<R> {
491 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
492 match syntax.kind() {
493 PAREN_TYPE => Some(TypeRef::ParenType(ParenType { syntax })),
494 TUPLE_TYPE => Some(TypeRef::TupleType(TupleType { syntax })),
495 NEVER_TYPE => Some(TypeRef::NeverType(NeverType { syntax })),
496 PATH_TYPE => Some(TypeRef::PathType(PathType { syntax })),
497 POINTER_TYPE => Some(TypeRef::PointerType(PointerType { syntax })),
498 ARRAY_TYPE => Some(TypeRef::ArrayType(ArrayType { syntax })),
499 SLICE_TYPE => Some(TypeRef::SliceType(SliceType { syntax })),
500 REFERENCE_TYPE => Some(TypeRef::ReferenceType(ReferenceType { syntax })),
501 PLACEHOLDER_TYPE => Some(TypeRef::PlaceholderType(PlaceholderType { syntax })),
502 FN_POINTER_TYPE => Some(TypeRef::FnPointerType(FnPointerType { syntax })),
503 FOR_TYPE => Some(TypeRef::ForType(ForType { syntax })),
504 IMPL_TRAIT_TYPE => Some(TypeRef::ImplTraitType(ImplTraitType { syntax })),
505 DYN_TRAIT_TYPE => Some(TypeRef::DynTraitType(DynTraitType { syntax })),
506 _ => None,
507 }
508 }
509 fn syntax(&self) -> &SyntaxNode<R> {
510 match self {
511 TypeRef::ParenType(inner) => inner.syntax(),
512 TypeRef::TupleType(inner) => inner.syntax(),
513 TypeRef::NeverType(inner) => inner.syntax(),
514 TypeRef::PathType(inner) => inner.syntax(),
515 TypeRef::PointerType(inner) => inner.syntax(),
516 TypeRef::ArrayType(inner) => inner.syntax(),
517 TypeRef::SliceType(inner) => inner.syntax(),
518 TypeRef::ReferenceType(inner) => inner.syntax(),
519 TypeRef::PlaceholderType(inner) => inner.syntax(),
520 TypeRef::FnPointerType(inner) => inner.syntax(),
521 TypeRef::ForType(inner) => inner.syntax(),
522 TypeRef::ImplTraitType(inner) => inner.syntax(),
523 TypeRef::DynTraitType(inner) => inner.syntax(),
524 }
525 }
526}
527
528impl<R: TreeRoot> TypeRef<R> {}
529
diff --git a/crates/libsyntax2/src/ast/generated.rs.tera b/crates/libsyntax2/src/ast/generated.rs.tera
index 3d79b5543..f83da0326 100644
--- a/crates/libsyntax2/src/ast/generated.rs.tera
+++ b/crates/libsyntax2/src/ast/generated.rs.tera
@@ -6,6 +6,32 @@ use {
6}; 6};
7{% for node, methods in ast %} 7{% for node, methods in ast %}
8// {{ node }} 8// {{ node }}
9{%- if methods.enum %}
10#[derive(Debug, Clone, Copy)]
11pub enum {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> {
12{%- for kind in methods.enum %}
13 {{ kind }}({{ kind }}<R>),
14{%- endfor %}
15}
16
17impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
18 fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
19 match syntax.kind() {
20{%- for kind in methods.enum %}
21 {{ kind | SCREAM }} => Some({{ node }}::{{ kind }}({{ kind }} { syntax })),
22{%- endfor %}
23 _ => None,
24 }
25 }
26 fn syntax(&self) -> &SyntaxNode<R> {
27 match self {
28{%- for kind in methods.enum %}
29 {{ node }}::{{ kind }}(inner) => inner.syntax(),
30{%- endfor %}
31 }
32 }
33}
34{% else %}
9#[derive(Debug, Clone, Copy)] 35#[derive(Debug, Clone, Copy)]
10pub struct {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> { 36pub struct {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> {
11 syntax: SyntaxNode<R>, 37 syntax: SyntaxNode<R>,
@@ -20,7 +46,7 @@ impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
20 } 46 }
21 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } 47 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
22} 48}
23 49{% endif %}
24{% if methods.traits -%} 50{% if methods.traits -%}
25{%- for t in methods.traits -%} 51{%- for t in methods.traits -%}
26impl<R: TreeRoot> ast::{{ t }}<R> for {{ node }}<R> {} 52impl<R: TreeRoot> ast::{{ t }}<R> for {{ node }}<R> {}
diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs
index f001d340e..679e292a2 100644
--- a/crates/libsyntax2/src/ast/mod.rs
+++ b/crates/libsyntax2/src/ast/mod.rs
@@ -82,3 +82,26 @@ impl<R: TreeRoot> NameRef<R> {
82 ident.leaf_text().unwrap() 82 ident.leaf_text().unwrap()
83 } 83 }
84} 84}
85
86impl <R: TreeRoot> ImplItem<R> {
87 pub fn target_type(&self) -> Option<TypeRef<R>> {
88 match self.target() {
89 (Some(t), None) | (_, Some(t)) => Some(t),
90 _ => None,
91 }
92 }
93
94 pub fn target_trait(&self) -> Option<TypeRef<R>> {
95 match self.target() {
96 (Some(t), Some(_)) => Some(t),
97 _ => None,
98 }
99 }
100
101 fn target(&self) -> (Option<TypeRef<R>>, Option<TypeRef<R>>) {
102 let mut types = self.syntax().children().filter_map(TypeRef::cast);
103 let first = types.next();
104 let second = types.next();
105 (first, second)
106 }
107}