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