2010-10-05 05:15:33 +08:00
|
|
|
//===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===//
|
2008-08-23 00:00:37 +08: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 23:08:12 +08:00
|
|
|
// We might split this into multiple files if it gets too unwieldy
|
2008-08-23 00:00:37 +08:00
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CodeGenModule.h"
|
2010-05-26 03:52:27 +08:00
|
|
|
#include "CGCXXABI.h"
|
2008-08-23 00:00:37 +08:00
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2009-04-04 06:50:24 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2008-08-24 03:42:54 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2011-01-14 02:57:25 +08:00
|
|
|
#include "clang/AST/Mangle.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-09-28 02:58:34 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2010-06-16 07:19:56 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2008-08-23 00:00:37 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2010-02-23 08:48:20 +08: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 09:32:20 +08:00
|
|
|
if (!getCodeGenOpts().CXXCtorDtorAliases)
|
|
|
|
return true;
|
|
|
|
|
2013-11-14 07:20:45 +08:00
|
|
|
// Producing an alias to a base class ctor/dtor can degrade debug quality
|
2013-12-06 00:25:25 +08:00
|
|
|
// as the debugger cannot tell them apart.
|
2013-11-14 07:20:45 +08:00
|
|
|
if (getCodeGenOpts().OptimizationLevel == 0)
|
|
|
|
return true;
|
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
// If the destructor doesn't have a trivial body, we have to emit it
|
|
|
|
// separately.
|
2011-05-15 07:26:09 +08:00
|
|
|
if (!D->hasTrivialBody())
|
2010-02-23 08:48:20 +08:00
|
|
|
return true;
|
2010-02-19 09:32:20 +08:00
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
const CXXRecordDecl *Class = D->getParent();
|
|
|
|
|
|
|
|
// 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 08:46:43 +08:00
|
|
|
// If any field has a non-trivial destructor, we have to emit the
|
|
|
|
// destructor separately.
|
2010-02-23 08:48:20 +08:00
|
|
|
for (CXXRecordDecl::field_iterator I = Class->field_begin(),
|
|
|
|
E = Class->field_end(); I != E; ++I)
|
2012-04-30 10:36:29 +08:00
|
|
|
if (I->getType().isDestructedType())
|
2011-02-13 08:46:43 +08:00
|
|
|
return true;
|
2010-02-23 08:48:20 +08:00
|
|
|
|
|
|
|
// Try to find a unique base class with a non-trivial destructor.
|
|
|
|
const CXXRecordDecl *UniqueBase = 0;
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
|
|
|
|
E = Class->bases_end(); I != E; ++I) {
|
|
|
|
|
|
|
|
// We're in the base destructor, so skip virtual bases.
|
|
|
|
if (I->isVirtual()) continue;
|
|
|
|
|
|
|
|
// Skip base classes with trivial destructors.
|
|
|
|
const CXXRecordDecl *Base
|
|
|
|
= cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
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 09:32:20 +08:00
|
|
|
}
|
|
|
|
|
2010-02-23 08:48:20 +08: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-05 02:45:14 +08:00
|
|
|
if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero())
|
2010-02-23 08:48:20 +08:00
|
|
|
return true;
|
|
|
|
|
2014-03-06 05:04:41 +08: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 09:57:21 +08:00
|
|
|
const CXXDestructorDecl *BaseD = UniqueBase->getDestructor();
|
2014-03-06 05:04:41 +08:00
|
|
|
if (BaseD->getType()->getAs<FunctionType>()->getCallConv() !=
|
|
|
|
D->getType()->getAs<FunctionType>()->getCallConv())
|
|
|
|
return true;
|
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base),
|
2013-11-05 02:38:59 +08:00
|
|
|
GlobalDecl(BaseD, Dtor_Base),
|
|
|
|
false);
|
2010-02-23 08:48:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to emit a definition as a global alias for another definition.
|
2013-11-05 02:38:59 +08:00
|
|
|
/// If \p InEveryTU is true, we know that an equivalent alias can be produced
|
|
|
|
/// in every translation unit.
|
2010-02-23 08:48:20 +08:00
|
|
|
bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl,
|
2013-11-05 02:38:59 +08:00
|
|
|
GlobalDecl TargetDecl,
|
|
|
|
bool InEveryTU) {
|
2010-02-23 08:48:20 +08:00
|
|
|
if (!getCodeGenOpts().CXXCtorDtorAliases)
|
|
|
|
return true;
|
|
|
|
|
2013-12-06 00:25:25 +08:00
|
|
|
// The alias will use the linkage of the referent. If we can't
|
2010-02-19 09:32:20 +08:00
|
|
|
// support aliases with that linkage, fail.
|
2013-06-06 01:49:37 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl);
|
2010-02-19 09:32:20 +08:00
|
|
|
|
2013-11-05 02:38:59 +08:00
|
|
|
// We can't use an alias if the linkage is not valid for one.
|
|
|
|
if (!llvm::GlobalAlias::isValidLinkage(Linkage))
|
2010-03-05 09:21:10 +08:00
|
|
|
return true;
|
|
|
|
|
2013-11-06 05:37:29 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes TargetLinkage =
|
|
|
|
getFunctionLinkage(TargetDecl);
|
|
|
|
|
2013-11-05 02:38:59 +08:00
|
|
|
// Check if we have it already.
|
|
|
|
StringRef MangledName = getMangledName(AliasDecl);
|
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
|
|
|
if (Entry && !Entry->isDeclaration())
|
|
|
|
return false;
|
2013-11-06 05:37:29 +08:00
|
|
|
if (Replacements.count(MangledName))
|
|
|
|
return false;
|
2013-11-05 02:38:59 +08:00
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
// Derive the type for the alias.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::PointerType *AliasType
|
2010-02-23 08:48:20 +08:00
|
|
|
= getTypes().GetFunctionType(AliasDecl)->getPointerTo();
|
|
|
|
|
2013-12-06 00:25:25 +08:00
|
|
|
// Find the referent. Some aliases might require a bitcast, in
|
2010-02-23 08:48:20 +08:00
|
|
|
// which case the caller is responsible for ensuring the soundness
|
|
|
|
// of these semantics.
|
|
|
|
llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
|
|
|
|
llvm::Constant *Aliasee = Ref;
|
|
|
|
if (Ref->getType() != AliasType)
|
|
|
|
Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
|
|
|
|
|
2013-11-09 06:59:46 +08:00
|
|
|
// Instead of creating as alias to a linkonce_odr, replace all of the uses
|
|
|
|
// of the aliassee.
|
2013-11-23 05:34:35 +08:00
|
|
|
if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) &&
|
|
|
|
(TargetLinkage != llvm::GlobalValue::AvailableExternallyLinkage ||
|
|
|
|
!TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) {
|
2013-12-06 00:25:25 +08:00
|
|
|
// FIXME: An extern template instantiation will create functions with
|
2013-11-23 05:34:35 +08: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.
|
2013-11-09 06:59:46 +08:00
|
|
|
Replacements[MangledName] = Aliasee;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-09 09:57:21 +08:00
|
|
|
if (!InEveryTU) {
|
|
|
|
/// 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.
|
|
|
|
if (Ref->isDeclaration())
|
2013-11-06 05:37:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-13 06:06:46 +08: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 09:32:20 +08:00
|
|
|
// Create the alias with no name.
|
|
|
|
llvm::GlobalAlias *Alias =
|
2010-02-23 08:48:20 +08:00
|
|
|
new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule());
|
2010-02-19 09:32:20 +08:00
|
|
|
|
2010-02-25 04:32:01 +08:00
|
|
|
// Switch any previous uses to the alias.
|
2010-02-19 09:32:20 +08:00
|
|
|
if (Entry) {
|
2010-02-25 04:32:01 +08:00
|
|
|
assert(Entry->getType() == AliasType &&
|
|
|
|
"declaration exists with different type");
|
2010-03-20 07:29:14 +08:00
|
|
|
Alias->takeName(Entry);
|
2010-02-19 09:32:20 +08:00
|
|
|
Entry->replaceAllUsesWith(Alias);
|
|
|
|
Entry->eraseFromParent();
|
2010-03-20 07:29:14 +08:00
|
|
|
} else {
|
2010-06-23 00:16:50 +08:00
|
|
|
Alias->setName(MangledName);
|
2010-02-19 09:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, set up the alias with its proper name and attributes.
|
2010-10-23 05:05:15 +08:00
|
|
|
SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
|
2010-02-19 09:32:20 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-12 07:37:08 +08:00
|
|
|
|
2011-03-09 16:12:35 +08:00
|
|
|
void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor,
|
|
|
|
CXXCtorType ctorType) {
|
2014-01-14 06:57:31 +08:00
|
|
|
if (!getTarget().getCXXABI().hasConstructorVariants()) {
|
|
|
|
// If there are no constructor variants, always emit the complete destructor.
|
|
|
|
ctorType = Ctor_Complete;
|
|
|
|
} else if (!ctor->getParent()->getNumVBases() &&
|
|
|
|
(ctorType == Ctor_Complete || ctorType == Ctor_Base)) {
|
|
|
|
// The complete constructor is equivalent to the base constructor
|
|
|
|
// for classes with no virtual bases. Try to emit it as an alias.
|
2013-11-05 02:38:59 +08:00
|
|
|
bool ProducedAlias =
|
|
|
|
!TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete),
|
|
|
|
GlobalDecl(ctor, Ctor_Base), true);
|
|
|
|
if (ctorType == Ctor_Complete && ProducedAlias)
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-17 11:33:10 +08:00
|
|
|
const CGFunctionInfo &fnInfo =
|
|
|
|
getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType);
|
2011-03-09 12:27:21 +08:00
|
|
|
|
2013-12-09 12:29:47 +08:00
|
|
|
llvm::Function *fn = cast<llvm::Function>(
|
|
|
|
GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo, true));
|
2013-06-06 01:49:37 +08:00
|
|
|
setFunctionLinkage(GlobalDecl(ctor, ctorType), fn);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-09 16:12:35 +08:00
|
|
|
CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-09 16:12:35 +08:00
|
|
|
SetFunctionDefinitionAttributes(ctor, fn);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(ctor, fn);
|
2009-04-17 09:58:57 +08:00
|
|
|
}
|
|
|
|
|
2010-02-19 09:32:20 +08:00
|
|
|
llvm::GlobalValue *
|
2011-03-09 16:12:35 +08:00
|
|
|
CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor,
|
|
|
|
CXXCtorType ctorType,
|
2013-12-09 12:29:47 +08:00
|
|
|
const CGFunctionInfo *fnInfo,
|
|
|
|
bool DontDefer) {
|
2011-03-09 16:12:35 +08:00
|
|
|
GlobalDecl GD(ctor, ctorType);
|
2010-06-09 10:30:12 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef name = getMangledName(GD);
|
2011-03-09 16:12:35 +08:00
|
|
|
if (llvm::GlobalValue *existing = GetGlobalValue(name))
|
|
|
|
return existing;
|
|
|
|
|
2012-02-17 11:33:10 +08:00
|
|
|
if (!fnInfo)
|
|
|
|
fnInfo = &getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType);
|
2011-03-09 16:12:35 +08:00
|
|
|
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::FunctionType *fnType = getTypes().GetFunctionType(*fnInfo);
|
2011-03-09 16:12:35 +08:00
|
|
|
return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
|
2013-12-09 12:29:47 +08:00
|
|
|
/*ForVTable=*/false,
|
|
|
|
DontDefer));
|
2009-04-17 07:57:24 +08:00
|
|
|
}
|
2009-04-17 09:58:57 +08:00
|
|
|
|
2011-03-09 16:12:35 +08:00
|
|
|
void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor,
|
|
|
|
CXXDtorType dtorType) {
|
2010-02-19 09:32:20 +08:00
|
|
|
// The complete destructor is equivalent to the base destructor for
|
|
|
|
// classes with no virtual bases, so try to emit it as an alias.
|
2013-11-05 02:38:59 +08:00
|
|
|
if (!dtor->getParent()->getNumVBases() &&
|
|
|
|
(dtorType == Dtor_Complete || dtorType == Dtor_Base)) {
|
|
|
|
bool ProducedAlias =
|
|
|
|
!TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete),
|
|
|
|
GlobalDecl(dtor, Dtor_Base), true);
|
|
|
|
if (ProducedAlias) {
|
|
|
|
if (dtorType == Dtor_Complete)
|
|
|
|
return;
|
|
|
|
if (dtor->isVirtual())
|
|
|
|
getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete));
|
|
|
|
}
|
|
|
|
}
|
2010-02-19 09:32:20 +08:00
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
// The base destructor is equivalent to the base destructor of its
|
|
|
|
// base class if there is exactly one non-virtual base class with a
|
|
|
|
// non-trivial destructor, there are no fields with a non-trivial
|
|
|
|
// destructor, and the body of the destructor is trivial.
|
2011-03-09 16:12:35 +08:00
|
|
|
if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor))
|
2010-02-23 08:48:20 +08:00
|
|
|
return;
|
|
|
|
|
2012-02-17 11:33:10 +08:00
|
|
|
const CGFunctionInfo &fnInfo =
|
|
|
|
getTypes().arrangeCXXDestructor(dtor, dtorType);
|
2011-03-09 12:27:21 +08:00
|
|
|
|
2013-12-09 12:29:47 +08:00
|
|
|
llvm::Function *fn = cast<llvm::Function>(
|
|
|
|
GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo, 0, true));
|
2013-06-06 01:49:37 +08:00
|
|
|
setFunctionLinkage(GlobalDecl(dtor, dtorType), fn);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-09 16:12:35 +08:00
|
|
|
CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-09 16:12:35 +08:00
|
|
|
SetFunctionDefinitionAttributes(dtor, fn);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(dtor, fn);
|
2009-04-17 09:58:57 +08:00
|
|
|
}
|
|
|
|
|
2010-02-19 09:32:20 +08:00
|
|
|
llvm::GlobalValue *
|
2011-03-09 16:12:35 +08:00
|
|
|
CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
|
|
|
|
CXXDtorType dtorType,
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
const CGFunctionInfo *fnInfo,
|
2013-12-09 12:29:47 +08:00
|
|
|
llvm::FunctionType *fnType,
|
|
|
|
bool DontDefer) {
|
2011-03-09 16:12:35 +08:00
|
|
|
GlobalDecl GD(dtor, dtorType);
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef name = getMangledName(GD);
|
2011-03-09 16:12:35 +08:00
|
|
|
if (llvm::GlobalValue *existing = GetGlobalValue(name))
|
|
|
|
return existing;
|
2010-06-09 10:30:12 +08:00
|
|
|
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
if (!fnType) {
|
|
|
|
if (!fnInfo) fnInfo = &getTypes().arrangeCXXDestructor(dtor, dtorType);
|
|
|
|
fnType = getTypes().GetFunctionType(*fnInfo);
|
|
|
|
}
|
2011-03-09 16:12:35 +08:00
|
|
|
return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
|
2013-12-09 12:29:47 +08:00
|
|
|
/*ForVTable=*/false,
|
|
|
|
DontDefer));
|
2009-04-17 09:58:57 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 16:14:45 +08:00
|
|
|
static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF,
|
|
|
|
GlobalDecl GD,
|
|
|
|
llvm::Type *Ty,
|
|
|
|
const CXXRecordDecl *RD) {
|
2013-08-21 14:25:03 +08:00
|
|
|
assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() &&
|
|
|
|
"No kext in Microsoft ABI");
|
2013-07-19 16:14:45 +08:00
|
|
|
GD = GD.getCanonicalDecl();
|
|
|
|
CodeGenModule &CGM = CGF.CGM;
|
2013-09-27 22:48:01 +08:00
|
|
|
llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
|
2013-07-19 16:14:45 +08:00
|
|
|
Ty = Ty->getPointerTo()->getPointerTo();
|
|
|
|
VTable = CGF.Builder.CreateBitCast(VTable, Ty);
|
|
|
|
assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
|
2013-11-05 23:54:58 +08:00
|
|
|
uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
|
2013-07-19 16:14:45 +08:00
|
|
|
uint64_t AddressPoint =
|
2013-11-05 23:54:58 +08:00
|
|
|
CGM.getItaniumVTableContext().getVTableLayout(RD)
|
2013-07-19 16:14:45 +08: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 12:45:41 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 16:14:45 +08:00
|
|
|
/// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making
|
2011-01-21 01:19:02 +08: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 12:24:23 +08:00
|
|
|
llvm::Type *Ty) {
|
2011-01-21 01:19:02 +08: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");
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
2011-02-02 07:22:34 +08:00
|
|
|
|
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD))
|
|
|
|
return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD);
|
2013-07-19 16:14:45 +08:00
|
|
|
|
|
|
|
return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD);
|
2011-01-21 01:19:02 +08:00
|
|
|
}
|
|
|
|
|
2011-02-02 07:22:34 +08: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) {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD);
|
|
|
|
// 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.
|
2013-07-19 16:14:45 +08:00
|
|
|
const CGFunctionInfo &FInfo =
|
|
|
|
CGM.getTypes().arrangeCXXDestructor(DD, Dtor_Complete);
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo);
|
2013-07-19 16:14:45 +08:00
|
|
|
return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD);
|
2011-02-02 07:22:34 +08:00
|
|
|
}
|
2013-07-19 16:14:45 +08:00
|
|
|
return 0;
|
2011-02-02 07:22:34 +08:00
|
|
|
}
|