]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGCXXABI.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / 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   /// Issue a diagnostic about unsupported features in the ABI.
58   void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S);
59
60   /// Get a null value for unsupported member pointers.
61   llvm::Constant *GetBogusMemberPointer(QualType T);
62
63   // FIXME: Every place that calls getVTT{Decl,Value} is something
64   // that needs to be abstracted properly.
65   ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
66     return CGF.CXXStructorImplicitParamDecl;
67   }
68   llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
69     return CGF.CXXStructorImplicitParamValue;
70   }
71
72   ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
73     return CGF.CXXStructorImplicitParamDecl;
74   }
75   llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
76     return CGF.CXXStructorImplicitParamValue;
77   }
78
79   /// Build a parameter variable suitable for 'this'.
80   void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
81
82   /// Perform prolog initialization of the parameter variable suitable
83   /// for 'this' emitted by BuildThisParam.
84   void EmitThisParam(CodeGenFunction &CGF);
85
86   ASTContext &getContext() const { return CGM.getContext(); }
87
88   virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
89   virtual bool requiresArrayCookie(const CXXNewExpr *E);
90
91 public:
92
93   virtual ~CGCXXABI();
94
95   /// Gets the mangle context.
96   MangleContext &getMangleContext() {
97     return *MangleCtx;
98   }
99
100   /// Returns true if the given constructor or destructor is one of the
101   /// kinds that the ABI says returns 'this' (only applies when called
102   /// non-virtually for destructors).
103   ///
104   /// There currently is no way to indicate if a destructor returns 'this'
105   /// when called virtually, and code generation does not support the case.
106   virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
107
108   /// Returns true if the given record type should be returned indirectly.
109   virtual bool isReturnTypeIndirect(const CXXRecordDecl *RD) const = 0;
110
111   /// Specify how one should pass an argument of a record type.
112   enum RecordArgABI {
113     /// Pass it using the normal C aggregate rules for the ABI, potentially
114     /// introducing extra copies and passing some or all of it in registers.
115     RAA_Default = 0,
116
117     /// Pass it on the stack using its defined layout.  The argument must be
118     /// evaluated directly into the correct stack position in the arguments area,
119     /// and the call machinery must not move it or introduce extra copies.
120     RAA_DirectInMemory,
121
122     /// Pass it as a pointer to temporary memory.
123     RAA_Indirect
124   };
125
126   /// Returns how an argument of the given record type should be passed.
127   virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
128
129   /// Find the LLVM type used to represent the given member pointer
130   /// type.
131   virtual llvm::Type *
132   ConvertMemberPointerType(const MemberPointerType *MPT);
133
134   /// Load a member function from an object and a member function
135   /// pointer.  Apply the this-adjustment and set 'This' to the
136   /// adjusted value.
137   virtual llvm::Value *
138   EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
139                                   llvm::Value *&This,
140                                   llvm::Value *MemPtr,
141                                   const MemberPointerType *MPT);
142
143   /// Calculate an l-value from an object and a data member pointer.
144   virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
145                                                     llvm::Value *Base,
146                                                     llvm::Value *MemPtr,
147                                             const MemberPointerType *MPT);
148
149   /// Perform a derived-to-base, base-to-derived, or bitcast member
150   /// pointer conversion.
151   virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
152                                                    const CastExpr *E,
153                                                    llvm::Value *Src);
154
155   /// Perform a derived-to-base, base-to-derived, or bitcast member
156   /// pointer conversion on a constant value.
157   virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
158                                                       llvm::Constant *Src);
159
160   /// Return true if the given member pointer can be zero-initialized
161   /// (in the C++ sense) with an LLVM zeroinitializer.
162   virtual bool isZeroInitializable(const MemberPointerType *MPT);
163
164   /// Create a null member pointer of the given type.
165   virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
166
167   /// Create a member pointer for the given method.
168   virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
169
170   /// Create a member pointer for the given field.
171   virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
172                                                 CharUnits offset);
173
174   /// Create a member pointer for the given member pointer constant.
175   virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
176
177   /// Emit a comparison between two member pointers.  Returns an i1.
178   virtual llvm::Value *
179   EmitMemberPointerComparison(CodeGenFunction &CGF,
180                               llvm::Value *L,
181                               llvm::Value *R,
182                               const MemberPointerType *MPT,
183                               bool Inequality);
184
185   /// Determine if a member pointer is non-null.  Returns an i1.
186   virtual llvm::Value *
187   EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
188                              llvm::Value *MemPtr,
189                              const MemberPointerType *MPT);
190
191 protected:
192   /// A utility method for computing the offset required for the given
193   /// base-to-derived or derived-to-base member-pointer conversion.
194   /// Does not handle virtual conversions (in case we ever fully
195   /// support an ABI that allows this).  Returns null if no adjustment
196   /// is required.
197   llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
198
199   /// \brief Computes the non-virtual adjustment needed for a member pointer
200   /// conversion along an inheritance path stored in an APValue.  Unlike
201   /// getMemberPointerAdjustment(), the adjustment can be negative if the path
202   /// is from a derived type to a base type.
203   CharUnits getMemberPointerPathAdjustment(const APValue &MP);
204
205 public:
206   /// Adjust the given non-null pointer to an object of polymorphic
207   /// type to point to the complete object.
208   ///
209   /// The IR type of the result should be a pointer but is otherwise
210   /// irrelevant.
211   virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
212                                               llvm::Value *ptr,
213                                               QualType type) = 0;
214
215   virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
216                                                  llvm::Value *This,
217                                                  const CXXRecordDecl *ClassDecl,
218                                         const CXXRecordDecl *BaseClassDecl) = 0;
219
220   /// Build the signature of the given constructor variant by adding
221   /// any required parameters.  For convenience, ArgTys has been initialized
222   /// with the type of 'this' and ResTy has been initialized with the type of
223   /// 'this' if HasThisReturn(GlobalDecl(Ctor, T)) is true or 'void' otherwise
224   /// (although both may be changed by the ABI).
225   ///
226   /// If there are ever any ABIs where the implicit parameters are
227   /// intermixed with the formal parameters, we can address those
228   /// then.
229   virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
230                                          CXXCtorType T,
231                                          CanQualType &ResTy,
232                                SmallVectorImpl<CanQualType> &ArgTys) = 0;
233
234   virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
235                                                           const CXXRecordDecl *RD);
236
237   /// Emit the code to initialize hidden members required
238   /// to handle virtual inheritance, if needed by the ABI.
239   virtual void
240   initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
241                                             const CXXRecordDecl *RD) {}
242
243   /// Emit constructor variants required by this ABI.
244   virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0;
245
246   /// Build the signature of the given destructor variant by adding
247   /// any required parameters.  For convenience, ArgTys has been initialized
248   /// with the type of 'this' and ResTy has been initialized with the type of
249   /// 'this' if HasThisReturn(GlobalDecl(Dtor, T)) is true or 'void' otherwise
250   /// (although both may be changed by the ABI).
251   virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
252                                         CXXDtorType T,
253                                         CanQualType &ResTy,
254                                SmallVectorImpl<CanQualType> &ArgTys) = 0;
255
256   /// Returns true if the given destructor type should be emitted as a linkonce
257   /// delegating thunk, regardless of whether the dtor is defined in this TU or
258   /// not.
259   virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
260                                       CXXDtorType DT) const = 0;
261
262   /// Emit destructor variants required by this ABI.
263   virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0;
264
265   /// Get the type of the implicit "this" parameter used by a method. May return
266   /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
267   /// parameter to point to some artificial offset in a complete object due to
268   /// vbases being reordered.
269   virtual const CXXRecordDecl *
270   getThisArgumentTypeForMethod(const CXXMethodDecl *MD) {
271     return MD->getParent();
272   }
273
274   /// Perform ABI-specific "this" argument adjustment required prior to
275   /// a virtual function call.
276   virtual llvm::Value *adjustThisArgumentForVirtualCall(CodeGenFunction &CGF,
277                                                         GlobalDecl GD,
278                                                         llvm::Value *This) {
279     return This;
280   }
281
282   /// Build the ABI-specific portion of the parameter list for a
283   /// function.  This generally involves a 'this' parameter and
284   /// possibly some extra data for constructors and destructors.
285   ///
286   /// ABIs may also choose to override the return type, which has been
287   /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
288   /// the formal return type of the function otherwise.
289   virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
290                                            QualType &ResTy,
291                                            FunctionArgList &Params) = 0;
292
293   /// Perform ABI-specific "this" parameter adjustment in a virtual function
294   /// prologue.
295   virtual llvm::Value *adjustThisParameterInVirtualFunctionPrologue(
296       CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) {
297     return This;
298   }
299
300   /// Emit the ABI-specific prolog for the function.
301   virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
302
303   /// Emit the constructor call. Return the function that is called.
304   virtual void EmitConstructorCall(CodeGenFunction &CGF,
305                                    const CXXConstructorDecl *D,
306                                    CXXCtorType Type,
307                                    bool ForVirtualBase, bool Delegating,
308                                    llvm::Value *This,
309                                    CallExpr::const_arg_iterator ArgBeg,
310                                    CallExpr::const_arg_iterator ArgEnd) = 0;
311
312   /// Emits the VTable definitions required for the given record type.
313   virtual void emitVTableDefinitions(CodeGenVTables &CGVT,
314                                      const CXXRecordDecl *RD) = 0;
315
316   /// Get the address point of the vtable for the given base subobject while
317   /// building a constructor or a destructor. On return, NeedsVirtualOffset
318   /// tells if a virtual base adjustment is needed in order to get the offset
319   /// of the base subobject.
320   virtual llvm::Value *getVTableAddressPointInStructor(
321       CodeGenFunction &CGF, const CXXRecordDecl *RD, BaseSubobject Base,
322       const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) = 0;
323
324   /// Get the address point of the vtable for the given base subobject while
325   /// building a constexpr.
326   virtual llvm::Constant *
327   getVTableAddressPointForConstExpr(BaseSubobject Base,
328                                     const CXXRecordDecl *VTableClass) = 0;
329
330   /// Get the address of the vtable for the given record decl which should be
331   /// used for the vptr at the given offset in RD.
332   virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
333                                                 CharUnits VPtrOffset) = 0;
334
335   /// Build a virtual function pointer in the ABI-specific way.
336   virtual llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF,
337                                                  GlobalDecl GD,
338                                                  llvm::Value *This,
339                                                  llvm::Type *Ty) = 0;
340
341   /// Emit the ABI-specific virtual destructor call.
342   virtual void EmitVirtualDestructorCall(CodeGenFunction &CGF,
343                                          const CXXDestructorDecl *Dtor,
344                                          CXXDtorType DtorType,
345                                          SourceLocation CallLoc,
346                                          llvm::Value *This) = 0;
347
348   virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
349                                                 GlobalDecl GD,
350                                                 CallArgList &CallArgs) {}
351
352   /// Emit any tables needed to implement virtual inheritance.  For Itanium,
353   /// this emits virtual table tables.  For the MSVC++ ABI, this emits virtual
354   /// base tables.
355   virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0;
356
357   virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) = 0;
358
359   virtual llvm::Value *performThisAdjustment(CodeGenFunction &CGF,
360                                              llvm::Value *This,
361                                              const ThisAdjustment &TA) = 0;
362
363   virtual llvm::Value *performReturnAdjustment(CodeGenFunction &CGF,
364                                                llvm::Value *Ret,
365                                                const ReturnAdjustment &RA) = 0;
366
367   virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
368                                    RValue RV, QualType ResultType);
369
370   /// Gets the pure virtual member call function.
371   virtual StringRef GetPureVirtualCallName() = 0;
372
373   /// Gets the deleted virtual member call name.
374   virtual StringRef GetDeletedVirtualCallName() = 0;
375
376   /// \brief Returns true iff static data members that are initialized in the
377   /// class definition should have linkonce linkage.
378   virtual bool isInlineInitializedStaticDataMemberLinkOnce() { return false; }
379
380   /**************************** Array cookies ******************************/
381
382   /// Returns the extra size required in order to store the array
383   /// cookie for the given new-expression.  May return 0 to indicate that no
384   /// array cookie is required.
385   ///
386   /// Several cases are filtered out before this method is called:
387   ///   - non-array allocations never need a cookie
388   ///   - calls to \::operator new(size_t, void*) never need a cookie
389   ///
390   /// \param expr - the new-expression being allocated.
391   virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
392
393   /// Initialize the array cookie for the given allocation.
394   ///
395   /// \param NewPtr - a char* which is the presumed-non-null
396   ///   return value of the allocation function
397   /// \param NumElements - the computed number of elements,
398   ///   potentially collapsed from the multidimensional array case;
399   ///   always a size_t
400   /// \param ElementType - the base element allocated type,
401   ///   i.e. the allocated type after stripping all array types
402   virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
403                                              llvm::Value *NewPtr,
404                                              llvm::Value *NumElements,
405                                              const CXXNewExpr *expr,
406                                              QualType ElementType);
407
408   /// Reads the array cookie associated with the given pointer,
409   /// if it has one.
410   ///
411   /// \param Ptr - a pointer to the first element in the array
412   /// \param ElementType - the base element type of elements of the array
413   /// \param NumElements - an out parameter which will be initialized
414   ///   with the number of elements allocated, or zero if there is no
415   ///   cookie
416   /// \param AllocPtr - an out parameter which will be initialized
417   ///   with a char* pointing to the address returned by the allocation
418   ///   function
419   /// \param CookieSize - an out parameter which will be initialized
420   ///   with the size of the cookie, or zero if there is no cookie
421   virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
422                                const CXXDeleteExpr *expr,
423                                QualType ElementType, llvm::Value *&NumElements,
424                                llvm::Value *&AllocPtr, CharUnits &CookieSize);
425
426   /// Return whether the given global decl needs a VTT parameter.
427   virtual bool NeedsVTTParameter(GlobalDecl GD);
428
429 protected:
430   /// Returns the extra size required in order to store the array
431   /// cookie for the given type.  Assumes that an array cookie is
432   /// required.
433   virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
434
435   /// Reads the array cookie for an allocation which is known to have one.
436   /// This is called by the standard implementation of ReadArrayCookie.
437   ///
438   /// \param ptr - a pointer to the allocation made for an array, as a char*
439   /// \param cookieSize - the computed cookie size of an array
440   ///
441   /// Other parameters are as above.
442   ///
443   /// \return a size_t
444   virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
445                                            llvm::Value *ptr,
446                                            CharUnits cookieSize);
447
448 public:
449
450   /*************************** Static local guards ****************************/
451
452   /// Emits the guarded initializer and destructor setup for the given
453   /// variable, given that it couldn't be emitted as a constant.
454   /// If \p PerformInit is false, the initialization has been folded to a
455   /// constant and should not be performed.
456   ///
457   /// The variable may be:
458   ///   - a static local variable
459   ///   - a static data member of a class template instantiation
460   virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
461                                llvm::GlobalVariable *DeclPtr,
462                                bool PerformInit) = 0;
463
464   /// Emit code to force the execution of a destructor during global
465   /// teardown.  The default implementation of this uses atexit.
466   ///
467   /// \param dtor - a function taking a single pointer argument
468   /// \param addr - a pointer to pass to the destructor function.
469   virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
470                                   llvm::Constant *dtor, llvm::Constant *addr);
471
472   /*************************** thread_local initialization ********************/
473
474   /// Emits ABI-required functions necessary to initialize thread_local
475   /// variables in this translation unit.
476   ///
477   /// \param Decls The thread_local declarations in this translation unit.
478   /// \param InitFunc If this translation unit contains any non-constant
479   ///        initialization or non-trivial destruction for thread_local
480   ///        variables, a function to perform the initialization. Otherwise, 0.
481   virtual void EmitThreadLocalInitFuncs(
482       llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
483       llvm::Function *InitFunc);
484
485   /// Emit a reference to a non-local thread_local variable (including
486   /// triggering the initialization of all thread_local variables in its
487   /// translation unit).
488   virtual LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
489                                             const DeclRefExpr *DRE);
490 };
491
492 // Create an instance of a C++ ABI class:
493
494 /// Creates an Itanium-family ABI.
495 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
496
497 /// Creates a Microsoft-family ABI.
498 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
499
500 }
501 }
502
503 #endif