absolute_node_id.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/sem_ir/absolute_node_id.h"
  5. #include "toolchain/sem_ir/ids.h"
  6. namespace Carbon::SemIR {
  7. // Notes an import on the diagnostic and updates cursors to point at the
  8. // imported IR.
  9. static auto FollowImportRef(
  10. llvm::SmallVector<AbsoluteNodeId>& absolute_node_ids,
  11. const File*& cursor_ir, InstId& cursor_inst_id,
  12. ImportIRInstId import_ir_inst_id) -> void {
  13. auto import_ir_inst = cursor_ir->import_ir_insts().Get(import_ir_inst_id);
  14. CARBON_CHECK(import_ir_inst.ir_id() != ImportIRId::Cpp);
  15. const auto& import_ir = cursor_ir->import_irs().Get(import_ir_inst.ir_id());
  16. CARBON_CHECK(import_ir.decl_id.has_value(),
  17. "If we get `None` locations here, we may need to more "
  18. "thoroughly track ImportDecls.");
  19. auto import_loc_id = cursor_ir->insts().GetLocId(import_ir.decl_id);
  20. switch (import_loc_id.kind()) {
  21. case LocId::Kind::None:
  22. break;
  23. case LocId::Kind::ImportIRInstId: {
  24. // For implicit imports, we need to unravel the location a little
  25. // further.
  26. auto implicit_import_ir_inst =
  27. cursor_ir->import_ir_insts().Get(import_loc_id.import_ir_inst_id());
  28. const auto& implicit_ir =
  29. cursor_ir->import_irs().Get(implicit_import_ir_inst.ir_id());
  30. auto implicit_loc_id = implicit_ir.sem_ir->insts().GetLocId(
  31. implicit_import_ir_inst.inst_id());
  32. CARBON_CHECK(implicit_loc_id.kind() == LocId::Kind::NodeId,
  33. "Should only be one layer of implicit imports");
  34. absolute_node_ids.push_back(
  35. {.check_ir_id = implicit_ir.sem_ir->check_ir_id(),
  36. .node_id = implicit_loc_id.node_id()});
  37. break;
  38. }
  39. case LocId::Kind::InstId:
  40. CARBON_FATAL("Unexpected LocId: {0}", import_loc_id);
  41. case LocId::Kind::NodeId: {
  42. // For imports in the current file, the location is simple.
  43. absolute_node_ids.push_back({.check_ir_id = cursor_ir->check_ir_id(),
  44. .node_id = import_loc_id.node_id()});
  45. break;
  46. }
  47. }
  48. cursor_ir = import_ir.sem_ir;
  49. cursor_inst_id = import_ir_inst.inst_id();
  50. }
  51. // Returns true if this is the final parse node location. If the location is is
  52. // an import, follows it and returns false.
  53. static auto HandleLocId(llvm::SmallVector<AbsoluteNodeId>& absolute_node_ids,
  54. const File*& cursor_ir, InstId& cursor_inst_id,
  55. LocId loc_id) -> bool {
  56. switch (loc_id.kind()) {
  57. case LocId::Kind::ImportIRInstId: {
  58. FollowImportRef(absolute_node_ids, cursor_ir, cursor_inst_id,
  59. loc_id.import_ir_inst_id());
  60. return false;
  61. }
  62. case LocId::Kind::NodeId: {
  63. // Parse nodes always refer to the current IR.
  64. absolute_node_ids.push_back({.check_ir_id = cursor_ir->check_ir_id(),
  65. .node_id = loc_id.node_id()});
  66. return true;
  67. }
  68. case LocId::Kind::None:
  69. case LocId::Kind::InstId:
  70. CARBON_FATAL("Unexpected LocId: {0}", loc_id);
  71. }
  72. }
  73. // Loops through imported instructions until the actual instruction is found.
  74. static auto GetAbsoluteNodeIdImpl(
  75. llvm::SmallVector<AbsoluteNodeId>& absolute_node_ids, const File* cursor_ir,
  76. InstId cursor_inst_id) -> void {
  77. while (cursor_inst_id.has_value()) {
  78. auto cursor_inst = cursor_ir->insts().Get(cursor_inst_id);
  79. if (auto bind_ref = cursor_inst.TryAs<ExportDecl>();
  80. bind_ref && bind_ref->value_id.has_value()) {
  81. cursor_inst_id = bind_ref->value_id;
  82. continue;
  83. }
  84. // If the parse node has a value, use it for the location.
  85. if (auto loc_id = cursor_ir->insts().GetLocId(cursor_inst_id);
  86. loc_id.has_value()) {
  87. if (HandleLocId(absolute_node_ids, cursor_ir, cursor_inst_id, loc_id)) {
  88. return;
  89. }
  90. continue;
  91. }
  92. // If a namespace has an instruction for an import, switch to looking at it.
  93. if (auto ns = cursor_inst.TryAs<Namespace>()) {
  94. if (ns->import_id.has_value()) {
  95. cursor_inst_id = ns->import_id;
  96. continue;
  97. }
  98. }
  99. break;
  100. }
  101. // `None` parse node but not an import; just nothing to point at.
  102. absolute_node_ids.push_back({.check_ir_id = cursor_ir->check_ir_id(),
  103. .node_id = Parse::NodeId::None});
  104. }
  105. auto GetAbsoluteNodeId(const File* sem_ir, LocId loc_id)
  106. -> llvm::SmallVector<AbsoluteNodeId> {
  107. llvm::SmallVector<AbsoluteNodeId> absolute_node_ids;
  108. switch (loc_id.kind()) {
  109. case LocId::Kind::None:
  110. absolute_node_ids.push_back({.check_ir_id = sem_ir->check_ir_id(),
  111. .node_id = Parse::NodeId::None});
  112. break;
  113. case LocId::Kind::InstId:
  114. GetAbsoluteNodeIdImpl(absolute_node_ids, sem_ir, loc_id.inst_id());
  115. break;
  116. case LocId::Kind::ImportIRInstId:
  117. case LocId::Kind::NodeId: {
  118. const File* cursor_ir = sem_ir;
  119. InstId cursor_inst_id = InstId::None;
  120. if (HandleLocId(absolute_node_ids, cursor_ir, cursor_inst_id, loc_id)) {
  121. break;
  122. }
  123. CARBON_CHECK(cursor_inst_id.has_value(), "Should be set by HandleLocId");
  124. GetAbsoluteNodeIdImpl(absolute_node_ids, cursor_ir, cursor_inst_id);
  125. break;
  126. }
  127. }
  128. return absolute_node_ids;
  129. }
  130. } // namespace Carbon::SemIR