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