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