1//===-- Symtab.h ------------------------------------------------*- 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
11#ifndef liblldb_Symtab_h_
12#define liblldb_Symtab_h_
13
14#include <vector>
15
16#include "lldb/lldb-private.h"
17#include "lldb/Core/RangeMap.h"
18#include "lldb/Core/UniqueCStringMap.h"
19#include "lldb/Host/Mutex.h"
20#include "lldb/Symbol/Symbol.h"
21
22namespace lldb_private {
23
24class Symtab
25{
26public:
27    typedef std::vector<uint32_t> IndexCollection;
28    typedef UniqueCStringMap<uint32_t> NameToIndexMap;
29
30    typedef enum Debug {
31        eDebugNo,   // Not a debug symbol
32        eDebugYes,  // A debug symbol
33        eDebugAny
34    } Debug;
35
36    typedef enum Visibility {
37        eVisibilityAny,
38        eVisibilityExtern,
39        eVisibilityPrivate
40    } Visibility;
41
42                        Symtab(ObjectFile *objfile);
43                        ~Symtab();
44
45            void        Reserve (size_t count);
46            Symbol *    Resize (size_t count);
47            uint32_t    AddSymbol(const Symbol& symbol);
48            size_t      GetNumSymbols() const;
49            void        Dump(Stream *s, Target *target, SortOrder sort_type);
50            void        Dump(Stream *s, Target *target, std::vector<uint32_t>& indexes) const;
51            uint32_t    GetIndexForSymbol (const Symbol *symbol) const;
52            Mutex &     GetMutex ()
53                        {
54                            return m_mutex;
55                        }
56            Symbol *    FindSymbolByID (lldb::user_id_t uid) const;
57            Symbol *    SymbolAtIndex (size_t idx);
58    const   Symbol *    SymbolAtIndex (size_t idx) const;
59            Symbol *    FindSymbolWithType (lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t &start_idx);
60            uint32_t    AppendSymbolIndexesWithType (lldb::SymbolType symbol_type, std::vector<uint32_t>& indexes, uint32_t start_idx = 0, uint32_t end_index = UINT32_MAX) const;
61            uint32_t    AppendSymbolIndexesWithTypeAndFlagsValue (lldb::SymbolType symbol_type, uint32_t flags_value, std::vector<uint32_t>& indexes, uint32_t start_idx = 0, uint32_t end_index = UINT32_MAX) const;
62            uint32_t    AppendSymbolIndexesWithType (lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& matches, uint32_t start_idx = 0, uint32_t end_index = UINT32_MAX) const;
63            uint32_t    AppendSymbolIndexesWithName (const ConstString& symbol_name, std::vector<uint32_t>& matches);
64            uint32_t    AppendSymbolIndexesWithName (const ConstString& symbol_name, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& matches);
65            uint32_t    AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, lldb::SymbolType symbol_type, std::vector<uint32_t>& matches);
66            uint32_t    AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& matches);
67            uint32_t    AppendSymbolIndexesMatchingRegExAndType (const RegularExpression &regex, lldb::SymbolType symbol_type, std::vector<uint32_t>& indexes);
68            uint32_t    AppendSymbolIndexesMatchingRegExAndType (const RegularExpression &regex, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes);
69            size_t      FindAllSymbolsWithNameAndType (const ConstString &name, lldb::SymbolType symbol_type, std::vector<uint32_t>& symbol_indexes);
70            size_t      FindAllSymbolsWithNameAndType (const ConstString &name, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes);
71            size_t      FindAllSymbolsMatchingRexExAndType (const RegularExpression &regex, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes);
72            Symbol *    FindFirstSymbolWithNameAndType (const ConstString &name, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility);
73            Symbol *    FindSymbolContainingFileAddress (lldb::addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes);
74            Symbol *    FindSymbolContainingFileAddress (lldb::addr_t file_addr);
75            size_t      FindFunctionSymbols (const ConstString &name, uint32_t name_type_mask, SymbolContextList& sc_list);
76            void        CalculateSymbolSizes ();
77
78            void        SortSymbolIndexesByValue (std::vector<uint32_t>& indexes, bool remove_duplicates) const;
79
80    static  void        DumpSymbolHeader (Stream *s);
81
82
83            void        Finalize ()
84                        {
85                            // Shrink to fit the symbols so we don't waste memory
86                            if (m_symbols.capacity() > m_symbols.size())
87                            {
88                                collection new_symbols (m_symbols.begin(), m_symbols.end());
89                                m_symbols.swap (new_symbols);
90                            }
91                        }
92
93            void        AppendSymbolNamesToMap (const IndexCollection &indexes,
94                                                bool add_demangled,
95                                                bool add_mangled,
96                                                NameToIndexMap &name_to_index_map) const;
97
98    ObjectFile *        GetObjectFile()
99                        {
100                            return m_objfile;
101                        }
102protected:
103    typedef std::vector<Symbol>         collection;
104    typedef collection::iterator        iterator;
105    typedef collection::const_iterator  const_iterator;
106    typedef RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t> FileRangeToIndexMap;
107            void        InitNameIndexes ();
108            void        InitAddressIndexes ();
109
110    ObjectFile *        m_objfile;
111    collection          m_symbols;
112    FileRangeToIndexMap m_file_addr_to_index;
113    UniqueCStringMap<uint32_t> m_name_to_index;
114    UniqueCStringMap<uint32_t> m_basename_to_index;
115    UniqueCStringMap<uint32_t> m_method_to_index;
116    UniqueCStringMap<uint32_t> m_selector_to_index;
117    mutable Mutex       m_mutex; // Provide thread safety for this symbol table
118    bool                m_file_addr_to_index_computed:1,
119                        m_name_indexes_computed:1;
120private:
121
122    bool
123    CheckSymbolAtIndex (size_t idx, Debug symbol_debug_type, Visibility symbol_visibility) const
124    {
125        switch (symbol_debug_type)
126        {
127        case eDebugNo:
128            if (m_symbols[idx].IsDebug() == true)
129                return false;
130            break;
131
132        case eDebugYes:
133            if (m_symbols[idx].IsDebug() == false)
134                return false;
135            break;
136
137        case eDebugAny:
138            break;
139        }
140
141        switch (symbol_visibility)
142        {
143        case eVisibilityAny:
144            return true;
145
146        case eVisibilityExtern:
147            return m_symbols[idx].IsExternal();
148
149        case eVisibilityPrivate:
150            return !m_symbols[idx].IsExternal();
151        }
152        return false;
153    }
154
155    void
156    SymbolIndicesToSymbolContextList (std::vector<uint32_t> &symbol_indexes,
157                                      SymbolContextList &sc_list);
158
159    DISALLOW_COPY_AND_ASSIGN (Symtab);
160};
161
162} // namespace lldb_private
163
164#endif  // liblldb_Symtab_h_
165