• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 &params) {
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 
registerGlobalDtor(CodeGenFunction & CGF,const VarDecl & D,llvm::Constant * dtor,llvm::Constant * addr)249 void CGCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
250                                   const VarDecl &D,
251                                   llvm::Constant *dtor,
252                                   llvm::Constant *addr) {
253   if (D.getTLSKind())
254     CGM.ErrorUnsupported(&D, "non-trivial TLS destruction");
255 
256   // The default behavior is to use atexit.
257   CGF.registerGlobalDtorWithAtExit(D, dtor, addr);
258 }
259 
260 /// Returns the adjustment, in bytes, required for the given
261 /// member-pointer operation.  Returns null if no adjustment is
262 /// required.
getMemberPointerAdjustment(const CastExpr * E)263 llvm::Constant *CGCXXABI::getMemberPointerAdjustment(const CastExpr *E) {
264   assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
265          E->getCastKind() == CK_BaseToDerivedMemberPointer);
266 
267   QualType derivedType;
268   if (E->getCastKind() == CK_DerivedToBaseMemberPointer)
269     derivedType = E->getSubExpr()->getType();
270   else
271     derivedType = E->getType();
272 
273   const CXXRecordDecl *derivedClass =
274     derivedType->castAs<MemberPointerType>()->getClass()->getAsCXXRecordDecl();
275 
276   return CGM.GetNonVirtualBaseClassOffset(derivedClass,
277                                           E->path_begin(),
278                                           E->path_end());
279 }
280 
getMemberPointerPathAdjustment(const APValue & MP)281 CharUnits CGCXXABI::getMemberPointerPathAdjustment(const APValue &MP) {
282   // TODO: Store base specifiers in APValue member pointer paths so we can
283   // easily reuse CGM.GetNonVirtualBaseClassOffset().
284   const ValueDecl *MPD = MP.getMemberPointerDecl();
285   CharUnits ThisAdjustment = CharUnits::Zero();
286   ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
287   bool DerivedMember = MP.isMemberPointerToDerivedMember();
288   const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
289   for (unsigned I = 0, N = Path.size(); I != N; ++I) {
290     const CXXRecordDecl *Base = RD;
291     const CXXRecordDecl *Derived = Path[I];
292     if (DerivedMember)
293       std::swap(Base, Derived);
294     ThisAdjustment +=
295       getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
296     RD = Path[I];
297   }
298   if (DerivedMember)
299     ThisAdjustment = -ThisAdjustment;
300   return ThisAdjustment;
301 }
302 
303 llvm::BasicBlock *
EmitCtorCompleteObjectHandler(CodeGenFunction & CGF,const CXXRecordDecl * RD)304 CGCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
305                                         const CXXRecordDecl *RD) {
306   if (CGM.getTarget().getCXXABI().hasConstructorVariants())
307     llvm_unreachable("shouldn't be called in this ABI");
308 
309   ErrorUnsupportedABI(CGF, "complete object detection in ctor");
310   return nullptr;
311 }
312 
EmitThreadLocalInitFuncs(ArrayRef<std::pair<const VarDecl *,llvm::GlobalVariable * >> Decls,llvm::Function * InitFunc)313 void CGCXXABI::EmitThreadLocalInitFuncs(
314     ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
315     llvm::Function *InitFunc) {
316 }
317 
EmitThreadLocalVarDeclLValue(CodeGenFunction & CGF,const VarDecl * VD,QualType LValType)318 LValue CGCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
319                                               const VarDecl *VD,
320                                               QualType LValType) {
321   ErrorUnsupportedABI(CGF, "odr-use of thread_local global");
322   return LValue();
323 }
324 
NeedsVTTParameter(GlobalDecl GD)325 bool CGCXXABI::NeedsVTTParameter(GlobalDecl GD) {
326   return false;
327 }
328