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#include <stdint.h>
15
16// C++ Includes
17#include <string>
18#include <vector>
19
20// Other libraries and framework includes
21#include "llvm/ADT/OwningPtr.h"
22#include "llvm/ADT/SmallVector.h"
23#include "clang/AST/TemplateBase.h"
24
25
26// Project includes
27#include "lldb/lldb-enumerations.h"
28#include "lldb/Core/ClangForward.h"
29#include "lldb/Symbol/ClangASTType.h"
30
31namespace lldb_private {
32
33class Declaration;
34
35class ClangASTContext
36{
37public:
38    typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *);
39    typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton, clang::ObjCInterfaceDecl *);
40
41    //------------------------------------------------------------------
42    // Constructors and Destructors
43    //------------------------------------------------------------------
44    ClangASTContext (const char *triple = NULL);
45
46    ~ClangASTContext();
47
48    clang::ASTContext *
49    getASTContext();
50
51    clang::Builtin::Context *
52    getBuiltinContext();
53
54    clang::IdentifierTable *
55    getIdentifierTable();
56
57    clang::LangOptions *
58    getLanguageOptions();
59
60    clang::SelectorTable *
61    getSelectorTable();
62
63    clang::FileManager *
64    getFileManager();
65
66    clang::SourceManager *
67    getSourceManager();
68
69    clang::DiagnosticsEngine *
70    getDiagnosticsEngine();
71
72    clang::DiagnosticConsumer *
73    getDiagnosticConsumer();
74
75    clang::TargetOptions *
76    getTargetOptions();
77
78    clang::TargetInfo *
79    getTargetInfo();
80
81    void
82    Clear();
83
84    const char *
85    GetTargetTriple ();
86
87    void
88    SetTargetTriple (const char *target_triple);
89
90    void
91    SetArchitecture (const ArchSpec &arch);
92
93    bool
94    HasExternalSource ();
95
96    void
97    SetExternalSource (llvm::OwningPtr<clang::ExternalASTSource> &ast_source_ap);
98
99    void
100    RemoveExternalSource ();
101
102    bool
103    GetCompleteDecl (clang::Decl *decl)
104    {
105        return ClangASTContext::GetCompleteDecl(getASTContext(), decl);
106    }
107
108    static bool
109    GetCompleteDecl (clang::ASTContext *ast,
110                     clang::Decl *decl);
111
112    void SetMetadataAsUserID (const void *object,
113                              lldb::user_id_t user_id);
114
115    void SetMetadata (const void *object,
116                      ClangASTMetadata &meta_data)
117    {
118        SetMetadata(getASTContext(), object, meta_data);
119    }
120
121    static void
122    SetMetadata (clang::ASTContext *ast,
123                 const void *object,
124                 ClangASTMetadata &meta_data);
125
126    ClangASTMetadata *
127    GetMetadata (const void *object)
128    {
129        return GetMetadata(getASTContext(), object);
130    }
131
132    static ClangASTMetadata *
133    GetMetadata (clang::ASTContext *ast,
134                 const void *object);
135
136    //------------------------------------------------------------------
137    // Basic Types
138    //------------------------------------------------------------------
139    ClangASTType
140    GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding,
141                                          uint32_t bit_size);
142
143    static ClangASTType
144    GetBuiltinTypeForEncodingAndBitSize (clang::ASTContext *ast,
145                                         lldb::Encoding encoding,
146                                         uint32_t bit_size);
147
148    ClangASTType
149    GetBasicType (lldb::BasicType type);
150
151    static ClangASTType
152    GetBasicType (clang::ASTContext *ast, lldb::BasicType type);
153
154    static ClangASTType
155    GetBasicType (clang::ASTContext *ast, const ConstString &name);
156
157    static lldb::BasicType
158    GetBasicTypeEnumeration (const ConstString &name);
159
160    ClangASTType
161    GetBuiltinTypeForDWARFEncodingAndBitSize (
162        const char *type_name,
163        uint32_t dw_ate,
164        uint32_t bit_size);
165
166    ClangASTType
167    GetCStringType(bool is_const);
168
169    static ClangASTType
170    GetUnknownAnyType(clang::ASTContext *ast);
171
172    ClangASTType
173    GetUnknownAnyType()
174    {
175        return ClangASTContext::GetUnknownAnyType(getASTContext());
176    }
177
178    uint32_t
179    GetPointerByteSize ();
180
181    static clang::DeclContext *
182    GetTranslationUnitDecl (clang::ASTContext *ast);
183
184    clang::DeclContext *
185    GetTranslationUnitDecl ()
186    {
187        return GetTranslationUnitDecl (getASTContext());
188    }
189
190    static bool
191    GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
192                                      lldb::LanguageType &language,
193                                      bool &is_instance_method,
194                                      ConstString &language_object_name);
195
196    static ClangASTType
197    CopyType(clang::ASTContext *dest_context,
198             ClangASTType source_type);
199
200    static clang::Decl *
201    CopyDecl (clang::ASTContext *dest_context,
202              clang::ASTContext *source_context,
203              clang::Decl *source_decl);
204
205    static bool
206    AreTypesSame(ClangASTType type1,
207                 ClangASTType type2,
208                 bool ignore_qualifiers = false);
209
210    ClangASTType
211    GetTypeForDecl (clang::TagDecl *decl);
212
213    ClangASTType
214    GetTypeForDecl (clang::ObjCInterfaceDecl *objc_decl);
215
216    //------------------------------------------------------------------
217    // Structure, Unions, Classes
218    //------------------------------------------------------------------
219
220    static clang::AccessSpecifier
221    ConvertAccessTypeToAccessSpecifier (lldb::AccessType access);
222
223    static clang::AccessSpecifier
224    UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs);
225
226    static uint32_t
227    GetNumBaseClasses (const clang::CXXRecordDecl *cxx_record_decl,
228                       bool omit_empty_base_classes);
229
230    static uint32_t
231    GetIndexForRecordBase (const clang::RecordDecl *record_decl,
232                           const clang::CXXBaseSpecifier *base_spec,
233                           bool omit_empty_base_classes);
234
235    ClangASTType
236    CreateRecordType (clang::DeclContext *decl_ctx,
237                      lldb::AccessType access_type,
238                      const char *name,
239                      int kind,
240                      lldb::LanguageType language,
241                      ClangASTMetadata *metadata = NULL);
242
243    class TemplateParameterInfos
244    {
245    public:
246        bool
247        IsValid() const
248        {
249            if (args.empty())
250                return false;
251            return args.size() == names.size();
252        }
253
254        size_t
255        GetSize () const
256        {
257            if (IsValid())
258                return args.size();
259            return 0;
260        }
261
262        llvm::SmallVector<const char *, 8> names;
263        llvm::SmallVector<clang::TemplateArgument, 8> args;
264    };
265
266    clang::FunctionTemplateDecl *
267    CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
268                                clang::FunctionDecl *func_decl,
269                                const char *name,
270                                const TemplateParameterInfos &infos);
271
272    void
273    CreateFunctionTemplateSpecializationInfo (clang::FunctionDecl *func_decl,
274                                              clang::FunctionTemplateDecl *Template,
275                                              const TemplateParameterInfos &infos);
276
277    clang::ClassTemplateDecl *
278    CreateClassTemplateDecl (clang::DeclContext *decl_ctx,
279                             lldb::AccessType access_type,
280                             const char *class_name,
281                             int kind,
282                             const TemplateParameterInfos &infos);
283
284    clang::ClassTemplateSpecializationDecl *
285    CreateClassTemplateSpecializationDecl (clang::DeclContext *decl_ctx,
286                                           clang::ClassTemplateDecl *class_template_decl,
287                                           int kind,
288                                           const TemplateParameterInfos &infos);
289
290    ClangASTType
291    CreateClassTemplateSpecializationType (clang::ClassTemplateSpecializationDecl *class_template_specialization_decl);
292
293    static clang::DeclContext *
294    GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl);
295
296    static clang::DeclContext *
297    GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl);
298
299
300    static bool
301    CheckOverloadedOperatorKindParameterCount (uint32_t op_kind,
302                                               uint32_t num_params);
303
304    bool
305    FieldIsBitfield (clang::FieldDecl* field,
306                     uint32_t& bitfield_bit_size);
307
308    static bool
309    FieldIsBitfield (clang::ASTContext *ast,
310                     clang::FieldDecl* field,
311                     uint32_t& bitfield_bit_size);
312
313    static bool
314    RecordHasFields (const clang::RecordDecl *record_decl);
315
316
317    ClangASTType
318    CreateObjCClass (const char *name,
319                     clang::DeclContext *decl_ctx,
320                     bool isForwardDecl,
321                     bool isInternal,
322                     ClangASTMetadata *metadata = NULL);
323
324    // Returns a mask containing bits from the ClangASTContext::eTypeXXX enumerations
325
326
327    //------------------------------------------------------------------
328    // Namespace Declarations
329    //------------------------------------------------------------------
330
331    clang::NamespaceDecl *
332    GetUniqueNamespaceDeclaration (const char *name,
333                                   clang::DeclContext *decl_ctx);
334
335    //------------------------------------------------------------------
336    // Function Types
337    //------------------------------------------------------------------
338
339    clang::FunctionDecl *
340    CreateFunctionDeclaration (clang::DeclContext *decl_ctx,
341                               const char *name,
342                               const ClangASTType &function_Type,
343                               int storage,
344                               bool is_inline);
345
346    static ClangASTType
347    CreateFunctionType (clang::ASTContext *ast,
348                        const ClangASTType &result_type,
349                        const ClangASTType *args,
350                        unsigned num_args,
351                        bool is_variadic,
352                        unsigned type_quals);
353
354    ClangASTType
355    CreateFunctionType (const ClangASTType &result_type,
356                        const ClangASTType *args,
357                        unsigned num_args,
358                        bool is_variadic,
359                        unsigned type_quals)
360    {
361        return ClangASTContext::CreateFunctionType(getASTContext(),
362                                                   result_type,
363                                                   args,
364                                                   num_args,
365                                                   is_variadic,
366                                                   type_quals);
367    }
368
369    clang::ParmVarDecl *
370    CreateParameterDeclaration (const char *name,
371                                const ClangASTType &param_type,
372                                int storage);
373
374    void
375    SetFunctionParameters (clang::FunctionDecl *function_decl,
376                           clang::ParmVarDecl **params,
377                           unsigned num_params);
378
379    //------------------------------------------------------------------
380    // Array Types
381    //------------------------------------------------------------------
382
383    ClangASTType
384    CreateArrayType (const ClangASTType &element_type,
385                     size_t element_count,
386                     bool is_vector);
387
388    //------------------------------------------------------------------
389    // Enumeration Types
390    //------------------------------------------------------------------
391    ClangASTType
392    CreateEnumerationType (const char *name,
393                           clang::DeclContext *decl_ctx,
394                           const Declaration &decl,
395                           const ClangASTType &integer_qual_type);
396
397    //------------------------------------------------------------------
398    // Floating point functions
399    //------------------------------------------------------------------
400
401    ClangASTType
402    GetFloatTypeFromBitSize (size_t bit_size)
403    {
404        return GetFloatTypeFromBitSize (getASTContext(), bit_size);
405    }
406
407    static ClangASTType
408    GetFloatTypeFromBitSize (clang::ASTContext *ast,
409                             size_t bit_size);
410protected:
411    //------------------------------------------------------------------
412    // Classes that inherit from ClangASTContext can see and modify these
413    //------------------------------------------------------------------
414    std::string                                     m_target_triple;
415    std::unique_ptr<clang::ASTContext>              m_ast_ap;
416    std::unique_ptr<clang::LangOptions>             m_language_options_ap;
417    std::unique_ptr<clang::FileManager>             m_file_manager_ap;
418    std::unique_ptr<clang::FileSystemOptions>       m_file_system_options_ap;
419    std::unique_ptr<clang::SourceManager>           m_source_manager_ap;
420    std::unique_ptr<clang::DiagnosticsEngine>       m_diagnostics_engine_ap;
421    std::unique_ptr<clang::DiagnosticConsumer>      m_diagnostic_consumer_ap;
422    llvm::IntrusiveRefCntPtr<clang::TargetOptions>  m_target_options_rp;
423    std::unique_ptr<clang::TargetInfo>              m_target_info_ap;
424    std::unique_ptr<clang::IdentifierTable>         m_identifier_table_ap;
425    std::unique_ptr<clang::SelectorTable>           m_selector_table_ap;
426    std::unique_ptr<clang::Builtin::Context>        m_builtins_ap;
427    CompleteTagDeclCallback                         m_callback_tag_decl;
428    CompleteObjCInterfaceDeclCallback               m_callback_objc_decl;
429    void *                                          m_callback_baton;
430    uint32_t                                        m_pointer_byte_size;
431private:
432    //------------------------------------------------------------------
433    // For ClangASTContext only
434    //------------------------------------------------------------------
435    ClangASTContext(const ClangASTContext&);
436    const ClangASTContext& operator=(const ClangASTContext&);
437};
438
439} // namespace lldb_private
440
441#endif  // liblldb_ClangASTContext_h_
442