ClangASTContext.h revision bf8e42b9da0e1c6349a727d644ad37610b00d556
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    GetTypeInfoMask (lldb::clang_type_t clang_type);
330
331    static uint32_t
332    GetNumChildren (lldb::clang_type_t clang_type,
333                    bool omit_empty_base_classes);
334
335    lldb::clang_type_t
336    GetChildClangTypeAtIndex (const char *parent_name,
337                              lldb::clang_type_t  parent_clang_type,
338                              uint32_t idx,
339                              bool transparent_pointers,
340                              bool omit_empty_base_classes,
341                              std::string& child_name,
342                              uint32_t &child_byte_size,
343                              int32_t &child_byte_offset,
344                              uint32_t &child_bitfield_bit_size,
345                              uint32_t &child_bitfield_bit_offset,
346                              bool &child_is_base_class);
347
348    static lldb::clang_type_t
349    GetChildClangTypeAtIndex (clang::ASTContext *ast_context,
350                              const char *parent_name,
351                              lldb::clang_type_t  parent_clang_type,
352                              uint32_t idx,
353                              bool transparent_pointers,
354                              bool omit_empty_base_classes,
355                              std::string& child_name,
356                              uint32_t &child_byte_size,
357                              int32_t &child_byte_offset,
358                              uint32_t &child_bitfield_bit_size,
359                              uint32_t &child_bitfield_bit_offset,
360                              bool &child_is_base_class);
361
362    // Lookup a child given a name. This function will match base class names
363    // and member member names in "clang_type" only, not descendants.
364    static uint32_t
365    GetIndexOfChildWithName (clang::ASTContext *ast_context,
366                             lldb::clang_type_t clang_type,
367                             const char *name,
368                             bool omit_empty_base_classes);
369
370    // Lookup a child member given a name. This function will match member names
371    // only and will descend into "clang_type" children in search for the first
372    // member in this class, or any base class that matches "name".
373    // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
374    // so we catch all names that match a given child name, not just the first.
375    static size_t
376    GetIndexOfChildMemberWithName (clang::ASTContext *ast_context,
377                                   lldb::clang_type_t clang_type,
378                                   const char *name,
379                                   bool omit_empty_base_classes,
380                                   std::vector<uint32_t>& child_indexes);
381
382    //------------------------------------------------------------------
383    // clang::TagType
384    //------------------------------------------------------------------
385
386    bool
387    SetTagTypeKind (lldb::clang_type_t  tag_qual_type,
388                    int kind);
389
390    //------------------------------------------------------------------
391    // C++ Base Classes
392    //------------------------------------------------------------------
393
394    clang::CXXBaseSpecifier *
395    CreateBaseClassSpecifier (lldb::clang_type_t  base_class_type,
396                              lldb::AccessType access,
397                              bool is_virtual,
398                              bool base_of_class);
399
400    static void
401    DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes,
402                               unsigned num_base_classes);
403
404    bool
405    SetBaseClassesForClassType (lldb::clang_type_t  class_clang_type,
406                                clang::CXXBaseSpecifier const * const *base_classes,
407                                unsigned num_base_classes);
408
409    //------------------------------------------------------------------
410    // DeclContext Functions
411    //------------------------------------------------------------------
412
413    static clang::DeclContext *
414    GetDeclContextForType (lldb::clang_type_t  qual_type);
415
416    //------------------------------------------------------------------
417    // Namespace Declarations
418    //------------------------------------------------------------------
419
420    clang::NamespaceDecl *
421    GetUniqueNamespaceDeclaration (const char *name,
422                                   const Declaration &decl,
423                                   clang::DeclContext *decl_ctx);
424
425    //------------------------------------------------------------------
426    // Function Types
427    //------------------------------------------------------------------
428
429    clang::FunctionDecl *
430    CreateFunctionDeclaration (const char *name,
431                               lldb::clang_type_t  function_Type,
432                               int storage,
433                               bool is_inline);
434
435    static lldb::clang_type_t
436    CreateFunctionType (clang::ASTContext *ast_context,
437                        lldb::clang_type_t result_type,
438                        lldb::clang_type_t *args,
439                        unsigned num_args,
440                        bool is_variadic,
441                        unsigned type_quals);
442
443    lldb::clang_type_t
444    CreateFunctionType (lldb::clang_type_t result_type,
445                        lldb::clang_type_t *args,
446                        unsigned num_args,
447                        bool is_variadic,
448                        unsigned type_quals)
449    {
450        return ClangASTContext::CreateFunctionType(getASTContext(),
451                                                   result_type,
452                                                   args,
453                                                   num_args,
454                                                   is_variadic,
455                                                   type_quals);
456    }
457
458    clang::ParmVarDecl *
459    CreateParameterDeclaration (const char *name,
460                               lldb::clang_type_t param_type,
461                               int storage);
462
463    void
464    SetFunctionParameters (clang::FunctionDecl *function_decl,
465                           clang::ParmVarDecl **params,
466                           unsigned num_params);
467
468    //------------------------------------------------------------------
469    // Array Types
470    //------------------------------------------------------------------
471
472    lldb::clang_type_t
473    CreateArrayType (lldb::clang_type_t  element_type,
474                     size_t element_count,
475                     uint32_t bit_stride);
476
477    //------------------------------------------------------------------
478    // Tag Declarations
479    //------------------------------------------------------------------
480    bool
481    StartTagDeclarationDefinition (lldb::clang_type_t  qual_type);
482
483    bool
484    CompleteTagDeclarationDefinition (lldb::clang_type_t  qual_type);
485
486    //------------------------------------------------------------------
487    // Enumeration Types
488    //------------------------------------------------------------------
489    lldb::clang_type_t
490    CreateEnumerationType (const Declaration &decl, const char *name, lldb::clang_type_t integer_qual_type);
491
492    static lldb::clang_type_t
493    GetEnumerationIntegerType (lldb::clang_type_t enum_clang_type);
494
495    bool
496    AddEnumerationValueToEnumerationType (lldb::clang_type_t  enum_qual_type,
497                                          lldb::clang_type_t  enumerator_qual_type,
498                                          const Declaration &decl,
499                                          const char *name,
500                                          int64_t enum_value,
501                                          uint32_t enum_value_bit_size);
502
503    //------------------------------------------------------------------
504    // Pointers & References
505    //------------------------------------------------------------------
506    lldb::clang_type_t
507    CreatePointerType (lldb::clang_type_t clang_type);
508
509    lldb::clang_type_t
510    CreateLValueReferenceType (lldb::clang_type_t clang_type);
511
512    lldb::clang_type_t
513    CreateRValueReferenceType (lldb::clang_type_t clang_type);
514
515    lldb::clang_type_t
516    CreateMemberPointerType (lldb::clang_type_t  clang_pointee_type,
517                             lldb::clang_type_t  clang_class_type);
518
519    size_t
520    GetPointerBitSize ();
521
522    static bool
523    IsIntegerType (lldb::clang_type_t clang_type, bool &is_signed);
524
525    static bool
526    IsPointerType (lldb::clang_type_t clang_type, lldb::clang_type_t *target_type = NULL);
527
528    static bool
529    IsPointerOrReferenceType (lldb::clang_type_t clang_type, lldb::clang_type_t *target_type = NULL);
530
531    static bool
532    IsCStringType (lldb::clang_type_t clang_type, uint32_t &length);
533
534    static bool
535    IsFunctionPointerType (lldb::clang_type_t clang_type);
536
537    static bool
538    IsArrayType (lldb::clang_type_t clang_type, lldb::clang_type_t *member_type = NULL, uint64_t *size = NULL);
539
540    //------------------------------------------------------------------
541    // Typedefs
542    //------------------------------------------------------------------
543    lldb::clang_type_t
544    CreateTypedefType (const char *name,
545                       lldb::clang_type_t clang_type,
546                       clang::DeclContext *decl_ctx);
547
548    //------------------------------------------------------------------
549    // Type names
550    //------------------------------------------------------------------
551    static std::string
552    GetTypeName(lldb::clang_type_t clang_type);
553
554    static bool
555    IsFloatingPointType (lldb::clang_type_t clang_type, uint32_t &count, bool &is_complex);
556
557    static bool
558    GetCXXClassName (lldb::clang_type_t clang_type,
559                     std::string &class_name);
560
561    static bool
562    IsCXXClassType (lldb::clang_type_t clang_type);
563
564    static bool
565    IsObjCClassType (lldb::clang_type_t clang_type);
566
567    //static bool
568    //ConvertFloatValueToString (clang::ASTContext *ast_context,
569    //                           lldb::clang_type_t clang_type,
570    //                           const uint8_t* bytes,
571    //                           size_t byte_size,
572    //                           int apint_byte_order,
573    //                           std::string &float_str);
574
575    static size_t
576    ConvertStringToFloatValue (clang::ASTContext *ast_context,
577                               lldb::clang_type_t clang_type,
578                               const char *s,
579                               uint8_t *dst,
580                               size_t dst_size);
581
582    //------------------------------------------------------------------
583    // Qualifiers
584    //------------------------------------------------------------------
585    static unsigned
586    GetTypeQualifiers(lldb::clang_type_t clang_type);
587protected:
588    //------------------------------------------------------------------
589    // Classes that inherit from ClangASTContext can see and modify these
590    //------------------------------------------------------------------
591    std::string                             m_target_triple;
592    std::auto_ptr<clang::ASTContext>        m_ast_context_ap;
593    std::auto_ptr<clang::LangOptions>       m_language_options_ap;
594    std::auto_ptr<clang::SourceManager>     m_source_manager_ap;
595    std::auto_ptr<clang::Diagnostic>        m_diagnostic_ap;
596    std::auto_ptr<clang::TargetOptions>     m_target_options_ap;
597    std::auto_ptr<clang::TargetInfo>        m_target_info_ap;
598    std::auto_ptr<clang::IdentifierTable>   m_identifier_table_ap;
599    std::auto_ptr<clang::SelectorTable>     m_selector_table_ap;
600    std::auto_ptr<clang::Builtin::Context>  m_builtins_ap;
601
602private:
603    //------------------------------------------------------------------
604    // For ClangASTContext only
605    //------------------------------------------------------------------
606    ClangASTContext(const ClangASTContext&);
607    const ClangASTContext& operator=(const ClangASTContext&);
608};
609
610} // namespace lldb_private
611
612#endif  // liblldb_ClangASTContext_h_
613