@@ -199,28 +199,16 @@ func_type_add_arg_types(
199199 * Get a type_T for a typval_T.
200200 * "type_list" is used to temporarily create types in.
201201 */
202- type_T *
203- typval2type (typval_T * tv , garray_T * type_gap )
202+ static type_T *
203+ typval2type_int (typval_T * tv , garray_T * type_gap )
204204{
205205 type_T * type ;
206206 type_T * member_type ;
207207
208208 if (tv -> v_type == VAR_NUMBER )
209- {
210- if (tv -> vval .v_number == 0 || tv -> vval .v_number == 1 )
211- {
212- // number 0 and 1 can also be used for bool
213- type = alloc_type (type_gap );
214- if (type == NULL )
215- return NULL ;
216- type -> tt_type = VAR_NUMBER ;
217- type -> tt_flags = TTFLAG_BOOL_OK ;
218- return type ;
219- }
220209 return & t_number ;
221- }
222210 if (tv -> v_type == VAR_BOOL )
223- return & t_bool ; // not used
211+ return & t_bool ;
224212 if (tv -> v_type == VAR_STRING )
225213 return & t_string ;
226214
@@ -297,6 +285,46 @@ typval2type(typval_T *tv, garray_T *type_gap)
297285 return type ;
298286}
299287
288+ /*
289+ * Return TRUE if "tv" is not a bool but should be converted to bool.
290+ */
291+ int
292+ need_convert_to_bool (type_T * type , typval_T * tv )
293+ {
294+ return type != NULL && type == & t_bool && tv -> v_type != VAR_BOOL
295+ && ((tv -> v_lock & VAR_BOOL_OK )
296+ || (tv -> v_type == VAR_NUMBER
297+ && (tv -> vval .v_number == 0 || tv -> vval .v_number == 1 )));
298+ }
299+
300+ /*
301+ * Get a type_T for a typval_T and handle VAR_BOOL_OK.
302+ * "type_list" is used to temporarily create types in.
303+ */
304+ type_T *
305+ typval2type (typval_T * tv , garray_T * type_gap )
306+ {
307+ type_T * type = typval2type_int (tv , type_gap );
308+
309+ if (type != NULL && type != & t_bool
310+ && ((tv -> v_type == VAR_NUMBER
311+ && (tv -> vval .v_number == 0 || tv -> vval .v_number == 1 ))
312+ || (tv -> v_lock & VAR_BOOL_OK )))
313+ {
314+ type_T * newtype = alloc_type (type_gap );
315+
316+ // Number 0 and 1 and expression with "&&" or "||" can also be used
317+ // for bool.
318+ if (newtype != NULL )
319+ {
320+ * newtype = * type ;
321+ newtype -> tt_flags = TTFLAG_BOOL_OK ;
322+ type = newtype ;
323+ }
324+ }
325+ return type ;
326+ }
327+
300328/*
301329 * Get a type_T for a typval_T, used for v: variables.
302330 * "type_list" is used to temporarily create types in.
@@ -371,7 +399,7 @@ check_type(type_T *expected, type_T *actual, int give_msg, int argidx)
371399 {
372400 if (expected -> tt_type != actual -> tt_type )
373401 {
374- if (expected -> tt_type == VAR_BOOL && actual -> tt_type == VAR_NUMBER
402+ if (expected -> tt_type == VAR_BOOL
375403 && (actual -> tt_flags & TTFLAG_BOOL_OK ))
376404 // Using number 0 or 1 for bool is OK.
377405 return OK ;
0 commit comments