diff options
Diffstat (limited to 'crates/ra_syntax/src/ast.rs')
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 199 |
1 files changed, 84 insertions, 115 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 9ab59738f..285dda1e0 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -1,119 +1,88 @@ | |||
1 | mod generated; | 1 | mod generated; |
2 | 2 | ||
3 | use std::marker::PhantomData; | 3 | use std::marker::PhantomData; |
4 | use std::string::String as RustString; | ||
5 | 4 | ||
6 | use itertools::Itertools; | 5 | use itertools::Itertools; |
7 | 6 | ||
8 | pub use self::generated::*; | 7 | pub use self::generated::*; |
9 | use crate::{ | 8 | use crate::{ |
10 | yellow::{RefRoot, SyntaxNodeChildren}, | 9 | yellow::{SyntaxNode, SyntaxNodeChildren, TreePtr, RaTypes}, |
11 | SmolStr, | 10 | SmolStr, |
12 | SyntaxKind::*, | 11 | SyntaxKind::*, |
13 | SyntaxNodeRef, | ||
14 | }; | 12 | }; |
15 | 13 | ||
16 | /// The main trait to go from untyped `SyntaxNode` to a typed ast. The | 14 | /// The main trait to go from untyped `SyntaxNode` to a typed ast. The |
17 | /// conversion itself has zero runtime cost: ast and syntax nodes have exactly | 15 | /// conversion itself has zero runtime cost: ast and syntax nodes have exactly |
18 | /// the same representation: a pointer to the tree root and a pointer to the | 16 | /// the same representation: a pointer to the tree root and a pointer to the |
19 | /// node itself. | 17 | /// node itself. |
20 | pub trait AstNode<'a>: Clone + Copy + 'a { | 18 | pub trait AstNode: rowan::TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>> { |
21 | fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> | 19 | fn cast(syntax: &SyntaxNode) -> Option<&Self> |
22 | where | 20 | where |
23 | Self: Sized; | 21 | Self: Sized; |
24 | fn syntax(self) -> SyntaxNodeRef<'a>; | 22 | fn syntax(&self) -> &SyntaxNode; |
23 | fn to_owned(&self) -> TreePtr<Self>; | ||
25 | } | 24 | } |
26 | 25 | ||
27 | pub trait NameOwner<'a>: AstNode<'a> { | 26 | pub trait NameOwner: AstNode { |
28 | fn name(self) -> Option<Name<'a>> { | 27 | fn name(&self) -> Option<&Name> { |
29 | child_opt(self) | 28 | child_opt(self) |
30 | } | 29 | } |
31 | } | 30 | } |
32 | 31 | ||
33 | pub trait VisibilityOwner<'a>: AstNode<'a> { | 32 | pub trait VisibilityOwner: AstNode { |
34 | fn visibility(self) -> Option<Visibility<'a>> { | 33 | fn visibility(&self) -> Option<&Visibility> { |
35 | child_opt(self) | 34 | child_opt(self) |
36 | } | 35 | } |
37 | } | 36 | } |
38 | 37 | ||
39 | pub trait LoopBodyOwner<'a>: AstNode<'a> { | 38 | pub trait LoopBodyOwner: AstNode { |
40 | fn loop_body(self) -> Option<Block<'a>> { | 39 | fn loop_body(&self) -> Option<&Block> { |
41 | child_opt(self) | 40 | child_opt(self) |
42 | } | 41 | } |
43 | } | 42 | } |
44 | 43 | ||
45 | pub trait ArgListOwner<'a>: AstNode<'a> { | 44 | pub trait ArgListOwner: AstNode { |
46 | fn arg_list(self) -> Option<ArgList<'a>> { | 45 | fn arg_list(&self) -> Option<&ArgList> { |
47 | child_opt(self) | 46 | child_opt(self) |
48 | } | 47 | } |
49 | } | 48 | } |
50 | 49 | ||
51 | pub trait FnDefOwner<'a>: AstNode<'a> { | 50 | pub trait FnDefOwner: AstNode { |
52 | fn functions(self) -> AstChildren<'a, FnDef<'a>> { | 51 | fn functions(&self) -> AstChildren<FnDef> { |
53 | children(self) | 52 | children(self) |
54 | } | 53 | } |
55 | } | 54 | } |
56 | 55 | ||
57 | // ModuleItem | 56 | pub trait ModuleItemOwner: AstNode { |
58 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 57 | fn items(&self) -> AstChildren<ModuleItem> { |
59 | pub enum ItemOrMacro<'a> { | ||
60 | Item(ModuleItem<'a>), | ||
61 | Macro(MacroCall<'a>), | ||
62 | } | ||
63 | |||
64 | impl<'a> AstNode<'a> for ItemOrMacro<'a> { | ||
65 | fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { | ||
66 | let res = if let Some(item) = ModuleItem::cast(syntax) { | ||
67 | ItemOrMacro::Item(item) | ||
68 | } else if let Some(macro_call) = MacroCall::cast(syntax) { | ||
69 | ItemOrMacro::Macro(macro_call) | ||
70 | } else { | ||
71 | return None; | ||
72 | }; | ||
73 | Some(res) | ||
74 | } | ||
75 | fn syntax(self) -> SyntaxNodeRef<'a> { | ||
76 | match self { | ||
77 | ItemOrMacro::Item(it) => it.syntax(), | ||
78 | ItemOrMacro::Macro(it) => it.syntax(), | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | pub trait ModuleItemOwner<'a>: AstNode<'a> { | ||
84 | fn items(self) -> AstChildren<'a, ModuleItem<'a>> { | ||
85 | children(self) | ||
86 | } | ||
87 | |||
88 | fn items_with_macros(self) -> AstChildren<'a, ItemOrMacro<'a>> { | ||
89 | children(self) | 58 | children(self) |
90 | } | 59 | } |
91 | } | 60 | } |
92 | 61 | ||
93 | pub trait TypeParamsOwner<'a>: AstNode<'a> { | 62 | pub trait TypeParamsOwner: AstNode { |
94 | fn type_param_list(self) -> Option<TypeParamList<'a>> { | 63 | fn type_param_list(&self) -> Option<&TypeParamList> { |
95 | child_opt(self) | 64 | child_opt(self) |
96 | } | 65 | } |
97 | 66 | ||
98 | fn where_clause(self) -> Option<WhereClause<'a>> { | 67 | fn where_clause(&self) -> Option<&WhereClause> { |
99 | child_opt(self) | 68 | child_opt(self) |
100 | } | 69 | } |
101 | } | 70 | } |
102 | 71 | ||
103 | pub trait AttrsOwner<'a>: AstNode<'a> { | 72 | pub trait AttrsOwner: AstNode { |
104 | fn attrs(self) -> AstChildren<'a, Attr<'a>> { | 73 | fn attrs(&self) -> AstChildren<Attr> { |
105 | children(self) | 74 | children(self) |
106 | } | 75 | } |
107 | } | 76 | } |
108 | 77 | ||
109 | pub trait DocCommentsOwner<'a>: AstNode<'a> { | 78 | pub trait DocCommentsOwner: AstNode { |
110 | fn doc_comments(self) -> AstChildren<'a, Comment<'a>> { | 79 | fn doc_comments(&self) -> AstChildren<Comment> { |
111 | children(self) | 80 | children(self) |
112 | } | 81 | } |
113 | 82 | ||
114 | /// Returns the textual content of a doc comment block as a single string. | 83 | /// Returns the textual content of a doc comment block as a single string. |
115 | /// That is, strips leading `///` and joins lines | 84 | /// That is, strips leading `///` and joins lines |
116 | fn doc_comment_text(self) -> RustString { | 85 | fn doc_comment_text(&self) -> std::string::String { |
117 | self.doc_comments() | 86 | self.doc_comments() |
118 | .filter(|comment| comment.is_doc_comment()) | 87 | .filter(|comment| comment.is_doc_comment()) |
119 | .map(|comment| { | 88 | .map(|comment| { |
@@ -130,13 +99,13 @@ pub trait DocCommentsOwner<'a>: AstNode<'a> { | |||
130 | } | 99 | } |
131 | } | 100 | } |
132 | 101 | ||
133 | impl<'a> FnDef<'a> { | 102 | impl FnDef { |
134 | pub fn has_atom_attr(&self, atom: &str) -> bool { | 103 | pub fn has_atom_attr(&self, atom: &str) -> bool { |
135 | self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) | 104 | self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) |
136 | } | 105 | } |
137 | } | 106 | } |
138 | 107 | ||
139 | impl<'a> Attr<'a> { | 108 | impl Attr { |
140 | pub fn as_atom(&self) -> Option<SmolStr> { | 109 | pub fn as_atom(&self) -> Option<SmolStr> { |
141 | let tt = self.value()?; | 110 | let tt = self.value()?; |
142 | let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; | 111 | let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; |
@@ -147,7 +116,7 @@ impl<'a> Attr<'a> { | |||
147 | } | 116 | } |
148 | } | 117 | } |
149 | 118 | ||
150 | pub fn as_call(&self) -> Option<(SmolStr, TokenTree<'a>)> { | 119 | pub fn as_call(&self) -> Option<(SmolStr, &TokenTree)> { |
151 | let tt = self.value()?; | 120 | let tt = self.value()?; |
152 | let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; | 121 | let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; |
153 | let args = TokenTree::cast(args)?; | 122 | let args = TokenTree::cast(args)?; |
@@ -159,37 +128,37 @@ impl<'a> Attr<'a> { | |||
159 | } | 128 | } |
160 | } | 129 | } |
161 | 130 | ||
162 | impl<'a> Lifetime<'a> { | 131 | impl Lifetime { |
163 | pub fn text(&self) -> SmolStr { | 132 | pub fn text(&self) -> SmolStr { |
164 | self.syntax().leaf_text().unwrap().clone() | 133 | self.syntax().leaf_text().unwrap().clone() |
165 | } | 134 | } |
166 | } | 135 | } |
167 | 136 | ||
168 | impl<'a> Char<'a> { | 137 | impl Char { |
169 | pub fn text(&self) -> &SmolStr { | 138 | pub fn text(&self) -> &SmolStr { |
170 | &self.syntax().leaf_text().unwrap() | 139 | &self.syntax().leaf_text().unwrap() |
171 | } | 140 | } |
172 | } | 141 | } |
173 | 142 | ||
174 | impl<'a> Byte<'a> { | 143 | impl Byte { |
175 | pub fn text(&self) -> &SmolStr { | 144 | pub fn text(&self) -> &SmolStr { |
176 | &self.syntax().leaf_text().unwrap() | 145 | &self.syntax().leaf_text().unwrap() |
177 | } | 146 | } |
178 | } | 147 | } |
179 | 148 | ||
180 | impl<'a> ByteString<'a> { | 149 | impl ByteString { |
181 | pub fn text(&self) -> &SmolStr { | 150 | pub fn text(&self) -> &SmolStr { |
182 | &self.syntax().leaf_text().unwrap() | 151 | &self.syntax().leaf_text().unwrap() |
183 | } | 152 | } |
184 | } | 153 | } |
185 | 154 | ||
186 | impl<'a> String<'a> { | 155 | impl String { |
187 | pub fn text(&self) -> &SmolStr { | 156 | pub fn text(&self) -> &SmolStr { |
188 | &self.syntax().leaf_text().unwrap() | 157 | &self.syntax().leaf_text().unwrap() |
189 | } | 158 | } |
190 | } | 159 | } |
191 | 160 | ||
192 | impl<'a> Comment<'a> { | 161 | impl Comment { |
193 | pub fn text(&self) -> &SmolStr { | 162 | pub fn text(&self) -> &SmolStr { |
194 | self.syntax().leaf_text().unwrap() | 163 | self.syntax().leaf_text().unwrap() |
195 | } | 164 | } |
@@ -251,7 +220,7 @@ impl CommentFlavor { | |||
251 | } | 220 | } |
252 | } | 221 | } |
253 | 222 | ||
254 | impl<'a> Whitespace<'a> { | 223 | impl Whitespace { |
255 | pub fn text(&self) -> &SmolStr { | 224 | pub fn text(&self) -> &SmolStr { |
256 | &self.syntax().leaf_text().unwrap() | 225 | &self.syntax().leaf_text().unwrap() |
257 | } | 226 | } |
@@ -265,36 +234,36 @@ impl<'a> Whitespace<'a> { | |||
265 | } | 234 | } |
266 | } | 235 | } |
267 | 236 | ||
268 | impl<'a> Name<'a> { | 237 | impl Name { |
269 | pub fn text(&self) -> SmolStr { | 238 | pub fn text(&self) -> SmolStr { |
270 | let ident = self.syntax().first_child().unwrap(); | 239 | let ident = self.syntax().first_child().unwrap(); |
271 | ident.leaf_text().unwrap().clone() | 240 | ident.leaf_text().unwrap().clone() |
272 | } | 241 | } |
273 | } | 242 | } |
274 | 243 | ||
275 | impl<'a> NameRef<'a> { | 244 | impl NameRef { |
276 | pub fn text(&self) -> SmolStr { | 245 | pub fn text(&self) -> SmolStr { |
277 | let ident = self.syntax().first_child().unwrap(); | 246 | let ident = self.syntax().first_child().unwrap(); |
278 | ident.leaf_text().unwrap().clone() | 247 | ident.leaf_text().unwrap().clone() |
279 | } | 248 | } |
280 | } | 249 | } |
281 | 250 | ||
282 | impl<'a> ImplBlock<'a> { | 251 | impl ImplBlock { |
283 | pub fn target_type(self) -> Option<TypeRef<'a>> { | 252 | pub fn target_type(&self) -> Option<&TypeRef> { |
284 | match self.target() { | 253 | match self.target() { |
285 | (Some(t), None) | (_, Some(t)) => Some(t), | 254 | (Some(t), None) | (_, Some(t)) => Some(t), |
286 | _ => None, | 255 | _ => None, |
287 | } | 256 | } |
288 | } | 257 | } |
289 | 258 | ||
290 | pub fn target_trait(self) -> Option<TypeRef<'a>> { | 259 | pub fn target_trait(&self) -> Option<&TypeRef> { |
291 | match self.target() { | 260 | match self.target() { |
292 | (Some(t), Some(_)) => Some(t), | 261 | (Some(t), Some(_)) => Some(t), |
293 | _ => None, | 262 | _ => None, |
294 | } | 263 | } |
295 | } | 264 | } |
296 | 265 | ||
297 | fn target(self) -> (Option<TypeRef<'a>>, Option<TypeRef<'a>>) { | 266 | fn target(&self) -> (Option<&TypeRef>, Option<&TypeRef>) { |
298 | let mut types = children(self); | 267 | let mut types = children(self); |
299 | let first = types.next(); | 268 | let first = types.next(); |
300 | let second = types.next(); | 269 | let second = types.next(); |
@@ -302,8 +271,8 @@ impl<'a> ImplBlock<'a> { | |||
302 | } | 271 | } |
303 | } | 272 | } |
304 | 273 | ||
305 | impl<'a> Module<'a> { | 274 | impl Module { |
306 | pub fn has_semi(self) -> bool { | 275 | pub fn has_semi(&self) -> bool { |
307 | match self.syntax().last_child() { | 276 | match self.syntax().last_child() { |
308 | None => false, | 277 | None => false, |
309 | Some(node) => node.kind() == SEMI, | 278 | Some(node) => node.kind() == SEMI, |
@@ -311,8 +280,8 @@ impl<'a> Module<'a> { | |||
311 | } | 280 | } |
312 | } | 281 | } |
313 | 282 | ||
314 | impl<'a> LetStmt<'a> { | 283 | impl LetStmt { |
315 | pub fn has_semi(self) -> bool { | 284 | pub fn has_semi(&self) -> bool { |
316 | match self.syntax().last_child() { | 285 | match self.syntax().last_child() { |
317 | None => false, | 286 | None => false, |
318 | Some(node) => node.kind() == SEMI, | 287 | Some(node) => node.kind() == SEMI, |
@@ -320,35 +289,35 @@ impl<'a> LetStmt<'a> { | |||
320 | } | 289 | } |
321 | } | 290 | } |
322 | 291 | ||
323 | impl<'a> IfExpr<'a> { | 292 | impl IfExpr { |
324 | pub fn then_branch(self) -> Option<Block<'a>> { | 293 | pub fn then_branch(&self) -> Option<&Block> { |
325 | self.blocks().nth(0) | 294 | self.blocks().nth(0) |
326 | } | 295 | } |
327 | pub fn else_branch(self) -> Option<Block<'a>> { | 296 | pub fn else_branch(&self) -> Option<&Block> { |
328 | self.blocks().nth(1) | 297 | self.blocks().nth(1) |
329 | } | 298 | } |
330 | fn blocks(self) -> AstChildren<'a, Block<'a>> { | 299 | fn blocks(&self) -> AstChildren<Block> { |
331 | children(self) | 300 | children(self) |
332 | } | 301 | } |
333 | } | 302 | } |
334 | 303 | ||
335 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 304 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
336 | pub enum PathSegmentKind<'a> { | 305 | pub enum PathSegmentKind<'a> { |
337 | Name(NameRef<'a>), | 306 | Name(&'a NameRef), |
338 | SelfKw, | 307 | SelfKw, |
339 | SuperKw, | 308 | SuperKw, |
340 | CrateKw, | 309 | CrateKw, |
341 | } | 310 | } |
342 | 311 | ||
343 | impl<'a> PathSegment<'a> { | 312 | impl PathSegment { |
344 | pub fn parent_path(self) -> Path<'a> { | 313 | pub fn parent_path(&self) -> &Path { |
345 | self.syntax() | 314 | self.syntax() |
346 | .parent() | 315 | .parent() |
347 | .and_then(Path::cast) | 316 | .and_then(Path::cast) |
348 | .expect("segments are always nested in paths") | 317 | .expect("segments are always nested in paths") |
349 | } | 318 | } |
350 | 319 | ||
351 | pub fn kind(self) -> Option<PathSegmentKind<'a>> { | 320 | pub fn kind(&self) -> Option<PathSegmentKind> { |
352 | let res = if let Some(name_ref) = self.name_ref() { | 321 | let res = if let Some(name_ref) = self.name_ref() { |
353 | PathSegmentKind::Name(name_ref) | 322 | PathSegmentKind::Name(name_ref) |
354 | } else { | 323 | } else { |
@@ -363,20 +332,20 @@ impl<'a> PathSegment<'a> { | |||
363 | } | 332 | } |
364 | } | 333 | } |
365 | 334 | ||
366 | impl<'a> Path<'a> { | 335 | impl Path { |
367 | pub fn parent_path(self) -> Option<Path<'a>> { | 336 | pub fn parent_path(&self) -> Option<&Path> { |
368 | self.syntax().parent().and_then(Path::cast) | 337 | self.syntax().parent().and_then(Path::cast) |
369 | } | 338 | } |
370 | } | 339 | } |
371 | 340 | ||
372 | impl<'a> UseTree<'a> { | 341 | impl UseTree { |
373 | pub fn has_star(self) -> bool { | 342 | pub fn has_star(&self) -> bool { |
374 | self.syntax().children().any(|it| it.kind() == STAR) | 343 | self.syntax().children().any(|it| it.kind() == STAR) |
375 | } | 344 | } |
376 | } | 345 | } |
377 | 346 | ||
378 | impl<'a> UseTreeList<'a> { | 347 | impl UseTreeList { |
379 | pub fn parent_use_tree(self) -> UseTree<'a> { | 348 | pub fn parent_use_tree(&self) -> &UseTree { |
380 | self.syntax() | 349 | self.syntax() |
381 | .parent() | 350 | .parent() |
382 | .and_then(UseTree::cast) | 351 | .and_then(UseTree::cast) |
@@ -384,22 +353,22 @@ impl<'a> UseTreeList<'a> { | |||
384 | } | 353 | } |
385 | } | 354 | } |
386 | 355 | ||
387 | fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> { | 356 | fn child_opt<P: AstNode, C: AstNode>(parent: &P) -> Option<&C> { |
388 | children(parent).next() | 357 | children(parent).next() |
389 | } | 358 | } |
390 | 359 | ||
391 | fn children<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> AstChildren<'a, C> { | 360 | fn children<P: AstNode, C: AstNode>(parent: &P) -> AstChildren<C> { |
392 | AstChildren::new(parent.syntax()) | 361 | AstChildren::new(parent.syntax()) |
393 | } | 362 | } |
394 | 363 | ||
395 | #[derive(Debug)] | 364 | #[derive(Debug)] |
396 | pub struct AstChildren<'a, N> { | 365 | pub struct AstChildren<'a, N> { |
397 | inner: SyntaxNodeChildren<RefRoot<'a>>, | 366 | inner: SyntaxNodeChildren<'a>, |
398 | ph: PhantomData<N>, | 367 | ph: PhantomData<N>, |
399 | } | 368 | } |
400 | 369 | ||
401 | impl<'a, N> AstChildren<'a, N> { | 370 | impl<'a, N> AstChildren<'a, N> { |
402 | fn new(parent: SyntaxNodeRef<'a>) -> Self { | 371 | fn new(parent: &'a SyntaxNode) -> Self { |
403 | AstChildren { | 372 | AstChildren { |
404 | inner: parent.children(), | 373 | inner: parent.children(), |
405 | ph: PhantomData, | 374 | ph: PhantomData, |
@@ -407,9 +376,9 @@ impl<'a, N> AstChildren<'a, N> { | |||
407 | } | 376 | } |
408 | } | 377 | } |
409 | 378 | ||
410 | impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> { | 379 | impl<'a, N: AstNode + 'a> Iterator for AstChildren<'a, N> { |
411 | type Item = N; | 380 | type Item = &'a N; |
412 | fn next(&mut self) -> Option<N> { | 381 | fn next(&mut self) -> Option<&'a N> { |
413 | loop { | 382 | loop { |
414 | if let Some(n) = N::cast(self.inner.next()?) { | 383 | if let Some(n) = N::cast(self.inner.next()?) { |
415 | return Some(n); | 384 | return Some(n); |
@@ -420,13 +389,13 @@ impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> { | |||
420 | 389 | ||
421 | #[derive(Debug, Clone, PartialEq, Eq)] | 390 | #[derive(Debug, Clone, PartialEq, Eq)] |
422 | pub enum StructFlavor<'a> { | 391 | pub enum StructFlavor<'a> { |
423 | Tuple(PosFieldList<'a>), | 392 | Tuple(&'a PosFieldList), |
424 | Named(NamedFieldDefList<'a>), | 393 | Named(&'a NamedFieldDefList), |
425 | Unit, | 394 | Unit, |
426 | } | 395 | } |
427 | 396 | ||
428 | impl<'a> StructFlavor<'a> { | 397 | impl StructFlavor<'_> { |
429 | fn from_node<N: AstNode<'a>>(node: N) -> StructFlavor<'a> { | 398 | fn from_node<N: AstNode>(node: &N) -> StructFlavor { |
430 | if let Some(nfdl) = child_opt::<_, NamedFieldDefList>(node) { | 399 | if let Some(nfdl) = child_opt::<_, NamedFieldDefList>(node) { |
431 | StructFlavor::Named(nfdl) | 400 | StructFlavor::Named(nfdl) |
432 | } else if let Some(pfl) = child_opt::<_, PosFieldList>(node) { | 401 | } else if let Some(pfl) = child_opt::<_, PosFieldList>(node) { |
@@ -437,31 +406,31 @@ impl<'a> StructFlavor<'a> { | |||
437 | } | 406 | } |
438 | } | 407 | } |
439 | 408 | ||
440 | impl<'a> StructDef<'a> { | 409 | impl StructDef { |
441 | pub fn flavor(self) -> StructFlavor<'a> { | 410 | pub fn flavor(&self) -> StructFlavor { |
442 | StructFlavor::from_node(self) | 411 | StructFlavor::from_node(self) |
443 | } | 412 | } |
444 | } | 413 | } |
445 | 414 | ||
446 | impl<'a> EnumVariant<'a> { | 415 | impl EnumVariant { |
447 | pub fn flavor(self) -> StructFlavor<'a> { | 416 | pub fn flavor(&self) -> StructFlavor { |
448 | StructFlavor::from_node(self) | 417 | StructFlavor::from_node(self) |
449 | } | 418 | } |
450 | } | 419 | } |
451 | 420 | ||
452 | impl<'a> PointerType<'a> { | 421 | impl PointerType { |
453 | pub fn is_mut(&self) -> bool { | 422 | pub fn is_mut(&self) -> bool { |
454 | self.syntax().children().any(|n| n.kind() == MUT_KW) | 423 | self.syntax().children().any(|n| n.kind() == MUT_KW) |
455 | } | 424 | } |
456 | } | 425 | } |
457 | 426 | ||
458 | impl<'a> ReferenceType<'a> { | 427 | impl ReferenceType { |
459 | pub fn is_mut(&self) -> bool { | 428 | pub fn is_mut(&self) -> bool { |
460 | self.syntax().children().any(|n| n.kind() == MUT_KW) | 429 | self.syntax().children().any(|n| n.kind() == MUT_KW) |
461 | } | 430 | } |
462 | } | 431 | } |
463 | 432 | ||
464 | impl<'a> RefExpr<'a> { | 433 | impl RefExpr { |
465 | pub fn is_mut(&self) -> bool { | 434 | pub fn is_mut(&self) -> bool { |
466 | self.syntax().children().any(|n| n.kind() == MUT_KW) | 435 | self.syntax().children().any(|n| n.kind() == MUT_KW) |
467 | } | 436 | } |
@@ -477,7 +446,7 @@ pub enum PrefixOp { | |||
477 | Neg, | 446 | Neg, |
478 | } | 447 | } |
479 | 448 | ||
480 | impl<'a> PrefixExpr<'a> { | 449 | impl PrefixExpr { |
481 | pub fn op(&self) -> Option<PrefixOp> { | 450 | pub fn op(&self) -> Option<PrefixOp> { |
482 | match self.syntax().first_child()?.kind() { | 451 | match self.syntax().first_child()?.kind() { |
483 | STAR => Some(PrefixOp::Deref), | 452 | STAR => Some(PrefixOp::Deref), |
@@ -552,7 +521,7 @@ pub enum BinOp { | |||
552 | BitXorAssign, | 521 | BitXorAssign, |
553 | } | 522 | } |
554 | 523 | ||
555 | impl<'a> BinExpr<'a> { | 524 | impl BinExpr { |
556 | pub fn op(&self) -> Option<BinOp> { | 525 | pub fn op(&self) -> Option<BinOp> { |
557 | self.syntax() | 526 | self.syntax() |
558 | .children() | 527 | .children() |
@@ -592,15 +561,15 @@ impl<'a> BinExpr<'a> { | |||
592 | .next() | 561 | .next() |
593 | } | 562 | } |
594 | 563 | ||
595 | pub fn lhs(self) -> Option<Expr<'a>> { | 564 | pub fn lhs(&self) -> Option<&Expr> { |
596 | children(self).nth(0) | 565 | children(self).nth(0) |
597 | } | 566 | } |
598 | 567 | ||
599 | pub fn rhs(self) -> Option<Expr<'a>> { | 568 | pub fn rhs(&self) -> Option<&Expr> { |
600 | children(self).nth(1) | 569 | children(self).nth(1) |
601 | } | 570 | } |
602 | 571 | ||
603 | pub fn sub_exprs(self) -> (Option<Expr<'a>>, Option<Expr<'a>>) { | 572 | pub fn sub_exprs(&self) -> (Option<&Expr>, Option<&Expr>) { |
604 | let mut children = children(self); | 573 | let mut children = children(self); |
605 | let first = children.next(); | 574 | let first = children.next(); |
606 | let second = children.next(); | 575 | let second = children.next(); |
@@ -618,7 +587,7 @@ pub enum SelfParamFlavor { | |||
618 | MutRef, | 587 | MutRef, |
619 | } | 588 | } |
620 | 589 | ||
621 | impl<'a> SelfParam<'a> { | 590 | impl SelfParam { |
622 | pub fn flavor(&self) -> SelfParamFlavor { | 591 | pub fn flavor(&self) -> SelfParamFlavor { |
623 | let borrowed = self.syntax().children().any(|n| n.kind() == AMP); | 592 | let borrowed = self.syntax().children().any(|n| n.kind() == AMP); |
624 | if borrowed { | 593 | if borrowed { |
@@ -641,7 +610,7 @@ impl<'a> SelfParam<'a> { | |||
641 | 610 | ||
642 | #[test] | 611 | #[test] |
643 | fn test_doc_comment_of_items() { | 612 | fn test_doc_comment_of_items() { |
644 | let file = SourceFileNode::parse( | 613 | let file = SourceFile::parse( |
645 | r#" | 614 | r#" |
646 | //! doc | 615 | //! doc |
647 | // non-doc | 616 | // non-doc |