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