context.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "clang/Parse/Parser.h"
  11. #include "common/check.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. namespace Carbon::Check {
  14. // Context for C++ code during check.
  15. //
  16. // This stores state for a Clang AST and Sema, as well as any additional
  17. // information needed to perform mapping between Carbon and C++ types,
  18. // declarations, and similar values.
  19. class CppContext {
  20. public:
  21. explicit CppContext(clang::CompilerInstance& instance,
  22. std::unique_ptr<clang::Parser> parser);
  23. ~CppContext();
  24. auto ast_context() -> clang::ASTContext& { return *ast_context_; }
  25. auto sema() -> clang::Sema& { return *sema_; }
  26. auto parser() -> clang::Parser& { return *parser_; }
  27. auto clang_mangle_context() -> clang::MangleContext&;
  28. auto carbon_file_locations() -> llvm::SmallVector<clang::SourceLocation>& {
  29. return carbon_file_locations_;
  30. }
  31. private:
  32. // The Clang AST context.
  33. clang::ASTContext* ast_context_;
  34. // The Clang semantic analysis engine.
  35. clang::Sema* sema_;
  36. // The Clang parser.
  37. std::unique_ptr<clang::Parser> parser_;
  38. // Per-Carbon-file start locations for corresponding Clang source buffers.
  39. // Owned and managed by code in location.cpp.
  40. llvm::SmallVector<clang::SourceLocation> carbon_file_locations_;
  41. // The Clang mangle context for the target in the ASTContext.
  42. std::unique_ptr<clang::MangleContext> clang_mangle_context_;
  43. };
  44. } // namespace Carbon::Check
  45. #endif // CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_