import.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_H_
  5. #define CARBON_TOOLCHAIN_CHECK_IMPORT_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/parse/node_ids.h"
  8. #include "toolchain/sem_ir/file.h"
  9. namespace Carbon::Check {
  10. struct AddImportNamespaceResult {
  11. // The namespace scope id.
  12. SemIR::NameScopeId name_scope_id;
  13. // The namespace instruction id.
  14. SemIR::InstId inst_id;
  15. };
  16. // Adds a namespace to the IR. Associates the namespace with the import referred
  17. // in `import_id`, if any. Do not try to add the namespace `name_id` to the
  18. // scope and don't check if it already exists. Used when the name is added
  19. // afterwards.
  20. auto AddImportNamespace(Context& context, SemIR::TypeId namespace_type_id,
  21. SemIR::NameId name_id,
  22. SemIR::NameScopeId parent_scope_id,
  23. SemIR::InstId import_id) -> AddImportNamespaceResult;
  24. struct AddImportNamespaceToScopeResult {
  25. AddImportNamespaceResult add_result;
  26. // When trying to add the namespace name, whether it already exists and refers
  27. // to a namespace.
  28. bool is_duplicate_of_namespace_in_current_package = false;
  29. };
  30. // Adds a namespace to the IR. Associates the namespace with the import returned
  31. // from `make_import_id`, which must return a value. Tries to add the namespace
  32. // `name_id` to the scope. If the name already exists, diagnose only if
  33. // `diagnose_duplicate_namespace` is true or if it doesn't refer to a namespace.
  34. // `diagnose_duplicate_namespace` is used when handling a cross-package import,
  35. // where an existing namespace is in the current package and the new namespace
  36. // is a different package.
  37. auto AddImportNamespaceToScope(
  38. Context& context, SemIR::TypeId namespace_type_id, SemIR::NameId name_id,
  39. SemIR::NameScopeId parent_scope_id, bool diagnose_duplicate_namespace,
  40. llvm::function_ref<SemIR::InstId()> make_import_id)
  41. -> AddImportNamespaceToScopeResult;
  42. // Imports the API file's name lookup information into a corresponding
  43. // implementation file. Only information for the current package will be copied;
  44. // information for other packages should be handled through
  45. // ImportLibrariesFromOtherPackage.
  46. auto ImportApiFile(Context& context, SemIR::TypeId namespace_type_id,
  47. const SemIR::File& api_sem_ir) -> void;
  48. // Add the current package's imports to name lookup. This pulls in all names;
  49. // conflicts for things such as `package.a.b.c` will be flagged even though they
  50. // are several layers deep.
  51. auto ImportLibrariesFromCurrentPackage(
  52. Context& context, SemIR::TypeId namespace_type_id,
  53. llvm::ArrayRef<SemIR::ImportIR> import_irs) -> void;
  54. // Adds another package's imports to name lookup. This only adds the package
  55. // name to lookup, so that `package.ImportedPackage` will resolve, and will
  56. // provide a name scope that can be used for further qualified name lookups.
  57. //
  58. // import_irs may be empty. has_load_error is used to indicate if any library in
  59. // the package failed to import correctly.
  60. auto ImportLibrariesFromOtherPackage(Context& context,
  61. SemIR::TypeId namespace_type_id,
  62. SemIR::InstId import_decl_id,
  63. PackageNameId package_id,
  64. llvm::ArrayRef<SemIR::ImportIR> import_irs,
  65. bool has_load_error) -> void;
  66. // Given a name scope that corresponds to another package (having one or more
  67. // `import_irs`), looks for the name in imports. Name resolution results are
  68. // added to the scope, and the `InstId` (possibly `None`) is returned.
  69. //
  70. // In general, this will add an `ImportRef` and load it; it's never left
  71. // unloaded because the result is expected to be immediately used. Namespaces
  72. // will be directly produced, similar to how they function for imports from the
  73. // current package. Conflicts will be resolved and diagnosed.
  74. //
  75. // Arguments are all in the context of the current IR. Scope lookup is expected
  76. // to be resolved first.
  77. auto ImportNameFromOtherPackage(
  78. Context& context, SemIR::LocId loc_id, SemIR::NameScopeId scope_id,
  79. llvm::ArrayRef<std::pair<SemIR::ImportIRId, SemIR::NameScopeId>>
  80. import_ir_scopes,
  81. SemIR::NameId name_id) -> SemIR::InstId;
  82. namespace Internal {
  83. // Checks that the provided imported location has a node kind that is
  84. // compatible with that of the given instruction.
  85. auto CheckCompatibleImportedNodeKind(Context& context,
  86. SemIR::ImportIRInstId imported_loc_id,
  87. SemIR::InstKind kind) -> void;
  88. } // namespace Internal
  89. // Returns a LocIdAndInst for an instruction with an imported location. Checks
  90. // that the imported location is compatible with the kind of instruction being
  91. // created.
  92. template <typename InstT>
  93. requires SemIR::Internal::HasNodeId<InstT>
  94. auto MakeImportedLocIdAndInst(Context& context,
  95. SemIR::ImportIRInstId imported_loc_id, InstT inst)
  96. -> SemIR::LocIdAndInst {
  97. if constexpr (!SemIR::Internal::HasUntypedNodeId<InstT>) {
  98. Internal::CheckCompatibleImportedNodeKind(context, imported_loc_id,
  99. InstT::Kind);
  100. }
  101. return SemIR::LocIdAndInst::UncheckedLoc(SemIR::LocId(imported_loc_id), inst);
  102. }
  103. } // namespace Carbon::Check
  104. #endif // CARBON_TOOLCHAIN_CHECK_IMPORT_H_