semantics_node.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 {
  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 {
  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. using IndexBase::IndexBase;
  53. auto Print(llvm::raw_ostream& out) const -> void {
  54. out << "ir";
  55. IndexBase::Print(out);
  56. }
  57. };
  58. // A boolean value.
  59. struct BoolValue : public IndexBase {
  60. static const BoolValue False;
  61. static const BoolValue True;
  62. using IndexBase::IndexBase;
  63. auto Print(llvm::raw_ostream& out) const -> void {
  64. switch (index) {
  65. case 0:
  66. out << "false";
  67. break;
  68. case 1:
  69. out << "true";
  70. break;
  71. default:
  72. CARBON_FATAL() << "Invalid bool value " << index;
  73. }
  74. }
  75. };
  76. constexpr BoolValue BoolValue::False = BoolValue(0);
  77. constexpr BoolValue BoolValue::True = BoolValue(1);
  78. // The ID of an integer literal.
  79. struct IntegerLiteralId : public IndexBase {
  80. using IndexBase::IndexBase;
  81. auto Print(llvm::raw_ostream& out) const -> void {
  82. out << "int";
  83. IndexBase::Print(out);
  84. }
  85. };
  86. // The ID of a name scope.
  87. struct NameScopeId : public IndexBase {
  88. // An explicitly invalid ID.
  89. static const NameScopeId Invalid;
  90. using IndexBase::IndexBase;
  91. auto Print(llvm::raw_ostream& out) const -> void {
  92. out << "name_scope";
  93. IndexBase::Print(out);
  94. }
  95. };
  96. constexpr NameScopeId NameScopeId::Invalid =
  97. NameScopeId(NameScopeId::InvalidIndex);
  98. // The ID of a node block.
  99. struct NodeBlockId : public IndexBase {
  100. // All File instances must provide the 0th node block as empty.
  101. static const NodeBlockId Empty;
  102. // An explicitly invalid ID.
  103. static const NodeBlockId Invalid;
  104. // An ID for unreachable code.
  105. static const NodeBlockId Unreachable;
  106. using IndexBase::IndexBase;
  107. auto Print(llvm::raw_ostream& out) const -> void {
  108. if (index == Unreachable.index) {
  109. out << "unreachable";
  110. } else {
  111. out << "block";
  112. IndexBase::Print(out);
  113. }
  114. }
  115. };
  116. constexpr NodeBlockId NodeBlockId::Empty = NodeBlockId(0);
  117. constexpr NodeBlockId NodeBlockId::Invalid =
  118. NodeBlockId(NodeBlockId::InvalidIndex);
  119. constexpr NodeBlockId NodeBlockId::Unreachable =
  120. NodeBlockId(NodeBlockId::InvalidIndex - 1);
  121. // The ID of a real literal.
  122. struct RealLiteralId : public IndexBase {
  123. using IndexBase::IndexBase;
  124. auto Print(llvm::raw_ostream& out) const -> void {
  125. out << "real";
  126. IndexBase::Print(out);
  127. }
  128. };
  129. // The ID of a string.
  130. struct StringId : public IndexBase {
  131. using IndexBase::IndexBase;
  132. auto Print(llvm::raw_ostream& out) const -> void {
  133. out << "str";
  134. IndexBase::Print(out);
  135. }
  136. };
  137. // The ID of a node block.
  138. struct TypeId : public IndexBase {
  139. // The builtin TypeType.
  140. static const TypeId TypeType;
  141. // The builtin Error.
  142. static const TypeId Error;
  143. // An explicitly invalid ID.
  144. static const TypeId Invalid;
  145. using IndexBase::IndexBase;
  146. auto Print(llvm::raw_ostream& out) const -> void {
  147. out << "type";
  148. if (index == TypeType.index) {
  149. out << "TypeType";
  150. } else if (index == Error.index) {
  151. out << "Error";
  152. } else {
  153. IndexBase::Print(out);
  154. }
  155. }
  156. };
  157. constexpr TypeId TypeId::TypeType = TypeId(TypeId::InvalidIndex - 2);
  158. constexpr TypeId TypeId::Error = TypeId(TypeId::InvalidIndex - 1);
  159. constexpr TypeId TypeId::Invalid = TypeId(TypeId::InvalidIndex);
  160. // The ID of a type block.
  161. struct TypeBlockId : public IndexBase {
  162. using IndexBase::IndexBase;
  163. auto Print(llvm::raw_ostream& out) const -> void {
  164. out << "typeBlock";
  165. IndexBase::Print(out);
  166. }
  167. };
  168. // An index for member access.
  169. struct MemberIndex : public IndexBase {
  170. using IndexBase::IndexBase;
  171. auto Print(llvm::raw_ostream& out) const -> void {
  172. out << "member";
  173. IndexBase::Print(out);
  174. }
  175. };
  176. // The standard structure for Node. This is trying to provide a minimal
  177. // amount of information for a node:
  178. //
  179. // - parse_node for error placement.
  180. // - kind for run-time logic when the input Kind is unknown.
  181. // - type_id for quick type checking.
  182. // - Up to two Kind-specific members.
  183. //
  184. // For each Kind in NodeKind, a typical flow looks like:
  185. //
  186. // - Create a `Node` using `Node::Kind::Make()`
  187. // - Access cross-Kind members using `node.type_id()` and similar.
  188. // - Access Kind-specific members using `node.GetAsKind()`, which depending on
  189. // the number of members will return one of NoArgs, a single value, or a
  190. // `std::pair` of values.
  191. // - Using the wrong `node.GetAsKind()` is a programming error, and should
  192. // CHECK-fail in debug modes (opt may too, but it's not an API guarantee).
  193. //
  194. // Internally, each Kind uses the `Factory*` types to provide a boilerplate
  195. // `Make` and `Get` methods.
  196. class Node {
  197. public:
  198. struct NoArgs {};
  199. // Factory base classes are private, then used for public classes. This class
  200. // has two public and two private sections to prevent accidents.
  201. private:
  202. // Factory templates need to use the raw enum instead of the class wrapper.
  203. using KindTemplateEnum = Internal::SemanticsNodeKindRawEnum;
  204. // Provides Make and Get to support 0, 1, or 2 arguments for a Node.
  205. // These are protected so that child factories can opt in to what pieces they
  206. // want to use.
  207. template <KindTemplateEnum Kind, typename... ArgTypes>
  208. class FactoryBase {
  209. protected:
  210. static auto Make(ParseTree::Node parse_node, TypeId type_id,
  211. ArgTypes... arg_ids) -> Node {
  212. return Node(parse_node, NodeKind::Create(Kind), type_id,
  213. arg_ids.index...);
  214. }
  215. static auto Get(Node node) {
  216. struct Unused {};
  217. return GetImpl<ArgTypes..., Unused>(node);
  218. }
  219. private:
  220. // GetImpl handles the different return types based on ArgTypes.
  221. template <typename Arg0Type, typename Arg1Type, typename>
  222. static auto GetImpl(Node node) -> std::pair<Arg0Type, Arg1Type> {
  223. CARBON_CHECK(node.kind() == Kind);
  224. return {Arg0Type(node.arg0_), Arg1Type(node.arg1_)};
  225. }
  226. template <typename Arg0Type, typename>
  227. static auto GetImpl(Node node) -> Arg0Type {
  228. CARBON_CHECK(node.kind() == Kind);
  229. return Arg0Type(node.arg0_);
  230. }
  231. template <typename>
  232. static auto GetImpl(Node node) -> NoArgs {
  233. CARBON_CHECK(node.kind() == Kind);
  234. return NoArgs();
  235. }
  236. };
  237. // Provide Get along with a Make that requires a type.
  238. template <KindTemplateEnum Kind, typename... ArgTypes>
  239. class Factory : public FactoryBase<Kind, ArgTypes...> {
  240. public:
  241. using FactoryBase<Kind, ArgTypes...>::Make;
  242. using FactoryBase<Kind, ArgTypes...>::Get;
  243. };
  244. // Provides Get along with a Make that assumes the node doesn't produce a
  245. // typed value.
  246. template <KindTemplateEnum Kind, typename... ArgTypes>
  247. class FactoryNoType : public FactoryBase<Kind, ArgTypes...> {
  248. public:
  249. static auto Make(ParseTree::Node parse_node, ArgTypes... args) {
  250. return FactoryBase<Kind, ArgTypes...>::Make(parse_node, TypeId::Invalid,
  251. args...);
  252. }
  253. using FactoryBase<Kind, ArgTypes...>::Get;
  254. };
  255. public:
  256. // Invalid is in the NodeKind enum, but should never be used.
  257. class Invalid {
  258. public:
  259. static auto Get(Node /*node*/) -> Node::NoArgs {
  260. CARBON_FATAL() << "Invalid access";
  261. }
  262. };
  263. using AddressOf = Node::Factory<NodeKind::AddressOf, NodeId /*lvalue_id*/>;
  264. using ArrayIndex =
  265. Factory<NodeKind::ArrayIndex, NodeId /*array_id*/, NodeId /*index*/>;
  266. using ArrayType = Node::Factory<NodeKind::ArrayType, NodeId /*bound_node_id*/,
  267. TypeId /*array_element_type_id*/>;
  268. using ArrayValue = Factory<NodeKind::ArrayValue, NodeId /*tuple_value_id*/>;
  269. using Assign = Node::FactoryNoType<NodeKind::Assign, NodeId /*lhs_id*/,
  270. NodeId /*rhs_id*/>;
  271. using BinaryOperatorAdd = Node::Factory<NodeKind::BinaryOperatorAdd,
  272. NodeId /*lhs_id*/, NodeId /*rhs_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 ParseTree 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(ParseTree::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(ParseTree::Node::Invalid, type_id, ir_id,
  308. node_id);
  309. }
  310. using FactoryBase::Get;
  311. };
  312. using Dereference = Factory<NodeKind::Dereference, NodeId /*pointer_id*/>;
  313. using FunctionDeclaration =
  314. FactoryNoType<NodeKind::FunctionDeclaration, FunctionId /*function_id*/>;
  315. using IntegerLiteral =
  316. Factory<NodeKind::IntegerLiteral, IntegerLiteralId /*integer_id*/>;
  317. using Namespace =
  318. FactoryNoType<NodeKind::Namespace, NameScopeId /*name_scope_id*/>;
  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 TupleIndex =
  336. Factory<NodeKind::TupleIndex, NodeId /*tuple_id*/, NodeId /*index*/>;
  337. using TupleType = Factory<NodeKind::TupleType, TypeBlockId /*refs_id*/>;
  338. using TupleValue = Factory<NodeKind::TupleValue, NodeBlockId /*refs_id*/>;
  339. using UnaryOperatorNot =
  340. Factory<NodeKind::UnaryOperatorNot, NodeId /*operand_id*/>;
  341. using VarStorage = Factory<NodeKind::VarStorage, StringId /*name_id*/>;
  342. explicit Node()
  343. : Node(ParseTree::Node::Invalid, NodeKind::Invalid, TypeId::Invalid) {}
  344. // Provide `node.GetAsKind()` as an instance method for all kinds, essentially
  345. // an alias for`Node::Kind::Get(node)`.
  346. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  347. auto GetAs##Name() const { return Name::Get(*this); }
  348. #include "toolchain/semantics/semantics_node_kind.def"
  349. auto parse_node() const -> ParseTree::Node { return parse_node_; }
  350. auto kind() const -> NodeKind { return kind_; }
  351. // Gets the type of the value produced by evaluating this node.
  352. auto type_id() const -> TypeId { return type_id_; }
  353. friend auto operator<<(llvm::raw_ostream& out, const Node& node)
  354. -> llvm::raw_ostream&;
  355. LLVM_DUMP_METHOD void Dump() const { llvm::errs() << *this; }
  356. private:
  357. // Builtins have peculiar construction, so they are a friend rather than using
  358. // a factory base class.
  359. friend struct NodeForBuiltin;
  360. explicit Node(ParseTree::Node parse_node, NodeKind kind, TypeId type_id,
  361. int32_t arg0 = NodeId::InvalidIndex,
  362. int32_t arg1 = NodeId::InvalidIndex)
  363. : parse_node_(parse_node),
  364. kind_(kind),
  365. type_id_(type_id),
  366. arg0_(arg0),
  367. arg1_(arg1) {}
  368. ParseTree::Node parse_node_;
  369. NodeKind kind_;
  370. TypeId type_id_;
  371. // Use GetAsKind to access arg0 and arg1.
  372. int32_t arg0_;
  373. int32_t arg1_;
  374. };
  375. // TODO: This is currently 20 bytes because we sometimes have 2 arguments for a
  376. // pair of Nodes. However, NodeKind is 1 byte; if args
  377. // were 3.5 bytes, we could potentially shrink Node by 4 bytes. This
  378. // may be worth investigating further.
  379. static_assert(sizeof(Node) == 20, "Unexpected Node size");
  380. // Provides base support for use of Id types as DenseMap/DenseSet keys.
  381. // Instantiated below.
  382. template <typename Id>
  383. struct IdMapInfo {
  384. static inline auto getEmptyKey() -> Id {
  385. return Id(llvm::DenseMapInfo<int32_t>::getEmptyKey());
  386. }
  387. static inline auto getTombstoneKey() -> Id {
  388. return Id(llvm::DenseMapInfo<int32_t>::getTombstoneKey());
  389. }
  390. static auto getHashValue(const Id& val) -> unsigned {
  391. return llvm::DenseMapInfo<int32_t>::getHashValue(val.index);
  392. }
  393. static auto isEqual(const Id& lhs, const Id& rhs) -> bool {
  394. return lhs == rhs;
  395. }
  396. };
  397. } // namespace Carbon::SemIR
  398. // Support use of Id types as DenseMap/DenseSet keys.
  399. template <>
  400. struct llvm::DenseMapInfo<Carbon::SemIR::NodeBlockId>
  401. : public Carbon::SemIR::IdMapInfo<Carbon::SemIR::NodeBlockId> {};
  402. template <>
  403. struct llvm::DenseMapInfo<Carbon::SemIR::NodeId>
  404. : public Carbon::SemIR::IdMapInfo<Carbon::SemIR::NodeId> {};
  405. template <>
  406. struct llvm::DenseMapInfo<Carbon::SemIR::StringId>
  407. : public Carbon::SemIR::IdMapInfo<Carbon::SemIR::StringId> {};
  408. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_H_