Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 1 | //===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with code generation of C++ declarations |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 15 | #include "clang/CodeGen/CodeGenOptions.h" |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 16 | using namespace clang; |
| 17 | using namespace CodeGen; |
| 18 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 19 | static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, |
| 20 | llvm::Constant *DeclPtr) { |
| 21 | assert(D.hasGlobalStorage() && "VarDecl must have global storage!"); |
| 22 | assert(!D.getType()->isReferenceType() && |
| 23 | "Should not call EmitDeclInit on a reference!"); |
| 24 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 25 | ASTContext &Context = CGF.getContext(); |
| 26 | |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 27 | const Expr *Init = D.getInit(); |
| 28 | QualType T = D.getType(); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 29 | bool isVolatile = Context.getCanonicalType(T).isVolatileQualified(); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 30 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 31 | if (!CGF.hasAggregateLLVMType(T)) { |
| 32 | llvm::Value *V = CGF.EmitScalarExpr(Init); |
| 33 | CGF.EmitStoreOfScalar(V, DeclPtr, isVolatile, T); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 34 | } else if (T->isAnyComplexType()) { |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 35 | CGF.EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 36 | } else { |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 37 | CGF.EmitAggExpr(Init, DeclPtr, isVolatile); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 41 | static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, |
| 42 | llvm::Constant *DeclPtr) { |
| 43 | CodeGenModule &CGM = CGF.CGM; |
| 44 | ASTContext &Context = CGF.getContext(); |
| 45 | |
| 46 | const Expr *Init = D.getInit(); |
| 47 | QualType T = D.getType(); |
| 48 | if (!CGF.hasAggregateLLVMType(T) || T->isAnyComplexType()) |
| 49 | return; |
| 50 | |
| 51 | // Avoid generating destructor(s) for initialized objects. |
| 52 | if (!isa<CXXConstructExpr>(Init)) |
| 53 | return; |
| 54 | |
| 55 | const ConstantArrayType *Array = Context.getAsConstantArrayType(T); |
| 56 | if (Array) |
| 57 | T = Context.getBaseElementType(Array); |
| 58 | |
| 59 | const RecordType *RT = T->getAs<RecordType>(); |
| 60 | if (!RT) |
| 61 | return; |
| 62 | |
| 63 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 64 | if (RD->hasTrivialDestructor()) |
| 65 | return; |
| 66 | |
| 67 | CXXDestructorDecl *Dtor = RD->getDestructor(Context); |
| 68 | |
| 69 | llvm::Constant *DtorFn; |
| 70 | if (Array) { |
| 71 | DtorFn = |
| 72 | CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(Dtor, |
| 73 | Array, |
| 74 | DeclPtr); |
| 75 | const llvm::Type *Int8PtrTy = |
| 76 | llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 77 | DeclPtr = llvm::Constant::getNullValue(Int8PtrTy); |
| 78 | } else |
| 79 | DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete); |
| 80 | |
| 81 | CGF.EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr); |
| 82 | } |
| 83 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 84 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
| 85 | llvm::Constant *DeclPtr) { |
| 86 | |
| 87 | const Expr *Init = D.getInit(); |
| 88 | QualType T = D.getType(); |
| 89 | |
| 90 | if (!T->isReferenceType()) { |
| 91 | EmitDeclInit(*this, D, DeclPtr); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 92 | EmitDeclDestroy(*this, D, DeclPtr); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 93 | return; |
| 94 | } |
Fariborz Jahanian | 616c173 | 2010-01-25 21:40:39 +0000 | [diff] [blame] | 95 | if (Init->isLvalue(getContext()) == Expr::LV_Valid) { |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 96 | RValue RV = EmitReferenceBindingToExpr(Init, /*IsInitializer=*/true); |
Fariborz Jahanian | 616c173 | 2010-01-25 21:40:39 +0000 | [diff] [blame] | 97 | EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, T); |
| 98 | return; |
| 99 | } |
| 100 | ErrorUnsupported(Init, |
| 101 | "global variable that binds reference to a non-lvalue"); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 102 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 103 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 104 | void |
| 105 | CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn, |
| 106 | llvm::Constant *DeclPtr) { |
| 107 | // Generate a global destructor entry if not using __cxa_atexit. |
| 108 | if (!CGM.getCodeGenOpts().CXAAtExit) { |
| 109 | CGM.AddCXXDtorEntry(DtorFn, DeclPtr); |
| 110 | return; |
| 111 | } |
| 112 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 113 | const llvm::Type *Int8PtrTy = |
| 114 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 115 | |
| 116 | std::vector<const llvm::Type *> Params; |
| 117 | Params.push_back(Int8PtrTy); |
| 118 | |
| 119 | // Get the destructor function type |
| 120 | const llvm::Type *DtorFnTy = |
| 121 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); |
| 122 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); |
| 123 | |
| 124 | Params.clear(); |
| 125 | Params.push_back(DtorFnTy); |
| 126 | Params.push_back(Int8PtrTy); |
| 127 | Params.push_back(Int8PtrTy); |
| 128 | |
| 129 | // Get the __cxa_atexit function type |
| 130 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
| 131 | const llvm::FunctionType *AtExitFnTy = |
| 132 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); |
| 133 | |
| 134 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, |
| 135 | "__cxa_atexit"); |
| 136 | |
| 137 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, |
| 138 | "__dso_handle"); |
| 139 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), |
| 140 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), |
| 141 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; |
| 142 | Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); |
| 143 | } |
| 144 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 145 | void |
| 146 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D) { |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 147 | const llvm::FunctionType *FTy |
| 148 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 149 | false); |
| 150 | |
| 151 | // Create a variable initialization function. |
| 152 | llvm::Function *Fn = |
| 153 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 154 | "__cxx_global_var_init", &TheModule); |
| 155 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 156 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 157 | |
| 158 | CXXGlobalInits.push_back(Fn); |
| 159 | } |
| 160 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 161 | void |
| 162 | CodeGenModule::EmitCXXGlobalInitFunc() { |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 163 | if (CXXGlobalInits.empty()) |
| 164 | return; |
| 165 | |
| 166 | const llvm::FunctionType *FTy |
| 167 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 168 | false); |
| 169 | |
| 170 | // Create our global initialization function. |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 171 | llvm::Function *Fn = |
| 172 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 173 | "_GLOBAL__I_a", &TheModule); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 174 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 175 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 176 | &CXXGlobalInits[0], |
| 177 | CXXGlobalInits.size()); |
| 178 | AddGlobalCtor(Fn); |
| 179 | } |
| 180 | |
| 181 | void CodeGenModule::AddCXXDtorEntry(llvm::Constant *DtorFn, |
| 182 | llvm::Constant *Object) { |
| 183 | CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object)); |
| 184 | } |
| 185 | |
| 186 | void CodeGenModule::EmitCXXGlobalDtorFunc() { |
| 187 | if (CXXGlobalDtors.empty()) |
| 188 | return; |
| 189 | |
| 190 | const llvm::FunctionType *FTy |
| 191 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 192 | false); |
| 193 | |
| 194 | // Create our global destructor function. |
| 195 | llvm::Function *Fn = |
| 196 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 197 | "_GLOBAL__D_a", &TheModule); |
| 198 | |
| 199 | CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors); |
| 200 | AddGlobalDtor(Fn); |
| 201 | } |
| 202 | |
| 203 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
| 204 | const VarDecl *D) { |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 205 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 206 | SourceLocation()); |
| 207 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 208 | llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); |
| 209 | EmitCXXGlobalVarDeclInit(*D, DeclPtr); |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 210 | |
| 211 | FinishFunction(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 212 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 213 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 214 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 215 | llvm::Constant **Decls, |
| 216 | unsigned NumDecls) { |
| 217 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 218 | SourceLocation()); |
| 219 | |
| 220 | for (unsigned i = 0; i != NumDecls; ++i) |
| 221 | Builder.CreateCall(Decls[i]); |
| 222 | |
| 223 | FinishFunction(); |
| 224 | } |
| 225 | |
| 226 | void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn, |
| 227 | const std::vector<std::pair<llvm::Constant*, llvm::Constant*> > |
| 228 | &DtorsAndObjects) { |
| 229 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 230 | SourceLocation()); |
| 231 | |
| 232 | // Emit the dtors, in reverse order from construction. |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 233 | for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { |
| 234 | llvm::Constant *Callee = DtorsAndObjects[e - i - 1].first; |
| 235 | llvm::CallInst *CI = Builder.CreateCall(Callee, |
| 236 | DtorsAndObjects[e - i - 1].second); |
| 237 | // Make sure the call and the callee agree on calling convention. |
| 238 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 239 | CI->setCallingConv(F->getCallingConv()); |
| 240 | } |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 241 | |
| 242 | FinishFunction(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 245 | static llvm::Constant *getGuardAcquireFn(CodeGenFunction &CGF) { |
| 246 | // int __cxa_guard_acquire(__int64_t *guard_object); |
| 247 | |
| 248 | const llvm::Type *Int64PtrTy = |
| 249 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 250 | |
| 251 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 252 | |
| 253 | const llvm::FunctionType *FTy = |
| 254 | llvm::FunctionType::get(CGF.ConvertType(CGF.getContext().IntTy), |
| 255 | Args, /*isVarArg=*/false); |
| 256 | |
| 257 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire"); |
| 258 | } |
| 259 | |
| 260 | static llvm::Constant *getGuardReleaseFn(CodeGenFunction &CGF) { |
| 261 | // void __cxa_guard_release(__int64_t *guard_object); |
| 262 | |
| 263 | const llvm::Type *Int64PtrTy = |
| 264 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 265 | |
| 266 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 267 | |
| 268 | const llvm::FunctionType *FTy = |
| 269 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 270 | Args, /*isVarArg=*/false); |
| 271 | |
| 272 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release"); |
| 273 | } |
| 274 | |
| 275 | static llvm::Constant *getGuardAbortFn(CodeGenFunction &CGF) { |
| 276 | // void __cxa_guard_abort(__int64_t *guard_object); |
| 277 | |
| 278 | const llvm::Type *Int64PtrTy = |
| 279 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 280 | |
| 281 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 282 | |
| 283 | const llvm::FunctionType *FTy = |
| 284 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 285 | Args, /*isVarArg=*/false); |
| 286 | |
| 287 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort"); |
| 288 | } |
| 289 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 290 | void |
| 291 | CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, |
| 292 | llvm::GlobalVariable *GV) { |
John McCall | fe67f3b | 2010-05-04 20:45:42 +0000 | [diff] [blame] | 293 | // Bail out early if this initializer isn't reachable. |
| 294 | if (!Builder.GetInsertBlock()) return; |
| 295 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 296 | bool ThreadsafeStatics = getContext().getLangOptions().ThreadsafeStatics; |
| 297 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 298 | llvm::SmallString<256> GuardVName; |
| 299 | CGM.getMangleContext().mangleGuardVariable(&D, GuardVName); |
| 300 | |
| 301 | // Create the guard variable. |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 302 | const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(VMContext); |
| 303 | llvm::GlobalValue *GuardVariable = |
| 304 | new llvm::GlobalVariable(CGM.getModule(), Int64Ty, |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 305 | false, GV->getLinkage(), |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 306 | llvm::Constant::getNullValue(Int64Ty), |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 307 | GuardVName.str()); |
| 308 | |
| 309 | // Load the first byte of the guard variable. |
| 310 | const llvm::Type *PtrTy |
| 311 | = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 312 | llvm::Value *V = |
| 313 | Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 314 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 315 | llvm::BasicBlock *InitCheckBlock = createBasicBlock("init.check"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 316 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
| 317 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 318 | // Check if the first byte of the guard variable is zero. |
| 319 | Builder.CreateCondBr(Builder.CreateIsNull(V, "tobool"), |
| 320 | InitCheckBlock, EndBlock); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 321 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 322 | EmitBlock(InitCheckBlock); |
| 323 | |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame^] | 324 | if (ThreadsafeStatics) { |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 325 | // Call __cxa_guard_acquire. |
| 326 | V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable); |
| 327 | |
| 328 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
| 329 | |
| 330 | Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), |
| 331 | InitBlock, EndBlock); |
| 332 | |
Douglas Gregor | bfcc823 | 2010-05-15 17:55:51 +0000 | [diff] [blame] | 333 | EmitBlock(InitBlock); |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame^] | 334 | |
| 335 | if (Exceptions) { |
| 336 | EHCleanupBlock Cleanup(*this); |
| 337 | |
| 338 | // Call __cxa_guard_abort. |
| 339 | Builder.CreateCall(getGuardAbortFn(*this), GuardVariable); |
| 340 | } |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 341 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 342 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 343 | if (D.getType()->isReferenceType()) { |
Anders Carlsson | 864143f | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 344 | QualType T = D.getType(); |
Anders Carlsson | c7974ca | 2009-12-10 01:05:11 +0000 | [diff] [blame] | 345 | // We don't want to pass true for IsInitializer here, because a static |
| 346 | // reference to a temporary does not extend its lifetime. |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 347 | RValue RV = EmitReferenceBindingToExpr(D.getInit(), |
Anders Carlsson | 864143f | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 348 | /*IsInitializer=*/false); |
| 349 | EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T); |
| 350 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 351 | } else |
| 352 | EmitDeclInit(*this, D, GV); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 353 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 354 | if (ThreadsafeStatics) { |
| 355 | // Call __cxa_guard_release. |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame^] | 356 | Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable); |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 357 | } else { |
| 358 | llvm::Value *One = |
| 359 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1); |
| 360 | Builder.CreateStore(One, Builder.CreateBitCast(GuardVariable, PtrTy)); |
| 361 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 362 | |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 363 | // Register the call to the destructor. |
| 364 | if (!D.getType()->isReferenceType()) |
| 365 | EmitDeclDestroy(*this, D, GV); |
| 366 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 367 | EmitBlock(EndBlock); |
| 368 | } |