Skip to content

Commit a0d4abd

Browse files
committed
Implement overriding Combine not operator
1 parent 86c39f2 commit a0d4abd

File tree

6 files changed

+70
-22
lines changed

6 files changed

+70
-22
lines changed

src/clang_ql/matchers/combine.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use super::Matcher;
2+
3+
#[derive(Clone)]
4+
enum CombineUnaryMatcherKind {
5+
Not,
6+
}
7+
8+
#[derive(Clone)]
9+
pub struct UnaryCombineMatcher<T> {
10+
matcher: Box<dyn Matcher<T>>,
11+
kind: CombineUnaryMatcherKind,
12+
}
13+
14+
impl<T: Clone> UnaryCombineMatcher<T> {
15+
pub fn not(matcher: Box<dyn Matcher<T>>) -> Self {
16+
UnaryCombineMatcher {
17+
matcher,
18+
kind: CombineUnaryMatcherKind::Not,
19+
}
20+
}
21+
}
22+
23+
impl<T: Clone> Matcher<T> for UnaryCombineMatcher<T> {
24+
fn is_match(&self, node: &T) -> bool {
25+
match &self.kind {
26+
CombineUnaryMatcherKind::Not => !self.matcher.is_match(node),
27+
}
28+
}
29+
}

src/clang_ql/matchers/function.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ use clang_sys::CX_CXXPublic;
1818

1919
use crate::clang_ql::values::FunctionNode;
2020

21-
use super::FunctionMatcher;
21+
use super::Matcher;
2222

2323
#[derive(Clone)]
2424
pub struct IsVirtualMatcher;
2525

26-
impl FunctionMatcher for IsVirtualMatcher {
26+
impl Matcher<FunctionNode> for IsVirtualMatcher {
2727
fn is_match(&self, function: &FunctionNode) -> bool {
2828
unsafe { clang_CXXMethod_isVirtual(function.cursor) != 0 }
2929
}
@@ -32,7 +32,7 @@ impl FunctionMatcher for IsVirtualMatcher {
3232
#[derive(Clone)]
3333
pub struct IsPureVirtualMatcher;
3434

35-
impl FunctionMatcher for IsPureVirtualMatcher {
35+
impl Matcher<FunctionNode> for IsPureVirtualMatcher {
3636
fn is_match(&self, function: &FunctionNode) -> bool {
3737
unsafe { clang_CXXMethod_isPureVirtual(function.cursor) != 0 }
3838
}
@@ -41,7 +41,7 @@ impl FunctionMatcher for IsPureVirtualMatcher {
4141
#[derive(Clone)]
4242
pub struct IsStaticMethodMatcher;
4343

44-
impl FunctionMatcher for IsStaticMethodMatcher {
44+
impl Matcher<FunctionNode> for IsStaticMethodMatcher {
4545
fn is_match(&self, function: &FunctionNode) -> bool {
4646
unsafe { clang_CXXMethod_isStatic(function.cursor) != 0 }
4747
}
@@ -50,7 +50,7 @@ impl FunctionMatcher for IsStaticMethodMatcher {
5050
#[derive(Clone)]
5151
pub struct IsConstMethodMatcher;
5252

53-
impl FunctionMatcher for IsConstMethodMatcher {
53+
impl Matcher<FunctionNode> for IsConstMethodMatcher {
5454
fn is_match(&self, function: &FunctionNode) -> bool {
5555
unsafe { clang_CXXMethod_isConst(function.cursor) != 0 }
5656
}
@@ -59,7 +59,7 @@ impl FunctionMatcher for IsConstMethodMatcher {
5959
#[derive(Clone)]
6060
pub struct IsDeletedMethodMatcher;
6161

62-
impl FunctionMatcher for IsDeletedMethodMatcher {
62+
impl Matcher<FunctionNode> for IsDeletedMethodMatcher {
6363
fn is_match(&self, function: &FunctionNode) -> bool {
6464
unsafe { clang_CXXMethod_isConst(function.cursor) != 0 }
6565
}
@@ -68,7 +68,7 @@ impl FunctionMatcher for IsDeletedMethodMatcher {
6868
#[derive(Clone)]
6969
pub struct IsMethodMatcher;
7070

71-
impl FunctionMatcher for IsMethodMatcher {
71+
impl Matcher<FunctionNode> for IsMethodMatcher {
7272
fn is_match(&self, function: &FunctionNode) -> bool {
7373
unsafe { clang_getCursorKind(function.cursor) == CXCursor_CXXMethod }
7474
}
@@ -77,7 +77,7 @@ impl FunctionMatcher for IsMethodMatcher {
7777
#[derive(Clone)]
7878
pub struct IsConstructorMatcher;
7979

80-
impl FunctionMatcher for IsConstructorMatcher {
80+
impl Matcher<FunctionNode> for IsConstructorMatcher {
8181
fn is_match(&self, function: &FunctionNode) -> bool {
8282
unsafe { clang_getCursorKind(function.cursor) == CXCursor_Constructor }
8383
}
@@ -86,7 +86,7 @@ impl FunctionMatcher for IsConstructorMatcher {
8686
#[derive(Clone)]
8787
pub struct IsDefaultConstructorMatcher;
8888

89-
impl FunctionMatcher for IsDefaultConstructorMatcher {
89+
impl Matcher<FunctionNode> for IsDefaultConstructorMatcher {
9090
fn is_match(&self, function: &FunctionNode) -> bool {
9191
unsafe { clang_CXXConstructor_isDefaultConstructor(function.cursor) != 0 }
9292
}
@@ -95,7 +95,7 @@ impl FunctionMatcher for IsDefaultConstructorMatcher {
9595
#[derive(Clone)]
9696
pub struct IsCopyConstructorMatcher;
9797

98-
impl FunctionMatcher for IsCopyConstructorMatcher {
98+
impl Matcher<FunctionNode> for IsCopyConstructorMatcher {
9999
fn is_match(&self, function: &FunctionNode) -> bool {
100100
unsafe { clang_CXXConstructor_isCopyConstructor(function.cursor) != 0 }
101101
}
@@ -104,7 +104,7 @@ impl FunctionMatcher for IsCopyConstructorMatcher {
104104
#[derive(Clone)]
105105
pub struct IsMoveConstructorMatcher;
106106

107-
impl FunctionMatcher for IsMoveConstructorMatcher {
107+
impl Matcher<FunctionNode> for IsMoveConstructorMatcher {
108108
fn is_match(&self, function: &FunctionNode) -> bool {
109109
unsafe { clang_CXXConstructor_isMoveConstructor(function.cursor) != 0 }
110110
}
@@ -113,7 +113,7 @@ impl FunctionMatcher for IsMoveConstructorMatcher {
113113
#[derive(Clone)]
114114
pub struct IsConvertingConstructorMatcher;
115115

116-
impl FunctionMatcher for IsConvertingConstructorMatcher {
116+
impl Matcher<FunctionNode> for IsConvertingConstructorMatcher {
117117
fn is_match(&self, function: &FunctionNode) -> bool {
118118
unsafe { clang_CXXConstructor_isConvertingConstructor(function.cursor) != 0 }
119119
}
@@ -122,7 +122,7 @@ impl FunctionMatcher for IsConvertingConstructorMatcher {
122122
#[derive(Clone)]
123123
pub struct IsDestructorMatcher;
124124

125-
impl FunctionMatcher for IsDestructorMatcher {
125+
impl Matcher<FunctionNode> for IsDestructorMatcher {
126126
fn is_match(&self, function: &FunctionNode) -> bool {
127127
unsafe { clang_getCursorKind(function.cursor) == CXCursor_Destructor }
128128
}
@@ -153,7 +153,7 @@ impl AccessSpecifierMatcher {
153153
}
154154
}
155155

156-
impl FunctionMatcher for AccessSpecifierMatcher {
156+
impl Matcher<FunctionNode> for AccessSpecifierMatcher {
157157
fn is_match(&self, function: &FunctionNode) -> bool {
158158
unsafe { clang_getCXXAccessSpecifier(function.cursor) == self.access }
159159
}

src/clang_ql/matchers/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use dyn_clone::DynClone;
22

3-
use super::values::FunctionNode;
4-
53
mod function;
64
pub use function::AccessSpecifierMatcher;
75
pub use function::IsConstMethodMatcher;
@@ -17,8 +15,11 @@ pub use function::IsPureVirtualMatcher;
1715
pub use function::IsStaticMethodMatcher;
1816
pub use function::IsVirtualMatcher;
1917

20-
dyn_clone::clone_trait_object!(FunctionMatcher);
18+
mod combine;
19+
pub use combine::UnaryCombineMatcher;
20+
21+
dyn_clone::clone_trait_object!(<T> Matcher<T>);
2122

22-
pub trait FunctionMatcher: DynClone {
23-
fn is_match(&self, node: &FunctionNode) -> bool;
23+
pub trait Matcher<T: Clone>: DynClone {
24+
fn is_match(&self, node: &T) -> bool;
2425
}

src/clang_ql/types/matcher.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ impl DataType for FunctionMatcherType {
2424
fn as_any(&self) -> &dyn Any {
2525
self
2626
}
27+
28+
fn can_perform_bang_op(&self) -> bool {
29+
true
30+
}
31+
32+
fn bang_op_result_type(&self) -> Box<dyn DataType> {
33+
Box::new(FunctionMatcherType)
34+
}
2735
}

src/clang_ql/values/function.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use super::FileLocation;
99
pub struct FunctionNode {
1010
pub name: String,
1111
pub cursor: CXCursor,
12+
13+
#[allow(dead_code)]
1214
pub parent: CXCursor,
15+
1316
pub signature: String,
1417
pub return_type: String,
1518
pub location: FileLocation,

src/clang_ql/values/matcher.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use gitql_core::values::base::Value;
22

3-
use crate::clang_ql::matchers::FunctionMatcher;
3+
use crate::clang_ql::matchers::{Matcher, UnaryCombineMatcher};
44
use crate::clang_ql::types::FunctionMatcherType;
55

6+
use super::FunctionNode;
7+
68
#[derive(Clone)]
79
pub struct FunctionMatcherValue {
8-
pub matcher: Box<dyn FunctionMatcher>,
10+
pub matcher: Box<dyn Matcher<FunctionNode>>,
911
}
1012

1113
impl FunctionMatcherValue {
12-
pub fn new(matcher: Box<dyn FunctionMatcher>) -> Self {
14+
pub fn new(matcher: Box<dyn Matcher<FunctionNode>>) -> Self {
1315
FunctionMatcherValue { matcher }
1416
}
1517
}
@@ -34,4 +36,9 @@ impl Value for FunctionMatcherValue {
3436
fn as_any(&self) -> &dyn std::any::Any {
3537
self
3638
}
39+
40+
fn bang_op(&self) -> Result<Box<dyn Value>, String> {
41+
let combine_matcher = Box::new(UnaryCombineMatcher::not(self.matcher.clone()));
42+
Ok(Box::new(FunctionMatcherValue::new(combine_matcher)))
43+
}
3744
}

0 commit comments

Comments
 (0)