1//===--- BackendConsumer.h - LLVM BackendConsumer Header File -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_LIB_CODEGEN_BACKENDCONSUMER_H
10#define LLVM_CLANG_LIB_CODEGEN_BACKENDCONSUMER_H
11
12#include "clang/CodeGen/BackendUtil.h"
13#include "clang/CodeGen/CodeGenAction.h"
14#include "clang/CodeGen/ModuleLinker.h"
15#include "clang/CodeGenUtils/BackendDiagnosticHandler.h"
16
17#include "llvm/Support/Timer.h"
18
19namespace clang {
20class ASTContext;
21class CodeGenAction;
22class CoverageSourceInfo;
23
24class BackendConsumer : public ASTConsumer {
25 virtual void anchor();
26 CompilerInstance &CI;
27 DiagnosticsEngine &Diags;
28 const CodeGenOptions &CodeGenOpts;
29 const TargetOptions &TargetOpts;
30 const LangOptions &LangOpts;
31 std::unique_ptr<raw_pwrite_stream> AsmOutStream;
32 ASTContext *Context = nullptr;
33 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
34
35 llvm::Timer LLVMIRGeneration;
36 unsigned LLVMIRGenerationRefCount = 0;
37
38 bool TimerIsEnabled = false;
39
40 BackendAction Action;
41
42 std::unique_ptr<CodeGenerator> Gen;
43
44 SmallVector<LinkModule, 4> LinkModules;
45
46 // Translates LLVM backend diagnostics into clang diagnostics; shared with
47 // CIR so that both LLVM-emitting pipelines report backend diagnostics
48 // through the same mechanism.
49 BackendDiagnosticConsumer DiagConsumer;
50
51public:
52 BackendConsumer(CompilerInstance &CI, BackendAction Action,
53 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
54 llvm::LLVMContext &C, SmallVector<LinkModule, 4> LinkModules,
55 StringRef InFile, std::unique_ptr<raw_pwrite_stream> OS,
56 CoverageSourceInfo *CoverageInfo,
57 llvm::Module *CurLinkModule = nullptr);
58
59 llvm::Module *getModule() const;
60 std::unique_ptr<llvm::Module> takeModule();
61
62 CodeGenerator *getCodeGenerator();
63
64 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override;
65 void Initialize(ASTContext &Ctx) override;
66 bool HandleTopLevelDecl(DeclGroupRef D) override;
67 void HandleInlineFunctionDefinition(FunctionDecl *D) override;
68 void HandleInterestingDecl(DeclGroupRef D) override;
69 void HandleTranslationUnit(ASTContext &C) override;
70 void HandleTagDeclDefinition(TagDecl *D) override;
71 void HandleTagDeclRequiredDefinition(const TagDecl *D) override;
72 void CompleteTentativeDefinition(VarDecl *D) override;
73 void CompleteExternalDeclaration(DeclaratorDecl *D) override;
74 void AssignInheritanceModel(CXXRecordDecl *RD) override;
75 void HandleVTable(CXXRecordDecl *RD) override;
76
77 // Links each entry in LinkModules into our module. Returns true on error.
78 bool LinkInModules(llvm::Module *M);
79 /// Create an llvm::DiagnosticHandler that routes LLVM backend diagnostics
80 /// through this consumer's clang diagnostics.
81 std::unique_ptr<llvm::DiagnosticHandler> createDiagnosticHandler();
82};
83
84} // namespace clang
85#endif
86