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