mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Use typed parse node ids in SemIR instruction types (#3560)
This involves a number of supporting changes: * The `parse_node;` member of instruction types may now have any type derived from `Parse::NodeId` and is no longer required to have that exact type. * `Parse::Node::Invalid` is now a singleton object of a separate type that is convertible to `Parse::NodeId` and its descendants. This replaces the `Invalid` member of its descendants, and avoids having to write long `NodeIdOneOf<...>` types when initializing variables to invalid. * `IndexBase` now allows `==` and `!=` comparisons between its derived classes and types that are convertible to those types. * A number of functions in the check stage have been changed to preserve more type information instead of using `Parse::NodeId`. * `NodeIdForKind<K>` (also known as `KId`) now has a `Kind` member so it may be used to declare `NodeIdOneOf<T, U>` types without #including `parse/typed_nodes.h`. * `NodeIdForKind<K>` (also known as `KId`) may be implicitly converted to `NodeIdOneOf<T, U>` if `T::Kind == K` or `U::Kind == K` (executing a TODO). Many of the `parse_node` members were not converted since they would have required more extensive changes. They have been marked with "TODO" comments. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
@@ -144,40 +144,45 @@ class NodeStack {
|
||||
template <const Parse::NodeKind& RequiredParseKind>
|
||||
auto PopWithParseNode() -> auto {
|
||||
constexpr IdKind RequiredIdKind = ParseNodeKindToIdKind(RequiredParseKind);
|
||||
auto NodeIdCast = [&](auto back) {
|
||||
using NodeIdT = Parse::NodeIdForKind<RequiredParseKind>;
|
||||
return std::pair<NodeIdT, decltype(back.second)>(back);
|
||||
};
|
||||
|
||||
if constexpr (RequiredIdKind == IdKind::InstId) {
|
||||
auto back = PopWithParseNode<SemIR::InstId>();
|
||||
RequireParseKind<RequiredParseKind>(back.first);
|
||||
return back;
|
||||
return NodeIdCast(back);
|
||||
}
|
||||
if constexpr (RequiredIdKind == IdKind::InstBlockId) {
|
||||
auto back = PopWithParseNode<SemIR::InstBlockId>();
|
||||
RequireParseKind<RequiredParseKind>(back.first);
|
||||
return back;
|
||||
return NodeIdCast(back);
|
||||
}
|
||||
if constexpr (RequiredIdKind == IdKind::FunctionId) {
|
||||
auto back = PopWithParseNode<SemIR::FunctionId>();
|
||||
RequireParseKind<RequiredParseKind>(back.first);
|
||||
return back;
|
||||
return NodeIdCast(back);
|
||||
}
|
||||
if constexpr (RequiredIdKind == IdKind::ClassId) {
|
||||
auto back = PopWithParseNode<SemIR::ClassId>();
|
||||
RequireParseKind<RequiredParseKind>(back.first);
|
||||
return back;
|
||||
return NodeIdCast(back);
|
||||
}
|
||||
if constexpr (RequiredIdKind == IdKind::InterfaceId) {
|
||||
auto back = PopWithParseNode<SemIR::InterfaceId>();
|
||||
RequireParseKind<RequiredParseKind>(back.first);
|
||||
return back;
|
||||
return NodeIdCast(back);
|
||||
}
|
||||
if constexpr (RequiredIdKind == IdKind::NameId) {
|
||||
auto back = PopWithParseNode<SemIR::NameId>();
|
||||
RequireParseKind<RequiredParseKind>(back.first);
|
||||
return back;
|
||||
return NodeIdCast(back);
|
||||
}
|
||||
if constexpr (RequiredIdKind == IdKind::TypeId) {
|
||||
auto back = PopWithParseNode<SemIR::TypeId>();
|
||||
RequireParseKind<RequiredParseKind>(back.first);
|
||||
return back;
|
||||
return NodeIdCast(back);
|
||||
}
|
||||
CARBON_FATAL() << "Unpoppable IdKind for parse kind: " << RequiredParseKind
|
||||
<< "; see value in ParseNodeKindToIdKind";
|
||||
|
||||
Reference in New Issue
Block a user