[ScriptInterpreter] Move ownership into debugger (NFC)

This is part two of the change started in r359330. This patch moves the
ownership of the script interpreter from the command interpreter into
the debugger. I would've preferred to remove the lazy initialization,
however the fact that the scripting language is set after the debugger
is created makes that tricky. So for now this does exactly the same
thing as when it was under the command interpreter. The result is that
this patch is fully NFC.

Differential revision: https://reviews.llvm.org/D61211

llvm-svn: 359354
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index 9c56905..7f180c1 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -767,8 +767,8 @@
       m_source_manager_up(), m_source_file_cache(),
       m_command_interpreter_up(
           llvm::make_unique<CommandInterpreter>(*this, false)),
-      m_input_reader_stack(), m_instance_name(), m_loaded_plugins(),
-      m_event_handler_thread(), m_io_handler_thread(),
+      m_script_interpreter_sp(), m_input_reader_stack(), m_instance_name(),
+      m_loaded_plugins(), m_event_handler_thread(), m_io_handler_thread(),
       m_sync_broadcaster(nullptr, "lldb.debugger.sync"),
       m_forward_listener_sp(), m_clear_once() {
   char instance_cstr[256];
@@ -905,12 +905,10 @@
   if (!out_file.IsValid())
     out_file.SetStream(stdout, false);
 
-  // do not create the ScriptInterpreter just for setting the output file
-  // handle as the constructor will know how to do the right thing on its own
-  const bool can_create = false;
-  ScriptInterpreter *script_interpreter =
-      GetCommandInterpreter().GetScriptInterpreter(can_create);
-  if (script_interpreter)
+  // Do not create the ScriptInterpreter just for setting the output file
+  // handle as the constructor will know how to do the right thing on its own.
+  if (ScriptInterpreter *script_interpreter =
+          GetScriptInterpreter(/*can_create=*/false))
     script_interpreter->ResetOutputFileHandle(fh);
 }
 
@@ -1288,6 +1286,19 @@
                                error_stream);
 }
 
+ScriptInterpreter *Debugger::GetScriptInterpreter(bool can_create) {
+  std::lock_guard<std::recursive_mutex> locker(m_script_interpreter_mutex);
+
+  if (!m_script_interpreter_sp) {
+    if (!can_create)
+      return nullptr;
+    m_script_interpreter_sp = PluginManager::GetScriptInterpreterForLanguage(
+        GetScriptLanguage(), *this);
+  }
+
+  return m_script_interpreter_sp.get();
+}
+
 SourceManager &Debugger::GetSourceManager() {
   if (!m_source_manager_up)
     m_source_manager_up = llvm::make_unique<SourceManager>(shared_from_this());
diff --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp
index 78c639aa..6ebe0e2 100644
--- a/lldb/source/Core/FormatEntity.cpp
+++ b/lldb/source/Core/FormatEntity.cpp
@@ -410,7 +410,7 @@
 
   if (target) {
     ScriptInterpreter *script_interpreter =
-        target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
+        target->GetDebugger().GetScriptInterpreter();
     if (script_interpreter) {
       Status error;
       std::string script_output;
diff --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp
index 9e7be79..153d5a7 100644
--- a/lldb/source/Core/Module.cpp
+++ b/lldb/source/Core/Module.cpp
@@ -1545,8 +1545,7 @@
 
     const uint32_t num_specs = file_specs.GetSize();
     if (num_specs) {
-      ScriptInterpreter *script_interpreter =
-          debugger.GetCommandInterpreter().GetScriptInterpreter();
+      ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
       if (script_interpreter) {
         for (uint32_t i = 0; i < num_specs; ++i) {
           FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i));