VariableList.cpp revision 256281
1168404Spjd//===-- VariableList.cpp ----------------------------------------*- C++ -*-===//
2168404Spjd//
3168404Spjd//                     The LLVM Compiler Infrastructure
4168404Spjd//
5168404Spjd// This file is distributed under the University of Illinois Open Source
6168404Spjd// License. See LICENSE.TXT for details.
7168404Spjd//
8168404Spjd//===----------------------------------------------------------------------===//
9168404Spjd
10168404Spjd#include "lldb/Symbol/VariableList.h"
11168404Spjd
12168404Spjd#include "lldb/Core/RegularExpression.h"
13168404Spjd#include "lldb/Symbol/Block.h"
14168404Spjd#include "lldb/Symbol/Function.h"
15168404Spjd#include "lldb/Symbol/CompileUnit.h"
16168404Spjd
17168404Spjdusing namespace lldb;
18168404Spjdusing namespace lldb_private;
19168404Spjd
20168404Spjd//----------------------------------------------------------------------
21168404Spjd// VariableList constructor
22209962Smm//----------------------------------------------------------------------
23168404SpjdVariableList::VariableList() :
24168404Spjd    m_variables()
25168404Spjd{
26169195Spjd}
27169195Spjd
28168404Spjd//----------------------------------------------------------------------
29168404Spjd// Destructor
30168404Spjd//----------------------------------------------------------------------
31168404SpjdVariableList::~VariableList()
32168404Spjd{
33168404Spjd}
34168404Spjd
35168404Spjdvoid
36185029SpjdVariableList::AddVariable(const VariableSP &var_sp)
37185029Spjd{
38168404Spjd    m_variables.push_back(var_sp);
39168404Spjd}
40168404Spjd
41168404Spjdbool
42168404SpjdVariableList::AddVariableIfUnique (const lldb::VariableSP &var_sp)
43168404Spjd{
44168404Spjd    if (FindVariableIndex (var_sp) == UINT32_MAX)
45168404Spjd    {
46168404Spjd        m_variables.push_back(var_sp);
47168404Spjd        return true;
48168404Spjd    }
49185029Spjd    return false;
50168404Spjd}
51185029Spjd
52168404Spjdvoid
53168404SpjdVariableList::AddVariables(VariableList *variable_list)
54168404Spjd{
55168404Spjd    if (variable_list)
56168404Spjd    {
57168404Spjd        std::copy(variable_list->m_variables.begin(), // source begin
58168404Spjd                  variable_list->m_variables.end(),   // source end
59168404Spjd                  back_inserter(m_variables));        // destination
60168404Spjd    }
61185029Spjd}
62185029Spjd
63173268Slulfvoid
64173268SlulfVariableList::Clear()
65173268Slulf{
66173268Slulf    m_variables.clear();
67168404Spjd}
68185029Spjd
69185029SpjdVariableSP
70185029SpjdVariableList::GetVariableAtIndex(size_t idx) const
71185029Spjd{
72185029Spjd    VariableSP var_sp;
73185029Spjd    if (idx < m_variables.size())
74185029Spjd        var_sp = m_variables[idx];
75185029Spjd    return var_sp;
76185029Spjd}
77185029Spjd
78185029SpjdVariableSP
79185029SpjdVariableList::RemoveVariableAtIndex(size_t idx)
80185029Spjd{
81185029Spjd    VariableSP var_sp;
82185029Spjd    if (idx < m_variables.size())
83185029Spjd    {
84185029Spjd        var_sp = m_variables[idx];
85168404Spjd        m_variables.erase (m_variables.begin() + idx);
86168404Spjd    }
87168404Spjd    return var_sp;
88168404Spjd}
89168404Spjd
90185029Spjduint32_t
91168404SpjdVariableList::FindVariableIndex (const VariableSP &var_sp)
92168404Spjd{
93168404Spjd    iterator pos, end = m_variables.end();
94185029Spjd    for (pos = m_variables.begin(); pos != end; ++pos)
95168404Spjd    {
96185029Spjd        if (pos->get() == var_sp.get())
97185029Spjd            return std::distance (m_variables.begin(), pos);
98185029Spjd    }
99185029Spjd    return UINT32_MAX;
100185029Spjd}
101185029Spjd
102185029SpjdVariableSP
103168404SpjdVariableList::FindVariable(const ConstString& name)
104168488Spjd{
105168404Spjd    VariableSP var_sp;
106168404Spjd    iterator pos, end = m_variables.end();
107185029Spjd    for (pos = m_variables.begin(); pos != end; ++pos)
108168488Spjd    {
109168404Spjd        if ((*pos)->NameMatches(name))
110168404Spjd        {
111168404Spjd            var_sp = (*pos);
112168404Spjd            break;
113197153Spjd        }
114168488Spjd    }
115168404Spjd    return var_sp;
116185029Spjd}
117168404Spjd
118175294Sattiliosize_t
119168404SpjdVariableList::AppendVariablesIfUnique (const RegularExpression& regex, VariableList &var_list, size_t& total_matches)
120168404Spjd{
121168404Spjd    const size_t initial_size = var_list.GetSize();
122168404Spjd    iterator pos, end = m_variables.end();
123168404Spjd    for (pos = m_variables.begin(); pos != end; ++pos)
124185029Spjd    {
125168404Spjd        if ((*pos)->NameMatches (regex))
126168404Spjd        {
127168404Spjd            // Note the total matches found
128168404Spjd            total_matches++;
129209962Smm            // Only add this variable if it isn't already in the "var_list"
130168404Spjd            var_list.AddVariableIfUnique (*pos);
131168404Spjd        }
132168404Spjd    }
133168404Spjd    // Return the number of new unique variables added to "var_list"
134168404Spjd    return var_list.GetSize() - initial_size;
135168404Spjd}
136168404Spjd
137168404Spjdsize_t
138185029SpjdVariableList::AppendVariablesWithScope (lldb::ValueType type,
139168404Spjd                                        VariableList &var_list,
140168404Spjd                                        bool if_unique)
141169196Spjd{
142185029Spjd    const size_t initial_size = var_list.GetSize();
143168404Spjd    iterator pos, end = m_variables.end();
144168404Spjd    for (pos = m_variables.begin(); pos != end; ++pos)
145185029Spjd    {
146185029Spjd        if ((*pos)->GetScope() == type)
147185029Spjd        {
148199156Spjd            if (if_unique)
149199156Spjd                var_list.AddVariableIfUnique (*pos);
150199156Spjd            else
151199156Spjd                var_list.AddVariable(*pos);
152199156Spjd        }
153199156Spjd    }
154199156Spjd    // Return the number of new unique variables added to "var_list"
155199156Spjd    return var_list.GetSize() - initial_size;
156199156Spjd}
157199156Spjd
158199156Spjduint32_t
159199156SpjdVariableList::FindIndexForVariable (Variable* variable)
160185029Spjd{
161185029Spjd    VariableSP var_sp;
162185029Spjd    iterator pos;
163168404Spjd    const iterator begin = m_variables.begin();
164168404Spjd    const iterator end = m_variables.end();
165168404Spjd    for (pos = m_variables.begin(); pos != end; ++pos)
166168404Spjd    {
167168404Spjd        if ((*pos).get() == variable)
168168404Spjd            return std::distance (begin, pos);
169168404Spjd    }
170168404Spjd    return UINT32_MAX;
171168404Spjd}
172185029Spjd
173185029Spjdsize_t
174168404SpjdVariableList::MemorySize() const
175168404Spjd{
176168404Spjd    size_t mem_size = sizeof(VariableList);
177168404Spjd    const_iterator pos, end = m_variables.end();
178168404Spjd    for (pos = m_variables.begin(); pos != end; ++pos)
179185029Spjd        mem_size += (*pos)->MemorySize();
180168404Spjd    return mem_size;
181168404Spjd}
182168404Spjd
183185029Spjdsize_t
184185029SpjdVariableList::GetSize() const
185185029Spjd{
186185029Spjd    return m_variables.size();
187168404Spjd}
188168404Spjd
189168404Spjdvoid
190168404SpjdVariableList::Dump(Stream *s, bool show_context) const
191185029Spjd{
192168404Spjd//  s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
193168404Spjd//  s.Indent();
194185029Spjd//  s << "VariableList\n";
195185029Spjd
196168404Spjd    const_iterator pos, end = m_variables.end();
197168404Spjd    for (pos = m_variables.begin(); pos != end; ++pos)
198185029Spjd    {
199185029Spjd        (*pos)->Dump(s, show_context);
200185029Spjd    }
201185029Spjd}
202185029Spjd