1//===----- CGCXXABI.cpp - Interface to C++ ABIs -----------------*- C++ -*-===//
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
17using namespace clang;
18using namespace CodeGen;
19
20CGCXXABI::~CGCXXABI() { }
21
22static void ErrorUnsupportedABI(CodeGenFunction &CGF,
23                                StringRef S) {
24  DiagnosticsEngine &Diags = CGF.CGM.getDiags();
25  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
26                                          "cannot yet compile %0 in this ABI");
27  Diags.Report(CGF.getContext().getFullLoc(CGF.CurCodeDecl->getLocation()),
28               DiagID)
29    << S;
30}
31
32static llvm::Constant *GetBogusMemberPointer(CodeGenModule &CGM,
33                                             QualType T) {
34  return llvm::Constant::getNullValue(CGM.getTypes().ConvertType(T));
35}
36
37llvm::Type *
38CGCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
39  return CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
40}
41
42llvm::Value *CGCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
43                                                       llvm::Value *&This,
44                                                       llvm::Value *MemPtr,
45                                                 const MemberPointerType *MPT) {
46  ErrorUnsupportedABI(CGF, "calls through member pointers");
47
48  const FunctionProtoType *FPT =
49    MPT->getPointeeType()->getAs<FunctionProtoType>();
50  const CXXRecordDecl *RD =
51    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
52  llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
53                              CGM.getTypes().arrangeCXXMethodType(RD, FPT));
54  return llvm::Constant::getNullValue(FTy->getPointerTo());
55}
56
57llvm::Value *CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
58                                                    llvm::Value *Base,
59                                                    llvm::Value *MemPtr,
60                                              const MemberPointerType *MPT) {
61  ErrorUnsupportedABI(CGF, "loads of member pointers");
62  llvm::Type *Ty = CGF.ConvertType(MPT->getPointeeType())->getPointerTo();
63  return llvm::Constant::getNullValue(Ty);
64}
65
66llvm::Value *CGCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
67                                                   const CastExpr *E,
68                                                   llvm::Value *Src) {
69  ErrorUnsupportedABI(CGF, "member function pointer conversions");
70  return GetBogusMemberPointer(CGM, E->getType());
71}
72
73llvm::Constant *CGCXXABI::EmitMemberPointerConversion(const CastExpr *E,
74                                                      llvm::Constant *Src) {
75  return GetBogusMemberPointer(CGM, E->getType());
76}
77
78llvm::Value *
79CGCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
80                                      llvm::Value *L,
81                                      llvm::Value *R,
82                                      const MemberPointerType *MPT,
83                                      bool Inequality) {
84  ErrorUnsupportedABI(CGF, "member function pointer comparison");
85  return CGF.Builder.getFalse();
86}
87
88llvm::Value *
89CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
90                                     llvm::Value *MemPtr,
91                                     const MemberPointerType *MPT) {
92  ErrorUnsupportedABI(CGF, "member function pointer null testing");
93  return CGF.Builder.getFalse();
94}
95
96llvm::Constant *
97CGCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
98  return GetBogusMemberPointer(CGM, QualType(MPT, 0));
99}
100
101llvm::Constant *CGCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
102  return GetBogusMemberPointer(CGM,
103                         CGM.getContext().getMemberPointerType(MD->getType(),
104                                         MD->getParent()->getTypeForDecl()));
105}
106
107llvm::Constant *CGCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
108                                                CharUnits offset) {
109  return GetBogusMemberPointer(CGM, QualType(MPT, 0));
110}
111
112llvm::Constant *CGCXXABI::EmitMemberPointer(const APValue &MP, QualType MPT) {
113  return GetBogusMemberPointer(CGM, MPT);
114}
115
116bool CGCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
117  // Fake answer.
118  return true;
119}
120
121void CGCXXABI::BuildThisParam(CodeGenFunction &CGF, FunctionArgList &params) {
122  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
123
124  // FIXME: I'm not entirely sure I like using a fake decl just for code
125  // generation. Maybe we can come up with a better way?
126  ImplicitParamDecl *ThisDecl
127    = ImplicitParamDecl::Create(CGM.getContext(), 0, MD->getLocation(),
128                                &CGM.getContext().Idents.get("this"),
129                                MD->getThisType(CGM.getContext()));
130  params.push_back(ThisDecl);
131  getThisDecl(CGF) = ThisDecl;
132}
133
134void CGCXXABI::EmitThisParam(CodeGenFunction &CGF) {
135  /// Initialize the 'this' slot.
136  assert(getThisDecl(CGF) && "no 'this' variable for function");
137  getThisValue(CGF)
138    = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getThisDecl(CGF)),
139                             "this");
140}
141
142void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
143                                   RValue RV, QualType ResultType) {
144  CGF.EmitReturnOfRValue(RV, ResultType);
145}
146
147CharUnits CGCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
148  if (!requiresArrayCookie(expr))
149    return CharUnits::Zero();
150  return getArrayCookieSizeImpl(expr->getAllocatedType());
151}
152
153CharUnits CGCXXABI::getArrayCookieSizeImpl(QualType elementType) {
154  // BOGUS
155  return CharUnits::Zero();
156}
157
158llvm::Value *CGCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
159                                             llvm::Value *NewPtr,
160                                             llvm::Value *NumElements,
161                                             const CXXNewExpr *expr,
162                                             QualType ElementType) {
163  // Should never be called.
164  ErrorUnsupportedABI(CGF, "array cookie initialization");
165  return 0;
166}
167
168bool CGCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
169                                   QualType elementType) {
170  // If the class's usual deallocation function takes two arguments,
171  // it needs a cookie.
172  if (expr->doesUsualArrayDeleteWantSize())
173    return true;
174
175  return elementType.isDestructedType();
176}
177
178bool CGCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
179  // If the class's usual deallocation function takes two arguments,
180  // it needs a cookie.
181  if (expr->doesUsualArrayDeleteWantSize())
182    return true;
183
184  return expr->getAllocatedType().isDestructedType();
185}
186
187void CGCXXABI::ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *ptr,
188                               const CXXDeleteExpr *expr, QualType eltTy,
189                               llvm::Value *&numElements,
190                               llvm::Value *&allocPtr, CharUnits &cookieSize) {
191  // Derive a char* in the same address space as the pointer.
192  unsigned AS = ptr->getType()->getPointerAddressSpace();
193  llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
194  ptr = CGF.Builder.CreateBitCast(ptr, charPtrTy);
195
196  // If we don't need an array cookie, bail out early.
197  if (!requiresArrayCookie(expr, eltTy)) {
198    allocPtr = ptr;
199    numElements = 0;
200    cookieSize = CharUnits::Zero();
201    return;
202  }
203
204  cookieSize = getArrayCookieSizeImpl(eltTy);
205  allocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ptr,
206                                                    -cookieSize.getQuantity());
207  numElements = readArrayCookieImpl(CGF, allocPtr, cookieSize);
208}
209
210llvm::Value *CGCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
211                                           llvm::Value *ptr,
212                                           CharUnits cookieSize) {
213  ErrorUnsupportedABI(CGF, "reading a new[] cookie");
214  return llvm::ConstantInt::get(CGF.SizeTy, 0);
215}
216
217void CGCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
218                               const VarDecl &D,
219                               llvm::GlobalVariable *GV,
220                               bool PerformInit) {
221  ErrorUnsupportedABI(CGF, "static local variable initialization");
222}
223
224void CGCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
225                                  llvm::Constant *dtor,
226                                  llvm::Constant *addr) {
227  // The default behavior is to use atexit.
228  CGF.registerGlobalDtorWithAtExit(dtor, addr);
229}
230
231/// Returns the adjustment, in bytes, required for the given
232/// member-pointer operation.  Returns null if no adjustment is
233/// required.
234llvm::Constant *CGCXXABI::getMemberPointerAdjustment(const CastExpr *E) {
235  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
236         E->getCastKind() == CK_BaseToDerivedMemberPointer);
237
238  QualType derivedType;
239  if (E->getCastKind() == CK_DerivedToBaseMemberPointer)
240    derivedType = E->getSubExpr()->getType();
241  else
242    derivedType = E->getType();
243
244  const CXXRecordDecl *derivedClass =
245    derivedType->castAs<MemberPointerType>()->getClass()->getAsCXXRecordDecl();
246
247  return CGM.GetNonVirtualBaseClassOffset(derivedClass,
248                                          E->path_begin(),
249                                          E->path_end());
250}
251
252llvm::BasicBlock *CGCXXABI::EmitCtorCompleteObjectHandler(
253                                                         CodeGenFunction &CGF) {
254  if (CGM.getTarget().getCXXABI().hasConstructorVariants())
255    llvm_unreachable("shouldn't be called in this ABI");
256
257  ErrorUnsupportedABI(CGF, "complete object detection in ctor");
258  return 0;
259}
260