]> granicus.if.org Git - clang/blob - lib/CodeGen/CGCXXABI.h
More __unknown_anytype work.
[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
20 namespace llvm {
21   class Constant;
22   class Type;
23   class Value;
24
25   template <class T> class SmallVectorImpl;
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 /// Implements C++ ABI-specific code generation functions.
42 class CGCXXABI {
43 protected:
44   CodeGenModule &CGM;
45   llvm::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.CXXThisDecl;
53   }
54   llvm::Value *&getThisValue(CodeGenFunction &CGF) {
55     return CGF.CXXThisValue;
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 public:
75
76   virtual ~CGCXXABI();
77
78   /// Gets the mangle context.
79   MangleContext &getMangleContext() {
80     return *MangleCtx;
81   }
82
83   /// Find the LLVM type used to represent the given member pointer
84   /// type.
85   virtual const llvm::Type *
86   ConvertMemberPointerType(const MemberPointerType *MPT);
87
88   /// Load a member function from an object and a member function
89   /// pointer.  Apply the this-adjustment and set 'This' to the
90   /// adjusted value.
91   virtual llvm::Value *
92   EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
93                                   llvm::Value *&This,
94                                   llvm::Value *MemPtr,
95                                   const MemberPointerType *MPT);
96
97   /// Calculate an l-value from an object and a data member pointer.
98   virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
99                                                     llvm::Value *Base,
100                                                     llvm::Value *MemPtr,
101                                             const MemberPointerType *MPT);
102
103   /// Perform a derived-to-base or base-to-derived member pointer
104   /// conversion.
105   virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
106                                                    const CastExpr *E,
107                                                    llvm::Value *Src);
108
109   /// Perform a derived-to-base or base-to-derived member pointer
110   /// conversion on a constant member pointer.
111   virtual llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
112                                                       const CastExpr *E);
113
114   /// Return true if the given member pointer can be zero-initialized
115   /// (in the C++ sense) with an LLVM zeroinitializer.
116   virtual bool isZeroInitializable(const MemberPointerType *MPT);
117
118   /// Create a null member pointer of the given type.
119   virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
120
121   /// Create a member pointer for the given method.
122   ///
123   /// \param unknownType - if non-null, use this type as the operand
124   ///   to CodeGenModule::getAddrOfUnknownAnyDecl instead of
125   ///   fetching the method's address in the normal way
126   virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD,
127                                             QualType unknownType = QualType());
128
129   /// Create a member pointer for the given field.
130   virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
131                                                 CharUnits offset);
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   /// Build the signature of the given constructor variant by adding
148   /// any required parameters.  For convenience, ResTy has been
149   /// initialized to 'void', and ArgTys has been initialized with the
150   /// type of 'this' (although this may be changed by the ABI) and
151   /// will have the formal parameters added to it afterwards.
152   ///
153   /// If there are ever any ABIs where the implicit parameters are
154   /// intermixed with the formal parameters, we can address those
155   /// then.
156   virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
157                                          CXXCtorType T,
158                                          CanQualType &ResTy,
159                                llvm::SmallVectorImpl<CanQualType> &ArgTys) = 0;
160
161   /// Build the signature of the given destructor variant by adding
162   /// any required parameters.  For convenience, ResTy has been
163   /// initialized to 'void' and ArgTys has been initialized with the
164   /// type of 'this' (although this may be changed by the ABI).
165   virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
166                                         CXXDtorType T,
167                                         CanQualType &ResTy,
168                                llvm::SmallVectorImpl<CanQualType> &ArgTys) = 0;
169
170   /// Build the ABI-specific portion of the parameter list for a
171   /// function.  This generally involves a 'this' parameter and
172   /// possibly some extra data for constructors and destructors.
173   ///
174   /// ABIs may also choose to override the return type, which has been
175   /// initialized with the formal return type of the function.
176   virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
177                                            QualType &ResTy,
178                                            FunctionArgList &Params) = 0;
179
180   /// Emit the ABI-specific prolog for the function.
181   virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
182
183   virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
184                                    RValue RV, QualType ResultType);
185
186   /**************************** Array cookies ******************************/
187
188   /// Returns the extra size required in order to store the array
189   /// cookie for the given type.  May return 0 to indicate that no
190   /// array cookie is required.
191   ///
192   /// Several cases are filtered out before this method is called:
193   ///   - non-array allocations never need a cookie
194   ///   - calls to ::operator new(size_t, void*) never need a cookie
195   ///
196   /// \param ElementType - the allocated type of the expression,
197   ///   i.e. the pointee type of the expression result type
198   virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
199
200   /// Initialize the array cookie for the given allocation.
201   ///
202   /// \param NewPtr - a char* which is the presumed-non-null
203   ///   return value of the allocation function
204   /// \param NumElements - the computed number of elements,
205   ///   potentially collapsed from the multidimensional array case
206   /// \param ElementType - the base element allocated type,
207   ///   i.e. the allocated type after stripping all array types
208   virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
209                                              llvm::Value *NewPtr,
210                                              llvm::Value *NumElements,
211                                              const CXXNewExpr *expr,
212                                              QualType ElementType);
213
214   /// Reads the array cookie associated with the given pointer,
215   /// if it has one.
216   ///
217   /// \param Ptr - a pointer to the first element in the array
218   /// \param ElementType - the base element type of elements of the array
219   /// \param NumElements - an out parameter which will be initialized
220   ///   with the number of elements allocated, or zero if there is no
221   ///   cookie
222   /// \param AllocPtr - an out parameter which will be initialized
223   ///   with a char* pointing to the address returned by the allocation
224   ///   function
225   /// \param CookieSize - an out parameter which will be initialized
226   ///   with the size of the cookie, or zero if there is no cookie
227   virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
228                                const CXXDeleteExpr *expr,
229                                QualType ElementType, llvm::Value *&NumElements,
230                                llvm::Value *&AllocPtr, CharUnits &CookieSize);
231
232   /*************************** Static local guards ****************************/
233
234   /// Emits the guarded initializer and destructor setup for the given
235   /// variable, given that it couldn't be emitted as a constant.
236   ///
237   /// The variable may be:
238   ///   - a static local variable
239   ///   - a static data member of a class template instantiation
240   virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
241                                llvm::GlobalVariable *DeclPtr);
242
243 };
244
245 /// Creates an instance of a C++ ABI class.
246 CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
247 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
248 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
249
250 }
251 }
252
253 #endif