semantics_ir.cpp 4.4 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(/*builtin_ir=*/nullptr);
  14. auto block_id = semantics.AddNodeBlock();
  15. semantics.nodes_.reserve(SemanticsBuiltinKind::ValidCount);
  16. constexpr int32_t TypeOfTypeType = 0;
  17. auto type_type = semantics.AddNode(
  18. block_id, SemanticsNode::MakeBuiltin(SemanticsBuiltinKind::TypeType(),
  19. SemanticsNodeId(TypeOfTypeType)));
  20. CARBON_CHECK(type_type.index == TypeOfTypeType)
  21. << "TypeType's type must be self-referential.";
  22. constexpr int32_t TypeOfInvalidType = 1;
  23. auto invalid_type = semantics.AddNode(
  24. block_id, SemanticsNode::MakeBuiltin(SemanticsBuiltinKind::InvalidType(),
  25. SemanticsNodeId(TypeOfInvalidType)));
  26. CARBON_CHECK(invalid_type.index == TypeOfInvalidType)
  27. << "InvalidType's type must be self-referential.";
  28. semantics.AddNode(
  29. block_id, SemanticsNode::MakeBuiltin(SemanticsBuiltinKind::IntegerType(),
  30. type_type));
  31. semantics.AddNode(block_id, SemanticsNode::MakeBuiltin(
  32. SemanticsBuiltinKind::RealType(), type_type));
  33. CARBON_CHECK(semantics.node_blocks_.size() == 1)
  34. << "BuildBuiltins should only produce 1 block, actual: "
  35. << semantics.node_blocks_.size();
  36. return semantics;
  37. }
  38. auto SemanticsIR::MakeFromParseTree(const SemanticsIR& builtin_ir,
  39. const TokenizedBuffer& tokens,
  40. const ParseTree& parse_tree,
  41. DiagnosticConsumer& consumer,
  42. llvm::raw_ostream* vlog_stream)
  43. -> SemanticsIR {
  44. SemanticsIR semantics(&builtin_ir);
  45. // Copy builtins over.
  46. semantics.nodes_.resize_for_overwrite(SemanticsBuiltinKind::ValidCount);
  47. static constexpr auto BuiltinIR = SemanticsCrossReferenceIRId(0);
  48. for (int i = 0; i < SemanticsBuiltinKind::ValidCount; ++i) {
  49. // We can reuse the type node ID because the offsets of cross-references
  50. // will be the same in this IR.
  51. auto type = builtin_ir.nodes_[i].type();
  52. semantics.nodes_[i] =
  53. SemanticsNode::MakeCrossReference(type, BuiltinIR, SemanticsNodeId(i));
  54. }
  55. TokenizedBuffer::TokenLocationTranslator translator(
  56. &tokens, /*last_line_lexed_to_column=*/nullptr);
  57. ErrorTrackingDiagnosticConsumer err_tracker(consumer);
  58. TokenDiagnosticEmitter emitter(translator, err_tracker);
  59. SemanticsParseTreeHandler(tokens, emitter, parse_tree, semantics, vlog_stream)
  60. .Build();
  61. semantics.has_errors_ = err_tracker.seen_error();
  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 << "integer_literals = {\n";
  68. for (int32_t i = 0; i < static_cast<int32_t>(integer_literals_.size()); ++i) {
  69. out.indent(Indent);
  70. out << SemanticsIntegerLiteralId(i) << " = " << integer_literals_[i]
  71. << ";\n";
  72. }
  73. out << "},\n";
  74. out << "strings = {\n";
  75. for (int32_t i = 0; i < static_cast<int32_t>(strings_.size()); ++i) {
  76. out.indent(Indent);
  77. out << SemanticsStringId(i) << " = \"" << strings_[i] << "\";\n";
  78. }
  79. out << "},\n";
  80. out << "nodes = {\n";
  81. for (int32_t i = 0; i < static_cast<int32_t>(nodes_.size()); ++i) {
  82. out.indent(Indent);
  83. out << SemanticsNodeId(i) << " = " << nodes_[i] << ";\n";
  84. }
  85. out << "},\n";
  86. out << "node_blocks = {\n";
  87. for (int32_t i = 0; i < static_cast<int32_t>(node_blocks_.size()); ++i) {
  88. out.indent(Indent);
  89. out << SemanticsNodeBlockId(i) << " = {\n";
  90. const auto& node_block = node_blocks_[i];
  91. for (int32_t i = 0; i < static_cast<int32_t>(node_block.size()); ++i) {
  92. out.indent(2 * Indent);
  93. out << node_block[i] << ";\n";
  94. }
  95. out.indent(Indent);
  96. out << "},\n";
  97. }
  98. out << "}\n";
  99. }
  100. } // namespace Carbon