]> granicus.if.org Git - clang/blob - lib/CodeGen/ModuleBuilder.cpp
[modules] Fix a rejects-valid resulting from emitting an inline function
[clang] / lib / CodeGen / ModuleBuilder.cpp
1 //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This builds an AST and converts it to LLVM Code.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/CodeGen/ModuleBuilder.h"
15 #include "CGDebugInfo.h"
16 #include "CodeGenModule.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/TargetInfo.h"
22 #include "clang/Frontend/CodeGenOptions.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include <memory>
28 using namespace clang;
29
30 namespace {
31   class CodeGeneratorImpl : public CodeGenerator {
32     DiagnosticsEngine &Diags;
33     std::unique_ptr<const llvm::DataLayout> TD;
34     ASTContext *Ctx;
35     const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
36
37     unsigned HandlingTopLevelDecls;
38     struct HandlingTopLevelDeclRAII {
39       CodeGeneratorImpl &Self;
40       HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self) : Self(Self) {
41         ++Self.HandlingTopLevelDecls;
42       }
43       ~HandlingTopLevelDeclRAII() {
44         if (--Self.HandlingTopLevelDecls == 0)
45           Self.EmitDeferredDecls();
46       }
47     };
48
49     CoverageSourceInfo *CoverageInfo;
50
51   protected:
52     std::unique_ptr<llvm::Module> M;
53     std::unique_ptr<CodeGen::CodeGenModule> Builder;
54
55   public:
56     CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
57                       const CodeGenOptions &CGO, llvm::LLVMContext& C,
58                       CoverageSourceInfo *CoverageInfo = nullptr)
59       : Diags(diags), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
60         CoverageInfo(CoverageInfo),
61         M(new llvm::Module(ModuleName, C)) {}
62
63     virtual ~CodeGeneratorImpl() {}
64
65     llvm::Module* GetModule() override {
66       return M.get();
67     }
68
69     const Decl *GetDeclForMangledName(StringRef MangledName) override {
70       GlobalDecl Result;
71       if (!Builder->lookupRepresentativeDecl(MangledName, Result))
72         return nullptr;
73       const Decl *D = Result.getCanonicalDecl().getDecl();
74       if (auto FD = dyn_cast<FunctionDecl>(D)) {
75         if (FD->hasBody(FD))
76           return FD;
77       } else if (auto TD = dyn_cast<TagDecl>(D)) {
78         if (auto Def = TD->getDefinition())
79           return Def;
80       }
81       return D;
82     }
83
84     llvm::Module *ReleaseModule() override { return M.release(); }
85
86     void Initialize(ASTContext &Context) override {
87       Ctx = &Context;
88
89       M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
90       M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
91       TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
92       Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
93                                                Diags, CoverageInfo));
94
95       for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
96         HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
97     }
98
99     void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
100       if (Diags.hasErrorOccurred())
101         return;
102
103       Builder->HandleCXXStaticMemberVarInstantiation(VD);
104     }
105
106     bool HandleTopLevelDecl(DeclGroupRef DG) override {
107       if (Diags.hasErrorOccurred())
108         return true;
109
110       HandlingTopLevelDeclRAII HandlingDecl(*this);
111
112       // Make sure to emit all elements of a Decl.
113       for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
114         Builder->EmitTopLevelDecl(*I);
115
116       return true;
117     }
118
119     void EmitDeferredDecls() {
120       if (DeferredInlineMethodDefinitions.empty())
121         return;
122
123       // Emit any deferred inline method definitions. Note that more deferred
124       // methods may be added during this loop, since ASTConsumer callbacks
125       // can be invoked if AST inspection results in declarations being added.
126       HandlingTopLevelDeclRAII HandlingDecl(*this);
127       for (unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
128         Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
129       DeferredInlineMethodDefinitions.clear();
130     }
131
132     void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
133       if (Diags.hasErrorOccurred())
134         return;
135
136       assert(D->doesThisDeclarationHaveABody());
137
138       // We may want to emit this definition. However, that decision might be
139       // based on computing the linkage, and we have to defer that in case we
140       // are inside of something that will change the method's final linkage,
141       // e.g.
142       //   typedef struct {
143       //     void bar();
144       //     void foo() { bar(); }
145       //   } A;
146       DeferredInlineMethodDefinitions.push_back(D);
147
148       // Always provide some coverage mapping
149       // even for the methods that aren't emitted.
150       Builder->AddDeferredUnusedCoverageMapping(D);
151     }
152
153     /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
154     /// to (e.g. struct, union, enum, class) is completed. This allows the
155     /// client hack on the type, which can occur at any point in the file
156     /// (because these can be defined in declspecs).
157     void HandleTagDeclDefinition(TagDecl *D) override {
158       if (Diags.hasErrorOccurred())
159         return;
160
161       Builder->UpdateCompletedType(D);
162
163       // For MSVC compatibility, treat declarations of static data members with
164       // inline initializers as definitions.
165       if (Ctx->getLangOpts().MSVCCompat) {
166         for (Decl *Member : D->decls()) {
167           if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
168             if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
169                 Ctx->DeclMustBeEmitted(VD)) {
170               Builder->EmitGlobal(VD);
171             }
172           }
173         }
174       }
175     }
176
177     void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
178       if (Diags.hasErrorOccurred())
179         return;
180
181       if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
182         if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
183           DI->completeRequiredType(RD);
184     }
185
186     void HandleTranslationUnit(ASTContext &Ctx) override {
187       if (Diags.hasErrorOccurred()) {
188         if (Builder)
189           Builder->clear();
190         M.reset();
191         return;
192       }
193
194       if (Builder)
195         Builder->Release();
196     }
197
198     void CompleteTentativeDefinition(VarDecl *D) override {
199       if (Diags.hasErrorOccurred())
200         return;
201
202       Builder->EmitTentativeDefinition(D);
203     }
204
205     void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
206       if (Diags.hasErrorOccurred())
207         return;
208
209       Builder->EmitVTable(RD, DefinitionRequired);
210     }
211
212     void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
213       Builder->AppendLinkerOptions(Opts);
214     }
215
216     void HandleDetectMismatch(llvm::StringRef Name,
217                               llvm::StringRef Value) override {
218       Builder->AddDetectMismatch(Name, Value);
219     }
220
221     void HandleDependentLibrary(llvm::StringRef Lib) override {
222       Builder->AddDependentLib(Lib);
223     }
224
225   private:
226     std::vector<CXXMethodDecl *> DeferredInlineMethodDefinitions;
227   };
228 }
229
230 void CodeGenerator::anchor() { }
231
232 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
233                                         const std::string& ModuleName,
234                                         const CodeGenOptions &CGO,
235                                         const TargetOptions &/*TO*/,
236                                         llvm::LLVMContext& C,
237                                         CoverageSourceInfo *CoverageInfo) {
238   return new CodeGeneratorImpl(Diags, ModuleName, CGO, C, CoverageInfo);
239 }