semantics_ir.cpp 4.3 KB

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