ClangASTContext.h revision 2ea8f27fe76e30ba8fff1f58a50ad3e409e8a8ba
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    //------------------------------------------------------------------
34    // Constructors and Destructors
35    //------------------------------------------------------------------
36    ClangASTContext(const char *target_triple);
37
38    ~ClangASTContext();
39
40    clang::ASTContext *
41    getASTContext();
42
43    clang::Builtin::Context *
44    getBuiltinContext();
45
46    clang::IdentifierTable *
47    getIdentifierTable();
48
49    clang::LangOptions *
50    getLanguageOptions();
51
52    clang::SelectorTable *
53    getSelectorTable();
54
55    clang::SourceManager *
56    getSourceManager();
57
58    clang::Diagnostic *
59    getDiagnostic();
60
61    clang::TargetOptions *
62    getTargetOptions();
63
64    clang::TargetInfo *
65    getTargetInfo();
66
67    void
68    Clear();
69
70    const char *
71    GetTargetTriple ();
72
73    void
74    SetTargetTriple (const char *target_triple);
75
76    //------------------------------------------------------------------
77    // Basic Types
78    //------------------------------------------------------------------
79
80    void *
81    GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding,
82                                          uint32_t bit_size);
83
84    static void *
85    GetBuiltinTypeForEncodingAndBitSize (clang::ASTContext *ast_context,
86                                         lldb::Encoding encoding,
87                                         uint32_t bit_size);
88
89    void *
90    GetBuiltinTypeForDWARFEncodingAndBitSize (
91        const char *type_name,
92        uint32_t dw_ate,
93        uint32_t bit_size);
94
95    void *
96    GetBuiltInType_void();
97
98    void *
99    GetBuiltInType_objc_id();
100
101    void *
102    GetBuiltInType_objc_Class();
103
104    void *
105    GetBuiltInType_objc_selector();
106
107    void *
108    GetCStringType(bool is_const);
109
110    void *
111    GetVoidPtrType(bool is_const);
112
113    static void *
114    GetVoidPtrType(clang::ASTContext *ast_context, bool is_const);
115
116    static void *
117    CopyType(clang::ASTContext *dest_context,
118             clang::ASTContext *source_context,
119             void *clang_type);
120
121    static bool
122    AreTypesSame(clang::ASTContext *ast_context,
123                 void *type1,
124                 void *type2);
125
126    bool
127    AreTypesSame(void *type1,
128                 void *type2)
129    {
130        return ClangASTContext::AreTypesSame(m_ast_context_ap.get(), type1, type2);
131    }
132
133    //------------------------------------------------------------------
134    // CVR modifiers
135    //------------------------------------------------------------------
136
137    static void *
138    AddConstModifier (void *clang_type);
139
140    static void *
141    AddRestrictModifier (void *clang_type);
142
143    static void *
144    AddVolatileModifier (void *clang_type);
145
146    //------------------------------------------------------------------
147    // Structure, Unions, Classes
148    //------------------------------------------------------------------
149
150    void *
151    CreateRecordType (const char *name,
152                      int kind,
153                      clang::DeclContext *decl_ctx,
154                      lldb::LanguageType language);
155
156    static bool
157    AddFieldToRecordType (clang::ASTContext *ast_context,
158                          void *record_qual_type,
159                          const char *name,
160                          void *field_type,
161                          lldb::AccessType access,
162                          uint32_t bitfield_bit_size);
163
164    bool
165    AddFieldToRecordType (void *record_qual_type,
166                          const char *name,
167                          void *field_type,
168                          lldb::AccessType access,
169                          uint32_t bitfield_bit_size)
170    {
171        return ClangASTContext::AddFieldToRecordType(m_ast_context_ap.get(),
172                                                     record_qual_type,
173                                                     name,
174                                                     field_type,
175                                                     access,
176                                                     bitfield_bit_size);
177    }
178
179    bool
180    FieldIsBitfield (clang::FieldDecl* field,
181                     uint32_t& bitfield_bit_size);
182
183    static bool
184    FieldIsBitfield (clang::ASTContext *ast_context,
185                     clang::FieldDecl* field,
186                     uint32_t& bitfield_bit_size);
187
188    static bool
189    RecordHasFields (const clang::RecordDecl *record_decl);
190
191    void
192    SetDefaultAccessForRecordFields (void *clang_type,
193                                     int default_accessibility,
194                                     int *assigned_accessibilities,
195                                     size_t num_assigned_accessibilities);
196
197    void *
198    CreateObjCClass (const char *name,
199                     clang::DeclContext *decl_ctx,
200                     bool isForwardDecl,
201                     bool isInternal);
202
203    static bool
204    AddObjCClassIVar (clang::ASTContext *ast_context,
205                      void *class_opaque_type,
206                      const char *name,
207                      void *ivar_opaque_type,
208                      lldb::AccessType access,
209                      uint32_t bitfield_bit_size,
210                      bool isSynthesized);
211
212    bool
213    AddObjCClassIVar (void *class_opaque_type,
214                      const char *name,
215                      void *ivar_opaque_type,
216                      lldb::AccessType access,
217                      uint32_t bitfield_bit_size,
218                      bool isSynthesized)
219    {
220        return ClangASTContext::AddObjCClassIVar (m_ast_context_ap.get(),
221                                                  class_opaque_type,
222                                                  name,
223                                                  ivar_opaque_type,
224                                                  access,
225                                                  bitfield_bit_size,
226                                                  isSynthesized);
227    }
228
229    bool
230    SetObjCSuperClass (void *class_clang_type,
231                       void *superclass_clang_type);
232
233    static bool
234    ObjCTypeHasIVars (void *class_clang_type, bool check_superclass);
235
236    static bool
237    ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl,
238                      bool check_superclass);
239
240
241    //------------------------------------------------------------------
242    // Aggregate Types
243    //------------------------------------------------------------------
244    static bool
245    IsAggregateType (void *clang_type);
246
247    static uint32_t
248    GetNumChildren (void *clang_type,
249                    bool omit_empty_base_classes);
250
251    void *
252    GetChildClangTypeAtIndex (const char *parent_name,
253                              void * parent_clang_type,
254                              uint32_t idx,
255                              bool transparent_pointers,
256                              bool omit_empty_base_classes,
257                              std::string& child_name,
258                              uint32_t &child_byte_size,
259                              int32_t &child_byte_offset,
260                              uint32_t &child_bitfield_bit_size,
261                              uint32_t &child_bitfield_bit_offset);
262
263    static void *
264    GetChildClangTypeAtIndex (clang::ASTContext *ast_context,
265                              const char *parent_name,
266                              void * parent_clang_type,
267                              uint32_t idx,
268                              bool transparent_pointers,
269                              bool omit_empty_base_classes,
270                              std::string& child_name,
271                              uint32_t &child_byte_size,
272                              int32_t &child_byte_offset,
273                              uint32_t &child_bitfield_bit_size,
274                              uint32_t &child_bitfield_bit_offset);
275
276    // Lookup a child given a name. This function will match base class names
277    // and member member names in "clang_type" only, not descendants.
278    static uint32_t
279    GetIndexOfChildWithName (clang::ASTContext *ast_context,
280                             void *clang_type,
281                             const char *name,
282                             bool omit_empty_base_classes);
283
284    // Lookup a child member given a name. This function will match member names
285    // only and will descend into "clang_type" children in search for the first
286    // member in this class, or any base class that matches "name".
287    // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
288    // so we catch all names that match a given child name, not just the first.
289    static size_t
290    GetIndexOfChildMemberWithName (clang::ASTContext *ast_context,
291                                   void *clang_type,
292                                   const char *name,
293                                   bool omit_empty_base_classes,
294                                   std::vector<uint32_t>& child_indexes);
295
296    //------------------------------------------------------------------
297    // clang::TagType
298    //------------------------------------------------------------------
299
300    bool
301    SetTagTypeKind (void * tag_qual_type,
302                    int kind);
303
304    //------------------------------------------------------------------
305    // C++ Base Classes
306    //------------------------------------------------------------------
307
308    clang::CXXBaseSpecifier *
309    CreateBaseClassSpecifier (void * base_class_type,
310                              lldb::AccessType access,
311                              bool is_virtual,
312                              bool base_of_class);
313
314    static void
315    DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes,
316                               unsigned num_base_classes);
317
318    bool
319    SetBaseClassesForClassType (void * class_clang_type,
320                                clang::CXXBaseSpecifier const * const *base_classes,
321                                unsigned num_base_classes);
322
323    //------------------------------------------------------------------
324    // DeclContext Functions
325    //------------------------------------------------------------------
326
327    static clang::DeclContext *
328    GetDeclContextForType (void * qual_type);
329
330    //------------------------------------------------------------------
331    // Namespace Declarations
332    //------------------------------------------------------------------
333
334    clang::NamespaceDecl *
335    GetUniqueNamespaceDeclaration (const char *name,
336                                   const Declaration &decl,
337                                   clang::DeclContext *decl_ctx);
338
339    //------------------------------------------------------------------
340    // Function Types
341    //------------------------------------------------------------------
342
343    clang::FunctionDecl *
344    CreateFunctionDeclaration (const char *name,
345                               void * function_Type,
346                               int storage,
347                               bool is_inline);
348
349    static void *
350    CreateFunctionType (clang::ASTContext *ast_context,
351                        void *result_type,
352                        void **args,
353                        unsigned num_args,
354                        bool is_variadic,
355                        unsigned type_quals);
356
357    void *
358    CreateFunctionType (void *result_type,
359                        void **args,
360                        unsigned num_args,
361                        bool is_variadic,
362                        unsigned type_quals)
363    {
364        return ClangASTContext::CreateFunctionType(m_ast_context_ap.get(),
365                                                   result_type,
366                                                   args,
367                                                   num_args,
368                                                   is_variadic,
369                                                   type_quals);
370    }
371
372    clang::ParmVarDecl *
373    CreateParameterDeclaration (const char *name,
374                               void *param_type,
375                               int storage);
376
377    void
378    SetFunctionParameters (clang::FunctionDecl *function_decl,
379                           clang::ParmVarDecl **params,
380                           unsigned num_params);
381
382    //------------------------------------------------------------------
383    // Array Types
384    //------------------------------------------------------------------
385
386    void *
387    CreateArrayType (void * element_type,
388                     size_t element_count,
389                     uint32_t bit_stride);
390
391    //------------------------------------------------------------------
392    // Tag Declarations
393    //------------------------------------------------------------------
394    bool
395    StartTagDeclarationDefinition (void * qual_type);
396
397    bool
398    CompleteTagDeclarationDefinition (void * qual_type);
399
400    //------------------------------------------------------------------
401    // Enumeration Types
402    //------------------------------------------------------------------
403    void *
404    CreateEnumerationType (const Declaration &decl, const char *name, void *integer_qual_type);
405
406    bool
407    AddEnumerationValueToEnumerationType (void * enum_qual_type,
408                                          void * enumerator_qual_type,
409                                          const Declaration &decl,
410                                          const char *name,
411                                          int64_t enum_value,
412                                          uint32_t enum_value_bit_size);
413
414    //------------------------------------------------------------------
415    // Pointers & References
416    //------------------------------------------------------------------
417    void *
418    CreatePointerType (void *clang_type);
419
420    void *
421    CreateLValueReferenceType (void *clang_type);
422
423    void *
424    CreateRValueReferenceType (void *clang_type);
425
426    void *
427    CreateMemberPointerType (void * clang_pointee_type,
428                             void * clang_class_type);
429
430    size_t
431    GetPointerBitSize ();
432
433    static bool
434    IsIntegerType (void *clang_type, bool &is_signed);
435
436    static bool
437    IsPointerType (void *clang_type, void **target_type = NULL);
438
439    static bool
440    IsPointerOrReferenceType (void *clang_type, void **target_type = NULL);
441
442    static bool
443    IsCStringType (void *clang_type, uint32_t &length);
444
445    static bool
446    IsFunctionPointerType (void *clang_type);
447
448    static bool
449    IsArrayType (void *clang_type, void **member_type = NULL, uint64_t *size = NULL);
450
451    //------------------------------------------------------------------
452    // Typedefs
453    //------------------------------------------------------------------
454    void *
455    CreateTypedefType (const char *name,
456                       void *clang_type,
457                       clang::DeclContext *decl_ctx);
458
459    //------------------------------------------------------------------
460    // Type names
461    //------------------------------------------------------------------
462    static std::string
463    GetTypeName(void *clang_type);
464
465    static bool
466    IsFloatingPointType (void *clang_type, uint32_t &count, bool &is_complex);
467
468    //static bool
469    //ConvertFloatValueToString (clang::ASTContext *ast_context,
470    //                           void *clang_type,
471    //                           const uint8_t* bytes,
472    //                           size_t byte_size,
473    //                           int apint_byte_order,
474    //                           std::string &float_str);
475
476    static size_t
477    ConvertStringToFloatValue (clang::ASTContext *ast_context,
478                               void *clang_type,
479                               const char *s,
480                               uint8_t *dst,
481                               size_t dst_size);
482
483protected:
484    //------------------------------------------------------------------
485    // Classes that inherit from ClangASTContext can see and modify these
486    //------------------------------------------------------------------
487    std::string                             m_target_triple;
488    std::auto_ptr<clang::ASTContext>        m_ast_context_ap;
489    std::auto_ptr<clang::LangOptions>       m_language_options_ap;
490    std::auto_ptr<clang::SourceManager>     m_source_manager_ap;
491    std::auto_ptr<clang::Diagnostic>        m_diagnostic_ap;
492    std::auto_ptr<clang::TargetOptions>     m_target_options_ap;
493    std::auto_ptr<clang::TargetInfo>        m_target_info_ap;
494    std::auto_ptr<clang::IdentifierTable>   m_identifier_table_ap;
495    std::auto_ptr<clang::SelectorTable>     m_selector_table_ap;
496    std::auto_ptr<clang::Builtin::Context>  m_builtins_ap;
497
498private:
499    //------------------------------------------------------------------
500    // For ClangASTContext only
501    //------------------------------------------------------------------
502    ClangASTContext(const ClangASTContext&);
503    const ClangASTContext& operator=(const ClangASTContext&);
504};
505
506} // namespace lldb_private
507
508#endif  // liblldb_ClangASTContext_h_
509