ClangASTContext.h revision f328c9ffe0bb00f48601027ec86dbdf238b42c2a
1//===-- ClangASTContext.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_ClangASTContext_h_
11#define liblldb_ClangASTContext_h_
12
13// C Includes
14// C++ Includes
15#include <string>
16#include <vector>
17#include <memory>
18#include <stdint.h>
19
20// Other libraries and framework includes
21// Project includes
22#include "lldb/lldb-enumerations.h"
23#include "lldb/Core/ClangForward.h"
24#include "lldb/Symbol/ASTType.h"
25
26namespace lldb_private {
27
28class Declaration;
29
30class ClangASTContext
31{
32public:
33    //------------------------------------------------------------------
34    // Constructors and Destructors
35    //------------------------------------------------------------------
36    ClangASTContext(const char *target_triple);
37
38    ~ClangASTContext();
39
40    clang::ASTContext *
41    getASTContext();
42
43    clang::Builtin::Context *
44    getBuiltinContext();
45
46    clang::IdentifierTable *
47    getIdentifierTable();
48
49    clang::LangOptions *
50    getLanguageOptions();
51
52    clang::SelectorTable *
53    getSelectorTable();
54
55    clang::SourceManager *
56    getSourceManager();
57
58    clang::Diagnostic *
59    getDiagnostic();
60
61    clang::TargetOptions *
62    getTargetOptions();
63
64    clang::TargetInfo *
65    getTargetInfo();
66
67    void
68    Clear();
69
70    const char *
71    GetTargetTriple ();
72
73    void
74    SetTargetTriple (const char *target_triple);
75
76    //------------------------------------------------------------------
77    // Basic Types
78    //------------------------------------------------------------------
79
80    void *
81    GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding,
82                                          uint32_t bit_size);
83
84    static void *
85    GetBuiltinTypeForEncodingAndBitSize (clang::ASTContext *ast_context,
86                                         lldb::Encoding encoding,
87                                         uint32_t bit_size);
88
89    void *
90    GetBuiltinTypeForDWARFEncodingAndBitSize (
91        const char *type_name,
92        uint32_t dw_ate,
93        uint32_t bit_size);
94
95    void *
96    GetVoidBuiltInType();
97
98    void *
99    GetCStringType(bool is_const);
100
101    void *
102    GetVoidPtrType(bool is_const);
103
104    static void *
105    GetVoidPtrType(clang::ASTContext *ast_context, bool is_const);
106
107    static void *
108    CopyType(clang::ASTContext *dest_context,
109             clang::ASTContext *source_context,
110             void * clang_type);
111
112    static bool
113    AreTypesSame(clang::ASTContext *ast_context,
114                 void *type1,
115                 void *type2);
116
117    bool
118    AreTypesSame(void *type1,
119                 void *type2)
120    {
121        return ClangASTContext::AreTypesSame(m_ast_context_ap.get(), type1, type2);
122    }
123
124    //------------------------------------------------------------------
125    // CVR modifiers
126    //------------------------------------------------------------------
127
128    static void *
129    AddConstModifier (void * clang_type);
130
131    static void *
132    AddRestrictModifier (void * clang_type);
133
134    static void *
135    AddVolatileModifier (void * clang_type);
136
137    //------------------------------------------------------------------
138    // Structure, Unions, Classes
139    //------------------------------------------------------------------
140
141    void *
142    CreateRecordType (const char *name,
143                      int kind,
144                      clang::DeclContext *decl_ctx);
145
146    bool
147    AddFieldToRecordType (void * record_qual_type,
148                          const char *name,
149                          void * field_type,
150                          int access,
151                          uint32_t bitfield_bit_size);
152
153    bool
154    FieldIsBitfield (clang::FieldDecl* field,
155                     uint32_t& bitfield_bit_size);
156
157    static bool
158    FieldIsBitfield (clang::ASTContext *ast_context,
159                     clang::FieldDecl* field,
160                     uint32_t& bitfield_bit_size);
161
162    static bool
163    RecordHasFields (const clang::RecordDecl *record_decl);
164
165    void
166    SetDefaultAccessForRecordFields (void * clang_qual_type,
167                                     int default_accessibility,
168                                     int *assigned_accessibilities,
169                                     size_t num_assigned_accessibilities);
170
171    //------------------------------------------------------------------
172    // Aggregate Types
173    //------------------------------------------------------------------
174    static bool
175    IsAggregateType (void * clang_type);
176
177    static uint32_t
178    GetNumChildren (void * clang_type,
179                    bool omit_empty_base_classes);
180
181    void *
182    GetChildClangTypeAtIndex (const char *parent_name,
183                              void * parent_clang_type,
184                              uint32_t idx,
185                              bool transparent_pointers,
186                              bool omit_empty_base_classes,
187                              std::string& child_name,
188                              uint32_t &child_byte_size,
189                              int32_t &child_byte_offset,
190                              uint32_t &child_bitfield_bit_size,
191                              uint32_t &child_bitfield_bit_offset);
192
193    static void *
194    GetChildClangTypeAtIndex (clang::ASTContext *ast_context,
195                              const char *parent_name,
196                              void * parent_clang_type,
197                              uint32_t idx,
198                              bool transparent_pointers,
199                              bool omit_empty_base_classes,
200                              std::string& child_name,
201                              uint32_t &child_byte_size,
202                              int32_t &child_byte_offset,
203                              uint32_t &child_bitfield_bit_size,
204                              uint32_t &child_bitfield_bit_offset);
205
206    // Lookup a child given a name. This function will match base class names
207    // and member member names in "clang_type" only, not descendants.
208    static uint32_t
209    GetIndexOfChildWithName (clang::ASTContext *ast_context,
210                             void *clang_type,
211                             const char *name,
212                             bool omit_empty_base_classes);
213
214    // Lookup a child member given a name. This function will match member names
215    // only and will descend into "clang_type" children in search for the first
216    // member in this class, or any base class that matches "name".
217    // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
218    // so we catch all names that match a given child name, not just the first.
219    static size_t
220    GetIndexOfChildMemberWithName (clang::ASTContext *ast_context,
221                                   void *clang_type,
222                                   const char *name,
223                                   bool omit_empty_base_classes,
224                                   std::vector<uint32_t>& child_indexes);
225
226    //------------------------------------------------------------------
227    // clang::TagType
228    //------------------------------------------------------------------
229
230    bool
231    SetTagTypeKind (void * tag_qual_type,
232                    int kind);
233
234    //------------------------------------------------------------------
235    // C++ Base Classes
236    //------------------------------------------------------------------
237
238    clang::CXXBaseSpecifier *
239    CreateBaseClassSpecifier (void * base_class_type,
240                              int access,
241                              bool is_virtual,
242                              bool base_of_class);
243
244    static void
245    DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes,
246                               unsigned num_base_classes);
247
248    bool
249    SetBaseClassesForClassType (void * class_clang_type,
250                                clang::CXXBaseSpecifier const * const *base_classes,
251                                unsigned num_base_classes);
252
253    //------------------------------------------------------------------
254    // DeclContext Functions
255    //------------------------------------------------------------------
256
257    static clang::DeclContext *
258    GetDeclContextForType (void * qual_type);
259
260    //------------------------------------------------------------------
261    // Namespace Declarations
262    //------------------------------------------------------------------
263
264    clang::NamespaceDecl *
265    GetUniqueNamespaceDeclaration (const char *name,
266                                   const Declaration &decl,
267                                   clang::DeclContext *decl_ctx);
268
269    //------------------------------------------------------------------
270    // Function Types
271    //------------------------------------------------------------------
272
273    clang::FunctionDecl *
274    CreateFunctionDeclaration (const char *name,
275                               void * function_Type,
276                               int storage,
277                               bool is_inline);
278
279    void *
280    CreateFunctionType (void * result_type,
281                        void **args,
282                        unsigned num_args,
283                        bool isVariadic,
284                        unsigned TypeQuals);
285
286    clang::ParmVarDecl *
287    CreateParmeterDeclaration (const char *name,
288                               void * return_type,
289                               int storage);
290
291    void
292    SetFunctionParameters (clang::FunctionDecl *function_decl,
293                           clang::ParmVarDecl **params,
294                           unsigned num_params);
295
296    //------------------------------------------------------------------
297    // Array Types
298    //------------------------------------------------------------------
299
300    void *
301    CreateArrayType (void * element_type,
302                     size_t element_count,
303                     uint32_t bit_stride);
304
305    //------------------------------------------------------------------
306    // Tag Declarations
307    //------------------------------------------------------------------
308    bool
309    StartTagDeclarationDefinition (void * qual_type);
310
311    bool
312    CompleteTagDeclarationDefinition (void * qual_type);
313
314    //------------------------------------------------------------------
315    // Enumeration Types
316    //------------------------------------------------------------------
317    void *
318    CreateEnumerationType (const Declaration &decl, const char *name);
319
320    bool
321    AddEnumerationValueToEnumerationType (void * enum_qual_type,
322                                          void * enumerator_qual_type,
323                                          const Declaration &decl,
324                                          const char *name,
325                                          int64_t enum_value,
326                                          uint32_t enum_value_bit_size);
327
328    //------------------------------------------------------------------
329    // Pointers & References
330    //------------------------------------------------------------------
331    void *
332    CreatePointerType (void * clang_type);
333
334    void *
335    CreateLValueReferenceType (void * clang_type);
336
337    void *
338    CreateRValueReferenceType (void * clang_type);
339
340    void *
341    CreateMemberPointerType (void * clang_pointee_type,
342                             void * clang_class_type);
343
344    size_t
345    GetPointerBitSize ();
346
347    static size_t
348    GetTypeBitSize (clang::ASTContext *ast_context, void * clang_type);
349
350    static size_t
351    GetTypeBitAlign (clang::ASTContext *ast_context, void * clang_type);
352
353    static bool
354    IsIntegerType (void * clang_type, bool &is_signed);
355
356    static bool
357    IsPointerType (void * clang_type, void **target_type = NULL);
358
359    static bool
360    IsPointerOrReferenceType (void * clang_type, void **target_type = NULL);
361
362    static bool
363    IsCStringType (void * clang_type, uint32_t &length);
364
365    static bool
366    IsArrayType (void * clang_type, void **member_type = NULL, uint64_t *size = NULL);
367
368    //------------------------------------------------------------------
369    // Typedefs
370    //------------------------------------------------------------------
371    void *
372    CreateTypedefType (const char *name,
373                       void * clang_type,
374                       clang::DeclContext *decl_ctx);
375
376    //------------------------------------------------------------------
377    // Type names
378    //------------------------------------------------------------------
379    static std::string
380    GetTypeName(void *clang_type);
381
382    static bool
383    IsFloatingPointType (void * clang_type, uint32_t &count, bool &is_complex);
384
385    //static bool
386    //ConvertFloatValueToString (clang::ASTContext *ast_context,
387    //                           void * clang_type,
388    //                           const uint8_t* bytes,
389    //                           size_t byte_size,
390    //                           int apint_byte_order,
391    //                           std::string &float_str);
392
393    static size_t
394    ConvertStringToFloatValue (clang::ASTContext *ast_context,
395                               void * clang_type,
396                               const char *s,
397                               uint8_t *dst,
398                               size_t dst_size);
399
400protected:
401    //------------------------------------------------------------------
402    // Classes that inherit from ClangASTContext can see and modify these
403    //------------------------------------------------------------------
404    std::string                             m_target_triple;
405    std::auto_ptr<clang::ASTContext>        m_ast_context_ap;
406    std::auto_ptr<clang::LangOptions>       m_language_options_ap;
407    std::auto_ptr<clang::SourceManager>     m_source_manager_ap;
408    std::auto_ptr<clang::Diagnostic>        m_diagnostic_ap;
409    std::auto_ptr<clang::TargetOptions>     m_target_options_ap;
410    std::auto_ptr<clang::TargetInfo>        m_target_info_ap;
411    std::auto_ptr<clang::IdentifierTable>   m_identifier_table_ap;
412    std::auto_ptr<clang::SelectorTable>     m_selector_table_ap;
413    std::auto_ptr<clang::Builtin::Context>  m_builtins_ap;
414
415private:
416    //------------------------------------------------------------------
417    // For ClangASTContext only
418    //------------------------------------------------------------------
419    ClangASTContext(const ClangASTContext&);
420    const ClangASTContext& operator=(const ClangASTContext&);
421};
422
423} // namespace lldb_private
424
425#endif  // liblldb_ClangASTContext_h_
426