semantics_ir.h 8.0 KB

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