semantics_node.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_H_
  6. #include <cstdint>
  7. #include "common/check.h"
  8. #include "common/ostream.h"
  9. #include "toolchain/base/index_base.h"
  10. #include "toolchain/parser/parse_tree.h"
  11. #include "toolchain/semantics/semantics_builtin_kind.h"
  12. #include "toolchain/semantics/semantics_node_kind.h"
  13. namespace Carbon::SemIR {
  14. // The ID of a node.
  15. struct NodeId : public IndexBase, public Printable<NodeId> {
  16. // An explicitly invalid node ID.
  17. static const NodeId Invalid;
  18. // Builtin node IDs.
  19. #define CARBON_SEMANTICS_BUILTIN_KIND_NAME(Name) \
  20. static const NodeId Builtin##Name;
  21. #include "toolchain/semantics/semantics_builtin_kind.def"
  22. using IndexBase::IndexBase;
  23. auto Print(llvm::raw_ostream& out) const -> void {
  24. out << "node";
  25. if (!is_valid()) {
  26. IndexBase::Print(out);
  27. } else if (index < BuiltinKind::ValidCount) {
  28. out << BuiltinKind::FromInt(index);
  29. } else {
  30. // Use the `+` as a small reminder that this is a delta, rather than an
  31. // absolute index.
  32. out << "+" << index - BuiltinKind::ValidCount;
  33. }
  34. }
  35. };
  36. constexpr NodeId NodeId::Invalid = NodeId(NodeId::InvalidIndex);
  37. // Uses the cross-reference node ID for a builtin. This relies on File
  38. // guarantees for builtin cross-reference placement.
  39. #define CARBON_SEMANTICS_BUILTIN_KIND_NAME(Name) \
  40. constexpr NodeId NodeId::Builtin##Name = NodeId(BuiltinKind::Name.AsInt());
  41. #include "toolchain/semantics/semantics_builtin_kind.def"
  42. // The ID of a function.
  43. struct FunctionId : public IndexBase, public Printable<FunctionId> {
  44. using IndexBase::IndexBase;
  45. auto Print(llvm::raw_ostream& out) const -> void {
  46. out << "function";
  47. IndexBase::Print(out);
  48. }
  49. };
  50. // The ID of a cross-referenced IR.
  51. struct CrossReferenceIRId : public IndexBase,
  52. public Printable<CrossReferenceIRId> {
  53. using IndexBase::IndexBase;
  54. auto Print(llvm::raw_ostream& out) const -> void {
  55. out << "ir";
  56. IndexBase::Print(out);
  57. }
  58. };
  59. // A boolean value.
  60. struct BoolValue : public IndexBase, public Printable<BoolValue> {
  61. static const BoolValue False;
  62. static const BoolValue True;
  63. using IndexBase::IndexBase;
  64. auto Print(llvm::raw_ostream& out) const -> void {
  65. switch (index) {
  66. case 0:
  67. out << "false";
  68. break;
  69. case 1:
  70. out << "true";
  71. break;
  72. default:
  73. CARBON_FATAL() << "Invalid bool value " << index;
  74. }
  75. }
  76. };
  77. constexpr BoolValue BoolValue::False = BoolValue(0);
  78. constexpr BoolValue BoolValue::True = BoolValue(1);
  79. // The ID of an integer literal.
  80. struct IntegerLiteralId : public IndexBase, public Printable<IntegerLiteralId> {
  81. using IndexBase::IndexBase;
  82. auto Print(llvm::raw_ostream& out) const -> void {
  83. out << "int";
  84. IndexBase::Print(out);
  85. }
  86. };
  87. // The ID of a name scope.
  88. struct NameScopeId : public IndexBase, public Printable<NameScopeId> {
  89. // An explicitly invalid ID.
  90. static const NameScopeId Invalid;
  91. using IndexBase::IndexBase;
  92. auto Print(llvm::raw_ostream& out) const -> void {
  93. out << "name_scope";
  94. IndexBase::Print(out);
  95. }
  96. };
  97. constexpr NameScopeId NameScopeId::Invalid =
  98. NameScopeId(NameScopeId::InvalidIndex);
  99. // The ID of a node block.
  100. struct NodeBlockId : public IndexBase, public Printable<NodeBlockId> {
  101. // All File instances must provide the 0th node block as empty.
  102. static const NodeBlockId Empty;
  103. // An explicitly invalid ID.
  104. static const NodeBlockId Invalid;
  105. // An ID for unreachable code.
  106. static const NodeBlockId Unreachable;
  107. using IndexBase::IndexBase;
  108. auto Print(llvm::raw_ostream& out) const -> void {
  109. if (index == Unreachable.index) {
  110. out << "unreachable";
  111. } else {
  112. out << "block";
  113. IndexBase::Print(out);
  114. }
  115. }
  116. };
  117. constexpr NodeBlockId NodeBlockId::Empty = NodeBlockId(0);
  118. constexpr NodeBlockId NodeBlockId::Invalid =
  119. NodeBlockId(NodeBlockId::InvalidIndex);
  120. constexpr NodeBlockId NodeBlockId::Unreachable =
  121. NodeBlockId(NodeBlockId::InvalidIndex - 1);
  122. // The ID of a real literal.
  123. struct RealLiteralId : public IndexBase, public Printable<RealLiteralId> {
  124. using IndexBase::IndexBase;
  125. auto Print(llvm::raw_ostream& out) const -> void {
  126. out << "real";
  127. IndexBase::Print(out);
  128. }
  129. };
  130. // The ID of a string.
  131. struct StringId : public IndexBase, public Printable<StringId> {
  132. using IndexBase::IndexBase;
  133. auto Print(llvm::raw_ostream& out) const -> void {
  134. out << "str";
  135. IndexBase::Print(out);
  136. }
  137. };
  138. // The ID of a node block.
  139. struct TypeId : public IndexBase, public Printable<TypeId> {
  140. // The builtin TypeType.
  141. static const TypeId TypeType;
  142. // The builtin Error.
  143. static const TypeId Error;
  144. // An explicitly invalid ID.
  145. static const TypeId Invalid;
  146. using IndexBase::IndexBase;
  147. auto Print(llvm::raw_ostream& out) const -> void {
  148. out << "type";
  149. if (index == TypeType.index) {
  150. out << "TypeType";
  151. } else if (index == Error.index) {
  152. out << "Error";
  153. } else {
  154. IndexBase::Print(out);
  155. }
  156. }
  157. };
  158. constexpr TypeId TypeId::TypeType = TypeId(TypeId::InvalidIndex - 2);
  159. constexpr TypeId TypeId::Error = TypeId(TypeId::InvalidIndex - 1);
  160. constexpr TypeId TypeId::Invalid = TypeId(TypeId::InvalidIndex);
  161. // The ID of a type block.
  162. struct TypeBlockId : public IndexBase, public Printable<TypeBlockId> {
  163. using IndexBase::IndexBase;
  164. auto Print(llvm::raw_ostream& out) const -> void {
  165. out << "typeBlock";
  166. IndexBase::Print(out);
  167. }
  168. };
  169. // An index for member access.
  170. struct MemberIndex : public IndexBase, public Printable<MemberIndex> {
  171. using IndexBase::IndexBase;
  172. auto Print(llvm::raw_ostream& out) const -> void {
  173. out << "member";
  174. IndexBase::Print(out);
  175. }
  176. };
  177. // The standard structure for Node. This is trying to provide a minimal
  178. // amount of information for a node:
  179. //
  180. // - parse_node for error placement.
  181. // - kind for run-time logic when the input Kind is unknown.
  182. // - type_id for quick type checking.
  183. // - Up to two Kind-specific members.
  184. //
  185. // For each Kind in NodeKind, a typical flow looks like:
  186. //
  187. // - Create a `Node` using `Node::Kind::Make()`
  188. // - Access cross-Kind members using `node.type_id()` and similar.
  189. // - Access Kind-specific members using `node.GetAsKind()`, which depending on
  190. // the number of members will return one of NoArgs, a single value, or a
  191. // `std::pair` of values.
  192. // - Using the wrong `node.GetAsKind()` is a programming error, and should
  193. // CHECK-fail in debug modes (opt may too, but it's not an API guarantee).
  194. //
  195. // Internally, each Kind uses the `Factory*` types to provide a boilerplate
  196. // `Make` and `Get` methods.
  197. class Node : public Printable<Node> {
  198. public:
  199. struct NoArgs {};
  200. // Factory base classes are private, then used for public classes. This class
  201. // has two public and two private sections to prevent accidents.
  202. private:
  203. // Provides Make and Get to support 0, 1, or 2 arguments for a Node.
  204. // These are protected so that child factories can opt in to what pieces they
  205. // want to use.
  206. template <NodeKind::RawEnumType Kind, typename... ArgTypes>
  207. class FactoryBase {
  208. protected:
  209. static auto Make(Parse::Node parse_node, TypeId type_id,
  210. ArgTypes... arg_ids) -> Node {
  211. return Node(parse_node, NodeKind::Create(Kind), type_id,
  212. arg_ids.index...);
  213. }
  214. static auto Get(Node node) {
  215. struct Unused {};
  216. return GetImpl<ArgTypes..., Unused>(node);
  217. }
  218. private:
  219. // GetImpl handles the different return types based on ArgTypes.
  220. template <typename Arg0Type, typename Arg1Type, typename>
  221. static auto GetImpl(Node node) -> std::pair<Arg0Type, Arg1Type> {
  222. CARBON_CHECK(node.kind() == Kind);
  223. return {Arg0Type(node.arg0_), Arg1Type(node.arg1_)};
  224. }
  225. template <typename Arg0Type, typename>
  226. static auto GetImpl(Node node) -> Arg0Type {
  227. CARBON_CHECK(node.kind() == Kind);
  228. return Arg0Type(node.arg0_);
  229. }
  230. template <typename>
  231. static auto GetImpl(Node node) -> NoArgs {
  232. CARBON_CHECK(node.kind() == Kind);
  233. return NoArgs();
  234. }
  235. };
  236. // Provide Get along with a Make that requires a type.
  237. template <NodeKind::RawEnumType Kind, typename... ArgTypes>
  238. class Factory : public FactoryBase<Kind, ArgTypes...> {
  239. public:
  240. using FactoryBase<Kind, ArgTypes...>::Make;
  241. using FactoryBase<Kind, ArgTypes...>::Get;
  242. };
  243. // Provides Get along with a Make that assumes the node doesn't produce a
  244. // typed value.
  245. template <NodeKind::RawEnumType Kind, typename... ArgTypes>
  246. class FactoryNoType : public FactoryBase<Kind, ArgTypes...> {
  247. public:
  248. static auto Make(Parse::Node parse_node, ArgTypes... args) {
  249. return FactoryBase<Kind, ArgTypes...>::Make(parse_node, TypeId::Invalid,
  250. args...);
  251. }
  252. using FactoryBase<Kind, ArgTypes...>::Get;
  253. };
  254. public:
  255. // Invalid is in the NodeKind enum, but should never be used.
  256. class Invalid {
  257. public:
  258. static auto Get(Node /*node*/) -> Node::NoArgs {
  259. CARBON_FATAL() << "Invalid access";
  260. }
  261. };
  262. using AddressOf = Node::Factory<NodeKind::AddressOf, NodeId /*lvalue_id*/>;
  263. using ArrayIndex =
  264. Factory<NodeKind::ArrayIndex, NodeId /*array_id*/, NodeId /*index*/>;
  265. using ArrayType = Node::Factory<NodeKind::ArrayType, NodeId /*bound_node_id*/,
  266. TypeId /*array_element_type_id*/>;
  267. using ArrayValue = Factory<NodeKind::ArrayValue, NodeId /*tuple_value_id*/>;
  268. using Assign = Node::FactoryNoType<NodeKind::Assign, NodeId /*lhs_id*/,
  269. NodeId /*rhs_id*/>;
  270. using BinaryOperatorAdd = Node::Factory<NodeKind::BinaryOperatorAdd,
  271. NodeId /*lhs_id*/, NodeId /*rhs_id*/>;
  272. using BindValue = Factory<NodeKind::BindValue, NodeId /*value_id*/>;
  273. using BlockArg = Factory<NodeKind::BlockArg, NodeBlockId /*block_id*/>;
  274. using BoolLiteral = Factory<NodeKind::BoolLiteral, BoolValue /*value*/>;
  275. using Branch = FactoryNoType<NodeKind::Branch, NodeBlockId /*target_id*/>;
  276. using BranchIf = FactoryNoType<NodeKind::BranchIf, NodeBlockId /*target_id*/,
  277. NodeId /*cond_id*/>;
  278. using BranchWithArg =
  279. FactoryNoType<NodeKind::BranchWithArg, NodeBlockId /*target_id*/,
  280. NodeId /*arg*/>;
  281. class Builtin {
  282. public:
  283. static auto Make(BuiltinKind builtin_kind, TypeId type_id) -> Node {
  284. // Builtins won't have a Parse::Tree node associated, so we provide the
  285. // default invalid one.
  286. // This can't use the standard Make function because of the `AsInt()` cast
  287. // instead of `.index`.
  288. return Node(Parse::Node::Invalid, NodeKind::Builtin, type_id,
  289. builtin_kind.AsInt());
  290. }
  291. static auto Get(Node node) -> BuiltinKind {
  292. return BuiltinKind::FromInt(node.arg0_);
  293. }
  294. };
  295. using Call = Factory<NodeKind::Call, NodeBlockId /*refs_id*/,
  296. FunctionId /*function_id*/>;
  297. using ConstType = Factory<NodeKind::ConstType, TypeId /*inner_id*/>;
  298. class CrossReference
  299. : public FactoryBase<NodeKind::CrossReference,
  300. CrossReferenceIRId /*ir_id*/, NodeId /*node_id*/> {
  301. public:
  302. static auto Make(TypeId type_id, CrossReferenceIRId ir_id, NodeId node_id)
  303. -> Node {
  304. // A node's parse tree node must refer to a node in the current parse
  305. // tree. This cannot use the cross-referenced node's parse tree node
  306. // because it will be in a different parse tree.
  307. return FactoryBase::Make(Parse::Node::Invalid, type_id, ir_id, node_id);
  308. }
  309. using FactoryBase::Get;
  310. };
  311. using Dereference = Factory<NodeKind::Dereference, NodeId /*pointer_id*/>;
  312. using FunctionDeclaration =
  313. FactoryNoType<NodeKind::FunctionDeclaration, FunctionId /*function_id*/>;
  314. using IntegerLiteral =
  315. Factory<NodeKind::IntegerLiteral, IntegerLiteralId /*integer_id*/>;
  316. using Namespace =
  317. FactoryNoType<NodeKind::Namespace, NameScopeId /*name_scope_id*/>;
  318. using NoOp = FactoryNoType<NodeKind::NoOp>;
  319. using Parameter = Factory<NodeKind::Parameter, StringId /*name_id*/>;
  320. using PointerType = Factory<NodeKind::PointerType, TypeId /*pointee_id*/>;
  321. using RealLiteral = Factory<NodeKind::RealLiteral, RealLiteralId /*real_id*/>;
  322. using Return = FactoryNoType<NodeKind::Return>;
  323. using ReturnExpression =
  324. FactoryNoType<NodeKind::ReturnExpression, NodeId /*expr_id*/>;
  325. using StringLiteral =
  326. Factory<NodeKind::StringLiteral, StringId /*string_id*/>;
  327. using StructAccess = Factory<NodeKind::StructAccess, NodeId /*struct_id*/,
  328. MemberIndex /*ref_index*/>;
  329. using StructType = Factory<NodeKind::StructType, NodeBlockId /*refs_id*/>;
  330. using StructTypeField =
  331. FactoryNoType<NodeKind::StructTypeField, StringId /*name_id*/,
  332. TypeId /*type_id*/>;
  333. using StructValue = Factory<NodeKind::StructValue, NodeBlockId /*refs_id*/>;
  334. using StubReference = Factory<NodeKind::StubReference, NodeId /*node_id*/>;
  335. using Temporary =
  336. Factory<NodeKind::Temporary, NodeId /*storage_id*/, NodeId /*init_id*/>;
  337. using TemporaryStorage = Factory<NodeKind::TemporaryStorage>;
  338. using TupleIndex =
  339. Factory<NodeKind::TupleIndex, NodeId /*tuple_id*/, NodeId /*index*/>;
  340. using TupleType = Factory<NodeKind::TupleType, TypeBlockId /*refs_id*/>;
  341. using TupleValue = Factory<NodeKind::TupleValue, NodeBlockId /*refs_id*/>;
  342. using UnaryOperatorNot =
  343. Factory<NodeKind::UnaryOperatorNot, NodeId /*operand_id*/>;
  344. using VarStorage = Factory<NodeKind::VarStorage, StringId /*name_id*/>;
  345. explicit Node()
  346. : Node(Parse::Node::Invalid, NodeKind::Invalid, TypeId::Invalid) {}
  347. // Provide `node.GetAsKind()` as an instance method for all kinds, essentially
  348. // an alias for`Node::Kind::Get(node)`.
  349. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  350. auto GetAs##Name() const { return Name::Get(*this); }
  351. #include "toolchain/semantics/semantics_node_kind.def"
  352. auto parse_node() const -> Parse::Node { return parse_node_; }
  353. auto kind() const -> NodeKind { return kind_; }
  354. // Gets the type of the value produced by evaluating this node.
  355. auto type_id() const -> TypeId { return type_id_; }
  356. auto Print(llvm::raw_ostream& out) const -> void;
  357. private:
  358. // Builtins have peculiar construction, so they are a friend rather than using
  359. // a factory base class.
  360. friend struct NodeForBuiltin;
  361. explicit Node(Parse::Node parse_node, NodeKind kind, TypeId type_id,
  362. int32_t arg0 = NodeId::InvalidIndex,
  363. int32_t arg1 = NodeId::InvalidIndex)
  364. : parse_node_(parse_node),
  365. kind_(kind),
  366. type_id_(type_id),
  367. arg0_(arg0),
  368. arg1_(arg1) {}
  369. Parse::Node parse_node_;
  370. NodeKind kind_;
  371. TypeId type_id_;
  372. // Use GetAsKind to access arg0 and arg1.
  373. int32_t arg0_;
  374. int32_t arg1_;
  375. };
  376. // TODO: This is currently 20 bytes because we sometimes have 2 arguments for a
  377. // pair of Nodes. However, NodeKind is 1 byte; if args
  378. // were 3.5 bytes, we could potentially shrink Node by 4 bytes. This
  379. // may be worth investigating further.
  380. static_assert(sizeof(Node) == 20, "Unexpected Node size");
  381. // Provides base support for use of Id types as DenseMap/DenseSet keys.
  382. // Instantiated below.
  383. template <typename Id>
  384. struct IdMapInfo {
  385. static inline auto getEmptyKey() -> Id {
  386. return Id(llvm::DenseMapInfo<int32_t>::getEmptyKey());
  387. }
  388. static inline auto getTombstoneKey() -> Id {
  389. return Id(llvm::DenseMapInfo<int32_t>::getTombstoneKey());
  390. }
  391. static auto getHashValue(const Id& val) -> unsigned {
  392. return llvm::DenseMapInfo<int32_t>::getHashValue(val.index);
  393. }
  394. static auto isEqual(const Id& lhs, const Id& rhs) -> bool {
  395. return lhs == rhs;
  396. }
  397. };
  398. } // namespace Carbon::SemIR
  399. // Support use of Id types as DenseMap/DenseSet keys.
  400. template <>
  401. struct llvm::DenseMapInfo<Carbon::SemIR::NodeBlockId>
  402. : public Carbon::SemIR::IdMapInfo<Carbon::SemIR::NodeBlockId> {};
  403. template <>
  404. struct llvm::DenseMapInfo<Carbon::SemIR::NodeId>
  405. : public Carbon::SemIR::IdMapInfo<Carbon::SemIR::NodeId> {};
  406. template <>
  407. struct llvm::DenseMapInfo<Carbon::SemIR::StringId>
  408. : public Carbon::SemIR::IdMapInfo<Carbon::SemIR::StringId> {};
  409. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_H_