import.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. SemIR::NameScopeId name_scope_id;
  12. SemIR::InstId inst_id;
  13. bool is_duplicate_of_namespace_in_current_package;
  14. };
  15. // Adds a namespace to the IR. The bool on return is true if there was a name
  16. // conflict. diagnose_duplicate_namespace is used when handling a cross-package
  17. // import, where an existing namespace is in the current package and the new
  18. // namespace is a different package.
  19. auto AddImportNamespace(Context& context, SemIR::TypeId namespace_type_id,
  20. SemIR::NameId name_id,
  21. SemIR::NameScopeId parent_scope_id,
  22. bool diagnose_duplicate_namespace,
  23. llvm::function_ref<SemIR::InstId()> make_import_id)
  24. -> AddImportNamespaceResult;
  25. // Imports the API file's name lookup information into a corresponding
  26. // implementation file. Only information for the current package will be copied;
  27. // information for other packages should be handled through
  28. // ImportLibrariesFromOtherPackage.
  29. auto ImportApiFile(Context& context, SemIR::TypeId namespace_type_id,
  30. const SemIR::File& api_sem_ir) -> void;
  31. // Add the current package's imports to name lookup. This pulls in all names;
  32. // conflicts for things such as `package.a.b.c` will be flagged even though they
  33. // are several layers deep.
  34. auto ImportLibrariesFromCurrentPackage(
  35. Context& context, SemIR::TypeId namespace_type_id,
  36. llvm::ArrayRef<SemIR::ImportIR> import_irs) -> void;
  37. // Adds another package's imports to name lookup. This only adds the package
  38. // name to lookup, so that `package.ImportedPackage` will resolve, and will
  39. // provide a name scope that can be used for further qualified name lookups.
  40. //
  41. // import_irs may be empty. has_load_error is used to indicate if any library in
  42. // the package failed to import correctly.
  43. auto ImportLibrariesFromOtherPackage(Context& context,
  44. SemIR::TypeId namespace_type_id,
  45. SemIR::InstId import_decl_id,
  46. PackageNameId package_id,
  47. llvm::ArrayRef<SemIR::ImportIR> import_irs,
  48. bool has_load_error) -> void;
  49. // Given a name scope that corresponds to another package (having one or more
  50. // `import_irs`), looks for the name in imports. Name resolution results are
  51. // added to the scope, and the `InstId` (possibly `None`) is returned.
  52. //
  53. // In general, this will add an `ImportRef` and load it; it's never left
  54. // unloaded because the result is expected to be immediately used. Namespaces
  55. // will be directly produced, similar to how they function for imports from the
  56. // current package. Conflicts will be resolved and diagnosed.
  57. //
  58. // Arguments are all in the context of the current IR. Scope lookup is expected
  59. // to be resolved first.
  60. auto ImportNameFromOtherPackage(
  61. Context& context, SemIRLoc loc, SemIR::NameScopeId scope_id,
  62. llvm::ArrayRef<std::pair<SemIR::ImportIRId, SemIR::NameScopeId>>
  63. import_ir_scopes,
  64. SemIR::NameId name_id) -> SemIR::InstId;
  65. } // namespace Carbon::Check
  66. #endif // CARBON_TOOLCHAIN_CHECK_IMPORT_H_