Skip to content

Commit a85d292

Browse files
authored
Change from ToImplicit to AsDesugared (#5591)
This changes `ToImplicit` to `AsDesugared`, and adds a `GetLocIdForDesugaring` to `InstStore`. In particular, I'm motivated by the latter, to make it clearer what the intended call convention is.
1 parent 6831c98 commit a85d292

File tree

9 files changed

+45
-39
lines changed

9 files changed

+45
-39
lines changed

toolchain/check/dump.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::LocId loc_id)
134134
auto token = context.parse_tree().node_token(loc_id.node_id());
135135
auto line = context.tokens().GetLineNumber(token);
136136
auto col = context.tokens().GetColumnNumber(token);
137-
const char* implicit = loc_id.is_implicit() ? " implicit" : "";
137+
const char* implicit = loc_id.is_desugared() ? " implicit" : "";
138138
out << "LocId(" << FormatEscaped(context.sem_ir().filename()) << ":"
139139
<< line << ":" << col << implicit << ")";
140140
break;

toolchain/check/impl_lookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static auto GetOrAddLookupImplWitness(Context& context, SemIR::LocId loc_id,
375375
SemIR::SpecificInterface interface)
376376
-> SemIR::InstId {
377377
auto witness_const_id = EvalOrAddInst(
378-
context, context.insts().GetCanonicalLocId(loc_id).ToImplicit(),
378+
context, context.insts().GetLocIdForDesugaring(loc_id),
379379
SemIR::LookupImplWitness{
380380
.type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId),
381381
.query_self_inst_id =

toolchain/check/inst.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
124124
return context.constant_values().GetInstId(const_id);
125125
};
126126

127-
// If the instruction is implicit, produce its constant value instead if
128-
// possible.
129-
if (loc_id_and_inst.loc_id.is_implicit()) {
127+
// If the instruction is from desugaring, produce its constant value instead
128+
// if possible.
129+
if (loc_id_and_inst.loc_id.is_desugared()) {
130130
switch (loc_id_and_inst.inst.kind().constant_needs_inst_id()) {
131131
case SemIR::InstConstantNeedsInstIdKind::No: {
132132
// Evaluation doesn't need an InstId. Just do it.

toolchain/check/operator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Carbon::Check {
1717
// Returns the `Op` function for the specified operator.
1818
static auto GetOperatorOpFunction(Context& context, SemIR::LocId loc_id,
1919
Operator op) -> SemIR::InstId {
20-
auto implicit_loc_id = context.insts().GetCanonicalLocId(loc_id).ToImplicit();
20+
auto implicit_loc_id = context.insts().GetLocIdForDesugaring(loc_id);
2121

2222
// Look up the interface, and pass it any generic arguments.
2323
auto interface_id =

toolchain/check/subst.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,7 @@ class SubstConstantCallbacks final : public SubstInstCallbacks {
393393
auto const_id = EvalOrAddInst(
394394
context(),
395395
SemIR::LocIdAndInst::UncheckedLoc(
396-
context().insts().GetCanonicalLocId(old_inst_id).ToImplicit(),
397-
new_inst));
396+
context().insts().GetLocIdForDesugaring(old_inst_id), new_inst));
398397
CARBON_CHECK(const_id.has_value(),
399398
"Substitution into constant produced non-constant");
400399
CARBON_CHECK(const_id.is_constant(),

toolchain/sem_ir/ids.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ auto LocId::Print(llvm::raw_ostream& out) const -> void {
178178
break;
179179
case Kind::NodeId:
180180
out << Label << "_" << node_id();
181+
if (is_desugared()) {
182+
out << "_desugared";
183+
}
181184
break;
182185
}
183186
}

toolchain/sem_ir/ids.h

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -860,14 +860,10 @@ struct ImportIRInstId : public IdBase<ImportIRInstId> {
860860
// values to 30 bits.
861861
// - [-2 - (1 << 24), -(1 << 30))
862862
//
863-
// In addition, one bit is used for flags: `ImplicitBit`.
863+
// In addition, one bit is used for flags: `DesugaredBit`.
864864
// Note that this can only be used with negative, non-`InstId` values.
865865
//
866-
// Use `InstStore::GetCanonicalLocId()` to get a canonical `LocId` which will
867-
// not be backed by an `InstId`. Note that the canonical `LocId` may be `None`
868-
// even when the original `LocId` was not, so this operation needs to be done
869-
// before checking `has_value()`. Only canonical locations can be converted with
870-
// `ToImplicit()`.
866+
// For desugaring, use `InstStore::GetLocIdForDesugaring()`.
871867
struct LocId : public IdBase<LocId> {
872868
// The contained index kind.
873869
enum class Kind {
@@ -896,16 +892,15 @@ struct LocId : public IdBase<LocId> {
896892
constexpr LocId(Parse::NodeId node_id)
897893
: IdBase(FirstNodeId - node_id.index) {}
898894

899-
// Forms an equivalent LocId for a desugared location. Requires a
900-
// canonical location. See `InstStore::GetCanonicalLocId()`.
901-
//
902-
// TODO: Rename to something like `ToDesugared`.
903-
auto ToImplicit() const -> LocId {
895+
// Forms an equivalent LocId for a desugared location. Prefer calling
896+
// `InstStore::GetLocIdForDesugaring`.
897+
auto AsDesugared() const -> LocId {
904898
// This should only be called for NodeId or ImportIRInstId (i.e. canonical
905899
// locations), but we only set the flag for NodeId.
906-
CARBON_CHECK(kind() != Kind::InstId);
900+
CARBON_CHECK(kind() != Kind::InstId,
901+
"Use InstStore::GetLocIdForDesugaring");
907902
if (kind() == Kind::NodeId) {
908-
return LocId(index & ~ImplicitBit);
903+
return LocId(index & ~DesugaredBit);
909904
}
910905
return *this;
911906
}
@@ -926,8 +921,8 @@ struct LocId : public IdBase<LocId> {
926921

927922
// Returns true if the location corresponds to desugared instructions.
928923
// Requires a non-`InstId` location.
929-
auto is_implicit() const -> bool {
930-
return (kind() == Kind::NodeId) && (index & ImplicitBit) == 0;
924+
auto is_desugared() const -> bool {
925+
return (kind() == Kind::NodeId) && (index & DesugaredBit) == 0;
931926
}
932927

933928
// Returns the equivalent `ImportIRInstId` when `kind()` matches or is `None`.
@@ -962,7 +957,7 @@ struct LocId : public IdBase<LocId> {
962957
private:
963958
// Whether a location corresponds to desugared instructions. This only applies
964959
// for `NodeId`.
965-
static constexpr int32_t ImplicitBit = 1 << 30;
960+
static constexpr int32_t DesugaredBit = 1 << 30;
966961

967962
// The value of the 0 index for each of `NodeId` and `ImportIRInstId`.
968963
static constexpr int32_t FirstNodeId = NoneIndex - 1;
@@ -971,7 +966,7 @@ struct LocId : public IdBase<LocId> {
971966

972967
auto index_without_flags() const -> int32_t {
973968
CARBON_DCHECK(index < NoneIndex, "Only for NodeId and ImportIRInstId");
974-
return index | ImplicitBit;
969+
return index | DesugaredBit;
975970
}
976971
};
977972

toolchain/sem_ir/ids_test.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,29 @@ TEST(IdsTest, LocIdValues) {
3333
Eq(-(1 << 30) + 1));
3434
}
3535

36-
// A standard parameterized test for (implicit, index).
36+
// A standard parameterized test for (is_desugared, index).
3737
class IdsTestWithParam
3838
: public testing::TestWithParam<std::tuple<bool, int32_t>> {
3939
public:
4040
explicit IdsTestWithParam() {
41-
llvm::errs() << "implicit=" << is_implicit()
42-
<< ", index=" << std::get<1>(GetParam()) << "\n";
41+
llvm::errs() << "is_desugared=" << is_desugared() << ", index=" << index()
42+
<< "\n";
4343
}
4444

4545
// Returns IdT with its matching LocId form. Sets flags based on test
4646
// parameters.
4747
template <typename IdT>
4848
auto BuildIdAndLocId() -> std::pair<IdT, LocId> {
49-
auto [implicit, index] = GetParam();
50-
IdT id(index);
51-
LocId loc_id(id);
52-
if (implicit) {
53-
loc_id = loc_id.ToImplicit();
49+
IdT id(index());
50+
if (is_desugared()) {
51+
return {id, LocId(id).AsDesugared()};
52+
} else {
53+
return {id, LocId(id)};
5454
}
55-
return {id, loc_id};
5655
}
5756

58-
auto is_implicit() -> bool { return std::get<0>(GetParam()); }
57+
auto is_desugared() -> bool { return std::get<0>(GetParam()); }
58+
auto index() -> int32_t { return std::get<1>(GetParam()); }
5959
};
6060

6161
// Returns a test case generator for edge-case values.
@@ -78,7 +78,7 @@ TEST_P(LocIdAsNoneTestWithParam, Test) {
7878
auto [_, loc_id] = BuildIdAndLocId<Parse::NodeId>();
7979
EXPECT_FALSE(loc_id.has_value());
8080
EXPECT_THAT(loc_id.kind(), Eq(LocId::Kind::None));
81-
EXPECT_FALSE(loc_id.is_implicit());
81+
EXPECT_FALSE(loc_id.is_desugared());
8282
EXPECT_THAT(loc_id.import_ir_inst_id(), Eq(ImportIRInstId::None));
8383
EXPECT_THAT(loc_id.inst_id(), Eq(InstId::None));
8484
EXPECT_THAT(loc_id.node_id(),
@@ -96,7 +96,7 @@ TEST_P(LocIdAsImportIRInstIdTest, Test) {
9696
EXPECT_TRUE(loc_id.has_value());
9797
ASSERT_THAT(loc_id.kind(), Eq(LocId::Kind::ImportIRInstId));
9898
EXPECT_THAT(loc_id.import_ir_inst_id(), import_ir_inst_id);
99-
EXPECT_FALSE(loc_id.is_implicit());
99+
EXPECT_FALSE(loc_id.is_desugared());
100100
}
101101

102102
class LocIdAsInstIdTest : public IdsTestWithParam {};
@@ -111,7 +111,7 @@ TEST_P(LocIdAsInstIdTest, Test) {
111111
EXPECT_TRUE(loc_id.has_value());
112112
ASSERT_THAT(loc_id.kind(), Eq(LocId::Kind::InstId));
113113
EXPECT_THAT(loc_id.inst_id(), inst_id);
114-
// Note that `is_implicit` is invalid to use with `InstId`.
114+
// Note that `is_desugared` is invalid to use with `InstId`.
115115
}
116116

117117
class LocIdAsNodeIdTest : public IdsTestWithParam {};
@@ -124,7 +124,7 @@ TEST_P(LocIdAsNodeIdTest, Test) {
124124
EXPECT_TRUE(loc_id.has_value());
125125
ASSERT_THAT(loc_id.kind(), Eq(LocId::Kind::NodeId));
126126
EXPECT_THAT(loc_id.node_id(), node_id);
127-
EXPECT_THAT(loc_id.is_implicit(), Eq(is_implicit()));
127+
EXPECT_THAT(loc_id.is_desugared(), Eq(is_desugared()));
128128
}
129129

130130
} // namespace

toolchain/sem_ir/inst.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,15 @@ class InstStore {
493493
return GetCanonicalLocId(GetNonCanonicalLocId(inst_id));
494494
}
495495

496+
// Returns a virtual location to use for the desugaring of the code at the
497+
// specified location.
498+
auto GetLocIdForDesugaring(LocId loc_id) const -> LocId {
499+
return GetCanonicalLocId(loc_id).AsDesugared();
500+
}
501+
auto GetLocIdForDesugaring(InstId inst_id) const -> LocId {
502+
return GetCanonicalLocId(inst_id).AsDesugared();
503+
}
504+
496505
// Returns the instruction that this instruction was imported from, or
497506
// ImportIRInstId::None if this instruction was not generated by importing
498507
// another instruction.

0 commit comments

Comments
 (0)