cpp_file.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_SEM_IR_CPP_FILE_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_CPP_FILE_H_
  6. #include "clang/Basic/CodeGenOptions.h"
  7. #include "clang/Basic/Diagnostic.h"
  8. #include "clang/CodeGen/ModuleBuilder.h"
  9. #include "clang/Frontend/CompilerInstance.h"
  10. #include "clang/Lex/PreprocessorOptions.h"
  11. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  12. #include "llvm/IR/Module.h"
  13. #include "llvm/Support/FileSystem.h"
  14. namespace Carbon::SemIR {
  15. // The result of compiling the C++ portion of a `File`, including both any
  16. // imported C++ headers and any inline C++ fragments.
  17. class CppFile {
  18. public:
  19. explicit CppFile(std::unique_ptr<clang::CompilerInstance> clang,
  20. llvm::LLVMContext* llvm_context)
  21. : clang_(std::move(clang)), llvm_context_(llvm_context) {}
  22. // Access to compilation options.
  23. auto diagnostic_options() const -> const clang::DiagnosticOptions& {
  24. return clang_->getDiagnostics().getDiagnosticOptions();
  25. }
  26. auto lang_options() const -> const clang::LangOptions& {
  27. return clang_->getLangOpts();
  28. }
  29. // Access to Clang's compilation environment.
  30. auto source_manager() -> clang::SourceManager& {
  31. return clang_->getSourceManager();
  32. }
  33. auto source_manager() const -> const clang::SourceManager& {
  34. return clang_->getSourceManager();
  35. }
  36. // TODO: This doesn't really belong here, but is currently used by lowering
  37. // because Clang's code generation may produce diagnostics.
  38. auto diagnostics() const -> clang::DiagnosticsEngine& {
  39. return clang_->getDiagnostics();
  40. }
  41. // Access to layers of Clang's C++ representation.
  42. auto ast_context() -> clang::ASTContext& { return clang_->getASTContext(); }
  43. auto ast_context() const -> const clang::ASTContext& {
  44. return clang_->getASTContext();
  45. }
  46. auto llvm_context() const -> llvm::LLVMContext* { return llvm_context_; }
  47. auto SetCodeGenerator(clang::CodeGenerator* code_generator) -> void {
  48. code_generator_ = code_generator;
  49. }
  50. auto GetCodeGenerator() const -> clang::CodeGenerator* {
  51. // Clang code generation should not actually modify the AST, but isn't
  52. // const-correct.
  53. return code_generator_;
  54. }
  55. private:
  56. std::unique_ptr<clang::CompilerInstance> clang_;
  57. llvm::LLVMContext* llvm_context_;
  58. clang::CodeGenerator* code_generator_ = nullptr;
  59. };
  60. } // namespace Carbon::SemIR
  61. #endif // CARBON_TOOLCHAIN_SEM_IR_CPP_FILE_H_