blob: a5c74e60f1ac8985849554f7bc4e899fed58c2ab [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Variable.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/Symbol/Variable.h"
11
12#include "lldb/Core/Stream.h"
13#include "lldb/Symbol/Block.h"
14#include "lldb/Symbol/Function.h"
15#include "lldb/Symbol/SymbolContext.h"
16#include "lldb/Symbol/Type.h"
Greg Claytonb04e7a82010-08-24 21:05:24 +000017#include "lldb/Target/RegisterContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Target/StackFrame.h"
19#include "lldb/Target/Thread.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24//----------------------------------------------------------------------
25// Variable constructor
26//----------------------------------------------------------------------
27Variable::Variable(lldb::user_id_t uid,
28 const ConstString& name,
29 Type *type,
30 ValueType scope,
31 SymbolContextScope *context,
32 Declaration* decl_ptr,
33 const DWARFExpression& location,
34 bool external,
35 bool artificial) :
36 UserID(uid),
37 m_name(name),
38 m_type(type),
39 m_scope(scope),
Greg Clayton4fb08152010-08-30 18:11:35 +000040 m_owner_scope(context),
Chris Lattner24943d22010-06-08 16:52:24 +000041 m_declaration(decl_ptr),
42 m_location(location),
43 m_external(external),
44 m_artificial(artificial)
45{
46}
47
48//----------------------------------------------------------------------
49// Destructor
50//----------------------------------------------------------------------
51Variable::~Variable()
52{
53}
54
55
56void
57Variable::Dump(Stream *s, bool show_context) const
58{
59 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
60 s->Indent();
61 *s << "Variable" << (const UserID&)*this;
62
63 if (m_name)
64 *s << ", name = \"" << m_name << "\"";
65
66 if (m_type != NULL)
67 {
68 *s << ", type = " << (void*)m_type << " (";
69 m_type->DumpTypeName(s);
70 s->PutChar(')');
71 }
72
73 if (m_scope != eValueTypeInvalid)
74 {
75 s->PutCString(", scope = ");
76 switch (m_scope)
77 {
78 case eValueTypeVariableGlobal: s->PutCString(m_external ? "global" : "static"); break;
79 case eValueTypeVariableArgument: s->PutCString("parameter"); break;
80 case eValueTypeVariableLocal: s->PutCString("local"); break;
81 default: *s << "??? (" << m_scope << ')';
82 }
83 }
84
Greg Clayton4fb08152010-08-30 18:11:35 +000085 if (show_context && m_owner_scope != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +000086 {
87 s->PutCString(", context = ( ");
Greg Clayton4fb08152010-08-30 18:11:35 +000088 m_owner_scope->DumpSymbolContext(s);
Chris Lattner24943d22010-06-08 16:52:24 +000089 s->PutCString(" )");
90 }
91
92 m_declaration.Dump(s);
93
94 if (m_location.IsValid())
95 {
96 s->PutCString(", location = ");
97 m_location.GetDescription(s, lldb::eDescriptionLevelBrief);
98 }
99
100 if (m_external)
101 s->PutCString(", external");
102
103 if (m_artificial)
104 s->PutCString(", artificial");
105
106 s->EOL();
107}
108
109
110size_t
111Variable::MemorySize() const
112{
113 return sizeof(Variable);
114}
115
116
117void
118Variable::CalculateSymbolContext (SymbolContext *sc)
119{
Greg Clayton4fb08152010-08-30 18:11:35 +0000120 if (m_owner_scope)
121 m_owner_scope->CalculateSymbolContext(sc);
Chris Lattner24943d22010-06-08 16:52:24 +0000122 else
123 sc->Clear();
124}
125
126
127bool
128Variable::IsInScope (StackFrame *frame)
129{
130 switch (m_scope)
131 {
132 case eValueTypeVariableGlobal:
133 // Globals and statics are always in scope.
134 return true;
135
136 case eValueTypeVariableArgument:
137 case eValueTypeVariableLocal:
138 // Check if the location has a location list that describes the value
139 // of the variable with address ranges and different locations for each
140 // address range?
141 if (m_location.IsLocationList())
142 {
143 // It is a location list. We just need to tell if the location
144 // list contains the current address when converted to a load
145 // address
Greg Claytonb04e7a82010-08-24 21:05:24 +0000146 return m_location.LocationListContainsLoadAddress (&frame->GetThread().GetProcess(), frame->GetRegisterContext()->GetPC());
Chris Lattner24943d22010-06-08 16:52:24 +0000147 }
148 else
149 {
150 // We don't have a location list, we just need to see if the block
151 // that this variable was defined in is currently
152 Block *frame_block = frame->GetSymbolContext(eSymbolContextBlock).block;
153 if (frame_block)
154 {
155 SymbolContext variable_sc;
156 CalculateSymbolContext (&variable_sc);
157 if (variable_sc.function && variable_sc.block)
Greg Clayton75ccf502010-08-21 02:22:51 +0000158 return variable_sc.block->FindBlockByID(frame_block->GetID()) != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000159 }
160 }
161 break;
162
163 default:
164 break;
165 }
166 return false;
167}
168