1 //===-- VariableList.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/VariableList.h"
11 
12 #include "lldb/Core/RegularExpression.h"
13 #include "lldb/Symbol/Block.h"
14 #include "lldb/Symbol/Function.h"
15 #include "lldb/Symbol/CompileUnit.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 //----------------------------------------------------------------------
21 // VariableList constructor
22 //----------------------------------------------------------------------
VariableList()23 VariableList::VariableList() :
24     m_variables()
25 {
26 }
27 
28 //----------------------------------------------------------------------
29 // Destructor
30 //----------------------------------------------------------------------
~VariableList()31 VariableList::~VariableList()
32 {
33 }
34 
35 void
AddVariable(const VariableSP & var_sp)36 VariableList::AddVariable(const VariableSP &var_sp)
37 {
38     m_variables.push_back(var_sp);
39 }
40 
41 bool
AddVariableIfUnique(const lldb::VariableSP & var_sp)42 VariableList::AddVariableIfUnique (const lldb::VariableSP &var_sp)
43 {
44     if (FindVariableIndex (var_sp) == UINT32_MAX)
45     {
46         m_variables.push_back(var_sp);
47         return true;
48     }
49     return false;
50 }
51 
52 void
AddVariables(VariableList * variable_list)53 VariableList::AddVariables(VariableList *variable_list)
54 {
55     if (variable_list)
56     {
57         std::copy(variable_list->m_variables.begin(), // source begin
58                   variable_list->m_variables.end(),   // source end
59                   back_inserter(m_variables));        // destination
60     }
61 }
62 
63 void
Clear()64 VariableList::Clear()
65 {
66     m_variables.clear();
67 }
68 
69 VariableSP
GetVariableAtIndex(size_t idx) const70 VariableList::GetVariableAtIndex(size_t idx) const
71 {
72     VariableSP var_sp;
73     if (idx < m_variables.size())
74         var_sp = m_variables[idx];
75     return var_sp;
76 }
77 
78 VariableSP
RemoveVariableAtIndex(size_t idx)79 VariableList::RemoveVariableAtIndex(size_t idx)
80 {
81     VariableSP var_sp;
82     if (idx < m_variables.size())
83     {
84         var_sp = m_variables[idx];
85         m_variables.erase (m_variables.begin() + idx);
86     }
87     return var_sp;
88 }
89 
90 uint32_t
FindVariableIndex(const VariableSP & var_sp)91 VariableList::FindVariableIndex (const VariableSP &var_sp)
92 {
93     iterator pos, end = m_variables.end();
94     for (pos = m_variables.begin(); pos != end; ++pos)
95     {
96         if (pos->get() == var_sp.get())
97             return std::distance (m_variables.begin(), pos);
98     }
99     return UINT32_MAX;
100 }
101 
102 VariableSP
FindVariable(const ConstString & name)103 VariableList::FindVariable(const ConstString& name)
104 {
105     VariableSP var_sp;
106     iterator pos, end = m_variables.end();
107     for (pos = m_variables.begin(); pos != end; ++pos)
108     {
109         if ((*pos)->NameMatches(name))
110         {
111             var_sp = (*pos);
112             break;
113         }
114     }
115     return var_sp;
116 }
117 
118 size_t
AppendVariablesIfUnique(const RegularExpression & regex,VariableList & var_list,size_t & total_matches)119 VariableList::AppendVariablesIfUnique (const RegularExpression& regex, VariableList &var_list, size_t& total_matches)
120 {
121     const size_t initial_size = var_list.GetSize();
122     iterator pos, end = m_variables.end();
123     for (pos = m_variables.begin(); pos != end; ++pos)
124     {
125         if ((*pos)->NameMatches (regex))
126         {
127             // Note the total matches found
128             total_matches++;
129             // Only add this variable if it isn't already in the "var_list"
130             var_list.AddVariableIfUnique (*pos);
131         }
132     }
133     // Return the number of new unique variables added to "var_list"
134     return var_list.GetSize() - initial_size;
135 }
136 
137 size_t
AppendVariablesWithScope(lldb::ValueType type,VariableList & var_list,bool if_unique)138 VariableList::AppendVariablesWithScope (lldb::ValueType type,
139                                         VariableList &var_list,
140                                         bool if_unique)
141 {
142     const size_t initial_size = var_list.GetSize();
143     iterator pos, end = m_variables.end();
144     for (pos = m_variables.begin(); pos != end; ++pos)
145     {
146         if ((*pos)->GetScope() == type)
147         {
148             if (if_unique)
149                 var_list.AddVariableIfUnique (*pos);
150             else
151                 var_list.AddVariable(*pos);
152         }
153     }
154     // Return the number of new unique variables added to "var_list"
155     return var_list.GetSize() - initial_size;
156 }
157 
158 uint32_t
FindIndexForVariable(Variable * variable)159 VariableList::FindIndexForVariable (Variable* variable)
160 {
161     VariableSP var_sp;
162     iterator pos;
163     const iterator begin = m_variables.begin();
164     const iterator end = m_variables.end();
165     for (pos = m_variables.begin(); pos != end; ++pos)
166     {
167         if ((*pos).get() == variable)
168             return std::distance (begin, pos);
169     }
170     return UINT32_MAX;
171 }
172 
173 size_t
MemorySize() const174 VariableList::MemorySize() const
175 {
176     size_t mem_size = sizeof(VariableList);
177     const_iterator pos, end = m_variables.end();
178     for (pos = m_variables.begin(); pos != end; ++pos)
179         mem_size += (*pos)->MemorySize();
180     return mem_size;
181 }
182 
183 size_t
GetSize() const184 VariableList::GetSize() const
185 {
186     return m_variables.size();
187 }
188 
189 void
Dump(Stream * s,bool show_context) const190 VariableList::Dump(Stream *s, bool show_context) const
191 {
192 //  s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
193 //  s.Indent();
194 //  s << "VariableList\n";
195 
196     const_iterator pos, end = m_variables.end();
197     for (pos = m_variables.begin(); pos != end; ++pos)
198     {
199         (*pos)->Dump(s, show_context);
200     }
201 }
202