semantics_ir.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. constexpr int32_t TypeOfTypeType = 0;
  19. auto type_type = semantics.AddNode(
  20. block_id, SemanticsNode::MakeBuiltin(SemanticsBuiltinKind::TypeType(),
  21. SemanticsNodeId(TypeOfTypeType)));
  22. semantics.cross_references_[SemanticsBuiltinKind::TypeType().AsInt()] =
  23. SemanticsCrossReference(BuiltinIR, block_id, type_type);
  24. CARBON_CHECK(type_type.index == TypeOfTypeType)
  25. << "TypeType's type must be self-referential.";
  26. constexpr int32_t TypeOfInvalidType = 1;
  27. auto invalid_type = semantics.AddNode(
  28. block_id, SemanticsNode::MakeBuiltin(SemanticsBuiltinKind::InvalidType(),
  29. SemanticsNodeId(TypeOfInvalidType)));
  30. semantics.cross_references_[SemanticsBuiltinKind::InvalidType().AsInt()] =
  31. SemanticsCrossReference(BuiltinIR, block_id, invalid_type);
  32. CARBON_CHECK(invalid_type.index == TypeOfInvalidType)
  33. << "InvalidType's type must be self-referential.";
  34. auto integer_literal_type = semantics.AddNode(
  35. block_id, SemanticsNode::MakeBuiltin(
  36. SemanticsBuiltinKind::IntegerLiteralType(), type_type));
  37. semantics
  38. .cross_references_[SemanticsBuiltinKind::IntegerLiteralType().AsInt()] =
  39. SemanticsCrossReference(BuiltinIR, block_id, integer_literal_type);
  40. auto real_literal_type = semantics.AddNode(
  41. block_id, SemanticsNode::MakeBuiltin(
  42. SemanticsBuiltinKind::RealLiteralType(), type_type));
  43. semantics.cross_references_[SemanticsBuiltinKind::RealLiteralType().AsInt()] =
  44. SemanticsCrossReference(BuiltinIR, block_id, real_literal_type);
  45. CARBON_CHECK(semantics.node_blocks_.size() == 1)
  46. << "BuildBuiltins should only produce 1 block, actual: "
  47. << semantics.node_blocks_.size();
  48. return semantics;
  49. }
  50. auto SemanticsIR::MakeFromParseTree(const SemanticsIR& builtin_ir,
  51. const TokenizedBuffer& tokens,
  52. const ParseTree& parse_tree,
  53. DiagnosticConsumer& consumer,
  54. llvm::raw_ostream* vlog_stream)
  55. -> SemanticsIR {
  56. SemanticsIR semantics(builtin_ir);
  57. TokenizedBuffer::TokenLocationTranslator translator(
  58. &tokens, /*last_line_lexed_to_column=*/nullptr);
  59. TokenDiagnosticEmitter emitter(translator, consumer);
  60. SemanticsParseTreeHandler(tokens, emitter, parse_tree, semantics, vlog_stream)
  61. .Build();
  62. return semantics;
  63. }
  64. auto SemanticsIR::Print(llvm::raw_ostream& out) const -> void {
  65. constexpr int Indent = 2;
  66. out << "cross_reference_irs.size == " << cross_reference_irs_.size() << ",\n";
  67. out << "cross_references = {\n";
  68. for (int32_t i = 0; i < static_cast<int32_t>(cross_references_.size()); ++i) {
  69. out.indent(Indent);
  70. out << SemanticsNodeId::MakeCrossReference(i) << " = "
  71. << cross_references_[i] << ";\n";
  72. }
  73. out << "},\n";
  74. out << "identifiers = {\n";
  75. for (int32_t i = 0; i < static_cast<int32_t>(identifiers_.size()); ++i) {
  76. out.indent(Indent);
  77. out << SemanticsIdentifierId(i) << " = \"" << identifiers_[i] << "\";\n";
  78. }
  79. out << "},\n";
  80. out << "integer_literals = {\n";
  81. for (int32_t i = 0; i < static_cast<int32_t>(integer_literals_.size()); ++i) {
  82. out.indent(Indent);
  83. out << SemanticsIntegerLiteralId(i) << " = " << integer_literals_[i]
  84. << ";\n";
  85. }
  86. out << "},\n";
  87. out << "node_blocks = {\n";
  88. for (int32_t i = 0; i < static_cast<int32_t>(node_blocks_.size()); ++i) {
  89. out.indent(Indent);
  90. out << SemanticsNodeBlockId(i) << " = {\n";
  91. const auto& node_block = node_blocks_[i];
  92. for (int32_t i = 0; i < static_cast<int32_t>(node_block.size()); ++i) {
  93. out.indent(2 * Indent);
  94. out << SemanticsNodeId(i) << " = " << node_block[i] << ";\n";
  95. }
  96. out.indent(Indent);
  97. out << "},\n";
  98. }
  99. out << "}\n";
  100. }
  101. } // namespace Carbon