//===--- CGVtable.cpp - Emit LLVM Code for C++ vtables --------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This contains code dealing with C++ code generation of virtual tables.
//
//===----------------------------------------------------------------------===//
#include "CodeGenModule.h"
#include "CodeGenFunction.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/RecordLayout.h"
#include "llvm/ADT/DenseSet.h"
#include <cstdio>
using namespace clang;
using namespace CodeGen;
namespace {
class VtableBuilder {
public:
/// Index_t - Vtable index type.
typedef uint64_t Index_t;
typedef std::vector<std::pair<GlobalDecl,
std::pair<GlobalDecl, ThunkAdjustment> > >
SavedAdjustmentsVectorTy;
private:
// VtableComponents - The components of the vtable being built.
typedef llvm::SmallVector<llvm::Constant *, 64> VtableComponentsVectorTy;
VtableComponentsVectorTy VtableComponents;
const bool BuildVtable;
llvm::Type *Ptr8Ty;
/// MostDerivedClass - The most derived class that this vtable is being
/// built for.
const CXXRecordDecl *MostDerivedClass;
/// LayoutClass - The most derived class used for virtual base layout
/// information.
const CXXRecordDecl *LayoutClass;
/// LayoutOffset - The offset for Class in LayoutClass.
uint64_t LayoutOffset;
/// BLayout - Layout for the most derived class that this vtable is being
/// built for.
const ASTRecordLayout &BLayout;
llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
llvm::Constant *rtti;
llvm::LLVMContext &VMContext;
CodeGenModule &CGM; // Per-module state.
llvm::DenseMap<GlobalDecl, Index_t> VCall;
llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
// This is the offset to the nearest virtual base
llvm::DenseMap<GlobalDecl, Index_t> NonVirtualOffset;
llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
/// PureVirtualFunction - Points to __cxa_pure_virtual.
llvm::Constant *PureVirtualFn;
/// VtableMethods - A data structure for keeping track of methods in a vtable.
/// Can add methods, override methods and iterate in vtable order.
class VtableMethods {
// MethodToIndexMap - Maps from a global decl to the index it has in the
// Methods vector.
llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap;
/// Methods - The methods, in vtable order.
typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy;
MethodsVectorTy Methods;
MethodsVectorTy OrigMethods;
public:
/// AddMethod - Add a method to the vtable methods.
void AddMethod(GlobalDecl GD) {
assert(!MethodToIndexMap.count(GD) &&
"Method has already been added!");
MethodToIndexMap[GD] = Methods.size();
Methods.push_back(GD);
OrigMethods