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