2010-10-04 21:15:33 +00:00
|
|
|
//===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===//
|
2008-08-22 16:00:37 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with C++ code generation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-09 15:08:12 +00:00
|
|
|
// We might split this into multiple files if it gets too unwieldy
|
2008-08-22 16:00:37 +00:00
|
|
|
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "CodeGenModule.h"
|
2010-05-25 19:52:27 +00:00
|
|
|
#include "CGCXXABI.h"
|
2008-08-22 16:00:37 +00:00
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2009-04-03 22:50:24 +00:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2008-08-23 19:42:54 +00:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2011-01-13 18:57:25 +00:00
|
|
|
#include "clang/AST/Mangle.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-09-27 18:58:34 +00:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2010-06-15 23:19:56 +00:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2008-08-22 16:00:37 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2010-02-23 00:48:20 +00:00
|
|
|
/// Try to emit a base destructor as an alias to its primary
|
|
|
|
/// base-class destructor.
|
|
|
|
bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) {
|
2010-02-19 01:32:20 +00:00
|
|
|
if (!getCodeGenOpts().CXXCtorDtorAliases)
|
|
|
|
return true;
|
|
|
|
|
2013-11-13 23:20:45 +00:00
|
|
|
// Producing an alias to a base class ctor/dtor can degrade debug quality
|
2013-12-05 16:25:25 +00:00
|
|
|
// as the debugger cannot tell them apart.
|
2013-11-13 23:20:45 +00:00
|
|
|
if (getCodeGenOpts().OptimizationLevel == 0)
|
|
|
|
return true;
|
|
|
|
|
2010-02-23 00:48:20 +00:00
|
|
|
// If the destructor doesn't have a trivial body, we have to emit it
|
|
|
|
// separately.
|
2011-05-14 23:26:09 +00:00
|
|
|
if (!D->hasTrivialBody())
|
2010-02-23 00:48:20 +00:00
|
|
|
return true;
|
2010-02-19 01:32:20 +00:00
|
|
|
|
2010-02-23 00:48:20 +00:00
|
|
|
const CXXRecordDecl *Class = D->getParent();
|
|
|
|
|
2014-10-31 19:01:02 +00:00
|
|
|
// We are going to instrument this destructor, so give up even if it is
|
|
|
|
// currently empty.
|
|
|
|
if (Class->mayInsertExtraPadding())
|
|
|
|
return true;
|
|
|
|
|
2010-02-23 00:48:20 +00:00
|
|
|
// If we need to manipulate a VTT parameter, give up.
|
|
|
|
if (Class->getNumVBases()) {
|
|
|
|
// Extra Credit: passing extra parameters is perfectly safe
|
|
|
|
// in many calling conventions, so only bail out if the ctor's
|
|
|
|
// calling convention is nonstandard.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-13 00:46:43 +00:00
|
|
|
// If any field has a non-trivial destructor, we have to emit the
|
|
|
|
// destructor separately.
|
2014-03-08 20:12:42 +00:00
|
|
|
for (const auto *I : Class->fields())
|
2012-04-30 02:36:29 +00:00
|
|
|
if (I->getType().isDestructedType())
|
2011-02-13 00:46:43 +00:00
|
|
|
return true;
|
2010-02-23 00:48:20 +00:00
|
|
|
|
|
|
|
// Try to find a unique base class with a non-trivial destructor.
|
2014-05-21 05:09:00 +00:00
|
|
|
const CXXRecordDecl *UniqueBase = nullptr;
|
2014-03-13 15:41:46 +00:00
|
|
|
for (const auto &I : Class->bases()) {
|
2010-02-23 00:48:20 +00:00
|
|
|
|
|
|
|
// We're in the base destructor, so skip virtual bases.
|
2014-03-13 15:41:46 +00:00
|
|
|
if (I.isVirtual()) continue;
|
2010-02-23 00:48:20 +00:00
|
|
|
|
|
|
|
// Skip base classes with trivial destructors.
|
2014-05-09 00:08:36 +00:00
|
|
|
const auto *Base =
|
|
|
|
cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
|
2010-02-23 00:48:20 +00:00
|
|
|
if (Base->hasTrivialDestructor()) continue;
|
|
|
|
|
|
|
|
// If we've already found a base class with a non-trivial
|
|
|
|
// destructor, give up.
|
|
|
|
if (UniqueBase) return true;
|
|
|
|
UniqueBase = Base;
|
2010-02-19 01:32:20 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 00:48:20 +00:00
|
|
|
// If we didn't find any bases with a non-trivial destructor, then
|
|
|
|
// the base destructor is actually effectively trivial, which can
|
|
|
|
// happen if it was needlessly user-defined or if there are virtual
|
|
|
|
// bases with non-trivial destructors.
|
|
|
|
if (!UniqueBase)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If the base is at a non-zero offset, give up.
|
|
|
|
const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class);
|
2012-07-04 18:45:14 +00:00
|
|
|
if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero())
|
2010-02-23 00:48:20 +00:00
|
|
|
return true;
|
|
|
|
|
2014-03-05 21:04:41 +00:00
|
|
|
// Give up if the calling conventions don't match. We could update the call,
|
|
|
|
// but it is probably not worth it.
|
2013-11-09 01:57:21 +00:00
|
|
|
const CXXDestructorDecl *BaseD = UniqueBase->getDestructor();
|
2014-03-05 21:04:41 +00:00
|
|
|
if (BaseD->getType()->getAs<FunctionType>()->getCallConv() !=
|
|
|
|
D->getType()->getAs<FunctionType>()->getCallConv())
|
|
|
|
return true;
|
|
|
|
|
2010-02-23 00:48:20 +00:00
|
|
|
return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base),
|
2013-11-04 18:38:59 +00:00
|
|
|
GlobalDecl(BaseD, Dtor_Base),
|
|
|
|
false);
|
2010-02-23 00:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to emit a definition as a global alias for another definition.
|
2013-11-04 18:38:59 +00:00
|
|
|
/// If \p InEveryTU is true, we know that an equivalent alias can be produced
|
|
|
|
/// in every translation unit.
|
2010-02-23 00:48:20 +00:00
|
|
|
bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl,
|
2013-11-04 18:38:59 +00:00
|
|
|
GlobalDecl TargetDecl,
|
|
|
|
bool InEveryTU) {
|
2010-02-23 00:48:20 +00:00
|
|
|
if (!getCodeGenOpts().CXXCtorDtorAliases)
|
|
|
|
return true;
|
|
|
|
|
2013-12-05 16:25:25 +00:00
|
|
|
// The alias will use the linkage of the referent. If we can't
|
2010-02-19 01:32:20 +00:00
|
|
|
// support aliases with that linkage, fail.
|
2013-06-05 17:49:37 +00:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl);
|
2010-02-19 01:32:20 +00:00
|
|
|
|
2013-11-04 18:38:59 +00:00
|
|
|
// We can't use an alias if the linkage is not valid for one.
|
|
|
|
if (!llvm::GlobalAlias::isValidLinkage(Linkage))
|
2010-03-05 01:21:10 +00:00
|
|
|
return true;
|
|
|
|
|
2014-10-24 22:05:27 +00:00
|
|
|
// Don't create a weak alias for a dllexport'd symbol.
|
|
|
|
if (AliasDecl.getDecl()->hasAttr<DLLExportAttr>() &&
|
|
|
|
llvm::GlobalValue::isWeakForLinker(Linkage))
|
|
|
|
return true;
|
|
|
|
|
2013-11-05 21:37:29 +00:00
|
|
|
llvm::GlobalValue::LinkageTypes TargetLinkage =
|
|
|
|
getFunctionLinkage(TargetDecl);
|
|
|
|
|
2013-11-04 18:38:59 +00:00
|
|
|
// Check if we have it already.
|
|
|
|
StringRef MangledName = getMangledName(AliasDecl);
|
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
|
|
|
if (Entry && !Entry->isDeclaration())
|
|
|
|
return false;
|
2013-11-05 21:37:29 +00:00
|
|
|
if (Replacements.count(MangledName))
|
|
|
|
return false;
|
2013-11-04 18:38:59 +00:00
|
|
|
|
2010-02-23 00:48:20 +00:00
|
|
|
// Derive the type for the alias.
|
2011-07-18 04:24:23 +00:00
|
|
|
llvm::PointerType *AliasType
|
2010-02-23 00:48:20 +00:00
|
|
|
= getTypes().GetFunctionType(AliasDecl)->getPointerTo();
|
|
|
|
|
2013-12-05 16:25:25 +00:00
|
|
|
// Find the referent. Some aliases might require a bitcast, in
|
2010-02-23 00:48:20 +00:00
|
|
|
// which case the caller is responsible for ensuring the soundness
|
|
|
|
// of these semantics.
|
2014-05-09 00:08:36 +00:00
|
|
|
auto *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
|
2014-06-03 02:42:01 +00:00
|
|
|
llvm::Constant *Aliasee = Ref;
|
|
|
|
if (Ref->getType() != AliasType)
|
|
|
|
Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
|
2010-02-23 00:48:20 +00:00
|
|
|
|
2013-11-08 22:59:46 +00:00
|
|
|
// Instead of creating as alias to a linkonce_odr, replace all of the uses
|
2014-05-16 19:35:48 +00:00
|
|
|
// of the aliasee.
|
2013-11-22 21:34:35 +00:00
|
|
|
if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) &&
|
|
|
|
(TargetLinkage != llvm::GlobalValue::AvailableExternallyLinkage ||
|
|
|
|
!TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) {
|
2013-12-05 16:25:25 +00:00
|
|
|
// FIXME: An extern template instantiation will create functions with
|
2013-11-22 21:34:35 +00:00
|
|
|
// linkage "AvailableExternally". In libc++, some classes also define
|
|
|
|
// members with attribute "AlwaysInline" and expect no reference to
|
|
|
|
// be generated. It is desirable to reenable this optimisation after
|
|
|
|
// corresponding LLVM changes.
|
2014-06-03 02:42:01 +00:00
|
|
|
Replacements[MangledName] = Aliasee;
|
2013-11-08 22:59:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-09 01:57:21 +00:00
|
|
|
if (!InEveryTU) {
|
2015-01-12 21:22:27 +00:00
|
|
|
// If we don't have a definition for the destructor yet, don't
|
|
|
|
// emit. We can't emit aliases to declarations; that's just not
|
|
|
|
// how aliases work.
|
2014-06-03 02:42:01 +00:00
|
|
|
if (Ref->isDeclaration())
|
2013-11-05 21:37:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-12 22:06:46 +00:00
|
|
|
// Don't create an alias to a linker weak symbol. This avoids producing
|
|
|
|
// different COMDATs in different TUs. Another option would be to
|
|
|
|
// output the alias both for weak_odr and linkonce_odr, but that
|
|
|
|
// requires explicit comdat support in the IL.
|
|
|
|
if (llvm::GlobalValue::isWeakForLinker(TargetLinkage))
|
|
|
|
return true;
|
|
|
|
|
2010-02-19 01:32:20 +00:00
|
|
|
// Create the alias with no name.
|
2015-04-29 21:22:47 +00:00
|
|
|
auto *Alias =
|
|
|
|
llvm::GlobalAlias::create(AliasType, Linkage, "", Aliasee, &getModule());
|
2010-02-19 01:32:20 +00:00
|
|
|
|
2010-02-24 20:32:01 +00:00
|
|
|
// Switch any previous uses to the alias.
|
2010-02-19 01:32:20 +00:00
|
|
|
if (Entry) {
|
2010-02-24 20:32:01 +00:00
|
|
|
assert(Entry->getType() == AliasType &&
|
|
|
|
"declaration exists with different type");
|
2010-03-19 23:29:14 +00:00
|
|
|
Alias->takeName(Entry);
|
2010-02-19 01:32:20 +00:00
|
|
|
Entry->replaceAllUsesWith(Alias);
|
|
|
|
Entry->eraseFromParent();
|
2010-03-19 23:29:14 +00:00
|
|
|
} else {
|
2010-06-22 16:16:50 +00:00
|
|
|
Alias->setName(MangledName);
|
2010-02-19 01:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, set up the alias with its proper name and attributes.
|
2014-10-07 13:34:42 +00:00
|
|
|
setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
|
2010-02-19 01:32:20 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-11 23:37:08 +00:00
|
|
|
|
2014-09-15 19:34:18 +00:00
|
|
|
llvm::Function *CodeGenModule::codegenCXXStructor(const CXXMethodDecl *MD,
|
|
|
|
StructorType Type) {
|
|
|
|
const CGFunctionInfo &FnInfo =
|
|
|
|
getTypes().arrangeCXXStructorDeclaration(MD, Type);
|
|
|
|
auto *Fn = cast<llvm::Function>(
|
|
|
|
getAddrOfCXXStructor(MD, Type, &FnInfo, nullptr, true));
|
|
|
|
|
|
|
|
GlobalDecl GD;
|
|
|
|
if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
GD = GlobalDecl(DD, toCXXDtorType(Type));
|
|
|
|
} else {
|
|
|
|
const auto *CD = cast<CXXConstructorDecl>(MD);
|
|
|
|
GD = GlobalDecl(CD, toCXXCtorType(Type));
|
|
|
|
}
|
|
|
|
|
|
|
|
setFunctionLinkage(GD, Fn);
|
2015-05-28 17:44:56 +00:00
|
|
|
setFunctionDLLStorageClass(GD, Fn);
|
|
|
|
|
2014-09-15 19:34:18 +00:00
|
|
|
CodeGenFunction(*this).GenerateCode(GD, Fn, FnInfo);
|
|
|
|
setFunctionDefinitionAttributes(MD, Fn);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(MD, Fn);
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
2014-09-08 16:01:27 +00:00
|
|
|
llvm::GlobalValue *CodeGenModule::getAddrOfCXXStructor(
|
|
|
|
const CXXMethodDecl *MD, StructorType Type, const CGFunctionInfo *FnInfo,
|
|
|
|
llvm::FunctionType *FnType, bool DontDefer) {
|
|
|
|
GlobalDecl GD;
|
|
|
|
if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
|
|
|
|
GD = GlobalDecl(CD, toCXXCtorType(Type));
|
|
|
|
} else {
|
2015-05-06 22:18:39 +00:00
|
|
|
GD = GlobalDecl(cast<CXXDestructorDecl>(MD), toCXXDtorType(Type));
|
2014-09-08 16:01:27 +00:00
|
|
|
}
|
2011-03-09 08:12:35 +00:00
|
|
|
|
2014-09-08 16:01:27 +00:00
|
|
|
StringRef Name = getMangledName(GD);
|
|
|
|
if (llvm::GlobalValue *Existing = GetGlobalValue(Name))
|
|
|
|
return Existing;
|
2011-03-09 08:12:35 +00:00
|
|
|
|
2014-09-08 16:01:27 +00:00
|
|
|
if (!FnType) {
|
|
|
|
if (!FnInfo)
|
|
|
|
FnInfo = &getTypes().arrangeCXXStructorDeclaration(MD, Type);
|
|
|
|
FnType = getTypes().GetFunctionType(*FnInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FnType, GD,
|
2013-12-09 04:29:47 +00:00
|
|
|
/*ForVTable=*/false,
|
|
|
|
DontDefer));
|
2009-04-16 23:57:24 +00:00
|
|
|
}
|
2009-04-17 01:58:57 +00:00
|
|
|
|
2013-07-19 08:14:45 +00:00
|
|
|
static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF,
|
|
|
|
GlobalDecl GD,
|
|
|
|
llvm::Type *Ty,
|
|
|
|
const CXXRecordDecl *RD) {
|
2013-08-21 06:25:03 +00:00
|
|
|
assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() &&
|
|
|
|
"No kext in Microsoft ABI");
|
2013-07-19 08:14:45 +00:00
|
|
|
GD = GD.getCanonicalDecl();
|
|
|
|
CodeGenModule &CGM = CGF.CGM;
|
2013-09-27 14:48:01 +00:00
|
|
|
llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
|
2013-07-19 08:14:45 +00:00
|
|
|
Ty = Ty->getPointerTo()->getPointerTo();
|
|
|
|
VTable = CGF.Builder.CreateBitCast(VTable, Ty);
|
|
|
|
assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
|
2013-11-05 15:54:58 +00:00
|
|
|
uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
|
2013-07-19 08:14:45 +00:00
|
|
|
uint64_t AddressPoint =
|
2013-11-05 15:54:58 +00:00
|
|
|
CGM.getItaniumVTableContext().getVTableLayout(RD)
|
2013-07-19 08:14:45 +00:00
|
|
|
.getAddressPoint(BaseSubobject(RD, CharUnits::Zero()));
|
|
|
|
VTableIndex += AddressPoint;
|
|
|
|
llvm::Value *VFuncPtr =
|
|
|
|
CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
|
|
|
|
return CGF.Builder.CreateLoad(VFuncPtr);
|
2009-11-13 04:45:41 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 08:14:45 +00:00
|
|
|
/// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making
|
2011-01-20 17:19:02 +00:00
|
|
|
/// indirect call to virtual functions. It makes the call through indexing
|
|
|
|
/// into the vtable.
|
|
|
|
llvm::Value *
|
|
|
|
CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
|
|
|
|
NestedNameSpecifier *Qual,
|
2011-07-18 04:24:23 +00:00
|
|
|
llvm::Type *Ty) {
|
2011-01-20 17:19:02 +00:00
|
|
|
assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) &&
|
|
|
|
"BuildAppleKextVirtualCall - bad Qual kind");
|
|
|
|
|
|
|
|
const Type *QTy = Qual->getAsType();
|
|
|
|
QualType T = QualType(QTy, 0);
|
|
|
|
const RecordType *RT = T->getAs<RecordType>();
|
|
|
|
assert(RT && "BuildAppleKextVirtualCall - Qual type must be record");
|
2014-05-09 00:08:36 +00:00
|
|
|
const auto *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
|
|
|
|
if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD))
|
2011-02-01 23:22:34 +00:00
|
|
|
return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD);
|
2013-07-19 08:14:45 +00:00
|
|
|
|
|
|
|
return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD);
|
2011-01-20 17:19:02 +00:00
|
|
|
}
|
|
|
|
|
2011-02-01 23:22:34 +00:00
|
|
|
/// BuildVirtualCall - This routine makes indirect vtable call for
|
|
|
|
/// call to virtual destructors. It returns 0 if it could not do it.
|
|
|
|
llvm::Value *
|
|
|
|
CodeGenFunction::BuildAppleKextVirtualDestructorCall(
|
|
|
|
const CXXDestructorDecl *DD,
|
|
|
|
CXXDtorType Type,
|
|
|
|
const CXXRecordDecl *RD) {
|
2014-05-09 00:08:36 +00:00
|
|
|
const auto *MD = cast<CXXMethodDecl>(DD);
|
2011-02-01 23:22:34 +00:00
|
|
|
// FIXME. Dtor_Base dtor is always direct!!
|
|
|
|
// It need be somehow inline expanded into the caller.
|
|
|
|
// -O does that. But need to support -O0 as well.
|
|
|
|
if (MD->isVirtual() && Type != Dtor_Base) {
|
|
|
|
// Compute the function type we're calling.
|
2014-09-08 16:01:27 +00:00
|
|
|
const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration(
|
|
|
|
DD, StructorType::Complete);
|
2012-02-17 03:33:10 +00:00
|
|
|
llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo);
|
2013-07-19 08:14:45 +00:00
|
|
|
return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD);
|
2011-02-01 23:22:34 +00:00
|
|
|
}
|
2014-05-21 05:09:00 +00:00
|
|
|
return nullptr;
|
2011-02-01 23:22:34 +00:00
|
|
|
}
|