import_ir.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 imported instruction to the instruction it was imported
  24. // from.
  25. if (auto import_ir_inst_id = sem_ir->insts().GetImportSource(inst_id);
  26. import_ir_inst_id.has_value()) {
  27. auto import_ir_inst = sem_ir->import_ir_insts().Get(import_ir_inst_id);
  28. sem_ir = sem_ir->import_irs().Get(import_ir_inst.ir_id()).sem_ir;
  29. inst_id = import_ir_inst.inst_id();
  30. continue;
  31. }
  32. // Step through export declarations to their exported value.
  33. if (auto export_decl =
  34. sem_ir->insts().TryGetAs<SemIR::ExportDecl>(inst_id)) {
  35. inst_id = export_decl->value_id;
  36. continue;
  37. }
  38. // Reached a non-imported entity.
  39. break;
  40. }
  41. return std::make_pair(sem_ir, inst_id);
  42. }
  43. } // namespace Carbon::SemIR