Remove all block_t subclasses

This commit is contained in:
ridiculousfish 2019-05-19 14:44:17 -07:00
parent eff4873eca
commit 159d6d669a
3 changed files with 12 additions and 81 deletions

View file

@ -10,8 +10,8 @@
#include "parse_tree.h" #include "parse_tree.h"
#include "proc.h" #include "proc.h"
class block_t;
class parser_t; class parser_t;
struct block_t;
enum parse_execution_result_t { enum parse_execution_result_t {
/// The job was successfully executed (though it have failed on its own). /// The job was successfully executed (though it have failed on its own).

View file

@ -354,9 +354,8 @@ void parser_t::stack_trace_internal(size_t block_idx, wcstring *buff) const {
if (b->type() == EVENT) { if (b->type() == EVENT) {
// This is an event handler. // This is an event handler.
const event_block_t *eb = static_cast<const event_block_t *>(b); assert(b->event && "Should have an event");
assert(eb->event && "Should have an event"); wcstring description = event_get_desc(*b->event);
wcstring description = event_get_desc(*eb->event);
append_format(*buff, _(L"in event handler: %ls\n"), description.c_str()); append_format(*buff, _(L"in event handler: %ls\n"), description.c_str());
// Stop recursing at event handler. No reason to believe that any other code is relevant. // Stop recursing at event handler. No reason to believe that any other code is relevant.
@ -371,19 +370,17 @@ void parser_t::stack_trace_internal(size_t block_idx, wcstring *buff) const {
// These types of blocks should be printed. // These types of blocks should be printed.
switch (b->type()) { switch (b->type()) {
case SOURCE: { case SOURCE: {
const source_block_t *sb = static_cast<const source_block_t *>(b); const wchar_t *source_dest = b->sourced_file;
const wchar_t *source_dest = sb->sourced_file;
append_format(*buff, _(L"from sourcing file %ls\n"), append_format(*buff, _(L"from sourcing file %ls\n"),
user_presentable_path(source_dest).c_str()); user_presentable_path(source_dest).c_str());
break; break;
} }
case FUNCTION_CALL: case FUNCTION_CALL:
case FUNCTION_CALL_NO_SHADOW: { case FUNCTION_CALL_NO_SHADOW: {
const function_block_t *fb = static_cast<const function_block_t *>(b); append_format(*buff, _(L"in function '%ls'"), b->function_name.c_str());
append_format(*buff, _(L"in function '%ls'"), fb->function_name.c_str());
// Print arguments on the same line. // Print arguments on the same line.
wcstring args_str; wcstring args_str;
for (const wcstring &arg : fb->function_args) { for (const wcstring &arg : b->function_args) {
if (!args_str.empty()) args_str.push_back(L' '); if (!args_str.empty()) args_str.push_back(L' ');
// We can't quote the arguments because we print this in quotes. // We can't quote the arguments because we print this in quotes.
// As a special-case, add the empty argument as "". // As a special-case, add the empty argument as "".
@ -439,8 +436,7 @@ const wchar_t *parser_t::is_function(size_t idx) const {
for (size_t block_idx = idx; block_idx < this->block_count(); block_idx++) { for (size_t block_idx = idx; block_idx < this->block_count(); block_idx++) {
const block_t *b = this->block_at_index(block_idx); const block_t *b = this->block_at_index(block_idx);
if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) { if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) {
const function_block_t *fb = static_cast<const function_block_t *>(b); result = b->function_name.c_str();
result = fb->function_name.c_str();
break; break;
} else if (b->type() == SOURCE) { } else if (b->type() == SOURCE) {
// If a function sources a file, obviously that function's offset doesn't contribute. // If a function sources a file, obviously that function's offset doesn't contribute.
@ -502,11 +498,9 @@ const wchar_t *parser_t::current_filename() const {
for (size_t i = 0; i < this->block_count(); i++) { for (size_t i = 0; i < this->block_count(); i++) {
const block_t *b = this->block_at_index(i); const block_t *b = this->block_at_index(i);
if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) { if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) {
const function_block_t *fb = static_cast<const function_block_t *>(b); return function_get_definition_file(b->function_name);
return function_get_definition_file(fb->function_name);
} else if (b->type() == SOURCE) { } else if (b->type() == SOURCE) {
const source_block_t *sb = static_cast<const source_block_t *>(b); return b->sourced_file;
return sb->sourced_file;
} }
} }
@ -866,27 +860,3 @@ block_t block_t::scope_block(block_type_t type) {
return block_t(type); return block_t(type);
} }
block_t block_t::breakpoint_block() { return block_t(BREAKPOINT); } block_t block_t::breakpoint_block() { return block_t(BREAKPOINT); }
if_block_t::if_block_t() : block_t(IF) {}
event_block_t::event_block_t(const event_t &evt) : block_t(EVENT) { this->event = evt; }
function_block_t::function_block_t(wcstring name, wcstring_list_t args, bool shadows)
: block_t(shadows ? FUNCTION_CALL : FUNCTION_CALL_NO_SHADOW) {
this->function_name = std::move(name);
this->function_args = std::move(args);
}
source_block_t::source_block_t(const wchar_t *src) : block_t(SOURCE) { this->sourced_file = src; }
for_block_t::for_block_t() : block_t(FOR) {}
while_block_t::while_block_t() : block_t(WHILE) {}
switch_block_t::switch_block_t() : block_t(SWITCH) {}
scope_block_t::scope_block_t(block_type_t type) : block_t(type) {
assert(type == BEGIN || type == TOP || type == SUBST);
}
breakpoint_block_t::breakpoint_block_t() : block_t(BREAKPOINT) {}

View file

@ -53,12 +53,10 @@ enum class loop_status_t {
}; };
/// block_t represents a block of commands. /// block_t represents a block of commands.
struct block_t { class block_t {
protected: /// Construct from a block type.
/// Protected constructor. Use one of the subclasses below.
explicit block_t(block_type_t t); explicit block_t(block_type_t t);
private:
/// Type of block. /// Type of block.
const block_type_t block_type; const block_type_t block_type;
@ -103,44 +101,7 @@ struct block_t {
static block_t scope_block(block_type_t type); static block_t scope_block(block_type_t type);
static block_t breakpoint_block(); static block_t breakpoint_block();
/// Destructor ~block_t();
virtual ~block_t();
};
struct if_block_t : public block_t {
if_block_t();
};
struct event_block_t : public block_t {
explicit event_block_t(const event_t &evt);
};
struct function_block_t : public block_t {
function_block_t(wcstring name, wcstring_list_t args, bool shadows);
};
struct source_block_t : public block_t {
explicit source_block_t(const wchar_t *src);
};
struct for_block_t : public block_t {
for_block_t();
};
struct while_block_t : public block_t {
while_block_t();
};
struct switch_block_t : public block_t {
switch_block_t();
};
struct scope_block_t : public block_t {
explicit scope_block_t(block_type_t type); // must be BEGIN, TOP or SUBST
};
struct breakpoint_block_t : public block_t {
breakpoint_block_t();
}; };
struct profile_item_t { struct profile_item_t {