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