1 //===----- CGCXXABI.cpp - Interface to C++ ABIs -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides an abstract class for C++ code generation. Concrete subclasses
11 // of this implement code generation for specific C++ ABIs.
13 //===----------------------------------------------------------------------===//
17 using namespace clang
;
18 using namespace CodeGen
;
20 CGCXXABI::~CGCXXABI() { }
22 static void ErrorUnsupportedABI(CodeGenFunction
&CGF
,
24 Diagnostic
&Diags
= CGF
.CGM
.getDiags();
25 unsigned DiagID
= Diags
.getCustomDiagID(Diagnostic::Error
,
26 "cannot yet compile %1 in this ABI");
27 Diags
.Report(CGF
.getContext().getFullLoc(CGF
.CurCodeDecl
->getLocation()),
32 static llvm::Constant
*GetBogusMemberPointer(CodeGenModule
&CGM
,
34 return llvm::Constant::getNullValue(CGM
.getTypes().ConvertType(T
));
38 CGCXXABI::ConvertMemberPointerType(const MemberPointerType
*MPT
) {
39 return CGM
.getTypes().ConvertType(CGM
.getContext().getPointerDiffType());
42 llvm::Value
*CGCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction
&CGF
,
45 const MemberPointerType
*MPT
) {
46 ErrorUnsupportedABI(CGF
, "calls through member pointers");
48 const FunctionProtoType
*FPT
=
49 MPT
->getPointeeType()->getAs
<FunctionProtoType
>();
50 const CXXRecordDecl
*RD
=
51 cast
<CXXRecordDecl
>(MPT
->getClass()->getAs
<RecordType
>()->getDecl());
52 const llvm::FunctionType
*FTy
=
53 CGM
.getTypes().GetFunctionType(CGM
.getTypes().getFunctionInfo(RD
, FPT
),
55 return llvm::Constant::getNullValue(FTy
->getPointerTo());
58 llvm::Value
*CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction
&CGF
,
61 const MemberPointerType
*MPT
) {
62 ErrorUnsupportedABI(CGF
, "loads of member pointers");
63 const llvm::Type
*Ty
= CGF
.ConvertType(MPT
->getPointeeType())->getPointerTo();
64 return llvm::Constant::getNullValue(Ty
);
67 llvm::Value
*CGCXXABI::EmitMemberPointerConversion(CodeGenFunction
&CGF
,
70 ErrorUnsupportedABI(CGF
, "member function pointer conversions");
71 return GetBogusMemberPointer(CGM
, E
->getType());
75 CGCXXABI::EmitMemberPointerComparison(CodeGenFunction
&CGF
,
78 const MemberPointerType
*MPT
,
80 ErrorUnsupportedABI(CGF
, "member function pointer comparison");
81 return CGF
.Builder
.getFalse();
85 CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction
&CGF
,
87 const MemberPointerType
*MPT
) {
88 ErrorUnsupportedABI(CGF
, "member function pointer null testing");
89 return CGF
.Builder
.getFalse();
93 CGCXXABI::EmitMemberPointerConversion(llvm::Constant
*C
, const CastExpr
*E
) {
94 return GetBogusMemberPointer(CGM
, E
->getType());
98 CGCXXABI::EmitNullMemberPointer(const MemberPointerType
*MPT
) {
99 return GetBogusMemberPointer(CGM
, QualType(MPT
, 0));
102 llvm::Constant
*CGCXXABI::EmitMemberPointer(const CXXMethodDecl
*MD
) {
103 return GetBogusMemberPointer(CGM
,
104 CGM
.getContext().getMemberPointerType(MD
->getType(),
105 MD
->getParent()->getTypeForDecl()));
108 llvm::Constant
*CGCXXABI::EmitMemberDataPointer(const MemberPointerType
*MPT
,
110 return GetBogusMemberPointer(CGM
, QualType(MPT
, 0));
113 bool CGCXXABI::isZeroInitializable(const MemberPointerType
*MPT
) {
118 void CGCXXABI::BuildThisParam(CodeGenFunction
&CGF
, FunctionArgList
&Params
) {
119 const CXXMethodDecl
*MD
= cast
<CXXMethodDecl
>(CGF
.CurGD
.getDecl());
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(std::make_pair(ThisDecl
, ThisDecl
->getType()));
128 getThisDecl(CGF
) = ThisDecl
;
131 void CGCXXABI::EmitThisParam(CodeGenFunction
&CGF
) {
132 /// Initialize the 'this' slot.
133 assert(getThisDecl(CGF
) && "no 'this' variable for function");
135 = CGF
.Builder
.CreateLoad(CGF
.GetAddrOfLocalVar(getThisDecl(CGF
)),
139 void CGCXXABI::EmitReturnFromThunk(CodeGenFunction
&CGF
,
140 RValue RV
, QualType ResultType
) {
141 CGF
.EmitReturnOfRValue(RV
, ResultType
);
144 CharUnits
CGCXXABI::GetArrayCookieSize(const CXXNewExpr
*expr
) {
145 return CharUnits::Zero();
148 llvm::Value
*CGCXXABI::InitializeArrayCookie(CodeGenFunction
&CGF
,
150 llvm::Value
*NumElements
,
151 const CXXNewExpr
*expr
,
152 QualType ElementType
) {
153 // Should never be called.
154 ErrorUnsupportedABI(CGF
, "array cookie initialization");
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");
164 // This should be enough to avoid assertions.
166 AllocPtr
= llvm::Constant::getNullValue(CGF
.Builder
.getInt8PtrTy());
167 CookieSize
= CharUnits::Zero();
170 void CGCXXABI::EmitGuardedInit(CodeGenFunction
&CGF
,
172 llvm::GlobalVariable
*GV
) {
173 ErrorUnsupportedABI(CGF
, "static local variable initialization");