semantics_ir.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. Allow omitting builtins so that unrelated changes are
  64. // less likely to alternate test golden files.
  65. // TODO: In the future, the things to print may change, for example by adding
  66. // preludes. We may then want the ability to omit other things similar to
  67. // builtins.
  68. auto Print(llvm::raw_ostream& out) const -> void {
  69. Print(out, /*include_builtins=*/false);
  70. }
  71. auto Print(llvm::raw_ostream& out, bool include_builtins) const -> void;
  72. // Returns the requested callable.
  73. auto GetCallable(SemanticsCallableId callable_id) const -> SemanticsCallable {
  74. return callables_[callable_id.index];
  75. }
  76. // Returns the requested integer literal.
  77. auto GetIntegerLiteral(SemanticsIntegerLiteralId int_id) const
  78. -> const llvm::APInt& {
  79. return integer_literals_[int_id.index];
  80. }
  81. // Returns the requested node.
  82. auto GetNode(SemanticsNodeId node_id) const -> SemanticsNode {
  83. return nodes_[node_id.index];
  84. }
  85. // Returns the requested node block.
  86. auto GetNodeBlock(SemanticsNodeBlockId block_id) const
  87. -> const llvm::SmallVector<SemanticsNodeId>& {
  88. return node_blocks_[block_id.index];
  89. }
  90. // Returns the requested real literal.
  91. auto GetRealLiteral(SemanticsRealLiteralId int_id) const
  92. -> const SemanticsRealLiteral& {
  93. return real_literals_[int_id.index];
  94. }
  95. // Returns the requested string.
  96. auto GetString(SemanticsStringId string_id) const -> llvm::StringRef {
  97. return strings_[string_id.index];
  98. }
  99. auto nodes_size() const -> int { return nodes_.size(); }
  100. auto top_node_block_id() const -> SemanticsNodeBlockId {
  101. return top_node_block_id_;
  102. }
  103. // Returns true if there were errors creating the semantics IR.
  104. auto has_errors() const -> bool { return has_errors_; }
  105. private:
  106. friend class SemanticsParseTreeHandler;
  107. explicit SemanticsIR(const SemanticsIR* builtin_ir)
  108. : cross_reference_irs_({builtin_ir == nullptr ? this : builtin_ir}) {
  109. // For SemanticsNodeBlockId::Empty.
  110. node_blocks_.resize(1);
  111. }
  112. // Adds a call, returning an ID to reference it.
  113. auto AddCall(SemanticsCall call) -> SemanticsCallId {
  114. SemanticsCallId id(calls_.size());
  115. calls_.push_back(call);
  116. return id;
  117. }
  118. // Adds a callable, returning an ID to reference it.
  119. auto AddCallable(SemanticsCallable callable) -> SemanticsCallableId {
  120. SemanticsCallableId id(callables_.size());
  121. callables_.push_back(callable);
  122. return id;
  123. }
  124. // Adds an integer literal, returning an ID to reference it.
  125. auto AddIntegerLiteral(llvm::APInt integer_literal)
  126. -> SemanticsIntegerLiteralId {
  127. SemanticsIntegerLiteralId id(integer_literals_.size());
  128. integer_literals_.push_back(integer_literal);
  129. return id;
  130. }
  131. // Adds a node to a specified block, returning an ID to reference the node.
  132. auto AddNode(SemanticsNodeBlockId block_id, SemanticsNode node)
  133. -> SemanticsNodeId {
  134. SemanticsNodeId node_id(nodes_.size());
  135. nodes_.push_back(node);
  136. node_blocks_[block_id.index].push_back(node_id);
  137. return node_id;
  138. }
  139. // Returns the type of the requested node.
  140. auto GetType(SemanticsNodeId node_id) -> SemanticsNodeId {
  141. return GetNode(node_id).type_id();
  142. }
  143. // Adds an empty new node block, returning an ID to reference it and add
  144. // items.
  145. auto AddNodeBlock() -> SemanticsNodeBlockId {
  146. SemanticsNodeBlockId id(node_blocks_.size());
  147. node_blocks_.resize(node_blocks_.size() + 1);
  148. return id;
  149. }
  150. auto GetNodeBlock(SemanticsNodeBlockId block_id)
  151. -> llvm::SmallVector<SemanticsNodeId>& {
  152. return node_blocks_[block_id.index];
  153. }
  154. // Adds a real literal, returning an ID to reference it.
  155. auto AddRealLiteral(SemanticsRealLiteral real_literal)
  156. -> SemanticsRealLiteralId {
  157. SemanticsRealLiteralId id(real_literals_.size());
  158. real_literals_.push_back(real_literal);
  159. return id;
  160. }
  161. // Adds an string, returning an ID to reference it.
  162. auto AddString(llvm::StringRef str) -> SemanticsStringId {
  163. // If the string has already been stored, return the corresponding ID.
  164. if (auto existing_id = GetStringID(str)) {
  165. return *existing_id;
  166. }
  167. // Allocate the string and store it in the map.
  168. SemanticsStringId id(strings_.size());
  169. strings_.push_back(str);
  170. CARBON_CHECK(string_to_id_.insert({str, id}).second);
  171. return id;
  172. }
  173. // Returns an ID for the string if it's previously been stored.
  174. auto GetStringID(llvm::StringRef str) -> std::optional<SemanticsStringId> {
  175. auto str_find = string_to_id_.find(str);
  176. if (str_find != string_to_id_.end()) {
  177. return str_find->second;
  178. }
  179. return std::nullopt;
  180. }
  181. // Produces a string version of a node.
  182. auto StringifyNode(SemanticsNodeId node_id) -> std::string;
  183. // Implements StringifyNode using streaming.
  184. auto StringifyNodeImpl(llvm::raw_ostream& out, SemanticsNodeId node_id)
  185. -> void;
  186. bool has_errors_ = false;
  187. // Storage for call objects.
  188. llvm::SmallVector<SemanticsCall> calls_;
  189. // Storage for callable objects.
  190. llvm::SmallVector<SemanticsCallable> callables_;
  191. // Related IRs. There will always be at least 2 entries, the builtin IR (used
  192. // for references of builtins) followed by the current IR (used for references
  193. // crossing node blocks).
  194. llvm::SmallVector<const SemanticsIR*> cross_reference_irs_;
  195. // Storage for integer literals.
  196. llvm::SmallVector<llvm::APInt> integer_literals_;
  197. // Storage for real literals.
  198. llvm::SmallVector<SemanticsRealLiteral> real_literals_;
  199. // Storage for strings. strings_ provides a list of allocated strings, while
  200. // string_to_id_ provides a mapping to identify strings.
  201. llvm::StringMap<SemanticsStringId> string_to_id_;
  202. llvm::SmallVector<llvm::StringRef> strings_;
  203. // All nodes. The first entries will always be cross-references to builtins,
  204. // at indices matching SemanticsBuiltinKind ordering.
  205. llvm::SmallVector<SemanticsNode> nodes_;
  206. // Storage for blocks within the IR. These reference entries in nodes_.
  207. llvm::SmallVector<llvm::SmallVector<SemanticsNodeId>> node_blocks_;
  208. // The top node block ID.
  209. SemanticsNodeBlockId top_node_block_id_ = SemanticsNodeBlockId::Invalid;
  210. };
  211. } // namespace Carbon
  212. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_