Files
carbon-lang/toolchain/semantics/semantics_node.h
T
Chandler CarruthandJon Ross-Perkins a1ad39fa29 Introduce helpers to build enum-wrapping classes. (#2504)
The goal here is to (significantly) reduce the boilerplate needed when defining classes that wrap enums, especially those managed with the `.def`-file style X-macros that are common in the toolchain.

This should also provide both better and more consistent functionality to those classes once ported over to it.

Initially, only `ParserState`, `SemanticsNodeKind`, and `SemanticsBuiltinKind` are ported as these were also the three that JonMeow ported in his original pull/2453 "option 5". This is heavily based on that version of the code.

Goals I was considering that influenced the design:

- Keep the individual enum-wrapping classes as simple and easy to read as possible. Especially important is keeping the `.def` files that are often filled with really important documentation clean and easy to maintain over time.

- Don't rely on computed `#include`s as that is an especially dark corner of the preprocessor and breaks some build systems.

- Have a really good API of the enum-wrapping class, including nice constant names for the values, easy printing, and even easy debugger-callable methods to get the name (as opposed to the integer value).

- Keep the API that users interact with in the base class as clean and easy to read as possible.

- Reduce the boiler plate for each instance of these as much as possible.

- Avoid excessive inline generated code or constants that would result in steady growth in object file sizes and linker effort doing deduplication.

These goals aren't always compatible, so we end up needing to pick a compromise between them when in tension. I think this version is a pretty good compromise.

The original version I started with already pull most of the API into a CRTP-style base class. This version pulls *all* of the common API. This is the main tool for getting consistency and avoiding duplication. However, connecting this base class to the individual enum wrappers is still difficult. Some specific changes here that try to do as much as possible there:

- Use a slightly fancier macro pattern to reduce the boilerplate of defining the raw `enum class` prior to the wrapper class.

- Use a macro to simplify naming the base class.

- Move the name table to a `.cpp` file to avoid every inclusion generating a complete copy of the strings (that the linker has to deduplicate). This is done with some care to sharply reduce the boilerplate needed in that `.cpp` file.

- Sink the name _API_ fully into the CRTP base class. This requires some significant complexity in the implementation, but all of that is hidden behind a single implementation detail macro, and the API itself is simple and readable. This also makes it much more reasonable to test the entire system a single time next to the base class.

This version also moves from constant factory functions to normal constants. This requires two batches -- first a declaration, and then a definition -- but the API result is significantly better and similar to the original option, the macro structure reduces the cost of these. Unfortunately that makes the adoption a bit noisy, but I think its worth the churn.

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2023-01-04 12:33:06 -08:00

249 lines
9.5 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
#ifndef CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_H_
#define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_H_
#include <cstdint>
#include "common/check.h"
#include "common/ostream.h"
#include "toolchain/parser/parse_tree.h"
#include "toolchain/semantics/semantics_builtin_kind.h"
#include "toolchain/semantics/semantics_node_kind.h"
namespace Carbon {
// Type-safe storage of Node IDs.
struct SemanticsNodeId : public IndexBase {
// Uses the cross-reference node ID for a builtin. This relies on SemanticsIR
// guarantees for builtin cross-reference placement.
static auto MakeBuiltinReference(SemanticsBuiltinKind kind)
-> SemanticsNodeId {
return SemanticsNodeId(kind.AsInt());
}
// Constructs an explicitly invalid instance.
static auto MakeInvalid() -> SemanticsNodeId { return SemanticsNodeId(); }
using IndexBase::IndexBase;
auto Print(llvm::raw_ostream& out) const -> void { out << "node" << index; }
};
// The ID of a cross-referenced IR.
struct SemanticsCrossReferenceIRId : public IndexBase {
using IndexBase::IndexBase;
auto Print(llvm::raw_ostream& out) const -> void { out << "ir" << index; }
};
// Type-safe storage of integer literals.
struct SemanticsIntegerLiteralId : public IndexBase {
using IndexBase::IndexBase;
auto Print(llvm::raw_ostream& out) const -> void { out << "int" << index; }
};
// Type-safe storage of node blocks.
struct SemanticsNodeBlockId : public IndexBase {
using IndexBase::IndexBase;
auto Print(llvm::raw_ostream& out) const -> void { out << "block" << index; }
};
// Type-safe storage of strings.
struct SemanticsStringId : public IndexBase {
using IndexBase::IndexBase;
auto Print(llvm::raw_ostream& out) const -> void { out << "str" << index; }
};
// The standard structure for nodes.
class SemanticsNode {
public:
struct NoArgs {};
auto GetAsInvalid() const -> NoArgs { CARBON_FATAL() << "Invalid access"; }
static auto MakeAssign(ParseTree::Node parse_node, SemanticsNodeId type,
SemanticsNodeId lhs, SemanticsNodeId rhs)
-> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::Assign, type, lhs.index,
rhs.index);
}
auto GetAsAssign() const -> std::pair<SemanticsNodeId, SemanticsNodeId> {
CARBON_CHECK(kind_ == SemanticsNodeKind::Assign);
return {SemanticsNodeId(arg0_), SemanticsNodeId(arg1_)};
}
static auto MakeBinaryOperatorAdd(ParseTree::Node parse_node,
SemanticsNodeId type, SemanticsNodeId lhs,
SemanticsNodeId rhs) -> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::BinaryOperatorAdd, type,
lhs.index, rhs.index);
}
auto GetAsBinaryOperatorAdd() const
-> std::pair<SemanticsNodeId, SemanticsNodeId> {
CARBON_CHECK(kind_ == SemanticsNodeKind::BinaryOperatorAdd);
return {SemanticsNodeId(arg0_), SemanticsNodeId(arg1_)};
}
static auto MakeBindName(ParseTree::Node parse_node, SemanticsNodeId type,
SemanticsStringId name, SemanticsNodeId node)
-> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::BindName, type,
name.index, node.index);
}
auto GetAsBindName() const -> std::pair<SemanticsStringId, SemanticsNodeId> {
CARBON_CHECK(kind_ == SemanticsNodeKind::BindName);
return {SemanticsStringId(arg0_), SemanticsNodeId(arg1_)};
}
static auto MakeBuiltin(SemanticsBuiltinKind builtin_kind,
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());
}
auto GetAsBuiltin() const -> SemanticsBuiltinKind {
CARBON_CHECK(kind_ == SemanticsNodeKind::Builtin);
return SemanticsBuiltinKind::FromInt(arg0_);
}
static auto MakeCodeBlock(ParseTree::Node parse_node,
SemanticsNodeBlockId node_block) -> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::CodeBlock,
SemanticsNodeId(), node_block.index);
}
auto GetAsCodeBlock() const -> SemanticsNodeBlockId {
CARBON_CHECK(kind_ == SemanticsNodeKind::CodeBlock);
return SemanticsNodeBlockId(arg0_);
}
static auto MakeCrossReference(SemanticsNodeId type,
SemanticsCrossReferenceIRId ir,
SemanticsNodeId node) -> SemanticsNode {
return SemanticsNode(ParseTree::Node::MakeInvalid(),
SemanticsNodeKind::CrossReference, type, ir.index,
node.index);
}
auto GetAsCrossReference() const
-> std::pair<SemanticsCrossReferenceIRId, SemanticsNodeBlockId> {
CARBON_CHECK(kind_ == SemanticsNodeKind::CrossReference);
return {SemanticsCrossReferenceIRId(arg0_), SemanticsNodeBlockId(arg1_)};
}
// TODO: The signature should be added as a parameter.
static auto MakeFunctionDeclaration(ParseTree::Node parse_node)
-> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::FunctionDeclaration,
SemanticsNodeId());
}
auto GetAsFunctionDeclaration() const -> NoArgs {
CARBON_CHECK(kind_ == SemanticsNodeKind::FunctionDeclaration);
return {};
}
static auto MakeFunctionDefinition(ParseTree::Node parse_node,
SemanticsNodeId decl,
SemanticsNodeBlockId node_block)
-> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::FunctionDefinition,
SemanticsNodeId(), decl.index, node_block.index);
}
auto GetAsFunctionDefinition() const
-> std::pair<SemanticsNodeId, SemanticsNodeBlockId> {
CARBON_CHECK(kind_ == SemanticsNodeKind::FunctionDefinition);
return {SemanticsNodeId(arg0_), SemanticsNodeBlockId(arg1_)};
}
static auto MakeIntegerLiteral(ParseTree::Node parse_node,
SemanticsIntegerLiteralId integer)
-> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::IntegerLiteral,
SemanticsNodeId::MakeBuiltinReference(
SemanticsBuiltinKind::IntegerType),
integer.index);
}
auto GetAsIntegerLiteral() const -> SemanticsIntegerLiteralId {
CARBON_CHECK(kind_ == SemanticsNodeKind::IntegerLiteral);
return SemanticsIntegerLiteralId(arg0_);
}
static auto MakeRealLiteral(ParseTree::Node parse_node) -> SemanticsNode {
return SemanticsNode(
parse_node, SemanticsNodeKind::RealLiteral,
SemanticsNodeId::MakeBuiltinReference(SemanticsBuiltinKind::RealType));
}
auto GetAsRealLiteral() const -> NoArgs {
CARBON_CHECK(kind_ == SemanticsNodeKind::RealLiteral);
return {};
}
static auto MakeReturn(ParseTree::Node parse_node) -> SemanticsNode {
// The actual type is `()`. However, code dealing with `return;` should
// understand the type without checking, so it's not necessary but could be
// specified if needed.
return SemanticsNode(parse_node, SemanticsNodeKind::Return,
SemanticsNodeId());
}
auto GetAsReturn() const -> NoArgs {
CARBON_CHECK(kind_ == SemanticsNodeKind::Return);
return {};
}
static auto MakeReturnExpression(ParseTree::Node parse_node,
SemanticsNodeId type, SemanticsNodeId expr)
-> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::ReturnExpression, type,
expr.index);
}
auto GetAsReturnExpression() const -> SemanticsNodeId {
CARBON_CHECK(kind_ == SemanticsNodeKind::ReturnExpression);
return SemanticsNodeId(arg0_);
}
static auto MakeVarStorage(ParseTree::Node parse_node, SemanticsNodeId type)
-> SemanticsNode {
return SemanticsNode(parse_node, SemanticsNodeKind::VarStorage, type);
}
auto GetAsVarStorage() const -> NoArgs {
CARBON_CHECK(kind_ == SemanticsNodeKind::VarStorage);
return NoArgs();
}
SemanticsNode()
: SemanticsNode(ParseTree::Node(), SemanticsNodeKind::Invalid,
SemanticsNodeId()) {}
auto parse_node() const -> ParseTree::Node { return parse_node_; }
auto kind() const -> SemanticsNodeKind { return kind_; }
auto type() const -> SemanticsNodeId { return type_; }
auto Print(llvm::raw_ostream& out) const -> void;
private:
explicit SemanticsNode(ParseTree::Node parse_node, SemanticsNodeKind kind,
SemanticsNodeId type, int32_t arg0 = -1,
int32_t arg1 = -1)
: parse_node_(parse_node),
kind_(kind),
type_(type),
arg0_(arg0),
arg1_(arg1) {}
ParseTree::Node parse_node_;
SemanticsNodeKind kind_;
SemanticsNodeId type_;
int32_t arg0_;
int32_t arg1_;
};
// TODO: This is currently 20 bytes because we sometimes have 2 arguments for a
// pair of SemanticsNodes. However, SemanticsNodeKind is 1 byte; if args
// were 3.5 bytes, we could potentially shrink SemanticsNode by 4 bytes. This
// may be worth investigating further.
static_assert(sizeof(SemanticsNode) == 20, "Unexpected SemanticsNode size");
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_H_