Skip to content

Commit

Permalink
refactor: Strong type for statement index
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Oct 10, 2023
1 parent 8298546 commit 9df8c37
Show file tree
Hide file tree
Showing 21 changed files with 194 additions and 117 deletions.
9 changes: 5 additions & 4 deletions parser_library/src/context/copy_member.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#ifndef CONTEXT_COPY_MEMBER_H
#define CONTEXT_COPY_MEMBER_H

#include "id_storage.h"
#include "id_index.h"
#include "location.h"
#include "statement_cache.h"
#include "statement_id.h"

namespace hlasm_plugin::parser_library::context {

Expand Down Expand Up @@ -46,7 +47,7 @@ using copy_member_ptr = std::shared_ptr<copy_member>;
struct copy_member_invocation
{
copy_member_ptr copy_member_definition;
size_t current_statement = (size_t)-1;
statement_id current_statement;

static constexpr size_t not_suspended = (size_t)-1;
size_t suspended_at = not_suspended;
Expand All @@ -61,8 +62,8 @@ struct copy_member_invocation

position current_statement_position() const
{
if (current_statement != (size_t)-1)
return cached_definition()->at(current_statement).get_base()->statement_position();
if (current_statement != statement_id())
return cached_definition()->at(current_statement.value).get_base()->statement_position();
else
return {};
}
Expand Down
28 changes: 9 additions & 19 deletions parser_library/src/context/hlasm_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ std::pair<source_position, source_snapshot> hlasm_context::get_begin_snapshot(bo

context::source_snapshot snapshot = current_source().create_snapshot();

if (snapshot.copy_frames.size() && is_in_macros)
++snapshot.copy_frames.back().statement_offset;
if (!snapshot.copy_frames.empty() && is_in_macros)
++snapshot.copy_frames.back().statement_offset.value;

return std::make_pair(std::move(statement_position), std::move(snapshot));
}
Expand All @@ -416,8 +416,8 @@ std::pair<source_position, source_snapshot> hlasm_context::get_end_snapshot() co

context::source_snapshot snapshot = current_source().create_snapshot();

if (snapshot.copy_frames.size())
++snapshot.copy_frames.back().statement_offset;
if (!snapshot.copy_frames.empty())
++snapshot.copy_frames.back().statement_offset.value;

return std::make_pair(std::move(statement_position), std::move(snapshot));
}
Expand Down Expand Up @@ -485,9 +485,7 @@ processing_stack_t hlasm_context::processing_stack()
first = false;
for (size_t j = 1; j < scope_stack_.size(); ++j)
{
auto offs = scope_stack_[j].this_macro->current_statement;

for (const auto& nest : scope_stack_[j].this_macro->copy_nests[offs])
for (const auto& nest : scope_stack_[j].this_macro->get_current_copy_nest())
result = m_stack_tree.step(
processing_frame(
nest.loc.pos, shared_resource_location(nest.loc.resource_loc), nest.member_name),
Expand All @@ -508,9 +506,7 @@ processing_frame hlasm_context::processing_stack_top(bool consider_macros)

if (consider_macros && source_stack_.size() == 1 && scope_stack_.size() > 1)
{
const auto& last_macro = scope_stack_.back().this_macro;

if (const auto& nest = last_macro->copy_nests[last_macro->current_statement]; !nest.empty())
if (const auto& nest = scope_stack_.back().this_macro->get_current_copy_nest(); !nest.empty())
{
const auto& last_copy = nest.back();
return processing_frame(
Expand Down Expand Up @@ -557,10 +553,8 @@ processing_stack_details_t hlasm_context::processing_stack_details()
first = false;
for (size_t j = 1; j < scope_stack_.size(); ++j)
{
auto offs = scope_stack_[j].this_macro->current_statement;

for (auto type = file_processing_type::MACRO;
const auto& nest : scope_stack_[j].this_macro->copy_nests[offs])
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),
Expand Down Expand Up @@ -591,9 +585,7 @@ location hlasm_context::current_statement_location(bool consider_macros) const
}
else
{
const auto& mac_invo = scope_stack_.back().this_macro;

return mac_invo->copy_nests[mac_invo->current_statement].back().loc;
return scope_stack_.back().this_macro->get_current_copy_nest().back().loc;
}
}

Expand All @@ -608,9 +600,7 @@ const utils::resource::resource_location& hlasm_context::current_statement_sourc
}
else
{
const auto& mac_invo = scope_stack_.back().this_macro;

return mac_invo->copy_nests[mac_invo->current_statement].back().loc.resource_loc;
return scope_stack_.back().this_macro->get_current_copy_nest().back().loc.resource_loc;
}
}

Expand Down
13 changes: 12 additions & 1 deletion parser_library/src/context/macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "macro.h"

#include <cassert>
#include <numeric>
#include <stdexcept>

#include "copy_member.h"
Expand Down Expand Up @@ -45,9 +46,20 @@ macro_definition::macro_definition(id_index name,
, definition_location(std::move(definition_location))
, used_copy_members(std::move(used_copy_members))
{
cached_definition.reserve(definition.size());
for (auto&& stmt : definition)
cached_definition.emplace_back(std::move(stmt));

auto r = std::accumulate(params.begin(), params.end(), std::pair<size_t, size_t>(1, 0), [](auto a, const auto& e) {
if (e.data)
++a.second;
else
++a.first;
return a;
});
positional_params_.reserve(r.first);
keyword_params_.reserve(r.second);

named_params_.emplace(label_param_name,
positional_params_
.emplace_back(std::make_unique<positional_param>(label_param_name, 0, *macro_param_data_component::dummy))
Expand Down Expand Up @@ -173,5 +185,4 @@ macro_invocation::macro_invocation(id_index name,
, copy_nests(copy_nests)
, labels(labels)
, definition_location(definition_location)
, current_statement((size_t)-1)
{}
18 changes: 17 additions & 1 deletion parser_library/src/context/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#ifndef CONTEXT_MACRO_H
#define CONTEXT_MACRO_H

#include <cassert>
#include <string>
#include <unordered_map>
#include <variant>

#include "common_types.h"
#include "sequence_symbol.h"
#include "statement_cache.h"
#include "statement_id.h"
#include "variables/macro_param.h"

namespace hlasm_plugin::parser_library::context {
Expand Down Expand Up @@ -99,6 +101,12 @@ class macro_definition
const std::vector<std::unique_ptr<positional_param>>& get_positional_params() const;
const std::vector<std::unique_ptr<keyword_param>>& get_keyword_params() const;
const id_index& get_label_param_name() const;

const auto& get_copy_nest(statement_id stmt_id) const noexcept
{
assert(stmt_id.value < copy_nests.size());
return copy_nests[stmt_id.value];
}
};

// represent macro instruction call
Expand All @@ -119,14 +127,22 @@ struct macro_invocation
// location of the macro definition in code
const location& definition_location;
// index to definition vector
size_t current_statement;
statement_id current_statement;

macro_invocation(id_index name,
cached_block& cached_definition,
const copy_nest_storage& copy_nests,
const label_storage& labels,
std::unordered_map<id_index, macro_param_ptr> named_params,
const location& definition_location);

const auto& get_copy_nest(statement_id stmt_id) const noexcept
{
assert(stmt_id.value < copy_nests.size());
return copy_nests[stmt_id.value];
}

const auto& get_current_copy_nest() const noexcept { return get_copy_nest(current_statement); }
};

} // namespace hlasm_plugin::parser_library::context
Expand Down
2 changes: 1 addition & 1 deletion parser_library/src/context/sequence_symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool opencode_sequence_symbol::operator==(const opencode_sequence_symbol& oth) c
return snapshot == oth.snapshot;
}

macro_sequence_symbol::macro_sequence_symbol(id_index name, location loc, size_t statement_offset)
macro_sequence_symbol::macro_sequence_symbol(id_index name, location loc, statement_id statement_offset)
: sequence_symbol(name, sequence_symbol_kind::MACRO, std::move(loc))
, statement_offset(statement_offset)
{}
5 changes: 3 additions & 2 deletions parser_library/src/context/sequence_symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <memory>

#include "source_snapshot.h"
#include "statement_id.h"

namespace hlasm_plugin::parser_library::context {

Expand Down Expand Up @@ -68,9 +69,9 @@ struct opencode_sequence_symbol : public sequence_symbol
struct macro_sequence_symbol : public sequence_symbol
{
// offset from start of the macro
size_t statement_offset;
statement_id statement_offset;

macro_sequence_symbol(id_index name, location symbol_location, size_t statement_offset);
macro_sequence_symbol(id_index name, location symbol_location, statement_id statement_offset);
};

} // namespace hlasm_plugin::parser_library::context
Expand Down
2 changes: 1 addition & 1 deletion parser_library/src/context/source_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ source_snapshot source_context::create_snapshot() const
copy_frames.emplace_back(member.name(), member.current_statement);

if (!copy_frames.empty())
--copy_frames.back().statement_offset;
--copy_frames.back().statement_offset.value;

return source_snapshot { current_instruction, begin_index, end_index, std::move(copy_frames) };
}
Expand Down
5 changes: 3 additions & 2 deletions parser_library/src/context/source_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "id_storage.h"
#include "location.h"
#include "statement_id.h"

namespace hlasm_plugin::parser_library::context {

Expand All @@ -40,9 +41,9 @@ struct source_position
struct copy_frame
{
id_index copy_member;
size_t statement_offset;
statement_id statement_offset;

copy_frame(id_index copy_member, size_t statement_offset)
copy_frame(id_index copy_member, statement_id statement_offset)
: copy_member(copy_member)
, statement_offset(statement_offset)
{}
Expand Down
29 changes: 29 additions & 0 deletions parser_library/src/context/statement_id.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*/

#ifndef HLASMPLUGIN_PARSERLIBRARY_CONTEXT_STATEMENT_ID_H
#define HLASMPLUGIN_PARSERLIBRARY_CONTEXT_STATEMENT_ID_H

#include <cstdint>

namespace hlasm_plugin::parser_library::context {
struct statement_id
{
std::size_t value = (std::size_t)-1;

bool operator==(const statement_id&) const noexcept = default;
};
} // namespace hlasm_plugin::parser_library::context

#endif
9 changes: 5 additions & 4 deletions parser_library/src/lsp/file_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,16 @@ file_slice_t file_slice_t::transform_slice(const macro_slice_t& slice, macro_inf
fslice.macro_lines.begin = slice.begin_statement;
fslice.macro_lines.end = slice.end_statement;

if (slice.begin_statement == 0)
if (slice.begin_statement.value == 0)
fslice.file_lines.begin = macro_i->definition_location.pos.line;
else
fslice.file_lines.begin = macro_i->macro_definition->copy_nests[fslice.macro_lines.begin].back().loc.pos.line;
fslice.file_lines.begin =
macro_i->macro_definition->get_copy_nest(fslice.macro_lines.begin).back().loc.pos.line;

if (slice.end_statement == macro_i->macro_definition->copy_nests.size())
if (slice.end_statement.value == macro_i->macro_definition->copy_nests.size())
fslice.file_lines.end = macro_i->macro_definition->copy_nests.back().back().loc.pos.line + 1;
else
fslice.file_lines.end = macro_i->macro_definition->copy_nests[fslice.macro_lines.end].back().loc.pos.line;
fslice.file_lines.end = macro_i->macro_definition->get_copy_nest(fslice.macro_lines.end).back().loc.pos.line;

fslice.type = slice.inner_macro ? scope_type::INNER_MACRO : scope_type::MACRO;
fslice.macro_context = macro_i;
Expand Down
9 changes: 8 additions & 1 deletion parser_library/src/lsp/file_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <vector>

#include "context/copy_member.h"
#include "context/statement_id.h"
#include "macro_info.h"
#include "symbol_occurrence.h"
#include "text_data_view.h"
Expand All @@ -44,6 +45,12 @@ struct line_range
size_t end;
};

struct macro_range
{
context::statement_id begin;
context::statement_id end;
};

bool operator==(const line_range& lhs, const line_range& rhs);
bool operator<(const line_range& lhs, const line_range& rhs);

Expand All @@ -54,7 +61,7 @@ struct file_slice_t
macro_info_ptr macro_context;

// range of slice within macro definition
line_range macro_lines;
macro_range macro_lines;
// range of slice within file
line_range file_lines;

Expand Down
2 changes: 1 addition & 1 deletion parser_library/src/lsp/lsp_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ std::optional<location> lsp_context::find_definition_location(const symbol_occur
{
if (macro_scope_i)
return location(sym->def_position,
macro_scope_i->macro_definition->copy_nests[sym->def_location].back().loc.resource_loc);
macro_scope_i->macro_definition->get_copy_nest(sym->def_location).back().loc.resource_loc);
return location(sym->def_position, sym->file);
}
break;
Expand Down
Loading

0 comments on commit 9df8c37

Please sign in to comment.