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_argcount < 0)))
+ 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.
*/
static int
need_type(
type_T *expected,
int offset,
cctx_T *cctx,
- int silent)
+ int silent,
+ int actual_is_const)
{
if (expected == &t_bool && actual != &t_bool
&& (actual->tt_flags & TTFLAG_BOOL_OK))
return OK;
// If the actual type can be the expected type add a runtime check.
- // TODO: if it's a constant a runtime check makes no sense.
- 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_argcount < 0))
- || (actual->tt_type == VAR_LIST
- && expected->tt_type == VAR_LIST
- && actual->tt_member == &t_any)
- || (actual->tt_type == VAR_DICT
- && expected->tt_type == VAR_DICT
- && actual->tt_member == &t_any))
+ // If it's a constant a runtime check makes no sense.
+ if (!actual_is_const && use_typecheck(actual, expected))
{
generate_TYPECHECK(cctx, expected, offset);
return OK;
else
expected = ufunc->uf_va_type->tt_member;
actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
- if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
+ if (need_type(actual, expected, -argcount + i, cctx,
+ TRUE, FALSE) == FAIL)
{
arg_type_mismatch(expected, actual, i + 1);
return FAIL;
typedef struct {
typval_T pp_tv[PPSIZE]; // stack of ppconst constants
int pp_used; // active entries in pp_tv[]
+ int pp_is_const; // all generated code was constants, used for a
+ // list or dict with constant members
} ppconst_T;
+static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
static int compile_expr0(char_u **arg, cctx_T *cctx);
static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
/*
* parse a list: [expr, expr]
* "*arg" points to the '['.
+ * ppconst->pp_is_const is set if all items are a constant.
*/
static int
-compile_list(char_u **arg, cctx_T *cctx)
+compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
char_u *p = skipwhite(*arg + 1);
char_u *whitep = *arg + 1;
int count = 0;
+ int is_const;
+ int is_all_const = TRUE; // reset when non-const encountered
for (;;)
{
++p;
break;
}
- if (compile_expr0(&p, cctx) == FAIL)
+ if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
return FAIL;
+ if (!is_const)
+ is_all_const = FALSE;
++count;
if (*p == ',')
{
}
*arg = p;
- generate_NEWLIST(cctx, count);
- return OK;
+ ppconst->pp_is_const = is_all_const;
+ return generate_NEWLIST(cctx, count);
}
/*
/*
* parse a dict: {'key': val} or #{key: val}
* "*arg" points to the '{'.
+ * ppconst->pp_is_const is set if all item values are a constant.
*/
static int
-compile_dict(char_u **arg, cctx_T *cctx, int literal)
+compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
{
garray_T *instr = &cctx->ctx_instr;
garray_T *stack = &cctx->ctx_type_stack;
dictitem_T *item;
char_u *whitep = *arg;
char_u *p;
+ int is_const;
+ int is_all_const = TRUE; // reset when non-const encountered
if (d == NULL)
return FAIL;
{
type_T *keytype = ((type_T **)stack->ga_data)
[stack->ga_len - 1];
- if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
+ if (need_type(keytype, &t_string, -1, cctx,
+ FALSE, FALSE) == FAIL)
return FAIL;
}
}
goto failret;
}
- if (compile_expr0(arg, cctx) == FAIL)
+ if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
return FAIL;
+ if (!is_const)
+ is_all_const = FALSE;
++count;
whitep = *arg;
*arg += STRLEN(*arg);
dict_unref(d);
+ ppconst->pp_is_const = is_all_const;
return generate_NEWDICT(cctx, count);
failret:
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
+ ppconst->pp_is_const = FALSE;
// funcref(arg)
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
+ ppconst->pp_is_const = FALSE;
// something->method()
// Apply the '!', '-' and '+' first:
// TODO: recognize list or dict at runtime
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
+ ppconst->pp_is_const = FALSE;
++p;
*arg = skipwhite(p);
vtype = VAR_DICT;
if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
{
- if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
+ if (need_type(valtype, &t_number, -1, cctx,
+ FALSE, FALSE) == FAIL)
return FAIL;
if (is_slice)
{
valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
- if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
+ if (need_type(valtype, &t_number, -2, cctx,
+ FALSE, FALSE) == FAIL)
return FAIL;
}
}
*typep = (*typep)->tt_member;
else
{
- if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
+ if (need_type(*typep, &t_dict_any, -2, cctx,
+ FALSE, FALSE) == FAIL)
return FAIL;
*typep = &t_any;
}
}
else if (*p == '.' && p[1] != '.')
{
+ // dictionary member: dict.name
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
+ ppconst->pp_is_const = FALSE;
*arg = p + 1;
if (may_get_next_line(*arg, arg, cctx) == FAIL)
emsg(_(e_missing_name_after_dot));
return FAIL;
}
- // dictionary member: dict.name
p = *arg;
if (eval_isdictc(*p))
while (eval_isnamec(*p))
* Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
* "arg" is advanced until after the expression, skipping white space.
*
- * If the value is a constant "ppconst->pp_ret" will be set.
+ * If the value is a constant "ppconst->pp_used" will be non-zero.
* Before instructions are generated, any values in "ppconst" will generated.
*
* This is the compiling equivalent of eval1(), eval2(), etc.
typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
int used_before = ppconst->pp_used;
+ ppconst->pp_is_const = FALSE;
+
/*
* Skip '!', '-' and '+' characters. They are handled later.
*/
/*
* List: [expr, expr]
*/
- case '[': ret = compile_list(arg, cctx);
+ case '[': ret = compile_list(arg, cctx, ppconst);
break;
/*
case '#': if ((*arg)[1] == '{')
{
++*arg;
- ret = compile_dict(arg, cctx, TRUE);
+ ret = compile_dict(arg, cctx, TRUE, ppconst);
}
else
ret = NOTDONE;
if (ret != FAIL && *start == '>')
ret = compile_lambda(arg, cctx);
else
- ret = compile_dict(arg, cctx, FALSE);
+ ret = compile_dict(arg, cctx, FALSE, ppconst);
}
break;
actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (check_type(want_type, actual, FALSE, 0) == FAIL)
{
- if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
+ if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
return FAIL;
}
}
/*
* Toplevel expression.
+ * Sets "is_const" (if not NULL) to indicate the value is a constant.
+ * Returns OK or FAIL.
*/
static int
-compile_expr0(char_u **arg, cctx_T *cctx)
+compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
{
ppconst_T ppconst;
clear_ppconst(&ppconst);
return FAIL;
}
+ if (is_const != NULL)
+ *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
if (generate_ppconst(cctx, &ppconst) == FAIL)
return FAIL;
return OK;
}
+/*
+ * Toplevel expression.
+ */
+ static int
+compile_expr0(char_u **arg, cctx_T *cctx)
+{
+ return compile_expr0_ext(arg, cctx, NULL);
+}
+
/*
* compile "return [expr]"
*/
return NULL;
}
if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
- cctx, FALSE) == FAIL)
+ cctx, FALSE, FALSE) == FAIL)
return NULL;
}
}
emsg(_(e_cannot_use_void_value));
goto theend;
}
- if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
+ if (need_type(stacktype, &t_list_any, -1, cctx,
+ FALSE, FALSE) == FAIL)
goto theend;
// TODO: check the length of a constant list here
generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
else if (oplen > 0)
{
type_T *stacktype;
+ int is_const = FALSE;
// For "var = expr" evaluate the expression.
if (var_count == 0)
--cctx->ctx_locals.ga_len;
instr_count = instr->ga_len;
p = skipwhite(op + oplen);
- r = compile_expr0(&p, cctx);
+ r = compile_expr0_ext(&p, cctx, &is_const);
if (new_local)
++cctx->ctx_locals.ga_len;
if (r == FAIL)
// could be indexing "any"
use_type = &t_any;
}
- if (need_type(stacktype, use_type, -1, cctx, FALSE)
- == FAIL)
+ if (need_type(stacktype, use_type, -1, cctx,
+ FALSE, is_const) == FAIL)
goto theend;
}
}
else if (*p != '=' && need_type(stacktype, member_type, -1,
- cctx, FALSE) == FAIL)
+ cctx, FALSE, FALSE) == FAIL)
goto theend;
}
else if (cmdidx == CMD_final)
// If variable is float operation with number is OK.
!(expected == &t_float && stacktype == &t_number) &&
#endif
- need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
+ need_type(stacktype, expected, -1, cctx,
+ FALSE, FALSE) == FAIL)
goto theend;
if (*op == '.')
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (type != &t_bool && type != &t_number && type != &t_any
- && need_type(type, &t_bool, -1, cctx, FALSE) == FAIL)
+ && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
return FAIL;
return OK;
}
// Now that we know the type of "var", check that it is a list, now or at
// runtime.
vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
- if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
+ if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
{
drop_scope(cctx);
return NULL;