clang API Documentation
00001 //===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This contains code dealing with C++ code generation. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 // We might split this into multiple files if it gets too unwieldy 00015 00016 #include "CGCXXABI.h" 00017 #include "CodeGenFunction.h" 00018 #include "CodeGenModule.h" 00019 #include "clang/AST/ASTContext.h" 00020 #include "clang/AST/RecordLayout.h" 00021 #include "clang/AST/Decl.h" 00022 #include "clang/AST/DeclCXX.h" 00023 #include "clang/AST/DeclObjC.h" 00024 #include "clang/AST/Mangle.h" 00025 #include "clang/AST/StmtCXX.h" 00026 #include "clang/Frontend/CodeGenOptions.h" 00027 #include "llvm/ADT/StringExtras.h" 00028 using namespace clang; 00029 using namespace CodeGen; 00030 00031 /// Try to emit a base destructor as an alias to its primary 00032 /// base-class destructor. 00033 bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) { 00034 if (!getCodeGenOpts().CXXCtorDtorAliases) 00035 return true; 00036 00037 // If the destructor doesn't have a trivial body, we have to emit it 00038 // separately. 00039 if (!D->hasTrivialBody()) 00040 return true; 00041 00042 const CXXRecordDecl *Class = D->getParent(); 00043 00044 // If we need to manipulate a VTT parameter, give up. 00045 if (Class->getNumVBases()) { 00046 // Extra Credit: passing extra parameters is perfectly safe 00047 // in many calling conventions, so only bail out if the ctor's 00048 // calling convention is nonstandard. 00049 return true; 00050 } 00051 00052 // If any field has a non-trivial destructor, we have to emit the 00053 // destructor separately. 00054 for (CXXRecordDecl::field_iterator I = Class->field_begin(), 00055 E = Class->field_end(); I != E; ++I) 00056 if (I->getType().isDestructedType()) 00057 return true; 00058 00059 // Try to find a unique base class with a non-trivial destructor. 00060 const CXXRecordDecl *UniqueBase = 0; 00061 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), 00062 E = Class->bases_end(); I != E; ++I) { 00063 00064 // We're in the base destructor, so skip virtual bases. 00065 if (I->isVirtual()) continue; 00066 00067 // Skip base classes with trivial destructors. 00068 const CXXRecordDecl *Base 00069 = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 00070 if (Base->hasTrivialDestructor()) continue; 00071 00072 // If we've already found a base class with a non-trivial 00073 // destructor, give up. 00074 if (UniqueBase) return true; 00075 UniqueBase = Base; 00076 } 00077 00078 // If we didn't find any bases with a non-trivial destructor, then 00079 // the base destructor is actually effectively trivial, which can 00080 // happen if it was needlessly user-defined or if there are virtual 00081 // bases with non-trivial destructors. 00082 if (!UniqueBase) 00083 return true; 00084 00085 /// If we don't have a definition for the destructor yet, don't 00086 /// emit. We can't emit aliases to declarations; that's just not 00087 /// how aliases work. 00088 const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); 00089 if (!BaseD->isImplicit() && !BaseD->hasBody()) 00090 return true; 00091 00092 // If the base is at a non-zero offset, give up. 00093 const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); 00094 if (ClassLayout.getBaseClassOffsetInBits(UniqueBase) != 0) 00095 return true; 00096 00097 return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base), 00098 GlobalDecl(BaseD, Dtor_Base)); 00099 } 00100 00101 /// Try to emit a definition as a global alias for another definition. 00102 bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, 00103 GlobalDecl TargetDecl) { 00104 if (!getCodeGenOpts().CXXCtorDtorAliases) 00105 return true; 00106 00107 // The alias will use the linkage of the referrent. If we can't 00108 // support aliases with that linkage, fail. 00109 llvm::GlobalValue::LinkageTypes Linkage 00110 = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl())); 00111 00112 switch (Linkage) { 00113 // We can definitely emit aliases to definitions with external linkage. 00114 case llvm::GlobalValue::ExternalLinkage: 00115 case llvm::GlobalValue::ExternalWeakLinkage: 00116 break; 00117 00118 // Same with local linkage. 00119 case llvm::GlobalValue::InternalLinkage: 00120 case llvm::GlobalValue::PrivateLinkage: 00121 case llvm::GlobalValue::LinkerPrivateLinkage: 00122 break; 00123 00124 // We should try to support linkonce linkages. 00125 case llvm::GlobalValue::LinkOnceAnyLinkage: 00126 case llvm::GlobalValue::LinkOnceODRLinkage: 00127 return true; 00128 00129 // Other linkages will probably never be supported. 00130 default: 00131 return true; 00132 } 00133 00134 llvm::GlobalValue::LinkageTypes TargetLinkage 00135 = getFunctionLinkage(cast<FunctionDecl>(TargetDecl.getDecl())); 00136 00137 if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) 00138 return true; 00139 00140 // Derive the type for the alias. 00141 llvm::PointerType *AliasType 00142 = getTypes().GetFunctionType(AliasDecl)->getPointerTo(); 00143 00144 // Find the referrent. Some aliases might require a bitcast, in 00145 // which case the caller is responsible for ensuring the soundness 00146 // of these semantics. 00147 llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); 00148 llvm::Constant *Aliasee = Ref; 00149 if (Ref->getType() != AliasType) 00150 Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); 00151 00152 // Create the alias with no name. 00153 llvm::GlobalAlias *Alias = 00154 new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule()); 00155 00156 // Switch any previous uses to the alias. 00157 StringRef MangledName = getMangledName(AliasDecl); 00158 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 00159 if (Entry) { 00160 assert(Entry->isDeclaration() && "definition already exists for alias"); 00161 assert(Entry->getType() == AliasType && 00162 "declaration exists with different type"); 00163 Alias->takeName(Entry); 00164 Entry->replaceAllUsesWith(Alias); 00165 Entry->eraseFromParent(); 00166 } else { 00167 Alias->setName(MangledName); 00168 } 00169 00170 // Finally, set up the alias with its proper name and attributes. 00171 SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); 00172 00173 return false; 00174 } 00175 00176 void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { 00177 // The constructor used for constructing this as a complete class; 00178 // constucts the virtual bases, then calls the base constructor. 00179 if (!D->getParent()->isAbstract()) { 00180 // We don't need to emit the complete ctor if the class is abstract. 00181 EmitGlobal(GlobalDecl(D, Ctor_Complete)); 00182 } 00183 00184 // The constructor used for constructing this as a base class; 00185 // ignores virtual bases. 00186 EmitGlobal(GlobalDecl(D, Ctor_Base)); 00187 } 00188 00189 void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor, 00190 CXXCtorType ctorType) { 00191 // The complete constructor is equivalent to the base constructor 00192 // for classes with no virtual bases. Try to emit it as an alias. 00193 if (ctorType == Ctor_Complete && 00194 !ctor->getParent()->getNumVBases() && 00195 !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete), 00196 GlobalDecl(ctor, Ctor_Base))) 00197 return; 00198 00199 const CGFunctionInfo &fnInfo = 00200 getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType); 00201 00202 llvm::Function *fn = 00203 cast<llvm::Function>(GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo)); 00204 setFunctionLinkage(ctor, fn); 00205 00206 CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo); 00207 00208 SetFunctionDefinitionAttributes(ctor, fn); 00209 SetLLVMFunctionAttributesForDefinition(ctor, fn); 00210 } 00211 00212 llvm::GlobalValue * 00213 CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, 00214 CXXCtorType ctorType, 00215 const CGFunctionInfo *fnInfo) { 00216 GlobalDecl GD(ctor, ctorType); 00217 00218 StringRef name = getMangledName(GD); 00219 if (llvm::GlobalValue *existing = GetGlobalValue(name)) 00220 return existing; 00221 00222 if (!fnInfo) 00223 fnInfo = &getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType); 00224 00225 llvm::FunctionType *fnType = getTypes().GetFunctionType(*fnInfo); 00226 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, 00227 /*ForVTable=*/false)); 00228 } 00229 00230 void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { 00231 // The destructor in a virtual table is always a 'deleting' 00232 // destructor, which calls the complete destructor and then uses the 00233 // appropriate operator delete. 00234 if (D->isVirtual()) 00235 EmitGlobal(GlobalDecl(D, Dtor_Deleting)); 00236 00237 // The destructor used for destructing this as a most-derived class; 00238 // call the base destructor and then destructs any virtual bases. 00239 EmitGlobal(GlobalDecl(D, Dtor_Complete)); 00240 00241 // The destructor used for destructing this as a base class; ignores 00242 // virtual bases. 00243 EmitGlobal(GlobalDecl(D, Dtor_Base)); 00244 } 00245 00246 void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor, 00247 CXXDtorType dtorType) { 00248 // The complete destructor is equivalent to the base destructor for 00249 // classes with no virtual bases, so try to emit it as an alias. 00250 if (dtorType == Dtor_Complete && 00251 !dtor->getParent()->getNumVBases() && 00252 !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete), 00253 GlobalDecl(dtor, Dtor_Base))) 00254 return; 00255 00256 // The base destructor is equivalent to the base destructor of its 00257 // base class if there is exactly one non-virtual base class with a 00258 // non-trivial destructor, there are no fields with a non-trivial 00259 // destructor, and the body of the destructor is trivial. 00260 if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor)) 00261 return; 00262 00263 const CGFunctionInfo &fnInfo = 00264 getTypes().arrangeCXXDestructor(dtor, dtorType); 00265 00266 llvm::Function *fn = 00267 cast<llvm::Function>(GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo)); 00268 setFunctionLinkage(dtor, fn); 00269 00270 CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo); 00271 00272 SetFunctionDefinitionAttributes(dtor, fn); 00273 SetLLVMFunctionAttributesForDefinition(dtor, fn); 00274 } 00275 00276 llvm::GlobalValue * 00277 CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor, 00278 CXXDtorType dtorType, 00279 const CGFunctionInfo *fnInfo) { 00280 GlobalDecl GD(dtor, dtorType); 00281 00282 StringRef name = getMangledName(GD); 00283 if (llvm::GlobalValue *existing = GetGlobalValue(name)) 00284 return existing; 00285 00286 if (!fnInfo) fnInfo = &getTypes().arrangeCXXDestructor(dtor, dtorType); 00287 00288 llvm::FunctionType *fnType = getTypes().GetFunctionType(*fnInfo); 00289 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, 00290 /*ForVTable=*/false)); 00291 } 00292 00293 static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex, 00294 llvm::Value *This, llvm::Type *Ty) { 00295 Ty = Ty->getPointerTo()->getPointerTo(); 00296 00297 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty); 00298 llvm::Value *VFuncPtr = 00299 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); 00300 return CGF.Builder.CreateLoad(VFuncPtr); 00301 } 00302 00303 llvm::Value * 00304 CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This, 00305 llvm::Type *Ty) { 00306 MD = MD->getCanonicalDecl(); 00307 uint64_t VTableIndex = CGM.getVTableContext().getMethodVTableIndex(MD); 00308 00309 return ::BuildVirtualCall(*this, VTableIndex, This, Ty); 00310 } 00311 00312 /// BuildVirtualCall - This routine is to support gcc's kext ABI making 00313 /// indirect call to virtual functions. It makes the call through indexing 00314 /// into the vtable. 00315 llvm::Value * 00316 CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, 00317 NestedNameSpecifier *Qual, 00318 llvm::Type *Ty) { 00319 llvm::Value *VTable = 0; 00320 assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) && 00321 "BuildAppleKextVirtualCall - bad Qual kind"); 00322 00323 const Type *QTy = Qual->getAsType(); 00324 QualType T = QualType(QTy, 0); 00325 const RecordType *RT = T->getAs<RecordType>(); 00326 assert(RT && "BuildAppleKextVirtualCall - Qual type must be record"); 00327 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 00328 00329 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) 00330 return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD); 00331 00332 VTable = CGM.getVTables().GetAddrOfVTable(RD); 00333 Ty = Ty->getPointerTo()->getPointerTo(); 00334 VTable = Builder.CreateBitCast(VTable, Ty); 00335 assert(VTable && "BuildVirtualCall = kext vtbl pointer is null"); 00336 MD = MD->getCanonicalDecl(); 00337 uint64_t VTableIndex = CGM.getVTableContext().getMethodVTableIndex(MD); 00338 uint64_t AddressPoint = 00339 CGM.getVTableContext().getVTableLayout(RD) 00340 .getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); 00341 VTableIndex += AddressPoint; 00342 llvm::Value *VFuncPtr = 00343 Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); 00344 return Builder.CreateLoad(VFuncPtr); 00345 } 00346 00347 /// BuildVirtualCall - This routine makes indirect vtable call for 00348 /// call to virtual destructors. It returns 0 if it could not do it. 00349 llvm::Value * 00350 CodeGenFunction::BuildAppleKextVirtualDestructorCall( 00351 const CXXDestructorDecl *DD, 00352 CXXDtorType Type, 00353 const CXXRecordDecl *RD) { 00354 llvm::Value * Callee = 0; 00355 const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD); 00356 // FIXME. Dtor_Base dtor is always direct!! 00357 // It need be somehow inline expanded into the caller. 00358 // -O does that. But need to support -O0 as well. 00359 if (MD->isVirtual() && Type != Dtor_Base) { 00360 // Compute the function type we're calling. 00361 const CGFunctionInfo &FInfo = 00362 CGM.getTypes().arrangeCXXDestructor(cast<CXXDestructorDecl>(MD), 00363 Dtor_Complete); 00364 llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo); 00365 00366 llvm::Value *VTable = CGM.getVTables().GetAddrOfVTable(RD); 00367 Ty = Ty->getPointerTo()->getPointerTo(); 00368 VTable = Builder.CreateBitCast(VTable, Ty); 00369 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); 00370 uint64_t VTableIndex = 00371 CGM.getVTableContext().getMethodVTableIndex(GlobalDecl(DD, Type)); 00372 uint64_t AddressPoint = 00373 CGM.getVTableContext().getVTableLayout(RD) 00374 .getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); 00375 VTableIndex += AddressPoint; 00376 llvm::Value *VFuncPtr = 00377 Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); 00378 Callee = Builder.CreateLoad(VFuncPtr); 00379 } 00380 return Callee; 00381 } 00382 00383 llvm::Value * 00384 CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type, 00385 llvm::Value *This, llvm::Type *Ty) { 00386 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); 00387 uint64_t VTableIndex = 00388 CGM.getVTableContext().getMethodVTableIndex(GlobalDecl(DD, Type)); 00389 00390 return ::BuildVirtualCall(*this, VTableIndex, This, Ty); 00391 } 00392