import_ref.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_CHECK_IMPORT_REF_H_
  5. #define CARBON_TOOLCHAIN_CHECK_IMPORT_REF_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/file.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::Check {
  10. // Sets the IR for ImportIRId::ApiForImpl. Should be called before AddImportIR
  11. // in order to ensure the correct ID is assigned.
  12. auto SetApiImportIR(Context& context, SemIR::ImportIR import_ir) -> void;
  13. // Adds an ImportIR, returning the ID. May use an existing ID if already added.
  14. auto AddImportIR(Context& context, SemIR::ImportIR import_ir)
  15. -> SemIR::ImportIRId;
  16. // Adds an import_ref instruction for the specified instruction in the
  17. // specified IR. The import_ref is initially marked as unused.
  18. auto AddImportRef(Context& context, SemIR::ImportIRInst import_ir_inst,
  19. SemIR::EntityNameId entity_name_id) -> SemIR::InstId;
  20. // Returns the canonical IR inst for an entity. Returns an `ImportIRInst` with
  21. // a `None` ir_id for an entity that was not imported.
  22. auto GetCanonicalImportIRInst(Context& context, SemIR::InstId inst_id)
  23. -> SemIR::ImportIRInst;
  24. // Verifies a new instruction is the same as a previous instruction.
  25. // prev_import_ir_inst should come from GetCanonicalImportIRInst.
  26. auto VerifySameCanonicalImportIRInst(Context& context, SemIR::NameId name_id,
  27. SemIR::InstId prev_id,
  28. SemIR::ImportIRInst prev_import_ir_inst,
  29. SemIR::ImportIRId new_ir_id,
  30. const SemIR::File* new_import_ir,
  31. SemIR::InstId new_inst_id) -> void;
  32. // If the passed in instruction ID is an ImportRefUnloaded, turns it into an
  33. // ImportRefLoaded for use.
  34. auto LoadImportRef(Context& context, SemIR::InstId inst_id) -> void;
  35. // Load all impls declared in the api file corresponding to this impl file.
  36. auto ImportImplsFromApiFile(Context& context) -> void;
  37. // Load a specific impl declared in an imported IR.
  38. auto ImportImpl(Context& context, SemIR::ImportIRId import_ir_id,
  39. SemIR::ImplId impl_id) -> void;
  40. namespace Internal {
  41. // Checks that the provided imported location has a node kind that is
  42. // compatible with that of the given instruction.
  43. auto CheckCompatibleImportedNodeKind(Context& context,
  44. SemIR::ImportIRInstId imported_loc_id,
  45. SemIR::InstKind kind) -> void;
  46. } // namespace Internal
  47. // Returns a LocIdAndInst for an instruction with an imported location. Checks
  48. // that the imported location is compatible with the kind of instruction being
  49. // created.
  50. template <typename InstT>
  51. requires SemIR::Internal::HasNodeId<InstT>
  52. auto MakeImportedLocIdAndInst(Context& context,
  53. SemIR::ImportIRInstId imported_loc_id, InstT inst)
  54. -> SemIR::LocIdAndInst {
  55. if constexpr (!SemIR::Internal::HasUntypedNodeId<InstT>) {
  56. Internal::CheckCompatibleImportedNodeKind(context, imported_loc_id,
  57. InstT::Kind);
  58. }
  59. return SemIR::LocIdAndInst::UncheckedLoc(imported_loc_id, inst);
  60. }
  61. } // namespace Carbon::Check
  62. #endif // CARBON_TOOLCHAIN_CHECK_IMPORT_REF_H_