clang API Documentation

CGCXXABI.cpp
Go to the documentation of this file.
00001 //===----- CGCXXABI.cpp - Interface to C++ ABIs ---------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This provides an abstract class for C++ code generation. Concrete subclasses
00011 // of this implement code generation for specific C++ ABIs.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "CGCXXABI.h"
00016 
00017 using namespace clang;
00018 using namespace CodeGen;
00019 
00020 CGCXXABI::~CGCXXABI() { }
00021 
00022 void CGCXXABI::ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S) {
00023   DiagnosticsEngine &Diags = CGF.CGM.getDiags();
00024   unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
00025                                           "cannot yet compile %0 in this ABI");
00026   Diags.Report(CGF.getContext().getFullLoc(CGF.CurCodeDecl->getLocation()),
00027                DiagID)
00028     << S;
00029 }
00030 
00031 bool CGCXXABI::canCopyArgument(const CXXRecordDecl *RD) const {
00032   // If RD has a non-trivial move or copy constructor, we cannot copy the
00033   // argument.
00034   if (RD->hasNonTrivialCopyConstructor() || RD->hasNonTrivialMoveConstructor())
00035     return false;
00036 
00037   // If RD has a non-trivial destructor, we cannot copy the argument.
00038   if (RD->hasNonTrivialDestructor())
00039     return false;
00040 
00041   // We can only copy the argument if there exists at least one trivial,
00042   // non-deleted copy or move constructor.
00043   // FIXME: This assumes that all lazily declared copy and move constructors are
00044   // not deleted.  This assumption might not be true in some corner cases.
00045   bool CopyDeleted = false;
00046   bool MoveDeleted = false;
00047   for (const CXXConstructorDecl *CD : RD->ctors()) {
00048     if (CD->isCopyConstructor() || CD->isMoveConstructor()) {
00049       assert(CD->isTrivial());
00050       // We had at least one undeleted trivial copy or move ctor.  Return
00051       // directly.
00052       if (!CD->isDeleted())
00053         return true;
00054       if (CD->isCopyConstructor())
00055         CopyDeleted = true;
00056       else
00057         MoveDeleted = true;
00058     }
00059   }
00060 
00061   // If all trivial copy and move constructors are deleted, we cannot copy the
00062   // argument.
00063   return !(CopyDeleted && MoveDeleted);
00064 }
00065 
00066 llvm::Constant *CGCXXABI::GetBogusMemberPointer(QualType T) {
00067   return llvm::Constant::getNullValue(CGM.getTypes().ConvertType(T));
00068 }
00069 
00070 llvm::Type *
00071 CGCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
00072   return CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
00073 }
00074 
00075 llvm::Value *CGCXXABI::EmitLoadOfMemberFunctionPointer(
00076     CodeGenFunction &CGF, const Expr *E, llvm::Value *&This,
00077     llvm::Value *MemPtr, const MemberPointerType *MPT) {
00078   ErrorUnsupportedABI(CGF, "calls through member pointers");
00079 
00080   const FunctionProtoType *FPT = 
00081     MPT->getPointeeType()->getAs<FunctionProtoType>();
00082   const CXXRecordDecl *RD = 
00083     cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
00084   llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
00085                               CGM.getTypes().arrangeCXXMethodType(RD, FPT));
00086   return llvm::Constant::getNullValue(FTy->getPointerTo());
00087 }
00088 
00089 llvm::Value *
00090 CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
00091                                        llvm::Value *Base, llvm::Value *MemPtr,
00092                                        const MemberPointerType *MPT) {
00093   ErrorUnsupportedABI(CGF, "loads of member pointers");
00094   llvm::Type *Ty = CGF.ConvertType(MPT->getPointeeType())->getPointerTo();
00095   return llvm::Constant::getNullValue(Ty);
00096 }
00097 
00098 llvm::Value *CGCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
00099                                                    const CastExpr *E,
00100                                                    llvm::Value *Src) {
00101   ErrorUnsupportedABI(CGF, "member function pointer conversions");
00102   return GetBogusMemberPointer(E->getType());
00103 }
00104 
00105 llvm::Constant *CGCXXABI::EmitMemberPointerConversion(const CastExpr *E,
00106                                                       llvm::Constant *Src) {
00107   return GetBogusMemberPointer(E->getType());
00108 }
00109 
00110 llvm::Value *
00111 CGCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
00112                                       llvm::Value *L,
00113                                       llvm::Value *R,
00114                                       const MemberPointerType *MPT,
00115                                       bool Inequality) {
00116   ErrorUnsupportedABI(CGF, "member function pointer comparison");
00117   return CGF.Builder.getFalse();
00118 }
00119 
00120 llvm::Value *
00121 CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
00122                                      llvm::Value *MemPtr,
00123                                      const MemberPointerType *MPT) {
00124   ErrorUnsupportedABI(CGF, "member function pointer null testing");
00125   return CGF.Builder.getFalse();
00126 }
00127 
00128 llvm::Constant *
00129 CGCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
00130   return GetBogusMemberPointer(QualType(MPT, 0));
00131 }
00132 
00133 llvm::Constant *CGCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
00134   return GetBogusMemberPointer(
00135                          CGM.getContext().getMemberPointerType(MD->getType(),
00136                                          MD->getParent()->getTypeForDecl()));
00137 }
00138 
00139 llvm::Constant *CGCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
00140                                                 CharUnits offset) {
00141   return GetBogusMemberPointer(QualType(MPT, 0));
00142 }
00143 
00144 llvm::Constant *CGCXXABI::EmitMemberPointer(const APValue &MP, QualType MPT) {
00145   return GetBogusMemberPointer(MPT);
00146 }
00147 
00148 bool CGCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
00149   // Fake answer.
00150   return true;
00151 }
00152 
00153 void CGCXXABI::buildThisParam(CodeGenFunction &CGF, FunctionArgList &params) {
00154   const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
00155 
00156   // FIXME: I'm not entirely sure I like using a fake decl just for code
00157   // generation. Maybe we can come up with a better way?
00158   ImplicitParamDecl *ThisDecl
00159     = ImplicitParamDecl::Create(CGM.getContext(), nullptr, MD->getLocation(),
00160                                 &CGM.getContext().Idents.get("this"),
00161                                 MD->getThisType(CGM.getContext()));
00162   params.push_back(ThisDecl);
00163   getThisDecl(CGF) = ThisDecl;
00164 }
00165 
00166 void CGCXXABI::EmitThisParam(CodeGenFunction &CGF) {
00167   /// Initialize the 'this' slot.
00168   assert(getThisDecl(CGF) && "no 'this' variable for function");
00169   getThisValue(CGF)
00170     = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getThisDecl(CGF)),
00171                              "this");
00172 }
00173 
00174 void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
00175                                    RValue RV, QualType ResultType) {
00176   CGF.EmitReturnOfRValue(RV, ResultType);
00177 }
00178 
00179 CharUnits CGCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
00180   if (!requiresArrayCookie(expr))
00181     return CharUnits::Zero();
00182   return getArrayCookieSizeImpl(expr->getAllocatedType());
00183 }
00184 
00185 CharUnits CGCXXABI::getArrayCookieSizeImpl(QualType elementType) {
00186   // BOGUS
00187   return CharUnits::Zero();
00188 }
00189 
00190 llvm::Value *CGCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
00191                                              llvm::Value *NewPtr,
00192                                              llvm::Value *NumElements,
00193                                              const CXXNewExpr *expr,
00194                                              QualType ElementType) {
00195   // Should never be called.
00196   ErrorUnsupportedABI(CGF, "array cookie initialization");
00197   return nullptr;
00198 }
00199 
00200 bool CGCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
00201                                    QualType elementType) {
00202   // If the class's usual deallocation function takes two arguments,
00203   // it needs a cookie.
00204   if (expr->doesUsualArrayDeleteWantSize())
00205     return true;
00206 
00207   return elementType.isDestructedType();
00208 }
00209 
00210 bool CGCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
00211   // If the class's usual deallocation function takes two arguments,
00212   // it needs a cookie.
00213   if (expr->doesUsualArrayDeleteWantSize())
00214     return true;
00215 
00216   return expr->getAllocatedType().isDestructedType();
00217 }
00218 
00219 void CGCXXABI::ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *ptr,
00220                                const CXXDeleteExpr *expr, QualType eltTy,
00221                                llvm::Value *&numElements,
00222                                llvm::Value *&allocPtr, CharUnits &cookieSize) {
00223   // Derive a char* in the same address space as the pointer.
00224   unsigned AS = ptr->getType()->getPointerAddressSpace();
00225   llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
00226   ptr = CGF.Builder.CreateBitCast(ptr, charPtrTy);
00227 
00228   // If we don't need an array cookie, bail out early.
00229   if (!requiresArrayCookie(expr, eltTy)) {
00230     allocPtr = ptr;
00231     numElements = nullptr;
00232     cookieSize = CharUnits::Zero();
00233     return;
00234   }
00235 
00236   cookieSize = getArrayCookieSizeImpl(eltTy);
00237   allocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ptr,
00238                                                     -cookieSize.getQuantity());
00239   numElements = readArrayCookieImpl(CGF, allocPtr, cookieSize);
00240 }
00241 
00242 llvm::Value *CGCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
00243                                            llvm::Value *ptr,
00244                                            CharUnits cookieSize) {
00245   ErrorUnsupportedABI(CGF, "reading a new[] cookie");
00246   return llvm::ConstantInt::get(CGF.SizeTy, 0);
00247 }
00248 
00249 /// Returns the adjustment, in bytes, required for the given
00250 /// member-pointer operation.  Returns null if no adjustment is
00251 /// required.
00252 llvm::Constant *CGCXXABI::getMemberPointerAdjustment(const CastExpr *E) {
00253   assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
00254          E->getCastKind() == CK_BaseToDerivedMemberPointer);
00255 
00256   QualType derivedType;
00257   if (E->getCastKind() == CK_DerivedToBaseMemberPointer)
00258     derivedType = E->getSubExpr()->getType();
00259   else
00260     derivedType = E->getType();
00261 
00262   const CXXRecordDecl *derivedClass =
00263     derivedType->castAs<MemberPointerType>()->getClass()->getAsCXXRecordDecl();
00264 
00265   return CGM.GetNonVirtualBaseClassOffset(derivedClass,
00266                                           E->path_begin(),
00267                                           E->path_end());
00268 }
00269 
00270 CharUnits CGCXXABI::getMemberPointerPathAdjustment(const APValue &MP) {
00271   // TODO: Store base specifiers in APValue member pointer paths so we can
00272   // easily reuse CGM.GetNonVirtualBaseClassOffset().
00273   const ValueDecl *MPD = MP.getMemberPointerDecl();
00274   CharUnits ThisAdjustment = CharUnits::Zero();
00275   ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
00276   bool DerivedMember = MP.isMemberPointerToDerivedMember();
00277   const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
00278   for (unsigned I = 0, N = Path.size(); I != N; ++I) {
00279     const CXXRecordDecl *Base = RD;
00280     const CXXRecordDecl *Derived = Path[I];
00281     if (DerivedMember)
00282       std::swap(Base, Derived);
00283     ThisAdjustment +=
00284       getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
00285     RD = Path[I];
00286   }
00287   if (DerivedMember)
00288     ThisAdjustment = -ThisAdjustment;
00289   return ThisAdjustment;
00290 }
00291 
00292 llvm::BasicBlock *
00293 CGCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
00294                                         const CXXRecordDecl *RD) {
00295   if (CGM.getTarget().getCXXABI().hasConstructorVariants())
00296     llvm_unreachable("shouldn't be called in this ABI");
00297 
00298   ErrorUnsupportedABI(CGF, "complete object detection in ctor");
00299   return nullptr;
00300 }
00301 
00302 bool CGCXXABI::NeedsVTTParameter(GlobalDecl GD) {
00303   return false;
00304 }