]> granicus.if.org Git - clang/blob - lib/CodeGen/CGCXXABI.h
Exploit this-return of a callsite in a this-return function.
[clang] / lib / CodeGen / CGCXXABI.h
1 //===----- CGCXXABI.h - 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 #ifndef CLANG_CODEGEN_CXXABI_H
16 #define CLANG_CODEGEN_CXXABI_H
17
18 #include "CodeGenFunction.h"
19 #include "clang/Basic/LLVM.h"
20
21 namespace llvm {
22   class Constant;
23   class Type;
24   class Value;
25 }
26
27 namespace clang {
28   class CastExpr;
29   class CXXConstructorDecl;
30   class CXXDestructorDecl;
31   class CXXMethodDecl;
32   class CXXRecordDecl;
33   class FieldDecl;
34   class MangleContext;
35
36 namespace CodeGen {
37   class CodeGenFunction;
38   class CodeGenModule;
39
40 /// \brief Implements C++ ABI-specific code generation functions.
41 class CGCXXABI {
42 protected:
43   CodeGenModule &CGM;
44   OwningPtr<MangleContext> MangleCtx;
45
46   CGCXXABI(CodeGenModule &CGM)
47     : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
48
49 protected:
50   ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
51     return CGF.CXXABIThisDecl;
52   }
53   llvm::Value *&getThisValue(CodeGenFunction &CGF) {
54     return CGF.CXXABIThisValue;
55   }
56
57   // FIXME: Every place that calls getVTT{Decl,Value} is something
58   // that needs to be abstracted properly.
59   ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
60     return CGF.CXXStructorImplicitParamDecl;
61   }
62   llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
63     return CGF.CXXStructorImplicitParamValue;
64   }
65
66   ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
67     return CGF.CXXStructorImplicitParamDecl;
68   }
69   llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
70     return CGF.CXXStructorImplicitParamValue;
71   }
72
73   /// Build a parameter variable suitable for 'this'.
74   void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
75
76   /// Perform prolog initialization of the parameter variable suitable
77   /// for 'this' emitted by BuildThisParam.
78   void EmitThisParam(CodeGenFunction &CGF);
79
80   ASTContext &getContext() const { return CGM.getContext(); }
81
82   virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
83   virtual bool requiresArrayCookie(const CXXNewExpr *E);
84
85 public:
86
87   virtual ~CGCXXABI();
88
89   /// Gets the mangle context.
90   MangleContext &getMangleContext() {
91     return *MangleCtx;
92   }
93
94   /// Returns true if the given instance method is one of the
95   /// kinds that the ABI says returns 'this'.
96   virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
97
98   /// Find the LLVM type used to represent the given member pointer
99   /// type.
100   virtual llvm::Type *
101   ConvertMemberPointerType(const MemberPointerType *MPT);
102
103   /// Load a member function from an object and a member function
104   /// pointer.  Apply the this-adjustment and set 'This' to the
105   /// adjusted value.
106   virtual llvm::Value *
107   EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
108                                   llvm::Value *&This,
109                                   llvm::Value *MemPtr,
110                                   const MemberPointerType *MPT);
111
112   /// Calculate an l-value from an object and a data member pointer.
113   virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
114                                                     llvm::Value *Base,
115                                                     llvm::Value *MemPtr,
116                                             const MemberPointerType *MPT);
117
118   /// Perform a derived-to-base, base-to-derived, or bitcast member
119   /// pointer conversion.
120   virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
121                                                    const CastExpr *E,
122                                                    llvm::Value *Src);
123
124   /// Perform a derived-to-base, base-to-derived, or bitcast member
125   /// pointer conversion on a constant value.
126   virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
127                                                       llvm::Constant *Src);
128
129   /// Return true if the given member pointer can be zero-initialized
130   /// (in the C++ sense) with an LLVM zeroinitializer.
131   virtual bool isZeroInitializable(const MemberPointerType *MPT);
132
133   /// Create a null member pointer of the given type.
134   virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
135
136   /// Create a member pointer for the given method.
137   virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
138
139   /// Create a member pointer for the given field.
140   virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
141                                                 CharUnits offset);
142
143   /// Create a member pointer for the given member pointer constant.
144   virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
145
146   /// Emit a comparison between two member pointers.  Returns an i1.
147   virtual llvm::Value *
148   EmitMemberPointerComparison(CodeGenFunction &CGF,
149                               llvm::Value *L,
150                               llvm::Value *R,
151                               const MemberPointerType *MPT,
152                               bool Inequality);
153
154   /// Determine if a member pointer is non-null.  Returns an i1.
155   virtual llvm::Value *
156   EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
157                              llvm::Value *MemPtr,
158                              const MemberPointerType *MPT);
159
160 protected:
161   /// A utility method for computing the offset required for the given
162   /// base-to-derived or derived-to-base member-pointer conversion.
163   /// Does not handle virtual conversions (in case we ever fully
164   /// support an ABI that allows this).  Returns null if no adjustment
165   /// is required.
166   llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
167
168 public:
169   /// Adjust the given non-null pointer to an object of polymorphic
170   /// type to point to the complete object.
171   ///
172   /// The IR type of the result should be a pointer but is otherwise
173   /// irrelevant.
174   virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
175                                               llvm::Value *ptr,
176                                               QualType type) = 0;
177
178   /// Build the signature of the given constructor variant by adding
179   /// any required parameters.  For convenience, ResTy has been
180   /// initialized to 'void', and ArgTys has been initialized with the
181   /// type of 'this' (although this may be changed by the ABI) and
182   /// will have the formal parameters added to it afterwards.
183   ///
184   /// If there are ever any ABIs where the implicit parameters are
185   /// intermixed with the formal parameters, we can address those
186   /// then.
187   virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
188                                          CXXCtorType T,
189                                          CanQualType &ResTy,
190                                SmallVectorImpl<CanQualType> &ArgTys) = 0;
191
192   virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF);
193
194   /// Build the signature of the given destructor variant by adding
195   /// any required parameters.  For convenience, ResTy has been
196   /// initialized to 'void' and ArgTys has been initialized with the
197   /// type of 'this' (although this may be changed by the ABI).
198   virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
199                                         CXXDtorType T,
200                                         CanQualType &ResTy,
201                                SmallVectorImpl<CanQualType> &ArgTys) = 0;
202
203   /// Build the ABI-specific portion of the parameter list for a
204   /// function.  This generally involves a 'this' parameter and
205   /// possibly some extra data for constructors and destructors.
206   ///
207   /// ABIs may also choose to override the return type, which has been
208   /// initialized with the formal return type of the function.
209   virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
210                                            QualType &ResTy,
211                                            FunctionArgList &Params) = 0;
212
213   /// Emit the ABI-specific prolog for the function.
214   virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
215
216   /// Emit the constructor call. Return the function that is called.
217   virtual llvm::Value *EmitConstructorCall(CodeGenFunction &CGF,
218                                    const CXXConstructorDecl *D,
219                                    CXXCtorType Type, bool ForVirtualBase,
220                                    bool Delegating,
221                                    llvm::Value *This,
222                                    CallExpr::const_arg_iterator ArgBeg,
223                                    CallExpr::const_arg_iterator ArgEnd) = 0;
224
225   /// Emit the ABI-specific virtual destructor call.
226   virtual RValue EmitVirtualDestructorCall(CodeGenFunction &CGF,
227                                            const CXXDestructorDecl *Dtor,
228                                            CXXDtorType DtorType,
229                                            SourceLocation CallLoc,
230                                            ReturnValueSlot ReturnValue,
231                                            llvm::Value *This) = 0;
232
233   virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
234                                    RValue RV, QualType ResultType);
235
236   /// Gets the pure virtual member call function.
237   virtual StringRef GetPureVirtualCallName() = 0;
238
239   /// Gets the deleted virtual member call name.
240   virtual StringRef GetDeletedVirtualCallName() = 0;
241
242   /**************************** Array cookies ******************************/
243
244   /// Returns the extra size required in order to store the array
245   /// cookie for the given new-expression.  May return 0 to indicate that no
246   /// array cookie is required.
247   ///
248   /// Several cases are filtered out before this method is called:
249   ///   - non-array allocations never need a cookie
250   ///   - calls to \::operator new(size_t, void*) never need a cookie
251   ///
252   /// \param expr - the new-expression being allocated.
253   virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
254
255   /// Initialize the array cookie for the given allocation.
256   ///
257   /// \param NewPtr - a char* which is the presumed-non-null
258   ///   return value of the allocation function
259   /// \param NumElements - the computed number of elements,
260   ///   potentially collapsed from the multidimensional array case;
261   ///   always a size_t
262   /// \param ElementType - the base element allocated type,
263   ///   i.e. the allocated type after stripping all array types
264   virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
265                                              llvm::Value *NewPtr,
266                                              llvm::Value *NumElements,
267                                              const CXXNewExpr *expr,
268                                              QualType ElementType);
269
270   /// Reads the array cookie associated with the given pointer,
271   /// if it has one.
272   ///
273   /// \param Ptr - a pointer to the first element in the array
274   /// \param ElementType - the base element type of elements of the array
275   /// \param NumElements - an out parameter which will be initialized
276   ///   with the number of elements allocated, or zero if there is no
277   ///   cookie
278   /// \param AllocPtr - an out parameter which will be initialized
279   ///   with a char* pointing to the address returned by the allocation
280   ///   function
281   /// \param CookieSize - an out parameter which will be initialized
282   ///   with the size of the cookie, or zero if there is no cookie
283   virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
284                                const CXXDeleteExpr *expr,
285                                QualType ElementType, llvm::Value *&NumElements,
286                                llvm::Value *&AllocPtr, CharUnits &CookieSize);
287
288 protected:
289   /// Returns the extra size required in order to store the array
290   /// cookie for the given type.  Assumes that an array cookie is
291   /// required.
292   virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
293
294   /// Reads the array cookie for an allocation which is known to have one.
295   /// This is called by the standard implementation of ReadArrayCookie.
296   ///
297   /// \param ptr - a pointer to the allocation made for an array, as a char*
298   /// \param cookieSize - the computed cookie size of an array
299   ///
300   /// Other parameters are as above.
301   ///
302   /// \return a size_t
303   virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
304                                            llvm::Value *ptr,
305                                            CharUnits cookieSize);
306
307 public:
308
309   /*************************** Static local guards ****************************/
310
311   /// Emits the guarded initializer and destructor setup for the given
312   /// variable, given that it couldn't be emitted as a constant.
313   /// If \p PerformInit is false, the initialization has been folded to a
314   /// constant and should not be performed.
315   ///
316   /// The variable may be:
317   ///   - a static local variable
318   ///   - a static data member of a class template instantiation
319   virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
320                                llvm::GlobalVariable *DeclPtr, bool PerformInit);
321
322   /// Emit code to force the execution of a destructor during global
323   /// teardown.  The default implementation of this uses atexit.
324   ///
325   /// \param dtor - a function taking a single pointer argument
326   /// \param addr - a pointer to pass to the destructor function.
327   virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
328                                   llvm::Constant *addr);
329 };
330
331 // Create an instance of a C++ ABI class:
332
333 /// Creates an Itanium-family ABI.
334 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
335
336 /// Creates a Microsoft-family ABI.
337 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
338
339 }
340 }
341
342 #endif