semantics_ir.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 function.
  12. struct SemanticsFunction {
  13. auto Print(llvm::raw_ostream& out) const -> void {
  14. out << "{name: " << name_id << ", "
  15. << "param_refs: " << param_refs_id;
  16. if (return_type_id.is_valid()) {
  17. out << ", return_type: " << return_type_id;
  18. }
  19. if (body_id.is_valid()) {
  20. out << ", body: " << body_id;
  21. }
  22. out << "}";
  23. }
  24. // The function name.
  25. SemanticsStringId name_id;
  26. // A block containing a single reference node per parameter.
  27. SemanticsNodeBlockId param_refs_id;
  28. // The return type. This will be invalid if the return type wasn't specified.
  29. SemanticsTypeId return_type_id;
  30. // The body. This will be invalid for declarations which don't have a visible
  31. // definition.
  32. SemanticsNodeBlockId body_id;
  33. };
  34. struct SemanticsRealLiteral {
  35. auto Print(llvm::raw_ostream& out) const -> void {
  36. out << "{mantissa: " << mantissa << ", exponent: " << exponent
  37. << ", is_decimal: " << is_decimal << "}";
  38. }
  39. llvm::APInt mantissa;
  40. llvm::APInt exponent;
  41. // If false, the value is mantissa * 2^exponent.
  42. // If true, the value is mantissa * 10^exponent.
  43. bool is_decimal;
  44. };
  45. // Provides semantic analysis on a ParseTree.
  46. class SemanticsIR {
  47. public:
  48. // Produces the builtins.
  49. static auto MakeBuiltinIR() -> SemanticsIR;
  50. // Adds the IR for the provided ParseTree.
  51. static auto MakeFromParseTree(const SemanticsIR& builtin_ir,
  52. const TokenizedBuffer& tokens,
  53. const ParseTree& parse_tree,
  54. DiagnosticConsumer& consumer,
  55. llvm::raw_ostream* vlog_stream) -> SemanticsIR;
  56. // Prints the full IR. Allow omitting builtins so that unrelated changes are
  57. // less likely to alternate test golden files.
  58. // TODO: In the future, the things to print may change, for example by adding
  59. // preludes. We may then want the ability to omit other things similar to
  60. // builtins.
  61. auto Print(llvm::raw_ostream& out) const -> void {
  62. Print(out, /*include_builtins=*/false);
  63. }
  64. auto Print(llvm::raw_ostream& out, bool include_builtins) const -> void;
  65. // Adds a callable, returning an ID to reference it.
  66. auto AddFunction(SemanticsFunction function) -> SemanticsFunctionId {
  67. SemanticsFunctionId id(functions_.size());
  68. functions_.push_back(function);
  69. return id;
  70. }
  71. // Returns the requested callable.
  72. auto GetFunction(SemanticsFunctionId function_id) const -> SemanticsFunction {
  73. return functions_[function_id.index];
  74. }
  75. // Returns the requested callable.
  76. auto GetFunction(SemanticsFunctionId function_id) -> SemanticsFunction& {
  77. return functions_[function_id.index];
  78. }
  79. // Adds an integer literal, returning an ID to reference it.
  80. auto AddIntegerLiteral(llvm::APInt integer_literal)
  81. -> SemanticsIntegerLiteralId {
  82. SemanticsIntegerLiteralId id(integer_literals_.size());
  83. integer_literals_.push_back(integer_literal);
  84. return id;
  85. }
  86. // Returns the requested integer literal.
  87. auto GetIntegerLiteral(SemanticsIntegerLiteralId int_id) const
  88. -> const llvm::APInt& {
  89. return integer_literals_[int_id.index];
  90. }
  91. // Adds a node to a specified block, returning an ID to reference the node.
  92. auto AddNode(SemanticsNodeBlockId block_id, SemanticsNode node)
  93. -> SemanticsNodeId {
  94. SemanticsNodeId node_id(nodes_.size());
  95. nodes_.push_back(node);
  96. node_blocks_[block_id.index].push_back(node_id);
  97. return node_id;
  98. }
  99. // Returns the requested node.
  100. auto GetNode(SemanticsNodeId node_id) const -> SemanticsNode {
  101. return nodes_[node_id.index];
  102. }
  103. // Returns the requested node block.
  104. auto GetNodeBlock(SemanticsNodeBlockId block_id) const
  105. -> const llvm::SmallVector<SemanticsNodeId>& {
  106. return node_blocks_[block_id.index];
  107. }
  108. // Returns the requested node block.
  109. auto GetNodeBlock(SemanticsNodeBlockId block_id)
  110. -> llvm::SmallVector<SemanticsNodeId>& {
  111. return node_blocks_[block_id.index];
  112. }
  113. // Adds a real literal, returning an ID to reference it.
  114. auto AddRealLiteral(SemanticsRealLiteral real_literal)
  115. -> SemanticsRealLiteralId {
  116. SemanticsRealLiteralId id(real_literals_.size());
  117. real_literals_.push_back(real_literal);
  118. return id;
  119. }
  120. // Returns the requested real literal.
  121. auto GetRealLiteral(SemanticsRealLiteralId int_id) const
  122. -> const SemanticsRealLiteral& {
  123. return real_literals_[int_id.index];
  124. }
  125. // Adds an string, returning an ID to reference it.
  126. auto AddString(llvm::StringRef str) -> SemanticsStringId {
  127. // If the string has already been stored, return the corresponding ID.
  128. if (auto existing_id = GetStringID(str)) {
  129. return *existing_id;
  130. }
  131. // Allocate the string and store it in the map.
  132. SemanticsStringId id(strings_.size());
  133. strings_.push_back(str);
  134. CARBON_CHECK(string_to_id_.insert({str, id}).second);
  135. return id;
  136. }
  137. // Returns the requested string.
  138. auto GetString(SemanticsStringId string_id) const -> llvm::StringRef {
  139. return strings_[string_id.index];
  140. }
  141. // Returns an ID for the string if it's previously been stored.
  142. auto GetStringID(llvm::StringRef str) -> std::optional<SemanticsStringId> {
  143. auto str_find = string_to_id_.find(str);
  144. if (str_find != string_to_id_.end()) {
  145. return str_find->second;
  146. }
  147. return std::nullopt;
  148. }
  149. // Adds a type, returning an ID to reference it.
  150. auto AddType(SemanticsNodeId node_id) -> SemanticsTypeId {
  151. SemanticsTypeId type_id(types_.size());
  152. types_.push_back(node_id);
  153. if (node_id == SemanticsNodeId::BuiltinEmptyTupleType) {
  154. CARBON_CHECK(!empty_tuple_type_id_.is_valid());
  155. empty_tuple_type_id_ = type_id;
  156. }
  157. return type_id;
  158. }
  159. // Gets the node ID for a type. This doesn't handle TypeType or InvalidType in
  160. // order to avoid a check; callers that need that should use
  161. // GetTypeAllowBuiltinTypes.
  162. auto GetType(SemanticsTypeId type_id) const -> SemanticsNodeId {
  163. // Double-check it's not called with TypeType or InvalidType.
  164. CARBON_CHECK(type_id.index >= 0)
  165. << "Invalid argument for GetType: " << type_id;
  166. return types_[type_id.index];
  167. }
  168. auto GetTypeAllowBuiltinTypes(SemanticsTypeId type_id) const
  169. -> SemanticsNodeId {
  170. if (type_id == SemanticsTypeId::TypeType) {
  171. return SemanticsNodeId::BuiltinTypeType;
  172. } else if (type_id == SemanticsTypeId::InvalidType) {
  173. return SemanticsNodeId::BuiltinInvalidType;
  174. } else {
  175. return GetType(type_id);
  176. }
  177. }
  178. // Produces a string version of a type.
  179. auto StringifyType(SemanticsTypeId type_id) -> std::string;
  180. auto functions_size() const -> int { return functions_.size(); }
  181. auto nodes_size() const -> int { return nodes_.size(); }
  182. auto types() const -> const llvm::SmallVector<SemanticsNodeId>& {
  183. return types_;
  184. }
  185. auto empty_tuple_type_id() const -> SemanticsTypeId {
  186. return empty_tuple_type_id_;
  187. }
  188. // The node blocks, for direct mutation.
  189. auto node_blocks() -> llvm::SmallVector<llvm::SmallVector<SemanticsNodeId>>& {
  190. return node_blocks_;
  191. }
  192. auto top_node_block_id() const -> SemanticsNodeBlockId {
  193. return top_node_block_id_;
  194. }
  195. // Returns true if there were errors creating the semantics IR.
  196. auto has_errors() const -> bool { return has_errors_; }
  197. private:
  198. explicit SemanticsIR(const SemanticsIR* builtin_ir)
  199. : cross_reference_irs_({builtin_ir == nullptr ? this : builtin_ir}) {
  200. // For SemanticsNodeBlockId::Empty.
  201. node_blocks_.resize(1);
  202. }
  203. bool has_errors_ = false;
  204. // Storage for callable objects.
  205. llvm::SmallVector<SemanticsFunction> functions_;
  206. // Related IRs. There will always be at least 2 entries, the builtin IR (used
  207. // for references of builtins) followed by the current IR (used for references
  208. // crossing node blocks).
  209. llvm::SmallVector<const SemanticsIR*> cross_reference_irs_;
  210. // Storage for integer literals.
  211. llvm::SmallVector<llvm::APInt> integer_literals_;
  212. // Storage for real literals.
  213. llvm::SmallVector<SemanticsRealLiteral> real_literals_;
  214. // Storage for strings. strings_ provides a list of allocated strings, while
  215. // string_to_id_ provides a mapping to identify strings.
  216. llvm::StringMap<SemanticsStringId> string_to_id_;
  217. llvm::SmallVector<llvm::StringRef> strings_;
  218. // Nodes which correspond to in-use types. Stored separately for easy access
  219. // by lowering.
  220. llvm::SmallVector<SemanticsNodeId> types_;
  221. // The type of the empty tuple. This is special-cased due to its use in
  222. // implicit function returns.
  223. SemanticsTypeId empty_tuple_type_id_ = SemanticsTypeId::Invalid;
  224. // All nodes. The first entries will always be cross-references to builtins,
  225. // at indices matching SemanticsBuiltinKind ordering.
  226. llvm::SmallVector<SemanticsNode> nodes_;
  227. // Storage for blocks within the IR. These reference entries in nodes_.
  228. llvm::SmallVector<llvm::SmallVector<SemanticsNodeId>> node_blocks_;
  229. // The top node block ID.
  230. SemanticsNodeBlockId top_node_block_id_ = SemanticsNodeBlockId::Invalid;
  231. };
  232. } // namespace Carbon
  233. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_