import_ir.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/import_ir.h"
  5. #include <utility>
  6. #include "toolchain/sem_ir/file.h"
  7. namespace Carbon::SemIR {
  8. auto ImportIR::Print(llvm::raw_ostream& out) const -> void {
  9. out << "{decl_id: " << decl_id
  10. << ", is_export: " << (is_export ? "true" : "false") << "}";
  11. }
  12. auto ImportIRInst::Print(llvm::raw_ostream& out) const -> void {
  13. out << "{ir_id: " << ir_id() << ", ";
  14. if (ir_id() == ImportIRId::Cpp) {
  15. out << "clang_source_loc_id: " << clang_source_loc_id();
  16. } else {
  17. out << "inst_id: " << inst_id();
  18. }
  19. out << "}";
  20. }
  21. auto GetCanonicalFileAndInstId(const File* sem_ir, SemIR::InstId inst_id)
  22. -> std::pair<const File*, InstId> {
  23. while (true) {
  24. // Step through an imported instruction to the instruction it was imported
  25. // from.
  26. if (auto import_ir_inst_id = sem_ir->insts().GetImportSource(inst_id);
  27. import_ir_inst_id.has_value()) {
  28. auto import_ir_inst = sem_ir->import_ir_insts().Get(import_ir_inst_id);
  29. sem_ir = sem_ir->import_irs().Get(import_ir_inst.ir_id()).sem_ir;
  30. inst_id = import_ir_inst.inst_id();
  31. continue;
  32. }
  33. // Step through export declarations to their exported value.
  34. if (auto export_decl =
  35. sem_ir->insts().TryGetAs<SemIR::ExportDecl>(inst_id)) {
  36. inst_id = export_decl->value_id;
  37. continue;
  38. }
  39. // Reached a non-imported entity.
  40. break;
  41. }
  42. return std::make_pair(sem_ir, inst_id);
  43. }
  44. } // namespace Carbon::SemIR