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