context.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(std::unique_ptr<clang::FrontendAction> action);
  22. ~CppContext();
  23. auto action() -> clang::FrontendAction& { return *action_; }
  24. auto ast_context() -> clang::ASTContext& {
  25. return action_->getCompilerInstance().getASTContext();
  26. }
  27. auto sema() -> clang::Sema& {
  28. return action_->getCompilerInstance().getSema();
  29. }
  30. auto parser() -> clang::Parser& { return *parser_; }
  31. auto set_parser(std::unique_ptr<clang::Parser> parser) {
  32. CARBON_CHECK(!parser_);
  33. parser_ = std::move(parser);
  34. }
  35. auto clang_mangle_context() -> clang::MangleContext&;
  36. auto carbon_file_locations() -> llvm::SmallVector<clang::SourceLocation>& {
  37. return carbon_file_locations_;
  38. }
  39. private:
  40. // The clang action that is generating the C++ AST.
  41. std::unique_ptr<clang::FrontendAction> action_;
  42. // Per-Carbon-file start locations for corresponding Clang source buffers.
  43. // Owned and managed by code in location.cpp.
  44. llvm::SmallVector<clang::SourceLocation> carbon_file_locations_;
  45. // The Clang mangle context for the target in the ASTContext.
  46. std::unique_ptr<clang::MangleContext> clang_mangle_context_;
  47. // The Clang parser.
  48. std::unique_ptr<clang::Parser> parser_;
  49. };
  50. } // namespace Carbon::Check
  51. #endif // CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_