semantics_ir.cpp 4.7 KB

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