Linker.h 3.0 KB

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