-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvim9expr.c
3530 lines (3180 loc) · 87.3 KB
/
vim9expr.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.
*/
/*
* vim9expr.c: Dealing with compiled function expressions
*/
#define USING_FLOAT_STUFF
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
// When not generating protos this is included in proto.h
#ifdef PROTO
# include "vim9.h"
#endif
// flag passed from compile_subscript() to compile_load_scriptvar()
static int paren_follows_after_expr = 0;
/*
* Generate code for any ppconst entries.
*/
int
generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
{
int i;
int ret = OK;
int save_skip = cctx->ctx_skip;
cctx->ctx_skip = SKIP_NOT;
for (i = 0; i < ppconst->pp_used; ++i)
if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
ret = FAIL;
ppconst->pp_used = 0;
cctx->ctx_skip = save_skip;
return ret;
}
/*
* Check that the last item of "ppconst" is a bool, if there is an item.
*/
static int
check_ppconst_bool(ppconst_T *ppconst)
{
if (ppconst->pp_used > 0)
{
typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
where_T where = WHERE_INIT;
return check_typval_type(&t_bool, tv, where);
}
return OK;
}
/*
* Clear ppconst constants. Used when failing.
*/
void
clear_ppconst(ppconst_T *ppconst)
{
int i;
for (i = 0; i < ppconst->pp_used; ++i)
clear_tv(&ppconst->pp_tv[i]);
ppconst->pp_used = 0;
}
/*
* Compile getting a member from a list/dict/string/blob. Stack has the
* indexable value and the index or the two indexes of a slice.
* "keeping_dict" is used for dict[func](arg) to pass dict to func.
*/
int
compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
{
type2_T *typep;
garray_T *stack = &cctx->ctx_type_stack;
vartype_T vartype;
type_T *idxtype;
// We can index a list, dict and blob. If we don't know the type
// we can use the index value type. If we still don't know use an "ANY"
// instruction.
// TODO: what about the decl type?
typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2));
vartype = typep->type_curr->tt_type;
idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
// If the index is a string, the variable must be a Dict.
if ((typep->type_curr->tt_type == VAR_ANY
|| typep->type_curr->tt_type == VAR_UNKNOWN)
&& idxtype == &t_string)
vartype = VAR_DICT;
if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
{
if (need_type(idxtype, &t_number, FALSE,
-1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
if (is_slice)
{
idxtype = get_type_on_stack(cctx, 1);
if (need_type(idxtype, &t_number, FALSE,
-2, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
}
}
if (vartype == VAR_DICT)
{
if (is_slice)
{
emsg(_(e_cannot_slice_dictionary));
return FAIL;
}
if (typep->type_curr->tt_type == VAR_DICT)
{
typep->type_curr = typep->type_curr->tt_member;
if (typep->type_curr == &t_unknown)
// empty dict was used
typep->type_curr = &t_any;
if (typep->type_decl->tt_type == VAR_DICT)
{
typep->type_decl = typep->type_decl->tt_member;
if (typep->type_decl == &t_unknown)
// empty dict was used
typep->type_decl = &t_any;
}
else
typep->type_decl = typep->type_curr;
}
else
{
if (need_type(typep->type_curr, &t_dict_any, FALSE,
-2, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
typep->type_curr = &t_any;
typep->type_decl = &t_any;
}
if (may_generate_2STRING(-1, FALSE, cctx) == FAIL
|| generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
return FAIL;
if (keeping_dict != NULL)
*keeping_dict = TRUE;
}
else if (vartype == VAR_STRING)
{
typep->type_curr = &t_string;
typep->type_decl = &t_string;
if ((is_slice
? generate_instr_drop(cctx, ISN_STRSLICE, 2)
: generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
return FAIL;
}
else if (vartype == VAR_BLOB)
{
if (is_slice)
{
typep->type_curr = &t_blob;
typep->type_decl = &t_blob;
if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
return FAIL;
}
else
{
typep->type_curr = &t_number;
typep->type_decl = &t_number;
if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
return FAIL;
}
}
else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY
|| typep->type_curr->tt_type == VAR_UNKNOWN)
{
if (is_slice)
{
if (generate_instr_drop(cctx,
vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2) == FAIL)
return FAIL;
// a copy is made so the member type is no longer declared
if (typep->type_decl->tt_type == VAR_LIST)
typep->type_decl = &t_list_any;
// a copy is made, the composite is no longer "const"
if (typep->type_curr->tt_flags & TTFLAG_CONST)
{
type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
if (type != typep->type_curr) // did get a copy
{
type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
typep->type_curr = type;
}
}
}
else
{
if (typep->type_curr->tt_type == VAR_LIST)
{
typep->type_curr = typep->type_curr->tt_member;
if (typep->type_curr == &t_unknown)
// empty list was used
typep->type_curr = &t_any;
if (typep->type_decl->tt_type == VAR_LIST)
{
typep->type_decl = typep->type_decl->tt_member;
if (typep->type_decl == &t_unknown)
// empty list was used
typep->type_decl = &t_any;
}
else
typep->type_decl = typep->type_curr;
}
if (generate_instr_drop(cctx,
vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
== FAIL)
return FAIL;
}
}
else
{
switch (vartype)
{
case VAR_FUNC:
case VAR_PARTIAL:
emsg(_(e_cannot_index_a_funcref));
break;
case VAR_BOOL:
case VAR_SPECIAL:
case VAR_JOB:
case VAR_CHANNEL:
case VAR_INSTR:
case VAR_CLASS:
case VAR_OBJECT:
case VAR_UNKNOWN:
case VAR_ANY:
case VAR_VOID:
emsg(_(e_cannot_index_special_variable));
break;
default:
emsg(_(e_string_list_dict_or_blob_required));
}
return FAIL;
}
return OK;
}
/*
* Compile ".member" coming after an object or class.
*/
static int
compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
{
if (VIM_ISWHITE((*arg)[1]))
{
semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
return FAIL;
}
class_T *cl = type->tt_class;
int is_super = type->tt_flags & TTFLAG_SUPER;
if (type == &t_super)
{
if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
{
emsg(_(e_using_super_not_in_class_function));
return FAIL;
}
is_super = TRUE;
cl = cctx->ctx_ufunc->uf_class;
// Remove &t_super from the stack.
--cctx->ctx_type_stack.ga_len;
}
else if (type->tt_type == VAR_CLASS)
{
garray_T *instr = &cctx->ctx_instr;
if (instr->ga_len > 0)
{
isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
if (isn->isn_type == ISN_LOADSCRIPT)
{
// The class was recognized as a script item. We only need
// to know what class it is, drop the instruction.
--instr->ga_len;
vim_free(isn->isn_arg.script.scriptref);
}
}
}
if (cl == NULL)
{
// TODO: this should not give an error but be handled at runtime
emsg(_(e_incomplete_type));
return FAIL;
}
++*arg;
char_u *name = *arg;
char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
if (name_end == name)
return FAIL;
size_t len = name_end - name;
if (*name_end == '(')
{
int function_count;
int child_count;
ufunc_T **functions;
if (type->tt_type == VAR_CLASS)
{
function_count = cl->class_class_function_count;
child_count = cl->class_class_function_count_child;
functions = cl->class_class_functions;
}
else
{
// type->tt_type == VAR_OBJECT: method call
function_count = cl->class_obj_method_count;
child_count = cl->class_obj_method_count_child;
functions = cl->class_obj_methods;
}
ufunc_T *ufunc = NULL;
int fi;
for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
{
ufunc_T *fp = functions[fi];
// Use a separate pointer to avoid that ASAN complains about
// uf_name[] only being 4 characters.
char_u *ufname = (char_u *)fp->uf_name;
if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
{
ufunc = fp;
break;
}
}
if (ufunc == NULL)
{
// TODO: different error for object method?
semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
return FAIL;
}
// Compile the arguments and call the class function or object method.
// The object method will know that the object is on the stack, just
// before the arguments.
*arg = skipwhite(name_end + 1);
int argcount = 0;
if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
return FAIL;
if (type->tt_type == VAR_OBJECT
&& (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
return generate_CALL(cctx, ufunc, cl, fi, argcount);
return generate_CALL(cctx, ufunc, NULL, 0, argcount);
}
if (type->tt_type == VAR_OBJECT)
{
for (int i = 0; i < cl->class_obj_member_count; ++i)
{
ocmember_T *m = &cl->class_obj_members[i];
if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
{
if (*name == '_' && !inside_class(cctx, cl))
{
semsg(_(e_cannot_access_private_member_str), m->ocm_name);
return FAIL;
}
*arg = name_end;
if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
return generate_GET_ITF_MEMBER(cctx, cl, i, m->ocm_type);
return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
}
}
// Could be a function reference: "obj.Func".
for (int i = 0; i < cl->class_obj_method_count; ++i)
{
ufunc_T *fp = cl->class_obj_methods[i];
// Use a separate pointer to avoid that ASAN complains about
// uf_name[] only being 4 characters.
char_u *ufname = (char_u *)fp->uf_name;
if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
{
if (type->tt_type == VAR_OBJECT
&& (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
return generate_FUNCREF(cctx, fp, cl, i, NULL);
return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
}
}
semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
}
else
{
// load class member
int idx;
for (idx = 0; idx < cl->class_class_member_count; ++idx)
{
ocmember_T *m = &cl->class_class_members[idx];
if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
break;
}
if (idx < cl->class_class_member_count)
{
*arg = name_end;
return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
}
semsg(_(e_class_member_not_found_str), name);
}
return FAIL;
}
/*
* Generate an instruction to load script-local variable "name", without the
* leading "s:".
* Also finds imported variables.
*/
int
compile_load_scriptvar(
cctx_T *cctx,
char_u *name, // variable NUL terminated
char_u *start, // start of variable
char_u **end) // end of variable, may be NULL
{
scriptitem_T *si;
int idx;
imported_T *import;
if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
return FAIL;
si = SCRIPT_ITEM(current_sctx.sc_sid);
idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
if (idx >= 0)
{
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
current_sctx.sc_sid, idx, sv->sv_type);
return OK;
}
import = end == NULL ? NULL : find_imported(name, 0, FALSE);
if (import != NULL)
{
char_u *p = skipwhite(*end);
char_u *exp_name;
int cc;
ufunc_T *ufunc = NULL;
type_T *type;
int done = FALSE;
int res = OK;
// Need to lookup the member.
if (*p != '.')
{
semsg(_(e_expected_dot_after_name_str), start);
return FAIL;
}
++p;
if (VIM_ISWHITE(*p))
{
emsg(_(e_no_white_space_allowed_after_dot));
return FAIL;
}
// isolate one name
exp_name = p;
while (eval_isnamec(*p))
++p;
cc = *p;
*p = NUL;
si = SCRIPT_ITEM(import->imp_sid);
if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
// "import autoload './dir/script.vim'" or
// "import autoload './autoload/script.vim'" - load script first
res = generate_SOURCE(cctx, import->imp_sid);
if (res == OK)
{
if (si->sn_autoload_prefix != NULL
&& si->sn_state == SN_STATE_NOT_LOADED)
{
char_u *auto_name =
concat_str(si->sn_autoload_prefix, exp_name);
// autoload script must be loaded later, access by the autoload
// name. If a '(' follows it must be a function. Otherwise we
// don't know, it can be "script.Func".
if (cc == '(' || paren_follows_after_expr)
res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
else
res = generate_AUTOLOAD(cctx, auto_name, &t_any);
vim_free(auto_name);
done = TRUE;
}
else if (si->sn_import_autoload
&& si->sn_state == SN_STATE_NOT_LOADED)
{
// If a '(' follows it must be a function. Otherwise we don't
// know, it can be "script.Func".
if (cc == '(' || paren_follows_after_expr)
{
char_u sid_name[MAX_FUNC_NAME_LEN];
func_name_with_sid(exp_name, import->imp_sid, sid_name);
res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
}
else
res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
import->imp_sid, &t_any);
done = TRUE;
}
else
{
idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
cctx, NULL, TRUE);
}
}
*p = cc;
*end = p;
if (done)
return res;
if (idx < 0)
{
if (ufunc != NULL)
{
// function call or function reference
generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
return OK;
}
return FAIL;
}
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
import->imp_sid,
idx,
type);
return OK;
}
// Can only get here if we know "name" is a script variable and not in a
// Vim9 script (variable is not in sn_var_vals): old style script.
return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
&t_any);
}
static int
generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
{
ufunc_T *ufunc = find_func(name, FALSE);
compiletype_T compile_type;
// Reject a global non-autoload function found without the "g:" prefix.
if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
return FAIL;
// Need to compile any default values to get the argument types.
compile_type = get_compile_type(ufunc);
if (func_needs_compiling(ufunc, compile_type)
&& compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
return FAIL;
return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
}
/*
* Compile a variable name into a load instruction.
* "end" points to just after the name.
* "is_expr" is TRUE when evaluating an expression, might be a funcref.
* When "error" is FALSE do not give an error when not found.
*/
int
compile_load(
char_u **arg,
char_u *end_arg,
cctx_T *cctx,
int is_expr,
int error)
{
type_T *type;
char_u *name = NULL;
char_u *end = end_arg;
int res = FAIL;
int prev_called_emsg = called_emsg;
if (*(*arg + 1) == ':')
{
if (end <= *arg + 2)
{
isntype_T isn_type;
// load dictionary of namespace
switch (**arg)
{
case 'g': isn_type = ISN_LOADGDICT; break;
case 'w': isn_type = ISN_LOADWDICT; break;
case 't': isn_type = ISN_LOADTDICT; break;
case 'b': isn_type = ISN_LOADBDICT; break;
default:
semsg(_(e_namespace_not_supported_str), *arg);
goto theend;
}
if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
goto theend;
res = OK;
}
else
{
isntype_T isn_type = ISN_DROP;
// load namespaced variable
name = vim_strnsave(*arg + 2, end - (*arg + 2));
if (name == NULL)
return FAIL;
switch (**arg)
{
case 'v': res = generate_LOADV(cctx, name);
break;
case 's': if (current_script_is_vim9())
{
semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
*arg);
vim_free(name);
return FAIL;
}
if (is_expr && find_func(name, FALSE) != NULL)
res = generate_funcref(cctx, name, FALSE);
else
res = compile_load_scriptvar(cctx, name,
NULL, &end);
break;
case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
{
if (is_expr && ASCII_ISUPPER(*name)
&& find_func(name, FALSE) != NULL)
res = generate_funcref(cctx, name, TRUE);
else
isn_type = ISN_LOADG;
}
else
{
isn_type = ISN_LOADAUTO;
vim_free(name);
name = vim_strnsave(*arg, end - *arg);
if (name == NULL)
return FAIL;
}
break;
case 'w': isn_type = ISN_LOADW; break;
case 't': isn_type = ISN_LOADT; break;
case 'b': isn_type = ISN_LOADB; break;
default: // cannot happen, just in case
semsg(_(e_namespace_not_supported_str), *arg);
goto theend;
}
if (isn_type != ISN_DROP)
{
// Global, Buffer-local, Window-local and Tabpage-local
// variables can be defined later, thus we don't check if it
// exists, give an error at runtime.
res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
}
}
}
else
{
size_t len = end - *arg;
int idx;
int gen_load = FALSE;
int gen_load_outer = 0;
int outer_loop_depth = -1;
int outer_loop_idx = -1;
name = vim_strnsave(*arg, end - *arg);
if (name == NULL)
return FAIL;
if (STRCMP(name, "super") == 0
&& cctx->ctx_ufunc != NULL
&& (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
{
// super.SomeFunc() in a class function: push &t_super type, this
// is recognized in compile_subscript().
res = push_type_stack(cctx, &t_super);
if (*end != '.')
emsg(_(e_super_must_be_followed_by_dot));
}
else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
{
script_autoload(name, FALSE);
res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
}
else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
== OK)
{
if (gen_load_outer == 0)
gen_load = TRUE;
}
else
{
lvar_T lvar;
class_T *cl = NULL;
if (lookup_local(*arg, len, &lvar, cctx) == OK)
{
type = lvar.lv_type;
idx = lvar.lv_idx;
if (lvar.lv_from_outer != 0)
{
gen_load_outer = lvar.lv_from_outer;
outer_loop_depth = lvar.lv_loop_depth;
outer_loop_idx = lvar.lv_loop_idx;
}
else
gen_load = TRUE;
}
else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
{
res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
}
else
{
// "var" can be script-local even without using "s:" if it
// already exists in a Vim9 script or when it's imported.
if (script_var_exists(*arg, len, cctx, NULL) == OK
|| find_imported(name, 0, FALSE) != NULL)
res = compile_load_scriptvar(cctx, name, *arg, &end);
// When evaluating an expression and the name starts with an
// uppercase letter it can be a user defined function.
// generate_funcref() will fail if the function can't be found.
if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
res = generate_funcref(cctx, name, FALSE);
}
}
if (gen_load)
res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
if (gen_load_outer > 0)
{
res = generate_LOADOUTER(cctx, idx, gen_load_outer,
outer_loop_depth, outer_loop_idx, type);
cctx->ctx_outer_used = TRUE;
}
}
*arg = end;
theend:
if (res == FAIL && error && called_emsg == prev_called_emsg)
semsg(_(e_variable_not_found_str), name);
vim_free(name);
return res;
}
/*
* Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
* "str_offset" is the number of leading bytes to skip from the string.
* Returns FAIL if compilation fails.
*/
static int
compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
{
char_u *s = isn->isn_arg.string + str_offset;
garray_T save_ga = cctx->ctx_instr;
int expr_res;
int trailing_error;
int instr_count;
isn_T *instr = NULL;
// Remove the string type from the stack.
--cctx->ctx_type_stack.ga_len;
// Temporarily reset the list of instructions so that the jump labels are
// correct.
cctx->ctx_instr.ga_len = 0;
cctx->ctx_instr.ga_maxlen = 0;
cctx->ctx_instr.ga_data = NULL;
// avoid peeking a next line
int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
cctx->ctx_ufunc->uf_lines.ga_len = 0;
expr_res = compile_expr0(&s, cctx);
cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
s = skipwhite(s);
trailing_error = *s != NUL;
if (expr_res == FAIL || trailing_error
|| GA_GROW_FAILS(&cctx->ctx_instr, 1))
{
if (trailing_error)
semsg(_(e_trailing_characters_str), s);
clear_instr_ga(&cctx->ctx_instr);
cctx->ctx_instr = save_ga;
++cctx->ctx_type_stack.ga_len;
return FAIL;
}
// Move the generated instructions into the ISN_INSTR instruction, then
// restore the list of instructions.
instr_count = cctx->ctx_instr.ga_len;
instr = cctx->ctx_instr.ga_data;
instr[instr_count].isn_type = ISN_FINISH;
cctx->ctx_instr = save_ga;
vim_free(isn->isn_arg.string);
isn->isn_type = ISN_INSTR;
isn->isn_arg.instr = instr;
return OK;
}
/*
* Compile the argument expressions.
* "arg" points to just after the "(" and is advanced to after the ")"
*/
int
compile_arguments(
char_u **arg,
cctx_T *cctx,
int *argcount,
ca_special_T special_fn)
{
char_u *p = *arg;
char_u *whitep = *arg;
int must_end = FALSE;
int instr_count;
for (;;)
{
if (may_get_next_line(whitep, &p, cctx) == FAIL)
goto failret;
if (*p == ')')
{
*arg = p + 1;
return OK;
}
if (must_end)
{
semsg(_(e_missing_comma_before_argument_str), p);
return FAIL;
}
instr_count = cctx->ctx_instr.ga_len;
if (compile_expr0(&p, cctx) == FAIL)
return FAIL;
++*argcount;
if (special_fn == CA_SEARCHPAIR && *argcount == 5
&& cctx->ctx_instr.ga_len == instr_count + 1)
{
isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
// {skip} argument of searchpair() can be compiled if not empty
if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
compile_string(isn, cctx, 0);
}
else if (special_fn == CA_SUBSTITUTE && *argcount == 3
&& cctx->ctx_instr.ga_len == instr_count + 1)
{
isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
// {sub} argument of substitute() can be compiled if it starts
// with \=
if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
&& isn->isn_arg.string[1] == '=')
compile_string(isn, cctx, 2);
}
if (*p != ',' && *skipwhite(p) == ',')
{
semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
p = skipwhite(p);
}
if (*p == ',')
{
++p;
if (*p != NUL && !VIM_ISWHITE(*p))
semsg(_(e_white_space_required_after_str_str), ",", p - 1);
}
else
must_end = TRUE;
whitep = p;
p = skipwhite(p);
}
failret:
emsg(_(e_missing_closing_paren));
return FAIL;
}
/*
* Compile a function call: name(arg1, arg2)
* "arg" points to "name", "arg + varlen" to the "(".
* "argcount_init" is 1 for "value->method()"
* Instructions:
* EVAL arg1
* EVAL arg2
* BCALL / DCALL / UCALL
*/
static int
compile_call(
char_u **arg,
size_t varlen,
cctx_T *cctx,
ppconst_T *ppconst,
int argcount_init)
{
char_u *name = *arg;
char_u *p;
int argcount = argcount_init;
char_u namebuf[MAX_FUNC_NAME_LEN];
char_u fname_buf[FLEN_FIXED + 1];
char_u *tofree = NULL;
ufunc_T *ufunc = NULL;
int res = FAIL;
int is_autoload;
int has_g_namespace;
ca_special_T special_fn;
imported_T *import;
if (varlen >= sizeof(namebuf))
{
semsg(_(e_name_too_long_str), name);
return FAIL;
}
vim_strncpy(namebuf, *arg, varlen);
import = find_imported(name, varlen, FALSE);
if (import != NULL)
{
semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
return FAIL;
}
// We can evaluate "has('name')" at compile time.
// We can evaluate "len('string')" at compile time.
// We always evaluate "exists_compiled()" at compile time.
if ((varlen == 3
&& (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
|| (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
{
char_u *s = skipwhite(*arg + varlen + 1);
typval_T argvars[2];
int is_has = **arg == 'h';
int is_len = **arg == 'l';
argvars[0].v_type = VAR_UNKNOWN;
if (*s == '"')
(void)eval_string(&s, &argvars[0], TRUE, FALSE);
else if (*s == '\'')
(void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
s = skipwhite(s);
if (*s == ')' && argvars[0].v_type == VAR_STRING
&& ((is_has && !dynamic_feature(argvars[0].vval.v_string))
|| !is_has))
{
typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
*arg = s + 1;
argvars[1].v_type = VAR_UNKNOWN;
tv->v_type = VAR_NUMBER;
tv->vval.v_number = 0;
if (is_has)
f_has(argvars, tv);
else if (is_len)
f_len(argvars, tv);
else
f_exists(argvars, tv);
clear_tv(&argvars[0]);
++ppconst->pp_used;
return OK;
}
clear_tv(&argvars[0]);
if (!is_has && !is_len)
{
emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
return FAIL;
}
}
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
funcerror_T error;
name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);