Skip to content

Commit

Permalink
refactor: Prune statement class hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Jun 28, 2024
1 parent 385b6b8 commit 52fa34a
Show file tree
Hide file tree
Showing 32 changed files with 315 additions and 348 deletions.
4 changes: 0 additions & 4 deletions parser_library/src/context/hlasm_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,3 @@ semantics::deferred_statement* hlasm_statement::access_deferred()
{
return kind == statement_kind::DEFERRED ? static_cast<semantics::deferred_statement*>(this) : nullptr;
}

hlasm_statement::hlasm_statement(const statement_kind kind)
: kind(kind)
{}
10 changes: 6 additions & 4 deletions parser_library/src/context/hlasm_statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ struct hlasm_statement
const semantics::deferred_statement* access_deferred() const;
semantics::deferred_statement* access_deferred();

virtual position statement_position() const = 0;
position statement_position() const { return stmt_range_ref().start; }

virtual const range& stmt_range_ref() const = 0;
virtual std::span<const diagnostic_op> diagnostics() const = 0;

virtual ~hlasm_statement() = default;

protected:
hlasm_statement(const statement_kind kind);
explicit hlasm_statement(const statement_kind kind)
: kind(kind)
{}
~hlasm_statement() = default;
};

using shared_stmt_ptr = std::shared_ptr<const hlasm_statement>;
Expand Down
18 changes: 9 additions & 9 deletions parser_library/src/context/literal_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,12 @@ void literal_pool::mentioned_in_ca_expr(std::shared_ptr<const expressions::data_
m_literals.try_emplace(literal_id { current_generation(), 0, std::move(dd) }, ca_only_literal());
}

class literal_pool::literal_postponed_statement : public context::postponed_statement,
public processing::resolved_statement
class literal_pool::literal_postponed_statement final : public context::postponed_statement,
public processing::resolved_statement
{
const literal_pool::literal_details* details;
semantics::operands_si op;
processing::op_code op_code;

static const processing::op_code op_code;
static const semantics::remarks_si empty_remarks;
static const semantics::label_si empty_label;
static const semantics::instruction_si empty_instr;
Expand All @@ -107,24 +106,25 @@ class literal_pool::literal_postponed_statement : public context::postponed_stat
public:
literal_postponed_statement(
const std::shared_ptr<const expressions::data_definition>& dd, const literal_pool::literal_details& details)
: details(&details)
: context::postponed_statement(details.stack, this)
, op(details.r, {})
, op_code(context::id_index("DC"), instruction_type::ASM, nullptr)
{
op.value.push_back(std::make_unique<semantics::data_def_operand_shared>(dd, details.r));
}
const processing_stack_t& location_stack() const override { return details->stack; }
const processing::resolved_statement* resolved_stmt() const override { return this; }

const range& stmt_range_ref() const override { return op.field_range; }
const processing::op_code& opcode_ref() const override { return op_code; }
processing::processing_format format_ref() const override { return dc_format; }
const semantics::operands_si& operands_ref() const override { return op; }
std::span<const semantics::literal_si> literals() const override { return {}; }
const semantics::remarks_si& remarks_ref() const override { return empty_remarks; }
const range& stmt_range_ref() const override { return details->r; }
const semantics::label_si& label_ref() const override { return empty_label; }
const semantics::instruction_si& instruction_ref() const override { return empty_instr; }
std::span<const diagnostic_op> diagnostics() const override { return {}; };
};

const processing::op_code literal_pool::literal_postponed_statement::op_code(
context::id_index("DC"), instruction_type::ASM, nullptr);
const semantics::remarks_si literal_pool::literal_postponed_statement::empty_remarks({}, {});
const semantics::label_si literal_pool::literal_postponed_statement::empty_label(range {});
const semantics::instruction_si literal_pool::literal_postponed_statement::empty_instr(range {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#ifndef CONTEXT_POSTPONED_STATEMENT_H
#define CONTEXT_POSTPONED_STATEMENT_H

#include <optional>

#include "context/source_context.h"

namespace hlasm_plugin::parser_library::processing {
Expand All @@ -29,9 +27,14 @@ namespace hlasm_plugin::parser_library::context {
// contains stack of file positions from where was it postponed
struct postponed_statement
{
virtual const processing_stack_t& location_stack() const = 0;
postponed_statement(processing_stack_t location_stack, const processing::resolved_statement* resolved_stmt)
: location_stack(location_stack)
, resolved_stmt(resolved_stmt)
{}

processing_stack_t location_stack;

virtual const processing::resolved_statement* resolved_stmt() const = 0;
const processing::resolved_statement* resolved_stmt;

virtual ~postponed_statement() = default;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ struct resolve_dependant_visitor

const auto add_diagnostic = [stmt, d = diag_consumer](auto f) {
if (stmt)
d->add_diagnostic(
add_stack_details(f(stmt->resolved_stmt()->stmt_range_ref()), stmt->location_stack()));
d->add_diagnostic(add_stack_details(f(stmt->resolved_stmt->stmt_range_ref()), stmt->location_stack));
else
d->add_diagnostic(add_stack_details(f(range()), {}));
};
Expand Down
13 changes: 3 additions & 10 deletions parser_library/src/context/statement_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@ statement_cache::statement_cache(shared_stmt_ptr base)
: base_stmt_(std::move(base))
{}

bool statement_cache::contains(processing::processing_status_cache_key key) const
const statement_cache::cached_statement_t& statement_cache::insert(
processing::processing_status_cache_key key, cached_statement_t statement)
{
for (const auto& entry : cache_)
if (entry.first == key)
return true;
return false;
}

void statement_cache::insert(processing::processing_status_cache_key key, cached_statement_t statement)
{
cache_.emplace_back(key, std::move(statement));
return cache_.emplace_back(key, std::move(statement)).second;
}

const statement_cache::cached_statement_t* statement_cache::get(processing::processing_status_cache_key key) const
Expand Down
10 changes: 4 additions & 6 deletions parser_library/src/context/statement_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include "hlasm_statement.h"
#include "processing/op_code.h"

namespace hlasm_plugin::parser_library::semantics {
struct complete_statement;
namespace hlasm_plugin::parser_library::processing {
struct statement_si_defer_done;
}

namespace hlasm_plugin::parser_library::context {
Expand All @@ -32,7 +32,7 @@ class statement_cache
public:
struct cached_statement_t
{
std::shared_ptr<semantics::complete_statement> stmt;
std::shared_ptr<processing::statement_si_defer_done> stmt;
std::vector<diagnostic_op> diags;
};
// pair of processing format and reparsed statement
Expand All @@ -46,9 +46,7 @@ class statement_cache
public:
statement_cache(shared_stmt_ptr base);

bool contains(processing::processing_status_cache_key key) const;

void insert(processing::processing_status_cache_key key, cached_statement_t statement);
const cached_statement_t& insert(processing::processing_status_cache_key key, cached_statement_t statement);

const cached_statement_t* get(processing::processing_status_cache_key key) const;

Expand Down
4 changes: 0 additions & 4 deletions parser_library/src/processing/error_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@

#include "error_statement.h"


namespace hlasm_plugin::parser_library::processing {


position error_statement::statement_position() const { return m_range.start; }

std::span<const diagnostic_op> error_statement::diagnostics() const
{
return { m_errors.data(), m_errors.data() + m_errors.size() };
Expand Down
4 changes: 2 additions & 2 deletions parser_library/src/processing/error_statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace hlasm_plugin::parser_library::processing {

class error_statement : public context::hlasm_statement
class error_statement final : public context::hlasm_statement
{
std::vector<diagnostic_op> m_errors;
range m_range;
Expand All @@ -32,7 +32,7 @@ class error_statement : public context::hlasm_statement
, m_range(r)
{}

position statement_position() const override;
const range& stmt_range_ref() const override { return m_range; }

std::span<const diagnostic_op> diagnostics() const override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ void asm_processor::process(std::shared_ptr<const processing::resolved_statement
}
}
std::optional<asm_processor::extract_copy_id_result> asm_processor::extract_copy_id(
const semantics::complete_statement& stmt, diagnosable_ctx* diagnoser)
const processing::resolved_statement& stmt, diagnosable_ctx* diagnoser)
{
if (stmt.operands_ref().value.size() != 1 || !stmt.operands_ref().value.front()->access_asm()
|| !stmt.operands_ref().value.front()->access_asm()->access_expr())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class asm_processor : public low_language_processor
range statement;
};
static std::optional<extract_copy_id_result> extract_copy_id(
const semantics::complete_statement& stmt, diagnosable_ctx* diagnoser);
const processing::resolved_statement& stmt, diagnosable_ctx* diagnoser);
static bool common_copy_postprocess(bool processed,
const extract_copy_id_result& data,
context::hlasm_context& hlasm_ctx,
Expand Down
Loading

0 comments on commit 52fa34a

Please sign in to comment.