context.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_
  6. #include <memory>
  7. #include "clang/Basic/SourceLocation.h"
  8. #include "clang/Frontend/CompilerInstance.h"
  9. #include "clang/Frontend/FrontendAction.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. namespace Carbon::Check {
  12. // Context for C++ code during check.
  13. //
  14. // This stores state for a Clang AST and Sema, as well as any additional
  15. // information needed to perform mapping between Carbon and C++ types,
  16. // declarations, and similar values.
  17. class CppContext {
  18. public:
  19. explicit CppContext(std::unique_ptr<clang::FrontendAction> action);
  20. ~CppContext();
  21. auto action() -> clang::FrontendAction& { return *action_; }
  22. auto ast_context() -> clang::ASTContext& {
  23. return action_->getCompilerInstance().getASTContext();
  24. }
  25. auto sema() -> clang::Sema& {
  26. return action_->getCompilerInstance().getSema();
  27. }
  28. auto clang_mangle_context() -> clang::MangleContext&;
  29. auto carbon_file_locations() -> llvm::SmallVector<clang::SourceLocation>& {
  30. return carbon_file_locations_;
  31. }
  32. private:
  33. // The clang action that is generating the C++ AST.
  34. std::unique_ptr<clang::FrontendAction> action_;
  35. // Per-Carbon-file start locations for corresponding Clang source buffers.
  36. // Owned and managed by code in location.cpp.
  37. llvm::SmallVector<clang::SourceLocation> carbon_file_locations_;
  38. // The Clang mangle context for the target in the ASTContext.
  39. std::unique_ptr<clang::MangleContext> clang_mangle_context_;
  40. };
  41. } // namespace Carbon::Check
  42. #endif // CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_