Factored out handling of the source code for an
expression into a separate class.  This class
encapsulates wrapping the function as needed.  I
am also moving from using booleans to indicate
what the expression's language should be to using
lldb::LanguageType instead.


git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@140545 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Expression/ClangUserExpression.cpp b/source/Expression/ClangUserExpression.cpp
index 73ed043..b4f4901 100644
--- a/source/Expression/ClangUserExpression.cpp
+++ b/source/Expression/ClangUserExpression.cpp
@@ -28,6 +28,7 @@
 #include "lldb/Expression/ClangExpressionParser.h"
 #include "lldb/Expression/ClangFunction.h"
 #include "lldb/Expression/ClangUserExpression.h"
+#include "lldb/Expression/ExpressionSourceCode.h"
 #include "lldb/Host/Host.h"
 #include "lldb/Symbol/VariableList.h"
 #include "lldb/Target/ExecutionContext.h"
@@ -106,6 +107,7 @@
         if (method_decl->isInstance())
         {
             m_cplusplus = true;
+            m_needs_object_ptr = true;
             
             do {
                 clang::QualType this_type = method_decl->getThisType(decl_context->getParentASTContext());
@@ -122,7 +124,10 @@
     else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
     {
         if (method_decl->isInstanceMethod())
+        {
             m_objectivec = true;
+            m_needs_object_ptr = true;
+        }
     }
 }
 
@@ -185,61 +190,23 @@
     ApplyObjcCastHack(m_expr_text);
     //ApplyUnicharHack(m_expr_text);
 
+    std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
+    
+    lldb::LanguageType lang_type;
+    
     if (m_cplusplus)
-    {
-        m_transformed_stream.Printf("%s                                     \n"
-                                    "typedef unsigned short unichar;        \n"
-                                    "void                                   \n"
-                                    "$__lldb_class::%s(void *$__lldb_arg) %s\n"
-                                    "{                                      \n"
-                                    "    %s;                                \n" 
-                                    "}                                      \n",
-                                    m_expr_prefix.c_str(),
-                                    FunctionName(),
-                                    (m_const_object ? "const" : ""),
-                                    m_expr_text.c_str());
-        
-        m_needs_object_ptr = true;
-    }
-    else if (m_objectivec)
-    {
-        const char *function_name = FunctionName();
-        
-        m_transformed_stream.Printf("%s                                                     \n"
-                                    "typedef unsigned short unichar;                        \n"
-                                    "@interface $__lldb_objc_class ($__lldb_category)       \n"
-                                    "-(void)%s:(void *)$__lldb_arg;                         \n"
-                                    "@end                                                   \n"
-                                    "@implementation $__lldb_objc_class ($__lldb_category)  \n"
-                                    "-(void)%s:(void *)$__lldb_arg                          \n"
-                                    "{                                                      \n"
-                                    "    %s;                                                \n"
-                                    "}                                                      \n"
-                                    "@end                                                   \n",
-                                    m_expr_prefix.c_str(),
-                                    function_name,
-                                    function_name,
-                                    m_expr_text.c_str());
-        
-        m_needs_object_ptr = true;
-    }
+        lang_type = lldb::eLanguageTypeC_plus_plus;
+    else if(m_objectivec)
+        lang_type = lldb::eLanguageTypeObjC;
     else
+        lang_type = lldb::eLanguageTypeC;
+    
+    if (!source_code->GetText(m_transformed_text, lang_type, m_const_object))
     {
-        m_transformed_stream.Printf("%s                             \n"
-                                    "typedef unsigned short unichar;\n"
-                                    "void                           \n"
-                                    "%s(void *$__lldb_arg)          \n"
-                                    "{                              \n"
-                                    "    %s;                        \n" 
-                                    "}                              \n",
-                                    m_expr_prefix.c_str(),
-                                    FunctionName(),
-                                    m_expr_text.c_str());
+        error_stream.PutCString ("error: couldn't construct expression body");
+        return false;
     }
     
-    m_transformed_text = m_transformed_stream.GetData();
-    
-    
     if (log)
         log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
     
diff --git a/source/Expression/ExpressionSourceCode.cpp b/source/Expression/ExpressionSourceCode.cpp
new file mode 100644
index 0000000..4b749b5
--- /dev/null
+++ b/source/Expression/ExpressionSourceCode.cpp
@@ -0,0 +1,88 @@
+//===-- ExpressionSourceCode.cpp --------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Expression/ExpressionSourceCode.h"
+
+#include "lldb/Core/StreamString.h"
+
+using namespace lldb_private;
+
+bool ExpressionSourceCode::GetText (std::string &text, lldb::LanguageType wrapping_language, bool const_object) const
+{
+    if (m_wrap)
+    {
+        switch (wrapping_language) 
+        {
+        default:
+            return false;
+        case lldb::eLanguageTypeC:
+        case lldb::eLanguageTypeC_plus_plus:
+        case lldb::eLanguageTypeObjC:
+            break;
+        }
+        
+        StreamString wrap_stream;
+        
+        switch (wrapping_language) 
+        {
+        default:
+            break;
+        case lldb::eLanguageTypeC:
+            wrap_stream.Printf("%s                             \n"
+                               "typedef unsigned short unichar;\n"
+                               "void                           \n"
+                               "%s(void *$__lldb_arg)          \n"
+                               "{                              \n"
+                               "    %s;                        \n" 
+                               "}                              \n",
+                               m_prefix.c_str(),
+                               m_name.c_str(),
+                               m_body.c_str());
+            break;
+        case lldb::eLanguageTypeC_plus_plus:
+            wrap_stream.Printf("%s                                     \n"
+                               "typedef unsigned short unichar;        \n"
+                               "void                                   \n"
+                               "$__lldb_class::%s(void *$__lldb_arg) %s\n"
+                               "{                                      \n"
+                               "    %s;                                \n" 
+                               "}                                      \n",
+                               m_prefix.c_str(),
+                               m_name.c_str(),
+                               (const_object ? "const" : ""),
+                               m_body.c_str());
+            break;
+        case lldb::eLanguageTypeObjC:
+            wrap_stream.Printf("%s                                                      \n"
+                                "typedef unsigned short unichar;                        \n"
+                                "@interface $__lldb_objc_class ($__lldb_category)       \n"
+                                "-(void)%s:(void *)$__lldb_arg;                         \n"
+                                "@end                                                   \n"
+                                "@implementation $__lldb_objc_class ($__lldb_category)  \n"
+                                "-(void)%s:(void *)$__lldb_arg                          \n"
+                                "{                                                      \n"
+                                "    %s;                                                \n"
+                                "}                                                      \n"
+                                "@end                                                   \n",
+                                m_prefix.c_str(),
+                                m_name.c_str(),
+                                m_name.c_str(),
+                                m_body.c_str());
+            break;
+        }
+        
+        text = wrap_stream.GetString();
+    }
+    else
+    {
+        text.append(m_body);
+    }
+    
+    return true;
+}