import.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // Given a clang declaration ID that was previously imported into another file,
  26. // returns the corresponding clang declaration key in the current context.
  27. // Produces an error and returns nullopt on failure.
  28. auto FindCorrespondingClangDeclKey(Context& context, SemIR::LocId loc_id,
  29. const SemIR::File& file,
  30. SemIR::ClangDeclId clang_decl_id)
  31. -> std::optional<SemIR::ClangDeclKey>;
  32. // Imports a declaration into the current context that was previously imported
  33. // into another file.
  34. auto ImportCppDeclFromFile(Context& context, SemIR::LocId loc_id,
  35. const SemIR::File& file,
  36. SemIR::ClangDeclId clang_decl_id)
  37. -> SemIR::ConstantId;
  38. // Imports a constant into the current context that was previously imported into
  39. // another file.
  40. auto ImportCppConstantFromFile(Context& context, SemIR::LocId loc_id,
  41. const SemIR::File& file, SemIR::InstId inst_id)
  42. -> SemIR::ConstantId;
  43. // Imports a declaration from Clang to Carbon. If successful, returns the new
  44. // Carbon declaration `InstId`. If the declaration was already imported, returns
  45. // the mapped instruction. All unimported dependencies are imported first.
  46. auto ImportCppDecl(Context& context, SemIR::LocId loc_id,
  47. SemIR::ClangDeclKey key) -> SemIR::InstId;
  48. // Imports a function declaration from Clang to Carbon. If successful, returns
  49. // the new Carbon function declaration `InstId`. If the declaration was already
  50. // imported, returns the mapped instruction.
  51. inline auto ImportCppFunctionDecl(Context& context, SemIR::LocId loc_id,
  52. clang::FunctionDecl* clang_decl,
  53. SemIR::ClangDeclKey::Signature signature)
  54. -> SemIR::InstId {
  55. return ImportCppDecl(
  56. context, loc_id,
  57. SemIR::ClangDeclKey::ForFunctionDecl(clang_decl, signature));
  58. }
  59. // Imports a function declaration from Clang to Carbon. If successful, returns
  60. // the new Carbon function declaration `InstId`. If the declaration was already
  61. // imported, returns the mapped instruction. All unimported dependencies are
  62. // imported first.
  63. auto ImportCppType(Context& context, SemIR::LocId loc_id, clang::QualType type)
  64. -> TypeExpr;
  65. // Imports an overloaded function set from Clang to Carbon.
  66. auto ImportCppOverloadSet(
  67. Context& context, SemIR::LocId loc_id, SemIR::NameScopeId scope_id,
  68. SemIR::NameId name_id, clang::CXXRecordDecl* naming_class,
  69. clang::UnresolvedSet<4>&& overload_set,
  70. clang::OverloadCandidateSet::OperatorRewriteInfo operator_rewrite_info)
  71. -> SemIR::InstId;
  72. // Looks up the given name in the Clang AST generated when importing C++ code
  73. // and returns a lookup result. If using the injected class name (`X.X()`),
  74. // imports the class constructor as a function named as the class.
  75. auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
  76. SemIR::NameScopeId scope_id, SemIR::NameId name_id)
  77. -> SemIR::ScopeLookupResult;
  78. // Given a Carbon class declaration that was imported from some kind of C++
  79. // declaration, such as a class or enum, attempt to import a corresponding class
  80. // definition. Returns true if nothing went wrong (whether or not a definition
  81. // could be imported), false if a diagnostic was produced.
  82. auto ImportClassDefinitionForClangDecl(Context& context,
  83. SemIR::ClassId class_id,
  84. SemIR::ClangDeclId clang_decl_id)
  85. -> bool;
  86. // Gets the identifier info for a name. Returns `nullptr` if the name is not an
  87. // identifier name.
  88. auto GetClangIdentifierInfo(Context& context, SemIR::NameId name_id)
  89. -> clang::IdentifierInfo*;
  90. // Maps from a `VarStorage` instruction to a `clang::VarDecl`. Returns
  91. // null if the instruction is not a `VarStorage`, or if its contents
  92. // cannot be mapped to a `clang::VarDecl`.
  93. auto GetAsClangVarDecl(Context& context, SemIR::InstId inst_id)
  94. -> clang::VarDecl*;
  95. // Maps a Clang name to a Carbon `NameId`.
  96. auto AddIdentifierName(Context& context, llvm::StringRef name) -> SemIR::NameId;
  97. } // namespace Carbon::Check
  98. #endif // CARBON_TOOLCHAIN_CHECK_CPP_IMPORT_H_