-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvim9type.c
1754 lines (1593 loc) · 44.5 KB
/
vim9type.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* vim9type.c: handling of types
*/
#define USING_FLOAT_STUFF
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
#ifdef VMS
# include <float.h>
#endif
// When not generating protos this is included in proto.h
#ifdef PROTO
# include "vim9.h"
#endif
/*
* Allocate memory for a type_T and add the pointer to type_gap, so that it can
* be easily freed later.
*/
type_T *
get_type_ptr(garray_T *type_gap)
{
type_T *type;
if (ga_grow(type_gap, 1) == FAIL)
return NULL;
type = ALLOC_CLEAR_ONE(type_T);
if (type == NULL)
return NULL;
((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
++type_gap->ga_len;
return type;
}
/*
* Make a shallow copy of "type".
* When allocation fails returns "type".
*/
type_T *
copy_type(type_T *type, garray_T *type_gap)
{
type_T *copy = get_type_ptr(type_gap);
if (copy == NULL)
return type;
*copy = *type;
copy->tt_flags &= ~TTFLAG_STATIC;
if (type->tt_args != NULL
&& func_type_add_arg_types(copy, type->tt_argcount, type_gap) == OK)
for (int i = 0; i < type->tt_argcount; ++i)
copy->tt_args[i] = type->tt_args[i];
return copy;
}
/*
* Inner part of copy_type_deep().
* When allocation fails returns "type".
*/
static type_T *
copy_type_deep_rec(type_T *type, garray_T *type_gap, garray_T *seen_types)
{
for (int i = 0; i < seen_types->ga_len; ++i)
if (((type_T **)seen_types->ga_data)[i * 2] == type)
// seen this type before, return the copy we made
return ((type_T **)seen_types->ga_data)[i * 2 + 1];
type_T *copy = copy_type(type, type_gap);
if (ga_grow(seen_types, 1) == FAIL)
return copy;
((type_T **)seen_types->ga_data)[seen_types->ga_len * 2] = type;
((type_T **)seen_types->ga_data)[seen_types->ga_len * 2 + 1] = copy;
++seen_types->ga_len;
if (copy->tt_member != NULL)
copy->tt_member = copy_type_deep_rec(copy->tt_member,
type_gap, seen_types);
if (type->tt_args != NULL)
for (int i = 0; i < type->tt_argcount; ++i)
copy->tt_args[i] = copy_type_deep_rec(copy->tt_args[i],
type_gap, seen_types);
return copy;
}
/*
* Make a deep copy of "type".
* When allocation fails returns "type".
*/
static type_T *
copy_type_deep(type_T *type, garray_T *type_gap)
{
garray_T seen_types;
// stores type pairs : a type we have seen and the copy used
ga_init2(&seen_types, sizeof(type_T *) * 2, 20);
type_T *res = copy_type_deep_rec(type, type_gap, &seen_types);
ga_clear(&seen_types);
return res;
}
void
clear_type_list(garray_T *gap)
{
while (gap->ga_len > 0)
vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
ga_clear(gap);
}
/*
* Take a type that is using entries in a growarray and turn it into a type
* with allocated entries.
*/
type_T *
alloc_type(type_T *type)
{
type_T *ret;
if (type == NULL)
return NULL;
// A fixed type never contains allocated types, return as-is.
if (type->tt_flags & TTFLAG_STATIC)
return type;
ret = ALLOC_ONE(type_T);
*ret = *type;
if (ret->tt_member != NULL)
ret->tt_member = alloc_type(ret->tt_member);
if (type->tt_args != NULL)
{
int i;
ret->tt_args = ALLOC_MULT(type_T *, type->tt_argcount);
if (ret->tt_args != NULL)
for (i = 0; i < type->tt_argcount; ++i)
ret->tt_args[i] = alloc_type(type->tt_args[i]);
}
return ret;
}
/*
* Free a type that was created with alloc_type().
*/
void
free_type(type_T *type)
{
int i;
if (type == NULL || (type->tt_flags & TTFLAG_STATIC))
return;
if (type->tt_args != NULL)
{
for (i = 0; i < type->tt_argcount; ++i)
free_type(type->tt_args[i]);
vim_free(type->tt_args);
}
free_type(type->tt_member);
vim_free(type);
}
/*
* Return TRUE if "type" is to be recursed into for setting the type.
*/
static int
set_tv_type_recurse(type_T *type)
{
return type->tt_member != NULL
&& (type->tt_member->tt_type == VAR_DICT
|| type->tt_member->tt_type == VAR_LIST)
&& type->tt_member->tt_member != NULL
&& type->tt_member->tt_member != &t_any
&& type->tt_member->tt_member != &t_unknown;
}
/*
* Set the type of "tv" to "type" if it is a list or dict.
*/
void
set_tv_type(typval_T *tv, type_T *type)
{
if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
{
dict_T *d = tv->vval.v_dict;
if (d->dv_type != type)
{
free_type(d->dv_type);
d->dv_type = alloc_type(type);
if (set_tv_type_recurse(type))
{
int todo = (int)d->dv_hashtab.ht_used;
hashitem_T *hi;
dictitem_T *di;
FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
{
if (!HASHITEM_EMPTY(hi))
{
--todo;
di = HI2DI(hi);
set_tv_type(&di->di_tv, type->tt_member);
}
}
}
}
}
else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
{
list_T *l = tv->vval.v_list;
if (l->lv_type != type)
{
free_type(l->lv_type);
l->lv_type = alloc_type(type);
if (l->lv_first != &range_list_item && set_tv_type_recurse(type))
{
listitem_T *li;
FOR_ALL_LIST_ITEMS(l, li)
set_tv_type(&li->li_tv, type->tt_member);
}
}
}
}
type_T *
get_list_type(type_T *member_type, garray_T *type_gap)
{
type_T *type;
// recognize commonly used types
if (member_type == NULL || member_type->tt_type == VAR_ANY)
return &t_list_any;
if (member_type->tt_type == VAR_VOID
|| member_type->tt_type == VAR_UNKNOWN)
return &t_list_empty;
if (member_type->tt_type == VAR_BOOL)
return &t_list_bool;
if (member_type->tt_type == VAR_NUMBER)
return &t_list_number;
if (member_type->tt_type == VAR_STRING)
return &t_list_string;
// Not a common type, create a new entry.
type = get_type_ptr(type_gap);
if (type == NULL)
return &t_any;
type->tt_type = VAR_LIST;
type->tt_member = member_type;
type->tt_argcount = 0;
type->tt_args = NULL;
return type;
}
type_T *
get_dict_type(type_T *member_type, garray_T *type_gap)
{
type_T *type;
// recognize commonly used types
if (member_type == NULL || member_type->tt_type == VAR_ANY)
return &t_dict_any;
if (member_type->tt_type == VAR_VOID
|| member_type->tt_type == VAR_UNKNOWN)
return &t_dict_empty;
if (member_type->tt_type == VAR_BOOL)
return &t_dict_bool;
if (member_type->tt_type == VAR_NUMBER)
return &t_dict_number;
if (member_type->tt_type == VAR_STRING)
return &t_dict_string;
// Not a common type, create a new entry.
type = get_type_ptr(type_gap);
if (type == NULL)
return &t_any;
type->tt_type = VAR_DICT;
type->tt_member = member_type;
type->tt_argcount = 0;
type->tt_args = NULL;
return type;
}
/*
* Allocate a new type for a function.
*/
type_T *
alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
{
type_T *type = get_type_ptr(type_gap);
if (type == NULL)
return &t_any;
type->tt_type = VAR_FUNC;
type->tt_member = ret_type == NULL ? &t_unknown : ret_type;
type->tt_argcount = argcount;
type->tt_args = NULL;
return type;
}
/*
* Get a function type, based on the return type "ret_type".
* "argcount" must be -1 or 0, a predefined type can be used.
*/
type_T *
get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
{
// recognize commonly used types
if (ret_type == &t_unknown || ret_type == NULL)
{
// (argcount == 0) is not possible
return &t_func_unknown;
}
if (ret_type == &t_void)
{
if (argcount == 0)
return &t_func_0_void;
else
return &t_func_void;
}
if (ret_type == &t_any)
{
if (argcount == 0)
return &t_func_0_any;
else
return &t_func_any;
}
if (ret_type == &t_number)
{
if (argcount == 0)
return &t_func_0_number;
else
return &t_func_number;
}
if (ret_type == &t_string)
{
if (argcount == 0)
return &t_func_0_string;
else
return &t_func_string;
}
return alloc_func_type(ret_type, argcount, type_gap);
}
/*
* For a function type, reserve space for "argcount" argument types (including
* vararg).
*/
int
func_type_add_arg_types(
type_T *functype,
int argcount,
garray_T *type_gap)
{
// To make it easy to free the space needed for the argument types, add the
// pointer to type_gap.
if (ga_grow(type_gap, 1) == FAIL)
return FAIL;
functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
if (functype->tt_args == NULL)
return FAIL;
((type_T **)type_gap->ga_data)[type_gap->ga_len] =
(void *)functype->tt_args;
++type_gap->ga_len;
return OK;
}
/*
* Return TRUE if "type" is NULL, any or unknown.
* This also works for const (comparing with &t_any and &t_unknown doesn't).
*/
int
type_any_or_unknown(type_T *type)
{
return type == NULL || type->tt_type == VAR_ANY
|| type->tt_type == VAR_UNKNOWN;
}
/*
* Get a type_T for a typval_T.
* "type_gap" is used to temporarily create types in.
* When "flags" has TVTT_DO_MEMBER also get the member type, otherwise use
* "any".
* When "flags" has TVTT_MORE_SPECIFIC get the more specific member type if it
* is "any".
*/
static type_T *
typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int flags)
{
type_T *type;
type_T *member_type = NULL;
class_T *class_type = NULL;
int argcount = 0;
int min_argcount = 0;
if (tv->v_type == VAR_NUMBER)
return &t_number;
if (tv->v_type == VAR_BOOL)
return &t_bool;
if (tv->v_type == VAR_SPECIAL)
{
if (tv->vval.v_number == VVAL_NULL)
return &t_null;
if (tv->vval.v_number == VVAL_NONE)
return &t_none;
if (tv->vval.v_number == VVAL_TRUE
|| tv->vval.v_number == VVAL_TRUE)
return &t_bool;
return &t_unknown;
}
if (tv->v_type == VAR_STRING)
return &t_string;
if (tv->v_type == VAR_BLOB)
{
if (tv->vval.v_blob == NULL)
return &t_blob_null;
return &t_blob;
}
if (tv->v_type == VAR_LIST)
{
list_T *l = tv->vval.v_list;
listitem_T *li;
// An empty list has type list<unknown>, unless the type was specified
// and is not list<any>. This matters when assigning to a variable
// with a specific list type.
if (l == NULL || (l->lv_first == NULL
&& (l->lv_type == NULL || l->lv_type->tt_member == &t_any)))
return &t_list_empty;
if ((flags & TVTT_DO_MEMBER) == 0)
return &t_list_any;
// If the type is list<any> go through the members, it may end up a
// more specific type.
if (l->lv_type != NULL && (l->lv_first == NULL
|| (flags & TVTT_MORE_SPECIFIC) == 0
|| l->lv_type->tt_member != &t_any))
// make a copy, lv_type may be freed if the list is freed
return copy_type_deep(l->lv_type, type_gap);
if (l->lv_first == &range_list_item)
return &t_list_number;
if (l->lv_copyID == copyID)
// avoid recursion
return &t_list_any;
l->lv_copyID = copyID;
// Use the common type of all members.
member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap,
TVTT_DO_MEMBER);
for (li = l->lv_first->li_next; li != NULL; li = li->li_next)
common_type(typval2type(&li->li_tv, copyID, type_gap,
TVTT_DO_MEMBER),
member_type, &member_type, type_gap);
return get_list_type(member_type, type_gap);
}
if (tv->v_type == VAR_DICT)
{
dict_iterator_T iter;
typval_T *value;
dict_T *d = tv->vval.v_dict;
if (d == NULL || (d->dv_hashtab.ht_used == 0 && d->dv_type == NULL))
return &t_dict_empty;
if ((flags & TVTT_DO_MEMBER) == 0)
return &t_dict_any;
// If the type is dict<any> go through the members, it may end up a
// more specific type.
if (d->dv_type != NULL && (d->dv_hashtab.ht_used == 0
|| (flags & TVTT_MORE_SPECIFIC) == 0
|| d->dv_type->tt_member != &t_any))
return d->dv_type;
if (d->dv_copyID == copyID)
// avoid recursion
return &t_dict_any;
d->dv_copyID = copyID;
// Use the common type of all values.
dict_iterate_start(tv, &iter);
dict_iterate_next(&iter, &value);
member_type = typval2type(value, copyID, type_gap, TVTT_DO_MEMBER);
while (dict_iterate_next(&iter, &value) != NULL)
common_type(typval2type(value, copyID, type_gap, TVTT_DO_MEMBER),
member_type, &member_type, type_gap);
return get_dict_type(member_type, type_gap);
}
if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
{
char_u *name = NULL;
ufunc_T *ufunc = NULL;
if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
{
if (tv->vval.v_partial->pt_func != NULL)
ufunc = tv->vval.v_partial->pt_func;
else
name = tv->vval.v_partial->pt_name;
}
else
name = tv->vval.v_string;
if (name == NULL && ufunc == NULL)
return &t_func_unknown;
if (name != NULL)
{
int idx = find_internal_func(name);
if (idx >= 0)
{
type_T *decl_type; // unused
internal_func_get_argcount(idx, &argcount, &min_argcount);
member_type = internal_func_ret_type(idx, 0, NULL, &decl_type,
type_gap);
}
else
ufunc = find_func(name, FALSE);
}
if (ufunc != NULL)
{
// May need to get the argument types from default values by
// compiling the function.
if (ufunc->uf_def_status == UF_TO_BE_COMPILED
&& compile_def_function(ufunc, TRUE, CT_NONE, NULL)
== FAIL)
return NULL;
if (ufunc->uf_func_type == NULL)
set_function_type(ufunc);
if (ufunc->uf_func_type != NULL)
{
if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
&& tv->vval.v_partial->pt_argc > 0)
{
type = get_type_ptr(type_gap);
if (type == NULL)
return NULL;
*type = *ufunc->uf_func_type;
if (type->tt_argcount >= 0)
{
type->tt_argcount -= tv->vval.v_partial->pt_argc;
type->tt_min_argcount -= tv->vval.v_partial->pt_argc;
if (type->tt_argcount > 0
&& func_type_add_arg_types(type,
type->tt_argcount, type_gap) == OK)
for (int i = 0; i < type->tt_argcount; ++i)
type->tt_args[i] =
ufunc->uf_func_type->tt_args[
i + tv->vval.v_partial->pt_argc];
}
return type;
}
return ufunc->uf_func_type;
}
}
}
if (tv->v_type == VAR_CLASS)
class_type = tv->vval.v_class;
else if (tv->v_type == VAR_OBJECT && tv->vval.v_object != NULL)
class_type = tv->vval.v_object->obj_class;
type = get_type_ptr(type_gap);
if (type == NULL)
return NULL;
type->tt_type = tv->v_type;
type->tt_argcount = argcount;
type->tt_min_argcount = min_argcount;
if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
&& tv->vval.v_partial->pt_argc > 0)
{
type->tt_argcount -= tv->vval.v_partial->pt_argc;
type->tt_min_argcount -= tv->vval.v_partial->pt_argc;
}
type->tt_member = member_type;
type->tt_class = class_type;
return type;
}
/*
* Return TRUE if "tv" is not a bool but should be converted to bool.
*/
int
need_convert_to_bool(type_T *type, typval_T *tv)
{
return type != NULL && type == &t_bool && tv->v_type != VAR_BOOL
&& (tv->v_type == VAR_NUMBER
&& (tv->vval.v_number == 0 || tv->vval.v_number == 1));
}
/*
* Get a type_T for a typval_T.
* "type_list" is used to temporarily create types in.
* When "flags" has TVTT_DO_MEMBER also get the member type, otherwise use
* "any".
* When "flags" has TVTT_MORE_SPECIFIC get the most specific member type.
*/
type_T *
typval2type(typval_T *tv, int copyID, garray_T *type_gap, int flags)
{
type_T *type = typval2type_int(tv, copyID, type_gap, flags);
if (type == NULL)
return NULL;
if (type != &t_bool && (tv->v_type == VAR_NUMBER
&& (tv->vval.v_number == 0 || tv->vval.v_number == 1)))
// Number 0 and 1 and expression with "&&" or "||" can also be used
// for bool.
type = &t_number_bool;
else if (type != &t_float && tv->v_type == VAR_NUMBER)
// A number can also be used for float.
type = &t_number_float;
return type;
}
/*
* Return TRUE if "type" can be used for a variable declaration.
* Give an error and return FALSE if not.
*/
int
valid_declaration_type(type_T *type)
{
if (type->tt_type == VAR_SPECIAL // null, none
|| type->tt_type == VAR_VOID)
{
char *tofree = NULL;
char *name = type_name(type, &tofree);
semsg(_(e_invalid_type_for_object_member_str), name);
vim_free(tofree);
return FALSE;
}
return TRUE;
}
/*
* Get a type_T for a typval_T, used for v: variables.
* "type_list" is used to temporarily create types in.
*/
type_T *
typval2type_vimvar(typval_T *tv, garray_T *type_gap)
{
if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
return &t_list_string;
if (tv->v_type == VAR_DICT) // e.g. for v:event
return &t_dict_any;
return typval2type(tv, get_copyID(), type_gap, TVTT_DO_MEMBER);
}
int
check_typval_arg_type(
type_T *expected,
typval_T *actual_tv,
char *func_name,
int arg_idx)
{
where_T where = WHERE_INIT;
where.wt_index = arg_idx;
where.wt_func_name = func_name;
return check_typval_type(expected, actual_tv, where);
}
/*
* Return FAIL if "expected" and "actual" don't match.
* When "argidx" > 0 it is included in the error message.
*/
int
check_typval_type(type_T *expected, typval_T *actual_tv, where_T where)
{
garray_T type_list;
type_T *actual_type;
int res = FAIL;
if (expected == NULL)
return OK; // didn't expect anything.
//
ga_init2(&type_list, sizeof(type_T *), 10);
// A null_function and null_partial are special cases, they can be used to
// clear a variable.
if ((actual_tv->v_type == VAR_FUNC && actual_tv->vval.v_string == NULL)
|| (actual_tv->v_type == VAR_PARTIAL
&& actual_tv->vval.v_partial == NULL))
actual_type = &t_func_unknown;
else
// When the actual type is list<any> or dict<any> go through the values
// to possibly get a more specific type.
actual_type = typval2type(actual_tv, get_copyID(), &type_list,
TVTT_DO_MEMBER | TVTT_MORE_SPECIFIC);
if (actual_type != NULL)
{
res = check_type_maybe(expected, actual_type, TRUE, where);
if (res == MAYBE && !(actual_type->tt_type == VAR_FUNC
&& actual_type->tt_member == &t_unknown))
{
// If a type check is needed that means assigning "any" or
// "unknown" to a more specific type, which fails here.
// Execpt when it looks like a lambda, since they have an
// incomplete type.
type_mismatch_where(expected, actual_type, where);
res = FAIL;
}
}
clear_type_list(&type_list);
return res;
}
void
arg_type_mismatch(type_T *expected, type_T *actual, int arg_idx)
{
where_T where = WHERE_INIT;
where.wt_index = arg_idx;
type_mismatch_where(expected, actual, where);
}
void
type_mismatch_where(type_T *expected, type_T *actual, where_T where)
{
char *tofree1, *tofree2;
char *typename1 = type_name(expected, &tofree1);
char *typename2 = type_name(actual, &tofree2);
if (where.wt_index > 0)
{
if (where.wt_func_name == NULL)
semsg(_(where.wt_variable
? e_variable_nr_type_mismatch_expected_str_but_got_str
: e_argument_nr_type_mismatch_expected_str_but_got_str),
where.wt_index, typename1, typename2);
else
semsg(_(where.wt_variable
? e_variable_nr_type_mismatch_expected_str_but_got_str_in_str
: e_argument_nr_type_mismatch_expected_str_but_got_str_in_str),
where.wt_index, typename1, typename2, where.wt_func_name);
}
else if (where.wt_func_name == NULL)
semsg(_(e_type_mismatch_expected_str_but_got_str),
typename1, typename2);
else
semsg(_(e_type_mismatch_expected_str_but_got_str_in_str),
typename1, typename2, where.wt_func_name);
vim_free(tofree1);
vim_free(tofree2);
}
/*
* Check if the expected and actual types match.
* Does not allow for assigning "any" to a specific type.
* When "argidx" > 0 it is included in the error message.
* Return OK if types match.
* Return FAIL if types do not match.
*/
int
check_type(
type_T *expected,
type_T *actual,
int give_msg,
where_T where)
{
int ret = check_type_maybe(expected, actual, give_msg, where);
return ret == MAYBE ? OK : ret;
}
/*
* As check_type() but return MAYBE when a runtime type check should be used
* when compiling.
*/
int
check_type_maybe(
type_T *expected,
type_T *actual,
int give_msg,
where_T where)
{
int ret = OK;
// When expected is "unknown" we accept any actual type.
// When expected is "any" we accept any actual type except "void".
if (expected->tt_type != VAR_UNKNOWN
&& !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
{
// tt_type should match, except that a "partial" can be assigned to a
// variable with type "func".
// And "unknown" (using global variable) and "any" need a runtime type
// check.
if (!(expected->tt_type == actual->tt_type
|| actual->tt_type == VAR_UNKNOWN
|| actual->tt_type == VAR_ANY
|| (expected->tt_type == VAR_FUNC
&& actual->tt_type == VAR_PARTIAL)))
{
if (expected->tt_type == VAR_BOOL
&& (actual->tt_flags & TTFLAG_BOOL_OK))
// Using number 0 or 1 for bool is OK.
return OK;
if (expected->tt_type == VAR_FLOAT
&& actual->tt_type == VAR_NUMBER
&& ((expected->tt_flags & TTFLAG_NUMBER_OK)
|| (actual->tt_flags & TTFLAG_FLOAT_OK)))
// Using a number where a float is expected is OK here.
return OK;
if (give_msg)
type_mismatch_where(expected, actual, where);
return FAIL;
}
if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
{
// "unknown" is used for an empty list or dict
if (actual->tt_member != NULL && actual->tt_member != &t_unknown)
ret = check_type_maybe(expected->tt_member, actual->tt_member,
FALSE, where);
}
else if (expected->tt_type == VAR_FUNC && actual != &t_any)
{
// If the return type is unknown it can be anything, including
// nothing, thus there is no point in checking.
if (expected->tt_member != &t_unknown)
{
if (actual->tt_member != NULL
&& actual->tt_member != &t_unknown)
ret = check_type_maybe(expected->tt_member,
actual->tt_member, FALSE, where);
else
ret = MAYBE;
}
if (ret != FAIL && expected->tt_argcount != -1
&& actual->tt_min_argcount != -1
&& (actual->tt_argcount == -1
|| (actual->tt_argcount < expected->tt_min_argcount
|| actual->tt_argcount > expected->tt_argcount)))
ret = FAIL;
if (ret != FAIL && expected->tt_args != NULL
&& actual->tt_args != NULL)
{
int i;
for (i = 0; i < expected->tt_argcount
&& i < actual->tt_argcount; ++i)
// Allow for using "any" argument type, lambda's have them.
if (actual->tt_args[i] != &t_any && check_type(
expected->tt_args[i], actual->tt_args[i], FALSE,
where) == FAIL)
{
ret = FAIL;
break;
}
}
if (ret == OK && expected->tt_argcount >= 0
&& actual->tt_argcount == -1)
// check the argument count at runtime
ret = MAYBE;
}
else if (expected->tt_type == VAR_OBJECT)
{
if (actual->tt_type == VAR_ANY)
return MAYBE; // use runtime type check
if (actual->tt_type != VAR_OBJECT)
return FAIL; // don't use tt_class
// check the class, base class or an implemented interface matches
class_T *cl;
for (cl = actual->tt_class; cl != NULL; cl = cl->class_extends)
{
if (expected->tt_class == cl)
break;
int i;
for (i = cl->class_interface_count - 1; i >= 0; --i)
if (expected->tt_class == cl->class_interfaces_cl[i])
break;
if (i >= 0)
break;
}
if (cl == NULL)
ret = FAIL;
}
if (ret == FAIL && give_msg)
type_mismatch_where(expected, actual, where);
}
if (ret == OK && expected->tt_type != VAR_UNKNOWN
&& expected->tt_type != VAR_ANY
&& (actual->tt_type == VAR_UNKNOWN || actual->tt_type == VAR_ANY))
// check the type at runtime
ret = MAYBE;
return ret;
}
/*
* Check that the arguments of "type" match "argvars[argcount]".
* "base_tv" is from "expr->Func()".
* Return OK/FAIL.
*/
int
check_argument_types(
type_T *type,
typval_T *argvars,
int argcount,
typval_T *base_tv,
char_u *name)
{
int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
int i;
int totcount = argcount + (base_tv == NULL ? 0 : 1);
if (type->tt_type != VAR_FUNC && type->tt_type != VAR_PARTIAL)
return OK; // just in case
if (totcount < type->tt_min_argcount - varargs)
{
emsg_funcname(e_not_enough_arguments_for_function_str, name);
return FAIL;
}
if (!varargs && type->tt_argcount >= 0 && totcount > type->tt_argcount)
{
emsg_funcname(e_too_many_arguments_for_function_str, name);
return FAIL;
}
if (type->tt_args == NULL)
return OK; // cannot check
for (i = 0; i < totcount; ++i)
{
type_T *expected;
typval_T *tv;
if (base_tv != NULL)
{
if (i == 0)
tv = base_tv;
else
tv = &argvars[i - 1];
}
else
tv = &argvars[i];
if (varargs && i >= type->tt_argcount - 1)
{
expected = type->tt_args[type->tt_argcount - 1];
if (expected != NULL && expected->tt_type == VAR_LIST)
expected = expected->tt_member;
if (expected == NULL)
expected = &t_any;
}
else
expected = type->tt_args[i];
if (check_typval_arg_type(expected, tv, NULL, i + 1) == FAIL)
return FAIL;
}
return OK;
}
/*
* Skip over a type definition and return a pointer to just after it.
* When "optional" is TRUE then a leading "?" is accepted.
*/
char_u *
skip_type(char_u *start, int optional)
{
char_u *p = start;
if (optional && *p == '?')
++p;
// Also skip over "." for imported classes: "import.ClassName".
while (ASCII_ISALNUM(*p) || *p == '_' || *p == '.')
++p;
// Skip over "<type>"; this is permissive about white space.
if (*skipwhite(p) == '<')
{
p = skipwhite(p);
p = skip_type(skipwhite(p + 1), FALSE);
p = skipwhite(p);