ClangASTType.h revision 946618a80e17d210e6592e94d10a15ad592572ba
1//===-- ClangASTType.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 liblldb_ClangASTType_h_
11#define liblldb_ClangASTType_h_
12
13#include <string>
14#include "lldb/lldb-private.h"
15#include "lldb/Core/ClangForward.h"
16
17namespace lldb_private {
18
19//----------------------------------------------------------------------
20// A class that can carry around a clang ASTContext and a opaque clang
21// QualType. A clang::QualType can be easily reconstructed from an
22// opaque clang type and often the ASTContext is needed when doing
23// various type related tasks, so this class allows both items to travel
24// in a single very lightweight class that can be used. There are many
25// static equivalents of the member functions that allow the ASTContext
26// and the opaque clang QualType to be specified for ease of use and
27// to avoid code duplication.
28//----------------------------------------------------------------------
29class ClangASTType
30{
31public:
32
33    ClangASTType (clang::ASTContext *ast_context, lldb::clang_type_t type) :
34        m_type (type),
35        m_ast  (ast_context)
36    {
37    }
38
39    ClangASTType (const ClangASTType &tw) :
40        m_type (tw.m_type),
41        m_ast  (tw.m_ast)
42    {
43    }
44
45    ClangASTType () :
46        m_type (0),
47        m_ast  (0)
48    {
49    }
50
51    virtual ~ClangASTType();
52
53    const ClangASTType &
54    operator= (const ClangASTType &atb)
55    {
56        m_type = atb.m_type;
57        m_ast = atb.m_ast;
58        return *this;
59    }
60
61    bool
62    IsValid () const
63    {
64        return m_type != NULL && m_ast != NULL;
65    }
66
67    lldb::clang_type_t
68    GetOpaqueQualType() const
69    {
70        return m_type;
71    }
72
73    clang::ASTContext *
74    GetASTContext() const
75    {
76        return m_ast;
77    }
78
79    static ClangASTType
80    GetCanonicalType (clang::ASTContext *ast, lldb::clang_type_t clang_type);
81
82    ClangASTType
83    GetCanonicalType ()
84    {
85        return GetCanonicalType (GetASTContext(), GetOpaqueQualType());
86    }
87
88    ConstString
89    GetConstTypeName ();
90
91    ConstString
92    GetConstQualifiedTypeName ();
93
94    static lldb::BasicType
95    GetBasicTypeEnumeration (const ConstString &name);
96
97    static ClangASTType
98    GetBasicType (clang::ASTContext *ast, lldb::BasicType type);
99
100    static ClangASTType
101    GetBasicType (clang::ASTContext *ast, const ConstString &name);
102
103    static ConstString
104    GetConstTypeName (clang::ASTContext *ast,
105                      lldb::clang_type_t clang_type);
106
107    static ConstString
108    GetConstQualifiedTypeName (clang::ASTContext *ast,
109                               lldb::clang_type_t clang_type);
110
111    static std::string
112    GetTypeNameForQualType (clang::ASTContext *ast,
113                            clang::QualType qual_type);
114
115    static std::string
116    GetTypeNameForOpaqueQualType (clang::ASTContext *ast,
117                                  lldb::clang_type_t opaque_qual_type);
118
119    uint64_t
120    GetClangTypeByteSize ();
121
122    static uint64_t
123    GetClangTypeByteSize (clang::ASTContext *ast_context,
124                          lldb::clang_type_t clang_type);
125
126    uint64_t
127    GetClangTypeBitWidth ();
128
129    static uint64_t
130    GetClangTypeBitWidth (clang::ASTContext *ast_context, lldb::clang_type_t opaque_clang_qual_type);
131
132    size_t
133    GetTypeBitAlign ();
134
135    static size_t
136    GetTypeBitAlign (clang::ASTContext *ast_context, lldb::clang_type_t clang_type);
137
138    lldb::LanguageType
139    GetMinimumLanguage ();
140
141    static lldb::LanguageType
142    GetMinimumLanguage (clang::ASTContext *ctx,
143                        lldb::clang_type_t clang_type);
144
145    static lldb::TypeClass
146    GetTypeClass (clang::ASTContext *ast_context,
147                  lldb::clang_type_t clang_type);
148
149    lldb::TypeClass
150    GetTypeClass () const
151    {
152        return GetTypeClass (GetASTContext(), GetOpaqueQualType());
153    }
154
155    void
156    DumpValue (ExecutionContext *exe_ctx,
157               Stream *s,
158               lldb::Format format,
159               const DataExtractor &data,
160               lldb::offset_t data_offset,
161               size_t data_byte_size,
162               uint32_t bitfield_bit_size,
163               uint32_t bitfield_bit_offset,
164               bool show_types,
165               bool show_summary,
166               bool verbose,
167               uint32_t depth);
168
169    static void
170    DumpValue (clang::ASTContext *ast_context,
171               lldb::clang_type_t opaque_clang_qual_type,
172               ExecutionContext *exe_ctx,
173               Stream *s,
174               lldb::Format format,
175               const DataExtractor &data,
176               lldb::offset_t data_offset,
177               size_t data_byte_size,
178               uint32_t bitfield_bit_size,
179               uint32_t bitfield_bit_offset,
180               bool show_types,
181               bool show_summary,
182               bool verbose,
183               uint32_t depth);
184
185    bool
186    DumpTypeValue (Stream *s,
187                   lldb::Format format,
188                   const DataExtractor &data,
189                   lldb::offset_t data_offset,
190                   size_t data_byte_size,
191                   uint32_t bitfield_bit_size,
192                   uint32_t bitfield_bit_offset,
193                   ExecutionContextScope *exe_scope);
194
195
196    static bool
197    DumpTypeValue (clang::ASTContext *ast_context,
198                   lldb::clang_type_t opaque_clang_qual_type,
199                   Stream *s,
200                   lldb::Format format,
201                   const DataExtractor &data,
202                   lldb::offset_t data_offset,
203                   size_t data_byte_size,
204                   uint32_t bitfield_bit_size,
205                   uint32_t bitfield_bit_offset,
206                   ExecutionContextScope *exe_scope);
207
208    void
209    DumpSummary (ExecutionContext *exe_ctx,
210                 Stream *s,
211                 const DataExtractor &data,
212                 lldb::offset_t data_offset,
213                 size_t data_byte_size);
214
215
216    static void
217    DumpSummary (clang::ASTContext *ast_context,
218                 lldb::clang_type_t opaque_clang_qual_type,
219                 ExecutionContext *exe_ctx,
220                 Stream *s,
221                 const DataExtractor &data,
222                 lldb::offset_t data_offset,
223                 size_t data_byte_size);
224
225    void
226    DumpTypeDescription (); // Dump to stdout
227
228    void
229    DumpTypeDescription (Stream *s);
230
231    static void
232    DumpTypeDescription (clang::ASTContext *ast_context,
233                         lldb::clang_type_t opaque_clang_qual_type,
234                         Stream *s);
235
236    lldb::Encoding
237    GetEncoding (uint64_t &count);
238
239    static lldb::Encoding
240    GetEncoding (lldb::clang_type_t opaque_clang_qual_type, uint64_t &count);
241
242    lldb::Format
243    GetFormat ();
244
245    static lldb::Format
246    GetFormat (lldb::clang_type_t opaque_clang_qual_type);
247
248    uint64_t
249    GetTypeByteSize() const;
250
251    static uint64_t
252    GetTypeByteSize(clang::ASTContext *ast_context,
253                    lldb::clang_type_t opaque_clang_qual_type);
254
255    bool
256    GetValueAsScalar (const DataExtractor &data,
257                      lldb::offset_t data_offset,
258                      size_t data_byte_size,
259                      Scalar &value);
260
261    static bool
262    GetValueAsScalar (clang::ASTContext *ast_context,
263                      lldb::clang_type_t opaque_clang_qual_type,
264                      const DataExtractor &data,
265                      lldb::offset_t data_offset,
266                      size_t data_byte_size,
267                      Scalar &value);
268
269
270    bool
271    IsDefined();
272
273    static bool
274    IsDefined (lldb::clang_type_t opaque_clang_qual_type);
275
276    bool
277    IsConst();
278
279    static bool
280    IsConst (lldb::clang_type_t opaque_clang_qual_type);
281
282    bool
283    SetValueFromScalar (const Scalar &value,
284                        Stream &strm);
285
286    static bool
287    SetValueFromScalar (clang::ASTContext *ast_context,
288                        lldb::clang_type_t opaque_clang_qual_type,
289                        const Scalar &value,
290                        Stream &strm);
291
292    void
293    SetClangType (clang::ASTContext *ast, lldb::clang_type_t type)
294    {
295        m_type = type;
296        m_ast = ast;
297    }
298
299    bool
300    ReadFromMemory (ExecutionContext *exe_ctx,
301                    lldb::addr_t addr,
302                    AddressType address_type,
303                    DataExtractor &data);
304
305    static bool
306    ReadFromMemory (clang::ASTContext *ast_context,
307                    lldb::clang_type_t opaque_clang_qual_type,
308                    ExecutionContext *exe_ctx,
309                    lldb::addr_t addr,
310                    AddressType address_type,
311                    DataExtractor &data);
312
313    bool
314    WriteToMemory (ExecutionContext *exe_ctx,
315                   lldb::addr_t addr,
316                   AddressType address_type,
317                   StreamString &new_value);
318
319    static bool
320    WriteToMemory (clang::ASTContext *ast_context,
321                   lldb::clang_type_t opaque_clang_qual_type,
322                   ExecutionContext *exe_ctx,
323                   lldb::addr_t addr,
324                   AddressType address_type,
325                   StreamString &new_value);
326
327    lldb::clang_type_t
328    GetPointeeType () const;
329
330    static lldb::clang_type_t
331    GetPointeeType (lldb::clang_type_t opaque_clang_qual_type);
332
333    lldb::clang_type_t
334    GetArrayElementType (uint64_t& stride);
335
336    static lldb::clang_type_t
337    GetArrayElementType (clang::ASTContext* ast,
338                         lldb::clang_type_t opaque_clang_qual_type,
339						 uint64_t& stride);
340
341    lldb::clang_type_t
342    GetPointerType () const;
343
344    static lldb::clang_type_t
345    GetPointerType (clang::ASTContext *ast_context,
346                    lldb::clang_type_t opaque_clang_qual_type);
347
348    static lldb::clang_type_t
349    RemoveFastQualifiers (lldb::clang_type_t);
350
351    static clang::CXXRecordDecl *
352    GetAsCXXRecordDecl (lldb::clang_type_t opaque_clang_qual_type);
353
354    void
355    Clear()
356    {
357        m_type = NULL;
358        m_ast = NULL;
359    }
360
361private:
362    lldb::clang_type_t m_type;
363    clang::ASTContext *m_ast;
364};
365
366bool operator == (const ClangASTType &lhs, const ClangASTType &rhs);
367bool operator != (const ClangASTType &lhs, const ClangASTType &rhs);
368
369
370} // namespace lldb_private
371
372#endif // #ifndef liblldb_ClangASTType_h_
373