From 159d6d669a857bb03cd699f1cae843dc0b08e3f1 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 19 May 2019 14:44:17 -0700 Subject: [PATCH] Remove all block_t subclasses --- src/parse_execution.h | 2 +- src/parser.cpp | 46 ++++++++----------------------------------- src/parser.h | 45 +++--------------------------------------- 3 files changed, 12 insertions(+), 81 deletions(-) diff --git a/src/parse_execution.h b/src/parse_execution.h index 38abdbf67..4d4b8f725 100644 --- a/src/parse_execution.h +++ b/src/parse_execution.h @@ -10,8 +10,8 @@ #include "parse_tree.h" #include "proc.h" +class block_t; class parser_t; -struct block_t; enum parse_execution_result_t { /// The job was successfully executed (though it have failed on its own). diff --git a/src/parser.cpp b/src/parser.cpp index 99c3188df..5b65f64b4 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -354,9 +354,8 @@ void parser_t::stack_trace_internal(size_t block_idx, wcstring *buff) const { if (b->type() == EVENT) { // This is an event handler. - const event_block_t *eb = static_cast(b); - assert(eb->event && "Should have an event"); - wcstring description = event_get_desc(*eb->event); + assert(b->event && "Should have an event"); + wcstring description = event_get_desc(*b->event); 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. @@ -371,19 +370,17 @@ void parser_t::stack_trace_internal(size_t block_idx, wcstring *buff) const { // These types of blocks should be printed. switch (b->type()) { case SOURCE: { - const source_block_t *sb = static_cast(b); - const wchar_t *source_dest = sb->sourced_file; + const wchar_t *source_dest = b->sourced_file; append_format(*buff, _(L"from sourcing file %ls\n"), user_presentable_path(source_dest).c_str()); break; } case FUNCTION_CALL: case FUNCTION_CALL_NO_SHADOW: { - const function_block_t *fb = static_cast(b); - append_format(*buff, _(L"in function '%ls'"), fb->function_name.c_str()); + append_format(*buff, _(L"in function '%ls'"), b->function_name.c_str()); // Print arguments on the same line. 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' '); // We can't quote the arguments because we print this in quotes. // 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++) { const block_t *b = this->block_at_index(block_idx); if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) { - const function_block_t *fb = static_cast(b); - result = fb->function_name.c_str(); + result = b->function_name.c_str(); break; } else if (b->type() == SOURCE) { // 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++) { const block_t *b = this->block_at_index(i); if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) { - const function_block_t *fb = static_cast(b); - return function_get_definition_file(fb->function_name); + return function_get_definition_file(b->function_name); } else if (b->type() == SOURCE) { - const source_block_t *sb = static_cast(b); - return sb->sourced_file; + return b->sourced_file; } } @@ -866,27 +860,3 @@ block_t block_t::scope_block(block_type_t type) { return block_t(type); } 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) {} diff --git a/src/parser.h b/src/parser.h index 1998abf2d..d4ed98631 100644 --- a/src/parser.h +++ b/src/parser.h @@ -53,12 +53,10 @@ enum class loop_status_t { }; /// block_t represents a block of commands. -struct block_t { - protected: - /// Protected constructor. Use one of the subclasses below. +class block_t { + /// Construct from a block type. explicit block_t(block_type_t t); - private: /// Type of block. 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 breakpoint_block(); - /// Destructor - 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(); + ~block_t(); }; struct profile_item_t {