1
1
#![ warn( clippy:: pedantic) ]
2
2
use super :: function:: Function ;
3
- use super :: node:: { Location , Node } ;
3
+ use super :: node:: { Location , Node , TLocation } ;
4
4
use super :: node_type:: {
5
- FunctionCallChildType , FunctionCallParentType , MethodCallChildType , MethodCallParentType ,
6
- NodeType ,
5
+ FunctionCallChildType , FunctionCallParentType , MemberAccessChildType , MemberAccessParentType ,
6
+ MethodCallChildType , MethodCallParentType , NodeKind ,
7
7
} ;
8
8
use soroban_security_rules_macro_lib:: node_location;
9
9
use std:: cell:: RefCell ;
10
10
use std:: rc:: Rc ;
11
- use syn:: spanned:: Spanned ;
12
11
use syn:: { Expr , ExprCall , ExprMethodCall } ;
13
12
14
- #[ derive( Clone ) ]
13
+ #[ derive( Clone , serde :: Serialize , serde :: Deserialize ) ]
15
14
pub enum Expression {
16
15
FunctionCall ( Rc < FunctionCall > ) ,
17
16
MethodCall ( Rc < MethodCall > ) ,
@@ -24,19 +23,21 @@ pub enum ExpressionParentType {
24
23
Expression ( Rc < Expression > ) ,
25
24
}
26
25
27
- #[ node_location( inner = "inner_struct" ) ]
26
+ #[ node_location]
27
+ #[ derive( Clone , serde:: Serialize , serde:: Deserialize ) ]
28
28
pub struct FunctionCall {
29
29
pub id : usize ,
30
- pub ( crate ) inner_struct : Rc < ExprCall > ,
30
+ pub location : Location ,
31
+ pub function_name : String ,
31
32
pub parent : FunctionCallParentType ,
32
33
pub children : Vec < FunctionCallChildType > ,
33
34
pub is_tried : bool ,
34
35
}
35
36
36
37
impl Node for FunctionCall {
37
- fn parent ( & self ) -> Option < NodeType > {
38
+ fn parent ( & self ) -> Option < NodeKind > {
38
39
match & self . parent {
39
- FunctionCallParentType :: Function ( parent) => Some ( NodeType :: Function ( parent. clone ( ) ) ) ,
40
+ FunctionCallParentType :: Function ( parent) => Some ( NodeKind :: Function ( parent. clone ( ) ) ) ,
40
41
}
41
42
}
42
43
@@ -48,8 +49,8 @@ impl Node for FunctionCall {
48
49
49
50
impl FunctionCall {
50
51
#[ must_use]
51
- pub fn function_name ( & self ) -> String {
52
- match self . inner_struct . func . as_ref ( ) {
52
+ pub fn function_name_from_syn_item ( function_call : & ExprCall ) -> String {
53
+ match function_call . func . as_ref ( ) {
53
54
Expr :: Path ( ref expr_path) => expr_path
54
55
. path
55
56
. segments
@@ -61,22 +62,24 @@ impl FunctionCall {
61
62
}
62
63
}
63
64
64
- #[ node_location( inner = "inner_struct" ) ]
65
+ #[ node_location]
66
+ #[ derive( Clone , serde:: Serialize , serde:: Deserialize ) ]
65
67
pub struct MethodCall {
66
68
pub id : usize ,
67
- pub ( crate ) inner_struct : Rc < ExprMethodCall > ,
69
+ pub location : Location ,
70
+ pub method_name : String ,
68
71
pub parent : MethodCallParentType ,
69
72
pub children : RefCell < Vec < MethodCallChildType > > ,
70
73
pub is_tried : bool ,
71
74
}
72
75
73
76
impl Node for MethodCall {
74
- fn parent ( & self ) -> Option < NodeType > {
77
+ fn parent ( & self ) -> Option < NodeKind > {
75
78
match & self . parent {
76
- MethodCallParentType :: Function ( parent) => Some ( NodeType :: Function ( parent. clone ( ) ) ) ,
79
+ MethodCallParentType :: Function ( parent) => Some ( NodeKind :: Function ( parent. clone ( ) ) ) ,
77
80
MethodCallParentType :: Expression ( parent) => match & * * parent {
78
81
Expression :: MethodCall ( parent) => parent. parent ( ) ,
79
- Expression :: FunctionCall ( parent) => NodeType :: FunctionCall ( parent. clone ( ) ) . into ( ) ,
82
+ Expression :: FunctionCall ( parent) => NodeKind :: FunctionCall ( parent. clone ( ) ) . into ( ) ,
80
83
Expression :: Empty => None ,
81
84
} ,
82
85
}
@@ -88,25 +91,77 @@ impl Node for MethodCall {
88
91
}
89
92
}
90
93
94
+ impl MethodCall {
95
+ #[ must_use]
96
+ pub fn method_name_from_syn_item ( method_call : & ExprMethodCall ) -> String {
97
+ method_call. method . to_string ( )
98
+ }
99
+ }
100
+
101
+ #[ node_location]
102
+ #[ derive( serde:: Serialize , serde:: Deserialize ) ]
103
+ pub struct MemberAccess {
104
+ pub id : usize ,
105
+ pub location : Location ,
106
+ pub member_name : String ,
107
+ pub parent : MemberAccessParentType ,
108
+ pub children : Vec < MemberAccessChildType > ,
109
+ pub is_tried : bool ,
110
+ }
111
+
112
+ impl Node for MemberAccess {
113
+ fn parent ( & self ) -> Option < NodeKind > {
114
+ match & self . parent {
115
+ MemberAccessParentType :: Function ( parent) => Some ( NodeKind :: Function ( parent. clone ( ) ) ) ,
116
+ MemberAccessParentType :: Expression ( parent) => match & * * parent {
117
+ Expression :: MethodCall ( parent) => parent. parent ( ) ,
118
+ Expression :: FunctionCall ( parent) => NodeKind :: FunctionCall ( parent. clone ( ) ) . into ( ) ,
119
+ Expression :: Empty => None ,
120
+ } ,
121
+ }
122
+ }
123
+
124
+ #[ allow( refining_impl_trait) ]
125
+ fn children ( & self ) -> impl Iterator < Item = MemberAccessChildType > {
126
+ self . children . iter ( ) . cloned ( )
127
+ }
128
+ }
129
+
130
+ impl MemberAccess {
131
+ // #[must_use]
132
+ // pub fn member_name(&self) -> String {
133
+ // match &self.inner_item.member {
134
+ // syn::Member::Named(ident) => ident.to_string(),
135
+ // syn::Member::Unnamed(index) => index.index.to_string(),
136
+ // }
137
+ // }
138
+
139
+ // #[must_use]
140
+ // pub fn base(&self) -> Expression {
141
+ // self.inner_struct.base
142
+ // }
143
+ }
144
+
91
145
#[ cfg( test) ]
92
146
mod function_call_tests {
93
147
use super :: * ;
94
- use crate :: utils:: test:: create_mock_function;
148
+ use crate :: { location , utils:: test:: create_mock_function} ;
95
149
use syn:: parse_quote;
96
150
97
151
#[ test]
98
152
fn test_function_call_function_name ( ) {
99
- let inner_struct = parse_quote ! {
153
+ let inner_struct: ExprCall = parse_quote ! {
100
154
execute( "Hello, world!" )
101
155
} ;
102
156
let function_call = FunctionCall {
103
157
id : 0 ,
104
- inner_struct : Rc :: new ( inner_struct) ,
158
+ location : location ! ( inner_struct) ,
159
+ function_name : FunctionCall :: function_name_from_syn_item ( & inner_struct) ,
105
160
parent : FunctionCallParentType :: Function ( Rc :: new ( create_mock_function ( 0 ) ) ) ,
106
161
children : vec ! [ ] ,
107
162
is_tried : false ,
108
163
} ;
109
164
110
- assert_eq ! ( function_call. function_name( ) , "execute" ) ;
165
+ assert_eq ! ( function_call. function_name, "execute" ) ;
111
166
}
112
167
}
0 commit comments