Remove default constructor from IndexBase (#2598)

There were some notes about default constructors being required in parse_tree.h, but they don't seem to be. Removing the default constructor forces the explicit `::Invalid` where there's no value immediately being assigned, which works fine for existing code. I think this actually reduces the chance of accidents more than the prior default-construct-as-invalid approach.
This commit is contained in:
Jon Ross-Perkins
2023-02-10 12:00:50 -08:00
committed by GitHub
parent aa307b1144
commit b76bc875c4
7 changed files with 28 additions and 30 deletions
+12 -10
View File
@@ -33,7 +33,8 @@ struct SemanticsNodeId : public IndexBase {
}
};
constexpr SemanticsNodeId SemanticsNodeId::Invalid = SemanticsNodeId();
constexpr SemanticsNodeId SemanticsNodeId::Invalid =
SemanticsNodeId(SemanticsNodeId::InvalidIndex);
// Uses the cross-reference node ID for a builtin. This relies on SemanticsIR
// guarantees for builtin cross-reference placement.
@@ -88,7 +89,7 @@ struct SemanticsNodeBlockId : public IndexBase {
constexpr SemanticsNodeBlockId SemanticsNodeBlockId::Empty =
SemanticsNodeBlockId(0);
constexpr SemanticsNodeBlockId SemanticsNodeBlockId::Invalid =
SemanticsNodeBlockId();
SemanticsNodeBlockId(SemanticsNodeBlockId::InvalidIndex);
// Type-safe storage of strings.
struct SemanticsStringId : public IndexBase {
@@ -144,8 +145,8 @@ class SemanticsNode {
SemanticsNodeId type) -> SemanticsNode {
// Builtins won't have a ParseTree node associated, so we provide the
// default invalid one.
return SemanticsNode(ParseTree::Node(), SemanticsNodeKind::Builtin, type,
builtin_kind.AsInt());
return SemanticsNode(ParseTree::Node::Invalid, SemanticsNodeKind::Builtin,
type, builtin_kind.AsInt());
}
auto GetAsBuiltin() const -> SemanticsBuiltinKind {
CARBON_CHECK(kind_ == SemanticsNodeKind::Builtin);
@@ -155,7 +156,7 @@ class SemanticsNode {
static auto MakeCodeBlock(ParseTree::Node parse_node,
SemanticsNodeBlockId node_block) -> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::CodeBlock,
SemanticsNodeId(), node_block.index);
SemanticsNodeId::Invalid, node_block.index);
}
auto GetAsCodeBlock() const -> SemanticsNodeBlockId {
CARBON_CHECK(kind_ == SemanticsNodeKind::CodeBlock);
@@ -180,7 +181,7 @@ class SemanticsNode {
SemanticsCallableId signature)
-> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::FunctionDeclaration,
SemanticsNodeId(), signature.index);
SemanticsNodeId::Invalid, signature.index);
}
auto GetAsFunctionDeclaration() const -> SemanticsCallableId {
CARBON_CHECK(kind_ == SemanticsNodeKind::FunctionDeclaration);
@@ -192,7 +193,8 @@ class SemanticsNode {
SemanticsNodeBlockId node_block)
-> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::FunctionDefinition,
SemanticsNodeId(), decl.index, node_block.index);
SemanticsNodeId::Invalid, decl.index,
node_block.index);
}
auto GetAsFunctionDefinition() const
-> std::pair<SemanticsNodeId, SemanticsNodeBlockId> {
@@ -225,7 +227,7 @@ class SemanticsNode {
// understand the type without checking, so it's not necessary but could be
// specified if needed.
return SemanticsNode(parse_node, SemanticsNodeKind::Return,
SemanticsNodeId());
SemanticsNodeId::Invalid);
}
auto GetAsReturn() const -> NoArgs {
CARBON_CHECK(kind_ == SemanticsNodeKind::Return);
@@ -253,8 +255,8 @@ class SemanticsNode {
}
SemanticsNode()
: SemanticsNode(ParseTree::Node(), SemanticsNodeKind::Invalid,
SemanticsNodeId()) {}
: SemanticsNode(ParseTree::Node::Invalid, SemanticsNodeKind::Invalid,
SemanticsNodeId::Invalid) {}
auto parse_node() const -> ParseTree::Node { return parse_node_; }
auto kind() const -> SemanticsNodeKind { return kind_; }