absolute_node_id.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. if (import_loc_id.is_node_id()) {
  20. // For imports in the current file, the location is simple.
  21. absolute_node_ids.push_back({.check_ir_id = cursor_ir->check_ir_id(),
  22. .node_id = import_loc_id.node_id()});
  23. } else if (import_loc_id.is_import_ir_inst_id()) {
  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 =
  31. implicit_ir.sem_ir->insts().GetLocId(implicit_import_ir_inst.inst_id);
  32. CARBON_CHECK(implicit_loc_id.is_node_id(),
  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. }
  38. cursor_ir = import_ir.sem_ir;
  39. cursor_inst_id = import_ir_inst.inst_id;
  40. }
  41. // Returns true if this is the final parse node location. If the location is is
  42. // an import, follows it and returns false.
  43. static auto HandleLocId(llvm::SmallVector<AbsoluteNodeId>& absolute_node_ids,
  44. const File*& cursor_ir, InstId& cursor_inst_id,
  45. LocId loc_id) -> bool {
  46. if (loc_id.is_import_ir_inst_id()) {
  47. FollowImportRef(absolute_node_ids, cursor_ir, cursor_inst_id,
  48. loc_id.import_ir_inst_id());
  49. return false;
  50. } else {
  51. // Parse nodes always refer to the current IR.
  52. absolute_node_ids.push_back(
  53. {.check_ir_id = cursor_ir->check_ir_id(), .node_id = loc_id.node_id()});
  54. return true;
  55. }
  56. }
  57. // Loops through imported instructions until the actual instruction is found.
  58. static auto GetAbsoluteNodeIdImpl(
  59. llvm::SmallVector<AbsoluteNodeId>& absolute_node_ids, const File* cursor_ir,
  60. InstId cursor_inst_id) -> void {
  61. while (true) {
  62. if (cursor_inst_id.has_value()) {
  63. auto cursor_inst = cursor_ir->insts().Get(cursor_inst_id);
  64. if (auto bind_ref = cursor_inst.TryAs<ExportDecl>();
  65. bind_ref && bind_ref->value_id.has_value()) {
  66. cursor_inst_id = bind_ref->value_id;
  67. continue;
  68. }
  69. // If the parse node has a value, use it for the location.
  70. if (auto loc_id = cursor_ir->insts().GetLocId(cursor_inst_id);
  71. loc_id.has_value()) {
  72. if (HandleLocId(absolute_node_ids, cursor_ir, cursor_inst_id, loc_id)) {
  73. return;
  74. }
  75. continue;
  76. }
  77. // If a namespace has an instruction for an import, switch to looking at
  78. // it.
  79. if (auto ns = cursor_inst.TryAs<Namespace>()) {
  80. if (ns->import_id.has_value()) {
  81. cursor_inst_id = ns->import_id;
  82. continue;
  83. }
  84. }
  85. }
  86. // `None` parse node but not an import; just nothing to point at.
  87. absolute_node_ids.push_back({.check_ir_id = cursor_ir->check_ir_id(),
  88. .node_id = Parse::NodeId::None});
  89. return;
  90. }
  91. }
  92. auto GetAbsoluteNodeId(const File* sem_ir, InstId inst_id)
  93. -> llvm::SmallVector<AbsoluteNodeId> {
  94. llvm::SmallVector<AbsoluteNodeId> absolute_node_ids;
  95. GetAbsoluteNodeIdImpl(absolute_node_ids, sem_ir, inst_id);
  96. return absolute_node_ids;
  97. }
  98. auto GetAbsoluteNodeId(const File* sem_ir, LocId loc_id)
  99. -> llvm::SmallVector<AbsoluteNodeId> {
  100. llvm::SmallVector<AbsoluteNodeId> absolute_node_ids;
  101. if (!loc_id.has_value()) {
  102. absolute_node_ids.push_back(
  103. {.check_ir_id = sem_ir->check_ir_id(), .node_id = Parse::NodeId::None});
  104. return absolute_node_ids;
  105. }
  106. const File* cursor_ir = sem_ir;
  107. InstId cursor_inst_id = InstId::None;
  108. if (HandleLocId(absolute_node_ids, cursor_ir, cursor_inst_id, loc_id)) {
  109. return absolute_node_ids;
  110. }
  111. CARBON_CHECK(cursor_inst_id.has_value(), "Should be set by HandleLocId");
  112. GetAbsoluteNodeIdImpl(absolute_node_ids, cursor_ir, cursor_inst_id);
  113. return absolute_node_ids;
  114. }
  115. } // namespace Carbon::SemIR