absolute_node_ref.cpp 5.4 KB

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