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