1 //===----- CGCXXABI.cpp - Interface to C++ ABIs ---------------------------===//
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 provides an abstract class for C++ code generation. Concrete subclasses
11 // of this implement code generation for specific C++ ABIs.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "CGCXXABI.h"
16
17 using namespace clang;
18 using namespace CodeGen;
19
~CGCXXABI()20 CGCXXABI::~CGCXXABI() { }
21
ErrorUnsupportedABI(CodeGenFunction & CGF,StringRef S)22 void CGCXXABI::ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S) {
23 DiagnosticsEngine &Diags = CGF.CGM.getDiags();
24 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
25 "cannot yet compile %0 in this ABI");
26 Diags.Report(CGF.getContext().getFullLoc(CGF.CurCodeDecl->getLocation()),
27 DiagID)
28 << S;
29 }
30
canCopyArgument(const CXXRecordDecl * RD) const31 bool CGCXXABI::canCopyArgument(const CXXRecordDecl *RD) const {
32 // If RD has a non-trivial move or copy constructor, we cannot copy the
33 // argument.
34 if (RD->hasNonTrivialCopyConstructor() || RD->hasNonTrivialMoveConstructor())
35 return false;
36
37 // If RD has a non-trivial destructor, we cannot copy the argument.
38 if (RD->hasNonTrivialDestructor())
39 return false;
40
41 // We can only copy the argument if there exists at least one trivial,
42 // non-deleted copy or move constructor.
43 // FIXME: This assumes that all lazily declared copy and move constructors are
44 // not deleted. This assumption might not be true in some corner cases.
45 bool CopyDeleted = false;
46 bool MoveDeleted = false;
47 for (const CXXConstructorDecl *CD : RD->ctors()) {
48 if (CD->isCopyConstructor() || CD->isMoveConstructor()) {
49 assert(CD->isTrivial());
50 // We had at least one undeleted trivial copy or move ctor. Return
51 // directly.
52 if (!CD->isDeleted())
53 return true;
54 if (CD->isCopyConstructor())
55 CopyDeleted = true;
56 else
57 MoveDeleted = true;
58 }
59 }
60
61 // If all trivial copy and move constructors are deleted, we cannot copy the
62 // argument.
63 return !(CopyDeleted && MoveDeleted);
64 }
65
GetBogusMemberPointer(QualType T)66 llvm::Constant *CGCXXABI::GetBogusMemberPointer(QualType T) {
67 return llvm::Constant::getNullValue(CGM.getTypes().ConvertType(T));
68 }
69
70 llvm::Type *
ConvertMemberPointerType(const MemberPointerType * MPT)71 CGCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
72 return CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
73 }
74
EmitLoadOfMemberFunctionPointer(CodeGenFunction & CGF,const Expr * E,llvm::Value * & This,llvm::Value * MemPtr,const MemberPointerType * MPT)75 llvm::Value *CGCXXABI::EmitLoadOfMemberFunctionPointer(
76 CodeGenFunction &CGF, const Expr *E, llvm::Value *&This,
77 llvm::Value *MemPtr, const MemberPointerType *MPT) {
78 ErrorUnsupportedABI(CGF, "calls through member pointers");
79
80 const FunctionProtoType *FPT =
81 MPT->getPointeeType()->getAs<FunctionProtoType>();
82 const CXXRecordDecl *RD =
83 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
84 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
85 CGM.getTypes().arrangeCXXMethodType(RD, FPT));
86 return llvm::Constant::getNullValue(FTy->getPointerTo());
87 }
88
89 llvm::Value *
EmitMemberDataPointerAddress(CodeGenFunction & CGF,const Expr * E,llvm::Value * Base,llvm::Value * MemPtr,const MemberPointerType * MPT)90 CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
91 llvm::Value *Base, llvm::Value *MemPtr,
92 const MemberPointerType *MPT) {
93 ErrorUnsupportedABI(CGF, "loads of member pointers");
94 llvm::Type *Ty = CGF.ConvertType(MPT->getPointeeType())->getPointerTo();
95 return llvm::Constant::getNullValue(Ty);
96 }
97
EmitMemberPointerConversion(CodeGenFunction & CGF,const CastExpr * E,llvm::Value * Src)98 llvm::Value *CGCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
99 const CastExpr *E,
100 llvm::Value *Src) {
101 ErrorUnsupportedABI(CGF, "member function pointer conversions");
102 return GetBogusMemberPointer(E->getType());
103 }
104
EmitMemberPointerConversion(const CastExpr * E,llvm::Constant * Src)105 llvm::Constant *CGCXXABI::EmitMemberPointerConversion(const CastExpr *E,
106 llvm::Constant *Src) {
107 return GetBogusMemberPointer(E->getType());
108 }
109
110 llvm::Value *
EmitMemberPointerComparison(CodeGenFunction & CGF,llvm::Value * L,llvm::Value * R,const MemberPointerType * MPT,bool Inequality)111 CGCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
112 llvm::Value *L,
113 llvm::Value *R,
114 const MemberPointerType *MPT,
115 bool Inequality) {
116 ErrorUnsupportedABI(CGF, "member function pointer comparison");
117 return CGF.Builder.getFalse();
118 }
119
120 llvm::Value *
EmitMemberPointerIsNotNull(CodeGenFunction & CGF,llvm::Value * MemPtr,const MemberPointerType * MPT)121 CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
122 llvm::Value *MemPtr,
123 const MemberPointerType *MPT) {
124 ErrorUnsupportedABI(CGF, "member function pointer null testing");
125 return CGF.Builder.getFalse();
126 }
127
128 llvm::Constant *
EmitNullMemberPointer(const MemberPointerType * MPT)129 CGCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
130 return GetBogusMemberPointer(QualType(MPT, 0));
131 }
132
EmitMemberPointer(const CXXMethodDecl * MD)133 llvm::Constant *CGCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
134 return GetBogusMemberPointer(
135 CGM.getContext().getMemberPointerType(MD->getType(),
136 MD->getParent()->getTypeForDecl()));
137 }
138
EmitMemberDataPointer(const MemberPointerType * MPT,CharUnits offset)139 llvm::Constant *CGCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
140 CharUnits offset) {
141 return GetBogusMemberPointer(QualType(MPT, 0));
142 }
143
EmitMemberPointer(const APValue & MP,QualType MPT)144 llvm::Constant *CGCXXABI::EmitMemberPointer(const APValue &MP, QualType MPT) {
145 return GetBogusMemberPointer(MPT);
146 }
147
isZeroInitializable(const MemberPointerType * MPT)148 bool CGCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
149 // Fake answer.
150 return true;
151 }
152
buildThisParam(CodeGenFunction & CGF,FunctionArgList & params)153 void CGCXXABI::buildThisParam(CodeGenFunction &CGF, FunctionArgList ¶ms) {
154 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
155
156 // FIXME: I'm not entirely sure I like using a fake decl just for code
157 // generation. Maybe we can come up with a better way?
158 ImplicitParamDecl *ThisDecl
159 = ImplicitParamDecl::Create(CGM.getContext(), nullptr, MD->getLocation(),
160 &CGM.getContext().Idents.get("this"),
161 MD->getThisType(CGM.getContext()));
162 params.push_back(ThisDecl);
163 getThisDecl(CGF) = ThisDecl;
164 }
165
EmitThisParam(CodeGenFunction & CGF)166 void CGCXXABI::EmitThisParam(CodeGenFunction &CGF) {
167 /// Initialize the 'this' slot.
168 assert(getThisDecl(CGF) && "no 'this' variable for function");
169 getThisValue(CGF)
170 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getThisDecl(CGF)),
171 "this");
172 }
173
EmitReturnFromThunk(CodeGenFunction & CGF,RValue RV,QualType ResultType)174 void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
175 RValue RV, QualType ResultType) {
176 CGF.EmitReturnOfRValue(RV, ResultType);
177 }
178
GetArrayCookieSize(const CXXNewExpr * expr)179 CharUnits CGCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
180 if (!requiresArrayCookie(expr))
181 return CharUnits::Zero();
182 return getArrayCookieSizeImpl(expr->getAllocatedType());
183 }
184
getArrayCookieSizeImpl(QualType elementType)185 CharUnits CGCXXABI::getArrayCookieSizeImpl(QualType elementType) {
186 // BOGUS
187 return CharUnits::Zero();
188 }
189
InitializeArrayCookie(CodeGenFunction & CGF,llvm::Value * NewPtr,llvm::Value * NumElements,const CXXNewExpr * expr,QualType ElementType)190 llvm::Value *CGCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
191 llvm::Value *NewPtr,
192 llvm::Value *NumElements,
193 const CXXNewExpr *expr,
194 QualType ElementType) {
195 // Should never be called.
196 ErrorUnsupportedABI(CGF, "array cookie initialization");
197 return nullptr;
198 }
199
requiresArrayCookie(const CXXDeleteExpr * expr,QualType elementType)200 bool CGCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
201 QualType elementType) {
202 // If the class's usual deallocation function takes two arguments,
203 // it needs a cookie.
204 if (expr->doesUsualArrayDeleteWantSize())
205 return true;
206
207 return elementType.isDestructedType();
208 }
209
requiresArrayCookie(const CXXNewExpr * expr)210 bool CGCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
211 // If the class's usual deallocation function takes two arguments,
212 // it needs a cookie.
213 if (expr->doesUsualArrayDeleteWantSize())
214 return true;
215
216 return expr->getAllocatedType().isDestructedType();
217 }
218
ReadArrayCookie(CodeGenFunction & CGF,llvm::Value * ptr,const CXXDeleteExpr * expr,QualType eltTy,llvm::Value * & numElements,llvm::Value * & allocPtr,CharUnits & cookieSize)219 void CGCXXABI::ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *ptr,
220 const CXXDeleteExpr *expr, QualType eltTy,
221 llvm::Value *&numElements,
222 llvm::Value *&allocPtr, CharUnits &cookieSize) {
223 // Derive a char* in the same address space as the pointer.
224 unsigned AS = ptr->getType()->getPointerAddressSpace();
225 llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
226 ptr = CGF.Builder.CreateBitCast(ptr, charPtrTy);
227
228 // If we don't need an array cookie, bail out early.
229 if (!requiresArrayCookie(expr, eltTy)) {
230 allocPtr = ptr;
231 numElements = nullptr;
232 cookieSize = CharUnits::Zero();
233 return;
234 }
235
236 cookieSize = getArrayCookieSizeImpl(eltTy);
237 allocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ptr,
238 -cookieSize.getQuantity());
239 numElements = readArrayCookieImpl(CGF, allocPtr, cookieSize);
240 }
241
readArrayCookieImpl(CodeGenFunction & CGF,llvm::Value * ptr,CharUnits cookieSize)242 llvm::Value *CGCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
243 llvm::Value *ptr,
244 CharUnits cookieSize) {
245 ErrorUnsupportedABI(CGF, "reading a new[] cookie");
246 return llvm::ConstantInt::get(CGF.SizeTy, 0);
247 }
248
249 /// Returns the adjustment, in bytes, required for the given
250 /// member-pointer operation. Returns null if no adjustment is
251 /// required.
getMemberPointerAdjustment(const CastExpr * E)252 llvm::Constant *CGCXXABI::getMemberPointerAdjustment(const CastExpr *E) {
253 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
254 E->getCastKind() == CK_BaseToDerivedMemberPointer);
255
256 QualType derivedType;
257 if (E->getCastKind() == CK_DerivedToBaseMemberPointer)
258 derivedType = E->getSubExpr()->getType();
259 else
260 derivedType = E->getType();
261
262 const CXXRecordDecl *derivedClass =
263 derivedType->castAs<MemberPointerType>()->getClass()->getAsCXXRecordDecl();
264
265 return CGM.GetNonVirtualBaseClassOffset(derivedClass,
266 E->path_begin(),
267 E->path_end());
268 }
269
getMemberPointerPathAdjustment(const APValue & MP)270 CharUnits CGCXXABI::getMemberPointerPathAdjustment(const APValue &MP) {
271 // TODO: Store base specifiers in APValue member pointer paths so we can
272 // easily reuse CGM.GetNonVirtualBaseClassOffset().
273 const ValueDecl *MPD = MP.getMemberPointerDecl();
274 CharUnits ThisAdjustment = CharUnits::Zero();
275 ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
276 bool DerivedMember = MP.isMemberPointerToDerivedMember();
277 const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
278 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
279 const CXXRecordDecl *Base = RD;
280 const CXXRecordDecl *Derived = Path[I];
281 if (DerivedMember)
282 std::swap(Base, Derived);
283 ThisAdjustment +=
284 getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
285 RD = Path[I];
286 }
287 if (DerivedMember)
288 ThisAdjustment = -ThisAdjustment;
289 return ThisAdjustment;
290 }
291
292 llvm::BasicBlock *
EmitCtorCompleteObjectHandler(CodeGenFunction & CGF,const CXXRecordDecl * RD)293 CGCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
294 const CXXRecordDecl *RD) {
295 if (CGM.getTarget().getCXXABI().hasConstructorVariants())
296 llvm_unreachable("shouldn't be called in this ABI");
297
298 ErrorUnsupportedABI(CGF, "complete object detection in ctor");
299 return nullptr;
300 }
301
NeedsVTTParameter(GlobalDecl GD)302 bool CGCXXABI::NeedsVTTParameter(GlobalDecl GD) {
303 return false;
304 }
305
306 llvm::CallInst *
emitTerminateForUnexpectedException(CodeGenFunction & CGF,llvm::Value * Exn)307 CGCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF,
308 llvm::Value *Exn) {
309 // Just call std::terminate and ignore the violating exception.
310 return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
311 }
312