ClangASTContext.h revision 8c6907c4daa74a8ebc418ec6eed151b68bda6321
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#include "llvm/ADT/OwningPtr.h"
22
23// Project includes
24#include "lldb/lldb-enumerations.h"
25#include "lldb/Core/ClangForward.h"
26#include "lldb/Symbol/ClangASTType.h"
27
28namespace lldb_private {
29
30class Declaration;
31
32class ClangASTContext
33{
34public:
35    enum {
36        eTypeHasChildren        = (1u <<  0),
37        eTypeHasValue           = (1u <<  1),
38        eTypeIsArray            = (1u <<  2),
39        eTypeIsBlock            = (1u <<  3),
40        eTypeIsBuiltIn          = (1u <<  4),
41        eTypeIsClass            = (1u <<  5),
42        eTypeIsCPlusPlus        = (1u <<  6),
43        eTypeIsEnumeration      = (1u <<  7),
44        eTypeIsFuncPrototype    = (1u <<  8),
45        eTypeIsMember           = (1u <<  9),
46        eTypeIsObjC             = (1u << 10),
47        eTypeIsPointer          = (1u << 11),
48        eTypeIsReference        = (1u << 12),
49        eTypeIsStructUnion      = (1u << 13),
50        eTypeIsTemplate         = (1u << 14),
51        eTypeIsTypedef          = (1u << 15),
52        eTypeIsVector           = (1u << 16)
53    };
54
55    typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *);
56    typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton, clang::ObjCInterfaceDecl *);
57
58    //------------------------------------------------------------------
59    // Constructors and Destructors
60    //------------------------------------------------------------------
61    ClangASTContext (const char *triple = NULL);
62
63    ~ClangASTContext();
64
65    clang::ASTContext *
66    getASTContext();
67
68    clang::Builtin::Context *
69    getBuiltinContext();
70
71    clang::IdentifierTable *
72    getIdentifierTable();
73
74    clang::LangOptions *
75    getLanguageOptions();
76
77    clang::SelectorTable *
78    getSelectorTable();
79
80    clang::FileManager *
81    getFileManager();
82
83    clang::SourceManager *
84    getSourceManager();
85
86    clang::Diagnostic *
87    getDiagnostic();
88
89    clang::DiagnosticClient *
90    getDiagnosticClient();
91
92    clang::TargetOptions *
93    getTargetOptions();
94
95    clang::TargetInfo *
96    getTargetInfo();
97
98    void
99    Clear();
100
101    const char *
102    GetTargetTriple ();
103
104    void
105    SetTargetTriple (const char *target_triple);
106
107    void
108    SetArchitecture (const ArchSpec &arch);
109
110    bool
111    HasExternalSource ();
112
113    void
114    SetExternalSource (llvm::OwningPtr<clang::ExternalASTSource> &ast_source_ap);
115
116    void
117    RemoveExternalSource ();
118
119    bool
120    GetCompleteType (lldb::clang_type_t clang_type);
121
122    static bool
123    GetCompleteType (clang::ASTContext *ast,
124                     lldb::clang_type_t clang_type);
125
126    //------------------------------------------------------------------
127    // Basic Types
128    //------------------------------------------------------------------
129
130    lldb::clang_type_t
131    GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding,
132                                          uint32_t bit_size);
133
134    static lldb::clang_type_t
135    GetBuiltinTypeForEncodingAndBitSize (clang::ASTContext *ast,
136                                         lldb::Encoding encoding,
137                                         uint32_t bit_size);
138
139    lldb::clang_type_t
140    GetBuiltinTypeForDWARFEncodingAndBitSize (
141        const char *type_name,
142        uint32_t dw_ate,
143        uint32_t bit_size);
144
145    static lldb::clang_type_t
146    GetBuiltInType_void(clang::ASTContext *ast);
147
148    lldb::clang_type_t
149    GetBuiltInType_void()
150    {
151        return GetBuiltInType_void(getASTContext());
152    }
153
154    lldb::clang_type_t
155    GetBuiltInType_bool();
156
157    lldb::clang_type_t
158    GetBuiltInType_objc_id();
159
160    lldb::clang_type_t
161    GetBuiltInType_objc_Class();
162
163    static lldb::clang_type_t
164    GetUnknownAnyType(clang::ASTContext *ast);
165
166    lldb::clang_type_t
167    GetUnknownAnyType()
168    {
169        return ClangASTContext::GetUnknownAnyType(getASTContext());
170    }
171
172    lldb::clang_type_t
173    GetBuiltInType_objc_selector();
174
175    lldb::clang_type_t
176    GetCStringType(bool is_const);
177
178    lldb::clang_type_t
179    GetVoidPtrType(bool is_const);
180
181    static lldb::clang_type_t
182    GetVoidPtrType(clang::ASTContext *ast, bool is_const);
183
184    static lldb::clang_type_t
185    CopyType(clang::ASTContext *dest_context,
186             clang::ASTContext *source_context,
187             lldb::clang_type_t clang_type);
188
189    static clang::Decl *
190    CopyDecl (clang::ASTContext *dest_context,
191              clang::ASTContext *source_context,
192              clang::Decl *source_decl);
193
194    static bool
195    AreTypesSame(clang::ASTContext *ast,
196                 lldb::clang_type_t type1,
197                 lldb::clang_type_t type2);
198
199    bool
200    AreTypesSame(lldb::clang_type_t type1,
201                 lldb::clang_type_t type2)
202    {
203        return ClangASTContext::AreTypesSame(getASTContext(), type1, type2);
204    }
205
206
207    lldb::clang_type_t
208    GetTypeForDecl (clang::TagDecl *decl);
209
210    lldb::clang_type_t
211    GetTypeForDecl (clang::ObjCInterfaceDecl *objc_decl);
212
213    //------------------------------------------------------------------
214    // CVR modifiers
215    //------------------------------------------------------------------
216
217    static lldb::clang_type_t
218    AddConstModifier (lldb::clang_type_t clang_type);
219
220    static lldb::clang_type_t
221    AddRestrictModifier (lldb::clang_type_t clang_type);
222
223    static lldb::clang_type_t
224    AddVolatileModifier (lldb::clang_type_t clang_type);
225
226    //------------------------------------------------------------------
227    // Structure, Unions, Classes
228    //------------------------------------------------------------------
229
230    lldb::clang_type_t
231    CreateRecordType (const char *name,
232                      int kind,
233                      clang::DeclContext *decl_ctx,
234                      lldb::LanguageType language);
235
236    static bool
237    AddFieldToRecordType (clang::ASTContext *ast,
238                          lldb::clang_type_t record_qual_type,
239                          const char *name,
240                          lldb::clang_type_t field_type,
241                          lldb::AccessType access,
242                          uint32_t bitfield_bit_size);
243
244    bool
245    AddFieldToRecordType (lldb::clang_type_t record_qual_type,
246                          const char *name,
247                          lldb::clang_type_t field_type,
248                          lldb::AccessType access,
249                          uint32_t bitfield_bit_size)
250    {
251        return ClangASTContext::AddFieldToRecordType (getASTContext(),
252                                                      record_qual_type,
253                                                      name,
254                                                      field_type,
255                                                      access,
256                                                      bitfield_bit_size);
257    }
258
259    static clang::CXXMethodDecl *
260    AddMethodToCXXRecordType (clang::ASTContext *ast,
261                              lldb::clang_type_t record_opaque_type,
262                              const char *name,
263                              lldb::clang_type_t method_type,
264                              lldb::AccessType access,
265                              bool is_virtual,
266                              bool is_static,
267                              bool is_inline,
268                              bool is_explicit);
269
270    clang::CXXMethodDecl *
271    AddMethodToCXXRecordType (lldb::clang_type_t record_opaque_type,
272                              const char *name,
273                              lldb::clang_type_t method_type,
274                              lldb::AccessType access,
275                              bool is_virtual,
276                              bool is_static,
277                              bool is_inline,
278                              bool is_explicit)
279
280    {
281        return ClangASTContext::AddMethodToCXXRecordType (getASTContext(),
282                                                          record_opaque_type,
283                                                          name,
284                                                          method_type,
285                                                          access,
286                                                          is_virtual,
287                                                          is_static,
288                                                          is_inline,
289                                                          is_explicit);
290    }
291
292    static bool
293    CheckOverloadedOperatorKindParameterCount (uint32_t op_kind,
294                                               uint32_t num_params);
295
296    bool
297    FieldIsBitfield (clang::FieldDecl* field,
298                     uint32_t& bitfield_bit_size);
299
300    static bool
301    FieldIsBitfield (clang::ASTContext *ast,
302                     clang::FieldDecl* field,
303                     uint32_t& bitfield_bit_size);
304
305    static bool
306    RecordHasFields (const clang::RecordDecl *record_decl);
307
308    void
309    SetDefaultAccessForRecordFields (lldb::clang_type_t clang_type,
310                                     int default_accessibility,
311                                     int *assigned_accessibilities,
312                                     size_t num_assigned_accessibilities);
313
314    lldb::clang_type_t
315    CreateObjCClass (const char *name,
316                     clang::DeclContext *decl_ctx,
317                     bool isForwardDecl,
318                     bool isInternal);
319
320    static bool
321    AddObjCClassIVar (clang::ASTContext *ast,
322                      lldb::clang_type_t class_opaque_type,
323                      const char *name,
324                      lldb::clang_type_t ivar_opaque_type,
325                      lldb::AccessType access,
326                      uint32_t bitfield_bit_size,
327                      bool isSynthesized);
328
329    bool
330    AddObjCClassIVar (lldb::clang_type_t class_opaque_type,
331                      const char *name,
332                      lldb::clang_type_t ivar_opaque_type,
333                      lldb::AccessType access,
334                      uint32_t bitfield_bit_size,
335                      bool isSynthesized)
336    {
337        return ClangASTContext::AddObjCClassIVar (getASTContext(),
338                                                  class_opaque_type,
339                                                  name,
340                                                  ivar_opaque_type,
341                                                  access,
342                                                  bitfield_bit_size,
343                                                  isSynthesized);
344    }
345
346    bool
347    SetObjCSuperClass (lldb::clang_type_t class_clang_type,
348                       lldb::clang_type_t superclass_clang_type);
349
350    static bool
351    ObjCTypeHasIVars (lldb::clang_type_t class_clang_type, bool check_superclass);
352
353    static bool
354    ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl,
355                      bool check_superclass);
356
357
358    static clang::ObjCMethodDecl *
359    AddMethodToObjCObjectType (clang::ASTContext *ast,
360                               lldb::clang_type_t class_opaque_type,
361                               const char *name,  // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
362                               lldb::clang_type_t method_opaque_type,
363                               lldb::AccessType access);
364
365    clang::ObjCMethodDecl *
366    AddMethodToObjCObjectType (lldb::clang_type_t class_opaque_type,
367                               const char *name,  // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
368                               lldb::clang_type_t method_opaque_type,
369                               lldb::AccessType access)
370    {
371        return AddMethodToObjCObjectType (getASTContext(),
372                                          class_opaque_type,
373                                          name,
374                                          method_opaque_type,
375                                          access);
376    }
377
378    static bool
379    SetHasExternalStorage (lldb::clang_type_t clang_type, bool has_extern);
380
381    //------------------------------------------------------------------
382    // Aggregate Types
383    //------------------------------------------------------------------
384    static bool
385    IsAggregateType (lldb::clang_type_t clang_type);
386
387    // Returns a mask containing bits from the ClangASTContext::eTypeXXX enumerations
388    static uint32_t
389    GetTypeInfo (lldb::clang_type_t clang_type,
390                     clang::ASTContext *ast,                // The AST for clang_type (can be NULL)
391                     lldb::clang_type_t *pointee_or_element_type);  // (can be NULL)
392
393    static uint32_t
394    GetNumChildren (clang::ASTContext *ast,
395                    lldb::clang_type_t clang_type,
396                    bool omit_empty_base_classes);
397
398    static uint32_t
399    GetNumPointeeChildren (lldb::clang_type_t clang_type);
400
401    lldb::clang_type_t
402    GetChildClangTypeAtIndex (const char *parent_name,
403                              lldb::clang_type_t  parent_clang_type,
404                              uint32_t idx,
405                              bool transparent_pointers,
406                              bool omit_empty_base_classes,
407                              std::string& child_name,
408                              uint32_t &child_byte_size,
409                              int32_t &child_byte_offset,
410                              uint32_t &child_bitfield_bit_size,
411                              uint32_t &child_bitfield_bit_offset,
412                              bool &child_is_base_class,
413                              bool &child_is_deref_of_parent);
414
415    static lldb::clang_type_t
416    GetChildClangTypeAtIndex (clang::ASTContext *ast,
417                              const char *parent_name,
418                              lldb::clang_type_t  parent_clang_type,
419                              uint32_t idx,
420                              bool transparent_pointers,
421                              bool omit_empty_base_classes,
422                              std::string& child_name,
423                              uint32_t &child_byte_size,
424                              int32_t &child_byte_offset,
425                              uint32_t &child_bitfield_bit_size,
426                              uint32_t &child_bitfield_bit_offset,
427                              bool &child_is_base_class,
428                              bool &child_is_deref_of_parent);
429
430    // Lookup a child given a name. This function will match base class names
431    // and member member names in "clang_type" only, not descendants.
432    static uint32_t
433    GetIndexOfChildWithName (clang::ASTContext *ast,
434                             lldb::clang_type_t clang_type,
435                             const char *name,
436                             bool omit_empty_base_classes);
437
438    // Lookup a child member given a name. This function will match member names
439    // only and will descend into "clang_type" children in search for the first
440    // member in this class, or any base class that matches "name".
441    // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
442    // so we catch all names that match a given child name, not just the first.
443    static size_t
444    GetIndexOfChildMemberWithName (clang::ASTContext *ast,
445                                   lldb::clang_type_t clang_type,
446                                   const char *name,
447                                   bool omit_empty_base_classes,
448                                   std::vector<uint32_t>& child_indexes);
449
450    //------------------------------------------------------------------
451    // clang::TagType
452    //------------------------------------------------------------------
453
454    bool
455    SetTagTypeKind (lldb::clang_type_t  tag_qual_type,
456                    int kind);
457
458    //------------------------------------------------------------------
459    // C++ Base Classes
460    //------------------------------------------------------------------
461
462    clang::CXXBaseSpecifier *
463    CreateBaseClassSpecifier (lldb::clang_type_t  base_class_type,
464                              lldb::AccessType access,
465                              bool is_virtual,
466                              bool base_of_class);
467
468    static void
469    DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes,
470                               unsigned num_base_classes);
471
472    bool
473    SetBaseClassesForClassType (lldb::clang_type_t  class_clang_type,
474                                clang::CXXBaseSpecifier const * const *base_classes,
475                                unsigned num_base_classes);
476
477    //------------------------------------------------------------------
478    // DeclContext Functions
479    //------------------------------------------------------------------
480
481    static clang::DeclContext *
482    GetDeclContextForType (lldb::clang_type_t  qual_type);
483
484    //------------------------------------------------------------------
485    // Namespace Declarations
486    //------------------------------------------------------------------
487
488    clang::NamespaceDecl *
489    GetUniqueNamespaceDeclaration (const char *name,
490                                   const Declaration &decl,
491                                   clang::DeclContext *decl_ctx);
492
493    //------------------------------------------------------------------
494    // Function Types
495    //------------------------------------------------------------------
496
497    clang::FunctionDecl *
498    CreateFunctionDeclaration (const char *name,
499                               lldb::clang_type_t  function_Type,
500                               int storage,
501                               bool is_inline);
502
503    static lldb::clang_type_t
504    CreateFunctionType (clang::ASTContext *ast,
505                        lldb::clang_type_t result_type,
506                        lldb::clang_type_t *args,
507                        unsigned num_args,
508                        bool is_variadic,
509                        unsigned type_quals);
510
511    lldb::clang_type_t
512    CreateFunctionType (lldb::clang_type_t result_type,
513                        lldb::clang_type_t *args,
514                        unsigned num_args,
515                        bool is_variadic,
516                        unsigned type_quals)
517    {
518        return ClangASTContext::CreateFunctionType(getASTContext(),
519                                                   result_type,
520                                                   args,
521                                                   num_args,
522                                                   is_variadic,
523                                                   type_quals);
524    }
525
526    clang::ParmVarDecl *
527    CreateParameterDeclaration (const char *name,
528                               lldb::clang_type_t param_type,
529                               int storage);
530
531    void
532    SetFunctionParameters (clang::FunctionDecl *function_decl,
533                           clang::ParmVarDecl **params,
534                           unsigned num_params);
535
536    //------------------------------------------------------------------
537    // Array Types
538    //------------------------------------------------------------------
539
540    lldb::clang_type_t
541    CreateArrayType (lldb::clang_type_t  element_type,
542                     size_t element_count,
543                     uint32_t bit_stride);
544
545    //------------------------------------------------------------------
546    // Tag Declarations
547    //------------------------------------------------------------------
548    bool
549    StartTagDeclarationDefinition (lldb::clang_type_t  qual_type);
550
551    bool
552    CompleteTagDeclarationDefinition (lldb::clang_type_t  qual_type);
553
554    //------------------------------------------------------------------
555    // Enumeration Types
556    //------------------------------------------------------------------
557    lldb::clang_type_t
558    CreateEnumerationType (const char *name,
559                           clang::DeclContext *decl_ctx,
560                           const Declaration &decl,
561                           lldb::clang_type_t integer_qual_type);
562
563    static lldb::clang_type_t
564    GetEnumerationIntegerType (lldb::clang_type_t enum_clang_type);
565
566    bool
567    AddEnumerationValueToEnumerationType (lldb::clang_type_t  enum_qual_type,
568                                          lldb::clang_type_t  enumerator_qual_type,
569                                          const Declaration &decl,
570                                          const char *name,
571                                          int64_t enum_value,
572                                          uint32_t enum_value_bit_size);
573
574    //------------------------------------------------------------------
575    // Pointers & References
576    //------------------------------------------------------------------
577    lldb::clang_type_t
578    CreatePointerType (lldb::clang_type_t clang_type);
579
580    static lldb::clang_type_t
581    CreatePointerType (clang::ASTContext *ast,
582                       lldb::clang_type_t clang_type);
583
584    static lldb::clang_type_t
585    CreateLValueReferenceType (clang::ASTContext *ast_context,
586                               lldb::clang_type_t clang_type);
587
588    static lldb::clang_type_t
589    CreateRValueReferenceType (clang::ASTContext *ast_context,
590                               lldb::clang_type_t clang_type);
591
592    lldb::clang_type_t
593    CreateLValueReferenceType (lldb::clang_type_t clang_type)
594    {
595        return ClangASTContext::CreateLValueReferenceType(getASTContext(), clang_type);
596    }
597
598    lldb::clang_type_t
599    CreateRValueReferenceType (lldb::clang_type_t clang_type)
600    {
601        return ClangASTContext::CreateRValueReferenceType(getASTContext(), clang_type);
602    }
603
604    lldb::clang_type_t
605    CreateMemberPointerType (lldb::clang_type_t  clang_pointee_type,
606                             lldb::clang_type_t  clang_class_type);
607
608    uint32_t
609    GetPointerBitSize ();
610
611    static bool
612    IsIntegerType (lldb::clang_type_t clang_type, bool &is_signed);
613
614    static bool
615    IsPointerType (lldb::clang_type_t clang_type, lldb::clang_type_t *target_type = NULL);
616
617    static bool
618    IsPointerOrReferenceType (lldb::clang_type_t clang_type, lldb::clang_type_t *target_type = NULL);
619
620    static bool
621    IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast,
622                                    lldb::clang_type_t clang_type,
623                                    lldb::clang_type_t *target_type = NULL);
624
625    static bool
626    IsCStringType (lldb::clang_type_t clang_type, uint32_t &length);
627
628    static bool
629    IsFunctionPointerType (lldb::clang_type_t clang_type);
630
631    static bool
632    IsArrayType (lldb::clang_type_t clang_type, lldb::clang_type_t *member_type = NULL, uint64_t *size = NULL);
633
634    //------------------------------------------------------------------
635    // Typedefs
636    //------------------------------------------------------------------
637    lldb::clang_type_t
638    CreateTypedefType (const char *name,
639                       lldb::clang_type_t clang_type,
640                       clang::DeclContext *decl_ctx);
641
642    //------------------------------------------------------------------
643    // Type names
644    //------------------------------------------------------------------
645    static std::string
646    GetTypeName(lldb::clang_type_t clang_type);
647
648    static bool
649    IsFloatingPointType (lldb::clang_type_t clang_type, uint32_t &count, bool &is_complex);
650
651    static bool
652    GetCXXClassName (lldb::clang_type_t clang_type,
653                     std::string &class_name);
654
655    static bool
656    IsCXXClassType (lldb::clang_type_t clang_type);
657
658    static bool
659    IsObjCClassType (lldb::clang_type_t clang_type);
660
661    static bool
662    IsCharType (lldb::clang_type_t clang_type);
663
664    static size_t
665    GetArraySize (lldb::clang_type_t clang_type);
666
667    //static bool
668    //ConvertFloatValueToString (clang::ASTContext *ast,
669    //                           lldb::clang_type_t clang_type,
670    //                           const uint8_t* bytes,
671    //                           size_t byte_size,
672    //                           int apint_byte_order,
673    //                           std::string &float_str);
674
675    static size_t
676    ConvertStringToFloatValue (clang::ASTContext *ast,
677                               lldb::clang_type_t clang_type,
678                               const char *s,
679                               uint8_t *dst,
680                               size_t dst_size);
681
682    //------------------------------------------------------------------
683    // Qualifiers
684    //------------------------------------------------------------------
685    static unsigned
686    GetTypeQualifiers(lldb::clang_type_t clang_type);
687protected:
688    //------------------------------------------------------------------
689    // Classes that inherit from ClangASTContext can see and modify these
690    //------------------------------------------------------------------
691    std::string                             m_target_triple;
692    std::auto_ptr<clang::ASTContext>        m_ast_ap;
693    std::auto_ptr<clang::LangOptions>       m_language_options_ap;
694    std::auto_ptr<clang::FileManager>       m_file_manager_ap;
695    std::auto_ptr<clang::FileSystemOptions> m_file_system_options_ap;
696    std::auto_ptr<clang::SourceManager>     m_source_manager_ap;
697    std::auto_ptr<clang::Diagnostic>        m_diagnostic_ap;
698    std::auto_ptr<clang::DiagnosticClient>  m_diagnostic_client_ap;
699    std::auto_ptr<clang::TargetOptions>     m_target_options_ap;
700    std::auto_ptr<clang::TargetInfo>        m_target_info_ap;
701    std::auto_ptr<clang::IdentifierTable>   m_identifier_table_ap;
702    std::auto_ptr<clang::SelectorTable>     m_selector_table_ap;
703    std::auto_ptr<clang::Builtin::Context>  m_builtins_ap;
704    CompleteTagDeclCallback                 m_callback_tag_decl;
705    CompleteObjCInterfaceDeclCallback       m_callback_objc_decl;
706    void *                                  m_callback_baton;
707private:
708    //------------------------------------------------------------------
709    // For ClangASTContext only
710    //------------------------------------------------------------------
711    ClangASTContext(const ClangASTContext&);
712    const ClangASTContext& operator=(const ClangASTContext&);
713};
714
715} // namespace lldb_private
716
717#endif  // liblldb_ClangASTContext_h_
718