blob: 8373b660b2f4573ea65e039b0b28e6f62976bd9d [file] [log] [blame]
Anders Carlsson4992eaf2010-11-28 17:46:52 +00001//===----- 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;
Anders Carlssonee7cde22010-11-28 17:49:03 +000018using namespace CodeGen;
Anders Carlsson4992eaf2010-11-28 17:46:52 +000019
Anders Carlsson1af610f2010-11-28 17:50:09 +000020CGCXXABI::~CGCXXABI() { }
Anders Carlssonee7cde22010-11-28 17:49:03 +000021
22static void ErrorUnsupportedABI(CodeGenFunction &CGF,
23 llvm::StringRef S) {
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()),
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
37const llvm::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 const llvm::FunctionType *FTy =
53 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
54 FPT->isVariadic());
55 return llvm::Constant::getNullValue(FTy->getPointerTo());
56}
57
58llvm::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 const llvm::Type *Ty = CGF.ConvertType(MPT->getPointeeType())->getPointerTo();
64 return llvm::Constant::getNullValue(Ty);
65}
66
67llvm::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
74llvm::Value *
75CGCXXABI::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
84llvm::Value *
85CGCXXABI::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
92llvm::Constant *
93CGCXXABI::EmitMemberPointerConversion(llvm::Constant *C, const CastExpr *E) {
94 return GetBogusMemberPointer(CGM, E->getType());
95}
96
97llvm::Constant *
98CGCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
99 return GetBogusMemberPointer(CGM, QualType(MPT, 0));
100}
101
102llvm::Constant *CGCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
103 return GetBogusMemberPointer(CGM,
104 CGM.getContext().getMemberPointerType(MD->getType(),
105 MD->getParent()->getTypeForDecl()));
106}
107
John McCall5808ce42011-02-03 08:15:49 +0000108llvm::Constant *CGCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
109 CharUnits offset) {
110 return GetBogusMemberPointer(CGM, QualType(MPT, 0));
Anders Carlssonee7cde22010-11-28 17:49:03 +0000111}
112
113bool CGCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
114 // Fake answer.
115 return true;
116}
117
118void CGCXXABI::BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params) {
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(std::make_pair(ThisDecl, ThisDecl->getType()));
128 getThisDecl(CGF) = ThisDecl;
129}
130
131void 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
139void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
140 RValue RV, QualType ResultType) {
141 CGF.EmitReturnOfRValue(RV, ResultType);
142}
143
John McCall6ec278d2011-01-27 09:37:56 +0000144CharUnits CGCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
Anders Carlssonee7cde22010-11-28 17:49:03 +0000145 return CharUnits::Zero();
146}
147
148llvm::Value *CGCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
149 llvm::Value *NewPtr,
150 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000151 const CXXNewExpr *expr,
Anders Carlssonee7cde22010-11-28 17:49:03 +0000152 QualType ElementType) {
153 // Should never be called.
154 ErrorUnsupportedABI(CGF, "array cookie initialization");
155 return 0;
156}
157
158void CGCXXABI::ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall6ec278d2011-01-27 09:37:56 +0000159 const CXXDeleteExpr *expr, QualType ElementType,
160 llvm::Value *&NumElements,
Anders Carlssonee7cde22010-11-28 17:49:03 +0000161 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
170void CGCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
171 const VarDecl &D,
172 llvm::GlobalVariable *GV) {
173 ErrorUnsupportedABI(CGF, "static local variable initialization");
174}