7
7
8
8
use crate :: class:: RpcAttr ;
9
9
use crate :: util:: { bail_fn, ident, safe_ident} ;
10
- use crate :: { util, ParseResult } ;
10
+ use crate :: { bail , util, ParseResult } ;
11
11
use proc_macro2:: { Group , Ident , TokenStream , TokenTree } ;
12
12
use quote:: { format_ident, quote} ;
13
13
@@ -126,6 +126,10 @@ pub fn make_method_registration(
126
126
. iter ( )
127
127
. map ( |ident| ident. to_string ( ) ) ;
128
128
129
+ let default_parameters =
130
+ validate_default_parameters ( & func_definition. signature_info . default_parameters ) ?;
131
+
132
+ let default_parameter_types = ( ) ;
129
133
// Transport #[cfg] attrs to the FFI glue to ensure functions which were conditionally
130
134
// removed from compilation don't cause errors.
131
135
let cfg_attrs = util:: extract_cfg_attrs ( & func_definition. external_attributes )
@@ -158,6 +162,9 @@ pub fn make_method_registration(
158
162
& [
159
163
#( #param_ident_strs ) , *
160
164
] ,
165
+ vec![
166
+ #( :: godot:: builtin:: Variant :: from( #default_parameters) ) , *
167
+ ]
161
168
)
162
169
} ;
163
170
@@ -175,6 +182,32 @@ pub fn make_method_registration(
175
182
Ok ( registration)
176
183
}
177
184
185
+ fn validate_default_parameters (
186
+ default_parameters : & [ Option < TokenStream > ] ,
187
+ ) -> ParseResult < Vec < TokenStream > > {
188
+ let mut res = vec ! [ ] ;
189
+ let mut allowed = true ;
190
+ for param in default_parameters. iter ( ) . rev ( ) {
191
+ match ( param, allowed) {
192
+ ( Some ( tk) , true ) => {
193
+ res. push ( tk. clone ( ) ) ; // toreview: if we really care about it, we can use &mut sig_info and mem::take() as we don't use this later
194
+ }
195
+ ( None , true ) => {
196
+ allowed = false ;
197
+ }
198
+ ( None , false ) => { }
199
+ ( Some ( tk) , false ) => {
200
+ return bail ! (
201
+ tk,
202
+ "opt arguments are only allowed at the end of the argument list."
203
+ ) ;
204
+ }
205
+ }
206
+ }
207
+ res. reverse ( ) ;
208
+ Ok ( res)
209
+ }
210
+
178
211
// ----------------------------------------------------------------------------------------------------------------------------------------------
179
212
// Implementation
180
213
@@ -199,6 +232,8 @@ pub struct SignatureInfo {
199
232
///
200
233
/// Index points into original venial tokens (i.e. takes into account potential receiver params).
201
234
pub modified_param_types : Vec < ( usize , venial:: TypeExpr ) > ,
235
+ /// Contains expressions of the default values of parameters.
236
+ pub default_parameters : Vec < Option < TokenStream > > ,
202
237
}
203
238
204
239
impl SignatureInfo {
@@ -210,6 +245,7 @@ impl SignatureInfo {
210
245
param_types : vec ! [ ] ,
211
246
return_type : quote ! { ( ) } ,
212
247
modified_param_types : vec ! [ ] ,
248
+ default_parameters : vec ! [ ] ,
213
249
}
214
250
}
215
251
@@ -412,6 +448,7 @@ pub(crate) fn into_signature_info(
412
448
param_types,
413
449
return_type : ret_type,
414
450
modified_param_types,
451
+ default_parameters : vec ! [ ] ,
415
452
}
416
453
}
417
454
0 commit comments