import.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_CPP_IMPORT_H_
  5. #define CARBON_TOOLCHAIN_CHECK_CPP_IMPORT_H_
  6. #include "llvm/ADT/ArrayRef.h"
  7. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/Support/VirtualFileSystem.h"
  10. #include "toolchain/check/context.h"
  11. #include "toolchain/check/convert.h"
  12. #include "toolchain/check/diagnostic_helpers.h"
  13. #include "toolchain/diagnostics/diagnostic_emitter.h"
  14. #include "toolchain/sem_ir/ids.h"
  15. namespace Carbon::Check {
  16. // Generates a C++ header that includes the imported cpp files, parses it,
  17. // generates the AST from it and links `SemIR::File` to it. Reports C++ errors
  18. // and warnings. If successful, adds a `Cpp` namespace.
  19. auto ImportCpp(Context& context,
  20. llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
  21. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  22. llvm::LLVMContext* llvm_context,
  23. std::shared_ptr<clang::CompilerInvocation> invocation) -> void;
  24. // Imports a declaration from Clang to Carbon. If successful, returns the new
  25. // Carbon declaration `InstId`. If the declaration was already imported, returns
  26. // the mapped instruction. All unimported dependencies are imported first.
  27. auto ImportCppDecl(Context& context, SemIR::LocId loc_id,
  28. SemIR::ClangDeclKey key) -> SemIR::InstId;
  29. // Imports a function declaration from Clang to Carbon. If successful, returns
  30. // the new Carbon function declaration `InstId`. If the declaration was already
  31. // imported, returns the mapped instruction.
  32. inline auto ImportCppFunctionDecl(Context& context, SemIR::LocId loc_id,
  33. clang::FunctionDecl* clang_decl,
  34. int num_params) -> SemIR::InstId {
  35. return ImportCppDecl(
  36. context, loc_id,
  37. SemIR::ClangDeclKey::ForFunctionDecl(clang_decl, num_params));
  38. }
  39. // Imports a function declaration from Clang to Carbon. If successful, returns
  40. // the new Carbon function declaration `InstId`. If the declaration was already
  41. // imported, returns the mapped instruction. All unimported dependencies are
  42. // imported first.
  43. auto ImportCppType(Context& context, SemIR::LocId loc_id, clang::QualType type)
  44. -> TypeExpr;
  45. // Imports an overloaded function set from Clang to Carbon.
  46. auto ImportCppOverloadSet(
  47. Context& context, SemIR::LocId loc_id, SemIR::NameScopeId scope_id,
  48. SemIR::NameId name_id, clang::CXXRecordDecl* naming_class,
  49. clang::UnresolvedSet<4>&& overload_set,
  50. clang::OverloadCandidateSet::OperatorRewriteInfo operator_rewrite_info)
  51. -> SemIR::InstId;
  52. // Looks up the given name in the Clang AST generated when importing C++ code
  53. // and returns a lookup result. If using the injected class name (`X.X()`),
  54. // imports the class constructor as a function named as the class.
  55. auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
  56. SemIR::NameScopeId scope_id, SemIR::NameId name_id)
  57. -> SemIR::ScopeLookupResult;
  58. // Given a Carbon class declaration that was imported from some kind of C++
  59. // declaration, such as a class or enum, attempt to import a corresponding class
  60. // definition. Returns true if nothing went wrong (whether or not a definition
  61. // could be imported), false if a diagnostic was produced.
  62. auto ImportClassDefinitionForClangDecl(Context& context, SemIR::LocId loc_id,
  63. SemIR::ClassId class_id,
  64. SemIR::ClangDeclId clang_decl_id)
  65. -> bool;
  66. } // namespace Carbon::Check
  67. #endif // CARBON_TOOLCHAIN_CHECK_CPP_IMPORT_H_