semantics_ir.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_IR_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "llvm/ADT/StringMap.h"
  8. #include "toolchain/parser/parse_tree.h"
  9. #include "toolchain/semantics/semantics_node.h"
  10. namespace Carbon::Testing {
  11. class SemanticsIRForTest;
  12. } // namespace Carbon::Testing
  13. namespace Carbon {
  14. // A call.
  15. struct SemanticsCall {
  16. auto Print(llvm::raw_ostream& out) const -> void {
  17. out << "{arg_ir: " << arg_ir_id << ", arg_refs: " << arg_refs_id << "}";
  18. }
  19. // The full IR for arguments.
  20. SemanticsNodeBlockId arg_ir_id;
  21. // A block containing a single reference node per argument.
  22. SemanticsNodeBlockId arg_refs_id;
  23. };
  24. // A callable object.
  25. struct SemanticsCallable {
  26. auto Print(llvm::raw_ostream& out) const -> void {
  27. out << "{param_ir: " << param_ir_id << ", param_refs: " << param_refs_id;
  28. if (return_type_id.is_valid()) {
  29. out << ", return_type: " << return_type_id;
  30. }
  31. out << "}";
  32. }
  33. // The full IR for parameters.
  34. SemanticsNodeBlockId param_ir_id;
  35. // A block containing a single reference node per parameter.
  36. SemanticsNodeBlockId param_refs_id;
  37. // The return type. This will be invalid if the return type wasn't specified.
  38. // The IR corresponding to the return type will be in a node block.
  39. SemanticsNodeId return_type_id;
  40. };
  41. struct SemanticsRealLiteral {
  42. auto Print(llvm::raw_ostream& out) const -> void {
  43. out << "{mantissa: " << mantissa << ", exponent: " << exponent
  44. << ", is_decimal: " << is_decimal << "}";
  45. }
  46. llvm::APInt mantissa;
  47. llvm::APInt exponent;
  48. // If false, the value is mantissa * 2^exponent.
  49. // If true, the value is mantissa * 10^exponent.
  50. bool is_decimal;
  51. };
  52. // Provides semantic analysis on a ParseTree.
  53. class SemanticsIR {
  54. public:
  55. // Produces the builtins.
  56. static auto MakeBuiltinIR() -> SemanticsIR;
  57. // Adds the IR for the provided ParseTree.
  58. static auto MakeFromParseTree(const SemanticsIR& builtin_ir,
  59. const TokenizedBuffer& tokens,
  60. const ParseTree& parse_tree,
  61. DiagnosticConsumer& consumer,
  62. llvm::raw_ostream* vlog_stream) -> SemanticsIR;
  63. // Prints the full IR.
  64. auto Print(llvm::raw_ostream& out) const -> void;
  65. // Returns the requested callable.
  66. auto GetCallable(SemanticsCallableId callable_id) const -> SemanticsCallable {
  67. return callables_[callable_id.index];
  68. }
  69. // Returns the requested integer literal.
  70. auto GetIntegerLiteral(SemanticsIntegerLiteralId int_id) const
  71. -> const llvm::APInt& {
  72. return integer_literals_[int_id.index];
  73. }
  74. // Returns the requested node.
  75. auto GetNode(SemanticsNodeId node_id) const -> SemanticsNode {
  76. return nodes_[node_id.index];
  77. }
  78. // Returns the requested node block.
  79. auto GetNodeBlock(SemanticsNodeBlockId block_id) const
  80. -> const llvm::SmallVector<SemanticsNodeId>& {
  81. return node_blocks_[block_id.index];
  82. }
  83. // Returns the requested string.
  84. auto GetString(SemanticsStringId string_id) const -> llvm::StringRef {
  85. return strings_[string_id.index];
  86. }
  87. auto top_node_block_id() const -> SemanticsNodeBlockId {
  88. return top_node_block_id_;
  89. }
  90. // Returns true if there were errors creating the semantics IR.
  91. auto has_errors() const -> bool { return has_errors_; }
  92. private:
  93. friend class SemanticsParseTreeHandler;
  94. explicit SemanticsIR(const SemanticsIR* builtin_ir)
  95. : cross_reference_irs_({builtin_ir == nullptr ? this : builtin_ir}) {
  96. // For SemanticsNodeBlockId::Empty.
  97. node_blocks_.resize(1);
  98. }
  99. // Adds a call, returning an ID to reference it.
  100. auto AddCall(SemanticsCall call) -> SemanticsCallId {
  101. SemanticsCallId id(calls_.size());
  102. calls_.push_back(call);
  103. return id;
  104. }
  105. // Adds a callable, returning an ID to reference it.
  106. auto AddCallable(SemanticsCallable callable) -> SemanticsCallableId {
  107. SemanticsCallableId id(callables_.size());
  108. callables_.push_back(callable);
  109. return id;
  110. }
  111. // Adds an integer literal, returning an ID to reference it.
  112. auto AddIntegerLiteral(llvm::APInt integer_literal)
  113. -> SemanticsIntegerLiteralId {
  114. SemanticsIntegerLiteralId id(integer_literals_.size());
  115. integer_literals_.push_back(integer_literal);
  116. return id;
  117. }
  118. // Adds a node to a specified block, returning an ID to reference the node.
  119. auto AddNode(SemanticsNodeBlockId block_id, SemanticsNode node)
  120. -> SemanticsNodeId {
  121. SemanticsNodeId node_id(nodes_.size());
  122. nodes_.push_back(node);
  123. node_blocks_[block_id.index].push_back(node_id);
  124. return node_id;
  125. }
  126. // Returns the type of the requested node.
  127. auto GetType(SemanticsNodeId node_id) -> SemanticsNodeId {
  128. return GetNode(node_id).type();
  129. }
  130. // Adds an empty new node block, returning an ID to reference it and add
  131. // items.
  132. auto AddNodeBlock() -> SemanticsNodeBlockId {
  133. SemanticsNodeBlockId id(node_blocks_.size());
  134. node_blocks_.resize(node_blocks_.size() + 1);
  135. return id;
  136. }
  137. auto GetNodeBlock(SemanticsNodeBlockId block_id)
  138. -> llvm::SmallVector<SemanticsNodeId>& {
  139. return node_blocks_[block_id.index];
  140. }
  141. // Adds a real literal, returning an ID to reference it.
  142. auto AddRealLiteral(SemanticsRealLiteral real_literal)
  143. -> SemanticsRealLiteralId {
  144. SemanticsRealLiteralId id(real_literals_.size());
  145. real_literals_.push_back(real_literal);
  146. return id;
  147. }
  148. // Adds an string, returning an ID to reference it.
  149. auto AddString(llvm::StringRef str) -> SemanticsStringId {
  150. // If the string has already been stored, return the corresponding ID.
  151. if (auto existing_id = GetStringID(str)) {
  152. return *existing_id;
  153. }
  154. // Allocate the string and store it in the map.
  155. SemanticsStringId id(strings_.size());
  156. strings_.push_back(str);
  157. CARBON_CHECK(string_to_id_.insert({str, id}).second);
  158. return id;
  159. }
  160. // Returns an ID for the string if it's previously been stored.
  161. auto GetStringID(llvm::StringRef str) -> std::optional<SemanticsStringId> {
  162. auto str_find = string_to_id_.find(str);
  163. if (str_find != string_to_id_.end()) {
  164. return str_find->second;
  165. }
  166. return std::nullopt;
  167. }
  168. bool has_errors_ = false;
  169. // Storage for call objects.
  170. llvm::SmallVector<SemanticsCall> calls_;
  171. // Storage for callable objects.
  172. llvm::SmallVector<SemanticsCallable> callables_;
  173. // Related IRs. There will always be at least 2 entries, the builtin IR (used
  174. // for references of builtins) followed by the current IR (used for references
  175. // crossing node blocks).
  176. llvm::SmallVector<const SemanticsIR*> cross_reference_irs_;
  177. // Storage for integer literals.
  178. llvm::SmallVector<llvm::APInt> integer_literals_;
  179. // Storage for real literals.
  180. llvm::SmallVector<SemanticsRealLiteral> real_literals_;
  181. // Storage for strings. strings_ provides a list of allocated strings, while
  182. // string_to_id_ provides a mapping to identify strings.
  183. llvm::StringMap<SemanticsStringId> string_to_id_;
  184. llvm::SmallVector<llvm::StringRef> strings_;
  185. // All nodes. The first entries will always be cross-references to builtins,
  186. // at indices matching SemanticsBuiltinKind ordering.
  187. llvm::SmallVector<SemanticsNode> nodes_;
  188. // Storage for blocks within the IR. These reference entries in nodes_.
  189. llvm::SmallVector<llvm::SmallVector<SemanticsNodeId>> node_blocks_;
  190. // The top node block ID.
  191. SemanticsNodeBlockId top_node_block_id_ = SemanticsNodeBlockId::Invalid;
  192. };
  193. } // namespace Carbon
  194. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_