context.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. auto placement_new_decl() const -> clang::FunctionDecl* {
  32. return placement_new_decl_;
  33. }
  34. void set_placement_new_decl(clang::FunctionDecl* decl) {
  35. placement_new_decl_ = decl;
  36. }
  37. private:
  38. // The Clang AST context.
  39. clang::ASTContext* ast_context_;
  40. // The Clang semantic analysis engine.
  41. clang::Sema* sema_;
  42. // The Clang parser.
  43. std::unique_ptr<clang::Parser> parser_;
  44. // Per-Carbon-file start locations for corresponding Clang source buffers.
  45. // Owned and managed by code in location.cpp.
  46. llvm::SmallVector<clang::SourceLocation> carbon_file_locations_;
  47. // The Clang mangle context for the target in the ASTContext.
  48. std::unique_ptr<clang::MangleContext> clang_mangle_context_;
  49. // The cached placement new function declaration.
  50. clang::FunctionDecl* placement_new_decl_ = nullptr;
  51. };
  52. } // namespace Carbon::Check
  53. #endif // CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_