1208600Srdivacky//===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===//
2208600Srdivacky//
3208600Srdivacky//                     The LLVM Compiler Infrastructure
4208600Srdivacky//
5208600Srdivacky// This file is distributed under the University of Illinois Open Source
6208600Srdivacky// License. See LICENSE.TXT for details.
7208600Srdivacky//
8208600Srdivacky//===----------------------------------------------------------------------===//
9208600Srdivacky//
10208600Srdivacky// This provides an abstract class for C++ code generation. Concrete subclasses
11208600Srdivacky// of this implement code generation for specific C++ ABIs.
12208600Srdivacky//
13208600Srdivacky//===----------------------------------------------------------------------===//
14208600Srdivacky
15208600Srdivacky#ifndef CLANG_CODEGEN_CXXABI_H
16208600Srdivacky#define CLANG_CODEGEN_CXXABI_H
17208600Srdivacky
18249423Sdim#include "CodeGenFunction.h"
19226633Sdim#include "clang/Basic/LLVM.h"
20226633Sdim
21212904Sdimnamespace llvm {
22212904Sdim  class Constant;
23212904Sdim  class Type;
24212904Sdim  class Value;
25212904Sdim}
26212904Sdim
27208600Srdivackynamespace clang {
28212904Sdim  class CastExpr;
29212904Sdim  class CXXConstructorDecl;
30212904Sdim  class CXXDestructorDecl;
31212904Sdim  class CXXMethodDecl;
32212904Sdim  class CXXRecordDecl;
33212904Sdim  class FieldDecl;
34218893Sdim  class MangleContext;
35212904Sdim
36208600Srdivackynamespace CodeGen {
37212904Sdim  class CodeGenFunction;
38208600Srdivacky  class CodeGenModule;
39208600Srdivacky
40239462Sdim/// \brief Implements C++ ABI-specific code generation functions.
41212904Sdimclass CGCXXABI {
42212904Sdimprotected:
43212904Sdim  CodeGenModule &CGM;
44234353Sdim  OwningPtr<MangleContext> MangleCtx;
45212904Sdim
46218893Sdim  CGCXXABI(CodeGenModule &CGM)
47218893Sdim    : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
48212904Sdim
49212904Sdimprotected:
50212904Sdim  ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
51234353Sdim    return CGF.CXXABIThisDecl;
52212904Sdim  }
53212904Sdim  llvm::Value *&getThisValue(CodeGenFunction &CGF) {
54234353Sdim    return CGF.CXXABIThisValue;
55212904Sdim  }
56212904Sdim
57249423Sdim  /// Issue a diagnostic about unsupported features in the ABI.
58249423Sdim  void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S);
59249423Sdim
60249423Sdim  /// Get a null value for unsupported member pointers.
61249423Sdim  llvm::Constant *GetBogusMemberPointer(QualType T);
62249423Sdim
63249423Sdim  // FIXME: Every place that calls getVTT{Decl,Value} is something
64249423Sdim  // that needs to be abstracted properly.
65212904Sdim  ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
66249423Sdim    return CGF.CXXStructorImplicitParamDecl;
67212904Sdim  }
68212904Sdim  llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
69249423Sdim    return CGF.CXXStructorImplicitParamValue;
70212904Sdim  }
71212904Sdim
72249423Sdim  ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
73249423Sdim    return CGF.CXXStructorImplicitParamDecl;
74249423Sdim  }
75249423Sdim  llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
76249423Sdim    return CGF.CXXStructorImplicitParamValue;
77249423Sdim  }
78249423Sdim
79212904Sdim  /// Build a parameter variable suitable for 'this'.
80212904Sdim  void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
81212904Sdim
82212904Sdim  /// Perform prolog initialization of the parameter variable suitable
83212904Sdim  /// for 'this' emitted by BuildThisParam.
84212904Sdim  void EmitThisParam(CodeGenFunction &CGF);
85212904Sdim
86212904Sdim  ASTContext &getContext() const { return CGM.getContext(); }
87212904Sdim
88239462Sdim  virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
89239462Sdim  virtual bool requiresArrayCookie(const CXXNewExpr *E);
90239462Sdim
91208600Srdivackypublic:
92208600Srdivacky
93212904Sdim  virtual ~CGCXXABI();
94212904Sdim
95208600Srdivacky  /// Gets the mangle context.
96218893Sdim  MangleContext &getMangleContext() {
97218893Sdim    return *MangleCtx;
98218893Sdim  }
99212904Sdim
100249423Sdim  /// Returns true if the given instance method is one of the
101249423Sdim  /// kinds that the ABI says returns 'this'.
102249423Sdim  virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
103249423Sdim
104251662Sdim  /// Returns true if the given record type should be returned indirectly.
105251662Sdim  virtual bool isReturnTypeIndirect(const CXXRecordDecl *RD) const = 0;
106251662Sdim
107251662Sdim  /// Specify how one should pass an argument of a record type.
108251662Sdim  enum RecordArgABI {
109251662Sdim    /// Pass it using the normal C aggregate rules for the ABI, potentially
110251662Sdim    /// introducing extra copies and passing some or all of it in registers.
111251662Sdim    RAA_Default = 0,
112251662Sdim
113251662Sdim    /// Pass it on the stack using its defined layout.  The argument must be
114251662Sdim    /// evaluated directly into the correct stack position in the arguments area,
115251662Sdim    /// and the call machinery must not move it or introduce extra copies.
116251662Sdim    RAA_DirectInMemory,
117251662Sdim
118251662Sdim    /// Pass it as a pointer to temporary memory.
119251662Sdim    RAA_Indirect
120251662Sdim  };
121251662Sdim
122251662Sdim  /// Returns how an argument of the given record type should be passed.
123251662Sdim  virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
124251662Sdim
125212904Sdim  /// Find the LLVM type used to represent the given member pointer
126212904Sdim  /// type.
127224145Sdim  virtual llvm::Type *
128212904Sdim  ConvertMemberPointerType(const MemberPointerType *MPT);
129212904Sdim
130212904Sdim  /// Load a member function from an object and a member function
131212904Sdim  /// pointer.  Apply the this-adjustment and set 'This' to the
132212904Sdim  /// adjusted value.
133212904Sdim  virtual llvm::Value *
134212904Sdim  EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
135212904Sdim                                  llvm::Value *&This,
136212904Sdim                                  llvm::Value *MemPtr,
137212904Sdim                                  const MemberPointerType *MPT);
138212904Sdim
139212904Sdim  /// Calculate an l-value from an object and a data member pointer.
140212904Sdim  virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
141212904Sdim                                                    llvm::Value *Base,
142212904Sdim                                                    llvm::Value *MemPtr,
143212904Sdim                                            const MemberPointerType *MPT);
144212904Sdim
145234353Sdim  /// Perform a derived-to-base, base-to-derived, or bitcast member
146234353Sdim  /// pointer conversion.
147212904Sdim  virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
148212904Sdim                                                   const CastExpr *E,
149212904Sdim                                                   llvm::Value *Src);
150212904Sdim
151234353Sdim  /// Perform a derived-to-base, base-to-derived, or bitcast member
152234353Sdim  /// pointer conversion on a constant value.
153234353Sdim  virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
154234353Sdim                                                      llvm::Constant *Src);
155212904Sdim
156212904Sdim  /// Return true if the given member pointer can be zero-initialized
157212904Sdim  /// (in the C++ sense) with an LLVM zeroinitializer.
158212904Sdim  virtual bool isZeroInitializable(const MemberPointerType *MPT);
159212904Sdim
160212904Sdim  /// Create a null member pointer of the given type.
161212904Sdim  virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
162212904Sdim
163212904Sdim  /// Create a member pointer for the given method.
164212904Sdim  virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
165212904Sdim
166212904Sdim  /// Create a member pointer for the given field.
167218893Sdim  virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
168218893Sdim                                                CharUnits offset);
169212904Sdim
170234353Sdim  /// Create a member pointer for the given member pointer constant.
171234353Sdim  virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
172234353Sdim
173212904Sdim  /// Emit a comparison between two member pointers.  Returns an i1.
174212904Sdim  virtual llvm::Value *
175212904Sdim  EmitMemberPointerComparison(CodeGenFunction &CGF,
176212904Sdim                              llvm::Value *L,
177212904Sdim                              llvm::Value *R,
178212904Sdim                              const MemberPointerType *MPT,
179212904Sdim                              bool Inequality);
180212904Sdim
181212904Sdim  /// Determine if a member pointer is non-null.  Returns an i1.
182212904Sdim  virtual llvm::Value *
183212904Sdim  EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
184212904Sdim                             llvm::Value *MemPtr,
185212904Sdim                             const MemberPointerType *MPT);
186212904Sdim
187234353Sdimprotected:
188234353Sdim  /// A utility method for computing the offset required for the given
189234353Sdim  /// base-to-derived or derived-to-base member-pointer conversion.
190234353Sdim  /// Does not handle virtual conversions (in case we ever fully
191234353Sdim  /// support an ABI that allows this).  Returns null if no adjustment
192234353Sdim  /// is required.
193234353Sdim  llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
194234353Sdim
195234353Sdimpublic:
196243830Sdim  /// Adjust the given non-null pointer to an object of polymorphic
197243830Sdim  /// type to point to the complete object.
198243830Sdim  ///
199243830Sdim  /// The IR type of the result should be a pointer but is otherwise
200243830Sdim  /// irrelevant.
201243830Sdim  virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
202243830Sdim                                              llvm::Value *ptr,
203243830Sdim                                              QualType type) = 0;
204243830Sdim
205212904Sdim  /// Build the signature of the given constructor variant by adding
206212904Sdim  /// any required parameters.  For convenience, ResTy has been
207212904Sdim  /// initialized to 'void', and ArgTys has been initialized with the
208212904Sdim  /// type of 'this' (although this may be changed by the ABI) and
209212904Sdim  /// will have the formal parameters added to it afterwards.
210212904Sdim  ///
211212904Sdim  /// If there are ever any ABIs where the implicit parameters are
212212904Sdim  /// intermixed with the formal parameters, we can address those
213212904Sdim  /// then.
214212904Sdim  virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
215212904Sdim                                         CXXCtorType T,
216212904Sdim                                         CanQualType &ResTy,
217226633Sdim                               SmallVectorImpl<CanQualType> &ArgTys) = 0;
218212904Sdim
219249423Sdim  virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF);
220249423Sdim
221212904Sdim  /// Build the signature of the given destructor variant by adding
222212904Sdim  /// any required parameters.  For convenience, ResTy has been
223212904Sdim  /// initialized to 'void' and ArgTys has been initialized with the
224212904Sdim  /// type of 'this' (although this may be changed by the ABI).
225212904Sdim  virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
226212904Sdim                                        CXXDtorType T,
227212904Sdim                                        CanQualType &ResTy,
228226633Sdim                               SmallVectorImpl<CanQualType> &ArgTys) = 0;
229212904Sdim
230212904Sdim  /// Build the ABI-specific portion of the parameter list for a
231212904Sdim  /// function.  This generally involves a 'this' parameter and
232212904Sdim  /// possibly some extra data for constructors and destructors.
233212904Sdim  ///
234212904Sdim  /// ABIs may also choose to override the return type, which has been
235212904Sdim  /// initialized with the formal return type of the function.
236212904Sdim  virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
237212904Sdim                                           QualType &ResTy,
238212904Sdim                                           FunctionArgList &Params) = 0;
239212904Sdim
240212904Sdim  /// Emit the ABI-specific prolog for the function.
241212904Sdim  virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
242212904Sdim
243249423Sdim  /// Emit the constructor call. Return the function that is called.
244249423Sdim  virtual llvm::Value *EmitConstructorCall(CodeGenFunction &CGF,
245249423Sdim                                   const CXXConstructorDecl *D,
246249423Sdim                                   CXXCtorType Type, bool ForVirtualBase,
247249423Sdim                                   bool Delegating,
248249423Sdim                                   llvm::Value *This,
249249423Sdim                                   CallExpr::const_arg_iterator ArgBeg,
250249423Sdim                                   CallExpr::const_arg_iterator ArgEnd) = 0;
251249423Sdim
252249423Sdim  /// Emit the ABI-specific virtual destructor call.
253249423Sdim  virtual RValue EmitVirtualDestructorCall(CodeGenFunction &CGF,
254249423Sdim                                           const CXXDestructorDecl *Dtor,
255249423Sdim                                           CXXDtorType DtorType,
256249423Sdim                                           SourceLocation CallLoc,
257249423Sdim                                           ReturnValueSlot ReturnValue,
258249423Sdim                                           llvm::Value *This) = 0;
259249423Sdim
260212904Sdim  virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
261212904Sdim                                   RValue RV, QualType ResultType);
262212904Sdim
263239462Sdim  /// Gets the pure virtual member call function.
264239462Sdim  virtual StringRef GetPureVirtualCallName() = 0;
265239462Sdim
266243830Sdim  /// Gets the deleted virtual member call name.
267243830Sdim  virtual StringRef GetDeletedVirtualCallName() = 0;
268243830Sdim
269212904Sdim  /**************************** Array cookies ******************************/
270212904Sdim
271212904Sdim  /// Returns the extra size required in order to store the array
272239462Sdim  /// cookie for the given new-expression.  May return 0 to indicate that no
273212904Sdim  /// array cookie is required.
274212904Sdim  ///
275212904Sdim  /// Several cases are filtered out before this method is called:
276212904Sdim  ///   - non-array allocations never need a cookie
277239462Sdim  ///   - calls to \::operator new(size_t, void*) never need a cookie
278212904Sdim  ///
279239462Sdim  /// \param expr - the new-expression being allocated.
280218893Sdim  virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
281212904Sdim
282212904Sdim  /// Initialize the array cookie for the given allocation.
283212904Sdim  ///
284212904Sdim  /// \param NewPtr - a char* which is the presumed-non-null
285212904Sdim  ///   return value of the allocation function
286212904Sdim  /// \param NumElements - the computed number of elements,
287239462Sdim  ///   potentially collapsed from the multidimensional array case;
288239462Sdim  ///   always a size_t
289212904Sdim  /// \param ElementType - the base element allocated type,
290212904Sdim  ///   i.e. the allocated type after stripping all array types
291212904Sdim  virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
292212904Sdim                                             llvm::Value *NewPtr,
293212904Sdim                                             llvm::Value *NumElements,
294218893Sdim                                             const CXXNewExpr *expr,
295212904Sdim                                             QualType ElementType);
296212904Sdim
297212904Sdim  /// Reads the array cookie associated with the given pointer,
298212904Sdim  /// if it has one.
299212904Sdim  ///
300212904Sdim  /// \param Ptr - a pointer to the first element in the array
301212904Sdim  /// \param ElementType - the base element type of elements of the array
302212904Sdim  /// \param NumElements - an out parameter which will be initialized
303212904Sdim  ///   with the number of elements allocated, or zero if there is no
304212904Sdim  ///   cookie
305212904Sdim  /// \param AllocPtr - an out parameter which will be initialized
306212904Sdim  ///   with a char* pointing to the address returned by the allocation
307212904Sdim  ///   function
308212904Sdim  /// \param CookieSize - an out parameter which will be initialized
309212904Sdim  ///   with the size of the cookie, or zero if there is no cookie
310212904Sdim  virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
311218893Sdim                               const CXXDeleteExpr *expr,
312212904Sdim                               QualType ElementType, llvm::Value *&NumElements,
313212904Sdim                               llvm::Value *&AllocPtr, CharUnits &CookieSize);
314212904Sdim
315239462Sdimprotected:
316239462Sdim  /// Returns the extra size required in order to store the array
317239462Sdim  /// cookie for the given type.  Assumes that an array cookie is
318239462Sdim  /// required.
319239462Sdim  virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
320239462Sdim
321239462Sdim  /// Reads the array cookie for an allocation which is known to have one.
322239462Sdim  /// This is called by the standard implementation of ReadArrayCookie.
323239462Sdim  ///
324239462Sdim  /// \param ptr - a pointer to the allocation made for an array, as a char*
325239462Sdim  /// \param cookieSize - the computed cookie size of an array
326239462Sdim  ///
327239462Sdim  /// Other parameters are as above.
328239462Sdim  ///
329239462Sdim  /// \return a size_t
330239462Sdim  virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
331239462Sdim                                           llvm::Value *ptr,
332239462Sdim                                           CharUnits cookieSize);
333239462Sdim
334239462Sdimpublic:
335239462Sdim
336218893Sdim  /*************************** Static local guards ****************************/
337218893Sdim
338218893Sdim  /// Emits the guarded initializer and destructor setup for the given
339218893Sdim  /// variable, given that it couldn't be emitted as a constant.
340234353Sdim  /// If \p PerformInit is false, the initialization has been folded to a
341234353Sdim  /// constant and should not be performed.
342218893Sdim  ///
343218893Sdim  /// The variable may be:
344218893Sdim  ///   - a static local variable
345218893Sdim  ///   - a static data member of a class template instantiation
346218893Sdim  virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
347234353Sdim                               llvm::GlobalVariable *DeclPtr, bool PerformInit);
348218893Sdim
349239462Sdim  /// Emit code to force the execution of a destructor during global
350239462Sdim  /// teardown.  The default implementation of this uses atexit.
351239462Sdim  ///
352239462Sdim  /// \param dtor - a function taking a single pointer argument
353239462Sdim  /// \param addr - a pointer to pass to the destructor function.
354251662Sdim  virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
355251662Sdim                                  llvm::Constant *dtor, llvm::Constant *addr);
356251662Sdim
357251662Sdim  /*************************** thread_local initialization ********************/
358251662Sdim
359251662Sdim  /// Emits ABI-required functions necessary to initialize thread_local
360251662Sdim  /// variables in this translation unit.
361251662Sdim  ///
362251662Sdim  /// \param Decls The thread_local declarations in this translation unit.
363251662Sdim  /// \param InitFunc If this translation unit contains any non-constant
364251662Sdim  ///        initialization or non-trivial destruction for thread_local
365251662Sdim  ///        variables, a function to perform the initialization. Otherwise, 0.
366251662Sdim  virtual void EmitThreadLocalInitFuncs(
367251662Sdim      llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
368251662Sdim      llvm::Function *InitFunc);
369251662Sdim
370251662Sdim  /// Emit a reference to a non-local thread_local variable (including
371251662Sdim  /// triggering the initialization of all thread_local variables in its
372251662Sdim  /// translation unit).
373251662Sdim  virtual LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
374251662Sdim                                            const DeclRefExpr *DRE);
375249423Sdim};
376239462Sdim
377249423Sdim// Create an instance of a C++ ABI class:
378239462Sdim
379249423Sdim/// Creates an Itanium-family ABI.
380249423SdimCGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
381208600Srdivacky
382249423Sdim/// Creates a Microsoft-family ABI.
383212904SdimCGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
384212904Sdim
385208600Srdivacky}
386208600Srdivacky}
387208600Srdivacky
388208600Srdivacky#endif
389