import_ir.h 2.7 KB

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