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