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