import_ir.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef CARBON_TOOLCHAIN_SEM_IR_IMPORT_IR_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_IMPORT_IR_H_
  6. #include "llvm/ADT/FoldingSet.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. #include "toolchain/sem_ir/inst.h"
  9. namespace Carbon::SemIR {
  10. // A reference to an imported IR.
  11. struct ImportIR : public Printable<ImportIR> {
  12. auto Print(llvm::raw_ostream& out) const -> void;
  13. // The `import` declaration.
  14. InstId decl_id;
  15. // True if this is part of an `export import`.
  16. bool is_export;
  17. // The imported IR.
  18. const File* sem_ir;
  19. };
  20. static_assert(sizeof(ImportIR) == 8 + sizeof(uintptr_t), "Unexpected size");
  21. // A reference to an instruction in an imported IR. Used for diagnostics with
  22. // LocId. For a `Cpp` import, points to a Clang source location.
  23. class ImportIRInst : public Printable<ImportIRInst> {
  24. public:
  25. // Constructor for a non-`Cpp` import.
  26. explicit ImportIRInst(ImportIRId ir_id, InstId inst_id)
  27. : ir_id_(ir_id), inst_id_(inst_id) {
  28. CARBON_CHECK(ir_id != ImportIRId::Cpp);
  29. }
  30. // Constructor for a `Cpp` import.
  31. explicit ImportIRInst(ClangSourceLocId clang_source_loc_id)
  32. : ir_id_(ImportIRId::Cpp), clang_source_loc_id_(clang_source_loc_id) {}
  33. auto Print(llvm::raw_ostream& out) const -> void;
  34. friend auto operator==(const ImportIRInst& lhs, const ImportIRInst& rhs)
  35. -> bool {
  36. return lhs.ir_id() == rhs.ir_id() &&
  37. (lhs.ir_id() == ImportIRId::Cpp
  38. ? lhs.clang_source_loc_id() == rhs.clang_source_loc_id()
  39. : lhs.inst_id() == rhs.inst_id());
  40. }
  41. auto ir_id() const -> ImportIRId { return ir_id_; }
  42. auto inst_id() const -> InstId {
  43. CARBON_CHECK(ir_id() != ImportIRId::Cpp);
  44. return inst_id_;
  45. }
  46. auto clang_source_loc_id() const -> ClangSourceLocId {
  47. CARBON_CHECK(ir_id() == ImportIRId::Cpp);
  48. return clang_source_loc_id_;
  49. }
  50. private:
  51. ImportIRId ir_id_;
  52. union {
  53. // Set iff `ir_id != ImportIRId::Cpp`.
  54. InstId inst_id_;
  55. // Set iff `ir_id == ImportIRId::Cpp`.
  56. ClangSourceLocId clang_source_loc_id_;
  57. };
  58. };
  59. // Returns the canonical `File` and `InstId` for an entity, tracing imported
  60. // instructions. Note the returned `File` might not be directly imported by the
  61. // input `sem_ir`.
  62. auto GetCanonicalFileAndInstId(const File* sem_ir, SemIR::InstId inst_id)
  63. -> std::pair<const File*, InstId>;
  64. } // namespace Carbon::SemIR
  65. #endif // CARBON_TOOLCHAIN_SEM_IR_IMPORT_IR_H_