//===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the LLVM module linker.
//
//===----------------------------------------------------------------------===//
#include "llvm/Linker.h"
#include "llvm-c/Linker.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/Module.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
#include "llvm/TypeFinder.h"
#include <cctype>
using namespace llvm;
//===----------------------------------------------------------------------===//
// TypeMap implementation.
//===----------------------------------------------------------------------===//
namespace {
class TypeMapTy : public ValueMapTypeRemapper {
/// MappedTypes - This is a mapping from a source type to a destination type
/// to use.
DenseMap<Type*, Type*> MappedTypes;
/// SpeculativeTypes - When checking to see if two subgraphs are isomorphic,
/// we speculatively add types to MappedTypes, but keep track of them here in
/// case we need to roll back.
SmallVector<Type*, 16> SpeculativeTypes;
/// SrcDefinitionsToResolve - This is a list of non-opaque structs in the
/// source module that are mapped to an opaque struct in the destination
/// module.
SmallVector<StructType*, 16> SrcDefinitionsToResolve;
/// DstResolvedOpaqueTypes - This is the set of opaque types in the
/// destination modules who are getting a body from the source module.
SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
public:
/// addTypeMapping - Indicate that the specified type in the destination
/// module is conceptually equivalent to the specified type in the source
/// module.
void addTypeMapping(Type *DstTy, Type *SrcTy);
/// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
/// module from a type definition in the source module.
void linkDefinedTypeBodies();
/// get - Return the mapped type to use for the specified input type from the
/// source module.
Type *get(Type *SrcTy);
FunctionType *get(FunctionType *T) {return cast<FunctionType>(get((Type*)T));}
/// dump - Dump out the type map for debugging purposes.
void dump() const {
for (DenseMap<Type*, Type*>::const_iterator
I = MappedTypes.begin(),