Skip to content

Commit 82f33c8

Browse files
committed
slightly-more-typesafe
1 parent b3726b9 commit 82f33c8

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

toolchain/base/value_store.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,24 @@ struct IdTag {
8080
return (llvm::reverseBits(2) & tagged_index) != 0;
8181
}
8282

83+
template <class TagT>
8384
struct TagAndIndex {
84-
int32_t tag;
85+
TagT tag;
8586
int32_t index;
8687
};
8788

88-
static auto DecomposeWithBestEffort(int32_t tagged_index) -> TagAndIndex {
89+
template <typename TagT>
90+
static auto DecomposeWithBestEffort(int32_t tagged_index)
91+
-> TagAndIndex<TagT> {
8992
if (tagged_index < 0) {
90-
return {-1, tagged_index};
93+
// TODO: This should return TagT::None, but we need a fallback TagT other
94+
// than `int32_t`.
95+
return {TagT{-1}, tagged_index};
9196
}
9297
if (!HasTag(tagged_index)) {
93-
return {-1, tagged_index};
98+
// TODO: This should return TagT::None, but we need a fallback TagT other
99+
// than `int32_t`.
100+
return {TagT{-1}, tagged_index};
94101
}
95102
int length = 0;
96103
int location = 0;
@@ -110,12 +117,14 @@ struct IdTag {
110117
}
111118
}
112119
if (length < 8) {
113-
return {-1, tagged_index};
120+
// TODO: This should return TagT::None, but we need a fallback TagT other
121+
// than `int32_t`.
122+
return {TagT{-1}, tagged_index};
114123
}
115124
auto index_mask = llvm::maskTrailingOnes<uint32_t>(location);
116125
auto tag = (llvm::reverseBits(tagged_index & ~index_mask) >> 2) - 1;
117126
auto index = tagged_index & index_mask;
118-
return {.tag = static_cast<int32_t>(tag),
127+
return {.tag = TagT{static_cast<int32_t>(tag)},
119128
.index = static_cast<int32_t>(index)};
120129
}
121130

@@ -296,11 +305,14 @@ class ValueStore
296305
// Attempt to decompose id.index to include extra detail in the check here
297306
#ifndef NDEBUG
298307
if (index >= size_) {
299-
auto [ir_id, decomposed_index] = IdTag::DecomposeWithBestEffort(id.index);
308+
// TODO: Could we add InstId::TagType = CheckIRId and use that here to
309+
// print the ChcekIRId properly?
310+
auto [ir_id, decomposed_index] =
311+
IdTag::DecomposeWithBestEffort<int32_t>(id.index);
300312
CARBON_DCHECK(
301313
index < size_,
302314
"Untagged index was outside of container range. Possibly tagged "
303-
"index {0}. Best-effort decomposition: CheckIRId: {1}, index: {2}. "
315+
"index {0}. Best-effort decomposition: Tag: {1}, index: {2}. "
304316
"The IdTag for this container is: {3}",
305317
id.index, ir_id, decomposed_index, tag_.GetContainerTag());
306318
}

toolchain/sem_ir/ids.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ auto InstId::Print(llvm::raw_ostream& out) const -> void {
1616
if (IsSingletonInstId(*this)) {
1717
out << Label << "(" << SingletonInstKinds[index] << ")";
1818
} else {
19-
auto [check_ir_id, inst_id] = IdTag::DecomposeWithBestEffort(index);
20-
if (check_ir_id == -1) {
19+
auto [check_ir_id, untagged_index] =
20+
IdTag::DecomposeWithBestEffort<CheckIRId>(index);
21+
if (!check_ir_id.has_value()) {
2122
IdBase::Print(out);
2223
} else {
23-
out << CheckIRId::Label << check_ir_id << "." << Label << inst_id;
24+
out << check_ir_id << "." << Label << untagged_index;
2425
}
2526
}
2627
}

0 commit comments

Comments
 (0)