semantics_ir.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. static constexpr int Indent = 2;
  52. template <typename T>
  53. static auto PrintList(llvm::raw_ostream& out, llvm::StringLiteral name,
  54. const llvm::SmallVector<T>& list) {
  55. out << name << ": [\n";
  56. for (const auto& element : list) {
  57. out.indent(Indent);
  58. out << element << ",\n";
  59. }
  60. out << "]\n";
  61. }
  62. auto SemanticsIR::Print(llvm::raw_ostream& out) const -> void {
  63. out << "cross_reference_irs_size: " << cross_reference_irs_.size() << "\n";
  64. PrintList(out, "calls", calls_);
  65. PrintList(out, "callables", callables_);
  66. PrintList(out, "integer_literals", integer_literals_);
  67. PrintList(out, "real_literals", real_literals_);
  68. PrintList(out, "strings", strings_);
  69. PrintList(out, "nodes", nodes_);
  70. out << "node_blocks: [\n";
  71. for (const auto& node_block : node_blocks_) {
  72. out.indent(Indent);
  73. out << "[\n";
  74. for (const auto& node : node_block) {
  75. out.indent(2 * Indent);
  76. out << node << ",\n";
  77. }
  78. out.indent(Indent);
  79. out << "],\n";
  80. }
  81. out << "]\n";
  82. }
  83. } // namespace Carbon