mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 10:01:06 +01:00
Remove calls to `InstStore::GetLocId()` to build a LocId from an InstId now that they can be constructed directly from the InstId. Most uses of LocId are just plumbing, so this does not affect them. However places that want to look inside the LocId do not want to work with the InstId form. In these places, introduce `InstStore::GetResolvedLocId()` which converts a LocId (or an InstId as an optimization) into a LocId which is not backed by an InstId. These locations can be printed (they have a line and column when they are a NodeId), they can have flags added to them (`ToImplicit`, `ToTokenOnly`), they can be converted to an underlying ImportIRInstId, or they may be `None`. `Dump()` is made to print a resolved location instead of printing the InstId in the location, since (at least in my experience) the resolved location is what is interesting in debugging, and this saves manual `MakeInstId` steps in the debugger every time a location is of interest. The LocId constructor from InstId is made `explicit` to add clarity to function calls passing an `inst_id` now directly instead of calling `context.insts().GetLocId(inst_id)`. To avoid needing to construct `SemIR::LocId(...)` explicitly in all cases though, the diagnostics code in Check uses `DiagnosticLocId` as its template parameter which accepts InstId as well and does the construction of LocId from it. Because LocId now requires an explicit construction from InstId, any callers to `AddInst()` functions will have to explicitly convert to LocId if they had an InstId, but not if they pass a NodeId. To make this difference clear to callers, we `requires` that the input type can be converted to LocId. This ensures that passing an InstId results in an error at the callsite where the InstId is passed, instead of generating a compiler error when trying to construct `LocIdAndInst` inside `AddInst()`, which is less clear about what went wrong and doesn't seem entirely intentional. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
140 lines
4.7 KiB
C++
140 lines
4.7 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/sem_ir/ids.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <limits>
|
|
#include <tuple>
|
|
|
|
namespace Carbon::SemIR {
|
|
namespace {
|
|
|
|
using ::testing::Eq;
|
|
|
|
TEST(IdsTest, LocIdValues) {
|
|
// This testing should match the ranges documented on LocId.
|
|
EXPECT_THAT(static_cast<LocId>(Parse::NodeId::None).index, Eq(-1));
|
|
|
|
EXPECT_THAT(static_cast<LocId>(InstId(0)).index, Eq(0));
|
|
EXPECT_THAT(
|
|
static_cast<LocId>(InstId(std::numeric_limits<int32_t>::max())).index,
|
|
Eq(std::numeric_limits<int32_t>::max()));
|
|
|
|
EXPECT_THAT(static_cast<LocId>(Parse::NodeId(0)).index, Eq(-2));
|
|
EXPECT_THAT(static_cast<LocId>(Parse::NodeId(Parse::NodeId::Max - 1)).index,
|
|
Eq(-2 - (1 << 24) + 1));
|
|
|
|
EXPECT_THAT(static_cast<LocId>(ImportIRInstId(0)).index, Eq(-2 - (1 << 24)));
|
|
EXPECT_THAT(static_cast<LocId>(ImportIRInstId(ImportIRInstId::Max - 1)).index,
|
|
Eq(-(1 << 29) + 1));
|
|
}
|
|
|
|
// A standard parameterized test for (implicit, token_only, index).
|
|
class IdsTestWithParam
|
|
: public testing::TestWithParam<std::tuple<bool, bool, int32_t>> {
|
|
public:
|
|
explicit IdsTestWithParam() {
|
|
llvm::errs() << "implicit=" << is_implicit()
|
|
<< ", token_only=" << is_token_only()
|
|
<< ", index=" << std::get<2>(GetParam()) << "\n";
|
|
}
|
|
|
|
// Returns IdT with its matching LocId form. Sets flags based on test
|
|
// parameters.
|
|
template <typename IdT>
|
|
auto BuildIdAndLocId() -> std::pair<IdT, LocId> {
|
|
auto [implicit, token_only, index] = GetParam();
|
|
IdT id(index);
|
|
LocId loc_id(id);
|
|
if (implicit) {
|
|
loc_id = loc_id.ToImplicit();
|
|
}
|
|
if (token_only) {
|
|
loc_id = loc_id.ToTokenOnly();
|
|
}
|
|
return {id, loc_id};
|
|
}
|
|
|
|
auto is_implicit() -> bool { return std::get<0>(GetParam()); }
|
|
auto is_token_only() -> bool { return std::get<1>(GetParam()); }
|
|
};
|
|
|
|
// Returns a test case generator for edge-case values.
|
|
static auto GetValueRange(int32_t max) -> auto {
|
|
return testing::Values(0, 1, max - 2, max - 1);
|
|
}
|
|
|
|
// Returns a test case generator for `IdsTestWithParam` uses.
|
|
static auto CombineWithFlags(auto value_range) -> auto {
|
|
return testing::Combine(testing::Bool(), testing::Bool(), value_range);
|
|
}
|
|
|
|
class LocIdAsNoneTestWithParam : public IdsTestWithParam {};
|
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
|
LocIdAsNoneTest, LocIdAsNoneTestWithParam,
|
|
CombineWithFlags(testing::Values(Parse::NodeId::NoneIndex)));
|
|
|
|
TEST_P(LocIdAsNoneTestWithParam, Test) {
|
|
auto [_, loc_id] = BuildIdAndLocId<Parse::NodeId>();
|
|
EXPECT_FALSE(loc_id.has_value());
|
|
EXPECT_THAT(loc_id.kind(), Eq(LocId::Kind::None));
|
|
EXPECT_FALSE(loc_id.is_implicit());
|
|
EXPECT_THAT(loc_id.import_ir_inst_id(), Eq(ImportIRInstId::None));
|
|
EXPECT_THAT(loc_id.inst_id(), Eq(InstId::None));
|
|
EXPECT_THAT(loc_id.node_id(),
|
|
// The actual type is NoneNodeId, so cast to NodeId.
|
|
Eq<Parse::NodeId>(Parse::NodeId::None));
|
|
}
|
|
|
|
class LocIdAsImportIRInstIdTest : public IdsTestWithParam {};
|
|
|
|
INSTANTIATE_TEST_SUITE_P(Test, LocIdAsImportIRInstIdTest,
|
|
CombineWithFlags(GetValueRange(ImportIRInstId::Max)));
|
|
|
|
TEST_P(LocIdAsImportIRInstIdTest, Test) {
|
|
auto [import_ir_inst_id, loc_id] = BuildIdAndLocId<ImportIRInstId>();
|
|
EXPECT_TRUE(loc_id.has_value());
|
|
ASSERT_THAT(loc_id.kind(), Eq(LocId::Kind::ImportIRInstId));
|
|
EXPECT_THAT(loc_id.import_ir_inst_id(), import_ir_inst_id);
|
|
EXPECT_FALSE(loc_id.is_implicit());
|
|
EXPECT_THAT(loc_id.is_token_only(), Eq(is_token_only()));
|
|
}
|
|
|
|
class LocIdAsInstIdTest : public IdsTestWithParam {};
|
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
|
Test, LocIdAsInstIdTest,
|
|
testing::Combine(testing::Values(false), testing::Values(false),
|
|
GetValueRange(std::numeric_limits<int32_t>::max())));
|
|
|
|
TEST_P(LocIdAsInstIdTest, Test) {
|
|
auto [inst_id, loc_id] = BuildIdAndLocId<InstId>();
|
|
EXPECT_TRUE(loc_id.has_value());
|
|
ASSERT_THAT(loc_id.kind(), Eq(LocId::Kind::InstId));
|
|
EXPECT_THAT(loc_id.inst_id(), inst_id);
|
|
// Note that `is_implicit` and `is_token_only` are invalid to use with
|
|
// `InstId`.
|
|
}
|
|
|
|
class LocIdAsNodeIdTest : public IdsTestWithParam {};
|
|
|
|
INSTANTIATE_TEST_SUITE_P(Test, LocIdAsNodeIdTest,
|
|
CombineWithFlags(GetValueRange(Parse::NodeId::Max)));
|
|
|
|
TEST_P(LocIdAsNodeIdTest, Test) {
|
|
auto [node_id, loc_id] = BuildIdAndLocId<Parse::NodeId>();
|
|
EXPECT_TRUE(loc_id.has_value());
|
|
ASSERT_THAT(loc_id.kind(), Eq(LocId::Kind::NodeId));
|
|
EXPECT_THAT(loc_id.node_id(), node_id);
|
|
EXPECT_THAT(loc_id.is_implicit(), Eq(is_implicit()));
|
|
EXPECT_THAT(loc_id.is_token_only(), Eq(is_token_only()));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::SemIR
|