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