blob: 7e1664889cc19097569bd6b9ba59bdb85628a23c [file] [log] [blame]
Sean Callanana48fe162010-08-11 03:57:18 +00001//===-- ClangPersistentVariables.cpp ----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ClangPersistentVariables.h"
Sean Callanan82b74c82010-08-12 01:56:52 +000011#include "lldb/Core/DataExtractor.h"
Sean Callanana48fe162010-08-11 03:57:18 +000012#include "lldb/Core/Log.h"
13#include "lldb/Core/StreamString.h"
Sean Callanan82b74c82010-08-12 01:56:52 +000014#include "lldb/Core/Value.h"
Sean Callanana48fe162010-08-11 03:57:18 +000015
16using namespace lldb_private;
Sean Callanan82b74c82010-08-12 01:56:52 +000017
18Error
19ClangPersistentVariable::Print (Stream &output_stream,
20 ExecutionContext &exe_ctx,
21 lldb::Format format,
22 bool show_types,
23 bool show_summary,
24 bool verbose)
25{
26 Error err;
27
28 Value val;
29
30 clang::ASTContext *ast_context = m_user_type.GetASTContext();
31
32 val.SetContext (Value::eContextTypeOpaqueClangQualType, m_user_type.GetOpaqueQualType ());
33 val.SetValueType (Value::eValueTypeHostAddress);
34 val.GetScalar() = (uint64_t)Data ();
35
36 val.ResolveValue (&exe_ctx, ast_context);
37
38 if (val.GetContextType () == Value::eContextTypeInvalid &&
39 val.GetValueType () == Value::eValueTypeScalar &&
40 format == lldb::eFormatDefault)
41 {
42 // The expression result is just a scalar with no special formatting
43 val.GetScalar ().GetValue (&output_stream, show_types);
44 output_stream.EOL ();
45 return err;
46 }
47
48 // The expression result is more complext and requires special handling
49 DataExtractor data;
50 Error expr_error = val.GetValueAsData (&exe_ctx, ast_context, data, 0);
51
52 if (!expr_error.Success ())
53 {
54 err.SetErrorToGenericError ();
55 err.SetErrorStringWithFormat ("Couldn't resolve result value: %s", expr_error.AsCString ());
56 return err;
57 }
58
59 if (format == lldb::eFormatDefault)
60 format = val.GetValueDefaultFormat ();
61
62 void *clang_type = val.GetValueOpaqueClangQualType ();
63
64 output_stream.Printf("%s = ", m_name.AsCString("<anonymous>"));
65
66 if (clang_type)
67 {
68 if (show_types)
69 output_stream.Printf("(%s) ", ClangASTType::GetClangTypeName (clang_type).GetCString());
70
71 ClangASTType::DumpValue (ast_context, // The ASTContext that the clang type belongs to
72 clang_type, // The opaque clang type we want to dump that value of
73 &exe_ctx, // The execution context for memory and variable access
74 &output_stream, // Stream to dump to
75 format, // Format to use when dumping
76 data, // A buffer containing the bytes for the clang type
77 0, // Byte offset within "data" where value is
78 data.GetByteSize (), // Size in bytes of the value we are dumping
79 0, // Bitfield bit size
80 0, // Bitfield bit offset
81 show_types, // Show types?
82 show_summary, // Show summary?
83 verbose, // Debug logging output?
84 UINT32_MAX); // Depth to dump in case this is an aggregate type
85 }
86 else
87 {
88 data.Dump (&output_stream, // Stream to dump to
89 0, // Byte offset within "data"
90 format, // Format to use when dumping
91 data.GetByteSize (), // Size in bytes of each item we are dumping
92 1, // Number of items to dump
93 UINT32_MAX, // Number of items per line
94 LLDB_INVALID_ADDRESS, // Invalid address, don't show any offset/address context
95 0, // Bitfield bit size
96 0); // Bitfield bit offset
97 }
98
99 output_stream.EOL();
100
101 return err;
102}
Sean Callanana48fe162010-08-11 03:57:18 +0000103
104ClangPersistentVariables::ClangPersistentVariables () :
105 m_variables(),
106 m_result_counter(0)
107{
108}
109
110ClangPersistentVariable *
111ClangPersistentVariables::CreateVariable (ConstString name,
112 TypeFromUser user_type)
113{
Sean Callanan82b74c82010-08-12 01:56:52 +0000114 ClangPersistentVariable new_var(name, user_type);
Sean Callanana48fe162010-08-11 03:57:18 +0000115
116 if (m_variables.find(name) != m_variables.end())
117 return NULL;
118
119 m_variables[name] = new_var;
120
121 return &m_variables[name];
122}
123
124ClangPersistentVariable *
Sean Callanana48fe162010-08-11 03:57:18 +0000125ClangPersistentVariables::GetVariable (ConstString name)
126{
127 if (m_variables.find(name) == m_variables.end())
128 return NULL;
129
130 return &m_variables[name];
131}
Sean Callanan82b74c82010-08-12 01:56:52 +0000132
133void
134ClangPersistentVariables::GetNextResultName (std::string &name)
135{
136 StreamString s;
137 s.Printf("$%llu", m_result_counter);
138
139 m_result_counter++;
140
141 name = s.GetString();
142}