blob: f406583dd726c4c61745f098cb9c432c972c17c9 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ClangExpressionVariable.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 "lldb/Expression/ClangExpressionVariable.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "clang/AST/ASTContext.h"
Sean Callanana6223432010-08-20 01:02:30 +000017#include "lldb/Core/ConstString.h"
18#include "lldb/Core/DataExtractor.h"
19#include "lldb/Core/Stream.h"
20#include "lldb/Core/Value.h"
Sean Callanane8a59a82010-09-13 21:34:21 +000021#include "lldb/Target/ExecutionContext.h"
22#include "lldb/Target/Process.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023
24using namespace lldb_private;
25using namespace clang;
26
Sean Callanana6223432010-08-20 01:02:30 +000027ClangExpressionVariable::ClangExpressionVariable()
Chris Lattner24943d22010-06-08 16:52:24 +000028{
Sean Callanana6223432010-08-20 01:02:30 +000029 m_name = "";
30 m_user_type = TypeFromUser(NULL, NULL);
31 m_parser_vars.reset(NULL);
32 m_jit_vars.reset(NULL);
33 m_data_vars.reset(NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000034}
35
Sean Callanana6223432010-08-20 01:02:30 +000036void ClangExpressionVariable::DisableDataVars()
Chris Lattner24943d22010-06-08 16:52:24 +000037{
Sean Callanana6223432010-08-20 01:02:30 +000038 if (m_data_vars.get() && m_data_vars->m_data)
39 delete m_data_vars->m_data;
40 m_data_vars.reset();
Chris Lattner24943d22010-06-08 16:52:24 +000041}
42
Sean Callanana6223432010-08-20 01:02:30 +000043Error
44ClangExpressionVariable::Print (Stream &output_stream,
45 ExecutionContext &exe_ctx,
46 lldb::Format format,
47 bool show_types,
48 bool show_summary,
49 bool verbose)
Chris Lattner24943d22010-06-08 16:52:24 +000050{
Sean Callanana6223432010-08-20 01:02:30 +000051 Error err;
Chris Lattner24943d22010-06-08 16:52:24 +000052
Jim Ingham324067b2010-09-30 00:54:27 +000053 Value val;
54 if (!PointValueAtData (val, &exe_ctx))
Chris Lattner24943d22010-06-08 16:52:24 +000055 {
Sean Callanana6223432010-08-20 01:02:30 +000056 err.SetErrorToGenericError();
57 err.SetErrorStringWithFormat("Variable doesn't contain a value");
58 return err;
Chris Lattner24943d22010-06-08 16:52:24 +000059 }
Chris Lattner24943d22010-06-08 16:52:24 +000060
Sean Callanana6223432010-08-20 01:02:30 +000061 if (val.GetContextType () == Value::eContextTypeInvalid &&
62 val.GetValueType () == Value::eValueTypeScalar &&
63 format == lldb::eFormatDefault)
64 {
65 // The expression result is just a scalar with no special formatting
66 val.GetScalar ().GetValue (&output_stream, show_types);
67 output_stream.EOL ();
68 return err;
69 }
70
Jim Ingham324067b2010-09-30 00:54:27 +000071 clang::ASTContext *ast_context = m_user_type.GetASTContext();
72
Sean Callanana6223432010-08-20 01:02:30 +000073 // The expression result is more complex and requires special handling
74 DataExtractor data;
75 Error expr_error = val.GetValueAsData (&exe_ctx, ast_context, data, 0);
76
Sean Callanane8a59a82010-09-13 21:34:21 +000077
78 // Set byte order and pointer size to TARGET byte order and pointer size!
79
80 data.SetByteOrder(exe_ctx.process->GetByteOrder());
81 data.SetAddressByteSize(exe_ctx.process->GetAddressByteSize());
82
Sean Callanana6223432010-08-20 01:02:30 +000083 if (!expr_error.Success ())
84 {
85 err.SetErrorToGenericError ();
86 err.SetErrorStringWithFormat ("Couldn't resolve variable value: %s", expr_error.AsCString ());
87 return err;
88 }
89
90 if (format == lldb::eFormatDefault)
91 format = val.GetValueDefaultFormat ();
92
Greg Clayton462d4142010-09-29 01:12:09 +000093 void *clang_type = val.GetClangType ();
Sean Callanana6223432010-08-20 01:02:30 +000094
95 output_stream.Printf("%s = ", m_name.c_str());
96
97 if (clang_type)
98 {
99 if (show_types)
100 output_stream.Printf("(%s) ", ClangASTType::GetClangTypeName (clang_type).GetCString());
101
102 ClangASTType::DumpValue (ast_context, // The ASTContext that the clang type belongs to
103 clang_type, // The opaque clang type we want to dump that value of
104 &exe_ctx, // The execution context for memory and variable access
105 &output_stream, // Stream to dump to
106 format, // Format to use when dumping
107 data, // A buffer containing the bytes for the clang type
108 0, // Byte offset within "data" where value is
109 data.GetByteSize (), // Size in bytes of the value we are dumping
110 0, // Bitfield bit size
111 0, // Bitfield bit offset
112 show_types, // Show types?
113 show_summary, // Show summary?
114 verbose, // Debug logging output?
115 UINT32_MAX); // Depth to dump in case this is an aggregate type
116 }
117 else
118 {
119 data.Dump (&output_stream, // Stream to dump to
120 0, // Byte offset within "data"
121 format, // Format to use when dumping
122 data.GetByteSize (), // Size in bytes of each item we are dumping
123 1, // Number of items to dump
124 UINT32_MAX, // Number of items per line
125 LLDB_INVALID_ADDRESS, // Invalid address, don't show any offset/address context
126 0, // Bitfield bit size
127 0); // Bitfield bit offset
128 }
129
130 output_stream.EOL();
131
132 return err;
Chris Lattner24943d22010-06-08 16:52:24 +0000133}
134
Sean Callanana6223432010-08-20 01:02:30 +0000135ClangExpressionVariable::ClangExpressionVariable(const ClangExpressionVariable &cev) :
136 m_name(cev.m_name),
Sean Callanan8194e422010-08-30 21:15:33 +0000137 m_user_type(cev.m_user_type),
138 m_store(cev.m_store),
139 m_index(cev.m_index)
Chris Lattner24943d22010-06-08 16:52:24 +0000140{
Sean Callanana6223432010-08-20 01:02:30 +0000141 if (cev.m_parser_vars.get())
142 {
143 m_parser_vars.reset(new struct ParserVars);
144 *m_parser_vars.get() = *cev.m_parser_vars.get();
145 }
Chris Lattner24943d22010-06-08 16:52:24 +0000146
Sean Callanana6223432010-08-20 01:02:30 +0000147 if (cev.m_jit_vars.get())
148 {
149 m_jit_vars.reset(new struct JITVars);
150 *m_jit_vars.get() = *cev.m_jit_vars.get();
151 }
152
153 if (cev.m_data_vars.get())
154 {
155 m_data_vars.reset(new struct DataVars);
156 *m_data_vars.get() = *cev.m_data_vars.get();
157 }
158}
159
160bool
Jim Ingham324067b2010-09-30 00:54:27 +0000161ClangExpressionVariable::PointValueAtData(Value &value, ExecutionContext *exe_ctx)
Sean Callanana6223432010-08-20 01:02:30 +0000162{
Jim Ingham324067b2010-09-30 00:54:27 +0000163 if (!m_data_vars.get() || !m_data_vars->m_data)
Sean Callanana6223432010-08-20 01:02:30 +0000164 return false;
165
166 value.SetContext(Value::eContextTypeOpaqueClangQualType, m_user_type.GetOpaqueQualType());
167 value.SetValueType(Value::eValueTypeHostAddress);
168 value.GetScalar() = (uint64_t)m_data_vars->m_data->GetBytes();
Jim Ingham324067b2010-09-30 00:54:27 +0000169 clang::ASTContext *ast_context = m_user_type.GetASTContext();
170
171 if (exe_ctx)
172 value.ResolveValue (exe_ctx, ast_context);
Sean Callanana6223432010-08-20 01:02:30 +0000173
174 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000175}