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