Add a pointer to the owning LLVMContext to Module.  This requires threading LLVMContext through a lot
of the bitcode reader and ASM parser APIs, as well as supporting it in all of the tools.

Patches for Clang and LLVM-GCC to follow.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74614 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/llvm-c/BitReader.h b/include/llvm-c/BitReader.h
index e30b431..4284d7d 100644
--- a/include/llvm-c/BitReader.h
+++ b/include/llvm-c/BitReader.h
@@ -29,13 +29,14 @@
 /* Builds a module from the bitcode in the specified memory buffer, returning a
    reference to the module via the OutModule parameter. Returns 0 on success.
    Optionally returns a human-readable error message via OutMessage. */ 
-int LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
+int LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMContextRef ContextRef,
                      LLVMModuleRef *OutModule, char **OutMessage);
 
 /* Reads a module from the specified path, returning via the OutMP parameter
    a module provider which performs lazy deserialization. Returns 0 on success.
    Optionally returns a human-readable error message via OutMessage. */ 
 int LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
+                                 LLVMContextRef ContextRef,
                                  LLVMModuleProviderRef *OutMP,
                                  char **OutMessage);
 
diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h
index d2d8845..7019b4c 100644
--- a/include/llvm-c/Core.h
+++ b/include/llvm-c/Core.h
@@ -47,6 +47,11 @@
 /* Opaque types. */
 
 /**
+ * The top-level container for all LLVM global data.  See the LLVMContext class.
+ */
+typedef struct LLVMCtxt *LLVMContextRef;
+
+/**
  * The top-level container for all other LLVM Intermediate Representation (IR)
  * objects. See the llvm::Module class.
  */
@@ -188,6 +193,10 @@
 
 /*===-- Modules -----------------------------------------------------------===*/
 
+/* Create and destroy contexts. */
+LLVMContextRef LLVMContextCreate();
+void LLVMContextDispose(LLVMContextRef C);
+
 /* Create and destroy modules. */ 
 /** See llvm::Module::Module. */
 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID);
@@ -815,6 +824,7 @@
   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PATypeHolder,       LLVMTypeHandleRef    )
   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ModuleProvider,     LLVMModuleProviderRef)
   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer,       LLVMMemoryBufferRef  )
+  DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext,        LLVMContextRef       )
   DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBase,    LLVMPassManagerRef   )
   
   #undef DEFINE_STDCXX_CONVERSION_FUNCTIONS
diff --git a/include/llvm/Assembly/Parser.h b/include/llvm/Assembly/Parser.h
index e4a38e4..2a5bac7 100644
--- a/include/llvm/Assembly/Parser.h
+++ b/include/llvm/Assembly/Parser.h
@@ -21,6 +21,7 @@
 class Module;
 class ParseError;
 class raw_ostream;
+class LLVMContext;
 
 /// This function is the main interface to the LLVM Assembly Parser. It parses
 /// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
@@ -30,7 +31,8 @@
 /// @brief Parse LLVM Assembly from a file
 Module *ParseAssemblyFile(
   const std::string &Filename, ///< The name of the file to parse
-  ParseError &Error            ///< If not null, an object to return errors in.
+  ParseError &Error,           ///< If not null, an object to return errors in.
+  LLVMContext* Context         ///< Context in which to allocate globals info.
 );
 
 /// The function is a secondary interface to the LLVM Assembly Parser. It parses
@@ -42,7 +44,8 @@
 Module *ParseAssemblyString(
   const char *AsmString, ///< The string containing assembly
   Module *M,             ///< A module to add the assembly too.
-  ParseError &Error      ///< If not null, an object to return errors in.
+  ParseError &Error,     ///< If not null, an object to return errors in.
+  LLVMContext* Context
 );
 
 //===------------------------------------------------------------------------===
diff --git a/include/llvm/Bitcode/Archive.h b/include/llvm/Bitcode/Archive.h
index a3631ac..c188df8 100644
--- a/include/llvm/Bitcode/Archive.h
+++ b/include/llvm/Bitcode/Archive.h
@@ -32,6 +32,7 @@
 class Module;              // From VMCore
 class Archive;             // Declared below
 class ArchiveMemberHeader; // Internal implementation class
+class LLVMContext;         // Global data
 
 /// This class is the main class manipulated by users of the Archive class. It
 /// holds information about one member of the Archive. It is also the element
@@ -278,7 +279,8 @@
     /// @returns An Archive* that represents the new archive file.
     /// @brief Create an empty Archive.
     static Archive* CreateEmpty(
-      const sys::Path& Filename ///< Name of the archive to (eventually) create.
+      const sys::Path& Filename,///< Name of the archive to (eventually) create.
+      LLVMContext* C            ///< Context to use for global information
     );
 
     /// Open an existing archive and load its contents in preparation for
@@ -289,6 +291,7 @@
     /// @brief Open and load an archive file
     static Archive* OpenAndLoad(
       const sys::Path& filePath,  ///< The file path to open and load
+      LLVMContext* C,             ///< The context to use for global information
       std::string* ErrorMessage   ///< An optional error string
     );
 
@@ -310,6 +313,7 @@
     /// @brief Open an existing archive and load its symbols.
     static Archive* OpenAndLoadSymbols(
       const sys::Path& Filename,   ///< Name of the archive file to open
+      LLVMContext* C,              ///< The context to use for global info
       std::string* ErrorMessage=0  ///< An optional error string
     );
 
@@ -449,7 +453,7 @@
   protected:
     /// @brief Construct an Archive for \p filename and optionally  map it
     /// into memory.
-    explicit Archive(const sys::Path& filename);
+    explicit Archive(const sys::Path& filename, LLVMContext* C);
 
     /// @param data The symbol table data to be parsed
     /// @param len  The length of the symbol table data
@@ -530,6 +534,7 @@
     unsigned firstFileOffset; ///< Offset to first normal file.
     ModuleMap modules;        ///< The modules loaded via symbol lookup.
     ArchiveMember* foreignST; ///< This holds the foreign symbol table.
+    LLVMContext* Context;     ///< This holds global data.
   /// @}
   /// @name Hidden
   /// @{
diff --git a/include/llvm/Bitcode/ReaderWriter.h b/include/llvm/Bitcode/ReaderWriter.h
index abdd5d3..a781187 100644
--- a/include/llvm/Bitcode/ReaderWriter.h
+++ b/include/llvm/Bitcode/ReaderWriter.h
@@ -23,6 +23,7 @@
   class MemoryBuffer;
   class ModulePass;
   class BitstreamWriter;
+  class LLVMContext;
   class raw_ostream;
   
   /// getBitcodeModuleProvider - Read the header of the specified bitcode buffer
@@ -31,12 +32,14 @@
   /// error, this returns null, *does not* take ownership of Buffer, and fills
   /// in *ErrMsg with an error description if ErrMsg is non-null.
   ModuleProvider *getBitcodeModuleProvider(MemoryBuffer *Buffer,
+                                           LLVMContext* Context,
                                            std::string *ErrMsg = 0);
 
   /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
   /// If an error occurs, this returns null and fills in *ErrMsg if it is
   /// non-null.  This method *never* takes ownership of Buffer.
-  Module *ParseBitcodeFile(MemoryBuffer *Buffer, std::string *ErrMsg = 0);
+  Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext* Context,
+                           std::string *ErrMsg = 0);
 
   /// WriteBitcodeToFile - Write the specified module to the specified output
   /// stream.
diff --git a/include/llvm/Debugger/Debugger.h b/include/llvm/Debugger/Debugger.h
index 5b0b97a..ed04ed5 100644
--- a/include/llvm/Debugger/Debugger.h
+++ b/include/llvm/Debugger/Debugger.h
@@ -20,6 +20,7 @@
 namespace llvm {
   class Module;
   class InferiorProcess;
+  class LLVMContext;
 
   /// Debugger class - This class implements the LLVM source-level debugger.
   /// This allows clients to handle the user IO processing without having to
@@ -95,7 +96,7 @@
     /// the PATH for the specified program, loading it when found.  If the
     /// specified program cannot be found, an exception is thrown to indicate
     /// the error.
-    void loadProgram(const std::string &Path);
+    void loadProgram(const std::string &Path, LLVMContext* Context);
 
     /// unloadProgram - If a program is running, kill it, then unload all traces
     /// of the current program.  If no program is loaded, this method silently
diff --git a/include/llvm/LinkAllVMCore.h b/include/llvm/LinkAllVMCore.h
index 4c428a0..3c4b9c4 100644
--- a/include/llvm/LinkAllVMCore.h
+++ b/include/llvm/LinkAllVMCore.h
@@ -44,7 +44,7 @@
       // to know that getenv() never returns -1, this will do the job.
       if (std::getenv("bar") != (char*) -1)
         return;
-      llvm::Module* M = new llvm::Module("");
+      llvm::Module* M = new llvm::Module("", 0);
       (void)new llvm::UnreachableInst();
       (void)    llvm::createVerifierPass(); 
       (void) new llvm::Mangler(*M,"");
diff --git a/include/llvm/Linker.h b/include/llvm/Linker.h
index 884e872..8389dc7 100644
--- a/include/llvm/Linker.h
+++ b/include/llvm/Linker.h
@@ -21,6 +21,7 @@
 namespace llvm {
 
 class Module;
+class LLVMContext;
 
 /// This class provides the core functionality of linking in LLVM. It retains a
 /// Module object which is the composite of the modules and libraries linked
@@ -66,6 +67,7 @@
     Linker(
         const std::string& progname, ///< name of tool running linker
         const std::string& modulename, ///< name of linker's end-result module
+        LLVMContext* C, ///< Context for global info
         unsigned Flags = 0  ///< ControlFlags (one or more |'d together)
     );
 
@@ -283,6 +285,7 @@
   /// @name Data
   /// @{
   private:
+    LLVMContext* Context; ///< The context for global information
     Module* Composite; ///< The composite module linked together
     std::vector<sys::Path> LibPaths; ///< The library search paths
     unsigned Flags;    ///< Flags to control optional behavior.
diff --git a/include/llvm/Module.h b/include/llvm/Module.h
index 9c8607a..8370ffb 100644
--- a/include/llvm/Module.h
+++ b/include/llvm/Module.h
@@ -25,6 +25,7 @@
 
 class GlobalValueRefMap;   // Used by ConstantVals.cpp
 class FunctionType;
+class LLVMContext;
 
 template<> struct ilist_traits<Function>
   : public SymbolTableListTraits<Function, Module> {
@@ -109,6 +110,8 @@
 /// @name Member Variables
 /// @{
 private:
+  LLVMContext* Context;          ///< The LLVMContext from which types and
+                                 ///< constants are allocated.
   GlobalListType GlobalList;     ///< The Global Variables in the module
   FunctionListType FunctionList; ///< The Functions in the module
   AliasListType AliasList;       ///< The Aliases in the module
@@ -128,7 +131,7 @@
 public:
   /// The Module constructor. Note that there is no default constructor. You
   /// must provide a name for the module upon construction.
-  explicit Module(const std::string &ModuleID);
+  explicit Module(const std::string &ModuleID, LLVMContext* C);
   /// The module destructor. This will dropAllReferences.
   ~Module();
 
@@ -157,6 +160,10 @@
   /// @returns PointerSize - an enumeration for the size of the target's pointer
   PointerSize getPointerSize() const;
 
+  /// Get the global data context.
+  /// @returns LLVMContext - a container for LLVM's global information
+  LLVMContext* getContext() const { return Context; }
+
   /// Get any module-scope inline assembly blocks.
   /// @returns a string containing the module-scope inline assembly blocks.
   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
diff --git a/include/llvm/Transforms/Utils/Cloning.h b/include/llvm/Transforms/Utils/Cloning.h
index 1e2bbaa..840d970 100644
--- a/include/llvm/Transforms/Utils/Cloning.h
+++ b/include/llvm/Transforms/Utils/Cloning.h
@@ -37,6 +37,7 @@
 class CallGraph;
 class TargetData;
 class LoopInfo;
+class LLVMContext;
 template<class N> class LoopBase;
 typedef LoopBase<BasicBlock> Loop;