semantics_ir.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. llvm::raw_ostream* vlog_stream)
  43. -> SemanticsIR {
  44. SemanticsIR semantics(builtin_ir);
  45. SemanticsParseTreeHandler(tokens, parse_tree, semantics, vlog_stream).Build();
  46. return semantics;
  47. }
  48. auto SemanticsIR::Print(llvm::raw_ostream& out) const -> void {
  49. constexpr int Indent = 2;
  50. out << "cross_reference_irs.size == " << cross_reference_irs_.size() << ",\n";
  51. out << "cross_references = {\n";
  52. for (int32_t i = 0; i < static_cast<int32_t>(cross_references_.size()); ++i) {
  53. out.indent(Indent);
  54. out << SemanticsNodeId::MakeCrossReference(i) << " = \""
  55. << cross_references_[i] << "\";\n";
  56. }
  57. out << "},\n";
  58. out << "identifiers = {\n";
  59. for (int32_t i = 0; i < static_cast<int32_t>(identifiers_.size()); ++i) {
  60. out.indent(Indent);
  61. out << SemanticsIdentifierId(i) << " = \"" << identifiers_[i] << "\";\n";
  62. }
  63. out << "},\n";
  64. out << "integer_literals = {\n";
  65. for (int32_t i = 0; i < static_cast<int32_t>(integer_literals_.size()); ++i) {
  66. out.indent(Indent);
  67. out << SemanticsIntegerLiteralId(i) << " = " << integer_literals_[i]
  68. << ";\n";
  69. }
  70. out << "},\n";
  71. out << "node_blocks = {\n";
  72. for (int32_t i = 0; i < static_cast<int32_t>(node_blocks_.size()); ++i) {
  73. out.indent(Indent);
  74. out << SemanticsNodeBlockId(i) << " = {\n";
  75. const auto& node_block = node_blocks_[i];
  76. for (int32_t i = 0; i < static_cast<int32_t>(node_block.size()); ++i) {
  77. out.indent(2 * Indent);
  78. out << SemanticsNodeId(i) << " = " << node_block[i] << ";\n";
  79. }
  80. out.indent(Indent);
  81. out << "},\n";
  82. }
  83. out << "}\n";
  84. }
  85. } // namespace Carbon