import_ir.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 "toolchain/sem_ir/file.h"
  6. namespace Carbon::SemIR {
  7. auto ImportIR::Print(llvm::raw_ostream& out) const -> void {
  8. out << "{decl_id: " << decl_id
  9. << ", is_export: " << (is_export ? "true" : "false") << "}";
  10. }
  11. auto ImportIRInst::Print(llvm::raw_ostream& out) const -> void {
  12. out << "{ir_id: " << ir_id() << ", ";
  13. if (ir_id() == ImportIRId::Cpp) {
  14. out << "clang_source_loc_id: " << clang_source_loc_id();
  15. } else {
  16. out << "inst_id: " << inst_id();
  17. }
  18. out << "}";
  19. }
  20. auto GetCanonicalFileAndInstId(const File* sem_ir, SemIR::InstId inst_id)
  21. -> std::pair<const File*, InstId> {
  22. while (true) {
  23. // Step through an instruction with an imported location to the imported
  24. // instruction.
  25. if (auto loc_id = sem_ir->insts().GetCanonicalLocId(inst_id);
  26. loc_id.kind() == SemIR::LocId::Kind::ImportIRInstId) {
  27. auto import_ir_inst =
  28. sem_ir->import_ir_insts().Get(loc_id.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