Skip to content

Commit

Permalink
refactor: Remove resource_location deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Jul 31, 2024
1 parent bad80a8 commit 19e29fa
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 67 deletions.
48 changes: 12 additions & 36 deletions parser_library/src/context/hlasm_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,17 +433,6 @@ const id_storage& hlasm_context::ids() const { return *ids_; }

std::shared_ptr<id_storage> hlasm_context::ids_ptr() { return ids_; }

const utils::resource::resource_location* hlasm_context::shared_resource_location(
const utils::resource::resource_location& l)
{
return std::to_address(m_resource_locations.emplace(l).first);
}
const utils::resource::resource_location* hlasm_context::shared_resource_location(
utils::resource::resource_location&& l)
{
return std::to_address(m_resource_locations.emplace(std::move(l)).first);
}

processing_stack_t hlasm_context::processing_stack()
{
auto result = m_stack_tree.root();
Expand All @@ -452,14 +441,14 @@ processing_stack_t hlasm_context::processing_stack()
{
result = m_stack_tree.step(result,
source.current_instruction.pos,
shared_resource_location(source.current_instruction.resource_loc),
source.current_instruction.resource_loc,
id_index(),
file_processing_type::OPENCODE);
for (const auto& member : source.copy_stack)
{
result = m_stack_tree.step(result,
member.current_statement_position(),
shared_resource_location(member.definition_location()->resource_loc),
member.definition_location()->resource_loc,
member.name(),
file_processing_type::COPY);
}
Expand All @@ -472,8 +461,7 @@ processing_stack_t hlasm_context::processing_stack()
for (auto type = file_processing_type::MACRO;
const auto& nest : scope_stack_[j].this_macro->get_current_copy_nest())
{
result = m_stack_tree.step(
result, nest.loc.pos, shared_resource_location(nest.loc.resource_loc), nest.member_name, type);
result = m_stack_tree.step(result, nest.loc.pos, nest.loc.resource_loc, nest.member_name, type);
type = file_processing_type::COPY;
}
}
Expand All @@ -490,14 +478,14 @@ processing_stack_details_t hlasm_context::processing_stack_details()
for (bool first = true; const auto& source : source_stack_)
{
res.emplace_back(source.current_instruction.pos,
shared_resource_location(source.current_instruction.resource_loc),
source.current_instruction.resource_loc,
scope_stack_.front(),
file_processing_type::OPENCODE,
id_index());
for (const auto& member : source.copy_stack)
{
res.emplace_back(member.current_statement_position(),
shared_resource_location(member.definition_location()->resource_loc),
member.definition_location()->resource_loc,
scope_stack_.front(),
file_processing_type::COPY,
member.name());
Expand All @@ -511,11 +499,7 @@ processing_stack_details_t hlasm_context::processing_stack_details()
for (auto type = file_processing_type::MACRO;
const auto& nest : scope_stack_[j].this_macro->get_current_copy_nest())
{
res.emplace_back(nest.loc.pos,
shared_resource_location(nest.loc.resource_loc),
scope_stack_[j],
type,
nest.member_name);
res.emplace_back(nest.loc.pos, nest.loc.resource_loc, scope_stack_[j], type, nest.member_name);
type = file_processing_type::COPY;
}
}
Expand All @@ -538,32 +522,24 @@ position hlasm_context::current_statement_position(bool consider_macros)
location hlasm_context::current_statement_location(bool consider_macros)
{
if (consider_macros && source_stack_.size() == 1 && scope_stack_.size() > 1)
{
const auto& [p, r] = scope_stack_.back().this_macro->get_current_copy_nest().back().loc;
return location(p, *shared_resource_location(r));
}
return scope_stack_.back().this_macro->get_current_copy_nest().back().loc;
else if (!source_stack_.back().copy_stack.empty())
{
const auto& member = source_stack_.back().copy_stack.back();
return location(
member.current_statement_position(), *shared_resource_location(member.definition_location()->resource_loc));
return location(member.current_statement_position(), member.definition_location()->resource_loc);
}
else
{
const auto& [p, r] = source_stack_.back().current_instruction;
return location(p, *shared_resource_location(r));
}
return source_stack_.back().current_instruction;
}

const utils::resource::resource_location& hlasm_context::current_statement_source(bool consider_macros)
{
if (consider_macros && source_stack_.size() == 1 && scope_stack_.size() > 1)
return *shared_resource_location(
scope_stack_.back().this_macro->get_current_copy_nest().back().loc.resource_loc);
return scope_stack_.back().this_macro->get_current_copy_nest().back().loc.resource_loc;
else if (source_stack_.back().copy_stack.size())
return *shared_resource_location(source_stack_.back().copy_stack.back().definition_location()->resource_loc);
return source_stack_.back().copy_stack.back().definition_location()->resource_loc;
else
return *shared_resource_location(source_stack_.back().current_instruction.resource_loc);
return source_stack_.back().current_instruction.resource_loc;
}

const std::deque<code_scope>& hlasm_context::scope_stack() const { return scope_stack_; }
Expand Down
3 changes: 0 additions & 3 deletions parser_library/src/context/hlasm_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ class hlasm_context
std::unordered_set<utils::resource::resource_location, utils::resource::resource_location_hasher>
m_resource_locations;

const utils::resource::resource_location* shared_resource_location(const utils::resource::resource_location&);
const utils::resource::resource_location* shared_resource_location(utils::resource::resource_location&&);

processing_frame_tree m_stack_tree;

std::string m_title_name;
Expand Down
6 changes: 3 additions & 3 deletions parser_library/src/context/source_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ source_snapshot source_context::create_snapshot() const
}

processing_frame_details::processing_frame_details(position pos,
const utils::resource::resource_location* resource_loc,
const utils::resource::resource_location& resource_loc,
const code_scope& scope,
file_processing_type proc_type,
id_index member)
: pos(pos)
, resource_loc(std::move(resource_loc))
, resource_loc(resource_loc)
, scope(scope)
, member_name(member)
, proc_type(std::move(proc_type))
Expand All @@ -56,7 +56,7 @@ processing_frame_tree::processing_frame_tree()

processing_frame_tree::node_pointer processing_frame_tree::step(node_pointer current,
position pos,
const utils::resource::resource_location* resource_loc,
const utils::resource::resource_location& resource_loc,
id_index member,
file_processing_type proc_type)
{
Expand Down
22 changes: 11 additions & 11 deletions parser_library/src/context/source_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ struct code_scope;

struct processing_frame
{
constexpr processing_frame(position pos,
const utils::resource::resource_location* resource_loc,
processing_frame(position pos,
const utils::resource::resource_location& resource_loc,
id_index member,
file_processing_type proc_type)
: pos(pos)
Expand All @@ -68,25 +68,25 @@ struct processing_frame
{}

position pos;
const utils::resource::resource_location* resource_loc;
utils::resource::resource_location resource_loc;
id_index member_name;
file_processing_type proc_type;

bool operator==(const processing_frame&) const = default;

location get_location() const { return location(pos, *resource_loc); }
location get_location() const { return location(pos, resource_loc); }
};

struct processing_frame_details
{
processing_frame_details(position pos,
const utils::resource::resource_location* resource_loc,
const utils::resource::resource_location& resource_loc,
const code_scope& scope,
file_processing_type proc_type,
id_index member);

position pos;
const utils::resource::resource_location* resource_loc;
utils::resource::resource_location resource_loc;
const code_scope& scope;
id_index member_name;
file_processing_type proc_type;
Expand All @@ -104,17 +104,17 @@ class processing_frame_tree

bool operator==(const processing_frame_node&) const = default;

constexpr processing_frame_node(const processing_frame_node* parent,
processing_frame_node(const processing_frame_node* parent,
position pos,
const utils::resource::resource_location* resource_loc,
const utils::resource::resource_location& resource_loc,
id_index member,
file_processing_type proc_type)
: m_parent(parent)
, frame(pos, resource_loc, member, proc_type)
{}
explicit constexpr processing_frame_node()
explicit processing_frame_node()
: m_parent(nullptr)
, frame({}, nullptr, id_index(), file_processing_type::NONE)
, frame({}, utils::resource::resource_location(), id_index(), file_processing_type::NONE)
{}
};

Expand Down Expand Up @@ -166,7 +166,7 @@ class processing_frame_tree

node_pointer step(node_pointer current,
position pos,
const utils::resource::resource_location* resource_loc,
const utils::resource::resource_location& resource_loc,
id_index member,
file_processing_type proc_type);
};
Expand Down
12 changes: 6 additions & 6 deletions parser_library/src/debugging/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class debugger::impl final : public processing::statement_analyzer, output_handl
// Specifies whether the debugger stops on the next statement call.
bool stop_on_next_stmt_ = false;
bool stop_on_stack_changes_ = false;
std::pair<context::processing_stack_t, const utils::resource::resource_location*> stop_on_stack_condition_;
std::pair<context::processing_stack_t, std::optional<utils::resource::resource_location>> stop_on_stack_condition_;

// True, if disconnect request was received
bool disconnected_ = false;
Expand Down Expand Up @@ -263,18 +263,18 @@ class debugger::impl final : public processing::statement_analyzer, output_handl
auto stack_node = ctx_->processing_stack();
auto stack = ctx_->processing_stack_details();

for (const auto& bp : breakpoints(*stack.back().resource_loc))
for (const auto& bp : breakpoints(stack.back().resource_loc))
{
if (bp.line >= stmt_range.start.line && bp.line <= stmt_range.end.line)
breakpoint_hit = true;
}

const auto stack_condition_violated = [cond = stop_on_stack_condition_](context::processing_stack_t cur) {
const auto stack_condition_violated = [&cond = stop_on_stack_condition_](context::processing_stack_t cur) {
auto last = cur;
for (cur = cur.parent(); !cur.empty(); last = cur, cur = cur.parent())
if (cur == cond.first)
break;
return cond.first != cur || (cond.second && cond.second != last.frame().resource_loc);
return cond.first != cur || (cond.second && *cond.second != last.frame().resource_loc);
};

// breakpoint check
Expand All @@ -291,7 +291,7 @@ class debugger::impl final : public processing::statement_analyzer, output_handl
return false;
stop_on_next_stmt_ = false;
stop_on_stack_changes_ = false;
stop_on_stack_condition_ = std::make_pair(stack_node, nullptr);
stop_on_stack_condition_ = std::make_pair(stack_node, std::nullopt);

continue_ = false;

Expand Down Expand Up @@ -366,7 +366,7 @@ class debugger::impl final : public processing::statement_analyzer, output_handl
return stack_frames_;
for (size_t i = proc_stack_.size() - 1; i != (size_t)-1; --i)
{
source source(proc_stack_[i].resource_loc->get_uri());
source source(proc_stack_[i].resource_loc.get_uri());
std::string name;
switch (proc_stack_[i].proc_type)
{
Expand Down
6 changes: 3 additions & 3 deletions parser_library/src/diagnostic_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ diagnostic add_stack_details(diagnostic_op d, context::processing_stack_t stack)
if (stack.empty())
return std::move(d).to_diagnostic();

auto diag = std::move(d).to_diagnostic(stack.frame().resource_loc->get_uri());
auto diag = std::move(d).to_diagnostic(stack.frame().resource_loc.get_uri());

for (stack = stack.parent(); !stack.empty(); stack = stack.parent())
{
const auto& f = stack.frame();
diag.related.emplace_back(range_uri(f.resource_loc->get_uri(), range(f.pos)),
std::format("While compiling {}({})", f.resource_loc->to_presentable(), f.pos.line + 1));
diag.related.emplace_back(range_uri(f.resource_loc.get_uri(), range(f.pos)),
std::format("While compiling {}({})", f.resource_loc.to_presentable(), f.pos.line + 1));
}

return diag;
Expand Down
8 changes: 4 additions & 4 deletions parser_library/src/lsp/lsp_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ std::vector<document_symbol_item> lsp_context::document_symbol(

for (auto s = stack; !s.empty(); s = s.parent())
{
if (*s.frame().resource_loc == dl)
if (s.frame().resource_loc == dl)
p = &s.frame().pos;
if (s.frame().proc_type == context::file_processing_type::MACRO)
p = nullptr;
Expand Down Expand Up @@ -612,21 +612,21 @@ location lsp_context::find_symbol_definition_location(
{
const auto& frame = stack.frame();

if (*frame.resource_loc == document_loc && frame.pos.line == pos.line)
if (frame.resource_loc == document_loc && frame.pos.line == pos.line)
{
top_reference = {};
break;
}

auto scope = find_occurrence_with_scope(*frame.resource_loc, position(frame.pos.line, 0));
auto scope = find_occurrence_with_scope(frame.resource_loc, position(frame.pos.line, 0));

if (scope.first && scope.first->kind == lsp::occurrence_kind::ORD && scope.first->name == sym.name())
top_reference = { scope, stack };

stack = stack.parent();
}
if (top_scope.first)
return location(top_scope.first->occurrence_range.start, *top_stack.frame().resource_loc);
return location(top_scope.first->occurrence_range.start, top_stack.frame().resource_loc);
else
return sym.symbol_location();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ void lsp_analyzer::collect_branch_info(
continue;

const auto loc = get_opencode_stackframe(stmt->location_stack);
if (loc.empty() && *loc.frame().resource_loc != opencode_loc)
if (loc.empty() && loc.frame().resource_loc != opencode_loc)
continue;

const auto& [target, condition] = *transfer;
Expand Down
13 changes: 13 additions & 0 deletions utils/include/utils/resource_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <atomic>
#include <compare>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -114,4 +115,16 @@ struct resource_location_hasher

} // namespace hlasm_plugin::utils::resource

namespace std {
template<>
class hash<::hlasm_plugin::utils::resource::resource_location>
{
public:
std::size_t operator()(const ::hlasm_plugin::utils::resource::resource_location& rl) const noexcept
{
return hlasm_plugin::utils::resource::resource_location_hasher()(rl);
}
};
} // namespace std

#endif

0 comments on commit 19e29fa

Please sign in to comment.