Turn some DenseMaps that are only used for set operations into DenseSets.
[oota-llvm.git] / include / llvm / Linker / Linker.h
1 //===- Linker.h - Module Linker Interface -----------------------*- 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 #ifndef LLVM_LINKER_LINKER_H
11 #define LLVM_LINKER_LINKER_H
12
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseSet.h"
16
17 #include <functional>
18
19 namespace llvm {
20 class DiagnosticInfo;
21 class Module;
22 class StructType;
23 class Type;
24
25 /// This class provides the core functionality of linking in LLVM. It keeps a
26 /// pointer to the merged module so far. It doesn't take ownership of the
27 /// module since it is assumed that the user of this class will want to do
28 /// something with it after the linking.
29 class Linker {
30 public:
31   typedef std::function<void(const DiagnosticInfo &)> DiagnosticHandlerFunction;
32
33   struct StructTypeKeyInfo {
34     struct KeyTy {
35       ArrayRef<Type *> ETypes;
36       bool IsPacked;
37       KeyTy(ArrayRef<Type *> E, bool P);
38       KeyTy(const StructType *ST);
39       bool operator==(const KeyTy &that) const;
40       bool operator!=(const KeyTy &that) const;
41     };
42     static StructType *getEmptyKey();
43     static StructType *getTombstoneKey();
44     static unsigned getHashValue(const KeyTy &Key);
45     static unsigned getHashValue(const StructType *ST);
46     static bool isEqual(const KeyTy &LHS, const StructType *RHS);
47     static bool isEqual(const StructType *LHS, const StructType *RHS);
48   };
49
50   typedef DenseSet<StructType *, StructTypeKeyInfo> NonOpaqueStructTypeSet;
51   typedef DenseSet<StructType *> OpaqueStructTypeSet;
52
53   struct IdentifiedStructTypeSet {
54     // The set of opaque types is the composite module.
55     OpaqueStructTypeSet OpaqueStructTypes;
56
57     // The set of identified but non opaque structures in the composite module.
58     NonOpaqueStructTypeSet NonOpaqueStructTypes;
59
60     void addNonOpaque(StructType *Ty);
61     void addOpaque(StructType *Ty);
62     StructType *findNonOpaque(ArrayRef<Type *> ETypes, bool IsPacked);
63     bool hasType(StructType *Ty);
64   };
65
66   Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler);
67   Linker(Module *M);
68   ~Linker();
69
70   Module *getModule() const { return Composite; }
71   void deleteModule();
72
73   /// \brief Link \p Src into the composite. The source is destroyed.
74   /// Returns true on error.
75   bool linkInModule(Module *Src);
76
77   static bool LinkModules(Module *Dest, Module *Src,
78                           DiagnosticHandlerFunction DiagnosticHandler);
79
80   static bool LinkModules(Module *Dest, Module *Src);
81
82 private:
83   void init(Module *M, DiagnosticHandlerFunction DiagnosticHandler);
84   Module *Composite;
85
86   IdentifiedStructTypeSet IdentifiedStructTypes;
87
88   DiagnosticHandlerFunction DiagnosticHandler;
89 };
90
91 } // End llvm namespace
92
93 #endif