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 //----------------------------------------------------------------------
23 VariableList::VariableList() :
24     m_variables()
25 {
26 }
27 
28 //----------------------------------------------------------------------
29 // Destructor
30 //----------------------------------------------------------------------
31 VariableList::~VariableList()
32 {
33 }
34 
35 void
36 VariableList::AddVariable(const VariableSP &var_sp)
37 {
38     m_variables.push_back(var_sp);
39 }
40 
41 bool
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
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
64 VariableList::Clear()
65 {
66     m_variables.clear();
67 }
68 
69 VariableSP
70 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
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
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
103 VariableList::FindVariable(const ConstString& name, bool include_static_members)
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             if (include_static_members || !(*pos)->IsStaticMember())
112             {
113                 var_sp = (*pos);
114                 break;
115             }
116         }
117     }
118     return var_sp;
119 }
120 
121 VariableSP
122 VariableList::FindVariable (const ConstString& name, lldb::ValueType value_type, bool include_static_members)
123 {
124     VariableSP var_sp;
125     iterator pos, end = m_variables.end();
126     for (pos = m_variables.begin(); pos != end; ++pos)
127     {
128         if ((*pos)->NameMatches(name) && (*pos)->GetScope() == value_type)
129         {
130             if (include_static_members || !(*pos)->IsStaticMember())
131             {
132                 var_sp = (*pos);
133                 break;
134             }
135         }
136     }
137     return var_sp;
138 }
139 
140 size_t
141 VariableList::AppendVariablesIfUnique(VariableList &var_list)
142 {
143     const size_t initial_size = var_list.GetSize();
144     iterator pos, end = m_variables.end();
145     for (pos = m_variables.begin(); pos != end; ++pos)
146         var_list.AddVariableIfUnique(*pos);
147     return var_list.GetSize() - initial_size;
148 }
149 
150 size_t
151 VariableList::AppendVariablesIfUnique (const RegularExpression& regex, VariableList &var_list, size_t& total_matches)
152 {
153     const size_t initial_size = var_list.GetSize();
154     iterator pos, end = m_variables.end();
155     for (pos = m_variables.begin(); pos != end; ++pos)
156     {
157         if ((*pos)->NameMatches (regex))
158         {
159             // Note the total matches found
160             total_matches++;
161             // Only add this variable if it isn't already in the "var_list"
162             var_list.AddVariableIfUnique (*pos);
163         }
164     }
165     // Return the number of new unique variables added to "var_list"
166     return var_list.GetSize() - initial_size;
167 }
168 
169 size_t
170 VariableList::AppendVariablesWithScope (lldb::ValueType type,
171                                         VariableList &var_list,
172                                         bool if_unique)
173 {
174     const size_t initial_size = var_list.GetSize();
175     iterator pos, end = m_variables.end();
176     for (pos = m_variables.begin(); pos != end; ++pos)
177     {
178         if ((*pos)->GetScope() == type)
179         {
180             if (if_unique)
181                 var_list.AddVariableIfUnique (*pos);
182             else
183                 var_list.AddVariable(*pos);
184         }
185     }
186     // Return the number of new unique variables added to "var_list"
187     return var_list.GetSize() - initial_size;
188 }
189 
190 uint32_t
191 VariableList::FindIndexForVariable (Variable* variable)
192 {
193     VariableSP var_sp;
194     iterator pos;
195     const iterator begin = m_variables.begin();
196     const iterator end = m_variables.end();
197     for (pos = m_variables.begin(); pos != end; ++pos)
198     {
199         if ((*pos).get() == variable)
200             return std::distance (begin, pos);
201     }
202     return UINT32_MAX;
203 }
204 
205 size_t
206 VariableList::MemorySize() const
207 {
208     size_t mem_size = sizeof(VariableList);
209     const_iterator pos, end = m_variables.end();
210     for (pos = m_variables.begin(); pos != end; ++pos)
211         mem_size += (*pos)->MemorySize();
212     return mem_size;
213 }
214 
215 size_t
216 VariableList::GetSize() const
217 {
218     return m_variables.size();
219 }
220 
221 void
222 VariableList::Dump(Stream *s, bool show_context) const
223 {
224 //  s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
225 //  s.Indent();
226 //  s << "VariableList\n";
227 
228     const_iterator pos, end = m_variables.end();
229     for (pos = m_variables.begin(); pos != end; ++pos)
230     {
231         (*pos)->Dump(s, show_context);
232     }
233 }
234