ClangASTContext.h revision 1d8173f1b622482bd6610cc34ffd5668b75cf7bd
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                              bool is_static,
193                              bool is_inline);
194
195    clang::CXXMethodDecl *
196    AddMethodToCXXRecordType (void *record_opaque_type,
197                              const char *name,
198                              void *method_type,
199                              lldb::AccessType access,
200                              bool is_virtual,
201                              bool is_static,
202                              bool is_inline)
203
204    {
205        return ClangASTContext::AddMethodToCXXRecordType (getASTContext(),
206                                                          record_opaque_type,
207                                                          name,
208                                                          method_type,
209                                                          access,
210                                                          is_virtual,
211                                                          is_static,
212                                                          is_inline);
213    }
214
215    bool
216    FieldIsBitfield (clang::FieldDecl* field,
217                     uint32_t& bitfield_bit_size);
218
219    static bool
220    FieldIsBitfield (clang::ASTContext *ast_context,
221                     clang::FieldDecl* field,
222                     uint32_t& bitfield_bit_size);
223
224    static bool
225    RecordHasFields (const clang::RecordDecl *record_decl);
226
227    void
228    SetDefaultAccessForRecordFields (void *clang_type,
229                                     int default_accessibility,
230                                     int *assigned_accessibilities,
231                                     size_t num_assigned_accessibilities);
232
233    void *
234    CreateObjCClass (const char *name,
235                     clang::DeclContext *decl_ctx,
236                     bool isForwardDecl,
237                     bool isInternal);
238
239    static bool
240    AddObjCClassIVar (clang::ASTContext *ast_context,
241                      void *class_opaque_type,
242                      const char *name,
243                      void *ivar_opaque_type,
244                      lldb::AccessType access,
245                      uint32_t bitfield_bit_size,
246                      bool isSynthesized);
247
248    bool
249    AddObjCClassIVar (void *class_opaque_type,
250                      const char *name,
251                      void *ivar_opaque_type,
252                      lldb::AccessType access,
253                      uint32_t bitfield_bit_size,
254                      bool isSynthesized)
255    {
256        return ClangASTContext::AddObjCClassIVar (getASTContext(),
257                                                  class_opaque_type,
258                                                  name,
259                                                  ivar_opaque_type,
260                                                  access,
261                                                  bitfield_bit_size,
262                                                  isSynthesized);
263    }
264
265    bool
266    SetObjCSuperClass (void *class_clang_type,
267                       void *superclass_clang_type);
268
269    static bool
270    ObjCTypeHasIVars (void *class_clang_type, bool check_superclass);
271
272    static bool
273    ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl,
274                      bool check_superclass);
275
276
277    static clang::ObjCMethodDecl *
278    AddMethodToObjCObjectType (clang::ASTContext *ast_context,
279                               void *class_opaque_type,
280                               const char *name,  // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
281                               void *method_opaque_type,
282                               lldb::AccessType access);
283
284    clang::ObjCMethodDecl *
285    AddMethodToObjCObjectType (void *class_opaque_type,
286                               const char *name,  // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
287                               void *method_opaque_type,
288                               lldb::AccessType access)
289    {
290        return AddMethodToObjCObjectType (getASTContext(),
291                                          class_opaque_type,
292                                          name,
293                                          method_opaque_type,
294                                          access);
295    }
296
297
298    //------------------------------------------------------------------
299    // Aggregate Types
300    //------------------------------------------------------------------
301    static bool
302    IsAggregateType (void *clang_type);
303
304    static uint32_t
305    GetNumChildren (void *clang_type,
306                    bool omit_empty_base_classes);
307
308    void *
309    GetChildClangTypeAtIndex (const char *parent_name,
310                              void * parent_clang_type,
311                              uint32_t idx,
312                              bool transparent_pointers,
313                              bool omit_empty_base_classes,
314                              std::string& child_name,
315                              uint32_t &child_byte_size,
316                              int32_t &child_byte_offset,
317                              uint32_t &child_bitfield_bit_size,
318                              uint32_t &child_bitfield_bit_offset);
319
320    static void *
321    GetChildClangTypeAtIndex (clang::ASTContext *ast_context,
322                              const char *parent_name,
323                              void * parent_clang_type,
324                              uint32_t idx,
325                              bool transparent_pointers,
326                              bool omit_empty_base_classes,
327                              std::string& child_name,
328                              uint32_t &child_byte_size,
329                              int32_t &child_byte_offset,
330                              uint32_t &child_bitfield_bit_size,
331                              uint32_t &child_bitfield_bit_offset);
332
333    // Lookup a child given a name. This function will match base class names
334    // and member member names in "clang_type" only, not descendants.
335    static uint32_t
336    GetIndexOfChildWithName (clang::ASTContext *ast_context,
337                             void *clang_type,
338                             const char *name,
339                             bool omit_empty_base_classes);
340
341    // Lookup a child member given a name. This function will match member names
342    // only and will descend into "clang_type" children in search for the first
343    // member in this class, or any base class that matches "name".
344    // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
345    // so we catch all names that match a given child name, not just the first.
346    static size_t
347    GetIndexOfChildMemberWithName (clang::ASTContext *ast_context,
348                                   void *clang_type,
349                                   const char *name,
350                                   bool omit_empty_base_classes,
351                                   std::vector<uint32_t>& child_indexes);
352
353    //------------------------------------------------------------------
354    // clang::TagType
355    //------------------------------------------------------------------
356
357    bool
358    SetTagTypeKind (void * tag_qual_type,
359                    int kind);
360
361    //------------------------------------------------------------------
362    // C++ Base Classes
363    //------------------------------------------------------------------
364
365    clang::CXXBaseSpecifier *
366    CreateBaseClassSpecifier (void * base_class_type,
367                              lldb::AccessType access,
368                              bool is_virtual,
369                              bool base_of_class);
370
371    static void
372    DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes,
373                               unsigned num_base_classes);
374
375    bool
376    SetBaseClassesForClassType (void * class_clang_type,
377                                clang::CXXBaseSpecifier const * const *base_classes,
378                                unsigned num_base_classes);
379
380    //------------------------------------------------------------------
381    // DeclContext Functions
382    //------------------------------------------------------------------
383
384    static clang::DeclContext *
385    GetDeclContextForType (void * qual_type);
386
387    //------------------------------------------------------------------
388    // Namespace Declarations
389    //------------------------------------------------------------------
390
391    clang::NamespaceDecl *
392    GetUniqueNamespaceDeclaration (const char *name,
393                                   const Declaration &decl,
394                                   clang::DeclContext *decl_ctx);
395
396    //------------------------------------------------------------------
397    // Function Types
398    //------------------------------------------------------------------
399
400    clang::FunctionDecl *
401    CreateFunctionDeclaration (const char *name,
402                               void * function_Type,
403                               int storage,
404                               bool is_inline);
405
406    static void *
407    CreateFunctionType (clang::ASTContext *ast_context,
408                        void *result_type,
409                        void **args,
410                        unsigned num_args,
411                        bool is_variadic,
412                        unsigned type_quals);
413
414    void *
415    CreateFunctionType (void *result_type,
416                        void **args,
417                        unsigned num_args,
418                        bool is_variadic,
419                        unsigned type_quals)
420    {
421        return ClangASTContext::CreateFunctionType(getASTContext(),
422                                                   result_type,
423                                                   args,
424                                                   num_args,
425                                                   is_variadic,
426                                                   type_quals);
427    }
428
429    clang::ParmVarDecl *
430    CreateParameterDeclaration (const char *name,
431                               void *param_type,
432                               int storage);
433
434    void
435    SetFunctionParameters (clang::FunctionDecl *function_decl,
436                           clang::ParmVarDecl **params,
437                           unsigned num_params);
438
439    //------------------------------------------------------------------
440    // Array Types
441    //------------------------------------------------------------------
442
443    void *
444    CreateArrayType (void * element_type,
445                     size_t element_count,
446                     uint32_t bit_stride);
447
448    //------------------------------------------------------------------
449    // Tag Declarations
450    //------------------------------------------------------------------
451    bool
452    StartTagDeclarationDefinition (void * qual_type);
453
454    bool
455    CompleteTagDeclarationDefinition (void * qual_type);
456
457    //------------------------------------------------------------------
458    // Enumeration Types
459    //------------------------------------------------------------------
460    void *
461    CreateEnumerationType (const Declaration &decl, const char *name, void *integer_qual_type);
462
463    bool
464    AddEnumerationValueToEnumerationType (void * enum_qual_type,
465                                          void * enumerator_qual_type,
466                                          const Declaration &decl,
467                                          const char *name,
468                                          int64_t enum_value,
469                                          uint32_t enum_value_bit_size);
470
471    //------------------------------------------------------------------
472    // Pointers & References
473    //------------------------------------------------------------------
474    void *
475    CreatePointerType (void *clang_type);
476
477    void *
478    CreateLValueReferenceType (void *clang_type);
479
480    void *
481    CreateRValueReferenceType (void *clang_type);
482
483    void *
484    CreateMemberPointerType (void * clang_pointee_type,
485                             void * clang_class_type);
486
487    size_t
488    GetPointerBitSize ();
489
490    static bool
491    IsIntegerType (void *clang_type, bool &is_signed);
492
493    static bool
494    IsPointerType (void *clang_type, void **target_type = NULL);
495
496    static bool
497    IsPointerOrReferenceType (void *clang_type, void **target_type = NULL);
498
499    static bool
500    IsCStringType (void *clang_type, uint32_t &length);
501
502    static bool
503    IsFunctionPointerType (void *clang_type);
504
505    static bool
506    IsArrayType (void *clang_type, void **member_type = NULL, uint64_t *size = NULL);
507
508    //------------------------------------------------------------------
509    // Typedefs
510    //------------------------------------------------------------------
511    void *
512    CreateTypedefType (const char *name,
513                       void *clang_type,
514                       clang::DeclContext *decl_ctx);
515
516    //------------------------------------------------------------------
517    // Type names
518    //------------------------------------------------------------------
519    static std::string
520    GetTypeName(void *clang_type);
521
522    static bool
523    IsFloatingPointType (void *clang_type, uint32_t &count, bool &is_complex);
524
525    static bool
526    IsCXXClassType (void *clang_type);
527
528    static bool
529    IsObjCClassType (void *clang_type);
530
531    //static bool
532    //ConvertFloatValueToString (clang::ASTContext *ast_context,
533    //                           void *clang_type,
534    //                           const uint8_t* bytes,
535    //                           size_t byte_size,
536    //                           int apint_byte_order,
537    //                           std::string &float_str);
538
539    static size_t
540    ConvertStringToFloatValue (clang::ASTContext *ast_context,
541                               void *clang_type,
542                               const char *s,
543                               uint8_t *dst,
544                               size_t dst_size);
545
546    //------------------------------------------------------------------
547    // Qualifiers
548    //------------------------------------------------------------------
549    static unsigned
550    GetTypeQualifiers(void *clang_type);
551protected:
552    //------------------------------------------------------------------
553    // Classes that inherit from ClangASTContext can see and modify these
554    //------------------------------------------------------------------
555    std::string                             m_target_triple;
556    std::auto_ptr<clang::ASTContext>        m_ast_context_ap;
557    std::auto_ptr<clang::LangOptions>       m_language_options_ap;
558    std::auto_ptr<clang::SourceManager>     m_source_manager_ap;
559    std::auto_ptr<clang::Diagnostic>        m_diagnostic_ap;
560    std::auto_ptr<clang::TargetOptions>     m_target_options_ap;
561    std::auto_ptr<clang::TargetInfo>        m_target_info_ap;
562    std::auto_ptr<clang::IdentifierTable>   m_identifier_table_ap;
563    std::auto_ptr<clang::SelectorTable>     m_selector_table_ap;
564    std::auto_ptr<clang::Builtin::Context>  m_builtins_ap;
565
566private:
567    //------------------------------------------------------------------
568    // For ClangASTContext only
569    //------------------------------------------------------------------
570    ClangASTContext(const ClangASTContext&);
571    const ClangASTContext& operator=(const ClangASTContext&);
572};
573
574} // namespace lldb_private
575
576#endif  // liblldb_ClangASTContext_h_
577