clang API Documentation

CGVTables.h
Go to the documentation of this file.
00001 //===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This contains code dealing with C++ code generation of virtual tables.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef CLANG_CODEGEN_CGVTABLE_H
00015 #define CLANG_CODEGEN_CGVTABLE_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/GlobalVariable.h"
00019 #include "clang/Basic/ABI.h"
00020 #include "clang/AST/BaseSubobject.h"
00021 #include "clang/AST/CharUnits.h"
00022 #include "clang/AST/GlobalDecl.h"
00023 #include "clang/AST/VTableBuilder.h"
00024 
00025 namespace clang {
00026   class CXXRecordDecl;
00027 
00028 namespace CodeGen {
00029   class CodeGenModule;
00030 
00031 class CodeGenVTables {
00032   CodeGenModule &CGM;
00033 
00034   VTableContext VTContext;
00035 
00036   /// VTables - All the vtables which have been defined.
00037   llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
00038   
00039   /// VTableAddressPointsMapTy - Address points for a single vtable.
00040   typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy;
00041 
00042   typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
00043   typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
00044   
00045   /// SubVTTIndicies - Contains indices into the various sub-VTTs.
00046   SubVTTIndiciesMapTy SubVTTIndicies;
00047 
00048   typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
00049     SecondaryVirtualPointerIndicesMapTy;
00050 
00051   /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
00052   /// indices.
00053   SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
00054 
00055   /// EmitThunk - Emit a single thunk.
00056   void EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk, 
00057                  bool UseAvailableExternallyLinkage);
00058 
00059   /// MaybeEmitThunkAvailableExternally - Try to emit the given thunk with
00060   /// available_externally linkage to allow for inlining of thunks.
00061   /// This will be done iff optimizations are enabled and the member function
00062   /// doesn't contain any incomplete types.
00063   void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk);
00064 
00065   /// CreateVTableInitializer - Create a vtable initializer for the given record
00066   /// decl.
00067   /// \param Components - The vtable components; this is really an array of
00068   /// VTableComponents.
00069   llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD,
00070                                           const VTableComponent *Components, 
00071                                           unsigned NumComponents,
00072                                 const VTableLayout::VTableThunkTy *VTableThunks,
00073                                           unsigned NumVTableThunks);
00074 
00075 public:
00076   CodeGenVTables(CodeGenModule &CGM);
00077 
00078   VTableContext &getVTableContext() { return VTContext; }
00079 
00080   /// \brief True if the VTable of this record must be emitted in the
00081   /// translation unit.
00082   bool ShouldEmitVTableInThisTU(const CXXRecordDecl *RD);
00083 
00084   /// needsVTTParameter - Return whether the given global decl needs a VTT
00085   /// parameter, which it does if it's a base constructor or destructor with
00086   /// virtual bases.
00087   static bool needsVTTParameter(GlobalDecl GD);
00088 
00089   /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
00090   /// given record decl.
00091   uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
00092   
00093   /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
00094   /// virtual pointer for the given subobject is located.
00095   uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
00096                                            BaseSubobject Base);
00097 
00098   /// getAddressPoint - Get the address point of the given subobject in the
00099   /// class decl.
00100   uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD);
00101   
00102   /// GetAddrOfVTable - Get the address of the vtable for the given record decl.
00103   llvm::GlobalVariable *GetAddrOfVTable(const CXXRecordDecl *RD);
00104 
00105   /// EmitVTableDefinition - Emit the definition of the given vtable.
00106   void EmitVTableDefinition(llvm::GlobalVariable *VTable,
00107                             llvm::GlobalVariable::LinkageTypes Linkage,
00108                             const CXXRecordDecl *RD);
00109   
00110   /// GenerateConstructionVTable - Generate a construction vtable for the given 
00111   /// base subobject.
00112   llvm::GlobalVariable *
00113   GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, 
00114                              bool BaseIsVirtual, 
00115                              llvm::GlobalVariable::LinkageTypes Linkage,
00116                              VTableAddressPointsMapTy& AddressPoints);
00117 
00118     
00119   /// GetAddrOfVTable - Get the address of the VTT for the given record decl.
00120   llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
00121 
00122   /// EmitVTTDefinition - Emit the definition of the given vtable.
00123   void EmitVTTDefinition(llvm::GlobalVariable *VTT,
00124                          llvm::GlobalVariable::LinkageTypes Linkage,
00125                          const CXXRecordDecl *RD);
00126 
00127   /// EmitThunks - Emit the associated thunks for the given global decl.
00128   void EmitThunks(GlobalDecl GD);
00129     
00130   /// GenerateClassData - Generate all the class data required to be generated
00131   /// upon definition of a KeyFunction.  This includes the vtable, the
00132   /// rtti data structure and the VTT.
00133   ///
00134   /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
00135   void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
00136                          const CXXRecordDecl *RD);
00137 };
00138 
00139 } // end namespace CodeGen
00140 } // end namespace clang
00141 #endif