Let SourceInfo take charage of and LLVM module.

I.e., a module never escapes from its associated SourceInfo object.

Change-Id: Ib7fe914052e8b1b06b879737f57828739d54e0f0
diff --git a/lib/ExecutionEngine/SourceInfo.h b/lib/ExecutionEngine/SourceInfo.h
index b6a4c54..9026f1c 100644
--- a/lib/ExecutionEngine/SourceInfo.h
+++ b/lib/ExecutionEngine/SourceInfo.h
@@ -19,14 +19,15 @@
 
 #include "Config.h"
 
-#include <llvm/ADT/OwningPtr.h>
 #include <llvm/Module.h>
 
 #include <stddef.h>
 
-namespace bcc {
-  class ScriptCompiled;
+namespace llvm {
+  class LLVMContext;
+}
 
+namespace bcc {
   namespace SourceKind {
     enum SourceType {
       File,
@@ -39,9 +40,12 @@
   private:
     SourceKind::SourceType type;
 
-    llvm::OwningPtr<llvm::Module> module;
     // Note: module should not be a part of union.  Since, we are going to
     // use module to store the pointer to parsed bitcode.
+    llvm::Module *module;
+    // If true, the LLVM context behind the module is shared with others.
+    // Therefore, don't try to destroy the context it when destroy the module.
+    bool shared_context;
 
     union {
       struct {
@@ -62,7 +66,7 @@
 #endif
 
   private:
-    SourceInfo() { }
+    SourceInfo() : module(NULL), shared_context(false) { }
 
   public:
     static SourceInfo *createFromBuffer(char const *resName,
@@ -76,19 +80,22 @@
     static SourceInfo *createFromModule(llvm::Module *module,
                                         unsigned long flags);
 
-    llvm::Module *takeModule() {
-      return module.take();
+    inline llvm::Module *getModule() const {
+      return module;
     }
 
-    llvm::Module *getModule() const {
-      return module.get();
+    inline llvm::LLVMContext *getContext() const {
+      return (module) ? &module->getContext() : NULL;
     }
 
-    int prepareModule(ScriptCompiled *);
+    // Share with the given context if it's provided.
+    int prepareModule(llvm::LLVMContext *context = NULL);
 
 #if USE_CACHE
     template <typename T> void introDependency(T &checker);
 #endif
+
+    ~SourceInfo();
   };