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