Chris Lattner | f97fe38 | 2007-05-24 06:29:05 +0000 | [diff] [blame] | 1 | //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | f97fe38 | 2007-05-24 06:29:05 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This builds an AST and converts it to LLVM Code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/CodeGen/ModuleBuilder.h" |
David Blaikie | 48ad6dc | 2013-07-13 21:08:14 +0000 | [diff] [blame] | 15 | #include "CGDebugInfo.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Chris Lattner | adf1f51 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/Expr.h" |
Chris Lattner | adf1f51 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
| 21 | #include "clang/Basic/TargetInfo.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CodeGenOptions.h" |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DataLayout.h" |
| 25 | #include "llvm/IR/LLVMContext.h" |
| 26 | #include "llvm/IR/Module.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 27 | #include <memory> |
John McCall | 6909fee | 2016-05-18 05:21:18 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 984fac5 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 29 | using namespace clang; |
John McCall | 6909fee | 2016-05-18 05:21:18 +0000 | [diff] [blame] | 30 | using namespace CodeGen; |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 31 | |
Chris Lattner | adf1f51 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 32 | namespace { |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 33 | class CodeGeneratorImpl : public CodeGenerator { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 34 | DiagnosticsEngine &Diags; |
Chris Lattner | adf1f51 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 35 | ASTContext *Ctx; |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 36 | const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info. |
| 37 | const PreprocessorOptions &PreprocessorOpts; // Only used for debug info. |
Chandler Carruth | bc55fe2 | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 38 | const CodeGenOptions CodeGenOpts; // Intentionally copied in. |
Richard Smith | 7f5755c | 2014-08-01 22:42:16 +0000 | [diff] [blame] | 39 | |
| 40 | unsigned HandlingTopLevelDecls; |
Reid Kleckner | ea53dba | 2016-04-22 18:46:33 +0000 | [diff] [blame] | 41 | |
| 42 | /// Use this when emitting decls to block re-entrant decl emission. It will |
| 43 | /// emit all deferred decls on scope exit. Set EmitDeferred to false if decl |
| 44 | /// emission must be deferred longer, like at the end of a tag definition. |
Richard Smith | 7f5755c | 2014-08-01 22:42:16 +0000 | [diff] [blame] | 45 | struct HandlingTopLevelDeclRAII { |
| 46 | CodeGeneratorImpl &Self; |
Reid Kleckner | ea53dba | 2016-04-22 18:46:33 +0000 | [diff] [blame] | 47 | bool EmitDeferred; |
| 48 | HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self, |
| 49 | bool EmitDeferred = true) |
| 50 | : Self(Self), EmitDeferred(EmitDeferred) { |
Richard Smith | 7f5755c | 2014-08-01 22:42:16 +0000 | [diff] [blame] | 51 | ++Self.HandlingTopLevelDecls; |
| 52 | } |
| 53 | ~HandlingTopLevelDeclRAII() { |
Reid Kleckner | ea53dba | 2016-04-22 18:46:33 +0000 | [diff] [blame] | 54 | unsigned Level = --Self.HandlingTopLevelDecls; |
| 55 | if (Level == 0 && EmitDeferred) |
Richard Smith | 7f5755c | 2014-08-01 22:42:16 +0000 | [diff] [blame] | 56 | Self.EmitDeferredDecls(); |
| 57 | } |
| 58 | }; |
| 59 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 60 | CoverageSourceInfo *CoverageInfo; |
| 61 | |
Chris Lattner | adf1f51 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 62 | protected: |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 63 | std::unique_ptr<llvm::Module> M; |
| 64 | std::unique_ptr<CodeGen::CodeGenModule> Builder; |
| 65 | |
Hans Wennborg | 2b0d014 | 2014-12-18 19:19:00 +0000 | [diff] [blame] | 66 | private: |
Reid Kleckner | 78381c6 | 2018-09-18 23:16:30 +0000 | [diff] [blame] | 67 | SmallVector<FunctionDecl *, 8> DeferredInlineMemberFuncDefs; |
Hans Wennborg | 2b0d014 | 2014-12-18 19:19:00 +0000 | [diff] [blame] | 68 | |
Chris Lattner | adf1f51 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 69 | public: |
John McCall | 6909fee | 2016-05-18 05:21:18 +0000 | [diff] [blame] | 70 | CodeGeneratorImpl(DiagnosticsEngine &diags, llvm::StringRef ModuleName, |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 71 | const HeaderSearchOptions &HSO, |
| 72 | const PreprocessorOptions &PPO, const CodeGenOptions &CGO, |
| 73 | llvm::LLVMContext &C, |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 74 | CoverageSourceInfo *CoverageInfo = nullptr) |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 75 | : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO), |
| 76 | PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0), |
Mehdi Amini | 557c20a | 2016-03-13 21:05:23 +0000 | [diff] [blame] | 77 | CoverageInfo(CoverageInfo), M(new llvm::Module(ModuleName, C)) { |
| 78 | C.setDiscardValueNames(CGO.DiscardValueNames); |
| 79 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 81 | ~CodeGeneratorImpl() override { |
Hans Wennborg | 0c0a8c8 | 2014-12-19 23:35:11 +0000 | [diff] [blame] | 82 | // There should normally not be any leftover inline method definitions. |
Reid Kleckner | 78381c6 | 2018-09-18 23:16:30 +0000 | [diff] [blame] | 83 | assert(DeferredInlineMemberFuncDefs.empty() || |
Hans Wennborg | 0c0a8c8 | 2014-12-19 23:35:11 +0000 | [diff] [blame] | 84 | Diags.hasErrorOccurred()); |
Hans Wennborg | 2b0d014 | 2014-12-18 19:19:00 +0000 | [diff] [blame] | 85 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
John McCall | 6909fee | 2016-05-18 05:21:18 +0000 | [diff] [blame] | 87 | CodeGenModule &CGM() { |
| 88 | return *Builder; |
| 89 | } |
| 90 | |
| 91 | llvm::Module *GetModule() { |
Daniel Dunbar | 30c514e | 2008-10-21 19:55:09 +0000 | [diff] [blame] | 92 | return M.get(); |
| 93 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Amjad Aboud | 546bc11 | 2017-02-09 22:07:24 +0000 | [diff] [blame] | 95 | CGDebugInfo *getCGDebugInfo() { |
| 96 | return Builder->getModuleDebugInfo(); |
| 97 | } |
| 98 | |
John McCall | 6909fee | 2016-05-18 05:21:18 +0000 | [diff] [blame] | 99 | llvm::Module *ReleaseModule() { |
| 100 | return M.release(); |
| 101 | } |
| 102 | |
| 103 | const Decl *GetDeclForMangledName(StringRef MangledName) { |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 104 | GlobalDecl Result; |
| 105 | if (!Builder->lookupRepresentativeDecl(MangledName, Result)) |
| 106 | return nullptr; |
| 107 | const Decl *D = Result.getCanonicalDecl().getDecl(); |
| 108 | if (auto FD = dyn_cast<FunctionDecl>(D)) { |
| 109 | if (FD->hasBody(FD)) |
| 110 | return FD; |
| 111 | } else if (auto TD = dyn_cast<TagDecl>(D)) { |
| 112 | if (auto Def = TD->getDefinition()) |
| 113 | return Def; |
| 114 | } |
| 115 | return D; |
| 116 | } |
| 117 | |
John McCall | 6909fee | 2016-05-18 05:21:18 +0000 | [diff] [blame] | 118 | llvm::Constant *GetAddrOfGlobal(GlobalDecl global, bool isForDefinition) { |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 119 | return Builder->GetAddrOfGlobal(global, ForDefinition_t(isForDefinition)); |
John McCall | 6909fee | 2016-05-18 05:21:18 +0000 | [diff] [blame] | 120 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Vassil Vassilev | 4d54e543 | 2017-08-27 10:58:03 +0000 | [diff] [blame] | 122 | llvm::Module *StartModule(llvm::StringRef ModuleName, |
| 123 | llvm::LLVMContext &C) { |
| 124 | assert(!M && "Replacing existing Module?"); |
| 125 | M.reset(new llvm::Module(ModuleName, C)); |
| 126 | Initialize(*Ctx); |
| 127 | return M.get(); |
| 128 | } |
| 129 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 130 | void Initialize(ASTContext &Context) override { |
Chris Lattner | adf1f51 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 131 | Ctx = &Context; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 133 | M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple()); |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 134 | M->setDataLayout(Ctx->getTargetInfo().getDataLayout()); |
Mehdi Amini | ca3cf9e | 2015-07-24 16:04:29 +0000 | [diff] [blame] | 135 | Builder.reset(new CodeGen::CodeGenModule(Context, HeaderSearchOpts, |
| 136 | PreprocessorOpts, CodeGenOpts, |
| 137 | *M, Diags, CoverageInfo)); |
Hans Wennborg | 75958c4 | 2013-08-08 00:17:41 +0000 | [diff] [blame] | 138 | |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 139 | for (auto &&Lib : CodeGenOpts.DependentLibraries) |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 140 | Builder->AddDependentLib(Lib); |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 141 | for (auto &&Opt : CodeGenOpts.LinkerOptions) |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 142 | Builder->AppendLinkerOptions(Opt); |
Chris Lattner | adf1f51 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 143 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 145 | void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override { |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 146 | if (Diags.hasErrorOccurred()) |
| 147 | return; |
| 148 | |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 149 | Builder->HandleCXXStaticMemberVarInstantiation(VD); |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 152 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 153 | if (Diags.hasErrorOccurred()) |
| 154 | return true; |
| 155 | |
Richard Smith | 7f5755c | 2014-08-01 22:42:16 +0000 | [diff] [blame] | 156 | HandlingTopLevelDeclRAII HandlingDecl(*this); |
| 157 | |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 158 | // Make sure to emit all elements of a Decl. |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 159 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 160 | Builder->EmitTopLevelDecl(*I); |
Hans Wennborg | dfcb7d6 | 2014-06-06 17:36:17 +0000 | [diff] [blame] | 161 | |
Richard Smith | 7f5755c | 2014-08-01 22:42:16 +0000 | [diff] [blame] | 162 | return true; |
| 163 | } |
| 164 | |
| 165 | void EmitDeferredDecls() { |
Reid Kleckner | 78381c6 | 2018-09-18 23:16:30 +0000 | [diff] [blame] | 166 | if (DeferredInlineMemberFuncDefs.empty()) |
Richard Smith | c9cbde7 | 2014-08-13 21:15:09 +0000 | [diff] [blame] | 167 | return; |
| 168 | |
Richard Smith | 1ba0a07 | 2014-08-01 20:39:36 +0000 | [diff] [blame] | 169 | // Emit any deferred inline method definitions. Note that more deferred |
| 170 | // methods may be added during this loop, since ASTConsumer callbacks |
| 171 | // can be invoked if AST inspection results in declarations being added. |
Richard Smith | c9cbde7 | 2014-08-13 21:15:09 +0000 | [diff] [blame] | 172 | HandlingTopLevelDeclRAII HandlingDecl(*this); |
Reid Kleckner | 78381c6 | 2018-09-18 23:16:30 +0000 | [diff] [blame] | 173 | for (unsigned I = 0; I != DeferredInlineMemberFuncDefs.size(); ++I) |
| 174 | Builder->EmitTopLevelDecl(DeferredInlineMemberFuncDefs[I]); |
| 175 | DeferredInlineMemberFuncDefs.clear(); |
Chris Lattner | adf1f51 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 176 | } |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 177 | |
Stephan Bergmann | 17d7d14 | 2016-03-30 06:27:31 +0000 | [diff] [blame] | 178 | void HandleInlineFunctionDefinition(FunctionDecl *D) override { |
Hans Wennborg | a926d84 | 2014-05-23 20:37:38 +0000 | [diff] [blame] | 179 | if (Diags.hasErrorOccurred()) |
| 180 | return; |
| 181 | |
| 182 | assert(D->doesThisDeclarationHaveABody()); |
| 183 | |
Hans Wennborg | dfcb7d6 | 2014-06-06 17:36:17 +0000 | [diff] [blame] | 184 | // We may want to emit this definition. However, that decision might be |
| 185 | // based on computing the linkage, and we have to defer that in case we |
| 186 | // are inside of something that will change the method's final linkage, |
| 187 | // e.g. |
| 188 | // typedef struct { |
| 189 | // void bar(); |
| 190 | // void foo() { bar(); } |
| 191 | // } A; |
Reid Kleckner | 78381c6 | 2018-09-18 23:16:30 +0000 | [diff] [blame] | 192 | DeferredInlineMemberFuncDefs.push_back(D); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 193 | |
Justin Bogner | 94d384e | 2014-11-18 00:34:46 +0000 | [diff] [blame] | 194 | // Provide some coverage mapping even for methods that aren't emitted. |
| 195 | // Don't do this for templated classes though, as they may not be |
| 196 | // instantiable. |
Reid Kleckner | 78381c6 | 2018-09-18 23:16:30 +0000 | [diff] [blame] | 197 | if (!D->getLexicalDeclContext()->isDependentContext()) |
| 198 | Builder->AddDeferredUnusedCoverageMapping(D); |
Hans Wennborg | a926d84 | 2014-05-23 20:37:38 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | a5e4d30 | 2008-02-06 04:51:19 +0000 | [diff] [blame] | 201 | /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 202 | /// to (e.g. struct, union, enum, class) is completed. This allows the |
| 203 | /// client hack on the type, which can occur at any point in the file |
| 204 | /// (because these can be defined in declspecs). |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 205 | void HandleTagDeclDefinition(TagDecl *D) override { |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 206 | if (Diags.hasErrorOccurred()) |
| 207 | return; |
| 208 | |
Reid Kleckner | ea53dba | 2016-04-22 18:46:33 +0000 | [diff] [blame] | 209 | // Don't allow re-entrant calls to CodeGen triggered by PCH |
| 210 | // deserialization to emit deferred decls. |
| 211 | HandlingTopLevelDeclRAII HandlingDecl(*this, /*EmitDeferred=*/false); |
| 212 | |
Chris Lattner | 68be606 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 213 | Builder->UpdateCompletedType(D); |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 214 | |
| 215 | // For MSVC compatibility, treat declarations of static data members with |
| 216 | // inline initializers as definitions. |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 217 | if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()) { |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 218 | for (Decl *Member : D->decls()) { |
| 219 | if (VarDecl *VD = dyn_cast<VarDecl>(Member)) { |
| 220 | if (Ctx->isMSStaticDataMemberInlineDefinition(VD) && |
| 221 | Ctx->DeclMustBeEmitted(VD)) { |
| 222 | Builder->EmitGlobal(VD); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 227 | // For OpenMP emit declare reduction functions, if required. |
| 228 | if (Ctx->getLangOpts().OpenMP) { |
| 229 | for (Decl *Member : D->decls()) { |
| 230 | if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Member)) { |
| 231 | if (Ctx->DeclMustBeEmitted(DRD)) |
| 232 | Builder->EmitGlobal(DRD); |
| 233 | } |
| 234 | } |
| 235 | } |
Chris Lattner | a5e4d30 | 2008-02-06 04:51:19 +0000 | [diff] [blame] | 236 | } |
Ted Kremenek | 7db4f60 | 2008-08-07 19:47:41 +0000 | [diff] [blame] | 237 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 238 | void HandleTagDeclRequiredDefinition(const TagDecl *D) override { |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 239 | if (Diags.hasErrorOccurred()) |
| 240 | return; |
| 241 | |
Reid Kleckner | ea53dba | 2016-04-22 18:46:33 +0000 | [diff] [blame] | 242 | // Don't allow re-entrant calls to CodeGen triggered by PCH |
| 243 | // deserialization to emit deferred decls. |
| 244 | HandlingTopLevelDeclRAII HandlingDecl(*this, /*EmitDeferred=*/false); |
| 245 | |
David Blaikie | 48ad6dc | 2013-07-13 21:08:14 +0000 | [diff] [blame] | 246 | if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo()) |
| 247 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 248 | DI->completeRequiredType(RD); |
David Blaikie | 48ad6dc | 2013-07-13 21:08:14 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 251 | void HandleTranslationUnit(ASTContext &Ctx) override { |
Manman Ren | 581c2b9 | 2016-01-28 23:29:02 +0000 | [diff] [blame] | 252 | // Release the Builder when there is no error. |
| 253 | if (!Diags.hasErrorOccurred() && Builder) |
| 254 | Builder->Release(); |
| 255 | |
| 256 | // If there are errors before or when releasing the Builder, reset |
| 257 | // the module to stop here before invoking the backend. |
Ted Kremenek | 7db4f60 | 2008-08-07 19:47:41 +0000 | [diff] [blame] | 258 | if (Diags.hasErrorOccurred()) { |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 259 | if (Builder) |
| 260 | Builder->clear(); |
Ted Kremenek | 7db4f60 | 2008-08-07 19:47:41 +0000 | [diff] [blame] | 261 | M.reset(); |
| 262 | return; |
| 263 | } |
Daniel Dunbar | e017ecc | 2009-12-19 17:50:07 +0000 | [diff] [blame] | 264 | } |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 265 | |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 266 | void AssignInheritanceModel(CXXRecordDecl *RD) override { |
| 267 | if (Diags.hasErrorOccurred()) |
| 268 | return; |
| 269 | |
| 270 | Builder->RefreshTypeCacheForClass(RD); |
| 271 | } |
| 272 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 273 | void CompleteTentativeDefinition(VarDecl *D) override { |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 274 | if (Diags.hasErrorOccurred()) |
| 275 | return; |
| 276 | |
| 277 | Builder->EmitTentativeDefinition(D); |
| 278 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 279 | |
Nico Weber | b6a5d05 | 2015-01-15 04:07:35 +0000 | [diff] [blame] | 280 | void HandleVTable(CXXRecordDecl *RD) override { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 281 | if (Diags.hasErrorOccurred()) |
| 282 | return; |
| 283 | |
Nico Weber | b6a5d05 | 2015-01-15 04:07:35 +0000 | [diff] [blame] | 284 | Builder->EmitVTable(RD); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 285 | } |
Chris Lattner | adf1f51 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 286 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 287 | } |
Chris Lattner | f97fe38 | 2007-05-24 06:29:05 +0000 | [diff] [blame] | 288 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 289 | void CodeGenerator::anchor() { } |
| 290 | |
John McCall | 6909fee | 2016-05-18 05:21:18 +0000 | [diff] [blame] | 291 | CodeGenModule &CodeGenerator::CGM() { |
| 292 | return static_cast<CodeGeneratorImpl*>(this)->CGM(); |
| 293 | } |
| 294 | |
| 295 | llvm::Module *CodeGenerator::GetModule() { |
| 296 | return static_cast<CodeGeneratorImpl*>(this)->GetModule(); |
| 297 | } |
| 298 | |
| 299 | llvm::Module *CodeGenerator::ReleaseModule() { |
| 300 | return static_cast<CodeGeneratorImpl*>(this)->ReleaseModule(); |
| 301 | } |
| 302 | |
Amjad Aboud | 546bc11 | 2017-02-09 22:07:24 +0000 | [diff] [blame] | 303 | CGDebugInfo *CodeGenerator::getCGDebugInfo() { |
| 304 | return static_cast<CodeGeneratorImpl*>(this)->getCGDebugInfo(); |
| 305 | } |
| 306 | |
John McCall | 6909fee | 2016-05-18 05:21:18 +0000 | [diff] [blame] | 307 | const Decl *CodeGenerator::GetDeclForMangledName(llvm::StringRef name) { |
| 308 | return static_cast<CodeGeneratorImpl*>(this)->GetDeclForMangledName(name); |
| 309 | } |
| 310 | |
| 311 | llvm::Constant *CodeGenerator::GetAddrOfGlobal(GlobalDecl global, |
| 312 | bool isForDefinition) { |
| 313 | return static_cast<CodeGeneratorImpl*>(this) |
| 314 | ->GetAddrOfGlobal(global, isForDefinition); |
| 315 | } |
| 316 | |
Vassil Vassilev | 4d54e543 | 2017-08-27 10:58:03 +0000 | [diff] [blame] | 317 | llvm::Module *CodeGenerator::StartModule(llvm::StringRef ModuleName, |
| 318 | llvm::LLVMContext &C) { |
| 319 | return static_cast<CodeGeneratorImpl*>(this)->StartModule(ModuleName, C); |
| 320 | } |
| 321 | |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 322 | CodeGenerator *clang::CreateLLVMCodeGen( |
John McCall | 6909fee | 2016-05-18 05:21:18 +0000 | [diff] [blame] | 323 | DiagnosticsEngine &Diags, llvm::StringRef ModuleName, |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 324 | const HeaderSearchOptions &HeaderSearchOpts, |
| 325 | const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO, |
| 326 | llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo) { |
| 327 | return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts, |
| 328 | PreprocessorOpts, CGO, C, CoverageInfo); |
Chris Lattner | f97fe38 | 2007-05-24 06:29:05 +0000 | [diff] [blame] | 329 | } |