typedef struct {
char_u *lv_name;
type_T *lv_type;
- int lv_idx; // index of the variable on the stack
- int lv_const; // when TRUE cannot be assigned to
- int lv_arg; // when TRUE this is an argument
+ int lv_idx; // index of the variable on the stack
+ int lv_from_outer; // when TRUE using ctx_outer scope
+ int lv_const; // when TRUE cannot be assigned to
+ int lv_arg; // when TRUE this is an argument
} lvar_T;
/*
cctx_T *ctx_outer; // outer scope for lambda or nested
// function
+ int ctx_outer_used; // var in ctx_outer was used
garray_T ctx_type_stack; // type of each item on the stack
garray_T *ctx_type_list; // list of pointers to allocated types
lookup_local(char_u *name, size_t len, cctx_T *cctx)
{
int idx;
+ lvar_T *lvar;
if (len == 0)
return NULL;
+
+ // Find local in current function scope.
for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
{
- lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
-
+ lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
if (STRNCMP(name, lvar->lv_name, len) == 0
&& STRLEN(lvar->lv_name) == len)
+ {
+ lvar->lv_from_outer = FALSE;
return lvar;
+ }
}
+
+ // Find local in outer function scope.
+ if (cctx->ctx_outer != NULL)
+ {
+ lvar = lookup_local(name, len, cctx->ctx_outer);
+ if (lvar != NULL)
+ {
+ // TODO: are there situations we should not mark the outer scope as
+ // used?
+ cctx->ctx_outer_used = TRUE;
+ lvar->lv_from_outer = TRUE;
+ return lvar;
+ }
+ }
+
return NULL;
}
return &t_any; // not used
}
+ static void
+type_mismatch(type_T *expected, type_T *actual)
+{
+ char *tofree1, *tofree2;
+
+ semsg(_("E1013: type mismatch, expected %s but got %s"),
+ type_name(expected, &tofree1), type_name(actual, &tofree2));
+ vim_free(tofree1);
+ vim_free(tofree2);
+}
+
+ static void
+arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
+{
+ char *tofree1, *tofree2;
+
+ semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
+ argidx,
+ type_name(expected, &tofree1), type_name(actual, &tofree2));
+ vim_free(tofree1);
+ vim_free(tofree2);
+}
+
+/*
+ * Check if the expected and actual types match.
+ * Does not allow for assigning "any" to a specific type.
+ */
+ static int
+check_type(type_T *expected, type_T *actual, int give_msg)
+{
+ 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))
+
+ {
+ if (expected->tt_type != actual->tt_type)
+ {
+ if (give_msg)
+ type_mismatch(expected, actual);
+ 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 != &t_unknown)
+ ret = check_type(expected->tt_member, actual->tt_member, FALSE);
+ }
+ else if (expected->tt_type == VAR_FUNC)
+ {
+ if (expected->tt_member != &t_unknown)
+ ret = check_type(expected->tt_member, actual->tt_member, FALSE);
+ if (ret == OK && expected->tt_argcount != -1
+ && (actual->tt_argcount < expected->tt_min_argcount
+ || actual->tt_argcount > expected->tt_argcount))
+ ret = FAIL;
+ }
+ if (ret == FAIL && give_msg)
+ type_mismatch(expected, actual);
+ }
+ return ret;
+}
+
/////////////////////////////////////////////////////////////////////
// Following generate_ functions expect the caller to call ga_grow().
return OK;
}
+/*
+ * Check that
+ * - "actual" is "expected" type or
+ * - "actual" is a type that can be "expected" type: add a runtime check; or
+ * - return FAIL.
+ */
+ static int
+need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
+{
+ if (check_type(expected, actual, FALSE) == OK)
+ return OK;
+ if (actual->tt_type != VAR_ANY
+ && actual->tt_type != VAR_UNKNOWN
+ && !(actual->tt_type == VAR_FUNC
+ && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
+ {
+ type_mismatch(expected, actual);
+ return FAIL;
+ }
+ generate_TYPECHECK(cctx, expected, offset);
+ return OK;
+}
+
/*
* Generate an ISN_PUSHNR instruction.
*/
else
expected = ufunc->uf_va_type->tt_member;
actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
- if (check_type(expected, actual, FALSE) == FAIL)
+ if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
{
arg_type_mismatch(expected, actual, i + 1);
return FAIL;
if (*p == '>')
++p;
}
+ else if (*p == '(' && STRNCMP("func", start, 4) == 0)
+ {
+ // handle func(args): type
+ ++p;
+ while (*p != ')' && *p != NUL)
+ {
+ p = skip_type(p);
+ if (*p == ',')
+ p = skipwhite(p + 1);
+ }
+ if (*p == ')' && p[1] == ':')
+ p = skip_type(skipwhite(p + 2));
+ }
+
return p;
}
size_t len = end - *arg;
int idx;
int gen_load = FALSE;
+ int gen_load_outer = FALSE;
name = vim_strnsave(*arg, end - *arg);
if (name == NULL)
{
type = lvar->lv_type;
idx = lvar->lv_idx;
- gen_load = TRUE;
+ if (lvar->lv_from_outer)
+ gen_load_outer = TRUE;
+ else
+ gen_load = TRUE;
}
else
{
}
if (gen_load)
res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
+ if (gen_load_outer)
+ res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
}
*arg = end;
return p;
}
- static void
-type_mismatch(type_T *expected, type_T *actual)
-{
- char *tofree1, *tofree2;
-
- semsg(_("E1013: type mismatch, expected %s but got %s"),
- type_name(expected, &tofree1), type_name(actual, &tofree2));
- vim_free(tofree1);
- vim_free(tofree2);
-}
-
- static void
-arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
-{
- char *tofree1, *tofree2;
-
- semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
- argidx,
- type_name(expected, &tofree1), type_name(actual, &tofree2));
- vim_free(tofree1);
- vim_free(tofree2);
-}
-
-/*
- * Check if the expected and actual types match.
- * Does not allow for assigning "any" to a specific type.
- */
- static int
-check_type(type_T *expected, type_T *actual, int give_msg)
-{
- 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))
-
- {
- if (expected->tt_type != actual->tt_type)
- {
- if (give_msg)
- type_mismatch(expected, actual);
- 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 != &t_unknown)
- ret = check_type(expected->tt_member, actual->tt_member, FALSE);
- }
- else if (expected->tt_type == VAR_FUNC)
- {
- if (expected->tt_member != &t_unknown)
- ret = check_type(expected->tt_member, actual->tt_member, FALSE);
- if (ret == OK && expected->tt_argcount != -1
- && (actual->tt_argcount < expected->tt_min_argcount
- || actual->tt_argcount > expected->tt_argcount))
- ret = FAIL;
- }
- if (ret == FAIL && give_msg)
- type_mismatch(expected, actual);
- }
- return ret;
-}
-
-/*
- * Check that
- * - "actual" is "expected" type or
- * - "actual" is a type that can be "expected" type: add a runtime check; or
- * - return FAIL.
- */
- static int
-need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
-{
- if (check_type(expected, actual, FALSE) == OK)
- return OK;
- if (actual->tt_type != VAR_ANY
- && actual->tt_type != VAR_UNKNOWN
- && !(actual->tt_type == VAR_FUNC
- && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
- {
- type_mismatch(expected, actual);
- return FAIL;
- }
- generate_TYPECHECK(cctx, expected, offset);
- return OK;
-}
-
/*
* parse a list: [expr, expr]
* "*arg" points to the '['.
// The function will have one line: "return {expr}".
// Compile it into instructions.
- compile_def_function(ufunc, TRUE);
+ compile_def_function(ufunc, TRUE, cctx);
if (ufunc->uf_dfunc_idx >= 0)
{
// The function will have one line: "return {expr}".
// Compile it into instructions.
- compile_def_function(ufunc, TRUE);
+ compile_def_function(ufunc, TRUE, cctx);
// compile the arguments
*arg = skipwhite(*arg + 1);
semsg(_("E1017: Variable already declared: %s"), name);
goto theend;
}
- else
+ else if (lvar->lv_const)
{
- if (lvar->lv_const)
- {
- semsg(_("E1018: Cannot assign to a constant: %s"),
- name);
- goto theend;
- }
+ semsg(_("E1018: Cannot assign to a constant: %s"), name);
+ goto theend;
}
}
else if (STRNCMP(arg, "s:", 2) == 0
* Adds the function to "def_functions".
* When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
* return statement (used for lambda).
+ * "outer_cctx" is set for a nested function.
* This can be used recursively through compile_lambda(), which may reallocate
* "def_functions".
*/
void
-compile_def_function(ufunc_T *ufunc, int set_return_type)
+compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
{
char_u *line = NULL;
char_u *p;
CLEAR_FIELD(cctx);
cctx.ctx_ufunc = ufunc;
cctx.ctx_lnum = -1;
+ cctx.ctx_outer = outer_cctx;
ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
dfunc->df_instr = instr->ga_data;
dfunc->df_instr_count = instr->ga_len;
dfunc->df_varcount = cctx.ctx_locals_count;
+ if (cctx.ctx_outer_used)
+ ufunc->uf_flags |= FC_CLOSURE;
}
{
case ISN_INDEX:
case ISN_JUMP:
case ISN_LOAD:
+ case ISN_LOADOUTER:
case ISN_LOADSCRIPT:
case ISN_LOADREG:
case ISN_LOADV:
garray_T ec_stack; // stack of typval_T values
int ec_frame; // index in ec_stack: context of ec_dfunc_idx
+ garray_T *ec_outer_stack; // stack used for closures
+ int ec_outer_frame; // stack frame in ec_outer_stack
+
garray_T ec_trystack; // stack of trycmd_T values
int ec_in_catch; // when TRUE in catch or finally block
ectx->ec_instr = dfunc->df_instr;
estack_push_ufunc(ETYPE_UFUNC, dfunc->df_ufunc, 1);
+ // used for closures
+ ectx->ec_outer_stack = ufunc->uf_ectx_stack;
+ ectx->ec_outer_frame = ufunc->uf_ectx_frame;
+
// Decide where to start execution, handles optional arguments.
init_instr_idx(ufunc, argcount, ectx);
// Get pointer to a local variable on the stack. Negative for arguments.
#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame + STACK_FRAME_SIZE + idx)
+// Like STACK_TV_VAR but use the outer scope
+#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
+
CLEAR_FIELD(ectx);
ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
if (ga_grow(&ectx.ec_stack, 20) == FAIL)
++ectx.ec_stack.ga_len;
break;
+ // load variable or argument from outer scope
+ case ISN_LOADOUTER:
+ if (ga_grow(&ectx.ec_stack, 1) == FAIL)
+ goto failed;
+ copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
+ STACK_TV_BOT(0));
+ ++ectx.ec_stack.ga_len;
+ break;
+
// load v: variable
case ISN_LOADV:
if (ga_grow(&ectx.ec_stack, 1) == FAIL)
pt->pt_refcount = 1;
++dfunc->df_ufunc->uf_refcount;
+ if (dfunc->df_ufunc->uf_flags & FC_CLOSURE)
+ {
+ // Closure needs to find local variables in the current
+ // stack.
+ dfunc->df_ufunc->uf_ectx_stack = &ectx.ec_stack;
+ dfunc->df_ufunc->uf_ectx_frame = ectx.ec_frame;
+ }
+
if (ga_grow(&ectx.ec_stack, 1) == FAIL)
goto failed;
tv = STACK_TV_BOT(0);
checktype_T *ct = &iptr->isn_arg.type;
tv = STACK_TV_BOT(ct->ct_off);
- if (tv->v_type != ct->ct_type)
+ // TODO: better type comparison
+ if (tv->v_type != ct->ct_type
+ && !((tv->v_type == VAR_PARTIAL
+ && ct->ct_type == VAR_FUNC)
+ || (tv->v_type == VAR_FUNC
+ && ct->ct_type == VAR_PARTIAL)))
{
semsg(_("E1029: Expected %s but got %s"),
vartype_name(ct->ct_type),
(long long)(iptr->isn_arg.number));
break;
case ISN_LOAD:
- if (iptr->isn_arg.number < 0)
- smsg("%4d LOAD arg[%lld]", current,
- (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
- else
- smsg("%4d LOAD $%lld", current,
+ case ISN_LOADOUTER:
+ {
+ char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
+
+ if (iptr->isn_arg.number < 0)
+ smsg("%4d LOAD%s arg[%lld]", current, add,
+ (long long)(iptr->isn_arg.number
+ + STACK_FRAME_SIZE));
+ else
+ smsg("%4d LOAD%s $%lld", current, add,
(long long)(iptr->isn_arg.number));
+ }
break;
case ISN_LOADV:
smsg("%4d LOADV v:%s", current,