import.h 4.9 KB

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