#define DEFINE_VIM9_GLOBALS
#include "vim9.h"
+// values for ctx_skip
+typedef enum {
+ SKIP_NOT, // condition is a constant, produce code
+ SKIP_YES, // condition is a constant, do NOT produce code
+ SKIP_UNKNONW // condition is not a constant, produce code
+} skip_T;
+
/*
* Chain of jump instructions where the end label needs to be set.
*/
* info specific for the scope of :if / elseif / else
*/
typedef struct {
+ int is_seen_else;
+ int is_had_return; // every block ends in :return
int is_if_label; // instruction idx at IF or ELSEIF
endlabel_T *is_end_label; // instructions to set end label
} ifscope_T;
scope_T *se_outer; // scope containing this one
scopetype_T se_type;
int se_local_count; // ctx_locals.ga_len before scope
+ skip_T se_skip_save; // ctx_skip before the block
union {
ifscope_T se_if;
whilescope_T se_while;
int lv_arg; // when TRUE this is an argument
} lvar_T;
-// values for ctx_skip
-typedef enum {
- SKIP_NOT, // condition is a constant, produce code
- SKIP_YES, // condition is a constant, do NOT produce code
- SKIP_UNKNONW // condition is not a constant, produce code
-} skip_T;
-
/*
* Context for compiling lines of Vim script.
* Stores info about the local variables and condition stack.
skip_T ctx_skip;
scope_T *ctx_scope; // current scope, NULL at toplevel
+ int ctx_had_return; // last seen statement was "return"
cctx_T *ctx_outer; // outer scope for lambda or nested
// function
garray_T *instr = &cctx->ctx_instr;
int instr_count = instr->ga_len;
scope_T *scope;
+ skip_T skip_save = cctx->ctx_skip;
ppconst_T ppconst;
CLEAR_FIELD(ppconst);
clear_ppconst(&ppconst);
return NULL;
}
- if (instr->ga_len == instr_count && ppconst.pp_used == 1)
+ if (cctx->ctx_skip == SKIP_YES)
+ clear_ppconst(&ppconst);
+ else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
{
// The expression results in a constant.
- // TODO: how about nesting?
cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
clear_ppconst(&ppconst);
}
scope = new_scope(cctx, IF_SCOPE);
if (scope == NULL)
return NULL;
+ scope->se_skip_save = skip_save;
+ // "is_had_return" will be reset if any block does not end in :return
+ scope->se_u.se_if.is_had_return = TRUE;
if (cctx->ctx_skip == SKIP_UNKNONW)
{
return NULL;
}
unwind_locals(cctx, scope->se_local_count);
+ if (!cctx->ctx_had_return)
+ scope->se_u.se_if.is_had_return = FALSE;
if (cctx->ctx_skip == SKIP_UNKNONW)
{
clear_ppconst(&ppconst);
return NULL;
}
- if (instr->ga_len == instr_count && ppconst.pp_used == 1)
+ if (scope->se_skip_save == SKIP_YES)
+ clear_ppconst(&ppconst);
+ else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
{
// The expression results in a constant.
// TODO: how about nesting?
return NULL;
}
unwind_locals(cctx, scope->se_local_count);
+ if (!cctx->ctx_had_return)
+ scope->se_u.se_if.is_had_return = FALSE;
+ scope->se_u.se_if.is_seen_else = TRUE;
- // jump from previous block to the end, unless the else block is empty
- if (cctx->ctx_skip == SKIP_UNKNONW)
+ if (scope->se_skip_save != SKIP_YES)
{
- if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
+ // jump from previous block to the end, unless the else block is empty
+ if (cctx->ctx_skip == SKIP_UNKNONW)
+ {
+ if (!cctx->ctx_had_return
+ && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
JUMP_ALWAYS, cctx) == FAIL)
- return NULL;
- }
+ return NULL;
+ }
- if (cctx->ctx_skip == SKIP_UNKNONW)
- {
- if (scope->se_u.se_if.is_if_label >= 0)
+ if (cctx->ctx_skip == SKIP_UNKNONW)
{
- // previous "if" or "elseif" jumps here
- isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
- isn->isn_arg.jump.jump_where = instr->ga_len;
- scope->se_u.se_if.is_if_label = -1;
+ if (scope->se_u.se_if.is_if_label >= 0)
+ {
+ // previous "if" or "elseif" jumps here
+ isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
+ isn->isn_arg.jump.jump_where = instr->ga_len;
+ scope->se_u.se_if.is_if_label = -1;
+ }
}
- }
- if (cctx->ctx_skip != SKIP_UNKNONW)
- cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
+ if (cctx->ctx_skip != SKIP_UNKNONW)
+ cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
+ }
return p;
}
}
ifscope = &scope->se_u.se_if;
unwind_locals(cctx, scope->se_local_count);
+ if (!cctx->ctx_had_return)
+ ifscope->is_had_return = FALSE;
if (scope->se_u.se_if.is_if_label >= 0)
{
}
// Fill in the "end" label in jumps at the end of the blocks.
compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
- // TODO: this should restore the value from before the :if
- cctx->ctx_skip = SKIP_UNKNONW;
+ cctx->ctx_skip = scope->se_skip_save;
+
+ // If all the blocks end in :return and there is an :else then the
+ // had_return flag is set.
+ cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
drop_scope(cctx);
return arg;
char_u *line = NULL;
char_u *p;
char *errormsg = NULL; // error message
- int had_return = FALSE;
cctx_T cctx;
garray_T *instr;
int called_emsg_before = called_emsg;
}
emsg_before = called_emsg;
- had_return = FALSE;
CLEAR_FIELD(ea);
ea.cmdlinep = &line;
ea.cmd = skipwhite(line);
continue;
}
+ if (ea.cmdidx != CMD_elseif
+ && ea.cmdidx != CMD_else
+ && ea.cmdidx != CMD_endif
+ && ea.cmdidx != CMD_endfor
+ && ea.cmdidx != CMD_endwhile
+ && ea.cmdidx != CMD_catch
+ && ea.cmdidx != CMD_finally
+ && ea.cmdidx != CMD_endtry)
+ {
+ if (cctx.ctx_had_return)
+ {
+ emsg(_("E1095: Unreachable code after :return"));
+ goto erret;
+ }
+ }
+
switch (ea.cmdidx)
{
case CMD_def:
case CMD_return:
line = compile_return(p, set_return_type, &cctx);
- had_return = TRUE;
+ cctx.ctx_had_return = TRUE;
break;
case CMD_let:
break;
case CMD_elseif:
line = compile_elseif(p, &cctx);
+ cctx.ctx_had_return = FALSE;
break;
case CMD_else:
line = compile_else(p, &cctx);
+ cctx.ctx_had_return = FALSE;
break;
case CMD_endif:
line = compile_endif(p, &cctx);
break;
case CMD_endwhile:
line = compile_endwhile(p, &cctx);
+ cctx.ctx_had_return = FALSE;
break;
case CMD_for:
break;
case CMD_endfor:
line = compile_endfor(p, &cctx);
+ cctx.ctx_had_return = FALSE;
break;
case CMD_continue:
line = compile_continue(p, &cctx);
break;
case CMD_catch:
line = compile_catch(p, &cctx);
+ cctx.ctx_had_return = FALSE;
break;
case CMD_finally:
line = compile_finally(p, &cctx);
+ cctx.ctx_had_return = FALSE;
break;
case CMD_endtry:
line = compile_endtry(p, &cctx);
+ cctx.ctx_had_return = FALSE;
break;
case CMD_throw:
line = compile_throw(p, &cctx);
goto erret;
}
- if (!had_return)
+ if (!cctx.ctx_had_return)
{
if (ufunc->uf_ret_type->tt_type != VAR_VOID)
{