import_ir.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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, 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. // TODO: For imports from C++, we return the importing instruction, which
  30. // isn't necessarily canonical.
  31. if (import_ir_inst.ir_id() != ImportIRId::Cpp) {
  32. sem_ir = sem_ir->import_irs().Get(import_ir_inst.ir_id()).sem_ir;
  33. inst_id = import_ir_inst.inst_id();
  34. continue;
  35. }
  36. }
  37. // Step through export declarations to their exported value.
  38. if (auto export_decl = sem_ir->insts().TryGetAs<ExportDecl>(inst_id)) {
  39. inst_id = export_decl->value_id;
  40. continue;
  41. }
  42. // Reached a non-imported entity.
  43. break;
  44. }
  45. return std::make_pair(sem_ir, inst_id);
  46. }
  47. } // namespace Carbon::SemIR