Implement reference / pointer types

- parse them
 - infer types of & and * expressions
This commit is contained in:
Florian Diebold
2018-12-25 17:17:39 +01:00
parent b96d361239
commit 2870effd5c
7 changed files with 173 additions and 23 deletions

View File

@@ -394,3 +394,42 @@ impl<'a> EnumVariant<'a> {
StructFlavor::from_node(self)
}
}
impl<'a> PointerType<'a> {
pub fn is_mut(&self) -> bool {
self.syntax().children().any(|n| n.kind() == MUT_KW)
}
}
impl<'a> ReferenceType<'a> {
pub fn is_mut(&self) -> bool {
self.syntax().children().any(|n| n.kind() == MUT_KW)
}
}
impl<'a> RefExpr<'a> {
pub fn is_mut(&self) -> bool {
self.syntax().children().any(|n| n.kind() == MUT_KW)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum PrefixOp {
/// The `*` operator for dereferencing
Deref,
/// The `!` operator for logical inversion
Not,
/// The `-` operator for negation
Neg,
}
impl<'a> PrefixExpr<'a> {
pub fn op(&self) -> Option<PrefixOp> {
match self.syntax().first_child()?.kind() {
STAR => Some(PrefixOp::Deref),
EXCL => Some(PrefixOp::Not),
MINUS => Some(PrefixOp::Neg),
_ => None,
}
}
}

View File

@@ -2607,7 +2607,11 @@ impl<R: TreeRoot<RaTypes>> ParenTypeNode<R> {
}
impl<'a> ParenType<'a> {}
impl<'a> ParenType<'a> {
pub fn type_ref(self) -> Option<TypeRef<'a>> {
super::child_opt(self)
}
}
// Pat
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -2972,7 +2976,11 @@ impl<R: TreeRoot<RaTypes>> PointerTypeNode<R> {
}
impl<'a> PointerType<'a> {}
impl<'a> PointerType<'a> {
pub fn type_ref(self) -> Option<TypeRef<'a>> {
super::child_opt(self)
}
}
// PosField
#[derive(Debug, Clone, Copy,)]
@@ -3285,7 +3293,11 @@ impl<R: TreeRoot<RaTypes>> ReferenceTypeNode<R> {
}
impl<'a> ReferenceType<'a> {}
impl<'a> ReferenceType<'a> {
pub fn type_ref(self) -> Option<TypeRef<'a>> {
super::child_opt(self)
}
}
// RetType
#[derive(Debug, Clone, Copy,)]

View File

@@ -303,14 +303,14 @@ Grammar(
] ),
"ImplItem": (),
"ParenType": (),
"ParenType": (options: ["TypeRef"]),
"TupleType": (),
"NeverType": (),
"PathType": (options: ["Path"]),
"PointerType": (),
"PointerType": (options: ["TypeRef"]),
"ArrayType": (),
"SliceType": (),
"ReferenceType": (),
"ReferenceType": (options: ["TypeRef"]),
"PlaceholderType": (),
"FnPointerType": (),
"ForType": (),