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