Stop storing 'is_block' inside the parser

is_block is a field which supports 'status is-block', and also controls
whether notifications get posted. However there is no reason to store
this as a distinct field since it is trivially computed from the block
list. Stop storing it. No functional changes in this commit.
This commit is contained in:
ridiculousfish 2021-07-28 12:55:01 -07:00
parent b3cdf4afe1
commit b914c94cc1
4 changed files with 16 additions and 20 deletions

View File

@ -409,7 +409,7 @@ maybe_t<int> builtin_status(parser_t &parser, io_streams_t &streams, const wchar
}
case STATUS_IS_BLOCK: {
CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd)
retval = parser.libdata().is_block ? 0 : 1;
retval = parser.is_block() ? 0 : 1;
break;
}
case STATUS_IS_BREAKPOINT: {

View File

@ -1350,7 +1350,7 @@ end_execution_reason_t parse_execution_context_t::run_1_job(const ast::job_t &jo
job_t::properties_t props{};
props.initial_background = job_node.bg.has_value();
props.skip_notification =
ld.is_subshell || ld.is_block || ld.is_event || !parser->is_interactive();
ld.is_subshell || parser->is_block() || ld.is_event || !parser->is_interactive();
props.from_event_handler = ld.is_event;
props.job_control = wants_job_control;
props.wants_timing = job_node_wants_timing(job_node);

View File

@ -136,11 +136,6 @@ block_t *parser_t::push_block(block_t &&block) {
new_current.src_filename = intern(filename);
}
// Types top and subst are not considered blocks for the purposes of `status is-block`.
if (type != block_type_t::top && type != block_type_t::subst) {
libdata().is_block = true;
}
if (type == block_type_t::breakpoint) {
libdata().is_breakpoint = true;
}
@ -167,16 +162,6 @@ void parser_t::pop_block(const block_t *expected) {
if (old.wants_pop_env) vars().pop();
// Figure out if `status is-block` should consider us to be in a block now.
bool new_is_block = false;
for (const auto &b : block_list) {
if (b.type() != block_type_t::top && b.type() != block_type_t::subst) {
new_is_block = true;
break;
}
}
libdata().is_block = new_is_block;
// Are we still in a breakpoint?
bool new_is_breakpoint = false;
for (const auto &b : block_list) {
@ -424,6 +409,16 @@ bool parser_t::is_function() const {
return false;
}
bool parser_t::is_block() const {
// Note historically this has descended into 'source', unlike 'is_function'.
for (const auto &b : block_list) {
if (b.type() != block_type_t::top && b.type() != block_type_t::subst) {
return true;
}
}
return false;
}
maybe_t<wcstring> parser_t::get_function_name(int level) {
if (level == 0) {
// Return the function name for the level preceding the most recent breakpoint. If there

View File

@ -169,9 +169,6 @@ struct library_data_t {
/// Whether we are running a subshell command.
bool is_subshell{false};
/// Whether we are running a block of commands.
bool is_block{false};
/// Whether we are running due to a `breakpoint` command.
bool is_breakpoint{false};
@ -340,6 +337,10 @@ class parser_t : public std::enable_shared_from_this<parser_t> {
/// Returns the current line number.
int get_lineno() const;
/// \return whether we are currently evaluating a "block" such as an if statement.
/// This supports 'status is-block'.
bool is_block() const;
/// Returns the block at the given index. 0 corresponds to the innermost block. Returns nullptr
/// when idx is at or equal to the number of blocks.
const block_t *block_at_index(size_t idx) const;