1 //===-- DWARFDeclContext.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 #ifndef SymbolFileDWARF_DWARFDeclContext_h_
11 #define SymbolFileDWARF_DWARFDeclContext_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <string>
16 #include <vector>
17 // Other libraries and framework includes
18 #include "lldb/Core/ConstString.h"
19 // Project includes
20 #include "DWARFDefines.h"
21 
22 //----------------------------------------------------------------------
23 // DWARFDeclContext
24 //
25 // A class that represents a declaration context all the way down to a
26 // DIE. This is useful when trying to find a DIE in one DWARF to a DIE
27 // in another DWARF file.
28 //----------------------------------------------------------------------
29 
30 class DWARFDeclContext
31 {
32 public:
33     struct Entry
34     {
35         Entry () :
36             tag(0),
37             name(NULL)
38         {
39         }
40         Entry (dw_tag_t t, const char *n) :
41             tag(t),
42             name(n)
43         {
44         }
45 
46         bool
47         NameMatches (const Entry& rhs) const
48         {
49             if (name == rhs.name)
50                 return true;
51             else if (name && rhs.name)
52                 return strcmp(name, rhs.name) == 0;
53             return false;
54         }
55 
56         // Test operator
57         explicit operator bool() const
58         {
59             return tag != 0;
60         }
61 
62         dw_tag_t tag;
63         const char *name;
64     };
65 
66     DWARFDeclContext () :
67         m_entries(),
68         m_language(lldb::eLanguageTypeUnknown)
69     {
70     }
71 
72     void
73     AppendDeclContext (dw_tag_t tag, const char *name)
74     {
75         m_entries.push_back(Entry(tag, name));
76     }
77 
78     bool
79     operator ==(const DWARFDeclContext& rhs) const;
80 
81     uint32_t
82     GetSize() const
83     {
84         return m_entries.size();
85     }
86 
87     Entry &
88     operator[] (uint32_t idx)
89     {
90         // "idx" must be valid
91         return m_entries[idx];
92     }
93 
94     const Entry &
95     operator[] (uint32_t idx) const
96     {
97         // "idx" must be valid
98         return m_entries[idx];
99     }
100 
101     const char *
102     GetQualifiedName () const;
103 
104     // Same as GetQaulifiedName, but the life time of the returned string will
105     // be that of the LLDB session.
106     lldb_private::ConstString
107     GetQualifiedNameAsConstString () const
108     {
109         return lldb_private::ConstString (GetQualifiedName ());
110     }
111 
112     void
113     Clear()
114     {
115         m_entries.clear();
116         m_qualified_name.clear();
117     }
118 
119     lldb::LanguageType
120     GetLanguage() const
121     {
122         return m_language;
123     }
124 
125     void
126     SetLanguage(lldb::LanguageType language)
127     {
128         m_language = language;
129     }
130 
131 protected:
132     typedef std::vector<Entry> collection;
133     collection m_entries;
134     mutable std::string m_qualified_name;
135     lldb::LanguageType m_language;
136 };
137 
138 #endif  // SymbolFileDWARF_DWARFDeclContext_h_
139