Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenModule.h" |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 15 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 17 | #include "CodeGenTBAA.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 18 | #include "CGCall.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 19 | #include "CGCXXABI.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 20 | #include "CGObjCRuntime.h" |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 21 | #include "CGOpenCLRuntime.h" |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 22 | #include "TargetInfo.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 687cc4a | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 25 | #include "clang/AST/CharUnits.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 27 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | af89689 | 2010-06-21 18:41:26 +0000 | [diff] [blame] | 28 | #include "clang/AST/DeclTemplate.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 29 | #include "clang/AST/Mangle.h" |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 30 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 31 | #include "clang/Basic/Diagnostic.h" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 32 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | e9b7d8a | 2009-04-01 15:50:34 +0000 | [diff] [blame] | 34 | #include "clang/Basic/ConvertUTF.h" |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 35 | #include "llvm/CallingConv.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 36 | #include "llvm/Module.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | #include "llvm/Intrinsics.h" |
Chris Lattner | d5b8902 | 2009-12-28 21:44:41 +0000 | [diff] [blame] | 38 | #include "llvm/LLVMContext.h" |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/Triple.h" |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 40 | #include "llvm/Target/Mangler.h" |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetData.h" |
Gabor Greif | 6ba728d | 2010-04-10 02:56:12 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 78f7ece | 2009-11-07 09:22:46 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | using namespace clang; |
| 45 | using namespace CodeGen; |
| 46 | |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 47 | static const char AnnotationSection[] = "llvm.metadata"; |
| 48 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 49 | static CGCXXABI &createCXXABI(CodeGenModule &CGM) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 50 | switch (CGM.getContext().getTargetInfo().getCXXABI()) { |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 51 | case CXXABI_ARM: return *CreateARMCXXABI(CGM); |
| 52 | case CXXABI_Itanium: return *CreateItaniumCXXABI(CGM); |
| 53 | case CXXABI_Microsoft: return *CreateMicrosoftCXXABI(CGM); |
| 54 | } |
| 55 | |
| 56 | llvm_unreachable("invalid C++ ABI kind"); |
| 57 | return *CreateItaniumCXXABI(CGM); |
| 58 | } |
| 59 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 61 | CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame] | 62 | llvm::Module &M, const llvm::TargetData &TD, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 63 | DiagnosticsEngine &diags) |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 64 | : Context(C), Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M), |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame] | 65 | TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags), |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 66 | ABI(createCXXABI(*this)), |
Daniel Dunbar | 08d4792 | 2011-06-21 18:54:39 +0000 | [diff] [blame] | 67 | Types(C, M, TD, getTargetCodeGenInfo().getABIInfo(), ABI, CGO), |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 68 | TBAA(0), |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 69 | VTables(*this), ObjCRuntime(0), OpenCLRuntime(0), DebugInfo(0), ARCData(0), |
| 70 | RRData(0), CFConstantStringClassRef(0), ConstantStringClassRef(0), |
Douglas Gregor | 45c4ea7 | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 71 | NSConstantStringType(0), |
Daniel Dunbar | 673431a | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 72 | VMContext(M.getContext()), |
| 73 | NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 74 | BlockObjectAssign(0), BlockObjectDispose(0), |
| 75 | BlockDescriptorType(0), GenericBlockLiteralType(0) { |
David Chisnall | 60be607 | 2011-03-22 21:21:24 +0000 | [diff] [blame] | 76 | if (Features.ObjC1) |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 77 | createObjCRuntime(); |
| 78 | if (Features.OpenCL) |
| 79 | createOpenCLRuntime(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 80 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 81 | // Enable TBAA unless it's suppressed. |
| 82 | if (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0) |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 83 | TBAA = new CodeGenTBAA(Context, VMContext, getLangOptions(), |
| 84 | ABI.getMangleContext()); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 85 | |
Nick Lewycky | e8ba8d7 | 2011-04-21 23:44:07 +0000 | [diff] [blame] | 86 | // If debug info or coverage generation is enabled, create the CGDebugInfo |
| 87 | // object. |
| 88 | if (CodeGenOpts.DebugInfo || CodeGenOpts.EmitGcovArcs || |
| 89 | CodeGenOpts.EmitGcovNotes) |
| 90 | DebugInfo = new CGDebugInfo(*this); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 91 | |
| 92 | Block.GlobalUniqueCount = 0; |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 93 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 94 | if (C.getLangOptions().ObjCAutoRefCount) |
| 95 | ARCData = new ARCEntrypoints(); |
| 96 | RRData = new RREntrypoints(); |
| 97 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 98 | // Initialize the type cache. |
| 99 | llvm::LLVMContext &LLVMContext = M.getContext(); |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 100 | VoidTy = llvm::Type::getVoidTy(LLVMContext); |
| 101 | Int8Ty = llvm::Type::getInt8Ty(LLVMContext); |
| 102 | Int32Ty = llvm::Type::getInt32Ty(LLVMContext); |
| 103 | Int64Ty = llvm::Type::getInt64Ty(LLVMContext); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 104 | PointerWidthInBits = C.getTargetInfo().getPointerWidth(0); |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 105 | PointerAlignInBytes = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 106 | C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity(); |
| 107 | IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth()); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 108 | IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits); |
| 109 | Int8PtrTy = Int8Ty->getPointerTo(0); |
| 110 | Int8PtrPtrTy = Int8PtrTy->getPointerTo(0); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | CodeGenModule::~CodeGenModule() { |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 114 | delete ObjCRuntime; |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 115 | delete OpenCLRuntime; |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 116 | delete &ABI; |
Dan Gohman | 4376c85 | 2010-10-15 18:04:46 +0000 | [diff] [blame] | 117 | delete TBAA; |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 118 | delete DebugInfo; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 119 | delete ARCData; |
| 120 | delete RRData; |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 121 | } |
| 122 | |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 123 | void CodeGenModule::createObjCRuntime() { |
| 124 | if (!Features.NeXTRuntime) |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 125 | ObjCRuntime = CreateGNUObjCRuntime(*this); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 126 | else |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 127 | ObjCRuntime = CreateMacObjCRuntime(*this); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 130 | void CodeGenModule::createOpenCLRuntime() { |
| 131 | OpenCLRuntime = new CGOpenCLRuntime(*this); |
| 132 | } |
| 133 | |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 134 | void CodeGenModule::Release() { |
Chris Lattner | 82227ff | 2009-03-22 21:21:57 +0000 | [diff] [blame] | 135 | EmitDeferred(); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 136 | EmitCXXGlobalInitFunc(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 137 | EmitCXXGlobalDtorFunc(); |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 138 | if (ObjCRuntime) |
| 139 | if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction()) |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 140 | AddGlobalCtor(ObjCInitFunction); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 141 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 142 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 143 | EmitGlobalAnnotations(); |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 144 | EmitLLVMUsed(); |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 145 | |
John McCall | b259383 | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 146 | SimplifyPersonality(); |
| 147 | |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 148 | if (getCodeGenOpts().EmitDeclMetadata) |
| 149 | EmitDeclMetadata(); |
Nick Lewycky | 5ea4f44 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 150 | |
| 151 | if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes) |
Nick Lewycky | 3dc0541 | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 152 | EmitCoverageFile(); |
Devang Patel | f391dbe | 2011-08-16 20:58:22 +0000 | [diff] [blame] | 153 | |
| 154 | if (DebugInfo) |
| 155 | DebugInfo->finalize(); |
Daniel Dunbar | f1968f2 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Devang Patel | e80d567 | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 158 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 159 | // Make sure that this type is translated. |
| 160 | Types.UpdateCompletedType(TD); |
| 161 | if (DebugInfo) |
| 162 | DebugInfo->UpdateCompletedType(TD); |
| 163 | } |
| 164 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 165 | llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) { |
| 166 | if (!TBAA) |
| 167 | return 0; |
| 168 | return TBAA->getTBAAInfo(QTy); |
| 169 | } |
| 170 | |
| 171 | void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst, |
| 172 | llvm::MDNode *TBAAInfo) { |
| 173 | Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo); |
| 174 | } |
| 175 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 176 | bool CodeGenModule::isTargetDarwin() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 177 | return getContext().getTargetInfo().getTriple().isOSDarwin(); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 180 | void CodeGenModule::Error(SourceLocation loc, StringRef error) { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 181 | unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, error); |
John McCall | 3209669 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 182 | getDiags().Report(Context.getFullLoc(loc), diagID); |
| 183 | } |
| 184 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 185 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 186 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 187 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, |
| 188 | bool OmitOnError) { |
| 189 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 190 | return; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 191 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Daniel Dunbar | 56b8001 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 192 | "cannot compile this %0 yet"); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 193 | std::string Msg = Type; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 194 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 195 | << Msg << S->getSourceRange(); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 197 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 198 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 199 | /// specified decl yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 200 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, |
| 201 | bool OmitOnError) { |
| 202 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 203 | return; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 204 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Daniel Dunbar | 56b8001 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 205 | "cannot compile this %0 yet"); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 206 | std::string Msg = Type; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 207 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 208 | } |
| 209 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 210 | llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) { |
| 211 | return llvm::ConstantInt::get(SizeTy, size.getQuantity()); |
| 212 | } |
| 213 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, |
Anders Carlsson | 0ffeaad | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 215 | const NamedDecl *D) const { |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 216 | // Internal definitions always have default visibility. |
Chris Lattner | df102fc | 2009-04-14 05:27:13 +0000 | [diff] [blame] | 217 | if (GV->hasLocalLinkage()) { |
Daniel Dunbar | 7e714cd | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 218 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 219 | return; |
Daniel Dunbar | 7e714cd | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 220 | } |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 221 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 222 | // Set visibility for definitions. |
| 223 | NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility(); |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 224 | if (LV.visibilityExplicit() || !GV->hasAvailableExternallyLinkage()) |
| 225 | GV->setVisibility(GetLLVMVisibility(LV.visibility())); |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 226 | } |
| 227 | |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 228 | /// Set the symbol visibility of type information (vtable and RTTI) |
| 229 | /// associated with the given type. |
| 230 | void CodeGenModule::setTypeVisibility(llvm::GlobalValue *GV, |
| 231 | const CXXRecordDecl *RD, |
Anders Carlsson | fa2e99f | 2011-01-29 20:24:48 +0000 | [diff] [blame] | 232 | TypeVisibilityKind TVK) const { |
Anders Carlsson | 0ffeaad | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 233 | setGlobalVisibility(GV, RD); |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 234 | |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 235 | if (!CodeGenOpts.HiddenWeakVTables) |
| 236 | return; |
| 237 | |
Anders Carlsson | 9a86a13 | 2011-01-29 20:36:11 +0000 | [diff] [blame] | 238 | // We never want to drop the visibility for RTTI names. |
| 239 | if (TVK == TVK_ForRTTIName) |
| 240 | return; |
| 241 | |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 242 | // We want to drop the visibility to hidden for weak type symbols. |
| 243 | // This isn't possible if there might be unresolved references |
| 244 | // elsewhere that rely on this symbol being visible. |
| 245 | |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 246 | // This should be kept roughly in sync with setThunkVisibility |
| 247 | // in CGVTables.cpp. |
| 248 | |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 249 | // Preconditions. |
Anders Carlsson | f502d93 | 2011-01-24 00:46:19 +0000 | [diff] [blame] | 250 | if (GV->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage || |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 251 | GV->getVisibility() != llvm::GlobalVariable::DefaultVisibility) |
| 252 | return; |
| 253 | |
| 254 | // Don't override an explicit visibility attribute. |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 255 | if (RD->getExplicitVisibility()) |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 256 | return; |
| 257 | |
| 258 | switch (RD->getTemplateSpecializationKind()) { |
| 259 | // We have to disable the optimization if this is an EI definition |
| 260 | // because there might be EI declarations in other shared objects. |
| 261 | case TSK_ExplicitInstantiationDefinition: |
| 262 | case TSK_ExplicitInstantiationDeclaration: |
| 263 | return; |
| 264 | |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 265 | // Every use of a non-template class's type information has to emit it. |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 266 | case TSK_Undeclared: |
| 267 | break; |
| 268 | |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 269 | // In theory, implicit instantiations can ignore the possibility of |
| 270 | // an explicit instantiation declaration because there necessarily |
| 271 | // must be an EI definition somewhere with default visibility. In |
| 272 | // practice, it's possible to have an explicit instantiation for |
| 273 | // an arbitrary template class, and linkers aren't necessarily able |
| 274 | // to deal with mixed-visibility symbols. |
| 275 | case TSK_ExplicitSpecialization: |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 276 | case TSK_ImplicitInstantiation: |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 277 | if (!CodeGenOpts.HiddenWeakTemplateVTables) |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 278 | return; |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | |
| 282 | // If there's a key function, there may be translation units |
| 283 | // that don't have the key function's definition. But ignore |
| 284 | // this if we're emitting RTTI under -fno-rtti. |
Anders Carlsson | fa2e99f | 2011-01-29 20:24:48 +0000 | [diff] [blame] | 285 | if (!(TVK != TVK_ForRTTI) || Features.RTTI) { |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 286 | if (Context.getKeyFunction(RD)) |
| 287 | return; |
Anders Carlsson | fa2e99f | 2011-01-29 20:24:48 +0000 | [diff] [blame] | 288 | } |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 289 | |
| 290 | // Otherwise, drop the visibility to hidden. |
| 291 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Rafael Espindola | b1c65ff | 2011-01-11 21:44:37 +0000 | [diff] [blame] | 292 | GV->setUnnamedAddr(true); |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 295 | StringRef CodeGenModule::getMangledName(GlobalDecl GD) { |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 296 | const NamedDecl *ND = cast<NamedDecl>(GD.getDecl()); |
| 297 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 298 | StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()]; |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 299 | if (!Str.empty()) |
| 300 | return Str; |
| 301 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 302 | if (!getCXXABI().getMangleContext().shouldMangleDeclName(ND)) { |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 303 | IdentifierInfo *II = ND->getIdentifier(); |
| 304 | assert(II && "Attempt to mangle unnamed decl."); |
| 305 | |
| 306 | Str = II->getName(); |
| 307 | return Str; |
| 308 | } |
| 309 | |
| 310 | llvm::SmallString<256> Buffer; |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 311 | llvm::raw_svector_ostream Out(Buffer); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 312 | if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 313 | getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Out); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 314 | else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 315 | getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Out); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 316 | else if (const BlockDecl *BD = dyn_cast<BlockDecl>(ND)) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 317 | getCXXABI().getMangleContext().mangleBlock(BD, Out); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 318 | else |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 319 | getCXXABI().getMangleContext().mangleName(ND, Out); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 320 | |
| 321 | // Allocate space for the mangled name. |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 322 | Out.flush(); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 323 | size_t Length = Buffer.size(); |
| 324 | char *Name = MangledNamesAllocator.Allocate<char>(Length); |
| 325 | std::copy(Buffer.begin(), Buffer.end(), Name); |
| 326 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 327 | Str = StringRef(Name, Length); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 328 | |
| 329 | return Str; |
| 330 | } |
| 331 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 332 | void CodeGenModule::getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer, |
| 333 | const BlockDecl *BD) { |
| 334 | MangleContext &MangleCtx = getCXXABI().getMangleContext(); |
| 335 | const Decl *D = GD.getDecl(); |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 336 | llvm::raw_svector_ostream Out(Buffer.getBuffer()); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 337 | if (D == 0) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 338 | MangleCtx.mangleGlobalBlock(BD, Out); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 339 | else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 340 | MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 341 | else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 342 | MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 343 | else |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 344 | MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out); |
Anders Carlsson | 9a8822b | 2010-06-09 02:36:32 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 347 | llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) { |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 348 | return getModule().getNamedValue(Name); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 351 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 352 | /// main() runs. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 353 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 354 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 355 | GlobalCtors.push_back(std::make_pair(Ctor, Priority)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 358 | /// AddGlobalDtor - Add a function to the list that will be called |
| 359 | /// when the module is unloaded. |
| 360 | void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 361 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 362 | GlobalDtors.push_back(std::make_pair(Dtor, Priority)); |
| 363 | } |
| 364 | |
| 365 | void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { |
| 366 | // Ctor function type is void()*. |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 367 | llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 368 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 369 | |
| 370 | // Get the type of a ctor entry, { i32, void ()* }. |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 371 | llvm::StructType *CtorStructTy = |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 372 | llvm::StructType::get(llvm::Type::getInt32Ty(VMContext), |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 373 | llvm::PointerType::getUnqual(CtorFTy), NULL); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 374 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 375 | // Construct the constructor and destructor arrays. |
| 376 | std::vector<llvm::Constant*> Ctors; |
| 377 | for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| 378 | std::vector<llvm::Constant*> S; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 380 | I->second, false)); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 381 | S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 382 | Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 383 | } |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 384 | |
| 385 | if (!Ctors.empty()) { |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 386 | llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 387 | new llvm::GlobalVariable(TheModule, AT, false, |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 388 | llvm::GlobalValue::AppendingLinkage, |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 389 | llvm::ConstantArray::get(AT, Ctors), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 390 | GlobalName); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 391 | } |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 392 | } |
| 393 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 394 | llvm::GlobalValue::LinkageTypes |
| 395 | CodeGenModule::getFunctionLinkage(const FunctionDecl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 396 | GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 397 | |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 398 | if (Linkage == GVA_Internal) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 399 | return llvm::Function::InternalLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 400 | |
| 401 | if (D->hasAttr<DLLExportAttr>()) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 402 | return llvm::Function::DLLExportLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 403 | |
| 404 | if (D->hasAttr<WeakAttr>()) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 405 | return llvm::Function::WeakAnyLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 406 | |
| 407 | // In C99 mode, 'inline' functions are guaranteed to have a strong |
| 408 | // definition somewhere else, so we can use available_externally linkage. |
| 409 | if (Linkage == GVA_C99Inline) |
Fariborz Jahanian | fd0f89d | 2011-02-04 00:08:13 +0000 | [diff] [blame] | 410 | return llvm::Function::AvailableExternallyLinkage; |
John McCall | 5584d91 | 2011-09-19 18:05:26 +0000 | [diff] [blame] | 411 | |
| 412 | // Note that Apple's kernel linker doesn't support symbol |
| 413 | // coalescing, so we need to avoid linkonce and weak linkages there. |
| 414 | // Normally, this means we just map to internal, but for explicit |
| 415 | // instantiations we'll map to external. |
| 416 | |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 417 | // In C++, the compiler has to emit a definition in every translation unit |
| 418 | // that references the function. We should use linkonce_odr because |
| 419 | // a) if all references in this translation unit are optimized away, we |
| 420 | // don't need to codegen it. b) if the function persists, it needs to be |
| 421 | // merged with other definitions. c) C++ has the ODR, so we know the |
| 422 | // definition is dependable. |
| 423 | if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 424 | return !Context.getLangOptions().AppleKext |
| 425 | ? llvm::Function::LinkOnceODRLinkage |
| 426 | : llvm::Function::InternalLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 427 | |
| 428 | // An explicit instantiation of a template has weak linkage, since |
| 429 | // explicit instantiations can occur in multiple translation units |
| 430 | // and must all be equivalent. However, we are not allowed to |
| 431 | // throw away these explicit instantiations. |
| 432 | if (Linkage == GVA_ExplicitTemplateInstantiation) |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 433 | return !Context.getLangOptions().AppleKext |
| 434 | ? llvm::Function::WeakODRLinkage |
John McCall | 5584d91 | 2011-09-19 18:05:26 +0000 | [diff] [blame] | 435 | : llvm::Function::ExternalLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 436 | |
| 437 | // Otherwise, we have strong external linkage. |
| 438 | assert(Linkage == GVA_StrongExternal); |
| 439 | return llvm::Function::ExternalLinkage; |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 440 | } |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 441 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 442 | |
| 443 | /// SetFunctionDefinitionAttributes - Set attributes for a global. |
| 444 | /// |
| 445 | /// FIXME: This is currently only done for aliases and functions, but not for |
| 446 | /// variables (these details are set in EmitGlobalVarDefinition for variables). |
| 447 | void CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D, |
| 448 | llvm::GlobalValue *GV) { |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 449 | SetCommonAttributes(D, GV); |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Daniel Dunbar | 7dbd819 | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 452 | void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | const CGFunctionInfo &Info, |
Daniel Dunbar | 7dbd819 | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 454 | llvm::Function *F) { |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 455 | unsigned CallingConv; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 456 | AttributeListType AttributeList; |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 457 | ConstructAttributeList(Info, D, AttributeList, CallingConv); |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 458 | F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 459 | AttributeList.size())); |
| 460 | F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 461 | } |
| 462 | |
John McCall | d1e40d5 | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 463 | /// Determines whether the language options require us to model |
| 464 | /// unwind exceptions. We treat -fexceptions as mandating this |
| 465 | /// except under the fragile ObjC ABI with only ObjC exceptions |
| 466 | /// enabled. This means, for example, that C with -fexceptions |
| 467 | /// enables this. |
| 468 | static bool hasUnwindExceptions(const LangOptions &Features) { |
| 469 | // If exceptions are completely disabled, obviously this is false. |
| 470 | if (!Features.Exceptions) return false; |
| 471 | |
| 472 | // If C++ exceptions are enabled, this is true. |
| 473 | if (Features.CXXExceptions) return true; |
| 474 | |
| 475 | // If ObjC exceptions are enabled, this depends on the ABI. |
| 476 | if (Features.ObjCExceptions) { |
| 477 | if (!Features.ObjCNonFragileABI) return false; |
| 478 | } |
| 479 | |
| 480 | return true; |
| 481 | } |
| 482 | |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 483 | void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, |
| 484 | llvm::Function *F) { |
Rafael Espindola | abca5a1 | 2011-05-25 03:44:55 +0000 | [diff] [blame] | 485 | if (CodeGenOpts.UnwindTables) |
| 486 | F->setHasUWTable(); |
| 487 | |
John McCall | d1e40d5 | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 488 | if (!hasUnwindExceptions(Features)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | F->addFnAttr(llvm::Attribute::NoUnwind); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 490 | |
Eli Friedman | 2873aee | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 491 | if (D->hasAttr<NakedAttr>()) { |
| 492 | // Naked implies noinline: we should not be inlining such functions. |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 493 | F->addFnAttr(llvm::Attribute::Naked); |
Eli Friedman | 2873aee | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 494 | F->addFnAttr(llvm::Attribute::NoInline); |
| 495 | } |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 496 | |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 497 | if (D->hasAttr<NoInlineAttr>()) |
Anders Carlsson | 81ebbde | 2009-02-19 19:22:11 +0000 | [diff] [blame] | 498 | F->addFnAttr(llvm::Attribute::NoInline); |
Mike Stump | f55314d | 2009-10-05 21:58:44 +0000 | [diff] [blame] | 499 | |
Eli Friedman | 2873aee | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 500 | // (noinline wins over always_inline, and we can't specify both in IR) |
| 501 | if (D->hasAttr<AlwaysInlineAttr>() && |
| 502 | !F->hasFnAttr(llvm::Attribute::NoInline)) |
| 503 | F->addFnAttr(llvm::Attribute::AlwaysInline); |
| 504 | |
Rafael Espindola | c5f657f | 2011-01-11 00:26:26 +0000 | [diff] [blame] | 505 | if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D)) |
| 506 | F->setUnnamedAddr(true); |
| 507 | |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 508 | if (Features.getStackProtector() == LangOptions::SSPOn) |
Anders Carlsson | fd01535 | 2009-11-16 16:56:03 +0000 | [diff] [blame] | 509 | F->addFnAttr(llvm::Attribute::StackProtect); |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 510 | else if (Features.getStackProtector() == LangOptions::SSPReq) |
Anders Carlsson | fd01535 | 2009-11-16 16:56:03 +0000 | [diff] [blame] | 511 | F->addFnAttr(llvm::Attribute::StackProtectReq); |
| 512 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 513 | unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); |
| 514 | if (alignment) |
| 515 | F->setAlignment(alignment); |
| 516 | |
Mike Stump | fb51ddf | 2009-10-05 22:49:20 +0000 | [diff] [blame] | 517 | // C++ ABI requires 2-byte alignment for member functions. |
Mike Stump | bd6dbd1 | 2009-10-05 23:08:21 +0000 | [diff] [blame] | 518 | if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) |
| 519 | F->setAlignment(2); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | void CodeGenModule::SetCommonAttributes(const Decl *D, |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 523 | llvm::GlobalValue *GV) { |
Anders Carlsson | 934176f | 2011-01-29 19:41:00 +0000 | [diff] [blame] | 524 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 525 | setGlobalVisibility(GV, ND); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 526 | else |
| 527 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 528 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 529 | if (D->hasAttr<UsedAttr>()) |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 530 | AddUsedGlobal(GV); |
| 531 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 532 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 533 | GV->setSection(SA->getName()); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 534 | |
| 535 | getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this); |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 538 | void CodeGenModule::SetInternalFunctionAttributes(const Decl *D, |
| 539 | llvm::Function *F, |
| 540 | const CGFunctionInfo &FI) { |
| 541 | SetLLVMFunctionAttributes(D, FI, F); |
| 542 | SetLLVMFunctionAttributesForDefinition(D, F); |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 543 | |
| 544 | F->setLinkage(llvm::Function::InternalLinkage); |
| 545 | |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 546 | SetCommonAttributes(D, F); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 549 | void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, |
Eli Friedman | c6c14d1 | 2009-05-26 01:22:57 +0000 | [diff] [blame] | 550 | llvm::Function *F, |
| 551 | bool IsIncompleteFunction) { |
Peter Collingbourne | 0ac2cf4 | 2011-04-06 12:29:04 +0000 | [diff] [blame] | 552 | if (unsigned IID = F->getIntrinsicID()) { |
| 553 | // If this is an intrinsic function, set the function's attributes |
| 554 | // to the intrinsic's attributes. |
| 555 | F->setAttributes(llvm::Intrinsic::getAttributes((llvm::Intrinsic::ID)IID)); |
| 556 | return; |
| 557 | } |
| 558 | |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 559 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 560 | |
Eli Friedman | c6c14d1 | 2009-05-26 01:22:57 +0000 | [diff] [blame] | 561 | if (!IsIncompleteFunction) |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 562 | SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 564 | // Only a few attributes are set on declarations; these may later be |
| 565 | // overridden by a definition. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 567 | if (FD->hasAttr<DLLImportAttr>()) { |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 568 | F->setLinkage(llvm::Function::DLLImportLinkage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | } else if (FD->hasAttr<WeakAttr>() || |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 570 | FD->isWeakImported()) { |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 571 | // "extern_weak" is overloaded in LLVM; we probably should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | // separate linkage types for this. |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 573 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
| 574 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | F->setLinkage(llvm::Function::ExternalLinkage); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 576 | |
| 577 | NamedDecl::LinkageInfo LV = FD->getLinkageAndVisibility(); |
| 578 | if (LV.linkage() == ExternalLinkage && LV.visibilityExplicit()) { |
| 579 | F->setVisibility(GetLLVMVisibility(LV.visibility())); |
| 580 | } |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 583 | if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 584 | F->setSection(SA->getName()); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 587 | void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | assert(!GV->isDeclaration() && |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 589 | "Only globals with definition can force usage."); |
Chris Lattner | 35f38a2 | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 590 | LLVMUsed.push_back(GV); |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | void CodeGenModule::EmitLLVMUsed() { |
| 594 | // Don't create llvm.used if there is no need. |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 595 | if (LLVMUsed.empty()) |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 596 | return; |
| 597 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 598 | llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 35f38a2 | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 600 | // Convert LLVMUsed to what ConstantArray needs. |
| 601 | std::vector<llvm::Constant*> UsedArray; |
| 602 | UsedArray.resize(LLVMUsed.size()); |
| 603 | for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | UsedArray[i] = |
| 605 | llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 606 | i8PTy); |
Chris Lattner | 35f38a2 | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | |
Fariborz Jahanian | c38e9af | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 609 | if (UsedArray.empty()) |
| 610 | return; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 611 | llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
| 613 | llvm::GlobalVariable *GV = |
| 614 | new llvm::GlobalVariable(getModule(), ATy, false, |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 615 | llvm::GlobalValue::AppendingLinkage, |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 616 | llvm::ConstantArray::get(ATy, UsedArray), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 617 | "llvm.used"); |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 618 | |
| 619 | GV->setSection("llvm.metadata"); |
| 620 | } |
| 621 | |
| 622 | void CodeGenModule::EmitDeferred() { |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 623 | // Emit code for any potentially referenced deferred decls. Since a |
| 624 | // previously unused static decl may become used during the generation of code |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 625 | // for a static function, iterate until no changes are made. |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 626 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 627 | while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) { |
| 628 | if (!DeferredVTables.empty()) { |
| 629 | const CXXRecordDecl *RD = DeferredVTables.back(); |
| 630 | DeferredVTables.pop_back(); |
| 631 | getVTables().GenerateClassData(getVTableLinkage(RD), RD); |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 632 | continue; |
| 633 | } |
| 634 | |
Anders Carlsson | 2a131fb | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 635 | GlobalDecl D = DeferredDeclsToEmit.back(); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 636 | DeferredDeclsToEmit.pop_back(); |
| 637 | |
John McCall | c76702c | 2010-05-27 01:45:30 +0000 | [diff] [blame] | 638 | // Check to see if we've already emitted this. This is necessary |
| 639 | // for a couple of reasons: first, decls can end up in the |
| 640 | // deferred-decls queue multiple times, and second, decls can end |
| 641 | // up with definitions in unusual ways (e.g. by an extern inline |
| 642 | // function acquiring a strong function redefinition). Just |
| 643 | // ignore these cases. |
| 644 | // |
| 645 | // TODO: That said, looking this up multiple times is very wasteful. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 646 | StringRef Name = getMangledName(D); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 647 | llvm::GlobalValue *CGRef = GetGlobalValue(Name); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 648 | assert(CGRef && "Deferred decl wasn't referenced?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 650 | if (!CGRef->isDeclaration()) |
| 651 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | |
John McCall | c76702c | 2010-05-27 01:45:30 +0000 | [diff] [blame] | 653 | // GlobalAlias::isDeclaration() defers to the aliasee, but for our |
| 654 | // purposes an alias counts as a definition. |
| 655 | if (isa<llvm::GlobalAlias>(CGRef)) |
| 656 | continue; |
| 657 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 658 | // Otherwise, emit the definition and move on to the next one. |
| 659 | EmitGlobalDefinition(D); |
| 660 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 663 | void CodeGenModule::EmitGlobalAnnotations() { |
| 664 | if (Annotations.empty()) |
| 665 | return; |
| 666 | |
| 667 | // Create a new global variable for the ConstantStruct in the Module. |
| 668 | llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get( |
| 669 | Annotations[0]->getType(), Annotations.size()), Annotations); |
| 670 | llvm::GlobalValue *gv = new llvm::GlobalVariable(getModule(), |
| 671 | Array->getType(), false, llvm::GlobalValue::AppendingLinkage, Array, |
| 672 | "llvm.global.annotations"); |
| 673 | gv->setSection(AnnotationSection); |
| 674 | } |
| 675 | |
| 676 | llvm::Constant *CodeGenModule::EmitAnnotationString(llvm::StringRef Str) { |
| 677 | llvm::StringMap<llvm::Constant*>::iterator i = AnnotationStrings.find(Str); |
| 678 | if (i != AnnotationStrings.end()) |
| 679 | return i->second; |
| 680 | |
| 681 | // Not found yet, create a new global. |
| 682 | llvm::Constant *s = llvm::ConstantArray::get(getLLVMContext(), Str, true); |
| 683 | llvm::GlobalValue *gv = new llvm::GlobalVariable(getModule(), s->getType(), |
| 684 | true, llvm::GlobalValue::PrivateLinkage, s, ".str"); |
| 685 | gv->setSection(AnnotationSection); |
| 686 | gv->setUnnamedAddr(true); |
| 687 | AnnotationStrings[Str] = gv; |
| 688 | return gv; |
| 689 | } |
| 690 | |
| 691 | llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) { |
| 692 | SourceManager &SM = getContext().getSourceManager(); |
| 693 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 694 | if (PLoc.isValid()) |
| 695 | return EmitAnnotationString(PLoc.getFilename()); |
| 696 | return EmitAnnotationString(SM.getBufferName(Loc)); |
| 697 | } |
| 698 | |
| 699 | llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) { |
| 700 | SourceManager &SM = getContext().getSourceManager(); |
| 701 | PresumedLoc PLoc = SM.getPresumedLoc(L); |
| 702 | unsigned LineNo = PLoc.isValid() ? PLoc.getLine() : |
| 703 | SM.getExpansionLineNumber(L); |
| 704 | return llvm::ConstantInt::get(Int32Ty, LineNo); |
| 705 | } |
| 706 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 707 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 708 | const AnnotateAttr *AA, |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 709 | SourceLocation L) { |
| 710 | // Get the globals for file name, annotation, and the line number. |
| 711 | llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()), |
| 712 | *UnitGV = EmitAnnotationUnit(L), |
| 713 | *LineNoCst = EmitAnnotationLineNo(L); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 714 | |
Daniel Dunbar | 57d5cee | 2009-04-14 22:41:13 +0000 | [diff] [blame] | 715 | // Create the ConstantStruct for the global annotation. |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 716 | llvm::Constant *Fields[4] = { |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 717 | llvm::ConstantExpr::getBitCast(GV, Int8PtrTy), |
| 718 | llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy), |
| 719 | llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy), |
| 720 | LineNoCst |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 721 | }; |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 722 | return llvm::ConstantStruct::getAnon(Fields); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 725 | void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D, |
| 726 | llvm::GlobalValue *GV) { |
| 727 | assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); |
| 728 | // Get the struct elements for these annotations. |
| 729 | for (specific_attr_iterator<AnnotateAttr> |
| 730 | ai = D->specific_attr_begin<AnnotateAttr>(), |
| 731 | ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai) |
| 732 | Annotations.push_back(EmitAnnotateAttr(GV, *ai, D->getLocation())); |
| 733 | } |
| 734 | |
Argyrios Kyrtzidis | a6d6af3 | 2010-07-27 22:37:14 +0000 | [diff] [blame] | 735 | bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 736 | // Never defer when EmitAllDecls is specified. |
| 737 | if (Features.EmitAllDecls) |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 738 | return false; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 739 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 740 | return !getContext().DeclMustBeEmitted(Global); |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 743 | llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { |
| 744 | const AliasAttr *AA = VD->getAttr<AliasAttr>(); |
| 745 | assert(AA && "No alias?"); |
| 746 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 747 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 748 | |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 749 | // See if there is already something with the target's name in the module. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 750 | llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 751 | |
| 752 | llvm::Constant *Aliasee; |
| 753 | if (isa<llvm::FunctionType>(DeclTy)) |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 754 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl(), |
| 755 | /*ForVTable=*/false); |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 756 | else |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 757 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 758 | llvm::PointerType::getUnqual(DeclTy), 0); |
| 759 | if (!Entry) { |
| 760 | llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee); |
| 761 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
| 762 | WeakRefReferences.insert(F); |
| 763 | } |
| 764 | |
| 765 | return Aliasee; |
| 766 | } |
| 767 | |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 768 | void CodeGenModule::EmitGlobal(GlobalDecl GD) { |
Anders Carlsson | 4a6835e | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 769 | const ValueDecl *Global = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 771 | // Weak references don't produce any output by themselves. |
| 772 | if (Global->hasAttr<WeakRefAttr>()) |
| 773 | return; |
| 774 | |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 775 | // If this is an alias definition (which otherwise looks like a declaration) |
| 776 | // emit it now. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 777 | if (Global->hasAttr<AliasAttr>()) |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 778 | return EmitAliasDefinition(GD); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 779 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 780 | // Ignore declarations, they will be emitted on their first use. |
Daniel Dunbar | 5e1e1f9 | 2009-03-19 08:27:24 +0000 | [diff] [blame] | 781 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 782 | // Forward declarations are emitted lazily on first use. |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 783 | if (!FD->doesThisDeclarationHaveABody()) { |
| 784 | if (!FD->doesDeclarationForceExternallyVisibleDefinition()) |
| 785 | return; |
| 786 | |
| 787 | const FunctionDecl *InlineDefinition = 0; |
| 788 | FD->getBody(InlineDefinition); |
| 789 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 790 | StringRef MangledName = getMangledName(GD); |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 791 | llvm::StringMap<GlobalDecl>::iterator DDI = |
| 792 | DeferredDecls.find(MangledName); |
| 793 | if (DDI != DeferredDecls.end()) |
| 794 | DeferredDecls.erase(DDI); |
| 795 | EmitGlobalDefinition(InlineDefinition); |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 796 | return; |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 797 | } |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 798 | } else { |
| 799 | const VarDecl *VD = cast<VarDecl>(Global); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 800 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 801 | |
Douglas Gregor | a9a55c0 | 2010-02-06 05:15:45 +0000 | [diff] [blame] | 802 | if (VD->isThisDeclarationADefinition() != VarDecl::Definition) |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 803 | return; |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 806 | // Defer code generation when possible if this is a static definition, inline |
| 807 | // function etc. These we only want to emit if they are used. |
Chris Lattner | 4357a82 | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 808 | if (!MayDeferGeneration(Global)) { |
| 809 | // Emit the definition if it can't be deferred. |
| 810 | EmitGlobalDefinition(GD); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 811 | return; |
| 812 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 813 | |
| 814 | // If we're deferring emission of a C++ variable with an |
| 815 | // initializer, remember the order in which it appeared in the file. |
| 816 | if (getLangOptions().CPlusPlus && isa<VarDecl>(Global) && |
| 817 | cast<VarDecl>(Global)->hasInit()) { |
| 818 | DelayedCXXInitPosition[Global] = CXXGlobalInits.size(); |
| 819 | CXXGlobalInits.push_back(0); |
| 820 | } |
Chris Lattner | 4357a82 | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 821 | |
| 822 | // If the value has already been used, add it directly to the |
| 823 | // DeferredDeclsToEmit list. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 824 | StringRef MangledName = getMangledName(GD); |
Chris Lattner | 4357a82 | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 825 | if (GetGlobalValue(MangledName)) |
| 826 | DeferredDeclsToEmit.push_back(GD); |
| 827 | else { |
| 828 | // Otherwise, remember that we saw a deferred decl with this name. The |
| 829 | // first use of the mangled name will cause it to move into |
| 830 | // DeferredDeclsToEmit. |
| 831 | DeferredDecls[MangledName] = GD; |
| 832 | } |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 835 | void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) { |
Anders Carlsson | 4a6835e | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 836 | const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 838 | PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), |
Anders Carlsson | 8e2efcc | 2009-10-27 14:32:27 +0000 | [diff] [blame] | 839 | Context.getSourceManager(), |
| 840 | "Generating code for declaration"); |
| 841 | |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 842 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 843 | // At -O0, don't generate IR for functions with available_externally |
| 844 | // linkage. |
Douglas Gregor | 7feaeee | 2010-07-15 22:58:18 +0000 | [diff] [blame] | 845 | if (CodeGenOpts.OptimizationLevel == 0 && |
| 846 | !Function->hasAttr<AlwaysInlineAttr>() && |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 847 | getFunctionLinkage(Function) |
| 848 | == llvm::Function::AvailableExternallyLinkage) |
| 849 | return; |
Anders Carlsson | 7270ee4 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 850 | |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 851 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 852 | // Make sure to emit the definition(s) before we emit the thunks. |
| 853 | // This is necessary for the generation of certain thunks. |
| 854 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Method)) |
| 855 | EmitCXXConstructor(CD, GD.getCtorType()); |
| 856 | else if (const CXXDestructorDecl *DD =dyn_cast<CXXDestructorDecl>(Method)) |
| 857 | EmitCXXDestructor(DD, GD.getDtorType()); |
| 858 | else |
| 859 | EmitGlobalFunctionDefinition(GD); |
| 860 | |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 861 | if (Method->isVirtual()) |
| 862 | getVTables().EmitThunks(GD); |
| 863 | |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 864 | return; |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 865 | } |
Chris Lattner | b5e8156 | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 866 | |
Chris Lattner | b5e8156 | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 867 | return EmitGlobalFunctionDefinition(GD); |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 868 | } |
Chris Lattner | b5e8156 | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 869 | |
| 870 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 871 | return EmitGlobalVarDefinition(VD); |
Chris Lattner | 4357a82 | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 872 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 873 | llvm_unreachable("Invalid argument to EmitGlobalDefinition()"); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 876 | /// GetOrCreateLLVMFunction - If the specified mangled name is not in the |
| 877 | /// module, create and return an llvm Function with the specified type. If there |
| 878 | /// is something in the module with the specified name, return it potentially |
| 879 | /// bitcasted to the right type. |
| 880 | /// |
| 881 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 882 | /// to set the attributes on the function when it is first created. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 883 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 884 | CodeGenModule::GetOrCreateLLVMFunction(StringRef MangledName, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 885 | llvm::Type *Ty, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 886 | GlobalDecl D, bool ForVTable, |
| 887 | llvm::Attributes ExtraAttrs) { |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 888 | // Lookup the entry, lazily creating it if necessary. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 889 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 890 | if (Entry) { |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 891 | if (WeakRefReferences.count(Entry)) { |
| 892 | const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl()); |
| 893 | if (FD && !FD->hasAttr<WeakAttr>()) |
Anders Carlsson | 7270ee4 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 894 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 895 | |
| 896 | WeakRefReferences.erase(Entry); |
| 897 | } |
| 898 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 899 | if (Entry->getType()->getElementType() == Ty) |
| 900 | return Entry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 902 | // Make sure the result is of the correct type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 903 | return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo()); |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 904 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | |
Eli Friedman | 654ad40 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 906 | // This function doesn't have a complete type (for example, the return |
| 907 | // type is an incomplete struct). Use a fake type instead, and make |
| 908 | // sure not to try to set attributes. |
| 909 | bool IsIncompleteFunction = false; |
John McCall | 784f211 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 910 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 911 | llvm::FunctionType *FTy; |
John McCall | 784f211 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 912 | if (isa<llvm::FunctionType>(Ty)) { |
| 913 | FTy = cast<llvm::FunctionType>(Ty); |
| 914 | } else { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 915 | FTy = llvm::FunctionType::get(VoidTy, false); |
Eli Friedman | 654ad40 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 916 | IsIncompleteFunction = true; |
| 917 | } |
Chris Lattner | bcaedae | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 918 | |
John McCall | 784f211 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 919 | llvm::Function *F = llvm::Function::Create(FTy, |
Eli Friedman | 654ad40 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 920 | llvm::Function::ExternalLinkage, |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 921 | MangledName, &getModule()); |
| 922 | assert(F->getName() == MangledName && "name was uniqued!"); |
Eli Friedman | 654ad40 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 923 | if (D.getDecl()) |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 924 | SetFunctionAttributes(D, F, IsIncompleteFunction); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 925 | if (ExtraAttrs != llvm::Attribute::None) |
| 926 | F->addFnAttr(ExtraAttrs); |
Eli Friedman | 654ad40 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 928 | // This is the first use or definition of a mangled name. If there is a |
| 929 | // deferred decl with this name, remember that we need to emit it at the end |
| 930 | // of the file. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 931 | llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 932 | if (DDI != DeferredDecls.end()) { |
| 933 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 934 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
| 935 | DeferredDeclsToEmit.push_back(DDI->second); |
| 936 | DeferredDecls.erase(DDI); |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 937 | |
| 938 | // Otherwise, there are cases we have to worry about where we're |
| 939 | // using a declaration for which we must emit a definition but where |
| 940 | // we might not find a top-level definition: |
| 941 | // - member functions defined inline in their classes |
| 942 | // - friend functions defined inline in some class |
| 943 | // - special member functions with implicit definitions |
| 944 | // If we ever change our AST traversal to walk into class methods, |
| 945 | // this will be unnecessary. |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 946 | // |
| 947 | // We also don't emit a definition for a function if it's going to be an entry |
| 948 | // in a vtable, unless it's already marked as used. |
Rafael Espindola | 01de7a4 | 2011-02-03 06:30:58 +0000 | [diff] [blame] | 949 | } else if (getLangOptions().CPlusPlus && D.getDecl()) { |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 950 | // Look for a declaration that's lexically in a record. |
| 951 | const FunctionDecl *FD = cast<FunctionDecl>(D.getDecl()); |
| 952 | do { |
| 953 | if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 954 | if (FD->isImplicit() && !ForVTable) { |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 955 | assert(FD->isUsed() && "Sema didn't mark implicit function as used!"); |
Douglas Gregor | a29bf41 | 2011-02-09 02:03:05 +0000 | [diff] [blame] | 956 | DeferredDeclsToEmit.push_back(D.getWithDecl(FD)); |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 957 | break; |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 958 | } else if (FD->doesThisDeclarationHaveABody()) { |
Douglas Gregor | a29bf41 | 2011-02-09 02:03:05 +0000 | [diff] [blame] | 959 | DeferredDeclsToEmit.push_back(D.getWithDecl(FD)); |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 960 | break; |
| 961 | } |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 962 | } |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 963 | FD = FD->getPreviousDeclaration(); |
| 964 | } while (FD); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 965 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
John McCall | 784f211 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 967 | // Make sure the result is of the requested type. |
| 968 | if (!IsIncompleteFunction) { |
| 969 | assert(F->getType()->getElementType() == Ty); |
| 970 | return F; |
| 971 | } |
| 972 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 973 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
John McCall | 784f211 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 974 | return llvm::ConstantExpr::getBitCast(F, PTy); |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 977 | /// GetAddrOfFunction - Return the address of the given function. If Ty is |
| 978 | /// non-null, then this function will use the specified type if it has to |
| 979 | /// create it (this occurs when we see a definition of the function). |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 980 | llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 981 | llvm::Type *Ty, |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 982 | bool ForVTable) { |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 983 | // If there was no specific requested type, just convert it now. |
| 984 | if (!Ty) |
Anders Carlsson | 4a6835e | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 985 | Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType()); |
Chris Lattner | bcaedae | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 986 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 987 | StringRef MangledName = getMangledName(GD); |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 988 | return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 989 | } |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 990 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 991 | /// CreateRuntimeFunction - Create a new runtime function with the specified |
| 992 | /// type and name. |
| 993 | llvm::Constant * |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 994 | CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 995 | StringRef Name, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 996 | llvm::Attributes ExtraAttrs) { |
| 997 | return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false, |
| 998 | ExtraAttrs); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 999 | } |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1000 | |
Douglas Gregor | da55074 | 2011-05-07 22:06:45 +0000 | [diff] [blame] | 1001 | static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D, |
| 1002 | bool ConstantInit) { |
John McCall | 8878686 | 2010-02-08 21:46:50 +0000 | [diff] [blame] | 1003 | if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType()) |
Eli Friedman | 20e098b | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 1004 | return false; |
Douglas Gregor | da55074 | 2011-05-07 22:06:45 +0000 | [diff] [blame] | 1005 | |
| 1006 | if (Context.getLangOptions().CPlusPlus) { |
| 1007 | if (const RecordType *Record |
| 1008 | = Context.getBaseElementType(D->getType())->getAs<RecordType>()) |
Douglas Gregor | 2bb1101 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 1009 | return ConstantInit && |
| 1010 | cast<CXXRecordDecl>(Record->getDecl())->isPOD() && |
| 1011 | !cast<CXXRecordDecl>(Record->getDecl())->hasMutableFields(); |
Eli Friedman | 20e098b | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 1012 | } |
Douglas Gregor | da55074 | 2011-05-07 22:06:45 +0000 | [diff] [blame] | 1013 | |
Eli Friedman | 20e098b | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 1014 | return true; |
| 1015 | } |
| 1016 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1017 | /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, |
| 1018 | /// create and return an llvm GlobalVariable with the specified type. If there |
| 1019 | /// is something in the module with the specified name, return it potentially |
| 1020 | /// bitcasted to the right type. |
| 1021 | /// |
| 1022 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 1023 | /// to set the attributes on the global when it is first created. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1024 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1025 | CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1026 | llvm::PointerType *Ty, |
Rafael Espindola | c532b50 | 2011-01-18 21:07:57 +0000 | [diff] [blame] | 1027 | const VarDecl *D, |
| 1028 | bool UnnamedAddr) { |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 1029 | // Lookup the entry, lazily creating it if necessary. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1030 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1031 | if (Entry) { |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1032 | if (WeakRefReferences.count(Entry)) { |
| 1033 | if (D && !D->hasAttr<WeakAttr>()) |
Anders Carlsson | 7270ee4 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 1034 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1035 | |
| 1036 | WeakRefReferences.erase(Entry); |
| 1037 | } |
| 1038 | |
Rafael Espindola | c532b50 | 2011-01-18 21:07:57 +0000 | [diff] [blame] | 1039 | if (UnnamedAddr) |
| 1040 | Entry->setUnnamedAddr(true); |
| 1041 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1042 | if (Entry->getType() == Ty) |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1043 | return Entry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1045 | // Make sure the result is of the correct type. |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1046 | return llvm::ConstantExpr::getBitCast(Entry, Ty); |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 1047 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1049 | // This is the first use or definition of a mangled name. If there is a |
| 1050 | // deferred decl with this name, remember that we need to emit it at the end |
| 1051 | // of the file. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1052 | llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1053 | if (DDI != DeferredDecls.end()) { |
| 1054 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 1055 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
| 1056 | DeferredDeclsToEmit.push_back(DDI->second); |
| 1057 | DeferredDecls.erase(DDI); |
| 1058 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
| 1060 | llvm::GlobalVariable *GV = |
| 1061 | new llvm::GlobalVariable(getModule(), Ty->getElementType(), false, |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1062 | llvm::GlobalValue::ExternalLinkage, |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1063 | 0, MangledName, 0, |
Eli Friedman | 56ebe50 | 2009-04-19 21:05:03 +0000 | [diff] [blame] | 1064 | false, Ty->getAddressSpace()); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1065 | |
| 1066 | // Handle things which are present even on external declarations. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1067 | if (D) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1068 | // FIXME: This code is overly simple and should be merged with other global |
| 1069 | // handling. |
Douglas Gregor | da55074 | 2011-05-07 22:06:45 +0000 | [diff] [blame] | 1070 | GV->setConstant(DeclIsConstantGlobal(Context, D, false)); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1071 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1072 | // Set linkage and visibility in case we never see a definition. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1073 | NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility(); |
| 1074 | if (LV.linkage() != ExternalLinkage) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 1075 | // Don't set internal linkage on declarations. |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1076 | } else { |
| 1077 | if (D->hasAttr<DLLImportAttr>()) |
| 1078 | GV->setLinkage(llvm::GlobalValue::DLLImportLinkage); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1079 | else if (D->hasAttr<WeakAttr>() || D->isWeakImported()) |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1080 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1081 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1082 | // Set visibility on a declaration only if it's explicit. |
| 1083 | if (LV.visibilityExplicit()) |
| 1084 | GV->setVisibility(GetLLVMVisibility(LV.visibility())); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1085 | } |
Eli Friedman | 56ebe50 | 2009-04-19 21:05:03 +0000 | [diff] [blame] | 1086 | |
| 1087 | GV->setThreadLocal(D->isThreadSpecified()); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1088 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1090 | return GV; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1093 | |
Anders Carlsson | 3bd6202 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1094 | llvm::GlobalVariable * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1095 | CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1096 | llvm::Type *Ty, |
Anders Carlsson | 3bd6202 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1097 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 1098 | llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); |
| 1099 | llvm::GlobalVariable *OldGV = 0; |
| 1100 | |
| 1101 | |
| 1102 | if (GV) { |
| 1103 | // Check if the variable has the right type. |
| 1104 | if (GV->getType()->getElementType() == Ty) |
| 1105 | return GV; |
| 1106 | |
| 1107 | // Because C++ name mangling, the only way we can end up with an already |
| 1108 | // existing global with the same name is if it has been declared extern "C". |
Anders Carlsson | 96eaf29 | 2011-01-29 18:25:07 +0000 | [diff] [blame] | 1109 | assert(GV->isDeclaration() && "Declaration has wrong type!"); |
Anders Carlsson | 3bd6202 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1110 | OldGV = GV; |
| 1111 | } |
| 1112 | |
| 1113 | // Create a new variable. |
| 1114 | GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, |
| 1115 | Linkage, 0, Name); |
| 1116 | |
| 1117 | if (OldGV) { |
| 1118 | // Replace occurrences of the old variable if needed. |
| 1119 | GV->takeName(OldGV); |
| 1120 | |
| 1121 | if (!OldGV->use_empty()) { |
| 1122 | llvm::Constant *NewPtrForOldDecl = |
| 1123 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 1124 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 1125 | } |
| 1126 | |
| 1127 | OldGV->eraseFromParent(); |
| 1128 | } |
| 1129 | |
| 1130 | return GV; |
| 1131 | } |
| 1132 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1133 | /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the |
| 1134 | /// given global variable. If Ty is non-null and if the global doesn't exist, |
| 1135 | /// then it will be greated with the specified type instead of whatever the |
| 1136 | /// normal requested type would be. |
| 1137 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1138 | llvm::Type *Ty) { |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1139 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 1140 | QualType ASTTy = D->getType(); |
| 1141 | if (Ty == 0) |
| 1142 | Ty = getTypes().ConvertTypeForMem(ASTTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1144 | llvm::PointerType *PTy = |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1145 | llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy)); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1146 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1147 | StringRef MangledName = getMangledName(D); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1148 | return GetOrCreateLLVMGlobal(MangledName, PTy, D); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /// CreateRuntimeVariable - Create a new runtime global variable with the |
| 1152 | /// specified type and name. |
| 1153 | llvm::Constant * |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1154 | CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1155 | StringRef Name) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1156 | return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0, |
Rafael Espindola | c532b50 | 2011-01-18 21:07:57 +0000 | [diff] [blame] | 1157 | true); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Daniel Dunbar | 03f5ad9 | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1160 | void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { |
| 1161 | assert(!D->getInit() && "Cannot emit definite definitions here!"); |
| 1162 | |
Douglas Gregor | 7520bd1 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 1163 | if (MayDeferGeneration(D)) { |
| 1164 | // If we have not seen a reference to this variable yet, place it |
| 1165 | // into the deferred declarations table to be emitted if needed |
| 1166 | // later. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1167 | StringRef MangledName = getMangledName(D); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1168 | if (!GetGlobalValue(MangledName)) { |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 1169 | DeferredDecls[MangledName] = D; |
Daniel Dunbar | 03f5ad9 | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1170 | return; |
Douglas Gregor | 7520bd1 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | // The tentative definition is the only definition. |
Daniel Dunbar | 03f5ad9 | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1175 | EmitGlobalVarDefinition(D); |
| 1176 | } |
| 1177 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1178 | void CodeGenModule::EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired) { |
| 1179 | if (DefinitionRequired) |
| 1180 | getVTables().GenerateClassData(getVTableLinkage(Class), Class); |
| 1181 | } |
| 1182 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1183 | llvm::GlobalVariable::LinkageTypes |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 1184 | CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { |
Eli Friedman | cb5d2d0 | 2011-06-10 21:53:06 +0000 | [diff] [blame] | 1185 | if (RD->getLinkage() != ExternalLinkage) |
Douglas Gregor | dffb801 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 1186 | return llvm::GlobalVariable::InternalLinkage; |
| 1187 | |
| 1188 | if (const CXXMethodDecl *KeyFunction |
| 1189 | = RD->getASTContext().getKeyFunction(RD)) { |
| 1190 | // If this class has a key function, use that to determine the linkage of |
| 1191 | // the vtable. |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1192 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1193 | if (KeyFunction->hasBody(Def)) |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1194 | KeyFunction = cast<CXXMethodDecl>(Def); |
Douglas Gregor | dffb801 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1196 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 1197 | case TSK_Undeclared: |
| 1198 | case TSK_ExplicitSpecialization: |
Anders Carlsson | 6d7f847 | 2011-01-30 20:45:54 +0000 | [diff] [blame] | 1199 | // When compiling with optimizations turned on, we emit all vtables, |
| 1200 | // even if the key function is not defined in the current translation |
| 1201 | // unit. If this is the case, use available_externally linkage. |
| 1202 | if (!Def && CodeGenOpts.OptimizationLevel) |
| 1203 | return llvm::GlobalVariable::AvailableExternallyLinkage; |
| 1204 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1205 | if (KeyFunction->isInlined()) |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1206 | return !Context.getLangOptions().AppleKext ? |
| 1207 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 1208 | llvm::Function::InternalLinkage; |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1209 | |
| 1210 | return llvm::GlobalVariable::ExternalLinkage; |
| 1211 | |
| 1212 | case TSK_ImplicitInstantiation: |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1213 | return !Context.getLangOptions().AppleKext ? |
| 1214 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 1215 | llvm::Function::InternalLinkage; |
Anders Carlsson | f502d93 | 2011-01-24 00:46:19 +0000 | [diff] [blame] | 1216 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1217 | case TSK_ExplicitInstantiationDefinition: |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1218 | return !Context.getLangOptions().AppleKext ? |
| 1219 | llvm::GlobalVariable::WeakODRLinkage : |
| 1220 | llvm::Function::InternalLinkage; |
Anders Carlsson | f502d93 | 2011-01-24 00:46:19 +0000 | [diff] [blame] | 1221 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1222 | case TSK_ExplicitInstantiationDeclaration: |
| 1223 | // FIXME: Use available_externally linkage. However, this currently |
| 1224 | // breaks LLVM's build due to undefined symbols. |
| 1225 | // return llvm::GlobalVariable::AvailableExternallyLinkage; |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1226 | return !Context.getLangOptions().AppleKext ? |
| 1227 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 1228 | llvm::Function::InternalLinkage; |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1229 | } |
Douglas Gregor | dffb801 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Fariborz Jahanian | 53bad4e | 2011-02-04 00:32:39 +0000 | [diff] [blame] | 1232 | if (Context.getLangOptions().AppleKext) |
| 1233 | return llvm::Function::InternalLinkage; |
| 1234 | |
Douglas Gregor | dffb801 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 1235 | switch (RD->getTemplateSpecializationKind()) { |
| 1236 | case TSK_Undeclared: |
| 1237 | case TSK_ExplicitSpecialization: |
| 1238 | case TSK_ImplicitInstantiation: |
Douglas Gregor | dffb801 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 1239 | // FIXME: Use available_externally linkage. However, this currently |
| 1240 | // breaks LLVM's build due to undefined symbols. |
| 1241 | // return llvm::GlobalVariable::AvailableExternallyLinkage; |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1242 | case TSK_ExplicitInstantiationDeclaration: |
Fariborz Jahanian | 53bad4e | 2011-02-04 00:32:39 +0000 | [diff] [blame] | 1243 | return llvm::GlobalVariable::LinkOnceODRLinkage; |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1244 | |
| 1245 | case TSK_ExplicitInstantiationDefinition: |
Fariborz Jahanian | 53bad4e | 2011-02-04 00:32:39 +0000 | [diff] [blame] | 1246 | return llvm::GlobalVariable::WeakODRLinkage; |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | // Silence GCC warning. |
Fariborz Jahanian | 53bad4e | 2011-02-04 00:32:39 +0000 | [diff] [blame] | 1250 | return llvm::GlobalVariable::LinkOnceODRLinkage; |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1253 | CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const { |
Ken Dyck | 06f486e | 2011-01-18 02:01:14 +0000 | [diff] [blame] | 1254 | return Context.toCharUnitsFromBits( |
| 1255 | TheTargetData.getTypeStoreSizeInBits(Ty)); |
Ken Dyck | 687cc4a | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1258 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 1259 | llvm::Constant *Init = 0; |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1260 | QualType ASTTy = D->getType(); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 1261 | bool NonConstInit = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1263 | const Expr *InitExpr = D->getAnyInitializer(); |
Anders Carlsson | 3bb9269 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 1264 | |
| 1265 | if (!InitExpr) { |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 1266 | // This is a tentative definition; tentative definitions are |
Daniel Dunbar | 03f5ad9 | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1267 | // implicitly initialized with { 0 }. |
| 1268 | // |
| 1269 | // Note that tentative definitions are only emitted at the end of |
| 1270 | // a translation unit, so they should never have incomplete |
| 1271 | // type. In addition, EmitTentativeDefinition makes sure that we |
| 1272 | // never attempt to emit a tentative definition if a real one |
| 1273 | // exists. A use may still exists, however, so we still may need |
| 1274 | // to do a RAUW. |
| 1275 | assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); |
Anders Carlsson | b0d0ea0 | 2009-08-02 21:18:22 +0000 | [diff] [blame] | 1276 | Init = EmitNullConstant(D->getType()); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1277 | } else { |
Douglas Gregor | c446d18 | 2010-05-05 20:15:55 +0000 | [diff] [blame] | 1278 | Init = EmitConstantExpr(InitExpr, D->getType()); |
Eli Friedman | 6e656f4 | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 1279 | if (!Init) { |
Anders Carlsson | 3bb9269 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 1280 | QualType T = InitExpr->getType(); |
Douglas Gregor | c446d18 | 2010-05-05 20:15:55 +0000 | [diff] [blame] | 1281 | if (D->getType()->isReferenceType()) |
| 1282 | T = D->getType(); |
| 1283 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 1284 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 1285 | Init = EmitNullConstant(T); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 1286 | NonConstInit = true; |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 1287 | } else { |
| 1288 | ErrorUnsupported(D, "static initializer"); |
| 1289 | Init = llvm::UndefValue::get(getTypes().ConvertType(T)); |
| 1290 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 1291 | } else { |
| 1292 | // We don't need an initializer, so remove the entry for the delayed |
| 1293 | // initializer position (just in case this entry was delayed). |
| 1294 | if (getLangOptions().CPlusPlus) |
| 1295 | DelayedCXXInitPosition.erase(D); |
Eli Friedman | 6e656f4 | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 1296 | } |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1297 | } |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1298 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1299 | llvm::Type* InitType = Init->getType(); |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1300 | llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1302 | // Strip off a bitcast if we got one back. |
| 1303 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Chris Lattner | 9d4a15f | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1304 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
| 1305 | // all zero index gep. |
| 1306 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1307 | Entry = CE->getOperand(0); |
| 1308 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1310 | // Entry is now either a Function or GlobalVariable. |
| 1311 | llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1313 | // We have a definition after a declaration with the wrong type. |
| 1314 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 1315 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 1316 | // (which will be a definition). |
| 1317 | // |
| 1318 | // This happens if there is a prototype for a global (e.g. |
| 1319 | // "extern int x[];") and then a definition of a different type (e.g. |
| 1320 | // "int x[10];"). This also happens when an initializer has a different type |
| 1321 | // from the type of the global (this happens with unions). |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1322 | if (GV == 0 || |
| 1323 | GV->getType()->getElementType() != InitType || |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1324 | GV->getType()->getAddressSpace() != |
| 1325 | getContext().getTargetAddressSpace(ASTTy)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1326 | |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1327 | // Move the old entry aside so that we'll create a new one. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1328 | Entry->setName(StringRef()); |
Daniel Dunbar | 232350d | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 1329 | |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1330 | // Make a new global with the correct type, this is now guaranteed to work. |
| 1331 | GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1332 | |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1333 | // Replace all uses of the old global with the new global |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | llvm::Constant *NewPtrForOldDecl = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1335 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1336 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Erase the old global, since it is no longer used. |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1339 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 1340 | } |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 1341 | |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1342 | if (D->hasAttr<AnnotateAttr>()) |
| 1343 | AddGlobalAnnotations(D, GV); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1345 | GV->setInitializer(Init); |
Chris Lattner | e78b86f | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1346 | |
| 1347 | // If it is safe to mark the global 'constant', do so now. |
| 1348 | GV->setConstant(false); |
Douglas Gregor | da55074 | 2011-05-07 22:06:45 +0000 | [diff] [blame] | 1349 | if (!NonConstInit && DeclIsConstantGlobal(Context, D, true)) |
Chris Lattner | e78b86f | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1350 | GV->setConstant(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1352 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
Fariborz Jahanian | 354e712 | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1353 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1354 | // Set the llvm linkage type as appropriate. |
Fariborz Jahanian | 354e712 | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1355 | llvm::GlobalValue::LinkageTypes Linkage = |
| 1356 | GetLLVMLinkageVarDefinition(D, GV); |
| 1357 | GV->setLinkage(Linkage); |
| 1358 | if (Linkage == llvm::GlobalVariable::CommonLinkage) |
Chris Lattner | e78b86f | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1359 | // common vars aren't constant even if declared const. |
| 1360 | GV->setConstant(false); |
Daniel Dunbar | 7e714cd | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 1361 | |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1362 | SetCommonAttributes(D, GV); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1363 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1364 | // Emit the initializer function if necessary. |
| 1365 | if (NonConstInit) |
| 1366 | EmitCXXGlobalVarDeclInitFunc(D, GV); |
| 1367 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1368 | // Emit global variable debug information. |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 1369 | if (CGDebugInfo *DI = getModuleDebugInfo()) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 1370 | DI->setLocation(D->getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1371 | DI->EmitGlobalVariable(GV, D); |
| 1372 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1373 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1374 | |
Fariborz Jahanian | 354e712 | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1375 | llvm::GlobalValue::LinkageTypes |
| 1376 | CodeGenModule::GetLLVMLinkageVarDefinition(const VarDecl *D, |
| 1377 | llvm::GlobalVariable *GV) { |
| 1378 | GVALinkage Linkage = getContext().GetGVALinkageForVariable(D); |
| 1379 | if (Linkage == GVA_Internal) |
| 1380 | return llvm::Function::InternalLinkage; |
| 1381 | else if (D->hasAttr<DLLImportAttr>()) |
| 1382 | return llvm::Function::DLLImportLinkage; |
| 1383 | else if (D->hasAttr<DLLExportAttr>()) |
| 1384 | return llvm::Function::DLLExportLinkage; |
| 1385 | else if (D->hasAttr<WeakAttr>()) { |
| 1386 | if (GV->isConstant()) |
| 1387 | return llvm::GlobalVariable::WeakODRLinkage; |
| 1388 | else |
| 1389 | return llvm::GlobalVariable::WeakAnyLinkage; |
| 1390 | } else if (Linkage == GVA_TemplateInstantiation || |
| 1391 | Linkage == GVA_ExplicitTemplateInstantiation) |
John McCall | 99ace16 | 2011-04-12 01:46:54 +0000 | [diff] [blame] | 1392 | return llvm::GlobalVariable::WeakODRLinkage; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1393 | else if (!getLangOptions().CPlusPlus && |
| 1394 | ((!CodeGenOpts.NoCommon && !D->getAttr<NoCommonAttr>()) || |
| 1395 | D->getAttr<CommonAttr>()) && |
Fariborz Jahanian | 354e712 | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1396 | !D->hasExternalStorage() && !D->getInit() && |
Fariborz Jahanian | ab27d6e | 2011-06-20 17:50:03 +0000 | [diff] [blame] | 1397 | !D->getAttr<SectionAttr>() && !D->isThreadSpecified() && |
| 1398 | !D->getAttr<WeakImportAttr>()) { |
Fariborz Jahanian | 354e712 | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1399 | // Thread local vars aren't considered common linkage. |
| 1400 | return llvm::GlobalVariable::CommonLinkage; |
| 1401 | } |
| 1402 | return llvm::GlobalVariable::ExternalLinkage; |
| 1403 | } |
| 1404 | |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1405 | /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we |
| 1406 | /// implement a function with no prototype, e.g. "int foo() {}". If there are |
| 1407 | /// existing call uses of the old function in the module, this adjusts them to |
| 1408 | /// call the new function directly. |
| 1409 | /// |
| 1410 | /// This is not just a cleanup: the always_inline pass requires direct calls to |
| 1411 | /// functions to be able to inline them. If there is a bitcast in the way, it |
| 1412 | /// won't inline them. Instcombine normally deletes these calls, but it isn't |
| 1413 | /// run at -O0. |
| 1414 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
| 1415 | llvm::Function *NewFn) { |
| 1416 | // If we're redefining a global as a function, don't transform it. |
| 1417 | llvm::Function *OldFn = dyn_cast<llvm::Function>(Old); |
| 1418 | if (OldFn == 0) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1420 | llvm::Type *NewRetTy = NewFn->getReturnType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1421 | SmallVector<llvm::Value*, 4> ArgList; |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1422 | |
| 1423 | for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end(); |
Benjamin Kramer | dbf02bc | 2010-04-10 11:02:40 +0000 | [diff] [blame] | 1424 | UI != E; ) { |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1425 | // TODO: Do invokes ever occur in C code? If so, we should handle them too. |
Benjamin Kramer | dbf02bc | 2010-04-10 11:02:40 +0000 | [diff] [blame] | 1426 | llvm::Value::use_iterator I = UI++; // Increment before the CI is erased. |
| 1427 | llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*I); |
Gabor Greif | 8670cd3 | 2010-07-28 09:19:33 +0000 | [diff] [blame] | 1428 | if (!CI) continue; // FIXME: when we allow Invoke, just do CallSite CS(*I) |
Gabor Greif | 35db3b9 | 2010-04-10 03:45:50 +0000 | [diff] [blame] | 1429 | llvm::CallSite CS(CI); |
Benjamin Kramer | dbf02bc | 2010-04-10 11:02:40 +0000 | [diff] [blame] | 1430 | if (!CI || !CS.isCallee(I)) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1432 | // If the return types don't match exactly, and if the call isn't dead, then |
| 1433 | // we can't transform this call. |
| 1434 | if (CI->getType() != NewRetTy && !CI->use_empty()) |
| 1435 | continue; |
| 1436 | |
John McCall | 40f9c30 | 2011-08-03 00:43:55 +0000 | [diff] [blame] | 1437 | // Get the attribute list. |
| 1438 | llvm::SmallVector<llvm::AttributeWithIndex, 8> AttrVec; |
| 1439 | llvm::AttrListPtr AttrList = CI->getAttributes(); |
| 1440 | |
| 1441 | // Get any return attributes. |
| 1442 | llvm::Attributes RAttrs = AttrList.getRetAttributes(); |
| 1443 | |
| 1444 | // Add the return attributes. |
| 1445 | if (RAttrs) |
| 1446 | AttrVec.push_back(llvm::AttributeWithIndex::get(0, RAttrs)); |
| 1447 | |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1448 | // If the function was passed too few arguments, don't transform. If extra |
| 1449 | // arguments were passed, we silently drop them. If any of the types |
| 1450 | // mismatch, we don't transform. |
| 1451 | unsigned ArgNo = 0; |
| 1452 | bool DontTransform = false; |
| 1453 | for (llvm::Function::arg_iterator AI = NewFn->arg_begin(), |
| 1454 | E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) { |
Gabor Greif | 35db3b9 | 2010-04-10 03:45:50 +0000 | [diff] [blame] | 1455 | if (CS.arg_size() == ArgNo || |
| 1456 | CS.getArgument(ArgNo)->getType() != AI->getType()) { |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1457 | DontTransform = true; |
| 1458 | break; |
| 1459 | } |
John McCall | 40f9c30 | 2011-08-03 00:43:55 +0000 | [diff] [blame] | 1460 | |
| 1461 | // Add any parameter attributes. |
| 1462 | if (llvm::Attributes PAttrs = AttrList.getParamAttributes(ArgNo + 1)) |
| 1463 | AttrVec.push_back(llvm::AttributeWithIndex::get(ArgNo + 1, PAttrs)); |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1464 | } |
| 1465 | if (DontTransform) |
| 1466 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
John McCall | 40f9c30 | 2011-08-03 00:43:55 +0000 | [diff] [blame] | 1468 | if (llvm::Attributes FnAttrs = AttrList.getFnAttributes()) |
| 1469 | AttrVec.push_back(llvm::AttributeWithIndex::get(~0, FnAttrs)); |
| 1470 | |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1471 | // Okay, we can transform this. Create the new call instruction and copy |
| 1472 | // over the required information. |
Gabor Greif | 6ba728d | 2010-04-10 02:56:12 +0000 | [diff] [blame] | 1473 | ArgList.append(CS.arg_begin(), CS.arg_begin() + ArgNo); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1474 | llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList, "", CI); |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1475 | ArgList.clear(); |
Benjamin Kramer | ffbb15e | 2009-10-05 13:47:21 +0000 | [diff] [blame] | 1476 | if (!NewCall->getType()->isVoidTy()) |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1477 | NewCall->takeName(CI); |
John McCall | 40f9c30 | 2011-08-03 00:43:55 +0000 | [diff] [blame] | 1478 | NewCall->setAttributes(llvm::AttrListPtr::get(AttrVec.begin(), |
| 1479 | AttrVec.end())); |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1480 | NewCall->setCallingConv(CI->getCallingConv()); |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1481 | |
| 1482 | // Finally, remove the old call, replacing any uses with the new one. |
Chris Lattner | 00549fc | 2009-10-14 05:49:21 +0000 | [diff] [blame] | 1483 | if (!CI->use_empty()) |
| 1484 | CI->replaceAllUsesWith(NewCall); |
Devang Patel | 3b122bc | 2009-10-13 17:02:04 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1486 | // Copy debug location attached to CI. |
| 1487 | if (!CI->getDebugLoc().isUnknown()) |
| 1488 | NewCall->setDebugLoc(CI->getDebugLoc()); |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1489 | CI->eraseFromParent(); |
| 1490 | } |
| 1491 | } |
| 1492 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1493 | |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1494 | void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1495 | const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1496 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 1497 | // Compute the function info and LLVM type. |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1498 | const CGFunctionInfo &FI = getTypes().getFunctionInfo(GD); |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 1499 | bool variadic = false; |
| 1500 | if (const FunctionProtoType *fpt = D->getType()->getAs<FunctionProtoType>()) |
| 1501 | variadic = fpt->isVariadic(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1502 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI, variadic); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1503 | |
Chris Lattner | 9fa959d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 1504 | // Get or create the prototype for the function. |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1505 | llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1507 | // Strip off a bitcast if we got one back. |
| 1508 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
| 1509 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
| 1510 | Entry = CE->getOperand(0); |
| 1511 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | |
| 1513 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1514 | if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1515 | llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
Daniel Dunbar | 4274581 | 2009-03-09 23:53:08 +0000 | [diff] [blame] | 1517 | // If the types mismatch then we have to rewrite the definition. |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1518 | assert(OldFn->isDeclaration() && |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1519 | "Shouldn't replace non-declaration"); |
Chris Lattner | 3480950 | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 1520 | |
Chris Lattner | 62b33ea | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 1521 | // F is the Function* for the one with the wrong type, we must make a new |
| 1522 | // Function* and update everything that used F (a declaration) with the new |
| 1523 | // Function* (which will be a definition). |
| 1524 | // |
| 1525 | // This happens if there is a prototype for a function |
| 1526 | // (e.g. "int f()") and then a definition of a different type |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1527 | // (e.g. "int f(int x)"). Move the old function aside so that it |
| 1528 | // doesn't interfere with GetAddrOfFunction. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1529 | OldFn->setName(StringRef()); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1530 | llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1532 | // If this is an implementation of a function without a prototype, try to |
| 1533 | // replace any existing uses of the function (which may be calls) with uses |
| 1534 | // of the new function |
Chris Lattner | 9fa959d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 1535 | if (D->getType()->isFunctionNoProtoType()) { |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1536 | ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn); |
Chris Lattner | 9fa959d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 1537 | OldFn->removeDeadConstantUsers(); |
| 1538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | |
Chris Lattner | 62b33ea | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 1540 | // Replace uses of F with the Function we will endow with a body. |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1541 | if (!Entry->use_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | llvm::Constant *NewPtrForOldDecl = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1543 | llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1544 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
| 1545 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1546 | |
Chris Lattner | 62b33ea | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 1547 | // Ok, delete the old function now, which is dead. |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1548 | OldFn->eraseFromParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1550 | Entry = NewFn; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1551 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1553 | // We need to set linkage and visibility on the function before |
| 1554 | // generating code for it because various parts of IR generation |
| 1555 | // want to propagate this information down (e.g. to local static |
| 1556 | // declarations). |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1557 | llvm::Function *Fn = cast<llvm::Function>(Entry); |
John McCall | 8b24233 | 2010-05-25 04:30:21 +0000 | [diff] [blame] | 1558 | setFunctionLinkage(D, Fn); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1559 | |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1560 | // FIXME: this is redundant with part of SetFunctionDefinitionAttributes |
Anders Carlsson | 0ffeaad | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 1561 | setGlobalVisibility(Fn, D); |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1562 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1563 | CodeGenFunction(*this).GenerateCode(D, Fn, FI); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1564 | |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1565 | SetFunctionDefinitionAttributes(D, Fn); |
| 1566 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1568 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1569 | AddGlobalCtor(Fn, CA->getPriority()); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1570 | if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1571 | AddGlobalDtor(Fn, DA->getPriority()); |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1572 | if (D->hasAttr<AnnotateAttr>()) |
| 1573 | AddGlobalAnnotations(D, Fn); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1576 | void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { |
| 1577 | const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1578 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1579 | assert(AA && "Not an alias?"); |
| 1580 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1581 | StringRef MangledName = getMangledName(GD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1583 | // If there is a definition in the module, then it wins over the alias. |
| 1584 | // This is dubious, but allow it to be safe. Just ignore the alias. |
| 1585 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 1586 | if (Entry && !Entry->isDeclaration()) |
| 1587 | return; |
| 1588 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1589 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1590 | |
| 1591 | // Create a reference to the named value. This ensures that it is emitted |
| 1592 | // if a deferred decl. |
| 1593 | llvm::Constant *Aliasee; |
| 1594 | if (isa<llvm::FunctionType>(DeclTy)) |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 1595 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl(), |
| 1596 | /*ForVTable=*/false); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1597 | else |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1598 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1599 | llvm::PointerType::getUnqual(DeclTy), 0); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1600 | |
| 1601 | // Create the new alias itself, but don't set a name yet. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | llvm::GlobalValue *GA = |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1603 | new llvm::GlobalAlias(Aliasee->getType(), |
| 1604 | llvm::Function::ExternalLinkage, |
| 1605 | "", Aliasee, &getModule()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1607 | if (Entry) { |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1608 | assert(Entry->isDeclaration()); |
| 1609 | |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1610 | // If there is a declaration in the module, then we had an extern followed |
| 1611 | // by the alias, as in: |
| 1612 | // extern int test6(); |
| 1613 | // ... |
| 1614 | // int test6() __attribute__((alias("test7"))); |
| 1615 | // |
| 1616 | // Remove it and replace uses of it with the alias. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1617 | GA->takeName(Entry); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1619 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1620 | Entry->getType())); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1621 | Entry->eraseFromParent(); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1622 | } else { |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 1623 | GA->setName(MangledName); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1624 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1626 | // Set attributes which are particular to an alias; this is a |
| 1627 | // specialization of the attributes which may be set on a global |
| 1628 | // variable/function. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1629 | if (D->hasAttr<DLLExportAttr>()) { |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1630 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1631 | // The dllexport attribute is ignored for undefined symbols. |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1632 | if (FD->hasBody()) |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1633 | GA->setLinkage(llvm::Function::DLLExportLinkage); |
| 1634 | } else { |
| 1635 | GA->setLinkage(llvm::Function::DLLExportLinkage); |
| 1636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | } else if (D->hasAttr<WeakAttr>() || |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1638 | D->hasAttr<WeakRefAttr>() || |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1639 | D->isWeakImported()) { |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1640 | GA->setLinkage(llvm::Function::WeakAnyLinkage); |
| 1641 | } |
| 1642 | |
| 1643 | SetCommonAttributes(D, GA); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Benjamin Kramer | 8dd55a3 | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 1646 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID, |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 1647 | ArrayRef<llvm::Type*> Tys) { |
Jay Foad | df983a8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1648 | return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID, |
Benjamin Kramer | 8dd55a3 | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 1649 | Tys); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 1650 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 1651 | |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1652 | static llvm::StringMapEntry<llvm::Constant*> & |
| 1653 | GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, |
| 1654 | const StringLiteral *Literal, |
Daniel Dunbar | 70ee975 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1655 | bool TargetIsLSB, |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1656 | bool &IsUTF16, |
| 1657 | unsigned &StringLength) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1658 | StringRef String = Literal->getString(); |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1659 | unsigned NumBytes = String.size(); |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1660 | |
Daniel Dunbar | f015b03 | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 1661 | // Check for simple case. |
| 1662 | if (!Literal->containsNonAsciiOrNull()) { |
| 1663 | StringLength = NumBytes; |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1664 | return Map.GetOrCreateValue(String); |
Daniel Dunbar | f015b03 | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1667 | // Otherwise, convert the UTF8 literals into a byte string. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1668 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1669 | const UTF8 *FromPtr = (UTF8 *)String.data(); |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1670 | UTF16 *ToPtr = &ToBuf[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
Fariborz Jahanian | e7ddfb9 | 2010-09-07 19:57:04 +0000 | [diff] [blame] | 1672 | (void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 1673 | &ToPtr, ToPtr + NumBytes, |
| 1674 | strictConversion); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | |
Daniel Dunbar | 70ee975 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1676 | // ConvertUTF8toUTF16 returns the length in ToPtr. |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1677 | StringLength = ToPtr - &ToBuf[0]; |
Daniel Dunbar | 70ee975 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1678 | |
| 1679 | // Render the UTF-16 string into a byte array and convert to the target byte |
| 1680 | // order. |
| 1681 | // |
| 1682 | // FIXME: This isn't something we should need to do here. |
| 1683 | llvm::SmallString<128> AsBytes; |
| 1684 | AsBytes.reserve(StringLength * 2); |
| 1685 | for (unsigned i = 0; i != StringLength; ++i) { |
| 1686 | unsigned short Val = ToBuf[i]; |
| 1687 | if (TargetIsLSB) { |
| 1688 | AsBytes.push_back(Val & 0xFF); |
| 1689 | AsBytes.push_back(Val >> 8); |
| 1690 | } else { |
| 1691 | AsBytes.push_back(Val >> 8); |
| 1692 | AsBytes.push_back(Val & 0xFF); |
| 1693 | } |
| 1694 | } |
Daniel Dunbar | 434da48 | 2009-08-03 21:47:08 +0000 | [diff] [blame] | 1695 | // Append one extra null character, the second is automatically added by our |
| 1696 | // caller. |
| 1697 | AsBytes.push_back(0); |
Daniel Dunbar | 70ee975 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1698 | |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1699 | IsUTF16 = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1700 | return Map.GetOrCreateValue(StringRef(AsBytes.data(), AsBytes.size())); |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Fariborz Jahanian | cf8e168 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 1703 | static llvm::StringMapEntry<llvm::Constant*> & |
| 1704 | GetConstantStringEntry(llvm::StringMap<llvm::Constant*> &Map, |
| 1705 | const StringLiteral *Literal, |
| 1706 | unsigned &StringLength) |
| 1707 | { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1708 | StringRef String = Literal->getString(); |
Fariborz Jahanian | cf8e168 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 1709 | StringLength = String.size(); |
| 1710 | return Map.GetOrCreateValue(String); |
| 1711 | } |
| 1712 | |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1713 | llvm::Constant * |
| 1714 | CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { |
| 1715 | unsigned StringLength = 0; |
| 1716 | bool isUTF16 = false; |
| 1717 | llvm::StringMapEntry<llvm::Constant*> &Entry = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | GetConstantCFStringEntry(CFConstantStringMap, Literal, |
Daniel Dunbar | 70ee975 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1719 | getTargetData().isLittleEndian(), |
| 1720 | isUTF16, StringLength); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1721 | |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1722 | if (llvm::Constant *C = Entry.getValue()) |
| 1723 | return C; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1725 | llvm::Constant *Zero = |
| 1726 | llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1727 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1728 | |
Chris Lattner | 9d4a15f | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1729 | // If we don't already have it, get __CFConstantStringClassReference. |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1730 | if (!CFConstantStringClassRef) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1731 | llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1732 | Ty = llvm::ArrayType::get(Ty, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | llvm::Constant *GV = CreateRuntimeVariable(Ty, |
Chris Lattner | 9d4a15f | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1734 | "__CFConstantStringClassReference"); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1735 | // Decay array -> ptr |
| 1736 | CFConstantStringClassRef = |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1737 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1738 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1740 | QualType CFTy = getContext().getCFConstantStringType(); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1741 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1742 | llvm::StructType *STy = |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1743 | cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
| 1744 | |
Anders Carlsson | 5add683 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1745 | std::vector<llvm::Constant*> Fields(4); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1746 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1747 | // Class pointer. |
Anders Carlsson | 5add683 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1748 | Fields[0] = CFConstantStringClassRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1750 | // Flags. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1751 | llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) : |
Anders Carlsson | 5add683 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1753 | llvm::ConstantInt::get(Ty, 0x07C8); |
| 1754 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1755 | // String pointer. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1756 | llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1757 | |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1758 | llvm::GlobalValue::LinkageTypes Linkage; |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1759 | bool isConstant; |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1760 | if (isUTF16) { |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1761 | // FIXME: why do utf strings get "_" labels instead of "L" labels? |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1762 | Linkage = llvm::GlobalValue::InternalLinkage; |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1763 | // Note: -fwritable-strings doesn't make unicode CFStrings writable, but |
| 1764 | // does make plain ascii ones writable. |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1765 | isConstant = true; |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1766 | } else { |
Rafael Espindola | 584acf2 | 2011-03-14 17:55:00 +0000 | [diff] [blame] | 1767 | // FIXME: With OS X ld 123.2 (xcode 4) and LTO we would get a linker error |
| 1768 | // when using private linkage. It is not clear if this is a bug in ld |
| 1769 | // or a reasonable new restriction. |
Rafael Espindola | dc0f137 | 2011-03-14 21:08:19 +0000 | [diff] [blame] | 1770 | Linkage = llvm::GlobalValue::LinkerPrivateLinkage; |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1771 | isConstant = !Features.WritableStrings; |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1772 | } |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1773 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | llvm::GlobalVariable *GV = |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1775 | new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, |
| 1776 | ".str"); |
Rafael Espindola | b266a1f | 2011-01-17 16:31:00 +0000 | [diff] [blame] | 1777 | GV->setUnnamedAddr(true); |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1778 | if (isUTF16) { |
Ken Dyck | 4da244c | 2010-01-26 18:46:23 +0000 | [diff] [blame] | 1779 | CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); |
| 1780 | GV->setAlignment(Align.getQuantity()); |
Daniel Dunbar | f7e903d | 2011-04-12 23:30:52 +0000 | [diff] [blame] | 1781 | } else { |
| 1782 | CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy); |
| 1783 | GV->setAlignment(Align.getQuantity()); |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1784 | } |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1785 | Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
Anders Carlsson | 5add683 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1786 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1787 | // String length. |
| 1788 | Ty = getTypes().ConvertType(getContext().LongTy); |
Anders Carlsson | 5add683 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1789 | Fields[3] = llvm::ConstantInt::get(Ty, StringLength); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1790 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1791 | // The struct. |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1792 | C = llvm::ConstantStruct::get(STy, Fields); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | GV = new llvm::GlobalVariable(getModule(), C->getType(), true, |
| 1794 | llvm::GlobalVariable::PrivateLinkage, C, |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1795 | "_unnamed_cfstring_"); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1796 | if (const char *Sect = getContext().getTargetInfo().getCFStringSection()) |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1797 | GV->setSection(Sect); |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1798 | Entry.setValue(GV); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1800 | return GV; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1801 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1802 | |
Douglas Gregor | 45c4ea7 | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 1803 | static RecordDecl * |
| 1804 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 1805 | DeclContext *DC, IdentifierInfo *Id) { |
| 1806 | SourceLocation Loc; |
| 1807 | if (Ctx.getLangOptions().CPlusPlus) |
| 1808 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
| 1809 | else |
| 1810 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
| 1811 | } |
| 1812 | |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1813 | llvm::Constant * |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1814 | CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) { |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1815 | unsigned StringLength = 0; |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1816 | llvm::StringMapEntry<llvm::Constant*> &Entry = |
Fariborz Jahanian | cf8e168 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 1817 | GetConstantStringEntry(CFConstantStringMap, Literal, StringLength); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1818 | |
| 1819 | if (llvm::Constant *C = Entry.getValue()) |
| 1820 | return C; |
| 1821 | |
| 1822 | llvm::Constant *Zero = |
| 1823 | llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); |
| 1824 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 1825 | |
Fariborz Jahanian | ec951e0 | 2010-04-23 22:33:39 +0000 | [diff] [blame] | 1826 | // If we don't already have it, get _NSConstantStringClassReference. |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1827 | if (!ConstantStringClassRef) { |
| 1828 | std::string StringClass(getLangOptions().ObjCConstantStringClass); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1829 | llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1830 | llvm::Constant *GV; |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1831 | if (Features.ObjCNonFragileABI) { |
Fariborz Jahanian | 25dba5d | 2011-05-17 22:46:11 +0000 | [diff] [blame] | 1832 | std::string str = |
| 1833 | StringClass.empty() ? "OBJC_CLASS_$_NSConstantString" |
| 1834 | : "OBJC_CLASS_$_" + StringClass; |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1835 | GV = getObjCRuntime().GetClassGlobal(str); |
| 1836 | // Make sure the result is of the correct type. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1837 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1838 | ConstantStringClassRef = |
| 1839 | llvm::ConstantExpr::getBitCast(GV, PTy); |
| 1840 | } else { |
Fariborz Jahanian | 25dba5d | 2011-05-17 22:46:11 +0000 | [diff] [blame] | 1841 | std::string str = |
| 1842 | StringClass.empty() ? "_NSConstantStringClassReference" |
| 1843 | : "_" + StringClass + "ClassReference"; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1844 | llvm::Type *PTy = llvm::ArrayType::get(Ty, 0); |
Fariborz Jahanian | 25dba5d | 2011-05-17 22:46:11 +0000 | [diff] [blame] | 1845 | GV = CreateRuntimeVariable(PTy, str); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1846 | // Decay array -> ptr |
| 1847 | ConstantStringClassRef = |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1848 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1849 | } |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1850 | } |
Douglas Gregor | 45c4ea7 | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 1851 | |
| 1852 | if (!NSConstantStringType) { |
| 1853 | // Construct the type for a constant NSString. |
| 1854 | RecordDecl *D = CreateRecordDecl(Context, TTK_Struct, |
| 1855 | Context.getTranslationUnitDecl(), |
| 1856 | &Context.Idents.get("__builtin_NSString")); |
| 1857 | D->startDefinition(); |
| 1858 | |
| 1859 | QualType FieldTypes[3]; |
| 1860 | |
| 1861 | // const int *isa; |
| 1862 | FieldTypes[0] = Context.getPointerType(Context.IntTy.withConst()); |
| 1863 | // const char *str; |
| 1864 | FieldTypes[1] = Context.getPointerType(Context.CharTy.withConst()); |
| 1865 | // unsigned int length; |
| 1866 | FieldTypes[2] = Context.UnsignedIntTy; |
| 1867 | |
| 1868 | // Create fields |
| 1869 | for (unsigned i = 0; i < 3; ++i) { |
| 1870 | FieldDecl *Field = FieldDecl::Create(Context, D, |
| 1871 | SourceLocation(), |
| 1872 | SourceLocation(), 0, |
| 1873 | FieldTypes[i], /*TInfo=*/0, |
| 1874 | /*BitWidth=*/0, |
| 1875 | /*Mutable=*/false, |
| 1876 | /*HasInit=*/false); |
| 1877 | Field->setAccess(AS_public); |
| 1878 | D->addDecl(Field); |
| 1879 | } |
| 1880 | |
| 1881 | D->completeDefinition(); |
| 1882 | QualType NSTy = Context.getTagDeclType(D); |
| 1883 | NSConstantStringType = cast<llvm::StructType>(getTypes().ConvertType(NSTy)); |
| 1884 | } |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1885 | |
| 1886 | std::vector<llvm::Constant*> Fields(3); |
| 1887 | |
| 1888 | // Class pointer. |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1889 | Fields[0] = ConstantStringClassRef; |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1890 | |
| 1891 | // String pointer. |
| 1892 | llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); |
| 1893 | |
| 1894 | llvm::GlobalValue::LinkageTypes Linkage; |
| 1895 | bool isConstant; |
Fariborz Jahanian | cf8e168 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 1896 | Linkage = llvm::GlobalValue::PrivateLinkage; |
| 1897 | isConstant = !Features.WritableStrings; |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1898 | |
| 1899 | llvm::GlobalVariable *GV = |
| 1900 | new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, |
| 1901 | ".str"); |
Rafael Espindola | 803d307 | 2011-01-17 22:11:21 +0000 | [diff] [blame] | 1902 | GV->setUnnamedAddr(true); |
Fariborz Jahanian | cf8e168 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 1903 | CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy); |
| 1904 | GV->setAlignment(Align.getQuantity()); |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1905 | Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1906 | |
| 1907 | // String length. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1908 | llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1909 | Fields[2] = llvm::ConstantInt::get(Ty, StringLength); |
| 1910 | |
| 1911 | // The struct. |
Douglas Gregor | 45c4ea7 | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 1912 | C = llvm::ConstantStruct::get(NSConstantStringType, Fields); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1913 | GV = new llvm::GlobalVariable(getModule(), C->getType(), true, |
| 1914 | llvm::GlobalVariable::PrivateLinkage, C, |
| 1915 | "_unnamed_nsstring_"); |
| 1916 | // FIXME. Fix section. |
Fariborz Jahanian | ec951e0 | 2010-04-23 22:33:39 +0000 | [diff] [blame] | 1917 | if (const char *Sect = |
| 1918 | Features.ObjCNonFragileABI |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1919 | ? getContext().getTargetInfo().getNSStringNonFragileABISection() |
| 1920 | : getContext().getTargetInfo().getNSStringSection()) |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1921 | GV->setSection(Sect); |
| 1922 | Entry.setValue(GV); |
| 1923 | |
| 1924 | return GV; |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Douglas Gregor | 0815b57 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 1927 | QualType CodeGenModule::getObjCFastEnumerationStateType() { |
| 1928 | if (ObjCFastEnumerationStateType.isNull()) { |
| 1929 | RecordDecl *D = CreateRecordDecl(Context, TTK_Struct, |
| 1930 | Context.getTranslationUnitDecl(), |
| 1931 | &Context.Idents.get("__objcFastEnumerationState")); |
| 1932 | D->startDefinition(); |
| 1933 | |
| 1934 | QualType FieldTypes[] = { |
| 1935 | Context.UnsignedLongTy, |
| 1936 | Context.getPointerType(Context.getObjCIdType()), |
| 1937 | Context.getPointerType(Context.UnsignedLongTy), |
| 1938 | Context.getConstantArrayType(Context.UnsignedLongTy, |
| 1939 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 1940 | }; |
| 1941 | |
| 1942 | for (size_t i = 0; i < 4; ++i) { |
| 1943 | FieldDecl *Field = FieldDecl::Create(Context, |
| 1944 | D, |
| 1945 | SourceLocation(), |
| 1946 | SourceLocation(), 0, |
| 1947 | FieldTypes[i], /*TInfo=*/0, |
| 1948 | /*BitWidth=*/0, |
| 1949 | /*Mutable=*/false, |
| 1950 | /*HasInit=*/false); |
| 1951 | Field->setAccess(AS_public); |
| 1952 | D->addDecl(Field); |
| 1953 | } |
| 1954 | |
| 1955 | D->completeDefinition(); |
| 1956 | ObjCFastEnumerationStateType = Context.getTagDeclType(D); |
| 1957 | } |
| 1958 | |
| 1959 | return ObjCFastEnumerationStateType; |
| 1960 | } |
| 1961 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1962 | /// GetStringForStringLiteral - Return the appropriate bytes for a |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1963 | /// string literal, properly padded to match the literal type. |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1964 | std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { |
Ken Dyck | 3b8037a | 2011-01-29 17:53:12 +0000 | [diff] [blame] | 1965 | const ASTContext &Context = getContext(); |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1966 | const ConstantArrayType *CAT = |
Ken Dyck | 3b8037a | 2011-01-29 17:53:12 +0000 | [diff] [blame] | 1967 | Context.getAsConstantArrayType(E->getType()); |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1968 | assert(CAT && "String isn't pointer or array!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1970 | // Resize the string to the right size. |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1971 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1972 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1973 | switch (E->getKind()) { |
| 1974 | case StringLiteral::Ascii: |
| 1975 | case StringLiteral::UTF8: |
| 1976 | break; |
| 1977 | case StringLiteral::Wide: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1978 | RealLen *= Context.getTargetInfo().getWCharWidth() / Context.getCharWidth(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1979 | break; |
| 1980 | case StringLiteral::UTF16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1981 | RealLen *= Context.getTargetInfo().getChar16Width() / Context.getCharWidth(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1982 | break; |
| 1983 | case StringLiteral::UTF32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1984 | RealLen *= Context.getTargetInfo().getChar32Width() / Context.getCharWidth(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1985 | break; |
| 1986 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1988 | std::string Str = E->getString().str(); |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1989 | Str.resize(RealLen, '\0'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1991 | return Str; |
| 1992 | } |
| 1993 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1994 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 1995 | /// constant array for the given string literal. |
| 1996 | llvm::Constant * |
| 1997 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { |
| 1998 | // FIXME: This can be more efficient. |
Eli Friedman | 7eb79c1 | 2009-11-16 05:55:46 +0000 | [diff] [blame] | 1999 | // FIXME: We shouldn't need to bitcast the constant in the wide string case. |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2000 | CharUnits Align = getContext().getTypeAlignInChars(S->getType()); |
| 2001 | llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S), |
| 2002 | /* GlobalName */ 0, |
| 2003 | Align.getQuantity()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2004 | if (S->isWide() || S->isUTF16() || S->isUTF32()) { |
Eli Friedman | 7eb79c1 | 2009-11-16 05:55:46 +0000 | [diff] [blame] | 2005 | llvm::Type *DestTy = |
| 2006 | llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType())); |
| 2007 | C = llvm::ConstantExpr::getBitCast(C, DestTy); |
| 2008 | } |
| 2009 | return C; |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2012 | /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant |
| 2013 | /// array for the given ObjCEncodeExpr node. |
| 2014 | llvm::Constant * |
| 2015 | CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { |
| 2016 | std::string Str; |
| 2017 | getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
Eli Friedman | a210f35 | 2009-03-07 20:17:55 +0000 | [diff] [blame] | 2018 | |
| 2019 | return GetAddrOfConstantCString(Str); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 2023 | /// GenerateWritableString -- Creates storage for a string literal. |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2024 | static llvm::GlobalVariable *GenerateStringLiteral(StringRef str, |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2025 | bool constant, |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 2026 | CodeGenModule &CGM, |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2027 | const char *GlobalName, |
| 2028 | unsigned Alignment) { |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2029 | // Create Constant for this string literal. Don't add a '\0'. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2030 | llvm::Constant *C = |
| 2031 | llvm::ConstantArray::get(CGM.getLLVMContext(), str, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2033 | // Create a global variable for this string |
Rafael Espindola | 1257bc6 | 2011-01-10 22:34:03 +0000 | [diff] [blame] | 2034 | llvm::GlobalVariable *GV = |
| 2035 | new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, |
| 2036 | llvm::GlobalValue::PrivateLinkage, |
| 2037 | C, GlobalName); |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2038 | GV->setAlignment(Alignment); |
Rafael Espindola | 1257bc6 | 2011-01-10 22:34:03 +0000 | [diff] [blame] | 2039 | GV->setUnnamedAddr(true); |
| 2040 | return GV; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2043 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 2044 | /// containing the literal. This contents are exactly that of the |
| 2045 | /// given string, i.e. it will not be null terminated automatically; |
| 2046 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 2047 | /// actually a pointer to an LLVM constant depends on |
| 2048 | /// Feature.WriteableStrings. |
| 2049 | /// |
| 2050 | /// The result has pointer to array type. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2051 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(StringRef Str, |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2052 | const char *GlobalName, |
| 2053 | unsigned Alignment) { |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 2054 | bool IsConstant = !Features.WritableStrings; |
| 2055 | |
| 2056 | // Get the default prefix if a name wasn't specified. |
| 2057 | if (!GlobalName) |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 2058 | GlobalName = ".str"; |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 2059 | |
| 2060 | // Don't share any string literals if strings aren't constant. |
| 2061 | if (!IsConstant) |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2062 | return GenerateStringLiteral(Str, false, *this, GlobalName, Alignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2063 | |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2064 | llvm::StringMapEntry<llvm::GlobalVariable *> &Entry = |
Benjamin Kramer | 9de4342 | 2011-03-05 13:45:23 +0000 | [diff] [blame] | 2065 | ConstantStringMap.GetOrCreateValue(Str); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2066 | |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2067 | if (llvm::GlobalVariable *GV = Entry.getValue()) { |
| 2068 | if (Alignment > GV->getAlignment()) { |
| 2069 | GV->setAlignment(Alignment); |
| 2070 | } |
| 2071 | return GV; |
| 2072 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2073 | |
| 2074 | // Create a global variable for this. |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2075 | llvm::GlobalVariable *GV = GenerateStringLiteral(Str, true, *this, GlobalName, Alignment); |
| 2076 | Entry.setValue(GV); |
| 2077 | return GV; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2078 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2079 | |
| 2080 | /// GetAddrOfConstantCString - Returns a pointer to a character |
Benjamin Kramer | 9de4342 | 2011-03-05 13:45:23 +0000 | [diff] [blame] | 2081 | /// array containing the literal and a terminating '\0' |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2082 | /// character. The result has pointer to array type. |
Benjamin Kramer | 9de4342 | 2011-03-05 13:45:23 +0000 | [diff] [blame] | 2083 | llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &Str, |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2084 | const char *GlobalName, |
| 2085 | unsigned Alignment) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2086 | StringRef StrWithNull(Str.c_str(), Str.size() + 1); |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2087 | return GetAddrOfConstantString(StrWithNull, GlobalName, Alignment); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2088 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2089 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2090 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 2091 | /// properties for an implementation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | void CodeGenModule::EmitObjCPropertyImplementations(const |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2093 | ObjCImplementationDecl *D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2095 | i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2096 | ObjCPropertyImplDecl *PID = *i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2098 | // Dynamic is just for type-checking. |
| 2099 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2100 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2101 | |
| 2102 | // Determine which methods need to be implemented, some may have |
| 2103 | // been overridden. Note that ::isSynthesized is not the method |
| 2104 | // we want, that just indicates if the decl came from a |
| 2105 | // property. What we want to know is if the method is defined in |
| 2106 | // this implementation. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2107 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 2108 | CodeGenFunction(*this).GenerateObjCGetter( |
| 2109 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2110 | if (!PD->isReadOnly() && |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2111 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 2112 | CodeGenFunction(*this).GenerateObjCSetter( |
| 2113 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | } |
| 2117 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2118 | static bool needsDestructMethod(ObjCImplementationDecl *impl) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2119 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
| 2120 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2121 | ivar; ivar = ivar->getNextIvar()) |
| 2122 | if (ivar->getType().isDestructedType()) |
| 2123 | return true; |
| 2124 | |
| 2125 | return false; |
| 2126 | } |
| 2127 | |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2128 | /// EmitObjCIvarInitializations - Emit information for ivar initialization |
| 2129 | /// for an implementation. |
| 2130 | void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2131 | // We might need a .cxx_destruct even if we don't have any ivar initializers. |
| 2132 | if (needsDestructMethod(D)) { |
| 2133 | IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); |
| 2134 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
| 2135 | ObjCMethodDecl *DTORMethod = |
| 2136 | ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(), |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 2137 | ArrayRef<SourceLocation>(), |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2138 | cxxSelector, getContext().VoidTy, 0, D, |
| 2139 | /*isInstance=*/true, /*isVariadic=*/false, |
| 2140 | /*isSynthesized=*/true, /*isImplicitlyDeclared=*/true, |
| 2141 | /*isDefined=*/false, ObjCMethodDecl::Required); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2142 | D->addInstanceMethod(DTORMethod); |
| 2143 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2144 | D->setHasCXXStructors(true); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | // If the implementation doesn't have any ivar initializers, we don't need |
| 2148 | // a .cxx_construct. |
David Chisnall | 827bbcc | 2011-03-17 14:19:08 +0000 | [diff] [blame] | 2149 | if (D->getNumIvarInitializers() == 0) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2150 | return; |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2151 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2152 | IdentifierInfo *II = &getContext().Idents.get(".cxx_construct"); |
| 2153 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2154 | // The constructor returns 'self'. |
| 2155 | ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), |
| 2156 | D->getLocation(), |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 2157 | D->getLocation(), |
| 2158 | ArrayRef<SourceLocation>(), |
| 2159 | cxxSelector, |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2160 | getContext().getObjCIdType(), 0, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2161 | D, /*isInstance=*/true, |
| 2162 | /*isVariadic=*/false, |
| 2163 | /*isSynthesized=*/true, |
| 2164 | /*isImplicitlyDeclared=*/true, |
| 2165 | /*isDefined=*/false, |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2166 | ObjCMethodDecl::Required); |
| 2167 | D->addInstanceMethod(CTORMethod); |
| 2168 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2169 | D->setHasCXXStructors(true); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2172 | /// EmitNamespace - Emit all declarations in a namespace. |
Anders Carlsson | 984e068 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 2173 | void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2174 | for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end(); |
Anders Carlsson | 984e068 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 2175 | I != E; ++I) |
| 2176 | EmitTopLevelDecl(*I); |
| 2177 | } |
| 2178 | |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2179 | // EmitLinkageSpec - Emit all declarations in a linkage spec. |
| 2180 | void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { |
Eli Friedman | f976be8 | 2009-08-01 20:48:04 +0000 | [diff] [blame] | 2181 | if (LSD->getLanguage() != LinkageSpecDecl::lang_c && |
| 2182 | LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2183 | ErrorUnsupported(LSD, "linkage spec"); |
| 2184 | return; |
| 2185 | } |
| 2186 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2187 | for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end(); |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2188 | I != E; ++I) |
| 2189 | EmitTopLevelDecl(*I); |
| 2190 | } |
| 2191 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2192 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 2193 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
| 2194 | // If an error has occurred, stop code generation, but continue |
| 2195 | // parsing and semantic analysis (to ensure all warnings and errors |
| 2196 | // are emitted). |
| 2197 | if (Diags.hasErrorOccurred()) |
| 2198 | return; |
| 2199 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2200 | // Ignore dependent declarations. |
| 2201 | if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) |
| 2202 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2204 | switch (D->getKind()) { |
Anders Carlsson | 293361a | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 2205 | case Decl::CXXConversion: |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 2206 | case Decl::CXXMethod: |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2207 | case Decl::Function: |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2208 | // Skip function templates |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2209 | if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() || |
| 2210 | cast<FunctionDecl>(D)->isLateTemplateParsed()) |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2211 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 2213 | EmitGlobal(cast<FunctionDecl>(D)); |
| 2214 | break; |
| 2215 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2216 | case Decl::Var: |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 2217 | EmitGlobal(cast<VarDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2218 | break; |
| 2219 | |
John McCall | 26fbc72 | 2011-04-12 01:01:22 +0000 | [diff] [blame] | 2220 | // Indirect fields from global anonymous structs and unions can be |
| 2221 | // ignored; only the actual variable requires IR gen support. |
| 2222 | case Decl::IndirectField: |
| 2223 | break; |
| 2224 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 2225 | // C++ Decls |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2226 | case Decl::Namespace: |
Anders Carlsson | 984e068 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 2227 | EmitNamespace(cast<NamespaceDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2228 | break; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2229 | // No code generation needed. |
John McCall | 9d0c661 | 2009-11-17 09:33:40 +0000 | [diff] [blame] | 2230 | case Decl::UsingShadow: |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2231 | case Decl::Using: |
Douglas Gregor | dd9967a | 2009-09-02 23:49:23 +0000 | [diff] [blame] | 2232 | case Decl::UsingDirective: |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 2233 | case Decl::ClassTemplate: |
| 2234 | case Decl::FunctionTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2235 | case Decl::TypeAliasTemplate: |
Anders Carlsson | 018837b | 2009-09-23 19:19:16 +0000 | [diff] [blame] | 2236 | case Decl::NamespaceAlias: |
Douglas Gregor | 6a576ab | 2011-06-05 05:04:23 +0000 | [diff] [blame] | 2237 | case Decl::Block: |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2238 | break; |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 2239 | case Decl::CXXConstructor: |
Anders Carlsson | 1fe598c | 2009-11-24 05:16:24 +0000 | [diff] [blame] | 2240 | // Skip function templates |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2241 | if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() || |
| 2242 | cast<FunctionDecl>(D)->isLateTemplateParsed()) |
Anders Carlsson | 1fe598c | 2009-11-24 05:16:24 +0000 | [diff] [blame] | 2243 | return; |
| 2244 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 2245 | EmitCXXConstructors(cast<CXXConstructorDecl>(D)); |
| 2246 | break; |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 2247 | case Decl::CXXDestructor: |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2248 | if (cast<FunctionDecl>(D)->isLateTemplateParsed()) |
| 2249 | return; |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 2250 | EmitCXXDestructors(cast<CXXDestructorDecl>(D)); |
| 2251 | break; |
Anders Carlsson | 36674d2 | 2009-06-11 21:22:55 +0000 | [diff] [blame] | 2252 | |
| 2253 | case Decl::StaticAssert: |
| 2254 | // Nothing to do. |
| 2255 | break; |
| 2256 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 2257 | // Objective-C Decls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2258 | |
Fariborz Jahanian | 38e24c7 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 2259 | // Forward declarations, no (immediate) code generation. |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2260 | case Decl::ObjCClass: |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2261 | case Decl::ObjCForwardProtocol: |
Chris Lattner | 285d0db | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 2262 | case Decl::ObjCInterface: |
Chris Lattner | 285d0db | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 2263 | break; |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 2264 | |
Chris Lattner | baf101d | 2011-04-09 07:11:53 +0000 | [diff] [blame] | 2265 | case Decl::ObjCCategory: { |
| 2266 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
| 2267 | if (CD->IsClassExtension() && CD->hasSynthBitfield()) |
| 2268 | Context.ResetObjCLayout(CD->getClassInterface()); |
| 2269 | break; |
| 2270 | } |
Chris Lattner | 285d0db | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 2271 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2272 | case Decl::ObjCProtocol: |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 2273 | ObjCRuntime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2274 | break; |
| 2275 | |
| 2276 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2277 | // Categories have properties but don't support synthesize so we |
| 2278 | // can ignore them here. |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 2279 | ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2280 | break; |
| 2281 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2282 | case Decl::ObjCImplementation: { |
| 2283 | ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 2284 | if (Features.ObjCNonFragileABI2 && OMD->hasSynthBitfield()) |
| 2285 | Context.ResetObjCLayout(OMD->getClassInterface()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2286 | EmitObjCPropertyImplementations(OMD); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2287 | EmitObjCIvarInitializations(OMD); |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 2288 | ObjCRuntime->GenerateClass(OMD); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2289 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2291 | case Decl::ObjCMethod: { |
| 2292 | ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); |
| 2293 | // If this is not a prototype, emit the body. |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2294 | if (OMD->getBody()) |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2295 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 2296 | break; |
| 2297 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2298 | case Decl::ObjCCompatibleAlias: |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 2299 | // compatibility-alias is a directive and has no code gen. |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2300 | break; |
| 2301 | |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2302 | case Decl::LinkageSpec: |
| 2303 | EmitLinkageSpec(cast<LinkageSpecDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2304 | break; |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2305 | |
| 2306 | case Decl::FileScopeAsm: { |
| 2307 | FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2308 | StringRef AsmString = AD->getAsmString()->getString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2309 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2310 | const std::string &S = getModule().getModuleInlineAsm(); |
| 2311 | if (S.empty()) |
| 2312 | getModule().setModuleInlineAsm(AsmString); |
Chris Lattner | 9f5bff0 | 2011-07-23 20:04:25 +0000 | [diff] [blame] | 2313 | else if (*--S.end() == '\n') |
| 2314 | getModule().setModuleInlineAsm(S + AsmString.str()); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2315 | else |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 2316 | getModule().setModuleInlineAsm(S + '\n' + AsmString.str()); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2317 | break; |
| 2318 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2319 | |
| 2320 | default: |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2321 | // Make sure we handled everything we should, every other kind is a |
| 2322 | // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind |
| 2323 | // function. Need to recode Decl::Kind to do that easily. |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2324 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
| 2325 | } |
| 2326 | } |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2327 | |
| 2328 | /// Turns the given pointer into a constant. |
| 2329 | static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, |
| 2330 | const void *Ptr) { |
| 2331 | uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2332 | llvm::Type *i64 = llvm::Type::getInt64Ty(Context); |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2333 | return llvm::ConstantInt::get(i64, PtrInt); |
| 2334 | } |
| 2335 | |
| 2336 | static void EmitGlobalDeclMetadata(CodeGenModule &CGM, |
| 2337 | llvm::NamedMDNode *&GlobalMetadata, |
| 2338 | GlobalDecl D, |
| 2339 | llvm::GlobalValue *Addr) { |
| 2340 | if (!GlobalMetadata) |
| 2341 | GlobalMetadata = |
| 2342 | CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); |
| 2343 | |
| 2344 | // TODO: should we report variant information for ctors/dtors? |
| 2345 | llvm::Value *Ops[] = { |
| 2346 | Addr, |
| 2347 | GetPointerConstant(CGM.getLLVMContext(), D.getDecl()) |
| 2348 | }; |
Jay Foad | 6f14165 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 2349 | GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | /// Emits metadata nodes associating all the global values in the |
| 2353 | /// current module with the Decls they came from. This is useful for |
| 2354 | /// projects using IR gen as a subroutine. |
| 2355 | /// |
| 2356 | /// Since there's currently no way to associate an MDNode directly |
| 2357 | /// with an llvm::GlobalValue, we create a global named metadata |
| 2358 | /// with the name 'clang.global.decl.ptrs'. |
| 2359 | void CodeGenModule::EmitDeclMetadata() { |
| 2360 | llvm::NamedMDNode *GlobalMetadata = 0; |
| 2361 | |
| 2362 | // StaticLocalDeclMap |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2363 | for (llvm::DenseMap<GlobalDecl,StringRef>::iterator |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2364 | I = MangledDeclNames.begin(), E = MangledDeclNames.end(); |
| 2365 | I != E; ++I) { |
| 2366 | llvm::GlobalValue *Addr = getModule().getNamedValue(I->second); |
| 2367 | EmitGlobalDeclMetadata(*this, GlobalMetadata, I->first, Addr); |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | /// Emits metadata nodes for all the local variables in the current |
| 2372 | /// function. |
| 2373 | void CodeGenFunction::EmitDeclMetadata() { |
| 2374 | if (LocalDeclMap.empty()) return; |
| 2375 | |
| 2376 | llvm::LLVMContext &Context = getLLVMContext(); |
| 2377 | |
| 2378 | // Find the unique metadata ID for this name. |
| 2379 | unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); |
| 2380 | |
| 2381 | llvm::NamedMDNode *GlobalMetadata = 0; |
| 2382 | |
| 2383 | for (llvm::DenseMap<const Decl*, llvm::Value*>::iterator |
| 2384 | I = LocalDeclMap.begin(), E = LocalDeclMap.end(); I != E; ++I) { |
| 2385 | const Decl *D = I->first; |
| 2386 | llvm::Value *Addr = I->second; |
| 2387 | |
| 2388 | if (llvm::AllocaInst *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { |
| 2389 | llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); |
Jay Foad | 6f14165 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 2390 | Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, DAddr)); |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2391 | } else if (llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(Addr)) { |
| 2392 | GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); |
| 2393 | EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); |
| 2394 | } |
| 2395 | } |
| 2396 | } |
Daniel Dunbar | 673431a | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 2397 | |
Nick Lewycky | 3dc0541 | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 2398 | void CodeGenModule::EmitCoverageFile() { |
| 2399 | if (!getCodeGenOpts().CoverageFile.empty()) { |
Nick Lewycky | 5ea4f44 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 2400 | if (llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu")) { |
| 2401 | llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov"); |
| 2402 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
Nick Lewycky | 3dc0541 | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 2403 | llvm::MDString *CoverageFile = |
| 2404 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageFile); |
Nick Lewycky | 5ea4f44 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 2405 | for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) { |
| 2406 | llvm::MDNode *CU = CUNode->getOperand(i); |
Nick Lewycky | 3dc0541 | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 2407 | llvm::Value *node[] = { CoverageFile, CU }; |
Nick Lewycky | 5ea4f44 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 2408 | llvm::MDNode *N = llvm::MDNode::get(Ctx, node); |
| 2409 | GCov->addOperand(N); |
| 2410 | } |
| 2411 | } |
| 2412 | } |
| 2413 | } |