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