semantics_ir.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "toolchain/semantics/semantics_ir.h"
  5. #include "common/check.h"
  6. #include "llvm/Support/FormatVariadic.h"
  7. #include "toolchain/lexer/tokenized_buffer.h"
  8. #include "toolchain/semantics/semantics_builtin_kind.h"
  9. #include "toolchain/semantics/semantics_node.h"
  10. #include "toolchain/semantics/semantics_parse_tree_handler.h"
  11. namespace Carbon {
  12. auto SemanticsIR::MakeBuiltinIR() -> SemanticsIR {
  13. SemanticsIR semantics;
  14. static constexpr auto BuiltinIR = SemanticsCrossReferenceIRId(0);
  15. auto block_id = semantics.AddNodeBlock();
  16. semantics.cross_references_.resize_for_overwrite(
  17. SemanticsBuiltinKind::ValidCount);
  18. // TODO: The type-type should be replaced with a constraint. It will probably
  19. // remain a builtin, just reimplemented as equivalent to `constraint type {}`.
  20. auto builtin_type_type = semantics.AddNode(
  21. block_id, SemanticsNode::MakeBuiltin(SemanticsBuiltinKind::TypeType(),
  22. SemanticsNodeId(0)));
  23. semantics.cross_references_[SemanticsBuiltinKind::TypeType().AsInt()] =
  24. SemanticsCrossReference(BuiltinIR, block_id, builtin_type_type);
  25. CARBON_CHECK(builtin_type_type.id == 0)
  26. << "TypeType's type must be self-referential.";
  27. auto builtin_int32 = semantics.AddNode(
  28. block_id,
  29. SemanticsNode::MakeBuiltin(SemanticsBuiltinKind::IntegerLiteralType(),
  30. builtin_type_type));
  31. semantics
  32. .cross_references_[SemanticsBuiltinKind::IntegerLiteralType().AsInt()] =
  33. SemanticsCrossReference(BuiltinIR, block_id, builtin_int32);
  34. CARBON_CHECK(semantics.node_blocks_.size() == 1)
  35. << "BuildBuiltins should only produce 1 block, actual: "
  36. << semantics.node_blocks_.size();
  37. return semantics;
  38. }
  39. auto SemanticsIR::MakeFromParseTree(const SemanticsIR& builtin_ir,
  40. const TokenizedBuffer& tokens,
  41. const ParseTree& parse_tree)
  42. -> SemanticsIR {
  43. SemanticsIR semantics(builtin_ir);
  44. SemanticsParseTreeHandler(tokens, parse_tree, semantics).Build();
  45. return semantics;
  46. }
  47. auto SemanticsIR::Print(llvm::raw_ostream& out) const -> void {
  48. constexpr int Indent = 2;
  49. out << "cross_reference_irs.size == " << cross_reference_irs_.size() << ",\n";
  50. out << "cross_references = {\n";
  51. for (int32_t i = 0; i < static_cast<int32_t>(cross_references_.size()); ++i) {
  52. out.indent(Indent);
  53. out << SemanticsNodeId::MakeCrossReference(i) << " = \""
  54. << cross_references_[i] << "\";\n";
  55. }
  56. out << "},\n";
  57. out << "identifiers = {\n";
  58. for (int32_t i = 0; i < static_cast<int32_t>(identifiers_.size()); ++i) {
  59. out.indent(Indent);
  60. out << SemanticsIdentifierId(i) << " = \"" << identifiers_[i] << "\";\n";
  61. }
  62. out << "},\n";
  63. out << "integer_literals = {\n";
  64. for (int32_t i = 0; i < static_cast<int32_t>(integer_literals_.size()); ++i) {
  65. out.indent(Indent);
  66. out << SemanticsIntegerLiteralId(i) << " = " << integer_literals_[i]
  67. << ";\n";
  68. }
  69. out << "},\n";
  70. out << "node_blocks = {\n";
  71. for (int32_t i = 0; i < static_cast<int32_t>(node_blocks_.size()); ++i) {
  72. out.indent(Indent);
  73. out << SemanticsNodeBlockId(i) << " = {\n";
  74. const auto& node_block = node_blocks_[i];
  75. for (int32_t i = 0; i < static_cast<int32_t>(node_block.size()); ++i) {
  76. out.indent(2 * Indent);
  77. out << SemanticsNodeId(i) << " = " << node_block[i] << ";\n";
  78. }
  79. out.indent(Indent);
  80. out << "},\n";
  81. }
  82. out << "}\n";
  83. }
  84. } // namespace Carbon