Skip to content

Commit

Permalink
refactor: Simpler structure for lookahead set
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Aug 17, 2023
1 parent 5f75430 commit 9d50f09
Show file tree
Hide file tree
Showing 40 changed files with 131 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace hlasm_plugin::parser_library::expressions {
class ca_expr_visitor;
class ca_expression;
using ca_expr_ptr = std::unique_ptr<ca_expression>;
using undef_sym_set = std::set<context::id_index>;

struct evaluation_context;

Expand Down Expand Up @@ -60,7 +59,8 @@ class ca_expression
ca_expression(context::SET_t_enum expr_kind, range expr_range);

// retrieves set of attributed symbols that are not yet defined
virtual bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const = 0;
virtual bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const = 0;

// builds parts of the expression tree that could not be built during parsing
virtual void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ca_binary_operator::ca_binary_operator(
{}

bool ca_binary_operator::get_undefined_attributed_symbols(
undef_sym_set& symbols, const evaluation_context& eval_ctx) const
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const
{
bool result = false;
result |= left_expr->get_undefined_attributed_symbols(symbols, eval_ctx);
Expand Down Expand Up @@ -79,7 +79,7 @@ ca_function_binary_operator::ca_function_binary_operator(ca_expr_ptr left_expr,
{}

// Detects (T'&VAR EQ 'O') condition
std::optional<bool> t_attr_special_case(undef_sym_set& symbols,
std::optional<bool> t_attr_special_case(std::vector<context::id_index>& symbols,
const expressions::ca_expression* left,
const expressions::ca_expression* right,
const evaluation_context& eval_ctx)
Expand Down Expand Up @@ -116,7 +116,7 @@ std::optional<bool> t_attr_special_case(undef_sym_set& symbols,
}

bool ca_function_binary_operator::get_undefined_attributed_symbols(
undef_sym_set& symbols, const evaluation_context& eval_ctx) const
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const
{
if (is_relational() && m_expr_ctx.parent_expr_kind == context::SET_t_enum::B_TYPE)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class ca_binary_operator : public ca_expression

ca_binary_operator(ca_expr_ptr left_expr, ca_expr_ptr right_expr, context::SET_t_enum expr_kind, range expr_range);

bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const override;
bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const override;

void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) override;

Expand Down Expand Up @@ -69,7 +70,8 @@ class ca_function_binary_operator final : public ca_binary_operator
range expr_range,
context::SET_t_enum parent_expr_kind = context::SET_t_enum::UNDEF_TYPE);

bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const override;
bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const override;

void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ca_unary_operator::ca_unary_operator(ca_expr_ptr expr, context::SET_t_enum expr_
{}

bool ca_unary_operator::get_undefined_attributed_symbols(
undef_sym_set& symbols, const evaluation_context& eval_ctx) const
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const
{
return expr->get_undefined_attributed_symbols(symbols, eval_ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class ca_unary_operator : public ca_expression

ca_unary_operator(ca_expr_ptr expr, context::SET_t_enum expr_kind, range expr_range);

bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const override;
bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const override;

void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ ca_constant::ca_constant(context::A_t value, range expr_range)
, value(value)
{}

bool ca_constant::get_undefined_attributed_symbols(undef_sym_set&, const evaluation_context&) const { return false; }
bool ca_constant::get_undefined_attributed_symbols(std::vector<context::id_index>&, const evaluation_context&) const
{
return false;
}

void ca_constant::resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class ca_constant final : public ca_expression

ca_constant(context::A_t value, range expr_range);

bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const override;
bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const override;

void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ ca_expr_list::ca_expr_list(std::vector<ca_expr_ptr> expr_list, range expr_range,
, parenthesized(parenthesized)
{}

bool ca_expr_list::get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const
bool ca_expr_list::get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const
{
bool result = false;
for (auto&& expr : expr_list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class ca_expr_list final : public ca_expression
public:
ca_expr_list(std::vector<ca_expr_ptr> expr_list, range expr_range, bool parenthesized);

bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const override;
bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const override;

void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ ca_function::ca_function(context::id_index function_name,
, duplication_factor(std::move(duplication_factor))
{}

bool ca_function::get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const
bool ca_function::get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const
{
bool result = false;
for (auto&& expr : parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class ca_function final : public ca_expression
ca_expr_ptr duplication_factor,
range expr_range);

bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const override;
bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const override;

void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ ca_string::ca_string(
, substring(std::move(substring))
{}

bool ca_string::get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const
bool ca_string::get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const
{
bool result = false;
if (duplication_factor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class ca_string final : public ca_expression

ca_string(semantics::concat_chain value, ca_expr_ptr duplication_factor, substring_t substring, range expr_range);

bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const override;
bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const override;

void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ ca_symbol::ca_symbol(context::id_index symbol, range expr_range)
, symbol(symbol)
{}

bool ca_symbol::get_undefined_attributed_symbols(undef_sym_set&, const evaluation_context&) const { return false; }
bool ca_symbol::get_undefined_attributed_symbols(std::vector<context::id_index>&, const evaluation_context&) const
{
return false;
}

void ca_symbol::resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class ca_symbol final : public ca_expression

ca_symbol(context::id_index symbol, range expr_range);

bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const override;
bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const override;

void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
#include "ca_var_sym.h"
#include "context/hlasm_context.h"
#include "context/literal_pool.h"
#include "context/ordinary_assembly/dependable.h"
#include "context/ordinary_assembly/ordinary_assembly_dependency_solver.h"
#include "diagnostic_consumer.h"
#include "ebcdic_encoding.h"
#include "expressions/conditional_assembly/ca_expr_visitor.h"
#include "expressions/evaluation_context.h"
#include "lexing/lexer.h"
#include "lexing/token_stream.h"
#include "parsing/parser_impl.h"
#include "processing/op_code.h"
#include "semantics/range_provider.h"
Expand Down Expand Up @@ -78,15 +76,15 @@ ca_symbol_attribute::ca_symbol_attribute(
{}

bool ca_symbol_attribute::get_undefined_attributed_symbols(
undef_sym_set& symbols, const evaluation_context& eval_ctx) const
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const
{
if (std::holds_alternative<context::id_index>(symbol))
{
if (context::symbol_attributes::is_ordinary_attribute(attribute)
&& !eval_ctx.hlasm_ctx.ord_ctx.get_symbol(std::get<context::id_index>(symbol))
&& !eval_ctx.hlasm_ctx.ord_ctx.get_symbol_reference(std::get<context::id_index>(symbol)))
{
symbols.emplace(std::get<context::id_index>(symbol));
symbols.emplace_back(std::get<context::id_index>(symbol));
return true;
}
}
Expand All @@ -113,7 +111,7 @@ bool ca_symbol_attribute::get_undefined_attributed_symbols(
&& !eval_ctx.hlasm_ctx.ord_ctx.get_symbol(ord_name)
&& !eval_ctx.hlasm_ctx.ord_ctx.get_symbol_reference(ord_name))
{
symbols.emplace(ord_name);
symbols.emplace_back(ord_name);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class ca_symbol_attribute final : public ca_expression
ca_symbol_attribute(
semantics::literal_si lit, context::data_attr_kind attribute, range expr_range, range symbol_range);

bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const override;
bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const override;

void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ca_var_sym::ca_var_sym(semantics::vs_ptr symbol, range expr_range)
{}

bool ca_var_sym::get_undefined_attributed_symbols_vs(
undef_sym_set& symbols, const semantics::vs_ptr& symbol, const evaluation_context& eval_ctx)
std::vector<context::id_index>& symbols, const semantics::vs_ptr& symbol, const evaluation_context& eval_ctx)
{
bool result = false;

Expand All @@ -44,7 +44,8 @@ bool ca_var_sym::get_undefined_attributed_symbols_vs(
return result;
}

bool ca_var_sym::get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const
bool ca_var_sym::get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const
{
return get_undefined_attributed_symbols_vs(symbols, symbol, eval_ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class ca_var_sym final : public ca_expression

ca_var_sym(semantics::vs_ptr symbol, range expr_range);

bool get_undefined_attributed_symbols(undef_sym_set& symbols, const evaluation_context& eval_ctx) const override;
bool get_undefined_attributed_symbols(
std::vector<context::id_index>& symbols, const evaluation_context& eval_ctx) const override;

void resolve_expression_tree(ca_expression_ctx expr_ctx, diagnostic_op_consumer& diags) override;

Expand All @@ -39,7 +40,7 @@ class ca_var_sym final : public ca_expression
context::SET_t evaluate(const evaluation_context& eval_ctx) const override;

static bool get_undefined_attributed_symbols_vs(
undef_sym_set& symbols, const semantics::vs_ptr& symbol, const evaluation_context& eval_ctx);
std::vector<context::id_index>& symbols, const semantics::vs_ptr& symbol, const evaluation_context& eval_ctx);

private:
context::SET_t convert_return_types(
Expand Down
6 changes: 4 additions & 2 deletions parser_library/src/processing/opencode_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@ std::shared_ptr<const context::hlasm_statement> opencode_provider::process_ordin
if (proc.kind == processing_kind::ORDINARY
&& try_trigger_attribute_lookahead(*result,
{ *m_ctx->hlasm_ctx, library_info_transitional(*m_lib_provider), drop_diagnostic_op },
*m_state_listener))
*m_state_listener,
std::move(lookahead_references)))
return nullptr;

if (m_current_logical_line.segments.size() > 1)
Expand Down Expand Up @@ -725,7 +726,8 @@ context::shared_stmt_ptr opencode_provider::get_next(const statement_processor&
if (proc.kind == processing_kind::ORDINARY
&& try_trigger_attribute_lookahead(collector.current_instruction(),
{ *m_ctx->hlasm_ctx, library_info_transitional(*m_lib_provider), drop_diagnostic_op },
*m_state_listener))
*m_state_listener,
std::move(lookahead_references)))
return nullptr;

const auto& current_instr = collector.current_instruction();
Expand Down
2 changes: 2 additions & 0 deletions parser_library/src/processing/opencode_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class opencode_provider final : public statement_provider

encoding_warning_issued m_encoding_warning_issued = {};

std::vector<context::id_index> lookahead_references;

public:
// rewinds position in file
void rewind_input(context::source_position pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef PROCESSING_LOOKAHEAD_PROCESSING_INFO_H
#define PROCESSING_LOOKAHEAD_PROCESSING_INFO_H

#include <set>
#include <vector>

#include "context/ordinary_assembly/symbol.h"
#include "context/source_snapshot.h"
Expand Down Expand Up @@ -51,9 +51,9 @@ struct lookahead_start_data
{}

// ORD action
std::set<context::id_index> targets;
std::vector<context::id_index> targets;

lookahead_start_data(std::set<context::id_index> targets,
lookahead_start_data(std::vector<context::id_index> targets,
context::source_position statement_position,
context::source_snapshot snapshot)
: action(lookahead_action::ORD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,11 @@ void lookahead_processor::find_ord(const resolved_statement& statement)
if (!valid)
return;

to_find_.erase(id);
if (auto it = std::find(to_find_.begin(), to_find_.end(), id); it != to_find_.end())
{
std::swap(*it, to_find_.back());
to_find_.pop_back();
}

// find attributes
// if found ord symbol on CA, macro or undefined instruction, only type attribute is assigned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class lookahead_processor final : public statement_processor
processing_state_listener& listener_;
workspaces::parse_lib_provider& lib_provider_;

std::set<context::id_index> to_find_;
std::vector<context::id_index> to_find_;
context::id_index target_;

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ context::shared_stmt_ptr members_statement_provider::get_next(const statement_pr
{
if (try_trigger_attribute_lookahead(*instr,
{ *m_ctx.hlasm_ctx, library_info_transitional(m_lib_provider), drop_diagnostic_op },
m_listener))
m_listener,
std::move(lookahead_references)))
return nullptr;
}
}
Expand Down Expand Up @@ -83,8 +84,10 @@ context::shared_stmt_ptr members_statement_provider::get_next(const statement_pr
}

if (processor.kind == processing_kind::ORDINARY
&& try_trigger_attribute_lookahead(
*stmt, { *m_ctx.hlasm_ctx, library_info_transitional(m_lib_provider), drop_diagnostic_op }, m_listener))
&& try_trigger_attribute_lookahead(*stmt,
{ *m_ctx.hlasm_ctx, library_info_transitional(m_lib_provider), drop_diagnostic_op },
m_listener,
std::move(lookahead_references)))
return nullptr;

return stmt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace hlasm_plugin::parser_library::processing {
// common class for copy and macro statement providers (provider of copy and macro members)
class members_statement_provider : public statement_provider
{
std::vector<context::id_index> lookahead_references;

public:
members_statement_provider(const statement_provider_kind kind,
analyzing_context ctx,
Expand Down
Loading

0 comments on commit 9d50f09

Please sign in to comment.