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 17 using namespace clang; 18 using namespace CodeGen; 19 20 CGCXXABI::~CGCXXABI() { } 21 22 static void ErrorUnsupportedABI(CodeGenFunction &CGF, 23 StringRef S) { 24 DiagnosticsEngine &Diags = CGF.CGM.getDiags(); 25 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, 26 "cannot yet compile %1 in this ABI"); 27 Diags.Report(CGF.getContext().getFullLoc(CGF.CurCodeDecl->getLocation()), 28 DiagID) 29 << S; 30 } 31 32 static llvm::Constant *GetBogusMemberPointer(CodeGenModule &CGM, 33 QualType T) { 34 return llvm::Constant::getNullValue(CGM.getTypes().ConvertType(T)); 35 } 36 37 llvm::Type * 38 CGCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { 39 return CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); 40 } 41 42 llvm::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 = 53 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), 54 FPT->isVariadic()); 55 return llvm::Constant::getNullValue(FTy->getPointerTo()); 56 } 57 58 llvm::Value *CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, 59 llvm::Value *Base, 60 llvm::Value *MemPtr, 61 const MemberPointerType *MPT) { 62 ErrorUnsupportedABI(CGF, "loads of member pointers"); 63 llvm::Type *Ty = CGF.ConvertType(MPT->getPointeeType())->getPointerTo(); 64 return llvm::Constant::getNullValue(Ty); 65 } 66 67 llvm::Value *CGCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, 68 const CastExpr *E, 69 llvm::Value *Src) { 70 ErrorUnsupportedABI(CGF, "member function pointer conversions"); 71 return GetBogusMemberPointer(CGM, E->getType()); 72 } 73 74 llvm::Value * 75 CGCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, 76 llvm::Value *L, 77 llvm::Value *R, 78 const MemberPointerType *MPT, 79 bool Inequality) { 80 ErrorUnsupportedABI(CGF, "member function pointer comparison"); 81 return CGF.Builder.getFalse(); 82 } 83 84 llvm::Value * 85 CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 86 llvm::Value *MemPtr, 87 const MemberPointerType *MPT) { 88 ErrorUnsupportedABI(CGF, "member function pointer null testing"); 89 return CGF.Builder.getFalse(); 90 } 91 92 llvm::Constant * 93 CGCXXABI::EmitMemberPointerConversion(llvm::Constant *C, const CastExpr *E) { 94 return GetBogusMemberPointer(CGM, E->getType()); 95 } 96 97 llvm::Constant * 98 CGCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { 99 return GetBogusMemberPointer(CGM, QualType(MPT, 0)); 100 } 101 102 llvm::Constant *CGCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { 103 return GetBogusMemberPointer(CGM, 104 CGM.getContext().getMemberPointerType(MD->getType(), 105 MD->getParent()->getTypeForDecl())); 106 } 107 108 llvm::Constant *CGCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, 109 CharUnits offset) { 110 return GetBogusMemberPointer(CGM, QualType(MPT, 0)); 111 } 112 113 bool CGCXXABI::isZeroInitializable(const MemberPointerType *MPT) { 114 // Fake answer. 115 return true; 116 } 117 118 void CGCXXABI::BuildThisParam(CodeGenFunction &CGF, FunctionArgList ¶ms) { 119 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 120 121 // FIXME: I'm not entirely sure I like using a fake decl just for code 122 // generation. Maybe we can come up with a better way? 123 ImplicitParamDecl *ThisDecl 124 = ImplicitParamDecl::Create(CGM.getContext(), 0, MD->getLocation(), 125 &CGM.getContext().Idents.get("this"), 126 MD->getThisType(CGM.getContext())); 127 params.push_back(ThisDecl); 128 getThisDecl(CGF) = ThisDecl; 129 } 130 131 void CGCXXABI::EmitThisParam(CodeGenFunction &CGF) { 132 /// Initialize the 'this' slot. 133 assert(getThisDecl(CGF) && "no 'this' variable for function"); 134 getThisValue(CGF) 135 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getThisDecl(CGF)), 136 "this"); 137 } 138 139 void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, 140 RValue RV, QualType ResultType) { 141 CGF.EmitReturnOfRValue(RV, ResultType); 142 } 143 144 CharUnits CGCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) { 145 return CharUnits::Zero(); 146 } 147 148 llvm::Value *CGCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 149 llvm::Value *NewPtr, 150 llvm::Value *NumElements, 151 const CXXNewExpr *expr, 152 QualType ElementType) { 153 // Should never be called. 154 ErrorUnsupportedABI(CGF, "array cookie initialization"); 155 return 0; 156 } 157 158 void CGCXXABI::ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, 159 const CXXDeleteExpr *expr, QualType ElementType, 160 llvm::Value *&NumElements, 161 llvm::Value *&AllocPtr, CharUnits &CookieSize) { 162 ErrorUnsupportedABI(CGF, "array cookie reading"); 163 164 // This should be enough to avoid assertions. 165 NumElements = 0; 166 AllocPtr = llvm::Constant::getNullValue(CGF.Builder.getInt8PtrTy()); 167 CookieSize = CharUnits::Zero(); 168 } 169 170 void CGCXXABI::EmitGuardedInit(CodeGenFunction &CGF, 171 const VarDecl &D, 172 llvm::GlobalVariable *GV) { 173 ErrorUnsupportedABI(CGF, "static local variable initialization"); 174 } 175