ClangASTContext.h revision 03e0f97cfa469792dd69b36f782d33a014225788
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/ClangASTType.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    GetBuiltInType_void();
97
98    void *
99    GetBuiltInType_objc_id();
100
101    void *
102    GetBuiltInType_objc_Class();
103
104    void *
105    GetBuiltInType_objc_selector();
106
107    void *
108    GetCStringType(bool is_const);
109
110    void *
111    GetVoidPtrType(bool is_const);
112
113    static void *
114    GetVoidPtrType(clang::ASTContext *ast_context, bool is_const);
115
116    static void *
117    CopyType(clang::ASTContext *dest_context,
118             clang::ASTContext *source_context,
119             void *clang_type);
120
121    static bool
122    AreTypesSame(clang::ASTContext *ast_context,
123                 void *type1,
124                 void *type2);
125
126    bool
127    AreTypesSame(void *type1,
128                 void *type2)
129    {
130        return ClangASTContext::AreTypesSame(m_ast_context_ap.get(), type1, type2);
131    }
132
133    //------------------------------------------------------------------
134    // CVR modifiers
135    //------------------------------------------------------------------
136
137    static void *
138    AddConstModifier (void *clang_type);
139
140    static void *
141    AddRestrictModifier (void *clang_type);
142
143    static void *
144    AddVolatileModifier (void *clang_type);
145
146    //------------------------------------------------------------------
147    // Structure, Unions, Classes
148    //------------------------------------------------------------------
149
150    void *
151    CreateRecordType (const char *name,
152                      int kind,
153                      clang::DeclContext *decl_ctx,
154                      lldb::LanguageType language);
155
156    bool
157    AddFieldToRecordType (void * record_qual_type,
158                          const char *name,
159                          void * field_type,
160                          lldb::AccessType access,
161                          uint32_t bitfield_bit_size);
162
163    bool
164    FieldIsBitfield (clang::FieldDecl* field,
165                     uint32_t& bitfield_bit_size);
166
167    static bool
168    FieldIsBitfield (clang::ASTContext *ast_context,
169                     clang::FieldDecl* field,
170                     uint32_t& bitfield_bit_size);
171
172    static bool
173    RecordHasFields (const clang::RecordDecl *record_decl);
174
175    void
176    SetDefaultAccessForRecordFields (void *clang_type,
177                                     int default_accessibility,
178                                     int *assigned_accessibilities,
179                                     size_t num_assigned_accessibilities);
180
181    void *
182    CreateObjCClass (const char *name,
183                     clang::DeclContext *decl_ctx,
184                     bool isForwardDecl,
185                     bool isInternal);
186
187    bool
188    AddObjCClassIVar (void *class_opaque_type,
189                      const char *name,
190                      void *ivar_opaque_type,
191                      lldb::AccessType access,
192                      uint32_t bitfield_bit_size,
193                      bool isSynthesized);
194
195    bool
196    SetObjCSuperClass (void *class_clang_type,
197                       void *superclass_clang_type);
198
199    static bool
200    ObjCTypeHasIVars (void *class_clang_type, bool check_superclass);
201
202    static bool
203    ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl,
204                      bool check_superclass);
205
206
207    //------------------------------------------------------------------
208    // Aggregate Types
209    //------------------------------------------------------------------
210    static bool
211    IsAggregateType (void *clang_type);
212
213    static uint32_t
214    GetNumChildren (void *clang_type,
215                    bool omit_empty_base_classes);
216
217    void *
218    GetChildClangTypeAtIndex (const char *parent_name,
219                              void * parent_clang_type,
220                              uint32_t idx,
221                              bool transparent_pointers,
222                              bool omit_empty_base_classes,
223                              std::string& child_name,
224                              uint32_t &child_byte_size,
225                              int32_t &child_byte_offset,
226                              uint32_t &child_bitfield_bit_size,
227                              uint32_t &child_bitfield_bit_offset);
228
229    static void *
230    GetChildClangTypeAtIndex (clang::ASTContext *ast_context,
231                              const char *parent_name,
232                              void * parent_clang_type,
233                              uint32_t idx,
234                              bool transparent_pointers,
235                              bool omit_empty_base_classes,
236                              std::string& child_name,
237                              uint32_t &child_byte_size,
238                              int32_t &child_byte_offset,
239                              uint32_t &child_bitfield_bit_size,
240                              uint32_t &child_bitfield_bit_offset);
241
242    // Lookup a child given a name. This function will match base class names
243    // and member member names in "clang_type" only, not descendants.
244    static uint32_t
245    GetIndexOfChildWithName (clang::ASTContext *ast_context,
246                             void *clang_type,
247                             const char *name,
248                             bool omit_empty_base_classes);
249
250    // Lookup a child member given a name. This function will match member names
251    // only and will descend into "clang_type" children in search for the first
252    // member in this class, or any base class that matches "name".
253    // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
254    // so we catch all names that match a given child name, not just the first.
255    static size_t
256    GetIndexOfChildMemberWithName (clang::ASTContext *ast_context,
257                                   void *clang_type,
258                                   const char *name,
259                                   bool omit_empty_base_classes,
260                                   std::vector<uint32_t>& child_indexes);
261
262    //------------------------------------------------------------------
263    // clang::TagType
264    //------------------------------------------------------------------
265
266    bool
267    SetTagTypeKind (void * tag_qual_type,
268                    int kind);
269
270    //------------------------------------------------------------------
271    // C++ Base Classes
272    //------------------------------------------------------------------
273
274    clang::CXXBaseSpecifier *
275    CreateBaseClassSpecifier (void * base_class_type,
276                              lldb::AccessType access,
277                              bool is_virtual,
278                              bool base_of_class);
279
280    static void
281    DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes,
282                               unsigned num_base_classes);
283
284    bool
285    SetBaseClassesForClassType (void * class_clang_type,
286                                clang::CXXBaseSpecifier const * const *base_classes,
287                                unsigned num_base_classes);
288
289    //------------------------------------------------------------------
290    // DeclContext Functions
291    //------------------------------------------------------------------
292
293    static clang::DeclContext *
294    GetDeclContextForType (void * qual_type);
295
296    //------------------------------------------------------------------
297    // Namespace Declarations
298    //------------------------------------------------------------------
299
300    clang::NamespaceDecl *
301    GetUniqueNamespaceDeclaration (const char *name,
302                                   const Declaration &decl,
303                                   clang::DeclContext *decl_ctx);
304
305    //------------------------------------------------------------------
306    // Function Types
307    //------------------------------------------------------------------
308
309    clang::FunctionDecl *
310    CreateFunctionDeclaration (const char *name,
311                               void * function_Type,
312                               int storage,
313                               bool is_inline);
314
315    void *
316    CreateFunctionType (void * result_type,
317                        void **args,
318                        unsigned num_args,
319                        bool isVariadic,
320                        unsigned TypeQuals);
321
322    clang::ParmVarDecl *
323    CreateParmeterDeclaration (const char *name,
324                               void * return_type,
325                               int storage);
326
327    void
328    SetFunctionParameters (clang::FunctionDecl *function_decl,
329                           clang::ParmVarDecl **params,
330                           unsigned num_params);
331
332    //------------------------------------------------------------------
333    // Array Types
334    //------------------------------------------------------------------
335
336    void *
337    CreateArrayType (void * element_type,
338                     size_t element_count,
339                     uint32_t bit_stride);
340
341    //------------------------------------------------------------------
342    // Tag Declarations
343    //------------------------------------------------------------------
344    bool
345    StartTagDeclarationDefinition (void * qual_type);
346
347    bool
348    CompleteTagDeclarationDefinition (void * qual_type);
349
350    //------------------------------------------------------------------
351    // Enumeration Types
352    //------------------------------------------------------------------
353    void *
354    CreateEnumerationType (const Declaration &decl, const char *name, void *integer_qual_type);
355
356    bool
357    AddEnumerationValueToEnumerationType (void * enum_qual_type,
358                                          void * enumerator_qual_type,
359                                          const Declaration &decl,
360                                          const char *name,
361                                          int64_t enum_value,
362                                          uint32_t enum_value_bit_size);
363
364    //------------------------------------------------------------------
365    // Pointers & References
366    //------------------------------------------------------------------
367    void *
368    CreatePointerType (void *clang_type);
369
370    void *
371    CreateLValueReferenceType (void *clang_type);
372
373    void *
374    CreateRValueReferenceType (void *clang_type);
375
376    void *
377    CreateMemberPointerType (void * clang_pointee_type,
378                             void * clang_class_type);
379
380    size_t
381    GetPointerBitSize ();
382
383    static bool
384    IsIntegerType (void *clang_type, bool &is_signed);
385
386    static bool
387    IsPointerType (void *clang_type, void **target_type = NULL);
388
389    static bool
390    IsPointerOrReferenceType (void *clang_type, void **target_type = NULL);
391
392    static bool
393    IsCStringType (void *clang_type, uint32_t &length);
394
395    static bool
396    IsFunctionPointerType (void *clang_type);
397
398    static bool
399    IsArrayType (void *clang_type, void **member_type = NULL, uint64_t *size = NULL);
400
401    //------------------------------------------------------------------
402    // Typedefs
403    //------------------------------------------------------------------
404    void *
405    CreateTypedefType (const char *name,
406                       void *clang_type,
407                       clang::DeclContext *decl_ctx);
408
409    //------------------------------------------------------------------
410    // Type names
411    //------------------------------------------------------------------
412    static std::string
413    GetTypeName(void *clang_type);
414
415    static bool
416    IsFloatingPointType (void *clang_type, uint32_t &count, bool &is_complex);
417
418    //static bool
419    //ConvertFloatValueToString (clang::ASTContext *ast_context,
420    //                           void *clang_type,
421    //                           const uint8_t* bytes,
422    //                           size_t byte_size,
423    //                           int apint_byte_order,
424    //                           std::string &float_str);
425
426    static size_t
427    ConvertStringToFloatValue (clang::ASTContext *ast_context,
428                               void *clang_type,
429                               const char *s,
430                               uint8_t *dst,
431                               size_t dst_size);
432
433protected:
434    //------------------------------------------------------------------
435    // Classes that inherit from ClangASTContext can see and modify these
436    //------------------------------------------------------------------
437    std::string                             m_target_triple;
438    std::auto_ptr<clang::ASTContext>        m_ast_context_ap;
439    std::auto_ptr<clang::LangOptions>       m_language_options_ap;
440    std::auto_ptr<clang::SourceManager>     m_source_manager_ap;
441    std::auto_ptr<clang::Diagnostic>        m_diagnostic_ap;
442    std::auto_ptr<clang::TargetOptions>     m_target_options_ap;
443    std::auto_ptr<clang::TargetInfo>        m_target_info_ap;
444    std::auto_ptr<clang::IdentifierTable>   m_identifier_table_ap;
445    std::auto_ptr<clang::SelectorTable>     m_selector_table_ap;
446    std::auto_ptr<clang::Builtin::Context>  m_builtins_ap;
447
448private:
449    //------------------------------------------------------------------
450    // For ClangASTContext only
451    //------------------------------------------------------------------
452    ClangASTContext(const ClangASTContext&);
453    const ClangASTContext& operator=(const ClangASTContext&);
454};
455
456} // namespace lldb_private
457
458#endif  // liblldb_ClangASTContext_h_
459