absolute_node_id.cpp 5.6 KB

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