semantics_ir.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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() == 2)
  33. << "BuildBuiltins should produce 2 blocks, 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 << "callables: [\n";
  66. for (auto callable : callables_) {
  67. out.indent(Indent);
  68. out << callable << "\n";
  69. }
  70. out << "]\n";
  71. out << "integer_literals: [\n";
  72. for (const auto& integer_literal : integer_literals_) {
  73. out.indent(Indent);
  74. out << integer_literal << ",\n";
  75. }
  76. out << "]\n";
  77. out << "strings: [\n";
  78. for (const auto& string : strings_) {
  79. out.indent(Indent);
  80. out << string << ",\n";
  81. }
  82. out << "]\n";
  83. out << "nodes: [\n";
  84. for (const auto& node : nodes_) {
  85. out.indent(Indent);
  86. out << node << ",\n";
  87. }
  88. out << "]\n";
  89. out << "node_blocks: [\n";
  90. for (const auto& node_block : node_blocks_) {
  91. out.indent(Indent);
  92. out << "[\n";
  93. for (const auto& node : node_block) {
  94. out.indent(2 * Indent);
  95. out << node << ",\n";
  96. }
  97. out.indent(Indent);
  98. out << "],\n";
  99. }
  100. out << "]\n";
  101. }
  102. } // namespace Carbon