Skip to content

Commit

Permalink
refactor: Remove unused owning shared pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Dec 15, 2023
1 parent 8c85623 commit 3303f23
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 42 deletions.
8 changes: 2 additions & 6 deletions parser_library/src/context/code_scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,10 @@ struct code_scope

bool is_in_macro() const { return !!this_macro; }

code_scope(std::unique_ptr<macro_invocation> macro_invo, macro_def_ptr macro_def)
explicit code_scope(std::unique_ptr<macro_invocation> macro_invo)
: this_macro(std::move(macro_invo))
, this_macro_def_(std::move(macro_def))
{}
code_scope() = default;

private:
macro_def_ptr this_macro_def_;
explicit code_scope() = default;
};

} // namespace hlasm_plugin::parser_library::context
Expand Down
25 changes: 10 additions & 15 deletions parser_library/src/context/hlasm_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ void hlasm_context::remove_mnemonic(id_index mnemo)
if (auto it = external_macros_.find(mnemo); it == external_macros_.end())
opcode_mnemo_[mnemo].emplace_back(opcode_t(), ++m_current_opcode_generation);
else // restore external macro when available
opcode_mnemo_[mnemo].emplace_back(opcode_t { it->first, it->second }, ++m_current_opcode_generation);
opcode_mnemo_[mnemo].emplace_back(opcode_t { it->first, it->second.get() }, ++m_current_opcode_generation);
}

opcode_t hlasm_context::get_operation_code(id_index symbol, opcode_generation gen) const
Expand Down Expand Up @@ -789,12 +789,8 @@ struct opcode_attr_visitor
std::string operator()(const ca_instruction*) const { return "A"; }
std::string operator()(const mnemonic_code*) const { return "E"; }
std::string operator()(const machine_instruction*) const { return "O"; }
std::string operator()(const macro_def_ptr&) const { return "M"; }
template<typename T>
std::string operator()(const T&) const
{
return "U";
}
std::string operator()(macro_definition*) const { return "M"; }
std::string operator()(std::monostate) const { return "U"; }
};

C_t hlasm_context::get_opcode_attr(id_index symbol, opcode_generation gen) const
Expand Down Expand Up @@ -832,7 +828,7 @@ void hlasm_context::add_macro(macro_def_ptr macro, bool external)
{
auto next_gen = ++m_current_opcode_generation;
const auto& m = macros_[macro->id].emplace_back(std::move(macro), next_gen).first;
opcode_mnemo_[m->id].emplace_back(opcode_t { m->id, m }, next_gen);
opcode_mnemo_[m->id].emplace_back(opcode_t { m->id, m.get() }, next_gen);
if (external)
external_macros_.try_emplace(m->id, m);
};
Expand All @@ -852,13 +848,12 @@ const macro_def_ptr* hlasm_context::find_macro(id_index name, opcode_generation

const hlasm_context::opcode_map& hlasm_context::opcode_mnemo_storage() const { return opcode_mnemo_; }

macro_def_ptr hlasm_context::get_macro_definition(id_index name, context::opcode_generation gen) const
context::macro_definition* hlasm_context::get_macro_definition(id_index name, context::opcode_generation gen) const
{
if (auto mnem = find_opcode_mnemo(name, gen);
mnem && std::holds_alternative<context::macro_def_ptr>(mnem->opcode_detail))
return std::get<context::macro_def_ptr>(mnem->opcode_detail);
if (auto mnem = find_opcode_mnemo(name, gen); mnem && mnem->is_macro())
return mnem->get_macro_details();

return macro_def_ptr();
return nullptr;
}

bool hlasm_context::is_in_macro() const { return scope_stack_.back().is_in_macro(); }
Expand All @@ -868,7 +863,7 @@ std::pair<const macro_invocation*, bool> hlasm_context::enter_macro(
{
assert(SYSNDX_ <= SYSNDX_limit);

macro_def_ptr macro_def = get_macro_definition(name);
auto* macro_def = get_macro_definition(name);
assert(macro_def);

if (label_param_data)
Expand All @@ -881,7 +876,7 @@ std::pair<const macro_invocation*, bool> hlasm_context::enter_macro(
macro_def->call(std::move(label_param_data), std::move(params), id_storage::well_known::SYSLIST);
auto* const result = invo.get();

auto& new_scope = scope_stack_.emplace_back(std::move(invo), macro_def);
auto& new_scope = scope_stack_.emplace_back(std::move(invo));
add_system_vars_to_scope(new_scope);
add_global_system_vars(new_scope);

Expand Down
2 changes: 1 addition & 1 deletion parser_library/src/context/hlasm_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class hlasm_context
// gets macro storage
const macro_storage& macros() const;
const macro_def_ptr* find_macro(id_index name, opcode_generation gen = opcode_generation::current) const;
macro_def_ptr get_macro_definition(id_index name, opcode_generation gen = opcode_generation::current) const;
macro_definition* get_macro_definition(id_index name, opcode_generation gen = opcode_generation::current) const;
// checks whether processing is currently in macro
bool is_in_macro() const;
// returns macro we are currently in or empty shared_ptr if in open code
Expand Down
16 changes: 13 additions & 3 deletions parser_library/src/context/operation_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

#include <variant>

#include "id_storage.h"
#include "macro.h"
#include "id_index.h"

namespace hlasm_plugin::parser_library::context {
class ca_instruction;
class assembler_instruction;
class machine_instruction;
class mnemonic_code;
class macro_definition;


// structure that represents operation code of an instruction
struct opcode_t
Expand All @@ -34,12 +35,21 @@ struct opcode_t
const assembler_instruction*,
const machine_instruction*,
const mnemonic_code*,
macro_def_ptr>;
macro_definition*>;

id_index opcode;
opcode_variant opcode_detail;

explicit operator bool() const { return !std::holds_alternative<std::monostate>(opcode_detail); }

bool is_macro() const noexcept { return std::holds_alternative<macro_definition*>(opcode_detail); }
macro_definition* get_macro_details() const noexcept
{
if (std::holds_alternative<macro_definition*>(opcode_detail))
return std::get<macro_definition*>(opcode_detail);
else
return nullptr;
}
};

} // namespace hlasm_plugin::parser_library::context
Expand Down
10 changes: 4 additions & 6 deletions parser_library/src/lsp/lsp_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,10 +935,9 @@ std::optional<location> lsp_context::find_definition_location(const symbol_occur
case lsp::occurrence_kind::INSTR_LIKE: {
if (auto it = m_macros.find(occ.opcode); it != m_macros.end())
return it->second->definition_location;
if (auto op = m_hlasm_ctx->find_any_valid_opcode(occ.name);
op && std::holds_alternative<context::macro_def_ptr>(op->opcode_detail))
if (auto op = m_hlasm_ctx->find_any_valid_opcode(occ.name); op && op->is_macro())
{
return std::get<context::macro_def_ptr>(op->opcode_detail)->definition_location;
return op->get_macro_details()->definition_location;
}
if (auto it = m_instr_like.find(occ.name); it != m_instr_like.end())
return location(position(), it->second);
Expand Down Expand Up @@ -1056,9 +1055,8 @@ std::string lsp_context::find_hover(const symbol_occurrence& occ,
return hover_for_macro(*it->second);
if (auto op = m_hlasm_ctx->find_any_valid_opcode(occ.name))
{
if (std::holds_alternative<context::macro_def_ptr>(op->opcode_detail))
return prefix_using(
hover_for_macro(*m_macros.at(std::get<context::macro_def_ptr>(op->opcode_detail).get())));
if (op->is_macro())
return prefix_using(hover_for_macro(*m_macros.at(op->get_macro_details())));
else
return prefix_using(hover_for_instruction(op->opcode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,8 @@ void lsp_analyzer::collect_occurrence(const semantics::instruction_si& instructi
{
auto opcode = hlasm_ctx_.get_operation_code(
std::get<context::id_index>(instruction.value)); // TODO: collect the instruction at the right time
auto* macro_def = std::get_if<context::macro_def_ptr>(&opcode.opcode_detail);
if (!opcode.opcode.empty() || macro_def)
collector.occurrences.emplace_back(
opcode.opcode, macro_def ? macro_def->get() : nullptr, instruction.field_range);
if (!opcode.opcode.empty() || opcode.is_macro())
collector.occurrences.emplace_back(opcode.opcode, opcode.get_macro_details(), instruction.field_range);
}
else if (instruction.type == semantics::instruction_si_type::ORD
&& collector.collector_kind == lsp::occurrence_kind::INSTR_LIKE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ std::optional<processing_status> ordinary_processor::get_instruction_processing_
{
auto code = hlasm_ctx.get_operation_code(instruction);

if (std::holds_alternative<context::macro_def_ptr>(code.opcode_detail))
if (code.is_macro())
{
return std::make_pair(processing_format(processing_kind::ORDINARY, processing_form::MAC),
op_code(instruction, context::instruction_type::MAC));
Expand Down
9 changes: 4 additions & 5 deletions parser_library/src/workspaces/macro_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ std::vector<cached_opsyn_mnemo> macro_cache_key::get_opsyn_state(context::hlasm_
// If there is an opsyn, that aliases an instruction to be CA instruction, add it to result
if (context::instruction_resolved_during_macro_parsing(opcode.opcode)
|| context::instruction_resolved_during_macro_parsing(from))
result.push_back(
{ from, opcode.opcode, std::holds_alternative<context::macro_def_ptr>(opcode.opcode_detail) });
result.push_back({ from, opcode.opcode, opcode.is_macro() });
}

sort_opsyn_state(result);
Expand Down Expand Up @@ -118,11 +117,11 @@ std::optional<std::vector<std::shared_ptr<file>>> macro_cache::load_from_cache(
return result;
}

version_stamp macro_cache::get_copy_member_versions(context::macro_def_ptr macro) const
version_stamp macro_cache::get_copy_member_versions(context::macro_definition& macro) const
{
version_stamp result;

for (const auto& copy_ptr : macro->used_copy_members)
for (const auto& copy_ptr : macro.used_copy_members)
{
auto file = file_mngr_->find(copy_ptr->definition_location.resource_loc);
if (!file)
Expand All @@ -140,7 +139,7 @@ void macro_cache::save_macro(const macro_cache_key& key, const analyzer& analyze
// Add stamps for all macro dependencies
auto parsed_macro = analyzer.context().hlasm_ctx->get_macro_definition(key.data.library_member);
if (parsed_macro)
cache_data.stamps = get_copy_member_versions(std::move(parsed_macro));
cache_data.stamps = get_copy_member_versions(*parsed_macro);
else
cache_data.stamps.clear();
}
Expand Down
2 changes: 1 addition & 1 deletion parser_library/src/workspaces/macro_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class macro_cache final

private:
[[nodiscard]] const macro_cache_data* find_cached_data(const macro_cache_key& key) const;
[[nodiscard]] version_stamp get_copy_member_versions(context::macro_def_ptr ctx) const;
[[nodiscard]] version_stamp get_copy_member_versions(context::macro_definition& ctx) const;
};

} // namespace hlasm_plugin::parser_library::workspaces
Expand Down

0 comments on commit 3303f23

Please sign in to comment.