Implemented a major overhaul of the way variables are handled
by LLDB.  Instead of being materialized into the input structure
passed to the expression, variables are left in place and pointers
to them are materialzied into the structure.  Variables not resident
in memory (notably, registers) get temporary memory regions allocated
for them.

Persistent variables are the most complex part of this, because they
are made in various ways and there are different expectations about
their lifetime.  Persistent variables now have flags indicating their
status and what the expectations for longevity are.  They can be
marked as residing in target memory permanently -- this is the
default for result variables from expressions entered on the command
line and for explicitly declared persistent variables (but more on
that below).  Other result variables have their memory freed.

Some major improvements resulting from this include being able to
properly take the address of variables, better and cleaner support
for functions that return references, and cleaner C++ support in
general.  One problem that remains is the problem of explicitly
declared persistent variables; I have not yet implemented the code
that makes references to them into indirect references, so currently
materialization and dematerialization of these variables is broken.


git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@123371 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Expression/IRForTarget.cpp b/source/Expression/IRForTarget.cpp
index ab3afeb..27a0f4a 100644
--- a/source/Expression/IRForTarget.cpp
+++ b/source/Expression/IRForTarget.cpp
@@ -43,7 +43,8 @@
     m_func_name(func_name),
     m_resolve_vars(resolve_vars),
     m_const_result(const_result),
-    m_has_side_effects(NULL)
+    m_has_side_effects(false),
+    m_result_is_pointer(false)
 {
 }
 
@@ -153,10 +154,19 @@
          vi != ve;
          ++vi)
     {
+        if (strstr(vi->first(), "$__lldb_expr_result_ptr") &&
+            !strstr(vi->first(), "GV"))
+        {
+            result_name = vi->first();
+            m_result_is_pointer = true;
+            break;
+        }
+        
         if (strstr(vi->first(), "$__lldb_expr_result") &&
             !strstr(vi->first(), "GV")) 
         {
             result_name = vi->first();
+            m_result_is_pointer = false;
             break;
         }
     }
@@ -178,7 +188,7 @@
     {
         if (log)
             log->PutCString("Result variable had no data");
-                
+        
         return false;
     }
         
@@ -240,14 +250,43 @@
     // Get the next available result name from m_decl_map and create the persistent
     // variable for it
     
-    lldb_private::TypeFromParser result_decl_type (result_decl->getType().getAsOpaquePtr(),
-                                                   &result_decl->getASTContext());
-
-    lldb_private::ConstString new_result_name (m_decl_map->GetPersistentResultName());
-    m_decl_map->AddPersistentVariable(result_decl, new_result_name, result_decl_type);
+    lldb_private::TypeFromParser result_decl_type;
+    
+    if (m_result_is_pointer)
+    {
+        clang::QualType pointer_qual_type = result_decl->getType();
+        clang::Type *pointer_type = pointer_qual_type.getTypePtr();
+        clang::PointerType *pointer_pointertype = dyn_cast<clang::PointerType>(pointer_type);
+        
+        if (!pointer_pointertype)
+        {
+            if (log)
+                log->PutCString("Expected result to have pointer type, but it did not");
+            return false;
+        }
+        
+        clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
+        
+        result_decl_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
+                                                        &result_decl->getASTContext());
+    }
+    else
+    {
+        result_decl_type = lldb_private::TypeFromParser(result_decl->getType().getAsOpaquePtr(),
+                                                        &result_decl->getASTContext());
+    }
+    
+    m_result_name = m_decl_map->GetPersistentResultName();
+    // If the result is an Lvalue, it is emitted as a pointer; see
+    // ASTResultSynthesizer::SynthesizeBodyResult.
+    m_decl_map->AddPersistentVariable(result_decl, 
+                                      m_result_name, 
+                                      result_decl_type,
+                                      true,
+                                      m_result_is_pointer);
     
     if (log)
-        log->Printf("Creating a new result global: \"%s\"", new_result_name.GetCString());
+        log->Printf("Creating a new result global: \"%s\"", m_result_name.GetCString());
         
     // Construct a new result global and set up its metadata
     
@@ -256,7 +295,7 @@
                                                            false, /* not constant */
                                                            GlobalValue::ExternalLinkage,
                                                            NULL, /* no initializer */
-                                                           new_result_name.GetCString ());
+                                                           m_result_name.GetCString ());
     
     // It's too late in compilation to create a new VarDecl for this, but we don't
     // need to.  We point the metadata at the old VarDecl.  This creates an odd
@@ -307,7 +346,7 @@
         if (!m_has_side_effects)
         {
             MaybeSetConstantResult (initializer, 
-                                    new_result_name, 
+                                    m_result_name, 
                                     result_decl_type);
         }
                 
@@ -800,7 +839,7 @@
     
     StringRef decl_name (decl->getName());
     lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
-    if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type))
+    if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
         return false;
     
     GlobalVariable *persistent_global = new GlobalVariable(llvm_module, 
@@ -964,9 +1003,27 @@
             return false;
         }
         
-        clang::QualType qual_type(clang::QualType::getFromOpaquePtr(opaque_type));
+        clang::QualType qual_type;
+        const Type *value_type;
+        
+        if (!name.compare("$__lldb_expr_result"))
+        {
+            // The $__lldb_expr_result name indicates the the return value has allocated as
+            // a static variable.  Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
+            // accesses to this static variable need to be redirected to the result of dereferencing
+            // a pointer that is passed in as one of the arguments.
+            //
+            // Consequently, when reporting the size of the type, we report a pointer type pointing
+            // to the type of $__lldb_expr_result, not the type itself.
             
-        const Type *value_type = global_variable->getType();
+            qual_type = ast_context->getPointerType(clang::QualType::getFromOpaquePtr(opaque_type));
+            value_type = PointerType::get(global_variable->getType(), 0);
+        }
+        else
+        {
+            qual_type = clang::QualType::getFromOpaquePtr(opaque_type);
+            value_type = global_variable->getType();
+        }
                 
         size_t value_size = (ast_context->getTypeSize(qual_type) + 7) / 8;
         off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7) / 8;
@@ -1479,12 +1536,31 @@
         
         ConstantInt *offset_int(ConstantInt::getSigned(offset_type, offset));
         GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", FirstEntryInstruction);
-        BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", FirstEntryInstruction);
+                
+        Value *replacement;
         
-        if (Constant *constant = dyn_cast<Constant>(value))
-            UnfoldConstant(constant, bit_cast, FirstEntryInstruction);
+        // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
+        // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
+        // entry in order to produce the static variable that the AST thinks it is accessing.
+        if (name == m_result_name && !m_result_is_pointer)
+        {
+            BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType()->getPointerTo(), "", FirstEntryInstruction);
+        
+            LoadInst *load = new LoadInst(bit_cast, "", FirstEntryInstruction);
+            
+            replacement = load;
+        }
         else
-            value->replaceAllUsesWith(bit_cast);
+        {
+            BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", FirstEntryInstruction);
+
+            replacement = bit_cast;
+        }
+            
+        if (Constant *constant = dyn_cast<Constant>(value))
+            UnfoldConstant(constant, replacement, FirstEntryInstruction);
+        else
+            value->replaceAllUsesWith(replacement);
         
         if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
             var->eraseFromParent();
@@ -1507,7 +1583,7 @@
     {
         if (log)
             log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
-        
+
         return false;
     }