-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvim9compile.c
4021 lines (3655 loc) · 103 KB
/
vim9compile.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.
*/
/*
* vim9compile.c: compiling a :def function
*/
#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
// Functions defined with :def are stored in this growarray.
// They are never removed, so that they can be found by index.
// Deleted functions have the df_deleted flag set.
garray_T def_functions = {0, 0, sizeof(dfunc_T), 50, NULL};
static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
/*
* Lookup variable "name" in the local scope and return it in "lvar".
* "lvar->lv_from_outer" is incremented accordingly.
* If "lvar" is NULL only check if the variable can be found.
* Return FAIL if not found.
*/
int
lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
{
int idx;
lvar_T *lvp;
if (len == 0)
return FAIL;
if (((len == 4 && STRNCMP(name, "this", 4) == 0)
|| (len == 5 && STRNCMP(name, "super", 5) == 0))
&& cctx->ctx_ufunc != NULL
&& (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)))
{
int is_super = *name == 's';
if (is_super)
{
if (name[5] != '.')
{
emsg(_(e_super_must_be_followed_by_dot));
return FAIL;
}
if (cctx->ctx_ufunc->uf_class != NULL
&& cctx->ctx_ufunc->uf_class->class_extends == NULL)
{
emsg(_(e_using_super_not_in_child_class));
return FAIL;
}
}
if (lvar != NULL)
{
CLEAR_POINTER(lvar);
lvar->lv_loop_depth = -1;
lvar->lv_name = (char_u *)(is_super ? "super" : "this");
if (cctx->ctx_ufunc->uf_class != NULL)
{
lvar->lv_type = &cctx->ctx_ufunc->uf_class->class_object_type;
if (is_super)
{
type_T *type = get_type_ptr(cctx->ctx_type_list);
if (type != NULL)
{
*type = *lvar->lv_type;
lvar->lv_type = type;
type->tt_flags |= TTFLAG_SUPER;
}
}
}
}
return OK;
}
// Find local in current function scope.
for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
{
lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
if (lvp->lv_name != NULL
&& STRNCMP(name, lvp->lv_name, len) == 0
&& STRLEN(lvp->lv_name) == len)
{
if (lvar != NULL)
{
*lvar = *lvp;
lvar->lv_from_outer = 0;
// If the variable was declared inside a loop set
// lvar->lv_loop_idx and lvar->lv_loop_depth.
get_loop_var_idx(cctx, idx, lvar);
}
return OK;
}
}
// Find local in outer function scope.
if (cctx->ctx_outer != NULL)
{
if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
{
if (lvar != NULL)
{
cctx->ctx_outer_used = TRUE;
++lvar->lv_from_outer;
}
return OK;
}
}
return FAIL;
}
/*
* Lookup an argument in the current function and an enclosing function.
* Returns the argument index in "idxp"
* Returns the argument type in "type"
* Sets "gen_load_outer" to TRUE if found in outer scope.
* Returns OK when found, FAIL otherwise.
*/
int
arg_exists(
char_u *name,
size_t len,
int *idxp,
type_T **type,
int *gen_load_outer,
cctx_T *cctx)
{
int idx;
char_u *va_name;
if (len == 0)
return FAIL;
for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
{
char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
{
if (idxp != NULL)
{
// Arguments are located above the frame pointer. One further
// if there is a vararg argument
*idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
+ STACK_FRAME_SIZE)
+ (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
if (cctx->ctx_ufunc->uf_arg_types != NULL)
*type = cctx->ctx_ufunc->uf_arg_types[idx];
else
*type = &t_any;
}
return OK;
}
}
va_name = cctx->ctx_ufunc->uf_va_name;
if (va_name != NULL
&& STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
{
if (idxp != NULL)
{
// varargs is always the last argument
*idxp = -STACK_FRAME_SIZE - 1;
*type = cctx->ctx_ufunc->uf_va_type;
}
return OK;
}
if (cctx->ctx_outer != NULL)
{
// Lookup the name for an argument of the outer function.
if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
== OK)
{
if (gen_load_outer != NULL)
++*gen_load_outer;
return OK;
}
}
return FAIL;
}
/*
* Lookup a script-local variable in the current script, possibly defined in a
* block that contains the function "cctx->ctx_ufunc".
* "cctx" is NULL at the script level, "cstack" is NULL in a function.
* If "len" is <= 0 "name" must be NUL terminated.
* Return NULL when not found.
*/
static sallvar_T *
find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
{
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
hashitem_T *hi;
int cc;
sallvar_T *sav;
ufunc_T *ufunc;
// Find the list of all script variables with the right name.
if (len > 0)
{
cc = name[len];
name[len] = NUL;
}
hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
if (len > 0)
name[len] = cc;
if (HASHITEM_EMPTY(hi))
return NULL;
sav = HI2SAV(hi);
if (sav->sav_block_id == 0)
// variable defined in the top script scope is always visible
return sav;
if (cctx == NULL)
{
if (cstack == NULL)
return NULL;
// Not in a function scope, find variable with block ID equal to or
// smaller than the current block id. Use "cstack" to go up the block
// scopes.
while (sav != NULL)
{
int idx;
for (idx = cstack->cs_idx; idx >= 0; --idx)
if (cstack->cs_block_id[idx] == sav->sav_block_id)
break;
if (idx >= 0)
break;
sav = sav->sav_next;
}
return sav;
}
// Go over the variables with this name and find one that was visible
// from the function.
ufunc = cctx->ctx_ufunc;
while (sav != NULL)
{
int idx;
// Go over the blocks that this function was defined in. If the
// variable block ID matches it was visible to the function.
for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
return sav;
sav = sav->sav_next;
}
// Not found, variable was not visible.
return NULL;
}
/*
* If "name" can be found in the current script set it's "block_id".
*/
void
update_script_var_block_id(char_u *name, int block_id)
{
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
hashitem_T *hi;
sallvar_T *sav;
hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
if (HASHITEM_EMPTY(hi))
return;
sav = HI2SAV(hi);
sav->sav_block_id = block_id;
}
/*
* Return TRUE if the script context is Vim9 script.
*/
int
script_is_vim9(void)
{
return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
}
/*
* Lookup a variable (without s: prefix) in the current script.
* "cctx" is NULL at the script level, "cstack" is NULL in a function.
* Returns OK or FAIL.
*/
int
script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
{
if (current_sctx.sc_sid <= 0)
return FAIL;
if (script_is_vim9())
{
// Check script variables that were visible where the function was
// defined.
if (find_script_var(name, len, cctx, cstack) != NULL)
return OK;
}
else
{
hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
dictitem_T *di;
int cc;
// Check script variables that are currently visible
cc = name[len];
name[len] = NUL;
di = find_var_in_ht(ht, 0, name, TRUE);
name[len] = cc;
if (di != NULL)
return OK;
}
return FAIL;
}
/*
* Return TRUE if "name" is a local variable, argument, script variable or
* imported. Also if "name" is "this" and in a class method.
*/
static int
variable_exists(char_u *name, size_t len, cctx_T *cctx)
{
return (cctx != NULL
&& (lookup_local(name, len, NULL, cctx) == OK
|| arg_exists(name, len, NULL, NULL, NULL, cctx) == OK
|| (len == 4
&& cctx->ctx_ufunc != NULL
&& (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW))
&& STRNCMP(name, "this", 4) == 0)))
|| script_var_exists(name, len, cctx, NULL) == OK
|| class_member_index(name, len, NULL, cctx) >= 0
|| find_imported(name, len, FALSE) != NULL;
}
/*
* Return TRUE if "name" is a local variable, argument, script variable,
* imported or function. Or commands are being skipped, a declaration may have
* been skipped then.
*/
static int
item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
{
return variable_exists(name, len, cctx);
}
/*
* Check if "p[len]" is already defined, either in script "import_sid" or in
* compilation context "cctx".
* "cctx" is NULL at the script level, "cstack" is NULL in a function.
* Does not check the global namespace.
* If "is_arg" is TRUE the error message is for an argument name.
* Return FAIL and give an error if it defined.
*/
int
check_defined(
char_u *p,
size_t len,
cctx_T *cctx,
cstack_T *cstack,
int is_arg)
{
int c = p[len];
ufunc_T *ufunc = NULL;
// underscore argument is OK
if (len == 1 && *p == '_')
return OK;
if (script_var_exists(p, len, cctx, cstack) == OK)
{
if (is_arg)
semsg(_(e_argument_already_declared_in_script_str), p);
else
semsg(_(e_variable_already_declared_in_script_str), p);
return FAIL;
}
if (class_member_index(p, len, NULL, cctx) >= 0)
{
if (is_arg)
semsg(_(e_argument_already_declared_in_class_str), p);
else
semsg(_(e_variable_already_declared_in_class_str), p);
return FAIL;
}
p[len] = NUL;
if ((cctx != NULL
&& (lookup_local(p, len, NULL, cctx) == OK
|| arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
|| find_imported(p, len, FALSE) != NULL
|| (ufunc = find_func_even_dead(p, 0)) != NULL)
{
// A local or script-local function can shadow a global function.
if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
&& (!func_is_global(ufunc)
|| (p[0] == 'g' && p[1] == ':'))))
{
if (is_arg)
semsg(_(e_argument_name_shadows_existing_variable_str), p);
else
semsg(_(e_name_already_defined_str), p);
p[len] = c;
return FAIL;
}
}
p[len] = c;
return OK;
}
/*
* Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
* used. Return FALSE if the types will never match.
*/
static int
use_typecheck(type_T *actual, type_T *expected)
{
if (actual->tt_type == VAR_ANY
|| actual->tt_type == VAR_UNKNOWN
|| (actual->tt_type == VAR_FUNC
&& (expected->tt_type == VAR_FUNC
|| expected->tt_type == VAR_PARTIAL)
&& (actual->tt_member == &t_any
|| actual->tt_member == &t_unknown
|| actual->tt_argcount < 0)
&& (actual->tt_member == &t_unknown ||
(actual->tt_member == &t_void)
== (expected->tt_member == &t_void))))
return TRUE;
if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
&& actual->tt_type == expected->tt_type)
// This takes care of a nested list or dict.
return use_typecheck(actual->tt_member, expected->tt_member);
return FALSE;
}
/*
* Check that
* - "actual" matches "expected" type or
* - "actual" is a type that can be "expected" type: add a runtime check; or
* - return FAIL.
* If "actual_is_const" is TRUE then the type won't change at runtime, do not
* generate a TYPECHECK.
*/
int
need_type_where(
type_T *actual,
type_T *expected,
int number_ok, // expect VAR_FLOAT but VAR_NUMBER is OK
int offset,
where_T where,
cctx_T *cctx,
int silent,
int actual_is_const)
{
int ret;
if (expected == &t_bool && actual != &t_bool
&& (actual->tt_flags & TTFLAG_BOOL_OK))
{
// Using "0", "1" or the result of an expression with "&&" or "||" as a
// boolean is OK but requires a conversion.
generate_2BOOL(cctx, FALSE, offset);
return OK;
}
ret = check_type_maybe(expected, actual, FALSE, where);
if (ret == OK)
return OK;
// If actual a constant a runtime check makes no sense. If it's
// null_function it is OK.
if (actual_is_const && ret == MAYBE && actual == &t_func_unknown)
return OK;
// If the actual type can be the expected type add a runtime check.
if (!actual_is_const && ret == MAYBE && use_typecheck(actual, expected))
{
generate_TYPECHECK(cctx, expected, number_ok, offset,
where.wt_variable, where.wt_index);
return OK;
}
if (!silent)
type_mismatch_where(expected, actual, where);
return FAIL;
}
int
need_type(
type_T *actual,
type_T *expected,
int number_ok, // when expected is float number is also OK
int offset,
int arg_idx,
cctx_T *cctx,
int silent,
int actual_is_const)
{
where_T where = WHERE_INIT;
where.wt_index = arg_idx;
return need_type_where(actual, expected, number_ok, offset, where,
cctx, silent, actual_is_const);
}
/*
* Set type of variable "lvar" to "type". If the variable is a constant then
* the type gets TTFLAG_CONST.
*/
static void
set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx)
{
type_T *type = type_arg;
if (lvar->lv_const == ASSIGN_CONST && (type->tt_flags & TTFLAG_CONST) == 0)
{
if (type->tt_flags & TTFLAG_STATIC)
// entry in static_types[] is followed by const type
type = type + 1;
else
{
type = copy_type(type, cctx->ctx_type_list);
type->tt_flags |= TTFLAG_CONST;
}
}
lvar->lv_type = type;
}
/*
* Reserve space for a local variable.
* "assign" can be ASSIGN_VAR for :var, ASSIGN_CONST for :const and
* ASSIGN_FINAL for :final.
* Return the variable or NULL if it failed.
*/
lvar_T *
reserve_local(
cctx_T *cctx,
char_u *name,
size_t len,
int assign,
type_T *type)
{
lvar_T *lvar;
dfunc_T *dfunc;
if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
{
emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
return NULL;
}
if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
return NULL;
lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
CLEAR_POINTER(lvar);
// Every local variable uses the next entry on the stack. We could re-use
// the last ones when leaving a scope, but then variables used in a closure
// might get overwritten. To keep things simple do not re-use stack
// entries. This is less efficient, but memory is cheap these days.
dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
lvar->lv_idx = dfunc->df_var_names.ga_len;
lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
lvar->lv_const = assign;
if (type == &t_unknown || type == &t_any)
// type not known yet, may be inferred from RHS
lvar->lv_type = type;
else
// may use TTFLAG_CONST
set_var_type(lvar, type, cctx);
// Remember the name for debugging.
if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
return NULL;
((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
vim_strsave(lvar->lv_name);
++dfunc->df_var_names.ga_len;
return lvar;
}
/*
* If "check_writable" is ASSIGN_CONST give an error if the variable was
* defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
* error if the variable was defined with :const.
*/
static int
check_item_writable(svar_T *sv, int check_writable, char_u *name)
{
if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
|| (check_writable == ASSIGN_FINAL
&& sv->sv_const == ASSIGN_CONST))
{
semsg(_(e_cannot_change_readonly_variable_str), name);
return FAIL;
}
return OK;
}
/*
* Find "name" in script-local items of script "sid".
* Pass "check_writable" to check_item_writable().
* "cctx" is NULL at the script level, "cstack" is NULL in a function.
* Returns the index in "sn_var_vals" if found.
* If found but not in "sn_var_vals" returns -1.
* If not found or the variable is not writable returns -2.
*/
int
get_script_item_idx(
int sid,
char_u *name,
int check_writable,
cctx_T *cctx,
cstack_T *cstack)
{
hashtab_T *ht;
dictitem_T *di;
scriptitem_T *si = SCRIPT_ITEM(sid);
svar_T *sv;
int idx;
if (!SCRIPT_ID_VALID(sid))
return -1;
if (sid == current_sctx.sc_sid)
{
sallvar_T *sav = find_script_var(name, 0, cctx, cstack);
if (sav == NULL)
return -2;
idx = sav->sav_var_vals_idx;
sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
if (check_item_writable(sv, check_writable, name) == FAIL)
return -2;
return idx;
}
// First look the name up in the hashtable.
ht = &SCRIPT_VARS(sid);
di = find_var_in_ht(ht, 0, name, TRUE);
if (di == NULL)
{
if (si->sn_autoload_prefix != NULL)
{
hashitem_T *hi;
// A variable exported from an autoload script is in the global
// variables, we can find it in the all_vars table.
hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
if (!HASHITEM_EMPTY(hi))
return HI2SAV(hi)->sav_var_vals_idx;
}
return -2;
}
// Now find the svar_T index in sn_var_vals.
for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
{
sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
if (sv->sv_tv == &di->di_tv)
{
if (check_item_writable(sv, check_writable, name) == FAIL)
return -2;
return idx;
}
}
return -1;
}
static imported_T *
find_imported_in_script(char_u *name, size_t len, int sid)
{
scriptitem_T *si;
int idx;
if (!SCRIPT_ID_VALID(sid))
return NULL;
si = SCRIPT_ITEM(sid);
for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
{
imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
if (len == 0 ? STRCMP(name, import->imp_name) == 0
: STRLEN(import->imp_name) == len
&& STRNCMP(name, import->imp_name, len) == 0)
return import;
}
return NULL;
}
/*
* Find "name" in imported items of the current script.
* If "len" is 0 use any length that works.
* If "load" is TRUE and the script was not loaded yet, load it now.
*/
imported_T *
find_imported(char_u *name, size_t len, int load)
{
if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
return NULL;
// Skip over "s:" before "s:something" to find the import name.
int off = name[0] == 's' && name[1] == ':' ? 2 : 0;
imported_T *ret = find_imported_in_script(name + off, len - off,
current_sctx.sc_sid);
if (ret != NULL && load && (ret->imp_flags & IMP_FLAGS_AUTOLOAD))
{
scid_T actual_sid = 0;
int save_emsg_off = emsg_off;
// "emsg_off" will be set when evaluating an expression silently, but
// we do want to know about errors in a script. Also because it then
// aborts when an error is encountered.
emsg_off = FALSE;
// script found before but not loaded yet
ret->imp_flags &= ~IMP_FLAGS_AUTOLOAD;
(void)do_source(SCRIPT_ITEM(ret->imp_sid)->sn_name, FALSE,
DOSO_NONE, &actual_sid);
// If the script is a symlink it may be sourced with another name, may
// need to adjust the script ID for that.
if (actual_sid != 0)
ret->imp_sid = actual_sid;
emsg_off = save_emsg_off;
}
return ret;
}
/*
* Called when checking for a following operator at "arg". When the rest of
* the line is empty or only a comment, peek the next line. If there is a next
* line return a pointer to it and set "nextp".
* Otherwise skip over white space.
*/
char_u *
may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
{
char_u *p = skipwhite(arg);
*nextp = NULL;
if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
{
*nextp = peek_next_line_from_context(cctx);
if (*nextp != NULL)
return *nextp;
}
return p;
}
/*
* Return a pointer to the next line that isn't empty or only contains a
* comment. Skips over white space.
* Returns NULL if there is none.
*/
char_u *
peek_next_line_from_context(cctx_T *cctx)
{
int lnum = cctx->ctx_lnum;
while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
{
char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
char_u *p;
// ignore NULLs inserted for continuation lines
if (line != NULL)
{
p = skipwhite(line);
if (vim9_bad_comment(p))
return NULL;
if (*p != NUL && !vim9_comment_start(p))
return p;
}
}
return NULL;
}
/*
* Get the next line of the function from "cctx".
* Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
* Returns NULL when at the end.
*/
char_u *
next_line_from_context(cctx_T *cctx, int skip_comment)
{
char_u *line;
do
{
++cctx->ctx_lnum;
if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
{
line = NULL;
break;
}
line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
cctx->ctx_line_start = line;
SOURCING_LNUM = cctx->ctx_lnum + 1;
} while (line == NULL || *skipwhite(line) == NUL
|| (skip_comment && vim9_comment_start(skipwhite(line))));
return line;
}
/*
* Skip over white space at "whitep" and assign to "*arg".
* If "*arg" is at the end of the line, advance to the next line.
* Also when "whitep" points to white space and "*arg" is on a "#".
* Return FAIL if beyond the last line, "*arg" is unmodified then.
*/
int
may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
{
*arg = skipwhite(whitep);
if (vim9_bad_comment(*arg))
return FAIL;
if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
{
char_u *next = next_line_from_context(cctx, TRUE);
if (next == NULL)
return FAIL;
*arg = skipwhite(next);
}
return OK;
}
/*
* Idem, and give an error when failed.
*/
int
may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
{
if (may_get_next_line(whitep, arg, cctx) == FAIL)
{
SOURCING_LNUM = cctx->ctx_lnum + 1;
emsg(_(e_line_incomplete));
return FAIL;
}
return OK;
}
/*
* Get a line from the compilation context, compatible with exarg_T getline().
* Return a pointer to the line in allocated memory.
* Return NULL for end-of-file or some error.
*/
static char_u *
exarg_getline(
int c UNUSED,
void *cookie,
int indent UNUSED,
getline_opt_T options UNUSED)
{
cctx_T *cctx = (cctx_T *)cookie;
char_u *p;
for (;;)
{
if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
return NULL;
++cctx->ctx_lnum;
p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
// Comment lines result in NULL pointers, skip them.
if (p != NULL)
return vim_strsave(p);
}
}
void
fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
{
eap->getline = exarg_getline;
eap->cookie = cctx;
eap->skip = cctx->ctx_skip == SKIP_YES;
}
/*
* Return TRUE if "ufunc" should be compiled, taking into account whether
* "profile" indicates profiling is to be done.
*/
int
func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
{
switch (ufunc->uf_def_status)
{
case UF_TO_BE_COMPILED:
return TRUE;
case UF_COMPILED:
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
switch (compile_type)
{
case CT_PROFILE:
#ifdef FEAT_PROFILE
return dfunc->df_instr_prof == NULL;
#endif
case CT_NONE:
return dfunc->df_instr == NULL;
case CT_DEBUG:
return dfunc->df_instr_debug == NULL;
}
}
case UF_NOT_COMPILED:
case UF_COMPILE_ERROR:
case UF_COMPILING:
break;
}
return FALSE;
}
/*
* Compile a nested :def command.
*/
static char_u *
compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
{
int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
char_u *name_start = eap->arg;
char_u *name_end = to_name_end(eap->arg, TRUE);
int off;
char_u *func_name;
char_u *lambda_name;
ufunc_T *ufunc;
int r = FAIL;
compiletype_T compile_type;
isn_T *funcref_isn = NULL;
lvar_T *lvar = NULL;
if (eap->forceit)
{
emsg(_(e_cannot_use_bang_with_nested_def));
return NULL;
}
if (*name_start == '/')
{
name_end = skip_regexp(name_start + 1, '/', TRUE);
if (*name_end == '/')
++name_end;
set_nextcmd(eap, name_end);
}
if (name_end == name_start || *skipwhite(name_end) != '(')
{
if (!ends_excmd2(name_start, name_end))
{
if (*skipwhite(name_end) == '.')
semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
eap->cmd);
else
semsg(_(e_invalid_command_str), eap->cmd);
return NULL;
}
// "def" or "def Name": list functions
if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
return NULL;
return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
}
// Only g:Func() can use a namespace.
if (name_start[1] == ':' && !is_global)
{
semsg(_(e_namespace_not_supported_str), name_start);
return NULL;
}
if (cctx->ctx_skip != SKIP_YES
&& check_defined(name_start, name_end - name_start, cctx,
NULL, FALSE) == FAIL)
return NULL;
if (!ASCII_ISUPPER(is_global ? name_start[2] : name_start[0]))
{
semsg(_(e_function_name_must_start_with_capital_str), name_start);
return NULL;
}