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
~CGCXXABI()20 CGCXXABI::~CGCXXABI() { }
21
ErrorUnsupportedABI(CodeGenFunction & CGF,StringRef S)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
GetBogusMemberPointer(CodeGenModule & CGM,QualType T)32 static llvm::Constant *GetBogusMemberPointer(CodeGenModule &CGM,
33 QualType T) {
34 return llvm::Constant::getNullValue(CGM.getTypes().ConvertType(T));
35 }
36
37 llvm::Type *
ConvertMemberPointerType(const MemberPointerType * MPT)38 CGCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
39 return CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
40 }
41
EmitLoadOfMemberFunctionPointer(CodeGenFunction & CGF,llvm::Value * & This,llvm::Value * MemPtr,const MemberPointerType * MPT)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 = CGM.getTypes().GetFunctionType(
53 CGM.getTypes().arrangeCXXMethodType(RD, FPT));
54 return llvm::Constant::getNullValue(FTy->getPointerTo());
55 }
56
EmitMemberDataPointerAddress(CodeGenFunction & CGF,llvm::Value * Base,llvm::Value * MemPtr,const MemberPointerType * MPT)57 llvm::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
EmitMemberPointerConversion(CodeGenFunction & CGF,const CastExpr * E,llvm::Value * Src)66 llvm::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
EmitMemberPointerConversion(const CastExpr * E,llvm::Constant * Src)73 llvm::Constant *CGCXXABI::EmitMemberPointerConversion(const CastExpr *E,
74 llvm::Constant *Src) {
75 return GetBogusMemberPointer(CGM, E->getType());
76 }
77
78 llvm::Value *
EmitMemberPointerComparison(CodeGenFunction & CGF,llvm::Value * L,llvm::Value * R,const MemberPointerType * MPT,bool Inequality)79 CGCXXABI::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
88 llvm::Value *
EmitMemberPointerIsNotNull(CodeGenFunction & CGF,llvm::Value * MemPtr,const MemberPointerType * MPT)89 CGCXXABI::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
96 llvm::Constant *
EmitNullMemberPointer(const MemberPointerType * MPT)97 CGCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
98 return GetBogusMemberPointer(CGM, QualType(MPT, 0));
99 }
100
EmitMemberPointer(const CXXMethodDecl * MD)101 llvm::Constant *CGCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
102 return GetBogusMemberPointer(CGM,
103 CGM.getContext().getMemberPointerType(MD->getType(),
104 MD->getParent()->getTypeForDecl()));
105 }
106
EmitMemberDataPointer(const MemberPointerType * MPT,CharUnits offset)107 llvm::Constant *CGCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
108 CharUnits offset) {
109 return GetBogusMemberPointer(CGM, QualType(MPT, 0));
110 }
111
EmitMemberPointer(const APValue & MP,QualType MPT)112 llvm::Constant *CGCXXABI::EmitMemberPointer(const APValue &MP, QualType MPT) {
113 return GetBogusMemberPointer(CGM, MPT);
114 }
115
isZeroInitializable(const MemberPointerType * MPT)116 bool CGCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
117 // Fake answer.
118 return true;
119 }
120
BuildThisParam(CodeGenFunction & CGF,FunctionArgList & params)121 void CGCXXABI::BuildThisParam(CodeGenFunction &CGF, FunctionArgList ¶ms) {
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
EmitThisParam(CodeGenFunction & CGF)134 void 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
EmitReturnFromThunk(CodeGenFunction & CGF,RValue RV,QualType ResultType)142 void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
143 RValue RV, QualType ResultType) {
144 CGF.EmitReturnOfRValue(RV, ResultType);
145 }
146
GetArrayCookieSize(const CXXNewExpr * expr)147 CharUnits CGCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
148 return CharUnits::Zero();
149 }
150
InitializeArrayCookie(CodeGenFunction & CGF,llvm::Value * NewPtr,llvm::Value * NumElements,const CXXNewExpr * expr,QualType ElementType)151 llvm::Value *CGCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
152 llvm::Value *NewPtr,
153 llvm::Value *NumElements,
154 const CXXNewExpr *expr,
155 QualType ElementType) {
156 // Should never be called.
157 ErrorUnsupportedABI(CGF, "array cookie initialization");
158 return 0;
159 }
160
ReadArrayCookie(CodeGenFunction & CGF,llvm::Value * Ptr,const CXXDeleteExpr * expr,QualType ElementType,llvm::Value * & NumElements,llvm::Value * & AllocPtr,CharUnits & CookieSize)161 void CGCXXABI::ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
162 const CXXDeleteExpr *expr, QualType ElementType,
163 llvm::Value *&NumElements,
164 llvm::Value *&AllocPtr, CharUnits &CookieSize) {
165 ErrorUnsupportedABI(CGF, "array cookie reading");
166
167 // This should be enough to avoid assertions.
168 NumElements = 0;
169 AllocPtr = llvm::Constant::getNullValue(CGF.Builder.getInt8PtrTy());
170 CookieSize = CharUnits::Zero();
171 }
172
EmitGuardedInit(CodeGenFunction & CGF,const VarDecl & D,llvm::GlobalVariable * GV,bool PerformInit)173 void CGCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
174 const VarDecl &D,
175 llvm::GlobalVariable *GV,
176 bool PerformInit) {
177 ErrorUnsupportedABI(CGF, "static local variable initialization");
178 }
179
180 /// Returns the adjustment, in bytes, required for the given
181 /// member-pointer operation. Returns null if no adjustment is
182 /// required.
getMemberPointerAdjustment(const CastExpr * E)183 llvm::Constant *CGCXXABI::getMemberPointerAdjustment(const CastExpr *E) {
184 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
185 E->getCastKind() == CK_BaseToDerivedMemberPointer);
186
187 QualType derivedType;
188 if (E->getCastKind() == CK_DerivedToBaseMemberPointer)
189 derivedType = E->getSubExpr()->getType();
190 else
191 derivedType = E->getType();
192
193 const CXXRecordDecl *derivedClass =
194 derivedType->castAs<MemberPointerType>()->getClass()->getAsCXXRecordDecl();
195
196 return CGM.GetNonVirtualBaseClassOffset(derivedClass,
197 E->path_begin(),
198 E->path_end());
199 }
200