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