semantics_ir.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. semantics.nodes_.reserve(SemanticsBuiltinKind::ValidCount);
  14. #define CARBON_SEMANTICS_BUILTIN_KIND(Name, Type) \
  15. semantics.nodes_.push_back(SemanticsNode::MakeBuiltin( \
  16. SemanticsBuiltinKind::Name, SemanticsNodeId::Builtin##Type));
  17. #include "toolchain/semantics/semantics_builtin_kind.def"
  18. CARBON_CHECK(semantics.node_blocks_.size() == 1)
  19. << "BuildBuiltins should only have the empty block, actual: "
  20. << semantics.node_blocks_.size();
  21. CARBON_CHECK(semantics.nodes_.size() == SemanticsBuiltinKind::ValidCount)
  22. << "BuildBuiltins should produce " << SemanticsBuiltinKind::ValidCount
  23. << " nodes, actual: " << semantics.nodes_.size();
  24. return semantics;
  25. }
  26. auto SemanticsIR::MakeFromParseTree(const SemanticsIR& builtin_ir,
  27. const TokenizedBuffer& tokens,
  28. const ParseTree& parse_tree,
  29. DiagnosticConsumer& consumer,
  30. llvm::raw_ostream* vlog_stream)
  31. -> SemanticsIR {
  32. SemanticsIR semantics(&builtin_ir);
  33. // Copy builtins over.
  34. semantics.nodes_.resize_for_overwrite(SemanticsBuiltinKind::ValidCount);
  35. static constexpr auto BuiltinIR = SemanticsCrossReferenceIRId(0);
  36. for (int i = 0; i < SemanticsBuiltinKind::ValidCount; ++i) {
  37. // We can reuse the type node ID because the offsets of cross-references
  38. // will be the same in this IR.
  39. auto type = builtin_ir.nodes_[i].type();
  40. semantics.nodes_[i] =
  41. SemanticsNode::MakeCrossReference(type, BuiltinIR, SemanticsNodeId(i));
  42. }
  43. ParseTreeNodeLocationTranslator translator(&tokens, &parse_tree);
  44. ErrorTrackingDiagnosticConsumer err_tracker(consumer);
  45. DiagnosticEmitter<ParseTree::Node> emitter(translator, err_tracker);
  46. SemanticsParseTreeHandler(tokens, emitter, parse_tree, semantics, vlog_stream)
  47. .Build();
  48. semantics.has_errors_ = err_tracker.seen_error();
  49. return semantics;
  50. }
  51. auto SemanticsIR::Print(llvm::raw_ostream& out) const -> void {
  52. constexpr int Indent = 2;
  53. out << "cross_reference_irs_size: " << cross_reference_irs_.size() << "\n";
  54. out << "callables: [\n";
  55. for (auto callable : callables_) {
  56. out.indent(Indent);
  57. out << callable << "\n";
  58. }
  59. out << "]\n";
  60. out << "integer_literals: [\n";
  61. for (const auto& integer_literal : integer_literals_) {
  62. out.indent(Indent);
  63. out << integer_literal << ",\n";
  64. }
  65. out << "]\n";
  66. out << "strings: [\n";
  67. for (const auto& string : strings_) {
  68. out.indent(Indent);
  69. out << string << ",\n";
  70. }
  71. out << "]\n";
  72. out << "nodes: [\n";
  73. for (const auto& node : nodes_) {
  74. out.indent(Indent);
  75. out << node << ",\n";
  76. }
  77. out << "]\n";
  78. out << "node_blocks: [\n";
  79. for (const auto& node_block : node_blocks_) {
  80. out.indent(Indent);
  81. out << "[\n";
  82. for (const auto& node : node_block) {
  83. out.indent(2 * Indent);
  84. out << node << ",\n";
  85. }
  86. out.indent(Indent);
  87. out << "],\n";
  88. }
  89. out << "]\n";
  90. }
  91. } // namespace Carbon