Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | b04e7a8 | 2010-08-24 21:05:24 +0000 | [diff] [blame] | 17 | #include "lldb/Target/RegisterContext.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Target/StackFrame.h" |
| 19 | #include "lldb/Target/Thread.h" |
| 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | //---------------------------------------------------------------------- |
| 25 | // Variable constructor |
| 26 | //---------------------------------------------------------------------- |
| 27 | Variable::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 Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 40 | m_owner_scope(context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | m_declaration(decl_ptr), |
| 42 | m_location(location), |
| 43 | m_external(external), |
| 44 | m_artificial(artificial) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | //---------------------------------------------------------------------- |
| 49 | // Destructor |
| 50 | //---------------------------------------------------------------------- |
| 51 | Variable::~Variable() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void |
| 57 | Variable::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 Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 85 | if (show_context && m_owner_scope != NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | { |
| 87 | s->PutCString(", context = ( "); |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 88 | m_owner_scope->DumpSymbolContext(s); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 89 | 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 | |
| 110 | size_t |
| 111 | Variable::MemorySize() const |
| 112 | { |
| 113 | return sizeof(Variable); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void |
| 118 | Variable::CalculateSymbolContext (SymbolContext *sc) |
| 119 | { |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 120 | if (m_owner_scope) |
| 121 | m_owner_scope->CalculateSymbolContext(sc); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | else |
| 123 | sc->Clear(); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | bool |
| 128 | Variable::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 Clayton | b04e7a8 | 2010-08-24 21:05:24 +0000 | [diff] [blame] | 146 | return m_location.LocationListContainsLoadAddress (&frame->GetThread().GetProcess(), frame->GetRegisterContext()->GetPC()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | } |
| 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 Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 158 | return variable_sc.block->FindBlockByID(frame_block->GetID()) != NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | break; |
| 162 | |
| 163 | default: |
| 164 | break; |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |