17
17
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
18
18
*/
19
19
20
- use proc_macro:: { Diagnostic , Level , TokenStream } ;
20
+ use proc_macro:: TokenStream ;
21
21
use proc_macro2:: { Ident , Span } ;
22
+
22
23
use quote:: quote;
23
- use syn:: { punctuated:: Punctuated , spanned:: Spanned , Expr , FnArg , Pat , Type } ;
24
+
25
+ use syn:: punctuated:: Punctuated ;
26
+ use syn:: spanned:: Spanned ;
27
+ use syn:: { Expr , FnArg , Pat , Type } ;
24
28
25
29
enum ArgType {
26
30
Array ( bool ) , // mutable?
@@ -33,53 +37,42 @@ enum ArgType {
33
37
34
38
pub fn parse ( _: TokenStream , item : TokenStream ) -> TokenStream {
35
39
let parsed_fn = syn:: parse_macro_input!( item as syn:: ItemFn ) ;
40
+ let signature = & parsed_fn. sig ;
36
41
37
- if let Some ( constness) = parsed_fn. sig . constness {
38
- Diagnostic :: spanned (
39
- constness. span ( ) . unwrap ( ) ,
40
- Level :: Error ,
41
- "syscall functions cannot be const" ,
42
- )
43
- . emit ( ) ;
44
- }
42
+ signature
43
+ . constness
44
+ . map ( |e| emit_error ! ( e. span( ) , "syscall functions cannot be `const`" ) ) ;
45
45
46
- if let Some ( asyncness) = parsed_fn. sig . asyncness {
47
- Diagnostic :: spanned (
48
- asyncness. span ( ) . unwrap ( ) ,
49
- Level :: Error ,
50
- "syscall functions cannot be async" ,
51
- )
52
- . emit ( ) ;
53
- }
46
+ signature
47
+ . asyncness
48
+ . map ( |e| emit_error ! ( e. span( ) , "syscall functions cannot be `async`" ) ) ;
54
49
55
- if let Some ( unsafety) = parsed_fn. sig . unsafety {
56
- Diagnostic :: spanned (
57
- unsafety. span ( ) . unwrap ( ) ,
58
- Level :: Error ,
59
- "syscall functions cannot be unsafe" ,
60
- )
61
- . emit ( ) ;
62
- }
50
+ signature
51
+ . unsafety
52
+ . map ( |e| emit_error ! ( e. span( ) , "syscalls functions cannot be `unsafe`" ) ) ;
53
+
54
+ let generics = & signature. generics ;
55
+
56
+ // NOTE: if `lt_token` is present then `gt_token` will also be present (else invalid syntax).
57
+ if generics. lt_token . is_some ( ) {
58
+ let lt_span = generics. lt_token . span ( ) . unwrap ( ) ;
59
+ let gt_span = generics. gt_token . span ( ) . unwrap ( ) ;
63
60
64
- if let Some ( _) = parsed_fn. sig . generics . lt_token {
65
- let lt_span = parsed_fn. sig . generics . lt_token . span ( ) . unwrap ( ) ;
66
- let gt_span = parsed_fn. sig . generics . gt_token . span ( ) . unwrap ( ) ;
61
+ let span_range = lt_span. join ( gt_span) . unwrap ( ) ;
67
62
68
- Diagnostic :: spanned (
69
- lt_span. join ( gt_span) . unwrap ( ) ,
70
- Level :: Error ,
71
- "syscall functions cannot have generic parameters" ,
72
- )
73
- . emit ( ) ;
63
+ emit_error ! (
64
+ span_range,
65
+ "syscall functions cannot have generic parameters"
66
+ ) ;
74
67
}
75
68
76
69
let attrs = & parsed_fn. attrs ;
77
70
let vis = & parsed_fn. vis ;
78
- let name = & parsed_fn . sig . ident ;
79
- let orig_args = & parsed_fn . sig . inputs ;
71
+ let name = & signature . ident ;
72
+ let orig_args = & signature . inputs ;
80
73
let processed_args = process_args ( orig_args) ;
81
74
let call_args = process_call_args ( orig_args) ;
82
- let ret = & parsed_fn . sig . output ;
75
+ let ret = & signature . output ;
83
76
let body = & parsed_fn. block ;
84
77
85
78
let result = quote ! {
@@ -150,21 +143,17 @@ fn process_args(args: &Punctuated<FnArg, syn::Token![,]>) -> Vec<FnArg> {
150
143
} ;
151
144
}
152
145
_ => {
153
- Diagnostic :: spanned (
154
- arg. span ( ) . unwrap ( ) ,
155
- Level :: Error ,
156
- "syscall function arguments cannot have non-ident patterns" ,
157
- )
158
- . emit ( ) ;
146
+ emit_error ! (
147
+ arg. span( ) ,
148
+ "syscall function arguments cannot have non-ident patterns"
149
+ ) ;
159
150
}
160
151
} ,
161
152
FnArg :: Receiver ( _) => {
162
- Diagnostic :: spanned (
163
- arg. span ( ) . unwrap ( ) ,
164
- Level :: Error ,
165
- "syscall functions cannot have receiver arguments" ,
166
- )
167
- . emit ( ) ;
153
+ emit_error ! (
154
+ arg. span( ) ,
155
+ "syscall functions cannot have receiver arguments"
156
+ ) ;
168
157
}
169
158
}
170
159
}
0 commit comments