clang  3.7.0
CGCXXABI.cpp
Go to the documentation of this file.
1 //===----- CGCXXABI.cpp - Interface to C++ ABIs ---------------------------===//
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 
21 
23  DiagnosticsEngine &Diags = CGF.CGM.getDiags();
24  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
25  "cannot yet compile %0 in this ABI");
27  DiagID)
28  << S;
29 }
30 
32  // If RD has a non-trivial move or copy constructor, we cannot copy the
33  // argument.
35  return false;
36 
37  // If RD has a non-trivial destructor, we cannot copy the argument.
38  if (RD->hasNonTrivialDestructor())
39  return false;
40 
41  // We can only copy the argument if there exists at least one trivial,
42  // non-deleted copy or move constructor.
43  // FIXME: This assumes that all lazily declared copy and move constructors are
44  // not deleted. This assumption might not be true in some corner cases.
45  bool CopyDeleted = false;
46  bool MoveDeleted = false;
47  for (const CXXConstructorDecl *CD : RD->ctors()) {
48  if (CD->isCopyConstructor() || CD->isMoveConstructor()) {
49  assert(CD->isTrivial());
50  // We had at least one undeleted trivial copy or move ctor. Return
51  // directly.
52  if (!CD->isDeleted())
53  return true;
54  if (CD->isCopyConstructor())
55  CopyDeleted = true;
56  else
57  MoveDeleted = true;
58  }
59  }
60 
61  // If all trivial copy and move constructors are deleted, we cannot copy the
62  // argument.
63  return !(CopyDeleted && MoveDeleted);
64 }
65 
67  return llvm::Constant::getNullValue(CGM.getTypes().ConvertType(T));
68 }
69 
70 llvm::Type *
73 }
74 
76  CodeGenFunction &CGF, const Expr *E, llvm::Value *&This,
77  llvm::Value *MemPtr, const MemberPointerType *MPT) {
78  ErrorUnsupportedABI(CGF, "calls through member pointers");
79 
80  const FunctionProtoType *FPT =
82  const CXXRecordDecl *RD =
83  cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
84  llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
85  CGM.getTypes().arrangeCXXMethodType(RD, FPT));
86  return llvm::Constant::getNullValue(FTy->getPointerTo());
87 }
88 
91  llvm::Value *Base, llvm::Value *MemPtr,
92  const MemberPointerType *MPT) {
93  ErrorUnsupportedABI(CGF, "loads of member pointers");
94  llvm::Type *Ty = CGF.ConvertType(MPT->getPointeeType())->getPointerTo();
95  return llvm::Constant::getNullValue(Ty);
96 }
97 
99  const CastExpr *E,
100  llvm::Value *Src) {
101  ErrorUnsupportedABI(CGF, "member function pointer conversions");
102  return GetBogusMemberPointer(E->getType());
103 }
104 
106  llvm::Constant *Src) {
107  return GetBogusMemberPointer(E->getType());
108 }
109 
110 llvm::Value *
112  llvm::Value *L,
113  llvm::Value *R,
114  const MemberPointerType *MPT,
115  bool Inequality) {
116  ErrorUnsupportedABI(CGF, "member function pointer comparison");
117  return CGF.Builder.getFalse();
118 }
119 
120 llvm::Value *
122  llvm::Value *MemPtr,
123  const MemberPointerType *MPT) {
124  ErrorUnsupportedABI(CGF, "member function pointer null testing");
125  return CGF.Builder.getFalse();
126 }
127 
128 llvm::Constant *
130  return GetBogusMemberPointer(QualType(MPT, 0));
131 }
132 
135  MD->getType(), MD->getParent()->getTypeForDecl()));
136 }
137 
139  CharUnits offset) {
140  return GetBogusMemberPointer(QualType(MPT, 0));
141 }
142 
143 llvm::Constant *CGCXXABI::EmitMemberPointer(const APValue &MP, QualType MPT) {
144  return GetBogusMemberPointer(MPT);
145 }
146 
148  // Fake answer.
149  return true;
150 }
151 
153  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
154 
155  // FIXME: I'm not entirely sure I like using a fake decl just for code
156  // generation. Maybe we can come up with a better way?
157  ImplicitParamDecl *ThisDecl
159  &CGM.getContext().Idents.get("this"),
160  MD->getThisType(CGM.getContext()));
161  params.push_back(ThisDecl);
162  getThisDecl(CGF) = ThisDecl;
163 }
164 
166  /// Initialize the 'this' slot.
167  assert(getThisDecl(CGF) && "no 'this' variable for function");
168  getThisValue(CGF)
169  = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getThisDecl(CGF)),
170  "this");
171 }
172 
174  RValue RV, QualType ResultType) {
175  CGF.EmitReturnOfRValue(RV, ResultType);
176 }
177 
179  if (!requiresArrayCookie(expr))
180  return CharUnits::Zero();
182 }
183 
185  // BOGUS
186  return CharUnits::Zero();
187 }
188 
190  llvm::Value *NewPtr,
191  llvm::Value *NumElements,
192  const CXXNewExpr *expr,
193  QualType ElementType) {
194  // Should never be called.
195  ErrorUnsupportedABI(CGF, "array cookie initialization");
196  return nullptr;
197 }
198 
200  QualType elementType) {
201  // If the class's usual deallocation function takes two arguments,
202  // it needs a cookie.
203  if (expr->doesUsualArrayDeleteWantSize())
204  return true;
205 
206  return elementType.isDestructedType();
207 }
208 
210  // If the class's usual deallocation function takes two arguments,
211  // it needs a cookie.
212  if (expr->doesUsualArrayDeleteWantSize())
213  return true;
214 
215  return expr->getAllocatedType().isDestructedType();
216 }
217 
219  const CXXDeleteExpr *expr, QualType eltTy,
220  llvm::Value *&numElements,
221  llvm::Value *&allocPtr, CharUnits &cookieSize) {
222  // Derive a char* in the same address space as the pointer.
223  unsigned AS = ptr->getType()->getPointerAddressSpace();
224  llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
225  ptr = CGF.Builder.CreateBitCast(ptr, charPtrTy);
226 
227  // If we don't need an array cookie, bail out early.
228  if (!requiresArrayCookie(expr, eltTy)) {
229  allocPtr = ptr;
230  numElements = nullptr;
231  cookieSize = CharUnits::Zero();
232  return;
233  }
234 
235  cookieSize = getArrayCookieSizeImpl(eltTy);
236  allocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ptr,
237  -cookieSize.getQuantity());
238  numElements = readArrayCookieImpl(CGF, allocPtr, cookieSize);
239 }
240 
242  llvm::Value *ptr,
243  CharUnits cookieSize) {
244  ErrorUnsupportedABI(CGF, "reading a new[] cookie");
245  return llvm::ConstantInt::get(CGF.SizeTy, 0);
246 }
247 
248 /// Returns the adjustment, in bytes, required for the given
249 /// member-pointer operation. Returns null if no adjustment is
250 /// required.
254 
255  QualType derivedType;
257  derivedType = E->getSubExpr()->getType();
258  else
259  derivedType = E->getType();
260 
261  const CXXRecordDecl *derivedClass =
262  derivedType->castAs<MemberPointerType>()->getClass()->getAsCXXRecordDecl();
263 
264  return CGM.GetNonVirtualBaseClassOffset(derivedClass,
265  E->path_begin(),
266  E->path_end());
267 }
268 
270  // TODO: Store base specifiers in APValue member pointer paths so we can
271  // easily reuse CGM.GetNonVirtualBaseClassOffset().
272  const ValueDecl *MPD = MP.getMemberPointerDecl();
275  bool DerivedMember = MP.isMemberPointerToDerivedMember();
276  const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
277  for (unsigned I = 0, N = Path.size(); I != N; ++I) {
278  const CXXRecordDecl *Base = RD;
279  const CXXRecordDecl *Derived = Path[I];
280  if (DerivedMember)
281  std::swap(Base, Derived);
282  ThisAdjustment +=
284  RD = Path[I];
285  }
286  if (DerivedMember)
287  ThisAdjustment = -ThisAdjustment;
288  return ThisAdjustment;
289 }
290 
291 llvm::BasicBlock *
293  const CXXRecordDecl *RD) {
295  llvm_unreachable("shouldn't be called in this ABI");
296 
297  ErrorUnsupportedABI(CGF, "complete object detection in ctor");
298  return nullptr;
299 }
300 
302  return false;
303 }
304 
305 llvm::CallInst *
307  llvm::Value *Exn) {
308  // Just call std::terminate and ignore the violating exception.
309  return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
310 }
CastKind getCastKind() const
Definition: Expr.h:2709
virtual llvm::Constant * EmitMemberPointer(const APValue &MP, QualType MPT)
Create a member pointer for the given member pointer constant.
Definition: CGCXXABI.cpp:143
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
Definition: ASTMatchers.h:1110
virtual llvm::Value * readArrayCookieImpl(CodeGenFunction &IGF, llvm::Value *ptr, CharUnits cookieSize)
Definition: CGCXXABI.cpp:241
DestructionKind isDestructedType() const
Definition: Type.h:999
virtual void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResultType)
Definition: CGCXXABI.cpp:173
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:163
ctor_range ctors() const
Definition: DeclCXX.h:774
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
bool hasNonTrivialDestructor() const
Determine whether this class has a non-trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1269
FullSourceLoc getFullLoc(SourceLocation Loc) const
Definition: ASTContext.h:541
QualType getPointeeType() const
Definition: Type.h:2364
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1118
ImplicitParamDecl *& getThisDecl(CodeGenFunction &CGF)
Definition: CGCXXABI.h:51
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2147
bool doesUsualArrayDeleteWantSize() const
Definition: ExprCXX.h:1860
virtual llvm::BasicBlock * EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, const CXXRecordDecl *RD)
Definition: CGCXXABI.cpp:292
A this pointer adjustment.
Definition: ABI.h:108
QualType getThisType(ASTContext &C) const
Returns the type of the this pointer.
Definition: DeclCXX.cpp:1592
void EmitThisParam(CodeGenFunction &CGF)
Definition: CGCXXABI.cpp:165
virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType)
Definition: CGCXXABI.cpp:199
ArrayRef< const CXXRecordDecl * > getMemberPointerPath() const
Definition: APValue.cpp:622
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
bool hasNonTrivialCopyConstructor() const
Determine whether this class has a non-trivial copy constructor (C++ [class.copy]p6, C++11 [class.copy]p12)
Definition: DeclCXX.h:1213
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
Expr * getSubExpr()
Definition: Expr.h:2713
const Decl * getDecl() const
Definition: GlobalDecl.h:60
IdentifierTable & Idents
Definition: ASTContext.h:439
const ValueDecl * getMemberPointerDecl() const
Definition: APValue.cpp:608
const CXXRecordDecl * getParent() const
Definition: DeclCXX.h:1817
path_iterator path_begin()
Definition: Expr.h:2729
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
virtual llvm::Value * EmitMemberPointerIsNotNull(CodeGenFunction &CGF, llvm::Value *MemPtr, const MemberPointerType *MPT)
Determine if a member pointer is non-null. Returns an i1.
Definition: CGCXXABI.cpp:121
CodeGenModule & CGM
Definition: CGCXXABI.h:44
llvm::Constant * getTerminateFn()
Get the declaration of std::terminate for the platform.
Definition: CGException.cpp:50
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
QualType getType() const
Definition: Decl.h:538
virtual llvm::Type * ConvertMemberPointerType(const MemberPointerType *MPT)
Definition: CGCXXABI.cpp:71
virtual bool isZeroInitializable(const MemberPointerType *MPT)
Definition: CGCXXABI.cpp:147
llvm::CallInst * EmitNounwindRuntimeCall(llvm::Value *callee, const Twine &name="")
virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr)
Definition: CGCXXABI.cpp:178
const TargetInfo & getTarget() const
bool hasConstructorVariants() const
Does this ABI have different entrypoints for complete-object and base-subobject constructors?
Definition: TargetCXXABI.h:169
virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, const CXXDeleteExpr *expr, QualType ElementType, llvm::Value *&NumElements, llvm::Value *&AllocPtr, CharUnits &CookieSize)
Definition: CGCXXABI.cpp:218
const Type * getTypeForDecl() const
Definition: Decl.h:2557
virtual CharUnits getArrayCookieSizeImpl(QualType elementType)
Definition: CGCXXABI.cpp:184
llvm::Value * GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
bool isMemberPointerToDerivedMember() const
Definition: APValue.cpp:615
DeclContext * getDeclContext()
Definition: DeclBase.h:381
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:224
ASTContext & getContext() const
QualType getAllocatedType() const
Definition: ExprCXX.h:1682
virtual llvm::Constant * EmitMemberFunctionPointer(const CXXMethodDecl *MD)
Create a member pointer for the given method.
Definition: CGCXXABI.cpp:133
void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S)
Issue a diagnostic about unsupported features in the ABI.
Definition: CGCXXABI.cpp:22
virtual llvm::Value * EmitMemberPointerConversion(CodeGenFunction &CGF, const CastExpr *E, llvm::Value *Src)
Definition: CGCXXABI.cpp:98
virtual bool NeedsVTTParameter(GlobalDecl GD)
Return whether the given global decl needs a VTT parameter.
Definition: CGCXXABI.cpp:301
ASTContext & getContext() const
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
bool canCopyArgument(const CXXRecordDecl *RD) const
Definition: CGCXXABI.cpp:31
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
Definition: ExprCXX.h:1623
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1717
virtual llvm::Value * EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, llvm::Value *MemPtr, const MemberPointerType *MPT)
Definition: CGCXXABI.cpp:75
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
Definition: Diagnostic.h:602
virtual llvm::Constant * EmitNullMemberPointer(const MemberPointerType *MPT)
Create a null member pointer of the given type.
Definition: CGCXXABI.cpp:129
const CGFunctionInfo & arrangeCXXMethodType(const CXXRecordDecl *RD, const FunctionProtoType *FTP)
Definition: CGCall.cpp:157
llvm::Constant * getMemberPointerAdjustment(const CastExpr *E)
Definition: CGCXXABI.cpp:251
virtual llvm::Value * EmitMemberPointerComparison(CodeGenFunction &CGF, llvm::Value *L, llvm::Value *R, const MemberPointerType *MPT, bool Inequality)
Emit a comparison between two member pointers. Returns an i1.
Definition: CGCXXABI.cpp:111
QualType getType() const
Definition: Expr.h:125
QualType getMemberPointerType(QualType T, const Type *Cls) const
Return the uniqued reference to the type for a member pointer to the specified type in the specified ...
Represents a delete expression for memory deallocation and destructor calls, e.g. "delete[] pArray"...
Definition: ExprCXX.h:1819
void buildThisParam(CodeGenFunction &CGF, FunctionArgList &Params)
Build a parameter variable suitable for 'this'.
Definition: CGCXXABI.cpp:152
path_iterator path_end()
Definition: Expr.h:2730
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>. Pointer - pointer requires t...
const T * getAs() const
Definition: Type.h:5555
virtual llvm::Value * EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr, const MemberPointerType *MPT)
Calculate an l-value from an object and a data member pointer.
Definition: CGCXXABI.cpp:90
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, IdentifierInfo *Id, QualType T)
Definition: Decl.cpp:3850
ASTContext & getContext() const
Definition: CGCXXABI.h:75
virtual llvm::Constant * EmitMemberDataPointer(const MemberPointerType *MPT, CharUnits offset)
Create a member pointer for the given field.
Definition: CGCXXABI.cpp:138
llvm::Constant * GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd)
Definition: CGClass.cpp:60
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1505
const Type * getClass() const
Definition: Type.h:2378
bool hasNonTrivialMoveConstructor() const
Determine whether this class has a non-trivial move constructor (C++11 [class.copy]p12) ...
Definition: DeclCXX.h:1227
DiagnosticsEngine & getDiags() const
Represents a C++ struct/union/class.
Definition: DeclCXX.h:285
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
llvm::Type * ConvertType(QualType T)
bool doesUsualArrayDeleteWantSize() const
Definition: ExprCXX.h:1766
virtual llvm::CallInst * emitTerminateForUnexpectedException(CodeGenFunction &CGF, llvm::Value *Exn)
Definition: CGCXXABI.cpp:306
SourceLocation getLocation() const
Definition: DeclBase.h:372
virtual llvm::Value * InitializeArrayCookie(CodeGenFunction &CGF, llvm::Value *NewPtr, llvm::Value *NumElements, const CXXNewExpr *expr, QualType ElementType)
Definition: CGCXXABI.cpp:189
llvm::Value *& getThisValue(CodeGenFunction &CGF)
Definition: CGCXXABI.h:54
llvm::Constant * GetBogusMemberPointer(QualType T)
Get a null value for unsupported member pointers.
Definition: CGCXXABI.cpp:66
CharUnits getMemberPointerPathAdjustment(const APValue &MP)
Computes the non-virtual adjustment needed for a member pointer conversion along an inheritance path ...
Definition: CGCXXABI.cpp:269
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1253