aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2018-12-25 16:17:39 +0000
committerFlorian Diebold <[email protected]>2018-12-25 19:36:06 +0000
commit2870effd5c69941bbf32a44c0ee6d9d42e0b038d (patch)
treebf2c5ff08e6f316c1d9d629ae3595e6f7c069e5d /crates/ra_syntax
parentb96d3612390e070936a176571c946ad0cafa69a9 (diff)
Implement reference / pointer types
- parse them - infer types of & and * expressions
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast.rs39
-rw-r--r--crates/ra_syntax/src/ast/generated.rs18
-rw-r--r--crates/ra_syntax/src/grammar.ron6
3 files changed, 57 insertions, 6 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 5dbf9b221..8fb6b6408 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -394,3 +394,42 @@ impl<'a> EnumVariant<'a> {
394 StructFlavor::from_node(self) 394 StructFlavor::from_node(self)
395 } 395 }
396} 396}
397
398impl<'a> PointerType<'a> {
399 pub fn is_mut(&self) -> bool {
400 self.syntax().children().any(|n| n.kind() == MUT_KW)
401 }
402}
403
404impl<'a> ReferenceType<'a> {
405 pub fn is_mut(&self) -> bool {
406 self.syntax().children().any(|n| n.kind() == MUT_KW)
407 }
408}
409
410impl<'a> RefExpr<'a> {
411 pub fn is_mut(&self) -> bool {
412 self.syntax().children().any(|n| n.kind() == MUT_KW)
413 }
414}
415
416#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
417pub enum PrefixOp {
418 /// The `*` operator for dereferencing
419 Deref,
420 /// The `!` operator for logical inversion
421 Not,
422 /// The `-` operator for negation
423 Neg,
424}
425
426impl<'a> PrefixExpr<'a> {
427 pub fn op(&self) -> Option<PrefixOp> {
428 match self.syntax().first_child()?.kind() {
429 STAR => Some(PrefixOp::Deref),
430 EXCL => Some(PrefixOp::Not),
431 MINUS => Some(PrefixOp::Neg),
432 _ => None,
433 }
434 }
435}
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 6b2800a0e..535dcc975 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -2607,7 +2607,11 @@ impl<R: TreeRoot<RaTypes>> ParenTypeNode<R> {
2607} 2607}
2608 2608
2609 2609
2610impl<'a> ParenType<'a> {} 2610impl<'a> ParenType<'a> {
2611 pub fn type_ref(self) -> Option<TypeRef<'a>> {
2612 super::child_opt(self)
2613 }
2614}
2611 2615
2612// Pat 2616// Pat
2613#[derive(Debug, Clone, Copy, PartialEq, Eq)] 2617#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -2972,7 +2976,11 @@ impl<R: TreeRoot<RaTypes>> PointerTypeNode<R> {
2972} 2976}
2973 2977
2974 2978
2975impl<'a> PointerType<'a> {} 2979impl<'a> PointerType<'a> {
2980 pub fn type_ref(self) -> Option<TypeRef<'a>> {
2981 super::child_opt(self)
2982 }
2983}
2976 2984
2977// PosField 2985// PosField
2978#[derive(Debug, Clone, Copy,)] 2986#[derive(Debug, Clone, Copy,)]
@@ -3285,7 +3293,11 @@ impl<R: TreeRoot<RaTypes>> ReferenceTypeNode<R> {
3285} 3293}
3286 3294
3287 3295
3288impl<'a> ReferenceType<'a> {} 3296impl<'a> ReferenceType<'a> {
3297 pub fn type_ref(self) -> Option<TypeRef<'a>> {
3298 super::child_opt(self)
3299 }
3300}
3289 3301
3290// RetType 3302// RetType
3291#[derive(Debug, Clone, Copy,)] 3303#[derive(Debug, Clone, Copy,)]
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index dcde32923..8b1bd6d1c 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -303,14 +303,14 @@ Grammar(
303 ] ), 303 ] ),
304 "ImplItem": (), 304 "ImplItem": (),
305 305
306 "ParenType": (), 306 "ParenType": (options: ["TypeRef"]),
307 "TupleType": (), 307 "TupleType": (),
308 "NeverType": (), 308 "NeverType": (),
309 "PathType": (options: ["Path"]), 309 "PathType": (options: ["Path"]),
310 "PointerType": (), 310 "PointerType": (options: ["TypeRef"]),
311 "ArrayType": (), 311 "ArrayType": (),
312 "SliceType": (), 312 "SliceType": (),
313 "ReferenceType": (), 313 "ReferenceType": (options: ["TypeRef"]),
314 "PlaceholderType": (), 314 "PlaceholderType": (),
315 "FnPointerType": (), 315 "FnPointerType": (),
316 "ForType": (), 316 "ForType": (),